This is the Math Object code for a Matrix.
Matrix
Allows students to enter [[3,4],[3,6]]
Complex-Matrix
Allows complex entries
Examples:
$M1 = Matrix(1, 2, 3); # degree 1 (row vector)
$M1 = Matrix([1, 2, 3]);
$M2 = Matrix([1, 2], [3, 4]); # degree 2 (matrix)
$M2 = Matrix([[1, 2], [3, 4]]);
$M3 = Matrix([[1, 2], [3, 4]], [[5, 6], [7, 8]]); # degree 3 (tensor)
$M3 = Matrix([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]);
$M4 = ...
$matrix->value produces an array of references to nested arrays, except at
the deepest level, where there will be the more basic MathObjects that make
up the Matrix (e.g. Real, Complex, Fraction, a mix of these, etc)
$M1->value is (1, 2, 3)
$M2->value is ([1, 2], [3, 4])
$M3->value is ([[1, 2], [3, 4]], [[5, 6], [7, 8]])
$matrix->wwMatrix produces CPAN MatrixReal1 matrix, used for computation subroutines and can only
be used on a degree 1 or degree 2 Matrix.
$matrix->dimensions produces an array. For an n-degree Matrix, the array has n entries for
the n dimensions.
$matrix->degree returns the degree of the Matrix (the depth of the nested array).
row(i) : MathObject Matrix
For a degree 1 Matrix, produces a degree 1 Matrix
For a degree n Matrix with n > 1, produces a degree (n-1) Matrix
column(j) : MathObject Matrix or Real or Complex
For a degree 1 Matrix, produces a Real or Complex
For a degree n Matrix with n > 1, produces a degree n Matrix where the 2nd dimesion is length 1
element : Real/Complex/Fraction value when passed the same number of arguments as the degree of the Matrix.
If passed more than n arguments, null. If the degree of the Matrix is n and C<element> is passed
k arguments with k < n, then this produces the corresponding degree (n-k) tensor.
see C<change_matrix_entry()> in MatrixReduce and L<http://webwork.maa.org/moodle/mod/forum/discuss.php?d=2970>
$matrix->data: ARRAY reference (internal data) of MathObjects (Real, Complex, Fractions)
stored at each location.
These cover subroutines in Matrix.pm
(which overrides or augment CPAN's MatrixReal1.pm
). Matrix
is a specialized subclass of MatrixReal1.pm
. The actual calculations for these methods are done in pg/lib/Matrix.pm
trace
proj
proj_coeff
L
R
PL
PR
These cover subroutines in pg/lib/MatrixReal1.pm
(this has been modified to handle complex numbers) The actual calculations are done in MatrixReal1.pm
subroutines. The commands below are Value::Matrix
methods unless otherwise noted.
condition
det
inverse
is_symmetric
decompose_LR
dim
norm_one
norm_max
kleene
normalize
solve_LR($v) - LR decomposition
solve($M,$v) - function version of solve_LR
order_LR - order of LR decomposition matrix (number of non-zero equations)(also order() )
order($M) - function version of order_LR
solve_GSM
solve_SSM
solve_RM
One can use fractions in Matrices by including Context("Fraction")
. For example
Context("Fraction");
$A = Matrix([
[Fraction(1,1), Fraction(1,2), Fraction(1,3)],
[Fraction(1,2), Fraction(1,3), Fraction(1,4)],
[Fraction(1,3), Fraction(1,4), Fraction(1,5)]
]);
and operations will be done using rational arithmetic. Also helpful is the method apply_fraction_to_matrix_entries
in the MatrixReduce.pl macro. Some additional information can be found at https://webwork.maa.org/moodle/mod/forum/discuss.php?d=2978.
value
Returns the array of arrayrefs of the matrix.
Usage:
$A = Matrix([ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ] ]);
$A->value;
# returns ([1,2,3,4],[5,6,7,8],[9,10,11,12])
dimensions
Returns the dimensions of the matrix as an array
Usage:
$A = Matrix([ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ] ]);
$A->dimensions;
returns the array (3,4)
$B = Matrix([ [ [ 1, 2 ], [ 3, 4 ] ], [ [ 5, 6 ], [ 7, 8 ] ] ]);
$B->dimensions;
returns (2,2,2)
isSquare
Return true if the Matrix is degree 1 of length 1 or its last two dimensions are equal, false otherwise
Usage:
$A = Matrix([ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]);
$B = Matrix([ [ 1, 0, 0 ], [ 0, 1, 0 ] ]);
$C = Matrix(1);
$D = Matrix(1, 2);
$E = Matrix([ [ [ 1, 2 ], [ 3, 4 ] ] ]);
$F = Matrix([ [ [ 1, 2 ] ], [ [ 3, 4 ] ] ]);
$A->isSquare; # is 1 (true) because it is a 3x3 matrix
$B->isSquare; # is '' (false) because it is a 2x3 matrix
$C->isSquare; # is 1 (true) because it is a degree 1 matrix of length 1
$D->isSquare; # is '' (false) because it is a degree 1 matrix of length 2
$E->isSquare; # is 1 (true) because it is a 1x2x2 tensor
$F->isSquare; # is '' (false) because it is a 2x1x2 tensor
isRow
Return true if the matix is degree 1 (i.e., is a matrix row)
Usage:
$A = Matrix([ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ] ]);
$row_vect = Matrix([ 1, 2, 3, 4 ]);
$A->isRow; # is '' (false)
$row_vect->isRow; # is 1 (true)
isOne
Check for identity Matrix (for degree > 2, this means the last two dimensions hold identity matrices)
Usage:
$A = Matrix([ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ], [13, 14, 15, 16] ]);
$A->isOne; # is false
$B = Matrix([ [ 1, 0, 0 ], [ 0, 1, 0 ], [ 0, 0, 1 ] ]);
$B->isOne; # is true;
$C = Matrix(1);
$C->isOne; # is true
$D = Matrix([ [ [ 1, 0 ], [ 0, 1 ] ], [ [ 1, 0 ], [ 0, 1 ] ] ]);
$D->isOne; # is true
isZero
Check for zero Matrix
Usage:
$A = Matrix([ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ], [13, 14, 15, 16] ]);
$A->isZero; # is false
$B = Matrix([ [ 0, 0, 0 ], [ 0, 0, 0 ], [ 0, 0, 0 ] ]);
$B->isZero; # is true;
$C = Matrix([ [ [ 1, 0 ], [ 0, 1 ] ], [ [ 1, 0 ], [ 0, 1 ] ] ]);
$C->isZero; # is false
$D = Matrix([ [ [ 0, 0 ], [ 0, 0 ] ], [ [ 0, 0 ], [ 0, 0 ] ] ]);
$D->isZero; # is true
isUpperTriangular
Check if a Matrix is upper triangular (for degree > 2, applies to frontal slice matrices)
isLowerTriangular
Check if a Matrix is lower triangular (for degree > 2, applies to frontal slice matrices)
isDiagonal
Check if a Matrix is diagonal (for degree > 2, applies to frontal slice matrices)
isSymmetric
Check if a Matrix is symmetric (for degree > 2, applies to frontal slice matrices)
isOrthogonal
Check if a Matrix is orthogonal (for degree > 2, applies to frontal slice matrices)
isREF
Check if a Matrix is in row echelon form (for degree > 2, applies to frontal slice matrices)
isRREF
Check if a Matrix is in reduced row echelon form (for degree > 2, applies to frontal slice matrices)
slice
Apply this to a degree n Matrix, passing (m, k), and produce the degree (n-1) Matrix defined by taking all entries whose position has mth index with value k. For example if $M
is a 4x5x6 Matrix, then m can be 1, 2, or 3. If m is 2, then k can be 1, 2, 3, 4, or 5. $M-<gt
slice(2,3)> is the 4x6 Matrix such that the entry at position (i,j) is the entry of $M
at position (i,3,j).
Usage:
$A = Matrix([ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ] ]);
$A->slice(1, 2)
# Index 1 identifies the 1st, 2nd, or 3rd row above, and with value 2 we get the second one:
# Matrix([5, 6, 7, 8])
$B = Matrix([ [ [ 1, 2 ], [ 3, 4 ] ], [ [ 5, 6 ], [ 7, 8 ] ] ]);
$B->slice(1, 2)
# Index 1 identifies the two arrays at depth 1, and with value 2 we get the second one:
# Matrix([ [ 5, 6 ], [ 7, 8 ] ])
$B->slice(2, 1)
# Here we take all entries from $B where the 2nd index is 1: the 1 at position (1,1,1),
# the 2 at position (1,1,2), the 5 at position (2,1,1), and the 6 at position (2,1,2):
# Matrix([ [ 1, 2 ], [ 5, 6 ] ])
$B->slice(3, 1)
# Here we take all entries from $B where the 3rd index is 1: the 1 at position (1,1,1),
# the 3 at position (1,2,1), the 5 at position (2,1,1), and the 7 at position (2,2,1):
# Matrix([ [ 1, 3 ], [ 5, 7 ] ])
transpose
Take the transpose of a matrix. For a degree 1 Matrix, first promote to a degree 2 Matrix. For a degree n Matrix, apply a permutation of the indices. The default permutation transposes the last two indices. To specify a permutation, provide an array reference representing a cycle or an array of array references that represents a product of cycles. If a permutation is not specified, the default is the usual transposition of the last two indices.
Usage:
$A = Matrix([
[ 1, 2, 3, 4 ],
[ 5, 6, 7, 8 ],
[ 9, 10, 11, 12 ]
]);
$A->transpose
# will be
# [
# [ 1, 5, 9 ],
# [ 2, 6, 10 ],
# [ 3, 7, 11 ],
# [ 4, 8, 12 ]
# ]
$B = Matrix([
[ [ 1, 2 ], [ 3, 4 ] ],
[ [ 5, 6 ], [ 7, 8 ] ]
]);
$B->transpose([1, 2, 3])
# will be
# [
# [ [ 1, 3 ], [ 5, 7 ] ],
# [ [ 2, 4 ], [ 6, 8 ] ]
# ]
$C = Matrix([
[ [ [ 1, 2 ], [ 3, 4 ] ], [ [ 5, 6 ], [ 7, 8 ] ] ],
[ [ [ 9, 10 ], [ 11, 12 ] ], [ [ 13, 14 ], [ 15, 16 ] ] ]
]);
$C->transpose([ [ 1, 2], [3, 4] ])
# will be
# [
# [ [ [ 1, 3 ], [ 2, 4 ] ], [ [ 9, 11 ], [ 10, 12 ] ] ],
# [ [ [ 5, 7 ], [ 6, 8 ] ], [ [ 13, 15 ], [ 14, 16 ] ] ]
# ]
I
Get an identity Matrix of the requested size
Value::Matrix->I(n)
Usage:
Value::Matrix->I(3); # returns a 3x3 identity matrix.
Value::Matrix->I([2],3); # returns a 2x3x3 identity Matrix (tensor)
Value::Matrix->I([2,4],2); # returns a 2x4x2x2 identity Matrix (tensor)
$A->I; # return an identity matrix of the appropriate degree and dimensions such that I*A = A
E
Get a degree 2 elementary matrix of the requested size and type. These include matrix that upon left multiply will perform row operations.
Row Swap
To perform a row swap between rows i
and j
, then E(n,[i, j])
.
Usage:
Value::Matrix->E(3, [ 1, 3 ]);
returns the matrix
[[0, 0, 1],
[0, 1, 0],
[1, 0, 0]]
or if the matrix $A
exists then
$A->E([1, 3]);
where the size of the resulting matrix is the number of rows of $A
.
Multiply a row by a constant
To create the matrix that will multiply a row i
, by constant k
, then E(n,[i],k)
Usage:
Value::Matrix->E(4, [2], 3);
generates the matrix
[ [ 1, 0, 0, 0 ],
[ 0, 4, 0, 0 ],
[ 0, 0, 1, 0 ],
[ 0, 0, 0, 1 ] ]
or if the matrix $A
exists then
$A->E([4], 3);
will generate the elementary matrix of size number of rows of $A
, which multiplies row 4 by 3.
Multiply a row by a constant and add to another row.
To create the matrix that will multiply a row i
, by constant k
and add to row j
then E(n,[i, j],k)
Usage:
Value::Matrix->E(4, [ 3, 2 ], -3);
generates the matrix:
[ [ 1, 0, 0, 0 ],
[ 0, 1, 0, 0 ],
[ 0, -3, 1, 0 ],
[ 0, 0, 0, 1 ] ]
or if the matrix $A
exists then
$A->E([3, 4], -5);
will generate the elementary matrix of size number of rows of $A
, which multiplies row 3 by -5 and adds to row 4.
P
Creates a degree 2 permutation matrix of the requested size.
Value::Matrix->P(n,(cycles))
in general where cycles
is a sequence of array references of the cycles.
If one has an existing matrix $A
, then $A->P(cycles)
generals a permutation matrix of the same size as $A
.
Usage:
Value::Matrix->P(3,[1, 2, 3]); # corresponds to cycle (123) applied to rows of I_3.
returns the matrix [[0,1,0],[0,0,1],[1,0,0]]
Value::Matrix->P(6,[1,3],[2,4,6]); # permutation matrix on cycle product (13)(246)
returns the matrix
[[0,0,1,0,0,0],
[0,0,0,0,0,1],
[1,0,0,0,0,0],
[0,1,0,0,0,0],
[0,0,0,0,1,0],
[0,0,0,1,0,0]]
$A = Matrix([ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ], [13, 14, 15, 16] ]);
$P3 = $A->P([1,4]);
returns the matrix [[0,0,0,1],[0,1,0,0],[0,0,1,0],[1,0,0,0]]
Zero
Create a degree 2 zero matrix of requested size. If called on existing matrix, creates a matrix as the same size as given matrix.
Usage:
Value::Matrix->Zero(m,n); # creates a m by n zero matrix.
Value::Matrix->Zero(n); # creates an n ny n zero matrix.
$A1 = Matrix([ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ] ]);
$A1->Zero; # generates a zero matrix as same size as $A1.
row
Extract a given row from the matrix. For a degree 1 Matrix, $M->row(1) will return $M itself. Otherwise, a "row" is defined by the first index. For an degree n Matrix, a "row" will be a degree (n-1) Matrix.
Usage:
$A1 = Matrix([ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ] ]);
$A1->row(2); # returns the row Matrix [5,6,7,8]
column
Extract a given column from the matrix. For a degree 1 Matrix, $M-
column(j)> will return the jth entry. Otherwise, for an degree n Matrix, $M-
column(j)> returns an degree n Matrix tensor using j for the second index. To obtain the corresponding degree (n-1) Matrix, see slice()
.
Usage:
$A1 = Matrix([ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ] ]);
$A1->column(2); # returns the column Matrix [[2],[6],[10]]
element
Extract an element from the given position.
Usage:
$A = Matrix([ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ] ]);
$A->element(2,3); # returns 7
$B = Matrix([ [ [ 1, 2 ], [ 3, 4 ] ], [ [ 5, 6 ], [ 7, 8 ] ] ]);
$B->element(1,2,1); # returns 3;
$row = Matrix([4,3,2,1]);
$row->element(2); # returns 3;