Matrix (MathObject Class)
Contents
Matrix Class[edit]
The Matrix class implements matrices (in any dimension) of Real entries, though the most common use is 2dimensional ones (i.e., [math]n\times m[/math] matrices). A 1dimensional Matrix is effectively a Vector. Typically, the rows of a Matrix are commaseparated 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 formed in the Matrix
Context. It is possible to have Complex entries in a Matrix, but there is not a predefined Context that makes that easy to do.
The answer checker for Matrices can give students hints about whether the dimension of the matrix is correct. It is also possible to have an array of answer blanks rather than having the student type the entire matrix in one long answer blank.
Construction[edit]
Matrices are created via the Matrix()
function, or by Compute()
.
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]]");
Higherdimensional Matrices can be obtained as a commaseparated list of lowerdimensional matrices delimited by square brackets. For example,
$M = Matrix([[[1,2],[3,4]],[[5,6],[7,8]]])
produces a [math]2\times 2\times 2[/math] matrix (a 3dimensional matrix).
The identity matrix for any dimension can be obtained from Value::Matrix>I(n)
where n
is the dimension of the matrix. For example
$I = Value::Matrix>I(2);
is a [math]2\times 2[/math] identity matrix.
Operations on Matrices[edit]
Matrices can be combined by addition, subtraction, and multiplication, when the dimensions match up properly. Square matrices can have integer powers taken of them (meaning repeated multiplication). You can perform scalar multiplication and division, and also matrixvector multiplication.
$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 # matrixvector 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.
Matrices have a number of methods that can be used to compute things like determinants, inverses, transposes, and so on; these are listed in the Methods section below. 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))
.
Answer Checker[edit]
As with all MathObjects, you obtain an answer checker for a Matrix object via the cmp()
method:
ANS(Matrix([[4,0,2],[1,1,5]])>cmp);
The Matrix class supports the common answerchecker options, and the following additional options:
Option  Description  Default 

showDimensionHints => 1 or 0

Show/don't show messages about the wrong number of rows or columns.  1

If you use a single answer blank for a Matrix answer checker, this means the student must type the entire Matrix, including the brackets and commas, all on one line. This allows the student to enter Matrixvalues expressions (like sums or multiplications), but it is hard to type, and difficult to see the Matrix as an array when it is presented linearly in this way. It is more common, therefore, to use the Matrix object's ans_array()
method rather than PG's ans_rule()
function to obtain a separate answer box for each entry. The Matrix answer checker will manage this collection of answer boxes for you, so your code doesn't have to change in any other way.
Context("Matrix"); $M = Matrix([[2,4,1],[1,0,3]]); Context()>texStrings; BEGIN_TEXT \($M\) = \{$M>ans_array\} END_TEXT Context()>normalStrings; ANS($M>cmp);
If you want students to enter Matrixvalued expressions, you can add Matrixvalued constants to the Context of your problem and have them write answers as matrix expressions in terms of those constants. For example
Context("Numeric"); # Numeric prevents students from entering Matrices or Vectors directly Context()>constants>add( M => Matrix([pi**2,1/pi],[sqrt(2),ln(pi)]), # an arbitrary matrix with no special properties or symmetries I => Matrix([[1,0],[0,1]]), # identity matrix ); Context()>flags>set( formatStudentAnswer => 'parsed', # leave student's answer as a formula ); $f = Formula("M^2 + 3M  I"); # a Matrix formula Context()>texStrings; BEGIN_TEXT \($f\) = \{ans_rule(20)\} END_TEXT Context()>normalStrings; ANS($f>cmp);
Methods[edit]
The Matrix class supports the Common MathObject Methods, and the following additional methods:
Method  Description 

$M>det

Returns the determinant of $M as a Real.

$M>inverse

Returns the inverse of $M . Note that $M must be square.

$M>transpose

Returns the transpose of $M .

$M>trace

Returns the trace of $M . Note that $M must be square.

$M>proj($V)

Returns the projection of $V onto the column space of $M . Note that $V must be a Matrix with one column.

$M>proj_coeff($V)

Returns the coordinates of $M>proj($V) . As with $M>proj , the columns of $M should be linearly independent.

$M>row(n)

Returns the [math]n[/math]th row of $M as a 1dimensional Matrix. To obtain it as a Vector, use Vector($M>row(n)) .

$M>column(n)

Returns the [math]n[/math]th column of $M as a 1dimensional Matrix. To obtain it as a Vector, use Vector($M>column(n)) or ColumnVector($m>column(n)) .

