From: ·············@gmail.com
Subject: repackage complex array into double-length vector
Date: 
Message-ID: <00307b10-f17b-4c90-bd79-1b1642539d54@m45g2000hsb.googlegroups.com>
I have to re-package a complex array into a real vector of form #(re
im re im re im ...)

Is there a lisp built-in or do I have to do it by hand, (snippet from
other code follows)


  (let* ((dim (length complex-vector))
	(double (make-array (* 2 dim)
			     :element-type 'double-float
			     :initial-element 0d0)))
    ;; repackaging complex as double -- is there a built-in?
    (loop
       for re-im across complex-vector
       for i from 0 to (* 2 (1- dim)) by 2
       do (progn
	    (setf (aref double i) (realpart re-im))
	    (setf (aref double (1+ i)) (imagpart re-im))))

Thanks,

Mirko

From: Daniel Janus
Subject: Re: repackage complex array into double-length vector
Date: 
Message-ID: <slrng38sdf.c05.przesunmalpe@students.mimuw.edu.pl>
Dnia 21.05.2008 ·············@gmail.com <·············@gmail.com> napisa�/a:

> I have to re-package a complex array into a real vector of form #(re
> im re im re im ...)
>
> Is there a lisp built-in or do I have to do it by hand, (snippet from
> other code follows)
>
>   (let* ((dim (length complex-vector))
> 	(double (make-array (* 2 dim)
> 			     :element-type 'double-float
> 			     :initial-element 0d0)))
>     ;; repackaging complex as double -- is there a built-in?
>     (loop
>        for re-im across complex-vector
>        for i from 0 to (* 2 (1- dim)) by 2
>        do (progn
> 	    (setf (aref double i) (realpart re-im))
> 	    (setf (aref double (1+ i)) (imagpart re-im))))

This one is much more concise and arguably clearer to the reader; 
on the other hand, it conses more:

(coerce (loop for c across complex-vector 
              collect (realpart c) 
              collect (imagpart c))
        'vector)

Best regards,
-- 
Daniel 'Nathell' Janus, ······@nathell.korpus.pl, http://korpus.pl/~nathell
Any sufficiently complicated Java program requires a programmable IDE to
make up for the half of Common Lisp not implemented in the program itself.
   -- Peter Seibel's Corollary to Greenspun's Tenth Rule
From: Pascal J. Bourguignon
Subject: Re: repackage complex array into double-length vector
Date: 
Message-ID: <7cy75xh47m.fsf@pbourguignon.anevia.com>
Daniel Janus <············@nathell.korpus.pl> writes:

> Dnia 21.05.2008 ·············@gmail.com <·············@gmail.com> napisał/a:
>
>> I have to re-package a complex array into a real vector of form #(re
>> im re im re im ...)
>>
>> Is there a lisp built-in or do I have to do it by hand, (snippet from
>> other code follows)
>>
>>   (let* ((dim (length complex-vector))
>> 	(double (make-array (* 2 dim)
>> 			     :element-type 'double-float
>> 			     :initial-element 0d0)))
>>     ;; repackaging complex as double -- is there a built-in?
>>     (loop
>>        for re-im across complex-vector
>>        for i from 0 to (* 2 (1- dim)) by 2
>>        do (progn
>> 	    (setf (aref double i) (realpart re-im))
>> 	    (setf (aref double (1+ i)) (imagpart re-im))))
>
> This one is much more concise and arguably clearer to the reader; 
> on the other hand, it conses more:
>
> (coerce (loop for c across complex-vector 
>               collect (realpart c) 
>               collect (imagpart c))
>         'vector)

No, it is not more concise and  neither clearer.  You're forgetting
that in both case, you shall use it as:

(let (([re\,im]* (real-coordinates-from-complexes [ai+b]*)))
   ...)



I would write it as:

(defun real-coordinates-from-complexes (complexes)
  (loop
     :with complex-vector = (etypecase complexes ; let's accept
                              (vector complexes) ; sequences
                              (list   (coerce complexes 'vector)))
     :with result = (make-array (* 2 (length complex-vector))
                                :element-type 'double-float
                                :initial-element 0d0)
     :for complex :across complex-vector
     :for i :from 0 :by 2 
     :do (setf (aref result     i)  (realpart complex)
               (aref result (1+ i)) (imagpart complex))
     :finally (return result)))  

-- 
__Pascal Bourguignon__
From: Madhu
Subject: Re: repackage complex array into double-length vector
Date: 
Message-ID: <m3lk1x6o45.fsf@meer.net>
  [Minor ponit]
* (Pascal J. Bourguignon) <··············@pbourguignon.anevia.com> :
Wrote on Mon, 26 May 2008 10:14:37 +0200:
[...]

|                        (etypecase complexes ; let's accept
|                          (vector complexes) ; sequences
|                          (list   (coerce complexes 'vector)))

This is identical to (coerce complexes 'vector)
--
Madhu
From: Pascal J. Bourguignon
Subject: Re: repackage complex array into double-length vector
Date: 
Message-ID: <7cy75xf0x9.fsf@pbourguignon.anevia.com>
Madhu <·······@meer.net> writes:

>   [Minor ponit]
> * (Pascal J. Bourguignon) <··············@pbourguignon.anevia.com> :
> Wrote on Mon, 26 May 2008 10:14:37 +0200:
> [...]
>
> |                        (etypecase complexes ; let's accept
> |                          (vector complexes) ; sequences
> |                          (list   (coerce complexes 'vector)))
>
> This is identical to (coerce complexes 'vector)

Oops!  Indeed.

-- 
__Pascal Bourguignon__
From: ·············@gmail.com
Subject: Re: repackage complex array into double-length vector
Date: 
Message-ID: <fbaa9844-a62e-4ee5-91c1-583c52f7933c@d45g2000hsc.googlegroups.com>
On May 26, 1:08 pm, ····@informatimago.com (Pascal J. Bourguignon)
wrote:
> Madhu <·······@meer.net> writes:
> >   [Minor ponit]
> > * (Pascal J. Bourguignon) <··············@pbourguignon.anevia.com> :
> > Wrote on Mon, 26 May 2008 10:14:37 +0200:
> > [...]
>
> > |                        (etypecase complexes ; let's accept
> > |                          (vector complexes) ; sequences
> > |                          (list   (coerce complexes 'vector)))
>
> > This is identical to (coerce complexes 'vector)
>
> Oops!  Indeed.
>
> --
> __Pascal Bourguignon__

Right, but at least it made me look up etypecase in hyperspec :-)

otoh, a wild question:

Suppose I point a double sized vector to the same memory location as
the complex vector.  What would I get?

Even more importantly, how would I do that? (I mean what functions).

Thanks,
Mirko
From: Pascal J. Bourguignon
Subject: Re: repackage complex array into double-length vector
Date: 
Message-ID: <7cfxs4f9aa.fsf@pbourguignon.anevia.com>
·············@gmail.com writes:
> Suppose I point a double sized vector to the same memory location as
> the complex vector.  

You mean, if you had pointers?  But this is not C ;-)


> What would I get?

Implementation dependant.


> Even more importantly, how would I do that? (I mean what functions).

You wouldn't.


(If you really meant to, you could, but only with implementation
dependent things).

-- 
__Pascal Bourguignon__
From: Thomas A. Russ
Subject: Re: repackage complex array into double-length vector
Date: 
Message-ID: <ymiskw3mn85.fsf@blackcat.isi.edu>
·············@gmail.com writes:

> Suppose I point a double sized vector to the same memory location as
> the complex vector.  What would I get?

Undefined behavior.  See below.

> Even more importantly, how would I do that? (I mean what functions).

The closest you could come would be to define a second array and have it
displaced to the same array as the earlier one that you created.  But
the hyperspec says:

"If displaced-to is non-nil, make-array will create a displaced array
 and displaced-to is the target of that displaced array. In that case,
 the consequences are undefined if the actual array element type of
 displaced-to is not type equivalent to the actual array element type of
 the array being created. If displaced-to is nil, the array is not a
 displaced array."

-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: ·············@gmail.com
Subject: Re: repackage complex array into double-length vector
Date: 
Message-ID: <ac56fad3-d2e2-4931-9d88-4dff4cb0072f@i76g2000hsf.googlegroups.com>
On May 27, 5:49 pm, ····@sevak.isi.edu (Thomas A. Russ) wrote:
> ·············@gmail.com writes:
> > Suppose I point a double sized vector to the same memory location as
> > the complex vector.  What would I get?
>
> Undefined behavior.  See below.
>
> > Even more importantly, how would I do that? (I mean what functions).
>
> The closest you could come would be to define a second array and have it
> displaced to the same array as the earlier one that you created.  But
> the hyperspec says:
>
> "If displaced-to is non-nil, make-array will create a displaced array
>  and displaced-to is the target of that displaced array. In that case,
>  the consequences are undefined if the actual array element type of
>  displaced-to is not type equivalent to the actual array element type of
>  the array being created. If displaced-to is nil, the array is not a
>  displaced array."
>
> --
> Thomas A. Russ,  USC/Information Sciences Institute

Not much happens in sbcl.  The displaced array acquires the same type
as the original array:

CL-USER> (let* ((n 5)
                (x (make-array n :initial-element (complex 1 0))))
           (dotimes (i n)
             (setf (aref x i) (complex i (1+ i))))
           (let ((2r (make-array n :displaced-to x
                                   :displaced-index-offset 0)))
             2r))

#(#C(0 1) #C(1 2) #C(2 3) #C(3 4) #C(4 5))

If I specify the type of 2r as 'float, or 'double-float, I get an
error.