On this page:
3.29.1 The Vector Datatype
Vector
Vector3D
Nat
Non  Zero  Nat
vector
vector3d
3.29.2 Vector Methods
.get
.length
.dot
.magnitude
.cross
.normalize
.scale
.to-row-matrix
.to-col-matrix
3.29.3 Vector Functions
vec-get
vec-length
vec-dot
vec-magnitude
vec-cross
vec-normalize
vec-scale
vec-add
vec-sub
3.29.4 The Matrix Datatype
Matrix
is-matrix
3.29.5 Matrix Constructors
matrix
row-matrix
col-matrix
identity-matrix
make-matrix
zero-matrix
build-matrix
3.29.6 Matrix Methods
.get
.to-list
.to-vector
.to-lists
.to-vectors
.row
.col
.submatrix
.transpose
.hermitian
.diagonal
.upper-triangle
.lower-triangle
.row-list
.col-list
.map
.map2
.row-map
.col-map
.augment
.stack
.trace
.scale
.dot
.expt
.determinant
.is-invertible
.is-orthonormal
.rref
.inverse
.solve
.least-squares-solve
.lu-decomposition
.lp-norm
.l1-norm
.l2-norm
.l-inf-norm
.qr-decomposition
.gram-schmidt
3.29.7 Matrix Binary Operations
3.29.8 Matrix Functions
mtx-get
mtx-to-list
mtx-to-vector
mtx-to-lists
mtx-to-vectors
mtx-row
mtx-col
mtx-submatrix
mtx-transpose
mtx-hermitian
mtx-diagonal
mtx-upper-triangle
mtx-lower-triangle
mtx-row-list
mtx-col-list
mtx-map
mtx-map2
mtx-row-map
mtx-col-map
mtx-augment
mtx-stack
mtx-trace
mtx-scale
mtx-dot
mtx-expt
mtx-determinant
mtx-is-invertible
mtx-is-orthonormal
mtx-rref
mtx-inverse
mtx-solve
mtx-least-squares-solve
mtx-lp-norm
mtx-l1-norm
mtx-l2-norm
mtx-l-inf-norm
mtx-qr-decomposition
mtx-gram-schmidt
mtx-add
mtx-sub
mtx-mult
3.29.9 Matrix Conversion Functions
is-row-matrix
is-col-matrix
is-square-matrix
vector-to-matrix
list-to-matrix
list-to-row-matrix
list-to-col-matrix
lists-to-matrix
vectors-to-matrix
matrix-within

3.29 matrices🔗

Usage:
include matrices
import matrices as ...
Matrices are rectangular grids of numbers, which define many useful mathematical operations. Matrices can manipulate each other, and are also used to manipulate vectors, which are lists of numbers that likewise define many useful mathematical operations.

This library defines both the Vector datatype and the Matrix datatype. All functionality in this library is defined both as methods on the data values and as analogous functions.

3.29.1 The Vector Datatype🔗

The Vector type represents mathematical vectors.

Like Vector, but only allows 3-dimensional vectors.

The type of natural numbers, i.e. non-negative integers.
The type of positive integers.

[vector: elt :: Number, ...] -> Vector

Vector constructor which creates a vector instance with the given elements.

[vector3d: elt1 :: Number, elt2 :: Number, elt3 :: Number] -> Vector

Vector constructor which only creates three-dimensional vector instances.

Vectors are defined to permit using addition and subtraction operators on them, whenever the lengths of the vectors are the same:

Examples:

check: [vector: 1, 2, 3] + [vector: 4, 5, 6] is [vector: 5, 7, 9] [vector: 1] + [vector: 1, 2] raises "vectors of different lengths" [vector: 1, 2, 3] - [vector: 4, 5, 6] is [vector: -3, -3, -3] [vector: 1] - [vector: 1, 2] raises "vectors of different lengths" end

See also vec-add and vec-sub.

Two vectors are considered equal when their lengths are the same and their corresponding elements are equal, and obeys the same restrictions on comparing exact and rough numbers for equality:

Examples:

check: ([vector: 1] == [vector: 1, 2]) is false ([vector: 1, 2] == [vector: 1, 2]) is true ([vector: ~1, ~2] == [vector: 1, 2]) raises "not allowed" roughly-equal([vector: ~1, ~2], [vector: 1, 2]) is true end

3.29.2 Vector Methods🔗

.get :: (index :: Nat) -> Number

Returns the item at the given index in this vector.

Examples:

check: [vector: 3, 5].get(1) is 5 end

.length :: () -> Number

Returns the length of this vector.

Examples:

check: [vector: 1, 2, 3, 4].length() is 4 end

.dot :: (other :: Vector) -> Number

