soma.cell

Matrices

Matrices, in the reference of the Soma programming language (soma 3.2.4). A block marked ✓ is a whole program that soma check accepts; the others are excerpts.

A matrix is a List<List<Float>> (a list of equal-length rows) and is a first-class value: bracket-indexed, method-callable, and operated on with arithmetic operators.

let M = [1, 0, 0, 1].reshape(2, 2)    // flat list → 2×2 matrix
let A = [1, 2, 3, 4, 5, 6].reshape(2, 3)
let s = A.shape                        // [2, 3]  (parens-free pseudo-field)
let t = A.T                            // transpose (also A.transpose())
let x = A[1][2]                        // element access (row 1, col 2)
let n = matrix("1 2; 3 4")             // MATLAB-style literal

// vectorized operators (numpy/MATLAB style):
let prod = A * A.T                     // matmul (both matrices)
let scaled = 2 * A                     // scalar broadcast: * / + - both orders
let shifted = A + 10                   // matrix + scalar
let halves = A / 2
let summed = A + A                     // elementwise (equal shapes)
let v = list(1.0, 2.0, 3.0)
let v2 = v * 2                         // vector broadcast: * / + -
let sq = v * v                         // vector elementwise: + * / -
let vsum = v + v                       // elementwise add (numeric vectors)
let mask = A > 2                       // comparison mask → 0/1 matrix
let vm = v >= 2.0                      // 0.0/1.0 vector (a mask: multiply with it)
// list CONCATENATION is explicit: concat(a, b). Non-numeric lists
// (strings, records) keep + = concat.
let d = det([4, 3, 6, 3].reshape(2, 2))   // -6.0

// any builtin is also a method (UFCS): xs.sum(), xs.sort(), xs.reverse(),
// m.det(), m.transpose(), m.matmul(other)

Builtins: reshape(values, r, c), transpose, shape, matmul, det, diag_sum (trace), identity(n) / eye(n), scale(m, k), mat, rows, cols, diag, zeros, ones, plus the quant suite (svd_lowrank, regress_sgd, clean_covariance, var_historical, …).