3.1.22 matrices
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. In parallel with the List and Table libraries, all indices in this library are zero-based.
3.1.22.1 The Vector Datatype
Vector constructor which creates a vector instance with the given elements.
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:
include matrices 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
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:
include matrices check: ([vector: 1] == [vector: 1, 2]) is false ([vector: 1, 2] == [vector: 1, 2]) is true ([vector: ~1, ~2] == [vector: 1, 2]) raises "Roughnums" roughly-equal([vector: ~1, ~2], [vector: 1, 2]) is true end
3.1.22.2 Vector Methods
Returns the item at the given (0-based) index in this vector.
include matrices check: [vector: 3, 5].get(1) is 5 end
Returns the length of this vector.
include matrices check: [vector: 1, 2, 3, 4].length() is 4 end
Returns the dot product of this vector with the given vector.
include matrices check: [vector: 1, 2, 3].dot([vector: 3, 2, 1]) is 10 end
Returns the magnitude of this vector.
include matrices check: [vector: 3, 4].magnitude() is 5 [vector: 4, 0].magnitude() is 4 end
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)
include matrices check: [vector: 2, -3, 1].cross([vector: -2, 1, 1]) is [vector: -4, -4, -4] [vector: 2, -3, 1, 4].cross([vector: -2, 1]) raises "3-d vectors" end
Normalizes this vector into a unit vector.
include matrices check: [vector: 1, 2, 3].normalize() is-roughly [vector: (1 / num-sqrt(14)), (2 / num-sqrt(14)), (3 / num-sqrt(14))] end
Scales this vector by the given constant.
include matrices check: [vector: 1, 2, 3].scale(2) is [vector: 2, 4, 6] end
Converts this vector to a one-row matrix.
include matrices check: [vector: 4, 5, 6].to-row-matrix() is [matrix(1, 3): 4, 5, 6] end
Converts this vector to a one-column matrix.
include matrices check: [vector: 4, 5, 6].to-col-matrix() is [matrix(3, 1): 4, 5, 6] end
3.1.22.3 Vector Functions
Returns the item at the given (0-based) index in the given vector.
include matrices check: vec-get([vector: 3, 5], 1) is 5 end
See .get.
Returns the length of the given vector.
include matrices check: vec-length([vector: 1, 2, 3, 4]) is 4 end
See .length.
Returns the dot product of the first vector with the second vector.
include matrices check: vec-dot([vector: 1, 2, 3], [vector: 3, 2, 1]) is 10 end
See .dot.
Returns the magnitude of the given vector.
include matrices check: vec-magnitude([vector: 3, 4]) is 5 vec-magnitude([vector: 4, 0]) is 4 end
See .magnitude.
Returns the cross product of the two given 3D vectors. (Raises an error if either vector is not 3-dimensional)
include matrices check: vec-cross([vector: 2, -3, 1], [vector: -2, 1, 1]) is [vector: -4, -4, -4] vec-cross([vector: 2, -3, 1, 4], [vector: -2, 1]) raises "length3" end
See .cross.
Normalizes the given vector into a unit vector.
include matrices check: vec-normalize([vector: 1, 2, 3]) is-roughly [vector: (1 / num-sqrt(14)), (2 / num-sqrt(14)), (3 / num-sqrt(14))] end
See .normalize.
Scales the given vector by the given constant.
include matrices check: vec-scale([vector: 1, 2, 3], 2) is [vector: 2, 4, 6] end
See .scale.
Adds the second vector to first one.
include matrices 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
Subtracts the second vector from first one.
include matrices 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.1.22.4 The Matrix Datatype
The Matrix type represents mathematical matrices.
Every matrix has a rows field and a cols field, which are the dimensions of the matrix.
include matrices 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.1.22.5 Matrix Constructors
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]\):
include matrices [matrix(2,3): 1, 2, 3, 4, 5, 6]
Supplying an inconsistent quantity of elements for a given matrix dimension will produce an error:
include matrices check: [matrix(4, 2): 100] raises "Invalid 4x2 Matrix" end
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]\):
include matrices check: [row-matrix: 1, 2, 3] is [matrix(1,3): 1, 2, 3] end
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]\):
include matrices check: [col-matrix: 1, 2, 3] is [matrix(3,1): 1, 2, 3] end
Constructs an \(n \times n\) identity matrix.
include matrices 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.
include matrices 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.
include matrices 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).
include matrices check: build-matrix(3, 2, lam(i,j): i + j end) is [matrix(3,2): 0, 1, 1, 2, 2, 3] end
3.1.22.6 Matrix Methods
These methods are available on all matrices.
Returns the matrix’s entry in the (0-based) ith row and the jth column.
include matrices 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
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]\):
include matrices 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
Returns a one-row/one-column matrix as a vector.
include matrices 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
Returns the matrix as a list of lists of numbers, with each list corresponding to one row.
include matrices check: [matrix(2,3): 1, 2, 3, 4, 5, 6].to-lists() is [list: [list: 1, 2, 3], [list: 4, 5, 6]] end
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]\):
include matrices check: [matrix(2,3): 1, 2, 3, 4, 5, 6].to-vectors() is [list: [vector: 1, 4], [vector: 2, 5], [vector: 3, 6]] end
Returns a one-row matrix with the matrix’s given row (0-based index).
include matrices check: [matrix(2,3): 1, 2, 3, 4, 5, 6].row(1) is [matrix(1,3): 4, 5, 6] [matrix(3,3): 1, 2, 3, 4, 5, 6, 7, 8, 9].row(2) is [matrix(1,3): 7, 8, 9] [matrix(1,1): 1].row(2) raises "index" end
Returns a one-column matrix with the matrix’s given column (0-based index).
include matrices check: [matrix(2,3): 1, 2, 3, 4, 5, 6].col(1) is [matrix(2,1): 2, 5] [matrix(3,3): 1, 2, 3, 4, 5, 6, 7, 8, 9].col(2) is [matrix(3,1): 3, 6, 9] [matrix(1,1): 1].col(2) raises "index" end
Returns the submatrix of the matrix comprised of the intersection of the given list of (0-based) row indices and the given list of (0-based) column indices.
For example, if our list of rows is \(\{0, 1\}\) and our list of columns is \(\{1, 2\}\), then the positions in the resulting submatrix will be the elements with \((row,col)\) positions \(\{(0, 1), (0, 2), (1, 1), (1, 2)\}\).
\(\left[\begin{matrix} a_{00} & a_{01} & a_{02} \\ a_{10} & a_{11} & a_{12} \\ a_{20} & a_{21} & a_{22} \end{matrix}\right]\).submatrix([list: 0, 1], [list: 1, 2]) \(= \left[\begin{matrix} a_{01} & a_{02} \\ a_{11} & a_{12}\end{matrix}\right]\)
This is shown in the below example:
include matrices check: [matrix(3,3): 1, 2, 3, 4, 5, 6, 7, 8, 9].submatrix([list: 0, 1], [list: 1, 2]) is [matrix(2,2): 2, 3, 5, 6] [matrix(3,3): 1, 2, 3, 4, 5, 6, 7, 8, 9].submatrix([list: 0, 3], [list: 1, 2]) raises "Invalid row index" [matrix(3,3): 1, 2, 3, 4, 5, 6, 7, 8, 9].submatrix([list: 0, 1], [list: 1, 3]) raises "Invalid column index" end
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}\]
include matrices check: [matrix(2,3): 1, 2, 3, 4, 5, 6].transpose() is [matrix(3,2): 1, 4, 2, 5, 3, 6] end
Computes the conjugate-transpose of this matrix. Since Pyret does not have complex numbers, this is synonymous with .transpose.
Returns a one-row matrix containing the matrix’s diagonal entries.
include matrices check: [matrix(3,3): 1, 2, 3, 4, 5, 6, 7, 8, 9].diagonal() is [matrix(1,3): 1, 5, 9] [matrix(2,3): 1, 2, 3, 4, 5, 6].diagonal() is [matrix(1,2): 1, 5] end
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]\).
include matrices 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
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]\).
include matrices 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
Returns the matrix as a list of one-row matrices. (Very similar to .to-lists, except this method returns a list of matrices instead.)
include matrices 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
Returns the matrix as a list of one-column matrices. (Very similar to .to-vectors, except this method returns a list of matrices instead.)
include matrices 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
Maps the given function entrywise over the matrix.
include matrices check: mult-two = lam(x): x * 2 end [matrix(2,2): 1, 2, 3, 4].map(mult-two) is [matrix(2,2): 2, 4, 6, 8] end
Maps the given function entrywise over corresponding elements of this and the given matrix.
include matrices 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
Maps the given function over each row in the matrix.
include matrices fun sum-row(r): for fold(sum from 0, item from r.to-list()): sum + item end end check: fun sum-row-once(row): [matrix(1,1): sum-row(row)] end fun sum-row-block(row): make-matrix(2, 2, sum-row(row)) end [matrix(2,3): 1, 2, 3, 4, 5, 6].row-map(sum-row-once) is [matrix(2,1): 6, 15] [matrix(2,2): 1, 2, 3, 4].row-map(sum-row-block) is [matrix(4,2): 3, 3, 3, 3, 7, 7, 7, 7] end
Maps the given function over each column in the matrix.
include matrices fun sum-col(c): for fold(sum from 0, item from c.to-list()): sum + item end end check: fun sum-col-once(col): [matrix(1,1): sum-col(col)] end fun sum-col-block(col): make-matrix(2, 2, sum-col(col)) end [matrix(2,3): 1, 2, 3, 4, 5, 6].col-map(sum-col-once) is [matrix(1,3): 5, 7, 9] [matrix(2,3): 1, 2, 3, 4, 5, 6].col-map(sum-col-block) is [matrix(2,6): 5,5,7,7,9,9, 5,5,7,7,9,9] end
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]\).
include matrices 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
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]\).
include matrices 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
Returns the trace of the matrix (i.e. the sum of its diagonal values).
include matrices 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
Multiplies each entry in the matrix by the given value.
include matrices 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
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\)
include matrices 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
Multiplies the matrix by itself the given number of times.
include matrices 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
Returns the determinant of the matrix, calculated via a recursive implementation of Laplace expansion.
include matrices 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
Returns true if the matrix is invertible, that is, it has a nonzero determinant.
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.
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}\]
include matrices check: [matrix(2,3): 1, 2, 3, 4, 5, 6].rref() is [matrix(2,3): 1, 0,-1, 0, 1, 2] end
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}\]
include matrices 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
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).
Returns the least squares solution for this and the given matrix, calculated using QR decomposition.
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}\]
Computes the Lp norm of the matrix using the given number.
Computes the L1, L2, and L∞ norms of the matrix, respectively.
include matrices 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(1 + 8 + 27, 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
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.
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.1.22.7 Matrix Binary Operations
Matrices are defined to permit using addition, subtraction, and multiplication operators on them, whenever the dimensions are compatible:
include matrices 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" end
include matrices 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" end
include matrices 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.1.22.8 Matrix Functions
The following functions are available to be performed on matrices.
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.
Returns a one-row matrix with the matrix’s given row. See .row.
Returns a one-column matrix with the matrix’s given column. See .col.
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.
Maps the given function entrywise over the matrix. See .map.
Maps the given function over the corresponding entries of the two given matrices. See .map2.
Maps the given function over each row in the matrix. See .row-map.
Maps the given function over each column in the matrix. See .col-map.
Returns the first matrix augmented with the second matrix. See .augment.
Returns the first matrix stacked on top of the second matrix. See .stack.
Returns the trace of the matrix (i.e. the sum of its diagonal values). See .trace.
Multiplies each entry in the matrix by the given value. See .scale.
Returns the Frobenius Product of the two matrices. See .dot.
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.
Returns the Reduced Row Echelon Form of the matrix. See .rref.
Returns the inverse of the matrix, if it is invertible. See .inverse.
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.
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.
See .qr-decomposition.
See .gram-schmidt.
Adds, subtracts, or multiplies the two matrices. See Matrix Binary Operations.
3.1.22.9 Matrix Conversion Functions
include matrices check: is-row-matrix([matrix(1, 3): 10, 20, 10]) is true is-row-matrix([matrix(3, 1): 10, 20, 10]) is false end
include matrices check: is-col-matrix([matrix(1, 3): 10, 20, 10]) is false is-col-matrix([matrix(3, 1): 10, 20, 10]) is true end
include matrices 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.
include matrices 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
include matrices 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
include matrices check: list-to-row-matrix([list: 1, 2, 3, 4]) is [matrix(1,4): 1, 2, 3, 4] end
include matrices check: list-to-col-matrix([list: 1, 2, 3, 4]) is [matrix(4,1): 1, 2, 3, 4] end
include matrices 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
include matrices 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
Returns a comparison predicate which returns true if each entry in both matrices is within delta of each other.