Returns the dot product of this vector with the given vector.

Examples:

check: [vector: 1, 2, 3].dot([vector: 3, 2, 1]) is 10 end

.magnitude :: () -> Number

Returns the magnitude of this vector.

Examples:

check: [vector: 3, 4].magnitude() is 5 [vector: 4, 0].magnitude() is 4 end

.cross :: (other :: Vector3D) -> Vector3D

Returns the cross product of this 3D vector and the given 3D vector. (Raises an error if either this or that vector are not 3-dimensional)

Examples:

check: [vector: 2, -3, 1].cross([vector: -2, 1, 1]) is [vector: -4, -4, -4] end

.normalize :: () -> Vector

Normalizes this vector into a unit vector.

Examples:

check: [vector: 1, 2, 3].normalize() is [vector: (1 / num-sqrt(14)), (2 / num-sqrt(14)), (3 / num-sqrt(14))] end

.scale :: (scalar :: Number) -> Vector

Scales this vector by the given constant.

Examples:

check: [vector: 1, 2, 3].scale(2) is [vector: 2, 4, 6] end

.to-row-matrix :: () -> Matrix

Converts this vector to a one-row matrix.

Examples:

check: [vector: 4, 5, 6].to-row-matrix() is [matrix(1, 3): 4, 5, 6] end

.to-col-matrix :: () -> Matrix

Converts this vector to a one-column matrix.

Examples:

check: [vector: 4, 5, 6].to-row-matrix() is [matrix(3, 1): 4, 5, 6] end

3.29.3 Vector Functions🔗

vec-get :: (v :: Vector, index :: Nat) -> Number

Returns the item at the given index in the given vector.

Examples:

check: vec-get([vector: 3, 5], 1) is 5 end

See .get.

vec-length :: (v :: Vector) -> Number

Returns the length of the given vector.

Examples:

check: vec-length([vector: 1, 2, 3, 4]) is 4 end

See .length.

vec-dot :: (v1 :: Vector, v2 :: Vector) -> Number

Returns the dot product of the first vector with the second vector.

Examples:

check: vec-dot[vector: 1, 2, 3], ([vector: 3, 2, 1]) is 10 end

See .dot.

vec-magnitude :: (vec :: Vector) -> Number

Returns the magnitude of the given vector.

Examples:

check: vec-magnitude([vector: 3, 4]) is 5 vec-magnitude([vector: 4, 0]) is 4 end

See .magnitude.

vec-cross :: (v1 :: Vector3D, v2 :: Vector3D) -> Vector3D

Returns the cross product of the two given 3D vectors. (Raises an error if either vector is not 3-dimensional)

Examples:

check: vec-cross([vector: 2, -3, 1], [vector: -2, 1, 1]) is [vector: -4, -4, -4] end

See .cross.

vec-normalize :: (vec :: Vector) -> Vector

Normalizes the given vector into a unit vector.

Examples:

check: vec-normalize([vector: 1, 2, 3]) is [vector: (1 / num-sqrt(14)), (2 / num-sqrt(14)), (3 / num-sqrt(14))] end

See .normalize.

vec-scale :: (vec :: Vector, factor :: Number) -> Vector

Scales the given vector by the given constant.

Examples:

check: vec-scale([vector: 1, 2, 3], 2) is [vector: 2, 4, 6] end

See .scale.

vec-add :: (v1 :: Vector, v2 :: Vector) -> Vector

Adds the second vector to first one.

Examples:

check: vec-add([vector: 1, 2, 3], [vector: 4, 5, 6]) is [vector: 5, 7, 9] vec-add([vector: 1], [vector: 1, 2]) raises "vectors of different lengths" end

vec-sub :: (v1 :: Vector, v2 :: Vector) -> Vector

Subtracts the second vector from first one.

Examples:

check: vec-sub([vector: 1, 2, 3], [vector: 4, 5, 6]) is [vector: -3, -3, -3] vec-sub([vector: 1], [vector: 1, 2]) raises "vectors of different lengths" end

3.29.4 The Matrix Datatype🔗

The Matrix type represents mathematical matrices.

is-matrix :: (val :: Any) -> Boolean

Every matrix has a rows field and a cols field, which are the dimensions of the matrix.

Examples:

check: [matrix(2, 3): 10, 20, 30, 40, 50, 60].rows is 2 [matrix(2, 3): 10, 20, 30, 40, 50, 60].cols is 3 end

3.29.5 Matrix Constructors🔗
[matrix(rows :: NonZeroNat, cols :: NonZeroNat): elt :: Number, ...] -> Matrix

Publicly exposed constructor which constructs a matrix of size rows by cols with the given elements, entered row by row.

