9.52 Matrix

The Matrix domain provides arithmetic operations on matrices and standard functions from linear algebra. This domain is similar to the TwoDimensionalArray domain, except that the entries for Matrix must belong to a Ring.

9.52.1 Creating Matrices

There are many ways to create a matrix from a collection of values or from existing matrices.

If the matrix has almost all items equal to the same value, use newnewMatrix to create a matrix filled with that value and then reset the entries that are different.

m : Matrix(Integer) := new(3,3,0)
\[\]
[000000000]

Type: Matrix Integer

To change the entry in the second row, third column to 5, use seteltseteltMatrix.

setelt(m,2,3,5)
\[\]
5

Type: PositiveInteger

An alternative syntax is to use assignment.

m(1,2) := 10
\[\]
10

Type: PositiveInteger

The matrix was destructively modified.

m
\[\]
[0100005000]

Type: Matrix Integer

If you already have the matrix entries as a list of lists, use matrixmatrixMatrix.

matrix [ [1,2,3,4],[0,9,8,7] ]
\[\]
[12340987]

Type: Matrix Integer

If the matrix is diagonal, use diagonalMatrixdiagonalMatrixMatrix.

dm := diagonalMatrix [1,x^2,x^3,x^4,x^5]
\[\]
[100000x200000x300000x400000x5]

Type: Matrix Polynomial Integer

Use setRowsetRowMatrix and setColumnsetColumnMatrix to change a row or column of a matrix.

setRow!(dm,5,vector [1,1,1,1,1])
\[\]
[100000x200000x300000x4011111]

Type: Matrix Polynomial Integer

setColumn!(dm,2,vector [y,y,y,y,y])
\[\]
[1y0000y0000yx3000y0x401y111]

Type: Matrix Polynomial Integer

Use copycopyMatrix to make a copy of a matrix.

cdm := copy(dm)
\[\]
[1y0000y0000yx3000y0x401y111]

Type: Matrix Polynomial Integer

This is useful if you intend to modify a matrix destructively but want a copy of the original.

setelt(dm,4,1,1-x^7)
\[\]
-x7+1

Type: Polynomial Integer

[dm,cdm]
\[\]
[[1y0000y0000yx300-x7+1y0x401y111],[1y0000y0000yx3000y0x401y111]]

Type: List Matrix Polynomial Integer

Use subMatrixsubMatrixMatrix to extract part of an existing matrix. The syntax is subMatrix(m, firstrow, lastrow, firstcol, lastcol).

subMatrix(dm,2,3,2,4)
\[\]
[y00yx30]

Type: Matrix Polynomial Integer

To change a submatrix, use setsubMatrixsetsubMatrixMatrix.

d := diagonalMatrix [1.2,-1.3,1.4,-1.5]
\[\]
[1.20.00.00.00.0-1.30.00.00.00.01.40.00.00.00.0-1.5]

Type: Matrix Float

If e is too big to fit where you specify, an error message is displayed. Use subMatrixsubMatrixMatrix to extract part of e, if necessary.

e := matrix [ [6.7,9.11],[-31.33,67.19] ]
\[\]
[6.79.11-31.3367.19]

Type: Matrix Float

This changes the submatrix of d whose upper left corner is at the first row and second column and whose size is that of e.

setsubMatrix!(d,1,2,e)
\[\]
[1.26.79.110.00.0-31.3367.190.00.00.01.40.00.00.00.0-1.5]

Type: Matrix Float

d
\[\]
[1.26.79.110.00.0-31.3367.190.00.00.01.40.00.00.00.0-1.5]

Type: Matrix Float

Matrices can be joined either horizontally or vertically to make new matrices.

a := matrix [ [1/2,1/3,1/4],[1/5,1/6,1/7] ]
\[\]
[121314151617]

Type: Matrix Fraction Integer

b := matrix [ [3/5,3/7,3/11],[3/13,3/17,3/19] ]
\[\]
[3537311313317319]

Type: Matrix Fraction Integer

Use horizConcathorizConcatMatrix to append them side to side. The two matrices must have the same number of rows.

