From: David Bakhash
Subject: :displaced-to for what? (in `make-array')
Date: 
Message-ID: <cxjaf1se51w.fsf@engc.bu.edu>
hey,

I am creating a matrix package for CL, that I hope will one day be
useful to all and fast.  But in the process I want to make sure that
I'm taking advantage of all CL has.

I was hoping to make use of displacement in arrays, but I can't see
how they can be useful.  Not only that, but I can't really see how
they can be made useful for multi-dimensional arrays, where two arrays 
might have different dimensions.

Can someone please tell me why the :displaced-to even exists?

what were they trying to accomplish?  in what contexts would this be
useful in multi-dimensional arrays?

dave

From: Lyman S. Taylor
Subject: Re: :displaced-to for what? (in `make-array')
Date: 
Message-ID: <72oiqo$itl@pravda.cc.gatech.edu>
In article <···············@engc.bu.edu>, David Bakhash  <·····@bu.edu> wrote:

>how they can be useful.  Not only that, but I can't really see how
>they can be made useful for multi-dimensional arrays, where two arrays 
>might have different dimensions.
>
>Can someone please tell me why the :displaced-to even exists?

  Seems like a classic tradeoff of speed for space and convenience. 

http://www.harlequin.com/education/books/HyperSpec/Body/glo_d.html#displaced_array
    
  creating a displaced array doesn't do much of a allocation from memory
  (only enough to contain the info about the alias).  The result is an
  alias to some portion of the target array.   Some computations may only 
  need to access a portion of the original array. 

  So for example: 


USER(1): (setq my-arr (make-array '(5 5 ) :initial-contents 
                  '( (11 12 13 14 15 )
                     (21 22 23 24 25 )
                     (31 32 33 34 35 )
                     (41 42 43 44 45 )
                     (51 52 53 54 55 ) ) ) )
#2A((11 12 13 14 15)
    (21 22 23 24 25)
    (31 32 33 34 35)
    (41 42 43 44 45)
    (51 52 53 54 55))



  Like a row vector 

USER(6): (make-array 5 :displaced-to my-arr  :displaced-index-offset 10 )
#(31 32 33 34 35)

>
>what were they trying to accomplish?  in what contexts would this be
>useful in multi-dimensional arrays?

   If it is a very large multi-dimension array you may not want to make 
   "n" duplicates of subcomponents and/or merge the results back into
   the original matrices. 

   It is a speed tradeoff because you're adding one level of indirection. 
   This doesn't have to be that bad of a penatly, but if strength reduction
   (other basic loop optimzations) is a "forgein concept", then I don't  think 
   "displaced" and "high performance" will often occur in the same sentence. 


-- 
					
Lyman S. Taylor                "Twinkie Cream; food of the Gods" 
(·····@cc.gatech.edu)                     Jarod, "The Pretender" 
From: Kent M Pitman
Subject: Re: :displaced-to for what? (in `make-array')
Date: 
Message-ID: <sfwzp9sccpb.fsf@world.std.com>
David Bakhash <·····@bu.edu> writes:

> I am creating a matrix package for CL, that I hope will one day be
> useful to all and fast.  But in the process I want to make sure that
> I'm taking advantage of all CL has.
 
uh, as with most language features, i'd recommend being aware of the
feature and waiting for a use to present itself.  just because you
don't use a feature doesn't mean it has no use.  if you accomplish your
goal without using these features, it's not clear that you are not taking
advantage of all CL has.

> I was hoping to make use of displacement in arrays, but I can't see
> how they can be useful.  Not only that, but I can't really see how
> they can be made useful for multi-dimensional arrays, where two arrays 
> might have different dimensions.

depends on the application.  in early CL, prior to ROW-MAJOR-AREF, it was
the only way to way to map across a multi-d array. You would displace a 
single-d array (size = product of dimensions) to the multi-d array, then
map the function over the elements using MAP, etc.  Nowadays that isn't
done so much.  if what you mean is that a displaced array of one shape
can't easily be displaced to another, that's probably right.  but single
to multi makes sense.  and pretty much anything following row-major layout
probably makes sense.
 
> Can someone please tell me why the :displaced-to even exists?
 
Sure.  What if you want to grab a row of an array and, without copying it,
pass it to someone else?  You may or may not need this, but someone might.

> what were they trying to accomplish?  in what contexts would this be
> useful in multi-dimensional arrays?

why do you assume it's there for multi-d arrays?  it's there for arrays,
some of which happen to be multi-d.  it has most of its application when
either the source or the target or both is 1d.
From: Marco Antoniotti
Subject: Re: :displaced-to for what? (in `make-array')
Date: 
Message-ID: <lwogq8xaku.fsf@copernico.parades.rm.cnr.it>
Apart from what has been said by Kent Pitman and Lyman Taylor, I would
also suggest that you familiarize yourself with the PACKAGE system and
the CONDITION system of CL before wrting up the final piece of code.

As per the usage of :displaced-to, here is a piece of code that irked
me, but that is nevertheless common in "some" :) programming circles.

typedef struct __packet1 {
	unsigned short header[2];     /* names and lengths arbitrary */
	unsigned short content[14];
} packet_view_1;

typedef struct __packet2 {
	unsigned short packet[16];
} packet_view_2;

typedef union {
	packet_view_1 raw;
	packet_view_2 cooked;
} packet;

The only way I see to achieve this result in CL is through displaced
arrays.


If you have better solutions please let me know.


-- 
Marco Antoniotti ===========================================
PARADES, Via San Pantaleo 66, I-00186 Rome, ITALY
tel. +39 - (0)6 - 68 10 03 16, fax. +39 - (0)6 - 68 80 79 26
http://www.parades.rm.cnr.it
From: Rainer Joswig
Subject: Re: :displaced-to for what? (in `make-array')
Date: 
Message-ID: <joswig-1611981024170001@pbg3.lavielle.com>
In article <··············@copernico.parades.rm.cnr.it>, Marco Antoniotti
<·······@copernico.parades.rm.cnr.it> wrote:

> Apart from what has been said by Kent Pitman and Lyman Taylor, I would
> also suggest that you familiarize yourself with the PACKAGE system and
> the CONDITION system of CL before wrting up the final piece of code.
> 
> As per the usage of :displaced-to, here is a piece of code that irked
> me, but that is nevertheless common in "some" :) programming circles.
> 
> typedef struct __packet1 {
>         unsigned short header[2];     /* names and lengths arbitrary */
>         unsigned short content[14];
> } packet_view_1;
> 
> typedef struct __packet2 {
>         unsigned short packet[16];
> } packet_view_2;
> 
> typedef union {
>         packet_view_1 raw;
>         packet_view_2 cooked;
> } packet;
> 
> The only way I see to achieve this result in CL is through displaced
> arrays.

I have seen this in CL, too. Just lately. The problem is:

(qoute-from (:hyperspec make-array)
 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.)

-- 
http://www.lavielle.com/~joswig