The following example represents the matrix \(\left[\begin{smallmatrix}1 & 2 & 3 \\ 4 & 5 & 6\end{smallmatrix}\right]\):

Examples:

[matrix(2,3): 1, 2, 3, 4, 5, 6]

Supplying an inconsistent quantity of elements for a given matrix dimension will produce an error:

Examples:

check: [matrix(4, 2): 100] raises "Invalid 1x2 Matrix" end

[row-matrix: elt :: Number, ...] -> Matrix

Constructor which returns a one-row matrix containing the given entries.

The following will construct the matrix \(\left[\begin{smallmatrix}1 & 2 & 3\end{smallmatrix}\right]\):

Examples:

check: [row-matrix: 1, 2, 3] is [matrix(1,3): 1, 2, 3] end

[col-matrix: elt :: Number, ...] -> Matrix

Constructor which returns a one-column matrix containing the given entries.

The following will construct the matrix \(\left[\begin{smallmatrix}1 \\ 2 \\ 3\end{smallmatrix}\right]\):

Examples:

check: [col-matrix: 1, 2, 3] is [matrix(3,1): 1, 2, 3] end

Constructs an \(n \times n\) identity matrix.

Examples:

check: identity-matrix(2) is [matrix(2,2): 1, 0, 0, 1] identity-matrix(3) is [matrix(3,3): 1, 0, 0, 0, 1, 0, 0, 0, 1] end

make-matrix :: (
rows :: NonZeroNat,
cols :: NonZeroNat,
elt :: Number
)
-> Matrix

Constructs a matrix of the given size using only the given element.

Examples:

check: make-matrix(2, 3, 1) is [matrix(2,3): 1, 1, 1, 1, 1, 1] make-matrix(3, 2, 5) is [matrix(3,2): 5, 5, 5, 5, 5, 5] end

zero-matrix :: (
rows :: NonZeroNat,
cols :: NonZeroNat,
elt :: Matrix
)
-> Matrix

Constructs a matrix of the given size containing only zeroes.

Examples:

check: zero-matrix(2, 3) is [matrix(2,3): 0, 0, 0, 0, 0, 0] end

build-matrix :: (
rows :: NonZeroNat,
cols :: NonZeroNat,
proc :: (Number, Number -> Number)
)
-> Matrix

Constructs a matrix of the given size, where entry (i,j) is the result of proc(i,j).

Examples:

check: build-matrix(2, 3, lam(i,j): i + j end) is [matrix(3,2): 0, 1, 1, 2, 2, 3] end

3.29.6 Matrix Methods🔗

These methods are available on all matrices.

.get :: (i :: Nat, j :: Nat) -> Number

Returns the matrix’s entry in the ith row and the jth column.

Examples:

check: [matrix(3,2): 1, 2, 3, 4, 5, 6].get(1,1) is 4 [matrix(3,2): 1, 2, 3, 4, 5, 6].get(2,0) is 5 [matrix(1,1): 1].get(2, 0) raises "Index out of bounds for matrix dimensions" end

.to-list :: () -> List<Number>

Returns the matrix as a list of numbers in row-major order.

For example, given the matrix \(\left[\begin{smallmatrix}2 & 4 & 6 \\ 8 & 10 & 12 \\ 14 & 16 & 18\end{smallmatrix}\right]\):

Examples:

check: [matrix(3,3): 2, 4, 6, 8, 10, 12, 14, 16, 18].to-list() is [list: 2, 4, 6, 8, 10, 12, 14, 16, 18] end

.to-vector :: () -> Vector

Returns a one-row/one-column matrix as a vector.

Examples:

check: [matrix(2,1): 4, 5].to-vector() is [vector: 4, 5] [matrix(1,2): 4, 5].to-vector() is [matrix(2,1): 4, 5].to-vector() [matrix(2,2): 1, 2, 3, 4].to-vector() raises "Cannot convert non-vector matrix to vector" end

.to-lists :: () -> List<List<Number>>

Returns the matrix as a list of lists of numbers, with each list corresponding to one row.

Examples:

check: [matrix(2,3): 1, 2, 3, 4, 5, 6].to-lists() is [list: [list: 1, 2, 3], [list: 4, 5, 6]] end

.to-vectors :: () -> List<Vector>

Returns the matrix as a list of lists of numbers (i.e. a list of Vectors), with each list corresponding to one column.

For example, the matrix \(\left[\begin{smallmatrix}1 & 2 & 3 \\ 4 & 5 & 6\end{smallmatrix}\right]\) corresponds to the vectors \(\left[\begin{smallmatrix}1 \\ 4\end{smallmatrix}\right]\), \(\left[\begin{smallmatrix}2 \\ 5\end{smallmatrix}\right]\), and \(\left[\begin{smallmatrix}3 \\ 6\end{smallmatrix}\right]\):

