|
| 1 | +/- |
| 2 | +Copyright (c) 2026 CompPoly Contributors. All rights reserved. |
| 3 | +Released under Apache 2.0 license as described in the file LICENSE. |
| 4 | +Authors: Valerii Huhnin |
| 5 | +-/ |
| 6 | + |
| 7 | +import CompPoly.Bivariate.Basic |
| 8 | +import CompPoly.LinearAlgebra.PolynomialMatrix.Shifted |
| 9 | + |
| 10 | +/-! |
| 11 | +# Coefficient Rows for Bivariate Polynomials |
| 12 | +
|
| 13 | +Conversions between finite `Y`-coefficient rows and `CBivariate`. |
| 14 | +-/ |
| 15 | + |
| 16 | +namespace CompPoly |
| 17 | + |
| 18 | +namespace CBivariate |
| 19 | + |
| 20 | +variable {F : Type*} |
| 21 | + |
| 22 | +/-- Interpret a polynomial row `[q_0, ..., q_l]` as `∑ q_j(X) Y^j`. -/ |
| 23 | +def ofCoeffRow [Zero F] [BEq F] [LawfulBEq F] |
| 24 | + (row : PolynomialRow F) : CBivariate F := |
| 25 | + CPolynomial.ofArray row |
| 26 | + |
| 27 | +/-- Truncate a bivariate polynomial to its first `width` `Y`-coefficient rows. -/ |
| 28 | +def toCoeffRow [Zero F] (width : Nat) (Q : CBivariate F) : |
| 29 | + PolynomialRow F := |
| 30 | + (List.range width).map (fun j ↦ CPolynomial.coeff Q j) |>.toArray |
| 31 | + |
| 32 | +/-- Truncated coefficient rows have the requested width. -/ |
| 33 | +theorem toCoeffRow_size [Zero F] (width : Nat) (Q : CBivariate F) : |
| 34 | + (toCoeffRow width Q).size = width := by |
| 35 | + simp [toCoeffRow] |
| 36 | + |
| 37 | +/-- Lee-style shift array for `(1, w)` weighted degree. -/ |
| 38 | +def weightedDegreeShift (w width : Nat) : Array Nat := |
| 39 | + (List.range width).map (fun j ↦ j * w) |>.toArray |
| 40 | + |
| 41 | +/-- Coefficient-row conversion preserves finite `Y` coefficients below row width. -/ |
| 42 | +theorem coeff_ofCoeffRow_of_lt [Zero F] [BEq F] [LawfulBEq F] |
| 43 | + (row : PolynomialRow F) {i j : Nat} (hj : j < row.size) : |
| 44 | + CBivariate.coeff (ofCoeffRow row) i j = CPolynomial.coeff (row.getD j 0) i := by |
| 45 | + have _ := hj |
| 46 | + rw [ofCoeffRow, CBivariate.coeff] |
| 47 | + unfold CPolynomial.ofArray CPolynomial.coeff |
| 48 | + rw [CPolynomial.Raw.Trim.coeff_eq_coeff] |
| 49 | + |
| 50 | +/-- Coefficients past the row width vanish after row-to-bivariate conversion. -/ |
| 51 | +theorem coeff_ofCoeffRow_of_size_le [Zero F] [BEq F] [LawfulBEq F] |
| 52 | + (row : PolynomialRow F) {i j : Nat} (hj : row.size ≤ j) : |
| 53 | + CBivariate.coeff (ofCoeffRow row) i j = 0 := by |
| 54 | + rw [ofCoeffRow, CBivariate.coeff] |
| 55 | + unfold CPolynomial.ofArray CPolynomial.coeff |
| 56 | + rw [CPolynomial.Raw.Trim.coeff_eq_coeff] |
| 57 | + change CPolynomial.coeff (CPolynomial.Raw.coeff row j) i = 0 |
| 58 | + simpa [CPolynomial.Raw.coeff, hj] using CPolynomial.coeff_zero (R := F) i |
| 59 | + |
| 60 | +/-- Row shifted degree for the Lee shift matches weighted degree of the bivariate view. -/ |
| 61 | +theorem rowShiftedDegree?_eq_natWeightedDegree_ofCoeffRow |
| 62 | + [Field F] [BEq F] [LawfulBEq F] |
| 63 | + (row : PolynomialRow F) (w d : Nat) |
| 64 | + (hdeg : |
| 65 | + PolynomialMatrix.rowShiftedDegree? row (weightedDegreeShift w row.size) = some d) : |
| 66 | + CBivariate.natWeightedDegree (ofCoeffRow row) 1 w = d := by |
| 67 | + have hshift : |
| 68 | + ∀ j, j < row.size → (weightedDegreeShift w row.size).getD j 0 = j * w := by |
| 69 | + intro j hj |
| 70 | + simp [weightedDegreeShift, Array.getD_eq_getD_getElem?, hj] |
| 71 | + apply le_antisymm |
| 72 | + · rw [CBivariate.natWeightedDegree_le_iff] |
| 73 | + intro j hj |
| 74 | + have hcoeff_ne : |
| 75 | + CPolynomial.coeff (ofCoeffRow row) j ≠ 0 := by |
| 76 | + exact (CPolynomial.mem_support_iff (ofCoeffRow row) j).mp hj |
| 77 | + have hjlt : j < row.size := by |
| 78 | + by_contra hnot |
| 79 | + have hge : row.size ≤ j := Nat.le_of_not_gt hnot |
| 80 | + have hcoeff_zero : CPolynomial.coeff (ofCoeffRow row) j = 0 := by |
| 81 | + rw [CPolynomial.eq_zero_iff_coeff_zero] |
| 82 | + intro i |
| 83 | + exact coeff_ofCoeffRow_of_size_le row (i := i) hge |
| 84 | + exact hcoeff_ne hcoeff_zero |
| 85 | + have houter : |
| 86 | + CPolynomial.coeff (ofCoeffRow row) j = row.getD j 0 := by |
| 87 | + rw [ofCoeffRow] |
| 88 | + unfold CPolynomial.ofArray CPolynomial.coeff |
| 89 | + rw [CPolynomial.Raw.Trim.coeff_eq_coeff] |
| 90 | + have hrow_ne : row.getD j 0 ≠ 0 := by |
| 91 | + intro hzero |
| 92 | + exact hcoeff_ne (by simp [houter, hzero]) |
| 93 | + have hentry : |
| 94 | + PolynomialMatrix.shiftedEntryDegree? row (weightedDegreeShift w row.size) j = |
| 95 | + some ((row.getD j 0).natDegree + |
| 96 | + (weightedDegreeShift w row.size).getD j 0) := by |
| 97 | + simp [PolynomialMatrix.shiftedEntryDegree?, PolynomialMatrix.rowGet] |
| 98 | + simpa [PolynomialMatrix.rowGet] using hrow_ne |
| 99 | + have hle := |
| 100 | + PolynomialMatrix.shiftedEntryDegree?_le_of_rowShiftedDegree?_eq_some |
| 101 | + hdeg hjlt hentry |
| 102 | + simpa [houter, hshift j hjlt, Nat.mul_comm] using hle |
| 103 | + · obtain ⟨j, hj, hentry⟩ := |
| 104 | + PolynomialMatrix.exists_shiftedEntryDegree?_eq_of_rowShiftedDegree?_eq_some hdeg |
| 105 | + have houter : |
| 106 | + CPolynomial.coeff (ofCoeffRow row) j = row.getD j 0 := by |
| 107 | + rw [ofCoeffRow] |
| 108 | + unfold CPolynomial.ofArray CPolynomial.coeff |
| 109 | + rw [CPolynomial.Raw.Trim.coeff_eq_coeff] |
| 110 | + have hrow_ne : row.getD j 0 ≠ 0 := by |
| 111 | + intro hzero |
| 112 | + have hnone : |
| 113 | + PolynomialMatrix.shiftedEntryDegree? row (weightedDegreeShift w row.size) j = |
| 114 | + none := by |
| 115 | + simp [PolynomialMatrix.shiftedEntryDegree?, PolynomialMatrix.rowGet, hzero] |
| 116 | + rw [hentry] at hnone |
| 117 | + contradiction |
| 118 | + have hsupport : j ∈ CPolynomial.support (ofCoeffRow row) := by |
| 119 | + rw [CPolynomial.mem_support_iff] |
| 120 | + simpa [houter] using hrow_ne |
| 121 | + have hd : |
| 122 | + d = (row.getD j 0).natDegree + |
| 123 | + (weightedDegreeShift w row.size).getD j 0 := by |
| 124 | + have hsimp : |
| 125 | + row[j]?.getD 0 ≠ 0 ∧ |
| 126 | + d = (row[j]?.getD 0).natDegree + |
| 127 | + (weightedDegreeShift w row.size)[j]?.getD 0 := by |
| 128 | + simpa [PolynomialMatrix.shiftedEntryDegree?, PolynomialMatrix.rowGet] using hentry.symm |
| 129 | + simpa [Array.getD_eq_getD_getElem?] using hsimp.2 |
| 130 | + calc |
| 131 | + d = (row.getD j 0).natDegree + |
| 132 | + (weightedDegreeShift w row.size).getD j 0 := hd |
| 133 | + _ = 1 * (CPolynomial.coeff (ofCoeffRow row) j).natDegree + w * j := by |
| 134 | + simp [houter, hshift j hj, Nat.mul_comm] |
| 135 | + _ ≤ (CPolynomial.support (ofCoeffRow row)).sup |
| 136 | + (fun j ↦ 1 * (CPolynomial.coeff (ofCoeffRow row) j).natDegree + w * j) := by |
| 137 | + exact Finset.le_sup |
| 138 | + (s := CPolynomial.support (ofCoeffRow row)) |
| 139 | + (f := fun j ↦ 1 * (CPolynomial.coeff (ofCoeffRow row) j).natDegree + w * j) |
| 140 | + hsupport |
| 141 | + _ = CBivariate.natWeightedDegree (ofCoeffRow row) 1 w := by |
| 142 | + rfl |
| 143 | + |
| 144 | +/-- Weighted-degree bounded rows convert to weighted-degree bounded bivariate polynomials. -/ |
| 145 | +theorem natWeightedDegree_ofCoeffRow_le_of_rowShiftedDegree?_le |
| 146 | + [Field F] [BEq F] [LawfulBEq F] |
| 147 | + (row : PolynomialRow F) (w bound d : Nat) |
| 148 | + (hdeg : |
| 149 | + PolynomialMatrix.rowShiftedDegree? row (weightedDegreeShift w row.size) = some d) |
| 150 | + (hle : d ≤ bound) : |
| 151 | + CBivariate.natWeightedDegree (ofCoeffRow row) 1 w ≤ bound := by |
| 152 | + rw [rowShiftedDegree?_eq_natWeightedDegree_ofCoeffRow row w d hdeg] |
| 153 | + exact hle |
| 154 | + |
| 155 | +end CBivariate |
| 156 | + |
| 157 | +end CompPoly |
0 commit comments