The Mathlib Handbook

The volumes of the handbook are not written yet. This site shows the map of Mathlib that they will follow, at Mathlib 4472534b60.

The areas / V07. Linear algebra

Mathlib.LinearAlgebra.Matrix.Vec

29 declarations. Mathlib docs · Source

Vectorization of matrices

This file defines Matrix.vec A, the vectorization of a matrix A, formed by stacking the columns of A into a single large column vector.

Since mathlib indices matrices by arbitrary types rather than Fin n, the result of Matrix.vec on A : Matrix m n R is indexed by n × m. The Fin (n * m) interpretation can be restored by composing with finProdFinEquiv.symm:

-- ![1, 2, 3, 4]
#eval vec !![1, 3; 2, 4] ∘ finProdFinEquiv.symm

While it may seem more natural to index by m × n, keeping the indices in the same order, this would amount to stacking the rows into one long row, and goes against the literature. If you want this function, you can write Matrix.vec Aᵀ instead.

References

def Matrix.vec E1 #

{m : Type u_1} → {n : Type u_2} → {R : Type u_3} → Matrix m n R → n × m → R

All the matrix entries, arranged into one column.

theorem Matrix.vec_of E0 #

∀ {m : Type u_2} {n : Type u_1} {R : Type u_3} (f : m → n → R), (Matrix.of f).vec = Function.uncurry (flip f)

theorem Matrix.vec_transpose E1 #

∀ {m : Type u_1} {n : Type u_2} {R : Type u_3} (A : Matrix m n R), A.transpose.vec = A.vec ∘ Prod.swap

theorem Matrix.vec_eq_uncurry E1 #

∀ {m : Type u_1} {n : Type u_2} {R : Type u_3} (A : Matrix m n R), A.vec = Function.uncurry fun i j ↦ A j i

theorem Matrix.vec_inj E1 #

∀ {m : Type u_1} {n : Type u_2} {R : Type u_3} {A B : Matrix m n R}, A.vec = B.vec ↔ A = B

theorem Matrix.vec_bijective E1 #

∀ {m : Type u_3} {n : Type u_2} {R : Type u_1}, Function.Bijective Matrix.vec

theorem Matrix.vec_map E1 #

∀ {m : Type u_1} {n : Type u_2} {R : Type u_3} {S : Type u_4} (A : Matrix m n R) (f : R → S), (A.map f).vec = f ∘ A.vec

theorem Matrix.vec_zero E0 #

∀ {m : Type u_3} {n : Type u_2} {R : Type u_1} [inst : Zero R], Matrix.vec 0 = 0

theorem Matrix.vec_eq_zero_iff E0 #

∀ {m : Type u_2} {n : Type u_3} {R : Type u_1} [inst : Zero R] {A : Matrix m n R}, A.vec = 0 ↔ A = 0

theorem Matrix.vec_add E0 #

∀ {m : Type u_2} {n : Type u_3} {R : Type u_1} [inst : Add R] (A B : Matrix m n R), (A + B).vec = A.vec + B.vec

theorem Matrix.vec_neg E1 #

∀ {m : Type u_2} {n : Type u_3} {R : Type u_1} [inst : Neg R] (A : Matrix m n R), (-A).vec = -A.vec

theorem Matrix.vec_sub E0 #

∀ {m : Type u_2} {n : Type u_3} {R : Type u_1} [inst : Sub R] (A B : Matrix m n R), (A - B).vec = A.vec - B.vec

theorem Matrix.vec_smul E0 #

∀ {m : Type u_3} {n : Type u_4} {R : Type u_2} {α : Type u_1} [inst : SMul α R] (r : α) (A : Matrix m n R),
  (r • A).vec = r • A.vec

theorem Matrix.vec_sum E1 #

∀ {ι : Type u_2} {m : Type u_3} {n : Type u_4} {R : Type u_1} [inst : AddCommMonoid R] (s : Finset ι)
  (A : ι → Matrix m n R), (∑ i ∈ s, A i).vec = ∑ i ∈ s, (A i).vec