Examples:

check: [matrix(2,3): 1, 2, 3, 4, 5, 6].to-vectors() is [list: [vector: 1, 4], [vector: 2, 5], [vector: 3, 6]] end

.row :: (i :: Nat) -> Matrix

Returns a one-row matrix with the matrix’s given row.

Examples:

check: [matrix(2,3): 1, 2, 3, 4, 5, 6].row(2) is [matrix(1,3): 4, 5, 6] [matrix(3,3): 1, 2, 3, 4, 5, 6, 7, 8, 9].row(3) is [matrix(1,3): 7, 8, 9] end

.col :: (j :: Nat) -> Matrix

Returns a one-column matrix with the matrix’s given column.

Examples:

check: [matrix(2,3): 1, 2, 3, 4, 5, 6].col(2) is [matrix(2,1): 2, 5] [matrix(3,3): 1, 2, 3, 4, 5, 6, 7, 8, 9].col(3) is [matrix(3,1): 3, 6, 9] end

.submatrix :: (loi :: List<Nat>, loj :: List<Nat>) -> Matrix

Returns the submatrix of the matrix comprised of the intersection of the given list of rows and the given list of columns.

For example, if our list of rows is \(\{1, 2\}\) and our list of columns is \(\{2, 3\}\), then the positions in the resulting submatrix will be the elements with \((row,col)\) positions \(\{(1, 2), (1, 3), (2, 2), (2, 3)\}\).

\(\left[\begin{matrix} a_{11} & a_{12} & a_{13} \\ a_{21} & a_{22} & a_{23} \\ a_{31} & a_{32} & a_{33} \end{matrix}\right]\).submatrix([list: 1, 2], [list: 2, 3]) \(= \left[\begin{matrix} a_{12} & a_{13} \\ a_{22} & a_{23}\end{matrix}\right]\)

This is shown in the below example:

Examples:

check: [matrix(3,3): 1, 2, 3, 4, 5, 6, 7, 8, 9].submatrix([list: 1, 2], [list: 2, 3]) is [matrix(2,2): 2, 3, 4, 5] end

.transpose :: () -> Matrix

Returns the transposition of the matrix. For example, \[\begin{bmatrix}1 & 2 & 3 \\ 4 & 5 & 6\end{bmatrix} \overrightarrow{Transpose} \begin{bmatrix}1 & 4 \\ 2 & 5 \\ 3 & 6\end{bmatrix}\]

Examples:

check: [matrix(2,3): 1, 2, 3, 4, 5, 6].transpose() is [matrix(3,2): 1, 4, 2, 5, 3, 6] end

.hermitian :: () -> Matrix

Computes the conjugate-transpose of this matrix. Since Pyret does not have complex numbers, this is synonymous with .transpose.

.diagonal :: () -> Matrix

Returns a one-row matrix containing the matrix’s diagonal entries.

Examples:

check: [matrix(3,3): 1, 2, 3, 4, 5, 6, 7, 8, 9].diagonal() is [matrix(1,3): 1, 5, 9] [matrix(3,2): 1, 2, 3, 4, 5, 6].diagonal() is [matrix(1,2): 1, 5] end

.upper-triangle :: () -> Matrix

Returns the upper triangle of the matrix, if the matrix is square. This consists of all the values on or above the main diagonal, and zeroes below it. For example, the upper triangle of \(\left[\begin{smallmatrix}1 & 2 & 3\\ 4 & 5 & 6\\ 7 & 8 & 9\end{smallmatrix}\right]\) would be \(\left[\begin{smallmatrix}1 & 2 & 3\\ 0 & 5 & 6 \\ 0 & 0 & 9\end{smallmatrix}\right]\).

Examples:

check: [matrix(2,2): 1, 2, 3, 4].upper-triangle() is [matrix(2,2): 1, 2, 0, 4] [matrix(3,3): 1, 2, 3, 4, 5, 6, 7, 8, 9].upper-triangle() is [matrix(3,3): 1, 2, 3, 0, 5, 6, 0, 0, 9] end

.lower-triangle :: () -> Matrix

Returns the lower triangle of the matrix, if the matrix is square. This consists of all the values on or below the main diagonal, and zeroes above it. For example, the upper triangle of \(\left[\begin{smallmatrix}1 & 2 & 3\\ 4 & 5 & 6\\ 7 & 8 & 9\end{smallmatrix}\right]\) would be \(\left[\begin{smallmatrix}1 & 0 & 0\\ 4 & 5 & 0\\ 7 & 8 & 9\end{smallmatrix}\right]\).

Examples:

check: [matrix(2,2): 1, 2, 3, 4].lower-triangle() is [matrix(2,2): 1, 0, 3, 4] [matrix(3,3): 1, 2, 3, 4, 5, 6, 7, 8, 9].lower-triangle() is [matrix(3,3): 1, 0, 0, 4, 5, 0, 7, 8, 9] end

.row-list :: () -> List<Matrix>

Returns the matrix as a list of one-row matrices. (Very similar to .to-lists, except this method returns a list of matrices instead.)

Examples:

check: [matrix(2,3): 1, 2, 3, 4, 5, 6].row-list() is [list: [matrix(1,3): 1, 2, 3], [matrix(1,3): 4, 5, 6]] end

.col-list :: () -> List<Matrix>

Returns the matrix as a list of one-column matrices. (Very similar to .to-vectors, except this method returns a list of matrices instead.)

Examples:

check: [matrix(2,3): 1, 2, 3, 4, 5, 6].col-list() is [list: [matrix(2,1): 1, 4], [matrix(2,1): 2, 5], [matrix(2,1): 3, 6]] end

.map :: (func :: (Number -> Number)) -> Matrix

Maps the given function entrywise over the matrix.

Examples:

check: multTwo = lam(x): x * 2 end [matrix(2,2): 1, 2, 3, 4].map(multTwo) is [matrix(2,2): 2, 4, 6, 8] end

.map2 :: (other :: Matrix, func :: (Number, Number -> Number)) -> Matrix

Maps the given function entrywise over corresponding elements of this and the given matrix.

Examples:

check: m1 = [matrix(2,2): 10, 20, 30, 40] m2 = [matrix(2,2): 4, 3, 2, 1] m1.map2(m2, num-expt) is [matrix(2,2): num-expt(10, 4), num-expt(20, 3), num-expt(30, 2), num-expt(40, 1)] end

.row-map :: (func :: (Matrix -> Matrix)) -> Matrix

Maps the given function over each row in the matrix.

Examples:

check: # sumRow :: 1*n matrix # Computes the total sum of all entries in the given row sumRow = lam(row): [matrix(1,1): row.to-vector().foldr(_ + _)] end [matrix(2,3): 1, 2, 3, 4, 5, 6].row-map(sumRow) is [matrix(2,1): 6, 15] end

.col-map :: (func :: (Matrix -> Matrix)) -> Matrix

Maps the given function over each column in the matrix.

Examples:

check: # sumCol :: m*1 matrix # Computes the total sum of all entries in the given column sumCol = lam(col): [matrix(1,1): col.to-vector().foldr(_ + _)] end [matrix(2,3): 1, 2, 3, 4, 5, 6].col-map(sumCol) is [matrix(1,3): 5, 7, 9] end

.augment :: (other :: Matrix) -> Matrix

Returns the matrix augmented with the given matrix. For example, augmenting the matrix \(\left[\begin{smallmatrix}1 & 2\\4 & 5\end{smallmatrix}\right]\) with the matrix \(\left[\begin{smallmatrix}3\\ 6\end{smallmatrix}\right]\) yields the matrix \(\left[\begin{smallmatrix}1 & 2 & 3\\ 4 & 5 & 6\end{smallmatrix}\right]\).

Examples:

check: [matrix(2,2): 1, 2, 4, 5].augment([matrix(2,1): 3, 6]) is [matrix(2,3): 1, 2, 3, 4, 5, 6] end

.stack :: (other :: Matrix) -> Matrix

Returns the matrix stacked on top of the given matrix. For example, stacking the matrix \(\left[\begin{smallmatrix}1 & 2 & 3\end{smallmatrix}\right]\) on top of the matrix \(\left[\begin{smallmatrix}4 & 5 & 6\end{smallmatrix}\right]\) gives the matrix \(\left[\begin{smallmatrix}1 & 2 & 3\\ 4 & 5 & 6\end{smallmatrix}\right]\).

Examples:

check: [matrix(1,3): 1, 2, 3].stack([matrix(1,3): 4, 5, 6]) is [matrix(2,3): 1, 2, 3, 4, 5, 6] end

.trace :: () -> Number

Returns the trace of the matrix (i.e. the sum of its diagonal values).

Examples:

check: [matrix(3,3): 1, 2, 3, 4, 5, 6, 7, 8, 9].trace() is (1 + 5 + 9) [matrix(2,2): 2, 4, 6, 8].trace() is (2 + 8) end

.scale :: (factor :: Number) -> Matrix

Multiplies each entry in the matrix by the given value.

Examples:

check: [matrix(2,2): 1, 2, 3, 4].scale(2) is [matrix(2,2): 2, 4, 6, 8] [matrix(2,2): 2, 4, 6, 8].scale(1/2) is [matrix(2,2): 1, 2, 3, 4] end

