Macro Library matrix
Matrix functions. Version 1.6, Jan 19
Contributors: David Lippman, Mike Jenck, Larry Green
matrix(vals,rows,cols)
Creates a new matrix item.
Use matrixformat() to create display or $answer from a matrix item
vals: list or array of numbers to form matrix values: R1C1,R1C2,...,R2C1,R2C2,...
rows, cols: dimensions of matrix
To define a matrix using calculations use:
$m = matrix(array(3/2,2^3,5+1,3),2,2)
matrixformat(matrix, [bracket])
Formats a matrix item into an ASCIIMath string for display or $answer
Defaults to square brackets, or set bracket to '(' or '|'.
matrixformatfrac(matrix, [bracket])
Formats a matrix item into an ASCIIMath string for display or $answer,
with entries converted to fractions where possible.
Defaults to square brackets, or set bracket to '(' or '|'.
matrixdisplaytable(matrix, [matrixname, displayASCIIticks, linemode, headernames, tablestyle, rownames, rowheader, caption])
Create a string that is a valid HTML table syntax for display.
matrix: a matrix to be displayed in a HTML table
matrixname: a string that holds the matrix name, like A or B. This does not contain
tick marks - if you want them you need to supply them.
default empty string
displayASCIIticks: put tick marks around each element of the table, either 0 or 1.
Use 0 if you are building an answerbox matrix.
0 do not use math ticks (default)
1 use math ticks
linemode: Show none, augments, or simplex, value is either 0, 1 or 2
0 show no lines (default)
1 show aumented line
2 show simplex lines
headernames: list or array of the variables "x1,x2,x3" that are used for the column titles.
default none
tablestyle: for any additional styles for the table that you may want. like "color:#40B3DF;"
default none
rownames: list or array of the variables "x1,x2,x3" that are used for the row titles.
default none
rowheader: string that is entered into the row name column in the header row
default none
caption: string - caption for the table
default none
matrixsystemdisp(matrix,[variables,leftbracket])
Writes out a matrix as an equivalent system of equations
variables is optional array of variables to use
leftbracket can be set to false to hide the left bracket
matrixgetentry(matrix,row,col)
get entry from a matrix at given row and col
rows and cols are 0 indexed (first row is row 0)
matrixgetrow(matrix,row,[asArray])
get row of a matrix as a new 1xm matrix
or array if asArray is set to true
rows and cols are 0 indexed (first row is row 0)
matrixgetcol(matrix,col,[asArray])
get col of a matrix as a new nx1 matrix
or array if asArray is set to true
rows and cols are 0 indexed (first row is row 0)
matrixgetsubmatrix(matrix,rowselector,colselector)
gets submatrix. rowselector and colselector are strings
with format: "start:end". ":" to select all
matrixsum(matrix,matrix)
Adds two matrices
matrixdiff(matrix1,matrix2)
Subtracts matrix1-matrix2
matrixscalar(matrix,n)
Multiplies the matrix times the number n
matrixprod(matrix1,matrix2)
Calculates the matrix product matrix1*matrix2
matrixaugment(matrix1,matrix2)
Augments matrix2 to the right side of matrix1
matrixrowscale(matrix,row,n)
Multiplies row of matrix by n
matrix rows are 0-indexed; first row is row 0
matrixrowswap(matrix,row1,row2)
swaps rows in matrix
matrix rows are 0-indexed; first row is row 0
matrixrowcombine(matrix,row1,a,row2,b,endrow)
replaces endrow in matrix with a*row1 + b*row2
matrix rows are 0-indexed; first row is row 0
matrixrowcombine3(matrix,row1,a,row2,b,row3,c,endrow)
replaces endrow in matrix with a*row1 + b*row2 + c*row3
matrix rows are 0-indexed; first row is row 0
matrixidentity(n)
Creates an n x n identity matrix
matrixtranspose(m)
Calculates the transpose of the matrix m
randinvertible(n)
Creates a random n x n invertible matrix by applying random row combinations to an identity matrix
returns an array of two matrices: M and M^-1
matrixrandunreduce(m,n)
Randomizes the matrix m by applying n random row combinations. These are sometimes multi-row
combinations, and sometimes additional operations are done to avoid a row of all zeros.
matrixinverse(m)
Finds the inverse of nxn matrices.
matrixinversefrac(m)
Finds the inverse of nxn matrices, with the resulting entries as fractions
the fraction entries are strings, so do NOT try to use the result of this
for calculations.
matrixsolve(A,b)
solves the matrix equation Ax = b
A and b are both matrices
A is nxn, b is nxm
returns nxm matrix x so Ax = b
matrixsolvefrac(A,b)
solves the matrix equation Ax = b
A and b are both matrices
A is nxn, b is nxm
returns nxm matrix x so Ax = b
entries may be fractions (as strings), so don't
try to use the result in calculations.
matrixreduce(A,[rref,frac])
reduces the matrix A to echelon or reduced echelon form
A is a matrix
rref = true for rref, false for echelon (default)
frac = true for fraction output, false for decimal output (default)
if true, entries may be fractions (as strings), so don't
try to use the result in calculations.
NOTE: In most cases, using matrixrandunreduce is a better option than using this!
matrixnumsolutions(A,n)
A is an arbitrary coefficient matrix augmented with n columns, after
being row reduced to reduced echelon form (see matrixreduce)
Returns the number of Ax=b equations that have at least one solution
matrixround(matrix, decimal places)
rounds each entry of the matrix the specified decimal places
polyregression(x,y,n)
find a nth degree polynomial that best fits the data
x,y arrays of data
returns an array (intercept, linear coeff, quad coeff, ...)
matrixGetRank(matrix)
returns the rank of a matrix
arrayIsZeroVector(vector)
vector is an array, not a matrix.
determines if a vector is the 0 vector
matrixFormMatrixFromEigValEigVec(eigenvalues,matrix of eigenvectors)
eigenvalues: The eigenvalues of the matrix include multiple times if
multiplicity > 0
matrix of eigenvectors: imput a matrix that consists of the eigenvectors of the
original matrix
returns the matrix PAP^-1
matrixFromEigenvals(eigenvalues)
Generates a matrix and random eigenvectors, where both have integer values, based on a
set of eigenvalues.
eigenvalues: An array of eigenvalues. If complex, give one of the conjugate pair as [real,imag].
Returns array(matrix, eigenvectors), where matrix is in the usual format, and eigenvectors is
an array of eigenvectors, where each element of the array is an array of the elements of that
eigenvector. If an element is complex, it is returned as [real,imag]. If an eigenvector has
complex elements, only one of the conjugate pair is returned.
matrixFormatEigenvecs(eigenvectors)
Takes a set of eigenvectors and formats them for display.
It returns an array of strings, each being the asciimath representation of that
eigenvector.
eigenvectors: in the same format matrixFromEigenvals returns. An array of eigenvectors,
where each is an array of the elements of that
eigenvector. If an element is complex, it should be entered as [real,imag]. If an
eigenvector has complex elements, only one of the conjugate pair should be listed.
Ex: [[1,2,3], [[1,3],[4,5],2]]
would describe the eigenvectors
(1,2,3), (1+3i,4+5i,2), and (1-3i,4-5i,2)
.
matrixIsRowsLinInd(matrix)
matrix: matrix's row vectors are the vectors we are looking at seeing if they
are linearly independent
returns true if they are LI and false if they are LD
matrixIsColsLinInd(matrix)
matrix: matrix's column vectors are the vectors we are looking at seeing if they
are linearly independent
returns true if they are LI and false if they are LD
matrixIsEigVec(matrix,eigenvector)
matrix: the matrix that we are testing
eigenvector: the possible eigenvector that we are checking. It is an
array, not a matrix.
returns true is eigenvector is an eigenvector of matrix. Otherwise it
returns false
matrixIsEigVal(matrix,eigenvalue)
matrix: the matrix that we are testing
eigenvalue a real number that we are testing to see if it is an eigenvalue of
matrix.
matrixGetRowSpace(matrix)
matrix: the matrix that we are finding the row space
returns a matrix whose rows are a basis of the row space of matrix
matrixGetColumnSpace(matrix)
matrix: the matrix that we are finding the column space
returns a matrix whose columns are a basis of the column space of matrix
matrixAxbHasSolution(matrix A,matrix b)
A is a marix and b is a mx1 matrix.
returns true if there is a solution and false if there isn't one
matrixAspansB(matrix A,matrix B)
A is the possible spanning set
This tests if the rows of A span the row space of B
matrixAbasisForB(matrix A, matrix B)
tests if the rows of A are a basis for the row space of B
matrixGetMinor(matrix,rowNo,colNo)
returns the n-1 by n-1 matrix minor obtained by removing the rowNo row and colNo
column. Only works for a square matrix.
det(matrix)
returns the determinant of a matrix
matrixRandomMatrix(min value, max value,number of rows,number of columns)
returns matrix with random integer entries where the integers are between max and min values.
For cases where you'll want to solve Ax=b, use matrixrandinvertible instead
matrixRandomSpan(matrix)
returns a matrix of rows that span the row space
of matrix
the number of rows of the spanning matrix will either be the same
or one larger than the original matrix's number of rows.
matrixNumberOfRows(matrix)
returns the number of rows of a matrix
matrixNumberOfColumns(matrix)
/returns the number of columns of a matrix
matrixParseStuans(student answer)
Converts a students matrix answer into a single array of values
student answer can come from $stuanswers[$thisq]
for single-part
questions, or getstuans($stuanswers, $thisq, (part index))
for
multi-part
matrixCompare(test matrix, correct matrix, [tol])
Compares the test matrix to the correct matrix, and returns
true if they are equivalent up to the tolerance.
Set tol to specify the relative tolerance (defaults to .001),
or prefix with | for an absolute tolerance.