horizConcat(a,b)
\[\]
[1213143537311151617313317319]

Type: Matrix Fraction Integer

Use vertConcatvertConcatMatrix to stack one upon the other. The two matrices must have the same number of columns.

vab := vertConcat(a,b)
\[\]
[1213141516173537311313317319]

Type: Matrix Fraction Integer

The operation transposetransposeMatrix is used to create a new matrix by reflection across the main diagonal.

transpose vab
\[\]
[1215353131316373171417311319]

Type: Matrix Fraction Integer

9.52.2 Operations on Matrices

FriCAS provides both left and right scalar multiplication.

m := matrix [ [1,2],[3,4] ]
\[\]
[1234]

Type: Matrix Integer

4 * m * (-5)
\[\]
[-20-40-60-80]

Type: Matrix Integer

You can add, subtract, and multiply matrices provided, of course, that the matrices have compatible dimensions. If not, an error message is displayed.

n := matrix([ [1,0,-2],[-3,5,1] ])
\[\]
[10-2-351]

Type: Matrix Integer

This following product is defined but n * m is not.

m * n
\[\]
[-5100-920-2]

Type: Matrix Integer

The operations nrowsnrowsMatrix and ncolsncolsMatrix return the number of rows and columns of a matrix. You can extract a row or a column of a matrix using the operations rowrowMatrix and columncolumnMatrix. The object returned is a Vector.

Here is the third column of the matrix n.

vec := column(n,3)
\[\]
[-2,1]

Type: Vector Integer

You can multiply a matrix on the left by a row vector and on the right by a column vector.

vec * m
\[\]
[1,0]

Type: Vector Integer

Of course, the dimensions of the vector and the matrix must be compatible or an error message is returned.

m * vec
\[\]
[0,-2]

Type: Vector Integer

The operation inverseinverseMatrix computes the inverse of a matrix if the matrix is invertible, and returns “failed” if not.

This Hilbert matrix is invertible.

hilb := matrix([ [1/(i + j) for i in 1..3] for j in 1..3])
\[\]
[121314131415141516]

Type: Matrix Fraction Integer

inverse(hilb)
\[\]
[72-240180-240900-720180-720600]

Type: Union(Matrix Fraction Integer,...)

This matrix is not invertible.

mm := matrix([ [1,2,3,4], [5,6,7,8], [9,10,11,12], [13,14,15,16] ])
\[\]
[12345678910111213141516]

Type: Matrix Integer

inverse(mm)
\[\]
“failed”

Type: Union(“failed”,...)

The operation determinantdeterminantMatrix computes the determinant of a matrix provided that the entries of the matrix belong to a CommutativeRing.

The above matrix mm is not invertible and, hence, must have determinant 0.

determinant(mm)
\[\]
0

Type: NonNegativeInteger

The operation tracetraceSquareMatrix computes the trace of a square matrix.

trace(mm)
\[\]
34

Type: PositiveInteger

The operation rankrankMatrix computes the rank of a matrix: the maximal number of linearly independent rows or columns.

rank(mm)
\[\]
2

Type: PositiveInteger

The operation nullitynullityMatrix computes the nullity of a matrix: the dimension of its null space.

nullity(mm)
\[\]
2

Type: PositiveInteger

The operation nullSpacenullSpaceMatrix returns a list containing a basis for the null space of a matrix. Note that the nullity is the number of elements in a basis for the null space.

nullSpace(mm)
\[\]
[[1,-2,1,0],[2,-3,0,1]]

Type: List Vector Integer

The operation rowEchelonrowEchelonMatrix returns the row echelon form of a matrix. It is easy to see that the rank of this matrix is two and that its nullity is also two.

rowEchelon(mm)
\[\]
[12340481200000000]

Type: Matrix Integer

For more information on related topics, see ugIntroTwoDimPage in Section ugIntroTwoDimNumber , ugProblemEigenPage in Section ugProblemEigenNumber , ugxFloatHilbertPage in Section ugxFloatHilbertNumber , PermanentXmpPage , VectorXmpPage , OneDimensionalArrayXmpPage , and TwoDimensionalArrayXmpPage .