.dot :: (other :: Matrix) -> Number

Returns the Frobenius Product of the matrix with the given matrix (for 1-dimensional matrices, this is simply the dot product). This is done by multiplying the matrix with the transposition of other and taking the trace of the result. An example of this calculation (\(\ast\) denotes matrix multiplication):

\(\left(\left[\begin{smallmatrix}1 & 2 & 3\end{smallmatrix}\right] \ast\left[\begin{smallmatrix}4\\ 2\\ ^4/_3 \end{smallmatrix}\right]\right)\).trace() \(= \underbrace{\left[\begin{smallmatrix}(1\cdot 4)+(2\cdot 2)+(3\cdot \frac{4}{3})\end{smallmatrix}\right]}_{ 1\times 1 \text{ matrix}}\).trace()\(=12\)

Examples:

check: [matrix(1,3): 1, 2, 3].dot([matrix(1,3): 4, 2, 4/3]) is 12 [matrix(1,3): 1, 2, 3].dot([matrix(1,3): 1, 1, 1]) is 6 end

.expt :: (power :: Number) -> Matrix

Multiplies the matrix by itself the given number of times.

Examples:

check: a = [matrix(2,2): 1, 2, 3, 4] a.expt(1) is a a.expt(2) is a * a a.expt(3) is a * a * a end

.determinant :: () -> Number

Returns the determinant of the matrix, calculated via a recursive implementation of Laplace expansion.

Examples:

check: [matrix(5,5): 1, 2, 1, 2, 3, 2, 3, 1, 0, 1, 2, 2, 1, 0, 0, 1, 1, 1, 1, 1, 0,-2, 0,-2,-2].determinant() is -2 end

.is-invertible :: () -> Boolean

Returns true if the matrix is invertible, that is, it has a nonzero determinant.

.is-orthonormal :: () -> Boolean

Returns true if the matrix is orthonormal, meaning that all rows (when treated as vectors) each have .magnitude 1, are all distinct, and distinct rows .dot of zero. Mathematically, this computes whether \(self * self^T\) is the identity matrix. Since numerical inaccuracy is quite likely, this check is performed using roughly-equal.

.rref :: () -> Matrix

Returns the Reduced Row Echelon Form of the matrix. For example: \[\begin{bmatrix}1 & 2 & 3 \\ 4 & 5 & 6\end{bmatrix} \overrightarrow{RREF} \begin{bmatrix}1 & 0 & -1\\ 0 & 1 & 2\end{bmatrix}\]

Examples:

check: [matrix(2,3): 1, 2, 3, 4, 5, 6].rref() is [matrix(2,3): 1, 0,-1, 0, 1, 2] end

.inverse :: () -> Matrix

Returns the inverse of the matrix, if it is invertible (found by augmenting the matrix with itself and finding the reduced-row echelon form). For example: \[\begin{bmatrix}1 & 0 & 4\\ 1 & 1 & 6\\ -3 & 0 & -10\end{bmatrix}^{-1} = \begin{bmatrix}-5 & 0 & -2\\ -4 & 1 & -1\\ ^3/_2 & 0 & ^1/_2\end{bmatrix}\]

Examples:

check: [matrix(3,3): 1, 0, 4, 1, 1, 6, -3, 0, -10].inverse() is [matrix(3,3): -5, 0, -2, -4, 1, -1, 3/2, 0, 1/2] end

.solve :: (other :: Matrix) -> Matrix

Returns the matrix which, when multiplied on the right of this matrix, results in the given matrix. In other words, this returns the solution to the system of equations represented by this and the given matrix. This method only works on invertible matrices (Calculated by inverting itself and multiplying the given matrix on the right side of this inverse).

.least-squares-solve :: (other :: Matrix) -> Matrix

Returns the least squares solution for this and the given matrix, calculated using QR decomposition.

.lu-decomposition :: () -> {L :: Matrix, U :: Matrix}

Computes the LU decomposition of this matrix, if possible. This returns a pair of matrices, L and U, that are respectively lower-triangular and upper-triangular, and whose product is this matrix:

\[\begin{bmatrix} a_{11} & a_{12} & a_{13} \\ a_{21} & a_{22} & a_{23} \\ a_{31} & a_{32} & a_{33} \end{bmatrix} = \begin{bmatrix} \ell_{11} & 0 & 0 \\ \ell_{21} & \ell_{22} & 0 \\ \ell_{31} & \ell_{32} & \ell_{33} \end{bmatrix} \begin{bmatrix} u_{11} & u_{12} & u_{13} \\ 0 & u_{22} & u_{23} \\ 0 & 0 & u_{33} \end{bmatrix}\]

