Macro Library matrix

Matrix functions. Version 1.6, Jan 19
Contributors: David Lippman, Mike Jenck, Larry Green

matrix

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

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

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

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

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

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

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

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

matrixgetsubmatrix(matrix,rowselector,colselector)
gets submatrix. rowselector and colselector are strings
with format: "start:end". ":" to select all

matrixsum

matrixsum(matrix,matrix)
Adds two matrices

matrixdiff

matrixdiff(matrix1,matrix2)
Subtracts matrix1-matrix2

matrixscalar

matrixscalar(matrix,n)
Multiplies the matrix times the number n

matrixprod

matrixprod(matrix1,matrix2)
Calculates the matrix product matrix1*matrix2

matrixaugment

matrixaugment(matrix1,matrix2)
Augments matrix2 to the right side of matrix1

matrixrowscale

matrixrowscale(matrix,row,n)
Multiplies row of matrix by n
matrix rows are 0-indexed; first row is row 0

matrixrowswap

matrixrowswap(matrix,row1,row2)
swaps rows in matrix
matrix rows are 0-indexed; first row is row 0

matrixrowcombine

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

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

matrixidentity(n)
Creates an n x n identity matrix

matrixtranspose

matrixtranspose(m)
Calculates the transpose of the matrix m

matrixrandinvertible

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

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

matrixinverse(m)
Finds the inverse of nxn matrices.

matrixinversefrac

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

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

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

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

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

matrixround(matrix, decimal places)
rounds each entry of the matrix the specified decimal places

matrixFromCols

matrixFromCols(col1, col2, ...)
creates a matrix from the given columns. The columns can either be nx1 column matrices, or arrays.

matrixFromRows

matrixFromRows(row1, row2, ...)
creates a matrix from the given rows. The row2 can either be 1xn row matrices, or arrays.

polyregression

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

 matrixGetRank(matrix)
 returns the rank of a matrix

arrayIsZeroVector

arrayIsZeroVector(vector)
vector is an array, not a matrix.
determines if a vector is the 0 vector

matrixFormMatrixFromEigValEigVec

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

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

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

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

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

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

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

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

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

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

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

matrixAbasisForB(matrix A, matrix B)
tests if the rows of A are a basis for the row space of B

matrixGetMinor

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.

matrixDet

det(matrix)
returns the determinant of a matrix

matrixRandomMatrix

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

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

matrixNumberOfRows(matrix)
 returns the number of rows of a matrix

matrixNumberOfColumns

matrixNumberOfColumns(matrix)
/returns the number of columns of a matrix

matrixParseStuans

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

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.