From: Barry Margolin
Subject: Re: want to ref a whole row in a 2d array
Date: 
Message-ID: <28te2uINNhm1@early-bird.think.com>
In article <··········@sparc0a.cs.uiuc.edu> ·····@ta10.cs.uiuc.edu (Hovig Heghinian) writes:
>(make-array '(x y)) returns a 2-D array.
>
>(aref my-array x y) returns a single element from a 2-D array
>
>Given a 2-D array, I want to return a 1-D array, like the i^th row (or
>even the j^th column if possible)

(defun ith-row (array i)
  (make-array (array-dimension array 1) :type (array-element-type array)
    :displaced-to array
    :displaced-index-offset (* i (array-dimension array 1))))

You can't do this for a column.
-- 
Barry Margolin
System Manager, Thinking Machines Corp.

······@think.com          {uunet,harvard}!think!barmar

From: J W Dalton
Subject: Re: want to ref a whole row in a 2d array
Date: 
Message-ID: <CEHD4o.GL6@festival.ed.ac.uk>
······@think.com (Barry Margolin) writes:

>In article <··········@sparc0a.cs.uiuc.edu> ·····@ta10.cs.uiuc.edu (Hovig Heghinian) writes:
>>(make-array '(x y)) returns a 2-D array.
>>
>>(aref my-array x y) returns a single element from a 2-D array
>>
>>Given a 2-D array, I want to return a 1-D array, like the i^th row (or
>>even the j^th column if possible)

>(defun ith-row (array i)
>  (make-array (array-dimension array 1) :type (array-element-type array)
>    :displaced-to array
>    :displaced-index-offset (* i (array-dimension array 1))))

>You can't do this for a column.

So the only way to get col-extract would be to implement your
own arrays or your own "columns".  This is not necessarily as
bad as it seems.  For instance, you might have something like

  (defstrict col   ; a column
    base-array
    col-index)

  (defun col-ref (col row-index)
    (aref (base-array col) row-index col-index))

Of course, this is specific to columns.  You'd have to write
something more complex for the more general case.  Just how
would depend on how efficient it needed to be.  CLOS would
let you be flexible easily, eg

  (defmethod ref ((c col) i)
    ...)

  (defmethod ref ((r row) i)
    ...)

  (defmethod ref ((v vector) i)
    ...)

-- jeff
From: Lee Schumacher
Subject: Re: want to ref a whole row in a 2d array
Date: 
Message-ID: <1993Oct6.173322.14443@cs.wisc.edu>
In article <··········@festival.ed.ac.uk>, ····@festival.ed.ac.uk (J W Dalton) writes:
|> ······@think.com (Barry Margolin) writes:
|> 
|> >In article <··········@sparc0a.cs.uiuc.edu> ·····@ta10.cs.uiuc.edu (Hovig Heghinian) writes:
|> >>(make-array '(x y)) returns a 2-D array.
|> >>
|> >>(aref my-array x y) returns a single element from a 2-D array
|> >>
|> >>Given a 2-D array, I want to return a 1-D array, like the i^th row (or
|> >>even the j^th column if possible)
|> 
|> >(defun ith-row (array i)
|> >  (make-array (array-dimension array 1) :type (array-element-type array)
|> >    :displaced-to array
|> >    :displaced-index-offset (* i (array-dimension array 1))))
|> 
|> >You can't do this for a column.
|> 
|> So the only way to get col-extract would be to implement your
|> own arrays or your own "columns".  

Certainly not!  You could just transpose the matrix, and then
your columns become rows, which you already know how to extract.
There are efficiency considerations here, but its sure to be better
than trying to re-implement arrays by hand.  If the array isn't changing
too much between column accesses you might even maintain the transposed
matrix in parallel with the original.

 Lee.
From: J W Dalton
Subject: Re: want to ref a whole row in a 2d array
Date: 
Message-ID: <CEJ79o.Cvs@festival.ed.ac.uk>
········@cs.wisc.edu (Lee Schumacher) writes:

>In article <··········@festival.ed.ac.uk>, ····@festival.ed.ac.uk (J W Dalton) writes:
>|> So the only way to get col-extract would be to implement your
>|> own arrays or your own "columns".  

>Certainly not!  You could just transpose the matrix, and then
>your columns become rows, which you already know how to extract.

True, but would you want to?

>There are efficiency considerations here, but its sure to be better
>than trying to re-implement arrays by hand.

But implementing columns is not reimplementing arrays.  It's
implementing a kind of displaced array, sure, but I'm not convinced
it will be significantly worse than the built-in kind.

>  If the array isn't changing
>too much between column accesses you might even maintain the transposed
>matrix in parallel with the original.

Why is that better than implementing "columns"?

-- jeff