.lp-norm :: (power :: Number) -> Number

Computes the Lp norm of the matrix using the given number.

.l1-norm :: () -> Number

.l2-norm :: () -> Number

.l-inf-norm :: () -> Number

Computes the L1, L2, and L norms of the matrix, respectively.

Examples:

check: a = [matrix(3,1): 1, 2, 3] b = [matrix(3,3): 1, 0, 0, 2, 0, 0, 3, 0, 0] a.lp-norm(3) is-roughly num-expt(35, 1/3) b.lp-norm(3) is-roughly (b * a).lp-norm(3) a.l1-norm() is-roughly 6 a.l2-norm() is-roughly num-sqrt(14) a.l-inf-norm() is 3 end

.qr-decomposition :: () -> {Q :: Matrix, R :: Matrix}

Returns the QR decomposition of this matrix, if possible. This returns a pair of matrices, Q and R, where Q is orthogonal and R is upper-triangular, whose product is this matrix.

.gram-schmidt :: () -> Matrix

Returns an orthogonal matrix whose image is the same as the span of the matrix’s columns. (The same as the first result of .qr-decomposition)

3.29.7 Matrix Binary Operations🔗

Matrices are defined to permit using addition, subtraction, and multiplication operators on them, whenever the dimensions are compatible:

Examples:

check: [matrix(2,2): 1, 2, 3, 4] + [matrix(2,2): 1, 2, 3, 4] is [matrix(2,2): 2, 4, 6, 8] [matrix(2,2): 1, 2, 3, 4] + [matrix(4, 1): 1, 2, 3, 4] raises "different sized matrices" end

Examples:

check: [matrix(2,2): 1, 2, 3, 4] - [matrix(2,2): 0, 2, 3, 3] is [matrix(2,2): 1, 0, 0, 1] [matrix(2,2): 1, 2, 3, 4] - [matrix(4, 1): 1, 2, 3, 4] raises "different sized matrices" end

Examples:

check: [matrix(2,2): 1, 2, 3, 4] * [matrix(2,2): 3, 0, 0, 3] is [matrix(2,2): 3, 6, 9, 12] end

3.29.8 Matrix Functions🔗

The following functions are available to be performed on matrices.

mtx-get :: (
m :: Matrix,
i :: Nat,
j :: Nat
)
-> Number

Returns the matrix’s entry in the ith row and the jth column. See .get.

Returns the matrix as a list of numbers in row-major order. See .to-list.

Returns a one-row/one-column matrix as a vector. See .to-vector.

Returns the matrix as a list of lists of numbers, with each list corresponding to one row. See .to-lists.

Returns the matrix as a list of lists of numbers (i.e. a list of Vectors), with each list corresponding to one column. See .to-vectors.

mtx-row :: (m :: Matrix, i :: Nat) -> Matrix

Returns a one-row matrix with the matrix’s given row. See .row.

mtx-col :: (m :: Matrix, j :: Nat) -> Matrix

Returns a one-column matrix with the matrix’s given column. See .col.

mtx-submatrix :: (
m :: Matrix,
loi :: List<Nat>,
loj :: List<Nat>
)
-> Matrix

Returns the submatrix of the matrix comprised of the intersection of the given list of rows and the given list of columns. See .submatrix.

See .transpose.

See .hermitian.

Returns a one-row matrix containing the matrix’s diagonal entries. See .diagonal.

Returns the upper triangle of the matrix, if the matrix is square. See .upper-triangle.

Returns the lower triangle of the matrix, if the matrix is square. See .lower-triangle.

Returns the matrix as a list of one-row matrices. See .row-list.

Returns the matrix as a list of one-column matrices. See .col-list.

mtx-map :: (func :: (Number -> Number), m :: Matrix) -> Matrix

Maps the given function entrywise over the matrix. See .map.

mtx-map2 :: (
func :: (Number, Number -> Number),
m :: Matrix,
n :: Matrix
)
-> Matrix

Maps the given function over the corresponding entries of the two given matrices. See .map2.

mtx-row-map :: (func :: (Matrix -> Matrix), m :: Matrix) -> Matrix

Maps the given function over each row in the matrix. See .row-map.

mtx-col-map :: (func :: (Matrix -> Matrix), m :: Matrix) -> Matrix

Maps the given function over each column in the matrix. See .col-map.

mtx-augment :: (m1 :: Matrix, m2 :: Matrix) -> Matrix

Returns the first matrix augmented with the second matrix. See .augment.

mtx-stack :: (m1 :: Matrix, m2 :: Matrix) -> Matrix

Returns the first matrix stacked on top of the second matrix. See .stack.

