Matrices #
This file defines basic properties of matrices.
Matrices with rows indexed by m
, columns indexed by n
, and entries of type α
are represented
with Matrix m n α
. For the typical approach of counting rows and columns,
Matrix (Fin m) (Fin n) α
can be used.
Notation #
The locale Matrix
gives the following notation:
⬝ᵥ
forMatrix.dotProduct
ᵀ
forMatrix.transpose
ᴴ
forMatrix.conjTranspose
Implementation notes #
For convenience, Matrix m n α
is defined as m → n → α
, as this allows elements of the matrix
to be accessed with A i j
. However, it is not advisable to construct matrices using terms of the
form λ i j, _
or even (λ i j, _ : Matrix m n α)
, as these are not recognized by lean as having
the right type. Instead, Matrix.of
should be used.
TODO #
Under various conditions, multiplication of infinite matrices makes sense. These have not yet been implemented.
Cast a function into a matrix.
The two sides of the equivalence are definitionally equal types. We want to use an explicit cast
to distinguish the types because Matrix
has different instances to pi types (such as Pi.mul
,
which performs elementwise multiplication, vs Matrix.mul
).
If you are defining a matrix, in terms of its entries, use of (fun i j ↦ _)
. The
purpose of this approach is to ensure that terms of the form (fun i j ↦ _) * (fun i j ↦ _)
do not
appear, as the type of *
can be misleading.
Porting note: In Lean 3, it is also safe to use pattern matching in a definition as | i j := _
,
which can only be unfolded when fully-applied. leanprover/lean4#2042 means this does not
(currently) work in Lean 4.
Equations
- Matrix.of = Equiv.refl (m → n → α)
Instances For
M.map f
is the matrix obtained by applying f
to each entry of the matrix M
.
This is available in bundled forms as:
AddMonoidHom.mapMatrix
LinearMap.mapMatrix
RingHom.mapMatrix
AlgHom.mapMatrix
Equiv.mapMatrix
AddEquiv.mapMatrix
LinearEquiv.mapMatrix
RingEquiv.mapMatrix
AlgEquiv.mapMatrix
Equations
- Matrix.map M f = ↑Matrix.of fun i j => f (M i j)
Instances For
The transpose of a matrix.
Equations
- Matrix.«term_ᵀ» = Lean.ParserDescr.trailingNode `Matrix.term_ᵀ 1024 1024 (Lean.ParserDescr.symbol "ᵀ")
Instances For
The conjugate transpose of a matrix defined in term of star
.
Equations
- Matrix.conjTranspose M = Matrix.map (Matrix.transpose M) star
Instances For
The conjugate transpose of a matrix defined in term of star
.
Equations
- Matrix.«term_ᴴ» = Lean.ParserDescr.trailingNode `Matrix.term_ᴴ 1024 1024 (Lean.ParserDescr.symbol "ᴴ")
Instances For
Matrix.col u
is the column matrix whose entries are given by u
.
Equations
- Matrix.col w = ↑Matrix.of fun x x_1 => w x
Instances For
Matrix.row u
is the row matrix whose entries are given by u
.
Equations
- Matrix.row v = ↑Matrix.of fun x y => v y
Instances For
Equations
- Matrix.inhabited = instInhabitedForAll_1 m
Equations
- Matrix.decidableEq = Fintype.decidablePiFintype
Equations
- Matrix.addSemigroup = Pi.addSemigroup
Equations
- Matrix.addCommSemigroup = Pi.addCommSemigroup
Equations
- Matrix.addZeroClass = Pi.addZeroClass
Equations
- Matrix.addCommMonoid = Pi.addCommMonoid
Equations
- Matrix.addCommGroup = Pi.addCommGroup
Equations
Equations
- Matrix.distribMulAction = Pi.distribMulAction R
simp-normal form pulls of
to the outside.
The scalar action via Mul.toSMul
is transformed by the same map as the elements
of the matrix, when f
preserves multiplication.
The scalar action via mul.toOppositeSMul
is transformed by the same map as the
elements of the matrix, when f
preserves multiplication.
diagonal d
is the square matrix such that (diagonal d) i i = d i
and (diagonal d) i j = 0
if i ≠ j
.
Note that bundled versions exist as:
Equations
- Matrix.diagonal d = ↑Matrix.of fun i j => if i = j then d i else 0
Instances For
Matrix.diagonal
as an AddMonoidHom
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Matrix.diagonal
as a LinearMap
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- Matrix.one = { one := Matrix.diagonal fun x => 1 }
Matrix.diag
as an AddMonoidHom
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Matrix.diag
as a LinearMap
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
dotProduct v w
is the sum of the entrywise products v i * w i
Equations
- Matrix.dotProduct v w = Finset.sum Finset.univ fun i => v i * w i
Instances For
dotProduct v w
is the sum of the entrywise products v i * w i
Equations
- Matrix.«term_⬝ᵥ_» = Lean.ParserDescr.trailingNode `Matrix.term_⬝ᵥ_ 72 72 (Lean.ParserDescr.binary `andthen (Lean.ParserDescr.symbol " ⬝ᵥ ") (Lean.ParserDescr.cat `term 73))
Instances For
Permuting a vector on the left of a dot product can be transferred to the right.
Permuting a vector on the right of a dot product can be transferred to the left.
Permuting vectors on both sides of a dot product is a no-op.
M * N
is the usual product of matrices M
and N
, i.e. we have that
(M * N) i k
is the dot product of the i
-th row of M
by the k
-th column of N
.
This is currently only defined when m
is finite.
Equations
- Matrix.instHMulMatrixMatrixMatrix = { hMul := fun M N i k => Matrix.dotProduct (fun j => M i j) fun j => N j k }
Equations
- One or more equations did not get rendered due to their size.
Left multiplication by a matrix, as an AddMonoidHom
from matrices to matrices.
Equations
Instances For
Right multiplication by a matrix, as an AddMonoidHom
from matrices to matrices.
Equations
Instances For
This instance enables use with smul_mul_assoc
.
This instance enables use with mul_smul_comm
.
Matrix.diagonal
as a RingHom
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
The ring homomorphism α →+* Matrix n n α
sending a
to the diagonal matrix with a
on the diagonal.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Matrix.diagonal
as an AlgHom
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Bundled versions of Matrix.map
#
The Equiv
between spaces of matrices induced by an Equiv
between their
coefficients. This is Matrix.map
as an Equiv
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The AddMonoidHom
between spaces of matrices induced by an AddMonoidHom
between their
coefficients. This is Matrix.map
as an AddMonoidHom
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The AddEquiv
between spaces of matrices induced by an AddEquiv
between their
coefficients. This is Matrix.map
as an AddEquiv
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The LinearMap
between spaces of matrices induced by a LinearMap
between their
coefficients. This is Matrix.map
as a LinearMap
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The LinearEquiv
between spaces of matrices induced by a LinearEquiv
between their
coefficients. This is Matrix.map
as a LinearEquiv
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The RingHom
between spaces of square matrices induced by a RingHom
between their
coefficients. This is Matrix.map
as a RingHom
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The RingEquiv
between spaces of square matrices induced by a RingEquiv
between their
coefficients. This is Matrix.map
as a RingEquiv
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The AlgHom
between spaces of square matrices induced by an AlgHom
between their
coefficients. This is Matrix.map
as an AlgHom
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The AlgEquiv
between spaces of square matrices induced by an AlgEquiv
between their
coefficients. This is Matrix.map
as an AlgEquiv
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
For two vectors w
and v
, vecMulVec w v i j
is defined to be w i * v j
.
Put another way, vecMulVec w v
is exactly col w * row v
.
Equations
- Matrix.vecMulVec w v = ↑Matrix.of fun x y => w x * v y
Instances For
mulVec M v
is the matrix-vector product of M
and v
, where v
is seen as a column matrix.
Put another way, mulVec M v
is the vector whose entries
are those of M * col v
(see col_mulVec
).
Equations
- Matrix.mulVec M v x = let i := x; Matrix.dotProduct (fun j => M i j) v
Instances For
vecMul v M
is the vector-matrix product of v
and M
, where v
is seen as a row matrix.
Put another way, vecMul v M
is the vector whose entries
are those of row v * M
(see row_vecMul
).
Equations
- Matrix.vecMul v M x = let j := x; Matrix.dotProduct v fun i => M i j
Instances For
Left multiplication by a matrix, as an AddMonoidHom
from vectors to vectors.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Associate the dot product of mulVec
to the left.
Matrix.transpose
as a LinearMap
Equations
- One or more equations did not get rendered due to their size.
Instances For
Matrix.transpose
as a RingEquiv
to the opposite ring
Equations
- One or more equations did not get rendered due to their size.
Instances For
Matrix.transpose
as an AlgEquiv
to the opposite ring
Equations
- One or more equations did not get rendered due to their size.
Instances For
Tell simp
what the entries are in a conjugate transposed matrix.
Compare with mul_apply
, diagonal_apply_eq
, etc.
Note that StarModule
is quite a strong requirement; as such we also provide the following
variants which this lemma would not apply to:
When star x = x
on the coefficients (such as the real numbers) conjTranspose
and transpose
are the same operation.
Matrix.conjTranspose
as an AddEquiv
Equations
- One or more equations did not get rendered due to their size.
Instances For
Matrix.conjTranspose
as a LinearMap
Equations
- One or more equations did not get rendered due to their size.
Instances For
When α
has a star operation, square matrices Matrix n n α
have a star
operation equal to Matrix.conjTranspose
.
Equations
- Matrix.instStarMatrix = { star := Matrix.conjTranspose }
Equations
- Matrix.instInvolutiveStarMatrix = InvolutiveStar.mk (_ : ∀ (M : Matrix n n α), Matrix.conjTranspose (Matrix.conjTranspose M) = M)
When α
is a *
-additive monoid, Matrix.star
is also a *
-additive monoid.
Equations
- Matrix.instStarAddMonoidMatrixAddMonoid = StarAddMonoid.mk (_ : ∀ (M N : Matrix n n α), Matrix.conjTranspose (M + N) = Matrix.conjTranspose M + Matrix.conjTranspose N)
When α
is a *
-(semi)ring, Matrix.star
is also a *
-(semi)ring.
Equations
- One or more equations did not get rendered due to their size.
Given maps (r_reindex : l → m)
and (c_reindex : o → n)
reindexing the rows and columns of
a matrix M : Matrix m n α
, the matrix M.submatrix r_reindex c_reindex : Matrix l o α
is defined
by (M.submatrix r_reindex c_reindex) i j = M (r_reindex i) (c_reindex j)
for (i,j) : l × o
.
Note that the total number of row and columns does not have to be preserved.
Equations
- Matrix.submatrix A r_reindex c_reindex = ↑Matrix.of fun i j => A (r_reindex i) (c_reindex j)
Instances For
Given a (m × m)
diagonal matrix defined by a map d : m → α
, if the reindexing map e
is
injective, then the resulting matrix is again diagonal.
simp
lemmas for Matrix.submatrix
s interaction with Matrix.diagonal
, 1
, and Matrix.mul
for when the mappings are bundled.
The natural map that reindexes a matrix's rows and columns with equivalent types is an equivalence.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The left n × l
part of an n × (l+r)
matrix.
Equations
- Matrix.subLeft A = Matrix.submatrix A id (Fin.castAdd r)
Instances For
The right n × r
part of an n × (l+r)
matrix.
Equations
- Matrix.subRight A = Matrix.submatrix A id (Fin.natAdd l)
Instances For
The top u × n
part of a (u+d) × n
matrix.
Equations
- Matrix.subUp A = Matrix.submatrix A (Fin.castAdd d) id
Instances For
The bottom d × n
part of a (u+d) × n
matrix.
Equations
- Matrix.subDown A = Matrix.submatrix A (Fin.natAdd u) id
Instances For
row_col
section #
Simplification lemmas for Matrix.row
and Matrix.col
.
Update, i.e. replace the i
th row of matrix A
with the values in b
.
Equations
- Matrix.updateRow M i b = ↑Matrix.of (Function.update M i b)
Instances For
Update, i.e. replace the j
th column of matrix A
with the values in b
.
Equations
- Matrix.updateColumn M j b = ↑Matrix.of fun i => Function.update (M i) j (b i)
Instances For
Updating rows and columns commutes in the obvious way with reindexing the matrix.