Matrix algebra — product, transpose, determinant, trace
Matrices are List<List<Float>> built by reshape. `*` is matmul, `.T` is transpose, det() is the determinant (LU with pivoting), diag_sum() the trace. Soma has no structural list ==, so we probe results by index/det.
A Soma program from the math part of the verified corpus: 56 lines, cells MatrixAlgebra, MatrixTests. It passes soma check with soma 3.2.4.
How it is built
The cell MatrixAlgebra declares 3 signals in its face: product(a: List, b: List) -> List, trace_of(m: List) -> Float and run() -> Map. Handlers: product (line 19), trace_of (line 23) and run (line 27).
The file also holds 1 test cell (MatrixTests) with 6 assertions, run by soma test.
The source
matrix_algebra.cell
// ============================================================================
// Matrix algebra — product, transpose, determinant, trace
// ============================================================================
//
// Matrices are List<List<Float>> built by reshape. `*` is matmul, `.T` is
// transpose, det() is the determinant (LU with pivoting), diag_sum() the
// trace. Soma has no structural list ==, so we probe results by index/det.
//
// soma run matrix_algebra.cell run
// ============================================================================
cell MatrixAlgebra {
face {
signal product(a: List, b: List) -> List
signal trace_of(m: List) -> Float
signal run() -> Map
}
on product(a: List, b: List) {
return a * b
}
on trace_of(m: List) {
return diag_sum(m)
}
on run() {
let A = [1, 2, 3, 4].reshape(2, 2)
let B = [5, 6, 7, 8].reshape(2, 2)
let C = A * B
return map(
"product_00", C[0][0],
"product_11", C[1][1],
"det_A", det(A),
"trace_A", diag_sum(A),
"transpose_shape", A.T.shape
)
}
}
cell test MatrixTests {
rules {
// [1,2;3,4]·[5,6;7,8] = [19,22;43,50]
assert run()["product_00"] == 19.0
assert run()["product_11"] == 50.0
// det([1,2;3,4]) = 1*4 - 2*3 = -2
assert abs(det([1, 2, 3, 4].reshape(2, 2)) - (0 - 2.0)) < 0.0000001
// trace = 1 + 4 = 5
assert diag_sum([1, 2, 3, 4].reshape(2, 2)) == 5.0
// transpose flips shape
assert [1, 2, 3, 4, 5, 6].reshape(2, 3).T.shape[0] == 3
// identity determinant is 1
assert det(identity(4)) == 1.0
}
}What the toolchain says
$ soma check matrix_algebra.cell
✓ All checks passed.$ soma test matrix_algebra.cell
test MatrixTests ...
✓ assert run()["product_00"] == 19.0
✓ assert run()["product_11"] == 50.0
✓ assert abs(det([1, 2, 3, 4].reshape(2, 2)) - (0 - 2.0)) < 0.0000001
✓ assert diag_sum([1, 2, 3, 4].reshape(2, 2)) == 5.0
✓ assert [1, 2, 3, 4, 5, 6].reshape(2, 3).T.shape[0] == 3
✓ assert det(identity(4)) == 1.0
6 tests: 6 passed, 0 failed$ soma verify matrix_algebra.cell
Verifying matrix_algebra.cell...
No state machine in this program: nothing to prove beyond `soma check` (invariants and lifecycles are what verify proves).
VERIFY OK (vacuous — no state machine)$ soma run matrix_algebra.cell run
{"product_00": 19.0, "product_11": 50.0, "det_A": -2.0, "trace_A": 5.0, "transpose_shape": [2, 2]}