viva_math/matrix_dense

Dense matrix backed by BitArray (binary row-major).

Counterpart to viva_math/matrix.MatN. Where MatN stores rows as nested List(Float), DenseMat keeps a single contiguous binary buffer with one IEEE-754 little-endian double per element. This:

Scope

Suitable for matrices up to a few thousand rows × cols. For tensor-scale work (batched GEMM, GPU acceleration) defer to viva_tensor. This module is the middle ground between the trivially-correct MatN and the heavy viva_tensor.

Limitations

Types

pub type DenseMat {
  DenseMat(rows: Int, cols: Int, data: BitArray)
}

Constructors

  • DenseMat(rows: Int, cols: Int, data: BitArray)

Values

pub fn add(a: DenseMat, b: DenseMat) -> Result(DenseMat, Nil)

Element-wise add. Errors on shape mismatch.

pub fn byte_size(m: DenseMat) -> Int

Number of bytes consumed by the data buffer. Useful for benchmarking.

pub fn frobenius(m: DenseMat) -> Float

Frobenius norm √(Σ aᵢⱼ²).

pub fn from_list(
  rows: Int,
  cols: Int,
  values: List(Float),
) -> Result(DenseMat, Nil)

Build from row-major list of values. Errors if shape doesn’t match.

pub fn get(m: DenseMat, row: Int, col: Int) -> Result(Float, Nil)

Random-access read in O(1). Errors on out-of-bounds.

pub fn hadamard(
  a: DenseMat,
  b: DenseMat,
) -> Result(DenseMat, Nil)

Element-wise Hadamard product.

pub fn identity(n: Int) -> Result(DenseMat, Nil)

Identity matrix of size n.

pub fn mul(a: DenseMat, b: DenseMat) -> Result(DenseMat, Nil)

Matrix product A · B. Errors on shape mismatch.

Naïve triple-loop O(n³). For larger matrices defer to viva_tensor which dispatches to BLAS / cuBLAS.

pub fn row(m: DenseMat, row_idx: Int) -> Result(List(Float), Nil)

Get an entire row as a list.

pub fn scale(m: DenseMat, s: Float) -> DenseMat

Scalar multiplication.

pub fn sub(a: DenseMat, b: DenseMat) -> Result(DenseMat, Nil)

Element-wise subtract.

pub fn to_rows(m: DenseMat) -> List(List(Float))

Convert to nested-list representation for interop with MatN.

pub fn trace(m: DenseMat) -> Result(Float, Nil)

Trace of a square matrix.

pub fn transpose(m: DenseMat) -> DenseMat

Transpose. O(rows · cols) but with contiguous writes.

pub fn zeros(rows: Int, cols: Int) -> Result(DenseMat, Nil)

Zero matrix of given shape. Errors on non-positive dimensions.

Search Document