Dear Lisp hackers,
I'd like to define a vector that is displaced to a column of a 2D
array. I know there must be a way to do this, but I can't for the life
of me figure it out.
The best thing I could come up with was to make a bunch of vectors of
length one that are displaced to each element of the column, but I
couldn't figure out how to assemble them into a single vector. If you
use make-array with :inital-contents set to a list of those vectors,
you get close, but it's a vector of vectors, not a vector of the
column elements. Using concatenate on those vectors copies the
contents, so they are no longer displaced to the actual matrix
elements, although it is a copy of the column.
Marco Antoniotti's array-slice package can be forced into service, but
it returns a special kind of object (an array-slice), not a vector.
There are lots of similar approaches if you don't need the returned
object to be a vector itself.
It would be so elegant for the matrix work that I am doing to be able
to do this. I know, I know, I can define my own set of objects to
provide this kind of functionality, but I can't believe there really
no way to make an actual vector that is displaced to a column of a
matrix. Can anyone else think of something?
Larry
--
Lawrence Hunter, Ph.D.
Director, Center for Computational Pharmacology
Associate Professor of Pharmacology, PMB & Computer Science
phone +1 303 315 1094 UCHSC, Campus Box C236
fax +1 303 315 1098 School of Medicine rm 2817b
cell +1 303 324 0355 4200 E. 9th Ave.
email: ············@uchsc.edu Denver, CO 80262
PGP key on public keyservers http://compbio.uchsc.edu/hunter
From: Larry Clapp
Subject: Re: vector displaced to the column of a matrix
Date:
Message-ID: <2ch91b.kok.ln@127.0.0.1>
In article <··············@huge.uchsc.edu>, Larry Hunter wrote:
> I'd like to define a vector that is displaced to a column of a
> 2D array. I know there must be a way to do this, but I can't
> for the life of me figure it out.
Would it work for your purposes to shadow aref in your package
and define your own to do what you want it to do?
> It would be so elegant for the matrix work that I am doing to
> be able to do this. I know, I know, I can define my own set of
> objects to provide this kind of functionality, but I can't
> believe there really no way to make an actual vector that is
> displaced to a column of a matrix. Can anyone else think of
> something?
Why do you need "an actual vector"? You'd access the vector via
aref, yes? Or something similar? So define your own function to
do what you need it to do (e.g. via row-major-aref), and use
that. You'll still get constant-time accesses, yes?
How 'bout:
* (defvar *array* (make-array '(10 10)))
*ARRAY*
* (loop for i below 100 do
(setf (row-major-aref *array* i) i))
NIL
* *array*
#2A((0 1 2 3 4 5 6 7 8 9)
(10 11 12 13 14 15 16 17 18 19)
(20 21 22 23 24 25 26 27 28 29)
(30 31 32 33 34 35 36 37 38 39)
(40 41 42 43 44 45 46 47 48 49)
(50 51 52 53 54 55 56 57 58 59)
(60 61 62 63 64 65 66 67 68 69)
(70 71 72 73 74 75 76 77 78 79)
(80 81 82 83 84 85 86 87 88 89)
(90 91 92 93 94 95 96 97 98 99))
* (aref *array* 3 5)
35
* (row-major-aref *array* (array-row-major-index *array* 3 5))
35
* (defun make-aref (array column)
#'(lambda (row)
(row-major-aref array (array-row-major-index array row column))))
MAKE-AREF
* (let ((column5 (make-aref *array* 5)))
(funcall column5 3))
35
* (defun make-aref2 (column)
#'(lambda (array row)
(row-major-aref array (array-row-major-index array row column))))
MAKE-AREF2
* (let ((column5 (make-aref2 5)))
(funcall column5 *array* 3))
35
How would displacement the way you want it differ from make-aref
or make-aref2?
If the language provided it, wouldn't it end up doing something
substantially similar to this approach?
-- Larry Clapp
In article <·············@127.0.0.1>, Larry Clapp <·····@theclapp.org> wrote:
>Why do you need "an actual vector"?
My guess is that he wants to be able to pass the object to any function
that accepts vectors, whether they're standard CL functions or from
3rd-party libraries.
--
Barry Margolin, ······@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: Larry Clapp
Subject: Re: vector displaced to the column of a matrix
Date:
Message-ID: <nan91b.kgn.ln@127.0.0.1>
In article <················@paloalto-snr1.gtei.net>, Barry Margolin wrote:
> In article <·············@127.0.0.1>, Larry Clapp <·····@theclapp.org> wrote:
>>Why do you need "an actual vector"?
>
> My guess is that he wants to be able to pass the object to any
> function that accepts vectors, whether they're standard CL
> functions or from 3rd-party libraries.
Ah, of course. Silly me. Thanks.
In article <··············@huge.uchsc.edu>,
Larry Hunter <············@uchsc.edu> wrote:
>I'd like to define a vector that is displaced to a column of a 2D
>array. I know there must be a way to do this, but I can't for the life
>of me figure it out.
Symbolics had something like this, they called it "conformally displaced
arrays". It allowed you to create a 2-d array that mapped to a rectangle
within another 2-d array (it was mostly used by the window system, to map a
window into the corresponding rectangle of the frame buffer).
Common Lisp doesn't have anything built-in that does this.
--
Barry Margolin, ······@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.