Context
stringlengths
227
76.5k
target
stringlengths
0
11.6k
file_name
stringlengths
21
79
start
int64
14
3.67k
end
int64
16
3.69k
/- Copyright (c) 2023 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import Mathlib.LinearAlgebra.QuadraticForm.TensorProduct import Mathlib.LinearAlgebra.CliffordAlgebra.Conjugation import Mathlib.LinearAlgebra.TensorProduct.Opposite import Mathlib.RingTheory.TensorProduct.Basic /-! # The base change of a clifford algebra In this file we show the isomorphism * `CliffordAlgebra.equivBaseChange A Q` : `CliffordAlgebra (Q.baseChange A) ≃ₐ[A] (A ⊗[R] CliffordAlgebra Q)` with forward direction `CliffordAlgebra.toBasechange A Q` and reverse direction `CliffordAlgebra.ofBasechange A Q`. This covers a more general case of the complexification of clifford algebras (as described in §2.2 of https://empg.maths.ed.ac.uk/Activities/Spin/Lecture2.pdf), where ℂ and ℝ are replaced by an `R`-algebra `A` (where `2 : R` is invertible). We show the additional results: * `CliffordAlgebra.toBasechange_ι`: the effect of base-changing pure vectors. * `CliffordAlgebra.ofBasechange_tmul_ι`: the effect of un-base-changing a tensor of a pure vectors. * `CliffordAlgebra.toBasechange_involute`: the effect of base-changing an involution. * `CliffordAlgebra.toBasechange_reverse`: the effect of base-changing a reversal. -/ variable {R A V : Type*} variable [CommRing R] [CommRing A] [AddCommGroup V] variable [Algebra R A] [Module R V] variable [Invertible (2 : R)] open scoped TensorProduct namespace CliffordAlgebra variable (A) /-- Auxiliary construction: note this is really just a heterobasic `CliffordAlgebra.map`. -/ -- `noncomputable` is a performance workaround for https://github.com/leanprover-community/mathlib4/issues/7103 noncomputable def ofBaseChangeAux (Q : QuadraticForm R V) : CliffordAlgebra Q →ₐ[R] CliffordAlgebra (Q.baseChange A) := CliffordAlgebra.lift Q <| by refine ⟨(ι (Q.baseChange A)).restrictScalars R ∘ₗ TensorProduct.mk R A V 1, fun v => ?_⟩ refine (CliffordAlgebra.ι_sq_scalar (Q.baseChange A) (1 ⊗ₜ v)).trans ?_ rw [QuadraticForm.baseChange_tmul, one_mul, ← Algebra.algebraMap_eq_smul_one, ← IsScalarTower.algebraMap_apply] @[simp] theorem ofBaseChangeAux_ι (Q : QuadraticForm R V) (v : V) : ofBaseChangeAux A Q (ι Q v) = ι (Q.baseChange A) (1 ⊗ₜ v) := CliffordAlgebra.lift_ι_apply _ _ v /-- Convert from the base-changed clifford algebra to the clifford algebra over a base-changed module. -/ -- `noncomputable` is a performance workaround for https://github.com/leanprover-community/mathlib4/issues/7103 noncomputable def ofBaseChange (Q : QuadraticForm R V) : A ⊗[R] CliffordAlgebra Q →ₐ[A] CliffordAlgebra (Q.baseChange A) := Algebra.TensorProduct.lift (Algebra.ofId _ _) (ofBaseChangeAux A Q) fun _a _x => Algebra.commutes _ _ @[simp] theorem ofBaseChange_tmul_ι (Q : QuadraticForm R V) (z : A) (v : V) : ofBaseChange A Q (z ⊗ₜ ι Q v) = ι (Q.baseChange A) (z ⊗ₜ v) := by show algebraMap _ _ z * ofBaseChangeAux A Q (ι Q v) = ι (Q.baseChange A) (z ⊗ₜ[R] v) rw [ofBaseChangeAux_ι, ← Algebra.smul_def, ← map_smul, TensorProduct.smul_tmul', smul_eq_mul, mul_one] @[simp] theorem ofBaseChange_tmul_one (Q : QuadraticForm R V) (z : A) : ofBaseChange A Q (z ⊗ₜ 1) = algebraMap _ _ z := by show algebraMap _ _ z * ofBaseChangeAux A Q 1 = _ rw [map_one, mul_one] /-- Convert from the clifford algebra over a base-changed module to the base-changed clifford algebra. -/ -- `noncomputable` is a performance workaround for https://github.com/leanprover-community/mathlib4/issues/7103 noncomputable def toBaseChange (Q : QuadraticForm R V) : CliffordAlgebra (Q.baseChange A) →ₐ[A] A ⊗[R] CliffordAlgebra Q := CliffordAlgebra.lift _ <| by refine ⟨TensorProduct.AlgebraTensorModule.map (LinearMap.id : A →ₗ[A] A) (ι Q), ?_⟩ letI : Invertible (2 : A) := (Invertible.map (algebraMap R A) 2).copy 2 (map_ofNat _ _).symm letI : Invertible (2 : A ⊗[R] CliffordAlgebra Q) := (Invertible.map (algebraMap R _) 2).copy 2 (map_ofNat _ _).symm suffices hpure_tensor : ∀ v w, (1 * 1) ⊗ₜ[R] (ι Q v * ι Q w) + (1 * 1) ⊗ₜ[R] (ι Q w * ι Q v) = QuadraticMap.polarBilin (Q.baseChange A) (1 ⊗ₜ[R] v) (1 ⊗ₜ[R] w) ⊗ₜ[R] 1 by -- the crux is that by converting to a statement about linear maps instead of quadratic forms, -- we then have access to all the partially-applied `ext` lemmas. rw [CliffordAlgebra.forall_mul_self_eq_iff (isUnit_of_invertible _)] refine TensorProduct.AlgebraTensorModule.curry_injective ?_ ext v w dsimp exact hpure_tensor v w intros v w rw [← TensorProduct.tmul_add, CliffordAlgebra.ι_mul_ι_add_swap, QuadraticForm.polarBilin_baseChange, LinearMap.BilinForm.baseChange_tmul, one_mul, TensorProduct.smul_tmul, Algebra.algebraMap_eq_smul_one, QuadraticMap.polarBilin_apply_apply] @[simp] theorem toBaseChange_ι (Q : QuadraticForm R V) (z : A) (v : V) : toBaseChange A Q (ι (Q.baseChange A) (z ⊗ₜ v)) = z ⊗ₜ ι Q v := CliffordAlgebra.lift_ι_apply _ _ _ theorem toBaseChange_comp_involute (Q : QuadraticForm R V) : (toBaseChange A Q).comp (involute : CliffordAlgebra (Q.baseChange A) →ₐ[A] _) = (Algebra.TensorProduct.map (AlgHom.id _ _) involute).comp (toBaseChange A Q) := by ext v show toBaseChange A Q (involute (ι (Q.baseChange A) (1 ⊗ₜ[R] v))) = (Algebra.TensorProduct.map (AlgHom.id _ _) involute : A ⊗[R] CliffordAlgebra Q →ₐ[A] _) (toBaseChange A Q (ι (Q.baseChange A) (1 ⊗ₜ[R] v))) rw [toBaseChange_ι, involute_ι, map_neg (toBaseChange A Q), toBaseChange_ι, Algebra.TensorProduct.map_tmul, AlgHom.id_apply, involute_ι, TensorProduct.tmul_neg] /-- The involution acts only on the right of the tensor product. -/ theorem toBaseChange_involute (Q : QuadraticForm R V) (x : CliffordAlgebra (Q.baseChange A)) : toBaseChange A Q (involute x) = TensorProduct.map LinearMap.id (involute.toLinearMap) (toBaseChange A Q x) := DFunLike.congr_fun (toBaseChange_comp_involute A Q) x open MulOpposite /-- Auxiliary theorem used to prove `toBaseChange_reverse` without needing induction. -/ theorem toBaseChange_comp_reverseOp (Q : QuadraticForm R V) : (toBaseChange A Q).op.comp reverseOp = ((Algebra.TensorProduct.opAlgEquiv R A A (CliffordAlgebra Q)).toAlgHom.comp <| (Algebra.TensorProduct.map (AlgEquiv.toOpposite A A).toAlgHom (reverseOp (Q := Q))).comp (toBaseChange A Q)) := by ext v show op (toBaseChange A Q (reverse (ι (Q.baseChange A) (1 ⊗ₜ[R] v)))) = Algebra.TensorProduct.opAlgEquiv R A A (CliffordAlgebra Q) (Algebra.TensorProduct.map (AlgEquiv.toOpposite A A).toAlgHom (reverseOp (Q := Q)) (toBaseChange A Q (ι (Q.baseChange A) (1 ⊗ₜ[R] v)))) rw [toBaseChange_ι, reverse_ι, toBaseChange_ι, Algebra.TensorProduct.map_tmul, Algebra.TensorProduct.opAlgEquiv_tmul, reverseOp_ι] rfl /-- `reverse` acts only on the right of the tensor product. -/
theorem toBaseChange_reverse (Q : QuadraticForm R V) (x : CliffordAlgebra (Q.baseChange A)) : toBaseChange A Q (reverse x) = TensorProduct.map LinearMap.id reverse (toBaseChange A Q x) := by have := DFunLike.congr_fun (toBaseChange_comp_reverseOp A Q) x refine (congr_arg unop this).trans ?_; clear this refine (LinearMap.congr_fun (TensorProduct.AlgebraTensorModule.map_comp _ _ _ _).symm _).trans ?_ rw [reverse, ← AlgEquiv.toLinearMap, ← AlgEquiv.toLinearEquiv_toLinearMap, AlgEquiv.toLinearEquiv_toOpposite] dsimp -- `simp` fails here due to a timeout looking for a `Subsingleton` instance!? rw [LinearEquiv.self_trans_symm] rfl
Mathlib/LinearAlgebra/CliffordAlgebra/BaseChange.lean
141
152
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro -/ import Mathlib.MeasureTheory.MeasurableSpace.MeasurablyGenerated import Mathlib.MeasureTheory.Measure.NullMeasurable import Mathlib.Order.Interval.Set.Monotone /-! # Measure spaces The definition of a measure and a measure space are in `MeasureTheory.MeasureSpaceDef`, with only a few basic properties. This file provides many more properties of these objects. This separation allows the measurability tactic to import only the file `MeasureSpaceDef`, and to be available in `MeasureSpace` (through `MeasurableSpace`). Given a measurable space `α`, a measure on `α` is a function that sends measurable sets to the extended nonnegative reals that satisfies the following conditions: 1. `μ ∅ = 0`; 2. `μ` is countably additive. This means that the measure of a countable union of pairwise disjoint sets is equal to the measure of the individual sets. Every measure can be canonically extended to an outer measure, so that it assigns values to all subsets, not just the measurable subsets. On the other hand, a measure that is countably additive on measurable sets can be restricted to measurable sets to obtain a measure. In this file a measure is defined to be an outer measure that is countably additive on measurable sets, with the additional assumption that the outer measure is the canonical extension of the restricted measure. Measures on `α` form a complete lattice, and are closed under scalar multiplication with `ℝ≥0∞`. Given a measure, the null sets are the sets where `μ s = 0`, where `μ` denotes the corresponding outer measure (so `s` might not be measurable). We can then define the completion of `μ` as the measure on the least `σ`-algebra that also contains all null sets, by defining the measure to be `0` on the null sets. ## Main statements * `completion` is the completion of a measure to all null measurable sets. * `Measure.ofMeasurable` and `OuterMeasure.toMeasure` are two important ways to define a measure. ## Implementation notes Given `μ : Measure α`, `μ s` is the value of the *outer measure* applied to `s`. This conveniently allows us to apply the measure to sets without proving that they are measurable. We get countable subadditivity for all sets, but only countable additivity for measurable sets. You often don't want to define a measure via its constructor. Two ways that are sometimes more convenient: * `Measure.ofMeasurable` is a way to define a measure by only giving its value on measurable sets and proving the properties (1) and (2) mentioned above. * `OuterMeasure.toMeasure` is a way of obtaining a measure from an outer measure by showing that all measurable sets in the measurable space are Carathéodory measurable. To prove that two measures are equal, there are multiple options: * `ext`: two measures are equal if they are equal on all measurable sets. * `ext_of_generateFrom_of_iUnion`: two measures are equal if they are equal on a π-system generating the measurable sets, if the π-system contains a spanning increasing sequence of sets where the measures take finite value (in particular the measures are σ-finite). This is a special case of the more general `ext_of_generateFrom_of_cover` * `ext_of_generate_finite`: two finite measures are equal if they are equal on a π-system generating the measurable sets. This is a special case of `ext_of_generateFrom_of_iUnion` using `C ∪ {univ}`, but is easier to work with. A `MeasureSpace` is a class that is a measurable space with a canonical measure. The measure is denoted `volume`. ## References * <https://en.wikipedia.org/wiki/Measure_(mathematics)> * <https://en.wikipedia.org/wiki/Complete_measure> * <https://en.wikipedia.org/wiki/Almost_everywhere> ## Tags measure, almost everywhere, measure space, completion, null set, null measurable set -/ noncomputable section open Set open Filter hiding map open Function MeasurableSpace Topology Filter ENNReal NNReal Interval MeasureTheory open scoped symmDiff variable {α β γ δ ι R R' : Type*} namespace MeasureTheory section variable {m : MeasurableSpace α} {μ μ₁ μ₂ : Measure α} {s s₁ s₂ t : Set α} instance ae_isMeasurablyGenerated : IsMeasurablyGenerated (ae μ) := ⟨fun _s hs => let ⟨t, hst, htm, htμ⟩ := exists_measurable_superset_of_null hs ⟨tᶜ, compl_mem_ae_iff.2 htμ, htm.compl, compl_subset_comm.1 hst⟩⟩ /-- See also `MeasureTheory.ae_restrict_uIoc_iff`. -/ theorem ae_uIoc_iff [LinearOrder α] {a b : α} {P : α → Prop} : (∀ᵐ x ∂μ, x ∈ Ι a b → P x) ↔ (∀ᵐ x ∂μ, x ∈ Ioc a b → P x) ∧ ∀ᵐ x ∂μ, x ∈ Ioc b a → P x := by simp only [uIoc_eq_union, mem_union, or_imp, eventually_and] theorem measure_union (hd : Disjoint s₁ s₂) (h : MeasurableSet s₂) : μ (s₁ ∪ s₂) = μ s₁ + μ s₂ := measure_union₀ h.nullMeasurableSet hd.aedisjoint theorem measure_union' (hd : Disjoint s₁ s₂) (h : MeasurableSet s₁) : μ (s₁ ∪ s₂) = μ s₁ + μ s₂ := measure_union₀' h.nullMeasurableSet hd.aedisjoint theorem measure_inter_add_diff (s : Set α) (ht : MeasurableSet t) : μ (s ∩ t) + μ (s \ t) = μ s := measure_inter_add_diff₀ _ ht.nullMeasurableSet theorem measure_diff_add_inter (s : Set α) (ht : MeasurableSet t) : μ (s \ t) + μ (s ∩ t) = μ s := (add_comm _ _).trans (measure_inter_add_diff s ht) theorem measure_diff_eq_top (hs : μ s = ∞) (ht : μ t ≠ ∞) : μ (s \ t) = ∞ := by contrapose! hs exact ((measure_mono (subset_diff_union s t)).trans_lt ((measure_union_le _ _).trans_lt (ENNReal.add_lt_top.2 ⟨hs.lt_top, ht.lt_top⟩))).ne theorem measure_union_add_inter (s : Set α) (ht : MeasurableSet t) : μ (s ∪ t) + μ (s ∩ t) = μ s + μ t := by rw [← measure_inter_add_diff (s ∪ t) ht, Set.union_inter_cancel_right, union_diff_right, ← measure_inter_add_diff s ht] ac_rfl theorem measure_union_add_inter' (hs : MeasurableSet s) (t : Set α) : μ (s ∪ t) + μ (s ∩ t) = μ s + μ t := by rw [union_comm, inter_comm, measure_union_add_inter t hs, add_comm] lemma measure_symmDiff_eq (hs : NullMeasurableSet s μ) (ht : NullMeasurableSet t μ) : μ (s ∆ t) = μ (s \ t) + μ (t \ s) := by simpa only [symmDiff_def, sup_eq_union] using measure_union₀ (ht.diff hs) disjoint_sdiff_sdiff.aedisjoint lemma measure_symmDiff_le (s t u : Set α) : μ (s ∆ u) ≤ μ (s ∆ t) + μ (t ∆ u) := le_trans (μ.mono <| symmDiff_triangle s t u) (measure_union_le (s ∆ t) (t ∆ u)) theorem measure_symmDiff_eq_top (hs : μ s ≠ ∞) (ht : μ t = ∞) : μ (s ∆ t) = ∞ := measure_mono_top subset_union_right (measure_diff_eq_top ht hs) theorem measure_add_measure_compl (h : MeasurableSet s) : μ s + μ sᶜ = μ univ := measure_add_measure_compl₀ h.nullMeasurableSet theorem measure_biUnion₀ {s : Set β} {f : β → Set α} (hs : s.Countable) (hd : s.Pairwise (AEDisjoint μ on f)) (h : ∀ b ∈ s, NullMeasurableSet (f b) μ) : μ (⋃ b ∈ s, f b) = ∑' p : s, μ (f p) := by haveI := hs.toEncodable rw [biUnion_eq_iUnion] exact measure_iUnion₀ (hd.on_injective Subtype.coe_injective fun x => x.2) fun x => h x x.2 theorem measure_biUnion {s : Set β} {f : β → Set α} (hs : s.Countable) (hd : s.PairwiseDisjoint f) (h : ∀ b ∈ s, MeasurableSet (f b)) : μ (⋃ b ∈ s, f b) = ∑' p : s, μ (f p) := measure_biUnion₀ hs hd.aedisjoint fun b hb => (h b hb).nullMeasurableSet theorem measure_sUnion₀ {S : Set (Set α)} (hs : S.Countable) (hd : S.Pairwise (AEDisjoint μ)) (h : ∀ s ∈ S, NullMeasurableSet s μ) : μ (⋃₀ S) = ∑' s : S, μ s := by rw [sUnion_eq_biUnion, measure_biUnion₀ hs hd h] theorem measure_sUnion {S : Set (Set α)} (hs : S.Countable) (hd : S.Pairwise Disjoint) (h : ∀ s ∈ S, MeasurableSet s) : μ (⋃₀ S) = ∑' s : S, μ s := by rw [sUnion_eq_biUnion, measure_biUnion hs hd h] theorem measure_biUnion_finset₀ {s : Finset ι} {f : ι → Set α} (hd : Set.Pairwise (↑s) (AEDisjoint μ on f)) (hm : ∀ b ∈ s, NullMeasurableSet (f b) μ) : μ (⋃ b ∈ s, f b) = ∑ p ∈ s, μ (f p) := by rw [← Finset.sum_attach, Finset.attach_eq_univ, ← tsum_fintype] exact measure_biUnion₀ s.countable_toSet hd hm theorem measure_biUnion_finset {s : Finset ι} {f : ι → Set α} (hd : PairwiseDisjoint (↑s) f) (hm : ∀ b ∈ s, MeasurableSet (f b)) : μ (⋃ b ∈ s, f b) = ∑ p ∈ s, μ (f p) := measure_biUnion_finset₀ hd.aedisjoint fun b hb => (hm b hb).nullMeasurableSet /-- The measure of an a.e. disjoint union (even uncountable) of null-measurable sets is at least the sum of the measures of the sets. -/ theorem tsum_meas_le_meas_iUnion_of_disjoint₀ {ι : Type*} {_ : MeasurableSpace α} (μ : Measure α) {As : ι → Set α} (As_mble : ∀ i : ι, NullMeasurableSet (As i) μ) (As_disj : Pairwise (AEDisjoint μ on As)) : (∑' i, μ (As i)) ≤ μ (⋃ i, As i) := by rw [ENNReal.tsum_eq_iSup_sum, iSup_le_iff] intro s simp only [← measure_biUnion_finset₀ (fun _i _hi _j _hj hij => As_disj hij) fun i _ => As_mble i] gcongr exact iUnion_subset fun _ ↦ Subset.rfl /-- The measure of a disjoint union (even uncountable) of measurable sets is at least the sum of the measures of the sets. -/ theorem tsum_meas_le_meas_iUnion_of_disjoint {ι : Type*} {_ : MeasurableSpace α} (μ : Measure α) {As : ι → Set α} (As_mble : ∀ i : ι, MeasurableSet (As i)) (As_disj : Pairwise (Disjoint on As)) : (∑' i, μ (As i)) ≤ μ (⋃ i, As i) := tsum_meas_le_meas_iUnion_of_disjoint₀ μ (fun i ↦ (As_mble i).nullMeasurableSet) (fun _ _ h ↦ Disjoint.aedisjoint (As_disj h)) /-- If `s` is a countable set, then the measure of its preimage can be found as the sum of measures of the fibers `f ⁻¹' {y}`. -/ theorem tsum_measure_preimage_singleton {s : Set β} (hs : s.Countable) {f : α → β} (hf : ∀ y ∈ s, MeasurableSet (f ⁻¹' {y})) : (∑' b : s, μ (f ⁻¹' {↑b})) = μ (f ⁻¹' s) := by rw [← Set.biUnion_preimage_singleton, measure_biUnion hs (pairwiseDisjoint_fiber f s) hf] lemma measure_preimage_eq_zero_iff_of_countable {s : Set β} {f : α → β} (hs : s.Countable) : μ (f ⁻¹' s) = 0 ↔ ∀ x ∈ s, μ (f ⁻¹' {x}) = 0 := by rw [← biUnion_preimage_singleton, measure_biUnion_null_iff hs] /-- If `s` is a `Finset`, then the measure of its preimage can be found as the sum of measures of the fibers `f ⁻¹' {y}`. -/ theorem sum_measure_preimage_singleton (s : Finset β) {f : α → β} (hf : ∀ y ∈ s, MeasurableSet (f ⁻¹' {y})) : (∑ b ∈ s, μ (f ⁻¹' {b})) = μ (f ⁻¹' ↑s) := by simp only [← measure_biUnion_finset (pairwiseDisjoint_fiber f s) hf, Finset.set_biUnion_preimage_singleton] @[simp] lemma sum_measure_singleton {s : Finset α} [MeasurableSingletonClass α] : ∑ x ∈ s, μ {x} = μ s := by trans ∑ x ∈ s, μ (id ⁻¹' {x}) · simp rw [sum_measure_preimage_singleton] · simp · simp theorem measure_diff_null' (h : μ (s₁ ∩ s₂) = 0) : μ (s₁ \ s₂) = μ s₁ := measure_congr <| diff_ae_eq_self.2 h theorem measure_add_diff (hs : NullMeasurableSet s μ) (t : Set α) : μ s + μ (t \ s) = μ (s ∪ t) := by rw [← measure_union₀' hs disjoint_sdiff_right.aedisjoint, union_diff_self] theorem measure_diff' (s : Set α) (hm : NullMeasurableSet t μ) (h_fin : μ t ≠ ∞) : μ (s \ t) = μ (s ∪ t) - μ t := ENNReal.eq_sub_of_add_eq h_fin <| by rw [add_comm, measure_add_diff hm, union_comm] theorem measure_diff (h : s₂ ⊆ s₁) (h₂ : NullMeasurableSet s₂ μ) (h_fin : μ s₂ ≠ ∞) : μ (s₁ \ s₂) = μ s₁ - μ s₂ := by rw [measure_diff' _ h₂ h_fin, union_eq_self_of_subset_right h] theorem le_measure_diff : μ s₁ - μ s₂ ≤ μ (s₁ \ s₂) := tsub_le_iff_left.2 <| (measure_le_inter_add_diff μ s₁ s₂).trans <| by gcongr; apply inter_subset_right /-- If the measure of the symmetric difference of two sets is finite, then one has infinite measure if and only if the other one does. -/ theorem measure_eq_top_iff_of_symmDiff (hμst : μ (s ∆ t) ≠ ∞) : μ s = ∞ ↔ μ t = ∞ := by suffices h : ∀ u v, μ (u ∆ v) ≠ ∞ → μ u = ∞ → μ v = ∞ from ⟨h s t hμst, h t s (symmDiff_comm s t ▸ hμst)⟩ intro u v hμuv hμu by_contra! hμv apply hμuv rw [Set.symmDiff_def, eq_top_iff] calc ∞ = μ u - μ v := by rw [ENNReal.sub_eq_top_iff.2 ⟨hμu, hμv⟩] _ ≤ μ (u \ v) := le_measure_diff _ ≤ μ (u \ v ∪ v \ u) := measure_mono subset_union_left /-- If the measure of the symmetric difference of two sets is finite, then one has finite measure if and only if the other one does. -/ theorem measure_ne_top_iff_of_symmDiff (hμst : μ (s ∆ t) ≠ ∞) : μ s ≠ ∞ ↔ μ t ≠ ∞ := (measure_eq_top_iff_of_symmDiff hμst).ne theorem measure_diff_lt_of_lt_add (hs : NullMeasurableSet s μ) (hst : s ⊆ t) (hs' : μ s ≠ ∞) {ε : ℝ≥0∞} (h : μ t < μ s + ε) : μ (t \ s) < ε := by rw [measure_diff hst hs hs']; rw [add_comm] at h exact ENNReal.sub_lt_of_lt_add (measure_mono hst) h theorem measure_diff_le_iff_le_add (hs : NullMeasurableSet s μ) (hst : s ⊆ t) (hs' : μ s ≠ ∞) {ε : ℝ≥0∞} : μ (t \ s) ≤ ε ↔ μ t ≤ μ s + ε := by rw [measure_diff hst hs hs', tsub_le_iff_left] theorem measure_eq_measure_of_null_diff {s t : Set α} (hst : s ⊆ t) (h_nulldiff : μ (t \ s) = 0) : μ s = μ t := measure_congr <| EventuallyLE.antisymm (HasSubset.Subset.eventuallyLE hst) (ae_le_set.mpr h_nulldiff) theorem measure_eq_measure_of_between_null_diff {s₁ s₂ s₃ : Set α} (h12 : s₁ ⊆ s₂) (h23 : s₂ ⊆ s₃) (h_nulldiff : μ (s₃ \ s₁) = 0) : μ s₁ = μ s₂ ∧ μ s₂ = μ s₃ := by have le12 : μ s₁ ≤ μ s₂ := measure_mono h12 have le23 : μ s₂ ≤ μ s₃ := measure_mono h23 have key : μ s₃ ≤ μ s₁ := calc μ s₃ = μ (s₃ \ s₁ ∪ s₁) := by rw [diff_union_of_subset (h12.trans h23)] _ ≤ μ (s₃ \ s₁) + μ s₁ := measure_union_le _ _ _ = μ s₁ := by simp only [h_nulldiff, zero_add] exact ⟨le12.antisymm (le23.trans key), le23.antisymm (key.trans le12)⟩ theorem measure_eq_measure_smaller_of_between_null_diff {s₁ s₂ s₃ : Set α} (h12 : s₁ ⊆ s₂) (h23 : s₂ ⊆ s₃) (h_nulldiff : μ (s₃ \ s₁) = 0) : μ s₁ = μ s₂ := (measure_eq_measure_of_between_null_diff h12 h23 h_nulldiff).1 theorem measure_eq_measure_larger_of_between_null_diff {s₁ s₂ s₃ : Set α} (h12 : s₁ ⊆ s₂) (h23 : s₂ ⊆ s₃) (h_nulldiff : μ (s₃ \ s₁) = 0) : μ s₂ = μ s₃ := (measure_eq_measure_of_between_null_diff h12 h23 h_nulldiff).2 lemma measure_compl₀ (h : NullMeasurableSet s μ) (hs : μ s ≠ ∞) : μ sᶜ = μ Set.univ - μ s := by rw [← measure_add_measure_compl₀ h, ENNReal.add_sub_cancel_left hs] theorem measure_compl (h₁ : MeasurableSet s) (h_fin : μ s ≠ ∞) : μ sᶜ = μ univ - μ s := measure_compl₀ h₁.nullMeasurableSet h_fin lemma measure_inter_conull' (ht : μ (s \ t) = 0) : μ (s ∩ t) = μ s := by rw [← diff_compl, measure_diff_null']; rwa [← diff_eq] lemma measure_inter_conull (ht : μ tᶜ = 0) : μ (s ∩ t) = μ s := by rw [← diff_compl, measure_diff_null ht] @[simp] theorem union_ae_eq_left_iff_ae_subset : (s ∪ t : Set α) =ᵐ[μ] s ↔ t ≤ᵐ[μ] s := by rw [ae_le_set] refine ⟨fun h => by simpa only [union_diff_left] using (ae_eq_set.mp h).1, fun h => eventuallyLE_antisymm_iff.mpr ⟨by rwa [ae_le_set, union_diff_left], HasSubset.Subset.eventuallyLE subset_union_left⟩⟩ @[simp] theorem union_ae_eq_right_iff_ae_subset : (s ∪ t : Set α) =ᵐ[μ] t ↔ s ≤ᵐ[μ] t := by rw [union_comm, union_ae_eq_left_iff_ae_subset] theorem ae_eq_of_ae_subset_of_measure_ge (h₁ : s ≤ᵐ[μ] t) (h₂ : μ t ≤ μ s) (hsm : NullMeasurableSet s μ) (ht : μ t ≠ ∞) : s =ᵐ[μ] t := by refine eventuallyLE_antisymm_iff.mpr ⟨h₁, ae_le_set.mpr ?_⟩ replace h₂ : μ t = μ s := h₂.antisymm (measure_mono_ae h₁) replace ht : μ s ≠ ∞ := h₂ ▸ ht rw [measure_diff' t hsm ht, measure_congr (union_ae_eq_left_iff_ae_subset.mpr h₁), h₂, tsub_self] /-- If `s ⊆ t`, `μ t ≤ μ s`, `μ t ≠ ∞`, and `s` is measurable, then `s =ᵐ[μ] t`. -/ theorem ae_eq_of_subset_of_measure_ge (h₁ : s ⊆ t) (h₂ : μ t ≤ μ s) (hsm : NullMeasurableSet s μ) (ht : μ t ≠ ∞) : s =ᵐ[μ] t := ae_eq_of_ae_subset_of_measure_ge (HasSubset.Subset.eventuallyLE h₁) h₂ hsm ht theorem measure_iUnion_congr_of_subset {ι : Sort*} [Countable ι] {s : ι → Set α} {t : ι → Set α} (hsub : ∀ i, s i ⊆ t i) (h_le : ∀ i, μ (t i) ≤ μ (s i)) : μ (⋃ i, s i) = μ (⋃ i, t i) := by refine le_antisymm (by gcongr; apply hsub) ?_ rcases Classical.em (∃ i, μ (t i) = ∞) with (⟨i, hi⟩ | htop) · calc μ (⋃ i, t i) ≤ ∞ := le_top _ ≤ μ (s i) := hi ▸ h_le i _ ≤ μ (⋃ i, s i) := measure_mono <| subset_iUnion _ _ push_neg at htop set M := toMeasurable μ have H : ∀ b, (M (t b) ∩ M (⋃ b, s b) : Set α) =ᵐ[μ] M (t b) := by refine fun b => ae_eq_of_subset_of_measure_ge inter_subset_left ?_ ?_ ?_ · calc μ (M (t b)) = μ (t b) := measure_toMeasurable _ _ ≤ μ (s b) := h_le b _ ≤ μ (M (t b) ∩ M (⋃ b, s b)) := measure_mono <| subset_inter ((hsub b).trans <| subset_toMeasurable _ _) ((subset_iUnion _ _).trans <| subset_toMeasurable _ _) · measurability · rw [measure_toMeasurable] exact htop b calc μ (⋃ b, t b) ≤ μ (⋃ b, M (t b)) := measure_mono (iUnion_mono fun b => subset_toMeasurable _ _) _ = μ (⋃ b, M (t b) ∩ M (⋃ b, s b)) := measure_congr (EventuallyEq.countable_iUnion H).symm _ ≤ μ (M (⋃ b, s b)) := measure_mono (iUnion_subset fun b => inter_subset_right) _ = μ (⋃ b, s b) := measure_toMeasurable _ theorem measure_union_congr_of_subset {t₁ t₂ : Set α} (hs : s₁ ⊆ s₂) (hsμ : μ s₂ ≤ μ s₁) (ht : t₁ ⊆ t₂) (htμ : μ t₂ ≤ μ t₁) : μ (s₁ ∪ t₁) = μ (s₂ ∪ t₂) := by rw [union_eq_iUnion, union_eq_iUnion] exact measure_iUnion_congr_of_subset (Bool.forall_bool.2 ⟨ht, hs⟩) (Bool.forall_bool.2 ⟨htμ, hsμ⟩) @[simp] theorem measure_iUnion_toMeasurable {ι : Sort*} [Countable ι] (s : ι → Set α) : μ (⋃ i, toMeasurable μ (s i)) = μ (⋃ i, s i) := Eq.symm <| measure_iUnion_congr_of_subset (fun _i => subset_toMeasurable _ _) fun _i ↦ (measure_toMeasurable _).le theorem measure_biUnion_toMeasurable {I : Set β} (hc : I.Countable) (s : β → Set α) : μ (⋃ b ∈ I, toMeasurable μ (s b)) = μ (⋃ b ∈ I, s b) := by haveI := hc.toEncodable simp only [biUnion_eq_iUnion, measure_iUnion_toMeasurable] @[simp] theorem measure_toMeasurable_union : μ (toMeasurable μ s ∪ t) = μ (s ∪ t) := Eq.symm <| measure_union_congr_of_subset (subset_toMeasurable _ _) (measure_toMeasurable _).le Subset.rfl le_rfl @[simp] theorem measure_union_toMeasurable : μ (s ∪ toMeasurable μ t) = μ (s ∪ t) := Eq.symm <| measure_union_congr_of_subset Subset.rfl le_rfl (subset_toMeasurable _ _) (measure_toMeasurable _).le theorem sum_measure_le_measure_univ {s : Finset ι} {t : ι → Set α} (h : ∀ i ∈ s, NullMeasurableSet (t i) μ) (H : Set.Pairwise s (AEDisjoint μ on t)) : (∑ i ∈ s, μ (t i)) ≤ μ (univ : Set α) := by rw [← measure_biUnion_finset₀ H h] exact measure_mono (subset_univ _) theorem tsum_measure_le_measure_univ {s : ι → Set α} (hs : ∀ i, NullMeasurableSet (s i) μ) (H : Pairwise (AEDisjoint μ on s)) : ∑' i, μ (s i) ≤ μ (univ : Set α) := by rw [ENNReal.tsum_eq_iSup_sum] exact iSup_le fun s => sum_measure_le_measure_univ (fun i _hi => hs i) fun i _hi j _hj hij => H hij /-- Pigeonhole principle for measure spaces: if `∑' i, μ (s i) > μ univ`, then one of the intersections `s i ∩ s j` is not empty. -/ theorem exists_nonempty_inter_of_measure_univ_lt_tsum_measure {m : MeasurableSpace α} (μ : Measure α) {s : ι → Set α} (hs : ∀ i, NullMeasurableSet (s i) μ) (H : μ (univ : Set α) < ∑' i, μ (s i)) : ∃ i j, i ≠ j ∧ (s i ∩ s j).Nonempty := by contrapose! H apply tsum_measure_le_measure_univ hs intro i j hij exact (disjoint_iff_inter_eq_empty.mpr (H i j hij)).aedisjoint /-- Pigeonhole principle for measure spaces: if `s` is a `Finset` and `∑ i ∈ s, μ (t i) > μ univ`, then one of the intersections `t i ∩ t j` is not empty. -/ theorem exists_nonempty_inter_of_measure_univ_lt_sum_measure {m : MeasurableSpace α} (μ : Measure α) {s : Finset ι} {t : ι → Set α} (h : ∀ i ∈ s, NullMeasurableSet (t i) μ) (H : μ (univ : Set α) < ∑ i ∈ s, μ (t i)) : ∃ i ∈ s, ∃ j ∈ s, ∃ _h : i ≠ j, (t i ∩ t j).Nonempty := by contrapose! H apply sum_measure_le_measure_univ h intro i hi j hj hij exact (disjoint_iff_inter_eq_empty.mpr (H i hi j hj hij)).aedisjoint /-- If two sets `s` and `t` are included in a set `u`, and `μ s + μ t > μ u`, then `s` intersects `t`. Version assuming that `t` is measurable. -/ theorem nonempty_inter_of_measure_lt_add {m : MeasurableSpace α} (μ : Measure α) {s t u : Set α} (ht : MeasurableSet t) (h's : s ⊆ u) (h't : t ⊆ u) (h : μ u < μ s + μ t) : (s ∩ t).Nonempty := by rw [← Set.not_disjoint_iff_nonempty_inter] contrapose! h calc μ s + μ t = μ (s ∪ t) := (measure_union h ht).symm _ ≤ μ u := measure_mono (union_subset h's h't) /-- If two sets `s` and `t` are included in a set `u`, and `μ s + μ t > μ u`, then `s` intersects `t`. Version assuming that `s` is measurable. -/ theorem nonempty_inter_of_measure_lt_add' {m : MeasurableSpace α} (μ : Measure α) {s t u : Set α} (hs : MeasurableSet s) (h's : s ⊆ u) (h't : t ⊆ u) (h : μ u < μ s + μ t) : (s ∩ t).Nonempty := by rw [add_comm] at h rw [inter_comm] exact nonempty_inter_of_measure_lt_add μ hs h't h's h /-- Continuity from below: the measure of the union of a directed sequence of (not necessarily measurable) sets is the supremum of the measures. -/ theorem _root_.Directed.measure_iUnion [Countable ι] {s : ι → Set α} (hd : Directed (· ⊆ ·) s) : μ (⋃ i, s i) = ⨆ i, μ (s i) := by -- WLOG, `ι = ℕ` rcases Countable.exists_injective_nat ι with ⟨e, he⟩ generalize ht : Function.extend e s ⊥ = t replace hd : Directed (· ⊆ ·) t := ht ▸ hd.extend_bot he suffices μ (⋃ n, t n) = ⨆ n, μ (t n) by simp only [← ht, Function.apply_extend μ, ← iSup_eq_iUnion, iSup_extend_bot he, Function.comp_def, Pi.bot_apply, bot_eq_empty, measure_empty] at this exact this.trans (iSup_extend_bot he _) clear! ι -- The `≥` inequality is trivial refine le_antisymm ?_ (iSup_le fun i ↦ measure_mono <| subset_iUnion _ _) -- Choose `T n ⊇ t n` of the same measure, put `Td n = disjointed T` set T : ℕ → Set α := fun n => toMeasurable μ (t n) set Td : ℕ → Set α := disjointed T have hm : ∀ n, MeasurableSet (Td n) := .disjointed fun n ↦ measurableSet_toMeasurable _ _ calc μ (⋃ n, t n) = μ (⋃ n, Td n) := by rw [iUnion_disjointed, measure_iUnion_toMeasurable] _ ≤ ∑' n, μ (Td n) := measure_iUnion_le _ _ = ⨆ I : Finset ℕ, ∑ n ∈ I, μ (Td n) := ENNReal.tsum_eq_iSup_sum _ ≤ ⨆ n, μ (t n) := iSup_le fun I => by rcases hd.finset_le I with ⟨N, hN⟩ calc (∑ n ∈ I, μ (Td n)) = μ (⋃ n ∈ I, Td n) := (measure_biUnion_finset ((disjoint_disjointed T).set_pairwise I) fun n _ => hm n).symm _ ≤ μ (⋃ n ∈ I, T n) := measure_mono (iUnion₂_mono fun n _hn => disjointed_subset _ _) _ = μ (⋃ n ∈ I, t n) := measure_biUnion_toMeasurable I.countable_toSet _ _ ≤ μ (t N) := measure_mono (iUnion₂_subset hN) _ ≤ ⨆ n, μ (t n) := le_iSup (μ ∘ t) N /-- Continuity from below: the measure of the union of a monotone family of sets is equal to the supremum of their measures. The theorem assumes that the `atTop` filter on the index set is countably generated, so it works for a family indexed by a countable type, as well as `ℝ`. -/ theorem _root_.Monotone.measure_iUnion [Preorder ι] [IsDirected ι (· ≤ ·)] [(atTop : Filter ι).IsCountablyGenerated] {s : ι → Set α} (hs : Monotone s) : μ (⋃ i, s i) = ⨆ i, μ (s i) := by cases isEmpty_or_nonempty ι with | inl _ => simp | inr _ => rcases exists_seq_monotone_tendsto_atTop_atTop ι with ⟨x, hxm, hx⟩ rw [← hs.iUnion_comp_tendsto_atTop hx, ← Monotone.iSup_comp_tendsto_atTop _ hx] exacts [(hs.comp hxm).directed_le.measure_iUnion, fun _ _ h ↦ measure_mono (hs h)] theorem _root_.Antitone.measure_iUnion [Preorder ι] [IsDirected ι (· ≥ ·)] [(atBot : Filter ι).IsCountablyGenerated] {s : ι → Set α} (hs : Antitone s) : μ (⋃ i, s i) = ⨆ i, μ (s i) := hs.dual_left.measure_iUnion /-- Continuity from below: the measure of the union of a sequence of (not necessarily measurable) sets is the supremum of the measures of the partial unions. -/ theorem measure_iUnion_eq_iSup_accumulate [Preorder ι] [IsDirected ι (· ≤ ·)] [(atTop : Filter ι).IsCountablyGenerated] {f : ι → Set α} : μ (⋃ i, f i) = ⨆ i, μ (Accumulate f i) := by rw [← iUnion_accumulate] exact monotone_accumulate.measure_iUnion theorem measure_biUnion_eq_iSup {s : ι → Set α} {t : Set ι} (ht : t.Countable) (hd : DirectedOn ((· ⊆ ·) on s) t) : μ (⋃ i ∈ t, s i) = ⨆ i ∈ t, μ (s i) := by haveI := ht.to_subtype rw [biUnion_eq_iUnion, hd.directed_val.measure_iUnion, ← iSup_subtype''] /-- **Continuity from above**: the measure of the intersection of a directed downwards countable family of measurable sets is the infimum of the measures. -/ theorem _root_.Directed.measure_iInter [Countable ι] {s : ι → Set α} (h : ∀ i, NullMeasurableSet (s i) μ) (hd : Directed (· ⊇ ·) s) (hfin : ∃ i, μ (s i) ≠ ∞) : μ (⋂ i, s i) = ⨅ i, μ (s i) := by rcases hfin with ⟨k, hk⟩ have : ∀ t ⊆ s k, μ t ≠ ∞ := fun t ht => ne_top_of_le_ne_top hk (measure_mono ht) rw [← ENNReal.sub_sub_cancel hk (iInf_le (fun i => μ (s i)) k), ENNReal.sub_iInf, ← ENNReal.sub_sub_cancel hk (measure_mono (iInter_subset _ k)), ← measure_diff (iInter_subset _ k) (.iInter h) (this _ (iInter_subset _ k)), diff_iInter, Directed.measure_iUnion] · congr 1 refine le_antisymm (iSup_mono' fun i => ?_) (iSup_mono fun i => le_measure_diff) rcases hd i k with ⟨j, hji, hjk⟩ use j rw [← measure_diff hjk (h _) (this _ hjk)] gcongr · exact hd.mono_comp _ fun _ _ => diff_subset_diff_right /-- **Continuity from above**: the measure of the intersection of a monotone family of measurable sets indexed by a type with countably generated `atBot` filter is equal to the infimum of the measures. -/ theorem _root_.Monotone.measure_iInter [Preorder ι] [IsDirected ι (· ≥ ·)] [(atBot : Filter ι).IsCountablyGenerated] {s : ι → Set α} (hs : Monotone s) (hsm : ∀ i, NullMeasurableSet (s i) μ) (hfin : ∃ i, μ (s i) ≠ ∞) : μ (⋂ i, s i) = ⨅ i, μ (s i) := by refine le_antisymm (le_iInf fun i ↦ measure_mono <| iInter_subset _ _) ?_ have := hfin.nonempty rcases exists_seq_antitone_tendsto_atTop_atBot ι with ⟨x, hxm, hx⟩ calc ⨅ i, μ (s i) ≤ ⨅ n, μ (s (x n)) := le_iInf_comp (μ ∘ s) x _ = μ (⋂ n, s (x n)) := by refine .symm <| (hs.comp_antitone hxm).directed_ge.measure_iInter (fun n ↦ hsm _) ?_ rcases hfin with ⟨k, hk⟩ rcases (hx.eventually_le_atBot k).exists with ⟨n, hn⟩ exact ⟨n, ne_top_of_le_ne_top hk <| measure_mono <| hs hn⟩ _ ≤ μ (⋂ i, s i) := by refine measure_mono <| iInter_mono' fun i ↦ ?_ rcases (hx.eventually_le_atBot i).exists with ⟨n, hn⟩ exact ⟨n, hs hn⟩ /-- **Continuity from above**: the measure of the intersection of an antitone family of measurable sets indexed by a type with countably generated `atTop` filter is equal to the infimum of the measures. -/ theorem _root_.Antitone.measure_iInter [Preorder ι] [IsDirected ι (· ≤ ·)] [(atTop : Filter ι).IsCountablyGenerated] {s : ι → Set α} (hs : Antitone s) (hsm : ∀ i, NullMeasurableSet (s i) μ) (hfin : ∃ i, μ (s i) ≠ ∞) : μ (⋂ i, s i) = ⨅ i, μ (s i) := hs.dual_left.measure_iInter hsm hfin /-- Continuity from above: the measure of the intersection of a sequence of measurable sets is the infimum of the measures of the partial intersections. -/ theorem measure_iInter_eq_iInf_measure_iInter_le {α ι : Type*} {_ : MeasurableSpace α} {μ : Measure α} [Countable ι] [Preorder ι] [IsDirected ι (· ≤ ·)] {f : ι → Set α} (h : ∀ i, NullMeasurableSet (f i) μ) (hfin : ∃ i, μ (f i) ≠ ∞) : μ (⋂ i, f i) = ⨅ i, μ (⋂ j ≤ i, f j) := by rw [← Antitone.measure_iInter] · rw [iInter_comm] exact congrArg μ <| iInter_congr fun i ↦ (biInf_const nonempty_Ici).symm · exact fun i j h ↦ biInter_mono (Iic_subset_Iic.2 h) fun _ _ ↦ Set.Subset.rfl · exact fun i ↦ .biInter (to_countable _) fun _ _ ↦ h _ · refine hfin.imp fun k hk ↦ ne_top_of_le_ne_top hk <| measure_mono <| iInter₂_subset k ?_ rfl /-- Continuity from below: the measure of the union of an increasing sequence of (not necessarily measurable) sets is the limit of the measures. -/ theorem tendsto_measure_iUnion_atTop [Preorder ι] [IsCountablyGenerated (atTop : Filter ι)] {s : ι → Set α} (hm : Monotone s) : Tendsto (μ ∘ s) atTop (𝓝 (μ (⋃ n, s n))) := by refine .of_neBot_imp fun h ↦ ?_ have := (atTop_neBot_iff.1 h).2 rw [hm.measure_iUnion] exact tendsto_atTop_iSup fun n m hnm => measure_mono <| hm hnm theorem tendsto_measure_iUnion_atBot [Preorder ι] [IsCountablyGenerated (atBot : Filter ι)] {s : ι → Set α} (hm : Antitone s) : Tendsto (μ ∘ s) atBot (𝓝 (μ (⋃ n, s n))) := tendsto_measure_iUnion_atTop (ι := ιᵒᵈ) hm.dual_left /-- Continuity from below: the measure of the union of a sequence of (not necessarily measurable) sets is the limit of the measures of the partial unions. -/ theorem tendsto_measure_iUnion_accumulate {α ι : Type*} [Preorder ι] [IsCountablyGenerated (atTop : Filter ι)] {_ : MeasurableSpace α} {μ : Measure α} {f : ι → Set α} : Tendsto (fun i ↦ μ (Accumulate f i)) atTop (𝓝 (μ (⋃ i, f i))) := by refine .of_neBot_imp fun h ↦ ?_ have := (atTop_neBot_iff.1 h).2 rw [measure_iUnion_eq_iSup_accumulate] exact tendsto_atTop_iSup fun i j hij ↦ by gcongr /-- Continuity from above: the measure of the intersection of a decreasing sequence of measurable sets is the limit of the measures. -/ theorem tendsto_measure_iInter_atTop [Preorder ι] [IsCountablyGenerated (atTop : Filter ι)] {s : ι → Set α} (hs : ∀ i, NullMeasurableSet (s i) μ) (hm : Antitone s) (hf : ∃ i, μ (s i) ≠ ∞) : Tendsto (μ ∘ s) atTop (𝓝 (μ (⋂ n, s n))) := by refine .of_neBot_imp fun h ↦ ?_ have := (atTop_neBot_iff.1 h).2 rw [hm.measure_iInter hs hf] exact tendsto_atTop_iInf fun n m hnm => measure_mono <| hm hnm /-- Continuity from above: the measure of the intersection of an increasing sequence of measurable sets is the limit of the measures. -/ theorem tendsto_measure_iInter_atBot [Preorder ι] [IsCountablyGenerated (atBot : Filter ι)] {s : ι → Set α} (hs : ∀ i, NullMeasurableSet (s i) μ) (hm : Monotone s) (hf : ∃ i, μ (s i) ≠ ∞) : Tendsto (μ ∘ s) atBot (𝓝 (μ (⋂ n, s n))) := tendsto_measure_iInter_atTop (ι := ιᵒᵈ) hs hm.dual_left hf /-- Continuity from above: the measure of the intersection of a sequence of measurable sets such that one has finite measure is the limit of the measures of the partial intersections. -/ theorem tendsto_measure_iInter_le {α ι : Type*} {_ : MeasurableSpace α} {μ : Measure α} [Countable ι] [Preorder ι] {f : ι → Set α} (hm : ∀ i, NullMeasurableSet (f i) μ) (hf : ∃ i, μ (f i) ≠ ∞) : Tendsto (fun i ↦ μ (⋂ j ≤ i, f j)) atTop (𝓝 (μ (⋂ i, f i))) := by refine .of_neBot_imp fun hne ↦ ?_ cases atTop_neBot_iff.mp hne rw [measure_iInter_eq_iInf_measure_iInter_le hm hf] exact tendsto_atTop_iInf fun i j hij ↦ measure_mono <| biInter_subset_biInter_left fun k hki ↦ le_trans hki hij /-- Some version of continuity of a measure in the empty set using the intersection along a set of sets. -/ theorem exists_measure_iInter_lt {α ι : Type*} {_ : MeasurableSpace α} {μ : Measure α} [SemilatticeSup ι] [Countable ι] {f : ι → Set α} (hm : ∀ i, NullMeasurableSet (f i) μ) {ε : ℝ≥0∞} (hε : 0 < ε) (hfin : ∃ i, μ (f i) ≠ ∞) (hfem : ⋂ n, f n = ∅) : ∃ m, μ (⋂ n ≤ m, f n) < ε := by let F m := μ (⋂ n ≤ m, f n) have hFAnti : Antitone F := fun i j hij => measure_mono (biInter_subset_biInter_left fun k hki => le_trans hki hij) suffices Filter.Tendsto F Filter.atTop (𝓝 0) by rw [@ENNReal.tendsto_atTop_zero_iff_lt_of_antitone _ (nonempty_of_exists hfin) _ _ hFAnti] at this exact this ε hε have hzero : μ (⋂ n, f n) = 0 := by simp only [hfem, measure_empty] rw [← hzero] exact tendsto_measure_iInter_le hm hfin /-- The measure of the intersection of a decreasing sequence of measurable sets indexed by a linear order with first countable topology is the limit of the measures. -/ theorem tendsto_measure_biInter_gt {ι : Type*} [LinearOrder ι] [TopologicalSpace ι] [OrderTopology ι] [DenselyOrdered ι] [FirstCountableTopology ι] {s : ι → Set α} {a : ι} (hs : ∀ r > a, NullMeasurableSet (s r) μ) (hm : ∀ i j, a < i → i ≤ j → s i ⊆ s j) (hf : ∃ r > a, μ (s r) ≠ ∞) : Tendsto (μ ∘ s) (𝓝[Ioi a] a) (𝓝 (μ (⋂ r > a, s r))) := by have : (atBot : Filter (Ioi a)).IsCountablyGenerated := by rw [← comap_coe_Ioi_nhdsGT] infer_instance simp_rw [← map_coe_Ioi_atBot, tendsto_map'_iff, ← mem_Ioi, biInter_eq_iInter] apply tendsto_measure_iInter_atBot · rwa [Subtype.forall] · exact fun i j h ↦ hm i j i.2 h · simpa only [Subtype.exists, exists_prop] theorem measure_if {x : β} {t : Set β} {s : Set α} [Decidable (x ∈ t)] : μ (if x ∈ t then s else ∅) = indicator t (fun _ => μ s) x := by split_ifs with h <;> simp [h] end section OuterMeasure variable [ms : MeasurableSpace α] {s t : Set α} /-- Obtain a measure by giving an outer measure where all sets in the σ-algebra are Carathéodory measurable. -/ def OuterMeasure.toMeasure (m : OuterMeasure α) (h : ms ≤ m.caratheodory) : Measure α := Measure.ofMeasurable (fun s _ => m s) m.empty fun _f hf hd => m.iUnion_eq_of_caratheodory (fun i => h _ (hf i)) hd theorem le_toOuterMeasure_caratheodory (μ : Measure α) : ms ≤ μ.toOuterMeasure.caratheodory := fun _s hs _t => (measure_inter_add_diff _ hs).symm @[simp] theorem toMeasure_toOuterMeasure (m : OuterMeasure α) (h : ms ≤ m.caratheodory) : (m.toMeasure h).toOuterMeasure = m.trim := rfl @[simp] theorem toMeasure_apply (m : OuterMeasure α) (h : ms ≤ m.caratheodory) {s : Set α} (hs : MeasurableSet s) : m.toMeasure h s = m s := m.trim_eq hs theorem le_toMeasure_apply (m : OuterMeasure α) (h : ms ≤ m.caratheodory) (s : Set α) : m s ≤ m.toMeasure h s := m.le_trim s theorem toMeasure_apply₀ (m : OuterMeasure α) (h : ms ≤ m.caratheodory) {s : Set α} (hs : NullMeasurableSet s (m.toMeasure h)) : m.toMeasure h s = m s := by refine le_antisymm ?_ (le_toMeasure_apply _ _ _) rcases hs.exists_measurable_subset_ae_eq with ⟨t, hts, htm, heq⟩ calc m.toMeasure h s = m.toMeasure h t := measure_congr heq.symm _ = m t := toMeasure_apply m h htm _ ≤ m s := m.mono hts @[simp] theorem toOuterMeasure_toMeasure {μ : Measure α} : μ.toOuterMeasure.toMeasure (le_toOuterMeasure_caratheodory _) = μ := Measure.ext fun _s => μ.toOuterMeasure.trim_eq @[simp] theorem boundedBy_measure (μ : Measure α) : OuterMeasure.boundedBy μ = μ.toOuterMeasure := μ.toOuterMeasure.boundedBy_eq_self end OuterMeasure section variable {m0 : MeasurableSpace α} {mβ : MeasurableSpace β} [MeasurableSpace γ] variable {μ μ₁ μ₂ μ₃ ν ν' ν₁ ν₂ : Measure α} {s s' t : Set α} namespace Measure /-- If `u` is a superset of `t` with the same (finite) measure (both sets possibly non-measurable), then for any measurable set `s` one also has `μ (t ∩ s) = μ (u ∩ s)`. -/ theorem measure_inter_eq_of_measure_eq {s t u : Set α} (hs : MeasurableSet s) (h : μ t = μ u) (htu : t ⊆ u) (ht_ne_top : μ t ≠ ∞) : μ (t ∩ s) = μ (u ∩ s) := by rw [h] at ht_ne_top refine le_antisymm (by gcongr) ?_ have A : μ (u ∩ s) + μ (u \ s) ≤ μ (t ∩ s) + μ (u \ s) := calc μ (u ∩ s) + μ (u \ s) = μ u := measure_inter_add_diff _ hs _ = μ t := h.symm _ = μ (t ∩ s) + μ (t \ s) := (measure_inter_add_diff _ hs).symm _ ≤ μ (t ∩ s) + μ (u \ s) := by gcongr have B : μ (u \ s) ≠ ∞ := (lt_of_le_of_lt (measure_mono diff_subset) ht_ne_top.lt_top).ne exact ENNReal.le_of_add_le_add_right B A /-- The measurable superset `toMeasurable μ t` of `t` (which has the same measure as `t`) satisfies, for any measurable set `s`, the equality `μ (toMeasurable μ t ∩ s) = μ (u ∩ s)`. Here, we require that the measure of `t` is finite. The conclusion holds without this assumption when the measure is s-finite (for example when it is σ-finite), see `measure_toMeasurable_inter_of_sFinite`. -/ theorem measure_toMeasurable_inter {s t : Set α} (hs : MeasurableSet s) (ht : μ t ≠ ∞) : μ (toMeasurable μ t ∩ s) = μ (t ∩ s) := (measure_inter_eq_of_measure_eq hs (measure_toMeasurable t).symm (subset_toMeasurable μ t) ht).symm /-! ### The `ℝ≥0∞`-module of measures -/ instance instZero {_ : MeasurableSpace α} : Zero (Measure α) := ⟨{ toOuterMeasure := 0 m_iUnion := fun _f _hf _hd => tsum_zero.symm trim_le := OuterMeasure.trim_zero.le }⟩ @[simp] theorem zero_toOuterMeasure {_m : MeasurableSpace α} : (0 : Measure α).toOuterMeasure = 0 := rfl @[simp, norm_cast] theorem coe_zero {_m : MeasurableSpace α} : ⇑(0 : Measure α) = 0 := rfl @[simp] lemma _root_.MeasureTheory.OuterMeasure.toMeasure_zero [ms : MeasurableSpace α] (h : ms ≤ (0 : OuterMeasure α).caratheodory) : (0 : OuterMeasure α).toMeasure h = 0 := by ext s hs simp [hs] @[simp] lemma _root_.MeasureTheory.OuterMeasure.toMeasure_eq_zero {ms : MeasurableSpace α} {μ : OuterMeasure α} (h : ms ≤ μ.caratheodory) : μ.toMeasure h = 0 ↔ μ = 0 where mp hμ := by ext s; exact le_bot_iff.1 <| (le_toMeasure_apply _ _ _).trans_eq congr($hμ s) mpr := by rintro rfl; simp @[nontriviality] lemma apply_eq_zero_of_isEmpty [IsEmpty α] {_ : MeasurableSpace α} (μ : Measure α) (s : Set α) : μ s = 0 := by rw [eq_empty_of_isEmpty s, measure_empty] instance instSubsingleton [IsEmpty α] {m : MeasurableSpace α} : Subsingleton (Measure α) := ⟨fun μ ν => by ext1 s _; rw [apply_eq_zero_of_isEmpty, apply_eq_zero_of_isEmpty]⟩ theorem eq_zero_of_isEmpty [IsEmpty α] {_m : MeasurableSpace α} (μ : Measure α) : μ = 0 := Subsingleton.elim μ 0 instance instInhabited {_ : MeasurableSpace α} : Inhabited (Measure α) := ⟨0⟩ instance instAdd {_ : MeasurableSpace α} : Add (Measure α) := ⟨fun μ₁ μ₂ => { toOuterMeasure := μ₁.toOuterMeasure + μ₂.toOuterMeasure m_iUnion := fun s hs hd => show μ₁ (⋃ i, s i) + μ₂ (⋃ i, s i) = ∑' i, (μ₁ (s i) + μ₂ (s i)) by rw [ENNReal.tsum_add, measure_iUnion hd hs, measure_iUnion hd hs] trim_le := by rw [OuterMeasure.trim_add, μ₁.trimmed, μ₂.trimmed] }⟩ @[simp] theorem add_toOuterMeasure {_m : MeasurableSpace α} (μ₁ μ₂ : Measure α) : (μ₁ + μ₂).toOuterMeasure = μ₁.toOuterMeasure + μ₂.toOuterMeasure := rfl @[simp, norm_cast] theorem coe_add {_m : MeasurableSpace α} (μ₁ μ₂ : Measure α) : ⇑(μ₁ + μ₂) = μ₁ + μ₂ := rfl theorem add_apply {_m : MeasurableSpace α} (μ₁ μ₂ : Measure α) (s : Set α) : (μ₁ + μ₂) s = μ₁ s + μ₂ s := rfl section SMul variable [SMul R ℝ≥0∞] [IsScalarTower R ℝ≥0∞ ℝ≥0∞] variable [SMul R' ℝ≥0∞] [IsScalarTower R' ℝ≥0∞ ℝ≥0∞] instance instSMul {_ : MeasurableSpace α} : SMul R (Measure α) := ⟨fun c μ => { toOuterMeasure := c • μ.toOuterMeasure m_iUnion := fun s hs hd => by simp only [OuterMeasure.smul_apply, coe_toOuterMeasure, ENNReal.tsum_const_smul, measure_iUnion hd hs] trim_le := by rw [OuterMeasure.trim_smul, μ.trimmed] }⟩ @[simp] theorem smul_toOuterMeasure {_m : MeasurableSpace α} (c : R) (μ : Measure α) : (c • μ).toOuterMeasure = c • μ.toOuterMeasure := rfl @[simp, norm_cast] theorem coe_smul {_m : MeasurableSpace α} (c : R) (μ : Measure α) : ⇑(c • μ) = c • ⇑μ := rfl @[simp] theorem smul_apply {_m : MeasurableSpace α} (c : R) (μ : Measure α) (s : Set α) : (c • μ) s = c • μ s := rfl instance instSMulCommClass [SMulCommClass R R' ℝ≥0∞] {_ : MeasurableSpace α} : SMulCommClass R R' (Measure α) := ⟨fun _ _ _ => ext fun _ _ => smul_comm _ _ _⟩ instance instIsScalarTower [SMul R R'] [IsScalarTower R R' ℝ≥0∞] {_ : MeasurableSpace α} : IsScalarTower R R' (Measure α) := ⟨fun _ _ _ => ext fun _ _ => smul_assoc _ _ _⟩ instance instIsCentralScalar [SMul Rᵐᵒᵖ ℝ≥0∞] [IsCentralScalar R ℝ≥0∞] {_ : MeasurableSpace α} : IsCentralScalar R (Measure α) := ⟨fun _ _ => ext fun _ _ => op_smul_eq_smul _ _⟩ end SMul instance instNoZeroSMulDivisors [Zero R] [SMulWithZero R ℝ≥0∞] [IsScalarTower R ℝ≥0∞ ℝ≥0∞] [NoZeroSMulDivisors R ℝ≥0∞] : NoZeroSMulDivisors R (Measure α) where eq_zero_or_eq_zero_of_smul_eq_zero h := by simpa [Ne, ext_iff', forall_or_left] using h instance instMulAction [Monoid R] [MulAction R ℝ≥0∞] [IsScalarTower R ℝ≥0∞ ℝ≥0∞] {_ : MeasurableSpace α} : MulAction R (Measure α) := Injective.mulAction _ toOuterMeasure_injective smul_toOuterMeasure instance instAddCommMonoid {_ : MeasurableSpace α} : AddCommMonoid (Measure α) := toOuterMeasure_injective.addCommMonoid toOuterMeasure zero_toOuterMeasure add_toOuterMeasure fun _ _ => smul_toOuterMeasure _ _ /-- Coercion to function as an additive monoid homomorphism. -/ def coeAddHom {_ : MeasurableSpace α} : Measure α →+ Set α → ℝ≥0∞ where toFun := (⇑) map_zero' := coe_zero map_add' := coe_add @[simp] theorem coeAddHom_apply {_ : MeasurableSpace α} (μ : Measure α) : coeAddHom μ = ⇑μ := rfl @[simp] theorem coe_finset_sum {_m : MeasurableSpace α} (I : Finset ι) (μ : ι → Measure α) : ⇑(∑ i ∈ I, μ i) = ∑ i ∈ I, ⇑(μ i) := map_sum coeAddHom μ I theorem finset_sum_apply {m : MeasurableSpace α} (I : Finset ι) (μ : ι → Measure α) (s : Set α) : (∑ i ∈ I, μ i) s = ∑ i ∈ I, μ i s := by rw [coe_finset_sum, Finset.sum_apply] instance instDistribMulAction [Monoid R] [DistribMulAction R ℝ≥0∞] [IsScalarTower R ℝ≥0∞ ℝ≥0∞] {_ : MeasurableSpace α} : DistribMulAction R (Measure α) := Injective.distribMulAction ⟨⟨toOuterMeasure, zero_toOuterMeasure⟩, add_toOuterMeasure⟩ toOuterMeasure_injective smul_toOuterMeasure instance instModule [Semiring R] [Module R ℝ≥0∞] [IsScalarTower R ℝ≥0∞ ℝ≥0∞] {_ : MeasurableSpace α} : Module R (Measure α) := Injective.module R ⟨⟨toOuterMeasure, zero_toOuterMeasure⟩, add_toOuterMeasure⟩ toOuterMeasure_injective smul_toOuterMeasure @[simp] theorem coe_nnreal_smul_apply {_m : MeasurableSpace α} (c : ℝ≥0) (μ : Measure α) (s : Set α) : (c • μ) s = c * μ s := rfl @[simp] theorem nnreal_smul_coe_apply {_m : MeasurableSpace α} (c : ℝ≥0) (μ : Measure α) (s : Set α) : c • μ s = c * μ s := by rfl theorem ae_smul_measure {p : α → Prop} [SMul R ℝ≥0∞] [IsScalarTower R ℝ≥0∞ ℝ≥0∞] (h : ∀ᵐ x ∂μ, p x) (c : R) : ∀ᵐ x ∂c • μ, p x := ae_iff.2 <| by rw [smul_apply, ae_iff.1 h, ← smul_one_smul ℝ≥0∞, smul_zero] theorem ae_smul_measure_le [SMul R ℝ≥0∞] [IsScalarTower R ℝ≥0∞ ℝ≥0∞] (c : R) : ae (c • μ) ≤ ae μ := fun _ h ↦ ae_smul_measure h c section SMulWithZero variable {R : Type*} [Zero R] [SMulWithZero R ℝ≥0∞] [IsScalarTower R ℝ≥0∞ ℝ≥0∞] [NoZeroSMulDivisors R ℝ≥0∞] {c : R} {p : α → Prop} lemma ae_smul_measure_iff (hc : c ≠ 0) {μ : Measure α} : (∀ᵐ x ∂c • μ, p x) ↔ ∀ᵐ x ∂μ, p x := by simp [ae_iff, hc] @[simp] lemma ae_smul_measure_eq (hc : c ≠ 0) (μ : Measure α) : ae (c • μ) = ae μ := by ext; exact ae_smul_measure_iff hc end SMulWithZero theorem measure_eq_left_of_subset_of_measure_add_eq {s t : Set α} (h : (μ + ν) t ≠ ∞) (h' : s ⊆ t) (h'' : (μ + ν) s = (μ + ν) t) : μ s = μ t := by refine le_antisymm (measure_mono h') ?_ have : μ t + ν t ≤ μ s + ν t := calc μ t + ν t = μ s + ν s := h''.symm _ ≤ μ s + ν t := by gcongr apply ENNReal.le_of_add_le_add_right _ this exact ne_top_of_le_ne_top h (le_add_left le_rfl) theorem measure_eq_right_of_subset_of_measure_add_eq {s t : Set α} (h : (μ + ν) t ≠ ∞) (h' : s ⊆ t) (h'' : (μ + ν) s = (μ + ν) t) : ν s = ν t := by rw [add_comm] at h'' h exact measure_eq_left_of_subset_of_measure_add_eq h h' h'' theorem measure_toMeasurable_add_inter_left {s t : Set α} (hs : MeasurableSet s) (ht : (μ + ν) t ≠ ∞) : μ (toMeasurable (μ + ν) t ∩ s) = μ (t ∩ s) := by refine (measure_inter_eq_of_measure_eq hs ?_ (subset_toMeasurable _ _) ?_).symm · refine measure_eq_left_of_subset_of_measure_add_eq ?_ (subset_toMeasurable _ _) (measure_toMeasurable t).symm rwa [measure_toMeasurable t] · simp only [not_or, ENNReal.add_eq_top, Pi.add_apply, Ne, coe_add] at ht exact ht.1 theorem measure_toMeasurable_add_inter_right {s t : Set α} (hs : MeasurableSet s) (ht : (μ + ν) t ≠ ∞) : ν (toMeasurable (μ + ν) t ∩ s) = ν (t ∩ s) := by rw [add_comm] at ht ⊢ exact measure_toMeasurable_add_inter_left hs ht /-! ### The complete lattice of measures -/ /-- Measures are partially ordered. -/ instance instPartialOrder {_ : MeasurableSpace α} : PartialOrder (Measure α) where le m₁ m₂ := ∀ s, m₁ s ≤ m₂ s le_refl _ _ := le_rfl le_trans _ _ _ h₁ h₂ s := le_trans (h₁ s) (h₂ s) le_antisymm _ _ h₁ h₂ := ext fun s _ => le_antisymm (h₁ s) (h₂ s) theorem toOuterMeasure_le : μ₁.toOuterMeasure ≤ μ₂.toOuterMeasure ↔ μ₁ ≤ μ₂ := .rfl theorem le_iff : μ₁ ≤ μ₂ ↔ ∀ s, MeasurableSet s → μ₁ s ≤ μ₂ s := outerMeasure_le_iff theorem le_intro (h : ∀ s, MeasurableSet s → s.Nonempty → μ₁ s ≤ μ₂ s) : μ₁ ≤ μ₂ := le_iff.2 fun s hs ↦ s.eq_empty_or_nonempty.elim (by rintro rfl; simp) (h s hs) theorem le_iff' : μ₁ ≤ μ₂ ↔ ∀ s, μ₁ s ≤ μ₂ s := .rfl theorem lt_iff : μ < ν ↔ μ ≤ ν ∧ ∃ s, MeasurableSet s ∧ μ s < ν s := lt_iff_le_not_le.trans <| and_congr Iff.rfl <| by simp only [le_iff, not_forall, not_le, exists_prop] theorem lt_iff' : μ < ν ↔ μ ≤ ν ∧ ∃ s, μ s < ν s := lt_iff_le_not_le.trans <| and_congr Iff.rfl <| by simp only [le_iff', not_forall, not_le] instance instAddLeftMono {_ : MeasurableSpace α} : AddLeftMono (Measure α) := ⟨fun _ν _μ₁ _μ₂ hμ s => add_le_add_left (hμ s) _⟩ protected theorem le_add_left (h : μ ≤ ν) : μ ≤ ν' + ν := fun s => le_add_left (h s) protected theorem le_add_right (h : μ ≤ ν) : μ ≤ ν + ν' := fun s => le_add_right (h s) section sInf variable {m : Set (Measure α)} theorem sInf_caratheodory (s : Set α) (hs : MeasurableSet s) : MeasurableSet[(sInf (toOuterMeasure '' m)).caratheodory] s := by rw [OuterMeasure.sInf_eq_boundedBy_sInfGen] refine OuterMeasure.boundedBy_caratheodory fun t => ?_ simp only [OuterMeasure.sInfGen, le_iInf_iff, forall_mem_image, measure_eq_iInf t, coe_toOuterMeasure] intro μ hμ u htu _hu have hm : ∀ {s t}, s ⊆ t → OuterMeasure.sInfGen (toOuterMeasure '' m) s ≤ μ t := by intro s t hst rw [OuterMeasure.sInfGen_def, iInf_image] exact iInf₂_le_of_le μ hμ <| measure_mono hst rw [← measure_inter_add_diff u hs] exact add_le_add (hm <| inter_subset_inter_left _ htu) (hm <| diff_subset_diff_left htu) instance {_ : MeasurableSpace α} : InfSet (Measure α) := ⟨fun m => (sInf (toOuterMeasure '' m)).toMeasure <| sInf_caratheodory⟩ theorem sInf_apply (hs : MeasurableSet s) : sInf m s = sInf (toOuterMeasure '' m) s := toMeasure_apply _ _ hs private theorem measure_sInf_le (h : μ ∈ m) : sInf m ≤ μ := have : sInf (toOuterMeasure '' m) ≤ μ.toOuterMeasure := sInf_le (mem_image_of_mem _ h) le_iff.2 fun s hs => by rw [sInf_apply hs]; exact this s private theorem measure_le_sInf (h : ∀ μ' ∈ m, μ ≤ μ') : μ ≤ sInf m := have : μ.toOuterMeasure ≤ sInf (toOuterMeasure '' m) := le_sInf <| forall_mem_image.2 fun _ hμ ↦ toOuterMeasure_le.2 <| h _ hμ le_iff.2 fun s hs => by rw [sInf_apply hs]; exact this s instance instCompleteSemilatticeInf {_ : MeasurableSpace α} : CompleteSemilatticeInf (Measure α) := { (by infer_instance : PartialOrder (Measure α)), (by infer_instance : InfSet (Measure α)) with sInf_le := fun _s _a => measure_sInf_le le_sInf := fun _s _a => measure_le_sInf } instance instCompleteLattice {_ : MeasurableSpace α} : CompleteLattice (Measure α) := { completeLatticeOfCompleteSemilatticeInf (Measure α) with top := { toOuterMeasure := ⊤, m_iUnion := by intro f _ _ refine (measure_iUnion_le _).antisymm ?_ if hne : (⋃ i, f i).Nonempty then rw [OuterMeasure.top_apply hne] exact le_top else simp_all [Set.not_nonempty_iff_eq_empty] trim_le := le_top }, le_top := fun _ => toOuterMeasure_le.mp le_top bot := 0 bot_le := fun _a _s => bot_le } end sInf
lemma inf_apply {s : Set α} (hs : MeasurableSet s) : (μ ⊓ ν) s = sInf {m | ∃ t, m = μ (t ∩ s) + ν (tᶜ ∩ s)} := by
Mathlib/MeasureTheory/Measure/MeasureSpace.lean
1,031
1,033
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Yury Kudryashov -/ import Mathlib.Algebra.Order.Ring.WithTop import Mathlib.Algebra.Order.Sub.WithTop import Mathlib.Data.NNReal.Defs import Mathlib.Order.Interval.Set.WithBotTop /-! # Extended non-negative reals We define `ENNReal = ℝ≥0∞ := WithTop ℝ≥0` to be the type of extended nonnegative real numbers, i.e., the interval `[0, +∞]`. This type is used as the codomain of a `MeasureTheory.Measure`, and of the extended distance `edist` in an `EMetricSpace`. In this file we set up many of the instances on `ℝ≥0∞`, and provide relationships between `ℝ≥0∞` and `ℝ≥0`, and between `ℝ≥0∞` and `ℝ`. In particular, we provide a coercion from `ℝ≥0` to `ℝ≥0∞` as well as functions `ENNReal.toNNReal`, `ENNReal.ofReal` and `ENNReal.toReal`, all of which take the value zero wherever they cannot be the identity. Also included is the relationship between `ℝ≥0∞` and `ℕ`. The interaction of these functions, especially `ENNReal.ofReal` and `ENNReal.toReal`, with the algebraic and lattice structure can be found in `Data.ENNReal.Real`. This file proves many of the order properties of `ℝ≥0∞`, with the exception of the ways those relate to the algebraic structure, which are included in `Data.ENNReal.Operations`. This file also defines inversion and division: this includes `Inv` and `Div` instances on `ℝ≥0∞` making it into a `DivInvOneMonoid`. As a consequence of being a `DivInvOneMonoid`, `ℝ≥0∞` inherits a power operation with integer exponent: this and other properties is shown in `Data.ENNReal.Inv`. ## Main definitions * `ℝ≥0∞`: the extended nonnegative real numbers `[0, ∞]`; defined as `WithTop ℝ≥0`; it is equipped with the following structures: - coercion from `ℝ≥0` defined in the natural way; - the natural structure of a complete dense linear order: `↑p ≤ ↑q ↔ p ≤ q` and `∀ a, a ≤ ∞`; - `a + b` is defined so that `↑p + ↑q = ↑(p + q)` for `(p q : ℝ≥0)` and `a + ∞ = ∞ + a = ∞`; - `a * b` is defined so that `↑p * ↑q = ↑(p * q)` for `(p q : ℝ≥0)`, `0 * ∞ = ∞ * 0 = 0`, and `a * ∞ = ∞ * a = ∞` for `a ≠ 0`; - `a - b` is defined as the minimal `d` such that `a ≤ d + b`; this way we have `↑p - ↑q = ↑(p - q)`, `∞ - ↑p = ∞`, `↑p - ∞ = ∞ - ∞ = 0`; note that there is no negation, only subtraction; The addition and multiplication defined this way together with `0 = ↑0` and `1 = ↑1` turn `ℝ≥0∞` into a canonically ordered commutative semiring of characteristic zero. - `a⁻¹` is defined as `Inf {b | 1 ≤ a * b}`. This way we have `(↑p)⁻¹ = ↑(p⁻¹)` for `p : ℝ≥0`, `p ≠ 0`, `0⁻¹ = ∞`, and `∞⁻¹ = 0`. - `a / b` is defined as `a * b⁻¹`. This inversion and division include `Inv` and `Div` instances on `ℝ≥0∞`, making it into a `DivInvOneMonoid`. Further properties of these are shown in `Data.ENNReal.Inv`. * Coercions to/from other types: - coercion `ℝ≥0 → ℝ≥0∞` is defined as `Coe`, so one can use `(p : ℝ≥0)` in a context that expects `a : ℝ≥0∞`, and Lean will apply `coe` automatically; - `ENNReal.toNNReal` sends `↑p` to `p` and `∞` to `0`; - `ENNReal.toReal := coe ∘ ENNReal.toNNReal` sends `↑p`, `p : ℝ≥0` to `(↑p : ℝ)` and `∞` to `0`; - `ENNReal.ofReal := coe ∘ Real.toNNReal` sends `x : ℝ` to `↑⟨max x 0, _⟩` - `ENNReal.neTopEquivNNReal` is an equivalence between `{a : ℝ≥0∞ // a ≠ 0}` and `ℝ≥0`. ## Implementation notes We define a `CanLift ℝ≥0∞ ℝ≥0` instance, so one of the ways to prove theorems about an `ℝ≥0∞` number `a` is to consider the cases `a = ∞` and `a ≠ ∞`, and use the tactic `lift a to ℝ≥0 using ha` in the second case. This instance is even more useful if one already has `ha : a ≠ ∞` in the context, or if we have `(f : α → ℝ≥0∞) (hf : ∀ x, f x ≠ ∞)`. ## Notations * `ℝ≥0∞`: the type of the extended nonnegative real numbers; * `ℝ≥0`: the type of nonnegative real numbers `[0, ∞)`; defined in `Data.Real.NNReal`; * `∞`: a localized notation in `ENNReal` for `⊤ : ℝ≥0∞`. -/ assert_not_exists Finset open Function Set NNReal variable {α : Type*} /-- The extended nonnegative real numbers. This is usually denoted [0, ∞], and is relevant as the codomain of a measure. -/ def ENNReal := WithTop ℝ≥0 deriving Zero, Top, AddCommMonoidWithOne, SemilatticeSup, DistribLattice, Nontrivial @[inherit_doc] scoped[ENNReal] notation "ℝ≥0∞" => ENNReal /-- Notation for infinity as an `ENNReal` number. -/ scoped[ENNReal] notation "∞" => (⊤ : ENNReal) namespace ENNReal instance : OrderBot ℝ≥0∞ := inferInstanceAs (OrderBot (WithTop ℝ≥0)) instance : OrderTop ℝ≥0∞ := inferInstanceAs (OrderTop (WithTop ℝ≥0)) instance : BoundedOrder ℝ≥0∞ := inferInstanceAs (BoundedOrder (WithTop ℝ≥0)) instance : CharZero ℝ≥0∞ := inferInstanceAs (CharZero (WithTop ℝ≥0)) instance : Min ℝ≥0∞ := SemilatticeInf.toMin instance : Max ℝ≥0∞ := SemilatticeSup.toMax noncomputable instance : CommSemiring ℝ≥0∞ := inferInstanceAs (CommSemiring (WithTop ℝ≥0)) instance : PartialOrder ℝ≥0∞ := inferInstanceAs (PartialOrder (WithTop ℝ≥0)) instance : IsOrderedRing ℝ≥0∞ := inferInstanceAs (IsOrderedRing (WithTop ℝ≥0)) instance : CanonicallyOrderedAdd ℝ≥0∞ := inferInstanceAs (CanonicallyOrderedAdd (WithTop ℝ≥0)) instance : NoZeroDivisors ℝ≥0∞ := inferInstanceAs (NoZeroDivisors (WithTop ℝ≥0)) noncomputable instance : CompleteLinearOrder ℝ≥0∞ := inferInstanceAs (CompleteLinearOrder (WithTop ℝ≥0)) instance : DenselyOrdered ℝ≥0∞ := inferInstanceAs (DenselyOrdered (WithTop ℝ≥0)) instance : AddCommMonoid ℝ≥0∞ := inferInstanceAs (AddCommMonoid (WithTop ℝ≥0)) noncomputable instance : LinearOrder ℝ≥0∞ := inferInstanceAs (LinearOrder (WithTop ℝ≥0)) instance : IsOrderedAddMonoid ℝ≥0∞ := inferInstanceAs (IsOrderedAddMonoid (WithTop ℝ≥0)) instance instSub : Sub ℝ≥0∞ := inferInstanceAs (Sub (WithTop ℝ≥0)) instance : OrderedSub ℝ≥0∞ := inferInstanceAs (OrderedSub (WithTop ℝ≥0)) noncomputable instance : LinearOrderedAddCommMonoidWithTop ℝ≥0∞ := inferInstanceAs (LinearOrderedAddCommMonoidWithTop (WithTop ℝ≥0)) -- RFC: redefine using pattern matching? noncomputable instance : Inv ℝ≥0∞ := ⟨fun a => sInf { b | 1 ≤ a * b }⟩ noncomputable instance : DivInvMonoid ℝ≥0∞ where variable {a b c d : ℝ≥0∞} {r p q : ℝ≥0} -- TODO: add a `WithTop` instance and use it here noncomputable instance : LinearOrderedCommMonoidWithZero ℝ≥0∞ := { inferInstanceAs (LinearOrderedAddCommMonoidWithTop ℝ≥0∞), inferInstanceAs (CommSemiring ℝ≥0∞) with bot_le _ := bot_le mul_le_mul_left := fun _ _ => mul_le_mul_left' zero_le_one := zero_le 1 } instance : Unique (AddUnits ℝ≥0∞) where default := 0 uniq a := AddUnits.ext <| le_zero_iff.1 <| by rw [← a.add_neg]; exact le_self_add instance : Inhabited ℝ≥0∞ := ⟨0⟩ /-- Coercion from `ℝ≥0` to `ℝ≥0∞`. -/ @[coe, match_pattern] def ofNNReal : ℝ≥0 → ℝ≥0∞ := WithTop.some instance : Coe ℝ≥0 ℝ≥0∞ := ⟨ofNNReal⟩ /-- A version of `WithTop.recTopCoe` that uses `ENNReal.ofNNReal`. -/ @[elab_as_elim, induction_eliminator, cases_eliminator] def recTopCoe {C : ℝ≥0∞ → Sort*} (top : C ∞) (coe : ∀ x : ℝ≥0, C x) (x : ℝ≥0∞) : C x := WithTop.recTopCoe top coe x instance canLift : CanLift ℝ≥0∞ ℝ≥0 ofNNReal (· ≠ ∞) := WithTop.canLift @[simp] theorem none_eq_top : (none : ℝ≥0∞) = ∞ := rfl @[simp] theorem some_eq_coe (a : ℝ≥0) : (Option.some a : ℝ≥0∞) = (↑a : ℝ≥0∞) := rfl @[simp] theorem some_eq_coe' (a : ℝ≥0) : (WithTop.some a : ℝ≥0∞) = (↑a : ℝ≥0∞) := rfl lemma coe_injective : Injective ((↑) : ℝ≥0 → ℝ≥0∞) := WithTop.coe_injective @[simp, norm_cast] lemma coe_inj : (p : ℝ≥0∞) = q ↔ p = q := coe_injective.eq_iff lemma coe_ne_coe : (p : ℝ≥0∞) ≠ q ↔ p ≠ q := coe_inj.not theorem range_coe' : range ofNNReal = Iio ∞ := WithTop.range_coe theorem range_coe : range ofNNReal = {∞}ᶜ := (isCompl_range_some_none ℝ≥0).symm.compl_eq.symm instance : NNRatCast ℝ≥0∞ where nnratCast r := ofNNReal r @[norm_cast] theorem coe_nnratCast (q : ℚ≥0) : ↑(q : ℝ≥0) = (q : ℝ≥0∞) := rfl /-- `toNNReal x` returns `x` if it is real, otherwise 0. -/ protected def toNNReal : ℝ≥0∞ → ℝ≥0 := WithTop.untopD 0 /-- `toReal x` returns `x` if it is real, `0` otherwise. -/ protected def toReal (a : ℝ≥0∞) : Real := a.toNNReal /-- `ofReal x` returns `x` if it is nonnegative, `0` otherwise. -/ protected def ofReal (r : Real) : ℝ≥0∞ := r.toNNReal @[simp, norm_cast] lemma toNNReal_coe (r : ℝ≥0) : (r : ℝ≥0∞).toNNReal = r := rfl @[simp] theorem coe_toNNReal : ∀ {a : ℝ≥0∞}, a ≠ ∞ → ↑a.toNNReal = a | ofNNReal _, _ => rfl | ⊤, h => (h rfl).elim @[simp] theorem coe_comp_toNNReal_comp {ι : Type*} {f : ι → ℝ≥0∞} (hf : ∀ x, f x ≠ ∞) : (fun (x : ℝ≥0) => (x : ℝ≥0∞)) ∘ ENNReal.toNNReal ∘ f = f := by ext x simp [coe_toNNReal (hf x)] @[simp] theorem ofReal_toReal {a : ℝ≥0∞} (h : a ≠ ∞) : ENNReal.ofReal a.toReal = a := by simp [ENNReal.toReal, ENNReal.ofReal, h] @[simp] theorem toReal_ofReal {r : ℝ} (h : 0 ≤ r) : (ENNReal.ofReal r).toReal = r := max_eq_left h theorem toReal_ofReal' {r : ℝ} : (ENNReal.ofReal r).toReal = max r 0 := rfl theorem coe_toNNReal_le_self : ∀ {a : ℝ≥0∞}, ↑a.toNNReal ≤ a | ofNNReal r => by rw [toNNReal_coe] | ⊤ => le_top theorem coe_nnreal_eq (r : ℝ≥0) : (r : ℝ≥0∞) = ENNReal.ofReal r := by rw [ENNReal.ofReal, Real.toNNReal_coe] theorem ofReal_eq_coe_nnreal {x : ℝ} (h : 0 ≤ x) : ENNReal.ofReal x = ofNNReal ⟨x, h⟩ := (coe_nnreal_eq ⟨x, h⟩).symm theorem ofNNReal_toNNReal (x : ℝ) : (Real.toNNReal x : ℝ≥0∞) = ENNReal.ofReal x := rfl @[simp] theorem ofReal_coe_nnreal : ENNReal.ofReal p = p := (coe_nnreal_eq p).symm @[simp, norm_cast] theorem coe_zero : ↑(0 : ℝ≥0) = (0 : ℝ≥0∞) := rfl @[simp, norm_cast] theorem coe_one : ↑(1 : ℝ≥0) = (1 : ℝ≥0∞) := rfl @[simp] theorem toReal_nonneg {a : ℝ≥0∞} : 0 ≤ a.toReal := a.toNNReal.2 @[norm_cast] theorem coe_toNNReal_eq_toReal (z : ℝ≥0∞) : (z.toNNReal : ℝ) = z.toReal := rfl @[simp] theorem toNNReal_toReal_eq (z : ℝ≥0∞) : z.toReal.toNNReal = z.toNNReal := by ext; simp [coe_toNNReal_eq_toReal] @[simp] theorem toNNReal_top : ∞.toNNReal = 0 := rfl @[deprecated (since := "2025-03-20")] alias top_toNNReal := toNNReal_top @[simp] theorem toReal_top : ∞.toReal = 0 := rfl @[deprecated (since := "2025-03-20")] alias top_toReal := toReal_top @[simp] theorem toReal_one : (1 : ℝ≥0∞).toReal = 1 := rfl @[deprecated (since := "2025-03-20")] alias one_toReal := toReal_one @[simp] theorem toNNReal_one : (1 : ℝ≥0∞).toNNReal = 1 := rfl @[deprecated (since := "2025-03-20")] alias one_toNNReal := toNNReal_one @[simp] theorem coe_toReal (r : ℝ≥0) : (r : ℝ≥0∞).toReal = r := rfl @[simp] theorem toNNReal_zero : (0 : ℝ≥0∞).toNNReal = 0 := rfl @[deprecated (since := "2025-03-20")] alias zero_toNNReal := toNNReal_zero @[simp] theorem toReal_zero : (0 : ℝ≥0∞).toReal = 0 := rfl @[deprecated (since := "2025-03-20")] alias zero_toReal := toReal_zero @[simp] theorem ofReal_zero : ENNReal.ofReal (0 : ℝ) = 0 := by simp [ENNReal.ofReal] @[simp] theorem ofReal_one : ENNReal.ofReal (1 : ℝ) = (1 : ℝ≥0∞) := by simp [ENNReal.ofReal] theorem ofReal_toReal_le {a : ℝ≥0∞} : ENNReal.ofReal a.toReal ≤ a := if ha : a = ∞ then ha.symm ▸ le_top else le_of_eq (ofReal_toReal ha) theorem forall_ennreal {p : ℝ≥0∞ → Prop} : (∀ a, p a) ↔ (∀ r : ℝ≥0, p r) ∧ p ∞ := Option.forall.trans and_comm theorem forall_ne_top {p : ℝ≥0∞ → Prop} : (∀ a, a ≠ ∞ → p a) ↔ ∀ r : ℝ≥0, p r := Option.forall_ne_none theorem exists_ne_top {p : ℝ≥0∞ → Prop} : (∃ a ≠ ∞, p a) ↔ ∃ r : ℝ≥0, p r := Option.exists_ne_none theorem toNNReal_eq_zero_iff (x : ℝ≥0∞) : x.toNNReal = 0 ↔ x = 0 ∨ x = ∞ := WithTop.untopD_eq_self_iff theorem toReal_eq_zero_iff (x : ℝ≥0∞) : x.toReal = 0 ↔ x = 0 ∨ x = ∞ := by simp [ENNReal.toReal, toNNReal_eq_zero_iff] theorem toNNReal_ne_zero : a.toNNReal ≠ 0 ↔ a ≠ 0 ∧ a ≠ ∞ := a.toNNReal_eq_zero_iff.not.trans not_or theorem toReal_ne_zero : a.toReal ≠ 0 ↔ a ≠ 0 ∧ a ≠ ∞ := a.toReal_eq_zero_iff.not.trans not_or theorem toNNReal_eq_one_iff (x : ℝ≥0∞) : x.toNNReal = 1 ↔ x = 1 := WithTop.untopD_eq_iff.trans <| by simp theorem toReal_eq_one_iff (x : ℝ≥0∞) : x.toReal = 1 ↔ x = 1 := by rw [ENNReal.toReal, NNReal.coe_eq_one, ENNReal.toNNReal_eq_one_iff] theorem toNNReal_ne_one : a.toNNReal ≠ 1 ↔ a ≠ 1 := a.toNNReal_eq_one_iff.not theorem toReal_ne_one : a.toReal ≠ 1 ↔ a ≠ 1 := a.toReal_eq_one_iff.not @[simp, aesop (rule_sets := [finiteness]) safe apply] theorem coe_ne_top : (r : ℝ≥0∞) ≠ ∞ := WithTop.coe_ne_top @[simp] theorem top_ne_coe : ∞ ≠ (r : ℝ≥0∞) := WithTop.top_ne_coe @[simp] theorem coe_lt_top : (r : ℝ≥0∞) < ∞ := WithTop.coe_lt_top r @[simp, aesop (rule_sets := [finiteness]) safe apply] theorem ofReal_ne_top {r : ℝ} : ENNReal.ofReal r ≠ ∞ := coe_ne_top @[simp] theorem ofReal_lt_top {r : ℝ} : ENNReal.ofReal r < ∞ := coe_lt_top @[simp] theorem top_ne_ofReal {r : ℝ} : ∞ ≠ ENNReal.ofReal r := top_ne_coe @[simp] theorem ofReal_toReal_eq_iff : ENNReal.ofReal a.toReal = a ↔ a ≠ ⊤ := ⟨fun h => by rw [← h] exact ofReal_ne_top, ofReal_toReal⟩ @[simp] theorem toReal_ofReal_eq_iff {a : ℝ} : (ENNReal.ofReal a).toReal = a ↔ 0 ≤ a := ⟨fun h => by rw [← h] exact toReal_nonneg, toReal_ofReal⟩ @[simp, aesop (rule_sets := [finiteness]) safe apply] theorem zero_ne_top : 0 ≠ ∞ := coe_ne_top @[simp] theorem top_ne_zero : ∞ ≠ 0 := top_ne_coe @[simp, aesop (rule_sets := [finiteness]) safe apply] theorem one_ne_top : 1 ≠ ∞ := coe_ne_top @[simp] theorem top_ne_one : ∞ ≠ 1 := top_ne_coe @[simp] theorem zero_lt_top : 0 < ∞ := coe_lt_top @[simp, norm_cast] theorem coe_le_coe : (↑r : ℝ≥0∞) ≤ ↑q ↔ r ≤ q := WithTop.coe_le_coe @[simp, norm_cast] theorem coe_lt_coe : (↑r : ℝ≥0∞) < ↑q ↔ r < q := WithTop.coe_lt_coe -- Needed until `@[gcongr]` accepts iff statements alias ⟨_, coe_le_coe_of_le⟩ := coe_le_coe attribute [gcongr] ENNReal.coe_le_coe_of_le -- Needed until `@[gcongr]` accepts iff statements alias ⟨_, coe_lt_coe_of_lt⟩ := coe_lt_coe attribute [gcongr] ENNReal.coe_lt_coe_of_lt theorem coe_mono : Monotone ofNNReal := fun _ _ => coe_le_coe.2 theorem coe_strictMono : StrictMono ofNNReal := fun _ _ => coe_lt_coe.2 @[simp, norm_cast] theorem coe_eq_zero : (↑r : ℝ≥0∞) = 0 ↔ r = 0 := coe_inj @[simp, norm_cast] theorem zero_eq_coe : 0 = (↑r : ℝ≥0∞) ↔ 0 = r := coe_inj @[simp, norm_cast] theorem coe_eq_one : (↑r : ℝ≥0∞) = 1 ↔ r = 1 := coe_inj @[simp, norm_cast] theorem one_eq_coe : 1 = (↑r : ℝ≥0∞) ↔ 1 = r := coe_inj @[simp, norm_cast] theorem coe_pos : 0 < (r : ℝ≥0∞) ↔ 0 < r := coe_lt_coe theorem coe_ne_zero : (r : ℝ≥0∞) ≠ 0 ↔ r ≠ 0 := coe_eq_zero.not lemma coe_ne_one : (r : ℝ≥0∞) ≠ 1 ↔ r ≠ 1 := coe_eq_one.not @[simp, norm_cast] lemma coe_add (x y : ℝ≥0) : (↑(x + y) : ℝ≥0∞) = x + y := rfl @[simp, norm_cast] lemma coe_mul (x y : ℝ≥0) : (↑(x * y) : ℝ≥0∞) = x * y := rfl @[norm_cast] lemma coe_nsmul (n : ℕ) (x : ℝ≥0) : (↑(n • x) : ℝ≥0∞) = n • x := rfl @[simp, norm_cast] lemma coe_pow (x : ℝ≥0) (n : ℕ) : (↑(x ^ n) : ℝ≥0∞) = x ^ n := rfl @[simp, norm_cast] theorem coe_ofNat (n : ℕ) [n.AtLeastTwo] : ((ofNat(n) : ℝ≥0) : ℝ≥0∞) = ofNat(n) := rfl -- TODO: add lemmas about `OfNat.ofNat` and `<`/`≤` theorem coe_two : ((2 : ℝ≥0) : ℝ≥0∞) = 2 := rfl theorem toNNReal_eq_toNNReal_iff (x y : ℝ≥0∞) : x.toNNReal = y.toNNReal ↔ x = y ∨ x = 0 ∧ y = ⊤ ∨ x = ⊤ ∧ y = 0 := WithTop.untopD_eq_untopD_iff theorem toReal_eq_toReal_iff (x y : ℝ≥0∞) : x.toReal = y.toReal ↔ x = y ∨ x = 0 ∧ y = ⊤ ∨ x = ⊤ ∧ y = 0 := by simp only [ENNReal.toReal, NNReal.coe_inj, toNNReal_eq_toNNReal_iff] theorem toNNReal_eq_toNNReal_iff' {x y : ℝ≥0∞} (hx : x ≠ ⊤) (hy : y ≠ ⊤) : x.toNNReal = y.toNNReal ↔ x = y := by simp only [ENNReal.toNNReal_eq_toNNReal_iff x y, hx, hy, and_false, false_and, or_false] theorem toReal_eq_toReal_iff' {x y : ℝ≥0∞} (hx : x ≠ ⊤) (hy : y ≠ ⊤) : x.toReal = y.toReal ↔ x = y := by simp only [ENNReal.toReal, NNReal.coe_inj, toNNReal_eq_toNNReal_iff' hx hy] theorem one_lt_two : (1 : ℝ≥0∞) < 2 := Nat.one_lt_ofNat /-- `(1 : ℝ≥0∞) ≤ 1`, recorded as a `Fact` for use with `Lp` spaces. -/ instance _root_.fact_one_le_one_ennreal : Fact ((1 : ℝ≥0∞) ≤ 1) := ⟨le_rfl⟩ /-- `(1 : ℝ≥0∞) ≤ 2`, recorded as a `Fact` for use with `Lp` spaces. -/ instance _root_.fact_one_le_two_ennreal : Fact ((1 : ℝ≥0∞) ≤ 2) := ⟨one_le_two⟩ /-- `(1 : ℝ≥0∞) ≤ ∞`, recorded as a `Fact` for use with `Lp` spaces. -/ instance _root_.fact_one_le_top_ennreal : Fact ((1 : ℝ≥0∞) ≤ ∞) := ⟨le_top⟩ /-- The set of numbers in `ℝ≥0∞` that are not equal to `∞` is equivalent to `ℝ≥0`. -/ def neTopEquivNNReal : { a | a ≠ ∞ } ≃ ℝ≥0 where toFun x := ENNReal.toNNReal x invFun x := ⟨x, coe_ne_top⟩ left_inv := fun x => Subtype.eq <| coe_toNNReal x.2 right_inv := toNNReal_coe theorem cinfi_ne_top [InfSet α] (f : ℝ≥0∞ → α) : ⨅ x : { x // x ≠ ∞ }, f x = ⨅ x : ℝ≥0, f x := Eq.symm <| neTopEquivNNReal.symm.surjective.iInf_congr _ fun _ => rfl theorem iInf_ne_top [CompleteLattice α] (f : ℝ≥0∞ → α) : ⨅ (x) (_ : x ≠ ∞), f x = ⨅ x : ℝ≥0, f x := by rw [iInf_subtype', cinfi_ne_top] theorem csupr_ne_top [SupSet α] (f : ℝ≥0∞ → α) : ⨆ x : { x // x ≠ ∞ }, f x = ⨆ x : ℝ≥0, f x := @cinfi_ne_top αᵒᵈ _ _ theorem iSup_ne_top [CompleteLattice α] (f : ℝ≥0∞ → α) : ⨆ (x) (_ : x ≠ ∞), f x = ⨆ x : ℝ≥0, f x := @iInf_ne_top αᵒᵈ _ _ theorem iInf_ennreal {α : Type*} [CompleteLattice α] {f : ℝ≥0∞ → α} : ⨅ n, f n = (⨅ n : ℝ≥0, f n) ⊓ f ∞ := (iInf_option f).trans (inf_comm _ _) theorem iSup_ennreal {α : Type*} [CompleteLattice α] {f : ℝ≥0∞ → α} : ⨆ n, f n = (⨆ n : ℝ≥0, f n) ⊔ f ∞ := @iInf_ennreal αᵒᵈ _ _ /-- Coercion `ℝ≥0 → ℝ≥0∞` as a `RingHom`. -/ def ofNNRealHom : ℝ≥0 →+* ℝ≥0∞ where toFun := some map_one' := coe_one map_mul' _ _ := coe_mul _ _ map_zero' := coe_zero map_add' _ _ := coe_add _ _ @[simp] theorem coe_ofNNRealHom : ⇑ofNNRealHom = some := rfl section Order theorem bot_eq_zero : (⊥ : ℝ≥0∞) = 0 := rfl -- `coe_lt_top` moved up theorem not_top_le_coe : ¬∞ ≤ ↑r := WithTop.not_top_le_coe r @[simp, norm_cast] theorem one_le_coe_iff : (1 : ℝ≥0∞) ≤ ↑r ↔ 1 ≤ r := coe_le_coe @[simp, norm_cast] theorem coe_le_one_iff : ↑r ≤ (1 : ℝ≥0∞) ↔ r ≤ 1 := coe_le_coe @[simp, norm_cast] theorem coe_lt_one_iff : (↑p : ℝ≥0∞) < 1 ↔ p < 1 := coe_lt_coe @[simp, norm_cast] theorem one_lt_coe_iff : 1 < (↑p : ℝ≥0∞) ↔ 1 < p := coe_lt_coe @[simp, norm_cast] theorem coe_natCast (n : ℕ) : ((n : ℝ≥0) : ℝ≥0∞) = n := rfl @[simp, norm_cast] lemma ofReal_natCast (n : ℕ) : ENNReal.ofReal n = n := by simp [ENNReal.ofReal] @[simp] theorem ofReal_ofNat (n : ℕ) [n.AtLeastTwo] : ENNReal.ofReal ofNat(n) = ofNat(n) := ofReal_natCast n @[simp, aesop (rule_sets := [finiteness]) safe apply] theorem natCast_ne_top (n : ℕ) : (n : ℝ≥0∞) ≠ ∞ := WithTop.natCast_ne_top n @[simp] theorem natCast_lt_top (n : ℕ) : (n : ℝ≥0∞) < ∞ := WithTop.natCast_lt_top n @[simp, aesop (rule_sets := [finiteness]) safe apply] lemma ofNat_ne_top {n : ℕ} [Nat.AtLeastTwo n] : ofNat(n) ≠ ∞ := natCast_ne_top n @[simp] lemma ofNat_lt_top {n : ℕ} [Nat.AtLeastTwo n] : ofNat(n) < ∞ := natCast_lt_top n @[simp] theorem top_ne_natCast (n : ℕ) : ∞ ≠ n := WithTop.top_ne_natCast n @[simp] theorem top_ne_ofNat {n : ℕ} [n.AtLeastTwo] : ∞ ≠ ofNat(n) := ofNat_ne_top.symm @[deprecated ofNat_ne_top (since := "2025-01-21")] lemma two_ne_top : (2 : ℝ≥0∞) ≠ ∞ := coe_ne_top @[deprecated ofNat_lt_top (since := "2025-01-21")] lemma two_lt_top : (2 : ℝ≥0∞) < ∞ := coe_lt_top @[simp] theorem one_lt_top : 1 < ∞ := coe_lt_top @[simp, norm_cast] theorem toNNReal_natCast (n : ℕ) : (n : ℝ≥0∞).toNNReal = n := by rw [← ENNReal.coe_natCast n, ENNReal.toNNReal_coe] @[deprecated (since := "2025-02-19")] alias toNNReal_nat := toNNReal_natCast theorem toNNReal_ofNat (n : ℕ) [n.AtLeastTwo] : ENNReal.toNNReal ofNat(n) = ofNat(n) := toNNReal_natCast n @[simp, norm_cast] theorem toReal_natCast (n : ℕ) : (n : ℝ≥0∞).toReal = n := by rw [← ENNReal.ofReal_natCast n, ENNReal.toReal_ofReal (Nat.cast_nonneg _)] @[deprecated (since := "2025-02-19")] alias toReal_nat := toReal_natCast @[simp] theorem toReal_ofNat (n : ℕ) [n.AtLeastTwo] : ENNReal.toReal ofNat(n) = ofNat(n) := toReal_natCast n lemma toNNReal_natCast_eq_toNNReal (n : ℕ) : (n : ℝ≥0∞).toNNReal = (n : ℝ).toNNReal := by rw [Real.toNNReal_of_nonneg (by positivity), ENNReal.toNNReal_natCast, mk_natCast] theorem le_coe_iff : a ≤ ↑r ↔ ∃ p : ℝ≥0, a = p ∧ p ≤ r := WithTop.le_coe_iff theorem coe_le_iff : ↑r ≤ a ↔ ∀ p : ℝ≥0, a = p → r ≤ p := WithTop.coe_le_iff theorem lt_iff_exists_coe : a < b ↔ ∃ p : ℝ≥0, a = p ∧ ↑p < b := WithTop.lt_iff_exists_coe theorem toReal_le_coe_of_le_coe {a : ℝ≥0∞} {b : ℝ≥0} (h : a ≤ b) : a.toReal ≤ b := by lift a to ℝ≥0 using ne_top_of_le_ne_top coe_ne_top h simpa using h @[simp] theorem max_eq_zero_iff : max a b = 0 ↔ a = 0 ∧ b = 0 := max_eq_bot theorem max_zero_left : max 0 a = a := max_eq_right (zero_le a) theorem max_zero_right : max a 0 = a := max_eq_left (zero_le a) theorem lt_iff_exists_rat_btwn : a < b ↔ ∃ q : ℚ, 0 ≤ q ∧ a < Real.toNNReal q ∧ (Real.toNNReal q : ℝ≥0∞) < b := ⟨fun h => by rcases lt_iff_exists_coe.1 h with ⟨p, rfl, _⟩ rcases exists_between h with ⟨c, pc, cb⟩ rcases lt_iff_exists_coe.1 cb with ⟨r, rfl, _⟩ rcases (NNReal.lt_iff_exists_rat_btwn _ _).1 (coe_lt_coe.1 pc) with ⟨q, hq0, pq, qr⟩ exact ⟨q, hq0, coe_lt_coe.2 pq, lt_trans (coe_lt_coe.2 qr) cb⟩, fun ⟨_, _, qa, qb⟩ => lt_trans qa qb⟩ theorem lt_iff_exists_real_btwn : a < b ↔ ∃ r : ℝ, 0 ≤ r ∧ a < ENNReal.ofReal r ∧ (ENNReal.ofReal r : ℝ≥0∞) < b := ⟨fun h => let ⟨q, q0, aq, qb⟩ := ENNReal.lt_iff_exists_rat_btwn.1 h ⟨q, Rat.cast_nonneg.2 q0, aq, qb⟩, fun ⟨_, _, qa, qb⟩ => lt_trans qa qb⟩ theorem lt_iff_exists_nnreal_btwn : a < b ↔ ∃ r : ℝ≥0, a < r ∧ (r : ℝ≥0∞) < b := WithTop.lt_iff_exists_coe_btwn theorem lt_iff_exists_add_pos_lt : a < b ↔ ∃ r : ℝ≥0, 0 < r ∧ a + r < b := by refine ⟨fun hab => ?_, fun ⟨r, _, hr⟩ => lt_of_le_of_lt le_self_add hr⟩ rcases lt_iff_exists_nnreal_btwn.1 hab with ⟨c, ac, cb⟩ lift a to ℝ≥0 using ac.ne_top rw [coe_lt_coe] at ac refine ⟨c - a, tsub_pos_iff_lt.2 ac, ?_⟩ rwa [← coe_add, add_tsub_cancel_of_le ac.le] theorem le_of_forall_pos_le_add (h : ∀ ε : ℝ≥0, 0 < ε → b < ∞ → a ≤ b + ε) : a ≤ b := by contrapose! h rcases lt_iff_exists_add_pos_lt.1 h with ⟨r, hr0, hr⟩ exact ⟨r, hr0, h.trans_le le_top, hr⟩ theorem natCast_lt_coe {n : ℕ} : n < (r : ℝ≥0∞) ↔ n < r := ENNReal.coe_natCast n ▸ coe_lt_coe theorem coe_lt_natCast {n : ℕ} : (r : ℝ≥0∞) < n ↔ r < n := ENNReal.coe_natCast n ▸ coe_lt_coe protected theorem exists_nat_gt {r : ℝ≥0∞} (h : r ≠ ∞) : ∃ n : ℕ, r < n := by lift r to ℝ≥0 using h rcases exists_nat_gt r with ⟨n, hn⟩ exact ⟨n, coe_lt_natCast.2 hn⟩ @[simp] theorem iUnion_Iio_coe_nat : ⋃ n : ℕ, Iio (n : ℝ≥0∞) = {∞}ᶜ := by ext x rw [mem_iUnion] exact ⟨fun ⟨n, hn⟩ => ne_top_of_lt hn, ENNReal.exists_nat_gt⟩ @[simp] theorem iUnion_Iic_coe_nat : ⋃ n : ℕ, Iic (n : ℝ≥0∞) = {∞}ᶜ := Subset.antisymm (iUnion_subset fun n _x hx => ne_top_of_le_ne_top (natCast_ne_top n) hx) <| iUnion_Iio_coe_nat ▸ iUnion_mono fun _ => Iio_subset_Iic_self @[simp] theorem iUnion_Ioc_coe_nat : ⋃ n : ℕ, Ioc a n = Ioi a \ {∞} := by simp only [← Ioi_inter_Iic, ← inter_iUnion, iUnion_Iic_coe_nat, diff_eq] @[simp] theorem iUnion_Ioo_coe_nat : ⋃ n : ℕ, Ioo a n = Ioi a \ {∞} := by simp only [← Ioi_inter_Iio, ← inter_iUnion, iUnion_Iio_coe_nat, diff_eq] @[simp] theorem iUnion_Icc_coe_nat : ⋃ n : ℕ, Icc a n = Ici a \ {∞} := by simp only [← Ici_inter_Iic, ← inter_iUnion, iUnion_Iic_coe_nat, diff_eq] @[simp] theorem iUnion_Ico_coe_nat : ⋃ n : ℕ, Ico a n = Ici a \ {∞} := by simp only [← Ici_inter_Iio, ← inter_iUnion, iUnion_Iio_coe_nat, diff_eq] @[simp] theorem iInter_Ici_coe_nat : ⋂ n : ℕ, Ici (n : ℝ≥0∞) = {∞} := by simp only [← compl_Iio, ← compl_iUnion, iUnion_Iio_coe_nat, compl_compl] @[simp] theorem iInter_Ioi_coe_nat : ⋂ n : ℕ, Ioi (n : ℝ≥0∞) = {∞} := by simp only [← compl_Iic, ← compl_iUnion, iUnion_Iic_coe_nat, compl_compl] @[simp, norm_cast] theorem coe_min (r p : ℝ≥0) : ((min r p : ℝ≥0) : ℝ≥0∞) = min (r : ℝ≥0∞) p := rfl @[simp, norm_cast] theorem coe_max (r p : ℝ≥0) : ((max r p : ℝ≥0) : ℝ≥0∞) = max (r : ℝ≥0∞) p := rfl theorem le_of_top_imp_top_of_toNNReal_le {a b : ℝ≥0∞} (h : a = ⊤ → b = ⊤) (h_nnreal : a ≠ ⊤ → b ≠ ⊤ → a.toNNReal ≤ b.toNNReal) : a ≤ b := by by_contra! hlt lift b to ℝ≥0 using hlt.ne_top lift a to ℝ≥0 using mt h coe_ne_top refine hlt.not_le ?_ simpa using h_nnreal @[simp] theorem abs_toReal {x : ℝ≥0∞} : |x.toReal| = x.toReal := by cases x <;> simp end Order section CompleteLattice variable {ι : Sort*} {f : ι → ℝ≥0} theorem coe_sSup {s : Set ℝ≥0} : BddAbove s → (↑(sSup s) : ℝ≥0∞) = ⨆ a ∈ s, ↑a := WithTop.coe_sSup theorem coe_sInf {s : Set ℝ≥0} (hs : s.Nonempty) : (↑(sInf s) : ℝ≥0∞) = ⨅ a ∈ s, ↑a := WithTop.coe_sInf hs (OrderBot.bddBelow s) theorem coe_iSup {ι : Sort*} {f : ι → ℝ≥0} (hf : BddAbove (range f)) : (↑(iSup f) : ℝ≥0∞) = ⨆ a, ↑(f a) := WithTop.coe_iSup _ hf @[norm_cast] theorem coe_iInf {ι : Sort*} [Nonempty ι] (f : ι → ℝ≥0) : (↑(iInf f) : ℝ≥0∞) = ⨅ a, ↑(f a) := WithTop.coe_iInf (OrderBot.bddBelow _) theorem coe_mem_upperBounds {s : Set ℝ≥0} : ↑r ∈ upperBounds (ofNNReal '' s) ↔ r ∈ upperBounds s := by simp +contextual [upperBounds, forall_mem_image, -mem_image, *] lemma iSup_coe_eq_top : ⨆ i, (f i : ℝ≥0∞) = ⊤ ↔ ¬ BddAbove (range f) := WithTop.iSup_coe_eq_top lemma iSup_coe_lt_top : ⨆ i, (f i : ℝ≥0∞) < ⊤ ↔ BddAbove (range f) := WithTop.iSup_coe_lt_top lemma iInf_coe_eq_top : ⨅ i, (f i : ℝ≥0∞) = ⊤ ↔ IsEmpty ι := WithTop.iInf_coe_eq_top lemma iInf_coe_lt_top : ⨅ i, (f i : ℝ≥0∞) < ⊤ ↔ Nonempty ι := WithTop.iInf_coe_lt_top end CompleteLattice section Bit -- TODO: add lemmas about `OfNat.ofNat` end Bit end ENNReal open ENNReal namespace Set namespace OrdConnected variable {s : Set ℝ} {t : Set ℝ≥0} {u : Set ℝ≥0∞} theorem preimage_coe_nnreal_ennreal (h : u.OrdConnected) : ((↑) ⁻¹' u : Set ℝ≥0).OrdConnected :=
h.preimage_mono ENNReal.coe_mono
Mathlib/Data/ENNReal/Basic.lean
708
709
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Floris van Doorn, Amelia Livingston, Yury Kudryashov, Neil Strickland, Aaron Anderson -/ import Mathlib.Algebra.Divisibility.Basic import Mathlib.Algebra.Group.Units.Basic /-! # Divisibility and units ## Main definition * `IsRelPrime x y`: that `x` and `y` are relatively prime, defined to mean that the only common divisors of `x` and `y` are the units. -/ variable {α : Type*} namespace Units section Monoid variable [Monoid α] {a b : α} {u : αˣ} /-- Elements of the unit group of a monoid represented as elements of the monoid divide any element of the monoid. -/ theorem coe_dvd : ↑u ∣ a := ⟨↑u⁻¹ * a, by simp⟩ /-- In a monoid, an element `a` divides an element `b` iff `a` divides all associates of `b`. -/ theorem dvd_mul_right : a ∣ b * u ↔ a ∣ b := Iff.intro (fun ⟨c, eq⟩ ↦ ⟨c * ↑u⁻¹, by rw [← mul_assoc, ← eq, Units.mul_inv_cancel_right]⟩) fun ⟨_, eq⟩ ↦ eq.symm ▸ (_root_.dvd_mul_right _ _).mul_right _ /-- In a monoid, an element `a` divides an element `b` iff all associates of `a` divide `b`. -/ theorem mul_right_dvd : a * u ∣ b ↔ a ∣ b := Iff.intro (fun ⟨c, eq⟩ => ⟨↑u * c, eq.trans (mul_assoc _ _ _)⟩) fun h => dvd_trans (Dvd.intro (↑u⁻¹) (by rw [mul_assoc, u.mul_inv, mul_one])) h end Monoid section CommMonoid variable [CommMonoid α] {a b : α} {u : αˣ} /-- In a commutative monoid, an element `a` divides an element `b` iff `a` divides all left associates of `b`. -/ theorem dvd_mul_left : a ∣ u * b ↔ a ∣ b := by rw [mul_comm] apply dvd_mul_right /-- In a commutative monoid, an element `a` divides an element `b` iff all
left associates of `a` divide `b`. -/ theorem mul_left_dvd : ↑u * a ∣ b ↔ a ∣ b := by rw [mul_comm]
Mathlib/Algebra/Divisibility/Units.lean
57
59
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Johannes Hölzl, Sander Dahmen, Kim Morrison, Chris Hughes, Anne Baanen -/ import Mathlib.Algebra.Algebra.Subalgebra.Lattice import Mathlib.LinearAlgebra.Basis.Prod import Mathlib.LinearAlgebra.Dimension.Free import Mathlib.LinearAlgebra.TensorProduct.Basis /-! # Rank of various constructions ## Main statements - `rank_quotient_add_rank_le` : `rank M/N + rank N ≤ rank M`. - `lift_rank_add_lift_rank_le_rank_prod`: `rank M × N ≤ rank M + rank N`. - `rank_span_le_of_finite`: `rank (span s) ≤ #s` for finite `s`. For free modules, we have - `rank_prod` : `rank M × N = rank M + rank N`. - `rank_finsupp` : `rank (ι →₀ M) = #ι * rank M` - `rank_directSum`: `rank (⨁ Mᵢ) = ∑ rank Mᵢ` - `rank_tensorProduct`: `rank (M ⊗ N) = rank M * rank N`. Lemmas for ranks of submodules and subalgebras are also provided. We have finrank variants for most lemmas as well. -/ noncomputable section universe u u' v v' u₁' w w' variable {R : Type u} {S : Type u'} {M : Type v} {M' : Type v'} {M₁ : Type v} variable {ι : Type w} {ι' : Type w'} {η : Type u₁'} {φ : η → Type*} open Basis Cardinal DirectSum Function Module Set Submodule section Quotient variable [Ring R] [CommRing S] [AddCommGroup M] [AddCommGroup M'] [AddCommGroup M₁] variable [Module R M] theorem LinearIndependent.sumElim_of_quotient {M' : Submodule R M} {ι₁ ι₂} {f : ι₁ → M'} (hf : LinearIndependent R f) (g : ι₂ → M) (hg : LinearIndependent R (Submodule.Quotient.mk (p := M') ∘ g)) : LinearIndependent R (Sum.elim (f · : ι₁ → M) g) := by refine .sum_type (hf.map' M'.subtype M'.ker_subtype) (.of_comp M'.mkQ hg) ?_ refine disjoint_def.mpr fun x h₁ h₂ ↦ ?_ have : x ∈ M' := span_le.mpr (Set.range_subset_iff.mpr fun i ↦ (f i).prop) h₁ obtain ⟨c, rfl⟩ := Finsupp.mem_span_range_iff_exists_finsupp.mp h₂ simp_rw [← Quotient.mk_eq_zero, ← mkQ_apply, map_finsuppSum, map_smul, mkQ_apply] at this rw [linearIndependent_iff.mp hg _ this, Finsupp.sum_zero_index] @[deprecated (since := "2025-02-21")] alias LinearIndependent.sum_elim_of_quotient := LinearIndependent.sumElim_of_quotient theorem LinearIndepOn.union_of_quotient {s t : Set ι} {f : ι → M} (hs : LinearIndepOn R f s) (ht : LinearIndepOn R (mkQ (span R (f '' s)) ∘ f) t) : LinearIndepOn R f (s ∪ t) := by apply hs.union ht.of_comp convert (Submodule.range_ker_disjoint ht).symm · simp aesop theorem LinearIndepOn.union_id_of_quotient {M' : Submodule R M} {s : Set M} (hs : s ⊆ M') (hs' : LinearIndepOn R id s) {t : Set M} (ht : LinearIndepOn R (mkQ M') t) : LinearIndepOn R id (s ∪ t) := hs'.union_of_quotient <| by rw [image_id] exact ht.of_comp ((span R s).mapQ M' (LinearMap.id) (span_le.2 hs)) @[deprecated (since := "2025-02-16")] alias LinearIndependent.union_of_quotient := LinearIndepOn.union_id_of_quotient theorem linearIndepOn_union_iff_quotient {s t : Set ι} {f : ι → M} (hst : Disjoint s t) : LinearIndepOn R f (s ∪ t) ↔ LinearIndepOn R f s ∧ LinearIndepOn R (mkQ (span R (f '' s)) ∘ f) t := by refine ⟨fun h ↦ ⟨?_, ?_⟩, fun h ↦ h.1.union_of_quotient h.2⟩ · exact h.mono subset_union_left apply (h.mono subset_union_right).map simpa [← image_eq_range] using ((linearIndepOn_union_iff hst).1 h).2.2.symm theorem LinearIndepOn.quotient_iff_union {s t : Set ι} {f : ι → M} (hs : LinearIndepOn R f s) (hst : Disjoint s t) : LinearIndepOn R (mkQ (span R (f '' s)) ∘ f) t ↔ LinearIndepOn R f (s ∪ t) := by rw [linearIndepOn_union_iff_quotient hst, and_iff_right hs] theorem rank_quotient_add_rank_le [Nontrivial R] (M' : Submodule R M) : Module.rank R (M ⧸ M') + Module.rank R M' ≤ Module.rank R M := by conv_lhs => simp only [Module.rank_def] have := nonempty_linearIndependent_set R (M ⧸ M') have := nonempty_linearIndependent_set R M' rw [Cardinal.ciSup_add_ciSup _ (bddAbove_range _) _ (bddAbove_range _)] refine ciSup_le fun ⟨s, hs⟩ ↦ ciSup_le fun ⟨t, ht⟩ ↦ ?_ choose f hf using Submodule.Quotient.mk_surjective M' simpa [add_comm] using (LinearIndependent.sumElim_of_quotient ht (fun (i : s) ↦ f i) (by simpa [Function.comp_def, hf] using hs)).cardinal_le_rank theorem rank_quotient_le (p : Submodule R M) : Module.rank R (M ⧸ p) ≤ Module.rank R M := (mkQ p).rank_le_of_surjective Quot.mk_surjective /-- The dimension of a quotient is bounded by the dimension of the ambient space. -/ theorem Submodule.finrank_quotient_le [StrongRankCondition R] [Module.Finite R M] (s : Submodule R M) : finrank R (M ⧸ s) ≤ finrank R M := toNat_le_toNat ((Submodule.mkQ s).rank_le_of_surjective Quot.mk_surjective) (rank_lt_aleph0 _ _) end Quotient variable [Semiring R] [CommSemiring S] [AddCommMonoid M] [AddCommMonoid M'] [AddCommMonoid M₁] variable [Module R M] section ULift @[simp] theorem rank_ulift : Module.rank R (ULift.{w} M) = Cardinal.lift.{w} (Module.rank R M) := Cardinal.lift_injective.{v} <| Eq.symm <| (lift_lift _).trans ULift.moduleEquiv.symm.lift_rank_eq @[simp] theorem finrank_ulift : finrank R (ULift M) = finrank R M := by simp_rw [finrank, rank_ulift, toNat_lift] end ULift section Prod variable (R M M') variable [Module R M₁] [Module R M'] theorem rank_add_rank_le_rank_prod [Nontrivial R] : Module.rank R M + Module.rank R M₁ ≤ Module.rank R (M × M₁) := by conv_lhs => simp only [Module.rank_def]
have := nonempty_linearIndependent_set R M have := nonempty_linearIndependent_set R M₁ rw [Cardinal.ciSup_add_ciSup _ (bddAbove_range _) _ (bddAbove_range _)] exact ciSup_le fun ⟨s, hs⟩ ↦ ciSup_le fun ⟨t, ht⟩ ↦
Mathlib/LinearAlgebra/Dimension/Constructions.lean
136
139
/- Copyright (c) 2020 Aaron Anderson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson, Yaël Dillies -/ import Mathlib.Order.Cover import Mathlib.Order.LatticeIntervals import Mathlib.Order.GaloisConnection.Defs /-! # Modular Lattices This file defines (semi)modular lattices, a kind of lattice useful in algebra. For examples, look to the subobject lattices of abelian groups, submodules, and ideals, or consider any distributive lattice. ## Typeclasses We define (semi)modularity typeclasses as Prop-valued mixins. * `IsWeakUpperModularLattice`: Weakly upper modular lattices. Lattice where `a ⊔ b` covers `a` and `b` if `a` and `b` both cover `a ⊓ b`. * `IsWeakLowerModularLattice`: Weakly lower modular lattices. Lattice where `a` and `b` cover `a ⊓ b` if `a ⊔ b` covers both `a` and `b` * `IsUpperModularLattice`: Upper modular lattices. Lattices where `a ⊔ b` covers `a` if `b` covers `a ⊓ b`. * `IsLowerModularLattice`: Lower modular lattices. Lattices where `a` covers `a ⊓ b` if `a ⊔ b` covers `b`. - `IsModularLattice`: Modular lattices. Lattices where `a ≤ c → (a ⊔ b) ⊓ c = a ⊔ (b ⊓ c)`. We only require an inequality because the other direction holds in all lattices. ## Main Definitions - `infIccOrderIsoIccSup` gives an order isomorphism between the intervals `[a ⊓ b, a]` and `[b, a ⊔ b]`. This corresponds to the diamond (or second) isomorphism theorems of algebra. ## Main Results - `isModularLattice_iff_inf_sup_inf_assoc`: Modularity is equivalent to the `inf_sup_inf_assoc`: `(x ⊓ z) ⊔ (y ⊓ z) = ((x ⊓ z) ⊔ y) ⊓ z` - `DistribLattice.isModularLattice`: Distributive lattices are modular. ## References * [Manfred Stern, *Semimodular lattices. {Theory} and applications*][stern2009] * [Wikipedia, *Modular Lattice*][https://en.wikipedia.org/wiki/Modular_lattice] ## TODO - Relate atoms and coatoms in modular lattices -/ open Set variable {α : Type*} /-- A weakly upper modular lattice is a lattice where `a ⊔ b` covers `a` and `b` if `a` and `b` both cover `a ⊓ b`. -/ class IsWeakUpperModularLattice (α : Type*) [Lattice α] : Prop where /-- `a ⊔ b` covers `a` and `b` if `a` and `b` both cover `a ⊓ b`. -/ covBy_sup_of_inf_covBy_covBy {a b : α} : a ⊓ b ⋖ a → a ⊓ b ⋖ b → a ⋖ a ⊔ b /-- A weakly lower modular lattice is a lattice where `a` and `b` cover `a ⊓ b` if `a ⊔ b` covers both `a` and `b`. -/ class IsWeakLowerModularLattice (α : Type*) [Lattice α] : Prop where /-- `a` and `b` cover `a ⊓ b` if `a ⊔ b` covers both `a` and `b` -/ inf_covBy_of_covBy_covBy_sup {a b : α} : a ⋖ a ⊔ b → b ⋖ a ⊔ b → a ⊓ b ⋖ a /-- An upper modular lattice, aka semimodular lattice, is a lattice where `a ⊔ b` covers `a` and `b` if either `a` or `b` covers `a ⊓ b`. -/ class IsUpperModularLattice (α : Type*) [Lattice α] : Prop where /-- `a ⊔ b` covers `a` and `b` if either `a` or `b` covers `a ⊓ b` -/ covBy_sup_of_inf_covBy {a b : α} : a ⊓ b ⋖ a → b ⋖ a ⊔ b /-- A lower modular lattice is a lattice where `a` and `b` both cover `a ⊓ b` if `a ⊔ b` covers either `a` or `b`. -/ class IsLowerModularLattice (α : Type*) [Lattice α] : Prop where /-- `a` and `b` both cover `a ⊓ b` if `a ⊔ b` covers either `a` or `b` -/ inf_covBy_of_covBy_sup {a b : α} : a ⋖ a ⊔ b → a ⊓ b ⋖ b /-- A modular lattice is one with a limited associativity between `⊓` and `⊔`. -/ class IsModularLattice (α : Type*) [Lattice α] : Prop where /-- Whenever `x ≤ z`, then for any `y`, `(x ⊔ y) ⊓ z ≤ x ⊔ (y ⊓ z)` -/ sup_inf_le_assoc_of_le : ∀ {x : α} (y : α) {z : α}, x ≤ z → (x ⊔ y) ⊓ z ≤ x ⊔ y ⊓ z section WeakUpperModular variable [Lattice α] [IsWeakUpperModularLattice α] {a b : α} theorem covBy_sup_of_inf_covBy_of_inf_covBy_left : a ⊓ b ⋖ a → a ⊓ b ⋖ b → a ⋖ a ⊔ b := IsWeakUpperModularLattice.covBy_sup_of_inf_covBy_covBy theorem covBy_sup_of_inf_covBy_of_inf_covBy_right : a ⊓ b ⋖ a → a ⊓ b ⋖ b → b ⋖ a ⊔ b := by rw [inf_comm, sup_comm] exact fun ha hb => covBy_sup_of_inf_covBy_of_inf_covBy_left hb ha alias CovBy.sup_of_inf_of_inf_left := covBy_sup_of_inf_covBy_of_inf_covBy_left alias CovBy.sup_of_inf_of_inf_right := covBy_sup_of_inf_covBy_of_inf_covBy_right instance : IsWeakLowerModularLattice (OrderDual α) := ⟨fun ha hb => (ha.ofDual.sup_of_inf_of_inf_left hb.ofDual).toDual⟩ end WeakUpperModular section WeakLowerModular variable [Lattice α] [IsWeakLowerModularLattice α] {a b : α} theorem inf_covBy_of_covBy_sup_of_covBy_sup_left : a ⋖ a ⊔ b → b ⋖ a ⊔ b → a ⊓ b ⋖ a := IsWeakLowerModularLattice.inf_covBy_of_covBy_covBy_sup theorem inf_covBy_of_covBy_sup_of_covBy_sup_right : a ⋖ a ⊔ b → b ⋖ a ⊔ b → a ⊓ b ⋖ b := by rw [sup_comm, inf_comm] exact fun ha hb => inf_covBy_of_covBy_sup_of_covBy_sup_left hb ha alias CovBy.inf_of_sup_of_sup_left := inf_covBy_of_covBy_sup_of_covBy_sup_left alias CovBy.inf_of_sup_of_sup_right := inf_covBy_of_covBy_sup_of_covBy_sup_right instance : IsWeakUpperModularLattice (OrderDual α) := ⟨fun ha hb => (ha.ofDual.inf_of_sup_of_sup_left hb.ofDual).toDual⟩ end WeakLowerModular section UpperModular variable [Lattice α] [IsUpperModularLattice α] {a b : α} theorem covBy_sup_of_inf_covBy_left : a ⊓ b ⋖ a → b ⋖ a ⊔ b := IsUpperModularLattice.covBy_sup_of_inf_covBy theorem covBy_sup_of_inf_covBy_right : a ⊓ b ⋖ b → a ⋖ a ⊔ b := by rw [sup_comm, inf_comm] exact covBy_sup_of_inf_covBy_left alias CovBy.sup_of_inf_left := covBy_sup_of_inf_covBy_left alias CovBy.sup_of_inf_right := covBy_sup_of_inf_covBy_right -- See note [lower instance priority] instance (priority := 100) IsUpperModularLattice.to_isWeakUpperModularLattice : IsWeakUpperModularLattice α := ⟨fun _ => CovBy.sup_of_inf_right⟩ instance : IsLowerModularLattice (OrderDual α) := ⟨fun h => h.ofDual.sup_of_inf_left.toDual⟩ end UpperModular section LowerModular variable [Lattice α] [IsLowerModularLattice α] {a b : α} theorem inf_covBy_of_covBy_sup_left : a ⋖ a ⊔ b → a ⊓ b ⋖ b := IsLowerModularLattice.inf_covBy_of_covBy_sup theorem inf_covBy_of_covBy_sup_right : b ⋖ a ⊔ b → a ⊓ b ⋖ a := by rw [inf_comm, sup_comm] exact inf_covBy_of_covBy_sup_left alias CovBy.inf_of_sup_left := inf_covBy_of_covBy_sup_left alias CovBy.inf_of_sup_right := inf_covBy_of_covBy_sup_right -- See note [lower instance priority] instance (priority := 100) IsLowerModularLattice.to_isWeakLowerModularLattice : IsWeakLowerModularLattice α := ⟨fun _ => CovBy.inf_of_sup_right⟩ instance : IsUpperModularLattice (OrderDual α) := ⟨fun h => h.ofDual.inf_of_sup_left.toDual⟩ end LowerModular section IsModularLattice variable [Lattice α] [IsModularLattice α] theorem sup_inf_assoc_of_le {x : α} (y : α) {z : α} (h : x ≤ z) : (x ⊔ y) ⊓ z = x ⊔ y ⊓ z := le_antisymm (IsModularLattice.sup_inf_le_assoc_of_le y h) (le_inf (sup_le_sup_left inf_le_left _) (sup_le h inf_le_right)) theorem IsModularLattice.inf_sup_inf_assoc {x y z : α} : x ⊓ z ⊔ y ⊓ z = (x ⊓ z ⊔ y) ⊓ z := (sup_inf_assoc_of_le y inf_le_right).symm theorem inf_sup_assoc_of_le {x : α} (y : α) {z : α} (h : z ≤ x) : x ⊓ y ⊔ z = x ⊓ (y ⊔ z) := by rw [inf_comm, sup_comm, ← sup_inf_assoc_of_le y h, inf_comm, sup_comm] instance : IsModularLattice αᵒᵈ := ⟨fun y z xz => le_of_eq (by rw [inf_comm, sup_comm, eq_comm, inf_comm, sup_comm] exact @sup_inf_assoc_of_le α _ _ _ y _ xz)⟩ variable {x y z : α} theorem IsModularLattice.sup_inf_sup_assoc : (x ⊔ z) ⊓ (y ⊔ z) = (x ⊔ z) ⊓ y ⊔ z := @IsModularLattice.inf_sup_inf_assoc αᵒᵈ _ _ _ _ _ theorem eq_of_le_of_inf_le_of_le_sup (hxy : x ≤ y) (hinf : y ⊓ z ≤ x) (hsup : y ≤ x ⊔ z) : x = y := by refine hxy.antisymm ?_ rw [← inf_eq_right, sup_inf_assoc_of_le _ hxy] at hsup rwa [← hsup, sup_le_iff, and_iff_right rfl.le, inf_comm] theorem eq_of_le_of_inf_le_of_sup_le (hxy : x ≤ y) (hinf : y ⊓ z ≤ x ⊓ z) (hsup : y ⊔ z ≤ x ⊔ z) : x = y := eq_of_le_of_inf_le_of_le_sup hxy (hinf.trans inf_le_left) (le_sup_left.trans hsup) theorem sup_lt_sup_of_lt_of_inf_le_inf (hxy : x < y) (hinf : y ⊓ z ≤ x ⊓ z) : x ⊔ z < y ⊔ z := lt_of_le_of_ne (sup_le_sup_right (le_of_lt hxy) _) fun hsup => ne_of_lt hxy <| eq_of_le_of_inf_le_of_sup_le (le_of_lt hxy) hinf (le_of_eq hsup.symm) theorem inf_lt_inf_of_lt_of_sup_le_sup (hxy : x < y) (hinf : y ⊔ z ≤ x ⊔ z) : x ⊓ z < y ⊓ z := sup_lt_sup_of_lt_of_inf_le_inf (α := αᵒᵈ) hxy hinf theorem strictMono_inf_prod_sup : StrictMono fun x ↦ (x ⊓ z, x ⊔ z) := fun _x _y hxy ↦ ⟨⟨inf_le_inf_right _ hxy.le, sup_le_sup_right hxy.le _⟩, fun ⟨inf_le, sup_le⟩ ↦ (sup_lt_sup_of_lt_of_inf_le_inf hxy inf_le).not_le sup_le⟩ /-- A generalization of the theorem that if `N` is a submodule of `M` and `N` and `M / N` are both Artinian, then `M` is Artinian. -/ theorem wellFounded_lt_exact_sequence {β γ : Type*} [Preorder β] [Preorder γ] [h₁ : WellFoundedLT β] [h₂ : WellFoundedLT γ] (K : α) (f₁ : β → α) (f₂ : α → β) (g₁ : γ → α) (g₂ : α → γ) (gci : GaloisCoinsertion f₁ f₂) (gi : GaloisInsertion g₂ g₁) (hf : ∀ a, f₁ (f₂ a) = a ⊓ K) (hg : ∀ a, g₁ (g₂ a) = a ⊔ K) : WellFoundedLT α := StrictMono.wellFoundedLT (f := fun A ↦ (f₂ A, g₂ A)) fun A B hAB ↦ by simp only [Prod.le_def, lt_iff_le_not_le, ← gci.l_le_l_iff, ← gi.u_le_u_iff, hf, hg] exact strictMono_inf_prod_sup hAB /-- A generalization of the theorem that if `N` is a submodule of `M` and `N` and `M / N` are both Noetherian, then `M` is Noetherian. -/ theorem wellFounded_gt_exact_sequence {β γ : Type*} [Preorder β] [Preorder γ] [WellFoundedGT β] [WellFoundedGT γ] (K : α) (f₁ : β → α) (f₂ : α → β) (g₁ : γ → α) (g₂ : α → γ) (gci : GaloisCoinsertion f₁ f₂) (gi : GaloisInsertion g₂ g₁) (hf : ∀ a, f₁ (f₂ a) = a ⊓ K) (hg : ∀ a, g₁ (g₂ a) = a ⊔ K) : WellFoundedGT α := wellFounded_lt_exact_sequence (α := αᵒᵈ) (β := γᵒᵈ) (γ := βᵒᵈ) K g₁ g₂ f₁ f₂ gi.dual gci.dual hg hf /-- The diamond isomorphism between the intervals `[a ⊓ b, a]` and `[b, a ⊔ b]` -/ @[simps] def infIccOrderIsoIccSup (a b : α) : Set.Icc (a ⊓ b) a ≃o Set.Icc b (a ⊔ b) where toFun x := ⟨x ⊔ b, ⟨le_sup_right, sup_le_sup_right x.prop.2 b⟩⟩ invFun x := ⟨a ⊓ x, ⟨inf_le_inf_left a x.prop.1, inf_le_left⟩⟩ left_inv x := Subtype.ext (by change a ⊓ (↑x ⊔ b) = ↑x rw [sup_comm, ← inf_sup_assoc_of_le _ x.prop.2, sup_eq_right.2 x.prop.1]) right_inv x := Subtype.ext (by change a ⊓ ↑x ⊔ b = ↑x rw [inf_comm, inf_sup_assoc_of_le _ x.prop.1, inf_eq_left.2 x.prop.2]) map_rel_iff' {x y} := by simp only [Subtype.mk_le_mk, Equiv.coe_fn_mk, le_sup_right] rw [← Subtype.coe_le_coe] refine ⟨fun h => ?_, fun h => sup_le_sup_right h _⟩ rw [← sup_eq_right.2 x.prop.1, inf_sup_assoc_of_le _ x.prop.2, sup_comm, ← sup_eq_right.2 y.prop.1, inf_sup_assoc_of_le _ y.prop.2, sup_comm b] exact inf_le_inf_left _ h theorem inf_strictMonoOn_Icc_sup {a b : α} : StrictMonoOn (fun c => a ⊓ c) (Icc b (a ⊔ b)) := StrictMono.of_restrict (infIccOrderIsoIccSup a b).symm.strictMono theorem sup_strictMonoOn_Icc_inf {a b : α} : StrictMonoOn (fun c => c ⊔ b) (Icc (a ⊓ b) a) := StrictMono.of_restrict (infIccOrderIsoIccSup a b).strictMono /-- The diamond isomorphism between the intervals `]a ⊓ b, a[` and `}b, a ⊔ b[`. -/ @[simps] def infIooOrderIsoIooSup (a b : α) : Ioo (a ⊓ b) a ≃o Ioo b (a ⊔ b) where toFun c := ⟨c ⊔ b, le_sup_right.trans_lt <| sup_strictMonoOn_Icc_inf (left_mem_Icc.2 inf_le_left) (Ioo_subset_Icc_self c.2) c.2.1, sup_strictMonoOn_Icc_inf (Ioo_subset_Icc_self c.2) (right_mem_Icc.2 inf_le_left) c.2.2⟩ invFun c := ⟨a ⊓ c, inf_strictMonoOn_Icc_sup (left_mem_Icc.2 le_sup_right) (Ioo_subset_Icc_self c.2) c.2.1, inf_le_left.trans_lt' <| inf_strictMonoOn_Icc_sup (Ioo_subset_Icc_self c.2) (right_mem_Icc.2 le_sup_right) c.2.2⟩ left_inv c := Subtype.ext <| by dsimp rw [sup_comm, ← inf_sup_assoc_of_le _ c.prop.2.le, sup_eq_right.2 c.prop.1.le] right_inv c := Subtype.ext <| by dsimp rw [inf_comm, inf_sup_assoc_of_le _ c.prop.1.le, inf_eq_left.2 c.prop.2.le] map_rel_iff' := @fun c d => @OrderIso.le_iff_le _ _ _ _ (infIccOrderIsoIccSup _ _) ⟨c.1, Ioo_subset_Icc_self c.2⟩ ⟨d.1, Ioo_subset_Icc_self d.2⟩ -- See note [lower instance priority] instance (priority := 100) IsModularLattice.to_isLowerModularLattice : IsLowerModularLattice α := ⟨fun {a b} => by simp_rw [covBy_iff_Ioo_eq, sup_comm a, inf_comm a, ← isEmpty_coe_sort, right_lt_sup, inf_lt_left, (infIooOrderIsoIooSup b a).symm.toEquiv.isEmpty_congr] exact id⟩ -- See note [lower instance priority] instance (priority := 100) IsModularLattice.to_isUpperModularLattice : IsUpperModularLattice α := ⟨fun {a b} => by simp_rw [covBy_iff_Ioo_eq, ← isEmpty_coe_sort, right_lt_sup, inf_lt_left, (infIooOrderIsoIooSup a b).toEquiv.isEmpty_congr] exact id⟩ end IsModularLattice namespace IsCompl variable [Lattice α] [BoundedOrder α] [IsModularLattice α] /-- The diamond isomorphism between the intervals `Set.Iic a` and `Set.Ici b`. -/ def IicOrderIsoIci {a b : α} (h : IsCompl a b) : Set.Iic a ≃o Set.Ici b := (OrderIso.setCongr (Set.Iic a) (Set.Icc (a ⊓ b) a) (h.inf_eq_bot.symm ▸ Set.Icc_bot.symm)).trans <| (infIccOrderIsoIccSup a b).trans (OrderIso.setCongr (Set.Icc b (a ⊔ b)) (Set.Ici b) (h.sup_eq_top.symm ▸ Set.Icc_top)) end IsCompl theorem isModularLattice_iff_inf_sup_inf_assoc [Lattice α] : IsModularLattice α ↔ ∀ x y z : α, x ⊓ z ⊔ y ⊓ z = (x ⊓ z ⊔ y) ⊓ z := ⟨fun h => @IsModularLattice.inf_sup_inf_assoc _ _ h, fun h => ⟨fun y z xz => by rw [← inf_eq_left.2 xz, h]⟩⟩ namespace DistribLattice instance (priority := 100) [DistribLattice α] : IsModularLattice α := ⟨fun y z xz => by rw [inf_sup_right, inf_eq_left.2 xz]⟩ end DistribLattice namespace Disjoint variable {a b c : α} theorem disjoint_sup_right_of_disjoint_sup_left [Lattice α] [OrderBot α] [IsModularLattice α] (h : Disjoint a b) (hsup : Disjoint (a ⊔ b) c) : Disjoint a (b ⊔ c) := by rw [disjoint_iff_inf_le, ← h.eq_bot, sup_comm] apply le_inf inf_le_left apply (inf_le_inf_right (c ⊔ b) le_sup_right).trans rw [sup_comm, IsModularLattice.sup_inf_sup_assoc, hsup.eq_bot, bot_sup_eq] theorem disjoint_sup_left_of_disjoint_sup_right [Lattice α] [OrderBot α] [IsModularLattice α] (h : Disjoint b c) (hsup : Disjoint a (b ⊔ c)) : Disjoint (a ⊔ b) c := by rw [disjoint_comm, sup_comm] apply Disjoint.disjoint_sup_right_of_disjoint_sup_left h.symm rwa [sup_comm, disjoint_comm] at hsup theorem isCompl_sup_right_of_isCompl_sup_left [Lattice α] [BoundedOrder α] [IsModularLattice α] (h : Disjoint a b) (hcomp : IsCompl (a ⊔ b) c) : IsCompl a (b ⊔ c) := ⟨h.disjoint_sup_right_of_disjoint_sup_left hcomp.disjoint, codisjoint_assoc.mp hcomp.codisjoint⟩ theorem isCompl_sup_left_of_isCompl_sup_right [Lattice α] [BoundedOrder α] [IsModularLattice α] (h : Disjoint b c) (hcomp : IsCompl a (b ⊔ c)) : IsCompl (a ⊔ b) c := ⟨h.disjoint_sup_left_of_disjoint_sup_right hcomp.disjoint, codisjoint_assoc.mpr hcomp.codisjoint⟩ end Disjoint lemma Set.Iic.isCompl_inf_inf_of_isCompl_of_le [Lattice α] [BoundedOrder α] [IsModularLattice α] {a b c : α} (h₁ : IsCompl b c) (h₂ : b ≤ a) : IsCompl (⟨a ⊓ b, inf_le_left⟩ : Iic a) (⟨a ⊓ c, inf_le_left⟩ : Iic a) := by constructor · simp [disjoint_iff, Subtype.ext_iff, inf_comm a c, inf_assoc a, ← inf_assoc b, h₁.inf_eq_bot] · simp only [Iic.codisjoint_iff, inf_comm a, IsModularLattice.inf_sup_inf_assoc] simp [inf_of_le_left h₂, h₁.sup_eq_top] namespace IsModularLattice variable [Lattice α] [IsModularLattice α] {a : α} instance isModularLattice_Iic : IsModularLattice (Set.Iic a) := ⟨@fun x y z xz => (sup_inf_le_assoc_of_le (y : α) xz : (↑x ⊔ ↑y) ⊓ ↑z ≤ ↑x ⊔ ↑y ⊓ ↑z)⟩ instance isModularLattice_Ici : IsModularLattice (Set.Ici a) := ⟨@fun x y z xz => (sup_inf_le_assoc_of_le (y : α) xz : (↑x ⊔ ↑y) ⊓ ↑z ≤ ↑x ⊔ ↑y ⊓ ↑z)⟩ section ComplementedLattice variable [BoundedOrder α] [ComplementedLattice α]
instance complementedLattice_Iic : ComplementedLattice (Set.Iic a) := ⟨fun ⟨x, hx⟩ => let ⟨y, hy⟩ := exists_isCompl x ⟨⟨y ⊓ a, Set.mem_Iic.2 inf_le_right⟩, by constructor · rw [disjoint_iff_inf_le]
Mathlib/Order/ModularLattice.lean
393
399
/- Copyright (c) 2022 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import Mathlib.Data.Fintype.Card import Mathlib.Order.UpperLower.Basic /-! # Intersecting families This file defines intersecting families and proves their basic properties. ## Main declarations * `Set.Intersecting`: Predicate for a set of elements in a generalized boolean algebra to be an intersecting family. * `Set.Intersecting.card_le`: An intersecting family can only take up to half the elements, because `a` and `aᶜ` cannot simultaneously be in it. * `Set.Intersecting.is_max_iff_card_eq`: Any maximal intersecting family takes up half the elements. ## References * [D. J. Kleitman, *Families of non-disjoint subsets*][kleitman1966] -/ assert_not_exists Monoid open Finset variable {α : Type*} namespace Set section SemilatticeInf variable [SemilatticeInf α] [OrderBot α] {s t : Set α} {a b c : α} /-- A set family is intersecting if every pair of elements is non-disjoint. -/ def Intersecting (s : Set α) : Prop := ∀ ⦃a⦄, a ∈ s → ∀ ⦃b⦄, b ∈ s → ¬Disjoint a b @[mono] theorem Intersecting.mono (h : t ⊆ s) (hs : s.Intersecting) : t.Intersecting := fun _a ha _b hb => hs (h ha) (h hb) theorem Intersecting.not_bot_mem (hs : s.Intersecting) : ⊥ ∉ s := fun h => hs h h disjoint_bot_left theorem Intersecting.ne_bot (hs : s.Intersecting) (ha : a ∈ s) : a ≠ ⊥ := ne_of_mem_of_not_mem ha hs.not_bot_mem theorem intersecting_empty : (∅ : Set α).Intersecting := fun _ => False.elim @[simp] theorem intersecting_singleton : ({a} : Set α).Intersecting ↔ a ≠ ⊥ := by simp [Intersecting] protected theorem Intersecting.insert (hs : s.Intersecting) (ha : a ≠ ⊥) (h : ∀ b ∈ s, ¬Disjoint a b) : (insert a s).Intersecting := by rintro b (rfl | hb) c (rfl | hc) · rwa [disjoint_self] · exact h _ hc · exact fun H => h _ hb H.symm · exact hs hb hc theorem intersecting_insert : (insert a s).Intersecting ↔ s.Intersecting ∧ a ≠ ⊥ ∧ ∀ b ∈ s, ¬Disjoint a b := ⟨fun h => ⟨h.mono <| subset_insert _ _, h.ne_bot <| mem_insert _ _, fun _b hb => h (mem_insert _ _) <| mem_insert_of_mem _ hb⟩, fun h => h.1.insert h.2.1 h.2.2⟩ theorem intersecting_iff_pairwise_not_disjoint : s.Intersecting ↔ (s.Pairwise fun a b => ¬Disjoint a b) ∧ s ≠ {⊥} := by refine ⟨fun h => ⟨fun a ha b hb _ => h ha hb, ?_⟩, fun h a ha b hb hab => ?_⟩ · rintro rfl exact intersecting_singleton.1 h rfl have := h.1.eq ha hb (Classical.not_not.2 hab) rw [this, disjoint_self] at hab rw [hab] at hb exact h.2 (eq_singleton_iff_unique_mem.2 ⟨hb, fun c hc => not_ne_iff.1 fun H => h.1 hb hc H.symm disjoint_bot_left⟩) protected theorem Subsingleton.intersecting (hs : s.Subsingleton) : s.Intersecting ↔ s ≠ {⊥} := intersecting_iff_pairwise_not_disjoint.trans <| and_iff_right <| hs.pairwise _ theorem intersecting_iff_eq_empty_of_subsingleton [Subsingleton α] (s : Set α) : s.Intersecting ↔ s = ∅ := by refine subsingleton_of_subsingleton.intersecting.trans ⟨not_imp_comm.2 fun h => subsingleton_of_subsingleton.eq_singleton_of_mem ?_, ?_⟩ · obtain ⟨a, ha⟩ := nonempty_iff_ne_empty.2 h rwa [Subsingleton.elim ⊥ a] · rintro rfl exact (Set.singleton_nonempty _).ne_empty.symm /-- Maximal intersecting families are upper sets. -/
protected theorem Intersecting.isUpperSet (hs : s.Intersecting) (h : ∀ t : Set α, t.Intersecting → s ⊆ t → s = t) : IsUpperSet s := by classical rintro a b hab ha rw [h (Insert.insert b s) _ (subset_insert _ _)] · exact mem_insert _ _ exact hs.insert (mt (eq_bot_mono hab) <| hs.ne_bot ha) fun c hc hbc => hs ha hc <| hbc.mono_left hab
Mathlib/Combinatorics/SetFamily/Intersecting.lean
99
107
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import Mathlib.MeasureTheory.Measure.Dirac import Mathlib.Topology.Algebra.InfiniteSum.ENNReal /-! # Counting measure In this file we define the counting measure `MeasurTheory.Measure.count` as `MeasureTheory.Measure.sum MeasureTheory.Measure.dirac` and prove basic properties of this measure. -/ open Set open scoped ENNReal Finset variable {α β : Type*} [MeasurableSpace α] [MeasurableSpace β] {s : Set α} noncomputable section namespace MeasureTheory.Measure /-- Counting measure on any measurable space. -/ def count : Measure α := sum dirac @[simp] lemma count_ne_zero'' [Nonempty α] : (count : Measure α) ≠ 0 := by simp [count] theorem le_count_apply : ∑' _ : s, (1 : ℝ≥0∞) ≤ count s := calc (∑' _ : s, 1 : ℝ≥0∞) = ∑' i, indicator s 1 i := tsum_subtype s 1 _ ≤ ∑' i, dirac i s := ENNReal.tsum_le_tsum fun _ => le_dirac_apply _ ≤ count s := le_sum_apply _ _ theorem count_apply (hs : MeasurableSet s) : count s = s.encard := by simp [count, hs, ← tsum_subtype, Set.encard] @[deprecated measure_empty (since := "2025-02-06")] theorem count_empty : count (∅ : Set α) = 0 := measure_empty @[simp] theorem count_apply_finset' {s : Finset α} (hs : MeasurableSet (s : Set α)) : count (↑s : Set α) = #s := by simp [count_apply hs] @[simp] theorem count_apply_finset [MeasurableSingletonClass α] (s : Finset α) : count (↑s : Set α) = #s := count_apply_finset' s.measurableSet theorem count_apply_finite' {s : Set α} (s_fin : s.Finite) (s_mble : MeasurableSet s) : count s = #s_fin.toFinset := by simp [← @count_apply_finset' _ _ s_fin.toFinset (by simpa only [Finite.coe_toFinset] using s_mble)] theorem count_apply_finite [MeasurableSingletonClass α] (s : Set α) (hs : s.Finite) : count s = #hs.toFinset := by rw [← count_apply_finset, Finite.coe_toFinset] /-- `count` measure evaluates to infinity at infinite sets. -/ theorem count_apply_infinite (hs : s.Infinite) : count s = ∞ := by refine top_unique (le_of_tendsto' ENNReal.tendsto_nat_nhds_top fun n => ?_) rcases hs.exists_subset_card_eq n with ⟨t, ht, rfl⟩ calc (#t : ℝ≥0∞) = ∑ i ∈ t, 1 := by simp _ = ∑' i : (t : Set α), 1 := (t.tsum_subtype 1).symm _ ≤ count (t : Set α) := le_count_apply _ ≤ count s := measure_mono ht
@[simp] theorem count_apply_eq_top' (s_mble : MeasurableSet s) : count s = ∞ ↔ s.Infinite := by by_cases hs : s.Finite · simp [Set.Infinite, hs, count_apply_finite' hs s_mble] · change s.Infinite at hs simp [hs, count_apply_infinite] @[simp]
Mathlib/MeasureTheory/Measure/Count.lean
71
78
/- Copyright (c) 2021 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import Mathlib.Algebra.Quaternion import Mathlib.Tactic.Ring /-! # Basis on a quaternion-like algebra ## Main definitions * `QuaternionAlgebra.Basis A c₁ c₂ c₃`: a basis for a subspace of an `R`-algebra `A` that has the same algebra structure as `ℍ[R,c₁,c₂,c₃]`. * `QuaternionAlgebra.Basis.self R`: the canonical basis for `ℍ[R,c₁,c₂,c₃]`. * `QuaternionAlgebra.Basis.compHom b f`: transform a basis `b` by an AlgHom `f`. * `QuaternionAlgebra.lift`: Define an `AlgHom` out of `ℍ[R,c₁,c₂,c₃]` by its action on the basis elements `i`, `j`, and `k`. In essence, this is a universal property. Analogous to `Complex.lift`, but takes a bundled `QuaternionAlgebra.Basis` instead of just a `Subtype` as the amount of data / proves is non-negligible. -/ open Quaternion namespace QuaternionAlgebra /-- A quaternion basis contains the information both sufficient and necessary to construct an `R`-algebra homomorphism from `ℍ[R,c₁,c₂,c₃]` to `A`; or equivalently, a surjective `R`-algebra homomorphism from `ℍ[R,c₁,c₂,c₃]` to an `R`-subalgebra of `A`. Note that for definitional convenience, `k` is provided as a field even though `i_mul_j` fully determines it. -/ structure Basis {R : Type*} (A : Type*) [CommRing R] [Ring A] [Algebra R A] (c₁ c₂ c₃ : R) where /-- The first imaginary unit -/ i : A /-- The second imaginary unit -/ j : A /-- The third imaginary unit -/ k : A i_mul_i : i * i = c₁ • (1 : A) + c₂ • i j_mul_j : j * j = c₃ • (1 : A) i_mul_j : i * j = k j_mul_i : j * i = c₂ • j - k variable {R : Type*} {A B : Type*} [CommRing R] [Ring A] [Ring B] [Algebra R A] [Algebra R B] variable {c₁ c₂ c₃ : R} namespace Basis /-- Since `k` is redundant, it is not necessary to show `q₁.k = q₂.k` when showing `q₁ = q₂`. -/ @[ext] protected theorem ext ⦃q₁ q₂ : Basis A c₁ c₂ c₃⦄ (hi : q₁.i = q₂.i) (hj : q₁.j = q₂.j) : q₁ = q₂ := by cases q₁; rename_i q₁_i_mul_j _ cases q₂; rename_i q₂_i_mul_j _ congr rw [← q₁_i_mul_j, ← q₂_i_mul_j] congr variable (R) in /-- There is a natural quaternionic basis for the `QuaternionAlgebra`. -/ @[simps i j k] protected def self : Basis ℍ[R,c₁,c₂,c₃] c₁ c₂ c₃ where i := ⟨0, 1, 0, 0⟩ i_mul_i := by ext <;> simp j := ⟨0, 0, 1, 0⟩ j_mul_j := by ext <;> simp k := ⟨0, 0, 0, 1⟩ i_mul_j := by ext <;> simp j_mul_i := by ext <;> simp instance : Inhabited (Basis ℍ[R,c₁,c₂,c₃] c₁ c₂ c₃) := ⟨Basis.self R⟩ variable (q : Basis A c₁ c₂ c₃) attribute [simp] i_mul_i j_mul_j i_mul_j j_mul_i @[simp] theorem i_mul_k : q.i * q.k = c₁ • q.j + c₂ • q.k := by rw [← i_mul_j, ← mul_assoc, i_mul_i, add_mul, smul_mul_assoc, one_mul, smul_mul_assoc] @[simp] theorem k_mul_i : q.k * q.i = -c₁ • q.j := by rw [← i_mul_j, mul_assoc, j_mul_i, mul_sub, i_mul_k, neg_smul, mul_smul_comm, i_mul_j] linear_combination (norm := module)
@[simp]
Mathlib/Algebra/QuaternionBasis.lean
89
90
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Johannes Hölzl, Sander Dahmen, Kim Morrison -/ import Mathlib.SetTheory.Cardinal.Cofinality import Mathlib.LinearAlgebra.FreeModule.Finite.Basic import Mathlib.LinearAlgebra.Dimension.StrongRankCondition import Mathlib.LinearAlgebra.Dimension.Constructions /-! # Conditions for rank to be finite Also contains characterization for when rank equals zero or rank equals one. -/ noncomputable section universe u v v' w variable {R : Type u} {M M₁ : Type v} {M' : Type v'} {ι : Type w} variable [Ring R] [AddCommGroup M] [AddCommGroup M'] [AddCommGroup M₁] variable [Module R M] [Module R M'] [Module R M₁] attribute [local instance] nontrivial_of_invariantBasisNumber open Basis Cardinal Function Module Set Submodule /-- If every finite set of linearly independent vectors has cardinality at most `n`, then the same is true for arbitrary sets of linearly independent vectors. -/ theorem linearIndependent_bounded_of_finset_linearIndependent_bounded {n : ℕ} (H : ∀ s : Finset M, (LinearIndependent R fun i : s => (i : M)) → s.card ≤ n) : ∀ s : Set M, LinearIndependent R ((↑) : s → M) → #s ≤ n := by intro s li apply Cardinal.card_le_of intro t rw [← Finset.card_map (Embedding.subtype s)] apply H apply linearIndependent_finset_map_embedding_subtype _ li theorem rank_le {n : ℕ} (H : ∀ s : Finset M, (LinearIndependent R fun i : s => (i : M)) → s.card ≤ n) : Module.rank R M ≤ n := by rw [Module.rank_def] apply ciSup_le' rintro ⟨s, li⟩ exact linearIndependent_bounded_of_finset_linearIndependent_bounded H _ li section RankZero /-- See `rank_zero_iff` for a stronger version with `NoZeroSMulDivisor R M`. -/ lemma rank_eq_zero_iff : Module.rank R M = 0 ↔ ∀ x : M, ∃ a : R, a ≠ 0 ∧ a • x = 0 := by nontriviality R constructor · contrapose! rintro ⟨x, hx⟩ rw [← Cardinal.one_le_iff_ne_zero] have : LinearIndependent R (fun _ : Unit ↦ x) := linearIndependent_iff.mpr (fun l hl ↦ Finsupp.unique_ext <| not_not.mp fun H ↦ hx _ H ((Finsupp.linearCombination_unique _ _ _).symm.trans hl)) simpa using this.cardinal_lift_le_rank · intro h rw [← le_zero_iff, Module.rank_def] apply ciSup_le' intro ⟨s, hs⟩ rw [nonpos_iff_eq_zero, Cardinal.mk_eq_zero_iff, ← not_nonempty_iff] rintro ⟨i : s⟩ obtain ⟨a, ha, ha'⟩ := h i apply ha simpa using DFunLike.congr_fun (linearIndependent_iff.mp hs (Finsupp.single i a) (by simpa)) i theorem rank_pos_of_free [Module.Free R M] [Nontrivial M] : 0 < Module.rank R M := have := Module.nontrivial R M (pos_of_ne_zero <| Cardinal.mk_ne_zero _).trans_le (Free.chooseBasis R M).linearIndependent.cardinal_le_rank variable [Nontrivial R] section variable [NoZeroSMulDivisors R M] theorem rank_zero_iff_forall_zero : Module.rank R M = 0 ↔ ∀ x : M, x = 0 := by simp_rw [rank_eq_zero_iff, smul_eq_zero, and_or_left, not_and_self_iff, false_or, exists_and_right, and_iff_right (exists_ne (0 : R))] /-- See `rank_subsingleton` for the reason that `Nontrivial R` is needed. Also see `rank_eq_zero_iff` for the version without `NoZeroSMulDivisor R M`. -/ theorem rank_zero_iff : Module.rank R M = 0 ↔ Subsingleton M := rank_zero_iff_forall_zero.trans (subsingleton_iff_forall_eq 0).symm theorem rank_pos_iff_exists_ne_zero : 0 < Module.rank R M ↔ ∃ x : M, x ≠ 0 := by rw [← not_iff_not] simpa using rank_zero_iff_forall_zero theorem rank_pos_iff_nontrivial : 0 < Module.rank R M ↔ Nontrivial M := rank_pos_iff_exists_ne_zero.trans (nontrivial_iff_exists_ne 0).symm theorem rank_pos [Nontrivial M] : 0 < Module.rank R M := rank_pos_iff_nontrivial.mpr ‹_› end variable (R M) /-- See `rank_subsingleton` that assumes `Subsingleton R` instead. -/ @[nontriviality] theorem rank_subsingleton' [Subsingleton M] : Module.rank R M = 0 := rank_eq_zero_iff.mpr fun _ ↦ ⟨1, one_ne_zero, Subsingleton.elim _ _⟩ @[simp] theorem rank_punit : Module.rank R PUnit = 0 := rank_subsingleton' _ _ @[simp] theorem rank_bot : Module.rank R (⊥ : Submodule R M) = 0 := rank_subsingleton' _ _ variable {R M} theorem exists_mem_ne_zero_of_rank_pos {s : Submodule R M} (h : 0 < Module.rank R s) : ∃ b : M, b ∈ s ∧ b ≠ 0 := exists_mem_ne_zero_of_ne_bot fun eq => by rw [eq, rank_bot] at h; exact lt_irrefl _ h end RankZero section Finite theorem Module.finite_of_rank_eq_nat [Module.Free R M] {n : ℕ} (h : Module.rank R M = n) : Module.Finite R M := by nontriviality R obtain ⟨⟨ι, b⟩⟩ := Module.Free.exists_basis (R := R) (M := M) have := mk_lt_aleph0_iff.mp <| b.linearIndependent.cardinal_le_rank |>.trans_eq h |>.trans_lt <| nat_lt_aleph0 n exact Module.Finite.of_basis b theorem Module.finite_of_rank_eq_zero [NoZeroSMulDivisors R M] (h : Module.rank R M = 0) : Module.Finite R M := by nontriviality R rw [rank_zero_iff] at h infer_instance theorem Module.finite_of_rank_eq_one [Module.Free R M] (h : Module.rank R M = 1) : Module.Finite R M := Module.finite_of_rank_eq_nat <| h.trans Nat.cast_one.symm section variable [StrongRankCondition R] /-- If a module has a finite dimension, all bases are indexed by a finite type. -/ theorem Basis.nonempty_fintype_index_of_rank_lt_aleph0 {ι : Type*} (b : Basis ι R M) (h : Module.rank R M < ℵ₀) : Nonempty (Fintype ι) := by rwa [← Cardinal.lift_lt, ← b.mk_eq_rank, Cardinal.lift_aleph0, Cardinal.lift_lt_aleph0, Cardinal.lt_aleph0_iff_fintype] at h /-- If a module has a finite dimension, all bases are indexed by a finite type. -/ noncomputable def Basis.fintypeIndexOfRankLtAleph0 {ι : Type*} (b : Basis ι R M) (h : Module.rank R M < ℵ₀) : Fintype ι := Classical.choice (b.nonempty_fintype_index_of_rank_lt_aleph0 h) /-- If a module has a finite dimension, all bases are indexed by a finite set. -/ theorem Basis.finite_index_of_rank_lt_aleph0 {ι : Type*} {s : Set ι} (b : Basis s R M) (h : Module.rank R M < ℵ₀) : s.Finite := finite_def.2 (b.nonempty_fintype_index_of_rank_lt_aleph0 h) end namespace LinearIndependent variable [StrongRankCondition R] theorem cardinalMk_le_finrank [Module.Finite R M] {ι : Type w} {b : ι → M} (h : LinearIndependent R b) : #ι ≤ finrank R M := by rw [← lift_le.{max v w}] simpa only [← finrank_eq_rank, lift_natCast, lift_le_nat_iff] using h.cardinal_lift_le_rank @[deprecated (since := "2024-11-10")] alias cardinal_mk_le_finrank := cardinalMk_le_finrank theorem fintype_card_le_finrank [Module.Finite R M] {ι : Type*} [Fintype ι] {b : ι → M} (h : LinearIndependent R b) : Fintype.card ι ≤ finrank R M := by simpa using h.cardinalMk_le_finrank theorem finset_card_le_finrank [Module.Finite R M] {b : Finset M} (h : LinearIndependent R (fun x => x : b → M)) : b.card ≤ finrank R M := by rw [← Fintype.card_coe] exact h.fintype_card_le_finrank theorem lt_aleph0_of_finite {ι : Type w} [Module.Finite R M] {v : ι → M} (h : LinearIndependent R v) : #ι < ℵ₀ := by apply Cardinal.lift_lt.1 apply lt_of_le_of_lt · apply h.cardinal_lift_le_rank · rw [← finrank_eq_rank, Cardinal.lift_aleph0, Cardinal.lift_natCast] apply Cardinal.nat_lt_aleph0 theorem finite [Module.Finite R M] {ι : Type*} {f : ι → M} (h : LinearIndependent R f) : Finite ι := Cardinal.lt_aleph0_iff_finite.1 <| h.lt_aleph0_of_finite theorem setFinite [Module.Finite R M] {b : Set M} (h : LinearIndependent R fun x : b => (x : M)) : b.Finite := Cardinal.lt_aleph0_iff_set_finite.mp h.lt_aleph0_of_finite end LinearIndependent lemma exists_set_linearIndependent_of_lt_rank {n : Cardinal} (hn : n < Module.rank R M) : ∃ s : Set M, #s = n ∧ LinearIndepOn R id s := by obtain ⟨⟨s, hs⟩, hs'⟩ := exists_lt_of_lt_ciSup' (hn.trans_eq (Module.rank_def R M)) obtain ⟨t, ht, ht'⟩ := le_mk_iff_exists_subset.mp hs'.le exact ⟨t, ht', hs.mono ht⟩ lemma exists_finset_linearIndependent_of_le_rank {n : ℕ} (hn : n ≤ Module.rank R M) : ∃ s : Finset M, s.card = n ∧ LinearIndepOn R id (s : Set M) := by have := nonempty_linearIndependent_set rcases hn.eq_or_lt with h | h
· obtain ⟨⟨s, hs⟩, hs'⟩ := Cardinal.exists_eq_natCast_of_iSup_eq _ (Cardinal.bddAbove_range _) _ (h.trans (Module.rank_def R M)).symm have : Finite s := lt_aleph0_iff_finite.mp (hs' ▸ nat_lt_aleph0 n) cases nonempty_fintype s refine ⟨s.toFinset, by simpa using hs', by simpa⟩ · obtain ⟨s, hs, hs'⟩ := exists_set_linearIndependent_of_lt_rank h have : Finite s := lt_aleph0_iff_finite.mp (hs ▸ nat_lt_aleph0 n) cases nonempty_fintype s exact ⟨s.toFinset, by simpa using hs, by simpa⟩ lemma exists_linearIndependent_of_le_rank {n : ℕ} (hn : n ≤ Module.rank R M) : ∃ f : Fin n → M, LinearIndependent R f := have ⟨_, hs, hs'⟩ := exists_finset_linearIndependent_of_le_rank hn
Mathlib/LinearAlgebra/Dimension/Finite.lean
220
232
/- Copyright (c) 2024 Joël Riou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joël Riou -/ import Mathlib.CategoryTheory.Limits.Final import Mathlib.CategoryTheory.Functor.TwoSquare /-! # Guitart exact squares Given four functors `T`, `L`, `R` and `B`, a 2-square `TwoSquare T L R B` consists of a natural transformation `w : T ⋙ R ⟶ L ⋙ B`: ``` T C₁ ⥤ C₂ L | | R v v C₃ ⥤ C₄ B ``` In this file, we define a typeclass `w.GuitartExact` which expresses that this square is exact in the sense of Guitart. This means that for any `X₃ : C₃`, the induced functor `CostructuredArrow L X₃ ⥤ CostructuredArrow R (B.obj X₃)` is final. It is also equivalent to the fact that for any `X₂ : C₂`, the induced functor `StructuredArrow X₂ T ⥤ StructuredArrow (R.obj X₂) B` is initial. Various categorical notions (fully faithful functors, adjunctions, etc.) can be characterized in terms of Guitart exact squares. Their particular role in pointwise Kan extensions shall also be used in the construction of derived functors. ## TODO * Define the notion of derivability structure from [the paper by Kahn and Maltsiniotis][KahnMaltsiniotis2008] using Guitart exact squares and construct (pointwise) derived functors using this notion ## References * https://ncatlab.org/nlab/show/exact+square * [René Guitart, *Relations et carrés exacts*][Guitart1980] * [Bruno Kahn and Georges Maltsiniotis, *Structures de dérivabilité*][KahnMaltsiniotis2008] -/ universe v₁ v₂ v₃ v₄ u₁ u₂ u₃ u₄ namespace CategoryTheory open Category variable {C₁ : Type u₁} {C₂ : Type u₂} {C₃ : Type u₃} {C₄ : Type u₄} [Category.{v₁} C₁] [Category.{v₂} C₂] [Category.{v₃} C₃] [Category.{v₄} C₄] (T : C₁ ⥤ C₂) (L : C₁ ⥤ C₃) (R : C₂ ⥤ C₄) (B : C₃ ⥤ C₄) namespace TwoSquare variable {T L R B} (w : TwoSquare T L R B) /-- Given `w : TwoSquare T L R B` and `X₃ : C₃`, this is the obvious functor `CostructuredArrow L X₃ ⥤ CostructuredArrow R (B.obj X₃)`. -/ @[simps! obj map] def costructuredArrowRightwards (X₃ : C₃) : CostructuredArrow L X₃ ⥤ CostructuredArrow R (B.obj X₃) := CostructuredArrow.post L B X₃ ⋙ Comma.mapLeft _ w ⋙ CostructuredArrow.pre T R (B.obj X₃) /-- Given `w : TwoSquare T L R B` and `X₂ : C₂`, this is the obvious functor `StructuredArrow X₂ T ⥤ StructuredArrow (R.obj X₂) B`. -/ @[simps! obj map] def structuredArrowDownwards (X₂ : C₂) : StructuredArrow X₂ T ⥤ StructuredArrow (R.obj X₂) B := StructuredArrow.post X₂ T R ⋙ Comma.mapRight _ w ⋙ StructuredArrow.pre (R.obj X₂) L B section variable {X₂ : C₂} {X₃ : C₃} (g : R.obj X₂ ⟶ B.obj X₃) /- In [the paper by Kahn and Maltsiniotis, §4.3][KahnMaltsiniotis2008], given `w : TwoSquare T L R B` and `g : R.obj X₂ ⟶ B.obj X₃`, a category `J` is introduced and it is observed that it is equivalent to the two categories `w.StructuredArrowRightwards g` and `w.CostructuredArrowDownwards g`. We shall show below that there is an equivalence `w.equivalenceJ g : w.StructuredArrowRightwards g ≌ w.CostructuredArrowDownwards g`. -/ /-- Given `w : TwoSquare T L R B` and a morphism `g : R.obj X₂ ⟶ B.obj X₃`, this is the category `StructuredArrow (CostructuredArrow.mk g) (w.costructuredArrowRightwards X₃)`, see the constructor `StructuredArrowRightwards.mk` for the data that is involved. -/ abbrev StructuredArrowRightwards := StructuredArrow (CostructuredArrow.mk g) (w.costructuredArrowRightwards X₃) /-- Given `w : TwoSquare T L R B` and a morphism `g : R.obj X₂ ⟶ B.obj X₃`, this is the category `CostructuredArrow (w.structuredArrowDownwards X₂) (StructuredArrow.mk g)`, see the constructor `CostructuredArrowDownwards.mk` for the data that is involved. -/ abbrev CostructuredArrowDownwards := CostructuredArrow (w.structuredArrowDownwards X₂) (StructuredArrow.mk g) section variable (X₁ : C₁) (a : X₂ ⟶ T.obj X₁) (b : L.obj X₁ ⟶ X₃) /-- Constructor for objects in `w.StructuredArrowRightwards g`. -/ abbrev StructuredArrowRightwards.mk (comm : R.map a ≫ w.app X₁ ≫ B.map b = g) : w.StructuredArrowRightwards g := StructuredArrow.mk (Y := CostructuredArrow.mk b) (CostructuredArrow.homMk a comm) /-- Constructor for objects in `w.CostructuredArrowDownwards g`. -/ abbrev CostructuredArrowDownwards.mk (comm : R.map a ≫ w.app X₁ ≫ B.map b = g) : w.CostructuredArrowDownwards g := CostructuredArrow.mk (Y := StructuredArrow.mk a) (StructuredArrow.homMk b (by simpa using comm)) variable {w g} lemma StructuredArrowRightwards.mk_surjective (f : w.StructuredArrowRightwards g) : ∃ (X₁ : C₁) (a : X₂ ⟶ T.obj X₁) (b : L.obj X₁ ⟶ X₃) (comm : R.map a ≫ w.app X₁ ≫ B.map b = g), f = mk w g X₁ a b comm := by obtain ⟨g, φ, rfl⟩ := StructuredArrow.mk_surjective f obtain ⟨X₁, b, rfl⟩ := g.mk_surjective obtain ⟨a, ha, rfl⟩ := CostructuredArrow.homMk_surjective φ exact ⟨X₁, a, b, by simpa using ha, rfl⟩ lemma CostructuredArrowDownwards.mk_surjective (f : w.CostructuredArrowDownwards g) : ∃ (X₁ : C₁) (a : X₂ ⟶ T.obj X₁) (b : L.obj X₁ ⟶ X₃) (comm : R.map a ≫ w.app X₁ ≫ B.map b = g), f = mk w g X₁ a b comm := by obtain ⟨g, φ, rfl⟩ := CostructuredArrow.mk_surjective f obtain ⟨X₁, a, rfl⟩ := g.mk_surjective obtain ⟨b, hb, rfl⟩ := StructuredArrow.homMk_surjective φ exact ⟨X₁, a, b, by simpa using hb, rfl⟩ end namespace EquivalenceJ /-- Given `w : TwoSquare T L R B` and a morphism `g : R.obj X₂ ⟶ B.obj X₃`, this is the obvious functor `w.StructuredArrowRightwards g ⥤ w.CostructuredArrowDownwards g`. -/ @[simps] def functor : w.StructuredArrowRightwards g ⥤ w.CostructuredArrowDownwards g where obj f := CostructuredArrow.mk (Y := StructuredArrow.mk f.hom.left) (StructuredArrow.homMk f.right.hom (by simpa using CostructuredArrow.w f.hom)) map {f₁ f₂} φ := CostructuredArrow.homMk (StructuredArrow.homMk φ.right.left (by dsimp; rw [← StructuredArrow.w φ]; rfl)) (by ext; exact CostructuredArrow.w φ.right) map_id _ := rfl map_comp _ _ := rfl /-- Given `w : TwoSquare T L R B` and a morphism `g : R.obj X₂ ⟶ B.obj X₃`, this is the obvious functor `w.CostructuredArrowDownwards g ⥤ w.StructuredArrowRightwards g`. -/ @[simps] def inverse : w.CostructuredArrowDownwards g ⥤ w.StructuredArrowRightwards g where obj f := StructuredArrow.mk (Y := CostructuredArrow.mk f.hom.right) (CostructuredArrow.homMk f.left.hom (by simpa using StructuredArrow.w f.hom)) map {f₁ f₂} φ := StructuredArrow.homMk (CostructuredArrow.homMk φ.left.right (by dsimp; rw [← CostructuredArrow.w φ]; rfl)) (by ext; exact StructuredArrow.w φ.left) map_id _ := rfl map_comp _ _ := rfl end EquivalenceJ /-- Given `w : TwoSquare T L R B` and a morphism `g : R.obj X₂ ⟶ B.obj X₃`, this is the obvious equivalence of categories `w.StructuredArrowRightwards g ≌ w.CostructuredArrowDownwards g`. -/ @[simps functor inverse unitIso counitIso] def equivalenceJ : w.StructuredArrowRightwards g ≌ w.CostructuredArrowDownwards g where functor := EquivalenceJ.functor w g inverse := EquivalenceJ.inverse w g unitIso := Iso.refl _ counitIso := Iso.refl _ lemma isConnected_rightwards_iff_downwards : IsConnected (w.StructuredArrowRightwards g) ↔ IsConnected (w.CostructuredArrowDownwards g) := isConnected_iff_of_equivalence (w.equivalenceJ g) end section /-- The functor `w.CostructuredArrowDownwards g ⥤ w.CostructuredArrowDownwards g'` induced by a morphism `γ` such that `R.map γ ≫ g = g'`. -/ @[simps] def costructuredArrowDownwardsPrecomp {X₂ X₂' : C₂} {X₃ : C₃} (g : R.obj X₂ ⟶ B.obj X₃) (g' : R.obj X₂' ⟶ B.obj X₃) (γ : X₂' ⟶ X₂) (hγ : R.map γ ≫ g = g') : w.CostructuredArrowDownwards g ⥤ w.CostructuredArrowDownwards g' where obj A := CostructuredArrowDownwards.mk _ _ A.left.right (γ ≫ A.left.hom) A.hom.right (by simpa [← hγ] using R.map γ ≫= StructuredArrow.w A.hom) map {A A'} φ := CostructuredArrow.homMk (StructuredArrow.homMk φ.left.right (by dsimp rw [assoc, StructuredArrow.w])) (by ext dsimp rw [← CostructuredArrow.w φ, structuredArrowDownwards_map] rfl) map_id _ := rfl map_comp _ _ := rfl end /-- Condition on `w : TwoSquare T L R B` expressing that it is a Guitart exact square. It is equivalent to saying that for any `X₃ : C₃`, the induced functor `CostructuredArrow L X₃ ⥤ CostructuredArrow R (B.obj X₃)` is final (see `guitartExact_iff_final`) or equivalently that for any `X₂ : C₂`, the induced functor `StructuredArrow X₂ T ⥤ StructuredArrow (R.obj X₂) B` is initial (see `guitartExact_iff_initial`). See also `guitartExact_iff_isConnected_rightwards`, `guitartExact_iff_isConnected_downwards` for characterizations in terms of the connectedness of auxiliary categories. -/ class GuitartExact : Prop where isConnected_rightwards {X₂ : C₂} {X₃ : C₃} (g : R.obj X₂ ⟶ B.obj X₃) : IsConnected (w.StructuredArrowRightwards g) lemma guitartExact_iff_isConnected_rightwards : w.GuitartExact ↔ ∀ {X₂ : C₂} {X₃ : C₃} (g : R.obj X₂ ⟶ B.obj X₃), IsConnected (w.StructuredArrowRightwards g) := ⟨fun h => h.isConnected_rightwards, fun h => ⟨h⟩⟩ lemma guitartExact_iff_isConnected_downwards : w.GuitartExact ↔ ∀ {X₂ : C₂} {X₃ : C₃} (g : R.obj X₂ ⟶ B.obj X₃), IsConnected (w.CostructuredArrowDownwards g) := by simp only [guitartExact_iff_isConnected_rightwards, isConnected_rightwards_iff_downwards] instance [hw : w.GuitartExact] {X₃ : C₃} (g : CostructuredArrow R (B.obj X₃)) : IsConnected (StructuredArrow g (w.costructuredArrowRightwards X₃)) := by rw [guitartExact_iff_isConnected_rightwards] at hw apply hw instance [hw : w.GuitartExact] {X₂ : C₂} (g : StructuredArrow (R.obj X₂) B) : IsConnected (CostructuredArrow (w.structuredArrowDownwards X₂) g) := by rw [guitartExact_iff_isConnected_downwards] at hw apply hw lemma guitartExact_iff_final : w.GuitartExact ↔ ∀ (X₃ : C₃), (w.costructuredArrowRightwards X₃).Final := ⟨fun _ _ => ⟨fun _ => inferInstance⟩, fun _ => ⟨fun _ => inferInstance⟩⟩ instance [hw : w.GuitartExact] (X₃ : C₃) : (w.costructuredArrowRightwards X₃).Final := by rw [guitartExact_iff_final] at hw apply hw lemma guitartExact_iff_initial : w.GuitartExact ↔ ∀ (X₂ : C₂), (w.structuredArrowDownwards X₂).Initial := ⟨fun _ _ => ⟨fun _ => inferInstance⟩, by rw [guitartExact_iff_isConnected_downwards] intros infer_instance⟩ instance [hw : w.GuitartExact] (X₂ : C₂) : (w.structuredArrowDownwards X₂).Initial := by rw [guitartExact_iff_initial] at hw apply hw /-- When the left and right functors of a 2-square are equivalences, and the natural
transformation of the 2-square is an isomorphism, then the 2-square is Guitart exact. -/ instance (priority := 100) guitartExact_of_isEquivalence_of_isIso [L.IsEquivalence] [R.IsEquivalence] [IsIso w] : GuitartExact w := by rw [guitartExact_iff_initial] intro X₂ have := StructuredArrow.isEquivalence_post X₂ T R
Mathlib/CategoryTheory/GuitartExact/Basic.lean
262
267
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov, Patrick Massot, Sébastien Gouëzel -/ import Mathlib.MeasureTheory.Integral.IntervalIntegral.Basic import Mathlib.MeasureTheory.Integral.IntervalIntegral.FundThmCalculus import Mathlib.MeasureTheory.Integral.IntervalIntegral.IntegrationByParts deprecated_module (since := "2025-04-13")
Mathlib/MeasureTheory/Integral/IntervalIntegral.lean
618
620
/- Copyright (c) 2022 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Oliver Nash -/ import Mathlib.Algebra.Lie.Abelian import Mathlib.Algebra.Lie.IdealOperations import Mathlib.Algebra.Lie.Quotient /-! # The normalizer of Lie submodules and subalgebras. Given a Lie module `M` over a Lie subalgebra `L`, the normalizer of a Lie submodule `N ⊆ M` is the Lie submodule with underlying set `{ m | ∀ (x : L), ⁅x, m⁆ ∈ N }`. The lattice of Lie submodules thus has two natural operations, the normalizer: `N ↦ N.normalizer` and the ideal operation: `N ↦ ⁅⊤, N⁆`; these are adjoint, i.e., they form a Galois connection. This adjointness is the reason that we may define nilpotency in terms of either the upper or lower central series. Given a Lie subalgebra `H ⊆ L`, we may regard `H` as a Lie submodule of `L` over `H`, and thus consider the normalizer. This turns out to be a Lie subalgebra. ## Main definitions * `LieSubmodule.normalizer` * `LieSubalgebra.normalizer` * `LieSubmodule.gc_top_lie_normalizer` ## Tags lie algebra, normalizer -/ variable {R L M M' : Type*} variable [CommRing R] [LieRing L] [LieAlgebra R L] variable [AddCommGroup M] [Module R M] [LieRingModule L M] [LieModule R L M] variable [AddCommGroup M'] [Module R M'] [LieRingModule L M'] [LieModule R L M'] namespace LieSubmodule variable (N : LieSubmodule R L M) {N₁ N₂ : LieSubmodule R L M} /-- The normalizer of a Lie submodule. See also `LieSubmodule.idealizer`. -/ def normalizer : LieSubmodule R L M where carrier := {m | ∀ x : L, ⁅x, m⁆ ∈ N} add_mem' hm₁ hm₂ x := by rw [lie_add]; exact N.add_mem' (hm₁ x) (hm₂ x) zero_mem' x := by simp smul_mem' t m hm x := by rw [lie_smul]; exact N.smul_mem' t (hm x) lie_mem {x m} hm y := by rw [leibniz_lie]; exact N.add_mem' (hm ⁅y, x⁆) (N.lie_mem (hm y)) @[simp] theorem mem_normalizer (m : M) : m ∈ N.normalizer ↔ ∀ x : L, ⁅x, m⁆ ∈ N := Iff.rfl @[simp] theorem le_normalizer : N ≤ N.normalizer := by intro m hm rw [mem_normalizer] exact fun x => N.lie_mem hm theorem normalizer_inf : (N₁ ⊓ N₂).normalizer = N₁.normalizer ⊓ N₂.normalizer := by ext; simp [← forall_and] @[gcongr, mono] theorem normalizer_mono (h : N₁ ≤ N₂) : normalizer N₁ ≤ normalizer N₂ := by intro m hm rw [mem_normalizer] at hm ⊢ exact fun x ↦ h (hm x) theorem monotone_normalizer : Monotone (normalizer : LieSubmodule R L M → LieSubmodule R L M) :=
fun _ _ ↦ normalizer_mono @[simp] theorem comap_normalizer (f : M' →ₗ⁅R,L⁆ M) : N.normalizer.comap f = (N.comap f).normalizer := by
Mathlib/Algebra/Lie/Normalizer.lean
75
78
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Johannes Hölzl, Sander Dahmen, Kim Morrison -/ import Mathlib.Algebra.Algebra.Tower import Mathlib.LinearAlgebra.LinearIndependent.Basic import Mathlib.Data.Set.Card /-! # Dimension of modules and vector spaces ## Main definitions * The rank of a module is defined as `Module.rank : Cardinal`. This is defined as the supremum of the cardinalities of linearly independent subsets. ## Main statements * `LinearMap.rank_le_of_injective`: the source of an injective linear map has dimension at most that of the target. * `LinearMap.rank_le_of_surjective`: the target of a surjective linear map has dimension at most that of that source. ## Implementation notes Many theorems in this file are not universe-generic when they relate dimensions in different universes. They should be as general as they can be without inserting `lift`s. The types `M`, `M'`, ... all live in different universes, and `M₁`, `M₂`, ... all live in the same universe. -/ noncomputable section universe w w' u u' v v' variable {R : Type u} {R' : Type u'} {M M₁ : Type v} {M' : Type v'} open Cardinal Submodule Function Set section Module section variable [Semiring R] [AddCommMonoid M] [Module R M] variable (R M) /-- The rank of a module, defined as a term of type `Cardinal`. We define this as the supremum of the cardinalities of linearly independent subsets. The supremum may not be attained, see https://mathoverflow.net/a/263053. For a free module over any ring satisfying the strong rank condition (e.g. left-noetherian rings, commutative rings, and in particular division rings and fields), this is the same as the dimension of the space (i.e. the cardinality of any basis). In particular this agrees with the usual notion of the dimension of a vector space. See also `Module.finrank` for a `ℕ`-valued function which returns the correct value for a finite-dimensional vector space (but 0 for an infinite-dimensional vector space). -/ @[stacks 09G3 "first part"] protected irreducible_def Module.rank : Cardinal := ⨆ ι : { s : Set M // LinearIndepOn R id s }, (#ι.1) theorem rank_le_card : Module.rank R M ≤ #M := (Module.rank_def _ _).trans_le (ciSup_le' fun _ ↦ mk_set_le _) lemma nonempty_linearIndependent_set : Nonempty {s : Set M // LinearIndepOn R id s } := ⟨⟨∅, linearIndepOn_empty _ _⟩⟩ end namespace LinearIndependent variable [Semiring R] [AddCommMonoid M] [Module R M] variable [Nontrivial R] theorem cardinal_lift_le_rank {ι : Type w} {v : ι → M} (hv : LinearIndependent R v) : Cardinal.lift.{v} #ι ≤ Cardinal.lift.{w} (Module.rank R M) := by rw [Module.rank] refine le_trans ?_ (lift_le.mpr <| le_ciSup (bddAbove_range _) ⟨_, hv.linearIndepOn_id⟩) exact lift_mk_le'.mpr ⟨(Equiv.ofInjective _ hv.injective).toEmbedding⟩ lemma aleph0_le_rank {ι : Type w} [Infinite ι] {v : ι → M} (hv : LinearIndependent R v) : ℵ₀ ≤ Module.rank R M := aleph0_le_lift.mp <| (aleph0_le_lift.mpr <| aleph0_le_mk ι).trans hv.cardinal_lift_le_rank theorem cardinal_le_rank {ι : Type v} {v : ι → M} (hv : LinearIndependent R v) : #ι ≤ Module.rank R M := by simpa using hv.cardinal_lift_le_rank theorem cardinal_le_rank' {s : Set M} (hs : LinearIndependent R (fun x => x : s → M)) : #s ≤ Module.rank R M := hs.cardinal_le_rank theorem _root_.LinearIndepOn.encard_le_toENat_rank {ι : Type*} {v : ι → M} {s : Set ι} (hs : LinearIndepOn R v s) : s.encard ≤ (Module.rank R M).toENat := by simpa using OrderHom.mono (β := ℕ∞) Cardinal.toENat hs.linearIndependent.cardinal_lift_le_rank end LinearIndependent section SurjectiveInjective section Semiring variable [Semiring R] [AddCommMonoid M] [Module R M] [Semiring R'] section variable [AddCommMonoid M'] [Module R' M'] /-- If `M / R` and `M' / R'` are modules, `i : R' → R` is an injective map non-zero elements, `j : M →+ M'` is an injective monoid homomorphism, such that the scalar multiplications on `M` and `M'` are compatible, then the rank of `M / R` is smaller than or equal to the rank of `M' / R'`. As a special case, taking `R = R'` it is `LinearMap.lift_rank_le_of_injective`. -/ theorem lift_rank_le_of_injective_injectiveₛ (i : R' → R) (j : M →+ M') (hi : Injective i) (hj : Injective j) (hc : ∀ (r : R') (m : M), j (i r • m) = r • j m) : lift.{v'} (Module.rank R M) ≤ lift.{v} (Module.rank R' M') := by simp_rw [Module.rank, lift_iSup (bddAbove_range _)] exact ciSup_mono' (bddAbove_range _) fun ⟨s, h⟩ ↦ ⟨⟨j '' s, LinearIndepOn.id_image (h.linearIndependent.map_of_injective_injectiveₛ i j hi hj hc)⟩, lift_mk_le'.mpr ⟨(Equiv.Set.image j s hj).toEmbedding⟩⟩ /-- If `M / R` and `M' / R'` are modules, `i : R → R'` is a surjective map, and `j : M →+ M'` is an injective monoid homomorphism, such that the scalar multiplications on `M` and `M'` are compatible, then the rank of `M / R` is smaller than or equal to the rank of `M' / R'`. As a special case, taking `R = R'` it is `LinearMap.lift_rank_le_of_injective`. -/ theorem lift_rank_le_of_surjective_injective (i : R → R') (j : M →+ M') (hi : Surjective i) (hj : Injective j) (hc : ∀ (r : R) (m : M), j (r • m) = i r • j m) : lift.{v'} (Module.rank R M) ≤ lift.{v} (Module.rank R' M') := by obtain ⟨i', hi'⟩ := hi.hasRightInverse
refine lift_rank_le_of_injective_injectiveₛ i' j (fun _ _ h ↦ ?_) hj fun r m ↦ ?_ · apply_fun i at h rwa [hi', hi'] at h rw [hc (i' r) m, hi'] /-- If `M / R` and `M' / R'` are modules, `i : R → R'` is a bijective map which maps zero to zero, `j : M ≃+ M'` is a group isomorphism, such that the scalar multiplications on `M` and `M'` are compatible, then the rank of `M / R` is equal to the rank of `M' / R'`.
Mathlib/LinearAlgebra/Dimension/Basic.lean
135
142
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Johan Commelin, Mario Carneiro -/ import Mathlib.Algebra.MonoidAlgebra.Degree import Mathlib.Algebra.MvPolynomial.Rename /-! # Degrees of polynomials This file establishes many results about the degree of a multivariate polynomial. The *degree set* of a polynomial $P \in R[X]$ is a `Multiset` containing, for each $x$ in the variable set, $n$ copies of $x$, where $n$ is the maximum number of copies of $x$ appearing in a monomial of $P$. ## Main declarations * `MvPolynomial.degrees p` : the multiset of variables representing the union of the multisets corresponding to each non-zero monomial in `p`. For example if `7 ≠ 0` in `R` and `p = x²y+7y³` then `degrees p = {x, x, y, y, y}` * `MvPolynomial.degreeOf n p : ℕ` : the total degree of `p` with respect to the variable `n`. For example if `p = x⁴y+yz` then `degreeOf y p = 1`. * `MvPolynomial.totalDegree p : ℕ` : the max of the sizes of the multisets `s` whose monomials `X^s` occur in `p`. For example if `p = x⁴y+yz` then `totalDegree p = 5`. ## Notation As in other polynomial files, we typically use the notation: + `σ τ : Type*` (indexing the variables) + `R : Type*` `[CommSemiring R]` (the coefficients) + `s : σ →₀ ℕ`, a function from `σ` to `ℕ` which is zero away from a finite set. This will give rise to a monomial in `MvPolynomial σ R` which mathematicians might call `X^s` + `r : R` + `i : σ`, with corresponding monomial `X i`, often denoted `X_i` by mathematicians + `p : MvPolynomial σ R` -/ noncomputable section open Set Function Finsupp AddMonoidAlgebra universe u v w variable {R : Type u} {S : Type v} namespace MvPolynomial variable {σ τ : Type*} {r : R} {e : ℕ} {n m : σ} {s : σ →₀ ℕ} section CommSemiring variable [CommSemiring R] {p q : MvPolynomial σ R} section Degrees /-! ### `degrees` -/ /-- The maximal degrees of each variable in a multi-variable polynomial, expressed as a multiset. (For example, `degrees (x^2 * y + y^3)` would be `{x, x, y, y, y}`.) -/ def degrees (p : MvPolynomial σ R) : Multiset σ := letI := Classical.decEq σ p.support.sup fun s : σ →₀ ℕ => toMultiset s theorem degrees_def [DecidableEq σ] (p : MvPolynomial σ R) : p.degrees = p.support.sup fun s : σ →₀ ℕ => Finsupp.toMultiset s := by rw [degrees]; convert rfl theorem degrees_monomial (s : σ →₀ ℕ) (a : R) : degrees (monomial s a) ≤ toMultiset s := by classical refine (supDegree_single s a).trans_le ?_ split_ifs exacts [bot_le, le_rfl] theorem degrees_monomial_eq (s : σ →₀ ℕ) (a : R) (ha : a ≠ 0) : degrees (monomial s a) = toMultiset s := by classical exact (supDegree_single s a).trans (if_neg ha) theorem degrees_C (a : R) : degrees (C a : MvPolynomial σ R) = 0 := Multiset.le_zero.1 <| degrees_monomial _ _ theorem degrees_X' (n : σ) : degrees (X n : MvPolynomial σ R) ≤ {n} := le_trans (degrees_monomial _ _) <| le_of_eq <| toMultiset_single _ _ @[simp] theorem degrees_X [Nontrivial R] (n : σ) : degrees (X n : MvPolynomial σ R) = {n} := (degrees_monomial_eq _ (1 : R) one_ne_zero).trans (toMultiset_single _ _) @[simp] theorem degrees_zero : degrees (0 : MvPolynomial σ R) = 0 := by rw [← C_0] exact degrees_C 0 @[simp] theorem degrees_one : degrees (1 : MvPolynomial σ R) = 0 := degrees_C 1 theorem degrees_add_le [DecidableEq σ] {p q : MvPolynomial σ R} : (p + q).degrees ≤ p.degrees ⊔ q.degrees := by simp_rw [degrees_def]; exact supDegree_add_le theorem degrees_sum_le {ι : Type*} [DecidableEq σ] (s : Finset ι) (f : ι → MvPolynomial σ R) : (∑ i ∈ s, f i).degrees ≤ s.sup fun i => (f i).degrees := by simp_rw [degrees_def]; exact supDegree_sum_le theorem degrees_mul_le {p q : MvPolynomial σ R} : (p * q).degrees ≤ p.degrees + q.degrees := by classical simp_rw [degrees_def] exact supDegree_mul_le (map_add _) theorem degrees_prod_le {ι : Type*} {s : Finset ι} {f : ι → MvPolynomial σ R} : (∏ i ∈ s, f i).degrees ≤ ∑ i ∈ s, (f i).degrees := by classical exact supDegree_prod_le (map_zero _) (map_add _) theorem degrees_pow_le {p : MvPolynomial σ R} {n : ℕ} : (p ^ n).degrees ≤ n • p.degrees := by simpa using degrees_prod_le (s := .range n) (f := fun _ ↦ p) @[deprecated (since := "2024-12-28")] alias degrees_add := degrees_add_le @[deprecated (since := "2024-12-28")] alias degrees_sum := degrees_sum_le @[deprecated (since := "2024-12-28")] alias degrees_mul := degrees_mul_le @[deprecated (since := "2024-12-28")] alias degrees_prod := degrees_prod_le @[deprecated (since := "2024-12-28")] alias degrees_pow := degrees_pow_le theorem mem_degrees {p : MvPolynomial σ R} {i : σ} : i ∈ p.degrees ↔ ∃ d, p.coeff d ≠ 0 ∧ i ∈ d.support := by classical simp only [degrees_def, Multiset.mem_sup, ← mem_support_iff, Finsupp.mem_toMultiset, exists_prop] theorem le_degrees_add_left (h : Disjoint p.degrees q.degrees) : p.degrees ≤ (p + q).degrees := by classical apply Finset.sup_le intro d hd rw [Multiset.disjoint_iff_ne] at h obtain rfl | h0 := eq_or_ne d 0 · rw [toMultiset_zero]; apply Multiset.zero_le · refine Finset.le_sup_of_le (b := d) ?_ le_rfl rw [mem_support_iff, coeff_add] suffices q.coeff d = 0 by rwa [this, add_zero, coeff, ← Finsupp.mem_support_iff] rw [Ne, ← Finsupp.support_eq_empty, ← Ne, ← Finset.nonempty_iff_ne_empty] at h0 obtain ⟨j, hj⟩ := h0 contrapose! h rw [mem_support_iff] at hd refine ⟨j, ?_, j, ?_, rfl⟩ all_goals rw [mem_degrees]; refine ⟨d, ?_, hj⟩; assumption @[deprecated (since := "2024-12-28")] alias le_degrees_add := le_degrees_add_left lemma le_degrees_add_right (h : Disjoint p.degrees q.degrees) : q.degrees ≤ (p + q).degrees := by simpa [add_comm] using le_degrees_add_left h.symm theorem degrees_add_of_disjoint [DecidableEq σ] (h : Disjoint p.degrees q.degrees) : (p + q).degrees = p.degrees ∪ q.degrees := degrees_add_le.antisymm <| Multiset.union_le (le_degrees_add_left h) (le_degrees_add_right h) lemma degrees_map_le [CommSemiring S] {f : R →+* S} : (map f p).degrees ≤ p.degrees := by classical exact Finset.sup_mono <| support_map_subset .. @[deprecated (since := "2024-12-28")] alias degrees_map := degrees_map_le theorem degrees_rename (f : σ → τ) (φ : MvPolynomial σ R) : (rename f φ).degrees ⊆ φ.degrees.map f := by classical intro i rw [mem_degrees, Multiset.mem_map] rintro ⟨d, hd, hi⟩ obtain ⟨x, rfl, hx⟩ := coeff_rename_ne_zero _ _ _ hd simp only [Finsupp.mapDomain, Finsupp.mem_support_iff] at hi rw [sum_apply, Finsupp.sum] at hi contrapose! hi rw [Finset.sum_eq_zero] intro j hj simp only [exists_prop, mem_degrees] at hi specialize hi j ⟨x, hx, hj⟩ rw [Finsupp.single_apply, if_neg hi] theorem degrees_map_of_injective [CommSemiring S] (p : MvPolynomial σ R) {f : R →+* S} (hf : Injective f) : (map f p).degrees = p.degrees := by simp only [degrees, MvPolynomial.support_map_of_injective _ hf] theorem degrees_rename_of_injective {p : MvPolynomial σ R} {f : σ → τ} (h : Function.Injective f) : degrees (rename f p) = (degrees p).map f := by classical simp only [degrees, Multiset.map_finset_sup p.support Finsupp.toMultiset f h, support_rename_of_injective h, Finset.sup_image] refine Finset.sup_congr rfl fun x _ => ?_ exact (Finsupp.toMultiset_map _ _).symm end Degrees section DegreeOf /-! ### `degreeOf` -/ /-- `degreeOf n p` gives the highest power of X_n that appears in `p` -/ def degreeOf (n : σ) (p : MvPolynomial σ R) : ℕ := letI := Classical.decEq σ p.degrees.count n theorem degreeOf_def [DecidableEq σ] (n : σ) (p : MvPolynomial σ R) : p.degreeOf n = p.degrees.count n := by rw [degreeOf]; convert rfl theorem degreeOf_eq_sup (n : σ) (f : MvPolynomial σ R) : degreeOf n f = f.support.sup fun m => m n := by classical rw [degreeOf_def, degrees, Multiset.count_finset_sup] congr ext simp only [count_toMultiset] theorem degreeOf_lt_iff {n : σ} {f : MvPolynomial σ R} {d : ℕ} (h : 0 < d) : degreeOf n f < d ↔ ∀ m : σ →₀ ℕ, m ∈ f.support → m n < d := by rwa [degreeOf_eq_sup, Finset.sup_lt_iff] lemma degreeOf_le_iff {n : σ} {f : MvPolynomial σ R} {d : ℕ} : degreeOf n f ≤ d ↔ ∀ m ∈ support f, m n ≤ d := by rw [degreeOf_eq_sup, Finset.sup_le_iff] @[simp] theorem degreeOf_zero (n : σ) : degreeOf n (0 : MvPolynomial σ R) = 0 := by classical simp only [degreeOf_def, degrees_zero, Multiset.count_zero] @[simp] theorem degreeOf_C (a : R) (x : σ) : degreeOf x (C a : MvPolynomial σ R) = 0 := by classical simp [degreeOf_def, degrees_C] theorem degreeOf_X [DecidableEq σ] (i j : σ) [Nontrivial R] : degreeOf i (X j : MvPolynomial σ R) = if i = j then 1 else 0 := by classical by_cases c : i = j · simp only [c, if_true, eq_self_iff_true, degreeOf_def, degrees_X, Multiset.count_singleton] simp [c, if_false, degreeOf_def, degrees_X] theorem degreeOf_add_le (n : σ) (f g : MvPolynomial σ R) : degreeOf n (f + g) ≤ max (degreeOf n f) (degreeOf n g) := by simp_rw [degreeOf_eq_sup]; exact supDegree_add_le theorem monomial_le_degreeOf (i : σ) {f : MvPolynomial σ R} {m : σ →₀ ℕ} (h_m : m ∈ f.support) : m i ≤ degreeOf i f := by rw [degreeOf_eq_sup i] apply Finset.le_sup h_m lemma degreeOf_monomial_eq (s : σ →₀ ℕ) (i : σ) {a : R} (ha : a ≠ 0) : (monomial s a).degreeOf i = s i := by classical rw [degreeOf_def, degrees_monomial_eq _ _ ha, Finsupp.count_toMultiset] -- TODO we can prove equality with `NoZeroDivisors R` theorem degreeOf_mul_le (i : σ) (f g : MvPolynomial σ R) : degreeOf i (f * g) ≤ degreeOf i f + degreeOf i g := by classical simp only [degreeOf] convert Multiset.count_le_of_le i degrees_mul_le rw [Multiset.count_add] theorem degreeOf_sum_le {ι : Type*} (i : σ) (s : Finset ι) (f : ι → MvPolynomial σ R) : degreeOf i (∑ j ∈ s, f j) ≤ s.sup fun j => degreeOf i (f j) := by simp_rw [degreeOf_eq_sup] exact supDegree_sum_le -- TODO we can prove equality with `NoZeroDivisors R` theorem degreeOf_prod_le {ι : Type*} (i : σ) (s : Finset ι) (f : ι → MvPolynomial σ R) : degreeOf i (∏ j ∈ s, f j) ≤ ∑ j ∈ s, (f j).degreeOf i := by simp_rw [degreeOf_eq_sup] exact supDegree_prod_le (by simp only [coe_zero, Pi.zero_apply]) (fun _ _ => by simp only [coe_add, Pi.add_apply]) -- TODO we can prove equality with `NoZeroDivisors R` theorem degreeOf_pow_le (i : σ) (p : MvPolynomial σ R) (n : ℕ) : degreeOf i (p ^ n) ≤ n * degreeOf i p := by simpa using degreeOf_prod_le i (Finset.range n) (fun _ => p) theorem degreeOf_mul_X_of_ne {i j : σ} (f : MvPolynomial σ R) (h : i ≠ j) : degreeOf i (f * X j) = degreeOf i f := by classical simp only [degreeOf_eq_sup i, support_mul_X, Finset.sup_map] congr ext simp only [Finsupp.single, add_eq_left, addRightEmbedding_apply, coe_mk, Pi.add_apply, comp_apply, ite_eq_right_iff, Finsupp.coe_add, Pi.single_eq_of_ne h] @[deprecated (since := "2024-12-01")] alias degreeOf_mul_X_ne := degreeOf_mul_X_of_ne theorem degreeOf_mul_X_self (j : σ) (f : MvPolynomial σ R) : degreeOf j (f * X j) ≤ degreeOf j f + 1 := by classical simp only [degreeOf] apply (Multiset.count_le_of_le j degrees_mul_le).trans simp only [Multiset.count_add, add_le_add_iff_left] convert Multiset.count_le_of_le j <| degrees_X' j rw [Multiset.count_singleton_self] @[deprecated (since := "2024-12-01")] alias degreeOf_mul_X_eq := degreeOf_mul_X_self theorem degreeOf_mul_X_eq_degreeOf_add_one_iff (j : σ) (f : MvPolynomial σ R) : degreeOf j (f * X j) = degreeOf j f + 1 ↔ f ≠ 0 := by refine ⟨fun h => by by_contra ha; simp [ha] at h, fun h => ?_⟩ apply Nat.le_antisymm (degreeOf_mul_X_self j f) have : (f.support.sup fun m ↦ m j) + 1 = (f.support.sup fun m ↦ (m j + 1)) := Finset.comp_sup_eq_sup_comp_of_nonempty @Nat.succ_le_succ (support_nonempty.mpr h) simp only [degreeOf_eq_sup, support_mul_X, this] apply Finset.sup_le intro x hx simp only [Finset.sup_map, bot_eq_zero', add_pos_iff, zero_lt_one, or_true, Finset.le_sup_iff] use x simpa using mem_support_iff.mp hx theorem degreeOf_C_mul_le (p : MvPolynomial σ R) (i : σ) (c : R) : (C c * p).degreeOf i ≤ p.degreeOf i := by unfold degreeOf convert Multiset.count_le_of_le i degrees_mul_le simp only [degrees_C, zero_add] theorem degreeOf_mul_C_le (p : MvPolynomial σ R) (i : σ) (c : R) : (p * C c).degreeOf i ≤ p.degreeOf i := by unfold degreeOf convert Multiset.count_le_of_le i degrees_mul_le simp only [degrees_C, add_zero] theorem degreeOf_rename_of_injective {p : MvPolynomial σ R} {f : σ → τ} (h : Function.Injective f) (i : σ) : degreeOf (f i) (rename f p) = degreeOf i p := by classical simp only [degreeOf, degrees_rename_of_injective h, Multiset.count_map_eq_count' f p.degrees h] end DegreeOf section TotalDegree /-! ### `totalDegree` -/ /-- `totalDegree p` gives the maximum |s| over the monomials X^s in `p` -/ def totalDegree (p : MvPolynomial σ R) : ℕ := p.support.sup fun s => s.sum fun _ e => e theorem totalDegree_eq (p : MvPolynomial σ R) : p.totalDegree = p.support.sup fun m => Multiset.card (toMultiset m) := by rw [totalDegree] congr; funext m exact (Finsupp.card_toMultiset _).symm theorem le_totalDegree {p : MvPolynomial σ R} {s : σ →₀ ℕ} (h : s ∈ p.support) : (s.sum fun _ e => e) ≤ totalDegree p := Finset.le_sup (α := ℕ) (f := fun s => sum s fun _ e => e) h theorem totalDegree_le_degrees_card (p : MvPolynomial σ R) : p.totalDegree ≤ Multiset.card p.degrees := by classical rw [totalDegree_eq] exact Finset.sup_le fun s hs => Multiset.card_le_card <| Finset.le_sup hs theorem totalDegree_le_of_support_subset (h : p.support ⊆ q.support) : totalDegree p ≤ totalDegree q := Finset.sup_mono h
@[simp] theorem totalDegree_C (a : R) : (C a : MvPolynomial σ R).totalDegree = 0 := (supDegree_single 0 a).trans <| by rw [sum_zero_index, bot_eq_zero', ite_self]
Mathlib/Algebra/MvPolynomial/Degrees.lean
369
373
/- Copyright (c) 2021 Roberto Alvarez. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Roberto Alvarez -/ import Mathlib.AlgebraicTopology.FundamentalGroupoid.FundamentalGroup import Mathlib.GroupTheory.EckmannHilton import Mathlib.Algebra.Equiv.TransferInstance import Mathlib.Algebra.Group.Ext /-! # `n`th homotopy group We define the `n`th homotopy group at `x : X`, `π_n X x`, as the equivalence classes of functions from the `n`-dimensional cube to the topological space `X` that send the boundary to the base point `x`, up to homotopic equivalence. Note that such functions are generalized loops `GenLoop (Fin n) x`; in particular `GenLoop (Fin 1) x ≃ Path x x`. We show that `π_0 X x` is equivalent to the path-connected components, and that `π_1 X x` is equivalent to the fundamental group at `x`. We provide a group instance using path composition and show commutativity when `n > 1`. ## definitions * `GenLoop N x` is the type of continuous functions `I^N → X` that send the boundary to `x`, * `HomotopyGroup.Pi n X x` denoted `π_ n X x` is the quotient of `GenLoop (Fin n) x` by homotopy relative to the boundary, * group instance `Group (π_(n+1) X x)`, * commutative group instance `CommGroup (π_(n+2) X x)`. TODO: * `Ω^M (Ω^N X) ≃ₜ Ω^(M⊕N) X`, and `Ω^M X ≃ₜ Ω^N X` when `M ≃ N`. Similarly for `π_`. * Path-induced homomorphisms. Show that `HomotopyGroup.pi1EquivFundamentalGroup` is a group isomorphism. * Examples with `𝕊^n`: `π_n (𝕊^n) = ℤ`, `π_m (𝕊^n)` trivial for `m < n`. * Actions of π_1 on π_n. * Lie algebra: `⁅π_(n+1), π_(m+1)⁆` contained in `π_(n+m+1)`. -/ open scoped unitInterval Topology open Homeomorph noncomputable section /-- `I^N` is notation (in the Topology namespace) for `N → I`, i.e. the unit cube indexed by a type `N`. -/ scoped[Topology] notation "I^" N => N → I namespace Cube /-- The points in a cube with at least one projection equal to 0 or 1. -/ def boundary (N : Type*) : Set (I^N) := {y | ∃ i, y i = 0 ∨ y i = 1} variable {N : Type*} [DecidableEq N] /-- The forward direction of the homeomorphism between the cube $I^N$ and $I × I^{N\setminus\{j\}}$. -/ abbrev splitAt (i : N) : (I^N) ≃ₜ I × I^{ j // j ≠ i } := funSplitAt I i /-- The backward direction of the homeomorphism between the cube $I^N$ and $I × I^{N\setminus\{j\}}$. -/ abbrev insertAt (i : N) : (I × I^{ j // j ≠ i }) ≃ₜ I^N := (funSplitAt I i).symm theorem insertAt_boundary (i : N) {t₀ : I} {t} (H : (t₀ = 0 ∨ t₀ = 1) ∨ t ∈ boundary { j // j ≠ i }) : insertAt i ⟨t₀, t⟩ ∈ boundary N := by obtain H | ⟨j, H⟩ := H · use i; rwa [funSplitAt_symm_apply, dif_pos rfl] · use j; rwa [funSplitAt_symm_apply, dif_neg j.prop, Subtype.coe_eta] end Cube variable (N X : Type*) [TopologicalSpace X] (x : X) /-- The space of paths with both endpoints equal to a specified point `x : X`. Denoted as `Ω`, within the `Topology.Homotopy` namespace. -/ abbrev LoopSpace := Path x x @[inherit_doc] scoped[Topology.Homotopy] notation "Ω" => LoopSpace instance LoopSpace.inhabited : Inhabited (Path x x) := ⟨Path.refl x⟩ /-- The `n`-dimensional generalized loops based at `x` in a space `X` are continuous functions `I^n → X` that sends the boundary to `x`. We allow an arbitrary indexing type `N` in place of `Fin n` here. -/ def GenLoop : Set C(I^N, X) := {p | ∀ y ∈ Cube.boundary N, p y = x} @[inherit_doc] scoped[Topology.Homotopy] notation "Ω^" => GenLoop open Topology.Homotopy variable {N X x} namespace GenLoop instance instFunLike : FunLike (Ω^ N X x) (I^N) X where coe f := f.1 coe_injective' := fun ⟨⟨f, _⟩, _⟩ ⟨⟨g, _⟩, _⟩ _ => by congr @[ext] theorem ext (f g : Ω^ N X x) (H : ∀ y, f y = g y) : f = g := DFunLike.coe_injective' (funext H) @[simp] theorem mk_apply (f : C(I^N, X)) (H y) : (⟨f, H⟩ : Ω^ N X x) y = f y := rfl instance instContinuousEval : ContinuousEval (Ω^ N X x) (I^N) X := .of_continuous_forget continuous_subtype_val instance instContinuousEvalConst : ContinuousEvalConst (Ω^ N X x) (I^N) X := inferInstance /-- Copy of a `GenLoop` with a new map from the unit cube equal to the old one. Useful to fix definitional equalities. -/ def copy (f : Ω^ N X x) (g : (I^N) → X) (h : g = f) : Ω^ N X x := ⟨⟨g, h.symm ▸ f.1.2⟩, by convert f.2⟩ /- porting note: this now requires the `instFunLike` instance, so the instance is now put before `copy`. -/ theorem coe_copy (f : Ω^ N X x) {g : (I^N) → X} (h : g = f) : ⇑(copy f g h) = g := rfl theorem copy_eq (f : Ω^ N X x) {g : (I^N) → X} (h : g = f) : copy f g h = f := by ext x exact congr_fun h x theorem boundary (f : Ω^ N X x) : ∀ y ∈ Cube.boundary N, f y = x := f.2 /-- The constant `GenLoop` at `x`. -/ def const : Ω^ N X x := ⟨ContinuousMap.const _ x, fun _ _ => rfl⟩ @[simp] theorem const_apply {t} : (@const N X _ x) t = x := rfl instance inhabited : Inhabited (Ω^ N X x) := ⟨const⟩ /-- The "homotopic relative to boundary" relation between `GenLoop`s. -/ def Homotopic (f g : Ω^ N X x) : Prop := f.1.HomotopicRel g.1 (Cube.boundary N) namespace Homotopic variable {f g h : Ω^ N X x} @[refl] theorem refl (f : Ω^ N X x) : Homotopic f f := ContinuousMap.HomotopicRel.refl _ @[symm] nonrec theorem symm (H : Homotopic f g) : Homotopic g f := H.symm @[trans] nonrec theorem trans (H0 : Homotopic f g) (H1 : Homotopic g h) : Homotopic f h := H0.trans H1 theorem equiv : Equivalence (@Homotopic N X _ x) := ⟨Homotopic.refl, Homotopic.symm, Homotopic.trans⟩ instance setoid (N) (x : X) : Setoid (Ω^ N X x) := ⟨Homotopic, equiv⟩ end Homotopic section LoopHomeo variable [DecidableEq N] /-- Loop from a generalized loop by currying $I^N → X$ into $I → (I^{N\setminus\{j\}} → X)$. -/ @[simps] def toLoop (i : N) (p : Ω^ N X x) : Ω (Ω^ { j // j ≠ i } X x) const where toFun t := ⟨(p.val.comp (Cube.insertAt i)).curry t, fun y yH => p.property (Cube.insertAt i (t, y)) (Cube.insertAt_boundary i <| Or.inr yH)⟩ source' := by ext t; refine p.property (Cube.insertAt i (0, t)) ⟨i, Or.inl ?_⟩; simp target' := by ext t; refine p.property (Cube.insertAt i (1, t)) ⟨i, Or.inr ?_⟩; simp theorem continuous_toLoop (i : N) : Continuous (@toLoop N X _ x _ i) := Path.continuous_uncurry_iff.1 <| Continuous.subtype_mk (continuous_eval.comp <| Continuous.prodMap (ContinuousMap.continuous_curry.comp <| (ContinuousMap.continuous_precomp _).comp continuous_subtype_val) continuous_id) _ /-- Generalized loop from a loop by uncurrying $I → (I^{N\setminus\{j\}} → X)$ into $I^N → X$. -/ @[simps] def fromLoop (i : N) (p : Ω (Ω^ { j // j ≠ i } X x) const) : Ω^ N X x := ⟨(ContinuousMap.comp ⟨Subtype.val, by fun_prop⟩ p.toContinuousMap).uncurry.comp (Cube.splitAt i), by rintro y ⟨j, Hj⟩ simp only [ContinuousMap.comp_apply, ContinuousMap.coe_coe, funSplitAt_apply, ContinuousMap.uncurry_apply, ContinuousMap.coe_mk, Function.uncurry_apply_pair] obtain rfl | Hne := eq_or_ne j i · rcases Hj with Hj | Hj <;> simp only [Hj, p.coe_toContinuousMap, p.source, p.target] <;> rfl · exact GenLoop.boundary _ _ ⟨⟨j, Hne⟩, Hj⟩⟩ theorem continuous_fromLoop (i : N) : Continuous (@fromLoop N X _ x _ i) := ((ContinuousMap.continuous_precomp _).comp <| ContinuousMap.continuous_uncurry.comp <| (ContinuousMap.continuous_postcomp _).comp continuous_induced_dom).subtype_mk _ theorem to_from (i : N) (p : Ω (Ω^ { j // j ≠ i } X x) const) : toLoop i (fromLoop i p) = p := by simp_rw [toLoop, fromLoop, ContinuousMap.comp_assoc, toContinuousMap_comp_symm, ContinuousMap.comp_id] ext; rfl /-- The `n+1`-dimensional loops are in bijection with the loops in the space of `n`-dimensional loops with base point `const`. We allow an arbitrary indexing type `N` in place of `Fin n` here. -/ @[simps] def loopHomeo (i : N) : Ω^ N X x ≃ₜ Ω (Ω^ { j // j ≠ i } X x) const where toFun := toLoop i invFun := fromLoop i left_inv p := by ext; exact congr_arg p (by dsimp; exact Equiv.apply_symm_apply _ _) right_inv := to_from i continuous_toFun := continuous_toLoop i continuous_invFun := continuous_fromLoop i theorem toLoop_apply (i : N) {p : Ω^ N X x} {t} {tn} : toLoop i p t tn = p (Cube.insertAt i ⟨t, tn⟩) := rfl theorem fromLoop_apply (i : N) {p : Ω (Ω^ { j // j ≠ i } X x) const} {t : I^N} : fromLoop i p t = p (t i) (Cube.splitAt i t).snd := rfl /-- Composition with `Cube.insertAt` as a continuous map. -/ abbrev cCompInsert (i : N) : C(C(I^N, X), C(I × I^{ j // j ≠ i }, X)) := ⟨fun f => f.comp (Cube.insertAt i), (toContinuousMap <| Cube.insertAt i).continuous_precomp⟩ /-- A homotopy between `n+1`-dimensional loops `p` and `q` constant on the boundary seen as a homotopy between two paths in the space of `n`-dimensional paths. -/ def homotopyTo (i : N) {p q : Ω^ N X x} (H : p.1.HomotopyRel q.1 (Cube.boundary N)) : C(I × I, C(I^{ j // j ≠ i }, X)) := ((⟨_, ContinuousMap.continuous_curry⟩ : C(_, _)).comp <| (cCompInsert i).comp H.toContinuousMap.curry).uncurry -- porting note: `@[simps]` generates this lemma but it's named `homotopyTo_apply_apply` instead theorem homotopyTo_apply (i : N) {p q : Ω^ N X x} (H : p.1.HomotopyRel q.1 <| Cube.boundary N) (t : I × I) (tₙ : I^{ j // j ≠ i }) : homotopyTo i H t tₙ = H (t.fst, Cube.insertAt i (t.snd, tₙ)) := rfl theorem homotopicTo (i : N) {p q : Ω^ N X x} : Homotopic p q → (toLoop i p).Homotopic (toLoop i q) := by refine Nonempty.map fun H => ⟨⟨⟨fun t => ⟨homotopyTo i H t, ?_⟩, ?_⟩, ?_, ?_⟩, ?_⟩ · rintro y ⟨i, iH⟩ rw [homotopyTo_apply, H.eq_fst, p.2] all_goals apply Cube.insertAt_boundary; right; exact ⟨i, iH⟩ · fun_prop iterate 2 intro ext dsimp rw [homotopyTo_apply, toLoop_apply] swap · apply H.apply_zero · apply H.apply_one intro t y yH ext dsimp rw [homotopyTo_apply] apply H.eq_fst; use i rw [funSplitAt_symm_apply, dif_pos rfl]; exact yH /-- The converse to `GenLoop.homotopyTo`: a homotopy between two loops in the space of `n`-dimensional loops can be seen as a homotopy between two `n+1`-dimensional paths. -/ @[simps!] def homotopyFrom (i : N) {p q : Ω^ N X x} (H : (toLoop i p).Homotopy (toLoop i q)) : C(I × I^N, X) := (ContinuousMap.comp ⟨_, ContinuousMap.continuous_uncurry⟩ (ContinuousMap.comp ⟨Subtype.val, by fun_prop⟩ H.toContinuousMap).curry).uncurry.comp <| (ContinuousMap.id I).prodMap (Cube.splitAt i) theorem homotopicFrom (i : N) {p q : Ω^ N X x} : (toLoop i p).Homotopic (toLoop i q) → Homotopic p q := by refine Nonempty.map fun H => ⟨⟨homotopyFrom i H, ?_, ?_⟩, ?_⟩ pick_goal 3 · rintro t y ⟨j, jH⟩ erw [homotopyFrom_apply] obtain rfl | h := eq_or_ne j i · simp only [Prod.map_apply, id_eq, funSplitAt_apply, Function.uncurry_apply_pair] rw [H.eq_fst] exacts [congr_arg p ((Cube.splitAt j).left_inv _), jH] · rw [p.2 _ ⟨j, jH⟩]; apply boundary; exact ⟨⟨j, h⟩, jH⟩ all_goals intro apply (homotopyFrom_apply _ _ _).trans simp only [Prod.map_apply, id_eq, funSplitAt_apply, Function.uncurry_apply_pair, ContinuousMap.HomotopyWith.apply_zero, ContinuousMap.HomotopyWith.apply_one, ne_eq, Path.coe_toContinuousMap, toLoop_apply_coe, ContinuousMap.curry_apply, ContinuousMap.comp_apply] first | apply congr_arg p | apply congr_arg q apply (Cube.splitAt i).left_inv /-- Concatenation of two `GenLoop`s along the `i`th coordinate. -/ def transAt (i : N) (f g : Ω^ N X x) : Ω^ N X x := copy (fromLoop i <| (toLoop i f).trans <| toLoop i g) (fun t => if (t i : ℝ) ≤ 1 / 2 then f (Function.update t i <| Set.projIcc 0 1 zero_le_one (2 * t i)) else g (Function.update t i <| Set.projIcc 0 1 zero_le_one (2 * t i - 1))) (by ext1; symm dsimp only [Path.trans, fromLoop, Path.coe_mk_mk, Function.comp_apply, mk_apply, ContinuousMap.comp_apply, ContinuousMap.coe_coe, funSplitAt_apply, ContinuousMap.uncurry_apply, ContinuousMap.coe_mk, Function.uncurry_apply_pair] split_ifs · show f _ = _; congr 1 · show g _ = _; congr 1) /-- Reversal of a `GenLoop` along the `i`th coordinate. -/ def symmAt (i : N) (f : Ω^ N X x) : Ω^ N X x := (copy (fromLoop i (toLoop i f).symm) fun t => f fun j => if j = i then σ (t i) else t j) <| by ext1; change _ = f _; congr; ext1; simp theorem transAt_distrib {i j : N} (h : i ≠ j) (a b c d : Ω^ N X x) : transAt i (transAt j a b) (transAt j c d) = transAt j (transAt i a c) (transAt i b d) := by ext; simp_rw [transAt, coe_copy, Function.update_apply, if_neg h, if_neg h.symm] split_ifs <;> · congr 1; ext1; simp only [Function.update, eq_rec_constant, dite_eq_ite] apply ite_ite_comm; rintro rfl; exact h.symm theorem fromLoop_trans_toLoop {i : N} {p q : Ω^ N X x} : fromLoop i ((toLoop i p).trans <| toLoop i q) = transAt i p q := (copy_eq _ _).symm theorem fromLoop_symm_toLoop {i : N} {p : Ω^ N X x} : fromLoop i (toLoop i p).symm = symmAt i p := (copy_eq _ _).symm end LoopHomeo end GenLoop /-- The `n`th homotopy group at `x` defined as the quotient of `Ω^n x` by the `GenLoop.Homotopic` relation. -/ def HomotopyGroup (N X : Type*) [TopologicalSpace X] (x : X) : Type _ := Quotient (GenLoop.Homotopic.setoid N x) instance : Inhabited (HomotopyGroup N X x) := inferInstanceAs <| Inhabited <| Quotient (GenLoop.Homotopic.setoid N x) variable [DecidableEq N] open GenLoop /-- Equivalence between the homotopy group of X and the fundamental group of `Ω^{j // j ≠ i} x`. -/ def homotopyGroupEquivFundamentalGroup (i : N) : HomotopyGroup N X x ≃ FundamentalGroup (Ω^ { j // j ≠ i } X x) const := by refine Equiv.trans ?_ (CategoryTheory.Groupoid.isoEquivHom _ _).symm apply Quotient.congr (loopHomeo i).toEquiv exact fun p q => ⟨homotopicTo i, homotopicFrom i⟩ /-- Homotopy group of finite index, denoted as `π_n` within the Topology namespace. -/ abbrev HomotopyGroup.Pi (n) (X : Type*) [TopologicalSpace X] (x : X) := HomotopyGroup (Fin n) _ x @[inherit_doc] scoped[Topology] notation "π_" => HomotopyGroup.Pi /-- The 0-dimensional generalized loops based at `x` are in bijection with `X`. -/ def genLoopHomeoOfIsEmpty (N x) [IsEmpty N] : Ω^ N X x ≃ₜ X where toFun f := f 0 invFun y := ⟨ContinuousMap.const _ y, fun _ ⟨i, _⟩ => isEmptyElim i⟩ left_inv f := by ext; exact congr_arg f (Subsingleton.elim _ _) right_inv _ := rfl continuous_invFun := ContinuousMap.const'.2.subtype_mk _ /-- The homotopy "group" indexed by an empty type is in bijection with the path components of `X`, aka the `ZerothHomotopy`. -/ def homotopyGroupEquivZerothHomotopyOfIsEmpty (N x) [IsEmpty N] : HomotopyGroup N X x ≃ ZerothHomotopy X := Quotient.congr (genLoopHomeoOfIsEmpty N x).toEquiv (by -- joined iff homotopic intros a₁ a₂ constructor <;> rintro ⟨H⟩ exacts [⟨{ toFun := fun t => H ⟨t, isEmptyElim⟩ source' := (H.apply_zero _).trans (congr_arg a₁ <| Subsingleton.elim _ _) target' := (H.apply_one _).trans (congr_arg a₂ <| Subsingleton.elim _ _) }⟩, ⟨{ toFun := fun t0 => H t0.fst map_zero_left := fun _ => H.source.trans (congr_arg a₁ <| Subsingleton.elim _ _) map_one_left := fun _ => H.target.trans (congr_arg a₂ <| Subsingleton.elim _ _) prop' := fun _ _ ⟨i, _⟩ => isEmptyElim i }⟩]) /-- The 0th homotopy "group" is in bijection with `ZerothHomotopy`. -/ def HomotopyGroup.pi0EquivZerothHomotopy : π_ 0 X x ≃ ZerothHomotopy X := homotopyGroupEquivZerothHomotopyOfIsEmpty (Fin 0) x /-- The 1-dimensional generalized loops based at `x` are in bijection with loops at `x`. -/ def genLoopEquivOfUnique (N) [Unique N] : Ω^ N X x ≃ Ω X x where toFun p := Path.mk ⟨fun t => p fun _ => t, by continuity⟩ (GenLoop.boundary _ (fun _ => 0) ⟨default, Or.inl rfl⟩) (GenLoop.boundary _ (fun _ => 1) ⟨default, Or.inr rfl⟩) invFun p := ⟨⟨fun c => p (c default), by continuity⟩, by rintro y ⟨i, iH | iH⟩ <;> cases Unique.eq_default i <;> apply (congr_arg p iH).trans exacts [p.source, p.target]⟩ left_inv p := by ext y; exact congr_arg p (eq_const_of_unique y).symm right_inv p := by ext; rfl /- TODO (?): deducing this from `homotopyGroupEquivFundamentalGroup` would require combination of `CategoryTheory.Functor.mapAut` and `FundamentalGroupoid.fundamentalGroupoidFunctor` applied to `genLoopHomeoOfIsEmpty`, with possibly worse defeq. -/ /-- The homotopy group at `x` indexed by a singleton is in bijection with the fundamental group, i.e. the loops based at `x` up to homotopy. -/ def homotopyGroupEquivFundamentalGroupOfUnique (N) [Unique N] : HomotopyGroup N X x ≃ FundamentalGroup X x := by refine Equiv.trans ?_ (CategoryTheory.Groupoid.isoEquivHom _ _).symm refine Quotient.congr (genLoopEquivOfUnique N) ?_ intros a₁ a₂; constructor <;> rintro ⟨H⟩ · exact ⟨{ toFun := fun tx => H (tx.fst, fun _ => tx.snd) map_zero_left := fun _ => H.apply_zero _ map_one_left := fun _ => H.apply_one _ prop' := fun t y iH => H.prop' _ _ ⟨default, iH⟩ }⟩ refine ⟨⟨⟨⟨fun tx => H (tx.fst, tx.snd default), H.continuous.comp ?_⟩, fun y => ?_, fun y => ?_⟩, ?_⟩⟩ · fun_prop · exact (H.apply_zero _).trans (congr_arg a₁ (eq_const_of_unique y).symm) · exact (H.apply_one _).trans (congr_arg a₂ (eq_const_of_unique y).symm) · rintro t y ⟨i, iH⟩ cases Unique.eq_default i exact (H.eq_fst _ iH).trans (congr_arg a₁ (eq_const_of_unique y).symm) /-- The first homotopy group at `x` is in bijection with the fundamental group. -/ def HomotopyGroup.pi1EquivFundamentalGroup : π_ 1 X x ≃ FundamentalGroup X x := homotopyGroupEquivFundamentalGroupOfUnique (Fin 1) namespace HomotopyGroup /-- Group structure on `HomotopyGroup N X x` for nonempty `N` (in particular `π_(n+1) X x`). -/ instance group (N) [DecidableEq N] [Nonempty N] : Group (HomotopyGroup N X x) := (homotopyGroupEquivFundamentalGroup <| Classical.arbitrary N).group /-- Group structure on `HomotopyGroup` obtained by pulling back path composition along the `i`th direction. The group structures for two different `i j : N` distribute over each other, and therefore are equal by the Eckmann-Hilton argument. -/ abbrev auxGroup (i : N) : Group (HomotopyGroup N X x) := (homotopyGroupEquivFundamentalGroup i).group theorem isUnital_auxGroup (i : N) : EckmannHilton.IsUnital (auxGroup i).mul (⟦const⟧ : HomotopyGroup N X x) where left_id := (auxGroup i).one_mul right_id := (auxGroup i).mul_one theorem auxGroup_indep (i j : N) : (auxGroup i : Group (HomotopyGroup N X x)) = auxGroup j := by by_cases h : i = j; · rw [h] refine Group.ext (EckmannHilton.mul (isUnital_auxGroup i) (isUnital_auxGroup j) ?_) rintro ⟨a⟩ ⟨b⟩ ⟨c⟩ ⟨d⟩ change Quotient.mk' _ = _ apply congr_arg Quotient.mk' simp only [fromLoop_trans_toLoop, transAt_distrib h, coe_toEquiv, loopHomeo_apply, coe_symm_toEquiv, loopHomeo_symm_apply] theorem transAt_indep {i} (j) (f g : Ω^ N X x) : (⟦transAt i f g⟧ : HomotopyGroup N X x) = ⟦transAt j f g⟧ := by simp_rw [← fromLoop_trans_toLoop] let m := fun (G) (_ : Group G) => ((· * ·) : G → G → G) exact congr_fun₂ (congr_arg (m <| HomotopyGroup N X x) <| auxGroup_indep i j) ⟦g⟧ ⟦f⟧ theorem symmAt_indep {i} (j) (f : Ω^ N X x) : (⟦symmAt i f⟧ : HomotopyGroup N X x) = ⟦symmAt j f⟧ := by simp_rw [← fromLoop_symm_toLoop] let inv := fun (G) (_ : Group G) => ((·⁻¹) : G → G) exact congr_fun (congr_arg (inv <| HomotopyGroup N X x) <| auxGroup_indep i j) ⟦f⟧ /-- Characterization of multiplicative identity -/ theorem one_def [Nonempty N] : (1 : HomotopyGroup N X x) = ⟦const⟧ := rfl /-- Characterization of multiplication -/ theorem mul_spec [Nonempty N] {i} {p q : Ω^ N X x} : -- TODO: introduce `HomotopyGroup.mk` and remove defeq abuse. ((· * ·) : _ → _ → HomotopyGroup N X x) ⟦p⟧ ⟦q⟧ = ⟦transAt i q p⟧ := by rw [transAt_indep (Classical.arbitrary N) q, ← fromLoop_trans_toLoop] apply Quotient.sound rfl /-- Characterization of multiplicative inverse -/ theorem inv_spec [Nonempty N] {i} {p : Ω^ N X x} : ((⟦p⟧)⁻¹ : HomotopyGroup N X x) = ⟦symmAt i p⟧ := by rw [symmAt_indep (Classical.arbitrary N) p, ← fromLoop_symm_toLoop] apply Quotient.sound rfl /-- Multiplication on `HomotopyGroup N X x` is commutative for nontrivial `N`. In particular, multiplication on `π_(n+2)` is commutative. -/ instance commGroup [Nontrivial N] : CommGroup (HomotopyGroup N X x) := let h := exists_ne (Classical.arbitrary N) @EckmannHilton.commGroup (HomotopyGroup N X x) _ 1 (isUnital_auxGroup <| Classical.choose h) _ (by rintro ⟨a⟩ ⟨b⟩ ⟨c⟩ ⟨d⟩ apply congr_arg Quotient.mk' simp only [fromLoop_trans_toLoop, transAt_distrib <| Classical.choose_spec h, coe_toEquiv, loopHomeo_apply, coe_symm_toEquiv, loopHomeo_symm_apply]) end HomotopyGroup
Mathlib/Topology/Homotopy/HomotopyGroup.lean
548
553
/- Copyright (c) 2020 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta, Emily Riehl -/ import Mathlib.CategoryTheory.Adjunction.Basic import Mathlib.CategoryTheory.Functor.TwoSquare import Mathlib.CategoryTheory.HomCongr import Mathlib.Tactic.ApplyFun /-! # Mate of natural transformations This file establishes the bijection between the 2-cells ``` L₁ R₁ C --→ D C ←-- D G ↓ ↗ ↓ H G ↓ ↘ ↓ H E --→ F E ←-- F L₂ R₂ ``` where `L₁ ⊣ R₁` and `L₂ ⊣ R₂`. The corresponding natural transformations are called mates. This bijection includes a number of interesting cases as specializations. For instance, in the special case where `G,H` are identity functors then the bijection preserves and reflects isomorphisms (i.e. we have bijections`(L₂ ⟶ L₁) ≃ (R₁ ⟶ R₂)`, and if either side is an iso then the other side is as well). This demonstrates that adjoints to a given functor are unique up to isomorphism (since if `L₁ ≅ L₂` then we deduce `R₁ ≅ R₂`). Another example arises from considering the square representing that a functor `H` preserves products, in particular the morphism `HA ⨯ H- ⟶ H(A ⨯ -)`. Then provided `(A ⨯ -)` and `HA ⨯ -` have left adjoints (for instance if the relevant categories are cartesian closed), the transferred natural transformation is the exponential comparison morphism: `H(A ^ -) ⟶ HA ^ H-`. Furthermore if `H` has a left adjoint `L`, this morphism is an isomorphism iff its mate `L(HA ⨯ -) ⟶ A ⨯ L-` is an isomorphism, see https://ncatlab.org/nlab/show/Frobenius+reciprocity#InCategoryTheory. This also relates to Grothendieck's yoga of six operations, though this is not spelled out in mathlib: https://ncatlab.org/nlab/show/six+operations. -/ universe v₁ v₂ v₃ v₄ v₅ v₆ v₇ v₈ v₉ u₁ u₂ u₃ u₄ u₅ u₆ u₇ u₈ u₉ namespace CategoryTheory open Category Functor Adjunction NatTrans TwoSquare section mateEquiv variable {C : Type u₁} {D : Type u₂} {E : Type u₃} {F : Type u₄} variable [Category.{v₁} C] [Category.{v₂} D] [Category.{v₃} E] [Category.{v₄} F] variable {G : C ⥤ E} {H : D ⥤ F} {L₁ : C ⥤ D} {R₁ : D ⥤ C} {L₂ : E ⥤ F} {R₂ : F ⥤ E} variable (adj₁ : L₁ ⊣ R₁) (adj₂ : L₂ ⊣ R₂) /-- Suppose we have a square of functors (where the top and bottom are adjunctions `L₁ ⊣ R₁` and `L₂ ⊣ R₂` respectively). ``` C ↔ D G ↓ ↓ H E ↔ F ``` Then we have a bijection between natural transformations `G ⋙ L₂ ⟶ L₁ ⋙ H` and `R₁ ⋙ G ⟶ H ⋙ R₂`. This can be seen as a bijection of the 2-cells: ``` L₁ R₁ C --→ D C ←-- D G ↓ ↗ ↓ H G ↓ ↘ ↓ H E --→ F E ←-- F L₂ R₂ ``` Note that if one of the transformations is an iso, it does not imply the other is an iso. -/ @[simps] def mateEquiv : TwoSquare G L₁ L₂ H ≃ TwoSquare R₁ H G R₂ where toFun α := .mk _ _ _ _ <| whiskerLeft (R₁ ⋙ G) adj₂.unit ≫ whiskerRight (whiskerLeft R₁ α.natTrans) R₂ ≫ whiskerRight adj₁.counit (H ⋙ R₂) invFun β := .mk _ _ _ _ <| whiskerRight adj₁.unit (G ⋙ L₂) ≫ whiskerRight (whiskerLeft L₁ β.natTrans) L₂ ≫ whiskerLeft (L₁ ⋙ H) adj₂.counit left_inv α := by ext unfold whiskerRight whiskerLeft simp only [comp_obj, id_obj, Functor.comp_map, comp_app, map_comp, assoc, counit_naturality, counit_naturality_assoc, left_triangle_components_assoc] rw [← assoc, ← Functor.comp_map, α.natTrans.naturality, Functor.comp_map, assoc, ← H.map_comp, left_triangle_components, map_id] simp only [comp_obj, comp_id] right_inv β := by ext unfold whiskerLeft whiskerRight simp only [comp_obj, id_obj, Functor.comp_map, comp_app, map_comp, assoc, unit_naturality_assoc, right_triangle_components_assoc] rw [← assoc, ← Functor.comp_map, assoc, ← β.natTrans.naturality, ← assoc, Functor.comp_map, ← G.map_comp, right_triangle_components, map_id, id_comp] /-- A component of a transposed version of the mates correspondence. -/ theorem mateEquiv_counit (α : TwoSquare G L₁ L₂ H) (d : D) : L₂.map ((mateEquiv adj₁ adj₂ α).app _) ≫ adj₂.counit.app _ = α.app _ ≫ H.map (adj₁.counit.app d) := by simp /-- A component of a transposed version of the inverse mates correspondence. -/ theorem mateEquiv_counit_symm (α : TwoSquare R₁ H G R₂) (d : D) : L₂.map (α.app _) ≫ adj₂.counit.app _ = ((mateEquiv adj₁ adj₂).symm α).app _ ≫ H.map (adj₁.counit.app d) := by conv_lhs => rw [← (mateEquiv adj₁ adj₂).right_inv α] exact (mateEquiv_counit adj₁ adj₂ ((mateEquiv adj₁ adj₂).symm α) d) /- A component of a transposed version of the mates correspondence. -/ theorem unit_mateEquiv (α : TwoSquare G L₁ L₂ H) (c : C) : G.map (adj₁.unit.app c) ≫ (mateEquiv adj₁ adj₂ α).app _ = adj₂.unit.app _ ≫ R₂.map (α.app _) := by dsimp [mateEquiv] rw [← adj₂.unit_naturality_assoc] slice_lhs 2 3 => rw [← R₂.map_comp, ← Functor.comp_map G L₂, α.naturality] rw [R₂.map_comp] slice_lhs 3 4 => rw [← R₂.map_comp, Functor.comp_map L₁ H, ← H.map_comp, left_triangle_components] simp only [comp_obj, map_id, comp_id] /-- A component of a transposed version of the inverse mates correspondence. -/ theorem unit_mateEquiv_symm (α : TwoSquare R₁ H G R₂) (c : C) : G.map (adj₁.unit.app c) ≫ α.app _ = adj₂.unit.app _ ≫ R₂.map (((mateEquiv adj₁ adj₂).symm α).app _) := by conv_lhs => rw [← (mateEquiv adj₁ adj₂).right_inv α] exact (unit_mateEquiv adj₁ adj₂ ((mateEquiv adj₁ adj₂).symm α) c) end mateEquiv section mateEquivVComp variable {A : Type u₁} {B : Type u₂} {C : Type u₃} {D : Type u₄} {E : Type u₅} {F : Type u₆} variable [Category.{v₁} A] [Category.{v₂} B] [Category.{v₃} C] variable [Category.{v₄} D] [Category.{v₅} E] [Category.{v₆} F] variable {G₁ : A ⥤ C} {G₂ : C ⥤ E} {H₁ : B ⥤ D} {H₂ : D ⥤ F} variable {L₁ : A ⥤ B} {R₁ : B ⥤ A} {L₂ : C ⥤ D} {R₂ : D ⥤ C} {L₃ : E ⥤ F} {R₃ : F ⥤ E} variable (adj₁ : L₁ ⊣ R₁) (adj₂ : L₂ ⊣ R₂) (adj₃ : L₃ ⊣ R₃) /-- The mates equivalence commutes with vertical composition. -/ theorem mateEquiv_vcomp (α : TwoSquare G₁ L₁ L₂ H₁) (β : TwoSquare G₂ L₂ L₃ H₂) : (mateEquiv adj₁ adj₃) (α ≫ₕ β) = (mateEquiv adj₁ adj₂ α) ≫ᵥ (mateEquiv adj₂ adj₃ β) := by unfold hComp vComp mateEquiv ext b simp only [comp_obj, Equiv.coe_fn_mk, whiskerLeft_comp, whiskerLeft_twice, whiskerRight_comp, assoc, comp_app, whiskerLeft_app, whiskerRight_app, associator_hom_app, map_id, associator_inv_app, id_obj, Functor.comp_map, id_comp, whiskerRight_twice, comp_id] slice_rhs 1 4 => rw [← assoc, ← assoc, ← unit_naturality (adj₃)] rw [L₃.map_comp, R₃.map_comp] slice_rhs 2 4 => rw [← R₃.map_comp, ← R₃.map_comp, ← assoc, ← L₃.map_comp, ← G₂.map_comp, ← G₂.map_comp] rw [← Functor.comp_map G₂ L₃, β.naturality] rw [(L₂ ⋙ H₂).map_comp, R₃.map_comp, R₃.map_comp] slice_rhs 4 5 => rw [← R₃.map_comp, Functor.comp_map L₂ _, ← Functor.comp_map _ L₂, ← H₂.map_comp] rw [adj₂.counit.naturality]
simp only [comp_obj, Functor.comp_map, map_comp, id_obj, Functor.id_map, assoc] slice_rhs 4 5 => rw [← R₃.map_comp, ← H₂.map_comp, ← Functor.comp_map _ L₂, adj₂.counit.naturality] simp only [comp_obj, id_obj, Functor.id_map, map_comp, assoc] slice_rhs 3 4 => rw [← R₃.map_comp, ← H₂.map_comp, left_triangle_components] simp only [map_id, id_comp] end mateEquivVComp
Mathlib/CategoryTheory/Adjunction/Mates.lean
164
172
/- Copyright (c) 2024 Mitchell Lee. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mitchell Lee, Óscar Álvarez -/ import Mathlib.GroupTheory.Coxeter.Length import Mathlib.Data.List.GetD import Mathlib.Tactic.Group /-! # Reflections, inversions, and inversion sequences Throughout this file, `B` is a type and `M : CoxeterMatrix B` is a Coxeter matrix. `cs : CoxeterSystem M W` is a Coxeter system; that is, `W` is a group, and `cs` holds the data of a group isomorphism `W ≃* M.group`, where `M.group` refers to the quotient of the free group on `B` by the Coxeter relations given by the matrix `M`. See `Mathlib/GroupTheory/Coxeter/Basic.lean` for more details. We define a *reflection* (`CoxeterSystem.IsReflection`) to be an element of the form $t = u s_i u^{-1}$, where $u \in W$ and $s_i$ is a simple reflection. We say that a reflection $t$ is a *left inversion* (`CoxeterSystem.IsLeftInversion`) of an element $w \in W$ if $\ell(t w) < \ell(w)$, and we say it is a *right inversion* (`CoxeterSystem.IsRightInversion`) of $w$ if $\ell(w t) > \ell(w)$. Here $\ell$ is the length function (see `Mathlib/GroupTheory/Coxeter/Length.lean`). Given a word, we define its *left inversion sequence* (`CoxeterSystem.leftInvSeq`) and its *right inversion sequence* (`CoxeterSystem.rightInvSeq`). We prove that if a word is reduced, then both of its inversion sequences contain no duplicates. In fact, the right (respectively, left) inversion sequence of a reduced word for $w$ consists of all of the right (respectively, left) inversions of $w$ in some order, but we do not prove that in this file. ## Main definitions * `CoxeterSystem.IsReflection` * `CoxeterSystem.IsLeftInversion` * `CoxeterSystem.IsRightInversion` * `CoxeterSystem.leftInvSeq` * `CoxeterSystem.rightInvSeq` ## References * [A. Björner and F. Brenti, *Combinatorics of Coxeter Groups*](bjorner2005) -/ assert_not_exists TwoSidedIdeal namespace CoxeterSystem open List Matrix Function variable {B : Type*} variable {W : Type*} [Group W] variable {M : CoxeterMatrix B} (cs : CoxeterSystem M W) local prefix:100 "s" => cs.simple local prefix:100 "π" => cs.wordProd local prefix:100 "ℓ" => cs.length /-- `t : W` is a *reflection* of the Coxeter system `cs` if it is of the form $w s_i w^{-1}$, where $w \in W$ and $s_i$ is a simple reflection. -/ def IsReflection (t : W) : Prop := ∃ w i, t = w * s i * w⁻¹ theorem isReflection_simple (i : B) : cs.IsReflection (s i) := by use 1, i; simp namespace IsReflection variable {cs} variable {t : W} (ht : cs.IsReflection t) include ht theorem pow_two : t ^ 2 = 1 := by rcases ht with ⟨w, i, rfl⟩ simp theorem mul_self : t * t = 1 := by rcases ht with ⟨w, i, rfl⟩ simp theorem inv : t⁻¹ = t := by rcases ht with ⟨w, i, rfl⟩ simp [mul_assoc] theorem isReflection_inv : cs.IsReflection t⁻¹ := by rwa [ht.inv] theorem odd_length : Odd (ℓ t) := by suffices cs.lengthParity t = Multiplicative.ofAdd 1 by simpa [lengthParity_eq_ofAdd_length, ZMod.eq_one_iff_odd] rcases ht with ⟨w, i, rfl⟩ simp [lengthParity_simple] theorem length_mul_left_ne (w : W) : ℓ (w * t) ≠ ℓ w := by suffices cs.lengthParity (w * t) ≠ cs.lengthParity w by contrapose! this simp only [lengthParity_eq_ofAdd_length, this] rcases ht with ⟨w, i, rfl⟩ simp [lengthParity_simple] theorem length_mul_right_ne (w : W) : ℓ (t * w) ≠ ℓ w := by suffices cs.lengthParity (t * w) ≠ cs.lengthParity w by contrapose! this simp only [lengthParity_eq_ofAdd_length, this] rcases ht with ⟨w, i, rfl⟩ simp [lengthParity_simple] theorem conj (w : W) : cs.IsReflection (w * t * w⁻¹) := by obtain ⟨u, i, rfl⟩ := ht use w * u, i group end IsReflection @[simp] theorem isReflection_conj_iff (w t : W) : cs.IsReflection (w * t * w⁻¹) ↔ cs.IsReflection t := by constructor · intro h simpa [← mul_assoc] using h.conj w⁻¹ · exact IsReflection.conj (w := w) /-- The proposition that `t` is a right inversion of `w`; i.e., `t` is a reflection and $\ell (w t) < \ell(w)$. -/ def IsRightInversion (w t : W) : Prop := cs.IsReflection t ∧ ℓ (w * t) < ℓ w /-- The proposition that `t` is a left inversion of `w`; i.e., `t` is a reflection and $\ell (t w) < \ell(w)$. -/ def IsLeftInversion (w t : W) : Prop := cs.IsReflection t ∧ ℓ (t * w) < ℓ w theorem isRightInversion_inv_iff {w t : W} : cs.IsRightInversion w⁻¹ t ↔ cs.IsLeftInversion w t := by apply and_congr_right intro ht rw [← length_inv, mul_inv_rev, inv_inv, ht.inv, cs.length_inv w] theorem isLeftInversion_inv_iff {w t : W} : cs.IsLeftInversion w⁻¹ t ↔ cs.IsRightInversion w t := by convert cs.isRightInversion_inv_iff.symm simp namespace IsReflection variable {cs} variable {t : W} (ht : cs.IsReflection t) include ht theorem isRightInversion_mul_left_iff {w : W} : cs.IsRightInversion (w * t) t ↔ ¬cs.IsRightInversion w t := by unfold IsRightInversion simp only [mul_assoc, ht.inv, ht.mul_self, mul_one, ht, true_and, not_lt] constructor · exact le_of_lt · exact (lt_of_le_of_ne' · (ht.length_mul_left_ne w)) theorem not_isRightInversion_mul_left_iff {w : W} : ¬cs.IsRightInversion (w * t) t ↔ cs.IsRightInversion w t := ht.isRightInversion_mul_left_iff.not_left theorem isLeftInversion_mul_right_iff {w : W} : cs.IsLeftInversion (t * w) t ↔ ¬cs.IsLeftInversion w t := by rw [← isRightInversion_inv_iff, ← isRightInversion_inv_iff, mul_inv_rev, ht.inv, ht.isRightInversion_mul_left_iff] theorem not_isLeftInversion_mul_right_iff {w : W} : ¬cs.IsLeftInversion (t * w) t ↔ cs.IsLeftInversion w t := ht.isLeftInversion_mul_right_iff.not_left end IsReflection @[simp] theorem isRightInversion_simple_iff_isRightDescent (w : W) (i : B) : cs.IsRightInversion w (s i) ↔ cs.IsRightDescent w i := by simp [IsRightInversion, IsRightDescent, cs.isReflection_simple i] @[simp] theorem isLeftInversion_simple_iff_isLeftDescent (w : W) (i : B) : cs.IsLeftInversion w (s i) ↔ cs.IsLeftDescent w i := by simp [IsLeftInversion, IsLeftDescent, cs.isReflection_simple i] /-- The right inversion sequence of `ω`. The right inversion sequence of a word $s_{i_1} \cdots s_{i_\ell}$ is the sequence $$s_{i_\ell}\cdots s_{i_1}\cdots s_{i_\ell}, \ldots, s_{i_{\ell}}s_{i_{\ell - 1}}s_{i_{\ell - 2}}s_{i_{\ell - 1}}s_{i_\ell}, \ldots, s_{i_{\ell}}s_{i_{\ell - 1}}s_{i_\ell}, s_{i_\ell}.$$ -/ def rightInvSeq (ω : List B) : List W := match ω with | [] => [] | i :: ω => (π ω)⁻¹ * (s i) * (π ω) :: rightInvSeq ω /-- The left inversion sequence of `ω`. The left inversion sequence of a word $s_{i_1} \cdots s_{i_\ell}$ is the sequence $$s_{i_1}, s_{i_1}s_{i_2}s_{i_1}, s_{i_1}s_{i_2}s_{i_3}s_{i_2}s_{i_1}, \ldots, s_{i_1}\cdots s_{i_\ell}\cdots s_{i_1}.$$ -/ def leftInvSeq (ω : List B) : List W := match ω with | [] => [] | i :: ω => s i :: List.map (MulAut.conj (s i)) (leftInvSeq ω) local prefix:100 "ris" => cs.rightInvSeq local prefix:100 "lis" => cs.leftInvSeq @[simp] theorem rightInvSeq_nil : ris [] = [] := rfl @[simp] theorem leftInvSeq_nil : lis [] = [] := rfl @[simp] theorem rightInvSeq_singleton (i : B) : ris [i] = [s i] := by simp [rightInvSeq] @[simp] theorem leftInvSeq_singleton (i : B) : lis [i] = [s i] := rfl theorem rightInvSeq_concat (ω : List B) (i : B) : ris (ω.concat i) = (List.map (MulAut.conj (s i)) (ris ω)).concat (s i) := by induction' ω with j ω ih · simp · dsimp [rightInvSeq, concat] rw [ih] simp only [concat_eq_append, wordProd_append, wordProd_cons, wordProd_nil, mul_one, mul_inv_rev, inv_simple, cons_append, cons.injEq, and_true] group private theorem leftInvSeq_eq_reverse_rightInvSeq_reverse (ω : List B) : lis ω = (ris ω.reverse).reverse := by induction' ω with i ω ih · simp · rw [leftInvSeq, reverse_cons, ← concat_eq_append, rightInvSeq_concat, ih] simp [map_reverse] theorem leftInvSeq_concat (ω : List B) (i : B) : lis (ω.concat i) = (lis ω).concat ((π ω) * (s i) * (π ω)⁻¹) := by simp [leftInvSeq_eq_reverse_rightInvSeq_reverse, rightInvSeq] theorem rightInvSeq_reverse (ω : List B) : ris (ω.reverse) = (lis ω).reverse := by simp [leftInvSeq_eq_reverse_rightInvSeq_reverse] theorem leftInvSeq_reverse (ω : List B) : lis (ω.reverse) = (ris ω).reverse := by simp [leftInvSeq_eq_reverse_rightInvSeq_reverse]
@[simp] theorem length_rightInvSeq (ω : List B) : (ris ω).length = ω.length := by induction' ω with i ω ih
Mathlib/GroupTheory/Coxeter/Inversion.lean
240
241
/- Copyright (c) 2020 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import Mathlib.Topology.Algebra.Module.LinearMapPiProd import Mathlib.LinearAlgebra.Multilinear.Basic /-! # Continuous multilinear maps We define continuous multilinear maps as maps from `(i : ι) → M₁ i` to `M₂` which are multilinear and continuous, by extending the space of multilinear maps with a continuity assumption. Here, `M₁ i` and `M₂` are modules over a ring `R`, and `ι` is an arbitrary type, and all these spaces are also topological spaces. ## Main definitions * `ContinuousMultilinearMap R M₁ M₂` is the space of continuous multilinear maps from `(i : ι) → M₁ i` to `M₂`. We show that it is an `R`-module. ## Implementation notes We mostly follow the API of multilinear maps. ## Notation We introduce the notation `M [×n]→L[R] M'` for the space of continuous `n`-multilinear maps from `M^n` to `M'`. This is a particular case of the general notion (where we allow varying dependent types as the arguments of our continuous multilinear maps), but arguably the most important one, especially when defining iterated derivatives. -/ open Function Fin Set universe u v w w₁ w₁' w₂ w₃ w₄ variable {R : Type u} {ι : Type v} {n : ℕ} {M : Fin n.succ → Type w} {M₁ : ι → Type w₁} {M₁' : ι → Type w₁'} {M₂ : Type w₂} {M₃ : Type w₃} {M₄ : Type w₄} /-- Continuous multilinear maps over the ring `R`, from `∀ i, M₁ i` to `M₂` where `M₁ i` and `M₂` are modules over `R` with a topological structure. In applications, there will be compatibility conditions between the algebraic and the topological structures, but this is not needed for the definition. -/ structure ContinuousMultilinearMap (R : Type u) {ι : Type v} (M₁ : ι → Type w₁) (M₂ : Type w₂) [Semiring R] [∀ i, AddCommMonoid (M₁ i)] [AddCommMonoid M₂] [∀ i, Module R (M₁ i)] [Module R M₂] [∀ i, TopologicalSpace (M₁ i)] [TopologicalSpace M₂] extends MultilinearMap R M₁ M₂ where cont : Continuous toFun attribute [inherit_doc ContinuousMultilinearMap] ContinuousMultilinearMap.cont @[inherit_doc] notation:25 M " [×" n "]→L[" R "] " M' => ContinuousMultilinearMap R (fun i : Fin n => M) M' namespace ContinuousMultilinearMap section Semiring variable [Semiring R] [∀ i, AddCommMonoid (M i)] [∀ i, AddCommMonoid (M₁ i)] [∀ i, AddCommMonoid (M₁' i)] [AddCommMonoid M₂] [AddCommMonoid M₃] [AddCommMonoid M₄] [∀ i, Module R (M i)] [∀ i, Module R (M₁ i)] [∀ i, Module R (M₁' i)] [Module R M₂] [Module R M₃] [Module R M₄] [∀ i, TopologicalSpace (M i)] [∀ i, TopologicalSpace (M₁ i)] [∀ i, TopologicalSpace (M₁' i)] [TopologicalSpace M₂] [TopologicalSpace M₃] [TopologicalSpace M₄] (f f' : ContinuousMultilinearMap R M₁ M₂) theorem toMultilinearMap_injective : Function.Injective (ContinuousMultilinearMap.toMultilinearMap : ContinuousMultilinearMap R M₁ M₂ → MultilinearMap R M₁ M₂) | ⟨f, hf⟩, ⟨g, hg⟩, h => by subst h; rfl instance funLike : FunLike (ContinuousMultilinearMap R M₁ M₂) (∀ i, M₁ i) M₂ where coe f := f.toFun coe_injective' _ _ h := toMultilinearMap_injective <| MultilinearMap.coe_injective h instance continuousMapClass : ContinuousMapClass (ContinuousMultilinearMap R M₁ M₂) (∀ i, M₁ i) M₂ where map_continuous := ContinuousMultilinearMap.cont /-- See Note [custom simps projection]. We need to specify this projection explicitly in this case, because it is a composition of multiple projections. -/ def Simps.apply (L₁ : ContinuousMultilinearMap R M₁ M₂) (v : ∀ i, M₁ i) : M₂ := L₁ v initialize_simps_projections ContinuousMultilinearMap (-toMultilinearMap, toMultilinearMap_toFun → apply) @[continuity] theorem coe_continuous : Continuous (f : (∀ i, M₁ i) → M₂) := f.cont @[simp] theorem coe_coe : (f.toMultilinearMap : (∀ i, M₁ i) → M₂) = f := rfl @[ext] theorem ext {f f' : ContinuousMultilinearMap R M₁ M₂} (H : ∀ x, f x = f' x) : f = f' := DFunLike.ext _ _ H @[simp] theorem map_update_add [DecidableEq ι] (m : ∀ i, M₁ i) (i : ι) (x y : M₁ i) : f (update m i (x + y)) = f (update m i x) + f (update m i y) := f.map_update_add' m i x y @[deprecated (since := "2024-11-03")] protected alias map_add := ContinuousMultilinearMap.map_update_add @[simp] theorem map_update_smul [DecidableEq ι] (m : ∀ i, M₁ i) (i : ι) (c : R) (x : M₁ i) : f (update m i (c • x)) = c • f (update m i x) := f.map_update_smul' m i c x @[deprecated (since := "2024-11-03")] protected alias map_smul := ContinuousMultilinearMap.map_update_smul theorem map_coord_zero {m : ∀ i, M₁ i} (i : ι) (h : m i = 0) : f m = 0 := f.toMultilinearMap.map_coord_zero i h @[simp] theorem map_zero [Nonempty ι] : f 0 = 0 := f.toMultilinearMap.map_zero instance : Zero (ContinuousMultilinearMap R M₁ M₂) := ⟨{ (0 : MultilinearMap R M₁ M₂) with cont := continuous_const }⟩ instance : Inhabited (ContinuousMultilinearMap R M₁ M₂) := ⟨0⟩ @[simp] theorem zero_apply (m : ∀ i, M₁ i) : (0 : ContinuousMultilinearMap R M₁ M₂) m = 0 := rfl @[simp] theorem toMultilinearMap_zero : (0 : ContinuousMultilinearMap R M₁ M₂).toMultilinearMap = 0 := rfl section SMul variable {R' R'' A : Type*} [Monoid R'] [Monoid R''] [Semiring A] [∀ i, Module A (M₁ i)] [Module A M₂] [DistribMulAction R' M₂] [ContinuousConstSMul R' M₂] [SMulCommClass A R' M₂] [DistribMulAction R'' M₂] [ContinuousConstSMul R'' M₂] [SMulCommClass A R'' M₂] instance : SMul R' (ContinuousMultilinearMap A M₁ M₂) := ⟨fun c f => { c • f.toMultilinearMap with cont := f.cont.const_smul c }⟩ @[simp] theorem smul_apply (f : ContinuousMultilinearMap A M₁ M₂) (c : R') (m : ∀ i, M₁ i) : (c • f) m = c • f m := rfl @[simp] theorem toMultilinearMap_smul (c : R') (f : ContinuousMultilinearMap A M₁ M₂) : (c • f).toMultilinearMap = c • f.toMultilinearMap := rfl instance [SMulCommClass R' R'' M₂] : SMulCommClass R' R'' (ContinuousMultilinearMap A M₁ M₂) := ⟨fun _ _ _ => ext fun _ => smul_comm _ _ _⟩ instance [SMul R' R''] [IsScalarTower R' R'' M₂] : IsScalarTower R' R'' (ContinuousMultilinearMap A M₁ M₂) := ⟨fun _ _ _ => ext fun _ => smul_assoc _ _ _⟩ instance [DistribMulAction R'ᵐᵒᵖ M₂] [IsCentralScalar R' M₂] : IsCentralScalar R' (ContinuousMultilinearMap A M₁ M₂) := ⟨fun _ _ => ext fun _ => op_smul_eq_smul _ _⟩ instance : MulAction R' (ContinuousMultilinearMap A M₁ M₂) := Function.Injective.mulAction toMultilinearMap toMultilinearMap_injective fun _ _ => rfl end SMul section ContinuousAdd variable [ContinuousAdd M₂] instance : Add (ContinuousMultilinearMap R M₁ M₂) := ⟨fun f f' => ⟨f.toMultilinearMap + f'.toMultilinearMap, f.cont.add f'.cont⟩⟩ @[simp] theorem add_apply (m : ∀ i, M₁ i) : (f + f') m = f m + f' m := rfl @[simp] theorem toMultilinearMap_add (f g : ContinuousMultilinearMap R M₁ M₂) : (f + g).toMultilinearMap = f.toMultilinearMap + g.toMultilinearMap := rfl instance addCommMonoid : AddCommMonoid (ContinuousMultilinearMap R M₁ M₂) := toMultilinearMap_injective.addCommMonoid _ rfl (fun _ _ => rfl) fun _ _ => rfl /-- Evaluation of a `ContinuousMultilinearMap` at a vector as an `AddMonoidHom`. -/ def applyAddHom (m : ∀ i, M₁ i) : ContinuousMultilinearMap R M₁ M₂ →+ M₂ where toFun f := f m map_zero' := rfl map_add' _ _ := rfl @[simp] theorem sum_apply {α : Type*} (f : α → ContinuousMultilinearMap R M₁ M₂) (m : ∀ i, M₁ i) {s : Finset α} : (∑ a ∈ s, f a) m = ∑ a ∈ s, f a m := map_sum (applyAddHom m) f s end ContinuousAdd /-- If `f` is a continuous multilinear map, then `f.toContinuousLinearMap m i` is the continuous linear map obtained by fixing all coordinates but `i` equal to those of `m`, and varying the `i`-th coordinate. -/ @[simps!] def toContinuousLinearMap [DecidableEq ι] (m : ∀ i, M₁ i) (i : ι) : M₁ i →L[R] M₂ := { f.toMultilinearMap.toLinearMap m i with cont := f.cont.comp (continuous_const.update i continuous_id) } /-- The cartesian product of two continuous multilinear maps, as a continuous multilinear map. -/ def prod (f : ContinuousMultilinearMap R M₁ M₂) (g : ContinuousMultilinearMap R M₁ M₃) : ContinuousMultilinearMap R M₁ (M₂ × M₃) := { f.toMultilinearMap.prod g.toMultilinearMap with cont := f.cont.prodMk g.cont } @[simp] theorem prod_apply (f : ContinuousMultilinearMap R M₁ M₂) (g : ContinuousMultilinearMap R M₁ M₃) (m : ∀ i, M₁ i) : (f.prod g) m = (f m, g m) := rfl /-- Combine a family of continuous multilinear maps with the same domain and codomains `M' i` into a continuous multilinear map taking values in the space of functions `∀ i, M' i`. -/ def pi {ι' : Type*} {M' : ι' → Type*} [∀ i, AddCommMonoid (M' i)] [∀ i, TopologicalSpace (M' i)] [∀ i, Module R (M' i)] (f : ∀ i, ContinuousMultilinearMap R M₁ (M' i)) : ContinuousMultilinearMap R M₁ (∀ i, M' i) where cont := continuous_pi fun i => (f i).coe_continuous toMultilinearMap := MultilinearMap.pi fun i => (f i).toMultilinearMap @[simp] theorem coe_pi {ι' : Type*} {M' : ι' → Type*} [∀ i, AddCommMonoid (M' i)] [∀ i, TopologicalSpace (M' i)] [∀ i, Module R (M' i)] (f : ∀ i, ContinuousMultilinearMap R M₁ (M' i)) : ⇑(pi f) = fun m j => f j m := rfl theorem pi_apply {ι' : Type*} {M' : ι' → Type*} [∀ i, AddCommMonoid (M' i)] [∀ i, TopologicalSpace (M' i)] [∀ i, Module R (M' i)] (f : ∀ i, ContinuousMultilinearMap R M₁ (M' i)) (m : ∀ i, M₁ i) (j : ι') : pi f m j = f j m := rfl /-- Restrict the codomain of a continuous multilinear map to a submodule. -/ @[simps! toMultilinearMap apply_coe] def codRestrict (f : ContinuousMultilinearMap R M₁ M₂) (p : Submodule R M₂) (h : ∀ v, f v ∈ p) : ContinuousMultilinearMap R M₁ p := ⟨f.1.codRestrict p h, f.cont.subtype_mk _⟩ section variable (R M₂ M₃) /-- The natural equivalence between continuous linear maps from `M₂` to `M₃` and continuous 1-multilinear maps from `M₂` to `M₃`. -/ @[simps! apply_toMultilinearMap apply_apply symm_apply_apply] def ofSubsingleton [Subsingleton ι] (i : ι) : (M₂ →L[R] M₃) ≃ ContinuousMultilinearMap R (fun _ : ι => M₂) M₃ where toFun f := ⟨MultilinearMap.ofSubsingleton R M₂ M₃ i f, (map_continuous f).comp (continuous_apply i)⟩ invFun f := ⟨(MultilinearMap.ofSubsingleton R M₂ M₃ i).symm f.toMultilinearMap, (map_continuous f).comp <| continuous_pi fun _ ↦ continuous_id⟩ left_inv _ := rfl right_inv f := toMultilinearMap_injective <| (MultilinearMap.ofSubsingleton R M₂ M₃ i).apply_symm_apply f.toMultilinearMap variable (M₁) {M₂} /-- The constant map is multilinear when `ι` is empty. -/ @[simps! toMultilinearMap apply] def constOfIsEmpty [IsEmpty ι] (m : M₂) : ContinuousMultilinearMap R M₁ M₂ where toMultilinearMap := MultilinearMap.constOfIsEmpty R _ m cont := continuous_const end /-- If `g` is continuous multilinear and `f` is a collection of continuous linear maps, then `g (f₁ m₁, ..., fₙ mₙ)` is again a continuous multilinear map, that we call `g.compContinuousLinearMap f`. -/ def compContinuousLinearMap (g : ContinuousMultilinearMap R M₁' M₄) (f : ∀ i : ι, M₁ i →L[R] M₁' i) : ContinuousMultilinearMap R M₁ M₄ := { g.toMultilinearMap.compLinearMap fun i => (f i).toLinearMap with cont := g.cont.comp <| continuous_pi fun j => (f j).cont.comp <| continuous_apply _ } @[simp] theorem compContinuousLinearMap_apply (g : ContinuousMultilinearMap R M₁' M₄) (f : ∀ i : ι, M₁ i →L[R] M₁' i) (m : ∀ i, M₁ i) : g.compContinuousLinearMap f m = g fun i => f i <| m i := rfl /-- Composing a continuous multilinear map with a continuous linear map gives again a continuous multilinear map. -/ def _root_.ContinuousLinearMap.compContinuousMultilinearMap (g : M₂ →L[R] M₃) (f : ContinuousMultilinearMap R M₁ M₂) : ContinuousMultilinearMap R M₁ M₃ := { g.toLinearMap.compMultilinearMap f.toMultilinearMap with cont := g.cont.comp f.cont } @[simp] theorem _root_.ContinuousLinearMap.compContinuousMultilinearMap_coe (g : M₂ →L[R] M₃) (f : ContinuousMultilinearMap R M₁ M₂) : (g.compContinuousMultilinearMap f : (∀ i, M₁ i) → M₃) = (g : M₂ → M₃) ∘ (f : (∀ i, M₁ i) → M₂) := by ext m rfl /-- `ContinuousMultilinearMap.prod` as an `Equiv`. -/ @[simps apply symm_apply_fst symm_apply_snd, simps -isSimp symm_apply] def prodEquiv : (ContinuousMultilinearMap R M₁ M₂ × ContinuousMultilinearMap R M₁ M₃) ≃ ContinuousMultilinearMap R M₁ (M₂ × M₃) where toFun f := f.1.prod f.2 invFun f := ((ContinuousLinearMap.fst _ _ _).compContinuousMultilinearMap f, (ContinuousLinearMap.snd _ _ _).compContinuousMultilinearMap f) left_inv _ := rfl right_inv _ := rfl theorem prod_ext_iff {f g : ContinuousMultilinearMap R M₁ (M₂ × M₃)} : f = g ↔ (ContinuousLinearMap.fst _ _ _).compContinuousMultilinearMap f = (ContinuousLinearMap.fst _ _ _).compContinuousMultilinearMap g ∧ (ContinuousLinearMap.snd _ _ _).compContinuousMultilinearMap f = (ContinuousLinearMap.snd _ _ _).compContinuousMultilinearMap g := by rw [← Prod.mk_inj, ← prodEquiv_symm_apply, ← prodEquiv_symm_apply, Equiv.apply_eq_iff_eq] @[ext] theorem prod_ext {f g : ContinuousMultilinearMap R M₁ (M₂ × M₃)} (h₁ : (ContinuousLinearMap.fst _ _ _).compContinuousMultilinearMap f = (ContinuousLinearMap.fst _ _ _).compContinuousMultilinearMap g) (h₂ : (ContinuousLinearMap.snd _ _ _).compContinuousMultilinearMap f = (ContinuousLinearMap.snd _ _ _).compContinuousMultilinearMap g) : f = g := prod_ext_iff.mpr ⟨h₁, h₂⟩ theorem eq_prod_iff {f : ContinuousMultilinearMap R M₁ (M₂ × M₃)} {g : ContinuousMultilinearMap R M₁ M₂} {h : ContinuousMultilinearMap R M₁ M₃} : f = g.prod h ↔ (ContinuousLinearMap.fst _ _ _).compContinuousMultilinearMap f = g ∧ (ContinuousLinearMap.snd _ _ _).compContinuousMultilinearMap f = h := prod_ext_iff theorem add_prod_add [ContinuousAdd M₂] [ContinuousAdd M₃] (f₁ f₂ : ContinuousMultilinearMap R M₁ M₂) (g₁ g₂ : ContinuousMultilinearMap R M₁ M₃) : (f₁ + f₂).prod (g₁ + g₂) = f₁.prod g₁ + f₂.prod g₂ := rfl theorem smul_prod_smul {S : Type*} [Monoid S] [DistribMulAction S M₂] [DistribMulAction S M₃] [ContinuousConstSMul S M₂] [SMulCommClass R S M₂] [ContinuousConstSMul S M₃] [SMulCommClass R S M₃] (c : S) (f : ContinuousMultilinearMap R M₁ M₂) (g : ContinuousMultilinearMap R M₁ M₃) : (c • f).prod (c • g) = c • f.prod g := rfl @[simp] theorem zero_prod_zero : (0 : ContinuousMultilinearMap R M₁ M₂).prod (0 : ContinuousMultilinearMap R M₁ M₃) = 0 := rfl /-- `ContinuousMultilinearMap.pi` as an `Equiv`. -/ @[simps] def piEquiv {ι' : Type*} {M' : ι' → Type*} [∀ i, AddCommMonoid (M' i)] [∀ i, TopologicalSpace (M' i)] [∀ i, Module R (M' i)] : (∀ i, ContinuousMultilinearMap R M₁ (M' i)) ≃ ContinuousMultilinearMap R M₁ (∀ i, M' i) where toFun := ContinuousMultilinearMap.pi invFun f i := (ContinuousLinearMap.proj i : _ →L[R] M' i).compContinuousMultilinearMap f left_inv _ := rfl right_inv _ := rfl /-- An equivalence of the index set defines an equivalence between the spaces of continuous multilinear maps. This is the forward map of this equivalence. -/ @[simps! toMultilinearMap apply] nonrec def domDomCongr {ι' : Type*} (e : ι ≃ ι') (f : ContinuousMultilinearMap R (fun _ : ι => M₂) M₃) : ContinuousMultilinearMap R (fun _ : ι' => M₂) M₃ where toMultilinearMap := f.domDomCongr e cont := f.cont.comp <| continuous_pi fun _ => continuous_apply _ /-- An equivalence of the index set defines an equivalence between the spaces of continuous multilinear maps. In case of normed spaces, this is a linear isometric equivalence, see `ContinuousMultilinearMap.domDomCongrₗᵢ`. -/ @[simps] def domDomCongrEquiv {ι' : Type*} (e : ι ≃ ι') : ContinuousMultilinearMap R (fun _ : ι => M₂) M₃ ≃ ContinuousMultilinearMap R (fun _ : ι' => M₂) M₃ where toFun := domDomCongr e invFun := domDomCongr e.symm left_inv _ := ext fun _ => by simp right_inv _ := ext fun _ => by simp section linearDeriv variable [ContinuousAdd M₂] [DecidableEq ι] [Fintype ι] (x y : ∀ i, M₁ i) /-- The derivative of a continuous multilinear map, as a continuous linear map from `∀ i, M₁ i` to `M₂`; see `ContinuousMultilinearMap.hasFDerivAt`. -/ def linearDeriv : (∀ i, M₁ i) →L[R] M₂ := ∑ i : ι, (f.toContinuousLinearMap x i).comp (.proj i) @[simp] lemma linearDeriv_apply : f.linearDeriv x y = ∑ i, f (Function.update x i (y i)) := by unfold linearDeriv toContinuousLinearMap simp only [ContinuousLinearMap.coe_sum', ContinuousLinearMap.coe_comp', ContinuousLinearMap.coe_mk', LinearMap.coe_mk, LinearMap.coe_toAddHom, Finset.sum_apply] rfl end linearDeriv /-- In the specific case of continuous multilinear maps on spaces indexed by `Fin (n+1)`, where one can build an element of `(i : Fin (n+1)) → M i` using `cons`, one can express directly the additivity of a multilinear map along the first variable. -/ theorem cons_add (f : ContinuousMultilinearMap R M M₂) (m : ∀ i : Fin n, M i.succ) (x y : M 0) : f (cons (x + y) m) = f (cons x m) + f (cons y m) := f.toMultilinearMap.cons_add m x y /-- In the specific case of continuous multilinear maps on spaces indexed by `Fin (n+1)`, where one can build an element of `(i : Fin (n+1)) → M i` using `cons`, one can express directly the multiplicativity of a multilinear map along the first variable. -/ theorem cons_smul (f : ContinuousMultilinearMap R M M₂) (m : ∀ i : Fin n, M i.succ) (c : R) (x : M 0) : f (cons (c • x) m) = c • f (cons x m) := f.toMultilinearMap.cons_smul m c x theorem map_piecewise_add [DecidableEq ι] (m m' : ∀ i, M₁ i) (t : Finset ι) : f (t.piecewise (m + m') m') = ∑ s ∈ t.powerset, f (s.piecewise m m') := f.toMultilinearMap.map_piecewise_add _ _ _ /-- Additivity of a continuous multilinear map along all coordinates at the same time, writing `f (m + m')` as the sum of `f (s.piecewise m m')` over all sets `s`. -/ theorem map_add_univ [DecidableEq ι] [Fintype ι] (m m' : ∀ i, M₁ i) : f (m + m') = ∑ s : Finset ι, f (s.piecewise m m') := f.toMultilinearMap.map_add_univ _ _ section ApplySum open Fintype Finset variable {α : ι → Type*} [Fintype ι] (g : ∀ i, α i → M₁ i) (A : ∀ i, Finset (α i)) /-- If `f` is continuous multilinear, then `f (Σ_{j₁ ∈ A₁} g₁ j₁, ..., Σ_{jₙ ∈ Aₙ} gₙ jₙ)` is the sum of `f (g₁ (r 1), ..., gₙ (r n))` where `r` ranges over all functions with `r 1 ∈ A₁`, ..., `r n ∈ Aₙ`. This follows from multilinearity by expanding successively with respect to each coordinate. -/ theorem map_sum_finset [DecidableEq ι] : (f fun i => ∑ j ∈ A i, g i j) = ∑ r ∈ piFinset A, f fun i => g i (r i) := f.toMultilinearMap.map_sum_finset _ _ /-- If `f` is continuous multilinear, then `f (Σ_{j₁} g₁ j₁, ..., Σ_{jₙ} gₙ jₙ)` is the sum of `f (g₁ (r 1), ..., gₙ (r n))` where `r` ranges over all functions `r`. This follows from multilinearity by expanding successively with respect to each coordinate. -/ theorem map_sum [DecidableEq ι] [∀ i, Fintype (α i)] : (f fun i => ∑ j, g i j) = ∑ r : ∀ i, α i, f fun i => g i (r i) := f.toMultilinearMap.map_sum _ end ApplySum section RestrictScalar variable (R) variable {A : Type*} [Semiring A] [SMul R A] [∀ i : ι, Module A (M₁ i)] [Module A M₂] [∀ i, IsScalarTower R A (M₁ i)] [IsScalarTower R A M₂] /-- Reinterpret an `A`-multilinear map as an `R`-multilinear map, if `A` is an algebra over `R` and their actions on all involved modules agree with the action of `R` on `A`. -/ def restrictScalars (f : ContinuousMultilinearMap A M₁ M₂) : ContinuousMultilinearMap R M₁ M₂ where toMultilinearMap := f.toMultilinearMap.restrictScalars R cont := f.cont @[simp] theorem coe_restrictScalars (f : ContinuousMultilinearMap A M₁ M₂) : ⇑(f.restrictScalars R) = f := rfl end RestrictScalar end Semiring section Ring variable [Ring R] [∀ i, AddCommGroup (M₁ i)] [AddCommGroup M₂] [∀ i, Module R (M₁ i)] [Module R M₂] [∀ i, TopologicalSpace (M₁ i)] [TopologicalSpace M₂] (f f' : ContinuousMultilinearMap R M₁ M₂) @[simp] theorem map_update_sub [DecidableEq ι] (m : ∀ i, M₁ i) (i : ι) (x y : M₁ i) : f (update m i (x - y)) = f (update m i x) - f (update m i y) := f.toMultilinearMap.map_update_sub _ _ _ _ @[deprecated (since := "2024-11-03")] protected alias map_sub := ContinuousMultilinearMap.map_update_sub section IsTopologicalAddGroup variable [IsTopologicalAddGroup M₂] instance : Neg (ContinuousMultilinearMap R M₁ M₂) := ⟨fun f => { -f.toMultilinearMap with cont := f.cont.neg }⟩ @[simp] theorem neg_apply (m : ∀ i, M₁ i) : (-f) m = -f m := rfl instance : Sub (ContinuousMultilinearMap R M₁ M₂) := ⟨fun f g => { f.toMultilinearMap - g.toMultilinearMap with cont := f.cont.sub g.cont }⟩ @[simp] theorem sub_apply (m : ∀ i, M₁ i) : (f - f') m = f m - f' m := rfl instance : AddCommGroup (ContinuousMultilinearMap R M₁ M₂) := toMultilinearMap_injective.addCommGroup _ rfl (fun _ _ => rfl) (fun _ => rfl) (fun _ _ => rfl) (fun _ _ => rfl) fun _ _ => rfl theorem neg_prod_neg [AddCommGroup M₃] [Module R M₃] [TopologicalSpace M₃] [IsTopologicalAddGroup M₃] (f : ContinuousMultilinearMap R M₁ M₂) (g : ContinuousMultilinearMap R M₁ M₃) : (-f).prod (-g) = - f.prod g := rfl theorem sub_prod_sub [AddCommGroup M₃] [Module R M₃] [TopologicalSpace M₃] [IsTopologicalAddGroup M₃] (f₁ f₂ : ContinuousMultilinearMap R M₁ M₂) (g₁ g₂ : ContinuousMultilinearMap R M₁ M₃) : (f₁ - f₂).prod (g₁ - g₂) = f₁.prod g₁ - f₂.prod g₂ := rfl end IsTopologicalAddGroup end Ring section CommSemiring variable [CommSemiring R] [∀ i, AddCommMonoid (M₁ i)] [AddCommMonoid M₂] [∀ i, Module R (M₁ i)] [Module R M₂] [∀ i, TopologicalSpace (M₁ i)] [TopologicalSpace M₂] (f : ContinuousMultilinearMap R M₁ M₂) theorem map_piecewise_smul [DecidableEq ι] (c : ι → R) (m : ∀ i, M₁ i) (s : Finset ι) : f (s.piecewise (fun i => c i • m i) m) = (∏ i ∈ s, c i) • f m := f.toMultilinearMap.map_piecewise_smul _ _ _ /-- Multiplicativity of a continuous multilinear map along all coordinates at the same time, writing `f (fun i ↦ c i • m i)` as `(∏ i, c i) • f m`. -/ theorem map_smul_univ [Fintype ι] (c : ι → R) (m : ∀ i, M₁ i) : (f fun i => c i • m i) = (∏ i, c i) • f m := f.toMultilinearMap.map_smul_univ _ _ end CommSemiring section DistribMulAction variable {R' R'' A : Type*} [Monoid R'] [Monoid R''] [Semiring A] [∀ i, AddCommMonoid (M₁ i)] [AddCommMonoid M₂] [∀ i, TopologicalSpace (M₁ i)] [TopologicalSpace M₂] [∀ i, Module A (M₁ i)] [Module A M₂] [DistribMulAction R' M₂] [ContinuousConstSMul R' M₂] [SMulCommClass A R' M₂] [DistribMulAction R'' M₂] [ContinuousConstSMul R'' M₂] [SMulCommClass A R'' M₂] instance [ContinuousAdd M₂] : DistribMulAction R' (ContinuousMultilinearMap A M₁ M₂) := Function.Injective.distribMulAction { toFun := toMultilinearMap, map_zero' := toMultilinearMap_zero, map_add' := toMultilinearMap_add } toMultilinearMap_injective fun _ _ => rfl end DistribMulAction section Module variable {R' A : Type*} [Semiring R'] [Semiring A] [∀ i, AddCommMonoid (M₁ i)] [AddCommMonoid M₂] [∀ i, TopologicalSpace (M₁ i)] [TopologicalSpace M₂] [ContinuousAdd M₂] [∀ i, Module A (M₁ i)] [Module A M₂] [Module R' M₂] [ContinuousConstSMul R' M₂] [SMulCommClass A R' M₂] /-- The space of continuous multilinear maps over an algebra over `R` is a module over `R`, for the pointwise addition and scalar multiplication. -/ instance : Module R' (ContinuousMultilinearMap A M₁ M₂) := Function.Injective.module _ { toFun := toMultilinearMap, map_zero' := toMultilinearMap_zero, map_add' := toMultilinearMap_add } toMultilinearMap_injective fun _ _ => rfl /-- Linear map version of the map `toMultilinearMap` associating to a continuous multilinear map the corresponding multilinear map. -/ @[simps] def toMultilinearMapLinear : ContinuousMultilinearMap A M₁ M₂ →ₗ[R'] MultilinearMap A M₁ M₂ where toFun := toMultilinearMap map_add' := toMultilinearMap_add map_smul' := toMultilinearMap_smul /-- `ContinuousMultilinearMap.pi` as a `LinearEquiv`. -/ @[simps +simpRhs] def piLinearEquiv {ι' : Type*} {M' : ι' → Type*} [∀ i, AddCommMonoid (M' i)] [∀ i, TopologicalSpace (M' i)] [∀ i, ContinuousAdd (M' i)] [∀ i, Module R' (M' i)] [∀ i, Module A (M' i)] [∀ i, SMulCommClass A R' (M' i)] [∀ i, ContinuousConstSMul R' (M' i)] : (∀ i, ContinuousMultilinearMap A M₁ (M' i)) ≃ₗ[R'] ContinuousMultilinearMap A M₁ (∀ i, M' i) := { piEquiv with map_add' := fun _ _ => rfl map_smul' := fun _ _ => rfl } end Module section CommAlgebra variable (R ι) (A : Type*) [Fintype ι] [CommSemiring R] [CommSemiring A] [Algebra R A] [TopologicalSpace A] [ContinuousMul A] /-- The continuous multilinear map on `A^ι`, where `A` is a normed commutative algebra over `𝕜`, associating to `m` the product of all the `m i`. See also `ContinuousMultilinearMap.mkPiAlgebraFin`. -/ protected def mkPiAlgebra : ContinuousMultilinearMap R (fun _ : ι => A) A where cont := continuous_finset_prod _ fun _ _ => continuous_apply _ toMultilinearMap := MultilinearMap.mkPiAlgebra R ι A @[simp] theorem mkPiAlgebra_apply (m : ι → A) : ContinuousMultilinearMap.mkPiAlgebra R ι A m = ∏ i, m i := rfl end CommAlgebra section Algebra variable (R n) (A : Type*) [CommSemiring R] [Semiring A] [Algebra R A] [TopologicalSpace A] [ContinuousMul A] /-- The continuous multilinear map on `A^n`, where `A` is a normed algebra over `𝕜`, associating to `m` the product of all the `m i`. See also: `ContinuousMultilinearMap.mkPiAlgebra`. -/ protected def mkPiAlgebraFin : A[×n]→L[R] A where cont := by change Continuous fun m => (List.ofFn m).prod simp_rw [List.ofFn_eq_map] exact continuous_list_prod _ fun i _ => continuous_apply _ toMultilinearMap := MultilinearMap.mkPiAlgebraFin R n A variable {R n A} @[simp] theorem mkPiAlgebraFin_apply (m : Fin n → A) : ContinuousMultilinearMap.mkPiAlgebraFin R n A m = (List.ofFn m).prod := rfl end Algebra section SMulRight variable [CommSemiring R] [∀ i, AddCommMonoid (M₁ i)] [AddCommMonoid M₂] [∀ i, Module R (M₁ i)] [Module R M₂] [TopologicalSpace R] [∀ i, TopologicalSpace (M₁ i)] [TopologicalSpace M₂] [ContinuousSMul R M₂] (f : ContinuousMultilinearMap R M₁ R) (z : M₂) /-- Given a continuous `R`-multilinear map `f` taking values in `R`, `f.smulRight z` is the continuous multilinear map sending `m` to `f m • z`. -/ @[simps! toMultilinearMap apply] def smulRight : ContinuousMultilinearMap R M₁ M₂ where toMultilinearMap := f.toMultilinearMap.smulRight z cont := f.cont.smul continuous_const end SMulRight section CommRing variable {M : Type*} variable [Fintype ι] [CommRing R] [AddCommMonoid M] [Module R M] variable [TopologicalSpace R] [TopologicalSpace M] variable [ContinuousMul R] [ContinuousSMul R M] variable (R ι) in /-- The canonical continuous multilinear map on `R^ι`, associating to `m` the product of all the `m i` (multiplied by a fixed reference element `z` in the target module) -/ protected def mkPiRing (z : M) : ContinuousMultilinearMap R (fun _ : ι => R) M := (ContinuousMultilinearMap.mkPiAlgebra R ι R).smulRight z @[simp] theorem mkPiRing_apply (z : M) (m : ι → R) : (ContinuousMultilinearMap.mkPiRing R ι z : (ι → R) → M) m = (∏ i, m i) • z := rfl theorem mkPiRing_apply_one_eq_self (f : ContinuousMultilinearMap R (fun _ : ι => R) M) : ContinuousMultilinearMap.mkPiRing R ι (f fun _ => 1) = f := toMultilinearMap_injective f.toMultilinearMap.mkPiRing_apply_one_eq_self theorem mkPiRing_eq_iff {z₁ z₂ : M} : ContinuousMultilinearMap.mkPiRing R ι z₁ = ContinuousMultilinearMap.mkPiRing R ι z₂ ↔ z₁ = z₂ := by rw [← toMultilinearMap_injective.eq_iff] exact MultilinearMap.mkPiRing_eq_iff theorem mkPiRing_zero : ContinuousMultilinearMap.mkPiRing R ι (0 : M) = 0 := by ext; rw [mkPiRing_apply, smul_zero, ContinuousMultilinearMap.zero_apply] theorem mkPiRing_eq_zero_iff (z : M) : ContinuousMultilinearMap.mkPiRing R ι z = 0 ↔ z = 0 := by rw [← mkPiRing_zero, mkPiRing_eq_iff] end CommRing end ContinuousMultilinearMap
Mathlib/Topology/Algebra/Module/Multilinear/Basic.lean
687
688
/- Copyright (c) 2023 Rémi Bottinelli. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Rémi Bottinelli -/ import Mathlib.Data.Set.Function import Mathlib.Analysis.RCLike.Basic import Mathlib.Topology.EMetricSpace.BoundedVariation /-! # Constant speed This file defines the notion of constant (and unit) speed for a function `f : ℝ → E` with pseudo-emetric structure on `E` with respect to a set `s : Set ℝ` and "speed" `l : ℝ≥0`, and shows that if `f` has locally bounded variation on `s`, it can be obtained (up to distance zero, on `s`), as a composite `φ ∘ (variationOnFromTo f s a)`, where `φ` has unit speed and `a ∈ s`. ## Main definitions * `HasConstantSpeedOnWith f s l`, stating that the speed of `f` on `s` is `l`. * `HasUnitSpeedOn f s`, stating that the speed of `f` on `s` is `1`. * `naturalParameterization f s a : ℝ → E`, the unit speed reparameterization of `f` on `s` relative to `a`. ## Main statements * `unique_unit_speed_on_Icc_zero` proves that if `f` and `f ∘ φ` are both naturally parameterized on closed intervals starting at `0`, then `φ` must be the identity on those intervals. * `edist_naturalParameterization_eq_zero` proves that if `f` has locally bounded variation, then precomposing `naturalParameterization f s a` with `variationOnFromTo f s a` yields a function at distance zero from `f` on `s`. * `has_unit_speed_naturalParameterization` proves that if `f` has locally bounded variation, then `naturalParameterization f s a` has unit speed on `s`. ## Tags arc-length, parameterization -/ open scoped NNReal ENNReal open Set variable {α : Type*} [LinearOrder α] {E : Type*} [PseudoEMetricSpace E] variable (f : ℝ → E) (s : Set ℝ) (l : ℝ≥0) /-- `f` has constant speed `l` on `s` if the variation of `f` on `s ∩ Icc x y` is equal to `l * (y - x)` for any `x y` in `s`. -/ def HasConstantSpeedOnWith := ∀ ⦃x⦄ (_ : x ∈ s) ⦃y⦄ (_ : y ∈ s), eVariationOn f (s ∩ Icc x y) = ENNReal.ofReal (l * (y - x)) variable {f s l} theorem HasConstantSpeedOnWith.hasLocallyBoundedVariationOn (h : HasConstantSpeedOnWith f s l) : LocallyBoundedVariationOn f s := fun x y hx hy => by
simp only [BoundedVariationOn, h hx hy, Ne, ENNReal.ofReal_ne_top, not_false_iff] theorem hasConstantSpeedOnWith_of_subsingleton (f : ℝ → E) {s : Set ℝ} (hs : s.Subsingleton)
Mathlib/Analysis/ConstantSpeed.lean
59
61
/- Copyright (c) 2020 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta, Yaël Dillies -/ import Mathlib.Data.Set.BooleanAlgebra import Mathlib.Data.SetLike.Basic import Mathlib.Order.Hom.Basic /-! # Closure operators between preorders We define (bundled) closure operators on a preorder as monotone (increasing), extensive (inflationary) and idempotent functions. We define closed elements for the operator as elements which are fixed by it. Lower adjoints to a function between preorders `u : β → α` allow to generalise closure operators to situations where the closure operator we are dealing with naturally decomposes as `u ∘ l` where `l` is a worthy function to have on its own. Typical examples include `l : Set G → Subgroup G := Subgroup.closure`, `u : Subgroup G → Set G := (↑)`, where `G` is a group. This shows there is a close connection between closure operators, lower adjoints and Galois connections/insertions: every Galois connection induces a lower adjoint which itself induces a closure operator by composition (see `GaloisConnection.lowerAdjoint` and `LowerAdjoint.closureOperator`), and every closure operator on a partial order induces a Galois insertion from the set of closed elements to the underlying type (see `ClosureOperator.gi`). ## Main definitions * `ClosureOperator`: A closure operator is a monotone function `f : α → α` such that `∀ x, x ≤ f x` and `∀ x, f (f x) = f x`. * `LowerAdjoint`: A lower adjoint to `u : β → α` is a function `l : α → β` such that `l` and `u` form a Galois connection. ## Implementation details Although `LowerAdjoint` is technically a generalisation of `ClosureOperator` (by defining `toFun := id`), it is desirable to have both as otherwise `id`s would be carried all over the place when using concrete closure operators such as `ConvexHull`. `LowerAdjoint` really is a semibundled `structure` version of `GaloisConnection`. ## References * https://en.wikipedia.org/wiki/Closure_operator#Closure_operators_on_partially_ordered_sets -/ open Set /-! ### Closure operator -/ variable (α : Type*) {ι : Sort*} {κ : ι → Sort*} /-- A closure operator on the preorder `α` is a monotone function which is extensive (every `x` is less than its closure) and idempotent. -/ structure ClosureOperator [Preorder α] extends α →o α where /-- An element is less than or equal its closure -/ le_closure' : ∀ x, x ≤ toFun x /-- Closures are idempotent -/ idempotent' : ∀ x, toFun (toFun x) = toFun x /-- Predicate for an element to be closed. By default, this is defined as `c.IsClosed x := (c x = x)` (see `isClosed_iff`). We allow an override to fix definitional equalities. -/ IsClosed (x : α) : Prop := toFun x = x isClosed_iff {x : α} : IsClosed x ↔ toFun x = x := by aesop namespace ClosureOperator instance [Preorder α] : FunLike (ClosureOperator α) α α where coe c := c.1 coe_injective' := by rintro ⟨⟩ ⟨⟩ h; obtain rfl := DFunLike.ext' h; congr with x; simp_all instance [Preorder α] : OrderHomClass (ClosureOperator α) α α where map_rel f _ _ h := f.mono h initialize_simps_projections ClosureOperator (toFun → apply, IsClosed → isClosed) /-- If `c` is a closure operator on `α` and `e` an order-isomorphism between `α` and `β` then `e ∘ c ∘ e⁻¹` is a closure operator on `β`. -/ @[simps apply] def conjBy {α β} [Preorder α] [Preorder β] (c : ClosureOperator α) (e : α ≃o β) : ClosureOperator β where toFun := e.conj c IsClosed b := c.IsClosed (e.symm b) monotone' _ _ h := (map_le_map_iff e).mpr <| c.monotone <| (map_le_map_iff e.symm).mpr h le_closure' _ := e.symm_apply_le.mp (c.le_closure' _) idempotent' _ := congrArg e <| Eq.trans (congrArg c (e.symm_apply_apply _)) (c.idempotent' _) isClosed_iff := Iff.trans c.isClosed_iff e.eq_symm_apply lemma conjBy_refl {α} [Preorder α] (c : ClosureOperator α) : c.conjBy (OrderIso.refl α) = c := rfl lemma conjBy_trans {α β γ} [Preorder α] [Preorder β] [Preorder γ] (e₁ : α ≃o β) (e₂ : β ≃o γ) (c : ClosureOperator α) : c.conjBy (e₁.trans e₂) = (c.conjBy e₁).conjBy e₂ := rfl section PartialOrder variable [PartialOrder α] /-- The identity function as a closure operator. -/ @[simps!] def id : ClosureOperator α where toOrderHom := OrderHom.id le_closure' _ := le_rfl idempotent' _ := rfl IsClosed _ := True instance : Inhabited (ClosureOperator α) := ⟨id α⟩ variable {α} variable (c : ClosureOperator α) @[ext] theorem ext : ∀ c₁ c₂ : ClosureOperator α, (∀ x, c₁ x = c₂ x) → c₁ = c₂ := DFunLike.ext /-- Constructor for a closure operator using the weaker idempotency axiom: `f (f x) ≤ f x`. -/ @[simps] def mk' (f : α → α) (hf₁ : Monotone f) (hf₂ : ∀ x, x ≤ f x) (hf₃ : ∀ x, f (f x) ≤ f x) : ClosureOperator α where toFun := f monotone' := hf₁ le_closure' := hf₂ idempotent' x := (hf₃ x).antisymm (hf₁ (hf₂ x)) /-- Convenience constructor for a closure operator using the weaker minimality axiom: `x ≤ f y → f x ≤ f y`, which is sometimes easier to prove in practice. -/ @[simps] def mk₂ (f : α → α) (hf : ∀ x, x ≤ f x) (hmin : ∀ ⦃x y⦄, x ≤ f y → f x ≤ f y) : ClosureOperator α where toFun := f monotone' _ y hxy := hmin (hxy.trans (hf y)) le_closure' := hf idempotent' _ := (hmin le_rfl).antisymm (hf _) /-- Construct a closure operator from an inflationary function `f` and a "closedness" predicate `p` witnessing minimality of `f x` among closed elements greater than `x`. -/ @[simps!] def ofPred (f : α → α) (p : α → Prop) (hf : ∀ x, x ≤ f x) (hfp : ∀ x, p (f x)) (hmin : ∀ ⦃x y⦄, x ≤ y → p y → f x ≤ y) : ClosureOperator α where __ := mk₂ f hf fun _ y hxy => hmin hxy (hfp y) IsClosed := p isClosed_iff := ⟨fun hx ↦ (hmin le_rfl hx).antisymm <| hf _, fun hx ↦ hx ▸ hfp _⟩ @[mono] theorem monotone : Monotone c := c.monotone' /-- Every element is less than its closure. This property is sometimes referred to as extensivity or inflationarity. -/ theorem le_closure (x : α) : x ≤ c x := c.le_closure' x @[simp] theorem idempotent (x : α) : c (c x) = c x := c.idempotent' x @[simp] lemma isClosed_closure (x : α) : c.IsClosed (c x) := c.isClosed_iff.2 <| c.idempotent x /-- The type of elements closed under a closure operator. -/ abbrev Closeds := {x // c.IsClosed x} /-- Send an element to a closed element (by taking the closure). -/ def toCloseds (x : α) : c.Closeds := ⟨c x, c.isClosed_closure x⟩ variable {c} {x y : α} theorem IsClosed.closure_eq : c.IsClosed x → c x = x := c.isClosed_iff.1 theorem isClosed_iff_closure_le : c.IsClosed x ↔ c x ≤ x := ⟨fun h ↦ h.closure_eq.le, fun h ↦ c.isClosed_iff.2 <| h.antisymm <| c.le_closure x⟩ /-- The set of closed elements for `c` is exactly its range. -/ theorem setOf_isClosed_eq_range_closure : {x | c.IsClosed x} = Set.range c := by ext x; exact ⟨fun hx ↦ ⟨x, hx.closure_eq⟩, by rintro ⟨y, rfl⟩; exact c.isClosed_closure _⟩ theorem le_closure_iff : x ≤ c y ↔ c x ≤ c y := ⟨fun h ↦ c.idempotent y ▸ c.monotone h, (c.le_closure x).trans⟩ @[simp] theorem IsClosed.closure_le_iff (hy : c.IsClosed y) : c x ≤ y ↔ x ≤ y := by rw [← hy.closure_eq, ← le_closure_iff] lemma closure_min (hxy : x ≤ y) (hy : c.IsClosed y) : c x ≤ y := hy.closure_le_iff.2 hxy lemma closure_isGLB (x : α) : IsGLB { y | x ≤ y ∧ c.IsClosed y } (c x) where left _ := and_imp.mpr closure_min right _ h := h ⟨c.le_closure x, c.isClosed_closure x⟩ theorem ext_isClosed (c₁ c₂ : ClosureOperator α) (h : ∀ x, c₁.IsClosed x ↔ c₂.IsClosed x) : c₁ = c₂ := ext c₁ c₂ <| fun x => IsGLB.unique (c₁.closure_isGLB x) <| (Set.ext (and_congr_right' <| h ·)).substr (c₂.closure_isGLB x) /-- A closure operator is equal to the closure operator obtained by feeding `c.closed` into the `ofPred` constructor. -/ theorem eq_ofPred_closed (c : ClosureOperator α) : c = ofPred c c.IsClosed c.le_closure c.isClosed_closure fun _ _ ↦ closure_min := by ext rfl end PartialOrder variable {α} section OrderTop variable [PartialOrder α] [OrderTop α] (c : ClosureOperator α) @[simp] theorem closure_top : c ⊤ = ⊤ := le_top.antisymm (c.le_closure _) @[simp] lemma isClosed_top : c.IsClosed ⊤ := c.isClosed_iff.2 c.closure_top end OrderTop theorem closure_inf_le [SemilatticeInf α] (c : ClosureOperator α) (x y : α) : c (x ⊓ y) ≤ c x ⊓ c y := c.monotone.map_inf_le _ _ section SemilatticeSup
variable [SemilatticeSup α] (c : ClosureOperator α) theorem closure_sup_closure_le (x y : α) : c x ⊔ c y ≤ c (x ⊔ y) := c.monotone.le_map_sup _ _
Mathlib/Order/Closure.lean
230
233
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Johan Commelin, Mario Carneiro -/ import Mathlib.Algebra.Algebra.Subalgebra.Lattice import Mathlib.Algebra.Algebra.Tower import Mathlib.Algebra.GroupWithZero.Divisibility import Mathlib.Algebra.MonoidAlgebra.Basic import Mathlib.Algebra.MonoidAlgebra.Support import Mathlib.Algebra.Regular.Pow import Mathlib.Data.Finsupp.Antidiagonal import Mathlib.Order.SymmDiff /-! # Multivariate polynomials This file defines polynomial rings over a base ring (or even semiring), with variables from a general type `σ` (which could be infinite). ## Important definitions Let `R` be a commutative ring (or a semiring) and let `σ` be an arbitrary type. This file creates the type `MvPolynomial σ R`, which mathematicians might denote $R[X_i : i \in σ]$. It is the type of multivariate (a.k.a. multivariable) polynomials, with variables corresponding to the terms in `σ`, and coefficients in `R`. ### Notation In the definitions below, we use the following notation: + `σ : Type*` (indexing the variables) + `R : Type*` `[CommSemiring R]` (the coefficients) + `s : σ →₀ ℕ`, a function from `σ` to `ℕ` which is zero away from a finite set. This will give rise to a monomial in `MvPolynomial σ R` which mathematicians might call `X^s` + `a : R` + `i : σ`, with corresponding monomial `X i`, often denoted `X_i` by mathematicians + `p : MvPolynomial σ R` ### Definitions * `MvPolynomial σ R` : the type of polynomials with variables of type `σ` and coefficients in the commutative semiring `R` * `monomial s a` : the monomial which mathematically would be denoted `a * X^s` * `C a` : the constant polynomial with value `a` * `X i` : the degree one monomial corresponding to i; mathematically this might be denoted `Xᵢ`. * `coeff s p` : the coefficient of `s` in `p`. ## Implementation notes Recall that if `Y` has a zero, then `X →₀ Y` is the type of functions from `X` to `Y` with finite support, i.e. such that only finitely many elements of `X` get sent to non-zero terms in `Y`. The definition of `MvPolynomial σ R` is `(σ →₀ ℕ) →₀ R`; here `σ →₀ ℕ` denotes the space of all monomials in the variables, and the function to `R` sends a monomial to its coefficient in the polynomial being represented. ## Tags polynomial, multivariate polynomial, multivariable polynomial -/ noncomputable section open Set Function Finsupp AddMonoidAlgebra open scoped Pointwise universe u v w x variable {R : Type u} {S₁ : Type v} {S₂ : Type w} {S₃ : Type x} /-- Multivariate polynomial, where `σ` is the index set of the variables and `R` is the coefficient ring -/ def MvPolynomial (σ : Type*) (R : Type*) [CommSemiring R] := AddMonoidAlgebra R (σ →₀ ℕ) namespace MvPolynomial -- Porting note: because of `MvPolynomial.C` and `MvPolynomial.X` this linter throws -- tons of warnings in this file, and it's easier to just disable them globally in the file variable {σ : Type*} {a a' a₁ a₂ : R} {e : ℕ} {n m : σ} {s : σ →₀ ℕ} section CommSemiring section Instances instance decidableEqMvPolynomial [CommSemiring R] [DecidableEq σ] [DecidableEq R] : DecidableEq (MvPolynomial σ R) := Finsupp.instDecidableEq instance commSemiring [CommSemiring R] : CommSemiring (MvPolynomial σ R) := AddMonoidAlgebra.commSemiring instance inhabited [CommSemiring R] : Inhabited (MvPolynomial σ R) := ⟨0⟩ instance distribuMulAction [Monoid R] [CommSemiring S₁] [DistribMulAction R S₁] : DistribMulAction R (MvPolynomial σ S₁) := AddMonoidAlgebra.distribMulAction instance smulZeroClass [CommSemiring S₁] [SMulZeroClass R S₁] : SMulZeroClass R (MvPolynomial σ S₁) := AddMonoidAlgebra.smulZeroClass instance faithfulSMul [CommSemiring S₁] [SMulZeroClass R S₁] [FaithfulSMul R S₁] : FaithfulSMul R (MvPolynomial σ S₁) := AddMonoidAlgebra.faithfulSMul instance module [Semiring R] [CommSemiring S₁] [Module R S₁] : Module R (MvPolynomial σ S₁) := AddMonoidAlgebra.module instance isScalarTower [CommSemiring S₂] [SMul R S₁] [SMulZeroClass R S₂] [SMulZeroClass S₁ S₂] [IsScalarTower R S₁ S₂] : IsScalarTower R S₁ (MvPolynomial σ S₂) := AddMonoidAlgebra.isScalarTower instance smulCommClass [CommSemiring S₂] [SMulZeroClass R S₂] [SMulZeroClass S₁ S₂] [SMulCommClass R S₁ S₂] : SMulCommClass R S₁ (MvPolynomial σ S₂) := AddMonoidAlgebra.smulCommClass instance isCentralScalar [CommSemiring S₁] [SMulZeroClass R S₁] [SMulZeroClass Rᵐᵒᵖ S₁] [IsCentralScalar R S₁] : IsCentralScalar R (MvPolynomial σ S₁) := AddMonoidAlgebra.isCentralScalar instance algebra [CommSemiring R] [CommSemiring S₁] [Algebra R S₁] : Algebra R (MvPolynomial σ S₁) := AddMonoidAlgebra.algebra instance isScalarTower_right [CommSemiring S₁] [DistribSMul R S₁] [IsScalarTower R S₁ S₁] : IsScalarTower R (MvPolynomial σ S₁) (MvPolynomial σ S₁) := AddMonoidAlgebra.isScalarTower_self _ instance smulCommClass_right [CommSemiring S₁] [DistribSMul R S₁] [SMulCommClass R S₁ S₁] : SMulCommClass R (MvPolynomial σ S₁) (MvPolynomial σ S₁) := AddMonoidAlgebra.smulCommClass_self _ /-- If `R` is a subsingleton, then `MvPolynomial σ R` has a unique element -/ instance unique [CommSemiring R] [Subsingleton R] : Unique (MvPolynomial σ R) := AddMonoidAlgebra.unique end Instances variable [CommSemiring R] [CommSemiring S₁] {p q : MvPolynomial σ R} /-- `monomial s a` is the monomial with coefficient `a` and exponents given by `s` -/ def monomial (s : σ →₀ ℕ) : R →ₗ[R] MvPolynomial σ R := AddMonoidAlgebra.lsingle s theorem one_def : (1 : MvPolynomial σ R) = monomial 0 1 := rfl theorem single_eq_monomial (s : σ →₀ ℕ) (a : R) : Finsupp.single s a = monomial s a := rfl theorem mul_def : p * q = p.sum fun m a => q.sum fun n b => monomial (m + n) (a * b) := AddMonoidAlgebra.mul_def /-- `C a` is the constant polynomial with value `a` -/ def C : R →+* MvPolynomial σ R := { singleZeroRingHom with toFun := monomial 0 } variable (R σ) @[simp] theorem algebraMap_eq : algebraMap R (MvPolynomial σ R) = C := rfl variable {R σ} /-- `X n` is the degree `1` monomial $X_n$. -/ def X (n : σ) : MvPolynomial σ R := monomial (Finsupp.single n 1) 1 theorem monomial_left_injective {r : R} (hr : r ≠ 0) : Function.Injective fun s : σ →₀ ℕ => monomial s r := Finsupp.single_left_injective hr @[simp] theorem monomial_left_inj {s t : σ →₀ ℕ} {r : R} (hr : r ≠ 0) : monomial s r = monomial t r ↔ s = t := Finsupp.single_left_inj hr theorem C_apply : (C a : MvPolynomial σ R) = monomial 0 a := rfl @[simp] theorem C_0 : C 0 = (0 : MvPolynomial σ R) := map_zero _ @[simp] theorem C_1 : C 1 = (1 : MvPolynomial σ R) := rfl theorem C_mul_monomial : C a * monomial s a' = monomial s (a * a') := by -- Porting note: this `show` feels like defeq abuse, but I can't find the appropriate lemmas show AddMonoidAlgebra.single _ _ * AddMonoidAlgebra.single _ _ = AddMonoidAlgebra.single _ _ simp [C_apply, single_mul_single] @[simp] theorem C_add : (C (a + a') : MvPolynomial σ R) = C a + C a' := Finsupp.single_add _ _ _ @[simp] theorem C_mul : (C (a * a') : MvPolynomial σ R) = C a * C a' := C_mul_monomial.symm @[simp] theorem C_pow (a : R) (n : ℕ) : (C (a ^ n) : MvPolynomial σ R) = C a ^ n := map_pow _ _ _ theorem C_injective (σ : Type*) (R : Type*) [CommSemiring R] : Function.Injective (C : R → MvPolynomial σ R) := Finsupp.single_injective _ theorem C_surjective {R : Type*} [CommSemiring R] (σ : Type*) [IsEmpty σ] : Function.Surjective (C : R → MvPolynomial σ R) := by refine fun p => ⟨p.toFun 0, Finsupp.ext fun a => ?_⟩ simp only [C_apply, ← single_eq_monomial, (Finsupp.ext isEmptyElim (α := σ) : a = 0), single_eq_same] rfl @[simp] theorem C_inj {σ : Type*} (R : Type*) [CommSemiring R] (r s : R) : (C r : MvPolynomial σ R) = C s ↔ r = s := (C_injective σ R).eq_iff @[simp] lemma C_eq_zero : (C a : MvPolynomial σ R) = 0 ↔ a = 0 := by rw [← map_zero C, C_inj] lemma C_ne_zero : (C a : MvPolynomial σ R) ≠ 0 ↔ a ≠ 0 := C_eq_zero.ne instance nontrivial_of_nontrivial (σ : Type*) (R : Type*) [CommSemiring R] [Nontrivial R] : Nontrivial (MvPolynomial σ R) := inferInstanceAs (Nontrivial <| AddMonoidAlgebra R (σ →₀ ℕ)) instance infinite_of_infinite (σ : Type*) (R : Type*) [CommSemiring R] [Infinite R] : Infinite (MvPolynomial σ R) := Infinite.of_injective C (C_injective _ _) instance infinite_of_nonempty (σ : Type*) (R : Type*) [Nonempty σ] [CommSemiring R] [Nontrivial R] : Infinite (MvPolynomial σ R) := Infinite.of_injective ((fun s : σ →₀ ℕ => monomial s 1) ∘ Finsupp.single (Classical.arbitrary σ)) <| (monomial_left_injective one_ne_zero).comp (Finsupp.single_injective _) theorem C_eq_coe_nat (n : ℕ) : (C ↑n : MvPolynomial σ R) = n := by induction n <;> simp [*] theorem C_mul' : MvPolynomial.C a * p = a • p := (Algebra.smul_def a p).symm theorem smul_eq_C_mul (p : MvPolynomial σ R) (a : R) : a • p = C a * p := C_mul'.symm theorem C_eq_smul_one : (C a : MvPolynomial σ R) = a • (1 : MvPolynomial σ R) := by rw [← C_mul', mul_one] theorem smul_monomial {S₁ : Type*} [SMulZeroClass S₁ R] (r : S₁) : r • monomial s a = monomial s (r • a) := Finsupp.smul_single _ _ _ theorem X_injective [Nontrivial R] : Function.Injective (X : σ → MvPolynomial σ R) := (monomial_left_injective one_ne_zero).comp (Finsupp.single_left_injective one_ne_zero) @[simp] theorem X_inj [Nontrivial R] (m n : σ) : X m = (X n : MvPolynomial σ R) ↔ m = n := X_injective.eq_iff theorem monomial_pow : monomial s a ^ e = monomial (e • s) (a ^ e) := AddMonoidAlgebra.single_pow e @[simp] theorem monomial_mul {s s' : σ →₀ ℕ} {a b : R} : monomial s a * monomial s' b = monomial (s + s') (a * b) := AddMonoidAlgebra.single_mul_single variable (σ R) /-- `fun s ↦ monomial s 1` as a homomorphism. -/ def monomialOneHom : Multiplicative (σ →₀ ℕ) →* MvPolynomial σ R := AddMonoidAlgebra.of _ _ variable {σ R} @[simp] theorem monomialOneHom_apply : monomialOneHom R σ s = (monomial s 1 : MvPolynomial σ R) := rfl theorem X_pow_eq_monomial : X n ^ e = monomial (Finsupp.single n e) (1 : R) := by simp [X, monomial_pow] theorem monomial_add_single : monomial (s + Finsupp.single n e) a = monomial s a * X n ^ e := by rw [X_pow_eq_monomial, monomial_mul, mul_one] theorem monomial_single_add : monomial (Finsupp.single n e + s) a = X n ^ e * monomial s a := by rw [X_pow_eq_monomial, monomial_mul, one_mul] theorem C_mul_X_pow_eq_monomial {s : σ} {a : R} {n : ℕ} : C a * X s ^ n = monomial (Finsupp.single s n) a := by rw [← zero_add (Finsupp.single s n), monomial_add_single, C_apply] theorem C_mul_X_eq_monomial {s : σ} {a : R} : C a * X s = monomial (Finsupp.single s 1) a := by rw [← C_mul_X_pow_eq_monomial, pow_one] @[simp] theorem monomial_zero {s : σ →₀ ℕ} : monomial s (0 : R) = 0 := Finsupp.single_zero _ @[simp] theorem monomial_zero' : (monomial (0 : σ →₀ ℕ) : R → MvPolynomial σ R) = C := rfl @[simp] theorem monomial_eq_zero {s : σ →₀ ℕ} {b : R} : monomial s b = 0 ↔ b = 0 := Finsupp.single_eq_zero @[simp] theorem sum_monomial_eq {A : Type*} [AddCommMonoid A] {u : σ →₀ ℕ} {r : R} {b : (σ →₀ ℕ) → R → A} (w : b u 0 = 0) : sum (monomial u r) b = b u r := Finsupp.sum_single_index w @[simp] theorem sum_C {A : Type*} [AddCommMonoid A] {b : (σ →₀ ℕ) → R → A} (w : b 0 0 = 0) : sum (C a) b = b 0 a := sum_monomial_eq w theorem monomial_sum_one {α : Type*} (s : Finset α) (f : α → σ →₀ ℕ) : (monomial (∑ i ∈ s, f i) 1 : MvPolynomial σ R) = ∏ i ∈ s, monomial (f i) 1 := map_prod (monomialOneHom R σ) (fun i => Multiplicative.ofAdd (f i)) s theorem monomial_sum_index {α : Type*} (s : Finset α) (f : α → σ →₀ ℕ) (a : R) : monomial (∑ i ∈ s, f i) a = C a * ∏ i ∈ s, monomial (f i) 1 := by rw [← monomial_sum_one, C_mul', ← (monomial _).map_smul, smul_eq_mul, mul_one] theorem monomial_finsupp_sum_index {α β : Type*} [Zero β] (f : α →₀ β) (g : α → β → σ →₀ ℕ) (a : R) : monomial (f.sum g) a = C a * f.prod fun a b => monomial (g a b) 1 := monomial_sum_index _ _ _ theorem monomial_eq_monomial_iff {α : Type*} (a₁ a₂ : α →₀ ℕ) (b₁ b₂ : R) : monomial a₁ b₁ = monomial a₂ b₂ ↔ a₁ = a₂ ∧ b₁ = b₂ ∨ b₁ = 0 ∧ b₂ = 0 := Finsupp.single_eq_single_iff _ _ _ _ theorem monomial_eq : monomial s a = C a * (s.prod fun n e => X n ^ e : MvPolynomial σ R) := by simp only [X_pow_eq_monomial, ← monomial_finsupp_sum_index, Finsupp.sum_single] @[simp] lemma prod_X_pow_eq_monomial : ∏ x ∈ s.support, X x ^ s x = monomial s (1 : R) := by simp only [monomial_eq, map_one, one_mul, Finsupp.prod] @[elab_as_elim] theorem induction_on_monomial {motive : MvPolynomial σ R → Prop} (C : ∀ a, motive (C a)) (mul_X : ∀ p n, motive p → motive (p * X n)) : ∀ s a, motive (monomial s a) := by intro s a apply @Finsupp.induction σ ℕ _ _ s · show motive (monomial 0 a) exact C a · intro n e p _hpn _he ih have : ∀ e : ℕ, motive (monomial p a * X n ^ e) := by intro e induction e with | zero => simp [ih] | succ e e_ih => simp [ih, pow_succ, (mul_assoc _ _ _).symm, mul_X, e_ih] simp [add_comm, monomial_add_single, this] /-- Analog of `Polynomial.induction_on'`. To prove something about mv_polynomials, it suffices to show the condition is closed under taking sums, and it holds for monomials. -/ @[elab_as_elim] theorem induction_on' {P : MvPolynomial σ R → Prop} (p : MvPolynomial σ R) (monomial : ∀ (u : σ →₀ ℕ) (a : R), P (monomial u a)) (add : ∀ p q : MvPolynomial σ R, P p → P q → P (p + q)) : P p := Finsupp.induction p (suffices P (MvPolynomial.monomial 0 0) by rwa [monomial_zero] at this show P (MvPolynomial.monomial 0 0) from monomial 0 0) fun _ _ _ _ha _hb hPf => add _ _ (monomial _ _) hPf /-- Similar to `MvPolynomial.induction_on` but only a weak form of `h_add` is required. In particular, this version only requires us to show that `motive` is closed under addition of nontrivial monomials not present in the support. -/ @[elab_as_elim] theorem monomial_add_induction_on {motive : MvPolynomial σ R → Prop} (p : MvPolynomial σ R) (C : ∀ a, motive (C a)) (monomial_add : ∀ (a : σ →₀ ℕ) (b : R) (f : MvPolynomial σ R), a ∉ f.support → b ≠ 0 → motive f → motive ((monomial a b) + f)) : motive p := Finsupp.induction p (C_0.rec <| C 0) monomial_add @[deprecated (since := "2025-03-11")] alias induction_on''' := monomial_add_induction_on /-- Similar to `MvPolynomial.induction_on` but only a yet weaker form of `h_add` is required. In particular, this version only requires us to show that `motive` is closed under addition of monomials not present in the support for which `motive` is already known to hold. -/ theorem induction_on'' {motive : MvPolynomial σ R → Prop} (p : MvPolynomial σ R) (C : ∀ a, motive (C a)) (monomial_add : ∀ (a : σ →₀ ℕ) (b : R) (f : MvPolynomial σ R), a ∉ f.support → b ≠ 0 → motive f → motive (monomial a b) → motive ((monomial a b) + f)) (mul_X : ∀ (p : MvPolynomial σ R) (n : σ), motive p → motive (p * MvPolynomial.X n)) : motive p := monomial_add_induction_on p C fun a b f ha hb hf => monomial_add a b f ha hb hf <| induction_on_monomial C mul_X a b /-- Analog of `Polynomial.induction_on`. If a property holds for any constant polynomial and is preserved under addition and multiplication by variables then it holds for all multivariate polynomials. -/ @[recursor 5] theorem induction_on {motive : MvPolynomial σ R → Prop} (p : MvPolynomial σ R) (C : ∀ a, motive (C a)) (add : ∀ p q, motive p → motive q → motive (p + q)) (mul_X : ∀ p n, motive p → motive (p * X n)) : motive p := induction_on'' p C (fun a b f _ha _hb hf hm => add (monomial a b) f hm hf) mul_X theorem ringHom_ext {A : Type*} [Semiring A] {f g : MvPolynomial σ R →+* A} (hC : ∀ r, f (C r) = g (C r)) (hX : ∀ i, f (X i) = g (X i)) : f = g := by refine AddMonoidAlgebra.ringHom_ext' ?_ ?_ -- Porting note (https://github.com/leanprover-community/mathlib4/issues/11041): this has high priority, but Lean still chooses `RingHom.ext`, why? -- probably because of the type synonym · ext x exact hC _ · apply Finsupp.mulHom_ext'; intros x -- Porting note (https://github.com/leanprover-community/mathlib4/issues/11041): `Finsupp.mulHom_ext'` needs to have increased priority apply MonoidHom.ext_mnat exact hX _ /-- See note [partially-applied ext lemmas]. -/ @[ext 1100] theorem ringHom_ext' {A : Type*} [Semiring A] {f g : MvPolynomial σ R →+* A} (hC : f.comp C = g.comp C) (hX : ∀ i, f (X i) = g (X i)) : f = g := ringHom_ext (RingHom.ext_iff.1 hC) hX theorem hom_eq_hom [Semiring S₂] (f g : MvPolynomial σ R →+* S₂) (hC : f.comp C = g.comp C) (hX : ∀ n : σ, f (X n) = g (X n)) (p : MvPolynomial σ R) : f p = g p := RingHom.congr_fun (ringHom_ext' hC hX) p theorem is_id (f : MvPolynomial σ R →+* MvPolynomial σ R) (hC : f.comp C = C) (hX : ∀ n : σ, f (X n) = X n) (p : MvPolynomial σ R) : f p = p := hom_eq_hom f (RingHom.id _) hC hX p @[ext 1100] theorem algHom_ext' {A B : Type*} [CommSemiring A] [CommSemiring B] [Algebra R A] [Algebra R B] {f g : MvPolynomial σ A →ₐ[R] B} (h₁ : f.comp (IsScalarTower.toAlgHom R A (MvPolynomial σ A)) = g.comp (IsScalarTower.toAlgHom R A (MvPolynomial σ A))) (h₂ : ∀ i, f (X i) = g (X i)) : f = g := AlgHom.coe_ringHom_injective (MvPolynomial.ringHom_ext' (congr_arg AlgHom.toRingHom h₁) h₂) @[ext 1200] theorem algHom_ext {A : Type*} [Semiring A] [Algebra R A] {f g : MvPolynomial σ R →ₐ[R] A} (hf : ∀ i : σ, f (X i) = g (X i)) : f = g := AddMonoidAlgebra.algHom_ext' (mulHom_ext' fun X : σ => MonoidHom.ext_mnat (hf X)) @[simp] theorem algHom_C {A : Type*} [Semiring A] [Algebra R A] (f : MvPolynomial σ R →ₐ[R] A) (r : R) : f (C r) = algebraMap R A r := f.commutes r @[simp] theorem adjoin_range_X : Algebra.adjoin R (range (X : σ → MvPolynomial σ R)) = ⊤ := by set S := Algebra.adjoin R (range (X : σ → MvPolynomial σ R)) refine top_unique fun p hp => ?_; clear hp induction p using MvPolynomial.induction_on with | C => exact S.algebraMap_mem _ | add p q hp hq => exact S.add_mem hp hq | mul_X p i hp => exact S.mul_mem hp (Algebra.subset_adjoin <| mem_range_self _) @[ext] theorem linearMap_ext {M : Type*} [AddCommMonoid M] [Module R M] {f g : MvPolynomial σ R →ₗ[R] M} (h : ∀ s, f ∘ₗ monomial s = g ∘ₗ monomial s) : f = g := Finsupp.lhom_ext' h section Support /-- The finite set of all `m : σ →₀ ℕ` such that `X^m` has a non-zero coefficient. -/ def support (p : MvPolynomial σ R) : Finset (σ →₀ ℕ) := Finsupp.support p theorem finsupp_support_eq_support (p : MvPolynomial σ R) : Finsupp.support p = p.support := rfl theorem support_monomial [h : Decidable (a = 0)] : (monomial s a).support = if a = 0 then ∅ else {s} := by rw [← Subsingleton.elim (Classical.decEq R a 0) h] rfl theorem support_monomial_subset : (monomial s a).support ⊆ {s} := support_single_subset theorem support_add [DecidableEq σ] : (p + q).support ⊆ p.support ∪ q.support := Finsupp.support_add theorem support_X [Nontrivial R] : (X n : MvPolynomial σ R).support = {Finsupp.single n 1} := by classical rw [X, support_monomial, if_neg]; exact one_ne_zero theorem support_X_pow [Nontrivial R] (s : σ) (n : ℕ) : (X s ^ n : MvPolynomial σ R).support = {Finsupp.single s n} := by classical rw [X_pow_eq_monomial, support_monomial, if_neg (one_ne_zero' R)] @[simp] theorem support_zero : (0 : MvPolynomial σ R).support = ∅ := rfl theorem support_smul {S₁ : Type*} [SMulZeroClass S₁ R] {a : S₁} {f : MvPolynomial σ R} : (a • f).support ⊆ f.support := Finsupp.support_smul theorem support_sum {α : Type*} [DecidableEq σ] {s : Finset α} {f : α → MvPolynomial σ R} : (∑ x ∈ s, f x).support ⊆ s.biUnion fun x => (f x).support := Finsupp.support_finset_sum end Support section Coeff /-- The coefficient of the monomial `m` in the multi-variable polynomial `p`. -/ def coeff (m : σ →₀ ℕ) (p : MvPolynomial σ R) : R := @DFunLike.coe ((σ →₀ ℕ) →₀ R) _ _ _ p m @[simp] theorem mem_support_iff {p : MvPolynomial σ R} {m : σ →₀ ℕ} : m ∈ p.support ↔ p.coeff m ≠ 0 := by simp [support, coeff] theorem not_mem_support_iff {p : MvPolynomial σ R} {m : σ →₀ ℕ} : m ∉ p.support ↔ p.coeff m = 0 := by simp theorem sum_def {A} [AddCommMonoid A] {p : MvPolynomial σ R} {b : (σ →₀ ℕ) → R → A} : p.sum b = ∑ m ∈ p.support, b m (p.coeff m) := by simp [support, Finsupp.sum, coeff] theorem support_mul [DecidableEq σ] (p q : MvPolynomial σ R) : (p * q).support ⊆ p.support + q.support := AddMonoidAlgebra.support_mul p q @[ext] theorem ext (p q : MvPolynomial σ R) : (∀ m, coeff m p = coeff m q) → p = q := Finsupp.ext @[simp] theorem coeff_add (m : σ →₀ ℕ) (p q : MvPolynomial σ R) : coeff m (p + q) = coeff m p + coeff m q := add_apply p q m @[simp] theorem coeff_smul {S₁ : Type*} [SMulZeroClass S₁ R] (m : σ →₀ ℕ) (C : S₁) (p : MvPolynomial σ R) : coeff m (C • p) = C • coeff m p := smul_apply C p m @[simp] theorem coeff_zero (m : σ →₀ ℕ) : coeff m (0 : MvPolynomial σ R) = 0 := rfl @[simp] theorem coeff_zero_X (i : σ) : coeff 0 (X i : MvPolynomial σ R) = 0 := single_eq_of_ne fun h => by cases Finsupp.single_eq_zero.1 h /-- `MvPolynomial.coeff m` but promoted to an `AddMonoidHom`. -/ @[simps] def coeffAddMonoidHom (m : σ →₀ ℕ) : MvPolynomial σ R →+ R where toFun := coeff m map_zero' := coeff_zero m map_add' := coeff_add m variable (R) in /-- `MvPolynomial.coeff m` but promoted to a `LinearMap`. -/ @[simps] def lcoeff (m : σ →₀ ℕ) : MvPolynomial σ R →ₗ[R] R where toFun := coeff m map_add' := coeff_add m map_smul' := coeff_smul m theorem coeff_sum {X : Type*} (s : Finset X) (f : X → MvPolynomial σ R) (m : σ →₀ ℕ) : coeff m (∑ x ∈ s, f x) = ∑ x ∈ s, coeff m (f x) := map_sum (@coeffAddMonoidHom R σ _ _) _ s theorem monic_monomial_eq (m) : monomial m (1 : R) = (m.prod fun n e => X n ^ e : MvPolynomial σ R) := by simp [monomial_eq] @[simp] theorem coeff_monomial [DecidableEq σ] (m n) (a) : coeff m (monomial n a : MvPolynomial σ R) = if n = m then a else 0 := Finsupp.single_apply @[simp] theorem coeff_C [DecidableEq σ] (m) (a) : coeff m (C a : MvPolynomial σ R) = if 0 = m then a else 0 := Finsupp.single_apply lemma eq_C_of_isEmpty [IsEmpty σ] (p : MvPolynomial σ R) : p = C (p.coeff 0) := by obtain ⟨x, rfl⟩ := C_surjective σ p simp theorem coeff_one [DecidableEq σ] (m) : coeff m (1 : MvPolynomial σ R) = if 0 = m then 1 else 0 := coeff_C m 1 @[simp] theorem coeff_zero_C (a) : coeff 0 (C a : MvPolynomial σ R) = a := single_eq_same @[simp] theorem coeff_zero_one : coeff 0 (1 : MvPolynomial σ R) = 1 := coeff_zero_C 1 theorem coeff_X_pow [DecidableEq σ] (i : σ) (m) (k : ℕ) : coeff m (X i ^ k : MvPolynomial σ R) = if Finsupp.single i k = m then 1 else 0 := by have := coeff_monomial m (Finsupp.single i k) (1 : R) rwa [@monomial_eq _ _ (1 : R) (Finsupp.single i k) _, C_1, one_mul, Finsupp.prod_single_index] at this exact pow_zero _ theorem coeff_X' [DecidableEq σ] (i : σ) (m) : coeff m (X i : MvPolynomial σ R) = if Finsupp.single i 1 = m then 1 else 0 := by rw [← coeff_X_pow, pow_one] @[simp] theorem coeff_X (i : σ) : coeff (Finsupp.single i 1) (X i : MvPolynomial σ R) = 1 := by classical rw [coeff_X', if_pos rfl] @[simp] theorem coeff_C_mul (m) (a : R) (p : MvPolynomial σ R) : coeff m (C a * p) = a * coeff m p := by classical rw [mul_def, sum_C] · simp +contextual [sum_def, coeff_sum] simp theorem coeff_mul [DecidableEq σ] (p q : MvPolynomial σ R) (n : σ →₀ ℕ) : coeff n (p * q) = ∑ x ∈ Finset.antidiagonal n, coeff x.1 p * coeff x.2 q := AddMonoidAlgebra.mul_apply_antidiagonal p q _ _ Finset.mem_antidiagonal @[simp] theorem coeff_mul_monomial (m) (s : σ →₀ ℕ) (r : R) (p : MvPolynomial σ R) : coeff (m + s) (p * monomial s r) = coeff m p * r := AddMonoidAlgebra.mul_single_apply_aux p _ _ _ _ fun _a _ => add_left_inj _ @[simp] theorem coeff_monomial_mul (m) (s : σ →₀ ℕ) (r : R) (p : MvPolynomial σ R) : coeff (s + m) (monomial s r * p) = r * coeff m p := AddMonoidAlgebra.single_mul_apply_aux p _ _ _ _ fun _a _ => add_right_inj _ @[simp] theorem coeff_mul_X (m) (s : σ) (p : MvPolynomial σ R) : coeff (m + Finsupp.single s 1) (p * X s) = coeff m p := (coeff_mul_monomial _ _ _ _).trans (mul_one _) @[simp] theorem coeff_X_mul (m) (s : σ) (p : MvPolynomial σ R) : coeff (Finsupp.single s 1 + m) (X s * p) = coeff m p := (coeff_monomial_mul _ _ _ _).trans (one_mul _) lemma coeff_single_X_pow [DecidableEq σ] (s s' : σ) (n n' : ℕ) : (X (R := R) s ^ n).coeff (Finsupp.single s' n') = if s = s' ∧ n = n' ∨ n = 0 ∧ n' = 0 then 1 else 0 := by simp only [coeff_X_pow, single_eq_single_iff] @[simp] lemma coeff_single_X [DecidableEq σ] (s s' : σ) (n : ℕ) : (X s).coeff (R := R) (Finsupp.single s' n) = if n = 1 ∧ s = s' then 1 else 0 := by simpa [eq_comm, and_comm] using coeff_single_X_pow s s' 1 n @[simp] theorem support_mul_X (s : σ) (p : MvPolynomial σ R) : (p * X s).support = p.support.map (addRightEmbedding (Finsupp.single s 1)) := AddMonoidAlgebra.support_mul_single p _ (by simp) _ @[simp] theorem support_X_mul (s : σ) (p : MvPolynomial σ R) : (X s * p).support = p.support.map (addLeftEmbedding (Finsupp.single s 1)) := AddMonoidAlgebra.support_single_mul p _ (by simp) _ @[simp] theorem support_smul_eq {S₁ : Type*} [Semiring S₁] [Module S₁ R] [NoZeroSMulDivisors S₁ R] {a : S₁} (h : a ≠ 0) (p : MvPolynomial σ R) : (a • p).support = p.support := Finsupp.support_smul_eq h theorem support_sdiff_support_subset_support_add [DecidableEq σ] (p q : MvPolynomial σ R) : p.support \ q.support ⊆ (p + q).support := by intro m hm simp only [Classical.not_not, mem_support_iff, Finset.mem_sdiff, Ne] at hm simp [hm.2, hm.1] open scoped symmDiff in theorem support_symmDiff_support_subset_support_add [DecidableEq σ] (p q : MvPolynomial σ R) : p.support ∆ q.support ⊆ (p + q).support := by rw [symmDiff_def, Finset.sup_eq_union] apply Finset.union_subset · exact support_sdiff_support_subset_support_add p q · rw [add_comm] exact support_sdiff_support_subset_support_add q p theorem coeff_mul_monomial' (m) (s : σ →₀ ℕ) (r : R) (p : MvPolynomial σ R) : coeff m (p * monomial s r) = if s ≤ m then coeff (m - s) p * r else 0 := by classical split_ifs with h · conv_rhs => rw [← coeff_mul_monomial _ s] congr with t rw [tsub_add_cancel_of_le h] · contrapose! h rw [← mem_support_iff] at h obtain ⟨j, -, rfl⟩ : ∃ j ∈ support p, j + s = m := by simpa [Finset.mem_add] using Finset.add_subset_add_left support_monomial_subset <| support_mul _ _ h exact le_add_left le_rfl theorem coeff_monomial_mul' (m) (s : σ →₀ ℕ) (r : R) (p : MvPolynomial σ R) : coeff m (monomial s r * p) = if s ≤ m then r * coeff (m - s) p else 0 := by -- note that if we allow `R` to be non-commutative we will have to duplicate the proof above. rw [mul_comm, mul_comm r] exact coeff_mul_monomial' _ _ _ _ theorem coeff_mul_X' [DecidableEq σ] (m) (s : σ) (p : MvPolynomial σ R) : coeff m (p * X s) = if s ∈ m.support then coeff (m - Finsupp.single s 1) p else 0 := by refine (coeff_mul_monomial' _ _ _ _).trans ?_ simp_rw [Finsupp.single_le_iff, Finsupp.mem_support_iff, Nat.succ_le_iff, pos_iff_ne_zero, mul_one] theorem coeff_X_mul' [DecidableEq σ] (m) (s : σ) (p : MvPolynomial σ R) : coeff m (X s * p) = if s ∈ m.support then coeff (m - Finsupp.single s 1) p else 0 := by refine (coeff_monomial_mul' _ _ _ _).trans ?_ simp_rw [Finsupp.single_le_iff, Finsupp.mem_support_iff, Nat.succ_le_iff, pos_iff_ne_zero, one_mul] theorem eq_zero_iff {p : MvPolynomial σ R} : p = 0 ↔ ∀ d, coeff d p = 0 := by rw [MvPolynomial.ext_iff] simp only [coeff_zero] theorem ne_zero_iff {p : MvPolynomial σ R} : p ≠ 0 ↔ ∃ d, coeff d p ≠ 0 := by rw [Ne, eq_zero_iff] push_neg rfl @[simp] theorem X_ne_zero [Nontrivial R] (s : σ) : X (R := R) s ≠ 0 := by rw [ne_zero_iff] use Finsupp.single s 1 simp only [coeff_X, ne_eq, one_ne_zero, not_false_eq_true] @[simp] theorem support_eq_empty {p : MvPolynomial σ R} : p.support = ∅ ↔ p = 0 := Finsupp.support_eq_empty @[simp] lemma support_nonempty {p : MvPolynomial σ R} : p.support.Nonempty ↔ p ≠ 0 := by rw [Finset.nonempty_iff_ne_empty, ne_eq, support_eq_empty] theorem exists_coeff_ne_zero {p : MvPolynomial σ R} (h : p ≠ 0) : ∃ d, coeff d p ≠ 0 := ne_zero_iff.mp h theorem C_dvd_iff_dvd_coeff (r : R) (φ : MvPolynomial σ R) : C r ∣ φ ↔ ∀ i, r ∣ φ.coeff i := by constructor · rintro ⟨φ, rfl⟩ c rw [coeff_C_mul] apply dvd_mul_right · intro h choose C hc using h classical let c' : (σ →₀ ℕ) → R := fun i => if i ∈ φ.support then C i else 0 let ψ : MvPolynomial σ R := ∑ i ∈ φ.support, monomial i (c' i) use ψ apply MvPolynomial.ext intro i simp only [ψ, c', coeff_C_mul, coeff_sum, coeff_monomial, Finset.sum_ite_eq'] split_ifs with hi · rw [hc] · rw [not_mem_support_iff] at hi rwa [mul_zero] @[simp] lemma isRegular_X : IsRegular (X n : MvPolynomial σ R) := by suffices IsLeftRegular (X n : MvPolynomial σ R) from ⟨this, this.right_of_commute <| Commute.all _⟩ intro P Q (hPQ : (X n) * P = (X n) * Q) ext i rw [← coeff_X_mul i n P, hPQ, coeff_X_mul i n Q] @[simp] lemma isRegular_X_pow (k : ℕ) : IsRegular (X n ^ k : MvPolynomial σ R) := isRegular_X.pow k @[simp] lemma isRegular_prod_X (s : Finset σ) : IsRegular (∏ n ∈ s, X n : MvPolynomial σ R) := IsRegular.prod fun _ _ ↦ isRegular_X /-- The finset of nonzero coefficients of a multivariate polynomial. -/ def coeffs (p : MvPolynomial σ R) : Finset R := letI := Classical.decEq R Finset.image p.coeff p.support @[simp] lemma coeffs_zero : coeffs (0 : MvPolynomial σ R) = ∅ := rfl lemma coeffs_one : coeffs (1 : MvPolynomial σ R) ⊆ {1} := by classical rw [coeffs, Finset.image_subset_iff] simp_all [coeff_one] @[nontriviality] lemma coeffs_eq_empty_of_subsingleton [Subsingleton R] (p : MvPolynomial σ R) : p.coeffs = ∅ := by simpa [coeffs] using Subsingleton.eq_zero p @[simp] lemma coeffs_one_of_nontrivial [Nontrivial R] : coeffs (1 : MvPolynomial σ R) = {1} := by apply Finset.Subset.antisymm coeffs_one simp only [coeffs, Finset.singleton_subset_iff, Finset.mem_image] exact ⟨0, by simp⟩ lemma mem_coeffs_iff {p : MvPolynomial σ R} {c : R} : c ∈ p.coeffs ↔ ∃ n ∈ p.support, c = p.coeff n := by simp [coeffs, eq_comm, (Finset.mem_image)] lemma coeff_mem_coeffs {p : MvPolynomial σ R} (m : σ →₀ ℕ) (h : p.coeff m ≠ 0) : p.coeff m ∈ p.coeffs := letI := Classical.decEq R Finset.mem_image_of_mem p.coeff (mem_support_iff.mpr h) lemma zero_not_mem_coeffs (p : MvPolynomial σ R) : 0 ∉ p.coeffs := by intro hz obtain ⟨n, hnsupp, hn⟩ := mem_coeffs_iff.mp hz exact (mem_support_iff.mp hnsupp) hn.symm end Coeff section ConstantCoeff /-- `constantCoeff p` returns the constant term of the polynomial `p`, defined as `coeff 0 p`. This is a ring homomorphism. -/ def constantCoeff : MvPolynomial σ R →+* R where toFun := coeff 0 map_one' := by simp [AddMonoidAlgebra.one_def] map_mul' := by classical simp [coeff_mul, Finsupp.support_single_ne_zero] map_zero' := coeff_zero _ map_add' := coeff_add _ theorem constantCoeff_eq : (constantCoeff : MvPolynomial σ R → R) = coeff 0 := rfl variable (σ) in @[simp] theorem constantCoeff_C (r : R) : constantCoeff (C r : MvPolynomial σ R) = r := by classical simp [constantCoeff_eq] variable (R) in @[simp] theorem constantCoeff_X (i : σ) : constantCoeff (X i : MvPolynomial σ R) = 0 := by simp [constantCoeff_eq] @[simp] theorem constantCoeff_smul {R : Type*} [SMulZeroClass R S₁] (a : R) (f : MvPolynomial σ S₁) : constantCoeff (a • f) = a • constantCoeff f := rfl theorem constantCoeff_monomial [DecidableEq σ] (d : σ →₀ ℕ) (r : R) : constantCoeff (monomial d r) = if d = 0 then r else 0 := by rw [constantCoeff_eq, coeff_monomial] variable (σ R) @[simp] theorem constantCoeff_comp_C : constantCoeff.comp (C : R →+* MvPolynomial σ R) = RingHom.id R := by ext x exact constantCoeff_C σ x theorem constantCoeff_comp_algebraMap : constantCoeff.comp (algebraMap R (MvPolynomial σ R)) = RingHom.id R := constantCoeff_comp_C _ _ end ConstantCoeff section AsSum @[simp] theorem support_sum_monomial_coeff (p : MvPolynomial σ R) : (∑ v ∈ p.support, monomial v (coeff v p)) = p := Finsupp.sum_single p theorem as_sum (p : MvPolynomial σ R) : p = ∑ v ∈ p.support, monomial v (coeff v p) := (support_sum_monomial_coeff p).symm end AsSum section coeffsIn variable {R S σ : Type*} [CommSemiring R] [CommSemiring S] section Module variable [Module R S] {M N : Submodule R S} {p : MvPolynomial σ S} {s : σ} {i : σ →₀ ℕ} {x : S} {n : ℕ} variable (σ M) in /-- The `R`-submodule of multivariate polynomials whose coefficients lie in a `R`-submodule `M`. -/ @[simps] def coeffsIn : Submodule R (MvPolynomial σ S) where carrier := {p | ∀ i, p.coeff i ∈ M} add_mem' := by simp+contextual [add_mem] zero_mem' := by simp smul_mem' := by simp+contextual [Submodule.smul_mem] lemma mem_coeffsIn : p ∈ coeffsIn σ M ↔ ∀ i, p.coeff i ∈ M := .rfl @[simp] lemma monomial_mem_coeffsIn : monomial i x ∈ coeffsIn σ M ↔ x ∈ M := by classical simp only [mem_coeffsIn, coeff_monomial] exact ⟨fun h ↦ by simpa using h i, fun hs j ↦ by split <;> simp [hs]⟩ @[simp] lemma C_mem_coeffsIn : C x ∈ coeffsIn σ M ↔ x ∈ M := by simpa using monomial_mem_coeffsIn (i := 0) @[simp] lemma one_coeffsIn : 1 ∈ coeffsIn σ M ↔ 1 ∈ M := by simpa using C_mem_coeffsIn (x := (1 : S)) @[simp] lemma mul_monomial_mem_coeffsIn : p * monomial i 1 ∈ coeffsIn σ M ↔ p ∈ coeffsIn σ M := by classical simp only [mem_coeffsIn, coeff_mul_monomial', Finsupp.mem_support_iff] constructor · rintro hp j simpa using hp (j + i) · rintro hp i split <;> simp [hp] @[simp] lemma monomial_mul_mem_coeffsIn : monomial i 1 * p ∈ coeffsIn σ M ↔ p ∈ coeffsIn σ M := by simp [mul_comm] @[simp] lemma mul_X_mem_coeffsIn : p * X s ∈ coeffsIn σ M ↔ p ∈ coeffsIn σ M := by simpa [-mul_monomial_mem_coeffsIn] using mul_monomial_mem_coeffsIn (i := .single s 1) @[simp] lemma X_mul_mem_coeffsIn : X s * p ∈ coeffsIn σ M ↔ p ∈ coeffsIn σ M := by simp [mul_comm] variable (M) in lemma coeffsIn_eq_span_monomial : coeffsIn σ M = .span R {monomial i m | (m ∈ M) (i : σ →₀ ℕ)} := by classical refine le_antisymm ?_ <| Submodule.span_le.2 ?_ · rintro p hp rw [p.as_sum] exact sum_mem fun i hi ↦ Submodule.subset_span ⟨_, hp i, _, rfl⟩ · rintro _ ⟨m, hm, s, n, rfl⟩ i simp [coeff_X_pow] split <;> simp [hm] lemma coeffsIn_le {N : Submodule R (MvPolynomial σ S)} : coeffsIn σ M ≤ N ↔ ∀ m ∈ M, ∀ i, monomial i m ∈ N := by simp [coeffsIn_eq_span_monomial, Submodule.span_le, Set.subset_def, forall_swap (α := MvPolynomial σ S)] end Module section Algebra variable [Algebra R S] {M : Submodule R S} lemma coeffsIn_mul (M N : Submodule R S) : coeffsIn σ (M * N) = coeffsIn σ M * coeffsIn σ N := by classical refine le_antisymm (coeffsIn_le.2 ?_) ?_ · intros r hr s induction hr using Submodule.mul_induction_on' with | mem_mul_mem m hm n hn => rw [← add_zero s, ← monomial_mul] apply Submodule.mul_mem_mul <;> simpa | add x _ y _ hx hy => simpa [map_add] using add_mem hx hy · rw [Submodule.mul_le] intros x hx y hy k rw [MvPolynomial.coeff_mul] exact sum_mem fun c hc ↦ Submodule.mul_mem_mul (hx _) (hy _) lemma coeffsIn_pow : ∀ {n}, n ≠ 0 → ∀ M : Submodule R S, coeffsIn σ (M ^ n) = coeffsIn σ M ^ n | 1, _, M => by simp | n + 2, _, M => by rw [pow_succ, coeffsIn_mul, coeffsIn_pow, ← pow_succ]; exact n.succ_ne_zero lemma le_coeffsIn_pow : ∀ {n}, coeffsIn σ M ^ n ≤ coeffsIn σ (M ^ n) | 0 => by simpa using ⟨1, map_one _⟩ | n + 1 => (coeffsIn_pow n.succ_ne_zero _).ge end Algebra end coeffsIn end CommSemiring end MvPolynomial
Mathlib/Algebra/MvPolynomial/Basic.lean
1,025
1,028
/- Copyright (c) 2022 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import Mathlib.Algebra.BigOperators.Fin import Mathlib.Algebra.Group.Fin.Tuple import Mathlib.Data.Finset.NatAntidiagonal import Mathlib.Order.Fin.Tuple /-! # Collections of tuples of naturals with the same sum This file generalizes `List.Nat.Antidiagonal n`, `Multiset.Nat.Antidiagonal n`, and `Finset.Nat.Antidiagonal n` from the pair of elements `x : ℕ × ℕ` such that `n = x.1 + x.2`, to the sequence of elements `x : Fin k → ℕ` such that `n = ∑ i, x i`. ## Main definitions * `List.Nat.antidiagonalTuple` * `Multiset.Nat.antidiagonalTuple` * `Finset.Nat.antidiagonalTuple` ## Main results * `antidiagonalTuple 2 n` is analogous to `antidiagonal n`: * `List.Nat.antidiagonalTuple_two` * `Multiset.Nat.antidiagonalTuple_two` * `Finset.Nat.antidiagonalTuple_two` ## Implementation notes While we could implement this by filtering `(Fintype.PiFinset fun _ ↦ range (n + 1))` or similar, this implementation would be much slower. In the future, we could consider generalizing `Finset.Nat.antidiagonalTuple` further to support finitely-supported functions, as is done with `cut` in `archive/100-theorems-list/45_partition.lean`. -/ /-! ### Lists -/ namespace List.Nat /-- `List.antidiagonalTuple k n` is a list of all `k`-tuples which sum to `n`. This list contains no duplicates (`List.Nat.nodup_antidiagonalTuple`), and is sorted lexicographically (`List.Nat.antidiagonalTuple_pairwise_pi_lex`), starting with `![0, ..., n]` and ending with `![n, ..., 0]`. ``` #eval antidiagonalTuple 3 2 -- [![0, 0, 2], ![0, 1, 1], ![0, 2, 0], ![1, 0, 1], ![1, 1, 0], ![2, 0, 0]] ``` -/ def antidiagonalTuple : ∀ k, ℕ → List (Fin k → ℕ) | 0, 0 => [![]] | 0, _ + 1 => [] | k + 1, n => (List.Nat.antidiagonal n).flatMap fun ni => (antidiagonalTuple k ni.2).map fun x => Fin.cons ni.1 x @[simp] theorem antidiagonalTuple_zero_zero : antidiagonalTuple 0 0 = [![]] := rfl @[simp] theorem antidiagonalTuple_zero_succ (n : ℕ) : antidiagonalTuple 0 (n + 1) = [] := rfl theorem mem_antidiagonalTuple {n : ℕ} {k : ℕ} {x : Fin k → ℕ} : x ∈ antidiagonalTuple k n ↔ ∑ i, x i = n := by induction x using Fin.consInduction generalizing n with | h0 => cases n
· decide · simp [eq_comm] | h x₀ x ih => simp_rw [Fin.sum_cons, antidiagonalTuple, List.mem_flatMap, List.mem_map, List.Nat.mem_antidiagonal, Fin.cons_inj, exists_eq_right_right, ih, @eq_comm _ _ (Prod.snd _), and_comm (a := Prod.snd _ = _), ← Prod.mk_inj (a₁ := Prod.fst _), exists_eq_right] /-- The antidiagonal of `n` does not contain duplicate entries. -/ theorem nodup_antidiagonalTuple (k n : ℕ) : List.Nodup (antidiagonalTuple k n) := by induction' k with k ih generalizing n · cases n
Mathlib/Data/Fin/Tuple/NatAntidiagonal.lean
79
90
/- Copyright (c) 2019 Rohan Mitta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Rohan Mitta, Kevin Buzzard, Alistair Tucker, Johannes Hölzl, Yury Kudryashov -/ import Mathlib.Analysis.SpecificLimits.Basic import Mathlib.Data.Setoid.Basic import Mathlib.Dynamics.FixedPoints.Topology import Mathlib.Topology.MetricSpace.Lipschitz /-! # Contracting maps A Lipschitz continuous self-map with Lipschitz constant `K < 1` is called a *contracting map*. In this file we prove the Banach fixed point theorem, some explicit estimates on the rate of convergence, and some properties of the map sending a contracting map to its fixed point. ## Main definitions * `ContractingWith K f` : a Lipschitz continuous self-map with `K < 1`; * `efixedPoint` : given a contracting map `f` on a complete emetric space and a point `x` such that `edist x (f x) ≠ ∞`, `efixedPoint f hf x hx` is the unique fixed point of `f` in `EMetric.ball x ∞`; * `fixedPoint` : the unique fixed point of a contracting map on a complete nonempty metric space. ## Tags contracting map, fixed point, Banach fixed point theorem -/ open NNReal Topology ENNReal Filter Function variable {α : Type*} /-- A map is said to be `ContractingWith K`, if `K < 1` and `f` is `LipschitzWith K`. -/ def ContractingWith [EMetricSpace α] (K : ℝ≥0) (f : α → α) := K < 1 ∧ LipschitzWith K f namespace ContractingWith variable [EMetricSpace α] {K : ℝ≥0} {f : α → α} open EMetric Set theorem toLipschitzWith (hf : ContractingWith K f) : LipschitzWith K f := hf.2 theorem one_sub_K_pos' (hf : ContractingWith K f) : (0 : ℝ≥0∞) < 1 - K := by simp [hf.1] theorem one_sub_K_ne_zero (hf : ContractingWith K f) : (1 : ℝ≥0∞) - K ≠ 0 := ne_of_gt hf.one_sub_K_pos' theorem one_sub_K_ne_top : (1 : ℝ≥0∞) - K ≠ ∞ := by norm_cast exact ENNReal.coe_ne_top theorem edist_inequality (hf : ContractingWith K f) {x y} (h : edist x y ≠ ∞) : edist x y ≤ (edist x (f x) + edist y (f y)) / (1 - K) := suffices edist x y ≤ edist x (f x) + edist y (f y) + K * edist x y by rwa [ENNReal.le_div_iff_mul_le (Or.inl hf.one_sub_K_ne_zero) (Or.inl one_sub_K_ne_top), mul_comm, ENNReal.sub_mul fun _ _ ↦ h, one_mul, tsub_le_iff_right] calc edist x y ≤ edist x (f x) + edist (f x) (f y) + edist (f y) y := edist_triangle4 _ _ _ _ _ = edist x (f x) + edist y (f y) + edist (f x) (f y) := by rw [edist_comm y, add_right_comm] _ ≤ edist x (f x) + edist y (f y) + K * edist x y := add_le_add le_rfl (hf.2 _ _) theorem edist_le_of_fixedPoint (hf : ContractingWith K f) {x y} (h : edist x y ≠ ∞) (hy : IsFixedPt f y) : edist x y ≤ edist x (f x) / (1 - K) := by
simpa only [hy.eq, edist_self, add_zero] using hf.edist_inequality h theorem eq_or_edist_eq_top_of_fixedPoints (hf : ContractingWith K f) {x y} (hx : IsFixedPt f x) (hy : IsFixedPt f y) : x = y ∨ edist x y = ∞ := by refine or_iff_not_imp_right.2 fun h ↦ edist_le_zero.1 ?_ simpa only [hx.eq, edist_self, add_zero, ENNReal.zero_div] using hf.edist_le_of_fixedPoint h hy /-- If a map `f` is `ContractingWith K`, and `s` is a forward-invariant set, then restriction of `f` to `s` is `ContractingWith K` as well. -/
Mathlib/Topology/MetricSpace/Contracting.lean
68
76
/- Copyright (c) 2022 Michael Stoll. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Michael Stoll, Thomas Zhu, Mario Carneiro -/ import Mathlib.NumberTheory.LegendreSymbol.QuadraticReciprocity /-! # The Jacobi Symbol We define the Jacobi symbol and prove its main properties. ## Main definitions We define the Jacobi symbol, `jacobiSym a b`, for integers `a` and natural numbers `b` as the product over the prime factors `p` of `b` of the Legendre symbols `legendreSym p a`. This agrees with the mathematical definition when `b` is odd. The prime factors are obtained via `Nat.factors`. Since `Nat.factors 0 = []`, this implies in particular that `jacobiSym a 0 = 1` for all `a`. ## Main statements We prove the main properties of the Jacobi symbol, including the following. * Multiplicativity in both arguments (`jacobiSym.mul_left`, `jacobiSym.mul_right`) * The value of the symbol is `1` or `-1` when the arguments are coprime (`jacobiSym.eq_one_or_neg_one`) * The symbol vanishes if and only if `b ≠ 0` and the arguments are not coprime (`jacobiSym.eq_zero_iff_not_coprime`) * If the symbol has the value `-1`, then `a : ZMod b` is not a square (`ZMod.nonsquare_of_jacobiSym_eq_neg_one`); the converse holds when `b = p` is a prime (`ZMod.nonsquare_iff_jacobiSym_eq_neg_one`); in particular, in this case `a` is a square mod `p` when the symbol has the value `1` (`ZMod.isSquare_of_jacobiSym_eq_one`). * Quadratic reciprocity (`jacobiSym.quadratic_reciprocity`, `jacobiSym.quadratic_reciprocity_one_mod_four`, `jacobiSym.quadratic_reciprocity_three_mod_four`) * The supplementary laws for `a = -1`, `a = 2`, `a = -2` (`jacobiSym.at_neg_one`, `jacobiSym.at_two`, `jacobiSym.at_neg_two`) * The symbol depends on `a` only via its residue class mod `b` (`jacobiSym.mod_left`) and on `b` only via its residue class mod `4*a` (`jacobiSym.mod_right`) * A `csimp` rule for `jacobiSym` and `legendreSym` that evaluates `J(a | b)` efficiently by reducing to the case `0 ≤ a < b` and `a`, `b` odd, and then swaps `a`, `b` and recurses using quadratic reciprocity. ## Notations We define the notation `J(a | b)` for `jacobiSym a b`, localized to `NumberTheorySymbols`. ## Tags Jacobi symbol, quadratic reciprocity -/ section Jacobi /-! ### Definition of the Jacobi symbol We define the Jacobi symbol $\Bigl(\frac{a}{b}\Bigr)$ for integers `a` and natural numbers `b` as the product of the Legendre symbols $\Bigl(\frac{a}{p}\Bigr)$, where `p` runs through the prime divisors (with multiplicity) of `b`, as provided by `b.factors`. This agrees with the Jacobi symbol when `b` is odd and gives less meaningful values when it is not (e.g., the symbol is `1` when `b = 0`). This is called `jacobiSym a b`. We define localized notation (locale `NumberTheorySymbols`) `J(a | b)` for the Jacobi symbol `jacobiSym a b`. -/ open Nat ZMod -- Since we need the fact that the factors are prime, we use `List.pmap`. /-- The Jacobi symbol of `a` and `b` -/ def jacobiSym (a : ℤ) (b : ℕ) : ℤ := (b.primeFactorsList.pmap (fun p pp => @legendreSym p ⟨pp⟩ a) fun _ pf => prime_of_mem_primeFactorsList pf).prod -- Notation for the Jacobi symbol. @[inherit_doc] scoped[NumberTheorySymbols] notation "J(" a " | " b ")" => jacobiSym a b open NumberTheorySymbols /-! ### Properties of the Jacobi symbol -/ namespace jacobiSym /-- The symbol `J(a | 0)` has the value `1`. -/ @[simp] theorem zero_right (a : ℤ) : J(a | 0) = 1 := by simp only [jacobiSym, primeFactorsList_zero, List.prod_nil, List.pmap] /-- The symbol `J(a | 1)` has the value `1`. -/ @[simp] theorem one_right (a : ℤ) : J(a | 1) = 1 := by simp only [jacobiSym, primeFactorsList_one, List.prod_nil, List.pmap] /-- The Legendre symbol `legendreSym p a` with an integer `a` and a prime number `p` is the same as the Jacobi symbol `J(a | p)`. -/ theorem legendreSym.to_jacobiSym (p : ℕ) [fp : Fact p.Prime] (a : ℤ) : legendreSym p a = J(a | p) := by simp only [jacobiSym, primeFactorsList_prime fp.1, List.prod_cons, List.prod_nil, mul_one, List.pmap] /-- The Jacobi symbol is multiplicative in its second argument. -/ theorem mul_right' (a : ℤ) {b₁ b₂ : ℕ} (hb₁ : b₁ ≠ 0) (hb₂ : b₂ ≠ 0) : J(a | b₁ * b₂) = J(a | b₁) * J(a | b₂) := by rw [jacobiSym, ((perm_primeFactorsList_mul hb₁ hb₂).pmap _).prod_eq, List.pmap_append, List.prod_append] pick_goal 2 · exact fun p hp => (List.mem_append.mp hp).elim prime_of_mem_primeFactorsList prime_of_mem_primeFactorsList · rfl /-- The Jacobi symbol is multiplicative in its second argument. -/ theorem mul_right (a : ℤ) (b₁ b₂ : ℕ) [NeZero b₁] [NeZero b₂] : J(a | b₁ * b₂) = J(a | b₁) * J(a | b₂) := mul_right' a (NeZero.ne b₁) (NeZero.ne b₂) /-- The Jacobi symbol takes only the values `0`, `1` and `-1`. -/ theorem trichotomy (a : ℤ) (b : ℕ) : J(a | b) = 0 ∨ J(a | b) = 1 ∨ J(a | b) = -1 := ((MonoidHom.mrange (@SignType.castHom ℤ _ _).toMonoidHom).copy {0, 1, -1} <| by rw [Set.pair_comm] exact (SignType.range_eq SignType.castHom).symm).list_prod_mem (by intro _ ha' rcases List.mem_pmap.mp ha' with ⟨p, hp, rfl⟩ haveI : Fact p.Prime := ⟨prime_of_mem_primeFactorsList hp⟩ exact quadraticChar_isQuadratic (ZMod p) a) /-- The symbol `J(1 | b)` has the value `1`. -/ @[simp] theorem one_left (b : ℕ) : J(1 | b) = 1 := List.prod_eq_one fun z hz => by let ⟨p, hp, he⟩ := List.mem_pmap.1 hz rw [← he, legendreSym.at_one] /-- The Jacobi symbol is multiplicative in its first argument. -/ theorem mul_left (a₁ a₂ : ℤ) (b : ℕ) : J(a₁ * a₂ | b) = J(a₁ | b) * J(a₂ | b) := by simp_rw [jacobiSym, List.pmap_eq_map_attach, legendreSym.mul _ _ _] exact List.prod_map_mul (α := ℤ) (l := (primeFactorsList b).attach) (f := fun x ↦ @legendreSym x {out := prime_of_mem_primeFactorsList x.2} a₁) (g := fun x ↦ @legendreSym x {out := prime_of_mem_primeFactorsList x.2} a₂) /-- The symbol `J(a | b)` vanishes iff `a` and `b` are not coprime (assuming `b ≠ 0`). -/ theorem eq_zero_iff_not_coprime {a : ℤ} {b : ℕ} [NeZero b] : J(a | b) = 0 ↔ a.gcd b ≠ 1 := List.prod_eq_zero_iff.trans (by rw [List.mem_pmap, Int.gcd_eq_natAbs, Ne, Prime.not_coprime_iff_dvd] simp_rw [legendreSym.eq_zero_iff _ _, intCast_zmod_eq_zero_iff_dvd, mem_primeFactorsList (NeZero.ne b), ← Int.natCast_dvd, Int.natCast_dvd_natCast, exists_prop, and_assoc, _root_.and_comm]) /-- The symbol `J(a | b)` is nonzero when `a` and `b` are coprime. -/ protected theorem ne_zero {a : ℤ} {b : ℕ} (h : a.gcd b = 1) : J(a | b) ≠ 0 := by rcases eq_zero_or_neZero b with hb | _ · rw [hb, zero_right] exact one_ne_zero · contrapose! h; exact eq_zero_iff_not_coprime.1 h /-- The symbol `J(a | b)` vanishes if and only if `b ≠ 0` and `a` and `b` are not coprime. -/ theorem eq_zero_iff {a : ℤ} {b : ℕ} : J(a | b) = 0 ↔ b ≠ 0 ∧ a.gcd b ≠ 1 := ⟨fun h => by rcases eq_or_ne b 0 with hb | hb · rw [hb, zero_right] at h; cases h exact ⟨hb, mt jacobiSym.ne_zero <| Classical.not_not.2 h⟩, fun ⟨hb, h⟩ => by rw [← neZero_iff] at hb; exact eq_zero_iff_not_coprime.2 h⟩ /-- The symbol `J(0 | b)` vanishes when `b > 1`. -/ theorem zero_left {b : ℕ} (hb : 1 < b) : J(0 | b) = 0 := (@eq_zero_iff_not_coprime 0 b ⟨ne_zero_of_lt hb⟩).mpr <| by rw [Int.gcd_zero_left, Int.natAbs_natCast]; exact hb.ne' /-- The symbol `J(a | b)` takes the value `1` or `-1` if `a` and `b` are coprime. -/ theorem eq_one_or_neg_one {a : ℤ} {b : ℕ} (h : a.gcd b = 1) : J(a | b) = 1 ∨ J(a | b) = -1 := (trichotomy a b).resolve_left <| jacobiSym.ne_zero h /-- We have that `J(a^e | b) = J(a | b)^e`. -/ theorem pow_left (a : ℤ) (e b : ℕ) : J(a ^ e | b) = J(a | b) ^ e := Nat.recOn e (by rw [_root_.pow_zero, _root_.pow_zero, one_left]) fun _ ih => by rw [_root_.pow_succ, _root_.pow_succ, mul_left, ih] /-- We have that `J(a | b^e) = J(a | b)^e`. -/ theorem pow_right (a : ℤ) (b e : ℕ) : J(a | b ^ e) = J(a | b) ^ e := by induction e with | zero => rw [Nat.pow_zero, _root_.pow_zero, one_right] | succ e ih => rcases eq_zero_or_neZero b with hb | _ · rw [hb, zero_pow e.succ_ne_zero, zero_right, one_pow] · rw [_root_.pow_succ, _root_.pow_succ, mul_right, ih] /-- The square of `J(a | b)` is `1` when `a` and `b` are coprime. -/ theorem sq_one {a : ℤ} {b : ℕ} (h : a.gcd b = 1) : J(a | b) ^ 2 = 1 := by rcases eq_one_or_neg_one h with h₁ | h₁ <;> rw [h₁] <;> rfl /-- The symbol `J(a^2 | b)` is `1` when `a` and `b` are coprime. -/ theorem sq_one' {a : ℤ} {b : ℕ} (h : a.gcd b = 1) : J(a ^ 2 | b) = 1 := by rw [pow_left, sq_one h] /-- The symbol `J(a | b)` depends only on `a` mod `b`. -/ theorem mod_left (a : ℤ) (b : ℕ) : J(a | b) = J(a % b | b) := congr_arg List.prod <| List.pmap_congr_left _ (by rintro p hp _ h₂ conv_rhs => rw [legendreSym.mod, Int.emod_emod_of_dvd _ (Int.natCast_dvd_natCast.2 <| dvd_of_mem_primeFactorsList hp), ← legendreSym.mod]) /-- The symbol `J(a | b)` depends only on `a` mod `b`. -/ theorem mod_left' {a₁ a₂ : ℤ} {b : ℕ} (h : a₁ % b = a₂ % b) : J(a₁ | b) = J(a₂ | b) := by rw [mod_left, h, ← mod_left] /-- If `p` is prime, `J(a | p) = -1` and `p` divides `x^2 - a*y^2`, then `p` must divide `x` and `y`. -/ theorem prime_dvd_of_eq_neg_one {p : ℕ} [Fact p.Prime] {a : ℤ} (h : J(a | p) = -1) {x y : ℤ} (hxy : ↑p ∣ (x ^ 2 - a * y ^ 2 : ℤ)) : ↑p ∣ x ∧ ↑p ∣ y := by rw [← legendreSym.to_jacobiSym] at h exact legendreSym.prime_dvd_of_eq_neg_one h hxy /-- We can pull out a product over a list in the first argument of the Jacobi symbol. -/ theorem list_prod_left {l : List ℤ} {n : ℕ} : J(l.prod | n) = (l.map fun a => J(a | n)).prod := by induction l with | nil => simp only [List.prod_nil, List.map_nil, one_left] | cons n l' ih => rw [List.map, List.prod_cons, List.prod_cons, mul_left, ih] /-- We can pull out a product over a list in the second argument of the Jacobi symbol. -/ theorem list_prod_right {a : ℤ} {l : List ℕ} (hl : ∀ n ∈ l, n ≠ 0) : J(a | l.prod) = (l.map fun n => J(a | n)).prod := by induction l with | nil => simp only [List.prod_nil, one_right, List.map_nil] | cons n l' ih => have hn := hl n List.mem_cons_self -- `n ≠ 0` have hl' := List.prod_ne_zero fun hf => hl 0 (List.mem_cons_of_mem _ hf) rfl -- `l'.prod ≠ 0` have h := fun m hm => hl m (List.mem_cons_of_mem _ hm) -- `∀ (m : ℕ), m ∈ l' → m ≠ 0` rw [List.map, List.prod_cons, List.prod_cons, mul_right' a hn hl', ih h] /-- If `J(a | n) = -1`, then `n` has a prime divisor `p` such that `J(a | p) = -1`. -/ theorem eq_neg_one_at_prime_divisor_of_eq_neg_one {a : ℤ} {n : ℕ} (h : J(a | n) = -1) : ∃ p : ℕ, p.Prime ∧ p ∣ n ∧ J(a | p) = -1 := by have hn₀ : n ≠ 0 := by rintro rfl rw [zero_right, CharZero.eq_neg_self_iff] at h exact one_ne_zero h have hf₀ (p) (hp : p ∈ n.primeFactorsList) : p ≠ 0 := (Nat.pos_of_mem_primeFactorsList hp).ne.symm rw [← Nat.prod_primeFactorsList hn₀, list_prod_right hf₀] at h obtain ⟨p, hmem, hj⟩ := List.mem_map.mp (List.neg_one_mem_of_prod_eq_neg_one h) exact ⟨p, Nat.prime_of_mem_primeFactorsList hmem, Nat.dvd_of_mem_primeFactorsList hmem, hj⟩ end jacobiSym namespace ZMod open jacobiSym /-- If `J(a | b)` is `-1`, then `a` is not a square modulo `b`. -/ theorem nonsquare_of_jacobiSym_eq_neg_one {a : ℤ} {b : ℕ} (h : J(a | b) = -1) : ¬IsSquare (a : ZMod b) := fun ⟨r, ha⟩ => by rw [← r.coe_valMinAbs, ← Int.cast_mul, intCast_eq_intCast_iff', ← sq] at ha apply (by norm_num : ¬(0 : ℤ) ≤ -1) rw [← h, mod_left, ha, ← mod_left, pow_left] apply sq_nonneg /-- If `p` is prime, then `J(a | p)` is `-1` iff `a` is not a square modulo `p`. -/ theorem nonsquare_iff_jacobiSym_eq_neg_one {a : ℤ} {p : ℕ} [Fact p.Prime] : J(a | p) = -1 ↔ ¬IsSquare (a : ZMod p) := by rw [← legendreSym.to_jacobiSym] exact legendreSym.eq_neg_one_iff p /-- If `p` is prime and `J(a | p) = 1`, then `a` is a square mod `p`. -/ theorem isSquare_of_jacobiSym_eq_one {a : ℤ} {p : ℕ} [Fact p.Prime] (h : J(a | p) = 1) : IsSquare (a : ZMod p) := Classical.not_not.mp <| by rw [← nonsquare_iff_jacobiSym_eq_neg_one, h]; decide end ZMod /-! ### Values at `-1`, `2` and `-2` -/ namespace jacobiSym /-- If `χ` is a multiplicative function such that `J(a | p) = χ p` for all odd primes `p`, then `J(a | b)` equals `χ b` for all odd natural numbers `b`. -/ theorem value_at (a : ℤ) {R : Type*} [Semiring R] (χ : R →* ℤ) (hp : ∀ (p : ℕ) (pp : p.Prime), p ≠ 2 → @legendreSym p ⟨pp⟩ a = χ p) {b : ℕ} (hb : Odd b) : J(a | b) = χ b := by conv_rhs => rw [← prod_primeFactorsList hb.pos.ne', cast_list_prod, map_list_prod χ] rw [jacobiSym, List.map_map, ← List.pmap_eq_map fun _ => prime_of_mem_primeFactorsList] congr 1; apply List.pmap_congr_left exact fun p h pp _ => hp p pp (hb.ne_two_of_dvd_nat <| dvd_of_mem_primeFactorsList h) /-- If `b` is odd, then `J(-1 | b)` is given by `χ₄ b`. -/ theorem at_neg_one {b : ℕ} (hb : Odd b) : J(-1 | b) = χ₄ b := -- Porting note: In mathlib3, it was written `χ₄` and Lean could guess that it had to use -- `χ₄.to_monoid_hom`. This is not the case with Lean 4. value_at (-1) χ₄.toMonoidHom (fun p pp => @legendreSym.at_neg_one p ⟨pp⟩) hb /-- If `b` is odd, then `J(-a | b) = χ₄ b * J(a | b)`. -/ protected theorem neg (a : ℤ) {b : ℕ} (hb : Odd b) : J(-a | b) = χ₄ b * J(a | b) := by rw [neg_eq_neg_one_mul, mul_left, at_neg_one hb] /-- If `b` is odd, then `J(2 | b)` is given by `χ₈ b`. -/ theorem at_two {b : ℕ} (hb : Odd b) : J(2 | b) = χ₈ b := value_at 2 χ₈.toMonoidHom (fun p pp => @legendreSym.at_two p ⟨pp⟩) hb /-- If `b` is odd, then `J(-2 | b)` is given by `χ₈' b`. -/ theorem at_neg_two {b : ℕ} (hb : Odd b) : J(-2 | b) = χ₈' b := value_at (-2) χ₈'.toMonoidHom (fun p pp => @legendreSym.at_neg_two p ⟨pp⟩) hb theorem div_four_left {a : ℤ} {b : ℕ} (ha4 : a % 4 = 0) (hb2 : b % 2 = 1) : J(a / 4 | b) = J(a | b) := by obtain ⟨a, rfl⟩ := Int.dvd_of_emod_eq_zero ha4 have : Int.gcd (2 : ℕ) b = 1 := by rw [Int.gcd_natCast_natCast, ← b.mod_add_div 2, hb2, Nat.gcd_add_mul_left_right, Nat.gcd_one_right] rw [Int.mul_ediv_cancel_left _ (by decide), jacobiSym.mul_left, (by decide : (4 : ℤ) = (2 : ℕ) ^ 2), jacobiSym.sq_one' this, one_mul] theorem even_odd {a : ℤ} {b : ℕ} (ha2 : a % 2 = 0) (hb2 : b % 2 = 1) : (if b % 8 = 3 ∨ b % 8 = 5 then -J(a / 2 | b) else J(a / 2 | b)) = J(a | b) := by obtain ⟨a, rfl⟩ := Int.dvd_of_emod_eq_zero ha2 rw [Int.mul_ediv_cancel_left _ (by decide), jacobiSym.mul_left, jacobiSym.at_two (Nat.odd_iff.mpr hb2), ZMod.χ₈_nat_eq_if_mod_eight, if_neg (Nat.mod_two_ne_zero.mpr hb2)] have := Nat.mod_lt b (by decide : 0 < 8) interval_cases h : b % 8 <;> simp_all <;> · have := hb2 ▸ h ▸ Nat.mod_mod_of_dvd b (by decide : 2 ∣ 8) simp_all end jacobiSym /-! ### Quadratic Reciprocity -/ /-- The bi-multiplicative map giving the sign in the Law of Quadratic Reciprocity -/ def qrSign (m n : ℕ) : ℤ := J(χ₄ m | n) namespace qrSign /-- We can express `qrSign m n` as a power of `-1` when `m` and `n` are odd. -/ theorem neg_one_pow {m n : ℕ} (hm : Odd m) (hn : Odd n) : qrSign m n = (-1) ^ (m / 2 * (n / 2)) := by rw [qrSign, pow_mul, ← χ₄_eq_neg_one_pow (odd_iff.mp hm)] rcases odd_mod_four_iff.mp (odd_iff.mp hm) with h | h · rw [χ₄_nat_one_mod_four h, jacobiSym.one_left, one_pow] · rw [χ₄_nat_three_mod_four h, ← χ₄_eq_neg_one_pow (odd_iff.mp hn), jacobiSym.at_neg_one hn] /-- When `m` and `n` are odd, then the square of `qrSign m n` is `1`. -/ theorem sq_eq_one {m n : ℕ} (hm : Odd m) (hn : Odd n) : qrSign m n ^ 2 = 1 := by rw [neg_one_pow hm hn, ← pow_mul, mul_comm, pow_mul, neg_one_sq, one_pow] /-- `qrSign` is multiplicative in the first argument. -/ theorem mul_left (m₁ m₂ n : ℕ) : qrSign (m₁ * m₂) n = qrSign m₁ n * qrSign m₂ n := by simp_rw [qrSign, Nat.cast_mul, map_mul, jacobiSym.mul_left] /-- `qrSign` is multiplicative in the second argument. -/ theorem mul_right (m n₁ n₂ : ℕ) [NeZero n₁] [NeZero n₂] : qrSign m (n₁ * n₂) = qrSign m n₁ * qrSign m n₂ := jacobiSym.mul_right (χ₄ m) n₁ n₂ /-- `qrSign` is symmetric when both arguments are odd. -/ protected theorem symm {m n : ℕ} (hm : Odd m) (hn : Odd n) : qrSign m n = qrSign n m := by rw [neg_one_pow hm hn, neg_one_pow hn hm, mul_comm (m / 2)] /-- We can move `qrSign m n` from one side of an equality to the other when `m` and `n` are odd. -/ theorem eq_iff_eq {m n : ℕ} (hm : Odd m) (hn : Odd n) (x y : ℤ) : qrSign m n * x = y ↔ x = qrSign m n * y := by refine ⟨fun h' => let h := h'.symm ?_, fun h => ?_⟩ <;> rw [h, ← mul_assoc, ← pow_two, sq_eq_one hm hn, one_mul] end qrSign namespace jacobiSym /-- The **Law of Quadratic Reciprocity for the Jacobi symbol**, version with `qrSign` -/ theorem quadratic_reciprocity' {a b : ℕ} (ha : Odd a) (hb : Odd b) : J(a | b) = qrSign b a * J(b | a) := by -- define the right hand side for fixed `a` as a `ℕ →* ℤ` let rhs : ℕ → ℕ →* ℤ := fun a => { toFun := fun x => qrSign x a * J(x | a) map_one' := by convert ← mul_one (M := ℤ) _; (on_goal 1 => symm); all_goals apply one_left map_mul' := fun x y => by simp_rw [qrSign.mul_left x y a, Nat.cast_mul, mul_left, mul_mul_mul_comm] } have rhs_apply : ∀ a b : ℕ, rhs a b = qrSign b a * J(b | a) := fun a b => rfl refine value_at a (rhs a) (fun p pp hp => Eq.symm ?_) hb have hpo := pp.eq_two_or_odd'.resolve_left hp rw [@legendreSym.to_jacobiSym p ⟨pp⟩, rhs_apply, Nat.cast_id, qrSign.eq_iff_eq hpo ha, qrSign.symm hpo ha] refine value_at p (rhs p) (fun q pq hq => ?_) ha have hqo := pq.eq_two_or_odd'.resolve_left hq rw [rhs_apply, Nat.cast_id, ← @legendreSym.to_jacobiSym p ⟨pp⟩, qrSign.symm hqo hpo, qrSign.neg_one_pow hpo hqo, @legendreSym.quadratic_reciprocity' p q ⟨pp⟩ ⟨pq⟩ hp hq] /-- The Law of Quadratic Reciprocity for the Jacobi symbol -/ theorem quadratic_reciprocity {a b : ℕ} (ha : Odd a) (hb : Odd b) : J(a | b) = (-1) ^ (a / 2 * (b / 2)) * J(b | a) := by rw [← qrSign.neg_one_pow ha hb, qrSign.symm ha hb, quadratic_reciprocity' ha hb] /-- The Law of Quadratic Reciprocity for the Jacobi symbol: if `a` and `b` are natural numbers with `a % 4 = 1` and `b` odd, then `J(a | b) = J(b | a)`. -/ theorem quadratic_reciprocity_one_mod_four {a b : ℕ} (ha : a % 4 = 1) (hb : Odd b) : J(a | b) = J(b | a) := by rw [quadratic_reciprocity (odd_iff.mpr (odd_of_mod_four_eq_one ha)) hb, pow_mul, neg_one_pow_div_two_of_one_mod_four ha, one_pow, one_mul] /-- The Law of Quadratic Reciprocity for the Jacobi symbol: if `a` and `b` are natural numbers with `a` odd and `b % 4 = 1`, then `J(a | b) = J(b | a)`. -/ theorem quadratic_reciprocity_one_mod_four' {a b : ℕ} (ha : Odd a) (hb : b % 4 = 1) : J(a | b) = J(b | a) := (quadratic_reciprocity_one_mod_four hb ha).symm /-- The Law of Quadratic Reciprocity for the Jacobi symbol: if `a` and `b` are natural numbers both congruent to `3` mod `4`, then `J(a | b) = -J(b | a)`. -/ theorem quadratic_reciprocity_three_mod_four {a b : ℕ} (ha : a % 4 = 3) (hb : b % 4 = 3) : J(a | b) = -J(b | a) := by let nop := @neg_one_pow_div_two_of_three_mod_four rw [quadratic_reciprocity, pow_mul, nop ha, nop hb, neg_one_mul] <;> rwa [odd_iff, odd_of_mod_four_eq_three] theorem quadratic_reciprocity_if {a b : ℕ} (ha2 : a % 2 = 1) (hb2 : b % 2 = 1) : (if a % 4 = 3 ∧ b % 4 = 3 then -J(b | a) else J(b | a)) = J(a | b) := by rcases Nat.odd_mod_four_iff.mp ha2 with ha1 | ha3 · simpa [ha1] using jacobiSym.quadratic_reciprocity_one_mod_four' (Nat.odd_iff.mpr hb2) ha1 rcases Nat.odd_mod_four_iff.mp hb2 with hb1 | hb3 · simpa [hb1] using jacobiSym.quadratic_reciprocity_one_mod_four hb1 (Nat.odd_iff.mpr ha2) simpa [ha3, hb3] using (jacobiSym.quadratic_reciprocity_three_mod_four ha3 hb3).symm /-- The Jacobi symbol `J(a | b)` depends only on `b` mod `4*a` (version for `a : ℕ`). -/ theorem mod_right' (a : ℕ) {b : ℕ} (hb : Odd b) : J(a | b) = J(a | b % (4 * a)) := by rcases eq_or_ne a 0 with (rfl | ha₀) · rw [mul_zero, mod_zero] have hb' : Odd (b % (4 * a)) := hb.mod_even (Even.mul_right (by decide) _) rcases exists_eq_pow_mul_and_not_dvd ha₀ 2 (by norm_num) with ⟨e, a', ha₁', ha₂⟩ have ha₁ := odd_iff.mpr (two_dvd_ne_zero.mp ha₁') nth_rw 2 [ha₂]; nth_rw 1 [ha₂] rw [Nat.cast_mul, mul_left, mul_left, quadratic_reciprocity' ha₁ hb, quadratic_reciprocity' ha₁ hb', Nat.cast_pow, pow_left, pow_left, Nat.cast_two, at_two hb, at_two hb'] congr 1; swap · congr 1 · simp_rw [qrSign] rw [χ₄_nat_mod_four, χ₄_nat_mod_four (b % (4 * a)), mod_mod_of_dvd b (dvd_mul_right 4 a)] · rw [mod_left ↑(b % _), mod_left b, Int.natCast_mod, Int.emod_emod_of_dvd b] simp only [ha₂, Nat.cast_mul, ← mul_assoc] apply dvd_mul_left rcases e with - | e; · rfl · rw [χ₈_nat_mod_eight, χ₈_nat_mod_eight (b % (4 * a)), mod_mod_of_dvd b] use 2 ^ e * a'; rw [ha₂, Nat.pow_succ]; ring /-- The Jacobi symbol `J(a | b)` depends only on `b` mod `4*a`. -/ theorem mod_right (a : ℤ) {b : ℕ} (hb : Odd b) : J(a | b) = J(a | b % (4 * a.natAbs)) := by rcases Int.natAbs_eq a with ha | ha <;> nth_rw 2 [ha] <;> nth_rw 1 [ha] · exact mod_right' a.natAbs hb · have hb' : Odd (b % (4 * a.natAbs)) := hb.mod_even (Even.mul_right (by decide) _) rw [jacobiSym.neg _ hb, jacobiSym.neg _ hb', mod_right' _ hb, χ₄_nat_mod_four, χ₄_nat_mod_four (b % (4 * _)), mod_mod_of_dvd b (dvd_mul_right 4 _)] end jacobiSym end Jacobi section FastJacobi /-! ### Fast computation of the Jacobi symbol We follow the implementation as in `Mathlib.Tactic.NormNum.LegendreSymbol`. -/ open NumberTheorySymbols jacobiSym /-- Computes `J(a | b)` (or `-J(a | b)` if `flip` is set to `true`) given assumptions, by reducing `a` to odd by repeated division and then using quadratic reciprocity to swap `a`, `b`. -/ private def fastJacobiSymAux (a b : ℕ) (flip : Bool) (ha0 : a > 0) : ℤ := if ha4 : a % 4 = 0 then fastJacobiSymAux (a / 4) b flip (Nat.div_pos (Nat.le_of_dvd ha0 (Nat.dvd_of_mod_eq_zero ha4)) (by decide)) else if ha2 : a % 2 = 0 then fastJacobiSymAux (a / 2) b (xor (b % 8 = 3 ∨ b % 8 = 5) flip) (Nat.div_pos (Nat.le_of_dvd ha0 (Nat.dvd_of_mod_eq_zero ha2)) (by decide)) else if ha1 : a = 1 then if flip then -1 else 1 else if hba : b % a = 0 then 0 else fastJacobiSymAux (b % a) a (xor (a % 4 = 3 ∧ b % 4 = 3) flip) (Nat.pos_of_ne_zero hba) termination_by a decreasing_by · exact a.div_lt_self ha0 (by decide) · exact a.div_lt_self ha0 (by decide) · exact b.mod_lt ha0 private theorem fastJacobiSymAux.eq_jacobiSym {a b : ℕ} {flip : Bool} {ha0 : a > 0} (hb2 : b % 2 = 1) (hb1 : b > 1) : fastJacobiSymAux a b flip ha0 = if flip then -J(a | b) else J(a | b) := by induction a using Nat.strongRecOn generalizing b flip with | ind a IH => unfold fastJacobiSymAux split <;> rename_i ha4 · rw [IH (a / 4) (a.div_lt_self ha0 (by decide)) hb2 hb1] simp only [Int.natCast_ediv, Nat.cast_ofNat, div_four_left (a := a) (mod_cast ha4) hb2] split <;> rename_i ha2 · rw [IH (a / 2) (a.div_lt_self ha0 (by decide)) hb2 hb1] simp only [Int.natCast_ediv, Nat.cast_ofNat, ← even_odd (a := a) (mod_cast ha2) hb2] by_cases h : b % 8 = 3 ∨ b % 8 = 5 <;> simp [h]; cases flip <;> simp split <;> rename_i ha1 · subst ha1; simp split <;> rename_i hba · suffices J(a | b) = 0 by simp [this] refine eq_zero_iff.mpr ⟨fun h ↦ absurd (h ▸ hb1) (by decide), ?_⟩ rwa [Int.gcd_natCast_natCast, Nat.gcd_eq_left (Nat.dvd_of_mod_eq_zero hba)] rw [IH (b % a) (b.mod_lt ha0) (Nat.mod_two_ne_zero.mp ha2) (lt_of_le_of_ne ha0 (Ne.symm ha1))] simp only [Int.natCast_mod, ← mod_left] rw [← quadratic_reciprocity_if (Nat.mod_two_ne_zero.mp ha2) hb2] by_cases h : a % 4 = 3 ∧ b % 4 = 3 <;> simp [h]; cases flip <;> simp /-- Computes `J(a | b)` by reducing `b` to odd by repeated division and then using `fastJacobiSymAux`. -/ private def fastJacobiSym (a : ℤ) (b : ℕ) : ℤ := if hb0 : b = 0 then 1 else if _ : b % 2 = 0 then if a % 2 = 0 then 0 else have : b / 2 < b := b.div_lt_self (Nat.pos_of_ne_zero hb0) one_lt_two fastJacobiSym a (b / 2) else if b = 1 then 1 else if hab : a % b = 0 then 0 else fastJacobiSymAux (a % b).natAbs b false (Int.natAbs_pos.mpr hab) @[csimp] private theorem fastJacobiSym.eq : jacobiSym = fastJacobiSym := by ext a b induction b using Nat.strongRecOn with | ind b IH => unfold fastJacobiSym split_ifs with hb0 hb2 ha2 hb1 hab · rw [hb0, zero_right] · refine eq_zero_iff.mpr ⟨hb0, ne_of_gt ?_⟩ refine Nat.le_of_dvd (Int.gcd_pos_iff.mpr (mod_cast .inr hb0)) ?_ refine Nat.dvd_gcd (Int.ofNat_dvd_left.mp (Int.dvd_of_emod_eq_zero ha2)) ?_ exact Int.ofNat_dvd_left.mp (Int.dvd_of_emod_eq_zero (mod_cast hb2)) · dsimp only rw [← IH (b / 2) (b.div_lt_self (Nat.pos_of_ne_zero hb0) one_lt_two)] obtain ⟨b, rfl⟩ := Nat.dvd_of_mod_eq_zero hb2 rw [mul_right' a (by decide) fun h ↦ hb0 (mul_eq_zero_of_right 2 h), b.mul_div_cancel_left (by decide), mod_left a 2, Nat.cast_ofNat, Int.emod_two_ne_zero.mp ha2, one_left, one_mul] · rw [hb1, one_right] · rw [mod_left, hab, zero_left (lt_of_le_of_ne (Nat.pos_of_ne_zero hb0) (Ne.symm hb1))] · rw [fastJacobiSymAux.eq_jacobiSym, if_neg Bool.false_ne_true, mod_left a b, Int.natAbs_of_nonneg (a.emod_nonneg (mod_cast hb0))] · exact Nat.mod_two_ne_zero.mp hb2 · exact lt_of_le_of_ne (Nat.one_le_iff_ne_zero.mpr hb0) (Ne.symm hb1) /-- Computes `legendreSym p a` using `fastJacobiSym`. -/ @[inline, nolint unusedArguments] private def fastLegendreSym (p : ℕ) [Fact p.Prime] (a : ℤ) : ℤ := J(a | p) @[csimp] private theorem fastLegendreSym.eq : legendreSym = fastLegendreSym := by ext p _ a; rw [legendreSym.to_jacobiSym, fastLegendreSym] end FastJacobi
Mathlib/NumberTheory/LegendreSymbol/JacobiSymbol.lean
639
640
/- Copyright (c) 2022 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import Mathlib.Order.PropInstances import Mathlib.Order.GaloisConnection.Defs /-! # Heyting algebras This file defines Heyting, co-Heyting and bi-Heyting algebras. A Heyting algebra is a bounded distributive lattice with an implication operation `⇨` such that `a ≤ b ⇨ c ↔ a ⊓ b ≤ c`. It also comes with a pseudo-complement `ᶜ`, such that `aᶜ = a ⇨ ⊥`. Co-Heyting algebras are dual to Heyting algebras. They have a difference `\` and a negation `¬` such that `a \ b ≤ c ↔ a ≤ b ⊔ c` and `¬a = ⊤ \ a`. Bi-Heyting algebras are Heyting algebras that are also co-Heyting algebras. From a logic standpoint, Heyting algebras precisely model intuitionistic logic, whereas boolean algebras model classical logic. Heyting algebras are the order theoretic equivalent of cartesian-closed categories. ## Main declarations * `GeneralizedHeytingAlgebra`: Heyting algebra without a top element (nor negation). * `GeneralizedCoheytingAlgebra`: Co-Heyting algebra without a bottom element (nor complement). * `HeytingAlgebra`: Heyting algebra. * `CoheytingAlgebra`: Co-Heyting algebra. * `BiheytingAlgebra`: bi-Heyting algebra. ## References * [Francis Borceux, *Handbook of Categorical Algebra III*][borceux-vol3] ## Tags Heyting, Brouwer, algebra, implication, negation, intuitionistic -/ assert_not_exists RelIso open Function OrderDual universe u variable {ι α β : Type*} /-! ### Notation -/ section variable (α β) instance Prod.instHImp [HImp α] [HImp β] : HImp (α × β) := ⟨fun a b => (a.1 ⇨ b.1, a.2 ⇨ b.2)⟩ instance Prod.instHNot [HNot α] [HNot β] : HNot (α × β) := ⟨fun a => (¬a.1, ¬a.2)⟩ instance Prod.instSDiff [SDiff α] [SDiff β] : SDiff (α × β) := ⟨fun a b => (a.1 \ b.1, a.2 \ b.2)⟩ instance Prod.instHasCompl [HasCompl α] [HasCompl β] : HasCompl (α × β) := ⟨fun a => (a.1ᶜ, a.2ᶜ)⟩ end @[simp] theorem fst_himp [HImp α] [HImp β] (a b : α × β) : (a ⇨ b).1 = a.1 ⇨ b.1 := rfl @[simp] theorem snd_himp [HImp α] [HImp β] (a b : α × β) : (a ⇨ b).2 = a.2 ⇨ b.2 := rfl @[simp] theorem fst_hnot [HNot α] [HNot β] (a : α × β) : (¬a).1 = ¬a.1 := rfl @[simp] theorem snd_hnot [HNot α] [HNot β] (a : α × β) : (¬a).2 = ¬a.2 := rfl @[simp] theorem fst_sdiff [SDiff α] [SDiff β] (a b : α × β) : (a \ b).1 = a.1 \ b.1 := rfl @[simp] theorem snd_sdiff [SDiff α] [SDiff β] (a b : α × β) : (a \ b).2 = a.2 \ b.2 := rfl @[simp] theorem fst_compl [HasCompl α] [HasCompl β] (a : α × β) : aᶜ.1 = a.1ᶜ := rfl @[simp] theorem snd_compl [HasCompl α] [HasCompl β] (a : α × β) : aᶜ.2 = a.2ᶜ := rfl namespace Pi variable {π : ι → Type*} instance [∀ i, HImp (π i)] : HImp (∀ i, π i) := ⟨fun a b i => a i ⇨ b i⟩ instance [∀ i, HNot (π i)] : HNot (∀ i, π i) := ⟨fun a i => ¬a i⟩ theorem himp_def [∀ i, HImp (π i)] (a b : ∀ i, π i) : a ⇨ b = fun i => a i ⇨ b i := rfl theorem hnot_def [∀ i, HNot (π i)] (a : ∀ i, π i) : ¬a = fun i => ¬a i := rfl @[simp] theorem himp_apply [∀ i, HImp (π i)] (a b : ∀ i, π i) (i : ι) : (a ⇨ b) i = a i ⇨ b i := rfl @[simp] theorem hnot_apply [∀ i, HNot (π i)] (a : ∀ i, π i) (i : ι) : (¬a) i = ¬a i := rfl end Pi /-- A generalized Heyting algebra is a lattice with an additional binary operation `⇨` called Heyting implication such that `(a ⇨ ·)` is right adjoint to `(a ⊓ ·)`. This generalizes `HeytingAlgebra` by not requiring a bottom element. -/ class GeneralizedHeytingAlgebra (α : Type*) extends Lattice α, OrderTop α, HImp α where /-- `(a ⇨ ·)` is right adjoint to `(a ⊓ ·)` -/ le_himp_iff (a b c : α) : a ≤ b ⇨ c ↔ a ⊓ b ≤ c /-- A generalized co-Heyting algebra is a lattice with an additional binary difference operation `\` such that `(· \ a)` is left adjoint to `(· ⊔ a)`. This generalizes `CoheytingAlgebra` by not requiring a top element. -/ class GeneralizedCoheytingAlgebra (α : Type*) extends Lattice α, OrderBot α, SDiff α where /-- `(· \ a)` is left adjoint to `(· ⊔ a)` -/ sdiff_le_iff (a b c : α) : a \ b ≤ c ↔ a ≤ b ⊔ c /-- A Heyting algebra is a bounded lattice with an additional binary operation `⇨` called Heyting implication such that `(a ⇨ ·)` is right adjoint to `(a ⊓ ·)`. -/ class HeytingAlgebra (α : Type*) extends GeneralizedHeytingAlgebra α, OrderBot α, HasCompl α where /-- `aᶜ` is defined as `a ⇨ ⊥` -/ himp_bot (a : α) : a ⇨ ⊥ = aᶜ /-- A co-Heyting algebra is a bounded lattice with an additional binary difference operation `\` such that `(· \ a)` is left adjoint to `(· ⊔ a)`. -/ class CoheytingAlgebra (α : Type*) extends GeneralizedCoheytingAlgebra α, OrderTop α, HNot α where /-- `⊤ \ a` is `¬a` -/ top_sdiff (a : α) : ⊤ \ a = ¬a /-- A bi-Heyting algebra is a Heyting algebra that is also a co-Heyting algebra. -/ class BiheytingAlgebra (α : Type*) extends HeytingAlgebra α, SDiff α, HNot α where /-- `(· \ a)` is left adjoint to `(· ⊔ a)` -/ sdiff_le_iff (a b c : α) : a \ b ≤ c ↔ a ≤ b ⊔ c /-- `⊤ \ a` is `¬a` -/ top_sdiff (a : α) : ⊤ \ a = ¬a -- See note [lower instance priority] attribute [instance 100] GeneralizedHeytingAlgebra.toOrderTop attribute [instance 100] GeneralizedCoheytingAlgebra.toOrderBot -- See note [lower instance priority] instance (priority := 100) HeytingAlgebra.toBoundedOrder [HeytingAlgebra α] : BoundedOrder α := { bot_le := ‹HeytingAlgebra α›.bot_le } -- See note [lower instance priority] instance (priority := 100) CoheytingAlgebra.toBoundedOrder [CoheytingAlgebra α] : BoundedOrder α := { ‹CoheytingAlgebra α› with } -- See note [lower instance priority] instance (priority := 100) BiheytingAlgebra.toCoheytingAlgebra [BiheytingAlgebra α] : CoheytingAlgebra α := { ‹BiheytingAlgebra α› with } -- See note [reducible non-instances] /-- Construct a Heyting algebra from the lattice structure and Heyting implication alone. -/ abbrev HeytingAlgebra.ofHImp [DistribLattice α] [BoundedOrder α] (himp : α → α → α) (le_himp_iff : ∀ a b c, a ≤ himp b c ↔ a ⊓ b ≤ c) : HeytingAlgebra α := { ‹DistribLattice α›, ‹BoundedOrder α› with himp, compl := fun a => himp a ⊥, le_himp_iff, himp_bot := fun _ => rfl } -- See note [reducible non-instances] /-- Construct a Heyting algebra from the lattice structure and complement operator alone. -/ abbrev HeytingAlgebra.ofCompl [DistribLattice α] [BoundedOrder α] (compl : α → α) (le_himp_iff : ∀ a b c, a ≤ compl b ⊔ c ↔ a ⊓ b ≤ c) : HeytingAlgebra α where himp := (compl · ⊔ ·) compl := compl le_himp_iff := le_himp_iff himp_bot _ := sup_bot_eq _ -- See note [reducible non-instances] /-- Construct a co-Heyting algebra from the lattice structure and the difference alone. -/ abbrev CoheytingAlgebra.ofSDiff [DistribLattice α] [BoundedOrder α] (sdiff : α → α → α) (sdiff_le_iff : ∀ a b c, sdiff a b ≤ c ↔ a ≤ b ⊔ c) : CoheytingAlgebra α := { ‹DistribLattice α›, ‹BoundedOrder α› with sdiff, hnot := fun a => sdiff ⊤ a, sdiff_le_iff, top_sdiff := fun _ => rfl } -- See note [reducible non-instances] /-- Construct a co-Heyting algebra from the difference and Heyting negation alone. -/ abbrev CoheytingAlgebra.ofHNot [DistribLattice α] [BoundedOrder α] (hnot : α → α) (sdiff_le_iff : ∀ a b c, a ⊓ hnot b ≤ c ↔ a ≤ b ⊔ c) : CoheytingAlgebra α where sdiff a b := a ⊓ hnot b hnot := hnot sdiff_le_iff := sdiff_le_iff top_sdiff _ := top_inf_eq _ /-! In this section, we'll give interpretations of these results in the Heyting algebra model of intuitionistic logic,- where `≤` can be interpreted as "validates", `⇨` as "implies", `⊓` as "and", `⊔` as "or", `⊥` as "false" and `⊤` as "true". Note that we confuse `→` and `⊢` because those are the same in this logic. See also `Prop.heytingAlgebra`. -/ section GeneralizedHeytingAlgebra variable [GeneralizedHeytingAlgebra α] {a b c d : α} /-- `p → q → r ↔ p ∧ q → r` -/ @[simp] theorem le_himp_iff : a ≤ b ⇨ c ↔ a ⊓ b ≤ c := GeneralizedHeytingAlgebra.le_himp_iff _ _ _ /-- `p → q → r ↔ q ∧ p → r` -/ theorem le_himp_iff' : a ≤ b ⇨ c ↔ b ⊓ a ≤ c := by rw [le_himp_iff, inf_comm] /-- `p → q → r ↔ q → p → r` -/ theorem le_himp_comm : a ≤ b ⇨ c ↔ b ≤ a ⇨ c := by rw [le_himp_iff, le_himp_iff'] /-- `p → q → p` -/ theorem le_himp : a ≤ b ⇨ a := le_himp_iff.2 inf_le_left /-- `p → p → q ↔ p → q` -/ theorem le_himp_iff_left : a ≤ a ⇨ b ↔ a ≤ b := by rw [le_himp_iff, inf_idem] /-- `p → p` -/ @[simp] theorem himp_self : a ⇨ a = ⊤ := top_le_iff.1 <| le_himp_iff.2 inf_le_right /-- `(p → q) ∧ p → q` -/ theorem himp_inf_le : (a ⇨ b) ⊓ a ≤ b := le_himp_iff.1 le_rfl /-- `p ∧ (p → q) → q` -/ theorem inf_himp_le : a ⊓ (a ⇨ b) ≤ b := by rw [inf_comm, ← le_himp_iff] /-- `p ∧ (p → q) ↔ p ∧ q` -/ @[simp] theorem inf_himp (a b : α) : a ⊓ (a ⇨ b) = a ⊓ b := le_antisymm (le_inf inf_le_left <| by rw [inf_comm, ← le_himp_iff]) <| inf_le_inf_left _ le_himp /-- `(p → q) ∧ p ↔ q ∧ p` -/ @[simp] theorem himp_inf_self (a b : α) : (a ⇨ b) ⊓ a = b ⊓ a := by rw [inf_comm, inf_himp, inf_comm] /-- The **deduction theorem** in the Heyting algebra model of intuitionistic logic: an implication holds iff the conclusion follows from the hypothesis. -/ @[simp] theorem himp_eq_top_iff : a ⇨ b = ⊤ ↔ a ≤ b := by rw [← top_le_iff, le_himp_iff, top_inf_eq] /-- `p → true`, `true → p ↔ p` -/ @[simp] theorem himp_top : a ⇨ ⊤ = ⊤ := himp_eq_top_iff.2 le_top @[simp] theorem top_himp : ⊤ ⇨ a = a := eq_of_forall_le_iff fun b => by rw [le_himp_iff, inf_top_eq] /-- `p → q → r ↔ p ∧ q → r` -/ theorem himp_himp (a b c : α) : a ⇨ b ⇨ c = a ⊓ b ⇨ c := eq_of_forall_le_iff fun d => by simp_rw [le_himp_iff, inf_assoc] /-- `(q → r) → (p → q) → q → r` -/ theorem himp_le_himp_himp_himp : b ⇨ c ≤ (a ⇨ b) ⇨ a ⇨ c := by rw [le_himp_iff, le_himp_iff, inf_assoc, himp_inf_self, ← inf_assoc, himp_inf_self, inf_assoc] exact inf_le_left @[simp] theorem himp_inf_himp_inf_le : (b ⇨ c) ⊓ (a ⇨ b) ⊓ a ≤ c := by simpa using @himp_le_himp_himp_himp /-- `p → q → r ↔ q → p → r` -/ theorem himp_left_comm (a b c : α) : a ⇨ b ⇨ c = b ⇨ a ⇨ c := by simp_rw [himp_himp, inf_comm] @[simp] theorem himp_idem : b ⇨ b ⇨ a = b ⇨ a := by rw [himp_himp, inf_idem] theorem himp_inf_distrib (a b c : α) : a ⇨ b ⊓ c = (a ⇨ b) ⊓ (a ⇨ c) := eq_of_forall_le_iff fun d => by simp_rw [le_himp_iff, le_inf_iff, le_himp_iff] theorem sup_himp_distrib (a b c : α) : a ⊔ b ⇨ c = (a ⇨ c) ⊓ (b ⇨ c) := eq_of_forall_le_iff fun d => by rw [le_inf_iff, le_himp_comm, sup_le_iff] simp_rw [le_himp_comm] theorem himp_le_himp_left (h : a ≤ b) : c ⇨ a ≤ c ⇨ b := le_himp_iff.2 <| himp_inf_le.trans h theorem himp_le_himp_right (h : a ≤ b) : b ⇨ c ≤ a ⇨ c := le_himp_iff.2 <| (inf_le_inf_left _ h).trans himp_inf_le theorem himp_le_himp (hab : a ≤ b) (hcd : c ≤ d) : b ⇨ c ≤ a ⇨ d := (himp_le_himp_right hab).trans <| himp_le_himp_left hcd @[simp] theorem sup_himp_self_left (a b : α) : a ⊔ b ⇨ a = b ⇨ a := by rw [sup_himp_distrib, himp_self, top_inf_eq] @[simp] theorem sup_himp_self_right (a b : α) : a ⊔ b ⇨ b = a ⇨ b := by rw [sup_himp_distrib, himp_self, inf_top_eq] theorem Codisjoint.himp_eq_right (h : Codisjoint a b) : b ⇨ a = a := by conv_rhs => rw [← @top_himp _ _ a] rw [← h.eq_top, sup_himp_self_left] theorem Codisjoint.himp_eq_left (h : Codisjoint a b) : a ⇨ b = b := h.symm.himp_eq_right theorem Codisjoint.himp_inf_cancel_right (h : Codisjoint a b) : a ⇨ a ⊓ b = b := by rw [himp_inf_distrib, himp_self, top_inf_eq, h.himp_eq_left] theorem Codisjoint.himp_inf_cancel_left (h : Codisjoint a b) : b ⇨ a ⊓ b = a := by rw [himp_inf_distrib, himp_self, inf_top_eq, h.himp_eq_right] /-- See `himp_le` for a stronger version in Boolean algebras. -/ theorem Codisjoint.himp_le_of_right_le (hac : Codisjoint a c) (hba : b ≤ a) : c ⇨ b ≤ a := (himp_le_himp_left hba).trans_eq hac.himp_eq_right theorem le_himp_himp : a ≤ (a ⇨ b) ⇨ b := le_himp_iff.2 inf_himp_le @[simp] lemma himp_eq_himp_iff : b ⇨ a = a ⇨ b ↔ a = b := by simp [le_antisymm_iff] lemma himp_ne_himp_iff : b ⇨ a ≠ a ⇨ b ↔ a ≠ b := himp_eq_himp_iff.not theorem himp_triangle (a b c : α) : (a ⇨ b) ⊓ (b ⇨ c) ≤ a ⇨ c := by rw [le_himp_iff, inf_right_comm, ← le_himp_iff] exact himp_inf_le.trans le_himp_himp theorem himp_inf_himp_cancel (hba : b ≤ a) (hcb : c ≤ b) : (a ⇨ b) ⊓ (b ⇨ c) = a ⇨ c := (himp_triangle _ _ _).antisymm <| le_inf (himp_le_himp_left hcb) (himp_le_himp_right hba) theorem gc_inf_himp : GaloisConnection (a ⊓ ·) (a ⇨ ·) := fun _ _ ↦ Iff.symm le_himp_iff' -- See note [lower instance priority] instance (priority := 100) GeneralizedHeytingAlgebra.toDistribLattice : DistribLattice α := DistribLattice.ofInfSupLe fun a b c => by simp_rw [inf_comm a, ← le_himp_iff, sup_le_iff, le_himp_iff, ← sup_le_iff]; rfl instance OrderDual.instGeneralizedCoheytingAlgebra : GeneralizedCoheytingAlgebra αᵒᵈ where sdiff a b := toDual (ofDual b ⇨ ofDual a) sdiff_le_iff a b c := by rw [sup_comm]; exact le_himp_iff instance Prod.instGeneralizedHeytingAlgebra [GeneralizedHeytingAlgebra β] : GeneralizedHeytingAlgebra (α × β) where le_himp_iff _ _ _ := and_congr le_himp_iff le_himp_iff instance Pi.instGeneralizedHeytingAlgebra {α : ι → Type*} [∀ i, GeneralizedHeytingAlgebra (α i)] : GeneralizedHeytingAlgebra (∀ i, α i) where le_himp_iff i := by simp [le_def] end GeneralizedHeytingAlgebra section GeneralizedCoheytingAlgebra variable [GeneralizedCoheytingAlgebra α] {a b c d : α} @[simp] theorem sdiff_le_iff : a \ b ≤ c ↔ a ≤ b ⊔ c := GeneralizedCoheytingAlgebra.sdiff_le_iff _ _ _ theorem sdiff_le_iff' : a \ b ≤ c ↔ a ≤ c ⊔ b := by rw [sdiff_le_iff, sup_comm] theorem sdiff_le_comm : a \ b ≤ c ↔ a \ c ≤ b := by rw [sdiff_le_iff, sdiff_le_iff'] theorem sdiff_le : a \ b ≤ a := sdiff_le_iff.2 le_sup_right theorem Disjoint.disjoint_sdiff_left (h : Disjoint a b) : Disjoint (a \ c) b := h.mono_left sdiff_le theorem Disjoint.disjoint_sdiff_right (h : Disjoint a b) : Disjoint a (b \ c) := h.mono_right sdiff_le theorem sdiff_le_iff_left : a \ b ≤ b ↔ a ≤ b := by rw [sdiff_le_iff, sup_idem] @[simp] theorem sdiff_self : a \ a = ⊥ := le_bot_iff.1 <| sdiff_le_iff.2 le_sup_left theorem le_sup_sdiff : a ≤ b ⊔ a \ b := sdiff_le_iff.1 le_rfl theorem le_sdiff_sup : a ≤ a \ b ⊔ b := by rw [sup_comm, ← sdiff_le_iff] theorem sup_sdiff_left : a ⊔ a \ b = a := sup_of_le_left sdiff_le theorem sup_sdiff_right : a \ b ⊔ a = a := sup_of_le_right sdiff_le theorem inf_sdiff_left : a \ b ⊓ a = a \ b := inf_of_le_left sdiff_le theorem inf_sdiff_right : a ⊓ a \ b = a \ b := inf_of_le_right sdiff_le @[simp] theorem sup_sdiff_self (a b : α) : a ⊔ b \ a = a ⊔ b := le_antisymm (sup_le_sup_left sdiff_le _) (sup_le le_sup_left le_sup_sdiff) @[simp] theorem sdiff_sup_self (a b : α) : b \ a ⊔ a = b ⊔ a := by rw [sup_comm, sup_sdiff_self, sup_comm] alias sup_sdiff_self_left := sdiff_sup_self alias sup_sdiff_self_right := sup_sdiff_self theorem sup_sdiff_eq_sup (h : c ≤ a) : a ⊔ b \ c = a ⊔ b := sup_congr_left (sdiff_le.trans le_sup_right) <| le_sup_sdiff.trans <| sup_le_sup_right h _ -- cf. `Set.union_diff_cancel'` theorem sup_sdiff_cancel' (hab : a ≤ b) (hbc : b ≤ c) : b ⊔ c \ a = c := by rw [sup_sdiff_eq_sup hab, sup_of_le_right hbc] theorem sup_sdiff_cancel_right (h : a ≤ b) : a ⊔ b \ a = b := sup_sdiff_cancel' le_rfl h theorem sdiff_sup_cancel (h : b ≤ a) : a \ b ⊔ b = a := by rw [sup_comm, sup_sdiff_cancel_right h] theorem sup_le_of_le_sdiff_left (h : b ≤ c \ a) (hac : a ≤ c) : a ⊔ b ≤ c := sup_le hac <| h.trans sdiff_le theorem sup_le_of_le_sdiff_right (h : a ≤ c \ b) (hbc : b ≤ c) : a ⊔ b ≤ c := sup_le (h.trans sdiff_le) hbc @[simp] theorem sdiff_eq_bot_iff : a \ b = ⊥ ↔ a ≤ b := by rw [← le_bot_iff, sdiff_le_iff, sup_bot_eq] @[simp] theorem sdiff_bot : a \ ⊥ = a := eq_of_forall_ge_iff fun b => by rw [sdiff_le_iff, bot_sup_eq] @[simp] theorem bot_sdiff : ⊥ \ a = ⊥ := sdiff_eq_bot_iff.2 bot_le theorem sdiff_sdiff_sdiff_le_sdiff : (a \ b) \ (a \ c) ≤ c \ b := by rw [sdiff_le_iff, sdiff_le_iff, sup_left_comm, sup_sdiff_self, sup_left_comm, sdiff_sup_self, sup_left_comm] exact le_sup_left @[simp] theorem le_sup_sdiff_sup_sdiff : a ≤ b ⊔ (a \ c ⊔ c \ b) := by simpa using @sdiff_sdiff_sdiff_le_sdiff theorem sdiff_sdiff (a b c : α) : (a \ b) \ c = a \ (b ⊔ c) := eq_of_forall_ge_iff fun d => by simp_rw [sdiff_le_iff, sup_assoc] theorem sdiff_sdiff_left : (a \ b) \ c = a \ (b ⊔ c) := sdiff_sdiff _ _ _ theorem sdiff_right_comm (a b c : α) : (a \ b) \ c = (a \ c) \ b := by simp_rw [sdiff_sdiff, sup_comm] theorem sdiff_sdiff_comm : (a \ b) \ c = (a \ c) \ b := sdiff_right_comm _ _ _ @[simp] theorem sdiff_idem : (a \ b) \ b = a \ b := by rw [sdiff_sdiff_left, sup_idem] @[simp] theorem sdiff_sdiff_self : (a \ b) \ a = ⊥ := by rw [sdiff_sdiff_comm, sdiff_self, bot_sdiff] theorem sup_sdiff_distrib (a b c : α) : (a ⊔ b) \ c = a \ c ⊔ b \ c := eq_of_forall_ge_iff fun d => by simp_rw [sdiff_le_iff, sup_le_iff, sdiff_le_iff] theorem sdiff_inf_distrib (a b c : α) : a \ (b ⊓ c) = a \ b ⊔ a \ c := eq_of_forall_ge_iff fun d => by rw [sup_le_iff, sdiff_le_comm, le_inf_iff] simp_rw [sdiff_le_comm] theorem sup_sdiff : (a ⊔ b) \ c = a \ c ⊔ b \ c := sup_sdiff_distrib _ _ _ @[simp] theorem sup_sdiff_right_self : (a ⊔ b) \ b = a \ b := by rw [sup_sdiff, sdiff_self, sup_bot_eq] @[simp] theorem sup_sdiff_left_self : (a ⊔ b) \ a = b \ a := by rw [sup_comm, sup_sdiff_right_self] @[gcongr] theorem sdiff_le_sdiff_right (h : a ≤ b) : a \ c ≤ b \ c := sdiff_le_iff.2 <| h.trans <| le_sup_sdiff @[gcongr] theorem sdiff_le_sdiff_left (h : a ≤ b) : c \ b ≤ c \ a := sdiff_le_iff.2 <| le_sup_sdiff.trans <| sup_le_sup_right h _ @[gcongr] theorem sdiff_le_sdiff (hab : a ≤ b) (hcd : c ≤ d) : a \ d ≤ b \ c := (sdiff_le_sdiff_right hab).trans <| sdiff_le_sdiff_left hcd -- cf. `IsCompl.inf_sup` theorem sdiff_inf : a \ (b ⊓ c) = a \ b ⊔ a \ c := sdiff_inf_distrib _ _ _ @[simp] theorem sdiff_inf_self_left (a b : α) : a \ (a ⊓ b) = a \ b := by rw [sdiff_inf, sdiff_self, bot_sup_eq] @[simp] theorem sdiff_inf_self_right (a b : α) : b \ (a ⊓ b) = b \ a := by rw [sdiff_inf, sdiff_self, sup_bot_eq] theorem Disjoint.sdiff_eq_left (h : Disjoint a b) : a \ b = a := by conv_rhs => rw [← @sdiff_bot _ _ a] rw [← h.eq_bot, sdiff_inf_self_left] theorem Disjoint.sdiff_eq_right (h : Disjoint a b) : b \ a = b := h.symm.sdiff_eq_left theorem Disjoint.sup_sdiff_cancel_left (h : Disjoint a b) : (a ⊔ b) \ a = b := by rw [sup_sdiff, sdiff_self, bot_sup_eq, h.sdiff_eq_right] theorem Disjoint.sup_sdiff_cancel_right (h : Disjoint a b) : (a ⊔ b) \ b = a := by rw [sup_sdiff, sdiff_self, sup_bot_eq, h.sdiff_eq_left] /-- See `le_sdiff` for a stronger version in generalised Boolean algebras. -/ theorem Disjoint.le_sdiff_of_le_left (hac : Disjoint a c) (hab : a ≤ b) : a ≤ b \ c := hac.sdiff_eq_left.ge.trans <| sdiff_le_sdiff_right hab theorem sdiff_sdiff_le : a \ (a \ b) ≤ b := sdiff_le_iff.2 le_sdiff_sup @[simp] lemma sdiff_eq_sdiff_iff : a \ b = b \ a ↔ a = b := by simp [le_antisymm_iff] lemma sdiff_ne_sdiff_iff : a \ b ≠ b \ a ↔ a ≠ b := sdiff_eq_sdiff_iff.not theorem sdiff_triangle (a b c : α) : a \ c ≤ a \ b ⊔ b \ c := by rw [sdiff_le_iff, sup_left_comm, ← sdiff_le_iff] exact sdiff_sdiff_le.trans le_sup_sdiff theorem sdiff_sup_sdiff_cancel (hba : b ≤ a) (hcb : c ≤ b) : a \ b ⊔ b \ c = a \ c := (sdiff_triangle _ _ _).antisymm' <| sup_le (sdiff_le_sdiff_left hcb) (sdiff_le_sdiff_right hba) /-- a version of `sdiff_sup_sdiff_cancel` with more general hypotheses. -/ theorem sdiff_sup_sdiff_cancel' (hinf : a ⊓ c ≤ b) (hsup : b ≤ a ⊔ c) : a \ b ⊔ b \ c = a \ c := by refine (sdiff_triangle ..).antisymm' <| sup_le ?_ <| by simpa [sup_comm] rw [← sdiff_inf_self_left (b := c)] exact sdiff_le_sdiff_left hinf theorem sdiff_le_sdiff_of_sup_le_sup_left (h : c ⊔ a ≤ c ⊔ b) : a \ c ≤ b \ c := by rw [← sup_sdiff_left_self, ← @sup_sdiff_left_self _ _ _ b] exact sdiff_le_sdiff_right h theorem sdiff_le_sdiff_of_sup_le_sup_right (h : a ⊔ c ≤ b ⊔ c) : a \ c ≤ b \ c := by rw [← sup_sdiff_right_self, ← @sup_sdiff_right_self _ _ b] exact sdiff_le_sdiff_right h @[simp] theorem inf_sdiff_sup_left : a \ c ⊓ (a ⊔ b) = a \ c := inf_of_le_left <| sdiff_le.trans le_sup_left @[simp]
theorem inf_sdiff_sup_right : a \ c ⊓ (b ⊔ a) = a \ c := inf_of_le_left <| sdiff_le.trans le_sup_right
Mathlib/Order/Heyting/Basic.lean
580
581
/- Copyright (c) 2018 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Mario Carneiro, Simon Hudon -/ import Mathlib.Data.Fin.Fin2 import Mathlib.Logic.Function.Basic import Mathlib.Tactic.Common /-! # Tuples of types, and their categorical structure. ## Features * `TypeVec n` - n-tuples of types * `α ⟹ β` - n-tuples of maps * `f ⊚ g` - composition Also, support functions for operating with n-tuples of types, such as: * `append1 α β` - append type `β` to n-tuple `α` to obtain an (n+1)-tuple * `drop α` - drops the last element of an (n+1)-tuple * `last α` - returns the last element of an (n+1)-tuple * `appendFun f g` - appends a function g to an n-tuple of functions * `dropFun f` - drops the last function from an n+1-tuple * `lastFun f` - returns the last function of a tuple. Since e.g. `append1 α.drop α.last` is propositionally equal to `α` but not definitionally equal to it, we need support functions and lemmas to mediate between constructions. -/ universe u v w /-- n-tuples of types, as a category -/ @[pp_with_univ] def TypeVec (n : ℕ) := Fin2 n → Type* instance {n} : Inhabited (TypeVec.{u} n) := ⟨fun _ => PUnit⟩ namespace TypeVec variable {n : ℕ} /-- arrow in the category of `TypeVec` -/ def Arrow (α β : TypeVec n) := ∀ i : Fin2 n, α i → β i @[inherit_doc] scoped[MvFunctor] infixl:40 " ⟹ " => TypeVec.Arrow open MvFunctor /-- Extensionality for arrows -/ @[ext] theorem Arrow.ext {α β : TypeVec n} (f g : α ⟹ β) : (∀ i, f i = g i) → f = g := by intro h; funext i; apply h instance Arrow.inhabited (α β : TypeVec n) [∀ i, Inhabited (β i)] : Inhabited (α ⟹ β) := ⟨fun _ _ => default⟩ /-- identity of arrow composition -/ def id {α : TypeVec n} : α ⟹ α := fun _ x => x /-- arrow composition in the category of `TypeVec` -/ def comp {α β γ : TypeVec n} (g : β ⟹ γ) (f : α ⟹ β) : α ⟹ γ := fun i x => g i (f i x) @[inherit_doc] scoped[MvFunctor] infixr:80 " ⊚ " => TypeVec.comp -- type as \oo @[simp] theorem id_comp {α β : TypeVec n} (f : α ⟹ β) : id ⊚ f = f := rfl @[simp] theorem comp_id {α β : TypeVec n} (f : α ⟹ β) : f ⊚ id = f := rfl theorem comp_assoc {α β γ δ : TypeVec n} (h : γ ⟹ δ) (g : β ⟹ γ) (f : α ⟹ β) : (h ⊚ g) ⊚ f = h ⊚ g ⊚ f := rfl /-- Support for extending a `TypeVec` by one element. -/ def append1 (α : TypeVec n) (β : Type*) : TypeVec (n + 1) | Fin2.fs i => α i | Fin2.fz => β @[inherit_doc] infixl:67 " ::: " => append1 /-- retain only a `n-length` prefix of the argument -/ def drop (α : TypeVec.{u} (n + 1)) : TypeVec n := fun i => α i.fs /-- take the last value of a `(n+1)-length` vector -/ def last (α : TypeVec.{u} (n + 1)) : Type _ := α Fin2.fz instance last.inhabited (α : TypeVec (n + 1)) [Inhabited (α Fin2.fz)] : Inhabited (last α) := ⟨show α Fin2.fz from default⟩ theorem drop_append1 {α : TypeVec n} {β : Type*} {i : Fin2 n} : drop (append1 α β) i = α i := rfl theorem drop_append1' {α : TypeVec n} {β : Type*} : drop (append1 α β) = α := funext fun _ => drop_append1 theorem last_append1 {α : TypeVec n} {β : Type*} : last (append1 α β) = β := rfl @[simp] theorem append1_drop_last (α : TypeVec (n + 1)) : append1 (drop α) (last α) = α := funext fun i => by cases i <;> rfl /-- cases on `(n+1)-length` vectors -/ @[elab_as_elim] def append1Cases {C : TypeVec (n + 1) → Sort u} (H : ∀ α β, C (append1 α β)) (γ) : C γ := by rw [← @append1_drop_last _ γ]; apply H @[simp] theorem append1_cases_append1 {C : TypeVec (n + 1) → Sort u} (H : ∀ α β, C (append1 α β)) (α β) : @append1Cases _ C H (append1 α β) = H α β := rfl /-- append an arrow and a function for arbitrary source and target type vectors -/ def splitFun {α α' : TypeVec (n + 1)} (f : drop α ⟹ drop α') (g : last α → last α') : α ⟹ α' | Fin2.fs i => f i | Fin2.fz => g /-- append an arrow and a function as well as their respective source and target types / typevecs -/ def appendFun {α α' : TypeVec n} {β β' : Type*} (f : α ⟹ α') (g : β → β') : append1 α β ⟹ append1 α' β' := splitFun f g @[inherit_doc] infixl:0 " ::: " => appendFun /-- split off the prefix of an arrow -/ def dropFun {α β : TypeVec (n + 1)} (f : α ⟹ β) : drop α ⟹ drop β := fun i => f i.fs /-- split off the last function of an arrow -/ def lastFun {α β : TypeVec (n + 1)} (f : α ⟹ β) : last α → last β := f Fin2.fz /-- arrow in the category of `0-length` vectors -/ def nilFun {α : TypeVec 0} {β : TypeVec 0} : α ⟹ β := fun i => by apply Fin2.elim0 i theorem eq_of_drop_last_eq {α β : TypeVec (n + 1)} {f g : α ⟹ β} (h₀ : dropFun f = dropFun g) (h₁ : lastFun f = lastFun g) : f = g := by refine funext (fun x => ?_) cases x · apply h₁ · apply congr_fun h₀ @[simp] theorem dropFun_splitFun {α α' : TypeVec (n + 1)} (f : drop α ⟹ drop α') (g : last α → last α') : dropFun (splitFun f g) = f := rfl /-- turn an equality into an arrow -/ def Arrow.mp {α β : TypeVec n} (h : α = β) : α ⟹ β | _ => Eq.mp (congr_fun h _) /-- turn an equality into an arrow, with reverse direction -/ def Arrow.mpr {α β : TypeVec n} (h : α = β) : β ⟹ α | _ => Eq.mpr (congr_fun h _) /-- decompose a vector into its prefix appended with its last element -/ def toAppend1DropLast {α : TypeVec (n + 1)} : α ⟹ (drop α ::: last α) := Arrow.mpr (append1_drop_last _) /-- stitch two bits of a vector back together -/ def fromAppend1DropLast {α : TypeVec (n + 1)} : (drop α ::: last α) ⟹ α := Arrow.mp (append1_drop_last _) @[simp] theorem lastFun_splitFun {α α' : TypeVec (n + 1)} (f : drop α ⟹ drop α') (g : last α → last α') : lastFun (splitFun f g) = g := rfl @[simp] theorem dropFun_appendFun {α α' : TypeVec n} {β β' : Type*} (f : α ⟹ α') (g : β → β') : dropFun (f ::: g) = f := rfl @[simp] theorem lastFun_appendFun {α α' : TypeVec n} {β β' : Type*} (f : α ⟹ α') (g : β → β') : lastFun (f ::: g) = g := rfl theorem split_dropFun_lastFun {α α' : TypeVec (n + 1)} (f : α ⟹ α') : splitFun (dropFun f) (lastFun f) = f := eq_of_drop_last_eq rfl rfl theorem splitFun_inj {α α' : TypeVec (n + 1)} {f f' : drop α ⟹ drop α'} {g g' : last α → last α'} (H : splitFun f g = splitFun f' g') : f = f' ∧ g = g' := by rw [← dropFun_splitFun f g, H, ← lastFun_splitFun f g, H]; simp theorem appendFun_inj {α α' : TypeVec n} {β β' : Type*} {f f' : α ⟹ α'} {g g' : β → β'} : (f ::: g : (α ::: β) ⟹ _) = (f' ::: g' : (α ::: β) ⟹ _) → f = f' ∧ g = g' := splitFun_inj theorem splitFun_comp {α₀ α₁ α₂ : TypeVec (n + 1)} (f₀ : drop α₀ ⟹ drop α₁) (f₁ : drop α₁ ⟹ drop α₂) (g₀ : last α₀ → last α₁) (g₁ : last α₁ → last α₂) : splitFun (f₁ ⊚ f₀) (g₁ ∘ g₀) = splitFun f₁ g₁ ⊚ splitFun f₀ g₀ := eq_of_drop_last_eq rfl rfl theorem appendFun_comp_splitFun {α γ : TypeVec n} {β δ : Type*} {ε : TypeVec (n + 1)} (f₀ : drop ε ⟹ α) (f₁ : α ⟹ γ) (g₀ : last ε → β) (g₁ : β → δ) : appendFun f₁ g₁ ⊚ splitFun f₀ g₀ = splitFun (α' := γ.append1 δ) (f₁ ⊚ f₀) (g₁ ∘ g₀) := (splitFun_comp _ _ _ _).symm theorem appendFun_comp {α₀ α₁ α₂ : TypeVec n} {β₀ β₁ β₂ : Type*} (f₀ : α₀ ⟹ α₁) (f₁ : α₁ ⟹ α₂) (g₀ : β₀ → β₁) (g₁ : β₁ → β₂) : (f₁ ⊚ f₀ ::: g₁ ∘ g₀) = (f₁ ::: g₁) ⊚ (f₀ ::: g₀) := eq_of_drop_last_eq rfl rfl theorem appendFun_comp' {α₀ α₁ α₂ : TypeVec n} {β₀ β₁ β₂ : Type*} (f₀ : α₀ ⟹ α₁) (f₁ : α₁ ⟹ α₂) (g₀ : β₀ → β₁) (g₁ : β₁ → β₂) : (f₁ ::: g₁) ⊚ (f₀ ::: g₀) = (f₁ ⊚ f₀ ::: g₁ ∘ g₀) := eq_of_drop_last_eq rfl rfl theorem nilFun_comp {α₀ : TypeVec 0} (f₀ : α₀ ⟹ Fin2.elim0) : nilFun ⊚ f₀ = f₀ := funext Fin2.elim0 theorem appendFun_comp_id {α : TypeVec n} {β₀ β₁ β₂ : Type u} (g₀ : β₀ → β₁) (g₁ : β₁ → β₂) : (@id _ α ::: g₁ ∘ g₀) = (id ::: g₁) ⊚ (id ::: g₀) := eq_of_drop_last_eq rfl rfl @[simp] theorem dropFun_comp {α₀ α₁ α₂ : TypeVec (n + 1)} (f₀ : α₀ ⟹ α₁) (f₁ : α₁ ⟹ α₂) : dropFun (f₁ ⊚ f₀) = dropFun f₁ ⊚ dropFun f₀ := rfl @[simp] theorem lastFun_comp {α₀ α₁ α₂ : TypeVec (n + 1)} (f₀ : α₀ ⟹ α₁) (f₁ : α₁ ⟹ α₂) : lastFun (f₁ ⊚ f₀) = lastFun f₁ ∘ lastFun f₀ := rfl theorem appendFun_aux {α α' : TypeVec n} {β β' : Type*} (f : (α ::: β) ⟹ (α' ::: β')) : (dropFun f ::: lastFun f) = f := eq_of_drop_last_eq rfl rfl theorem appendFun_id_id {α : TypeVec n} {β : Type*} : (@TypeVec.id n α ::: @_root_.id β) = TypeVec.id := eq_of_drop_last_eq rfl rfl instance subsingleton0 : Subsingleton (TypeVec 0) := ⟨fun _ _ => funext Fin2.elim0⟩ -- See `Mathlib.Tactic.Attr.Register` for `register_simp_attr typevec` /-- cases distinction for 0-length type vector -/ protected def casesNil {β : TypeVec 0 → Sort*} (f : β Fin2.elim0) : ∀ v, β v := fun v => cast (by congr; funext i; cases i) f /-- cases distinction for (n+1)-length type vector -/ protected def casesCons (n : ℕ) {β : TypeVec (n + 1) → Sort*} (f : ∀ (t) (v : TypeVec n), β (v ::: t)) : ∀ v, β v := fun v : TypeVec (n + 1) => cast (by simp) (f v.last v.drop) protected theorem casesNil_append1 {β : TypeVec 0 → Sort*} (f : β Fin2.elim0) : TypeVec.casesNil f Fin2.elim0 = f := rfl protected theorem casesCons_append1 (n : ℕ) {β : TypeVec (n + 1) → Sort*} (f : ∀ (t) (v : TypeVec n), β (v ::: t)) (v : TypeVec n) (α) : TypeVec.casesCons n f (v ::: α) = f α v := rfl /-- cases distinction for an arrow in the category of 0-length type vectors -/ def typevecCasesNil₃ {β : ∀ v v' : TypeVec 0, v ⟹ v' → Sort*} (f : β Fin2.elim0 Fin2.elim0 nilFun) : ∀ v v' fs, β v v' fs := fun v v' fs => by refine cast ?_ f have eq₁ : v = Fin2.elim0 := by funext i; contradiction have eq₂ : v' = Fin2.elim0 := by funext i; contradiction have eq₃ : fs = nilFun := by funext i; contradiction cases eq₁; cases eq₂; cases eq₃; rfl /-- cases distinction for an arrow in the category of (n+1)-length type vectors -/ def typevecCasesCons₃ (n : ℕ) {β : ∀ v v' : TypeVec (n + 1), v ⟹ v' → Sort*} (F : ∀ (t t') (f : t → t') (v v' : TypeVec n) (fs : v ⟹ v'), β (v ::: t) (v' ::: t') (fs ::: f)) : ∀ v v' fs, β v v' fs := by intro v v' rw [← append1_drop_last v, ← append1_drop_last v'] intro fs rw [← split_dropFun_lastFun fs] apply F /-- specialized cases distinction for an arrow in the category of 0-length type vectors -/ def typevecCasesNil₂ {β : Fin2.elim0 ⟹ Fin2.elim0 → Sort*} (f : β nilFun) : ∀ f, β f := by intro g suffices g = nilFun by rwa [this] ext ⟨⟩ /-- specialized cases distinction for an arrow in the category of (n+1)-length type vectors -/ def typevecCasesCons₂ (n : ℕ) (t t' : Type*) (v v' : TypeVec n) {β : (v ::: t) ⟹ (v' ::: t') → Sort*} (F : ∀ (f : t → t') (fs : v ⟹ v'), β (fs ::: f)) : ∀ fs, β fs := by intro fs rw [← split_dropFun_lastFun fs] apply F theorem typevecCasesNil₂_appendFun {β : Fin2.elim0 ⟹ Fin2.elim0 → Sort*} (f : β nilFun) : typevecCasesNil₂ f nilFun = f := rfl theorem typevecCasesCons₂_appendFun (n : ℕ) (t t' : Type*) (v v' : TypeVec n) {β : (v ::: t) ⟹ (v' ::: t') → Sort*} (F : ∀ (f : t → t') (fs : v ⟹ v'), β (fs ::: f)) (f fs) : typevecCasesCons₂ n t t' v v' F (fs ::: f) = F f fs := rfl -- for lifting predicates and relations /-- `PredLast α p x` predicates `p` of the last element of `x : α.append1 β`. -/ def PredLast (α : TypeVec n) {β : Type*} (p : β → Prop) : ∀ ⦃i⦄, (α.append1 β) i → Prop | Fin2.fs _ => fun _ => True | Fin2.fz => p /-- `RelLast α r x y` says that `p` the last elements of `x y : α.append1 β` are related by `r` and all the other elements are equal. -/ def RelLast (α : TypeVec n) {β γ : Type u} (r : β → γ → Prop) : ∀ ⦃i⦄, (α.append1 β) i → (α.append1 γ) i → Prop | Fin2.fs _ => Eq | Fin2.fz => r section Liftp' open Nat /-- `repeat n t` is a `n-length` type vector that contains `n` occurrences of `t` -/ def «repeat» : ∀ (n : ℕ), Sort _ → TypeVec n | 0, _ => Fin2.elim0 | Nat.succ i, t => append1 («repeat» i t) t /-- `prod α β` is the pointwise product of the components of `α` and `β` -/ def prod : ∀ {n}, TypeVec.{u} n → TypeVec.{u} n → TypeVec n | 0, _, _ => Fin2.elim0 | n + 1, α, β => (@prod n (drop α) (drop β)) ::: (last α × last β) @[inherit_doc] scoped[MvFunctor] infixl:45 " ⊗ " => TypeVec.prod /-- `const x α` is an arrow that ignores its source and constructs a `TypeVec` that contains nothing but `x` -/ protected def const {β} (x : β) : ∀ {n} (α : TypeVec n), α ⟹ «repeat» _ β | succ _, α, Fin2.fs _ => TypeVec.const x (drop α) _ | succ _, _, Fin2.fz => fun _ => x open Function (uncurry) /-- vector of equality on a product of vectors -/ def repeatEq : ∀ {n} (α : TypeVec n), (α ⊗ α) ⟹ «repeat» _ Prop | 0, _ => nilFun | succ _, α => repeatEq (drop α) ::: uncurry Eq theorem const_append1 {β γ} (x : γ) {n} (α : TypeVec n) : TypeVec.const x (α ::: β) = appendFun (TypeVec.const x α) fun _ => x := by ext i : 1; cases i <;> rfl theorem eq_nilFun {α β : TypeVec 0} (f : α ⟹ β) : f = nilFun := by ext x; cases x theorem id_eq_nilFun {α : TypeVec 0} : @id _ α = nilFun := by ext x; cases x theorem const_nil {β} (x : β) (α : TypeVec 0) : TypeVec.const x α = nilFun := by ext i : 1; cases i @[typevec] theorem repeat_eq_append1 {β} {n} (α : TypeVec n) : repeatEq (α ::: β) = splitFun (α := (α ⊗ α) ::: _) (α' := («repeat» n Prop) ::: _) (repeatEq α) (uncurry Eq) := by induction n <;> rfl @[typevec] theorem repeat_eq_nil (α : TypeVec 0) : repeatEq α = nilFun := by ext i; cases i /-- predicate on a type vector to constrain only the last object -/ def PredLast' (α : TypeVec n) {β : Type*} (p : β → Prop) : (α ::: β) ⟹ «repeat» (n + 1) Prop := splitFun (TypeVec.const True α) p /-- predicate on the product of two type vectors to constrain only their last object -/ def RelLast' (α : TypeVec n) {β : Type*} (p : β → β → Prop) : (α ::: β) ⊗ (α ::: β) ⟹ «repeat» (n + 1) Prop := splitFun (repeatEq α) (uncurry p) /-- given `F : TypeVec.{u} (n+1) → Type u`, `curry F : Type u → TypeVec.{u} → Type u`, i.e. its first argument can be fed in separately from the rest of the vector of arguments -/ def Curry (F : TypeVec.{u} (n + 1) → Type*) (α : Type u) (β : TypeVec.{u} n) : Type _ := F (β ::: α) instance Curry.inhabited (F : TypeVec.{u} (n + 1) → Type*) (α : Type u) (β : TypeVec.{u} n) [I : Inhabited (F <| (β ::: α))] : Inhabited (Curry F α β) := I /-- arrow to remove one element of a `repeat` vector -/ def dropRepeat (α : Type*) : ∀ {n}, drop («repeat» (succ n) α) ⟹ «repeat» n α | succ _, Fin2.fs i => dropRepeat α i | succ _, Fin2.fz => fun (a : α) => a /-- projection for a repeat vector -/ def ofRepeat {α : Sort _} : ∀ {n i}, «repeat» n α i → α | _, Fin2.fz => fun (a : α) => a | _, Fin2.fs i => @ofRepeat _ _ i theorem const_iff_true {α : TypeVec n} {i x p} : ofRepeat (TypeVec.const p α i x) ↔ p := by induction i with | fz => rfl | fs _ ih => rw [TypeVec.const] exact ih section variable {α β : TypeVec.{u} n} variable (p : α ⟹ «repeat» n Prop) /-- left projection of a `prod` vector -/ def prod.fst : ∀ {n} {α β : TypeVec.{u} n}, α ⊗ β ⟹ α | succ _, α, β, Fin2.fs i => @prod.fst _ (drop α) (drop β) i | succ _, _, _, Fin2.fz => Prod.fst /-- right projection of a `prod` vector -/ def prod.snd : ∀ {n} {α β : TypeVec.{u} n}, α ⊗ β ⟹ β | succ _, α, β, Fin2.fs i => @prod.snd _ (drop α) (drop β) i | succ _, _, _, Fin2.fz => Prod.snd /-- introduce a product where both components are the same -/ def prod.diag : ∀ {n} {α : TypeVec.{u} n}, α ⟹ α ⊗ α | succ _, α, Fin2.fs _, x => @prod.diag _ (drop α) _ x | succ _, _, Fin2.fz, x => (x, x) /-- constructor for `prod` -/ def prod.mk : ∀ {n} {α β : TypeVec.{u} n} (i : Fin2 n), α i → β i → (α ⊗ β) i | succ _, α, β, Fin2.fs i => mk (α := fun i => α i.fs) (β := fun i => β i.fs) i | succ _, _, _, Fin2.fz => Prod.mk end @[simp] theorem prod_fst_mk {α β : TypeVec n} (i : Fin2 n) (a : α i) (b : β i) : TypeVec.prod.fst i (prod.mk i a b) = a := by induction i with | fz => simp_all only [prod.fst, prod.mk] | fs _ i_ih => apply i_ih @[simp] theorem prod_snd_mk {α β : TypeVec n} (i : Fin2 n) (a : α i) (b : β i) : TypeVec.prod.snd i (prod.mk i a b) = b := by induction i with | fz => simp_all [prod.snd, prod.mk] | fs _ i_ih => apply i_ih /-- `prod` is functorial -/ protected def prod.map : ∀ {n} {α α' β β' : TypeVec.{u} n}, α ⟹ β → α' ⟹ β' → α ⊗ α' ⟹ β ⊗ β' | succ _, α, α', β, β', x, y, Fin2.fs _, a => @prod.map _ (drop α) (drop α') (drop β) (drop β') (dropFun x) (dropFun y) _ a | succ _, _, _, _, _, x, y, Fin2.fz, a => (x _ a.1, y _ a.2) @[inherit_doc] scoped[MvFunctor] infixl:45 " ⊗' " => TypeVec.prod.map theorem fst_prod_mk {α α' β β' : TypeVec n} (f : α ⟹ β) (g : α' ⟹ β') : TypeVec.prod.fst ⊚ (f ⊗' g) = f ⊚ TypeVec.prod.fst := by funext i; induction i with | fz => rfl | fs _ i_ih => apply i_ih theorem snd_prod_mk {α α' β β' : TypeVec n} (f : α ⟹ β) (g : α' ⟹ β') : TypeVec.prod.snd ⊚ (f ⊗' g) = g ⊚ TypeVec.prod.snd := by funext i; induction i with | fz => rfl | fs _ i_ih => apply i_ih theorem fst_diag {α : TypeVec n} : TypeVec.prod.fst ⊚ (prod.diag : α ⟹ _) = id := by funext i; induction i with | fz => rfl | fs _ i_ih => apply i_ih theorem snd_diag {α : TypeVec n} : TypeVec.prod.snd ⊚ (prod.diag : α ⟹ _) = id := by funext i; induction i with | fz => rfl | fs _ i_ih => apply i_ih theorem repeatEq_iff_eq {α : TypeVec n} {i x y} : ofRepeat (repeatEq α i (prod.mk _ x y)) ↔ x = y := by induction i with | fz => rfl | fs _ i_ih => rw [repeatEq] exact i_ih /-- given a predicate vector `p` over vector `α`, `Subtype_ p` is the type of vectors that contain an `α` that satisfies `p` -/ def Subtype_ : ∀ {n} {α : TypeVec.{u} n}, (α ⟹ «repeat» n Prop) → TypeVec n | _, _, p, Fin2.fz => Subtype fun x => p Fin2.fz x | _, _, p, Fin2.fs i => Subtype_ (dropFun p) i /-- projection on `Subtype_` -/ def subtypeVal : ∀ {n} {α : TypeVec.{u} n} (p : α ⟹ «repeat» n Prop), Subtype_ p ⟹ α | succ n, _, _, Fin2.fs i => @subtypeVal n _ _ i | succ _, _, _, Fin2.fz => Subtype.val /-- arrow that rearranges the type of `Subtype_` to turn a subtype of vector into a vector of subtypes -/ def toSubtype : ∀ {n} {α : TypeVec.{u} n} (p : α ⟹ «repeat» n Prop), (fun i : Fin2 n => { x // ofRepeat <| p i x }) ⟹ Subtype_ p | succ _, _, p, Fin2.fs i, x => toSubtype (dropFun p) i x | succ _, _, _, Fin2.fz, x => x /-- arrow that rearranges the type of `Subtype_` to turn a vector of subtypes into a subtype of vector -/ def ofSubtype {n} {α : TypeVec.{u} n} (p : α ⟹ «repeat» n Prop) : Subtype_ p ⟹ fun i : Fin2 n => { x // ofRepeat <| p i x } | Fin2.fs i, x => ofSubtype _ i x | Fin2.fz, x => x /-- similar to `toSubtype` adapted to relations (i.e. predicate on product) -/ def toSubtype' {n} {α : TypeVec.{u} n} (p : α ⊗ α ⟹ «repeat» n Prop) : (fun i : Fin2 n => { x : α i × α i // ofRepeat <| p i (prod.mk _ x.1 x.2) }) ⟹ Subtype_ p | Fin2.fs i, x => toSubtype' (dropFun p) i x | Fin2.fz, x => ⟨x.val, cast (by congr) x.property⟩ /-- similar to `of_subtype` adapted to relations (i.e. predicate on product) -/ def ofSubtype' {n} {α : TypeVec.{u} n} (p : α ⊗ α ⟹ «repeat» n Prop) : Subtype_ p ⟹ fun i : Fin2 n => { x : α i × α i // ofRepeat <| p i (prod.mk _ x.1 x.2) } | Fin2.fs i, x => ofSubtype' _ i x | Fin2.fz, x => ⟨x.val, cast (by congr) x.property⟩ /-- similar to `diag` but the target vector is a `Subtype_` guaranteeing the equality of the components -/ def diagSub {n} {α : TypeVec.{u} n} : α ⟹ Subtype_ (repeatEq α) | Fin2.fs _, x => @diagSub _ (drop α) _ x | Fin2.fz, x => ⟨(x, x), rfl⟩ theorem subtypeVal_nil {α : TypeVec.{u} 0} (ps : α ⟹ «repeat» 0 Prop) : TypeVec.subtypeVal ps = nilFun := funext <| by rintro ⟨⟩ theorem diag_sub_val {n} {α : TypeVec.{u} n} : subtypeVal (repeatEq α) ⊚ diagSub = prod.diag := by ext i x induction i with | fz => simp only [comp, subtypeVal, repeatEq.eq_2, diagSub, prod.diag] | fs _ i_ih => apply @i_ih (drop α) theorem prod_id : ∀ {n} {α β : TypeVec.{u} n}, (id ⊗' id) = (id : α ⊗ β ⟹ _) := by intros
ext i a induction i with | fz => cases a; rfl | fs _ i_ih => apply i_ih
Mathlib/Data/TypeVec.lean
556
560
/- Copyright (c) 2022 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import Mathlib.Order.Filter.Lift import Mathlib.Order.Interval.Set.Monotone import Mathlib.Topology.Separation.Basic /-! # Topology on the set of filters on a type This file introduces a topology on `Filter α`. It is generated by the sets `Set.Iic (𝓟 s) = {l : Filter α | s ∈ l}`, `s : Set α`. A set `s : Set (Filter α)` is open if and only if it is a union of a family of these basic open sets, see `Filter.isOpen_iff`. This topology has the following important properties. * If `X` is a topological space, then the map `𝓝 : X → Filter X` is a topology inducing map. * In particular, it is a continuous map, so `𝓝 ∘ f` tends to `𝓝 (𝓝 a)` whenever `f` tends to `𝓝 a`. * If `X` is an ordered topological space with order topology and no max element, then `𝓝 ∘ f` tends to `𝓝 Filter.atTop` whenever `f` tends to `Filter.atTop`. * It turns `Filter X` into a T₀ space and the order on `Filter X` is the dual of the `specializationOrder (Filter X)`. ## Tags filter, topological space -/ open Set Filter TopologicalSpace open Filter Topology variable {ι : Sort*} {α β X Y : Type*} namespace Filter /-- The topology on `Filter α` is generated by the sets `Set.Iic (𝓟 s) = {l : Filter α | s ∈ l}`, `s : Set α`. A set `s : Set (Filter α)` is open if and only if it is a union of a family of these basic open sets, see `Filter.isOpen_iff`. -/ instance : TopologicalSpace (Filter α) := generateFrom <| range <| Iic ∘ 𝓟 theorem isOpen_Iic_principal {s : Set α} : IsOpen (Iic (𝓟 s)) := GenerateOpen.basic _ (mem_range_self _) theorem isOpen_setOf_mem {s : Set α} : IsOpen { l : Filter α | s ∈ l } := by simpa only [Iic_principal] using isOpen_Iic_principal theorem isTopologicalBasis_Iic_principal : IsTopologicalBasis (range (Iic ∘ 𝓟 : Set α → Set (Filter α))) := { exists_subset_inter := by rintro _ ⟨s, rfl⟩ _ ⟨t, rfl⟩ l hl exact ⟨Iic (𝓟 s) ∩ Iic (𝓟 t), ⟨s ∩ t, by simp⟩, hl, Subset.rfl⟩ sUnion_eq := sUnion_eq_univ_iff.2 fun _ => ⟨Iic ⊤, ⟨univ, congr_arg Iic principal_univ⟩, mem_Iic.2 le_top⟩ eq_generateFrom := rfl } theorem isOpen_iff {s : Set (Filter α)} : IsOpen s ↔ ∃ T : Set (Set α), s = ⋃ t ∈ T, Iic (𝓟 t) := isTopologicalBasis_Iic_principal.open_iff_eq_sUnion.trans <| by simp only [exists_subset_range_and_iff, sUnion_image, (· ∘ ·)] theorem nhds_eq (l : Filter α) : 𝓝 l = l.lift' (Iic ∘ 𝓟) := nhds_generateFrom.trans <| by simp only [mem_setOf_eq, @and_comm (l ∈ _), iInf_and, iInf_range, Filter.lift', Filter.lift, (· ∘ ·), mem_Iic, le_principal_iff] theorem nhds_eq' (l : Filter α) : 𝓝 l = l.lift' fun s => { l' | s ∈ l' } := by simpa only [Function.comp_def, Iic_principal] using nhds_eq l protected theorem tendsto_nhds {la : Filter α} {lb : Filter β} {f : α → Filter β} : Tendsto f la (𝓝 lb) ↔ ∀ s ∈ lb, ∀ᶠ a in la, s ∈ f a := by simp only [nhds_eq', tendsto_lift', mem_setOf_eq] protected theorem HasBasis.nhds {l : Filter α} {p : ι → Prop} {s : ι → Set α} (h : HasBasis l p s) : HasBasis (𝓝 l) p fun i => Iic (𝓟 (s i)) := by rw [nhds_eq] exact h.lift' monotone_principal.Iic protected theorem tendsto_pure_self (l : Filter X) : Tendsto (pure : X → Filter X) l (𝓝 l) := by rw [Filter.tendsto_nhds] exact fun s hs ↦ Eventually.mono hs fun x ↦ id /-- Neighborhoods of a countably generated filter is a countably generated filter. -/ instance {l : Filter α} [IsCountablyGenerated l] : IsCountablyGenerated (𝓝 l) := let ⟨_b, hb⟩ := l.exists_antitone_basis HasCountableBasis.isCountablyGenerated <| ⟨hb.nhds, Set.to_countable _⟩ theorem HasBasis.nhds' {l : Filter α} {p : ι → Prop} {s : ι → Set α} (h : HasBasis l p s) : HasBasis (𝓝 l) p fun i => { l' | s i ∈ l' } := by simpa only [Iic_principal] using h.nhds protected theorem mem_nhds_iff {l : Filter α} {S : Set (Filter α)} : S ∈ 𝓝 l ↔ ∃ t ∈ l, Iic (𝓟 t) ⊆ S := l.basis_sets.nhds.mem_iff theorem mem_nhds_iff' {l : Filter α} {S : Set (Filter α)} : S ∈ 𝓝 l ↔ ∃ t ∈ l, ∀ ⦃l' : Filter α⦄, t ∈ l' → l' ∈ S := l.basis_sets.nhds'.mem_iff @[simp] theorem nhds_bot : 𝓝 (⊥ : Filter α) = pure ⊥ := by simp [nhds_eq, Function.comp_def, lift'_bot monotone_principal.Iic] @[simp] theorem nhds_top : 𝓝 (⊤ : Filter α) = ⊤ := by simp [nhds_eq] @[simp] theorem nhds_principal (s : Set α) : 𝓝 (𝓟 s) = 𝓟 (Iic (𝓟 s)) := (hasBasis_principal s).nhds.eq_of_same_basis (hasBasis_principal _) @[simp] theorem nhds_pure (x : α) : 𝓝 (pure x : Filter α) = 𝓟 {⊥, pure x} := by rw [← principal_singleton, nhds_principal, principal_singleton, Iic_pure] @[simp] protected theorem nhds_iInf (f : ι → Filter α) : 𝓝 (⨅ i, f i) = ⨅ i, 𝓝 (f i) := by simp only [nhds_eq] apply lift'_iInf_of_map_univ <;> simp
Mathlib/Topology/Filter.lean
125
125
/- Copyright (c) 2021 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta -/ import Mathlib.CategoryTheory.Comma.StructuredArrow.Small import Mathlib.CategoryTheory.Generator.Basic import Mathlib.CategoryTheory.Limits.ConeCategory import Mathlib.CategoryTheory.Limits.Constructions.WeaklyInitial import Mathlib.CategoryTheory.Limits.FunctorCategory.Basic import Mathlib.CategoryTheory.Subobject.Comma /-! # Adjoint functor theorem This file proves the (general) adjoint functor theorem, in the form: * If `G : D ⥤ C` preserves limits and `D` has limits, and satisfies the solution set condition, then it has a left adjoint: `isRightAdjointOfPreservesLimitsOfIsCoseparating`. We show that the converse holds, i.e. that if `G` has a left adjoint then it satisfies the solution set condition, see `solutionSetCondition_of_isRightAdjoint` (the file `CategoryTheory/Adjunction/Limits` already shows it preserves limits). We define the *solution set condition* for the functor `G : D ⥤ C` to mean, for every object `A : C`, there is a set-indexed family ${f_i : A ⟶ G (B_i)}$ such that any morphism `A ⟶ G X` factors through one of the `f_i`. This file also proves the special adjoint functor theorem, in the form: * If `G : D ⥤ C` preserves limits and `D` is complete, well-powered and has a small coseparating set, then `G` has a left adjoint: `isRightAdjointOfPreservesLimitsOfIsCoseparating` Finally, we prove the following corollaries of the special adjoint functor theorem: * If `C` is complete, well-powered and has a small coseparating set, then it is cocomplete: `hasColimits_of_hasLimits_of_isCoseparating`, `hasColimits_of_hasLimits_of_hasCoseparator` * If `C` is cocomplete, co-well-powered and has a small separating set, then it is complete: `hasLimits_of_hasColimits_of_isSeparating`, `hasLimits_of_hasColimits_of_hasSeparator` -/ universe v u u' namespace CategoryTheory open Limits variable {J : Type v} variable {C : Type u} [Category.{v} C] /-- The functor `G : D ⥤ C` satisfies the *solution set condition* if for every `A : C`, there is a family of morphisms `{f_i : A ⟶ G (B_i) // i ∈ ι}` such that given any morphism `h : A ⟶ G X`, there is some `i ∈ ι` such that `h` factors through `f_i`. The key part of this definition is that the indexing set `ι` lives in `Type v`, where `v` is the universe of morphisms of the category: this is the "smallness" condition which allows the general adjoint functor theorem to go through. -/ def SolutionSetCondition {D : Type u} [Category.{v} D] (G : D ⥤ C) : Prop := ∀ A : C, ∃ (ι : Type v) (B : ι → D) (f : ∀ i : ι, A ⟶ G.obj (B i)), ∀ (X) (h : A ⟶ G.obj X), ∃ (i : ι) (g : B i ⟶ X), f i ≫ G.map g = h section GeneralAdjointFunctorTheorem variable {D : Type u} [Category.{v} D] variable (G : D ⥤ C) /-- If `G : D ⥤ C` is a right adjoint it satisfies the solution set condition. -/
theorem solutionSetCondition_of_isRightAdjoint [G.IsRightAdjoint] : SolutionSetCondition G := by intro A refine ⟨PUnit, fun _ => G.leftAdjoint.obj A, fun _ => (Adjunction.ofIsRightAdjoint G).unit.app A, ?_⟩ intro B h refine ⟨PUnit.unit, ((Adjunction.ofIsRightAdjoint G).homEquiv _ _).symm h, ?_⟩ rw [← Adjunction.homEquiv_unit, Equiv.apply_symm_apply]
Mathlib/CategoryTheory/Adjunction/AdjointFunctorTheorems.lean
69
75
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Julian Kuelshammer -/ import Mathlib.Algebra.CharP.Defs import Mathlib.Algebra.Group.Commute.Basic import Mathlib.Algebra.Group.Pointwise.Set.Finite import Mathlib.Algebra.Group.Subgroup.Finite import Mathlib.Algebra.Module.NatInt import Mathlib.Algebra.Order.Group.Action import Mathlib.Algebra.Order.Ring.Abs import Mathlib.Data.Int.ModEq import Mathlib.Dynamics.PeriodicPts.Lemmas import Mathlib.GroupTheory.Index import Mathlib.NumberTheory.Divisors import Mathlib.Order.Interval.Set.Infinite /-! # Order of an element This file defines the order of an element of a finite group. For a finite group `G` the order of `x ∈ G` is the minimal `n ≥ 1` such that `x ^ n = 1`. ## Main definitions * `IsOfFinOrder` is a predicate on an element `x` of a monoid `G` saying that `x` is of finite order. * `IsOfFinAddOrder` is the additive analogue of `IsOfFinOrder`. * `orderOf x` defines the order of an element `x` of a monoid `G`, by convention its value is `0` if `x` has infinite order. * `addOrderOf` is the additive analogue of `orderOf`. ## Tags order of an element -/ assert_not_exists Field open Function Fintype Nat Pointwise Subgroup Submonoid open scoped Finset variable {G H A α β : Type*} section Monoid variable [Monoid G] {a b x y : G} {n m : ℕ} section IsOfFinOrder -- Porting note (https://github.com/leanprover-community/mathlib4/issues/12129): additional beta reduction needed @[to_additive] theorem isPeriodicPt_mul_iff_pow_eq_one (x : G) : IsPeriodicPt (x * ·) n 1 ↔ x ^ n = 1 := by rw [IsPeriodicPt, IsFixedPt, mul_left_iterate]; beta_reduce; rw [mul_one] /-- `IsOfFinOrder` is a predicate on an element `x` of a monoid to be of finite order, i.e. there exists `n ≥ 1` such that `x ^ n = 1`. -/ @[to_additive "`IsOfFinAddOrder` is a predicate on an element `a` of an additive monoid to be of finite order, i.e. there exists `n ≥ 1` such that `n • a = 0`."] def IsOfFinOrder (x : G) : Prop := (1 : G) ∈ periodicPts (x * ·) theorem isOfFinAddOrder_ofMul_iff : IsOfFinAddOrder (Additive.ofMul x) ↔ IsOfFinOrder x := Iff.rfl theorem isOfFinOrder_ofAdd_iff {α : Type*} [AddMonoid α] {x : α} : IsOfFinOrder (Multiplicative.ofAdd x) ↔ IsOfFinAddOrder x := Iff.rfl @[to_additive] theorem isOfFinOrder_iff_pow_eq_one : IsOfFinOrder x ↔ ∃ n, 0 < n ∧ x ^ n = 1 := by simp [IsOfFinOrder, mem_periodicPts, isPeriodicPt_mul_iff_pow_eq_one] @[to_additive] alias ⟨IsOfFinOrder.exists_pow_eq_one, _⟩ := isOfFinOrder_iff_pow_eq_one @[to_additive] lemma isOfFinOrder_iff_zpow_eq_one {G} [DivisionMonoid G] {x : G} : IsOfFinOrder x ↔ ∃ (n : ℤ), n ≠ 0 ∧ x ^ n = 1 := by rw [isOfFinOrder_iff_pow_eq_one] refine ⟨fun ⟨n, hn, hn'⟩ ↦ ⟨n, Int.natCast_ne_zero_iff_pos.mpr hn, zpow_natCast x n ▸ hn'⟩, fun ⟨n, hn, hn'⟩ ↦ ⟨n.natAbs, Int.natAbs_pos.mpr hn, ?_⟩⟩ rcases (Int.natAbs_eq_iff (a := n)).mp rfl with h | h · rwa [h, zpow_natCast] at hn' · rwa [h, zpow_neg, inv_eq_one, zpow_natCast] at hn' /-- See also `injective_pow_iff_not_isOfFinOrder`. -/ @[to_additive "See also `injective_nsmul_iff_not_isOfFinAddOrder`."] theorem not_isOfFinOrder_of_injective_pow {x : G} (h : Injective fun n : ℕ => x ^ n) : ¬IsOfFinOrder x := by simp_rw [isOfFinOrder_iff_pow_eq_one, not_exists, not_and] intro n hn_pos hnx rw [← pow_zero x] at hnx rw [h hnx] at hn_pos exact irrefl 0 hn_pos /-- 1 is of finite order in any monoid. -/ @[to_additive (attr := simp) "0 is of finite order in any additive monoid."] theorem IsOfFinOrder.one : IsOfFinOrder (1 : G) := isOfFinOrder_iff_pow_eq_one.mpr ⟨1, Nat.one_pos, one_pow 1⟩ @[to_additive] lemma IsOfFinOrder.pow {n : ℕ} : IsOfFinOrder a → IsOfFinOrder (a ^ n) := by simp_rw [isOfFinOrder_iff_pow_eq_one] rintro ⟨m, hm, ha⟩ exact ⟨m, hm, by simp [pow_right_comm _ n, ha]⟩ @[to_additive] lemma IsOfFinOrder.of_pow {n : ℕ} (h : IsOfFinOrder (a ^ n)) (hn : n ≠ 0) : IsOfFinOrder a := by rw [isOfFinOrder_iff_pow_eq_one] at * rcases h with ⟨m, hm, ha⟩ exact ⟨n * m, mul_pos hn.bot_lt hm, by rwa [pow_mul]⟩ @[to_additive (attr := simp)] lemma isOfFinOrder_pow {n : ℕ} : IsOfFinOrder (a ^ n) ↔ IsOfFinOrder a ∨ n = 0 := by rcases Decidable.eq_or_ne n 0 with rfl | hn · simp · exact ⟨fun h ↦ .inl <| h.of_pow hn, fun h ↦ (h.resolve_right hn).pow⟩ /-- Elements of finite order are of finite order in submonoids. -/ @[to_additive "Elements of finite order are of finite order in submonoids."] theorem Submonoid.isOfFinOrder_coe {H : Submonoid G} {x : H} : IsOfFinOrder (x : G) ↔ IsOfFinOrder x := by rw [isOfFinOrder_iff_pow_eq_one, isOfFinOrder_iff_pow_eq_one] norm_cast theorem IsConj.isOfFinOrder (h : IsConj x y) : IsOfFinOrder x → IsOfFinOrder y := by simp_rw [isOfFinOrder_iff_pow_eq_one] rintro ⟨n, n_gt_0, eq'⟩ exact ⟨n, n_gt_0, by rw [← isConj_one_right, ← eq']; exact h.pow n⟩ /-- The image of an element of finite order has finite order. -/ @[to_additive "The image of an element of finite additive order has finite additive order."] theorem MonoidHom.isOfFinOrder [Monoid H] (f : G →* H) {x : G} (h : IsOfFinOrder x) : IsOfFinOrder <| f x := isOfFinOrder_iff_pow_eq_one.mpr <| by obtain ⟨n, npos, hn⟩ := h.exists_pow_eq_one exact ⟨n, npos, by rw [← f.map_pow, hn, f.map_one]⟩ /-- If a direct product has finite order then so does each component. -/ @[to_additive "If a direct product has finite additive order then so does each component."] theorem IsOfFinOrder.apply {η : Type*} {Gs : η → Type*} [∀ i, Monoid (Gs i)] {x : ∀ i, Gs i} (h : IsOfFinOrder x) : ∀ i, IsOfFinOrder (x i) := by obtain ⟨n, npos, hn⟩ := h.exists_pow_eq_one exact fun _ => isOfFinOrder_iff_pow_eq_one.mpr ⟨n, npos, (congr_fun hn.symm _).symm⟩ /-- The submonoid generated by an element is a group if that element has finite order. -/ @[to_additive "The additive submonoid generated by an element is an additive group if that element has finite order."] noncomputable abbrev IsOfFinOrder.groupPowers (hx : IsOfFinOrder x) : Group (Submonoid.powers x) := by obtain ⟨hpos, hx⟩ := hx.exists_pow_eq_one.choose_spec exact Submonoid.groupPowers hpos hx end IsOfFinOrder /-- `orderOf x` is the order of the element `x`, i.e. the `n ≥ 1`, s.t. `x ^ n = 1` if it exists. Otherwise, i.e. if `x` is of infinite order, then `orderOf x` is `0` by convention. -/ @[to_additive "`addOrderOf a` is the order of the element `a`, i.e. the `n ≥ 1`, s.t. `n • a = 0` if it exists. Otherwise, i.e. if `a` is of infinite order, then `addOrderOf a` is `0` by convention."] noncomputable def orderOf (x : G) : ℕ := minimalPeriod (x * ·) 1 @[simp] theorem addOrderOf_ofMul_eq_orderOf (x : G) : addOrderOf (Additive.ofMul x) = orderOf x := rfl @[simp] lemma orderOf_ofAdd_eq_addOrderOf {α : Type*} [AddMonoid α] (a : α) : orderOf (Multiplicative.ofAdd a) = addOrderOf a := rfl @[to_additive] protected lemma IsOfFinOrder.orderOf_pos (h : IsOfFinOrder x) : 0 < orderOf x := minimalPeriod_pos_of_mem_periodicPts h @[to_additive addOrderOf_nsmul_eq_zero] theorem pow_orderOf_eq_one (x : G) : x ^ orderOf x = 1 := by convert Eq.trans _ (isPeriodicPt_minimalPeriod (x * ·) 1) -- Porting note (https://github.com/leanprover-community/mathlib4/issues/12129): additional beta reduction needed in the middle of the rewrite rw [orderOf, mul_left_iterate]; beta_reduce; rw [mul_one] @[to_additive] theorem orderOf_eq_zero (h : ¬IsOfFinOrder x) : orderOf x = 0 := by rwa [orderOf, minimalPeriod, dif_neg] @[to_additive] theorem orderOf_eq_zero_iff : orderOf x = 0 ↔ ¬IsOfFinOrder x := ⟨fun h H ↦ H.orderOf_pos.ne' h, orderOf_eq_zero⟩ @[to_additive] theorem orderOf_eq_zero_iff' : orderOf x = 0 ↔ ∀ n : ℕ, 0 < n → x ^ n ≠ 1 := by simp_rw [orderOf_eq_zero_iff, isOfFinOrder_iff_pow_eq_one, not_exists, not_and] @[to_additive] theorem orderOf_eq_iff {n} (h : 0 < n) : orderOf x = n ↔ x ^ n = 1 ∧ ∀ m, m < n → 0 < m → x ^ m ≠ 1 := by simp_rw [Ne, ← isPeriodicPt_mul_iff_pow_eq_one, orderOf, minimalPeriod] split_ifs with h1 · classical rw [find_eq_iff] simp only [h, true_and] push_neg rfl · rw [iff_false_left h.ne] rintro ⟨h', -⟩ exact h1 ⟨n, h, h'⟩ /-- A group element has finite order iff its order is positive. -/ @[to_additive "A group element has finite additive order iff its order is positive."] theorem orderOf_pos_iff : 0 < orderOf x ↔ IsOfFinOrder x := by rw [iff_not_comm.mp orderOf_eq_zero_iff, pos_iff_ne_zero] @[to_additive] theorem IsOfFinOrder.mono [Monoid β] {y : β} (hx : IsOfFinOrder x) (h : orderOf y ∣ orderOf x) : IsOfFinOrder y := by rw [← orderOf_pos_iff] at hx ⊢; exact Nat.pos_of_dvd_of_pos h hx @[to_additive] theorem pow_ne_one_of_lt_orderOf (n0 : n ≠ 0) (h : n < orderOf x) : x ^ n ≠ 1 := fun j => not_isPeriodicPt_of_pos_of_lt_minimalPeriod n0 h ((isPeriodicPt_mul_iff_pow_eq_one x).mpr j) @[to_additive] theorem orderOf_le_of_pow_eq_one (hn : 0 < n) (h : x ^ n = 1) : orderOf x ≤ n := IsPeriodicPt.minimalPeriod_le hn (by rwa [isPeriodicPt_mul_iff_pow_eq_one]) @[to_additive (attr := simp)] theorem orderOf_one : orderOf (1 : G) = 1 := by rw [orderOf, ← minimalPeriod_id (x := (1 : G)), ← one_mul_eq_id] @[to_additive (attr := simp) AddMonoid.addOrderOf_eq_one_iff] theorem orderOf_eq_one_iff : orderOf x = 1 ↔ x = 1 := by rw [orderOf, minimalPeriod_eq_one_iff_isFixedPt, IsFixedPt, mul_one] @[to_additive (attr := simp) mod_addOrderOf_nsmul] lemma pow_mod_orderOf (x : G) (n : ℕ) : x ^ (n % orderOf x) = x ^ n := calc x ^ (n % orderOf x) = x ^ (n % orderOf x + orderOf x * (n / orderOf x)) := by simp [pow_add, pow_mul, pow_orderOf_eq_one] _ = x ^ n := by rw [Nat.mod_add_div] @[to_additive] theorem orderOf_dvd_of_pow_eq_one (h : x ^ n = 1) : orderOf x ∣ n := IsPeriodicPt.minimalPeriod_dvd ((isPeriodicPt_mul_iff_pow_eq_one _).mpr h) @[to_additive] theorem orderOf_dvd_iff_pow_eq_one {n : ℕ} : orderOf x ∣ n ↔ x ^ n = 1 := ⟨fun h => by rw [← pow_mod_orderOf, Nat.mod_eq_zero_of_dvd h, _root_.pow_zero], orderOf_dvd_of_pow_eq_one⟩ @[to_additive addOrderOf_smul_dvd] theorem orderOf_pow_dvd (n : ℕ) : orderOf (x ^ n) ∣ orderOf x := by rw [orderOf_dvd_iff_pow_eq_one, pow_right_comm, pow_orderOf_eq_one, one_pow] @[to_additive] lemma pow_injOn_Iio_orderOf : (Set.Iio <| orderOf x).InjOn (x ^ ·) := by simpa only [mul_left_iterate, mul_one] using iterate_injOn_Iio_minimalPeriod (f := (x * ·)) (x := 1) @[to_additive] protected lemma IsOfFinOrder.mem_powers_iff_mem_range_orderOf [DecidableEq G] (hx : IsOfFinOrder x) : y ∈ Submonoid.powers x ↔ y ∈ (Finset.range (orderOf x)).image (x ^ ·) := Finset.mem_range_iff_mem_finset_range_of_mod_eq' hx.orderOf_pos <| pow_mod_orderOf _ @[to_additive] protected lemma IsOfFinOrder.powers_eq_image_range_orderOf [DecidableEq G] (hx : IsOfFinOrder x) : (Submonoid.powers x : Set G) = (Finset.range (orderOf x)).image (x ^ ·) := Set.ext fun _ ↦ hx.mem_powers_iff_mem_range_orderOf @[to_additive] theorem pow_eq_one_iff_modEq : x ^ n = 1 ↔ n ≡ 0 [MOD orderOf x] := by rw [modEq_zero_iff_dvd, orderOf_dvd_iff_pow_eq_one] @[to_additive] theorem orderOf_map_dvd {H : Type*} [Monoid H] (ψ : G →* H) (x : G) : orderOf (ψ x) ∣ orderOf x := by apply orderOf_dvd_of_pow_eq_one rw [← map_pow, pow_orderOf_eq_one] apply map_one @[to_additive] theorem exists_pow_eq_self_of_coprime (h : n.Coprime (orderOf x)) : ∃ m : ℕ, (x ^ n) ^ m = x := by by_cases h0 : orderOf x = 0 · rw [h0, coprime_zero_right] at h exact ⟨1, by rw [h, pow_one, pow_one]⟩ by_cases h1 : orderOf x = 1 · exact ⟨0, by rw [orderOf_eq_one_iff.mp h1, one_pow, one_pow]⟩ obtain ⟨m, h⟩ := exists_mul_emod_eq_one_of_coprime h (one_lt_iff_ne_zero_and_ne_one.mpr ⟨h0, h1⟩) exact ⟨m, by rw [← pow_mul, ← pow_mod_orderOf, h, pow_one]⟩ /-- If `x^n = 1`, but `x^(n/p) ≠ 1` for all prime factors `p` of `n`, then `x` has order `n` in `G`. -/ @[to_additive addOrderOf_eq_of_nsmul_and_div_prime_nsmul "If `n * x = 0`, but `n/p * x ≠ 0` for all prime factors `p` of `n`, then `x` has order `n` in `G`."] theorem orderOf_eq_of_pow_and_pow_div_prime (hn : 0 < n) (hx : x ^ n = 1) (hd : ∀ p : ℕ, p.Prime → p ∣ n → x ^ (n / p) ≠ 1) : orderOf x = n := by -- Let `a` be `n/(orderOf x)`, and show `a = 1` obtain ⟨a, ha⟩ := exists_eq_mul_right_of_dvd (orderOf_dvd_of_pow_eq_one hx) suffices a = 1 by simp [this, ha] -- Assume `a` is not one... by_contra h have a_min_fac_dvd_p_sub_one : a.minFac ∣ n := by obtain ⟨b, hb⟩ : ∃ b : ℕ, a = b * a.minFac := exists_eq_mul_left_of_dvd a.minFac_dvd rw [hb, ← mul_assoc] at ha exact Dvd.intro_left (orderOf x * b) ha.symm -- Use the minimum prime factor of `a` as `p`. refine hd a.minFac (Nat.minFac_prime h) a_min_fac_dvd_p_sub_one ?_ rw [← orderOf_dvd_iff_pow_eq_one, Nat.dvd_div_iff_mul_dvd a_min_fac_dvd_p_sub_one, ha, mul_comm, Nat.mul_dvd_mul_iff_left (IsOfFinOrder.orderOf_pos _)] · exact Nat.minFac_dvd a · rw [isOfFinOrder_iff_pow_eq_one] exact Exists.intro n (id ⟨hn, hx⟩) @[to_additive] theorem orderOf_eq_orderOf_iff {H : Type*} [Monoid H] {y : H} : orderOf x = orderOf y ↔ ∀ n : ℕ, x ^ n = 1 ↔ y ^ n = 1 := by simp_rw [← isPeriodicPt_mul_iff_pow_eq_one, ← minimalPeriod_eq_minimalPeriod_iff, orderOf] /-- An injective homomorphism of monoids preserves orders of elements. -/ @[to_additive "An injective homomorphism of additive monoids preserves orders of elements."] theorem orderOf_injective {H : Type*} [Monoid H] (f : G →* H) (hf : Function.Injective f) (x : G) : orderOf (f x) = orderOf x := by simp_rw [orderOf_eq_orderOf_iff, ← f.map_pow, ← f.map_one, hf.eq_iff, forall_const] /-- A multiplicative equivalence preserves orders of elements. -/ @[to_additive (attr := simp) "An additive equivalence preserves orders of elements."] lemma MulEquiv.orderOf_eq {H : Type*} [Monoid H] (e : G ≃* H) (x : G) : orderOf (e x) = orderOf x := orderOf_injective e.toMonoidHom e.injective x @[to_additive] theorem Function.Injective.isOfFinOrder_iff [Monoid H] {f : G →* H} (hf : Injective f) : IsOfFinOrder (f x) ↔ IsOfFinOrder x := by rw [← orderOf_pos_iff, orderOf_injective f hf x, ← orderOf_pos_iff] @[to_additive (attr := norm_cast, simp)] theorem orderOf_submonoid {H : Submonoid G} (y : H) : orderOf (y : G) = orderOf y := orderOf_injective H.subtype Subtype.coe_injective y @[to_additive] theorem orderOf_units {y : Gˣ} : orderOf (y : G) = orderOf y := orderOf_injective (Units.coeHom G) Units.ext y /-- If the order of `x` is finite, then `x` is a unit with inverse `x ^ (orderOf x - 1)`. -/ @[to_additive (attr := simps) "If the additive order of `x` is finite, then `x` is an additive unit with inverse `(addOrderOf x - 1) • x`. "] noncomputable def IsOfFinOrder.unit {M} [Monoid M] {x : M} (hx : IsOfFinOrder x) : Mˣ := ⟨x, x ^ (orderOf x - 1), by rw [← _root_.pow_succ', tsub_add_cancel_of_le (by exact hx.orderOf_pos), pow_orderOf_eq_one], by rw [← _root_.pow_succ, tsub_add_cancel_of_le (by exact hx.orderOf_pos), pow_orderOf_eq_one]⟩ @[to_additive] lemma IsOfFinOrder.isUnit {M} [Monoid M] {x : M} (hx : IsOfFinOrder x) : IsUnit x := ⟨hx.unit, rfl⟩ variable (x) @[to_additive] theorem orderOf_pow' (h : n ≠ 0) : orderOf (x ^ n) = orderOf x / gcd (orderOf x) n := by unfold orderOf rw [← minimalPeriod_iterate_eq_div_gcd h, mul_left_iterate] @[to_additive] lemma orderOf_pow_of_dvd {x : G} {n : ℕ} (hn : n ≠ 0) (dvd : n ∣ orderOf x) : orderOf (x ^ n) = orderOf x / n := by rw [orderOf_pow' _ hn, Nat.gcd_eq_right dvd] @[to_additive] lemma orderOf_pow_orderOf_div {x : G} {n : ℕ} (hx : orderOf x ≠ 0) (hn : n ∣ orderOf x) : orderOf (x ^ (orderOf x / n)) = n := by rw [orderOf_pow_of_dvd _ (Nat.div_dvd_of_dvd hn), Nat.div_div_self hn hx] rw [← Nat.div_mul_cancel hn] at hx; exact left_ne_zero_of_mul hx variable (n) @[to_additive] protected lemma IsOfFinOrder.orderOf_pow (h : IsOfFinOrder x) : orderOf (x ^ n) = orderOf x / gcd (orderOf x) n := by unfold orderOf rw [← minimalPeriod_iterate_eq_div_gcd' h, mul_left_iterate] @[to_additive] lemma Nat.Coprime.orderOf_pow (h : (orderOf y).Coprime m) : orderOf (y ^ m) = orderOf y := by by_cases hg : IsOfFinOrder y · rw [hg.orderOf_pow y m , h.gcd_eq_one, Nat.div_one] · rw [m.coprime_zero_left.1 (orderOf_eq_zero hg ▸ h), pow_one] @[to_additive] lemma IsOfFinOrder.natCard_powers_le_orderOf (ha : IsOfFinOrder a) : Nat.card (powers a : Set G) ≤ orderOf a := by classical simpa [ha.powers_eq_image_range_orderOf, Finset.card_range, Nat.Iio_eq_range] using Finset.card_image_le (s := Finset.range (orderOf a)) @[to_additive] lemma IsOfFinOrder.finite_powers (ha : IsOfFinOrder a) : (powers a : Set G).Finite := by classical rw [ha.powers_eq_image_range_orderOf]; exact Finset.finite_toSet _ namespace Commute variable {x} @[to_additive] theorem orderOf_mul_dvd_lcm (h : Commute x y) : orderOf (x * y) ∣ Nat.lcm (orderOf x) (orderOf y) := by rw [orderOf, ← comp_mul_left] exact Function.Commute.minimalPeriod_of_comp_dvd_lcm h.function_commute_mul_left @[to_additive] theorem orderOf_dvd_lcm_mul (h : Commute x y): orderOf y ∣ Nat.lcm (orderOf x) (orderOf (x * y)) := by by_cases h0 : orderOf x = 0 · rw [h0, lcm_zero_left] apply dvd_zero conv_lhs => rw [← one_mul y, ← pow_orderOf_eq_one x, ← succ_pred_eq_of_pos (Nat.pos_of_ne_zero h0), _root_.pow_succ, mul_assoc] exact (((Commute.refl x).mul_right h).pow_left _).orderOf_mul_dvd_lcm.trans (lcm_dvd_iff.2 ⟨(orderOf_pow_dvd _).trans (dvd_lcm_left _ _), dvd_lcm_right _ _⟩) @[to_additive addOrderOf_add_dvd_mul_addOrderOf] theorem orderOf_mul_dvd_mul_orderOf (h : Commute x y): orderOf (x * y) ∣ orderOf x * orderOf y := dvd_trans h.orderOf_mul_dvd_lcm (lcm_dvd_mul _ _) @[to_additive addOrderOf_add_eq_mul_addOrderOf_of_coprime] theorem orderOf_mul_eq_mul_orderOf_of_coprime (h : Commute x y) (hco : (orderOf x).Coprime (orderOf y)) : orderOf (x * y) = orderOf x * orderOf y := by rw [orderOf, ← comp_mul_left] exact h.function_commute_mul_left.minimalPeriod_of_comp_eq_mul_of_coprime hco /-- Commuting elements of finite order are closed under multiplication. -/ @[to_additive "Commuting elements of finite additive order are closed under addition."] theorem isOfFinOrder_mul (h : Commute x y) (hx : IsOfFinOrder x) (hy : IsOfFinOrder y) : IsOfFinOrder (x * y) := orderOf_pos_iff.mp <| pos_of_dvd_of_pos h.orderOf_mul_dvd_mul_orderOf <| mul_pos hx.orderOf_pos hy.orderOf_pos /-- If each prime factor of `orderOf x` has higher multiplicity in `orderOf y`, and `x` commutes with `y`, then `x * y` has the same order as `y`. -/ @[to_additive addOrderOf_add_eq_right_of_forall_prime_mul_dvd "If each prime factor of `addOrderOf x` has higher multiplicity in `addOrderOf y`, and `x` commutes with `y`, then `x + y` has the same order as `y`."] theorem orderOf_mul_eq_right_of_forall_prime_mul_dvd (h : Commute x y) (hy : IsOfFinOrder y) (hdvd : ∀ p : ℕ, p.Prime → p ∣ orderOf x → p * orderOf x ∣ orderOf y) : orderOf (x * y) = orderOf y := by have hoy := hy.orderOf_pos have hxy := dvd_of_forall_prime_mul_dvd hdvd apply orderOf_eq_of_pow_and_pow_div_prime hoy <;> simp only [Ne, ← orderOf_dvd_iff_pow_eq_one] · exact h.orderOf_mul_dvd_lcm.trans (lcm_dvd hxy dvd_rfl) refine fun p hp hpy hd => hp.ne_one ?_ rw [← Nat.dvd_one, ← mul_dvd_mul_iff_right hoy.ne', one_mul, ← dvd_div_iff_mul_dvd hpy] refine (orderOf_dvd_lcm_mul h).trans (lcm_dvd ((dvd_div_iff_mul_dvd hpy).2 ?_) hd) by_cases h : p ∣ orderOf x exacts [hdvd p hp h, (hp.coprime_iff_not_dvd.2 h).mul_dvd_of_dvd_of_dvd hpy hxy] end Commute section PPrime variable {x n} {p : ℕ} [hp : Fact p.Prime] @[to_additive] theorem orderOf_eq_prime_iff : orderOf x = p ↔ x ^ p = 1 ∧ x ≠ 1 := by rw [orderOf, minimalPeriod_eq_prime_iff, isPeriodicPt_mul_iff_pow_eq_one, IsFixedPt, mul_one] /-- The backward direction of `orderOf_eq_prime_iff`. -/ @[to_additive "The backward direction of `addOrderOf_eq_prime_iff`."] theorem orderOf_eq_prime (hg : x ^ p = 1) (hg1 : x ≠ 1) : orderOf x = p := orderOf_eq_prime_iff.mpr ⟨hg, hg1⟩ @[to_additive addOrderOf_eq_prime_pow] theorem orderOf_eq_prime_pow (hnot : ¬x ^ p ^ n = 1) (hfin : x ^ p ^ (n + 1) = 1) : orderOf x = p ^ (n + 1) := by apply minimalPeriod_eq_prime_pow <;> rwa [isPeriodicPt_mul_iff_pow_eq_one] @[to_additive exists_addOrderOf_eq_prime_pow_iff] theorem exists_orderOf_eq_prime_pow_iff : (∃ k : ℕ, orderOf x = p ^ k) ↔ ∃ m : ℕ, x ^ (p : ℕ) ^ m = 1 := ⟨fun ⟨k, hk⟩ => ⟨k, by rw [← hk, pow_orderOf_eq_one]⟩, fun ⟨_, hm⟩ => by obtain ⟨k, _, hk⟩ := (Nat.dvd_prime_pow hp.elim).mp (orderOf_dvd_of_pow_eq_one hm) exact ⟨k, hk⟩⟩ end PPrime /-- The equivalence between `Fin (orderOf x)` and `Submonoid.powers x`, sending `i` to `x ^ i` -/ @[to_additive "The equivalence between `Fin (addOrderOf a)` and `AddSubmonoid.multiples a`, sending `i` to `i • a`"] noncomputable def finEquivPowers {x : G} (hx : IsOfFinOrder x) : Fin (orderOf x) ≃ powers x := Equiv.ofBijective (fun n ↦ ⟨x ^ (n : ℕ), ⟨n, rfl⟩⟩) ⟨fun ⟨_, h₁⟩ ⟨_, h₂⟩ ij ↦ Fin.ext (pow_injOn_Iio_orderOf h₁ h₂ (Subtype.mk_eq_mk.1 ij)), fun ⟨_, i, rfl⟩ ↦ ⟨⟨i % orderOf x, mod_lt _ hx.orderOf_pos⟩, Subtype.eq <| pow_mod_orderOf _ _⟩⟩ @[to_additive (attr := simp)] lemma finEquivPowers_apply {x : G} (hx : IsOfFinOrder x) {n : Fin (orderOf x)} : finEquivPowers hx n = ⟨x ^ (n : ℕ), n, rfl⟩ := rfl @[to_additive (attr := simp)] lemma finEquivPowers_symm_apply {x : G} (hx : IsOfFinOrder x) (n : ℕ) : (finEquivPowers hx).symm ⟨x ^ n, _, rfl⟩ = ⟨n % orderOf x, Nat.mod_lt _ hx.orderOf_pos⟩ := by rw [Equiv.symm_apply_eq, finEquivPowers_apply, Subtype.mk_eq_mk, ← pow_mod_orderOf, Fin.val_mk] variable {x n} (hx : IsOfFinOrder x) include hx @[to_additive] theorem IsOfFinOrder.pow_eq_pow_iff_modEq : x ^ n = x ^ m ↔ n ≡ m [MOD orderOf x] := by wlog hmn : m ≤ n generalizing m n · rw [eq_comm, ModEq.comm, this (le_of_not_le hmn)] obtain ⟨k, rfl⟩ := Nat.exists_eq_add_of_le hmn rw [pow_add, (hx.isUnit.pow _).mul_eq_left, pow_eq_one_iff_modEq] exact ⟨fun h ↦ Nat.ModEq.add_left _ h, fun h ↦ Nat.ModEq.add_left_cancel' _ h⟩ @[to_additive] lemma IsOfFinOrder.pow_inj_mod {n m : ℕ} : x ^ n = x ^ m ↔ n % orderOf x = m % orderOf x := hx.pow_eq_pow_iff_modEq end Monoid section CancelMonoid variable [LeftCancelMonoid G] {x y : G} {a : G} {m n : ℕ} @[to_additive] theorem pow_eq_pow_iff_modEq : x ^ n = x ^ m ↔ n ≡ m [MOD orderOf x] := by wlog hmn : m ≤ n generalizing m n · rw [eq_comm, ModEq.comm, this (le_of_not_le hmn)] obtain ⟨k, rfl⟩ := Nat.exists_eq_add_of_le hmn rw [← mul_one (x ^ m), pow_add, mul_left_cancel_iff, pow_eq_one_iff_modEq] exact ⟨fun h => Nat.ModEq.add_left _ h, fun h => Nat.ModEq.add_left_cancel' _ h⟩ @[to_additive (attr := simp)] lemma injective_pow_iff_not_isOfFinOrder : Injective (fun n : ℕ ↦ x ^ n) ↔ ¬IsOfFinOrder x := by refine ⟨fun h => not_isOfFinOrder_of_injective_pow h, fun h n m hnm => ?_⟩ rwa [pow_eq_pow_iff_modEq, orderOf_eq_zero_iff.mpr h, modEq_zero_iff] at hnm @[to_additive] lemma pow_inj_mod {n m : ℕ} : x ^ n = x ^ m ↔ n % orderOf x = m % orderOf x := pow_eq_pow_iff_modEq @[to_additive] theorem pow_inj_iff_of_orderOf_eq_zero (h : orderOf x = 0) {n m : ℕ} : x ^ n = x ^ m ↔ n = m := by rw [pow_eq_pow_iff_modEq, h, modEq_zero_iff] @[to_additive] theorem infinite_not_isOfFinOrder {x : G} (h : ¬IsOfFinOrder x) : { y : G | ¬IsOfFinOrder y }.Infinite := by let s := { n | 0 < n }.image fun n : ℕ => x ^ n have hs : s ⊆ { y : G | ¬IsOfFinOrder y } := by rintro - ⟨n, hn : 0 < n, rfl⟩ (contra : IsOfFinOrder (x ^ n)) apply h rw [isOfFinOrder_iff_pow_eq_one] at contra ⊢ obtain ⟨m, hm, hm'⟩ := contra exact ⟨n * m, mul_pos hn hm, by rwa [pow_mul]⟩ suffices s.Infinite by exact this.mono hs contrapose! h have : ¬Injective fun n : ℕ => x ^ n := by have := Set.not_injOn_infinite_finite_image (Set.Ioi_infinite 0) (Set.not_infinite.mp h) contrapose! this exact Set.injOn_of_injective this rwa [injective_pow_iff_not_isOfFinOrder, Classical.not_not] at this @[to_additive (attr := simp)] lemma finite_powers : (powers a : Set G).Finite ↔ IsOfFinOrder a := by refine ⟨fun h ↦ ?_, IsOfFinOrder.finite_powers⟩ obtain ⟨m, n, hmn, ha⟩ := h.exists_lt_map_eq_of_forall_mem (f := fun n : ℕ ↦ a ^ n) (fun n ↦ by simp [mem_powers_iff]) refine isOfFinOrder_iff_pow_eq_one.2 ⟨n - m, tsub_pos_iff_lt.2 hmn, ?_⟩ rw [← mul_left_cancel_iff (a := a ^ m), ← pow_add, add_tsub_cancel_of_le hmn.le, ha, mul_one] @[to_additive (attr := simp)] lemma infinite_powers : (powers a : Set G).Infinite ↔ ¬ IsOfFinOrder a := finite_powers.not /-- See also `orderOf_eq_card_powers`. -/ @[to_additive "See also `addOrder_eq_card_multiples`."] lemma Nat.card_submonoidPowers : Nat.card (powers a) = orderOf a := by classical by_cases ha : IsOfFinOrder a · exact (Nat.card_congr (finEquivPowers ha).symm).trans <| by simp · have := (infinite_powers.2 ha).to_subtype rw [orderOf_eq_zero ha, Nat.card_eq_zero_of_infinite] end CancelMonoid section Group variable [Group G] {x y : G} {i : ℤ} /-- Inverses of elements of finite order have finite order. -/ @[to_additive (attr := simp) "Inverses of elements of finite additive order have finite additive order."] theorem isOfFinOrder_inv_iff {x : G} : IsOfFinOrder x⁻¹ ↔ IsOfFinOrder x := by simp [isOfFinOrder_iff_pow_eq_one] @[to_additive] alias ⟨IsOfFinOrder.of_inv, IsOfFinOrder.inv⟩ := isOfFinOrder_inv_iff @[to_additive] theorem orderOf_dvd_iff_zpow_eq_one : (orderOf x : ℤ) ∣ i ↔ x ^ i = 1 := by rcases Int.eq_nat_or_neg i with ⟨i, rfl | rfl⟩ · rw [Int.natCast_dvd_natCast, orderOf_dvd_iff_pow_eq_one, zpow_natCast] · rw [dvd_neg, Int.natCast_dvd_natCast, zpow_neg, inv_eq_one, zpow_natCast, orderOf_dvd_iff_pow_eq_one] @[to_additive (attr := simp)] theorem orderOf_inv (x : G) : orderOf x⁻¹ = orderOf x := by simp [orderOf_eq_orderOf_iff] @[to_additive] theorem orderOf_dvd_sub_iff_zpow_eq_zpow {a b : ℤ} : (orderOf x : ℤ) ∣ a - b ↔ x ^ a = x ^ b := by rw [orderOf_dvd_iff_zpow_eq_one, zpow_sub, mul_inv_eq_one] namespace Subgroup variable {H : Subgroup G} @[to_additive (attr := norm_cast)] lemma orderOf_coe (a : H) : orderOf (a : G) = orderOf a := orderOf_injective H.subtype Subtype.coe_injective _ @[to_additive (attr := simp)] lemma orderOf_mk (a : G) (ha) : orderOf (⟨a, ha⟩ : H) = orderOf a := (orderOf_coe _).symm end Subgroup @[to_additive mod_addOrderOf_zsmul] lemma zpow_mod_orderOf (x : G) (z : ℤ) : x ^ (z % (orderOf x : ℤ)) = x ^ z := calc x ^ (z % (orderOf x : ℤ)) = x ^ (z % orderOf x + orderOf x * (z / orderOf x) : ℤ) := by simp [zpow_add, zpow_mul, pow_orderOf_eq_one] _ = x ^ z := by rw [Int.emod_add_ediv] @[to_additive (attr := simp) zsmul_smul_addOrderOf] theorem zpow_pow_orderOf : (x ^ i) ^ orderOf x = 1 := by by_cases h : IsOfFinOrder x · rw [← zpow_natCast, ← zpow_mul, mul_comm, zpow_mul, zpow_natCast, pow_orderOf_eq_one, one_zpow] · rw [orderOf_eq_zero h, _root_.pow_zero] @[to_additive] theorem IsOfFinOrder.zpow (h : IsOfFinOrder x) {i : ℤ} : IsOfFinOrder (x ^ i) := isOfFinOrder_iff_pow_eq_one.mpr ⟨orderOf x, h.orderOf_pos, zpow_pow_orderOf⟩ @[to_additive] theorem IsOfFinOrder.of_mem_zpowers (h : IsOfFinOrder x) (h' : y ∈ Subgroup.zpowers x) : IsOfFinOrder y := by obtain ⟨k, rfl⟩ := Subgroup.mem_zpowers_iff.mp h' exact h.zpow @[to_additive] theorem orderOf_dvd_of_mem_zpowers (h : y ∈ Subgroup.zpowers x) : orderOf y ∣ orderOf x := by obtain ⟨k, rfl⟩ := Subgroup.mem_zpowers_iff.mp h rw [orderOf_dvd_iff_pow_eq_one] exact zpow_pow_orderOf theorem smul_eq_self_of_mem_zpowers {α : Type*} [MulAction G α] (hx : x ∈ Subgroup.zpowers y) {a : α} (hs : y • a = a) : x • a = a := by obtain ⟨k, rfl⟩ := Subgroup.mem_zpowers_iff.mp hx rw [← MulAction.toPerm_apply, ← MulAction.toPermHom_apply, MonoidHom.map_zpow _ y k, MulAction.toPermHom_apply] exact Function.IsFixedPt.perm_zpow (by exact hs) k -- Porting note: help elab'n with `by exact` theorem vadd_eq_self_of_mem_zmultiples {α G : Type*} [AddGroup G] [AddAction G α] {x y : G} (hx : x ∈ AddSubgroup.zmultiples y) {a : α} (hs : y +ᵥ a = a) : x +ᵥ a = a := @smul_eq_self_of_mem_zpowers (Multiplicative G) _ _ _ α _ hx a hs attribute [to_additive existing] smul_eq_self_of_mem_zpowers @[to_additive] lemma IsOfFinOrder.mem_powers_iff_mem_zpowers (hx : IsOfFinOrder x) : y ∈ powers x ↔ y ∈ zpowers x := ⟨fun ⟨n, hn⟩ ↦ ⟨n, by simp_all⟩, fun ⟨i, hi⟩ ↦ ⟨(i % orderOf x).natAbs, by dsimp only rwa [← zpow_natCast, Int.natAbs_of_nonneg <| Int.emod_nonneg _ <| Int.natCast_ne_zero_iff_pos.2 <| hx.orderOf_pos, zpow_mod_orderOf]⟩⟩ @[to_additive] lemma IsOfFinOrder.powers_eq_zpowers (hx : IsOfFinOrder x) : (powers x : Set G) = zpowers x :=
Set.ext fun _ ↦ hx.mem_powers_iff_mem_zpowers @[to_additive] lemma IsOfFinOrder.mem_zpowers_iff_mem_range_orderOf [DecidableEq G] (hx : IsOfFinOrder x) : y ∈ zpowers x ↔ y ∈ (Finset.range (orderOf x)).image (x ^ ·) :=
Mathlib/GroupTheory/OrderOfElement.lean
669
673
/- Copyright (c) 2021 Frédéric Dupuis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Frédéric Dupuis, Heather Macbeth -/ import Mathlib.Analysis.InnerProductSpace.Dual import Mathlib.Analysis.InnerProductSpace.PiL2 /-! # Adjoint of operators on Hilbert spaces Given an operator `A : E →L[𝕜] F`, where `E` and `F` are Hilbert spaces, its adjoint `adjoint A : F →L[𝕜] E` is the unique operator such that `⟪x, A y⟫ = ⟪adjoint A x, y⟫` for all `x` and `y`. We then use this to put a C⋆-algebra structure on `E →L[𝕜] E` with the adjoint as the star operation. This construction is used to define an adjoint for linear maps (i.e. not continuous) between finite dimensional spaces. ## Main definitions * `ContinuousLinearMap.adjoint : (E →L[𝕜] F) ≃ₗᵢ⋆[𝕜] (F →L[𝕜] E)`: the adjoint of a continuous linear map, bundled as a conjugate-linear isometric equivalence. * `LinearMap.adjoint : (E →ₗ[𝕜] F) ≃ₗ⋆[𝕜] (F →ₗ[𝕜] E)`: the adjoint of a linear map between finite-dimensional spaces, this time only as a conjugate-linear equivalence, since there is no norm defined on these maps. ## Implementation notes * The continuous conjugate-linear version `adjointAux` is only an intermediate definition and is not meant to be used outside this file. ## Tags adjoint -/ noncomputable section open RCLike open scoped ComplexConjugate variable {𝕜 E F G : Type*} [RCLike 𝕜] variable [NormedAddCommGroup E] [NormedAddCommGroup F] [NormedAddCommGroup G] variable [InnerProductSpace 𝕜 E] [InnerProductSpace 𝕜 F] [InnerProductSpace 𝕜 G] local notation "⟪" x ", " y "⟫" => @inner 𝕜 _ _ x y /-! ### Adjoint operator -/ open InnerProductSpace namespace ContinuousLinearMap variable [CompleteSpace E] [CompleteSpace G] -- Note: made noncomputable to stop excess compilation -- https://github.com/leanprover-community/mathlib4/issues/7103 /-- The adjoint, as a continuous conjugate-linear map. This is only meant as an auxiliary definition for the main definition `adjoint`, where this is bundled as a conjugate-linear isometric equivalence. -/ noncomputable def adjointAux : (E →L[𝕜] F) →L⋆[𝕜] F →L[𝕜] E := (ContinuousLinearMap.compSL _ _ _ _ _ ((toDual 𝕜 E).symm : NormedSpace.Dual 𝕜 E →L⋆[𝕜] E)).comp (toSesqForm : (E →L[𝕜] F) →L[𝕜] F →L⋆[𝕜] NormedSpace.Dual 𝕜 E) @[simp] theorem adjointAux_apply (A : E →L[𝕜] F) (x : F) : adjointAux A x = ((toDual 𝕜 E).symm : NormedSpace.Dual 𝕜 E → E) ((toSesqForm A) x) := rfl theorem adjointAux_inner_left (A : E →L[𝕜] F) (x : E) (y : F) : ⟪adjointAux A y, x⟫ = ⟪y, A x⟫ := by rw [adjointAux_apply, toDual_symm_apply, toSesqForm_apply_coe, coe_comp', innerSL_apply_coe, Function.comp_apply] theorem adjointAux_inner_right (A : E →L[𝕜] F) (x : E) (y : F) : ⟪x, adjointAux A y⟫ = ⟪A x, y⟫ := by rw [← inner_conj_symm, adjointAux_inner_left, inner_conj_symm] variable [CompleteSpace F] theorem adjointAux_adjointAux (A : E →L[𝕜] F) : adjointAux (adjointAux A) = A := by ext v refine ext_inner_left 𝕜 fun w => ?_ rw [adjointAux_inner_right, adjointAux_inner_left] @[simp] theorem adjointAux_norm (A : E →L[𝕜] F) : ‖adjointAux A‖ = ‖A‖ := by refine le_antisymm ?_ ?_ · refine ContinuousLinearMap.opNorm_le_bound _ (norm_nonneg _) fun x => ?_ rw [adjointAux_apply, LinearIsometryEquiv.norm_map] exact toSesqForm_apply_norm_le · nth_rw 1 [← adjointAux_adjointAux A] refine ContinuousLinearMap.opNorm_le_bound _ (norm_nonneg _) fun x => ?_ rw [adjointAux_apply, LinearIsometryEquiv.norm_map] exact toSesqForm_apply_norm_le /-- The adjoint of a bounded operator `A` from a Hilbert space `E` to another Hilbert space `F`, denoted as `A†`. -/ def adjoint : (E →L[𝕜] F) ≃ₗᵢ⋆[𝕜] F →L[𝕜] E := LinearIsometryEquiv.ofSurjective { adjointAux with norm_map' := adjointAux_norm } fun A => ⟨adjointAux A, adjointAux_adjointAux A⟩ @[inherit_doc] scoped[InnerProduct] postfix:1000 "†" => ContinuousLinearMap.adjoint open InnerProduct /-- The fundamental property of the adjoint. -/ theorem adjoint_inner_left (A : E →L[𝕜] F) (x : E) (y : F) : ⟪(A†) y, x⟫ = ⟪y, A x⟫ := adjointAux_inner_left A x y /-- The fundamental property of the adjoint. -/ theorem adjoint_inner_right (A : E →L[𝕜] F) (x : E) (y : F) : ⟪x, (A†) y⟫ = ⟪A x, y⟫ := adjointAux_inner_right A x y /-- The adjoint is involutive. -/ @[simp] theorem adjoint_adjoint (A : E →L[𝕜] F) : A†† = A := adjointAux_adjointAux A /-- The adjoint of the composition of two operators is the composition of the two adjoints in reverse order. -/ @[simp] theorem adjoint_comp (A : F →L[𝕜] G) (B : E →L[𝕜] F) : (A ∘L B)† = B† ∘L A† := by ext v refine ext_inner_left 𝕜 fun w => ?_ simp only [adjoint_inner_right, ContinuousLinearMap.coe_comp', Function.comp_apply] theorem apply_norm_sq_eq_inner_adjoint_left (A : E →L[𝕜] F) (x : E) : ‖A x‖ ^ 2 = re ⟪(A† ∘L A) x, x⟫ := by have h : ⟪(A† ∘L A) x, x⟫ = ⟪A x, A x⟫ := by rw [← adjoint_inner_left]; rfl rw [h, ← inner_self_eq_norm_sq (𝕜 := 𝕜) _] theorem apply_norm_eq_sqrt_inner_adjoint_left (A : E →L[𝕜] F) (x : E) : ‖A x‖ = √(re ⟪(A† ∘L A) x, x⟫) := by rw [← apply_norm_sq_eq_inner_adjoint_left, Real.sqrt_sq (norm_nonneg _)] theorem apply_norm_sq_eq_inner_adjoint_right (A : E →L[𝕜] F) (x : E) : ‖A x‖ ^ 2 = re ⟪x, (A† ∘L A) x⟫ := by have h : ⟪x, (A† ∘L A) x⟫ = ⟪A x, A x⟫ := by rw [← adjoint_inner_right]; rfl rw [h, ← inner_self_eq_norm_sq (𝕜 := 𝕜) _] theorem apply_norm_eq_sqrt_inner_adjoint_right (A : E →L[𝕜] F) (x : E) : ‖A x‖ = √(re ⟪x, (A† ∘L A) x⟫) := by rw [← apply_norm_sq_eq_inner_adjoint_right, Real.sqrt_sq (norm_nonneg _)] /-- The adjoint is unique: a map `A` is the adjoint of `B` iff it satisfies `⟪A x, y⟫ = ⟪x, B y⟫` for all `x` and `y`. -/ theorem eq_adjoint_iff (A : E →L[𝕜] F) (B : F →L[𝕜] E) : A = B† ↔ ∀ x y, ⟪A x, y⟫ = ⟪x, B y⟫ := by refine ⟨fun h x y => by rw [h, adjoint_inner_left], fun h => ?_⟩ ext x exact ext_inner_right 𝕜 fun y => by simp only [adjoint_inner_left, h x y] @[simp] theorem adjoint_id : ContinuousLinearMap.adjoint (ContinuousLinearMap.id 𝕜 E) = ContinuousLinearMap.id 𝕜 E := by refine Eq.symm ?_ rw [eq_adjoint_iff] simp theorem _root_.Submodule.adjoint_subtypeL (U : Submodule 𝕜 E) [CompleteSpace U] : U.subtypeL† = U.orthogonalProjection := by symm rw [eq_adjoint_iff] intro x u rw [U.coe_inner, U.inner_orthogonalProjection_left_eq_right, U.orthogonalProjection_mem_subspace_eq_self] rfl theorem _root_.Submodule.adjoint_orthogonalProjection (U : Submodule 𝕜 E) [CompleteSpace U] : (U.orthogonalProjection : E →L[𝕜] U)† = U.subtypeL := by rw [← U.adjoint_subtypeL, adjoint_adjoint] /-- `E →L[𝕜] E` is a star algebra with the adjoint as the star operation. -/ instance : Star (E →L[𝕜] E) := ⟨adjoint⟩ instance : InvolutiveStar (E →L[𝕜] E) := ⟨adjoint_adjoint⟩ instance : StarMul (E →L[𝕜] E) := ⟨adjoint_comp⟩ instance : StarRing (E →L[𝕜] E) := ⟨LinearIsometryEquiv.map_add adjoint⟩ instance : StarModule 𝕜 (E →L[𝕜] E) := ⟨LinearIsometryEquiv.map_smulₛₗ adjoint⟩ theorem star_eq_adjoint (A : E →L[𝕜] E) : star A = A† := rfl /-- A continuous linear operator is self-adjoint iff it is equal to its adjoint. -/ theorem isSelfAdjoint_iff' {A : E →L[𝕜] E} : IsSelfAdjoint A ↔ ContinuousLinearMap.adjoint A = A := Iff.rfl theorem norm_adjoint_comp_self (A : E →L[𝕜] F) : ‖ContinuousLinearMap.adjoint A ∘L A‖ = ‖A‖ * ‖A‖ := by refine le_antisymm ?_ ?_ · calc ‖A† ∘L A‖ ≤ ‖A†‖ * ‖A‖ := opNorm_comp_le _ _ _ = ‖A‖ * ‖A‖ := by rw [LinearIsometryEquiv.norm_map] · rw [← sq, ← Real.sqrt_le_sqrt_iff (norm_nonneg _), Real.sqrt_sq (norm_nonneg _)] refine opNorm_le_bound _ (Real.sqrt_nonneg _) fun x => ?_ have := calc re ⟪(A† ∘L A) x, x⟫ ≤ ‖(A† ∘L A) x‖ * ‖x‖ := re_inner_le_norm _ _ _ ≤ ‖A† ∘L A‖ * ‖x‖ * ‖x‖ := mul_le_mul_of_nonneg_right (le_opNorm _ _) (norm_nonneg _) calc ‖A x‖ = √(re ⟪(A† ∘L A) x, x⟫) := by rw [apply_norm_eq_sqrt_inner_adjoint_left] _ ≤ √(‖A† ∘L A‖ * ‖x‖ * ‖x‖) := Real.sqrt_le_sqrt this _ = √‖A† ∘L A‖ * ‖x‖ := by simp_rw [mul_assoc, Real.sqrt_mul (norm_nonneg _) (‖x‖ * ‖x‖), Real.sqrt_mul_self (norm_nonneg x)] /-- The C⋆-algebra instance when `𝕜 := ℂ` can be found in `Analysis.CStarAlgebra.ContinuousLinearMap`. -/ instance : CStarRing (E →L[𝕜] E) where norm_mul_self_le x := le_of_eq <| Eq.symm <| norm_adjoint_comp_self x theorem isAdjointPair_inner (A : E →L[𝕜] F) : LinearMap.IsAdjointPair (sesqFormOfInner : E →ₗ[𝕜] E →ₗ⋆[𝕜] 𝕜) (sesqFormOfInner : F →ₗ[𝕜] F →ₗ⋆[𝕜] 𝕜) A (A†) := by intro x y simp only [sesqFormOfInner_apply_apply, adjoint_inner_left, coe_coe] end ContinuousLinearMap /-! ### Self-adjoint operators -/ namespace IsSelfAdjoint open ContinuousLinearMap variable [CompleteSpace E] [CompleteSpace F] theorem adjoint_eq {A : E →L[𝕜] E} (hA : IsSelfAdjoint A) : ContinuousLinearMap.adjoint A = A := hA /-- Every self-adjoint operator on an inner product space is symmetric. -/ theorem isSymmetric {A : E →L[𝕜] E} (hA : IsSelfAdjoint A) : (A : E →ₗ[𝕜] E).IsSymmetric := by intro x y rw_mod_cast [← A.adjoint_inner_right, hA.adjoint_eq] /-- Conjugating preserves self-adjointness. -/ theorem conj_adjoint {T : E →L[𝕜] E} (hT : IsSelfAdjoint T) (S : E →L[𝕜] F) : IsSelfAdjoint (S ∘L T ∘L ContinuousLinearMap.adjoint S) := by rw [isSelfAdjoint_iff'] at hT ⊢ simp only [hT, adjoint_comp, adjoint_adjoint] exact ContinuousLinearMap.comp_assoc _ _ _ /-- Conjugating preserves self-adjointness. -/ theorem adjoint_conj {T : E →L[𝕜] E} (hT : IsSelfAdjoint T) (S : F →L[𝕜] E) : IsSelfAdjoint (ContinuousLinearMap.adjoint S ∘L T ∘L S) := by rw [isSelfAdjoint_iff'] at hT ⊢ simp only [hT, adjoint_comp, adjoint_adjoint] exact ContinuousLinearMap.comp_assoc _ _ _ theorem _root_.ContinuousLinearMap.isSelfAdjoint_iff_isSymmetric {A : E →L[𝕜] E} : IsSelfAdjoint A ↔ (A : E →ₗ[𝕜] E).IsSymmetric := ⟨fun hA => hA.isSymmetric, fun hA => ext fun x => ext_inner_right 𝕜 fun y => (A.adjoint_inner_left y x).symm ▸ (hA x y).symm⟩ theorem _root_.LinearMap.IsSymmetric.isSelfAdjoint {A : E →L[𝕜] E} (hA : (A : E →ₗ[𝕜] E).IsSymmetric) : IsSelfAdjoint A := by rwa [← ContinuousLinearMap.isSelfAdjoint_iff_isSymmetric] at hA /-- The orthogonal projection is self-adjoint. -/ theorem _root_.orthogonalProjection_isSelfAdjoint (U : Submodule 𝕜 E) [CompleteSpace U] : IsSelfAdjoint (U.subtypeL ∘L U.orthogonalProjection) := U.orthogonalProjection_isSymmetric.isSelfAdjoint theorem conj_orthogonalProjection {T : E →L[𝕜] E} (hT : IsSelfAdjoint T) (U : Submodule 𝕜 E) [CompleteSpace U] : IsSelfAdjoint (U.subtypeL ∘L U.orthogonalProjection ∘L T ∘L U.subtypeL ∘L U.orthogonalProjection) := by rw [← ContinuousLinearMap.comp_assoc] nth_rw 1 [← (orthogonalProjection_isSelfAdjoint U).adjoint_eq] exact hT.adjoint_conj _ end IsSelfAdjoint namespace LinearMap variable [CompleteSpace E] variable {T : E →ₗ[𝕜] E} /-- The **Hellinger--Toeplitz theorem**: Construct a self-adjoint operator from an everywhere defined symmetric operator. -/ def IsSymmetric.toSelfAdjoint (hT : IsSymmetric T) : selfAdjoint (E →L[𝕜] E) := ⟨⟨T, hT.continuous⟩, ContinuousLinearMap.isSelfAdjoint_iff_isSymmetric.mpr hT⟩ theorem IsSymmetric.coe_toSelfAdjoint (hT : IsSymmetric T) : (hT.toSelfAdjoint : E →ₗ[𝕜] E) = T := rfl theorem IsSymmetric.toSelfAdjoint_apply (hT : IsSymmetric T) {x : E} : (hT.toSelfAdjoint : E → E) x = T x := rfl end LinearMap namespace LinearMap variable [FiniteDimensional 𝕜 E] [FiniteDimensional 𝕜 F] [FiniteDimensional 𝕜 G] /- Porting note: Lean can't use `FiniteDimensional.complete` since it was generalized to topological vector spaces. Use local instances instead. -/ /-- The adjoint of an operator from the finite-dimensional inner product space `E` to the finite-dimensional inner product space `F`. -/ def adjoint : (E →ₗ[𝕜] F) ≃ₗ⋆[𝕜] F →ₗ[𝕜] E := have := FiniteDimensional.complete 𝕜 E have := FiniteDimensional.complete 𝕜 F /- Note: Instead of the two instances above, the following works: ``` have := FiniteDimensional.complete 𝕜 have := FiniteDimensional.complete 𝕜 ``` But removing one of the `have`s makes it fail. The reason is that `E` and `F` don't live in the same universe, so the first `have` can no longer be used for `F` after its universe metavariable has been assigned to that of `E`! -/ ((LinearMap.toContinuousLinearMap : (E →ₗ[𝕜] F) ≃ₗ[𝕜] E →L[𝕜] F).trans ContinuousLinearMap.adjoint.toLinearEquiv).trans LinearMap.toContinuousLinearMap.symm theorem adjoint_toContinuousLinearMap (A : E →ₗ[𝕜] F) : haveI := FiniteDimensional.complete 𝕜 E haveI := FiniteDimensional.complete 𝕜 F LinearMap.toContinuousLinearMap (LinearMap.adjoint A) = ContinuousLinearMap.adjoint (LinearMap.toContinuousLinearMap A) := rfl theorem adjoint_eq_toCLM_adjoint (A : E →ₗ[𝕜] F) : haveI := FiniteDimensional.complete 𝕜 E haveI := FiniteDimensional.complete 𝕜 F LinearMap.adjoint A = ContinuousLinearMap.adjoint (LinearMap.toContinuousLinearMap A) := rfl /-- The fundamental property of the adjoint. -/ theorem adjoint_inner_left (A : E →ₗ[𝕜] F) (x : E) (y : F) : ⟪adjoint A y, x⟫ = ⟪y, A x⟫ := by haveI := FiniteDimensional.complete 𝕜 E haveI := FiniteDimensional.complete 𝕜 F rw [← coe_toContinuousLinearMap A, adjoint_eq_toCLM_adjoint] exact ContinuousLinearMap.adjoint_inner_left _ x y /-- The fundamental property of the adjoint. -/ theorem adjoint_inner_right (A : E →ₗ[𝕜] F) (x : E) (y : F) : ⟪x, adjoint A y⟫ = ⟪A x, y⟫ := by haveI := FiniteDimensional.complete 𝕜 E haveI := FiniteDimensional.complete 𝕜 F rw [← coe_toContinuousLinearMap A, adjoint_eq_toCLM_adjoint] exact ContinuousLinearMap.adjoint_inner_right _ x y /-- The adjoint is involutive. -/ @[simp] theorem adjoint_adjoint (A : E →ₗ[𝕜] F) : LinearMap.adjoint (LinearMap.adjoint A) = A := by ext v refine ext_inner_left 𝕜 fun w => ?_ rw [adjoint_inner_right, adjoint_inner_left] /-- The adjoint of the composition of two operators is the composition of the two adjoints in reverse order. -/ @[simp] theorem adjoint_comp (A : F →ₗ[𝕜] G) (B : E →ₗ[𝕜] F) : LinearMap.adjoint (A ∘ₗ B) = LinearMap.adjoint B ∘ₗ LinearMap.adjoint A := by ext v refine ext_inner_left 𝕜 fun w => ?_ simp only [adjoint_inner_right, LinearMap.coe_comp, Function.comp_apply] /-- The adjoint is unique: a map `A` is the adjoint of `B` iff it satisfies `⟪A x, y⟫ = ⟪x, B y⟫` for all `x` and `y`. -/ theorem eq_adjoint_iff (A : E →ₗ[𝕜] F) (B : F →ₗ[𝕜] E) : A = LinearMap.adjoint B ↔ ∀ x y, ⟪A x, y⟫ = ⟪x, B y⟫ := by refine ⟨fun h x y => by rw [h, adjoint_inner_left], fun h => ?_⟩ ext x
exact ext_inner_right 𝕜 fun y => by simp only [adjoint_inner_left, h x y] /-- The adjoint is unique: a map `A` is the adjoint of `B` iff it satisfies `⟪A x, y⟫ = ⟪x, B y⟫` for all basis vectors `x` and `y`. -/ theorem eq_adjoint_iff_basis {ι₁ : Type*} {ι₂ : Type*} (b₁ : Basis ι₁ 𝕜 E) (b₂ : Basis ι₂ 𝕜 F)
Mathlib/Analysis/InnerProductSpace/Adjoint.lean
381
385
/- Copyright (c) 2024 Chris Birkbeck. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Birkbeck, David Loeffler -/ import Mathlib.Analysis.Complex.UpperHalfPlane.Topology import Mathlib.Analysis.NormedSpace.FunctionSeries import Mathlib.Analysis.PSeries import Mathlib.Order.Interval.Finset.Box import Mathlib.NumberTheory.ModularForms.EisensteinSeries.Defs /-! # Uniform convergence of Eisenstein series We show that the sum of `eisSummand` converges locally uniformly on `ℍ` to the Eisenstein series of weight `k` and level `Γ(N)` with congruence condition `a : Fin 2 → ZMod N`. ## Outline of argument The key lemma `r_mul_max_le` shows that, for `z ∈ ℍ` and `c, d ∈ ℤ` (not both zero), `|c z + d|` is bounded below by `r z * max (|c|, |d|)`, where `r z` is an explicit function of `z` (independent of `c, d`) satisfying `0 < r z < 1` for all `z`. We then show in `summable_one_div_rpow_max` that the sum of `max (|c|, |d|) ^ (-k)` over `(c, d) ∈ ℤ × ℤ` is convergent for `2 < k`. This is proved by decomposing `ℤ × ℤ` using the `Finset.box` lemmas. -/ noncomputable section open Complex UpperHalfPlane Set Finset CongruenceSubgroup Topology open scoped UpperHalfPlane variable (z : ℍ) namespace EisensteinSeries
lemma norm_eq_max_natAbs (x : Fin 2 → ℤ) : ‖x‖ = max (x 0).natAbs (x 1).natAbs := by rw [← coe_nnnorm, ← NNReal.coe_natCast, NNReal.coe_inj, Nat.cast_max] refine eq_of_forall_ge_iff fun c ↦ ?_ simp only [pi_nnnorm_le_iff, Fin.forall_fin_two, max_le_iff, NNReal.natCast_natAbs]
Mathlib/NumberTheory/ModularForms/EisensteinSeries/UniformConvergence.lean
41
44
/- Copyright (c) 2021 Kim Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kim Morrison, Joël Riou -/ import Mathlib.Algebra.Homology.Homotopy import Mathlib.Algebra.Homology.ShortComplex.Retract import Mathlib.CategoryTheory.MorphismProperty.Composition /-! # Quasi-isomorphisms A chain map is a quasi-isomorphism if it induces isomorphisms on homology. -/ open CategoryTheory Limits universe v u open HomologicalComplex section variable {ι : Type*} {C : Type u} [Category.{v} C] [HasZeroMorphisms C] {c : ComplexShape ι} {K L M K' L' : HomologicalComplex C c} /-- A morphism of homological complexes `f : K ⟶ L` is a quasi-isomorphism in degree `i` when it induces a quasi-isomorphism of short complexes `K.sc i ⟶ L.sc i`. -/ class QuasiIsoAt (f : K ⟶ L) (i : ι) [K.HasHomology i] [L.HasHomology i] : Prop where quasiIso : ShortComplex.QuasiIso ((shortComplexFunctor C c i).map f) lemma quasiIsoAt_iff (f : K ⟶ L) (i : ι) [K.HasHomology i] [L.HasHomology i] : QuasiIsoAt f i ↔ ShortComplex.QuasiIso ((shortComplexFunctor C c i).map f) := by constructor · intro h exact h.quasiIso · intro h exact ⟨h⟩ instance quasiIsoAt_of_isIso (f : K ⟶ L) [IsIso f] (i : ι) [K.HasHomology i] [L.HasHomology i] : QuasiIsoAt f i := by rw [quasiIsoAt_iff] infer_instance lemma quasiIsoAt_iff' (f : K ⟶ L) (i j k : ι) (hi : c.prev j = i) (hk : c.next j = k) [K.HasHomology j] [L.HasHomology j] [(K.sc' i j k).HasHomology] [(L.sc' i j k).HasHomology] : QuasiIsoAt f j ↔ ShortComplex.QuasiIso ((shortComplexFunctor' C c i j k).map f) := by rw [quasiIsoAt_iff] exact ShortComplex.quasiIso_iff_of_arrow_mk_iso _ _ (Arrow.isoOfNatIso (natIsoSc' C c i j k hi hk) (Arrow.mk f)) lemma quasiIsoAt_of_retract {f : K ⟶ L} {f' : K' ⟶ L'} (h : RetractArrow f f') (i : ι) [K.HasHomology i] [L.HasHomology i] [K'.HasHomology i] [L'.HasHomology i] [hf' : QuasiIsoAt f' i] : QuasiIsoAt f i := by rw [quasiIsoAt_iff] at hf' ⊢ have : RetractArrow ((shortComplexFunctor C c i).map f) ((shortComplexFunctor C c i).map f') := h.map (shortComplexFunctor C c i).mapArrow exact ShortComplex.quasiIso_of_retract this lemma quasiIsoAt_iff_isIso_homologyMap (f : K ⟶ L) (i : ι) [K.HasHomology i] [L.HasHomology i] : QuasiIsoAt f i ↔ IsIso (homologyMap f i) := by rw [quasiIsoAt_iff, ShortComplex.quasiIso_iff] rfl lemma quasiIsoAt_iff_exactAt (f : K ⟶ L) (i : ι) [K.HasHomology i] [L.HasHomology i] (hK : K.ExactAt i) : QuasiIsoAt f i ↔ L.ExactAt i := by simp only [quasiIsoAt_iff, ShortComplex.quasiIso_iff, exactAt_iff, ShortComplex.exact_iff_isZero_homology] at hK ⊢ constructor · intro h exact IsZero.of_iso hK (@asIso _ _ _ _ _ h).symm · intro hL exact ⟨⟨0, IsZero.eq_of_src hK _ _, IsZero.eq_of_tgt hL _ _⟩⟩ lemma quasiIsoAt_iff_exactAt' (f : K ⟶ L) (i : ι) [K.HasHomology i] [L.HasHomology i] (hL : L.ExactAt i) : QuasiIsoAt f i ↔ K.ExactAt i := by simp only [quasiIsoAt_iff, ShortComplex.quasiIso_iff, exactAt_iff, ShortComplex.exact_iff_isZero_homology] at hL ⊢ constructor · intro h exact IsZero.of_iso hL (@asIso _ _ _ _ _ h) · intro hK exact ⟨⟨0, IsZero.eq_of_src hK _ _, IsZero.eq_of_tgt hL _ _⟩⟩ lemma exactAt_iff_of_quasiIsoAt (f : K ⟶ L) (i : ι) [K.HasHomology i] [L.HasHomology i] [QuasiIsoAt f i] : K.ExactAt i ↔ L.ExactAt i := ⟨fun hK => (quasiIsoAt_iff_exactAt f i hK).1 inferInstance, fun hL => (quasiIsoAt_iff_exactAt' f i hL).1 inferInstance⟩ instance (f : K ⟶ L) (i : ι) [K.HasHomology i] [L.HasHomology i] [hf : QuasiIsoAt f i] : IsIso (homologyMap f i) := by simpa only [quasiIsoAt_iff, ShortComplex.quasiIso_iff] using hf /-- The isomorphism `K.homology i ≅ L.homology i` induced by a morphism `f : K ⟶ L` such that `[QuasiIsoAt f i]` holds. -/ @[simps! hom] noncomputable def isoOfQuasiIsoAt (f : K ⟶ L) (i : ι) [K.HasHomology i] [L.HasHomology i] [QuasiIsoAt f i] : K.homology i ≅ L.homology i := asIso (homologyMap f i) @[reassoc (attr := simp)] lemma isoOfQuasiIsoAt_hom_inv_id (f : K ⟶ L) (i : ι) [K.HasHomology i] [L.HasHomology i] [QuasiIsoAt f i] : homologyMap f i ≫ (isoOfQuasiIsoAt f i).inv = 𝟙 _ := (isoOfQuasiIsoAt f i).hom_inv_id @[reassoc (attr := simp)] lemma isoOfQuasiIsoAt_inv_hom_id (f : K ⟶ L) (i : ι) [K.HasHomology i] [L.HasHomology i] [QuasiIsoAt f i] : (isoOfQuasiIsoAt f i).inv ≫ homologyMap f i = 𝟙 _ := (isoOfQuasiIsoAt f i).inv_hom_id lemma CochainComplex.quasiIsoAt₀_iff {K L : CochainComplex C ℕ} (f : K ⟶ L) [K.HasHomology 0] [L.HasHomology 0] [(K.sc' 0 0 1).HasHomology] [(L.sc' 0 0 1).HasHomology] : QuasiIsoAt f 0 ↔ ShortComplex.QuasiIso ((HomologicalComplex.shortComplexFunctor' C _ 0 0 1).map f) := quasiIsoAt_iff' _ _ _ _ (by simp) (by simp) lemma ChainComplex.quasiIsoAt₀_iff {K L : ChainComplex C ℕ} (f : K ⟶ L) [K.HasHomology 0] [L.HasHomology 0] [(K.sc' 1 0 0).HasHomology] [(L.sc' 1 0 0).HasHomology] : QuasiIsoAt f 0 ↔ ShortComplex.QuasiIso ((HomologicalComplex.shortComplexFunctor' C _ 1 0 0).map f) := quasiIsoAt_iff' _ _ _ _ (by simp) (by simp) /-- A morphism of homological complexes `f : K ⟶ L` is a quasi-isomorphism when it is so in every degree, i.e. when the induced maps `homologyMap f i : K.homology i ⟶ L.homology i` are all isomorphisms (see `quasiIso_iff` and `quasiIsoAt_iff_isIso_homologyMap`). -/ class QuasiIso (f : K ⟶ L) [∀ i, K.HasHomology i] [∀ i, L.HasHomology i] : Prop where quasiIsoAt : ∀ i, QuasiIsoAt f i := by infer_instance lemma quasiIso_iff (f : K ⟶ L) [∀ i, K.HasHomology i] [∀ i, L.HasHomology i] : QuasiIso f ↔ ∀ i, QuasiIsoAt f i := ⟨fun h => h.quasiIsoAt, fun h => ⟨h⟩⟩ attribute [instance] QuasiIso.quasiIsoAt instance quasiIso_of_isIso (f : K ⟶ L) [IsIso f] [∀ i, K.HasHomology i] [∀ i, L.HasHomology i] : QuasiIso f where instance quasiIsoAt_comp (φ : K ⟶ L) (φ' : L ⟶ M) (i : ι) [K.HasHomology i] [L.HasHomology i] [M.HasHomology i] [hφ : QuasiIsoAt φ i] [hφ' : QuasiIsoAt φ' i] : QuasiIsoAt (φ ≫ φ') i := by rw [quasiIsoAt_iff] at hφ hφ' ⊢ rw [Functor.map_comp] exact ShortComplex.quasiIso_comp _ _ instance quasiIso_comp (φ : K ⟶ L) (φ' : L ⟶ M) [∀ i, K.HasHomology i] [∀ i, L.HasHomology i] [∀ i, M.HasHomology i] [hφ : QuasiIso φ] [hφ' : QuasiIso φ'] : QuasiIso (φ ≫ φ') where lemma quasiIsoAt_of_comp_left (φ : K ⟶ L) (φ' : L ⟶ M) (i : ι) [K.HasHomology i] [L.HasHomology i] [M.HasHomology i] [hφ : QuasiIsoAt φ i] [hφφ' : QuasiIsoAt (φ ≫ φ') i] : QuasiIsoAt φ' i := by rw [quasiIsoAt_iff_isIso_homologyMap] at hφ hφφ' ⊢ rw [homologyMap_comp] at hφφ' exact IsIso.of_isIso_comp_left (homologyMap φ i) (homologyMap φ' i) lemma quasiIsoAt_iff_comp_left (φ : K ⟶ L) (φ' : L ⟶ M) (i : ι) [K.HasHomology i] [L.HasHomology i] [M.HasHomology i] [hφ : QuasiIsoAt φ i] : QuasiIsoAt (φ ≫ φ') i ↔ QuasiIsoAt φ' i := by constructor · intro exact quasiIsoAt_of_comp_left φ φ' i · intro infer_instance lemma quasiIso_iff_comp_left (φ : K ⟶ L) (φ' : L ⟶ M) [∀ i, K.HasHomology i] [∀ i, L.HasHomology i] [∀ i, M.HasHomology i] [hφ : QuasiIso φ] : QuasiIso (φ ≫ φ') ↔ QuasiIso φ' := by simp only [quasiIso_iff, quasiIsoAt_iff_comp_left φ φ'] lemma quasiIso_of_comp_left (φ : K ⟶ L) (φ' : L ⟶ M) [∀ i, K.HasHomology i] [∀ i, L.HasHomology i] [∀ i, M.HasHomology i] [hφ : QuasiIso φ] [hφφ' : QuasiIso (φ ≫ φ')] : QuasiIso φ' := by rw [← quasiIso_iff_comp_left φ φ'] infer_instance lemma quasiIsoAt_of_comp_right (φ : K ⟶ L) (φ' : L ⟶ M) (i : ι) [K.HasHomology i] [L.HasHomology i] [M.HasHomology i] [hφ' : QuasiIsoAt φ' i] [hφφ' : QuasiIsoAt (φ ≫ φ') i] : QuasiIsoAt φ i := by rw [quasiIsoAt_iff_isIso_homologyMap] at hφ' hφφ' ⊢ rw [homologyMap_comp] at hφφ' exact IsIso.of_isIso_comp_right (homologyMap φ i) (homologyMap φ' i) lemma quasiIsoAt_iff_comp_right (φ : K ⟶ L) (φ' : L ⟶ M) (i : ι) [K.HasHomology i] [L.HasHomology i] [M.HasHomology i] [hφ' : QuasiIsoAt φ' i] : QuasiIsoAt (φ ≫ φ') i ↔ QuasiIsoAt φ i := by constructor · intro exact quasiIsoAt_of_comp_right φ φ' i · intro infer_instance lemma quasiIso_iff_comp_right (φ : K ⟶ L) (φ' : L ⟶ M) [∀ i, K.HasHomology i] [∀ i, L.HasHomology i] [∀ i, M.HasHomology i] [hφ' : QuasiIso φ'] : QuasiIso (φ ≫ φ') ↔ QuasiIso φ := by simp only [quasiIso_iff, quasiIsoAt_iff_comp_right φ φ'] lemma quasiIso_of_comp_right (φ : K ⟶ L) (φ' : L ⟶ M) [∀ i, K.HasHomology i] [∀ i, L.HasHomology i] [∀ i, M.HasHomology i] [hφ : QuasiIso φ'] [hφφ' : QuasiIso (φ ≫ φ')] : QuasiIso φ := by rw [← quasiIso_iff_comp_right φ φ'] infer_instance lemma quasiIso_iff_of_arrow_mk_iso (φ : K ⟶ L) (φ' : K' ⟶ L') (e : Arrow.mk φ ≅ Arrow.mk φ') [∀ i, K.HasHomology i] [∀ i, L.HasHomology i] [∀ i, K'.HasHomology i] [∀ i, L'.HasHomology i] : QuasiIso φ ↔ QuasiIso φ' := by simp [← quasiIso_iff_comp_left (show K' ⟶ K from e.inv.left) φ, ← quasiIso_iff_comp_right φ' (show L' ⟶ L from e.inv.right)] lemma quasiIso_of_arrow_mk_iso (φ : K ⟶ L) (φ' : K' ⟶ L') (e : Arrow.mk φ ≅ Arrow.mk φ') [∀ i, K.HasHomology i] [∀ i, L.HasHomology i] [∀ i, K'.HasHomology i] [∀ i, L'.HasHomology i] [hφ : QuasiIso φ] : QuasiIso φ' := by simpa only [← quasiIso_iff_of_arrow_mk_iso φ φ' e] lemma quasiIso_of_retractArrow {f : K ⟶ L} {f' : K' ⟶ L'} (h : RetractArrow f f') [∀ i, K.HasHomology i] [∀ i, L.HasHomology i] [∀ i, K'.HasHomology i] [∀ i, L'.HasHomology i] [QuasiIso f'] : QuasiIso f where quasiIsoAt i := quasiIsoAt_of_retract h i namespace HomologicalComplex section PreservesHomology variable {C₁ C₂ : Type*} [Category C₁] [Category C₂] [Preadditive C₁] [Preadditive C₂] {K L : HomologicalComplex C₁ c} (φ : K ⟶ L) (F : C₁ ⥤ C₂) [F.Additive] [F.PreservesHomology] section variable (i : ι) [K.HasHomology i] [L.HasHomology i] [((F.mapHomologicalComplex c).obj K).HasHomology i] [((F.mapHomologicalComplex c).obj L).HasHomology i] instance quasiIsoAt_map_of_preservesHomology [hφ : QuasiIsoAt φ i] : QuasiIsoAt ((F.mapHomologicalComplex c).map φ) i := by rw [quasiIsoAt_iff] at hφ ⊢ exact ShortComplex.quasiIso_map_of_preservesLeftHomology F ((shortComplexFunctor C₁ c i).map φ) lemma quasiIsoAt_map_iff_of_preservesHomology [F.ReflectsIsomorphisms] : QuasiIsoAt ((F.mapHomologicalComplex c).map φ) i ↔ QuasiIsoAt φ i := by simp only [quasiIsoAt_iff] exact ShortComplex.quasiIso_map_iff_of_preservesLeftHomology F ((shortComplexFunctor C₁ c i).map φ) end section variable [∀ i, K.HasHomology i] [∀ i, L.HasHomology i] [∀ i, ((F.mapHomologicalComplex c).obj K).HasHomology i] [∀ i, ((F.mapHomologicalComplex c).obj L).HasHomology i] instance quasiIso_map_of_preservesHomology [hφ : QuasiIso φ] : QuasiIso ((F.mapHomologicalComplex c).map φ) where lemma quasiIso_map_iff_of_preservesHomology [F.ReflectsIsomorphisms] : QuasiIso ((F.mapHomologicalComplex c).map φ) ↔ QuasiIso φ := by simp only [quasiIso_iff, quasiIsoAt_map_iff_of_preservesHomology φ F] end end PreservesHomology variable (C c) /-- The morphism property on `HomologicalComplex C c` given by quasi-isomorphisms. -/ def quasiIso [CategoryWithHomology C] : MorphismProperty (HomologicalComplex C c) := fun _ _ f => QuasiIso f variable {C c} [CategoryWithHomology C] @[simp] lemma mem_quasiIso_iff (f : K ⟶ L) : quasiIso C c f ↔ QuasiIso f := by rfl instance : (quasiIso C c).IsMultiplicative where id_mem _ := by rw [mem_quasiIso_iff] infer_instance comp_mem _ _ hf hg := by rw [mem_quasiIso_iff] at hf hg ⊢ infer_instance instance : (quasiIso C c).HasTwoOutOfThreeProperty where of_postcomp f g hg hfg := by rw [mem_quasiIso_iff] at hg hfg ⊢ rwa [← quasiIso_iff_comp_right f g] of_precomp f g hf hfg := by rw [mem_quasiIso_iff] at hf hfg ⊢ rwa [← quasiIso_iff_comp_left f g] instance : (quasiIso C c).IsStableUnderRetracts where of_retract h hg := by rw [mem_quasiIso_iff] at hg ⊢ exact quasiIso_of_retractArrow h instance : (quasiIso C c).RespectsIso := MorphismProperty.respectsIso_of_isStableUnderComposition (fun _ _ _ (_ : IsIso _) ↦ by rw [mem_quasiIso_iff]; infer_instance) end HomologicalComplex end section variable {ι : Type*} {C : Type u} [Category.{v} C] [Preadditive C] {c : ComplexShape ι} {K L : HomologicalComplex C c} (e : HomotopyEquiv K L) [∀ i, K.HasHomology i] [∀ i, L.HasHomology i] instance : QuasiIso e.hom where quasiIsoAt n := by classical rw [quasiIsoAt_iff_isIso_homologyMap] exact (e.toHomologyIso n).isIso_hom instance : QuasiIso e.inv := (inferInstance : QuasiIso e.symm.hom) variable (C c) lemma homotopyEquivalences_le_quasiIso [CategoryWithHomology C] : homotopyEquivalences C c ≤ quasiIso C c := by rintro K L _ ⟨e, rfl⟩ simp only [HomologicalComplex.mem_quasiIso_iff] infer_instance end
Mathlib/Algebra/Homology/QuasiIso.lean
375
381
/- Copyright (c) 2022 Rémy Degenne. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Rémy Degenne -/ import Mathlib.Probability.Kernel.Defs /-! # Basic kernels This file contains basic results about kernels in general and definitions of some particular kernels. ## Main definitions * `ProbabilityTheory.Kernel.deterministic (f : α → β) (hf : Measurable f)`: kernel `a ↦ Measure.dirac (f a)`. * `ProbabilityTheory.Kernel.id`: the identity kernel, deterministic kernel for the identity function. * `ProbabilityTheory.Kernel.copy α`: the deterministic kernel that maps `x : α` to the Dirac measure at `(x, x) : α × α`. * `ProbabilityTheory.Kernel.discard α`: the Markov kernel to the type `Unit`. * `ProbabilityTheory.Kernel.swap α β`: the deterministic kernel that maps `(x, y)` to the Dirac measure at `(y, x)`. * `ProbabilityTheory.Kernel.const α (μβ : measure β)`: constant kernel `a ↦ μβ`. * `ProbabilityTheory.Kernel.restrict κ (hs : MeasurableSet s)`: kernel for which the image of `a : α` is `(κ a).restrict s`. Integral: `∫⁻ b, f b ∂(κ.restrict hs a) = ∫⁻ b in s, f b ∂(κ a)` * `ProbabilityTheory.Kernel.comapRight`: Kernel with value `(κ a).comap f`, for a measurable embedding `f`. That is, for a measurable set `t : Set β`, `ProbabilityTheory.Kernel.comapRight κ hf a t = κ a (f '' t)` * `ProbabilityTheory.Kernel.piecewise (hs : MeasurableSet s) κ η`: the kernel equal to `κ` on the measurable set `s` and to `η` on its complement. ## Main statements -/ assert_not_exists MeasureTheory.integral open MeasureTheory open scoped ENNReal namespace ProbabilityTheory variable {α β ι : Type*} {mα : MeasurableSpace α} {mβ : MeasurableSpace β} {κ : Kernel α β} namespace Kernel section Deterministic /-- Kernel which to `a` associates the dirac measure at `f a`. This is a Markov kernel. -/ noncomputable def deterministic (f : α → β) (hf : Measurable f) : Kernel α β where toFun a := Measure.dirac (f a) measurable' := by refine Measure.measurable_of_measurable_coe _ fun s hs => ?_ simp_rw [Measure.dirac_apply' _ hs] exact measurable_one.indicator (hf hs) theorem deterministic_apply {f : α → β} (hf : Measurable f) (a : α) : deterministic f hf a = Measure.dirac (f a) := rfl theorem deterministic_apply' {f : α → β} (hf : Measurable f) (a : α) {s : Set β} (hs : MeasurableSet s) : deterministic f hf a s = s.indicator (fun _ => 1) (f a) := by rw [deterministic] change Measure.dirac (f a) s = s.indicator 1 (f a) simp_rw [Measure.dirac_apply' _ hs] /-- Because of the measurability field in `Kernel.deterministic`, `rw [h]` will not rewrite `deterministic f hf` to `deterministic g ⋯`. Instead one can do `rw [deterministic_congr h]`. -/ theorem deterministic_congr {f g : α → β} {hf : Measurable f} (h : f = g) : deterministic f hf = deterministic g (h ▸ hf) := by conv_lhs => enter [1]; rw [h] instance isMarkovKernel_deterministic {f : α → β} (hf : Measurable f) : IsMarkovKernel (deterministic f hf) := ⟨fun a => by rw [deterministic_apply hf]; infer_instance⟩ theorem lintegral_deterministic' {f : β → ℝ≥0∞} {g : α → β} {a : α} (hg : Measurable g) (hf : Measurable f) : ∫⁻ x, f x ∂deterministic g hg a = f (g a) := by rw [deterministic_apply, lintegral_dirac' _ hf] @[simp] theorem lintegral_deterministic {f : β → ℝ≥0∞} {g : α → β} {a : α} (hg : Measurable g) [MeasurableSingletonClass β] : ∫⁻ x, f x ∂deterministic g hg a = f (g a) := by rw [deterministic_apply, lintegral_dirac (g a) f] theorem setLIntegral_deterministic' {f : β → ℝ≥0∞} {g : α → β} {a : α} (hg : Measurable g) (hf : Measurable f) {s : Set β} (hs : MeasurableSet s) [Decidable (g a ∈ s)] : ∫⁻ x in s, f x ∂deterministic g hg a = if g a ∈ s then f (g a) else 0 := by rw [deterministic_apply, setLIntegral_dirac' hf hs] @[simp] theorem setLIntegral_deterministic {f : β → ℝ≥0∞} {g : α → β} {a : α} (hg : Measurable g) [MeasurableSingletonClass β] (s : Set β) [Decidable (g a ∈ s)] : ∫⁻ x in s, f x ∂deterministic g hg a = if g a ∈ s then f (g a) else 0 := by rw [deterministic_apply, setLIntegral_dirac f s] end Deterministic section Id /-- The identity kernel, that maps `x : α` to the Dirac measure at `x`. -/ protected noncomputable def id : Kernel α α := Kernel.deterministic id measurable_id instance : IsMarkovKernel (Kernel.id : Kernel α α) := by rw [Kernel.id]; infer_instance lemma id_apply (a : α) : Kernel.id a = Measure.dirac a := by rw [Kernel.id, deterministic_apply, id_def] lemma lintegral_id' {f : α → ℝ≥0∞} (hf : Measurable f) (a : α) : ∫⁻ a, f a ∂(@Kernel.id α mα a) = f a := by rw [id_apply, lintegral_dirac' _ hf] lemma lintegral_id [MeasurableSingletonClass α] {f : α → ℝ≥0∞} (a : α) : ∫⁻ a, f a ∂(@Kernel.id α mα a) = f a := by rw [id_apply, lintegral_dirac] end Id section Copy /-- The deterministic kernel that maps `x : α` to the Dirac measure at `(x, x) : α × α`. -/ noncomputable def copy (α : Type*) [MeasurableSpace α] : Kernel α (α × α) := Kernel.deterministic (fun x ↦ (x, x)) (measurable_id.prod measurable_id) instance : IsMarkovKernel (copy α) := by rw [copy]; infer_instance lemma copy_apply (a : α) : copy α a = Measure.dirac (a, a) := by simp [copy, deterministic_apply] end Copy section Discard /-- The Markov kernel to the `Unit` type. -/ noncomputable def discard (α : Type*) [MeasurableSpace α] : Kernel α Unit := Kernel.deterministic (fun _ ↦ ()) measurable_const instance : IsMarkovKernel (discard α) := by rw [discard]; infer_instance @[simp] lemma discard_apply (a : α) : discard α a = Measure.dirac () := deterministic_apply _ _ end Discard section Swap /-- The deterministic kernel that maps `(x, y)` to the Dirac measure at `(y, x)`. -/ noncomputable def swap (α β : Type*) [MeasurableSpace α] [MeasurableSpace β] : Kernel (α × β) (β × α) := Kernel.deterministic Prod.swap measurable_swap instance : IsMarkovKernel (swap α β) := by rw [swap]; infer_instance /-- See `swap_apply'` for a fully applied version of this lemma. -/ lemma swap_apply (ab : α × β) : swap α β ab = Measure.dirac ab.swap := by rw [swap, deterministic_apply] /-- See `swap_apply` for a partially applied version of this lemma. -/ lemma swap_apply' (ab : α × β) {s : Set (β × α)} (hs : MeasurableSet s) : swap α β ab s = s.indicator 1 ab.swap := by rw [swap_apply, Measure.dirac_apply' _ hs] end Swap section Const /-- Constant kernel, which always returns the same measure. -/ def const (α : Type*) {β : Type*} [MeasurableSpace α] {_ : MeasurableSpace β} (μβ : Measure β) : Kernel α β where toFun _ := μβ measurable' := measurable_const @[simp] theorem const_apply (μβ : Measure β) (a : α) : const α μβ a = μβ := rfl @[simp] lemma const_zero : const α (0 : Measure β) = 0 := by ext x s _; simp [const_apply] lemma const_add (β : Type*) [MeasurableSpace β] (μ ν : Measure α) : const β (μ + ν) = const β μ + const β ν := by ext; simp lemma sum_const [Countable ι] (μ : ι → Measure β) : Kernel.sum (fun n ↦ const α (μ n)) = const α (Measure.sum μ) := rfl instance const.instIsFiniteKernel {μβ : Measure β} [IsFiniteMeasure μβ] : IsFiniteKernel (const α μβ) := ⟨⟨μβ Set.univ, measure_lt_top _ _, fun _ => le_rfl⟩⟩ instance const.instIsSFiniteKernel {μβ : Measure β} [SFinite μβ] : IsSFiniteKernel (const α μβ) := ⟨fun n ↦ const α (sfiniteSeq μβ n), fun n ↦ inferInstance, by rw [sum_const, sum_sfiniteSeq]⟩ instance const.instIsMarkovKernel {μβ : Measure β} [hμβ : IsProbabilityMeasure μβ] : IsMarkovKernel (const α μβ) := ⟨fun _ => hμβ⟩ instance const.instIsZeroOrMarkovKernel {μβ : Measure β} [hμβ : IsZeroOrProbabilityMeasure μβ] : IsZeroOrMarkovKernel (const α μβ) := by rcases eq_zero_or_isProbabilityMeasure μβ with rfl | h · simp only [const_zero] infer_instance · infer_instance lemma isSFiniteKernel_const [Nonempty α] {μβ : Measure β} : IsSFiniteKernel (const α μβ) ↔ SFinite μβ :=
⟨fun h ↦ h.sFinite (Classical.arbitrary α), fun _ ↦ inferInstance⟩ @[simp]
Mathlib/Probability/Kernel/Basic.lean
214
216
/- Copyright (c) 2019 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou, Sébastien Gouëzel, Frédéric Dupuis -/ import Mathlib.Algebra.BigOperators.Field import Mathlib.Analysis.Complex.Basic import Mathlib.Analysis.InnerProductSpace.Defs import Mathlib.GroupTheory.MonoidLocalization.Basic /-! # Properties of inner product spaces This file proves many basic properties of inner product spaces (real or complex). ## Main results - `inner_mul_inner_self_le`: the Cauchy-Schwartz inequality (one of many variants). - `norm_inner_eq_norm_iff`: the equality criteion in the Cauchy-Schwartz inequality (also in many variants). - `inner_eq_sum_norm_sq_div_four`: the polarization identity. ## Tags inner product space, Hilbert space, norm -/ noncomputable section open RCLike Real Filter Topology ComplexConjugate Finsupp open LinearMap (BilinForm) variable {𝕜 E F : Type*} [RCLike 𝕜] section BasicProperties_Seminormed open scoped InnerProductSpace variable [SeminormedAddCommGroup E] [InnerProductSpace 𝕜 E] variable [SeminormedAddCommGroup F] [InnerProductSpace ℝ F] local notation "⟪" x ", " y "⟫" => @inner 𝕜 _ _ x y local postfix:90 "†" => starRingEnd _ export InnerProductSpace (norm_sq_eq_re_inner) @[simp] theorem inner_conj_symm (x y : E) : ⟪y, x⟫† = ⟪x, y⟫ := InnerProductSpace.conj_inner_symm _ _ theorem real_inner_comm (x y : F) : ⟪y, x⟫_ℝ = ⟪x, y⟫_ℝ := @inner_conj_symm ℝ _ _ _ _ x y theorem inner_eq_zero_symm {x y : E} : ⟪x, y⟫ = 0 ↔ ⟪y, x⟫ = 0 := by rw [← inner_conj_symm] exact star_eq_zero @[simp] theorem inner_self_im (x : E) : im ⟪x, x⟫ = 0 := by rw [← @ofReal_inj 𝕜, im_eq_conj_sub]; simp theorem inner_add_left (x y z : E) : ⟪x + y, z⟫ = ⟪x, z⟫ + ⟪y, z⟫ := InnerProductSpace.add_left _ _ _ theorem inner_add_right (x y z : E) : ⟪x, y + z⟫ = ⟪x, y⟫ + ⟪x, z⟫ := by rw [← inner_conj_symm, inner_add_left, RingHom.map_add] simp only [inner_conj_symm] theorem inner_re_symm (x y : E) : re ⟪x, y⟫ = re ⟪y, x⟫ := by rw [← inner_conj_symm, conj_re] theorem inner_im_symm (x y : E) : im ⟪x, y⟫ = -im ⟪y, x⟫ := by rw [← inner_conj_symm, conj_im] section Algebra variable {𝕝 : Type*} [CommSemiring 𝕝] [StarRing 𝕝] [Algebra 𝕝 𝕜] [Module 𝕝 E] [IsScalarTower 𝕝 𝕜 E] [StarModule 𝕝 𝕜] /-- See `inner_smul_left` for the common special when `𝕜 = 𝕝`. -/ lemma inner_smul_left_eq_star_smul (x y : E) (r : 𝕝) : ⟪r • x, y⟫ = r† • ⟪x, y⟫ := by rw [← algebraMap_smul 𝕜 r, InnerProductSpace.smul_left, starRingEnd_apply, starRingEnd_apply, ← algebraMap_star_comm, ← smul_eq_mul, algebraMap_smul] /-- Special case of `inner_smul_left_eq_star_smul` when the acting ring has a trivial star (eg `ℕ`, `ℤ`, `ℚ≥0`, `ℚ`, `ℝ`). -/ lemma inner_smul_left_eq_smul [TrivialStar 𝕝] (x y : E) (r : 𝕝) : ⟪r • x, y⟫ = r • ⟪x, y⟫ := by rw [inner_smul_left_eq_star_smul, starRingEnd_apply, star_trivial] /-- See `inner_smul_right` for the common special when `𝕜 = 𝕝`. -/ lemma inner_smul_right_eq_smul (x y : E) (r : 𝕝) : ⟪x, r • y⟫ = r • ⟪x, y⟫ := by rw [← inner_conj_symm, inner_smul_left_eq_star_smul, starRingEnd_apply, starRingEnd_apply, star_smul, star_star, ← starRingEnd_apply, inner_conj_symm] end Algebra /-- See `inner_smul_left_eq_star_smul` for the case of a general algebra action. -/ theorem inner_smul_left (x y : E) (r : 𝕜) : ⟪r • x, y⟫ = r† * ⟪x, y⟫ := inner_smul_left_eq_star_smul .. theorem real_inner_smul_left (x y : F) (r : ℝ) : ⟪r • x, y⟫_ℝ = r * ⟪x, y⟫_ℝ := inner_smul_left _ _ _ theorem inner_smul_real_left (x y : E) (r : ℝ) : ⟪(r : 𝕜) • x, y⟫ = r • ⟪x, y⟫ := by rw [inner_smul_left, conj_ofReal, Algebra.smul_def] /-- See `inner_smul_right_eq_smul` for the case of a general algebra action. -/ theorem inner_smul_right (x y : E) (r : 𝕜) : ⟪x, r • y⟫ = r * ⟪x, y⟫ := inner_smul_right_eq_smul .. theorem real_inner_smul_right (x y : F) (r : ℝ) : ⟪x, r • y⟫_ℝ = r * ⟪x, y⟫_ℝ := inner_smul_right _ _ _ theorem inner_smul_real_right (x y : E) (r : ℝ) : ⟪x, (r : 𝕜) • y⟫ = r • ⟪x, y⟫ := by rw [inner_smul_right, Algebra.smul_def] /-- The inner product as a sesquilinear form. Note that in the case `𝕜 = ℝ` this is a bilinear form. -/ @[simps!] def sesqFormOfInner : E →ₗ[𝕜] E →ₗ⋆[𝕜] 𝕜 := LinearMap.mk₂'ₛₗ (RingHom.id 𝕜) (starRingEnd _) (fun x y => ⟪y, x⟫) (fun _x _y _z => inner_add_right _ _ _) (fun _r _x _y => inner_smul_right _ _ _) (fun _x _y _z => inner_add_left _ _ _) fun _r _x _y => inner_smul_left _ _ _ /-- The real inner product as a bilinear form. Note that unlike `sesqFormOfInner`, this does not reverse the order of the arguments. -/ @[simps!] def bilinFormOfRealInner : BilinForm ℝ F := sesqFormOfInner.flip /-- An inner product with a sum on the left. -/ theorem sum_inner {ι : Type*} (s : Finset ι) (f : ι → E) (x : E) : ⟪∑ i ∈ s, f i, x⟫ = ∑ i ∈ s, ⟪f i, x⟫ := map_sum (sesqFormOfInner (𝕜 := 𝕜) (E := E) x) _ _ /-- An inner product with a sum on the right. -/ theorem inner_sum {ι : Type*} (s : Finset ι) (f : ι → E) (x : E) : ⟪x, ∑ i ∈ s, f i⟫ = ∑ i ∈ s, ⟪x, f i⟫ := map_sum (LinearMap.flip sesqFormOfInner x) _ _ /-- An inner product with a sum on the left, `Finsupp` version. -/ protected theorem Finsupp.sum_inner {ι : Type*} (l : ι →₀ 𝕜) (v : ι → E) (x : E) : ⟪l.sum fun (i : ι) (a : 𝕜) => a • v i, x⟫ = l.sum fun (i : ι) (a : 𝕜) => conj a • ⟪v i, x⟫ := by convert sum_inner (𝕜 := 𝕜) l.support (fun a => l a • v a) x simp only [inner_smul_left, Finsupp.sum, smul_eq_mul] /-- An inner product with a sum on the right, `Finsupp` version. -/ protected theorem Finsupp.inner_sum {ι : Type*} (l : ι →₀ 𝕜) (v : ι → E) (x : E) : ⟪x, l.sum fun (i : ι) (a : 𝕜) => a • v i⟫ = l.sum fun (i : ι) (a : 𝕜) => a • ⟪x, v i⟫ := by convert inner_sum (𝕜 := 𝕜) l.support (fun a => l a • v a) x simp only [inner_smul_right, Finsupp.sum, smul_eq_mul] protected theorem DFinsupp.sum_inner {ι : Type*} [DecidableEq ι] {α : ι → Type*} [∀ i, AddZeroClass (α i)] [∀ (i) (x : α i), Decidable (x ≠ 0)] (f : ∀ i, α i → E) (l : Π₀ i, α i) (x : E) : ⟪l.sum f, x⟫ = l.sum fun i a => ⟪f i a, x⟫ := by simp +contextual only [DFinsupp.sum, sum_inner, smul_eq_mul] protected theorem DFinsupp.inner_sum {ι : Type*} [DecidableEq ι] {α : ι → Type*} [∀ i, AddZeroClass (α i)] [∀ (i) (x : α i), Decidable (x ≠ 0)] (f : ∀ i, α i → E) (l : Π₀ i, α i) (x : E) : ⟪x, l.sum f⟫ = l.sum fun i a => ⟪x, f i a⟫ := by simp +contextual only [DFinsupp.sum, inner_sum, smul_eq_mul] @[simp] theorem inner_zero_left (x : E) : ⟪0, x⟫ = 0 := by rw [← zero_smul 𝕜 (0 : E), inner_smul_left, RingHom.map_zero, zero_mul] theorem inner_re_zero_left (x : E) : re ⟪0, x⟫ = 0 := by simp only [inner_zero_left, AddMonoidHom.map_zero] @[simp] theorem inner_zero_right (x : E) : ⟪x, 0⟫ = 0 := by rw [← inner_conj_symm, inner_zero_left, RingHom.map_zero] theorem inner_re_zero_right (x : E) : re ⟪x, 0⟫ = 0 := by simp only [inner_zero_right, AddMonoidHom.map_zero] theorem inner_self_nonneg {x : E} : 0 ≤ re ⟪x, x⟫ := PreInnerProductSpace.toCore.re_inner_nonneg x theorem real_inner_self_nonneg {x : F} : 0 ≤ ⟪x, x⟫_ℝ := @inner_self_nonneg ℝ F _ _ _ x @[simp] theorem inner_self_ofReal_re (x : E) : (re ⟪x, x⟫ : 𝕜) = ⟪x, x⟫ := ((RCLike.is_real_TFAE (⟪x, x⟫ : 𝕜)).out 2 3).2 (inner_self_im (𝕜 := 𝕜) x) theorem inner_self_eq_norm_sq_to_K (x : E) : ⟪x, x⟫ = (‖x‖ : 𝕜) ^ 2 := by rw [← inner_self_ofReal_re, ← norm_sq_eq_re_inner, ofReal_pow] theorem inner_self_re_eq_norm (x : E) : re ⟪x, x⟫ = ‖⟪x, x⟫‖ := by conv_rhs => rw [← inner_self_ofReal_re] symm exact norm_of_nonneg inner_self_nonneg theorem inner_self_ofReal_norm (x : E) : (‖⟪x, x⟫‖ : 𝕜) = ⟪x, x⟫ := by rw [← inner_self_re_eq_norm] exact inner_self_ofReal_re _ theorem real_inner_self_abs (x : F) : |⟪x, x⟫_ℝ| = ⟪x, x⟫_ℝ := @inner_self_ofReal_norm ℝ F _ _ _ x theorem norm_inner_symm (x y : E) : ‖⟪x, y⟫‖ = ‖⟪y, x⟫‖ := by rw [← inner_conj_symm, norm_conj] @[simp] theorem inner_neg_left (x y : E) : ⟪-x, y⟫ = -⟪x, y⟫ := by rw [← neg_one_smul 𝕜 x, inner_smul_left] simp @[simp] theorem inner_neg_right (x y : E) : ⟪x, -y⟫ = -⟪x, y⟫ := by rw [← inner_conj_symm, inner_neg_left]; simp only [RingHom.map_neg, inner_conj_symm] theorem inner_neg_neg (x y : E) : ⟪-x, -y⟫ = ⟪x, y⟫ := by simp theorem inner_self_conj (x : E) : ⟪x, x⟫† = ⟪x, x⟫ := inner_conj_symm _ _ theorem inner_sub_left (x y z : E) : ⟪x - y, z⟫ = ⟪x, z⟫ - ⟪y, z⟫ := by simp [sub_eq_add_neg, inner_add_left] theorem inner_sub_right (x y z : E) : ⟪x, y - z⟫ = ⟪x, y⟫ - ⟪x, z⟫ := by simp [sub_eq_add_neg, inner_add_right] theorem inner_mul_symm_re_eq_norm (x y : E) : re (⟪x, y⟫ * ⟪y, x⟫) = ‖⟪x, y⟫ * ⟪y, x⟫‖ := by rw [← inner_conj_symm, mul_comm] exact re_eq_norm_of_mul_conj (inner y x) /-- Expand `⟪x + y, x + y⟫` -/ theorem inner_add_add_self (x y : E) : ⟪x + y, x + y⟫ = ⟪x, x⟫ + ⟪x, y⟫ + ⟪y, x⟫ + ⟪y, y⟫ := by simp only [inner_add_left, inner_add_right]; ring /-- Expand `⟪x + y, x + y⟫_ℝ` -/ theorem real_inner_add_add_self (x y : F) : ⟪x + y, x + y⟫_ℝ = ⟪x, x⟫_ℝ + 2 * ⟪x, y⟫_ℝ + ⟪y, y⟫_ℝ := by have : ⟪y, x⟫_ℝ = ⟪x, y⟫_ℝ := by rw [← inner_conj_symm]; rfl simp only [inner_add_add_self, this, add_left_inj] ring -- Expand `⟪x - y, x - y⟫` theorem inner_sub_sub_self (x y : E) : ⟪x - y, x - y⟫ = ⟪x, x⟫ - ⟪x, y⟫ - ⟪y, x⟫ + ⟪y, y⟫ := by simp only [inner_sub_left, inner_sub_right]; ring /-- Expand `⟪x - y, x - y⟫_ℝ` -/ theorem real_inner_sub_sub_self (x y : F) : ⟪x - y, x - y⟫_ℝ = ⟪x, x⟫_ℝ - 2 * ⟪x, y⟫_ℝ + ⟪y, y⟫_ℝ := by have : ⟪y, x⟫_ℝ = ⟪x, y⟫_ℝ := by rw [← inner_conj_symm]; rfl simp only [inner_sub_sub_self, this, add_left_inj] ring /-- Parallelogram law -/ theorem parallelogram_law {x y : E} : ⟪x + y, x + y⟫ + ⟪x - y, x - y⟫ = 2 * (⟪x, x⟫ + ⟪y, y⟫) := by simp only [inner_add_add_self, inner_sub_sub_self] ring /-- **Cauchy–Schwarz inequality**. -/ theorem inner_mul_inner_self_le (x y : E) : ‖⟪x, y⟫‖ * ‖⟪y, x⟫‖ ≤ re ⟪x, x⟫ * re ⟪y, y⟫ := letI cd : PreInnerProductSpace.Core 𝕜 E := PreInnerProductSpace.toCore InnerProductSpace.Core.inner_mul_inner_self_le x y /-- Cauchy–Schwarz inequality for real inner products. -/ theorem real_inner_mul_inner_self_le (x y : F) : ⟪x, y⟫_ℝ * ⟪x, y⟫_ℝ ≤ ⟪x, x⟫_ℝ * ⟪y, y⟫_ℝ := calc ⟪x, y⟫_ℝ * ⟪x, y⟫_ℝ ≤ ‖⟪x, y⟫_ℝ‖ * ‖⟪y, x⟫_ℝ‖ := by rw [real_inner_comm y, ← norm_mul] exact le_abs_self _ _ ≤ ⟪x, x⟫_ℝ * ⟪y, y⟫_ℝ := @inner_mul_inner_self_le ℝ _ _ _ _ x y end BasicProperties_Seminormed section BasicProperties variable [NormedAddCommGroup E] [InnerProductSpace 𝕜 E] variable [NormedAddCommGroup F] [InnerProductSpace ℝ F] local notation "⟪" x ", " y "⟫" => @inner 𝕜 _ _ x y export InnerProductSpace (norm_sq_eq_re_inner) @[simp] theorem inner_self_eq_zero {x : E} : ⟪x, x⟫ = 0 ↔ x = 0 := by rw [inner_self_eq_norm_sq_to_K, sq_eq_zero_iff, ofReal_eq_zero, norm_eq_zero] theorem inner_self_ne_zero {x : E} : ⟪x, x⟫ ≠ 0 ↔ x ≠ 0 := inner_self_eq_zero.not variable (𝕜) theorem ext_inner_left {x y : E} (h : ∀ v, ⟪v, x⟫ = ⟪v, y⟫) : x = y := by rw [← sub_eq_zero, ← @inner_self_eq_zero 𝕜, inner_sub_right, sub_eq_zero, h (x - y)] theorem ext_inner_right {x y : E} (h : ∀ v, ⟪x, v⟫ = ⟪y, v⟫) : x = y := by rw [← sub_eq_zero, ← @inner_self_eq_zero 𝕜, inner_sub_left, sub_eq_zero, h (x - y)] variable {𝕜} @[simp] theorem re_inner_self_nonpos {x : E} : re ⟪x, x⟫ ≤ 0 ↔ x = 0 := by rw [← norm_sq_eq_re_inner, (sq_nonneg _).le_iff_eq, sq_eq_zero_iff, norm_eq_zero] @[simp] lemma re_inner_self_pos {x : E} : 0 < re ⟪x, x⟫ ↔ x ≠ 0 := by simpa [-re_inner_self_nonpos] using re_inner_self_nonpos (𝕜 := 𝕜) (x := x).not @[deprecated (since := "2025-04-22")] alias inner_self_nonpos := re_inner_self_nonpos @[deprecated (since := "2025-04-22")] alias inner_self_pos := re_inner_self_pos open scoped InnerProductSpace in theorem real_inner_self_nonpos {x : F} : ⟪x, x⟫_ℝ ≤ 0 ↔ x = 0 := re_inner_self_nonpos (𝕜 := ℝ) open scoped InnerProductSpace in theorem real_inner_self_pos {x : F} : 0 < ⟪x, x⟫_ℝ ↔ x ≠ 0 := re_inner_self_pos (𝕜 := ℝ) /-- A family of vectors is linearly independent if they are nonzero and orthogonal. -/ theorem linearIndependent_of_ne_zero_of_inner_eq_zero {ι : Type*} {v : ι → E} (hz : ∀ i, v i ≠ 0) (ho : Pairwise fun i j => ⟪v i, v j⟫ = 0) : LinearIndependent 𝕜 v := by rw [linearIndependent_iff'] intro s g hg i hi have h' : g i * inner (v i) (v i) = inner (v i) (∑ j ∈ s, g j • v j) := by rw [inner_sum] symm convert Finset.sum_eq_single (M := 𝕜) i ?_ ?_ · rw [inner_smul_right] · intro j _hj hji rw [inner_smul_right, ho hji.symm, mul_zero] · exact fun h => False.elim (h hi) simpa [hg, hz] using h' end BasicProperties section Norm_Seminormed open scoped InnerProductSpace variable [SeminormedAddCommGroup E] [InnerProductSpace 𝕜 E] variable [SeminormedAddCommGroup F] [InnerProductSpace ℝ F] local notation "⟪" x ", " y "⟫" => @inner 𝕜 _ _ x y local notation "IK" => @RCLike.I 𝕜 _ theorem norm_eq_sqrt_re_inner (x : E) : ‖x‖ = √(re ⟪x, x⟫) := calc ‖x‖ = √(‖x‖ ^ 2) := (sqrt_sq (norm_nonneg _)).symm _ = √(re ⟪x, x⟫) := congr_arg _ (norm_sq_eq_re_inner _) @[deprecated (since := "2025-04-22")] alias norm_eq_sqrt_inner := norm_eq_sqrt_re_inner theorem norm_eq_sqrt_real_inner (x : F) : ‖x‖ = √⟪x, x⟫_ℝ := @norm_eq_sqrt_re_inner ℝ _ _ _ _ x theorem inner_self_eq_norm_mul_norm (x : E) : re ⟪x, x⟫ = ‖x‖ * ‖x‖ := by rw [@norm_eq_sqrt_re_inner 𝕜, ← sqrt_mul inner_self_nonneg (re ⟪x, x⟫), sqrt_mul_self inner_self_nonneg] theorem inner_self_eq_norm_sq (x : E) : re ⟪x, x⟫ = ‖x‖ ^ 2 := by rw [pow_two, inner_self_eq_norm_mul_norm] theorem real_inner_self_eq_norm_mul_norm (x : F) : ⟪x, x⟫_ℝ = ‖x‖ * ‖x‖ := by have h := @inner_self_eq_norm_mul_norm ℝ F _ _ _ x simpa using h theorem real_inner_self_eq_norm_sq (x : F) : ⟪x, x⟫_ℝ = ‖x‖ ^ 2 := by rw [pow_two, real_inner_self_eq_norm_mul_norm] /-- Expand the square -/ theorem norm_add_sq (x y : E) : ‖x + y‖ ^ 2 = ‖x‖ ^ 2 + 2 * re ⟪x, y⟫ + ‖y‖ ^ 2 := by repeat' rw [sq (M := ℝ), ← @inner_self_eq_norm_mul_norm 𝕜] rw [inner_add_add_self, two_mul] simp only [add_assoc, add_left_inj, add_right_inj, AddMonoidHom.map_add] rw [← inner_conj_symm, conj_re] alias norm_add_pow_two := norm_add_sq /-- Expand the square -/ theorem norm_add_sq_real (x y : F) : ‖x + y‖ ^ 2 = ‖x‖ ^ 2 + 2 * ⟪x, y⟫_ℝ + ‖y‖ ^ 2 := by have h := @norm_add_sq ℝ _ _ _ _ x y simpa using h alias norm_add_pow_two_real := norm_add_sq_real /-- Expand the square -/ theorem norm_add_mul_self (x y : E) : ‖x + y‖ * ‖x + y‖ = ‖x‖ * ‖x‖ + 2 * re ⟪x, y⟫ + ‖y‖ * ‖y‖ := by repeat' rw [← sq (M := ℝ)] exact norm_add_sq _ _ /-- Expand the square -/ theorem norm_add_mul_self_real (x y : F) : ‖x + y‖ * ‖x + y‖ = ‖x‖ * ‖x‖ + 2 * ⟪x, y⟫_ℝ + ‖y‖ * ‖y‖ := by have h := @norm_add_mul_self ℝ _ _ _ _ x y simpa using h /-- Expand the square -/ theorem norm_sub_sq (x y : E) : ‖x - y‖ ^ 2 = ‖x‖ ^ 2 - 2 * re ⟪x, y⟫ + ‖y‖ ^ 2 := by rw [sub_eq_add_neg, @norm_add_sq 𝕜 _ _ _ _ x (-y), norm_neg, inner_neg_right, map_neg, mul_neg, sub_eq_add_neg] alias norm_sub_pow_two := norm_sub_sq /-- Expand the square -/ theorem norm_sub_sq_real (x y : F) : ‖x - y‖ ^ 2 = ‖x‖ ^ 2 - 2 * ⟪x, y⟫_ℝ + ‖y‖ ^ 2 := @norm_sub_sq ℝ _ _ _ _ _ _ alias norm_sub_pow_two_real := norm_sub_sq_real /-- Expand the square -/ theorem norm_sub_mul_self (x y : E) : ‖x - y‖ * ‖x - y‖ = ‖x‖ * ‖x‖ - 2 * re ⟪x, y⟫ + ‖y‖ * ‖y‖ := by repeat' rw [← sq (M := ℝ)] exact norm_sub_sq _ _ /-- Expand the square -/ theorem norm_sub_mul_self_real (x y : F) : ‖x - y‖ * ‖x - y‖ = ‖x‖ * ‖x‖ - 2 * ⟪x, y⟫_ℝ + ‖y‖ * ‖y‖ := by have h := @norm_sub_mul_self ℝ _ _ _ _ x y simpa using h /-- Cauchy–Schwarz inequality with norm -/ theorem norm_inner_le_norm (x y : E) : ‖⟪x, y⟫‖ ≤ ‖x‖ * ‖y‖ := by rw [norm_eq_sqrt_re_inner (𝕜 := 𝕜) x, norm_eq_sqrt_re_inner (𝕜 := 𝕜) y] letI : PreInnerProductSpace.Core 𝕜 E := PreInnerProductSpace.toCore exact InnerProductSpace.Core.norm_inner_le_norm x y theorem nnnorm_inner_le_nnnorm (x y : E) : ‖⟪x, y⟫‖₊ ≤ ‖x‖₊ * ‖y‖₊ := norm_inner_le_norm x y theorem re_inner_le_norm (x y : E) : re ⟪x, y⟫ ≤ ‖x‖ * ‖y‖ := le_trans (re_le_norm (inner x y)) (norm_inner_le_norm x y) /-- Cauchy–Schwarz inequality with norm -/ theorem abs_real_inner_le_norm (x y : F) : |⟪x, y⟫_ℝ| ≤ ‖x‖ * ‖y‖ := (Real.norm_eq_abs _).ge.trans (norm_inner_le_norm x y) /-- Cauchy–Schwarz inequality with norm -/ theorem real_inner_le_norm (x y : F) : ⟪x, y⟫_ℝ ≤ ‖x‖ * ‖y‖ := le_trans (le_abs_self _) (abs_real_inner_le_norm _ _) lemma inner_eq_zero_of_left {x : E} (y : E) (h : ‖x‖ = 0) : ⟪x, y⟫_𝕜 = 0 := by rw [← norm_eq_zero] refine le_antisymm ?_ (by positivity) exact norm_inner_le_norm _ _ |>.trans <| by simp [h] lemma inner_eq_zero_of_right (x : E) {y : E} (h : ‖y‖ = 0) : ⟪x, y⟫_𝕜 = 0 := by rw [inner_eq_zero_symm, inner_eq_zero_of_left _ h] variable (𝕜) include 𝕜 in theorem parallelogram_law_with_norm (x y : E) : ‖x + y‖ * ‖x + y‖ + ‖x - y‖ * ‖x - y‖ = 2 * (‖x‖ * ‖x‖ + ‖y‖ * ‖y‖) := by simp only [← @inner_self_eq_norm_mul_norm 𝕜] rw [← re.map_add, parallelogram_law, two_mul, two_mul] simp only [re.map_add] include 𝕜 in theorem parallelogram_law_with_nnnorm (x y : E) : ‖x + y‖₊ * ‖x + y‖₊ + ‖x - y‖₊ * ‖x - y‖₊ = 2 * (‖x‖₊ * ‖x‖₊ + ‖y‖₊ * ‖y‖₊) := Subtype.ext <| parallelogram_law_with_norm 𝕜 x y variable {𝕜} /-- Polarization identity: The real part of the inner product, in terms of the norm. -/ theorem re_inner_eq_norm_add_mul_self_sub_norm_mul_self_sub_norm_mul_self_div_two (x y : E) : re ⟪x, y⟫ = (‖x + y‖ * ‖x + y‖ - ‖x‖ * ‖x‖ - ‖y‖ * ‖y‖) / 2 := by rw [@norm_add_mul_self 𝕜] ring /-- Polarization identity: The real part of the inner product, in terms of the norm. -/ theorem re_inner_eq_norm_mul_self_add_norm_mul_self_sub_norm_sub_mul_self_div_two (x y : E) : re ⟪x, y⟫ = (‖x‖ * ‖x‖ + ‖y‖ * ‖y‖ - ‖x - y‖ * ‖x - y‖) / 2 := by rw [@norm_sub_mul_self 𝕜] ring /-- Polarization identity: The real part of the inner product, in terms of the norm. -/ theorem re_inner_eq_norm_add_mul_self_sub_norm_sub_mul_self_div_four (x y : E) : re ⟪x, y⟫ = (‖x + y‖ * ‖x + y‖ - ‖x - y‖ * ‖x - y‖) / 4 := by rw [@norm_add_mul_self 𝕜, @norm_sub_mul_self 𝕜] ring /-- Polarization identity: The imaginary part of the inner product, in terms of the norm. -/ theorem im_inner_eq_norm_sub_i_smul_mul_self_sub_norm_add_i_smul_mul_self_div_four (x y : E) : im ⟪x, y⟫ = (‖x - IK • y‖ * ‖x - IK • y‖ - ‖x + IK • y‖ * ‖x + IK • y‖) / 4 := by simp only [@norm_add_mul_self 𝕜, @norm_sub_mul_self 𝕜, inner_smul_right, I_mul_re] ring /-- Polarization identity: The inner product, in terms of the norm. -/ theorem inner_eq_sum_norm_sq_div_four (x y : E) : ⟪x, y⟫ = ((‖x + y‖ : 𝕜) ^ 2 - (‖x - y‖ : 𝕜) ^ 2 + ((‖x - IK • y‖ : 𝕜) ^ 2 - (‖x + IK • y‖ : 𝕜) ^ 2) * IK) / 4 := by rw [← re_add_im ⟪x, y⟫, re_inner_eq_norm_add_mul_self_sub_norm_sub_mul_self_div_four, im_inner_eq_norm_sub_i_smul_mul_self_sub_norm_add_i_smul_mul_self_div_four] push_cast simp only [sq, ← mul_div_right_comm, ← add_div] /-- Polarization identity: The real inner product, in terms of the norm. -/ theorem real_inner_eq_norm_add_mul_self_sub_norm_mul_self_sub_norm_mul_self_div_two (x y : F) : ⟪x, y⟫_ℝ = (‖x + y‖ * ‖x + y‖ - ‖x‖ * ‖x‖ - ‖y‖ * ‖y‖) / 2 := re_to_real.symm.trans <| re_inner_eq_norm_add_mul_self_sub_norm_mul_self_sub_norm_mul_self_div_two x y /-- Polarization identity: The real inner product, in terms of the norm. -/ theorem real_inner_eq_norm_mul_self_add_norm_mul_self_sub_norm_sub_mul_self_div_two (x y : F) : ⟪x, y⟫_ℝ = (‖x‖ * ‖x‖ + ‖y‖ * ‖y‖ - ‖x - y‖ * ‖x - y‖) / 2 := re_to_real.symm.trans <| re_inner_eq_norm_mul_self_add_norm_mul_self_sub_norm_sub_mul_self_div_two x y /-- Pythagorean theorem, if-and-only-if vector inner product form. -/ theorem norm_add_sq_eq_norm_sq_add_norm_sq_iff_real_inner_eq_zero (x y : F) : ‖x + y‖ * ‖x + y‖ = ‖x‖ * ‖x‖ + ‖y‖ * ‖y‖ ↔ ⟪x, y⟫_ℝ = 0 := by rw [@norm_add_mul_self ℝ, add_right_cancel_iff, add_eq_left, mul_eq_zero] norm_num /-- Pythagorean theorem, if-and-if vector inner product form using square roots. -/ theorem norm_add_eq_sqrt_iff_real_inner_eq_zero {x y : F} : ‖x + y‖ = √(‖x‖ * ‖x‖ + ‖y‖ * ‖y‖) ↔ ⟪x, y⟫_ℝ = 0 := by rw [← norm_add_sq_eq_norm_sq_add_norm_sq_iff_real_inner_eq_zero, eq_comm, sqrt_eq_iff_mul_self_eq, eq_comm] <;> positivity /-- Pythagorean theorem, vector inner product form. -/ theorem norm_add_sq_eq_norm_sq_add_norm_sq_of_inner_eq_zero (x y : E) (h : ⟪x, y⟫ = 0) : ‖x + y‖ * ‖x + y‖ = ‖x‖ * ‖x‖ + ‖y‖ * ‖y‖ := by rw [@norm_add_mul_self 𝕜, add_right_cancel_iff, add_eq_left, mul_eq_zero] apply Or.inr simp only [h, zero_re'] /-- Pythagorean theorem, vector inner product form. -/ theorem norm_add_sq_eq_norm_sq_add_norm_sq_real {x y : F} (h : ⟪x, y⟫_ℝ = 0) : ‖x + y‖ * ‖x + y‖ = ‖x‖ * ‖x‖ + ‖y‖ * ‖y‖ := (norm_add_sq_eq_norm_sq_add_norm_sq_iff_real_inner_eq_zero x y).2 h /-- Pythagorean theorem, subtracting vectors, if-and-only-if vector inner product form. -/ theorem norm_sub_sq_eq_norm_sq_add_norm_sq_iff_real_inner_eq_zero (x y : F) : ‖x - y‖ * ‖x - y‖ = ‖x‖ * ‖x‖ + ‖y‖ * ‖y‖ ↔ ⟪x, y⟫_ℝ = 0 := by rw [@norm_sub_mul_self ℝ, add_right_cancel_iff, sub_eq_add_neg, add_eq_left, neg_eq_zero, mul_eq_zero] norm_num /-- Pythagorean theorem, subtracting vectors, if-and-if vector inner product form using square roots. -/ theorem norm_sub_eq_sqrt_iff_real_inner_eq_zero {x y : F} : ‖x - y‖ = √(‖x‖ * ‖x‖ + ‖y‖ * ‖y‖) ↔ ⟪x, y⟫_ℝ = 0 := by rw [← norm_sub_sq_eq_norm_sq_add_norm_sq_iff_real_inner_eq_zero, eq_comm, sqrt_eq_iff_mul_self_eq, eq_comm] <;> positivity /-- Pythagorean theorem, subtracting vectors, vector inner product form. -/ theorem norm_sub_sq_eq_norm_sq_add_norm_sq_real {x y : F} (h : ⟪x, y⟫_ℝ = 0) : ‖x - y‖ * ‖x - y‖ = ‖x‖ * ‖x‖ + ‖y‖ * ‖y‖ := (norm_sub_sq_eq_norm_sq_add_norm_sq_iff_real_inner_eq_zero x y).2 h /-- The sum and difference of two vectors are orthogonal if and only if they have the same norm. -/ theorem real_inner_add_sub_eq_zero_iff (x y : F) : ⟪x + y, x - y⟫_ℝ = 0 ↔ ‖x‖ = ‖y‖ := by conv_rhs => rw [← mul_self_inj_of_nonneg (norm_nonneg _) (norm_nonneg _)] simp only [← @inner_self_eq_norm_mul_norm ℝ, inner_add_left, inner_sub_right, real_inner_comm y x, sub_eq_zero, re_to_real] constructor · intro h rw [add_comm] at h linarith · intro h linarith /-- Given two orthogonal vectors, their sum and difference have equal norms. -/ theorem norm_sub_eq_norm_add {v w : E} (h : ⟪v, w⟫ = 0) : ‖w - v‖ = ‖w + v‖ := by rw [← mul_self_inj_of_nonneg (norm_nonneg _) (norm_nonneg _)] simp only [h, ← @inner_self_eq_norm_mul_norm 𝕜, sub_neg_eq_add, sub_zero, map_sub, zero_re', zero_sub, add_zero, map_add, inner_add_right, inner_sub_left, inner_sub_right, inner_re_symm, zero_add] /-- The real inner product of two vectors, divided by the product of their norms, has absolute value at most 1. -/ theorem abs_real_inner_div_norm_mul_norm_le_one (x y : F) : |⟪x, y⟫_ℝ / (‖x‖ * ‖y‖)| ≤ 1 := by rw [abs_div, abs_mul, abs_norm, abs_norm] exact div_le_one_of_le₀ (abs_real_inner_le_norm x y) (by positivity) /-- The inner product of a vector with a multiple of itself. -/ theorem real_inner_smul_self_left (x : F) (r : ℝ) : ⟪r • x, x⟫_ℝ = r * (‖x‖ * ‖x‖) := by rw [real_inner_smul_left, ← real_inner_self_eq_norm_mul_norm]
/-- The inner product of a vector with a multiple of itself. -/ theorem real_inner_smul_self_right (x : F) (r : ℝ) : ⟪x, r • x⟫_ℝ = r * (‖x‖ * ‖x‖) := by rw [inner_smul_right, ← real_inner_self_eq_norm_mul_norm]
Mathlib/Analysis/InnerProductSpace/Basic.lean
582
585
/- Copyright (c) 2022 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou, Yury Kudryashov, Heather Macbeth -/ import Mathlib.MeasureTheory.Function.L1Space.AEEqFun import Mathlib.MeasureTheory.Function.LpSpace.Complete import Mathlib.MeasureTheory.Function.LpSpace.Indicator /-! # Density of simple functions Show that each `Lᵖ` Borel measurable function can be approximated in `Lᵖ` norm by a sequence of simple functions. ## Main definitions * `MeasureTheory.Lp.simpleFunc`, the type of `Lp` simple functions * `coeToLp`, the embedding of `Lp.simpleFunc E p μ` into `Lp E p μ` ## Main results * `tendsto_approxOn_Lp_eLpNorm` (Lᵖ convergence): If `E` is a `NormedAddCommGroup` and `f` is measurable and `MemLp` (for `p < ∞`), then the simple functions `SimpleFunc.approxOn f hf s 0 h₀ n` may be considered as elements of `Lp E p μ`, and they tend in Lᵖ to `f`. * `Lp.simpleFunc.isDenseEmbedding`: the embedding `coeToLp` of the `Lp` simple functions into `Lp` is dense. * `Lp.simpleFunc.induction`, `Lp.induction`, `MemLp.induction`, `Integrable.induction`: to prove a predicate for all elements of one of these classes of functions, it suffices to check that it behaves correctly on simple functions. ## TODO For `E` finite-dimensional, simple functions `α →ₛ E` are dense in L^∞ -- prove this. ## Notations * `α →ₛ β` (local notation): the type of simple functions `α → β`. * `α →₁ₛ[μ] E`: the type of `L1` simple functions `α → β`. -/ noncomputable section open Set Function Filter TopologicalSpace ENNReal EMetric Finset open scoped Topology ENNReal MeasureTheory variable {α β ι E F 𝕜 : Type*} namespace MeasureTheory local infixr:25 " →ₛ " => SimpleFunc namespace SimpleFunc /-! ### Lp approximation by simple functions -/ section Lp variable [MeasurableSpace β] [MeasurableSpace E] [NormedAddCommGroup E] [NormedAddCommGroup F] {q : ℝ} {p : ℝ≥0∞} theorem nnnorm_approxOn_le [OpensMeasurableSpace E] {f : β → E} (hf : Measurable f) {s : Set E} {y₀ : E} (h₀ : y₀ ∈ s) [SeparableSpace s] (x : β) (n : ℕ) : ‖approxOn f hf s y₀ h₀ n x - f x‖₊ ≤ ‖f x - y₀‖₊ := by have := edist_approxOn_le hf h₀ x n rw [edist_comm y₀] at this simp only [edist_nndist, nndist_eq_nnnorm] at this exact mod_cast this theorem norm_approxOn_y₀_le [OpensMeasurableSpace E] {f : β → E} (hf : Measurable f) {s : Set E} {y₀ : E} (h₀ : y₀ ∈ s) [SeparableSpace s] (x : β) (n : ℕ) : ‖approxOn f hf s y₀ h₀ n x - y₀‖ ≤ ‖f x - y₀‖ + ‖f x - y₀‖ := by simpa [enorm, edist_eq_enorm_sub, ← ENNReal.coe_add, norm_sub_rev] using edist_approxOn_y0_le hf h₀ x n theorem norm_approxOn_zero_le [OpensMeasurableSpace E] {f : β → E} (hf : Measurable f) {s : Set E} (h₀ : (0 : E) ∈ s) [SeparableSpace s] (x : β) (n : ℕ) : ‖approxOn f hf s 0 h₀ n x‖ ≤ ‖f x‖ + ‖f x‖ := by simpa [enorm, edist_eq_enorm_sub, ← ENNReal.coe_add, norm_sub_rev] using edist_approxOn_y0_le hf h₀ x n theorem tendsto_approxOn_Lp_eLpNorm [OpensMeasurableSpace E] {f : β → E} (hf : Measurable f) {s : Set E} {y₀ : E} (h₀ : y₀ ∈ s) [SeparableSpace s] (hp_ne_top : p ≠ ∞) {μ : Measure β} (hμ : ∀ᵐ x ∂μ, f x ∈ closure s) (hi : eLpNorm (fun x => f x - y₀) p μ < ∞) : Tendsto (fun n => eLpNorm (⇑(approxOn f hf s y₀ h₀ n) - f) p μ) atTop (𝓝 0) := by by_cases hp_zero : p = 0 · simpa only [hp_zero, eLpNorm_exponent_zero] using tendsto_const_nhds have hp : 0 < p.toReal := toReal_pos hp_zero hp_ne_top suffices Tendsto (fun n => ∫⁻ x, ‖approxOn f hf s y₀ h₀ n x - f x‖ₑ ^ p.toReal ∂μ) atTop (𝓝 0) by simp only [eLpNorm_eq_lintegral_rpow_enorm hp_zero hp_ne_top] convert continuous_rpow_const.continuousAt.tendsto.comp this simp [zero_rpow_of_pos (_root_.inv_pos.mpr hp)] -- We simply check the conditions of the Dominated Convergence Theorem: -- (1) The function "`p`-th power of distance between `f` and the approximation" is measurable have hF_meas n : Measurable fun x => ‖approxOn f hf s y₀ h₀ n x - f x‖ₑ ^ p.toReal := by simpa only [← edist_eq_enorm_sub] using (approxOn f hf s y₀ h₀ n).measurable_bind (fun y x => edist y (f x) ^ p.toReal) fun y => (measurable_edist_right.comp hf).pow_const p.toReal -- (2) The functions "`p`-th power of distance between `f` and the approximation" are uniformly -- bounded, at any given point, by `fun x => ‖f x - y₀‖ ^ p.toReal` have h_bound n : (fun x ↦ ‖approxOn f hf s y₀ h₀ n x - f x‖ₑ ^ p.toReal) ≤ᵐ[μ] (‖f · - y₀‖ₑ ^ p.toReal) := .of_forall fun x => rpow_le_rpow (coe_mono (nnnorm_approxOn_le hf h₀ x n)) toReal_nonneg -- (3) The bounding function `fun x => ‖f x - y₀‖ ^ p.toReal` has finite integral have h_fin : (∫⁻ a : β, ‖f a - y₀‖ₑ ^ p.toReal ∂μ) ≠ ⊤ := (lintegral_rpow_enorm_lt_top_of_eLpNorm_lt_top hp_zero hp_ne_top hi).ne -- (4) The functions "`p`-th power of distance between `f` and the approximation" tend pointwise -- to zero have h_lim : ∀ᵐ a : β ∂μ, Tendsto (‖approxOn f hf s y₀ h₀ · a - f a‖ₑ ^ p.toReal) atTop (𝓝 0) := by filter_upwards [hμ] with a ha have : Tendsto (fun n => (approxOn f hf s y₀ h₀ n) a - f a) atTop (𝓝 (f a - f a)) := (tendsto_approxOn hf h₀ ha).sub tendsto_const_nhds convert continuous_rpow_const.continuousAt.tendsto.comp (tendsto_coe.mpr this.nnnorm) simp [zero_rpow_of_pos hp] -- Then we apply the Dominated Convergence Theorem simpa using tendsto_lintegral_of_dominated_convergence _ hF_meas h_bound h_fin h_lim theorem memLp_approxOn [BorelSpace E] {f : β → E} {μ : Measure β} (fmeas : Measurable f) (hf : MemLp f p μ) {s : Set E} {y₀ : E} (h₀ : y₀ ∈ s) [SeparableSpace s] (hi₀ : MemLp (fun _ => y₀) p μ) (n : ℕ) : MemLp (approxOn f fmeas s y₀ h₀ n) p μ := by refine ⟨(approxOn f fmeas s y₀ h₀ n).aestronglyMeasurable, ?_⟩ suffices eLpNorm (fun x => approxOn f fmeas s y₀ h₀ n x - y₀) p μ < ⊤ by have : MemLp (fun x => approxOn f fmeas s y₀ h₀ n x - y₀) p μ := ⟨(approxOn f fmeas s y₀ h₀ n - const β y₀).aestronglyMeasurable, this⟩ convert eLpNorm_add_lt_top this hi₀ ext x simp have hf' : MemLp (fun x => ‖f x - y₀‖) p μ := by have h_meas : Measurable fun x => ‖f x - y₀‖ := by simp only [← dist_eq_norm] exact (continuous_id.dist continuous_const).measurable.comp fmeas refine ⟨h_meas.aemeasurable.aestronglyMeasurable, ?_⟩ rw [eLpNorm_norm] convert eLpNorm_add_lt_top hf hi₀.neg with x simp [sub_eq_add_neg] have : ∀ᵐ x ∂μ, ‖approxOn f fmeas s y₀ h₀ n x - y₀‖ ≤ ‖‖f x - y₀‖ + ‖f x - y₀‖‖ := by filter_upwards with x convert norm_approxOn_y₀_le fmeas h₀ x n using 1 rw [Real.norm_eq_abs, abs_of_nonneg] positivity calc eLpNorm (fun x => approxOn f fmeas s y₀ h₀ n x - y₀) p μ ≤ eLpNorm (fun x => ‖f x - y₀‖ + ‖f x - y₀‖) p μ := eLpNorm_mono_ae this _ < ⊤ := eLpNorm_add_lt_top hf' hf' theorem tendsto_approxOn_range_Lp_eLpNorm [BorelSpace E] {f : β → E} (hp_ne_top : p ≠ ∞) {μ : Measure β} (fmeas : Measurable f) [SeparableSpace (range f ∪ {0} : Set E)] (hf : eLpNorm f p μ < ∞) : Tendsto (fun n => eLpNorm (⇑(approxOn f fmeas (range f ∪ {0}) 0 (by simp) n) - f) p μ) atTop (𝓝 0) := by refine tendsto_approxOn_Lp_eLpNorm fmeas _ hp_ne_top ?_ ?_ · filter_upwards with x using subset_closure (by simp) · simpa using hf theorem memLp_approxOn_range [BorelSpace E] {f : β → E} {μ : Measure β} (fmeas : Measurable f) [SeparableSpace (range f ∪ {0} : Set E)] (hf : MemLp f p μ) (n : ℕ) : MemLp (approxOn f fmeas (range f ∪ {0}) 0 (by simp) n) p μ := memLp_approxOn fmeas hf (y₀ := 0) (by simp) MemLp.zero n theorem tendsto_approxOn_range_Lp [BorelSpace E] {f : β → E} [hp : Fact (1 ≤ p)] (hp_ne_top : p ≠ ∞) {μ : Measure β} (fmeas : Measurable f) [SeparableSpace (range f ∪ {0} : Set E)] (hf : MemLp f p μ) : Tendsto (fun n => (memLp_approxOn_range fmeas hf n).toLp (approxOn f fmeas (range f ∪ {0}) 0 (by simp) n)) atTop (𝓝 (hf.toLp f)) := by simpa only [Lp.tendsto_Lp_iff_tendsto_eLpNorm''] using tendsto_approxOn_range_Lp_eLpNorm hp_ne_top fmeas hf.2 /-- Any function in `ℒp` can be approximated by a simple function if `p < ∞`. -/ theorem _root_.MeasureTheory.MemLp.exists_simpleFunc_eLpNorm_sub_lt {E : Type*} [NormedAddCommGroup E] {f : β → E} {μ : Measure β} (hf : MemLp f p μ) (hp_ne_top : p ≠ ∞) {ε : ℝ≥0∞} (hε : ε ≠ 0) : ∃ g : β →ₛ E, eLpNorm (f - ⇑g) p μ < ε ∧ MemLp g p μ := by borelize E let f' := hf.1.mk f rsuffices ⟨g, hg, g_mem⟩ : ∃ g : β →ₛ E, eLpNorm (f' - ⇑g) p μ < ε ∧ MemLp g p μ · refine ⟨g, ?_, g_mem⟩ suffices eLpNorm (f - ⇑g) p μ = eLpNorm (f' - ⇑g) p μ by rwa [this] apply eLpNorm_congr_ae filter_upwards [hf.1.ae_eq_mk] with x hx simpa only [Pi.sub_apply, sub_left_inj] using hx have hf' : MemLp f' p μ := hf.ae_eq hf.1.ae_eq_mk have f'meas : Measurable f' := hf.1.measurable_mk have : SeparableSpace (range f' ∪ {0} : Set E) := StronglyMeasurable.separableSpace_range_union_singleton hf.1.stronglyMeasurable_mk rcases ((tendsto_approxOn_range_Lp_eLpNorm hp_ne_top f'meas hf'.2).eventually <| gt_mem_nhds hε.bot_lt).exists with ⟨n, hn⟩ rw [← eLpNorm_neg, neg_sub] at hn exact ⟨_, hn, memLp_approxOn_range f'meas hf' _⟩ end Lp /-! ### L1 approximation by simple functions -/ section Integrable variable [MeasurableSpace β] variable [MeasurableSpace E] [NormedAddCommGroup E] theorem tendsto_approxOn_L1_enorm [OpensMeasurableSpace E] {f : β → E} (hf : Measurable f) {s : Set E} {y₀ : E} (h₀ : y₀ ∈ s) [SeparableSpace s] {μ : Measure β} (hμ : ∀ᵐ x ∂μ, f x ∈ closure s) (hi : HasFiniteIntegral (fun x => f x - y₀) μ) : Tendsto (fun n => ∫⁻ x, ‖approxOn f hf s y₀ h₀ n x - f x‖ₑ ∂μ) atTop (𝓝 0) := by simpa [eLpNorm_one_eq_lintegral_enorm] using tendsto_approxOn_Lp_eLpNorm hf h₀ one_ne_top hμ (by simpa [eLpNorm_one_eq_lintegral_enorm] using hi) @[deprecated (since := "2025-01-21")] alias tendsto_approxOn_L1_nnnorm := tendsto_approxOn_L1_enorm theorem integrable_approxOn [BorelSpace E] {f : β → E} {μ : Measure β} (fmeas : Measurable f) (hf : Integrable f μ) {s : Set E} {y₀ : E} (h₀ : y₀ ∈ s) [SeparableSpace s] (hi₀ : Integrable (fun _ => y₀) μ) (n : ℕ) : Integrable (approxOn f fmeas s y₀ h₀ n) μ := by rw [← memLp_one_iff_integrable] at hf hi₀ ⊢ exact memLp_approxOn fmeas hf h₀ hi₀ n theorem tendsto_approxOn_range_L1_enorm [OpensMeasurableSpace E] {f : β → E} {μ : Measure β} [SeparableSpace (range f ∪ {0} : Set E)] (fmeas : Measurable f) (hf : Integrable f μ) : Tendsto (fun n => ∫⁻ x, ‖approxOn f fmeas (range f ∪ {0}) 0 (by simp) n x - f x‖ₑ ∂μ) atTop (𝓝 0) := by apply tendsto_approxOn_L1_enorm fmeas · filter_upwards with x using subset_closure (by simp) · simpa using hf.2 @[deprecated (since := "2025-01-21")] alias tendsto_approxOn_range_L1_nnnorm := tendsto_approxOn_range_L1_enorm theorem integrable_approxOn_range [BorelSpace E] {f : β → E} {μ : Measure β} (fmeas : Measurable f) [SeparableSpace (range f ∪ {0} : Set E)] (hf : Integrable f μ) (n : ℕ) : Integrable (approxOn f fmeas (range f ∪ {0}) 0 (by simp) n) μ := integrable_approxOn fmeas hf _ (integrable_zero _ _ _) n end Integrable section SimpleFuncProperties variable [MeasurableSpace α] variable [NormedAddCommGroup E] [NormedAddCommGroup F] variable {μ : Measure α} {p : ℝ≥0∞} /-! ### Properties of simple functions in `Lp` spaces A simple function `f : α →ₛ E` into a normed group `E` verifies, for a measure `μ`: - `MemLp f 0 μ` and `MemLp f ∞ μ`, since `f` is a.e.-measurable and bounded, - for `0 < p < ∞`, `MemLp f p μ ↔ Integrable f μ ↔ f.FinMeasSupp μ ↔ ∀ y, y ≠ 0 → μ (f ⁻¹' {y}) < ∞`. -/ theorem exists_forall_norm_le (f : α →ₛ F) : ∃ C, ∀ x, ‖f x‖ ≤ C := exists_forall_le (f.map fun x => ‖x‖) theorem memLp_zero (f : α →ₛ E) (μ : Measure α) : MemLp f 0 μ := memLp_zero_iff_aestronglyMeasurable.mpr f.aestronglyMeasurable theorem memLp_top (f : α →ₛ E) (μ : Measure α) : MemLp f ∞ μ := let ⟨C, hfC⟩ := f.exists_forall_norm_le memLp_top_of_bound f.aestronglyMeasurable C <| Eventually.of_forall hfC protected theorem eLpNorm'_eq {p : ℝ} (f : α →ₛ F) (μ : Measure α) : eLpNorm' f p μ = (∑ y ∈ f.range, ‖y‖ₑ ^ p * μ (f ⁻¹' {y})) ^ (1 / p) := by have h_map : (‖f ·‖ₑ ^ p) = f.map (‖·‖ₑ ^ p) := by simp; rfl rw [eLpNorm'_eq_lintegral_enorm, h_map, lintegral_eq_lintegral, map_lintegral] theorem measure_preimage_lt_top_of_memLp (hp_pos : p ≠ 0) (hp_ne_top : p ≠ ∞) (f : α →ₛ E) (hf : MemLp f p μ) (y : E) (hy_ne : y ≠ 0) : μ (f ⁻¹' {y}) < ∞ := by have hp_pos_real : 0 < p.toReal := ENNReal.toReal_pos hp_pos hp_ne_top have hf_eLpNorm := MemLp.eLpNorm_lt_top hf rw [eLpNorm_eq_eLpNorm' hp_pos hp_ne_top, f.eLpNorm'_eq, one_div, ← @ENNReal.lt_rpow_inv_iff _ _ p.toReal⁻¹ (by simp [hp_pos_real]), @ENNReal.top_rpow_of_pos p.toReal⁻¹⁻¹ (by simp [hp_pos_real]), ENNReal.sum_lt_top] at hf_eLpNorm by_cases hyf : y ∈ f.range swap · suffices h_empty : f ⁻¹' {y} = ∅ by rw [h_empty, measure_empty]; exact ENNReal.coe_lt_top ext1 x rw [Set.mem_preimage, Set.mem_singleton_iff, mem_empty_iff_false, iff_false] refine fun hxy => hyf ?_ rw [mem_range, Set.mem_range] exact ⟨x, hxy⟩ specialize hf_eLpNorm y hyf rw [ENNReal.mul_lt_top_iff] at hf_eLpNorm cases hf_eLpNorm with | inl hf_eLpNorm => exact hf_eLpNorm.2 | inr hf_eLpNorm => cases hf_eLpNorm with | inl hf_eLpNorm => refine absurd ?_ hy_ne simpa [hp_pos_real] using hf_eLpNorm | inr hf_eLpNorm => simp [hf_eLpNorm] theorem memLp_of_finite_measure_preimage (p : ℝ≥0∞) {f : α →ₛ E} (hf : ∀ y, y ≠ 0 → μ (f ⁻¹' {y}) < ∞) : MemLp f p μ := by by_cases hp0 : p = 0 · rw [hp0, memLp_zero_iff_aestronglyMeasurable]; exact f.aestronglyMeasurable by_cases hp_top : p = ∞ · rw [hp_top]; exact memLp_top f μ refine ⟨f.aestronglyMeasurable, ?_⟩ rw [eLpNorm_eq_eLpNorm' hp0 hp_top, f.eLpNorm'_eq] refine ENNReal.rpow_lt_top_of_nonneg (by simp) (ENNReal.sum_lt_top.mpr fun y _ => ?_).ne by_cases hy0 : y = 0 · simp [hy0, ENNReal.toReal_pos hp0 hp_top] · refine ENNReal.mul_lt_top ?_ (hf y hy0) exact ENNReal.rpow_lt_top_of_nonneg ENNReal.toReal_nonneg ENNReal.coe_ne_top theorem memLp_iff {f : α →ₛ E} (hp_pos : p ≠ 0) (hp_ne_top : p ≠ ∞) : MemLp f p μ ↔ ∀ y, y ≠ 0 → μ (f ⁻¹' {y}) < ∞ := ⟨fun h => measure_preimage_lt_top_of_memLp hp_pos hp_ne_top f h, fun h => memLp_of_finite_measure_preimage p h⟩ theorem integrable_iff {f : α →ₛ E} : Integrable f μ ↔ ∀ y, y ≠ 0 → μ (f ⁻¹' {y}) < ∞ := memLp_one_iff_integrable.symm.trans <| memLp_iff one_ne_zero ENNReal.coe_ne_top theorem memLp_iff_integrable {f : α →ₛ E} (hp_pos : p ≠ 0) (hp_ne_top : p ≠ ∞) : MemLp f p μ ↔ Integrable f μ := (memLp_iff hp_pos hp_ne_top).trans integrable_iff.symm theorem memLp_iff_finMeasSupp {f : α →ₛ E} (hp_pos : p ≠ 0) (hp_ne_top : p ≠ ∞) : MemLp f p μ ↔ f.FinMeasSupp μ := (memLp_iff hp_pos hp_ne_top).trans finMeasSupp_iff.symm theorem integrable_iff_finMeasSupp {f : α →ₛ E} : Integrable f μ ↔ f.FinMeasSupp μ := integrable_iff.trans finMeasSupp_iff.symm theorem FinMeasSupp.integrable {f : α →ₛ E} (h : f.FinMeasSupp μ) : Integrable f μ := integrable_iff_finMeasSupp.2 h theorem integrable_pair {f : α →ₛ E} {g : α →ₛ F} : Integrable f μ → Integrable g μ → Integrable (pair f g) μ := by simpa only [integrable_iff_finMeasSupp] using FinMeasSupp.pair theorem memLp_of_isFiniteMeasure (f : α →ₛ E) (p : ℝ≥0∞) (μ : Measure α) [IsFiniteMeasure μ] : MemLp f p μ := let ⟨C, hfC⟩ := f.exists_forall_norm_le MemLp.of_bound f.aestronglyMeasurable C <| Eventually.of_forall hfC @[fun_prop] theorem integrable_of_isFiniteMeasure [IsFiniteMeasure μ] (f : α →ₛ E) : Integrable f μ := memLp_one_iff_integrable.mp (f.memLp_of_isFiniteMeasure 1 μ) theorem measure_preimage_lt_top_of_integrable (f : α →ₛ E) (hf : Integrable f μ) {x : E} (hx : x ≠ 0) : μ (f ⁻¹' {x}) < ∞ := integrable_iff.mp hf x hx theorem measure_support_lt_top_of_memLp (f : α →ₛ E) (hf : MemLp f p μ) (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) : μ (support f) < ∞ := f.measure_support_lt_top ((memLp_iff hp_ne_zero hp_ne_top).mp hf) theorem measure_support_lt_top_of_integrable (f : α →ₛ E) (hf : Integrable f μ) : μ (support f) < ∞ := f.measure_support_lt_top (integrable_iff.mp hf) theorem measure_lt_top_of_memLp_indicator (hp_pos : p ≠ 0) (hp_ne_top : p ≠ ∞) {c : E} (hc : c ≠ 0) {s : Set α} (hs : MeasurableSet s) (hcs : MemLp ((const α c).piecewise s hs (const α 0)) p μ) : μ s < ⊤ := by have : Function.support (const α c) = Set.univ := Function.support_const hc simpa only [memLp_iff_finMeasSupp hp_pos hp_ne_top, finMeasSupp_iff_support, support_indicator, Set.inter_univ, this] using hcs end SimpleFuncProperties end SimpleFunc /-! Construction of the space of `Lp` simple functions, and its dense embedding into `Lp`. -/ namespace Lp open AEEqFun variable [MeasurableSpace α] [NormedAddCommGroup E] [NormedAddCommGroup F] (p : ℝ≥0∞) (μ : Measure α) variable (E) /-- `Lp.simpleFunc` is a subspace of Lp consisting of equivalence classes of an integrable simple function. -/ def simpleFunc : AddSubgroup (Lp E p μ) where carrier := { f : Lp E p μ | ∃ s : α →ₛ E, (AEEqFun.mk s s.aestronglyMeasurable : α →ₘ[μ] E) = f } zero_mem' := ⟨0, rfl⟩ add_mem' := by rintro f g ⟨s, hs⟩ ⟨t, ht⟩ use s + t simp only [← hs, ← ht, AEEqFun.mk_add_mk, AddSubgroup.coe_add, AEEqFun.mk_eq_mk, SimpleFunc.coe_add] neg_mem' := by rintro f ⟨s, hs⟩ use -s simp only [← hs, AEEqFun.neg_mk, SimpleFunc.coe_neg, AEEqFun.mk_eq_mk, AddSubgroup.coe_neg] variable {E p μ} namespace simpleFunc section Instances /-! Simple functions in Lp space form a `NormedSpace`. -/ protected theorem eq' {f g : Lp.simpleFunc E p μ} : (f : α →ₘ[μ] E) = (g : α →ₘ[μ] E) → f = g := Subtype.eq ∘ Subtype.eq /-! Implementation note: If `Lp.simpleFunc E p μ` were defined as a `𝕜`-submodule of `Lp E p μ`, then the next few lemmas, putting a normed `𝕜`-group structure on `Lp.simpleFunc E p μ`, would be unnecessary. But instead, `Lp.simpleFunc E p μ` is defined as an `AddSubgroup` of `Lp E p μ`, which does not permit this (but has the advantage of working when `E` itself is a normed group, i.e. has no scalar action). -/ variable [NormedRing 𝕜] [Module 𝕜 E] [IsBoundedSMul 𝕜 E] /-- If `E` is a normed space, `Lp.simpleFunc E p μ` is a `SMul`. Not declared as an instance as it is (as of writing) used only in the construction of the Bochner integral. -/ protected def smul : SMul 𝕜 (Lp.simpleFunc E p μ) := ⟨fun k f => ⟨k • (f : Lp E p μ), by rcases f with ⟨f, ⟨s, hs⟩⟩ use k • s apply Eq.trans (AEEqFun.smul_mk k s s.aestronglyMeasurable).symm _ rw [hs] rfl⟩⟩ attribute [local instance] simpleFunc.smul @[simp, norm_cast] theorem coe_smul (c : 𝕜) (f : Lp.simpleFunc E p μ) : ((c • f : Lp.simpleFunc E p μ) : Lp E p μ) = c • (f : Lp E p μ) := rfl /-- If `E` is a normed space, `Lp.simpleFunc E p μ` is a module. Not declared as an instance as it is (as of writing) used only in the construction of the Bochner integral. -/ protected def module : Module 𝕜 (Lp.simpleFunc E p μ) where one_smul f := by ext1; exact one_smul _ _ mul_smul x y f := by ext1; exact mul_smul _ _ _ smul_add x f g := by ext1; exact smul_add _ _ _ smul_zero x := by ext1; exact smul_zero _ add_smul x y f := by ext1; exact add_smul _ _ _ zero_smul f := by ext1; exact zero_smul _ _ attribute [local instance] simpleFunc.module /-- If `E` is a normed space, `Lp.simpleFunc E p μ` is a normed space. Not declared as an instance as it is (as of writing) used only in the construction of the Bochner integral. -/ protected theorem isBoundedSMul [Fact (1 ≤ p)] : IsBoundedSMul 𝕜 (Lp.simpleFunc E p μ) := IsBoundedSMul.of_norm_smul_le fun r f => (norm_smul_le r (f : Lp E p μ) :) @[deprecated (since := "2025-03-10")] protected alias boundedSMul := simpleFunc.isBoundedSMul attribute [local instance] simpleFunc.isBoundedSMul /-- If `E` is a normed space, `Lp.simpleFunc E p μ` is a normed space. Not declared as an instance as it is (as of writing) used only in the construction of the Bochner integral. -/ protected def normedSpace {𝕜} [NormedField 𝕜] [NormedSpace 𝕜 E] [Fact (1 ≤ p)] : NormedSpace 𝕜 (Lp.simpleFunc E p μ) := ⟨norm_smul_le (α := 𝕜) (β := Lp.simpleFunc E p μ)⟩ end Instances attribute [local instance] simpleFunc.module simpleFunc.normedSpace simpleFunc.isBoundedSMul section ToLp /-- Construct the equivalence class `[f]` of a simple function `f` satisfying `MemLp`. -/ abbrev toLp (f : α →ₛ E) (hf : MemLp f p μ) : Lp.simpleFunc E p μ := ⟨hf.toLp f, ⟨f, rfl⟩⟩ theorem toLp_eq_toLp (f : α →ₛ E) (hf : MemLp f p μ) : (toLp f hf : Lp E p μ) = hf.toLp f := rfl theorem toLp_eq_mk (f : α →ₛ E) (hf : MemLp f p μ) : (toLp f hf : α →ₘ[μ] E) = AEEqFun.mk f f.aestronglyMeasurable := rfl theorem toLp_zero : toLp (0 : α →ₛ E) MemLp.zero = (0 : Lp.simpleFunc E p μ) := rfl theorem toLp_add (f g : α →ₛ E) (hf : MemLp f p μ) (hg : MemLp g p μ) : toLp (f + g) (hf.add hg) = toLp f hf + toLp g hg := rfl theorem toLp_neg (f : α →ₛ E) (hf : MemLp f p μ) : toLp (-f) hf.neg = -toLp f hf := rfl theorem toLp_sub (f g : α →ₛ E) (hf : MemLp f p μ) (hg : MemLp g p μ) : toLp (f - g) (hf.sub hg) = toLp f hf - toLp g hg := by simp only [sub_eq_add_neg, ← toLp_neg, ← toLp_add] variable [NormedRing 𝕜] [Module 𝕜 E] [IsBoundedSMul 𝕜 E] theorem toLp_smul (f : α →ₛ E) (hf : MemLp f p μ) (c : 𝕜) : toLp (c • f) (hf.const_smul c) = c • toLp f hf := rfl nonrec theorem norm_toLp [Fact (1 ≤ p)] (f : α →ₛ E) (hf : MemLp f p μ) : ‖toLp f hf‖ = ENNReal.toReal (eLpNorm f p μ) := norm_toLp f hf end ToLp section ToSimpleFunc /-- Find a representative of a `Lp.simpleFunc`. -/ def toSimpleFunc (f : Lp.simpleFunc E p μ) : α →ₛ E := Classical.choose f.2 /-- `(toSimpleFunc f)` is measurable. -/ @[measurability] protected theorem measurable [MeasurableSpace E] (f : Lp.simpleFunc E p μ) : Measurable (toSimpleFunc f) := (toSimpleFunc f).measurable protected theorem stronglyMeasurable (f : Lp.simpleFunc E p μ) : StronglyMeasurable (toSimpleFunc f) := (toSimpleFunc f).stronglyMeasurable @[measurability] protected theorem aemeasurable [MeasurableSpace E] (f : Lp.simpleFunc E p μ) : AEMeasurable (toSimpleFunc f) μ := (simpleFunc.measurable f).aemeasurable protected theorem aestronglyMeasurable (f : Lp.simpleFunc E p μ) : AEStronglyMeasurable (toSimpleFunc f) μ := (simpleFunc.stronglyMeasurable f).aestronglyMeasurable theorem toSimpleFunc_eq_toFun (f : Lp.simpleFunc E p μ) : toSimpleFunc f =ᵐ[μ] f := show ⇑(toSimpleFunc f) =ᵐ[μ] ⇑(f : α →ₘ[μ] E) by convert (AEEqFun.coeFn_mk (toSimpleFunc f) (toSimpleFunc f).aestronglyMeasurable).symm using 2 exact (Classical.choose_spec f.2).symm /-- `toSimpleFunc f` satisfies the predicate `MemLp`. -/ protected theorem memLp (f : Lp.simpleFunc E p μ) : MemLp (toSimpleFunc f) p μ := MemLp.ae_eq (toSimpleFunc_eq_toFun f).symm <| mem_Lp_iff_memLp.mp (f : Lp E p μ).2 theorem toLp_toSimpleFunc (f : Lp.simpleFunc E p μ) : toLp (toSimpleFunc f) (simpleFunc.memLp f) = f := simpleFunc.eq' (Classical.choose_spec f.2) theorem toSimpleFunc_toLp (f : α →ₛ E) (hfi : MemLp f p μ) : toSimpleFunc (toLp f hfi) =ᵐ[μ] f := by rw [← AEEqFun.mk_eq_mk]; exact Classical.choose_spec (toLp f hfi).2 variable (E μ) theorem zero_toSimpleFunc : toSimpleFunc (0 : Lp.simpleFunc E p μ) =ᵐ[μ] 0 := by filter_upwards [toSimpleFunc_eq_toFun (0 : Lp.simpleFunc E p μ), Lp.coeFn_zero E 1 μ] with _ h₁ _ rwa [h₁] variable {E μ} theorem add_toSimpleFunc (f g : Lp.simpleFunc E p μ) : toSimpleFunc (f + g) =ᵐ[μ] toSimpleFunc f + toSimpleFunc g := by filter_upwards [toSimpleFunc_eq_toFun (f + g), toSimpleFunc_eq_toFun f, toSimpleFunc_eq_toFun g, Lp.coeFn_add (f : Lp E p μ) g] with _ simp only [AddSubgroup.coe_add, Pi.add_apply] iterate 4 intro h; rw [h] theorem neg_toSimpleFunc (f : Lp.simpleFunc E p μ) : toSimpleFunc (-f) =ᵐ[μ] -toSimpleFunc f := by filter_upwards [toSimpleFunc_eq_toFun (-f), toSimpleFunc_eq_toFun f, Lp.coeFn_neg (f : Lp E p μ)] with _ simp only [Pi.neg_apply, AddSubgroup.coe_neg] repeat intro h; rw [h] theorem sub_toSimpleFunc (f g : Lp.simpleFunc E p μ) : toSimpleFunc (f - g) =ᵐ[μ] toSimpleFunc f - toSimpleFunc g := by filter_upwards [toSimpleFunc_eq_toFun (f - g), toSimpleFunc_eq_toFun f, toSimpleFunc_eq_toFun g, Lp.coeFn_sub (f : Lp E p μ) g] with _ simp only [AddSubgroup.coe_sub, Pi.sub_apply] repeat' intro h; rw [h] variable [NormedRing 𝕜] [Module 𝕜 E] [IsBoundedSMul 𝕜 E] theorem smul_toSimpleFunc (k : 𝕜) (f : Lp.simpleFunc E p μ) : toSimpleFunc (k • f) =ᵐ[μ] k • ⇑(toSimpleFunc f) := by filter_upwards [toSimpleFunc_eq_toFun (k • f), toSimpleFunc_eq_toFun f, Lp.coeFn_smul k (f : Lp E p μ)] with _ simp only [Pi.smul_apply, coe_smul] repeat intro h; rw [h] theorem norm_toSimpleFunc [Fact (1 ≤ p)] (f : Lp.simpleFunc E p μ) : ‖f‖ = ENNReal.toReal (eLpNorm (toSimpleFunc f) p μ) := by simpa [toLp_toSimpleFunc] using norm_toLp (toSimpleFunc f) (simpleFunc.memLp f) end ToSimpleFunc section Induction variable (p) in /-- The characteristic function of a finite-measure measurable set `s`, as an `Lp` simple function. -/ def indicatorConst {s : Set α} (hs : MeasurableSet s) (hμs : μ s ≠ ∞) (c : E) : Lp.simpleFunc E p μ := toLp ((SimpleFunc.const _ c).piecewise s hs (SimpleFunc.const _ 0)) (memLp_indicator_const p hs c (Or.inr hμs)) @[simp] theorem coe_indicatorConst {s : Set α} (hs : MeasurableSet s) (hμs : μ s ≠ ∞) (c : E) : (↑(indicatorConst p hs hμs c) : Lp E p μ) = indicatorConstLp p hs hμs c := rfl theorem toSimpleFunc_indicatorConst {s : Set α} (hs : MeasurableSet s) (hμs : μ s ≠ ∞) (c : E) : toSimpleFunc (indicatorConst p hs hμs c) =ᵐ[μ] (SimpleFunc.const _ c).piecewise s hs (SimpleFunc.const _ 0) := Lp.simpleFunc.toSimpleFunc_toLp _ _ /-- To prove something for an arbitrary `Lp` simple function, with `0 < p < ∞`, it suffices to show that the property holds for (multiples of) characteristic functions of finite-measure measurable sets and is closed under addition (of functions with disjoint support). -/ @[elab_as_elim] protected theorem induction (hp_pos : p ≠ 0) (hp_ne_top : p ≠ ∞) {P : Lp.simpleFunc E p μ → Prop} (indicatorConst : ∀ (c : E) {s : Set α} (hs : MeasurableSet s) (hμs : μ s < ∞), P (Lp.simpleFunc.indicatorConst p hs hμs.ne c)) (add : ∀ ⦃f g : α →ₛ E⦄, ∀ hf : MemLp f p μ, ∀ hg : MemLp g p μ, Disjoint (support f) (support g) → P (Lp.simpleFunc.toLp f hf) → P (Lp.simpleFunc.toLp g hg) → P (Lp.simpleFunc.toLp f hf + Lp.simpleFunc.toLp g hg)) (f : Lp.simpleFunc E p μ) : P f := by suffices ∀ f : α →ₛ E, ∀ hf : MemLp f p μ, P (toLp f hf) by rw [← toLp_toSimpleFunc f] apply this clear f apply SimpleFunc.induction · intro c s hs hf by_cases hc : c = 0 · convert indicatorConst 0 MeasurableSet.empty (by simp) using 1 ext1 simp [hc] exact indicatorConst c hs (SimpleFunc.measure_lt_top_of_memLp_indicator hp_pos hp_ne_top hc hs hf) · intro f g hfg hf hg hfg' obtain ⟨hf', hg'⟩ : MemLp f p μ ∧ MemLp g p μ := (memLp_add_of_disjoint hfg f.stronglyMeasurable g.stronglyMeasurable).mp hfg' exact add hf' hg' hfg (hf hf') (hg hg') end Induction section CoeToLp variable [Fact (1 ≤ p)] protected theorem uniformContinuous : UniformContinuous ((↑) : Lp.simpleFunc E p μ → Lp E p μ) := uniformContinuous_comap lemma isUniformEmbedding : IsUniformEmbedding ((↑) : Lp.simpleFunc E p μ → Lp E p μ) := isUniformEmbedding_comap Subtype.val_injective theorem isUniformInducing : IsUniformInducing ((↑) : Lp.simpleFunc E p μ → Lp E p μ) := simpleFunc.isUniformEmbedding.isUniformInducing lemma isDenseEmbedding (hp_ne_top : p ≠ ∞) : IsDenseEmbedding ((↑) : Lp.simpleFunc E p μ → Lp E p μ) := by borelize E apply simpleFunc.isUniformEmbedding.isDenseEmbedding intro f rw [mem_closure_iff_seq_limit] have hfi' : MemLp f p μ := Lp.memLp f haveI : SeparableSpace (range f ∪ {0} : Set E) := (Lp.stronglyMeasurable f).separableSpace_range_union_singleton refine ⟨fun n => toLp (SimpleFunc.approxOn f (Lp.stronglyMeasurable f).measurable (range f ∪ {0}) 0 _ n) (SimpleFunc.memLp_approxOn_range (Lp.stronglyMeasurable f).measurable hfi' n), fun n => mem_range_self _, ?_⟩ convert SimpleFunc.tendsto_approxOn_range_Lp hp_ne_top (Lp.stronglyMeasurable f).measurable hfi' rw [toLp_coeFn f (Lp.memLp f)] protected theorem isDenseInducing (hp_ne_top : p ≠ ∞) : IsDenseInducing ((↑) : Lp.simpleFunc E p μ → Lp E p μ) := (simpleFunc.isDenseEmbedding hp_ne_top).isDenseInducing protected theorem denseRange (hp_ne_top : p ≠ ∞) : DenseRange ((↑) : Lp.simpleFunc E p μ → Lp E p μ) := (simpleFunc.isDenseInducing hp_ne_top).dense protected theorem dense (hp_ne_top : p ≠ ∞) : Dense (Lp.simpleFunc E p μ : Set (Lp E p μ)) := by simpa only [denseRange_subtype_val] using simpleFunc.denseRange (E := E) (μ := μ) hp_ne_top variable [NormedRing 𝕜] [Module 𝕜 E] [IsBoundedSMul 𝕜 E] variable (α E 𝕜) /-- The embedding of Lp simple functions into Lp functions, as a continuous linear map. -/ def coeToLp : Lp.simpleFunc E p μ →L[𝕜] Lp E p μ := { AddSubgroup.subtype (Lp.simpleFunc E p μ) with map_smul' := fun _ _ => rfl cont := Lp.simpleFunc.uniformContinuous.continuous } variable {α E 𝕜}
end CoeToLp section Order variable {G : Type*} [NormedAddCommGroup G] theorem coeFn_le [PartialOrder G] (f g : Lp.simpleFunc G p μ) : (f : α → G) ≤ᵐ[μ] g ↔ f ≤ g := by rw [← Subtype.coe_le_coe, ← Lp.coeFn_le] instance instAddLeftMono [PartialOrder G] [IsOrderedAddMonoid G] : AddLeftMono (Lp.simpleFunc G p μ) := by refine ⟨fun f g₁ g₂ hg₁₂ => ?_⟩ exact add_le_add_left hg₁₂ f variable (p μ G) theorem coeFn_zero : (0 : Lp.simpleFunc G p μ) =ᵐ[μ] (0 : α → G) := Lp.coeFn_zero _ _ _ variable {p μ G} variable [PartialOrder G] theorem coeFn_nonneg (f : Lp.simpleFunc G p μ) : (0 : α → G) ≤ᵐ[μ] f ↔ 0 ≤ f := by rw [← Subtype.coe_le_coe, Lp.coeFn_nonneg, AddSubmonoid.coe_zero] theorem exists_simpleFunc_nonneg_ae_eq {f : Lp.simpleFunc G p μ} (hf : 0 ≤ f) :
Mathlib/MeasureTheory/Function/SimpleFuncDenseLp.lean
703
729
/- Copyright (c) 2022 Joël Riou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joël Riou -/ import Mathlib.Algebra.Homology.Homotopy import Mathlib.AlgebraicTopology.DoldKan.Notations /-! # Construction of homotopies for the Dold-Kan correspondence (The general strategy of proof of the Dold-Kan correspondence is explained in `Equivalence.lean`.) The purpose of the files `Homotopies.lean`, `Faces.lean`, `Projections.lean` and `PInfty.lean` is to construct an idempotent endomorphism `PInfty : K[X] ⟶ K[X]` of the alternating face map complex for each `X : SimplicialObject C` when `C` is a preadditive category. In the case `C` is abelian, this `PInfty` shall be the projection on the normalized Moore subcomplex of `K[X]` associated to the decomposition of the complex `K[X]` as a direct sum of this normalized subcomplex and of the degenerate subcomplex. In `PInfty.lean`, this endomorphism `PInfty` shall be obtained by passing to the limit idempotent endomorphisms `P q` for all `(q : ℕ)`. These endomorphisms `P q` are defined by induction. The idea is to start from the identity endomorphism `P 0` of `K[X]` and to ensure by induction that the `q` higher face maps (except $d_0$) vanish on the image of `P q`. Then, in a certain degree `n`, the image of `P q` for a big enough `q` will be contained in the normalized subcomplex. This construction is done in `Projections.lean`. It would be easy to define the `P q` degreewise (similarly as it is done in *Simplicial Homotopy Theory* by Goerrs-Jardine p. 149), but then we would have to prove that they are compatible with the differential (i.e. they are chain complex maps), and also that they are homotopic to the identity. These two verifications are quite technical. In order to reduce the number of such technical lemmas, the strategy that is followed here is to define a series of null homotopic maps `Hσ q` (attached to families of maps `hσ`) and use these in order to construct `P q` : the endomorphisms `P q` shall basically be obtained by altering the identity endomorphism by adding null homotopic maps, so that we get for free that they are morphisms of chain complexes and that they are homotopic to the identity. The most technical verifications that are needed about the null homotopic maps `Hσ` are obtained in `Faces.lean`. In this file `Homotopies.lean`, we define the null homotopic maps `Hσ q : K[X] ⟶ K[X]`, show that they are natural (see `natTransHσ`) and compatible the application of additive functors (see `map_Hσ`). ## References * [Albrecht Dold, *Homology of Symmetric Products and Other Functors of Complexes*][dold1958] * [Paul G. Goerss, John F. Jardine, *Simplicial Homotopy Theory*][goerss-jardine-2009] -/ open CategoryTheory CategoryTheory.Category CategoryTheory.Limits CategoryTheory.Preadditive CategoryTheory.SimplicialObject Homotopy Opposite Simplicial DoldKan noncomputable section namespace AlgebraicTopology namespace DoldKan variable {C : Type*} [Category C] [Preadditive C] variable {X : SimplicialObject C} /-- As we are using chain complexes indexed by `ℕ`, we shall need the relation `c` such `c m n` if and only if `n+1=m`. -/ abbrev c := ComplexShape.down ℕ /-- Helper when we need some `c.rel i j` (i.e. `ComplexShape.down ℕ`), e.g. `c_mk n (n+1) rfl` -/ theorem c_mk (i j : ℕ) (h : j + 1 = i) : c.Rel i j := ComplexShape.down_mk i j h /-- This lemma is meant to be used with `nullHomotopicMap'_f_of_not_rel_left` -/ theorem cs_down_0_not_rel_left (j : ℕ) : ¬c.Rel 0 j := by intro hj dsimp at hj apply Nat.not_succ_le_zero j rw [Nat.succ_eq_add_one, hj] /-- The sequence of maps which gives the null homotopic maps `Hσ` that shall be in the inductive construction of the projections `P q : K[X] ⟶ K[X]` -/ def hσ (q : ℕ) (n : ℕ) : X _⦋n⦌ ⟶ X _⦋n + 1⦌ := if n < q then 0 else (-1 : ℤ) ^ (n - q) • X.σ ⟨n - q, Nat.lt_succ_of_le (Nat.sub_le _ _)⟩ /-- We can turn `hσ` into a datum that can be passed to `nullHomotopicMap'`. -/ def hσ' (q : ℕ) : ∀ n m, c.Rel m n → (K[X].X n ⟶ K[X].X m) := fun n m hnm => hσ q n ≫ eqToHom (by congr) theorem hσ'_eq_zero {q n m : ℕ} (hnq : n < q) (hnm : c.Rel m n) : (hσ' q n m hnm : X _⦋n⦌ ⟶ X _⦋m⦌) = 0 := by simp only [hσ', hσ] split_ifs exact zero_comp theorem hσ'_eq {q n a m : ℕ} (ha : n = a + q) (hnm : c.Rel m n) : (hσ' q n m hnm : X _⦋n⦌ ⟶ X _⦋m⦌) = ((-1 : ℤ) ^ a • X.σ ⟨a, Nat.lt_succ_iff.mpr (Nat.le.intro (Eq.symm ha))⟩) ≫ eqToHom (by congr) := by simp only [hσ', hσ] split_ifs · omega · have h' := tsub_eq_of_eq_add ha congr theorem hσ'_eq' {q n a : ℕ} (ha : n = a + q) : (hσ' q n (n + 1) rfl : X _⦋n⦌ ⟶ X _⦋n + 1⦌) = (-1 : ℤ) ^ a • X.σ ⟨a, Nat.lt_succ_iff.mpr (Nat.le.intro (Eq.symm ha))⟩ := by rw [hσ'_eq ha rfl, eqToHom_refl, comp_id] /-- The null homotopic map $(hσ q) ∘ d + d ∘ (hσ q)$ -/ def Hσ (q : ℕ) : K[X] ⟶ K[X] := nullHomotopicMap' (hσ' q) /-- `Hσ` is null homotopic -/ def homotopyHσToZero (q : ℕ) : Homotopy (Hσ q : K[X] ⟶ K[X]) 0 := nullHomotopy' (hσ' q) /-- In degree `0`, the null homotopic map `Hσ` is zero. -/ theorem Hσ_eq_zero (q : ℕ) : (Hσ q : K[X] ⟶ K[X]).f 0 = 0 := by unfold Hσ rw [nullHomotopicMap'_f_of_not_rel_left (c_mk 1 0 rfl) cs_down_0_not_rel_left] rcases q with (_|q) · rw [hσ'_eq (show 0 = 0 + 0 by rfl) (c_mk 1 0 rfl)] simp only [pow_zero, Fin.mk_zero, one_zsmul, eqToHom_refl, Category.comp_id] -- This `erw` is needed to show `0 + 1 = 1`. erw [ChainComplex.of_d] rw [AlternatingFaceMapComplex.objD, Fin.sum_univ_two, Fin.val_zero, Fin.val_one, pow_zero, pow_one, one_smul, neg_smul, one_smul, comp_add, comp_neg, add_neg_eq_zero, ← Fin.castSucc_zero, ← Fin.succ_zero_eq_one, δ_comp_σ_self, δ_comp_σ_succ] · rw [hσ'_eq_zero (Nat.succ_pos q) (c_mk 1 0 rfl), zero_comp] /-- The maps `hσ' q n m hnm` are natural on the simplicial object -/
theorem hσ'_naturality (q : ℕ) (n m : ℕ) (hnm : c.Rel m n) {X Y : SimplicialObject C} (f : X ⟶ Y) : f.app (op ⦋n⦌) ≫ hσ' q n m hnm = hσ' q n m hnm ≫ f.app (op ⦋m⦌) := by have h : n + 1 = m := hnm subst h simp only [hσ', eqToHom_refl, comp_id] unfold hσ split_ifs · rw [zero_comp, comp_zero] · simp /-- For each q, `Hσ q` is a natural transformation. -/
Mathlib/AlgebraicTopology/DoldKan/Homotopies.lean
141
151
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Algebra.Order.Ring.Nat import Mathlib.Algebra.Ring.Int.Defs import Mathlib.Data.Nat.Bitwise import Mathlib.Data.Nat.Cast.Order.Basic import Mathlib.Data.Nat.PSub import Mathlib.Data.Nat.Size import Mathlib.Data.Num.Bitwise /-! # Properties of the binary representation of integers -/ open Int attribute [local simp] add_assoc namespace PosNum variable {α : Type*} @[simp, norm_cast] theorem cast_one [One α] [Add α] : ((1 : PosNum) : α) = 1 := rfl @[simp] theorem cast_one' [One α] [Add α] : (PosNum.one : α) = 1 := rfl @[simp, norm_cast] theorem cast_bit0 [One α] [Add α] (n : PosNum) : (n.bit0 : α) = (n : α) + n := rfl @[simp, norm_cast] theorem cast_bit1 [One α] [Add α] (n : PosNum) : (n.bit1 : α) = ((n : α) + n) + 1 := rfl @[simp, norm_cast] theorem cast_to_nat [AddMonoidWithOne α] : ∀ n : PosNum, ((n : ℕ) : α) = n | 1 => Nat.cast_one | bit0 p => by dsimp; rw [Nat.cast_add, p.cast_to_nat] | bit1 p => by dsimp; rw [Nat.cast_add, Nat.cast_add, Nat.cast_one, p.cast_to_nat] @[norm_cast] theorem to_nat_to_int (n : PosNum) : ((n : ℕ) : ℤ) = n := cast_to_nat _ @[simp, norm_cast] theorem cast_to_int [AddGroupWithOne α] (n : PosNum) : ((n : ℤ) : α) = n := by rw [← to_nat_to_int, Int.cast_natCast, cast_to_nat] theorem succ_to_nat : ∀ n, (succ n : ℕ) = n + 1 | 1 => rfl | bit0 _ => rfl | bit1 p => (congr_arg (fun n ↦ n + n) (succ_to_nat p)).trans <| show ↑p + 1 + ↑p + 1 = ↑p + ↑p + 1 + 1 by simp [add_left_comm] theorem one_add (n : PosNum) : 1 + n = succ n := by cases n <;> rfl theorem add_one (n : PosNum) : n + 1 = succ n := by cases n <;> rfl @[norm_cast] theorem add_to_nat : ∀ m n, ((m + n : PosNum) : ℕ) = m + n | 1, b => by rw [one_add b, succ_to_nat, add_comm, cast_one] | a, 1 => by rw [add_one a, succ_to_nat, cast_one] | bit0 a, bit0 b => (congr_arg (fun n ↦ n + n) (add_to_nat a b)).trans <| add_add_add_comm _ _ _ _ | bit0 a, bit1 b => (congr_arg (fun n ↦ (n + n) + 1) (add_to_nat a b)).trans <| show (a + b + (a + b) + 1 : ℕ) = a + a + (b + b + 1) by simp [add_left_comm] | bit1 a, bit0 b => (congr_arg (fun n ↦ (n + n) + 1) (add_to_nat a b)).trans <| show (a + b + (a + b) + 1 : ℕ) = a + a + 1 + (b + b) by simp [add_comm, add_left_comm] | bit1 a, bit1 b => show (succ (a + b) + succ (a + b) : ℕ) = a + a + 1 + (b + b + 1) by rw [succ_to_nat, add_to_nat a b]; simp [add_left_comm] theorem add_succ : ∀ m n : PosNum, m + succ n = succ (m + n) | 1, b => by simp [one_add] | bit0 a, 1 => congr_arg bit0 (add_one a) | bit1 a, 1 => congr_arg bit1 (add_one a) | bit0 _, bit0 _ => rfl | bit0 a, bit1 b => congr_arg bit0 (add_succ a b) | bit1 _, bit0 _ => rfl | bit1 a, bit1 b => congr_arg bit1 (add_succ a b) theorem bit0_of_bit0 : ∀ n, n + n = bit0 n | 1 => rfl | bit0 p => congr_arg bit0 (bit0_of_bit0 p) | bit1 p => show bit0 (succ (p + p)) = _ by rw [bit0_of_bit0 p, succ] theorem bit1_of_bit1 (n : PosNum) : (n + n) + 1 = bit1 n := show (n + n) + 1 = bit1 n by rw [add_one, bit0_of_bit0, succ] @[norm_cast] theorem mul_to_nat (m) : ∀ n, ((m * n : PosNum) : ℕ) = m * n | 1 => (mul_one _).symm | bit0 p => show (↑(m * p) + ↑(m * p) : ℕ) = ↑m * (p + p) by rw [mul_to_nat m p, left_distrib] | bit1 p => (add_to_nat (bit0 (m * p)) m).trans <| show (↑(m * p) + ↑(m * p) + ↑m : ℕ) = ↑m * (p + p) + m by rw [mul_to_nat m p, left_distrib] theorem to_nat_pos : ∀ n : PosNum, 0 < (n : ℕ) | 1 => Nat.zero_lt_one | bit0 p => let h := to_nat_pos p add_pos h h | bit1 _p => Nat.succ_pos _ theorem cmp_to_nat_lemma {m n : PosNum} : (m : ℕ) < n → (bit1 m : ℕ) < bit0 n := show (m : ℕ) < n → (m + m + 1 + 1 : ℕ) ≤ n + n by intro h; rw [Nat.add_right_comm m m 1, add_assoc]; exact Nat.add_le_add h h theorem cmp_swap (m) : ∀ n, (cmp m n).swap = cmp n m := by induction' m with m IH m IH <;> intro n <;> obtain - | n | n := n <;> unfold cmp <;> try { rfl } <;> rw [← IH] <;> cases cmp m n <;> rfl theorem cmp_to_nat : ∀ m n, (Ordering.casesOn (cmp m n) ((m : ℕ) < n) (m = n) ((n : ℕ) < m) : Prop) | 1, 1 => rfl | bit0 a, 1 => let h : (1 : ℕ) ≤ a := to_nat_pos a Nat.add_le_add h h | bit1 a, 1 => Nat.succ_lt_succ <| to_nat_pos <| bit0 a | 1, bit0 b => let h : (1 : ℕ) ≤ b := to_nat_pos b Nat.add_le_add h h | 1, bit1 b => Nat.succ_lt_succ <| to_nat_pos <| bit0 b | bit0 a, bit0 b => by dsimp [cmp] have := cmp_to_nat a b; revert this; cases cmp a b <;> dsimp <;> intro this · exact Nat.add_lt_add this this · rw [this] · exact Nat.add_lt_add this this | bit0 a, bit1 b => by dsimp [cmp] have := cmp_to_nat a b; revert this; cases cmp a b <;> dsimp <;> intro this · exact Nat.le_succ_of_le (Nat.add_lt_add this this) · rw [this] apply Nat.lt_succ_self · exact cmp_to_nat_lemma this | bit1 a, bit0 b => by dsimp [cmp] have := cmp_to_nat a b; revert this; cases cmp a b <;> dsimp <;> intro this · exact cmp_to_nat_lemma this · rw [this] apply Nat.lt_succ_self · exact Nat.le_succ_of_le (Nat.add_lt_add this this) | bit1 a, bit1 b => by dsimp [cmp] have := cmp_to_nat a b; revert this; cases cmp a b <;> dsimp <;> intro this · exact Nat.succ_lt_succ (Nat.add_lt_add this this) · rw [this] · exact Nat.succ_lt_succ (Nat.add_lt_add this this) @[norm_cast] theorem lt_to_nat {m n : PosNum} : (m : ℕ) < n ↔ m < n := show (m : ℕ) < n ↔ cmp m n = Ordering.lt from match cmp m n, cmp_to_nat m n with | Ordering.lt, h => by simp only at h; simp [h] | Ordering.eq, h => by simp only at h; simp [h, lt_irrefl] | Ordering.gt, h => by simp [not_lt_of_gt h] @[norm_cast] theorem le_to_nat {m n : PosNum} : (m : ℕ) ≤ n ↔ m ≤ n := by rw [← not_lt]; exact not_congr lt_to_nat end PosNum namespace Num variable {α : Type*} open PosNum theorem add_zero (n : Num) : n + 0 = n := by cases n <;> rfl theorem zero_add (n : Num) : 0 + n = n := by cases n <;> rfl theorem add_one : ∀ n : Num, n + 1 = succ n | 0 => rfl | pos p => by cases p <;> rfl theorem add_succ : ∀ m n : Num, m + succ n = succ (m + n) | 0, n => by simp [zero_add] | pos p, 0 => show pos (p + 1) = succ (pos p + 0) by rw [PosNum.add_one, add_zero, succ, succ'] | pos _, pos _ => congr_arg pos (PosNum.add_succ _ _) theorem bit0_of_bit0 : ∀ n : Num, n + n = n.bit0 | 0 => rfl | pos p => congr_arg pos p.bit0_of_bit0 theorem bit1_of_bit1 : ∀ n : Num, (n + n) + 1 = n.bit1 | 0 => rfl | pos p => congr_arg pos p.bit1_of_bit1 @[simp] theorem ofNat'_zero : Num.ofNat' 0 = 0 := by simp [Num.ofNat'] theorem ofNat'_bit (b n) : ofNat' (Nat.bit b n) = cond b Num.bit1 Num.bit0 (ofNat' n) := Nat.binaryRec_eq _ _ (.inl rfl) @[simp] theorem ofNat'_one : Num.ofNat' 1 = 1 := by erw [ofNat'_bit true 0, cond, ofNat'_zero]; rfl theorem bit1_succ : ∀ n : Num, n.bit1.succ = n.succ.bit0 | 0 => rfl | pos _n => rfl theorem ofNat'_succ : ∀ {n}, ofNat' (n + 1) = ofNat' n + 1 := @(Nat.binaryRec (by simp [zero_add]) fun b n ih => by cases b · erw [ofNat'_bit true n, ofNat'_bit] simp only [← bit1_of_bit1, ← bit0_of_bit0, cond] · rw [show n.bit true + 1 = (n + 1).bit false by simp [Nat.bit, mul_add], ofNat'_bit, ofNat'_bit, ih] simp only [cond, add_one, bit1_succ]) @[simp] theorem add_ofNat' (m n) : Num.ofNat' (m + n) = Num.ofNat' m + Num.ofNat' n := by induction n · simp only [Nat.add_zero, ofNat'_zero, add_zero] · simp only [Nat.add_succ, Nat.add_zero, ofNat'_succ, add_one, add_succ, *] @[simp, norm_cast] theorem cast_zero [Zero α] [One α] [Add α] : ((0 : Num) : α) = 0 := rfl @[simp] theorem cast_zero' [Zero α] [One α] [Add α] : (Num.zero : α) = 0 := rfl @[simp, norm_cast] theorem cast_one [Zero α] [One α] [Add α] : ((1 : Num) : α) = 1 := rfl @[simp] theorem cast_pos [Zero α] [One α] [Add α] (n : PosNum) : (Num.pos n : α) = n := rfl theorem succ'_to_nat : ∀ n, (succ' n : ℕ) = n + 1 | 0 => (Nat.zero_add _).symm | pos _p => PosNum.succ_to_nat _ theorem succ_to_nat (n) : (succ n : ℕ) = n + 1 := succ'_to_nat n @[simp, norm_cast] theorem cast_to_nat [AddMonoidWithOne α] : ∀ n : Num, ((n : ℕ) : α) = n | 0 => Nat.cast_zero | pos p => p.cast_to_nat @[norm_cast] theorem add_to_nat : ∀ m n, ((m + n : Num) : ℕ) = m + n | 0, 0 => rfl | 0, pos _q => (Nat.zero_add _).symm | pos _p, 0 => rfl | pos _p, pos _q => PosNum.add_to_nat _ _ @[norm_cast] theorem mul_to_nat : ∀ m n, ((m * n : Num) : ℕ) = m * n | 0, 0 => rfl | 0, pos _q => (zero_mul _).symm | pos _p, 0 => rfl | pos _p, pos _q => PosNum.mul_to_nat _ _ theorem cmp_to_nat : ∀ m n, (Ordering.casesOn (cmp m n) ((m : ℕ) < n) (m = n) ((n : ℕ) < m) : Prop) | 0, 0 => rfl | 0, pos _ => to_nat_pos _ | pos _, 0 => to_nat_pos _ | pos a, pos b => by have := PosNum.cmp_to_nat a b; revert this; dsimp [cmp]; cases PosNum.cmp a b exacts [id, congr_arg pos, id] @[norm_cast] theorem lt_to_nat {m n : Num} : (m : ℕ) < n ↔ m < n := show (m : ℕ) < n ↔ cmp m n = Ordering.lt from match cmp m n, cmp_to_nat m n with | Ordering.lt, h => by simp only at h; simp [h] | Ordering.eq, h => by simp only at h; simp [h, lt_irrefl] | Ordering.gt, h => by simp [not_lt_of_gt h] @[norm_cast] theorem le_to_nat {m n : Num} : (m : ℕ) ≤ n ↔ m ≤ n := by rw [← not_lt]; exact not_congr lt_to_nat end Num namespace PosNum @[simp] theorem of_to_nat' : ∀ n : PosNum, Num.ofNat' (n : ℕ) = Num.pos n | 1 => by erw [@Num.ofNat'_bit true 0, Num.ofNat'_zero]; rfl | bit0 p => by simpa only [Nat.bit_false, cond_false, two_mul, of_to_nat' p] using Num.ofNat'_bit false p | bit1 p => by simpa only [Nat.bit_true, cond_true, two_mul, of_to_nat' p] using Num.ofNat'_bit true p end PosNum namespace Num @[simp, norm_cast] theorem of_to_nat' : ∀ n : Num, Num.ofNat' (n : ℕ) = n | 0 => ofNat'_zero | pos p => p.of_to_nat' lemma toNat_injective : Function.Injective (castNum : Num → ℕ) := Function.LeftInverse.injective of_to_nat' @[norm_cast] theorem to_nat_inj {m n : Num} : (m : ℕ) = n ↔ m = n := toNat_injective.eq_iff /-- This tactic tries to turn an (in)equality about `Num`s to one about `Nat`s by rewriting. ```lean example (n : Num) (m : Num) : n ≤ n + m := by transfer_rw exact Nat.le_add_right _ _ ``` -/ scoped macro (name := transfer_rw) "transfer_rw" : tactic => `(tactic| (repeat first | rw [← to_nat_inj] | rw [← lt_to_nat] | rw [← le_to_nat] repeat first | rw [add_to_nat] | rw [mul_to_nat] | rw [cast_one] | rw [cast_zero])) /-- This tactic tries to prove (in)equalities about `Num`s by transferring them to the `Nat` world and then trying to call `simp`. ```lean example (n : Num) (m : Num) : n ≤ n + m := by transfer ``` -/ scoped macro (name := transfer) "transfer" : tactic => `(tactic| (intros; transfer_rw; try simp)) instance addMonoid : AddMonoid Num where add := (· + ·) zero := 0 zero_add := zero_add add_zero := add_zero add_assoc := by transfer nsmul := nsmulRec instance addMonoidWithOne : AddMonoidWithOne Num := { Num.addMonoid with natCast := Num.ofNat' one := 1 natCast_zero := ofNat'_zero natCast_succ := fun _ => ofNat'_succ } instance commSemiring : CommSemiring Num where __ := Num.addMonoid __ := Num.addMonoidWithOne mul := (· * ·) npow := @npowRec Num ⟨1⟩ ⟨(· * ·)⟩ mul_zero _ := by rw [← to_nat_inj, mul_to_nat, cast_zero, mul_zero] zero_mul _ := by rw [← to_nat_inj, mul_to_nat, cast_zero, zero_mul] mul_one _ := by rw [← to_nat_inj, mul_to_nat, cast_one, mul_one] one_mul _ := by rw [← to_nat_inj, mul_to_nat, cast_one, one_mul] add_comm _ _ := by simp_rw [← to_nat_inj, add_to_nat, add_comm] mul_comm _ _ := by simp_rw [← to_nat_inj, mul_to_nat, mul_comm] mul_assoc _ _ _ := by simp_rw [← to_nat_inj, mul_to_nat, mul_assoc] left_distrib _ _ _ := by simp only [← to_nat_inj, mul_to_nat, add_to_nat, mul_add] right_distrib _ _ _ := by simp only [← to_nat_inj, mul_to_nat, add_to_nat, add_mul] instance partialOrder : PartialOrder Num where lt_iff_le_not_le a b := by simp only [← lt_to_nat, ← le_to_nat, lt_iff_le_not_le] le_refl := by transfer le_trans a b c := by transfer_rw; apply le_trans le_antisymm a b := by transfer_rw; apply le_antisymm instance isOrderedCancelAddMonoid : IsOrderedCancelAddMonoid Num where add_le_add_left a b h c := by revert h; transfer_rw; exact fun h => add_le_add_left h c le_of_add_le_add_left a b c := show a + b ≤ a + c → b ≤ c by transfer_rw; apply le_of_add_le_add_left instance linearOrder : LinearOrder Num := { le_total := by intro a b transfer_rw apply le_total toDecidableLT := Num.decidableLT toDecidableLE := Num.decidableLE -- This is relying on an automatically generated instance name, -- generated in a `deriving` handler. -- See https://github.com/leanprover/lean4/issues/2343 toDecidableEq := instDecidableEqNum } instance isStrictOrderedRing : IsStrictOrderedRing Num := { zero_le_one := by decide mul_lt_mul_of_pos_left := by intro a b c transfer_rw apply mul_lt_mul_of_pos_left mul_lt_mul_of_pos_right := by intro a b c transfer_rw apply mul_lt_mul_of_pos_right exists_pair_ne := ⟨0, 1, by decide⟩ } @[norm_cast] theorem add_of_nat (m n) : ((m + n : ℕ) : Num) = m + n := add_ofNat' _ _ @[norm_cast] theorem to_nat_to_int (n : Num) : ((n : ℕ) : ℤ) = n := cast_to_nat _ @[simp, norm_cast] theorem cast_to_int {α} [AddGroupWithOne α] (n : Num) : ((n : ℤ) : α) = n := by rw [← to_nat_to_int, Int.cast_natCast, cast_to_nat] theorem to_of_nat : ∀ n : ℕ, ((n : Num) : ℕ) = n | 0 => by rw [Nat.cast_zero, cast_zero] | n + 1 => by rw [Nat.cast_succ, add_one, succ_to_nat, to_of_nat n] @[simp, norm_cast] theorem of_natCast {α} [AddMonoidWithOne α] (n : ℕ) : ((n : Num) : α) = n := by rw [← cast_to_nat, to_of_nat] @[norm_cast] theorem of_nat_inj {m n : ℕ} : (m : Num) = n ↔ m = n := ⟨fun h => Function.LeftInverse.injective to_of_nat h, congr_arg _⟩ -- The priority should be `high`er than `cast_to_nat`. @[simp high, norm_cast] theorem of_to_nat : ∀ n : Num, ((n : ℕ) : Num) = n := of_to_nat' @[norm_cast] theorem dvd_to_nat (m n : Num) : (m : ℕ) ∣ n ↔ m ∣ n := ⟨fun ⟨k, e⟩ => ⟨k, by rw [← of_to_nat n, e]; simp⟩, fun ⟨k, e⟩ => ⟨k, by simp [e, mul_to_nat]⟩⟩ end Num namespace PosNum variable {α : Type*} open Num -- The priority should be `high`er than `cast_to_nat`. @[simp high, norm_cast] theorem of_to_nat : ∀ n : PosNum, ((n : ℕ) : Num) = Num.pos n := of_to_nat' @[norm_cast] theorem to_nat_inj {m n : PosNum} : (m : ℕ) = n ↔ m = n := ⟨fun h => Num.pos.inj <| by rw [← PosNum.of_to_nat, ← PosNum.of_to_nat, h], congr_arg _⟩ theorem pred'_to_nat : ∀ n, (pred' n : ℕ) = Nat.pred n | 1 => rfl | bit0 n => have : Nat.succ ↑(pred' n) = ↑n := by rw [pred'_to_nat n, Nat.succ_pred_eq_of_pos (to_nat_pos n)] match (motive := ∀ k : Num, Nat.succ ↑k = ↑n → ↑(Num.casesOn k 1 bit1 : PosNum) = Nat.pred (n + n)) pred' n, this with | 0, (h : ((1 : Num) : ℕ) = n) => by rw [← to_nat_inj.1 h]; rfl | Num.pos p, (h : Nat.succ ↑p = n) => by rw [← h]; exact (Nat.succ_add p p).symm | bit1 _ => rfl @[simp] theorem pred'_succ' (n) : pred' (succ' n) = n := Num.to_nat_inj.1 <| by rw [pred'_to_nat, succ'_to_nat, Nat.add_one, Nat.pred_succ] @[simp] theorem succ'_pred' (n) : succ' (pred' n) = n := to_nat_inj.1 <| by rw [succ'_to_nat, pred'_to_nat, Nat.add_one, Nat.succ_pred_eq_of_pos (to_nat_pos _)] instance dvd : Dvd PosNum := ⟨fun m n => pos m ∣ pos n⟩ @[norm_cast] theorem dvd_to_nat {m n : PosNum} : (m : ℕ) ∣ n ↔ m ∣ n := Num.dvd_to_nat (pos m) (pos n) theorem size_to_nat : ∀ n, (size n : ℕ) = Nat.size n | 1 => Nat.size_one.symm | bit0 n => by rw [size, succ_to_nat, size_to_nat n, cast_bit0, ← two_mul] erw [@Nat.size_bit false n] have := to_nat_pos n dsimp [Nat.bit]; omega | bit1 n => by rw [size, succ_to_nat, size_to_nat n, cast_bit1, ← two_mul] erw [@Nat.size_bit true n] dsimp [Nat.bit]; omega theorem size_eq_natSize : ∀ n, (size n : ℕ) = natSize n | 1 => rfl | bit0 n => by rw [size, succ_to_nat, natSize, size_eq_natSize n] | bit1 n => by rw [size, succ_to_nat, natSize, size_eq_natSize n] theorem natSize_to_nat (n) : natSize n = Nat.size n := by rw [← size_eq_natSize, size_to_nat] theorem natSize_pos (n) : 0 < natSize n := by cases n <;> apply Nat.succ_pos /-- This tactic tries to turn an (in)equality about `PosNum`s to one about `Nat`s by rewriting. ```lean example (n : PosNum) (m : PosNum) : n ≤ n + m := by transfer_rw exact Nat.le_add_right _ _ ``` -/ scoped macro (name := transfer_rw) "transfer_rw" : tactic => `(tactic| (repeat first | rw [← to_nat_inj] | rw [← lt_to_nat] | rw [← le_to_nat] repeat first | rw [add_to_nat] | rw [mul_to_nat] | rw [cast_one] | rw [cast_zero])) /-- This tactic tries to prove (in)equalities about `PosNum`s by transferring them to the `Nat` world and then trying to call `simp`. ```lean example (n : PosNum) (m : PosNum) : n ≤ n + m := by transfer ``` -/ scoped macro (name := transfer) "transfer" : tactic => `(tactic| (intros; transfer_rw; try simp [add_comm, add_left_comm, mul_comm, mul_left_comm])) instance addCommSemigroup : AddCommSemigroup PosNum where add := (· + ·) add_assoc := by transfer add_comm := by transfer instance commMonoid : CommMonoid PosNum where mul := (· * ·) one := (1 : PosNum) npow := @npowRec PosNum ⟨1⟩ ⟨(· * ·)⟩ mul_assoc := by transfer one_mul := by transfer mul_one := by transfer mul_comm := by transfer instance distrib : Distrib PosNum where add := (· + ·) mul := (· * ·) left_distrib := by transfer; simp [mul_add] right_distrib := by transfer; simp [mul_add, mul_comm] instance linearOrder : LinearOrder PosNum where lt := (· < ·) lt_iff_le_not_le := by intro a b transfer_rw apply lt_iff_le_not_le le := (· ≤ ·) le_refl := by transfer le_trans := by intro a b c transfer_rw apply le_trans le_antisymm := by intro a b transfer_rw apply le_antisymm le_total := by intro a b transfer_rw apply le_total toDecidableLT := by infer_instance toDecidableLE := by infer_instance toDecidableEq := by infer_instance @[simp] theorem cast_to_num (n : PosNum) : ↑n = Num.pos n := by rw [← cast_to_nat, ← of_to_nat n] @[simp, norm_cast] theorem bit_to_nat (b n) : (bit b n : ℕ) = Nat.bit b n := by cases b <;> simp [bit, two_mul] @[simp, norm_cast] theorem cast_add [AddMonoidWithOne α] (m n) : ((m + n : PosNum) : α) = m + n := by rw [← cast_to_nat, add_to_nat, Nat.cast_add, cast_to_nat, cast_to_nat] @[simp 500, norm_cast] theorem cast_succ [AddMonoidWithOne α] (n : PosNum) : (succ n : α) = n + 1 := by rw [← add_one, cast_add, cast_one] @[simp, norm_cast] theorem cast_inj [AddMonoidWithOne α] [CharZero α] {m n : PosNum} : (m : α) = n ↔ m = n := by rw [← cast_to_nat m, ← cast_to_nat n, Nat.cast_inj, to_nat_inj] @[simp] theorem one_le_cast [Semiring α] [PartialOrder α] [IsStrictOrderedRing α] (n : PosNum) : (1 : α) ≤ n := by rw [← cast_to_nat, ← Nat.cast_one, Nat.cast_le (α := α)]; apply to_nat_pos @[simp] theorem cast_pos [Semiring α] [PartialOrder α] [IsStrictOrderedRing α] (n : PosNum) : 0 < (n : α) := lt_of_lt_of_le zero_lt_one (one_le_cast n) @[simp, norm_cast] theorem cast_mul [NonAssocSemiring α] (m n) : ((m * n : PosNum) : α) = m * n := by rw [← cast_to_nat, mul_to_nat, Nat.cast_mul, cast_to_nat, cast_to_nat] @[simp] theorem cmp_eq (m n) : cmp m n = Ordering.eq ↔ m = n := by have := cmp_to_nat m n -- Porting note: `cases` didn't rewrite at `this`, so `revert` & `intro` are required. revert this; cases cmp m n <;> intro this <;> simp at this ⊢ <;> try { exact this } <;> simp [show m ≠ n from fun e => by rw [e] at this;exact lt_irrefl _ this] @[simp, norm_cast] theorem cast_lt [Semiring α] [PartialOrder α] [IsStrictOrderedRing α] {m n : PosNum} : (m : α) < n ↔ m < n := by rw [← cast_to_nat m, ← cast_to_nat n, Nat.cast_lt (α := α), lt_to_nat] @[simp, norm_cast] theorem cast_le [Semiring α] [LinearOrder α] [IsStrictOrderedRing α] {m n : PosNum} : (m : α) ≤ n ↔ m ≤ n := by rw [← not_lt]; exact not_congr cast_lt end PosNum namespace Num variable {α : Type*} open PosNum theorem bit_to_nat (b n) : (bit b n : ℕ) = Nat.bit b n := by cases b <;> cases n <;> simp [bit, two_mul] <;> rfl theorem cast_succ' [AddMonoidWithOne α] (n) : (succ' n : α) = n + 1 := by rw [← PosNum.cast_to_nat, succ'_to_nat, Nat.cast_add_one, cast_to_nat] theorem cast_succ [AddMonoidWithOne α] (n) : (succ n : α) = n + 1 := cast_succ' n @[simp, norm_cast] theorem cast_add [AddMonoidWithOne α] (m n) : ((m + n : Num) : α) = m + n := by rw [← cast_to_nat, add_to_nat, Nat.cast_add, cast_to_nat, cast_to_nat] @[simp, norm_cast] theorem cast_bit0 [NonAssocSemiring α] (n : Num) : (n.bit0 : α) = 2 * (n : α) := by rw [← bit0_of_bit0, two_mul, cast_add] @[simp, norm_cast] theorem cast_bit1 [NonAssocSemiring α] (n : Num) : (n.bit1 : α) = 2 * (n : α) + 1 := by rw [← bit1_of_bit1, bit0_of_bit0, cast_add, cast_bit0]; rfl @[simp, norm_cast] theorem cast_mul [NonAssocSemiring α] : ∀ m n, ((m * n : Num) : α) = m * n | 0, 0 => (zero_mul _).symm | 0, pos _q => (zero_mul _).symm | pos _p, 0 => (mul_zero _).symm | pos _p, pos _q => PosNum.cast_mul _ _ theorem size_to_nat : ∀ n, (size n : ℕ) = Nat.size n | 0 => Nat.size_zero.symm | pos p => p.size_to_nat theorem size_eq_natSize : ∀ n, (size n : ℕ) = natSize n | 0 => rfl | pos p => p.size_eq_natSize theorem natSize_to_nat (n) : natSize n = Nat.size n := by rw [← size_eq_natSize, size_to_nat] @[simp 999] theorem ofNat'_eq : ∀ n, Num.ofNat' n = n := Nat.binaryRec (by simp) fun b n IH => by tauto theorem zneg_toZNum (n : Num) : -n.toZNum = n.toZNumNeg := by cases n <;> rfl theorem zneg_toZNumNeg (n : Num) : -n.toZNumNeg = n.toZNum := by cases n <;> rfl theorem toZNum_inj {m n : Num} : m.toZNum = n.toZNum ↔ m = n := ⟨fun h => by cases m <;> cases n <;> cases h <;> rfl, congr_arg _⟩ @[simp] theorem cast_toZNum [Zero α] [One α] [Add α] [Neg α] : ∀ n : Num, (n.toZNum : α) = n | 0 => rfl | Num.pos _p => rfl @[simp] theorem cast_toZNumNeg [SubtractionMonoid α] [One α] : ∀ n : Num, (n.toZNumNeg : α) = -n | 0 => neg_zero.symm | Num.pos _p => rfl @[simp] theorem add_toZNum (m n : Num) : Num.toZNum (m + n) = m.toZNum + n.toZNum := by cases m <;> cases n <;> rfl end Num namespace PosNum open Num theorem pred_to_nat {n : PosNum} (h : 1 < n) : (pred n : ℕ) = Nat.pred n := by unfold pred cases e : pred' n · have : (1 : ℕ) ≤ Nat.pred n := Nat.pred_le_pred ((@cast_lt ℕ _ _ _).2 h) rw [← pred'_to_nat, e] at this exact absurd this (by decide) · rw [← pred'_to_nat, e] rfl theorem sub'_one (a : PosNum) : sub' a 1 = (pred' a).toZNum := by cases a <;> rfl theorem one_sub' (a : PosNum) : sub' 1 a = (pred' a).toZNumNeg := by cases a <;> rfl theorem lt_iff_cmp {m n} : m < n ↔ cmp m n = Ordering.lt := Iff.rfl theorem le_iff_cmp {m n} : m ≤ n ↔ cmp m n ≠ Ordering.gt := not_congr <| lt_iff_cmp.trans <| by rw [← cmp_swap]; cases cmp m n <;> decide end PosNum namespace Num variable {α : Type*} open PosNum theorem pred_to_nat : ∀ n : Num, (pred n : ℕ) = Nat.pred n | 0 => rfl | pos p => by rw [pred, PosNum.pred'_to_nat]; rfl theorem ppred_to_nat : ∀ n : Num, (↑) <$> ppred n = Nat.ppred n | 0 => rfl | pos p => by rw [ppred, Option.map_some, Nat.ppred_eq_some.2] rw [PosNum.pred'_to_nat, Nat.succ_pred_eq_of_pos (PosNum.to_nat_pos _)] rfl theorem cmp_swap (m n) : (cmp m n).swap = cmp n m := by cases m <;> cases n <;> try { rfl }; apply PosNum.cmp_swap theorem cmp_eq (m n) : cmp m n = Ordering.eq ↔ m = n := by have := cmp_to_nat m n -- Porting note: `cases` didn't rewrite at `this`, so `revert` & `intro` are required. revert this; cases cmp m n <;> intro this <;> simp at this ⊢ <;> try { exact this } <;> simp [show m ≠ n from fun e => by rw [e] at this; exact lt_irrefl _ this] @[simp, norm_cast] theorem cast_lt [Semiring α] [PartialOrder α] [IsStrictOrderedRing α] {m n : Num} : (m : α) < n ↔ m < n := by rw [← cast_to_nat m, ← cast_to_nat n, Nat.cast_lt (α := α), lt_to_nat] @[simp, norm_cast] theorem cast_le [Semiring α] [LinearOrder α] [IsStrictOrderedRing α] {m n : Num} : (m : α) ≤ n ↔ m ≤ n := by rw [← not_lt]; exact not_congr cast_lt @[simp, norm_cast] theorem cast_inj [Semiring α] [PartialOrder α] [IsStrictOrderedRing α] {m n : Num} : (m : α) = n ↔ m = n := by rw [← cast_to_nat m, ← cast_to_nat n, Nat.cast_inj, to_nat_inj] theorem lt_iff_cmp {m n} : m < n ↔ cmp m n = Ordering.lt := Iff.rfl theorem le_iff_cmp {m n} : m ≤ n ↔ cmp m n ≠ Ordering.gt := not_congr <| lt_iff_cmp.trans <| by rw [← cmp_swap]; cases cmp m n <;> decide theorem castNum_eq_bitwise {f : Num → Num → Num} {g : Bool → Bool → Bool} (p : PosNum → PosNum → Num) (gff : g false false = false) (f00 : f 0 0 = 0) (f0n : ∀ n, f 0 (pos n) = cond (g false true) (pos n) 0) (fn0 : ∀ n, f (pos n) 0 = cond (g true false) (pos n) 0) (fnn : ∀ m n, f (pos m) (pos n) = p m n) (p11 : p 1 1 = cond (g true true) 1 0) (p1b : ∀ b n, p 1 (PosNum.bit b n) = bit (g true b) (cond (g false true) (pos n) 0)) (pb1 : ∀ a m, p (PosNum.bit a m) 1 = bit (g a true) (cond (g true false) (pos m) 0)) (pbb : ∀ a b m n, p (PosNum.bit a m) (PosNum.bit b n) = bit (g a b) (p m n)) : ∀ m n : Num, (f m n : ℕ) = Nat.bitwise g m n := by intros m n obtain - | m := m <;> obtain - | n := n <;> try simp only [show zero = 0 from rfl, show ((0 : Num) : ℕ) = 0 from rfl] · rw [f00, Nat.bitwise_zero]; rfl · rw [f0n, Nat.bitwise_zero_left] cases g false true <;> rfl · rw [fn0, Nat.bitwise_zero_right] cases g true false <;> rfl · rw [fnn] have this b (n : PosNum) : (cond b (↑n) 0 : ℕ) = ↑(cond b (pos n) 0 : Num) := by cases b <;> rfl have this' b (n : PosNum) : ↑ (pos (PosNum.bit b n)) = Nat.bit b ↑n := by cases b <;> simp induction' m with m IH m IH generalizing n <;> obtain - | n | n := n any_goals simp only [show one = 1 from rfl, show pos 1 = 1 from rfl, show PosNum.bit0 = PosNum.bit false from rfl, show PosNum.bit1 = PosNum.bit true from rfl, show ((1 : Num) : ℕ) = Nat.bit true 0 from rfl] all_goals repeat rw [this'] rw [Nat.bitwise_bit gff] any_goals rw [Nat.bitwise_zero, p11]; cases g true true <;> rfl any_goals rw [Nat.bitwise_zero_left, ← Bool.cond_eq_ite, this, ← bit_to_nat, p1b] any_goals rw [Nat.bitwise_zero_right, ← Bool.cond_eq_ite, this, ← bit_to_nat, pb1] all_goals rw [← show ∀ n : PosNum, ↑(p m n) = Nat.bitwise g ↑m ↑n from IH] rw [← bit_to_nat, pbb] @[simp, norm_cast] theorem castNum_or : ∀ m n : Num, ↑(m ||| n) = (↑m ||| ↑n : ℕ) := by apply castNum_eq_bitwise fun x y => pos (PosNum.lor x y) <;> (try rintro (_ | _)) <;> (try rintro (_ | _)) <;> intros <;> rfl @[simp, norm_cast] theorem castNum_and : ∀ m n : Num, ↑(m &&& n) = (↑m &&& ↑n : ℕ) := by apply castNum_eq_bitwise PosNum.land <;> intros <;> (try cases_type* Bool) <;> rfl @[simp, norm_cast] theorem castNum_ldiff : ∀ m n : Num, (ldiff m n : ℕ) = Nat.ldiff m n := by apply castNum_eq_bitwise PosNum.ldiff <;> intros <;> (try cases_type* Bool) <;> rfl @[simp, norm_cast] theorem castNum_xor : ∀ m n : Num, ↑(m ^^^ n) = (↑m ^^^ ↑n : ℕ) := by apply castNum_eq_bitwise PosNum.lxor <;> intros <;> (try cases_type* Bool) <;> rfl @[simp, norm_cast] theorem castNum_shiftLeft (m : Num) (n : Nat) : ↑(m <<< n) = (m : ℕ) <<< (n : ℕ) := by cases m <;> dsimp only [← shiftl_eq_shiftLeft, shiftl] · symm apply Nat.zero_shiftLeft simp only [cast_pos] induction' n with n IH · rfl
simp [PosNum.shiftl_succ_eq_bit0_shiftl, Nat.shiftLeft_succ, IH, pow_succ, ← mul_assoc, mul_comm, -shiftl_eq_shiftLeft, -PosNum.shiftl_eq_shiftLeft, shiftl, mul_two]
Mathlib/Data/Num/Lemmas.lean
822
823
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Ken Lee, Chris Hughes -/ import Mathlib.Algebra.Group.Action.Units import Mathlib.Algebra.Group.Nat.Units import Mathlib.Algebra.GroupWithZero.Divisibility import Mathlib.Algebra.Ring.Divisibility.Basic import Mathlib.Algebra.Ring.Hom.Defs import Mathlib.Logic.Basic import Mathlib.Tactic.Ring /-! # Coprime elements of a ring or monoid ## Main definition * `IsCoprime x y`: that `x` and `y` are coprime, defined to be the existence of `a` and `b` such that `a * x + b * y = 1`. Note that elements with no common divisors (`IsRelPrime`) are not necessarily coprime, e.g., the multivariate polynomials `x₁` and `x₂` are not coprime. The two notions are equivalent in Bézout rings, see `isRelPrime_iff_isCoprime`. This file also contains lemmas about `IsRelPrime` parallel to `IsCoprime`. See also `RingTheory.Coprime.Lemmas` for further development of coprime elements. -/ universe u v section CommSemiring variable {R : Type u} [CommSemiring R] (x y z : R) /-- The proposition that `x` and `y` are coprime, defined to be the existence of `a` and `b` such that `a * x + b * y = 1`. Note that elements with no common divisors are not necessarily coprime, e.g., the multivariate polynomials `x₁` and `x₂` are not coprime. -/ def IsCoprime : Prop := ∃ a b, a * x + b * y = 1 variable {x y z} @[symm] theorem IsCoprime.symm (H : IsCoprime x y) : IsCoprime y x := let ⟨a, b, H⟩ := H ⟨b, a, by rw [add_comm, H]⟩ theorem isCoprime_comm : IsCoprime x y ↔ IsCoprime y x := ⟨IsCoprime.symm, IsCoprime.symm⟩ theorem isCoprime_self : IsCoprime x x ↔ IsUnit x := ⟨fun ⟨a, b, h⟩ => isUnit_of_mul_eq_one x (a + b) <| by rwa [mul_comm, add_mul], fun h => let ⟨b, hb⟩ := isUnit_iff_exists_inv'.1 h ⟨b, 0, by rwa [zero_mul, add_zero]⟩⟩
theorem isCoprime_zero_left : IsCoprime 0 x ↔ IsUnit x := ⟨fun ⟨a, b, H⟩ => isUnit_of_mul_eq_one x b <| by rwa [mul_zero, zero_add, mul_comm] at H, fun H => let ⟨b, hb⟩ := isUnit_iff_exists_inv'.1 H
Mathlib/RingTheory/Coprime/Basic.lean
56
59
/- Copyright (c) 2019 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Kevin Kappelmann -/ import Mathlib.Algebra.Order.Round import Mathlib.Data.Rat.Cast.Order import Mathlib.Tactic.FieldSimp import Mathlib.Tactic.Ring /-! # Floor Function for Rational Numbers ## Summary We define the `FloorRing` instance on `ℚ`. Some technical lemmas relating `floor` to integer division and modulo arithmetic are derived as well as some simple inequalities. ## Tags rat, rationals, ℚ, floor -/ assert_not_exists Finset open Int namespace Rat variable {α : Type*} [Field α] [LinearOrder α] [IsStrictOrderedRing α] [FloorRing α] protected theorem floor_def' (a : ℚ) : a.floor = a.num / a.den := by rw [Rat.floor] split · next h => simp [h] · next => rfl protected theorem le_floor {z : ℤ} : ∀ {r : ℚ}, z ≤ Rat.floor r ↔ (z : ℚ) ≤ r
| ⟨n, d, h, c⟩ => by simp only [Rat.floor_def'] rw [mk'_eq_divInt] have h' := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h) conv => rhs rw [intCast_eq_divInt, Rat.divInt_le_divInt zero_lt_one h', mul_one] exact Int.le_ediv_iff_mul_le h'
Mathlib/Data/Rat/Floor.lean
39
47
/- Copyright (c) 2020 Nicolò Cavalleri. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Nicolò Cavalleri, Yury Kudryashov -/ import Mathlib.Geometry.Manifold.ContMDiffMap import Mathlib.Geometry.Manifold.MFDeriv.UniqueDifferential /-! # Diffeomorphisms This file implements diffeomorphisms. ## Definitions * `Diffeomorph I I' M M' n`: `n`-times continuously differentiable diffeomorphism between `M` and `M'` with respect to I and I'; we do not introduce a separate definition for the case `n = ∞`; we use notation instead. * `Diffeomorph.toHomeomorph`: reinterpret a diffeomorphism as a homeomorphism. * `ContinuousLinearEquiv.toDiffeomorph`: reinterpret a continuous equivalence as a diffeomorphism. * `ModelWithCorners.transDiffeomorph`: compose a given `ModelWithCorners` with a diffeomorphism between the old and the new target spaces. Useful, e.g, to turn any finite dimensional manifold into a manifold modelled on a Euclidean space. * `Diffeomorph.toTransDiffeomorph`: the identity diffeomorphism between `M` with model `I` and `M` with model `I.trans_diffeomorph e`. This file also provides diffeomorphisms related to products and disjoint unions. * `Diffeomorph.prodCongr`: the product of two diffeomorphisms * `Diffeomorph.prodComm`: `M × N` is diffeomorphic to `N × M` * `Diffeomorph.prodAssoc`: `(M × N) × N'` is diffeomorphic to `M × (N × N')` * `Diffeomorph.sumCongr`: the disjoint union of two diffeomorphisms * `Diffeomorph.sumComm`: `M ⊕ M'` is diffeomorphic to `M' × M` * `Diffeomorph.sumAssoc`: `(M ⊕ N) ⊕ P` is diffeomorphic to `M ⊕ (N ⊕ P)` * `Diffeomorph.sumEmpty`: `M ⊕ ∅` is diffeomorphic to `M` ## Notations * `M ≃ₘ^n⟮I, I'⟯ M'` := `Diffeomorph I J M N n` * `M ≃ₘ⟮I, I'⟯ M'` := `Diffeomorph I J M N ∞` * `E ≃ₘ^n[𝕜] E'` := `E ≃ₘ^n⟮𝓘(𝕜, E), 𝓘(𝕜, E')⟯ E'` * `E ≃ₘ[𝕜] E'` := `E ≃ₘ⟮𝓘(𝕜, E), 𝓘(𝕜, E')⟯ E'` ## Implementation notes This notion of diffeomorphism is needed although there is already a notion of structomorphism because structomorphisms do not allow the model spaces `H` and `H'` of the two manifolds to be different, i.e. for a structomorphism one has to impose `H = H'` which is often not the case in practice. ## Keywords diffeomorphism, manifold -/ open scoped Manifold Topology ContDiff open Function Set variable {𝕜 : Type*} [NontriviallyNormedField 𝕜] {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {E' : Type*} [NormedAddCommGroup E'] [NormedSpace 𝕜 E'] {F : Type*} [NormedAddCommGroup F] [NormedSpace 𝕜 F] {H : Type*} [TopologicalSpace H] {H' : Type*} [TopologicalSpace H'] {G : Type*} [TopologicalSpace G] {G' : Type*} [TopologicalSpace G'] {I : ModelWithCorners 𝕜 E H} {I' : ModelWithCorners 𝕜 E' H'} {J : ModelWithCorners 𝕜 F G} {J' : ModelWithCorners 𝕜 F G'} variable {M : Type*} [TopologicalSpace M] [ChartedSpace H M] {M' : Type*} [TopologicalSpace M'] [ChartedSpace H' M'] {N : Type*} [TopologicalSpace N] [ChartedSpace G N] {N' : Type*} [TopologicalSpace N'] [ChartedSpace G' N'] {n : WithTop ℕ∞} section Defs variable (I I' M M' n) /-- `n`-times continuously differentiable diffeomorphism between `M` and `M'` with respect to `I` and `I'`, denoted as `M ≃ₘ^n⟮I, I'⟯ M'` (in the `Manifold` namespace). -/ structure Diffeomorph extends M ≃ M' where protected contMDiff_toFun : ContMDiff I I' n toEquiv protected contMDiff_invFun : ContMDiff I' I n toEquiv.symm end Defs @[inherit_doc] scoped[Manifold] notation M " ≃ₘ^" n:1000 "⟮" I ", " J "⟯ " N => Diffeomorph I J M N n /-- Infinitely differentiable diffeomorphism between `M` and `M'` with respect to `I` and `I'`. -/ scoped[Manifold] notation M " ≃ₘ⟮" I ", " J "⟯ " N => Diffeomorph I J M N ∞ /-- `n`-times continuously differentiable diffeomorphism between `E` and `E'`. -/ scoped[Manifold] notation E " ≃ₘ^" n:1000 "[" 𝕜 "] " E' => Diffeomorph 𝓘(𝕜, E) 𝓘(𝕜, E') E E' n /-- Infinitely differentiable diffeomorphism between `E` and `E'`. -/ scoped[Manifold] notation3 E " ≃ₘ[" 𝕜 "] " E' => Diffeomorph 𝓘(𝕜, E) 𝓘(𝕜, E') E E' ∞ namespace Diffeomorph theorem toEquiv_injective : Injective (Diffeomorph.toEquiv : (M ≃ₘ^n⟮I, I'⟯ M') → M ≃ M') | ⟨_, _, _⟩, ⟨_, _, _⟩, rfl => rfl instance : EquivLike (M ≃ₘ^n⟮I, I'⟯ M') M M' where coe Φ := Φ.toEquiv inv Φ := Φ.toEquiv.symm left_inv Φ := Φ.left_inv right_inv Φ := Φ.right_inv coe_injective' _ _ h _ := toEquiv_injective <| DFunLike.ext' h /-- Interpret a diffeomorphism as a `ContMDiffMap`. -/ @[coe] def toContMDiffMap (Φ : M ≃ₘ^n⟮I, I'⟯ M') : C^n⟮I, M; I', M'⟯ := ⟨Φ, Φ.contMDiff_toFun⟩ instance : Coe (M ≃ₘ^n⟮I, I'⟯ M') C^n⟮I, M; I', M'⟯ := ⟨toContMDiffMap⟩ @[continuity] protected theorem continuous (h : M ≃ₘ^n⟮I, I'⟯ M') : Continuous h := h.contMDiff_toFun.continuous protected theorem contMDiff (h : M ≃ₘ^n⟮I, I'⟯ M') : ContMDiff I I' n h := h.contMDiff_toFun protected theorem contMDiffAt (h : M ≃ₘ^n⟮I, I'⟯ M') {x} : ContMDiffAt I I' n h x := h.contMDiff.contMDiffAt protected theorem contMDiffWithinAt (h : M ≃ₘ^n⟮I, I'⟯ M') {s x} : ContMDiffWithinAt I I' n h s x := h.contMDiffAt.contMDiffWithinAt -- Porting note (https://github.com/leanprover-community/mathlib4/issues/11215): TODO: should use `E ≃ₘ^n[𝕜] F` notation protected theorem contDiff (h : E ≃ₘ^n⟮𝓘(𝕜, E), 𝓘(𝕜, E')⟯ E') : ContDiff 𝕜 n h := h.contMDiff.contDiff @[deprecated (since := "2024-11-21")] alias smooth := Diffeomorph.contDiff protected theorem mdifferentiable (h : M ≃ₘ^n⟮I, I'⟯ M') (hn : 1 ≤ n) : MDifferentiable I I' h := h.contMDiff.mdifferentiable hn protected theorem mdifferentiableOn (h : M ≃ₘ^n⟮I, I'⟯ M') (s : Set M) (hn : 1 ≤ n) : MDifferentiableOn I I' h s := (h.mdifferentiable hn).mdifferentiableOn @[simp] theorem coe_toEquiv (h : M ≃ₘ^n⟮I, I'⟯ M') : ⇑h.toEquiv = h := rfl @[simp, norm_cast] theorem coe_coe (h : M ≃ₘ^n⟮I, I'⟯ M') : ⇑(h : C^n⟮I, M; I', M'⟯) = h := rfl @[simp] theorem toEquiv_inj {h h' : M ≃ₘ^n⟮I, I'⟯ M'} : h.toEquiv = h'.toEquiv ↔ h = h' := toEquiv_injective.eq_iff /-- Coercion to function `fun h : M ≃ₘ^n⟮I, I'⟯ M' ↦ (h : M → M')` is injective. -/ theorem coeFn_injective : Injective ((↑) : (M ≃ₘ^n⟮I, I'⟯ M') → (M → M')) := DFunLike.coe_injective @[ext] theorem ext {h h' : M ≃ₘ^n⟮I, I'⟯ M'} (Heq : ∀ x, h x = h' x) : h = h' := coeFn_injective <| funext Heq instance : ContinuousMapClass (M ≃ₘ⟮I, J⟯ N) M N where map_continuous f := f.continuous section variable (M I n) /-- Identity map as a diffeomorphism. -/ protected def refl : M ≃ₘ^n⟮I, I⟯ M where contMDiff_toFun := contMDiff_id contMDiff_invFun := contMDiff_id toEquiv := Equiv.refl M @[simp] theorem refl_toEquiv : (Diffeomorph.refl I M n).toEquiv = Equiv.refl _ := rfl @[simp] theorem coe_refl : ⇑(Diffeomorph.refl I M n) = id := rfl end /-- Composition of two diffeomorphisms. -/ @[trans] protected def trans (h₁ : M ≃ₘ^n⟮I, I'⟯ M') (h₂ : M' ≃ₘ^n⟮I', J⟯ N) : M ≃ₘ^n⟮I, J⟯ N where contMDiff_toFun := h₂.contMDiff.comp h₁.contMDiff contMDiff_invFun := h₁.contMDiff_invFun.comp h₂.contMDiff_invFun toEquiv := h₁.toEquiv.trans h₂.toEquiv @[simp] theorem trans_refl (h : M ≃ₘ^n⟮I, I'⟯ M') : h.trans (Diffeomorph.refl I' M' n) = h := ext fun _ => rfl @[simp] theorem refl_trans (h : M ≃ₘ^n⟮I, I'⟯ M') : (Diffeomorph.refl I M n).trans h = h := ext fun _ => rfl @[simp] theorem coe_trans (h₁ : M ≃ₘ^n⟮I, I'⟯ M') (h₂ : M' ≃ₘ^n⟮I', J⟯ N) : ⇑(h₁.trans h₂) = h₂ ∘ h₁ := rfl /-- Inverse of a diffeomorphism. -/ @[symm] protected def symm (h : M ≃ₘ^n⟮I, J⟯ N) : N ≃ₘ^n⟮J, I⟯ M where contMDiff_toFun := h.contMDiff_invFun contMDiff_invFun := h.contMDiff_toFun toEquiv := h.toEquiv.symm @[simp] theorem apply_symm_apply (h : M ≃ₘ^n⟮I, J⟯ N) (x : N) : h (h.symm x) = x := h.toEquiv.apply_symm_apply x @[simp] theorem symm_apply_apply (h : M ≃ₘ^n⟮I, J⟯ N) (x : M) : h.symm (h x) = x := h.toEquiv.symm_apply_apply x @[simp] theorem symm_refl : (Diffeomorph.refl I M n).symm = Diffeomorph.refl I M n := ext fun _ => rfl @[simp] theorem self_trans_symm (h : M ≃ₘ^n⟮I, J⟯ N) : h.trans h.symm = Diffeomorph.refl I M n := ext h.symm_apply_apply @[simp] theorem symm_trans_self (h : M ≃ₘ^n⟮I, J⟯ N) : h.symm.trans h = Diffeomorph.refl J N n := ext h.apply_symm_apply @[simp] theorem symm_trans' (h₁ : M ≃ₘ^n⟮I, I'⟯ M') (h₂ : M' ≃ₘ^n⟮I', J⟯ N) : (h₁.trans h₂).symm = h₂.symm.trans h₁.symm := rfl @[simp] theorem symm_toEquiv (h : M ≃ₘ^n⟮I, J⟯ N) : h.symm.toEquiv = h.toEquiv.symm := rfl @[simp, mfld_simps] theorem toEquiv_coe_symm (h : M ≃ₘ^n⟮I, J⟯ N) : ⇑h.toEquiv.symm = h.symm := rfl theorem image_eq_preimage (h : M ≃ₘ^n⟮I, J⟯ N) (s : Set M) : h '' s = h.symm ⁻¹' s := h.toEquiv.image_eq_preimage s theorem symm_image_eq_preimage (h : M ≃ₘ^n⟮I, J⟯ N) (s : Set N) : h.symm '' s = h ⁻¹' s := h.symm.image_eq_preimage s @[simp, mfld_simps] nonrec theorem range_comp {α} (h : M ≃ₘ^n⟮I, J⟯ N) (f : α → M) : range (h ∘ f) = h.symm ⁻¹' range f := by rw [range_comp, image_eq_preimage] @[simp] theorem image_symm_image (h : M ≃ₘ^n⟮I, J⟯ N) (s : Set N) : h '' (h.symm '' s) = s := h.toEquiv.image_symm_image s @[simp] theorem symm_image_image (h : M ≃ₘ^n⟮I, J⟯ N) (s : Set M) : h.symm '' (h '' s) = s := h.toEquiv.symm_image_image s /-- A diffeomorphism is a homeomorphism. -/ def toHomeomorph (h : M ≃ₘ^n⟮I, J⟯ N) : M ≃ₜ N := ⟨h.toEquiv, h.continuous, h.symm.continuous⟩ @[simp] theorem toHomeomorph_toEquiv (h : M ≃ₘ^n⟮I, J⟯ N) : h.toHomeomorph.toEquiv = h.toEquiv := rfl @[simp] theorem symm_toHomeomorph (h : M ≃ₘ^n⟮I, J⟯ N) : h.symm.toHomeomorph = h.toHomeomorph.symm := rfl @[simp] theorem coe_toHomeomorph (h : M ≃ₘ^n⟮I, J⟯ N) : ⇑h.toHomeomorph = h := rfl @[simp] theorem coe_toHomeomorph_symm (h : M ≃ₘ^n⟮I, J⟯ N) : ⇑h.toHomeomorph.symm = h.symm := rfl
@[simp] theorem contMDiffWithinAt_comp_diffeomorph_iff {m} (h : M ≃ₘ^n⟮I, J⟯ N) {f : N → M'} {s x}
Mathlib/Geometry/Manifold/Diffeomorph.lean
281
283
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Abhimanyu Pallavi Sudhir, Jean Lo, Calle Sönne, Sébastien Gouëzel, Rémy Degenne, David Loeffler -/ import Mathlib.Analysis.SpecialFunctions.Pow.Complex import Qq /-! # Power function on `ℝ` We construct the power functions `x ^ y`, where `x` and `y` are real numbers. -/ noncomputable section open Real ComplexConjugate Finset Set /- ## Definitions -/ namespace Real variable {x y z : ℝ} /-- The real power function `x ^ y`, defined as the real part of the complex power function. For `x > 0`, it is equal to `exp (y log x)`. For `x = 0`, one sets `0 ^ 0=1` and `0 ^ y=0` for `y ≠ 0`. For `x < 0`, the definition is somewhat arbitrary as it depends on the choice of a complex determination of the logarithm. With our conventions, it is equal to `exp (y log x) cos (π y)`. -/ noncomputable def rpow (x y : ℝ) := ((x : ℂ) ^ (y : ℂ)).re noncomputable instance : Pow ℝ ℝ := ⟨rpow⟩ @[simp] theorem rpow_eq_pow (x y : ℝ) : rpow x y = x ^ y := rfl theorem rpow_def (x y : ℝ) : x ^ y = ((x : ℂ) ^ (y : ℂ)).re := rfl theorem rpow_def_of_nonneg {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : x ^ y = if x = 0 then if y = 0 then 1 else 0 else exp (log x * y) := by simp only [rpow_def, Complex.cpow_def]; split_ifs <;> simp_all [(Complex.ofReal_log hx).symm, -Complex.ofReal_mul, (Complex.ofReal_mul _ _).symm, Complex.exp_ofReal_re, Complex.ofReal_eq_zero] theorem rpow_def_of_pos {x : ℝ} (hx : 0 < x) (y : ℝ) : x ^ y = exp (log x * y) := by rw [rpow_def_of_nonneg (le_of_lt hx), if_neg (ne_of_gt hx)] theorem exp_mul (x y : ℝ) : exp (x * y) = exp x ^ y := by rw [rpow_def_of_pos (exp_pos _), log_exp] @[simp, norm_cast] theorem rpow_intCast (x : ℝ) (n : ℤ) : x ^ (n : ℝ) = x ^ n := by simp only [rpow_def, ← Complex.ofReal_zpow, Complex.cpow_intCast, Complex.ofReal_intCast, Complex.ofReal_re] @[simp, norm_cast] theorem rpow_natCast (x : ℝ) (n : ℕ) : x ^ (n : ℝ) = x ^ n := by simpa using rpow_intCast x n @[simp] theorem exp_one_rpow (x : ℝ) : exp 1 ^ x = exp x := by rw [← exp_mul, one_mul] @[simp] lemma exp_one_pow (n : ℕ) : exp 1 ^ n = exp n := by rw [← rpow_natCast, exp_one_rpow] theorem rpow_eq_zero_iff_of_nonneg (hx : 0 ≤ x) : x ^ y = 0 ↔ x = 0 ∧ y ≠ 0 := by simp only [rpow_def_of_nonneg hx] split_ifs <;> simp [*, exp_ne_zero] @[simp] lemma rpow_eq_zero (hx : 0 ≤ x) (hy : y ≠ 0) : x ^ y = 0 ↔ x = 0 := by simp [rpow_eq_zero_iff_of_nonneg, *] @[simp] lemma rpow_ne_zero (hx : 0 ≤ x) (hy : y ≠ 0) : x ^ y ≠ 0 ↔ x ≠ 0 := Real.rpow_eq_zero hx hy |>.not open Real theorem rpow_def_of_neg {x : ℝ} (hx : x < 0) (y : ℝ) : x ^ y = exp (log x * y) * cos (y * π) := by rw [rpow_def, Complex.cpow_def, if_neg] · have : Complex.log x * y = ↑(log (-x) * y) + ↑(y * π) * Complex.I := by simp only [Complex.log, Complex.norm_real, norm_eq_abs, abs_of_neg hx, log_neg_eq_log, Complex.arg_ofReal_of_neg hx, Complex.ofReal_mul] ring rw [this, Complex.exp_add_mul_I, ← Complex.ofReal_exp, ← Complex.ofReal_cos, ←
Complex.ofReal_sin, mul_add, ← Complex.ofReal_mul, ← mul_assoc, ← Complex.ofReal_mul, Complex.add_re, Complex.ofReal_re, Complex.mul_re, Complex.I_re, Complex.ofReal_im, Real.log_neg_eq_log]
Mathlib/Analysis/SpecialFunctions/Pow/Real.lean
85
87
/- Copyright (c) 2024 Fabrizio Barroero. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Fabrizio Barroero, Laura Capuano, Amos Turchet -/ import Mathlib.Analysis.Matrix import Mathlib.Data.Pi.Interval import Mathlib.Tactic.Rify /-! # Siegel's Lemma In this file we introduce and prove Siegel's Lemma in its most basic version. This is a fundamental tool in diophantine approximation and transcendency and says that there exists a "small" integral non-zero solution of a non-trivial underdetermined system of linear equations with integer coefficients. ## Main results - `exists_ne_zero_int_vec_norm_le`: Given a non-zero `m × n` matrix `A` with `m < n` the linear system it determines has a non-zero integer solution `t` with `‖t‖ ≤ ((n * ‖A‖) ^ ((m : ℝ) / (n - m)))` ## Notation - `‖_‖ ` : Matrix.seminormedAddCommGroup is the sup norm, the maximum of the absolute values of the entries of the matrix ## References See [M. Hindry and J. Silverman, Diophantine Geometry: an Introduction][hindrysilverman00]. -/ /- We set ‖⬝‖ to be Matrix.seminormedAddCommGroup -/ attribute [local instance] Matrix.seminormedAddCommGroup open Matrix Finset namespace Int.Matrix variable {α β : Type*} [Fintype α] [Fintype β] (A : Matrix α β ℤ) -- Some definitions and relative properties local notation3 "m" => Fintype.card α local notation3 "n" => Fintype.card β local notation3 "e" => m / ((n : ℝ) - m) -- exponent local notation3 "B" => Nat.floor (((n : ℝ) * max 1 ‖A‖) ^ e) -- B' is the vector with all components = B local notation3 "B'" => fun _ : β => (B : ℤ) -- T is the box [0 B]^n local notation3 "T" => Finset.Icc 0 B' local notation3 "P" => fun i : α => ∑ j : β, B * posPart (A i j) local notation3 "N" => fun i : α => ∑ j : β, B * (- negPart (A i j)) -- S is the box where the image of T goes local notation3 "S" => Finset.Icc N P section preparation /- In order to apply Pigeonhole we need: # Step 1: ∀ v ∈ T, A *ᵥ v ∈ S and # Step 2: #S < #T Pigeonhole will give different x and y in T with A.mulVec x = A.mulVec y in S Their difference is the solution we are looking for -/ -- # Step 1: ∀ v ∈ T, A *ᵥ v ∈ S private lemma image_T_subset_S [DecidableEq α] [DecidableEq β] (v) (hv : v ∈ T) : A *ᵥ v ∈ S := by rw [mem_Icc] at hv ⊢ have mulVec_def : A.mulVec v = fun i ↦ Finset.sum univ fun j : β ↦ A i j * v j := rfl rw [mulVec_def] refine ⟨fun i ↦ ?_, fun i ↦ ?_⟩ all_goals simp only [mul_neg] gcongr ∑ _ : α, ?_ with j _ -- Get rid of sums rw [← mul_comm (v j)] -- Move A i j to the right of the products rcases le_total 0 (A i j) with hsign | hsign-- We have to distinguish cases: we have now 4 goals · rw [negPart_eq_zero.2 hsign] exact mul_nonneg (hv.1 j) hsign · rw [negPart_eq_neg.2 hsign] simp only [mul_neg, neg_neg] exact mul_le_mul_of_nonpos_right (hv.2 j) hsign · rw [posPart_eq_self.2 hsign] exact mul_le_mul_of_nonneg_right (hv.2 j) hsign · rw [posPart_eq_zero.2 hsign] exact mul_nonpos_of_nonneg_of_nonpos (hv.1 j) hsign -- # Preparation for Step 2 private lemma card_T_eq [DecidableEq β] : #T = (B + 1) ^ n := by rw [Pi.card_Icc 0 B'] simp only [Pi.zero_apply, card_Icc, sub_zero, toNat_natCast_add_one, prod_const, card_univ,
add_pos_iff, zero_lt_one, or_true] -- This lemma is necessary to be able to apply the formula #(Icc a b) = b + 1 - a private lemma N_le_P_add_one (i : α) : N i ≤ P i + 1 := by calc N i _ ≤ 0 := by apply Finset.sum_nonpos intro j _ simp only [mul_neg, Left.neg_nonpos_iff] exact mul_nonneg (Nat.cast_nonneg B) (negPart_nonneg (A i j)) _ ≤ P i + 1 := by
Mathlib/NumberTheory/SiegelsLemma.lean
96
106
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import Mathlib.Algebra.CharP.Frobenius import Mathlib.Algebra.Polynomial.Derivative import Mathlib.Algebra.Polynomial.RingDivision import Mathlib.RingTheory.Polynomial.Basic /-! # Expand a polynomial by a factor of p, so `∑ aₙ xⁿ` becomes `∑ aₙ xⁿᵖ`. ## Main definitions * `Polynomial.expand R p f`: expand the polynomial `f` with coefficients in a commutative semiring `R` by a factor of p, so `expand R p (∑ aₙ xⁿ)` is `∑ aₙ xⁿᵖ`. * `Polynomial.contract p f`: the opposite of `expand`, so it sends `∑ aₙ xⁿᵖ` to `∑ aₙ xⁿ`. -/ universe u v w open Polynomial open Finset namespace Polynomial section CommSemiring variable (R : Type u) [CommSemiring R] {S : Type v} [CommSemiring S] (p q : ℕ) /-- Expand the polynomial by a factor of p, so `∑ aₙ xⁿ` becomes `∑ aₙ xⁿᵖ`. -/ noncomputable def expand : R[X] →ₐ[R] R[X] := { (eval₂RingHom C (X ^ p) : R[X] →+* R[X]) with commutes' := fun _ => eval₂_C _ _ } theorem coe_expand : (expand R p : R[X] → R[X]) = eval₂ C (X ^ p) := rfl variable {R} theorem expand_eq_comp_X_pow {f : R[X]} : expand R p f = f.comp (X ^ p) := rfl theorem expand_eq_sum {f : R[X]} : expand R p f = f.sum fun e a => C a * (X ^ p) ^ e := by simp [expand, eval₂] @[simp] theorem expand_C (r : R) : expand R p (C r) = C r := eval₂_C _ _ @[simp] theorem expand_X : expand R p X = X ^ p := eval₂_X _ _ @[simp] theorem expand_monomial (r : R) : expand R p (monomial q r) = monomial (q * p) r := by simp_rw [← smul_X_eq_monomial, map_smul, map_pow, expand_X, mul_comm, pow_mul] theorem expand_expand (f : R[X]) : expand R p (expand R q f) = expand R (p * q) f := Polynomial.induction_on f (fun r => by simp_rw [expand_C]) (fun f g ihf ihg => by simp_rw [map_add, ihf, ihg]) fun n r _ => by simp_rw [map_mul, expand_C, map_pow, expand_X, map_pow, expand_X, pow_mul] theorem expand_mul (f : R[X]) : expand R (p * q) f = expand R p (expand R q f) := (expand_expand p q f).symm @[simp] theorem expand_zero (f : R[X]) : expand R 0 f = C (eval 1 f) := by simp [expand] @[simp] theorem expand_one (f : R[X]) : expand R 1 f = f := Polynomial.induction_on f (fun r => by rw [expand_C]) (fun f g ihf ihg => by rw [map_add, ihf, ihg]) fun n r _ => by rw [map_mul, expand_C, map_pow, expand_X, pow_one] theorem expand_pow (f : R[X]) : expand R (p ^ q) f = (expand R p)^[q] f := Nat.recOn q (by rw [pow_zero, expand_one, Function.iterate_zero, id]) fun n ih => by rw [Function.iterate_succ_apply', pow_succ', expand_mul, ih] theorem derivative_expand (f : R[X]) : Polynomial.derivative (expand R p f) = expand R p (Polynomial.derivative f) * (p * (X ^ (p - 1) : R[X])) := by rw [coe_expand, derivative_eval₂_C, derivative_pow, C_eq_natCast, derivative_X, mul_one] theorem coeff_expand {p : ℕ} (hp : 0 < p) (f : R[X]) (n : ℕ) : (expand R p f).coeff n = if p ∣ n then f.coeff (n / p) else 0 := by simp only [expand_eq_sum] simp_rw [coeff_sum, ← pow_mul, C_mul_X_pow_eq_monomial, coeff_monomial, sum] split_ifs with h · rw [Finset.sum_eq_single (n / p), Nat.mul_div_cancel' h, if_pos rfl] · intro b _ hb2 rw [if_neg] intro hb3 apply hb2 rw [← hb3, Nat.mul_div_cancel_left b hp] · intro hn rw [not_mem_support_iff.1 hn] split_ifs <;> rfl · rw [Finset.sum_eq_zero] intro k _ rw [if_neg] exact fun hkn => h ⟨k, hkn.symm⟩ @[simp] theorem coeff_expand_mul {p : ℕ} (hp : 0 < p) (f : R[X]) (n : ℕ) : (expand R p f).coeff (n * p) = f.coeff n := by rw [coeff_expand hp, if_pos (dvd_mul_left _ _), Nat.mul_div_cancel _ hp] @[simp] theorem coeff_expand_mul' {p : ℕ} (hp : 0 < p) (f : R[X]) (n : ℕ) : (expand R p f).coeff (p * n) = f.coeff n := by rw [mul_comm, coeff_expand_mul hp] /-- Expansion is injective. -/ theorem expand_injective {n : ℕ} (hn : 0 < n) : Function.Injective (expand R n) := fun g g' H => ext fun k => by rw [← coeff_expand_mul hn, H, coeff_expand_mul hn] theorem expand_inj {p : ℕ} (hp : 0 < p) {f g : R[X]} : expand R p f = expand R p g ↔ f = g := (expand_injective hp).eq_iff theorem expand_eq_zero {p : ℕ} (hp : 0 < p) {f : R[X]} : expand R p f = 0 ↔ f = 0 := (expand_injective hp).eq_iff' (map_zero _) theorem expand_ne_zero {p : ℕ} (hp : 0 < p) {f : R[X]} : expand R p f ≠ 0 ↔ f ≠ 0 := (expand_eq_zero hp).not theorem expand_eq_C {p : ℕ} (hp : 0 < p) {f : R[X]} {r : R} : expand R p f = C r ↔ f = C r := by rw [← expand_C, expand_inj hp, expand_C] theorem natDegree_expand (p : ℕ) (f : R[X]) : (expand R p f).natDegree = f.natDegree * p := by rcases p.eq_zero_or_pos with hp | hp · rw [hp, coe_expand, pow_zero, mul_zero, ← C_1, eval₂_hom, natDegree_C] by_cases hf : f = 0 · rw [hf, map_zero, natDegree_zero, zero_mul] have hf1 : expand R p f ≠ 0 := mt (expand_eq_zero hp).1 hf rw [← Nat.cast_inj (R := WithBot ℕ), ← degree_eq_natDegree hf1] refine le_antisymm ((degree_le_iff_coeff_zero _ _).2 fun n hn => ?_) ?_ · rw [coeff_expand hp] split_ifs with hpn · rw [coeff_eq_zero_of_natDegree_lt] contrapose! hn norm_cast rw [← Nat.div_mul_cancel hpn] exact Nat.mul_le_mul_right p hn · rfl · refine le_degree_of_ne_zero ?_ rw [coeff_expand_mul hp, ← leadingCoeff] exact mt leadingCoeff_eq_zero.1 hf theorem leadingCoeff_expand {p : ℕ} {f : R[X]} (hp : 0 < p) : (expand R p f).leadingCoeff = f.leadingCoeff := by simp_rw [leadingCoeff, natDegree_expand, coeff_expand_mul hp] theorem monic_expand_iff {p : ℕ} {f : R[X]} (hp : 0 < p) : (expand R p f).Monic ↔ f.Monic := by simp only [Monic, leadingCoeff_expand hp] alias ⟨_, Monic.expand⟩ := monic_expand_iff theorem map_expand {p : ℕ} {f : R →+* S} {q : R[X]} : map f (expand R p q) = expand S p (map f q) := by by_cases hp : p = 0 · simp [hp] ext rw [coeff_map, coeff_expand (Nat.pos_of_ne_zero hp), coeff_expand (Nat.pos_of_ne_zero hp)] split_ifs <;> simp_all @[simp] theorem expand_eval (p : ℕ) (P : R[X]) (r : R) : eval r (expand R p P) = eval (r ^ p) P := by refine Polynomial.induction_on P (fun a => by simp) (fun f g hf hg => ?_) fun n a _ => by simp rw [map_add, eval_add, eval_add, hf, hg] @[simp] theorem expand_aeval {A : Type*} [Semiring A] [Algebra R A] (p : ℕ) (P : R[X]) (r : A) : aeval r (expand R p P) = aeval (r ^ p) P := by refine Polynomial.induction_on P (fun a => by simp) (fun f g hf hg => ?_) fun n a _ => by simp rw [map_add, aeval_add, aeval_add, hf, hg] /-- The opposite of `expand`: sends `∑ aₙ xⁿᵖ` to `∑ aₙ xⁿ`. -/ noncomputable def contract (p : ℕ) (f : R[X]) : R[X] := ∑ n ∈ range (f.natDegree + 1), monomial n (f.coeff (n * p)) theorem coeff_contract {p : ℕ} (hp : p ≠ 0) (f : R[X]) (n : ℕ) : (contract p f).coeff n = f.coeff (n * p) := by simp only [contract, coeff_monomial, sum_ite_eq', finset_sum_coeff, mem_range, not_lt, ite_eq_left_iff] intro hn apply (coeff_eq_zero_of_natDegree_lt _).symm calc f.natDegree < f.natDegree + 1 := Nat.lt_succ_self _ _ ≤ n * 1 := by simpa only [mul_one] using hn _ ≤ n * p := mul_le_mul_of_nonneg_left (show 1 ≤ p from hp.bot_lt) (zero_le n) theorem map_contract {p : ℕ} (hp : p ≠ 0) {f : R →+* S} {q : R[X]} : (q.contract p).map f = (q.map f).contract p := ext fun n ↦ by simp only [coeff_map, coeff_contract hp] theorem contract_expand {f : R[X]} (hp : p ≠ 0) : contract p (expand R p f) = f := by ext simp [coeff_contract hp, coeff_expand hp.bot_lt, Nat.mul_div_cancel _ hp.bot_lt] theorem contract_one {f : R[X]} : contract 1 f = f := ext fun n ↦ by rw [coeff_contract one_ne_zero, mul_one] @[simp] theorem contract_C (r : R) : contract p (C r) = C r := by simp [contract] theorem contract_add {p : ℕ} (hp : p ≠ 0) (f g : R[X]) : contract p (f + g) = contract p f + contract p g := by ext; simp_rw [coeff_add, coeff_contract hp, coeff_add] theorem contract_mul_expand {p : ℕ} (hp : p ≠ 0) (f g : R[X]) : contract p (f * expand R p g) = contract p f * g := by ext n rw [coeff_contract hp, coeff_mul, coeff_mul, ← sum_subset (s₁ := (antidiagonal n).image fun x ↦ (x.1 * p, x.2 * p)), sum_image] · simp_rw [coeff_expand_mul hp.bot_lt, coeff_contract hp] · intro x hx y hy eq; simpa only [Prod.ext_iff, Nat.mul_right_cancel_iff hp.bot_lt] using eq · simp_rw [subset_iff, mem_image, mem_antidiagonal]; rintro _ ⟨x, rfl, rfl⟩; simp_rw [add_mul] simp_rw [mem_image, mem_antidiagonal] intro ⟨x, y⟩ eq nex by_cases h : p ∣ y · obtain ⟨x, rfl⟩ : p ∣ x := (Nat.dvd_add_iff_left h).mpr (eq ▸ dvd_mul_left p n) obtain ⟨y, rfl⟩ := h refine (nex ⟨⟨x, y⟩, (Nat.mul_right_cancel_iff hp.bot_lt).mp ?_, by simp_rw [mul_comm]⟩).elim rw [← eq, mul_comm, mul_add] · rw [coeff_expand hp.bot_lt, if_neg h, mul_zero] @[simp] theorem isCoprime_expand {f g : R[X]} {p : ℕ} (hp : p ≠ 0) : IsCoprime (expand R p f) (expand R p g) ↔ IsCoprime f g := ⟨fun ⟨a, b, eq⟩ ↦ ⟨contract p a, contract p b, by simp_rw [← contract_mul_expand hp, ← contract_add hp, eq, ← C_1, contract_C]⟩, (·.map _)⟩ section ExpChar theorem expand_contract [CharP R p] [NoZeroDivisors R] {f : R[X]} (hf : Polynomial.derivative f = 0) (hp : p ≠ 0) : expand R p (contract p f) = f := by ext n rw [coeff_expand hp.bot_lt, coeff_contract hp] split_ifs with h · rw [Nat.div_mul_cancel h] · rcases n with - | n · exact absurd (dvd_zero p) h have := coeff_derivative f n rw [hf, coeff_zero, zero_eq_mul] at this rcases this with h' | _ · rw [h'] rename_i _ _ _ h' rw [← Nat.cast_succ, CharP.cast_eq_zero_iff R p] at h' exact absurd h' h variable [ExpChar R p] theorem expand_contract' [NoZeroDivisors R] {f : R[X]} (hf : Polynomial.derivative f = 0) : expand R p (contract p f) = f := by obtain _ | @⟨_, hprime, hchar⟩ := ‹ExpChar R p› · rw [expand_one, contract_one] · haveI := Fact.mk hchar; exact expand_contract p hf hprime.ne_zero theorem expand_char (f : R[X]) : map (frobenius R p) (expand R p f) = f ^ p := by refine f.induction_on' (fun a b ha hb => ?_) fun n a => ?_ · rw [map_add, Polynomial.map_add, ha, hb, add_pow_expChar] · rw [expand_monomial, map_monomial, ← C_mul_X_pow_eq_monomial, ← C_mul_X_pow_eq_monomial, mul_pow, ← C.map_pow, frobenius_def] ring theorem map_expand_pow_char (f : R[X]) (n : ℕ) : map (frobenius R p ^ n) (expand R (p ^ n) f) = f ^ p ^ n := by induction n with | zero => simp [RingHom.one_def] | succ _ n_ih => symm rw [pow_succ, pow_mul, ← n_ih, ← expand_char, pow_succ', RingHom.mul_def, ← map_map, mul_comm, expand_mul, ← map_expand] end ExpChar end CommSemiring section rootMultiplicity variable {R : Type u} [CommRing R] {p n : ℕ} [ExpChar R p] {f : R[X]} {r : R} theorem rootMultiplicity_expand_pow : (expand R (p ^ n) f).rootMultiplicity r = p ^ n * f.rootMultiplicity (r ^ p ^ n) := by obtain rfl | h0 := eq_or_ne f 0; · simp obtain ⟨g, hg, ndvd⟩ := f.exists_eq_pow_rootMultiplicity_mul_and_not_dvd h0 (r ^ p ^ n) rw [dvd_iff_isRoot, ← eval_X (x := r), ← eval_pow, ← isRoot_comp, ← expand_eq_comp_X_pow] at ndvd conv_lhs => rw [hg, map_mul, map_pow, map_sub, expand_X, expand_C, map_pow, ← sub_pow_expChar_pow, ← pow_mul, mul_comm, rootMultiplicity_mul_X_sub_C_pow (expand_ne_zero (expChar_pow_pos R p n) |>.mpr <| right_ne_zero_of_mul <| hg ▸ h0), rootMultiplicity_eq_zero ndvd, zero_add] theorem rootMultiplicity_expand : (expand R p f).rootMultiplicity r = p * f.rootMultiplicity (r ^ p) := by rw [← pow_one p, rootMultiplicity_expand_pow] end rootMultiplicity section IsDomain variable (R : Type u) [CommRing R] [IsDomain R] theorem isLocalHom_expand {p : ℕ} (hp : 0 < p) : IsLocalHom (expand R p) := by refine ⟨fun f hf1 => ?_⟩ have hf2 := eq_C_of_degree_eq_zero (degree_eq_zero_of_isUnit hf1) rw [coeff_expand hp, if_pos (dvd_zero _), p.zero_div] at hf2 rw [hf2, isUnit_C] at hf1; rw [expand_eq_C hp] at hf2; rwa [hf2, isUnit_C] variable {R} theorem of_irreducible_expand {p : ℕ} (hp : p ≠ 0) {f : R[X]} (hf : Irreducible (expand R p f)) : Irreducible f := let _ := isLocalHom_expand R hp.bot_lt hf.of_map theorem of_irreducible_expand_pow {p : ℕ} (hp : p ≠ 0) {f : R[X]} {n : ℕ} : Irreducible (expand R (p ^ n) f) → Irreducible f := Nat.recOn n (fun hf => by rwa [pow_zero, expand_one] at hf) fun n ih hf => ih <| of_irreducible_expand hp <| by rw [pow_succ'] at hf rwa [expand_expand] end IsDomain
end Polynomial
Mathlib/Algebra/Polynomial/Expand.lean
323
328
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Chris Hughes, Floris van Doorn, Yaël Dillies -/ import Mathlib.Data.Nat.Basic import Mathlib.Tactic.GCongr.CoreAttrs import Mathlib.Tactic.Common import Mathlib.Tactic.Monotonicity.Attr /-! # Factorial and variants This file defines the factorial, along with the ascending and descending variants. For the proof that the factorial of `n` counts the permutations of an `n`-element set, see `Fintype.card_perm`. ## Main declarations * `Nat.factorial`: The factorial. * `Nat.ascFactorial`: The ascending factorial. It is the product of natural numbers from `n` to `n + k - 1`. * `Nat.descFactorial`: The descending factorial. It is the product of natural numbers from `n - k + 1` to `n`. -/ namespace Nat /-- `Nat.factorial n` is the factorial of `n`. -/ def factorial : ℕ → ℕ | 0 => 1 | succ n => succ n * factorial n /-- factorial notation `(n)!` for `Nat.factorial n`. In Lean, names can end with exclamation marks (e.g. `List.get!`), so you cannot write `n!` in Lean, but must write `(n)!` or `n !` instead. The former is preferred, since Lean can confuse the `!` in `n !` as the (prefix) boolean negation operation in some cases. For numerals the parentheses are not required, so e.g. `0!` or `1!` work fine. Todo: replace occurrences of `n !` with `(n)!` in Mathlib. -/ scoped notation:10000 n "!" => Nat.factorial n section Factorial variable {m n : ℕ} @[simp] theorem factorial_zero : 0! = 1 := rfl theorem factorial_succ (n : ℕ) : (n + 1)! = (n + 1) * n ! := rfl @[simp] theorem factorial_one : 1! = 1 := rfl @[simp] theorem factorial_two : 2! = 2 := rfl theorem mul_factorial_pred (hn : n ≠ 0) : n * (n - 1)! = n ! := Nat.sub_add_cancel (one_le_iff_ne_zero.mpr hn) ▸ rfl theorem factorial_pos : ∀ n, 0 < n ! | 0 => Nat.zero_lt_one | succ n => Nat.mul_pos (succ_pos _) (factorial_pos n) theorem factorial_ne_zero (n : ℕ) : n ! ≠ 0 := ne_of_gt (factorial_pos _) theorem factorial_dvd_factorial {m n} (h : m ≤ n) : m ! ∣ n ! := by induction h with | refl => exact Nat.dvd_refl _ | step _ ih => exact Nat.dvd_trans ih (Nat.dvd_mul_left _ _) theorem dvd_factorial : ∀ {m n}, 0 < m → m ≤ n → m ∣ n ! | succ _, _, _, h => Nat.dvd_trans (Nat.dvd_mul_right _ _) (factorial_dvd_factorial h) @[mono, gcongr] theorem factorial_le {m n} (h : m ≤ n) : m ! ≤ n ! := le_of_dvd (factorial_pos _) (factorial_dvd_factorial h) theorem factorial_mul_pow_le_factorial : ∀ {m n : ℕ}, m ! * (m + 1) ^ n ≤ (m + n)! | m, 0 => by simp | m, n + 1 => by rw [← Nat.add_assoc, factorial_succ, Nat.mul_comm (_ + 1), Nat.pow_succ, ← Nat.mul_assoc] exact Nat.mul_le_mul factorial_mul_pow_le_factorial (succ_le_succ (le_add_right _ _)) theorem factorial_lt (hn : 0 < n) : n ! < m ! ↔ n < m := by refine ⟨fun h => not_le.mp fun hmn => Nat.not_le_of_lt h (factorial_le hmn), fun h => ?_⟩ have : ∀ {n}, 0 < n → n ! < (n + 1)! := by intro k hk rw [factorial_succ, succ_mul, Nat.lt_add_left_iff_pos] exact Nat.mul_pos hk k.factorial_pos induction h generalizing hn with | refl => exact this hn | step hnk ih => exact lt_trans (ih hn) <| this <| lt_trans hn <| lt_of_succ_le hnk @[gcongr] lemma factorial_lt_of_lt {m n : ℕ} (hn : 0 < n) (h : n < m) : n ! < m ! := (factorial_lt hn).mpr h @[simp] lemma one_lt_factorial : 1 < n ! ↔ 1 < n := factorial_lt Nat.one_pos @[simp] theorem factorial_eq_one : n ! = 1 ↔ n ≤ 1 := by constructor · intro h rw [← not_lt, ← one_lt_factorial, h] apply lt_irrefl · rintro (_|_|_) <;> rfl theorem factorial_inj (hn : 1 < n) : n ! = m ! ↔ n = m := by refine ⟨fun h => ?_, congr_arg _⟩ obtain hnm | rfl | hnm := lt_trichotomy n m · rw [← factorial_lt <| lt_of_succ_lt hn, h] at hnm cases lt_irrefl _ hnm · rfl rw [← one_lt_factorial, h, one_lt_factorial] at hn rw [← factorial_lt <| lt_of_succ_lt hn, h] at hnm cases lt_irrefl _ hnm theorem factorial_inj' (h : 1 < n ∨ 1 < m) : n ! = m ! ↔ n = m := by obtain hn|hm := h · exact factorial_inj hn · rw [eq_comm, factorial_inj hm, eq_comm] theorem self_le_factorial : ∀ n : ℕ, n ≤ n ! | 0 => Nat.zero_le _ | k + 1 => Nat.le_mul_of_pos_right _ (Nat.one_le_of_lt k.factorial_pos) theorem lt_factorial_self {n : ℕ} (hi : 3 ≤ n) : n < n ! := by have : 0 < n := by omega have hn : 1 < pred n := le_pred_of_lt (succ_le_iff.mp hi) rw [← succ_pred_eq_of_pos ‹0 < n›, factorial_succ] exact (Nat.lt_mul_iff_one_lt_right (pred n).succ_pos).2 ((Nat.lt_of_lt_of_le hn (self_le_factorial _))) theorem add_factorial_succ_lt_factorial_add_succ {i : ℕ} (n : ℕ) (hi : 2 ≤ i) : i + (n + 1)! < (i + n + 1)! := by rw [factorial_succ (i + _), Nat.add_mul, Nat.one_mul] have := (i + n).self_le_factorial refine Nat.add_lt_add_of_lt_of_le (Nat.lt_of_le_of_lt ?_ ((Nat.lt_mul_iff_one_lt_right ?_).2 ?_)) (factorial_le ?_) <;> omega theorem add_factorial_lt_factorial_add {i n : ℕ} (hi : 2 ≤ i) (hn : 1 ≤ n) : i + n ! < (i + n)! := by cases hn · rw [factorial_one] exact lt_factorial_self (succ_le_succ hi) exact add_factorial_succ_lt_factorial_add_succ _ hi theorem add_factorial_succ_le_factorial_add_succ (i : ℕ) (n : ℕ) : i + (n + 1)! ≤ (i + (n + 1))! := by cases (le_or_lt (2 : ℕ) i) · rw [← Nat.add_assoc] apply Nat.le_of_lt apply add_factorial_succ_lt_factorial_add_succ assumption · match i with | 0 => simp | 1 => rw [← Nat.add_assoc, factorial_succ (1 + n), Nat.add_mul, Nat.one_mul, Nat.add_comm 1 n, Nat.add_le_add_iff_right] exact Nat.mul_pos n.succ_pos n.succ.factorial_pos | succ (succ n) => contradiction
theorem add_factorial_le_factorial_add (i : ℕ) {n : ℕ} (n1 : 1 ≤ n) : i + n ! ≤ (i + n)! := by rcases n1 with - | @h · exact self_le_factorial _ exact add_factorial_succ_le_factorial_add_succ i h theorem factorial_mul_pow_sub_le_factorial {n m : ℕ} (hnm : n ≤ m) : n ! * n ^ (m - n) ≤ m ! := by calc _ ≤ n ! * (n + 1) ^ (m - n) := Nat.mul_le_mul_left _ (Nat.pow_le_pow_left n.le_succ _) _ ≤ _ := by simpa [hnm] using @Nat.factorial_mul_pow_le_factorial n (m - n) lemma factorial_le_pow : ∀ n, n ! ≤ n ^ n | 0 => le_refl _ | n + 1 =>
Mathlib/Data/Nat/Factorial/Basic.lean
166
179
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Mathlib.Data.Stream.Defs import Mathlib.Logic.Function.Basic import Mathlib.Data.List.Defs import Mathlib.Data.Nat.Basic import Mathlib.Tactic.Common /-! # Streams a.k.a. infinite lists a.k.a. infinite sequences -/ open Nat Function Option namespace Stream' universe u v w variable {α : Type u} {β : Type v} {δ : Type w} variable (m n : ℕ) (x y : List α) (a b : Stream' α) instance [Inhabited α] : Inhabited (Stream' α) := ⟨Stream'.const default⟩ @[simp] protected theorem eta (s : Stream' α) : head s :: tail s = s := funext fun i => by cases i <;> rfl /-- Alias for `Stream'.eta` to match `List` API. -/ alias cons_head_tail := Stream'.eta @[ext] protected theorem ext {s₁ s₂ : Stream' α} : (∀ n, get s₁ n = get s₂ n) → s₁ = s₂ := fun h => funext h @[simp] theorem get_zero_cons (a : α) (s : Stream' α) : get (a::s) 0 = a := rfl @[simp] theorem head_cons (a : α) (s : Stream' α) : head (a::s) = a := rfl @[simp] theorem tail_cons (a : α) (s : Stream' α) : tail (a::s) = s := rfl @[simp] theorem get_drop (n m : ℕ) (s : Stream' α) : get (drop m s) n = get s (m + n) := by rw [Nat.add_comm] rfl theorem tail_eq_drop (s : Stream' α) : tail s = drop 1 s := rfl @[simp] theorem drop_drop (n m : ℕ) (s : Stream' α) : drop n (drop m s) = drop (m + n) s := by ext; simp [Nat.add_assoc] @[simp] theorem get_tail {n : ℕ} {s : Stream' α} : s.tail.get n = s.get (n + 1) := rfl @[simp] theorem tail_drop' {i : ℕ} {s : Stream' α} : tail (drop i s) = s.drop (i + 1) := by ext; simp [Nat.add_comm, Nat.add_assoc, Nat.add_left_comm] @[simp] theorem drop_tail' {i : ℕ} {s : Stream' α} : drop i (tail s) = s.drop (i + 1) := rfl theorem tail_drop (n : ℕ) (s : Stream' α) : tail (drop n s) = drop n (tail s) := by simp theorem get_succ (n : ℕ) (s : Stream' α) : get s (succ n) = get (tail s) n := rfl @[simp] theorem get_succ_cons (n : ℕ) (s : Stream' α) (x : α) : get (x :: s) n.succ = get s n := rfl @[simp] lemma get_cons_append_zero {a : α} {x : List α} {s : Stream' α} : (a :: x ++ₛ s).get 0 = a := rfl @[simp] lemma append_eq_cons {a : α} {as : Stream' α} : [a] ++ₛ as = a :: as := by rfl @[simp] theorem drop_zero {s : Stream' α} : s.drop 0 = s := rfl theorem drop_succ (n : ℕ) (s : Stream' α) : drop (succ n) s = drop n (tail s) := rfl theorem head_drop (a : Stream' α) (n : ℕ) : (a.drop n).head = a.get n := by simp theorem cons_injective2 : Function.Injective2 (cons : α → Stream' α → Stream' α) := fun x y s t h => ⟨by rw [← get_zero_cons x s, h, get_zero_cons], Stream'.ext fun n => by rw [← get_succ_cons n _ x, h, get_succ_cons]⟩ theorem cons_injective_left (s : Stream' α) : Function.Injective fun x => cons x s := cons_injective2.left _ theorem cons_injective_right (x : α) : Function.Injective (cons x) := cons_injective2.right _ theorem all_def (p : α → Prop) (s : Stream' α) : All p s = ∀ n, p (get s n) := rfl theorem any_def (p : α → Prop) (s : Stream' α) : Any p s = ∃ n, p (get s n) := rfl @[simp] theorem mem_cons (a : α) (s : Stream' α) : a ∈ a::s := Exists.intro 0 rfl theorem mem_cons_of_mem {a : α} {s : Stream' α} (b : α) : a ∈ s → a ∈ b::s := fun ⟨n, h⟩ => Exists.intro (succ n) (by rw [get_succ, tail_cons, h]) theorem eq_or_mem_of_mem_cons {a b : α} {s : Stream' α} : (a ∈ b::s) → a = b ∨ a ∈ s := fun ⟨n, h⟩ => by rcases n with - | n' · left exact h · right rw [get_succ, tail_cons] at h exact ⟨n', h⟩ theorem mem_of_get_eq {n : ℕ} {s : Stream' α} {a : α} : a = get s n → a ∈ s := fun h => Exists.intro n h section Map variable (f : α → β) theorem drop_map (n : ℕ) (s : Stream' α) : drop n (map f s) = map f (drop n s) := Stream'.ext fun _ => rfl @[simp] theorem get_map (n : ℕ) (s : Stream' α) : get (map f s) n = f (get s n) := rfl theorem tail_map (s : Stream' α) : tail (map f s) = map f (tail s) := rfl @[simp] theorem head_map (s : Stream' α) : head (map f s) = f (head s) := rfl theorem map_eq (s : Stream' α) : map f s = f (head s)::map f (tail s) := by rw [← Stream'.eta (map f s), tail_map, head_map] theorem map_cons (a : α) (s : Stream' α) : map f (a::s) = f a::map f s := by rw [← Stream'.eta (map f (a::s)), map_eq]; rfl @[simp] theorem map_id (s : Stream' α) : map id s = s := rfl @[simp] theorem map_map (g : β → δ) (f : α → β) (s : Stream' α) : map g (map f s) = map (g ∘ f) s := rfl @[simp] theorem map_tail (s : Stream' α) : map f (tail s) = tail (map f s) := rfl theorem mem_map {a : α} {s : Stream' α} : a ∈ s → f a ∈ map f s := fun ⟨n, h⟩ => Exists.intro n (by rw [get_map, h]) theorem exists_of_mem_map {f} {b : β} {s : Stream' α} : b ∈ map f s → ∃ a, a ∈ s ∧ f a = b := fun ⟨n, h⟩ => ⟨get s n, ⟨n, rfl⟩, h.symm⟩ end Map section Zip variable (f : α → β → δ) theorem drop_zip (n : ℕ) (s₁ : Stream' α) (s₂ : Stream' β) : drop n (zip f s₁ s₂) = zip f (drop n s₁) (drop n s₂) := Stream'.ext fun _ => rfl @[simp] theorem get_zip (n : ℕ) (s₁ : Stream' α) (s₂ : Stream' β) : get (zip f s₁ s₂) n = f (get s₁ n) (get s₂ n) := rfl theorem head_zip (s₁ : Stream' α) (s₂ : Stream' β) : head (zip f s₁ s₂) = f (head s₁) (head s₂) := rfl theorem tail_zip (s₁ : Stream' α) (s₂ : Stream' β) : tail (zip f s₁ s₂) = zip f (tail s₁) (tail s₂) := rfl theorem zip_eq (s₁ : Stream' α) (s₂ : Stream' β) : zip f s₁ s₂ = f (head s₁) (head s₂)::zip f (tail s₁) (tail s₂) := by rw [← Stream'.eta (zip f s₁ s₂)]; rfl @[simp] theorem get_enum (s : Stream' α) (n : ℕ) : get (enum s) n = (n, s.get n) := rfl theorem enum_eq_zip (s : Stream' α) : enum s = zip Prod.mk nats s := rfl end Zip @[simp] theorem mem_const (a : α) : a ∈ const a := Exists.intro 0 rfl theorem const_eq (a : α) : const a = a::const a := by apply Stream'.ext; intro n cases n <;> rfl @[simp] theorem tail_const (a : α) : tail (const a) = const a := suffices tail (a::const a) = const a by rwa [← const_eq] at this rfl @[simp] theorem map_const (f : α → β) (a : α) : map f (const a) = const (f a) := rfl @[simp] theorem get_const (n : ℕ) (a : α) : get (const a) n = a := rfl @[simp] theorem drop_const (n : ℕ) (a : α) : drop n (const a) = const a := Stream'.ext fun _ => rfl @[simp] theorem head_iterate (f : α → α) (a : α) : head (iterate f a) = a := rfl theorem get_succ_iterate' (n : ℕ) (f : α → α) (a : α) : get (iterate f a) (succ n) = f (get (iterate f a) n) := rfl theorem tail_iterate (f : α → α) (a : α) : tail (iterate f a) = iterate f (f a) := by ext n rw [get_tail] induction' n with n' ih · rfl · rw [get_succ_iterate', ih, get_succ_iterate'] theorem iterate_eq (f : α → α) (a : α) : iterate f a = a::iterate f (f a) := by rw [← Stream'.eta (iterate f a)] rw [tail_iterate]; rfl @[simp] theorem get_zero_iterate (f : α → α) (a : α) : get (iterate f a) 0 = a := rfl
theorem get_succ_iterate (n : ℕ) (f : α → α) (a : α) : get (iterate f a) (succ n) = get (iterate f (f a)) n := by rw [get_succ, tail_iterate]
Mathlib/Data/Stream/Init.lean
246
248
/- Copyright (c) 2024 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import Mathlib.Algebra.Order.Hom.Ring import Mathlib.Data.ENat.Basic import Mathlib.SetTheory.Cardinal.Basic /-! # Conversion between `Cardinal` and `ℕ∞` In this file we define a coercion `Cardinal.ofENat : ℕ∞ → Cardinal` and a projection `Cardinal.toENat : Cardinal →+*o ℕ∞`. We also prove basic theorems about these definitions. ## Implementation notes We define `Cardinal.ofENat` as a function instead of a bundled homomorphism so that we can use it as a coercion and delaborate its application to `↑n`. We define `Cardinal.toENat` as a bundled homomorphism so that we can use all the theorems about homomorphisms without specializing them to this function. Since it is not registered as a coercion, the argument about delaboration does not apply. ## Keywords set theory, cardinals, extended natural numbers -/ assert_not_exists Field open Function Set universe u v namespace Cardinal /-- Coercion `ℕ∞ → Cardinal`. It sends natural numbers to natural numbers and `⊤` to `ℵ₀`. See also `Cardinal.ofENatHom` for a bundled homomorphism version. -/ @[coe] def ofENat : ℕ∞ → Cardinal | (n : ℕ) => n | ⊤ => ℵ₀ instance : Coe ENat Cardinal := ⟨Cardinal.ofENat⟩ @[simp, norm_cast] lemma ofENat_top : ofENat ⊤ = ℵ₀ := rfl @[simp, norm_cast] lemma ofENat_nat (n : ℕ) : ofENat n = n := rfl @[simp, norm_cast] lemma ofENat_zero : ofENat 0 = 0 := rfl @[simp, norm_cast] lemma ofENat_one : ofENat 1 = 1 := rfl @[simp, norm_cast] lemma ofENat_ofNat (n : ℕ) [n.AtLeastTwo] : ((ofNat(n) : ℕ∞) : Cardinal) = OfNat.ofNat n := rfl lemma ofENat_strictMono : StrictMono ofENat := WithTop.strictMono_iff.2 ⟨Nat.strictMono_cast, nat_lt_aleph0⟩ @[simp, norm_cast] lemma ofENat_lt_ofENat {m n : ℕ∞} : (m : Cardinal) < n ↔ m < n := ofENat_strictMono.lt_iff_lt @[gcongr, mono] alias ⟨_, ofENat_lt_ofENat_of_lt⟩ := ofENat_lt_ofENat @[simp, norm_cast] lemma ofENat_lt_aleph0 {m : ℕ∞} : (m : Cardinal) < ℵ₀ ↔ m < ⊤ :=
ofENat_lt_ofENat (n := ⊤)
Mathlib/SetTheory/Cardinal/ENat.lean
67
67
/- Copyright (c) 2021 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import Mathlib.Geometry.Manifold.Algebra.Structures import Mathlib.Geometry.Manifold.BumpFunction import Mathlib.Topology.MetricSpace.PartitionOfUnity import Mathlib.Topology.ShrinkingLemma /-! # Smooth partition of unity In this file we define two structures, `SmoothBumpCovering` and `SmoothPartitionOfUnity`. Both structures describe coverings of a set by a locally finite family of supports of smooth functions with some additional properties. The former structure is mostly useful as an intermediate step in the construction of a smooth partition of unity but some proofs that traditionally deal with a partition of unity can use a `SmoothBumpCovering` as well. Given a real manifold `M` and its subset `s`, a `SmoothBumpCovering ι I M s` is a collection of `SmoothBumpFunction`s `f i` indexed by `i : ι` such that * the center of each `f i` belongs to `s`; * the family of sets `support (f i)` is locally finite; * for each `x ∈ s`, there exists `i : ι` such that `f i =ᶠ[𝓝 x] 1`. In the same settings, a `SmoothPartitionOfUnity ι I M s` is a collection of smooth nonnegative functions `f i : C^∞⟮I, M; 𝓘(ℝ), ℝ⟯`, `i : ι`, such that * the family of sets `support (f i)` is locally finite; * for each `x ∈ s`, the sum `∑ᶠ i, f i x` equals one; * for each `x`, the sum `∑ᶠ i, f i x` is less than or equal to one. We say that `f : SmoothBumpCovering ι I M s` is *subordinate* to a map `U : M → Set M` if for each index `i`, we have `tsupport (f i) ⊆ U (f i).c`. This notion is a bit more general than being subordinate to an open covering of `M`, because we make no assumption about the way `U x` depends on `x`. We prove that on a smooth finitely dimensional real manifold with `σ`-compact Hausdorff topology, for any `U : M → Set M` such that `∀ x ∈ s, U x ∈ 𝓝 x` there exists a `SmoothBumpCovering ι I M s` subordinate to `U`. Then we use this fact to prove a similar statement about smooth partitions of unity, see `SmoothPartitionOfUnity.exists_isSubordinate`. Finally, we use existence of a partition of unity to prove lemma `exists_smooth_forall_mem_convex_of_local` that allows us to construct a globally defined smooth function from local functions. ## TODO * Build a framework for to transfer local definitions to global using partition of unity and use it to define, e.g., the integral of a differential form over a manifold. Lemma `exists_smooth_forall_mem_convex_of_local` is a first step in this direction. ## Tags smooth bump function, partition of unity -/ universe uι uE uH uM uF open Function Filter Module Set open scoped Topology Manifold ContDiff noncomputable section variable {ι : Type uι} {E : Type uE} [NormedAddCommGroup E] [NormedSpace ℝ E] {F : Type uF} [NormedAddCommGroup F] [NormedSpace ℝ F] {H : Type uH} [TopologicalSpace H] (I : ModelWithCorners ℝ E H) {M : Type uM} [TopologicalSpace M] [ChartedSpace H M] /-! ### Covering by supports of smooth bump functions In this section we define `SmoothBumpCovering ι I M s` to be a collection of `SmoothBumpFunction`s such that their supports is a locally finite family of sets and for each `x ∈ s` some function `f i` from the collection is equal to `1` in a neighborhood of `x`. A covering of this type is useful to construct a smooth partition of unity and can be used instead of a partition of unity in some proofs. We prove that on a smooth finite dimensional real manifold with `σ`-compact Hausdorff topology, for any `U : M → Set M` such that `∀ x ∈ s, U x ∈ 𝓝 x` there exists a `SmoothBumpCovering ι I M s` subordinate to `U`. -/ variable (ι M) /-- We say that a collection of `SmoothBumpFunction`s is a `SmoothBumpCovering` of a set `s` if * `(f i).c ∈ s` for all `i`; * the family `fun i ↦ support (f i)` is locally finite; * for each point `x ∈ s` there exists `i` such that `f i =ᶠ[𝓝 x] 1`; in other words, `x` belongs to the interior of `{y | f i y = 1}`; If `M` is a finite dimensional real manifold which is a `σ`-compact Hausdorff topological space, then for every covering `U : M → Set M`, `∀ x, U x ∈ 𝓝 x`, there exists a `SmoothBumpCovering` subordinate to `U`, see `SmoothBumpCovering.exists_isSubordinate`. This covering can be used, e.g., to construct a partition of unity and to prove the weak Whitney embedding theorem. -/ structure SmoothBumpCovering [FiniteDimensional ℝ E] (s : Set M := univ) where /-- The center point of each bump in the smooth covering. -/ c : ι → M /-- A smooth bump function around `c i`. -/ toFun : ∀ i, SmoothBumpFunction I (c i) /-- All the bump functions in the covering are centered at points in `s`. -/ c_mem' : ∀ i, c i ∈ s /-- Around each point, there are only finitely many nonzero bump functions in the family. -/ locallyFinite' : LocallyFinite fun i => support (toFun i) /-- Around each point in `s`, one of the bump functions is equal to `1`. -/ eventuallyEq_one' : ∀ x ∈ s, ∃ i, toFun i =ᶠ[𝓝 x] 1 /-- We say that a collection of functions form a smooth partition of unity on a set `s` if * all functions are infinitely smooth and nonnegative; * the family `fun i ↦ support (f i)` is locally finite; * for all `x ∈ s` the sum `∑ᶠ i, f i x` equals one; * for all `x`, the sum `∑ᶠ i, f i x` is less than or equal to one. -/ structure SmoothPartitionOfUnity (s : Set M := univ) where /-- The family of functions forming the partition of unity. -/ toFun : ι → C^∞⟮I, M; 𝓘(ℝ), ℝ⟯ /-- Around each point, there are only finitely many nonzero functions in the family. -/ locallyFinite' : LocallyFinite fun i => support (toFun i) /-- All the functions in the partition of unity are nonnegative. -/ nonneg' : ∀ i x, 0 ≤ toFun i x /-- The functions in the partition of unity add up to `1` at any point of `s`. -/ sum_eq_one' : ∀ x ∈ s, ∑ᶠ i, toFun i x = 1 /-- The functions in the partition of unity add up to at most `1` everywhere. -/ sum_le_one' : ∀ x, ∑ᶠ i, toFun i x ≤ 1 variable {ι I M} namespace SmoothPartitionOfUnity variable {s : Set M} (f : SmoothPartitionOfUnity ι I M s) {n : ℕ∞} instance {s : Set M} : FunLike (SmoothPartitionOfUnity ι I M s) ι C^∞⟮I, M; 𝓘(ℝ), ℝ⟯ where coe := toFun coe_injective' f g h := by cases f; cases g; congr protected theorem locallyFinite : LocallyFinite fun i => support (f i) := f.locallyFinite' theorem nonneg (i : ι) (x : M) : 0 ≤ f i x := f.nonneg' i x theorem sum_eq_one {x} (hx : x ∈ s) : ∑ᶠ i, f i x = 1 := f.sum_eq_one' x hx theorem exists_pos_of_mem {x} (hx : x ∈ s) : ∃ i, 0 < f i x := by by_contra! h have H : ∀ i, f i x = 0 := fun i ↦ le_antisymm (h i) (f.nonneg i x) have := f.sum_eq_one hx simp_rw [H] at this simpa theorem sum_le_one (x : M) : ∑ᶠ i, f i x ≤ 1 := f.sum_le_one' x /-- Reinterpret a smooth partition of unity as a continuous partition of unity. -/ @[simps] def toPartitionOfUnity : PartitionOfUnity ι M s := { f with toFun := fun i => f i } theorem contMDiff_sum : ContMDiff I 𝓘(ℝ) ∞ fun x => ∑ᶠ i, f i x := contMDiff_finsum (fun i => (f i).contMDiff) f.locallyFinite @[deprecated (since := "2024-11-21")] alias smooth_sum := contMDiff_sum theorem le_one (i : ι) (x : M) : f i x ≤ 1 := f.toPartitionOfUnity.le_one i x theorem sum_nonneg (x : M) : 0 ≤ ∑ᶠ i, f i x := f.toPartitionOfUnity.sum_nonneg x theorem finsum_smul_mem_convex {g : ι → M → F} {t : Set F} {x : M} (hx : x ∈ s) (hg : ∀ i, f i x ≠ 0 → g i x ∈ t) (ht : Convex ℝ t) : ∑ᶠ i, f i x • g i x ∈ t := ht.finsum_mem (fun _ => f.nonneg _ _) (f.sum_eq_one hx) hg theorem contMDiff_smul {g : M → F} {i} (hg : ∀ x ∈ tsupport (f i), ContMDiffAt I 𝓘(ℝ, F) n g x) : ContMDiff I 𝓘(ℝ, F) n fun x => f i x • g x := contMDiff_of_tsupport fun x hx => ((f i).contMDiff.contMDiffAt.of_le (mod_cast le_top)).smul <| hg x <| tsupport_smul_subset_left _ _ hx @[deprecated (since := "2024-11-21")] alias smooth_smul := contMDiff_smul /-- If `f` is a smooth partition of unity on a set `s : Set M` and `g : ι → M → F` is a family of functions such that `g i` is $C^n$ smooth at every point of the topological support of `f i`, then the sum `fun x ↦ ∑ᶠ i, f i x • g i x` is smooth on the whole manifold. -/ theorem contMDiff_finsum_smul {g : ι → M → F} (hg : ∀ (i), ∀ x ∈ tsupport (f i), ContMDiffAt I 𝓘(ℝ, F) n (g i) x) : ContMDiff I 𝓘(ℝ, F) n fun x => ∑ᶠ i, f i x • g i x := (contMDiff_finsum fun i => f.contMDiff_smul (hg i)) <| f.locallyFinite.subset fun _ => support_smul_subset_left _ _ @[deprecated (since := "2024-11-21")] alias smooth_finsum_smul := contMDiff_finsum_smul theorem contMDiffAt_finsum {x₀ : M} {g : ι → M → F} (hφ : ∀ i, x₀ ∈ tsupport (f i) → ContMDiffAt I 𝓘(ℝ, F) n (g i) x₀) : ContMDiffAt I 𝓘(ℝ, F) n (fun x ↦ ∑ᶠ i, f i x • g i x) x₀ := by refine _root_.contMDiffAt_finsum (f.locallyFinite.smul_left _) fun i ↦ ?_ by_cases hx : x₀ ∈ tsupport (f i) · exact ContMDiffAt.smul ((f i).contMDiff.of_le (mod_cast le_top)).contMDiffAt (hφ i hx) · exact contMDiffAt_of_not_mem (compl_subset_compl.mpr (tsupport_smul_subset_left (f i) (g i)) hx) n theorem contDiffAt_finsum {s : Set E} (f : SmoothPartitionOfUnity ι 𝓘(ℝ, E) E s) {x₀ : E} {g : ι → E → F} (hφ : ∀ i, x₀ ∈ tsupport (f i) → ContDiffAt ℝ n (g i) x₀) : ContDiffAt ℝ n (fun x ↦ ∑ᶠ i, f i x • g i x) x₀ := by simp only [← contMDiffAt_iff_contDiffAt] at * exact f.contMDiffAt_finsum hφ section finsupport variable {s : Set M} (ρ : SmoothPartitionOfUnity ι I M s) (x₀ : M) /-- The support of a smooth partition of unity at a point `x₀` as a `Finset`. This is the set of `i : ι` such that `x₀ ∈ support f i`, i.e. `f i ≠ x₀`. -/ def finsupport : Finset ι := ρ.toPartitionOfUnity.finsupport x₀ @[simp] theorem mem_finsupport {i : ι} : i ∈ ρ.finsupport x₀ ↔ i ∈ support fun i ↦ ρ i x₀ := ρ.toPartitionOfUnity.mem_finsupport x₀ @[simp] theorem coe_finsupport : (ρ.finsupport x₀ : Set ι) = support fun i ↦ ρ i x₀ := ρ.toPartitionOfUnity.coe_finsupport x₀ theorem sum_finsupport (hx₀ : x₀ ∈ s) : ∑ i ∈ ρ.finsupport x₀, ρ i x₀ = 1 := ρ.toPartitionOfUnity.sum_finsupport hx₀ theorem sum_finsupport' (hx₀ : x₀ ∈ s) {I : Finset ι} (hI : ρ.finsupport x₀ ⊆ I) : ∑ i ∈ I, ρ i x₀ = 1 := ρ.toPartitionOfUnity.sum_finsupport' hx₀ hI theorem sum_finsupport_smul_eq_finsum {A : Type*} [AddCommGroup A] [Module ℝ A] (φ : ι → M → A) : ∑ i ∈ ρ.finsupport x₀, ρ i x₀ • φ i x₀ = ∑ᶠ i, ρ i x₀ • φ i x₀ := ρ.toPartitionOfUnity.sum_finsupport_smul_eq_finsum φ end finsupport section fintsupport -- smooth partitions of unity have locally finite `tsupport` variable {s : Set M} (ρ : SmoothPartitionOfUnity ι I M s) (x₀ : M) /-- The `tsupport`s of a smooth partition of unity are locally finite. -/ theorem finite_tsupport : {i | x₀ ∈ tsupport (ρ i)}.Finite := ρ.toPartitionOfUnity.finite_tsupport _ /-- The tsupport of a partition of unity at a point `x₀` as a `Finset`. This is the set of `i : ι` such that `x₀ ∈ tsupport f i`. -/ def fintsupport (x : M) : Finset ι := (ρ.finite_tsupport x).toFinset theorem mem_fintsupport_iff (i : ι) : i ∈ ρ.fintsupport x₀ ↔ x₀ ∈ tsupport (ρ i) := Finite.mem_toFinset _ theorem eventually_fintsupport_subset : ∀ᶠ y in 𝓝 x₀, ρ.fintsupport y ⊆ ρ.fintsupport x₀ := ρ.toPartitionOfUnity.eventually_fintsupport_subset _ theorem finsupport_subset_fintsupport : ρ.finsupport x₀ ⊆ ρ.fintsupport x₀ := ρ.toPartitionOfUnity.finsupport_subset_fintsupport x₀ theorem eventually_finsupport_subset : ∀ᶠ y in 𝓝 x₀, ρ.finsupport y ⊆ ρ.fintsupport x₀ := ρ.toPartitionOfUnity.eventually_finsupport_subset x₀ end fintsupport section IsSubordinate /-- A smooth partition of unity `f i` is subordinate to a family of sets `U i` indexed by the same type if for each `i` the closure of the support of `f i` is a subset of `U i`. -/ def IsSubordinate (f : SmoothPartitionOfUnity ι I M s) (U : ι → Set M) := ∀ i, tsupport (f i) ⊆ U i variable {f} variable {U : ι → Set M} @[simp] theorem isSubordinate_toPartitionOfUnity : f.toPartitionOfUnity.IsSubordinate U ↔ f.IsSubordinate U := Iff.rfl alias ⟨_, IsSubordinate.toPartitionOfUnity⟩ := isSubordinate_toPartitionOfUnity /-- If `f` is a smooth partition of unity on a set `s : Set M` subordinate to a family of open sets `U : ι → Set M` and `g : ι → M → F` is a family of functions such that `g i` is $C^n$ smooth on `U i`, then the sum `fun x ↦ ∑ᶠ i, f i x • g i x` is $C^n$ smooth on the whole manifold. -/ theorem IsSubordinate.contMDiff_finsum_smul {g : ι → M → F} (hf : f.IsSubordinate U) (ho : ∀ i, IsOpen (U i)) (hg : ∀ i, ContMDiffOn I 𝓘(ℝ, F) n (g i) (U i)) : ContMDiff I 𝓘(ℝ, F) n fun x => ∑ᶠ i, f i x • g i x := f.contMDiff_finsum_smul fun i _ hx => (hg i).contMDiffAt <| (ho i).mem_nhds (hf i hx) @[deprecated (since := "2024-11-21")] alias IsSubordinate.smooth_finsum_smul := IsSubordinate.contMDiff_finsum_smul end IsSubordinate end SmoothPartitionOfUnity namespace BumpCovering -- Repeat variables to drop `[FiniteDimensional ℝ E]` and `[IsManifold I ∞ M]` theorem contMDiff_toPartitionOfUnity {E : Type uE} [NormedAddCommGroup E] [NormedSpace ℝ E] {H : Type uH} [TopologicalSpace H] {I : ModelWithCorners ℝ E H} {M : Type uM} [TopologicalSpace M] [ChartedSpace H M] {s : Set M} (f : BumpCovering ι M s) (hf : ∀ i, ContMDiff I 𝓘(ℝ) ∞ (f i)) (i : ι) : ContMDiff I 𝓘(ℝ) ∞ (f.toPartitionOfUnity i) := (hf i).mul <| (contMDiff_finprod_cond fun j _ => contMDiff_const.sub (hf j)) <| by simp only [Pi.sub_def, mulSupport_one_sub] exact f.locallyFinite @[deprecated (since := "2024-11-21")] alias smooth_toPartitionOfUnity := contMDiff_toPartitionOfUnity variable {s : Set M} /-- A `BumpCovering` such that all functions in this covering are smooth generates a smooth partition of unity. In our formalization, not every `f : BumpCovering ι M s` with smooth functions `f i` is a `SmoothBumpCovering`; instead, a `SmoothBumpCovering` is a covering by supports of `SmoothBumpFunction`s. So, we define `BumpCovering.toSmoothPartitionOfUnity`, then reuse it in `SmoothBumpCovering.toSmoothPartitionOfUnity`. -/ def toSmoothPartitionOfUnity (f : BumpCovering ι M s) (hf : ∀ i, ContMDiff I 𝓘(ℝ) ∞ (f i)) : SmoothPartitionOfUnity ι I M s := { f.toPartitionOfUnity with toFun := fun i => ⟨f.toPartitionOfUnity i, f.contMDiff_toPartitionOfUnity hf i⟩ } @[simp] theorem toSmoothPartitionOfUnity_toPartitionOfUnity (f : BumpCovering ι M s) (hf : ∀ i, ContMDiff I 𝓘(ℝ) ∞ (f i)) : (f.toSmoothPartitionOfUnity hf).toPartitionOfUnity = f.toPartitionOfUnity := rfl @[simp] theorem coe_toSmoothPartitionOfUnity (f : BumpCovering ι M s) (hf : ∀ i, ContMDiff I 𝓘(ℝ) ∞ (f i)) (i : ι) : ⇑(f.toSmoothPartitionOfUnity hf i) = f.toPartitionOfUnity i := rfl theorem IsSubordinate.toSmoothPartitionOfUnity {f : BumpCovering ι M s} {U : ι → Set M} (h : f.IsSubordinate U) (hf : ∀ i, ContMDiff I 𝓘(ℝ) ∞ (f i)) : (f.toSmoothPartitionOfUnity hf).IsSubordinate U := h.toPartitionOfUnity end BumpCovering namespace SmoothBumpCovering variable [FiniteDimensional ℝ E] variable {s : Set M} {U : M → Set M} (fs : SmoothBumpCovering ι I M s) instance : CoeFun (SmoothBumpCovering ι I M s) fun x => ∀ i : ι, SmoothBumpFunction I (x.c i) := ⟨toFun⟩ /-- We say that `f : SmoothBumpCovering ι I M s` is *subordinate* to a map `U : M → Set M` if for each index `i`, we have `tsupport (f i) ⊆ U (f i).c`. This notion is a bit more general than being subordinate to an open covering of `M`, because we make no assumption about the way `U x` depends on `x`. -/ def IsSubordinate {s : Set M} (f : SmoothBumpCovering ι I M s) (U : M → Set M) := ∀ i, tsupport (f i) ⊆ U (f.c i) theorem IsSubordinate.support_subset {fs : SmoothBumpCovering ι I M s} {U : M → Set M} (h : fs.IsSubordinate U) (i : ι) : support (fs i) ⊆ U (fs.c i) := Subset.trans subset_closure (h i) variable (I) in /-- Let `M` be a smooth manifold modelled on a finite dimensional real vector space. Suppose also that `M` is a Hausdorff `σ`-compact topological space. Let `s` be a closed set in `M` and `U : M → Set M` be a collection of sets such that `U x ∈ 𝓝 x` for every `x ∈ s`. Then there exists a smooth bump covering of `s` that is subordinate to `U`. -/ theorem exists_isSubordinate [T2Space M] [SigmaCompactSpace M] (hs : IsClosed s) (hU : ∀ x ∈ s, U x ∈ 𝓝 x) : ∃ (ι : Type uM) (f : SmoothBumpCovering ι I M s), f.IsSubordinate U := by -- First we deduce some missing instances haveI : LocallyCompactSpace H := I.locallyCompactSpace haveI : LocallyCompactSpace M := ChartedSpace.locallyCompactSpace H M -- Next we choose a covering by supports of smooth bump functions have hB := fun x hx => SmoothBumpFunction.nhds_basis_support (I := I) (hU x hx) rcases refinement_of_locallyCompact_sigmaCompact_of_nhds_basis_set hs hB with ⟨ι, c, f, hf, hsub', hfin⟩ choose hcs hfU using hf -- Then we use the shrinking lemma to get a covering by smaller open rcases exists_subset_iUnion_closed_subset hs (fun i => (f i).isOpen_support) (fun x _ => hfin.point_finite x) hsub' with ⟨V, hsV, hVc, hVf⟩ choose r hrR hr using fun i => (f i).exists_r_pos_lt_subset_ball (hVc i) (hVf i) refine ⟨ι, ⟨c, fun i => (f i).updateRIn (r i) (hrR i), hcs, ?_, fun x hx => ?_⟩, fun i => ?_⟩ · simpa only [SmoothBumpFunction.support_updateRIn] · refine (mem_iUnion.1 <| hsV hx).imp fun i hi => ?_ exact ((f i).updateRIn _ _).eventuallyEq_one_of_dist_lt ((f i).support_subset_source <| hVf _ hi) (hr i hi).2 · simpa only [SmoothBumpFunction.support_updateRIn, tsupport] using hfU i protected theorem locallyFinite : LocallyFinite fun i => support (fs i) := fs.locallyFinite' protected theorem point_finite (x : M) : {i | fs i x ≠ 0}.Finite := fs.locallyFinite.point_finite x /-- Index of a bump function such that `fs i =ᶠ[𝓝 x] 1`. -/ def ind (x : M) (hx : x ∈ s) : ι := (fs.eventuallyEq_one' x hx).choose theorem eventuallyEq_one (x : M) (hx : x ∈ s) : fs (fs.ind x hx) =ᶠ[𝓝 x] 1 := (fs.eventuallyEq_one' x hx).choose_spec theorem apply_ind (x : M) (hx : x ∈ s) : fs (fs.ind x hx) x = 1 := (fs.eventuallyEq_one x hx).eq_of_nhds theorem mem_support_ind (x : M) (hx : x ∈ s) : x ∈ support (fs <| fs.ind x hx) := by simp [fs.apply_ind x hx] theorem mem_chartAt_source_of_eq_one {i : ι} {x : M} (h : fs i x = 1) : x ∈ (chartAt H (fs.c i)).source := (fs i).support_subset_source <| by simp [h] theorem mem_extChartAt_source_of_eq_one {i : ι} {x : M} (h : fs i x = 1) : x ∈ (extChartAt I (fs.c i)).source := by rw [extChartAt_source]; exact fs.mem_chartAt_source_of_eq_one h theorem mem_chartAt_ind_source (x : M) (hx : x ∈ s) : x ∈ (chartAt H (fs.c (fs.ind x hx))).source := fs.mem_chartAt_source_of_eq_one (fs.apply_ind x hx) theorem mem_extChartAt_ind_source (x : M) (hx : x ∈ s) : x ∈ (extChartAt I (fs.c (fs.ind x hx))).source := fs.mem_extChartAt_source_of_eq_one (fs.apply_ind x hx) /-- The index type of a `SmoothBumpCovering` of a compact manifold is finite. -/ protected def fintype [CompactSpace M] : Fintype ι := fs.locallyFinite.fintypeOfCompact fun i => (fs i).nonempty_support variable [T2Space M] variable [IsManifold I ∞ M] /-- Reinterpret a `SmoothBumpCovering` as a continuous `BumpCovering`. Note that not every `f : BumpCovering ι M s` with smooth functions `f i` is a `SmoothBumpCovering`. -/ def toBumpCovering : BumpCovering ι M s where toFun i := ⟨fs i, (fs i).continuous⟩ locallyFinite' := fs.locallyFinite nonneg' i _ := (fs i).nonneg le_one' i _ := (fs i).le_one eventuallyEq_one' := fs.eventuallyEq_one' @[simp] theorem isSubordinate_toBumpCovering {f : SmoothBumpCovering ι I M s} {U : M → Set M} : (f.toBumpCovering.IsSubordinate fun i => U (f.c i)) ↔ f.IsSubordinate U := Iff.rfl alias ⟨_, IsSubordinate.toBumpCovering⟩ := isSubordinate_toBumpCovering /-- Every `SmoothBumpCovering` defines a smooth partition of unity. -/ def toSmoothPartitionOfUnity : SmoothPartitionOfUnity ι I M s := fs.toBumpCovering.toSmoothPartitionOfUnity fun i => (fs i).contMDiff theorem toSmoothPartitionOfUnity_apply (i : ι) (x : M) : fs.toSmoothPartitionOfUnity i x = fs i x * ∏ᶠ (j) (_ : WellOrderingRel j i), (1 - fs j x) := rfl open Classical in theorem toSmoothPartitionOfUnity_eq_mul_prod (i : ι) (x : M) (t : Finset ι) (ht : ∀ j, WellOrderingRel j i → fs j x ≠ 0 → j ∈ t) : fs.toSmoothPartitionOfUnity i x = fs i x * ∏ j ∈ t with WellOrderingRel j i, (1 - fs j x) := fs.toBumpCovering.toPartitionOfUnity_eq_mul_prod i x t ht open Classical in theorem exists_finset_toSmoothPartitionOfUnity_eventuallyEq (i : ι) (x : M) : ∃ t : Finset ι, fs.toSmoothPartitionOfUnity i =ᶠ[𝓝 x] fs i * ∏ j ∈ t with WellOrderingRel j i, ((1 : M → ℝ) - fs j) := by -- Porting note: was defeq, now the continuous lemma uses bundled homs simpa using fs.toBumpCovering.exists_finset_toPartitionOfUnity_eventuallyEq i x theorem toSmoothPartitionOfUnity_zero_of_zero {i : ι} {x : M} (h : fs i x = 0) : fs.toSmoothPartitionOfUnity i x = 0 := fs.toBumpCovering.toPartitionOfUnity_zero_of_zero h theorem support_toSmoothPartitionOfUnity_subset (i : ι) : support (fs.toSmoothPartitionOfUnity i) ⊆ support (fs i) := fs.toBumpCovering.support_toPartitionOfUnity_subset i theorem IsSubordinate.toSmoothPartitionOfUnity {f : SmoothBumpCovering ι I M s} {U : M → Set M} (h : f.IsSubordinate U) : f.toSmoothPartitionOfUnity.IsSubordinate fun i => U (f.c i) := h.toBumpCovering.toPartitionOfUnity theorem sum_toSmoothPartitionOfUnity_eq (x : M) : ∑ᶠ i, fs.toSmoothPartitionOfUnity i x = 1 - ∏ᶠ i, (1 - fs i x) := fs.toBumpCovering.sum_toPartitionOfUnity_eq x end SmoothBumpCovering variable (I) variable [FiniteDimensional ℝ E] variable [IsManifold I ∞ M] /-- Given two disjoint closed sets `s, t` in a Hausdorff σ-compact finite dimensional manifold, there exists an infinitely smooth function that is equal to `0` on `s` and to `1` on `t`. See also `exists_msmooth_zero_iff_one_iff_of_isClosed`, which ensures additionally that `f` is equal to `0` exactly on `s` and to `1` exactly on `t`. -/ theorem exists_smooth_zero_one_of_isClosed [T2Space M] [SigmaCompactSpace M] {s t : Set M} (hs : IsClosed s) (ht : IsClosed t) (hd : Disjoint s t) : ∃ f : C^∞⟮I, M; 𝓘(ℝ), ℝ⟯, EqOn f 0 s ∧ EqOn f 1 t ∧ ∀ x, f x ∈ Icc 0 1 := by have : ∀ x ∈ t, sᶜ ∈ 𝓝 x := fun x hx => hs.isOpen_compl.mem_nhds (disjoint_right.1 hd hx) rcases SmoothBumpCovering.exists_isSubordinate I ht this with ⟨ι, f, hf⟩ set g := f.toSmoothPartitionOfUnity refine ⟨⟨_, g.contMDiff_sum⟩, fun x hx => ?_, fun x => g.sum_eq_one, fun x => ⟨g.sum_nonneg x, g.sum_le_one x⟩⟩ suffices ∀ i, g i x = 0 by simp only [this, ContMDiffMap.coeFn_mk, finsum_zero, Pi.zero_apply] refine fun i => f.toSmoothPartitionOfUnity_zero_of_zero ?_ exact nmem_support.1 (subset_compl_comm.1 (hf.support_subset i) hx) /-- Given two disjoint closed sets `s, t` in a Hausdorff normal σ-compact finite dimensional manifold `M`, there exists a smooth function `f : M → [0,1]` that vanishes in a neighbourhood of `s` and is equal to `1` in a neighbourhood of `t`. -/ theorem exists_smooth_zero_one_nhds_of_isClosed [T2Space M] [NormalSpace M] [SigmaCompactSpace M] {s t : Set M} (hs : IsClosed s) (ht : IsClosed t) (hd : Disjoint s t) : ∃ f : C^∞⟮I, M; 𝓘(ℝ), ℝ⟯, (∀ᶠ x in 𝓝ˢ s, f x = 0) ∧ (∀ᶠ x in 𝓝ˢ t, f x = 1) ∧ ∀ x, f x ∈ Icc 0 1 := by obtain ⟨u, u_op, hsu, hut⟩ := normal_exists_closure_subset hs ht.isOpen_compl (subset_compl_iff_disjoint_left.mpr hd.symm) obtain ⟨v, v_op, htv, hvu⟩ := normal_exists_closure_subset ht isClosed_closure.isOpen_compl (subset_compl_comm.mp hut) obtain ⟨f, hfu, hfv, hf⟩ := exists_smooth_zero_one_of_isClosed I isClosed_closure isClosed_closure (subset_compl_iff_disjoint_left.mp hvu) refine ⟨f, ?_, ?_, hf⟩ · exact eventually_of_mem (mem_of_superset (u_op.mem_nhdsSet.mpr hsu) subset_closure) hfu · exact eventually_of_mem (mem_of_superset (v_op.mem_nhdsSet.mpr htv) subset_closure) hfv /-- Given two sets `s, t` in a Hausdorff normal σ-compact finite-dimensional manifold `M` with `s` open and `s ⊆ interior t`, there is a smooth function `f : M → [0,1]` which is equal to `s` in a neighbourhood of `s` and has support contained in `t`. -/ theorem exists_smooth_one_nhds_of_subset_interior [T2Space M] [NormalSpace M] [SigmaCompactSpace M] {s t : Set M} (hs : IsClosed s) (hd : s ⊆ interior t) : ∃ f : C^∞⟮I, M; 𝓘(ℝ), ℝ⟯, (∀ᶠ x in 𝓝ˢ s, f x = 1) ∧ (∀ x ∉ t, f x = 0) ∧ ∀ x, f x ∈ Icc 0 1 := by rcases exists_smooth_zero_one_nhds_of_isClosed I isOpen_interior.isClosed_compl hs (by rwa [← subset_compl_iff_disjoint_left, compl_compl]) with ⟨f, h0, h1, hf⟩ refine ⟨f, h1, fun x hx ↦ ?_, hf⟩ exact h0.self_of_nhdsSet _ fun hx' ↦ hx <| interior_subset hx' namespace SmoothPartitionOfUnity /-- A `SmoothPartitionOfUnity` that consists of a single function, uniformly equal to one, defined as an example for `Inhabited` instance. -/ def single (i : ι) (s : Set M) : SmoothPartitionOfUnity ι I M s := (BumpCovering.single i s).toSmoothPartitionOfUnity fun j => by classical rcases eq_or_ne j i with (rfl | h) · simp only [contMDiff_one, ContinuousMap.coe_one, BumpCovering.coe_single, Pi.single_eq_same] · simp only [contMDiff_zero, BumpCovering.coe_single, Pi.single_eq_of_ne h, ContinuousMap.coe_zero] instance [Inhabited ι] (s : Set M) : Inhabited (SmoothPartitionOfUnity ι I M s) := ⟨single I default s⟩ variable [T2Space M] [SigmaCompactSpace M] /-- If `X` is a paracompact normal topological space and `U` is an open covering of a closed set `s`, then there exists a `SmoothPartitionOfUnity ι M s` that is subordinate to `U`. -/ theorem exists_isSubordinate {s : Set M} (hs : IsClosed s) (U : ι → Set M) (ho : ∀ i, IsOpen (U i))
(hU : s ⊆ ⋃ i, U i) : ∃ f : SmoothPartitionOfUnity ι I M s, f.IsSubordinate U := by haveI : LocallyCompactSpace H := I.locallyCompactSpace haveI : LocallyCompactSpace M := ChartedSpace.locallyCompactSpace H M -- porting note(https://github.com/leanprover/std4/issues/116): -- split `rcases` into `have` + `rcases` have := BumpCovering.exists_isSubordinate_of_prop (ContMDiff I 𝓘(ℝ) ∞) ?_ hs U ho hU · rcases this with ⟨f, hf, hfU⟩ exact ⟨f.toSmoothPartitionOfUnity hf, hfU.toSmoothPartitionOfUnity hf⟩ · intro s t hs ht hd rcases exists_smooth_zero_one_of_isClosed I hs ht hd with ⟨f, hf⟩ exact ⟨f, f.contMDiff, hf⟩
Mathlib/Geometry/Manifold/PartitionOfUnity.lean
559
570
/- Copyright (c) 2023 Joël Riou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joël Riou -/ import Mathlib.Algebra.Homology.HomotopyCategory.HomComplex import Mathlib.Algebra.Homology.HomotopyCofiber /-! # The mapping cone of a morphism of cochain complexes In this file, we study the homotopy cofiber `HomologicalComplex.homotopyCofiber` of a morphism `φ : F ⟶ G` of cochain complexes indexed by `ℤ`. In this case, we redefine it as `CochainComplex.mappingCone φ`. The API involves definitions - `mappingCone.inl φ : Cochain F (mappingCone φ) (-1)`, - `mappingCone.inr φ : G ⟶ mappingCone φ`, - `mappingCone.fst φ : Cocycle (mappingCone φ) F 1` and - `mappingCone.snd φ : Cochain (mappingCone φ) G 0`. -/ assert_not_exists TwoSidedIdeal open CategoryTheory Limits variable {C D : Type*} [Category C] [Category D] [Preadditive C] [Preadditive D] namespace CochainComplex open HomologicalComplex section variable {ι : Type*} [AddRightCancelSemigroup ι] [One ι] {F G : CochainComplex C ι} (φ : F ⟶ G) instance [∀ p, HasBinaryBiproduct (F.X (p + 1)) (G.X p)] : HasHomotopyCofiber φ where hasBinaryBiproduct := by rintro i _ rfl infer_instance end variable {F G : CochainComplex C ℤ} (φ : F ⟶ G) variable [HasHomotopyCofiber φ] /-- The mapping cone of a morphism of cochain complexes indexed by `ℤ`. -/ noncomputable def mappingCone := homotopyCofiber φ namespace mappingCone open HomComplex /-- The left inclusion in the mapping cone, as a cochain of degree `-1`. -/ noncomputable def inl : Cochain F (mappingCone φ) (-1) := Cochain.mk (fun p q hpq => homotopyCofiber.inlX φ p q (by dsimp; omega)) /-- The right inclusion in the mapping cone. -/ noncomputable def inr : G ⟶ mappingCone φ := homotopyCofiber.inr φ /-- The first projection from the mapping cone, as a cocyle of degree `1`. -/ noncomputable def fst : Cocycle (mappingCone φ) F 1 := Cocycle.mk (Cochain.mk (fun p q hpq => homotopyCofiber.fstX φ p q hpq)) 2 (by omega) (by ext p _ rfl simp [δ_v 1 2 (by omega) _ p (p + 2) (by omega) (p + 1) (p + 1) (by omega) rfl, homotopyCofiber.d_fstX φ p (p + 1) (p + 2) rfl, mappingCone, show Int.negOnePow 2 = 1 by rfl]) /-- The second projection from the mapping cone, as a cochain of degree `0`. -/ noncomputable def snd : Cochain (mappingCone φ) G 0 := Cochain.ofHoms (homotopyCofiber.sndX φ) @[reassoc (attr := simp)] lemma inl_v_fst_v (p q : ℤ) (hpq : q + 1 = p) : (inl φ).v p q (by rw [← hpq, add_neg_cancel_right]) ≫ (fst φ : Cochain (mappingCone φ) F 1).v q p hpq = 𝟙 _ := by simp [inl, fst] @[reassoc (attr := simp)] lemma inl_v_snd_v (p q : ℤ) (hpq : p + (-1) = q) : (inl φ).v p q hpq ≫ (snd φ).v q q (add_zero q) = 0 := by simp [inl, snd] @[reassoc (attr := simp)] lemma inr_f_fst_v (p q : ℤ) (hpq : p + 1 = q) : (inr φ).f p ≫ (fst φ).1.v p q hpq = 0 := by simp [inr, fst] @[reassoc (attr := simp)] lemma inr_f_snd_v (p : ℤ) : (inr φ).f p ≫ (snd φ).v p p (add_zero p) = 𝟙 _ := by simp [inr, snd] @[simp] lemma inl_fst : (inl φ).comp (fst φ).1 (neg_add_cancel 1) = Cochain.ofHom (𝟙 F) := by ext p simp [Cochain.comp_v _ _ (neg_add_cancel 1) p (p-1) p rfl (by omega)] @[simp] lemma inl_snd : (inl φ).comp (snd φ) (add_zero (-1)) = 0 := by ext p q hpq simp [Cochain.comp_v _ _ (add_zero (-1)) p q q (by omega) (by omega)] @[simp] lemma inr_fst : (Cochain.ofHom (inr φ)).comp (fst φ).1 (zero_add 1) = 0 := by ext p q hpq simp [Cochain.comp_v _ _ (zero_add 1) p p q (by omega) (by omega)] @[simp] lemma inr_snd : (Cochain.ofHom (inr φ)).comp (snd φ) (zero_add 0) = Cochain.ofHom (𝟙 G) := by aesop_cat /-! In order to obtain identities of cochains involving `inl`, `inr`, `fst` and `snd`, it is often convenient to use an `ext` lemma, and use simp lemmas like `inl_v_f_fst_v`, but it is sometimes possible to get identities of cochains by using rewrites of identities of cochains like `inl_fst`. Then, similarly as in category theory, if we associate the compositions of cochains to the right as much as possible, it is also interesting to have `reassoc` variants of lemmas, like `inl_fst_assoc`. -/ @[simp] lemma inl_fst_assoc {K : CochainComplex C ℤ} {d e : ℤ} (γ : Cochain F K d) (he : 1 + d = e) : (inl φ).comp ((fst φ).1.comp γ he) (by rw [← he, neg_add_cancel_left]) = γ := by rw [← Cochain.comp_assoc _ _ _ (neg_add_cancel 1) (by omega) (by omega), inl_fst, Cochain.id_comp] @[simp] lemma inl_snd_assoc {K : CochainComplex C ℤ} {d e f : ℤ} (γ : Cochain G K d) (he : 0 + d = e) (hf : -1 + e = f) : (inl φ).comp ((snd φ).comp γ he) hf = 0 := by obtain rfl : e = d := by omega rw [← Cochain.comp_assoc_of_second_is_zero_cochain, inl_snd, Cochain.zero_comp] @[simp] lemma inr_fst_assoc {K : CochainComplex C ℤ} {d e f : ℤ} (γ : Cochain F K d) (he : 1 + d = e) (hf : 0 + e = f) : (Cochain.ofHom (inr φ)).comp ((fst φ).1.comp γ he) hf = 0 := by obtain rfl : e = f := by omega rw [← Cochain.comp_assoc_of_first_is_zero_cochain, inr_fst, Cochain.zero_comp] @[simp] lemma inr_snd_assoc {K : CochainComplex C ℤ} {d e : ℤ} (γ : Cochain G K d) (he : 0 + d = e) : (Cochain.ofHom (inr φ)).comp ((snd φ).comp γ he) (by simp only [← he, zero_add]) = γ := by obtain rfl : d = e := by omega rw [← Cochain.comp_assoc_of_first_is_zero_cochain, inr_snd, Cochain.id_comp] lemma ext_to (i j : ℤ) (hij : i + 1 = j) {A : C} {f g : A ⟶ (mappingCone φ).X i} (h₁ : f ≫ (fst φ).1.v i j hij = g ≫ (fst φ).1.v i j hij) (h₂ : f ≫ (snd φ).v i i (add_zero i) = g ≫ (snd φ).v i i (add_zero i)) : f = g := homotopyCofiber.ext_to_X φ i j hij h₁ (by simpa [snd] using h₂) lemma ext_to_iff (i j : ℤ) (hij : i + 1 = j) {A : C} (f g : A ⟶ (mappingCone φ).X i) : f = g ↔ f ≫ (fst φ).1.v i j hij = g ≫ (fst φ).1.v i j hij ∧ f ≫ (snd φ).v i i (add_zero i) = g ≫ (snd φ).v i i (add_zero i) := by constructor · rintro rfl tauto · rintro ⟨h₁, h₂⟩ exact ext_to φ i j hij h₁ h₂ lemma ext_from (i j : ℤ) (hij : j + 1 = i) {A : C} {f g : (mappingCone φ).X j ⟶ A} (h₁ : (inl φ).v i j (by omega) ≫ f = (inl φ).v i j (by omega) ≫ g) (h₂ : (inr φ).f j ≫ f = (inr φ).f j ≫ g) : f = g := homotopyCofiber.ext_from_X φ i j hij h₁ h₂ lemma ext_from_iff (i j : ℤ) (hij : j + 1 = i) {A : C} (f g : (mappingCone φ).X j ⟶ A) : f = g ↔ (inl φ).v i j (by omega) ≫ f = (inl φ).v i j (by omega) ≫ g ∧ (inr φ).f j ≫ f = (inr φ).f j ≫ g := by constructor · rintro rfl tauto · rintro ⟨h₁, h₂⟩ exact ext_from φ i j hij h₁ h₂ lemma decomp_to {i : ℤ} {A : C} (f : A ⟶ (mappingCone φ).X i) (j : ℤ) (hij : i + 1 = j) : ∃ (a : A ⟶ F.X j) (b : A ⟶ G.X i), f = a ≫ (inl φ).v j i (by omega) + b ≫ (inr φ).f i := ⟨f ≫ (fst φ).1.v i j hij, f ≫ (snd φ).v i i (add_zero i), by apply ext_to φ i j hij <;> simp⟩ lemma decomp_from {j : ℤ} {A : C} (f : (mappingCone φ).X j ⟶ A) (i : ℤ) (hij : j + 1 = i) : ∃ (a : F.X i ⟶ A) (b : G.X j ⟶ A), f = (fst φ).1.v j i hij ≫ a + (snd φ).v j j (add_zero j) ≫ b := ⟨(inl φ).v i j (by omega) ≫ f, (inr φ).f j ≫ f, by apply ext_from φ i j hij <;> simp⟩ lemma ext_cochain_to_iff (i j : ℤ) (hij : i + 1 = j) {K : CochainComplex C ℤ} {γ₁ γ₂ : Cochain K (mappingCone φ) i} : γ₁ = γ₂ ↔ γ₁.comp (fst φ).1 hij = γ₂.comp (fst φ).1 hij ∧ γ₁.comp (snd φ) (add_zero i) = γ₂.comp (snd φ) (add_zero i) := by constructor · rintro rfl tauto · rintro ⟨h₁, h₂⟩ ext p q hpq rw [ext_to_iff φ q (q + 1) rfl] replace h₁ := Cochain.congr_v h₁ p (q + 1) (by omega) replace h₂ := Cochain.congr_v h₂ p q hpq simp only [Cochain.comp_v _ _ _ p q (q + 1) hpq rfl] at h₁ simp only [Cochain.comp_zero_cochain_v] at h₂ exact ⟨h₁, h₂⟩ lemma ext_cochain_from_iff (i j : ℤ) (hij : i + 1 = j) {K : CochainComplex C ℤ} {γ₁ γ₂ : Cochain (mappingCone φ) K j} : γ₁ = γ₂ ↔ (inl φ).comp γ₁ (show _ = i by omega) = (inl φ).comp γ₂ (by omega) ∧ (Cochain.ofHom (inr φ)).comp γ₁ (zero_add j) = (Cochain.ofHom (inr φ)).comp γ₂ (zero_add j) := by constructor · rintro rfl tauto · rintro ⟨h₁, h₂⟩ ext p q hpq rw [ext_from_iff φ (p + 1) p rfl] replace h₁ := Cochain.congr_v h₁ (p + 1) q (by omega) replace h₂ := Cochain.congr_v h₂ p q (by omega) simp only [Cochain.comp_v (inl φ) _ _ (p + 1) p q (by omega) hpq] at h₁ simp only [Cochain.zero_cochain_comp_v, Cochain.ofHom_v] at h₂ exact ⟨h₁, h₂⟩ lemma id : (fst φ).1.comp (inl φ) (add_neg_cancel 1) + (snd φ).comp (Cochain.ofHom (inr φ)) (add_zero 0) = Cochain.ofHom (𝟙 _) := by simp [ext_cochain_from_iff φ (-1) 0 (neg_add_cancel 1)] lemma id_X (p q : ℤ) (hpq : p + 1 = q) : (fst φ).1.v p q hpq ≫ (inl φ).v q p (by omega) + (snd φ).v p p (add_zero p) ≫ (inr φ).f p = 𝟙 ((mappingCone φ).X p) := by simpa only [Cochain.add_v, Cochain.comp_zero_cochain_v, Cochain.ofHom_v, id_f, Cochain.comp_v _ _ (add_neg_cancel 1) p q p hpq (by omega)] using Cochain.congr_v (id φ) p p (add_zero p) @[reassoc] lemma inl_v_d (i j k : ℤ) (hij : i + (-1) = j) (hik : k + (-1) = i) : (inl φ).v i j hij ≫ (mappingCone φ).d j i = φ.f i ≫ (inr φ).f i - F.d i k ≫ (inl φ).v _ _ hik := by dsimp [mappingCone, inl, inr] rw [homotopyCofiber.inlX_d φ j i k (by dsimp; omega) (by dsimp; omega)] abel @[reassoc] lemma inr_f_d (n₁ n₂ : ℤ) : (inr φ).f n₁ ≫ (mappingCone φ).d n₁ n₂ = G.d n₁ n₂ ≫ (inr φ).f n₂ := by simp @[reassoc] lemma d_fst_v (i j k : ℤ) (hij : i + 1 = j) (hjk : j + 1 = k) : (mappingCone φ).d i j ≫ (fst φ).1.v j k hjk = -(fst φ).1.v i j hij ≫ F.d j k := by apply homotopyCofiber.d_fstX @[reassoc (attr := simp)] lemma d_fst_v' (i j : ℤ) (hij : i + 1 = j) : (mappingCone φ).d (i - 1) i ≫ (fst φ).1.v i j hij = -(fst φ).1.v (i - 1) i (by omega) ≫ F.d i j := d_fst_v φ (i - 1) i j (by omega) hij @[reassoc] lemma d_snd_v (i j : ℤ) (hij : i + 1 = j) : (mappingCone φ).d i j ≫ (snd φ).v j j (add_zero _) = (fst φ).1.v i j hij ≫ φ.f j + (snd φ).v i i (add_zero i) ≫ G.d i j := by dsimp [mappingCone, snd, fst] simp only [Cochain.ofHoms_v] apply homotopyCofiber.d_sndX @[reassoc (attr := simp)] lemma d_snd_v' (n : ℤ) : (mappingCone φ).d (n - 1) n ≫ (snd φ).v n n (add_zero n) = (fst φ : Cochain (mappingCone φ) F 1).v (n - 1) n (by omega) ≫ φ.f n + (snd φ).v (n - 1) (n - 1) (add_zero _) ≫ G.d (n - 1) n := by apply d_snd_v @[simp] lemma δ_inl : δ (-1) 0 (inl φ) = Cochain.ofHom (φ ≫ inr φ) := by ext p simp [δ_v (-1) 0 (neg_add_cancel 1) (inl φ) p p (add_zero p) _ _ rfl rfl, inl_v_d φ p (p - 1) (p + 1) (by omega) (by omega)] @[simp] lemma δ_snd : δ 0 1 (snd φ) = -(fst φ).1.comp (Cochain.ofHom φ) (add_zero 1) := by ext p q hpq simp [d_snd_v φ p q hpq] section variable {K : CochainComplex C ℤ} {n m : ℤ} /-- Given `φ : F ⟶ G`, this is the cochain in `Cochain (mappingCone φ) K n` that is constructed from two cochains `α : Cochain F K m` (with `m + 1 = n`) and `β : Cochain F K n`. -/ noncomputable def descCochain (α : Cochain F K m) (β : Cochain G K n) (h : m + 1 = n) : Cochain (mappingCone φ) K n := (fst φ).1.comp α (by rw [← h, add_comm]) + (snd φ).comp β (zero_add n) variable (α : Cochain F K m) (β : Cochain G K n) (h : m + 1 = n) @[simp] lemma inl_descCochain : (inl φ).comp (descCochain φ α β h) (by omega) = α := by simp [descCochain] @[simp] lemma inr_descCochain : (Cochain.ofHom (inr φ)).comp (descCochain φ α β h) (zero_add n) = β := by simp [descCochain] @[reassoc (attr := simp)] lemma inl_v_descCochain_v (p₁ p₂ p₃ : ℤ) (h₁₂ : p₁ + (-1) = p₂) (h₂₃ : p₂ + n = p₃) : (inl φ).v p₁ p₂ h₁₂ ≫ (descCochain φ α β h).v p₂ p₃ h₂₃ = α.v p₁ p₃ (by rw [← h₂₃, ← h₁₂, ← h, add_comm m, add_assoc, neg_add_cancel_left]) := by simpa only [Cochain.comp_v _ _ (show -1 + n = m by omega) p₁ p₂ p₃ (by omega) (by omega)] using Cochain.congr_v (inl_descCochain φ α β h) p₁ p₃ (by omega) @[reassoc (attr := simp)] lemma inr_f_descCochain_v (p₁ p₂ : ℤ) (h₁₂ : p₁ + n = p₂) : (inr φ).f p₁ ≫ (descCochain φ α β h).v p₁ p₂ h₁₂ = β.v p₁ p₂ h₁₂ := by simpa only [Cochain.comp_v _ _ (zero_add n) p₁ p₁ p₂ (add_zero p₁) h₁₂, Cochain.ofHom_v] using Cochain.congr_v (inr_descCochain φ α β h) p₁ p₂ (by omega) lemma δ_descCochain (n' : ℤ) (hn' : n + 1 = n') : δ n n' (descCochain φ α β h) = (fst φ).1.comp (δ m n α + n'.negOnePow • (Cochain.ofHom φ).comp β (zero_add n)) (by omega) + (snd φ).comp (δ n n' β) (zero_add n') := by dsimp only [descCochain] simp only [δ_add, Cochain.comp_add, δ_comp (fst φ).1 α _ 2 n n' hn' (by omega) (by omega), Cocycle.δ_eq_zero, Cochain.zero_comp, smul_zero, add_zero, δ_comp (snd φ) β (zero_add n) 1 n' n' hn' (zero_add 1) hn', δ_snd, Cochain.neg_comp, smul_neg, Cochain.comp_assoc_of_second_is_zero_cochain, Cochain.comp_units_smul, ← hn', Int.negOnePow_succ, Units.neg_smul, Cochain.comp_neg] abel end /-- Given `φ : F ⟶ G`, this is the cocycle in `Cocycle (mappingCone φ) K n` that is constructed from `α : Cochain F K m` (with `m + 1 = n`) and `β : Cocycle F K n`, when a suitable cocycle relation is satisfied. -/ @[simps!] noncomputable def descCocycle {K : CochainComplex C ℤ} {n m : ℤ} (α : Cochain F K m) (β : Cocycle G K n) (h : m + 1 = n) (eq : δ m n α = n.negOnePow • (Cochain.ofHom φ).comp β.1 (zero_add n)) : Cocycle (mappingCone φ) K n := Cocycle.mk (descCochain φ α β.1 h) (n + 1) rfl (by simp [δ_descCochain _ _ _ _ _ rfl, eq, Int.negOnePow_succ]) section variable {K : CochainComplex C ℤ} /-- Given `φ : F ⟶ G`, this is the morphism `mappingCone φ ⟶ K` that is constructed from a cochain `α : Cochain F K (-1)` and a morphism `β : G ⟶ K` such that `δ (-1) 0 α = Cochain.ofHom (φ ≫ β)`. -/ noncomputable def desc (α : Cochain F K (-1)) (β : G ⟶ K) (eq : δ (-1) 0 α = Cochain.ofHom (φ ≫ β)) : mappingCone φ ⟶ K := Cocycle.homOf (descCocycle φ α (Cocycle.ofHom β) (neg_add_cancel 1) (by simp [eq])) variable (α : Cochain F K (-1)) (β : G ⟶ K) (eq : δ (-1) 0 α = Cochain.ofHom (φ ≫ β)) @[simp] lemma ofHom_desc : Cochain.ofHom (desc φ α β eq) = descCochain φ α (Cochain.ofHom β) (neg_add_cancel 1) := by simp [desc] @[reassoc (attr := simp)] lemma inl_v_desc_f (p q : ℤ) (h : p + (-1) = q) : (inl φ).v p q h ≫ (desc φ α β eq).f q = α.v p q h := by simp [desc] lemma inl_desc : (inl φ).comp (Cochain.ofHom (desc φ α β eq)) (add_zero _) = α := by simp @[reassoc (attr := simp)] lemma inr_f_desc_f (p : ℤ) : (inr φ).f p ≫ (desc φ α β eq).f p = β.f p := by simp [desc] @[reassoc (attr := simp)] lemma inr_desc : inr φ ≫ desc φ α β eq = β := by aesop_cat lemma desc_f (p q : ℤ) (hpq : p + 1 = q) : (desc φ α β eq).f p = (fst φ).1.v p q hpq ≫ α.v q p (by omega) + (snd φ).v p p (add_zero p) ≫ β.f p := by simp [ext_from_iff _ _ _ hpq] end /-- Constructor for homotopies between morphisms from a mapping cone. -/ noncomputable def descHomotopy {K : CochainComplex C ℤ} (f₁ f₂ : mappingCone φ ⟶ K) (γ₁ : Cochain F K (-2)) (γ₂ : Cochain G K (-1)) (h₁ : (inl φ).comp (Cochain.ofHom f₁) (add_zero (-1)) = δ (-2) (-1) γ₁ + (Cochain.ofHom φ).comp γ₂ (zero_add (-1)) + (inl φ).comp (Cochain.ofHom f₂) (add_zero (-1))) (h₂ : Cochain.ofHom (inr φ ≫ f₁) = δ (-1) 0 γ₂ + Cochain.ofHom (inr φ ≫ f₂)) : Homotopy f₁ f₂ := (Cochain.equivHomotopy f₁ f₂).symm ⟨descCochain φ γ₁ γ₂ (by norm_num), by simp only [Cochain.ofHom_comp] at h₂ simp [ext_cochain_from_iff _ _ _ (neg_add_cancel 1), δ_descCochain _ _ _ _ _ (neg_add_cancel 1), h₁, h₂]⟩ section variable {K : CochainComplex C ℤ} {n m : ℤ} /-- Given `φ : F ⟶ G`, this is the cochain in `Cochain (mappingCone φ) K n` that is constructed from two cochains `α : Cochain F K m` (with `m + 1 = n`) and `β : Cochain F K n`. -/ noncomputable def liftCochain (α : Cochain K F m) (β : Cochain K G n) (h : n + 1 = m) : Cochain K (mappingCone φ) n := α.comp (inl φ) (by omega) + β.comp (Cochain.ofHom (inr φ)) (add_zero n) variable (α : Cochain K F m) (β : Cochain K G n) (h : n + 1 = m) @[simp] lemma liftCochain_fst : (liftCochain φ α β h).comp (fst φ).1 h = α := by simp [liftCochain] @[simp] lemma liftCochain_snd : (liftCochain φ α β h).comp (snd φ) (add_zero n) = β := by simp [liftCochain] @[reassoc (attr := simp)] lemma liftCochain_v_fst_v (p₁ p₂ p₃ : ℤ) (h₁₂ : p₁ + n = p₂) (h₂₃ : p₂ + 1 = p₃) : (liftCochain φ α β h).v p₁ p₂ h₁₂ ≫ (fst φ).1.v p₂ p₃ h₂₃ = α.v p₁ p₃ (by omega) := by simpa only [Cochain.comp_v _ _ h p₁ p₂ p₃ h₁₂ h₂₃] using Cochain.congr_v (liftCochain_fst φ α β h) p₁ p₃ (by omega) @[reassoc (attr := simp)] lemma liftCochain_v_snd_v (p₁ p₂ : ℤ) (h₁₂ : p₁ + n = p₂) : (liftCochain φ α β h).v p₁ p₂ h₁₂ ≫ (snd φ).v p₂ p₂ (add_zero p₂) = β.v p₁ p₂ h₁₂ := by simpa only [Cochain.comp_v _ _ (add_zero n) p₁ p₂ p₂ h₁₂ (add_zero p₂)] using Cochain.congr_v (liftCochain_snd φ α β h) p₁ p₂ (by omega) lemma δ_liftCochain (m' : ℤ) (hm' : m + 1 = m') : δ n m (liftCochain φ α β h) = -(δ m m' α).comp (inl φ) (by omega) + (δ n m β + α.comp (Cochain.ofHom φ) (add_zero m)).comp (Cochain.ofHom (inr φ)) (add_zero m) := by dsimp only [liftCochain] simp only [δ_add, δ_comp α (inl φ) _ m' _ _ h hm' (neg_add_cancel 1), δ_comp_zero_cochain _ _ _ h, δ_inl, Cochain.ofHom_comp, Int.negOnePow_neg, Int.negOnePow_one, Units.neg_smul, one_smul, δ_ofHom, Cochain.comp_zero, zero_add, Cochain.add_comp, Cochain.comp_assoc_of_second_is_zero_cochain] abel end /-- Given `φ : F ⟶ G`, this is the cocycle in `Cocycle K (mappingCone φ) n` that is constructed from `α : Cochain K F m` (with `n + 1 = m`) and `β : Cocycle K G n`, when a suitable cocycle relation is satisfied. -/ @[simps!] noncomputable def liftCocycle {K : CochainComplex C ℤ} {n m : ℤ} (α : Cocycle K F m) (β : Cochain K G n) (h : n + 1 = m) (eq : δ n m β + α.1.comp (Cochain.ofHom φ) (add_zero m) = 0) : Cocycle K (mappingCone φ) n := Cocycle.mk (liftCochain φ α β h) m h (by simp only [δ_liftCochain φ α β h (m+1) rfl, eq, Cocycle.δ_eq_zero, Cochain.zero_comp, neg_zero, add_zero]) section variable {K : CochainComplex C ℤ} (α : Cocycle K F 1) (β : Cochain K G 0) (eq : δ 0 1 β + α.1.comp (Cochain.ofHom φ) (add_zero 1) = 0) /-- Given `φ : F ⟶ G`, this is the morphism `K ⟶ mappingCone φ` that is constructed from a cocycle `α : Cochain K F 1` and a cochain `β : Cochain K G 0` when a suitable cocycle relation is satisfied. -/ noncomputable def lift : K ⟶ mappingCone φ := Cocycle.homOf (liftCocycle φ α β (zero_add 1) eq) @[simp] lemma ofHom_lift : Cochain.ofHom (lift φ α β eq) = liftCochain φ α β (zero_add 1) := by simp only [lift, Cocycle.cochain_ofHom_homOf_eq_coe, liftCocycle_coe] @[reassoc (attr := simp)] lemma lift_f_fst_v (p q : ℤ) (hpq : p + 1 = q) : (lift φ α β eq).f p ≫ (fst φ).1.v p q hpq = α.1.v p q hpq := by simp [lift] lemma lift_fst : (Cochain.ofHom (lift φ α β eq)).comp (fst φ).1 (zero_add 1) = α.1 := by simp @[reassoc (attr := simp)] lemma lift_f_snd_v (p q : ℤ) (hpq : p + 0 = q) : (lift φ α β eq).f p ≫ (snd φ).v p q hpq = β.v p q hpq := by obtain rfl : q = p := by omega simp [lift] lemma lift_snd : (Cochain.ofHom (lift φ α β eq)).comp (snd φ) (zero_add 0) = β := by simp lemma lift_f (p q : ℤ) (hpq : p + 1 = q) : (lift φ α β eq).f p = α.1.v p q hpq ≫ (inl φ).v q p (by omega) + β.v p p (add_zero p) ≫ (inr φ).f p := by simp [ext_to_iff _ _ _ hpq] end /-- Constructor for homotopies between morphisms to a mapping cone. -/ noncomputable def liftHomotopy {K : CochainComplex C ℤ} (f₁ f₂ : K ⟶ mappingCone φ) (α : Cochain K F 0) (β : Cochain K G (-1)) (h₁ : (Cochain.ofHom f₁).comp (fst φ).1 (zero_add 1) = -δ 0 1 α + (Cochain.ofHom f₂).comp (fst φ).1 (zero_add 1)) (h₂ : (Cochain.ofHom f₁).comp (snd φ) (zero_add 0) = δ (-1) 0 β + α.comp (Cochain.ofHom φ) (zero_add 0) + (Cochain.ofHom f₂).comp (snd φ) (zero_add 0)) : Homotopy f₁ f₂ := (Cochain.equivHomotopy f₁ f₂).symm ⟨liftCochain φ α β (neg_add_cancel 1), by simp [δ_liftCochain _ _ _ _ _ (zero_add 1), ext_cochain_to_iff _ _ _ (zero_add 1), h₁, h₂]⟩ section variable {K L : CochainComplex C ℤ} {n m : ℤ} (α : Cochain K F m) (β : Cochain K G n) {n' m' : ℤ} (α' : Cochain F L m') (β' : Cochain G L n') (h : n + 1 = m) (h' : m' + 1 = n') (p : ℤ) (hp : n + n' = p)
@[simp] lemma liftCochain_descCochain : (liftCochain φ α β h).comp (descCochain φ α' β' h') hp = α.comp α' (by omega) + β.comp β' (by omega) := by simp [liftCochain, descCochain, Cochain.comp_assoc α (inl φ) _ _ (show -1 + n' = m' by omega) (by linarith)]
Mathlib/Algebra/Homology/HomotopyCategory/MappingCone.lean
524
530
/- Copyright (c) 2021 Junyan Xu. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Junyan Xu -/ import Mathlib.AlgebraicGeometry.Restrict import Mathlib.CategoryTheory.Adjunction.Limits import Mathlib.CategoryTheory.Adjunction.Reflective /-! # Adjunction between `Γ` and `Spec` We define the adjunction `ΓSpec.adjunction : Γ ⊣ Spec` by defining the unit (`toΓSpec`, in multiple steps in this file) and counit (done in `Spec.lean`) and checking that they satisfy the left and right triangle identities. The constructions and proofs make use of maps and lemmas defined and proved in structure_sheaf.lean extensively. Notice that since the adjunction is between contravariant functors, you get to choose one of the two categories to have arrows reversed, and it is equally valid to present the adjunction as `Spec ⊣ Γ` (`Spec.to_LocallyRingedSpace.right_op ⊣ Γ`), in which case the unit and the counit would switch to each other. ## Main definition * `AlgebraicGeometry.identityToΓSpec` : The natural transformation `𝟭 _ ⟶ Γ ⋙ Spec`. * `AlgebraicGeometry.ΓSpec.locallyRingedSpaceAdjunction` : The adjunction `Γ ⊣ Spec` from `CommRingᵒᵖ` to `LocallyRingedSpace`. * `AlgebraicGeometry.ΓSpec.adjunction` : The adjunction `Γ ⊣ Spec` from `CommRingᵒᵖ` to `Scheme`. -/ -- Explicit universe annotations were used in this file to improve performance https://github.com/leanprover-community/mathlib4/issues/12737 noncomputable section universe u open PrimeSpectrum namespace AlgebraicGeometry open Opposite open CategoryTheory open StructureSheaf open Spec (structureSheaf) open TopologicalSpace open AlgebraicGeometry.LocallyRingedSpace open TopCat.Presheaf open TopCat.Presheaf.SheafCondition namespace LocallyRingedSpace variable (X : LocallyRingedSpace.{u}) /-- The canonical map from the underlying set to the prime spectrum of `Γ(X)`. -/ def toΓSpecFun : X → PrimeSpectrum (Γ.obj (op X)) := fun x => comap (X.presheaf.Γgerm x).hom (IsLocalRing.closedPoint (X.presheaf.stalk x)) theorem not_mem_prime_iff_unit_in_stalk (r : Γ.obj (op X)) (x : X) : r ∉ (X.toΓSpecFun x).asIdeal ↔ IsUnit (X.presheaf.Γgerm x r) := by simp [toΓSpecFun, IsLocalRing.closedPoint] /-- The preimage of a basic open in `Spec Γ(X)` under the unit is the basic open in `X` defined by the same element (they are equal as sets). -/ theorem toΓSpec_preimage_basicOpen_eq (r : Γ.obj (op X)) : X.toΓSpecFun ⁻¹' basicOpen r = SetLike.coe (X.toRingedSpace.basicOpen r) := by ext dsimp simp only [Set.mem_preimage, SetLike.mem_coe] rw [X.toRingedSpace.mem_top_basicOpen] exact not_mem_prime_iff_unit_in_stalk .. /-- `toΓSpecFun` is continuous. -/ theorem toΓSpec_continuous : Continuous X.toΓSpecFun := by rw [isTopologicalBasis_basic_opens.continuous_iff] rintro _ ⟨r, rfl⟩ rw [X.toΓSpec_preimage_basicOpen_eq r] exact (X.toRingedSpace.basicOpen r).2 /-- The canonical (bundled) continuous map from the underlying topological space of `X` to the prime spectrum of its global sections. -/ def toΓSpecBase : X.toTopCat ⟶ Spec.topObj (Γ.obj (op X)) := TopCat.ofHom { toFun := X.toΓSpecFun continuous_toFun := X.toΓSpec_continuous } variable (r : Γ.obj (op X)) /-- The preimage in `X` of a basic open in `Spec Γ(X)` (as an open set). -/ abbrev toΓSpecMapBasicOpen : Opens X := (Opens.map X.toΓSpecBase).obj (basicOpen r) /-- The preimage is the basic open in `X` defined by the same element `r`. -/ theorem toΓSpecMapBasicOpen_eq : X.toΓSpecMapBasicOpen r = X.toRingedSpace.basicOpen r := Opens.ext (X.toΓSpec_preimage_basicOpen_eq r) /-- The map from the global sections `Γ(X)` to the sections on the (preimage of) a basic open. -/ abbrev toToΓSpecMapBasicOpen : X.presheaf.obj (op ⊤) ⟶ X.presheaf.obj (op <| X.toΓSpecMapBasicOpen r) := X.presheaf.map (X.toΓSpecMapBasicOpen r).leTop.op /-- `r` is a unit as a section on the basic open defined by `r`. -/ theorem isUnit_res_toΓSpecMapBasicOpen : IsUnit (X.toToΓSpecMapBasicOpen r r) := by convert (X.presheaf.map <| (eqToHom <| X.toΓSpecMapBasicOpen_eq r).op).hom.isUnit_map (X.toRingedSpace.isUnit_res_basicOpen r) rw [← CommRingCat.comp_apply, ← Functor.map_comp] congr /-- Define the sheaf hom on individual basic opens for the unit. -/ def toΓSpecCApp : (structureSheaf <| Γ.obj <| op X).val.obj (op <| basicOpen r) ⟶ X.presheaf.obj (op <| X.toΓSpecMapBasicOpen r) := -- note: the explicit type annotations were not needed before -- https://github.com/leanprover-community/mathlib4/pull/19757 CommRingCat.ofHom <| IsLocalization.Away.lift (R := Γ.obj (op X)) (S := (structureSheaf ↑(Γ.obj (op X))).val.obj (op (basicOpen r))) r (isUnit_res_toΓSpecMapBasicOpen _ r) /-- Characterization of the sheaf hom on basic opens, direction ← (next lemma) is used at various places, but → is not used in this file. -/ theorem toΓSpecCApp_iff (f : (structureSheaf <| Γ.obj <| op X).val.obj (op <| basicOpen r) ⟶ X.presheaf.obj (op <| X.toΓSpecMapBasicOpen r)) : toOpen _ (basicOpen r) ≫ f = X.toToΓSpecMapBasicOpen r ↔ f = X.toΓSpecCApp r := by -- Porting Note: Type class problem got stuck in `IsLocalization.Away.AwayMap.lift_comp` -- created instance manually. This replaces the `pick_goal` tactics have loc_inst := IsLocalization.to_basicOpen (Γ.obj (op X)) r refine ConcreteCategory.ext_iff.trans ?_ rw [← @IsLocalization.Away.lift_comp _ _ _ _ _ _ _ r loc_inst _ (X.isUnit_res_toΓSpecMapBasicOpen r)] --pick_goal 5; exact is_localization.to_basic_open _ r constructor · intro h ext : 1 exact IsLocalization.ringHom_ext (Submonoid.powers r) h apply congr_arg theorem toΓSpecCApp_spec : toOpen _ (basicOpen r) ≫ X.toΓSpecCApp r = X.toToΓSpecMapBasicOpen r := (X.toΓSpecCApp_iff r _).2 rfl /-- The sheaf hom on all basic opens, commuting with restrictions. -/ @[simps app] def toΓSpecCBasicOpens : (inducedFunctor basicOpen).op ⋙ (structureSheaf (Γ.obj (op X))).1 ⟶ (inducedFunctor basicOpen).op ⋙ ((TopCat.Sheaf.pushforward _ X.toΓSpecBase).obj X.𝒪).1 where app r := X.toΓSpecCApp r.unop naturality r s f := by apply (StructureSheaf.to_basicOpen_epi (Γ.obj (op X)) r.unop).1 simp only [← Category.assoc] rw [X.toΓSpecCApp_spec r.unop] convert X.toΓSpecCApp_spec s.unop symm apply X.presheaf.map_comp /-- The canonical morphism of sheafed spaces from `X` to the spectrum of its global sections. -/ @[simps] def toΓSpecSheafedSpace : X.toSheafedSpace ⟶ Spec.toSheafedSpace.obj (op (Γ.obj (op X))) where base := X.toΓSpecBase c := TopCat.Sheaf.restrictHomEquivHom (structureSheaf (Γ.obj (op X))).1 _ isBasis_basic_opens X.toΓSpecCBasicOpens theorem toΓSpecSheafedSpace_app_eq : X.toΓSpecSheafedSpace.c.app (op (basicOpen r)) = X.toΓSpecCApp r := by apply TopCat.Sheaf.extend_hom_app _ _ _ -- Porting note: need a helper lemma `toΓSpecSheafedSpace_app_spec_assoc` to help compile -- `toStalk_stalkMap_to_Γ_Spec` @[reassoc] theorem toΓSpecSheafedSpace_app_spec (r : Γ.obj (op X)) : toOpen (Γ.obj (op X)) (basicOpen r) ≫ X.toΓSpecSheafedSpace.c.app (op (basicOpen r)) = X.toToΓSpecMapBasicOpen r := (X.toΓSpecSheafedSpace_app_eq r).symm ▸ X.toΓSpecCApp_spec r /-- The map on stalks induced by the unit commutes with maps from `Γ(X)` to stalks (in `Spec Γ(X)` and in `X`). -/ theorem toStalk_stalkMap_toΓSpec (x : X) : toStalk _ _ ≫ X.toΓSpecSheafedSpace.stalkMap x = X.presheaf.Γgerm x := by rw [PresheafedSpace.Hom.stalkMap, ← toOpen_germ _ (basicOpen (1 : Γ.obj (op X))) _ (by rw [basicOpen_one]; trivial), ← Category.assoc, Category.assoc (toOpen _ _), stalkFunctor_map_germ, ← Category.assoc, toΓSpecSheafedSpace_app_spec, Γgerm] erw [← stalkPushforward_germ _ _ X.presheaf ⊤] congr 1 exact (X.toΓSpecBase _* X.presheaf).germ_res le_top.hom _ _ /-- The canonical morphism from `X` to the spectrum of its global sections. -/ @[simps! base] def toΓSpec : X ⟶ Spec.locallyRingedSpaceObj (Γ.obj (op X)) where __ := X.toΓSpecSheafedSpace prop := by intro x let p : PrimeSpectrum (Γ.obj (op X)) := X.toΓSpecFun x constructor -- show stalk map is local hom ↓ let S := (structureSheaf _).presheaf.stalk p rintro (t : S) ht obtain ⟨⟨r, s⟩, he⟩ := IsLocalization.surj p.asIdeal.primeCompl t dsimp at he set t' := _ change t * t' = _ at he apply isUnit_of_mul_isUnit_left (y := t') rw [he] refine IsLocalization.map_units S (⟨r, ?_⟩ : p.asIdeal.primeCompl) apply (not_mem_prime_iff_unit_in_stalk _ _ _).mpr rw [← toStalk_stalkMap_toΓSpec, CommRingCat.comp_apply] erw [← he] rw [RingHom.map_mul] exact ht.mul <| (IsLocalization.map_units (R := Γ.obj (op X)) S s).map _ /-- On a locally ringed space `X`, the preimage of the zero locus of the prime spectrum of `Γ(X, ⊤)` under `toΓSpec` agrees with the associated zero locus on `X`. -/ lemma toΓSpec_preimage_zeroLocus_eq {X : LocallyRingedSpace.{u}} (s : Set (X.presheaf.obj (op ⊤))) : X.toΓSpec.base ⁻¹' PrimeSpectrum.zeroLocus s = X.toRingedSpace.zeroLocus s := by simp only [RingedSpace.zeroLocus] have (i : LocallyRingedSpace.Γ.obj (op X)) (_ : i ∈ s) : (SetLike.coe (X.toRingedSpace.basicOpen i))ᶜ = X.toΓSpec.base ⁻¹' ((PrimeSpectrum.basicOpen i).carrier)ᶜ := by symm rw [Set.preimage_compl, Opens.carrier_eq_coe] erw [X.toΓSpec_preimage_basicOpen_eq i] erw [Set.iInter₂_congr this] simp_rw [← Set.preimage_iInter₂, Opens.carrier_eq_coe, PrimeSpectrum.basicOpen_eq_zeroLocus_compl, compl_compl] rw [← PrimeSpectrum.zeroLocus_iUnion₂] simp theorem comp_ring_hom_ext {X : LocallyRingedSpace.{u}} {R : CommRingCat.{u}} {f : R ⟶ Γ.obj (op X)} {β : X ⟶ Spec.locallyRingedSpaceObj R} (w : X.toΓSpec.base ≫ (Spec.locallyRingedSpaceMap f).base = β.base) (h : ∀ r : R, f ≫ X.presheaf.map (homOfLE le_top : (Opens.map β.base).obj (basicOpen r) ⟶ _).op = toOpen R (basicOpen r) ≫ β.c.app (op (basicOpen r))) : X.toΓSpec ≫ Spec.locallyRingedSpaceMap f = β := by ext1 refine Spec.basicOpen_hom_ext w ?_ intro r U rw [LocallyRingedSpace.comp_c_app] erw [toOpen_comp_comap_assoc] rw [Category.assoc] erw [toΓSpecSheafedSpace_app_spec, ← X.presheaf.map_comp] exact h r /-- `toSpecΓ _` is an isomorphism so these are mutually two-sided inverses. -/ theorem Γ_Spec_left_triangle : toSpecΓ (Γ.obj (op X)) ≫ X.toΓSpec.c.app (op ⊤) = 𝟙 _ := by unfold toSpecΓ rw [← toOpen_res _ (basicOpen (1 : Γ.obj (op X))) ⊤ (eqToHom basicOpen_one.symm), Category.assoc, NatTrans.naturality, ← Category.assoc] erw [X.toΓSpecSheafedSpace_app_spec 1, ← Functor.map_comp] convert eqToHom_map X.presheaf _; rfl end LocallyRingedSpace /-- The unit as a natural transformation. -/ def identityToΓSpec : 𝟭 LocallyRingedSpace.{u} ⟶ Γ.rightOp ⋙ Spec.toLocallyRingedSpace where app := LocallyRingedSpace.toΓSpec naturality X Y f := by symm apply LocallyRingedSpace.comp_ring_hom_ext · ext1 x dsimp show PrimeSpectrum.comap (f.c.app (op ⊤)).hom (X.toΓSpecFun x) = Y.toΓSpecFun (f.base x) dsimp [toΓSpecFun] rw [← IsLocalRing.comap_closedPoint (f.stalkMap x).hom, ← PrimeSpectrum.comap_comp_apply, ← PrimeSpectrum.comap_comp_apply, ← CommRingCat.hom_comp, ← CommRingCat.hom_comp] congr 3 exact (PresheafedSpace.stalkMap_germ f.1 ⊤ x trivial).symm · intro r rw [LocallyRingedSpace.comp_c_app, ← Category.assoc] erw [Y.toΓSpecSheafedSpace_app_spec, f.c.naturality] rfl namespace ΓSpec theorem left_triangle (X : LocallyRingedSpace) : SpecΓIdentity.inv.app (Γ.obj (op X)) ≫ (identityToΓSpec.app X).c.app (op ⊤) = 𝟙 _ := X.Γ_Spec_left_triangle /-- `SpecΓIdentity` is iso so these are mutually two-sided inverses. -/ theorem right_triangle (R : CommRingCat) : identityToΓSpec.app (Spec.toLocallyRingedSpace.obj <| op R) ≫ Spec.toLocallyRingedSpace.map (SpecΓIdentity.inv.app R).op = 𝟙 _ := by apply LocallyRingedSpace.comp_ring_hom_ext · ext (p : PrimeSpectrum R) dsimp refine PrimeSpectrum.ext (Ideal.ext fun x => ?_) rw [← IsLocalization.AtPrime.to_map_mem_maximal_iff ((structureSheaf R).presheaf.stalk p) p.asIdeal x] rfl · intro r; apply toOpen_res /-- The adjunction `Γ ⊣ Spec` from `CommRingᵒᵖ` to `LocallyRingedSpace`. -/ -- Porting note: `simps` generates lemmas not in `simp` normal form, so `unit` and `counit` have to -- be added manually def locallyRingedSpaceAdjunction : Γ.rightOp ⊣ Spec.toLocallyRingedSpace.{u} where unit := identityToΓSpec counit := (NatIso.op SpecΓIdentity).inv left_triangle_components X := by simp only [Functor.id_obj, Functor.rightOp_obj, Γ_obj, Functor.comp_obj, Spec.toLocallyRingedSpace_obj, Spec.locallyRingedSpaceObj_toSheafedSpace, Spec.sheafedSpaceObj_carrier, Spec.sheafedSpaceObj_presheaf, Functor.rightOp_map, Γ_map, Quiver.Hom.unop_op, NatIso.op_inv, NatTrans.op_app, SpecΓIdentity_inv_app] exact congr_arg Quiver.Hom.op (left_triangle X) right_triangle_components R := by simp only [Spec.toLocallyRingedSpace_obj, Functor.id_obj, Functor.comp_obj, Functor.rightOp_obj, Γ_obj, Spec.locallyRingedSpaceObj_toSheafedSpace, Spec.sheafedSpaceObj_carrier, Spec.sheafedSpaceObj_presheaf, NatIso.op_inv, NatTrans.op_app, op_unop, SpecΓIdentity_inv_app, Spec.toLocallyRingedSpace_map, Quiver.Hom.unop_op] exact right_triangle R.unop lemma locallyRingedSpaceAdjunction_unit : locallyRingedSpaceAdjunction.unit = identityToΓSpec := rfl lemma locallyRingedSpaceAdjunction_counit : locallyRingedSpaceAdjunction.counit = (NatIso.op SpecΓIdentity.{u}).inv := rfl @[simp] lemma locallyRingedSpaceAdjunction_counit_app (R : CommRingCatᵒᵖ) : locallyRingedSpaceAdjunction.counit.app R = (toOpen R.unop ⊤).op := rfl @[simp] lemma locallyRingedSpaceAdjunction_counit_app' (R : Type u) [CommRing R] : locallyRingedSpaceAdjunction.counit.app (op <| CommRingCat.of R) = (toOpen R ⊤).op := rfl lemma locallyRingedSpaceAdjunction_homEquiv_apply {X : LocallyRingedSpace} {R : CommRingCatᵒᵖ} (f : Γ.rightOp.obj X ⟶ R) : locallyRingedSpaceAdjunction.homEquiv X R f = identityToΓSpec.app X ≫ Spec.locallyRingedSpaceMap f.unop := rfl lemma locallyRingedSpaceAdjunction_homEquiv_apply' {X : LocallyRingedSpace} {R : Type u} [CommRing R] (f : CommRingCat.of R ⟶ Γ.obj <| op X) : locallyRingedSpaceAdjunction.homEquiv X (op <| CommRingCat.of R) (op f) = identityToΓSpec.app X ≫ Spec.locallyRingedSpaceMap f := rfl lemma toOpen_comp_locallyRingedSpaceAdjunction_homEquiv_app {X : LocallyRingedSpace} {R : Type u} [CommRing R] (f : Γ.rightOp.obj X ⟶ op (CommRingCat.of R)) (U) : StructureSheaf.toOpen R U.unop ≫ (locallyRingedSpaceAdjunction.homEquiv X (op <| CommRingCat.of R) f).c.app U = f.unop ≫ X.presheaf.map (homOfLE le_top).op := by rw [← StructureSheaf.toOpen_res _ _ _ (homOfLE le_top), Category.assoc, NatTrans.naturality _ (homOfLE (le_top (a := U.unop))).op, show (toOpen R ⊤) = (toOpen R ⊤).op.unop from rfl, ← locallyRingedSpaceAdjunction_counit_app'] simp_rw [← Γ_map_op] rw [← Γ.rightOp_map_unop, ← Category.assoc, ← unop_comp, ← Adjunction.homEquiv_counit, Equiv.symm_apply_apply] rfl /-- The adjunction `Γ ⊣ Spec` from `CommRingᵒᵖ` to `Scheme`. -/ def adjunction : Scheme.Γ.rightOp ⊣ Scheme.Spec.{u} where unit := { app := fun X ↦ ⟨locallyRingedSpaceAdjunction.{u}.unit.app X.toLocallyRingedSpace⟩ naturality := fun _ _ f ↦ Scheme.Hom.ext' (locallyRingedSpaceAdjunction.{u}.unit.naturality f.toLRSHom) } counit := (NatIso.op Scheme.SpecΓIdentity.{u}).inv left_triangle_components Y := locallyRingedSpaceAdjunction.left_triangle_components Y.toLocallyRingedSpace right_triangle_components R := Scheme.Hom.ext' <| locallyRingedSpaceAdjunction.right_triangle_components R theorem adjunction_homEquiv_apply {X : Scheme} {R : CommRingCatᵒᵖ} (f : (op <| Scheme.Γ.obj <| op X) ⟶ R) : ΓSpec.adjunction.homEquiv X R f = ⟨locallyRingedSpaceAdjunction.homEquiv X.1 R f⟩ := rfl theorem adjunction_homEquiv_symm_apply {X : Scheme} {R : CommRingCatᵒᵖ} (f : X ⟶ Scheme.Spec.obj R) : (ΓSpec.adjunction.homEquiv X R).symm f = (locallyRingedSpaceAdjunction.homEquiv X.1 R).symm f.toLRSHom := rfl
theorem adjunction_counit_app' {R : CommRingCatᵒᵖ} : ΓSpec.adjunction.counit.app R = locallyRingedSpaceAdjunction.counit.app R := rfl @[simp] theorem adjunction_counit_app {R : CommRingCatᵒᵖ} : ΓSpec.adjunction.counit.app R = (Scheme.ΓSpecIso (unop R)).inv.op := rfl /-- The canonical map `X ⟶ Spec Γ(X, ⊤)`. This is the unit of the `Γ-Spec` adjunction. -/ def _root_.AlgebraicGeometry.Scheme.toSpecΓ (X : Scheme.{u}) : X ⟶ Spec Γ(X, ⊤) := ΓSpec.adjunction.unit.app X @[simp] theorem adjunction_unit_app {X : Scheme} : ΓSpec.adjunction.unit.app X = X.toSpecΓ := rfl
Mathlib/AlgebraicGeometry/GammaSpecAdjunction.lean
392
405
/- Copyright (c) 2017 Kim Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Stephen Morgan, Kim Morrison -/ import Mathlib.CategoryTheory.Equivalence /-! # Opposite categories We provide a category instance on `Cᵒᵖ`. The morphisms `X ⟶ Y` are defined to be the morphisms `unop Y ⟶ unop X` in `C`. Here `Cᵒᵖ` is an irreducible typeclass synonym for `C` (it is the same one used in the algebra library). We also provide various mechanisms for constructing opposite morphisms, functors, and natural transformations. Unfortunately, because we do not have a definitional equality `op (op X) = X`, there are quite a few variations that are needed in practice. -/ universe v₁ v₂ u₁ u₂ -- morphism levels before object levels. See note [CategoryTheory universes]. open Opposite variable {C : Type u₁} section Quiver variable [Quiver.{v₁} C] theorem Quiver.Hom.op_inj {X Y : C} : Function.Injective (Quiver.Hom.op : (X ⟶ Y) → (Opposite.op Y ⟶ Opposite.op X)) := fun _ _ H => congr_arg Quiver.Hom.unop H theorem Quiver.Hom.unop_inj {X Y : Cᵒᵖ} : Function.Injective (Quiver.Hom.unop : (X ⟶ Y) → (Opposite.unop Y ⟶ Opposite.unop X)) := fun _ _ H => congr_arg Quiver.Hom.op H @[simp] theorem Quiver.Hom.unop_op {X Y : C} (f : X ⟶ Y) : f.op.unop = f := rfl @[simp] theorem Quiver.Hom.unop_op' {X Y : Cᵒᵖ} {x} : @Quiver.Hom.unop C _ X Y no_index (Opposite.op (unop := x)) = x := rfl @[simp] theorem Quiver.Hom.op_unop {X Y : Cᵒᵖ} (f : X ⟶ Y) : f.unop.op = f := rfl @[simp] theorem Quiver.Hom.unop_mk {X Y : Cᵒᵖ} (f : X ⟶ Y) : Quiver.Hom.unop {unop := f} = f := rfl end Quiver namespace CategoryTheory variable [Category.{v₁} C] /-- The opposite category. -/ @[stacks 001M] instance Category.opposite : Category.{v₁} Cᵒᵖ where comp f g := (g.unop ≫ f.unop).op id X := (𝟙 (unop X)).op @[simp, reassoc] theorem op_comp {X Y Z : C} {f : X ⟶ Y} {g : Y ⟶ Z} : (f ≫ g).op = g.op ≫ f.op := rfl @[simp] theorem op_id {X : C} : (𝟙 X).op = 𝟙 (op X) := rfl @[simp, reassoc] theorem unop_comp {X Y Z : Cᵒᵖ} {f : X ⟶ Y} {g : Y ⟶ Z} : (f ≫ g).unop = g.unop ≫ f.unop := rfl @[simp] theorem unop_id {X : Cᵒᵖ} : (𝟙 X).unop = 𝟙 (unop X) := rfl @[simp] theorem unop_id_op {X : C} : (𝟙 (op X)).unop = 𝟙 X := rfl @[simp] theorem op_id_unop {X : Cᵒᵖ} : (𝟙 (unop X)).op = 𝟙 X := rfl section variable (C) /-- The functor from the double-opposite of a category to the underlying category. -/ @[simps] def unopUnop : Cᵒᵖᵒᵖ ⥤ C where obj X := unop (unop X) map f := f.unop.unop /-- The functor from a category to its double-opposite. -/ @[simps] def opOp : C ⥤ Cᵒᵖᵒᵖ where obj X := op (op X) map f := f.op.op /-- The double opposite category is equivalent to the original. -/ @[simps] def opOpEquivalence : Cᵒᵖᵒᵖ ≌ C where functor := unopUnop C inverse := opOp C unitIso := Iso.refl (𝟭 Cᵒᵖᵒᵖ) counitIso := Iso.refl (opOp C ⋙ unopUnop C) instance : (opOp C).IsEquivalence := (opOpEquivalence C).isEquivalence_inverse instance : (unopUnop C).IsEquivalence := (opOpEquivalence C).isEquivalence_functor end /-- If `f` is an isomorphism, so is `f.op` -/ instance isIso_op {X Y : C} (f : X ⟶ Y) [IsIso f] : IsIso f.op := ⟨⟨(inv f).op, ⟨Quiver.Hom.unop_inj (by simp), Quiver.Hom.unop_inj (by simp)⟩⟩⟩ /-- If `f.op` is an isomorphism `f` must be too. (This cannot be an instance as it would immediately loop!) -/ theorem isIso_of_op {X Y : C} (f : X ⟶ Y) [IsIso f.op] : IsIso f := ⟨⟨(inv f.op).unop, ⟨Quiver.Hom.op_inj (by simp), Quiver.Hom.op_inj (by simp)⟩⟩⟩ theorem isIso_op_iff {X Y : C} (f : X ⟶ Y) : IsIso f.op ↔ IsIso f := ⟨fun _ => isIso_of_op _, fun _ => inferInstance⟩ theorem isIso_unop_iff {X Y : Cᵒᵖ} (f : X ⟶ Y) : IsIso f.unop ↔ IsIso f := by rw [← isIso_op_iff f.unop, Quiver.Hom.op_unop] instance isIso_unop {X Y : Cᵒᵖ} (f : X ⟶ Y) [IsIso f] : IsIso f.unop := (isIso_unop_iff _).2 inferInstance @[simp] theorem op_inv {X Y : C} (f : X ⟶ Y) [IsIso f] : (inv f).op = inv f.op := by apply IsIso.eq_inv_of_hom_inv_id rw [← op_comp, IsIso.inv_hom_id, op_id] @[simp] theorem unop_inv {X Y : Cᵒᵖ} (f : X ⟶ Y) [IsIso f] : (inv f).unop = inv f.unop := by apply IsIso.eq_inv_of_hom_inv_id rw [← unop_comp, IsIso.inv_hom_id, unop_id] namespace Functor section variable {D : Type u₂} [Category.{v₂} D] /-- The opposite of a functor, i.e. considering a functor `F : C ⥤ D` as a functor `Cᵒᵖ ⥤ Dᵒᵖ`. In informal mathematics no distinction is made between these. -/ @[simps] protected def op (F : C ⥤ D) : Cᵒᵖ ⥤ Dᵒᵖ where obj X := op (F.obj (unop X)) map f := (F.map f.unop).op /-- Given a functor `F : Cᵒᵖ ⥤ Dᵒᵖ` we can take the "unopposite" functor `F : C ⥤ D`. In informal mathematics no distinction is made between these. -/ @[simps] protected def unop (F : Cᵒᵖ ⥤ Dᵒᵖ) : C ⥤ D where obj X := unop (F.obj (op X)) map f := (F.map f.op).unop /-- The isomorphism between `F.op.unop` and `F`. -/ @[simps!] def opUnopIso (F : C ⥤ D) : F.op.unop ≅ F := NatIso.ofComponents fun _ => Iso.refl _ /-- The isomorphism between `F.unop.op` and `F`. -/ @[simps!] def unopOpIso (F : Cᵒᵖ ⥤ Dᵒᵖ) : F.unop.op ≅ F := NatIso.ofComponents fun _ => Iso.refl _ variable (C D) /-- Taking the opposite of a functor is functorial. -/ @[simps] def opHom : (C ⥤ D)ᵒᵖ ⥤ Cᵒᵖ ⥤ Dᵒᵖ where obj F := (unop F).op map α := { app := fun X => (α.unop.app (unop X)).op naturality := fun _ _ f => Quiver.Hom.unop_inj (α.unop.naturality f.unop).symm } /-- Take the "unopposite" of a functor is functorial. -/ @[simps] def opInv : (Cᵒᵖ ⥤ Dᵒᵖ) ⥤ (C ⥤ D)ᵒᵖ where obj F := op F.unop map α := Quiver.Hom.op { app := fun X => (α.app (op X)).unop naturality := fun _ _ f => Quiver.Hom.op_inj <| (α.naturality f.op).symm } variable {C D} /-- Another variant of the opposite of functor, turning a functor `C ⥤ Dᵒᵖ` into a functor `Cᵒᵖ ⥤ D`. In informal mathematics no distinction is made. -/ @[simps] protected def leftOp (F : C ⥤ Dᵒᵖ) : Cᵒᵖ ⥤ D where obj X := unop (F.obj (unop X)) map f := (F.map f.unop).unop /-- Another variant of the opposite of functor, turning a functor `Cᵒᵖ ⥤ D` into a functor `C ⥤ Dᵒᵖ`. In informal mathematics no distinction is made. -/ @[simps] protected def rightOp (F : Cᵒᵖ ⥤ D) : C ⥤ Dᵒᵖ where obj X := op (F.obj (op X)) map f := (F.map f.op).op lemma rightOp_map_unop {F : Cᵒᵖ ⥤ D} {X Y} (f : X ⟶ Y) : (F.rightOp.map f).unop = F.map f.op := rfl instance {F : C ⥤ D} [Full F] : Full F.op where map_surjective f := ⟨(F.preimage f.unop).op, by simp⟩ instance {F : C ⥤ D} [Faithful F] : Faithful F.op where map_injective h := Quiver.Hom.unop_inj <| by simpa using map_injective F (Quiver.Hom.op_inj h) /-- If F is faithful then the right_op of F is also faithful. -/ instance rightOp_faithful {F : Cᵒᵖ ⥤ D} [Faithful F] : Faithful F.rightOp where map_injective h := Quiver.Hom.op_inj (map_injective F (Quiver.Hom.op_inj h)) /-- If F is faithful then the left_op of F is also faithful. -/ instance leftOp_faithful {F : C ⥤ Dᵒᵖ} [Faithful F] : Faithful F.leftOp where map_injective h := Quiver.Hom.unop_inj (map_injective F (Quiver.Hom.unop_inj h)) instance rightOp_full {F : Cᵒᵖ ⥤ D} [Full F] : Full F.rightOp where map_surjective f := ⟨(F.preimage f.unop).unop, by simp⟩ instance leftOp_full {F : C ⥤ Dᵒᵖ} [Full F] : Full F.leftOp where map_surjective f := ⟨(F.preimage f.op).op, by simp⟩ /-- The isomorphism between `F.leftOp.rightOp` and `F`. -/ @[simps!] def leftOpRightOpIso (F : C ⥤ Dᵒᵖ) : F.leftOp.rightOp ≅ F := NatIso.ofComponents fun _ => Iso.refl _ /-- The isomorphism between `F.rightOp.leftOp` and `F`. -/ @[simps!] def rightOpLeftOpIso (F : Cᵒᵖ ⥤ D) : F.rightOp.leftOp ≅ F := NatIso.ofComponents fun _ => Iso.refl _ /-- Whenever possible, it is advisable to use the isomorphism `rightOpLeftOpIso` instead of this equality of functors. -/ theorem rightOp_leftOp_eq (F : Cᵒᵖ ⥤ D) : F.rightOp.leftOp = F := by cases F rfl end end Functor namespace NatTrans variable {D : Type u₂} [Category.{v₂} D] section variable {F G : C ⥤ D} /-- The opposite of a natural transformation. -/ @[simps] protected def op (α : F ⟶ G) : G.op ⟶ F.op where app X := (α.app (unop X)).op naturality X Y f := Quiver.Hom.unop_inj (by simp) @[simp] theorem op_id (F : C ⥤ D) : NatTrans.op (𝟙 F) = 𝟙 F.op := rfl /-- The "unopposite" of a natural transformation. -/ @[simps] protected def unop {F G : Cᵒᵖ ⥤ Dᵒᵖ} (α : F ⟶ G) : G.unop ⟶ F.unop where app X := (α.app (op X)).unop naturality X Y f := Quiver.Hom.op_inj (by simp)
@[simp] theorem unop_id (F : Cᵒᵖ ⥤ Dᵒᵖ) : NatTrans.unop (𝟙 F) = 𝟙 F.unop :=
Mathlib/CategoryTheory/Opposites.lean
293
295
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Kim Morrison -/ import Mathlib.Algebra.BigOperators.Finsupp.Basic import Mathlib.Algebra.BigOperators.Group.Finset.Preimage import Mathlib.Algebra.Module.Defs import Mathlib.Data.Rat.BigOperators /-! # Miscellaneous definitions, lemmas, and constructions using finsupp ## Main declarations * `Finsupp.graph`: the finset of input and output pairs with non-zero outputs. * `Finsupp.mapRange.equiv`: `Finsupp.mapRange` as an equiv. * `Finsupp.mapDomain`: maps the domain of a `Finsupp` by a function and by summing. * `Finsupp.comapDomain`: postcomposition of a `Finsupp` with a function injective on the preimage of its support. * `Finsupp.some`: restrict a finitely supported function on `Option α` to a finitely supported function on `α`. * `Finsupp.filter`: `filter p f` is the finitely supported function that is `f a` if `p a` is true and 0 otherwise. * `Finsupp.frange`: the image of a finitely supported function on its support. * `Finsupp.subtype_domain`: the restriction of a finitely supported function `f` to a subtype. ## Implementation notes This file is a `noncomputable theory` and uses classical logic throughout. ## TODO * This file is currently ~1600 lines long and is quite a miscellany of definitions and lemmas, so it should be divided into smaller pieces. * Expand the list of definitions and important lemmas to the module docstring. -/ noncomputable section open Finset Function variable {α β γ ι M M' N P G H R S : Type*} namespace Finsupp /-! ### Declarations about `graph` -/ section Graph variable [Zero M] /-- The graph of a finitely supported function over its support, i.e. the finset of input and output pairs with non-zero outputs. -/ def graph (f : α →₀ M) : Finset (α × M) := f.support.map ⟨fun a => Prod.mk a (f a), fun _ _ h => (Prod.mk.inj h).1⟩ theorem mk_mem_graph_iff {a : α} {m : M} {f : α →₀ M} : (a, m) ∈ f.graph ↔ f a = m ∧ m ≠ 0 := by simp_rw [graph, mem_map, mem_support_iff] constructor · rintro ⟨b, ha, rfl, -⟩ exact ⟨rfl, ha⟩ · rintro ⟨rfl, ha⟩ exact ⟨a, ha, rfl⟩ @[simp] theorem mem_graph_iff {c : α × M} {f : α →₀ M} : c ∈ f.graph ↔ f c.1 = c.2 ∧ c.2 ≠ 0 := by cases c exact mk_mem_graph_iff theorem mk_mem_graph (f : α →₀ M) {a : α} (ha : a ∈ f.support) : (a, f a) ∈ f.graph := mk_mem_graph_iff.2 ⟨rfl, mem_support_iff.1 ha⟩ theorem apply_eq_of_mem_graph {a : α} {m : M} {f : α →₀ M} (h : (a, m) ∈ f.graph) : f a = m := (mem_graph_iff.1 h).1 @[simp 1100] -- Higher priority shortcut instance for `mem_graph_iff`. theorem not_mem_graph_snd_zero (a : α) (f : α →₀ M) : (a, (0 : M)) ∉ f.graph := fun h => (mem_graph_iff.1 h).2.irrefl @[simp] theorem image_fst_graph [DecidableEq α] (f : α →₀ M) : f.graph.image Prod.fst = f.support := by classical simp only [graph, map_eq_image, image_image, Embedding.coeFn_mk, Function.comp_def, image_id'] theorem graph_injective (α M) [Zero M] : Injective (@graph α M _) := by intro f g h classical have hsup : f.support = g.support := by rw [← image_fst_graph, h, image_fst_graph] refine ext_iff'.2 ⟨hsup, fun x hx => apply_eq_of_mem_graph <| h.symm ▸ ?_⟩ exact mk_mem_graph _ (hsup ▸ hx) @[simp] theorem graph_inj {f g : α →₀ M} : f.graph = g.graph ↔ f = g := (graph_injective α M).eq_iff @[simp] theorem graph_zero : graph (0 : α →₀ M) = ∅ := by simp [graph] @[simp] theorem graph_eq_empty {f : α →₀ M} : f.graph = ∅ ↔ f = 0 := (graph_injective α M).eq_iff' graph_zero end Graph end Finsupp /-! ### Declarations about `mapRange` -/ section MapRange namespace Finsupp section Equiv variable [Zero M] [Zero N] [Zero P] /-- `Finsupp.mapRange` as an equiv. -/ @[simps apply] def mapRange.equiv (f : M ≃ N) (hf : f 0 = 0) (hf' : f.symm 0 = 0) : (α →₀ M) ≃ (α →₀ N) where toFun := (mapRange f hf : (α →₀ M) → α →₀ N) invFun := (mapRange f.symm hf' : (α →₀ N) → α →₀ M) left_inv x := by rw [← mapRange_comp _ _ _ _] <;> simp_rw [Equiv.symm_comp_self] · exact mapRange_id _ · rfl right_inv x := by rw [← mapRange_comp _ _ _ _] <;> simp_rw [Equiv.self_comp_symm] · exact mapRange_id _ · rfl @[simp] theorem mapRange.equiv_refl : mapRange.equiv (Equiv.refl M) rfl rfl = Equiv.refl (α →₀ M) := Equiv.ext mapRange_id theorem mapRange.equiv_trans (f : M ≃ N) (hf : f 0 = 0) (hf') (f₂ : N ≃ P) (hf₂ : f₂ 0 = 0) (hf₂') : (mapRange.equiv (f.trans f₂) (by rw [Equiv.trans_apply, hf, hf₂]) (by rw [Equiv.symm_trans_apply, hf₂', hf']) : (α →₀ _) ≃ _) = (mapRange.equiv f hf hf').trans (mapRange.equiv f₂ hf₂ hf₂') := Equiv.ext <| mapRange_comp f₂ hf₂ f hf ((congrArg f₂ hf).trans hf₂) @[simp] theorem mapRange.equiv_symm (f : M ≃ N) (hf hf') : ((mapRange.equiv f hf hf').symm : (α →₀ _) ≃ _) = mapRange.equiv f.symm hf' hf := Equiv.ext fun _ => rfl end Equiv section ZeroHom variable [Zero M] [Zero N] [Zero P] /-- Composition with a fixed zero-preserving homomorphism is itself a zero-preserving homomorphism on functions. -/ @[simps] def mapRange.zeroHom (f : ZeroHom M N) : ZeroHom (α →₀ M) (α →₀ N) where toFun := (mapRange f f.map_zero : (α →₀ M) → α →₀ N) map_zero' := mapRange_zero @[simp] theorem mapRange.zeroHom_id : mapRange.zeroHom (ZeroHom.id M) = ZeroHom.id (α →₀ M) := ZeroHom.ext mapRange_id theorem mapRange.zeroHom_comp (f : ZeroHom N P) (f₂ : ZeroHom M N) : (mapRange.zeroHom (f.comp f₂) : ZeroHom (α →₀ _) _) = (mapRange.zeroHom f).comp (mapRange.zeroHom f₂) := ZeroHom.ext <| mapRange_comp f (map_zero f) f₂ (map_zero f₂) (by simp only [comp_apply, map_zero]) end ZeroHom section AddMonoidHom variable [AddCommMonoid M] [AddCommMonoid N] [AddCommMonoid P] variable {F : Type*} [FunLike F M N] [AddMonoidHomClass F M N] /-- Composition with a fixed additive homomorphism is itself an additive homomorphism on functions. -/ @[simps] def mapRange.addMonoidHom (f : M →+ N) : (α →₀ M) →+ α →₀ N where toFun := (mapRange f f.map_zero : (α →₀ M) → α →₀ N) map_zero' := mapRange_zero -- Porting note: need either `dsimp only` or to specify `hf`: -- see also: https://github.com/leanprover-community/mathlib4/issues/12129 map_add' := mapRange_add (hf := f.map_zero) f.map_add @[simp] theorem mapRange.addMonoidHom_id : mapRange.addMonoidHom (AddMonoidHom.id M) = AddMonoidHom.id (α →₀ M) := AddMonoidHom.ext mapRange_id theorem mapRange.addMonoidHom_comp (f : N →+ P) (f₂ : M →+ N) : (mapRange.addMonoidHom (f.comp f₂) : (α →₀ _) →+ _) = (mapRange.addMonoidHom f).comp (mapRange.addMonoidHom f₂) := AddMonoidHom.ext <| mapRange_comp f (map_zero f) f₂ (map_zero f₂) (by simp only [comp_apply, map_zero]) @[simp] theorem mapRange.addMonoidHom_toZeroHom (f : M →+ N) : (mapRange.addMonoidHom f).toZeroHom = (mapRange.zeroHom f.toZeroHom : ZeroHom (α →₀ _) _) := ZeroHom.ext fun _ => rfl theorem mapRange_multiset_sum (f : F) (m : Multiset (α →₀ M)) : mapRange f (map_zero f) m.sum = (m.map fun x => mapRange f (map_zero f) x).sum := (mapRange.addMonoidHom (f : M →+ N) : (α →₀ _) →+ _).map_multiset_sum _ theorem mapRange_finset_sum (f : F) (s : Finset ι) (g : ι → α →₀ M) : mapRange f (map_zero f) (∑ x ∈ s, g x) = ∑ x ∈ s, mapRange f (map_zero f) (g x) := map_sum (mapRange.addMonoidHom (f : M →+ N)) _ _ /-- `Finsupp.mapRange.AddMonoidHom` as an equiv. -/ @[simps apply] def mapRange.addEquiv (f : M ≃+ N) : (α →₀ M) ≃+ (α →₀ N) := { mapRange.addMonoidHom f.toAddMonoidHom with toFun := (mapRange f f.map_zero : (α →₀ M) → α →₀ N) invFun := (mapRange f.symm f.symm.map_zero : (α →₀ N) → α →₀ M) left_inv := fun x => by rw [← mapRange_comp _ _ _ _] <;> simp_rw [AddEquiv.symm_comp_self] · exact mapRange_id _ · rfl right_inv := fun x => by rw [← mapRange_comp _ _ _ _] <;> simp_rw [AddEquiv.self_comp_symm] · exact mapRange_id _ · rfl } @[simp] theorem mapRange.addEquiv_refl : mapRange.addEquiv (AddEquiv.refl M) = AddEquiv.refl (α →₀ M) := AddEquiv.ext mapRange_id theorem mapRange.addEquiv_trans (f : M ≃+ N) (f₂ : N ≃+ P) : (mapRange.addEquiv (f.trans f₂) : (α →₀ M) ≃+ (α →₀ P)) = (mapRange.addEquiv f).trans (mapRange.addEquiv f₂) := AddEquiv.ext (mapRange_comp _ f₂.map_zero _ f.map_zero (by simp)) @[simp] theorem mapRange.addEquiv_symm (f : M ≃+ N) : ((mapRange.addEquiv f).symm : (α →₀ _) ≃+ _) = mapRange.addEquiv f.symm := AddEquiv.ext fun _ => rfl @[simp] theorem mapRange.addEquiv_toAddMonoidHom (f : M ≃+ N) : ((mapRange.addEquiv f : (α →₀ _) ≃+ _) : _ →+ _) = (mapRange.addMonoidHom f.toAddMonoidHom : (α →₀ _) →+ _) := AddMonoidHom.ext fun _ => rfl @[simp] theorem mapRange.addEquiv_toEquiv (f : M ≃+ N) : ↑(mapRange.addEquiv f : (α →₀ _) ≃+ _) = (mapRange.equiv (f : M ≃ N) f.map_zero f.symm.map_zero : (α →₀ _) ≃ _) := Equiv.ext fun _ => rfl end AddMonoidHom end Finsupp end MapRange /-! ### Declarations about `equivCongrLeft` -/ section EquivCongrLeft variable [Zero M] namespace Finsupp /-- Given `f : α ≃ β`, we can map `l : α →₀ M` to `equivMapDomain f l : β →₀ M` (computably) by mapping the support forwards and the function backwards. -/ def equivMapDomain (f : α ≃ β) (l : α →₀ M) : β →₀ M where support := l.support.map f.toEmbedding toFun a := l (f.symm a) mem_support_toFun a := by simp only [Finset.mem_map_equiv, mem_support_toFun]; rfl @[simp] theorem equivMapDomain_apply (f : α ≃ β) (l : α →₀ M) (b : β) : equivMapDomain f l b = l (f.symm b) := rfl theorem equivMapDomain_symm_apply (f : α ≃ β) (l : β →₀ M) (a : α) : equivMapDomain f.symm l a = l (f a) := rfl @[simp] theorem equivMapDomain_refl (l : α →₀ M) : equivMapDomain (Equiv.refl _) l = l := by ext x; rfl theorem equivMapDomain_refl' : equivMapDomain (Equiv.refl _) = @id (α →₀ M) := by ext x; rfl theorem equivMapDomain_trans (f : α ≃ β) (g : β ≃ γ) (l : α →₀ M) : equivMapDomain (f.trans g) l = equivMapDomain g (equivMapDomain f l) := by ext x; rfl theorem equivMapDomain_trans' (f : α ≃ β) (g : β ≃ γ) : @equivMapDomain _ _ M _ (f.trans g) = equivMapDomain g ∘ equivMapDomain f := by ext x; rfl @[simp] theorem equivMapDomain_single (f : α ≃ β) (a : α) (b : M) : equivMapDomain f (single a b) = single (f a) b := by classical ext x simp only [single_apply, Equiv.apply_eq_iff_eq_symm_apply, equivMapDomain_apply] @[simp] theorem equivMapDomain_zero {f : α ≃ β} : equivMapDomain f (0 : α →₀ M) = (0 : β →₀ M) := by ext; simp only [equivMapDomain_apply, coe_zero, Pi.zero_apply] @[to_additive (attr := simp)] theorem prod_equivMapDomain [CommMonoid N] (f : α ≃ β) (l : α →₀ M) (g : β → M → N) : prod (equivMapDomain f l) g = prod l (fun a m => g (f a) m) := by simp [prod, equivMapDomain] /-- Given `f : α ≃ β`, the finitely supported function spaces are also in bijection: `(α →₀ M) ≃ (β →₀ M)`. This is the finitely-supported version of `Equiv.piCongrLeft`. -/ def equivCongrLeft (f : α ≃ β) : (α →₀ M) ≃ (β →₀ M) := by refine ⟨equivMapDomain f, equivMapDomain f.symm, fun f => ?_, fun f => ?_⟩ <;> ext x <;> simp only [equivMapDomain_apply, Equiv.symm_symm, Equiv.symm_apply_apply, Equiv.apply_symm_apply] @[simp] theorem equivCongrLeft_apply (f : α ≃ β) (l : α →₀ M) : equivCongrLeft f l = equivMapDomain f l := rfl @[simp] theorem equivCongrLeft_symm (f : α ≃ β) : (@equivCongrLeft _ _ M _ f).symm = equivCongrLeft f.symm := rfl end Finsupp end EquivCongrLeft section CastFinsupp variable [Zero M] (f : α →₀ M) namespace Nat @[simp, norm_cast] theorem cast_finsuppProd [CommSemiring R] (g : α → M → ℕ) : (↑(f.prod g) : R) = f.prod fun a b => ↑(g a b) := Nat.cast_prod _ _ @[deprecated (since := "2025-04-06")] alias cast_finsupp_prod := cast_finsuppProd @[simp, norm_cast] theorem cast_finsupp_sum [AddCommMonoidWithOne R] (g : α → M → ℕ) : (↑(f.sum g) : R) = f.sum fun a b => ↑(g a b) := Nat.cast_sum _ _ end Nat namespace Int @[simp, norm_cast] theorem cast_finsuppProd [CommRing R] (g : α → M → ℤ) : (↑(f.prod g) : R) = f.prod fun a b => ↑(g a b) := Int.cast_prod _ _ @[deprecated (since := "2025-04-06")] alias cast_finsupp_prod := cast_finsuppProd @[simp, norm_cast] theorem cast_finsupp_sum [AddCommGroupWithOne R] (g : α → M → ℤ) : (↑(f.sum g) : R) = f.sum fun a b => ↑(g a b) := Int.cast_sum _ _ end Int namespace Rat @[simp, norm_cast] theorem cast_finsupp_sum [DivisionRing R] [CharZero R] (g : α → M → ℚ) : (↑(f.sum g) : R) = f.sum fun a b => ↑(g a b) := cast_sum _ _ @[simp, norm_cast] theorem cast_finsuppProd [Field R] [CharZero R] (g : α → M → ℚ) : (↑(f.prod g) : R) = f.prod fun a b => ↑(g a b) := cast_prod _ _ @[deprecated (since := "2025-04-06")] alias cast_finsupp_prod := cast_finsuppProd end Rat end CastFinsupp /-! ### Declarations about `mapDomain` -/ namespace Finsupp section MapDomain variable [AddCommMonoid M] {v v₁ v₂ : α →₀ M} /-- Given `f : α → β` and `v : α →₀ M`, `mapDomain f v : β →₀ M` is the finitely supported function whose value at `a : β` is the sum of `v x` over all `x` such that `f x = a`. -/ def mapDomain (f : α → β) (v : α →₀ M) : β →₀ M := v.sum fun a => single (f a) theorem mapDomain_apply {f : α → β} (hf : Function.Injective f) (x : α →₀ M) (a : α) : mapDomain f x (f a) = x a := by rw [mapDomain, sum_apply, sum_eq_single a, single_eq_same] · intro b _ hba exact single_eq_of_ne (hf.ne hba) · intro _ rw [single_zero, coe_zero, Pi.zero_apply] theorem mapDomain_notin_range {f : α → β} (x : α →₀ M) (a : β) (h : a ∉ Set.range f) : mapDomain f x a = 0 := by rw [mapDomain, sum_apply, sum] exact Finset.sum_eq_zero fun a' _ => single_eq_of_ne fun eq => h <| eq ▸ Set.mem_range_self _ @[simp] theorem mapDomain_id : mapDomain id v = v := sum_single _ theorem mapDomain_comp {f : α → β} {g : β → γ} : mapDomain (g ∘ f) v = mapDomain g (mapDomain f v) := by refine ((sum_sum_index ?_ ?_).trans ?_).symm · intro exact single_zero _ · intro exact single_add _ refine sum_congr fun _ _ => sum_single_index ?_ exact single_zero _ @[simp] theorem mapDomain_single {f : α → β} {a : α} {b : M} : mapDomain f (single a b) = single (f a) b := sum_single_index <| single_zero _ @[simp] theorem mapDomain_zero {f : α → β} : mapDomain f (0 : α →₀ M) = (0 : β →₀ M) := sum_zero_index theorem mapDomain_congr {f g : α → β} (h : ∀ x ∈ v.support, f x = g x) : v.mapDomain f = v.mapDomain g := Finset.sum_congr rfl fun _ H => by simp only [h _ H] theorem mapDomain_add {f : α → β} : mapDomain f (v₁ + v₂) = mapDomain f v₁ + mapDomain f v₂ := sum_add_index' (fun _ => single_zero _) fun _ => single_add _ @[simp] theorem mapDomain_equiv_apply {f : α ≃ β} (x : α →₀ M) (a : β) : mapDomain f x a = x (f.symm a) := by conv_lhs => rw [← f.apply_symm_apply a] exact mapDomain_apply f.injective _ _ /-- `Finsupp.mapDomain` is an `AddMonoidHom`. -/ @[simps] def mapDomain.addMonoidHom (f : α → β) : (α →₀ M) →+ β →₀ M where toFun := mapDomain f map_zero' := mapDomain_zero map_add' _ _ := mapDomain_add @[simp] theorem mapDomain.addMonoidHom_id : mapDomain.addMonoidHom id = AddMonoidHom.id (α →₀ M) := AddMonoidHom.ext fun _ => mapDomain_id theorem mapDomain.addMonoidHom_comp (f : β → γ) (g : α → β) : (mapDomain.addMonoidHom (f ∘ g) : (α →₀ M) →+ γ →₀ M) = (mapDomain.addMonoidHom f).comp (mapDomain.addMonoidHom g) := AddMonoidHom.ext fun _ => mapDomain_comp theorem mapDomain_finset_sum {f : α → β} {s : Finset ι} {v : ι → α →₀ M} : mapDomain f (∑ i ∈ s, v i) = ∑ i ∈ s, mapDomain f (v i) := map_sum (mapDomain.addMonoidHom f) _ _ theorem mapDomain_sum [Zero N] {f : α → β} {s : α →₀ N} {v : α → N → α →₀ M} : mapDomain f (s.sum v) = s.sum fun a b => mapDomain f (v a b) := map_finsuppSum (mapDomain.addMonoidHom f : (α →₀ M) →+ β →₀ M) _ _ theorem mapDomain_support [DecidableEq β] {f : α → β} {s : α →₀ M} : (s.mapDomain f).support ⊆ s.support.image f := Finset.Subset.trans support_sum <| Finset.Subset.trans (Finset.biUnion_mono fun _ _ => support_single_subset) <| by rw [Finset.biUnion_singleton] theorem mapDomain_apply' (S : Set α) {f : α → β} (x : α →₀ M) (hS : (x.support : Set α) ⊆ S) (hf : Set.InjOn f S) {a : α} (ha : a ∈ S) : mapDomain f x (f a) = x a := by classical rw [mapDomain, sum_apply, sum] simp_rw [single_apply] by_cases hax : a ∈ x.support · rw [← Finset.add_sum_erase _ _ hax, if_pos rfl] convert add_zero (x a) refine Finset.sum_eq_zero fun i hi => if_neg ?_ exact (hf.mono hS).ne (Finset.mem_of_mem_erase hi) hax (Finset.ne_of_mem_erase hi) · rw [not_mem_support_iff.1 hax] refine Finset.sum_eq_zero fun i hi => if_neg ?_ exact hf.ne (hS hi) ha (ne_of_mem_of_not_mem hi hax) theorem mapDomain_support_of_injOn [DecidableEq β] {f : α → β} (s : α →₀ M) (hf : Set.InjOn f s.support) : (mapDomain f s).support = Finset.image f s.support := Finset.Subset.antisymm mapDomain_support <| by intro x hx simp only [mem_image, exists_prop, mem_support_iff, Ne] at hx rcases hx with ⟨hx_w, hx_h_left, rfl⟩ simp only [mem_support_iff, Ne] rw [mapDomain_apply' (↑s.support : Set _) _ _ hf] · exact hx_h_left · simp only [mem_coe, mem_support_iff, Ne] exact hx_h_left · exact Subset.refl _ theorem mapDomain_support_of_injective [DecidableEq β] {f : α → β} (hf : Function.Injective f) (s : α →₀ M) : (mapDomain f s).support = Finset.image f s.support := mapDomain_support_of_injOn s hf.injOn @[to_additive] theorem prod_mapDomain_index [CommMonoid N] {f : α → β} {s : α →₀ M} {h : β → M → N} (h_zero : ∀ b, h b 0 = 1) (h_add : ∀ b m₁ m₂, h b (m₁ + m₂) = h b m₁ * h b m₂) : (mapDomain f s).prod h = s.prod fun a m => h (f a) m := (prod_sum_index h_zero h_add).trans <| prod_congr fun _ _ => prod_single_index (h_zero _) -- Note that in `prod_mapDomain_index`, `M` is still an additive monoid, -- so there is no analogous version in terms of `MonoidHom`. /-- A version of `sum_mapDomain_index` that takes a bundled `AddMonoidHom`, rather than separate linearity hypotheses. -/ @[simp] theorem sum_mapDomain_index_addMonoidHom [AddCommMonoid N] {f : α → β} {s : α →₀ M} (h : β → M →+ N) : ((mapDomain f s).sum fun b m => h b m) = s.sum fun a m => h (f a) m := sum_mapDomain_index (fun b => (h b).map_zero) (fun b _ _ => (h b).map_add _ _) theorem embDomain_eq_mapDomain (f : α ↪ β) (v : α →₀ M) : embDomain f v = mapDomain f v := by ext a by_cases h : a ∈ Set.range f · rcases h with ⟨a, rfl⟩ rw [mapDomain_apply f.injective, embDomain_apply] · rw [mapDomain_notin_range, embDomain_notin_range] <;> assumption @[to_additive] theorem prod_mapDomain_index_inj [CommMonoid N] {f : α → β} {s : α →₀ M} {h : β → M → N} (hf : Function.Injective f) : (s.mapDomain f).prod h = s.prod fun a b => h (f a) b := by rw [← Function.Embedding.coeFn_mk f hf, ← embDomain_eq_mapDomain, prod_embDomain] theorem mapDomain_injective {f : α → β} (hf : Function.Injective f) : Function.Injective (mapDomain f : (α →₀ M) → β →₀ M) := by intro v₁ v₂ eq ext a have : mapDomain f v₁ (f a) = mapDomain f v₂ (f a) := by rw [eq] rwa [mapDomain_apply hf, mapDomain_apply hf] at this /-- When `f` is an embedding we have an embedding `(α →₀ ℕ) ↪ (β →₀ ℕ)` given by `mapDomain`. -/ @[simps] def mapDomainEmbedding {α β : Type*} (f : α ↪ β) : (α →₀ ℕ) ↪ β →₀ ℕ := ⟨Finsupp.mapDomain f, Finsupp.mapDomain_injective f.injective⟩ theorem mapDomain.addMonoidHom_comp_mapRange [AddCommMonoid N] (f : α → β) (g : M →+ N) : (mapDomain.addMonoidHom f).comp (mapRange.addMonoidHom g) = (mapRange.addMonoidHom g).comp (mapDomain.addMonoidHom f) := by ext simp only [AddMonoidHom.coe_comp, Finsupp.mapRange_single, Finsupp.mapDomain.addMonoidHom_apply, Finsupp.singleAddHom_apply, eq_self_iff_true, Function.comp_apply, Finsupp.mapDomain_single, Finsupp.mapRange.addMonoidHom_apply] /-- When `g` preserves addition, `mapRange` and `mapDomain` commute. -/ theorem mapDomain_mapRange [AddCommMonoid N] (f : α → β) (v : α →₀ M) (g : M → N) (h0 : g 0 = 0) (hadd : ∀ x y, g (x + y) = g x + g y) : mapDomain f (mapRange g h0 v) = mapRange g h0 (mapDomain f v) := let g' : M →+ N := { toFun := g map_zero' := h0 map_add' := hadd } DFunLike.congr_fun (mapDomain.addMonoidHom_comp_mapRange f g') v theorem sum_update_add [AddZeroClass α] [AddCommMonoid β] (f : ι →₀ α) (i : ι) (a : α) (g : ι → α → β) (hg : ∀ i, g i 0 = 0) (hgg : ∀ (j : ι) (a₁ a₂ : α), g j (a₁ + a₂) = g j a₁ + g j a₂) : (f.update i a).sum g + g i (f i) = f.sum g + g i a := by rw [update_eq_erase_add_single, sum_add_index' hg hgg] conv_rhs => rw [← Finsupp.update_self f i] rw [update_eq_erase_add_single, sum_add_index' hg hgg, add_assoc, add_assoc] congr 1 rw [add_comm, sum_single_index (hg _), sum_single_index (hg _)] theorem mapDomain_injOn (S : Set α) {f : α → β} (hf : Set.InjOn f S) : Set.InjOn (mapDomain f : (α →₀ M) → β →₀ M) { w | (w.support : Set α) ⊆ S } := by intro v₁ hv₁ v₂ hv₂ eq ext a classical by_cases h : a ∈ v₁.support ∪ v₂.support · rw [← mapDomain_apply' S _ hv₁ hf _, ← mapDomain_apply' S _ hv₂ hf _, eq] <;> · apply Set.union_subset hv₁ hv₂ exact mod_cast h · simp only [not_or, mem_union, not_not, mem_support_iff] at h simp [h] theorem equivMapDomain_eq_mapDomain {M} [AddCommMonoid M] (f : α ≃ β) (l : α →₀ M) : equivMapDomain f l = mapDomain f l := by ext x; simp [mapDomain_equiv_apply] end MapDomain /-! ### Declarations about `comapDomain` -/ section ComapDomain /-- Given `f : α → β`, `l : β →₀ M` and a proof `hf` that `f` is injective on the preimage of `l.support`, `comapDomain f l hf` is the finitely supported function from `α` to `M` given by composing `l` with `f`. -/ @[simps support] def comapDomain [Zero M] (f : α → β) (l : β →₀ M) (hf : Set.InjOn f (f ⁻¹' ↑l.support)) : α →₀ M where support := l.support.preimage f hf toFun a := l (f a) mem_support_toFun := by intro a simp only [Finset.mem_def.symm, Finset.mem_preimage] exact l.mem_support_toFun (f a) @[simp] theorem comapDomain_apply [Zero M] (f : α → β) (l : β →₀ M) (hf : Set.InjOn f (f ⁻¹' ↑l.support)) (a : α) : comapDomain f l hf a = l (f a) := rfl theorem sum_comapDomain [Zero M] [AddCommMonoid N] (f : α → β) (l : β →₀ M) (g : β → M → N) (hf : Set.BijOn f (f ⁻¹' ↑l.support) ↑l.support) : (comapDomain f l hf.injOn).sum (g ∘ f) = l.sum g := by simp only [sum, comapDomain_apply, (· ∘ ·), comapDomain] exact Finset.sum_preimage_of_bij f _ hf fun x => g x (l x) theorem eq_zero_of_comapDomain_eq_zero [Zero M] (f : α → β) (l : β →₀ M) (hf : Set.BijOn f (f ⁻¹' ↑l.support) ↑l.support) : comapDomain f l hf.injOn = 0 → l = 0 := by rw [← support_eq_empty, ← support_eq_empty, comapDomain] simp only [Finset.ext_iff, Finset.not_mem_empty, iff_false, mem_preimage] intro h a ha obtain ⟨b, hb⟩ := hf.2.2 ha exact h b (hb.2.symm ▸ ha) section FInjective section Zero variable [Zero M] lemma embDomain_comapDomain {f : α ↪ β} {g : β →₀ M} (hg : ↑g.support ⊆ Set.range f) : embDomain f (comapDomain f g f.injective.injOn) = g := by ext b by_cases hb : b ∈ Set.range f · obtain ⟨a, rfl⟩ := hb rw [embDomain_apply, comapDomain_apply] · replace hg : g b = 0 := not_mem_support_iff.mp <| mt (hg ·) hb rw [embDomain_notin_range _ _ _ hb, hg] /-- Note the `hif` argument is needed for this to work in `rw`. -/ @[simp] theorem comapDomain_zero (f : α → β) (hif : Set.InjOn f (f ⁻¹' ↑(0 : β →₀ M).support) := Finset.coe_empty ▸ (Set.injOn_empty f)) : comapDomain f (0 : β →₀ M) hif = (0 : α →₀ M) := by ext rfl @[simp] theorem comapDomain_single (f : α → β) (a : α) (m : M) (hif : Set.InjOn f (f ⁻¹' (single (f a) m).support)) : comapDomain f (Finsupp.single (f a) m) hif = Finsupp.single a m := by rcases eq_or_ne m 0 with (rfl | hm) · simp only [single_zero, comapDomain_zero] · rw [eq_single_iff, comapDomain_apply, comapDomain_support, ← Finset.coe_subset, coe_preimage, support_single_ne_zero _ hm, coe_singleton, coe_singleton, single_eq_same] rw [support_single_ne_zero _ hm, coe_singleton] at hif exact ⟨fun x hx => hif hx rfl hx, rfl⟩ end Zero section AddZeroClass variable [AddZeroClass M] {f : α → β} theorem comapDomain_add (v₁ v₂ : β →₀ M) (hv₁ : Set.InjOn f (f ⁻¹' ↑v₁.support)) (hv₂ : Set.InjOn f (f ⁻¹' ↑v₂.support)) (hv₁₂ : Set.InjOn f (f ⁻¹' ↑(v₁ + v₂).support)) : comapDomain f (v₁ + v₂) hv₁₂ = comapDomain f v₁ hv₁ + comapDomain f v₂ hv₂ := by ext simp only [comapDomain_apply, coe_add, Pi.add_apply] /-- A version of `Finsupp.comapDomain_add` that's easier to use. -/ theorem comapDomain_add_of_injective (hf : Function.Injective f) (v₁ v₂ : β →₀ M) : comapDomain f (v₁ + v₂) hf.injOn = comapDomain f v₁ hf.injOn + comapDomain f v₂ hf.injOn := comapDomain_add _ _ _ _ _ /-- `Finsupp.comapDomain` is an `AddMonoidHom`. -/ @[simps] def comapDomain.addMonoidHom (hf : Function.Injective f) : (β →₀ M) →+ α →₀ M where toFun x := comapDomain f x hf.injOn map_zero' := comapDomain_zero f map_add' := comapDomain_add_of_injective hf end AddZeroClass variable [AddCommMonoid M] (f : α → β) theorem mapDomain_comapDomain (hf : Function.Injective f) (l : β →₀ M) (hl : ↑l.support ⊆ Set.range f) : mapDomain f (comapDomain f l hf.injOn) = l := by conv_rhs => rw [← embDomain_comapDomain (f := ⟨f, hf⟩) hl (M := M), embDomain_eq_mapDomain] rfl end FInjective end ComapDomain /-! ### Declarations about finitely supported functions whose support is an `Option` type -/ section Option /-- Restrict a finitely supported function on `Option α` to a finitely supported function on `α`. -/ def some [Zero M] (f : Option α →₀ M) : α →₀ M := f.comapDomain Option.some fun _ => by simp @[simp] theorem some_apply [Zero M] (f : Option α →₀ M) (a : α) : f.some a = f (Option.some a) := rfl @[simp] theorem some_zero [Zero M] : (0 : Option α →₀ M).some = 0 := by ext simp @[simp] theorem some_add [AddZeroClass M] (f g : Option α →₀ M) : (f + g).some = f.some + g.some := by ext simp @[simp] theorem some_single_none [Zero M] (m : M) : (single none m : Option α →₀ M).some = 0 := by ext simp @[simp] theorem some_single_some [Zero M] (a : α) (m : M) : (single (Option.some a) m : Option α →₀ M).some = single a m := by classical ext b simp [single_apply] @[to_additive] theorem prod_option_index [AddZeroClass M] [CommMonoid N] (f : Option α →₀ M) (b : Option α → M → N) (h_zero : ∀ o, b o 0 = 1) (h_add : ∀ o m₁ m₂, b o (m₁ + m₂) = b o m₁ * b o m₂) : f.prod b = b none (f none) * f.some.prod fun a => b (Option.some a) := by classical induction f using induction_linear with | zero => simp [some_zero, h_zero] | add f₁ f₂ h₁ h₂ => rw [Finsupp.prod_add_index, h₁, h₂, some_add, Finsupp.prod_add_index] · simp only [h_add, Pi.add_apply, Finsupp.coe_add] rw [mul_mul_mul_comm] all_goals simp [h_zero, h_add] | single a m => cases a <;> simp [h_zero, h_add] theorem sum_option_index_smul [Semiring R] [AddCommMonoid M] [Module R M] (f : Option α →₀ R) (b : Option α → M) : (f.sum fun o r => r • b o) = f none • b none + f.some.sum fun a r => r • b (Option.some a) := f.sum_option_index _ (fun _ => zero_smul _ _) fun _ _ _ => add_smul _ _ _ theorem eq_option_embedding_update_none_iff [Zero M] {n : Option α →₀ M} {m : α →₀ M} {i : M} : (n = (embDomain Embedding.some m).update none i) ↔ n none = i ∧ n.some = m := by classical rw [Finsupp.ext_iff, Option.forall, Finsupp.ext_iff] apply and_congr · simp · apply forall_congr' intro simp only [coe_update, ne_eq, reduceCtorEq, not_false_eq_true, update_of_ne, some_apply] rw [← Embedding.some_apply, embDomain_apply, Embedding.some_apply] @[simp] lemma some_embDomain_some [Zero M] (f : α →₀ M) : (f.embDomain .some).some = f := by ext; rw [some_apply]; exact embDomain_apply _ _ _ @[simp] lemma embDomain_some_none [Zero M] (f : α →₀ M) : f.embDomain .some .none = 0 := embDomain_notin_range _ _ _ (by simp) end Option /-! ### Declarations about `Finsupp.filter` -/ section Filter section Zero variable [Zero M] (p : α → Prop) [DecidablePred p] (f : α →₀ M) /-- `Finsupp.filter p f` is the finitely supported function that is `f a` if `p a` is true and `0` otherwise. -/ def filter (p : α → Prop) [DecidablePred p] (f : α →₀ M) : α →₀ M where toFun a := if p a then f a else 0 support := f.support.filter p mem_support_toFun a := by split_ifs with h <;> · simp only [h, mem_filter, mem_support_iff] tauto theorem filter_apply (a : α) : f.filter p a = if p a then f a else 0 := rfl theorem filter_eq_indicator : ⇑(f.filter p) = Set.indicator { x | p x } f := by ext simp [filter_apply, Set.indicator_apply] theorem filter_eq_zero_iff : f.filter p = 0 ↔ ∀ x, p x → f x = 0 := by simp only [DFunLike.ext_iff, filter_eq_indicator, zero_apply, Set.indicator_apply_eq_zero, Set.mem_setOf_eq] theorem filter_eq_self_iff : f.filter p = f ↔ ∀ x, f x ≠ 0 → p x := by simp only [DFunLike.ext_iff, filter_eq_indicator, Set.indicator_apply_eq_self, Set.mem_setOf_eq, not_imp_comm] @[simp] theorem filter_apply_pos {a : α} (h : p a) : f.filter p a = f a := if_pos h @[simp] theorem filter_apply_neg {a : α} (h : ¬p a) : f.filter p a = 0 := if_neg h @[simp] theorem support_filter : (f.filter p).support = {x ∈ f.support | p x} := rfl theorem filter_zero : (0 : α →₀ M).filter p = 0 := by classical rw [← support_eq_empty, support_filter, support_zero, Finset.filter_empty] @[simp] theorem filter_single_of_pos {a : α} {b : M} (h : p a) : (single a b).filter p = single a b := (filter_eq_self_iff _ _).2 fun _ hx => (single_apply_ne_zero.1 hx).1.symm ▸ h @[simp] theorem filter_single_of_neg {a : α} {b : M} (h : ¬p a) : (single a b).filter p = 0 := (filter_eq_zero_iff _ _).2 fun _ hpx => single_apply_eq_zero.2 fun hxa => absurd hpx (hxa.symm ▸ h) @[to_additive] theorem prod_filter_index [CommMonoid N] (g : α → M → N) : (f.filter p).prod g = ∏ x ∈ (f.filter p).support, g x (f x) := by classical refine Finset.prod_congr rfl fun x hx => ?_ rw [support_filter, Finset.mem_filter] at hx rw [filter_apply_pos _ _ hx.2] @[to_additive (attr := simp)] theorem prod_filter_mul_prod_filter_not [CommMonoid N] (g : α → M → N) : (f.filter p).prod g * (f.filter fun a => ¬p a).prod g = f.prod g := by classical simp_rw [prod_filter_index, support_filter, Finset.prod_filter_mul_prod_filter_not, Finsupp.prod] @[to_additive (attr := simp)] theorem prod_div_prod_filter [CommGroup G] (g : α → M → G) : f.prod g / (f.filter p).prod g = (f.filter fun a => ¬p a).prod g := div_eq_of_eq_mul' (prod_filter_mul_prod_filter_not _ _ _).symm end Zero theorem filter_pos_add_filter_neg [AddZeroClass M] (f : α →₀ M) (p : α → Prop) [DecidablePred p] : (f.filter p + f.filter fun a => ¬p a) = f := DFunLike.coe_injective <| by simp only [coe_add, filter_eq_indicator] exact Set.indicator_self_add_compl { x | p x } f end Filter /-! ### Declarations about `frange` -/ section Frange variable [Zero M] /-- `frange f` is the image of `f` on the support of `f`. -/ def frange (f : α →₀ M) : Finset M := haveI := Classical.decEq M Finset.image f f.support theorem mem_frange {f : α →₀ M} {y : M} : y ∈ f.frange ↔ y ≠ 0 ∧ ∃ x, f x = y := by rw [frange, @Finset.mem_image _ _ (Classical.decEq _) _ f.support] exact ⟨fun ⟨x, hx1, hx2⟩ => ⟨hx2 ▸ mem_support_iff.1 hx1, x, hx2⟩, fun ⟨hy, x, hx⟩ => ⟨x, mem_support_iff.2 (hx.symm ▸ hy), hx⟩⟩ theorem zero_not_mem_frange {f : α →₀ M} : (0 : M) ∉ f.frange := fun H => (mem_frange.1 H).1 rfl theorem frange_single {x : α} {y : M} : frange (single x y) ⊆ {y} := fun r hr => let ⟨t, ht1, ht2⟩ := mem_frange.1 hr ht2 ▸ by classical rw [single_apply] at ht2 ⊢ split_ifs at ht2 ⊢ · exact Finset.mem_singleton_self _ · exact (t ht2.symm).elim end Frange /-! ### Declarations about `Finsupp.subtypeDomain` -/ section SubtypeDomain section Zero variable [Zero M] {p : α → Prop} /-- `subtypeDomain p f` is the restriction of the finitely supported function `f` to subtype `p`. -/ def subtypeDomain (p : α → Prop) (f : α →₀ M) : Subtype p →₀ M where support := haveI := Classical.decPred p f.support.subtype p toFun := f ∘ Subtype.val mem_support_toFun a := by simp only [@mem_subtype _ _ (Classical.decPred p), mem_support_iff]; rfl @[simp] theorem support_subtypeDomain [D : DecidablePred p] {f : α →₀ M} : (subtypeDomain p f).support = f.support.subtype p := by rw [Subsingleton.elim D] <;> rfl @[simp] theorem subtypeDomain_apply {a : Subtype p} {v : α →₀ M} : (subtypeDomain p v) a = v a.val := rfl @[simp] theorem subtypeDomain_zero : subtypeDomain p (0 : α →₀ M) = 0 := rfl theorem subtypeDomain_eq_iff_forall {f g : α →₀ M} : f.subtypeDomain p = g.subtypeDomain p ↔ ∀ x, p x → f x = g x := by simp_rw [DFunLike.ext_iff, subtypeDomain_apply, Subtype.forall] theorem subtypeDomain_eq_iff {f g : α →₀ M} (hf : ∀ x ∈ f.support, p x) (hg : ∀ x ∈ g.support, p x) : f.subtypeDomain p = g.subtypeDomain p ↔ f = g := subtypeDomain_eq_iff_forall.trans ⟨fun H ↦ Finsupp.ext fun _a ↦ (em _).elim (H _ <| hf _ ·) fun haf ↦ (em _).elim (H _ <| hg _ ·) fun hag ↦ (not_mem_support_iff.mp haf).trans (not_mem_support_iff.mp hag).symm, fun H _ _ ↦ congr($H _)⟩ theorem subtypeDomain_eq_zero_iff' {f : α →₀ M} : f.subtypeDomain p = 0 ↔ ∀ x, p x → f x = 0 := subtypeDomain_eq_iff_forall (g := 0) theorem subtypeDomain_eq_zero_iff {f : α →₀ M} (hf : ∀ x ∈ f.support, p x) : f.subtypeDomain p = 0 ↔ f = 0 := subtypeDomain_eq_iff (g := 0) hf (by simp) @[to_additive] theorem prod_subtypeDomain_index [CommMonoid N] {v : α →₀ M} {h : α → M → N} (hp : ∀ x ∈ v.support, p x) : (v.subtypeDomain p).prod (fun a b ↦ h a b) = v.prod h := by refine Finset.prod_bij (fun p _ ↦ p) ?_ ?_ ?_ ?_ <;> aesop end Zero section AddZeroClass variable [AddZeroClass M] {p : α → Prop} {v v' : α →₀ M} @[simp] theorem subtypeDomain_add {v v' : α →₀ M} : (v + v').subtypeDomain p = v.subtypeDomain p + v'.subtypeDomain p := ext fun _ => rfl /-- `subtypeDomain` but as an `AddMonoidHom`. -/ def subtypeDomainAddMonoidHom : (α →₀ M) →+ Subtype p →₀ M where toFun := subtypeDomain p map_zero' := subtypeDomain_zero map_add' _ _ := subtypeDomain_add /-- `Finsupp.filter` as an `AddMonoidHom`. -/ def filterAddHom (p : α → Prop) [DecidablePred p] : (α →₀ M) →+ α →₀ M where toFun := filter p map_zero' := filter_zero p map_add' f g := DFunLike.coe_injective <| by simp only [filter_eq_indicator, coe_add] exact Set.indicator_add { x | p x } f g @[simp] theorem filter_add [DecidablePred p] {v v' : α →₀ M} : (v + v').filter p = v.filter p + v'.filter p := (filterAddHom p).map_add v v' end AddZeroClass section CommMonoid variable [AddCommMonoid M] {p : α → Prop} theorem subtypeDomain_sum {s : Finset ι} {h : ι → α →₀ M} : (∑ c ∈ s, h c).subtypeDomain p = ∑ c ∈ s, (h c).subtypeDomain p := map_sum subtypeDomainAddMonoidHom _ s theorem subtypeDomain_finsupp_sum [Zero N] {s : β →₀ N} {h : β → N → α →₀ M} : (s.sum h).subtypeDomain p = s.sum fun c d => (h c d).subtypeDomain p := subtypeDomain_sum theorem filter_sum [DecidablePred p] (s : Finset ι) (f : ι → α →₀ M) : (∑ a ∈ s, f a).filter p = ∑ a ∈ s, filter p (f a) := map_sum (filterAddHom p) f s theorem filter_eq_sum (p : α → Prop) [DecidablePred p] (f : α →₀ M) : f.filter p = ∑ i ∈ f.support.filter p, single i (f i) := (f.filter p).sum_single.symm.trans <| Finset.sum_congr rfl fun x hx => by rw [filter_apply_pos _ _ (mem_filter.1 hx).2] end CommMonoid section Group variable [AddGroup G] {p : α → Prop} {v v' : α →₀ G} @[simp] theorem subtypeDomain_neg : (-v).subtypeDomain p = -v.subtypeDomain p := ext fun _ => rfl @[simp] theorem subtypeDomain_sub : (v - v').subtypeDomain p = v.subtypeDomain p - v'.subtypeDomain p := ext fun _ => rfl @[simp] theorem filter_neg (p : α → Prop) [DecidablePred p] (f : α →₀ G) : filter p (-f) = -filter p f := (filterAddHom p : (_ →₀ G) →+ _).map_neg f @[simp] theorem filter_sub (p : α → Prop) [DecidablePred p] (f₁ f₂ : α →₀ G) : filter p (f₁ - f₂) = filter p f₁ - filter p f₂ := (filterAddHom p : (_ →₀ G) →+ _).map_sub f₁ f₂ end Group end SubtypeDomain theorem mem_support_multiset_sum [AddCommMonoid M] {s : Multiset (α →₀ M)} (a : α) : a ∈ s.sum.support → ∃ f ∈ s, a ∈ (f : α →₀ M).support := Multiset.induction_on s (fun h => False.elim (by simp at h)) (by intro f s ih ha by_cases h : a ∈ f.support · exact ⟨f, Multiset.mem_cons_self _ _, h⟩ · simp only [Multiset.sum_cons, mem_support_iff, add_apply, not_mem_support_iff.1 h, zero_add] at ha rcases ih (mem_support_iff.2 ha) with ⟨f', h₀, h₁⟩ exact ⟨f', Multiset.mem_cons_of_mem h₀, h₁⟩) theorem mem_support_finset_sum [AddCommMonoid M] {s : Finset ι} {h : ι → α →₀ M} (a : α) (ha : a ∈ (∑ c ∈ s, h c).support) : ∃ c ∈ s, a ∈ (h c).support := let ⟨_, hf, hfa⟩ := mem_support_multiset_sum a ha let ⟨c, hc, Eq⟩ := Multiset.mem_map.1 hf ⟨c, hc, Eq.symm ▸ hfa⟩ /-! ### Declarations about `curry` and `uncurry` -/ section CurryUncurry variable [AddCommMonoid M] [AddCommMonoid N] /-- Given a finitely supported function `f` from a product type `α × β` to `γ`, `curry f` is the "curried" finitely supported function from `α` to the type of finitely supported functions from `β` to `γ`. -/ protected def curry (f : α × β →₀ M) : α →₀ β →₀ M := f.sum fun p c => single p.1 (single p.2 c) @[simp] theorem curry_apply (f : α × β →₀ M) (x : α) (y : β) : f.curry x y = f (x, y) := by classical have : ∀ b : α × β, single b.fst (single b.snd (f b)) x y = if b = (x, y) then f b else 0 := by rintro ⟨b₁, b₂⟩ simp only [ne_eq, single_apply, Prod.ext_iff, ite_and] split_ifs <;> simp [single_apply, *] rw [Finsupp.curry, sum_apply, sum_apply, sum_eq_single, this, if_pos rfl] · intro b _ b_ne rw [this b, if_neg b_ne] · intro _ rw [single_zero, single_zero, coe_zero, Pi.zero_apply, coe_zero, Pi.zero_apply] theorem sum_curry_index (f : α × β →₀ M) (g : α → β → M → N) (hg₀ : ∀ a b, g a b 0 = 0) (hg₁ : ∀ a b c₀ c₁, g a b (c₀ + c₁) = g a b c₀ + g a b c₁) : (f.curry.sum fun a f => f.sum (g a)) = f.sum fun p c => g p.1 p.2 c := by rw [Finsupp.curry] trans · exact sum_sum_index (fun a => sum_zero_index) fun a b₀ b₁ => sum_add_index' (fun a => hg₀ _ _) fun c d₀ d₁ => hg₁ _ _ _ _ congr; funext p c trans · exact sum_single_index sum_zero_index exact sum_single_index (hg₀ _ _) /-- Given a finitely supported function `f` from `α` to the type of finitely supported functions from `β` to `M`, `uncurry f` is the "uncurried" finitely supported function from `α × β` to `M`. -/ protected def uncurry (f : α →₀ β →₀ M) : α × β →₀ M := f.sum fun a g => g.sum fun b c => single (a, b) c @[simp] protected theorem uncurry_apply_pair (f : α →₀ β →₀ M) (x : α) (y : β) : f.uncurry (x, y) = f x y := by rw [← curry_apply (f.uncurry) x y] simp only [Finsupp.curry, Finsupp.uncurry, sum_sum_index, single_zero, single_add, forall_true_iff, sum_single_index, single_zero, ← single_sum, sum_single] @[simp] theorem curry_uncurry (f : α →₀ β →₀ M) : f.uncurry.curry = f := by ext a b rw [curry_apply, Finsupp.uncurry_apply_pair] @[simp] theorem uncurry_curry (f : α × β →₀ M) : f.curry.uncurry = f := by ext ⟨a, b⟩ rw [Finsupp.uncurry_apply_pair, curry_apply] /-- `finsuppProdEquiv` defines the `Equiv` between `((α × β) →₀ M)` and `(α →₀ (β →₀ M))` given by currying and uncurrying. -/ def finsuppProdEquiv : (α × β →₀ M) ≃ (α →₀ β →₀ M) where toFun := Finsupp.curry invFun := Finsupp.uncurry left_inv := uncurry_curry right_inv := curry_uncurry theorem filter_curry (f : α × β →₀ M) (p : α → Prop) [DecidablePred p] : (f.filter fun a : α × β => p a.1).curry = f.curry.filter p := by classical rw [Finsupp.curry, Finsupp.curry, Finsupp.sum, Finsupp.sum, filter_sum, support_filter, sum_filter] refine Finset.sum_congr rfl ?_ rintro ⟨a₁, a₂⟩ _ split_ifs with h · rw [filter_apply_pos, filter_single_of_pos] <;> exact h · rwa [filter_single_of_neg] theorem support_curry [DecidableEq α] (f : α × β →₀ M) : f.curry.support ⊆ f.support.image Prod.fst := by rw [← Finset.biUnion_singleton] refine Finset.Subset.trans support_sum ?_ exact Finset.biUnion_mono fun a _ => support_single_subset end CurryUncurry /-! ### Declarations about finitely supported functions whose support is a `Sum` type -/ section Sum /-- `Finsupp.sumElim f g` maps `inl x` to `f x` and `inr y` to `g y`. -/ @[simps support] def sumElim {α β γ : Type*} [Zero γ] (f : α →₀ γ) (g : β →₀ γ) : α ⊕ β →₀ γ where support := f.support.disjSum g.support toFun := Sum.elim f g mem_support_toFun := by simp @[simp, norm_cast] theorem coe_sumElim {α β γ : Type*} [Zero γ] (f : α →₀ γ) (g : β →₀ γ) : ⇑(sumElim f g) = Sum.elim f g := rfl theorem sumElim_apply {α β γ : Type*} [Zero γ] (f : α →₀ γ) (g : β →₀ γ) (x : α ⊕ β) : sumElim f g x = Sum.elim f g x := rfl theorem sumElim_inl {α β γ : Type*} [Zero γ] (f : α →₀ γ) (g : β →₀ γ) (x : α) : sumElim f g (Sum.inl x) = f x := rfl theorem sumElim_inr {α β γ : Type*} [Zero γ] (f : α →₀ γ) (g : β →₀ γ) (x : β) : sumElim f g (Sum.inr x) = g x := rfl @[to_additive] lemma prod_sumElim {ι₁ ι₂ α M : Type*} [Zero α] [CommMonoid M] (f₁ : ι₁ →₀ α) (f₂ : ι₂ →₀ α) (g : ι₁ ⊕ ι₂ → α → M) : (f₁.sumElim f₂).prod g = f₁.prod (g ∘ Sum.inl) * f₂.prod (g ∘ Sum.inr) := by simp [Finsupp.prod, Finset.prod_disj_sum] /-- The equivalence between `(α ⊕ β) →₀ γ` and `(α →₀ γ) × (β →₀ γ)`. This is the `Finsupp` version of `Equiv.sum_arrow_equiv_prod_arrow`. -/ @[simps apply symm_apply] def sumFinsuppEquivProdFinsupp {α β γ : Type*} [Zero γ] : (α ⊕ β →₀ γ) ≃ (α →₀ γ) × (β →₀ γ) where toFun f := ⟨f.comapDomain Sum.inl Sum.inl_injective.injOn, f.comapDomain Sum.inr Sum.inr_injective.injOn⟩ invFun fg := sumElim fg.1 fg.2 left_inv f := by ext ab rcases ab with a | b <;> simp right_inv fg := by ext <;> simp theorem fst_sumFinsuppEquivProdFinsupp {α β γ : Type*} [Zero γ] (f : α ⊕ β →₀ γ) (x : α) : (sumFinsuppEquivProdFinsupp f).1 x = f (Sum.inl x) := rfl theorem snd_sumFinsuppEquivProdFinsupp {α β γ : Type*} [Zero γ] (f : α ⊕ β →₀ γ) (y : β) : (sumFinsuppEquivProdFinsupp f).2 y = f (Sum.inr y) := rfl theorem sumFinsuppEquivProdFinsupp_symm_inl {α β γ : Type*} [Zero γ] (fg : (α →₀ γ) × (β →₀ γ)) (x : α) : (sumFinsuppEquivProdFinsupp.symm fg) (Sum.inl x) = fg.1 x := rfl theorem sumFinsuppEquivProdFinsupp_symm_inr {α β γ : Type*} [Zero γ] (fg : (α →₀ γ) × (β →₀ γ)) (y : β) : (sumFinsuppEquivProdFinsupp.symm fg) (Sum.inr y) = fg.2 y := rfl variable [AddMonoid M] /-- The additive equivalence between `(α ⊕ β) →₀ M` and `(α →₀ M) × (β →₀ M)`. This is the `Finsupp` version of `Equiv.sum_arrow_equiv_prod_arrow`. -/ @[simps! apply symm_apply] def sumFinsuppAddEquivProdFinsupp {α β : Type*} : (α ⊕ β →₀ M) ≃+ (α →₀ M) × (β →₀ M) := { sumFinsuppEquivProdFinsupp with map_add' := by intros ext <;> simp only [Equiv.toFun_as_coe, Prod.fst_add, Prod.snd_add, add_apply, snd_sumFinsuppEquivProdFinsupp, fst_sumFinsuppEquivProdFinsupp] } theorem fst_sumFinsuppAddEquivProdFinsupp {α β : Type*} (f : α ⊕ β →₀ M) (x : α) : (sumFinsuppAddEquivProdFinsupp f).1 x = f (Sum.inl x) := rfl theorem snd_sumFinsuppAddEquivProdFinsupp {α β : Type*} (f : α ⊕ β →₀ M) (y : β) : (sumFinsuppAddEquivProdFinsupp f).2 y = f (Sum.inr y) := rfl theorem sumFinsuppAddEquivProdFinsupp_symm_inl {α β : Type*} (fg : (α →₀ M) × (β →₀ M)) (x : α) : (sumFinsuppAddEquivProdFinsupp.symm fg) (Sum.inl x) = fg.1 x := rfl theorem sumFinsuppAddEquivProdFinsupp_symm_inr {α β : Type*} (fg : (α →₀ M) × (β →₀ M)) (y : β) : (sumFinsuppAddEquivProdFinsupp.symm fg) (Sum.inr y) = fg.2 y := rfl end Sum section variable [Zero R] /-- The `Finsupp` version of `Pi.unique`. -/ instance uniqueOfRight [Subsingleton R] : Unique (α →₀ R) := DFunLike.coe_injective.unique /-- The `Finsupp` version of `Pi.uniqueOfIsEmpty`. -/ instance uniqueOfLeft [IsEmpty α] : Unique (α →₀ R) := DFunLike.coe_injective.unique end section variable {M : Type*} [Zero M] {P : α → Prop} [DecidablePred P] /-- Combine finitely supported functions over `{a // P a}` and `{a // ¬P a}`, by case-splitting on `P a`. -/ @[simps] def piecewise (f : Subtype P →₀ M) (g : {a // ¬ P a} →₀ M) : α →₀ M where toFun a := if h : P a then f ⟨a, h⟩ else g ⟨a, h⟩ support := (f.support.map (.subtype _)).disjUnion (g.support.map (.subtype _)) <| by simp_rw [Finset.disjoint_left, mem_map, forall_exists_index, Embedding.coe_subtype, Subtype.forall, Subtype.exists] rintro _ a ha ⟨-, rfl⟩ ⟨b, hb, -, rfl⟩ exact hb ha mem_support_toFun a := by by_cases ha : P a <;> simp [ha] @[simp] theorem subtypeDomain_piecewise (f : Subtype P →₀ M) (g : {a // ¬ P a} →₀ M) : subtypeDomain P (f.piecewise g) = f := Finsupp.ext fun a => dif_pos a.prop @[simp] theorem subtypeDomain_not_piecewise (f : Subtype P →₀ M) (g : {a // ¬ P a} →₀ M) : subtypeDomain (¬P ·) (f.piecewise g) = g := Finsupp.ext fun a => dif_neg a.prop /-- Extend the domain of a `Finsupp` by using `0` where `P x` does not hold. -/ @[simps! support toFun] def extendDomain (f : Subtype P →₀ M) : α →₀ M := piecewise f 0 theorem extendDomain_eq_embDomain_subtype (f : Subtype P →₀ M) : extendDomain f = embDomain (.subtype _) f := by ext a by_cases h : P a · refine Eq.trans ?_ (embDomain_apply (.subtype P) f (Subtype.mk a h)).symm simp [h] · rw [embDomain_notin_range, extendDomain_toFun, dif_neg h] simp [h] theorem support_extendDomain_subset (f : Subtype P →₀ M) : ↑(f.extendDomain).support ⊆ {x | P x} := by intro x rw [extendDomain_support, mem_coe, mem_map, Embedding.coe_subtype] rintro ⟨x, -, rfl⟩ exact x.prop @[simp] theorem subtypeDomain_extendDomain (f : Subtype P →₀ M) : subtypeDomain P f.extendDomain = f := subtypeDomain_piecewise _ _ theorem extendDomain_subtypeDomain (f : α →₀ M) (hf : ∀ a ∈ f.support, P a) : (subtypeDomain P f).extendDomain = f := by ext a by_cases h : P a · exact dif_pos h · #adaptation_note /-- nightly-2024-06-18 this `rw` was done by `dsimp`. -/ rw [extendDomain_toFun] dsimp rw [if_neg h, eq_comm, ← not_mem_support_iff] refine mt ?_ h exact @hf _ @[simp] theorem extendDomain_single (a : Subtype P) (m : M) : (single a m).extendDomain = single a.val m := by ext a' #adaptation_note /-- nightly-2024-06-18 this `rw` was instead `dsimp only`. -/ rw [extendDomain_toFun] obtain rfl | ha := eq_or_ne a.val a' · simp_rw [single_eq_same, dif_pos a.prop] · simp_rw [single_eq_of_ne ha, dite_eq_right_iff] intro h rw [single_eq_of_ne] simp [Subtype.ext_iff, ha] end /-- Given an `AddCommMonoid M` and `s : Set α`, `restrictSupportEquiv s M` is the `Equiv` between the subtype of finitely supported functions with support contained in `s` and the type of finitely supported functions from `s`. -/ -- TODO: add [DecidablePred (· ∈ s)] as an assumption @[simps] def restrictSupportEquiv (s : Set α) (M : Type*) [AddCommMonoid M] : { f : α →₀ M // ↑f.support ⊆ s } ≃ (s →₀ M) where toFun f := subtypeDomain (· ∈ s) f.1 invFun f := letI := Classical.decPred (· ∈ s); ⟨f.extendDomain, support_extendDomain_subset _⟩ left_inv f := letI := Classical.decPred (· ∈ s); Subtype.ext <| extendDomain_subtypeDomain f.1 f.prop right_inv _ := letI := Classical.decPred (· ∈ s); subtypeDomain_extendDomain _ /-- Given `AddCommMonoid M` and `e : α ≃ β`, `domCongr e` is the corresponding `Equiv` between `α →₀ M` and `β →₀ M`. This is `Finsupp.equivCongrLeft` as an `AddEquiv`. -/ @[simps apply] protected def domCongr [AddCommMonoid M] (e : α ≃ β) : (α →₀ M) ≃+ (β →₀ M) where toFun := equivMapDomain e invFun := equivMapDomain e.symm left_inv v := by simp only [← equivMapDomain_trans, Equiv.self_trans_symm] exact equivMapDomain_refl _ right_inv := by intro v simp only [← equivMapDomain_trans, Equiv.symm_trans_self] exact equivMapDomain_refl _ map_add' a b := by simp only [equivMapDomain_eq_mapDomain]; exact mapDomain_add @[simp] theorem domCongr_refl [AddCommMonoid M] : Finsupp.domCongr (Equiv.refl α) = AddEquiv.refl (α →₀ M) := AddEquiv.ext fun _ => equivMapDomain_refl _ @[simp] theorem domCongr_symm [AddCommMonoid M] (e : α ≃ β) : (Finsupp.domCongr e).symm = (Finsupp.domCongr e.symm : (β →₀ M) ≃+ (α →₀ M)) := AddEquiv.ext fun _ => rfl @[simp] theorem domCongr_trans [AddCommMonoid M] (e : α ≃ β) (f : β ≃ γ) : (Finsupp.domCongr e).trans (Finsupp.domCongr f) = (Finsupp.domCongr (e.trans f) : (α →₀ M) ≃+ _) := AddEquiv.ext fun _ => (equivMapDomain_trans _ _ _).symm end Finsupp namespace Finsupp /-! ### Declarations about sigma types -/ section Sigma variable {αs : ι → Type*} [Zero M] (l : (Σ i, αs i) →₀ M) /-- Given `l`, a finitely supported function from the sigma type `Σ (i : ι), αs i` to `M` and an index element `i : ι`, `split l i` is the `i`th component of `l`, a finitely supported function from `as i` to `M`. This is the `Finsupp` version of `Sigma.curry`. -/ def split (i : ι) : αs i →₀ M := l.comapDomain (Sigma.mk i) fun _ _ _ _ hx => heq_iff_eq.1 (Sigma.mk.inj hx).2 theorem split_apply (i : ι) (x : αs i) : split l i x = l ⟨i, x⟩ := by dsimp only [split] rw [comapDomain_apply] /-- Given `l`, a finitely supported function from the sigma type `Σ (i : ι), αs i` to `β`, `split_support l` is the finset of indices in `ι` that appear in the support of `l`. -/ def splitSupport (l : (Σ i, αs i) →₀ M) : Finset ι := haveI := Classical.decEq ι l.support.image Sigma.fst theorem mem_splitSupport_iff_nonzero (i : ι) : i ∈ splitSupport l ↔ split l i ≠ 0 := by classical rw [splitSupport, mem_image, Ne, ← support_eq_empty, ← Ne, ← Finset.nonempty_iff_ne_empty, split, comapDomain, Finset.Nonempty] simp only [exists_prop, Finset.mem_preimage, exists_and_right, exists_eq_right, mem_support_iff, Sigma.exists, Ne] /-- Given `l`, a finitely supported function from the sigma type `Σ i, αs i` to `β` and an `ι`-indexed family `g` of functions from `(αs i →₀ β)` to `γ`, `split_comp` defines a finitely supported function from the index type `ι` to `γ` given by composing `g i` with `split l i`. -/ def splitComp [Zero N] (g : ∀ i, (αs i →₀ M) → N) (hg : ∀ i x, x = 0 ↔ g i x = 0) : ι →₀ N where support := splitSupport l toFun i := g i (split l i) mem_support_toFun := by intro i rw [mem_splitSupport_iff_nonzero, not_iff_not, hg] theorem sigma_support : l.support = l.splitSupport.sigma fun i => (l.split i).support := by simp only [Finset.ext_iff, splitSupport, split, comapDomain, mem_image, mem_preimage, Sigma.forall, mem_sigma] tauto theorem sigma_sum [AddCommMonoid N] (f : (Σ i : ι, αs i) → M → N) : l.sum f = ∑ i ∈ splitSupport l, (split l i).sum fun (a : αs i) b => f ⟨i, a⟩ b := by simp only [sum, sigma_support, sum_sigma, split_apply] variable {η : Type*} [Fintype η] {ιs : η → Type*} [Zero α] /-- On a `Fintype η`, `Finsupp.split` is an equivalence between `(Σ (j : η), ιs j) →₀ α` and `Π j, (ιs j →₀ α)`. This is the `Finsupp` version of `Equiv.Pi_curry`. -/ noncomputable def sigmaFinsuppEquivPiFinsupp : ((Σ j, ιs j) →₀ α) ≃ ∀ j, ιs j →₀ α where toFun := split invFun f := onFinset (Finset.univ.sigma fun j => (f j).support) (fun ji => f ji.1 ji.2) fun _ hg => Finset.mem_sigma.mpr ⟨Finset.mem_univ _, mem_support_iff.mpr hg⟩ left_inv f := by ext simp [split] right_inv f := by ext simp [split] @[simp] theorem sigmaFinsuppEquivPiFinsupp_apply (f : (Σ j, ιs j) →₀ α) (j i) : sigmaFinsuppEquivPiFinsupp f j i = f ⟨j, i⟩ := rfl /-- On a `Fintype η`, `Finsupp.split` is an additive equivalence between `(Σ (j : η), ιs j) →₀ α` and `Π j, (ιs j →₀ α)`. This is the `AddEquiv` version of `Finsupp.sigmaFinsuppEquivPiFinsupp`. -/ noncomputable def sigmaFinsuppAddEquivPiFinsupp {α : Type*} {ιs : η → Type*} [AddMonoid α] : ((Σ j, ιs j) →₀ α) ≃+ ∀ j, ιs j →₀ α := { sigmaFinsuppEquivPiFinsupp with map_add' := fun f g => by ext simp } @[simp] theorem sigmaFinsuppAddEquivPiFinsupp_apply {α : Type*} {ιs : η → Type*} [AddMonoid α] (f : (Σ j, ιs j) →₀ α) (j i) : sigmaFinsuppAddEquivPiFinsupp f j i = f ⟨j, i⟩ := rfl end Sigma end Finsupp
Mathlib/Data/Finsupp/Basic.lean
1,878
1,880
/- Copyright (c) 2022 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import Mathlib.Order.Filter.SmallSets import Mathlib.Topology.ContinuousOn /-! ### Locally finite families of sets We say that a family of sets in a topological space is *locally finite* if at every point `x : X`, there is a neighborhood of `x` which meets only finitely many sets in the family. In this file we give the definition and prove basic properties of locally finite families of sets. -/ -- locally finite family [General Topology (Bourbaki, 1995)] open Set Function Filter Topology variable {ι ι' α X Y : Type*} [TopologicalSpace X] [TopologicalSpace Y] {f g : ι → Set X} /-- A family of sets in `Set X` is locally finite if at every point `x : X`, there is a neighborhood of `x` which meets only finitely many sets in the family. -/ def LocallyFinite (f : ι → Set X) := ∀ x : X, ∃ t ∈ 𝓝 x, { i | (f i ∩ t).Nonempty }.Finite theorem locallyFinite_of_finite [Finite ι] (f : ι → Set X) : LocallyFinite f := fun _ => ⟨univ, univ_mem, toFinite _⟩ namespace LocallyFinite theorem point_finite (hf : LocallyFinite f) (x : X) : { b | x ∈ f b }.Finite := let ⟨_t, hxt, ht⟩ := hf x ht.subset fun _b hb => ⟨x, hb, mem_of_mem_nhds hxt⟩ protected theorem subset (hf : LocallyFinite f) (hg : ∀ i, g i ⊆ f i) : LocallyFinite g := fun a => let ⟨t, ht₁, ht₂⟩ := hf a ⟨t, ht₁, ht₂.subset fun i hi => hi.mono <| inter_subset_inter (hg i) Subset.rfl⟩ theorem comp_injOn {g : ι' → ι} (hf : LocallyFinite f) (hg : InjOn g { i | (f (g i)).Nonempty }) : LocallyFinite (f ∘ g) := fun x => by let ⟨t, htx, htf⟩ := hf x refine ⟨t, htx, htf.preimage <| ?_⟩ exact hg.mono fun i (hi : Set.Nonempty _) => hi.left
theorem comp_injective {g : ι' → ι} (hf : LocallyFinite f) (hg : Injective g) : LocallyFinite (f ∘ g) := hf.comp_injOn hg.injOn theorem _root_.locallyFinite_iff_smallSets :
Mathlib/Topology/LocallyFinite.lean
47
51
/- Copyright (c) 2024 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import Mathlib.Algebra.MvPolynomial.Equiv import Mathlib.LinearAlgebra.Matrix.Charpoly.Coeff import Mathlib.RingTheory.MvPolynomial.Homogeneous /-! # The universal characteristic polynomial In this file we define the universal characteristic polynomial `Matrix.charpoly.univ`, which is the charactistic polynomial of the matrix with entries `Xᵢⱼ`, and hence has coefficients that are multivariate polynomials. It is universal in the sense that one obtains the characteristic polynomial of a matrix `M` by evaluating the coefficients of `univ` at the entries of `M`. We use it to show that the coefficients of the characteristic polynomial of a matrix are homogeneous polynomials in the matrix entries. ## Main results * `Matrix.charpoly.univ`: the universal characteristic polynomial * `Matrix.charpoly.univ_map_eval₂Hom`: evaluating `univ` on the entries of a matrix `M` gives the characteristic polynomial of `M`. * `Matrix.charpoly.univ_coeff_isHomogeneous`: the `i`-th coefficient of `univ` is a homogeneous polynomial of degree `n - i`. -/ namespace Matrix.charpoly variable {R S : Type*} (n : Type*) [CommRing R] [CommRing S] [Fintype n] [DecidableEq n] variable (f : R →+* S) variable (R) in /-- The universal characteristic polynomial for `n × n`-matrices, is the charactistic polynomial of `Matrix.mvPolynomialX n n ℤ` with entries `Xᵢⱼ`. Its `i`-th coefficient is a homogeneous polynomial of degree `n - i`, see `Matrix.charpoly.univ_coeff_isHomogeneous`. By evaluating the coefficients at the entries of a matrix `M`, one obtains the characteristic polynomial of `M`, see `Matrix.charpoly.univ_map_eval₂Hom`. -/ noncomputable abbrev univ : Polynomial (MvPolynomial (n × n) R) := charpoly <| mvPolynomialX n n R open MvPolynomial RingHomClass in @[simp] lemma univ_map_eval₂Hom (M : n × n → S) : (univ R n).map (eval₂Hom f M) = charpoly (Matrix.of M.curry) := by rw [univ, ← charpoly_map, coe_eval₂Hom, ← mvPolynomialX_map_eval₂ f (Matrix.of M.curry)] simp only [of_apply, Function.curry_apply, Prod.mk.eta] lemma univ_map_map : (univ R n).map (MvPolynomial.map f) = univ S n := by rw [MvPolynomial.map, univ_map_eval₂Hom]; rfl @[simp] lemma univ_coeff_eval₂Hom (M : n × n → S) (i : ℕ) : MvPolynomial.eval₂Hom f M ((univ R n).coeff i) = (charpoly (Matrix.of M.curry)).coeff i := by rw [← univ_map_eval₂Hom n f M, Polynomial.coeff_map] variable (R) lemma univ_monic : (univ R n).Monic := charpoly_monic (mvPolynomialX n n R) lemma univ_natDegree [Nontrivial R] : (univ R n).natDegree = Fintype.card n := charpoly_natDegree_eq_dim (mvPolynomialX n n R) @[simp] lemma univ_coeff_card : (univ R n).coeff (Fintype.card n) = 1 := by suffices Polynomial.coeff (univ ℤ n) (Fintype.card n) = 1 by rw [← univ_map_map n (Int.castRingHom R), Polynomial.coeff_map, this, map_one] rw [← univ_natDegree ℤ n] exact (univ_monic ℤ n).leadingCoeff open MvPolynomial in lemma optionEquivLeft_symm_univ_isHomogeneous : ((optionEquivLeft R (n × n)).symm (univ R n)).IsHomogeneous (Fintype.card n) := by have aux : Fintype.card n = 0 + ∑ i : n, 1 := by
simp only [zero_add, Finset.sum_const, smul_eq_mul, mul_one, Fintype.card] simp only [aux, univ, charpoly, charmatrix, scalar_apply, RingHom.mapMatrix_apply, det_apply', sub_apply, map_apply, of_apply, map_sum, map_mul, map_intCast, map_prod, map_sub, optionEquivLeft_symm_apply, Polynomial.aevalTower_C, rename_X, diagonal, mvPolynomialX] apply IsHomogeneous.sum rintro i - apply IsHomogeneous.mul · apply isHomogeneous_C · apply IsHomogeneous.prod rintro j - by_cases h : i j = j · simp only [h, ↓reduceIte, Polynomial.aevalTower_X, IsHomogeneous.sub, isHomogeneous_X] · simp only [h, ↓reduceIte, map_zero, zero_sub, (isHomogeneous_X _ _).neg] lemma univ_coeff_isHomogeneous (i j : ℕ) (h : i + j = Fintype.card n) : ((univ R n).coeff i).IsHomogeneous j :=
Mathlib/LinearAlgebra/Matrix/Charpoly/Univ.lean
86
101
/- Copyright (c) 2023 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import Mathlib.Dynamics.Ergodic.Ergodic import Mathlib.MeasureTheory.Function.AEEqFun /-! # Functions invariant under (quasi)ergodic map In this file we prove that an a.e. strongly measurable function `g : α → X` that is a.e. invariant under a (quasi)ergodic map is a.e. equal to a constant. We prove several versions of this statement with slightly different measurability assumptions. We also formulate a version for `MeasureTheory.AEEqFun` functions with all a.e. equalities replaced with equalities in the quotient space. -/ open Function Set Filter MeasureTheory Topology TopologicalSpace variable {α X : Type*} [MeasurableSpace α] {μ : MeasureTheory.Measure α} /-- Let `f : α → α` be a (quasi)ergodic map. Let `g : α → X` is a null-measurable function from `α` to a nonempty space with a countable family of measurable sets separating points of a set `s` such that `f x ∈ s` for a.e. `x`. If `g` that is a.e.-invariant under `f`, then `g` is a.e. constant. -/ theorem QuasiErgodic.ae_eq_const_of_ae_eq_comp_of_ae_range₀ [Nonempty X] [MeasurableSpace X] {s : Set X} [MeasurableSpace.CountablySeparated s] {f : α → α} {g : α → X} (h : QuasiErgodic f μ) (hs : ∀ᵐ x ∂μ, g x ∈ s) (hgm : NullMeasurable g μ) (hg_eq : g ∘ f =ᵐ[μ] g) : ∃ c, g =ᵐ[μ] const α c := by refine exists_eventuallyEq_const_of_eventually_mem_of_forall_separating MeasurableSet hs ?_ refine fun U hU ↦ h.ae_mem_or_ae_nmem₀ (s := g ⁻¹' U) (hgm hU) ?_b refine (hg_eq.mono fun x hx ↦ ?_).set_eq rw [← preimage_comp, mem_preimage, mem_preimage, hx] section CountableSeparatingOnUniv variable [Nonempty X] [MeasurableSpace X] [MeasurableSpace.CountablySeparated X] {f : α → α} {g : α → X} /-- Let `f : α → α` be a (pre)ergodic map. Let `g : α → X` be a measurable function from `α` to a nonempty measurable space with a countable family of measurable sets separating the points of `X`. If `g` is invariant under `f`, then `g` is a.e. constant. -/ theorem PreErgodic.ae_eq_const_of_ae_eq_comp (h : PreErgodic f μ) (hgm : Measurable g) (hg_eq : g ∘ f = g) : ∃ c, g =ᵐ[μ] const α c := exists_eventuallyEq_const_of_forall_separating MeasurableSet fun U hU ↦ h.ae_mem_or_ae_nmem (s := g ⁻¹' U) (hgm hU) <| by rw [← preimage_comp, hg_eq] /-- Let `f : α → α` be a quasi ergodic map. Let `g : α → X` be a null-measurable function from `α` to a nonempty measurable space with a countable family of measurable sets separating the points of `X`. If `g` is a.e.-invariant under `f`, then `g` is a.e. constant. -/ theorem QuasiErgodic.ae_eq_const_of_ae_eq_comp₀ (h : QuasiErgodic f μ) (hgm : NullMeasurable g μ) (hg_eq : g ∘ f =ᵐ[μ] g) : ∃ c, g =ᵐ[μ] const α c := h.ae_eq_const_of_ae_eq_comp_of_ae_range₀ (s := univ) univ_mem hgm hg_eq /-- Let `f : α → α` be an ergodic map. Let `g : α → X` be a null-measurable function from `α` to a nonempty measurable space with a countable family of measurable sets separating the points of `X`. If `g` is a.e.-invariant under `f`, then `g` is a.e. constant. -/ theorem Ergodic.ae_eq_const_of_ae_eq_comp₀ (h : Ergodic f μ) (hgm : NullMeasurable g μ) (hg_eq : g ∘ f =ᵐ[μ] g) : ∃ c, g =ᵐ[μ] const α c := h.quasiErgodic.ae_eq_const_of_ae_eq_comp₀ hgm hg_eq end CountableSeparatingOnUniv variable [TopologicalSpace X] [MetrizableSpace X] [Nonempty X] {f : α → α} namespace QuasiErgodic /-- Let `f : α → α` be a quasi ergodic map. Let `g : α → X` be an a.e. strongly measurable function from `α` to a nonempty metrizable topological space. If `g` is a.e.-invariant under `f`, then `g` is a.e. constant. -/
theorem ae_eq_const_of_ae_eq_comp_ae {g : α → X} (h : QuasiErgodic f μ) (hgm : AEStronglyMeasurable g μ) (hg_eq : g ∘ f =ᵐ[μ] g) : ∃ c, g =ᵐ[μ] const α c := by borelize X rcases hgm.isSeparable_ae_range with ⟨t, ht, hgt⟩ haveI := ht.secondCountableTopology exact h.ae_eq_const_of_ae_eq_comp_of_ae_range₀ hgt hgm.aemeasurable.nullMeasurable hg_eq
Mathlib/Dynamics/Ergodic/Function.lean
77
82
/- Copyright (c) 2023 Joël Riou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joël Riou -/ import Mathlib.Algebra.Homology.Homotopy import Mathlib.Algebra.Ring.NegOnePow import Mathlib.Algebra.Category.Grp.Preadditive import Mathlib.Tactic.Linarith import Mathlib.CategoryTheory.Linear.LinearFunctor /-! The cochain complex of homomorphisms between cochain complexes If `F` and `G` are cochain complexes (indexed by `ℤ`) in a preadditive category, there is a cochain complex of abelian groups whose `0`-cocycles identify to morphisms `F ⟶ G`. Informally, in degree `n`, this complex shall consist of cochains of degree `n` from `F` to `G`, i.e. arbitrary families for morphisms `F.X p ⟶ G.X (p + n)`. This complex shall be denoted `HomComplex F G`. In order to avoid type theoretic issues, a cochain of degree `n : ℤ` (i.e. a term of type of `Cochain F G n`) shall be defined here as the data of a morphism `F.X p ⟶ G.X q` for all triplets `⟨p, q, hpq⟩` where `p` and `q` are integers and `hpq : p + n = q`. If `α : Cochain F G n`, we shall define `α.v p q hpq : F.X p ⟶ G.X q`. We follow the signs conventions appearing in the introduction of [Brian Conrad's book *Grothendieck duality and base change*][conrad2000]. ## References * [Brian Conrad, Grothendieck duality and base change][conrad2000] -/ assert_not_exists TwoSidedIdeal open CategoryTheory Category Limits Preadditive universe v u variable {C : Type u} [Category.{v} C] [Preadditive C] {R : Type*} [Ring R] [Linear R C] namespace CochainComplex variable {F G K L : CochainComplex C ℤ} (n m : ℤ) namespace HomComplex /-- A term of type `HomComplex.Triplet n` consists of two integers `p` and `q` such that `p + n = q`. (This type is introduced so that the instance `AddCommGroup (Cochain F G n)` defined below can be found automatically.) -/ structure Triplet (n : ℤ) where /-- a first integer -/ p : ℤ /-- a second integer -/ q : ℤ /-- the condition on the two integers -/ hpq : p + n = q variable (F G) /-- A cochain of degree `n : ℤ` between to cochain complexes `F` and `G` consists of a family of morphisms `F.X p ⟶ G.X q` whenever `p + n = q`, i.e. for all triplets in `HomComplex.Triplet n`. -/ def Cochain := ∀ (T : Triplet n), F.X T.p ⟶ G.X T.q instance : AddCommGroup (Cochain F G n) := by dsimp only [Cochain] infer_instance instance : Module R (Cochain F G n) := by dsimp only [Cochain] infer_instance namespace Cochain variable {F G n} /-- A practical constructor for cochains. -/ def mk (v : ∀ (p q : ℤ) (_ : p + n = q), F.X p ⟶ G.X q) : Cochain F G n := fun ⟨p, q, hpq⟩ => v p q hpq /-- The value of a cochain on a triplet `⟨p, q, hpq⟩`. -/ def v (γ : Cochain F G n) (p q : ℤ) (hpq : p + n = q) : F.X p ⟶ G.X q := γ ⟨p, q, hpq⟩ @[simp] lemma mk_v (v : ∀ (p q : ℤ) (_ : p + n = q), F.X p ⟶ G.X q) (p q : ℤ) (hpq : p + n = q) : (Cochain.mk v).v p q hpq = v p q hpq := rfl lemma congr_v {z₁ z₂ : Cochain F G n} (h : z₁ = z₂) (p q : ℤ) (hpq : p + n = q) : z₁.v p q hpq = z₂.v p q hpq := by subst h; rfl @[ext] lemma ext (z₁ z₂ : Cochain F G n) (h : ∀ (p q hpq), z₁.v p q hpq = z₂.v p q hpq) : z₁ = z₂ := by funext ⟨p, q, hpq⟩ apply h @[ext 1100] lemma ext₀ (z₁ z₂ : Cochain F G 0) (h : ∀ (p : ℤ), z₁.v p p (add_zero p) = z₂.v p p (add_zero p)) : z₁ = z₂ := by ext p q hpq obtain rfl : q = p := by rw [← hpq, add_zero] exact h q @[simp] lemma zero_v {n : ℤ} (p q : ℤ) (hpq : p + n = q) : (0 : Cochain F G n).v p q hpq = 0 := rfl @[simp] lemma add_v {n : ℤ} (z₁ z₂ : Cochain F G n) (p q : ℤ) (hpq : p + n = q) : (z₁ + z₂).v p q hpq = z₁.v p q hpq + z₂.v p q hpq := rfl @[simp] lemma sub_v {n : ℤ} (z₁ z₂ : Cochain F G n) (p q : ℤ) (hpq : p + n = q) : (z₁ - z₂).v p q hpq = z₁.v p q hpq - z₂.v p q hpq := rfl @[simp] lemma neg_v {n : ℤ} (z : Cochain F G n) (p q : ℤ) (hpq : p + n = q) : (-z).v p q hpq = - (z.v p q hpq) := rfl @[simp] lemma smul_v {n : ℤ} (k : R) (z : Cochain F G n) (p q : ℤ) (hpq : p + n = q) : (k • z).v p q hpq = k • (z.v p q hpq) := rfl @[simp] lemma units_smul_v {n : ℤ} (k : Rˣ) (z : Cochain F G n) (p q : ℤ) (hpq : p + n = q) : (k • z).v p q hpq = k • (z.v p q hpq) := rfl /-- A cochain of degree `0` from `F` to `G` can be constructed from a family of morphisms `F.X p ⟶ G.X p` for all `p : ℤ`. -/ def ofHoms (ψ : ∀ (p : ℤ), F.X p ⟶ G.X p) : Cochain F G 0 := Cochain.mk (fun p q hpq => ψ p ≫ eqToHom (by rw [← hpq, add_zero])) @[simp] lemma ofHoms_v (ψ : ∀ (p : ℤ), F.X p ⟶ G.X p) (p : ℤ) : (ofHoms ψ).v p p (add_zero p) = ψ p := by simp only [ofHoms, mk_v, eqToHom_refl, comp_id] @[simp] lemma ofHoms_zero : ofHoms (fun p => (0 : F.X p ⟶ G.X p)) = 0 := by aesop_cat @[simp] lemma ofHoms_v_comp_d (ψ : ∀ (p : ℤ), F.X p ⟶ G.X p) (p q q' : ℤ) (hpq : p + 0 = q) : (ofHoms ψ).v p q hpq ≫ G.d q q' = ψ p ≫ G.d p q' := by rw [add_zero] at hpq subst hpq rw [ofHoms_v] @[simp] lemma d_comp_ofHoms_v (ψ : ∀ (p : ℤ), F.X p ⟶ G.X p) (p' p q : ℤ) (hpq : p + 0 = q) : F.d p' p ≫ (ofHoms ψ).v p q hpq = F.d p' q ≫ ψ q := by rw [add_zero] at hpq subst hpq rw [ofHoms_v] /-- The `0`-cochain attached to a morphism of cochain complexes. -/ def ofHom (φ : F ⟶ G) : Cochain F G 0 := ofHoms (fun p => φ.f p) variable (F G) @[simp] lemma ofHom_zero : ofHom (0 : F ⟶ G) = 0 := by simp only [ofHom, HomologicalComplex.zero_f_apply, ofHoms_zero] variable {F G} @[simp] lemma ofHom_v (φ : F ⟶ G) (p : ℤ) : (ofHom φ).v p p (add_zero p) = φ.f p := by simp only [ofHom, ofHoms_v] @[simp] lemma ofHom_v_comp_d (φ : F ⟶ G) (p q q' : ℤ) (hpq : p + 0 = q) : (ofHom φ).v p q hpq ≫ G.d q q' = φ.f p ≫ G.d p q' := by simp only [ofHom, ofHoms_v_comp_d] @[simp] lemma d_comp_ofHom_v (φ : F ⟶ G) (p' p q : ℤ) (hpq : p + 0 = q) : F.d p' p ≫ (ofHom φ).v p q hpq = F.d p' q ≫ φ.f q := by simp only [ofHom, d_comp_ofHoms_v] @[simp] lemma ofHom_add (φ₁ φ₂ : F ⟶ G) : Cochain.ofHom (φ₁ + φ₂) = Cochain.ofHom φ₁ + Cochain.ofHom φ₂ := by aesop_cat @[simp] lemma ofHom_sub (φ₁ φ₂ : F ⟶ G) : Cochain.ofHom (φ₁ - φ₂) = Cochain.ofHom φ₁ - Cochain.ofHom φ₂ := by aesop_cat @[simp] lemma ofHom_neg (φ : F ⟶ G) : Cochain.ofHom (-φ) = -Cochain.ofHom φ := by aesop_cat /-- The cochain of degree `-1` given by an homotopy between two morphism of complexes. -/ def ofHomotopy {φ₁ φ₂ : F ⟶ G} (ho : Homotopy φ₁ φ₂) : Cochain F G (-1) := Cochain.mk (fun p q _ => ho.hom p q) @[simp] lemma ofHomotopy_ofEq {φ₁ φ₂ : F ⟶ G} (h : φ₁ = φ₂) : ofHomotopy (Homotopy.ofEq h) = 0 := rfl @[simp] lemma ofHomotopy_refl (φ : F ⟶ G) : ofHomotopy (Homotopy.refl φ) = 0 := rfl @[reassoc] lemma v_comp_XIsoOfEq_hom (γ : Cochain F G n) (p q q' : ℤ) (hpq : p + n = q) (hq' : q = q') : γ.v p q hpq ≫ (HomologicalComplex.XIsoOfEq G hq').hom = γ.v p q' (by rw [← hq', hpq]) := by subst hq' simp only [HomologicalComplex.XIsoOfEq, eqToIso_refl, Iso.refl_hom, comp_id] @[reassoc] lemma v_comp_XIsoOfEq_inv (γ : Cochain F G n) (p q q' : ℤ) (hpq : p + n = q) (hq' : q' = q) : γ.v p q hpq ≫ (HomologicalComplex.XIsoOfEq G hq').inv = γ.v p q' (by rw [hq', hpq]) := by subst hq' simp only [HomologicalComplex.XIsoOfEq, eqToIso_refl, Iso.refl_inv, comp_id] /-- The composition of cochains. -/ def comp {n₁ n₂ n₁₂ : ℤ} (z₁ : Cochain F G n₁) (z₂ : Cochain G K n₂) (h : n₁ + n₂ = n₁₂) : Cochain F K n₁₂ := Cochain.mk (fun p q hpq => z₁.v p (p + n₁) rfl ≫ z₂.v (p + n₁) q (by omega)) /-! If `z₁` is a cochain of degree `n₁` and `z₂` is a cochain of degree `n₂`, and that we have a relation `h : n₁ + n₂ = n₁₂`, then `z₁.comp z₂ h` is a cochain of degree `n₁₂`. The following lemma `comp_v` computes the value of this composition `z₁.comp z₂ h` on a triplet `⟨p₁, p₃, _⟩` (with `p₁ + n₁₂ = p₃`). In order to use this lemma, we need to provide an intermediate integer `p₂` such that `p₁ + n₁ = p₂`. It is advisable to use a `p₂` that has good definitional properties (i.e. `p₁ + n₁` is not always the best choice.) When `z₁` or `z₂` is a `0`-cochain, there is a better choice of `p₂`, and this leads to the two simplification lemmas `comp_zero_cochain_v` and `zero_cochain_comp_v`. -/ lemma comp_v {n₁ n₂ n₁₂ : ℤ} (z₁ : Cochain F G n₁) (z₂ : Cochain G K n₂) (h : n₁ + n₂ = n₁₂) (p₁ p₂ p₃ : ℤ) (h₁ : p₁ + n₁ = p₂) (h₂ : p₂ + n₂ = p₃) : (z₁.comp z₂ h).v p₁ p₃ (by rw [← h₂, ← h₁, ← h, add_assoc]) = z₁.v p₁ p₂ h₁ ≫ z₂.v p₂ p₃ h₂ := by subst h₁; rfl @[simp] lemma comp_zero_cochain_v (z₁ : Cochain F G n) (z₂ : Cochain G K 0) (p q : ℤ) (hpq : p + n = q) : (z₁.comp z₂ (add_zero n)).v p q hpq = z₁.v p q hpq ≫ z₂.v q q (add_zero q) := comp_v z₁ z₂ (add_zero n) p q q hpq (add_zero q) @[simp] lemma zero_cochain_comp_v (z₁ : Cochain F G 0) (z₂ : Cochain G K n) (p q : ℤ) (hpq : p + n = q) : (z₁.comp z₂ (zero_add n)).v p q hpq = z₁.v p p (add_zero p) ≫ z₂.v p q hpq := comp_v z₁ z₂ (zero_add n) p p q (add_zero p) hpq /-- The associativity of the composition of cochains. -/ lemma comp_assoc {n₁ n₂ n₃ n₁₂ n₂₃ n₁₂₃ : ℤ} (z₁ : Cochain F G n₁) (z₂ : Cochain G K n₂) (z₃ : Cochain K L n₃) (h₁₂ : n₁ + n₂ = n₁₂) (h₂₃ : n₂ + n₃ = n₂₃) (h₁₂₃ : n₁ + n₂ + n₃ = n₁₂₃) : (z₁.comp z₂ h₁₂).comp z₃ (show n₁₂ + n₃ = n₁₂₃ by rw [← h₁₂, h₁₂₃]) = z₁.comp (z₂.comp z₃ h₂₃) (by rw [← h₂₃, ← h₁₂₃, add_assoc]) := by substs h₁₂ h₂₃ h₁₂₃ ext p q hpq rw [comp_v _ _ rfl p (p + n₁ + n₂) q (add_assoc _ _ _).symm (by omega), comp_v z₁ z₂ rfl p (p + n₁) (p + n₁ + n₂) (by omega) (by omega), comp_v z₁ (z₂.comp z₃ rfl) (add_assoc n₁ n₂ n₃).symm p (p + n₁) q (by omega) (by omega), comp_v z₂ z₃ rfl (p + n₁) (p + n₁ + n₂) q (by omega) (by omega), assoc] /-! The formulation of the associativity of the composition of cochains given by the lemma `comp_assoc` often requires a careful selection of degrees with good definitional properties. In a few cases, like when one of the three cochains is a `0`-cochain, there are better choices, which provides the following simplification lemmas. -/ @[simp] lemma comp_assoc_of_first_is_zero_cochain {n₂ n₃ n₂₃ : ℤ} (z₁ : Cochain F G 0) (z₂ : Cochain G K n₂) (z₃ : Cochain K L n₃) (h₂₃ : n₂ + n₃ = n₂₃) : (z₁.comp z₂ (zero_add n₂)).comp z₃ h₂₃ = z₁.comp (z₂.comp z₃ h₂₃) (zero_add n₂₃) := comp_assoc _ _ _ _ _ (by omega) @[simp] lemma comp_assoc_of_second_is_zero_cochain {n₁ n₃ n₁₃ : ℤ} (z₁ : Cochain F G n₁) (z₂ : Cochain G K 0) (z₃ : Cochain K L n₃) (h₁₃ : n₁ + n₃ = n₁₃) : (z₁.comp z₂ (add_zero n₁)).comp z₃ h₁₃ = z₁.comp (z₂.comp z₃ (zero_add n₃)) h₁₃ := comp_assoc _ _ _ _ _ (by omega) @[simp] lemma comp_assoc_of_third_is_zero_cochain {n₁ n₂ n₁₂ : ℤ} (z₁ : Cochain F G n₁) (z₂ : Cochain G K n₂) (z₃ : Cochain K L 0) (h₁₂ : n₁ + n₂ = n₁₂) : (z₁.comp z₂ h₁₂).comp z₃ (add_zero n₁₂) = z₁.comp (z₂.comp z₃ (add_zero n₂)) h₁₂ := comp_assoc _ _ _ _ _ (by omega) @[simp] lemma comp_assoc_of_second_degree_eq_neg_third_degree {n₁ n₂ n₁₂ : ℤ} (z₁ : Cochain F G n₁) (z₂ : Cochain G K (-n₂)) (z₃ : Cochain K L n₂) (h₁₂ : n₁ + (-n₂) = n₁₂) : (z₁.comp z₂ h₁₂).comp z₃ (show n₁₂ + n₂ = n₁ by rw [← h₁₂, add_assoc, neg_add_cancel, add_zero]) = z₁.comp (z₂.comp z₃ (neg_add_cancel n₂)) (add_zero n₁) := comp_assoc _ _ _ _ _ (by omega) @[simp] protected lemma zero_comp {n₁ n₂ n₁₂ : ℤ} (z₂ : Cochain G K n₂) (h : n₁ + n₂ = n₁₂) : (0 : Cochain F G n₁).comp z₂ h = 0 := by ext p q hpq simp only [comp_v _ _ h p _ q rfl (by omega), zero_v, zero_comp] @[simp] protected lemma add_comp {n₁ n₂ n₁₂ : ℤ} (z₁ z₁' : Cochain F G n₁) (z₂ : Cochain G K n₂) (h : n₁ + n₂ = n₁₂) : (z₁ + z₁').comp z₂ h = z₁.comp z₂ h + z₁'.comp z₂ h := by ext p q hpq simp only [comp_v _ _ h p _ q rfl (by omega), add_v, add_comp] @[simp] protected lemma sub_comp {n₁ n₂ n₁₂ : ℤ} (z₁ z₁' : Cochain F G n₁) (z₂ : Cochain G K n₂) (h : n₁ + n₂ = n₁₂) : (z₁ - z₁').comp z₂ h = z₁.comp z₂ h - z₁'.comp z₂ h := by ext p q hpq simp only [comp_v _ _ h p _ q rfl (by omega), sub_v, sub_comp] @[simp] protected lemma neg_comp {n₁ n₂ n₁₂ : ℤ} (z₁ : Cochain F G n₁) (z₂ : Cochain G K n₂) (h : n₁ + n₂ = n₁₂) : (-z₁).comp z₂ h = -z₁.comp z₂ h := by ext p q hpq simp only [comp_v _ _ h p _ q rfl (by omega), neg_v, neg_comp] @[simp] protected lemma smul_comp {n₁ n₂ n₁₂ : ℤ} (k : R) (z₁ : Cochain F G n₁) (z₂ : Cochain G K n₂) (h : n₁ + n₂ = n₁₂) : (k • z₁).comp z₂ h = k • (z₁.comp z₂ h) := by ext p q hpq simp only [comp_v _ _ h p _ q rfl (by omega), smul_v, Linear.smul_comp] @[simp] lemma units_smul_comp {n₁ n₂ n₁₂ : ℤ} (k : Rˣ) (z₁ : Cochain F G n₁) (z₂ : Cochain G K n₂) (h : n₁ + n₂ = n₁₂) : (k • z₁).comp z₂ h = k • (z₁.comp z₂ h) := by apply Cochain.smul_comp @[simp] protected lemma id_comp {n : ℤ} (z₂ : Cochain F G n) : (Cochain.ofHom (𝟙 F)).comp z₂ (zero_add n) = z₂ := by ext p q hpq simp only [zero_cochain_comp_v, ofHom_v, HomologicalComplex.id_f, id_comp] @[simp] protected lemma comp_zero {n₁ n₂ n₁₂ : ℤ} (z₁ : Cochain F G n₁) (h : n₁ + n₂ = n₁₂) : z₁.comp (0 : Cochain G K n₂) h = 0 := by ext p q hpq simp only [comp_v _ _ h p _ q rfl (by omega), zero_v, comp_zero] @[simp] protected lemma comp_add {n₁ n₂ n₁₂ : ℤ} (z₁ : Cochain F G n₁) (z₂ z₂' : Cochain G K n₂) (h : n₁ + n₂ = n₁₂) : z₁.comp (z₂ + z₂') h = z₁.comp z₂ h + z₁.comp z₂' h := by ext p q hpq simp only [comp_v _ _ h p _ q rfl (by omega), add_v, comp_add] @[simp] protected lemma comp_sub {n₁ n₂ n₁₂ : ℤ} (z₁ : Cochain F G n₁) (z₂ z₂' : Cochain G K n₂) (h : n₁ + n₂ = n₁₂) : z₁.comp (z₂ - z₂') h = z₁.comp z₂ h - z₁.comp z₂' h := by ext p q hpq
simp only [comp_v _ _ h p _ q rfl (by omega), sub_v, comp_sub] @[simp] protected lemma comp_neg {n₁ n₂ n₁₂ : ℤ} (z₁ : Cochain F G n₁) (z₂ : Cochain G K n₂) (h : n₁ + n₂ = n₁₂) : z₁.comp (-z₂) h = -z₁.comp z₂ h := by
Mathlib/Algebra/Homology/HomotopyCategory/HomComplex.lean
356
360
/- Copyright (c) 2019 Rohan Mitta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Rohan Mitta, Kevin Buzzard, Alistair Tucker, Johannes Hölzl, Yury Kudryashov -/ import Mathlib.Analysis.SpecificLimits.Basic import Mathlib.Data.Setoid.Basic import Mathlib.Dynamics.FixedPoints.Topology import Mathlib.Topology.MetricSpace.Lipschitz /-! # Contracting maps A Lipschitz continuous self-map with Lipschitz constant `K < 1` is called a *contracting map*. In this file we prove the Banach fixed point theorem, some explicit estimates on the rate of convergence, and some properties of the map sending a contracting map to its fixed point. ## Main definitions * `ContractingWith K f` : a Lipschitz continuous self-map with `K < 1`; * `efixedPoint` : given a contracting map `f` on a complete emetric space and a point `x` such that `edist x (f x) ≠ ∞`, `efixedPoint f hf x hx` is the unique fixed point of `f` in `EMetric.ball x ∞`; * `fixedPoint` : the unique fixed point of a contracting map on a complete nonempty metric space. ## Tags contracting map, fixed point, Banach fixed point theorem -/ open NNReal Topology ENNReal Filter Function variable {α : Type*} /-- A map is said to be `ContractingWith K`, if `K < 1` and `f` is `LipschitzWith K`. -/ def ContractingWith [EMetricSpace α] (K : ℝ≥0) (f : α → α) := K < 1 ∧ LipschitzWith K f namespace ContractingWith variable [EMetricSpace α] {K : ℝ≥0} {f : α → α} open EMetric Set theorem toLipschitzWith (hf : ContractingWith K f) : LipschitzWith K f := hf.2 theorem one_sub_K_pos' (hf : ContractingWith K f) : (0 : ℝ≥0∞) < 1 - K := by simp [hf.1] theorem one_sub_K_ne_zero (hf : ContractingWith K f) : (1 : ℝ≥0∞) - K ≠ 0 := ne_of_gt hf.one_sub_K_pos' theorem one_sub_K_ne_top : (1 : ℝ≥0∞) - K ≠ ∞ := by
norm_cast
Mathlib/Topology/MetricSpace/Contracting.lean
53
53
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Abhimanyu Pallavi Sudhir, Jean Lo, Calle Sönne, Benjamin Davidson -/ import Mathlib.Analysis.SpecialFunctions.Trigonometric.Basic import Mathlib.Topology.Order.ProjIcc /-! # Inverse trigonometric functions. See also `Analysis.SpecialFunctions.Trigonometric.Arctan` for the inverse tan function. (This is delayed as it is easier to set up after developing complex trigonometric functions.) Basic inequalities on trigonometric functions. -/ noncomputable section open Topology Filter Set Filter Real namespace Real variable {x y : ℝ} /-- Inverse of the `sin` function, returns values in the range `-π / 2 ≤ arcsin x ≤ π / 2`. It defaults to `-π / 2` on `(-∞, -1)` and to `π / 2` to `(1, ∞)`. -/ @[pp_nodot] noncomputable def arcsin : ℝ → ℝ := Subtype.val ∘ IccExtend (neg_le_self zero_le_one) sinOrderIso.symm theorem arcsin_mem_Icc (x : ℝ) : arcsin x ∈ Icc (-(π / 2)) (π / 2) := Subtype.coe_prop _ @[simp] theorem range_arcsin : range arcsin = Icc (-(π / 2)) (π / 2) := by rw [arcsin, range_comp Subtype.val] simp [Icc] theorem arcsin_le_pi_div_two (x : ℝ) : arcsin x ≤ π / 2 := (arcsin_mem_Icc x).2 theorem neg_pi_div_two_le_arcsin (x : ℝ) : -(π / 2) ≤ arcsin x := (arcsin_mem_Icc x).1 theorem arcsin_projIcc (x : ℝ) : arcsin (projIcc (-1) 1 (neg_le_self zero_le_one) x) = arcsin x := by rw [arcsin, Function.comp_apply, IccExtend_val, Function.comp_apply, IccExtend, Function.comp_apply] theorem sin_arcsin' {x : ℝ} (hx : x ∈ Icc (-1 : ℝ) 1) : sin (arcsin x) = x := by simpa [arcsin, IccExtend_of_mem _ _ hx, -OrderIso.apply_symm_apply] using Subtype.ext_iff.1 (sinOrderIso.apply_symm_apply ⟨x, hx⟩) theorem sin_arcsin {x : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) : sin (arcsin x) = x := sin_arcsin' ⟨hx₁, hx₂⟩ theorem arcsin_sin' {x : ℝ} (hx : x ∈ Icc (-(π / 2)) (π / 2)) : arcsin (sin x) = x := injOn_sin (arcsin_mem_Icc _) hx <| by rw [sin_arcsin (neg_one_le_sin _) (sin_le_one _)] theorem arcsin_sin {x : ℝ} (hx₁ : -(π / 2) ≤ x) (hx₂ : x ≤ π / 2) : arcsin (sin x) = x := arcsin_sin' ⟨hx₁, hx₂⟩ theorem strictMonoOn_arcsin : StrictMonoOn arcsin (Icc (-1) 1) := (Subtype.strictMono_coe _).comp_strictMonoOn <| sinOrderIso.symm.strictMono.strictMonoOn_IccExtend _ @[gcongr] theorem arcsin_lt_arcsin {x y : ℝ} (hx : -1 ≤ x) (hlt : x < y) (hy : y ≤ 1) : arcsin x < arcsin y := strictMonoOn_arcsin ⟨hx, hlt.le.trans hy⟩ ⟨hx.trans hlt.le, hy⟩ hlt theorem monotone_arcsin : Monotone arcsin := (Subtype.mono_coe _).comp <| sinOrderIso.symm.monotone.IccExtend _ @[gcongr] theorem arcsin_le_arcsin {x y : ℝ} (h : x ≤ y) : arcsin x ≤ arcsin y := monotone_arcsin h theorem injOn_arcsin : InjOn arcsin (Icc (-1) 1) := strictMonoOn_arcsin.injOn theorem arcsin_inj {x y : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) (hy₁ : -1 ≤ y) (hy₂ : y ≤ 1) : arcsin x = arcsin y ↔ x = y := injOn_arcsin.eq_iff ⟨hx₁, hx₂⟩ ⟨hy₁, hy₂⟩ @[continuity, fun_prop] theorem continuous_arcsin : Continuous arcsin := continuous_subtype_val.comp sinOrderIso.symm.continuous.Icc_extend' @[fun_prop] theorem continuousAt_arcsin {x : ℝ} : ContinuousAt arcsin x := continuous_arcsin.continuousAt theorem arcsin_eq_of_sin_eq {x y : ℝ} (h₁ : sin x = y) (h₂ : x ∈ Icc (-(π / 2)) (π / 2)) : arcsin y = x := by subst y exact injOn_sin (arcsin_mem_Icc _) h₂ (sin_arcsin' (sin_mem_Icc x)) @[simp] theorem arcsin_zero : arcsin 0 = 0 := arcsin_eq_of_sin_eq sin_zero ⟨neg_nonpos.2 pi_div_two_pos.le, pi_div_two_pos.le⟩ @[simp] theorem arcsin_one : arcsin 1 = π / 2 := arcsin_eq_of_sin_eq sin_pi_div_two <| right_mem_Icc.2 (neg_le_self pi_div_two_pos.le) theorem arcsin_of_one_le {x : ℝ} (hx : 1 ≤ x) : arcsin x = π / 2 := by rw [← arcsin_projIcc, projIcc_of_right_le _ hx, Subtype.coe_mk, arcsin_one] theorem arcsin_neg_one : arcsin (-1) = -(π / 2) := arcsin_eq_of_sin_eq (by rw [sin_neg, sin_pi_div_two]) <| left_mem_Icc.2 (neg_le_self pi_div_two_pos.le) theorem arcsin_of_le_neg_one {x : ℝ} (hx : x ≤ -1) : arcsin x = -(π / 2) := by rw [← arcsin_projIcc, projIcc_of_le_left _ hx, Subtype.coe_mk, arcsin_neg_one] @[simp] theorem arcsin_neg (x : ℝ) : arcsin (-x) = -arcsin x := by rcases le_total x (-1) with hx₁ | hx₁ · rw [arcsin_of_le_neg_one hx₁, neg_neg, arcsin_of_one_le (le_neg.2 hx₁)] rcases le_total 1 x with hx₂ | hx₂ · rw [arcsin_of_one_le hx₂, arcsin_of_le_neg_one (neg_le_neg hx₂)] refine arcsin_eq_of_sin_eq ?_ ?_ · rw [sin_neg, sin_arcsin hx₁ hx₂] · exact ⟨neg_le_neg (arcsin_le_pi_div_two _), neg_le.2 (neg_pi_div_two_le_arcsin _)⟩ theorem arcsin_le_iff_le_sin {x y : ℝ} (hx : x ∈ Icc (-1 : ℝ) 1) (hy : y ∈ Icc (-(π / 2)) (π / 2)) : arcsin x ≤ y ↔ x ≤ sin y := by rw [← arcsin_sin' hy, strictMonoOn_arcsin.le_iff_le hx (sin_mem_Icc _), arcsin_sin' hy] theorem arcsin_le_iff_le_sin' {x y : ℝ} (hy : y ∈ Ico (-(π / 2)) (π / 2)) : arcsin x ≤ y ↔ x ≤ sin y := by rcases le_total x (-1) with hx₁ | hx₁ · simp [arcsin_of_le_neg_one hx₁, hy.1, hx₁.trans (neg_one_le_sin _)] rcases lt_or_le 1 x with hx₂ | hx₂ · simp [arcsin_of_one_le hx₂.le, hy.2.not_le, (sin_le_one y).trans_lt hx₂] exact arcsin_le_iff_le_sin ⟨hx₁, hx₂⟩ (mem_Icc_of_Ico hy) theorem le_arcsin_iff_sin_le {x y : ℝ} (hx : x ∈ Icc (-(π / 2)) (π / 2)) (hy : y ∈ Icc (-1 : ℝ) 1) : x ≤ arcsin y ↔ sin x ≤ y := by rw [← neg_le_neg_iff, ← arcsin_neg, arcsin_le_iff_le_sin ⟨neg_le_neg hy.2, neg_le.2 hy.1⟩ ⟨neg_le_neg hx.2, neg_le.2 hx.1⟩, sin_neg, neg_le_neg_iff] theorem le_arcsin_iff_sin_le' {x y : ℝ} (hx : x ∈ Ioc (-(π / 2)) (π / 2)) : x ≤ arcsin y ↔ sin x ≤ y := by rw [← neg_le_neg_iff, ← arcsin_neg, arcsin_le_iff_le_sin' ⟨neg_le_neg hx.2, neg_lt.2 hx.1⟩, sin_neg, neg_le_neg_iff] theorem arcsin_lt_iff_lt_sin {x y : ℝ} (hx : x ∈ Icc (-1 : ℝ) 1) (hy : y ∈ Icc (-(π / 2)) (π / 2)) : arcsin x < y ↔ x < sin y := not_le.symm.trans <| (not_congr <| le_arcsin_iff_sin_le hy hx).trans not_le theorem arcsin_lt_iff_lt_sin' {x y : ℝ} (hy : y ∈ Ioc (-(π / 2)) (π / 2)) : arcsin x < y ↔ x < sin y := not_le.symm.trans <| (not_congr <| le_arcsin_iff_sin_le' hy).trans not_le theorem lt_arcsin_iff_sin_lt {x y : ℝ} (hx : x ∈ Icc (-(π / 2)) (π / 2)) (hy : y ∈ Icc (-1 : ℝ) 1) : x < arcsin y ↔ sin x < y := not_le.symm.trans <| (not_congr <| arcsin_le_iff_le_sin hy hx).trans not_le theorem lt_arcsin_iff_sin_lt' {x y : ℝ} (hx : x ∈ Ico (-(π / 2)) (π / 2)) : x < arcsin y ↔ sin x < y := not_le.symm.trans <| (not_congr <| arcsin_le_iff_le_sin' hx).trans not_le theorem arcsin_eq_iff_eq_sin {x y : ℝ} (hy : y ∈ Ioo (-(π / 2)) (π / 2)) : arcsin x = y ↔ x = sin y := by simp only [le_antisymm_iff, arcsin_le_iff_le_sin' (mem_Ico_of_Ioo hy), le_arcsin_iff_sin_le' (mem_Ioc_of_Ioo hy)] @[simp] theorem arcsin_nonneg {x : ℝ} : 0 ≤ arcsin x ↔ 0 ≤ x := (le_arcsin_iff_sin_le' ⟨neg_lt_zero.2 pi_div_two_pos, pi_div_two_pos.le⟩).trans <| by rw [sin_zero] @[simp] theorem arcsin_nonpos {x : ℝ} : arcsin x ≤ 0 ↔ x ≤ 0 := neg_nonneg.symm.trans <| arcsin_neg x ▸ arcsin_nonneg.trans neg_nonneg @[simp] theorem arcsin_eq_zero_iff {x : ℝ} : arcsin x = 0 ↔ x = 0 := by simp [le_antisymm_iff] @[simp] theorem zero_eq_arcsin_iff {x} : 0 = arcsin x ↔ x = 0 := eq_comm.trans arcsin_eq_zero_iff @[simp] theorem arcsin_pos {x : ℝ} : 0 < arcsin x ↔ 0 < x := lt_iff_lt_of_le_iff_le arcsin_nonpos @[simp] theorem arcsin_lt_zero {x : ℝ} : arcsin x < 0 ↔ x < 0 := lt_iff_lt_of_le_iff_le arcsin_nonneg @[simp] theorem arcsin_lt_pi_div_two {x : ℝ} : arcsin x < π / 2 ↔ x < 1 := (arcsin_lt_iff_lt_sin' (right_mem_Ioc.2 <| neg_lt_self pi_div_two_pos)).trans <| by rw [sin_pi_div_two] @[simp] theorem neg_pi_div_two_lt_arcsin {x : ℝ} : -(π / 2) < arcsin x ↔ -1 < x := (lt_arcsin_iff_sin_lt' <| left_mem_Ico.2 <| neg_lt_self pi_div_two_pos).trans <| by rw [sin_neg, sin_pi_div_two] @[simp] theorem arcsin_eq_pi_div_two {x : ℝ} : arcsin x = π / 2 ↔ 1 ≤ x := ⟨fun h => not_lt.1 fun h' => (arcsin_lt_pi_div_two.2 h').ne h, arcsin_of_one_le⟩ @[simp] theorem pi_div_two_eq_arcsin {x} : π / 2 = arcsin x ↔ 1 ≤ x := eq_comm.trans arcsin_eq_pi_div_two @[simp] theorem pi_div_two_le_arcsin {x} : π / 2 ≤ arcsin x ↔ 1 ≤ x := (arcsin_le_pi_div_two x).le_iff_eq.trans pi_div_two_eq_arcsin @[simp] theorem arcsin_eq_neg_pi_div_two {x : ℝ} : arcsin x = -(π / 2) ↔ x ≤ -1 := ⟨fun h => not_lt.1 fun h' => (neg_pi_div_two_lt_arcsin.2 h').ne' h, arcsin_of_le_neg_one⟩ @[simp] theorem neg_pi_div_two_eq_arcsin {x} : -(π / 2) = arcsin x ↔ x ≤ -1 := eq_comm.trans arcsin_eq_neg_pi_div_two @[simp] theorem arcsin_le_neg_pi_div_two {x} : arcsin x ≤ -(π / 2) ↔ x ≤ -1 := (neg_pi_div_two_le_arcsin x).le_iff_eq.trans arcsin_eq_neg_pi_div_two @[simp] theorem pi_div_four_le_arcsin {x} : π / 4 ≤ arcsin x ↔ √2 / 2 ≤ x := by rw [← sin_pi_div_four, le_arcsin_iff_sin_le'] have := pi_pos constructor <;> linarith theorem mapsTo_sin_Ioo : MapsTo sin (Ioo (-(π / 2)) (π / 2)) (Ioo (-1) 1) := fun x h => by rwa [mem_Ioo, ← arcsin_lt_pi_div_two, ← neg_pi_div_two_lt_arcsin, arcsin_sin h.1.le h.2.le] /-- `Real.sin` as a `PartialHomeomorph` between `(-π / 2, π / 2)` and `(-1, 1)`. -/ @[simp] def sinPartialHomeomorph : PartialHomeomorph ℝ ℝ where toFun := sin invFun := arcsin source := Ioo (-(π / 2)) (π / 2) target := Ioo (-1) 1 map_source' := mapsTo_sin_Ioo map_target' _ hy := ⟨neg_pi_div_two_lt_arcsin.2 hy.1, arcsin_lt_pi_div_two.2 hy.2⟩ left_inv' _ hx := arcsin_sin hx.1.le hx.2.le right_inv' _ hy := sin_arcsin hy.1.le hy.2.le open_source := isOpen_Ioo open_target := isOpen_Ioo continuousOn_toFun := continuous_sin.continuousOn continuousOn_invFun := continuous_arcsin.continuousOn theorem cos_arcsin_nonneg (x : ℝ) : 0 ≤ cos (arcsin x) := cos_nonneg_of_mem_Icc ⟨neg_pi_div_two_le_arcsin _, arcsin_le_pi_div_two _⟩ -- The junk values for `arcsin` and `sqrt` make this true even outside `[-1, 1]`. theorem cos_arcsin (x : ℝ) : cos (arcsin x) = √(1 - x ^ 2) := by by_cases hx₁ : -1 ≤ x; swap · rw [not_le] at hx₁ rw [arcsin_of_le_neg_one hx₁.le, cos_neg, cos_pi_div_two, sqrt_eq_zero_of_nonpos] nlinarith by_cases hx₂ : x ≤ 1; swap · rw [not_le] at hx₂ rw [arcsin_of_one_le hx₂.le, cos_pi_div_two, sqrt_eq_zero_of_nonpos] nlinarith have : sin (arcsin x) ^ 2 + cos (arcsin x) ^ 2 = 1 := sin_sq_add_cos_sq (arcsin x) rw [← eq_sub_iff_add_eq', ← sqrt_inj (sq_nonneg _) (sub_nonneg.2 (sin_sq_le_one (arcsin x))), sq, sqrt_mul_self (cos_arcsin_nonneg _)] at this rw [this, sin_arcsin hx₁ hx₂] -- The junk values for `arcsin` and `sqrt` make this true even outside `[-1, 1]`. theorem tan_arcsin (x : ℝ) : tan (arcsin x) = x / √(1 - x ^ 2) := by rw [tan_eq_sin_div_cos, cos_arcsin] by_cases hx₁ : -1 ≤ x; swap · have h : √(1 - x ^ 2) = 0 := sqrt_eq_zero_of_nonpos (by nlinarith) rw [h] simp by_cases hx₂ : x ≤ 1; swap · have h : √(1 - x ^ 2) = 0 := sqrt_eq_zero_of_nonpos (by nlinarith) rw [h] simp rw [sin_arcsin hx₁ hx₂] /-- Inverse of the `cos` function, returns values in the range `0 ≤ arccos x` and `arccos x ≤ π`. It defaults to `π` on `(-∞, -1)` and to `0` to `(1, ∞)`. -/ @[pp_nodot] noncomputable def arccos (x : ℝ) : ℝ := π / 2 - arcsin x theorem arccos_eq_pi_div_two_sub_arcsin (x : ℝ) : arccos x = π / 2 - arcsin x := rfl theorem arcsin_eq_pi_div_two_sub_arccos (x : ℝ) : arcsin x = π / 2 - arccos x := by simp [arccos] theorem arccos_le_pi (x : ℝ) : arccos x ≤ π := by unfold arccos; linarith [neg_pi_div_two_le_arcsin x] theorem arccos_nonneg (x : ℝ) : 0 ≤ arccos x := by unfold arccos; linarith [arcsin_le_pi_div_two x] @[simp] theorem arccos_pos {x : ℝ} : 0 < arccos x ↔ x < 1 := by simp [arccos] theorem cos_arccos {x : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) : cos (arccos x) = x := by rw [arccos, cos_pi_div_two_sub, sin_arcsin hx₁ hx₂] theorem arccos_cos {x : ℝ} (hx₁ : 0 ≤ x) (hx₂ : x ≤ π) : arccos (cos x) = x := by rw [arccos, ← sin_pi_div_two_sub, arcsin_sin] <;> simp [sub_eq_add_neg] <;> linarith lemma arccos_eq_of_eq_cos (hy₀ : 0 ≤ y) (hy₁ : y ≤ π) (hxy : x = cos y) : arccos x = y := by rw [hxy, arccos_cos hy₀ hy₁] theorem strictAntiOn_arccos : StrictAntiOn arccos (Icc (-1) 1) := fun _ hx _ hy h => sub_lt_sub_left (strictMonoOn_arcsin hx hy h) _ @[gcongr] lemma arccos_lt_arccos {x y : ℝ} (hx : -1 ≤ x) (hlt : x < y) (hy : y ≤ 1) : arccos y < arccos x := by unfold arccos; gcongr <;> assumption @[gcongr] lemma arccos_le_arccos {x y : ℝ} (hlt : x ≤ y) : arccos y ≤ arccos x := by unfold arccos; gcongr theorem antitone_arccos : Antitone arccos := fun _ _ ↦ arccos_le_arccos theorem arccos_injOn : InjOn arccos (Icc (-1) 1) := strictAntiOn_arccos.injOn theorem arccos_inj {x y : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) (hy₁ : -1 ≤ y) (hy₂ : y ≤ 1) : arccos x = arccos y ↔ x = y := arccos_injOn.eq_iff ⟨hx₁, hx₂⟩ ⟨hy₁, hy₂⟩ @[simp] theorem arccos_zero : arccos 0 = π / 2 := by simp [arccos] @[simp] theorem arccos_one : arccos 1 = 0 := by simp [arccos] @[simp] theorem arccos_neg_one : arccos (-1) = π := by simp [arccos, add_halves] @[simp] theorem arccos_eq_zero {x} : arccos x = 0 ↔ 1 ≤ x := by simp [arccos, sub_eq_zero] @[simp] theorem arccos_eq_pi_div_two {x} : arccos x = π / 2 ↔ x = 0 := by simp [arccos] @[simp] theorem arccos_eq_pi {x} : arccos x = π ↔ x ≤ -1 := by rw [arccos, sub_eq_iff_eq_add, ← sub_eq_iff_eq_add', div_two_sub_self, neg_pi_div_two_eq_arcsin] theorem arccos_neg (x : ℝ) : arccos (-x) = π - arccos x := by rw [← add_halves π, arccos, arcsin_neg, arccos, add_sub_assoc, sub_sub_self, sub_neg_eq_add] theorem arccos_of_one_le {x : ℝ} (hx : 1 ≤ x) : arccos x = 0 := by rw [arccos, arcsin_of_one_le hx, sub_self] theorem arccos_of_le_neg_one {x : ℝ} (hx : x ≤ -1) : arccos x = π := by rw [arccos, arcsin_of_le_neg_one hx, sub_neg_eq_add, add_halves] -- The junk values for `arccos` and `sqrt` make this true even outside `[-1, 1]`. theorem sin_arccos (x : ℝ) : sin (arccos x) = √(1 - x ^ 2) := by by_cases hx₁ : -1 ≤ x; swap · rw [not_le] at hx₁ rw [arccos_of_le_neg_one hx₁.le, sin_pi, sqrt_eq_zero_of_nonpos] nlinarith by_cases hx₂ : x ≤ 1; swap · rw [not_le] at hx₂ rw [arccos_of_one_le hx₂.le, sin_zero, sqrt_eq_zero_of_nonpos] nlinarith rw [arccos_eq_pi_div_two_sub_arcsin, sin_pi_div_two_sub, cos_arcsin] @[simp] theorem arccos_le_pi_div_two {x} : arccos x ≤ π / 2 ↔ 0 ≤ x := by simp [arccos] @[simp] theorem arccos_lt_pi_div_two {x : ℝ} : arccos x < π / 2 ↔ 0 < x := by simp [arccos] @[simp] theorem arccos_le_pi_div_four {x} : arccos x ≤ π / 4 ↔ √2 / 2 ≤ x := by rw [arccos, ← pi_div_four_le_arcsin] constructor <;> · intro linarith @[continuity, fun_prop] theorem continuous_arccos : Continuous arccos := continuous_const.sub continuous_arcsin -- The junk values for `arccos` and `sqrt` make this true even outside `[-1, 1]`. theorem tan_arccos (x : ℝ) : tan (arccos x) = √(1 - x ^ 2) / x := by rw [arccos, tan_pi_div_two_sub, tan_arcsin, inv_div] -- The junk values for `arccos` and `sqrt` make this true even for `1 < x`. theorem arccos_eq_arcsin {x : ℝ} (h : 0 ≤ x) : arccos x = arcsin (√(1 - x ^ 2)) := (arcsin_eq_of_sin_eq (sin_arccos _) ⟨(Left.neg_nonpos_iff.2 (div_nonneg pi_pos.le (by norm_num))).trans (arccos_nonneg _), arccos_le_pi_div_two.2 h⟩).symm -- The junk values for `arcsin` and `sqrt` make this true even for `1 < x`. theorem arcsin_eq_arccos {x : ℝ} (h : 0 ≤ x) : arcsin x = arccos (√(1 - x ^ 2)) := by rw [eq_comm, ← cos_arcsin] exact arccos_cos (arcsin_nonneg.2 h) ((arcsin_le_pi_div_two _).trans (div_le_self pi_pos.le one_le_two)) end Real open Real /-! ### Convenience dot notation lemmas -/ namespace Filter.Tendsto variable {α : Type*} {l : Filter α} {x : ℝ} {f : α → ℝ} protected theorem arcsin (h : Tendsto f l (𝓝 x)) : Tendsto (arcsin <| f ·) l (𝓝 (arcsin x)) := (continuous_arcsin.tendsto _).comp h
theorem arcsin_nhdsLE (h : Tendsto f l (𝓝[≤] x)) : Tendsto (arcsin <| f ·) l (𝓝[≤] (arcsin x)) := by refine ((continuous_arcsin.tendsto _).inf <| MapsTo.tendsto fun y hy ↦ ?_).comp h exact monotone_arcsin hy theorem arcsin_nhdsGE (h : Tendsto f l (𝓝[≥] x)) : Tendsto (arcsin <| f ·) l (𝓝[≥] (arcsin x)) := ((continuous_arcsin.tendsto _).inf <| MapsTo.tendsto fun _ ↦ arcsin_le_arcsin).comp h protected theorem arccos (h : Tendsto f l (𝓝 x)) : Tendsto (arccos <| f ·) l (𝓝 (arccos x)) := (continuous_arccos.tendsto _).comp h
Mathlib/Analysis/SpecialFunctions/Trigonometric/Inverse.lean
423
432
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Patrick Massot, Yury Kudryashov, Rémy Degenne -/ import Mathlib.Algebra.Order.Group.Abs import Mathlib.Algebra.Order.Group.Basic import Mathlib.Algebra.Order.Ring.Defs import Mathlib.Order.Interval.Set.Basic import Mathlib.Logic.Pairwise /-! ### Lemmas about arithmetic operations and intervals. -/ variable {α : Type*} namespace Set section OrderedCommGroup variable [CommGroup α] [PartialOrder α] [IsOrderedMonoid α] {a c d : α} /-! `inv_mem_Ixx_iff`, `sub_mem_Ixx_iff` -/ @[to_additive] theorem inv_mem_Icc_iff : a⁻¹ ∈ Set.Icc c d ↔ a ∈ Set.Icc d⁻¹ c⁻¹ := and_comm.trans <| and_congr inv_le' le_inv' @[to_additive] theorem inv_mem_Ico_iff : a⁻¹ ∈ Set.Ico c d ↔ a ∈ Set.Ioc d⁻¹ c⁻¹ := and_comm.trans <| and_congr inv_lt' le_inv' @[to_additive] theorem inv_mem_Ioc_iff : a⁻¹ ∈ Set.Ioc c d ↔ a ∈ Set.Ico d⁻¹ c⁻¹ := and_comm.trans <| and_congr inv_le' lt_inv' @[to_additive] theorem inv_mem_Ioo_iff : a⁻¹ ∈ Set.Ioo c d ↔ a ∈ Set.Ioo d⁻¹ c⁻¹ := and_comm.trans <| and_congr inv_lt' lt_inv' end OrderedCommGroup section OrderedAddCommGroup variable [AddCommGroup α] [PartialOrder α] [IsOrderedAddMonoid α] {a b c d : α} /-! `add_mem_Ixx_iff_left` -/ theorem add_mem_Icc_iff_left : a + b ∈ Set.Icc c d ↔ a ∈ Set.Icc (c - b) (d - b) := (and_congr sub_le_iff_le_add le_sub_iff_add_le).symm theorem add_mem_Ico_iff_left : a + b ∈ Set.Ico c d ↔ a ∈ Set.Ico (c - b) (d - b) := (and_congr sub_le_iff_le_add lt_sub_iff_add_lt).symm theorem add_mem_Ioc_iff_left : a + b ∈ Set.Ioc c d ↔ a ∈ Set.Ioc (c - b) (d - b) := (and_congr sub_lt_iff_lt_add le_sub_iff_add_le).symm theorem add_mem_Ioo_iff_left : a + b ∈ Set.Ioo c d ↔ a ∈ Set.Ioo (c - b) (d - b) := (and_congr sub_lt_iff_lt_add lt_sub_iff_add_lt).symm /-! `add_mem_Ixx_iff_right` -/ theorem add_mem_Icc_iff_right : a + b ∈ Set.Icc c d ↔ b ∈ Set.Icc (c - a) (d - a) := (and_congr sub_le_iff_le_add' le_sub_iff_add_le').symm theorem add_mem_Ico_iff_right : a + b ∈ Set.Ico c d ↔ b ∈ Set.Ico (c - a) (d - a) := (and_congr sub_le_iff_le_add' lt_sub_iff_add_lt').symm theorem add_mem_Ioc_iff_right : a + b ∈ Set.Ioc c d ↔ b ∈ Set.Ioc (c - a) (d - a) := (and_congr sub_lt_iff_lt_add' le_sub_iff_add_le').symm theorem add_mem_Ioo_iff_right : a + b ∈ Set.Ioo c d ↔ b ∈ Set.Ioo (c - a) (d - a) := (and_congr sub_lt_iff_lt_add' lt_sub_iff_add_lt').symm /-! `sub_mem_Ixx_iff_left` -/ theorem sub_mem_Icc_iff_left : a - b ∈ Set.Icc c d ↔ a ∈ Set.Icc (c + b) (d + b) := and_congr le_sub_iff_add_le sub_le_iff_le_add theorem sub_mem_Ico_iff_left : a - b ∈ Set.Ico c d ↔ a ∈ Set.Ico (c + b) (d + b) := and_congr le_sub_iff_add_le sub_lt_iff_lt_add theorem sub_mem_Ioc_iff_left : a - b ∈ Set.Ioc c d ↔ a ∈ Set.Ioc (c + b) (d + b) := and_congr lt_sub_iff_add_lt sub_le_iff_le_add theorem sub_mem_Ioo_iff_left : a - b ∈ Set.Ioo c d ↔ a ∈ Set.Ioo (c + b) (d + b) := and_congr lt_sub_iff_add_lt sub_lt_iff_lt_add /-! `sub_mem_Ixx_iff_right` -/ theorem sub_mem_Icc_iff_right : a - b ∈ Set.Icc c d ↔ b ∈ Set.Icc (a - d) (a - c) := and_comm.trans <| and_congr sub_le_comm le_sub_comm theorem sub_mem_Ico_iff_right : a - b ∈ Set.Ico c d ↔ b ∈ Set.Ioc (a - d) (a - c) := and_comm.trans <| and_congr sub_lt_comm le_sub_comm theorem sub_mem_Ioc_iff_right : a - b ∈ Set.Ioc c d ↔ b ∈ Set.Ico (a - d) (a - c) := and_comm.trans <| and_congr sub_le_comm lt_sub_comm theorem sub_mem_Ioo_iff_right : a - b ∈ Set.Ioo c d ↔ b ∈ Set.Ioo (a - d) (a - c) := and_comm.trans <| and_congr sub_lt_comm lt_sub_comm -- I think that symmetric intervals deserve attention and API: they arise all the time, -- for instance when considering metric balls in `ℝ`. theorem mem_Icc_iff_abs_le {R : Type*} [AddCommGroup R] [LinearOrder R] [IsOrderedAddMonoid R] {x y z : R} : |x - y| ≤ z ↔ y ∈ Icc (x - z) (x + z) := abs_le.trans <| and_comm.trans <| and_congr sub_le_comm neg_le_sub_iff_le_add /-! `sub_mem_Ixx_zero_right` and `sub_mem_Ixx_zero_iff_right`; this specializes the previous lemmas to the case of reflecting the interval. -/ theorem sub_mem_Icc_zero_iff_right : b - a ∈ Icc 0 b ↔ a ∈ Icc 0 b := by simp only [sub_mem_Icc_iff_right, sub_self, sub_zero] theorem sub_mem_Ico_zero_iff_right : b - a ∈ Ico 0 b ↔ a ∈ Ioc 0 b := by simp only [sub_mem_Ico_iff_right, sub_self, sub_zero] theorem sub_mem_Ioc_zero_iff_right : b - a ∈ Ioc 0 b ↔ a ∈ Ico 0 b := by simp only [sub_mem_Ioc_iff_right, sub_self, sub_zero] theorem sub_mem_Ioo_zero_iff_right : b - a ∈ Ioo 0 b ↔ a ∈ Ioo 0 b := by simp only [sub_mem_Ioo_iff_right, sub_self, sub_zero] end OrderedAddCommGroup section LinearOrderedAddCommGroup variable [AddCommGroup α] [LinearOrder α] [IsOrderedAddMonoid α] /-- If we remove a smaller interval from a larger, the result is nonempty -/ theorem nonempty_Ico_sdiff {x dx y dy : α} (h : dy < dx) (hx : 0 < dx) : Nonempty ↑(Ico x (x + dx) \ Ico y (y + dy)) := by rcases lt_or_le x y with h' | h' · use x simp [*, not_le.2 h'] · use max x (x + dy) simp [*, le_refl] end LinearOrderedAddCommGroup /-! ### Lemmas about disjointness of translates of intervals -/ open scoped Function -- required for scoped `on` notation section PairwiseDisjoint section OrderedCommGroup variable [CommGroup α] [PartialOrder α] [IsOrderedMonoid α] (a b : α) @[to_additive] theorem pairwise_disjoint_Ioc_mul_zpow : Pairwise (Disjoint on fun n : ℤ => Ioc (a * b ^ n) (a * b ^ (n + 1))) := by simp +unfoldPartialApp only [Function.onFun] simp_rw [Set.disjoint_iff] intro m n hmn x hx apply hmn have hb : 1 < b := by have : a * b ^ m < a * b ^ (m + 1) := hx.1.1.trans_le hx.1.2 rwa [mul_lt_mul_iff_left, ← mul_one (b ^ m), zpow_add_one, mul_lt_mul_iff_left] at this have i1 := hx.1.1.trans_le hx.2.2 have i2 := hx.2.1.trans_le hx.1.2 rw [mul_lt_mul_iff_left, zpow_lt_zpow_iff_right hb, Int.lt_add_one_iff] at i1 i2 exact le_antisymm i1 i2 @[to_additive] theorem pairwise_disjoint_Ico_mul_zpow : Pairwise (Disjoint on fun n : ℤ => Ico (a * b ^ n) (a * b ^ (n + 1))) := by simp +unfoldPartialApp only [Function.onFun] simp_rw [Set.disjoint_iff] intro m n hmn x hx apply hmn have hb : 1 < b := by have : a * b ^ m < a * b ^ (m + 1) := hx.1.1.trans_lt hx.1.2 rwa [mul_lt_mul_iff_left, ← mul_one (b ^ m), zpow_add_one, mul_lt_mul_iff_left] at this have i1 := hx.1.1.trans_lt hx.2.2 have i2 := hx.2.1.trans_lt hx.1.2 rw [mul_lt_mul_iff_left, zpow_lt_zpow_iff_right hb, Int.lt_add_one_iff] at i1 i2 exact le_antisymm i1 i2 @[to_additive] theorem pairwise_disjoint_Ioo_mul_zpow : Pairwise (Disjoint on fun n : ℤ => Ioo (a * b ^ n) (a * b ^ (n + 1))) := fun _ _ hmn => (pairwise_disjoint_Ioc_mul_zpow a b hmn).mono Ioo_subset_Ioc_self Ioo_subset_Ioc_self @[to_additive] theorem pairwise_disjoint_Ioc_zpow : Pairwise (Disjoint on fun n : ℤ => Ioc (b ^ n) (b ^ (n + 1))) := by simpa only [one_mul] using pairwise_disjoint_Ioc_mul_zpow 1 b @[to_additive] theorem pairwise_disjoint_Ico_zpow : Pairwise (Disjoint on fun n : ℤ => Ico (b ^ n) (b ^ (n + 1))) := by simpa only [one_mul] using pairwise_disjoint_Ico_mul_zpow 1 b @[to_additive] theorem pairwise_disjoint_Ioo_zpow : Pairwise (Disjoint on fun n : ℤ => Ioo (b ^ n) (b ^ (n + 1))) := by simpa only [one_mul] using pairwise_disjoint_Ioo_mul_zpow 1 b end OrderedCommGroup section OrderedRing variable [Ring α] [PartialOrder α] [IsOrderedRing α] (a : α) theorem pairwise_disjoint_Ioc_add_intCast : Pairwise (Disjoint on fun n : ℤ => Ioc (a + n) (a + n + 1)) := by simpa only [zsmul_one, Int.cast_add, Int.cast_one, ← add_assoc] using pairwise_disjoint_Ioc_add_zsmul a (1 : α) theorem pairwise_disjoint_Ico_add_intCast : Pairwise (Disjoint on fun n : ℤ => Ico (a + n) (a + n + 1)) := by simpa only [zsmul_one, Int.cast_add, Int.cast_one, ← add_assoc] using pairwise_disjoint_Ico_add_zsmul a (1 : α) theorem pairwise_disjoint_Ioo_add_intCast : Pairwise (Disjoint on fun n : ℤ => Ioo (a + n) (a + n + 1)) := by simpa only [zsmul_one, Int.cast_add, Int.cast_one, ← add_assoc] using pairwise_disjoint_Ioo_add_zsmul a (1 : α) variable (α) theorem pairwise_disjoint_Ico_intCast : Pairwise (Disjoint on fun n : ℤ => Ico (n : α) (n + 1)) := by simpa only [zero_add] using pairwise_disjoint_Ico_add_intCast (0 : α) theorem pairwise_disjoint_Ioo_intCast : Pairwise (Disjoint on fun n : ℤ => Ioo (n : α) (n + 1)) := by simpa only [zero_add] using pairwise_disjoint_Ioo_add_intCast (0 : α) theorem pairwise_disjoint_Ioc_intCast : Pairwise (Disjoint on fun n : ℤ => Ioc (n : α) (n + 1)) := by simpa only [zero_add] using pairwise_disjoint_Ioc_add_intCast (0 : α) end OrderedRing end PairwiseDisjoint end Set
Mathlib/Algebra/Order/Interval/Set/Group.lean
267
269
/- Copyright (c) 2022 Yaël Dillies, Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies, Bhavik Mehta -/ import Mathlib.Analysis.InnerProductSpace.Convex import Mathlib.Analysis.InnerProductSpace.PiL2 import Mathlib.Combinatorics.Additive.AP.Three.Defs import Mathlib.Combinatorics.Pigeonhole import Mathlib.Data.Complex.ExponentialBounds /-! # Behrend's bound on Roth numbers This file proves Behrend's lower bound on Roth numbers. This says that we can find a subset of `{1, ..., n}` of size `n / exp (O (sqrt (log n)))` which does not contain arithmetic progressions of length `3`. The idea is that the sphere (in the `n` dimensional Euclidean space) doesn't contain arithmetic progressions (literally) because the corresponding ball is strictly convex. Thus we can take integer points on that sphere and map them onto `ℕ` in a way that preserves arithmetic progressions (`Behrend.map`). ## Main declarations * `Behrend.sphere`: The intersection of the Euclidean sphere with the positive integer quadrant. This is the set that we will map on `ℕ`. * `Behrend.map`: Given a natural number `d`, `Behrend.map d : ℕⁿ → ℕ` reads off the coordinates as digits in base `d`. * `Behrend.card_sphere_le_rothNumberNat`: Implicit lower bound on Roth numbers in terms of `Behrend.sphere`. * `Behrend.roth_lower_bound`: Behrend's explicit lower bound on Roth numbers. ## References * [Bryan Gillespie, *Behrend’s Construction*] (http://www.epsilonsmall.com/resources/behrends-construction/behrend.pdf) * Behrend, F. A., "On sets of integers which contain no three terms in arithmetical progression" * [Wikipedia, *Salem-Spencer set*](https://en.wikipedia.org/wiki/Salem–Spencer_set) ## Tags 3AP-free, Salem-Spencer, Behrend construction, arithmetic progression, sphere, strictly convex -/ assert_not_exists IsConformalMap Conformal open Nat hiding log open Finset Metric Real open scoped Pointwise /-- The frontier of a closed strictly convex set only contains trivial arithmetic progressions. The idea is that an arithmetic progression is contained on a line and the frontier of a strictly convex set does not contain lines. -/ lemma threeAPFree_frontier {𝕜 E : Type*} [Field 𝕜] [LinearOrder 𝕜] [IsStrictOrderedRing 𝕜] [TopologicalSpace E] [AddCommMonoid E] [Module 𝕜 E] {s : Set E} (hs₀ : IsClosed s) (hs₁ : StrictConvex 𝕜 s) : ThreeAPFree (frontier s) := by intro a ha b hb c hc habc obtain rfl : (1 / 2 : 𝕜) • a + (1 / 2 : 𝕜) • c = b := by rwa [← smul_add, one_div, inv_smul_eq_iff₀ (show (2 : 𝕜) ≠ 0 by norm_num), two_smul] have := hs₁.eq (hs₀.frontier_subset ha) (hs₀.frontier_subset hc) one_half_pos one_half_pos (add_halves _) hb.2 simp [this, ← add_smul] ring_nf simp lemma threeAPFree_sphere {E : Type*} [NormedAddCommGroup E] [NormedSpace ℝ E] [StrictConvexSpace ℝ E] (x : E) (r : ℝ) : ThreeAPFree (sphere x r) := by obtain rfl | hr := eq_or_ne r 0 · rw [sphere_zero] exact threeAPFree_singleton _ · convert threeAPFree_frontier isClosed_closedBall (strictConvex_closedBall ℝ x r) exact (frontier_closedBall _ hr).symm namespace Behrend variable {n d k N : ℕ} {x : Fin n → ℕ} /-! ### Turning the sphere into 3AP-free set We define `Behrend.sphere`, the intersection of the $L^2$ sphere with the positive quadrant of integer points. Because the $L^2$ closed ball is strictly convex, the $L^2$ sphere and `Behrend.sphere` are 3AP-free (`threeAPFree_sphere`). Then we can turn this set in `Fin n → ℕ` into a set in `ℕ` using `Behrend.map`, which preserves `ThreeAPFree` because it is an additive monoid homomorphism. -/ /-- The box `{0, ..., d - 1}^n` as a `Finset`. -/ def box (n d : ℕ) : Finset (Fin n → ℕ) := Fintype.piFinset fun _ => range d theorem mem_box : x ∈ box n d ↔ ∀ i, x i < d := by simp only [box, Fintype.mem_piFinset, mem_range] @[simp] theorem card_box : #(box n d) = d ^ n := by simp [box] @[simp] theorem box_zero : box (n + 1) 0 = ∅ := by simp [box] /-- The intersection of the sphere of radius `√k` with the integer points in the positive quadrant. -/ def sphere (n d k : ℕ) : Finset (Fin n → ℕ) := {x ∈ box n d | ∑ i, x i ^ 2 = k} theorem sphere_zero_subset : sphere n d 0 ⊆ 0 := fun x => by simp [sphere, funext_iff] @[simp] theorem sphere_zero_right (n k : ℕ) : sphere (n + 1) 0 k = ∅ := by simp [sphere] theorem sphere_subset_box : sphere n d k ⊆ box n d := filter_subset _ _ theorem norm_of_mem_sphere {x : Fin n → ℕ} (hx : x ∈ sphere n d k) : ‖(WithLp.equiv 2 _).symm ((↑) ∘ x : Fin n → ℝ)‖ = √↑k := by rw [EuclideanSpace.norm_eq] dsimp simp_rw [abs_cast, ← cast_pow, ← cast_sum, (mem_filter.1 hx).2] theorem sphere_subset_preimage_metric_sphere : (sphere n d k : Set (Fin n → ℕ)) ⊆ (fun x : Fin n → ℕ => (WithLp.equiv 2 _).symm ((↑) ∘ x : Fin n → ℝ)) ⁻¹' Metric.sphere (0 : PiLp 2 fun _ : Fin n => ℝ) (√↑k) :=
fun x hx => by rw [Set.mem_preimage, mem_sphere_zero_iff_norm, norm_of_mem_sphere hx] /-- The map that appears in Behrend's bound on Roth numbers. -/ @[simps] def map (d : ℕ) : (Fin n → ℕ) →+ ℕ where
Mathlib/Combinatorics/Additive/AP/Three/Behrend.lean
125
129
/- Copyright (c) 2022 Kexing Ying. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kexing Ying -/ import Mathlib.MeasureTheory.Function.StronglyMeasurable.Basic /-! # Egorov theorem This file contains the Egorov theorem which states that an almost everywhere convergent sequence on a finite measure space converges uniformly except on an arbitrarily small set. This theorem is useful for the Vitali convergence theorem as well as theorems regarding convergence in measure. ## Main results * `MeasureTheory.tendstoUniformlyOn_of_ae_tendsto`: Egorov's theorem which shows that a sequence of almost everywhere convergent functions converges uniformly except on an arbitrarily small set. -/ noncomputable section open MeasureTheory NNReal ENNReal Topology namespace MeasureTheory open Set Filter TopologicalSpace variable {α β ι : Type*} {m : MeasurableSpace α} [MetricSpace β] {μ : Measure α} namespace Egorov /-- Given a sequence of functions `f` and a function `g`, `notConvergentSeq f g n j` is the set of elements such that `f k x` and `g x` are separated by at least `1 / (n + 1)` for some `k ≥ j`. This definition is useful for Egorov's theorem. -/ def notConvergentSeq [Preorder ι] (f : ι → α → β) (g : α → β) (n : ℕ) (j : ι) : Set α := ⋃ (k) (_ : j ≤ k), { x | 1 / (n + 1 : ℝ) < dist (f k x) (g x) } variable {n : ℕ} {j : ι} {s : Set α} {ε : ℝ} {f : ι → α → β} {g : α → β} theorem mem_notConvergentSeq_iff [Preorder ι] {x : α} : x ∈ notConvergentSeq f g n j ↔ ∃ k ≥ j, 1 / (n + 1 : ℝ) < dist (f k x) (g x) := by simp_rw [notConvergentSeq, Set.mem_iUnion, exists_prop, mem_setOf] theorem notConvergentSeq_antitone [Preorder ι] : Antitone (notConvergentSeq f g n) := fun _ _ hjk => Set.iUnion₂_mono' fun l hl => ⟨l, le_trans hjk hl, Set.Subset.rfl⟩ theorem measure_inter_notConvergentSeq_eq_zero [SemilatticeSup ι] [Nonempty ι] (hfg : ∀ᵐ x ∂μ, x ∈ s → Tendsto (fun n => f n x) atTop (𝓝 (g x))) (n : ℕ) : μ (s ∩ ⋂ j, notConvergentSeq f g n j) = 0 := by simp_rw [Metric.tendsto_atTop, ae_iff] at hfg rw [← nonpos_iff_eq_zero, ← hfg] refine measure_mono fun x => ?_ simp only [Set.mem_inter_iff, Set.mem_iInter, mem_notConvergentSeq_iff] push_neg rintro ⟨hmem, hx⟩ refine ⟨hmem, 1 / (n + 1 : ℝ), Nat.one_div_pos_of_nat, fun N => ?_⟩ obtain ⟨n, hn₁, hn₂⟩ := hx N exact ⟨n, hn₁, hn₂.le⟩ theorem notConvergentSeq_measurableSet [Preorder ι] [Countable ι] (hf : ∀ n, StronglyMeasurable[m] (f n)) (hg : StronglyMeasurable g) : MeasurableSet (notConvergentSeq f g n j) := MeasurableSet.iUnion fun k => MeasurableSet.iUnion fun _ => StronglyMeasurable.measurableSet_lt stronglyMeasurable_const <| (hf k).dist hg theorem measure_notConvergentSeq_tendsto_zero [SemilatticeSup ι] [Countable ι] (hf : ∀ n, StronglyMeasurable (f n)) (hg : StronglyMeasurable g) (hsm : MeasurableSet s) (hs : μ s ≠ ∞) (hfg : ∀ᵐ x ∂μ, x ∈ s → Tendsto (fun n => f n x) atTop (𝓝 (g x))) (n : ℕ) : Tendsto (fun j => μ (s ∩ notConvergentSeq f g n j)) atTop (𝓝 0) := by rcases isEmpty_or_nonempty ι with h | h · have : (fun j => μ (s ∩ notConvergentSeq f g n j)) = fun j => 0 := by simp only [eq_iff_true_of_subsingleton] rw [this] exact tendsto_const_nhds rw [← measure_inter_notConvergentSeq_eq_zero hfg n, Set.inter_iInter] refine tendsto_measure_iInter_atTop (fun n ↦ (hsm.inter <| notConvergentSeq_measurableSet hf hg).nullMeasurableSet) (fun k l hkl => Set.inter_subset_inter_right _ <| notConvergentSeq_antitone hkl) ⟨h.some, ne_top_of_le_ne_top hs (measure_mono Set.inter_subset_left)⟩ variable [SemilatticeSup ι] [Nonempty ι] [Countable ι] theorem exists_notConvergentSeq_lt (hε : 0 < ε) (hf : ∀ n, StronglyMeasurable (f n)) (hg : StronglyMeasurable g) (hsm : MeasurableSet s) (hs : μ s ≠ ∞) (hfg : ∀ᵐ x ∂μ, x ∈ s → Tendsto (fun n => f n x) atTop (𝓝 (g x))) (n : ℕ) : ∃ j : ι, μ (s ∩ notConvergentSeq f g n j) ≤ ENNReal.ofReal (ε * 2⁻¹ ^ n) := by have ⟨N, hN⟩ := (ENNReal.tendsto_atTop ENNReal.zero_ne_top).1 (measure_notConvergentSeq_tendsto_zero hf hg hsm hs hfg n) (ENNReal.ofReal (ε * 2⁻¹ ^ n)) (by rw [gt_iff_lt, ENNReal.ofReal_pos] exact mul_pos hε (pow_pos (by norm_num) n))
rw [zero_add] at hN exact ⟨N, (hN N le_rfl).2⟩ /-- Given some `ε > 0`, `notConvergentSeqLTIndex` provides the index such that `notConvergentSeq` (intersected with a set of finite measure) has measure less than `ε * 2⁻¹ ^ n`. This definition is useful for Egorov's theorem. -/ def notConvergentSeqLTIndex (hε : 0 < ε) (hf : ∀ n, StronglyMeasurable (f n)) (hg : StronglyMeasurable g) (hsm : MeasurableSet s) (hs : μ s ≠ ∞)
Mathlib/MeasureTheory/Function/Egorov.lean
98
107
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Kim Morrison, Jens Wagemaker -/ import Mathlib.Algebra.Group.Submonoid.Operations import Mathlib.Algebra.MonoidAlgebra.Defs import Mathlib.Algebra.Order.Monoid.Unbundled.WithTop import Mathlib.Algebra.Ring.Action.Rat import Mathlib.Data.Finset.Sort import Mathlib.Tactic.FastInstance /-! # Theory of univariate polynomials This file defines `Polynomial R`, the type of univariate polynomials over the semiring `R`, builds a semiring structure on it, and gives basic definitions that are expanded in other files in this directory. ## Main definitions * `monomial n a` is the polynomial `a X^n`. Note that `monomial n` is defined as an `R`-linear map. * `C a` is the constant polynomial `a`. Note that `C` is defined as a ring homomorphism. * `X` is the polynomial `X`, i.e., `monomial 1 1`. * `p.sum f` is `∑ n ∈ p.support, f n (p.coeff n)`, i.e., one sums the values of functions applied to coefficients of the polynomial `p`. * `p.erase n` is the polynomial `p` in which one removes the `c X^n` term. There are often two natural variants of lemmas involving sums, depending on whether one acts on the polynomials, or on the function. The naming convention is that one adds `index` when acting on the polynomials. For instance, * `sum_add_index` states that `(p + q).sum f = p.sum f + q.sum f`; * `sum_add` states that `p.sum (fun n x ↦ f n x + g n x) = p.sum f + p.sum g`. * Notation to refer to `Polynomial R`, as `R[X]` or `R[t]`. ## Implementation Polynomials are defined using `R[ℕ]`, where `R` is a semiring. The variable `X` commutes with every polynomial `p`: lemma `X_mul` proves the identity `X * p = p * X`. The relationship to `R[ℕ]` is through a structure to make polynomials irreducible from the point of view of the kernel. Most operations are irreducible since Lean can not compute anyway with `AddMonoidAlgebra`. There are two exceptions that we make semireducible: * The zero polynomial, so that its coefficients are definitionally equal to `0`. * The scalar action, to permit typeclass search to unfold it to resolve potential instance diamonds. The raw implementation of the equivalence between `R[X]` and `R[ℕ]` is done through `ofFinsupp` and `toFinsupp` (or, equivalently, `rcases p` when `p` is a polynomial gives an element `q` of `R[ℕ]`, and conversely `⟨q⟩` gives back `p`). The equivalence is also registered as a ring equiv in `Polynomial.toFinsuppIso`. These should in general not be used once the basic API for polynomials is constructed. -/ noncomputable section /-- `Polynomial R` is the type of univariate polynomials over `R`, denoted as `R[X]` within the `Polynomial` namespace. Polynomials should be seen as (semi-)rings with the additional constructor `X`. The embedding from `R` is called `C`. -/ structure Polynomial (R : Type*) [Semiring R] where ofFinsupp :: toFinsupp : AddMonoidAlgebra R ℕ @[inherit_doc] scoped[Polynomial] notation:9000 R "[X]" => Polynomial R open AddMonoidAlgebra Finset open Finsupp hiding single open Function hiding Commute namespace Polynomial universe u variable {R : Type u} {a b : R} {m n : ℕ} section Semiring variable [Semiring R] {p q : R[X]} theorem forall_iff_forall_finsupp (P : R[X] → Prop) : (∀ p, P p) ↔ ∀ q : R[ℕ], P ⟨q⟩ := ⟨fun h q => h ⟨q⟩, fun h ⟨p⟩ => h p⟩ theorem exists_iff_exists_finsupp (P : R[X] → Prop) : (∃ p, P p) ↔ ∃ q : R[ℕ], P ⟨q⟩ := ⟨fun ⟨⟨p⟩, hp⟩ => ⟨p, hp⟩, fun ⟨q, hq⟩ => ⟨⟨q⟩, hq⟩⟩ @[simp] theorem eta (f : R[X]) : Polynomial.ofFinsupp f.toFinsupp = f := by cases f; rfl /-! ### Conversions to and from `AddMonoidAlgebra` Since `R[X]` is not defeq to `R[ℕ]`, but instead is a structure wrapping it, we have to copy across all the arithmetic operators manually, along with the lemmas about how they unfold around `Polynomial.ofFinsupp` and `Polynomial.toFinsupp`. -/ section AddMonoidAlgebra private irreducible_def add : R[X] → R[X] → R[X] | ⟨a⟩, ⟨b⟩ => ⟨a + b⟩ private irreducible_def neg {R : Type u} [Ring R] : R[X] → R[X] | ⟨a⟩ => ⟨-a⟩ private irreducible_def mul : R[X] → R[X] → R[X] | ⟨a⟩, ⟨b⟩ => ⟨a * b⟩ instance zero : Zero R[X] := ⟨⟨0⟩⟩ instance one : One R[X] := ⟨⟨1⟩⟩ instance add' : Add R[X] := ⟨add⟩ instance neg' {R : Type u} [Ring R] : Neg R[X] := ⟨neg⟩ instance sub {R : Type u} [Ring R] : Sub R[X] := ⟨fun a b => a + -b⟩ instance mul' : Mul R[X] := ⟨mul⟩ -- If the private definitions are accidentally exposed, simplify them away. @[simp] theorem add_eq_add : add p q = p + q := rfl @[simp] theorem mul_eq_mul : mul p q = p * q := rfl instance instNSMul : SMul ℕ R[X] where smul r p := ⟨r • p.toFinsupp⟩ instance smulZeroClass {S : Type*} [SMulZeroClass S R] : SMulZeroClass S R[X] where smul r p := ⟨r • p.toFinsupp⟩ smul_zero a := congr_arg ofFinsupp (smul_zero a) instance {S : Type*} [Zero S] [SMulZeroClass S R] [NoZeroSMulDivisors S R] : NoZeroSMulDivisors S R[X] where eq_zero_or_eq_zero_of_smul_eq_zero eq := (eq_zero_or_eq_zero_of_smul_eq_zero <| congr_arg toFinsupp eq).imp id (congr_arg ofFinsupp) -- to avoid a bug in the `ring` tactic instance (priority := 1) pow : Pow R[X] ℕ where pow p n := npowRec n p @[simp] theorem ofFinsupp_zero : (⟨0⟩ : R[X]) = 0 := rfl @[simp] theorem ofFinsupp_one : (⟨1⟩ : R[X]) = 1 := rfl @[simp] theorem ofFinsupp_add {a b} : (⟨a + b⟩ : R[X]) = ⟨a⟩ + ⟨b⟩ := show _ = add _ _ by rw [add_def] @[simp] theorem ofFinsupp_neg {R : Type u} [Ring R] {a} : (⟨-a⟩ : R[X]) = -⟨a⟩ := show _ = neg _ by rw [neg_def] @[simp] theorem ofFinsupp_sub {R : Type u} [Ring R] {a b} : (⟨a - b⟩ : R[X]) = ⟨a⟩ - ⟨b⟩ := by rw [sub_eq_add_neg, ofFinsupp_add, ofFinsupp_neg] rfl @[simp] theorem ofFinsupp_mul (a b) : (⟨a * b⟩ : R[X]) = ⟨a⟩ * ⟨b⟩ := show _ = mul _ _ by rw [mul_def] @[simp] theorem ofFinsupp_nsmul (a : ℕ) (b) : (⟨a • b⟩ : R[X]) = (a • ⟨b⟩ : R[X]) := rfl @[simp] theorem ofFinsupp_smul {S : Type*} [SMulZeroClass S R] (a : S) (b) : (⟨a • b⟩ : R[X]) = (a • ⟨b⟩ : R[X]) := rfl @[simp] theorem ofFinsupp_pow (a) (n : ℕ) : (⟨a ^ n⟩ : R[X]) = ⟨a⟩ ^ n := by change _ = npowRec n _ induction n with | zero => simp [npowRec] | succ n n_ih => simp [npowRec, n_ih, pow_succ] @[simp] theorem toFinsupp_zero : (0 : R[X]).toFinsupp = 0 := rfl @[simp] theorem toFinsupp_one : (1 : R[X]).toFinsupp = 1 := rfl @[simp] theorem toFinsupp_add (a b : R[X]) : (a + b).toFinsupp = a.toFinsupp + b.toFinsupp := by cases a cases b rw [← ofFinsupp_add] @[simp] theorem toFinsupp_neg {R : Type u} [Ring R] (a : R[X]) : (-a).toFinsupp = -a.toFinsupp := by cases a rw [← ofFinsupp_neg] @[simp] theorem toFinsupp_sub {R : Type u} [Ring R] (a b : R[X]) : (a - b).toFinsupp = a.toFinsupp - b.toFinsupp := by rw [sub_eq_add_neg, ← toFinsupp_neg, ← toFinsupp_add] rfl @[simp] theorem toFinsupp_mul (a b : R[X]) : (a * b).toFinsupp = a.toFinsupp * b.toFinsupp := by cases a cases b rw [← ofFinsupp_mul] @[simp] theorem toFinsupp_nsmul (a : ℕ) (b : R[X]) : (a • b).toFinsupp = a • b.toFinsupp := rfl @[simp] theorem toFinsupp_smul {S : Type*} [SMulZeroClass S R] (a : S) (b : R[X]) : (a • b).toFinsupp = a • b.toFinsupp := rfl @[simp] theorem toFinsupp_pow (a : R[X]) (n : ℕ) : (a ^ n).toFinsupp = a.toFinsupp ^ n := by cases a rw [← ofFinsupp_pow] theorem _root_.IsSMulRegular.polynomial {S : Type*} [SMulZeroClass S R] {a : S} (ha : IsSMulRegular R a) : IsSMulRegular R[X] a | ⟨_x⟩, ⟨_y⟩, h => congr_arg _ <| ha.finsupp (Polynomial.ofFinsupp.inj h) theorem toFinsupp_injective : Function.Injective (toFinsupp : R[X] → AddMonoidAlgebra _ _) := fun ⟨_x⟩ ⟨_y⟩ => congr_arg _ @[simp] theorem toFinsupp_inj {a b : R[X]} : a.toFinsupp = b.toFinsupp ↔ a = b := toFinsupp_injective.eq_iff @[simp] theorem toFinsupp_eq_zero {a : R[X]} : a.toFinsupp = 0 ↔ a = 0 := by rw [← toFinsupp_zero, toFinsupp_inj] @[simp] theorem toFinsupp_eq_one {a : R[X]} : a.toFinsupp = 1 ↔ a = 1 := by rw [← toFinsupp_one, toFinsupp_inj] /-- A more convenient spelling of `Polynomial.ofFinsupp.injEq` in terms of `Iff`. -/ theorem ofFinsupp_inj {a b} : (⟨a⟩ : R[X]) = ⟨b⟩ ↔ a = b := iff_of_eq (ofFinsupp.injEq _ _) @[simp] theorem ofFinsupp_eq_zero {a} : (⟨a⟩ : R[X]) = 0 ↔ a = 0 := by rw [← ofFinsupp_zero, ofFinsupp_inj] @[simp] theorem ofFinsupp_eq_one {a} : (⟨a⟩ : R[X]) = 1 ↔ a = 1 := by rw [← ofFinsupp_one, ofFinsupp_inj] instance inhabited : Inhabited R[X] := ⟨0⟩ instance instNatCast : NatCast R[X] where natCast n := ofFinsupp n @[simp] theorem ofFinsupp_natCast (n : ℕ) : (⟨n⟩ : R[X]) = n := rfl @[simp] theorem toFinsupp_natCast (n : ℕ) : (n : R[X]).toFinsupp = n := rfl @[simp] theorem ofFinsupp_ofNat (n : ℕ) [n.AtLeastTwo] : (⟨ofNat(n)⟩ : R[X]) = ofNat(n) := rfl @[simp] theorem toFinsupp_ofNat (n : ℕ) [n.AtLeastTwo] : (ofNat(n) : R[X]).toFinsupp = ofNat(n) := rfl instance semiring : Semiring R[X] := fast_instance% Function.Injective.semiring toFinsupp toFinsupp_injective toFinsupp_zero toFinsupp_one toFinsupp_add toFinsupp_mul (fun _ _ => toFinsupp_nsmul _ _) toFinsupp_pow fun _ => rfl instance distribSMul {S} [DistribSMul S R] : DistribSMul S R[X] := fast_instance% Function.Injective.distribSMul ⟨⟨toFinsupp, toFinsupp_zero⟩, toFinsupp_add⟩ toFinsupp_injective toFinsupp_smul instance distribMulAction {S} [Monoid S] [DistribMulAction S R] : DistribMulAction S R[X] := fast_instance% Function.Injective.distribMulAction ⟨⟨toFinsupp, toFinsupp_zero (R := R)⟩, toFinsupp_add⟩ toFinsupp_injective toFinsupp_smul instance faithfulSMul {S} [SMulZeroClass S R] [FaithfulSMul S R] : FaithfulSMul S R[X] where eq_of_smul_eq_smul {_s₁ _s₂} h := eq_of_smul_eq_smul fun a : ℕ →₀ R => congr_arg toFinsupp (h ⟨a⟩) instance module {S} [Semiring S] [Module S R] : Module S R[X] := fast_instance% Function.Injective.module _ ⟨⟨toFinsupp, toFinsupp_zero⟩, toFinsupp_add⟩ toFinsupp_injective toFinsupp_smul instance smulCommClass {S₁ S₂} [SMulZeroClass S₁ R] [SMulZeroClass S₂ R] [SMulCommClass S₁ S₂ R] : SMulCommClass S₁ S₂ R[X] := ⟨by rintro m n ⟨f⟩ simp_rw [← ofFinsupp_smul, smul_comm m n f]⟩ instance isScalarTower {S₁ S₂} [SMul S₁ S₂] [SMulZeroClass S₁ R] [SMulZeroClass S₂ R] [IsScalarTower S₁ S₂ R] : IsScalarTower S₁ S₂ R[X] := ⟨by rintro _ _ ⟨⟩ simp_rw [← ofFinsupp_smul, smul_assoc]⟩ instance isScalarTower_right {α K : Type*} [Semiring K] [DistribSMul α K] [IsScalarTower α K K] : IsScalarTower α K[X] K[X] := ⟨by rintro _ ⟨⟩ ⟨⟩ simp_rw [smul_eq_mul, ← ofFinsupp_smul, ← ofFinsupp_mul, ← ofFinsupp_smul, smul_mul_assoc]⟩ instance isCentralScalar {S} [SMulZeroClass S R] [SMulZeroClass Sᵐᵒᵖ R] [IsCentralScalar S R] : IsCentralScalar S R[X] := ⟨by rintro _ ⟨⟩ simp_rw [← ofFinsupp_smul, op_smul_eq_smul]⟩ instance unique [Subsingleton R] : Unique R[X] := { Polynomial.inhabited with uniq := by rintro ⟨x⟩ apply congr_arg ofFinsupp simp [eq_iff_true_of_subsingleton] } variable (R) /-- Ring isomorphism between `R[X]` and `R[ℕ]`. This is just an implementation detail, but it can be useful to transfer results from `Finsupp` to polynomials. -/ @[simps apply symm_apply] def toFinsuppIso : R[X] ≃+* R[ℕ] where toFun := toFinsupp invFun := ofFinsupp left_inv := fun ⟨_p⟩ => rfl right_inv _p := rfl map_mul' := toFinsupp_mul map_add' := toFinsupp_add instance [DecidableEq R] : DecidableEq R[X] := @Equiv.decidableEq R[X] _ (toFinsuppIso R).toEquiv (Finsupp.instDecidableEq) /-- Linear isomorphism between `R[X]` and `R[ℕ]`. This is just an implementation detail, but it can be useful to transfer results from `Finsupp` to polynomials. -/ @[simps!] def toFinsuppIsoLinear : R[X] ≃ₗ[R] R[ℕ] where __ := toFinsuppIso R map_smul' _ _ := rfl end AddMonoidAlgebra theorem ofFinsupp_sum {ι : Type*} (s : Finset ι) (f : ι → R[ℕ]) : (⟨∑ i ∈ s, f i⟩ : R[X]) = ∑ i ∈ s, ⟨f i⟩ := map_sum (toFinsuppIso R).symm f s theorem toFinsupp_sum {ι : Type*} (s : Finset ι) (f : ι → R[X]) : (∑ i ∈ s, f i : R[X]).toFinsupp = ∑ i ∈ s, (f i).toFinsupp := map_sum (toFinsuppIso R) f s /-- The set of all `n` such that `X^n` has a non-zero coefficient. -/ def support : R[X] → Finset ℕ | ⟨p⟩ => p.support @[simp] theorem support_ofFinsupp (p) : support (⟨p⟩ : R[X]) = p.support := by rw [support] theorem support_toFinsupp (p : R[X]) : p.toFinsupp.support = p.support := by rw [support] @[simp] theorem support_zero : (0 : R[X]).support = ∅ := rfl @[simp] theorem support_eq_empty : p.support = ∅ ↔ p = 0 := by rcases p with ⟨⟩ simp [support] @[simp] lemma support_nonempty : p.support.Nonempty ↔ p ≠ 0 := Finset.nonempty_iff_ne_empty.trans support_eq_empty.not theorem card_support_eq_zero : #p.support = 0 ↔ p = 0 := by simp /-- `monomial s a` is the monomial `a * X^s` -/ def monomial (n : ℕ) : R →ₗ[R] R[X] where toFun t := ⟨Finsupp.single n t⟩ -- Porting note (https://github.com/leanprover-community/mathlib4/issues/10745): was `simp`. map_add' x y := by simp; rw [ofFinsupp_add] -- Porting note (https://github.com/leanprover-community/mathlib4/issues/10745): was `simp [← ofFinsupp_smul]`. map_smul' r x := by simp; rw [← ofFinsupp_smul, smul_single'] @[simp] theorem toFinsupp_monomial (n : ℕ) (r : R) : (monomial n r).toFinsupp = Finsupp.single n r := by simp [monomial] @[simp] theorem ofFinsupp_single (n : ℕ) (r : R) : (⟨Finsupp.single n r⟩ : R[X]) = monomial n r := by simp [monomial] @[simp] theorem monomial_zero_right (n : ℕ) : monomial n (0 : R) = 0 := (monomial n).map_zero -- This is not a `simp` lemma as `monomial_zero_left` is more general. theorem monomial_zero_one : monomial 0 (1 : R) = 1 := rfl -- TODO: can't we just delete this one? theorem monomial_add (n : ℕ) (r s : R) : monomial n (r + s) = monomial n r + monomial n s := (monomial n).map_add _ _ theorem monomial_mul_monomial (n m : ℕ) (r s : R) : monomial n r * monomial m s = monomial (n + m) (r * s) := toFinsupp_injective <| by simp only [toFinsupp_monomial, toFinsupp_mul, AddMonoidAlgebra.single_mul_single] @[simp] theorem monomial_pow (n : ℕ) (r : R) (k : ℕ) : monomial n r ^ k = monomial (n * k) (r ^ k) := by induction k with | zero => simp [pow_zero, monomial_zero_one] | succ k ih => simp [pow_succ, ih, monomial_mul_monomial, mul_add, add_comm] theorem smul_monomial {S} [SMulZeroClass S R] (a : S) (n : ℕ) (b : R) : a • monomial n b = monomial n (a • b) := toFinsupp_injective <| AddMonoidAlgebra.smul_single _ _ _ theorem monomial_injective (n : ℕ) : Function.Injective (monomial n : R → R[X]) := (toFinsuppIso R).symm.injective.comp (single_injective n) @[simp] theorem monomial_eq_zero_iff (t : R) (n : ℕ) : monomial n t = 0 ↔ t = 0 := LinearMap.map_eq_zero_iff _ (Polynomial.monomial_injective n) theorem monomial_eq_monomial_iff {m n : ℕ} {a b : R} : monomial m a = monomial n b ↔ m = n ∧ a = b ∨ a = 0 ∧ b = 0 := by rw [← toFinsupp_inj, toFinsupp_monomial, toFinsupp_monomial, Finsupp.single_eq_single_iff] theorem support_add : (p + q).support ⊆ p.support ∪ q.support := by simpa [support] using Finsupp.support_add /-- `C a` is the constant polynomial `a`. `C` is provided as a ring homomorphism. -/ def C : R →+* R[X] := { monomial 0 with map_one' := by simp [monomial_zero_one] map_mul' := by simp [monomial_mul_monomial] map_zero' := by simp } @[simp] theorem monomial_zero_left (a : R) : monomial 0 a = C a := rfl @[simp] theorem toFinsupp_C (a : R) : (C a).toFinsupp = single 0 a := rfl theorem C_0 : C (0 : R) = 0 := by simp theorem C_1 : C (1 : R) = 1 := rfl theorem C_mul : C (a * b) = C a * C b := C.map_mul a b theorem C_add : C (a + b) = C a + C b := C.map_add a b @[simp] theorem smul_C {S} [SMulZeroClass S R] (s : S) (r : R) : s • C r = C (s • r) := smul_monomial _ _ r theorem C_pow : C (a ^ n) = C a ^ n := C.map_pow a n theorem C_eq_natCast (n : ℕ) : C (n : R) = (n : R[X]) := map_natCast C n @[simp] theorem C_mul_monomial : C a * monomial n b = monomial n (a * b) := by simp only [← monomial_zero_left, monomial_mul_monomial, zero_add] @[simp] theorem monomial_mul_C : monomial n a * C b = monomial n (a * b) := by simp only [← monomial_zero_left, monomial_mul_monomial, add_zero] /-- `X` is the polynomial variable (aka indeterminate). -/ def X : R[X] := monomial 1 1 theorem monomial_one_one_eq_X : monomial 1 (1 : R) = X := rfl theorem monomial_one_right_eq_X_pow (n : ℕ) : monomial n (1 : R) = X ^ n := by induction n with | zero => simp [monomial_zero_one] | succ n ih => rw [pow_succ, ← ih, ← monomial_one_one_eq_X, monomial_mul_monomial, mul_one] @[simp] theorem toFinsupp_X : X.toFinsupp = Finsupp.single 1 (1 : R) := rfl theorem X_ne_C [Nontrivial R] (a : R) : X ≠ C a := by intro he simpa using monomial_eq_monomial_iff.1 he /-- `X` commutes with everything, even when the coefficients are noncommutative. -/ theorem X_mul : X * p = p * X := by rcases p with ⟨⟩ simp only [X, ← ofFinsupp_single, ← ofFinsupp_mul, LinearMap.coe_mk, ofFinsupp.injEq] ext simp [AddMonoidAlgebra.mul_apply, AddMonoidAlgebra.sum_single_index, add_comm] theorem X_pow_mul {n : ℕ} : X ^ n * p = p * X ^ n := by induction n with | zero => simp | succ n ih => conv_lhs => rw [pow_succ] rw [mul_assoc, X_mul, ← mul_assoc, ih, mul_assoc, ← pow_succ] /-- Prefer putting constants to the left of `X`. This lemma is the loop-avoiding `simp` version of `Polynomial.X_mul`. -/ @[simp] theorem X_mul_C (r : R) : X * C r = C r * X := X_mul /-- Prefer putting constants to the left of `X ^ n`. This lemma is the loop-avoiding `simp` version of `X_pow_mul`. -/ @[simp] theorem X_pow_mul_C (r : R) (n : ℕ) : X ^ n * C r = C r * X ^ n := X_pow_mul theorem X_pow_mul_assoc {n : ℕ} : p * X ^ n * q = p * q * X ^ n := by rw [mul_assoc, X_pow_mul, ← mul_assoc] /-- Prefer putting constants to the left of `X ^ n`. This lemma is the loop-avoiding `simp` version of `X_pow_mul_assoc`. -/ @[simp] theorem X_pow_mul_assoc_C {n : ℕ} (r : R) : p * X ^ n * C r = p * C r * X ^ n := X_pow_mul_assoc theorem commute_X (p : R[X]) : Commute X p := X_mul theorem commute_X_pow (p : R[X]) (n : ℕ) : Commute (X ^ n) p := X_pow_mul @[simp] theorem monomial_mul_X (n : ℕ) (r : R) : monomial n r * X = monomial (n + 1) r := by rw [X, monomial_mul_monomial, mul_one] @[simp] theorem monomial_mul_X_pow (n : ℕ) (r : R) (k : ℕ) : monomial n r * X ^ k = monomial (n + k) r := by induction k with | zero => simp | succ k ih => simp [ih, pow_succ, ← mul_assoc, add_assoc] @[simp] theorem X_mul_monomial (n : ℕ) (r : R) : X * monomial n r = monomial (n + 1) r := by rw [X_mul, monomial_mul_X] @[simp] theorem X_pow_mul_monomial (k n : ℕ) (r : R) : X ^ k * monomial n r = monomial (n + k) r := by rw [X_pow_mul, monomial_mul_X_pow] /-- `coeff p n` (often denoted `p.coeff n`) is the coefficient of `X^n` in `p`. -/ def coeff : R[X] → ℕ → R | ⟨p⟩ => p @[simp] theorem coeff_ofFinsupp (p) : coeff (⟨p⟩ : R[X]) = p := by rw [coeff] theorem coeff_injective : Injective (coeff : R[X] → ℕ → R) := by rintro ⟨p⟩ ⟨q⟩ simp only [coeff, DFunLike.coe_fn_eq, imp_self, ofFinsupp.injEq] @[simp] theorem coeff_inj : p.coeff = q.coeff ↔ p = q := coeff_injective.eq_iff theorem toFinsupp_apply (f : R[X]) (i) : f.toFinsupp i = f.coeff i := by cases f; rfl theorem coeff_monomial : coeff (monomial n a) m = if n = m then a else 0 := by simp [coeff, Finsupp.single_apply] @[simp] theorem coeff_monomial_same (n : ℕ) (c : R) : (monomial n c).coeff n = c := Finsupp.single_eq_same theorem coeff_monomial_of_ne {m n : ℕ} (c : R) (h : n ≠ m) : (monomial n c).coeff m = 0 := Finsupp.single_eq_of_ne h @[simp] theorem coeff_zero (n : ℕ) : coeff (0 : R[X]) n = 0 := rfl theorem coeff_one {n : ℕ} : coeff (1 : R[X]) n = if n = 0 then 1 else 0 := by simp_rw [eq_comm (a := n) (b := 0)] exact coeff_monomial @[simp] theorem coeff_one_zero : coeff (1 : R[X]) 0 = 1 := by simp [coeff_one] @[simp] theorem coeff_X_one : coeff (X : R[X]) 1 = 1 := coeff_monomial @[simp] theorem coeff_X_zero : coeff (X : R[X]) 0 = 0 := coeff_monomial @[simp] theorem coeff_monomial_succ : coeff (monomial (n + 1) a) 0 = 0 := by simp [coeff_monomial] theorem coeff_X : coeff (X : R[X]) n = if 1 = n then 1 else 0 := coeff_monomial theorem coeff_X_of_ne_one {n : ℕ} (hn : n ≠ 1) : coeff (X : R[X]) n = 0 := by rw [coeff_X, if_neg hn.symm] @[simp] theorem mem_support_iff : n ∈ p.support ↔ p.coeff n ≠ 0 := by rcases p with ⟨⟩ simp theorem not_mem_support_iff : n ∉ p.support ↔ p.coeff n = 0 := by simp theorem coeff_C : coeff (C a) n = ite (n = 0) a 0 := by convert coeff_monomial (a := a) (m := n) (n := 0) using 2 simp [eq_comm] @[simp] theorem coeff_C_zero : coeff (C a) 0 = a := coeff_monomial theorem coeff_C_ne_zero (h : n ≠ 0) : (C a).coeff n = 0 := by rw [coeff_C, if_neg h] @[simp] lemma coeff_C_succ {r : R} {n : ℕ} : coeff (C r) (n + 1) = 0 := by simp [coeff_C] @[simp] theorem coeff_natCast_ite : (Nat.cast m : R[X]).coeff n = ite (n = 0) m 0 := by simp only [← C_eq_natCast, coeff_C, Nat.cast_ite, Nat.cast_zero] @[simp] theorem coeff_ofNat_zero (a : ℕ) [a.AtLeastTwo] : coeff (ofNat(a) : R[X]) 0 = ofNat(a) := coeff_monomial @[simp] theorem coeff_ofNat_succ (a n : ℕ) [h : a.AtLeastTwo] : coeff (ofNat(a) : R[X]) (n + 1) = 0 := by rw [← Nat.cast_ofNat] simp [-Nat.cast_ofNat] theorem C_mul_X_pow_eq_monomial : ∀ {n : ℕ}, C a * X ^ n = monomial n a | 0 => mul_one _ | n + 1 => by rw [pow_succ, ← mul_assoc, C_mul_X_pow_eq_monomial, X, monomial_mul_monomial, mul_one] @[simp high] theorem toFinsupp_C_mul_X_pow (a : R) (n : ℕ) : Polynomial.toFinsupp (C a * X ^ n) = Finsupp.single n a := by rw [C_mul_X_pow_eq_monomial, toFinsupp_monomial] theorem C_mul_X_eq_monomial : C a * X = monomial 1 a := by rw [← C_mul_X_pow_eq_monomial, pow_one] @[simp high] theorem toFinsupp_C_mul_X (a : R) : Polynomial.toFinsupp (C a * X) = Finsupp.single 1 a := by rw [C_mul_X_eq_monomial, toFinsupp_monomial] theorem C_injective : Injective (C : R → R[X]) := monomial_injective 0 @[simp] theorem C_inj : C a = C b ↔ a = b := C_injective.eq_iff @[simp] theorem C_eq_zero : C a = 0 ↔ a = 0 := C_injective.eq_iff' (map_zero C) theorem C_ne_zero : C a ≠ 0 ↔ a ≠ 0 := C_eq_zero.not theorem subsingleton_iff_subsingleton : Subsingleton R[X] ↔ Subsingleton R := ⟨@Injective.subsingleton _ _ _ C_injective, by intro infer_instance⟩ theorem Nontrivial.of_polynomial_ne (h : p ≠ q) : Nontrivial R := (subsingleton_or_nontrivial R).resolve_left fun _hI => h <| Subsingleton.elim _ _ theorem forall_eq_iff_forall_eq : (∀ f g : R[X], f = g) ↔ ∀ a b : R, a = b := by simpa only [← subsingleton_iff] using subsingleton_iff_subsingleton theorem ext_iff {p q : R[X]} : p = q ↔ ∀ n, coeff p n = coeff q n := by rcases p with ⟨f : ℕ →₀ R⟩ rcases q with ⟨g : ℕ →₀ R⟩ simpa [coeff] using DFunLike.ext_iff (f := f) (g := g) @[ext] theorem ext {p q : R[X]} : (∀ n, coeff p n = coeff q n) → p = q := ext_iff.2 /-- Monomials generate the additive monoid of polynomials. -/ theorem addSubmonoid_closure_setOf_eq_monomial : AddSubmonoid.closure { p : R[X] | ∃ n a, p = monomial n a } = ⊤ := by apply top_unique rw [← AddSubmonoid.map_equiv_top (toFinsuppIso R).symm.toAddEquiv, ← Finsupp.add_closure_setOf_eq_single, AddMonoidHom.map_mclosure] refine AddSubmonoid.closure_mono (Set.image_subset_iff.2 ?_) rintro _ ⟨n, a, rfl⟩ exact ⟨n, a, Polynomial.ofFinsupp_single _ _⟩ theorem addHom_ext {M : Type*} [AddZeroClass M] {f g : R[X] →+ M} (h : ∀ n a, f (monomial n a) = g (monomial n a)) : f = g := AddMonoidHom.eq_of_eqOn_denseM addSubmonoid_closure_setOf_eq_monomial <| by rintro p ⟨n, a, rfl⟩ exact h n a @[ext high] theorem addHom_ext' {M : Type*} [AddZeroClass M] {f g : R[X] →+ M} (h : ∀ n, f.comp (monomial n).toAddMonoidHom = g.comp (monomial n).toAddMonoidHom) : f = g := addHom_ext fun n => DFunLike.congr_fun (h n) @[ext high] theorem lhom_ext' {M : Type*} [AddCommMonoid M] [Module R M] {f g : R[X] →ₗ[R] M} (h : ∀ n, f.comp (monomial n) = g.comp (monomial n)) : f = g := LinearMap.toAddMonoidHom_injective <| addHom_ext fun n => LinearMap.congr_fun (h n) -- this has the same content as the subsingleton theorem eq_zero_of_eq_zero (h : (0 : R) = (1 : R)) (p : R[X]) : p = 0 := by rw [← one_smul R p, ← h, zero_smul] section Fewnomials theorem support_monomial (n) {a : R} (H : a ≠ 0) : (monomial n a).support = singleton n := by rw [← ofFinsupp_single, support]; exact Finsupp.support_single_ne_zero _ H theorem support_monomial' (n) (a : R) : (monomial n a).support ⊆ singleton n := by rw [← ofFinsupp_single, support] exact Finsupp.support_single_subset theorem support_C {a : R} (h : a ≠ 0) : (C a).support = singleton 0 := support_monomial 0 h theorem support_C_subset (a : R) : (C a).support ⊆ singleton 0 := support_monomial' 0 a theorem support_C_mul_X {c : R} (h : c ≠ 0) : Polynomial.support (C c * X) = singleton 1 := by rw [C_mul_X_eq_monomial, support_monomial 1 h] theorem support_C_mul_X' (c : R) : Polynomial.support (C c * X) ⊆ singleton 1 := by simpa only [C_mul_X_eq_monomial] using support_monomial' 1 c theorem support_C_mul_X_pow (n : ℕ) {c : R} (h : c ≠ 0) : Polynomial.support (C c * X ^ n) = singleton n := by rw [C_mul_X_pow_eq_monomial, support_monomial n h] theorem support_C_mul_X_pow' (n : ℕ) (c : R) : Polynomial.support (C c * X ^ n) ⊆ singleton n := by simpa only [C_mul_X_pow_eq_monomial] using support_monomial' n c open Finset theorem support_binomial' (k m : ℕ) (x y : R) : Polynomial.support (C x * X ^ k + C y * X ^ m) ⊆ {k, m} := support_add.trans (union_subset ((support_C_mul_X_pow' k x).trans (singleton_subset_iff.mpr (mem_insert_self k {m}))) ((support_C_mul_X_pow' m y).trans (singleton_subset_iff.mpr (mem_insert_of_mem (mem_singleton_self m))))) theorem support_trinomial' (k m n : ℕ) (x y z : R) : Polynomial.support (C x * X ^ k + C y * X ^ m + C z * X ^ n) ⊆ {k, m, n} := support_add.trans (union_subset (support_add.trans (union_subset ((support_C_mul_X_pow' k x).trans (singleton_subset_iff.mpr (mem_insert_self k {m, n}))) ((support_C_mul_X_pow' m y).trans (singleton_subset_iff.mpr (mem_insert_of_mem (mem_insert_self m {n})))))) ((support_C_mul_X_pow' n z).trans (singleton_subset_iff.mpr (mem_insert_of_mem (mem_insert_of_mem (mem_singleton_self n)))))) end Fewnomials theorem X_pow_eq_monomial (n) : X ^ n = monomial n (1 : R) := by induction n with | zero => rw [pow_zero, monomial_zero_one] | succ n hn => rw [pow_succ, hn, X, monomial_mul_monomial, one_mul] @[simp high] theorem toFinsupp_X_pow (n : ℕ) : (X ^ n).toFinsupp = Finsupp.single n (1 : R) := by rw [X_pow_eq_monomial, toFinsupp_monomial] theorem smul_X_eq_monomial {n} : a • X ^ n = monomial n (a : R) := by rw [X_pow_eq_monomial, smul_monomial, smul_eq_mul, mul_one] theorem support_X_pow (H : ¬(1 : R) = 0) (n : ℕ) : (X ^ n : R[X]).support = singleton n := by convert support_monomial n H exact X_pow_eq_monomial n theorem support_X_empty (H : (1 : R) = 0) : (X : R[X]).support = ∅ := by rw [X, H, monomial_zero_right, support_zero] theorem support_X (H : ¬(1 : R) = 0) : (X : R[X]).support = singleton 1 := by rw [← pow_one X, support_X_pow H 1] theorem monomial_left_inj {a : R} (ha : a ≠ 0) {i j : ℕ} : monomial i a = monomial j a ↔ i = j := by simp only [← ofFinsupp_single, ofFinsupp.injEq, Finsupp.single_left_inj ha] theorem binomial_eq_binomial {k l m n : ℕ} {u v : R} (hu : u ≠ 0) (hv : v ≠ 0) : C u * X ^ k + C v * X ^ l = C u * X ^ m + C v * X ^ n ↔ k = m ∧ l = n ∨ u = v ∧ k = n ∧ l = m ∨ u + v = 0 ∧ k = l ∧ m = n := by simp_rw [C_mul_X_pow_eq_monomial, ← toFinsupp_inj, toFinsupp_add, toFinsupp_monomial] exact Finsupp.single_add_single_eq_single_add_single hu hv theorem natCast_mul (n : ℕ) (p : R[X]) : (n : R[X]) * p = n • p := (nsmul_eq_mul _ _).symm /-- Summing the values of a function applied to the coefficients of a polynomial -/ def sum {S : Type*} [AddCommMonoid S] (p : R[X]) (f : ℕ → R → S) : S := ∑ n ∈ p.support, f n (p.coeff n) theorem sum_def {S : Type*} [AddCommMonoid S] (p : R[X]) (f : ℕ → R → S) : p.sum f = ∑ n ∈ p.support, f n (p.coeff n) := rfl theorem sum_eq_of_subset {S : Type*} [AddCommMonoid S] {p : R[X]} (f : ℕ → R → S) (hf : ∀ i, f i 0 = 0) {s : Finset ℕ} (hs : p.support ⊆ s) : p.sum f = ∑ n ∈ s, f n (p.coeff n) := Finsupp.sum_of_support_subset _ hs f (fun i _ ↦ hf i) /-- Expressing the product of two polynomials as a double sum. -/ theorem mul_eq_sum_sum : p * q = ∑ i ∈ p.support, q.sum fun j a => (monomial (i + j)) (p.coeff i * a) := by apply toFinsupp_injective rcases p with ⟨⟩; rcases q with ⟨⟩ simp_rw [sum, coeff, toFinsupp_sum, support, toFinsupp_mul, toFinsupp_monomial, AddMonoidAlgebra.mul_def, Finsupp.sum] @[simp] theorem sum_zero_index {S : Type*} [AddCommMonoid S] (f : ℕ → R → S) : (0 : R[X]).sum f = 0 := by simp [sum] @[simp] theorem sum_monomial_index {S : Type*} [AddCommMonoid S] {n : ℕ} (a : R) (f : ℕ → R → S) (hf : f n 0 = 0) : (monomial n a : R[X]).sum f = f n a := Finsupp.sum_single_index hf @[simp] theorem sum_C_index {a} {β} [AddCommMonoid β] {f : ℕ → R → β} (h : f 0 0 = 0) : (C a).sum f = f 0 a := sum_monomial_index a f h -- the assumption `hf` is only necessary when the ring is trivial @[simp] theorem sum_X_index {S : Type*} [AddCommMonoid S] {f : ℕ → R → S} (hf : f 1 0 = 0) : (X : R[X]).sum f = f 1 1 := sum_monomial_index 1 f hf theorem sum_add_index {S : Type*} [AddCommMonoid S] (p q : R[X]) (f : ℕ → R → S) (hf : ∀ i, f i 0 = 0) (h_add : ∀ a b₁ b₂, f a (b₁ + b₂) = f a b₁ + f a b₂) : (p + q).sum f = p.sum f + q.sum f := by rw [show p + q = ⟨p.toFinsupp + q.toFinsupp⟩ from add_def p q] exact Finsupp.sum_add_index (fun i _ ↦ hf i) (fun a _ b₁ b₂ ↦ h_add a b₁ b₂) theorem sum_add' {S : Type*} [AddCommMonoid S] (p : R[X]) (f g : ℕ → R → S) : p.sum (f + g) = p.sum f + p.sum g := by simp [sum_def, Finset.sum_add_distrib] theorem sum_add {S : Type*} [AddCommMonoid S] (p : R[X]) (f g : ℕ → R → S) : (p.sum fun n x => f n x + g n x) = p.sum f + p.sum g := sum_add' _ _ _ theorem sum_smul_index {S : Type*} [AddCommMonoid S] (p : R[X]) (b : R) (f : ℕ → R → S) (hf : ∀ i, f i 0 = 0) : (b • p).sum f = p.sum fun n a => f n (b * a) := Finsupp.sum_smul_index hf theorem sum_smul_index' {S T : Type*} [DistribSMul T R] [AddCommMonoid S] (p : R[X]) (b : T) (f : ℕ → R → S) (hf : ∀ i, f i 0 = 0) : (b • p).sum f = p.sum fun n a => f n (b • a) := Finsupp.sum_smul_index' hf protected theorem smul_sum {S T : Type*} [AddCommMonoid S] [DistribSMul T S] (p : R[X]) (b : T) (f : ℕ → R → S) : b • p.sum f = p.sum fun n a => b • f n a := Finsupp.smul_sum @[simp] theorem sum_monomial_eq : ∀ p : R[X], (p.sum fun n a => monomial n a) = p | ⟨_p⟩ => (ofFinsupp_sum _ _).symm.trans (congr_arg _ <| Finsupp.sum_single _) theorem sum_C_mul_X_pow_eq (p : R[X]) : (p.sum fun n a => C a * X ^ n) = p := by simp_rw [C_mul_X_pow_eq_monomial, sum_monomial_eq] @[elab_as_elim] protected theorem induction_on {motive : R[X] → Prop} (p : R[X]) (C : ∀ a, motive (C a)) (add : ∀ p q, motive p → motive q → motive (p + q)) (monomial : ∀ (n : ℕ) (a : R), motive (Polynomial.C a * X ^ n) → motive (Polynomial.C a * X ^ (n + 1))) : motive p := by have A : ∀ {n : ℕ} {a}, motive (Polynomial.C a * X ^ n) := by intro n a induction n with | zero => rw [pow_zero, mul_one]; exact C a | succ n ih => exact monomial _ _ ih have B : ∀ s : Finset ℕ, motive (s.sum fun n : ℕ => Polynomial.C (p.coeff n) * X ^ n) := by apply Finset.induction · convert C 0 exact C_0.symm · intro n s ns ih rw [sum_insert ns] exact add _ _ A ih rw [← sum_C_mul_X_pow_eq p, Polynomial.sum] exact B (support p) /-- To prove something about polynomials, it suffices to show the condition is closed under taking sums, and it holds for monomials. -/ @[elab_as_elim] protected theorem induction_on' {motive : R[X] → Prop} (p : R[X]) (add : ∀ p q, motive p → motive q → motive (p + q)) (monomial : ∀ (n : ℕ) (a : R), motive (monomial n a)) : motive p := Polynomial.induction_on p (monomial 0) add fun n a _h => by rw [C_mul_X_pow_eq_monomial]; exact monomial _ _ /-- `erase p n` is the polynomial `p` in which the `X^n` term has been erased. -/ irreducible_def erase (n : ℕ) : R[X] → R[X] | ⟨p⟩ => ⟨p.erase n⟩ @[simp] theorem toFinsupp_erase (p : R[X]) (n : ℕ) : toFinsupp (p.erase n) = p.toFinsupp.erase n := by rcases p with ⟨⟩ simp only [erase_def] @[simp] theorem ofFinsupp_erase (p : R[ℕ]) (n : ℕ) : (⟨p.erase n⟩ : R[X]) = (⟨p⟩ : R[X]).erase n := by rcases p with ⟨⟩ simp only [erase_def] @[simp] theorem support_erase (p : R[X]) (n : ℕ) : support (p.erase n) = (support p).erase n := by rcases p with ⟨⟩ simp only [support, erase_def, Finsupp.support_erase] theorem monomial_add_erase (p : R[X]) (n : ℕ) : monomial n (coeff p n) + p.erase n = p := toFinsupp_injective <| by rcases p with ⟨⟩ rw [toFinsupp_add, toFinsupp_monomial, toFinsupp_erase, coeff] exact Finsupp.single_add_erase _ _ theorem coeff_erase (p : R[X]) (n i : ℕ) : (p.erase n).coeff i = if i = n then 0 else p.coeff i := by rcases p with ⟨⟩ simp only [erase_def, coeff] exact ite_congr rfl (fun _ => rfl) (fun _ => rfl) @[simp] theorem erase_zero (n : ℕ) : (0 : R[X]).erase n = 0 := toFinsupp_injective <| by simp @[simp] theorem erase_monomial {n : ℕ} {a : R} : erase n (monomial n a) = 0 := toFinsupp_injective <| by simp @[simp] theorem erase_same (p : R[X]) (n : ℕ) : coeff (p.erase n) n = 0 := by simp [coeff_erase] @[simp] theorem erase_ne (p : R[X]) (n i : ℕ) (h : i ≠ n) : coeff (p.erase n) i = coeff p i := by simp [coeff_erase, h] section Update /-- Replace the coefficient of a `p : R[X]` at a given degree `n : ℕ` by a given value `a : R`. If `a = 0`, this is equal to `p.erase n` If `p.natDegree < n` and `a ≠ 0`, this increases the degree to `n`. -/ def update (p : R[X]) (n : ℕ) (a : R) : R[X] := Polynomial.ofFinsupp (p.toFinsupp.update n a) theorem coeff_update (p : R[X]) (n : ℕ) (a : R) : (p.update n a).coeff = Function.update p.coeff n a := by ext cases p simp only [coeff, update, Function.update_apply, coe_update] theorem coeff_update_apply (p : R[X]) (n : ℕ) (a : R) (i : ℕ) : (p.update n a).coeff i = if i = n then a else p.coeff i := by rw [coeff_update, Function.update_apply] @[simp] theorem coeff_update_same (p : R[X]) (n : ℕ) (a : R) : (p.update n a).coeff n = a := by rw [p.coeff_update_apply, if_pos rfl] theorem coeff_update_ne (p : R[X]) {n : ℕ} (a : R) {i : ℕ} (h : i ≠ n) : (p.update n a).coeff i = p.coeff i := by rw [p.coeff_update_apply, if_neg h] @[simp] theorem update_zero_eq_erase (p : R[X]) (n : ℕ) : p.update n 0 = p.erase n := by ext rw [coeff_update_apply, coeff_erase] theorem support_update (p : R[X]) (n : ℕ) (a : R) [Decidable (a = 0)] : support (p.update n a) = if a = 0 then p.support.erase n else insert n p.support := by classical cases p simp only [support, update, Finsupp.support_update] congr theorem support_update_zero (p : R[X]) (n : ℕ) : support (p.update n 0) = p.support.erase n := by rw [update_zero_eq_erase, support_erase] theorem support_update_ne_zero (p : R[X]) (n : ℕ) {a : R} (ha : a ≠ 0) : support (p.update n a) = insert n p.support := by classical rw [support_update, if_neg ha] end Update /-- The finset of nonzero coefficients of a polynomial. -/ def coeffs (p : R[X]) : Finset R := letI := Classical.decEq R Finset.image (fun n => p.coeff n) p.support @[simp] theorem coeffs_zero : coeffs (0 : R[X]) = ∅ := rfl theorem mem_coeffs_iff {p : R[X]} {c : R} : c ∈ p.coeffs ↔ ∃ n ∈ p.support, c = p.coeff n := by simp [coeffs, eq_comm, (Finset.mem_image)] theorem coeffs_one : coeffs (1 : R[X]) ⊆ {1} := by classical simp_rw [coeffs, Finset.image_subset_iff] simp_all [coeff_one] theorem coeff_mem_coeffs (p : R[X]) (n : ℕ) (h : p.coeff n ≠ 0) : p.coeff n ∈ p.coeffs := by classical simp only [coeffs, exists_prop, mem_support_iff, Finset.mem_image, Ne] exact ⟨n, h, rfl⟩ theorem coeffs_monomial (n : ℕ) {c : R} (hc : c ≠ 0) : (monomial n c).coeffs = {c} := by rw [coeffs, support_monomial n hc] simp end Semiring section CommSemiring variable [CommSemiring R] instance commSemiring : CommSemiring R[X] := fast_instance% { Function.Injective.commSemigroup toFinsupp toFinsupp_injective toFinsupp_mul with toSemiring := Polynomial.semiring } end CommSemiring section Ring variable [Ring R] instance instZSMul : SMul ℤ R[X] where smul r p := ⟨r • p.toFinsupp⟩ @[simp] theorem ofFinsupp_zsmul (a : ℤ) (b) : (⟨a • b⟩ : R[X]) = (a • ⟨b⟩ : R[X]) := rfl @[simp] theorem toFinsupp_zsmul (a : ℤ) (b : R[X]) : (a • b).toFinsupp = a • b.toFinsupp := rfl instance instIntCast : IntCast R[X] where intCast n := ofFinsupp n @[simp] theorem ofFinsupp_intCast (z : ℤ) : (⟨z⟩ : R[X]) = z := rfl @[simp] theorem toFinsupp_intCast (z : ℤ) : (z : R[X]).toFinsupp = z := rfl instance ring : Ring R[X] := fast_instance% Function.Injective.ring toFinsupp toFinsupp_injective (toFinsupp_zero (R := R)) toFinsupp_one toFinsupp_add toFinsupp_mul toFinsupp_neg toFinsupp_sub (fun _ _ => toFinsupp_nsmul _ _) (fun _ _ => toFinsupp_zsmul _ _) toFinsupp_pow (fun _ => rfl) fun _ => rfl @[simp] theorem coeff_neg (p : R[X]) (n : ℕ) : coeff (-p) n = -coeff p n := by rcases p with ⟨⟩ rw [← ofFinsupp_neg, coeff, coeff, Finsupp.neg_apply] @[simp] theorem coeff_sub (p q : R[X]) (n : ℕ) : coeff (p - q) n = coeff p n - coeff q n := by rcases p with ⟨⟩ rcases q with ⟨⟩ rw [← ofFinsupp_sub, coeff, coeff, coeff, Finsupp.sub_apply] @[simp] theorem monomial_neg (n : ℕ) (a : R) : monomial n (-a) = -monomial n a := by rw [eq_neg_iff_add_eq_zero, ← monomial_add, neg_add_cancel, monomial_zero_right] theorem monomial_sub (n : ℕ) : monomial n (a - b) = monomial n a - monomial n b := by rw [sub_eq_add_neg, monomial_add, monomial_neg, sub_eq_add_neg] @[simp] theorem support_neg {p : R[X]} : (-p).support = p.support := by rcases p with ⟨⟩ rw [← ofFinsupp_neg, support, support, Finsupp.support_neg] theorem C_eq_intCast (n : ℤ) : C (n : R) = n := by simp theorem C_neg : C (-a) = -C a := RingHom.map_neg C a theorem C_sub : C (a - b) = C a - C b := RingHom.map_sub C a b end Ring
instance commRing [CommRing R] : CommRing R[X] := --TODO: add reference to library note in PR https://github.com/leanprover-community/mathlib4/pull/7432
Mathlib/Algebra/Polynomial/Basic.lean
1,133
1,134
/- Copyright (c) 2015, 2017 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Robert Y. Lewis, Johannes Hölzl, Mario Carneiro, Sébastien Gouëzel -/ import Mathlib.Topology.Bornology.Constructions import Mathlib.Topology.MetricSpace.Pseudo.Defs import Mathlib.Topology.UniformSpace.UniformEmbedding /-! # Products of pseudometric spaces and other constructions This file constructs the supremum distance on binary products of pseudometric spaces and provides instances for type synonyms. -/ open Bornology Filter Metric Set Topology open scoped NNReal variable {α β : Type*} [PseudoMetricSpace α] /-- Pseudometric space structure pulled back by a function. -/ abbrev PseudoMetricSpace.induced {α β} (f : α → β) (m : PseudoMetricSpace β) : PseudoMetricSpace α where dist x y := dist (f x) (f y) dist_self _ := dist_self _ dist_comm _ _ := dist_comm _ _ dist_triangle _ _ _ := dist_triangle _ _ _ edist x y := edist (f x) (f y) edist_dist _ _ := edist_dist _ _ toUniformSpace := UniformSpace.comap f m.toUniformSpace uniformity_dist := (uniformity_basis_dist.comap _).eq_biInf toBornology := Bornology.induced f cobounded_sets := Set.ext fun s => mem_comap_iff_compl.trans <| by simp only [← isBounded_def, isBounded_iff, forall_mem_image, mem_setOf] /-- Pull back a pseudometric space structure by an inducing map. This is a version of `PseudoMetricSpace.induced` useful in case if the domain already has a `TopologicalSpace` structure. -/ def Topology.IsInducing.comapPseudoMetricSpace {α β : Type*} [TopologicalSpace α] [m : PseudoMetricSpace β] {f : α → β} (hf : IsInducing f) : PseudoMetricSpace α := .replaceTopology (.induced f m) hf.eq_induced @[deprecated (since := "2024-10-28")] alias Inducing.comapPseudoMetricSpace := IsInducing.comapPseudoMetricSpace /-- Pull back a pseudometric space structure by a uniform inducing map. This is a version of `PseudoMetricSpace.induced` useful in case if the domain already has a `UniformSpace` structure. -/ def IsUniformInducing.comapPseudoMetricSpace {α β} [UniformSpace α] [m : PseudoMetricSpace β] (f : α → β) (h : IsUniformInducing f) : PseudoMetricSpace α := .replaceUniformity (.induced f m) h.comap_uniformity.symm instance Subtype.pseudoMetricSpace {p : α → Prop} : PseudoMetricSpace (Subtype p) := PseudoMetricSpace.induced Subtype.val ‹_› lemma Subtype.dist_eq {p : α → Prop} (x y : Subtype p) : dist x y = dist (x : α) y := rfl lemma Subtype.nndist_eq {p : α → Prop} (x y : Subtype p) : nndist x y = nndist (x : α) y := rfl namespace MulOpposite @[to_additive] instance instPseudoMetricSpace : PseudoMetricSpace αᵐᵒᵖ := PseudoMetricSpace.induced MulOpposite.unop ‹_› @[to_additive (attr := simp)] lemma dist_unop (x y : αᵐᵒᵖ) : dist (unop x) (unop y) = dist x y := rfl @[to_additive (attr := simp)] lemma dist_op (x y : α) : dist (op x) (op y) = dist x y := rfl @[to_additive (attr := simp)] lemma nndist_unop (x y : αᵐᵒᵖ) : nndist (unop x) (unop y) = nndist x y := rfl @[to_additive (attr := simp)] lemma nndist_op (x y : α) : nndist (op x) (op y) = nndist x y := rfl end MulOpposite section NNReal instance : PseudoMetricSpace ℝ≥0 := Subtype.pseudoMetricSpace lemma NNReal.dist_eq (a b : ℝ≥0) : dist a b = |(a : ℝ) - b| := rfl lemma NNReal.nndist_eq (a b : ℝ≥0) : nndist a b = max (a - b) (b - a) := eq_of_forall_ge_iff fun _ => by simp only [max_le_iff, tsub_le_iff_right (α := ℝ≥0)] simp only [← NNReal.coe_le_coe, coe_nndist, dist_eq, abs_sub_le_iff, tsub_le_iff_right, NNReal.coe_add] @[simp] lemma NNReal.nndist_zero_eq_val (z : ℝ≥0) : nndist 0 z = z := by simp only [NNReal.nndist_eq, max_eq_right, tsub_zero, zero_tsub, zero_le'] @[simp] lemma NNReal.nndist_zero_eq_val' (z : ℝ≥0) : nndist z 0 = z := by rw [nndist_comm] exact NNReal.nndist_zero_eq_val z lemma NNReal.le_add_nndist (a b : ℝ≥0) : a ≤ b + nndist a b := by suffices (a : ℝ) ≤ (b : ℝ) + dist a b by rwa [← NNReal.coe_le_coe, NNReal.coe_add, coe_nndist] rw [← sub_le_iff_le_add'] exact le_of_abs_le (dist_eq a b).ge lemma NNReal.ball_zero_eq_Ico' (c : ℝ≥0) : Metric.ball (0 : ℝ≥0) c.toReal = Set.Ico 0 c := by ext x; simp lemma NNReal.ball_zero_eq_Ico (c : ℝ) : Metric.ball (0 : ℝ≥0) c = Set.Ico 0 c.toNNReal := by by_cases c_pos : 0 < c · convert NNReal.ball_zero_eq_Ico' ⟨c, c_pos.le⟩ simp [Real.toNNReal, c_pos.le] simp [not_lt.mp c_pos] lemma NNReal.closedBall_zero_eq_Icc' (c : ℝ≥0) : Metric.closedBall (0 : ℝ≥0) c.toReal = Set.Icc 0 c := by ext x; simp lemma NNReal.closedBall_zero_eq_Icc {c : ℝ} (c_nn : 0 ≤ c) : Metric.closedBall (0 : ℝ≥0) c = Set.Icc 0 c.toNNReal := by convert NNReal.closedBall_zero_eq_Icc' ⟨c, c_nn⟩ simp [Real.toNNReal, c_nn] end NNReal namespace ULift variable [PseudoMetricSpace β] instance : PseudoMetricSpace (ULift β) := PseudoMetricSpace.induced ULift.down ‹_› lemma dist_eq (x y : ULift β) : dist x y = dist x.down y.down := rfl lemma nndist_eq (x y : ULift β) : nndist x y = nndist x.down y.down := rfl @[simp] lemma dist_up_up (x y : β) : dist (ULift.up x) (ULift.up y) = dist x y := rfl @[simp] lemma nndist_up_up (x y : β) : nndist (ULift.up x) (ULift.up y) = nndist x y := rfl end ULift section Prod variable [PseudoMetricSpace β] instance Prod.pseudoMetricSpaceMax : PseudoMetricSpace (α × β) := let i := PseudoEMetricSpace.toPseudoMetricSpaceOfDist (fun x y : α × β => dist x.1 y.1 ⊔ dist x.2 y.2) (fun _ _ => (max_lt (edist_lt_top _ _) (edist_lt_top _ _)).ne) fun x y => by simp only [dist_edist, ← ENNReal.toReal_max (edist_ne_top _ _) (edist_ne_top _ _), Prod.edist_eq] i.replaceBornology fun s => by simp only [← isBounded_image_fst_and_snd, isBounded_iff_eventually, forall_mem_image, ← eventually_and, ← forall_and, ← max_le_iff] rfl lemma Prod.dist_eq {x y : α × β} : dist x y = max (dist x.1 y.1) (dist x.2 y.2) := rfl @[simp] lemma dist_prod_same_left {x : α} {y₁ y₂ : β} : dist (x, y₁) (x, y₂) = dist y₁ y₂ := by simp [Prod.dist_eq, dist_nonneg] @[simp] lemma dist_prod_same_right {x₁ x₂ : α} {y : β} : dist (x₁, y) (x₂, y) = dist x₁ x₂ := by simp [Prod.dist_eq, dist_nonneg] lemma ball_prod_same (x : α) (y : β) (r : ℝ) : ball x r ×ˢ ball y r = ball (x, y) r := ext fun z => by simp [Prod.dist_eq] lemma closedBall_prod_same (x : α) (y : β) (r : ℝ) : closedBall x r ×ˢ closedBall y r = closedBall (x, y) r := ext fun z => by simp [Prod.dist_eq] lemma sphere_prod (x : α × β) (r : ℝ) : sphere x r = sphere x.1 r ×ˢ closedBall x.2 r ∪ closedBall x.1 r ×ˢ sphere x.2 r := by obtain hr | rfl | hr := lt_trichotomy r 0 · simp [hr] · cases x simp_rw [← closedBall_eq_sphere_of_nonpos le_rfl, union_self, closedBall_prod_same] · ext ⟨x', y'⟩ simp_rw [Set.mem_union, Set.mem_prod, Metric.mem_closedBall, Metric.mem_sphere, Prod.dist_eq, max_eq_iff] refine or_congr (and_congr_right ?_) (and_comm.trans (and_congr_left ?_)) all_goals rintro rfl; rfl end Prod lemma uniformContinuous_dist : UniformContinuous fun p : α × α => dist p.1 p.2 := Metric.uniformContinuous_iff.2 fun ε ε0 => ⟨ε / 2, half_pos ε0, fun {a b} h => calc dist (dist a.1 a.2) (dist b.1 b.2) ≤ dist a.1 b.1 + dist a.2 b.2 := dist_dist_dist_le _ _ _ _ _ ≤ dist a b + dist a b := add_le_add (le_max_left _ _) (le_max_right _ _) _ < ε / 2 + ε / 2 := add_lt_add h h _ = ε := add_halves ε⟩ protected lemma UniformContinuous.dist [UniformSpace β] {f g : β → α} (hf : UniformContinuous f) (hg : UniformContinuous g) : UniformContinuous fun b => dist (f b) (g b) := uniformContinuous_dist.comp (hf.prodMk hg) @[continuity] lemma continuous_dist : Continuous fun p : α × α ↦ dist p.1 p.2 := uniformContinuous_dist.continuous @[continuity, fun_prop] protected lemma Continuous.dist [TopologicalSpace β] {f g : β → α} (hf : Continuous f) (hg : Continuous g) : Continuous fun b => dist (f b) (g b) := continuous_dist.comp₂ hf hg protected lemma Filter.Tendsto.dist {f g : β → α} {x : Filter β} {a b : α} (hf : Tendsto f x (𝓝 a)) (hg : Tendsto g x (𝓝 b)) : Tendsto (fun x => dist (f x) (g x)) x (𝓝 (dist a b)) := (continuous_dist.tendsto (a, b)).comp (hf.prodMk_nhds hg) lemma continuous_iff_continuous_dist [TopologicalSpace β] {f : β → α} : Continuous f ↔ Continuous fun x : β × β => dist (f x.1) (f x.2) := ⟨fun h => h.fst'.dist h.snd', fun h => continuous_iff_continuousAt.2 fun _ => tendsto_iff_dist_tendsto_zero.2 <| (h.comp (.prodMk_left _)).tendsto' _ _ <| dist_self _⟩ lemma uniformContinuous_nndist : UniformContinuous fun p : α × α => nndist p.1 p.2 := uniformContinuous_dist.subtype_mk _ protected lemma UniformContinuous.nndist [UniformSpace β] {f g : β → α} (hf : UniformContinuous f) (hg : UniformContinuous g) : UniformContinuous fun b => nndist (f b) (g b) := uniformContinuous_nndist.comp (hf.prodMk hg) lemma continuous_nndist : Continuous fun p : α × α => nndist p.1 p.2 := uniformContinuous_nndist.continuous @[fun_prop] protected lemma Continuous.nndist [TopologicalSpace β] {f g : β → α} (hf : Continuous f) (hg : Continuous g) : Continuous fun b => nndist (f b) (g b) := continuous_nndist.comp₂ hf hg protected lemma Filter.Tendsto.nndist {f g : β → α} {x : Filter β} {a b : α} (hf : Tendsto f x (𝓝 a)) (hg : Tendsto g x (𝓝 b)) : Tendsto (fun x => nndist (f x) (g x)) x (𝓝 (nndist a b)) := (continuous_nndist.tendsto (a, b)).comp (hf.prodMk_nhds hg)
Mathlib/Topology/MetricSpace/Pseudo/Constructions.lean
362
364
/- Copyright (c) 2021 Hunter Monroe. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Hunter Monroe, Kyle Miller, Alena Gusakov -/ import Mathlib.Combinatorics.SimpleGraph.DeleteEdges import Mathlib.Data.Fintype.Powerset /-! # Subgraphs of a simple graph A subgraph of a simple graph consists of subsets of the graph's vertices and edges such that the endpoints of each edge are present in the vertex subset. The edge subset is formalized as a sub-relation of the adjacency relation of the simple graph. ## Main definitions * `Subgraph G` is the type of subgraphs of a `G : SimpleGraph V`. * `Subgraph.neighborSet`, `Subgraph.incidenceSet`, and `Subgraph.degree` are like their `SimpleGraph` counterparts, but they refer to vertices from `G` to avoid subtype coercions. * `Subgraph.coe` is the coercion from a `G' : Subgraph G` to a `SimpleGraph G'.verts`. (In Lean 3 this could not be a `Coe` instance since the destination type depends on `G'`.) * `Subgraph.IsSpanning` for whether a subgraph is a spanning subgraph and `Subgraph.IsInduced` for whether a subgraph is an induced subgraph. * Instances for `Lattice (Subgraph G)` and `BoundedOrder (Subgraph G)`. * `SimpleGraph.toSubgraph`: If a `SimpleGraph` is a subgraph of another, then you can turn it into a member of the larger graph's `SimpleGraph.Subgraph` type. * Graph homomorphisms from a subgraph to a graph (`Subgraph.map_top`) and between subgraphs (`Subgraph.map`). ## Implementation notes * Recall that subgraphs are not determined by their vertex sets, so `SetLike` does not apply to this kind of subobject. ## TODO * Images of graph homomorphisms as subgraphs. -/ universe u v namespace SimpleGraph /-- A subgraph of a `SimpleGraph` is a subset of vertices along with a restriction of the adjacency relation that is symmetric and is supported by the vertex subset. They also form a bounded lattice. Thinking of `V → V → Prop` as `Set (V × V)`, a set of darts (i.e., half-edges), then `Subgraph.adj_sub` is that the darts of a subgraph are a subset of the darts of `G`. -/ @[ext] structure Subgraph {V : Type u} (G : SimpleGraph V) where /-- Vertices of the subgraph -/ verts : Set V /-- Edges of the subgraph -/ Adj : V → V → Prop adj_sub : ∀ {v w : V}, Adj v w → G.Adj v w edge_vert : ∀ {v w : V}, Adj v w → v ∈ verts symm : Symmetric Adj := by aesop_graph -- Porting note: Originally `by obviously` initialize_simps_projections SimpleGraph.Subgraph (Adj → adj) variable {ι : Sort*} {V : Type u} {W : Type v} /-- The one-vertex subgraph. -/ @[simps] protected def singletonSubgraph (G : SimpleGraph V) (v : V) : G.Subgraph where verts := {v} Adj := ⊥ adj_sub := False.elim edge_vert := False.elim symm _ _ := False.elim /-- The one-edge subgraph. -/ @[simps] def subgraphOfAdj (G : SimpleGraph V) {v w : V} (hvw : G.Adj v w) : G.Subgraph where verts := {v, w} Adj a b := s(v, w) = s(a, b) adj_sub h := by rw [← G.mem_edgeSet, ← h] exact hvw edge_vert {a b} h := by apply_fun fun e ↦ a ∈ e at h simp only [Sym2.mem_iff, true_or, eq_iff_iff, iff_true] at h exact h namespace Subgraph variable {G : SimpleGraph V} {G₁ G₂ : G.Subgraph} {a b : V} protected theorem loopless (G' : Subgraph G) : Irreflexive G'.Adj := fun v h ↦ G.loopless v (G'.adj_sub h) theorem adj_comm (G' : Subgraph G) (v w : V) : G'.Adj v w ↔ G'.Adj w v := ⟨fun x ↦ G'.symm x, fun x ↦ G'.symm x⟩ @[symm] theorem adj_symm (G' : Subgraph G) {u v : V} (h : G'.Adj u v) : G'.Adj v u := G'.symm h protected theorem Adj.symm {G' : Subgraph G} {u v : V} (h : G'.Adj u v) : G'.Adj v u := G'.symm h protected theorem Adj.adj_sub {H : G.Subgraph} {u v : V} (h : H.Adj u v) : G.Adj u v := H.adj_sub h protected theorem Adj.fst_mem {H : G.Subgraph} {u v : V} (h : H.Adj u v) : u ∈ H.verts := H.edge_vert h protected theorem Adj.snd_mem {H : G.Subgraph} {u v : V} (h : H.Adj u v) : v ∈ H.verts := h.symm.fst_mem protected theorem Adj.ne {H : G.Subgraph} {u v : V} (h : H.Adj u v) : u ≠ v := h.adj_sub.ne theorem adj_congr_of_sym2 {H : G.Subgraph} {u v w x : V} (h2 : s(u, v) = s(w, x)) : H.Adj u v ↔ H.Adj w x := by simp only [Sym2.eq, Sym2.rel_iff', Prod.mk.injEq, Prod.swap_prod_mk] at h2 rcases h2 with hl | hr · rw [hl.1, hl.2] · rw [hr.1, hr.2, Subgraph.adj_comm] /-- Coercion from `G' : Subgraph G` to a `SimpleGraph G'.verts`. -/ @[simps] protected def coe (G' : Subgraph G) : SimpleGraph G'.verts where Adj v w := G'.Adj v w symm _ _ h := G'.symm h loopless v h := loopless G v (G'.adj_sub h) @[simp] theorem coe_adj_sub (G' : Subgraph G) (u v : G'.verts) (h : G'.coe.Adj u v) : G.Adj u v := G'.adj_sub h -- Given `h : H.Adj u v`, then `h.coe : H.coe.Adj ⟨u, _⟩ ⟨v, _⟩`. protected theorem Adj.coe {H : G.Subgraph} {u v : V} (h : H.Adj u v) : H.coe.Adj ⟨u, H.edge_vert h⟩ ⟨v, H.edge_vert h.symm⟩ := h instance (G : SimpleGraph V) (H : Subgraph G) [DecidableRel H.Adj] : DecidableRel H.coe.Adj := fun a b ↦ ‹DecidableRel H.Adj› _ _ /-- A subgraph is called a *spanning subgraph* if it contains all the vertices of `G`. -/ def IsSpanning (G' : Subgraph G) : Prop := ∀ v : V, v ∈ G'.verts theorem isSpanning_iff {G' : Subgraph G} : G'.IsSpanning ↔ G'.verts = Set.univ := Set.eq_univ_iff_forall.symm protected alias ⟨IsSpanning.verts_eq_univ, _⟩ := isSpanning_iff /-- Coercion from `Subgraph G` to `SimpleGraph V`. If `G'` is a spanning subgraph, then `G'.spanningCoe` yields an isomorphic graph. In general, this adds in all vertices from `V` as isolated vertices. -/ @[simps] protected def spanningCoe (G' : Subgraph G) : SimpleGraph V where Adj := G'.Adj symm := G'.symm loopless v hv := G.loopless v (G'.adj_sub hv) @[simp] theorem Adj.of_spanningCoe {G' : Subgraph G} {u v : G'.verts} (h : G'.spanningCoe.Adj u v) : G.Adj u v := G'.adj_sub h lemma spanningCoe_le (G' : G.Subgraph) : G'.spanningCoe ≤ G := fun _ _ ↦ G'.3 theorem spanningCoe_inj : G₁.spanningCoe = G₂.spanningCoe ↔ G₁.Adj = G₂.Adj := by simp [Subgraph.spanningCoe] lemma mem_of_adj_spanningCoe {v w : V} {s : Set V} (G : SimpleGraph s) (hadj : G.spanningCoe.Adj v w) : v ∈ s := by aesop @[simp] lemma spanningCoe_subgraphOfAdj {v w : V} (hadj : G.Adj v w) : (G.subgraphOfAdj hadj).spanningCoe = fromEdgeSet {s(v, w)} := by ext v w aesop /-- `spanningCoe` is equivalent to `coe` for a subgraph that `IsSpanning`. -/ @[simps] def spanningCoeEquivCoeOfSpanning (G' : Subgraph G) (h : G'.IsSpanning) : G'.spanningCoe ≃g G'.coe where toFun v := ⟨v, h v⟩ invFun v := v left_inv _ := rfl right_inv _ := rfl map_rel_iff' := Iff.rfl /-- A subgraph is called an *induced subgraph* if vertices of `G'` are adjacent if they are adjacent in `G`. -/ def IsInduced (G' : Subgraph G) : Prop := ∀ ⦃v⦄, v ∈ G'.verts → ∀ ⦃w⦄, w ∈ G'.verts → G.Adj v w → G'.Adj v w @[simp] protected lemma IsInduced.adj {G' : G.Subgraph} (hG' : G'.IsInduced) {a b : G'.verts} : G'.Adj a b ↔ G.Adj a b := ⟨coe_adj_sub _ _ _, hG' a.2 b.2⟩ /-- `H.support` is the set of vertices that form edges in the subgraph `H`. -/ def support (H : Subgraph G) : Set V := Rel.dom H.Adj theorem mem_support (H : Subgraph G) {v : V} : v ∈ H.support ↔ ∃ w, H.Adj v w := Iff.rfl theorem support_subset_verts (H : Subgraph G) : H.support ⊆ H.verts := fun _ ⟨_, h⟩ ↦ H.edge_vert h /-- `G'.neighborSet v` is the set of vertices adjacent to `v` in `G'`. -/ def neighborSet (G' : Subgraph G) (v : V) : Set V := {w | G'.Adj v w} theorem neighborSet_subset (G' : Subgraph G) (v : V) : G'.neighborSet v ⊆ G.neighborSet v := fun _ ↦ G'.adj_sub theorem neighborSet_subset_verts (G' : Subgraph G) (v : V) : G'.neighborSet v ⊆ G'.verts := fun _ h ↦ G'.edge_vert (adj_symm G' h) @[simp] theorem mem_neighborSet (G' : Subgraph G) (v w : V) : w ∈ G'.neighborSet v ↔ G'.Adj v w := Iff.rfl /-- A subgraph as a graph has equivalent neighbor sets. -/ def coeNeighborSetEquiv {G' : Subgraph G} (v : G'.verts) : G'.coe.neighborSet v ≃ G'.neighborSet v where toFun w := ⟨w, w.2⟩ invFun w := ⟨⟨w, G'.edge_vert (G'.adj_symm w.2)⟩, w.2⟩ left_inv _ := rfl right_inv _ := rfl /-- The edge set of `G'` consists of a subset of edges of `G`. -/ def edgeSet (G' : Subgraph G) : Set (Sym2 V) := Sym2.fromRel G'.symm theorem edgeSet_subset (G' : Subgraph G) : G'.edgeSet ⊆ G.edgeSet := Sym2.ind (fun _ _ ↦ G'.adj_sub) @[simp] protected lemma mem_edgeSet {G' : Subgraph G} {v w : V} : s(v, w) ∈ G'.edgeSet ↔ G'.Adj v w := .rfl @[simp] lemma edgeSet_coe {G' : G.Subgraph} : G'.coe.edgeSet = Sym2.map (↑) ⁻¹' G'.edgeSet := by ext e; induction e using Sym2.ind; simp lemma image_coe_edgeSet_coe (G' : G.Subgraph) : Sym2.map (↑) '' G'.coe.edgeSet = G'.edgeSet := by rw [edgeSet_coe, Set.image_preimage_eq_iff] rintro e he induction e using Sym2.ind with | h a b => rw [Subgraph.mem_edgeSet] at he exact ⟨s(⟨a, edge_vert _ he⟩, ⟨b, edge_vert _ he.symm⟩), Sym2.map_pair_eq ..⟩ theorem mem_verts_of_mem_edge {G' : Subgraph G} {e : Sym2 V} {v : V} (he : e ∈ G'.edgeSet) (hv : v ∈ e) : v ∈ G'.verts := by induction e rcases Sym2.mem_iff.mp hv with (rfl | rfl) · exact G'.edge_vert he · exact G'.edge_vert (G'.symm he) /-- The `incidenceSet` is the set of edges incident to a given vertex. -/ def incidenceSet (G' : Subgraph G) (v : V) : Set (Sym2 V) := {e ∈ G'.edgeSet | v ∈ e} theorem incidenceSet_subset_incidenceSet (G' : Subgraph G) (v : V) : G'.incidenceSet v ⊆ G.incidenceSet v := fun _ h ↦ ⟨G'.edgeSet_subset h.1, h.2⟩ theorem incidenceSet_subset (G' : Subgraph G) (v : V) : G'.incidenceSet v ⊆ G'.edgeSet := fun _ h ↦ h.1 /-- Give a vertex as an element of the subgraph's vertex type. -/ abbrev vert (G' : Subgraph G) (v : V) (h : v ∈ G'.verts) : G'.verts := ⟨v, h⟩ /-- Create an equal copy of a subgraph (see `copy_eq`) with possibly different definitional equalities. See Note [range copy pattern]. -/ def copy (G' : Subgraph G) (V'' : Set V) (hV : V'' = G'.verts) (adj' : V → V → Prop) (hadj : adj' = G'.Adj) : Subgraph G where verts := V'' Adj := adj' adj_sub := hadj.symm ▸ G'.adj_sub edge_vert := hV.symm ▸ hadj.symm ▸ G'.edge_vert symm := hadj.symm ▸ G'.symm theorem copy_eq (G' : Subgraph G) (V'' : Set V) (hV : V'' = G'.verts) (adj' : V → V → Prop) (hadj : adj' = G'.Adj) : G'.copy V'' hV adj' hadj = G' := Subgraph.ext hV hadj /-- The union of two subgraphs. -/ instance : Max G.Subgraph where max G₁ G₂ := { verts := G₁.verts ∪ G₂.verts Adj := G₁.Adj ⊔ G₂.Adj adj_sub := fun hab => Or.elim hab (fun h => G₁.adj_sub h) fun h => G₂.adj_sub h edge_vert := Or.imp (fun h => G₁.edge_vert h) fun h => G₂.edge_vert h symm := fun _ _ => Or.imp G₁.adj_symm G₂.adj_symm } /-- The intersection of two subgraphs. -/ instance : Min G.Subgraph where min G₁ G₂ := { verts := G₁.verts ∩ G₂.verts Adj := G₁.Adj ⊓ G₂.Adj adj_sub := fun hab => G₁.adj_sub hab.1 edge_vert := And.imp (fun h => G₁.edge_vert h) fun h => G₂.edge_vert h symm := fun _ _ => And.imp G₁.adj_symm G₂.adj_symm } /-- The `top` subgraph is `G` as a subgraph of itself. -/ instance : Top G.Subgraph where top := { verts := Set.univ Adj := G.Adj adj_sub := id edge_vert := @fun v _ _ => Set.mem_univ v symm := G.symm } /-- The `bot` subgraph is the subgraph with no vertices or edges. -/ instance : Bot G.Subgraph where bot := { verts := ∅ Adj := ⊥ adj_sub := False.elim edge_vert := False.elim symm := fun _ _ => id } instance : SupSet G.Subgraph where sSup s := { verts := ⋃ G' ∈ s, verts G' Adj := fun a b => ∃ G' ∈ s, Adj G' a b adj_sub := by rintro a b ⟨G', -, hab⟩ exact G'.adj_sub hab edge_vert := by rintro a b ⟨G', hG', hab⟩ exact Set.mem_iUnion₂_of_mem hG' (G'.edge_vert hab) symm := fun a b h => by simpa [adj_comm] using h } instance : InfSet G.Subgraph where sInf s := { verts := ⋂ G' ∈ s, verts G' Adj := fun a b => (∀ ⦃G'⦄, G' ∈ s → Adj G' a b) ∧ G.Adj a b adj_sub := And.right edge_vert := fun hab => Set.mem_iInter₂_of_mem fun G' hG' => G'.edge_vert <| hab.1 hG' symm := fun _ _ => And.imp (forall₂_imp fun _ _ => Adj.symm) G.adj_symm } @[simp] theorem sup_adj : (G₁ ⊔ G₂).Adj a b ↔ G₁.Adj a b ∨ G₂.Adj a b := Iff.rfl @[simp] theorem inf_adj : (G₁ ⊓ G₂).Adj a b ↔ G₁.Adj a b ∧ G₂.Adj a b := Iff.rfl @[simp] theorem top_adj : (⊤ : Subgraph G).Adj a b ↔ G.Adj a b := Iff.rfl @[simp] theorem not_bot_adj : ¬ (⊥ : Subgraph G).Adj a b := not_false @[simp] theorem verts_sup (G₁ G₂ : G.Subgraph) : (G₁ ⊔ G₂).verts = G₁.verts ∪ G₂.verts := rfl @[simp] theorem verts_inf (G₁ G₂ : G.Subgraph) : (G₁ ⊓ G₂).verts = G₁.verts ∩ G₂.verts := rfl @[simp] theorem verts_top : (⊤ : G.Subgraph).verts = Set.univ := rfl @[simp] theorem verts_bot : (⊥ : G.Subgraph).verts = ∅ := rfl @[simp] theorem sSup_adj {s : Set G.Subgraph} : (sSup s).Adj a b ↔ ∃ G ∈ s, Adj G a b := Iff.rfl @[simp] theorem sInf_adj {s : Set G.Subgraph} : (sInf s).Adj a b ↔ (∀ G' ∈ s, Adj G' a b) ∧ G.Adj a b := Iff.rfl @[simp] theorem iSup_adj {f : ι → G.Subgraph} : (⨆ i, f i).Adj a b ↔ ∃ i, (f i).Adj a b := by simp [iSup] @[simp] theorem iInf_adj {f : ι → G.Subgraph} : (⨅ i, f i).Adj a b ↔ (∀ i, (f i).Adj a b) ∧ G.Adj a b := by simp [iInf] theorem sInf_adj_of_nonempty {s : Set G.Subgraph} (hs : s.Nonempty) : (sInf s).Adj a b ↔ ∀ G' ∈ s, Adj G' a b := sInf_adj.trans <| and_iff_left_of_imp <| by obtain ⟨G', hG'⟩ := hs exact fun h => G'.adj_sub (h _ hG') theorem iInf_adj_of_nonempty [Nonempty ι] {f : ι → G.Subgraph} : (⨅ i, f i).Adj a b ↔ ∀ i, (f i).Adj a b := by rw [iInf, sInf_adj_of_nonempty (Set.range_nonempty _)] simp @[simp] theorem verts_sSup (s : Set G.Subgraph) : (sSup s).verts = ⋃ G' ∈ s, verts G' := rfl @[simp] theorem verts_sInf (s : Set G.Subgraph) : (sInf s).verts = ⋂ G' ∈ s, verts G' := rfl @[simp] theorem verts_iSup {f : ι → G.Subgraph} : (⨆ i, f i).verts = ⋃ i, (f i).verts := by simp [iSup] @[simp] theorem verts_iInf {f : ι → G.Subgraph} : (⨅ i, f i).verts = ⋂ i, (f i).verts := by simp [iInf] @[simp] lemma coe_bot : (⊥ : G.Subgraph).coe = ⊥ := rfl @[simp] lemma IsInduced.top : (⊤ : G.Subgraph).IsInduced := fun _ _ _ _ ↦ id /-- The graph isomorphism between the top element of `G.subgraph` and `G`. -/ def topIso : (⊤ : G.Subgraph).coe ≃g G where toFun := (↑) invFun a := ⟨a, Set.mem_univ _⟩ left_inv _ := Subtype.eta .. right_inv _ := rfl map_rel_iff' := .rfl theorem verts_spanningCoe_injective : (fun G' : Subgraph G => (G'.verts, G'.spanningCoe)).Injective := by intro G₁ G₂ h rw [Prod.ext_iff] at h exact Subgraph.ext h.1 (spanningCoe_inj.1 h.2) /-- For subgraphs `G₁`, `G₂`, `G₁ ≤ G₂` iff `G₁.verts ⊆ G₂.verts` and `∀ a b, G₁.adj a b → G₂.adj a b`. -/ instance distribLattice : DistribLattice G.Subgraph := { show DistribLattice G.Subgraph from verts_spanningCoe_injective.distribLattice _ (fun _ _ => rfl) fun _ _ => rfl with le := fun x y => x.verts ⊆ y.verts ∧ ∀ ⦃v w : V⦄, x.Adj v w → y.Adj v w } instance : BoundedOrder (Subgraph G) where top := ⊤ bot := ⊥ le_top x := ⟨Set.subset_univ _, fun _ _ => x.adj_sub⟩ bot_le _ := ⟨Set.empty_subset _, fun _ _ => False.elim⟩ /-- Note that subgraphs do not form a Boolean algebra, because of `verts`. -/ def completelyDistribLatticeMinimalAxioms : CompletelyDistribLattice.MinimalAxioms G.Subgraph := { Subgraph.distribLattice with le := (· ≤ ·) sup := (· ⊔ ·) inf := (· ⊓ ·) top := ⊤ bot := ⊥ le_top := fun G' => ⟨Set.subset_univ _, fun _ _ => G'.adj_sub⟩ bot_le := fun _ => ⟨Set.empty_subset _, fun _ _ => False.elim⟩ sSup := sSup -- Porting note: needed `apply` here to modify elaboration; previously the term itself was fine. le_sSup := fun s G' hG' => ⟨by apply Set.subset_iUnion₂ G' hG', fun _ _ hab => ⟨G', hG', hab⟩⟩ sSup_le := fun s G' hG' => ⟨Set.iUnion₂_subset fun _ hH => (hG' _ hH).1, by rintro a b ⟨H, hH, hab⟩ exact (hG' _ hH).2 hab⟩ sInf := sInf sInf_le := fun _ G' hG' => ⟨Set.iInter₂_subset G' hG', fun _ _ hab => hab.1 hG'⟩ le_sInf := fun _ G' hG' => ⟨Set.subset_iInter₂ fun _ hH => (hG' _ hH).1, fun _ _ hab => ⟨fun _ hH => (hG' _ hH).2 hab, G'.adj_sub hab⟩⟩ iInf_iSup_eq := fun f => Subgraph.ext (by simpa using iInf_iSup_eq) (by ext; simp [Classical.skolem]) } instance : CompletelyDistribLattice G.Subgraph := .ofMinimalAxioms completelyDistribLatticeMinimalAxioms @[gcongr] lemma verts_mono {H H' : G.Subgraph} (h : H ≤ H') : H.verts ⊆ H'.verts := h.1 lemma verts_monotone : Monotone (verts : G.Subgraph → Set V) := fun _ _ h ↦ h.1 @[simps] instance subgraphInhabited : Inhabited (Subgraph G) := ⟨⊥⟩ @[simp] theorem neighborSet_sup {H H' : G.Subgraph} (v : V) : (H ⊔ H').neighborSet v = H.neighborSet v ∪ H'.neighborSet v := rfl @[simp] theorem neighborSet_inf {H H' : G.Subgraph} (v : V) : (H ⊓ H').neighborSet v = H.neighborSet v ∩ H'.neighborSet v := rfl @[simp] theorem neighborSet_top (v : V) : (⊤ : G.Subgraph).neighborSet v = G.neighborSet v := rfl @[simp] theorem neighborSet_bot (v : V) : (⊥ : G.Subgraph).neighborSet v = ∅ := rfl @[simp] theorem neighborSet_sSup (s : Set G.Subgraph) (v : V) : (sSup s).neighborSet v = ⋃ G' ∈ s, neighborSet G' v := by ext simp @[simp] theorem neighborSet_sInf (s : Set G.Subgraph) (v : V) : (sInf s).neighborSet v = (⋂ G' ∈ s, neighborSet G' v) ∩ G.neighborSet v := by ext simp @[simp] theorem neighborSet_iSup (f : ι → G.Subgraph) (v : V) : (⨆ i, f i).neighborSet v = ⋃ i, (f i).neighborSet v := by simp [iSup] @[simp] theorem neighborSet_iInf (f : ι → G.Subgraph) (v : V) : (⨅ i, f i).neighborSet v = (⋂ i, (f i).neighborSet v) ∩ G.neighborSet v := by simp [iInf] @[simp] theorem edgeSet_top : (⊤ : Subgraph G).edgeSet = G.edgeSet := rfl @[simp] theorem edgeSet_bot : (⊥ : Subgraph G).edgeSet = ∅ := Set.ext <| Sym2.ind (by simp) @[simp] theorem edgeSet_inf {H₁ H₂ : Subgraph G} : (H₁ ⊓ H₂).edgeSet = H₁.edgeSet ∩ H₂.edgeSet := Set.ext <| Sym2.ind (by simp) @[simp] theorem edgeSet_sup {H₁ H₂ : Subgraph G} : (H₁ ⊔ H₂).edgeSet = H₁.edgeSet ∪ H₂.edgeSet := Set.ext <| Sym2.ind (by simp) @[simp] theorem edgeSet_sSup (s : Set G.Subgraph) : (sSup s).edgeSet = ⋃ G' ∈ s, edgeSet G' := by ext e induction e simp @[simp] theorem edgeSet_sInf (s : Set G.Subgraph) : (sInf s).edgeSet = (⋂ G' ∈ s, edgeSet G') ∩ G.edgeSet := by ext e induction e simp @[simp] theorem edgeSet_iSup (f : ι → G.Subgraph) : (⨆ i, f i).edgeSet = ⋃ i, (f i).edgeSet := by simp [iSup] @[simp] theorem edgeSet_iInf (f : ι → G.Subgraph) : (⨅ i, f i).edgeSet = (⋂ i, (f i).edgeSet) ∩ G.edgeSet := by simp [iInf] @[simp] theorem spanningCoe_top : (⊤ : Subgraph G).spanningCoe = G := rfl @[simp] theorem spanningCoe_bot : (⊥ : Subgraph G).spanningCoe = ⊥ := rfl /-- Turn a subgraph of a `SimpleGraph` into a member of its subgraph type. -/ @[simps] def _root_.SimpleGraph.toSubgraph (H : SimpleGraph V) (h : H ≤ G) : G.Subgraph where verts := Set.univ Adj := H.Adj adj_sub e := h e edge_vert _ := Set.mem_univ _ symm := H.symm theorem support_mono {H H' : Subgraph G} (h : H ≤ H') : H.support ⊆ H'.support := Rel.dom_mono h.2 theorem _root_.SimpleGraph.toSubgraph.isSpanning (H : SimpleGraph V) (h : H ≤ G) : (toSubgraph H h).IsSpanning := Set.mem_univ theorem spanningCoe_le_of_le {H H' : Subgraph G} (h : H ≤ H') : H.spanningCoe ≤ H'.spanningCoe := h.2 @[simp] lemma sup_spanningCoe (H H' : Subgraph G) : (H ⊔ H').spanningCoe = H.spanningCoe ⊔ H'.spanningCoe := rfl /-- The top of the `Subgraph G` lattice is equivalent to the graph itself. -/ def topEquiv : (⊤ : Subgraph G).coe ≃g G where toFun v := ↑v invFun v := ⟨v, trivial⟩ left_inv _ := rfl right_inv _ := rfl map_rel_iff' := Iff.rfl /-- The bottom of the `Subgraph G` lattice is equivalent to the empty graph on the empty vertex type. -/ def botEquiv : (⊥ : Subgraph G).coe ≃g (⊥ : SimpleGraph Empty) where toFun v := v.property.elim invFun v := v.elim left_inv := fun ⟨_, h⟩ ↦ h.elim right_inv v := v.elim map_rel_iff' := Iff.rfl theorem edgeSet_mono {H₁ H₂ : Subgraph G} (h : H₁ ≤ H₂) : H₁.edgeSet ≤ H₂.edgeSet := Sym2.ind h.2 theorem _root_.Disjoint.edgeSet {H₁ H₂ : Subgraph G} (h : Disjoint H₁ H₂) : Disjoint H₁.edgeSet H₂.edgeSet := disjoint_iff_inf_le.mpr <| by simpa using edgeSet_mono h.le_bot section map variable {G' : SimpleGraph W} {f : G →g G'} /-- Graph homomorphisms induce a covariant function on subgraphs. -/ @[simps] protected def map (f : G →g G') (H : G.Subgraph) : G'.Subgraph where verts := f '' H.verts Adj := Relation.Map H.Adj f f adj_sub := by rintro _ _ ⟨u, v, h, rfl, rfl⟩ exact f.map_rel (H.adj_sub h) edge_vert := by rintro _ _ ⟨u, v, h, rfl, rfl⟩ exact Set.mem_image_of_mem _ (H.edge_vert h) symm := by rintro _ _ ⟨u, v, h, rfl, rfl⟩ exact ⟨v, u, H.symm h, rfl, rfl⟩ @[simp] lemma map_id (H : G.Subgraph) : H.map Hom.id = H := by ext <;> simp lemma map_comp {U : Type*} {G'' : SimpleGraph U} (H : G.Subgraph) (f : G →g G') (g : G' →g G'') : H.map (g.comp f) = (H.map f).map g := by ext <;> simp [Subgraph.map] @[gcongr] lemma map_mono {H₁ H₂ : G.Subgraph} (hH : H₁ ≤ H₂) : H₁.map f ≤ H₂.map f := by constructor · intro simp only [map_verts, Set.mem_image, forall_exists_index, and_imp] rintro v hv rfl exact ⟨_, hH.1 hv, rfl⟩ · rintro _ _ ⟨u, v, ha, rfl, rfl⟩ exact ⟨_, _, hH.2 ha, rfl, rfl⟩ lemma map_monotone : Monotone (Subgraph.map f) := fun _ _ ↦ map_mono theorem map_sup (f : G →g G') (H₁ H₂ : G.Subgraph) : (H₁ ⊔ H₂).map f = H₁.map f ⊔ H₂.map f := by ext <;> simp [Set.image_union, map_adj, sup_adj, Relation.Map, or_and_right, exists_or] @[simp] lemma map_iso_top {H : SimpleGraph W} (e : G ≃g H) : Subgraph.map e.toHom ⊤ = ⊤ := by ext <;> simp [Relation.Map, e.apply_eq_iff_eq_symm_apply, ← e.map_rel_iff] @[simp] lemma edgeSet_map (f : G →g G') (H : G.Subgraph) : (H.map f).edgeSet = Sym2.map f '' H.edgeSet := Sym2.fromRel_relationMap .. end map /-- Graph homomorphisms induce a contravariant function on subgraphs. -/ @[simps] protected def comap {G' : SimpleGraph W} (f : G →g G') (H : G'.Subgraph) : G.Subgraph where verts := f ⁻¹' H.verts Adj u v := G.Adj u v ∧ H.Adj (f u) (f v) adj_sub h := h.1 edge_vert h := Set.mem_preimage.1 (H.edge_vert h.2) symm _ _ h := ⟨G.symm h.1, H.symm h.2⟩ theorem comap_monotone {G' : SimpleGraph W} (f : G →g G') : Monotone (Subgraph.comap f) := by intro H H' h constructor · intro simp only [comap_verts, Set.mem_preimage] apply h.1 · intro v w simp +contextual only [comap_adj, and_imp, true_and] intro apply h.2 @[simp] lemma comap_equiv_top {H : SimpleGraph W} (f : G →g H) : Subgraph.comap f ⊤ = ⊤ := by ext <;> simp +contextual [f.map_adj] theorem map_le_iff_le_comap {G' : SimpleGraph W} (f : G →g G') (H : G.Subgraph) (H' : G'.Subgraph) : H.map f ≤ H' ↔ H ≤ H'.comap f := by refine ⟨fun h ↦ ⟨fun v hv ↦ ?_, fun v w hvw ↦ ?_⟩, fun h ↦ ⟨fun v ↦ ?_, fun v w ↦ ?_⟩⟩ · simp only [comap_verts, Set.mem_preimage] exact h.1 ⟨v, hv, rfl⟩ · simp only [H.adj_sub hvw, comap_adj, true_and] exact h.2 ⟨v, w, hvw, rfl, rfl⟩ · simp only [map_verts, Set.mem_image, forall_exists_index, and_imp] rintro w hw rfl exact h.1 hw · simp only [Relation.Map, map_adj, forall_exists_index, and_imp] rintro u u' hu rfl rfl exact (h.2 hu).2 instance [DecidableEq V] [Fintype V] [DecidableRel G.Adj] : Fintype G.Subgraph := by refine .ofBijective (α := {H : Finset V × (V → V → Bool) // (∀ a b, H.2 a b → G.Adj a b) ∧ (∀ a b, H.2 a b → a ∈ H.1) ∧ ∀ a b, H.2 a b = H.2 b a}) (fun H ↦ ⟨H.1.1, fun a b ↦ H.1.2 a b, @H.2.1, @H.2.2.1, by simp [Symmetric, H.2.2.2]⟩) ⟨?_, fun H ↦ ?_⟩ · rintro ⟨⟨_, _⟩, -⟩ ⟨⟨_, _⟩, -⟩ simp [funext_iff] · classical exact ⟨⟨(H.verts.toFinset, fun a b ↦ H.Adj a b), fun a b ↦ by simpa using H.adj_sub, fun a b ↦ by simpa using H.edge_vert, by simp [H.adj_comm]⟩, by simp⟩ instance [Finite V] : Finite G.Subgraph := by classical cases nonempty_fintype V; infer_instance /-- Given two subgraphs, one a subgraph of the other, there is an induced injective homomorphism of the subgraphs as graphs. -/ @[simps] def inclusion {x y : Subgraph G} (h : x ≤ y) : x.coe →g y.coe where toFun v := ⟨↑v, And.left h v.property⟩ map_rel' hvw := h.2 hvw theorem inclusion.injective {x y : Subgraph G} (h : x ≤ y) : Function.Injective (inclusion h) := by intro v w h rw [inclusion, DFunLike.coe, Subtype.mk_eq_mk] at h exact Subtype.ext h /-- There is an induced injective homomorphism of a subgraph of `G` into `G`. -/ @[simps] protected def hom (x : Subgraph G) : x.coe →g G where toFun v := v map_rel' := x.adj_sub @[simp] lemma coe_hom (x : Subgraph G) : (x.hom : x.verts → V) = (fun (v : x.verts) => (v : V)) := rfl theorem hom_injective {x : Subgraph G} : Function.Injective x.hom := fun _ _ ↦ Subtype.ext @[deprecated (since := "2025-03-15")] alias hom.injective := hom_injective @[simp] lemma map_hom_top (G' : G.Subgraph) : Subgraph.map G'.hom ⊤ = G' := by aesop (add unfold safe Relation.Map, unsafe G'.edge_vert, unsafe Adj.symm) /-- There is an induced injective homomorphism of a subgraph of `G` as a spanning subgraph into `G`. -/ @[simps] def spanningHom (x : Subgraph G) : x.spanningCoe →g G where toFun := id map_rel' := x.adj_sub theorem spanningHom_injective {x : Subgraph G} : Function.Injective x.spanningHom := fun _ _ ↦ id @[deprecated (since := "2025-03-15")] alias spanningHom.injective := spanningHom_injective theorem neighborSet_subset_of_subgraph {x y : Subgraph G} (h : x ≤ y) (v : V) : x.neighborSet v ⊆ y.neighborSet v := fun _ h' ↦ h.2 h' instance neighborSet.decidablePred (G' : Subgraph G) [h : DecidableRel G'.Adj] (v : V) : DecidablePred (· ∈ G'.neighborSet v) := h v /-- If a graph is locally finite at a vertex, then so is a subgraph of that graph. -/ instance finiteAt {G' : Subgraph G} (v : G'.verts) [DecidableRel G'.Adj] [Fintype (G.neighborSet v)] : Fintype (G'.neighborSet v) := Set.fintypeSubset (G.neighborSet v) (G'.neighborSet_subset v) /-- If a subgraph is locally finite at a vertex, then so are subgraphs of that subgraph. This is not an instance because `G''` cannot be inferred. -/ def finiteAtOfSubgraph {G' G'' : Subgraph G} [DecidableRel G'.Adj] (h : G' ≤ G'') (v : G'.verts) [Fintype (G''.neighborSet v)] : Fintype (G'.neighborSet v) := Set.fintypeSubset (G''.neighborSet v) (neighborSet_subset_of_subgraph h v) instance (G' : Subgraph G) [Fintype G'.verts] (v : V) [DecidablePred (· ∈ G'.neighborSet v)] : Fintype (G'.neighborSet v) := Set.fintypeSubset G'.verts (neighborSet_subset_verts G' v) instance coeFiniteAt {G' : Subgraph G} (v : G'.verts) [Fintype (G'.neighborSet v)] : Fintype (G'.coe.neighborSet v) := Fintype.ofEquiv _ (coeNeighborSetEquiv v).symm theorem IsSpanning.card_verts [Fintype V] {G' : Subgraph G} [Fintype G'.verts] (h : G'.IsSpanning) : G'.verts.toFinset.card = Fintype.card V := by simp only [isSpanning_iff.1 h, Set.toFinset_univ] congr /-- The degree of a vertex in a subgraph. It's zero for vertices outside the subgraph. -/ def degree (G' : Subgraph G) (v : V) [Fintype (G'.neighborSet v)] : ℕ := Fintype.card (G'.neighborSet v) theorem finset_card_neighborSet_eq_degree {G' : Subgraph G} {v : V} [Fintype (G'.neighborSet v)] : (G'.neighborSet v).toFinset.card = G'.degree v := by rw [degree, Set.toFinset_card] theorem degree_le (G' : Subgraph G) (v : V) [Fintype (G'.neighborSet v)] [Fintype (G.neighborSet v)] : G'.degree v ≤ G.degree v := by rw [← card_neighborSet_eq_degree] exact Set.card_le_card (G'.neighborSet_subset v) theorem degree_le' (G' G'' : Subgraph G) (h : G' ≤ G'') (v : V) [Fintype (G'.neighborSet v)] [Fintype (G''.neighborSet v)] : G'.degree v ≤ G''.degree v := Set.card_le_card (neighborSet_subset_of_subgraph h v) @[simp] theorem coe_degree (G' : Subgraph G) (v : G'.verts) [Fintype (G'.coe.neighborSet v)] [Fintype (G'.neighborSet v)] : G'.coe.degree v = G'.degree v := by rw [← card_neighborSet_eq_degree] exact Fintype.card_congr (coeNeighborSetEquiv v) @[simp] theorem degree_spanningCoe {G' : G.Subgraph} (v : V) [Fintype (G'.neighborSet v)] [Fintype (G'.spanningCoe.neighborSet v)] : G'.spanningCoe.degree v = G'.degree v := by rw [← card_neighborSet_eq_degree, Subgraph.degree] congr! theorem degree_eq_one_iff_unique_adj {G' : Subgraph G} {v : V} [Fintype (G'.neighborSet v)] : G'.degree v = 1 ↔ ∃! w : V, G'.Adj v w := by rw [← finset_card_neighborSet_eq_degree, Finset.card_eq_one, Finset.singleton_iff_unique_mem] simp only [Set.mem_toFinset, mem_neighborSet] lemma neighborSet_eq_of_equiv {v : V} {H : Subgraph G} (h : G.neighborSet v ≃ H.neighborSet v) (hfin : (G.neighborSet v).Finite) : H.neighborSet v = G.neighborSet v := by lift H.neighborSet v to Finset V using h.set_finite_iff.mp hfin with s hs lift G.neighborSet v to Finset V using hfin with t ht refine congrArg _ <| Finset.eq_of_subset_of_card_le ?_ (Finset.card_eq_of_equiv h).le rw [← Finset.coe_subset, hs, ht] exact H.neighborSet_subset _ lemma adj_iff_of_neighborSet_equiv {v : V} {H : Subgraph G} (h : G.neighborSet v ≃ H.neighborSet v) (hfin : (G.neighborSet v).Finite) : ∀ {w}, H.Adj v w ↔ G.Adj v w := Set.ext_iff.mp (neighborSet_eq_of_equiv h hfin) _ end Subgraph section MkProperties /-! ### Properties of `singletonSubgraph` and `subgraphOfAdj` -/ variable {G : SimpleGraph V} {G' : SimpleGraph W} instance nonempty_singletonSubgraph_verts (v : V) : Nonempty (G.singletonSubgraph v).verts := ⟨⟨v, Set.mem_singleton v⟩⟩ @[simp] theorem singletonSubgraph_le_iff (v : V) (H : G.Subgraph) : G.singletonSubgraph v ≤ H ↔ v ∈ H.verts := by refine ⟨fun h ↦ h.1 (Set.mem_singleton v), ?_⟩ intro h constructor · rwa [singletonSubgraph_verts, Set.singleton_subset_iff] · exact fun _ _ ↦ False.elim @[simp] theorem map_singletonSubgraph (f : G →g G') {v : V} : Subgraph.map f (G.singletonSubgraph v) = G'.singletonSubgraph (f v) := by ext <;> simp only [Relation.Map, Subgraph.map_adj, singletonSubgraph_adj, Pi.bot_apply, exists_and_left, and_iff_left_iff_imp, IsEmpty.forall_iff, Subgraph.map_verts, singletonSubgraph_verts, Set.image_singleton] exact False.elim @[simp] theorem neighborSet_singletonSubgraph (v w : V) : (G.singletonSubgraph v).neighborSet w = ∅ := rfl @[simp] theorem edgeSet_singletonSubgraph (v : V) : (G.singletonSubgraph v).edgeSet = ∅ := Sym2.fromRel_bot theorem eq_singletonSubgraph_iff_verts_eq (H : G.Subgraph) {v : V} : H = G.singletonSubgraph v ↔ H.verts = {v} := by refine ⟨fun h ↦ by rw [h, singletonSubgraph_verts], fun h ↦ ?_⟩ ext · rw [h, singletonSubgraph_verts] · simp only [Prop.bot_eq_false, singletonSubgraph_adj, Pi.bot_apply, iff_false] intro ha have ha1 := ha.fst_mem have ha2 := ha.snd_mem rw [h, Set.mem_singleton_iff] at ha1 ha2 subst_vars exact ha.ne rfl instance nonempty_subgraphOfAdj_verts {v w : V} (hvw : G.Adj v w) : Nonempty (G.subgraphOfAdj hvw).verts := ⟨⟨v, by simp⟩⟩ @[simp] theorem edgeSet_subgraphOfAdj {v w : V} (hvw : G.Adj v w) : (G.subgraphOfAdj hvw).edgeSet = {s(v, w)} := by ext e refine e.ind ?_ simp only [eq_comm, Set.mem_singleton_iff, Subgraph.mem_edgeSet, subgraphOfAdj_adj, forall₂_true_iff] lemma subgraphOfAdj_le_of_adj {v w : V} (H : G.Subgraph) (h : H.Adj v w) : G.subgraphOfAdj (H.adj_sub h) ≤ H := by constructor · intro x rintro (rfl | rfl) <;> simp [H.edge_vert h, H.edge_vert h.symm] · simp only [subgraphOfAdj_adj, Sym2.eq, Sym2.rel_iff] rintro _ _ (⟨rfl, rfl⟩ | ⟨rfl, rfl⟩) <;> simp [h, h.symm] theorem subgraphOfAdj_symm {v w : V} (hvw : G.Adj v w) : G.subgraphOfAdj hvw.symm = G.subgraphOfAdj hvw := by ext <;> simp [or_comm, and_comm] @[simp] theorem map_subgraphOfAdj (f : G →g G') {v w : V} (hvw : G.Adj v w) : Subgraph.map f (G.subgraphOfAdj hvw) = G'.subgraphOfAdj (f.map_adj hvw) := by ext · simp only [Subgraph.map_verts, subgraphOfAdj_verts, Set.mem_image, Set.mem_insert_iff, Set.mem_singleton_iff] constructor · rintro ⟨u, rfl | rfl, rfl⟩ <;> simp · rintro (rfl | rfl) · use v simp · use w simp · simp only [Relation.Map, Subgraph.map_adj, subgraphOfAdj_adj, Sym2.eq, Sym2.rel_iff] constructor · rintro ⟨a, b, ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩, rfl, rfl⟩ <;> simp · rintro (⟨rfl, rfl⟩ | ⟨rfl, rfl⟩) · use v, w simp · use w, v simp theorem neighborSet_subgraphOfAdj_subset {u v w : V} (hvw : G.Adj v w) : (G.subgraphOfAdj hvw).neighborSet u ⊆ {v, w} := (G.subgraphOfAdj hvw).neighborSet_subset_verts _ @[simp] theorem neighborSet_fst_subgraphOfAdj {v w : V} (hvw : G.Adj v w) : (G.subgraphOfAdj hvw).neighborSet v = {w} := by ext u suffices w = u ↔ u = w by simpa [hvw.ne.symm] using this rw [eq_comm] @[simp] theorem neighborSet_snd_subgraphOfAdj {v w : V} (hvw : G.Adj v w) : (G.subgraphOfAdj hvw).neighborSet w = {v} := by rw [subgraphOfAdj_symm hvw.symm] exact neighborSet_fst_subgraphOfAdj hvw.symm @[simp] theorem neighborSet_subgraphOfAdj_of_ne_of_ne {u v w : V} (hvw : G.Adj v w) (hv : u ≠ v) (hw : u ≠ w) : (G.subgraphOfAdj hvw).neighborSet u = ∅ := by ext simp [hv.symm, hw.symm] theorem neighborSet_subgraphOfAdj [DecidableEq V] {u v w : V} (hvw : G.Adj v w) : (G.subgraphOfAdj hvw).neighborSet u = (if u = v then {w} else ∅) ∪ if u = w then {v} else ∅ := by split_ifs <;> subst_vars <;> simp [*] theorem singletonSubgraph_fst_le_subgraphOfAdj {u v : V} {h : G.Adj u v} : G.singletonSubgraph u ≤ G.subgraphOfAdj h := by simp theorem singletonSubgraph_snd_le_subgraphOfAdj {u v : V} {h : G.Adj u v} : G.singletonSubgraph v ≤ G.subgraphOfAdj h := by simp @[simp] lemma support_subgraphOfAdj {u v : V} (h : G.Adj u v) : (G.subgraphOfAdj h).support = {u , v} := by ext rw [Subgraph.mem_support] simp only [subgraphOfAdj_adj, Sym2.eq, Sym2.rel_iff', Prod.mk.injEq, Prod.swap_prod_mk] refine ⟨?_, fun h ↦ h.elim (fun hl ↦ ⟨v, .inl ⟨hl.symm, rfl⟩⟩) fun hr ↦ ⟨u, .inr ⟨rfl, hr.symm⟩⟩⟩ rintro ⟨_, hw⟩ exact hw.elim (fun h1 ↦ .inl h1.1.symm) fun hr ↦ .inr hr.2.symm end MkProperties namespace Subgraph variable {G : SimpleGraph V} /-! ### Subgraphs of subgraphs -/ /-- Given a subgraph of a subgraph of `G`, construct a subgraph of `G`. -/ protected abbrev coeSubgraph {G' : G.Subgraph} : G'.coe.Subgraph → G.Subgraph := Subgraph.map G'.hom /-- Given a subgraph of `G`, restrict it to being a subgraph of another subgraph `G'` by taking the portion of `G` that intersects `G'`. -/ protected abbrev restrict {G' : G.Subgraph} : G.Subgraph → G'.coe.Subgraph := Subgraph.comap G'.hom @[simp] lemma verts_coeSubgraph {G' : Subgraph G} (G'' : Subgraph G'.coe) : (Subgraph.coeSubgraph G'').verts = (G''.verts : Set V) := rfl lemma coeSubgraph_adj {G' : G.Subgraph} (G'' : G'.coe.Subgraph) (v w : V) : (G'.coeSubgraph G'').Adj v w ↔ ∃ (hv : v ∈ G'.verts) (hw : w ∈ G'.verts), G''.Adj ⟨v, hv⟩ ⟨w, hw⟩ := by simp [Relation.Map] lemma restrict_adj {G' G'' : G.Subgraph} (v w : G'.verts) : (G'.restrict G'').Adj v w ↔ G'.Adj v w ∧ G''.Adj v w := Iff.rfl theorem restrict_coeSubgraph {G' : G.Subgraph} (G'' : G'.coe.Subgraph) : Subgraph.restrict (Subgraph.coeSubgraph G'') = G'' := by ext · simp · rw [restrict_adj, coeSubgraph_adj] simpa using G''.adj_sub theorem coeSubgraph_injective (G' : G.Subgraph) : Function.Injective (Subgraph.coeSubgraph : G'.coe.Subgraph → G.Subgraph) := Function.LeftInverse.injective restrict_coeSubgraph lemma coeSubgraph_le {H : G.Subgraph} (H' : H.coe.Subgraph) : Subgraph.coeSubgraph H' ≤ H := by constructor · simp · rintro v w ⟨_, _, h, rfl, rfl⟩ exact H'.adj_sub h lemma coeSubgraph_restrict_eq {H : G.Subgraph} (H' : G.Subgraph) : Subgraph.coeSubgraph (H.restrict H') = H ⊓ H' := by ext · simp [and_comm] · simp_rw [coeSubgraph_adj, restrict_adj] simp only [exists_and_left, exists_prop, inf_adj, and_congr_right_iff] intro h simp [H.edge_vert h, H.edge_vert h.symm] /-! ### Edge deletion -/ /-- Given a subgraph `G'` and a set of vertex pairs, remove all of the corresponding edges from its edge set, if present. See also: `SimpleGraph.deleteEdges`. -/ def deleteEdges (G' : G.Subgraph) (s : Set (Sym2 V)) : G.Subgraph where verts := G'.verts Adj := G'.Adj \ Sym2.ToRel s adj_sub h' := G'.adj_sub h'.1 edge_vert h' := G'.edge_vert h'.1 symm a b := by simp [G'.adj_comm, Sym2.eq_swap] section DeleteEdges variable {G' : G.Subgraph} (s : Set (Sym2 V)) @[simp] theorem deleteEdges_verts : (G'.deleteEdges s).verts = G'.verts := rfl @[simp] theorem deleteEdges_adj (v w : V) : (G'.deleteEdges s).Adj v w ↔ G'.Adj v w ∧ ¬s(v, w) ∈ s := Iff.rfl @[simp] theorem deleteEdges_deleteEdges (s s' : Set (Sym2 V)) : (G'.deleteEdges s).deleteEdges s' = G'.deleteEdges (s ∪ s') := by ext <;> simp [and_assoc, not_or] @[simp] theorem deleteEdges_empty_eq : G'.deleteEdges ∅ = G' := by ext <;> simp @[simp] theorem deleteEdges_spanningCoe_eq : G'.spanningCoe.deleteEdges s = (G'.deleteEdges s).spanningCoe := by ext simp theorem deleteEdges_coe_eq (s : Set (Sym2 G'.verts)) : G'.coe.deleteEdges s = (G'.deleteEdges (Sym2.map (↑) '' s)).coe := by ext ⟨v, hv⟩ ⟨w, hw⟩ simp only [SimpleGraph.deleteEdges_adj, coe_adj, deleteEdges_adj, Set.mem_image, not_exists, not_and, and_congr_right_iff] intro constructor · intro hs refine Sym2.ind ?_ rintro ⟨v', hv'⟩ ⟨w', hw'⟩ simp only [Sym2.map_pair_eq, Sym2.eq] contrapose! rintro (_ | _) <;> simpa only [Sym2.eq_swap] · intro h' hs exact h' _ hs rfl theorem coe_deleteEdges_eq (s : Set (Sym2 V)) : (G'.deleteEdges s).coe = G'.coe.deleteEdges (Sym2.map (↑) ⁻¹' s) := by ext ⟨v, hv⟩ ⟨w, hw⟩ simp theorem deleteEdges_le : G'.deleteEdges s ≤ G' := by constructor <;> simp +contextual [subset_rfl] theorem deleteEdges_le_of_le {s s' : Set (Sym2 V)} (h : s ⊆ s') : G'.deleteEdges s' ≤ G'.deleteEdges s := by constructor <;> simp +contextual only [deleteEdges_verts, deleteEdges_adj, true_and, and_imp, subset_rfl] exact fun _ _ _ hs' hs ↦ hs' (h hs) @[simp] theorem deleteEdges_inter_edgeSet_left_eq : G'.deleteEdges (G'.edgeSet ∩ s) = G'.deleteEdges s := by ext <;> simp +contextual [imp_false] @[simp] theorem deleteEdges_inter_edgeSet_right_eq : G'.deleteEdges (s ∩ G'.edgeSet) = G'.deleteEdges s := by ext <;> simp +contextual [imp_false] theorem coe_deleteEdges_le : (G'.deleteEdges s).coe ≤ (G'.coe : SimpleGraph G'.verts) := by intro v w simp +contextual theorem spanningCoe_deleteEdges_le (G' : G.Subgraph) (s : Set (Sym2 V)) : (G'.deleteEdges s).spanningCoe ≤ G'.spanningCoe :=
spanningCoe_le_of_le (deleteEdges_le s) end DeleteEdges /-! ### Induced subgraphs -/
Mathlib/Combinatorics/SimpleGraph/Subgraph.lean
1,111
1,115
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro -/ import Mathlib.MeasureTheory.Measure.Typeclasses.Finite import Mathlib.MeasureTheory.Measure.Typeclasses.NoAtoms import Mathlib.MeasureTheory.Measure.Typeclasses.Probability import Mathlib.MeasureTheory.Measure.Typeclasses.SFinite deprecated_module (since := "2025-04-13")
Mathlib/MeasureTheory/Measure/Typeclasses.lean
311
312
/- Copyright (c) 2017 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Y. Lewis, Keeley Hoek -/ import Mathlib.Algebra.NeZero import Mathlib.Data.Int.DivMod import Mathlib.Logic.Embedding.Basic import Mathlib.Logic.Equiv.Set import Mathlib.Tactic.Common import Mathlib.Tactic.Attr.Register /-! # The finite type with `n` elements `Fin n` is the type whose elements are natural numbers smaller than `n`. This file expands on the development in the core library. ## Main definitions ### Induction principles * `finZeroElim` : Elimination principle for the empty set `Fin 0`, generalizes `Fin.elim0`. Further definitions and eliminators can be found in `Init.Data.Fin.Lemmas` ### Embeddings and isomorphisms * `Fin.valEmbedding` : coercion to natural numbers as an `Embedding`; * `Fin.succEmb` : `Fin.succ` as an `Embedding`; * `Fin.castLEEmb h` : `Fin.castLE` as an `Embedding`, embed `Fin n` into `Fin m`, `h : n ≤ m`; * `finCongr` : `Fin.cast` as an `Equiv`, equivalence between `Fin n` and `Fin m` when `n = m`; * `Fin.castAddEmb m` : `Fin.castAdd` as an `Embedding`, embed `Fin n` into `Fin (n+m)`; * `Fin.castSuccEmb` : `Fin.castSucc` as an `Embedding`, embed `Fin n` into `Fin (n+1)`; * `Fin.addNatEmb m i` : `Fin.addNat` as an `Embedding`, add `m` on `i` on the right, generalizes `Fin.succ`; * `Fin.natAddEmb n i` : `Fin.natAdd` as an `Embedding`, adds `n` on `i` on the left; ### Other casts * `Fin.divNat i` : divides `i : Fin (m * n)` by `n`; * `Fin.modNat i` : takes the mod of `i : Fin (m * n)` by `n`; -/ assert_not_exists Monoid Finset open Fin Nat Function attribute [simp] Fin.succ_ne_zero Fin.castSucc_lt_last /-- Elimination principle for the empty set `Fin 0`, dependent version. -/ def finZeroElim {α : Fin 0 → Sort*} (x : Fin 0) : α x := x.elim0 namespace Fin @[simp] theorem mk_eq_one {n a : Nat} {ha : a < n + 2} : (⟨a, ha⟩ : Fin (n + 2)) = 1 ↔ a = 1 := mk.inj_iff @[simp] theorem one_eq_mk {n a : Nat} {ha : a < n + 2} : 1 = (⟨a, ha⟩ : Fin (n + 2)) ↔ a = 1 := by simp [eq_comm] instance {n : ℕ} : CanLift ℕ (Fin n) Fin.val (· < n) where prf k hk := ⟨⟨k, hk⟩, rfl⟩ /-- A dependent variant of `Fin.elim0`. -/ def rec0 {α : Fin 0 → Sort*} (i : Fin 0) : α i := absurd i.2 (Nat.not_lt_zero _) variable {n m : ℕ} --variable {a b : Fin n} -- this *really* breaks stuff theorem val_injective : Function.Injective (@Fin.val n) := @Fin.eq_of_val_eq n /-- If you actually have an element of `Fin n`, then the `n` is always positive -/ lemma size_positive : Fin n → 0 < n := Fin.pos lemma size_positive' [Nonempty (Fin n)] : 0 < n := ‹Nonempty (Fin n)›.elim Fin.pos protected theorem prop (a : Fin n) : a.val < n := a.2 lemma lt_last_iff_ne_last {a : Fin (n + 1)} : a < last n ↔ a ≠ last n := by simp [Fin.lt_iff_le_and_ne, le_last] lemma ne_zero_of_lt {a b : Fin (n + 1)} (hab : a < b) : b ≠ 0 := Fin.ne_of_gt <| Fin.lt_of_le_of_lt a.zero_le hab lemma ne_last_of_lt {a b : Fin (n + 1)} (hab : a < b) : a ≠ last n := Fin.ne_of_lt <| Fin.lt_of_lt_of_le hab b.le_last /-- Equivalence between `Fin n` and `{ i // i < n }`. -/ @[simps apply symm_apply] def equivSubtype : Fin n ≃ { i // i < n } where toFun a := ⟨a.1, a.2⟩ invFun a := ⟨a.1, a.2⟩ left_inv := fun ⟨_, _⟩ => rfl right_inv := fun ⟨_, _⟩ => rfl section coe /-! ### coercions and constructions -/ theorem val_eq_val (a b : Fin n) : (a : ℕ) = b ↔ a = b := Fin.ext_iff.symm theorem ne_iff_vne (a b : Fin n) : a ≠ b ↔ a.1 ≠ b.1 := Fin.ext_iff.not theorem mk_eq_mk {a h a' h'} : @mk n a h = @mk n a' h' ↔ a = a' := Fin.ext_iff -- syntactic tautologies now /-- Assume `k = l`. If two functions defined on `Fin k` and `Fin l` are equal on each element, then they coincide (in the heq sense). -/ protected theorem heq_fun_iff {α : Sort*} {k l : ℕ} (h : k = l) {f : Fin k → α} {g : Fin l → α} : HEq f g ↔ ∀ i : Fin k, f i = g ⟨(i : ℕ), h ▸ i.2⟩ := by subst h simp [funext_iff] /-- Assume `k = l` and `k' = l'`. If two functions `Fin k → Fin k' → α` and `Fin l → Fin l' → α` are equal on each pair, then they coincide (in the heq sense). -/ protected theorem heq_fun₂_iff {α : Sort*} {k l k' l' : ℕ} (h : k = l) (h' : k' = l') {f : Fin k → Fin k' → α} {g : Fin l → Fin l' → α} : HEq f g ↔ ∀ (i : Fin k) (j : Fin k'), f i j = g ⟨(i : ℕ), h ▸ i.2⟩ ⟨(j : ℕ), h' ▸ j.2⟩ := by subst h subst h' simp [funext_iff] /-- Two elements of `Fin k` and `Fin l` are heq iff their values in `ℕ` coincide. This requires `k = l`. For the left implication without this assumption, see `val_eq_val_of_heq`. -/ protected theorem heq_ext_iff {k l : ℕ} (h : k = l) {i : Fin k} {j : Fin l} : HEq i j ↔ (i : ℕ) = (j : ℕ) := by subst h simp [val_eq_val] end coe section Order /-! ### order -/ theorem le_iff_val_le_val {a b : Fin n} : a ≤ b ↔ (a : ℕ) ≤ b := Iff.rfl /-- `a < b` as natural numbers if and only if `a < b` in `Fin n`. -/ @[norm_cast, simp] theorem val_fin_lt {n : ℕ} {a b : Fin n} : (a : ℕ) < (b : ℕ) ↔ a < b := Iff.rfl /-- `a ≤ b` as natural numbers if and only if `a ≤ b` in `Fin n`. -/ @[norm_cast, simp] theorem val_fin_le {n : ℕ} {a b : Fin n} : (a : ℕ) ≤ (b : ℕ) ↔ a ≤ b := Iff.rfl theorem min_val {a : Fin n} : min (a : ℕ) n = a := by simp theorem max_val {a : Fin n} : max (a : ℕ) n = n := by simp /-- The inclusion map `Fin n → ℕ` is an embedding. -/ @[simps -fullyApplied apply] def valEmbedding : Fin n ↪ ℕ := ⟨val, val_injective⟩ @[simp] theorem equivSubtype_symm_trans_valEmbedding : equivSubtype.symm.toEmbedding.trans valEmbedding = Embedding.subtype (· < n) := rfl /-- Use the ordering on `Fin n` for checking recursive definitions. For example, the following definition is not accepted by the termination checker, unless we declare the `WellFoundedRelation` instance: ```lean def factorial {n : ℕ} : Fin n → ℕ | ⟨0, _⟩ := 1 | ⟨i + 1, hi⟩ := (i + 1) * factorial ⟨i, i.lt_succ_self.trans hi⟩ ``` -/ instance {n : ℕ} : WellFoundedRelation (Fin n) := measure (val : Fin n → ℕ) @[deprecated (since := "2025-02-24")] alias val_zero' := val_zero /-- `Fin.mk_zero` in `Lean` only applies in `Fin (n + 1)`. This one instead uses a `NeZero n` typeclass hypothesis. -/ @[simp] theorem mk_zero' (n : ℕ) [NeZero n] : (⟨0, pos_of_neZero n⟩ : Fin n) = 0 := rfl /-- The `Fin.zero_le` in `Lean` only applies in `Fin (n+1)`. This one instead uses a `NeZero n` typeclass hypothesis. -/ @[simp] protected theorem zero_le' [NeZero n] (a : Fin n) : 0 ≤ a := Nat.zero_le a.val @[simp, norm_cast] theorem val_eq_zero_iff [NeZero n] {a : Fin n} : a.val = 0 ↔ a = 0 := by rw [Fin.ext_iff, val_zero] theorem val_ne_zero_iff [NeZero n] {a : Fin n} : a.val ≠ 0 ↔ a ≠ 0 := val_eq_zero_iff.not @[simp, norm_cast] theorem val_pos_iff [NeZero n] {a : Fin n} : 0 < a.val ↔ 0 < a := by rw [← val_fin_lt, val_zero] /-- The `Fin.pos_iff_ne_zero` in `Lean` only applies in `Fin (n+1)`. This one instead uses a `NeZero n` typeclass hypothesis. -/ theorem pos_iff_ne_zero' [NeZero n] (a : Fin n) : 0 < a ↔ a ≠ 0 := by rw [← val_pos_iff, Nat.pos_iff_ne_zero, val_ne_zero_iff] @[simp] lemma cast_eq_self (a : Fin n) : a.cast rfl = a := rfl @[simp] theorem cast_eq_zero {k l : ℕ} [NeZero k] [NeZero l] (h : k = l) (x : Fin k) : Fin.cast h x = 0 ↔ x = 0 := by simp [← val_eq_zero_iff] lemma cast_injective {k l : ℕ} (h : k = l) : Injective (Fin.cast h) := fun a b hab ↦ by simpa [← val_eq_val] using hab theorem last_pos' [NeZero n] : 0 < last n := n.pos_of_neZero theorem one_lt_last [NeZero n] : 1 < last (n + 1) := by rw [lt_iff_val_lt_val, val_one, val_last, Nat.lt_add_left_iff_pos, Nat.pos_iff_ne_zero] exact NeZero.ne n end Order /-! ### Coercions to `ℤ` and the `fin_omega` tactic. -/ open Int theorem coe_int_sub_eq_ite {n : Nat} (u v : Fin n) : ((u - v : Fin n) : Int) = if v ≤ u then (u - v : Int) else (u - v : Int) + n := by rw [Fin.sub_def] split · rw [natCast_emod, Int.emod_eq_sub_self_emod, Int.emod_eq_of_lt] <;> omega · rw [natCast_emod, Int.emod_eq_of_lt] <;> omega theorem coe_int_sub_eq_mod {n : Nat} (u v : Fin n) : ((u - v : Fin n) : Int) = ((u : Int) - (v : Int)) % n := by rw [coe_int_sub_eq_ite] split · rw [Int.emod_eq_of_lt] <;> omega · rw [Int.emod_eq_add_self_emod, Int.emod_eq_of_lt] <;> omega theorem coe_int_add_eq_ite {n : Nat} (u v : Fin n) : ((u + v : Fin n) : Int) = if (u + v : ℕ) < n then (u + v : Int) else (u + v : Int) - n := by rw [Fin.add_def] split · rw [natCast_emod, Int.emod_eq_of_lt] <;> omega · rw [natCast_emod, Int.emod_eq_sub_self_emod, Int.emod_eq_of_lt] <;> omega theorem coe_int_add_eq_mod {n : Nat} (u v : Fin n) : ((u + v : Fin n) : Int) = ((u : Int) + (v : Int)) % n := by rw [coe_int_add_eq_ite] split · rw [Int.emod_eq_of_lt] <;> omega · rw [Int.emod_eq_sub_self_emod, Int.emod_eq_of_lt] <;> omega -- Write `a + b` as `if (a + b : ℕ) < n then (a + b : ℤ) else (a + b : ℤ) - n` and -- similarly `a - b` as `if (b : ℕ) ≤ a then (a - b : ℤ) else (a - b : ℤ) + n`. attribute [fin_omega] coe_int_sub_eq_ite coe_int_add_eq_ite -- Rewrite inequalities in `Fin` to inequalities in `ℕ` attribute [fin_omega] Fin.lt_iff_val_lt_val Fin.le_iff_val_le_val -- Rewrite `1 : Fin (n + 2)` to `1 : ℤ` attribute [fin_omega] val_one /-- Preprocessor for `omega` to handle inequalities in `Fin`. Note that this involves a lot of case splitting, so may be slow. -/ -- Further adjustment to the simp set can probably make this more powerful. -- Please experiment and PR updates! macro "fin_omega" : tactic => `(tactic| { try simp only [fin_omega, ← Int.ofNat_lt, ← Int.ofNat_le] at * omega }) section Add /-! ### addition, numerals, and coercion from Nat -/ @[simp] theorem val_one' (n : ℕ) [NeZero n] : ((1 : Fin n) : ℕ) = 1 % n := rfl @[deprecated val_one' (since := "2025-03-10")] theorem val_one'' {n : ℕ} : ((1 : Fin (n + 1)) : ℕ) = 1 % (n + 1) := rfl instance nontrivial {n : ℕ} : Nontrivial (Fin (n + 2)) where exists_pair_ne := ⟨0, 1, (ne_iff_vne 0 1).mpr (by simp [val_one, val_zero])⟩ theorem nontrivial_iff_two_le : Nontrivial (Fin n) ↔ 2 ≤ n := by rcases n with (_ | _ | n) <;> simp [Fin.nontrivial, not_nontrivial, Nat.succ_le_iff] section Monoid instance inhabitedFinOneAdd (n : ℕ) : Inhabited (Fin (1 + n)) := haveI : NeZero (1 + n) := by rw [Nat.add_comm]; infer_instance inferInstance @[simp] theorem default_eq_zero (n : ℕ) [NeZero n] : (default : Fin n) = 0 := rfl instance instNatCast [NeZero n] : NatCast (Fin n) where natCast i := Fin.ofNat' n i lemma natCast_def [NeZero n] (a : ℕ) : (a : Fin n) = ⟨a % n, mod_lt _ n.pos_of_neZero⟩ := rfl end Monoid theorem val_add_eq_ite {n : ℕ} (a b : Fin n) : (↑(a + b) : ℕ) = if n ≤ a + b then a + b - n else a + b := by rw [Fin.val_add, Nat.add_mod_eq_ite, Nat.mod_eq_of_lt (show ↑a < n from a.2), Nat.mod_eq_of_lt (show ↑b < n from b.2)] theorem val_add_eq_of_add_lt {n : ℕ} {a b : Fin n} (huv : a.val + b.val < n) : (a + b).val = a.val + b.val := by rw [val_add] simp [Nat.mod_eq_of_lt huv] lemma intCast_val_sub_eq_sub_add_ite {n : ℕ} (a b : Fin n) : ((a - b).val : ℤ) = a.val - b.val + if b ≤ a then 0 else n := by split <;> fin_omega lemma one_le_of_ne_zero {n : ℕ} [NeZero n] {k : Fin n} (hk : k ≠ 0) : 1 ≤ k := by obtain ⟨n, rfl⟩ := Nat.exists_eq_succ_of_ne_zero (NeZero.ne n) cases n with | zero => simp only [Nat.reduceAdd, Fin.isValue, Fin.zero_le] | succ n => rwa [Fin.le_iff_val_le_val, Fin.val_one, Nat.one_le_iff_ne_zero, val_ne_zero_iff] lemma val_sub_one_of_ne_zero [NeZero n] {i : Fin n} (hi : i ≠ 0) : (i - 1).val = i - 1 := by obtain ⟨n, rfl⟩ := Nat.exists_eq_succ_of_ne_zero (NeZero.ne n) rw [Fin.sub_val_of_le (one_le_of_ne_zero hi), Fin.val_one', Nat.mod_eq_of_lt (Nat.succ_le_iff.mpr (nontrivial_iff_two_le.mp <| nontrivial_of_ne i 0 hi))] section OfNatCoe @[simp] theorem ofNat'_eq_cast (n : ℕ) [NeZero n] (a : ℕ) : Fin.ofNat' n a = a := rfl @[simp] lemma val_natCast (a n : ℕ) [NeZero n] : (a : Fin n).val = a % n := rfl /-- Converting an in-range number to `Fin (n + 1)` produces a result whose value is the original number. -/ theorem val_cast_of_lt {n : ℕ} [NeZero n] {a : ℕ} (h : a < n) : (a : Fin n).val = a := Nat.mod_eq_of_lt h /-- If `n` is non-zero, converting the value of a `Fin n` to `Fin n` results in the same value. -/ @[simp, norm_cast] theorem cast_val_eq_self {n : ℕ} [NeZero n] (a : Fin n) : (a.val : Fin n) = a := Fin.ext <| val_cast_of_lt a.isLt -- This is a special case of `CharP.cast_eq_zero` that doesn't require typeclass search @[simp high] lemma natCast_self (n : ℕ) [NeZero n] : (n : Fin n) = 0 := by ext; simp @[simp] lemma natCast_eq_zero {a n : ℕ} [NeZero n] : (a : Fin n) = 0 ↔ n ∣ a := by simp [Fin.ext_iff, Nat.dvd_iff_mod_eq_zero] @[simp] theorem natCast_eq_last (n) : (n : Fin (n + 1)) = Fin.last n := by ext; simp theorem le_val_last (i : Fin (n + 1)) : i ≤ n := by rw [Fin.natCast_eq_last] exact Fin.le_last i variable {a b : ℕ} lemma natCast_le_natCast (han : a ≤ n) (hbn : b ≤ n) : (a : Fin (n + 1)) ≤ b ↔ a ≤ b := by rw [← Nat.lt_succ_iff] at han hbn simp [le_iff_val_le_val, -val_fin_le, Nat.mod_eq_of_lt, han, hbn] lemma natCast_lt_natCast (han : a ≤ n) (hbn : b ≤ n) : (a : Fin (n + 1)) < b ↔ a < b := by rw [← Nat.lt_succ_iff] at han hbn; simp [lt_iff_val_lt_val, Nat.mod_eq_of_lt, han, hbn] lemma natCast_mono (hbn : b ≤ n) (hab : a ≤ b) : (a : Fin (n + 1)) ≤ b := (natCast_le_natCast (hab.trans hbn) hbn).2 hab lemma natCast_strictMono (hbn : b ≤ n) (hab : a < b) : (a : Fin (n + 1)) < b := (natCast_lt_natCast (hab.le.trans hbn) hbn).2 hab end OfNatCoe end Add section Succ /-! ### succ and casts into larger Fin types -/ lemma succ_injective (n : ℕ) : Injective (@Fin.succ n) := fun a b ↦ by simp [Fin.ext_iff] /-- `Fin.succ` as an `Embedding` -/ def succEmb (n : ℕ) : Fin n ↪ Fin (n + 1) where toFun := succ inj' := succ_injective _ @[simp] theorem coe_succEmb : ⇑(succEmb n) = Fin.succ := rfl @[deprecated (since := "2025-04-12")] alias val_succEmb := coe_succEmb @[simp] theorem exists_succ_eq {x : Fin (n + 1)} : (∃ y, Fin.succ y = x) ↔ x ≠ 0 := ⟨fun ⟨_, hy⟩ => hy ▸ succ_ne_zero _, x.cases (fun h => h.irrefl.elim) (fun _ _ => ⟨_, rfl⟩)⟩ theorem exists_succ_eq_of_ne_zero {x : Fin (n + 1)} (h : x ≠ 0) : ∃ y, Fin.succ y = x := exists_succ_eq.mpr h @[simp] theorem succ_zero_eq_one' [NeZero n] : Fin.succ (0 : Fin n) = 1 := by cases n · exact (NeZero.ne 0 rfl).elim · rfl theorem one_pos' [NeZero n] : (0 : Fin (n + 1)) < 1 := succ_zero_eq_one' (n := n) ▸ succ_pos _ theorem zero_ne_one' [NeZero n] : (0 : Fin (n + 1)) ≠ 1 := Fin.ne_of_lt one_pos' /-- The `Fin.succ_one_eq_two` in `Lean` only applies in `Fin (n+2)`. This one instead uses a `NeZero n` typeclass hypothesis. -/ @[simp] theorem succ_one_eq_two' [NeZero n] : Fin.succ (1 : Fin (n + 1)) = 2 := by cases n · exact (NeZero.ne 0 rfl).elim · rfl -- Version of `succ_one_eq_two` to be used by `dsimp`. -- Note the `'` swapped around due to a move to std4. /-- The `Fin.le_zero_iff` in `Lean` only applies in `Fin (n+1)`. This one instead uses a `NeZero n` typeclass hypothesis. -/ @[simp] theorem le_zero_iff' {n : ℕ} [NeZero n] {k : Fin n} : k ≤ 0 ↔ k = 0 := ⟨fun h => Fin.ext <| by rw [Nat.eq_zero_of_le_zero h]; rfl, by rintro rfl; exact Nat.le_refl _⟩ -- TODO: Move to Batteries @[simp] lemma castLE_inj {hmn : m ≤ n} {a b : Fin m} : castLE hmn a = castLE hmn b ↔ a = b := by simp [Fin.ext_iff] @[simp] lemma castAdd_inj {a b : Fin m} : castAdd n a = castAdd n b ↔ a = b := by simp [Fin.ext_iff] attribute [simp] castSucc_inj lemma castLE_injective (hmn : m ≤ n) : Injective (castLE hmn) := fun _ _ hab ↦ Fin.ext (congr_arg val hab :) lemma castAdd_injective (m n : ℕ) : Injective (@Fin.castAdd m n) := castLE_injective _ lemma castSucc_injective (n : ℕ) : Injective (@Fin.castSucc n) := castAdd_injective _ _ /-- `Fin.castLE` as an `Embedding`, `castLEEmb h i` embeds `i` into a larger `Fin` type. -/ @[simps apply] def castLEEmb (h : n ≤ m) : Fin n ↪ Fin m where toFun := castLE h inj' := castLE_injective _ @[simp, norm_cast] lemma coe_castLEEmb {m n} (hmn : m ≤ n) : castLEEmb hmn = castLE hmn := rfl /- The next proof can be golfed a lot using `Fintype.card`. It is written this way to define `ENat.card` and `Nat.card` without a `Fintype` dependency (not done yet). -/ lemma nonempty_embedding_iff : Nonempty (Fin n ↪ Fin m) ↔ n ≤ m := by refine ⟨fun h ↦ ?_, fun h ↦ ⟨castLEEmb h⟩⟩ induction n generalizing m with | zero => exact m.zero_le | succ n ihn => obtain ⟨e⟩ := h rcases exists_eq_succ_of_ne_zero (pos_iff_nonempty.2 (Nonempty.map e inferInstance)).ne' with ⟨m, rfl⟩ refine Nat.succ_le_succ <| ihn ⟨?_⟩ refine ⟨fun i ↦ (e.setValue 0 0 i.succ).pred (mt e.setValue_eq_iff.1 i.succ_ne_zero), fun i j h ↦ ?_⟩ simpa only [pred_inj, EmbeddingLike.apply_eq_iff_eq, succ_inj] using h lemma equiv_iff_eq : Nonempty (Fin m ≃ Fin n) ↔ m = n := ⟨fun ⟨e⟩ ↦ le_antisymm (nonempty_embedding_iff.1 ⟨e⟩) (nonempty_embedding_iff.1 ⟨e.symm⟩), fun h ↦ h ▸ ⟨.refl _⟩⟩ @[simp] lemma castLE_castSucc {n m} (i : Fin n) (h : n + 1 ≤ m) : i.castSucc.castLE h = i.castLE (Nat.le_of_succ_le h) := rfl @[simp] lemma castLE_comp_castSucc {n m} (h : n + 1 ≤ m) : Fin.castLE h ∘ Fin.castSucc = Fin.castLE (Nat.le_of_succ_le h) := rfl @[simp] lemma castLE_rfl (n : ℕ) : Fin.castLE (le_refl n) = id := rfl @[simp] theorem range_castLE {n k : ℕ} (h : n ≤ k) : Set.range (castLE h) = { i : Fin k | (i : ℕ) < n } := Set.ext fun x => ⟨fun ⟨y, hy⟩ => hy ▸ y.2, fun hx => ⟨⟨x, hx⟩, rfl⟩⟩ @[simp] theorem coe_of_injective_castLE_symm {n k : ℕ} (h : n ≤ k) (i : Fin k) (hi) : ((Equiv.ofInjective _ (castLE_injective h)).symm ⟨i, hi⟩ : ℕ) = i := by rw [← coe_castLE h] exact congr_arg Fin.val (Equiv.apply_ofInjective_symm _ _) theorem leftInverse_cast (eq : n = m) : LeftInverse (Fin.cast eq.symm) (Fin.cast eq) := fun _ => rfl theorem rightInverse_cast (eq : n = m) : RightInverse (Fin.cast eq.symm) (Fin.cast eq) := fun _ => rfl @[simp] theorem cast_inj (eq : n = m) {a b : Fin n} : a.cast eq = b.cast eq ↔ a = b := by simp [← val_inj] @[simp] theorem cast_lt_cast (eq : n = m) {a b : Fin n} : a.cast eq < b.cast eq ↔ a < b := Iff.rfl @[simp] theorem cast_le_cast (eq : n = m) {a b : Fin n} : a.cast eq ≤ b.cast eq ↔ a ≤ b := Iff.rfl /-- The 'identity' equivalence between `Fin m` and `Fin n` when `m = n`. -/ @[simps] def _root_.finCongr (eq : n = m) : Fin n ≃ Fin m where toFun := Fin.cast eq invFun := Fin.cast eq.symm left_inv := leftInverse_cast eq right_inv := rightInverse_cast eq @[simp] lemma _root_.finCongr_apply_mk (h : m = n) (k : ℕ) (hk : k < m) : finCongr h ⟨k, hk⟩ = ⟨k, h ▸ hk⟩ := rfl @[simp] lemma _root_.finCongr_refl (h : n = n := rfl) : finCongr h = Equiv.refl (Fin n) := by ext; simp @[simp] lemma _root_.finCongr_symm (h : m = n) : (finCongr h).symm = finCongr h.symm := rfl @[simp] lemma _root_.finCongr_apply_coe (h : m = n) (k : Fin m) : (finCongr h k : ℕ) = k := rfl lemma _root_.finCongr_symm_apply_coe (h : m = n) (k : Fin n) : ((finCongr h).symm k : ℕ) = k := rfl /-- While in many cases `finCongr` is better than `Equiv.cast`/`cast`, sometimes we want to apply a generic theorem about `cast`. -/ lemma _root_.finCongr_eq_equivCast (h : n = m) : finCongr h = .cast (h ▸ rfl) := by subst h; simp /-- While in many cases `Fin.cast` is better than `Equiv.cast`/`cast`, sometimes we want to apply a generic theorem about `cast`. -/ theorem cast_eq_cast (h : n = m) : (Fin.cast h : Fin n → Fin m) = _root_.cast (h ▸ rfl) := by subst h ext rfl /-- `Fin.castAdd` as an `Embedding`, `castAddEmb m i` embeds `i : Fin n` in `Fin (n+m)`. See also `Fin.natAddEmb` and `Fin.addNatEmb`. -/ def castAddEmb (m) : Fin n ↪ Fin (n + m) := castLEEmb (le_add_right n m) @[simp] lemma coe_castAddEmb (m) : (castAddEmb m : Fin n → Fin (n + m)) = castAdd m := rfl lemma castAddEmb_apply (m) (i : Fin n) : castAddEmb m i = castAdd m i := rfl /-- `Fin.castSucc` as an `Embedding`, `castSuccEmb i` embeds `i : Fin n` in `Fin (n+1)`. -/ def castSuccEmb : Fin n ↪ Fin (n + 1) := castAddEmb _ @[simp, norm_cast] lemma coe_castSuccEmb : (castSuccEmb : Fin n → Fin (n + 1)) = Fin.castSucc := rfl lemma castSuccEmb_apply (i : Fin n) : castSuccEmb i = i.castSucc := rfl theorem castSucc_le_succ {n} (i : Fin n) : i.castSucc ≤ i.succ := Nat.le_succ i @[simp] theorem castSucc_le_castSucc_iff {a b : Fin n} : castSucc a ≤ castSucc b ↔ a ≤ b := .rfl
@[simp] theorem succ_le_castSucc_iff {a b : Fin n} : succ a ≤ castSucc b ↔ a < b := by rw [le_castSucc_iff, succ_lt_succ_iff]
Mathlib/Data/Fin/Basic.lean
600
602
/- Copyright (c) 2021 Alex Kontorovich and Heather Macbeth and Marc Masdeu. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Alex Kontorovich, Heather Macbeth, Marc Masdeu -/ import Mathlib.Analysis.Complex.Basic import Mathlib.Data.Fintype.Parity import Mathlib.LinearAlgebra.Matrix.GeneralLinearGroup.Defs /-! # The upper half plane and its automorphisms This file defines `UpperHalfPlane` to be the upper half plane in `ℂ`. We furthermore equip it with the structure of a `GLPos 2 ℝ` action by fractional linear transformations. We define the notation `ℍ` for the upper half plane available in the locale `UpperHalfPlane` so as not to conflict with the quaternions. -/ noncomputable section open Matrix Matrix.SpecialLinearGroup open scoped MatrixGroups /-- The open upper half plane, denoted as `ℍ` within the `UpperHalfPlane` namespace -/ def UpperHalfPlane := { point : ℂ // 0 < point.im } @[inherit_doc] scoped[UpperHalfPlane] notation "ℍ" => UpperHalfPlane open UpperHalfPlane namespace UpperHalfPlane /-- The coercion first into an element of `GL(2, ℝ)⁺`, then `GL(2, ℝ)` and finally a 2 × 2 matrix. This notation is scoped in namespace `UpperHalfPlane`. -/ scoped notation:1024 "↑ₘ" A:1024 => (((A : GL(2, ℝ)⁺) : GL (Fin 2) ℝ) : Matrix (Fin 2) (Fin 2) _) instance instCoeFun : CoeFun GL(2, ℝ)⁺ fun _ => Fin 2 → Fin 2 → ℝ where coe A := ↑ₘA /-- The coercion into an element of `GL(2, R)` and finally a 2 × 2 matrix over `R`. This is similar to `↑ₘ`, but without positivity requirements, and allows the user to specify the ring `R`, which can be useful to help Lean elaborate correctly. This notation is scoped in namespace `UpperHalfPlane`. -/ scoped notation:1024 "↑ₘ[" R "]" A:1024 => ((A : GL (Fin 2) R) : Matrix (Fin 2) (Fin 2) R) /-- Canonical embedding of the upper half-plane into `ℂ`. -/ @[coe] protected def coe (z : ℍ) : ℂ := z.1 instance : CoeOut ℍ ℂ := ⟨UpperHalfPlane.coe⟩ instance : Inhabited ℍ := ⟨⟨Complex.I, by simp⟩⟩ @[ext] theorem ext {a b : ℍ} (h : (a : ℂ) = b) : a = b := Subtype.eq h @[simp, norm_cast] theorem ext_iff' {a b : ℍ} : (a : ℂ) = b ↔ a = b := UpperHalfPlane.ext_iff.symm instance canLift : CanLift ℂ ℍ ((↑) : ℍ → ℂ) fun z => 0 < z.im := Subtype.canLift fun (z : ℂ) => 0 < z.im /-- Imaginary part -/ def im (z : ℍ) := (z : ℂ).im /-- Real part -/ def re (z : ℍ) := (z : ℂ).re /-- Extensionality lemma in terms of `UpperHalfPlane.re` and `UpperHalfPlane.im`. -/ theorem ext' {a b : ℍ} (hre : a.re = b.re) (him : a.im = b.im) : a = b := ext <| Complex.ext hre him /-- Constructor for `UpperHalfPlane`. It is useful if `⟨z, h⟩` makes Lean use a wrong typeclass instance. -/ def mk (z : ℂ) (h : 0 < z.im) : ℍ := ⟨z, h⟩ @[simp] theorem coe_im (z : ℍ) : (z : ℂ).im = z.im := rfl @[simp] theorem coe_re (z : ℍ) : (z : ℂ).re = z.re := rfl @[simp] theorem mk_re (z : ℂ) (h : 0 < z.im) : (mk z h).re = z.re := rfl @[simp] theorem mk_im (z : ℂ) (h : 0 < z.im) : (mk z h).im = z.im := rfl @[simp] theorem coe_mk (z : ℂ) (h : 0 < z.im) : (mk z h : ℂ) = z := rfl @[simp] lemma coe_mk_subtype {z : ℂ} (hz : 0 < z.im) : UpperHalfPlane.coe ⟨z, hz⟩ = z := by rfl @[simp] theorem mk_coe (z : ℍ) (h : 0 < (z : ℂ).im := z.2) : mk z h = z := rfl theorem re_add_im (z : ℍ) : (z.re + z.im * Complex.I : ℂ) = z := Complex.re_add_im z theorem im_pos (z : ℍ) : 0 < z.im := z.2 theorem im_ne_zero (z : ℍ) : z.im ≠ 0 := z.im_pos.ne' theorem ne_zero (z : ℍ) : (z : ℂ) ≠ 0 := mt (congr_arg Complex.im) z.im_ne_zero /-- Define I := √-1 as an element on the upper half plane. -/ def I : ℍ := ⟨Complex.I, by simp⟩ @[simp] lemma I_im : I.im = 1 := rfl @[simp] lemma I_re : I.re = 0 := rfl @[simp, norm_cast] lemma coe_I : I = Complex.I := rfl end UpperHalfPlane namespace Mathlib.Meta.Positivity open Lean Meta Qq /-- Extension for the `positivity` tactic: `UpperHalfPlane.im`. -/ @[positivity UpperHalfPlane.im _] def evalUpperHalfPlaneIm : PositivityExt where eval {u α} _zα _pα e := do match u, α, e with | 0, ~q(ℝ), ~q(UpperHalfPlane.im $a) => assertInstancesCommute pure (.positive q(@UpperHalfPlane.im_pos $a)) | _, _, _ => throwError "not UpperHalfPlane.im" /-- Extension for the `positivity` tactic: `UpperHalfPlane.coe`. -/ @[positivity UpperHalfPlane.coe _] def evalUpperHalfPlaneCoe : PositivityExt where eval {u α} _zα _pα e := do match u, α, e with | 0, ~q(ℂ), ~q(UpperHalfPlane.coe $a) => assertInstancesCommute pure (.nonzero q(@UpperHalfPlane.ne_zero $a)) | _, _, _ => throwError "not UpperHalfPlane.coe" end Mathlib.Meta.Positivity namespace UpperHalfPlane theorem normSq_pos (z : ℍ) : 0 < Complex.normSq (z : ℂ) := by rw [Complex.normSq_pos]; exact z.ne_zero theorem normSq_ne_zero (z : ℍ) : Complex.normSq (z : ℂ) ≠ 0 := (normSq_pos z).ne' theorem im_inv_neg_coe_pos (z : ℍ) : 0 < (-z : ℂ)⁻¹.im := by simpa [neg_div] using div_pos z.property (normSq_pos z) lemma ne_nat (z : ℍ) : ∀ n : ℕ, z.1 ≠ n := by intro n have h1 := z.2 aesop lemma ne_int (z : ℍ) : ∀ n : ℤ, z.1 ≠ n := by intro n have h1 := z.2 aesop /-- Numerator of the formula for a fractional linear transformation -/ def num (g : GL(2, ℝ)⁺) (z : ℍ) : ℂ := g 0 0 * z + g 0 1 /-- Denominator of the formula for a fractional linear transformation -/ def denom (g : GL(2, ℝ)⁺) (z : ℍ) : ℂ := g 1 0 * z + g 1 1 theorem linear_ne_zero (cd : Fin 2 → ℝ) (z : ℍ) (h : cd ≠ 0) : (cd 0 : ℂ) * z + cd 1 ≠ 0 := by contrapose! h have : cd 0 = 0 := by -- we will need this twice apply_fun Complex.im at h simpa only [z.im_ne_zero, Complex.add_im, add_zero, coe_im, zero_mul, or_false, Complex.ofReal_im, Complex.zero_im, Complex.mul_im, mul_eq_zero] using h simp only [this, zero_mul, Complex.ofReal_zero, zero_add, Complex.ofReal_eq_zero] at h ext i fin_cases i <;> assumption theorem denom_ne_zero (g : GL(2, ℝ)⁺) (z : ℍ) : denom g z ≠ 0 := by intro H have DET := (mem_glpos _).1 g.prop simp only [GeneralLinearGroup.val_det_apply] at DET obtain hg | hz : g 1 0 = 0 ∨ z.im = 0 := by simpa [num, denom] using congr_arg Complex.im H · simp only [hg, Complex.ofReal_zero, denom, zero_mul, zero_add, Complex.ofReal_eq_zero] at H simp only [Matrix.det_fin_two g.1.1, H, hg, mul_zero, sub_zero, lt_self_iff_false] at DET · exact z.prop.ne' hz theorem normSq_denom_pos (g : GL(2, ℝ)⁺) (z : ℍ) : 0 < Complex.normSq (denom g z) := Complex.normSq_pos.mpr (denom_ne_zero g z) theorem normSq_denom_ne_zero (g : GL(2, ℝ)⁺) (z : ℍ) : Complex.normSq (denom g z) ≠ 0 := ne_of_gt (normSq_denom_pos g z) /-- Fractional linear transformation, also known as the Moebius transformation -/ def smulAux' (g : GL(2, ℝ)⁺) (z : ℍ) : ℂ := num g z / denom g z theorem smulAux'_im (g : GL(2, ℝ)⁺) (z : ℍ) : (smulAux' g z).im = det ↑ₘg * z.im / Complex.normSq (denom g z) := by simp only [smulAux', num, denom, Complex.div_im, Complex.add_im, Complex.mul_im, Complex.ofReal_re, coe_im, Complex.ofReal_im, coe_re, zero_mul, add_zero, Complex.add_re, Complex.mul_re, sub_zero, ← sub_div, g.1.1.det_fin_two] ring /-- Fractional linear transformation, also known as the Moebius transformation -/ def smulAux (g : GL(2, ℝ)⁺) (z : ℍ) : ℍ := mk (smulAux' g z) <| by rw [smulAux'_im] convert mul_pos ((mem_glpos _).1 g.prop) (div_pos z.im_pos (Complex.normSq_pos.mpr (denom_ne_zero g z))) using 1 simp only [GeneralLinearGroup.val_det_apply] ring theorem denom_cocycle (x y : GL(2, ℝ)⁺) (z : ℍ) : denom (x * y) z = denom x (smulAux y z) * denom y z := by change _ = (_ * (_ / _) + _) * _ field_simp [denom_ne_zero] simp only [denom, Subgroup.coe_mul, Fin.isValue, Units.val_mul, mul_apply, Fin.sum_univ_succ, Finset.univ_unique, Fin.default_eq_zero, Finset.sum_singleton, Fin.succ_zero_eq_one, Complex.ofReal_add, Complex.ofReal_mul, num] ring theorem mul_smul' (x y : GL(2, ℝ)⁺) (z : ℍ) : smulAux (x * y) z = smulAux x (smulAux y z) := by ext1 change _ / _ = (_ * (_ / _) + _) / _ rw [denom_cocycle] field_simp [denom_ne_zero] simp only [num, Subgroup.coe_mul, Fin.isValue, Units.val_mul, mul_apply, Fin.sum_univ_succ, Finset.univ_unique, Fin.default_eq_zero, Finset.sum_singleton, Fin.succ_zero_eq_one, Complex.ofReal_add, Complex.ofReal_mul, denom] ring /-- The action of `GLPos 2 ℝ` on the upper half-plane by fractional linear transformations. -/ instance : MulAction GL(2, ℝ)⁺ ℍ where smul := smulAux one_smul z := by ext1 change _ / _ = _ simp [num, denom] mul_smul := mul_smul' instance SLAction {R : Type*} [CommRing R] [Algebra R ℝ] : MulAction SL(2, R) ℍ := MulAction.compHom ℍ <| SpecialLinearGroup.toGLPos.comp <| map (algebraMap R ℝ) -- Porting note: in the statement, we used to have coercions `↑· : ℝ` -- rather than `algebraMap R ℝ ·`. theorem specialLinearGroup_apply {R : Type*} [CommRing R] [Algebra R ℝ] (g : SL(2, R)) (z : ℍ) : g • z = mk (((algebraMap R ℝ (g 0 0) : ℂ) * z + (algebraMap R ℝ (g 0 1) : ℂ)) / ((algebraMap R ℝ (g 1 0) : ℂ) * z + (algebraMap R ℝ (g 1 1) : ℂ))) (g • z).property := rfl variable (g : GL(2, ℝ)⁺) (z : ℍ) @[simp] theorem coe_smul : ↑(g • z) = num g z / denom g z := rfl @[simp] theorem re_smul : (g • z).re = (num g z / denom g z).re := rfl theorem im_smul : (g • z).im = (num g z / denom g z).im := rfl theorem im_smul_eq_div_normSq : (g • z).im = det ↑ₘg * z.im / Complex.normSq (denom g z) := smulAux'_im g z theorem c_mul_im_sq_le_normSq_denom : (g 1 0 * z.im) ^ 2 ≤ Complex.normSq (denom g z) := by set c := g 1 0 set d := g 1 1 calc (c * z.im) ^ 2 ≤ (c * z.im) ^ 2 + (c * z.re + d) ^ 2 := by nlinarith _ = Complex.normSq (denom g z) := by dsimp [c, d, denom, Complex.normSq]; ring @[simp] theorem neg_smul : -g • z = g • z := by ext1 change _ / _ = _ / _ field_simp [denom_ne_zero] simp only [num, denom, Complex.ofReal_neg, neg_mul, GLPos.coe_neg_GL, Units.val_neg, neg_apply] ring_nf lemma denom_one : denom 1 z = 1 := by simp [denom] section PosRealAction instance posRealAction : MulAction { x : ℝ // 0 < x } ℍ where smul x z := mk ((x : ℝ) • (z : ℂ)) <| by simpa using mul_pos x.2 z.2 one_smul _ := Subtype.ext <| one_smul _ _ mul_smul x y z := Subtype.ext <| mul_smul (x : ℝ) y (z : ℂ) variable (x : { x : ℝ // 0 < x }) (z : ℍ) @[simp] theorem coe_pos_real_smul : ↑(x • z) = (x : ℝ) • (z : ℂ) := rfl @[simp] theorem pos_real_im : (x • z).im = x * z.im := Complex.smul_im _ _ @[simp] theorem pos_real_re : (x • z).re = x * z.re := Complex.smul_re _ _ end PosRealAction section RealAddAction instance : AddAction ℝ ℍ where vadd x z := mk (x + z) <| by simpa using z.im_pos zero_vadd _ := Subtype.ext <| by simp [HVAdd.hVAdd] add_vadd x y z := Subtype.ext <| by simp [HVAdd.hVAdd, add_assoc] variable (x : ℝ) (z : ℍ) @[simp] theorem coe_vadd : ↑(x +ᵥ z) = (x + z : ℂ) := rfl @[simp] theorem vadd_re : (x +ᵥ z).re = x + z.re := rfl @[simp] theorem vadd_im : (x +ᵥ z).im = z.im := zero_add _ end RealAddAction /- these next few lemmas are *not* flagged `@simp` because of the constructors on the RHS; instead we use the versions with coercions to `ℂ` as simp lemmas instead. -/ theorem modular_S_smul (z : ℍ) : ModularGroup.S • z = mk (-z : ℂ)⁻¹ z.im_inv_neg_coe_pos := by rw [specialLinearGroup_apply]; simp [ModularGroup.S, neg_div, inv_neg, toGL] theorem modular_T_zpow_smul (z : ℍ) (n : ℤ) : ModularGroup.T ^ n • z = (n : ℝ) +ᵥ z := by rw [UpperHalfPlane.ext_iff, coe_vadd, add_comm, specialLinearGroup_apply, coe_mk] simp [toGL, ModularGroup.coe_T_zpow, of_apply, cons_val_zero, algebraMap.coe_one, Complex.ofReal_one, one_mul, cons_val_one, head_cons, algebraMap.coe_zero, zero_mul, zero_add, div_one] theorem modular_T_smul (z : ℍ) : ModularGroup.T • z = (1 : ℝ) +ᵥ z := by simpa only [Int.cast_one] using modular_T_zpow_smul z 1 theorem exists_SL2_smul_eq_of_apply_zero_one_eq_zero (g : SL(2, ℝ)) (hc : g 1 0 = 0) : ∃ (u : { x : ℝ // 0 < x }) (v : ℝ), (g • · : ℍ → ℍ) = (v +ᵥ ·) ∘ (u • ·) := by obtain ⟨a, b, ha, rfl⟩ := g.fin_two_exists_eq_mk_of_apply_zero_one_eq_zero hc refine ⟨⟨_, mul_self_pos.mpr ha⟩, b * a, ?_⟩ ext1 ⟨z, hz⟩; ext1 suffices ↑a * z * a + b * a = b * a + a * a * z by simpa [toGL, specialLinearGroup_apply, add_mul] ring theorem exists_SL2_smul_eq_of_apply_zero_one_ne_zero (g : SL(2, ℝ)) (hc : g 1 0 ≠ 0) : ∃ (u : { x : ℝ // 0 < x }) (v w : ℝ), (g • · : ℍ → ℍ) = (w +ᵥ ·) ∘ (ModularGroup.S • · : ℍ → ℍ) ∘ (v +ᵥ · : ℍ → ℍ) ∘ (u • · : ℍ → ℍ) := by have h_denom := denom_ne_zero g induction g using Matrix.SpecialLinearGroup.fin_two_induction with | _ a b c d h => ?_ replace hc : c ≠ 0 := by simpa using hc refine ⟨⟨_, mul_self_pos.mpr hc⟩, c * d, a / c, ?_⟩ ext1 ⟨z, hz⟩; ext1 suffices (↑a * z + b) / (↑c * z + d) = a / c - (c * d + ↑c * ↑c * z)⁻¹ by simpa only [modular_S_smul, inv_neg, Function.comp_apply, coe_vadd, Complex.ofReal_mul, coe_pos_real_smul, Complex.real_smul, Complex.ofReal_div, coe_mk] replace hc : (c : ℂ) ≠ 0 := by norm_cast replace h_denom : ↑c * z + d ≠ 0 := by simpa using h_denom ⟨z, hz⟩ have h_aux : (c : ℂ) * d + ↑c * ↑c * z ≠ 0 := by rw [mul_assoc, ← mul_add, add_comm] exact mul_ne_zero hc h_denom replace h : (a * d - b * c : ℂ) = (1 : ℂ) := by norm_cast field_simp linear_combination (-(z * (c : ℂ) ^ 2) - c * d) * h end UpperHalfPlane namespace ModularGroup -- results specific to `SL(2, ℤ)` section ModularScalarTowers /-- Canonical embedding of `SL(2, ℤ)` into `GL(2, ℝ)⁺`. -/ @[coe] def coe (g : SL(2, ℤ)) : GL(2, ℝ)⁺ := ((g : SL(2, ℝ)) : GL(2, ℝ)⁺) @[deprecated (since := "2024-11-19")] noncomputable alias coe' := coe instance : Coe SL(2, ℤ) GL(2, ℝ)⁺ := ⟨coe⟩ @[simp] theorem coe_apply_complex {g : SL(2, ℤ)} {i j : Fin 2} : (Units.val <| Subtype.val <| coe g) i j = (Subtype.val g i j : ℂ) := rfl @[deprecated (since := "2024-11-19")] alias coe'_apply_complex := coe_apply_complex @[simp] theorem det_coe {g : SL(2, ℤ)} : det (Units.val <| Subtype.val <| coe g) = 1 := by simp only [SpecialLinearGroup.coe_GLPos_coe_GL_coe_matrix, SpecialLinearGroup.det_coe, coe] @[deprecated (since := "2024-11-19")] alias det_coe' := det_coe lemma coe_one : coe 1 = 1 := by simp only [coe, map_one] instance SLOnGLPos : SMul SL(2, ℤ) GL(2, ℝ)⁺ := ⟨fun s g => s * g⟩ theorem SLOnGLPos_smul_apply (s : SL(2, ℤ)) (g : GL(2, ℝ)⁺) (z : ℍ) : (s • g) • z = ((s : GL(2, ℝ)⁺) * g) • z := rfl instance SL_to_GL_tower : IsScalarTower SL(2, ℤ) GL(2, ℝ)⁺ ℍ where smul_assoc s g z := by simp only [SLOnGLPos_smul_apply] apply mul_smul' end ModularScalarTowers section SLModularAction variable (g : SL(2, ℤ)) (z : ℍ) @[simp] theorem sl_moeb (A : SL(2, ℤ)) (z : ℍ) : A • z = (A : GL(2, ℝ)⁺) • z := rfl @[simp high] theorem SL_neg_smul (g : SL(2, ℤ)) (z : ℍ) : -g • z = g • z := by simp only [coe_GLPos_neg, sl_moeb, coe_int_neg, neg_smul, coe] theorem im_smul_eq_div_normSq : (g • z).im = z.im / Complex.normSq (denom g z) := by simpa only [coe, coe_GLPos_coe_GL_coe_matrix, (g : SL(2, ℝ)).prop, one_mul] using z.im_smul_eq_div_normSq g theorem denom_apply (g : SL(2, ℤ)) (z : ℍ) : denom g z = g 1 0 * z + g 1 1 := by simp [denom, coe] @[simp] lemma denom_S (z : ℍ) : denom S z = z := by simp only [S, denom_apply, of_apply, cons_val', cons_val_zero, empty_val', cons_val_fin_one, cons_val_one, head_fin_const, Int.cast_one, one_mul, head_cons, Int.cast_zero, add_zero] end SLModularAction end ModularGroup
Mathlib/Analysis/Complex/UpperHalfPlane/Basic.lean
515
516
/- Copyright (c) 2019 Neil Strickland. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Neil Strickland -/ import Mathlib.Algebra.BigOperators.Group.Multiset.Basic import Mathlib.Data.PNat.Prime import Mathlib.Data.Nat.Factors import Mathlib.Data.Multiset.OrderedMonoid import Mathlib.Data.Multiset.Sort /-! # Prime factors of nonzero naturals This file defines the factorization of a nonzero natural number `n` as a multiset of primes, the multiplicity of `p` in this factors multiset being the p-adic valuation of `n`. ## Main declarations * `PrimeMultiset`: Type of multisets of prime numbers. * `FactorMultiset n`: Multiset of prime factors of `n`. -/ /-- The type of multisets of prime numbers. Unique factorization gives an equivalence between this set and ℕ+, as we will formalize below. -/ def PrimeMultiset := Multiset Nat.Primes deriving Inhabited, AddCommMonoid, DistribLattice, SemilatticeSup, Sub -- The `CanonicallyOrderedAdd, OrderBot, OrderedSub` instances should be constructed by a deriving -- handler. -- https://github.com/leanprover-community/mathlib4/issues/380 instance : IsOrderedCancelAddMonoid PrimeMultiset := inferInstanceAs (IsOrderedCancelAddMonoid (Multiset Nat.Primes)) instance : CanonicallyOrderedAdd PrimeMultiset := inferInstanceAs (CanonicallyOrderedAdd (Multiset Nat.Primes)) instance : OrderBot PrimeMultiset := inferInstanceAs (OrderBot (Multiset Nat.Primes)) instance : OrderedSub PrimeMultiset := inferInstanceAs (OrderedSub (Multiset Nat.Primes)) namespace PrimeMultiset -- `@[derive]` doesn't work for `meta` instances unsafe instance : Repr PrimeMultiset := by delta PrimeMultiset; infer_instance /-- The multiset consisting of a single prime -/ def ofPrime (p : Nat.Primes) : PrimeMultiset := ({p} : Multiset Nat.Primes) theorem card_ofPrime (p : Nat.Primes) : Multiset.card (ofPrime p) = 1 := rfl /-- We can forget the primality property and regard a multiset of primes as just a multiset of positive integers, or a multiset of natural numbers. In the opposite direction, if we have a multiset of positive integers or natural numbers, together with a proof that all the elements are prime, then we can regard it as a multiset of primes. The next block of results records obvious properties of these coercions. -/ def toNatMultiset : PrimeMultiset → Multiset ℕ := fun v => v.map (↑) instance coeNat : Coe PrimeMultiset (Multiset ℕ) := ⟨toNatMultiset⟩ /-- `PrimeMultiset.coe`, the coercion from a multiset of primes to a multiset of naturals, promoted to an `AddMonoidHom`. -/ def coeNatMonoidHom : PrimeMultiset →+ Multiset ℕ := Multiset.mapAddMonoidHom (↑) @[simp] theorem coe_coeNatMonoidHom : (coeNatMonoidHom : PrimeMultiset → Multiset ℕ) = (↑) := rfl theorem coeNat_injective : Function.Injective ((↑) : PrimeMultiset → Multiset ℕ) := Multiset.map_injective Nat.Primes.coe_nat_injective theorem coeNat_ofPrime (p : Nat.Primes) : (ofPrime p : Multiset ℕ) = {(p : ℕ)} := rfl theorem coeNat_prime (v : PrimeMultiset) (p : ℕ) (h : p ∈ (v : Multiset ℕ)) : p.Prime := by rcases Multiset.mem_map.mp h with ⟨⟨_, hp'⟩, ⟨_, h_eq⟩⟩ exact h_eq ▸ hp' /-- Converts a `PrimeMultiset` to a `Multiset ℕ+`. -/ def toPNatMultiset : PrimeMultiset → Multiset ℕ+ := fun v => v.map (↑) instance coePNat : Coe PrimeMultiset (Multiset ℕ+) := ⟨toPNatMultiset⟩ /-- `coePNat`, the coercion from a multiset of primes to a multiset of positive naturals, regarded as an `AddMonoidHom`. -/ def coePNatMonoidHom : PrimeMultiset →+ Multiset ℕ+ := Multiset.mapAddMonoidHom (↑) @[simp] theorem coe_coePNatMonoidHom : (coePNatMonoidHom : PrimeMultiset → Multiset ℕ+) = (↑) := rfl theorem coePNat_injective : Function.Injective ((↑) : PrimeMultiset → Multiset ℕ+) := Multiset.map_injective Nat.Primes.coe_pnat_injective theorem coePNat_ofPrime (p : Nat.Primes) : (ofPrime p : Multiset ℕ+) = {(p : ℕ+)} := rfl theorem coePNat_prime (v : PrimeMultiset) (p : ℕ+) (h : p ∈ (v : Multiset ℕ+)) : p.Prime := by rcases Multiset.mem_map.mp h with ⟨⟨_, hp'⟩, ⟨_, h_eq⟩⟩ exact h_eq ▸ hp' instance coeMultisetPNatNat : Coe (Multiset ℕ+) (Multiset ℕ) := ⟨fun v => v.map (↑)⟩ theorem coePNat_nat (v : PrimeMultiset) : ((v : Multiset ℕ+) : Multiset ℕ) = (v : Multiset ℕ) := by change (v.map ((↑) : Nat.Primes → ℕ+)).map Subtype.val = v.map Subtype.val rw [Multiset.map_map] congr /-- The product of a `PrimeMultiset`, as a `ℕ+`. -/ def prod (v : PrimeMultiset) : ℕ+ := (v : Multiset PNat).prod theorem coe_prod (v : PrimeMultiset) : (v.prod : ℕ) = (v : Multiset ℕ).prod := by have h : (v.prod : ℕ) = ((v.map (↑) : Multiset ℕ+).map (↑)).prod := PNat.coeMonoidHom.map_multiset_prod v.toPNatMultiset simpa [Multiset.map_map] using h theorem prod_ofPrime (p : Nat.Primes) : (ofPrime p).prod = (p : ℕ+) := Multiset.prod_singleton _ /-- If a `Multiset ℕ` consists only of primes, it can be recast as a `PrimeMultiset`. -/ def ofNatMultiset (v : Multiset ℕ) (h : ∀ p : ℕ, p ∈ v → p.Prime) : PrimeMultiset := @Multiset.pmap ℕ Nat.Primes Nat.Prime (fun p hp => ⟨p, hp⟩) v h theorem to_ofNatMultiset (v : Multiset ℕ) (h) : (ofNatMultiset v h : Multiset ℕ) = v := by dsimp [ofNatMultiset, toNatMultiset] rw [Multiset.map_pmap, Multiset.pmap_eq_map, Multiset.map_id'] theorem prod_ofNatMultiset (v : Multiset ℕ) (h) : ((ofNatMultiset v h).prod : ℕ) = (v.prod : ℕ) := by rw [coe_prod, to_ofNatMultiset] /-- If a `Multiset ℕ+` consists only of primes, it can be recast as a `PrimeMultiset`. -/ def ofPNatMultiset (v : Multiset ℕ+) (h : ∀ p : ℕ+, p ∈ v → p.Prime) : PrimeMultiset := @Multiset.pmap ℕ+ Nat.Primes PNat.Prime (fun p hp => ⟨(p : ℕ), hp⟩) v h theorem to_ofPNatMultiset (v : Multiset ℕ+) (h) : (ofPNatMultiset v h : Multiset ℕ+) = v := by dsimp [ofPNatMultiset, toPNatMultiset] have : (fun (p : ℕ+) (h : p.Prime) => ((↑) : Nat.Primes → ℕ+) ⟨p, h⟩) = fun p _ => id p := by funext p h apply Subtype.eq rfl rw [Multiset.map_pmap, this, Multiset.pmap_eq_map, Multiset.map_id] theorem prod_ofPNatMultiset (v : Multiset ℕ+) (h) : ((ofPNatMultiset v h).prod : ℕ+) = v.prod := by dsimp [prod] rw [to_ofPNatMultiset] /-- Lists can be coerced to multisets; here we have some results about how this interacts with our constructions on multisets. -/ def ofNatList (l : List ℕ) (h : ∀ p : ℕ, p ∈ l → p.Prime) : PrimeMultiset := ofNatMultiset (l : Multiset ℕ) h theorem prod_ofNatList (l : List ℕ) (h) : ((ofNatList l h).prod : ℕ) = l.prod := by have := prod_ofNatMultiset (l : Multiset ℕ) h rw [Multiset.prod_coe] at this exact this /-- If a `List ℕ+` consists only of primes, it can be recast as a `PrimeMultiset` with the coercion from lists to multisets. -/ def ofPNatList (l : List ℕ+) (h : ∀ p : ℕ+, p ∈ l → p.Prime) : PrimeMultiset := ofPNatMultiset (l : Multiset ℕ+) h theorem prod_ofPNatList (l : List ℕ+) (h) : (ofPNatList l h).prod = l.prod := by have := prod_ofPNatMultiset (l : Multiset ℕ+) h rw [Multiset.prod_coe] at this exact this /-- The product map gives a homomorphism from the additive monoid of multisets to the multiplicative monoid ℕ+. -/ theorem prod_zero : (0 : PrimeMultiset).prod = 1 := by exact Multiset.prod_zero theorem prod_add (u v : PrimeMultiset) : (u + v).prod = u.prod * v.prod := by change (coePNatMonoidHom (u + v)).prod = _ rw [coePNatMonoidHom.map_add] exact Multiset.prod_add _ _ theorem prod_smul (d : ℕ) (u : PrimeMultiset) : (d • u).prod = u.prod ^ d := by induction d with | zero => simp only [zero_nsmul, pow_zero, prod_zero] | succ n ih => rw [succ_nsmul, prod_add, ih, pow_succ] end PrimeMultiset namespace PNat /-- The prime factors of n, regarded as a multiset -/ def factorMultiset (n : ℕ+) : PrimeMultiset := PrimeMultiset.ofNatList (Nat.primeFactorsList n) (@Nat.prime_of_mem_primeFactorsList n) /-- The product of the factors is the original number -/ theorem prod_factorMultiset (n : ℕ+) : (factorMultiset n).prod = n := eq <| by dsimp [factorMultiset] rw [PrimeMultiset.prod_ofNatList] exact Nat.prod_primeFactorsList n.ne_zero theorem coeNat_factorMultiset (n : ℕ+) : (factorMultiset n : Multiset ℕ) = (Nat.primeFactorsList n : Multiset ℕ) := PrimeMultiset.to_ofNatMultiset (Nat.primeFactorsList n) (@Nat.prime_of_mem_primeFactorsList n) end PNat namespace PrimeMultiset /-- If we start with a multiset of primes, take the product and then factor it, we get back the original multiset. -/ theorem factorMultiset_prod (v : PrimeMultiset) : v.prod.factorMultiset = v := by apply PrimeMultiset.coeNat_injective rw [v.prod.coeNat_factorMultiset, PrimeMultiset.coe_prod] rcases v with ⟨l⟩ dsimp [PrimeMultiset.toNatMultiset] let l' := l.map ((↑) : Nat.Primes → ℕ) have (p : ℕ) (hp : p ∈ l') : p.Prime := by simp only [List.map_subtype, List.map_id_fun', id_eq, List.mem_unattach, l'] at hp obtain ⟨hp', -⟩ := hp exact hp' exact Multiset.coe_eq_coe.mpr (@Nat.primeFactorsList_unique _ l' rfl this).symm end PrimeMultiset namespace PNat /-- Positive integers biject with multisets of primes. -/ def factorMultisetEquiv : ℕ+ ≃ PrimeMultiset where toFun := factorMultiset invFun := PrimeMultiset.prod left_inv := prod_factorMultiset right_inv := PrimeMultiset.factorMultiset_prod /-- Factoring gives a homomorphism from the multiplicative monoid ℕ+ to the additive monoid of multisets. -/ theorem factorMultiset_one : factorMultiset 1 = 0 := by simp [factorMultiset, PrimeMultiset.ofNatList, PrimeMultiset.ofNatMultiset] theorem factorMultiset_mul (n m : ℕ+) : factorMultiset (n * m) = factorMultiset n + factorMultiset m := by let u := factorMultiset n let v := factorMultiset m have : n = u.prod := (prod_factorMultiset n).symm; rw [this] have : m = v.prod := (prod_factorMultiset m).symm; rw [this] rw [← PrimeMultiset.prod_add] repeat' rw [PrimeMultiset.factorMultiset_prod] theorem factorMultiset_pow (n : ℕ+) (m : ℕ) : factorMultiset (n ^ m) = m • factorMultiset n := by let u := factorMultiset n have : n = u.prod := (prod_factorMultiset n).symm rw [this, ← PrimeMultiset.prod_smul] repeat' rw [PrimeMultiset.factorMultiset_prod] /-- Factoring a prime gives the corresponding one-element multiset. -/ theorem factorMultiset_ofPrime (p : Nat.Primes) : (p : ℕ+).factorMultiset = PrimeMultiset.ofPrime p := by apply factorMultisetEquiv.symm.injective change (p : ℕ+).factorMultiset.prod = (PrimeMultiset.ofPrime p).prod rw [(p : ℕ+).prod_factorMultiset, PrimeMultiset.prod_ofPrime] /-- We now have four different results that all encode the idea that inequality of multisets corresponds to divisibility of positive integers. -/ theorem factorMultiset_le_iff {m n : ℕ+} : factorMultiset m ≤ factorMultiset n ↔ m ∣ n := by constructor · intro h rw [← prod_factorMultiset m, ← prod_factorMultiset m] apply Dvd.intro (n.factorMultiset - m.factorMultiset).prod rw [← PrimeMultiset.prod_add, PrimeMultiset.factorMultiset_prod, add_tsub_cancel_of_le h, prod_factorMultiset] · intro h rw [← mul_div_exact h, factorMultiset_mul] exact le_self_add theorem factorMultiset_le_iff' {m : ℕ+} {v : PrimeMultiset} : factorMultiset m ≤ v ↔ m ∣ v.prod := by let h := @factorMultiset_le_iff m v.prod rw [v.factorMultiset_prod] at h exact h end PNat namespace PrimeMultiset theorem prod_dvd_iff {u v : PrimeMultiset} : u.prod ∣ v.prod ↔ u ≤ v := by let h := @PNat.factorMultiset_le_iff' u.prod v rw [u.factorMultiset_prod] at h exact h.symm theorem prod_dvd_iff' {u : PrimeMultiset} {n : ℕ+} : u.prod ∣ n ↔ u ≤ n.factorMultiset := by let h := @prod_dvd_iff u n.factorMultiset rw [n.prod_factorMultiset] at h exact h end PrimeMultiset namespace PNat /-- The gcd and lcm operations on positive integers correspond to the inf and sup operations on multisets. -/ theorem factorMultiset_gcd (m n : ℕ+) : factorMultiset (gcd m n) = factorMultiset m ⊓ factorMultiset n := by apply le_antisymm · apply le_inf_iff.mpr; constructor <;> apply factorMultiset_le_iff.mpr · exact gcd_dvd_left m n · exact gcd_dvd_right m n · rw [← PrimeMultiset.prod_dvd_iff, prod_factorMultiset] apply dvd_gcd <;> rw [PrimeMultiset.prod_dvd_iff'] · exact inf_le_left · exact inf_le_right theorem factorMultiset_lcm (m n : ℕ+) : factorMultiset (lcm m n) = factorMultiset m ⊔ factorMultiset n := by apply le_antisymm · rw [← PrimeMultiset.prod_dvd_iff, prod_factorMultiset] apply lcm_dvd <;> rw [← factorMultiset_le_iff'] · exact le_sup_left · exact le_sup_right · apply sup_le_iff.mpr; constructor <;> apply factorMultiset_le_iff.mpr · exact dvd_lcm_left m n · exact dvd_lcm_right m n /-- The number of occurrences of p in the factor multiset of m is the same as the p-adic valuation of m. -/ theorem count_factorMultiset (m : ℕ+) (p : Nat.Primes) (k : ℕ) : (p : ℕ+) ^ k ∣ m ↔ k ≤ m.factorMultiset.count p := by rw [Multiset.le_count_iff_replicate_le, ← factorMultiset_le_iff, factorMultiset_pow, factorMultiset_ofPrime] congr! 2 apply Multiset.eq_replicate.mpr constructor · rw [Multiset.card_nsmul, PrimeMultiset.card_ofPrime, mul_one] · intro q h rw [PrimeMultiset.ofPrime, Multiset.nsmul_singleton _ k] at h exact Multiset.eq_of_mem_replicate h end PNat namespace PrimeMultiset theorem prod_inf (u v : PrimeMultiset) : (u ⊓ v).prod = PNat.gcd u.prod v.prod := by let n := u.prod let m := v.prod change (u ⊓ v).prod = PNat.gcd n m have : u = n.factorMultiset := u.factorMultiset_prod.symm; rw [this] have : v = m.factorMultiset := v.factorMultiset_prod.symm; rw [this] rw [← PNat.factorMultiset_gcd n m, PNat.prod_factorMultiset] theorem prod_sup (u v : PrimeMultiset) : (u ⊔ v).prod = PNat.lcm u.prod v.prod := by let n := u.prod let m := v.prod change (u ⊔ v).prod = PNat.lcm n m have : u = n.factorMultiset := u.factorMultiset_prod.symm; rw [this] have : v = m.factorMultiset := v.factorMultiset_prod.symm; rw [this] rw [← PNat.factorMultiset_lcm n m, PNat.prod_factorMultiset] end PrimeMultiset
Mathlib/Data/PNat/Factors.lean
388
398
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Kim Morrison, Jens Wagemaker, Johan Commelin -/ import Mathlib.Algebra.Polynomial.BigOperators import Mathlib.Algebra.Polynomial.RingDivision import Mathlib.Data.Set.Finite.Lemmas import Mathlib.RingTheory.Coprime.Lemmas import Mathlib.RingTheory.Localization.FractionRing import Mathlib.SetTheory.Cardinal.Order /-! # Theory of univariate polynomials We define the multiset of roots of a polynomial, and prove basic results about it. ## Main definitions * `Polynomial.roots p`: The multiset containing all the roots of `p`, including their multiplicities. * `Polynomial.rootSet p E`: The set of distinct roots of `p` in an algebra `E`. ## Main statements * `Polynomial.C_leadingCoeff_mul_prod_multiset_X_sub_C`: If a polynomial has as many roots as its degree, it can be written as the product of its leading coefficient with `∏ (X - a)` where `a` ranges through its roots. -/ assert_not_exists Ideal open Multiset Finset noncomputable section namespace Polynomial universe u v w z variable {R : Type u} {S : Type v} {T : Type w} {a b : R} {n : ℕ} section CommRing variable [CommRing R] [IsDomain R] {p q : R[X]} section Roots /-- `roots p` noncomputably gives a multiset containing all the roots of `p`, including their multiplicities. -/ noncomputable def roots (p : R[X]) : Multiset R := haveI := Classical.decEq R haveI := Classical.dec (p = 0) if h : p = 0 then ∅ else Classical.choose (exists_multiset_roots h) theorem roots_def [DecidableEq R] (p : R[X]) [Decidable (p = 0)] : p.roots = if h : p = 0 then ∅ else Classical.choose (exists_multiset_roots h) := by rename_i iR ip0 obtain rfl := Subsingleton.elim iR (Classical.decEq R) obtain rfl := Subsingleton.elim ip0 (Classical.dec (p = 0)) rfl @[simp] theorem roots_zero : (0 : R[X]).roots = 0 := dif_pos rfl theorem card_roots (hp0 : p ≠ 0) : (Multiset.card (roots p) : WithBot ℕ) ≤ degree p := by classical unfold roots rw [dif_neg hp0] exact (Classical.choose_spec (exists_multiset_roots hp0)).1 theorem card_roots' (p : R[X]) : Multiset.card p.roots ≤ natDegree p := by by_cases hp0 : p = 0 · simp [hp0] exact WithBot.coe_le_coe.1 (le_trans (card_roots hp0) (le_of_eq <| degree_eq_natDegree hp0)) theorem card_roots_sub_C {p : R[X]} {a : R} (hp0 : 0 < degree p) : (Multiset.card (p - C a).roots : WithBot ℕ) ≤ degree p := calc (Multiset.card (p - C a).roots : WithBot ℕ) ≤ degree (p - C a) := card_roots <| mt sub_eq_zero.1 fun h => not_le_of_gt hp0 <| h.symm ▸ degree_C_le _ = degree p := by rw [sub_eq_add_neg, ← C_neg]; exact degree_add_C hp0 theorem card_roots_sub_C' {p : R[X]} {a : R} (hp0 : 0 < degree p) : Multiset.card (p - C a).roots ≤ natDegree p := WithBot.coe_le_coe.1 (le_trans (card_roots_sub_C hp0) (le_of_eq <| degree_eq_natDegree fun h => by simp_all [lt_irrefl])) @[simp] theorem count_roots [DecidableEq R] (p : R[X]) : p.roots.count a = rootMultiplicity a p := by classical by_cases hp : p = 0 · simp [hp] rw [roots_def, dif_neg hp] exact (Classical.choose_spec (exists_multiset_roots hp)).2 a @[simp] theorem mem_roots' : a ∈ p.roots ↔ p ≠ 0 ∧ IsRoot p a := by classical rw [← count_pos, count_roots p, rootMultiplicity_pos'] theorem mem_roots (hp : p ≠ 0) : a ∈ p.roots ↔ IsRoot p a := mem_roots'.trans <| and_iff_right hp theorem ne_zero_of_mem_roots (h : a ∈ p.roots) : p ≠ 0 := (mem_roots'.1 h).1 theorem isRoot_of_mem_roots (h : a ∈ p.roots) : IsRoot p a := (mem_roots'.1 h).2 theorem mem_roots_map_of_injective [Semiring S] {p : S[X]} {f : S →+* R} (hf : Function.Injective f) {x : R} (hp : p ≠ 0) : x ∈ (p.map f).roots ↔ p.eval₂ f x = 0 := by rw [mem_roots ((Polynomial.map_ne_zero_iff hf).mpr hp), IsRoot, eval_map] lemma mem_roots_iff_aeval_eq_zero {x : R} (w : p ≠ 0) : x ∈ roots p ↔ aeval x p = 0 := by rw [aeval_def, ← mem_roots_map_of_injective (FaithfulSMul.algebraMap_injective _ _) w, Algebra.id.map_eq_id, map_id] theorem card_le_degree_of_subset_roots {p : R[X]} {Z : Finset R} (h : Z.val ⊆ p.roots) : #Z ≤ p.natDegree := (Multiset.card_le_card (Finset.val_le_iff_val_subset.2 h)).trans (Polynomial.card_roots' p) theorem finite_setOf_isRoot {p : R[X]} (hp : p ≠ 0) : Set.Finite { x | IsRoot p x } := by classical simpa only [← Finset.setOf_mem, Multiset.mem_toFinset, mem_roots hp] using p.roots.toFinset.finite_toSet theorem eq_zero_of_infinite_isRoot (p : R[X]) (h : Set.Infinite { x | IsRoot p x }) : p = 0 := not_imp_comm.mp finite_setOf_isRoot h theorem exists_max_root [LinearOrder R] (p : R[X]) (hp : p ≠ 0) : ∃ x₀, ∀ x, p.IsRoot x → x ≤ x₀ := Set.exists_upper_bound_image _ _ <| finite_setOf_isRoot hp theorem exists_min_root [LinearOrder R] (p : R[X]) (hp : p ≠ 0) : ∃ x₀, ∀ x, p.IsRoot x → x₀ ≤ x := Set.exists_lower_bound_image _ _ <| finite_setOf_isRoot hp theorem eq_of_infinite_eval_eq (p q : R[X]) (h : Set.Infinite { x | eval x p = eval x q }) : p = q := by rw [← sub_eq_zero] apply eq_zero_of_infinite_isRoot simpa only [IsRoot, eval_sub, sub_eq_zero] theorem roots_mul {p q : R[X]} (hpq : p * q ≠ 0) : (p * q).roots = p.roots + q.roots := by classical exact Multiset.ext.mpr fun r => by rw [count_add, count_roots, count_roots, count_roots, rootMultiplicity_mul hpq] theorem roots.le_of_dvd (h : q ≠ 0) : p ∣ q → roots p ≤ roots q := by rintro ⟨k, rfl⟩ exact Multiset.le_iff_exists_add.mpr ⟨k.roots, roots_mul h⟩ theorem mem_roots_sub_C' {p : R[X]} {a x : R} : x ∈ (p - C a).roots ↔ p ≠ C a ∧ p.eval x = a := by rw [mem_roots', IsRoot.def, sub_ne_zero, eval_sub, sub_eq_zero, eval_C] theorem mem_roots_sub_C {p : R[X]} {a x : R} (hp0 : 0 < degree p) : x ∈ (p - C a).roots ↔ p.eval x = a := mem_roots_sub_C'.trans <| and_iff_right fun hp => hp0.not_le <| hp.symm ▸ degree_C_le @[simp] theorem roots_X_sub_C (r : R) : roots (X - C r) = {r} := by classical ext s rw [count_roots, rootMultiplicity_X_sub_C, count_singleton] @[simp] theorem roots_X_add_C (r : R) : roots (X + C r) = {-r} := by simpa using roots_X_sub_C (-r) @[simp] theorem roots_X : roots (X : R[X]) = {0} := by rw [← roots_X_sub_C, C_0, sub_zero] @[simp] theorem roots_C (x : R) : (C x).roots = 0 := by classical exact if H : x = 0 then by rw [H, C_0, roots_zero] else Multiset.ext.mpr fun r => (by rw [count_roots, count_zero, rootMultiplicity_eq_zero (not_isRoot_C _ _ H)]) @[simp] theorem roots_one : (1 : R[X]).roots = ∅ := roots_C 1 @[simp] theorem roots_C_mul (p : R[X]) (ha : a ≠ 0) : (C a * p).roots = p.roots := by by_cases hp : p = 0 <;> simp only [roots_mul, *, Ne, mul_eq_zero, C_eq_zero, or_self_iff, not_false_iff, roots_C, zero_add, mul_zero] @[simp] theorem roots_smul_nonzero (p : R[X]) (ha : a ≠ 0) : (a • p).roots = p.roots := by rw [smul_eq_C_mul, roots_C_mul _ ha] @[simp] lemma roots_neg (p : R[X]) : (-p).roots = p.roots := by rw [← neg_one_smul R p, roots_smul_nonzero p (neg_ne_zero.mpr one_ne_zero)] @[simp] theorem roots_C_mul_X_sub_C_of_IsUnit (b : R) (a : Rˣ) : (C (a : R) * X - C b).roots = {a⁻¹ * b} := by rw [← roots_C_mul _ (Units.ne_zero a⁻¹), mul_sub, ← mul_assoc, ← C_mul, ← C_mul, Units.inv_mul, C_1, one_mul] exact roots_X_sub_C (a⁻¹ * b) @[simp] theorem roots_C_mul_X_add_C_of_IsUnit (b : R) (a : Rˣ) : (C (a : R) * X + C b).roots = {-(a⁻¹ * b)} := by rw [← sub_neg_eq_add, ← C_neg, roots_C_mul_X_sub_C_of_IsUnit, mul_neg] theorem roots_list_prod (L : List R[X]) : (0 : R[X]) ∉ L → L.prod.roots = (L : Multiset R[X]).bind roots := List.recOn L (fun _ => roots_one) fun hd tl ih H => by rw [List.mem_cons, not_or] at H rw [List.prod_cons, roots_mul (mul_ne_zero (Ne.symm H.1) <| List.prod_ne_zero H.2), ← Multiset.cons_coe, Multiset.cons_bind, ih H.2] theorem roots_multiset_prod (m : Multiset R[X]) : (0 : R[X]) ∉ m → m.prod.roots = m.bind roots := by rcases m with ⟨L⟩ simpa only [Multiset.prod_coe, quot_mk_to_coe''] using roots_list_prod L theorem roots_prod {ι : Type*} (f : ι → R[X]) (s : Finset ι) : s.prod f ≠ 0 → (s.prod f).roots = s.val.bind fun i => roots (f i) := by rcases s with ⟨m, hm⟩ simpa [Multiset.prod_eq_zero_iff, Multiset.bind_map] using roots_multiset_prod (m.map f) @[simp] theorem roots_pow (p : R[X]) (n : ℕ) : (p ^ n).roots = n • p.roots := by induction n with | zero => rw [pow_zero, roots_one, zero_smul, empty_eq_zero] | succ n ihn => rcases eq_or_ne p 0 with (rfl | hp) · rw [zero_pow n.succ_ne_zero, roots_zero, smul_zero] · rw [pow_succ, roots_mul (mul_ne_zero (pow_ne_zero _ hp) hp), ihn, add_smul, one_smul] theorem roots_X_pow (n : ℕ) : (X ^ n : R[X]).roots = n • ({0} : Multiset R) := by rw [roots_pow, roots_X] theorem roots_C_mul_X_pow (ha : a ≠ 0) (n : ℕ) : Polynomial.roots (C a * X ^ n) = n • ({0} : Multiset R) := by rw [roots_C_mul _ ha, roots_X_pow] @[simp] theorem roots_monomial (ha : a ≠ 0) (n : ℕ) : (monomial n a).roots = n • ({0} : Multiset R) := by rw [← C_mul_X_pow_eq_monomial, roots_C_mul_X_pow ha] theorem roots_prod_X_sub_C (s : Finset R) : (s.prod fun a => X - C a).roots = s.val := by apply (roots_prod (fun a => X - C a) s ?_).trans · simp_rw [roots_X_sub_C] rw [Multiset.bind_singleton, Multiset.map_id'] · refine prod_ne_zero_iff.mpr (fun a _ => X_sub_C_ne_zero a) @[simp] theorem roots_multiset_prod_X_sub_C (s : Multiset R) : (s.map fun a => X - C a).prod.roots = s := by rw [roots_multiset_prod, Multiset.bind_map] · simp_rw [roots_X_sub_C] rw [Multiset.bind_singleton, Multiset.map_id'] · rw [Multiset.mem_map] rintro ⟨a, -, h⟩ exact X_sub_C_ne_zero a h theorem card_roots_X_pow_sub_C {n : ℕ} (hn : 0 < n) (a : R) : Multiset.card (roots ((X : R[X]) ^ n - C a)) ≤ n := WithBot.coe_le_coe.1 <| calc (Multiset.card (roots ((X : R[X]) ^ n - C a)) : WithBot ℕ) ≤ degree ((X : R[X]) ^ n - C a) := card_roots (X_pow_sub_C_ne_zero hn a) _ = n := degree_X_pow_sub_C hn a section NthRoots /-- `nthRoots n a` noncomputably returns the solutions to `x ^ n = a`. -/ def nthRoots (n : ℕ) (a : R) : Multiset R := roots ((X : R[X]) ^ n - C a) @[simp] theorem mem_nthRoots {n : ℕ} (hn : 0 < n) {a x : R} : x ∈ nthRoots n a ↔ x ^ n = a := by rw [nthRoots, mem_roots (X_pow_sub_C_ne_zero hn a), IsRoot.def, eval_sub, eval_C, eval_pow, eval_X, sub_eq_zero] @[simp] theorem nthRoots_zero (r : R) : nthRoots 0 r = 0 := by simp only [empty_eq_zero, pow_zero, nthRoots, ← C_1, ← C_sub, roots_C] @[simp] theorem nthRoots_zero_right {R} [CommRing R] [IsDomain R] (n : ℕ) : nthRoots n (0 : R) = Multiset.replicate n 0 := by rw [nthRoots, C.map_zero, sub_zero, roots_pow, roots_X, Multiset.nsmul_singleton] theorem card_nthRoots (n : ℕ) (a : R) : Multiset.card (nthRoots n a) ≤ n := by classical exact (if hn : n = 0 then if h : (X : R[X]) ^ n - C a = 0 then by simp [Nat.zero_le, nthRoots, roots, h, dif_pos rfl, empty_eq_zero, Multiset.card_zero] else WithBot.coe_le_coe.1 (le_trans (card_roots h) (by rw [hn, pow_zero, ← C_1, ← RingHom.map_sub] exact degree_C_le)) else by rw [← Nat.cast_le (α := WithBot ℕ)] rw [← degree_X_pow_sub_C (Nat.pos_of_ne_zero hn) a] exact card_roots (X_pow_sub_C_ne_zero (Nat.pos_of_ne_zero hn) a)) @[simp] theorem nthRoots_two_eq_zero_iff {r : R} : nthRoots 2 r = 0 ↔ ¬IsSquare r := by simp_rw [isSquare_iff_exists_sq, eq_zero_iff_forall_not_mem, mem_nthRoots (by norm_num : 0 < 2), ← not_exists, eq_comm] /-- The multiset `nthRoots ↑n a` as a Finset. Previously `nthRootsFinset n` was defined to be `nthRoots n (1 : R)` as a Finset. That situation can be recovered by setting `a` to be `(1 : R)` -/ def nthRootsFinset (n : ℕ) {R : Type*} (a : R) [CommRing R] [IsDomain R] : Finset R := haveI := Classical.decEq R Multiset.toFinset (nthRoots n a) lemma nthRootsFinset_def (n : ℕ) {R : Type*} (a : R) [CommRing R] [IsDomain R] [DecidableEq R] : nthRootsFinset n a = Multiset.toFinset (nthRoots n a) := by unfold nthRootsFinset convert rfl @[simp] theorem mem_nthRootsFinset {n : ℕ} (h : 0 < n) (a : R) {x : R} : x ∈ nthRootsFinset n a ↔ x ^ (n : ℕ) = a := by classical rw [nthRootsFinset_def, mem_toFinset, mem_nthRoots h] @[simp] theorem nthRootsFinset_zero (a : R) : nthRootsFinset 0 a = ∅ := by classical simp [nthRootsFinset_def] theorem map_mem_nthRootsFinset {S F : Type*} [CommRing S] [IsDomain S] [FunLike F R S] [MonoidHomClass F R S] {a : R} {x : R} (hx : x ∈ nthRootsFinset n a) (f : F) : f x ∈ nthRootsFinset n (f a) := by by_cases hn : n = 0 · simp [hn] at hx · rw [mem_nthRootsFinset <| Nat.pos_of_ne_zero hn, ← map_pow, (mem_nthRootsFinset (Nat.pos_of_ne_zero hn) a).1 hx] theorem map_mem_nthRootsFinset_one {S F : Type*} [CommRing S] [IsDomain S] [FunLike F R S] [RingHomClass F R S] {x : R} (hx : x ∈ nthRootsFinset n 1) (f : F) : f x ∈ nthRootsFinset n 1 := by rw [← (map_one f)] exact map_mem_nthRootsFinset hx _ theorem mul_mem_nthRootsFinset {η₁ η₂ : R} {a₁ a₂ : R} (hη₁ : η₁ ∈ nthRootsFinset n a₁) (hη₂ : η₂ ∈ nthRootsFinset n a₂) : η₁ * η₂ ∈ nthRootsFinset n (a₁ * a₂) := by cases n with | zero => simp only [nthRootsFinset_zero, not_mem_empty] at hη₁ | succ n => rw [mem_nthRootsFinset n.succ_pos] at hη₁ hη₂ ⊢ rw [mul_pow, hη₁, hη₂] theorem ne_zero_of_mem_nthRootsFinset {η : R} {a : R} (ha : a ≠ 0) (hη : η ∈ nthRootsFinset n a) : η ≠ 0 := by nontriviality R rintro rfl cases n with | zero => simp only [nthRootsFinset_zero, not_mem_empty] at hη | succ n => rw [mem_nthRootsFinset n.succ_pos, zero_pow n.succ_ne_zero] at hη exact ha hη.symm theorem one_mem_nthRootsFinset (hn : 0 < n) : 1 ∈ nthRootsFinset n (1 : R) := by rw [mem_nthRootsFinset hn, one_pow] end NthRoots theorem zero_of_eval_zero [Infinite R] (p : R[X]) (h : ∀ x, p.eval x = 0) : p = 0 := by classical by_contra hp refine @Fintype.false R _ ?_ exact ⟨p.roots.toFinset, fun x => Multiset.mem_toFinset.mpr ((mem_roots hp).mpr (h _))⟩ theorem funext [Infinite R] {p q : R[X]} (ext : ∀ r : R, p.eval r = q.eval r) : p = q := by rw [← sub_eq_zero] apply zero_of_eval_zero intro x rw [eval_sub, sub_eq_zero, ext] variable [CommRing T] /-- Given a polynomial `p` with coefficients in a ring `T` and a `T`-algebra `S`, `aroots p S` is the multiset of roots of `p` regarded as a polynomial over `S`. -/ noncomputable abbrev aroots (p : T[X]) (S) [CommRing S] [IsDomain S] [Algebra T S] : Multiset S := (p.map (algebraMap T S)).roots theorem aroots_def (p : T[X]) (S) [CommRing S] [IsDomain S] [Algebra T S] : p.aroots S = (p.map (algebraMap T S)).roots := rfl theorem mem_aroots' [CommRing S] [IsDomain S] [Algebra T S] {p : T[X]} {a : S} : a ∈ p.aroots S ↔ p.map (algebraMap T S) ≠ 0 ∧ aeval a p = 0 := by rw [mem_roots', IsRoot.def, ← eval₂_eq_eval_map, aeval_def] theorem mem_aroots [CommRing S] [IsDomain S] [Algebra T S] [NoZeroSMulDivisors T S] {p : T[X]} {a : S} : a ∈ p.aroots S ↔ p ≠ 0 ∧ aeval a p = 0 := by rw [mem_aroots', Polynomial.map_ne_zero_iff] exact FaithfulSMul.algebraMap_injective T S theorem aroots_mul [CommRing S] [IsDomain S] [Algebra T S] [NoZeroSMulDivisors T S] {p q : T[X]} (hpq : p * q ≠ 0) : (p * q).aroots S = p.aroots S + q.aroots S := by suffices map (algebraMap T S) p * map (algebraMap T S) q ≠ 0 by rw [aroots_def, Polynomial.map_mul, roots_mul this] rwa [← Polynomial.map_mul, Polynomial.map_ne_zero_iff (FaithfulSMul.algebraMap_injective T S)] @[simp] theorem aroots_X_sub_C [CommRing S] [IsDomain S] [Algebra T S] (r : T) : aroots (X - C r) S = {algebraMap T S r} := by rw [aroots_def, Polynomial.map_sub, map_X, map_C, roots_X_sub_C] @[simp] theorem aroots_X [CommRing S] [IsDomain S] [Algebra T S] : aroots (X : T[X]) S = {0} := by rw [aroots_def, map_X, roots_X] @[simp] theorem aroots_C [CommRing S] [IsDomain S] [Algebra T S] (a : T) : (C a).aroots S = 0 := by rw [aroots_def, map_C, roots_C] @[simp] theorem aroots_zero (S) [CommRing S] [IsDomain S] [Algebra T S] : (0 : T[X]).aroots S = 0 := by rw [← C_0, aroots_C] @[simp] theorem aroots_one [CommRing S] [IsDomain S] [Algebra T S] : (1 : T[X]).aroots S = 0 := aroots_C 1 @[simp] theorem aroots_neg [CommRing S] [IsDomain S] [Algebra T S] (p : T[X]) : (-p).aroots S = p.aroots S := by rw [aroots, Polynomial.map_neg, roots_neg] @[simp] theorem aroots_C_mul [CommRing S] [IsDomain S] [Algebra T S] [NoZeroSMulDivisors T S] {a : T} (p : T[X]) (ha : a ≠ 0) : (C a * p).aroots S = p.aroots S := by rw [aroots_def, Polynomial.map_mul, map_C, roots_C_mul] rwa [map_ne_zero_iff] exact FaithfulSMul.algebraMap_injective T S @[simp] theorem aroots_smul_nonzero [CommRing S] [IsDomain S] [Algebra T S] [NoZeroSMulDivisors T S] {a : T} (p : T[X]) (ha : a ≠ 0) : (a • p).aroots S = p.aroots S := by rw [smul_eq_C_mul, aroots_C_mul _ ha] @[simp] theorem aroots_pow [CommRing S] [IsDomain S] [Algebra T S] (p : T[X]) (n : ℕ) : (p ^ n).aroots S = n • p.aroots S := by rw [aroots_def, Polynomial.map_pow, roots_pow] theorem aroots_X_pow [CommRing S] [IsDomain S] [Algebra T S] (n : ℕ) : (X ^ n : T[X]).aroots S = n • ({0} : Multiset S) := by rw [aroots_pow, aroots_X] theorem aroots_C_mul_X_pow [CommRing S] [IsDomain S] [Algebra T S] [NoZeroSMulDivisors T S] {a : T} (ha : a ≠ 0) (n : ℕ) : (C a * X ^ n : T[X]).aroots S = n • ({0} : Multiset S) := by rw [aroots_C_mul _ ha, aroots_X_pow] @[simp] theorem aroots_monomial [CommRing S] [IsDomain S] [Algebra T S] [NoZeroSMulDivisors T S] {a : T} (ha : a ≠ 0) (n : ℕ) : (monomial n a).aroots S = n • ({0} : Multiset S) := by rw [← C_mul_X_pow_eq_monomial, aroots_C_mul_X_pow ha] variable (R S) in @[simp] theorem aroots_map (p : T[X]) [CommRing S] [Algebra T S] [Algebra S R] [Algebra T R] [IsScalarTower T S R] : (p.map (algebraMap T S)).aroots R = p.aroots R := by rw [aroots_def, aroots_def, map_map, IsScalarTower.algebraMap_eq T S R] /-- The set of distinct roots of `p` in `S`. If you have a non-separable polynomial, use `Polynomial.aroots` for the multiset where multiple roots have the appropriate multiplicity. -/ def rootSet (p : T[X]) (S) [CommRing S] [IsDomain S] [Algebra T S] : Set S := haveI := Classical.decEq S (p.aroots S).toFinset theorem rootSet_def (p : T[X]) (S) [CommRing S] [IsDomain S] [Algebra T S] [DecidableEq S] : p.rootSet S = (p.aroots S).toFinset := by rw [rootSet] convert rfl @[simp] theorem rootSet_C [CommRing S] [IsDomain S] [Algebra T S] (a : T) : (C a).rootSet S = ∅ := by classical rw [rootSet_def, aroots_C, Multiset.toFinset_zero, Finset.coe_empty] @[simp] theorem rootSet_zero (S) [CommRing S] [IsDomain S] [Algebra T S] : (0 : T[X]).rootSet S = ∅ := by rw [← C_0, rootSet_C] @[simp] theorem rootSet_one (S) [CommRing S] [IsDomain S] [Algebra T S] : (1 : T[X]).rootSet S = ∅ := by rw [← C_1, rootSet_C] @[simp] theorem rootSet_neg (p : T[X]) (S) [CommRing S] [IsDomain S] [Algebra T S] : (-p).rootSet S = p.rootSet S := by rw [rootSet, aroots_neg, rootSet] instance rootSetFintype (p : T[X]) (S : Type*) [CommRing S] [IsDomain S] [Algebra T S] : Fintype (p.rootSet S) := FinsetCoe.fintype _ theorem rootSet_finite (p : T[X]) (S : Type*) [CommRing S] [IsDomain S] [Algebra T S] : (p.rootSet S).Finite := Set.toFinite _ /-- The set of roots of all polynomials of bounded degree and having coefficients in a finite set is finite. -/ theorem bUnion_roots_finite {R S : Type*} [Semiring R] [CommRing S] [IsDomain S] [DecidableEq S] (m : R →+* S) (d : ℕ) {U : Set R} (h : U.Finite) : (⋃ (f : R[X]) (_ : f.natDegree ≤ d ∧ ∀ i, f.coeff i ∈ U), ((f.map m).roots.toFinset.toSet : Set S)).Finite := Set.Finite.biUnion (by -- We prove that the set of polynomials under consideration is finite because its -- image by the injective map `π` is finite let π : R[X] → Fin (d + 1) → R := fun f i => f.coeff i refine ((Set.Finite.pi fun _ => h).subset <| ?_).of_finite_image (?_ : Set.InjOn π _) · exact Set.image_subset_iff.2 fun f hf i _ => hf.2 i · refine fun x hx y hy hxy => (ext_iff_natDegree_le hx.1 hy.1).2 fun i hi => ?_ exact id congr_fun hxy ⟨i, Nat.lt_succ_of_le hi⟩) fun _ _ => Finset.finite_toSet _ theorem mem_rootSet' {p : T[X]} {S : Type*} [CommRing S] [IsDomain S] [Algebra T S] {a : S} : a ∈ p.rootSet S ↔ p.map (algebraMap T S) ≠ 0 ∧ aeval a p = 0 := by classical rw [rootSet_def, Finset.mem_coe, mem_toFinset, mem_aroots'] theorem mem_rootSet {p : T[X]} {S : Type*} [CommRing S] [IsDomain S] [Algebra T S] [NoZeroSMulDivisors T S] {a : S} : a ∈ p.rootSet S ↔ p ≠ 0 ∧ aeval a p = 0 := by rw [mem_rootSet', Polynomial.map_ne_zero_iff (FaithfulSMul.algebraMap_injective T S)] theorem mem_rootSet_of_ne {p : T[X]} {S : Type*} [CommRing S] [IsDomain S] [Algebra T S] [NoZeroSMulDivisors T S] (hp : p ≠ 0) {a : S} : a ∈ p.rootSet S ↔ aeval a p = 0 := mem_rootSet.trans <| and_iff_right hp theorem rootSet_maps_to' {p : T[X]} {S S'} [CommRing S] [IsDomain S] [Algebra T S] [CommRing S'] [IsDomain S'] [Algebra T S'] (hp : p.map (algebraMap T S') = 0 → p.map (algebraMap T S) = 0) (f : S →ₐ[T] S') : (p.rootSet S).MapsTo f (p.rootSet S') := fun x hx => by rw [mem_rootSet'] at hx ⊢ rw [aeval_algHom, AlgHom.comp_apply, hx.2, _root_.map_zero] exact ⟨mt hp hx.1, rfl⟩ theorem ne_zero_of_mem_rootSet {p : T[X]} [CommRing S] [IsDomain S] [Algebra T S] {a : S} (h : a ∈ p.rootSet S) : p ≠ 0 := fun hf => by rwa [hf, rootSet_zero] at h theorem aeval_eq_zero_of_mem_rootSet {p : T[X]} [CommRing S] [IsDomain S] [Algebra T S] {a : S} (hx : a ∈ p.rootSet S) : aeval a p = 0 := (mem_rootSet'.1 hx).2 theorem rootSet_mapsTo {p : T[X]} {S S'} [CommRing S] [IsDomain S] [Algebra T S] [CommRing S'] [IsDomain S'] [Algebra T S'] [NoZeroSMulDivisors T S'] (f : S →ₐ[T] S') : (p.rootSet S).MapsTo f (p.rootSet S') := by refine rootSet_maps_to' (fun h₀ => ?_) f obtain rfl : p = 0 := map_injective _ (FaithfulSMul.algebraMap_injective T S') (by rwa [Polynomial.map_zero]) exact Polynomial.map_zero _ theorem mem_rootSet_of_injective [CommRing S] {p : S[X]} [Algebra S R] (h : Function.Injective (algebraMap S R)) {x : R} (hp : p ≠ 0) : x ∈ p.rootSet R ↔ aeval x p = 0 := by classical exact Multiset.mem_toFinset.trans (mem_roots_map_of_injective h hp) end Roots lemma eq_zero_of_natDegree_lt_card_of_eval_eq_zero {R} [CommRing R] [IsDomain R] (p : R[X]) {ι} [Fintype ι] {f : ι → R} (hf : Function.Injective f) (heval : ∀ i, p.eval (f i) = 0) (hcard : natDegree p < Fintype.card ι) : p = 0 := by classical by_contra hp refine lt_irrefl #p.roots.toFinset ?_ calc #p.roots.toFinset ≤ Multiset.card p.roots := Multiset.toFinset_card_le _ _ ≤ natDegree p := Polynomial.card_roots' p _ < Fintype.card ι := hcard _ = Fintype.card (Set.range f) := (Set.card_range_of_injective hf).symm _ = #(Finset.univ.image f) := by rw [← Set.toFinset_card, Set.toFinset_range] _ ≤ #p.roots.toFinset := Finset.card_mono ?_ intro _ simp only [Finset.mem_image, Finset.mem_univ, true_and, Multiset.mem_toFinset, mem_roots', ne_eq, IsRoot.def, forall_exists_index, hp, not_false_eq_true] rintro x rfl exact heval _ lemma eq_zero_of_natDegree_lt_card_of_eval_eq_zero' {R} [CommRing R] [IsDomain R] (p : R[X]) (s : Finset R) (heval : ∀ i ∈ s, p.eval i = 0) (hcard : natDegree p < #s) : p = 0 := eq_zero_of_natDegree_lt_card_of_eval_eq_zero p Subtype.val_injective (fun i : s ↦ heval i i.prop) (hcard.trans_eq (Fintype.card_coe s).symm) open Cardinal in lemma eq_zero_of_forall_eval_zero_of_natDegree_lt_card (f : R[X]) (hf : ∀ r, f.eval r = 0) (hfR : f.natDegree < #R) : f = 0 := by obtain hR|hR := finite_or_infinite R · have := Fintype.ofFinite R apply eq_zero_of_natDegree_lt_card_of_eval_eq_zero f Function.injective_id hf simpa only [mk_fintype, Nat.cast_lt] using hfR · exact zero_of_eval_zero _ hf open Cardinal in lemma exists_eval_ne_zero_of_natDegree_lt_card (f : R[X]) (hf : f ≠ 0) (hfR : f.natDegree < #R) : ∃ r, f.eval r ≠ 0 := by contrapose! hf exact eq_zero_of_forall_eval_zero_of_natDegree_lt_card f hf hfR section omit [IsDomain R] theorem monic_multisetProd_X_sub_C (s : Multiset R) : Monic (s.map fun a => X - C a).prod := monic_multiset_prod_of_monic _ _ fun a _ => monic_X_sub_C a theorem monic_prod_X_sub_C {α : Type*} (b : α → R) (s : Finset α) : Monic (∏ a ∈ s, (X - C (b a))) := monic_prod_of_monic _ _ fun a _ => monic_X_sub_C (b a) theorem monic_finprod_X_sub_C {α : Type*} (b : α → R) : Monic (∏ᶠ k, (X - C (b k))) := monic_finprod_of_monic _ _ fun a _ => monic_X_sub_C (b a) end theorem prod_multiset_root_eq_finset_root [DecidableEq R] : (p.roots.map fun a => X - C a).prod = p.roots.toFinset.prod fun a => (X - C a) ^ rootMultiplicity a p := by simp only [count_roots, Finset.prod_multiset_map_count] /-- The product `∏ (X - a)` for `a` inside the multiset `p.roots` divides `p`. -/ theorem prod_multiset_X_sub_C_dvd (p : R[X]) : (p.roots.map fun a => X - C a).prod ∣ p := by classical rw [← map_dvd_map _ (IsFractionRing.injective R <| FractionRing R) (monic_multisetProd_X_sub_C p.roots)] rw [prod_multiset_root_eq_finset_root, Polynomial.map_prod] refine Finset.prod_dvd_of_coprime (fun a _ b _ h => ?_) fun a _ => ?_ · simp_rw [Polynomial.map_pow, Polynomial.map_sub, map_C, map_X] exact (pairwise_coprime_X_sub_C (IsFractionRing.injective R <| FractionRing R) h).pow · exact Polynomial.map_dvd _ (pow_rootMultiplicity_dvd p a) /-- A Galois connection. -/ theorem _root_.Multiset.prod_X_sub_C_dvd_iff_le_roots {p : R[X]} (hp : p ≠ 0) (s : Multiset R) : (s.map fun a => X - C a).prod ∣ p ↔ s ≤ p.roots := by classical exact ⟨fun h => Multiset.le_iff_count.2 fun r => by rw [count_roots, le_rootMultiplicity_iff hp, ← Multiset.prod_replicate, ← Multiset.map_replicate fun a => X - C a, ← Multiset.filter_eq] exact (Multiset.prod_dvd_prod_of_le <| Multiset.map_le_map <| s.filter_le _).trans h, fun h => (Multiset.prod_dvd_prod_of_le <| Multiset.map_le_map h).trans p.prod_multiset_X_sub_C_dvd⟩ theorem exists_prod_multiset_X_sub_C_mul (p : R[X]) : ∃ q, (p.roots.map fun a => X - C a).prod * q = p ∧ Multiset.card p.roots + q.natDegree = p.natDegree ∧ q.roots = 0 := by obtain ⟨q, he⟩ := p.prod_multiset_X_sub_C_dvd use q, he.symm obtain rfl | hq := eq_or_ne q 0 · rw [mul_zero] at he subst he simp constructor · conv_rhs => rw [he] rw [(monic_multisetProd_X_sub_C p.roots).natDegree_mul' hq, natDegree_multiset_prod_X_sub_C_eq_card] · replace he := congr_arg roots he.symm rw [roots_mul, roots_multiset_prod_X_sub_C] at he exacts [add_eq_left.1 he, mul_ne_zero (monic_multisetProd_X_sub_C p.roots).ne_zero hq] /-- A polynomial `p` that has as many roots as its degree can be written `p = p.leadingCoeff * ∏(X - a)`, for `a` in `p.roots`. -/ theorem C_leadingCoeff_mul_prod_multiset_X_sub_C (hroots : Multiset.card p.roots = p.natDegree) : C p.leadingCoeff * (p.roots.map fun a => X - C a).prod = p := (eq_leadingCoeff_mul_of_monic_of_dvd_of_natDegree_le (monic_multisetProd_X_sub_C p.roots) p.prod_multiset_X_sub_C_dvd ((natDegree_multiset_prod_X_sub_C_eq_card _).trans hroots).ge).symm /-- A monic polynomial `p` that has as many roots as its degree can be written `p = ∏(X - a)`, for `a` in `p.roots`. -/ theorem prod_multiset_X_sub_C_of_monic_of_roots_card_eq (hp : p.Monic) (hroots : Multiset.card p.roots = p.natDegree) : (p.roots.map fun a => X - C a).prod = p := by convert C_leadingCoeff_mul_prod_multiset_X_sub_C hroots rw [hp.leadingCoeff, C_1, one_mul] theorem Monic.isUnit_leadingCoeff_of_dvd {a p : R[X]} (hp : Monic p) (hap : a ∣ p) : IsUnit a.leadingCoeff := isUnit_of_dvd_one (by simpa only [hp.leadingCoeff] using leadingCoeff_dvd_leadingCoeff hap) /-- To check a monic polynomial is irreducible, it suffices to check only for divisors that have smaller degree. See also: `Polynomial.Monic.irreducible_iff_natDegree`. -/ theorem Monic.irreducible_iff_degree_lt {p : R[X]} (p_monic : Monic p) (p_1 : p ≠ 1) : Irreducible p ↔ ∀ q, degree q ≤ ↑(p.natDegree / 2) → q ∣ p → IsUnit q := by simp only [p_monic.irreducible_iff_lt_natDegree_lt p_1, Finset.mem_Ioc, and_imp, natDegree_pos_iff_degree_pos, natDegree_le_iff_degree_le] constructor · rintro h q deg_le dvd by_contra q_unit have := degree_pos_of_not_isUnit_of_dvd_monic p_monic q_unit dvd have hu := p_monic.isUnit_leadingCoeff_of_dvd dvd refine (h _ (monic_of_isUnit_leadingCoeff_inv_smul hu) ?_ ?_ (dvd_trans ?_ dvd)).elim · rwa [degree_smul_of_smul_regular _ (isSMulRegular_of_group _)] · rwa [degree_smul_of_smul_regular _ (isSMulRegular_of_group _)] · rw [Units.smul_def, Polynomial.smul_eq_C_mul, (isUnit_C.mpr (Units.isUnit _)).mul_left_dvd] · rintro h q _ deg_pos deg_le dvd exact deg_pos.ne' <| degree_eq_zero_of_isUnit (h q deg_le dvd) end CommRing section variable {A B : Type*} [CommRing A] [CommRing B] theorem le_rootMultiplicity_map {p : A[X]} {f : A →+* B} (hmap : map f p ≠ 0) (a : A) : rootMultiplicity a p ≤ rootMultiplicity (f a) (p.map f) := by rw [le_rootMultiplicity_iff hmap] refine _root_.trans ?_ ((mapRingHom f).map_dvd (pow_rootMultiplicity_dvd p a)) rw [map_pow, map_sub, coe_mapRingHom, map_X, map_C] theorem eq_rootMultiplicity_map {p : A[X]} {f : A →+* B} (hf : Function.Injective f) (a : A) : rootMultiplicity a p = rootMultiplicity (f a) (p.map f) := by by_cases hp0 : p = 0; · simp only [hp0, rootMultiplicity_zero, Polynomial.map_zero] apply le_antisymm (le_rootMultiplicity_map ((Polynomial.map_ne_zero_iff hf).mpr hp0) a) rw [le_rootMultiplicity_iff hp0, ← map_dvd_map f hf ((monic_X_sub_C a).pow _), Polynomial.map_pow, Polynomial.map_sub, map_X, map_C] apply pow_rootMultiplicity_dvd theorem count_map_roots [IsDomain A] [DecidableEq B] {p : A[X]} {f : A →+* B} (hmap : map f p ≠ 0) (b : B) : (p.roots.map f).count b ≤ rootMultiplicity b (p.map f) := by rw [le_rootMultiplicity_iff hmap, ← Multiset.prod_replicate, ← Multiset.map_replicate fun a => X - C a] rw [← Multiset.filter_eq] refine (Multiset.prod_dvd_prod_of_le <| Multiset.map_le_map <| Multiset.filter_le (Eq b) _).trans ?_ convert Polynomial.map_dvd f p.prod_multiset_X_sub_C_dvd simp only [Polynomial.map_multiset_prod, Multiset.map_map] congr; ext1 simp only [Function.comp_apply, Polynomial.map_sub, map_X, map_C] theorem count_map_roots_of_injective [IsDomain A] [DecidableEq B] (p : A[X]) {f : A →+* B} (hf : Function.Injective f) (b : B) : (p.roots.map f).count b ≤ rootMultiplicity b (p.map f) := by by_cases hp0 : p = 0 · simp only [hp0, roots_zero, Multiset.map_zero, Multiset.count_zero, Polynomial.map_zero, rootMultiplicity_zero, le_refl] · exact count_map_roots ((Polynomial.map_ne_zero_iff hf).mpr hp0) b theorem map_roots_le [IsDomain A] [IsDomain B] {p : A[X]} {f : A →+* B} (h : p.map f ≠ 0) : p.roots.map f ≤ (p.map f).roots := by classical exact Multiset.le_iff_count.2 fun b => by rw [count_roots] apply count_map_roots h theorem map_roots_le_of_injective [IsDomain A] [IsDomain B] (p : A[X]) {f : A →+* B} (hf : Function.Injective f) : p.roots.map f ≤ (p.map f).roots := by by_cases hp0 : p = 0 · simp only [hp0, roots_zero, Multiset.map_zero, Polynomial.map_zero, le_rfl] exact map_roots_le ((Polynomial.map_ne_zero_iff hf).mpr hp0) theorem card_roots_le_map [IsDomain A] [IsDomain B] {p : A[X]} {f : A →+* B} (h : p.map f ≠ 0) : Multiset.card p.roots ≤ Multiset.card (p.map f).roots := by rw [← p.roots.card_map f] exact Multiset.card_le_card (map_roots_le h)
theorem card_roots_le_map_of_injective [IsDomain A] [IsDomain B] {p : A[X]} {f : A →+* B} (hf : Function.Injective f) : Multiset.card p.roots ≤ Multiset.card (p.map f).roots := by by_cases hp0 : p = 0 · simp only [hp0, roots_zero, Polynomial.map_zero, Multiset.card_zero, le_rfl] exact card_roots_le_map ((Polynomial.map_ne_zero_iff hf).mpr hp0) theorem roots_map_of_injective_of_card_eq_natDegree [IsDomain A] [IsDomain B] {p : A[X]}
Mathlib/Algebra/Polynomial/Roots.lean
782
788
/- Copyright (c) 2019 Kim Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kim Morrison, Jakob von Raumer -/ import Mathlib.CategoryTheory.Limits.Shapes.FiniteProducts import Mathlib.CategoryTheory.Limits.Shapes.Kernels /-! # Biproducts and binary biproducts We introduce the notion of (finite) biproducts. Binary biproducts are defined in `CategoryTheory.Limits.Shapes.BinaryBiproducts`. These are slightly unusual relative to the other shapes in the library, as they are simultaneously limits and colimits. (Zero objects are similar; they are "biterminal".) For results about biproducts in preadditive categories see `CategoryTheory.Preadditive.Biproducts`. For biproducts indexed by a `Fintype J`, a `bicone` consists of a cone point `X` and morphisms `π j : X ⟶ F j` and `ι j : F j ⟶ X` for each `j`, such that `ι j ≫ π j'` is the identity when `j = j'` and zero otherwise. ## Notation As `⊕` is already taken for the sum of types, we introduce the notation `X ⊞ Y` for a binary biproduct. We introduce `⨁ f` for the indexed biproduct. ## Implementation notes Prior to https://github.com/leanprover-community/mathlib3/pull/14046, `HasFiniteBiproducts` required a `DecidableEq` instance on the indexing type. As this had no pay-off (everything about limits is non-constructive in mathlib), and occasional cost (constructing decidability instances appropriate for constructions involving the indexing type), we made everything classical. -/ noncomputable section universe w w' v u open CategoryTheory Functor namespace CategoryTheory.Limits variable {J : Type w} universe uC' uC uD' uD variable {C : Type uC} [Category.{uC'} C] [HasZeroMorphisms C] variable {D : Type uD} [Category.{uD'} D] [HasZeroMorphisms D] open scoped Classical in /-- A `c : Bicone F` is: * an object `c.pt` and * morphisms `π j : pt ⟶ F j` and `ι j : F j ⟶ pt` for each `j`, * such that `ι j ≫ π j'` is the identity when `j = j'` and zero otherwise. -/ structure Bicone (F : J → C) where pt : C π : ∀ j, pt ⟶ F j ι : ∀ j, F j ⟶ pt ι_π : ∀ j j', ι j ≫ π j' = if h : j = j' then eqToHom (congrArg F h) else 0 := by aesop attribute [inherit_doc Bicone] Bicone.pt Bicone.π Bicone.ι Bicone.ι_π @[reassoc (attr := simp)] theorem bicone_ι_π_self {F : J → C} (B : Bicone F) (j : J) : B.ι j ≫ B.π j = 𝟙 (F j) := by simpa using B.ι_π j j @[reassoc (attr := simp)] theorem bicone_ι_π_ne {F : J → C} (B : Bicone F) {j j' : J} (h : j ≠ j') : B.ι j ≫ B.π j' = 0 := by simpa [h] using B.ι_π j j' variable {F : J → C} /-- A bicone morphism between two bicones for the same diagram is a morphism of the bicone points which commutes with the cone and cocone legs. -/ structure BiconeMorphism {F : J → C} (A B : Bicone F) where /-- A morphism between the two vertex objects of the bicones -/ hom : A.pt ⟶ B.pt /-- The triangle consisting of the two natural transformations and `hom` commutes -/ wπ : ∀ j : J, hom ≫ B.π j = A.π j := by aesop_cat /-- The triangle consisting of the two natural transformations and `hom` commutes -/ wι : ∀ j : J, A.ι j ≫ hom = B.ι j := by aesop_cat attribute [reassoc (attr := simp)] BiconeMorphism.wι BiconeMorphism.wπ /-- The category of bicones on a given diagram. -/ @[simps] instance Bicone.category : Category (Bicone F) where Hom A B := BiconeMorphism A B comp f g := { hom := f.hom ≫ g.hom } id B := { hom := 𝟙 B.pt } -- Porting note: if we do not have `simps` automatically generate the lemma for simplifying -- the `hom` field of a category, we need to write the `ext` lemma in terms of the categorical -- morphism, rather than the underlying structure. @[ext] theorem BiconeMorphism.ext {c c' : Bicone F} (f g : c ⟶ c') (w : f.hom = g.hom) : f = g := by cases f cases g congr namespace Bicones /-- To give an isomorphism between cocones, it suffices to give an isomorphism between their vertices which commutes with the cocone maps. -/ @[aesop apply safe (rule_sets := [CategoryTheory]), simps] def ext {c c' : Bicone F} (φ : c.pt ≅ c'.pt) (wι : ∀ j, c.ι j ≫ φ.hom = c'.ι j := by aesop_cat) (wπ : ∀ j, φ.hom ≫ c'.π j = c.π j := by aesop_cat) : c ≅ c' where hom := { hom := φ.hom } inv := { hom := φ.inv wι := fun j => φ.comp_inv_eq.mpr (wι j).symm wπ := fun j => φ.inv_comp_eq.mpr (wπ j).symm } variable (F) in /-- A functor `G : C ⥤ D` sends bicones over `F` to bicones over `G.obj ∘ F` functorially. -/ @[simps] def functoriality (G : C ⥤ D) [Functor.PreservesZeroMorphisms G] : Bicone F ⥤ Bicone (G.obj ∘ F) where obj A := { pt := G.obj A.pt π := fun j => G.map (A.π j) ι := fun j => G.map (A.ι j) ι_π := fun i j => (Functor.map_comp _ _ _).symm.trans <| by rw [A.ι_π] aesop_cat } map f := { hom := G.map f.hom wπ := fun j => by simp [-BiconeMorphism.wπ, ← f.wπ j] wι := fun j => by simp [-BiconeMorphism.wι, ← f.wι j] } variable (G : C ⥤ D) instance functoriality_full [G.PreservesZeroMorphisms] [G.Full] [G.Faithful] : (functoriality F G).Full where map_surjective t := ⟨{ hom := G.preimage t.hom wι := fun j => G.map_injective (by simpa using t.wι j) wπ := fun j => G.map_injective (by simpa using t.wπ j) }, by aesop_cat⟩ instance functoriality_faithful [G.PreservesZeroMorphisms] [G.Faithful] : (functoriality F G).Faithful where map_injective {_X} {_Y} f g h := BiconeMorphism.ext f g <| G.map_injective <| congr_arg BiconeMorphism.hom h end Bicones namespace Bicone attribute [local aesop safe tactic (rule_sets := [CategoryTheory])] CategoryTheory.Discrete.discreteCases -- Porting note: would it be okay to use this more generally? attribute [local aesop safe cases (rule_sets := [CategoryTheory])] Eq /-- Extract the cone from a bicone. -/ def toConeFunctor : Bicone F ⥤ Cone (Discrete.functor F) where obj B := { pt := B.pt, π := { app := fun j => B.π j.as } } map {_ _} F := { hom := F.hom, w := fun _ => F.wπ _ } /-- A shorthand for `toConeFunctor.obj` -/ abbrev toCone (B : Bicone F) : Cone (Discrete.functor F) := toConeFunctor.obj B -- TODO Consider changing this API to `toFan (B : Bicone F) : Fan F`. @[simp] theorem toCone_pt (B : Bicone F) : B.toCone.pt = B.pt := rfl @[simp] theorem toCone_π_app (B : Bicone F) (j : Discrete J) : B.toCone.π.app j = B.π j.as := rfl theorem toCone_π_app_mk (B : Bicone F) (j : J) : B.toCone.π.app ⟨j⟩ = B.π j := rfl @[simp] theorem toCone_proj (B : Bicone F) (j : J) : Fan.proj B.toCone j = B.π j := rfl /-- Extract the cocone from a bicone. -/ def toCoconeFunctor : Bicone F ⥤ Cocone (Discrete.functor F) where obj B := { pt := B.pt, ι := { app := fun j => B.ι j.as } } map {_ _} F := { hom := F.hom, w := fun _ => F.wι _ } /-- A shorthand for `toCoconeFunctor.obj` -/ abbrev toCocone (B : Bicone F) : Cocone (Discrete.functor F) := toCoconeFunctor.obj B @[simp] theorem toCocone_pt (B : Bicone F) : B.toCocone.pt = B.pt := rfl @[simp] theorem toCocone_ι_app (B : Bicone F) (j : Discrete J) : B.toCocone.ι.app j = B.ι j.as := rfl @[simp] theorem toCocone_inj (B : Bicone F) (j : J) : Cofan.inj B.toCocone j = B.ι j := rfl theorem toCocone_ι_app_mk (B : Bicone F) (j : J) : B.toCocone.ι.app ⟨j⟩ = B.ι j := rfl open scoped Classical in /-- We can turn any limit cone over a discrete collection of objects into a bicone. -/ @[simps] def ofLimitCone {f : J → C} {t : Cone (Discrete.functor f)} (ht : IsLimit t) : Bicone f where pt := t.pt π j := t.π.app ⟨j⟩ ι j := ht.lift (Fan.mk _ fun j' => if h : j = j' then eqToHom (congr_arg f h) else 0) ι_π j j' := by simp open scoped Classical in theorem ι_of_isLimit {f : J → C} {t : Bicone f} (ht : IsLimit t.toCone) (j : J) : t.ι j = ht.lift (Fan.mk _ fun j' => if h : j = j' then eqToHom (congr_arg f h) else 0) := ht.hom_ext fun j' => by rw [ht.fac] simp [t.ι_π] open scoped Classical in /-- We can turn any colimit cocone over a discrete collection of objects into a bicone. -/ @[simps] def ofColimitCocone {f : J → C} {t : Cocone (Discrete.functor f)} (ht : IsColimit t) : Bicone f where pt := t.pt π j := ht.desc (Cofan.mk _ fun j' => if h : j' = j then eqToHom (congr_arg f h) else 0) ι j := t.ι.app ⟨j⟩ ι_π j j' := by simp open scoped Classical in theorem π_of_isColimit {f : J → C} {t : Bicone f} (ht : IsColimit t.toCocone) (j : J) : t.π j = ht.desc (Cofan.mk _ fun j' => if h : j' = j then eqToHom (congr_arg f h) else 0) := ht.hom_ext fun j' => by rw [ht.fac] simp [t.ι_π] /-- Structure witnessing that a bicone is both a limit cone and a colimit cocone. -/ structure IsBilimit {F : J → C} (B : Bicone F) where isLimit : IsLimit B.toCone isColimit : IsColimit B.toCocone attribute [inherit_doc IsBilimit] IsBilimit.isLimit IsBilimit.isColimit attribute [simp] IsBilimit.mk.injEq attribute [local ext] Bicone.IsBilimit instance subsingleton_isBilimit {f : J → C} {c : Bicone f} : Subsingleton c.IsBilimit := ⟨fun _ _ => Bicone.IsBilimit.ext (Subsingleton.elim _ _) (Subsingleton.elim _ _)⟩ section Whisker variable {K : Type w'} /-- Whisker a bicone with an equivalence between the indexing types. -/ @[simps] def whisker {f : J → C} (c : Bicone f) (g : K ≃ J) : Bicone (f ∘ g) where pt := c.pt π k := c.π (g k) ι k := c.ι (g k) ι_π k k' := by simp only [c.ι_π] split_ifs with h h' h' <;> simp [Equiv.apply_eq_iff_eq g] at h h' <;> tauto /-- Taking the cone of a whiskered bicone results in a cone isomorphic to one gained by whiskering the cone and postcomposing with a suitable isomorphism. -/ def whiskerToCone {f : J → C} (c : Bicone f) (g : K ≃ J) : (c.whisker g).toCone ≅ (Cones.postcompose (Discrete.functorComp f g).inv).obj (c.toCone.whisker (Discrete.functor (Discrete.mk ∘ g))) := Cones.ext (Iso.refl _) (by simp) /-- Taking the cocone of a whiskered bicone results in a cone isomorphic to one gained by whiskering the cocone and precomposing with a suitable isomorphism. -/ def whiskerToCocone {f : J → C} (c : Bicone f) (g : K ≃ J) : (c.whisker g).toCocone ≅ (Cocones.precompose (Discrete.functorComp f g).hom).obj (c.toCocone.whisker (Discrete.functor (Discrete.mk ∘ g))) := Cocones.ext (Iso.refl _) (by simp) /-- Whiskering a bicone with an equivalence between types preserves being a bilimit bicone. -/ noncomputable def whiskerIsBilimitIff {f : J → C} (c : Bicone f) (g : K ≃ J) : (c.whisker g).IsBilimit ≃ c.IsBilimit := by refine equivOfSubsingletonOfSubsingleton (fun hc => ⟨?_, ?_⟩) fun hc => ⟨?_, ?_⟩ · let this := IsLimit.ofIsoLimit hc.isLimit (Bicone.whiskerToCone c g) let this := (IsLimit.postcomposeHomEquiv (Discrete.functorComp f g).symm _) this exact IsLimit.ofWhiskerEquivalence (Discrete.equivalence g) this · let this := IsColimit.ofIsoColimit hc.isColimit (Bicone.whiskerToCocone c g) let this := (IsColimit.precomposeHomEquiv (Discrete.functorComp f g) _) this exact IsColimit.ofWhiskerEquivalence (Discrete.equivalence g) this · apply IsLimit.ofIsoLimit _ (Bicone.whiskerToCone c g).symm apply (IsLimit.postcomposeHomEquiv (Discrete.functorComp f g).symm _).symm _ exact IsLimit.whiskerEquivalence hc.isLimit (Discrete.equivalence g) · apply IsColimit.ofIsoColimit _ (Bicone.whiskerToCocone c g).symm apply (IsColimit.precomposeHomEquiv (Discrete.functorComp f g) _).symm _ exact IsColimit.whiskerEquivalence hc.isColimit (Discrete.equivalence g) end Whisker end Bicone /-- A bicone over `F : J → C`, which is both a limit cone and a colimit cocone. -/ structure LimitBicone (F : J → C) where bicone : Bicone F isBilimit : bicone.IsBilimit attribute [inherit_doc LimitBicone] LimitBicone.bicone LimitBicone.isBilimit /-- `HasBiproduct F` expresses the mere existence of a bicone which is simultaneously a limit and a colimit of the diagram `F`. -/ class HasBiproduct (F : J → C) : Prop where mk' :: exists_biproduct : Nonempty (LimitBicone F) attribute [inherit_doc HasBiproduct] HasBiproduct.exists_biproduct theorem HasBiproduct.mk {F : J → C} (d : LimitBicone F) : HasBiproduct F := ⟨Nonempty.intro d⟩ /-- Use the axiom of choice to extract explicit `BiproductData F` from `HasBiproduct F`. -/ def getBiproductData (F : J → C) [HasBiproduct F] : LimitBicone F := Classical.choice HasBiproduct.exists_biproduct /-- A bicone for `F` which is both a limit cone and a colimit cocone. -/ def biproduct.bicone (F : J → C) [HasBiproduct F] : Bicone F := (getBiproductData F).bicone /-- `biproduct.bicone F` is a bilimit bicone. -/ def biproduct.isBilimit (F : J → C) [HasBiproduct F] : (biproduct.bicone F).IsBilimit := (getBiproductData F).isBilimit /-- `biproduct.bicone F` is a limit cone. -/ def biproduct.isLimit (F : J → C) [HasBiproduct F] : IsLimit (biproduct.bicone F).toCone := (getBiproductData F).isBilimit.isLimit /-- `biproduct.bicone F` is a colimit cocone. -/ def biproduct.isColimit (F : J → C) [HasBiproduct F] : IsColimit (biproduct.bicone F).toCocone := (getBiproductData F).isBilimit.isColimit instance (priority := 100) hasProduct_of_hasBiproduct [HasBiproduct F] : HasProduct F := HasLimit.mk { cone := (biproduct.bicone F).toCone isLimit := biproduct.isLimit F } instance (priority := 100) hasCoproduct_of_hasBiproduct [HasBiproduct F] : HasCoproduct F := HasColimit.mk { cocone := (biproduct.bicone F).toCocone isColimit := biproduct.isColimit F } variable (J C) /-- `C` has biproducts of shape `J` if we have a limit and a colimit, with the same cone points, of every function `F : J → C`. -/ class HasBiproductsOfShape : Prop where has_biproduct : ∀ F : J → C, HasBiproduct F attribute [instance 100] HasBiproductsOfShape.has_biproduct /-- `HasFiniteBiproducts C` represents a choice of biproduct for every family of objects in `C` indexed by a finite type. -/ class HasFiniteBiproducts : Prop where out : ∀ n, HasBiproductsOfShape (Fin n) C attribute [inherit_doc HasFiniteBiproducts] HasFiniteBiproducts.out variable {J} theorem hasBiproductsOfShape_of_equiv {K : Type w'} [HasBiproductsOfShape K C] (e : J ≃ K) : HasBiproductsOfShape J C := ⟨fun F => let ⟨⟨h⟩⟩ := HasBiproductsOfShape.has_biproduct (F ∘ e.symm) let ⟨c, hc⟩ := h HasBiproduct.mk <| by simpa only [Function.comp_def, e.symm_apply_apply] using LimitBicone.mk (c.whisker e) ((c.whiskerIsBilimitIff _).2 hc)⟩ instance (priority := 100) hasBiproductsOfShape_finite [HasFiniteBiproducts C] [Finite J] : HasBiproductsOfShape J C := by rcases Finite.exists_equiv_fin J with ⟨n, ⟨e⟩⟩ haveI : HasBiproductsOfShape (Fin n) C := HasFiniteBiproducts.out n exact hasBiproductsOfShape_of_equiv C e instance (priority := 100) hasFiniteProducts_of_hasFiniteBiproducts [HasFiniteBiproducts C] : HasFiniteProducts C where out _ := ⟨fun _ => hasLimit_of_iso Discrete.natIsoFunctor.symm⟩ instance (priority := 100) hasFiniteCoproducts_of_hasFiniteBiproducts [HasFiniteBiproducts C] : HasFiniteCoproducts C where out _ := ⟨fun _ => hasColimit_of_iso Discrete.natIsoFunctor⟩ instance (priority := 100) hasProductsOfShape_of_hasBiproductsOfShape [HasBiproductsOfShape J C] : HasProductsOfShape J C where has_limit _ := hasLimit_of_iso Discrete.natIsoFunctor.symm instance (priority := 100) hasCoproductsOfShape_of_hasBiproductsOfShape [HasBiproductsOfShape J C] : HasCoproductsOfShape J C where has_colimit _ := hasColimit_of_iso Discrete.natIsoFunctor variable {C} /-- The isomorphism between the specified limit and the specified colimit for a functor with a bilimit. -/ def biproductIso (F : J → C) [HasBiproduct F] : Limits.piObj F ≅ Limits.sigmaObj F := (IsLimit.conePointUniqueUpToIso (limit.isLimit _) (biproduct.isLimit F)).trans <| IsColimit.coconePointUniqueUpToIso (biproduct.isColimit F) (colimit.isColimit _) variable {J : Type w} {K : Type*} variable {C : Type u} [Category.{v} C] [HasZeroMorphisms C] /-- `biproduct f` computes the biproduct of a family of elements `f`. (It is defined as an abbreviation for `limit (Discrete.functor f)`, so for most facts about `biproduct f`, you will just use general facts about limits and colimits.) -/ abbrev biproduct (f : J → C) [HasBiproduct f] : C := (biproduct.bicone f).pt @[inherit_doc biproduct] notation "⨁ " f:20 => biproduct f /-- The projection onto a summand of a biproduct. -/ abbrev biproduct.π (f : J → C) [HasBiproduct f] (b : J) : ⨁ f ⟶ f b := (biproduct.bicone f).π b @[simp] theorem biproduct.bicone_π (f : J → C) [HasBiproduct f] (b : J) : (biproduct.bicone f).π b = biproduct.π f b := rfl /-- The inclusion into a summand of a biproduct. -/ abbrev biproduct.ι (f : J → C) [HasBiproduct f] (b : J) : f b ⟶ ⨁ f := (biproduct.bicone f).ι b @[simp] theorem biproduct.bicone_ι (f : J → C) [HasBiproduct f] (b : J) : (biproduct.bicone f).ι b = biproduct.ι f b := rfl /-- Note that as this lemma has an `if` in the statement, we include a `DecidableEq` argument. This means you may not be able to `simp` using this lemma unless you `open scoped Classical`. -/ @[reassoc] theorem biproduct.ι_π [DecidableEq J] (f : J → C) [HasBiproduct f] (j j' : J) : biproduct.ι f j ≫ biproduct.π f j' = if h : j = j' then eqToHom (congr_arg f h) else 0 := by convert (biproduct.bicone f).ι_π j j' @[reassoc] -- Porting note: both versions proven by simp theorem biproduct.ι_π_self (f : J → C) [HasBiproduct f] (j : J) : biproduct.ι f j ≫ biproduct.π f j = 𝟙 _ := by simp [biproduct.ι_π] @[reassoc (attr := simp)] theorem biproduct.ι_π_ne (f : J → C) [HasBiproduct f] {j j' : J} (h : j ≠ j') : biproduct.ι f j ≫ biproduct.π f j' = 0 := by simp [biproduct.ι_π, h] -- The `simpNF` linter incorrectly identifies these as simp lemmas that could never apply. -- It seems the side condition `w` is not applied by `simpNF`. -- https://github.com/leanprover-community/mathlib4/issues/5049 -- They are used by `simp` in `biproduct.whiskerEquiv` below. @[reassoc (attr := simp, nolint simpNF)] theorem biproduct.eqToHom_comp_ι (f : J → C) [HasBiproduct f] {j j' : J} (w : j = j') : eqToHom (by simp [w]) ≫ biproduct.ι f j' = biproduct.ι f j := by cases w simp -- The `simpNF` linter incorrectly identifies these as simp lemmas that could never apply. -- It seems the side condition `w` is not applied by `simpNF`. -- https://github.com/leanprover-community/mathlib4/issues/5049 -- They are used by `simp` in `biproduct.whiskerEquiv` below. @[reassoc (attr := simp, nolint simpNF)] theorem biproduct.π_comp_eqToHom (f : J → C) [HasBiproduct f] {j j' : J} (w : j = j') : biproduct.π f j ≫ eqToHom (by simp [w]) = biproduct.π f j' := by cases w simp /-- Given a collection of maps into the summands, we obtain a map into the biproduct. -/ abbrev biproduct.lift {f : J → C} [HasBiproduct f] {P : C} (p : ∀ b, P ⟶ f b) : P ⟶ ⨁ f := (biproduct.isLimit f).lift (Fan.mk P p) /-- Given a collection of maps out of the summands, we obtain a map out of the biproduct. -/ abbrev biproduct.desc {f : J → C} [HasBiproduct f] {P : C} (p : ∀ b, f b ⟶ P) : ⨁ f ⟶ P := (biproduct.isColimit f).desc (Cofan.mk P p) @[reassoc (attr := simp)] theorem biproduct.lift_π {f : J → C} [HasBiproduct f] {P : C} (p : ∀ b, P ⟶ f b) (j : J) : biproduct.lift p ≫ biproduct.π f j = p j := (biproduct.isLimit f).fac _ ⟨j⟩ @[reassoc (attr := simp)] theorem biproduct.ι_desc {f : J → C} [HasBiproduct f] {P : C} (p : ∀ b, f b ⟶ P) (j : J) : biproduct.ι f j ≫ biproduct.desc p = p j := (biproduct.isColimit f).fac _ ⟨j⟩ /-- Given a collection of maps between corresponding summands of a pair of biproducts indexed by the same type, we obtain a map between the biproducts. -/ abbrev biproduct.map {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ b, f b ⟶ g b) : ⨁ f ⟶ ⨁ g := IsLimit.map (biproduct.bicone f).toCone (biproduct.isLimit g) (Discrete.natTrans (fun j => p j.as)) /-- An alternative to `biproduct.map` constructed via colimits. This construction only exists in order to show it is equal to `biproduct.map`. -/ abbrev biproduct.map' {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ b, f b ⟶ g b) : ⨁ f ⟶ ⨁ g := IsColimit.map (biproduct.isColimit f) (biproduct.bicone g).toCocone (Discrete.natTrans fun j => p j.as) -- We put this at slightly higher priority than `biproduct.hom_ext'`, -- to get the matrix indices in the "right" order. @[ext 1001] theorem biproduct.hom_ext {f : J → C} [HasBiproduct f] {Z : C} (g h : Z ⟶ ⨁ f) (w : ∀ j, g ≫ biproduct.π f j = h ≫ biproduct.π f j) : g = h := (biproduct.isLimit f).hom_ext fun j => w j.as @[ext] theorem biproduct.hom_ext' {f : J → C} [HasBiproduct f] {Z : C} (g h : ⨁ f ⟶ Z) (w : ∀ j, biproduct.ι f j ≫ g = biproduct.ι f j ≫ h) : g = h := (biproduct.isColimit f).hom_ext fun j => w j.as /-- The canonical isomorphism between the chosen biproduct and the chosen product. -/ def biproduct.isoProduct (f : J → C) [HasBiproduct f] : ⨁ f ≅ ∏ᶜ f := IsLimit.conePointUniqueUpToIso (biproduct.isLimit f) (limit.isLimit _) @[simp] theorem biproduct.isoProduct_hom {f : J → C} [HasBiproduct f] : (biproduct.isoProduct f).hom = Pi.lift (biproduct.π f) := limit.hom_ext fun j => by simp [biproduct.isoProduct] @[simp] theorem biproduct.isoProduct_inv {f : J → C} [HasBiproduct f] : (biproduct.isoProduct f).inv = biproduct.lift (Pi.π f) := biproduct.hom_ext _ _ fun j => by simp [Iso.inv_comp_eq] /-- The canonical isomorphism between the chosen biproduct and the chosen coproduct. -/ def biproduct.isoCoproduct (f : J → C) [HasBiproduct f] : ⨁ f ≅ ∐ f := IsColimit.coconePointUniqueUpToIso (biproduct.isColimit f) (colimit.isColimit _) @[simp] theorem biproduct.isoCoproduct_inv {f : J → C} [HasBiproduct f] : (biproduct.isoCoproduct f).inv = Sigma.desc (biproduct.ι f) := colimit.hom_ext fun j => by simp [biproduct.isoCoproduct] @[simp] theorem biproduct.isoCoproduct_hom {f : J → C} [HasBiproduct f] : (biproduct.isoCoproduct f).hom = biproduct.desc (Sigma.ι f) := biproduct.hom_ext' _ _ fun j => by simp [← Iso.eq_comp_inv] /-- If a category has biproducts of a shape `J`, its `colim` and `lim` functor on diagrams over `J` are isomorphic. -/ @[simps!] def HasBiproductsOfShape.colimIsoLim [HasBiproductsOfShape J C] : colim (J := Discrete J) (C := C) ≅ lim := NatIso.ofComponents (fun F => (Sigma.isoColimit F).symm ≪≫ (biproduct.isoCoproduct _).symm ≪≫ biproduct.isoProduct _ ≪≫ Pi.isoLimit F) fun η => colimit.hom_ext fun ⟨i⟩ => limit.hom_ext fun ⟨j⟩ => by classical by_cases h : i = j <;> simp_all [h, Sigma.isoColimit, Pi.isoLimit, biproduct.ι_π, biproduct.ι_π_assoc] theorem biproduct.map_eq_map' {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ b, f b ⟶ g b) : biproduct.map p = biproduct.map' p := by classical ext dsimp simp only [Discrete.natTrans_app, Limits.IsColimit.ι_map_assoc, Limits.IsLimit.map_π, Category.assoc, ← Bicone.toCone_π_app_mk, ← biproduct.bicone_π, ← Bicone.toCocone_ι_app_mk, ← biproduct.bicone_ι] dsimp rw [biproduct.ι_π_assoc, biproduct.ι_π] split_ifs with h · subst h; simp · simp @[reassoc (attr := simp)] theorem biproduct.map_π {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ j, f j ⟶ g j) (j : J) : biproduct.map p ≫ biproduct.π g j = biproduct.π f j ≫ p j := Limits.IsLimit.map_π _ _ _ (Discrete.mk j) @[reassoc (attr := simp)] theorem biproduct.ι_map {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ j, f j ⟶ g j) (j : J) : biproduct.ι f j ≫ biproduct.map p = p j ≫ biproduct.ι g j := by rw [biproduct.map_eq_map'] apply Limits.IsColimit.ι_map (biproduct.isColimit f) (biproduct.bicone g).toCocone (Discrete.natTrans fun j => p j.as) (Discrete.mk j) @[reassoc (attr := simp)] theorem biproduct.map_desc {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ j, f j ⟶ g j) {P : C} (k : ∀ j, g j ⟶ P) : biproduct.map p ≫ biproduct.desc k = biproduct.desc fun j => p j ≫ k j := by ext; simp @[reassoc (attr := simp)] theorem biproduct.lift_map {f g : J → C} [HasBiproduct f] [HasBiproduct g] {P : C} (k : ∀ j, P ⟶ f j) (p : ∀ j, f j ⟶ g j) : biproduct.lift k ≫ biproduct.map p = biproduct.lift fun j => k j ≫ p j := by ext; simp /-- Given a collection of isomorphisms between corresponding summands of a pair of biproducts indexed by the same type, we obtain an isomorphism between the biproducts. -/ @[simps] def biproduct.mapIso {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ b, f b ≅ g b) : ⨁ f ≅ ⨁ g where hom := biproduct.map fun b => (p b).hom inv := biproduct.map fun b => (p b).inv instance biproduct.map_epi {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ j, f j ⟶ g j) [∀ j, Epi (p j)] : Epi (biproduct.map p) := by classical have : biproduct.map p = (biproduct.isoCoproduct _).hom ≫ Sigma.map p ≫ (biproduct.isoCoproduct _).inv := by ext simp only [map_π, isoCoproduct_hom, isoCoproduct_inv, Category.assoc, ι_desc_assoc, ι_colimMap_assoc, Discrete.functor_obj_eq_as, Discrete.natTrans_app, colimit.ι_desc_assoc, Cofan.mk_pt, Cofan.mk_ι_app, ι_π, ι_π_assoc] split all_goals simp_all rw [this] infer_instance instance Pi.map_epi {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ j, f j ⟶ g j) [∀ j, Epi (p j)] : Epi (Pi.map p) := by rw [show Pi.map p = (biproduct.isoProduct _).inv ≫ biproduct.map p ≫ (biproduct.isoProduct _).hom by aesop] infer_instance instance biproduct.map_mono {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ j, f j ⟶ g j) [∀ j, Mono (p j)] : Mono (biproduct.map p) := by rw [show biproduct.map p = (biproduct.isoProduct _).hom ≫ Pi.map p ≫ (biproduct.isoProduct _).inv by aesop] infer_instance instance Sigma.map_mono {f g : J → C} [HasBiproduct f] [HasBiproduct g] (p : ∀ j, f j ⟶ g j) [∀ j, Mono (p j)] : Mono (Sigma.map p) := by rw [show Sigma.map p = (biproduct.isoCoproduct _).inv ≫ biproduct.map p ≫ (biproduct.isoCoproduct _).hom by aesop] infer_instance /-- Two biproducts which differ by an equivalence in the indexing type, and up to isomorphism in the factors, are isomorphic. Unfortunately there are two natural ways to define each direction of this isomorphism (because it is true for both products and coproducts separately). We give the alternative definitions as lemmas below. -/ @[simps] def biproduct.whiskerEquiv {f : J → C} {g : K → C} (e : J ≃ K) (w : ∀ j, g (e j) ≅ f j) [HasBiproduct f] [HasBiproduct g] : ⨁ f ≅ ⨁ g where hom := biproduct.desc fun j => (w j).inv ≫ biproduct.ι g (e j) inv := biproduct.desc fun k => eqToHom (by simp) ≫ (w (e.symm k)).hom ≫ biproduct.ι f _ lemma biproduct.whiskerEquiv_hom_eq_lift {f : J → C} {g : K → C} (e : J ≃ K) (w : ∀ j, g (e j) ≅ f j) [HasBiproduct f] [HasBiproduct g] : (biproduct.whiskerEquiv e w).hom = biproduct.lift fun k => biproduct.π f (e.symm k) ≫ (w _).inv ≫ eqToHom (by simp) := by simp only [whiskerEquiv_hom] ext k j by_cases h : k = e j · subst h simp · simp only [ι_desc_assoc, Category.assoc, ne_eq, lift_π] rw [biproduct.ι_π_ne, biproduct.ι_π_ne_assoc] · simp · rintro rfl simp at h · exact Ne.symm h lemma biproduct.whiskerEquiv_inv_eq_lift {f : J → C} {g : K → C} (e : J ≃ K) (w : ∀ j, g (e j) ≅ f j) [HasBiproduct f] [HasBiproduct g] : (biproduct.whiskerEquiv e w).inv = biproduct.lift fun j => biproduct.π g (e j) ≫ (w j).hom := by simp only [whiskerEquiv_inv] ext j k by_cases h : k = e j · subst h simp only [ι_desc_assoc, ← eqToHom_iso_hom_naturality_assoc w (e.symm_apply_apply j).symm, Equiv.symm_apply_apply, eqToHom_comp_ι, Category.assoc, bicone_ι_π_self, Category.comp_id, lift_π, bicone_ι_π_self_assoc] · simp only [ι_desc_assoc, Category.assoc, ne_eq, lift_π] rw [biproduct.ι_π_ne, biproduct.ι_π_ne_assoc] · simp · exact h · rintro rfl simp at h attribute [local simp] Sigma.forall in instance {ι} (f : ι → Type*) (g : (i : ι) → (f i) → C) [∀ i, HasBiproduct (g i)] [HasBiproduct fun i => ⨁ g i] : HasBiproduct fun p : Σ i, f i => g p.1 p.2 where exists_biproduct := Nonempty.intro { bicone := { pt := ⨁ fun i => ⨁ g i ι := fun X => biproduct.ι (g X.1) X.2 ≫ biproduct.ι (fun i => ⨁ g i) X.1 π := fun X => biproduct.π (fun i => ⨁ g i) X.1 ≫ biproduct.π (g X.1) X.2 ι_π := fun ⟨j, x⟩ ⟨j', y⟩ => by split_ifs with h · obtain ⟨rfl, rfl⟩ := h simp · simp only [Sigma.mk.inj_iff, not_and] at h by_cases w : j = j' · cases w simp only [heq_eq_eq, forall_true_left] at h simp [biproduct.ι_π_ne _ h] · simp [biproduct.ι_π_ne_assoc _ w] } isBilimit := { isLimit := mkFanLimit _ (fun s => biproduct.lift fun b => biproduct.lift fun c => s.proj ⟨b, c⟩) isColimit := mkCofanColimit _ (fun s => biproduct.desc fun b => biproduct.desc fun c => s.inj ⟨b, c⟩) } } /-- An iterated biproduct is a biproduct over a sigma type. -/ @[simps] def biproductBiproductIso {ι} (f : ι → Type*) (g : (i : ι) → (f i) → C) [∀ i, HasBiproduct (g i)] [HasBiproduct fun i => ⨁ g i] : (⨁ fun i => ⨁ g i) ≅ (⨁ fun p : Σ i, f i => g p.1 p.2) where hom := biproduct.lift fun ⟨i, x⟩ => biproduct.π _ i ≫ biproduct.π _ x inv := biproduct.lift fun i => biproduct.lift fun x => biproduct.π _ (⟨i, x⟩ : Σ i, f i) section πKernel section variable (f : J → C) [HasBiproduct f] variable (p : J → Prop) [HasBiproduct (Subtype.restrict p f)] /-- The canonical morphism from the biproduct over a restricted index type to the biproduct of the full index type. -/ def biproduct.fromSubtype : ⨁ Subtype.restrict p f ⟶ ⨁ f := biproduct.desc fun j => biproduct.ι _ j.val /-- The canonical morphism from a biproduct to the biproduct over a restriction of its index type. -/ def biproduct.toSubtype : ⨁ f ⟶ ⨁ Subtype.restrict p f := biproduct.lift fun _ => biproduct.π _ _ @[reassoc (attr := simp)] theorem biproduct.fromSubtype_π [DecidablePred p] (j : J) : biproduct.fromSubtype f p ≫ biproduct.π f j = if h : p j then biproduct.π (Subtype.restrict p f) ⟨j, h⟩ else 0 := by classical ext i; dsimp rw [biproduct.fromSubtype, biproduct.ι_desc_assoc, biproduct.ι_π] by_cases h : p j · rw [dif_pos h, biproduct.ι_π] split_ifs with h₁ h₂ h₂ exacts [rfl, False.elim (h₂ (Subtype.ext h₁)), False.elim (h₁ (congr_arg Subtype.val h₂)), rfl] · rw [dif_neg h, dif_neg (show (i : J) ≠ j from fun h₂ => h (h₂ ▸ i.2)), comp_zero] theorem biproduct.fromSubtype_eq_lift [DecidablePred p] : biproduct.fromSubtype f p = biproduct.lift fun j => if h : p j then biproduct.π (Subtype.restrict p f) ⟨j, h⟩ else 0 := biproduct.hom_ext _ _ (by simp) @[reassoc] -- Porting note: both version solved using simp theorem biproduct.fromSubtype_π_subtype (j : Subtype p) : biproduct.fromSubtype f p ≫ biproduct.π f j = biproduct.π (Subtype.restrict p f) j := by classical ext rw [biproduct.fromSubtype, biproduct.ι_desc_assoc, biproduct.ι_π, biproduct.ι_π] split_ifs with h₁ h₂ h₂ exacts [rfl, False.elim (h₂ (Subtype.ext h₁)), False.elim (h₁ (congr_arg Subtype.val h₂)), rfl] @[reassoc (attr := simp)] theorem biproduct.toSubtype_π (j : Subtype p) : biproduct.toSubtype f p ≫ biproduct.π (Subtype.restrict p f) j = biproduct.π f j := biproduct.lift_π _ _ @[reassoc (attr := simp)] theorem biproduct.ι_toSubtype [DecidablePred p] (j : J) : biproduct.ι f j ≫ biproduct.toSubtype f p = if h : p j then biproduct.ι (Subtype.restrict p f) ⟨j, h⟩ else 0 := by classical ext i rw [biproduct.toSubtype, Category.assoc, biproduct.lift_π, biproduct.ι_π] by_cases h : p j · rw [dif_pos h, biproduct.ι_π] split_ifs with h₁ h₂ h₂ exacts [rfl, False.elim (h₂ (Subtype.ext h₁)), False.elim (h₁ (congr_arg Subtype.val h₂)), rfl] · rw [dif_neg h, dif_neg (show j ≠ i from fun h₂ => h (h₂.symm ▸ i.2)), zero_comp] theorem biproduct.toSubtype_eq_desc [DecidablePred p] : biproduct.toSubtype f p = biproduct.desc fun j => if h : p j then biproduct.ι (Subtype.restrict p f) ⟨j, h⟩ else 0 := biproduct.hom_ext' _ _ (by simp) @[reassoc] theorem biproduct.ι_toSubtype_subtype (j : Subtype p) : biproduct.ι f j ≫ biproduct.toSubtype f p = biproduct.ι (Subtype.restrict p f) j := by classical ext rw [biproduct.toSubtype, Category.assoc, biproduct.lift_π, biproduct.ι_π, biproduct.ι_π] split_ifs with h₁ h₂ h₂ exacts [rfl, False.elim (h₂ (Subtype.ext h₁)), False.elim (h₁ (congr_arg Subtype.val h₂)), rfl] @[reassoc (attr := simp)] theorem biproduct.ι_fromSubtype (j : Subtype p) : biproduct.ι (Subtype.restrict p f) j ≫ biproduct.fromSubtype f p = biproduct.ι f j := biproduct.ι_desc _ _ @[reassoc (attr := simp)] theorem biproduct.fromSubtype_toSubtype : biproduct.fromSubtype f p ≫ biproduct.toSubtype f p = 𝟙 (⨁ Subtype.restrict p f) := by refine biproduct.hom_ext _ _ fun j => ?_ rw [Category.assoc, biproduct.toSubtype_π, biproduct.fromSubtype_π_subtype, Category.id_comp] @[reassoc (attr := simp)] theorem biproduct.toSubtype_fromSubtype [DecidablePred p] : biproduct.toSubtype f p ≫ biproduct.fromSubtype f p = biproduct.map fun j => if p j then 𝟙 (f j) else 0 := by ext1 i by_cases h : p i · simp [h] · simp [h] end section variable (f : J → C) (i : J) [HasBiproduct f] [HasBiproduct (Subtype.restrict (fun j => j ≠ i) f)] open scoped Classical in /-- The kernel of `biproduct.π f i` is the inclusion from the biproduct which omits `i` from the index set `J` into the biproduct over `J`. -/ def biproduct.isLimitFromSubtype : IsLimit (KernelFork.ofι (biproduct.fromSubtype f fun j => j ≠ i) (by simp) : KernelFork (biproduct.π f i)) := Fork.IsLimit.mk' _ fun s => ⟨s.ι ≫ biproduct.toSubtype _ _, by apply biproduct.hom_ext; intro j rw [KernelFork.ι_ofι, Category.assoc, Category.assoc, biproduct.toSubtype_fromSubtype_assoc, biproduct.map_π] rcases Classical.em (i = j) with (rfl | h)
· rw [if_neg (Classical.not_not.2 rfl), comp_zero, comp_zero, KernelFork.condition] · rw [if_pos (Ne.symm h), Category.comp_id], by intro m hm rw [← hm, KernelFork.ι_ofι, Category.assoc, biproduct.fromSubtype_toSubtype]
Mathlib/CategoryTheory/Limits/Shapes/Biproducts.lean
816
819
/- Copyright (c) 2018 Ellen Arlt. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Ellen Arlt, Blair Shi, Sean Leather, Mario Carneiro, Johan Commelin, Lu-Ming Zhang -/ import Mathlib.Algebra.Algebra.Opposite import Mathlib.Algebra.Algebra.Pi import Mathlib.Algebra.BigOperators.RingEquiv import Mathlib.Data.Finite.Prod import Mathlib.Data.Matrix.Mul import Mathlib.LinearAlgebra.Pi /-! # Matrices This file contains basic results on matrices including bundled versions of matrix operators. ## Implementation notes For convenience, `Matrix m n α` is defined as `m → n → α`, as this allows elements of the matrix to be accessed with `A i j`. However, it is not advisable to _construct_ matrices using terms of the form `fun i j ↦ _` or even `(fun i j ↦ _ : Matrix m n α)`, as these are not recognized by Lean as having the right type. Instead, `Matrix.of` should be used. ## TODO Under various conditions, multiplication of infinite matrices makes sense. These have not yet been implemented. -/ assert_not_exists Star universe u u' v w variable {l m n o : Type*} {m' : o → Type*} {n' : o → Type*} variable {R : Type*} {S : Type*} {α : Type v} {β : Type w} {γ : Type*} namespace Matrix instance decidableEq [DecidableEq α] [Fintype m] [Fintype n] : DecidableEq (Matrix m n α) := Fintype.decidablePiFintype instance {n m} [Fintype m] [DecidableEq m] [Fintype n] [DecidableEq n] (α) [Fintype α] : Fintype (Matrix m n α) := inferInstanceAs (Fintype (m → n → α)) instance {n m} [Finite m] [Finite n] (α) [Finite α] : Finite (Matrix m n α) := inferInstanceAs (Finite (m → n → α)) section variable (R) /-- This is `Matrix.of` bundled as a linear equivalence. -/ def ofLinearEquiv [Semiring R] [AddCommMonoid α] [Module R α] : (m → n → α) ≃ₗ[R] Matrix m n α where __ := ofAddEquiv map_smul' _ _ := rfl @[simp] lemma coe_ofLinearEquiv [Semiring R] [AddCommMonoid α] [Module R α] : ⇑(ofLinearEquiv _ : (m → n → α) ≃ₗ[R] Matrix m n α) = of := rfl @[simp] lemma coe_ofLinearEquiv_symm [Semiring R] [AddCommMonoid α] [Module R α] : ⇑((ofLinearEquiv _).symm : Matrix m n α ≃ₗ[R] (m → n → α)) = of.symm := rfl end theorem sum_apply [AddCommMonoid α] (i : m) (j : n) (s : Finset β) (g : β → Matrix m n α) : (∑ c ∈ s, g c) i j = ∑ c ∈ s, g c i j := (congr_fun (s.sum_apply i g) j).trans (s.sum_apply j _) end Matrix open Matrix namespace Matrix section Diagonal variable [DecidableEq n] variable (n α) /-- `Matrix.diagonal` as an `AddMonoidHom`. -/ @[simps] def diagonalAddMonoidHom [AddZeroClass α] : (n → α) →+ Matrix n n α where toFun := diagonal map_zero' := diagonal_zero map_add' x y := (diagonal_add x y).symm variable (R) /-- `Matrix.diagonal` as a `LinearMap`. -/ @[simps] def diagonalLinearMap [Semiring R] [AddCommMonoid α] [Module R α] : (n → α) →ₗ[R] Matrix n n α := { diagonalAddMonoidHom n α with map_smul' := diagonal_smul } variable {n α R} section One variable [Zero α] [One α] lemma zero_le_one_elem [Preorder α] [ZeroLEOneClass α] (i j : n) : 0 ≤ (1 : Matrix n n α) i j := by by_cases hi : i = j · subst hi simp · simp [hi] lemma zero_le_one_row [Preorder α] [ZeroLEOneClass α] (i : n) : 0 ≤ (1 : Matrix n n α) i := zero_le_one_elem i end One end Diagonal section Diag variable (n α) /-- `Matrix.diag` as an `AddMonoidHom`. -/ @[simps] def diagAddMonoidHom [AddZeroClass α] : Matrix n n α →+ n → α where toFun := diag map_zero' := diag_zero map_add' := diag_add variable (R) /-- `Matrix.diag` as a `LinearMap`. -/ @[simps] def diagLinearMap [Semiring R] [AddCommMonoid α] [Module R α] : Matrix n n α →ₗ[R] n → α := { diagAddMonoidHom n α with map_smul' := diag_smul } variable {n α R} @[simp] theorem diag_list_sum [AddMonoid α] (l : List (Matrix n n α)) : diag l.sum = (l.map diag).sum := map_list_sum (diagAddMonoidHom n α) l @[simp] theorem diag_multiset_sum [AddCommMonoid α] (s : Multiset (Matrix n n α)) : diag s.sum = (s.map diag).sum := map_multiset_sum (diagAddMonoidHom n α) s @[simp] theorem diag_sum {ι} [AddCommMonoid α] (s : Finset ι) (f : ι → Matrix n n α) : diag (∑ i ∈ s, f i) = ∑ i ∈ s, diag (f i) := map_sum (diagAddMonoidHom n α) f s end Diag open Matrix section AddCommMonoid variable [AddCommMonoid α] [Mul α] end AddCommMonoid section NonAssocSemiring variable [NonAssocSemiring α] variable (α n) /-- `Matrix.diagonal` as a `RingHom`. -/ @[simps] def diagonalRingHom [Fintype n] [DecidableEq n] : (n → α) →+* Matrix n n α := { diagonalAddMonoidHom n α with toFun := diagonal map_one' := diagonal_one map_mul' := fun _ _ => (diagonal_mul_diagonal' _ _).symm } end NonAssocSemiring section Semiring variable [Semiring α] theorem diagonal_pow [Fintype n] [DecidableEq n] (v : n → α) (k : ℕ) : diagonal v ^ k = diagonal (v ^ k) := (map_pow (diagonalRingHom n α) v k).symm /-- The ring homomorphism `α →+* Matrix n n α` sending `a` to the diagonal matrix with `a` on the diagonal. -/ def scalar (n : Type u) [DecidableEq n] [Fintype n] : α →+* Matrix n n α := (diagonalRingHom n α).comp <| Pi.constRingHom n α section Scalar variable [DecidableEq n] [Fintype n] @[simp] theorem scalar_apply (a : α) : scalar n a = diagonal fun _ => a := rfl theorem scalar_inj [Nonempty n] {r s : α} : scalar n r = scalar n s ↔ r = s := (diagonal_injective.comp Function.const_injective).eq_iff theorem scalar_commute_iff {r : α} {M : Matrix n n α} : Commute (scalar n r) M ↔ r • M = MulOpposite.op r • M := by simp_rw [Commute, SemiconjBy, scalar_apply, ← smul_eq_diagonal_mul, ← op_smul_eq_mul_diagonal] theorem scalar_commute (r : α) (hr : ∀ r', Commute r r') (M : Matrix n n α) : Commute (scalar n r) M := scalar_commute_iff.2 <| ext fun _ _ => hr _ end Scalar end Semiring section Algebra variable [Fintype n] [DecidableEq n] variable [CommSemiring R] [Semiring α] [Semiring β] [Algebra R α] [Algebra R β] instance instAlgebra : Algebra R (Matrix n n α) where algebraMap := (Matrix.scalar n).comp (algebraMap R α) commutes' _ _ := scalar_commute _ (fun _ => Algebra.commutes _ _) _ smul_def' r x := by ext; simp [Matrix.scalar, Algebra.smul_def r] theorem algebraMap_matrix_apply {r : R} {i j : n} : algebraMap R (Matrix n n α) r i j = if i = j then algebraMap R α r else 0 := by dsimp [algebraMap, Algebra.algebraMap, Matrix.scalar] split_ifs with h <;> simp [h, Matrix.one_apply_ne] theorem algebraMap_eq_diagonal (r : R) : algebraMap R (Matrix n n α) r = diagonal (algebraMap R (n → α) r) := rfl theorem algebraMap_eq_diagonalRingHom : algebraMap R (Matrix n n α) = (diagonalRingHom n α).comp (algebraMap R _) := rfl @[simp] theorem map_algebraMap (r : R) (f : α → β) (hf : f 0 = 0) (hf₂ : f (algebraMap R α r) = algebraMap R β r) : (algebraMap R (Matrix n n α) r).map f = algebraMap R (Matrix n n β) r := by rw [algebraMap_eq_diagonal, algebraMap_eq_diagonal, diagonal_map hf] simp [hf₂] variable (R) /-- `Matrix.diagonal` as an `AlgHom`. -/ @[simps] def diagonalAlgHom : (n → α) →ₐ[R] Matrix n n α := { diagonalRingHom n α with toFun := diagonal commutes' := fun r => (algebraMap_eq_diagonal r).symm } end Algebra section AddHom variable [Add α] variable (R α) in /-- Extracting entries from a matrix as an additive homomorphism. -/ @[simps] def entryAddHom (i : m) (j : n) : AddHom (Matrix m n α) α where toFun M := M i j map_add' _ _ := rfl -- It is necessary to spell out the name of the coercion explicitly on the RHS -- for unification to succeed lemma entryAddHom_eq_comp {i : m} {j : n} : entryAddHom α i j = ((Pi.evalAddHom (fun _ => α) j).comp (Pi.evalAddHom _ i)).comp (AddHomClass.toAddHom ofAddEquiv.symm) := rfl end AddHom section AddMonoidHom variable [AddZeroClass α] variable (R α) in /-- Extracting entries from a matrix as an additive monoid homomorphism. Note this cannot be upgraded to a ring homomorphism, as it does not respect multiplication. -/ @[simps] def entryAddMonoidHom (i : m) (j : n) : Matrix m n α →+ α where toFun M := M i j map_add' _ _ := rfl map_zero' := rfl -- It is necessary to spell out the name of the coercion explicitly on the RHS -- for unification to succeed lemma entryAddMonoidHom_eq_comp {i : m} {j : n} : entryAddMonoidHom α i j = ((Pi.evalAddMonoidHom (fun _ => α) j).comp (Pi.evalAddMonoidHom _ i)).comp (AddMonoidHomClass.toAddMonoidHom ofAddEquiv.symm) := by rfl @[simp] lemma evalAddMonoidHom_comp_diagAddMonoidHom (i : m) : (Pi.evalAddMonoidHom _ i).comp (diagAddMonoidHom m α) = entryAddMonoidHom α i i := by simp [AddMonoidHom.ext_iff] @[simp] lemma entryAddMonoidHom_toAddHom {i : m} {j : n} : (entryAddMonoidHom α i j : AddHom _ _) = entryAddHom α i j := rfl end AddMonoidHom section LinearMap variable [Semiring R] [AddCommMonoid α] [Module R α] variable (R α) in /-- Extracting entries from a matrix as a linear map. Note this cannot be upgraded to an algebra homomorphism, as it does not respect multiplication. -/ @[simps] def entryLinearMap (i : m) (j : n) : Matrix m n α →ₗ[R] α where toFun M := M i j map_add' _ _ := rfl map_smul' _ _ := rfl -- It is necessary to spell out the name of the coercion explicitly on the RHS -- for unification to succeed lemma entryLinearMap_eq_comp {i : m} {j : n} : entryLinearMap R α i j = LinearMap.proj j ∘ₗ LinearMap.proj i ∘ₗ (ofLinearEquiv R).symm.toLinearMap := by rfl @[simp] lemma proj_comp_diagLinearMap (i : m) : LinearMap.proj i ∘ₗ diagLinearMap m R α = entryLinearMap R α i i := by simp [LinearMap.ext_iff] @[simp] lemma entryLinearMap_toAddMonoidHom {i : m} {j : n} : (entryLinearMap R α i j : _ →+ _) = entryAddMonoidHom α i j := rfl @[simp] lemma entryLinearMap_toAddHom {i : m} {j : n} : (entryLinearMap R α i j : AddHom _ _) = entryAddHom α i j := rfl end LinearMap end Matrix /-! ### Bundled versions of `Matrix.map` -/ namespace Equiv /-- The `Equiv` between spaces of matrices induced by an `Equiv` between their coefficients. This is `Matrix.map` as an `Equiv`. -/ @[simps apply] def mapMatrix (f : α ≃ β) : Matrix m n α ≃ Matrix m n β where toFun M := M.map f invFun M := M.map f.symm left_inv _ := Matrix.ext fun _ _ => f.symm_apply_apply _ right_inv _ := Matrix.ext fun _ _ => f.apply_symm_apply _ @[simp] theorem mapMatrix_refl : (Equiv.refl α).mapMatrix = Equiv.refl (Matrix m n α) := rfl @[simp] theorem mapMatrix_symm (f : α ≃ β) : f.mapMatrix.symm = (f.symm.mapMatrix : Matrix m n β ≃ _) := rfl @[simp] theorem mapMatrix_trans (f : α ≃ β) (g : β ≃ γ) : f.mapMatrix.trans g.mapMatrix = ((f.trans g).mapMatrix : Matrix m n α ≃ _) := rfl end Equiv namespace AddMonoidHom variable [AddZeroClass α] [AddZeroClass β] [AddZeroClass γ] /-- The `AddMonoidHom` between spaces of matrices induced by an `AddMonoidHom` between their coefficients. This is `Matrix.map` as an `AddMonoidHom`. -/ @[simps] def mapMatrix (f : α →+ β) : Matrix m n α →+ Matrix m n β where toFun M := M.map f map_zero' := Matrix.map_zero f f.map_zero map_add' := Matrix.map_add f f.map_add @[simp] theorem mapMatrix_id : (AddMonoidHom.id α).mapMatrix = AddMonoidHom.id (Matrix m n α) := rfl @[simp] theorem mapMatrix_comp (f : β →+ γ) (g : α →+ β) : f.mapMatrix.comp g.mapMatrix = ((f.comp g).mapMatrix : Matrix m n α →+ _) := rfl @[simp] lemma entryAddMonoidHom_comp_mapMatrix (f : α →+ β) (i : m) (j : n) : (entryAddMonoidHom β i j).comp f.mapMatrix = f.comp (entryAddMonoidHom α i j) := rfl end AddMonoidHom namespace AddEquiv variable [Add α] [Add β] [Add γ] /-- The `AddEquiv` between spaces of matrices induced by an `AddEquiv` between their coefficients. This is `Matrix.map` as an `AddEquiv`. -/ @[simps apply] def mapMatrix (f : α ≃+ β) : Matrix m n α ≃+ Matrix m n β := { f.toEquiv.mapMatrix with toFun := fun M => M.map f invFun := fun M => M.map f.symm map_add' := Matrix.map_add f (map_add f) } @[simp] theorem mapMatrix_refl : (AddEquiv.refl α).mapMatrix = AddEquiv.refl (Matrix m n α) := rfl @[simp] theorem mapMatrix_symm (f : α ≃+ β) : f.mapMatrix.symm = (f.symm.mapMatrix : Matrix m n β ≃+ _) := rfl @[simp] theorem mapMatrix_trans (f : α ≃+ β) (g : β ≃+ γ) : f.mapMatrix.trans g.mapMatrix = ((f.trans g).mapMatrix : Matrix m n α ≃+ _) := rfl @[simp] lemma entryAddHom_comp_mapMatrix (f : α ≃+ β) (i : m) (j : n) : (entryAddHom β i j).comp (AddHomClass.toAddHom f.mapMatrix) = (f : AddHom α β).comp (entryAddHom _ i j) := rfl end AddEquiv namespace LinearMap variable [Semiring R] [AddCommMonoid α] [AddCommMonoid β] [AddCommMonoid γ] variable [Module R α] [Module R β] [Module R γ] /-- The `LinearMap` between spaces of matrices induced by a `LinearMap` between their coefficients. This is `Matrix.map` as a `LinearMap`. -/ @[simps] def mapMatrix (f : α →ₗ[R] β) : Matrix m n α →ₗ[R] Matrix m n β where toFun M := M.map f map_add' := Matrix.map_add f f.map_add map_smul' r := Matrix.map_smul f r (f.map_smul r) @[simp] theorem mapMatrix_id : LinearMap.id.mapMatrix = (LinearMap.id : Matrix m n α →ₗ[R] _) := rfl @[simp] theorem mapMatrix_comp (f : β →ₗ[R] γ) (g : α →ₗ[R] β) : f.mapMatrix.comp g.mapMatrix = ((f.comp g).mapMatrix : Matrix m n α →ₗ[R] _) := rfl @[simp] lemma entryLinearMap_comp_mapMatrix (f : α →ₗ[R] β) (i : m) (j : n) : entryLinearMap R _ i j ∘ₗ f.mapMatrix = f ∘ₗ entryLinearMap R _ i j := rfl end LinearMap namespace LinearEquiv variable [Semiring R] [AddCommMonoid α] [AddCommMonoid β] [AddCommMonoid γ] variable [Module R α] [Module R β] [Module R γ] /-- The `LinearEquiv` between spaces of matrices induced by a `LinearEquiv` between their coefficients. This is `Matrix.map` as a `LinearEquiv`. -/ @[simps apply] def mapMatrix (f : α ≃ₗ[R] β) : Matrix m n α ≃ₗ[R] Matrix m n β := { f.toEquiv.mapMatrix, f.toLinearMap.mapMatrix with toFun := fun M => M.map f invFun := fun M => M.map f.symm } @[simp] theorem mapMatrix_refl : (LinearEquiv.refl R α).mapMatrix = LinearEquiv.refl R (Matrix m n α) := rfl @[simp] theorem mapMatrix_symm (f : α ≃ₗ[R] β) : f.mapMatrix.symm = (f.symm.mapMatrix : Matrix m n β ≃ₗ[R] _) := rfl @[simp] theorem mapMatrix_trans (f : α ≃ₗ[R] β) (g : β ≃ₗ[R] γ) : f.mapMatrix.trans g.mapMatrix = ((f.trans g).mapMatrix : Matrix m n α ≃ₗ[R] _) := rfl @[simp] lemma mapMatrix_toLinearMap (f : α ≃ₗ[R] β) : (f.mapMatrix : _ ≃ₗ[R] Matrix m n β).toLinearMap = f.toLinearMap.mapMatrix := by rfl @[simp] lemma entryLinearMap_comp_mapMatrix (f : α ≃ₗ[R] β) (i : m) (j : n) : entryLinearMap R _ i j ∘ₗ f.mapMatrix.toLinearMap = f.toLinearMap ∘ₗ entryLinearMap R _ i j := by simp only [mapMatrix_toLinearMap, LinearMap.entryLinearMap_comp_mapMatrix] end LinearEquiv namespace RingHom variable [Fintype m] [DecidableEq m] variable [NonAssocSemiring α] [NonAssocSemiring β] [NonAssocSemiring γ] /-- The `RingHom` between spaces of square matrices induced by a `RingHom` between their coefficients. This is `Matrix.map` as a `RingHom`. -/ @[simps] def mapMatrix (f : α →+* β) : Matrix m m α →+* Matrix m m β := { f.toAddMonoidHom.mapMatrix with toFun := fun M => M.map f map_one' := by simp map_mul' := fun _ _ => Matrix.map_mul } @[simp] theorem mapMatrix_id : (RingHom.id α).mapMatrix = RingHom.id (Matrix m m α) := rfl @[simp] theorem mapMatrix_comp (f : β →+* γ) (g : α →+* β) : f.mapMatrix.comp g.mapMatrix = ((f.comp g).mapMatrix : Matrix m m α →+* _) := rfl end RingHom namespace RingEquiv variable [Fintype m] [DecidableEq m] variable [NonAssocSemiring α] [NonAssocSemiring β] [NonAssocSemiring γ] /-- The `RingEquiv` between spaces of square matrices induced by a `RingEquiv` between their coefficients. This is `Matrix.map` as a `RingEquiv`. -/ @[simps apply] def mapMatrix (f : α ≃+* β) : Matrix m m α ≃+* Matrix m m β := { f.toRingHom.mapMatrix, f.toAddEquiv.mapMatrix with toFun := fun M => M.map f invFun := fun M => M.map f.symm } @[simp] theorem mapMatrix_refl : (RingEquiv.refl α).mapMatrix = RingEquiv.refl (Matrix m m α) := rfl @[simp] theorem mapMatrix_symm (f : α ≃+* β) : f.mapMatrix.symm = (f.symm.mapMatrix : Matrix m m β ≃+* _) := rfl @[simp] theorem mapMatrix_trans (f : α ≃+* β) (g : β ≃+* γ) : f.mapMatrix.trans g.mapMatrix = ((f.trans g).mapMatrix : Matrix m m α ≃+* _) := rfl open MulOpposite in /-- For any ring `R`, we have ring isomorphism `Matₙₓₙ(Rᵒᵖ) ≅ (Matₙₓₙ(R))ᵒᵖ` given by transpose. -/ @[simps apply symm_apply] def mopMatrix : Matrix m m αᵐᵒᵖ ≃+* (Matrix m m α)ᵐᵒᵖ where toFun M := op (M.transpose.map unop) invFun M := M.unop.transpose.map op left_inv _ := by aesop right_inv _ := by aesop map_mul' _ _ := unop_injective <| by ext; simp [transpose, mul_apply] map_add' _ _ := by aesop end RingEquiv namespace AlgHom variable [Fintype m] [DecidableEq m] variable [CommSemiring R] [Semiring α] [Semiring β] [Semiring γ] variable [Algebra R α] [Algebra R β] [Algebra R γ] /-- The `AlgHom` between spaces of square matrices induced by an `AlgHom` between their coefficients. This is `Matrix.map` as an `AlgHom`. -/ @[simps] def mapMatrix (f : α →ₐ[R] β) : Matrix m m α →ₐ[R] Matrix m m β := { f.toRingHom.mapMatrix with toFun := fun M => M.map f commutes' := fun r => Matrix.map_algebraMap r f (map_zero _) (f.commutes r) } @[simp] theorem mapMatrix_id : (AlgHom.id R α).mapMatrix = AlgHom.id R (Matrix m m α) := rfl @[simp] theorem mapMatrix_comp (f : β →ₐ[R] γ) (g : α →ₐ[R] β) : f.mapMatrix.comp g.mapMatrix = ((f.comp g).mapMatrix : Matrix m m α →ₐ[R] _) := rfl end AlgHom namespace AlgEquiv variable [Fintype m] [DecidableEq m] variable [CommSemiring R] [Semiring α] [Semiring β] [Semiring γ] variable [Algebra R α] [Algebra R β] [Algebra R γ] /-- The `AlgEquiv` between spaces of square matrices induced by an `AlgEquiv` between their coefficients. This is `Matrix.map` as an `AlgEquiv`. -/ @[simps apply] def mapMatrix (f : α ≃ₐ[R] β) : Matrix m m α ≃ₐ[R] Matrix m m β := { f.toAlgHom.mapMatrix, f.toRingEquiv.mapMatrix with toFun := fun M => M.map f invFun := fun M => M.map f.symm } @[simp] theorem mapMatrix_refl : AlgEquiv.refl.mapMatrix = (AlgEquiv.refl : Matrix m m α ≃ₐ[R] _) := rfl @[simp] theorem mapMatrix_symm (f : α ≃ₐ[R] β) : f.mapMatrix.symm = (f.symm.mapMatrix : Matrix m m β ≃ₐ[R] _) := rfl @[simp] theorem mapMatrix_trans (f : α ≃ₐ[R] β) (g : β ≃ₐ[R] γ) : f.mapMatrix.trans g.mapMatrix = ((f.trans g).mapMatrix : Matrix m m α ≃ₐ[R] _) := rfl /-- For any algebra `α` over a ring `R`, we have an `R`-algebra isomorphism `Matₙₓₙ(αᵒᵖ) ≅ (Matₙₓₙ(R))ᵒᵖ` given by transpose. If `α` is commutative, we can get rid of the `ᵒᵖ` in the left-hand side, see `Matrix.transposeAlgEquiv`. -/ @[simps!] def mopMatrix : Matrix m m αᵐᵒᵖ ≃ₐ[R] (Matrix m m α)ᵐᵒᵖ where __ := RingEquiv.mopMatrix commutes' _ := MulOpposite.unop_injective <| by ext; simp [algebraMap_matrix_apply, eq_comm, apply_ite MulOpposite.unop] end AlgEquiv open Matrix namespace Matrix section Transpose open Matrix variable (m n α) /-- `Matrix.transpose` as an `AddEquiv` -/ @[simps apply] def transposeAddEquiv [Add α] : Matrix m n α ≃+ Matrix n m α where toFun := transpose invFun := transpose left_inv := transpose_transpose right_inv := transpose_transpose map_add' := transpose_add @[simp] theorem transposeAddEquiv_symm [Add α] : (transposeAddEquiv m n α).symm = transposeAddEquiv n m α := rfl variable {m n α} theorem transpose_list_sum [AddMonoid α] (l : List (Matrix m n α)) : l.sumᵀ = (l.map transpose).sum := map_list_sum (transposeAddEquiv m n α) l theorem transpose_multiset_sum [AddCommMonoid α] (s : Multiset (Matrix m n α)) : s.sumᵀ = (s.map transpose).sum := (transposeAddEquiv m n α).toAddMonoidHom.map_multiset_sum s theorem transpose_sum [AddCommMonoid α] {ι : Type*} (s : Finset ι) (M : ι → Matrix m n α) : (∑ i ∈ s, M i)ᵀ = ∑ i ∈ s, (M i)ᵀ := map_sum (transposeAddEquiv m n α) _ s variable (m n R α) /-- `Matrix.transpose` as a `LinearMap` -/ @[simps apply] def transposeLinearEquiv [Semiring R] [AddCommMonoid α] [Module R α] : Matrix m n α ≃ₗ[R] Matrix n m α := { transposeAddEquiv m n α with map_smul' := transpose_smul } @[simp] theorem transposeLinearEquiv_symm [Semiring R] [AddCommMonoid α] [Module R α] : (transposeLinearEquiv m n R α).symm = transposeLinearEquiv n m R α := rfl variable {m n R α} variable (m α) /-- `Matrix.transpose` as a `RingEquiv` to the opposite ring -/ @[simps] def transposeRingEquiv [AddCommMonoid α] [CommSemigroup α] [Fintype m] : Matrix m m α ≃+* (Matrix m m α)ᵐᵒᵖ := { (transposeAddEquiv m m α).trans MulOpposite.opAddEquiv with toFun := fun M => MulOpposite.op Mᵀ invFun := fun M => M.unopᵀ map_mul' := fun M N => (congr_arg MulOpposite.op (transpose_mul M N)).trans (MulOpposite.op_mul _ _) left_inv := fun M => transpose_transpose M right_inv := fun M => MulOpposite.unop_injective <| transpose_transpose M.unop } variable {m α} @[simp] theorem transpose_pow [CommSemiring α] [Fintype m] [DecidableEq m] (M : Matrix m m α) (k : ℕ) : (M ^ k)ᵀ = Mᵀ ^ k := MulOpposite.op_injective <| map_pow (transposeRingEquiv m α) M k theorem transpose_list_prod [CommSemiring α] [Fintype m] [DecidableEq m] (l : List (Matrix m m α)) : l.prodᵀ = (l.map transpose).reverse.prod := (transposeRingEquiv m α).unop_map_list_prod l variable (R m α) /-- `Matrix.transpose` as an `AlgEquiv` to the opposite ring -/ @[simps] def transposeAlgEquiv [CommSemiring R] [CommSemiring α] [Fintype m] [DecidableEq m] [Algebra R α] : Matrix m m α ≃ₐ[R] (Matrix m m α)ᵐᵒᵖ := { (transposeAddEquiv m m α).trans MulOpposite.opAddEquiv, transposeRingEquiv m α with toFun := fun M => MulOpposite.op Mᵀ commutes' := fun r => by simp only [algebraMap_eq_diagonal, diagonal_transpose, MulOpposite.algebraMap_apply] } variable {R m α} end Transpose end Matrix
Mathlib/Data/Matrix/Basic.lean
2,309
2,312
/- Copyright (c) 2022 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import Mathlib.Analysis.InnerProductSpace.Orientation import Mathlib.MeasureTheory.Measure.Lebesgue.EqHaar /-! # Volume forms and measures on inner product spaces A volume form induces a Lebesgue measure on general finite-dimensional real vector spaces. In this file, we discuss the specific situation of inner product spaces, where an orientation gives rise to a canonical volume form. We show that the measure coming from this volume form gives measure `1` to the parallelepiped spanned by any orthonormal basis, and that it coincides with the canonical `volume` from the `MeasureSpace` instance. -/ open Module MeasureTheory MeasureTheory.Measure Set variable {ι E F : Type*} variable [NormedAddCommGroup F] [InnerProductSpace ℝ F] [NormedAddCommGroup E] [InnerProductSpace ℝ E] [MeasurableSpace E] [BorelSpace E] [MeasurableSpace F] [BorelSpace F] namespace LinearIsometryEquiv variable (f : E ≃ₗᵢ[ℝ] F) /-- Every linear isometry equivalence is a measurable equivalence. -/ def toMeasurableEquiv : E ≃ᵐ F where toEquiv := f
measurable_toFun := f.continuous.measurable measurable_invFun := f.symm.continuous.measurable @[deprecated (since := "2025-03-22")] alias toMeasureEquiv := toMeasurableEquiv @[simp] theorem coe_toMeasurableEquiv : (f.toMeasurableEquiv : E → F) = f := rfl @[deprecated (since := "2025-03-22")] alias coe_toMeasureEquiv := coe_toMeasurableEquiv theorem toMeasurableEquiv_symm : f.toMeasurableEquiv.symm = f.symm.toMeasurableEquiv := rfl
Mathlib/MeasureTheory/Measure/Haar/InnerProductSpace.lean
34
43
/- Copyright (c) 2020 Kim Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kim Morrison, Johan Commelin -/ import Mathlib.Algebra.Algebra.RestrictScalars import Mathlib.Algebra.Algebra.Subalgebra.Lattice import Mathlib.Algebra.Module.Rat import Mathlib.GroupTheory.MonoidLocalization.Basic import Mathlib.LinearAlgebra.TensorProduct.Tower /-! # The tensor product of R-algebras This file provides results about the multiplicative structure on `A ⊗[R] B` when `R` is a commutative (semi)ring and `A` and `B` are both `R`-algebras. On these tensor products, multiplication is characterized by `(a₁ ⊗ₜ b₁) * (a₂ ⊗ₜ b₂) = (a₁ * a₂) ⊗ₜ (b₁ * b₂)`. ## Main declarations - `LinearMap.baseChange A f` is the `A`-linear map `A ⊗ f`, for an `R`-linear map `f`. - `Algebra.TensorProduct.semiring`: the ring structure on `A ⊗[R] B` for two `R`-algebras `A`, `B`. - `Algebra.TensorProduct.leftAlgebra`: the `S`-algebra structure on `A ⊗[R] B`, for when `A` is additionally an `S` algebra. - the structure isomorphisms * `Algebra.TensorProduct.lid : R ⊗[R] A ≃ₐ[R] A` * `Algebra.TensorProduct.rid : A ⊗[R] R ≃ₐ[S] A` (usually used with `S = R` or `S = A`) * `Algebra.TensorProduct.comm : A ⊗[R] B ≃ₐ[R] B ⊗[R] A` * `Algebra.TensorProduct.assoc : ((A ⊗[R] B) ⊗[R] C) ≃ₐ[R] (A ⊗[R] (B ⊗[R] C))` - `Algebra.TensorProduct.liftEquiv`: a universal property for the tensor product of algebras. ## References * [C. Kassel, *Quantum Groups* (§II.4)][Kassel1995] -/ assert_not_exists Equiv.Perm.cycleType suppress_compilation open scoped TensorProduct open TensorProduct namespace LinearMap open TensorProduct /-! ### The base-change of a linear map of `R`-modules to a linear map of `A`-modules -/ section Semiring variable {R A B M N P : Type*} [CommSemiring R] variable [Semiring A] [Algebra R A] [Semiring B] [Algebra R B] variable [AddCommMonoid M] [AddCommMonoid N] [AddCommMonoid P] variable [Module R M] [Module R N] [Module R P] variable (r : R) (f g : M →ₗ[R] N) variable (A) in /-- `baseChange A f` for `f : M →ₗ[R] N` is the `A`-linear map `A ⊗[R] M →ₗ[A] A ⊗[R] N`. This "base change" operation is also known as "extension of scalars". -/ def baseChange (f : M →ₗ[R] N) : A ⊗[R] M →ₗ[A] A ⊗[R] N := AlgebraTensorModule.map (LinearMap.id : A →ₗ[A] A) f @[simp] theorem baseChange_tmul (a : A) (x : M) : f.baseChange A (a ⊗ₜ x) = a ⊗ₜ f x := rfl theorem baseChange_eq_ltensor : (f.baseChange A : A ⊗ M → A ⊗ N) = f.lTensor A := rfl @[simp] theorem baseChange_add : (f + g).baseChange A = f.baseChange A + g.baseChange A := by ext -- Porting note: added `-baseChange_tmul` simp [baseChange_eq_ltensor, -baseChange_tmul] @[simp] theorem baseChange_zero : baseChange A (0 : M →ₗ[R] N) = 0 := by ext simp [baseChange_eq_ltensor] @[simp] theorem baseChange_smul : (r • f).baseChange A = r • f.baseChange A := by ext simp [baseChange_tmul] @[simp] lemma baseChange_id : (.id : M →ₗ[R] M).baseChange A = .id := by ext; simp lemma baseChange_comp (g : N →ₗ[R] P) : (g ∘ₗ f).baseChange A = g.baseChange A ∘ₗ f.baseChange A := by ext; simp variable (R M) in @[simp] lemma baseChange_one : (1 : Module.End R M).baseChange A = 1 := baseChange_id lemma baseChange_mul (f g : Module.End R M) : (f * g).baseChange A = f.baseChange A * g.baseChange A := by ext; simp variable (R A M N) /-- `baseChange A e` for `e : M ≃ₗ[R] N` is the `A`-linear map `A ⊗[R] M ≃ₗ[A] A ⊗[R] N`. -/ def _root_.LinearEquiv.baseChange (e : M ≃ₗ[R] N) : A ⊗[R] M ≃ₗ[A] A ⊗[R] N := AlgebraTensorModule.congr (.refl _ _) e /-- `baseChange` as a linear map. When `M = N`, this is true more strongly as `Module.End.baseChangeHom`. -/ @[simps] def baseChangeHom : (M →ₗ[R] N) →ₗ[R] A ⊗[R] M →ₗ[A] A ⊗[R] N where toFun := baseChange A map_add' := baseChange_add map_smul' := baseChange_smul /-- `baseChange` as an `AlgHom`. -/ @[simps!] def _root_.Module.End.baseChangeHom : Module.End R M →ₐ[R] Module.End A (A ⊗[R] M) := .ofLinearMap (LinearMap.baseChangeHom _ _ _ _) (baseChange_one _ _) baseChange_mul lemma baseChange_pow (f : Module.End R M) (n : ℕ) : (f ^ n).baseChange A = f.baseChange A ^ n := map_pow (Module.End.baseChangeHom _ _ _) f n end Semiring section Ring variable {R A B M N : Type*} [CommRing R] variable [Ring A] [Algebra R A] [Ring B] [Algebra R B] variable [AddCommGroup M] [Module R M] [AddCommGroup N] [Module R N] variable (f g : M →ₗ[R] N) @[simp] theorem baseChange_sub : (f - g).baseChange A = f.baseChange A - g.baseChange A := by ext simp [baseChange_eq_ltensor, tmul_sub] @[simp] theorem baseChange_neg : (-f).baseChange A = -f.baseChange A := by ext simp [baseChange_eq_ltensor, tmul_neg] end Ring section liftBaseChange variable {R M N} (A) [CommSemiring R] [CommSemiring A] [Algebra R A] [AddCommMonoid M] variable [AddCommMonoid N] [Module R M] [Module R N] [Module A N] [IsScalarTower R A N] /-- If `M` is an `R`-module and `N` is an `A`-module, then `A`-linear maps `A ⊗[R] M →ₗ[A] N` correspond to `R` linear maps `M →ₗ[R] N` by composing with `M → A ⊗ M`, `x ↦ 1 ⊗ x`. -/ noncomputable def liftBaseChangeEquiv : (M →ₗ[R] N) ≃ₗ[A] (A ⊗[R] M →ₗ[A] N) := (LinearMap.ringLmapEquivSelf _ _ _).symm.trans (AlgebraTensorModule.lift.equiv _ _ _ _ _ _) /-- If `N` is an `A` module, we may lift a linear map `M →ₗ[R] N` to `A ⊗[R] M →ₗ[A] N` -/ noncomputable abbrev liftBaseChange (l : M →ₗ[R] N) : A ⊗[R] M →ₗ[A] N := LinearMap.liftBaseChangeEquiv A l @[simp] lemma liftBaseChange_tmul (l : M →ₗ[R] N) (x y) : l.liftBaseChange A (x ⊗ₜ y) = x • l y := rfl lemma liftBaseChange_one_tmul (l : M →ₗ[R] N) (y) : l.liftBaseChange A (1 ⊗ₜ y) = l y := by simp @[simp] lemma liftBaseChangeEquiv_symm_apply (l : A ⊗[R] M →ₗ[A] N) (x) : (liftBaseChangeEquiv A).symm l x = l (1 ⊗ₜ x) := rfl lemma liftBaseChange_comp {P} [AddCommMonoid P] [Module A P] [Module R P] [IsScalarTower R A P] (l : M →ₗ[R] N) (l' : N →ₗ[A] P) : l' ∘ₗ l.liftBaseChange A = (l'.restrictScalars R ∘ₗ l).liftBaseChange A := by ext simp @[simp] lemma range_liftBaseChange (l : M →ₗ[R] N) : LinearMap.range (l.liftBaseChange A) = Submodule.span A (LinearMap.range l) := by apply le_antisymm · rintro _ ⟨x, rfl⟩ induction x using TensorProduct.induction_on · simp · rw [LinearMap.liftBaseChange_tmul] exact Submodule.smul_mem _ _ (Submodule.subset_span ⟨_, rfl⟩) · rw [map_add] exact add_mem ‹_› ‹_› · rw [Submodule.span_le] rintro _ ⟨x, rfl⟩ exact ⟨1 ⊗ₜ x, by simp⟩ end liftBaseChange end LinearMap namespace Module.End open LinearMap variable (R M N : Type*) [CommSemiring R] [AddCommMonoid M] [Module R M] [AddCommMonoid N] [Module R N] /-- The map `LinearMap.lTensorHom` which sends `f ↦ 1 ⊗ f` as a morphism of algebras. -/ @[simps!] noncomputable def lTensorAlgHom : Module.End R M →ₐ[R] Module.End R (N ⊗[R] M) := .ofLinearMap (lTensorHom (M := N)) (lTensor_id N M) (lTensor_mul N) /-- The map `LinearMap.rTensorHom` which sends `f ↦ f ⊗ 1` as a morphism of algebras. -/ @[simps!] noncomputable def rTensorAlgHom : Module.End R M →ₐ[R] Module.End R (M ⊗[R] N) := .ofLinearMap (rTensorHom (M := N)) (rTensor_id N M) (rTensor_mul N) end Module.End namespace Algebra namespace TensorProduct universe uR uS uA uB uC uD uE uF variable {R : Type uR} {S : Type uS} variable {A : Type uA} {B : Type uB} {C : Type uC} {D : Type uD} {E : Type uE} {F : Type uF} /-! ### The `R`-algebra structure on `A ⊗[R] B` -/ section AddCommMonoidWithOne variable [CommSemiring R] variable [AddCommMonoidWithOne A] [Module R A] variable [AddCommMonoidWithOne B] [Module R B] instance : One (A ⊗[R] B) where one := 1 ⊗ₜ 1 theorem one_def : (1 : A ⊗[R] B) = (1 : A) ⊗ₜ (1 : B) := rfl instance instAddCommMonoidWithOne : AddCommMonoidWithOne (A ⊗[R] B) where natCast n := n ⊗ₜ 1 natCast_zero := by simp natCast_succ n := by simp [add_tmul, one_def] add_comm := add_comm theorem natCast_def (n : ℕ) : (n : A ⊗[R] B) = (n : A) ⊗ₜ (1 : B) := rfl theorem natCast_def' (n : ℕ) : (n : A ⊗[R] B) = (1 : A) ⊗ₜ (n : B) := by rw [natCast_def, ← nsmul_one, smul_tmul, nsmul_one] end AddCommMonoidWithOne section NonUnitalNonAssocSemiring variable [CommSemiring R] variable [NonUnitalNonAssocSemiring A] [Module R A] [SMulCommClass R A A] [IsScalarTower R A A] variable [NonUnitalNonAssocSemiring B] [Module R B] [SMulCommClass R B B] [IsScalarTower R B B] /-- (Implementation detail) The multiplication map on `A ⊗[R] B`, as an `R`-bilinear map. -/ @[irreducible] def mul : A ⊗[R] B →ₗ[R] A ⊗[R] B →ₗ[R] A ⊗[R] B := TensorProduct.map₂ (LinearMap.mul R A) (LinearMap.mul R B) unseal mul in @[simp] theorem mul_apply (a₁ a₂ : A) (b₁ b₂ : B) : mul (a₁ ⊗ₜ[R] b₁) (a₂ ⊗ₜ[R] b₂) = (a₁ * a₂) ⊗ₜ[R] (b₁ * b₂) := rfl -- providing this instance separately makes some downstream code substantially faster instance instMul : Mul (A ⊗[R] B) where mul a b := mul a b unseal mul in @[simp] theorem tmul_mul_tmul (a₁ a₂ : A) (b₁ b₂ : B) : a₁ ⊗ₜ[R] b₁ * a₂ ⊗ₜ[R] b₂ = (a₁ * a₂) ⊗ₜ[R] (b₁ * b₂) := rfl unseal mul in theorem _root_.SemiconjBy.tmul {a₁ a₂ a₃ : A} {b₁ b₂ b₃ : B} (ha : SemiconjBy a₁ a₂ a₃) (hb : SemiconjBy b₁ b₂ b₃) : SemiconjBy (a₁ ⊗ₜ[R] b₁) (a₂ ⊗ₜ[R] b₂) (a₃ ⊗ₜ[R] b₃) := congr_arg₂ (· ⊗ₜ[R] ·) ha.eq hb.eq nonrec theorem _root_.Commute.tmul {a₁ a₂ : A} {b₁ b₂ : B} (ha : Commute a₁ a₂) (hb : Commute b₁ b₂) : Commute (a₁ ⊗ₜ[R] b₁) (a₂ ⊗ₜ[R] b₂) := ha.tmul hb instance instNonUnitalNonAssocSemiring : NonUnitalNonAssocSemiring (A ⊗[R] B) where left_distrib a b c := by simp [HMul.hMul, Mul.mul] right_distrib a b c := by simp [HMul.hMul, Mul.mul] zero_mul a := by simp [HMul.hMul, Mul.mul] mul_zero a := by simp [HMul.hMul, Mul.mul] -- we want `isScalarTower_right` to take priority since it's better for unification elsewhere instance (priority := 100) isScalarTower_right [Monoid S] [DistribMulAction S A] [IsScalarTower S A A] [SMulCommClass R S A] : IsScalarTower S (A ⊗[R] B) (A ⊗[R] B) where smul_assoc r x y := by change r • x * y = r • (x * y) induction y with | zero => simp [smul_zero] | tmul a b => induction x with | zero => simp [smul_zero] | tmul a' b' => dsimp rw [TensorProduct.smul_tmul', TensorProduct.smul_tmul', tmul_mul_tmul, smul_mul_assoc] | add x y hx hy => simp [smul_add, add_mul _, *] | add x y hx hy => simp [smul_add, mul_add _, *] -- we want `Algebra.to_smulCommClass` to take priority since it's better for unification elsewhere instance (priority := 100) sMulCommClass_right [Monoid S] [DistribMulAction S A] [SMulCommClass S A A] [SMulCommClass R S A] : SMulCommClass S (A ⊗[R] B) (A ⊗[R] B) where smul_comm r x y := by change r • (x * y) = x * r • y induction y with | zero => simp [smul_zero] | tmul a b => induction x with | zero => simp [smul_zero] | tmul a' b' => dsimp rw [TensorProduct.smul_tmul', TensorProduct.smul_tmul', tmul_mul_tmul, mul_smul_comm] | add x y hx hy => simp [smul_add, add_mul _, *] | add x y hx hy => simp [smul_add, mul_add _, *] end NonUnitalNonAssocSemiring section NonAssocSemiring variable [CommSemiring R] variable [NonAssocSemiring A] [Module R A] [SMulCommClass R A A] [IsScalarTower R A A] variable [NonAssocSemiring B] [Module R B] [SMulCommClass R B B] [IsScalarTower R B B] protected theorem one_mul (x : A ⊗[R] B) : mul (1 ⊗ₜ 1) x = x := by refine TensorProduct.induction_on x ?_ ?_ ?_ <;> simp +contextual protected theorem mul_one (x : A ⊗[R] B) : mul x (1 ⊗ₜ 1) = x := by refine TensorProduct.induction_on x ?_ ?_ ?_ <;> simp +contextual instance instNonAssocSemiring : NonAssocSemiring (A ⊗[R] B) where one_mul := Algebra.TensorProduct.one_mul mul_one := Algebra.TensorProduct.mul_one toNonUnitalNonAssocSemiring := instNonUnitalNonAssocSemiring __ := instAddCommMonoidWithOne end NonAssocSemiring section NonUnitalSemiring variable [CommSemiring R] variable [NonUnitalSemiring A] [Module R A] [SMulCommClass R A A] [IsScalarTower R A A] variable [NonUnitalSemiring B] [Module R B] [SMulCommClass R B B] [IsScalarTower R B B] unseal mul in protected theorem mul_assoc (x y z : A ⊗[R] B) : mul (mul x y) z = mul x (mul y z) := by -- restate as an equality of morphisms so that we can use `ext` suffices LinearMap.llcomp R _ _ _ mul ∘ₗ mul = (LinearMap.llcomp R _ _ _ LinearMap.lflip <| LinearMap.llcomp R _ _ _ mul.flip ∘ₗ mul).flip by exact DFunLike.congr_fun (DFunLike.congr_fun (DFunLike.congr_fun this x) y) z ext xa xb ya yb za zb exact congr_arg₂ (· ⊗ₜ ·) (mul_assoc xa ya za) (mul_assoc xb yb zb) instance instNonUnitalSemiring : NonUnitalSemiring (A ⊗[R] B) where mul_assoc := Algebra.TensorProduct.mul_assoc end NonUnitalSemiring section Semiring variable [CommSemiring R] variable [Semiring A] [Algebra R A] variable [Semiring B] [Algebra R B] variable [Semiring C] [Algebra R C] instance instSemiring : Semiring (A ⊗[R] B) where left_distrib a b c := by simp [HMul.hMul, Mul.mul] right_distrib a b c := by simp [HMul.hMul, Mul.mul] zero_mul a := by simp [HMul.hMul, Mul.mul] mul_zero a := by simp [HMul.hMul, Mul.mul] mul_assoc := Algebra.TensorProduct.mul_assoc one_mul := Algebra.TensorProduct.one_mul mul_one := Algebra.TensorProduct.mul_one natCast_zero := AddMonoidWithOne.natCast_zero natCast_succ := AddMonoidWithOne.natCast_succ @[simp] theorem tmul_pow (a : A) (b : B) (k : ℕ) : a ⊗ₜ[R] b ^ k = (a ^ k) ⊗ₜ[R] (b ^ k) := by induction' k with k ih · simp [one_def] · simp [pow_succ, ih] /-- The ring morphism `A →+* A ⊗[R] B` sending `a` to `a ⊗ₜ 1`. -/ @[simps] def includeLeftRingHom : A →+* A ⊗[R] B where toFun a := a ⊗ₜ 1 map_zero' := by simp map_add' := by simp [add_tmul] map_one' := rfl map_mul' := by simp variable [CommSemiring S] [Algebra S A] instance leftAlgebra [SMulCommClass R S A] : Algebra S (A ⊗[R] B) := { commutes' := fun r x => by dsimp only [RingHom.toFun_eq_coe, RingHom.comp_apply, includeLeftRingHom_apply] rw [algebraMap_eq_smul_one, ← smul_tmul', ← one_def, mul_smul_comm, smul_mul_assoc, mul_one, one_mul] smul_def' := fun r x => by dsimp only [RingHom.toFun_eq_coe, RingHom.comp_apply, includeLeftRingHom_apply] rw [algebraMap_eq_smul_one, ← smul_tmul', smul_mul_assoc, ← one_def, one_mul] algebraMap := TensorProduct.includeLeftRingHom.comp (algebraMap S A) } example : (Semiring.toNatAlgebra : Algebra ℕ (ℕ ⊗[ℕ] B)) = leftAlgebra := rfl -- This is for the `undergrad.yaml` list. /-- The tensor product of two `R`-algebras is an `R`-algebra. -/ instance instAlgebra : Algebra R (A ⊗[R] B) := inferInstance @[simp] theorem algebraMap_apply [SMulCommClass R S A] (r : S) : algebraMap S (A ⊗[R] B) r = (algebraMap S A) r ⊗ₜ 1 := rfl theorem algebraMap_apply' (r : R) : algebraMap R (A ⊗[R] B) r = 1 ⊗ₜ algebraMap R B r := by rw [algebraMap_apply, Algebra.algebraMap_eq_smul_one, Algebra.algebraMap_eq_smul_one, smul_tmul] /-- The `R`-algebra morphism `A →ₐ[R] A ⊗[R] B` sending `a` to `a ⊗ₜ 1`. -/ def includeLeft [SMulCommClass R S A] : A →ₐ[S] A ⊗[R] B := { includeLeftRingHom with commutes' := by simp } @[simp] theorem includeLeft_apply [SMulCommClass R S A] (a : A) : (includeLeft : A →ₐ[S] A ⊗[R] B) a = a ⊗ₜ 1 := rfl /-- The algebra morphism `B →ₐ[R] A ⊗[R] B` sending `b` to `1 ⊗ₜ b`. -/ def includeRight : B →ₐ[R] A ⊗[R] B where toFun b := 1 ⊗ₜ b map_zero' := by simp map_add' := by simp [tmul_add] map_one' := rfl map_mul' := by simp commutes' r := by simp only [algebraMap_apply'] @[simp] theorem includeRight_apply (b : B) : (includeRight : B →ₐ[R] A ⊗[R] B) b = 1 ⊗ₜ b := rfl theorem includeLeftRingHom_comp_algebraMap : (includeLeftRingHom.comp (algebraMap R A) : R →+* A ⊗[R] B) = includeRight.toRingHom.comp (algebraMap R B) := by ext simp section ext variable [Algebra R S] [Algebra S C] [IsScalarTower R S A] [IsScalarTower R S C] /-- A version of `TensorProduct.ext` for `AlgHom`. Using this as the `@[ext]` lemma instead of `Algebra.TensorProduct.ext'` allows `ext` to apply lemmas specific to `A →ₐ[S] _` and `B →ₐ[R] _`; notably this allows recursion into nested tensor products of algebras. See note [partially-applied ext lemmas]. -/ @[ext high] theorem ext ⦃f g : (A ⊗[R] B) →ₐ[S] C⦄ (ha : f.comp includeLeft = g.comp includeLeft) (hb : (f.restrictScalars R).comp includeRight = (g.restrictScalars R).comp includeRight) : f = g := by apply AlgHom.toLinearMap_injective ext a b have := congr_arg₂ HMul.hMul (AlgHom.congr_fun ha a) (AlgHom.congr_fun hb b) dsimp at * rwa [← map_mul, ← map_mul, tmul_mul_tmul, one_mul, mul_one] at this theorem ext' {g h : A ⊗[R] B →ₐ[S] C} (H : ∀ a b, g (a ⊗ₜ b) = h (a ⊗ₜ b)) : g = h := ext (AlgHom.ext fun _ => H _ _) (AlgHom.ext fun _ => H _ _) end ext end Semiring section AddCommGroupWithOne variable [CommSemiring R] variable [AddCommGroupWithOne A] [Module R A] variable [AddCommGroupWithOne B] [Module R B] instance instAddCommGroupWithOne : AddCommGroupWithOne (A ⊗[R] B) where toAddCommGroup := TensorProduct.addCommGroup __ := instAddCommMonoidWithOne intCast z := z ⊗ₜ (1 : B) intCast_ofNat n := by simp [natCast_def] intCast_negSucc n := by simp [natCast_def, add_tmul, neg_tmul, one_def] theorem intCast_def (z : ℤ) : (z : A ⊗[R] B) = (z : A) ⊗ₜ (1 : B) := rfl end AddCommGroupWithOne section NonUnitalNonAssocRing variable [CommRing R] variable [NonUnitalNonAssocRing A] [Module R A] [SMulCommClass R A A] [IsScalarTower R A A] variable [NonUnitalNonAssocRing B] [Module R B] [SMulCommClass R B B] [IsScalarTower R B B] instance instNonUnitalNonAssocRing : NonUnitalNonAssocRing (A ⊗[R] B) where toAddCommGroup := TensorProduct.addCommGroup __ := instNonUnitalNonAssocSemiring end NonUnitalNonAssocRing section NonAssocRing variable [CommRing R] variable [NonAssocRing A] [Module R A] [SMulCommClass R A A] [IsScalarTower R A A] variable [NonAssocRing B] [Module R B] [SMulCommClass R B B] [IsScalarTower R B B] instance instNonAssocRing : NonAssocRing (A ⊗[R] B) where toAddCommGroup := TensorProduct.addCommGroup __ := instNonAssocSemiring __ := instAddCommGroupWithOne end NonAssocRing section NonUnitalRing variable [CommRing R] variable [NonUnitalRing A] [Module R A] [SMulCommClass R A A] [IsScalarTower R A A] variable [NonUnitalRing B] [Module R B] [SMulCommClass R B B] [IsScalarTower R B B] instance instNonUnitalRing : NonUnitalRing (A ⊗[R] B) where toAddCommGroup := TensorProduct.addCommGroup __ := instNonUnitalSemiring end NonUnitalRing section CommSemiring variable [CommSemiring R] variable [CommSemiring A] [Algebra R A] variable [CommSemiring B] [Algebra R B] instance instCommSemiring : CommSemiring (A ⊗[R] B) where toSemiring := inferInstance mul_comm x y := by refine TensorProduct.induction_on x ?_ ?_ ?_ · simp · intro a₁ b₁ refine TensorProduct.induction_on y ?_ ?_ ?_ · simp · intro a₂ b₂ simp [mul_comm] · intro a₂ b₂ ha hb simp [mul_add, add_mul, ha, hb] · intro x₁ x₂ h₁ h₂ simp [mul_add, add_mul, h₁, h₂] end CommSemiring section Ring variable [CommRing R] variable [Ring A] [Algebra R A] variable [Ring B] [Algebra R B] instance instRing : Ring (A ⊗[R] B) where toSemiring := instSemiring __ := TensorProduct.addCommGroup __ := instNonAssocRing theorem intCast_def' (z : ℤ) : (z : A ⊗[R] B) = (1 : A) ⊗ₜ (z : B) := by rw [intCast_def, ← zsmul_one, smul_tmul, zsmul_one] -- verify there are no diamonds example : (instRing : Ring (A ⊗[R] B)).toAddCommGroup = addCommGroup := by with_reducible_and_instances rfl -- fails at `with_reducible_and_instances rfl` https://github.com/leanprover-community/mathlib4/issues/10906 example : (Ring.toIntAlgebra _ : Algebra ℤ (ℤ ⊗[ℤ] B)) = leftAlgebra := rfl end Ring section CommRing variable [CommRing R] variable [CommRing A] [Algebra R A] variable [CommRing B] [Algebra R B] instance instCommRing : CommRing (A ⊗[R] B) := { toRing := inferInstance mul_comm := mul_comm } end CommRing section RightAlgebra variable [CommSemiring R] variable [Semiring A] [Algebra R A] variable [CommSemiring B] [Algebra R B] /-- `S ⊗[R] T` has a `T`-algebra structure. This is not a global instance or else the action of `S` on `S ⊗[R] S` would be ambiguous. -/ abbrev rightAlgebra : Algebra B (A ⊗[R] B) := includeRight.toRingHom.toAlgebra' fun b x => by suffices LinearMap.mulLeft R (includeRight b) = LinearMap.mulRight R (includeRight b) from congr($this x) ext xa xb simp [mul_comm] attribute [local instance] TensorProduct.rightAlgebra instance right_isScalarTower : IsScalarTower R B (A ⊗[R] B) := IsScalarTower.of_algebraMap_eq fun r => (Algebra.TensorProduct.includeRight.commutes r).symm end RightAlgebra /-- Verify that typeclass search finds the ring structure on `A ⊗[ℤ] B` when `A` and `B` are merely rings, by treating both as `ℤ`-algebras. -/ example [Ring A] [Ring B] : Ring (A ⊗[ℤ] B) := by infer_instance /-- Verify that typeclass search finds the comm_ring structure on `A ⊗[ℤ] B` when `A` and `B` are merely comm_rings, by treating both as `ℤ`-algebras. -/ example [CommRing A] [CommRing B] : CommRing (A ⊗[ℤ] B) := by infer_instance /-! We now build the structure maps for the symmetric monoidal category of `R`-algebras. -/ section Monoidal section variable [CommSemiring R] [CommSemiring S] [Algebra R S] variable [Semiring A] [Algebra R A] [Algebra S A] [IsScalarTower R S A] variable [Semiring B] [Algebra R B] variable [Semiring C] [Algebra S C] variable [Semiring D] [Algebra R D] /-- To check a linear map preserves multiplication, it suffices to check it on pure tensors. See `algHomOfLinearMapTensorProduct` for a bundled version. -/ lemma _root_.LinearMap.map_mul_of_map_mul_tmul {f : A ⊗[R] B →ₗ[S] C} (hf : ∀ (a₁ a₂ : A) (b₁ b₂ : B), f ((a₁ * a₂) ⊗ₜ (b₁ * b₂)) = f (a₁ ⊗ₜ b₁) * f (a₂ ⊗ₜ b₂)) (x y : A ⊗[R] B) : f (x * y) = f x * f y := f.map_mul_iff.2 (by -- these instances are needed by the statement of `ext`, but not by the current definition. letI : Algebra R C := RestrictScalars.algebra R S C letI : IsScalarTower R S C := RestrictScalars.isScalarTower R S C ext dsimp exact hf _ _ _ _) x y /-- Build an algebra morphism from a linear map out of a tensor product, and evidence that on pure tensors, it preserves multiplication and the identity. Note that we state `h_one` using `1 ⊗ₜ[R] 1` instead of `1` so that lemmas about `f` applied to pure tensors can be directly applied by the caller (without needing `TensorProduct.one_def`). -/ def algHomOfLinearMapTensorProduct (f : A ⊗[R] B →ₗ[S] C) (h_mul : ∀ (a₁ a₂ : A) (b₁ b₂ : B), f ((a₁ * a₂) ⊗ₜ (b₁ * b₂)) = f (a₁ ⊗ₜ b₁) * f (a₂ ⊗ₜ b₂)) (h_one : f (1 ⊗ₜ[R] 1) = 1) : A ⊗[R] B →ₐ[S] C := AlgHom.ofLinearMap f h_one (f.map_mul_of_map_mul_tmul h_mul) @[simp] theorem algHomOfLinearMapTensorProduct_apply (f h_mul h_one x) : (algHomOfLinearMapTensorProduct f h_mul h_one : A ⊗[R] B →ₐ[S] C) x = f x := rfl /-- Build an algebra equivalence from a linear equivalence out of a tensor product, and evidence that on pure tensors, it preserves multiplication and the identity. Note that we state `h_one` using `1 ⊗ₜ[R] 1` instead of `1` so that lemmas about `f` applied to pure tensors can be directly applied by the caller (without needing `TensorProduct.one_def`). -/ def algEquivOfLinearEquivTensorProduct (f : A ⊗[R] B ≃ₗ[S] C) (h_mul : ∀ (a₁ a₂ : A) (b₁ b₂ : B), f ((a₁ * a₂) ⊗ₜ (b₁ * b₂)) = f (a₁ ⊗ₜ b₁) * f (a₂ ⊗ₜ b₂)) (h_one : f (1 ⊗ₜ[R] 1) = 1) : A ⊗[R] B ≃ₐ[S] C := { algHomOfLinearMapTensorProduct (f : A ⊗[R] B →ₗ[S] C) h_mul h_one, f with } @[simp] theorem algEquivOfLinearEquivTensorProduct_apply (f h_mul h_one x) : (algEquivOfLinearEquivTensorProduct f h_mul h_one : A ⊗[R] B ≃ₐ[S] C) x = f x := rfl variable [Algebra R C] /-- Build an algebra equivalence from a linear equivalence out of a triple tensor product, and evidence of multiplicativity on pure tensors. -/ def algEquivOfLinearEquivTripleTensorProduct (f : (A ⊗[R] B) ⊗[R] C ≃ₗ[R] D) (h_mul : ∀ (a₁ a₂ : A) (b₁ b₂ : B) (c₁ c₂ : C), f ((a₁ * a₂) ⊗ₜ (b₁ * b₂) ⊗ₜ (c₁ * c₂)) = f (a₁ ⊗ₜ b₁ ⊗ₜ c₁) * f (a₂ ⊗ₜ b₂ ⊗ₜ c₂)) (h_one : f (((1 : A) ⊗ₜ[R] (1 : B)) ⊗ₜ[R] (1 : C)) = 1) : (A ⊗[R] B) ⊗[R] C ≃ₐ[R] D := AlgEquiv.ofLinearEquiv f h_one <| f.map_mul_iff.2 <| by ext dsimp exact h_mul _ _ _ _ _ _ @[simp] theorem algEquivOfLinearEquivTripleTensorProduct_apply (f h_mul h_one x) : (algEquivOfLinearEquivTripleTensorProduct f h_mul h_one : (A ⊗[R] B) ⊗[R] C ≃ₐ[R] D) x = f x := rfl section lift variable [IsScalarTower R S C] /-- The forward direction of the universal property of tensor products of algebras; any algebra morphism from the tensor product can be factored as the product of two algebra morphisms that commute. See `Algebra.TensorProduct.liftEquiv` for the fact that every morphism factors this way. -/ def lift (f : A →ₐ[S] C) (g : B →ₐ[R] C) (hfg : ∀ x y, Commute (f x) (g y)) : (A ⊗[R] B) →ₐ[S] C := algHomOfLinearMapTensorProduct (AlgebraTensorModule.lift <| letI restr : (C →ₗ[S] C) →ₗ[S] _ := { toFun := (·.restrictScalars R) map_add' := fun _ _ => LinearMap.ext fun _ => rfl map_smul' := fun _ _ => LinearMap.ext fun _ => rfl } LinearMap.flip <| (restr ∘ₗ LinearMap.mul S C ∘ₗ f.toLinearMap).flip ∘ₗ g) (fun a₁ a₂ b₁ b₂ => show f (a₁ * a₂) * g (b₁ * b₂) = f a₁ * g b₁ * (f a₂ * g b₂) by rw [map_mul, map_mul, (hfg a₂ b₁).mul_mul_mul_comm]) (show f 1 * g 1 = 1 by rw [map_one, map_one, one_mul]) @[simp] theorem lift_tmul (f : A →ₐ[S] C) (g : B →ₐ[R] C) (hfg : ∀ x y, Commute (f x) (g y)) (a : A) (b : B) : lift f g hfg (a ⊗ₜ b) = f a * g b := rfl @[simp] theorem lift_includeLeft_includeRight : lift includeLeft includeRight (fun _ _ => (Commute.one_right _).tmul (Commute.one_left _)) = .id S (A ⊗[R] B) := by ext <;> simp @[simp] theorem lift_comp_includeLeft (f : A →ₐ[S] C) (g : B →ₐ[R] C) (hfg : ∀ x y, Commute (f x) (g y)) : (lift f g hfg).comp includeLeft = f := AlgHom.ext <| by simp @[simp] theorem lift_comp_includeRight (f : A →ₐ[S] C) (g : B →ₐ[R] C) (hfg : ∀ x y, Commute (f x) (g y)) : ((lift f g hfg).restrictScalars R).comp includeRight = g := AlgHom.ext <| by simp /-- The universal property of the tensor product of algebras. Pairs of algebra morphisms that commute are equivalent to algebra morphisms from the tensor product. This is `Algebra.TensorProduct.lift` as an equivalence. See also `GradedTensorProduct.liftEquiv` for an alternative commutativity requirement for graded algebra. -/ @[simps] def liftEquiv : {fg : (A →ₐ[S] C) × (B →ₐ[R] C) // ∀ x y, Commute (fg.1 x) (fg.2 y)} ≃ ((A ⊗[R] B) →ₐ[S] C) where toFun fg := lift fg.val.1 fg.val.2 fg.prop invFun f' := ⟨(f'.comp includeLeft, (f'.restrictScalars R).comp includeRight), fun _ _ => ((Commute.one_right _).tmul (Commute.one_left _)).map f'⟩ left_inv fg := by ext <;> simp right_inv f' := by ext <;> simp end lift end variable [CommSemiring R] [CommSemiring S] [Algebra R S] variable [Semiring A] [Algebra R A] [Algebra S A] [IsScalarTower R S A] variable [Semiring B] [Algebra R B] variable [Semiring C] [Algebra R C] [Algebra S C] [IsScalarTower R S C] variable [Semiring D] [Algebra R D] variable [Semiring E] [Algebra R E] [Algebra S E] [IsScalarTower R S E] variable [Semiring F] [Algebra R F] section variable (R A) /-- The base ring is a left identity for the tensor product of algebra, up to algebra isomorphism. -/ protected nonrec def lid : R ⊗[R] A ≃ₐ[R] A := algEquivOfLinearEquivTensorProduct (TensorProduct.lid R A) (by simp only [mul_smul, lid_tmul, Algebra.smul_mul_assoc, Algebra.mul_smul_comm] simp_rw [← mul_smul, mul_comm] simp) (by simp [Algebra.smul_def]) @[simp] theorem lid_toLinearEquiv : (TensorProduct.lid R A).toLinearEquiv = _root_.TensorProduct.lid R A := rfl variable {R} {A} in @[simp] theorem lid_tmul (r : R) (a : A) : TensorProduct.lid R A (r ⊗ₜ a) = r • a := rfl variable {A} in @[simp] theorem lid_symm_apply (a : A) : (TensorProduct.lid R A).symm a = 1 ⊗ₜ a := rfl variable (S) /-- The base ring is a right identity for the tensor product of algebra, up to algebra isomorphism. Note that if `A` is commutative this can be instantiated with `S = A`. -/ protected nonrec def rid : A ⊗[R] R ≃ₐ[S] A := algEquivOfLinearEquivTensorProduct (AlgebraTensorModule.rid R S A) (fun a₁ a₂ r₁ r₂ => smul_mul_smul_comm r₁ a₁ r₂ a₂ |>.symm) (one_smul R _) @[simp] theorem rid_toLinearEquiv : (TensorProduct.rid R S A).toLinearEquiv = AlgebraTensorModule.rid R S A := rfl variable {R A} in @[simp] theorem rid_tmul (r : R) (a : A) : TensorProduct.rid R S A (a ⊗ₜ r) = r • a := rfl variable {A} in @[simp] theorem rid_symm_apply (a : A) : (TensorProduct.rid R S A).symm a = a ⊗ₜ 1 := rfl section CompatibleSMul variable (R S A B : Type*) [CommSemiring R] [CommSemiring S] [Semiring A] [Semiring B] variable [Algebra R A] [Algebra R B] [Algebra S A] [Algebra S B] variable [SMulCommClass R S A] [CompatibleSMul R S A B] /-- If A and B are both R- and S-algebras and their actions on them commute, and if the S-action on `A ⊗[R] B` can switch between the two factors, then there is a canonical S-algebra homomorphism from `A ⊗[S] B` to `A ⊗[R] B`. -/ def mapOfCompatibleSMul : A ⊗[S] B →ₐ[S] A ⊗[R] B := .ofLinearMap (_root_.TensorProduct.mapOfCompatibleSMul R S A B) rfl fun x ↦ x.induction_on (by simp) (fun _ _ y ↦ y.induction_on (by simp) (by simp) fun _ _ h h' ↦ by simp only [mul_add, map_add, h, h']) fun _ _ h h' _ ↦ by simp only [add_mul, map_add, h, h'] @[simp] theorem mapOfCompatibleSMul_tmul (m n) : mapOfCompatibleSMul R S A B (m ⊗ₜ n) = m ⊗ₜ n := rfl theorem mapOfCompatibleSMul_surjective : Function.Surjective (mapOfCompatibleSMul R S A B) := _root_.TensorProduct.mapOfCompatibleSMul_surjective R S A B attribute [local instance] SMulCommClass.symm /-- `mapOfCompatibleSMul R S A B` is also A-linear. -/ def mapOfCompatibleSMul' : A ⊗[S] B →ₐ[R] A ⊗[R] B := .ofLinearMap (_root_.TensorProduct.mapOfCompatibleSMul' R S A B) rfl (map_mul <| mapOfCompatibleSMul R S A B) /-- If the R- and S-actions on A and B satisfy `CompatibleSMul` both ways, then `A ⊗[S] B` is canonically isomorphic to `A ⊗[R] B`. -/ def equivOfCompatibleSMul [CompatibleSMul S R A B] : A ⊗[S] B ≃ₐ[S] A ⊗[R] B where __ := mapOfCompatibleSMul R S A B invFun := mapOfCompatibleSMul S R A B __ := _root_.TensorProduct.equivOfCompatibleSMul R S A B variable [Algebra R S] [CompatibleSMul R S S A] [CompatibleSMul S R S A] omit [SMulCommClass R S A] /-- If the R- and S- action on S and A satisfy `CompatibleSMul` both ways, then `S ⊗[R] A` is canonically isomorphic to `A`. -/ def lidOfCompatibleSMul : S ⊗[R] A ≃ₐ[S] A := (equivOfCompatibleSMul R S S A).symm.trans (TensorProduct.lid _ _) theorem lidOfCompatibleSMul_tmul (s a) : lidOfCompatibleSMul R S A (s ⊗ₜ[R] a) = s • a := rfl instance {R M N : Type*} [CommSemiring R] [AddCommGroup M] [AddCommGroup N] [Module R M] [Module R N] [Module ℚ M] [Module ℚ N] : CompatibleSMul R ℚ M N where smul_tmul q m n := by suffices q.den • ((q • m) ⊗ₜ[R] n) = q.den • (m ⊗ₜ[R] (q • n)) from smul_right_injective (M ⊗[R] N) (c := q.den) q.den_nz <| by norm_cast rw [smul_tmul', ← tmul_smul, ← smul_assoc, ← smul_assoc, nsmul_eq_mul, Rat.den_mul_eq_num] norm_cast rw [smul_tmul] end CompatibleSMul section variable (B) unseal mul in /-- The tensor product of R-algebras is commutative, up to algebra isomorphism. -/ protected def comm : A ⊗[R] B ≃ₐ[R] B ⊗[R] A := algEquivOfLinearEquivTensorProduct (_root_.TensorProduct.comm R A B) (fun _ _ _ _ => rfl) rfl @[simp] theorem comm_toLinearEquiv : (Algebra.TensorProduct.comm R A B).toLinearEquiv = _root_.TensorProduct.comm R A B := rfl variable {A B} in @[simp] theorem comm_tmul (a : A) (b : B) : TensorProduct.comm R A B (a ⊗ₜ b) = b ⊗ₜ a := rfl variable {A B} in @[simp] theorem comm_symm_tmul (a : A) (b : B) : (TensorProduct.comm R A B).symm (b ⊗ₜ a) = a ⊗ₜ b := rfl theorem comm_symm : (TensorProduct.comm R A B).symm = TensorProduct.comm R B A := by ext; rfl @[simp] lemma comm_comp_includeLeft : (TensorProduct.comm R A B : A ⊗[R] B →ₐ[R] B ⊗[R] A).comp includeLeft = includeRight := rfl @[simp] lemma comm_comp_includeRight : (TensorProduct.comm R A B : A ⊗[R] B →ₐ[R] B ⊗[R] A).comp includeRight = includeLeft := rfl theorem adjoin_tmul_eq_top : adjoin R { t : A ⊗[R] B | ∃ a b, a ⊗ₜ[R] b = t } = ⊤ := top_le_iff.mp <| (top_le_iff.mpr <| span_tmul_eq_top R A B).trans (span_le_adjoin R _) end section variable {R A} unseal mul in theorem assoc_aux_1 (a₁ a₂ : A) (b₁ b₂ : B) (c₁ c₂ : C) : (TensorProduct.assoc R A B C) (((a₁ * a₂) ⊗ₜ[R] (b₁ * b₂)) ⊗ₜ[R] (c₁ * c₂)) = (TensorProduct.assoc R A B C) ((a₁ ⊗ₜ[R] b₁) ⊗ₜ[R] c₁) * (TensorProduct.assoc R A B C) ((a₂ ⊗ₜ[R] b₂) ⊗ₜ[R] c₂) := rfl theorem assoc_aux_2 : (TensorProduct.assoc R A B C) ((1 ⊗ₜ[R] 1) ⊗ₜ[R] 1) = 1 := rfl variable (R A B C) -- Porting note: much nicer than Lean 3 proof /-- The associator for tensor product of R-algebras, as an algebra isomorphism. -/ protected def assoc : (A ⊗[R] B) ⊗[R] C ≃ₐ[R] A ⊗[R] B ⊗[R] C := algEquivOfLinearEquivTripleTensorProduct (_root_.TensorProduct.assoc R A B C) Algebra.TensorProduct.assoc_aux_1 Algebra.TensorProduct.assoc_aux_2 @[simp] theorem assoc_toLinearEquiv : (Algebra.TensorProduct.assoc R A B C).toLinearEquiv = _root_.TensorProduct.assoc R A B C := rfl variable {A B C} @[simp] theorem assoc_tmul (a : A) (b : B) (c : C) : Algebra.TensorProduct.assoc R A B C ((a ⊗ₜ b) ⊗ₜ c) = a ⊗ₜ (b ⊗ₜ c) := rfl @[simp] theorem assoc_symm_tmul (a : A) (b : B) (c : C) : (Algebra.TensorProduct.assoc R A B C).symm (a ⊗ₜ (b ⊗ₜ c)) = (a ⊗ₜ b) ⊗ₜ c := rfl end section variable (T A B : Type*) [CommSemiring T] [CommSemiring A] [CommSemiring B] [Algebra R T] [Algebra R A] [Algebra R B] [Algebra T A] [IsScalarTower R T A] [Algebra S A] [IsScalarTower R S A] [Algebra S T] [IsScalarTower S T A] /-- The natural isomorphism `A ⊗[S] (S ⊗[R] B) ≃ₐ[T] A ⊗[R] B`. -/ noncomputable def cancelBaseChange : A ⊗[S] (S ⊗[R] B) ≃ₐ[T] A ⊗[R] B := AlgEquiv.symm <| AlgEquiv.ofLinearEquiv (TensorProduct.AlgebraTensorModule.cancelBaseChange R S T A B).symm (by simp [Algebra.TensorProduct.one_def]) <| LinearMap.map_mul_of_map_mul_tmul (fun _ _ _ _ ↦ by simp) @[simp] lemma cancelBaseChange_tmul (a : A) (s : S) (b : B) : Algebra.TensorProduct.cancelBaseChange R S T A B (a ⊗ₜ (s ⊗ₜ b)) = (s • a) ⊗ₜ b := TensorProduct.AlgebraTensorModule.cancelBaseChange_tmul R S T a b s @[simp] lemma cancelBaseChange_symm_tmul (a : A) (b : B) : (Algebra.TensorProduct.cancelBaseChange R S T A B).symm (a ⊗ₜ b) = a ⊗ₜ (1 ⊗ₜ b) := TensorProduct.AlgebraTensorModule.cancelBaseChange_symm_tmul R S T a b end variable {R S A} /-- The tensor product of a pair of algebra morphisms. -/ def map (f : A →ₐ[S] C) (g : B →ₐ[R] D) : A ⊗[R] B →ₐ[S] C ⊗[R] D := algHomOfLinearMapTensorProduct (AlgebraTensorModule.map f.toLinearMap g.toLinearMap) (by simp) (by simp [one_def]) @[simp] theorem map_tmul (f : A →ₐ[S] C) (g : B →ₐ[R] D) (a : A) (b : B) : map f g (a ⊗ₜ b) = f a ⊗ₜ g b := rfl @[simp] theorem map_id : map (.id S A) (.id R B) = .id S _ := ext (AlgHom.ext fun _ => rfl) (AlgHom.ext fun _ => rfl) theorem map_comp (f₂ : C →ₐ[S] E) (f₁ : A →ₐ[S] C) (g₂ : D →ₐ[R] F) (g₁ : B →ₐ[R] D) : map (f₂.comp f₁) (g₂.comp g₁) = (map f₂ g₂).comp (map f₁ g₁) := ext (AlgHom.ext fun _ => rfl) (AlgHom.ext fun _ => rfl) lemma map_id_comp (g₂ : D →ₐ[R] F) (g₁ : B →ₐ[R] D) : map (AlgHom.id S A) (g₂.comp g₁) = (map (AlgHom.id S A) g₂).comp (map (AlgHom.id S A) g₁) := ext (AlgHom.ext fun _ => rfl) (AlgHom.ext fun _ => rfl) lemma map_comp_id (f₂ : C →ₐ[S] E) (f₁ : A →ₐ[S] C) : map (f₂.comp f₁) (AlgHom.id R E) = (map f₂ (AlgHom.id R E)).comp (map f₁ (AlgHom.id R E)) := ext (AlgHom.ext fun _ => rfl) (AlgHom.ext fun _ => rfl) @[simp] theorem map_comp_includeLeft (f : A →ₐ[S] C) (g : B →ₐ[R] D) : (map f g).comp includeLeft = includeLeft.comp f := AlgHom.ext <| by simp @[simp] theorem map_restrictScalars_comp_includeRight (f : A →ₐ[S] C) (g : B →ₐ[R] D) : ((map f g).restrictScalars R).comp includeRight = includeRight.comp g := AlgHom.ext <| by simp @[simp] theorem map_comp_includeRight (f : A →ₐ[R] C) (g : B →ₐ[R] D) : (map f g).comp includeRight = includeRight.comp g := map_restrictScalars_comp_includeRight f g theorem map_range (f : A →ₐ[R] C) (g : B →ₐ[R] D) : (map f g).range = (includeLeft.comp f).range ⊔ (includeRight.comp g).range := by apply le_antisymm · rw [← map_top, ← adjoin_tmul_eq_top, ← adjoin_image, adjoin_le_iff] rintro _ ⟨_, ⟨a, b, rfl⟩, rfl⟩ rw [map_tmul, ← mul_one (f a), ← one_mul (g b), ← tmul_mul_tmul] exact mul_mem_sup (AlgHom.mem_range_self _ a) (AlgHom.mem_range_self _ b) · rw [← map_comp_includeLeft f g, ← map_comp_includeRight f g] exact sup_le (AlgHom.range_comp_le_range _ _) (AlgHom.range_comp_le_range _ _) lemma comm_comp_map (f : A →ₐ[R] C) (g : B →ₐ[R] D) : (TensorProduct.comm R C D : C ⊗[R] D →ₐ[R] D ⊗[R] C).comp (Algebra.TensorProduct.map f g) = (Algebra.TensorProduct.map g f).comp (TensorProduct.comm R A B).toAlgHom := by
ext <;> rfl lemma comm_comp_map_apply (f : A →ₐ[R] C) (g : B →ₐ[R] D) (x) : TensorProduct.comm R C D (Algebra.TensorProduct.map f g x) = (Algebra.TensorProduct.map g f) (TensorProduct.comm R A B x) :=
Mathlib/RingTheory/TensorProduct/Basic.lean
1,049
1,053
/- Copyright (c) 2021 Justus Springer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Justus Springer -/ import Mathlib.CategoryTheory.Sites.Spaces import Mathlib.Topology.Sheaves.Sheaf import Mathlib.CategoryTheory.Sites.DenseSubsite.Basic /-! # Coverings and sieves; from sheaves on sites and sheaves on spaces In this file, we connect coverings in a topological space to sieves in the associated Grothendieck topology, in preparation of connecting the sheaf condition on sites to the various sheaf conditions on spaces. We also specialize results about sheaves on sites to sheaves on spaces; we show that the inclusion functor from a topological basis to `TopologicalSpace.Opens` is cover dense, that open maps induce cover preserving functors, and that open embeddings induce continuous functors. -/ noncomputable section open CategoryTheory TopologicalSpace Topology universe w v u namespace TopCat.Presheaf variable {X : TopCat.{w}} /-- Given a presieve `R` on `U`, we obtain a covering family of open sets in `X`, by taking as index type the type of dependent pairs `(V, f)`, where `f : V ⟶ U` is in `R`. -/ def coveringOfPresieve (U : Opens X) (R : Presieve U) : (ΣV, { f : V ⟶ U // R f }) → Opens X := fun f => f.1 @[simp] theorem coveringOfPresieve_apply (U : Opens X) (R : Presieve U) (f : Σ V, { f : V ⟶ U // R f }) : coveringOfPresieve U R f = f.1 := rfl namespace coveringOfPresieve variable (U : Opens X) (R : Presieve U) /-- If `R` is a presieve in the grothendieck topology on `Opens X`, the covering family associated to `R` really is _covering_, i.e. the union of all open sets equals `U`. -/ theorem iSup_eq_of_mem_grothendieck (hR : Sieve.generate R ∈ Opens.grothendieckTopology X U) : iSup (coveringOfPresieve U R) = U := by apply le_antisymm · refine iSup_le ?_ intro f exact f.2.1.le intro x hxU rw [Opens.coe_iSup, Set.mem_iUnion] obtain ⟨V, iVU, ⟨W, iVW, iWU, hiWU, -⟩, hxV⟩ := hR x hxU exact ⟨⟨W, ⟨iWU, hiWU⟩⟩, iVW.le hxV⟩ end coveringOfPresieve /-- Given a family of opens `U : ι → Opens X` and any open `Y : Opens X`, we obtain a presieve on `Y` by declaring that a morphism `f : V ⟶ Y` is a member of the presieve if and only if there exists an index `i : ι` such that `V = U i`. -/ def presieveOfCoveringAux {ι : Type v} (U : ι → Opens X) (Y : Opens X) : Presieve Y := fun V _ => ∃ i, V = U i /-- Take `Y` to be `iSup U` and obtain a presieve over `iSup U`. -/ def presieveOfCovering {ι : Type v} (U : ι → Opens X) : Presieve (iSup U) := presieveOfCoveringAux U (iSup U) /-- Given a presieve `R` on `Y`, if we take its associated family of opens via `coveringOfPresieve` (which may not cover `Y` if `R` is not covering), and take the presieve on `Y` associated to the family of opens via `presieveOfCoveringAux`, then we get back the original presieve `R`. -/ @[simp] theorem covering_presieve_eq_self {Y : Opens X} (R : Presieve Y) : presieveOfCoveringAux (coveringOfPresieve Y R) Y = R := by funext Z ext f exact ⟨fun ⟨⟨_, f', h⟩, rfl⟩ => by rwa [Subsingleton.elim f f'], fun h => ⟨⟨Z, f, h⟩, rfl⟩⟩ namespace presieveOfCovering variable {ι : Type v} (U : ι → Opens X) /-- The sieve generated by `presieveOfCovering U` is a member of the grothendieck topology. -/ theorem mem_grothendieckTopology : Sieve.generate (presieveOfCovering U) ∈ Opens.grothendieckTopology X (iSup U) := by intro x hx obtain ⟨i, hxi⟩ := Opens.mem_iSup.mp hx exact ⟨U i, Opens.leSupr U i, ⟨U i, 𝟙 _, Opens.leSupr U i, ⟨i, rfl⟩, Category.id_comp _⟩, hxi⟩ /-- An index `i : ι` can be turned into a dependent pair `(V, f)`, where `V` is an open set and `f : V ⟶ iSup U` is a member of `presieveOfCovering U f`. -/ def homOfIndex (i : ι) : ΣV, { f : V ⟶ iSup U // presieveOfCovering U f } := ⟨U i, Opens.leSupr U i, i, rfl⟩ /-- By using the axiom of choice, a dependent pair `(V, f)` where `f : V ⟶ iSup U` is a member of `presieveOfCovering U f` can be turned into an index `i : ι`, such that `V = U i`. -/ def indexOfHom (f : Σ V, { f : V ⟶ iSup U // presieveOfCovering U f }) : ι := f.2.2.choose theorem indexOfHom_spec (f : Σ V, { f : V ⟶ iSup U // presieveOfCovering U f }) : f.1 = U (indexOfHom U f) := f.2.2.choose_spec end presieveOfCovering end TopCat.Presheaf namespace TopCat.Opens variable {X : TopCat} {ι : Type*} theorem coverDense_iff_isBasis [Category ι] (B : ι ⥤ Opens X) : B.IsCoverDense (Opens.grothendieckTopology X) ↔ Opens.IsBasis (Set.range B.obj) := by rw [Opens.isBasis_iff_nbhd] constructor · intro hd U x hx; rcases hd.1 U x hx with ⟨V, f, ⟨i, f₁, f₂, _⟩, hV⟩ exact ⟨B.obj i, ⟨i, rfl⟩, f₁.le hV, f₂.le⟩ intro hb; constructor; intro U x hx; rcases hb hx with ⟨_, ⟨i, rfl⟩, hx, hi⟩ exact ⟨B.obj i, ⟨⟨hi⟩⟩, ⟨⟨i, 𝟙 _, ⟨⟨hi⟩⟩, rfl⟩⟩, hx⟩ theorem coverDense_inducedFunctor {B : ι → Opens X} (h : Opens.IsBasis (Set.range B)) : (inducedFunctor B).IsCoverDense (Opens.grothendieckTopology X) := (coverDense_iff_isBasis _).2 h end TopCat.Opens section IsOpenEmbedding open TopCat.Presheaf Opposite variable {C : Type u} [Category.{v} C] variable {X Y : TopCat.{w}} {f : X ⟶ Y} {F : Y.Presheaf C} theorem Topology.IsOpenEmbedding.compatiblePreserving (hf : IsOpenEmbedding f) : CompatiblePreserving (Opens.grothendieckTopology Y) hf.isOpenMap.functor := by haveI : Mono f := (TopCat.mono_iff_injective f).mpr hf.injective apply compatiblePreservingOfDownwardsClosed intro U V i refine ⟨(Opens.map f).obj V, eqToIso <| Opens.ext <| Set.image_preimage_eq_of_subset fun x h ↦ ?_⟩ obtain ⟨_, _, rfl⟩ := i.le h exact ⟨_, rfl⟩ theorem IsOpenMap.coverPreserving (hf : IsOpenMap f) : CoverPreserving (Opens.grothendieckTopology X) (Opens.grothendieckTopology Y) hf.functor := by constructor rintro U S hU _ ⟨x, hx, rfl⟩ obtain ⟨V, i, hV, hxV⟩ := hU x hx exact ⟨_, hf.functor.map i, ⟨_, i, 𝟙 _, hV, rfl⟩, Set.mem_image_of_mem f hxV⟩ lemma Topology.IsOpenEmbedding.functor_isContinuous (h : IsOpenEmbedding f) : h.isOpenMap.functor.IsContinuous (Opens.grothendieckTopology X) (Opens.grothendieckTopology Y) := by apply Functor.isContinuous_of_coverPreserving · exact h.compatiblePreserving · exact h.isOpenMap.coverPreserving theorem TopCat.Presheaf.isSheaf_of_isOpenEmbedding (h : IsOpenEmbedding f) (hF : F.IsSheaf) : IsSheaf (h.isOpenMap.functor.op ⋙ F) := by
have := h.functor_isContinuous exact Functor.op_comp_isSheaf _ _ _ ⟨_, hF⟩ variable (f) instance : RepresentablyFlat (Opens.map f) := by
Mathlib/Topology/Sheaves/SheafCondition/Sites.lean
171
176
/- Copyright (c) 2023 Adam Topaz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Adam Topaz, Dagur Asgeirsson, Filippo A. E. Nuccio, Riccardo Brasca -/ import Mathlib.Topology.Category.CompHausLike.Limits import Mathlib.Topology.Category.Stonean.Basic /-! # Explicit limits and colimits This file applies the general API for explicit limits and colimits in `CompHausLike P` (see the file `Mathlib.Topology.Category.CompHausLike.Limits`) to the special case of `Stonean`. -/ universe w u open CategoryTheory Limits CompHausLike Topology namespace Stonean instance : HasExplicitFiniteCoproducts.{w, u} (fun Y ↦ ExtremallyDisconnected Y) where hasProp _ := { hasProp := show ExtremallyDisconnected (Σ (_a : _), _) from inferInstance} variable {X Y Z : Stonean} {f : X ⟶ Z} (i : Y ⟶ Z) (hi : IsOpenEmbedding f) include hi lemma extremallyDisconnected_preimage : ExtremallyDisconnected (i ⁻¹' (Set.range f)) where open_closure U hU := by have h : IsClopen (i ⁻¹' (Set.range f)) := ⟨IsClosed.preimage i.hom.continuous (isCompact_range f.hom.continuous).isClosed, IsOpen.preimage i.hom.continuous hi.isOpen_range⟩ rw [← (closure U).preimage_image_eq Subtype.coe_injective, ← h.1.isClosedEmbedding_subtypeVal.closure_image_eq U] exact isOpen_induced (ExtremallyDisconnected.open_closure _ (h.2.isOpenEmbedding_subtypeVal.isOpenMap U hU)) lemma extremallyDisconnected_pullback : ExtremallyDisconnected {xy : X × Y | f xy.1 = i xy.2} := have := extremallyDisconnected_preimage i hi let e := (TopCat.pullbackHomeoPreimage i i.hom.2 f hi.isEmbedding).symm let e' : {xy : X × Y | f xy.1 = i xy.2} ≃ₜ {xy : Y × X | i xy.1 = f xy.2} := by exact TopCat.homeoOfIso ((TopCat.pullbackIsoProdSubtype f i).symm ≪≫ pullbackSymmetry _ _ ≪≫ (TopCat.pullbackIsoProdSubtype i f)) extremallyDisconnected_of_homeo (e.trans e'.symm) instance : HasExplicitPullbacksOfInclusions (fun (Y : TopCat.{u}) ↦ ExtremallyDisconnected Y) := by apply CompHausLike.hasPullbacksOfInclusions intro _ _ _ _ _ hi exact ⟨extremallyDisconnected_pullback _ hi⟩ example : FinitaryExtensive Stonean.{u} := inferInstance noncomputable example : PreservesFiniteCoproducts Stonean.toCompHaus := inferInstance noncomputable example : PreservesFiniteCoproducts Stonean.toProfinite := inferInstance end Stonean
Mathlib/Topology/Category/Stonean/Limits.lean
223
234
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Data.Nat.Find import Mathlib.Data.Stream.Init import Mathlib.Tactic.Common /-! # Coinductive formalization of unbounded computations. This file provides a `Computation` type where `Computation α` is the type of unbounded computations returning `α`. -/ open Function universe u v w /- coinductive Computation (α : Type u) : Type u | pure : α → Computation α | think : Computation α → Computation α -/ /-- `Computation α` is the type of unbounded computations returning `α`. An element of `Computation α` is an infinite sequence of `Option α` such that if `f n = some a` for some `n` then it is constantly `some a` after that. -/ def Computation (α : Type u) : Type u := { f : Stream' (Option α) // ∀ ⦃n a⦄, f n = some a → f (n + 1) = some a } namespace Computation variable {α : Type u} {β : Type v} {γ : Type w} -- constructors /-- `pure a` is the computation that immediately terminates with result `a`. -/ def pure (a : α) : Computation α := ⟨Stream'.const (some a), fun _ _ => id⟩ instance : CoeTC α (Computation α) := ⟨pure⟩ -- note [use has_coe_t] /-- `think c` is the computation that delays for one "tick" and then performs computation `c`. -/ def think (c : Computation α) : Computation α := ⟨Stream'.cons none c.1, fun n a h => by rcases n with - | n · contradiction · exact c.2 h⟩ /-- `thinkN c n` is the computation that delays for `n` ticks and then performs computation `c`. -/ def thinkN (c : Computation α) : ℕ → Computation α | 0 => c | n + 1 => think (thinkN c n) -- check for immediate result /-- `head c` is the first step of computation, either `some a` if `c = pure a` or `none` if `c = think c'`. -/ def head (c : Computation α) : Option α := c.1.head -- one step of computation /-- `tail c` is the remainder of computation, either `c` if `c = pure a` or `c'` if `c = think c'`. -/ def tail (c : Computation α) : Computation α := ⟨c.1.tail, fun _ _ h => c.2 h⟩ /-- `empty α` is the computation that never returns, an infinite sequence of `think`s. -/ def empty (α) : Computation α := ⟨Stream'.const none, fun _ _ => id⟩ instance : Inhabited (Computation α) := ⟨empty _⟩ /-- `runFor c n` evaluates `c` for `n` steps and returns the result, or `none` if it did not terminate after `n` steps. -/ def runFor : Computation α → ℕ → Option α := Subtype.val /-- `destruct c` is the destructor for `Computation α` as a coinductive type. It returns `inl a` if `c = pure a` and `inr c'` if `c = think c'`. -/ def destruct (c : Computation α) : α ⊕ (Computation α) := match c.1 0 with | none => Sum.inr (tail c) | some a => Sum.inl a /-- `run c` is an unsound meta function that runs `c` to completion, possibly resulting in an infinite loop in the VM. -/ unsafe def run : Computation α → α | c => match destruct c with | Sum.inl a => a | Sum.inr ca => run ca theorem destruct_eq_pure {s : Computation α} {a : α} : destruct s = Sum.inl a → s = pure a := by dsimp [destruct] induction' f0 : s.1 0 with _ <;> intro h · contradiction · apply Subtype.eq funext n induction' n with n IH · injection h with h' rwa [h'] at f0 · exact s.2 IH theorem destruct_eq_think {s : Computation α} {s'} : destruct s = Sum.inr s' → s = think s' := by dsimp [destruct] induction' f0 : s.1 0 with a' <;> intro h · injection h with h' rw [← h'] obtain ⟨f, al⟩ := s apply Subtype.eq dsimp [think, tail] rw [← f0] exact (Stream'.eta f).symm · contradiction @[simp] theorem destruct_pure (a : α) : destruct (pure a) = Sum.inl a := rfl @[simp] theorem destruct_think : ∀ s : Computation α, destruct (think s) = Sum.inr s | ⟨_, _⟩ => rfl @[simp] theorem destruct_empty : destruct (empty α) = Sum.inr (empty α) := rfl @[simp] theorem head_pure (a : α) : head (pure a) = some a := rfl @[simp] theorem head_think (s : Computation α) : head (think s) = none := rfl @[simp] theorem head_empty : head (empty α) = none := rfl @[simp] theorem tail_pure (a : α) : tail (pure a) = pure a := rfl @[simp] theorem tail_think (s : Computation α) : tail (think s) = s := by obtain ⟨f, al⟩ := s; apply Subtype.eq; dsimp [tail, think] @[simp] theorem tail_empty : tail (empty α) = empty α := rfl theorem think_empty : empty α = think (empty α) := destruct_eq_think destruct_empty /-- Recursion principle for computations, compare with `List.recOn`. -/ def recOn {C : Computation α → Sort v} (s : Computation α) (h1 : ∀ a, C (pure a)) (h2 : ∀ s, C (think s)) : C s := match H : destruct s with | Sum.inl v => by rw [destruct_eq_pure H] apply h1 | Sum.inr v => match v with | ⟨a, s'⟩ => by rw [destruct_eq_think H] apply h2 /-- Corecursor constructor for `corec` -/ def Corec.f (f : β → α ⊕ β) : α ⊕ β → Option α × (α ⊕ β) | Sum.inl a => (some a, Sum.inl a) | Sum.inr b => (match f b with | Sum.inl a => some a | Sum.inr _ => none, f b) /-- `corec f b` is the corecursor for `Computation α` as a coinductive type. If `f b = inl a` then `corec f b = pure a`, and if `f b = inl b'` then `corec f b = think (corec f b')`. -/ def corec (f : β → α ⊕ β) (b : β) : Computation α := by refine ⟨Stream'.corec' (Corec.f f) (Sum.inr b), fun n a' h => ?_⟩ rw [Stream'.corec'_eq] change Stream'.corec' (Corec.f f) (Corec.f f (Sum.inr b)).2 n = some a' revert h; generalize Sum.inr b = o; revert o induction' n with n IH <;> intro o · change (Corec.f f o).1 = some a' → (Corec.f f (Corec.f f o).2).1 = some a' rcases o with _ | b <;> intro h · exact h unfold Corec.f at *; split <;> simp_all · rw [Stream'.corec'_eq (Corec.f f) (Corec.f f o).2, Stream'.corec'_eq (Corec.f f) o] exact IH (Corec.f f o).2 /-- left map of `⊕` -/ def lmap (f : α → β) : α ⊕ γ → β ⊕ γ | Sum.inl a => Sum.inl (f a) | Sum.inr b => Sum.inr b /-- right map of `⊕` -/ def rmap (f : β → γ) : α ⊕ β → α ⊕ γ | Sum.inl a => Sum.inl a | Sum.inr b => Sum.inr (f b) attribute [simp] lmap rmap @[simp] theorem corec_eq (f : β → α ⊕ β) (b : β) : destruct (corec f b) = rmap (corec f) (f b) := by dsimp [corec, destruct] rw [show Stream'.corec' (Corec.f f) (Sum.inr b) 0 = Sum.rec Option.some (fun _ ↦ none) (f b) by dsimp [Corec.f, Stream'.corec', Stream'.corec, Stream'.map, Stream'.get, Stream'.iterate] match (f b) with | Sum.inl x => rfl | Sum.inr x => rfl ] induction' h : f b with a b'; · rfl dsimp [Corec.f, destruct] apply congr_arg; apply Subtype.eq dsimp [corec, tail] rw [Stream'.corec'_eq, Stream'.tail_cons] dsimp [Corec.f]; rw [h] section Bisim variable (R : Computation α → Computation α → Prop) /-- bisimilarity relation -/ local infixl:50 " ~ " => R /-- Bisimilarity over a sum of `Computation`s -/ def BisimO : α ⊕ (Computation α) → α ⊕ (Computation α) → Prop | Sum.inl a, Sum.inl a' => a = a' | Sum.inr s, Sum.inr s' => R s s' | _, _ => False attribute [simp] BisimO attribute [nolint simpNF] BisimO.eq_3 /-- Attribute expressing bisimilarity over two `Computation`s -/ def IsBisimulation := ∀ ⦃s₁ s₂⦄, s₁ ~ s₂ → BisimO R (destruct s₁) (destruct s₂) -- If two computations are bisimilar, then they are equal theorem eq_of_bisim (bisim : IsBisimulation R) {s₁ s₂} (r : s₁ ~ s₂) : s₁ = s₂ := by apply Subtype.eq apply Stream'.eq_of_bisim fun x y => ∃ s s' : Computation α, s.1 = x ∧ s'.1 = y ∧ R s s' · dsimp [Stream'.IsBisimulation] intro t₁ t₂ e match t₁, t₂, e with | _, _, ⟨s, s', rfl, rfl, r⟩ => suffices head s = head s' ∧ R (tail s) (tail s') from And.imp id (fun r => ⟨tail s, tail s', by cases s; rfl, by cases s'; rfl, r⟩) this have h := bisim r; revert r h apply recOn s _ _ <;> intro r' <;> apply recOn s' _ _ <;> intro a' r h · constructor <;> dsimp at h · rw [h] · rw [h] at r rw [tail_pure, tail_pure,h] assumption · rw [destruct_pure, destruct_think] at h exact False.elim h · rw [destruct_pure, destruct_think] at h exact False.elim h · simp_all · exact ⟨s₁, s₂, rfl, rfl, r⟩ end Bisim -- It's more of a stretch to use ∈ for this relation, but it -- asserts that the computation limits to the given value. /-- Assertion that a `Computation` limits to a given value -/ protected def Mem (s : Computation α) (a : α) := some a ∈ s.1 instance : Membership α (Computation α) := ⟨Computation.Mem⟩ theorem le_stable (s : Computation α) {a m n} (h : m ≤ n) : s.1 m = some a → s.1 n = some a := by obtain ⟨f, al⟩ := s induction' h with n _ IH exacts [id, fun h2 => al (IH h2)] theorem mem_unique {s : Computation α} {a b : α} : a ∈ s → b ∈ s → a = b | ⟨m, ha⟩, ⟨n, hb⟩ => by injection (le_stable s (le_max_left m n) ha.symm).symm.trans (le_stable s (le_max_right m n) hb.symm) theorem Mem.left_unique : Relator.LeftUnique ((· ∈ ·) : α → Computation α → Prop) := fun _ _ _ => mem_unique /-- `Terminates s` asserts that the computation `s` eventually terminates with some value. -/ class Terminates (s : Computation α) : Prop where /-- assertion that there is some term `a` such that the `Computation` terminates -/ term : ∃ a, a ∈ s theorem terminates_iff (s : Computation α) : Terminates s ↔ ∃ a, a ∈ s := ⟨fun h => h.1, Terminates.mk⟩ theorem terminates_of_mem {s : Computation α} {a : α} (h : a ∈ s) : Terminates s := ⟨⟨a, h⟩⟩ theorem terminates_def (s : Computation α) : Terminates s ↔ ∃ n, (s.1 n).isSome := ⟨fun ⟨⟨a, n, h⟩⟩ => ⟨n, by dsimp [Stream'.get] at h rw [← h] exact rfl⟩, fun ⟨n, h⟩ => ⟨⟨Option.get _ h, n, (Option.eq_some_of_isSome h).symm⟩⟩⟩ theorem ret_mem (a : α) : a ∈ pure a := Exists.intro 0 rfl theorem eq_of_pure_mem {a a' : α} (h : a' ∈ pure a) : a' = a := mem_unique h (ret_mem _) @[simp] theorem mem_pure_iff (a b : α) : a ∈ pure b ↔ a = b := ⟨eq_of_pure_mem, fun h => h ▸ ret_mem _⟩ instance ret_terminates (a : α) : Terminates (pure a) := terminates_of_mem (ret_mem _) theorem think_mem {s : Computation α} {a} : a ∈ s → a ∈ think s | ⟨n, h⟩ => ⟨n + 1, h⟩ instance think_terminates (s : Computation α) : ∀ [Terminates s], Terminates (think s) | ⟨⟨a, n, h⟩⟩ => ⟨⟨a, n + 1, h⟩⟩ theorem of_think_mem {s : Computation α} {a} : a ∈ think s → a ∈ s | ⟨n, h⟩ => by rcases n with - | n' · contradiction · exact ⟨n', h⟩ theorem of_think_terminates {s : Computation α} : Terminates (think s) → Terminates s | ⟨⟨a, h⟩⟩ => ⟨⟨a, of_think_mem h⟩⟩ theorem not_mem_empty (a : α) : a ∉ empty α := fun ⟨n, h⟩ => by contradiction theorem not_terminates_empty : ¬Terminates (empty α) := fun ⟨⟨a, h⟩⟩ => not_mem_empty a h theorem eq_empty_of_not_terminates {s} (H : ¬Terminates s) : s = empty α := by apply Subtype.eq; funext n induction' h : s.val n with _; · rfl refine absurd ?_ H; exact ⟨⟨_, _, h.symm⟩⟩ theorem thinkN_mem {s : Computation α} {a} : ∀ n, a ∈ thinkN s n ↔ a ∈ s | 0 => Iff.rfl | n + 1 => Iff.trans ⟨of_think_mem, think_mem⟩ (thinkN_mem n) instance thinkN_terminates (s : Computation α) : ∀ [Terminates s] (n), Terminates (thinkN s n) | ⟨⟨a, h⟩⟩, n => ⟨⟨a, (thinkN_mem n).2 h⟩⟩ theorem of_thinkN_terminates (s : Computation α) (n) : Terminates (thinkN s n) → Terminates s | ⟨⟨a, h⟩⟩ => ⟨⟨a, (thinkN_mem _).1 h⟩⟩ /-- `Promises s a`, or `s ~> a`, asserts that although the computation `s` may not terminate, if it does, then the result is `a`. -/ def Promises (s : Computation α) (a : α) : Prop := ∀ ⦃a'⦄, a' ∈ s → a = a' /-- `Promises s a`, or `s ~> a`, asserts that although the computation `s` may not terminate, if it does, then the result is `a`. -/ scoped infixl:50 " ~> " => Promises theorem mem_promises {s : Computation α} {a : α} : a ∈ s → s ~> a := fun h _ => mem_unique h theorem empty_promises (a : α) : empty α ~> a := fun _ h => absurd h (not_mem_empty _) section get variable (s : Computation α) [h : Terminates s] /-- `length s` gets the number of steps of a terminating computation -/ def length : ℕ := Nat.find ((terminates_def _).1 h) /-- `get s` returns the result of a terminating computation -/ def get : α := Option.get _ (Nat.find_spec <| (terminates_def _).1 h) theorem get_mem : get s ∈ s := Exists.intro (length s) (Option.eq_some_of_isSome _).symm theorem get_eq_of_mem {a} : a ∈ s → get s = a := mem_unique (get_mem _) theorem mem_of_get_eq {a} : get s = a → a ∈ s := by intro h; rw [← h]; apply get_mem @[simp] theorem get_think : get (think s) = get s := get_eq_of_mem _ <| let ⟨n, h⟩ := get_mem s ⟨n + 1, h⟩ @[simp] theorem get_thinkN (n) : get (thinkN s n) = get s := get_eq_of_mem _ <| (thinkN_mem _).2 (get_mem _) theorem get_promises : s ~> get s := fun _ => get_eq_of_mem _ theorem mem_of_promises {a} (p : s ~> a) : a ∈ s := by obtain ⟨h⟩ := h obtain ⟨a', h⟩ := h rw [p h] exact h theorem get_eq_of_promises {a} : s ~> a → get s = a := get_eq_of_mem _ ∘ mem_of_promises _ end get /-- `Results s a n` completely characterizes a terminating computation: it asserts that `s` terminates after exactly `n` steps, with result `a`. -/ def Results (s : Computation α) (a : α) (n : ℕ) := ∃ h : a ∈ s, @length _ s (terminates_of_mem h) = n theorem results_of_terminates (s : Computation α) [_T : Terminates s] : Results s (get s) (length s) := ⟨get_mem _, rfl⟩ theorem results_of_terminates' (s : Computation α) [T : Terminates s] {a} (h : a ∈ s) : Results s a (length s) := by rw [← get_eq_of_mem _ h]; apply results_of_terminates theorem Results.mem {s : Computation α} {a n} : Results s a n → a ∈ s | ⟨m, _⟩ => m theorem Results.terminates {s : Computation α} {a n} (h : Results s a n) : Terminates s := terminates_of_mem h.mem theorem Results.length {s : Computation α} {a n} [_T : Terminates s] : Results s a n → length s = n | ⟨_, h⟩ => h theorem Results.val_unique {s : Computation α} {a b m n} (h1 : Results s a m) (h2 : Results s b n) : a = b := mem_unique h1.mem h2.mem theorem Results.len_unique {s : Computation α} {a b m n} (h1 : Results s a m) (h2 : Results s b n) : m = n := by haveI := h1.terminates; haveI := h2.terminates; rw [← h1.length, h2.length] theorem exists_results_of_mem {s : Computation α} {a} (h : a ∈ s) : ∃ n, Results s a n := haveI := terminates_of_mem h ⟨_, results_of_terminates' s h⟩ @[simp] theorem get_pure (a : α) : get (pure a) = a := get_eq_of_mem _ ⟨0, rfl⟩ @[simp] theorem length_pure (a : α) : length (pure a) = 0 := let h := Computation.ret_terminates a Nat.eq_zero_of_le_zero <| Nat.find_min' ((terminates_def (pure a)).1 h) rfl theorem results_pure (a : α) : Results (pure a) a 0 := ⟨ret_mem a, length_pure _⟩ @[simp] theorem length_think (s : Computation α) [h : Terminates s] : length (think s) = length s + 1 := by apply le_antisymm · exact Nat.find_min' _ (Nat.find_spec ((terminates_def _).1 h)) · have : (Option.isSome ((think s).val (length (think s))) : Prop) := Nat.find_spec ((terminates_def _).1 s.think_terminates) revert this; rcases length (think s) with - | n <;> intro this · simp [think, Stream'.cons] at this · apply Nat.succ_le_succ apply Nat.find_min' apply this theorem results_think {s : Computation α} {a n} (h : Results s a n) : Results (think s) a (n + 1) := haveI := h.terminates ⟨think_mem h.mem, by rw [length_think, h.length]⟩ theorem of_results_think {s : Computation α} {a n} (h : Results (think s) a n) : ∃ m, Results s a m ∧ n = m + 1 := by haveI := of_think_terminates h.terminates have := results_of_terminates' _ (of_think_mem h.mem) exact ⟨_, this, Results.len_unique h (results_think this)⟩ @[simp] theorem results_think_iff {s : Computation α} {a n} : Results (think s) a (n + 1) ↔ Results s a n := ⟨fun h => by let ⟨n', r, e⟩ := of_results_think h injection e with h'; rwa [h'], results_think⟩ theorem results_thinkN {s : Computation α} {a m} : ∀ n, Results s a m → Results (thinkN s n) a (m + n) | 0, h => h | n + 1, h => results_think (results_thinkN n h) theorem results_thinkN_pure (a : α) (n) : Results (thinkN (pure a) n) a n := by have := results_thinkN n (results_pure a); rwa [Nat.zero_add] at this @[simp] theorem length_thinkN (s : Computation α) [_h : Terminates s] (n) : length (thinkN s n) = length s + n := (results_thinkN n (results_of_terminates _)).length theorem eq_thinkN {s : Computation α} {a n} (h : Results s a n) : s = thinkN (pure a) n := by revert s induction n with | zero => _ | succ n IH => _ <;> (intro s; apply recOn s (fun a' => _) fun s => _) <;> intro a h · rw [← eq_of_pure_mem h.mem] rfl · obtain ⟨n, h⟩ := of_results_think h cases h contradiction · have := h.len_unique (results_pure _) contradiction · rw [IH (results_think_iff.1 h)] rfl theorem eq_thinkN' (s : Computation α) [_h : Terminates s] : s = thinkN (pure (get s)) (length s) := eq_thinkN (results_of_terminates _) /-- Recursor based on membership -/ def memRecOn {C : Computation α → Sort v} {a s} (M : a ∈ s) (h1 : C (pure a)) (h2 : ∀ s, C s → C (think s)) : C s := by haveI T := terminates_of_mem M rw [eq_thinkN' s, get_eq_of_mem s M] generalize length s = n induction' n with n IH; exacts [h1, h2 _ IH] /-- Recursor based on assertion of `Terminates` -/ def terminatesRecOn {C : Computation α → Sort v} (s) [Terminates s] (h1 : ∀ a, C (pure a)) (h2 : ∀ s, C s → C (think s)) : C s := memRecOn (get_mem s) (h1 _) h2 /-- Map a function on the result of a computation. -/ def map (f : α → β) : Computation α → Computation β | ⟨s, al⟩ => ⟨s.map fun o => Option.casesOn o none (some ∘ f), fun n b => by dsimp [Stream'.map, Stream'.get] induction' e : s n with a <;> intro h · contradiction · rw [al e]; exact h⟩ /-- bind over a `Sum` of `Computation` -/ def Bind.g : β ⊕ Computation β → β ⊕ (Computation α ⊕ Computation β) | Sum.inl b => Sum.inl b | Sum.inr cb' => Sum.inr <| Sum.inr cb' /-- bind over a function mapping `α` to a `Computation` -/ def Bind.f (f : α → Computation β) : Computation α ⊕ Computation β → β ⊕ (Computation α ⊕ Computation β) | Sum.inl ca => match destruct ca with | Sum.inl a => Bind.g <| destruct (f a) | Sum.inr ca' => Sum.inr <| Sum.inl ca' | Sum.inr cb => Bind.g <| destruct cb /-- Compose two computations into a monadic `bind` operation. -/ def bind (c : Computation α) (f : α → Computation β) : Computation β := corec (Bind.f f) (Sum.inl c) instance : Bind Computation := ⟨@bind⟩ theorem has_bind_eq_bind {β} (c : Computation α) (f : α → Computation β) : c >>= f = bind c f := rfl /-- Flatten a computation of computations into a single computation. -/ def join (c : Computation (Computation α)) : Computation α := c >>= id @[simp] theorem map_pure (f : α → β) (a) : map f (pure a) = pure (f a) := rfl @[simp] theorem map_think (f : α → β) : ∀ s, map f (think s) = think (map f s) | ⟨s, al⟩ => by apply Subtype.eq; dsimp [think, map]; rw [Stream'.map_cons] @[simp] theorem destruct_map (f : α → β) (s) : destruct (map f s) = lmap f (rmap (map f) (destruct s)) := by apply s.recOn <;> intro <;> simp @[simp] theorem map_id : ∀ s : Computation α, map id s = s | ⟨f, al⟩ => by apply Subtype.eq; simp only [map, comp_apply, id_eq] have e : @Option.rec α (fun _ => Option α) none some = id := by ext ⟨⟩ <;> rfl have h : ((fun x : Option α => x) = id) := rfl simp [e, h, Stream'.map_id] theorem map_comp (f : α → β) (g : β → γ) : ∀ s : Computation α, map (g ∘ f) s = map g (map f s) | ⟨s, al⟩ => by apply Subtype.eq; dsimp [map] apply congr_arg fun f : _ → Option γ => Stream'.map f s ext ⟨⟩ <;> rfl @[simp] theorem ret_bind (a) (f : α → Computation β) : bind (pure a) f = f a := by apply eq_of_bisim fun c₁ c₂ => c₁ = bind (pure a) f ∧ c₂ = f a ∨ c₁ = corec (Bind.f f) (Sum.inr c₂) · intro c₁ c₂ h match c₁, c₂, h with | _, _, Or.inl ⟨rfl, rfl⟩ => simp only [BisimO, bind, Bind.f, corec_eq, rmap, destruct_pure] rcases destruct (f a) with b | cb <;> simp [Bind.g] | _, c, Or.inr rfl => simp only [BisimO, Bind.f, corec_eq, rmap] rcases destruct c with b | cb <;> simp [Bind.g] · simp @[simp] theorem think_bind (c) (f : α → Computation β) : bind (think c) f = think (bind c f) := destruct_eq_think <| by simp [bind, Bind.f] @[simp] theorem bind_pure (f : α → β) (s) : bind s (pure ∘ f) = map f s := by apply eq_of_bisim fun c₁ c₂ => c₁ = c₂ ∨ ∃ s, c₁ = bind s (pure ∘ f) ∧ c₂ = map f s · intro c₁ c₂ h match c₁, c₂, h with | _, c₂, Or.inl (Eq.refl _) => rcases destruct c₂ with b | cb <;> simp | _, _, Or.inr ⟨s, rfl, rfl⟩ => apply recOn s <;> intro s · simp · simpa using Or.inr ⟨s, rfl, rfl⟩ · exact Or.inr ⟨s, rfl, rfl⟩ @[simp] theorem bind_pure' (s : Computation α) : bind s pure = s := by simpa using bind_pure id s @[simp] theorem bind_assoc (s : Computation α) (f : α → Computation β) (g : β → Computation γ) : bind (bind s f) g = bind s fun x : α => bind (f x) g := by apply eq_of_bisim fun c₁ c₂ => c₁ = c₂ ∨ ∃ s, c₁ = bind (bind s f) g ∧ c₂ = bind s fun x : α => bind (f x) g · intro c₁ c₂ h match c₁, c₂, h with | _, c₂, Or.inl (Eq.refl _) => rcases destruct c₂ with b | cb <;> simp | _, _, Or.inr ⟨s, rfl, rfl⟩ => apply recOn s <;> intro s · simp only [BisimO, ret_bind]; generalize f s = fs apply recOn fs <;> intro t <;> simp · rcases destruct (g t) with b | cb <;> simp · simpa [BisimO] using Or.inr ⟨s, rfl, rfl⟩ · exact Or.inr ⟨s, rfl, rfl⟩ theorem results_bind {s : Computation α} {f : α → Computation β} {a b m n} (h1 : Results s a m) (h2 : Results (f a) b n) : Results (bind s f) b (n + m) := by have := h1.mem; revert m apply memRecOn this _ fun s IH => _ · intro _ h1 rw [ret_bind] rw [h1.len_unique (results_pure _)] exact h2 · intro _ h3 _ h1 rw [think_bind] obtain ⟨m', h⟩ := of_results_think h1 obtain ⟨h1, e⟩ := h rw [e] exact results_think (h3 h1) theorem mem_bind {s : Computation α} {f : α → Computation β} {a b} (h1 : a ∈ s) (h2 : b ∈ f a) : b ∈ bind s f := let ⟨_, h1⟩ := exists_results_of_mem h1 let ⟨_, h2⟩ := exists_results_of_mem h2 (results_bind h1 h2).mem instance terminates_bind (s : Computation α) (f : α → Computation β) [Terminates s] [Terminates (f (get s))] : Terminates (bind s f) := terminates_of_mem (mem_bind (get_mem s) (get_mem (f (get s)))) @[simp] theorem get_bind (s : Computation α) (f : α → Computation β) [Terminates s] [Terminates (f (get s))] : get (bind s f) = get (f (get s)) := get_eq_of_mem _ (mem_bind (get_mem s) (get_mem (f (get s)))) @[simp] theorem length_bind (s : Computation α) (f : α → Computation β) [_T1 : Terminates s] [_T2 : Terminates (f (get s))] : length (bind s f) = length (f (get s)) + length s := (results_of_terminates _).len_unique <| results_bind (results_of_terminates _) (results_of_terminates _) theorem of_results_bind {s : Computation α} {f : α → Computation β} {b k} : Results (bind s f) b k → ∃ a m n, Results s a m ∧ Results (f a) b n ∧ k = n + m := by induction k generalizing s with | zero => _ | succ n IH => _ <;> apply recOn s (fun a => _) fun s' => _ <;> intro e h · simp only [ret_bind] at h exact ⟨e, _, _, results_pure _, h, rfl⟩ · have := congr_arg head (eq_thinkN h) contradiction · simp only [ret_bind] at h exact ⟨e, _, n + 1, results_pure _, h, rfl⟩ · simp only [think_bind, results_think_iff] at h let ⟨a, m, n', h1, h2, e'⟩ := IH h rw [e'] exact ⟨a, m.succ, n', results_think h1, h2, rfl⟩ theorem exists_of_mem_bind {s : Computation α} {f : α → Computation β} {b} (h : b ∈ bind s f) : ∃ a ∈ s, b ∈ f a := let ⟨_, h⟩ := exists_results_of_mem h let ⟨a, _, _, h1, h2, _⟩ := of_results_bind h ⟨a, h1.mem, h2.mem⟩ theorem bind_promises {s : Computation α} {f : α → Computation β} {a b} (h1 : s ~> a) (h2 : f a ~> b) : bind s f ~> b := fun b' bB => by rcases exists_of_mem_bind bB with ⟨a', a's, ba'⟩ rw [← h1 a's] at ba'; exact h2 ba' instance monad : Monad Computation where map := @map pure := @pure bind := @bind instance : LawfulMonad Computation := LawfulMonad.mk' (id_map := @map_id) (bind_pure_comp := @bind_pure) (pure_bind := @ret_bind) (bind_assoc := @bind_assoc) theorem has_map_eq_map {β} (f : α → β) (c : Computation α) : f <$> c = map f c := rfl @[simp] theorem pure_def (a) : (return a : Computation α) = pure a := rfl @[simp] theorem map_pure' {α β} : ∀ (f : α → β) (a), f <$> pure a = pure (f a) := map_pure @[simp] theorem map_think' {α β} : ∀ (f : α → β) (s), f <$> think s = think (f <$> s) := map_think theorem mem_map (f : α → β) {a} {s : Computation α} (m : a ∈ s) : f a ∈ map f s := by rw [← bind_pure]; apply mem_bind m; apply ret_mem theorem exists_of_mem_map {f : α → β} {b : β} {s : Computation α} (h : b ∈ map f s) : ∃ a, a ∈ s ∧ f a = b := by rw [← bind_pure] at h let ⟨a, as, fb⟩ := exists_of_mem_bind h exact ⟨a, as, mem_unique (ret_mem _) fb⟩ instance terminates_map (f : α → β) (s : Computation α) [Terminates s] : Terminates (map f s) := by rw [← bind_pure]; exact terminates_of_mem (mem_bind (get_mem s) (get_mem (α := β) (f (get s)))) theorem terminates_map_iff (f : α → β) (s : Computation α) : Terminates (map f s) ↔ Terminates s := ⟨fun ⟨⟨_, h⟩⟩ => let ⟨_, h1, _⟩ := exists_of_mem_map h ⟨⟨_, h1⟩⟩, @Computation.terminates_map _ _ _ _⟩ -- Parallel computation /-- `c₁ <|> c₂` calculates `c₁` and `c₂` simultaneously, returning the first one that gives a result. -/ def orElse (c₁ : Computation α) (c₂ : Unit → Computation α) : Computation α := @Computation.corec α (Computation α × Computation α) (fun ⟨c₁, c₂⟩ => match destruct c₁ with | Sum.inl a => Sum.inl a | Sum.inr c₁' => match destruct c₂ with | Sum.inl a => Sum.inl a | Sum.inr c₂' => Sum.inr (c₁', c₂')) (c₁, c₂ ()) instance instAlternativeComputation : Alternative Computation := { Computation.monad with orElse := @orElse failure := @empty } @[simp] theorem ret_orElse (a : α) (c₂ : Computation α) : (pure a <|> c₂) = pure a := destruct_eq_pure <| by unfold_projs simp [orElse] @[simp] theorem orElse_pure (c₁ : Computation α) (a : α) : (think c₁ <|> pure a) = pure a := destruct_eq_pure <| by unfold_projs simp [orElse] @[simp] theorem orElse_think (c₁ c₂ : Computation α) : (think c₁ <|> think c₂) = think (c₁ <|> c₂) := destruct_eq_think <| by unfold_projs simp [orElse] @[simp] theorem empty_orElse (c) : (empty α <|> c) = c := by apply eq_of_bisim (fun c₁ c₂ => (empty α <|> c₂) = c₁) _ rfl intro s' s h; rw [← h] apply recOn s <;> intro s <;> rw [think_empty] <;> simp rw [← think_empty] @[simp] theorem orElse_empty (c : Computation α) : (c <|> empty α) = c := by apply eq_of_bisim (fun c₁ c₂ => (c₂ <|> empty α) = c₁) _ rfl intro s' s h; rw [← h] apply recOn s <;> intro s <;> rw [think_empty] <;> simp rw [← think_empty] /-- `c₁ ~ c₂` asserts that `c₁` and `c₂` either both terminate with the same result, or both loop forever. -/ def Equiv (c₁ c₂ : Computation α) : Prop := ∀ a, a ∈ c₁ ↔ a ∈ c₂ /-- equivalence relation for computations -/ scoped infixl:50 " ~ " => Equiv @[refl] theorem Equiv.refl (s : Computation α) : s ~ s := fun _ => Iff.rfl
@[symm] theorem Equiv.symm {s t : Computation α} : s ~ t → t ~ s := fun h a => (h a).symm @[trans] theorem Equiv.trans {s t u : Computation α} : s ~ t → t ~ u → s ~ u := fun h1 h2 a => (h1 a).trans (h2 a) theorem Equiv.equivalence : Equivalence (@Equiv α) := ⟨@Equiv.refl _, @Equiv.symm _, @Equiv.trans _⟩ theorem equiv_of_mem {s t : Computation α} {a} (h1 : a ∈ s) (h2 : a ∈ t) : s ~ t := fun a' => ⟨fun ma => by rw [mem_unique ma h1]; exact h2, fun ma => by rw [mem_unique ma h2]; exact h1⟩
Mathlib/Data/Seq/Computation.lean
817
829
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Aaron Anderson, Yakov Pechersky -/ import Mathlib.Data.Fintype.Card import Mathlib.Algebra.Group.Commute.Basic import Mathlib.Algebra.Group.End import Mathlib.Data.Finset.NoncommProd /-! # support of a permutation ## Main definitions In the following, `f g : Equiv.Perm α`. * `Equiv.Perm.Disjoint`: two permutations `f` and `g` are `Disjoint` if every element is fixed either by `f`, or by `g`. Equivalently, `f` and `g` are `Disjoint` iff their `support` are disjoint. * `Equiv.Perm.IsSwap`: `f = swap x y` for `x ≠ y`. * `Equiv.Perm.support`: the elements `x : α` that are not fixed by `f`. Assume `α` is a Fintype: * `Equiv.Perm.fixed_point_card_lt_of_ne_one f` says that `f` has strictly less than `Fintype.card α - 1` fixed points, unless `f = 1`. (Equivalently, `f.support` has at least 2 elements.) -/ open Equiv Finset Function namespace Equiv.Perm variable {α : Type*} section Disjoint /-- Two permutations `f` and `g` are `Disjoint` if their supports are disjoint, i.e., every element is fixed either by `f`, or by `g`. -/ def Disjoint (f g : Perm α) := ∀ x, f x = x ∨ g x = x variable {f g h : Perm α} @[symm] theorem Disjoint.symm : Disjoint f g → Disjoint g f := by simp only [Disjoint, or_comm, imp_self] theorem Disjoint.symmetric : Symmetric (@Disjoint α) := fun _ _ => Disjoint.symm instance : IsSymm (Perm α) Disjoint := ⟨Disjoint.symmetric⟩ theorem disjoint_comm : Disjoint f g ↔ Disjoint g f := ⟨Disjoint.symm, Disjoint.symm⟩ theorem Disjoint.commute (h : Disjoint f g) : Commute f g := Equiv.ext fun x => (h x).elim (fun hf => (h (g x)).elim (fun hg => by simp [mul_apply, hf, hg]) fun hg => by simp [mul_apply, hf, g.injective hg]) fun hg => (h (f x)).elim (fun hf => by simp [mul_apply, f.injective hf, hg]) fun hf => by simp [mul_apply, hf, hg] @[simp] theorem disjoint_one_left (f : Perm α) : Disjoint 1 f := fun _ => Or.inl rfl @[simp] theorem disjoint_one_right (f : Perm α) : Disjoint f 1 := fun _ => Or.inr rfl theorem disjoint_iff_eq_or_eq : Disjoint f g ↔ ∀ x : α, f x = x ∨ g x = x := Iff.rfl @[simp] theorem disjoint_refl_iff : Disjoint f f ↔ f = 1 := by refine ⟨fun h => ?_, fun h => h.symm ▸ disjoint_one_left 1⟩ ext x rcases h x with hx | hx <;> simp [hx] theorem Disjoint.inv_left (h : Disjoint f g) : Disjoint f⁻¹ g := by intro x rw [inv_eq_iff_eq, eq_comm] exact h x theorem Disjoint.inv_right (h : Disjoint f g) : Disjoint f g⁻¹ := h.symm.inv_left.symm @[simp] theorem disjoint_inv_left_iff : Disjoint f⁻¹ g ↔ Disjoint f g := by refine ⟨fun h => ?_, Disjoint.inv_left⟩ convert h.inv_left @[simp] theorem disjoint_inv_right_iff : Disjoint f g⁻¹ ↔ Disjoint f g := by rw [disjoint_comm, disjoint_inv_left_iff, disjoint_comm] theorem Disjoint.mul_left (H1 : Disjoint f h) (H2 : Disjoint g h) : Disjoint (f * g) h := fun x => by cases H1 x <;> cases H2 x <;> simp [*] theorem Disjoint.mul_right (H1 : Disjoint f g) (H2 : Disjoint f h) : Disjoint f (g * h) := by rw [disjoint_comm] exact H1.symm.mul_left H2.symm -- Porting note (https://github.com/leanprover-community/mathlib4/issues/11215): TODO: make it `@[simp]` theorem disjoint_conj (h : Perm α) : Disjoint (h * f * h⁻¹) (h * g * h⁻¹) ↔ Disjoint f g := (h⁻¹).forall_congr fun {_} ↦ by simp only [mul_apply, eq_inv_iff_eq] theorem Disjoint.conj (H : Disjoint f g) (h : Perm α) : Disjoint (h * f * h⁻¹) (h * g * h⁻¹) := (disjoint_conj h).2 H theorem disjoint_prod_right (l : List (Perm α)) (h : ∀ g ∈ l, Disjoint f g) : Disjoint f l.prod := by induction' l with g l ih · exact disjoint_one_right _ · rw [List.prod_cons] exact (h _ List.mem_cons_self).mul_right (ih fun g hg => h g (List.mem_cons_of_mem _ hg)) theorem disjoint_noncommProd_right {ι : Type*} {k : ι → Perm α} {s : Finset ι} (hs : Set.Pairwise s fun i j ↦ Commute (k i) (k j)) (hg : ∀ i ∈ s, g.Disjoint (k i)) : Disjoint g (s.noncommProd k (hs)) := noncommProd_induction s k hs g.Disjoint (fun _ _ ↦ Disjoint.mul_right) (disjoint_one_right g) hg open scoped List in theorem disjoint_prod_perm {l₁ l₂ : List (Perm α)} (hl : l₁.Pairwise Disjoint) (hp : l₁ ~ l₂) : l₁.prod = l₂.prod := hp.prod_eq' <| hl.imp Disjoint.commute theorem nodup_of_pairwise_disjoint {l : List (Perm α)} (h1 : (1 : Perm α) ∉ l) (h2 : l.Pairwise Disjoint) : l.Nodup := by refine List.Pairwise.imp_of_mem ?_ h2 intro τ σ h_mem _ h_disjoint _ subst τ suffices (σ : Perm α) = 1 by rw [this] at h_mem exact h1 h_mem exact ext fun a => or_self_iff.mp (h_disjoint a) theorem pow_apply_eq_self_of_apply_eq_self {x : α} (hfx : f x = x) : ∀ n : ℕ, (f ^ n) x = x | 0 => rfl | n + 1 => by rw [pow_succ, mul_apply, hfx, pow_apply_eq_self_of_apply_eq_self hfx n] theorem zpow_apply_eq_self_of_apply_eq_self {x : α} (hfx : f x = x) : ∀ n : ℤ, (f ^ n) x = x | (n : ℕ) => pow_apply_eq_self_of_apply_eq_self hfx n | Int.negSucc n => by rw [zpow_negSucc, inv_eq_iff_eq, pow_apply_eq_self_of_apply_eq_self hfx] theorem pow_apply_eq_of_apply_apply_eq_self {x : α} (hffx : f (f x) = x) : ∀ n : ℕ, (f ^ n) x = x ∨ (f ^ n) x = f x | 0 => Or.inl rfl | n + 1 => (pow_apply_eq_of_apply_apply_eq_self hffx n).elim (fun h => Or.inr (by rw [pow_succ', mul_apply, h])) fun h => Or.inl (by rw [pow_succ', mul_apply, h, hffx]) theorem zpow_apply_eq_of_apply_apply_eq_self {x : α} (hffx : f (f x) = x) : ∀ i : ℤ, (f ^ i) x = x ∨ (f ^ i) x = f x | (n : ℕ) => pow_apply_eq_of_apply_apply_eq_self hffx n | Int.negSucc n => by rw [zpow_negSucc, inv_eq_iff_eq, ← f.injective.eq_iff, ← mul_apply, ← pow_succ', eq_comm, inv_eq_iff_eq, ← mul_apply, ← pow_succ, @eq_comm _ x, or_comm] exact pow_apply_eq_of_apply_apply_eq_self hffx _ theorem Disjoint.mul_apply_eq_iff {σ τ : Perm α} (hστ : Disjoint σ τ) {a : α} : (σ * τ) a = a ↔ σ a = a ∧ τ a = a := by refine ⟨fun h => ?_, fun h => by rw [mul_apply, h.2, h.1]⟩ rcases hστ a with hσ | hτ · exact ⟨hσ, σ.injective (h.trans hσ.symm)⟩ · exact ⟨(congr_arg σ hτ).symm.trans h, hτ⟩ theorem Disjoint.mul_eq_one_iff {σ τ : Perm α} (hστ : Disjoint σ τ) : σ * τ = 1 ↔ σ = 1 ∧ τ = 1 := by simp_rw [Perm.ext_iff, one_apply, hστ.mul_apply_eq_iff, forall_and] theorem Disjoint.zpow_disjoint_zpow {σ τ : Perm α} (hστ : Disjoint σ τ) (m n : ℤ) : Disjoint (σ ^ m) (τ ^ n) := fun x => Or.imp (fun h => zpow_apply_eq_self_of_apply_eq_self h m) (fun h => zpow_apply_eq_self_of_apply_eq_self h n) (hστ x) theorem Disjoint.pow_disjoint_pow {σ τ : Perm α} (hστ : Disjoint σ τ) (m n : ℕ) : Disjoint (σ ^ m) (τ ^ n) := hστ.zpow_disjoint_zpow m n end Disjoint section IsSwap variable [DecidableEq α] /-- `f.IsSwap` indicates that the permutation `f` is a transposition of two elements. -/ def IsSwap (f : Perm α) : Prop := ∃ x y, x ≠ y ∧ f = swap x y @[simp] theorem ofSubtype_swap_eq {p : α → Prop} [DecidablePred p] (x y : Subtype p) : ofSubtype (Equiv.swap x y) = Equiv.swap ↑x ↑y := Equiv.ext fun z => by by_cases hz : p z · rw [swap_apply_def, ofSubtype_apply_of_mem _ hz] split_ifs with hzx hzy · simp_rw [hzx, Subtype.coe_eta, swap_apply_left] · simp_rw [hzy, Subtype.coe_eta, swap_apply_right] · rw [swap_apply_of_ne_of_ne] <;> simp [Subtype.ext_iff, *] · rw [ofSubtype_apply_of_not_mem _ hz, swap_apply_of_ne_of_ne] · intro h apply hz rw [h] exact Subtype.prop x intro h apply hz rw [h] exact Subtype.prop y theorem IsSwap.of_subtype_isSwap {p : α → Prop} [DecidablePred p] {f : Perm (Subtype p)} (h : f.IsSwap) : (ofSubtype f).IsSwap := let ⟨⟨x, hx⟩, ⟨y, hy⟩, hxy⟩ := h ⟨x, y, by simp only [Ne, Subtype.ext_iff] at hxy exact hxy.1, by rw [hxy.2, ofSubtype_swap_eq]⟩ theorem ne_and_ne_of_swap_mul_apply_ne_self {f : Perm α} {x y : α} (hy : (swap x (f x) * f) y ≠ y) : f y ≠ y ∧ y ≠ x := by simp only [swap_apply_def, mul_apply, f.injective.eq_iff] at * by_cases h : f y = x · constructor <;> intro <;> simp_all only [if_true, eq_self_iff_true, not_true, Ne] · split_ifs at hy with h <;> try { simp [*] at * } end IsSwap section support section Set variable (p q : Perm α) theorem set_support_inv_eq : { x | p⁻¹ x ≠ x } = { x | p x ≠ x } := by ext x simp only [Set.mem_setOf_eq, Ne] rw [inv_def, symm_apply_eq, eq_comm] theorem set_support_apply_mem {p : Perm α} {a : α} : p a ∈ { x | p x ≠ x } ↔ a ∈ { x | p x ≠ x } := by simp theorem set_support_zpow_subset (n : ℤ) : { x | (p ^ n) x ≠ x } ⊆ { x | p x ≠ x } := by intro x simp only [Set.mem_setOf_eq, Ne] intro hx H simp [zpow_apply_eq_self_of_apply_eq_self H] at hx theorem set_support_mul_subset : { x | (p * q) x ≠ x } ⊆ { x | p x ≠ x } ∪ { x | q x ≠ x } := by intro x simp only [Perm.coe_mul, Function.comp_apply, Ne, Set.mem_union, Set.mem_setOf_eq] by_cases hq : q x = x <;> simp [hq] end Set @[simp] theorem apply_pow_apply_eq_iff (f : Perm α) (n : ℕ) {x : α} : f ((f ^ n) x) = (f ^ n) x ↔ f x = x := by rw [← mul_apply, Commute.self_pow f, mul_apply, apply_eq_iff_eq] @[simp] theorem apply_zpow_apply_eq_iff (f : Perm α) (n : ℤ) {x : α} : f ((f ^ n) x) = (f ^ n) x ↔ f x = x := by rw [← mul_apply, Commute.self_zpow f, mul_apply, apply_eq_iff_eq] variable [DecidableEq α] [Fintype α] {f g : Perm α} /-- The `Finset` of nonfixed points of a permutation. -/ def support (f : Perm α) : Finset α := {x | f x ≠ x} @[simp] theorem mem_support {x : α} : x ∈ f.support ↔ f x ≠ x := by rw [support, mem_filter, and_iff_right (mem_univ x)] theorem not_mem_support {x : α} : x ∉ f.support ↔ f x = x := by simp theorem coe_support_eq_set_support (f : Perm α) : (f.support : Set α) = { x | f x ≠ x } := by ext simp @[simp] theorem support_eq_empty_iff {σ : Perm α} : σ.support = ∅ ↔ σ = 1 := by simp_rw [Finset.ext_iff, mem_support, Finset.not_mem_empty, iff_false, not_not, Equiv.Perm.ext_iff, one_apply] @[simp] theorem support_one : (1 : Perm α).support = ∅ := by rw [support_eq_empty_iff] @[simp] theorem support_refl : support (Equiv.refl α) = ∅ := support_one theorem support_congr (h : f.support ⊆ g.support) (h' : ∀ x ∈ g.support, f x = g x) : f = g := by ext x by_cases hx : x ∈ g.support · exact h' x hx · rw [not_mem_support.mp hx, ← not_mem_support] exact fun H => hx (h H) /-- If g and c commute, then g stabilizes the support of c -/ theorem mem_support_iff_of_commute {g c : Perm α} (hgc : Commute g c) (x : α) : x ∈ c.support ↔ g x ∈ c.support := by simp only [mem_support, not_iff_not, ← mul_apply] rw [← hgc, mul_apply, Equiv.apply_eq_iff_eq] theorem support_mul_le (f g : Perm α) : (f * g).support ≤ f.support ⊔ g.support := fun x => by simp only [sup_eq_union] rw [mem_union, mem_support, mem_support, mem_support, mul_apply, ← not_and_or, not_imp_not] rintro ⟨hf, hg⟩ rw [hg, hf] theorem exists_mem_support_of_mem_support_prod {l : List (Perm α)} {x : α} (hx : x ∈ l.prod.support) : ∃ f : Perm α, f ∈ l ∧ x ∈ f.support := by contrapose! hx simp_rw [mem_support, not_not] at hx ⊢ induction' l with f l ih · rfl · rw [List.prod_cons, mul_apply, ih, hx] · simp only [List.find?, List.mem_cons, true_or] intros f' hf' refine hx f' ?_ simp only [List.find?, List.mem_cons] exact Or.inr hf' theorem support_pow_le (σ : Perm α) (n : ℕ) : (σ ^ n).support ≤ σ.support := fun _ h1 => mem_support.mpr fun h2 => mem_support.mp h1 (pow_apply_eq_self_of_apply_eq_self h2 n) @[simp] theorem support_inv (σ : Perm α) : support σ⁻¹ = σ.support := by simp_rw [Finset.ext_iff, mem_support, not_iff_not, inv_eq_iff_eq.trans eq_comm, imp_true_iff] theorem apply_mem_support {x : α} : f x ∈ f.support ↔ x ∈ f.support := by rw [mem_support, mem_support, Ne, Ne, apply_eq_iff_eq] /-- The support of a permutation is invariant -/ theorem isInvariant_of_support_le {c : Perm α} {s : Finset α} (hcs : c.support ≤ s) (x : α) : x ∈ s ↔ c x ∈ s := by by_cases hx' : x ∈ c.support · simp only [hcs hx', true_iff, hcs (apply_mem_support.mpr hx')] · rw [not_mem_support.mp hx'] /-- A permutation c is the extension of a restriction of g to s iff its support is contained in s and its restriction is that of g -/ lemma ofSubtype_eq_iff {g c : Equiv.Perm α} {s : Finset α} (hg : ∀ x, x ∈ s ↔ g x ∈ s) : ofSubtype (g.subtypePerm hg) = c ↔ c.support ≤ s ∧ ∀ (hc' : ∀ x, x ∈ s ↔ c x ∈ s), c.subtypePerm hc' = g.subtypePerm hg := by simp only [Equiv.ext_iff, subtypePerm_apply, Subtype.mk.injEq, Subtype.forall] constructor · intro h constructor · intro a ha by_contra ha' rw [mem_support, ← h a, ofSubtype_apply_of_not_mem (p := (· ∈ s)) _ ha'] at ha exact ha rfl · intro _ a ha rw [← h a, ofSubtype_apply_of_mem (p := (· ∈ s)) _ ha, subtypePerm_apply] · rintro ⟨hc, h⟩ a specialize h (isInvariant_of_support_le hc) by_cases ha : a ∈ s · rw [h a ha, ofSubtype_apply_of_mem (p := (· ∈ s)) _ ha, subtypePerm_apply] · rw [ofSubtype_apply_of_not_mem (p := (· ∈ s)) _ ha, eq_comm, ← not_mem_support] exact Finset.not_mem_mono hc ha theorem support_ofSubtype {p : α → Prop} [DecidablePred p] (u : Perm (Subtype p)) : (ofSubtype u).support = u.support.map (Function.Embedding.subtype p) := by ext x simp only [mem_support, ne_eq, Finset.mem_map, Function.Embedding.coe_subtype, Subtype.exists, exists_and_right, exists_eq_right, not_iff_comm, not_exists, not_not] by_cases hx : p x · simp only [forall_prop_of_true hx, ofSubtype_apply_of_mem u hx, ← Subtype.coe_inj] · simp only [forall_prop_of_false hx, true_iff, ofSubtype_apply_of_not_mem u hx] theorem mem_support_of_mem_noncommProd_support {α β : Type*} [DecidableEq β] [Fintype β] {s : Finset α} {f : α → Perm β} {comm : (s : Set α).Pairwise (Commute on f)} {x : β} (hx : x ∈ (s.noncommProd f comm).support) : ∃ a ∈ s, x ∈ (f a).support := by contrapose! hx classical revert hx comm s apply Finset.induction · simp · intro a s ha ih comm hs rw [Finset.noncommProd_insert_of_not_mem s a f comm ha] apply mt (Finset.mem_of_subset (support_mul_le _ _)) rw [Finset.sup_eq_union, Finset.not_mem_union] exact ⟨hs a (s.mem_insert_self a), ih (fun a ha ↦ hs a (Finset.mem_insert_of_mem ha))⟩ theorem pow_apply_mem_support {n : ℕ} {x : α} : (f ^ n) x ∈ f.support ↔ x ∈ f.support := by
simp only [mem_support, ne_eq, apply_pow_apply_eq_iff] theorem zpow_apply_mem_support {n : ℤ} {x : α} : (f ^ n) x ∈ f.support ↔ x ∈ f.support := by
Mathlib/GroupTheory/Perm/Support.lean
396
398
/- Copyright (c) 2024 Riccardo Brasca. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Riccardo Brasca, Pietro Monticone -/ import Mathlib.NumberTheory.Cyclotomic.Embeddings import Mathlib.NumberTheory.Cyclotomic.Rat import Mathlib.NumberTheory.NumberField.Units.DirichletTheorem import Mathlib.RingTheory.Fintype /-! # Third Cyclotomic Field We gather various results about the third cyclotomic field. The following notations are used in this file: `K` is a number field such that `IsCyclotomicExtension {3} ℚ K`, `ζ` is any primitive `3`-rd root of unity in `K`, `η` is the element in the units of the ring of integers corresponding to `ζ` and `λ = η - 1`. ## Main results * `IsCyclotomicExtension.Rat.Three.Units.mem`: Given a unit `u : (𝓞 K)ˣ`, we have that `u ∈ {1, -1, η, -η, η^2, -η^2}`. * `IsCyclotomicExtension.Rat.Three.eq_one_or_neg_one_of_unit_of_congruent`: Given a unit `u : (𝓞 K)ˣ`, if `u` is congruent to an integer modulo `3`, then `u = 1` or `u = -1`. This is a special case of the so-called *Kummer's lemma* (see for example [washington_cyclotomic], Theorem 5.36 -/ open NumberField Units InfinitePlace nonZeroDivisors Polynomial namespace IsCyclotomicExtension.Rat.Three variable {K : Type*} [Field K] variable {ζ : K} (hζ : IsPrimitiveRoot ζ ↑(3 : ℕ+)) (u : (𝓞 K)ˣ) local notation3 "η" => (IsPrimitiveRoot.isUnit (hζ.toInteger_isPrimitiveRoot) (by decide)).unit local notation3 "λ" => hζ.toInteger - 1 lemma coe_eta : (η : 𝓞 K) = hζ.toInteger := rfl lemma _root_.IsPrimitiveRoot.toInteger_cube_eq_one : hζ.toInteger ^ 3 = 1 := hζ.toInteger_isPrimitiveRoot.pow_eq_one /-- Let `u` be a unit in `(𝓞 K)ˣ`, then `u ∈ [1, -1, η, -η, η^2, -η^2]`. -/ -- Here `List` is more convenient than `Finset`, even if further from the informal statement. -- For example, `fin_cases` below does not work with a `Finset`. theorem Units.mem [NumberField K] [IsCyclotomicExtension {3} ℚ K] : u ∈ [1, -1, η, -η, η ^ 2, -η ^ 2] := by have hrank : rank K = 0 := by dsimp only [rank] rw [card_eq_nrRealPlaces_add_nrComplexPlaces, nrRealPlaces_eq_zero (n := 3) K (by decide), zero_add, nrComplexPlaces_eq_totient_div_two (n := 3)] rfl obtain ⟨⟨x, e⟩, hxu, -⟩ := exist_unique_eq_mul_prod _ u replace hxu : u = x := by rw [← mul_one x.1, hxu] apply congr_arg rw [← Finset.prod_empty] congr rw [Finset.univ_eq_empty_iff, hrank] infer_instance obtain ⟨n, hnpos, hn⟩ := isOfFinOrder_iff_pow_eq_one.1 <| (CommGroup.mem_torsion _ _).1 x.2 replace hn : (↑u : K) ^ ((⟨n, hnpos⟩ : ℕ+) : ℕ) = 1 := by rw [← map_pow] convert map_one (algebraMap (𝓞 K) K) rw_mod_cast [hxu, hn] simp obtain ⟨r, hr3, hru⟩ := hζ.exists_pow_or_neg_mul_pow_of_isOfFinOrder (by decide) (isOfFinOrder_iff_pow_eq_one.2 ⟨n, hnpos, hn⟩) replace hr : r ∈ Finset.Ico 0 3 := Finset.mem_Ico.2 ⟨by simp, hr3⟩ replace hru : ↑u = η ^ r ∨ ↑u = -η ^ r := by rcases hru with h | h · left; ext; exact h · right; ext; exact h fin_cases hr <;> rcases hru with h | h <;> simp [h] /-- We have that `λ ^ 2 = -3 * η`. -/ private lemma lambda_sq : λ ^ 2 = -3 * η := by ext calc (λ ^ 2 : K) = η ^ 2 + η + 1 - 3 * η := by simp only [RingOfIntegers.map_mk, IsUnit.unit_spec]; ring _ = 0 - 3 * η := by simpa using hζ.isRoot_cyclotomic (by decide) _ = -3 * η := by ring /-- We have that `η ^ 2 = -η - 1`. -/ lemma eta_sq : (η ^ 2 : 𝓞 K) = - η - 1 := by rw [← neg_add', ← add_eq_zero_iff_eq_neg, ← add_assoc] ext; simpa using hζ.isRoot_cyclotomic (by decide) /-- If a unit `u` is congruent to an integer modulo `λ ^ 2`, then `u = 1` or `u = -1`.
This is a special case of the so-called *Kummer's lemma*. -/ theorem eq_one_or_neg_one_of_unit_of_congruent [NumberField K] [IsCyclotomicExtension {3} ℚ K] (hcong : ∃ n : ℤ, λ ^ 2 ∣ (u - n : 𝓞 K)) : u = 1 ∨ u = -1 := by replace hcong : ∃ n : ℤ, (3 : 𝓞 K) ∣ (↑u - n : 𝓞 K) := by obtain ⟨n, x, hx⟩ := hcong exact ⟨n, -η * x, by rw [← mul_assoc, mul_neg, ← neg_mul, ← lambda_sq, hx]⟩ have hζ := IsCyclotomicExtension.zeta_spec 3 ℚ K have := Units.mem hζ u fin_cases this · left; rfl · right; rfl all_goals exfalso · exact hζ.not_exists_int_prime_dvd_sub_of_prime_ne_two' (by decide) hcong · apply hζ.not_exists_int_prime_dvd_sub_of_prime_ne_two' (by decide) obtain ⟨n, x, hx⟩ := hcong rw [sub_eq_iff_eq_add] at hx refine ⟨-n, -x, sub_eq_iff_eq_add.2 ?_⟩ simp only [PNat.val_ofNat, Nat.cast_ofNat, mul_neg, Int.cast_neg, ← neg_add, ← hx, Units.val_neg, IsUnit.unit_spec, RingOfIntegers.neg_mk, neg_neg] · exact (hζ.pow_of_coprime 2 (by decide)).not_exists_int_prime_dvd_sub_of_prime_ne_two' (by decide) hcong · apply (hζ.pow_of_coprime 2 (by decide)).not_exists_int_prime_dvd_sub_of_prime_ne_two' (by decide) obtain ⟨n, x, hx⟩ := hcong refine ⟨-n, -x, sub_eq_iff_eq_add.2 ?_⟩ have : (hζ.pow_of_coprime 2 (by decide)).toInteger = hζ.toInteger ^ 2 := by ext; simp
Mathlib/NumberTheory/Cyclotomic/Three.lean
92
118
/- Copyright (c) 2024 Thomas Browning, Junyan Xu. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Thomas Browning, Junyan Xu -/ import Mathlib.Algebra.Group.Subgroup.Ker import Mathlib.GroupTheory.GroupAction.Basic import Mathlib.GroupTheory.GroupAction.FixedPoints import Mathlib.GroupTheory.Perm.Support import Mathlib.Data.Set.Finite.Basic /-! # Subgroups generated by transpositions This file studies subgroups generated by transpositions. ## Main results - `swap_mem_closure_isSwap` : If a subgroup is generated by transpositions, then a transposition `swap x y` lies in the subgroup if and only if `x` lies in the same orbit as `y`. - `mem_closure_isSwap` : If a subgroup is generated by transpositions, then a permutation `f` lies in the subgroup if and only if `f` has finite support and `f x` always lies in the same orbit as `x`. -/ open Equiv List MulAction Pointwise Set Subgroup variable {G α : Type*} [Group G] [MulAction G α] /-- If the support of each element in a generating set of a permutation group is finite, then the support of every element in the group is finite. -/ theorem finite_compl_fixedBy_closure_iff {S : Set G} : (∀ g ∈ closure S, (fixedBy α g)ᶜ.Finite) ↔ ∀ g ∈ S, (fixedBy α g)ᶜ.Finite := ⟨fun h g hg ↦ h g (subset_closure hg), fun h g hg ↦ by refine closure_induction h (by simp) (fun g g' _ _ hg hg' ↦ (hg.union hg').subset ?_) (by simp) hg simp_rw [← compl_inter, compl_subset_compl, fixedBy_mul]⟩ /-- Given a symmetric generating set of a permutation group, if T is a nonempty proper subset of an orbit, then there exists a generator that sends some element of T into the complement of T. -/ theorem exists_smul_not_mem_of_subset_orbit_closure (S : Set G) (T : Set α) {a : α} (hS : ∀ g ∈ S, g⁻¹ ∈ S) (subset : T ⊆ orbit (closure S) a) (not_mem : a ∉ T) (nonempty : T.Nonempty) : ∃ σ ∈ S, ∃ a ∈ T, σ • a ∉ T := by have key0 : ¬ closure S ≤ stabilizer G T := by have ⟨b, hb⟩ := nonempty obtain ⟨σ, rfl⟩ := subset hb contrapose! not_mem with h exact smul_mem_smul_set_iff.mp ((h σ.2).symm ▸ hb) contrapose! key0 refine (closure_le _).mpr fun σ hσ ↦ ?_ simp_rw [SetLike.mem_coe, mem_stabilizer_iff, Set.ext_iff, mem_smul_set_iff_inv_smul_mem] exact fun a ↦ ⟨fun h ↦ smul_inv_smul σ a ▸ key0 σ hσ (σ⁻¹ • a) h, key0 σ⁻¹ (hS σ hσ) a⟩ variable [DecidableEq α] theorem finite_compl_fixedBy_swap {x y : α} : (fixedBy α (swap x y))ᶜ.Finite := Set.Finite.subset (s := {x, y}) (by simp) (compl_subset_comm.mp fun z h ↦ by apply swap_apply_of_ne_of_ne <;> rintro rfl <;> simp at h) theorem Equiv.Perm.IsSwap.finite_compl_fixedBy {σ : Perm α} (h : σ.IsSwap) : (fixedBy α σ)ᶜ.Finite := by obtain ⟨x, y, -, rfl⟩ := h exact finite_compl_fixedBy_swap -- this result cannot be moved to Perm/Basic since Perm/Basic is not allowed to import Submonoid theorem SubmonoidClass.swap_mem_trans {a b c : α} {C} [SetLike C (Perm α)] [SubmonoidClass C (Perm α)] (M : C) (hab : swap a b ∈ M) (hbc : swap b c ∈ M) : swap a c ∈ M := by obtain rfl | hab' := eq_or_ne a b · exact hbc obtain rfl | hac := eq_or_ne a c · exact swap_self a ▸ one_mem M rw [swap_comm, ← swap_mul_swap_mul_swap hab' hac] exact mul_mem (mul_mem hbc hab) hbc /-- If a subgroup is generated by transpositions, then a transposition `swap x y` lies in the subgroup if and only if `x` lies in the same orbit as `y`. -/ theorem swap_mem_closure_isSwap {S : Set (Perm α)} (hS : ∀ f ∈ S, f.IsSwap) {x y : α} : swap x y ∈ closure S ↔ x ∈ orbit (closure S) y := by refine ⟨fun h ↦ ⟨⟨swap x y, h⟩, swap_apply_right x y⟩, fun hf ↦ ?_⟩ by_contra h have := exists_smul_not_mem_of_subset_orbit_closure S {x | swap x y ∈ closure S} (fun f hf ↦ ?_) (fun z hz ↦ ?_) h ⟨y, ?_⟩ · obtain ⟨σ, hσ, a, ha, hσa⟩ := this obtain ⟨z, w, hzw, rfl⟩ := hS σ hσ have := ne_of_mem_of_not_mem ha hσa rw [Perm.smul_def, ne_comm, swap_apply_ne_self_iff, and_iff_right hzw] at this refine hσa (SubmonoidClass.swap_mem_trans (closure S) ?_ ha) obtain rfl | rfl := this <;> simpa [swap_comm] using subset_closure hσ · obtain ⟨x, y, -, rfl⟩ := hS f hf; rwa [swap_inv] · exact orbit_eq_iff.mpr hf ▸ ⟨⟨swap z y, hz⟩, swap_apply_right z y⟩ · rw [mem_setOf, swap_self]; apply one_mem /-- If a subgroup is generated by transpositions, then a permutation `f` lies in the subgroup if and only if `f` has finite support and `f x` always lies in the same orbit as `x`. -/ theorem mem_closure_isSwap {S : Set (Perm α)} (hS : ∀ f ∈ S, f.IsSwap) {f : Perm α} : f ∈ closure S ↔ (fixedBy α f)ᶜ.Finite ∧ ∀ x, f x ∈ orbit (closure S) x := by refine ⟨fun hf ↦ ⟨?_, fun x ↦ mem_orbit_iff.mpr ⟨⟨f, hf⟩, rfl⟩⟩, ?_⟩ · exact finite_compl_fixedBy_closure_iff.mpr (fun f hf ↦ (hS f hf).finite_compl_fixedBy) _ hf rintro ⟨fin, hf⟩ set supp := (fixedBy α f)ᶜ with supp_eq suffices h : (fixedBy α f)ᶜ ⊆ supp → f ∈ closure S from h supp_eq.symm.subset clear_value supp; clear supp_eq; revert f apply fin.induction_on .. · rintro f - emp; convert (closure S).one_mem; ext; by_contra h; exact emp h rintro a s - - ih f hf supp_subset refine (mul_mem_cancel_left ((swap_mem_closure_isSwap hS).2 (hf a))).1 (ih (fun b ↦ ?_) fun b hb ↦ ?_) · rw [Perm.mul_apply, swap_apply_def]; split_ifs with h1 h2 · rw [← orbit_eq_iff.mpr (hf b), h1, orbit_eq_iff.mpr (hf a)]; apply mem_orbit_self · rw [← orbit_eq_iff.mpr (hf b), h2]; apply hf · exact hf b · contrapose! hb simp_rw [not_mem_compl_iff, mem_fixedBy, Perm.smul_def, Perm.mul_apply, swap_apply_def, apply_eq_iff_eq] by_cases hb' : f b = b · rw [hb']; split_ifs with h <;> simp only [h] simp [show b = a by simpa [hb] using supp_subset hb'] /-- A permutation is a product of transpositions if and only if it has finite support. -/ theorem mem_closure_isSwap' {f : Perm α} : f ∈ closure {σ : Perm α | σ.IsSwap} ↔ (fixedBy α f)ᶜ.Finite := by refine (mem_closure_isSwap fun _ ↦ id).trans (and_iff_left fun x ↦ ⟨⟨swap x (f x), ?_⟩, swap_apply_left x (f x)⟩) by_cases h : x = f x
· rw [← h, swap_self] apply Subgroup.one_mem · exact subset_closure ⟨x, f x, h, rfl⟩
Mathlib/GroupTheory/Perm/ClosureSwap.lean
127
129
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Data.Ordmap.Invariants /-! # Verification of `Ordnode` This file uses the invariants defined in `Mathlib.Data.Ordmap.Invariants` to construct `Ordset α`, a wrapper around `Ordnode α` which includes the correctness invariant of the type. It exposes parallel operations like `insert` as functions on `Ordset` that do the same thing but bundle the correctness proofs. The advantage is that it is possible to, for example, prove that the result of `find` on `insert` will actually find the element, while `Ordnode` cannot guarantee this if the input tree did not satisfy the type invariants. ## Main definitions * `Ordnode.Valid`: The validity predicate for an `Ordnode` subtree. * `Ordset α`: A well formed set of values of type `α`. ## Implementation notes Because the `Ordnode` file was ported from Haskell, the correctness invariants of some of the functions have not been spelled out, and some theorems like `Ordnode.Valid'.balanceL_aux` show very intricate assumptions on the sizes, which may need to be revised if it turns out some operations violate these assumptions, because there is a decent amount of slop in the actual data structure invariants, so the theorem will go through with multiple choices of assumption. -/ variable {α : Type*} namespace Ordnode section Valid variable [Preorder α] /-- The validity predicate for an `Ordnode` subtree. This asserts that the `size` fields are correct, the tree is balanced, and the elements of the tree are organized according to the ordering. This version of `Valid` also puts all elements in the tree in the interval `(lo, hi)`. -/ structure Valid' (lo : WithBot α) (t : Ordnode α) (hi : WithTop α) : Prop where ord : t.Bounded lo hi sz : t.Sized bal : t.Balanced /-- The validity predicate for an `Ordnode` subtree. This asserts that the `size` fields are correct, the tree is balanced, and the elements of the tree are organized according to the ordering. -/ def Valid (t : Ordnode α) : Prop := Valid' ⊥ t ⊤ theorem Valid'.mono_left {x y : α} (xy : x ≤ y) {t : Ordnode α} {o} (h : Valid' y t o) : Valid' x t o := ⟨h.1.mono_left xy, h.2, h.3⟩ theorem Valid'.mono_right {x y : α} (xy : x ≤ y) {t : Ordnode α} {o} (h : Valid' o t x) : Valid' o t y := ⟨h.1.mono_right xy, h.2, h.3⟩ theorem Valid'.trans_left {t₁ t₂ : Ordnode α} {x : α} {o₁ o₂} (h : Bounded t₁ o₁ x) (H : Valid' x t₂ o₂) : Valid' o₁ t₂ o₂ := ⟨h.trans_left H.1, H.2, H.3⟩ theorem Valid'.trans_right {t₁ t₂ : Ordnode α} {x : α} {o₁ o₂} (H : Valid' o₁ t₁ x) (h : Bounded t₂ x o₂) : Valid' o₁ t₁ o₂ := ⟨H.1.trans_right h, H.2, H.3⟩ theorem Valid'.of_lt {t : Ordnode α} {x : α} {o₁ o₂} (H : Valid' o₁ t o₂) (h₁ : Bounded nil o₁ x) (h₂ : All (· < x) t) : Valid' o₁ t x := ⟨H.1.of_lt h₁ h₂, H.2, H.3⟩ theorem Valid'.of_gt {t : Ordnode α} {x : α} {o₁ o₂} (H : Valid' o₁ t o₂) (h₁ : Bounded nil x o₂) (h₂ : All (· > x) t) : Valid' x t o₂ := ⟨H.1.of_gt h₁ h₂, H.2, H.3⟩ theorem Valid'.valid {t o₁ o₂} (h : @Valid' α _ o₁ t o₂) : Valid t := ⟨h.1.weak, h.2, h.3⟩ theorem valid'_nil {o₁ o₂} (h : Bounded nil o₁ o₂) : Valid' o₁ (@nil α) o₂ := ⟨h, ⟨⟩, ⟨⟩⟩ theorem valid_nil : Valid (@nil α) := valid'_nil ⟨⟩ theorem Valid'.node {s l} {x : α} {r o₁ o₂} (hl : Valid' o₁ l x) (hr : Valid' x r o₂) (H : BalancedSz (size l) (size r)) (hs : s = size l + size r + 1) : Valid' o₁ (@node α s l x r) o₂ := ⟨⟨hl.1, hr.1⟩, ⟨hs, hl.2, hr.2⟩, ⟨H, hl.3, hr.3⟩⟩ theorem Valid'.dual : ∀ {t : Ordnode α} {o₁ o₂}, Valid' o₁ t o₂ → @Valid' αᵒᵈ _ o₂ (dual t) o₁ | .nil, _, _, h => valid'_nil h.1.dual | .node _ l _ r, _, _, ⟨⟨ol, Or⟩, ⟨rfl, sl, sr⟩, ⟨b, bl, br⟩⟩ => let ⟨ol', sl', bl'⟩ := Valid'.dual ⟨ol, sl, bl⟩ let ⟨or', sr', br'⟩ := Valid'.dual ⟨Or, sr, br⟩ ⟨⟨or', ol'⟩, ⟨by simp [size_dual, add_comm], sr', sl'⟩, ⟨by rw [size_dual, size_dual]; exact b.symm, br', bl'⟩⟩ theorem Valid'.dual_iff {t : Ordnode α} {o₁ o₂} : Valid' o₁ t o₂ ↔ @Valid' αᵒᵈ _ o₂ (.dual t) o₁ := ⟨Valid'.dual, fun h => by have := Valid'.dual h; rwa [dual_dual, OrderDual.Preorder.dual_dual] at this⟩ theorem Valid.dual {t : Ordnode α} : Valid t → @Valid αᵒᵈ _ (.dual t) := Valid'.dual theorem Valid.dual_iff {t : Ordnode α} : Valid t ↔ @Valid αᵒᵈ _ (.dual t) := Valid'.dual_iff theorem Valid'.left {s l x r o₁ o₂} (H : Valid' o₁ (@Ordnode.node α s l x r) o₂) : Valid' o₁ l x := ⟨H.1.1, H.2.2.1, H.3.2.1⟩ theorem Valid'.right {s l x r o₁ o₂} (H : Valid' o₁ (@Ordnode.node α s l x r) o₂) : Valid' x r o₂ := ⟨H.1.2, H.2.2.2, H.3.2.2⟩ nonrec theorem Valid.left {s l x r} (H : Valid (@node α s l x r)) : Valid l := H.left.valid nonrec theorem Valid.right {s l x r} (H : Valid (@node α s l x r)) : Valid r := H.right.valid theorem Valid.size_eq {s l x r} (H : Valid (@node α s l x r)) : size (@node α s l x r) = size l + size r + 1 := H.2.1 theorem Valid'.node' {l} {x : α} {r o₁ o₂} (hl : Valid' o₁ l x) (hr : Valid' x r o₂) (H : BalancedSz (size l) (size r)) : Valid' o₁ (@node' α l x r) o₂ := hl.node hr H rfl theorem valid'_singleton {x : α} {o₁ o₂} (h₁ : Bounded nil o₁ x) (h₂ : Bounded nil x o₂) : Valid' o₁ (singleton x : Ordnode α) o₂ := (valid'_nil h₁).node (valid'_nil h₂) (Or.inl zero_le_one) rfl theorem valid_singleton {x : α} : Valid (singleton x : Ordnode α) := valid'_singleton ⟨⟩ ⟨⟩ theorem Valid'.node3L {l} {x : α} {m} {y : α} {r o₁ o₂} (hl : Valid' o₁ l x) (hm : Valid' x m y) (hr : Valid' y r o₂) (H1 : BalancedSz (size l) (size m)) (H2 : BalancedSz (size l + size m + 1) (size r)) : Valid' o₁ (@node3L α l x m y r) o₂ := (hl.node' hm H1).node' hr H2 theorem Valid'.node3R {l} {x : α} {m} {y : α} {r o₁ o₂} (hl : Valid' o₁ l x) (hm : Valid' x m y) (hr : Valid' y r o₂) (H1 : BalancedSz (size l) (size m + size r + 1)) (H2 : BalancedSz (size m) (size r)) : Valid' o₁ (@node3R α l x m y r) o₂ := hl.node' (hm.node' hr H2) H1 theorem Valid'.node4L_lemma₁ {a b c d : ℕ} (lr₂ : 3 * (b + c + 1 + d) ≤ 16 * a + 9) (mr₂ : b + c + 1 ≤ 3 * d) (mm₁ : b ≤ 3 * c) : b < 3 * a + 1 := by omega theorem Valid'.node4L_lemma₂ {b c d : ℕ} (mr₂ : b + c + 1 ≤ 3 * d) : c ≤ 3 * d := by omega theorem Valid'.node4L_lemma₃ {b c d : ℕ} (mr₁ : 2 * d ≤ b + c + 1) (mm₁ : b ≤ 3 * c) : d ≤ 3 * c := by omega theorem Valid'.node4L_lemma₄ {a b c d : ℕ} (lr₁ : 3 * a ≤ b + c + 1 + d) (mr₂ : b + c + 1 ≤ 3 * d) (mm₁ : b ≤ 3 * c) : a + b + 1 ≤ 3 * (c + d + 1) := by omega theorem Valid'.node4L_lemma₅ {a b c d : ℕ} (lr₂ : 3 * (b + c + 1 + d) ≤ 16 * a + 9) (mr₁ : 2 * d ≤ b + c + 1) (mm₂ : c ≤ 3 * b) : c + d + 1 ≤ 3 * (a + b + 1) := by omega theorem Valid'.node4L {l} {x : α} {m} {y : α} {r o₁ o₂} (hl : Valid' o₁ l x) (hm : Valid' x m y) (hr : Valid' (↑y) r o₂) (Hm : 0 < size m) (H : size l = 0 ∧ size m = 1 ∧ size r ≤ 1 ∨ 0 < size l ∧ ratio * size r ≤ size m ∧ delta * size l ≤ size m + size r ∧ 3 * (size m + size r) ≤ 16 * size l + 9 ∧ size m ≤ delta * size r) : Valid' o₁ (@node4L α l x m y r) o₂ := by obtain - | ⟨s, ml, z, mr⟩ := m; · cases Hm suffices BalancedSz (size l) (size ml) ∧ BalancedSz (size mr) (size r) ∧ BalancedSz (size l + size ml + 1) (size mr + size r + 1) from Valid'.node' (hl.node' hm.left this.1) (hm.right.node' hr this.2.1) this.2.2 rcases H with (⟨l0, m1, r0⟩ | ⟨l0, mr₁, lr₁, lr₂, mr₂⟩) · rw [hm.2.size_eq, Nat.succ_inj, add_eq_zero] at m1 rw [l0, m1.1, m1.2]; revert r0; rcases size r with (_ | _ | _) <;> [decide; decide; (intro r0; unfold BalancedSz delta; omega)] · rcases Nat.eq_zero_or_pos (size r) with r0 | r0 · rw [r0] at mr₂; cases not_le_of_lt Hm mr₂ rw [hm.2.size_eq] at lr₁ lr₂ mr₁ mr₂ by_cases mm : size ml + size mr ≤ 1 · have r1 := le_antisymm ((mul_le_mul_left (by decide)).1 (le_trans mr₁ (Nat.succ_le_succ mm) : _ ≤ ratio * 1)) r0 rw [r1, add_assoc] at lr₁ have l1 := le_antisymm ((mul_le_mul_left (by decide)).1 (le_trans lr₁ (add_le_add_right mm 2) : _ ≤ delta * 1)) l0 rw [l1, r1] revert mm; cases size ml <;> cases size mr <;> intro mm · decide · rw [zero_add] at mm; rcases mm with (_ | ⟨⟨⟩⟩) decide · rcases mm with (_ | ⟨⟨⟩⟩); decide · rw [Nat.succ_add] at mm; rcases mm with (_ | ⟨⟨⟩⟩) rcases hm.3.1.resolve_left mm with ⟨mm₁, mm₂⟩ rcases Nat.eq_zero_or_pos (size ml) with ml0 | ml0 · rw [ml0, mul_zero, Nat.le_zero] at mm₂ rw [ml0, mm₂] at mm; cases mm (by decide) have : 2 * size l ≤ size ml + size mr + 1 := by have := Nat.mul_le_mul_left ratio lr₁ rw [mul_left_comm, mul_add] at this have := le_trans this (add_le_add_left mr₁ _) rw [← Nat.succ_mul] at this exact (mul_le_mul_left (by decide)).1 this refine ⟨Or.inr ⟨?_, ?_⟩, Or.inr ⟨?_, ?_⟩, Or.inr ⟨?_, ?_⟩⟩ · refine (mul_le_mul_left (by decide)).1 (le_trans this ?_) rw [two_mul, Nat.succ_le_iff] refine add_lt_add_of_lt_of_le ?_ mm₂ simpa using (mul_lt_mul_right ml0).2 (by decide : 1 < 3) · exact Nat.le_of_lt_succ (Valid'.node4L_lemma₁ lr₂ mr₂ mm₁) · exact Valid'.node4L_lemma₂ mr₂ · exact Valid'.node4L_lemma₃ mr₁ mm₁ · exact Valid'.node4L_lemma₄ lr₁ mr₂ mm₁ · exact Valid'.node4L_lemma₅ lr₂ mr₁ mm₂ theorem Valid'.rotateL_lemma₁ {a b c : ℕ} (H2 : 3 * a ≤ b + c) (hb₂ : c ≤ 3 * b) : a ≤ 3 * b := by omega theorem Valid'.rotateL_lemma₂ {a b c : ℕ} (H3 : 2 * (b + c) ≤ 9 * a + 3) (h : b < 2 * c) : b < 3 * a + 1 := by omega theorem Valid'.rotateL_lemma₃ {a b c : ℕ} (H2 : 3 * a ≤ b + c) (h : b < 2 * c) : a + b < 3 * c := by omega theorem Valid'.rotateL_lemma₄ {a b : ℕ} (H3 : 2 * b ≤ 9 * a + 3) : 3 * b ≤ 16 * a + 9 := by omega theorem Valid'.rotateL {l} {x : α} {r o₁ o₂} (hl : Valid' o₁ l x) (hr : Valid' x r o₂) (H1 : ¬size l + size r ≤ 1) (H2 : delta * size l < size r) (H3 : 2 * size r ≤ 9 * size l + 5 ∨ size r ≤ 3) : Valid' o₁ (@rotateL α l x r) o₂ := by obtain - | ⟨rs, rl, rx, rr⟩ := r; · cases H2 rw [hr.2.size_eq, Nat.lt_succ_iff] at H2 rw [hr.2.size_eq] at H3 replace H3 : 2 * (size rl + size rr) ≤ 9 * size l + 3 ∨ size rl + size rr ≤ 2 := H3.imp (@Nat.le_of_add_le_add_right _ 2 _) Nat.le_of_succ_le_succ have H3_0 : size l = 0 → size rl + size rr ≤ 2 := by intro l0; rw [l0] at H3 exact (or_iff_right_of_imp fun h => (mul_le_mul_left (by decide)).1 (le_trans h (by decide))).1 H3 have H3p : size l > 0 → 2 * (size rl + size rr) ≤ 9 * size l + 3 := fun l0 : 1 ≤ size l => (or_iff_left_of_imp <| by omega).1 H3 have ablem : ∀ {a b : ℕ}, 1 ≤ a → a + b ≤ 2 → b ≤ 1 := by omega have hlp : size l > 0 → ¬size rl + size rr ≤ 1 := fun l0 hb => absurd (le_trans (le_trans (Nat.mul_le_mul_left _ l0) H2) hb) (by decide) rw [Ordnode.rotateL_node]; split_ifs with h · have rr0 : size rr > 0 := (mul_lt_mul_left (by decide)).1 (lt_of_le_of_lt (Nat.zero_le _) h : ratio * 0 < _) suffices BalancedSz (size l) (size rl) ∧ BalancedSz (size l + size rl + 1) (size rr) by exact hl.node3L hr.left hr.right this.1 this.2 rcases Nat.eq_zero_or_pos (size l) with l0 | l0 · rw [l0]; replace H3 := H3_0 l0 have := hr.3.1 rcases Nat.eq_zero_or_pos (size rl) with rl0 | rl0 · rw [rl0] at this ⊢ rw [le_antisymm (balancedSz_zero.1 this.symm) rr0] decide have rr1 : size rr = 1 := le_antisymm (ablem rl0 H3) rr0 rw [add_comm] at H3 rw [rr1, show size rl = 1 from le_antisymm (ablem rr0 H3) rl0] decide replace H3 := H3p l0 rcases hr.3.1.resolve_left (hlp l0) with ⟨_, hb₂⟩ refine ⟨Or.inr ⟨?_, ?_⟩, Or.inr ⟨?_, ?_⟩⟩ · exact Valid'.rotateL_lemma₁ H2 hb₂ · exact Nat.le_of_lt_succ (Valid'.rotateL_lemma₂ H3 h) · exact Valid'.rotateL_lemma₃ H2 h · exact le_trans hb₂ (Nat.mul_le_mul_left _ <| le_trans (Nat.le_add_left _ _) (Nat.le_add_right _ _)) · rcases Nat.eq_zero_or_pos (size rl) with rl0 | rl0 · rw [rl0, not_lt, Nat.le_zero, Nat.mul_eq_zero] at h replace h := h.resolve_left (by decide) rw [rl0, h, Nat.le_zero, Nat.mul_eq_zero] at H2 rw [hr.2.size_eq, rl0, h, H2.resolve_left (by decide)] at H1 cases H1 (by decide) refine hl.node4L hr.left hr.right rl0 ?_ rcases Nat.eq_zero_or_pos (size l) with l0 | l0 · replace H3 := H3_0 l0 rcases Nat.eq_zero_or_pos (size rr) with rr0 | rr0 · have := hr.3.1 rw [rr0] at this exact Or.inl ⟨l0, le_antisymm (balancedSz_zero.1 this) rl0, rr0.symm ▸ zero_le_one⟩ exact Or.inl ⟨l0, le_antisymm (ablem rr0 <| by rwa [add_comm]) rl0, ablem rl0 H3⟩ exact Or.inr ⟨l0, not_lt.1 h, H2, Valid'.rotateL_lemma₄ (H3p l0), (hr.3.1.resolve_left (hlp l0)).1⟩ theorem Valid'.rotateR {l} {x : α} {r o₁ o₂} (hl : Valid' o₁ l x) (hr : Valid' x r o₂) (H1 : ¬size l + size r ≤ 1) (H2 : delta * size r < size l) (H3 : 2 * size l ≤ 9 * size r + 5 ∨ size l ≤ 3) : Valid' o₁ (@rotateR α l x r) o₂ := by refine Valid'.dual_iff.2 ?_ rw [dual_rotateR] refine hr.dual.rotateL hl.dual ?_ ?_ ?_ · rwa [size_dual, size_dual, add_comm] · rwa [size_dual, size_dual] · rwa [size_dual, size_dual] theorem Valid'.balance'_aux {l} {x : α} {r o₁ o₂} (hl : Valid' o₁ l x) (hr : Valid' x r o₂) (H₁ : 2 * @size α r ≤ 9 * size l + 5 ∨ size r ≤ 3) (H₂ : 2 * @size α l ≤ 9 * size r + 5 ∨ size l ≤ 3) : Valid' o₁ (@balance' α l x r) o₂ := by rw [balance']; split_ifs with h h_1 h_2 · exact hl.node' hr (Or.inl h) · exact hl.rotateL hr h h_1 H₁ · exact hl.rotateR hr h h_2 H₂ · exact hl.node' hr (Or.inr ⟨not_lt.1 h_2, not_lt.1 h_1⟩) theorem Valid'.balance'_lemma {α l l' r r'} (H1 : BalancedSz l' r') (H2 : Nat.dist (@size α l) l' ≤ 1 ∧ size r = r' ∨ Nat.dist (size r) r' ≤ 1 ∧ size l = l') : 2 * @size α r ≤ 9 * size l + 5 ∨ size r ≤ 3 := by suffices @size α r ≤ 3 * (size l + 1) by omega rcases H2 with (⟨hl, rfl⟩ | ⟨hr, rfl⟩) <;> rcases H1 with (h | ⟨_, h₂⟩) · exact le_trans (Nat.le_add_left _ _) (le_trans h (Nat.le_add_left _ _)) · exact le_trans h₂ (Nat.mul_le_mul_left _ <| le_trans (Nat.dist_tri_right _ _) (Nat.add_le_add_left hl _)) · exact le_trans (Nat.dist_tri_left' _ _) (le_trans (add_le_add hr (le_trans (Nat.le_add_left _ _) h)) (by omega)) · rw [Nat.mul_succ] exact le_trans (Nat.dist_tri_right' _ _) (add_le_add h₂ (le_trans hr (by decide))) theorem Valid'.balance' {l} {x : α} {r o₁ o₂} (hl : Valid' o₁ l x) (hr : Valid' x r o₂) (H : ∃ l' r', BalancedSz l' r' ∧ (Nat.dist (size l) l' ≤ 1 ∧ size r = r' ∨ Nat.dist (size r) r' ≤ 1 ∧ size l = l')) : Valid' o₁ (@balance' α l x r) o₂ := let ⟨_, _, H1, H2⟩ := H Valid'.balance'_aux hl hr (Valid'.balance'_lemma H1 H2) (Valid'.balance'_lemma H1.symm H2.symm) theorem Valid'.balance {l} {x : α} {r o₁ o₂} (hl : Valid' o₁ l x) (hr : Valid' x r o₂) (H : ∃ l' r', BalancedSz l' r' ∧ (Nat.dist (size l) l' ≤ 1 ∧ size r = r' ∨ Nat.dist (size r) r' ≤ 1 ∧ size l = l')) : Valid' o₁ (@balance α l x r) o₂ := by rw [balance_eq_balance' hl.3 hr.3 hl.2 hr.2]; exact hl.balance' hr H theorem Valid'.balanceL_aux {l} {x : α} {r o₁ o₂} (hl : Valid' o₁ l x) (hr : Valid' x r o₂) (H₁ : size l = 0 → size r ≤ 1) (H₂ : 1 ≤ size l → 1 ≤ size r → size r ≤ delta * size l) (H₃ : 2 * @size α l ≤ 9 * size r + 5 ∨ size l ≤ 3) : Valid' o₁ (@balanceL α l x r) o₂ := by rw [balanceL_eq_balance hl.2 hr.2 H₁ H₂, balance_eq_balance' hl.3 hr.3 hl.2 hr.2] refine hl.balance'_aux hr (Or.inl ?_) H₃ rcases Nat.eq_zero_or_pos (size r) with r0 | r0 · rw [r0]; exact Nat.zero_le _ rcases Nat.eq_zero_or_pos (size l) with l0 | l0 · rw [l0]; exact le_trans (Nat.mul_le_mul_left _ (H₁ l0)) (by decide) replace H₂ : _ ≤ 3 * _ := H₂ l0 r0; omega theorem Valid'.balanceL {l} {x : α} {r o₁ o₂} (hl : Valid' o₁ l x) (hr : Valid' x r o₂) (H : (∃ l', Raised l' (size l) ∧ BalancedSz l' (size r)) ∨ ∃ r', Raised (size r) r' ∧ BalancedSz (size l) r') : Valid' o₁ (@balanceL α l x r) o₂ := by rw [balanceL_eq_balance' hl.3 hr.3 hl.2 hr.2 H] refine hl.balance' hr ?_ rcases H with (⟨l', e, H⟩ | ⟨r', e, H⟩) · exact ⟨_, _, H, Or.inl ⟨e.dist_le', rfl⟩⟩ · exact ⟨_, _, H, Or.inr ⟨e.dist_le, rfl⟩⟩ theorem Valid'.balanceR_aux {l} {x : α} {r o₁ o₂} (hl : Valid' o₁ l x) (hr : Valid' x r o₂) (H₁ : size r = 0 → size l ≤ 1) (H₂ : 1 ≤ size r → 1 ≤ size l → size l ≤ delta * size r) (H₃ : 2 * @size α r ≤ 9 * size l + 5 ∨ size r ≤ 3) : Valid' o₁ (@balanceR α l x r) o₂ := by rw [Valid'.dual_iff, dual_balanceR] have := hr.dual.balanceL_aux hl.dual rw [size_dual, size_dual] at this exact this H₁ H₂ H₃ theorem Valid'.balanceR {l} {x : α} {r o₁ o₂} (hl : Valid' o₁ l x) (hr : Valid' x r o₂) (H : (∃ l', Raised (size l) l' ∧ BalancedSz l' (size r)) ∨ ∃ r', Raised r' (size r) ∧ BalancedSz (size l) r') : Valid' o₁ (@balanceR α l x r) o₂ := by rw [Valid'.dual_iff, dual_balanceR]; exact hr.dual.balanceL hl.dual (balance_sz_dual H) theorem Valid'.eraseMax_aux {s l x r o₁ o₂} (H : Valid' o₁ (.node s l x r) o₂) : Valid' o₁ (@eraseMax α (.node' l x r)) ↑(findMax' x r) ∧ size (.node' l x r) = size (eraseMax (.node' l x r)) + 1 := by have := H.2.eq_node'; rw [this] at H; clear this induction r generalizing l x o₁ with | nil => exact ⟨H.left, rfl⟩ | node rs rl rx rr _ IHrr => have := H.2.2.2.eq_node'; rw [this] at H ⊢ rcases IHrr H.right with ⟨h, e⟩ refine ⟨Valid'.balanceL H.left h (Or.inr ⟨_, Or.inr e, H.3.1⟩), ?_⟩ rw [eraseMax, size_balanceL H.3.2.1 h.3 H.2.2.1 h.2 (Or.inr ⟨_, Or.inr e, H.3.1⟩)] rw [size_node, e]; rfl theorem Valid'.eraseMin_aux {s l} {x : α} {r o₁ o₂} (H : Valid' o₁ (.node s l x r) o₂) : Valid' ↑(findMin' l x) (@eraseMin α (.node' l x r)) o₂ ∧ size (.node' l x r) = size (eraseMin (.node' l x r)) + 1 := by have := H.dual.eraseMax_aux rwa [← dual_node', size_dual, ← dual_eraseMin, size_dual, ← Valid'.dual_iff, findMax'_dual] at this theorem eraseMin.valid : ∀ {t}, @Valid α _ t → Valid (eraseMin t) | nil, _ => valid_nil | node _ l x r, h => by rw [h.2.eq_node']; exact h.eraseMin_aux.1.valid theorem eraseMax.valid {t} (h : @Valid α _ t) : Valid (eraseMax t) := by rw [Valid.dual_iff, dual_eraseMax]; exact eraseMin.valid h.dual theorem Valid'.glue_aux {l r o₁ o₂} (hl : Valid' o₁ l o₂) (hr : Valid' o₁ r o₂) (sep : l.All fun x => r.All fun y => x < y) (bal : BalancedSz (size l) (size r)) : Valid' o₁ (@glue α l r) o₂ ∧ size (glue l r) = size l + size r := by obtain - | ⟨ls, ll, lx, lr⟩ := l; · exact ⟨hr, (zero_add _).symm⟩ obtain - | ⟨rs, rl, rx, rr⟩ := r; · exact ⟨hl, rfl⟩ dsimp [glue]; split_ifs · rw [splitMax_eq] · obtain ⟨v, e⟩ := Valid'.eraseMax_aux hl suffices H : _ by refine ⟨Valid'.balanceR v (hr.of_gt ?_ ?_) H, ?_⟩ · refine findMax'_all (P := fun a : α => Bounded nil (a : WithTop α) o₂) lx lr hl.1.2.to_nil (sep.2.2.imp ?_) exact fun x h => hr.1.2.to_nil.mono_left (le_of_lt h.2.1) · exact @findMax'_all _ (fun a => All (· > a) (.node rs rl rx rr)) lx lr sep.2.1 sep.2.2 · rw [size_balanceR v.3 hr.3 v.2 hr.2 H, add_right_comm, ← e, hl.2.1]; rfl refine Or.inl ⟨_, Or.inr e, ?_⟩ rwa [hl.2.eq_node'] at bal · rw [splitMin_eq] · obtain ⟨v, e⟩ := Valid'.eraseMin_aux hr suffices H : _ by refine ⟨Valid'.balanceL (hl.of_lt ?_ ?_) v H, ?_⟩ · refine @findMin'_all (P := fun a : α => Bounded nil o₁ (a : WithBot α)) _ rl rx (sep.2.1.1.imp ?_) hr.1.1.to_nil exact fun y h => hl.1.1.to_nil.mono_right (le_of_lt h) · exact @findMin'_all _ (fun a => All (· < a) (.node ls ll lx lr)) rl rx (all_iff_forall.2 fun x hx => sep.imp fun y hy => all_iff_forall.1 hy.1 _ hx) (sep.imp fun y hy => hy.2.1) · rw [size_balanceL hl.3 v.3 hl.2 v.2 H, add_assoc, ← e, hr.2.1]; rfl refine Or.inr ⟨_, Or.inr e, ?_⟩ rwa [hr.2.eq_node'] at bal theorem Valid'.glue {l} {x : α} {r o₁ o₂} (hl : Valid' o₁ l x) (hr : Valid' x r o₂) : BalancedSz (size l) (size r) → Valid' o₁ (@glue α l r) o₂ ∧ size (@glue α l r) = size l + size r := Valid'.glue_aux (hl.trans_right hr.1) (hr.trans_left hl.1) (hl.1.to_sep hr.1) theorem Valid'.merge_lemma {a b c : ℕ} (h₁ : 3 * a < b + c + 1) (h₂ : b ≤ 3 * c) : 2 * (a + b) ≤ 9 * c + 5 := by omega theorem Valid'.merge_aux₁ {o₁ o₂ ls ll lx lr rs rl rx rr t} (hl : Valid' o₁ (@Ordnode.node α ls ll lx lr) o₂) (hr : Valid' o₁ (.node rs rl rx rr) o₂) (h : delta * ls < rs) (v : Valid' o₁ t rx) (e : size t = ls + size rl) : Valid' o₁ (.balanceL t rx rr) o₂ ∧ size (.balanceL t rx rr) = ls + rs := by rw [hl.2.1] at e rw [hl.2.1, hr.2.1, delta] at h rcases hr.3.1 with (H | ⟨hr₁, hr₂⟩); · omega suffices H₂ : _ by suffices H₁ : _ by refine ⟨Valid'.balanceL_aux v hr.right H₁ H₂ ?_, ?_⟩ · rw [e]; exact Or.inl (Valid'.merge_lemma h hr₁) · rw [balanceL_eq_balance v.2 hr.2.2.2 H₁ H₂, balance_eq_balance' v.3 hr.3.2.2 v.2 hr.2.2.2, size_balance' v.2 hr.2.2.2, e, hl.2.1, hr.2.1] abel · rw [e, add_right_comm]; rintro ⟨⟩ intro _ _; rw [e]; unfold delta at hr₂ ⊢; omega theorem Valid'.merge_aux {l r o₁ o₂} (hl : Valid' o₁ l o₂) (hr : Valid' o₁ r o₂) (sep : l.All fun x => r.All fun y => x < y) : Valid' o₁ (@merge α l r) o₂ ∧ size (merge l r) = size l + size r := by induction l generalizing o₁ o₂ r with | nil => exact ⟨hr, (zero_add _).symm⟩ | node ls ll lx lr _ IHlr => ?_ induction r generalizing o₁ o₂ with | nil => exact ⟨hl, rfl⟩ | node rs rl rx rr IHrl _ => ?_ rw [merge_node]; split_ifs with h h_1 · obtain ⟨v, e⟩ := IHrl (hl.of_lt hr.1.1.to_nil <| sep.imp fun x h => h.2.1) hr.left (sep.imp fun x h => h.1) exact Valid'.merge_aux₁ hl hr h v e · obtain ⟨v, e⟩ := IHlr hl.right (hr.of_gt hl.1.2.to_nil sep.2.1) sep.2.2 have := Valid'.merge_aux₁ hr.dual hl.dual h_1 v.dual rw [size_dual, add_comm, size_dual, ← dual_balanceR, ← Valid'.dual_iff, size_dual, add_comm rs] at this exact this e · refine Valid'.glue_aux hl hr sep (Or.inr ⟨not_lt.1 h_1, not_lt.1 h⟩) theorem Valid.merge {l r} (hl : Valid l) (hr : Valid r) (sep : l.All fun x => r.All fun y => x < y) : Valid (@merge α l r) := (Valid'.merge_aux hl hr sep).1 theorem insertWith.valid_aux [IsTotal α (· ≤ ·)] [DecidableLE α] (f : α → α) (x : α) (hf : ∀ y, x ≤ y ∧ y ≤ x → x ≤ f y ∧ f y ≤ x) : ∀ {t o₁ o₂}, Valid' o₁ t o₂ → Bounded nil o₁ x → Bounded nil x o₂ → Valid' o₁ (insertWith f x t) o₂ ∧ Raised (size t) (size (insertWith f x t)) | nil, _, _, _, bl, br => ⟨valid'_singleton bl br, Or.inr rfl⟩ | node sz l y r, o₁, o₂, h, bl, br => by rw [insertWith, cmpLE] split_ifs with h_1 h_2 <;> dsimp only · rcases h with ⟨⟨lx, xr⟩, hs, hb⟩ rcases hf _ ⟨h_1, h_2⟩ with ⟨xf, fx⟩ refine ⟨⟨⟨lx.mono_right (le_trans h_2 xf), xr.mono_left (le_trans fx h_1)⟩, hs, hb⟩, Or.inl rfl⟩ · rcases insertWith.valid_aux f x hf h.left bl (lt_of_le_not_le h_1 h_2) with ⟨vl, e⟩ suffices H : _ by refine ⟨vl.balanceL h.right H, ?_⟩ rw [size_balanceL vl.3 h.3.2.2 vl.2 h.2.2.2 H, h.2.size_eq] exact (e.add_right _).add_right _ exact Or.inl ⟨_, e, h.3.1⟩ · have : y < x := lt_of_le_not_le ((total_of (· ≤ ·) _ _).resolve_left h_1) h_1 rcases insertWith.valid_aux f x hf h.right this br with ⟨vr, e⟩ suffices H : _ by refine ⟨h.left.balanceR vr H, ?_⟩ rw [size_balanceR h.3.2.1 vr.3 h.2.2.1 vr.2 H, h.2.size_eq] exact (e.add_left _).add_right _ exact Or.inr ⟨_, e, h.3.1⟩ theorem insertWith.valid [IsTotal α (· ≤ ·)] [DecidableLE α] (f : α → α) (x : α) (hf : ∀ y, x ≤ y ∧ y ≤ x → x ≤ f y ∧ f y ≤ x) {t} (h : Valid t) : Valid (insertWith f x t) := (insertWith.valid_aux _ _ hf h ⟨⟩ ⟨⟩).1 theorem insert_eq_insertWith [DecidableLE α] (x : α) : ∀ t, Ordnode.insert x t = insertWith (fun _ => x) x t | nil => rfl | node _ l y r => by unfold Ordnode.insert insertWith; cases cmpLE x y <;> simp [insert_eq_insertWith] theorem insert.valid [IsTotal α (· ≤ ·)] [DecidableLE α] (x : α) {t} (h : Valid t) : Valid (Ordnode.insert x t) := by rw [insert_eq_insertWith]; exact insertWith.valid _ _ (fun _ _ => ⟨le_rfl, le_rfl⟩) h theorem insert'_eq_insertWith [DecidableLE α] (x : α) : ∀ t, insert' x t = insertWith id x t | nil => rfl | node _ l y r => by unfold insert' insertWith; cases cmpLE x y <;> simp [insert'_eq_insertWith] theorem insert'.valid [IsTotal α (· ≤ ·)] [DecidableLE α] (x : α) {t} (h : Valid t) : Valid (insert' x t) := by rw [insert'_eq_insertWith]; exact insertWith.valid _ _ (fun _ => id) h theorem Valid'.map_aux {β} [Preorder β] {f : α → β} (f_strict_mono : StrictMono f) {t a₁ a₂} (h : Valid' a₁ t a₂) : Valid' (Option.map f a₁) (map f t) (Option.map f a₂) ∧ (map f t).size = t.size := by induction t generalizing a₁ a₂ with | nil => simp only [map, size_nil, and_true]; apply valid'_nil cases a₁; · trivial cases a₂; · trivial simp only [Option.map, Bounded] exact f_strict_mono h.ord | node _ _ _ _ t_ih_l t_ih_r => have t_ih_l' := t_ih_l h.left have t_ih_r' := t_ih_r h.right clear t_ih_l t_ih_r obtain ⟨t_l_valid, t_l_size⟩ := t_ih_l' obtain ⟨t_r_valid, t_r_size⟩ := t_ih_r' simp only [map, size_node, and_true] constructor · exact And.intro t_l_valid.ord t_r_valid.ord · constructor · rw [t_l_size, t_r_size]; exact h.sz.1 · constructor · exact t_l_valid.sz · exact t_r_valid.sz · constructor · rw [t_l_size, t_r_size]; exact h.bal.1 · constructor · exact t_l_valid.bal · exact t_r_valid.bal theorem map.valid {β} [Preorder β] {f : α → β} (f_strict_mono : StrictMono f) {t} (h : Valid t) : Valid (map f t) := (Valid'.map_aux f_strict_mono h).1 theorem Valid'.erase_aux [DecidableLE α] (x : α) {t a₁ a₂} (h : Valid' a₁ t a₂) : Valid' a₁ (erase x t) a₂ ∧ Raised (erase x t).size t.size := by induction t generalizing a₁ a₂ with | nil => simpa [erase, Raised] | node _ t_l t_x t_r t_ih_l t_ih_r => simp only [erase, size_node] have t_ih_l' := t_ih_l h.left have t_ih_r' := t_ih_r h.right clear t_ih_l t_ih_r obtain ⟨t_l_valid, t_l_size⟩ := t_ih_l' obtain ⟨t_r_valid, t_r_size⟩ := t_ih_r' cases cmpLE x t_x <;> rw [h.sz.1] · suffices h_balanceable : _ by constructor · exact Valid'.balanceR t_l_valid h.right h_balanceable · rw [size_balanceR t_l_valid.bal h.right.bal t_l_valid.sz h.right.sz h_balanceable] repeat apply Raised.add_right exact t_l_size left; exists t_l.size; exact And.intro t_l_size h.bal.1 · have h_glue := Valid'.glue h.left h.right h.bal.1 obtain ⟨h_glue_valid, h_glue_sized⟩ := h_glue constructor · exact h_glue_valid · right; rw [h_glue_sized] · suffices h_balanceable : _ by constructor · exact Valid'.balanceL h.left t_r_valid h_balanceable · rw [size_balanceL h.left.bal t_r_valid.bal h.left.sz t_r_valid.sz h_balanceable] apply Raised.add_right apply Raised.add_left exact t_r_size right; exists t_r.size; exact And.intro t_r_size h.bal.1 theorem erase.valid [DecidableLE α] (x : α) {t} (h : Valid t) : Valid (erase x t) := (Valid'.erase_aux x h).1 theorem size_erase_of_mem [DecidableLE α] {x : α} {t a₁ a₂} (h : Valid' a₁ t a₂) (h_mem : x ∈ t) : size (erase x t) = size t - 1 := by induction t generalizing a₁ a₂ with | nil => contradiction | node _ t_l t_x t_r t_ih_l t_ih_r => have t_ih_l' := t_ih_l h.left have t_ih_r' := t_ih_r h.right clear t_ih_l t_ih_r dsimp only [Membership.mem, mem] at h_mem unfold erase revert h_mem; cases cmpLE x t_x <;> intro h_mem <;> dsimp only at h_mem ⊢ · have t_ih_l := t_ih_l' h_mem clear t_ih_l' t_ih_r' have t_l_h := Valid'.erase_aux x h.left obtain ⟨t_l_valid, t_l_size⟩ := t_l_h rw [size_balanceR t_l_valid.bal h.right.bal t_l_valid.sz h.right.sz (Or.inl (Exists.intro t_l.size (And.intro t_l_size h.bal.1)))] rw [t_ih_l, h.sz.1] have h_pos_t_l_size := pos_size_of_mem h.left.sz h_mem revert h_pos_t_l_size; rcases t_l.size with - | t_l_size <;> intro h_pos_t_l_size · cases h_pos_t_l_size · simp [Nat.add_right_comm] · rw [(Valid'.glue h.left h.right h.bal.1).2, h.sz.1]; rfl · have t_ih_r := t_ih_r' h_mem clear t_ih_l' t_ih_r' have t_r_h := Valid'.erase_aux x h.right obtain ⟨t_r_valid, t_r_size⟩ := t_r_h rw [size_balanceL h.left.bal t_r_valid.bal h.left.sz t_r_valid.sz (Or.inr (Exists.intro t_r.size (And.intro t_r_size h.bal.1)))] rw [t_ih_r, h.sz.1] have h_pos_t_r_size := pos_size_of_mem h.right.sz h_mem revert h_pos_t_r_size; rcases t_r.size with - | t_r_size <;> intro h_pos_t_r_size · cases h_pos_t_r_size · simp [Nat.add_assoc] end Valid end Ordnode /-- An `Ordset α` is a finite set of values, represented as a tree. The operations on this type maintain that the tree is balanced and correctly stores subtree sizes at each level. The correctness property of the tree is baked into the type, so all operations on this type are correct by construction. -/ def Ordset (α : Type*) [Preorder α] := { t : Ordnode α // t.Valid } namespace Ordset open Ordnode variable [Preorder α] /-- O(1). The empty set. -/ nonrec def nil : Ordset α := ⟨nil, ⟨⟩, ⟨⟩, ⟨⟩⟩ /-- O(1). Get the size of the set. -/ def size (s : Ordset α) : ℕ := s.1.size /-- O(1). Construct a singleton set containing value `a`. -/ protected def singleton (a : α) : Ordset α := ⟨singleton a, valid_singleton⟩ instance instEmptyCollection : EmptyCollection (Ordset α) := ⟨nil⟩ instance instInhabited : Inhabited (Ordset α) := ⟨nil⟩ instance instSingleton : Singleton α (Ordset α) := ⟨Ordset.singleton⟩ /-- O(1). Is the set empty? -/ def Empty (s : Ordset α) : Prop := s = ∅ theorem empty_iff {s : Ordset α} : s = ∅ ↔ s.1.empty := ⟨fun h => by cases h; exact rfl, fun h => by cases s with | mk s_val _ => cases s_val <;> [rfl; cases h]⟩ instance Empty.instDecidablePred : DecidablePred (@Empty α _) := fun _ => decidable_of_iff' _ empty_iff /-- O(log n). Insert an element into the set, preserving balance and the BST property. If an equivalent element is already in the set, this replaces it. -/ protected def insert [IsTotal α (· ≤ ·)] [DecidableLE α] (x : α) (s : Ordset α) : Ordset α := ⟨Ordnode.insert x s.1, insert.valid _ s.2⟩ instance instInsert [IsTotal α (· ≤ ·)] [DecidableLE α] : Insert α (Ordset α) := ⟨Ordset.insert⟩ /-- O(log n). Insert an element into the set, preserving balance and the BST property. If an equivalent element is already in the set, the set is returned as is. -/ nonrec def insert' [IsTotal α (· ≤ ·)] [DecidableLE α] (x : α) (s : Ordset α) : Ordset α := ⟨insert' x s.1, insert'.valid _ s.2⟩ section variable [DecidableLE α] /-- O(log n). Does the set contain the element `x`? That is, is there an element that is equivalent to `x` in the order? -/ def mem (x : α) (s : Ordset α) : Bool := x ∈ s.val /-- O(log n). Retrieve an element in the set that is equivalent to `x` in the order, if it exists. -/ def find (x : α) (s : Ordset α) : Option α := Ordnode.find x s.val instance instMembership : Membership α (Ordset α) := ⟨fun s x => mem x s⟩ instance mem.decidable (x : α) (s : Ordset α) : Decidable (x ∈ s) := instDecidableEqBool _ _ theorem pos_size_of_mem {x : α} {t : Ordset α} (h_mem : x ∈ t) : 0 < size t := by simp? [Membership.mem, mem] at h_mem says simp only [Membership.mem, mem, Bool.decide_eq_true] at h_mem apply Ordnode.pos_size_of_mem t.property.sz h_mem end /-- O(log n). Remove an element from the set equivalent to `x`. Does nothing if there is no such element. -/ def erase [DecidableLE α] (x : α) (s : Ordset α) : Ordset α := ⟨Ordnode.erase x s.val, Ordnode.erase.valid x s.property⟩ /-- O(n). Map a function across a tree, without changing the structure. -/ def map {β} [Preorder β] (f : α → β) (f_strict_mono : StrictMono f) (s : Ordset α) : Ordset β := ⟨Ordnode.map f s.val, Ordnode.map.valid f_strict_mono s.property⟩ end Ordset
Mathlib/Data/Ordmap/Ordset.lean
1,154
1,155
/- Copyright (c) 2018 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import Mathlib.Algebra.BigOperators.Expect import Mathlib.Algebra.Order.BigOperators.Ring.Finset import Mathlib.Algebra.Order.Field.Canonical import Mathlib.Algebra.Order.Nonneg.Floor import Mathlib.Data.Real.Pointwise import Mathlib.Data.NNReal.Defs import Mathlib.Order.ConditionallyCompleteLattice.Group /-! # Basic results on nonnegative real numbers This file contains all results on `NNReal` that do not directly follow from its basic structure. As a consequence, it is a bit of a random collection of results, and is a good target for cleanup. ## Notations This file uses `ℝ≥0` as a localized notation for `NNReal`. -/ assert_not_exists Star open Function open scoped BigOperators namespace NNReal noncomputable instance : FloorSemiring ℝ≥0 := Nonneg.floorSemiring @[simp, norm_cast] theorem coe_indicator {α} (s : Set α) (f : α → ℝ≥0) (a : α) : ((s.indicator f a : ℝ≥0) : ℝ) = s.indicator (fun x => ↑(f x)) a := (toRealHom : ℝ≥0 →+ ℝ).map_indicator _ _ _ @[norm_cast] theorem coe_list_sum (l : List ℝ≥0) : ((l.sum : ℝ≥0) : ℝ) = (l.map (↑)).sum := map_list_sum toRealHom l @[norm_cast] theorem coe_list_prod (l : List ℝ≥0) : ((l.prod : ℝ≥0) : ℝ) = (l.map (↑)).prod := map_list_prod toRealHom l @[norm_cast] theorem coe_multiset_sum (s : Multiset ℝ≥0) : ((s.sum : ℝ≥0) : ℝ) = (s.map (↑)).sum := map_multiset_sum toRealHom s @[norm_cast] theorem coe_multiset_prod (s : Multiset ℝ≥0) : ((s.prod : ℝ≥0) : ℝ) = (s.map (↑)).prod := map_multiset_prod toRealHom s variable {ι : Type*} {s : Finset ι} {f : ι → ℝ} @[simp, norm_cast] theorem coe_sum (s : Finset ι) (f : ι → ℝ≥0) : ∑ i ∈ s, f i = ∑ i ∈ s, (f i : ℝ) := map_sum toRealHom _ _ @[simp, norm_cast] lemma coe_expect (s : Finset ι) (f : ι → ℝ≥0) : 𝔼 i ∈ s, f i = 𝔼 i ∈ s, (f i : ℝ) := map_expect toRealHom .. theorem _root_.Real.toNNReal_sum_of_nonneg (hf : ∀ i ∈ s, 0 ≤ f i) : Real.toNNReal (∑ a ∈ s, f a) = ∑ a ∈ s, Real.toNNReal (f a) := by rw [← coe_inj, NNReal.coe_sum, Real.coe_toNNReal _ (Finset.sum_nonneg hf)] exact Finset.sum_congr rfl fun x hxs => by rw [Real.coe_toNNReal _ (hf x hxs)] @[simp, norm_cast] theorem coe_prod (s : Finset ι) (f : ι → ℝ≥0) : ↑(∏ a ∈ s, f a) = ∏ a ∈ s, (f a : ℝ) := map_prod toRealHom _ _ theorem _root_.Real.toNNReal_prod_of_nonneg (hf : ∀ a, a ∈ s → 0 ≤ f a) : Real.toNNReal (∏ a ∈ s, f a) = ∏ a ∈ s, Real.toNNReal (f a) := by rw [← coe_inj, NNReal.coe_prod, Real.coe_toNNReal _ (Finset.prod_nonneg hf)] exact Finset.prod_congr rfl fun x hxs => by rw [Real.coe_toNNReal _ (hf x hxs)] theorem le_iInf_add_iInf {ι ι' : Sort*} [Nonempty ι] [Nonempty ι'] {f : ι → ℝ≥0} {g : ι' → ℝ≥0} {a : ℝ≥0} (h : ∀ i j, a ≤ f i + g j) : a ≤ (⨅ i, f i) + ⨅ j, g j := by rw [← NNReal.coe_le_coe, NNReal.coe_add, coe_iInf, coe_iInf] exact le_ciInf_add_ciInf h theorem mul_finset_sup {α} (r : ℝ≥0) (s : Finset α) (f : α → ℝ≥0) : r * s.sup f = s.sup fun a => r * f a := Finset.comp_sup_eq_sup_comp _ (NNReal.mul_sup r) (mul_zero r) theorem finset_sup_mul {α} (s : Finset α) (f : α → ℝ≥0) (r : ℝ≥0) : s.sup f * r = s.sup fun a => f a * r := Finset.comp_sup_eq_sup_comp (· * r) (fun x y => NNReal.sup_mul x y r) (zero_mul r) theorem finset_sup_div {α} {f : α → ℝ≥0} {s : Finset α} (r : ℝ≥0) : s.sup f / r = s.sup fun a => f a / r := by simp only [div_eq_inv_mul, mul_finset_sup] open Real section Sub /-! ### Lemmas about subtraction In this section we provide a few lemmas about subtraction that do not fit well into any other typeclass. For lemmas about subtraction and addition see lemmas about `OrderedSub` in the file `Mathlib.Algebra.Order.Sub.Basic`. See also `mul_tsub` and `tsub_mul`. -/ theorem sub_div (a b c : ℝ≥0) : (a - b) / c = a / c - b / c := tsub_div _ _ _ end Sub section Csupr open Set variable {ι : Sort*} {f : ι → ℝ≥0} theorem iInf_mul (f : ι → ℝ≥0) (a : ℝ≥0) : iInf f * a = ⨅ i, f i * a := by rw [← coe_inj, NNReal.coe_mul, coe_iInf, coe_iInf] exact Real.iInf_mul_of_nonneg (NNReal.coe_nonneg _) _ theorem mul_iInf (f : ι → ℝ≥0) (a : ℝ≥0) : a * iInf f = ⨅ i, a * f i := by simpa only [mul_comm] using iInf_mul f a theorem mul_iSup (f : ι → ℝ≥0) (a : ℝ≥0) : (a * ⨆ i, f i) = ⨆ i, a * f i := by rw [← coe_inj, NNReal.coe_mul, NNReal.coe_iSup, NNReal.coe_iSup] exact Real.mul_iSup_of_nonneg (NNReal.coe_nonneg _) _ theorem iSup_mul (f : ι → ℝ≥0) (a : ℝ≥0) : (⨆ i, f i) * a = ⨆ i, f i * a := by rw [mul_comm, mul_iSup] simp_rw [mul_comm] theorem iSup_div (f : ι → ℝ≥0) (a : ℝ≥0) : (⨆ i, f i) / a = ⨆ i, f i / a := by simp only [div_eq_mul_inv, iSup_mul] theorem mul_iSup_le {a : ℝ≥0} {g : ℝ≥0} {h : ι → ℝ≥0} (H : ∀ j, g * h j ≤ a) : g * iSup h ≤ a := by rw [mul_iSup] exact ciSup_le' H theorem iSup_mul_le {a : ℝ≥0} {g : ι → ℝ≥0} {h : ℝ≥0} (H : ∀ i, g i * h ≤ a) : iSup g * h ≤ a := by rw [iSup_mul] exact ciSup_le' H theorem iSup_mul_iSup_le {a : ℝ≥0} {g h : ι → ℝ≥0} (H : ∀ i j, g i * h j ≤ a) : iSup g * iSup h ≤ a := iSup_mul_le fun _ => mul_iSup_le <| H _ variable [Nonempty ι] theorem le_mul_iInf {a : ℝ≥0} {g : ℝ≥0} {h : ι → ℝ≥0} (H : ∀ j, a ≤ g * h j) : a ≤ g * iInf h := by rw [mul_iInf] exact le_ciInf H theorem le_iInf_mul {a : ℝ≥0} {g : ι → ℝ≥0} {h : ℝ≥0} (H : ∀ i, a ≤ g i * h) : a ≤ iInf g * h := by rw [iInf_mul] exact le_ciInf H theorem le_iInf_mul_iInf {a : ℝ≥0} {g h : ι → ℝ≥0} (H : ∀ i j, a ≤ g i * h j) : a ≤ iInf g * iInf h := le_iInf_mul fun i => le_mul_iInf <| H i end Csupr end NNReal
Mathlib/Data/NNReal/Basic.lean
1,017
1,021
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Abhimanyu Pallavi Sudhir, Jean Lo, Calle Sönne, Sébastien Gouëzel, Rémy Degenne, David Loeffler -/ import Mathlib.Analysis.SpecialFunctions.Pow.Real /-! # Power function on `ℝ≥0` and `ℝ≥0∞` We construct the power functions `x ^ y` where * `x` is a nonnegative real number and `y` is a real number; * `x` is a number from `[0, +∞]` (a.k.a. `ℝ≥0∞`) and `y` is a real number. We also prove basic properties of these functions. -/ noncomputable section open Real NNReal ENNReal ComplexConjugate Finset Function Set namespace NNReal variable {x : ℝ≥0} {w y z : ℝ} /-- The nonnegative real power function `x^y`, defined for `x : ℝ≥0` and `y : ℝ` as the restriction of the real power function. For `x > 0`, it is equal to `exp (y log x)`. For `x = 0`, one sets `0 ^ 0 = 1` and `0 ^ y = 0` for `y ≠ 0`. -/ noncomputable def rpow (x : ℝ≥0) (y : ℝ) : ℝ≥0 := ⟨(x : ℝ) ^ y, Real.rpow_nonneg x.2 y⟩ noncomputable instance : Pow ℝ≥0 ℝ := ⟨rpow⟩ @[simp] theorem rpow_eq_pow (x : ℝ≥0) (y : ℝ) : rpow x y = x ^ y := rfl @[simp, norm_cast] theorem coe_rpow (x : ℝ≥0) (y : ℝ) : ((x ^ y : ℝ≥0) : ℝ) = (x : ℝ) ^ y := rfl @[simp] theorem rpow_zero (x : ℝ≥0) : x ^ (0 : ℝ) = 1 := NNReal.eq <| Real.rpow_zero _ @[simp] theorem rpow_eq_zero_iff {x : ℝ≥0} {y : ℝ} : x ^ y = 0 ↔ x = 0 ∧ y ≠ 0 := by rw [← NNReal.coe_inj, coe_rpow, ← NNReal.coe_eq_zero] exact Real.rpow_eq_zero_iff_of_nonneg x.2 lemma rpow_eq_zero (hy : y ≠ 0) : x ^ y = 0 ↔ x = 0 := by simp [hy] @[simp] theorem zero_rpow {x : ℝ} (h : x ≠ 0) : (0 : ℝ≥0) ^ x = 0 := NNReal.eq <| Real.zero_rpow h @[simp] theorem rpow_one (x : ℝ≥0) : x ^ (1 : ℝ) = x := NNReal.eq <| Real.rpow_one _ lemma rpow_neg (x : ℝ≥0) (y : ℝ) : x ^ (-y) = (x ^ y)⁻¹ := NNReal.eq <| Real.rpow_neg x.2 _ @[simp, norm_cast] lemma rpow_natCast (x : ℝ≥0) (n : ℕ) : x ^ (n : ℝ) = x ^ n := NNReal.eq <| by simpa only [coe_rpow, coe_pow] using Real.rpow_natCast x n @[simp, norm_cast] lemma rpow_intCast (x : ℝ≥0) (n : ℤ) : x ^ (n : ℝ) = x ^ n := by cases n <;> simp only [Int.ofNat_eq_coe, Int.cast_natCast, rpow_natCast, zpow_natCast, Int.cast_negSucc, rpow_neg, zpow_negSucc] @[simp] theorem one_rpow (x : ℝ) : (1 : ℝ≥0) ^ x = 1 := NNReal.eq <| Real.one_rpow _ theorem rpow_add {x : ℝ≥0} (hx : x ≠ 0) (y z : ℝ) : x ^ (y + z) = x ^ y * x ^ z := NNReal.eq <| Real.rpow_add ((NNReal.coe_pos.trans pos_iff_ne_zero).mpr hx) _ _ theorem rpow_add' (h : y + z ≠ 0) (x : ℝ≥0) : x ^ (y + z) = x ^ y * x ^ z := NNReal.eq <| Real.rpow_add' x.2 h lemma rpow_add_intCast (hx : x ≠ 0) (y : ℝ) (n : ℤ) : x ^ (y + n) = x ^ y * x ^ n := by ext; exact Real.rpow_add_intCast (mod_cast hx) _ _ lemma rpow_add_natCast (hx : x ≠ 0) (y : ℝ) (n : ℕ) : x ^ (y + n) = x ^ y * x ^ n := by ext; exact Real.rpow_add_natCast (mod_cast hx) _ _ lemma rpow_sub_intCast (hx : x ≠ 0) (y : ℝ) (n : ℕ) : x ^ (y - n) = x ^ y / x ^ n := by ext; exact Real.rpow_sub_intCast (mod_cast hx) _ _ lemma rpow_sub_natCast (hx : x ≠ 0) (y : ℝ) (n : ℕ) : x ^ (y - n) = x ^ y / x ^ n := by ext; exact Real.rpow_sub_natCast (mod_cast hx) _ _ lemma rpow_add_intCast' {n : ℤ} (h : y + n ≠ 0) (x : ℝ≥0) : x ^ (y + n) = x ^ y * x ^ n := by ext; exact Real.rpow_add_intCast' (mod_cast x.2) h lemma rpow_add_natCast' {n : ℕ} (h : y + n ≠ 0) (x : ℝ≥0) : x ^ (y + n) = x ^ y * x ^ n := by ext; exact Real.rpow_add_natCast' (mod_cast x.2) h lemma rpow_sub_intCast' {n : ℤ} (h : y - n ≠ 0) (x : ℝ≥0) : x ^ (y - n) = x ^ y / x ^ n := by ext; exact Real.rpow_sub_intCast' (mod_cast x.2) h lemma rpow_sub_natCast' {n : ℕ} (h : y - n ≠ 0) (x : ℝ≥0) : x ^ (y - n) = x ^ y / x ^ n := by ext; exact Real.rpow_sub_natCast' (mod_cast x.2) h lemma rpow_add_one (hx : x ≠ 0) (y : ℝ) : x ^ (y + 1) = x ^ y * x := by simpa using rpow_add_natCast hx y 1 lemma rpow_sub_one (hx : x ≠ 0) (y : ℝ) : x ^ (y - 1) = x ^ y / x := by simpa using rpow_sub_natCast hx y 1 lemma rpow_add_one' (h : y + 1 ≠ 0) (x : ℝ≥0) : x ^ (y + 1) = x ^ y * x := by rw [rpow_add' h, rpow_one] lemma rpow_one_add' (h : 1 + y ≠ 0) (x : ℝ≥0) : x ^ (1 + y) = x * x ^ y := by rw [rpow_add' h, rpow_one] theorem rpow_add_of_nonneg (x : ℝ≥0) {y z : ℝ} (hy : 0 ≤ y) (hz : 0 ≤ z) : x ^ (y + z) = x ^ y * x ^ z := by ext; exact Real.rpow_add_of_nonneg x.2 hy hz /-- Variant of `NNReal.rpow_add'` that avoids having to prove `y + z = w` twice. -/ lemma rpow_of_add_eq (x : ℝ≥0) (hw : w ≠ 0) (h : y + z = w) : x ^ w = x ^ y * x ^ z := by rw [← h, rpow_add']; rwa [h] theorem rpow_mul (x : ℝ≥0) (y z : ℝ) : x ^ (y * z) = (x ^ y) ^ z := NNReal.eq <| Real.rpow_mul x.2 y z lemma rpow_natCast_mul (x : ℝ≥0) (n : ℕ) (z : ℝ) : x ^ (n * z) = (x ^ n) ^ z := by rw [rpow_mul, rpow_natCast] lemma rpow_mul_natCast (x : ℝ≥0) (y : ℝ) (n : ℕ) : x ^ (y * n) = (x ^ y) ^ n := by rw [rpow_mul, rpow_natCast] lemma rpow_intCast_mul (x : ℝ≥0) (n : ℤ) (z : ℝ) : x ^ (n * z) = (x ^ n) ^ z := by rw [rpow_mul, rpow_intCast] lemma rpow_mul_intCast (x : ℝ≥0) (y : ℝ) (n : ℤ) : x ^ (y * n) = (x ^ y) ^ n := by rw [rpow_mul, rpow_intCast] theorem rpow_neg_one (x : ℝ≥0) : x ^ (-1 : ℝ) = x⁻¹ := by simp [rpow_neg] theorem rpow_sub {x : ℝ≥0} (hx : x ≠ 0) (y z : ℝ) : x ^ (y - z) = x ^ y / x ^ z := NNReal.eq <| Real.rpow_sub ((NNReal.coe_pos.trans pos_iff_ne_zero).mpr hx) y z theorem rpow_sub' (h : y - z ≠ 0) (x : ℝ≥0) : x ^ (y - z) = x ^ y / x ^ z := NNReal.eq <| Real.rpow_sub' x.2 h lemma rpow_sub_one' (h : y - 1 ≠ 0) (x : ℝ≥0) : x ^ (y - 1) = x ^ y / x := by rw [rpow_sub' h, rpow_one] lemma rpow_one_sub' (h : 1 - y ≠ 0) (x : ℝ≥0) : x ^ (1 - y) = x / x ^ y := by rw [rpow_sub' h, rpow_one] theorem rpow_inv_rpow_self {y : ℝ} (hy : y ≠ 0) (x : ℝ≥0) : (x ^ y) ^ (1 / y) = x := by field_simp [← rpow_mul] theorem rpow_self_rpow_inv {y : ℝ} (hy : y ≠ 0) (x : ℝ≥0) : (x ^ (1 / y)) ^ y = x := by field_simp [← rpow_mul] theorem inv_rpow (x : ℝ≥0) (y : ℝ) : x⁻¹ ^ y = (x ^ y)⁻¹ := NNReal.eq <| Real.inv_rpow x.2 y theorem div_rpow (x y : ℝ≥0) (z : ℝ) : (x / y) ^ z = x ^ z / y ^ z := NNReal.eq <| Real.div_rpow x.2 y.2 z theorem sqrt_eq_rpow (x : ℝ≥0) : sqrt x = x ^ (1 / (2 : ℝ)) := by refine NNReal.eq ?_ push_cast exact Real.sqrt_eq_rpow x.1 @[simp] lemma rpow_ofNat (x : ℝ≥0) (n : ℕ) [n.AtLeastTwo] : x ^ (ofNat(n) : ℝ) = x ^ (OfNat.ofNat n : ℕ) := rpow_natCast x n theorem rpow_two (x : ℝ≥0) : x ^ (2 : ℝ) = x ^ 2 := rpow_ofNat x 2 theorem mul_rpow {x y : ℝ≥0} {z : ℝ} : (x * y) ^ z = x ^ z * y ^ z := NNReal.eq <| Real.mul_rpow x.2 y.2 /-- `rpow` as a `MonoidHom` -/ @[simps] def rpowMonoidHom (r : ℝ) : ℝ≥0 →* ℝ≥0 where toFun := (· ^ r) map_one' := one_rpow _ map_mul' _x _y := mul_rpow /-- `rpow` variant of `List.prod_map_pow` for `ℝ≥0` -/ theorem list_prod_map_rpow (l : List ℝ≥0) (r : ℝ) : (l.map (· ^ r)).prod = l.prod ^ r := l.prod_hom (rpowMonoidHom r) theorem list_prod_map_rpow' {ι} (l : List ι) (f : ι → ℝ≥0) (r : ℝ) : (l.map (f · ^ r)).prod = (l.map f).prod ^ r := by rw [← list_prod_map_rpow, List.map_map]; rfl /-- `rpow` version of `Multiset.prod_map_pow` for `ℝ≥0`. -/ lemma multiset_prod_map_rpow {ι} (s : Multiset ι) (f : ι → ℝ≥0) (r : ℝ) : (s.map (f · ^ r)).prod = (s.map f).prod ^ r := s.prod_hom' (rpowMonoidHom r) _ /-- `rpow` version of `Finset.prod_pow` for `ℝ≥0`. -/ lemma finset_prod_rpow {ι} (s : Finset ι) (f : ι → ℝ≥0) (r : ℝ) : (∏ i ∈ s, f i ^ r) = (∏ i ∈ s, f i) ^ r := multiset_prod_map_rpow _ _ _ -- note: these don't really belong here, but they're much easier to prove in terms of the above section Real /-- `rpow` version of `List.prod_map_pow` for `Real`. -/ theorem _root_.Real.list_prod_map_rpow (l : List ℝ) (hl : ∀ x ∈ l, (0 : ℝ) ≤ x) (r : ℝ) : (l.map (· ^ r)).prod = l.prod ^ r := by lift l to List ℝ≥0 using hl have := congr_arg ((↑) : ℝ≥0 → ℝ) (NNReal.list_prod_map_rpow l r) push_cast at this rw [List.map_map] at this ⊢ exact mod_cast this theorem _root_.Real.list_prod_map_rpow' {ι} (l : List ι) (f : ι → ℝ) (hl : ∀ i ∈ l, (0 : ℝ) ≤ f i) (r : ℝ) : (l.map (f · ^ r)).prod = (l.map f).prod ^ r := by rw [← Real.list_prod_map_rpow (l.map f) _ r, List.map_map] · rfl simpa using hl /-- `rpow` version of `Multiset.prod_map_pow`. -/ theorem _root_.Real.multiset_prod_map_rpow {ι} (s : Multiset ι) (f : ι → ℝ) (hs : ∀ i ∈ s, (0 : ℝ) ≤ f i) (r : ℝ) : (s.map (f · ^ r)).prod = (s.map f).prod ^ r := by induction' s using Quotient.inductionOn with l simpa using Real.list_prod_map_rpow' l f hs r /-- `rpow` version of `Finset.prod_pow`. -/ theorem _root_.Real.finset_prod_rpow {ι} (s : Finset ι) (f : ι → ℝ) (hs : ∀ i ∈ s, 0 ≤ f i) (r : ℝ) : (∏ i ∈ s, f i ^ r) = (∏ i ∈ s, f i) ^ r := Real.multiset_prod_map_rpow s.val f hs r end Real @[gcongr] theorem rpow_le_rpow {x y : ℝ≥0} {z : ℝ} (h₁ : x ≤ y) (h₂ : 0 ≤ z) : x ^ z ≤ y ^ z := Real.rpow_le_rpow x.2 h₁ h₂ @[gcongr] theorem rpow_lt_rpow {x y : ℝ≥0} {z : ℝ} (h₁ : x < y) (h₂ : 0 < z) : x ^ z < y ^ z := Real.rpow_lt_rpow x.2 h₁ h₂ theorem rpow_lt_rpow_iff {x y : ℝ≥0} {z : ℝ} (hz : 0 < z) : x ^ z < y ^ z ↔ x < y := Real.rpow_lt_rpow_iff x.2 y.2 hz theorem rpow_le_rpow_iff {x y : ℝ≥0} {z : ℝ} (hz : 0 < z) : x ^ z ≤ y ^ z ↔ x ≤ y := Real.rpow_le_rpow_iff x.2 y.2 hz theorem le_rpow_inv_iff {x y : ℝ≥0} {z : ℝ} (hz : 0 < z) : x ≤ y ^ z⁻¹ ↔ x ^ z ≤ y := by rw [← rpow_le_rpow_iff hz, ← one_div, rpow_self_rpow_inv hz.ne'] theorem rpow_inv_le_iff {x y : ℝ≥0} {z : ℝ} (hz : 0 < z) : x ^ z⁻¹ ≤ y ↔ x ≤ y ^ z := by rw [← rpow_le_rpow_iff hz, ← one_div, rpow_self_rpow_inv hz.ne'] theorem lt_rpow_inv_iff {x y : ℝ≥0} {z : ℝ} (hz : 0 < z) : x < y ^ z⁻¹ ↔ x ^z < y := by simp only [← not_le, rpow_inv_le_iff hz] theorem rpow_inv_lt_iff {x y : ℝ≥0} {z : ℝ} (hz : 0 < z) : x ^ z⁻¹ < y ↔ x < y ^ z := by simp only [← not_le, le_rpow_inv_iff hz] section variable {y : ℝ≥0} lemma rpow_lt_rpow_of_neg (hx : 0 < x) (hxy : x < y) (hz : z < 0) : y ^ z < x ^ z := Real.rpow_lt_rpow_of_neg hx hxy hz lemma rpow_le_rpow_of_nonpos (hx : 0 < x) (hxy : x ≤ y) (hz : z ≤ 0) : y ^ z ≤ x ^ z := Real.rpow_le_rpow_of_nonpos hx hxy hz lemma rpow_lt_rpow_iff_of_neg (hx : 0 < x) (hy : 0 < y) (hz : z < 0) : x ^ z < y ^ z ↔ y < x := Real.rpow_lt_rpow_iff_of_neg hx hy hz lemma rpow_le_rpow_iff_of_neg (hx : 0 < x) (hy : 0 < y) (hz : z < 0) : x ^ z ≤ y ^ z ↔ y ≤ x := Real.rpow_le_rpow_iff_of_neg hx hy hz lemma le_rpow_inv_iff_of_pos (hy : 0 ≤ y) (hz : 0 < z) (x : ℝ≥0) : x ≤ y ^ z⁻¹ ↔ x ^ z ≤ y := Real.le_rpow_inv_iff_of_pos x.2 hy hz lemma rpow_inv_le_iff_of_pos (hy : 0 ≤ y) (hz : 0 < z) (x : ℝ≥0) : x ^ z⁻¹ ≤ y ↔ x ≤ y ^ z := Real.rpow_inv_le_iff_of_pos x.2 hy hz lemma lt_rpow_inv_iff_of_pos (hy : 0 ≤ y) (hz : 0 < z) (x : ℝ≥0) : x < y ^ z⁻¹ ↔ x ^ z < y := Real.lt_rpow_inv_iff_of_pos x.2 hy hz lemma rpow_inv_lt_iff_of_pos (hy : 0 ≤ y) (hz : 0 < z) (x : ℝ≥0) : x ^ z⁻¹ < y ↔ x < y ^ z := Real.rpow_inv_lt_iff_of_pos x.2 hy hz lemma le_rpow_inv_iff_of_neg (hx : 0 < x) (hy : 0 < y) (hz : z < 0) : x ≤ y ^ z⁻¹ ↔ y ≤ x ^ z := Real.le_rpow_inv_iff_of_neg hx hy hz lemma lt_rpow_inv_iff_of_neg (hx : 0 < x) (hy : 0 < y) (hz : z < 0) : x < y ^ z⁻¹ ↔ y < x ^ z := Real.lt_rpow_inv_iff_of_neg hx hy hz lemma rpow_inv_lt_iff_of_neg (hx : 0 < x) (hy : 0 < y) (hz : z < 0) : x ^ z⁻¹ < y ↔ y ^ z < x := Real.rpow_inv_lt_iff_of_neg hx hy hz lemma rpow_inv_le_iff_of_neg (hx : 0 < x) (hy : 0 < y) (hz : z < 0) : x ^ z⁻¹ ≤ y ↔ y ^ z ≤ x := Real.rpow_inv_le_iff_of_neg hx hy hz end @[gcongr] theorem rpow_lt_rpow_of_exponent_lt {x : ℝ≥0} {y z : ℝ} (hx : 1 < x) (hyz : y < z) : x ^ y < x ^ z := Real.rpow_lt_rpow_of_exponent_lt hx hyz @[gcongr] theorem rpow_le_rpow_of_exponent_le {x : ℝ≥0} {y z : ℝ} (hx : 1 ≤ x) (hyz : y ≤ z) : x ^ y ≤ x ^ z := Real.rpow_le_rpow_of_exponent_le hx hyz theorem rpow_lt_rpow_of_exponent_gt {x : ℝ≥0} {y z : ℝ} (hx0 : 0 < x) (hx1 : x < 1) (hyz : z < y) : x ^ y < x ^ z := Real.rpow_lt_rpow_of_exponent_gt hx0 hx1 hyz theorem rpow_le_rpow_of_exponent_ge {x : ℝ≥0} {y z : ℝ} (hx0 : 0 < x) (hx1 : x ≤ 1) (hyz : z ≤ y) : x ^ y ≤ x ^ z := Real.rpow_le_rpow_of_exponent_ge hx0 hx1 hyz theorem rpow_pos {p : ℝ} {x : ℝ≥0} (hx_pos : 0 < x) : 0 < x ^ p := by have rpow_pos_of_nonneg : ∀ {p : ℝ}, 0 < p → 0 < x ^ p := by intro p hp_pos rw [← zero_rpow hp_pos.ne'] exact rpow_lt_rpow hx_pos hp_pos rcases lt_trichotomy (0 : ℝ) p with (hp_pos | rfl | hp_neg) · exact rpow_pos_of_nonneg hp_pos · simp only [zero_lt_one, rpow_zero] · rw [← neg_neg p, rpow_neg, inv_pos] exact rpow_pos_of_nonneg (neg_pos.mpr hp_neg) theorem rpow_lt_one {x : ℝ≥0} {z : ℝ} (hx1 : x < 1) (hz : 0 < z) : x ^ z < 1 := Real.rpow_lt_one (coe_nonneg x) hx1 hz theorem rpow_le_one {x : ℝ≥0} {z : ℝ} (hx2 : x ≤ 1) (hz : 0 ≤ z) : x ^ z ≤ 1 := Real.rpow_le_one x.2 hx2 hz theorem rpow_lt_one_of_one_lt_of_neg {x : ℝ≥0} {z : ℝ} (hx : 1 < x) (hz : z < 0) : x ^ z < 1 := Real.rpow_lt_one_of_one_lt_of_neg hx hz theorem rpow_le_one_of_one_le_of_nonpos {x : ℝ≥0} {z : ℝ} (hx : 1 ≤ x) (hz : z ≤ 0) : x ^ z ≤ 1 := Real.rpow_le_one_of_one_le_of_nonpos hx hz theorem one_lt_rpow {x : ℝ≥0} {z : ℝ} (hx : 1 < x) (hz : 0 < z) : 1 < x ^ z := Real.one_lt_rpow hx hz theorem one_le_rpow {x : ℝ≥0} {z : ℝ} (h : 1 ≤ x) (h₁ : 0 ≤ z) : 1 ≤ x ^ z := Real.one_le_rpow h h₁ theorem one_lt_rpow_of_pos_of_lt_one_of_neg {x : ℝ≥0} {z : ℝ} (hx1 : 0 < x) (hx2 : x < 1) (hz : z < 0) : 1 < x ^ z := Real.one_lt_rpow_of_pos_of_lt_one_of_neg hx1 hx2 hz theorem one_le_rpow_of_pos_of_le_one_of_nonpos {x : ℝ≥0} {z : ℝ} (hx1 : 0 < x) (hx2 : x ≤ 1) (hz : z ≤ 0) : 1 ≤ x ^ z := Real.one_le_rpow_of_pos_of_le_one_of_nonpos hx1 hx2 hz theorem rpow_le_self_of_le_one {x : ℝ≥0} {z : ℝ} (hx : x ≤ 1) (h_one_le : 1 ≤ z) : x ^ z ≤ x := by rcases eq_bot_or_bot_lt x with (rfl | (h : 0 < x)) · have : z ≠ 0 := by linarith simp [this] nth_rw 2 [← NNReal.rpow_one x] exact NNReal.rpow_le_rpow_of_exponent_ge h hx h_one_le theorem rpow_left_injective {x : ℝ} (hx : x ≠ 0) : Function.Injective fun y : ℝ≥0 => y ^ x := fun y z hyz => by simpa only [rpow_inv_rpow_self hx] using congr_arg (fun y => y ^ (1 / x)) hyz theorem rpow_eq_rpow_iff {x y : ℝ≥0} {z : ℝ} (hz : z ≠ 0) : x ^ z = y ^ z ↔ x = y := (rpow_left_injective hz).eq_iff theorem rpow_left_surjective {x : ℝ} (hx : x ≠ 0) : Function.Surjective fun y : ℝ≥0 => y ^ x := fun y => ⟨y ^ x⁻¹, by simp_rw [← rpow_mul, inv_mul_cancel₀ hx, rpow_one]⟩ theorem rpow_left_bijective {x : ℝ} (hx : x ≠ 0) : Function.Bijective fun y : ℝ≥0 => y ^ x := ⟨rpow_left_injective hx, rpow_left_surjective hx⟩ theorem eq_rpow_inv_iff {x y : ℝ≥0} {z : ℝ} (hz : z ≠ 0) : x = y ^ z⁻¹ ↔ x ^ z = y := by rw [← rpow_eq_rpow_iff hz, ← one_div, rpow_self_rpow_inv hz] theorem rpow_inv_eq_iff {x y : ℝ≥0} {z : ℝ} (hz : z ≠ 0) : x ^ z⁻¹ = y ↔ x = y ^ z := by rw [← rpow_eq_rpow_iff hz, ← one_div, rpow_self_rpow_inv hz] @[simp] lemma rpow_rpow_inv {y : ℝ} (hy : y ≠ 0) (x : ℝ≥0) : (x ^ y) ^ y⁻¹ = x := by rw [← rpow_mul, mul_inv_cancel₀ hy, rpow_one] @[simp] lemma rpow_inv_rpow {y : ℝ} (hy : y ≠ 0) (x : ℝ≥0) : (x ^ y⁻¹) ^ y = x := by rw [← rpow_mul, inv_mul_cancel₀ hy, rpow_one] theorem pow_rpow_inv_natCast (x : ℝ≥0) {n : ℕ} (hn : n ≠ 0) : (x ^ n) ^ (n⁻¹ : ℝ) = x := by rw [← NNReal.coe_inj, coe_rpow, NNReal.coe_pow] exact Real.pow_rpow_inv_natCast x.2 hn theorem rpow_inv_natCast_pow (x : ℝ≥0) {n : ℕ} (hn : n ≠ 0) : (x ^ (n⁻¹ : ℝ)) ^ n = x := by rw [← NNReal.coe_inj, NNReal.coe_pow, coe_rpow] exact Real.rpow_inv_natCast_pow x.2 hn theorem _root_.Real.toNNReal_rpow_of_nonneg {x y : ℝ} (hx : 0 ≤ x) : Real.toNNReal (x ^ y) = Real.toNNReal x ^ y := by nth_rw 1 [← Real.coe_toNNReal x hx] rw [← NNReal.coe_rpow, Real.toNNReal_coe] theorem strictMono_rpow_of_pos {z : ℝ} (h : 0 < z) : StrictMono fun x : ℝ≥0 => x ^ z := fun x y hxy => by simp only [NNReal.rpow_lt_rpow hxy h, coe_lt_coe] theorem monotone_rpow_of_nonneg {z : ℝ} (h : 0 ≤ z) : Monotone fun x : ℝ≥0 => x ^ z := h.eq_or_lt.elim (fun h0 => h0 ▸ by simp only [rpow_zero, monotone_const]) fun h0 => (strictMono_rpow_of_pos h0).monotone /-- Bundles `fun x : ℝ≥0 => x ^ y` into an order isomorphism when `y : ℝ` is positive, where the inverse is `fun x : ℝ≥0 => x ^ (1 / y)`. -/ @[simps! apply] def orderIsoRpow (y : ℝ) (hy : 0 < y) : ℝ≥0 ≃o ℝ≥0 := (strictMono_rpow_of_pos hy).orderIsoOfRightInverse (fun x => x ^ y) (fun x => x ^ (1 / y)) fun x => by dsimp rw [← rpow_mul, one_div_mul_cancel hy.ne.symm, rpow_one] theorem orderIsoRpow_symm_eq (y : ℝ) (hy : 0 < y) : (orderIsoRpow y hy).symm = orderIsoRpow (1 / y) (one_div_pos.2 hy) := by simp only [orderIsoRpow, one_div_one_div]; rfl theorem _root_.Real.nnnorm_rpow_of_nonneg {x y : ℝ} (hx : 0 ≤ x) : ‖x ^ y‖₊ = ‖x‖₊ ^ y := by ext; exact Real.norm_rpow_of_nonneg hx end NNReal namespace ENNReal /-- The real power function `x^y` on extended nonnegative reals, defined for `x : ℝ≥0∞` and `y : ℝ` as the restriction of the real power function if `0 < x < ⊤`, and with the natural values for `0` and `⊤` (i.e., `0 ^ x = 0` for `x > 0`, `1` for `x = 0` and `⊤` for `x < 0`, and `⊤ ^ x = 1 / 0 ^ x`). -/ noncomputable def rpow : ℝ≥0∞ → ℝ → ℝ≥0∞ | some x, y => if x = 0 ∧ y < 0 then ⊤ else (x ^ y : ℝ≥0) | none, y => if 0 < y then ⊤ else if y = 0 then 1 else 0 noncomputable instance : Pow ℝ≥0∞ ℝ := ⟨rpow⟩ @[simp] theorem rpow_eq_pow (x : ℝ≥0∞) (y : ℝ) : rpow x y = x ^ y := rfl @[simp] theorem rpow_zero {x : ℝ≥0∞} : x ^ (0 : ℝ) = 1 := by cases x <;> · dsimp only [(· ^ ·), Pow.pow, rpow] simp [lt_irrefl] theorem top_rpow_def (y : ℝ) : (⊤ : ℝ≥0∞) ^ y = if 0 < y then ⊤ else if y = 0 then 1 else 0 := rfl @[simp] theorem top_rpow_of_pos {y : ℝ} (h : 0 < y) : (⊤ : ℝ≥0∞) ^ y = ⊤ := by simp [top_rpow_def, h] @[simp] theorem top_rpow_of_neg {y : ℝ} (h : y < 0) : (⊤ : ℝ≥0∞) ^ y = 0 := by simp [top_rpow_def, asymm h, ne_of_lt h] @[simp] theorem zero_rpow_of_pos {y : ℝ} (h : 0 < y) : (0 : ℝ≥0∞) ^ y = 0 := by rw [← ENNReal.coe_zero, ← ENNReal.some_eq_coe] dsimp only [(· ^ ·), rpow, Pow.pow] simp [h, asymm h, ne_of_gt h] @[simp] theorem zero_rpow_of_neg {y : ℝ} (h : y < 0) : (0 : ℝ≥0∞) ^ y = ⊤ := by rw [← ENNReal.coe_zero, ← ENNReal.some_eq_coe] dsimp only [(· ^ ·), rpow, Pow.pow] simp [h, ne_of_gt h] theorem zero_rpow_def (y : ℝ) : (0 : ℝ≥0∞) ^ y = if 0 < y then 0 else if y = 0 then 1 else ⊤ := by rcases lt_trichotomy (0 : ℝ) y with (H | rfl | H) · simp [H, ne_of_gt, zero_rpow_of_pos, lt_irrefl] · simp [lt_irrefl] · simp [H, asymm H, ne_of_lt, zero_rpow_of_neg] @[simp] theorem zero_rpow_mul_self (y : ℝ) : (0 : ℝ≥0∞) ^ y * (0 : ℝ≥0∞) ^ y = (0 : ℝ≥0∞) ^ y := by rw [zero_rpow_def] split_ifs exacts [zero_mul _, one_mul _, top_mul_top] @[norm_cast] theorem coe_rpow_of_ne_zero {x : ℝ≥0} (h : x ≠ 0) (y : ℝ) : (↑(x ^ y) : ℝ≥0∞) = x ^ y := by rw [← ENNReal.some_eq_coe] dsimp only [(· ^ ·), Pow.pow, rpow] simp [h] @[norm_cast] theorem coe_rpow_of_nonneg (x : ℝ≥0) {y : ℝ} (h : 0 ≤ y) : ↑(x ^ y) = (x : ℝ≥0∞) ^ y := by by_cases hx : x = 0 · rcases le_iff_eq_or_lt.1 h with (H | H) · simp [hx, H.symm] · simp [hx, zero_rpow_of_pos H, NNReal.zero_rpow (ne_of_gt H)] · exact coe_rpow_of_ne_zero hx _ theorem coe_rpow_def (x : ℝ≥0) (y : ℝ) : (x : ℝ≥0∞) ^ y = if x = 0 ∧ y < 0 then ⊤ else ↑(x ^ y) := rfl theorem rpow_ofNNReal {M : ℝ≥0} {P : ℝ} (hP : 0 ≤ P) : (M : ℝ≥0∞) ^ P = ↑(M ^ P) := by rw [ENNReal.coe_rpow_of_nonneg _ hP, ← ENNReal.rpow_eq_pow] @[simp] theorem rpow_one (x : ℝ≥0∞) : x ^ (1 : ℝ) = x := by cases x · exact dif_pos zero_lt_one · change ite _ _ _ = _ simp only [NNReal.rpow_one, some_eq_coe, ite_eq_right_iff, top_ne_coe, and_imp] exact fun _ => zero_le_one.not_lt @[simp] theorem one_rpow (x : ℝ) : (1 : ℝ≥0∞) ^ x = 1 := by rw [← coe_one, ← coe_rpow_of_ne_zero one_ne_zero] simp @[simp] theorem rpow_eq_zero_iff {x : ℝ≥0∞} {y : ℝ} : x ^ y = 0 ↔ x = 0 ∧ 0 < y ∨ x = ⊤ ∧ y < 0 := by cases x with | top => rcases lt_trichotomy y 0 with (H | H | H) <;> simp [H, top_rpow_of_neg, top_rpow_of_pos, le_of_lt] | coe x => by_cases h : x = 0 · rcases lt_trichotomy y 0 with (H | H | H) <;> simp [h, H, zero_rpow_of_neg, zero_rpow_of_pos, le_of_lt] · simp [← coe_rpow_of_ne_zero h, h] lemma rpow_eq_zero_iff_of_pos {x : ℝ≥0∞} {y : ℝ} (hy : 0 < y) : x ^ y = 0 ↔ x = 0 := by simp [hy, hy.not_lt] @[simp] theorem rpow_eq_top_iff {x : ℝ≥0∞} {y : ℝ} : x ^ y = ⊤ ↔ x = 0 ∧ y < 0 ∨ x = ⊤ ∧ 0 < y := by cases x with | top => rcases lt_trichotomy y 0 with (H | H | H) <;> simp [H, top_rpow_of_neg, top_rpow_of_pos, le_of_lt] | coe x => by_cases h : x = 0 · rcases lt_trichotomy y 0 with (H | H | H) <;> simp [h, H, zero_rpow_of_neg, zero_rpow_of_pos, le_of_lt] · simp [← coe_rpow_of_ne_zero h, h] theorem rpow_eq_top_iff_of_pos {x : ℝ≥0∞} {y : ℝ} (hy : 0 < y) : x ^ y = ⊤ ↔ x = ⊤ := by simp [rpow_eq_top_iff, hy, asymm hy] lemma rpow_lt_top_iff_of_pos {x : ℝ≥0∞} {y : ℝ} (hy : 0 < y) : x ^ y < ∞ ↔ x < ∞ := by simp only [lt_top_iff_ne_top, Ne, rpow_eq_top_iff_of_pos hy] theorem rpow_eq_top_of_nonneg (x : ℝ≥0∞) {y : ℝ} (hy0 : 0 ≤ y) : x ^ y = ⊤ → x = ⊤ := by rw [ENNReal.rpow_eq_top_iff] rintro (h|h) · exfalso rw [lt_iff_not_ge] at h exact h.right hy0 · exact h.left theorem rpow_ne_top_of_nonneg {x : ℝ≥0∞} {y : ℝ} (hy0 : 0 ≤ y) (h : x ≠ ⊤) : x ^ y ≠ ⊤ := mt (ENNReal.rpow_eq_top_of_nonneg x hy0) h theorem rpow_lt_top_of_nonneg {x : ℝ≥0∞} {y : ℝ} (hy0 : 0 ≤ y) (h : x ≠ ⊤) : x ^ y < ⊤ := lt_top_iff_ne_top.mpr (ENNReal.rpow_ne_top_of_nonneg hy0 h) theorem rpow_add {x : ℝ≥0∞} (y z : ℝ) (hx : x ≠ 0) (h'x : x ≠ ⊤) : x ^ (y + z) = x ^ y * x ^ z := by cases x with | top => exact (h'x rfl).elim | coe x => have : x ≠ 0 := fun h => by simp [h] at hx simp [← coe_rpow_of_ne_zero this, NNReal.rpow_add this] theorem rpow_add_of_nonneg {x : ℝ≥0∞} (y z : ℝ) (hy : 0 ≤ y) (hz : 0 ≤ z) : x ^ (y + z) = x ^ y * x ^ z := by induction x using recTopCoe · rcases hy.eq_or_lt with rfl|hy · rw [rpow_zero, one_mul, zero_add] rcases hz.eq_or_lt with rfl|hz · rw [rpow_zero, mul_one, add_zero] simp [top_rpow_of_pos, hy, hz, add_pos hy hz] simp [← coe_rpow_of_nonneg, hy, hz, add_nonneg hy hz, NNReal.rpow_add_of_nonneg _ hy hz] theorem rpow_neg (x : ℝ≥0∞) (y : ℝ) : x ^ (-y) = (x ^ y)⁻¹ := by cases x with | top => rcases lt_trichotomy y 0 with (H | H | H) <;> simp [top_rpow_of_pos, top_rpow_of_neg, H, neg_pos.mpr] | coe x => by_cases h : x = 0 · rcases lt_trichotomy y 0 with (H | H | H) <;> simp [h, zero_rpow_of_pos, zero_rpow_of_neg, H, neg_pos.mpr] · have A : x ^ y ≠ 0 := by simp [h] simp [← coe_rpow_of_ne_zero h, ← coe_inv A, NNReal.rpow_neg] theorem rpow_sub {x : ℝ≥0∞} (y z : ℝ) (hx : x ≠ 0) (h'x : x ≠ ⊤) : x ^ (y - z) = x ^ y / x ^ z := by rw [sub_eq_add_neg, rpow_add _ _ hx h'x, rpow_neg, div_eq_mul_inv]
theorem rpow_neg_one (x : ℝ≥0∞) : x ^ (-1 : ℝ) = x⁻¹ := by simp [rpow_neg] theorem rpow_mul (x : ℝ≥0∞) (y z : ℝ) : x ^ (y * z) = (x ^ y) ^ z := by
Mathlib/Analysis/SpecialFunctions/Pow/NNReal.lean
601
604
/- Copyright (c) 2019 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Kenny Lau -/ import Mathlib.Algebra.Polynomial.Coeff import Mathlib.Algebra.Polynomial.Degree.Lemmas import Mathlib.RingTheory.PowerSeries.Basic /-! # Formal power series in one variable - Truncation `PowerSeries.trunc n φ` truncates a (univariate) formal power series to the polynomial that has the same coefficients as `φ`, for all `m < n`, and `0` otherwise. -/ noncomputable section open Polynomial open Finset (antidiagonal mem_antidiagonal) namespace PowerSeries open Finsupp (single) variable {R : Type*} section Trunc variable [Semiring R] open Finset Nat /-- The `n`th truncation of a formal power series to a polynomial -/ def trunc (n : ℕ) (φ : R⟦X⟧) : R[X] := ∑ m ∈ Ico 0 n, Polynomial.monomial m (coeff R m φ) theorem coeff_trunc (m) (n) (φ : R⟦X⟧) : (trunc n φ).coeff m = if m < n then coeff R m φ else 0 := by simp [trunc, Polynomial.coeff_sum, Polynomial.coeff_monomial, Nat.lt_succ_iff] @[simp] theorem trunc_zero (n) : trunc n (0 : R⟦X⟧) = 0 := Polynomial.ext fun m => by rw [coeff_trunc, LinearMap.map_zero, Polynomial.coeff_zero] split_ifs <;> rfl @[simp] theorem trunc_one (n) : trunc (n + 1) (1 : R⟦X⟧) = 1 := Polynomial.ext fun m => by rw [coeff_trunc, coeff_one, Polynomial.coeff_one] split_ifs with h _ h' · rfl · rfl · subst h'; simp at h · rfl @[simp] theorem trunc_C (n) (a : R) : trunc (n + 1) (C R a) = Polynomial.C a := Polynomial.ext fun m => by rw [coeff_trunc, coeff_C, Polynomial.coeff_C] split_ifs with H <;> first |rfl|try simp_all @[simp] theorem trunc_add (n) (φ ψ : R⟦X⟧) : trunc n (φ + ψ) = trunc n φ + trunc n ψ := Polynomial.ext fun m => by simp only [coeff_trunc, AddMonoidHom.map_add, Polynomial.coeff_add] split_ifs with H · rfl · rw [zero_add] theorem trunc_succ (f : R⟦X⟧) (n : ℕ) : trunc n.succ f = trunc n f + Polynomial.monomial n (coeff R n f) := by rw [trunc, Ico_zero_eq_range, sum_range_succ, trunc, Ico_zero_eq_range] theorem natDegree_trunc_lt (f : R⟦X⟧) (n) : (trunc (n + 1) f).natDegree < n + 1 := by rw [Nat.lt_succ_iff, natDegree_le_iff_coeff_eq_zero] intros rw [coeff_trunc] split_ifs with h · rw [lt_succ, ← not_lt] at h contradiction · rfl @[simp] lemma trunc_zero' {f : R⟦X⟧} : trunc 0 f = 0 := rfl theorem degree_trunc_lt (f : R⟦X⟧) (n) : (trunc n f).degree < n := by rw [degree_lt_iff_coeff_zero] intros rw [coeff_trunc] split_ifs with h · rw [← not_le] at h contradiction · rfl theorem eval₂_trunc_eq_sum_range {S : Type*} [Semiring S] (s : S) (G : R →+* S) (n) (f : R⟦X⟧) : (trunc n f).eval₂ G s = ∑ i ∈ range n, G (coeff R i f) * s ^ i := by cases n with | zero => rw [trunc_zero', range_zero, sum_empty, eval₂_zero] | succ n => have := natDegree_trunc_lt f n rw [eval₂_eq_sum_range' (hn := this)] apply sum_congr rfl intro _ h rw [mem_range] at h congr rw [coeff_trunc, if_pos h] @[simp] theorem trunc_X (n) : trunc (n + 2) X = (Polynomial.X : R[X]) := by ext d rw [coeff_trunc, coeff_X] split_ifs with h₁ h₂ · rw [h₂, coeff_X_one] · rw [coeff_X_of_ne_one h₂] · rw [coeff_X_of_ne_one] intro hd apply h₁ rw [hd] exact n.one_lt_succ_succ lemma trunc_X_of {n : ℕ} (hn : 2 ≤ n) : trunc n X = (Polynomial.X : R[X]) := by cases n with | zero => contradiction | succ n => cases n with | zero => contradiction | succ n => exact trunc_X n @[simp] lemma trunc_one_left (p : R⟦X⟧) : trunc (R := R) 1 p = .C (coeff R 0 p) := by ext i; simp +contextual [coeff_trunc, Polynomial.coeff_C] lemma trunc_one_X : trunc (R := R) 1 X = 0 := by simp @[simp] lemma trunc_C_mul (n : ℕ) (r : R) (f : R⟦X⟧) : trunc n (C R r * f) = .C r * trunc n f := by ext i; simp [coeff_trunc] @[simp] lemma trunc_mul_C (n : ℕ) (f : R⟦X⟧) (r : R) : trunc n (f * C R r) = trunc n f * .C r := by ext i; simp [coeff_trunc] end Trunc section Trunc /- Lemmas in this section involve the coercion `R[X] → R⟦X⟧`, so they may only be stated in the case `R` is commutative. This is because the coercion is an `R`-algebra map. -/ variable {R : Type*} [CommSemiring R] open Nat hiding pow_succ pow_zero open Polynomial Finset Finset.Nat theorem trunc_trunc_of_le {n m} (f : R⟦X⟧) (hnm : n ≤ m := by rfl) : trunc n ↑(trunc m f) = trunc n f := by ext d rw [coeff_trunc, coeff_trunc, coeff_coe] split_ifs with h · rw [coeff_trunc, if_pos <| lt_of_lt_of_le h hnm] · rfl @[simp] theorem trunc_trunc {n} (f : R⟦X⟧) : trunc n ↑(trunc n f) = trunc n f := trunc_trunc_of_le f @[simp] theorem trunc_trunc_mul {n} (f g : R⟦X⟧) : trunc n ((trunc n f) * g : R⟦X⟧) = trunc n (f * g) := by ext m rw [coeff_trunc, coeff_trunc] split_ifs with h · rw [coeff_mul, coeff_mul, sum_congr rfl] intro _ hab have ha := lt_of_le_of_lt (antidiagonal.fst_le hab) h rw [coeff_coe, coeff_trunc, if_pos ha] · rfl @[simp] theorem trunc_mul_trunc {n} (f g : R⟦X⟧) : trunc n (f * (trunc n g) : R⟦X⟧) = trunc n (f * g) := by rw [mul_comm, trunc_trunc_mul, mul_comm] theorem trunc_trunc_mul_trunc {n} (f g : R⟦X⟧) : trunc n (trunc n f * trunc n g : R⟦X⟧) = trunc n (f * g) := by rw [trunc_trunc_mul, trunc_mul_trunc] @[simp] theorem trunc_trunc_pow (f : R⟦X⟧) (n a : ℕ) : trunc n ((trunc n f : R⟦X⟧) ^ a) = trunc n (f ^ a) := by induction a with | zero => rw [pow_zero, pow_zero] | succ a ih => rw [_root_.pow_succ', _root_.pow_succ', trunc_trunc_mul, ← trunc_trunc_mul_trunc, ih, trunc_trunc_mul_trunc] theorem trunc_coe_eq_self {n} {f : R[X]} (hn : natDegree f < n) : trunc n (f : R⟦X⟧) = f := by rw [← Polynomial.coe_inj] ext m rw [coeff_coe, coeff_trunc] split case isTrue h => rfl case isFalse h => rw [not_lt] at h rw [coeff_coe]; symm exact coeff_eq_zero_of_natDegree_lt <| lt_of_lt_of_le hn h /-- The function `coeff n : R⟦X⟧ → R` is continuous. I.e. `coeff n f` depends only on a sufficiently long truncation of the power series `f`. -/ theorem coeff_coe_trunc_of_lt {n m} {f : R⟦X⟧} (h : n < m) : coeff R n (trunc m f) = coeff R n f := by
rwa [coeff_coe, coeff_trunc, if_pos] /-- The `n`-th coefficient of `f*g` may be calculated from the truncations of `f` and `g`. -/ theorem coeff_mul_eq_coeff_trunc_mul_trunc₂ {n a b} (f g) (ha : n < a) (hb : n < b) :
Mathlib/RingTheory/PowerSeries/Trunc.lean
212
216
/- Copyright (c) 2020 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import Mathlib.Algebra.BigOperators.Fin import Mathlib.Algebra.Order.BigOperators.Group.Finset import Mathlib.Data.Finset.Sort /-! # Compositions A composition of a natural number `n` is a decomposition `n = i₀ + ... + i_{k-1}` of `n` into a sum of positive integers. Combinatorially, it corresponds to a decomposition of `{0, ..., n-1}` into non-empty blocks of consecutive integers, where the `iⱼ` are the lengths of the blocks. This notion is closely related to that of a partition of `n`, but in a composition of `n` the order of the `iⱼ`s matters. We implement two different structures covering these two viewpoints on compositions. The first one, made of a list of positive integers summing to `n`, is the main one and is called `Composition n`. The second one is useful for combinatorial arguments (for instance to show that the number of compositions of `n` is `2^(n-1)`). It is given by a subset of `{0, ..., n}` containing `0` and `n`, where the elements of the subset (other than `n`) correspond to the leftmost points of each block. The main API is built on `Composition n`, and we provide an equivalence between the two types. ## Main functions * `c : Composition n` is a structure, made of a list of integers which are all positive and add up to `n`. * `composition_card` states that the cardinality of `Composition n` is exactly `2^(n-1)`, which is proved by constructing an equiv with `CompositionAsSet n` (see below), which is itself in bijection with the subsets of `Fin (n-1)` (this holds even for `n = 0`, where `-` is nat subtraction). Let `c : Composition n` be a composition of `n`. Then * `c.blocks` is the list of blocks in `c`. * `c.length` is the number of blocks in the composition. * `c.blocksFun : Fin c.length → ℕ` is the realization of `c.blocks` as a function on `Fin c.length`. This is the main object when using compositions to understand the composition of analytic functions. * `c.sizeUpTo : ℕ → ℕ` is the sum of the size of the blocks up to `i`.; * `c.embedding i : Fin (c.blocksFun i) → Fin n` is the increasing embedding of the `i`-th block in `Fin n`; * `c.index j`, for `j : Fin n`, is the index of the block containing `j`. * `Composition.ones n` is the composition of `n` made of ones, i.e., `[1, ..., 1]`. * `Composition.single n (hn : 0 < n)` is the composition of `n` made of a single block of size `n`. Compositions can also be used to split lists. Let `l` be a list of length `n` and `c` a composition of `n`. * `l.splitWrtComposition c` is a list of lists, made of the slices of `l` corresponding to the blocks of `c`. * `join_splitWrtComposition` states that splitting a list and then joining it gives back the original list. * `splitWrtComposition_join` states that joining a list of lists, and then splitting it back according to the right composition, gives back the original list of lists. We turn to the second viewpoint on compositions, that we realize as a finset of `Fin (n+1)`. `c : CompositionAsSet n` is a structure made of a finset of `Fin (n+1)` called `c.boundaries` and proofs that it contains `0` and `n`. (Taking a finset of `Fin n` containing `0` would not make sense in the edge case `n = 0`, while the previous description works in all cases). The elements of this set (other than `n`) correspond to leftmost points of blocks. Thus, there is an equiv between `Composition n` and `CompositionAsSet n`. We only construct basic API on `CompositionAsSet` (notably `c.length` and `c.blocks`) to be able to construct this equiv, called `compositionEquiv n`. Since there is a straightforward equiv between `CompositionAsSet n` and finsets of `{1, ..., n-1}` (obtained by removing `0` and `n` from a `CompositionAsSet` and called `compositionAsSetEquiv n`), we deduce that `CompositionAsSet n` and `Composition n` are both fintypes of cardinality `2^(n - 1)` (see `compositionAsSet_card` and `composition_card`). ## Implementation details The main motivation for this structure and its API is in the construction of the composition of formal multilinear series, and the proof that the composition of analytic functions is analytic. The representation of a composition as a list is very handy as lists are very flexible and already have a well-developed API. ## Tags Composition, partition ## References <https://en.wikipedia.org/wiki/Composition_(combinatorics)> -/ assert_not_exists Field open List variable {n : ℕ} /-- A composition of `n` is a list of positive integers summing to `n`. -/ @[ext] structure Composition (n : ℕ) where /-- List of positive integers summing to `n` -/ blocks : List ℕ /-- Proof of positivity for `blocks` -/ blocks_pos : ∀ {i}, i ∈ blocks → 0 < i /-- Proof that `blocks` sums to `n` -/ blocks_sum : blocks.sum = n deriving DecidableEq attribute [simp] Composition.blocks_sum /-- Combinatorial viewpoint on a composition of `n`, by seeing it as non-empty blocks of consecutive integers in `{0, ..., n-1}`. We register every block by its left end-point, yielding a finset containing `0`. As this does not make sense for `n = 0`, we add `n` to this finset, and get a finset of `{0, ..., n}` containing `0` and `n`. This is the data in the structure `CompositionAsSet n`. -/ @[ext] structure CompositionAsSet (n : ℕ) where /-- Combinatorial viewpoint on a composition of `n` as consecutive integers `{0, ..., n-1}` -/ boundaries : Finset (Fin n.succ) /-- Proof that `0` is a member of `boundaries` -/ zero_mem : (0 : Fin n.succ) ∈ boundaries /-- Last element of the composition -/ getLast_mem : Fin.last n ∈ boundaries deriving DecidableEq instance {n : ℕ} : Inhabited (CompositionAsSet n) := ⟨⟨Finset.univ, Finset.mem_univ _, Finset.mem_univ _⟩⟩ attribute [simp] CompositionAsSet.zero_mem CompositionAsSet.getLast_mem /-! ### Compositions A composition of an integer `n` is a decomposition `n = i₀ + ... + i_{k-1}` of `n` into a sum of positive integers. -/ namespace Composition variable (c : Composition n) instance (n : ℕ) : ToString (Composition n) := ⟨fun c => toString c.blocks⟩ /-- The length of a composition, i.e., the number of blocks in the composition. -/ abbrev length : ℕ := c.blocks.length theorem blocks_length : c.blocks.length = c.length := rfl /-- The blocks of a composition, seen as a function on `Fin c.length`. When composing analytic functions using compositions, this is the main player. -/ def blocksFun : Fin c.length → ℕ := c.blocks.get @[simp] theorem ofFn_blocksFun : ofFn c.blocksFun = c.blocks := ofFn_get _ @[simp] theorem sum_blocksFun : ∑ i, c.blocksFun i = n := by conv_rhs => rw [← c.blocks_sum, ← ofFn_blocksFun, sum_ofFn]
@[simp]
Mathlib/Combinatorics/Enumerative/Composition.lean
160
161
/- Copyright (c) 2018 Sean Leather. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sean Leather, Mario Carneiro -/ import Mathlib.Data.List.AList import Mathlib.Data.Finset.Sigma import Mathlib.Data.Part /-! # Finite maps over `Multiset` -/ universe u v w open List variable {α : Type u} {β : α → Type v} /-! ### Multisets of sigma types -/ namespace Multiset /-- Multiset of keys of an association multiset. -/ def keys (s : Multiset (Sigma β)) : Multiset α := s.map Sigma.fst @[simp] theorem coe_keys {l : List (Sigma β)} : keys (l : Multiset (Sigma β)) = (l.keys : Multiset α) := rfl @[simp] theorem keys_zero : keys (0 : Multiset (Sigma β)) = 0 := rfl @[simp] theorem keys_cons {a : α} {b : β a} {s : Multiset (Sigma β)} : keys (⟨a, b⟩ ::ₘ s) = a ::ₘ keys s := by simp [keys] @[simp] theorem keys_singleton {a : α} {b : β a} : keys ({⟨a, b⟩} : Multiset (Sigma β)) = {a} := rfl /-- `NodupKeys s` means that `s` has no duplicate keys. -/ def NodupKeys (s : Multiset (Sigma β)) : Prop := Quot.liftOn s List.NodupKeys fun _ _ p => propext <| perm_nodupKeys p @[simp] theorem coe_nodupKeys {l : List (Sigma β)} : @NodupKeys α β l ↔ l.NodupKeys := Iff.rfl lemma nodup_keys {m : Multiset (Σ a, β a)} : m.keys.Nodup ↔ m.NodupKeys := by rcases m with ⟨l⟩; rfl alias ⟨_, NodupKeys.nodup_keys⟩ := nodup_keys protected lemma NodupKeys.nodup {m : Multiset (Σ a, β a)} (h : m.NodupKeys) : m.Nodup := h.nodup_keys.of_map _ end Multiset /-! ### Finmap -/ /-- `Finmap β` is the type of finite maps over a multiset. It is effectively a quotient of `AList β` by permutation of the underlying list. -/ structure Finmap (β : α → Type v) : Type max u v where /-- The underlying `Multiset` of a `Finmap` -/ entries : Multiset (Sigma β) /-- There are no duplicate keys in `entries` -/ nodupKeys : entries.NodupKeys /-- The quotient map from `AList` to `Finmap`. -/ def AList.toFinmap (s : AList β) : Finmap β := ⟨s.entries, s.nodupKeys⟩ local notation:arg "⟦" a "⟧" => AList.toFinmap a theorem AList.toFinmap_eq {s₁ s₂ : AList β} : toFinmap s₁ = toFinmap s₂ ↔ s₁.entries ~ s₂.entries := by cases s₁ cases s₂ simp [AList.toFinmap] @[simp] theorem AList.toFinmap_entries (s : AList β) : ⟦s⟧.entries = s.entries := rfl /-- Given `l : List (Sigma β)`, create a term of type `Finmap β` by removing entries with duplicate keys. -/ def List.toFinmap [DecidableEq α] (s : List (Sigma β)) : Finmap β := s.toAList.toFinmap namespace Finmap open AList lemma nodup_entries (f : Finmap β) : f.entries.Nodup := f.nodupKeys.nodup /-! ### Lifting from AList -/ /-- Lift a permutation-respecting function on `AList` to `Finmap`. -/ def liftOn {γ} (s : Finmap β) (f : AList β → γ) (H : ∀ a b : AList β, a.entries ~ b.entries → f a = f b) : γ := by refine (Quotient.liftOn s.entries (fun (l : List (Sigma β)) => (⟨_, fun nd => f ⟨l, nd⟩⟩ : Part γ)) (fun l₁ l₂ p => Part.ext' (perm_nodupKeys p) ?_) : Part γ).get ?_ · exact fun h1 h2 => H _ _ p · have := s.nodupKeys revert this rcases s.entries with ⟨l⟩ exact id @[simp] theorem liftOn_toFinmap {γ} (s : AList β) (f : AList β → γ) (H) : liftOn ⟦s⟧ f H = f s := by cases s rfl /-- Lift a permutation-respecting function on 2 `AList`s to 2 `Finmap`s. -/ def liftOn₂ {γ} (s₁ s₂ : Finmap β) (f : AList β → AList β → γ) (H : ∀ a₁ b₁ a₂ b₂ : AList β, a₁.entries ~ a₂.entries → b₁.entries ~ b₂.entries → f a₁ b₁ = f a₂ b₂) : γ := liftOn s₁ (fun l₁ => liftOn s₂ (f l₁) fun _ _ p => H _ _ _ _ (Perm.refl _) p) fun a₁ a₂ p => by have H' : f a₁ = f a₂ := funext fun _ => H _ _ _ _ p (Perm.refl _) simp only [H'] @[simp] theorem liftOn₂_toFinmap {γ} (s₁ s₂ : AList β) (f : AList β → AList β → γ) (H) : liftOn₂ ⟦s₁⟧ ⟦s₂⟧ f H = f s₁ s₂ := by cases s₁; cases s₂; rfl /-! ### Induction -/ @[elab_as_elim] theorem induction_on {C : Finmap β → Prop} (s : Finmap β) (H : ∀ a : AList β, C ⟦a⟧) : C s := by rcases s with ⟨⟨a⟩, h⟩; exact H ⟨a, h⟩ @[elab_as_elim] theorem induction_on₂ {C : Finmap β → Finmap β → Prop} (s₁ s₂ : Finmap β) (H : ∀ a₁ a₂ : AList β, C ⟦a₁⟧ ⟦a₂⟧) : C s₁ s₂ := induction_on s₁ fun l₁ => induction_on s₂ fun l₂ => H l₁ l₂ @[elab_as_elim] theorem induction_on₃ {C : Finmap β → Finmap β → Finmap β → Prop} (s₁ s₂ s₃ : Finmap β) (H : ∀ a₁ a₂ a₃ : AList β, C ⟦a₁⟧ ⟦a₂⟧ ⟦a₃⟧) : C s₁ s₂ s₃ := induction_on₂ s₁ s₂ fun l₁ l₂ => induction_on s₃ fun l₃ => H l₁ l₂ l₃ /-! ### extensionality -/ @[ext] theorem ext : ∀ {s t : Finmap β}, s.entries = t.entries → s = t | ⟨l₁, h₁⟩, ⟨l₂, _⟩, H => by congr @[simp] theorem ext_iff' {s t : Finmap β} : s.entries = t.entries ↔ s = t := Finmap.ext_iff.symm /-! ### mem -/ /-- The predicate `a ∈ s` means that `s` has a value associated to the key `a`. -/ instance : Membership α (Finmap β) := ⟨fun s a => a ∈ s.entries.keys⟩ theorem mem_def {a : α} {s : Finmap β} : a ∈ s ↔ a ∈ s.entries.keys := Iff.rfl @[simp] theorem mem_toFinmap {a : α} {s : AList β} : a ∈ toFinmap s ↔ a ∈ s := Iff.rfl /-! ### keys -/ /-- The set of keys of a finite map. -/ def keys (s : Finmap β) : Finset α := ⟨s.entries.keys, s.nodupKeys.nodup_keys⟩ @[simp] theorem keys_val (s : AList β) : (keys ⟦s⟧).val = s.keys := rfl @[simp] theorem keys_ext {s₁ s₂ : AList β} : keys ⟦s₁⟧ = keys ⟦s₂⟧ ↔ s₁.keys ~ s₂.keys := by simp [keys, AList.keys] theorem mem_keys {a : α} {s : Finmap β} : a ∈ s.keys ↔ a ∈ s := induction_on s fun _ => AList.mem_keys /-! ### empty -/ /-- The empty map. -/ instance : EmptyCollection (Finmap β) := ⟨⟨0, nodupKeys_nil⟩⟩ instance : Inhabited (Finmap β) := ⟨∅⟩ @[simp] theorem empty_toFinmap : (⟦∅⟧ : Finmap β) = ∅ := rfl @[simp] theorem toFinmap_nil [DecidableEq α] : ([].toFinmap : Finmap β) = ∅ := rfl theorem not_mem_empty {a : α} : a ∉ (∅ : Finmap β) := Multiset.not_mem_zero a @[simp] theorem keys_empty : (∅ : Finmap β).keys = ∅ := rfl /-! ### singleton -/ /-- The singleton map. -/ def singleton (a : α) (b : β a) : Finmap β := ⟦AList.singleton a b⟧ @[simp] theorem keys_singleton (a : α) (b : β a) : (singleton a b).keys = {a} := rfl @[simp] theorem mem_singleton (x y : α) (b : β y) : x ∈ singleton y b ↔ x = y := by simp [singleton, mem_def] section variable [DecidableEq α] instance decidableEq [∀ a, DecidableEq (β a)] : DecidableEq (Finmap β) | _, _ => decidable_of_iff _ Finmap.ext_iff.symm /-! ### lookup -/ /-- Look up the value associated to a key in a map. -/ def lookup (a : α) (s : Finmap β) : Option (β a) := liftOn s (AList.lookup a) fun _ _ => perm_lookup @[simp] theorem lookup_toFinmap (a : α) (s : AList β) : lookup a ⟦s⟧ = s.lookup a := rfl @[simp] theorem dlookup_list_toFinmap (a : α) (s : List (Sigma β)) : lookup a s.toFinmap = s.dlookup a := by rw [List.toFinmap, lookup_toFinmap, lookup_to_alist] @[simp] theorem lookup_empty (a) : lookup a (∅ : Finmap β) = none := rfl theorem lookup_isSome {a : α} {s : Finmap β} : (s.lookup a).isSome ↔ a ∈ s := induction_on s fun _ => AList.lookup_isSome theorem lookup_eq_none {a} {s : Finmap β} : lookup a s = none ↔ a ∉ s := induction_on s fun _ => AList.lookup_eq_none lemma mem_lookup_iff {s : Finmap β} {a : α} {b : β a} : b ∈ s.lookup a ↔ Sigma.mk a b ∈ s.entries := by rcases s with ⟨⟨l⟩, hl⟩; exact List.mem_dlookup_iff hl lemma lookup_eq_some_iff {s : Finmap β} {a : α} {b : β a} : s.lookup a = b ↔ Sigma.mk a b ∈ s.entries := mem_lookup_iff @[simp] lemma sigma_keys_lookup (s : Finmap β) : s.keys.sigma (fun i => (s.lookup i).toFinset) = ⟨s.entries, s.nodup_entries⟩ := by ext x have : x ∈ s.entries → x.1 ∈ s.keys := Multiset.mem_map_of_mem _ simpa [lookup_eq_some_iff] @[simp] theorem lookup_singleton_eq {a : α} {b : β a} : (singleton a b).lookup a = some b := by rw [singleton, lookup_toFinmap, AList.singleton, AList.lookup, dlookup_cons_eq] instance (a : α) (s : Finmap β) : Decidable (a ∈ s) := decidable_of_iff _ lookup_isSome theorem mem_iff {a : α} {s : Finmap β} : a ∈ s ↔ ∃ b, s.lookup a = some b := induction_on s fun s => Iff.trans List.mem_keys <| exists_congr fun _ => (mem_dlookup_iff s.nodupKeys).symm theorem mem_of_lookup_eq_some {a : α} {b : β a} {s : Finmap β} (h : s.lookup a = some b) : a ∈ s := mem_iff.mpr ⟨_, h⟩ theorem ext_lookup {s₁ s₂ : Finmap β} : (∀ x, s₁.lookup x = s₂.lookup x) → s₁ = s₂ := induction_on₂ s₁ s₂ fun s₁ s₂ h => by simp only [AList.lookup, lookup_toFinmap] at h rw [AList.toFinmap_eq] apply lookup_ext s₁.nodupKeys s₂.nodupKeys intro x y rw [h] /-- An equivalence between `Finmap β` and pairs `(keys : Finset α, lookup : ∀ a, Option (β a))` such that `(lookup a).isSome ↔ a ∈ keys`. -/ @[simps apply_coe_fst apply_coe_snd] def keysLookupEquiv : Finmap β ≃ { f : Finset α × (∀ a, Option (β a)) // ∀ i, (f.2 i).isSome ↔ i ∈ f.1 } where toFun s := ⟨(s.keys, fun i => s.lookup i), fun _ => lookup_isSome⟩ invFun f := mk (f.1.1.sigma fun i => (f.1.2 i).toFinset).val <| by refine Multiset.nodup_keys.1 ((Finset.nodup _).map_on ?_) simp only [Finset.mem_val, Finset.mem_sigma, Option.mem_toFinset, Option.mem_def] rintro ⟨i, x⟩ ⟨_, hx⟩ ⟨j, y⟩ ⟨_, hy⟩ (rfl : i = j) simpa using hx.symm.trans hy left_inv f := ext <| by simp right_inv := fun ⟨(s, f), hf⟩ => by dsimp only at hf ext · simp [keys, Multiset.keys, ← hf, Option.isSome_iff_exists] · simp +contextual [lookup_eq_some_iff, ← hf] @[simp] lemma keysLookupEquiv_symm_apply_keys : ∀ f : {f : Finset α × (∀ a, Option (β a)) // ∀ i, (f.2 i).isSome ↔ i ∈ f.1}, (keysLookupEquiv.symm f).keys = f.1.1 := keysLookupEquiv.surjective.forall.2 fun _ => by simp only [Equiv.symm_apply_apply, keysLookupEquiv_apply_coe_fst] @[simp] lemma keysLookupEquiv_symm_apply_lookup : ∀ (f : {f : Finset α × (∀ a, Option (β a)) // ∀ i, (f.2 i).isSome ↔ i ∈ f.1}) a, (keysLookupEquiv.symm f).lookup a = f.1.2 a := keysLookupEquiv.surjective.forall.2 fun _ _ => by simp only [Equiv.symm_apply_apply, keysLookupEquiv_apply_coe_snd] /-! ### replace -/ /-- Replace a key with a given value in a finite map. If the key is not present it does nothing. -/ def replace (a : α) (b : β a) (s : Finmap β) : Finmap β := (liftOn s fun t => AList.toFinmap (AList.replace a b t)) fun _ _ p => toFinmap_eq.2 <| perm_replace p @[simp] theorem replace_toFinmap (a : α) (b : β a) (s : AList β) : replace a b ⟦s⟧ = (⟦s.replace a b⟧ : Finmap β) := by simp [replace] @[simp] theorem keys_replace (a : α) (b : β a) (s : Finmap β) : (replace a b s).keys = s.keys := induction_on s fun s => by simp @[simp] theorem mem_replace {a a' : α} {b : β a} {s : Finmap β} : a' ∈ replace a b s ↔ a' ∈ s := induction_on s fun s => by simp end /-! ### foldl -/ /-- Fold a commutative function over the key-value pairs in the map -/ def foldl {δ : Type w} (f : δ → ∀ a, β a → δ) (H : ∀ d a₁ b₁ a₂ b₂, f (f d a₁ b₁) a₂ b₂ = f (f d a₂ b₂) a₁ b₁) (d : δ) (m : Finmap β) : δ := letI : RightCommutative fun d (s : Sigma β) ↦ f d s.1 s.2 := ⟨fun _ _ _ ↦ H _ _ _ _ _⟩ m.entries.foldl (fun d s => f d s.1 s.2) d /-- `any f s` returns `true` iff there exists a value `v` in `s` such that `f v = true`. -/ def any (f : ∀ x, β x → Bool) (s : Finmap β) : Bool := s.foldl (fun x y z => x || f y z) (fun _ _ _ _ => by simp_rw [Bool.or_assoc, Bool.or_comm, imp_true_iff]) false /-- `all f s` returns `true` iff `f v = true` for all values `v` in `s`. -/ def all (f : ∀ x, β x → Bool) (s : Finmap β) : Bool := s.foldl (fun x y z => x && f y z) (fun _ _ _ _ => by simp_rw [Bool.and_assoc, Bool.and_comm, imp_true_iff]) true /-! ### erase -/ section variable [DecidableEq α] /-- Erase a key from the map. If the key is not present it does nothing. -/ def erase (a : α) (s : Finmap β) : Finmap β := (liftOn s fun t => AList.toFinmap (AList.erase a t)) fun _ _ p => toFinmap_eq.2 <| perm_erase p @[simp] theorem erase_toFinmap (a : α) (s : AList β) : erase a ⟦s⟧ = AList.toFinmap (s.erase a) := by simp [erase] @[simp] theorem keys_erase_toFinset (a : α) (s : AList β) : keys ⟦s.erase a⟧ = (keys ⟦s⟧).erase a := by simp [Finset.erase, keys, AList.erase, keys_kerase] @[simp] theorem keys_erase (a : α) (s : Finmap β) : (erase a s).keys = s.keys.erase a := induction_on s fun s => by simp @[simp] theorem mem_erase {a a' : α} {s : Finmap β} : a' ∈ erase a s ↔ a' ≠ a ∧ a' ∈ s := induction_on s fun s => by simp theorem not_mem_erase_self {a : α} {s : Finmap β} : ¬a ∈ erase a s := by rw [mem_erase, not_and_or, not_not] left rfl @[simp] theorem lookup_erase (a) (s : Finmap β) : lookup a (erase a s) = none := induction_on s <| AList.lookup_erase a @[simp] theorem lookup_erase_ne {a a'} {s : Finmap β} (h : a ≠ a') : lookup a (erase a' s) = lookup a s := induction_on s fun _ => AList.lookup_erase_ne h theorem erase_erase {a a' : α} {s : Finmap β} : erase a (erase a' s) = erase a' (erase a s) := induction_on s fun s => ext (by simp only [AList.erase_erase, erase_toFinmap]) /-! ### sdiff -/ /-- `sdiff s s'` consists of all key-value pairs from `s` and `s'` where the keys are in `s` or `s'` but not both. -/ def sdiff (s s' : Finmap β) : Finmap β := s'.foldl (fun s x _ => s.erase x) (fun _ _ _ _ _ => erase_erase) s instance : SDiff (Finmap β) := ⟨sdiff⟩ /-! ### insert -/ /-- Insert a key-value pair into a finite map, replacing any existing pair with the same key. -/ def insert (a : α) (b : β a) (s : Finmap β) : Finmap β := (liftOn s fun t => AList.toFinmap (AList.insert a b t)) fun _ _ p => toFinmap_eq.2 <| perm_insert p @[simp] theorem insert_toFinmap (a : α) (b : β a) (s : AList β) : insert a b (AList.toFinmap s) = AList.toFinmap (s.insert a b) := by simp [insert] theorem entries_insert_of_not_mem {a : α} {b : β a} {s : Finmap β} : a ∉ s → (insert a b s).entries = ⟨a, b⟩ ::ₘ s.entries := induction_on s fun s h => by simp [AList.entries_insert_of_not_mem (mt mem_toFinmap.1 h), -entries_insert] @[deprecated (since := "2024-12-14")] alias insert_entries_of_neg := entries_insert_of_not_mem @[simp] theorem mem_insert {a a' : α} {b' : β a'} {s : Finmap β} : a ∈ insert a' b' s ↔ a = a' ∨ a ∈ s := induction_on s AList.mem_insert
@[simp] theorem lookup_insert {a} {b : β a} (s : Finmap β) : lookup a (insert a b s) = some b := induction_on s fun s => by simp only [insert_toFinmap, lookup_toFinmap, AList.lookup_insert]
Mathlib/Data/Finmap.lean
438
441
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Jens Wagemaker -/ import Mathlib.Algebra.Ring.Associated import Mathlib.Algebra.Ring.Regular /-! # Monoids with normalization functions, `gcd`, and `lcm` This file defines extra structures on `CancelCommMonoidWithZero`s, including `IsDomain`s. ## Main Definitions * `NormalizationMonoid` * `GCDMonoid` * `NormalizedGCDMonoid` * `gcdMonoidOfGCD`, `gcdMonoidOfExistsGCD`, `normalizedGCDMonoidOfGCD`, `normalizedGCDMonoidOfExistsGCD` * `gcdMonoidOfLCM`, `gcdMonoidOfExistsLCM`, `normalizedGCDMonoidOfLCM`, `normalizedGCDMonoidOfExistsLCM` For the `NormalizedGCDMonoid` instances on `ℕ` and `ℤ`, see `Mathlib.Algebra.GCDMonoid.Nat`. ## Implementation Notes * `NormalizationMonoid` is defined by assigning to each element a `normUnit` such that multiplying by that unit normalizes the monoid, and `normalize` is an idempotent monoid homomorphism. This definition as currently implemented does casework on `0`. * `GCDMonoid` contains the definitions of `gcd` and `lcm` with the usual properties. They are both determined up to a unit. * `NormalizedGCDMonoid` extends `NormalizationMonoid`, so the `gcd` and `lcm` are always normalized. This makes `gcd`s of polynomials easier to work with, but excludes Euclidean domains, and monoids without zero. * `gcdMonoidOfGCD` and `normalizedGCDMonoidOfGCD` noncomputably construct a `GCDMonoid` (resp. `NormalizedGCDMonoid`) structure just from the `gcd` and its properties. * `gcdMonoidOfExistsGCD` and `normalizedGCDMonoidOfExistsGCD` noncomputably construct a `GCDMonoid` (resp. `NormalizedGCDMonoid`) structure just from a proof that any two elements have a (not necessarily normalized) `gcd`. * `gcdMonoidOfLCM` and `normalizedGCDMonoidOfLCM` noncomputably construct a `GCDMonoid` (resp. `NormalizedGCDMonoid`) structure just from the `lcm` and its properties. * `gcdMonoidOfExistsLCM` and `normalizedGCDMonoidOfExistsLCM` noncomputably construct a `GCDMonoid` (resp. `NormalizedGCDMonoid`) structure just from a proof that any two elements have a (not necessarily normalized) `lcm`. ## TODO * Port GCD facts about nats, definition of coprime * Generalize normalization monoids to commutative (cancellative) monoids with or without zero ## Tags divisibility, gcd, lcm, normalize -/ variable {α : Type*} /-- Normalization monoid: multiplying with `normUnit` gives a normal form for associated elements. -/ class NormalizationMonoid (α : Type*) [CancelCommMonoidWithZero α] where /-- `normUnit` assigns to each element of the monoid a unit of the monoid. -/ normUnit : α → αˣ /-- The proposition that `normUnit` maps `0` to the identity. -/ normUnit_zero : normUnit 0 = 1 /-- The proposition that `normUnit` respects multiplication of non-zero elements. -/ normUnit_mul : ∀ {a b}, a ≠ 0 → b ≠ 0 → normUnit (a * b) = normUnit a * normUnit b /-- The proposition that `normUnit` maps units to their inverses. -/ normUnit_coe_units : ∀ u : αˣ, normUnit u = u⁻¹ export NormalizationMonoid (normUnit normUnit_zero normUnit_mul normUnit_coe_units) attribute [simp] normUnit_coe_units normUnit_zero normUnit_mul section NormalizationMonoid variable [CancelCommMonoidWithZero α] [NormalizationMonoid α] @[simp] theorem normUnit_one : normUnit (1 : α) = 1 := normUnit_coe_units 1 /-- Chooses an element of each associate class, by multiplying by `normUnit` -/ def normalize : α →*₀ α where toFun x := x * normUnit x map_zero' := by simp only [normUnit_zero] exact mul_one (0 : α) map_one' := by rw [normUnit_one, one_mul]; rfl map_mul' x y := (by_cases fun hx : x = 0 => by rw [hx, zero_mul, zero_mul, zero_mul]) fun hx => (by_cases fun hy : y = 0 => by rw [hy, mul_zero, zero_mul, mul_zero]) fun hy => by simp only [normUnit_mul hx hy, Units.val_mul]; simp only [mul_assoc, mul_left_comm y] theorem associated_normalize (x : α) : Associated x (normalize x) := ⟨_, rfl⟩ theorem normalize_associated (x : α) : Associated (normalize x) x := (associated_normalize _).symm theorem associated_normalize_iff {x y : α} : Associated x (normalize y) ↔ Associated x y := ⟨fun h => h.trans (normalize_associated y), fun h => h.trans (associated_normalize y)⟩ theorem normalize_associated_iff {x y : α} : Associated (normalize x) y ↔ Associated x y := ⟨fun h => (associated_normalize _).trans h, fun h => (normalize_associated _).trans h⟩ theorem Associates.mk_normalize (x : α) : Associates.mk (normalize x) = Associates.mk x := Associates.mk_eq_mk_iff_associated.2 (normalize_associated _) theorem normalize_apply (x : α) : normalize x = x * normUnit x := rfl theorem normalize_zero : normalize (0 : α) = 0 := normalize.map_zero theorem normalize_one : normalize (1 : α) = 1 := normalize.map_one theorem normalize_coe_units (u : αˣ) : normalize (u : α) = 1 := by simp [normalize_apply] theorem normalize_eq_zero {x : α} : normalize x = 0 ↔ x = 0 := ⟨fun hx => (associated_zero_iff_eq_zero x).1 <| hx ▸ associated_normalize _, by rintro rfl; exact normalize_zero⟩ theorem normalize_eq_one {x : α} : normalize x = 1 ↔ IsUnit x := ⟨fun hx => isUnit_iff_exists_inv.2 ⟨_, hx⟩, fun ⟨u, hu⟩ => hu ▸ normalize_coe_units u⟩ @[simp] theorem normUnit_mul_normUnit (a : α) : normUnit (a * normUnit a) = 1 := by nontriviality α using Subsingleton.elim a 0 obtain rfl | h := eq_or_ne a 0 · rw [normUnit_zero, zero_mul, normUnit_zero] · rw [normUnit_mul h (Units.ne_zero _), normUnit_coe_units, mul_inv_eq_one] @[simp] theorem normalize_idem (x : α) : normalize (normalize x) = normalize x := by simp [normalize_apply] theorem normalize_eq_normalize {a b : α} (hab : a ∣ b) (hba : b ∣ a) : normalize a = normalize b := by nontriviality α rcases associated_of_dvd_dvd hab hba with ⟨u, rfl⟩ refine by_cases (by rintro rfl; simp only [zero_mul]) fun ha : a ≠ 0 => ?_ suffices a * ↑(normUnit a) = a * ↑u * ↑(normUnit a) * ↑u⁻¹ by simpa only [normalize_apply, mul_assoc, normUnit_mul ha u.ne_zero, normUnit_coe_units] calc a * ↑(normUnit a) = a * ↑(normUnit a) * ↑u * ↑u⁻¹ := (Units.mul_inv_cancel_right _ _).symm _ = a * ↑u * ↑(normUnit a) * ↑u⁻¹ := by rw [mul_right_comm a] theorem normalize_eq_normalize_iff {x y : α} : normalize x = normalize y ↔ x ∣ y ∧ y ∣ x := ⟨fun h => ⟨Units.dvd_mul_right.1 ⟨_, h.symm⟩, Units.dvd_mul_right.1 ⟨_, h⟩⟩, fun ⟨hxy, hyx⟩ => normalize_eq_normalize hxy hyx⟩ theorem dvd_antisymm_of_normalize_eq {a b : α} (ha : normalize a = a) (hb : normalize b = b) (hab : a ∣ b) (hba : b ∣ a) : a = b := ha ▸ hb ▸ normalize_eq_normalize hab hba theorem Associated.eq_of_normalized {a b : α} (h : Associated a b) (ha : normalize a = a) (hb : normalize b = b) : a = b := dvd_antisymm_of_normalize_eq ha hb h.dvd h.dvd' @[simp] theorem dvd_normalize_iff {a b : α} : a ∣ normalize b ↔ a ∣ b := Units.dvd_mul_right @[simp] theorem normalize_dvd_iff {a b : α} : normalize a ∣ b ↔ a ∣ b := Units.mul_right_dvd end NormalizationMonoid namespace Associates variable [CancelCommMonoidWithZero α] [NormalizationMonoid α] /-- Maps an element of `Associates` back to the normalized element of its associate class -/ protected def out : Associates α → α := (Quotient.lift (normalize : α → α)) fun a _ ⟨_, hu⟩ => hu ▸ normalize_eq_normalize ⟨_, rfl⟩ (Units.mul_right_dvd.2 <| dvd_refl a) @[simp] theorem out_mk (a : α) : (Associates.mk a).out = normalize a := rfl @[simp] theorem out_one : (1 : Associates α).out = 1 := normalize_one theorem out_mul (a b : Associates α) : (a * b).out = a.out * b.out := Quotient.inductionOn₂ a b fun _ _ => by simp only [Associates.quotient_mk_eq_mk, out_mk, mk_mul_mk, normalize.map_mul] theorem dvd_out_iff (a : α) (b : Associates α) : a ∣ b.out ↔ Associates.mk a ≤ b := Quotient.inductionOn b <| by simp [Associates.out_mk, Associates.quotient_mk_eq_mk, mk_le_mk_iff_dvd] theorem out_dvd_iff (a : α) (b : Associates α) : b.out ∣ a ↔ b ≤ Associates.mk a := Quotient.inductionOn b <| by simp [Associates.out_mk, Associates.quotient_mk_eq_mk, mk_le_mk_iff_dvd] @[simp] theorem out_top : (⊤ : Associates α).out = 0 := normalize_zero @[simp] theorem normalize_out (a : Associates α) : normalize a.out = a.out := Quotient.inductionOn a normalize_idem @[simp] theorem mk_out (a : Associates α) : Associates.mk a.out = a := Quotient.inductionOn a mk_normalize theorem out_injective : Function.Injective (Associates.out : _ → α) := Function.LeftInverse.injective mk_out end Associates /-- GCD monoid: a `CancelCommMonoidWithZero` with `gcd` (greatest common divisor) and `lcm` (least common multiple) operations, determined up to a unit. The type class focuses on `gcd` and we derive the corresponding `lcm` facts from `gcd`. -/ class GCDMonoid (α : Type*) [CancelCommMonoidWithZero α] where /-- The greatest common divisor between two elements. -/ gcd : α → α → α /-- The least common multiple between two elements. -/ lcm : α → α → α /-- The GCD is a divisor of the first element. -/ gcd_dvd_left : ∀ a b, gcd a b ∣ a /-- The GCD is a divisor of the second element. -/ gcd_dvd_right : ∀ a b, gcd a b ∣ b /-- Any common divisor of both elements is a divisor of the GCD. -/ dvd_gcd : ∀ {a b c}, a ∣ c → a ∣ b → a ∣ gcd c b /-- The product of two elements is `Associated` with the product of their GCD and LCM. -/ gcd_mul_lcm : ∀ a b, Associated (gcd a b * lcm a b) (a * b) /-- `0` is left-absorbing. -/ lcm_zero_left : ∀ a, lcm 0 a = 0 /-- `0` is right-absorbing. -/ lcm_zero_right : ∀ a, lcm a 0 = 0 /-- Normalized GCD monoid: a `CancelCommMonoidWithZero` with normalization and `gcd` (greatest common divisor) and `lcm` (least common multiple) operations. In this setting `gcd` and `lcm` form a bounded lattice on the associated elements where `gcd` is the infimum, `lcm` is the supremum, `1` is bottom, and `0` is top. The type class focuses on `gcd` and we derive the corresponding `lcm` facts from `gcd`. -/ class NormalizedGCDMonoid (α : Type*) [CancelCommMonoidWithZero α] extends NormalizationMonoid α, GCDMonoid α where /-- The GCD is normalized to itself. -/ normalize_gcd : ∀ a b, normalize (gcd a b) = gcd a b /-- The LCM is normalized to itself. -/ normalize_lcm : ∀ a b, normalize (lcm a b) = lcm a b export GCDMonoid (gcd lcm gcd_dvd_left gcd_dvd_right dvd_gcd lcm_zero_left lcm_zero_right) attribute [simp] lcm_zero_left lcm_zero_right section GCDMonoid variable [CancelCommMonoidWithZero α] instance [NormalizationMonoid α] : Nonempty (NormalizationMonoid α) := ⟨‹_›⟩ instance [GCDMonoid α] : Nonempty (GCDMonoid α) := ⟨‹_›⟩ instance [NormalizedGCDMonoid α] : Nonempty (NormalizedGCDMonoid α) := ⟨‹_›⟩ instance [h : Nonempty (NormalizedGCDMonoid α)] : Nonempty (NormalizationMonoid α) := h.elim fun _ ↦ inferInstance instance [h : Nonempty (NormalizedGCDMonoid α)] : Nonempty (GCDMonoid α) := h.elim fun _ ↦ inferInstance theorem gcd_isUnit_iff_isRelPrime [GCDMonoid α] {a b : α} : IsUnit (gcd a b) ↔ IsRelPrime a b := ⟨fun h _ ha hb ↦ isUnit_of_dvd_unit (dvd_gcd ha hb) h, (· (gcd_dvd_left a b) (gcd_dvd_right a b))⟩ @[simp] theorem normalize_gcd [NormalizedGCDMonoid α] : ∀ a b : α, normalize (gcd a b) = gcd a b := NormalizedGCDMonoid.normalize_gcd theorem gcd_mul_lcm [GCDMonoid α] : ∀ a b : α, Associated (gcd a b * lcm a b) (a * b) := GCDMonoid.gcd_mul_lcm section GCD theorem dvd_gcd_iff [GCDMonoid α] (a b c : α) : a ∣ gcd b c ↔ a ∣ b ∧ a ∣ c := Iff.intro (fun h => ⟨h.trans (gcd_dvd_left _ _), h.trans (gcd_dvd_right _ _)⟩) fun ⟨hab, hac⟩ => dvd_gcd hab hac theorem gcd_comm [NormalizedGCDMonoid α] (a b : α) : gcd a b = gcd b a := dvd_antisymm_of_normalize_eq (normalize_gcd _ _) (normalize_gcd _ _) (dvd_gcd (gcd_dvd_right _ _) (gcd_dvd_left _ _)) (dvd_gcd (gcd_dvd_right _ _) (gcd_dvd_left _ _)) theorem gcd_comm' [GCDMonoid α] (a b : α) : Associated (gcd a b) (gcd b a) := associated_of_dvd_dvd (dvd_gcd (gcd_dvd_right _ _) (gcd_dvd_left _ _)) (dvd_gcd (gcd_dvd_right _ _) (gcd_dvd_left _ _)) theorem gcd_assoc [NormalizedGCDMonoid α] (m n k : α) : gcd (gcd m n) k = gcd m (gcd n k) := dvd_antisymm_of_normalize_eq (normalize_gcd _ _) (normalize_gcd _ _) (dvd_gcd ((gcd_dvd_left (gcd m n) k).trans (gcd_dvd_left m n)) (dvd_gcd ((gcd_dvd_left (gcd m n) k).trans (gcd_dvd_right m n)) (gcd_dvd_right (gcd m n) k))) (dvd_gcd (dvd_gcd (gcd_dvd_left m (gcd n k)) ((gcd_dvd_right m (gcd n k)).trans (gcd_dvd_left n k))) ((gcd_dvd_right m (gcd n k)).trans (gcd_dvd_right n k))) theorem gcd_assoc' [GCDMonoid α] (m n k : α) : Associated (gcd (gcd m n) k) (gcd m (gcd n k)) := associated_of_dvd_dvd (dvd_gcd ((gcd_dvd_left (gcd m n) k).trans (gcd_dvd_left m n)) (dvd_gcd ((gcd_dvd_left (gcd m n) k).trans (gcd_dvd_right m n)) (gcd_dvd_right (gcd m n) k))) (dvd_gcd (dvd_gcd (gcd_dvd_left m (gcd n k)) ((gcd_dvd_right m (gcd n k)).trans (gcd_dvd_left n k))) ((gcd_dvd_right m (gcd n k)).trans (gcd_dvd_right n k))) instance [NormalizedGCDMonoid α] : Std.Commutative (α := α) gcd where comm := gcd_comm instance [NormalizedGCDMonoid α] : Std.Associative (α := α) gcd where assoc := gcd_assoc theorem gcd_eq_normalize [NormalizedGCDMonoid α] {a b c : α} (habc : gcd a b ∣ c) (hcab : c ∣ gcd a b) : gcd a b = normalize c := normalize_gcd a b ▸ normalize_eq_normalize habc hcab @[simp] theorem gcd_zero_left [NormalizedGCDMonoid α] (a : α) : gcd 0 a = normalize a := gcd_eq_normalize (gcd_dvd_right 0 a) (dvd_gcd (dvd_zero _) (dvd_refl a)) theorem gcd_zero_left' [GCDMonoid α] (a : α) : Associated (gcd 0 a) a := associated_of_dvd_dvd (gcd_dvd_right 0 a) (dvd_gcd (dvd_zero _) (dvd_refl a)) @[simp] theorem gcd_zero_right [NormalizedGCDMonoid α] (a : α) : gcd a 0 = normalize a := gcd_eq_normalize (gcd_dvd_left a 0) (dvd_gcd (dvd_refl a) (dvd_zero _)) theorem gcd_zero_right' [GCDMonoid α] (a : α) : Associated (gcd a 0) a := associated_of_dvd_dvd (gcd_dvd_left a 0) (dvd_gcd (dvd_refl a) (dvd_zero _)) @[simp] theorem gcd_eq_zero_iff [GCDMonoid α] (a b : α) : gcd a b = 0 ↔ a = 0 ∧ b = 0 := Iff.intro (fun h => by let ⟨ca, ha⟩ := gcd_dvd_left a b let ⟨cb, hb⟩ := gcd_dvd_right a b rw [h, zero_mul] at ha hb exact ⟨ha, hb⟩) fun ⟨ha, hb⟩ => by rw [ha, hb, ← zero_dvd_iff] apply dvd_gcd <;> rfl theorem gcd_ne_zero_of_left [GCDMonoid α] {a b : α} (ha : a ≠ 0) : gcd a b ≠ 0 := by simp_all theorem gcd_ne_zero_of_right [GCDMonoid α] {a b : α} (hb : b ≠ 0) : gcd a b ≠ 0 := by simp_all @[simp] theorem gcd_one_left [NormalizedGCDMonoid α] (a : α) : gcd 1 a = 1 := dvd_antisymm_of_normalize_eq (normalize_gcd _ _) normalize_one (gcd_dvd_left _ _) (one_dvd _) @[simp] theorem isUnit_gcd_one_left [GCDMonoid α] (a : α) : IsUnit (gcd 1 a) := isUnit_of_dvd_one (gcd_dvd_left _ _) theorem gcd_one_left' [GCDMonoid α] (a : α) : Associated (gcd 1 a) 1 := by simp @[simp] theorem gcd_one_right [NormalizedGCDMonoid α] (a : α) : gcd a 1 = 1 := dvd_antisymm_of_normalize_eq (normalize_gcd _ _) normalize_one (gcd_dvd_right _ _) (one_dvd _) @[simp] theorem isUnit_gcd_one_right [GCDMonoid α] (a : α) : IsUnit (gcd a 1) := isUnit_of_dvd_one (gcd_dvd_right _ _) theorem gcd_one_right' [GCDMonoid α] (a : α) : Associated (gcd a 1) 1 := by simp theorem gcd_dvd_gcd [GCDMonoid α] {a b c d : α} (hab : a ∣ b) (hcd : c ∣ d) : gcd a c ∣ gcd b d := dvd_gcd ((gcd_dvd_left _ _).trans hab) ((gcd_dvd_right _ _).trans hcd) protected theorem Associated.gcd [GCDMonoid α] {a₁ a₂ b₁ b₂ : α} (ha : Associated a₁ a₂) (hb : Associated b₁ b₂) : Associated (gcd a₁ b₁) (gcd a₂ b₂) := associated_of_dvd_dvd (gcd_dvd_gcd ha.dvd hb.dvd) (gcd_dvd_gcd ha.dvd' hb.dvd') @[simp] theorem gcd_same [NormalizedGCDMonoid α] (a : α) : gcd a a = normalize a := gcd_eq_normalize (gcd_dvd_left _ _) (dvd_gcd (dvd_refl a) (dvd_refl a)) @[simp] theorem gcd_mul_left [NormalizedGCDMonoid α] (a b c : α) : gcd (a * b) (a * c) = normalize a * gcd b c := (by_cases (by rintro rfl; simp only [zero_mul, gcd_zero_left, normalize_zero])) fun ha : a ≠ 0 => suffices gcd (a * b) (a * c) = normalize (a * gcd b c) by simpa let ⟨d, eq⟩ := dvd_gcd (dvd_mul_right a b) (dvd_mul_right a c) gcd_eq_normalize (eq.symm ▸ mul_dvd_mul_left a (show d ∣ gcd b c from dvd_gcd ((mul_dvd_mul_iff_left ha).1 <| eq ▸ gcd_dvd_left _ _) ((mul_dvd_mul_iff_left ha).1 <| eq ▸ gcd_dvd_right _ _))) (dvd_gcd (mul_dvd_mul_left a <| gcd_dvd_left _ _) (mul_dvd_mul_left a <| gcd_dvd_right _ _)) theorem gcd_mul_left' [GCDMonoid α] (a b c : α) : Associated (gcd (a * b) (a * c)) (a * gcd b c) := by obtain rfl | ha := eq_or_ne a 0 · simp only [zero_mul, gcd_zero_left'] obtain ⟨d, eq⟩ := dvd_gcd (dvd_mul_right a b) (dvd_mul_right a c) apply associated_of_dvd_dvd · rw [eq] apply mul_dvd_mul_left exact dvd_gcd ((mul_dvd_mul_iff_left ha).1 <| eq ▸ gcd_dvd_left _ _) ((mul_dvd_mul_iff_left ha).1 <| eq ▸ gcd_dvd_right _ _) · exact dvd_gcd (mul_dvd_mul_left a <| gcd_dvd_left _ _) (mul_dvd_mul_left a <| gcd_dvd_right _ _) @[simp] theorem gcd_mul_right [NormalizedGCDMonoid α] (a b c : α) : gcd (b * a) (c * a) = gcd b c * normalize a := by simp only [mul_comm, gcd_mul_left] @[simp] theorem gcd_mul_right' [GCDMonoid α] (a b c : α) : Associated (gcd (b * a) (c * a)) (gcd b c * a) := by simp only [mul_comm, gcd_mul_left'] theorem gcd_eq_left_iff [NormalizedGCDMonoid α] (a b : α) (h : normalize a = a) : gcd a b = a ↔ a ∣ b := (Iff.intro fun eq => eq ▸ gcd_dvd_right _ _) fun hab => dvd_antisymm_of_normalize_eq (normalize_gcd _ _) h (gcd_dvd_left _ _) (dvd_gcd (dvd_refl a) hab) theorem gcd_eq_right_iff [NormalizedGCDMonoid α] (a b : α) (h : normalize b = b) : gcd a b = b ↔ b ∣ a := by simpa only [gcd_comm a b] using gcd_eq_left_iff b a h theorem gcd_dvd_gcd_mul_left [GCDMonoid α] (m n k : α) : gcd m n ∣ gcd (k * m) n := gcd_dvd_gcd (dvd_mul_left _ _) dvd_rfl theorem gcd_dvd_gcd_mul_right [GCDMonoid α] (m n k : α) : gcd m n ∣ gcd (m * k) n := gcd_dvd_gcd (dvd_mul_right _ _) dvd_rfl theorem gcd_dvd_gcd_mul_left_right [GCDMonoid α] (m n k : α) : gcd m n ∣ gcd m (k * n) := gcd_dvd_gcd dvd_rfl (dvd_mul_left _ _) theorem gcd_dvd_gcd_mul_right_right [GCDMonoid α] (m n k : α) : gcd m n ∣ gcd m (n * k) := gcd_dvd_gcd dvd_rfl (dvd_mul_right _ _) theorem Associated.gcd_eq_left [NormalizedGCDMonoid α] {m n : α} (h : Associated m n) (k : α) : gcd m k = gcd n k := dvd_antisymm_of_normalize_eq (normalize_gcd _ _) (normalize_gcd _ _) (gcd_dvd_gcd h.dvd dvd_rfl) (gcd_dvd_gcd h.symm.dvd dvd_rfl) theorem Associated.gcd_eq_right [NormalizedGCDMonoid α] {m n : α} (h : Associated m n) (k : α) : gcd k m = gcd k n := dvd_antisymm_of_normalize_eq (normalize_gcd _ _) (normalize_gcd _ _) (gcd_dvd_gcd dvd_rfl h.dvd) (gcd_dvd_gcd dvd_rfl h.symm.dvd) theorem dvd_gcd_mul_of_dvd_mul [GCDMonoid α] {m n k : α} (H : k ∣ m * n) : k ∣ gcd k m * n := (dvd_gcd (dvd_mul_right _ n) H).trans (gcd_mul_right' n k m).dvd theorem dvd_gcd_mul_iff_dvd_mul [GCDMonoid α] {m n k : α} : k ∣ gcd k m * n ↔ k ∣ m * n := ⟨fun h => h.trans (mul_dvd_mul (gcd_dvd_right k m) dvd_rfl), dvd_gcd_mul_of_dvd_mul⟩ theorem dvd_mul_gcd_of_dvd_mul [GCDMonoid α] {m n k : α} (H : k ∣ m * n) : k ∣ m * gcd k n := by rw [mul_comm] at H ⊢ exact dvd_gcd_mul_of_dvd_mul H theorem dvd_mul_gcd_iff_dvd_mul [GCDMonoid α] {m n k : α} : k ∣ m * gcd k n ↔ k ∣ m * n := ⟨fun h => h.trans (mul_dvd_mul dvd_rfl (gcd_dvd_right k n)), dvd_mul_gcd_of_dvd_mul⟩ /-- Represent a divisor of `m * n` as a product of a divisor of `m` and a divisor of `n`. Note: In general, this representation is highly non-unique. See `Nat.dvdProdDvdOfDvdProd` for a constructive version on `ℕ`. -/ instance [h : Nonempty (GCDMonoid α)] : DecompositionMonoid α where primal k m n H := by cases h by_cases h0 : gcd k m = 0 · rw [gcd_eq_zero_iff] at h0 rcases h0 with ⟨rfl, rfl⟩ exact ⟨0, n, dvd_refl 0, dvd_refl n, by simp⟩ · obtain ⟨a, ha⟩ := gcd_dvd_left k m refine ⟨gcd k m, a, gcd_dvd_right _ _, ?_, ha⟩ rw [← mul_dvd_mul_iff_left h0, ← ha] exact dvd_gcd_mul_of_dvd_mul H theorem gcd_mul_dvd_mul_gcd [GCDMonoid α] (k m n : α) : gcd k (m * n) ∣ gcd k m * gcd k n := by obtain ⟨m', n', hm', hn', h⟩ := exists_dvd_and_dvd_of_dvd_mul (gcd_dvd_right k (m * n)) replace h : gcd k (m * n) = m' * n' := h rw [h] have hm'n' : m' * n' ∣ k := h ▸ gcd_dvd_left _ _ apply mul_dvd_mul · have hm'k : m' ∣ k := (dvd_mul_right m' n').trans hm'n' exact dvd_gcd hm'k hm' · have hn'k : n' ∣ k := (dvd_mul_left n' m').trans hm'n' exact dvd_gcd hn'k hn' theorem gcd_pow_right_dvd_pow_gcd [GCDMonoid α] {a b : α} {k : ℕ} : gcd a (b ^ k) ∣ gcd a b ^ k := by by_cases hg : gcd a b = 0 · rw [gcd_eq_zero_iff] at hg rcases hg with ⟨rfl, rfl⟩ exact (gcd_zero_left' (0 ^ k : α)).dvd.trans (pow_dvd_pow_of_dvd (gcd_zero_left' (0 : α)).symm.dvd _) · induction k with | zero => rw [pow_zero, pow_zero]; exact (gcd_one_right' a).dvd | succ k hk => rw [pow_succ', pow_succ'] trans gcd a b * gcd a (b ^ k) · exact gcd_mul_dvd_mul_gcd a b (b ^ k) · exact (mul_dvd_mul_iff_left hg).mpr hk theorem gcd_pow_left_dvd_pow_gcd [GCDMonoid α] {a b : α} {k : ℕ} : gcd (a ^ k) b ∣ gcd a b ^ k := calc gcd (a ^ k) b ∣ gcd b (a ^ k) := (gcd_comm' _ _).dvd _ ∣ gcd b a ^ k := gcd_pow_right_dvd_pow_gcd _ ∣ gcd a b ^ k := pow_dvd_pow_of_dvd (gcd_comm' _ _).dvd _ theorem pow_dvd_of_mul_eq_pow [GCDMonoid α] {a b c d₁ d₂ : α} (ha : a ≠ 0) (hab : IsUnit (gcd a b)) {k : ℕ} (h : a * b = c ^ k) (hc : c = d₁ * d₂) (hd₁ : d₁ ∣ a) : d₁ ^ k ≠ 0 ∧ d₁ ^ k ∣ a := by have h1 : IsUnit (gcd (d₁ ^ k) b) := by apply isUnit_of_dvd_one trans gcd d₁ b ^ k · exact gcd_pow_left_dvd_pow_gcd · apply IsUnit.dvd apply IsUnit.pow apply isUnit_of_dvd_one apply dvd_trans _ hab.dvd apply gcd_dvd_gcd hd₁ (dvd_refl b) have h2 : d₁ ^ k ∣ a * b := by use d₂ ^ k rw [h, hc] exact mul_pow d₁ d₂ k rw [mul_comm] at h2 have h3 : d₁ ^ k ∣ a := by apply (dvd_gcd_mul_of_dvd_mul h2).trans rw [h1.mul_left_dvd] have h4 : d₁ ^ k ≠ 0 := by intro hdk rw [hdk] at h3 apply absurd (zero_dvd_iff.mp h3) ha exact ⟨h4, h3⟩ theorem exists_associated_pow_of_mul_eq_pow [GCDMonoid α] {a b c : α} (hab : IsUnit (gcd a b)) {k : ℕ} (h : a * b = c ^ k) : ∃ d : α, Associated (d ^ k) a := by cases subsingleton_or_nontrivial α · use 0 rw [Subsingleton.elim a (0 ^ k)] by_cases ha : a = 0 · use 0 obtain rfl | hk := eq_or_ne k 0 · simp [ha] at h · rw [ha, zero_pow hk] by_cases hb : b = 0 · use 1 rw [one_pow] apply (associated_one_iff_isUnit.mpr hab).symm.trans rw [hb] exact gcd_zero_right' a obtain rfl | hk := k.eq_zero_or_pos · use 1 rw [pow_zero] at h ⊢ use Units.mkOfMulEqOne _ _ h rw [Units.val_mkOfMulEqOne, one_mul] have hc : c ∣ a * b := by rw [h] exact dvd_pow_self _ hk.ne' obtain ⟨d₁, d₂, hd₁, hd₂, hc⟩ := exists_dvd_and_dvd_of_dvd_mul hc use d₁ obtain ⟨h0₁, ⟨a', ha'⟩⟩ := pow_dvd_of_mul_eq_pow ha hab h hc hd₁ rw [mul_comm] at h hc rw [(gcd_comm' a b).isUnit_iff] at hab obtain ⟨h0₂, ⟨b', hb'⟩⟩ := pow_dvd_of_mul_eq_pow hb hab h hc hd₂ rw [ha', hb', hc, mul_pow] at h have h' : a' * b' = 1 := by apply (mul_right_inj' h0₁).mp rw [mul_one] apply (mul_right_inj' h0₂).mp rw [← h] rw [mul_assoc, mul_comm a', ← mul_assoc _ b', ← mul_assoc b', mul_comm b'] use Units.mkOfMulEqOne _ _ h' rw [Units.val_mkOfMulEqOne, ha'] theorem exists_eq_pow_of_mul_eq_pow [GCDMonoid α] [Subsingleton αˣ] {a b c : α} (hab : IsUnit (gcd a b)) {k : ℕ} (h : a * b = c ^ k) : ∃ d : α, a = d ^ k := let ⟨d, hd⟩ := exists_associated_pow_of_mul_eq_pow hab h ⟨d, (associated_iff_eq.mp hd).symm⟩ theorem gcd_greatest {α : Type*} [CancelCommMonoidWithZero α] [NormalizedGCDMonoid α] {a b d : α} (hda : d ∣ a) (hdb : d ∣ b) (hd : ∀ e : α, e ∣ a → e ∣ b → e ∣ d) : GCDMonoid.gcd a b = normalize d := haveI h := hd _ (GCDMonoid.gcd_dvd_left a b) (GCDMonoid.gcd_dvd_right a b) gcd_eq_normalize h (GCDMonoid.dvd_gcd hda hdb) theorem gcd_greatest_associated {α : Type*} [CancelCommMonoidWithZero α] [GCDMonoid α] {a b d : α} (hda : d ∣ a) (hdb : d ∣ b) (hd : ∀ e : α, e ∣ a → e ∣ b → e ∣ d) : Associated d (GCDMonoid.gcd a b) := haveI h := hd _ (GCDMonoid.gcd_dvd_left a b) (GCDMonoid.gcd_dvd_right a b) associated_of_dvd_dvd (GCDMonoid.dvd_gcd hda hdb) h theorem isUnit_gcd_of_eq_mul_gcd {α : Type*} [CancelCommMonoidWithZero α] [GCDMonoid α] {x y x' y' : α} (ex : x = gcd x y * x') (ey : y = gcd x y * y') (h : gcd x y ≠ 0) : IsUnit (gcd x' y') := by rw [← associated_one_iff_isUnit] refine Associated.of_mul_left ?_ (Associated.refl <| gcd x y) h convert (gcd_mul_left' (gcd x y) x' y').symm using 1 rw [← ex, ← ey, mul_one] theorem extract_gcd {α : Type*} [CancelCommMonoidWithZero α] [GCDMonoid α] (x y : α) : ∃ x' y', x = gcd x y * x' ∧ y = gcd x y * y' ∧ IsUnit (gcd x' y') := by by_cases h : gcd x y = 0 · obtain ⟨rfl, rfl⟩ := (gcd_eq_zero_iff x y).1 h simp_rw [← associated_one_iff_isUnit] exact ⟨1, 1, by rw [h, zero_mul], by rw [h, zero_mul], gcd_one_left' 1⟩ obtain ⟨x', ex⟩ := gcd_dvd_left x y obtain ⟨y', ey⟩ := gcd_dvd_right x y exact ⟨x', y', ex, ey, isUnit_gcd_of_eq_mul_gcd ex ey h⟩ theorem associated_gcd_left_iff [GCDMonoid α] {x y : α} : Associated x (gcd x y) ↔ x ∣ y := ⟨fun hx => hx.dvd.trans (gcd_dvd_right x y), fun hxy => associated_of_dvd_dvd (dvd_gcd dvd_rfl hxy) (gcd_dvd_left x y)⟩ theorem associated_gcd_right_iff [GCDMonoid α] {x y : α} : Associated y (gcd x y) ↔ y ∣ x := ⟨fun hx => hx.dvd.trans (gcd_dvd_left x y), fun hxy => associated_of_dvd_dvd (dvd_gcd hxy dvd_rfl) (gcd_dvd_right x y)⟩ theorem Irreducible.isUnit_gcd_iff [GCDMonoid α] {x y : α} (hx : Irreducible x) : IsUnit (gcd x y) ↔ ¬(x ∣ y) := by rw [hx.isUnit_iff_not_associated_of_dvd (gcd_dvd_left x y), not_iff_not, associated_gcd_left_iff] theorem Irreducible.gcd_eq_one_iff [NormalizedGCDMonoid α] {x y : α} (hx : Irreducible x) : gcd x y = 1 ↔ ¬(x ∣ y) := by rw [← hx.isUnit_gcd_iff, ← normalize_eq_one, NormalizedGCDMonoid.normalize_gcd] section Neg variable [HasDistribNeg α] lemma gcd_neg' [GCDMonoid α] {a b : α} : Associated (gcd a (-b)) (gcd a b) := Associated.gcd .rfl (.neg_left .rfl) lemma gcd_neg [NormalizedGCDMonoid α] {a b : α} : gcd a (-b) = gcd a b := gcd_neg'.eq_of_normalized (normalize_gcd _ _) (normalize_gcd _ _) lemma neg_gcd' [GCDMonoid α] {a b : α} : Associated (gcd (-a) b) (gcd a b) := Associated.gcd (.neg_left .rfl) .rfl lemma neg_gcd [NormalizedGCDMonoid α] {a b : α} : gcd (-a) b = gcd a b := neg_gcd'.eq_of_normalized (normalize_gcd _ _) (normalize_gcd _ _) end Neg end GCD section LCM theorem lcm_dvd_iff [GCDMonoid α] {a b c : α} : lcm a b ∣ c ↔ a ∣ c ∧ b ∣ c := by by_cases h : a = 0 ∨ b = 0 · rcases h with (rfl | rfl) <;> simp +contextual only [iff_def, lcm_zero_left, lcm_zero_right, zero_dvd_iff, dvd_zero, eq_self_iff_true, and_true, imp_true_iff] · obtain ⟨h1, h2⟩ := not_or.1 h have h : gcd a b ≠ 0 := fun H => h1 ((gcd_eq_zero_iff _ _).1 H).1 rw [← mul_dvd_mul_iff_left h, (gcd_mul_lcm a b).dvd_iff_dvd_left, ← (gcd_mul_right' c a b).dvd_iff_dvd_right, dvd_gcd_iff, mul_comm b c, mul_dvd_mul_iff_left h1, mul_dvd_mul_iff_right h2, and_comm] theorem dvd_lcm_left [GCDMonoid α] (a b : α) : a ∣ lcm a b := (lcm_dvd_iff.1 (dvd_refl (lcm a b))).1 theorem dvd_lcm_right [GCDMonoid α] (a b : α) : b ∣ lcm a b := (lcm_dvd_iff.1 (dvd_refl (lcm a b))).2 theorem lcm_dvd [GCDMonoid α] {a b c : α} (hab : a ∣ b) (hcb : c ∣ b) : lcm a c ∣ b := lcm_dvd_iff.2 ⟨hab, hcb⟩ @[simp] theorem lcm_eq_zero_iff [GCDMonoid α] (a b : α) : lcm a b = 0 ↔ a = 0 ∨ b = 0 := Iff.intro (fun h : lcm a b = 0 => by have : Associated (a * b) 0 := (gcd_mul_lcm a b).symm.trans <| by rw [h, mul_zero] rwa [← mul_eq_zero, ← associated_zero_iff_eq_zero]) (by rintro (rfl | rfl) <;> [apply lcm_zero_left; apply lcm_zero_right]) @[simp] theorem normalize_lcm [NormalizedGCDMonoid α] (a b : α) : normalize (lcm a b) = lcm a b := NormalizedGCDMonoid.normalize_lcm a b theorem lcm_comm [NormalizedGCDMonoid α] (a b : α) : lcm a b = lcm b a := dvd_antisymm_of_normalize_eq (normalize_lcm _ _) (normalize_lcm _ _) (lcm_dvd (dvd_lcm_right _ _) (dvd_lcm_left _ _)) (lcm_dvd (dvd_lcm_right _ _) (dvd_lcm_left _ _))
theorem lcm_comm' [GCDMonoid α] (a b : α) : Associated (lcm a b) (lcm b a) := associated_of_dvd_dvd (lcm_dvd (dvd_lcm_right _ _) (dvd_lcm_left _ _)) (lcm_dvd (dvd_lcm_right _ _) (dvd_lcm_left _ _)) theorem lcm_assoc [NormalizedGCDMonoid α] (m n k : α) : lcm (lcm m n) k = lcm m (lcm n k) := dvd_antisymm_of_normalize_eq (normalize_lcm _ _) (normalize_lcm _ _) (lcm_dvd (lcm_dvd (dvd_lcm_left _ _) ((dvd_lcm_left _ _).trans (dvd_lcm_right _ _))) ((dvd_lcm_right _ _).trans (dvd_lcm_right _ _)))
Mathlib/Algebra/GCDMonoid/Basic.lean
695
703
/- Copyright (c) 2020 Kim Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kim Morrison -/ import Mathlib.CategoryTheory.Limits.Shapes.Equalizers import Mathlib.CategoryTheory.Limits.Shapes.Products import Mathlib.Topology.Sheaves.SheafCondition.PairwiseIntersections /-! # The sheaf condition in terms of an equalizer of products Here we set up the machinery for the "usual" definition of the sheaf condition, e.g. as in https://stacks.math.columbia.edu/tag/0072 in terms of an equalizer diagram where the two objects are `∏ᶜ F.obj (U i)` and `∏ᶜ F.obj (U i) ⊓ (U j)`. We show that this sheaf condition is equivalent to the "pairwise intersections" sheaf condition when the presheaf is valued in a category with products, and thereby equivalent to the default sheaf condition. -/ universe v' v u noncomputable section open CategoryTheory CategoryTheory.Limits TopologicalSpace Opposite TopologicalSpace.Opens namespace TopCat variable {C : Type u} [Category.{v} C] [HasProducts.{v'} C] variable {X : TopCat.{v'}} (F : Presheaf C X) {ι : Type v'} (U : ι → Opens X) namespace Presheaf namespace SheafConditionEqualizerProducts /-- The product of the sections of a presheaf over a family of open sets. -/ def piOpens : C := ∏ᶜ fun i : ι => F.obj (op (U i)) /-- The product of the sections of a presheaf over the pairwise intersections of a family of open sets. -/ def piInters : C := ∏ᶜ fun p : ι × ι => F.obj (op (U p.1 ⊓ U p.2)) /-- The morphism `Π F.obj (U i) ⟶ Π F.obj (U i) ⊓ (U j)` whose components are given by the restriction maps from `U i` to `U i ⊓ U j`. -/ def leftRes : piOpens F U ⟶ piInters.{v'} F U := Pi.lift fun p : ι × ι => Pi.π _ p.1 ≫ F.map (infLELeft (U p.1) (U p.2)).op /-- The morphism `Π F.obj (U i) ⟶ Π F.obj (U i) ⊓ (U j)` whose components are given by the restriction maps from `U j` to `U i ⊓ U j`. -/ def rightRes : piOpens F U ⟶ piInters.{v'} F U := Pi.lift fun p : ι × ι => Pi.π _ p.2 ≫ F.map (infLERight (U p.1) (U p.2)).op /-- The morphism `F.obj U ⟶ Π F.obj (U i)` whose components are given by the restriction maps from `U j` to `U i ⊓ U j`. -/ def res : F.obj (op (iSup U)) ⟶ piOpens.{v'} F U := Pi.lift fun i : ι => F.map (TopologicalSpace.Opens.leSupr U i).op @[simp, elementwise] theorem res_π (i : ι) : res F U ≫ limit.π _ ⟨i⟩ = F.map (Opens.leSupr U i).op := by rw [res, limit.lift_π, Fan.mk_π_app] @[elementwise] theorem w : res F U ≫ leftRes F U = res F U ≫ rightRes F U := by dsimp [res, leftRes, rightRes] -- Porting note (https://github.com/leanprover-community/mathlib4/issues/11041): `ext` can't see `limit.hom_ext` applies here: refine limit.hom_ext (fun _ => ?_) simp only [limit.lift_π, limit.lift_π_assoc, Fan.mk_π_app, Category.assoc] rw [← F.map_comp] rw [← F.map_comp] congr 1
/-- The equalizer diagram for the sheaf condition.
Mathlib/Topology/Sheaves/SheafCondition/EqualizerProducts.lean
80
81
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov, Sébastien Gouëzel -/ import Mathlib.Analysis.Normed.Operator.Banach import Mathlib.Analysis.NormedSpace.OperatorNorm.NormedSpace import Mathlib.Topology.PartialHomeomorph /-! # Non-linear maps close to affine maps In this file we study a map `f` such that `‖f x - f y - f' (x - y)‖ ≤ c * ‖x - y‖` on an open set `s`, where `f' : E →L[𝕜] F` is a continuous linear map and `c` is suitably small. Maps of this type behave like `f a + f' (x - a)` near each `a ∈ s`. When `f'` is onto, we show that `f` is locally onto. When `f'` is a continuous linear equiv, we show that `f` is a homeomorphism between `s` and `f '' s`. More precisely, we define `ApproximatesLinearOn.toPartialHomeomorph` to be a `PartialHomeomorph` with `toFun = f`, `source = s`, and `target = f '' s`. between `s` and `f '' s`. More precisely, we define `ApproximatesLinearOn.toPartialHomeomorph` to be a `PartialHomeomorph` with `toFun = f`, `source = s`, and `target = f '' s`. Maps of this type naturally appear in the proof of the inverse function theorem (see next section), and `ApproximatesLinearOn.toPartialHomeomorph` will imply that the locally inverse function and `ApproximatesLinearOn.toPartialHomeomorph` will imply that the locally inverse function exists. We define this auxiliary notion to split the proof of the inverse function theorem into small lemmas. This approach makes it possible - to prove a lower estimate on the size of the domain of the inverse function; - to reuse parts of the proofs in the case if a function is not strictly differentiable. E.g., for a function `f : E × F → G` with estimates on `f x y₁ - f x y₂` but not on `f x₁ y - f x₂ y`. ## Notations We introduce some `local notation` to make formulas shorter: * by `N` we denote `‖f'⁻¹‖`; * by `g` we denote the auxiliary contracting map `x ↦ x + f'.symm (y - f x)` used to prove that `{x | f x = y}` is nonempty. -/ open Function Set Filter Metric open scoped Topology NNReal noncomputable section variable {𝕜 : Type*} [NontriviallyNormedField 𝕜] variable {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] variable {F : Type*} [NormedAddCommGroup F] [NormedSpace 𝕜 F] variable {ε : ℝ} open Filter Metric Set open ContinuousLinearMap (id) /-- We say that `f` approximates a continuous linear map `f'` on `s` with constant `c`, if `‖f x - f y - f' (x - y)‖ ≤ c * ‖x - y‖` whenever `x, y ∈ s`. This predicate is defined to facilitate the splitting of the inverse function theorem into small lemmas. Some of these lemmas can be useful, e.g., to prove that the inverse function is defined on a specific set. -/ def ApproximatesLinearOn (f : E → F) (f' : E →L[𝕜] F) (s : Set E) (c : ℝ≥0) : Prop := ∀ x ∈ s, ∀ y ∈ s, ‖f x - f y - f' (x - y)‖ ≤ c * ‖x - y‖ @[simp] theorem approximatesLinearOn_empty (f : E → F) (f' : E →L[𝕜] F) (c : ℝ≥0) : ApproximatesLinearOn f f' ∅ c := by simp [ApproximatesLinearOn] namespace ApproximatesLinearOn variable {f : E → F} /-! First we prove some properties of a function that `ApproximatesLinearOn` a (not necessarily invertible) continuous linear map. -/ section variable {f' : E →L[𝕜] F} {s t : Set E} {c c' : ℝ≥0} theorem mono_num (hc : c ≤ c') (hf : ApproximatesLinearOn f f' s c) : ApproximatesLinearOn f f' s c' := fun x hx y hy => le_trans (hf x hx y hy) (mul_le_mul_of_nonneg_right hc <| norm_nonneg _) theorem mono_set (hst : s ⊆ t) (hf : ApproximatesLinearOn f f' t c) : ApproximatesLinearOn f f' s c := fun x hx y hy => hf x (hst hx) y (hst hy) theorem approximatesLinearOn_iff_lipschitzOnWith {f : E → F} {f' : E →L[𝕜] F} {s : Set E} {c : ℝ≥0} : ApproximatesLinearOn f f' s c ↔ LipschitzOnWith c (f - ⇑f') s := by have : ∀ x y, f x - f y - f' (x - y) = (f - f') x - (f - f') y := fun x y ↦ by simp only [map_sub, Pi.sub_apply]; abel simp only [this, lipschitzOnWith_iff_norm_sub_le, ApproximatesLinearOn] alias ⟨lipschitzOnWith, _root_.LipschitzOnWith.approximatesLinearOn⟩ := approximatesLinearOn_iff_lipschitzOnWith theorem lipschitz_sub (hf : ApproximatesLinearOn f f' s c) : LipschitzWith c fun x : s => f x - f' x := hf.lipschitzOnWith.to_restrict protected theorem lipschitz (hf : ApproximatesLinearOn f f' s c) : LipschitzWith (‖f'‖₊ + c) (s.restrict f) := by simpa only [restrict_apply, add_sub_cancel] using (f'.lipschitz.restrict s).add hf.lipschitz_sub protected theorem continuous (hf : ApproximatesLinearOn f f' s c) : Continuous (s.restrict f) := hf.lipschitz.continuous protected theorem continuousOn (hf : ApproximatesLinearOn f f' s c) : ContinuousOn f s := continuousOn_iff_continuous_restrict.2 hf.continuous end section LocallyOnto /-! We prove that a function which is linearly approximated by a continuous linear map with a nonlinear right inverse is locally onto. This will apply to the case where the approximating map is a linear equivalence, for the local inverse theorem, but also whenever the approximating map is onto, by Banach's open mapping theorem. -/ variable [CompleteSpace E] {s : Set E} {c : ℝ≥0} {f' : E →L[𝕜] F} /-- If a function is linearly approximated by a continuous linear map with a (possibly nonlinear) right inverse, then it is locally onto: a ball of an explicit radius is included in the image of the map. -/ theorem surjOn_closedBall_of_nonlinearRightInverse (hf : ApproximatesLinearOn f f' s c) (f'symm : f'.NonlinearRightInverse) {ε : ℝ} {b : E} (ε0 : 0 ≤ ε) (hε : closedBall b ε ⊆ s) : SurjOn f (closedBall b ε) (closedBall (f b) (((f'symm.nnnorm : ℝ)⁻¹ - c) * ε)) := by intro y hy rcases le_or_lt (f'symm.nnnorm : ℝ)⁻¹ c with hc | hc · refine ⟨b, by simp [ε0], ?_⟩ have : dist y (f b) ≤ 0 := (mem_closedBall.1 hy).trans (mul_nonpos_of_nonpos_of_nonneg (by linarith) ε0) simp only [dist_le_zero] at this rw [this] have If' : (0 : ℝ) < f'symm.nnnorm := by rw [← inv_pos]; exact (NNReal.coe_nonneg _).trans_lt hc have Icf' : (c : ℝ) * f'symm.nnnorm < 1 := by rwa [inv_eq_one_div, lt_div_iff₀ If'] at hc have Jf' : (f'symm.nnnorm : ℝ) ≠ 0 := ne_of_gt If' have Jcf' : (1 : ℝ) - c * f'symm.nnnorm ≠ 0 := by apply ne_of_gt; linarith /- We have to show that `y` can be written as `f x` for some `x ∈ closedBall b ε`. The idea of the proof is to apply the Banach contraction principle to the map `g : x ↦ x + f'symm (y - f x)`, as a fixed point of this map satisfies `f x = y`. When `f'symm` is a genuine linear inverse, `g` is a contracting map. In our case, since `f'symm` is nonlinear, this map is not contracting (it is not even continuous), but still the proof of the contraction theorem holds: `uₙ = gⁿ b` is a Cauchy sequence, converging exponentially fast to the desired point `x`. Instead of appealing to general results, we check this by hand. The main point is that `f (u n)` becomes exponentially close to `y`, and therefore `dist (u (n+1)) (u n)` becomes exponentally small, making it possible to get an inductive bound on `dist (u n) b`, from which one checks that `u n` stays in the ball on which one has a control. Therefore, the bound can be checked at the next step, and so on inductively. -/ set g := fun x => x + f'symm (y - f x) with hg set u := fun n : ℕ => g^[n] b with hu have usucc : ∀ n, u (n + 1) = g (u n) := by simp [hu, ← iterate_succ_apply' g _ b] -- First bound: if `f z` is close to `y`, then `g z` is close to `z` (i.e., almost a fixed point). have A : ∀ z, dist (g z) z ≤ f'symm.nnnorm * dist (f z) y := by intro z rw [dist_eq_norm, hg, add_sub_cancel_left, dist_eq_norm'] exact f'symm.bound _ -- Second bound: if `z` and `g z` are in the set with good control, then `f (g z)` becomes closer -- to `y` than `f z` was (this uses the linear approximation property, and is the reason for the -- choice of the formula for `g`). have B : ∀ z ∈ closedBall b ε, g z ∈ closedBall b ε → dist (f (g z)) y ≤ c * f'symm.nnnorm * dist (f z) y := by intro z hz hgz set v := f'symm (y - f z) calc dist (f (g z)) y = ‖f (z + v) - y‖ := by rw [dist_eq_norm] _ = ‖f (z + v) - f z - f' v + f' v - (y - f z)‖ := by congr 1; abel _ = ‖f (z + v) - f z - f' (z + v - z)‖ := by simp only [v, ContinuousLinearMap.NonlinearRightInverse.right_inv, add_sub_cancel_left, sub_add_cancel] _ ≤ c * ‖z + v - z‖ := hf _ (hε hgz) _ (hε hz) _ ≤ c * (f'symm.nnnorm * dist (f z) y) := by gcongr simpa [dist_eq_norm'] using f'symm.bound (y - f z) _ = c * f'symm.nnnorm * dist (f z) y := by ring -- Third bound: a complicated bound on `dist w b` (that will show up in the induction) is enough -- to check that `w` is in the ball on which one has controls. Will be used to check that `u n` -- belongs to this ball for all `n`. have C : ∀ (n : ℕ) (w : E), dist w b ≤ f'symm.nnnorm * (1 - ((c : ℝ) * f'symm.nnnorm) ^ n) / (1 - c * f'symm.nnnorm) * dist (f b) y → w ∈ closedBall b ε := fun n w hw ↦ by apply hw.trans rw [div_mul_eq_mul_div, div_le_iff₀]; swap; · linarith calc (f'symm.nnnorm : ℝ) * (1 - ((c : ℝ) * f'symm.nnnorm) ^ n) * dist (f b) y = f'symm.nnnorm * dist (f b) y * (1 - ((c : ℝ) * f'symm.nnnorm) ^ n) := by ring _ ≤ f'symm.nnnorm * dist (f b) y * 1 := by gcongr rw [sub_le_self_iff] positivity _ ≤ f'symm.nnnorm * (((f'symm.nnnorm : ℝ)⁻¹ - c) * ε) := by rw [mul_one] gcongr exact mem_closedBall'.1 hy _ = ε * (1 - c * f'symm.nnnorm) := by field_simp; ring /- Main inductive control: `f (u n)` becomes exponentially close to `y`, and therefore `dist (u (n+1)) (u n)` becomes exponentally small, making it possible to get an inductive bound on `dist (u n) b`, from which one checks that `u n` remains in the ball on which we have estimates. -/ have D : ∀ n : ℕ, dist (f (u n)) y ≤ ((c : ℝ) * f'symm.nnnorm) ^ n * dist (f b) y ∧ dist (u n) b ≤ f'symm.nnnorm * (1 - ((c : ℝ) * f'symm.nnnorm) ^ n) / (1 - (c : ℝ) * f'symm.nnnorm) * dist (f b) y := fun n ↦ by induction' n with n IH; · simp [hu, le_refl] rw [usucc] have Ign : dist (g (u n)) b ≤ f'symm.nnnorm * (1 - ((c : ℝ) * f'symm.nnnorm) ^ n.succ) / (1 - c * f'symm.nnnorm) * dist (f b) y := calc dist (g (u n)) b ≤ dist (g (u n)) (u n) + dist (u n) b := dist_triangle _ _ _ _ ≤ f'symm.nnnorm * dist (f (u n)) y + dist (u n) b := add_le_add (A _) le_rfl _ ≤ f'symm.nnnorm * (((c : ℝ) * f'symm.nnnorm) ^ n * dist (f b) y) + f'symm.nnnorm * (1 - ((c : ℝ) * f'symm.nnnorm) ^ n) / (1 - c * f'symm.nnnorm) * dist (f b) y := by gcongr · exact IH.1 · exact IH.2 _ = f'symm.nnnorm * (1 - ((c : ℝ) * f'symm.nnnorm) ^ n.succ) / (1 - (c : ℝ) * f'symm.nnnorm) * dist (f b) y := by field_simp [Jcf', pow_succ]; ring refine ⟨?_, Ign⟩ calc dist (f (g (u n))) y ≤ c * f'symm.nnnorm * dist (f (u n)) y := B _ (C n _ IH.2) (C n.succ _ Ign) _ ≤ (c : ℝ) * f'symm.nnnorm * (((c : ℝ) * f'symm.nnnorm) ^ n * dist (f b) y) := by gcongr apply IH.1 _ = ((c : ℝ) * f'symm.nnnorm) ^ n.succ * dist (f b) y := by simp only [pow_succ']; ring -- Deduce from the inductive bound that `uₙ` is a Cauchy sequence, therefore converging. have : CauchySeq u := by refine cauchySeq_of_le_geometric _ (↑f'symm.nnnorm * dist (f b) y) Icf' fun n ↦ ?_ calc dist (u n) (u (n + 1)) = dist (g (u n)) (u n) := by rw [usucc, dist_comm] _ ≤ f'symm.nnnorm * dist (f (u n)) y := A _ _ ≤ f'symm.nnnorm * (((c : ℝ) * f'symm.nnnorm) ^ n * dist (f b) y) := by gcongr exact (D n).1 _ = f'symm.nnnorm * dist (f b) y * ((c : ℝ) * f'symm.nnnorm) ^ n := by ring obtain ⟨x, hx⟩ : ∃ x, Tendsto u atTop (𝓝 x) := cauchySeq_tendsto_of_complete this -- As all the `uₙ` belong to the ball `closedBall b ε`, so does their limit `x`. have xmem : x ∈ closedBall b ε := isClosed_closedBall.mem_of_tendsto hx (Eventually.of_forall fun n => C n _ (D n).2) refine ⟨x, xmem, ?_⟩ -- It remains to check that `f x = y`. This follows from continuity of `f` on `closedBall b ε` -- and from the fact that `f uₙ` is converging to `y` by construction. have hx' : Tendsto u atTop (𝓝[closedBall b ε] x) := by simp only [nhdsWithin, tendsto_inf, hx, true_and, tendsto_principal] exact Eventually.of_forall fun n => C n _ (D n).2 have T1 : Tendsto (f ∘ u) atTop (𝓝 (f x)) := (hf.continuousOn.mono hε x xmem).tendsto.comp hx' have T2 : Tendsto (f ∘ u) atTop (𝓝 y) := by rw [tendsto_iff_dist_tendsto_zero] refine squeeze_zero (fun _ => dist_nonneg) (fun n => (D n).1) ?_ simpa using (tendsto_pow_atTop_nhds_zero_of_lt_one (by positivity) Icf').mul tendsto_const_nhds exact tendsto_nhds_unique T1 T2 theorem open_image (hf : ApproximatesLinearOn f f' s c) (f'symm : f'.NonlinearRightInverse) (hs : IsOpen s) (hc : Subsingleton F ∨ c < f'symm.nnnorm⁻¹) : IsOpen (f '' s) := by rcases hc with hE | hc · exact isOpen_discrete _ simp only [isOpen_iff_mem_nhds, nhds_basis_closedBall.mem_iff, forall_mem_image] at hs ⊢ intro x hx rcases hs x hx with ⟨ε, ε0, hε⟩ refine ⟨(f'symm.nnnorm⁻¹ - c) * ε, mul_pos (sub_pos.2 hc) ε0, ?_⟩ exact (hf.surjOn_closedBall_of_nonlinearRightInverse f'symm (le_of_lt ε0) hε).mono hε Subset.rfl theorem image_mem_nhds (hf : ApproximatesLinearOn f f' s c) (f'symm : f'.NonlinearRightInverse) {x : E} (hs : s ∈ 𝓝 x) (hc : Subsingleton F ∨ c < f'symm.nnnorm⁻¹) : f '' s ∈ 𝓝 (f x) := by obtain ⟨t, hts, ht, xt⟩ : ∃ t, t ⊆ s ∧ IsOpen t ∧ x ∈ t := _root_.mem_nhds_iff.1 hs have := IsOpen.mem_nhds ((hf.mono_set hts).open_image f'symm ht hc) (mem_image_of_mem _ xt) exact mem_of_superset this (image_subset _ hts) theorem map_nhds_eq (hf : ApproximatesLinearOn f f' s c) (f'symm : f'.NonlinearRightInverse) {x : E} (hs : s ∈ 𝓝 x) (hc : Subsingleton F ∨ c < f'symm.nnnorm⁻¹) : map f (𝓝 x) = 𝓝 (f x) := by refine le_antisymm ((hf.continuousOn x (mem_of_mem_nhds hs)).continuousAt hs) (le_map fun t ht => ?_) have : f '' (s ∩ t) ∈ 𝓝 (f x) := (hf.mono_set inter_subset_left).image_mem_nhds f'symm (inter_mem hs ht) hc exact mem_of_superset this (image_subset _ inter_subset_right) end LocallyOnto /-! From now on we assume that `f` approximates an invertible continuous linear map `f : E ≃L[𝕜] F`. We also assume that either `E = {0}`, or `c < ‖f'⁻¹‖⁻¹`. We use `N` as an abbreviation for `‖f'⁻¹‖`. -/ variable {f' : E ≃L[𝕜] F} {s : Set E} {c : ℝ≥0} local notation "N" => ‖(f'.symm : F →L[𝕜] E)‖₊ protected theorem antilipschitz (hf : ApproximatesLinearOn f (f' : E →L[𝕜] F) s c) (hc : Subsingleton E ∨ c < N⁻¹) : AntilipschitzWith (N⁻¹ - c)⁻¹ (s.restrict f) := by rcases hc with hE | hc · exact AntilipschitzWith.of_subsingleton convert (f'.antilipschitz.restrict s).add_lipschitzWith hf.lipschitz_sub hc simp [restrict] protected theorem injective (hf : ApproximatesLinearOn f (f' : E →L[𝕜] F) s c) (hc : Subsingleton E ∨ c < N⁻¹) : Injective (s.restrict f) := (hf.antilipschitz hc).injective protected theorem injOn (hf : ApproximatesLinearOn f (f' : E →L[𝕜] F) s c) (hc : Subsingleton E ∨ c < N⁻¹) : InjOn f s := injOn_iff_injective.2 <| hf.injective hc protected theorem surjective [CompleteSpace E] (hf : ApproximatesLinearOn f (f' : E →L[𝕜] F) univ c) (hc : Subsingleton E ∨ c < N⁻¹) : Surjective f := by rcases hc with hE | hc · haveI : Subsingleton F := (Equiv.subsingleton_congr f'.toEquiv).1 hE exact surjective_to_subsingleton _ · apply forall_of_forall_mem_closedBall (fun y : F => ∃ a, f a = y) (f 0) _ have hc' : (0 : ℝ) < N⁻¹ - c := by rw [sub_pos]; exact hc let p : ℝ → Prop := fun R => closedBall (f 0) R ⊆ Set.range f have hp : ∀ᶠ r : ℝ in atTop, p ((N⁻¹ - c) * r) := by have hr : ∀ᶠ r : ℝ in atTop, 0 ≤ r := eventually_ge_atTop 0 refine hr.mono fun r hr => Subset.trans ?_ (image_subset_range f (closedBall 0 r)) refine hf.surjOn_closedBall_of_nonlinearRightInverse f'.toNonlinearRightInverse hr ?_ exact subset_univ _ refine ((tendsto_id.const_mul_atTop hc').frequently hp.frequently).mono ?_ exact fun R h y hy => h hy /-- A map approximating a linear equivalence on a set defines a partial equivalence on this set. Should not be used outside of this file, because it is superseded by `toPartialHomeomorph` below. This is a first step towards the inverse function. -/ def toPartialEquiv (hf : ApproximatesLinearOn f (f' : E →L[𝕜] F) s c) (hc : Subsingleton E ∨ c < N⁻¹) : PartialEquiv E F := (hf.injOn hc).toPartialEquiv _ _ /-- The inverse function is continuous on `f '' s`. Use properties of `PartialHomeomorph` instead. -/ theorem inverse_continuousOn (hf : ApproximatesLinearOn f (f' : E →L[𝕜] F) s c) (hc : Subsingleton E ∨ c < N⁻¹) : ContinuousOn (hf.toPartialEquiv hc).symm (f '' s) := by apply continuousOn_iff_continuous_restrict.2 refine ((hf.antilipschitz hc).to_rightInvOn' ?_ (hf.toPartialEquiv hc).right_inv').continuous exact fun x hx => (hf.toPartialEquiv hc).map_target hx /-- The inverse function is approximated linearly on `f '' s` by `f'.symm`. -/ theorem to_inv (hf : ApproximatesLinearOn f (f' : E →L[𝕜] F) s c) (hc : Subsingleton E ∨ c < N⁻¹) : ApproximatesLinearOn (hf.toPartialEquiv hc).symm (f'.symm : F →L[𝕜] E) (f '' s) (N * (N⁻¹ - c)⁻¹ * c) := fun x hx y hy ↦ by set A := hf.toPartialEquiv hc have Af : ∀ z, A z = f z := fun z => rfl rcases (mem_image _ _ _).1 hx with ⟨x', x's, rfl⟩ rcases (mem_image _ _ _).1 hy with ⟨y', y's, rfl⟩ rw [← Af x', ← Af y', A.left_inv x's, A.left_inv y's] calc ‖x' - y' - f'.symm (A x' - A y')‖ ≤ N * ‖f' (x' - y' - f'.symm (A x' - A y'))‖ := (f' : E →L[𝕜] F).bound_of_antilipschitz f'.antilipschitz _ _ = N * ‖A y' - A x' - f' (y' - x')‖ := by congr 2 simp only [ContinuousLinearEquiv.apply_symm_apply, ContinuousLinearEquiv.map_sub] abel _ ≤ N * (c * ‖y' - x'‖) := mul_le_mul_of_nonneg_left (hf _ y's _ x's) (NNReal.coe_nonneg _)
_ ≤ N * (c * (((N⁻¹ - c)⁻¹ : ℝ≥0) * ‖A y' - A x'‖)) := by gcongr rw [← dist_eq_norm, ← dist_eq_norm] exact (hf.antilipschitz hc).le_mul_dist ⟨y', y's⟩ ⟨x', x's⟩ _ = (N * (N⁻¹ - c)⁻¹ * c : ℝ≥0) * ‖A x' - A y'‖ := by
Mathlib/Analysis/Calculus/InverseFunctionTheorem/ApproximatesLinearOn.lean
369
373
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Simon Hudon, Mario Carneiro -/ import Aesop import Mathlib.Algebra.Group.Defs import Mathlib.Data.Nat.Init import Mathlib.Data.Int.Init import Mathlib.Logic.Function.Iterate import Mathlib.Tactic.SimpRw import Mathlib.Tactic.SplitIfs /-! # Basic lemmas about semigroups, monoids, and groups This file lists various basic lemmas about semigroups, monoids, and groups. Most proofs are one-liners from the corresponding axioms. For the definitions of semigroups, monoids and groups, see `Algebra/Group/Defs.lean`. -/ assert_not_exists MonoidWithZero DenselyOrdered open Function variable {α β G M : Type*} section ite variable [Pow α β] @[to_additive (attr := simp) dite_smul] lemma pow_dite (p : Prop) [Decidable p] (a : α) (b : p → β) (c : ¬ p → β) : a ^ (if h : p then b h else c h) = if h : p then a ^ b h else a ^ c h := by split_ifs <;> rfl @[to_additive (attr := simp) smul_dite] lemma dite_pow (p : Prop) [Decidable p] (a : p → α) (b : ¬ p → α) (c : β) : (if h : p then a h else b h) ^ c = if h : p then a h ^ c else b h ^ c := by split_ifs <;> rfl @[to_additive (attr := simp) ite_smul] lemma pow_ite (p : Prop) [Decidable p] (a : α) (b c : β) : a ^ (if p then b else c) = if p then a ^ b else a ^ c := pow_dite _ _ _ _ @[to_additive (attr := simp) smul_ite] lemma ite_pow (p : Prop) [Decidable p] (a b : α) (c : β) : (if p then a else b) ^ c = if p then a ^ c else b ^ c := dite_pow _ _ _ _ set_option linter.existingAttributeWarning false in attribute [to_additive (attr := simp)] dite_smul smul_dite ite_smul smul_ite end ite section Semigroup variable [Semigroup α] @[to_additive] instance Semigroup.to_isAssociative : Std.Associative (α := α) (· * ·) := ⟨mul_assoc⟩ /-- Composing two multiplications on the left by `y` then `x` is equal to a multiplication on the left by `x * y`. -/ @[to_additive (attr := simp) "Composing two additions on the left by `y` then `x` is equal to an addition on the left by `x + y`."] theorem comp_mul_left (x y : α) : (x * ·) ∘ (y * ·) = (x * y * ·) := by ext z simp [mul_assoc] /-- Composing two multiplications on the right by `y` and `x` is equal to a multiplication on the right by `y * x`. -/ @[to_additive (attr := simp) "Composing two additions on the right by `y` and `x` is equal to an addition on the right by `y + x`."] theorem comp_mul_right (x y : α) : (· * x) ∘ (· * y) = (· * (y * x)) := by ext z simp [mul_assoc] end Semigroup @[to_additive] instance CommMagma.to_isCommutative [CommMagma G] : Std.Commutative (α := G) (· * ·) := ⟨mul_comm⟩ section MulOneClass variable [MulOneClass M] @[to_additive] theorem ite_mul_one {P : Prop} [Decidable P] {a b : M} : ite P (a * b) 1 = ite P a 1 * ite P b 1 := by by_cases h : P <;> simp [h] @[to_additive] theorem ite_one_mul {P : Prop} [Decidable P] {a b : M} : ite P 1 (a * b) = ite P 1 a * ite P 1 b := by by_cases h : P <;> simp [h] @[to_additive] theorem eq_one_iff_eq_one_of_mul_eq_one {a b : M} (h : a * b = 1) : a = 1 ↔ b = 1 := by constructor <;> (rintro rfl; simpa using h) @[to_additive] theorem one_mul_eq_id : ((1 : M) * ·) = id := funext one_mul @[to_additive] theorem mul_one_eq_id : (· * (1 : M)) = id := funext mul_one end MulOneClass section CommSemigroup variable [CommSemigroup G] @[to_additive] theorem mul_left_comm (a b c : G) : a * (b * c) = b * (a * c) := by rw [← mul_assoc, mul_comm a, mul_assoc] @[to_additive] theorem mul_right_comm (a b c : G) : a * b * c = a * c * b := by rw [mul_assoc, mul_comm b, mul_assoc] @[to_additive] theorem mul_mul_mul_comm (a b c d : G) : a * b * (c * d) = a * c * (b * d) := by simp only [mul_left_comm, mul_assoc] @[to_additive] theorem mul_rotate (a b c : G) : a * b * c = b * c * a := by simp only [mul_left_comm, mul_comm] @[to_additive] theorem mul_rotate' (a b c : G) : a * (b * c) = b * (c * a) := by simp only [mul_left_comm, mul_comm] end CommSemigroup attribute [local simp] mul_assoc sub_eq_add_neg section Monoid variable [Monoid M] {a b : M} {m n : ℕ} @[to_additive boole_nsmul] lemma pow_boole (P : Prop) [Decidable P] (a : M) : (a ^ if P then 1 else 0) = if P then a else 1 := by simp only [pow_ite, pow_one, pow_zero] @[to_additive nsmul_add_sub_nsmul] lemma pow_mul_pow_sub (a : M) (h : m ≤ n) : a ^ m * a ^ (n - m) = a ^ n := by rw [← pow_add, Nat.add_comm, Nat.sub_add_cancel h] @[to_additive sub_nsmul_nsmul_add] lemma pow_sub_mul_pow (a : M) (h : m ≤ n) : a ^ (n - m) * a ^ m = a ^ n := by rw [← pow_add, Nat.sub_add_cancel h] @[to_additive sub_one_nsmul_add] lemma mul_pow_sub_one (hn : n ≠ 0) (a : M) : a * a ^ (n - 1) = a ^ n := by rw [← pow_succ', Nat.sub_add_cancel <| Nat.one_le_iff_ne_zero.2 hn] @[to_additive add_sub_one_nsmul] lemma pow_sub_one_mul (hn : n ≠ 0) (a : M) : a ^ (n - 1) * a = a ^ n := by rw [← pow_succ, Nat.sub_add_cancel <| Nat.one_le_iff_ne_zero.2 hn] /-- If `x ^ n = 1`, then `x ^ m` is the same as `x ^ (m % n)` -/ @[to_additive nsmul_eq_mod_nsmul "If `n • x = 0`, then `m • x` is the same as `(m % n) • x`"] lemma pow_eq_pow_mod (m : ℕ) (ha : a ^ n = 1) : a ^ m = a ^ (m % n) := by calc a ^ m = a ^ (m % n + n * (m / n)) := by rw [Nat.mod_add_div] _ = a ^ (m % n) := by simp [pow_add, pow_mul, ha] @[to_additive] lemma pow_mul_pow_eq_one : ∀ n, a * b = 1 → a ^ n * b ^ n = 1 | 0, _ => by simp | n + 1, h => calc a ^ n.succ * b ^ n.succ = a ^ n * a * (b * b ^ n) := by rw [pow_succ, pow_succ'] _ = a ^ n * (a * b) * b ^ n := by simp only [mul_assoc] _ = 1 := by simp [h, pow_mul_pow_eq_one] @[to_additive (attr := simp)] lemma mul_left_iterate (a : M) : ∀ n : ℕ, (a * ·)^[n] = (a ^ n * ·) | 0 => by ext; simp | n + 1 => by ext; simp [pow_succ, mul_left_iterate] @[to_additive (attr := simp)] lemma mul_right_iterate (a : M) : ∀ n : ℕ, (· * a)^[n] = (· * a ^ n) | 0 => by ext; simp | n + 1 => by ext; simp [pow_succ', mul_right_iterate] @[to_additive] lemma mul_left_iterate_apply_one (a : M) : (a * ·)^[n] 1 = a ^ n := by simp [mul_right_iterate] @[to_additive] lemma mul_right_iterate_apply_one (a : M) : (· * a)^[n] 1 = a ^ n := by simp [mul_right_iterate] @[to_additive (attr := simp)] lemma pow_iterate (k : ℕ) : ∀ n : ℕ, (fun x : M ↦ x ^ k)^[n] = (· ^ k ^ n) | 0 => by ext; simp | n + 1 => by ext; simp [pow_iterate, Nat.pow_succ', pow_mul] end Monoid section CommMonoid variable [CommMonoid M] {x y z : M} @[to_additive] theorem inv_unique (hy : x * y = 1) (hz : x * z = 1) : y = z := left_inv_eq_right_inv (Trans.trans (mul_comm _ _) hy) hz @[to_additive nsmul_add] lemma mul_pow (a b : M) : ∀ n, (a * b) ^ n = a ^ n * b ^ n | 0 => by rw [pow_zero, pow_zero, pow_zero, one_mul] | n + 1 => by rw [pow_succ', pow_succ', pow_succ', mul_pow, mul_mul_mul_comm] end CommMonoid section LeftCancelMonoid variable [Monoid M] [IsLeftCancelMul M] {a b : M} @[to_additive (attr := simp)] theorem mul_eq_left : a * b = a ↔ b = 1 := calc a * b = a ↔ a * b = a * 1 := by rw [mul_one] _ ↔ b = 1 := mul_left_cancel_iff @[deprecated (since := "2025-03-05")] alias mul_right_eq_self := mul_eq_left @[deprecated (since := "2025-03-05")] alias add_right_eq_self := add_eq_left set_option linter.existingAttributeWarning false in attribute [to_additive existing] mul_right_eq_self @[to_additive (attr := simp)] theorem left_eq_mul : a = a * b ↔ b = 1 := eq_comm.trans mul_eq_left @[deprecated (since := "2025-03-05")] alias self_eq_mul_right := left_eq_mul @[deprecated (since := "2025-03-05")] alias self_eq_add_right := left_eq_add set_option linter.existingAttributeWarning false in attribute [to_additive existing] self_eq_mul_right @[to_additive] theorem mul_ne_left : a * b ≠ a ↔ b ≠ 1 := mul_eq_left.not @[deprecated (since := "2025-03-05")] alias mul_right_ne_self := mul_ne_left @[deprecated (since := "2025-03-05")] alias add_right_ne_self := add_ne_left set_option linter.existingAttributeWarning false in attribute [to_additive existing] mul_right_ne_self @[to_additive] theorem left_ne_mul : a ≠ a * b ↔ b ≠ 1 := left_eq_mul.not @[deprecated (since := "2025-03-05")] alias self_ne_mul_right := left_ne_mul @[deprecated (since := "2025-03-05")] alias self_ne_add_right := left_ne_add set_option linter.existingAttributeWarning false in attribute [to_additive existing] self_ne_mul_right end LeftCancelMonoid section RightCancelMonoid variable [RightCancelMonoid M] {a b : M} @[to_additive (attr := simp)] theorem mul_eq_right : a * b = b ↔ a = 1 := calc a * b = b ↔ a * b = 1 * b := by rw [one_mul] _ ↔ a = 1 := mul_right_cancel_iff @[deprecated (since := "2025-03-05")] alias mul_left_eq_self := mul_eq_right @[deprecated (since := "2025-03-05")] alias add_left_eq_self := add_eq_right set_option linter.existingAttributeWarning false in attribute [to_additive existing] mul_left_eq_self @[to_additive (attr := simp)] theorem right_eq_mul : b = a * b ↔ a = 1 := eq_comm.trans mul_eq_right @[deprecated (since := "2025-03-05")] alias self_eq_mul_left := right_eq_mul @[deprecated (since := "2025-03-05")] alias self_eq_add_left := right_eq_add set_option linter.existingAttributeWarning false in attribute [to_additive existing] self_eq_mul_left @[to_additive] theorem mul_ne_right : a * b ≠ b ↔ a ≠ 1 := mul_eq_right.not @[deprecated (since := "2025-03-05")] alias mul_left_ne_self := mul_ne_right @[deprecated (since := "2025-03-05")] alias add_left_ne_self := add_ne_right set_option linter.existingAttributeWarning false in attribute [to_additive existing] mul_left_ne_self @[to_additive] theorem right_ne_mul : b ≠ a * b ↔ a ≠ 1 := right_eq_mul.not @[deprecated (since := "2025-03-05")] alias self_ne_mul_left := right_ne_mul @[deprecated (since := "2025-03-05")] alias self_ne_add_left := right_ne_add set_option linter.existingAttributeWarning false in attribute [to_additive existing] self_ne_mul_left end RightCancelMonoid section CancelCommMonoid variable [CancelCommMonoid α] {a b c d : α} @[to_additive] lemma eq_iff_eq_of_mul_eq_mul (h : a * b = c * d) : a = c ↔ b = d := by aesop @[to_additive] lemma ne_iff_ne_of_mul_eq_mul (h : a * b = c * d) : a ≠ c ↔ b ≠ d := by aesop end CancelCommMonoid section InvolutiveInv variable [InvolutiveInv G] {a b : G} @[to_additive (attr := simp)] theorem inv_involutive : Function.Involutive (Inv.inv : G → G) := inv_inv @[to_additive (attr := simp)] theorem inv_surjective : Function.Surjective (Inv.inv : G → G) := inv_involutive.surjective @[to_additive] theorem inv_injective : Function.Injective (Inv.inv : G → G) := inv_involutive.injective @[to_additive (attr := simp)] theorem inv_inj : a⁻¹ = b⁻¹ ↔ a = b := inv_injective.eq_iff @[to_additive] theorem inv_eq_iff_eq_inv : a⁻¹ = b ↔ a = b⁻¹ := ⟨fun h => h ▸ (inv_inv a).symm, fun h => h.symm ▸ inv_inv b⟩ variable (G) @[to_additive] theorem inv_comp_inv : Inv.inv ∘ Inv.inv = @id G := inv_involutive.comp_self @[to_additive] theorem leftInverse_inv : LeftInverse (fun a : G ↦ a⁻¹) fun a ↦ a⁻¹ := inv_inv @[to_additive] theorem rightInverse_inv : RightInverse (fun a : G ↦ a⁻¹) fun a ↦ a⁻¹ := inv_inv end InvolutiveInv section DivInvMonoid variable [DivInvMonoid G] @[to_additive] theorem mul_one_div (x y : G) : x * (1 / y) = x / y := by rw [div_eq_mul_inv, one_mul, div_eq_mul_inv] @[to_additive, field_simps] -- The attributes are out of order on purpose theorem mul_div_assoc' (a b c : G) : a * (b / c) = a * b / c := (mul_div_assoc _ _ _).symm @[to_additive] theorem mul_div (a b c : G) : a * (b / c) = a * b / c := by simp only [mul_assoc, div_eq_mul_inv] @[to_additive] theorem div_eq_mul_one_div (a b : G) : a / b = a * (1 / b) := by rw [div_eq_mul_inv, one_div] end DivInvMonoid section DivInvOneMonoid variable [DivInvOneMonoid G] @[to_additive (attr := simp)] theorem div_one (a : G) : a / 1 = a := by simp [div_eq_mul_inv] @[to_additive] theorem one_div_one : (1 : G) / 1 = 1 := div_one _ end DivInvOneMonoid section DivisionMonoid variable [DivisionMonoid α] {a b c d : α} attribute [local simp] mul_assoc div_eq_mul_inv @[to_additive] theorem eq_inv_of_mul_eq_one_right (h : a * b = 1) : b = a⁻¹ := (inv_eq_of_mul_eq_one_right h).symm @[to_additive] theorem eq_one_div_of_mul_eq_one_left (h : b * a = 1) : b = 1 / a := by rw [eq_inv_of_mul_eq_one_left h, one_div] @[to_additive] theorem eq_one_div_of_mul_eq_one_right (h : a * b = 1) : b = 1 / a := by rw [eq_inv_of_mul_eq_one_right h, one_div] @[to_additive] theorem eq_of_div_eq_one (h : a / b = 1) : a = b := inv_injective <| inv_eq_of_mul_eq_one_right <| by rwa [← div_eq_mul_inv] @[to_additive] lemma eq_of_inv_mul_eq_one (h : a⁻¹ * b = 1) : a = b := by simpa using eq_inv_of_mul_eq_one_left h @[to_additive] lemma eq_of_mul_inv_eq_one (h : a * b⁻¹ = 1) : a = b := by simpa using eq_inv_of_mul_eq_one_left h @[to_additive] theorem div_ne_one_of_ne : a ≠ b → a / b ≠ 1 := mt eq_of_div_eq_one variable (a b c) @[to_additive] theorem one_div_mul_one_div_rev : 1 / a * (1 / b) = 1 / (b * a) := by simp @[to_additive] theorem inv_div_left : a⁻¹ / b = (b * a)⁻¹ := by simp @[to_additive (attr := simp)] theorem inv_div : (a / b)⁻¹ = b / a := by simp @[to_additive] theorem one_div_div : 1 / (a / b) = b / a := by simp @[to_additive] theorem one_div_one_div : 1 / (1 / a) = a := by simp @[to_additive] theorem div_eq_div_iff_comm : a / b = c / d ↔ b / a = d / c := inv_inj.symm.trans <| by simp only [inv_div] @[to_additive] instance (priority := 100) DivisionMonoid.toDivInvOneMonoid : DivInvOneMonoid α := { DivisionMonoid.toDivInvMonoid with inv_one := by simpa only [one_div, inv_inv] using (inv_div (1 : α) 1).symm } @[to_additive (attr := simp)] lemma inv_pow (a : α) : ∀ n : ℕ, a⁻¹ ^ n = (a ^ n)⁻¹ | 0 => by rw [pow_zero, pow_zero, inv_one] | n + 1 => by rw [pow_succ', pow_succ, inv_pow _ n, mul_inv_rev] -- the attributes are intentionally out of order. `smul_zero` proves `zsmul_zero`. @[to_additive zsmul_zero, simp] lemma one_zpow : ∀ n : ℤ, (1 : α) ^ n = 1 | (n : ℕ) => by rw [zpow_natCast, one_pow] | .negSucc n => by rw [zpow_negSucc, one_pow, inv_one] @[to_additive (attr := simp) neg_zsmul] lemma zpow_neg (a : α) : ∀ n : ℤ, a ^ (-n) = (a ^ n)⁻¹ | (_ + 1 : ℕ) => DivInvMonoid.zpow_neg' _ _ | 0 => by simp | Int.negSucc n => by rw [zpow_negSucc, inv_inv, ← zpow_natCast] rfl @[to_additive neg_one_zsmul_add] lemma mul_zpow_neg_one (a b : α) : (a * b) ^ (-1 : ℤ) = b ^ (-1 : ℤ) * a ^ (-1 : ℤ) := by simp only [zpow_neg, zpow_one, mul_inv_rev] @[to_additive zsmul_neg] lemma inv_zpow (a : α) : ∀ n : ℤ, a⁻¹ ^ n = (a ^ n)⁻¹ | (n : ℕ) => by rw [zpow_natCast, zpow_natCast, inv_pow] | .negSucc n => by rw [zpow_negSucc, zpow_negSucc, inv_pow] @[to_additive (attr := simp) zsmul_neg'] lemma inv_zpow' (a : α) (n : ℤ) : a⁻¹ ^ n = a ^ (-n) := by rw [inv_zpow, zpow_neg] @[to_additive nsmul_zero_sub] lemma one_div_pow (a : α) (n : ℕ) : (1 / a) ^ n = 1 / a ^ n := by simp only [one_div, inv_pow] @[to_additive zsmul_zero_sub] lemma one_div_zpow (a : α) (n : ℤ) : (1 / a) ^ n = 1 / a ^ n := by simp only [one_div, inv_zpow] variable {a b c} @[to_additive (attr := simp)] theorem inv_eq_one : a⁻¹ = 1 ↔ a = 1 := inv_injective.eq_iff' inv_one @[to_additive (attr := simp)] theorem one_eq_inv : 1 = a⁻¹ ↔ a = 1 := eq_comm.trans inv_eq_one @[to_additive] theorem inv_ne_one : a⁻¹ ≠ 1 ↔ a ≠ 1 := inv_eq_one.not @[to_additive] theorem eq_of_one_div_eq_one_div (h : 1 / a = 1 / b) : a = b := by rw [← one_div_one_div a, h, one_div_one_div] -- Note that `mul_zsmul` and `zpow_mul` have the primes swapped -- when additivised since their argument order, -- and therefore the more "natural" choice of lemma, is reversed. @[to_additive mul_zsmul'] lemma zpow_mul (a : α) : ∀ m n : ℤ, a ^ (m * n) = (a ^ m) ^ n | (m : ℕ), (n : ℕ) => by rw [zpow_natCast, zpow_natCast, ← pow_mul, ← zpow_natCast] rfl | (m : ℕ), .negSucc n => by rw [zpow_natCast, zpow_negSucc, ← pow_mul, Int.ofNat_mul_negSucc, zpow_neg, inv_inj, ← zpow_natCast] | .negSucc m, (n : ℕ) => by rw [zpow_natCast, zpow_negSucc, ← inv_pow, ← pow_mul, Int.negSucc_mul_ofNat, zpow_neg, inv_pow, inv_inj, ← zpow_natCast] | .negSucc m, .negSucc n => by rw [zpow_negSucc, zpow_negSucc, Int.negSucc_mul_negSucc, inv_pow, inv_inv, ← pow_mul, ← zpow_natCast] rfl @[to_additive mul_zsmul] lemma zpow_mul' (a : α) (m n : ℤ) : a ^ (m * n) = (a ^ n) ^ m := by rw [Int.mul_comm, zpow_mul] @[to_additive] theorem zpow_comm (a : α) (m n : ℤ) : (a ^ m) ^ n = (a ^ n) ^ m := by rw [← zpow_mul, zpow_mul'] variable (a b c) @[to_additive, field_simps] -- The attributes are out of order on purpose theorem div_div_eq_mul_div : a / (b / c) = a * c / b := by simp @[to_additive (attr := simp)] theorem div_inv_eq_mul : a / b⁻¹ = a * b := by simp @[to_additive] theorem div_mul_eq_div_div_swap : a / (b * c) = a / c / b := by simp only [mul_assoc, mul_inv_rev, div_eq_mul_inv] end DivisionMonoid section DivisionCommMonoid variable [DivisionCommMonoid α] (a b c d : α) attribute [local simp] mul_assoc mul_comm mul_left_comm div_eq_mul_inv @[to_additive neg_add] theorem mul_inv : (a * b)⁻¹ = a⁻¹ * b⁻¹ := by simp @[to_additive] theorem inv_div' : (a / b)⁻¹ = a⁻¹ / b⁻¹ := by simp @[to_additive] theorem div_eq_inv_mul : a / b = b⁻¹ * a := by simp @[to_additive] theorem inv_mul_eq_div : a⁻¹ * b = b / a := by simp @[to_additive] lemma inv_div_comm (a b : α) : a⁻¹ / b = b⁻¹ / a := by simp @[to_additive] theorem inv_mul' : (a * b)⁻¹ = a⁻¹ / b := by simp @[to_additive] theorem inv_div_inv : a⁻¹ / b⁻¹ = b / a := by simp @[to_additive] theorem inv_inv_div_inv : (a⁻¹ / b⁻¹)⁻¹ = a / b := by simp @[to_additive] theorem one_div_mul_one_div : 1 / a * (1 / b) = 1 / (a * b) := by simp @[to_additive] theorem div_right_comm : a / b / c = a / c / b := by simp @[to_additive, field_simps] theorem div_div : a / b / c = a / (b * c) := by simp @[to_additive] theorem div_mul : a / b * c = a / (b / c) := by simp @[to_additive] theorem mul_div_left_comm : a * (b / c) = b * (a / c) := by simp @[to_additive] theorem mul_div_right_comm : a * b / c = a / c * b := by simp @[to_additive] theorem div_mul_eq_div_div : a / (b * c) = a / b / c := by simp @[to_additive, field_simps] theorem div_mul_eq_mul_div : a / b * c = a * c / b := by simp @[to_additive] theorem one_div_mul_eq_div : 1 / a * b = b / a := by simp @[to_additive] theorem mul_comm_div : a / b * c = a * (c / b) := by simp @[to_additive] theorem div_mul_comm : a / b * c = c / b * a := by simp @[to_additive] theorem div_mul_eq_div_mul_one_div : a / (b * c) = a / b * (1 / c) := by simp @[to_additive] theorem div_div_div_eq : a / b / (c / d) = a * d / (b * c) := by simp @[to_additive] theorem div_div_div_comm : a / b / (c / d) = a / c / (b / d) := by simp @[to_additive] theorem div_mul_div_comm : a / b * (c / d) = a * c / (b * d) := by simp @[to_additive] theorem mul_div_mul_comm : a * b / (c * d) = a / c * (b / d) := by simp @[to_additive zsmul_add] lemma mul_zpow : ∀ n : ℤ, (a * b) ^ n = a ^ n * b ^ n | (n : ℕ) => by simp_rw [zpow_natCast, mul_pow] | .negSucc n => by simp_rw [zpow_negSucc, ← inv_pow, mul_inv, mul_pow] @[to_additive nsmul_sub] lemma div_pow (a b : α) (n : ℕ) : (a / b) ^ n = a ^ n / b ^ n := by simp only [div_eq_mul_inv, mul_pow, inv_pow] @[to_additive zsmul_sub] lemma div_zpow (a b : α) (n : ℤ) : (a / b) ^ n = a ^ n / b ^ n := by simp only [div_eq_mul_inv, mul_zpow, inv_zpow]
attribute [field_simps] div_pow div_zpow
Mathlib/Algebra/Group/Basic.lean
622
623
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Mario Carneiro, Johan Commelin, Amelia Livingston, Anne Baanen -/ import Mathlib.Algebra.Algebra.Tower import Mathlib.Algebra.Field.IsField import Mathlib.Algebra.GroupWithZero.NonZeroDivisors import Mathlib.GroupTheory.MonoidLocalization.MonoidWithZero import Mathlib.RingTheory.Localization.Defs import Mathlib.RingTheory.OreLocalization.Ring /-! # Localizations of commutative rings This file contains various basic results on localizations. We characterize the localization of a commutative ring `R` at a submonoid `M` up to isomorphism; that is, a commutative ring `S` is the localization of `R` at `M` iff we can find a ring homomorphism `f : R →+* S` satisfying 3 properties: 1. For all `y ∈ M`, `f y` is a unit; 2. For all `z : S`, there exists `(x, y) : R × M` such that `z * f y = f x`; 3. For all `x, y : R` such that `f x = f y`, there exists `c ∈ M` such that `x * c = y * c`. (The converse is a consequence of 1.) In the following, let `R, P` be commutative rings, `S, Q` be `R`- and `P`-algebras and `M, T` be submonoids of `R` and `P` respectively, e.g.: ``` variable (R S P Q : Type*) [CommRing R] [CommRing S] [CommRing P] [CommRing Q] variable [Algebra R S] [Algebra P Q] (M : Submonoid R) (T : Submonoid P) ``` ## Main definitions * `IsLocalization.algEquiv`: if `Q` is another localization of `R` at `M`, then `S` and `Q` are isomorphic as `R`-algebras ## Implementation notes In maths it is natural to reason up to isomorphism, but in Lean we cannot naturally `rewrite` one structure with an isomorphic one; one way around this is to isolate a predicate characterizing a structure up to isomorphism, and reason about things that satisfy the predicate. A previous version of this file used a fully bundled type of ring localization maps, then used a type synonym `f.codomain` for `f : LocalizationMap M S` to instantiate the `R`-algebra structure on `S`. This results in defining ad-hoc copies for everything already defined on `S`. By making `IsLocalization` a predicate on the `algebraMap R S`, we can ensure the localization map commutes nicely with other `algebraMap`s. To prove most lemmas about a localization map `algebraMap R S` in this file we invoke the corresponding proof for the underlying `CommMonoid` localization map `IsLocalization.toLocalizationMap M S`, which can be found in `GroupTheory.MonoidLocalization` and the namespace `Submonoid.LocalizationMap`. To reason about the localization as a quotient type, use `mk_eq_of_mk'` and associated lemmas. These show the quotient map `mk : R → M → Localization M` equals the surjection `LocalizationMap.mk'` induced by the map `algebraMap : R →+* Localization M`. The lemma `mk_eq_of_mk'` hence gives you access to the results in the rest of the file, which are about the `LocalizationMap.mk'` induced by any localization map. The proof that "a `CommRing` `K` which is the localization of an integral domain `R` at `R \ {0}` is a field" is a `def` rather than an `instance`, so if you want to reason about a field of fractions `K`, assume `[Field K]` instead of just `[CommRing K]`. ## Tags localization, ring localization, commutative ring localization, characteristic predicate, commutative ring, field of fractions -/ assert_not_exists Ideal open Function namespace Localization open IsLocalization variable {ι : Type*} {R : ι → Type*} [∀ i, CommSemiring (R i)] variable {i : ι} (S : Submonoid (R i)) /-- `IsLocalization.map` applied to a projection homomorphism from a product ring. -/ noncomputable abbrev mapPiEvalRingHom : Localization (S.comap <| Pi.evalRingHom R i) →+* Localization S := map (T := S) _ (Pi.evalRingHom R i) le_rfl open Function in theorem mapPiEvalRingHom_bijective : Bijective (mapPiEvalRingHom S) := by let T := S.comap (Pi.evalRingHom R i) classical refine ⟨fun x₁ x₂ eq ↦ ?_, fun x ↦ ?_⟩ · obtain ⟨r₁, s₁, rfl⟩ := mk'_surjective T x₁ obtain ⟨r₂, s₂, rfl⟩ := mk'_surjective T x₂ simp_rw [map_mk'] at eq rw [IsLocalization.eq] at eq ⊢ obtain ⟨s, hs⟩ := eq refine ⟨⟨update 0 i s, by apply update_self i s.1 0 ▸ s.2⟩, funext fun j ↦ ?_⟩ obtain rfl | ne := eq_or_ne j i · simpa using hs · simp [update_of_ne ne] · obtain ⟨r, s, rfl⟩ := mk'_surjective S x exact ⟨mk' (M := T) _ (update 0 i r) ⟨update 0 i s, by apply update_self i s.1 0 ▸ s.2⟩, by simp [map_mk']⟩ end Localization section CommSemiring variable {R : Type*} [CommSemiring R] {M N : Submonoid R} {S : Type*} [CommSemiring S] variable [Algebra R S] {P : Type*} [CommSemiring P] namespace IsLocalization section IsLocalization variable [IsLocalization M S] variable (M S) in include M in theorem linearMap_compatibleSMul (N₁ N₂) [AddCommMonoid N₁] [AddCommMonoid N₂] [Module R N₁] [Module S N₁] [Module R N₂] [Module S N₂] [IsScalarTower R S N₁] [IsScalarTower R S N₂] : LinearMap.CompatibleSMul N₁ N₂ S R where map_smul f s s' := by obtain ⟨r, m, rfl⟩ := mk'_surjective M s rw [← (map_units S m).smul_left_cancel] simp_rw [algebraMap_smul, ← map_smul, ← smul_assoc, smul_mk'_self, algebraMap_smul, map_smul] variable {g : R →+* P} (hg : ∀ y : M, IsUnit (g y)) variable (M) in include M in -- This is not an instance since the submonoid `M` would become a metavariable in typeclass search. theorem algHom_subsingleton [Algebra R P] : Subsingleton (S →ₐ[R] P) := ⟨fun f g => AlgHom.coe_ringHom_injective <| IsLocalization.ringHom_ext M <| by rw [f.comp_algebraMap, g.comp_algebraMap]⟩ section AlgEquiv variable {Q : Type*} [CommSemiring Q] [Algebra R Q] [IsLocalization M Q] section variable (M S Q) /-- If `S`, `Q` are localizations of `R` at the submonoid `M` respectively, there is an isomorphism of localizations `S ≃ₐ[R] Q`. -/ @[simps!] noncomputable def algEquiv : S ≃ₐ[R] Q := { ringEquivOfRingEquiv S Q (RingEquiv.refl R) M.map_id with commutes' := ringEquivOfRingEquiv_eq _ } end theorem algEquiv_mk' (x : R) (y : M) : algEquiv M S Q (mk' S x y) = mk' Q x y := by simp theorem algEquiv_symm_mk' (x : R) (y : M) : (algEquiv M S Q).symm (mk' Q x y) = mk' S x y := by simp variable (M) in include M in protected lemma bijective (f : S →+* Q) (hf : f.comp (algebraMap R S) = algebraMap R Q) : Function.Bijective f := (show f = IsLocalization.algEquiv M S Q by apply IsLocalization.ringHom_ext M; rw [hf]; ext; simp) ▸ (IsLocalization.algEquiv M S Q).toEquiv.bijective end AlgEquiv section liftAlgHom variable {A : Type*} [CommSemiring A] {R : Type*} [CommSemiring R] [Algebra A R] {M : Submonoid R} {S : Type*} [CommSemiring S] [Algebra A S] [Algebra R S] [IsScalarTower A R S] {P : Type*} [CommSemiring P] [Algebra A P] [IsLocalization M S] {f : R →ₐ[A] P} (hf : ∀ y : M, IsUnit (f y)) (x : S) include hf /-- `AlgHom` version of `IsLocalization.lift`. -/ noncomputable def liftAlgHom : S →ₐ[A] P where __ := lift hf commutes' r := show lift hf (algebraMap A S r) = _ by simp [IsScalarTower.algebraMap_apply A R S] theorem liftAlgHom_toRingHom : (liftAlgHom hf : S →ₐ[A] P).toRingHom = lift hf := rfl @[simp] theorem coe_liftAlgHom : ⇑(liftAlgHom hf : S →ₐ[A] P) = lift hf := rfl theorem liftAlgHom_apply : liftAlgHom hf x = lift hf x := rfl end liftAlgHom section AlgEquivOfAlgEquiv variable {A : Type*} [CommSemiring A] {R : Type*} [CommSemiring R] [Algebra A R] {M : Submonoid R} (S : Type*) [CommSemiring S] [Algebra A S] [Algebra R S] [IsScalarTower A R S] [IsLocalization M S] {P : Type*} [CommSemiring P] [Algebra A P] {T : Submonoid P} (Q : Type*) [CommSemiring Q] [Algebra A Q] [Algebra P Q] [IsScalarTower A P Q] [IsLocalization T Q] (h : R ≃ₐ[A] P) (H : Submonoid.map h M = T) include H /-- If `S`, `Q` are localizations of `R` and `P` at submonoids `M`, `T` respectively, an isomorphism `h : R ≃ₐ[A] P` such that `h(M) = T` induces an isomorphism of localizations `S ≃ₐ[A] Q`. -/ @[simps!] noncomputable def algEquivOfAlgEquiv : S ≃ₐ[A] Q where __ := ringEquivOfRingEquiv S Q h.toRingEquiv H commutes' _ := by dsimp; rw [IsScalarTower.algebraMap_apply A R S, map_eq, RingHom.coe_coe, AlgEquiv.commutes, IsScalarTower.algebraMap_apply A P Q] variable {S Q h} theorem algEquivOfAlgEquiv_eq_map : (algEquivOfAlgEquiv S Q h H : S →+* Q) = map Q (h : R →+* P) (M.le_comap_of_map_le (le_of_eq H)) := rfl theorem algEquivOfAlgEquiv_eq (x : R) : algEquivOfAlgEquiv S Q h H ((algebraMap R S) x) = algebraMap P Q (h x) := by simp set_option linter.docPrime false in theorem algEquivOfAlgEquiv_mk' (x : R) (y : M) : algEquivOfAlgEquiv S Q h H (mk' S x y) = mk' Q (h x) ⟨h y, show h y ∈ T from H ▸ Set.mem_image_of_mem h y.2⟩ := by simp [map_mk'] theorem algEquivOfAlgEquiv_symm : (algEquivOfAlgEquiv S Q h H).symm = algEquivOfAlgEquiv Q S h.symm (show Submonoid.map h.symm T = M by rw [← H, ← Submonoid.map_coe_toMulEquiv, AlgEquiv.symm_toMulEquiv, ← Submonoid.comap_equiv_eq_map_symm, ← Submonoid.map_coe_toMulEquiv, Submonoid.comap_map_eq_of_injective (h : R ≃* P).injective]) := rfl end AlgEquivOfAlgEquiv section at_units variable (R M) /-- The localization at a module of units is isomorphic to the ring. -/ noncomputable def atUnits (H : M ≤ IsUnit.submonoid R) : R ≃ₐ[R] S := by refine AlgEquiv.ofBijective (Algebra.ofId R S) ⟨?_, ?_⟩ · intro x y hxy obtain ⟨c, eq⟩ := (IsLocalization.eq_iff_exists M S).mp hxy obtain ⟨u, hu⟩ := H c.prop rwa [← hu, Units.mul_right_inj] at eq · intro y obtain ⟨⟨x, s⟩, eq⟩ := IsLocalization.surj M y obtain ⟨u, hu⟩ := H s.prop use x * u.inv dsimp [Algebra.ofId, RingHom.toFun_eq_coe, AlgHom.coe_mks] rw [RingHom.map_mul, ← eq, ← hu, mul_assoc, ← RingHom.map_mul] simp end at_units end IsLocalization section variable (M N) theorem isLocalization_of_algEquiv [Algebra R P] [IsLocalization M S] (h : S ≃ₐ[R] P) : IsLocalization M P := by constructor · intro y convert (IsLocalization.map_units S y).map h.toAlgHom.toRingHom.toMonoidHom exact (h.commutes y).symm · intro y obtain ⟨⟨x, s⟩, e⟩ := IsLocalization.surj M (h.symm y) apply_fun (show S → P from h) at e simp only [map_mul, h.apply_symm_apply, h.commutes] at e exact ⟨⟨x, s⟩, e⟩ · intro x y rw [← h.symm.toEquiv.injective.eq_iff, ← IsLocalization.eq_iff_exists M S, ← h.symm.commutes, ← h.symm.commutes] exact id theorem isLocalization_iff_of_algEquiv [Algebra R P] (h : S ≃ₐ[R] P) : IsLocalization M S ↔ IsLocalization M P := ⟨fun _ => isLocalization_of_algEquiv M h, fun _ => isLocalization_of_algEquiv M h.symm⟩ theorem isLocalization_iff_of_ringEquiv (h : S ≃+* P) : IsLocalization M S ↔ haveI := (h.toRingHom.comp <| algebraMap R S).toAlgebra; IsLocalization M P := letI := (h.toRingHom.comp <| algebraMap R S).toAlgebra isLocalization_iff_of_algEquiv M { h with commutes' := fun _ => rfl } variable (S) in /-- If an algebra is simultaneously localizations for two submonoids, then an arbitrary algebra is a localization of one submonoid iff it is a localization of the other. -/ theorem isLocalization_iff_of_isLocalization [IsLocalization M S] [IsLocalization N S] [Algebra R P] : IsLocalization M P ↔ IsLocalization N P := ⟨fun _ ↦ isLocalization_of_algEquiv N (algEquiv M S P), fun _ ↦ isLocalization_of_algEquiv M (algEquiv N S P)⟩ theorem iff_of_le_of_exists_dvd (N : Submonoid R) (h₁ : M ≤ N) (h₂ : ∀ n ∈ N, ∃ m ∈ M, n ∣ m) : IsLocalization M S ↔ IsLocalization N S := have : IsLocalization N (Localization M) := of_le_of_exists_dvd _ _ h₁ h₂ isLocalization_iff_of_isLocalization _ _ (Localization M) end variable (M) /-- If `S₁` is the localization of `R` at `M₁` and `S₂` is the localization of `R` at `M₂`, then every localization `T` of `S₂` at `M₁` is also a localization of `S₁` at `M₂`, in other words `M₁⁻¹M₂⁻¹R` can be identified with `M₂⁻¹M₁⁻¹R`. -/ lemma commutes (S₁ S₂ T : Type*) [CommSemiring S₁] [CommSemiring S₂] [CommSemiring T] [Algebra R S₁] [Algebra R S₂] [Algebra R T] [Algebra S₁ T] [Algebra S₂ T] [IsScalarTower R S₁ T] [IsScalarTower R S₂ T] (M₁ M₂ : Submonoid R) [IsLocalization M₁ S₁] [IsLocalization M₂ S₂] [IsLocalization (Algebra.algebraMapSubmonoid S₂ M₁) T] : IsLocalization (Algebra.algebraMapSubmonoid S₁ M₂) T where map_units' := by rintro ⟨m, ⟨a, ha, rfl⟩⟩ rw [← IsScalarTower.algebraMap_apply, IsScalarTower.algebraMap_apply R S₂ T] exact IsUnit.map _ (IsLocalization.map_units' ⟨a, ha⟩) surj' a := by obtain ⟨⟨y, -, m, hm, rfl⟩, hy⟩ := surj (M := Algebra.algebraMapSubmonoid S₂ M₁) a rw [← IsScalarTower.algebraMap_apply, IsScalarTower.algebraMap_apply R S₁ T] at hy obtain ⟨⟨z, n, hn⟩, hz⟩ := IsLocalization.surj (M := M₂) y have hunit : IsUnit (algebraMap R S₁ m) := map_units' ⟨m, hm⟩ use ⟨algebraMap R S₁ z * hunit.unit⁻¹, ⟨algebraMap R S₁ n, n, hn, rfl⟩⟩ rw [map_mul, ← IsScalarTower.algebraMap_apply, IsScalarTower.algebraMap_apply R S₂ T] conv_rhs => rw [← IsScalarTower.algebraMap_apply] rw [IsScalarTower.algebraMap_apply R S₂ T, ← hz, map_mul, ← hy] convert_to _ = a * (algebraMap S₂ T) ((algebraMap R S₂) n) * (algebraMap S₁ T) (((algebraMap R S₁) m) * hunit.unit⁻¹.val) · rw [map_mul] ring simp exists_of_eq {x y} hxy := by obtain ⟨r, s, d, hr, hs⟩ := IsLocalization.surj₂ M₁ S₁ x y apply_fun (· * algebraMap S₁ T (algebraMap R S₁ d)) at hxy simp_rw [← map_mul, hr, hs, ← IsScalarTower.algebraMap_apply, IsScalarTower.algebraMap_apply R S₂ T] at hxy obtain ⟨⟨-, c, hmc, rfl⟩, hc⟩ := exists_of_eq (M := Algebra.algebraMapSubmonoid S₂ M₁) hxy simp_rw [← map_mul] at hc obtain ⟨a, ha⟩ := IsLocalization.exists_of_eq (M := M₂) hc use ⟨algebraMap R S₁ a, a, a.property, rfl⟩ apply (map_units S₁ d).mul_right_cancel rw [mul_assoc, hr, mul_assoc, hs] apply (map_units S₁ ⟨c, hmc⟩).mul_right_cancel rw [← map_mul, ← map_mul, mul_assoc, mul_comm _ c, ha, map_mul, map_mul] ring end IsLocalization namespace Localization open IsLocalization theorem mk_natCast (m : ℕ) : (mk m 1 : Localization M) = m := by simpa using mk_algebraMap (R := R) (A := ℕ) _ variable [IsLocalization M S] section variable (S) (M) /-- The localization of `R` at `M` as a quotient type is isomorphic to any other localization. -/ @[simps!] noncomputable def algEquiv : Localization M ≃ₐ[R] S := IsLocalization.algEquiv M _ _ /-- The localization of a singleton is a singleton. Cannot be an instance due to metavariables. -/ noncomputable def _root_.IsLocalization.unique (R Rₘ) [CommSemiring R] [CommSemiring Rₘ] (M : Submonoid R) [Subsingleton R] [Algebra R Rₘ] [IsLocalization M Rₘ] : Unique Rₘ := have : Inhabited Rₘ := ⟨1⟩ (algEquiv M Rₘ).symm.injective.unique end nonrec theorem algEquiv_mk' (x : R) (y : M) : algEquiv M S (mk' (Localization M) x y) = mk' S x y := algEquiv_mk' _ _ nonrec theorem algEquiv_symm_mk' (x : R) (y : M) : (algEquiv M S).symm (mk' S x y) = mk' (Localization M) x y := algEquiv_symm_mk' _ _ theorem algEquiv_mk (x y) : algEquiv M S (mk x y) = mk' S x y := by rw [mk_eq_mk', algEquiv_mk'] theorem algEquiv_symm_mk (x : R) (y : M) : (algEquiv M S).symm (mk' S x y) = mk x y := by rw [mk_eq_mk', algEquiv_symm_mk'] lemma coe_algEquiv : (Localization.algEquiv M S : Localization M →+* S) = IsLocalization.map (M := M) (T := M) _ (RingHom.id R) le_rfl := rfl lemma coe_algEquiv_symm : ((Localization.algEquiv M S).symm : S →+* Localization M) = IsLocalization.map (M := M) (T := M) _ (RingHom.id R) le_rfl := rfl end Localization end CommSemiring section CommRing variable {R : Type*} [CommRing R] {M : Submonoid R} (S : Type*) [CommRing S] variable [Algebra R S] {P : Type*} [CommRing P] namespace Localization theorem mk_intCast (m : ℤ) : (mk m 1 : Localization M) = m := by simpa using mk_algebraMap (R := R) (A := ℤ) _ end Localization open IsLocalization /-- If `R` is a field, then localizing at a submonoid not containing `0` adds no new elements. -/ theorem IsField.localization_map_bijective {R Rₘ : Type*} [CommRing R] [CommRing Rₘ] {M : Submonoid R} (hM : (0 : R) ∉ M) (hR : IsField R) [Algebra R Rₘ] [IsLocalization M Rₘ] : Function.Bijective (algebraMap R Rₘ) := by letI := hR.toField replace hM := le_nonZeroDivisors_of_noZeroDivisors hM refine ⟨IsLocalization.injective _ hM, fun x => ?_⟩ obtain ⟨r, ⟨m, hm⟩, rfl⟩ := mk'_surjective M x obtain ⟨n, hn⟩ := hR.mul_inv_cancel (nonZeroDivisors.ne_zero <| hM hm) exact ⟨r * n, by rw [eq_mk'_iff_mul_eq, ← map_mul, mul_assoc, _root_.mul_comm n, hn, mul_one]⟩ /-- If `R` is a field, then localizing at a submonoid not containing `0` adds no new elements. -/ theorem Field.localization_map_bijective {K Kₘ : Type*} [Field K] [CommRing Kₘ] {M : Submonoid K} (hM : (0 : K) ∉ M) [Algebra K Kₘ] [IsLocalization M Kₘ] : Function.Bijective (algebraMap K Kₘ) := (Field.toIsField K).localization_map_bijective hM -- this looks weird due to the `letI` inside the above lemma, but trying to do it the other -- way round causes issues with defeq of instances, so this is actually easier. section Algebra variable {S} {Rₘ Sₘ : Type*} [CommRing Rₘ] [CommRing Sₘ] variable [Algebra R Rₘ] [IsLocalization M Rₘ] variable [Algebra S Sₘ] [i : IsLocalization (Algebra.algebraMapSubmonoid S M) Sₘ] include S section variable (S M) /-- Definition of the natural algebra induced by the localization of an algebra. Given an algebra `R → S`, a submonoid `R` of `M`, and a localization `Rₘ` for `M`, let `Sₘ` be the localization of `S` to the image of `M` under `algebraMap R S`. Then this is the natural algebra structure on `Rₘ → Sₘ`, such that the entire square commutes, where `localization_map.map_comp` gives the commutativity of the underlying maps. This instance can be helpful if you define `Sₘ := Localization (Algebra.algebraMapSubmonoid S M)`, however we will instead use the hypotheses `[Algebra Rₘ Sₘ] [IsScalarTower R Rₘ Sₘ]` in lemmas since the algebra structure may arise in different ways. -/ noncomputable def localizationAlgebra : Algebra Rₘ Sₘ := (map Sₘ (algebraMap R S) (show _ ≤ (Algebra.algebraMapSubmonoid S M).comap _ from M.le_comap_map) : Rₘ →+* Sₘ).toAlgebra end section variable [Algebra Rₘ Sₘ] [Algebra R Sₘ] [IsScalarTower R Rₘ Sₘ] [IsScalarTower R S Sₘ] variable (S Rₘ Sₘ) theorem IsLocalization.map_units_map_submonoid (y : M) : IsUnit (algebraMap R Sₘ y) := by rw [IsScalarTower.algebraMap_apply _ S] exact IsLocalization.map_units Sₘ ⟨algebraMap R S y, Algebra.mem_algebraMapSubmonoid_of_mem y⟩ -- can't be simp, as `S` only appears on the RHS theorem IsLocalization.algebraMap_mk' (x : R) (y : M) : algebraMap Rₘ Sₘ (IsLocalization.mk' Rₘ x y) = IsLocalization.mk' Sₘ (algebraMap R S x) ⟨algebraMap R S y, Algebra.mem_algebraMapSubmonoid_of_mem y⟩ := by rw [IsLocalization.eq_mk'_iff_mul_eq, Subtype.coe_mk, ← IsScalarTower.algebraMap_apply, ← IsScalarTower.algebraMap_apply, IsScalarTower.algebraMap_apply R Rₘ Sₘ, IsScalarTower.algebraMap_apply R Rₘ Sₘ, ← map_mul, mul_comm, IsLocalization.mul_mk'_eq_mk'_of_mul] exact congr_arg (algebraMap Rₘ Sₘ) (IsLocalization.mk'_mul_cancel_left x y) variable (M) /-- If the square below commutes, the bottom map is uniquely specified: ``` R → S ↓ ↓ Rₘ → Sₘ ``` -/ theorem IsLocalization.algebraMap_eq_map_map_submonoid : algebraMap Rₘ Sₘ = map Sₘ (algebraMap R S) (show _ ≤ (Algebra.algebraMapSubmonoid S M).comap _ from M.le_comap_map) := Eq.symm <| IsLocalization.map_unique _ (algebraMap Rₘ Sₘ) fun x => by rw [← IsScalarTower.algebraMap_apply R S Sₘ, ← IsScalarTower.algebraMap_apply R Rₘ Sₘ] /-- If the square below commutes, the bottom map is uniquely specified: ``` R → S ↓ ↓ Rₘ → Sₘ ``` -/ theorem IsLocalization.algebraMap_apply_eq_map_map_submonoid (x) : algebraMap Rₘ Sₘ x = map Sₘ (algebraMap R S) (show _ ≤ (Algebra.algebraMapSubmonoid S M).comap _ from M.le_comap_map) x := DFunLike.congr_fun (IsLocalization.algebraMap_eq_map_map_submonoid _ _ _ _) x theorem IsLocalization.lift_algebraMap_eq_algebraMap : IsLocalization.lift (M := M) (IsLocalization.map_units_map_submonoid S Sₘ) = algebraMap Rₘ Sₘ := IsLocalization.lift_unique _ fun _ => (IsScalarTower.algebraMap_apply _ _ _ _).symm end variable (Rₘ Sₘ) theorem localizationAlgebraMap_def : @algebraMap Rₘ Sₘ _ _ (localizationAlgebra M S) = map Sₘ (algebraMap R S) (show _ ≤ (Algebra.algebraMapSubmonoid S M).comap _ from M.le_comap_map) := rfl /-- Injectivity of the underlying `algebraMap` descends to the algebra induced by localization. -/ theorem localizationAlgebra_injective (hRS : Function.Injective (algebraMap R S)) : Function.Injective (@algebraMap Rₘ Sₘ _ _ (localizationAlgebra M S)) := have : IsLocalization (M.map (algebraMap R S)) Sₘ := i IsLocalization.map_injective_of_injective _ _ _ hRS end Algebra end CommRing
Mathlib/RingTheory/Localization/Basic.lean
1,098
1,099
/- Copyright (c) 2021 Rémy Degenne. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Rémy Degenne, Kexing Ying -/ import Mathlib.Probability.Notation import Mathlib.Probability.Process.Stopping /-! # Martingales A family of functions `f : ι → Ω → E` is a martingale with respect to a filtration `ℱ` if every `f i` is integrable, `f` is adapted with respect to `ℱ` and for all `i ≤ j`, `μ[f j | ℱ i] =ᵐ[μ] f i`. On the other hand, `f : ι → Ω → E` is said to be a supermartingale with respect to the filtration `ℱ` if `f i` is integrable, `f` is adapted with resepct to `ℱ` and for all `i ≤ j`, `μ[f j | ℱ i] ≤ᵐ[μ] f i`. Finally, `f : ι → Ω → E` is said to be a submartingale with respect to the filtration `ℱ` if `f i` is integrable, `f` is adapted with resepct to `ℱ` and for all `i ≤ j`, `f i ≤ᵐ[μ] μ[f j | ℱ i]`. The definitions of filtration and adapted can be found in `Probability.Process.Stopping`. ### Definitions * `MeasureTheory.Martingale f ℱ μ`: `f` is a martingale with respect to filtration `ℱ` and measure `μ`. * `MeasureTheory.Supermartingale f ℱ μ`: `f` is a supermartingale with respect to filtration `ℱ` and measure `μ`. * `MeasureTheory.Submartingale f ℱ μ`: `f` is a submartingale with respect to filtration `ℱ` and measure `μ`. ### Results * `MeasureTheory.martingale_condExp f ℱ μ`: the sequence `fun i => μ[f | ℱ i, ℱ.le i])` is a martingale with respect to `ℱ` and `μ`. -/ open TopologicalSpace Filter open scoped NNReal ENNReal MeasureTheory ProbabilityTheory namespace MeasureTheory variable {Ω E ι : Type*} [Preorder ι] {m0 : MeasurableSpace Ω} {μ : Measure Ω} [NormedAddCommGroup E] [NormedSpace ℝ E] [CompleteSpace E] {f g : ι → Ω → E} {ℱ : Filtration ι m0} /-- A family of functions `f : ι → Ω → E` is a martingale with respect to a filtration `ℱ` if `f` is adapted with respect to `ℱ` and for all `i ≤ j`, `μ[f j | ℱ i] =ᵐ[μ] f i`. -/ def Martingale (f : ι → Ω → E) (ℱ : Filtration ι m0) (μ : Measure Ω) : Prop := Adapted ℱ f ∧ ∀ i j, i ≤ j → μ[f j|ℱ i] =ᵐ[μ] f i /-- A family of integrable functions `f : ι → Ω → E` is a supermartingale with respect to a filtration `ℱ` if `f` is adapted with respect to `ℱ` and for all `i ≤ j`, `μ[f j | ℱ.le i] ≤ᵐ[μ] f i`. -/ def Supermartingale [LE E] (f : ι → Ω → E) (ℱ : Filtration ι m0) (μ : Measure Ω) : Prop := Adapted ℱ f ∧ (∀ i j, i ≤ j → μ[f j|ℱ i] ≤ᵐ[μ] f i) ∧ ∀ i, Integrable (f i) μ /-- A family of integrable functions `f : ι → Ω → E` is a submartingale with respect to a filtration `ℱ` if `f` is adapted with respect to `ℱ` and for all `i ≤ j`, `f i ≤ᵐ[μ] μ[f j | ℱ.le i]`. -/ def Submartingale [LE E] (f : ι → Ω → E) (ℱ : Filtration ι m0) (μ : Measure Ω) : Prop := Adapted ℱ f ∧ (∀ i j, i ≤ j → f i ≤ᵐ[μ] μ[f j|ℱ i]) ∧ ∀ i, Integrable (f i) μ theorem martingale_const (ℱ : Filtration ι m0) (μ : Measure Ω) [IsFiniteMeasure μ] (x : E) : Martingale (fun _ _ => x) ℱ μ := ⟨adapted_const ℱ _, fun i j _ => by rw [condExp_const (ℱ.le _)]⟩ theorem martingale_const_fun [OrderBot ι] (ℱ : Filtration ι m0) (μ : Measure Ω) [IsFiniteMeasure μ] {f : Ω → E} (hf : StronglyMeasurable[ℱ ⊥] f) (hfint : Integrable f μ) : Martingale (fun _ => f) ℱ μ := by refine ⟨fun i => hf.mono <| ℱ.mono bot_le, fun i j _ => ?_⟩ rw [condExp_of_stronglyMeasurable (ℱ.le _) (hf.mono <| ℱ.mono bot_le) hfint] variable (E) in theorem martingale_zero (ℱ : Filtration ι m0) (μ : Measure Ω) : Martingale (0 : ι → Ω → E) ℱ μ := ⟨adapted_zero E ℱ, fun i j _ => by rw [Pi.zero_apply, condExp_zero]; simp⟩ namespace Martingale protected theorem adapted (hf : Martingale f ℱ μ) : Adapted ℱ f := hf.1 protected theorem stronglyMeasurable (hf : Martingale f ℱ μ) (i : ι) : StronglyMeasurable[ℱ i] (f i) := hf.adapted i theorem condExp_ae_eq (hf : Martingale f ℱ μ) {i j : ι} (hij : i ≤ j) : μ[f j|ℱ i] =ᵐ[μ] f i := hf.2 i j hij @[deprecated (since := "2025-01-21")] alias condexp_ae_eq := condExp_ae_eq protected theorem integrable (hf : Martingale f ℱ μ) (i : ι) : Integrable (f i) μ := integrable_condExp.congr (hf.condExp_ae_eq (le_refl i)) theorem setIntegral_eq [SigmaFiniteFiltration μ ℱ] (hf : Martingale f ℱ μ) {i j : ι} (hij : i ≤ j) {s : Set Ω} (hs : MeasurableSet[ℱ i] s) : ∫ ω in s, f i ω ∂μ = ∫ ω in s, f j ω ∂μ := by rw [← setIntegral_condExp (ℱ.le i) (hf.integrable j) hs] refine setIntegral_congr_ae (ℱ.le i s hs) ?_ filter_upwards [hf.2 i j hij] with _ heq _ using heq.symm theorem add (hf : Martingale f ℱ μ) (hg : Martingale g ℱ μ) : Martingale (f + g) ℱ μ := by refine ⟨hf.adapted.add hg.adapted, fun i j hij => ?_⟩ exact (condExp_add (hf.integrable j) (hg.integrable j) _).trans ((hf.2 i j hij).add (hg.2 i j hij)) theorem neg (hf : Martingale f ℱ μ) : Martingale (-f) ℱ μ := ⟨hf.adapted.neg, fun i j hij => (condExp_neg ..).trans (hf.2 i j hij).neg⟩ theorem sub (hf : Martingale f ℱ μ) (hg : Martingale g ℱ μ) : Martingale (f - g) ℱ μ := by rw [sub_eq_add_neg]; exact hf.add hg.neg theorem smul (c : ℝ) (hf : Martingale f ℱ μ) : Martingale (c • f) ℱ μ := by refine ⟨hf.adapted.smul c, fun i j hij => ?_⟩ refine (condExp_smul ..).trans ((hf.2 i j hij).mono fun x hx => ?_) simp only [Pi.smul_apply, hx] theorem supermartingale [Preorder E] (hf : Martingale f ℱ μ) : Supermartingale f ℱ μ := ⟨hf.1, fun i j hij => (hf.2 i j hij).le, fun i => hf.integrable i⟩ theorem submartingale [Preorder E] (hf : Martingale f ℱ μ) : Submartingale f ℱ μ := ⟨hf.1, fun i j hij => (hf.2 i j hij).symm.le, fun i => hf.integrable i⟩ end Martingale theorem martingale_iff [PartialOrder E] : Martingale f ℱ μ ↔ Supermartingale f ℱ μ ∧ Submartingale f ℱ μ := ⟨fun hf => ⟨hf.supermartingale, hf.submartingale⟩, fun ⟨hf₁, hf₂⟩ => ⟨hf₁.1, fun i j hij => (hf₁.2.1 i j hij).antisymm (hf₂.2.1 i j hij)⟩⟩ theorem martingale_condExp (f : Ω → E) (ℱ : Filtration ι m0) (μ : Measure Ω) [SigmaFiniteFiltration μ ℱ] : Martingale (fun i => μ[f|ℱ i]) ℱ μ := ⟨fun _ => stronglyMeasurable_condExp, fun _ j hij => condExp_condExp_of_le (ℱ.mono hij) (ℱ.le j)⟩ @[deprecated (since := "2025-01-21")] alias martingale_condexp := martingale_condExp namespace Supermartingale protected theorem adapted [LE E] (hf : Supermartingale f ℱ μ) : Adapted ℱ f := hf.1 protected theorem stronglyMeasurable [LE E] (hf : Supermartingale f ℱ μ) (i : ι) : StronglyMeasurable[ℱ i] (f i) := hf.adapted i protected theorem integrable [LE E] (hf : Supermartingale f ℱ μ) (i : ι) : Integrable (f i) μ := hf.2.2 i theorem condExp_ae_le [LE E] (hf : Supermartingale f ℱ μ) {i j : ι} (hij : i ≤ j) : μ[f j|ℱ i] ≤ᵐ[μ] f i := hf.2.1 i j hij @[deprecated (since := "2025-01-21")] alias condexp_ae_le := condExp_ae_le theorem setIntegral_le [SigmaFiniteFiltration μ ℱ] {f : ι → Ω → ℝ} (hf : Supermartingale f ℱ μ) {i j : ι} (hij : i ≤ j) {s : Set Ω} (hs : MeasurableSet[ℱ i] s) : ∫ ω in s, f j ω ∂μ ≤ ∫ ω in s, f i ω ∂μ := by rw [← setIntegral_condExp (ℱ.le i) (hf.integrable j) hs] refine setIntegral_mono_ae integrable_condExp.integrableOn (hf.integrable i).integrableOn ?_ filter_upwards [hf.2.1 i j hij] with _ heq using heq theorem add [Preorder E] [AddLeftMono E] (hf : Supermartingale f ℱ μ) (hg : Supermartingale g ℱ μ) : Supermartingale (f + g) ℱ μ := by refine ⟨hf.1.add hg.1, fun i j hij => ?_, fun i => (hf.2.2 i).add (hg.2.2 i)⟩ refine (condExp_add (hf.integrable j) (hg.integrable j) _).le.trans ?_ filter_upwards [hf.2.1 i j hij, hg.2.1 i j hij] intros refine add_le_add ?_ ?_ <;> assumption theorem add_martingale [Preorder E] [AddLeftMono E] (hf : Supermartingale f ℱ μ) (hg : Martingale g ℱ μ) : Supermartingale (f + g) ℱ μ := hf.add hg.supermartingale theorem neg [Preorder E] [AddLeftMono E] (hf : Supermartingale f ℱ μ) : Submartingale (-f) ℱ μ := by refine ⟨hf.1.neg, fun i j hij => ?_, fun i => (hf.2.2 i).neg⟩ refine EventuallyLE.trans ?_ (condExp_neg ..).symm.le filter_upwards [hf.2.1 i j hij] with _ _ simpa end Supermartingale namespace Submartingale protected theorem adapted [LE E] (hf : Submartingale f ℱ μ) : Adapted ℱ f := hf.1 protected theorem stronglyMeasurable [LE E] (hf : Submartingale f ℱ μ) (i : ι) : StronglyMeasurable[ℱ i] (f i) := hf.adapted i protected theorem integrable [LE E] (hf : Submartingale f ℱ μ) (i : ι) : Integrable (f i) μ := hf.2.2 i theorem ae_le_condExp [LE E] (hf : Submartingale f ℱ μ) {i j : ι} (hij : i ≤ j) : f i ≤ᵐ[μ] μ[f j|ℱ i] := hf.2.1 i j hij @[deprecated (since := "2025-01-21")] alias ae_le_condexp := ae_le_condExp theorem add [Preorder E] [AddLeftMono E] (hf : Submartingale f ℱ μ) (hg : Submartingale g ℱ μ) : Submartingale (f + g) ℱ μ := by refine ⟨hf.1.add hg.1, fun i j hij => ?_, fun i => (hf.2.2 i).add (hg.2.2 i)⟩ refine EventuallyLE.trans ?_ (condExp_add (hf.integrable j) (hg.integrable j) _).symm.le filter_upwards [hf.2.1 i j hij, hg.2.1 i j hij] intros refine add_le_add ?_ ?_ <;> assumption theorem add_martingale [Preorder E] [AddLeftMono E] (hf : Submartingale f ℱ μ) (hg : Martingale g ℱ μ) : Submartingale (f + g) ℱ μ := hf.add hg.submartingale theorem neg [Preorder E] [AddLeftMono E] (hf : Submartingale f ℱ μ) : Supermartingale (-f) ℱ μ := by refine ⟨hf.1.neg, fun i j hij => (condExp_neg ..).le.trans ?_, fun i => (hf.2.2 i).neg⟩ filter_upwards [hf.2.1 i j hij] with _ _ simpa /-- The converse of this lemma is `MeasureTheory.submartingale_of_setIntegral_le`. -/ theorem setIntegral_le [SigmaFiniteFiltration μ ℱ] {f : ι → Ω → ℝ} (hf : Submartingale f ℱ μ) {i j : ι} (hij : i ≤ j) {s : Set Ω} (hs : MeasurableSet[ℱ i] s) : ∫ ω in s, f i ω ∂μ ≤ ∫ ω in s, f j ω ∂μ := by rw [← neg_le_neg_iff, ← integral_neg, ← integral_neg] exact Supermartingale.setIntegral_le hf.neg hij hs theorem sub_supermartingale [Preorder E] [AddLeftMono E] (hf : Submartingale f ℱ μ) (hg : Supermartingale g ℱ μ) : Submartingale (f - g) ℱ μ := by rw [sub_eq_add_neg]; exact hf.add hg.neg theorem sub_martingale [Preorder E] [AddLeftMono E] (hf : Submartingale f ℱ μ) (hg : Martingale g ℱ μ) : Submartingale (f - g) ℱ μ := hf.sub_supermartingale hg.supermartingale protected theorem sup {f g : ι → Ω → ℝ} (hf : Submartingale f ℱ μ) (hg : Submartingale g ℱ μ) : Submartingale (f ⊔ g) ℱ μ := by refine ⟨fun i => @StronglyMeasurable.sup _ _ _ _ (ℱ i) _ _ _ (hf.adapted i) (hg.adapted i), fun i j hij => ?_, fun i => Integrable.sup (hf.integrable _) (hg.integrable _)⟩ refine EventuallyLE.sup_le ?_ ?_ · exact EventuallyLE.trans (hf.2.1 i j hij) (condExp_mono (hf.integrable _) (Integrable.sup (hf.integrable j) (hg.integrable j)) (Eventually.of_forall fun x => le_max_left _ _)) · exact EventuallyLE.trans (hg.2.1 i j hij) (condExp_mono (hg.integrable _) (Integrable.sup (hf.integrable j) (hg.integrable j)) (Eventually.of_forall fun x => le_max_right _ _)) protected theorem pos {f : ι → Ω → ℝ} (hf : Submartingale f ℱ μ) : Submartingale (f⁺) ℱ μ := hf.sup (martingale_zero _ _ _).submartingale end Submartingale section Submartingale theorem submartingale_of_setIntegral_le [IsFiniteMeasure μ] {f : ι → Ω → ℝ} (hadp : Adapted ℱ f) (hint : ∀ i, Integrable (f i) μ) (hf : ∀ i j : ι, i ≤ j → ∀ s : Set Ω, MeasurableSet[ℱ i] s → ∫ ω in s, f i ω ∂μ ≤ ∫ ω in s, f j ω ∂μ) : Submartingale f ℱ μ := by refine ⟨hadp, fun i j hij => ?_, hint⟩ suffices f i ≤ᵐ[μ.trim (ℱ.le i)] μ[f j|ℱ i] by exact ae_le_of_ae_le_trim this suffices 0 ≤ᵐ[μ.trim (ℱ.le i)] μ[f j|ℱ i] - f i by filter_upwards [this] with x hx rwa [← sub_nonneg] refine ae_nonneg_of_forall_setIntegral_nonneg ((integrable_condExp.sub (hint i)).trim _ (stronglyMeasurable_condExp.sub <| hadp i)) fun s hs _ => ?_ specialize hf i j hij s hs rwa [← setIntegral_trim _ (stronglyMeasurable_condExp.sub <| hadp i) hs, integral_sub' integrable_condExp.integrableOn (hint i).integrableOn, sub_nonneg, setIntegral_condExp (ℱ.le i) (hint j) hs] theorem submartingale_of_condExp_sub_nonneg [IsFiniteMeasure μ] {f : ι → Ω → ℝ} (hadp : Adapted ℱ f) (hint : ∀ i, Integrable (f i) μ) (hf : ∀ i j, i ≤ j → 0 ≤ᵐ[μ] μ[f j - f i|ℱ i]) : Submartingale f ℱ μ := by refine ⟨hadp, fun i j hij => ?_, hint⟩ rw [← condExp_of_stronglyMeasurable (ℱ.le _) (hadp _) (hint _), ← eventually_sub_nonneg] exact EventuallyLE.trans (hf i j hij) (condExp_sub (hint _) (hint _) _).le @[deprecated (since := "2025-01-21")] alias submartingale_of_condexp_sub_nonneg := submartingale_of_condExp_sub_nonneg theorem Submartingale.condExp_sub_nonneg {f : ι → Ω → ℝ} (hf : Submartingale f ℱ μ) {i j : ι} (hij : i ≤ j) : 0 ≤ᵐ[μ] μ[f j - f i|ℱ i] := by by_cases h : SigmaFinite (μ.trim (ℱ.le i)) swap; · rw [condExp_of_not_sigmaFinite (ℱ.le i) h] refine EventuallyLE.trans ?_ (condExp_sub (hf.integrable _) (hf.integrable _) _).symm.le rw [eventually_sub_nonneg, condExp_of_stronglyMeasurable (ℱ.le _) (hf.adapted _) (hf.integrable _)] exact hf.2.1 i j hij @[deprecated (since := "2025-01-21")] alias Submartingale.condexp_sub_nonneg := Submartingale.condExp_sub_nonneg theorem submartingale_iff_condExp_sub_nonneg [IsFiniteMeasure μ] {f : ι → Ω → ℝ} : Submartingale f ℱ μ ↔ Adapted ℱ f ∧ (∀ i, Integrable (f i) μ) ∧ ∀ i j, i ≤ j → 0 ≤ᵐ[μ] μ[f j - f i|ℱ i] := ⟨fun h => ⟨h.adapted, h.integrable, fun _ _ => h.condExp_sub_nonneg⟩, fun ⟨hadp, hint, h⟩ => submartingale_of_condExp_sub_nonneg hadp hint h⟩ @[deprecated (since := "2025-01-21")] alias submartingale_iff_condexp_sub_nonneg := submartingale_iff_condExp_sub_nonneg end Submartingale namespace Supermartingale theorem sub_submartingale [Preorder E] [AddLeftMono E] (hf : Supermartingale f ℱ μ) (hg : Submartingale g ℱ μ) : Supermartingale (f - g) ℱ μ := by rw [sub_eq_add_neg]; exact hf.add hg.neg theorem sub_martingale [Preorder E] [AddLeftMono E] (hf : Supermartingale f ℱ μ) (hg : Martingale g ℱ μ) : Supermartingale (f - g) ℱ μ := hf.sub_submartingale hg.submartingale section variable {F : Type*} [NormedAddCommGroup F] [Lattice F] [NormedSpace ℝ F] [CompleteSpace F] [OrderedSMul ℝ F] theorem smul_nonneg {f : ι → Ω → F} {c : ℝ} (hc : 0 ≤ c) (hf : Supermartingale f ℱ μ) : Supermartingale (c • f) ℱ μ := by refine ⟨hf.1.smul c, fun i j hij => ?_, fun i => (hf.2.2 i).smul c⟩ filter_upwards [condExp_smul c (f j) (ℱ i), hf.2.1 i j hij] with ω hω hle simpa only [hω, Pi.smul_apply] using smul_le_smul_of_nonneg_left hle hc theorem smul_nonpos [IsOrderedAddMonoid F] {f : ι → Ω → F} {c : ℝ} (hc : c ≤ 0) (hf : Supermartingale f ℱ μ) : Submartingale (c • f) ℱ μ := by rw [← neg_neg c, (by ext (i x); simp : - -c • f = -(-c • f))] exact (hf.smul_nonneg <| neg_nonneg.2 hc).neg end end Supermartingale namespace Submartingale section variable {F : Type*} [NormedAddCommGroup F] [Lattice F] [IsOrderedAddMonoid F] [NormedSpace ℝ F] [CompleteSpace F] [OrderedSMul ℝ F] theorem smul_nonneg {f : ι → Ω → F} {c : ℝ} (hc : 0 ≤ c) (hf : Submartingale f ℱ μ) : Submartingale (c • f) ℱ μ := by rw [← neg_neg c, (by ext (i x); simp : - -c • f = -(c • -f))] exact Supermartingale.neg (hf.neg.smul_nonneg hc) theorem smul_nonpos {f : ι → Ω → F} {c : ℝ} (hc : c ≤ 0) (hf : Submartingale f ℱ μ) : Supermartingale (c • f) ℱ μ := by rw [← neg_neg c, (by ext (i x); simp : - -c • f = -(-c • f))] exact (hf.smul_nonneg <| neg_nonneg.2 hc).neg end end Submartingale section Nat variable {𝒢 : Filtration ℕ m0} theorem submartingale_of_setIntegral_le_succ [IsFiniteMeasure μ] {f : ℕ → Ω → ℝ} (hadp : Adapted 𝒢 f) (hint : ∀ i, Integrable (f i) μ) (hf : ∀ i, ∀ s : Set Ω, MeasurableSet[𝒢 i] s → ∫ ω in s, f i ω ∂μ ≤ ∫ ω in s, f (i + 1) ω ∂μ) : Submartingale f 𝒢 μ := by refine submartingale_of_setIntegral_le hadp hint fun i j hij s hs => ?_ induction' hij with k hk₁ hk₂ · exact le_rfl · exact le_trans hk₂ (hf k s (𝒢.mono hk₁ _ hs)) theorem supermartingale_of_setIntegral_succ_le [IsFiniteMeasure μ] {f : ℕ → Ω → ℝ} (hadp : Adapted 𝒢 f) (hint : ∀ i, Integrable (f i) μ) (hf : ∀ i, ∀ s : Set Ω, MeasurableSet[𝒢 i] s → ∫ ω in s, f (i + 1) ω ∂μ ≤ ∫ ω in s, f i ω ∂μ) : Supermartingale f 𝒢 μ := by rw [← neg_neg f] refine (submartingale_of_setIntegral_le_succ hadp.neg (fun i => (hint i).neg) ?_).neg simpa only [integral_neg, Pi.neg_apply, neg_le_neg_iff] theorem martingale_of_setIntegral_eq_succ [IsFiniteMeasure μ] {f : ℕ → Ω → ℝ} (hadp : Adapted 𝒢 f) (hint : ∀ i, Integrable (f i) μ) (hf : ∀ i, ∀ s : Set Ω, MeasurableSet[𝒢 i] s → ∫ ω in s, f i ω ∂μ = ∫ ω in s, f (i + 1) ω ∂μ) : Martingale f 𝒢 μ := martingale_iff.2 ⟨supermartingale_of_setIntegral_succ_le hadp hint fun i s hs => (hf i s hs).ge, submartingale_of_setIntegral_le_succ hadp hint fun i s hs => (hf i s hs).le⟩ theorem submartingale_nat [IsFiniteMeasure μ] {f : ℕ → Ω → ℝ} (hadp : Adapted 𝒢 f) (hint : ∀ i, Integrable (f i) μ) (hf : ∀ i, f i ≤ᵐ[μ] μ[f (i + 1)|𝒢 i]) : Submartingale f 𝒢 μ := by refine submartingale_of_setIntegral_le_succ hadp hint fun i s hs => ?_ have : ∫ ω in s, f (i + 1) ω ∂μ = ∫ ω in s, (μ[f (i + 1)|𝒢 i]) ω ∂μ := (setIntegral_condExp (𝒢.le i) (hint _) hs).symm rw [this] exact setIntegral_mono_ae (hint i).integrableOn integrable_condExp.integrableOn (hf i) theorem supermartingale_nat [IsFiniteMeasure μ] {f : ℕ → Ω → ℝ} (hadp : Adapted 𝒢 f) (hint : ∀ i, Integrable (f i) μ) (hf : ∀ i, μ[f (i + 1)|𝒢 i] ≤ᵐ[μ] f i) : Supermartingale f 𝒢 μ := by rw [← neg_neg f] refine (submartingale_nat hadp.neg (fun i => (hint i).neg) fun i => EventuallyLE.trans ?_ (condExp_neg ..).symm.le).neg filter_upwards [hf i] with x hx using neg_le_neg hx theorem martingale_nat [IsFiniteMeasure μ] {f : ℕ → Ω → ℝ} (hadp : Adapted 𝒢 f) (hint : ∀ i, Integrable (f i) μ) (hf : ∀ i, f i =ᵐ[μ] μ[f (i + 1)|𝒢 i]) : Martingale f 𝒢 μ := martingale_iff.2 ⟨supermartingale_nat hadp hint fun i => (hf i).symm.le, submartingale_nat hadp hint fun i => (hf i).le⟩ theorem submartingale_of_condExp_sub_nonneg_nat [IsFiniteMeasure μ] {f : ℕ → Ω → ℝ} (hadp : Adapted 𝒢 f) (hint : ∀ i, Integrable (f i) μ) (hf : ∀ i, 0 ≤ᵐ[μ] μ[f (i + 1) - f i|𝒢 i]) : Submartingale f 𝒢 μ := by refine submartingale_nat hadp hint fun i => ?_ rw [← condExp_of_stronglyMeasurable (𝒢.le _) (hadp _) (hint _), ← eventually_sub_nonneg] exact EventuallyLE.trans (hf i) (condExp_sub (hint _) (hint _) _).le @[deprecated (since := "2025-01-21")] alias submartingale_of_condexp_sub_nonneg_nat := submartingale_of_condExp_sub_nonneg_nat theorem supermartingale_of_condExp_sub_nonneg_nat [IsFiniteMeasure μ] {f : ℕ → Ω → ℝ} (hadp : Adapted 𝒢 f) (hint : ∀ i, Integrable (f i) μ) (hf : ∀ i, 0 ≤ᵐ[μ] μ[f i - f (i + 1)|𝒢 i]) : Supermartingale f 𝒢 μ := by rw [← neg_neg f] refine (submartingale_of_condExp_sub_nonneg_nat hadp.neg (fun i => (hint i).neg) ?_).neg simpa only [Pi.zero_apply, Pi.neg_apply, neg_sub_neg]
@[deprecated (since := "2025-01-21")] alias supermartingale_of_condexp_sub_nonneg_nat := supermartingale_of_condExp_sub_nonneg_nat theorem martingale_of_condExp_sub_eq_zero_nat [IsFiniteMeasure μ] {f : ℕ → Ω → ℝ} (hadp : Adapted 𝒢 f) (hint : ∀ i, Integrable (f i) μ) (hf : ∀ i, μ[f (i + 1) - f i|𝒢 i] =ᵐ[μ] 0) : Martingale f 𝒢 μ := by
Mathlib/Probability/Martingale/Basic.lean
421
427
/- Copyright (c) 2024 Dagur Asgeirsson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Dagur Asgeirsson -/ import Mathlib.CategoryTheory.Sites.Coherent.Comparison import Mathlib.CategoryTheory.Sites.Coherent.ExtensiveSheaves import Mathlib.CategoryTheory.Sites.Coherent.ReflectsPrecoherent import Mathlib.CategoryTheory.Sites.Coherent.ReflectsPreregular import Mathlib.CategoryTheory.Sites.DenseSubsite.InducedTopology import Mathlib.CategoryTheory.Sites.Whiskering /-! # Categories of coherent sheaves Given a fully faithful functor `F : C ⥤ D` into a precoherent category, which preserves and reflects finite effective epi families, and satisfies the property `F.EffectivelyEnough` (meaning that to every object in `C` there is an effective epi from an object in the image of `F`), the categories of coherent sheaves on `C` and `D` are equivalent (see `CategoryTheory.coherentTopology.equivalence`). The main application of this equivalence is the characterisation of condensed sets as coherent sheaves on either `CompHaus`, `Profinite` or `Stonean`. See the file `Condensed/Equivalence.lean` We give the corresponding result for the regular topology as well (see `CategoryTheory.regularTopology.equivalence`). -/ universe v₁ v₂ v₃ v₄ u₁ u₂ u₃ u₄ namespace CategoryTheory open Limits Functor regularTopology variable {C D : Type*} [Category C] [Category D] (F : C ⥤ D) namespace coherentTopology variable [F.PreservesFiniteEffectiveEpiFamilies] [F.ReflectsFiniteEffectiveEpiFamilies] [F.Full] [F.Faithful] [F.EffectivelyEnough] [Precoherent D] instance : F.IsCoverDense (coherentTopology _) := by refine F.isCoverDense_of_generate_singleton_functor_π_mem _ fun B ↦ ⟨_, F.effectiveEpiOver B, ?_⟩ apply Coverage.Saturate.of refine ⟨Unit, inferInstance, fun _ => F.effectiveEpiOverObj B, fun _ => F.effectiveEpiOver B, ?_ , ?_⟩ · funext; ext -- Do we want `Presieve.ext`? refine ⟨fun ⟨⟩ ↦ ⟨()⟩, ?_⟩ rintro ⟨⟩ simp · rw [← effectiveEpi_iff_effectiveEpiFamily] infer_instance theorem exists_effectiveEpiFamily_iff_mem_induced (X : C) (S : Sieve X) : (∃ (α : Type) (_ : Finite α) (Y : α → C) (π : (a : α) → (Y a ⟶ X)), EffectiveEpiFamily Y π ∧ (∀ a : α, (S.arrows) (π a)) ) ↔ (S ∈ F.inducedTopology (coherentTopology _) X) := by refine ⟨fun ⟨α, _, Y, π, ⟨H₁, H₂⟩⟩ ↦ ?_, fun hS ↦ ?_⟩ · apply (mem_sieves_iff_hasEffectiveEpiFamily (Sieve.functorPushforward _ S)).mpr refine ⟨α, inferInstance, fun i => F.obj (Y i), fun i => F.map (π i), ⟨?_, fun a => Sieve.image_mem_functorPushforward F S (H₂ a)⟩⟩ exact F.map_finite_effectiveEpiFamily _ _ · obtain ⟨α, _, Y, π, ⟨H₁, H₂⟩⟩ := (mem_sieves_iff_hasEffectiveEpiFamily _).mp hS refine ⟨α, inferInstance, ?_⟩ let Z : α → C := fun a ↦ (Functor.EffectivelyEnough.presentation (F := F) (Y a)).some.p let g₀ : (a : α) → F.obj (Z a) ⟶ Y a := fun a ↦ F.effectiveEpiOver (Y a) have : EffectiveEpiFamily _ (fun a ↦ g₀ a ≫ π a) := inferInstance refine ⟨Z , fun a ↦ F.preimage (g₀ a ≫ π a), ?_, fun a ↦ (?_ : S.arrows (F.preimage _))⟩ · refine F.finite_effectiveEpiFamily_of_map _ _ ?_ simpa using this · obtain ⟨W, g₁, g₂, h₁, h₂⟩ := H₂ a rw [h₂] convert S.downward_closed h₁ (F.preimage (g₀ a ≫ g₂)) exact F.map_injective (by simp) lemma eq_induced : haveI := F.reflects_precoherent coherentTopology C = F.inducedTopology (coherentTopology _) := by ext X S have := F.reflects_precoherent rw [← exists_effectiveEpiFamily_iff_mem_induced F X] rw [← coherentTopology.mem_sieves_iff_hasEffectiveEpiFamily S] instance : haveI := F.reflects_precoherent; F.IsDenseSubsite (coherentTopology C) (coherentTopology D) where functorPushforward_mem_iff := by rw [eq_induced F] rfl lemma coverPreserving : haveI := F.reflects_precoherent CoverPreserving (coherentTopology _) (coherentTopology _) F := IsDenseSubsite.coverPreserving _ _ _ section SheafEquiv variable {C : Type u₁} {D : Type u₂} [Category.{v₁} C] [Category.{v₂} D] (F : C ⥤ D) [F.PreservesFiniteEffectiveEpiFamilies] [F.ReflectsFiniteEffectiveEpiFamilies] [F.Full] [F.Faithful] [Precoherent D] [F.EffectivelyEnough] /-- The equivalence from coherent sheaves on `C` to coherent sheaves on `D`, given a fully faithful functor `F : C ⥤ D` to a precoherent category, which preserves and reflects effective epimorphic families, and satisfies `F.EffectivelyEnough`. -/ noncomputable def equivalence (A : Type u₃) [Category.{v₃} A] [∀ X, HasLimitsOfShape (StructuredArrow X F.op) A] : haveI := F.reflects_precoherent Sheaf (coherentTopology C) A ≌ Sheaf (coherentTopology D) A := Functor.IsDenseSubsite.sheafEquiv F _ _ _ end SheafEquiv section RegularExtensive variable {C : Type u₁} {D : Type u₂} [Category.{v₁} C] [Category.{v₂} D] (F : C ⥤ D) [F.PreservesEffectiveEpis] [F.ReflectsEffectiveEpis] [F.Full] [F.Faithful] [FinitaryExtensive D] [Preregular D] [FinitaryPreExtensive C] [PreservesFiniteCoproducts F] [F.EffectivelyEnough] /-- The equivalence from coherent sheaves on `C` to coherent sheaves on `D`, given a fully faithful functor `F : C ⥤ D` to an extensive preregular category, which preserves and reflects effective epimorphisms and satisfies `F.EffectivelyEnough`. -/ noncomputable def equivalence' (A : Type u₃) [Category.{v₃} A] [∀ X, HasLimitsOfShape (StructuredArrow X F.op) A] : haveI := F.reflects_precoherent Sheaf (coherentTopology C) A ≌ Sheaf (coherentTopology D) A := Functor.IsDenseSubsite.sheafEquiv F _ _ _ end RegularExtensive end coherentTopology namespace regularTopology variable [F.PreservesEffectiveEpis] [F.ReflectsEffectiveEpis] [F.Full] [F.Faithful] [F.EffectivelyEnough] [Preregular D] instance : F.IsCoverDense (regularTopology _) := by refine F.isCoverDense_of_generate_singleton_functor_π_mem _ fun B ↦ ⟨_, F.effectiveEpiOver B, ?_⟩ apply Coverage.Saturate.of refine ⟨F.effectiveEpiOverObj B, F.effectiveEpiOver B, ?_, inferInstance⟩ funext; ext -- Do we want `Presieve.ext`? refine ⟨fun ⟨⟩ ↦ ⟨()⟩, ?_⟩ rintro ⟨⟩ simp theorem exists_effectiveEpi_iff_mem_induced (X : C) (S : Sieve X) : (∃ (Y : C) (π : Y ⟶ X), EffectiveEpi π ∧ S.arrows π) ↔ (S ∈ F.inducedTopology (regularTopology _) X) := by refine ⟨fun ⟨Y, π, ⟨H₁, H₂⟩⟩ ↦ ?_, fun hS ↦ ?_⟩ · apply (mem_sieves_iff_hasEffectiveEpi (Sieve.functorPushforward _ S)).mpr refine ⟨F.obj Y, F.map π, ⟨?_, Sieve.image_mem_functorPushforward F S H₂⟩⟩ exact F.map_effectiveEpi _ · obtain ⟨Y, π, ⟨H₁, H₂⟩⟩ := (mem_sieves_iff_hasEffectiveEpi _).mp hS let g₀ := F.effectiveEpiOver Y refine ⟨_, F.preimage (g₀ ≫ π), ?_, (?_ : S.arrows (F.preimage _))⟩ · refine F.effectiveEpi_of_map _ ?_ simp only [map_preimage] infer_instance · obtain ⟨W, g₁, g₂, h₁, h₂⟩ := H₂ rw [h₂] convert S.downward_closed h₁ (F.preimage (g₀ ≫ g₂)) exact F.map_injective (by simp) lemma eq_induced : haveI := F.reflects_preregular regularTopology C = F.inducedTopology (regularTopology _) := by ext X S have := F.reflects_preregular rw [← exists_effectiveEpi_iff_mem_induced F X] rw [← mem_sieves_iff_hasEffectiveEpi S] instance : haveI := F.reflects_preregular; F.IsDenseSubsite (regularTopology C) (regularTopology D) where functorPushforward_mem_iff := by rw [eq_induced F] rfl lemma coverPreserving : haveI := F.reflects_preregular CoverPreserving (regularTopology _) (regularTopology _) F := IsDenseSubsite.coverPreserving _ _ _ section SheafEquiv variable {C : Type u₁} {D : Type u₂} [Category.{v₁} C] [Category.{v₂} D] (F : C ⥤ D) [F.PreservesEffectiveEpis] [F.ReflectsEffectiveEpis] [F.Full] [F.Faithful] [Preregular D] [F.EffectivelyEnough] /-- The equivalence from regular sheaves on `C` to regular sheaves on `D`, given a fully faithful functor `F : C ⥤ D` to a preregular category, which preserves and reflects effective epimorphisms and satisfies `F.EffectivelyEnough`. -/ noncomputable def equivalence (A : Type u₃) [Category.{v₃} A] [∀ X, HasLimitsOfShape (StructuredArrow X F.op) A] : haveI := F.reflects_preregular Sheaf (regularTopology C) A ≌ Sheaf (regularTopology D) A := Functor.IsDenseSubsite.sheafEquiv F _ _ _ end SheafEquiv end regularTopology namespace Presheaf variable {A : Type u₃} [Category.{v₃} A] (F : Cᵒᵖ ⥤ A) theorem isSheaf_coherent_iff_regular_and_extensive [Preregular C] [FinitaryPreExtensive C] : IsSheaf (coherentTopology C) F ↔ IsSheaf (extensiveTopology C) F ∧ IsSheaf (regularTopology C) F := by rw [← extensive_regular_generate_coherent] exact isSheaf_sup (extensiveCoverage C) (regularCoverage C) F theorem isSheaf_iff_preservesFiniteProducts_and_equalizerCondition [Preregular C] [FinitaryExtensive C] [h : ∀ {Y X : C} (f : Y ⟶ X) [EffectiveEpi f], HasPullback f f] : IsSheaf (coherentTopology C) F ↔ PreservesFiniteProducts F ∧ EqualizerCondition F := by rw [isSheaf_coherent_iff_regular_and_extensive] exact and_congr (isSheaf_iff_preservesFiniteProducts _) (@equalizerCondition_iff_isSheaf _ _ _ _ F _ h).symm noncomputable instance [Preregular C] [FinitaryExtensive C] (F : Sheaf (coherentTopology C) A) : PreservesFiniteProducts F.val := (Presheaf.isSheaf_iff_preservesFiniteProducts F.val).1 ((Presheaf.isSheaf_coherent_iff_regular_and_extensive F.val).mp F.cond).1 theorem isSheaf_iff_preservesFiniteProducts_of_projective [Preregular C] [FinitaryExtensive C] [∀ (X : C), Projective X] : IsSheaf (coherentTopology C) F ↔ PreservesFiniteProducts F := by rw [isSheaf_coherent_iff_regular_and_extensive, and_iff_left (isSheaf_of_projective F), isSheaf_iff_preservesFiniteProducts] theorem isSheaf_iff_extensiveSheaf_of_projective [Preregular C] [FinitaryExtensive C] [∀ (X : C), Projective X] : IsSheaf (coherentTopology C) F ↔ IsSheaf (extensiveTopology C) F := by rw [isSheaf_iff_preservesFiniteProducts_of_projective, isSheaf_iff_preservesFiniteProducts] /-- The categories of coherent sheaves and extensive sheaves on `C` are equivalent if `C` is preregular, finitary extensive, and every object is projective. -/ @[simps] def coherentExtensiveEquivalence [Preregular C] [FinitaryExtensive C] [∀ (X : C), Projective X] : Sheaf (coherentTopology C) A ≌ Sheaf (extensiveTopology C) A where functor := { obj := fun F ↦ ⟨F.val, (isSheaf_iff_extensiveSheaf_of_projective F.val).mp F.cond⟩ map := fun f ↦ ⟨f.val⟩ } inverse := { obj := fun F ↦ ⟨F.val, (isSheaf_iff_extensiveSheaf_of_projective F.val).mpr F.cond⟩ map := fun f ↦ ⟨f.val⟩ } unitIso := Iso.refl _ counitIso := Iso.refl _ variable {B : Type u₄} [Category.{v₄} B] variable (s : A ⥤ B) lemma isSheaf_coherent_of_hasPullbacks_comp [Preregular C] [FinitaryExtensive C] [h : ∀ {Y X : C} (f : Y ⟶ X) [EffectiveEpi f], HasPullback f f] [PreservesFiniteLimits s] (hF : IsSheaf (coherentTopology C) F) : IsSheaf (coherentTopology C) (F ⋙ s) := by rw [isSheaf_iff_preservesFiniteProducts_and_equalizerCondition (h := h)] at hF ⊢ have := hF.1 refine ⟨inferInstance, fun _ _ π _ c hc ↦ ⟨?_⟩⟩ exact isLimitForkMapOfIsLimit s _ (hF.2 π c hc).some lemma isSheaf_coherent_of_hasPullbacks_of_comp [Preregular C] [FinitaryExtensive C] [h : ∀ {Y X : C} (f : Y ⟶ X) [EffectiveEpi f], HasPullback f f] [ReflectsFiniteLimits s] (hF : IsSheaf (coherentTopology C) (F ⋙ s)) : IsSheaf (coherentTopology C) F := by rw [isSheaf_iff_preservesFiniteProducts_and_equalizerCondition (h := h)] at hF ⊢ obtain ⟨_, hF₂⟩ := hF refine ⟨⟨fun n ↦ ⟨fun {K} ↦ ⟨fun {c} hc ↦ ?_⟩⟩⟩, fun _ _ π _ c hc ↦ ⟨?_⟩⟩ · exact ⟨isLimitOfReflects s (isLimitOfPreserves (F ⋙ s) hc)⟩
· exact isLimitOfIsLimitForkMap s _ (hF₂ π c hc).some lemma isSheaf_coherent_of_projective_comp [Preregular C] [FinitaryExtensive C] [∀ (X : C), Projective X] [PreservesFiniteProducts s] (hF : IsSheaf (coherentTopology C) F) : IsSheaf (coherentTopology C) (F ⋙ s) := by rw [isSheaf_iff_preservesFiniteProducts_of_projective] at hF ⊢ infer_instance
Mathlib/CategoryTheory/Sites/Coherent/SheafComparison.lean
287
294
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Floris van Doorn, Violeta Hernández Palacios -/ import Mathlib.Algebra.GroupWithZero.Divisibility import Mathlib.Data.Nat.SuccPred import Mathlib.Order.SuccPred.InitialSeg import Mathlib.SetTheory.Ordinal.Basic /-! # Ordinal arithmetic Ordinals have an addition (corresponding to disjoint union) that turns them into an additive monoid, and a multiplication (corresponding to the lexicographic order on the product) that turns them into a monoid. One can also define correspondingly a subtraction, a division, a successor function, a power function and a logarithm function. We also define limit ordinals and prove the basic induction principle on ordinals separating successor ordinals and limit ordinals, in `limitRecOn`. ## Main definitions and results * `o₁ + o₂` is the order on the disjoint union of `o₁` and `o₂` obtained by declaring that every element of `o₁` is smaller than every element of `o₂`. * `o₁ - o₂` is the unique ordinal `o` such that `o₂ + o = o₁`, when `o₂ ≤ o₁`. * `o₁ * o₂` is the lexicographic order on `o₂ × o₁`. * `o₁ / o₂` is the ordinal `o` such that `o₁ = o₂ * o + o'` with `o' < o₂`. We also define the divisibility predicate, and a modulo operation. * `Order.succ o = o + 1` is the successor of `o`. * `pred o` if the predecessor of `o`. If `o` is not a successor, we set `pred o = o`. We discuss the properties of casts of natural numbers of and of `ω` with respect to these operations. Some properties of the operations are also used to discuss general tools on ordinals: * `IsLimit o`: an ordinal is a limit ordinal if it is neither `0` nor a successor. * `limitRecOn` is the main induction principle of ordinals: if one can prove a property by induction at successor ordinals and at limit ordinals, then it holds for all ordinals. * `IsNormal`: a function `f : Ordinal → Ordinal` satisfies `IsNormal` if it is strictly increasing and order-continuous, i.e., the image `f o` of a limit ordinal `o` is the sup of `f a` for `a < o`. Various other basic arithmetic results are given in `Principal.lean` instead. -/ assert_not_exists Field Module noncomputable section open Function Cardinal Set Equiv Order open scoped Ordinal universe u v w namespace Ordinal variable {α β γ : Type*} {r : α → α → Prop} {s : β → β → Prop} {t : γ → γ → Prop} /-! ### Further properties of addition on ordinals -/ @[simp] theorem lift_add (a b : Ordinal.{v}) : lift.{u} (a + b) = lift.{u} a + lift.{u} b := Quotient.inductionOn₂ a b fun ⟨_α, _r, _⟩ ⟨_β, _s, _⟩ => Quotient.sound ⟨(RelIso.preimage Equiv.ulift _).trans (RelIso.sumLexCongr (RelIso.preimage Equiv.ulift _) (RelIso.preimage Equiv.ulift _)).symm⟩ @[simp] theorem lift_succ (a : Ordinal.{v}) : lift.{u} (succ a) = succ (lift.{u} a) := by rw [← add_one_eq_succ, lift_add, lift_one] rfl instance instAddLeftReflectLE : AddLeftReflectLE Ordinal.{u} where elim c a b := by refine inductionOn₃ a b c fun α r _ β s _ γ t _ ⟨f⟩ ↦ ?_ have H₁ a : f (Sum.inl a) = Sum.inl a := by simpa using ((InitialSeg.leAdd t r).trans f).eq (InitialSeg.leAdd t s) a have H₂ a : ∃ b, f (Sum.inr a) = Sum.inr b := by generalize hx : f (Sum.inr a) = x obtain x | x := x · rw [← H₁, f.inj] at hx contradiction · exact ⟨x, rfl⟩ choose g hg using H₂ refine (RelEmbedding.ofMonotone g fun _ _ h ↦ ?_).ordinal_type_le rwa [← @Sum.lex_inr_inr _ t _ s, ← hg, ← hg, f.map_rel_iff, Sum.lex_inr_inr] instance : IsLeftCancelAdd Ordinal where add_left_cancel a b c h := by simpa only [le_antisymm_iff, add_le_add_iff_left] using h @[deprecated add_left_cancel_iff (since := "2024-12-11")] protected theorem add_left_cancel (a) {b c : Ordinal} : a + b = a + c ↔ b = c := add_left_cancel_iff private theorem add_lt_add_iff_left' (a) {b c : Ordinal} : a + b < a + c ↔ b < c := by rw [← not_le, ← not_le, add_le_add_iff_left] instance instAddLeftStrictMono : AddLeftStrictMono Ordinal.{u} := ⟨fun a _b _c ↦ (add_lt_add_iff_left' a).2⟩ instance instAddLeftReflectLT : AddLeftReflectLT Ordinal.{u} := ⟨fun a _b _c ↦ (add_lt_add_iff_left' a).1⟩ instance instAddRightReflectLT : AddRightReflectLT Ordinal.{u} := ⟨fun _a _b _c ↦ lt_imp_lt_of_le_imp_le fun h => add_le_add_right h _⟩ theorem add_le_add_iff_right {a b : Ordinal} : ∀ n : ℕ, a + n ≤ b + n ↔ a ≤ b | 0 => by simp | n + 1 => by simp only [natCast_succ, add_succ, add_succ, succ_le_succ_iff, add_le_add_iff_right] theorem add_right_cancel {a b : Ordinal} (n : ℕ) : a + n = b + n ↔ a = b := by simp only [le_antisymm_iff, add_le_add_iff_right] theorem add_eq_zero_iff {a b : Ordinal} : a + b = 0 ↔ a = 0 ∧ b = 0 := inductionOn₂ a b fun α r _ β s _ => by simp_rw [← type_sum_lex, type_eq_zero_iff_isEmpty] exact isEmpty_sum theorem left_eq_zero_of_add_eq_zero {a b : Ordinal} (h : a + b = 0) : a = 0 := (add_eq_zero_iff.1 h).1 theorem right_eq_zero_of_add_eq_zero {a b : Ordinal} (h : a + b = 0) : b = 0 := (add_eq_zero_iff.1 h).2 /-! ### The predecessor of an ordinal -/ open Classical in /-- The ordinal predecessor of `o` is `o'` if `o = succ o'`, and `o` otherwise. -/ def pred (o : Ordinal) : Ordinal := if h : ∃ a, o = succ a then Classical.choose h else o @[simp] theorem pred_succ (o) : pred (succ o) = o := by have h : ∃ a, succ o = succ a := ⟨_, rfl⟩ simpa only [pred, dif_pos h] using (succ_injective <| Classical.choose_spec h).symm theorem pred_le_self (o) : pred o ≤ o := by classical exact if h : ∃ a, o = succ a then by let ⟨a, e⟩ := h rw [e, pred_succ]; exact le_succ a else by rw [pred, dif_neg h] theorem pred_eq_iff_not_succ {o} : pred o = o ↔ ¬∃ a, o = succ a := ⟨fun e ⟨a, e'⟩ => by rw [e', pred_succ] at e; exact (lt_succ a).ne e, fun h => dif_neg h⟩ theorem pred_eq_iff_not_succ' {o} : pred o = o ↔ ∀ a, o ≠ succ a := by simpa using pred_eq_iff_not_succ theorem pred_lt_iff_is_succ {o} : pred o < o ↔ ∃ a, o = succ a := Iff.trans (by simp only [le_antisymm_iff, pred_le_self, true_and, not_le]) (iff_not_comm.1 pred_eq_iff_not_succ).symm @[simp] theorem pred_zero : pred 0 = 0 := pred_eq_iff_not_succ'.2 fun a => (succ_ne_zero a).symm theorem succ_pred_iff_is_succ {o} : succ (pred o) = o ↔ ∃ a, o = succ a := ⟨fun e => ⟨_, e.symm⟩, fun ⟨a, e⟩ => by simp only [e, pred_succ]⟩ theorem succ_lt_of_not_succ {o b : Ordinal} (h : ¬∃ a, o = succ a) : succ b < o ↔ b < o := ⟨(lt_succ b).trans, fun l => lt_of_le_of_ne (succ_le_of_lt l) fun e => h ⟨_, e.symm⟩⟩ theorem lt_pred {a b} : a < pred b ↔ succ a < b := by classical exact if h : ∃ a, b = succ a then by let ⟨c, e⟩ := h rw [e, pred_succ, succ_lt_succ_iff] else by simp only [pred, dif_neg h, succ_lt_of_not_succ h] theorem pred_le {a b} : pred a ≤ b ↔ a ≤ succ b := le_iff_le_iff_lt_iff_lt.2 lt_pred @[simp] theorem lift_is_succ {o : Ordinal.{v}} : (∃ a, lift.{u} o = succ a) ↔ ∃ a, o = succ a := ⟨fun ⟨a, h⟩ => let ⟨b, e⟩ := mem_range_lift_of_le <| show a ≤ lift.{u} o from le_of_lt <| h.symm ▸ lt_succ a ⟨b, (lift_inj.{u,v}).1 <| by rw [h, ← e, lift_succ]⟩, fun ⟨a, h⟩ => ⟨lift.{u} a, by simp only [h, lift_succ]⟩⟩ @[simp] theorem lift_pred (o : Ordinal.{v}) : lift.{u} (pred o) = pred (lift.{u} o) := by classical exact if h : ∃ a, o = succ a then by obtain ⟨a, e⟩ := h; simp only [e, pred_succ, lift_succ] else by rw [pred_eq_iff_not_succ.2 h, pred_eq_iff_not_succ.2 (mt lift_is_succ.1 h)] /-! ### Limit ordinals -/ /-- A limit ordinal is an ordinal which is not zero and not a successor. TODO: deprecate this in favor of `Order.IsSuccLimit`. -/ def IsLimit (o : Ordinal) : Prop := IsSuccLimit o theorem isLimit_iff {o} : IsLimit o ↔ o ≠ 0 ∧ IsSuccPrelimit o := by simp [IsLimit, IsSuccLimit] theorem IsLimit.isSuccPrelimit {o} (h : IsLimit o) : IsSuccPrelimit o := IsSuccLimit.isSuccPrelimit h theorem IsLimit.succ_lt {o a : Ordinal} (h : IsLimit o) : a < o → succ a < o := IsSuccLimit.succ_lt h theorem isSuccPrelimit_zero : IsSuccPrelimit (0 : Ordinal) := isSuccPrelimit_bot theorem not_zero_isLimit : ¬IsLimit 0 := not_isSuccLimit_bot theorem not_succ_isLimit (o) : ¬IsLimit (succ o) := not_isSuccLimit_succ o theorem not_succ_of_isLimit {o} (h : IsLimit o) : ¬∃ a, o = succ a | ⟨a, e⟩ => not_succ_isLimit a (e ▸ h) theorem succ_lt_of_isLimit {o a : Ordinal} (h : IsLimit o) : succ a < o ↔ a < o := IsSuccLimit.succ_lt_iff h theorem le_succ_of_isLimit {o} (h : IsLimit o) {a} : o ≤ succ a ↔ o ≤ a := le_iff_le_iff_lt_iff_lt.2 <| succ_lt_of_isLimit h theorem limit_le {o} (h : IsLimit o) {a} : o ≤ a ↔ ∀ x < o, x ≤ a := ⟨fun h _x l => l.le.trans h, fun H => (le_succ_of_isLimit h).1 <| le_of_not_lt fun hn => not_lt_of_le (H _ hn) (lt_succ a)⟩ theorem lt_limit {o} (h : IsLimit o) {a} : a < o ↔ ∃ x < o, a < x := by -- Porting note: `bex_def` is required. simpa only [not_forall₂, not_le, bex_def] using not_congr (@limit_le _ h a) @[simp] theorem lift_isLimit (o : Ordinal.{v}) : IsLimit (lift.{u,v} o) ↔ IsLimit o := liftInitialSeg.isSuccLimit_apply_iff theorem IsLimit.pos {o : Ordinal} (h : IsLimit o) : 0 < o := IsSuccLimit.bot_lt h theorem IsLimit.ne_zero {o : Ordinal} (h : IsLimit o) : o ≠ 0 := h.pos.ne' theorem IsLimit.one_lt {o : Ordinal} (h : IsLimit o) : 1 < o := by simpa only [succ_zero] using h.succ_lt h.pos theorem IsLimit.nat_lt {o : Ordinal} (h : IsLimit o) : ∀ n : ℕ, (n : Ordinal) < o | 0 => h.pos | n + 1 => h.succ_lt (IsLimit.nat_lt h n) theorem zero_or_succ_or_limit (o : Ordinal) : o = 0 ∨ (∃ a, o = succ a) ∨ IsLimit o := by simpa [eq_comm] using isMin_or_mem_range_succ_or_isSuccLimit o theorem isLimit_of_not_succ_of_ne_zero {o : Ordinal} (h : ¬∃ a, o = succ a) (h' : o ≠ 0) : IsLimit o := ((zero_or_succ_or_limit o).resolve_left h').resolve_left h -- TODO: this is an iff with `IsSuccPrelimit` theorem IsLimit.sSup_Iio {o : Ordinal} (h : IsLimit o) : sSup (Iio o) = o := by apply (csSup_le' (fun a ha ↦ le_of_lt ha)).antisymm apply le_of_forall_lt intro a ha exact (lt_succ a).trans_le (le_csSup bddAbove_Iio (h.succ_lt ha)) theorem IsLimit.iSup_Iio {o : Ordinal} (h : IsLimit o) : ⨆ a : Iio o, a.1 = o := by rw [← sSup_eq_iSup', h.sSup_Iio] /-- Main induction principle of ordinals: if one can prove a property by induction at successor ordinals and at limit ordinals, then it holds for all ordinals. -/ @[elab_as_elim] def limitRecOn {motive : Ordinal → Sort*} (o : Ordinal) (zero : motive 0) (succ : ∀ o, motive o → motive (succ o)) (isLimit : ∀ o, IsLimit o → (∀ o' < o, motive o') → motive o) : motive o := by refine SuccOrder.limitRecOn o (fun a ha ↦ ?_) (fun a _ ↦ succ a) isLimit convert zero simpa using ha @[simp] theorem limitRecOn_zero {motive} (H₁ H₂ H₃) : @limitRecOn motive 0 H₁ H₂ H₃ = H₁ := SuccOrder.limitRecOn_isMin _ _ _ isMin_bot @[simp] theorem limitRecOn_succ {motive} (o H₁ H₂ H₃) : @limitRecOn motive (succ o) H₁ H₂ H₃ = H₂ o (@limitRecOn motive o H₁ H₂ H₃) := SuccOrder.limitRecOn_succ .. @[simp] theorem limitRecOn_limit {motive} (o H₁ H₂ H₃ h) : @limitRecOn motive o H₁ H₂ H₃ = H₃ o h fun x _h => @limitRecOn motive x H₁ H₂ H₃ := SuccOrder.limitRecOn_of_isSuccLimit .. /-- Bounded recursion on ordinals. Similar to `limitRecOn`, with the assumption `o < l` added to all cases. The final term's domain is the ordinals below `l`. -/ @[elab_as_elim] def boundedLimitRecOn {l : Ordinal} (lLim : l.IsLimit) {motive : Iio l → Sort*} (o : Iio l) (zero : motive ⟨0, lLim.pos⟩) (succ : (o : Iio l) → motive o → motive ⟨succ o, lLim.succ_lt o.2⟩) (isLimit : (o : Iio l) → IsLimit o → (Π o' < o, motive o') → motive o) : motive o := limitRecOn (motive := fun p ↦ (h : p < l) → motive ⟨p, h⟩) o.1 (fun _ ↦ zero) (fun o ih h ↦ succ ⟨o, _⟩ <| ih <| (lt_succ o).trans h) (fun _o ho ih _ ↦ isLimit _ ho fun _o' h ↦ ih _ h _) o.2 @[simp] theorem boundedLimitRec_zero {l} (lLim : l.IsLimit) {motive} (H₁ H₂ H₃) : @boundedLimitRecOn l lLim motive ⟨0, lLim.pos⟩ H₁ H₂ H₃ = H₁ := by rw [boundedLimitRecOn, limitRecOn_zero] @[simp] theorem boundedLimitRec_succ {l} (lLim : l.IsLimit) {motive} (o H₁ H₂ H₃) : @boundedLimitRecOn l lLim motive ⟨succ o.1, lLim.succ_lt o.2⟩ H₁ H₂ H₃ = H₂ o (@boundedLimitRecOn l lLim motive o H₁ H₂ H₃) := by rw [boundedLimitRecOn, limitRecOn_succ] rfl theorem boundedLimitRec_limit {l} (lLim : l.IsLimit) {motive} (o H₁ H₂ H₃ oLim) : @boundedLimitRecOn l lLim motive o H₁ H₂ H₃ = H₃ o oLim (fun x _ ↦ @boundedLimitRecOn l lLim motive x H₁ H₂ H₃) := by rw [boundedLimitRecOn, limitRecOn_limit] rfl instance orderTopToTypeSucc (o : Ordinal) : OrderTop (succ o).toType := @OrderTop.mk _ _ (Top.mk _) le_enum_succ theorem enum_succ_eq_top {o : Ordinal} : enum (α := (succ o).toType) (· < ·) ⟨o, type_toType _ ▸ lt_succ o⟩ = ⊤ := rfl theorem has_succ_of_type_succ_lt {α} {r : α → α → Prop} [wo : IsWellOrder α r] (h : ∀ a < type r, succ a < type r) (x : α) : ∃ y, r x y := by use enum r ⟨succ (typein r x), h _ (typein_lt_type r x)⟩ convert enum_lt_enum.mpr _ · rw [enum_typein] · rw [Subtype.mk_lt_mk, lt_succ_iff] theorem toType_noMax_of_succ_lt {o : Ordinal} (ho : ∀ a < o, succ a < o) : NoMaxOrder o.toType := ⟨has_succ_of_type_succ_lt (type_toType _ ▸ ho)⟩ theorem bounded_singleton {r : α → α → Prop} [IsWellOrder α r] (hr : (type r).IsLimit) (x) : Bounded r {x} := by refine ⟨enum r ⟨succ (typein r x), hr.succ_lt (typein_lt_type r x)⟩, ?_⟩ intro b hb rw [mem_singleton_iff.1 hb] nth_rw 1 [← enum_typein r x] rw [@enum_lt_enum _ r, Subtype.mk_lt_mk] apply lt_succ @[simp] theorem typein_ordinal (o : Ordinal.{u}) : @typein Ordinal (· < ·) _ o = Ordinal.lift.{u + 1} o := by refine Quotient.inductionOn o ?_ rintro ⟨α, r, wo⟩; apply Quotient.sound constructor; refine ((RelIso.preimage Equiv.ulift r).trans (enum r).symm).symm theorem mk_Iio_ordinal (o : Ordinal.{u}) : #(Iio o) = Cardinal.lift.{u + 1} o.card := by rw [lift_card, ← typein_ordinal] rfl /-! ### Normal ordinal functions -/ /-- A normal ordinal function is a strictly increasing function which is order-continuous, i.e., the image `f o` of a limit ordinal `o` is the sup of `f a` for `a < o`. -/ def IsNormal (f : Ordinal → Ordinal) : Prop := (∀ o, f o < f (succ o)) ∧ ∀ o, IsLimit o → ∀ a, f o ≤ a ↔ ∀ b < o, f b ≤ a theorem IsNormal.limit_le {f} (H : IsNormal f) : ∀ {o}, IsLimit o → ∀ {a}, f o ≤ a ↔ ∀ b < o, f b ≤ a := @H.2 theorem IsNormal.limit_lt {f} (H : IsNormal f) {o} (h : IsLimit o) {a} : a < f o ↔ ∃ b < o, a < f b := not_iff_not.1 <| by simpa only [exists_prop, not_exists, not_and, not_lt] using H.2 _ h a theorem IsNormal.strictMono {f} (H : IsNormal f) : StrictMono f := fun a b => limitRecOn b (Not.elim (not_lt_of_le <| Ordinal.zero_le _)) (fun _b IH h => (lt_or_eq_of_le (le_of_lt_succ h)).elim (fun h => (IH h).trans (H.1 _)) fun e => e ▸ H.1 _) fun _b l _IH h => lt_of_lt_of_le (H.1 a) ((H.2 _ l _).1 le_rfl _ (l.succ_lt h)) theorem IsNormal.monotone {f} (H : IsNormal f) : Monotone f := H.strictMono.monotone theorem isNormal_iff_strictMono_limit (f : Ordinal → Ordinal) : IsNormal f ↔ StrictMono f ∧ ∀ o, IsLimit o → ∀ a, (∀ b < o, f b ≤ a) → f o ≤ a := ⟨fun hf => ⟨hf.strictMono, fun a ha c => (hf.2 a ha c).2⟩, fun ⟨hs, hl⟩ => ⟨fun a => hs (lt_succ a), fun a ha c => ⟨fun hac _b hba => ((hs hba).trans_le hac).le, hl a ha c⟩⟩⟩ theorem IsNormal.lt_iff {f} (H : IsNormal f) {a b} : f a < f b ↔ a < b := StrictMono.lt_iff_lt <| H.strictMono theorem IsNormal.le_iff {f} (H : IsNormal f) {a b} : f a ≤ f b ↔ a ≤ b := le_iff_le_iff_lt_iff_lt.2 H.lt_iff theorem IsNormal.inj {f} (H : IsNormal f) {a b} : f a = f b ↔ a = b := by simp only [le_antisymm_iff, H.le_iff] theorem IsNormal.id_le {f} (H : IsNormal f) : id ≤ f := H.strictMono.id_le theorem IsNormal.le_apply {f} (H : IsNormal f) {a} : a ≤ f a := H.strictMono.le_apply theorem IsNormal.le_iff_eq {f} (H : IsNormal f) {a} : f a ≤ a ↔ f a = a := H.le_apply.le_iff_eq theorem IsNormal.le_set {f o} (H : IsNormal f) (p : Set Ordinal) (p0 : p.Nonempty) (b) (H₂ : ∀ o, b ≤ o ↔ ∀ a ∈ p, a ≤ o) : f b ≤ o ↔ ∀ a ∈ p, f a ≤ o := ⟨fun h _ pa => (H.le_iff.2 ((H₂ _).1 le_rfl _ pa)).trans h, fun h => by induction b using limitRecOn with | zero => obtain ⟨x, px⟩ := p0 have := Ordinal.le_zero.1 ((H₂ _).1 (Ordinal.zero_le _) _ px) rw [this] at px exact h _ px | succ S _ => rcases not_forall₂.1 (mt (H₂ S).2 <| (lt_succ S).not_le) with ⟨a, h₁, h₂⟩ exact (H.le_iff.2 <| succ_le_of_lt <| not_le.1 h₂).trans (h _ h₁) | isLimit S L _ => refine (H.2 _ L _).2 fun a h' => ?_ rcases not_forall₂.1 (mt (H₂ a).2 h'.not_le) with ⟨b, h₁, h₂⟩ exact (H.le_iff.2 <| (not_le.1 h₂).le).trans (h _ h₁)⟩ theorem IsNormal.le_set' {f o} (H : IsNormal f) (p : Set α) (p0 : p.Nonempty) (g : α → Ordinal) (b) (H₂ : ∀ o, b ≤ o ↔ ∀ a ∈ p, g a ≤ o) : f b ≤ o ↔ ∀ a ∈ p, f (g a) ≤ o := by simpa [H₂] using H.le_set (g '' p) (p0.image g) b theorem IsNormal.refl : IsNormal id := ⟨lt_succ, fun _o l _a => Ordinal.limit_le l⟩ theorem IsNormal.trans {f g} (H₁ : IsNormal f) (H₂ : IsNormal g) : IsNormal (f ∘ g) := ⟨fun _x => H₁.lt_iff.2 (H₂.1 _), fun o l _a => H₁.le_set' (· < o) ⟨0, l.pos⟩ g _ fun _c => H₂.2 _ l _⟩ theorem IsNormal.isLimit {f} (H : IsNormal f) {o} (ho : IsLimit o) : IsLimit (f o) := by rw [isLimit_iff, isSuccPrelimit_iff_succ_lt] use (H.lt_iff.2 ho.pos).ne_bot intro a ha obtain ⟨b, hb, hab⟩ := (H.limit_lt ho).1 ha
rw [← succ_le_iff] at hab apply hab.trans_lt rwa [H.lt_iff] theorem add_le_of_limit {a b c : Ordinal} (h : IsLimit b) : a + b ≤ c ↔ ∀ b' < b, a + b' ≤ c := ⟨fun h _ l => (add_le_add_left l.le _).trans h, fun H => le_of_not_lt <| by -- Porting note: `induction` tactics are required because of the parser bug. induction a using inductionOn with | H α r => induction b using inductionOn with | H β s => intro l suffices ∀ x : β, Sum.Lex r s (Sum.inr x) (enum _ ⟨_, l⟩) by -- Porting note: `revert` & `intro` is required because `cases'` doesn't replace -- `enum _ _ l` in `this`.
Mathlib/SetTheory/Ordinal/Arithmetic.lean
442
458
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Sébastien Gouëzel, Patrick Massot -/ import Mathlib.Topology.UniformSpace.Cauchy import Mathlib.Topology.UniformSpace.Separation import Mathlib.Topology.DenseEmbedding /-! # Uniform embeddings of uniform spaces. Extension of uniform continuous functions. -/ open Filter Function Set Uniformity Topology section universe u v w variable {α : Type u} {β : Type v} {γ : Type w} [UniformSpace α] [UniformSpace β] [UniformSpace γ] {f : α → β} /-! ### Uniform inducing maps -/ /-- A map `f : α → β` between uniform spaces is called *uniform inducing* if the uniformity filter on `α` is the pullback of the uniformity filter on `β` under `Prod.map f f`. If `α` is a separated space, then this implies that `f` is injective, hence it is a `IsUniformEmbedding`. -/ @[mk_iff] structure IsUniformInducing (f : α → β) : Prop where /-- The uniformity filter on the domain is the pullback of the uniformity filter on the codomain under `Prod.map f f`. -/ comap_uniformity : comap (fun x : α × α => (f x.1, f x.2)) (𝓤 β) = 𝓤 α lemma isUniformInducing_iff_uniformSpace {f : α → β} : IsUniformInducing f ↔ ‹UniformSpace β›.comap f = ‹UniformSpace α› := by rw [isUniformInducing_iff, UniformSpace.ext_iff, Filter.ext_iff] rfl protected alias ⟨IsUniformInducing.comap_uniformSpace, _⟩ := isUniformInducing_iff_uniformSpace lemma isUniformInducing_iff' {f : α → β} : IsUniformInducing f ↔ UniformContinuous f ∧ comap (Prod.map f f) (𝓤 β) ≤ 𝓤 α := by rw [isUniformInducing_iff, UniformContinuous, tendsto_iff_comap, le_antisymm_iff, and_comm]; rfl protected lemma Filter.HasBasis.isUniformInducing_iff {ι ι'} {p : ι → Prop} {p' : ι' → Prop} {s s'} (h : (𝓤 α).HasBasis p s) (h' : (𝓤 β).HasBasis p' s') {f : α → β} : IsUniformInducing f ↔ (∀ i, p' i → ∃ j, p j ∧ ∀ x y, (x, y) ∈ s j → (f x, f y) ∈ s' i) ∧ (∀ j, p j → ∃ i, p' i ∧ ∀ x y, (f x, f y) ∈ s' i → (x, y) ∈ s j) := by simp [isUniformInducing_iff', h.uniformContinuous_iff h', (h'.comap _).le_basis_iff h, subset_def] theorem IsUniformInducing.mk' {f : α → β} (h : ∀ s, s ∈ 𝓤 α ↔ ∃ t ∈ 𝓤 β, ∀ x y : α, (f x, f y) ∈ t → (x, y) ∈ s) : IsUniformInducing f := ⟨by simp [eq_comm, Filter.ext_iff, subset_def, h]⟩ theorem IsUniformInducing.id : IsUniformInducing (@id α) := ⟨by rw [← Prod.map_def, Prod.map_id, comap_id]⟩ theorem IsUniformInducing.comp {g : β → γ} (hg : IsUniformInducing g) {f : α → β} (hf : IsUniformInducing f) : IsUniformInducing (g ∘ f) := ⟨by rw [← hf.1, ← hg.1, comap_comap]; rfl⟩ theorem IsUniformInducing.of_comp_iff {g : β → γ} (hg : IsUniformInducing g) {f : α → β} : IsUniformInducing (g ∘ f) ↔ IsUniformInducing f := by refine ⟨fun h ↦ ?_, hg.comp⟩ rw [isUniformInducing_iff, ← hg.comap_uniformity, comap_comap, ← h.comap_uniformity, Function.comp_def, Function.comp_def] theorem IsUniformInducing.basis_uniformity {f : α → β} (hf : IsUniformInducing f) {ι : Sort*} {p : ι → Prop} {s : ι → Set (β × β)} (H : (𝓤 β).HasBasis p s) : (𝓤 α).HasBasis p fun i => Prod.map f f ⁻¹' s i := hf.1 ▸ H.comap _ theorem IsUniformInducing.cauchy_map_iff {f : α → β} (hf : IsUniformInducing f) {F : Filter α} : Cauchy (map f F) ↔ Cauchy F := by simp only [Cauchy, map_neBot_iff, prod_map_map_eq, map_le_iff_le_comap, ← hf.comap_uniformity] theorem IsUniformInducing.of_comp {f : α → β} {g : β → γ} (hf : UniformContinuous f) (hg : UniformContinuous g) (hgf : IsUniformInducing (g ∘ f)) : IsUniformInducing f := by refine ⟨le_antisymm ?_ hf.le_comap⟩ rw [← hgf.1, ← Prod.map_def, ← Prod.map_def, ← Prod.map_comp_map f f g g, ← comap_comap] exact comap_mono hg.le_comap theorem IsUniformInducing.uniformContinuous {f : α → β} (hf : IsUniformInducing f) : UniformContinuous f := (isUniformInducing_iff'.1 hf).1 theorem IsUniformInducing.uniformContinuous_iff {f : α → β} {g : β → γ} (hg : IsUniformInducing g) : UniformContinuous f ↔ UniformContinuous (g ∘ f) := by dsimp only [UniformContinuous, Tendsto] simp only [← hg.comap_uniformity, ← map_le_iff_le_comap, Filter.map_map, Function.comp_def] protected theorem IsUniformInducing.isUniformInducing_comp_iff {f : α → β} {g : β → γ} (hg : IsUniformInducing g) : IsUniformInducing (g ∘ f) ↔ IsUniformInducing f := by simp only [isUniformInducing_iff, ← hg.comap_uniformity, comap_comap, Function.comp_def] theorem IsUniformInducing.uniformContinuousOn_iff {f : α → β} {g : β → γ} {S : Set α} (hg : IsUniformInducing g) : UniformContinuousOn f S ↔ UniformContinuousOn (g ∘ f) S := by dsimp only [UniformContinuousOn, Tendsto] rw [← hg.comap_uniformity, ← map_le_iff_le_comap, Filter.map_map, comp_def, comp_def] theorem IsUniformInducing.isInducing {f : α → β} (h : IsUniformInducing f) : IsInducing f := by obtain rfl := h.comap_uniformSpace exact .induced f @[deprecated (since := "2024-10-28")] alias IsUniformInducing.inducing := IsUniformInducing.isInducing @[deprecated (since := "2024-10-28")] alias UniformInducing.inducing := IsUniformInducing.isInducing theorem IsUniformInducing.prod {α' : Type*} {β' : Type*} [UniformSpace α'] [UniformSpace β'] {e₁ : α → α'} {e₂ : β → β'} (h₁ : IsUniformInducing e₁) (h₂ : IsUniformInducing e₂) : IsUniformInducing fun p : α × β => (e₁ p.1, e₂ p.2) := ⟨by simp [Function.comp_def, uniformity_prod, ← h₁.1, ← h₂.1, comap_inf, comap_comap]⟩ lemma IsUniformInducing.isDenseInducing (h : IsUniformInducing f) (hd : DenseRange f) : IsDenseInducing f where toIsInducing := h.isInducing dense := hd lemma SeparationQuotient.isUniformInducing_mk : IsUniformInducing (mk : α → SeparationQuotient α) := ⟨comap_mk_uniformity⟩ protected theorem IsUniformInducing.injective [T0Space α] {f : α → β} (h : IsUniformInducing f) : Injective f := h.isInducing.injective /-! ### Uniform embeddings -/ /-- A map `f : α → β` between uniform spaces is a *uniform embedding* if it is uniform inducing and injective. If `α` is a separated space, then the latter assumption follows from the former. -/ @[mk_iff] structure IsUniformEmbedding (f : α → β) : Prop extends IsUniformInducing f where /-- A uniform embedding is injective. -/ injective : Function.Injective f lemma IsUniformEmbedding.isUniformInducing (hf : IsUniformEmbedding f) : IsUniformInducing f := hf.toIsUniformInducing theorem isUniformEmbedding_iff' {f : α → β} : IsUniformEmbedding f ↔ Injective f ∧ UniformContinuous f ∧ comap (Prod.map f f) (𝓤 β) ≤ 𝓤 α := by rw [isUniformEmbedding_iff, and_comm, isUniformInducing_iff'] theorem Filter.HasBasis.isUniformEmbedding_iff' {ι ι'} {p : ι → Prop} {p' : ι' → Prop} {s s'} (h : (𝓤 α).HasBasis p s) (h' : (𝓤 β).HasBasis p' s') {f : α → β} : IsUniformEmbedding f ↔ Injective f ∧ (∀ i, p' i → ∃ j, p j ∧ ∀ x y, (x, y) ∈ s j → (f x, f y) ∈ s' i) ∧ (∀ j, p j → ∃ i, p' i ∧ ∀ x y, (f x, f y) ∈ s' i → (x, y) ∈ s j) := by rw [isUniformEmbedding_iff, and_comm, h.isUniformInducing_iff h'] theorem Filter.HasBasis.isUniformEmbedding_iff {ι ι'} {p : ι → Prop} {p' : ι' → Prop} {s s'} (h : (𝓤 α).HasBasis p s) (h' : (𝓤 β).HasBasis p' s') {f : α → β} : IsUniformEmbedding f ↔ Injective f ∧ UniformContinuous f ∧ (∀ j, p j → ∃ i, p' i ∧ ∀ x y, (f x, f y) ∈ s' i → (x, y) ∈ s j) := by simp only [h.isUniformEmbedding_iff' h', h.uniformContinuous_iff h'] theorem isUniformEmbedding_subtype_val {p : α → Prop} :
IsUniformEmbedding (Subtype.val : Subtype p → α) := { comap_uniformity := rfl injective := Subtype.val_injective } theorem isUniformEmbedding_set_inclusion {s t : Set α} (hst : s ⊆ t) :
Mathlib/Topology/UniformSpace/UniformEmbedding.lean
166
170
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import Mathlib.Algebra.Group.Basic import Mathlib.Algebra.GroupWithZero.NeZero import Mathlib.Logic.Unique import Mathlib.Tactic.Conv /-! # Groups with an adjoined zero element This file describes structures that are not usually studied on their own right in mathematics, namely a special sort of monoid: apart from a distinguished “zero element” they form a group, or in other words, they are groups with an adjoined zero element. Examples are: * division rings; * the value monoid of a multiplicative valuation; * in particular, the non-negative real numbers. ## Main definitions Various lemmas about `GroupWithZero` and `CommGroupWithZero`. To reduce import dependencies, the type-classes themselves are in `Algebra.GroupWithZero.Defs`. ## Implementation details As is usual in mathlib, we extend the inverse function to the zero element, and require `0⁻¹ = 0`. -/ assert_not_exists DenselyOrdered open Function variable {M₀ G₀ : Type*} section section MulZeroClass variable [MulZeroClass M₀] {a b : M₀} theorem left_ne_zero_of_mul : a * b ≠ 0 → a ≠ 0 := mt fun h => mul_eq_zero_of_left h b theorem right_ne_zero_of_mul : a * b ≠ 0 → b ≠ 0 := mt (mul_eq_zero_of_right a) theorem ne_zero_and_ne_zero_of_mul (h : a * b ≠ 0) : a ≠ 0 ∧ b ≠ 0 := ⟨left_ne_zero_of_mul h, right_ne_zero_of_mul h⟩ theorem mul_eq_zero_of_ne_zero_imp_eq_zero {a b : M₀} (h : a ≠ 0 → b = 0) : a * b = 0 := by have : Decidable (a = 0) := Classical.propDecidable (a = 0) exact if ha : a = 0 then by rw [ha, zero_mul] else by rw [h ha, mul_zero] /-- To match `one_mul_eq_id`. -/ theorem zero_mul_eq_const : ((0 : M₀) * ·) = Function.const _ 0 := funext zero_mul /-- To match `mul_one_eq_id`. -/ theorem mul_zero_eq_const : (· * (0 : M₀)) = Function.const _ 0 := funext mul_zero end MulZeroClass section Mul variable [Mul M₀] [Zero M₀] [NoZeroDivisors M₀] {a b : M₀} theorem eq_zero_of_mul_self_eq_zero (h : a * a = 0) : a = 0 := (eq_zero_or_eq_zero_of_mul_eq_zero h).elim id id @[field_simps] theorem mul_ne_zero (ha : a ≠ 0) (hb : b ≠ 0) : a * b ≠ 0 := mt eq_zero_or_eq_zero_of_mul_eq_zero <| not_or.mpr ⟨ha, hb⟩ end Mul namespace NeZero instance mul [Zero M₀] [Mul M₀] [NoZeroDivisors M₀] {x y : M₀} [NeZero x] [NeZero y] : NeZero (x * y) := ⟨mul_ne_zero out out⟩ end NeZero end section variable [MulZeroOneClass M₀] /-- In a monoid with zero, if zero equals one, then zero is the only element. -/ theorem eq_zero_of_zero_eq_one (h : (0 : M₀) = 1) (a : M₀) : a = 0 := by rw [← mul_one a, ← h, mul_zero] /-- In a monoid with zero, if zero equals one, then zero is the unique element. Somewhat arbitrarily, we define the default element to be `0`. All other elements will be provably equal to it, but not necessarily definitionally equal. -/ def uniqueOfZeroEqOne (h : (0 : M₀) = 1) : Unique M₀ where default := 0 uniq := eq_zero_of_zero_eq_one h /-- In a monoid with zero, zero equals one if and only if all elements of that semiring are equal. -/ theorem subsingleton_iff_zero_eq_one : (0 : M₀) = 1 ↔ Subsingleton M₀ := ⟨fun h => haveI := uniqueOfZeroEqOne h; inferInstance, fun h => @Subsingleton.elim _ h _ _⟩ alias ⟨subsingleton_of_zero_eq_one, _⟩ := subsingleton_iff_zero_eq_one theorem eq_of_zero_eq_one (h : (0 : M₀) = 1) (a b : M₀) : a = b := @Subsingleton.elim _ (subsingleton_of_zero_eq_one h) a b /-- In a monoid with zero, either zero and one are nonequal, or zero is the only element. -/ theorem zero_ne_one_or_forall_eq_0 : (0 : M₀) ≠ 1 ∨ ∀ a : M₀, a = 0 := not_or_of_imp eq_zero_of_zero_eq_one end section variable [MulZeroOneClass M₀] [Nontrivial M₀] {a b : M₀} theorem left_ne_zero_of_mul_eq_one (h : a * b = 1) : a ≠ 0 := left_ne_zero_of_mul <| ne_zero_of_eq_one h theorem right_ne_zero_of_mul_eq_one (h : a * b = 1) : b ≠ 0 := right_ne_zero_of_mul <| ne_zero_of_eq_one h end section MonoidWithZero variable [MonoidWithZero M₀] {a : M₀} {n : ℕ} @[simp] lemma zero_pow : ∀ {n : ℕ}, n ≠ 0 → (0 : M₀) ^ n = 0 | n + 1, _ => by rw [pow_succ, mul_zero] lemma zero_pow_eq (n : ℕ) : (0 : M₀) ^ n = if n = 0 then 1 else 0 := by split_ifs with h · rw [h, pow_zero] · rw [zero_pow h] lemma zero_pow_eq_one₀ [Nontrivial M₀] : (0 : M₀) ^ n = 1 ↔ n = 0 := by rw [zero_pow_eq, one_ne_zero.ite_eq_left_iff] lemma pow_eq_zero_of_le : ∀ {m n}, m ≤ n → a ^ m = 0 → a ^ n = 0 | _, _, Nat.le.refl, ha => ha | _, _, Nat.le.step hmn, ha => by rw [pow_succ, pow_eq_zero_of_le hmn ha, zero_mul] lemma ne_zero_pow (hn : n ≠ 0) (ha : a ^ n ≠ 0) : a ≠ 0 := by rintro rfl; exact ha <| zero_pow hn @[simp] lemma zero_pow_eq_zero [Nontrivial M₀] : (0 : M₀) ^ n = 0 ↔ n ≠ 0 := ⟨by rintro h rfl; simp at h, zero_pow⟩ lemma pow_mul_eq_zero_of_le {a b : M₀} {m n : ℕ} (hmn : m ≤ n) (h : a ^ m * b = 0) : a ^ n * b = 0 := by
rw [show n = n - m + m by omega, pow_add, mul_assoc, h] simp variable [NoZeroDivisors M₀]
Mathlib/Algebra/GroupWithZero/Basic.lean
165
168
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Johan Commelin, Mario Carneiro -/ import Mathlib.Algebra.MonoidAlgebra.Degree import Mathlib.Algebra.MvPolynomial.Rename /-! # Degrees of polynomials This file establishes many results about the degree of a multivariate polynomial. The *degree set* of a polynomial $P \in R[X]$ is a `Multiset` containing, for each $x$ in the variable set, $n$ copies of $x$, where $n$ is the maximum number of copies of $x$ appearing in a monomial of $P$. ## Main declarations * `MvPolynomial.degrees p` : the multiset of variables representing the union of the multisets corresponding to each non-zero monomial in `p`. For example if `7 ≠ 0` in `R` and `p = x²y+7y³` then `degrees p = {x, x, y, y, y}` * `MvPolynomial.degreeOf n p : ℕ` : the total degree of `p` with respect to the variable `n`. For example if `p = x⁴y+yz` then `degreeOf y p = 1`. * `MvPolynomial.totalDegree p : ℕ` : the max of the sizes of the multisets `s` whose monomials `X^s` occur in `p`. For example if `p = x⁴y+yz` then `totalDegree p = 5`. ## Notation As in other polynomial files, we typically use the notation: + `σ τ : Type*` (indexing the variables) + `R : Type*` `[CommSemiring R]` (the coefficients) + `s : σ →₀ ℕ`, a function from `σ` to `ℕ` which is zero away from a finite set. This will give rise to a monomial in `MvPolynomial σ R` which mathematicians might call `X^s` + `r : R` + `i : σ`, with corresponding monomial `X i`, often denoted `X_i` by mathematicians + `p : MvPolynomial σ R` -/ noncomputable section open Set Function Finsupp AddMonoidAlgebra universe u v w variable {R : Type u} {S : Type v} namespace MvPolynomial variable {σ τ : Type*} {r : R} {e : ℕ} {n m : σ} {s : σ →₀ ℕ} section CommSemiring variable [CommSemiring R] {p q : MvPolynomial σ R} section Degrees /-! ### `degrees` -/ /-- The maximal degrees of each variable in a multi-variable polynomial, expressed as a multiset. (For example, `degrees (x^2 * y + y^3)` would be `{x, x, y, y, y}`.) -/ def degrees (p : MvPolynomial σ R) : Multiset σ := letI := Classical.decEq σ p.support.sup fun s : σ →₀ ℕ => toMultiset s theorem degrees_def [DecidableEq σ] (p : MvPolynomial σ R) : p.degrees = p.support.sup fun s : σ →₀ ℕ => Finsupp.toMultiset s := by rw [degrees]; convert rfl theorem degrees_monomial (s : σ →₀ ℕ) (a : R) : degrees (monomial s a) ≤ toMultiset s := by classical refine (supDegree_single s a).trans_le ?_ split_ifs exacts [bot_le, le_rfl] theorem degrees_monomial_eq (s : σ →₀ ℕ) (a : R) (ha : a ≠ 0) : degrees (monomial s a) = toMultiset s := by classical exact (supDegree_single s a).trans (if_neg ha) theorem degrees_C (a : R) : degrees (C a : MvPolynomial σ R) = 0 := Multiset.le_zero.1 <| degrees_monomial _ _ theorem degrees_X' (n : σ) : degrees (X n : MvPolynomial σ R) ≤ {n} := le_trans (degrees_monomial _ _) <| le_of_eq <| toMultiset_single _ _ @[simp] theorem degrees_X [Nontrivial R] (n : σ) : degrees (X n : MvPolynomial σ R) = {n} := (degrees_monomial_eq _ (1 : R) one_ne_zero).trans (toMultiset_single _ _) @[simp] theorem degrees_zero : degrees (0 : MvPolynomial σ R) = 0 := by rw [← C_0] exact degrees_C 0 @[simp] theorem degrees_one : degrees (1 : MvPolynomial σ R) = 0 := degrees_C 1 theorem degrees_add_le [DecidableEq σ] {p q : MvPolynomial σ R} : (p + q).degrees ≤ p.degrees ⊔ q.degrees := by simp_rw [degrees_def]; exact supDegree_add_le theorem degrees_sum_le {ι : Type*} [DecidableEq σ] (s : Finset ι) (f : ι → MvPolynomial σ R) : (∑ i ∈ s, f i).degrees ≤ s.sup fun i => (f i).degrees := by simp_rw [degrees_def]; exact supDegree_sum_le theorem degrees_mul_le {p q : MvPolynomial σ R} : (p * q).degrees ≤ p.degrees + q.degrees := by classical simp_rw [degrees_def] exact supDegree_mul_le (map_add _) theorem degrees_prod_le {ι : Type*} {s : Finset ι} {f : ι → MvPolynomial σ R} : (∏ i ∈ s, f i).degrees ≤ ∑ i ∈ s, (f i).degrees := by classical exact supDegree_prod_le (map_zero _) (map_add _) theorem degrees_pow_le {p : MvPolynomial σ R} {n : ℕ} : (p ^ n).degrees ≤ n • p.degrees := by simpa using degrees_prod_le (s := .range n) (f := fun _ ↦ p) @[deprecated (since := "2024-12-28")] alias degrees_add := degrees_add_le @[deprecated (since := "2024-12-28")] alias degrees_sum := degrees_sum_le @[deprecated (since := "2024-12-28")] alias degrees_mul := degrees_mul_le @[deprecated (since := "2024-12-28")] alias degrees_prod := degrees_prod_le @[deprecated (since := "2024-12-28")] alias degrees_pow := degrees_pow_le theorem mem_degrees {p : MvPolynomial σ R} {i : σ} : i ∈ p.degrees ↔ ∃ d, p.coeff d ≠ 0 ∧ i ∈ d.support := by classical simp only [degrees_def, Multiset.mem_sup, ← mem_support_iff, Finsupp.mem_toMultiset, exists_prop] theorem le_degrees_add_left (h : Disjoint p.degrees q.degrees) : p.degrees ≤ (p + q).degrees := by classical apply Finset.sup_le intro d hd rw [Multiset.disjoint_iff_ne] at h obtain rfl | h0 := eq_or_ne d 0 · rw [toMultiset_zero]; apply Multiset.zero_le · refine Finset.le_sup_of_le (b := d) ?_ le_rfl rw [mem_support_iff, coeff_add] suffices q.coeff d = 0 by rwa [this, add_zero, coeff, ← Finsupp.mem_support_iff] rw [Ne, ← Finsupp.support_eq_empty, ← Ne, ← Finset.nonempty_iff_ne_empty] at h0 obtain ⟨j, hj⟩ := h0 contrapose! h rw [mem_support_iff] at hd refine ⟨j, ?_, j, ?_, rfl⟩ all_goals rw [mem_degrees]; refine ⟨d, ?_, hj⟩; assumption @[deprecated (since := "2024-12-28")] alias le_degrees_add := le_degrees_add_left lemma le_degrees_add_right (h : Disjoint p.degrees q.degrees) : q.degrees ≤ (p + q).degrees := by simpa [add_comm] using le_degrees_add_left h.symm theorem degrees_add_of_disjoint [DecidableEq σ] (h : Disjoint p.degrees q.degrees) : (p + q).degrees = p.degrees ∪ q.degrees := degrees_add_le.antisymm <| Multiset.union_le (le_degrees_add_left h) (le_degrees_add_right h) lemma degrees_map_le [CommSemiring S] {f : R →+* S} : (map f p).degrees ≤ p.degrees := by classical exact Finset.sup_mono <| support_map_subset .. @[deprecated (since := "2024-12-28")] alias degrees_map := degrees_map_le theorem degrees_rename (f : σ → τ) (φ : MvPolynomial σ R) : (rename f φ).degrees ⊆ φ.degrees.map f := by classical intro i rw [mem_degrees, Multiset.mem_map] rintro ⟨d, hd, hi⟩ obtain ⟨x, rfl, hx⟩ := coeff_rename_ne_zero _ _ _ hd simp only [Finsupp.mapDomain, Finsupp.mem_support_iff] at hi rw [sum_apply, Finsupp.sum] at hi contrapose! hi rw [Finset.sum_eq_zero] intro j hj simp only [exists_prop, mem_degrees] at hi specialize hi j ⟨x, hx, hj⟩ rw [Finsupp.single_apply, if_neg hi] theorem degrees_map_of_injective [CommSemiring S] (p : MvPolynomial σ R) {f : R →+* S} (hf : Injective f) : (map f p).degrees = p.degrees := by simp only [degrees, MvPolynomial.support_map_of_injective _ hf] theorem degrees_rename_of_injective {p : MvPolynomial σ R} {f : σ → τ} (h : Function.Injective f) : degrees (rename f p) = (degrees p).map f := by classical simp only [degrees, Multiset.map_finset_sup p.support Finsupp.toMultiset f h, support_rename_of_injective h, Finset.sup_image] refine Finset.sup_congr rfl fun x _ => ?_ exact (Finsupp.toMultiset_map _ _).symm end Degrees section DegreeOf /-! ### `degreeOf` -/ /-- `degreeOf n p` gives the highest power of X_n that appears in `p` -/ def degreeOf (n : σ) (p : MvPolynomial σ R) : ℕ := letI := Classical.decEq σ p.degrees.count n theorem degreeOf_def [DecidableEq σ] (n : σ) (p : MvPolynomial σ R) : p.degreeOf n = p.degrees.count n := by rw [degreeOf]; convert rfl theorem degreeOf_eq_sup (n : σ) (f : MvPolynomial σ R) : degreeOf n f = f.support.sup fun m => m n := by classical rw [degreeOf_def, degrees, Multiset.count_finset_sup] congr ext simp only [count_toMultiset] theorem degreeOf_lt_iff {n : σ} {f : MvPolynomial σ R} {d : ℕ} (h : 0 < d) : degreeOf n f < d ↔ ∀ m : σ →₀ ℕ, m ∈ f.support → m n < d := by rwa [degreeOf_eq_sup, Finset.sup_lt_iff] lemma degreeOf_le_iff {n : σ} {f : MvPolynomial σ R} {d : ℕ} : degreeOf n f ≤ d ↔ ∀ m ∈ support f, m n ≤ d := by rw [degreeOf_eq_sup, Finset.sup_le_iff] @[simp] theorem degreeOf_zero (n : σ) : degreeOf n (0 : MvPolynomial σ R) = 0 := by classical simp only [degreeOf_def, degrees_zero, Multiset.count_zero] @[simp] theorem degreeOf_C (a : R) (x : σ) : degreeOf x (C a : MvPolynomial σ R) = 0 := by classical simp [degreeOf_def, degrees_C] theorem degreeOf_X [DecidableEq σ] (i j : σ) [Nontrivial R] : degreeOf i (X j : MvPolynomial σ R) = if i = j then 1 else 0 := by classical by_cases c : i = j · simp only [c, if_true, eq_self_iff_true, degreeOf_def, degrees_X, Multiset.count_singleton] simp [c, if_false, degreeOf_def, degrees_X] theorem degreeOf_add_le (n : σ) (f g : MvPolynomial σ R) : degreeOf n (f + g) ≤ max (degreeOf n f) (degreeOf n g) := by simp_rw [degreeOf_eq_sup]; exact supDegree_add_le theorem monomial_le_degreeOf (i : σ) {f : MvPolynomial σ R} {m : σ →₀ ℕ} (h_m : m ∈ f.support) : m i ≤ degreeOf i f := by rw [degreeOf_eq_sup i] apply Finset.le_sup h_m lemma degreeOf_monomial_eq (s : σ →₀ ℕ) (i : σ) {a : R} (ha : a ≠ 0) : (monomial s a).degreeOf i = s i := by classical rw [degreeOf_def, degrees_monomial_eq _ _ ha, Finsupp.count_toMultiset] -- TODO we can prove equality with `NoZeroDivisors R` theorem degreeOf_mul_le (i : σ) (f g : MvPolynomial σ R) : degreeOf i (f * g) ≤ degreeOf i f + degreeOf i g := by classical simp only [degreeOf] convert Multiset.count_le_of_le i degrees_mul_le rw [Multiset.count_add] theorem degreeOf_sum_le {ι : Type*} (i : σ) (s : Finset ι) (f : ι → MvPolynomial σ R) : degreeOf i (∑ j ∈ s, f j) ≤ s.sup fun j => degreeOf i (f j) := by simp_rw [degreeOf_eq_sup] exact supDegree_sum_le -- TODO we can prove equality with `NoZeroDivisors R` theorem degreeOf_prod_le {ι : Type*} (i : σ) (s : Finset ι) (f : ι → MvPolynomial σ R) : degreeOf i (∏ j ∈ s, f j) ≤ ∑ j ∈ s, (f j).degreeOf i := by simp_rw [degreeOf_eq_sup] exact supDegree_prod_le (by simp only [coe_zero, Pi.zero_apply]) (fun _ _ => by simp only [coe_add, Pi.add_apply]) -- TODO we can prove equality with `NoZeroDivisors R` theorem degreeOf_pow_le (i : σ) (p : MvPolynomial σ R) (n : ℕ) : degreeOf i (p ^ n) ≤ n * degreeOf i p := by simpa using degreeOf_prod_le i (Finset.range n) (fun _ => p) theorem degreeOf_mul_X_of_ne {i j : σ} (f : MvPolynomial σ R) (h : i ≠ j) : degreeOf i (f * X j) = degreeOf i f := by classical simp only [degreeOf_eq_sup i, support_mul_X, Finset.sup_map] congr ext simp only [Finsupp.single, add_eq_left, addRightEmbedding_apply, coe_mk, Pi.add_apply, comp_apply, ite_eq_right_iff, Finsupp.coe_add, Pi.single_eq_of_ne h] @[deprecated (since := "2024-12-01")] alias degreeOf_mul_X_ne := degreeOf_mul_X_of_ne theorem degreeOf_mul_X_self (j : σ) (f : MvPolynomial σ R) : degreeOf j (f * X j) ≤ degreeOf j f + 1 := by classical simp only [degreeOf] apply (Multiset.count_le_of_le j degrees_mul_le).trans simp only [Multiset.count_add, add_le_add_iff_left] convert Multiset.count_le_of_le j <| degrees_X' j rw [Multiset.count_singleton_self] @[deprecated (since := "2024-12-01")] alias degreeOf_mul_X_eq := degreeOf_mul_X_self theorem degreeOf_mul_X_eq_degreeOf_add_one_iff (j : σ) (f : MvPolynomial σ R) : degreeOf j (f * X j) = degreeOf j f + 1 ↔ f ≠ 0 := by refine ⟨fun h => by by_contra ha; simp [ha] at h, fun h => ?_⟩ apply Nat.le_antisymm (degreeOf_mul_X_self j f) have : (f.support.sup fun m ↦ m j) + 1 = (f.support.sup fun m ↦ (m j + 1)) := Finset.comp_sup_eq_sup_comp_of_nonempty @Nat.succ_le_succ (support_nonempty.mpr h) simp only [degreeOf_eq_sup, support_mul_X, this] apply Finset.sup_le intro x hx simp only [Finset.sup_map, bot_eq_zero', add_pos_iff, zero_lt_one, or_true, Finset.le_sup_iff] use x simpa using mem_support_iff.mp hx theorem degreeOf_C_mul_le (p : MvPolynomial σ R) (i : σ) (c : R) : (C c * p).degreeOf i ≤ p.degreeOf i := by unfold degreeOf convert Multiset.count_le_of_le i degrees_mul_le simp only [degrees_C, zero_add] theorem degreeOf_mul_C_le (p : MvPolynomial σ R) (i : σ) (c : R) : (p * C c).degreeOf i ≤ p.degreeOf i := by unfold degreeOf convert Multiset.count_le_of_le i degrees_mul_le simp only [degrees_C, add_zero]
theorem degreeOf_rename_of_injective {p : MvPolynomial σ R} {f : σ → τ} (h : Function.Injective f) (i : σ) : degreeOf (f i) (rename f p) = degreeOf i p := by classical simp only [degreeOf, degrees_rename_of_injective h, Multiset.count_map_eq_count' f p.degrees h]
Mathlib/Algebra/MvPolynomial/Degrees.lean
334
338
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Yury Kudryashov -/ import Mathlib.Topology.Bases import Mathlib.Topology.Compactness.LocallyCompact import Mathlib.Topology.Compactness.LocallyFinite /-! # Sigma-compactness in topological spaces ## Main definitions * `IsSigmaCompact`: a set that is the union of countably many compact sets. * `SigmaCompactSpace X`: `X` is a σ-compact topological space; i.e., is the union of a countable collection of compact subspaces. -/ open Set Filter Topology TopologicalSpace universe u v variable {X : Type*} {Y : Type*} {ι : Type*} variable [TopologicalSpace X] [TopologicalSpace Y] {s t : Set X} /-- A subset `s ⊆ X` is called **σ-compact** if it is the union of countably many compact sets. -/ def IsSigmaCompact (s : Set X) : Prop := ∃ K : ℕ → Set X, (∀ n, IsCompact (K n)) ∧ ⋃ n, K n = s /-- Compact sets are σ-compact. -/ lemma IsCompact.isSigmaCompact {s : Set X} (hs : IsCompact s) : IsSigmaCompact s := ⟨fun _ => s, fun _ => hs, iUnion_const _⟩ /-- The empty set is σ-compact. -/ @[simp] lemma isSigmaCompact_empty : IsSigmaCompact (∅ : Set X) := IsCompact.isSigmaCompact isCompact_empty /-- Countable unions of compact sets are σ-compact. -/ lemma isSigmaCompact_iUnion_of_isCompact [hι : Countable ι] (s : ι → Set X) (hcomp : ∀ i, IsCompact (s i)) : IsSigmaCompact (⋃ i, s i) := by rcases isEmpty_or_nonempty ι · simp only [iUnion_of_empty, isSigmaCompact_empty] · -- If ι is non-empty, choose a surjection f : ℕ → ι, this yields a map ℕ → Set X. obtain ⟨f, hf⟩ := countable_iff_exists_surjective.mp hι exact ⟨s ∘ f, fun n ↦ hcomp (f n), Function.Surjective.iUnion_comp hf _⟩ /-- Countable unions of compact sets are σ-compact. -/ lemma isSigmaCompact_sUnion_of_isCompact {S : Set (Set X)} (hc : Set.Countable S) (hcomp : ∀ (s : Set X), s ∈ S → IsCompact s) : IsSigmaCompact (⋃₀ S) := by have : Countable S := countable_coe_iff.mpr hc rw [sUnion_eq_iUnion] apply isSigmaCompact_iUnion_of_isCompact _ (fun ⟨s, hs⟩ ↦ hcomp s hs) /-- Countable unions of σ-compact sets are σ-compact. -/ lemma isSigmaCompact_iUnion [Countable ι] (s : ι → Set X) (hcomp : ∀ i, IsSigmaCompact (s i)) : IsSigmaCompact (⋃ i, s i) := by -- Choose a decomposition s_i = ⋃ K_i,j for each i. choose K hcomp hcov using fun i ↦ hcomp i -- Then, we have a countable union of countable unions of compact sets, i.e. countably many. have := calc ⋃ i, s i _ = ⋃ i, ⋃ n, (K i n) := by simp_rw [hcov] _ = ⋃ (i) (n : ℕ), (K.uncurry ⟨i, n⟩) := by rw [Function.uncurry_def] _ = ⋃ x, K.uncurry x := by rw [← iUnion_prod'] rw [this] exact isSigmaCompact_iUnion_of_isCompact K.uncurry fun x ↦ (hcomp x.1 x.2) /-- Countable unions of σ-compact sets are σ-compact. -/ lemma isSigmaCompact_sUnion (S : Set (Set X)) (hc : Set.Countable S) (hcomp : ∀ s : S, IsSigmaCompact s (X := X)) : IsSigmaCompact (⋃₀ S) := by have : Countable S := countable_coe_iff.mpr hc apply sUnion_eq_iUnion.symm ▸ isSigmaCompact_iUnion _ hcomp /-- Countable unions of σ-compact sets are σ-compact. -/ lemma isSigmaCompact_biUnion {s : Set ι} {S : ι → Set X} (hc : Set.Countable s) (hcomp : ∀ (i : ι), i ∈ s → IsSigmaCompact (S i)) : IsSigmaCompact (⋃ (i : ι) (_ : i ∈ s), S i) := by have : Countable ↑s := countable_coe_iff.mpr hc rw [biUnion_eq_iUnion] exact isSigmaCompact_iUnion _ (fun ⟨i', hi'⟩ ↦ hcomp i' hi') /-- A closed subset of a σ-compact set is σ-compact. -/ lemma IsSigmaCompact.of_isClosed_subset {s t : Set X} (ht : IsSigmaCompact t) (hs : IsClosed s) (h : s ⊆ t) : IsSigmaCompact s := by rcases ht with ⟨K, hcompact, hcov⟩ refine ⟨(fun n ↦ s ∩ (K n)), fun n ↦ (hcompact n).inter_left hs, ?_⟩ rw [← inter_iUnion, hcov] exact inter_eq_left.mpr h /-- If `s` is σ-compact and `f` is continuous on `s`, `f(s)` is σ-compact. -/ lemma IsSigmaCompact.image_of_continuousOn {f : X → Y} {s : Set X} (hs : IsSigmaCompact s) (hf : ContinuousOn f s) : IsSigmaCompact (f '' s) := by rcases hs with ⟨K, hcompact, hcov⟩ refine ⟨fun n ↦ f '' K n, ?_, hcov.symm ▸ image_iUnion.symm⟩ exact fun n ↦ (hcompact n).image_of_continuousOn (hf.mono (hcov.symm ▸ subset_iUnion K n)) /-- If `s` is σ-compact and `f` continuous, `f(s)` is σ-compact. -/ lemma IsSigmaCompact.image {f : X → Y} (hf : Continuous f) {s : Set X} (hs : IsSigmaCompact s) : IsSigmaCompact (f '' s) := hs.image_of_continuousOn hf.continuousOn /-- If `f : X → Y` is an inducing map, the image `f '' s` of a set `s` is σ-compact if and only `s` is σ-compact. -/ lemma Topology.IsInducing.isSigmaCompact_iff {f : X → Y} {s : Set X} (hf : IsInducing f) : IsSigmaCompact s ↔ IsSigmaCompact (f '' s) := by constructor · exact fun h ↦ h.image hf.continuous · rintro ⟨L, hcomp, hcov⟩ -- Suppose f(s) is σ-compact; we want to show s is σ-compact. -- Write f(s) as a union of compact sets L n, so s = ⋃ K n with K n := f⁻¹(L n) ∩ s. -- Since f is inducing, each K n is compact iff L n is. refine ⟨fun n ↦ f ⁻¹' (L n) ∩ s, ?_, ?_⟩ · intro n have : f '' (f ⁻¹' (L n) ∩ s) = L n := by rw [image_preimage_inter, inter_eq_left.mpr] exact (subset_iUnion _ n).trans hcov.le apply hf.isCompact_iff.mpr (this.symm ▸ (hcomp n)) · calc ⋃ n, f ⁻¹' L n ∩ s _ = f ⁻¹' (⋃ n, L n) ∩ s := by rw [preimage_iUnion, iUnion_inter] _ = f ⁻¹' (f '' s) ∩ s := by rw [hcov] _ = s := inter_eq_right.mpr (subset_preimage_image _ _) @[deprecated (since := "2024-10-28")] alias Inducing.isSigmaCompact_iff := IsInducing.isSigmaCompact_iff /-- If `f : X → Y` is an embedding, the image `f '' s` of a set `s` is σ-compact if and only `s` is σ-compact. -/ lemma Topology.IsEmbedding.isSigmaCompact_iff {f : X → Y} {s : Set X} (hf : IsEmbedding f) : IsSigmaCompact s ↔ IsSigmaCompact (f '' s) := hf.isInducing.isSigmaCompact_iff @[deprecated (since := "2024-10-26")] alias Embedding.isSigmaCompact_iff := IsEmbedding.isSigmaCompact_iff /-- Sets of subtype are σ-compact iff the image under a coercion is. -/ lemma Subtype.isSigmaCompact_iff {p : X → Prop} {s : Set { a // p a }} : IsSigmaCompact s ↔ IsSigmaCompact ((↑) '' s : Set X) := IsEmbedding.subtypeVal.isSigmaCompact_iff /-- A σ-compact space is a space that is the union of a countable collection of compact subspaces. Note that a locally compact separable T₂ space need not be σ-compact. The sequence can be extracted using `compactCovering`. -/ class SigmaCompactSpace (X : Type*) [TopologicalSpace X] : Prop where /-- In a σ-compact space, `Set.univ` is a σ-compact set. -/ isSigmaCompact_univ : IsSigmaCompact (univ : Set X) /-- A topological space is σ-compact iff `univ` is σ-compact. -/ lemma isSigmaCompact_univ_iff : IsSigmaCompact (univ : Set X) ↔ SigmaCompactSpace X := ⟨fun h => ⟨h⟩, fun h => h.1⟩ /-- In a σ-compact space, `univ` is σ-compact. -/ lemma isSigmaCompact_univ [h : SigmaCompactSpace X] : IsSigmaCompact (univ : Set X) := isSigmaCompact_univ_iff.mpr h /-- A topological space is σ-compact iff there exists a countable collection of compact subspaces that cover the entire space. -/ lemma SigmaCompactSpace_iff_exists_compact_covering : SigmaCompactSpace X ↔ ∃ K : ℕ → Set X, (∀ n, IsCompact (K n)) ∧ ⋃ n, K n = univ := by rw [← isSigmaCompact_univ_iff, IsSigmaCompact] lemma SigmaCompactSpace.exists_compact_covering [h : SigmaCompactSpace X] : ∃ K : ℕ → Set X, (∀ n, IsCompact (K n)) ∧ ⋃ n, K n = univ := SigmaCompactSpace_iff_exists_compact_covering.mp h /-- If `X` is σ-compact, `im f` is σ-compact. -/ lemma isSigmaCompact_range {f : X → Y} (hf : Continuous f) [SigmaCompactSpace X] : IsSigmaCompact (range f) := image_univ ▸ isSigmaCompact_univ.image hf /-- A subset `s` is σ-compact iff `s` (with the subspace topology) is a σ-compact space. -/ lemma isSigmaCompact_iff_isSigmaCompact_univ {s : Set X} : IsSigmaCompact s ↔ IsSigmaCompact (univ : Set s) := by rw [Subtype.isSigmaCompact_iff, image_univ, Subtype.range_coe] lemma isSigmaCompact_iff_sigmaCompactSpace {s : Set X} : IsSigmaCompact s ↔ SigmaCompactSpace s := isSigmaCompact_iff_isSigmaCompact_univ.trans isSigmaCompact_univ_iff -- see Note [lower instance priority] instance (priority := 200) CompactSpace.sigmaCompact [CompactSpace X] : SigmaCompactSpace X := ⟨⟨fun _ => univ, fun _ => isCompact_univ, iUnion_const _⟩⟩ -- The `alias` command creates a definition, triggering the defLemma linter. @[nolint defLemma, deprecated (since := "2024-11-13")] alias CompactSpace.sigma_compact := CompactSpace.sigmaCompact theorem SigmaCompactSpace.of_countable (S : Set (Set X)) (Hc : S.Countable) (Hcomp : ∀ s ∈ S, IsCompact s) (HU : ⋃₀ S = univ) : SigmaCompactSpace X := ⟨(exists_seq_cover_iff_countable ⟨_, isCompact_empty⟩).2 ⟨S, Hc, Hcomp, HU⟩⟩ -- see Note [lower instance priority] instance (priority := 100) sigmaCompactSpace_of_locallyCompact_secondCountable [LocallyCompactSpace X] [SecondCountableTopology X] : SigmaCompactSpace X := by choose K hKc hxK using fun x : X => exists_compact_mem_nhds x rcases countable_cover_nhds hxK with ⟨s, hsc, hsU⟩ refine SigmaCompactSpace.of_countable _ (hsc.image K) (forall_mem_image.2 fun x _ => hKc x) ?_ rwa [sUnion_image] -- The `alias` command creates a definition, triggering the defLemma linter. @[nolint defLemma, deprecated (since := "2024-11-13")] alias sigmaCompactSpace_of_locally_compact_second_countable := sigmaCompactSpace_of_locallyCompact_secondCountable section variable (X) variable [SigmaCompactSpace X] open SigmaCompactSpace /-- A choice of compact covering for a `σ`-compact space, chosen to be monotone. -/ def compactCovering : ℕ → Set X := Accumulate exists_compact_covering.choose theorem isCompact_compactCovering (n : ℕ) : IsCompact (compactCovering X n) := isCompact_accumulate (Classical.choose_spec SigmaCompactSpace.exists_compact_covering).1 n theorem iUnion_compactCovering : ⋃ n, compactCovering X n = univ := by rw [compactCovering, iUnion_accumulate] exact (Classical.choose_spec SigmaCompactSpace.exists_compact_covering).2 theorem iUnion_closure_compactCovering : ⋃ n, closure (compactCovering X n) = univ := eq_top_mono (iUnion_mono fun _ ↦ subset_closure) (iUnion_compactCovering X) @[mono, gcongr] theorem compactCovering_subset ⦃m n : ℕ⦄ (h : m ≤ n) : compactCovering X m ⊆ compactCovering X n := monotone_accumulate h variable {X} theorem exists_mem_compactCovering (x : X) : ∃ n, x ∈ compactCovering X n := iUnion_eq_univ_iff.mp (iUnion_compactCovering X) x instance [SigmaCompactSpace Y] : SigmaCompactSpace (X × Y) := ⟨⟨fun n => compactCovering X n ×ˢ compactCovering Y n, fun _ => (isCompact_compactCovering _ _).prod (isCompact_compactCovering _ _), by simp only [iUnion_prod_of_monotone (compactCovering_subset X) (compactCovering_subset Y), iUnion_compactCovering, univ_prod_univ]⟩⟩ instance [Finite ι] {X : ι → Type*} [∀ i, TopologicalSpace (X i)] [∀ i, SigmaCompactSpace (X i)] : SigmaCompactSpace (∀ i, X i) := by refine ⟨⟨fun n => Set.pi univ fun i => compactCovering (X i) n, fun n => isCompact_univ_pi fun i => isCompact_compactCovering (X i) _, ?_⟩⟩ rw [iUnion_univ_pi_of_monotone] · simp only [iUnion_compactCovering, pi_univ] · exact fun i => compactCovering_subset (X i) instance [SigmaCompactSpace Y] : SigmaCompactSpace (X ⊕ Y) := ⟨⟨fun n => Sum.inl '' compactCovering X n ∪ Sum.inr '' compactCovering Y n, fun n => ((isCompact_compactCovering X n).image continuous_inl).union ((isCompact_compactCovering Y n).image continuous_inr), by simp only [iUnion_union_distrib, ← image_iUnion, iUnion_compactCovering, image_univ, range_inl_union_range_inr]⟩⟩ instance [Countable ι] {X : ι → Type*} [∀ i, TopologicalSpace (X i)] [∀ i, SigmaCompactSpace (X i)] : SigmaCompactSpace (Σi, X i) := by cases isEmpty_or_nonempty ι · infer_instance · rcases exists_surjective_nat ι with ⟨f, hf⟩ refine ⟨⟨fun n => ⋃ k ≤ n, Sigma.mk (f k) '' compactCovering (X (f k)) n, fun n => ?_, ?_⟩⟩ · refine (finite_le_nat _).isCompact_biUnion fun k _ => ?_ exact (isCompact_compactCovering _ _).image continuous_sigmaMk · simp only [iUnion_eq_univ_iff, Sigma.forall, mem_iUnion, hf.forall] intro k y rcases exists_mem_compactCovering y with ⟨n, hn⟩ refine ⟨max k n, k, le_max_left _ _, mem_image_of_mem _ ?_⟩ exact compactCovering_subset _ (le_max_right _ _) hn protected lemma Topology.IsClosedEmbedding.sigmaCompactSpace {e : Y → X}
(he : IsClosedEmbedding e) : SigmaCompactSpace Y := ⟨⟨fun n => e ⁻¹' compactCovering X n, fun _ => he.isCompact_preimage (isCompact_compactCovering _ _), by rw [← preimage_iUnion, iUnion_compactCovering, preimage_univ]⟩⟩ theorem IsClosed.sigmaCompactSpace {s : Set X} (hs : IsClosed s) : SigmaCompactSpace s := hs.isClosedEmbedding_subtypeVal.sigmaCompactSpace
Mathlib/Topology/Compactness/SigmaCompact.lean
271
277
/- Copyright (c) 2023 David Loeffler. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: David Loeffler -/ import Mathlib.Analysis.Calculus.SmoothSeries import Mathlib.Analysis.NormedSpace.OperatorNorm.Prod import Mathlib.Analysis.SpecialFunctions.Gaussian.PoissonSummation import Mathlib.Data.Complex.FiniteDimensional /-! # The two-variable Jacobi theta function This file defines the two-variable Jacobi theta function $$\theta(z, \tau) = \sum_{n \in \mathbb{Z}} \exp (2 i \pi n z + i \pi n ^ 2 \tau),$$ and proves the functional equation relating the values at `(z, τ)` and `(z / τ, -1 / τ)`, using Poisson's summation formula. We also show holomorphy (jointly in both variables). Additionally, we show some analogous results about the derivative (in the `z`-variable) $$\theta'(z, τ) = \sum_{n \in \mathbb{Z}} 2 \pi i n \exp (2 i \pi n z + i \pi n ^ 2 \tau).$$ (Note that the Mellin transform of `θ` will give us functional equations for `L`-functions of even Dirichlet characters, and that of `θ'` will do the same for odd Dirichlet characters.) -/ open Complex Real Asymptotics Filter Topology open scoped ComplexConjugate noncomputable section section term_defs /-! ## Definitions of the summands -/ /-- Summand in the series for the Jacobi theta function. -/ def jacobiTheta₂_term (n : ℤ) (z τ : ℂ) : ℂ := cexp (2 * π * I * n * z + π * I * n ^ 2 * τ) /-- Summand in the series for the Fréchet derivative of the Jacobi theta function. -/ def jacobiTheta₂_term_fderiv (n : ℤ) (z τ : ℂ) : ℂ × ℂ →L[ℂ] ℂ := cexp (2 * π * I * n * z + π * I * n ^ 2 * τ) • ((2 * π * I * n) • (ContinuousLinearMap.fst ℂ ℂ ℂ) + (π * I * n ^ 2) • (ContinuousLinearMap.snd ℂ ℂ ℂ)) lemma hasFDerivAt_jacobiTheta₂_term (n : ℤ) (z τ : ℂ) : HasFDerivAt (fun p : ℂ × ℂ ↦ jacobiTheta₂_term n p.1 p.2) (jacobiTheta₂_term_fderiv n z τ) (z, τ) := by let f : ℂ × ℂ → ℂ := fun p ↦ 2 * π * I * n * p.1 + π * I * n ^ 2 * p.2 suffices HasFDerivAt f ((2 * π * I * n) • (ContinuousLinearMap.fst ℂ ℂ ℂ) + (π * I * n ^ 2) • (ContinuousLinearMap.snd ℂ ℂ ℂ)) (z, τ) from this.cexp exact (hasFDerivAt_fst.const_mul _).add (hasFDerivAt_snd.const_mul _) /-- Summand in the series for the `z`-derivative of the Jacobi theta function. -/ def jacobiTheta₂'_term (n : ℤ) (z τ : ℂ) := 2 * π * I * n * jacobiTheta₂_term n z τ end term_defs section term_bounds /-! ## Bounds for the summands We show that the sums of the three functions `jacobiTheta₂_term`, `jacobiTheta₂'_term` and `jacobiTheta₂_term_fderiv` are locally uniformly convergent in the domain `0 < im τ`, and diverge everywhere else. -/ lemma norm_jacobiTheta₂_term (n : ℤ) (z τ : ℂ) : ‖jacobiTheta₂_term n z τ‖ = rexp (-π * n ^ 2 * τ.im - 2 * π * n * z.im) := by rw [jacobiTheta₂_term, Complex.norm_exp, (by push_cast; ring : (2 * π : ℂ) * I * n * z + π * I * n ^ 2 * τ = (π * (2 * n):) * z * I + (π * n ^ 2 :) * τ * I), add_re, mul_I_re, im_ofReal_mul, mul_I_re, im_ofReal_mul] ring_nf /-- A uniform upper bound for `jacobiTheta₂_term` on compact subsets. -/ lemma norm_jacobiTheta₂_term_le {S T : ℝ} (hT : 0 < T) {z τ : ℂ} (hz : |im z| ≤ S) (hτ : T ≤ im τ) (n : ℤ) : ‖jacobiTheta₂_term n z τ‖ ≤ rexp (-π * (T * n ^ 2 - 2 * S * |n|)) := by simp_rw [norm_jacobiTheta₂_term, Real.exp_le_exp, sub_eq_add_neg, neg_mul, ← neg_add, neg_le_neg_iff, mul_comm (2 : ℝ), mul_assoc π, ← mul_add, mul_le_mul_left pi_pos, mul_comm T, mul_comm S] refine add_le_add (mul_le_mul le_rfl hτ hT.le (sq_nonneg _)) ?_ rw [← mul_neg, mul_assoc, mul_assoc, mul_le_mul_left two_pos, mul_comm, neg_mul, ← mul_neg] refine le_trans ?_ (neg_abs_le _) rw [mul_neg, neg_le_neg_iff, abs_mul, Int.cast_abs] exact mul_le_mul_of_nonneg_left hz (abs_nonneg _) /-- A uniform upper bound for `jacobiTheta₂'_term` on compact subsets. -/ lemma norm_jacobiTheta₂'_term_le {S T : ℝ} (hT : 0 < T) {z τ : ℂ} (hz : |im z| ≤ S) (hτ : T ≤ im τ) (n : ℤ) : ‖jacobiTheta₂'_term n z τ‖ ≤ 2 * π * |n| * rexp (-π * (T * n ^ 2 - 2 * S * |n|)) := by rw [jacobiTheta₂'_term, norm_mul] refine mul_le_mul (le_of_eq ?_) (norm_jacobiTheta₂_term_le hT hz hτ n) (norm_nonneg _) (by positivity) simp only [norm_mul, Complex.norm_two, norm_I, Complex.norm_of_nonneg pi_pos.le, norm_intCast, mul_one, Int.cast_abs] /-- The uniform bound we have given is summable, and remains so after multiplying by any fixed power of `|n|` (we shall need this for `k = 0, 1, 2`). -/ lemma summable_pow_mul_jacobiTheta₂_term_bound (S : ℝ) {T : ℝ} (hT : 0 < T) (k : ℕ) : Summable (fun n : ℤ ↦ (|n| ^ k : ℝ) * Real.exp (-π * (T * n ^ 2 - 2 * S * |n|))) := by suffices Summable (fun n : ℕ ↦ (n ^ k : ℝ) * Real.exp (-π * (T * n ^ 2 - 2 * S * n))) by apply Summable.of_nat_of_neg <;> simpa only [Int.cast_neg, neg_sq, abs_neg, Int.cast_natCast, Nat.abs_cast] apply summable_of_isBigO_nat (summable_pow_mul_exp_neg_nat_mul k zero_lt_one) apply IsBigO.mul (isBigO_refl _ _) refine Real.isBigO_exp_comp_exp_comp.mpr (Tendsto.isBoundedUnder_le_atBot ?_) simp_rw [← tendsto_neg_atTop_iff, Pi.sub_apply] conv => enter [1, n] rw [show -(-π * (T * n ^ 2 - 2 * S * n) - -1 * n) = n * (π * T * n - (2 * π * S + 1)) by ring] refine tendsto_natCast_atTop_atTop.atTop_mul_atTop₀ (tendsto_atTop_add_const_right _ _ ?_) exact tendsto_natCast_atTop_atTop.const_mul_atTop (mul_pos pi_pos hT) /-- The series defining the theta function is summable if and only if `0 < im τ`. -/ lemma summable_jacobiTheta₂_term_iff (z τ : ℂ) : Summable (jacobiTheta₂_term · z τ) ↔ 0 < im τ := by -- NB. This is a statement of no great mathematical interest; it is included largely to avoid -- having to impose `0 < im τ` as a hypothesis on many later lemmas. refine Iff.symm ⟨fun hτ ↦ ?_, fun h ↦ ?_⟩ -- do quicker implication first! · refine (summable_pow_mul_jacobiTheta₂_term_bound |im z| hτ 0).of_norm_bounded _ ?_ simpa only [pow_zero, one_mul] using norm_jacobiTheta₂_term_le hτ le_rfl le_rfl · by_contra! hτ rcases lt_or_eq_of_le hτ with hτ | hτ · -- easy case `im τ < 0` suffices Tendsto (fun n : ℕ ↦ ‖jacobiTheta₂_term ↑n z τ‖) atTop atTop by replace h := (h.comp_injective (fun a b ↦ Int.ofNat_inj.mp)).tendsto_atTop_zero.norm exact atTop_neBot.ne (disjoint_self.mp <| h.disjoint (disjoint_nhds_atTop _) this) simp only [norm_zero, Function.comp_def, norm_jacobiTheta₂_term, Int.cast_natCast] conv => enter [1, n] rw [show -π * n ^ 2 * τ.im - 2 * π * n * z.im = n * (n * (-π * τ.im) - 2 * π * z.im) by ring] refine tendsto_exp_atTop.comp (tendsto_natCast_atTop_atTop.atTop_mul_atTop₀ ?_) exact tendsto_atTop_add_const_right _ _ (tendsto_natCast_atTop_atTop.atTop_mul_const (mul_pos_of_neg_of_neg (neg_lt_zero.mpr pi_pos) hτ)) · -- case im τ = 0: 3-way split according to `im z` simp_rw [← summable_norm_iff (E := ℂ), norm_jacobiTheta₂_term, hτ, mul_zero, zero_sub] at h rcases lt_trichotomy (im z) 0 with hz | hz | hz · replace h := (h.comp_injective (fun a b ↦ Int.ofNat_inj.mp)).tendsto_atTop_zero simp_rw [Function.comp_def, Int.cast_natCast] at h refine atTop_neBot.ne (disjoint_self.mp <| h.disjoint (disjoint_nhds_atTop 0) ?_) refine tendsto_exp_atTop.comp ?_ simp only [tendsto_neg_atTop_iff, mul_assoc] apply Filter.Tendsto.const_mul_atBot two_pos exact (tendsto_natCast_atTop_atTop.atTop_mul_const_of_neg hz).const_mul_atBot pi_pos · revert h simpa only [hz, mul_zero, neg_zero, Real.exp_zero, summable_const_iff] using one_ne_zero · have : ((-↑·) : ℕ → ℤ).Injective := fun _ _ ↦ by simp only [neg_inj, Nat.cast_inj, imp_self] replace h := (h.comp_injective this).tendsto_atTop_zero simp_rw [Function.comp_def, Int.cast_neg, Int.cast_natCast, mul_neg, neg_mul, neg_neg] at h refine atTop_neBot.ne (disjoint_self.mp <| h.disjoint (disjoint_nhds_atTop 0) ?_) exact tendsto_exp_atTop.comp ((tendsto_natCast_atTop_atTop.const_mul_atTop (mul_pos two_pos pi_pos)).atTop_mul_const hz) lemma norm_jacobiTheta₂_term_fderiv_le (n : ℤ) (z τ : ℂ) : ‖jacobiTheta₂_term_fderiv n z τ‖ ≤ 3 * π * |n| ^ 2 * ‖jacobiTheta₂_term n z τ‖ := by -- this is slow to elaborate so do it once and reuse: have hns (a : ℂ) (f : (ℂ × ℂ) →L[ℂ] ℂ) : ‖a • f‖ = ‖a‖ * ‖f‖ := norm_smul a f rw [jacobiTheta₂_term_fderiv, jacobiTheta₂_term, hns, mul_comm _ ‖cexp _‖, (by norm_num : (3 : ℝ) = 2 + 1), add_mul, add_mul] refine mul_le_mul_of_nonneg_left ((norm_add_le _ _).trans (add_le_add ?_ ?_)) (norm_nonneg _) · simp_rw [hns, norm_mul, ← ofReal_ofNat, ← ofReal_intCast, norm_real, norm_of_nonneg zero_le_two, Real.norm_of_nonneg pi_pos.le, norm_I, mul_one, Real.norm_eq_abs, Int.cast_abs, mul_assoc] refine mul_le_mul_of_nonneg_left (mul_le_mul_of_nonneg_left ?_ pi_pos.le) two_pos.le refine le_trans ?_ (?_ : |(n : ℝ)| ≤ |(n : ℝ)| ^ 2) · exact mul_le_of_le_one_right (abs_nonneg _) (ContinuousLinearMap.norm_fst_le ..) · exact_mod_cast Int.le_self_sq |n| · simp_rw [hns, norm_mul, one_mul, norm_I, mul_one, norm_real, norm_of_nonneg pi_pos.le, ← ofReal_intCast, ← ofReal_pow, norm_real, Real.norm_eq_abs, Int.cast_abs, abs_pow] apply mul_le_of_le_one_right (mul_nonneg pi_pos.le (pow_nonneg (abs_nonneg _) _)) exact ContinuousLinearMap.norm_snd_le .. lemma norm_jacobiTheta₂_term_fderiv_ge (n : ℤ) (z τ : ℂ) : π * |n| ^ 2 * ‖jacobiTheta₂_term n z τ‖ ≤ ‖jacobiTheta₂_term_fderiv n z τ‖ := by have : ‖(jacobiTheta₂_term_fderiv n z τ) (0, 1)‖ ≤ ‖jacobiTheta₂_term_fderiv n z τ‖ := by refine (ContinuousLinearMap.le_opNorm _ _).trans ?_ simp_rw [Prod.norm_def, norm_one, norm_zero, max_eq_right zero_le_one, mul_one, le_refl] refine le_trans ?_ this simp_rw [jacobiTheta₂_term_fderiv, jacobiTheta₂_term, ContinuousLinearMap.coe_smul', Pi.smul_apply, ContinuousLinearMap.add_apply, ContinuousLinearMap.coe_smul', ContinuousLinearMap.coe_fst', ContinuousLinearMap.coe_snd', Pi.smul_apply, smul_zero, zero_add, smul_eq_mul, mul_one, mul_comm _ ‖cexp _‖, norm_mul] refine mul_le_mul_of_nonneg_left (le_of_eq ?_) (norm_nonneg _) simp_rw [norm_real, norm_of_nonneg pi_pos.le, norm_I, mul_one, Int.cast_abs, ← norm_intCast, norm_pow] lemma summable_jacobiTheta₂_term_fderiv_iff (z τ : ℂ) : Summable (jacobiTheta₂_term_fderiv · z τ) ↔ 0 < im τ := by constructor · rw [← summable_jacobiTheta₂_term_iff (z := z)] intro h have := h.norm refine this.of_norm_bounded_eventually _ ?_ have : ∀ᶠ (n : ℤ) in cofinite, n ≠ 0 := Int.cofinite_eq ▸ (mem_sup.mpr ⟨eventually_ne_atBot 0, eventually_ne_atTop 0⟩) filter_upwards [this] with n hn refine le_trans ?_ (norm_jacobiTheta₂_term_fderiv_ge n z τ) apply le_mul_of_one_le_left (norm_nonneg _) refine one_le_pi_div_two.trans (mul_le_mul_of_nonneg_left ?_ pi_pos.le) refine (by norm_num : 2⁻¹ ≤ (1 : ℝ)).trans ?_ rw [one_le_sq_iff_one_le_abs, ← Int.cast_abs, abs_abs, ← Int.cast_one, Int.cast_le] exact Int.one_le_abs hn · intro hτ refine ((summable_pow_mul_jacobiTheta₂_term_bound |z.im| hτ 2).mul_left (3 * π)).of_norm_bounded _ (fun n ↦ ?_) refine (norm_jacobiTheta₂_term_fderiv_le n z τ).trans (?_ : 3 * π * |n| ^ 2 * ‖jacobiTheta₂_term n z τ‖ ≤ _) simp_rw [mul_assoc (3 * π)] refine mul_le_mul_of_nonneg_left ?_ (mul_pos (by norm_num : 0 < (3 : ℝ)) pi_pos).le refine mul_le_mul_of_nonneg_left ?_ (pow_nonneg (Int.cast_nonneg.mpr (abs_nonneg _)) _) exact norm_jacobiTheta₂_term_le hτ le_rfl le_rfl n lemma summable_jacobiTheta₂'_term_iff (z τ : ℂ) : Summable (jacobiTheta₂'_term · z τ) ↔ 0 < im τ := by constructor · rw [← summable_jacobiTheta₂_term_iff (z := z)] refine fun h ↦ (h.norm.mul_left (2 * π)⁻¹).of_norm_bounded_eventually _ ?_ have : ∀ᶠ (n : ℤ) in cofinite, n ≠ 0 := Int.cofinite_eq ▸ (mem_sup.mpr ⟨eventually_ne_atBot 0, eventually_ne_atTop 0⟩) filter_upwards [this] with n hn rw [jacobiTheta₂'_term, norm_mul, ← mul_assoc] refine le_mul_of_one_le_left (norm_nonneg _) ?_ simp_rw [norm_mul, norm_I, norm_real, mul_one, norm_of_nonneg pi_pos.le, ← ofReal_ofNat, norm_real, norm_of_nonneg two_pos.le, ← ofReal_intCast, norm_real, Real.norm_eq_abs, ← Int.cast_abs, ← mul_assoc _ (2 * π), inv_mul_cancel₀ (mul_pos two_pos pi_pos).ne', one_mul] rw [← Int.cast_one, Int.cast_le] exact Int.one_le_abs hn · refine fun hτ ↦ ((summable_pow_mul_jacobiTheta₂_term_bound |z.im| hτ 1).mul_left (2 * π)).of_norm_bounded _ (fun n ↦ ?_) rw [jacobiTheta₂'_term, norm_mul, ← mul_assoc, pow_one] refine mul_le_mul (le_of_eq ?_) (norm_jacobiTheta₂_term_le hτ le_rfl le_rfl n) (norm_nonneg _) (by positivity) simp_rw [norm_mul, Complex.norm_two, norm_I, Complex.norm_of_nonneg pi_pos.le, norm_intCast, mul_one, Int.cast_abs] end term_bounds /-! ## Definitions of the functions -/ /-- The two-variable Jacobi theta function, `θ z τ = ∑' (n : ℤ), cexp (2 * π * I * n * z + π * I * n ^ 2 * τ)`. -/ def jacobiTheta₂ (z τ : ℂ) : ℂ := ∑' n : ℤ, jacobiTheta₂_term n z τ /-- Fréchet derivative of the two-variable Jacobi theta function. -/ def jacobiTheta₂_fderiv (z τ : ℂ) : ℂ × ℂ →L[ℂ] ℂ := ∑' n : ℤ, jacobiTheta₂_term_fderiv n z τ /-- The `z`-derivative of the Jacobi theta function, `θ' z τ = ∑' (n : ℤ), 2 * π * I * n * cexp (2 * π * I * n * z + π * I * n ^ 2 * τ)`. -/ def jacobiTheta₂' (z τ : ℂ) := ∑' n : ℤ, jacobiTheta₂'_term n z τ lemma hasSum_jacobiTheta₂_term (z : ℂ) {τ : ℂ} (hτ : 0 < im τ) : HasSum (fun n ↦ jacobiTheta₂_term n z τ) (jacobiTheta₂ z τ) := ((summable_jacobiTheta₂_term_iff z τ).mpr hτ).hasSum lemma hasSum_jacobiTheta₂_term_fderiv (z : ℂ) {τ : ℂ} (hτ : 0 < im τ) : HasSum (fun n ↦ jacobiTheta₂_term_fderiv n z τ) (jacobiTheta₂_fderiv z τ) := ((summable_jacobiTheta₂_term_fderiv_iff z τ).mpr hτ).hasSum lemma hasSum_jacobiTheta₂'_term (z : ℂ) {τ : ℂ} (hτ : 0 < im τ) : HasSum (fun n ↦ jacobiTheta₂'_term n z τ) (jacobiTheta₂' z τ) := ((summable_jacobiTheta₂'_term_iff z τ).mpr hτ).hasSum lemma jacobiTheta₂_undef (z : ℂ) {τ : ℂ} (hτ : im τ ≤ 0) : jacobiTheta₂ z τ = 0 := by apply tsum_eq_zero_of_not_summable rw [summable_jacobiTheta₂_term_iff] exact not_lt.mpr hτ
lemma jacobiTheta₂_fderiv_undef (z : ℂ) {τ : ℂ} (hτ : im τ ≤ 0) : jacobiTheta₂_fderiv z τ = 0 := by apply tsum_eq_zero_of_not_summable rw [summable_jacobiTheta₂_term_fderiv_iff]
Mathlib/NumberTheory/ModularForms/JacobiTheta/TwoVariable.lean
277
280
/- Copyright (c) 2019 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Yaël Dillies -/ import Mathlib.Algebra.Module.BigOperators import Mathlib.GroupTheory.Perm.Basic import Mathlib.GroupTheory.Perm.Finite import Mathlib.GroupTheory.Perm.List import Mathlib.GroupTheory.Perm.Sign /-! # Cycles of a permutation This file starts the theory of cycles in permutations. ## Main definitions In the following, `f : Equiv.Perm β`. * `Equiv.Perm.SameCycle`: `f.SameCycle x y` when `x` and `y` are in the same cycle of `f`. * `Equiv.Perm.IsCycle`: `f` is a cycle if any two nonfixed points of `f` are related by repeated applications of `f`, and `f` is not the identity. * `Equiv.Perm.IsCycleOn`: `f` is a cycle on a set `s` when any two points of `s` are related by repeated applications of `f`. ## Notes `Equiv.Perm.IsCycle` and `Equiv.Perm.IsCycleOn` are different in three ways: * `IsCycle` is about the entire type while `IsCycleOn` is restricted to a set. * `IsCycle` forbids the identity while `IsCycleOn` allows it (if `s` is a subsingleton). * `IsCycleOn` forbids fixed points on `s` (if `s` is nontrivial), while `IsCycle` allows them. -/ open Equiv Function Finset variable {ι α β : Type*} namespace Equiv.Perm /-! ### `SameCycle` -/ section SameCycle variable {f g : Perm α} {p : α → Prop} {x y z : α} /-- The equivalence relation indicating that two points are in the same cycle of a permutation. -/ def SameCycle (f : Perm α) (x y : α) : Prop := ∃ i : ℤ, (f ^ i) x = y @[refl] theorem SameCycle.refl (f : Perm α) (x : α) : SameCycle f x x := ⟨0, rfl⟩ theorem SameCycle.rfl : SameCycle f x x := SameCycle.refl _ _ protected theorem _root_.Eq.sameCycle (h : x = y) (f : Perm α) : f.SameCycle x y := by rw [h] @[symm] theorem SameCycle.symm : SameCycle f x y → SameCycle f y x := fun ⟨i, hi⟩ => ⟨-i, by rw [zpow_neg, ← hi, inv_apply_self]⟩ theorem sameCycle_comm : SameCycle f x y ↔ SameCycle f y x := ⟨SameCycle.symm, SameCycle.symm⟩ @[trans] theorem SameCycle.trans : SameCycle f x y → SameCycle f y z → SameCycle f x z := fun ⟨i, hi⟩ ⟨j, hj⟩ => ⟨j + i, by rw [zpow_add, mul_apply, hi, hj]⟩ variable (f) in theorem SameCycle.equivalence : Equivalence (SameCycle f) := ⟨SameCycle.refl f, SameCycle.symm, SameCycle.trans⟩ /-- The setoid defined by the `SameCycle` relation. -/ def SameCycle.setoid (f : Perm α) : Setoid α where r := f.SameCycle iseqv := SameCycle.equivalence f @[simp] theorem sameCycle_one : SameCycle 1 x y ↔ x = y := by simp [SameCycle] @[simp] theorem sameCycle_inv : SameCycle f⁻¹ x y ↔ SameCycle f x y := (Equiv.neg _).exists_congr_left.trans <| by simp [SameCycle] alias ⟨SameCycle.of_inv, SameCycle.inv⟩ := sameCycle_inv @[simp] theorem sameCycle_conj : SameCycle (g * f * g⁻¹) x y ↔ SameCycle f (g⁻¹ x) (g⁻¹ y) := exists_congr fun i => by simp [conj_zpow, eq_inv_iff_eq] theorem SameCycle.conj : SameCycle f x y → SameCycle (g * f * g⁻¹) (g x) (g y) := by simp [sameCycle_conj] theorem SameCycle.apply_eq_self_iff : SameCycle f x y → (f x = x ↔ f y = y) := fun ⟨i, hi⟩ => by rw [← hi, ← mul_apply, ← zpow_one_add, add_comm, zpow_add_one, mul_apply, (f ^ i).injective.eq_iff] theorem SameCycle.eq_of_left (h : SameCycle f x y) (hx : IsFixedPt f x) : x = y := let ⟨_, hn⟩ := h (hx.perm_zpow _).eq.symm.trans hn theorem SameCycle.eq_of_right (h : SameCycle f x y) (hy : IsFixedPt f y) : x = y := h.eq_of_left <| h.apply_eq_self_iff.2 hy @[simp] theorem sameCycle_apply_left : SameCycle f (f x) y ↔ SameCycle f x y := (Equiv.addRight 1).exists_congr_left.trans <| by simp [zpow_sub, SameCycle, Int.add_neg_one, Function.comp] @[simp] theorem sameCycle_apply_right : SameCycle f x (f y) ↔ SameCycle f x y := by rw [sameCycle_comm, sameCycle_apply_left, sameCycle_comm] @[simp] theorem sameCycle_inv_apply_left : SameCycle f (f⁻¹ x) y ↔ SameCycle f x y := by rw [← sameCycle_apply_left, apply_inv_self] @[simp] theorem sameCycle_inv_apply_right : SameCycle f x (f⁻¹ y) ↔ SameCycle f x y := by rw [← sameCycle_apply_right, apply_inv_self] @[simp] theorem sameCycle_zpow_left {n : ℤ} : SameCycle f ((f ^ n) x) y ↔ SameCycle f x y := (Equiv.addRight (n : ℤ)).exists_congr_left.trans <| by simp [SameCycle, zpow_add] @[simp] theorem sameCycle_zpow_right {n : ℤ} : SameCycle f x ((f ^ n) y) ↔ SameCycle f x y := by rw [sameCycle_comm, sameCycle_zpow_left, sameCycle_comm] @[simp] theorem sameCycle_pow_left {n : ℕ} : SameCycle f ((f ^ n) x) y ↔ SameCycle f x y := by rw [← zpow_natCast, sameCycle_zpow_left] @[simp] theorem sameCycle_pow_right {n : ℕ} : SameCycle f x ((f ^ n) y) ↔ SameCycle f x y := by rw [← zpow_natCast, sameCycle_zpow_right] alias ⟨SameCycle.of_apply_left, SameCycle.apply_left⟩ := sameCycle_apply_left alias ⟨SameCycle.of_apply_right, SameCycle.apply_right⟩ := sameCycle_apply_right alias ⟨SameCycle.of_inv_apply_left, SameCycle.inv_apply_left⟩ := sameCycle_inv_apply_left alias ⟨SameCycle.of_inv_apply_right, SameCycle.inv_apply_right⟩ := sameCycle_inv_apply_right alias ⟨SameCycle.of_pow_left, SameCycle.pow_left⟩ := sameCycle_pow_left alias ⟨SameCycle.of_pow_right, SameCycle.pow_right⟩ := sameCycle_pow_right alias ⟨SameCycle.of_zpow_left, SameCycle.zpow_left⟩ := sameCycle_zpow_left alias ⟨SameCycle.of_zpow_right, SameCycle.zpow_right⟩ := sameCycle_zpow_right theorem SameCycle.of_pow {n : ℕ} : SameCycle (f ^ n) x y → SameCycle f x y := fun ⟨m, h⟩ => ⟨n * m, by simp [zpow_mul, h]⟩ theorem SameCycle.of_zpow {n : ℤ} : SameCycle (f ^ n) x y → SameCycle f x y := fun ⟨m, h⟩ => ⟨n * m, by simp [zpow_mul, h]⟩ @[simp] theorem sameCycle_subtypePerm {h} {x y : { x // p x }} : (f.subtypePerm h).SameCycle x y ↔ f.SameCycle x y := exists_congr fun n => by simp [Subtype.ext_iff] alias ⟨_, SameCycle.subtypePerm⟩ := sameCycle_subtypePerm @[simp] theorem sameCycle_extendDomain {p : β → Prop} [DecidablePred p] {f : α ≃ Subtype p} : SameCycle (g.extendDomain f) (f x) (f y) ↔ g.SameCycle x y := exists_congr fun n => by rw [← extendDomain_zpow, extendDomain_apply_image, Subtype.coe_inj, f.injective.eq_iff] alias ⟨_, SameCycle.extendDomain⟩ := sameCycle_extendDomain theorem SameCycle.exists_pow_eq' [Finite α] : SameCycle f x y → ∃ i < orderOf f, (f ^ i) x = y := by rintro ⟨k, rfl⟩ use (k % orderOf f).natAbs have h₀ := Int.natCast_pos.mpr (orderOf_pos f) have h₁ := Int.emod_nonneg k h₀.ne' rw [← zpow_natCast, Int.natAbs_of_nonneg h₁, zpow_mod_orderOf] refine ⟨?_, by rfl⟩ rw [← Int.ofNat_lt, Int.natAbs_of_nonneg h₁] exact Int.emod_lt_of_pos _ h₀ theorem SameCycle.exists_pow_eq'' [Finite α] (h : SameCycle f x y) : ∃ i : ℕ, 0 < i ∧ i ≤ orderOf f ∧ (f ^ i) x = y := by obtain ⟨_ | i, hi, rfl⟩ := h.exists_pow_eq' · refine ⟨orderOf f, orderOf_pos f, le_rfl, ?_⟩ rw [pow_orderOf_eq_one, pow_zero] · exact ⟨i.succ, i.zero_lt_succ, hi.le, by rfl⟩ theorem SameCycle.exists_fin_pow_eq [Finite α] (h : SameCycle f x y) : ∃ i : Fin (orderOf f), (f ^ (i : ℕ)) x = y := by obtain ⟨i, hi, hx⟩ := SameCycle.exists_pow_eq' h exact ⟨⟨i, hi⟩, hx⟩ theorem SameCycle.exists_nat_pow_eq [Finite α] (h : SameCycle f x y) : ∃ i : ℕ, (f ^ i) x = y := by obtain ⟨i, _, hi⟩ := h.exists_pow_eq' exact ⟨i, hi⟩ instance (f : Perm α) [DecidableRel (SameCycle f)] : DecidableRel (SameCycle f⁻¹) := fun x y => decidable_of_iff (f.SameCycle x y) (sameCycle_inv).symm instance (priority := 100) [DecidableEq α] : DecidableRel (SameCycle (1 : Perm α)) := fun x y => decidable_of_iff (x = y) sameCycle_one.symm end SameCycle /-! ### `IsCycle` -/ section IsCycle variable {f g : Perm α} {x y : α} /-- A cycle is a non identity permutation where any two nonfixed points of the permutation are related by repeated application of the permutation. -/ def IsCycle (f : Perm α) : Prop := ∃ x, f x ≠ x ∧ ∀ ⦃y⦄, f y ≠ y → SameCycle f x y theorem IsCycle.ne_one (h : IsCycle f) : f ≠ 1 := fun hf => by simp [hf, IsCycle] at h @[simp] theorem not_isCycle_one : ¬(1 : Perm α).IsCycle := fun H => H.ne_one rfl protected theorem IsCycle.sameCycle (hf : IsCycle f) (hx : f x ≠ x) (hy : f y ≠ y) : SameCycle f x y := let ⟨g, hg⟩ := hf let ⟨a, ha⟩ := hg.2 hx let ⟨b, hb⟩ := hg.2 hy ⟨b - a, by rw [← ha, ← mul_apply, ← zpow_add, sub_add_cancel, hb]⟩ theorem IsCycle.exists_zpow_eq : IsCycle f → f x ≠ x → f y ≠ y → ∃ i : ℤ, (f ^ i) x = y := IsCycle.sameCycle theorem IsCycle.inv (hf : IsCycle f) : IsCycle f⁻¹ := hf.imp fun _ ⟨hx, h⟩ => ⟨inv_eq_iff_eq.not.2 hx.symm, fun _ hy => (h <| inv_eq_iff_eq.not.2 hy.symm).inv⟩ @[simp] theorem isCycle_inv : IsCycle f⁻¹ ↔ IsCycle f := ⟨fun h => h.inv, IsCycle.inv⟩ theorem IsCycle.conj : IsCycle f → IsCycle (g * f * g⁻¹) := by rintro ⟨x, hx, h⟩ refine ⟨g x, by simp [coe_mul, inv_apply_self, hx], fun y hy => ?_⟩ rw [← apply_inv_self g y] exact (h <| eq_inv_iff_eq.not.2 hy).conj protected theorem IsCycle.extendDomain {p : β → Prop} [DecidablePred p] (f : α ≃ Subtype p) : IsCycle g → IsCycle (g.extendDomain f) := by rintro ⟨a, ha, ha'⟩ refine ⟨f a, ?_, fun b hb => ?_⟩ · rw [extendDomain_apply_image] exact Subtype.coe_injective.ne (f.injective.ne ha) have h : b = f (f.symm ⟨b, of_not_not <| hb ∘ extendDomain_apply_not_subtype _ _⟩) := by rw [apply_symm_apply, Subtype.coe_mk] rw [h] at hb ⊢ simp only [extendDomain_apply_image, Subtype.coe_injective.ne_iff, f.injective.ne_iff] at hb exact (ha' hb).extendDomain theorem isCycle_iff_sameCycle (hx : f x ≠ x) : IsCycle f ↔ ∀ {y}, SameCycle f x y ↔ f y ≠ y := ⟨fun hf y => ⟨fun ⟨i, hi⟩ hy => hx <| by rw [← zpow_apply_eq_self_of_apply_eq_self hy i, (f ^ i).injective.eq_iff] at hi rw [hi, hy], hf.exists_zpow_eq hx⟩, fun h => ⟨x, hx, fun _ hy => h.2 hy⟩⟩ section Finite variable [Finite α] theorem IsCycle.exists_pow_eq (hf : IsCycle f) (hx : f x ≠ x) (hy : f y ≠ y) : ∃ i : ℕ, (f ^ i) x = y := by let ⟨n, hn⟩ := hf.exists_zpow_eq hx hy classical exact ⟨(n % orderOf f).toNat, by {have := n.emod_nonneg (Int.natCast_ne_zero.mpr (ne_of_gt (orderOf_pos f))) rwa [← zpow_natCast, Int.toNat_of_nonneg this, zpow_mod_orderOf]}⟩ end Finite variable [DecidableEq α] theorem isCycle_swap (hxy : x ≠ y) : IsCycle (swap x y) := ⟨y, by rwa [swap_apply_right], fun a (ha : ite (a = x) y (ite (a = y) x a) ≠ a) => if hya : y = a then ⟨0, hya⟩ else ⟨1, by rw [zpow_one, swap_apply_def] split_ifs at * <;> tauto⟩⟩ protected theorem IsSwap.isCycle : IsSwap f → IsCycle f := by rintro ⟨x, y, hxy, rfl⟩ exact isCycle_swap hxy variable [Fintype α] theorem IsCycle.two_le_card_support (h : IsCycle f) : 2 ≤ #f.support := two_le_card_support_of_ne_one h.ne_one /-- The subgroup generated by a cycle is in bijection with its support -/ noncomputable def IsCycle.zpowersEquivSupport {σ : Perm α} (hσ : IsCycle σ) : (Subgroup.zpowers σ) ≃ σ.support := Equiv.ofBijective (fun (τ : ↥ ((Subgroup.zpowers σ) : Set (Perm α))) => ⟨(τ : Perm α) (Classical.choose hσ), by obtain ⟨τ, n, rfl⟩ := τ rw [Subtype.coe_mk, zpow_apply_mem_support, mem_support] exact (Classical.choose_spec hσ).1⟩) (by constructor · rintro ⟨a, m, rfl⟩ ⟨b, n, rfl⟩ h ext y by_cases hy : σ y = y · simp_rw [zpow_apply_eq_self_of_apply_eq_self hy] · obtain ⟨i, rfl⟩ := (Classical.choose_spec hσ).2 hy rw [Subtype.coe_mk, Subtype.coe_mk, zpow_apply_comm σ m i, zpow_apply_comm σ n i] exact congr_arg _ (Subtype.ext_iff.mp h) · rintro ⟨y, hy⟩ rw [mem_support] at hy obtain ⟨n, rfl⟩ := (Classical.choose_spec hσ).2 hy exact ⟨⟨σ ^ n, n, rfl⟩, rfl⟩) @[simp] theorem IsCycle.zpowersEquivSupport_apply {σ : Perm α} (hσ : IsCycle σ) {n : ℕ} : hσ.zpowersEquivSupport ⟨σ ^ n, n, rfl⟩ = ⟨(σ ^ n) (Classical.choose hσ), pow_apply_mem_support.2 (mem_support.2 (Classical.choose_spec hσ).1)⟩ := rfl @[simp] theorem IsCycle.zpowersEquivSupport_symm_apply {σ : Perm α} (hσ : IsCycle σ) (n : ℕ) : hσ.zpowersEquivSupport.symm ⟨(σ ^ n) (Classical.choose hσ), pow_apply_mem_support.2 (mem_support.2 (Classical.choose_spec hσ).1)⟩ = ⟨σ ^ n, n, rfl⟩ :=
(Equiv.symm_apply_eq _).2 hσ.zpowersEquivSupport_apply protected theorem IsCycle.orderOf (hf : IsCycle f) : orderOf f = #f.support := by rw [← Fintype.card_zpowers, ← Fintype.card_coe] convert Fintype.card_congr (IsCycle.zpowersEquivSupport hf) theorem isCycle_swap_mul_aux₁ {α : Type*} [DecidableEq α] :
Mathlib/GroupTheory/Perm/Cycle/Basic.lean
347
353
/- Copyright (c) 2022 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import Mathlib.Analysis.Calculus.FDeriv.Add import Mathlib.Analysis.Calculus.FDeriv.Equiv import Mathlib.Analysis.Calculus.FDeriv.Prod import Mathlib.Analysis.Calculus.Monotone import Mathlib.Topology.EMetricSpace.BoundedVariation /-! # Almost everywhere differentiability of functions with locally bounded variation In this file we show that a bounded variation function is differentiable almost everywhere. This implies that Lipschitz functions from the real line into finite-dimensional vector space are also differentiable almost everywhere. ## Main definitions and results * `LocallyBoundedVariationOn.ae_differentiableWithinAt` shows that a bounded variation function into a finite dimensional real vector space is differentiable almost everywhere. * `LipschitzOnWith.ae_differentiableWithinAt` is the same result for Lipschitz functions. We also give several variations around these results. -/ open scoped NNReal ENNReal Topology open Set MeasureTheory Filter variable {α : Type*} [LinearOrder α] {E : Type*} [PseudoEMetricSpace E] /-! ## -/ variable {V : Type*} [NormedAddCommGroup V] [NormedSpace ℝ V] [FiniteDimensional ℝ V] namespace LocallyBoundedVariationOn /-- A bounded variation function into `ℝ` is differentiable almost everywhere. Superseded by `ae_differentiableWithinAt_of_mem`. -/ theorem ae_differentiableWithinAt_of_mem_real {f : ℝ → ℝ} {s : Set ℝ} (h : LocallyBoundedVariationOn f s) : ∀ᵐ x, x ∈ s → DifferentiableWithinAt ℝ f s x := by obtain ⟨p, q, hp, hq, rfl⟩ : ∃ p q, MonotoneOn p s ∧ MonotoneOn q s ∧ f = p - q := h.exists_monotoneOn_sub_monotoneOn filter_upwards [hp.ae_differentiableWithinAt_of_mem, hq.ae_differentiableWithinAt_of_mem] with x hxp hxq xs exact (hxp xs).sub (hxq xs) /-- A bounded variation function into a finite dimensional product vector space is differentiable almost everywhere. Superseded by `ae_differentiableWithinAt_of_mem`. -/ theorem ae_differentiableWithinAt_of_mem_pi {ι : Type*} [Fintype ι] {f : ℝ → ι → ℝ} {s : Set ℝ} (h : LocallyBoundedVariationOn f s) : ∀ᵐ x, x ∈ s → DifferentiableWithinAt ℝ f s x := by have A : ∀ i : ι, LipschitzWith 1 fun x : ι → ℝ => x i := fun i => LipschitzWith.eval i have : ∀ i : ι, ∀ᵐ x, x ∈ s → DifferentiableWithinAt ℝ (fun x : ℝ => f x i) s x := fun i ↦ by apply ae_differentiableWithinAt_of_mem_real exact LipschitzWith.comp_locallyBoundedVariationOn (A i) h filter_upwards [ae_all_iff.2 this] with x hx xs exact differentiableWithinAt_pi.2 fun i => hx i xs /-- A real function into a finite dimensional real vector space with bounded variation on a set is differentiable almost everywhere in this set. -/ theorem ae_differentiableWithinAt_of_mem {f : ℝ → V} {s : Set ℝ} (h : LocallyBoundedVariationOn f s) : ∀ᵐ x, x ∈ s → DifferentiableWithinAt ℝ f s x := by let A := (Basis.ofVectorSpace ℝ V).equivFun.toContinuousLinearEquiv suffices H : ∀ᵐ x, x ∈ s → DifferentiableWithinAt ℝ (A ∘ f) s x by filter_upwards [H] with x hx xs have : f = (A.symm ∘ A) ∘ f := by simp only [ContinuousLinearEquiv.symm_comp_self, Function.id_comp] rw [this] exact A.symm.differentiableAt.comp_differentiableWithinAt x (hx xs) apply ae_differentiableWithinAt_of_mem_pi exact A.lipschitz.comp_locallyBoundedVariationOn h /-- A real function into a finite dimensional real vector space with bounded variation on a set is differentiable almost everywhere in this set. -/ theorem ae_differentiableWithinAt {f : ℝ → V} {s : Set ℝ} (h : LocallyBoundedVariationOn f s) (hs : MeasurableSet s) : ∀ᵐ x ∂volume.restrict s, DifferentiableWithinAt ℝ f s x := by rw [ae_restrict_iff' hs] exact h.ae_differentiableWithinAt_of_mem /-- A real function into a finite dimensional real vector space with bounded variation is differentiable almost everywhere. -/ theorem ae_differentiableAt {f : ℝ → V} (h : LocallyBoundedVariationOn f univ) : ∀ᵐ x, DifferentiableAt ℝ f x := by filter_upwards [h.ae_differentiableWithinAt_of_mem] with x hx rw [differentiableWithinAt_univ] at hx exact hx (mem_univ _) end LocallyBoundedVariationOn /-- A real function into a finite dimensional real vector space which is Lipschitz on a set is differentiable almost everywhere in this set. For the general Rademacher theorem assuming that the source space is finite dimensional, see `LipschitzOnWith.ae_differentiableWithinAt_of_mem`. -/ theorem LipschitzOnWith.ae_differentiableWithinAt_of_mem_real {C : ℝ≥0} {f : ℝ → V} {s : Set ℝ} (h : LipschitzOnWith C f s) : ∀ᵐ x, x ∈ s → DifferentiableWithinAt ℝ f s x := h.locallyBoundedVariationOn.ae_differentiableWithinAt_of_mem /-- A real function into a finite dimensional real vector space which is Lipschitz on a set is differentiable almost everywhere in this set. For the general Rademacher theorem assuming that the source space is finite dimensional, see `LipschitzOnWith.ae_differentiableWithinAt`. -/ theorem LipschitzOnWith.ae_differentiableWithinAt_real {C : ℝ≥0} {f : ℝ → V} {s : Set ℝ} (h : LipschitzOnWith C f s) (hs : MeasurableSet s) : ∀ᵐ x ∂volume.restrict s, DifferentiableWithinAt ℝ f s x := h.locallyBoundedVariationOn.ae_differentiableWithinAt hs /-- A real Lipschitz function into a finite dimensional real vector space is differentiable almost everywhere. For the general Rademacher theorem assuming that the source space is finite dimensional, see `LipschitzWith.ae_differentiableAt`. -/ theorem LipschitzWith.ae_differentiableAt_real {C : ℝ≥0} {f : ℝ → V} (h : LipschitzWith C f) : ∀ᵐ x, DifferentiableAt ℝ f x := (h.locallyBoundedVariationOn univ).ae_differentiableAt
Mathlib/Analysis/BoundedVariation.lean
893
896