$M>element(i,j)

Returns the [math](i,j)[/math]th element of $M (i.e., the element in row [math]i[/math] and column [math]j[/math]) as a Real.

$M>dimensions

Returns an array of number of rows and columns of $M if it is 2dimensional. (If 1dimensional, it is the number of elements in $M , if 3dimensional, it is three number giving the sizes of the three dimension, and so on.)

$M>isZero

Returns 1 if the matrix is a zero matrix and 0 otherwise. (Overrides MathObject method.) 
$M>isOne

Returns 1 if the matrix is an identity matrix and 0 otherwise. (Overrides MathObject method.) 
$M>isSquare

Returns 1 if the matrix is [math]n\times n[/math] and 0 otherwise. 
$M>isRow

Returns 1 if the matrix is 1dimensional, 0 otherwise. 
$M>is_symmetric

Returns 1 if the matrix is symmetric across the main diagonal, 0 otherwise. 
$M>wwMatrix

Returns returns an oldstyle Matrix from the Matrix package. Note that if $M1 is an oldstyle Matrix object, Matrix($M1) will convert it to a MathObject Matrix.

$M>norm_one

Returns the onenorm of $M (the maximum of the sums of the columns).

$M>norm_max

Returns the maximumnorm of $M (the maximum of the sums of the rows).

$M>kleene

Returns a copy of $M where Kleene's algorithm has been applied. Note that $M must be square.

$M>normalize($v)

Returns a matrix and vector that have been "normalized" to improve numeric stability, but still represent the same system as the original $M and $v . Note that $M must be square.

$M>decompose_LR

Returns a matrix that encodes the LR decomposition of $M . Note that $M must be square. ([math]L[/math] is a lowertriangular matrix and [math]R[/math] is an uppertriangular matrix where [math]L R = M[/math], but the matrix returned here is a special combination of the two stored in one square matrix, the socalled LR matrix.)

$M>L

Returns the [math]L[/math] matrix from the LR decomposition of $M .

$M>R

Returns the [math]R[/math] matrix from the LR decomposition of $M .

$M>PL

Returns the left pivot permutation matrix for the LR decomposition of $M . (Note that [math]PL \, L \, R \, PR = M[/math].)

$M>PR

Returns the right pivot permuation matrix for the LR decomposition of $M . (Note that [math]PL \, L \, R \, PR = M[/math].)

$A>solve_LR($b)

Solves the system [math]A\boldsymbol{x}=\boldsymbol{b}[/math] using an LR matrix and returns an array consisting of the dimension of the solution space, the solution vector (as a Matrix object), and the base matrix (see the MatrixReal1 documentation for details). 
$M>condition($Minv)

Shorthand for $M>norm_one*$Minv>norm_one . Note that $Minv should be the inverse of $M , and both must be square. The number is a measure of the numerical stability of the matrix; the smaller the number, the better stabiity.

$M>order_LR

Returns the order of $M (the number of linearly independent columns or rows).

$A>solve_GSM($x0,$b,$epsilon)

Solves the system [math]A\boldsymbol{x}=\boldsymbol{b}[/math] using the Global Step Method fixedpoint iterative approach. Here, $x0 is a starting guess solution (that should be near the actual solution), $b is the vector [math]\boldsymbol{b}[/math], and $epsilon is an error tolerance for determining when to stop. See the MatrixReal1 documentation for details.

$A>solve_SSM($x0,$b,$epsilon)

Solves the system [math]A\boldsymbol{x}=\boldsymbol{b}[/math] using the Single Step Method fixedpoint iterative approach. Here, $x0 is a starting guess solution (that should be near the actual solution), $b is the vector [math]\boldsymbol{b}[/math], and $epsilon is an error tolerance for determining when to stop. See the MatrixReal1 documentation for details.

$A>solve_RM($x0,$b,$weight,$epsilon)

Solves the system [math]A\boldsymbol{x}=\boldsymbol{b}[/math] using the Relaxation Method fixedpoint iterative approach. Here, $x0 is a starting guess solution (that should be near the actual solution), $b is the vector [math]\boldsymbol{b}[/math], $weight is a number between 0 and 2, and $epsilon is an error tolerance for determining when to stop. See the MatrixReal1 documentation for details.

Properties[edit]
The Matrix class supports the Common MathObject Properties, and the following additional ones:
Property  Description  Default 

$r>{open}

The symbol to use for the open square bracket  [

$r>{close}

The symbol to use for the close square bracket  ]
