WeBWorK Main Forum

operations on Matrix MathObjects with dimension(s) 1

Re: operations on Matrix MathObjects with dimension(s) 1

by Davide Cervone -
Number of replies: 0
There are two things going on here, one a bug in MathObject (for which I have submitted a patch) and one a misunderstanding of the handling of the arguments for Matrix (no doubt aggravated by bad documentation).

Taking the second issue first, the Matrix object is more general than just a 2D array of numbers. It actually will handle arbitrary dimensions. For example, you can do a 3D matrix, or a 1D matrix (which is essentially a vector). Because of this, however, you do have to be a little careful about how you enter a matrix that has only one row.

The Matrix() macro treats multiple inputs as though they were enclosed in brackets. So

is the same as
which is a 2x3 matrix. This is meant for convenience, and it works well most of the time, but there is one ambiguity:
is not a 1x3 matrix, it is a 1-dimensional matrix of length 3 (i.e., essentially a vector). To get a 1x3 matrix, you must use the double brackets explicitly:
That is why when you added the brackets, your multiplications worked out OK.

[Note that this also means Matrix(1,2,3) is the same as Matrix([1,2,3]), so this is a 1D matrix of length 3).]

So that is the misunderstanding of what Matrix arguments mean. It is really best to include the extra brackets for a 2D matrix, but they are not necessary except for 1 x n matrices.

As for the bug, it turns out that there was a problem with ans_array for 1 x n matrices which caused the dimension mismatch error that you were getting. I have submitted a patch for it, and that should take care of it in the future. If you want a work-around for now, I can give you one, but it's not pretty.