Part of hex, a computer algebra
library for Lean 4. The aim is fast executable code, fully verified, built
with spec-driven development.
hex-matrix provides dense matrices, represented as Vector (Vector R m) n
and generic over the coefficient type R. This library has no dependencies.
See hex-matrix-mathlib for
the correspondence with Mathlib's types and theory.
Add to your lakefile.toml:
[[require]]
name = "hex-matrix"git = "https://github.com/leanprover/hex-matrix.git"rev = "main"import HexMatrix
open Hex
-- Build a matrix from an entry function, or write one out explicitly.defA : Matrix Int 23 := Matrix.ofFn fun i j => (i + j : Int)
defB : Matrix Int 32 := #m[1, 2; 3, 4; 5, 6]
#eval A * B -- 2×2 matrix product#eval Matrix.transpose A -- 3×2 transpose#eval Matrix.gramMatrix A -- A * Aᵀ#eval Matrix.mulVec A (Vector.ofFn fun j => (j + 1 : Int))
-- Elementary row operations.#eval Matrix.rowSwap A 01#eval Matrix.rowScale A 05#eval Matrix.identity (R := Int) 3-- the 3×3 identityDense n × m matrices over an arbitrary coefficient type:
- constructors and accessors:
ofFn,row,col,setRow,setCol,transpose, the zero and identity matrices; - arithmetic: vector dot product and norm-squared, matrix-vector and
matrix-matrix multiplication (also via
*); - elementary row operations
rowSwap,rowScale,rowAdd,rowMoveUpand the column analoguescolAdd/colAddRight; - submatrix / leading-submatrix slicing and the Gram matrix
M * Mᵀ.
Row reduction (hex-row-reduce),
the Leibniz determinant (hex-determinant),
and the Bareiss algorithm (hex-bareiss)
build on it.
The elementary operations and matrix algebra carry their algebraic
identities: entrywise characterizations of every operation, the identity
matrix acting as a left and right multiplicative identity, involutivity of
transpose, gramMatrix M = M * Mᵀ, and the involutivity, multiplicative,
and inverse-preservation lemmas for the elementary operations.
The Semiring / Ring structure and the equivalence with Mathlib's
Matrix, which let you transfer Mathlib's linear-algebra results, live in
hex-matrix-mathlib.
The hex reference manual covers this library at https://kim-em.github.io/hex-dev/find/?domain=Verso.Genre.Manual.section&name=hex-matrix.
Development happens in the hex-dev
monorepo, not in this published mirror. Contributions are welcome as pull
requests to the SPEC/ directory: describe the behaviour you want, and
leave the implementation to the maintainer.