From: Michiel Borkent
Subject: matrix implementation considerations
Date: 
Message-ID: <1121687691.154942.57990@g14g2000cwa.googlegroups.com>
I have began writing my own collection of matrix functions, like:

(matrix &rest columns) -> constructs a matrix of the columns
(implemented as simple arrays)

(tranpose matrix) -> returns (or modifies, optionally) the tranposed
matrix

(scale-matrix const matrix) -> returns the matrix multiplied with a
scalar value

etc.

For some reason I have implemented the matrix as a list of arrays.

Transpose now would look something like this:
(defun transpose-matrix (matrix &optional (destructive nil))
  (let ((transposed (loop for row-index below (length (first matrix))
                          collect (make-array (length matrix)
:initial-contents (loop for col-index below (length matrix)

collect (aref (nth col-index matrix) row-index))))))
    (when destructive (setf matrix transposed))
    transposed))


I didn't know much about all the functionality of arrays in CL when I
started this collection on matrices.

My question: would it be favorable to implement matrices as
2-dimensional arrays, in terms of possibility to write cleaner code for
functions like 'transpose-matrix'.
I don't care much about efficiency right now, since working with large
matrix works fine on my system, but it would be nice if working with
2-dimensional arrays is more efficient as well, of course. Right now I
don't really see how it would be more efficient, since I lack the
experience and theoretical foundations on list and array structure.

Kind Regards,
Michiel Borkent

From: ···············@yahoo.com
Subject: Re: matrix implementation considerations
Date: 
Message-ID: <1122333991.413716.279000@g47g2000cwa.googlegroups.com>
Say you implement an m-by-n matrix as a list of arrays, and then you
want to look up an element in the last row.  It will take you m steps,
because you have to iterate through the first m-1 items in the list
before you can get to the last item.  This is a general fact about
Lisp's lists: you can't look at the i-th item without running past the
first i-1 items.

By constrast, looking up an element in any array only takes one step.
From: Karol Skocik
Subject: Re: matrix implementation considerations
Date: 
Message-ID: <1122373372.937456.55200@g49g2000cwa.googlegroups.com>
Hi,
  you can traspose matrix easily with : (apply #'mapcar #'list
*matrix*)

Bye,
  Karol Skocik
From: Michiel Borkent
Subject: Re: matrix implementation considerations
Date: 
Message-ID: <a58e8f47.0507260917.26114b47@posting.google.com>
"Karol Skocik" <············@gmail.com> wrote in message news:<·······················@g49g2000cwa.googlegroups.com>...
> Hi,
>   you can traspose matrix easily with : (apply #'mapcar #'list
> *matrix*)
> 
> Bye,
>   Karol Skocik

Hi Karol,

This works only if you implement a matrix as a list of lists I guess.
From: Karol Skocik
Subject: Re: matrix implementation considerations
Date: 
Message-ID: <1122496246.813899.47010@f14g2000cwb.googlegroups.com>
yes, it works on list only, and yes, it's limited. but it's nice
example :)
but i was playing for a while and it can be rewritten like this to work
on arrays :

CL-USER> (apply #'map 'vector #'(lambda (&rest args)
                                  (make-array (length args)
                                              :initial-contents
`(,@args)))
                (coerce #(#(1 2 4) #(3 4 5)) 'list))
#(#(1 3) #(2 4) #(4 5))

i will be happy if somebody posts nicer solution.

Have a nice day,
  Karol Skocik
From: Ivan Boldyrev
Subject: Re: matrix implementation considerations
Date: 
Message-ID: <ojshr2-s9s.ln1@ibhome.cgitftp.uiggm.nsc.ru>
On 9182 day of my life Karol Skocik wrote:
> Hi,
>   you can traspose matrix easily with : (apply #'mapcar #'list
> *matrix*)

But it limits matrix size to CALL-ARGUMENTS-LIMIT.  It is 4096 at
CLisp 2.33.2 and 64 at GCL 2.6.6.

-- 
Ivan Boldyrev

                                      Life!  Don't talk to me about life.
From: Kent M Pitman
Subject: Re: matrix implementation considerations
Date: 
Message-ID: <uy87tttr0.fsf@nhplace.com>
Ivan Boldyrev <···············@cgitftp.uiggm.nsc.ru> writes:

> On 9182 day of my life Karol Skocik wrote:

I hope Google is keeping track of people's birthdays (and the timezone
in which the birthday occurred) or this might not be 100% useful for
reference...

> > Hi,
> >   you can traspose matrix easily with : (apply #'mapcar #'list
> > *matrix*)
> 
> But it limits matrix size to CALL-ARGUMENTS-LIMIT.
> It is 4096 at CLisp 2.33.2 and 64 at GCL 2.6.6.

And 255 in LispWorks 4.4.5.

And 50 in Symbolics Genera 8.3
 [since I happen to have mine up and running today]
From: Ivan Boldyrev
Subject: Re: matrix implementation considerations
Date: 
Message-ID: <rgbmr2-fn3.ln1@ibhome.cgitftp.uiggm.nsc.ru>
On 9182 day of my life Kent M. Pitman wrote:
> Ivan Boldyrev <···············@cgitftp.uiggm.nsc.ru> writes:
>
>> On 9182 day of my life Karol Skocik wrote:
>
> I hope Google is keeping track of people's birthdays (and the timezone
> in which the birthday occurred) or this might not be 100% useful for
> reference...

Google?  What is Google? :)

I get date from "Date" header, convert to "absolute" time and
substract my birtday's absolute time.  So, birthdays of Google users
are irrelevant here.

It is 9182nd day of my life, not Karol's :)

>> >   you can traspose matrix easily with : (apply #'mapcar #'list
>> > *matrix*)
>> 
>> But it limits matrix size to CALL-ARGUMENTS-LIMIT.
>> It is 4096 at CLisp 2.33.2 and 64 at GCL 2.6.6.
>
> And 255 in LispWorks 4.4.5.
>
> And 50 in Symbolics Genera 8.3

Not too much, anyway...

-- 
Ivan Boldyrev

       Assembly of a Japanese bicycle requires greatest peace of spirit.
From: Rob Warnock
Subject: Re: matrix implementation considerations
Date: 
Message-ID: <Eu6dnZRJ_qkz1nHfRVn-pQ@speakeasy.net>
Michiel Borkent <··············@gmail.com> wrote:
+---------------
| For some reason I have implemented the matrix as a list of arrays.
+---------------

Is there some particular reason you didn't use Common Lisp's native
multi-dimensional arrays?  E.g.:

    > (let ((demo-data (loop for i below 3 collect
			 (loop for j below 4 collect (+ (* i 10) j)))))
        (make-array '(3 4) :initial-contents demo-data))

    #2A((0 1 2 3) (10 11 12 13) (20 21 22 23))
    > (describe *)

    #2A((0 1 2 3) (10 11 12 13) (20 21 22 23)) is an array of rank 2.
    Its dimensions are (3 4).
    > 


-Rob

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607