theorem Matrix.vec_dotProduct_vec E1 #

∀ {m : Type u_2} {n : Type u_3} {R : Type u_1} [inst : AddCommMonoid R] [inst_1 : Mul R] [inst_2 : Fintype m]
  [inst_3 : Fintype n] (A B : Matrix m n R), A.vec ⬝ᵥ B.vec = (A.transpose * B).trace

theorem Matrix.star_vec E1 #

∀ {m : Type u_2} {n : Type u_3} {R : Type u_1} [inst : Star R] (x : Matrix m n R), star x.vec = (x.map star).vec

theorem Matrix.star_vec_dotProduct_vec E1 #

∀ {m : Type u_2} {n : Type u_3} {R : Type u_1} [inst : AddCommMonoid R] [inst_1 : Mul R] [inst_2 : Star R]
  [inst_3 : Fintype m] [inst_4 : Fintype n] (A B : Matrix m n R), star A.vec ⬝ᵥ B.vec = (A.conjTranspose * B).trace

theorem Matrix.vec_hadamard E1 #

∀ {m : Type u_2} {n : Type u_3} {R : Type u_1} [inst : Mul R] (A B : Matrix m n R), (A.hadamard B).vec = A.vec * B.vec

theorem Matrix.vec_single E0 #

∀ {m : Type u_1} {n : Type u_2} {R : Type u_3} [inst : DecidableEq m] [inst_1 : DecidableEq n] [inst_2 : Zero R] (i : m)
  (j : n) (r : R), (Matrix.single i j r).vec = Pi.single (j, i) r

theorem Matrix.hadamard_kronecker_hadamard E1 #

∀ {l : Type u_1} {m : Type u_2} {n : Type u_4} {p : Type u_5} {R : Type u_3} [inst : CommSemigroup R]
  (A B : Matrix l m R) (C D : Matrix n p R),
  Matrix.kroneckerMap (fun x1 x2 ↦ x1 * x2) (A.hadamard B) (C.hadamard D) =
    (Matrix.kroneckerMap (fun x1 x2 ↦ x1 * x2) A C).hadamard (Matrix.kroneckerMap (fun x1 x2 ↦ x1 * x2) B D)

theorem Matrix.kronecker_hadamard_kronecker E1 #

∀ {l : Type u_1} {m : Type u_2} {n : Type u_4} {p : Type u_5} {R : Type u_3} [inst : CommSemigroup R] (A : Matrix l m R)
  (B : Matrix n p R) (C : Matrix l m R) (D : Matrix n p R),
  (Matrix.kroneckerMap (fun x1 x2 ↦ x1 * x2) A B).hadamard (Matrix.kroneckerMap (fun x1 x2 ↦ x1 * x2) C D) =
    Matrix.kroneckerMap (fun x1 x2 ↦ x1 * x2) (A.hadamard C) (B.hadamard D)

theorem Matrix.kronecker_mulVec_vec_of_commute E2 #

∀ {l : Type u_1} {m : Type u_2} {n : Type u_4} {p : Type u_5} {R : Type u_3} [inst : NonUnitalSemiring R]
  [inst_1 : Fintype m] [inst_2 : Fintype n] (A : Matrix l m R) (X : Matrix m n R) (B : Matrix p n R),
  (∀ (x : R) (i : p) (j : n), Commute x (B i j)) →
    (Matrix.kroneckerMap (fun x1 x2 ↦ x1 * x2) B A).mulVec X.vec = (A * X * B.transpose).vec

Technical lemma shared with kronecker_mulVec_vec and vec_mul_eq_mulVec.

theorem Matrix.vec_vecMul_kronecker_of_commute E2 #

∀ {l : Type u_2} {m : Type u_1} {n : Type u_4} {p : Type u_5} {R : Type u_3} [inst : NonUnitalSemiring R]
  [inst_1 : Fintype m] [inst_2 : Fintype n] (A : Matrix m l R) (X : Matrix m n R) (B : Matrix n p R),
  (∀ (x : R) (i : m) (j : l), Commute (A i j) x) →
    Matrix.vecMul X.vec (Matrix.kroneckerMap (fun x1 x2 ↦ x1 * x2) B A) = (A.transpose * X * B).vec

