import Mathlib open Polynomial namespace Green28TwoBlock /-- Every coefficient is zero or one. -/ def Binary (p : ℝ[X]) : Prop := ∀ n : ℕ, p.coeff n = 0 ∨ p.coeff n = 1 /-- Every coefficient is nonnegative. -/ def Nonneg (p : ℝ[X]) : Prop := ∀ n : ℕ, 0 ≤ p.coeff n /-- Every monic nonnegative factorization has binary factors. -/ def Fair (p : ℝ[X]) : Prop := ∀ a b : ℝ[X], a.Monic → b.Monic → Nonneg a → Nonneg b → a * b = p → Binary a ∧ Binary b private theorem term_le_coeff {a b : ℝ[X]} (ha : Nonneg a) (hb : Nonneg b) (i j : ℕ) : a.coeff i * b.coeff j ≤ (a * b).coeff (i + j) := by rw [coeff_mul] exact Finset.single_le_sum (fun t _ => mul_nonneg (ha t.1) (hb t.2)) (show (i, j) ∈ Finset.HasAntidiagonal.antidiagonal (i + j) by simp) private theorem terms_le_coeff {a b : ℝ[X]} (ha : Nonneg a) (hb : Nonneg b) {t u : ℕ × ℕ} {n : ℕ} (ht : t.1 + t.2 = n) (hu : u.1 + u.2 = n) (htu : t ≠ u) : a.coeff t.1 * b.coeff t.2 + a.coeff u.1 * b.coeff u.2 ≤ (a * b).coeff n := by rw [coeff_mul] have ht' : t ∈ Finset.HasAntidiagonal.antidiagonal n := by simpa using ht have hu' : u ∈ Finset.HasAntidiagonal.antidiagonal n := by simpa using hu have h := Finset.single_le_sum (s := (Finset.HasAntidiagonal.antidiagonal n).erase u) (f := fun v => a.coeff v.1 * b.coeff v.2) (fun v _ => mul_nonneg (ha v.1) (hb v.2)) (Finset.mem_erase.mpr ⟨htu, ht'⟩) rw [← Finset.sum_erase_add _ _ hu'] simpa only [add_comm] using add_le_add_right h (a.coeff u.1 * b.coeff u.2) private theorem binary_le_one {p : ℝ[X]} (hp : Binary p) (n : ℕ) : p.coeff n ≤ 1 := by rcases hp n with h | h <;> linarith private theorem binary_right {a b : ℝ[X]} (ha : Binary a) (ha0 : a.coeff 0 = 1) (han : Nonneg a) (hbn : Nonneg b) (hab : Binary (a * b)) : Binary b := by intro n induction n using Nat.strong_induction_on with | h n ih => have edge (t : ℕ × ℕ) (ht : t ∈ Finset.HasAntidiagonal.antidiagonal n) (hne : t ≠ (0, n)) : a.coeff t.1 * b.coeff t.2 = 0 ∨ a.coeff t.1 * b.coeff t.2 = 1 := by have hsum : t.1 + t.2 = n := by simpa using ht have ht1 : t.1 ≠ 0 := by intro he apply hne exact Prod.ext he (by omega) rcases ha t.1 with hp | hp <;> rcases ih t.2 (by omega) with hq | hq <;> simp [hp, hq] by_cases he : ∃ t ∈ Finset.HasAntidiagonal.antidiagonal n, t ≠ (0, n) ∧ a.coeff t.1 * b.coeff t.2 = 1 · obtain ⟨t, ht, hne, hone⟩ := he have h := terms_le_coeff han hbn (t := t) (u := (0, n)) (by simpa using ht) (by simp) hne simp [hone, ha0] at h exact Or.inl (le_antisymm (by linarith [binary_le_one hab n]) (hbn n)) · have hsum : (a * b).coeff n = b.coeff n := by rw [coeff_mul, Finset.sum_eq_single (0, n)] · simp [ha0] · intro t ht hne rcases edge t ht hne with h | h · exact h · exact (he ⟨t, ht, hne, h⟩).elim · simp simpa [hsum] using hab n private noncomputable def low (p : ℝ[X]) (d : ℕ) : ℝ[X] := ∑ i ∈ Finset.range (d + 1), monomial i (p.coeff i) private theorem coeff_low (p : ℝ[X]) (d n : ℕ) : (low p d).coeff n = if n ≤ d then p.coeff n else 0 := by classical rw [low, finsetSum_coeff] simp [coeff_monomial, Finset.sum_ite_eq'] private theorem low_degree (p : ℝ[X]) (d : ℕ) : (low p d).natDegree ≤ d := natDegree_le_iff_coeff_eq_zero.mpr fun n hn => by simp [coeff_low, Nat.not_le_of_lt hn] private theorem gap {V W : ℝ[X]} {d L n : ℕ} (hVd : V.natDegree = d) (hn : d < n) (hnL : n < L) : (V + X ^ L * W).coeff n = 0 := by rw [coeff_add, coeff_X_pow_mul', if_neg (by omega : ¬ L ≤ n), coeff_eq_zero_of_natDegree_lt (hVd.trans_lt hn), zero_add] private theorem fair_low_factor {V W a b : ℝ[X]} {d L : ℕ} (hVd : V.natDegree = d) (hV : Binary V) (hV0 : V.coeff 0 = 1) (hW0 : W.coeff 0 = 1) (hfair : Fair V) (hd : 2 ≤ d) (hL : 2 * d ≤ L + 1) (ha : a.Monic) (hb : b.Monic) (han : Nonneg a) (hbn : Nonneg b) (hab : a * b = V + X ^ L * W) (ha0 : a.coeff 0 = 1) (hb0 : b.coeff 0 = 1) (hdegree : a.natDegree + b.natDegree = L + d) (hbin : Binary (a * b)) (hbd : b.natDegree ≤ d) : Binary a ∧ Binary b := by have hdL : d < L := by omega let U := low a d have hUd : U.natDegree ≤ d := low_degree _ _ have hU0 : U.coeff 0 = 1 := by simp [U, coeff_low, ha0] have hUne : U ≠ 0 := by intro h simp [h] at hU0 have hgap (n : ℕ) (hn : d < n) (hnL : n < L) : a.coeff n = 0 := by have h := term_le_coeff han hbn n 0 rw [hb0, mul_one, Nat.add_zero, hab, gap hVd hn hnL] at h exact le_antisymm h (han n) have hnl (n : ℕ) (hn : n < L) : (U * b).coeff n = V.coeff n := by have heq : (U * b).coeff n = (a * b).coeff n := by rw [coeff_mul, coeff_mul] refine Finset.sum_congr rfl ?_ intro t ht have hsum : t.1 + t.2 = n := by simpa using ht by_cases ht1 : t.1 ≤ d · simp [U, coeff_low, ht1] · simp [U, coeff_low, ht1, hgap t.1 (by omega) (by omega)] rw [heq, hab, coeff_add, coeff_X_pow_mul', if_neg (by omega : ¬ L ≤ n), add_zero] have hbBin : Binary b := by by_cases hsmall : U.natDegree + b.natDegree < L · have huv : U * b = V := by ext n by_cases hn : n < L · exact hnl n hn · rw [coeff_eq_zero_of_natDegree_lt (lt_of_le_of_lt natDegree_mul_le (by omega)), coeff_eq_zero_of_natDegree_lt (hVd.trans_lt (by omega))] have hVm : V.Monic := by change V.leadingCoeff = 1 rw [← coeff_natDegree] rcases hV V.natDegree with hv | hv · exact (leadingCoeff_ne_zero.mpr (show V ≠ 0 by intro h; simp [h] at hV0) (coeff_natDegree.symm.trans hv)).elim · exact hv have hUm : U.Monic := by change U.leadingCoeff = 1 rw [← leadingCoeff_mul_monic hb, huv] exact hVm.leadingCoeff have hUn : Nonneg U := by intro n by_cases hn : n ≤ d <;> simp [U, coeff_low, hn, han n] exact (hfair U b hUm hb hUn hbn huv).2 · have hne : ¬(U.natDegree = d ∧ b.natDegree = d) := by rintro ⟨hU, hB⟩ have hb1 : b.coeff d = 1 := hB ▸ hb.coeff_natDegree have hv1 : V.coeff d ≤ 1 := binary_le_one hV d have h := terms_le_coeff han hbn (t := (d, 0)) (u := (0, d)) (n := d) (by simp) (by simp) (by intro he; have := congrArg Prod.fst he; simp at this; omega) rw [hab, coeff_add, coeff_X_pow_mul', if_neg (by omega : ¬ L ≤ d)] at h simp [ha0, hb0, hb1] at h have hz : a.coeff d = 0 := le_antisymm (by linarith) (han d) have hc : U.coeff U.natDegree = 0 := by rw [hU]; simp [U, coeff_low, hz] exact leadingCoeff_ne_zero.mpr hUne (coeff_natDegree.symm.trans hc) have hsum_le : U.natDegree + b.natDegree ≤ 2 * d - 1 := by by_contra! h exact hne ⟨by omega, by omega⟩ have hLex : L = 2 * d - 1 := by omega by_cases hB : b.natDegree = d · exfalso have hU : U.natDegree = d - 1 := by omega have haL : a.natDegree = L := by omega have ha1 : a.coeff L = 1 := haL ▸ ha.coeff_natDegree have hb1 : b.coeff d = 1 := hB ▸ hb.coeff_natDegree have hc : a.coeff (d - 1) ≠ 0 := by have h := leadingCoeff_ne_zero.mpr hUne rw [← coeff_natDegree, hU, show U.coeff (d - 1) = a.coeff (d - 1) by simp [U, coeff_low]] at h exact h have h := terms_le_coeff han hbn (t := (d - 1, d)) (u := (L, 0)) (n := L) (by dsimp; omega) (by simp) (by intro he; have he' := congrArg Prod.fst he; dsimp at he'; omega) rw [hab, coeff_add, coeff_X_pow_mul', if_pos le_rfl, coeff_eq_zero_of_natDegree_lt (hVd.trans_lt hdL), Nat.sub_self, hW0, zero_add] at h simp [hb1, ha1, hb0] at h exact hc (le_antisymm (by linarith) (han _)) · have hb' : b.natDegree = d - 1 := by omega have hU : U.natDegree = d := by omega have ha' : a.coeff d ≠ 0 := by have h := leadingCoeff_ne_zero.mpr hUne rw [← coeff_natDegree, hU, show U.coeff d = a.coeff d by simp [U, coeff_low]] at h exact h intro n rcases eq_or_ne n 0 with rfl | hn0 · exact Or.inr hb0 rcases lt_trichotomy n (d - 1) with hn | rfl | hn · have h := term_le_coeff han hbn d n rw [hab, gap hVd (n := d + n) (by omega) (by omega)] at h have hz : a.coeff d * b.coeff n = 0 := le_antisymm h (mul_nonneg (han d) (hbn n)) exact Or.inl ((mul_eq_zero.mp hz).resolve_left ha') · exact Or.inr (hb' ▸ hb.coeff_natDegree) · exact Or.inl (coeff_eq_zero_of_natDegree_lt (hb'.trans_lt hn)) exact ⟨binary_right hbBin hb0 hbn han (by simpa [mul_comm] using hbin), hbBin⟩ /-- Equal-degree binary blocks preserve fairness when the shift is at least 2d − 1. -/ theorem fair_two_blocks (V W : ℝ[X]) (d L : ℕ) (hd : 2 ≤ d) (hL : 2 * d ≤ L + 1) (hVd : V.natDegree = d) (hWd : W.natDegree = d) (hV : Binary V) (hW : Binary W) (hV0 : V.coeff 0 = 1) (hW0 : W.coeff 0 = 1) (hfair : Fair V) : Fair (V + X ^ L * W) := by intro a b ha hb han hbn hab have hdL : d < L := by omega have hWne : W ≠ 0 := by intro h simp [h] at hW0 have hshift : (X ^ L * W).natDegree = L + d := by rw [natDegree_X_pow_mul L hWne, hWd, Nat.add_comm] have hdegree : a.natDegree + b.natDegree = L + d := by rw [← ha.natDegree_mul hb, hab, natDegree_add_eq_right_of_natDegree_lt, hshift] rw [hVd, hshift] omega have hF : Binary (a * b) := by intro n rw [hab, coeff_add, coeff_X_pow_mul'] by_cases h : L ≤ n · rw [if_pos h, coeff_eq_zero_of_natDegree_lt (hVd.trans_lt (by omega)), zero_add] exact hW (n - L) · simpa [h] using hV n have hz : a.coeff 0 * b.coeff 0 = 1 := by have hc : (a * b).coeff 0 = a.coeff 0 * b.coeff 0 := by simp [coeff_mul] rw [← hc, hab, coeff_add, coeff_X_pow_mul', if_neg (by omega : ¬ L ≤ 0), add_zero, hV0] have ha0le : a.coeff 0 ≤ 1 := by simpa [hb.coeff_natDegree] using (term_le_coeff han hbn 0 b.natDegree).trans (binary_le_one hF _) have hb0le : b.coeff 0 ≤ 1 := by simpa [ha.coeff_natDegree] using (term_le_coeff han hbn a.natDegree 0).trans (binary_le_one hF _) have ha0 : a.coeff 0 = 1 := by nlinarith [mul_nonneg (han 0) (sub_nonneg.mpr hb0le)] have hb0 : b.coeff 0 = 1 := by simpa [ha0] using hz by_cases hbd : b.natDegree ≤ d · exact fair_low_factor hVd hV hV0 hW0 hfair hd hL ha hb han hbn hab ha0 hb0 hdegree hF hbd · have hbL : L ≤ b.natDegree := by by_contra! hn have h := term_le_coeff han hbn 0 b.natDegree rw [ha0, hb.coeff_natDegree, one_mul, zero_add, hab, gap hVd (by omega) hn] at h norm_num at h have had : a.natDegree ≤ d := by omega have hr := fair_low_factor hVd hV hV0 hW0 hfair hd hL hb ha hbn han (by simpa [mul_comm] using hab) hb0 ha0 (by omega : b.natDegree + a.natDegree = L + d) (by simpa [mul_comm] using hF) had exact ⟨hr.2, hr.1⟩ end Green28TwoBlock