Matrix (MathObject Class)

From WeBWorK_wiki
Revision as of 15:41, 2 August 2012 by Dpvc (talk | contribs) (Created page)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

The Matrix Class

The Matrix class implements matrices (in any dimension) of real entries, though the most common use is 2-dimensional ones (i.e., [math]n\times m[/math] matrices). A 1-dimensional Matrix is effectively a Vector. Typically, the rows of a Matrix are comma-separated lists of numbers delimited by square brackets, and a Matrix is a comma separated list of rows delimited by square brackets. E.g., [[1,2,3],[4,5,6]] is a [math]2\times 3[/math] matrix whose first row has entries 1, 2 and 3, and whose second row has entries 4, 5 and 6. Matrices can be combined by addition, subtraction, and multiplication, when the dimensions match up properly, and square matrices can have integer powers taken of them (meaning repeated multiplication). You can perform scalar multiplication and division, and also matrix-vector multiplication.

   Context("Matrix");
   
   $M = Matrix([1,2,3],[4,5,6]);
   $M = Matrix([[1,2,3],[4,5,6]]);
   $M = Matrix("[[1,2,3],[4,5,6]]");
   $M = Compute("[[1,2,3],[4,5,6]]");
   
   $M1 = Matrix([1,2],[3,4]);
   $M2 = Matrix([5,6],[7,8]);
   $v = Vector(9,10);
   
   $M3 = $M1 + $M2;     # same as Matrix([6,8],[10,12]);
   $A = $M1 * $M2;      # usual matrix multiplication
   $B = $M1 ** 2;       # same as $M1 * $M1
   $C = 3 * $M1;        # same as Matrix([3,6],[9,12]);
   $D = $M1 / 2;        # same as Matrix([.5,1],[1.5,2]);
   
   $u = $M1 * $v        # matrix-vector multiplication

The value of $u above will be a [math]2\times 1[/math] matrix, which is effectively a column vector but it is not a Vector object, it is a Matrix object. You can force it to be a Vector object using $u = Vector($M1 * $v); or $u = ColumnVector($m1 * $v) instead.

Students can enter Matrix-valued answers using the bracket notation above, or you can add Matrix-valued constants to the Context of your problem and have them write answers as matrix expressions in terms of those constants. Alternatively, you can use $M->ans_array() rather than ans_rule() to create an array of answer blanks where the student enters the entries of the matrix individually.

Matrices have a number of methods that can be used to compute things like determinants, inverses, transposes, and so on. See ```(need reference)``` for complete details, but some examples are

   $a = $M1->det;
   $A = $M1->transpose;
   $B = $M1->inverse;
   $t = $M1->trace;
   $v = $M1->row(1);        # the first row of the matrix
   $v = $M1->column(1);     # the first column of the matrix
   $a = $M1->element(2,1);  # the element in the second row, first column

Note that $M1->row(1) and $M1->column(1) produce Matrix objects, not Vectors. You can coerce them to Vectors via Vector($M1->row(1)) and ColumnVector($M1->column(1)).

It is possible to create Matrices of complex numbers, but there is no pre-defined Context that makes this easy to do.

Higher-dimensional Matrices can be obtained as a comma-separated list of lower-dimensional matrices delimited by square brackets. For example, Matrix([[[1,2],[3,4]],[[5,6],[7,8]]]) is a [math]2\times 2\times 2[/math] matrix (a 3-dimensional matrix).