From: Nicolas Neuss
Subject: Initializing a (complex rational) array
Date: 
Message-ID: <87zlj55rpe.fsf@ma-patru.mathematik.uni-karlsruhe.de>
Hello,

consider 

 (make-array 4 :element-type '(complex rational) :initial-element 0)

SBCL initializes the array as I expected, however Allegro 8.1 (trial
edition) gives the error

Error: Initial element 0 is not of type (COMPLEX RATIONAL)

The Hyperspec says in "System class complex"

  "The type complex includes all mathematical complex numbers other than
   those included in the type rational. Complexes are expressed in
   Cartesian form with a real part and an imaginary part, each of which is
   a real. The real part and imaginary part are either both rational or
   both of the same float type. The imaginary part can be a float zero, but
   can never be a rational zero, for such a number is always represented by
   Common Lisp as a rational rather than a complex."

which I find quite strange and which might be interpreted as allowing
Allegro's behavior.  However, then it gets difficult to initialize a
(complex rational) array at all with 0, since (complex 0 0) or #C(0 0) both
return a simple integer 0.

What do you think?

Nicolas

From: Ariel Badichi
Subject: Re: Initializing a (complex rational) array
Date: 
Message-ID: <9af2ebf8-09f0-405f-baa6-6aa084ccbc1b@o40g2000prn.googlegroups.com>
On Dec 9, 4:49 pm, Nicolas Neuss <········@math.uni-karlsruhe.de>
wrote:
>
> Error: Initial element 0 is not of type (COMPLEX RATIONAL)
>
> What do you think?
>

The type specifier should be (OR COMPLEX RATIONAL).

Ariel
From: Tamas K Papp
Subject: Re: Initializing a (complex rational) array
Date: 
Message-ID: <6q7ulqFbbe84U1@mid.individual.net>
On Tue, 09 Dec 2008 07:35:58 -0800, Ariel Badichi wrote:

> On Dec 9, 4:49 pm, Nicolas Neuss <········@math.uni-karlsruhe.de> wrote:
>>
>> Error: Initial element 0 is not of type (COMPLEX RATIONAL)
>>
>> What do you think?
>>
>>
> The type specifier should be (OR COMPLEX RATIONAL).

Also, what do you expect to gain from the type declaration?

(upgraded-array-element-type '(or complex rational)) ; => T

Tamas
From: Nicolas Neuss
Subject: Re: Initializing a (complex rational) array
Date: 
Message-ID: <877i6985p5.fsf@ma-patru.mathematik.uni-karlsruhe.de>
Ariel Badichi <····@tidexsystems.com> writes:

> The type specifier should be (OR COMPLEX RATIONAL).

No, I wanted to have a complex number with rational real and imaginary
part.  Maybe such a beast could be interesting for some algebra
applications, although admittedly one does not really need a specialized
array in this case - computing with rationals is slow anyway:-)

Nicolas
From: Barry Margolin
Subject: Re: Initializing a (complex rational) array
Date: 
Message-ID: <barmar-F3E82D.23511109122008@mara100-84.onlink.net>
In article <··············@ma-patru.mathematik.uni-karlsruhe.de>,
 Nicolas Neuss <········@math.uni-karlsruhe.de> wrote:

> Ariel Badichi <····@tidexsystems.com> writes:
> 
> > The type specifier should be (OR COMPLEX RATIONAL).
> 
> No, I wanted to have a complex number with rational real and imaginary
> part.  Maybe such a beast could be interesting for some algebra
> applications, although admittedly one does not really need a specialized
> array in this case - computing with rationals is slow anyway:-)

But there's no such thing in CL as a complex with zero imaginary part.  
It would be like a bignum between 0 and MOST-POSITIVE-FIXNUM.

Some of CL's types are representational, not mathematical.

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: Nicolas Neuss
Subject: Re: Initializing a (complex rational) array
Date: 
Message-ID: <873agw491b.fsf@ma-patru.mathematik.uni-karlsruhe.de>
Barry Margolin <······@alum.mit.edu> writes:

> In article <··············@ma-patru.mathematik.uni-karlsruhe.de>,
>  Nicolas Neuss <········@math.uni-karlsruhe.de> wrote:
>
>> Ariel Badichi <····@tidexsystems.com> writes:
>> 
>> > The type specifier should be (OR COMPLEX RATIONAL).
>> 
>> No, I wanted to have a complex number with rational real and imaginary
>> part.  Maybe such a beast could be interesting for some algebra
>> applications, although admittedly one does not really need a specialized
>> array in this case - computing with rationals is slow anyway:-)
>
> But there's no such thing in CL as a complex with zero imaginary part.  
> It would be like a bignum between 0 and MOST-POSITIVE-FIXNUM.
>
> Some of CL's types are representational, not mathematical.

OK, thank you.  Yes, this makes sense.

