# Documentation

Mathlib.Data.Vector.Basic

# Additional theorems and definitions about the Vector type #

This file introduces the infix notation ::ᵥ for Vector.cons.

If a : α and l : Vector α n, then cons a l, is the vector of length n + 1 whose first element is a and with l as the rest of the list.

Equations
instance Vector.instInhabitedVector {n : } {α : Type u_1} [inst : ] :
Equations
• Vector.instInhabitedVector = { default := Vector.ofFn default }
theorem Vector.toList_injective {n : } {α : Type u_1} :
Function.Injective Vector.toList
theorem Vector.ext {n : } {α : Type u_1} {v : Vector α n} {w : Vector α n} :
(∀ (m : Fin n), = ) → v = w

Two v w : Vector α n are equal iff they are equal at every single index.

instance Vector.zero_subsingleton {α : Type u_1} :

The empty Vector is a Subsingleton.

Equations
@[simp]
theorem Vector.cons_val {n : } {α : Type u_1} (a : α) (v : Vector α n) :
↑(a ::ᵥ v) = a :: v
theorem Vector.eq_cons_iff {n : } {α : Type u_1} (a : α) (v : Vector α ()) (v' : Vector α n) :
v = a ::ᵥ v' = a = v'
theorem Vector.ne_cons_iff {n : } {α : Type u_1} (a : α) (v : Vector α ()) (v' : Vector α n) :
v a ::ᵥ v' a v'
theorem Vector.exists_eq_cons {n : } {α : Type u_1} (v : Vector α ()) :
a as, v = a ::ᵥ as
@[simp]
theorem Vector.toList_ofFn {α : Type u_1} {n : } (f : Fin nα) :
@[simp]
theorem Vector.mk_toList {n : } {α : Type u_1} (v : Vector α n) (h : ) :
{ val := , property := h } = v
@[simp]
theorem Vector.length_val {n : } {α : Type u_1} (v : Vector α n) :
= n
@[simp]
theorem Vector.toList_map {n : } {α : Type u_2} {β : Type u_1} (v : Vector α n) (f : αβ) :
@[simp]
theorem Vector.head_map {n : } {α : Type u_2} {β : Type u_1} (v : Vector α (n + 1)) (f : αβ) :
@[simp]
theorem Vector.tail_map {n : } {α : Type u_2} {β : Type u_1} (v : Vector α (n + 1)) (f : αβ) :
theorem Vector.get_eq_get {n : } {α : Type u_1} (v : Vector α n) (i : Fin n) :
= List.get () ((RelIso.toRelEmbedding (Fin.cast (_ : ))).toEmbedding i)
theorem Vector.nth_eq_nthLe {n : } {α : Type u_1} (v : Vector α n) (i : Fin n) :
= List.nthLe () i (_ : i < )
@[simp]
theorem Vector.get_replicate {n : } {α : Type u_1} (a : α) (i : Fin n) :
Vector.get () i = a
@[simp]
theorem Vector.get_map {n : } {α : Type u_2} {β : Type u_1} (v : Vector α n) (f : αβ) (i : Fin n) :
Vector.get () i = f ()
@[simp]
theorem Vector.get_ofFn {α : Type u_1} {n : } (f : Fin nα) (i : Fin n) :
= f i
@[simp]
theorem Vector.ofFn_get {n : } {α : Type u_1} (v : Vector α n) :
= v
def Equiv.vectorEquivFin (α : Type u_1) (n : ) :
Vector α n (Fin nα)

The natural equivalence between length-n vectors and functions from Fin n.

Equations
• One or more equations did not get rendered due to their size.
theorem Vector.get_tail {n : } {α : Type u_1} (x : Vector α n) (i : Fin (n - 1)) :
= Vector.get x { val := i + 1, isLt := (_ : i + 1 < n) }
@[simp]
theorem Vector.get_tail_succ {n : } {α : Type u_1} (v : Vector α ()) (i : Fin n) :
@[simp]
theorem Vector.tail_val {n : } {α : Type u_1} (v : Vector α ()) :
↑() =
@[simp]
theorem Vector.tail_nil {α : Type u_1} :
Vector.tail Vector.nil = Vector.nil

The tail of a nil vector is nil.

@[simp]
theorem Vector.singleton_tail {α : Type u_1} (v : Vector α 1) :
= Vector.nil

The tail of a vector made up of one element is nil.

@[simp]
theorem Vector.tail_ofFn {α : Type u_1} {n : } (f : Fin ()α) :
= Vector.ofFn fun i => f ()
@[simp]
theorem Vector.toList_empty {α : Type u_1} (v : Vector α 0) :
= []
@[simp]
theorem Vector.toList_singleton {α : Type u_1} (v : Vector α 1) :

The list that makes up a Vector made up of a single element, retrieved via toList, is equal to the list of that single element.

@[simp]
theorem Vector.empty_toList_eq_ff {n : } {α : Type u_1} (v : Vector α (n + 1)) :
theorem Vector.not_empty_toList {n : } {α : Type u_1} (v : Vector α (n + 1)) :
@[simp]
theorem Vector.map_id {α : Type u_1} {n : } (v : Vector α n) :
Vector.map id v = v

Mapping under id does not change a vector.

theorem Vector.nodup_iff_injective_get {n : } {α : Type u_1} {v : Vector α n} :
theorem Vector.head?_toList {n : } {α : Type u_1} (v : Vector α ()) :
= some ()
def Vector.reverse {n : } {α : Type u_1} (v : Vector α n) :
Vector α n

Reverse a vector.

Equations
• = { val := , property := (_ : ) }
theorem Vector.toList_reverse {n : } {α : Type u_1} {v : Vector α n} :

The List of a vector after a reverse, retrieved by toList is equal to the List.reverse after retrieving a vector's toList.

@[simp]
theorem Vector.reverse_reverse {n : } {α : Type u_1} {v : Vector α n} :
@[simp]
theorem Vector.get_zero {n : } {α : Type u_1} (v : Vector α ()) :
=
@[simp]
theorem Vector.head_ofFn {α : Type u_1} {n : } (f : Fin ()α) :
= f 0
theorem Vector.get_cons_zero {n : } {α : Type u_1} (a : α) (v : Vector α n) :
Vector.get (a ::ᵥ v) 0 = a
@[simp]
theorem Vector.get_cons_nil {α : Type u_1} {ix : Fin 1} (x : α) :
Vector.get (x ::ᵥ Vector.nil) ix = x

Accessing the nth element of a vector made up of one element x : α is x itself.

@[simp]
theorem Vector.get_cons_succ {n : } {α : Type u_1} (a : α) (v : Vector α n) (i : Fin n) :
Vector.get (a ::ᵥ v) () =
def Vector.last {n : } {α : Type u_1} (v : Vector α (n + 1)) :
α

The last element of a Vector, given that the vector is at least one element.

Equations
theorem Vector.last_def {n : } {α : Type u_1} {v : Vector α (n + 1)} :

The last element of a Vector, given that the vector is at least one element.

theorem Vector.reverse_get_zero {n : } {α : Type u_1} {v : Vector α (n + 1)} :

The last element of a vector is the head of the reverse vector.

def Vector.scanl {n : } {α : Type u_1} {β : Type u_2} (f : βαβ) (b : β) (v : Vector α n) :
Vector β (n + 1)

Construct a Vector β (n + 1) from a Vector α n by scanning f : β → α → β→ α → β→ β from the "left", that is, from 0 to Fin.last n, using b : β as the starting value.

Equations
@[simp]
theorem Vector.scanl_nil {α : Type u_2} {β : Type u_1} (f : βαβ) (b : β) :
Vector.scanl f b Vector.nil = b ::ᵥ Vector.nil

Providing an empty vector to scanl gives the starting value b : β.

@[simp]
theorem Vector.scanl_cons {n : } {α : Type u_2} {β : Type u_1} (f : βαβ) (b : β) (v : Vector α n) (x : α) :
Vector.scanl f b (x ::ᵥ v) = b ::ᵥ Vector.scanl f (f b x) v

The recursive step of scanl splits a vector x ::ᵥ v : Vector α (n + 1) into the provided starting value b : β and the recursed scanl f b x : β as the starting value.

This lemma is the cons version of scanl_get.

@[simp]
theorem Vector.scanl_val {n : } {α : Type u_1} {β : Type u_2} (f : βαβ) (b : β) {v : Vector α n} :
↑(Vector.scanl f b v) = List.scanl f b v

The underlying List of a Vector after a scanl is the List.scanl of the underlying List of the original Vector.

@[simp]
theorem Vector.toList_scanl {n : } {α : Type u_2} {β : Type u_1} (f : βαβ) (b : β) (v : Vector α n) :

The toList of a Vector after a scanl is the List.scanl of the toList of the original Vector.

@[simp]
theorem Vector.scanl_singleton {α : Type u_1} {β : Type u_2} (f : βαβ) (b : β) (v : Vector α 1) :
Vector.scanl f b v = b ::ᵥ f b () ::ᵥ Vector.nil

The recursive step of scanl splits a vector made up of a single element x ::ᵥ nil : Vector α 1 into a Vector of the provided starting value b : β and the mapped f b x : β as the last value.

@[simp]
theorem Vector.scanl_head {n : } {α : Type u_2} {β : Type u_1} (f : βαβ) (b : β) (v : Vector α n) :

The first element of scanl of a vector v : Vector α n, retrieved via head, is the starting value b : β.

@[simp]
theorem Vector.scanl_get {n : } {α : Type u_2} {β : Type u_1} (f : βαβ) (b : β) (v : Vector α n) (i : Fin n) :
Vector.get (Vector.scanl f b v) () = f (Vector.get (Vector.scanl f b v) (Fin.castSucc.toEmbedding i)) ()

For an index i : Fin n, the nth element of scanl of a vector v : Vector α n at i.succ, is equal to the application function f : β → α → β→ α → β→ β of the castSucc i element of scanl f b v and get v i.

This lemma is the get version of scanl_cons.

def Vector.mOfFn {m : Type u → Type u_1} [inst : ] {α : Type u} {n : } :
(Fin nm α) → m (Vector α n)

Monadic analog of Vector.ofFn. Given a monadic function on fin n, return a Vector α n inside the monad.

Equations
theorem Vector.mOfFn_pure {m : Type u_1 → Type u_2} [inst : ] [inst : ] {α : Type u_1} {n : } (f : Fin nα) :
(Vector.mOfFn fun i => pure (f i)) = pure ()
def Vector.mmap {m : Type u → Type u_1} [inst : ] {α : Type u_2} {β : Type u} (f : αm β) {n : } :
Vector α nm (Vector β n)

Apply a monadic function to each component of a vector, returning a vector inside the monad.

Equations
@[simp]
theorem Vector.mmap_nil {m : Type u_1 → Type u_2} [inst : ] {α : Type u_3} {β : Type u_1} (f : αm β) :
Vector.mmap f Vector.nil = pure Vector.nil
@[simp]
theorem Vector.mmap_cons {m : Type u_1 → Type u_2} [inst : ] {α : Type u_3} {β : Type u_1} (f : αm β) (a : α) {n : } (v : Vector α n) :
Vector.mmap f (a ::ᵥ v) = do let h' ← f a let t' ← pure (h' ::ᵥ t')
def Vector.inductionOn {α : Type u_1} {C : {n : } → Vector α nSort u_2} {n : } (v : Vector α n) (h_nil : C 0 Vector.nil) (h_cons : {n : } → {x : α} → {w : Vector α n} → C n wC () (x ::ᵥ w)) :
C n v

Define C v by induction on v : Vector α n.

This function has two arguments: h_nil handles the base case on C nil, and h_cons defines the inductive step using ∀ x : α, C w → C (x ::ᵥ w)∀ x : α, C w → C (x ::ᵥ w)→ C (x ::ᵥ w).

This can be used as induction v using Vector.inductionOn.

Equations
• One or more equations did not get rendered due to their size.
def Vector.inductionOn₂ {n : } {α : Type u_1} {β : Type u_2} {C : {n : } → Vector α nVector β nSort u_3} (v : Vector α n) (w : Vector β n) (nil : C 0 Vector.nil Vector.nil) (cons : {n : } → {a : α} → {b : β} → {x : Vector α n} → {y : Vector β n} → C n x yC () (a ::ᵥ x) (b ::ᵥ y)) :
C n v w

Define C v w by induction on a pair of vectors v : Vector α n and w : Vector β n.

Equations
• One or more equations did not get rendered due to their size.
def Vector.inductionOn₃ {n : } {α : Type u_1} {β : Type u_2} {γ : Type u_3} {C : {n : } → Vector α nVector β nVector γ nSort u_4} (u : Vector α n) (v : Vector β n) (w : Vector γ n) (nil : C 0 Vector.nil Vector.nil Vector.nil) (cons : {n : } → {a : α} → {b : β} → {c : γ} → {x : Vector α n} → {y : Vector β n} → {z : Vector γ n} → C n x y zC () (a ::ᵥ x) (b ::ᵥ y) (c ::ᵥ z)) :
C n u v w

Define C u v w by induction on a triplet of vectors u : Vector α n, v : Vector β n, and w : Vector γ b.

Equations
• One or more equations did not get rendered due to their size.
def Vector.toArray {n : } {α : Type u_1} :
Vector α n

Cast a vector to an array.

Equations
• = match x with | { val := xs, property := property } => cast (_ : = ) ()
def Vector.insertNth {n : } {α : Type u_1} (a : α) (i : Fin (n + 1)) (v : Vector α n) :
Vector α (n + 1)

v.insertNth a i inserts a into the vector v at position i (and shifting later components to the right).

Equations
theorem Vector.insertNth_val {n : } {α : Type u_1} {a : α} {i : Fin (n + 1)} {v : Vector α n} :
↑() = List.insertNth (i) a v
@[simp]
theorem Vector.removeNth_val {n : } {α : Type u_1} {i : Fin n} {v : Vector α n} :
↑() = List.removeNth v i
theorem Vector.removeNth_insertNth {n : } {α : Type u_1} {a : α} {v : Vector α n} {i : Fin (n + 1)} :
theorem Vector.removeNth_insertNth' {n : } {α : Type u_1} {a : α} {v : Vector α (n + 1)} {i : Fin (n + 1)} {j : Fin (n + 2)} :
Vector.removeNth (().toEmbedding i) () = Vector.insertNth a () ()
theorem Vector.insertNth_comm {n : } {α : Type u_1} (a : α) (b : α) (i : Fin (n + 1)) (j : Fin (n + 1)) (h : i j) (v : Vector α n) :
Vector.insertNth b () () = Vector.insertNth a (Fin.castSucc.toEmbedding i) ()
def Vector.set {n : } {α : Type u_1} (v : Vector α n) (i : Fin n) (a : α) :
Vector α n

set v n a replaces the nth element of v with a

Equations
@[simp]
theorem Vector.toList_set {n : } {α : Type u_1} (v : Vector α n) (i : Fin n) (a : α) :
Vector.toList (Vector.set v i a) = List.set () (i) a
@[simp]
theorem Vector.get_set_same {n : } {α : Type u_1} (v : Vector α n) (i : Fin n) (a : α) :
Vector.get (Vector.set v i a) i = a
theorem Vector.get_set_of_ne {n : } {α : Type u_1} {v : Vector α n} {i : Fin n} {j : Fin n} (h : i j) (a : α) :
theorem Vector.get_set_eq_if {n : } {α : Type u_1} {v : Vector α n} {i : Fin n} {j : Fin n} (a : α) :
Vector.get (Vector.set v i a) j = if i = j then a else
theorem Vector.sum_set {n : } {α : Type u_1} [inst : ] (v : Vector α n) (i : Fin n) (a : α) :
theorem Vector.prod_set {n : } {α : Type u_1} [inst : ] (v : Vector α n) (i : Fin n) (a : α) :
theorem Vector.sum_set' {n : } {α : Type u_1} [inst : ] (v : Vector α n) (i : Fin n) (a : α) :
theorem Vector.prod_set' {n : } {α : Type u_1} [inst : ] (v : Vector α n) (i : Fin n) (a : α) :
def Vector.traverse {n : } {F : Type u → Type u} [inst : ] {α : Type u} {β : Type u} (f : αF β) :
Vector α nF (Vector β n)

Apply an applicative function to each component of a vector.

Equations
• = match x with | { val := v, property := Hv } => cast (_ : F (Vector β ()) = F (Vector β n)) ()
@[simp]
theorem Vector.traverse_def {n : } {F : Type u → Type u} [inst : ] {α : Type u} {β : Type u} (f : αF β) (x : α) (xs : Vector α n) :
Vector.traverse f (x ::ᵥ xs) = Seq.seq (Vector.cons <\$> f x) fun x =>
theorem Vector.id_traverse {n : } {α : Type u} (x : Vector α n) :
theorem Vector.comp_traverse {n : } {F : Type u → Type u} {G : Type u → Type u} [inst : ] [inst : ] [inst : ] {α : Type u} {β : Type u} {γ : Type u} (f : βF γ) (g : αG β) (x : Vector α n) :
Vector.traverse (Functor.Comp.mk g) x =
theorem Vector.traverse_eq_map_id {n : } {α : Type u_1} {β : Type u_1} (f : αβ) (x : Vector α n) :
Vector.traverse (pure f) x = pure ()
theorem Vector.naturality {n : } {F : Type u → Type u} {G : Type u → Type u} [inst : ] [inst : ] [inst : ] [inst : ] (η : ) {α : Type u} {β : Type u} (f : αF β) (x : Vector α n) :
(fun {α} => ) (Vector β n) () = Vector.traverse ((fun {α} => ) β f) x
Equations
• Vector.instTraversableFlipTypeNatVector =
Equations
• One or more equations did not get rendered due to their size.