Skip to content

Commit 9766d04

Browse files
committed
more update on UniPoly
1 parent 85954b7 commit 9766d04

File tree

2 files changed

+48
-28
lines changed

2 files changed

+48
-28
lines changed

ZKLib/Data/Math/Operations.lean

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ theorem mapM_single (f : α → m β) (a : α) : List.mapM f [a] = return [← f
108108
@[simp]
109109
theorem getLastI_append_single [Inhabited α] (x : α) : (l ++ [x]).getLastI = x := by
110110
simp only [List.getLastI_eq_getLast?, List.getLast?_append, List.getLast?_singleton,
111-
Option.or_some]
111+
Option.some_or]
112112

113113
variable {α : Type*} {unit : α}
114114

@@ -205,6 +205,10 @@ theorem matchSize_eq_iff_forall_eq (l₁ l₂ : List α) (unit : α) :
205205
def dropLastWhile (p : α → Bool) (l : List α) : List α :=
206206
(l.reverse.dropWhile p).reverse
207207

208+
lemma zipWith_const {α β : Type _} {f : α → β → β} {l₁ : List α} {l₂ : List β}
209+
(h₁ : l₁.length = l₂.length) (h₂ : ∀ a b, f a b = b) : l₁.zipWith f l₂ = l₂ := by
210+
induction' l₁ with hd tl ih generalizing l₂ <;> rcases l₂ <;> aesop
211+
208212
end List
209213

210214
-- abbrev isBoolean {R : Type _} [Zero R] [One R] (x : R) : Prop := x = 0 ∨ x = 1

ZKLib/Data/UniPoly/Basic.lean

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,50 @@ import ZKLib.Data.Math.Operations
1111
/-!
1212
# Univariate Polynomials with Efficient Operations
1313
14-
Note: this file was originally taken from Bolton Bailey, but has been heavily modified to fit our
15-
needs.
14+
This file is based on various similar implementations. Credits:
15+
- Bolton Bailey
16+
- ...
1617
-/
1718

18-
section Polynomial
19+
namespace Array
20+
21+
def trim {R : Type*} [DecidableEq R] (a : Array R) (y : R) : Array R :=
22+
a.popWhile (fun x => x = y)
23+
24+
theorem trim_trim {R : Type*} [DecidableEq R] (a : Array R) (y : R) :
25+
(a.trim y).trim y = a.trim y := by
26+
simp [trim]
27+
sorry
28+
29+
end Array
30+
31+
open Polynomial
1932

2033
/-- A type analogous to `Polynomial` that supports computable operations. This polynomial is
2134
represented internally as an Array of coefficients.
2235
2336
For example the Array `#[1,2,3]` represents the polynomial `1 + 2x + 3x^2`. Two arrays may represent
2437
the same polynomial via zero-padding, for example `#[1,2,3] = #[1,2,3,0,0,0,...]`.
2538
-/
26-
structure UniPoly (R : Type _) [Semiring R] where
39+
@[ext, specialize]
40+
structure UniPoly (R : Type*) [Semiring R] where
2741
mk::
2842
coeffs : Array R
29-
-- h : coeffs last is non zero
3043
deriving Inhabited, DecidableEq, Repr
3144

45+
@[ext, specialize]
46+
structure UniPoly' (R : Type*) [Semiring R] [DecidableEq R] where
47+
coeffs : Array R
48+
hTrim : coeffs.trim 0 = coeffs
49+
-- Alternatively (requires `Nontrivial R` as well)
50+
-- hTrim' : coeffs.getLastD 1 ≠ 0
51+
deriving Repr
52+
3253
namespace UniPoly
3354