Nicolas
From: Thomas M. Hermann
Subject: Re: Initializing a (complex rational) array
Date: 
Message-ID: <1e1758d1-5274-4f6e-adb5-482589251bc5@q26g2000prq.googlegroups.com>
On Dec 9, 8:49 am, Nicolas Neuss <········@math.uni-karlsruhe.de>
wrote:
> Hello,
>
> consider
>
>  (make-array 4 :element-type '(complex rational) :initial-element 0)
>
> SBCL initializes the array as I expected, however Allegro 8.1 (trial
> edition) gives the error
>
> Error: Initial element 0 is not of type (COMPLEX RATIONAL)
>
> The Hyperspec says in "System class complex"
>
>   "The type complex includes all mathematical complex numbers other than
>    those included in the type rational. Complexes are expressed in
>    Cartesian form with a real part and an imaginary part, each of which is
>    a real. The real part and imaginary part are either both rational or
>    both of the same float type. The imaginary part can be a float zero, but
>    can never be a rational zero, for such a number is always represented by
>    Common Lisp as a rational rather than a complex."
>
> which I find quite strange and which might be interpreted as allowing
> Allegro's behavior.  However, then it gets difficult to initialize a
> (complex rational) array at all with 0, since (complex 0 0) or #C(0 0) both
> return a simple integer 0.
>
> What do you think?
>
> Nicolas

In addition to the issue with how to represent #C(0 0), it is also an
issue with array element upgrading.

http://www.lispworks.com/documentation/HyperSpec/Body/15_aba.htmu

SBCL does not have a special array type for (complex rational), it is
upgraded to type T. So, if you are expecting that SBCL is creating a
(complex rational) array, it isn't.

CL-USER> (defparameter *complex-array*
	   (make-array 4 :element-type '(complex rational) :initial-element
0))
*COMPLEX-ARRAY*
CL-USER> (type-of *complex-array*)
(SIMPLE-VECTOR 4)
CL-USER> *complex-array*
#(0 0 0 0)
CL-USER> (array-element-type *complex-array*)
T

I don't have Allegro CL, but apparently it does have a specialized
array for (complex rational). This appears to create an
incompatibility in the system because of section 12.1.5.3 Rule of
Canonical Representation for Complex Rationals.

http://www.lispworks.com/documentation/HyperSpec/Body/12_aec.htm

Since a (complex rational) with a zero imaginary component will always
be converted to a rational, an array specialized to (complex rational)
becomes difficult to use for the very reason that you have
encountered.

My recommendation is that you should review why you need an array
specialized to (complex rational). Maybe that isn't necessary and a
simple-vector will work fine.

Good luck,

Tom
From: Nicolas Neuss
Subject: Re: Initializing a (complex rational) array
Date: 
Message-ID: <87ej0h85ze.fsf@ma-patru.mathematik.uni-karlsruhe.de>
"Thomas M. Hermann" <··········@gmail.com> writes:

> I don't have Allegro CL, but apparently it does have a specialized
> array for (complex rational). This appears to create an
> incompatibility in the system because of section 12.1.5.3 Rule of
> Canonical Representation for Complex Rationals.
>
> http://www.lispworks.com/documentation/HyperSpec/Body/12_aec.htm
>
> Since a (complex rational) with a zero imaginary component will always
> be converted to a rational, an array specialized to (complex rational)
> becomes difficult to use for the very reason that you have
> encountered.
>
> My recommendation is that you should review why you need an array
> specialized to (complex rational). Maybe that isn't necessary and a
> simple-vector will work fine.

OK, thanks for the explanation.  No, I do not really need this feature.  I
came across the problem when generating custom-generated classes for
certain numbers: (standard-matrix '(complex double-float)) worked, so I
tried also (standard-matrix '(complex rational)).

Nicolas
From: Nicolas Neuss
Subject: Re: Initializing a (complex rational) array
Date: 
Message-ID: <87tz9c2t47.fsf@ma-patru.mathematik.uni-karlsruhe.de>
Nicolas Neuss <········@math.uni-karlsruhe.de> writes:

>  (make-array 4 :element-type '(complex rational) :initial-element 0)
>
> SBCL initializes the array as I expected, however Allegro 8.1 (trial
> edition) gives the error
>
> Error: Initial element 0 is not of type (COMPLEX RATIONAL)

Interestingly, as I learned from Franz support, the spec states

"If initial-element is supplied, it is used to initialize each element of
new-array. If initial-element is supplied, it must be of the type given by
element-type."

So the above code is wrong, and an implementation should not accept it.
Could this be something for the ANSI CL testsuite?

Nicolas
From: Juho Snellman
Subject: Re: Initializing a (complex rational) array
Date: 
Message-ID: <87y6yoteng.fsf@vasara.proghammer.com>
Nicolas Neuss <········@math.uni-karlsruhe.de> writes:

> Nicolas Neuss <········@math.uni-karlsruhe.de> writes:
> 
> >  (make-array 4 :element-type '(complex rational) :initial-element 0)
> >
> > SBCL initializes the array as I expected, however Allegro 8.1 (trial
> > edition) gives the error
> >
> > Error: Initial element 0 is not of type (COMPLEX RATIONAL)
> 
> Interestingly, as I learned from Franz support, the spec states
> 
> "If initial-element is supplied, it is used to initialize each element of
> new-array. If initial-element is supplied, it must be of the type given by
> element-type."
> 
> So the above code is wrong, and an implementation should not accept it.
> Could this be something for the ANSI CL testsuite?

No, that's a constraint on conforming programs, not
implementations. Basically it just means that the programmer must
ensure that the initial-element and type match. If they don't, an
implementation is free to do anything (eg. signal an error, accept the
initial element anyway, refuse to compile the code, etc).

-- 
Juho Snellman