Zulip Chat Archive

Stream: new members

Topic: Adding vectors of reals


Marcus Rossel (May 09 2022 at 08:00):

I tried to define addition on vector ℝ n as follows:

protected def add : Π {n : ℕ}, vector ℝ n → vector ℝ n → vector ℝ n
  | 0       ⟨[], _⟩        ⟨[], _⟩        := nil
  | (m + 1) ⟨hd₁::tl₁, h₁⟩ ⟨hd₂::tl₂, h₂⟩ :=
    let v₁ : vector ℝ m := ⟨tl₁, (by simp [list.length_cons] at h₁; exact h₁)⟩ in
    let v₂ : vector ℝ m := ⟨tl₂, (by simp [list.length_cons] at h₂; exact h₂)⟩ in
    (hd₁ + hd₂) ::ᵥ (add v₁ v₂)

Proving things about this definition isn't very nice though. I always have to obtain the underlying list and then do a case distinction on n and the list:

protected theorem add_comm (v₁ v₂ : vector ℝ n) : v₁ + v₂ = v₂ + v₁ := begin
  cases n,
  obtain ⟨l₁, hl₁⟩ := v₁,
  { cases l₁ ; simp [vector.add] },
  {
    cases l₁,
    ...
  }
end

Is there a way to define add directly over vectors, instead of the lists somehow?

Kevin Buzzard (May 09 2022 at 08:11):

vector is a pretty gnarly dependent type. Why not just use the non-dependent type fin n -> \R instead, like in mathlib? Proving things about the latter definition is really easy. I agree that proving things about your definition isn't very nice, but I am suggesting that you don't bother trying to solve this problem.

Kevin Buzzard (May 09 2022 at 08:12):

See for example docs#vector3

Eric Wieser (May 09 2022 at 08:17):

Does docs#vector.zip_with exist?

Eric Wieser (May 09 2022 at 08:19):

Your add is just zip_with (+)

Eric Wieser (May 09 2022 at 08:20):

And your add_comm follows from docs#list.zip_with_comm


Last updated: Feb 28 2026 at 14:05 UTC