viva_math/matrix

Small dense matrices (2×2, 3×3, 4×4) and a generic MatN.

Scope: geometry, dynamics, small-system linear algebra. For batched or large-scale work (GEMM, BLAS, eigendecomposition) use viva_tensor, which dispatches to MKL/CUDA.

Conventions

Types

pub type Mat2 {
  Mat2(m11: Float, m12: Float, m21: Float, m22: Float)
}

Constructors

  • Mat2(m11: Float, m12: Float, m21: Float, m22: Float)
pub type Mat3 {
  Mat3(
    m11: Float,
    m12: Float,
    m13: Float,
    m21: Float,
    m22: Float,
    m23: Float,
    m31: Float,
    m32: Float,
    m33: Float,
  )
}

Constructors

  • Mat3(
      m11: Float,
      m12: Float,
      m13: Float,
      m21: Float,
      m22: Float,
      m23: Float,
      m31: Float,
      m32: Float,
      m33: Float,
    )
pub type Mat4 {
  Mat4(rows: List(List(Float)))
}

Constructors

  • Mat4(rows: List(List(Float)))
pub type MatN {
  MatN(rows: Int, cols: Int, data: List(List(Float)))
}

Constructors

  • MatN(rows: Int, cols: Int, data: List(List(Float)))

Values

pub fn mat2_add(a: Mat2, b: Mat2) -> Mat2
pub fn mat2_determinant(m: Mat2) -> Float

det(M) = m₁₁m₂₂ - m₁₂m₂₁.

pub fn mat2_eigenvalues(m: Mat2) -> Result(#(Float, Float), Nil)

Real eigenvalues of a 2×2 matrix via the characteristic quadratic.

λ² - tr(M)·λ + det(M) = 0 → λ = (tr ± √(tr² - 4·det)) / 2.

Returns Error for matrices with non-real eigenvalues (negative discriminant). For symmetric matrices the discriminant is always non-negative, so this always succeeds.

pub fn mat2_identity() -> Mat2
pub fn mat2_inverse(m: Mat2) -> Result(Mat2, Nil)

Inverse of a 2×2 matrix. Errors if singular.

pub fn mat2_mul(a: Mat2, b: Mat2) -> Mat2
pub fn mat2_rotation(theta: Float) -> Mat2

2-D rotation matrix by theta radians.

pub fn mat2_scale(m: Mat2, s: Float) -> Mat2
pub fn mat2_trace(m: Mat2) -> Float

Trace tr(M) = Σ mᵢᵢ.

pub fn mat2_transpose(m: Mat2) -> Mat2
pub fn mat2_zero() -> Mat2
pub fn mat3_add(a: Mat3, b: Mat3) -> Mat3
pub fn mat3_determinant(m: Mat3) -> Float

Determinant via cofactor expansion along the first row.

pub fn mat3_diagonal(a: Float, b: Float, c: Float) -> Mat3

Diagonal matrix.

pub fn mat3_frobenius(m: Mat3) -> Float

Frobenius norm √(Σ aᵢⱼ²) of a Mat3.

pub fn mat3_identity() -> Mat3
pub fn mat3_inverse(m: Mat3) -> Result(Mat3, Nil)

Inverse of a 3×3 matrix via adjugate / determinant.

Errors when the matrix is singular or ill-conditioned: the threshold is |det| < ε · ‖M‖_F³ where ε ≈ machine epsilon. Pure det == 0.0 is too permissive — matrices with det = 1e-20 would still pass and produce catastrophic blow-up after division.

pub fn mat3_mul(a: Mat3, b: Mat3) -> Mat3
pub fn mat3_mul_vec3(m: Mat3, v: vector.Vec3) -> vector.Vec3

Matrix × Vec3 product.

pub fn mat3_rot_x(theta: Float) -> Mat3

Rotation around the X axis.

pub fn mat3_rot_y(theta: Float) -> Mat3

Rotation around the Y axis.

pub fn mat3_rot_z(theta: Float) -> Mat3

Rotation around the Z axis.

pub fn mat3_scale(m: Mat3, s: Float) -> Mat3
pub fn mat3_symmetric_eigenvalues(
  m: Mat3,
) -> Result(#(Float, Float, Float), Nil)

Real eigenvalues of a symmetric 3×3 matrix via Smith’s trigonometric closed form. The matrix must be (numerically) symmetric.

Algorithm: characteristic cubic det(M - λI) = 0 in the form λ³ - tr·λ² + … Then shift by p = tr/3 and solve the depressed cubic using trigonometric substitution.

Reference: Smith (1961) “Eigenvalues of a Symmetric 3×3 Matrix”.

pub fn mat3_trace(m: Mat3) -> Float
pub fn mat3_transpose(m: Mat3) -> Mat3
pub fn mat3_zero() -> Mat3
pub fn mat4_identity() -> Mat4
pub fn mat4_scale(sx: Float, sy: Float, sz: Float) -> Mat4

Non-uniform scale.

pub fn mat4_translation(tx: Float, ty: Float, tz: Float) -> Mat4

Translation homogeneous transform.

pub fn mat4_zero() -> Mat4
pub fn matn_add(a: MatN, b: MatN) -> Result(MatN, Nil)

Add two matrices. Errors on shape mismatch.

pub fn matn_frobenius(m: MatN) -> Float

Frobenius norm: √(Σ aᵢⱼ²).

pub fn matn_from_rows(
  rows: List(List(Float)),
) -> Result(MatN, Nil)

Build a MatN from a row-major list of rows.

Errors if rows is empty, has empty rows, or rows of inconsistent length.

pub fn matn_identity(n: Int) -> MatN

Identity matrix of size n.

pub fn matn_mul(a: MatN, b: MatN) -> Result(MatN, Nil)

Matrix product A·B. Errors on shape mismatch.

pub fn matn_mul_vec(
  m: MatN,
  v: List(Float),
) -> Result(List(Float), Nil)

Matrix × column-vector product. Returns the resulting vector as a list.

pub fn matn_scale(m: MatN, s: Float) -> MatN

Scalar multiplication.

pub fn matn_trace(m: MatN) -> Result(Float, Nil)

Trace (sum of diagonal). Defined for square matrices.

pub fn matn_transpose(m: MatN) -> MatN

Transpose of a MatN. O(rows · cols).

pub fn matn_zeros(rows: Int, cols: Int) -> MatN

Zero matrix of given shape.

Search Document