linalg/mat
dynamic :float matrix type and core operations
mat
(defstruct mat [rows : int cols : int data : (Vec float)])
a row-major rows x cols float64 matrix over a heap (Vec float).
mat-new
(mat-new [rows : int cols : int] :)
create a zero-initialized rows x cols matrix.
mat-new-zeroed
(mat-new-zeroed [rows : int cols : int] :)
create a zero-initialized rows x cols matrix.
mat-wrap
(mat-wrap [rows : int cols : int d : (Vec float)] :)
take ownership of a (Vec float) of rows*cols elements as a mat.
mat-of
(mat-of [rows cols & xs])
build a rows x cols matrix from row-major float literals.
mat-rows
(mat-rows [^borrow m : mat] :)
shape accessors.
mat-cols
(mat-cols [^borrow m : mat] :)
mat-shape
(mat-shape [^borrow m : mat] :)
(cons rows cols).
mat-get
(mat-get [^borrow m : mat r : int c : int] :)
element at (r, c), row-major. Bounds-checked by the backing Vec.
mat-set!
(mat-set! [^borrow m : mat r : int c : int v : float] :)
set element at (r, c).
mat-free
(mat-free [m : mat] :)
free the matrix and its backing buffer.
mat-identity
(mat-identity [n : int] :)
n x n identity matrix.
mat-diag
(mat-diag [^borrow v : lavec] :)
n x n diagonal matrix from a vector's elements.
mat-copy
(mat-copy [^borrow m : mat] :)
deep copy.
mat-row
(mat-row [^borrow m : mat r : int] :)
copy of row r as a vector.
mat-col
(mat-col [^borrow m : mat c : int] :)
copy of column c as a vector.
mat-submat
(mat-submat [^borrow m : mat r0 : int c0 : int r1 : int c1 : int] :)
copy of submatrix [r0,r1) x [c0,c1).
mat-add
(mat-add [^borrow a : mat ^borrow b : mat] :)
element-wise addition (dimensions must match).
mat-sub
(mat-sub [^borrow a : mat ^borrow b : mat] :)
element-wise subtraction.
mat-scale
(mat-scale [^borrow m : mat s : float] :)
scalar multiplication.
mat-mul
(mat-mul [^borrow a : mat ^borrow b : mat] :)
matrix multiplication: (m x n) * (n x p) -> (m x p).
mat-mul-vec
(mat-mul-vec [^borrow m : mat ^borrow v : lavec] :)
matrix-vector multiplication: (m x n) * n-vec -> m-vec.
mat-transpose
(mat-transpose [^borrow m : mat] :)
transpose: (m x n) -> (n x m).
mat-trace
(mat-trace [^borrow m : mat] :)
sum of diagonal elements (square matrices).
mat-norm-fro
(mat-norm-fro [^borrow m : mat] :)
Frobenius norm.
mat-norm-max
(mat-norm-max [^borrow m : mat] :)
maximum absolute element.
mat-square?
(mat-square? [^borrow m : mat] :)
1 if rows == cols else 0.
mat-approx-eq?
(mat-approx-eq? [^borrow a : mat ^borrow b : mat tol : float] :)
1 if all elements within tol and shapes match, else 0.
la-vec-outer
(la-vec-outer [^borrow a : lavec ^borrow b : lavec] :)
outer product of two vectors: a (len m) (x) b (len n)
Internal definitions
__mzeros-- Private: build a zero-filled (Vec float) of length n.__mat-zipwith-add-- arithmetic