34-
variable {R : Type _} [Semiring R]
55+
variable {R : Type*} [Semiring R]
56+
57+
instance [DecidableEq R] : Inhabited (UniPoly' R) := ⟨⟨#[], rfl⟩⟩
3558

3659
/-- Another way to access `coeffs` -/
3760
def toArray (p : UniPoly R) : Array R := p.coeffs
@@ -189,20 +212,15 @@ instance [BEq R] [Field R] : Mod (UniPoly R) := ⟨UniPoly.mod⟩
189212
to the left by one. -/
190213
def divX (p : UniPoly R) : UniPoly R := ⟨p.coeffs.extract 1 p.size⟩
191214

192-
theorem ext {p q : UniPoly R} (h : p.coeffs = q.coeffs) : p = q := by
193-
cases p; cases q; simp at h; rw [h]
194-
195215
@[simp] theorem zero_def : (0 : UniPoly R) = ⟨#[]⟩ := rfl
196216

197-
theorem add_comm {p q : UniPoly R} : p + q = q + p := by
217+
variable {p q r : UniPoly R}
218+
219+
theorem add_comm : p + q = q + p := by
198220
simp only [instHAdd, Add.add, add, List.zipWith_toArray, mk.injEq, Array.mk.injEq]
199221
exact List.zipWith_comm_of_comm _ (fun x y ↦ by change x + y = y + x; rw [_root_.add_comm]) _ _
200222

201-
private lemma zipWith_const {α β : Type _} {f : α → β → β} {l₁ : List α} {l₂ : List β}
202-
(h₁ : l₁.length = l₂.length) (h₂ : ∀ a b, f a b = b) : l₁.zipWith f l₂ = l₂ := by
203-
induction' l₁ with hd tl ih generalizing l₂ <;> rcases l₂ <;> aesop
204-
205-
@[simp] theorem zero_add {p : UniPoly R} : 0 + p = p := by
223+
@[simp] theorem zero_add : 0 + p = p := by
206224
simp [instHAdd, instAdd, add, List.matchSize]
207225
refine UniPoly.ext (Array.ext' ?_)
208226
simp only
@@ -212,12 +230,10 @@ private lemma zipWith_const {α β : Type _} {f : α → β → β} {l₁ : List
212230
intros i h
213231
change 0 + p.coeffs[i] = p.coeffs[i]
214232
simp)]
215-
exact zipWith_const (by simp) (by simp)
233+
exact List.zipWith_const (by simp) (by simp)
216234

217-
@[simp] theorem add_assoc (p q r : UniPoly R) : p + q + r = p + (q + r) := by
235+
theorem add_assoc : p + q + r = p + (q + r) := by
218236
simp [instHAdd, instAdd, add, List.matchSize]
219-
-- refine Array.ext' ?_
220-
-- simp [Array.toList_zipWith]
221237
sorry
222238

223239
-- TODO: define `SemiRing` structure on `UniPoly`
@@ -284,17 +300,20 @@ end Equiv
284300

285301
end UniPoly
286302

303+
namespace Lagrange
304+
287305
-- unique polynomial of degree n that has nodes at ω^i for i = 0, 1, ..., n-1
288-
def Lagrange.nodal' {F : Type} [Semiring F] (n : ℕ) (ω : F) : UniPoly F :=
289-
.mk (Array.range n |>.map (fun i => ω^i))
306+
def nodal {R : Type*} [Semiring R] (n : ℕ) (ω : R) : UniPoly R := sorry
307+
-- .mk (Array.range n |>.map (fun i => ω^i))
290308

291309
/--
292310
This function produces the polynomial which is of degree n and is equal to r i at ω^i for i = 0, 1,
293311
..., n-1.
294312
-/
295-
def Lagrange.interpolate' {F : Type} [Semiring F] (n : ℕ) (ω : F) (r : Fin n → F) : UniPoly F :=
296-
-- .mk (Array.finRange n |>.map (fun i => r i))
297-
sorry
313+
def interpolate {R : Type*} [Semiring R] (n : ℕ) (ω : R) (r : Vector R n) : UniPoly R := sorry
314+
-- .mk (Array.finRange n |>.map (fun i => r[i])) * nodal n ω
315+
316+
end Lagrange
298317

299318
section Tropical
300319
/-- This section courtesy of Junyan Xu -/
@@ -345,6 +364,3 @@ noncomputable def Equiv.UniPoly.TropicallyBoundPolynomial {R : Type} [Semiring R
345364
map_add' := by sorry
346365

347366
end Tropical
348-
349-
350-
end Polynomial

0 commit comments

Comments
 (0)