Mathlib.Data.Fin.VecNotation
65 declarations and 1 deprecated aliases. Mathlib docs · Source
Matrix and vector notation
This file defines notation for vectors and matrices. Given a b c d : α,
the notation allows us to write ![a, b, c, d] : Fin 4 → α.
Nesting vectors gives coefficients of a matrix, so ![![a, b], ![c, d]] : Fin 2 → Fin 2 → α.
In later files we introduce !![a, b; c, d] as notation for Matrix.of ![![a, b], ![c, d]].
Main definitions
vecEmptyis the empty vector (or0bynmatrix)![]vecConsprepends an entry to a vector, so![a, b]isvecCons a (vecCons b vecEmpty)
Implementation notes
The simp lemmas require that one of the arguments is of the form vecCons _ _.
This ensures simp works with entries only when (some) entries are already given.
In other words, this notation will only appear in the output of simp if it
already appears in the input.
Notation
The main new notation is ![a, b], which gets expanded to vecCons a (vecCons b vecEmpty).
Examples
Examples of usage can be found in the MathlibTest/matrix.lean file.
bit0 and bit1 indices
The following definitions and simp lemmas are used to allow
numeral-indexed element of a vector given with matrix notation to
be extracted by simp in Lean 3 (even when the numeral is larger than the
number of elements in the vector, which is taken modulo that number
of elements by virtue of the semantics of bit0 and bit1 and of
addition on Fin n).
Interaction between cons and Equiv.swap
theorem Matrix.vecAlt1.congr_simp E1 #
∀ {α : Type u} {m n : ℕ} (hm : m = n + n) (v v_1 : Fin m → α),
v = v_1 → ∀ (k k_1 : Fin n), k = k_1 → Matrix.vecAlt1 hm v k = Matrix.vecAlt1 hm v_1 k_1
theorem Matrix.vecAppend.congr_simp E1 #
∀ {m n : ℕ} {α : Type u_1} {o : ℕ} (ho : o = m + n) (u u_1 : Fin m → α),
u = u_1 →
∀ (v v_1 : Fin n → α),
v = v_1 → ∀ (a a_1 : Fin o), a = a_1 → Matrix.vecAppend ho u v a = Matrix.vecAppend ho u_1 v_1 a_1
theorem Matrix.vecAlt0.congr_simp E1 #
∀ {α : Type u} {m n : ℕ} (hm : m = n + n) (v v_1 : Fin m → α),
v = v_1 → ∀ (k k_1 : Fin n), k = k_1 → Matrix.vecAlt0 hm v k = Matrix.vecAlt0 hm v_1 k_1
def Matrix.vecEmpty E3 #
{α : Type u} → Fin 0 → α
![] is the vector with no entries.
def Matrix.vecCons E3 #
{α : Type u} → {n : ℕ} → α → (Fin n → α) → Fin n.succ → α
vecCons h t prepends an entry h to a vector t.
The inverse functions are vecHead and vecTail.
The notation ![a, b, ...] expands to vecCons a (vecCons b ...).
def Matrix.vecNotation E1 #
Lean.ParserDescr
![...] notation is used to construct a vector Fin n → α using Matrix.vecEmpty and
Matrix.vecCons.
For instance, ![a, b, c] : Fin 3 is syntax for vecCons a (vecCons b (vecCons c vecEmpty)).
Note that this should not be used as syntax for Matrix as it generates a term with the wrong type.
The !![a, b; c, d] syntax (provided by Matrix.matrixNotation) should be used instead.
def Matrix.vecConsUnexpander E1 #
Lean.PrettyPrinter.Unexpander
Unexpander for the ![x, y, ...] notation.
def Matrix.vecEmptyUnexpander E1 #
Lean.PrettyPrinter.Unexpander
Unexpander for the ![] notation.
def Matrix.vecHead E1 #
{α : Type u} → {n : ℕ} → (Fin n.succ → α) → α
vecHead v gives the first entry of the vector v
def Matrix.vecTail E1 #
{α : Type u} → {n : ℕ} → (Fin n.succ → α) → Fin n → α
vecTail v gives a vector consisting of all entries of v except the first
instance PiFin.hasRepr E0 #
{α : Type u} → {n : ℕ} → [Repr α] → Repr (Fin n → α)
Use ![...] notation for displaying a vector Fin n → α, for example:
#eval ![1, 2] + ![3, 4] -- ![4, 6]
theorem Matrix.empty_eq E0 #
∀ {α : Type u} (v : Fin 0 → α), v = ![]
theorem Matrix.head_fin_const E0 #
∀ {α : Type u} {n : ℕ} (a : α), (Matrix.vecHead fun x ↦ a) = a
theorem Matrix.cons_val_zero E0 #
∀ {α : Type u} {m : ℕ} (x : α) (u : Fin m → α), Matrix.vecCons x u 0 = x
theorem Matrix.cons_val_zero' E1 #
∀ {α : Type u} {m : ℕ} (h : 0 < m.succ) (x : α) (u : Fin m → α), Matrix.vecCons x u ⟨0, h⟩ = x
theorem Matrix.cons_val_succ E0 #
∀ {α : Type u} {m : ℕ} (x : α) (u : Fin m → α) (i : Fin m), Matrix.vecCons x u i.succ = u i
theorem Matrix.cons_val_succ' E0 #
∀ {α : Type u} {m i : ℕ} (h : i.succ < m.succ) (x : α) (u : Fin m → α), Matrix.vecCons x u ⟨i.succ, h⟩ = u ⟨i, ⋯⟩
theorem Matrix.Fin.cons_vecEmpty E0 #
∀ {α : Type u_1} (x : α), Fin.cons x ![] = ![x]
We don't want to always simplify Fin.cons to vecCons.
But in cases that we are already mixing the declarations for dependent tuples and non-dependent
tuples, we can simplify to the non-dependent tuples.
theorem Matrix.Fin.snoc_vecEmpty E0 #
∀ {α : Type u_1} (x : α), Fin.snoc ![] x = ![x]
Simplify Fin.snoc to vecCons in this case.
theorem Matrix.Fin.cons_vecCons E0 #
∀ {n : ℕ} {α : Type u_1} (x y : α) (p : Fin n → α),
Fin.cons x (Matrix.vecCons y p) = Matrix.vecCons x (Matrix.vecCons y p)
We don't want to always simplify Fin.cons to vecCons.
But in cases that we are already mixing the declarations for dependent tuples and non-dependent
tuples, we can simplify to the non-dependent tuples.
This allows us to simplify Fin.cons 5 ![1, 3, 7] to ![5, 1, 3, 7].
theorem Matrix.Fin.snoc_vecCons E0 #
∀ {n : ℕ} {α : Type u_1} (x y : α) (p : Fin n → α), Fin.snoc (Matrix.vecCons y p) x = Matrix.vecCons y (Fin.snoc p x)
We push Fin.snoc inside vecCons. This allows us to simplify e.g.
Fin.snoc ![1, 3, 7] 5 to ![1, 3, 7, 5].
opaque Matrix.matchVecConsPrefix E1 #
Q(ℕ) → Lean.Expr → Lean.MetaM (List Lean.Expr × Q(ℕ) × Lean.Expr)
Parses a chain of Matrix.vecCons calls into elements, leaving everything else in the tail.
let ⟨xs, tailn, tail⟩ ← matchVecConsPrefix n e decomposes e : Fin n → _ in the form
vecCons x₀ <| ... <| vecCons xₙ <| tail where tail : Fin tailn → _.
def Matrix.cons_val E0 #
Lean.Meta.Simp.DSimproc
A simproc that handles terms of the form Matrix.vecCons a f i where i is a numeric literal.
In practice, this is most effective at handling ![a, b, c] i-style terms.
theorem Matrix.head_cons E0 #
∀ {α : Type u} {m : ℕ} (x : α) (u : Fin m → α), Matrix.vecHead (Matrix.vecCons x u) = x
theorem Matrix.tail_cons E0 #
∀ {α : Type u} {m : ℕ} (x : α) (u : Fin m → α), Matrix.vecTail (Matrix.vecCons x u) = u
theorem Fin.tail_vecCons E0 #
∀ {α : Type u} {n : ℕ} (x : α) (t : Fin n → α), Fin.tail (Matrix.vecCons x t) = t
theorem Matrix.empty_val' E1 #
∀ {α : Type u} {n' : Type u_1} (j : n'), (fun i ↦ ![] i j) = ![]
theorem Matrix.cons_head_tail E0 #
∀ {α : Type u} {m : ℕ} (u : Fin m.succ → α), Matrix.vecCons (Matrix.vecHead u) (Matrix.vecTail u) = u
theorem Matrix.range_cons E0 #
∀ {α : Type u} {n : ℕ} (x : α) (u : Fin n → α), Set.range (Matrix.vecCons x u) = {x} ∪ Set.range u
theorem Matrix.range_empty E0 #
∀ {α : Type u} (u : Fin 0 → α), Set.range u = ∅
theorem Matrix.range_cons_empty E1 #
∀ {α : Type u} (x : α) (u : Fin 0 → α), Set.range (Matrix.vecCons x u) = {x}
theorem Matrix.range_cons_cons_empty E1 #
∀ {α : Type u} (x y : α) (u : Fin 0 → α), Set.range (Matrix.vecCons x (Matrix.vecCons y u)) = {x, y}
theorem Matrix.vecCons_const E1 #
∀ {α : Type u} {n : ℕ} (a : α), (Matrix.vecCons a fun x ↦ a) = fun x ↦ a
theorem Matrix.vec_single_eq_const E1 #
∀ {α : Type u} (a : α), ![a] = fun x ↦ a
theorem Matrix.cons_val_one E0 #
∀ {α : Type u} {m : ℕ} (x : α) (u : Fin m.succ → α), Matrix.vecCons x u 1 = u 0
![a, b, ...] 1 is equal to b.
The simplifier needs a special lemma for length ≥ 2, in addition to
cons_val_succ, because 1 : Fin 1 = 0 : Fin 1.
theorem Matrix.cons_val_two E1 #
∀ {α : Type u} {m : ℕ} (x : α) (u : Fin m.succ.succ → α), Matrix.vecCons x u 2 = Matrix.vecHead (Matrix.vecTail u)
theorem Matrix.cons_val_three E1 #
∀ {α : Type u} {m : ℕ} (x : α) (u : Fin m.succ.succ.succ → α),
Matrix.vecCons x u 3 = Matrix.vecHead (Matrix.vecTail (Matrix.vecTail u))
theorem Matrix.cons_val_four E1 #
∀ {α : Type u} {m : ℕ} (x : α) (u : Fin m.succ.succ.succ.succ → α),
Matrix.vecCons x u 4 = Matrix.vecHead (Matrix.vecTail (Matrix.vecTail (Matrix.vecTail u)))
theorem Matrix.cons_val_fin_one E0 #
∀ {α : Type u} (x : α) (u : Fin 0 → α) (i : Fin 1), Matrix.vecCons x u i = x
theorem Matrix.cons_fin_one E1 #
∀ {α : Type u} (x : α) (u : Fin 0 → α), Matrix.vecCons x u = fun x_1 ↦ x
theorem Matrix.vecCons_inj E0 #
∀ {α : Type u} {n : ℕ} {x y : α} {u v : Fin n → α}, Matrix.vecCons x u = Matrix.vecCons y v ↔ x = y ∧ u = v
def PiFin.mkLiteralQ E1 #
{u : Lean.Level} → {α : Q(Type u)} → {n : ℕ} → (Fin n → Q(«$α»)) → Q(Fin «$n» → «$α»)
mkVecLiteralQ ![x, y, z] produces the term q(![$x, $y, $z]).
instance PiFin.toExpr E0 #
{α : Type u} → [Lean.ToLevel] → [Lean.ToExpr α] → (n : ℕ) → Lean.ToExpr (Fin n → α)
def Matrix.vecAppend E1 #
{m n : ℕ} → {α : Type u_1} → {o : ℕ} → o = m + n → (Fin m → α) → (Fin n → α) → Fin o → α
vecAppend ho u v appends two vectors of lengths m and n to produce
one of length o = m + n. This is a variant of Fin.append with an additional ho argument,
which provides control of definitional equality for the vector length.
This turns out to be helpful when providing simp lemmas to reduce ![a, b, c] n, and also means
that vecAppend ho u v 0 is valid. Fin.append u v 0 is not valid in this case because there is
no Zero (Fin (m + n)) instance.
theorem Matrix.vecAppend_eq_ite E1 #
∀ {m n : ℕ} {α : Type u_1} {o : ℕ} (ho : o = m + n) (u : Fin m → α) (v : Fin n → α),
Matrix.vecAppend ho u v = fun i ↦ if h : ↑i < m then u ⟨↑i, h⟩ else v ⟨↑i - m, ⋯⟩
theorem Matrix.vecAppend_apply_zero E0 #
∀ {m n : ℕ} {α : Type u_1} {o : ℕ} (ho : o + 1 = m + 1 + n) (u : Fin (m + 1) → α) (v : Fin n → α),
Matrix.vecAppend ho u v 0 = u 0
theorem Matrix.empty_vecAppend E0 #
∀ {α : Type u} {n : ℕ} (v : Fin n → α), Matrix.vecAppend ⋯ ![] v = v
theorem Matrix.vecAppend_empty E0 #
∀ {α : Type u} {n : ℕ} (v : Fin n → α), Matrix.vecAppend ⋯ v ![] = v
theorem Matrix.cons_vecAppend E0 #
∀ {α : Type u} {m n o : ℕ} (ho : o + 1 = m + 1 + n) (x : α) (u : Fin m → α) (v : Fin n → α),
Matrix.vecAppend ho (Matrix.vecCons x u) v = Matrix.vecCons x (Matrix.vecAppend ⋯ u v)
def Matrix.vecAlt0 E1 #
{α : Type u} → {m n : ℕ} → m = n + n → (Fin m → α) → Fin n → α
vecAlt0 v gives a vector with half the length of v, with
only alternate elements (even-numbered).
def Matrix.vecAlt1 E1 #
{α : Type u} → {m n : ℕ} → m = n + n → (Fin m → α) → Fin n → α
vecAlt1 v gives a vector with half the length of v, with
only alternate elements (odd-numbered).
theorem Matrix.vecAlt0_vecAppend E1 #
∀ {α : Type u} {n : ℕ} (v : Fin n → α), Matrix.vecAlt0 ⋯ (Matrix.vecAppend ⋯ v v) = v ∘ fun n_1 ↦ n_1 + n_1
theorem Matrix.vecAlt1_vecAppend E1 #
∀ {α : Type u} {n : ℕ} (v : Fin (n + 1) → α), Matrix.vecAlt1 ⋯ (Matrix.vecAppend ⋯ v v) = v ∘ fun n_1 ↦ n_1 + n_1 + 1
theorem Matrix.vecHead_vecAlt0 E0 #
∀ {α : Type u} {m n : ℕ} (hm : m + 2 = n + 1 + (n + 1)) (v : Fin (m + 2) → α),
Matrix.vecHead (Matrix.vecAlt0 hm v) = v 0
theorem Matrix.vecHead_vecAlt1 E0 #
∀ {α : Type u} {m n : ℕ} (hm : m + 2 = n + 1 + (n + 1)) (v : Fin (m + 2) → α),
Matrix.vecHead (Matrix.vecAlt1 hm v) = v 1
theorem Matrix.cons_vec_bit0_eq_alt0 E1 #
∀ {α : Type u} {n : ℕ} (x : α) (u : Fin n → α) (i : Fin (n + 1)),
Matrix.vecCons x u (i + i) = Matrix.vecAlt0 ⋯ (Matrix.vecAppend ⋯ (Matrix.vecCons x u) (Matrix.vecCons x u)) i
theorem Matrix.cons_vec_bit1_eq_alt1 E1 #
∀ {α : Type u} {n : ℕ} (x : α) (u : Fin n → α) (i : Fin (n + 1)),
Matrix.vecCons x u (i + i + 1) = Matrix.vecAlt1 ⋯ (Matrix.vecAppend ⋯ (Matrix.vecCons x u) (Matrix.vecCons x u)) i
theorem Matrix.cons_vecAlt0 E0 #
∀ {α : Type u} {m n : ℕ} (h : m + 1 + 1 = n + 1 + (n + 1)) (x y : α) (u : Fin m → α),
Matrix.vecAlt0 h (Matrix.vecCons x (Matrix.vecCons y u)) = Matrix.vecCons x (Matrix.vecAlt0 ⋯ u)
theorem Matrix.empty_vecAlt0 E0 #
∀ (α : Type u_1) {h : 0 = 0 + 0}, Matrix.vecAlt0 h ![] = ![]
theorem Matrix.cons_vecAlt1 E0 #
∀ {α : Type u} {m n : ℕ} (h : m + 1 + 1 = n + 1 + (n + 1)) (x y : α) (u : Fin m → α),
Matrix.vecAlt1 h (Matrix.vecCons x (Matrix.vecCons y u)) = Matrix.vecCons y (Matrix.vecAlt1 ⋯ u)
theorem Matrix.empty_vecAlt1 E0 #
∀ (α : Type u_1) {h : 0 = 0 + 0}, Matrix.vecAlt1 h ![] = ![]
theorem Matrix.const_fin1_eq E0 #
∀ {α : Type u} (x : α), (fun x_1 ↦ x) = ![x]
theorem Matrix.cons_cons_comp_swap_zero_one E0 #
∀ {α : Type u} {n : ℕ} (a b : α) (x : Fin n → α),
Matrix.vecCons a (Matrix.vecCons b x) ∘ ⇑(Equiv.swap 0 1) = Matrix.vecCons b (Matrix.vecCons a x)
theorem Matrix.cons_swap E1 #
∀ {α : Type u} {n : ℕ} (a : α) (x : Fin n → α) (i j : Fin n),
Matrix.vecCons a (x ∘ ⇑(Equiv.swap i j)) = Matrix.vecCons a x ∘ ⇑(Equiv.swap i.succ j.succ)
theorem Matrix.injective_pair_iff_ne E0 #
∀ {α : Type u} {x y : α}, Function.Injective ![x, y] ↔ x ≠ y
Deprecated aliases
| Name | New name | Since |
|---|---|---|
injective_pair_iff_ne | Matrix.injective_pair_iff_ne | 2026-09-04 |