mtx-trace :: (m :: Matrix) -> Number

Returns the trace of the matrix (i.e. the sum of its diagonal values). See .trace.

mtx-scale :: (m :: Matrix, factor :: Number) -> Matrix

Multiplies each entry in the matrix by the given value. See .scale.

mtx-dot :: (m1 :: Matrix, m2 :: Matrix) -> Number

Returns the Frobenius Product of the two matrices. See .dot.

mtx-expt :: (m :: Matrix, power :: Nat) -> Matrix

Multiplies the matrix by itself the given number of times. See .expt.

Returns the determinant of the matrix. See .determinant.

Returns true if the matrix is invertible. See .is-invertible.

Returns true if the matrix is orthonormal. See .is-orthonormal.

mtx-rref :: (m :: Matrix) -> Matrix

Returns the Reduced Row Echelon Form of the matrix. See .rref.

Returns the inverse of the matrix, if it is invertible. See .inverse.

mtx-solve :: (m1 :: Matrix, m2 :: Matrix) -> Matrix

Returns the matrix which, when multiplied on the right of the first matrix, results in the second matrix. See .solve.

Returns the least squares solution for the first and the second matrix, calculated using QR decomposition. See .least-squares-solve.

mtx-lp-norm :: (m :: Matrix, power :: Number) -> Number

Computes the Lp norm of the matrix using the given number. See .lp-norm.

Computes the L1, L2, and L norms of the matrix, respectively. See .l1-norm.

mtx-qr-decomposition :: (m :: Matrix) -> {Q :: Matrix, R :: Matrix}

See .qr-decomposition.

See .gram-schmidt.

mtx-add :: (m1 :: Matrix, m2 :: Matrix) -> Matrix

mtx-sub :: (m1 :: Matrix, m2 :: Matrix) -> Matrix

mtx-mult :: (m1 :: Matrix, m2 :: Matrix) -> Matrix

Adds, subtracts, or multiplies the two matrices. See Matrix Binary Operations.

3.29.9 Matrix Conversion Functions🔗

Returns whether the matrix has exactly one row:

Examples:

check: is-row-matrix([matrix(1, 3): 10, 20, 10]) is true is-row-matrix([matrix(3, 1): 10, 20, 10]) is false end

Returns whether the matrix has exactly one column:

Examples:

check: is-row-matrix([matrix(1, 3): 10, 20, 10]) is false is-row-matrix([matrix(3, 1): 10, 20, 10]) is true end

Returns true if the given matrix has the same number of rows and columns.

Examples:

check: is-square-matrix([matrix(2, 2): 10, 20, 30, 40]) is true is-square-matrix([matrix(4, 1): 10, 20, 30, 40]) is false end

Converts the given vector into a one-row matrix.

Examples:

check: vector-to-matrix([vector: 1, 2, 3]) is [matrix(1,3): 1, 2, 3] end

list-to-matrix :: (
rows :: NonZeroNat,
cols :: NonZeroNat,
lst :: List<Number>
)
-> Matrix

Converts the given list of numbers into a matrix of the given size.

Examples:

check: list-to-matrix(2, 2, [list: 1, 2, 3, 4]) is [matrix(2,2): 1, 2, 3, 4] list-to-matrix(2, 3, [list: 1, 2, 3, 4, 5, 6]) is [matrix(2,3): 1, 2, 3, 4, 5, 6] end

Converts the given list of numbers into a one-row matrix.

Examples:

check: list-to-row-matrix([list: 1, 2, 3, 4]) is [matrix(1,4): 1, 2, 3, 4] end

Converts the given list of numbers into a one-column matrix.

Examples:

check: list-to-col-matrix([list: 1, 2, 3, 4]) is [matrix(4,1): 1, 2, 3, 4] end

Converts the given list of lists into a matrix, with each list as a row.

Examples:

check: lists-to-matrix([list: [list: 1, 2, 3, 4]]) is [matrix(1,4): 1, 2, 3, 4] lists-to-matrix([list: [list: 1, 2, 3], [list: 4, 5, 6]]) is [matrix(2,3): 1, 2, 3, 4, 5, 6] end

Converts the given list of vectors into a matrix, with each vector as a column.

Examples:

check: vectors-to-matrix([list: [vector: 1, 2, 3]]) is [matrix(3,1): 1, 2, 3] vectors-to-matrix([list: [vector: 1, 3, 5], [vector: 2, 4, 6]]) is [matrix(3,2): 1, 2, 3, 4, 5, 6] end

matrix-within :: (delta :: Number) -> (Matrix, Matrix -> Boolean)

Returns a comparison predicate which returns true if each entry in both matrices is within delta of each other.