Discussion:
Size of a matrix
Dimitri Pissarenko
2004-01-29 11:02:36 UTC
Permalink
Hello!

Is there a way to determine the size (number of rows, number of
columns) of a matrix in Maxima?

TIA

dap
Dimitri Pissarenko
2004-01-29 11:37:13 UTC
Permalink
Hello!

Concerning the determining of the size of matrix: Is it possible that
the description of this feature will be documented in Maxima's
documentation?

How could I help to do this?

TIA

dap
Dan Stanger
2004-01-29 13:08:05 UTC
Permalink
It seems to me when I was writing some state variable code years ago,
I needed to keep track of the size of matrixes, and I did so by keeping
a list of that information. A cursory look at the commercial Macsyma
documentation lists functions that were a add on for matlab users.
That said, the following works.
m:matrix([1,2,3],[4,5,6]);
length(m) gives 2,
and
length(transpose(row(m,1))) gives 3.
Post by Dimitri Pissarenko
Hello!
Concerning the determining of the size of matrix: Is it possible that
the description of this feature will be documented in Maxima's
documentation?
How could I help to do this?
TIA
dap
_______________________________________________
Maxima mailing list
http://www.math.utexas.edu/mailman/listinfo/maxima
Andrei Zorine
2004-01-29 16:09:08 UTC
Permalink
Yep, Maxima lacks some useful functions that operate on matrices.
Another two of them are _deleting_ rows and columns. I searched through
this mail archive from 2001 up to now and haven't found any question
concerning the operations. Does this mean that a) maxima users never
delete rows/cols, they only add cols/rows, or b) these functions are
present but are undocumented, or c) writing these functions should be a
primer finger-excersise for any maxima beginner?

I had to write them myself, and I attach my code here. Hope somebody
will find it useful. (Maybe the chapter 'Definitions for Matrices and
Linear Algebra' could be extended?)

--
Andrei Zorine
Post by Dimitri Pissarenko
Hello!
Concerning the determining of the size of matrix: Is it possible that
the description of this feature will be documented in Maxima's
documentation?
How could I help to do this?
TIA
dap
_______________________________________________
Maxima mailing list
http://www.math.utexas.edu/mailman/listinfo/maxima
Stavros Macrakis
2004-01-29 16:59:47 UTC
Permalink
Post by Andrei Zorine
Maxima lacks some useful functions that operate on matrices.
Another two of them are _deleting_ rows and columns.
...Does this mean that a) maxima users never delete rows/cols,
they only add cols/rows, or b) these functions are
present but are undocumented, or c) writing these functions
should be a primer finger-excersise for any maxima beginner?
The functions are present and documented.

- Function: SUBMATRIX (m1, ..., M, n1, ...)
creates a new matrix composed of the matrix M with rows mi
deleted, and columns ni deleted.

- Function: MINOR (M, i, j)
computes the i,j minor of the matrix M. That is, M with row i and
column j removed.

They are also easy finger exercises for beginners (no error-checking for
simplicity):

removerow(m,r):=
genmatrix(
lambda([i,j],
m[ if i>=r then i+1 else i, j ]),
length(m)-1,
length(m[1]));

removecol(m,c):=
genmatrix(
lambda([i,j],
m[ i, if j>=c then j+1 else j ]),
length(m),
length(m[1])-1);
Andrei Zorine
2004-01-29 18:30:59 UTC
Permalink
Excuse me, but I was taught at my University that for a matrix M its
submatrix contains rows i1,i2,... and columnt j1,j2,.... And a minor of
k-th order of a matrix is a determinant of a submatrix od size k... my
Encyclopedia of Mathematics say so too. So, when I look for a certain
function in the docs I assume it is named corectly.
--
Andrei Zorine

p.s. thanks for genmatrix trick.
Post by Stavros Macrakis
Post by Andrei Zorine
Maxima lacks some useful functions that operate on matrices.
Another two of them are _deleting_ rows and columns.
...Does this mean that a) maxima users never delete rows/cols,
they only add cols/rows, or b) these functions are
present but are undocumented, or c) writing these functions
should be a primer finger-excersise for any maxima beginner?
The functions are present and documented.
- Function: SUBMATRIX (m1, ..., M, n1, ...)
creates a new matrix composed of the matrix M with rows mi
deleted, and columns ni deleted.
- Function: MINOR (M, i, j)
computes the i,j minor of the matrix M. That is, M with row i and
column j removed.
They are also easy finger exercises for beginners (no error-checking for
removerow(m,r):=
genmatrix(
lambda([i,j],
m[ if i>=r then i+1 else i, j ]),
length(m)-1,
length(m[1]));
removecol(m,c):=
genmatrix(
lambda([i,j],
m[ i, if j>=c then j+1 else j ]),
length(m),
length(m[1])-1);
_______________________________________________
Maxima mailing list
http://www.math.utexas.edu/mailman/listinfo/maxima
Stavros Macrakis
2004-01-29 19:25:02 UTC
Permalink
Post by Andrei Zorine
Excuse me, but I was taught at my University that for a
matrix M its submatrix contains rows i1,i2,... and columnt
j1,j2,....
According to mathworld, a submatrix must be a contiguous subblock, not
containing arbitrary rows i1, i2, etc. but i1, i1+1, ... in. Perhaps
there is no standard notation for a submatrix? Still, though the Maxima
function submatrix subsumes the functionality of the block definition, I
agree that its syntax is not convenient.
Post by Andrei Zorine
a minor of k-th order of a matrix is a determinant of a
submatrix od size k...
Yes, and normally minor by itself (without an order specified) means
what Maxima writes as determinant(minor(m,i,j)).

So I agree that the terminology could be better.

-s

Stavros Macrakis
2004-01-29 18:47:17 UTC
Permalink
Here are more concise ways of deleting rows and columns without using
the built-in submatrix function:

removerow(m,r):=part(m,allbut(r))$
removecol(m,c):=transpose(removerow(transpose(m),c))$

Removerow is about as efficient as submatrix. Removecol is slower,
because it has to make two complete copies of the matrix. Both are
faster than the genmatrix approach, since part is native. The addrow
approach is inefficient because it makes many new matrices. The addcol
approach is even more inefficient. I tried to measure speeds of
compiled code, but there was a bug in Translate (887152 -- fix supplied)
and a bug in Genmatrix/lambda (887174 -- yuck).

-s
Stavros Macrakis
2004-01-29 13:43:27 UTC
Permalink
Post by Dimitri Pissarenko
Is there a way to determine the size (number of rows, number of
columns) of a matrix in Maxima?
Number of columns: length(m)
Number of rows: length(m[1])

Explanation:

m: matrix([a,b,c],[d,e,f])$
op(m) => matrix
args(m) => [ [a,b,c], [d,e,f] ]
length(args(m)) == length(m) => 2
m[1] => [a,b,c]
(Note that this is NOT the same as row(m,1) =>
matrix([a,b,c]), a matrix containing one row.)
length(m[1]) => 3
(In a matrix, all rows are guaranteed to be the
same length.)
Dimitri Pissarenko
2004-01-29 14:21:27 UTC
Permalink
Thanks everybody for the help!

dap
Loading...