Rax Adaam
Rax Adaam

Reputation: 810

Maxima: how to distinguish between a row of a matrix and a row vector?

I'd like to build a function that can take a vector (i.e. a 1xm or nx1 matrix) or a column / row of a matrix, as input; however, I've come up on something that seems a bit weird: even though maxima handles vectors as matrices with either 1 row or col, it has different requirements for referring to their elements.

For example:

aMatrix:matrix([1,2,3],[4,5,6]);
matrixVec: aMatrix[1];
aVec:matrix([1,2,3]);

Now, even though matrixVec and aVec were a) obtained from the matrix function, and have the same dimensions (as determined by length() and length(transpose()), referencing their elements requires completely different notations: matrixVec[1,1]; returns an error; whereas aVec[1,1]; returns 1, as expected.

I think I understand why this would be; however, because both of these objects return true from matrixp (and have the same dimensions), I have no idea how to distinguish them in my code, so that I can define proper handling.

What kind of if statement could I use to distinguish these two so that I can define value = x[i] for the matrix and value = x[1,i] for the row vector?

Upvotes: 2

Views: 265

Answers (1)

Rax Adaam
Rax Adaam

Reputation: 810

Stumbled upon a solution while working on something else: it turns out that Maxima treats the row or column of a matrix as a list, although it doesn't treat a row or column vector as a list, i.e. given

aMatrix : matrix([1,2,3],[4,5,6]);
matrixVec : aMatrix[1];
aVec : matrix([1,2,3]);

listp(matrixVec) returns "true" whereas listp(aVec) returns "false".

i.e. listp() can be used to distinguish a 1xm or nx1 matrix from the row or column of a matrix.

Upvotes: 2

Related Questions