Matrix by fin (finite set over nat) in Coq.
We develop a formal matrix library FinMatrix
in Coq, which contains : vector and matrix type, all basic vector operations and matrix operations, and lots of properties about them. It is designed polymorphic and hierarchy depended on element type and structure, and is computational supported for Coq to quick evaluate results of vector (or matrix) operations. The core idea and features are following:
-
fin n
type means a finite set withn
elements. It is defined asInductive fin (n : nat) := Fin (i : nat) (H : i < n).
. Thus,fin 3
type will have only 3 inhabitantsFin 3 0 _, Fin 3 1 _,, Fin 3 2 _
. -
vec A n
type means an-
dimensional vector overA
. It is defined as a functionfin n -> A
, whereA
is any type. The Leibniz equal (i.e. built-in "=") could be used for vector equality, which is easier than that use Setoid equal when with general function model. Because the later way have proof burden for equality preservation by any new operation, in order to support rewriting. -
mat A r c
type means a$r\times c$ matrix overA
type. It is defined asvec (vec A c) r
, i.e., ar
dimensional vector overvec A c
. This design have many good features:- The vector theory is reused.
-
mat A r c
is$\alpha-conversion$ tofin r -> fin c -> A
, which made a matrix is just a function from row and column index to element. - High rank of vectors is supported with no burden. For example,
vec (vec (vec A n3) n2) n1
is a$n_1\times n_2\times n_3$ "matrix" overA
.
- The element type is organized by two technologies both
Typeclass
andModule
. We useTypeclass
to develop polymorphic theories of vector or matrix, and organized the hierarchy withmonoid
,ring
,orderedRing
,field
,orderedField
,normedOrderedField
. We also useModule
to encapsulate this hierarchy. Thus, usual number fields such asnat
,Z
,Q
,Qc
,R
andC
type could be used easily. - The main operations or predications of vectors contain:
- get/insert/remove elements of vector:
vnth: v.[i], vinsertH, vinsertT, vremoveH, vremoveT
- converting between function/list to vector:
v2f, f2v, v2l, l2v
- addition:
vadd: v1 + v2
- scalar multiplication:
vcmul: c \.* v
- dot product:
vdot: <v1, v2>
- sum of a vector:
vsum
- orthogonal:
vorth: v1 _|_ v2
- projection/perpendicular component:
vperp, vproj
- collinear:
vcoll: v1 // v2
- parallel:
vpara: v1 //+ v2
- anti parallel:
vantipara: v1 //- v2
- get/insert/remove elements of vector:
- The main operations or predications of matrix contain:
- get/insert/remove elements/rows/columns of a matrix:
mnth: M.[i].[j], mrow: M.[i], mcol: M&[j]
- converting between function/list to matrix:
m2f, f2m, m2l, l2m
- addition:
madd: M1 + M2
- scalar multiplication:
mcmul: c \.* M
- multiplication:
mmul: M1 * M2
- left/right multiply vector:
mmulv: M *v a, mvmul: a v* M
- transpose:
mtrans: M\T
- trace:
mtrace: tr M
- determinant:
mdet: |M|
- invertible matrix:
minvertible
- inversion by gauss elimination:
minvGE: M\-1
- inversion by adjoint matrix:
minvAM: M\-1
- Orthogonal matrix, SO(n)
- get/insert/remove elements/rows/columns of a matrix:
FinMatrix
is different with existing works.
- CoqMatrix is a recent formal matrix library which is contain 6 different models, and it is aiming to integration and comparison. This library is a good place for experimenting variety of different models. However, there have not too much rich vector / matrix theories are developed.
-
matrix in Mathcomp is a popular formal matrix library which have rich theories. However, this library also has some drawbacks.
- There are two kinds of vector, row vector is a
$1\times n$ matrix, column vector is a$n\times 1$ matrix. That means, vector is a derived concept from matrix. But ourFinMatrix
library provided a standalone vector type, and related vector theory. - High rank matrix is not supported. For example,
$n_1\times n_2\times n_3$ matrix is not supoorted. - Most of matrix operations are not computational. For example,
Compute
command cannot get a friendly simple result when we want get an element from a constant matrix, and it just return a complicated expression. I guess it is related with itslock
mechanism which I not very familiar. Another possible reason is due to its complicated hierarchy.
- There are two kinds of vector, row vector is a
-
nat-index-matrix
is a formal matrix library used in Verified Quantum Computing by Robert Rand. An old website is University of Maryland, and a newer website is University of Chicago. It uses a simple matrix type definition which isDefinition mat (r c : nat) := nat -> nat -> C
. Here, they use complex number (C
type) as element type. Another thing is that the matrix equality must be Setoid equal, thus bring many proof burden to enable rewriting. Moreover, becauser
andc
are dummy type parameters, such thatmat 3 4
andmat 2 5
could not be distinguished by Coq type system.
- From
opam
We are happy to announced thatFinMatrix
has been submitted toCoq-Package Index
opam install coq-finmatrix
- From source code
make make install
- example usage can be found in
./FinMatrix/examples/