3.29 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.
3.29.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:
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:
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
Returns the item at the given index in this vector.
check: [vector: 3, 5].get(1) is 5 end
Returns the length of this vector.
check: [vector: 1, 2, 3, 4].length() is 4 end
Returns the dot product of this vector with the given vector.
check: [vector: 1, 2, 3].dot([vector: 3, 2, 1]) is 10 end
Returns the magnitude of this vector.
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)
check: [vector: 2, -3, 1].cross([vector: -2, 1, 1]) is [vector: -4, -4, -4] end
Normalizes this vector into a unit vector.
check: [vector: 1, 2, 3].normalize() is [vector: (1 / num-sqrt(14)), (2 / num-sqrt(14)), (3 / num-sqrt(14))] end
Scales this vector by the given constant.
check: [vector: 1, 2, 3].scale(2) is [vector: 2, 4, 6] end
Converts this vector to a one-row matrix.
check: [vector: 4, 5, 6].to-row-matrix() is [matrix(1, 3): 4, 5, 6] end
Converts this vector to a one-column matrix.
check: [vector: 4, 5, 6].to-row-matrix() is [matrix(3, 1): 4, 5, 6] end
3.29.3 Vector Functions
Returns the item at the given index in the given vector.
check: vec-get([vector: 3, 5], 1) is 5 end
See .get.
Returns the length of the given vector.
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.
check: vec-dot[vector: 1, 2, 3], ([vector: 3, 2, 1]) is 10 end
See .dot.
Returns the magnitude of the given vector.
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)
check: vec-cross([vector: 2, -3, 1], [vector: -2, 1, 1]) is [vector: -4, -4, -4] end
See .cross.
Normalizes the given vector into a unit vector.
check: vec-normalize([vector: 1, 2, 3]) is [vector: (1 / num-sqrt(14)), (2 / num-sqrt(14)), (3 / num-sqrt(14))] end
See .normalize.
Scales the given vector by the given constant.
check: vec-scale([vector: 1, 2, 3], 2) is [vector: 2, 4, 6] end
See .scale.
Adds the second vector to first one.
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.
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.
Every matrix has a rows field and a cols field, which are the dimensions of the matrix.
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
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]\):
[matrix(2,3): 1, 2, 3, 4, 5, 6]
Supplying an inconsistent quantity of elements for a given matrix dimension will produce an error:
check: [matrix(4, 2): 100] raises "Invalid 1x2 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]\):
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]\):
check: [col-matrix: 1, 2, 3] is [matrix(3,1): 1, 2, 3] end
Constructs an \(n \times n\) identity matrix.
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.
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.
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).
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.
Returns the matrix’s entry in the ith row and the jth column.
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]\):
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.
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.
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]\):
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.
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
Returns a one-column matrix with the matrix’s given column.
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
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:
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
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}\]
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.
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
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]\).
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]\).
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.)
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.)
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.
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
Maps the given function entrywise over corresponding elements of this and the given matrix.
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.
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
Maps the given function over each column in the matrix.
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
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]\).
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]\).
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).
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.
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\)
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.
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.
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}\]
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}\]
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.
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
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.29.7 Matrix Binary Operations
Matrices are defined to permit using addition, subtraction, and multiplication operators on them, whenever the dimensions are compatible:
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
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
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.
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.29.9 Matrix Conversion Functions
check: is-row-matrix([matrix(1, 3): 10, 20, 10]) is true is-row-matrix([matrix(3, 1): 10, 20, 10]) is false end
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.
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.
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.
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.
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.
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.
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.
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.