tur/mat
dynamic :float matrix type and core operations
mat
(defstruct mat [])
a heap-allocated row-major m x n float matrix.
mat--alloc-data
(mat--alloc-data [n] :int)
Private helper: allocate float array
mat--free-data
(mat--free-data [data] :void)
Private helper: free float array
mat-new
(mat-new [rows cols] :int)
create an uninitialized m x n matrix
mat-new-zeroed
(mat-new-zeroed [rows cols] :int)
create an m x n matrix filled with zeros
mat-identity
(mat-identity [n] :int)
create an n x n identity matrix
mat-diag
(mat-diag [v] :int)
create a diagonal matrix from a vector
mat-from-list
(mat-from-list [rows cols data] :int)
create a matrix from a cons list of floats (row-major)
mat-copy
(mat-copy [m] :int)
create a deep copy of a matrix
mat-free
(mat-free [m] :void)
free a matrix
mat-rows
(mat-rows [m] :int)
get number of rows
mat-cols
(mat-cols [m] :int)
get number of columns
mat-shape
(mat-shape [m] :int)
get matrix shape as (cons rows cols)
mat-get
(mat-get [m r c] :float)
get element at (r, c) with bounds checking
mat-set!
(mat-set! [m r c v] :void)
set element at (r, c) with bounds checking
mat-row
(mat-row [m r] :int)
get a copy of row r as a vector
mat-col
(mat-col [m c] :int)
get a copy of column c as a vector
mat-submat
(mat-submat [m r0 c0 r1 c1] :int)
get a copy of submatrix from (r0, c0) to (r1, c1) (exclusive)
mat-add
(mat-add [a b] :int)
Arithmetic operations
mat-sub
(mat-sub [a b] :int)
element-wise matrix subtraction
mat-scale
(mat-scale [m s] :int)
scalar multiplication
mat-mul
(mat-mul [a b] :int)
matrix multiplication
mat-mul-vec
(mat-mul-vec [m v] :int)
matrix-vector multiplication
mat-transpose
(mat-transpose [m] :int)
transpose a matrix
mat-trace
(mat-trace [m] :float)
sum of diagonal elements (for square matrices)
mat-norm-fro
(mat-norm-fro [m] :float)
Frobenius norm (sqrt of sum of squares of all elements)
mat-norm-max
(mat-norm-max [m] :float)
maximum absolute element
mat-square?
(mat-square? [m] :int)
check if matrix is square
mat-approx-eq?
(mat-approx-eq? [a b tol] :int)
check if two matrices are approximately equal