## WeBWorK Main Forum

### operations on Matrix MathObjects with dimension(s) 1 ### operations on Matrix MathObjects with dimension(s) 1

by Gavin LaRose -
Number of replies: 1
Hi all,

In the context of partitioned matrices I'm trying to do the following:

    $a11 = Matrix( [ 1 ] );$a12 = Matrix( [ 2, 3, 4 ] );
$a21 = Matrix( [ 5 ], [ 6 ], [ 7 ] );$a22 = Matrix( [ 8, 9, 10 ], [ 11, 12, 13 ], [ 14, 15, 16 ] );
$b11 = Matrix( [ 21 ] );$b12 = Matrix( [ 22, 23, 24 ] );
$b21 = Matrix( [ 25 ], [ 26 ], [ 27 ] );$b22 = Matrix( [ 28, 29, 30 ], [ 31, 32, 33 ], [ 34, 35, 36 ] );

$c11 =$a11 * $b11 +$a12 * $b21;$c12 = $a11 *$b12 + $a12 *$b22;
$c21 =$a21 * $b11 +$a22 * $b21;$c22 = $a21 *$b12 + $a22 *$b22;


but this fails: I'm accused of trying to add or multiply matrices of different dimensions. I tried building the matrices by explicitly indicating the full structure, e.g.,

    $a11 = Matrix( [ [ 1 ] ] );$a12 = Matrix( [ [  2, 3, 4 ] ] );
$a21 = Matrix( [ [ 5 ], [ 6 ], [ 7 ] ] );$a22 = Matrix( [ [ 8, 9, 10 ], [ 11, 12, 13 ], [ 14, 15, 16 ] ] );


in which case the problem displays fine, but the answers solicited by $c11->ans_array() and $c12->ans_array() are marked wrong with the message "Matrix dimension is not correct."

Any suggestions are welcome.

Thanks,
Gavin ### Re: operations on Matrix MathObjects with dimension(s) 1

by Davide Cervone -
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

   Matrix([1,2,3],[4,5,6])

is the same as
  Matrix([[1,2,3],[4,5,6]])

which is a 2x3 matrix. This is meant for convenience, and it works well most of the time, but there is one ambiguity:
  Matrix([1,2,3])

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:
  Matrix([[1,2,3]]);

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.

Davide