Abstract

Small library of simple linear algebra-ish operations.

Synopsis

local module type linalg = {
type t
val veczeros: (n: i64) -> [n]t
val vecones: (n: i64) -> [n]t
val linspace: t -> t -> (n: i64) -> [n]t
val eye: (n: i64) -> [n][n]t
val matzeros: (n: i64) -> (m: i64) -> [n][m]t
val matones: (n: i64) -> (m: i64) -> [n][m]t
val mat [m] [n]: t -> [m][n]t
val i64 [m] [n]: i64 -> [m][n]t
val f64 [m] [n]: f64 -> [m][n]t
val dotprod [n]: [n]t -> [n]t -> t
val outer [n] [m]: [n]t -> [m]t -> *[n][m]t
val cross: [3]t -> [3]t -> [3]t
val matvecmul_row [n] [m]: [n][m]t -> [m]t -> *[n]t
val matvecmul_col [n] [m]: [n][m]t -> [n]t -> *[n][n]t
val matmul [n] [p] [m]: [n][p]t -> [p][m]t -> *[n][m]t
val block [m1] [m2] [n1] [n2]: (A: [m1][n1]t) -> (B: [m1][n2]t) -> (C: [m2][n1]t) -> (D: [m2][n2]t) -> [m1 + m2][n1 + n2]t
val matunary [n] [m]: (t -> t) -> [n][m]t -> [n][m]t
val matop [n] [m]: (t -> t -> t) -> [n][m]t -> [n][m]t -> [n][m]t
val matcomp [n] [m]: (t -> t -> bool) -> [n][m]t -> [n][m]t -> [n][m]bool
val vecscale [n]: t -> [n]t -> *[n]t
val matscale [n] [m]: t -> [n][m]t -> *[n][m]t
val matsub [m] [n]: [m][n]t -> [m][n]t -> *[m][n]t
val matadd [m] [n]: [m][n]t -> [m][n]t -> *[m][n]t
val vecnorm [n]: [n]t -> t
val matdiag [n]: [n][n]t -> [n][n]t
val fromdiag [n]: [n][n]t -> [n]t
val todiag [n]: [n]t -> [n][n]t
val kronecker [n] [m] [p] [q]: [m][n]t -> [p][q]t -> *[m * p][n * q]t
val kronecker' [m] [n] [p] [q]: [m][n]t -> [p][q]t -> *[m][n][p][q]t
val inv [n]: [n][n]t -> *[n][n]t
val ols [n] [m]: [n][m]t -> [n]t -> *[m]t
val househess [n]: [n][n]t -> ([n][n]t, [n][n]t)
val eig [n]: [n][n]t -> ([n][n]t, [n][n]t)
val matsqrt [n]: [n][n]t -> [n][n]t
}
module type field = {
type t
val +: t -> t -> t
val -: t -> t -> t
val *: t -> t -> t
val /: t -> t -> t
val **: t -> t -> t
val neg: t -> t
val i64: i64 -> t
val f64: f64 -> t
val abs: t -> t
val fma: t -> t -> t -> t
val sqrt: t -> t
val isnan: t -> bool
val isinf: t -> bool
}
module type ordered_field = {
include field
val ==: t -> t -> bool
val <: t -> t -> bool
val >: t -> t -> bool
val <=: t -> t -> bool
val >=: t -> t -> bool
val !=: t -> t -> bool
}
module mk_linalg: (T: ordered_field) -> linalg with t = T.t

Description

local module type linalg
type t

The scalar type.

val veczeros: (n: i64) -> [n]t

Make a zero vector.

val vecones: (n: i64) -> [n]t

Make a ones vector.

val linspace: t -> t -> (n: i64) -> [n]t

Create an array of n values varying linearly from the first to second argument.

val eye: (n: i64) -> [n][n]t

Make an identity matrix.

val matzeros: (n: i64) -> (m: i64) -> [n][m]t

Make a zero matrix.

val matones: (n: i64) -> (m: i64) -> [n][m]t

Make a ones matrix.

val mat [m] [n]: t -> [m][n]t

Cast scalar values up into matrices (simulated broadcasting).

val i64 [m] [n]: i64 -> [m][n]t

