@@ -8,6 +8,8 @@ import Std.Data.List.Basic
8
8
import Std.Data.List.Lemmas
9
9
import Mathlib.Init.Data.List.Basic
10
10
import Mathlib.Init.Data.List.Lemmas
11
+ import Mathlib.Data.Nat.Order.Basic
12
+ import Mathlib.Algebra.Order.Monoid.OrderDual
11
13
12
14
#align_import data.vector from "leanprover-community/lean" @"855e5b74e3a52a40552e8f067169d747d48743fd"
13
15
@@ -168,6 +170,11 @@ def removeNth (i : Fin n) : Vector α n → Vector α (n - 1)
168
170
def ofFn : ∀ {n}, (Fin n → α) → Vector α n
169
171
| 0 , _ => nil
170
172
| _ + 1 , f => cons (f 0 ) (ofFn fun i ↦ f i.succ)
173
+
174
+ /-- Create a vector from another with a provably equal length. -/
175
+ protected def congr {n m : ℕ} (h : n = m) : Vector α n → Vector α m
176
+ | ⟨x, p⟩ => ⟨x, h ▸ p⟩
177
+
171
178
#align vector.of_fn Vector.ofFn
172
179
173
180
section Accum
@@ -197,6 +204,31 @@ def mapAccumr₂ {α β σ φ : Type} (f : α → β → σ → σ × φ) :
197
204
198
205
end Accum
199
206
207
+ /-! ### Shift Primitives-/
208
+ section Shift
209
+
210
+ /-- `shiftLeftFill v i` is the vector obtained by left-shifting `v` `i` times and padding with the
211
+ `fill` argument. If `v.length < i` then this will return `replicate n fill`. -/
212
+ def shiftLeftFill (v : Vector α n) (i : ℕ) (fill : α) : Vector α n :=
213
+ Vector.congr (by simp) <|
214
+ append (drop i v) (replicate (min n i) fill)
215
+
216
+ /-- `shiftRightFill v i` is the vector obtained by right-shifting `v` `i` times and padding with the
217
+ `fill` argument. If `v.length < i` then this will return `replicate n fill`. -/
218
+ def shiftRightFill (v : Vector α n) (i : ℕ) (fill : α) : Vector α n :=
219
+ Vector.congr (by
220
+ by_cases h : i ≤ n
221
+ · have h₁ := Nat.sub_le n i
222
+ rw [min_eq_right h]
223
+ rw [min_eq_left h₁, ← add_tsub_assoc_of_le h, Nat.add_comm, add_tsub_cancel_right]
224
+ · have h₁ := le_of_not_ge h
225
+ rw [min_eq_left h₁, tsub_eq_zero_iff_le.mpr h₁, zero_min, Nat.add_zero]) <|
226
+ append (replicate (min n i) fill) (take (n - i) v)
227
+
228
+ end Shift
229
+
230
+
231
+ /-! ### Basic Theorems -/
200
232
/-- Vector is determined by the underlying list. -/
201
233
protected theorem eq {n : ℕ} : ∀ a1 a2 : Vector α n, toList a1 = toList a2 → a1 = a2
202
234
| ⟨_, _⟩, ⟨_, _⟩, rfl => rfl
0 commit comments