Skip to content

Latest commit

 

History

History
167 lines (121 loc) · 3.9 KB

File metadata and controls

167 lines (121 loc) · 3.9 KB

Core Module

Matrix, vector, and array types — the foundation of Zigen.

Matrix (Fixed Size)

Matrix(T, rows, cols) — Compile-time-sized matrix stored on the stack.

Construction

const M = Zigen.Matrix(f64, 3, 3);

M.zero()                    // All zeros
M.identity()                // Identity matrix
M.constant(value)           // Fill with constant
M.random(rng)               // Random values
M.fromArray(data)           // From [rows][cols]T array
M.fromSlice(slice)          // From flat slice

Arithmetic

m.add(other)                // m + other
m.sub(other)                // m - other
m.scale(scalar)             // scalar * m
m.mul(cols, other)          // m * other (compile-time cols)
m.cwiseMul(other)           // Element-wise multiply
m.cwiseDiv(other)           // Element-wise divide

In-Place Variants (zero allocation)

m.addInto(other, &result)
m.subInto(other, &result)
m.mulInto(other, &result)
m.transposeInto(&result)

Properties

m.at(i, j)                 // Read element
m.set(i, j, value)         // Write element
m.rows()                   // Number of rows (comptime)
m.cols()                   // Number of columns (comptime)
m.trace()                  // Trace (square only)
m.norm()                   // Frobenius norm
m.determinant()            // Determinant (up to 4x4)
m.inverse()                // Inverse (up to 4x4)
m.transpose()              // Transpose

MatrixDynamic (Dynamic Size)

MatrixDynamic(T) — Heap-allocated matrix with runtime dimensions.

Construction

const MatXd = Zigen.MatrixDynamic(f64);

var m = try MatXd.init(allocator, rows, cols);
defer m.deinit();

MatXd.zero(allocator, rows, cols)
MatXd.identity(allocator, n)

Operations

Same API as fixed-size plus:

m.inverseDynamic()              // Gauss-Jordan inverse (allocating)
m.inverseDynamicInto(&result, work)  // Zero-allocation inverse

In-Place Operations

a.addInto(b, &result)
a.subInto(b, &result)
a.mulInto(b, &result)
a.transposeInto(&result)

Vector (Fixed Size)

Vector(T, n) — Compile-time-sized vector.

v.at(i)                    // Read element
v.dot(other)               // Dot product
v.cross(other)             // Cross product (3D only)
v.norm()                   // Euclidean norm
v.squaredNorm()            // Squared norm
v.normalized()             // Return unit vector
v.normalize()              // Normalize in-place

VectorDynamic (Dynamic Size)

VectorDynamic(T) — Heap-allocated vector.

var v = try Zigen.VectorDynamic(f64).init(allocator, n);
defer v.deinit();

Array (Element-wise)

Array(T, n) and ArrayDynamic(T) — Element-wise operations analogous to Eigen's Array.

a.cwiseAdd(b)               // Element-wise add
a.cwiseMul(b)               // Element-wise multiply
a.cwiseDiv(b)               // Element-wise divide
a.cwiseAbs()                // Element-wise absolute value
a.cwiseSqrt()               // Element-wise square root

Map / View Types

MapMatrix(T, rows, cols) and MapVector(T, n) — Non-owning views over existing memory.

const view = Zigen.MapMatrix(f64, 3, 3).fromSlice(data_ptr);

Matrix Functions

Zigen.matExp(f64, 3, A)         // e^A
Zigen.matPow(f64, 3, A, p)      // A^p
Zigen.matSqrt(f64, 3, A)        // √A
Zigen.matLog(f64, 3, A)         // log(A)

Kronecker Product

Zigen.kronecker(f64, 2, 2, 2, 2, A, B)      // Fixed: A ⊗ B
Zigen.kroneckerDynamic(f64, A, B)             // Dynamic: A ⊗ B

Triangular Utilities

Zigen.solveLowerTriangular(f64, 3, L, b)     // Solve Lx = b
Zigen.solveUpperTriangular(f64, 3, U, b)     // Solve Ux = b
Zigen.lowerTriangular(f64, 3, M)             // Extract lower triangle
Zigen.upperTriangular(f64, 3, M)             // Extract upper triangle
Zigen.conditionNumber(f64, 3, M)             // Estimate κ(M)