Cast integers up into matrices (simulated broadcasting).

val f64 [m] [n]: f64 -> [m][n]t

Cast floating point numbers up into matrices (simulated broadcasting).

val dotprod [n]: [n]t -> [n]t -> t

Dot product.

val outer [n] [m]: [n]t -> [m]t -> *[n][m]t

Outer product.

val cross: [3]t -> [3]t -> [3]t

Cross product (only for three-element vectors).

val matvecmul_row [n] [m]: [n][m]t -> [m]t -> *[n]t

Multiply a matrix with a row vector.

val matvecmul_col [n] [m]: [n][m]t -> [n]t -> *[n][n]t

Multiply a matrix with a column vector.

val matmul [n] [p] [m]: [n][p]t -> [p][m]t -> *[n][m]t

Multiply two matrices.

val block [m1] [m2] [n1] [n2]: (A: [m1][n1]t) -> (B: [m1][n2]t) -> (C: [m2][n1]t) -> (D: [m2][n2]t) -> [m1 + m2][n1 + n2]t

Form a block matrix from 4 submatrices.

val matunary [n] [m]: (t -> t) -> [n][m]t -> [n][m]t

A general way to apply a unary operator (e.g., (neg), etc.) element-wise to a matrix.

val matop [n] [m]: (t -> t -> t) -> [n][m]t -> [n][m]t -> [n][m]t

A general way to apply an operator (e.g., (+), (-), etc.) element-wise between two matrices.

val matcomp [n] [m]: (t -> t -> bool) -> [n][m]t -> [n][m]t -> [n][m]bool

A general way to apply a comparison operator (e.g., (==), (<), etc.) element-wise between two matrices.

val vecscale [n]: t -> [n]t -> *[n]t

Scale a vector by some constant.

val matscale [n] [m]: t -> [n][m]t -> *[n][m]t

Scale a matrix by some constant.

val matsub [m] [n]: [m][n]t -> [m][n]t -> *[m][n]t

Compute the difference of two matrices.

val matadd [m] [n]: [m][n]t -> [m][n]t -> *[m][n]t

Compute the sum of two matrices.

val vecnorm [n]: [n]t -> t

Compute the L2 norm of a vector.

val matdiag [n]: [n][n]t -> [n][n]t

Return a matrix containing only the diagonal of the argument.

val fromdiag [n]: [n][n]t -> [n]t

Return a vector of the input matrix's diagonal.

val todiag [n]: [n]t -> [n][n]t

Return a diagonal matrix with its diagonal being the input vector.

val kronecker [n] [m] [p] [q]: [m][n]t -> [p][q]t -> *[m * p][n * q]t

Kronecker product of two matrices.

val kronecker' [m] [n] [p] [q]: [m][n]t -> [p][q]t -> *[m][n][p][q]t

Kronecker product of two matrices, but preserving the blocked structure in the result.

val inv [n]: [n][n]t -> *[n][n]t

Compute the inverse of a matrix.

val ols [n] [m]: [n][m]t -> [n]t -> *[m]t

Solve a linear system.

val househess [n]: [n][n]t -> ([n][n]t, [n][n]t)

Compute the hessenberg form H of input matrix and its transformation matrix Q. Returns (H, Q).

val eig [n]: [n][n]t -> ([n][n]t, [n][n]t)

Compute the eigenvalue decomposition of matrix. Returns (D, V), where D is a diagonal matrix of the eigenvalues, and the columns of V are their eigenvectors.

val matsqrt [n]: [n][n]t -> [n][n]t

Compute the square root of the input matrix.

module type field
type t
val +: t -> t -> t
val -: t -> t -> t
val *: t -> t -> t
val /: t -> t -> t
val **: t -> t -> t
val neg: t -> t
val i64: i64 -> t
val f64: f64 -> t
val abs: t -> t
val fma: t -> t -> t -> t
val sqrt: t -> t
val isnan: t -> bool
val isinf: t -> bool
module type ordered_field
include field
val ==: t -> t -> bool
val <: t -> t -> bool
val >: t -> t -> bool
val <=: t -> t -> bool
val >=: t -> t -> bool
val !=: t -> t -> bool
module mk_linalg

Given some numeric type, produce a linalg module.