Technical lemma shared with vec_vecMul_kronecker and vec_mul_eq_vecMul.

theorem Matrix.kronecker_mulVec_vec E1 #

∀ {l : Type u_1} {m : Type u_2} {n : Type u_4} {p : Type u_5} {R : Type u_3} [inst : NonUnitalCommSemiring R]
  [inst_1 : Fintype m] [inst_2 : Fintype n] (A : Matrix l m R) (X : Matrix m n R) (B : Matrix p n R),
  (Matrix.kroneckerMap (fun x1 x2 ↦ x1 * x2) B A).mulVec X.vec = (A * X * B.transpose).vec

theorem Matrix.vec_vecMul_kronecker E1 #

∀ {l : Type u_2} {m : Type u_1} {n : Type u_4} {p : Type u_5} {R : Type u_3} [inst : NonUnitalCommSemiring R]
  [inst_1 : Fintype m] [inst_2 : Fintype n] (A : Matrix m l R) (X : Matrix m n R) (B : Matrix n p R),
  Matrix.vecMul X.vec (Matrix.kroneckerMap (fun x1 x2 ↦ x1 * x2) B A) = (A.transpose * X * B).vec

theorem Matrix.vec_mul_eq_mulVec E1 #

∀ {l : Type u_2} {m : Type u_3} {n : Type u_1} {R : Type u_4} [inst : Semiring R] [inst_1 : Fintype m]
  [inst_2 : Fintype n] [inst_3 : DecidableEq n] (A : Matrix l m R) (B : Matrix m n R),
  (A * B).vec = (Matrix.kroneckerMap (fun x1 x2 ↦ x1 * x2) 1 A).mulVec B.vec

theorem Matrix.vec_mul_eq_vecMul E1 #

∀ {m : Type u_1} {n : Type u_2} {p : Type u_4} {R : Type u_3} [inst : Semiring R] [inst_1 : Fintype m]
  [inst_2 : Fintype n] [inst_3 : DecidableEq m] (A : Matrix m n R) (B : Matrix n p R),
  (A * B).vec = Matrix.vecMul A.vec (Matrix.kroneckerMap (fun x1 x2 ↦ x1 * x2) B 1)

theorem Matrix.dotProduct_hadamard_mulVec_eq_kronecker E1 #

∀ {m : Type u_1} {n : Type u_2} {R : Type u_3} [inst : NonUnitalSemiring R] [inst_1 : DecidableEq m]
  [inst_2 : Fintype m] [inst_3 : DecidableEq n] [inst_4 : Fintype n] (x : m → R) (A B : Matrix m n R) (x' : n → R),
  x ⬝ᵥ (A.hadamard B).mulVec x' =
    (Matrix.diagonal x).vec ⬝ᵥ (Matrix.kroneckerMap (fun x1 x2 ↦ x1 * x2) A B).mulVec (Matrix.diagonal x').vec

The Hadamard bilinear form equals the Kronecker bilinear form on diagonal embeddings.

theorem Matrix.star_dotProduct_hadamard_mulVec_eq_kronecker E1 #

∀ {m : Type u_2} {n : Type u_3} {R : Type u_1} [inst : NonUnitalSemiring R] [inst_1 : DecidableEq m]
  [inst_2 : Fintype m] [inst_3 : DecidableEq n] [inst_4 : Fintype n] [inst_5 : StarAddMonoid R] (x : m → R)
  (A B : Matrix m n R) (x' : n → R),
  star x ⬝ᵥ (A.hadamard B).mulVec x' =
    star (Matrix.diagonal x).vec ⬝ᵥ (Matrix.kroneckerMap (fun x1 x2 ↦ x1 * x2) A B).mulVec (Matrix.diagonal x').vec

The starred Hadamard bilinear form equals the starred Kronecker bilinear form on diagonal embeddings.