Matrix VS Arrays



Thanks, Stavros!

My great mistake was that I didn't generate object Matrix by genmatrix:

I did:
for i thru 4 do for j thru 4 do A[i,j]: i+j$

But without genmatrix(A,4,4) further...

Thanks a lot!

On Sun, 2007-12-23 at 13:19 -0500, Stavros Macrakis wrote:
> You have found one of the more confusing areas of Maxima, I'm afraid.
> 
> The three different tests you did in fact operate on *three different*
> types of object in Maxima: sparse arrays (assigning a[..] with no
> declaration), matrices (matrix/genmatrix/etc.), and dense arrays
> (explicit array declaration). 
> 
> First of all, I would recommend that you not use dense arrays at all.
> Those are best reserved for optimization of performance by advanced
> users.
> 
> Sparse arrays allow you to store values very flexibly, but do not work
> as single assignable objects.  For example: 
> 
>     qq[3]: 5$
>     qq["my dog"]: 'fido $
>     qq[-2/3] : "minus two thirds"
> 
>     qq[3]  => 5
> 
> But you can't do much with "qq" by itself -- you can't print it, you
> can't perform arithmetic on it ( e.g. add elements index-by-index with
> another sparse array), etc. (but see arrayinfo and listarray for more
> advanced operations).
> 
> You can write matrices explicitly, e.g. matrix([1,0],[0,1]).
> 
> You can create matrices using genmatrix: 
> 
>     genmatrix( lambda([i,j], i+j), 4,4) => matrix([2,3,4,5],[3,4 ...)
> 
> To make a matrix from a sparse array, use genmatrix:
> 
>    for i thru 4 do for j thru 4 do A[i,j]: i+j$
>   genmatrix(A,4,4) => 
>      matrix([2,3,4,5],[3,4,5,6] ...)
> 
> You can also modify the elements of a matrix:
> 
>    MA: genmatrix(A,3,3)$
>    for i thru 4 do MA[i,i]: 0$
>    MA => matrix([0,3,4,5],[3,0,5,6]...)
> 
> There are various other constructors for matrices, e.g. ident(2) =>
> matrix([1,0],[0,1])
> 
> Try working with these constructs and let us know if you have further
> questions.
> 
>           -s
>