import CRTMoments import Mathlib.Data.Nat.Squarefree namespace DivisorMoments open scoped BigOperators open KKFiniteCoreDraft IncidenceMoments /-- Explicit prime subset belonging to a divisor. -/ def selectedPrimes (S : Finset ℕ) (d : ℕ) : Finset ℕ := S.filter (fun p => p ∣ d) /-- One global, integer-valued local cardinality function. -/ def localG (Ω : ℕ → Finset ℕ) (d : ℕ) : ℕ := if d = 0 then 0 else ∏ p ∈ d.primeFactors, (Ω p).card /-- Literal canonical residues modulo d for its selected local prime conditions. -/ def divisorResidues (S : Finset ℕ) (Ω : ℕ → Finset ℕ) (d : ℕ) : Finset ℕ := (Finset.range d).filter (fun r => ∀ p ∈ selectedPrimes S d, r % p ∈ Ω p) theorem period_positive (S : Finset ℕ) (hS : PrimeFamily S) : 0 < period S := CRTMoments.period_pos S hS theorem divisor_nonzero (S : Finset ℕ) (hS : PrimeFamily S) (d : ℕ) (hd : d ∣ period S) : d ≠ 0 := by intro hz subst d have he : period S = 0 := by simpa using hd exact (Nat.ne_of_gt (period_positive S hS)) he theorem period_squarefree (S : Finset ℕ) (hS : PrimeFamily S) : Squarefree (period S) := by unfold period refine Finset.squarefree_prod_of_pairwise_isCoprime ?_ (fun p hp => (hS p hp).squarefree) intro p hp q hq hne exact Nat.coprime_iff_isRelPrime.mp (CRTMoments.primes_pairwise S hS hp hq hne) theorem selectedPrimes_eq_primeFactors (S : Finset ℕ) (hS : PrimeFamily S) (d : ℕ) (hd : d ∣ period S) : selectedPrimes S d = d.primeFactors := by have hd0 := divisor_nonzero S hS d hd have hp0 : period S ≠ 0 := Nat.ne_of_gt (period_positive S hS) have hsubset : d.primeFactors ⊆ S := by simpa only [period, Nat.primeFactors_prod hS] using Nat.primeFactors_mono hd hp0 ext p constructor · intro hp obtain ⟨hpS, hpd⟩ := Finset.mem_filter.mp hp exact (Nat.mem_primeFactors_of_ne_zero hd0).mpr ⟨hS p hpS, hpd⟩ · intro hp exact Finset.mem_filter.mpr ⟨hsubset hp, (Nat.mem_primeFactors_of_ne_zero hd0).mp hp |>.2⟩ /-- Every divisor is the product of the explicit selected subset; no extra nonzero hypothesis. -/ theorem period_selectedPrimes (S : Finset ℕ) (hS : PrimeFamily S) (d : ℕ) (hd : d ∣ period S) : period (selectedPrimes S d) = d := by rw [selectedPrimes_eq_primeFactors S hS d hd] exact Nat.prod_primeFactors_of_squarefree (Squarefree.squarefree_of_dvd hd (period_squarefree S hS)) theorem divisor_representation (S : Finset ℕ) (hS : PrimeFamily S) (d : ℕ) (hd : d ∣ period S) : ∃ T : Finset ℕ, T ⊆ S ∧ period T = d := ⟨selectedPrimes S d, Finset.filter_subset _ _, period_selectedPrimes S hS d hd⟩ theorem selectedPrimes_one (S : Finset ℕ) (hS : PrimeFamily S) : selectedPrimes S 1 = ∅ := by rw [selectedPrimes_eq_primeFactors S hS 1 (one_dvd _)] exact Nat.primeFactors_one theorem empty_family_divisor (d : ℕ) (hd : d ∣ period ∅) : d = 1 := by simpa only [period, Finset.prod_empty, Nat.dvd_one] using hd theorem localG_zero (Ω : ℕ → Finset ℕ) : localG Ω 0 = 0 := by simp [localG] theorem localG_one (Ω : ℕ → Finset ℕ) : localG Ω 1 = 1 := by simp [localG] theorem localG_prime (Ω : ℕ → Finset ℕ) (p : ℕ) (hp : Nat.Prime p) : localG Ω p = (Ω p).card := by simp [localG, hp.ne_zero, hp.primeFactors] /-- Global multiplicativity for coprime arguments, including zero boundary cases. -/ theorem localG_mul (Ω : ℕ → Finset ℕ) (a b : ℕ) (hab : Nat.Coprime a b) : localG Ω (a * b) = localG Ω a * localG Ω b := by by_cases ha : a = 0 · subst a simp [localG] by_cases hb : b = 0 · subst b simp [localG] simp only [localG, ite_eq_right ha, ite_eq_right hb, ite_eq_right (mul_ne_zero ha hb), Nat.primeFactors_mul ha hb, Finset.prod_union hab.disjoint_primeFactors] theorem localG_supported (S : Finset ℕ) (hS : PrimeFamily S) (Ω : ℕ → Finset ℕ) (d : ℕ) (hd : d ∣ period S) : localG Ω d = ∏ p ∈ selectedPrimes S d, (Ω p).card := by rw [localG, ite_eq_right (divisor_nonzero S hS d hd), selectedPrimes_eq_primeFactors S hS d hd] theorem divisorResidues_eq_crt (S : Finset ℕ) (hS : PrimeFamily S) (Ω : ℕ → Finset ℕ) (d : ℕ) (hd : d ∣ period S) : divisorResidues S Ω d = CRTMoments.residues (selectedPrimes S d) Ω := by simp only [divisorResidues, CRTMoments.residues, period_selectedPrimes S hS d hd] /-- Agreement of the single global g(d) with the proved CRT residue cardinality. -/ theorem divisorResidues_card (S : Finset ℕ) (hS : PrimeFamily S) (Ω : ℕ → Finset ℕ) (hΩ : CRTMoments.Canonical S Ω) (d : ℕ) (hd : d ∣ period S) : (divisorResidues S Ω d).card = localG Ω d := by rw [divisorResidues_eq_crt S hS Ω d hd] rw [CRTMoments.residues_card (selectedPrimes S d) Ω (fun p hp => hS p (Finset.mem_filter.mp hp).1) (fun p hp a ha => hΩ p (Finset.mem_filter.mp hp).1 a ha)] exact (localG_supported S hS Ω d hd).symm /-- The real moment error for every divisor of the original period. -/ theorem real_divisor_moment_remainder (X : ℕ) (S : Finset ℕ) (hS : PrimeFamily S) (Ω : ℕ → Finset ℕ) (hΩ : CRTMoments.Canonical S Ω) (d : ℕ) (hd : d ∣ period S) : |((∑ t ∈ support X S Ω, if d ∣ t then weight X S Ω t else 0 : ℕ) : ℝ) - ((X : ℝ) / d) * (localG Ω d : ℝ)| ≤ (localG Ω d : ℝ) := by have h := CRTMoments.real_divisor_moment_remainder X S (selectedPrimes S d) Ω hS (Finset.filter_subset _ _) (fun p hp a ha => hΩ p (Finset.mem_filter.mp hp).1 a ha) rw [period_selectedPrimes S hS d hd, ← localG_supported S hS Ω d hd] at h exact h #print axioms period_positive #print axioms divisor_nonzero #print axioms period_squarefree #print axioms selectedPrimes_eq_primeFactors #print axioms period_selectedPrimes #print axioms divisor_representation #print axioms selectedPrimes_one #print axioms empty_family_divisor #print axioms localG_zero #print axioms localG_one #print axioms localG_prime #print axioms localG_mul #print axioms localG_supported #print axioms divisorResidues_eq_crt #print axioms divisorResidues_card #print axioms real_divisor_moment_remainder end DivisorMoments