Linear algebra primitives (spec layer) #
This file defines the basic matrix/vector operations used across model specifications:
matMulSpec(matrix × matrix)matVecMulSpec(matrix × vector)vecMatMulSpec(vector × matrix)outerProductSpec
All operations are shape-indexed in their types, so misuse is caught by elaboration.
These are kept simple, “obvious” definitions (folding over List.finRange) so that:
- they are easy to reason about in proofs, and
- they can be instantiated over many scalar backends (
Float,ℚ,ExecFloat.Binary 8 23,ℝ, …).
PyTorch analogies:
matMulSpec A BisA @ BmatVecMulSpec A visA @ vvecMatMulSpec v Aisv @ AouterProductSpec a bis likea.unsqueeze(1) * b.unsqueeze(0)(result is(m,n)).
Create an identity matrix (n x n).
Notes:
- The
n = 0case is an empty matrix; it still exists as a well-typed tensor. - We use
i.val == j.valrather thanDecidableEq (Fin n)to keep the definition directly executable across backends.
Instances For
Matrix multiplication (m x n) @ (n x p) = (m x p).
This is the simplest definitional version: sum over the shared n dimension.
For performance-oriented runtime code, use the runtime layer; this spec is about clarity and proofs.
Instances For
Matrix-vector multiplication (m x n) @ (n) = (m).
Instances For
Rank-one tensor by matrix multiplication: (m) @ (m x n) = (n).
Instances For
Outer product (m) otimes (n) = (m x n).
Instances For
A coordinate of an outer product is the product of the corresponding vector entries.