source
stringlengths
17
118
lean4
stringlengths
0
335k
.lake/packages/mathlib/Mathlib/Algebra/Ring/Divisibility/Basic.lean
import Mathlib.Algebra.Divisibility.Hom import Mathlib.Algebra.Group.Equiv.Basic import Mathlib.Algebra.Ring.Defs /-! # Lemmas about divisibility in rings Note that this file is imported by basic tactics like `linarith` and so must have only minimal imports. Further results about divisibility in rings may be found in `Mathlib/Algebra/Ring/Divisibility/Lemmas.lean` which is not subject to this import constraint. -/ variable {α β : Type*} section Semigroup variable [Semigroup α] [Semigroup β] {F : Type*} [EquivLike F α β] [MulEquivClass F α β] theorem map_dvd_iff (f : F) {a b} : f a ∣ f b ↔ a ∣ b := let f := MulEquivClass.toMulEquiv f ⟨fun h ↦ by rw [← f.left_inv a, ← f.left_inv b]; exact map_dvd f.symm h, map_dvd f⟩ theorem map_dvd_iff_dvd_symm (f : F) {a : α} {b : β} : f a ∣ b ↔ a ∣ (MulEquivClass.toMulEquiv f).symm b := by obtain ⟨c, rfl⟩ : ∃ c, f c = b := EquivLike.surjective f b simp [map_dvd_iff] theorem MulEquiv.decompositionMonoid (f : F) [DecompositionMonoid β] : DecompositionMonoid α where primal a b c h := by rw [← map_dvd_iff f, map_mul] at h obtain ⟨a₁, a₂, h⟩ := DecompositionMonoid.primal _ h refine ⟨symm f a₁, symm f a₂, ?_⟩ simp_rw [← map_dvd_iff f, ← map_mul, eq_symm_apply] iterate 2 erw [(f : α ≃* β).apply_symm_apply] exact h /-- If `G` is a `LeftCancelSemiGroup`, left multiplication by `g` yields an equivalence between `G` and the set of elements of `G` divisible by `g`. -/ protected noncomputable def Equiv.dvd {G : Type*} [LeftCancelSemigroup G] (g : G) : G ≃ {a : G // g ∣ a} where toFun := fun a ↦ ⟨g * a, ⟨a, rfl⟩⟩ invFun := fun ⟨_, h⟩ ↦ h.choose left_inv := fun _ ↦ by simp right_inv := by rintro ⟨_, ⟨_, rfl⟩⟩ simp @[simp] theorem Equiv.dvd_apply {G : Type*} [LeftCancelSemigroup G] (g a : G) : Equiv.dvd g a = g * a := rfl end Semigroup section DistribSemigroup variable [Add α] [Semigroup α] theorem dvd_add [LeftDistribClass α] {a b c : α} (h₁ : a ∣ b) (h₂ : a ∣ c) : a ∣ b + c := Dvd.elim h₁ fun d hd => Dvd.elim h₂ fun e he => Dvd.intro (d + e) (by simp [left_distrib, hd, he]) alias Dvd.dvd.add := dvd_add end DistribSemigroup section Semiring variable [Semiring α] {a b c : α} {m n : ℕ} lemma min_pow_dvd_add (ha : c ^ m ∣ a) (hb : c ^ n ∣ b) : c ^ min m n ∣ a + b := ((pow_dvd_pow c (m.min_le_left n)).trans ha).add ((pow_dvd_pow c (m.min_le_right n)).trans hb) end Semiring section NonUnitalCommSemiring variable [NonUnitalCommSemiring α] theorem Dvd.dvd.linear_comb {d x y : α} (hdx : d ∣ x) (hdy : d ∣ y) (a b : α) : d ∣ a * x + b * y := dvd_add (hdx.mul_left a) (hdy.mul_left b) end NonUnitalCommSemiring section Semigroup variable [Semigroup α] [HasDistribNeg α] {a b : α} /-- An element `a` of a semigroup with a distributive negation divides the negation of an element `b` iff `a` divides `b`. -/ @[simp] theorem dvd_neg : a ∣ -b ↔ a ∣ b := (Equiv.neg _).exists_congr_left.trans <| by simp only [Equiv.neg_symm, Equiv.neg_apply, mul_neg, neg_inj, Dvd.dvd] /-- The negation of an element `a` of a semigroup with a distributive negation divides another element `b` iff `a` divides `b`. -/ @[simp] theorem neg_dvd : -a ∣ b ↔ a ∣ b := (Equiv.neg _).exists_congr_left.trans <| by simp only [Equiv.neg_symm, Equiv.neg_apply, mul_neg, neg_mul, neg_neg, Dvd.dvd] alias ⟨Dvd.dvd.of_neg_left, Dvd.dvd.neg_left⟩ := neg_dvd alias ⟨Dvd.dvd.of_neg_right, Dvd.dvd.neg_right⟩ := dvd_neg end Semigroup section NonUnitalRing variable [NonUnitalRing α] {a b c : α} theorem dvd_sub (h₁ : a ∣ b) (h₂ : a ∣ c) : a ∣ b - c := by simpa only [← sub_eq_add_neg] using h₁.add h₂.neg_right alias Dvd.dvd.sub := dvd_sub /-- If an element `a` divides another element `c` in a ring, `a` divides the sum of another element `b` with `c` iff `a` divides `b`. -/ theorem dvd_add_left (h : a ∣ c) : a ∣ b + c ↔ a ∣ b := ⟨fun H => by simpa only [add_sub_cancel_right] using dvd_sub H h, fun h₂ => dvd_add h₂ h⟩ /-- If an element `a` divides another element `b` in a ring, `a` divides the sum of `b` and another element `c` iff `a` divides `c`. -/ theorem dvd_add_right (h : a ∣ b) : a ∣ b + c ↔ a ∣ c := by rw [add_comm]; exact dvd_add_left h /-- If an element `a` divides another element `c` in a ring, `a` divides the difference of another element `b` with `c` iff `a` divides `b`. -/ theorem dvd_sub_left (h : a ∣ c) : a ∣ b - c ↔ a ∣ b := by simpa only [← sub_eq_add_neg] using dvd_add_left (dvd_neg.2 h) /-- If an element `a` divides another element `b` in a ring, `a` divides the difference of `b` and another element `c` iff `a` divides `c`. -/ theorem dvd_sub_right (h : a ∣ b) : a ∣ b - c ↔ a ∣ c := by rw [sub_eq_add_neg, dvd_add_right h, dvd_neg] theorem dvd_iff_dvd_of_dvd_sub (h : a ∣ b - c) : a ∣ b ↔ a ∣ c := by rw [← sub_add_cancel b c, dvd_add_right h] theorem dvd_sub_comm : a ∣ b - c ↔ a ∣ c - b := by rw [← dvd_neg, neg_sub] end NonUnitalRing section Ring variable [Ring α] {a b : α} /-- An element a divides the sum a + b if and only if a divides b. -/ @[simp] theorem dvd_add_self_left {a b : α} : a ∣ a + b ↔ a ∣ b := dvd_add_right (dvd_refl a) /-- An element a divides the sum b + a if and only if a divides b. -/ @[simp] theorem dvd_add_self_right {a b : α} : a ∣ b + a ↔ a ∣ b := dvd_add_left (dvd_refl a) /-- An element `a` divides the difference `a - b` if and only if `a` divides `b`. -/ @[simp] theorem dvd_sub_self_left : a ∣ a - b ↔ a ∣ b := dvd_sub_right dvd_rfl /-- An element `a` divides the difference `b - a` if and only if `a` divides `b`. -/ @[simp] theorem dvd_sub_self_right : a ∣ b - a ↔ a ∣ b := dvd_sub_left dvd_rfl end Ring section NonUnitalCommRing variable [NonUnitalCommRing α] theorem dvd_mul_sub_mul {k a b x y : α} (hab : k ∣ a - b) (hxy : k ∣ x - y) : k ∣ a * x - b * y := by convert dvd_add (hxy.mul_left a) (hab.mul_right y) using 1 rw [mul_sub_left_distrib, mul_sub_right_distrib] simp only [sub_eq_add_neg, add_assoc, neg_add_cancel_left] end NonUnitalCommRing
.lake/packages/mathlib/Mathlib/Algebra/Ring/Int/Defs.lean
import Mathlib.Algebra.CharZero.Defs import Mathlib.Algebra.Ring.Defs import Mathlib.Algebra.Group.Int.Defs import Mathlib.Data.Int.Basic import Mathlib.Data.Int.Cast.Basic import Mathlib.Algebra.Ring.GrindInstances /-! # The integers are a ring This file contains the commutative ring instance on `ℤ`. See note [foundational algebra order theory]. -/ assert_not_exists DenselyOrdered Set.Subsingleton namespace Int instance instCommRing : CommRing ℤ where __ := instAddCommGroup __ := instCommSemigroup zero_mul := Int.zero_mul mul_zero := Int.mul_zero left_distrib := Int.mul_add right_distrib := Int.add_mul mul_one := Int.mul_one one_mul := Int.one_mul npow n x := x ^ n npow_zero _ := rfl npow_succ _ _ := rfl natCast := (·) natCast_zero := rfl natCast_succ _ := rfl intCast := (·) intCast_ofNat _ := rfl intCast_negSucc _ := rfl instance instCancelCommMonoidWithZero : CancelCommMonoidWithZero ℤ where mul_left_cancel_of_ne_zero ha _ _ := (mul_eq_mul_left_iff ha).1 instance instIsDomain : IsDomain ℤ where instance instCharZero : CharZero ℤ where cast_injective _ _ := ofNat.inj instance instMulDivCancelClass : MulDivCancelClass ℤ where mul_div_cancel _ _ := mul_ediv_cancel _ @[simp, norm_cast] lemma cast_mul {α : Type*} [NonAssocRing α] : ∀ m n, ((m * n : ℤ) : α) = m * n := fun m => by obtain ⟨m, rfl | rfl⟩ := Int.eq_nat_or_neg m · induction m with | zero => simp | succ m ih => simp_all [add_mul] · induction m with | zero => simp | succ m ih => simp_all [add_mul] /-- Note this holds in marginally more generality than `Int.cast_mul` -/ lemma cast_mul_eq_zsmul_cast {α : Type*} [AddGroupWithOne α] : ∀ m n : ℤ, ↑(m * n) = m • (n : α) := fun m ↦ Int.induction_on m (by simp) (fun _ ih ↦ by simp [add_mul, add_zsmul, ih]) fun _ ih ↦ by simp only [sub_mul, one_mul, cast_sub, ih, sub_zsmul, one_zsmul, ← sub_eq_add_neg, forall_const] @[simp, norm_cast] lemma cast_pow {R : Type*} [Ring R] (n : ℤ) (m : ℕ) : ↑(n ^ m) = (n ^ m : R) := by induction m <;> simp [_root_.pow_succ, *] /-! ### Extra instances to short-circuit type class resolution These also prevent non-computable instances like `Int.normedCommRing` being used to construct these instances non-computably. -/ set_option linter.style.commandStart false instance instCommSemiring : CommSemiring ℤ := inferInstance instance instSemiring : Semiring ℤ := inferInstance instance instRing : Ring ℤ := inferInstance instance instDistrib : Distrib ℤ := inferInstance set_option linter.style.commandStart true end Int
.lake/packages/mathlib/Mathlib/Algebra/Ring/Int/Parity.lean
import Mathlib.Algebra.Ring.Parity import Mathlib.Algebra.Ring.Int.Defs import Mathlib.Algebra.Group.Int.Even /-! # Basic parity lemmas for the ring `ℤ` See note [foundational algebra order theory]. -/ assert_not_exists DenselyOrdered Set.Subsingleton namespace Int /-! #### Parity -/ variable {m n : ℤ} @[grind =] lemma odd_iff : Odd n ↔ n % 2 = 1 where mp := fun ⟨m, hm⟩ ↦ by simp [hm, add_emod] mpr h := ⟨n / 2, by grind⟩ lemma not_odd_iff : ¬Odd n ↔ n % 2 = 0 := by grind @[simp] lemma not_odd_zero : ¬Odd (0 : ℤ) := by grind @[simp, grind =] lemma not_odd_iff_even : ¬Odd n ↔ Even n := by grind @[simp] lemma not_even_iff_odd : ¬Even n ↔ Odd n := by grind lemma even_or_odd (n : ℤ) : Even n ∨ Odd n := by grind lemma even_or_odd' (n : ℤ) : ∃ k, n = 2 * k ∨ n = 2 * k + 1 := by simpa only [two_mul, exists_or, Odd, Even] using even_or_odd n lemma even_xor'_odd (n : ℤ) : Xor' (Even n) (Odd n) := by grind lemma even_xor'_odd' (n : ℤ) : ∃ k, Xor' (n = 2 * k) (n = 2 * k + 1) := by rcases even_or_odd n with (⟨k, rfl⟩ | ⟨k, rfl⟩) <;> · use k grind instance : DecidablePred (Odd : ℤ → Prop) := fun _ => decidable_of_iff _ not_even_iff_odd lemma even_add' : Even (m + n) ↔ (Odd m ↔ Odd n) := by grind lemma not_even_two_mul_add_one (n : ℤ) : ¬ Even (2 * n + 1) := by grind lemma even_sub' : Even (m - n) ↔ (Odd m ↔ Odd n) := by grind lemma odd_mul : Odd (m * n) ↔ Odd m ∧ Odd n := by simp [← not_even_iff_odd, not_or, parity_simps] lemma Odd.of_mul_left (h : Odd (m * n)) : Odd m := (odd_mul.mp h).1 lemma Odd.of_mul_right (h : Odd (m * n)) : Odd n := (odd_mul.mp h).2 @[parity_simps] lemma odd_pow {n : ℕ} : Odd (m ^ n) ↔ Odd m ∨ n = 0 := by grind lemma odd_pow' {n : ℕ} (h : n ≠ 0) : Odd (m ^ n) ↔ Odd m := by grind @[parity_simps] lemma odd_add : Odd (m + n) ↔ (Odd m ↔ Even n) := by grind lemma odd_add' : Odd (m + n) ↔ (Odd n ↔ Even m) := by grind lemma ne_of_odd_add (h : Odd (m + n)) : m ≠ n := by grind @[parity_simps] lemma odd_sub : Odd (m - n) ↔ (Odd m ↔ Even n) := by grind lemma odd_sub' : Odd (m - n) ↔ (Odd n ↔ Even m) := by grind lemma even_mul_succ_self (n : ℤ) : Even (n * (n + 1)) := by grind lemma even_mul_pred_self (n : ℤ) : Even (n * (n - 1)) := by grind @[simp, norm_cast] lemma odd_coe_nat (n : ℕ) : Odd (n : ℤ) ↔ Odd n := by grind @[simp] lemma natAbs_even : Even n.natAbs ↔ Even n := by grind @[simp] lemma natAbs_odd : Odd n.natAbs ↔ Odd n := by grind protected alias ⟨_, _root_.Even.natAbs⟩ := natAbs_even protected alias ⟨_, _root_.Odd.natAbs⟩ := natAbs_odd lemma four_dvd_add_or_sub_of_odd {a b : ℤ} (ha : Odd a) (hb : Odd b) : 4 ∣ a + b ∨ 4 ∣ a - b := by grind lemma two_dvd_mul_add_one (k : ℤ) : 2 ∣ k * (k + 1) := even_iff_two_dvd.mp (even_mul_succ_self k) lemma two_mul_ediv_two_add_one_of_odd : Odd n → 2 * (n / 2) + 1 = n := by grind lemma ediv_two_mul_two_add_one_of_odd : Odd n → n / 2 * 2 + 1 = n := by grind lemma add_one_ediv_two_mul_two_of_odd : Odd n → 1 + n / 2 * 2 = n := by grind lemma two_mul_ediv_two_of_odd (h : Odd n) : 2 * (n / 2) = n - 1 := by grind @[simp, grind =] theorem even_sign_iff {z : ℤ} : Even z.sign ↔ z = 0 := by induction z using wlog_sign with | inv => simp | w n => cases n · simp · norm_cast simp @[simp] theorem odd_sign_iff {z : ℤ} : Odd z.sign ↔ z ≠ 0 := by grind @[norm_cast, simp] theorem isSquare_natCast_iff {n : ℕ} : IsSquare (n : ℤ) ↔ IsSquare n := by constructor <;> rintro ⟨x, h⟩ · exact ⟨x.natAbs, (natAbs_mul_natAbs_eq h.symm).symm⟩ · exact ⟨x, mod_cast h⟩ @[simp] theorem isSquare_ofNat_iff {n : ℕ} : IsSquare (ofNat(n) : ℤ) ↔ IsSquare (ofNat(n) : ℕ) := isSquare_natCast_iff end Int
.lake/packages/mathlib/Mathlib/Algebra/Ring/Int/Units.lean
import Mathlib.Algebra.Ring.Int.Defs import Mathlib.Algebra.Ring.Units import Mathlib.Algebra.Group.Int.Units /-! # Basic lemmas for `ℤˣ`. This file contains lemmas on the units of `ℤ`. ## Main results * `Int.units_eq_one_or`: the invertible integers are 1 and -1. See note [foundational algebra order theory]. -/ assert_not_exists DenselyOrdered Set.Subsingleton namespace Int /-! #### Units -/ lemma units_eq_one_or (u : ℤˣ) : u = 1 ∨ u = -1 := by simpa only [Units.ext_iff] using isUnit_eq_one_or u.isUnit lemma units_ne_iff_eq_neg {u v : ℤˣ} : u ≠ v ↔ u = -v := by simpa only [Ne, Units.ext_iff] using isUnit_ne_iff_eq_neg u.isUnit v.isUnit end Int
.lake/packages/mathlib/Mathlib/Algebra/Ring/Action/Invariant.lean
import Mathlib.GroupTheory.GroupAction.Hom import Mathlib.Algebra.Ring.Subring.Defs /-! # Subrings invariant under an action If a monoid acts on a ring via a `MulSemiringAction`, then `IsInvariantSubring` is a predicate on subrings asserting that the subring is fixed elementwise by the action. -/ assert_not_exists RelIso section Ring variable (M R : Type*) [Monoid M] [Ring R] [MulSemiringAction M R] variable (S : Subring R) open MulAction variable {R} /-- A typeclass for subrings invariant under a `MulSemiringAction`. -/ class IsInvariantSubring : Prop where smul_mem : ∀ (m : M) {x : R}, x ∈ S → m • x ∈ S instance IsInvariantSubring.toMulSemiringAction [IsInvariantSubring M S] : MulSemiringAction M S where smul m x := ⟨m • ↑x, IsInvariantSubring.smul_mem m x.2⟩ one_smul s := Subtype.eq <| one_smul M (s : R) mul_smul m₁ m₂ s := Subtype.eq <| mul_smul m₁ m₂ (s : R) smul_add m s₁ s₂ := Subtype.eq <| smul_add m (s₁ : R) (s₂ : R) smul_zero m := Subtype.eq <| smul_zero m smul_one m := Subtype.eq <| smul_one m smul_mul m s₁ s₂ := Subtype.eq <| smul_mul' m (s₁ : R) (s₂ : R) end Ring section variable (M : Type*) [Monoid M] variable {R' : Type*} [Ring R'] [MulSemiringAction M R'] variable (U : Subring R') [IsInvariantSubring M U] /-- The canonical inclusion from an invariant subring. -/ def IsInvariantSubring.subtypeHom : U →+*[M] R' := { U.subtype with map_smul' := fun _ _ ↦ rfl } @[simp] theorem IsInvariantSubring.coe_subtypeHom : (IsInvariantSubring.subtypeHom M U : U → R') = Subtype.val := rfl @[simp] theorem IsInvariantSubring.coe_subtypeHom' : ((IsInvariantSubring.subtypeHom M U) : U →+* R') = U.subtype := rfl end
.lake/packages/mathlib/Mathlib/Algebra/Ring/Action/Rat.lean
import Mathlib.Algebra.Field.Defs import Mathlib.Algebra.GroupWithZero.Action.Defs /-! # Actions by nonnegative rational numbers -/ assert_not_exists IsOrderedMonoid variable {R : Type*} /-! ### Scalar multiplication -/ namespace NNRat variable [DivisionSemiring R] instance (priority := 100) instDistribSMul : DistribSMul ℚ≥0 R where smul_zero a := by rw [smul_def, mul_zero] smul_add a x y := by rw [smul_def, smul_def, smul_def, mul_add] instance instIsScalarTowerRight : IsScalarTower ℚ≥0 R R where smul_assoc a x y := by simp only [smul_def, smul_eq_mul, mul_assoc] end NNRat namespace Rat variable [DivisionRing R] instance (priority := 100) instDistribSMul : DistribSMul ℚ R where smul_zero a := by rw [smul_def, mul_zero] smul_add a x y := by rw [smul_def, smul_def, smul_def, mul_add] instance instIsScalarTowerRight : IsScalarTower ℚ R R where smul_assoc a x y := by simp only [smul_def, smul_eq_mul, mul_assoc] end Rat
.lake/packages/mathlib/Mathlib/Algebra/Ring/Action/ConjAct.lean
import Mathlib.Algebra.Ring.Action.Basic import Mathlib.GroupTheory.GroupAction.ConjAct /-! # Conjugation action of a ring on itself -/ assert_not_exists Field namespace ConjAct variable {R : Type*} [Semiring R] instance unitsMulSemiringAction : MulSemiringAction (ConjAct Rˣ) R := { ConjAct.unitsMulDistribMulAction with smul_zero := by simp [units_smul_def] smul_add := by simp [units_smul_def, mul_add, add_mul] } end ConjAct
.lake/packages/mathlib/Mathlib/Algebra/Ring/Action/Group.lean
import Mathlib.Algebra.GroupWithZero.Action.Basic import Mathlib.Algebra.Ring.Action.Basic import Mathlib.Algebra.Ring.Equiv /-! # If a group acts multiplicatively on a semiring, each group element acts by a ring automorphism. This result is split out from `Mathlib/Algebra/Ring/Action/Basic.lean` to avoid needing the import of `Mathlib/Algebra/GroupWithZero/Action/Basic.lean`. -/ section Semiring variable (G : Type*) [Group G] variable (R : Type*) [Semiring R] /-- Each element of the group defines a semiring isomorphism. -/ @[simps!] def MulSemiringAction.toRingEquiv [MulSemiringAction G R] (x : G) : R ≃+* R := { DistribMulAction.toAddEquiv R x, MulSemiringAction.toRingHom G R x with } end Semiring
.lake/packages/mathlib/Mathlib/Algebra/Ring/Action/Basic.lean
import Mathlib.Algebra.Group.Action.Basic import Mathlib.Algebra.GroupWithZero.Action.End import Mathlib.Algebra.Ring.Hom.Defs /-! # Group action on rings This file defines the typeclass of monoid acting on semirings `MulSemiringAction M R`. An example of a `MulSemiringAction` is the action of the Galois group `Gal(L/K)` on the big field `L`. Note that `Algebra` does not in general satisfy the axioms of `MulSemiringAction`. ## Implementation notes There is no separate typeclass for group acting on rings, group acting on fields, etc. They are all grouped under `MulSemiringAction`. ## Note The corresponding typeclass of subrings invariant under such an action, `IsInvariantSubring`, is defined in `Mathlib/Algebra/Ring/Action/Invariant.lean`. ## Tags group action -/ assert_not_exists Equiv.Perm.equivUnitsEnd Prod.fst_mul universe u v /-- Typeclass for multiplicative actions by monoids on semirings. This combines `DistribMulAction` with `MulDistribMulAction`: it expresses the interplay between the action and both addition and multiplication on the target. Two key axioms are `g • (x + y) = (g • x) + (g • y)` and `g • (x * y) = (g • x) * (g • y)`. A typical use case is the action of a Galois group $Gal(L/K)$ on the field `L`. -/ class MulSemiringAction (M : Type u) (R : Type v) [Monoid M] [Semiring R] extends DistribMulAction M R where /-- Multiplying `1` by a scalar gives `1` -/ smul_one : ∀ g : M, (g • (1 : R) : R) = 1 /-- Scalar multiplication distributes across multiplication -/ smul_mul : ∀ (g : M) (x y : R), g • (x * y) = g • x * g • y section Semiring variable (M N : Type*) [Monoid M] [Monoid N] variable (R : Type v) [Semiring R] -- note we could not use `extends` since these typeclasses are made with `old_structure_cmd` instance (priority := 100) MulSemiringAction.toMulDistribMulAction (M R) {_ : Monoid M} {_ : Semiring R} [h : MulSemiringAction M R] : MulDistribMulAction M R := { h with } /-- Each element of the monoid defines a semiring homomorphism. -/ @[simps!] def MulSemiringAction.toRingHom [MulSemiringAction M R] (x : M) : R →+* R := { MulDistribMulAction.toMonoidHom R x, DistribMulAction.toAddMonoidHom R x with } theorem toRingHom_injective [MulSemiringAction M R] [FaithfulSMul M R] : Function.Injective (MulSemiringAction.toRingHom M R) := fun _ _ h => eq_of_smul_eq_smul fun r => RingHom.ext_iff.1 h r /-- The tautological action by `R →+* R` on `R`. This generalizes `Function.End.applyMulAction`. -/ instance RingHom.applyMulSemiringAction : MulSemiringAction (R →+* R) R where smul := (· <| ·) smul_one := map_one smul_mul := map_mul smul_zero := RingHom.map_zero smul_add := RingHom.map_add one_smul _ := rfl mul_smul _ _ _ := rfl @[simp] protected theorem RingHom.smul_def (f : R →+* R) (a : R) : f • a = f a := rfl /-- `RingHom.applyMulSemiringAction` is faithful. -/ instance RingHom.applyFaithfulSMul : FaithfulSMul (R →+* R) R := ⟨fun {_ _} h => RingHom.ext h⟩ section variable {M N} /-- Compose a `MulSemiringAction` with a `MonoidHom`, with action `f r' • m`. See note [reducible non-instances]. -/ abbrev MulSemiringAction.compHom (f : N →* M) [MulSemiringAction M R] : MulSemiringAction N R := { DistribMulAction.compHom R f, MulDistribMulAction.compHom R f with } end section SimpLemmas attribute [simp] smul_one smul_mul' smul_zero smul_add end SimpLemmas end Semiring
.lake/packages/mathlib/Mathlib/Algebra/Ring/Action/End.lean
import Mathlib.Algebra.Ring.Action.Group import Mathlib.Algebra.Ring.Aut /-! # Ring automorphisms This file defines the automorphism group structure on `RingAut R := RingEquiv R R`. ## Implementation notes The definition of multiplication in the automorphism group agrees with function composition, multiplication in `Equiv.Perm`, and multiplication in `CategoryTheory.End`, but not with `CategoryTheory.comp`. This file is kept separate from `Mathlib/Algebra/Ring/Equiv.lean` so that `Mathlib/GroupTheory/Perm.lean` is free to use equivalences (and other files that use them) before the group structure is defined. ## Tags ring aut -/ namespace RingAut variable {G R : Type*} [Group G] [Semiring R] /-- The tautological action by the group of automorphism of a ring `R` on `R`. -/ instance applyMulSemiringAction : MulSemiringAction (RingAut R) R where smul := (· <| ·) smul_zero := RingEquiv.map_zero smul_add := RingEquiv.map_add smul_one := RingEquiv.map_one smul_mul := RingEquiv.map_mul one_smul _ := rfl mul_smul _ _ _ := rfl @[simp] protected theorem smul_def (f : RingAut R) (r : R) : f • r = f r := rfl instance apply_faithfulSMul : FaithfulSMul (RingAut R) R := ⟨RingEquiv.ext⟩ variable (G R) /-- Each element of the group defines a ring automorphism. This is a stronger version of `DistribMulAction.toAddAut` and `MulDistribMulAction.toMulAut`. -/ @[simps] def _root_.MulSemiringAction.toRingAut [MulSemiringAction G R] : G →* RingAut R where toFun := MulSemiringAction.toRingEquiv G R map_mul' g h := RingEquiv.ext <| mul_smul g h map_one' := RingEquiv.ext <| one_smul _ end RingAut
.lake/packages/mathlib/Mathlib/Algebra/Ring/Action/Subobjects.lean
import Mathlib.Algebra.Group.Subgroup.Defs import Mathlib.Algebra.Group.Submonoid.DistribMulAction import Mathlib.Algebra.Ring.Action.Basic /-! # Instances of `MulSemiringAction` for subobjects These are defined in this file as `Semiring`s are not available yet where `Submonoid` and `Subgroup` are defined. Instances for `Subsemiring` and `Subring` are provided next to the other scalar actions instances for those subobjects. -/ assert_not_exists RelIso variable {M G R : Type*} variable [Monoid M] [Group G] [Semiring R] instance (priority := low) [MulSemiringAction M R] {S : Type*} [SetLike S M] (s : S) [SubmonoidClass S M] : MulSemiringAction s R := { inferInstanceAs (DistribMulAction s R), inferInstanceAs (MulDistribMulAction s R) with } /-- A stronger version of `Submonoid.distribMulAction`. -/ instance Submonoid.mulSemiringAction [MulSemiringAction M R] (H : Submonoid M) : MulSemiringAction H R := inferInstance /-- A stronger version of `Subgroup.distribMulAction`. -/ instance Subgroup.mulSemiringAction [MulSemiringAction G R] (H : Subgroup G) : MulSemiringAction H R := inferInstance
.lake/packages/mathlib/Mathlib/Algebra/Ring/Action/Submonoid.lean
import Mathlib.Algebra.GroupWithZero.Action.Defs import Mathlib.GroupTheory.GroupAction.Defs /-! # The subgroup of fixed points of an action -/ variable (M α : Type*) [Monoid M] section AddMonoid variable [AddMonoid α] [DistribMulAction M α] /-- The additive submonoid of elements fixed under the whole action. -/ def FixedPoints.addSubmonoid : AddSubmonoid α where carrier := MulAction.fixedPoints M α zero_mem' := smul_zero add_mem' ha hb _ := by rw [smul_add, ha, hb] @[simp] lemma FixedPoints.mem_addSubmonoid (a : α) : a ∈ addSubmonoid M α ↔ ∀ m : M, m • a = a := Iff.rfl end AddMonoid section AddGroup variable [AddGroup α] [DistribMulAction M α] /-- The additive subgroup of elements fixed under the whole action. -/ def FixedPoints.addSubgroup : AddSubgroup α where __ := addSubmonoid M α neg_mem' ha _ := by rw [smul_neg, ha] /-- The notation for `FixedPoints.addSubgroup`, chosen to resemble `αᴹ`. -/ notation α "^+" M:51 => FixedPoints.addSubgroup M α @[simp] lemma FixedPoints.mem_addSubgroup (a : α) : a ∈ α^+M ↔ ∀ m : M, m • a = a := Iff.rfl @[simp] lemma FixedPoints.addSubgroup_toAddSubmonoid : (α^+M).toAddSubmonoid = addSubmonoid M α := rfl end AddGroup
.lake/packages/mathlib/Mathlib/Algebra/Ring/Action/Field.lean
import Mathlib.Algebra.Ring.Action.Basic import Mathlib.Algebra.Field.Defs import Mathlib.Algebra.GroupWithZero.Units.Lemmas /-! # Group action on fields -/ variable {M} [Monoid M] {F} [DivisionRing F] /-- Note that `smul_inv'` refers to the group case, and `smul_inv` has an additional inverse on `x`. -/ @[simp] theorem smul_inv'' [MulSemiringAction M F] (x : M) (m : F) : x • m⁻¹ = (x • m)⁻¹ := map_inv₀ (MulSemiringAction.toRingHom M F x) _
.lake/packages/mathlib/Mathlib/Algebra/Ring/Action/Pointwise/Finset.lean
import Mathlib.Algebra.Group.Pointwise.Finset.Scalar import Mathlib.Algebra.Module.Defs import Mathlib.Algebra.Group.Pointwise.Finset.Basic /-! # Pointwise actions on sets in a ring This file proves properties of pointwise actions on sets in a ring. ## Tags set multiplication, set addition, pointwise addition, pointwise multiplication, pointwise subtraction -/ open scoped Pointwise variable {R G : Type*} namespace Finset variable [Ring R] [AddCommGroup G] [Module R G] [DecidableEq G] {s : Finset R} {t : Finset G} {a : R} @[simp] lemma neg_smul_finset : -a • t = -(a • t) := by simp only [← image_smul, ← image_neg_eq_neg, image_image, neg_smul, Function.comp_def] @[simp] protected lemma neg_smul [DecidableEq R] : -s • t = -(s • t) := by simp_rw [← image_neg_eq_neg] exact image₂_image_left_comm neg_smul end Finset
.lake/packages/mathlib/Mathlib/Algebra/Ring/Action/Pointwise/Set.lean
import Mathlib.Algebra.Group.Pointwise.Set.Scalar import Mathlib.Algebra.Module.Defs import Mathlib.Algebra.Group.Pointwise.Set.Basic /-! # Pointwise operations of sets in a ring This file proves properties of pointwise operations of sets in a ring. ## Tags set multiplication, set addition, pointwise addition, pointwise multiplication, pointwise subtraction -/ assert_not_exists IsOrderedMonoid Field open Function open scoped Pointwise variable {α β : Type*} namespace Set section Monoid variable [Monoid α] [AddGroup β] [DistribMulAction α β] (a : α) (s : Set α) (t : Set β) @[simp] lemma smul_set_neg : a • -t = -(a • t) := by simp_rw [← image_smul, ← image_neg_eq_neg, image_image, smul_neg] @[simp] protected lemma smul_neg : s • -t = -(s • t) := by simp_rw [← image_neg_eq_neg] exact image_image2_right_comm smul_neg end Monoid section Semiring variable [Semiring α] [AddCommMonoid β] [Module α β] lemma add_smul_subset (a b : α) (s : Set β) : (a + b) • s ⊆ a • s + b • s := by rintro _ ⟨x, hx, rfl⟩ simpa only [add_smul] using add_mem_add (smul_mem_smul_set hx) (smul_mem_smul_set hx) end Semiring section Ring variable [Ring α] [AddCommGroup β] [Module α β] (a : α) (s : Set α) (t : Set β) @[simp] lemma neg_smul_set : -a • t = -(a • t) := by simp_rw [← image_smul, ← image_neg_eq_neg, image_image, neg_smul] @[simp] protected lemma neg_smul : -s • t = -(s • t) := by simp_rw [← image_neg_eq_neg] exact image2_image_left_comm neg_smul end Ring end Set
.lake/packages/mathlib/Mathlib/Algebra/Ring/Subring/IntPolynomial.lean
import Mathlib.Algebra.Polynomial.AlgebraMap /-! # Polynomials over subrings. Given a field `K` with a subring `R`, in this file we construct a map from polynomials in `K[X]` with coefficients in `R` to `R[X]`. We provide several lemmas to deal with coefficients, degree, and evaluation of `Polynomial.int`. This is useful when dealing with integral elements in an extension of fields. ## Main Definitions * `Polynomial.int` : given a polynomial `P` in `K[X]` whose coefficients all belong to a subring `R` of the field `K`, `P.int R` is the corresponding polynomial in `R[X]`. -/ variable {K : Type*} [Field K] (R : Subring K) open scoped Polynomial /-- Given a polynomial in `K[X]` such that all coefficients belong to the subring `R`, `Polynomial.int` is the corresponding polynomial in `R[X]`. -/ def Polynomial.int (P : K[X]) (hP : ∀ n : ℕ, P.coeff n ∈ R) : R[X] where toFinsupp := { support := P.support toFun := fun n => ⟨P.coeff n, hP n⟩ mem_support_toFun := fun n => by rw [ne_eq, ← Subring.coe_eq_zero_iff, mem_support_iff] } namespace Polynomial variable (P : K[X]) (hP : ∀ n : ℕ, P.coeff n ∈ R) @[simp] theorem int_coeff_eq (n : ℕ) : ↑((P.int R hP).coeff n) = P.coeff n := rfl @[simp] theorem int_leadingCoeff_eq : ↑(P.int R hP).leadingCoeff = P.leadingCoeff := rfl @[simp] theorem int_monic_iff : (P.int R hP).Monic ↔ P.Monic := by rw [Monic, Monic, ← int_leadingCoeff_eq, OneMemClass.coe_eq_one] @[simp] theorem int_natDegree : (P.int R hP).natDegree = P.natDegree := rfl variable {L : Type*} [Field L] [Algebra K L] @[simp] theorem int_eval₂_eq (x : L) : eval₂ (algebraMap R L) x (P.int R hP) = aeval x P := by rw [aeval_eq_sum_range, eval₂_eq_sum_range] exact Finset.sum_congr rfl (fun n _ => by rw [Algebra.smul_def]; rfl) end Polynomial
.lake/packages/mathlib/Mathlib/Algebra/Ring/Subring/MulOpposite.lean
import Mathlib.Algebra.Ring.Subsemiring.MulOpposite import Mathlib.Algebra.Ring.Subring.Basic /-! # Subring of opposite rings For every ring `R`, we construct an equivalence between subrings of `R` and that of `Rᵐᵒᵖ`. -/ namespace Subring variable {ι : Sort*} {R : Type*} [Ring R] /-- Pull a subring back to an opposite subring along `MulOpposite.unop` -/ @[simps! coe toSubsemiring] protected def op (S : Subring R) : Subring Rᵐᵒᵖ where toSubsemiring := S.toSubsemiring.op neg_mem' {x} hx := neg_mem (show x.unop ∈ S from hx) attribute [norm_cast] coe_op @[simp] theorem mem_op {x : Rᵐᵒᵖ} {S : Subring R} : x ∈ S.op ↔ x.unop ∈ S := Iff.rfl /-- Pull an opposite subring back to a subring along `MulOpposite.op` -/ @[simps! coe toSubsemiring] protected def unop (S : Subring Rᵐᵒᵖ) : Subring R where toSubsemiring := S.toSubsemiring.unop neg_mem' {x} hx := neg_mem (show MulOpposite.op x ∈ S from hx) attribute [norm_cast] coe_unop @[simp] theorem mem_unop {x : R} {S : Subring Rᵐᵒᵖ} : x ∈ S.unop ↔ MulOpposite.op x ∈ S := Iff.rfl @[simp] theorem unop_op (S : Subring R) : S.op.unop = S := rfl @[simp] theorem op_unop (S : Subring Rᵐᵒᵖ) : S.unop.op = S := rfl /-! ### Lattice results -/ theorem op_le_iff {S₁ : Subring R} {S₂ : Subring Rᵐᵒᵖ} : S₁.op ≤ S₂ ↔ S₁ ≤ S₂.unop := MulOpposite.op_surjective.forall theorem le_op_iff {S₁ : Subring Rᵐᵒᵖ} {S₂ : Subring R} : S₁ ≤ S₂.op ↔ S₁.unop ≤ S₂ := MulOpposite.op_surjective.forall @[simp] theorem op_le_op_iff {S₁ S₂ : Subring R} : S₁.op ≤ S₂.op ↔ S₁ ≤ S₂ := MulOpposite.op_surjective.forall @[simp] theorem unop_le_unop_iff {S₁ S₂ : Subring Rᵐᵒᵖ} : S₁.unop ≤ S₂.unop ↔ S₁ ≤ S₂ := MulOpposite.unop_surjective.forall /-- A subring `S` of `R` determines a subring `S.op` of the opposite ring `Rᵐᵒᵖ`. -/ @[simps] def opEquiv : Subring R ≃o Subring Rᵐᵒᵖ where toFun := Subring.op invFun := Subring.unop left_inv := unop_op right_inv := op_unop map_rel_iff' := op_le_op_iff theorem op_injective : (@Subring.op R _).Injective := opEquiv.injective theorem unop_injective : (@Subring.unop R _).Injective := opEquiv.symm.injective @[simp] theorem op_inj {S T : Subring R} : S.op = T.op ↔ S = T := opEquiv.eq_iff_eq @[simp] theorem unop_inj {S T : Subring Rᵐᵒᵖ} : S.unop = T.unop ↔ S = T := opEquiv.symm.eq_iff_eq @[simp] theorem op_bot : (⊥ : Subring R).op = ⊥ := opEquiv.map_bot @[simp] theorem op_eq_bot {S : Subring R} : S.op = ⊥ ↔ S = ⊥ := op_injective.eq_iff' op_bot @[simp] theorem unop_bot : (⊥ : Subring Rᵐᵒᵖ).unop = ⊥ := opEquiv.symm.map_bot @[simp] theorem unop_eq_bot {S : Subring Rᵐᵒᵖ} : S.unop = ⊥ ↔ S = ⊥ := unop_injective.eq_iff' unop_bot @[simp] theorem op_top : (⊤ : Subring R).op = ⊤ := rfl @[simp] theorem op_eq_top {S : Subring R} : S.op = ⊤ ↔ S = ⊤ := op_injective.eq_iff' op_top @[simp] theorem unop_top : (⊤ : Subring Rᵐᵒᵖ).unop = ⊤ := rfl @[simp] theorem unop_eq_top {S : Subring Rᵐᵒᵖ} : S.unop = ⊤ ↔ S = ⊤ := unop_injective.eq_iff' unop_top theorem op_sup (S₁ S₂ : Subring R) : (S₁ ⊔ S₂).op = S₁.op ⊔ S₂.op := opEquiv.map_sup _ _ theorem unop_sup (S₁ S₂ : Subring Rᵐᵒᵖ) : (S₁ ⊔ S₂).unop = S₁.unop ⊔ S₂.unop := opEquiv.symm.map_sup _ _ theorem op_inf (S₁ S₂ : Subring R) : (S₁ ⊓ S₂).op = S₁.op ⊓ S₂.op := rfl theorem unop_inf (S₁ S₂ : Subring Rᵐᵒᵖ) : (S₁ ⊓ S₂).unop = S₁.unop ⊓ S₂.unop := rfl theorem op_sSup (S : Set (Subring R)) : (sSup S).op = sSup (.unop ⁻¹' S) := opEquiv.map_sSup_eq_sSup_symm_preimage _ theorem unop_sSup (S : Set (Subring Rᵐᵒᵖ)) : (sSup S).unop = sSup (.op ⁻¹' S) := opEquiv.symm.map_sSup_eq_sSup_symm_preimage _ theorem op_sInf (S : Set (Subring R)) : (sInf S).op = sInf (.unop ⁻¹' S) := opEquiv.map_sInf_eq_sInf_symm_preimage _ theorem unop_sInf (S : Set (Subring Rᵐᵒᵖ)) : (sInf S).unop = sInf (.op ⁻¹' S) := opEquiv.symm.map_sInf_eq_sInf_symm_preimage _ theorem op_iSup (S : ι → Subring R) : (iSup S).op = ⨆ i, (S i).op := opEquiv.map_iSup _ theorem unop_iSup (S : ι → Subring Rᵐᵒᵖ) : (iSup S).unop = ⨆ i, (S i).unop := opEquiv.symm.map_iSup _ theorem op_iInf (S : ι → Subring R) : (iInf S).op = ⨅ i, (S i).op := opEquiv.map_iInf _ theorem unop_iInf (S : ι → Subring Rᵐᵒᵖ) : (iInf S).unop = ⨅ i, (S i).unop := opEquiv.symm.map_iInf _ theorem op_closure (s : Set R) : (closure s).op = closure (MulOpposite.unop ⁻¹' s) := by simp_rw [closure, op_sInf, Set.preimage_setOf_eq, coe_unop] congr with a exact MulOpposite.unop_surjective.forall theorem unop_closure (s : Set Rᵐᵒᵖ) : (closure s).unop = closure (MulOpposite.op ⁻¹' s) := by rw [← op_inj, op_unop, op_closure] simp_rw [Set.preimage_preimage, MulOpposite.op_unop, Set.preimage_id'] /-- Bijection between a subring `S` and its opposite. -/ @[simps!] def addEquivOp (S : Subring R) : S ≃+ S.op := S.toSubsemiring.addEquivOp /-- Bijection between a subring `S` and `MulOpposite` of its opposite. -/ @[simps!] def ringEquivOpMop (S : Subring R) : S ≃+* (S.op)ᵐᵒᵖ := S.toSubsemiring.ringEquivOpMop /-- Bijection between `MulOpposite` of a subring `S` and its opposite. -/ @[simps!] def mopRingEquivOp (S : Subring R) : Sᵐᵒᵖ ≃+* S.op := S.toSubsemiring.mopRingEquivOp end Subring
.lake/packages/mathlib/Mathlib/Algebra/Ring/Subring/Pointwise.lean
import Mathlib.Algebra.GroupWithZero.Subgroup import Mathlib.Algebra.Ring.Subring.Basic import Mathlib.Algebra.Ring.Subsemiring.Pointwise /-! # Pointwise instances on `Subring`s This file provides the action `Subring.pointwiseMulAction` which matches the action of `mulActionSet`. This actions is available in the `Pointwise` locale. ## Implementation notes This file is almost identical to the file `Mathlib/Algebra/Ring/Subsemiring/Pointwise.lean`. Where possible, try to keep them in sync. -/ open Set variable {M R : Type*} namespace Subring section Monoid variable [Monoid M] [Ring R] [MulSemiringAction M R] /-- The action on a subring corresponding to applying the action to every element. This is available as an instance in the `Pointwise` locale. -/ protected def pointwiseMulAction : MulAction M (Subring R) where smul a S := S.map (MulSemiringAction.toRingHom _ _ a) one_smul S := (congr_arg (fun f => S.map f) (RingHom.ext <| one_smul M)).trans S.map_id mul_smul _ _ S := (congr_arg (fun f => S.map f) (RingHom.ext <| mul_smul _ _)).trans (S.map_map _ _).symm scoped[Pointwise] attribute [instance] Subring.pointwiseMulAction open Pointwise theorem pointwise_smul_def {a : M} (S : Subring R) : a • S = S.map (MulSemiringAction.toRingHom _ _ a) := rfl @[simp, norm_cast] theorem coe_pointwise_smul (m : M) (S : Subring R) : ↑(m • S) = m • (S : Set R) := rfl @[simp] theorem pointwise_smul_toAddSubgroup (m : M) (S : Subring R) : (m • S).toAddSubgroup = m • S.toAddSubgroup := rfl @[simp] theorem pointwise_smul_toSubsemiring (m : M) (S : Subring R) : (m • S).toSubsemiring = m • S.toSubsemiring := rfl theorem smul_mem_pointwise_smul (m : M) (r : R) (S : Subring R) : r ∈ S → m • r ∈ m • S := (Set.smul_mem_smul_set : _ → _ ∈ m • (S : Set R)) instance : CovariantClass M (Subring R) HSMul.hSMul LE.le := ⟨fun _ _ => image_mono⟩ theorem mem_smul_pointwise_iff_exists (m : M) (r : R) (S : Subring R) : r ∈ m • S ↔ ∃ s : R, s ∈ S ∧ m • s = r := (Set.mem_smul_set : r ∈ m • (S : Set R) ↔ _) @[simp] theorem smul_bot (a : M) : a • (⊥ : Subring R) = ⊥ := map_bot _ theorem smul_sup (a : M) (S T : Subring R) : a • (S ⊔ T) = a • S ⊔ a • T := map_sup _ _ _ theorem smul_closure (a : M) (s : Set R) : a • closure s = closure (a • s) := RingHom.map_closure _ _ instance pointwise_central_scalar [MulSemiringAction Mᵐᵒᵖ R] [IsCentralScalar M R] : IsCentralScalar M (Subring R) := ⟨fun _ S => (congr_arg fun f => S.map f) <| RingHom.ext <| op_smul_eq_smul _⟩ end Monoid section Group variable [Group M] [Ring R] [MulSemiringAction M R] open Pointwise @[simp] theorem smul_mem_pointwise_smul_iff {a : M} {S : Subring R} {x : R} : a • x ∈ a • S ↔ x ∈ S := smul_mem_smul_set_iff theorem mem_pointwise_smul_iff_inv_smul_mem {a : M} {S : Subring R} {x : R} : x ∈ a • S ↔ a⁻¹ • x ∈ S := mem_smul_set_iff_inv_smul_mem theorem mem_inv_pointwise_smul_iff {a : M} {S : Subring R} {x : R} : x ∈ a⁻¹ • S ↔ a • x ∈ S := mem_inv_smul_set_iff @[simp] theorem pointwise_smul_le_pointwise_smul_iff {a : M} {S T : Subring R} : a • S ≤ a • T ↔ S ≤ T := smul_set_subset_smul_set_iff theorem pointwise_smul_subset_iff {a : M} {S T : Subring R} : a • S ≤ T ↔ S ≤ a⁻¹ • T := smul_set_subset_iff_subset_inv_smul_set theorem subset_pointwise_smul_iff {a : M} {S T : Subring R} : S ≤ a • T ↔ a⁻¹ • S ≤ T := subset_smul_set_iff /-! TODO: add `equivSMul` like we have for subgroup. -/ end Group section GroupWithZero variable [GroupWithZero M] [Ring R] [MulSemiringAction M R] open Pointwise @[simp] theorem smul_mem_pointwise_smul_iff₀ {a : M} (ha : a ≠ 0) (S : Subring R) (x : R) : a • x ∈ a • S ↔ x ∈ S := smul_mem_smul_set_iff₀ ha (S : Set R) x theorem mem_pointwise_smul_iff_inv_smul_mem₀ {a : M} (ha : a ≠ 0) (S : Subring R) (x : R) : x ∈ a • S ↔ a⁻¹ • x ∈ S := mem_smul_set_iff_inv_smul_mem₀ ha (S : Set R) x theorem mem_inv_pointwise_smul_iff₀ {a : M} (ha : a ≠ 0) (S : Subring R) (x : R) : x ∈ a⁻¹ • S ↔ a • x ∈ S := mem_inv_smul_set_iff₀ ha (S : Set R) x @[simp] theorem pointwise_smul_le_pointwise_smul_iff₀ {a : M} (ha : a ≠ 0) {S T : Subring R} : a • S ≤ a • T ↔ S ≤ T := smul_set_subset_smul_set_iff₀ ha theorem pointwise_smul_le_iff₀ {a : M} (ha : a ≠ 0) {S T : Subring R} : a • S ≤ T ↔ S ≤ a⁻¹ • T := smul_set_subset_iff₀ ha theorem le_pointwise_smul_iff₀ {a : M} (ha : a ≠ 0) {S T : Subring R} : S ≤ a • T ↔ a⁻¹ • S ≤ T := subset_smul_set_iff₀ ha end GroupWithZero end Subring
.lake/packages/mathlib/Mathlib/Algebra/Ring/Subring/Order.lean
import Mathlib.Algebra.Order.Hom.Ring import Mathlib.Algebra.Order.Ring.InjSurj import Mathlib.Algebra.Ring.Subring.Defs /-! # Subrings of ordered rings We study subrings of ordered rings and prove their basic properties. ## Main definitions and results * `Subring.orderedSubtype`: the inclusion `S → R` of a subring as an ordered ring homomorphism * various ordered instances: a subring of an `OrderedRing`, `OrderedCommRing`, `LinearOrderedRing`, `toLinearOrderedCommRing` is again an ordering ring -/ namespace Subring variable {R : Type*} /-- A subring of an ordered ring is an ordered ring. -/ instance toIsOrderedRing [Ring R] [PartialOrder R] [IsOrderedRing R] (s : Subring R) : IsOrderedRing s := Function.Injective.isOrderedRing Subtype.val rfl rfl (fun _ _ => rfl) (fun _ _ => rfl) .rfl /-- A subring of a strict ordered ring is a strict ordered ring. -/ instance toIsStrictOrderedRing [Ring R] [PartialOrder R] [IsStrictOrderedRing R] (s : Subring R) : IsStrictOrderedRing s := Function.Injective.isStrictOrderedRing Subtype.val rfl rfl (fun _ _ => rfl) (fun _ _ => rfl) .rfl .rfl /-- The inclusion `S → R` of a subring, as an ordered ring homomorphism. -/ def orderedSubtype {R : Type*} [Ring R] [PartialOrder R] (s : Subring R) : s →+*o R where __ := s.subtype monotone' := fun _ _ h ↦ h variable {R : Type*} [Ring R] [PartialOrder R] lemma orderedSubtype_coe (s : Subring R) : Subring.orderedSubtype s = Subring.subtype s := rfl end Subring
.lake/packages/mathlib/Mathlib/Algebra/Ring/Subring/Basic.lean
import Mathlib.Algebra.Field.Defs import Mathlib.Algebra.Group.Subgroup.Basic import Mathlib.Algebra.Ring.Subring.Defs import Mathlib.Algebra.Ring.Subsemiring.Basic import Mathlib.RingTheory.NonUnitalSubring.Defs import Mathlib.Data.Set.Finite.Basic /-! # Subrings We prove that subrings are a complete lattice, and that you can `map` (pushforward) and `comap` (pull back) them along ring homomorphisms. We define the `closure` construction from `Set R` to `Subring R`, sending a subset of `R` to the subring it generates, and prove that it is a Galois insertion. ## Main definitions Notation used here: `(R : Type u) [Ring R] (S : Type u) [Ring S] (f g : R →+* S)` `(A : Subring R) (B : Subring S) (s : Set R)` * `instance : CompleteLattice (Subring R)` : the complete lattice structure on the subrings. * `Subring.center` : the center of a ring `R`. * `Subring.closure` : subring closure of a set, i.e., the smallest subring that includes the set. * `Subring.gi` : `closure : Set M → Subring M` and coercion `(↑) : Subring M → et M` form a `GaloisInsertion`. * `comap f B : Subring A` : the preimage of a subring `B` along the ring homomorphism `f` * `map f A : Subring B` : the image of a subring `A` along the ring homomorphism `f`. * `prod A B : Subring (R × S)` : the product of subrings * `f.range : Subring B` : the range of the ring homomorphism `f`. * `eqLocus f g : Subring R` : given ring homomorphisms `f g : R →+* S`, the subring of `R` where `f x = g x` ## Implementation notes A subring is implemented as a subsemiring which is also an additive subgroup. The initial PR was as a submonoid which is also an additive subgroup. Lattice inclusion (e.g. `≤` and `⊓`) is used rather than set notation (`⊆` and `∩`), although `∈` is defined as membership of a subring's underlying set. ## Tags subring, subrings -/ assert_not_exists IsOrderedRing universe u v w variable {R : Type u} {S : Type v} {T : Type w} [Ring R] variable [Ring S] [Ring T] namespace Subring variable {s t : Subring R} @[gcongr, mono] theorem toSubsemiring_strictMono : StrictMono (toSubsemiring : Subring R → Subsemiring R) := fun _ _ => id @[gcongr, mono] theorem toSubsemiring_mono : Monotone (toSubsemiring : Subring R → Subsemiring R) := toSubsemiring_strictMono.monotone @[deprecated toSubsemiring_strictMono (since := "2025-10-20")] lemma toSubsemiring_lt_toSubsemiring (hst : s < t) : s.toSubsemiring < t.toSubsemiring := hst @[deprecated toSubsemiring_mono (since := "2025-10-20")] lemma toSubsemiring_le_toSubsemiring (hst : s ≤ t) : s.toSubsemiring ≤ t.toSubsemiring := hst @[gcongr, mono] theorem toAddSubgroup_strictMono : StrictMono (toAddSubgroup : Subring R → AddSubgroup R) := fun _ _ => id @[gcongr, mono] theorem toAddSubgroup_mono : Monotone (toAddSubgroup : Subring R → AddSubgroup R) := toAddSubgroup_strictMono.monotone @[deprecated toAddSubgroup_strictMono (since := "2025-10-20")] lemma toAddSubgroup_lt_toAddSubgroup (hst : s < t) : s.toAddSubgroup < t.toAddSubgroup := hst @[deprecated toAddSubgroup_mono (since := "2025-10-20")] lemma toAddSubgroup_le_toAddSubgroup (hst : s ≤ t) : s.toAddSubgroup ≤ t.toAddSubgroup := hst @[mono] theorem toSubmonoid_strictMono : StrictMono (fun s : Subring R => s.toSubmonoid) := fun _ _ => id @[mono] theorem toSubmonoid_mono : Monotone (fun s : Subring R => s.toSubmonoid) := toSubmonoid_strictMono.monotone end Subring namespace Subring variable (s : Subring R) /-- Product of a list of elements in a subring is in the subring. -/ protected theorem list_prod_mem {l : List R} : (∀ x ∈ l, x ∈ s) → l.prod ∈ s := list_prod_mem /-- Sum of a list of elements in a subring is in the subring. -/ protected theorem list_sum_mem {l : List R} : (∀ x ∈ l, x ∈ s) → l.sum ∈ s := list_sum_mem /-- Product of a multiset of elements in a subring of a `CommRing` is in the subring. -/ protected theorem multiset_prod_mem {R} [CommRing R] (s : Subring R) (m : Multiset R) : (∀ a ∈ m, a ∈ s) → m.prod ∈ s := multiset_prod_mem _ /-- Sum of a multiset of elements in a `Subring` of a `Ring` is in the `Subring`. -/ protected theorem multiset_sum_mem {R} [Ring R] (s : Subring R) (m : Multiset R) : (∀ a ∈ m, a ∈ s) → m.sum ∈ s := multiset_sum_mem _ /-- Product of elements of a subring of a `CommRing` indexed by a `Finset` is in the subring. -/ protected theorem prod_mem {R : Type*} [CommRing R] (s : Subring R) {ι : Type*} {t : Finset ι} {f : ι → R} (h : ∀ c ∈ t, f c ∈ s) : (∏ i ∈ t, f i) ∈ s := prod_mem h /-- Sum of elements in a `Subring` of a `Ring` indexed by a `Finset` is in the `Subring`. -/ protected theorem sum_mem {R : Type*} [Ring R] (s : Subring R) {ι : Type*} {t : Finset ι} {f : ι → R} (h : ∀ c ∈ t, f c ∈ s) : (∑ i ∈ t, f i) ∈ s := sum_mem h /-! ## top -/ /-- The subring `R` of the ring `R`. -/ instance : Top (Subring R) := ⟨{ (⊤ : Submonoid R), (⊤ : AddSubgroup R) with }⟩ @[simp] theorem mem_top (x : R) : x ∈ (⊤ : Subring R) := Set.mem_univ x @[simp, norm_cast] theorem coe_top : ((⊤ : Subring R) : Set R) = Set.univ := rfl /-- The ring equiv between the top element of `Subring R` and `R`. -/ @[simps!] def topEquiv : (⊤ : Subring R) ≃+* R := Subsemiring.topEquiv instance {R : Type*} [Ring R] [Fintype R] : Fintype (⊤ : Subring R) := inferInstanceAs (Fintype (⊤ : Set R)) theorem card_top (R) [Ring R] [Fintype R] : Fintype.card (⊤ : Subring R) = Fintype.card R := Fintype.card_congr topEquiv.toEquiv /-! ## comap -/ /-- The preimage of a subring along a ring homomorphism is a subring. -/ def comap {R : Type u} {S : Type v} [Ring R] [Ring S] (f : R →+* S) (s : Subring S) : Subring R := { s.toSubmonoid.comap (f : R →* S), s.toAddSubgroup.comap (f : R →+ S) with carrier := f ⁻¹' s.carrier } @[simp] theorem coe_comap (s : Subring S) (f : R →+* S) : (s.comap f : Set R) = f ⁻¹' s := rfl @[simp] theorem mem_comap {s : Subring S} {f : R →+* S} {x : R} : x ∈ s.comap f ↔ f x ∈ s := Iff.rfl theorem comap_comap (s : Subring T) (g : S →+* T) (f : R →+* S) : (s.comap g).comap f = s.comap (g.comp f) := rfl /-! ## map -/ /-- The image of a subring along a ring homomorphism is a subring. -/ def map {R : Type u} {S : Type v} [Ring R] [Ring S] (f : R →+* S) (s : Subring R) : Subring S := { s.toSubmonoid.map (f : R →* S), s.toAddSubgroup.map (f : R →+ S) with carrier := f '' s.carrier } @[simp] theorem coe_map (f : R →+* S) (s : Subring R) : (s.map f : Set S) = f '' s := rfl @[simp] theorem mem_map {f : R →+* S} {s : Subring R} {y : S} : y ∈ s.map f ↔ ∃ x ∈ s, f x = y := Iff.rfl @[simp] theorem map_id : s.map (RingHom.id R) = s := SetLike.coe_injective <| Set.image_id _ theorem map_map (g : S →+* T) (f : R →+* S) : (s.map f).map g = s.map (g.comp f) := SetLike.coe_injective <| Set.image_image _ _ _ theorem map_le_iff_le_comap {f : R →+* S} {s : Subring R} {t : Subring S} : s.map f ≤ t ↔ s ≤ t.comap f := Set.image_subset_iff theorem gc_map_comap (f : R →+* S) : GaloisConnection (map f) (comap f) := fun _ _ => map_le_iff_le_comap /-- A subring is isomorphic to its image under an injective function -/ noncomputable def equivMapOfInjective (f : R →+* S) (hf : Function.Injective f) : s ≃+* s.map f := { Equiv.Set.image f s hf with map_mul' := fun _ _ => Subtype.ext (f.map_mul _ _) map_add' := fun _ _ => Subtype.ext (f.map_add _ _) } @[simp] theorem coe_equivMapOfInjective_apply (f : R →+* S) (hf : Function.Injective f) (x : s) : (equivMapOfInjective s f hf x : S) = f x := rfl end Subring namespace RingHom variable (g : S →+* T) (f : R →+* S) /-! ## range -/ /-- The range of a ring homomorphism, as a subring of the target. See Note [range copy pattern]. -/ def range {R : Type u} {S : Type v} [Ring R] [Ring S] (f : R →+* S) : Subring S := ((⊤ : Subring R).map f).copy (Set.range f) Set.image_univ.symm @[simp] theorem coe_range : (f.range : Set S) = Set.range f := rfl @[simp] theorem mem_range {f : R →+* S} {y : S} : y ∈ f.range ↔ ∃ x, f x = y := Iff.rfl theorem range_eq_map (f : R →+* S) : f.range = Subring.map f ⊤ := by ext simp theorem mem_range_self (f : R →+* S) (x : R) : f x ∈ f.range := mem_range.mpr ⟨x, rfl⟩ theorem map_range : f.range.map g = (g.comp f).range := by simpa only [range_eq_map] using (⊤ : Subring R).map_map g f /-- The range of a ring homomorphism is a fintype, if the domain is a fintype. Note: this instance can form a diamond with `Subtype.fintype` in the presence of `Fintype S`. -/ instance fintypeRange [Fintype R] [DecidableEq S] (f : R →+* S) : Fintype (range f) := Set.fintypeRange f end RingHom namespace Subring /-! ## bot -/ instance : Bot (Subring R) := ⟨(Int.castRingHom R).range⟩ instance : Inhabited (Subring R) := ⟨⊥⟩ @[norm_cast] theorem coe_bot : ((⊥ : Subring R) : Set R) = Set.range ((↑) : ℤ → R) := RingHom.coe_range (Int.castRingHom R) theorem mem_bot {x : R} : x ∈ (⊥ : Subring R) ↔ ∃ n : ℤ, ↑n = x := RingHom.mem_range /-! ## inf -/ /-- The inf of two subrings is their intersection. -/ instance : Min (Subring R) := ⟨fun s t => { s.toSubmonoid ⊓ t.toSubmonoid, s.toAddSubgroup ⊓ t.toAddSubgroup with carrier := s ∩ t }⟩ @[simp, norm_cast] theorem coe_inf (p p' : Subring R) : ((p ⊓ p' : Subring R) : Set R) = (p : Set R) ∩ p' := rfl @[simp] theorem mem_inf {p p' : Subring R} {x : R} : x ∈ p ⊓ p' ↔ x ∈ p ∧ x ∈ p' := Iff.rfl instance : InfSet (Subring R) := ⟨fun s => Subring.mk' (⋂ t ∈ s, ↑t) (⨅ t ∈ s, t.toSubmonoid) (⨅ t ∈ s, Subring.toAddSubgroup t) (by simp) (by simp)⟩ @[simp, norm_cast] theorem coe_sInf (S : Set (Subring R)) : ((sInf S : Subring R) : Set R) = ⋂ s ∈ S, ↑s := rfl theorem mem_sInf {S : Set (Subring R)} {x : R} : x ∈ sInf S ↔ ∀ p ∈ S, x ∈ p := Set.mem_iInter₂ @[simp, norm_cast] theorem coe_iInf {ι : Sort*} {S : ι → Subring R} : (↑(⨅ i, S i) : Set R) = ⋂ i, S i := by simp only [iInf, coe_sInf, Set.biInter_range] theorem mem_iInf {ι : Sort*} {S : ι → Subring R} {x : R} : (x ∈ ⨅ i, S i) ↔ ∀ i, x ∈ S i := by simp only [iInf, mem_sInf, Set.forall_mem_range] @[simp] theorem sInf_toSubmonoid (s : Set (Subring R)) : (sInf s).toSubmonoid = ⨅ t ∈ s, t.toSubmonoid := mk'_toSubmonoid _ _ @[simp] theorem sInf_toAddSubgroup (s : Set (Subring R)) : (sInf s).toAddSubgroup = ⨅ t ∈ s, Subring.toAddSubgroup t := mk'_toAddSubgroup _ _ /-- Subrings of a ring form a complete lattice. -/ instance : CompleteLattice (Subring R) := { completeLatticeOfInf (Subring R) fun _ => IsGLB.of_image SetLike.coe_subset_coe isGLB_biInf with bot := ⊥ bot_le := fun s _x hx => let ⟨n, hn⟩ := mem_bot.1 hx hn ▸ intCast_mem s n top := ⊤ le_top := fun _s _x _hx => trivial inf := (· ⊓ ·) inf_le_left := fun _s _t _x => And.left inf_le_right := fun _s _t _x => And.right le_inf := fun _s _t₁ _t₂ h₁ h₂ _x hx => ⟨h₁ hx, h₂ hx⟩ } theorem eq_top_iff' (A : Subring R) : A = ⊤ ↔ ∀ x : R, x ∈ A := eq_top_iff.trans ⟨fun h m => h <| mem_top m, fun h m _ => h m⟩ /-! ## Center of a ring -/ section variable (R) /-- The center of a ring `R` is the set of elements that commute with everything in `R` -/ def center : Subring R := { Subsemiring.center R with carrier := Set.center R neg_mem' := Set.neg_mem_center } theorem coe_center : ↑(center R) = Set.center R := rfl @[simp] theorem center_toSubsemiring : (center R).toSubsemiring = Subsemiring.center R := rfl variable {R} theorem mem_center_iff {z : R} : z ∈ center R ↔ ∀ g, g * z = z * g := Subsemigroup.mem_center_iff instance decidableMemCenter [DecidableEq R] [Fintype R] : DecidablePred (· ∈ center R) := fun _ => decidable_of_iff' _ mem_center_iff @[simp] theorem center_eq_top (R) [CommRing R] : center R = ⊤ := SetLike.coe_injective (Set.center_eq_univ R) /-- The center is commutative. -/ instance : CommRing (center R) := { inferInstanceAs (CommSemiring (Subsemiring.center R)), (center R).toRing with } /-- The center of isomorphic (not necessarily associative) rings are isomorphic. -/ @[simps!] def centerCongr (e : R ≃+* S) : center R ≃+* center S := NonUnitalSubsemiring.centerCongr e /-- The center of a (not necessarily associative) ring is isomorphic to the center of its opposite. -/ @[simps!] def centerToMulOpposite : center R ≃+* center Rᵐᵒᵖ := NonUnitalSubsemiring.centerToMulOpposite end section DivisionRing variable {K : Type u} [DivisionRing K] instance instField : Field (center K) where inv a := ⟨a⁻¹, Set.inv_mem_center a.prop⟩ mul_inv_cancel _ ha := Subtype.ext <| mul_inv_cancel₀ <| Subtype.coe_injective.ne ha div a b := ⟨a / b, Set.div_mem_center a.prop b.prop⟩ div_eq_mul_inv _ _ := Subtype.ext <| div_eq_mul_inv _ _ inv_zero := Subtype.ext inv_zero -- TODO: use a nicer defeq nnqsmul := _ nnqsmul_def := fun _ _ => rfl qsmul := _ qsmul_def := fun _ _ => rfl @[simp] theorem center.coe_inv (a : center K) : ((a⁻¹ : center K) : K) = (a : K)⁻¹ := rfl @[simp] theorem center.coe_div (a b : center K) : ((a / b : center K) : K) = (a : K) / (b : K) := rfl end DivisionRing section Centralizer /-- The centralizer of a set inside a ring as a `Subring`. -/ def centralizer (s : Set R) : Subring R := { Subsemiring.centralizer s with neg_mem' := Set.neg_mem_centralizer } @[simp, norm_cast] theorem coe_centralizer (s : Set R) : (centralizer s : Set R) = s.centralizer := rfl theorem centralizer_toSubmonoid (s : Set R) : (centralizer s).toSubmonoid = Submonoid.centralizer s := rfl theorem centralizer_toSubsemiring (s : Set R) : (centralizer s).toSubsemiring = Subsemiring.centralizer s := rfl theorem mem_centralizer_iff {s : Set R} {z : R} : z ∈ centralizer s ↔ ∀ g ∈ s, g * z = z * g := Iff.rfl theorem center_le_centralizer (s) : center R ≤ centralizer s := s.center_subset_centralizer theorem centralizer_le (s t : Set R) (h : s ⊆ t) : centralizer t ≤ centralizer s := Set.centralizer_subset h @[simp] theorem centralizer_eq_top_iff_subset {s : Set R} : centralizer s = ⊤ ↔ s ⊆ center R := SetLike.ext'_iff.trans Set.centralizer_eq_top_iff_subset @[simp] theorem centralizer_univ : centralizer Set.univ = center R := SetLike.ext' (Set.centralizer_univ R) end Centralizer /-! ## subring closure of a subset -/ /-- The `Subring` generated by a set. -/ def closure (s : Set R) : Subring R := sInf { S | s ⊆ S } theorem mem_closure {x : R} {s : Set R} : x ∈ closure s ↔ ∀ S : Subring R, s ⊆ S → x ∈ S := mem_sInf /-- The subring generated by a set includes the set. -/ @[simp, aesop safe 20 (rule_sets := [SetLike])] theorem subset_closure {s : Set R} : s ⊆ closure s := fun _ hx => mem_closure.2 fun _ hS => hS hx @[aesop 80% (rule_sets := [SetLike])] theorem mem_closure_of_mem {s : Set R} {x : R} (hx : x ∈ s) : x ∈ closure s := subset_closure hx theorem notMem_of_notMem_closure {s : Set R} {P : R} (hP : P ∉ closure s) : P ∉ s := fun h => hP (subset_closure h) @[deprecated (since := "2025-05-23")] alias not_mem_of_not_mem_closure := notMem_of_notMem_closure /-- A subring `t` includes `closure s` if and only if it includes `s`. -/ @[simp] theorem closure_le {s : Set R} {t : Subring R} : closure s ≤ t ↔ s ⊆ t := ⟨Set.Subset.trans subset_closure, fun h => sInf_le h⟩ /-- Subring closure of a set is monotone in its argument: if `s ⊆ t`, then `closure s ≤ closure t`. -/ @[gcongr] theorem closure_mono ⦃s t : Set R⦄ (h : s ⊆ t) : closure s ≤ closure t := closure_le.2 <| Set.Subset.trans h subset_closure theorem closure_eq_of_le {s : Set R} {t : Subring R} (h₁ : s ⊆ t) (h₂ : t ≤ closure s) : closure s = t := le_antisymm (closure_le.2 h₁) h₂ /-- An induction principle for closure membership. If `p` holds for `0`, `1`, and all elements of `s`, and is preserved under addition, negation, and multiplication, then `p` holds for all elements of the closure of `s`. -/ @[elab_as_elim] theorem closure_induction {s : Set R} {p : (x : R) → x ∈ closure s → Prop} (mem : ∀ (x) (hx : x ∈ s), p x (subset_closure hx)) (zero : p 0 (zero_mem _)) (one : p 1 (one_mem _)) (add : ∀ x y hx hy, p x hx → p y hy → p (x + y) (add_mem hx hy)) (neg : ∀ x hx, p x hx → p (-x) (neg_mem hx)) (mul : ∀ x y hx hy, p x hx → p y hy → p (x * y) (mul_mem hx hy)) {x} (hx : x ∈ closure s) : p x hx := let K : Subring R := { carrier := { x | ∃ hx, p x hx } mul_mem' := fun ⟨_, hpx⟩ ⟨_, hpy⟩ ↦ ⟨_, mul _ _ _ _ hpx hpy⟩ add_mem' := fun ⟨_, hpx⟩ ⟨_, hpy⟩ ↦ ⟨_, add _ _ _ _ hpx hpy⟩ neg_mem' := fun ⟨_, hpx⟩ ↦ ⟨_, neg _ _ hpx⟩ zero_mem' := ⟨_, zero⟩ one_mem' := ⟨_, one⟩ } closure_le (t := K) |>.mpr (fun y hy ↦ ⟨subset_closure hy, mem y hy⟩) hx |>.elim fun _ ↦ id /-- An induction principle for closure membership, for predicates with two arguments. -/ @[elab_as_elim] theorem closure_induction₂ {s : Set R} {p : (x y : R) → x ∈ closure s → y ∈ closure s → Prop} (mem_mem : ∀ (x) (y) (hx : x ∈ s) (hy : y ∈ s), p x y (subset_closure hx) (subset_closure hy)) (zero_left : ∀ x hx, p 0 x (zero_mem _) hx) (zero_right : ∀ x hx, p x 0 hx (zero_mem _)) (one_left : ∀ x hx, p 1 x (one_mem _) hx) (one_right : ∀ x hx, p x 1 hx (one_mem _)) (neg_left : ∀ x y hx hy, p x y hx hy → p (-x) y (neg_mem hx) hy) (neg_right : ∀ x y hx hy, p x y hx hy → p x (-y) hx (neg_mem hy)) (add_left : ∀ x y z hx hy hz, p x z hx hz → p y z hy hz → p (x + y) z (add_mem hx hy) hz) (add_right : ∀ x y z hx hy hz, p x y hx hy → p x z hx hz → p x (y + z) hx (add_mem hy hz)) (mul_left : ∀ x y z hx hy hz, p x z hx hz → p y z hy hz → p (x * y) z (mul_mem hx hy) hz) (mul_right : ∀ x y z hx hy hz, p x y hx hy → p x z hx hz → p x (y * z) hx (mul_mem hy hz)) {x y : R} (hx : x ∈ closure s) (hy : y ∈ closure s) : p x y hx hy := by induction hy using closure_induction with | mem z hz => induction hx using closure_induction with | mem _ h => exact mem_mem _ _ h hz | zero => exact zero_left _ _ | one => exact one_left _ _ | mul _ _ _ _ h₁ h₂ => exact mul_left _ _ _ _ _ _ h₁ h₂ | add _ _ _ _ h₁ h₂ => exact add_left _ _ _ _ _ _ h₁ h₂ | neg _ _ h => exact neg_left _ _ _ _ h | zero => exact zero_right x hx | one => exact one_right x hx | mul _ _ _ _ h₁ h₂ => exact mul_right _ _ _ _ _ _ h₁ h₂ | add _ _ _ _ h₁ h₂ => exact add_right _ _ _ _ _ _ h₁ h₂ | neg _ _ h => exact neg_right _ _ _ _ h theorem mem_closure_iff {s : Set R} {x} : x ∈ closure s ↔ x ∈ AddSubgroup.closure (Submonoid.closure s : Set R) := ⟨fun h => by induction h using closure_induction with | mem _ hx => exact AddSubgroup.subset_closure (Submonoid.subset_closure hx) | zero => exact zero_mem _ | one => exact AddSubgroup.subset_closure (one_mem _) | add _ _ _ _ hx hy => exact add_mem hx hy | neg _ _ hx => exact neg_mem hx | mul _ _ _hx _hy hx hy => clear _hx _hy induction hx, hy using AddSubgroup.closure_induction₂ with | mem _ _ hx hy => exact AddSubgroup.subset_closure (mul_mem hx hy) | zero_left => simp | zero_right => simp | add_left _ _ _ _ _ _ h₁ h₂ => simpa [add_mul] using add_mem h₁ h₂ | add_right _ _ _ _ _ _ h₁ h₂ => simpa [mul_add] using add_mem h₁ h₂ | neg_left _ _ _ _ h => simpa [neg_mul] using neg_mem h | neg_right _ _ _ _ h => simpa [mul_neg] using neg_mem h, fun h => by induction h using AddSubgroup.closure_induction with | mem x hx => induction hx using Submonoid.closure_induction with | mem _ h => exact subset_closure h | one => exact one_mem _ | mul _ _ _ _ h₁ h₂ => exact mul_mem h₁ h₂ | zero => exact zero_mem _ | add _ _ _ _ h₁ h₂ => exact add_mem h₁ h₂ | neg _ _ h => exact neg_mem h⟩ lemma closure_le_centralizer_centralizer (s : Set R) : closure s ≤ centralizer (centralizer s) := closure_le.mpr Set.subset_centralizer_centralizer /-- If all elements of `s : Set A` commute pairwise, then `closure s` is a commutative ring. -/ abbrev closureCommRingOfComm {s : Set R} (hcomm : ∀ x ∈ s, ∀ y ∈ s, x * y = y * x) : CommRing (closure s) := { (closure s).toRing with mul_comm := fun ⟨_, h₁⟩ ⟨_, h₂⟩ ↦ have := closure_le_centralizer_centralizer s Subtype.ext <| Set.centralizer_centralizer_comm_of_comm hcomm _ (this h₁) _ (this h₂) } theorem exists_list_of_mem_closure {s : Set R} {x : R} (hx : x ∈ closure s) : ∃ L : List (List R), (∀ t ∈ L, ∀ y ∈ t, y ∈ s ∨ y = (-1 : R)) ∧ (L.map List.prod).sum = x := by rw [mem_closure_iff] at hx induction hx using AddSubgroup.closure_induction with | mem _ hx => obtain ⟨l, hl, h⟩ := Submonoid.exists_list_of_mem_closure hx exact ⟨[l], by simp [h]; clear_aux_decl; tauto⟩ | zero => exact ⟨[], List.forall_mem_nil _, rfl⟩ | add _ _ _ _ hL hM => obtain ⟨⟨L, HL1, HL2⟩, ⟨M, HM1, HM2⟩⟩ := And.intro hL hM exact ⟨L ++ M, List.forall_mem_append.2 ⟨HL1, HM1⟩, by rw [List.map_append, List.sum_append, HL2, HM2]⟩ | neg _ _ hL => obtain ⟨L, hL⟩ := hL exact ⟨L.map (List.cons (-1)), List.forall_mem_map.2 fun j hj => List.forall_mem_cons.2 ⟨Or.inr rfl, hL.1 j hj⟩, hL.2 ▸ List.recOn L (by simp) (by simp +contextual [List.map_cons, add_comm])⟩ variable (R) in /-- `closure` forms a Galois insertion with the coercion to set. -/ protected def gi : GaloisInsertion (@closure R _) (↑) where choice s _ := closure s gc _s _t := closure_le le_l_u _s := subset_closure choice_eq _s _h := rfl /-- Closure of a subring `S` equals `S`. -/ @[simp] theorem closure_eq (s : Subring R) : closure (s : Set R) = s := (Subring.gi R).l_u_eq s @[simp] theorem closure_empty : closure (∅ : Set R) = ⊥ := (Subring.gi R).gc.l_bot @[simp] theorem closure_univ : closure (Set.univ : Set R) = ⊤ := @coe_top R _ ▸ closure_eq ⊤ theorem closure_union (s t : Set R) : closure (s ∪ t) = closure s ⊔ closure t := (Subring.gi R).gc.l_sup theorem closure_iUnion {ι} (s : ι → Set R) : closure (⋃ i, s i) = ⨆ i, closure (s i) := (Subring.gi R).gc.l_iSup theorem closure_sUnion (s : Set (Set R)) : closure (⋃₀ s) = ⨆ t ∈ s, closure t := (Subring.gi R).gc.l_sSup @[simp] theorem closure_singleton_intCast (n : ℤ) : closure {(n : R)} = ⊥ := bot_unique <| closure_le.2 <| Set.singleton_subset_iff.mpr <| intCast_mem _ _ @[simp] theorem closure_singleton_natCast (n : ℕ) : closure {(n : R)} = ⊥ := mod_cast closure_singleton_intCast n @[simp] theorem closure_singleton_zero : closure {(0 : R)} = ⊥ := mod_cast closure_singleton_natCast 0 @[simp] theorem closure_singleton_one : closure {(1 : R)} = ⊥ := mod_cast closure_singleton_natCast 1 @[simp] theorem closure_insert_intCast (n : ℤ) (s : Set R) : closure (insert (n : R) s) = closure s := by rw [Set.insert_eq, closure_union] simp @[simp] theorem closure_insert_natCast (n : ℕ) (s : Set R) : closure (insert (n : R) s) = closure s := mod_cast closure_insert_intCast n s @[simp] theorem closure_insert_zero (s : Set R) : closure (insert 0 s) = closure s := mod_cast closure_insert_natCast 0 s @[simp] theorem closure_insert_one (s : Set R) : closure (insert 1 s) = closure s := mod_cast closure_insert_natCast 1 s theorem map_sup (s t : Subring R) (f : R →+* S) : (s ⊔ t).map f = s.map f ⊔ t.map f := (gc_map_comap f).l_sup theorem map_iSup {ι : Sort*} (f : R →+* S) (s : ι → Subring R) : (iSup s).map f = ⨆ i, (s i).map f := (gc_map_comap f).l_iSup theorem map_inf (s t : Subring R) (f : R →+* S) (hf : Function.Injective f) : (s ⊓ t).map f = s.map f ⊓ t.map f := SetLike.coe_injective (Set.image_inter hf) theorem map_iInf {ι : Sort*} [Nonempty ι] (f : R →+* S) (hf : Function.Injective f) (s : ι → Subring R) : (iInf s).map f = ⨅ i, (s i).map f := by apply SetLike.coe_injective simpa using (Set.injOn_of_injective hf).image_iInter_eq (s := SetLike.coe ∘ s) theorem comap_inf (s t : Subring S) (f : R →+* S) : (s ⊓ t).comap f = s.comap f ⊓ t.comap f := (gc_map_comap f).u_inf theorem comap_iInf {ι : Sort*} (f : R →+* S) (s : ι → Subring S) : (iInf s).comap f = ⨅ i, (s i).comap f := (gc_map_comap f).u_iInf @[simp] theorem map_bot (f : R →+* S) : (⊥ : Subring R).map f = ⊥ := (gc_map_comap f).l_bot @[simp] theorem comap_top (f : R →+* S) : (⊤ : Subring S).comap f = ⊤ := (gc_map_comap f).u_top /-- Given `Subring`s `s`, `t` of rings `R`, `S` respectively, `s.prod t` is `s ×̂ t` as a subring of `R × S`. -/ def prod (s : Subring R) (t : Subring S) : Subring (R × S) := { s.toSubmonoid.prod t.toSubmonoid, s.toAddSubgroup.prod t.toAddSubgroup with carrier := s ×ˢ t } @[norm_cast] theorem coe_prod (s : Subring R) (t : Subring S) : (s.prod t : Set (R × S)) = (s : Set R) ×ˢ (t : Set S) := rfl theorem mem_prod {s : Subring R} {t : Subring S} {p : R × S} : p ∈ s.prod t ↔ p.1 ∈ s ∧ p.2 ∈ t := Iff.rfl @[gcongr, mono] theorem prod_mono ⦃s₁ s₂ : Subring R⦄ (hs : s₁ ≤ s₂) ⦃t₁ t₂ : Subring S⦄ (ht : t₁ ≤ t₂) : s₁.prod t₁ ≤ s₂.prod t₂ := Set.prod_mono hs ht theorem prod_mono_right (s : Subring R) : Monotone fun t : Subring S => s.prod t := prod_mono (le_refl s) theorem prod_mono_left (t : Subring S) : Monotone fun s : Subring R => s.prod t := fun _ _ hs => prod_mono hs (le_refl t) theorem prod_top (s : Subring R) : s.prod (⊤ : Subring S) = s.comap (RingHom.fst R S) := ext fun x => by simp [mem_prod] theorem top_prod (s : Subring S) : (⊤ : Subring R).prod s = s.comap (RingHom.snd R S) := ext fun x => by simp [mem_prod] @[simp] theorem top_prod_top : (⊤ : Subring R).prod (⊤ : Subring S) = ⊤ := (top_prod _).trans <| comap_top _ /-- Product of subrings is isomorphic to their product as rings. -/ def prodEquiv (s : Subring R) (t : Subring S) : s.prod t ≃+* s × t := { Equiv.Set.prod (s : Set R) (t : Set S) with map_mul' := fun _x _y => rfl map_add' := fun _x _y => rfl } /-- The underlying set of a non-empty directed sSup of subrings is just a union of the subrings. Note that this fails without the directedness assumption (the union of two subrings is typically not a subring) -/ theorem mem_iSup_of_directed {ι} [hι : Nonempty ι] {S : ι → Subring R} (hS : Directed (· ≤ ·) S) {x : R} : (x ∈ ⨆ i, S i) ↔ ∃ i, x ∈ S i := by refine ⟨?_, fun ⟨i, hi⟩ ↦ le_iSup S i hi⟩ let U : Subring R := Subring.mk' (⋃ i, (S i : Set R)) (⨆ i, (S i).toSubmonoid) (⨆ i, (S i).toAddSubgroup) (Submonoid.coe_iSup_of_directed hS) (AddSubgroup.coe_iSup_of_directed hS) suffices ⨆ i, S i ≤ U by simpa [U] using @this x exact iSup_le fun i x hx ↦ Set.mem_iUnion.2 ⟨i, hx⟩ theorem coe_iSup_of_directed {ι} [hι : Nonempty ι] {S : ι → Subring R} (hS : Directed (· ≤ ·) S) : ((⨆ i, S i : Subring R) : Set R) = ⋃ i, S i := Set.ext fun x ↦ by simp [mem_iSup_of_directed hS] theorem mem_sSup_of_directedOn {S : Set (Subring R)} (Sne : S.Nonempty) (hS : DirectedOn (· ≤ ·) S) {x : R} : x ∈ sSup S ↔ ∃ s ∈ S, x ∈ s := by haveI : Nonempty S := Sne.to_subtype simp only [sSup_eq_iSup', mem_iSup_of_directed hS.directed_val, SetCoe.exists, exists_prop] theorem coe_sSup_of_directedOn {S : Set (Subring R)} (Sne : S.Nonempty) (hS : DirectedOn (· ≤ ·) S) : (↑(sSup S) : Set R) = ⋃ s ∈ S, ↑s := Set.ext fun x => by simp [mem_sSup_of_directedOn Sne hS] theorem mem_map_equiv {f : R ≃+* S} {K : Subring R} {x : S} : x ∈ K.map (f : R →+* S) ↔ f.symm x ∈ K := @Set.mem_image_equiv _ _ (K : Set R) f.toEquiv x theorem map_equiv_eq_comap_symm (f : R ≃+* S) (K : Subring R) : K.map (f : R →+* S) = K.comap f.symm := SetLike.coe_injective (f.toEquiv.image_eq_preimage_symm K) theorem comap_equiv_eq_map_symm (f : R ≃+* S) (K : Subring S) : K.comap (f : R →+* S) = K.map f.symm := (map_equiv_eq_comap_symm f.symm K).symm end Subring namespace RingHom variable {s : Subring R} open Subring /-- Restriction of a ring homomorphism to its range interpreted as a subsemiring. This is the bundled version of `Set.rangeFactorization`. -/ def rangeRestrict (f : R →+* S) : R →+* f.range := f.codRestrict f.range fun x => ⟨x, rfl⟩ @[simp] theorem coe_rangeRestrict (f : R →+* S) (x : R) : (f.rangeRestrict x : S) = f x := rfl theorem rangeRestrict_surjective (f : R →+* S) : Function.Surjective f.rangeRestrict := fun ⟨_y, hy⟩ => let ⟨x, hx⟩ := mem_range.mp hy ⟨x, Subtype.ext hx⟩ theorem range_eq_top {f : R →+* S} : f.range = (⊤ : Subring S) ↔ Function.Surjective f := SetLike.ext'_iff.trans <| Iff.trans (by rw [coe_range, coe_top]) Set.range_eq_univ /-- The range of a surjective ring homomorphism is the whole of the codomain. -/ @[simp] theorem range_eq_top_of_surjective (f : R →+* S) (hf : Function.Surjective f) : f.range = (⊤ : Subring S) := range_eq_top.2 hf section eqLocus variable {S : Type v} [Semiring S] /-- The subring of elements `x : R` such that `f x = g x`, i.e., the equalizer of f and g as a subring of R -/ def eqLocus (f g : R →+* S) : Subring R := { (f : R →* S).eqLocusM g, (f : R →+ S).eqLocus g with carrier := { x | f x = g x } } @[simp] theorem mem_eqLocus {f g : R →+* S} {x : R} : x ∈ f.eqLocus g ↔ f x = g x := Iff.rfl @[simp] theorem eqLocus_same (f : R →+* S) : f.eqLocus f = ⊤ := SetLike.ext fun _ => eq_self_iff_true _ /-- If two ring homomorphisms are equal on a set, then they are equal on its subring closure. -/ theorem eqOn_set_closure {f g : R →+* S} {s : Set R} (h : Set.EqOn f g s) : Set.EqOn f g (closure s) := show closure s ≤ f.eqLocus g from closure_le.2 h theorem eq_of_eqOn_set_top {f g : R →+* S} (h : Set.EqOn f g (⊤ : Subring R)) : f = g := ext fun _x => h trivial theorem eq_of_eqOn_set_dense {s : Set R} (hs : closure s = ⊤) {f g : R →+* S} (h : s.EqOn f g) : f = g := eq_of_eqOn_set_top <| hs ▸ eqOn_set_closure h end eqLocus theorem closure_preimage_le (f : R →+* S) (s : Set S) : closure (f ⁻¹' s) ≤ (closure s).comap f := closure_le.2 fun _ hx => SetLike.mem_coe.2 <| mem_comap.2 <| subset_closure hx /-- The image under a ring homomorphism of the subring generated by a set equals the subring generated by the image of the set. -/ theorem map_closure (f : R →+* S) (s : Set R) : (closure s).map f = closure (f '' s) := Set.image_preimage.l_comm_of_u_comm (gc_map_comap f) (Subring.gi S).gc (Subring.gi R).gc fun _ ↦ rfl end RingHom namespace Subring open RingHom theorem mem_closure_image_of (f : R →+* S) {s : Set R} {x : R} (hx : x ∈ Subring.closure s) : f x ∈ Subring.closure (f '' s) := by rw [← f.map_closure, Subring.mem_map] exact ⟨x, hx, rfl⟩ /-- The ring homomorphism associated to an inclusion of subrings. -/ def inclusion {S T : Subring R} (h : S ≤ T) : S →+* T := S.subtype.codRestrict _ fun x => h x.2 @[simp] theorem coe_inclusion {S T : Subring R} (h : S ≤ T) (x : S) : (Subring.inclusion h x : R) = x := by simp [Subring.inclusion] @[simp] theorem range_subtype (s : Subring R) : s.subtype.range = s := SetLike.coe_injective <| (coe_rangeS _).trans Subtype.range_coe theorem range_fst : (fst R S).rangeS = ⊤ := (fst R S).rangeS_top_of_surjective <| Prod.fst_surjective theorem range_snd : (snd R S).rangeS = ⊤ := (snd R S).rangeS_top_of_surjective <| Prod.snd_surjective @[simp] theorem prod_bot_sup_bot_prod (s : Subring R) (t : Subring S) : s.prod ⊥ ⊔ prod ⊥ t = s.prod t := le_antisymm (sup_le (prod_mono_right s bot_le) (prod_mono_left t bot_le)) fun p hp => Prod.fst_mul_snd p ▸ mul_mem ((le_sup_left : s.prod ⊥ ≤ s.prod ⊥ ⊔ prod ⊥ t) ⟨hp.1, SetLike.mem_coe.2 <| one_mem ⊥⟩) ((le_sup_right : prod ⊥ t ≤ s.prod ⊥ ⊔ prod ⊥ t) ⟨SetLike.mem_coe.2 <| one_mem ⊥, hp.2⟩) end Subring namespace RingEquiv variable {s t : Subring R} /-- Makes the identity isomorphism from a proof two subrings of a multiplicative monoid are equal. -/ def subringCongr (h : s = t) : s ≃+* t := { Equiv.setCongr <| congr_arg _ h with map_mul' := fun _ _ => rfl map_add' := fun _ _ => rfl } /-- Restrict a ring homomorphism with a left inverse to a ring isomorphism to its `RingHom.range`. -/ def ofLeftInverse {g : S → R} {f : R →+* S} (h : Function.LeftInverse g f) : R ≃+* f.range := { f.rangeRestrict with toFun := fun x => f.rangeRestrict x invFun := fun x => (g ∘ f.range.subtype) x left_inv := h right_inv := fun x => Subtype.ext <| let ⟨x', hx'⟩ := RingHom.mem_range.mp x.prop show f (g x) = x by rw [← hx', h x'] } @[simp] theorem ofLeftInverse_apply {g : S → R} {f : R →+* S} (h : Function.LeftInverse g f) (x : R) : ↑(ofLeftInverse h x) = f x := rfl @[simp] theorem ofLeftInverse_symm_apply {g : S → R} {f : R →+* S} (h : Function.LeftInverse g f) (x : f.range) : (ofLeftInverse h).symm x = g x := rfl /-- Given an equivalence `e : R ≃+* S` of rings and a subring `s` of `R`, `subringMap e s` is the induced equivalence between `s` and `s.map e` -/ def subringMap (e : R ≃+* S) : s ≃+* s.map e.toRingHom := e.subsemiringMap s.toSubsemiring end RingEquiv namespace Subring variable {s : Set R} @[elab_as_elim] protected theorem InClosure.recOn {C : R → Prop} {x : R} (hx : x ∈ closure s) (h1 : C 1) (hneg1 : C (-1)) (hs : ∀ z ∈ s, ∀ n, C n → C (z * n)) (ha : ∀ {x y}, C x → C y → C (x + y)) : C x := by have h0 : C 0 := add_neg_cancel (1 : R) ▸ ha h1 hneg1 rcases exists_list_of_mem_closure hx with ⟨L, HL, rfl⟩ clear hx induction L with | nil => exact h0 | cons hd tl ih => ?_ rw [List.forall_mem_cons] at HL suffices C (List.prod hd) by rw [List.map_cons, List.sum_cons] exact ha this (ih HL.2) replace HL := HL.1 clear ih tl rsuffices ⟨L, HL', HP | HP⟩ : ∃ L : List R, (∀ x ∈ L, x ∈ s) ∧ (List.prod hd = List.prod L ∨ List.prod hd = -List.prod L) · rw [HP] clear HP HL hd induction L with | nil => exact h1 | cons hd tl ih => rw [List.forall_mem_cons] at HL' rw [List.prod_cons] exact hs _ HL'.1 _ (ih HL'.2) · rw [HP] clear HP HL hd induction L with | nil => exact hneg1 | cons hd tl ih => rw [List.prod_cons, neg_mul_eq_mul_neg] rw [List.forall_mem_cons] at HL' exact hs _ HL'.1 _ (ih HL'.2) induction hd with | nil => exact ⟨[], List.forall_mem_nil _, Or.inl rfl⟩ | cons hd tl ih => ?_ rw [List.forall_mem_cons] at HL rcases ih HL.2 with ⟨L, HL', HP | HP⟩ <;> rcases HL.1 with hhd | hhd · exact ⟨hd::L, List.forall_mem_cons.2 ⟨hhd, HL'⟩, Or.inl <| by rw [List.prod_cons, List.prod_cons, HP]⟩ · exact ⟨L, HL', Or.inr <| by rw [List.prod_cons, hhd, neg_one_mul, HP]⟩ · exact ⟨hd::L, List.forall_mem_cons.2 ⟨hhd, HL'⟩, Or.inr <| by rw [List.prod_cons, List.prod_cons, HP, neg_mul_eq_mul_neg]⟩ · exact ⟨L, HL', Or.inl <| by rw [List.prod_cons, hhd, HP, neg_one_mul, neg_neg]⟩ theorem closure_preimage_le (f : R →+* S) (s : Set S) : closure (f ⁻¹' s) ≤ (closure s).comap f := closure_le.2 fun _ hx => SetLike.mem_coe.2 <| mem_comap.2 <| subset_closure hx end Subring /-! ## Actions by `Subring`s These are just copies of the definitions about `Subsemiring` starting from `Subsemiring.MulAction`. When `R` is commutative, `Algebra.ofSubring` provides a stronger result than those found in this file, which uses the same scalar action. -/ section Actions namespace Subring variable {α β : Type*} /-- The action by a subring is the action by the underlying ring. -/ instance [SMul R α] (S : Subring R) : SMul S α := inferInstanceAs (SMul S.toSubsemiring α) theorem smul_def [SMul R α] {S : Subring R} (g : S) (m : α) : g • m = (g : R) • m := rfl instance smulCommClass_left [SMul R β] [SMul α β] [SMulCommClass R α β] (S : Subring R) : SMulCommClass S α β := inferInstanceAs (SMulCommClass S.toSubsemiring α β) instance smulCommClass_right [SMul α β] [SMul R β] [SMulCommClass α R β] (S : Subring R) : SMulCommClass α S β := inferInstanceAs (SMulCommClass α S.toSubsemiring β) /-- Note that this provides `IsScalarTower S R R` which is needed by `smul_mul_assoc`. -/ instance [SMul α β] [SMul R α] [SMul R β] [IsScalarTower R α β] (S : Subring R) : IsScalarTower S α β := inferInstanceAs (IsScalarTower S.toSubsemiring α β) instance [SMul R α] [FaithfulSMul R α] (S : Subring R) : FaithfulSMul S α := inferInstanceAs (FaithfulSMul S.toSubsemiring α) /-- The action by a subring is the action by the underlying ring. -/ instance [MulAction R α] (S : Subring R) : MulAction S α := inferInstanceAs (MulAction S.toSubsemiring α) /-- The action by a subring is the action by the underlying ring. -/ instance [AddMonoid α] [DistribMulAction R α] (S : Subring R) : DistribMulAction S α := inferInstanceAs (DistribMulAction S.toSubsemiring α) /-- The action by a subring is the action by the underlying ring. -/ instance [Monoid α] [MulDistribMulAction R α] (S : Subring R) : MulDistribMulAction S α := inferInstanceAs (MulDistribMulAction S.toSubsemiring α) /-- The action by a subring is the action by the underlying ring. -/ instance [Zero α] [SMulWithZero R α] (S : Subring R) : SMulWithZero S α := inferInstanceAs (SMulWithZero S.toSubsemiring α) /-- The action by a subring is the action by the underlying ring. -/ instance [Zero α] [MulActionWithZero R α] (S : Subring R) : MulActionWithZero S α := inferInstanceAs (MulActionWithZero S.toSubsemiring α) /-- The action by a subring is the action by the underlying ring. -/ instance [AddCommMonoid α] [Module R α] (S : Subring R) : Module S α := inferInstanceAs (Module S.toSubsemiring α) /-- The action by a subsemiring is the action by the underlying ring. -/ instance [Semiring α] [MulSemiringAction R α] (S : Subring R) : MulSemiringAction S α := inferInstanceAs (MulSemiringAction S.toSubmonoid α) /-- The center of a semiring acts commutatively on that semiring. -/ instance center.smulCommClass_left : SMulCommClass (center R) R R := Subsemiring.center.smulCommClass_left /-- The center of a semiring acts commutatively on that semiring. -/ instance center.smulCommClass_right : SMulCommClass R (center R) R := Subsemiring.center.smulCommClass_right end Subring end Actions namespace Subring theorem map_comap_eq (f : R →+* S) (t : Subring S) : (t.comap f).map f = t ⊓ f.range := SetLike.coe_injective Set.image_preimage_eq_inter_range theorem map_comap_eq_self {f : R →+* S} {t : Subring S} (h : t ≤ f.range) : (t.comap f).map f = t := by simpa only [inf_of_le_left h] using Subring.map_comap_eq f t theorem map_comap_eq_self_of_surjective {f : R →+* S} (hf : Function.Surjective f) (t : Subring S) : (t.comap f).map f = t := map_comap_eq_self <| by simp [hf] theorem comap_map_eq (f : R →+* S) (s : Subring R) : (s.map f).comap f = s ⊔ closure (f ⁻¹' {0}) := by apply le_antisymm · intro x hx rw [mem_comap, mem_map] at hx obtain ⟨y, hy, hxy⟩ := hx replace hxy : x - y ∈ f ⁻¹' {0} := by simp [hxy] rw [← closure_eq s, ← closure_union, ← add_sub_cancel y x] exact Subring.add_mem _ (subset_closure <| Or.inl hy) (subset_closure <| Or.inr hxy) · rw [← map_le_iff_le_comap, map_sup, f.map_closure] apply le_of_eq rw [sup_eq_left, closure_le] exact (Set.image_preimage_subset f {0}).trans (Set.singleton_subset_iff.2 (s.map f).zero_mem) theorem comap_map_eq_self {f : R →+* S} {s : Subring R} (h : f ⁻¹' {0} ⊆ s) : (s.map f).comap f = s := by convert comap_map_eq f s rwa [left_eq_sup, closure_le] theorem comap_map_eq_self_of_injective {f : R →+* S} (hf : Function.Injective f) (s : Subring R) : (s.map f).comap f = s := SetLike.coe_injective (Set.preimage_image_eq _ hf) end Subring theorem AddSubgroup.int_mul_mem {G : AddSubgroup R} (k : ℤ) {g : R} (h : g ∈ G) : (k : R) * g ∈ G := by convert AddSubgroup.zsmul_mem G h k using 1 rw [zsmul_eq_mul]
.lake/packages/mathlib/Mathlib/Algebra/Ring/Subring/Defs.lean
import Mathlib.Algebra.Ring.Subsemiring.Defs import Mathlib.RingTheory.NonUnitalSubring.Defs /-! # Subrings Let `R` be a ring. This file defines the "bundled" subring type `Subring R`, a type whose terms correspond to subrings of `R`. This is the preferred way to talk about subrings in mathlib. Unbundled subrings (`s : Set R` and `IsSubring s`) are not in this file, and they will ultimately be deprecated. We prove that subrings are a complete lattice, and that you can `map` (pushforward) and `comap` (pull back) them along ring homomorphisms. We define the `closure` construction from `Set R` to `Subring R`, sending a subset of `R` to the subring it generates, and prove that it is a Galois insertion. ## Main definitions Notation used here: `(R : Type u) [Ring R] (S : Type u) [Ring S] (f g : R →+* S)` `(A : Subring R) (B : Subring S) (s : Set R)` * `Subring R` : the type of subrings of a ring `R`. * `instance : CompleteLattice (Subring R)` : the complete lattice structure on the subrings. * `Subring.center` : the center of a ring `R`. * `Subring.closure` : subring closure of a set, i.e., the smallest subring that includes the set. * `Subring.gi` : `closure : Set M → Subring M` and coercion `(↑) : Subring M → et M` form a `GaloisInsertion`. * `comap f B : Subring A` : the preimage of a subring `B` along the ring homomorphism `f` * `map f A : Subring B` : the image of a subring `A` along the ring homomorphism `f`. * `prod A B : Subring (R × S)` : the product of subrings * `f.range : Subring B` : the range of the ring homomorphism `f`. * `eqLocus f g : Subring R` : given ring homomorphisms `f g : R →+* S`, the subring of `R` where `f x = g x` ## Implementation notes A subring is implemented as a subsemiring which is also an additive subgroup. The initial PR was as a submonoid which is also an additive subgroup. Lattice inclusion (e.g. `≤` and `⊓`) is used rather than set notation (`⊆` and `∩`), although `∈` is defined as membership of a subring's underlying set. ## Tags subring, subrings -/ assert_not_exists RelIso Even IsOrderedMonoid universe u v w variable {R : Type u} {S : Type v} {T : Type w} [Ring R] section SubringClass /-- `SubringClass S R` states that `S` is a type of subsets `s ⊆ R` that are both a multiplicative submonoid and an additive subgroup. -/ class SubringClass (S : Type*) (R : outParam (Type u)) [Ring R] [SetLike S R] : Prop extends SubsemiringClass S R, NegMemClass S R -- See note [lower instance priority] instance (priority := 100) SubringClass.addSubgroupClass (S : Type*) (R : Type u) [SetLike S R] [Ring R] [h : SubringClass S R] : AddSubgroupClass S R := { h with } instance (priority := 100) SubringClass.nonUnitalSubringClass (S : Type*) (R : Type u) [SetLike S R] [Ring R] [SubringClass S R] : NonUnitalSubringClass S R where variable [SetLike S R] [hSR : SubringClass S R] (s : S) @[simp, aesop safe (rule_sets := [SetLike])] theorem intCast_mem (n : ℤ) : (n : R) ∈ s := by simp only [← zsmul_one, zsmul_mem, one_mem] namespace SubringClass instance (priority := 75) toHasIntCast : IntCast s := ⟨fun n => ⟨n, intCast_mem s n⟩⟩ -- Prefer subclasses of `Ring` over subclasses of `SubringClass`. /-- A subring of a ring inherits a ring structure -/ instance (priority := 75) toRing : Ring s := fast_instance% Subtype.coe_injective.ring Subtype.val rfl rfl (fun _ _ => rfl) (fun _ _ => rfl) (fun _ => rfl) (fun _ _ => rfl) (fun _ _ => rfl) (fun _ _ => rfl) (fun _ _ => rfl) (fun _ => rfl) fun _ => rfl -- Prefer subclasses of `Ring` over subclasses of `SubringClass`. /-- A subring of a `CommRing` is a `CommRing`. -/ instance (priority := 75) toCommRing {R} [CommRing R] [SetLike S R] [SubringClass S R] : CommRing s := fast_instance% Subtype.coe_injective.commRing Subtype.val rfl rfl (fun _ _ => rfl) (fun _ _ => rfl) (fun _ => rfl) (fun _ _ => rfl) (fun _ _ => rfl) (fun _ _ => rfl) (fun _ _ => rfl) (fun _ => rfl) fun _ => rfl -- Prefer subclasses of `Ring` over subclasses of `SubringClass`. /-- A subring of a domain is a domain. -/ instance (priority := 75) {R} [Ring R] [IsDomain R] [SetLike S R] [SubringClass S R] : IsDomain s := NoZeroDivisors.to_isDomain _ /-- The natural ring hom from a subring of ring `R` to `R`. -/ def subtype (s : S) : s →+* R := { SubmonoidClass.subtype s, AddSubgroupClass.subtype s with toFun := (↑) } variable {s} in @[simp] lemma subtype_apply (x : s) : SubringClass.subtype s x = x := rfl lemma subtype_injective : Function.Injective (subtype s) := Subtype.coe_injective @[simp] theorem coe_subtype : (subtype s : s → R) = ((↑) : s → R) := rfl @[simp, norm_cast] theorem coe_natCast (n : ℕ) : ((n : s) : R) = n := rfl @[simp, norm_cast] theorem coe_intCast (n : ℤ) : ((n : s) : R) = n := rfl end SubringClass end SubringClass variable [Ring S] [Ring T] /-- `Subring R` is the type of subrings of `R`. A subring of `R` is a subset `s` that is a multiplicative submonoid and an additive subgroup. Note in particular that it shares the same 0 and 1 as R. -/ structure Subring (R : Type u) [Ring R] extends Subsemiring R, AddSubgroup R /-- Reinterpret a `Subring` as a `Subsemiring`. -/ add_decl_doc Subring.toSubsemiring /-- Reinterpret a `Subring` as an `AddSubgroup`. -/ add_decl_doc Subring.toAddSubgroup namespace Subring instance : SetLike (Subring R) R where coe s := s.carrier coe_injective' p q h := by cases p; cases q; congr; exact SetLike.ext' h initialize_simps_projections Subring (carrier → coe, as_prefix coe) /-- The actual `Subring` obtained from an element of a `SubringClass`. -/ @[simps] def ofClass {S R : Type*} [Ring R] [SetLike S R] [SubringClass S R] (s : S) : Subring R where carrier := s add_mem' := add_mem zero_mem' := zero_mem _ mul_mem' := mul_mem neg_mem' := neg_mem one_mem' := one_mem _ instance (priority := 100) : CanLift (Set R) (Subring R) (↑) (fun s ↦ 0 ∈ s ∧ (∀ {x y}, x ∈ s → y ∈ s → x + y ∈ s) ∧ 1 ∈ s ∧ (∀ {x y}, x ∈ s → y ∈ s → x * y ∈ s) ∧ ∀ {x}, x ∈ s → -x ∈ s) where prf s h := ⟨ { carrier := s zero_mem' := h.1 add_mem' := h.2.1 one_mem' := h.2.2.1 mul_mem' := h.2.2.2.1 neg_mem' := h.2.2.2.2 }, rfl ⟩ instance : SubringClass (Subring R) R where zero_mem s := s.zero_mem' add_mem {s} := s.add_mem' one_mem s := s.one_mem' mul_mem {s} := s.mul_mem' neg_mem {s} := s.neg_mem' /-- Turn a `Subring` into a `NonUnitalSubring` by forgetting that it contains `1`. -/ def toNonUnitalSubring (S : Subring R) : NonUnitalSubring R where __ := S @[simp] theorem mem_toSubsemiring {s : Subring R} {x : R} : x ∈ s.toSubsemiring ↔ x ∈ s := Iff.rfl theorem mem_carrier {s : Subring R} {x : R} : x ∈ s.carrier ↔ x ∈ s := Iff.rfl @[simp] theorem mem_mk {S : Subsemiring R} {x : R} (h) : x ∈ (⟨S, h⟩ : Subring R) ↔ x ∈ S := Iff.rfl @[simp] theorem coe_set_mk (S : Subsemiring R) (h) : ((⟨S, h⟩ : Subring R) : Set R) = S := rfl @[simp] theorem mk_le_mk {S S' : Subsemiring R} (h₁ h₂) : (⟨S, h₁⟩ : Subring R) ≤ (⟨S', h₂⟩ : Subring R) ↔ S ≤ S' := Iff.rfl lemma one_mem_toNonUnitalSubring (S : Subring R) : 1 ∈ S.toNonUnitalSubring := S.one_mem /-- Two subrings are equal if they have the same elements. -/ @[ext] theorem ext {S T : Subring R} (h : ∀ x, x ∈ S ↔ x ∈ T) : S = T := SetLike.ext h /-- Copy of a subring with a new `carrier` equal to the old one. Useful to fix definitional equalities. -/ @[simps coe toSubsemiring] protected def copy (S : Subring R) (s : Set R) (hs : s = ↑S) : Subring R := { S.toSubsemiring.copy s hs with carrier := s neg_mem' := hs.symm ▸ S.neg_mem' } theorem copy_eq (S : Subring R) (s : Set R) (hs : s = ↑S) : S.copy s hs = S := SetLike.coe_injective hs theorem toSubsemiring_injective : Function.Injective (toSubsemiring : Subring R → Subsemiring R) | _, _, h => ext (SetLike.ext_iff.mp h :) theorem toAddSubgroup_injective : Function.Injective (toAddSubgroup : Subring R → AddSubgroup R) | _, _, h => ext (SetLike.ext_iff.mp h :) theorem toSubmonoid_injective : Function.Injective (fun s : Subring R => s.toSubmonoid) | _, _, h => ext (SetLike.ext_iff.mp h :) /-- Construct a `Subring R` from a set `s`, a submonoid `sm`, and an additive subgroup `sa` such that `x ∈ s ↔ x ∈ sm ↔ x ∈ sa`. -/ @[simps! coe] protected def mk' (s : Set R) (sm : Submonoid R) (sa : AddSubgroup R) (hm : ↑sm = s) (ha : ↑sa = s) : Subring R := { sm.copy s hm.symm, sa.copy s ha.symm with } @[simp] theorem mem_mk' {s : Set R} {sm : Submonoid R} (hm : ↑sm = s) {sa : AddSubgroup R} (ha : ↑sa = s) {x : R} : x ∈ Subring.mk' s sm sa hm ha ↔ x ∈ s := Iff.rfl @[simp] theorem mk'_toSubmonoid {s : Set R} {sm : Submonoid R} (hm : ↑sm = s) {sa : AddSubgroup R} (ha : ↑sa = s) : (Subring.mk' s sm sa hm ha).toSubmonoid = sm := SetLike.coe_injective hm.symm @[simp] theorem mk'_toAddSubgroup {s : Set R} {sm : Submonoid R} (hm : ↑sm = s) {sa : AddSubgroup R} (ha : ↑sa = s) : (Subring.mk' s sm sa hm ha).toAddSubgroup = sa := SetLike.coe_injective ha.symm end Subring /-- A `Subsemiring` containing -1 is a `Subring`. -/ @[simps toSubsemiring] def Subsemiring.toSubring (s : Subsemiring R) (hneg : (-1 : R) ∈ s) : Subring R where toSubsemiring := s neg_mem' h := by rw [← neg_one_mul] exact mul_mem hneg h namespace Subring variable (s : Subring R) /-- A subring contains the ring's 1. -/ protected theorem one_mem : (1 : R) ∈ s := one_mem _ /-- A subring contains the ring's 0. -/ protected theorem zero_mem : (0 : R) ∈ s := zero_mem _ /-- A subring is closed under multiplication. -/ protected theorem mul_mem {x y : R} : x ∈ s → y ∈ s → x * y ∈ s := mul_mem /-- A subring is closed under addition. -/ protected theorem add_mem {x y : R} : x ∈ s → y ∈ s → x + y ∈ s := add_mem /-- A subring is closed under negation. -/ protected theorem neg_mem {x : R} : x ∈ s → -x ∈ s := neg_mem /-- A subring is closed under subtraction -/ protected theorem sub_mem {x y : R} (hx : x ∈ s) (hy : y ∈ s) : x - y ∈ s := sub_mem hx hy /-- A subring of a ring inherits a ring structure -/ instance toRing : Ring s := SubringClass.toRing s protected theorem zsmul_mem {x : R} (hx : x ∈ s) (n : ℤ) : n • x ∈ s := zsmul_mem hx n protected theorem pow_mem {x : R} (hx : x ∈ s) (n : ℕ) : x ^ n ∈ s := pow_mem hx n @[simp, norm_cast] theorem coe_add (x y : s) : (↑(x + y) : R) = ↑x + ↑y := rfl @[simp, norm_cast] theorem coe_neg (x : s) : (↑(-x) : R) = -↑x := rfl @[simp, norm_cast] theorem coe_mul (x y : s) : (↑(x * y) : R) = ↑x * ↑y := rfl @[simp, norm_cast] theorem coe_zero : ((0 : s) : R) = 0 := rfl @[simp, norm_cast] theorem coe_one : ((1 : s) : R) = 1 := rfl @[simp, norm_cast] theorem coe_pow (x : s) (n : ℕ) : ↑(x ^ n) = (x : R) ^ n := SubmonoidClass.coe_pow x n theorem coe_eq_zero_iff {x : s} : (x : R) = 0 ↔ x = 0 := ⟨fun h => Subtype.ext (Trans.trans h s.coe_zero.symm), fun h => h.symm ▸ s.coe_zero⟩ /-- A subring of a `CommRing` is a `CommRing`. -/ instance toCommRing {R} [CommRing R] (s : Subring R) : CommRing s := SubringClass.toCommRing s /-- A subring of a non-trivial ring is non-trivial. -/ instance {R} [Ring R] [Nontrivial R] (s : Subring R) : Nontrivial s := s.toSubsemiring.nontrivial /-- A subring of a ring with no zero divisors has no zero divisors. -/ instance {R} [Ring R] [NoZeroDivisors R] (s : Subring R) : NoZeroDivisors s := s.toSubsemiring.noZeroDivisors /-- A subring of a domain is a domain. -/ instance {R} [Ring R] [IsDomain R] (s : Subring R) : IsDomain s := NoZeroDivisors.to_isDomain _ /-- The natural ring hom from a subring of ring `R` to `R`. -/ def subtype (s : Subring R) : s →+* R := { s.toSubmonoid.subtype, s.toAddSubgroup.subtype with toFun := (↑) } @[simp] lemma subtype_apply {s : Subring R} (x : s) : s.subtype x = x := rfl lemma subtype_injective (s : Subring R) : Function.Injective s.subtype := s.toSubmonoid.subtype_injective @[simp] theorem coe_subtype : ⇑s.subtype = ((↑) : s → R) := rfl @[norm_cast] theorem coe_natCast (n : ℕ) : ((n : s) : R) = n := rfl @[norm_cast] theorem coe_intCast (n : ℤ) : ((n : s) : R) = n := rfl /-! ## Partial order -/ @[simp] theorem coe_toSubsemiring (s : Subring R) : (s.toSubsemiring : Set R) = s := rfl theorem mem_toSubmonoid {s : Subring R} {x : R} : x ∈ s.toSubmonoid ↔ x ∈ s := Iff.rfl @[simp] theorem coe_toSubmonoid (s : Subring R) : (s.toSubmonoid : Set R) = s := rfl theorem mem_toAddSubgroup {s : Subring R} {x : R} : x ∈ s.toAddSubgroup ↔ x ∈ s := Iff.rfl @[simp] theorem coe_toAddSubgroup (s : Subring R) : (s.toAddSubgroup : Set R) = s := rfl end Subring /-- Turn a non-unital subring containing `1` into a subring. -/ def NonUnitalSubring.toSubring (S : NonUnitalSubring R) (h1 : (1 : R) ∈ S) : Subring R where __ := S one_mem' := h1 lemma Subring.toNonUnitalSubring_toSubring (S : Subring R) : S.toNonUnitalSubring.toSubring S.one_mem = S := by cases S; rfl lemma NonUnitalSubring.toSubring_toNonUnitalSubring (S : NonUnitalSubring R) (h1 : (1 : R) ∈ S) : (NonUnitalSubring.toSubring S h1).toNonUnitalSubring = S := by cases S; rfl
.lake/packages/mathlib/Mathlib/Algebra/Ring/Subring/Units.lean
import Mathlib.Algebra.Group.Subgroup.Defs import Mathlib.Algebra.Group.Submonoid.Operations import Mathlib.Algebra.Order.GroupWithZero.Submonoid import Mathlib.Algebra.Order.Ring.Defs /-! # Unit subgroups of a ring -/ /-- The subgroup of positive units of a linear ordered semiring. -/ def Units.posSubgroup (R : Type*) [Semiring R] [LinearOrder R] [IsStrictOrderedRing R] : Subgroup Rˣ := { (Submonoid.pos R).comap (Units.coeHom R) with carrier := { x | (0 : R) < x } inv_mem' := Units.inv_pos.mpr } @[simp] theorem Units.mem_posSubgroup {R : Type*} [Semiring R] [LinearOrder R] [IsStrictOrderedRing R] (u : Rˣ) : u ∈ Units.posSubgroup R ↔ (0 : R) < u := Iff.rfl
.lake/packages/mathlib/Mathlib/Algebra/Ring/Pointwise/Finset.lean
import Mathlib.Algebra.Ring.Pointwise.Set import Mathlib.Algebra.Ring.InjSurj import Mathlib.Algebra.Group.Pointwise.Finset.Basic /-! # Pointwise operations of sets in a ring This file proves properties of pointwise operations of sets in a ring. ## Tags set multiplication, set addition, pointwise addition, pointwise multiplication, pointwise subtraction -/ assert_not_exists MulAction open scoped Pointwise namespace Finset variable {α β : Type*} /-- `Finset α` has distributive negation if `α` has. -/ protected noncomputable def distribNeg [DecidableEq α] [Mul α] [HasDistribNeg α] : HasDistribNeg (Finset α) := coe_injective.hasDistribNeg _ coe_neg coe_mul scoped[Pointwise] attribute [instance] Finset.distribNeg section Distrib variable [DecidableEq α] [Distrib α] (s t u : Finset α) /-! Note that `Finset α` is not a `Distrib` because `s * t + s * u` has cross terms that `s * (t + u)` lacks. ```lean -- {10, 16, 18, 20, 8, 9} #eval {1, 2} * ({3, 4} + {5, 6} : Finset ℕ) -- {10, 11, 12, 13, 14, 15, 16, 18, 20, 8, 9} #eval ({1, 2} : Finset ℕ) * {3, 4} + {1, 2} * {5, 6} ``` -/ lemma mul_add_subset : s * (t + u) ⊆ s * t + s * u := image₂_distrib_subset_left mul_add lemma add_mul_subset : (s + t) * u ⊆ s * u + t * u := image₂_distrib_subset_right add_mul end Distrib end Finset
.lake/packages/mathlib/Mathlib/Algebra/Ring/Pointwise/Set.lean
import Mathlib.Algebra.Ring.Defs import Mathlib.Algebra.Group.Pointwise.Set.Basic /-! # Pointwise operations of sets in a ring This file proves properties of pointwise operations of sets in a ring. ## Tags set multiplication, set addition, pointwise addition, pointwise multiplication, pointwise subtraction -/ assert_not_exists MulAction IsOrderedMonoid Field open Function open scoped Pointwise variable {α : Type*} namespace Set /-- `Set α` has distributive negation if `α` has. -/ protected noncomputable def hasDistribNeg [Mul α] [HasDistribNeg α] : HasDistribNeg (Set α) where __ := Set.involutiveNeg neg_mul _ _ := by simp_rw [← image_neg_eq_neg]; exact image2_image_left_comm neg_mul mul_neg _ _ := by simp_rw [← image_neg_eq_neg]; exact image_image2_right_comm mul_neg scoped[Pointwise] attribute [instance] Set.hasDistribNeg section Distrib variable [Distrib α] (s t u : Set α) /-! Note that `Set α` is not a `Distrib` because `s * t + s * u` has cross terms that `s * (t + u)` lacks. -/ lemma mul_add_subset : s * (t + u) ⊆ s * t + s * u := image2_distrib_subset_left mul_add lemma add_mul_subset : (s + t) * u ⊆ s * u + t * u := image2_distrib_subset_right add_mul end Distrib end Set
.lake/packages/mathlib/Mathlib/Algebra/Ring/Subsemiring/MulOpposite.lean
import Mathlib.Algebra.Group.Submonoid.MulOpposite import Mathlib.Algebra.Ring.Subsemiring.Basic import Mathlib.Algebra.Ring.Opposite /-! # Subsemiring of opposite semirings For every semiring `R`, we construct an equivalence between subsemirings of `R` and that of `Rᵐᵒᵖ`. -/ namespace Subsemiring variable {ι : Sort*} {R : Type*} [NonAssocSemiring R] /-- Pull a subsemiring back to an opposite subsemiring along `MulOpposite.unop` -/ @[simps! coe toSubmonoid] protected def op (S : Subsemiring R) : Subsemiring Rᵐᵒᵖ where toSubmonoid := S.toSubmonoid.op add_mem' {x} {y} hx hy := add_mem (show x.unop ∈ S from hx) (show y.unop ∈ S from hy) zero_mem' := zero_mem S attribute [norm_cast] coe_op @[simp] theorem mem_op {x : Rᵐᵒᵖ} {S : Subsemiring R} : x ∈ S.op ↔ x.unop ∈ S := Iff.rfl /-- Pull an opposite subsemiring back to a subsemiring along `MulOpposite.op` -/ @[simps! coe toSubmonoid] protected def unop (S : Subsemiring Rᵐᵒᵖ) : Subsemiring R where toSubmonoid := S.toSubmonoid.unop add_mem' {x} {y} hx hy := add_mem (show MulOpposite.op x ∈ S from hx) (show MulOpposite.op y ∈ S from hy) zero_mem' := zero_mem S attribute [norm_cast] coe_unop @[simp] theorem mem_unop {x : R} {S : Subsemiring Rᵐᵒᵖ} : x ∈ S.unop ↔ MulOpposite.op x ∈ S := Iff.rfl @[simp] theorem unop_op (S : Subsemiring R) : S.op.unop = S := rfl @[simp] theorem op_unop (S : Subsemiring Rᵐᵒᵖ) : S.unop.op = S := rfl /-! ### Lattice results -/ theorem op_le_iff {S₁ : Subsemiring R} {S₂ : Subsemiring Rᵐᵒᵖ} : S₁.op ≤ S₂ ↔ S₁ ≤ S₂.unop := MulOpposite.op_surjective.forall theorem le_op_iff {S₁ : Subsemiring Rᵐᵒᵖ} {S₂ : Subsemiring R} : S₁ ≤ S₂.op ↔ S₁.unop ≤ S₂ := MulOpposite.op_surjective.forall @[simp] theorem op_le_op_iff {S₁ S₂ : Subsemiring R} : S₁.op ≤ S₂.op ↔ S₁ ≤ S₂ := MulOpposite.op_surjective.forall @[simp] theorem unop_le_unop_iff {S₁ S₂ : Subsemiring Rᵐᵒᵖ} : S₁.unop ≤ S₂.unop ↔ S₁ ≤ S₂ := MulOpposite.unop_surjective.forall /-- A subsemiring `S` of `R` determines a subsemiring `S.op` of the opposite ring `Rᵐᵒᵖ`. -/ @[simps] def opEquiv : Subsemiring R ≃o Subsemiring Rᵐᵒᵖ where toFun := Subsemiring.op invFun := Subsemiring.unop left_inv := unop_op right_inv := op_unop map_rel_iff' := op_le_op_iff theorem op_injective : (@Subsemiring.op R _).Injective := opEquiv.injective theorem unop_injective : (@Subsemiring.unop R _).Injective := opEquiv.symm.injective @[simp] theorem op_inj {S T : Subsemiring R} : S.op = T.op ↔ S = T := opEquiv.eq_iff_eq @[simp] theorem unop_inj {S T : Subsemiring Rᵐᵒᵖ} : S.unop = T.unop ↔ S = T := opEquiv.symm.eq_iff_eq @[simp] theorem op_bot : (⊥ : Subsemiring R).op = ⊥ := opEquiv.map_bot @[simp] theorem op_eq_bot {S : Subsemiring R} : S.op = ⊥ ↔ S = ⊥ := op_injective.eq_iff' op_bot @[simp] theorem unop_bot : (⊥ : Subsemiring Rᵐᵒᵖ).unop = ⊥ := opEquiv.symm.map_bot @[simp] theorem unop_eq_bot {S : Subsemiring Rᵐᵒᵖ} : S.unop = ⊥ ↔ S = ⊥ := unop_injective.eq_iff' unop_bot @[simp] theorem op_top : (⊤ : Subsemiring R).op = ⊤ := rfl @[simp] theorem op_eq_top {S : Subsemiring R} : S.op = ⊤ ↔ S = ⊤ := op_injective.eq_iff' op_top @[simp] theorem unop_top : (⊤ : Subsemiring Rᵐᵒᵖ).unop = ⊤ := rfl @[simp] theorem unop_eq_top {S : Subsemiring Rᵐᵒᵖ} : S.unop = ⊤ ↔ S = ⊤ := unop_injective.eq_iff' unop_top theorem op_sup (S₁ S₂ : Subsemiring R) : (S₁ ⊔ S₂).op = S₁.op ⊔ S₂.op := opEquiv.map_sup _ _ theorem unop_sup (S₁ S₂ : Subsemiring Rᵐᵒᵖ) : (S₁ ⊔ S₂).unop = S₁.unop ⊔ S₂.unop := opEquiv.symm.map_sup _ _ theorem op_inf (S₁ S₂ : Subsemiring R) : (S₁ ⊓ S₂).op = S₁.op ⊓ S₂.op := rfl theorem unop_inf (S₁ S₂ : Subsemiring Rᵐᵒᵖ) : (S₁ ⊓ S₂).unop = S₁.unop ⊓ S₂.unop := rfl theorem op_sSup (S : Set (Subsemiring R)) : (sSup S).op = sSup (.unop ⁻¹' S) := opEquiv.map_sSup_eq_sSup_symm_preimage _ theorem unop_sSup (S : Set (Subsemiring Rᵐᵒᵖ)) : (sSup S).unop = sSup (.op ⁻¹' S) := opEquiv.symm.map_sSup_eq_sSup_symm_preimage _ theorem op_sInf (S : Set (Subsemiring R)) : (sInf S).op = sInf (.unop ⁻¹' S) := opEquiv.map_sInf_eq_sInf_symm_preimage _ theorem unop_sInf (S : Set (Subsemiring Rᵐᵒᵖ)) : (sInf S).unop = sInf (.op ⁻¹' S) := opEquiv.symm.map_sInf_eq_sInf_symm_preimage _ theorem op_iSup (S : ι → Subsemiring R) : (iSup S).op = ⨆ i, (S i).op := opEquiv.map_iSup _ theorem unop_iSup (S : ι → Subsemiring Rᵐᵒᵖ) : (iSup S).unop = ⨆ i, (S i).unop := opEquiv.symm.map_iSup _ theorem op_iInf (S : ι → Subsemiring R) : (iInf S).op = ⨅ i, (S i).op := opEquiv.map_iInf _ theorem unop_iInf (S : ι → Subsemiring Rᵐᵒᵖ) : (iInf S).unop = ⨅ i, (S i).unop := opEquiv.symm.map_iInf _ theorem op_closure (s : Set R) : (closure s).op = closure (MulOpposite.unop ⁻¹' s) := by simp_rw [closure, op_sInf, Set.preimage_setOf_eq, coe_unop] congr with a exact MulOpposite.unop_surjective.forall theorem unop_closure (s : Set Rᵐᵒᵖ) : (closure s).unop = closure (MulOpposite.op ⁻¹' s) := by rw [← op_inj, op_unop, op_closure] simp_rw [Set.preimage_preimage, MulOpposite.op_unop, Set.preimage_id'] /-- Bijection between a subsemiring `S` and its opposite. -/ @[simps!] def addEquivOp (S : Subsemiring R) : S ≃+ S.op where toEquiv := S.toSubmonoid.equivOp map_add' _ _ := rfl -- TODO: Add this for `[Add]Submonoid` and `[Add]Subgroup` /-- Bijection between a subsemiring `S` and `MulOpposite` of its opposite. -/ @[simps!] def ringEquivOpMop (S : Subsemiring R) : S ≃+* (S.op)ᵐᵒᵖ where __ := S.addEquivOp.trans MulOpposite.opAddEquiv map_mul' _ _ := rfl -- TODO: Add this for `[Add]Submonoid` and `[Add]Subgroup` /-- Bijection between `MulOpposite` of a subsemiring `S` and its opposite. -/ @[simps!] def mopRingEquivOp (S : Subsemiring R) : Sᵐᵒᵖ ≃+* S.op where __ := MulOpposite.opAddEquiv.symm.trans S.addEquivOp map_mul' _ _ := rfl end Subsemiring
.lake/packages/mathlib/Mathlib/Algebra/Ring/Subsemiring/Pointwise.lean
import Mathlib.Algebra.GroupWithZero.Submonoid.Pointwise import Mathlib.Algebra.Ring.Subsemiring.Basic /-! # Pointwise instances on `Subsemiring`s This file provides the action `Subsemiring.PointwiseMulAction` which matches the action of `MulActionSet`. This actions is available in the `Pointwise` locale. ## Implementation notes This file is almost identical to the files `Mathlib/Algebra/GroupWithZero/Submonoid/Pointwise.lean` and `Mathlib/Algebra/Ring/Submonoid/Pointwise.lean`. Where possible, try to keep them in sync. -/ open Set variable {M R : Type*} namespace Subsemiring section Monoid variable [Monoid M] [Semiring R] [MulSemiringAction M R] /-- The action on a subsemiring corresponding to applying the action to every element. This is available as an instance in the `Pointwise` locale. -/ protected def pointwiseMulAction : MulAction M (Subsemiring R) where smul a S := S.map (MulSemiringAction.toRingHom _ _ a) one_smul S := (congr_arg (fun f => S.map f) (RingHom.ext <| one_smul M)).trans S.map_id mul_smul _a₁ _a₂ S := (congr_arg (fun f => S.map f) (RingHom.ext <| mul_smul _ _)).trans (S.map_map _ _).symm scoped[Pointwise] attribute [instance] Subsemiring.pointwiseMulAction open Pointwise theorem pointwise_smul_def {a : M} (S : Subsemiring R) : a • S = S.map (MulSemiringAction.toRingHom _ _ a) := rfl @[simp, norm_cast] theorem coe_pointwise_smul (m : M) (S : Subsemiring R) : ↑(m • S) = m • (S : Set R) := rfl @[simp] theorem pointwise_smul_toAddSubmonoid (m : M) (S : Subsemiring R) : (m • S).toAddSubmonoid = m • S.toAddSubmonoid := rfl theorem smul_mem_pointwise_smul (m : M) (r : R) (S : Subsemiring R) : r ∈ S → m • r ∈ m • S := (Set.smul_mem_smul_set : _ → _ ∈ m • (S : Set R)) instance : CovariantClass M (Subsemiring R) HSMul.hSMul LE.le := ⟨fun _ _ => image_mono⟩ theorem mem_smul_pointwise_iff_exists (m : M) (r : R) (S : Subsemiring R) : r ∈ m • S ↔ ∃ s : R, s ∈ S ∧ m • s = r := (Set.mem_smul_set : r ∈ m • (S : Set R) ↔ _) @[simp] theorem smul_bot (a : M) : a • (⊥ : Subsemiring R) = ⊥ := map_bot _ theorem smul_sup (a : M) (S T : Subsemiring R) : a • (S ⊔ T) = a • S ⊔ a • T := map_sup _ _ _ theorem smul_closure (a : M) (s : Set R) : a • closure s = closure (a • s) := RingHom.map_closureS _ _ instance pointwise_central_scalar [MulSemiringAction Mᵐᵒᵖ R] [IsCentralScalar M R] : IsCentralScalar M (Subsemiring R) := ⟨fun _a S => (congr_arg fun f => S.map f) <| RingHom.ext <| op_smul_eq_smul _⟩ end Monoid section Group variable [Group M] [Semiring R] [MulSemiringAction M R] open Pointwise @[simp] theorem smul_mem_pointwise_smul_iff {a : M} {S : Subsemiring R} {x : R} : a • x ∈ a • S ↔ x ∈ S := smul_mem_smul_set_iff theorem mem_pointwise_smul_iff_inv_smul_mem {a : M} {S : Subsemiring R} {x : R} : x ∈ a • S ↔ a⁻¹ • x ∈ S := mem_smul_set_iff_inv_smul_mem theorem mem_inv_pointwise_smul_iff {a : M} {S : Subsemiring R} {x : R} : x ∈ a⁻¹ • S ↔ a • x ∈ S := mem_inv_smul_set_iff @[simp] theorem pointwise_smul_le_pointwise_smul_iff {a : M} {S T : Subsemiring R} : a • S ≤ a • T ↔ S ≤ T := smul_set_subset_smul_set_iff theorem pointwise_smul_subset_iff {a : M} {S T : Subsemiring R} : a • S ≤ T ↔ S ≤ a⁻¹ • T := smul_set_subset_iff_subset_inv_smul_set theorem subset_pointwise_smul_iff {a : M} {S T : Subsemiring R} : S ≤ a • T ↔ a⁻¹ • S ≤ T := subset_smul_set_iff /-! TODO: add `equiv_smul` like we have for subgroup. -/ end Group section GroupWithZero variable [GroupWithZero M] [Semiring R] [MulSemiringAction M R] open Pointwise @[simp] theorem smul_mem_pointwise_smul_iff₀ {a : M} (ha : a ≠ 0) (S : Subsemiring R) (x : R) : a • x ∈ a • S ↔ x ∈ S := smul_mem_smul_set_iff₀ ha (S : Set R) x theorem mem_pointwise_smul_iff_inv_smul_mem₀ {a : M} (ha : a ≠ 0) (S : Subsemiring R) (x : R) : x ∈ a • S ↔ a⁻¹ • x ∈ S := mem_smul_set_iff_inv_smul_mem₀ ha (S : Set R) x theorem mem_inv_pointwise_smul_iff₀ {a : M} (ha : a ≠ 0) (S : Subsemiring R) (x : R) : x ∈ a⁻¹ • S ↔ a • x ∈ S := mem_inv_smul_set_iff₀ ha (S : Set R) x @[simp] theorem pointwise_smul_le_pointwise_smul_iff₀ {a : M} (ha : a ≠ 0) {S T : Subsemiring R} : a • S ≤ a • T ↔ S ≤ T := smul_set_subset_smul_set_iff₀ ha theorem pointwise_smul_le_iff₀ {a : M} (ha : a ≠ 0) {S T : Subsemiring R} : a • S ≤ T ↔ S ≤ a⁻¹ • T := smul_set_subset_iff₀ ha theorem le_pointwise_smul_iff₀ {a : M} (ha : a ≠ 0) {S T : Subsemiring R} : S ≤ a • T ↔ a⁻¹ • S ≤ T := subset_smul_set_iff₀ ha end GroupWithZero end Subsemiring
.lake/packages/mathlib/Mathlib/Algebra/Ring/Subsemiring/Order.lean
import Mathlib.Algebra.Order.Ring.InjSurj import Mathlib.Algebra.Ring.Subsemiring.Defs import Mathlib.Order.Interval.Set.Defs import Mathlib.Tactic.FastInstance /-! # `Order`ed instances for `SubsemiringClass` and `Subsemiring`. -/ namespace SubsemiringClass variable {R S : Type*} [SetLike S R] (s : S) /-- A subsemiring of an ordered semiring is an ordered semiring. -/ instance toIsOrderedRing [Semiring R] [PartialOrder R] [IsOrderedRing R] [SubsemiringClass S R] : IsOrderedRing s := Function.Injective.isOrderedRing Subtype.val rfl rfl (fun _ _ => rfl) (fun _ _ => rfl) .rfl /-- A subsemiring of a strict ordered semiring is a strict ordered semiring. -/ instance toIsStrictOrderedRing [Semiring R] [PartialOrder R] [IsStrictOrderedRing R] [SubsemiringClass S R] : IsStrictOrderedRing s := Function.Injective.isStrictOrderedRing Subtype.val rfl rfl (fun _ _ => rfl) (fun _ _ => rfl) .rfl .rfl end SubsemiringClass namespace Subsemiring variable {R : Type*} /-- A subsemiring of an ordered semiring is an ordered semiring. -/ instance toIsOrderedRing [Semiring R] [PartialOrder R] [IsOrderedRing R] (s : Subsemiring R) : IsOrderedRing s := SubsemiringClass.toIsOrderedRing _ /-- A subsemiring of a strict ordered semiring is a strict ordered semiring. -/ instance toIsStrictOrderedRing [Semiring R] [PartialOrder R] [IsStrictOrderedRing R] (s : Subsemiring R) : IsStrictOrderedRing s := SubsemiringClass.toIsStrictOrderedRing _ section nonneg variable [Semiring R] [PartialOrder R] [IsOrderedRing R] variable (R) in /-- The set of nonnegative elements in an ordered semiring, as a subsemiring. -/ @[simps] def nonneg : Subsemiring R where carrier := Set.Ici 0 mul_mem' := mul_nonneg one_mem' := zero_le_one add_mem' := add_nonneg zero_mem' := le_rfl @[simp] lemma mem_nonneg {x : R} : x ∈ nonneg R ↔ 0 ≤ x := .rfl end nonneg end Subsemiring
.lake/packages/mathlib/Mathlib/Algebra/Ring/Subsemiring/Basic.lean
import Mathlib.Algebra.Group.Submonoid.BigOperators import Mathlib.Algebra.Ring.Action.Subobjects import Mathlib.Algebra.Ring.Equiv import Mathlib.Algebra.Ring.Prod import Mathlib.Algebra.Ring.Subsemiring.Defs import Mathlib.GroupTheory.Submonoid.Centralizer import Mathlib.RingTheory.NonUnitalSubsemiring.Basic import Mathlib.Algebra.Module.Defs /-! # Bundled subsemirings We define some standard constructions on bundled subsemirings: `CompleteLattice` structure, subsemiring `map`, `comap` and range (`rangeS`) of a `RingHom` etc. -/ universe u v w variable {R : Type u} {S : Type v} {T : Type w} [NonAssocSemiring R] (M : Submonoid R) section SubsemiringClass variable [SetLike S R] [hSR : SubsemiringClass S R] (s : S) namespace SubsemiringClass instance instCharZero [CharZero R] : CharZero s := ⟨Function.Injective.of_comp (f := Subtype.val) (g := Nat.cast (R := s)) Nat.cast_injective⟩ end SubsemiringClass end SubsemiringClass variable [NonAssocSemiring S] [NonAssocSemiring T] namespace Subsemiring variable (s : Subsemiring R) @[mono] theorem toSubmonoid_strictMono : StrictMono (toSubmonoid : Subsemiring R → Submonoid R) := fun _ _ => id @[mono] theorem toSubmonoid_mono : Monotone (toSubmonoid : Subsemiring R → Submonoid R) := toSubmonoid_strictMono.monotone @[mono] theorem toAddSubmonoid_strictMono : StrictMono (toAddSubmonoid : Subsemiring R → AddSubmonoid R) := fun _ _ => id @[mono] theorem toAddSubmonoid_mono : Monotone (toAddSubmonoid : Subsemiring R → AddSubmonoid R) := toAddSubmonoid_strictMono.monotone /-- Product of a list of elements in a `Subsemiring` is in the `Subsemiring`. -/ nonrec theorem list_prod_mem {R : Type*} [Semiring R] (s : Subsemiring R) {l : List R} : (∀ x ∈ l, x ∈ s) → l.prod ∈ s := list_prod_mem /-- Sum of a list of elements in a `Subsemiring` is in the `Subsemiring`. -/ protected theorem list_sum_mem {l : List R} : (∀ x ∈ l, x ∈ s) → l.sum ∈ s := list_sum_mem /-- Product of a multiset of elements in a `Subsemiring` of a `CommSemiring` is in the `Subsemiring`. -/ protected theorem multiset_prod_mem {R} [CommSemiring R] (s : Subsemiring R) (m : Multiset R) : (∀ a ∈ m, a ∈ s) → m.prod ∈ s := multiset_prod_mem m /-- Sum of a multiset of elements in a `Subsemiring` of a `NonAssocSemiring` is in the `Subsemiring`. -/ protected theorem multiset_sum_mem (m : Multiset R) : (∀ a ∈ m, a ∈ s) → m.sum ∈ s := multiset_sum_mem m /-- Product of elements of a subsemiring of a `CommSemiring` indexed by a `Finset` is in the `Subsemiring`. -/ protected theorem prod_mem {R : Type*} [CommSemiring R] (s : Subsemiring R) {ι : Type*} {t : Finset ι} {f : ι → R} (h : ∀ c ∈ t, f c ∈ s) : (∏ i ∈ t, f i) ∈ s := prod_mem h /-- Sum of elements in a `Subsemiring` of a `NonAssocSemiring` indexed by a `Finset` is in the `Subsemiring`. -/ protected theorem sum_mem (s : Subsemiring R) {ι : Type*} {t : Finset ι} {f : ι → R} (h : ∀ c ∈ t, f c ∈ s) : (∑ i ∈ t, f i) ∈ s := sum_mem h /-- The ring equiv between the top element of `Subsemiring R` and `R`. -/ @[simps] def topEquiv : (⊤ : Subsemiring R) ≃+* R where toFun r := r invFun r := ⟨r, Subsemiring.mem_top r⟩ map_mul' := (⊤ : Subsemiring R).coe_mul map_add' := (⊤ : Subsemiring R).coe_add /-- The preimage of a subsemiring along a ring homomorphism is a subsemiring. -/ @[simps coe toSubmonoid] def comap (f : R →+* S) (s : Subsemiring S) : Subsemiring R := { s.toSubmonoid.comap (f : R →* S), s.toAddSubmonoid.comap (f : R →+ S) with carrier := f ⁻¹' s } @[simp] theorem mem_comap {s : Subsemiring S} {f : R →+* S} {x : R} : x ∈ s.comap f ↔ f x ∈ s := Iff.rfl theorem comap_comap (s : Subsemiring T) (g : S →+* T) (f : R →+* S) : (s.comap g).comap f = s.comap (g.comp f) := rfl /-- The image of a subsemiring along a ring homomorphism is a subsemiring. -/ @[simps coe toSubmonoid] def map (f : R →+* S) (s : Subsemiring R) : Subsemiring S := { s.toSubmonoid.map (f : R →* S), s.toAddSubmonoid.map (f : R →+ S) with carrier := f '' s } @[simp] lemma mem_map {f : R →+* S} {s : Subsemiring R} {y : S} : y ∈ s.map f ↔ ∃ x ∈ s, f x = y := Iff.rfl @[simp] theorem map_id : s.map (RingHom.id R) = s := SetLike.coe_injective <| Set.image_id _ theorem map_map (g : S →+* T) (f : R →+* S) : (s.map f).map g = s.map (g.comp f) := SetLike.coe_injective <| Set.image_image _ _ _ theorem map_le_iff_le_comap {f : R →+* S} {s : Subsemiring R} {t : Subsemiring S} : s.map f ≤ t ↔ s ≤ t.comap f := Set.image_subset_iff theorem gc_map_comap (f : R →+* S) : GaloisConnection (map f) (comap f) := fun _ _ => map_le_iff_le_comap /-- A subsemiring is isomorphic to its image under an injective function -/ noncomputable def equivMapOfInjective (f : R →+* S) (hf : Function.Injective f) : s ≃+* s.map f := { Equiv.Set.image f s hf with map_mul' := fun _ _ => Subtype.ext (f.map_mul _ _) map_add' := fun _ _ => Subtype.ext (f.map_add _ _) } @[simp] theorem coe_equivMapOfInjective_apply (f : R →+* S) (hf : Function.Injective f) (x : s) : (equivMapOfInjective s f hf x : S) = f x := rfl end Subsemiring namespace RingHom variable (g : S →+* T) (f : R →+* S) /-- The range of a ring homomorphism is a subsemiring. See Note [range copy pattern]. -/ @[simps! coe toSubmonoid] def rangeS : Subsemiring S := ((⊤ : Subsemiring R).map f).copy (Set.range f) Set.image_univ.symm @[simp] theorem mem_rangeS {f : R →+* S} {y : S} : y ∈ f.rangeS ↔ ∃ x, f x = y := Iff.rfl theorem rangeS_eq_map (f : R →+* S) : f.rangeS = (⊤ : Subsemiring R).map f := by ext simp theorem mem_rangeS_self (f : R →+* S) (x : R) : f x ∈ f.rangeS := mem_rangeS.mpr ⟨x, rfl⟩ theorem map_rangeS : f.rangeS.map g = (g.comp f).rangeS := by simpa only [rangeS_eq_map] using (⊤ : Subsemiring R).map_map g f /-- The range of a morphism of semirings is a fintype, if the domain is a fintype. Note: this instance can form a diamond with `Subtype.fintype` in the presence of `Fintype S`. -/ instance fintypeRangeS [Fintype R] [DecidableEq S] (f : R →+* S) : Fintype (rangeS f) := Set.fintypeRange f end RingHom namespace Subsemiring instance : Bot (Subsemiring R) := ⟨(Nat.castRingHom R).rangeS⟩ instance : Inhabited (Subsemiring R) := ⟨⊥⟩ @[norm_cast] theorem coe_bot : ((⊥ : Subsemiring R) : Set R) = Set.range ((↑) : ℕ → R) := (Nat.castRingHom R).coe_rangeS theorem mem_bot {x : R} : x ∈ (⊥ : Subsemiring R) ↔ ∃ n : ℕ, ↑n = x := RingHom.mem_rangeS instance : InfSet (Subsemiring R) := ⟨fun s => Subsemiring.mk' (⋂ t ∈ s, ↑t) (⨅ t ∈ s, Subsemiring.toSubmonoid t) (by simp) (⨅ t ∈ s, Subsemiring.toAddSubmonoid t) (by simp)⟩ @[simp, norm_cast] theorem coe_sInf (S : Set (Subsemiring R)) : ((sInf S : Subsemiring R) : Set R) = ⋂ s ∈ S, ↑s := rfl theorem mem_sInf {S : Set (Subsemiring R)} {x : R} : x ∈ sInf S ↔ ∀ p ∈ S, x ∈ p := Set.mem_iInter₂ @[simp, norm_cast] theorem coe_iInf {ι : Sort*} {S : ι → Subsemiring R} : (↑(⨅ i, S i) : Set R) = ⋂ i, S i := by simp only [iInf, coe_sInf, Set.biInter_range] theorem mem_iInf {ι : Sort*} {S : ι → Subsemiring R} {x : R} : (x ∈ ⨅ i, S i) ↔ ∀ i, x ∈ S i := by simp only [iInf, mem_sInf, Set.forall_mem_range] @[simp] theorem sInf_toSubmonoid (s : Set (Subsemiring R)) : (sInf s).toSubmonoid = ⨅ t ∈ s, Subsemiring.toSubmonoid t := mk'_toSubmonoid _ _ @[simp] theorem sInf_toAddSubmonoid (s : Set (Subsemiring R)) : (sInf s).toAddSubmonoid = ⨅ t ∈ s, Subsemiring.toAddSubmonoid t := mk'_toAddSubmonoid _ _ /-- Subsemirings of a semiring form a complete lattice. -/ instance : CompleteLattice (Subsemiring R) := { completeLatticeOfInf (Subsemiring R) fun _ => IsGLB.of_image (fun {s t : Subsemiring R} => show (s : Set R) ⊆ t ↔ s ≤ t from SetLike.coe_subset_coe) isGLB_biInf with bot := ⊥ bot_le := fun s _ hx => let ⟨n, hn⟩ := mem_bot.1 hx hn ▸ natCast_mem s n top := ⊤ le_top := fun _ _ _ => mem_top _ inf := (· ⊓ ·) inf_le_left := fun _ _ _ => And.left inf_le_right := fun _ _ _ => And.right le_inf := fun _ _ _ h₁ h₂ _ hx => ⟨h₁ hx, h₂ hx⟩ } theorem eq_top_iff' (A : Subsemiring R) : A = ⊤ ↔ ∀ x : R, x ∈ A := eq_top_iff.trans ⟨fun h m => h <| mem_top m, fun h m _ => h m⟩ section NonAssocSemiring variable (R) /-- The center of a non-associative semiring `R` is the set of elements that commute and associate with everything in `R` -/ @[simps coe toSubmonoid] def center : Subsemiring R := { NonUnitalSubsemiring.center R with one_mem' := Set.one_mem_center } /-- The center is commutative and associative. This is not an instance as it forms a non-defeq diamond with `NonUnitalSubringClass.toNonUnitalRing` in the `npow` field. -/ abbrev center.commSemiring' : CommSemiring (center R) := { Submonoid.center.commMonoid', (center R).toNonAssocSemiring with } variable {R} /-- The center of isomorphic (not necessarily associative) semirings are isomorphic. -/ @[simps!] def centerCongr [NonAssocSemiring S] (e : R ≃+* S) : center R ≃+* center S := NonUnitalSubsemiring.centerCongr e /-- The center of a (not necessarily associative) semiring is isomorphic to the center of its opposite. -/ @[simps!] def centerToMulOpposite : center R ≃+* center Rᵐᵒᵖ := NonUnitalSubsemiring.centerToMulOpposite end NonAssocSemiring section Semiring /-- The center is commutative. -/ instance center.commSemiring {R} [Semiring R] : CommSemiring (center R) := { Submonoid.center.commMonoid, (center R).toSemiring with } -- no instance diamond, unlike the primed version example {R} [Semiring R] : center.commSemiring.toSemiring = Subsemiring.toSemiring (center R) := by with_reducible_and_instances rfl theorem mem_center_iff {R} [Semiring R] {z : R} : z ∈ center R ↔ ∀ g, g * z = z * g := Subsemigroup.mem_center_iff instance decidableMemCenter {R} [Semiring R] [DecidableEq R] [Fintype R] : DecidablePred (· ∈ center R) := fun _ => decidable_of_iff' _ mem_center_iff @[simp] theorem center_eq_top (R) [CommSemiring R] : center R = ⊤ := SetLike.coe_injective (Set.center_eq_univ R) end Semiring section Centralizer /-- The centralizer of a set as subsemiring. -/ def centralizer {R} [Semiring R] (s : Set R) : Subsemiring R := { Submonoid.centralizer s with carrier := s.centralizer zero_mem' := Set.zero_mem_centralizer add_mem' := Set.add_mem_centralizer } @[simp, norm_cast] theorem coe_centralizer {R} [Semiring R] (s : Set R) : (centralizer s : Set R) = s.centralizer := rfl theorem centralizer_toSubmonoid {R} [Semiring R] (s : Set R) : (centralizer s).toSubmonoid = Submonoid.centralizer s := rfl theorem mem_centralizer_iff {R} [Semiring R] {s : Set R} {z : R} : z ∈ centralizer s ↔ ∀ g ∈ s, g * z = z * g := Iff.rfl theorem center_le_centralizer {R} [Semiring R] (s) : center R ≤ centralizer s := s.center_subset_centralizer theorem centralizer_le {R} [Semiring R] (s t : Set R) (h : s ⊆ t) : centralizer t ≤ centralizer s := Set.centralizer_subset h @[simp] theorem centralizer_eq_top_iff_subset {R} [Semiring R] {s : Set R} : centralizer s = ⊤ ↔ s ⊆ center R := SetLike.ext'_iff.trans Set.centralizer_eq_top_iff_subset @[simp] theorem centralizer_univ {R} [Semiring R] : centralizer Set.univ = center R := SetLike.ext' (Set.centralizer_univ R) lemma le_centralizer_centralizer {R} [Semiring R] {s : Subsemiring R} : s ≤ centralizer (centralizer (s : Set R)) := Set.subset_centralizer_centralizer @[simp] lemma centralizer_centralizer_centralizer {R} [Semiring R] {s : Set R} : centralizer s.centralizer.centralizer = centralizer s := by apply SetLike.coe_injective simp only [coe_centralizer, Set.centralizer_centralizer_centralizer] end Centralizer /-- The `Subsemiring` generated by a set. -/ def closure (s : Set R) : Subsemiring R := sInf { S | s ⊆ S } theorem mem_closure {x : R} {s : Set R} : x ∈ closure s ↔ ∀ S : Subsemiring R, s ⊆ S → x ∈ S := mem_sInf /-- The subsemiring generated by a set includes the set. -/ @[simp, aesop safe 20 (rule_sets := [SetLike])] theorem subset_closure {s : Set R} : s ⊆ closure s := fun _ hx => mem_closure.2 fun _ hS => hS hx @[aesop 80% (rule_sets := [SetLike])] theorem mem_closure_of_mem {s : Set R} {x : R} (hx : x ∈ s) : x ∈ closure s := subset_closure hx theorem notMem_of_notMem_closure {s : Set R} {P : R} (hP : P ∉ closure s) : P ∉ s := fun h => hP (subset_closure h) @[deprecated (since := "2025-05-23")] alias not_mem_of_not_mem_closure := notMem_of_notMem_closure /-- A subsemiring `S` includes `closure s` if and only if it includes `s`. -/ @[simp] theorem closure_le {s : Set R} {t : Subsemiring R} : closure s ≤ t ↔ s ⊆ t := ⟨Set.Subset.trans subset_closure, fun h => sInf_le h⟩ /-- Subsemiring closure of a set is monotone in its argument: if `s ⊆ t`, then `closure s ≤ closure t`. -/ @[gcongr] theorem closure_mono ⦃s t : Set R⦄ (h : s ⊆ t) : closure s ≤ closure t := closure_le.2 <| Set.Subset.trans h subset_closure theorem closure_eq_of_le {s : Set R} {t : Subsemiring R} (h₁ : s ⊆ t) (h₂ : t ≤ closure s) : closure s = t := le_antisymm (closure_le.2 h₁) h₂ theorem mem_map_equiv {f : R ≃+* S} {K : Subsemiring R} {x : S} : x ∈ K.map (f : R →+* S) ↔ f.symm x ∈ K := by convert @Set.mem_image_equiv _ _ (↑K) f.toEquiv x using 1 theorem map_equiv_eq_comap_symm (f : R ≃+* S) (K : Subsemiring R) : K.map (f : R →+* S) = K.comap f.symm := SetLike.coe_injective (f.toEquiv.image_eq_preimage_symm K) theorem comap_equiv_eq_map_symm (f : R ≃+* S) (K : Subsemiring S) : K.comap (f : R →+* S) = K.map f.symm := (map_equiv_eq_comap_symm f.symm K).symm end Subsemiring namespace Submonoid /-- The additive closure of a submonoid is a subsemiring. -/ def subsemiringClosure (M : Submonoid R) : Subsemiring R := { AddSubmonoid.closure (M : Set R) with one_mem' := AddSubmonoid.mem_closure.mpr fun _ hy => hy M.one_mem mul_mem' := MulMemClass.mul_mem_add_closure } theorem subsemiringClosure_coe : (M.subsemiringClosure : Set R) = AddSubmonoid.closure (M : Set R) := rfl theorem subsemiringClosure_mem {x : R} : x ∈ M.subsemiringClosure ↔ x ∈ AddSubmonoid.closure (M : Set R) := Iff.rfl theorem subsemiringClosure_toAddSubmonoid : M.subsemiringClosure.toAddSubmonoid = AddSubmonoid.closure (M : Set R) := rfl @[simp] lemma subsemiringClosure_toNonUnitalSubsemiring (M : Submonoid R) : M.subsemiringClosure.toNonUnitalSubsemiring = .closure M := by refine Eq.symm (NonUnitalSubsemiring.closure_eq_of_le ?_ fun _ hx ↦ ?_) · simp [Submonoid.subsemiringClosure_coe] · simp only [Subsemiring.mem_toNonUnitalSubsemiring, subsemiringClosure_mem] at hx induction hx using AddSubmonoid.closure_induction <;> aesop /-- The `Subsemiring` generated by a multiplicative submonoid coincides with the `Subsemiring.closure` of the submonoid itself . -/ theorem subsemiringClosure_eq_closure : M.subsemiringClosure = Subsemiring.closure (M : Set R) := by ext refine ⟨fun hx => ?_, fun hx => (Subsemiring.mem_closure.mp hx) M.subsemiringClosure fun s sM => ?_⟩ <;> rintro - ⟨H1, rfl⟩ <;> rintro - ⟨H2, rfl⟩ · exact AddSubmonoid.mem_closure.mp hx H1.toAddSubmonoid H2 · exact H2 sM end Submonoid namespace Subsemiring @[simp] theorem closure_submonoid_closure (s : Set R) : closure ↑(Submonoid.closure s) = closure s := le_antisymm (closure_le.mpr fun _ hy => (Submonoid.mem_closure.mp hy) (closure s).toSubmonoid subset_closure) (closure_mono Submonoid.subset_closure) /-- The elements of the subsemiring closure of `M` are exactly the elements of the additive closure of a multiplicative submonoid `M`. -/ theorem coe_closure_eq (s : Set R) : (closure s : Set R) = AddSubmonoid.closure (Submonoid.closure s : Set R) := by simp [← Submonoid.subsemiringClosure_toAddSubmonoid, Submonoid.subsemiringClosure_eq_closure] theorem mem_closure_iff {s : Set R} {x} : x ∈ closure s ↔ x ∈ AddSubmonoid.closure (Submonoid.closure s : Set R) := Set.ext_iff.mp (coe_closure_eq s) x @[simp] theorem closure_addSubmonoid_closure {s : Set R} : closure ↑(AddSubmonoid.closure s) = closure s := by ext x refine ⟨fun hx => ?_, fun hx => closure_mono AddSubmonoid.subset_closure hx⟩ rintro - ⟨H, rfl⟩ rintro - ⟨J, rfl⟩ refine (AddSubmonoid.mem_closure.mp (mem_closure_iff.mp hx)) H.toAddSubmonoid fun y hy => ?_ refine (Submonoid.mem_closure.mp hy) H.toSubmonoid fun z hz => ?_ exact (AddSubmonoid.mem_closure.mp hz) H.toAddSubmonoid fun w hw => J hw /-- An induction principle for closure membership. If `p` holds for `0`, `1`, and all elements of `s`, and is preserved under addition and multiplication, then `p` holds for all elements of the closure of `s`. -/ @[elab_as_elim] theorem closure_induction {s : Set R} {p : (x : R) → x ∈ closure s → Prop} (mem : ∀ (x) (hx : x ∈ s), p x (subset_closure hx)) (zero : p 0 (zero_mem _)) (one : p 1 (one_mem _)) (add : ∀ x y hx hy, p x hx → p y hy → p (x + y) (add_mem hx hy)) (mul : ∀ x y hx hy, p x hx → p y hy → p (x * y) (mul_mem hx hy)) {x} (hx : x ∈ closure s) : p x hx := let K : Subsemiring R := { carrier := { x | ∃ hx, p x hx } mul_mem' := fun ⟨_, hpx⟩ ⟨_, hpy⟩ ↦ ⟨_, mul _ _ _ _ hpx hpy⟩ add_mem' := fun ⟨_, hpx⟩ ⟨_, hpy⟩ ↦ ⟨_, add _ _ _ _ hpx hpy⟩ one_mem' := ⟨_, one⟩ zero_mem' := ⟨_, zero⟩ } closure_le (t := K) |>.mpr (fun y hy ↦ ⟨subset_closure hy, mem y hy⟩) hx |>.elim fun _ ↦ id /-- An induction principle for closure membership for predicates with two arguments. -/ @[elab_as_elim] theorem closure_induction₂ {s : Set R} {p : (x y : R) → x ∈ closure s → y ∈ closure s → Prop} (mem_mem : ∀ (x) (y) (hx : x ∈ s) (hy : y ∈ s), p x y (subset_closure hx) (subset_closure hy)) (zero_left : ∀ x hx, p 0 x (zero_mem _) hx) (zero_right : ∀ x hx, p x 0 hx (zero_mem _)) (one_left : ∀ x hx, p 1 x (one_mem _) hx) (one_right : ∀ x hx, p x 1 hx (one_mem _)) (add_left : ∀ x y z hx hy hz, p x z hx hz → p y z hy hz → p (x + y) z (add_mem hx hy) hz) (add_right : ∀ x y z hx hy hz, p x y hx hy → p x z hx hz → p x (y + z) hx (add_mem hy hz)) (mul_left : ∀ x y z hx hy hz, p x z hx hz → p y z hy hz → p (x * y) z (mul_mem hx hy) hz) (mul_right : ∀ x y z hx hy hz, p x y hx hy → p x z hx hz → p x (y * z) hx (mul_mem hy hz)) {x y : R} (hx : x ∈ closure s) (hy : y ∈ closure s) : p x y hx hy := by induction hy using closure_induction with | mem z hz => induction hx using closure_induction with | mem _ h => exact mem_mem _ _ h hz | zero => exact zero_left _ _ | one => exact one_left _ _ | mul _ _ _ _ h₁ h₂ => exact mul_left _ _ _ _ _ _ h₁ h₂ | add _ _ _ _ h₁ h₂ => exact add_left _ _ _ _ _ _ h₁ h₂ | zero => exact zero_right x hx | one => exact one_right x hx | mul _ _ _ _ h₁ h₂ => exact mul_right _ _ _ _ _ _ h₁ h₂ | add _ _ _ _ h₁ h₂ => exact add_right _ _ _ _ _ _ h₁ h₂ theorem mem_closure_iff_exists_list {R} [Semiring R] {s : Set R} {x} : x ∈ closure s ↔ ∃ L : List (List R), (∀ t ∈ L, ∀ y ∈ t, y ∈ s) ∧ (L.map List.prod).sum = x := by constructor · intro hx rw [mem_closure_iff] at hx induction hx using AddSubmonoid.closure_induction with | mem x hx => suffices ∃ t : List R, (∀ y ∈ t, y ∈ s) ∧ t.prod = x from let ⟨t, ht1, ht2⟩ := this ⟨[t], List.forall_mem_singleton.2 ht1, by rw [List.map_singleton, List.sum_singleton, ht2]⟩ induction hx using Submonoid.closure_induction with | mem x hx => exact ⟨[x], List.forall_mem_singleton.2 hx, List.prod_singleton⟩ | one => exact ⟨[], List.forall_mem_nil _, rfl⟩ | mul x y _ _ ht hu => obtain ⟨⟨t, ht1, ht2⟩, ⟨u, hu1, hu2⟩⟩ := And.intro ht hu exact ⟨t ++ u, List.forall_mem_append.2 ⟨ht1, hu1⟩, by rw [List.prod_append, ht2, hu2]⟩ | zero => exact ⟨[], List.forall_mem_nil _, rfl⟩ | add x y _ _ hL hM => obtain ⟨⟨L, HL1, HL2⟩, ⟨M, HM1, HM2⟩⟩ := And.intro hL hM exact ⟨L ++ M, List.forall_mem_append.2 ⟨HL1, HM1⟩, by rw [List.map_append, List.sum_append, HL2, HM2]⟩ · rintro ⟨L, HL1, rfl⟩ exact list_sum_mem fun r hr => let ⟨t, ht1, ht2⟩ := List.mem_map.1 hr ht2 ▸ list_prod_mem _ fun y hy => subset_closure <| HL1 t ht1 y hy variable (R) in /-- `closure` forms a Galois insertion with the coercion to set. -/ protected def gi : GaloisInsertion (@closure R _) (↑) where choice s _ := closure s gc _ _ := closure_le le_l_u _ := subset_closure choice_eq _ _ := rfl /-- Closure of a subsemiring `S` equals `S`. -/ @[simp] theorem closure_eq (s : Subsemiring R) : closure (s : Set R) = s := (Subsemiring.gi R).l_u_eq s @[simp] theorem closure_empty : closure (∅ : Set R) = ⊥ := (Subsemiring.gi R).gc.l_bot @[simp] theorem closure_univ : closure (Set.univ : Set R) = ⊤ := @coe_top R _ ▸ closure_eq ⊤ theorem closure_union (s t : Set R) : closure (s ∪ t) = closure s ⊔ closure t := (Subsemiring.gi R).gc.l_sup theorem closure_iUnion {ι} (s : ι → Set R) : closure (⋃ i, s i) = ⨆ i, closure (s i) := (Subsemiring.gi R).gc.l_iSup theorem closure_sUnion (s : Set (Set R)) : closure (⋃₀ s) = ⨆ t ∈ s, closure t := (Subsemiring.gi R).gc.l_sSup @[simp] theorem closure_singleton_natCast (n : ℕ) : closure {(n : R)} = ⊥ := bot_unique <| closure_le.2 <| Set.singleton_subset_iff.mpr <| natCast_mem _ _ @[simp] theorem closure_singleton_zero : closure {(0 : R)} = ⊥ := mod_cast closure_singleton_natCast 0 @[simp] theorem closure_singleton_one : closure {(1 : R)} = ⊥ := mod_cast closure_singleton_natCast 1 @[simp] theorem closure_insert_natCast (n : ℕ) (s : Set R) : closure (insert (n : R) s) = closure s := by rw [Set.insert_eq, closure_union] simp @[simp] theorem closure_insert_zero (s : Set R) : closure (insert 0 s) = closure s := mod_cast closure_insert_natCast 0 s @[simp] theorem closure_insert_one (s : Set R) : closure (insert 1 s) = closure s := mod_cast closure_insert_natCast 1 s theorem map_sup (s t : Subsemiring R) (f : R →+* S) : (s ⊔ t).map f = s.map f ⊔ t.map f := (gc_map_comap f).l_sup theorem map_iSup {ι : Sort*} (f : R →+* S) (s : ι → Subsemiring R) : (iSup s).map f = ⨆ i, (s i).map f := (gc_map_comap f).l_iSup theorem map_inf (s t : Subsemiring R) (f : R →+* S) (hf : Function.Injective f) : (s ⊓ t).map f = s.map f ⊓ t.map f := SetLike.coe_injective (Set.image_inter hf) theorem map_iInf {ι : Sort*} [Nonempty ι] (f : R →+* S) (hf : Function.Injective f) (s : ι → Subsemiring R) : (iInf s).map f = ⨅ i, (s i).map f := by apply SetLike.coe_injective simpa using (Set.injOn_of_injective hf).image_iInter_eq (s := SetLike.coe ∘ s) theorem comap_inf (s t : Subsemiring S) (f : R →+* S) : (s ⊓ t).comap f = s.comap f ⊓ t.comap f := (gc_map_comap f).u_inf theorem comap_iInf {ι : Sort*} (f : R →+* S) (s : ι → Subsemiring S) : (iInf s).comap f = ⨅ i, (s i).comap f := (gc_map_comap f).u_iInf @[simp] theorem map_bot (f : R →+* S) : (⊥ : Subsemiring R).map f = ⊥ := (gc_map_comap f).l_bot @[simp] theorem comap_top (f : R →+* S) : (⊤ : Subsemiring S).comap f = ⊤ := (gc_map_comap f).u_top /-- Given `Subsemiring`s `s`, `t` of semirings `R`, `S` respectively, `s.prod t` is `s × t` as a subsemiring of `R × S`. -/ def prod (s : Subsemiring R) (t : Subsemiring S) : Subsemiring (R × S) := { s.toSubmonoid.prod t.toSubmonoid, s.toAddSubmonoid.prod t.toAddSubmonoid with carrier := s ×ˢ t } @[norm_cast] theorem coe_prod (s : Subsemiring R) (t : Subsemiring S) : (s.prod t : Set (R × S)) = (s : Set R) ×ˢ (t : Set S) := rfl theorem mem_prod {s : Subsemiring R} {t : Subsemiring S} {p : R × S} : p ∈ s.prod t ↔ p.1 ∈ s ∧ p.2 ∈ t := Iff.rfl @[gcongr, mono] theorem prod_mono ⦃s₁ s₂ : Subsemiring R⦄ (hs : s₁ ≤ s₂) ⦃t₁ t₂ : Subsemiring S⦄ (ht : t₁ ≤ t₂) : s₁.prod t₁ ≤ s₂.prod t₂ := Set.prod_mono hs ht theorem prod_mono_right (s : Subsemiring R) : Monotone fun t : Subsemiring S => s.prod t := prod_mono (le_refl s) theorem prod_mono_left (t : Subsemiring S) : Monotone fun s : Subsemiring R => s.prod t := fun _ _ hs => prod_mono hs (le_refl t) theorem prod_top (s : Subsemiring R) : s.prod (⊤ : Subsemiring S) = s.comap (RingHom.fst R S) := ext fun x => by simp [mem_prod] theorem top_prod (s : Subsemiring S) : (⊤ : Subsemiring R).prod s = s.comap (RingHom.snd R S) := ext fun x => by simp [mem_prod] @[simp] theorem top_prod_top : (⊤ : Subsemiring R).prod (⊤ : Subsemiring S) = ⊤ := (top_prod _).trans <| comap_top _ /-- Product of subsemirings is isomorphic to their product as monoids. -/ def prodEquiv (s : Subsemiring R) (t : Subsemiring S) : s.prod t ≃+* s × t := { Equiv.Set.prod (s : Set R) (t : Set S) with map_mul' := fun _ _ => rfl map_add' := fun _ _ => rfl } theorem mem_iSup_of_directed {ι} [hι : Nonempty ι] {S : ι → Subsemiring R} (hS : Directed (· ≤ ·) S) {x : R} : (x ∈ ⨆ i, S i) ↔ ∃ i, x ∈ S i := by refine ⟨?_, fun ⟨i, hi⟩ ↦ le_iSup S i hi⟩ let U : Subsemiring R := Subsemiring.mk' (⋃ i, (S i : Set R)) (⨆ i, (S i).toSubmonoid) (Submonoid.coe_iSup_of_directed hS) (⨆ i, (S i).toAddSubmonoid) (AddSubmonoid.coe_iSup_of_directed hS) suffices ⨆ i, S i ≤ U by simpa [U] using @this x exact iSup_le fun i x hx ↦ Set.mem_iUnion.2 ⟨i, hx⟩ theorem coe_iSup_of_directed {ι} [hι : Nonempty ι] {S : ι → Subsemiring R} (hS : Directed (· ≤ ·) S) : ((⨆ i, S i : Subsemiring R) : Set R) = ⋃ i, S i := Set.ext fun x ↦ by simp [mem_iSup_of_directed hS] theorem mem_sSup_of_directedOn {S : Set (Subsemiring R)} (Sne : S.Nonempty) (hS : DirectedOn (· ≤ ·) S) {x : R} : x ∈ sSup S ↔ ∃ s ∈ S, x ∈ s := by haveI : Nonempty S := Sne.to_subtype simp only [sSup_eq_iSup', mem_iSup_of_directed hS.directed_val, SetCoe.exists, exists_prop] theorem coe_sSup_of_directedOn {S : Set (Subsemiring R)} (Sne : S.Nonempty) (hS : DirectedOn (· ≤ ·) S) : (↑(sSup S) : Set R) = ⋃ s ∈ S, ↑s := Set.ext fun x => by simp [mem_sSup_of_directedOn Sne hS] end Subsemiring namespace RingHom variable [NonAssocSemiring T] {s : Subsemiring R} variable {σR σS : Type*} variable [SetLike σR R] [SetLike σS S] [SubsemiringClass σR R] [SubsemiringClass σS S] open Subsemiring /-- Restriction of a ring homomorphism to a subsemiring of the codomain. -/ def codRestrict (f : R →+* S) (s : σS) (h : ∀ x, f x ∈ s) : R →+* s := { (f : R →* S).codRestrict s h, (f : R →+ S).codRestrict s h with toFun := fun n => ⟨f n, h n⟩ } @[simp] theorem codRestrict_apply (f : R →+* S) (s : σS) (h : ∀ x, f x ∈ s) (x : R) : (f.codRestrict s h x : S) = f x := rfl /-- The ring homomorphism from the preimage of `s` to `s`. -/ def restrict (f : R →+* S) (s' : σR) (s : σS) (h : ∀ x ∈ s', f x ∈ s) : s' →+* s := (f.domRestrict s').codRestrict s fun x => h x x.2 @[simp] theorem coe_restrict_apply (f : R →+* S) (s' : σR) (s : σS) (h : ∀ x ∈ s', f x ∈ s) (x : s') : (f.restrict s' s h x : S) = f x := rfl @[simp] theorem comp_restrict (f : R →+* S) (s' : σR) (s : σS) (h : ∀ x ∈ s', f x ∈ s) : (SubsemiringClass.subtype s).comp (f.restrict s' s h) = f.comp (SubsemiringClass.subtype s') := rfl /-- Restriction of a ring homomorphism to its range interpreted as a subsemiring. This is the bundled version of `Set.rangeFactorization`. -/ def rangeSRestrict (f : R →+* S) : R →+* f.rangeS := f.codRestrict (R := R) (S := S) (σS := Subsemiring S) f.rangeS f.mem_rangeS_self @[simp] theorem coe_rangeSRestrict (f : R →+* S) (x : R) : (f.rangeSRestrict x : S) = f x := rfl theorem rangeSRestrict_surjective (f : R →+* S) : Function.Surjective f.rangeSRestrict := fun ⟨_, hy⟩ => let ⟨x, hx⟩ := mem_rangeS.mp hy ⟨x, Subtype.ext hx⟩ theorem rangeS_top_iff_surjective {f : R →+* S} : f.rangeS = (⊤ : Subsemiring S) ↔ Function.Surjective f := SetLike.ext'_iff.trans <| Iff.trans (by rw [coe_rangeS, coe_top]) Set.range_eq_univ /-- The range of a surjective ring homomorphism is the whole of the codomain. -/ @[simp] theorem rangeS_top_of_surjective (f : R →+* S) (hf : Function.Surjective f) : f.rangeS = (⊤ : Subsemiring S) := rangeS_top_iff_surjective.2 hf /-- If two ring homomorphisms are equal on a set, then they are equal on its subsemiring closure. -/ theorem eqOn_sclosure {f g : R →+* S} {s : Set R} (h : Set.EqOn f g s) : Set.EqOn f g (closure s) := show closure s ≤ f.eqLocusS g from closure_le.2 h theorem eq_of_eqOn_stop {f g : R →+* S} (h : Set.EqOn f g (⊤ : Subsemiring R)) : f = g := ext fun _ => h (mem_top _) theorem eq_of_eqOn_sdense {s : Set R} (hs : closure s = ⊤) {f g : R →+* S} (h : s.EqOn f g) : f = g := eq_of_eqOn_stop <| hs ▸ eqOn_sclosure h theorem sclosure_preimage_le (f : R →+* S) (s : Set S) : closure (f ⁻¹' s) ≤ (closure s).comap f := closure_le.2 fun _ hx => SetLike.mem_coe.2 <| mem_comap.2 <| subset_closure hx /-- The image under a ring homomorphism of the subsemiring generated by a set equals the subsemiring generated by the image of the set. -/ theorem map_closureS (f : R →+* S) (s : Set R) : (closure s).map f = closure (f '' s) := Set.image_preimage.l_comm_of_u_comm (gc_map_comap f) (Subsemiring.gi S).gc (Subsemiring.gi R).gc fun _ ↦ coe_comap _ _ end RingHom namespace Subsemiring open RingHom /-- The ring homomorphism associated to an inclusion of subsemirings. -/ def inclusion {S T : Subsemiring R} (h : S ≤ T) : S →+* T := S.subtype.codRestrict _ fun x => h x.2 theorem inclusion_injective {S T : Subsemiring R} (h : S ≤ T) : Function.Injective (inclusion h) := Set.inclusion_injective h @[simp] theorem rangeS_subtype (s : Subsemiring R) : s.subtype.rangeS = s := SetLike.coe_injective <| (coe_rangeS _).trans Subtype.range_coe @[simp] theorem range_fst : (fst R S).rangeS = ⊤ := (fst R S).rangeS_top_of_surjective <| Prod.fst_surjective @[simp] theorem range_snd : (snd R S).rangeS = ⊤ := (snd R S).rangeS_top_of_surjective <| Prod.snd_surjective @[simp] theorem prod_bot_sup_bot_prod (s : Subsemiring R) (t : Subsemiring S) : s.prod ⊥ ⊔ prod ⊥ t = s.prod t := le_antisymm (sup_le (prod_mono_right s bot_le) (prod_mono_left t bot_le)) fun p hp => Prod.fst_mul_snd p ▸ mul_mem ((le_sup_left : s.prod ⊥ ≤ s.prod ⊥ ⊔ prod ⊥ t) ⟨hp.1, SetLike.mem_coe.2 <| one_mem ⊥⟩) ((le_sup_right : prod ⊥ t ≤ s.prod ⊥ ⊔ prod ⊥ t) ⟨SetLike.mem_coe.2 <| one_mem ⊥, hp.2⟩) end Subsemiring namespace RingEquiv variable {s t : Subsemiring R} /-- Makes the identity isomorphism from a proof two subsemirings of a multiplicative monoid are equal. -/ def subsemiringCongr (h : s = t) : s ≃+* t := { Equiv.setCongr <| congr_arg _ h with map_mul' := fun _ _ => rfl map_add' := fun _ _ => rfl } /-- Restrict a ring homomorphism with a left inverse to a ring isomorphism to its `RingHom.rangeS`. -/ def ofLeftInverseS {g : S → R} {f : R →+* S} (h : Function.LeftInverse g f) : R ≃+* f.rangeS := { f.rangeSRestrict with toFun := fun x => f.rangeSRestrict x invFun := fun x => (g ∘ f.rangeS.subtype) x left_inv := h right_inv := fun x => Subtype.ext <| by let ⟨x', hx'⟩ := RingHom.mem_rangeS.mp x.prop simp [← hx', h x'] } @[simp] theorem ofLeftInverseS_apply {g : S → R} {f : R →+* S} (h : Function.LeftInverse g f) (x : R) : ↑(ofLeftInverseS h x) = f x := rfl @[simp] theorem ofLeftInverseS_symm_apply {g : S → R} {f : R →+* S} (h : Function.LeftInverse g f) (x : f.rangeS) : (ofLeftInverseS h).symm x = g x := rfl /-- Given an equivalence `e : R ≃+* S` of semirings and a subsemiring `s` of `R`, `subsemiringMap e s` is the induced equivalence between `s` and `s.map e` -/ def subsemiringMap (e : R ≃+* S) (s : Subsemiring R) : s ≃+* s.map (e : R →+* S) := { e.toAddEquiv.addSubmonoidMap s.toAddSubmonoid, e.toMulEquiv.submonoidMap s.toSubmonoid with } @[simp] theorem subsemiringMap_apply_coe (e : R ≃+* S) (s : Subsemiring R) (x : s) : ((subsemiringMap e s) x : S) = e x := rfl @[simp] theorem subsemiringMap_symm_apply_coe (e : R ≃+* S) (s : Subsemiring R) (x : s.map e.toRingHom) : ((subsemiringMap e s).symm x : R) = e.symm x := rfl end RingEquiv /-! ### Actions by `Subsemiring`s These are just copies of the definitions about `Submonoid` starting from `Submonoid.mulAction`. The only new result is `Subsemiring.module`. When `R` is commutative, `Algebra.ofSubsemiring` provides a stronger result than those found in this file, which uses the same scalar action. -/ section Actions namespace Subsemiring variable {R' α β : Type*} variable {S' : Type*} [SetLike S' R'] (s : S) section NonAssocSemiring variable [NonAssocSemiring R'] /-- The action by a subsemiring is the action by the underlying semiring. -/ instance smul [SMul R' α] (S : Subsemiring R') : SMul S α := inferInstance theorem smul_def [SMul R' α] {S : Subsemiring R'} (g : S) (m : α) : g • m = (g : R') • m := rfl instance smulCommClass_left [SMul R' β] [SMul α β] [SMulCommClass R' α β] (S : Subsemiring R') : SMulCommClass S α β := inferInstance instance smulCommClass_right [SMul α β] [SMul R' β] [SMulCommClass α R' β] (S : Subsemiring R') : SMulCommClass α S β := inferInstance /-- Note that this provides `IsScalarTower S R R` which is needed by `smul_mul_assoc`. -/ instance isScalarTower [SMul α β] [SMul R' α] [SMul R' β] [IsScalarTower R' α β] (S : Subsemiring R') : IsScalarTower S α β := inferInstance instance (priority := low) {M' α : Type*} [SMul M' α] {S' : Type*} [SetLike S' M'] (s : S') [FaithfulSMul M' α] : FaithfulSMul s α := ⟨fun h => Subtype.ext <| eq_of_smul_eq_smul h⟩ instance faithfulSMul [SMul R' α] [FaithfulSMul R' α] (S : Subsemiring R') : FaithfulSMul S α := inferInstance instance (priority := low) {S' : Type*} [SetLike S' R'] [SubsemiringClass S' R'] (s : S') [Zero α] [SMulWithZero R' α] : SMulWithZero s α where smul_zero r := smul_zero (r : R') zero_smul := zero_smul R' /-- The action by a subsemiring is the action by the underlying semiring. -/ instance [Zero α] [SMulWithZero R' α] (S : Subsemiring R') : SMulWithZero S α := inferInstance end NonAssocSemiring variable [Semiring R'] /-- The action by a subsemiring is the action by the underlying semiring. -/ instance mulAction [MulAction R' α] (S : Subsemiring R') : MulAction S α := inferInstance /-- The action by a subsemiring is the action by the underlying semiring. -/ instance distribMulAction [AddMonoid α] [DistribMulAction R' α] (S : Subsemiring R') : DistribMulAction S α := inferInstance /-- The action by a subsemiring is the action by the underlying semiring. -/ instance mulDistribMulAction [Monoid α] [MulDistribMulAction R' α] (S : Subsemiring R') : MulDistribMulAction S α := inferInstance instance (priority := low) {S' : Type*} [SetLike S' R'] [SubsemiringClass S' R'] (s : S') [Zero α] [MulActionWithZero R' α] : MulActionWithZero s α where smul_zero r := smul_zero (r : R') zero_smul := zero_smul R' /-- The action by a subsemiring is the action by the underlying semiring. -/ instance mulActionWithZero [Zero α] [MulActionWithZero R' α] (S : Subsemiring R') : MulActionWithZero S α := inferInstance instance (priority := low) [AddCommMonoid α] [Module R' α] {S' : Type*} [SetLike S' R'] [SubsemiringClass S' R'] (s : S') : Module s α where toDistribMulAction := inferInstance add_smul r₁ r₂ := add_smul (r₁ : R') r₂ zero_smul := zero_smul R' /-- The action by a subsemiring is the action by the underlying semiring. -/ instance module [AddCommMonoid α] [Module R' α] (S : Subsemiring R') : Module S α := inferInstance /-- The action by a subsemiring is the action by the underlying semiring. -/ instance [Semiring α] [MulSemiringAction R' α] (S : Subsemiring R') : MulSemiringAction S α := inferInstance /-- The center of a semiring acts commutatively on that semiring. -/ instance center.smulCommClass_left : SMulCommClass (center R') R' R' := Submonoid.center.smulCommClass_left /-- The center of a semiring acts commutatively on that semiring. -/ instance center.smulCommClass_right : SMulCommClass R' (center R') R' := Submonoid.center.smulCommClass_right lemma closure_le_centralizer_centralizer (s : Set R') : closure s ≤ centralizer (centralizer s) := closure_le.mpr Set.subset_centralizer_centralizer /-- If all the elements of a set `s` commute, then `closure s` is a commutative semiring. -/ abbrev closureCommSemiringOfComm {s : Set R'} (hcomm : ∀ x ∈ s, ∀ y ∈ s, x * y = y * x) : CommSemiring (closure s) := { (closure s).toSemiring with mul_comm := fun ⟨_, h₁⟩ ⟨_, h₂⟩ ↦ have := closure_le_centralizer_centralizer s Subtype.ext <| Set.centralizer_centralizer_comm_of_comm hcomm _ (this h₁) _ (this h₂) } end Subsemiring end Actions namespace Subsemiring theorem map_comap_eq (f : R →+* S) (t : Subsemiring S) : (t.comap f).map f = t ⊓ f.rangeS := SetLike.coe_injective Set.image_preimage_eq_inter_range theorem map_comap_eq_self {f : R →+* S} {t : Subsemiring S} (h : t ≤ f.rangeS) : (t.comap f).map f = t := by simpa only [inf_of_le_left h] using map_comap_eq f t theorem map_comap_eq_self_of_surjective {f : R →+* S} (hf : Function.Surjective f) (t : Subsemiring S) : (t.comap f).map f = t := map_comap_eq_self <| by simp [hf] theorem comap_map_eq_self_of_injective {f : R →+* S} (hf : Function.Injective f) (s : Subsemiring R) : (s.map f).comap f = s := SetLike.coe_injective (Set.preimage_image_eq _ hf) end Subsemiring
.lake/packages/mathlib/Mathlib/Algebra/Ring/Subsemiring/Defs.lean
import Mathlib.RingTheory.NonUnitalSubsemiring.Defs /-! # Bundled subsemirings We define bundled subsemirings and some standard constructions: `subtype` and `inclusion` ring homomorphisms. -/ assert_not_exists RelIso universe u v w section AddSubmonoidWithOneClass /-- `AddSubmonoidWithOneClass S R` says `S` is a type of subsets `s ≤ R` that contain `0`, `1`, and are closed under `(+)` -/ class AddSubmonoidWithOneClass (S : Type*) (R : outParam Type*) [AddMonoidWithOne R] [SetLike S R] : Prop extends AddSubmonoidClass S R, OneMemClass S R variable {S R : Type*} [AddMonoidWithOne R] [SetLike S R] (s : S) @[simp, aesop safe (rule_sets := [SetLike])] theorem natCast_mem [AddSubmonoidWithOneClass S R] (n : ℕ) : (n : R) ∈ s := by induction n <;> simp [zero_mem, add_mem, one_mem, *] @[simp, aesop safe (rule_sets := [SetLike])] lemma ofNat_mem [AddSubmonoidWithOneClass S R] (s : S) (n : ℕ) [n.AtLeastTwo] : ofNat(n) ∈ s := by rw [← Nat.cast_ofNat]; exact natCast_mem s n instance (priority := 74) AddSubmonoidWithOneClass.toAddMonoidWithOne [AddSubmonoidWithOneClass S R] : AddMonoidWithOne s := { AddSubmonoidClass.toAddMonoid s with one := ⟨_, one_mem s⟩ natCast := fun n => ⟨n, natCast_mem s n⟩ natCast_zero := Subtype.ext Nat.cast_zero natCast_succ := fun _ => Subtype.ext (Nat.cast_succ _) } end AddSubmonoidWithOneClass variable {R : Type u} {S : Type v} [NonAssocSemiring R] section SubsemiringClass /-- `SubsemiringClass S R` states that `S` is a type of subsets `s ⊆ R` that are both a multiplicative and an additive submonoid. -/ class SubsemiringClass (S : Type*) (R : outParam (Type u)) [NonAssocSemiring R] [SetLike S R] : Prop extends SubmonoidClass S R, AddSubmonoidClass S R -- See note [lower instance priority] instance (priority := 100) SubsemiringClass.addSubmonoidWithOneClass (S : Type*) (R : Type u) {_ : NonAssocSemiring R} [SetLike S R] [h : SubsemiringClass S R] : AddSubmonoidWithOneClass S R := { h with } instance (priority := 100) SubsemiringClass.nonUnitalSubsemiringClass (S : Type*) (R : Type u) [NonAssocSemiring R] [SetLike S R] [SubsemiringClass S R] : NonUnitalSubsemiringClass S R where mul_mem := mul_mem variable [SetLike S R] [hSR : SubsemiringClass S R] (s : S) namespace SubsemiringClass -- Prefer subclasses of `NonAssocSemiring` over subclasses of `SubsemiringClass`. /-- A subsemiring of a `NonAssocSemiring` inherits a `NonAssocSemiring` structure -/ instance (priority := 75) toNonAssocSemiring : NonAssocSemiring s := fast_instance% Subtype.coe_injective.nonAssocSemiring Subtype.val rfl rfl (fun _ _ => rfl) (fun _ _ => rfl) (fun _ _ => rfl) fun _ => rfl instance nontrivial [Nontrivial R] : Nontrivial s := nontrivial_of_ne 0 1 fun H => zero_ne_one (congr_arg Subtype.val H) instance noZeroDivisors [NoZeroDivisors R] : NoZeroDivisors s := Subtype.coe_injective.noZeroDivisors _ rfl fun _ _ => rfl /-- The natural ring hom from a subsemiring of semiring `R` to `R`. -/ def subtype : s →+* R := { SubmonoidClass.subtype s, AddSubmonoidClass.subtype s with toFun := (↑) } @[simp] theorem coe_subtype : (subtype s : s → R) = ((↑) : s → R) := rfl variable {s} in @[simp] lemma subtype_apply (x : s) : SubsemiringClass.subtype s x = x := rfl lemma subtype_injective : Function.Injective (SubsemiringClass.subtype s) := fun _ ↦ by simp -- Prefer subclasses of `Semiring` over subclasses of `SubsemiringClass`. /-- A subsemiring of a `Semiring` is a `Semiring`. -/ instance (priority := 75) toSemiring {R} [Semiring R] [SetLike S R] [SubsemiringClass S R] : Semiring s := fast_instance% Subtype.coe_injective.semiring Subtype.val rfl rfl (fun _ _ => rfl) (fun _ _ => rfl) (fun _ _ => rfl) (fun _ _ => rfl) fun _ => rfl @[deprecated (since := "2025-07-29")] alias coe_pow := SubmonoidClass.coe_pow /-- A subsemiring of a `CommSemiring` is a `CommSemiring`. -/ instance toCommSemiring {R} [CommSemiring R] [SetLike S R] [SubsemiringClass S R] : CommSemiring s := fast_instance% Subtype.coe_injective.commSemiring Subtype.val rfl rfl (fun _ _ => rfl) (fun _ _ => rfl) (fun _ _ => rfl) (fun _ _ => rfl) fun _ => rfl end SubsemiringClass end SubsemiringClass variable [NonAssocSemiring S] /-- A subsemiring of a semiring `R` is a subset `s` that is both a multiplicative and an additive submonoid. -/ structure Subsemiring (R : Type u) [NonAssocSemiring R] extends Submonoid R, AddSubmonoid R /-- Reinterpret a `Subsemiring` as a `Submonoid`. -/ add_decl_doc Subsemiring.toSubmonoid /-- Reinterpret a `Subsemiring` as an `AddSubmonoid`. -/ add_decl_doc Subsemiring.toAddSubmonoid namespace Subsemiring instance : SetLike (Subsemiring R) R where coe s := s.carrier coe_injective' p q h := by cases p; cases q; congr; exact SetLike.coe_injective' h initialize_simps_projections Subsemiring (carrier → coe, as_prefix coe) /-- The actual `Subsemiring` obtained from an element of a `SubsemiringClass`. -/ @[simps] def ofClass {S R : Type*} [NonAssocSemiring R] [SetLike S R] [SubsemiringClass S R] (s : S) : Subsemiring R where carrier := s add_mem' := add_mem zero_mem' := zero_mem _ mul_mem' := mul_mem one_mem' := one_mem _ instance (priority := 100) : CanLift (Set R) (Subsemiring R) (↑) (fun s ↦ 0 ∈ s ∧ (∀ {x y}, x ∈ s → y ∈ s → x + y ∈ s) ∧ 1 ∈ s ∧ ∀ {x y}, x ∈ s → y ∈ s → x * y ∈ s) where prf s h := ⟨ { carrier := s zero_mem' := h.1 add_mem' := h.2.1 one_mem' := h.2.2.1 mul_mem' := h.2.2.2 }, rfl ⟩ instance : SubsemiringClass (Subsemiring R) R where zero_mem := zero_mem' add_mem {s} := AddSubsemigroup.add_mem' s.toAddSubmonoid.toAddSubsemigroup one_mem {s} := Submonoid.one_mem' s.toSubmonoid mul_mem {s} := Subsemigroup.mul_mem' s.toSubmonoid.toSubsemigroup /-- Turn a `Subsemiring` into a `NonUnitalSubsemiring` by forgetting that it contains `1`. -/ def toNonUnitalSubsemiring (S : Subsemiring R) : NonUnitalSubsemiring R where __ := S @[simp] theorem mem_toSubmonoid {s : Subsemiring R} {x : R} : x ∈ s.toSubmonoid ↔ x ∈ s := Iff.rfl @[simp] lemma mem_toNonUnitalSubsemiring {S : Subsemiring R} {x : R} : x ∈ S.toNonUnitalSubsemiring ↔ x ∈ S := .rfl theorem mem_carrier {s : Subsemiring R} {x : R} : x ∈ s.carrier ↔ x ∈ s := Iff.rfl @[simp] lemma coe_toNonUnitalSubsemiring (S : Subsemiring R) : (S.toNonUnitalSubsemiring : Set R) = S := rfl @[simp] theorem mem_mk {toSubmonoid : Submonoid R} (add_mem zero_mem) {x : R} : x ∈ mk toSubmonoid add_mem zero_mem ↔ x ∈ toSubmonoid := .rfl @[simp] theorem coe_set_mk {toSubmonoid : Submonoid R} (add_mem zero_mem) : (mk toSubmonoid add_mem zero_mem : Set R) = toSubmonoid := rfl /-- Two subsemirings are equal if they have the same elements. -/ @[ext] theorem ext {S T : Subsemiring R} (h : ∀ x, x ∈ S ↔ x ∈ T) : S = T := SetLike.ext h /-- Copy of a subsemiring with a new `carrier` equal to the old one. Useful to fix definitional equalities. -/ @[simps coe toSubmonoid] protected def copy (S : Subsemiring R) (s : Set R) (hs : s = ↑S) : Subsemiring R := { S.toAddSubmonoid.copy s hs, S.toSubmonoid.copy s hs with carrier := s } theorem copy_eq (S : Subsemiring R) (s : Set R) (hs : s = ↑S) : S.copy s hs = S := SetLike.coe_injective hs theorem toSubmonoid_injective : Function.Injective (toSubmonoid : Subsemiring R → Submonoid R) | _, _, h => ext (SetLike.ext_iff.mp h :) theorem toAddSubmonoid_injective : Function.Injective (toAddSubmonoid : Subsemiring R → AddSubmonoid R) | _, _, h => ext (SetLike.ext_iff.mp h :) lemma toNonUnitalSubsemiring_injective : Function.Injective (toNonUnitalSubsemiring : Subsemiring R → _) := fun S₁ S₂ h => SetLike.ext'_iff.2 (show (S₁.toNonUnitalSubsemiring : Set R) = S₂ from SetLike.ext'_iff.1 h) @[simp] lemma toNonUnitalSubsemiring_inj {S₁ S₂ : Subsemiring R} : S₁.toNonUnitalSubsemiring = S₂.toNonUnitalSubsemiring ↔ S₁ = S₂ := toNonUnitalSubsemiring_injective.eq_iff lemma one_mem_toNonUnitalSubsemiring (S : Subsemiring R) : (1 : R) ∈ S.toNonUnitalSubsemiring := S.one_mem /-- Construct a `Subsemiring R` from a set `s`, a submonoid `sm`, and an additive submonoid `sa` such that `x ∈ s ↔ x ∈ sm ↔ x ∈ sa`. -/ @[simps coe] protected def mk' (s : Set R) (sm : Submonoid R) (hm : ↑sm = s) (sa : AddSubmonoid R) (ha : ↑sa = s) : Subsemiring R where carrier := s zero_mem' := by exact ha ▸ sa.zero_mem one_mem' := by exact hm ▸ sm.one_mem add_mem' {x y} := by simpa only [← ha] using sa.add_mem mul_mem' {x y} := by simpa only [← hm] using sm.mul_mem @[simp] theorem mem_mk' {s : Set R} {sm : Submonoid R} (hm : ↑sm = s) {sa : AddSubmonoid R} (ha : ↑sa = s) {x : R} : x ∈ Subsemiring.mk' s sm hm sa ha ↔ x ∈ s := Iff.rfl @[simp] theorem mk'_toSubmonoid {s : Set R} {sm : Submonoid R} (hm : ↑sm = s) {sa : AddSubmonoid R} (ha : ↑sa = s) : (Subsemiring.mk' s sm hm sa ha).toSubmonoid = sm := SetLike.coe_injective hm.symm @[simp] theorem mk'_toAddSubmonoid {s : Set R} {sm : Submonoid R} (hm : ↑sm = s) {sa : AddSubmonoid R} (ha : ↑sa = s) : (Subsemiring.mk' s sm hm sa ha).toAddSubmonoid = sa := SetLike.coe_injective ha.symm end Subsemiring namespace Subsemiring variable (s : Subsemiring R) /-- A subsemiring contains the semiring's 1. -/ protected theorem one_mem : (1 : R) ∈ s := one_mem s /-- A subsemiring contains the semiring's 0. -/ protected theorem zero_mem : (0 : R) ∈ s := zero_mem s /-- A subsemiring is closed under multiplication. -/ protected theorem mul_mem {x y : R} : x ∈ s → y ∈ s → x * y ∈ s := mul_mem /-- A subsemiring is closed under addition. -/ protected theorem add_mem {x y : R} : x ∈ s → y ∈ s → x + y ∈ s := add_mem /-- A subsemiring of a `NonAssocSemiring` inherits a `NonAssocSemiring` structure -/ instance toNonAssocSemiring : NonAssocSemiring s := SubsemiringClass.toNonAssocSemiring _ @[simp, norm_cast] theorem coe_one : ((1 : s) : R) = (1 : R) := rfl @[simp, norm_cast] theorem coe_zero : ((0 : s) : R) = (0 : R) := rfl @[simp, norm_cast] theorem coe_add (x y : s) : ((x + y : s) : R) = (x + y : R) := rfl @[simp, norm_cast] theorem coe_mul (x y : s) : ((x * y : s) : R) = (x * y : R) := rfl instance nontrivial [Nontrivial R] : Nontrivial s := nontrivial_of_ne 0 1 fun H => zero_ne_one (congr_arg Subtype.val H) protected theorem pow_mem {R : Type*} [Semiring R] (s : Subsemiring R) {x : R} (hx : x ∈ s) (n : ℕ) : x ^ n ∈ s := pow_mem hx n instance noZeroDivisors [NoZeroDivisors R] : NoZeroDivisors s where eq_zero_or_eq_zero_of_mul_eq_zero {_ _} h := (eq_zero_or_eq_zero_of_mul_eq_zero <| Subtype.ext_iff.mp h).imp Subtype.eq Subtype.eq /-- A subsemiring of a `Semiring` is a `Semiring`. -/ instance toSemiring {R} [Semiring R] (s : Subsemiring R) : Semiring s := { s.toNonAssocSemiring, s.toSubmonoid.toMonoid with } @[simp, norm_cast] theorem coe_pow {R} [Semiring R] (s : Subsemiring R) (x : s) (n : ℕ) : ((x ^ n : s) : R) = (x : R) ^ n := rfl /-- A subsemiring of a `CommSemiring` is a `CommSemiring`. -/ instance toCommSemiring {R} [CommSemiring R] (s : Subsemiring R) : CommSemiring s := { s.toSemiring with mul_comm := fun _ _ => Subtype.eq <| mul_comm _ _ } /-- The natural ring hom from a subsemiring of semiring `R` to `R`. -/ def subtype : s →+* R := { s.toSubmonoid.subtype, s.toAddSubmonoid.subtype with toFun := (↑) } variable {s} in @[simp] lemma subtype_apply (x : s) : s.subtype x = x := rfl lemma subtype_injective : Function.Injective s.subtype := Subtype.coe_injective @[simp] theorem coe_subtype : ⇑s.subtype = ((↑) : s → R) := rfl protected theorem nsmul_mem {x : R} (hx : x ∈ s) (n : ℕ) : n • x ∈ s := nsmul_mem hx n @[simp] theorem coe_toSubmonoid (s : Subsemiring R) : (s.toSubmonoid : Set R) = s := rfl @[simp] theorem coe_carrier_toSubmonoid (s : Subsemiring R) : (s.toSubmonoid.carrier : Set R) = s := rfl theorem mem_toAddSubmonoid {s : Subsemiring R} {x : R} : x ∈ s.toAddSubmonoid ↔ x ∈ s := Iff.rfl theorem coe_toAddSubmonoid (s : Subsemiring R) : (s.toAddSubmonoid : Set R) = s := rfl /-- The subsemiring `R` of the semiring `R`. -/ instance : Top (Subsemiring R) := ⟨{ (⊤ : Submonoid R), (⊤ : AddSubmonoid R) with }⟩ @[simp] theorem mem_top (x : R) : x ∈ (⊤ : Subsemiring R) := Set.mem_univ x @[simp, norm_cast] theorem coe_top : ((⊤ : Subsemiring R) : Set R) = Set.univ := rfl end Subsemiring namespace Subsemiring /-- The inf of two subsemirings is their intersection. -/ instance : Min (Subsemiring R) := ⟨fun s t => { s.toSubmonoid ⊓ t.toSubmonoid, s.toAddSubmonoid ⊓ t.toAddSubmonoid with carrier := s ∩ t }⟩ @[simp, norm_cast] theorem coe_inf (p p' : Subsemiring R) : ((p ⊓ p' : Subsemiring R) : Set R) = (p : Set R) ∩ p' := rfl @[simp] theorem mem_inf {p p' : Subsemiring R} {x : R} : x ∈ p ⊓ p' ↔ x ∈ p ∧ x ∈ p' := Iff.rfl end Subsemiring namespace RingHom variable {s : Subsemiring R} {σR : Type*} [SetLike σR R] [SubsemiringClass σR R] open Subsemiring /-- Restriction of a ring homomorphism to a subsemiring of the domain. -/ def domRestrict (f : R →+* S) (s : σR) : s →+* S := f.comp <| SubsemiringClass.subtype s @[simp] theorem restrict_apply (f : R →+* S) {s : σR} (x : s) : f.domRestrict s x = f x := rfl /-- The subsemiring of elements `x : R` such that `f x = g x` -/ def eqLocusS (f g : R →+* S) : Subsemiring R := { (f : R →* S).eqLocusM g, (f : R →+ S).eqLocusM g with carrier := { x | f x = g x } } @[simp] theorem mem_eqLocusS {f g : R →+* S} {x : R} : x ∈ f.eqLocusS g ↔ f x = g x := Iff.rfl @[simp] theorem eqLocusS_same (f : R →+* S) : f.eqLocusS f = ⊤ := SetLike.ext fun _ => eq_self_iff_true _ end RingHom /-- Turn a non-unital subsemiring containing `1` into a subsemiring. -/ def NonUnitalSubsemiring.toSubsemiring (S : NonUnitalSubsemiring R) (h1 : 1 ∈ S) : Subsemiring R where __ := S one_mem' := h1 lemma Subsemiring.toNonUnitalSubsemiring_toSubsemiring (S : Subsemiring R) : S.toNonUnitalSubsemiring.toSubsemiring S.one_mem = S := rfl lemma NonUnitalSubsemiring.toSubsemiring_toNonUnitalSubsemiring (S : NonUnitalSubsemiring R) (h1) : (NonUnitalSubsemiring.toSubsemiring S h1).toNonUnitalSubsemiring = S := rfl
.lake/packages/mathlib/Mathlib/Algebra/Ring/Semireal/Defs.lean
import Mathlib.Algebra.Ring.SumsOfSquares /-! # Semireal rings A semireal ring is a commutative ring (with unit) in which `-1` is *not* a sum of squares. For instance, linearly ordered rings are semireal, because sums of squares are positive and `-1` is not. ## Main declaration - `IsSemireal`: the predicate asserting that a commutative ring `R` is semireal. ## References - *An introduction to real algebra*, by T.Y. Lam. Rocky Mountain J. Math. 14(4): 767-814 (1984). [lam_1984](https://doi.org/10.1216/RMJ-1984-14-4-767) -/ variable {R : Type*} variable (R) in /-- A semireal ring is a commutative ring (with unit) in which `-1` is *not* a sum of squares. We define the predicate `IsSemireal R` for structures `R` equipped with a multiplication, an addition, a multiplicative unit and an additive unit. -/ @[mk_iff] class IsSemireal [Add R] [Mul R] [One R] [Zero R] : Prop where one_add_ne_zero {s : R} (hs : IsSumSq s) : 1 + s ≠ 0 /-- In a semireal ring, `-1` is not a sum of squares. -/ theorem IsSemireal.not_isSumSq_neg_one [AddGroup R] [One R] [Mul R] [IsSemireal R] : ¬ IsSumSq (-1 : R) := (by simpa using one_add_ne_zero ·) /-- Linearly ordered semirings with the property `a ≤ b → ∃ c, a + c = b` (e.g. `ℕ`) are semireal. -/ instance [Semiring R] [LinearOrder R] [IsStrictOrderedRing R] [ExistsAddOfLE R] : IsSemireal R where one_add_ne_zero hs amo := zero_ne_one' R (le_antisymm zero_le_one (le_of_le_of_eq (le_add_of_nonneg_right hs.nonneg) amo))
.lake/packages/mathlib/Mathlib/Algebra/Vertex/HVertexOperator.lean
import Mathlib.RingTheory.HahnSeries.Multiplication /-! # Vertex operators In this file we introduce heterogeneous vertex operators using Hahn series. When `R = ℂ`, `V = W`, and `Γ = ℤ`, then this is the usual notion of "meromorphic left-moving 2D field". The notion we use here allows us to consider composites and scalar-multiply by multivariable Laurent series. ## Definitions * `HVertexOperator` : An `R`-linear map from an `R`-module `V` to `HahnModule Γ W`. * The coefficient function as an `R`-linear map. * Composition of heterogeneous vertex operators - values are Hahn series on lex order product. ## Main results * Ext ## TODO * curry for tensor product inputs * more API to make ext comparisons easier. * formal variable API, e.g., like the `T` function for Laurent polynomials. ## References * [R. Borcherds, *Vertex Algebras, Kac-Moody Algebras, and the Monster*][borcherds1986vertex] -/ assert_not_exists Cardinal noncomputable section variable {Γ : Type*} [PartialOrder Γ] {R : Type*} {V W : Type*} [CommRing R] [AddCommGroup V] [Module R V] [AddCommGroup W] [Module R W] /-- A heterogeneous `Γ`-vertex operator over a commutator ring `R` is an `R`-linear map from an `R`-module `V` to `Γ`-Hahn series with coefficients in an `R`-module `W`. -/ abbrev HVertexOperator (Γ : Type*) [PartialOrder Γ] (R : Type*) [CommRing R] (V : Type*) (W : Type*) [AddCommGroup V] [Module R V] [AddCommGroup W] [Module R W] := V →ₗ[R] (HahnModule Γ R W) namespace HVertexOperator section Coeff open HahnModule @[ext] theorem ext (A B : HVertexOperator Γ R V W) (h : ∀ v : V, A v = B v) : A = B := LinearMap.ext h /-- The coefficients of a heterogeneous vertex operator, viewed as a linear map to formal power series with coefficients in linear maps. -/ @[simps] def coeff : HVertexOperator Γ R V W →ₗ[R] Γ → V →ₗ[R] W where toFun A n := { toFun v := ((of R).symm (A v)).coeff n map_add' u v := by simp map_smul' r v := by simp } map_add' _ _ := by ext; simp map_smul' _ _ := by ext; simp theorem coeff_isPWOsupport (A : HVertexOperator Γ R V W) (v : V) : ((of R).symm (A v)).coeff.support.IsPWO := ((of R).symm (A v)).isPWO_support' @[ext] theorem coeff_inj : Function.Injective (coeff : HVertexOperator Γ R V W →ₗ[R] Γ → (V →ₗ[R] W)) := by intro _ _ h ext v n exact congrFun (congrArg DFunLike.coe (congrFun h n)) v /-- Given a coefficient function valued in linear maps satisfying a partially well-ordered support condition, we produce a heterogeneous vertex operator. -/ @[simps] def of_coeff (f : Γ → V →ₗ[R] W) (hf : ∀ (x : V), (Function.support (f · x)).IsPWO) : HVertexOperator Γ R V W where toFun x := (of R) { coeff := fun g => f g x, isPWO_support' := hf x } map_add' _ _ := by ext; simp map_smul' _ _ := by ext; simp @[deprecated (since := "2025-08-30")] alias coeff_add := map_add @[deprecated (since := "2025-08-30")] alias coeff_smul := map_smul @[simp] theorem coeff_of_coeff (f : Γ → V →ₗ[R] W) (hf : ∀ (x : V), (Function.support (fun g => f g x)).IsPWO) : (of_coeff f hf).coeff = f := rfl @[simp] theorem of_coeff_coeff (A : HVertexOperator Γ R V W) : of_coeff A.coeff A.coeff_isPWOsupport = A := rfl end Coeff section Products variable {Γ Γ' : Type*} [PartialOrder Γ] [PartialOrder Γ'] {R : Type*} [CommRing R] {U V W : Type*} [AddCommGroup U] [Module R U] [AddCommGroup V] [Module R V] [AddCommGroup W] [Module R W] (A : HVertexOperator Γ R V W) (B : HVertexOperator Γ' R U V) open HahnModule /-- The composite of two heterogeneous vertex operators acting on a vector, as an iterated Hahn series. -/ @[simps] def compHahnSeries (u : U) : HahnSeries Γ' (HahnSeries Γ W) where coeff g' := A (coeff B g' u) isPWO_support' := by refine Set.IsPWO.mono (((of R).symm (B u)).isPWO_support') ?_ simp only [coeff_apply_apply, Function.support_subset_iff, ne_eq, Function.mem_support] intro g' hg' hAB exact hg' (by simp [hAB]) @[simp] theorem compHahnSeries_add (u v : U) : compHahnSeries A B (u + v) = compHahnSeries A B u + compHahnSeries A B v := by ext simp only [compHahnSeries_coeff, map_add, coeff_apply_apply, HahnSeries.coeff_add', Pi.add_apply] rw [← HahnSeries.coeff_add] @[simp] theorem compHahnSeries_smul (r : R) (u : U) : compHahnSeries A B (r • u) = r • compHahnSeries A B u := by ext simp only [compHahnSeries_coeff, map_smul, coeff_apply_apply, HahnSeries.coeff_smul] rw [← HahnSeries.coeff_smul] /-- The composite of two heterogeneous vertex operators, as a heterogeneous vertex operator. -/ @[simps] def comp : HVertexOperator (Γ' ×ₗ Γ) R U W where toFun u := HahnModule.of R (HahnSeries.ofIterate (compHahnSeries A B u)) map_add' := by intro u v ext g simp [HahnSeries.ofIterate] map_smul' := by intro r x ext g simp [HahnSeries.ofIterate] @[simp] theorem coeff_comp (g : Γ' ×ₗ Γ) : (comp A B).coeff g = A.coeff (ofLex g).2 ∘ₗ B.coeff (ofLex g).1 := by rfl end Products end HVertexOperator
.lake/packages/mathlib/Mathlib/Algebra/Vertex/VertexOperator.lean
import Mathlib.Algebra.Vertex.HVertexOperator import Mathlib.Data.Int.Interval /-! # Vertex operators In this file we introduce vertex operators as linear maps to Laurent series. ## Definitions * `VertexOperator` is an `R`-linear map from an `R`-module `V` to `LaurentSeries V`. * `VertexOperator.ncoeff` is the coefficient of a vertex operator under normalized indexing. ## TODO * `HasseDerivative` : A divided-power derivative. * `Locality` : A weak form of commutativity. * `Residue products` : A family of products on `VertexOperator R V` parametrized by integers. ## References * [G. Mason, *Vertex rings and Pierce bundles*][mason2017] * [A. Matsuo, K. Nagatomo, *On axioms for a vertex algebra and locality of quantum fields*][matsuo1997] -/ noncomputable section variable {R V : Type*} [CommRing R] [AddCommGroup V] [Module R V] /-- A vertex operator over a commutative ring `R` is an `R`-linear map from an `R`-module `V` to Laurent series with coefficients in `V`. We write this as a specialization of the heterogeneous case. -/ abbrev VertexOperator (R : Type*) (V : Type*) [CommRing R] [AddCommGroup V] [Module R V] := HVertexOperator ℤ R V V namespace VertexOperator open HVertexOperator @[ext] theorem ext (A B : VertexOperator R V) (h : ∀ v : V, A v = B v) : A = B := LinearMap.ext h /-- The coefficient of a vertex operator under normalized indexing. -/ def ncoeff : VertexOperator R V →ₗ[R] ℤ → Module.End R V where toFun A n := HVertexOperator.coeff A (-n - 1) map_add' _ _ := by ext; simp map_smul' _ _ := by ext; simp theorem ncoeff_apply (A : VertexOperator R V) (n : ℤ) : ncoeff A n = coeff A (-n - 1) := rfl /-- In the literature, the `n`th normalized coefficient of a vertex operator `A` is written as either `Aₙ` or `A(n)`. -/ scoped[VertexOperator] notation A "[[" n "]]" => ncoeff A n @[simp] theorem coeff_eq_ncoeff (A : VertexOperator R V) (n : ℤ) : HVertexOperator.coeff A n = A [[-n - 1]] := by rw [ncoeff_apply, neg_sub, Int.sub_neg, add_sub_cancel_left] @[deprecated (since := "2025-08-30")] alias ncoeff_add := map_add @[deprecated (since := "2025-08-30")] alias ncoeff_smul := map_smul theorem ncoeff_eq_zero_of_lt_order (A : VertexOperator R V) (n : ℤ) (x : V) (h : -n - 1 < HahnSeries.order ((HahnModule.of R).symm (A x))) : (A [[n]]) x = 0 := by simp only [ncoeff, HVertexOperator.coeff, LinearMap.coe_mk, AddHom.coe_mk] exact HahnSeries.coeff_eq_zero_of_lt_order h theorem coeff_eq_zero_of_lt_order (A : VertexOperator R V) (n : ℤ) (x : V) (h : n < HahnSeries.order ((HahnModule.of R).symm (A x))) : coeff A n x = 0 := by rw [coeff_eq_ncoeff, ncoeff_eq_zero_of_lt_order A (-n - 1) x] cutsat /-- Given an endomorphism-valued function on integers satisfying a pointwise bounded-pole condition, we produce a vertex operator. -/ noncomputable def of_coeff (f : ℤ → Module.End R V) (hf : ∀ x : V, ∃ n : ℤ, ∀ m : ℤ, m < n → f m x = 0) : VertexOperator R V := HVertexOperator.of_coeff f (fun x => HahnSeries.suppBddBelow_supp_PWO (fun n => f n x) (HahnSeries.forallLTEqZero_supp_BddBelow (fun n => f n x) (Exists.choose (hf x)) (Exists.choose_spec (hf x)))) @[simp] theorem of_coeff_apply_coeff (f : ℤ → Module.End R V) (hf : ∀ (x : V), ∃ n, ∀ m < n, (f m) x = 0) (x : V) (n : ℤ) : ((HahnModule.of R).symm ((of_coeff f hf) x)).coeff n = (f n) x := by rfl @[simp] theorem ncoeff_of_coeff (f : ℤ → Module.End R V) (hf : ∀ (x : V), ∃ (n : ℤ), ∀ (m : ℤ), m < n → (f m) x = 0) (n : ℤ) : (of_coeff f hf) [[n]] = f (-n - 1) := by ext v rw [ncoeff_apply, coeff_apply_apply, of_coeff_apply_coeff] end VertexOperator
.lake/packages/mathlib/Mathlib/Algebra/MonoidAlgebra/Opposite.lean
import Mathlib.Algebra.MonoidAlgebra.Defs import Mathlib.Algebra.Ring.Opposite import Mathlib.Data.Finsupp.Basic /-! # Monoid algebras and the opposite ring -/ assert_not_exists NonUnitalAlgHom AlgEquiv noncomputable section open Finsupp hiding single universe u₁ u₂ u₃ u₄ variable (k : Type u₁) (G : Type u₂) (H : Type*) {R S M : Type*} /-! ### Multiplicative monoids -/ namespace MonoidAlgebra variable {k G} open Finsupp MulOpposite variable [Semiring k] /-- The opposite of a `MonoidAlgebra R I` equivalent as a ring to the `MonoidAlgebra Rᵐᵒᵖ Iᵐᵒᵖ` over the opposite ring, taking elements to their opposite. -/ @[simps! +simpRhs apply symm_apply] protected noncomputable def opRingEquiv [Mul G] : (MonoidAlgebra k G)ᵐᵒᵖ ≃+* MonoidAlgebra kᵐᵒᵖ Gᵐᵒᵖ := { opAddEquiv.symm.trans <| (Finsupp.mapRange.addEquiv (opAddEquiv : k ≃+ kᵐᵒᵖ)).trans <| Finsupp.domCongr opEquiv with map_mul' := by -- This used to be `rw`, but we need `erw` after https://github.com/leanprover/lean4/pull/2644 rw [Equiv.toFun_as_coe, AddEquiv.toEquiv_eq_coe]; erw [AddEquiv.coe_toEquiv] rw [← AddEquiv.coe_toAddMonoidHom] refine Iff.mpr (AddMonoidHom.map_mul_iff (R := (MonoidAlgebra k G)ᵐᵒᵖ) (S := MonoidAlgebra kᵐᵒᵖ Gᵐᵒᵖ) _) ?_ ext -- Porting note: `reducible` cannot be `local` so proof gets long. simp only [AddMonoidHom.coe_comp, Function.comp_apply, singleAddHom_apply, AddMonoidHom.compr₂_apply, AddMonoidHom.coe_mul, AddMonoidHom.coe_mulLeft, AddMonoidHom.compl₂_apply, AddEquiv.toAddMonoidHom_eq_coe, AddEquiv.coe_addMonoidHom_trans] -- This used to be `rw`, but we need `erw` after https://github.com/leanprover/lean4/pull/2644 erw [AddEquiv.trans_apply, AddEquiv.trans_apply, AddEquiv.trans_apply, MulOpposite.opAddEquiv_symm_apply] rw [MulOpposite.unop_mul (α := MonoidAlgebra k G)] simp } theorem opRingEquiv_single [Mul G] (r : k) (x : G) : MonoidAlgebra.opRingEquiv (op (single x r)) = single (op x) (op r) := by simp theorem opRingEquiv_symm_single [Mul G] (r : kᵐᵒᵖ) (x : Gᵐᵒᵖ) : MonoidAlgebra.opRingEquiv.symm (single x r) = op (single x.unop r.unop) := by simp end MonoidAlgebra /-! ### Additive monoids -/ namespace AddMonoidAlgebra variable {k G H} open Finsupp MulOpposite variable [Semiring k] /-- The opposite of an `R[I]` is ring equivalent to the `AddMonoidAlgebra Rᵐᵒᵖ I` over the opposite ring, taking elements to their opposite. -/ @[simps! +simpRhs apply symm_apply] protected noncomputable def opRingEquiv [AddCommMagma G] : k[G]ᵐᵒᵖ ≃+* kᵐᵒᵖ[G] := { opAddEquiv.symm.trans (mapRange.addEquiv (opAddEquiv : k ≃+ kᵐᵒᵖ)) with map_mul' := by let f : k[G]ᵐᵒᵖ ≃+ kᵐᵒᵖ[G] := opAddEquiv.symm.trans (mapRange.addEquiv (opAddEquiv : k ≃+ kᵐᵒᵖ)) change ∀ (x y : k[G]ᵐᵒᵖ), f (x * y) = f x * f y rw [← AddEquiv.coe_toAddMonoidHom, AddMonoidHom.map_mul_iff] ext i₁ r₁ i₂ r₂ : 6 dsimp only [f, AddMonoidHom.compr₂_apply, AddMonoidHom.compl₂_apply, AddMonoidHom.comp_apply, AddMonoidHom.coe_coe, AddEquiv.toAddMonoidHom_eq_coe, AddEquiv.coe_addMonoidHom_trans, singleAddHom_apply, opAddEquiv_apply, opAddEquiv_symm_apply, AddMonoidHom.coe_mul, AddMonoidHom.coe_mulLeft, unop_mul, unop_op] -- Defeq abuse. Probably `k[G]` vs `G →₀ k`. erw [mapRange.addEquiv_apply, mapRange.addEquiv_apply, mapRange.addEquiv_apply] rw [single_mul_single, mapRange_single, mapRange_single, mapRange_single, single_mul_single] simp only [opAddEquiv_apply, op_mul, add_comm] } -- Not `@[simp]` because the LHS simplifies further. -- TODO: the LHS simplifies to `Finsupp.single`, which implies there's some defeq abuse going on. theorem opRingEquiv_single [AddCommMagma G] (r : k) (x : G) : AddMonoidAlgebra.opRingEquiv (op (single x r)) = single x (op r) := by simp theorem opRingEquiv_symm_single [AddCommMagma G] (r : kᵐᵒᵖ) (x : Gᵐᵒᵖ) : AddMonoidAlgebra.opRingEquiv.symm (single x r) = op (single x r.unop) := by simp end AddMonoidAlgebra
.lake/packages/mathlib/Mathlib/Algebra/MonoidAlgebra/Support.lean
import Mathlib.Algebra.Group.Embedding import Mathlib.Algebra.MonoidAlgebra.Module import Mathlib.LinearAlgebra.Finsupp.Supported import Mathlib.Algebra.Group.Pointwise.Finset.Basic /-! # Lemmas about the support of a finitely supported function -/ open scoped Pointwise universe u₁ u₂ u₃ namespace MonoidAlgebra open Finset Finsupp variable {k : Type u₁} {G : Type u₂} [Semiring k] theorem support_mul [Mul G] [DecidableEq G] (a b : MonoidAlgebra k G) : (a * b).support ⊆ a.support * b.support := by rw [MonoidAlgebra.mul_def] exact support_sum.trans <| biUnion_subset.2 fun _x hx ↦ support_sum.trans <| biUnion_subset.2 fun _y hy ↦ support_single_subset.trans <| singleton_subset_iff.2 <| mem_image₂_of_mem hx hy theorem support_single_mul_subset [DecidableEq G] [Mul G] (f : MonoidAlgebra k G) (r : k) (a : G) : (single a r * f : MonoidAlgebra k G).support ⊆ Finset.image (a * ·) f.support := (support_mul _ _).trans <| (Finset.image₂_subset_right support_single_subset).trans <| by rw [Finset.image₂_singleton_left] theorem support_mul_single_subset [DecidableEq G] [Mul G] (f : MonoidAlgebra k G) (r : k) (a : G) : (f * single a r).support ⊆ Finset.image (· * a) f.support := (support_mul _ _).trans <| (Finset.image₂_subset_left support_single_subset).trans <| by rw [Finset.image₂_singleton_right] theorem support_single_mul_eq_image [DecidableEq G] [Mul G] (f : MonoidAlgebra k G) {r : k} (hr : ∀ y, r * y = 0 ↔ y = 0) {x : G} (lx : IsLeftRegular x) : (single x r * f : MonoidAlgebra k G).support = Finset.image (x * ·) f.support := by refine subset_antisymm (support_single_mul_subset f _ _) fun y hy => ?_ obtain ⟨y, yf, rfl⟩ : ∃ a : G, a ∈ f.support ∧ x * a = y := by grind simp only [mul_apply, mem_support_iff.mp yf, hr, mem_support_iff, sum_single_index, Finsupp.sum_ite_eq', Ne, not_false_iff, if_true, zero_mul, ite_self, sum_zero, lx.eq_iff] theorem support_mul_single_eq_image [DecidableEq G] [Mul G] (f : MonoidAlgebra k G) {r : k} (hr : ∀ y, y * r = 0 ↔ y = 0) {x : G} (rx : IsRightRegular x) : (f * single x r).support = Finset.image (· * x) f.support := by refine subset_antisymm (support_mul_single_subset f _ _) fun y hy => ?_ obtain ⟨y, yf, rfl⟩ : ∃ a : G, a ∈ f.support ∧ a * x = y := by grind simp only [mul_apply, mem_support_iff.mp yf, hr, mem_support_iff, sum_single_index, Finsupp.sum_ite_eq', Ne, not_false_iff, if_true, mul_zero, ite_self, rx.eq_iff] theorem support_mul_single [Mul G] [IsRightCancelMul G] (f : MonoidAlgebra k G) (r : k) (hr : ∀ y, y * r = 0 ↔ y = 0) (x : G) : (f * single x r).support = f.support.map (mulRightEmbedding x) := by classical ext simp only [support_mul_single_eq_image f hr (IsRightRegular.all x), mem_image, mem_map, mulRightEmbedding_apply] theorem support_single_mul [Mul G] [IsLeftCancelMul G] (f : MonoidAlgebra k G) (r : k) (hr : ∀ y, r * y = 0 ↔ y = 0) (x : G) : (single x r * f : MonoidAlgebra k G).support = f.support.map (mulLeftEmbedding x) := by classical ext simp only [support_single_mul_eq_image f hr (IsLeftRegular.all x), mem_image, mem_map, mulLeftEmbedding_apply] lemma support_one_subset [One G] : (1 : MonoidAlgebra k G).support ⊆ 1 := Finsupp.support_single_subset @[simp] lemma support_one [One G] [NeZero (1 : k)] : (1 : MonoidAlgebra k G).support = 1 := Finsupp.support_single_ne_zero _ one_ne_zero section Span variable [MulOneClass G] /-- An element of `MonoidAlgebra k G` is in the subalgebra generated by its support. -/ theorem mem_span_support (f : MonoidAlgebra k G) : f ∈ Submodule.span k (of k G '' (f.support : Set G)) := by erw [of, MonoidHom.coe_mk, ← supported_eq_span_single, Finsupp.mem_supported] end Span end MonoidAlgebra namespace AddMonoidAlgebra open Finset Finsupp MulOpposite variable {k : Type u₁} {G : Type u₂} [Semiring k] theorem support_mul [DecidableEq G] [Add G] (a b : k[G]) : (a * b).support ⊆ a.support + b.support := @MonoidAlgebra.support_mul k (Multiplicative G) _ _ _ _ _ theorem support_mul_single [Add G] [IsRightCancelAdd G] (f : k[G]) (r : k) (hr : ∀ y, y * r = 0 ↔ y = 0) (x : G) : (f * single x r : k[G]).support = f.support.map (addRightEmbedding x) := MonoidAlgebra.support_mul_single (G := Multiplicative G) _ _ hr _ theorem support_single_mul [Add G] [IsLeftCancelAdd G] (f : k[G]) (r : k) (hr : ∀ y, r * y = 0 ↔ y = 0) (x : G) : (single x r * f : k[G]).support = f.support.map (addLeftEmbedding x) := MonoidAlgebra.support_single_mul (G := Multiplicative G) _ _ hr _ lemma support_one_subset [Zero G] : (1 : k[G]).support ⊆ 0 := Finsupp.support_single_subset @[simp] lemma support_one [Zero G] [NeZero (1 : k)] : (1 : k[G]).support = 0 := Finsupp.support_single_ne_zero _ one_ne_zero section Span /-- An element of `k[G]` is in the submodule generated by its support. -/ theorem mem_span_support [AddZeroClass G] (f : k[G]) : f ∈ Submodule.span k (of k G '' (f.support : Set G)) := by erw [of, MonoidHom.coe_mk, ← Finsupp.supported_eq_span_single, Finsupp.mem_supported] /-- An element of `k[G]` is in the subalgebra generated by its support, using unbundled inclusion. -/ theorem mem_span_support' (f : k[G]) : f ∈ Submodule.span k (of' k G '' (f.support : Set G)) := by delta of' rw [← Finsupp.supported_eq_span_single, Finsupp.mem_supported] end Span end AddMonoidAlgebra
.lake/packages/mathlib/Mathlib/Algebra/MonoidAlgebra/Module.lean
import Mathlib.Algebra.Module.BigOperators import Mathlib.Algebra.Module.Submodule.Basic import Mathlib.Algebra.MonoidAlgebra.Lift import Mathlib.LinearAlgebra.Finsupp.LSum /-! # Module structure on monoid algebras ## Main results * `MonoidAlgebra.module`, `AddMonoidAlgebra.module`: lift a module structure to monoid algebras -/ assert_not_exists NonUnitalAlgHom AlgEquiv noncomputable section open Finsupp hiding single universe u₁ u₂ u₃ u₄ variable (k : Type u₁) (G : Type u₂) (H : Type*) {R S M : Type*} /-! ### Multiplicative monoids -/ namespace MonoidAlgebra variable {k G} section SMul variable {S : Type*} instance noZeroSMulDivisors [Zero R] [Semiring k] [SMulZeroClass R k] [NoZeroSMulDivisors R k] : NoZeroSMulDivisors R (MonoidAlgebra k G) := Finsupp.noZeroSMulDivisors instance distribMulAction [Monoid R] [Semiring k] [DistribMulAction R k] : DistribMulAction R (MonoidAlgebra k G) := Finsupp.distribMulAction G k instance module [Semiring R] [Semiring k] [Module R k] : Module R (MonoidAlgebra k G) := Finsupp.module G k instance faithfulSMul [Semiring k] [SMulZeroClass R k] [FaithfulSMul R k] [Nonempty G] : FaithfulSMul R (MonoidAlgebra k G) := Finsupp.faithfulSMul /-- This is not an instance as it conflicts with `MonoidAlgebra.distribMulAction` when `G = kˣ`. -/ def comapDistribMulActionSelf [Group G] [Semiring k] : DistribMulAction G (MonoidAlgebra k G) := Finsupp.comapDistribMulAction end SMul /-! #### Copies of `ext` lemmas and bundled `single`s from `Finsupp` As `MonoidAlgebra` is a type synonym, `ext` will not unfold it to find `ext` lemmas. We need bundled version of `Finsupp.single` with the right types to state these lemmas. It is good practice to have those, regardless of the `ext` issue. -/ section ExtLemmas /-- A copy of `Finsupp.distribMulActionHom_ext'` for `MonoidAlgebra`. -/ @[ext] theorem distribMulActionHom_ext' {N : Type*} [Monoid R] [Semiring k] [AddMonoid N] [DistribMulAction R N] [DistribMulAction R k] {f g : MonoidAlgebra k G →+[R] N} (h : ∀ a : G, f.comp (DistribMulActionHom.single (M := k) a) = g.comp (DistribMulActionHom.single a)) : f = g := Finsupp.distribMulActionHom_ext' h /-- A copy of `Finsupp.lsingle` for `MonoidAlgebra`. -/ abbrev lsingle [Semiring R] [Semiring k] [Module R k] (a : G) : k →ₗ[R] MonoidAlgebra k G := Finsupp.lsingle a @[simp] lemma lsingle_apply [Semiring R] [Semiring k] [Module R k] (a : G) (b : k) : lsingle (R := R) a b = single a b := rfl /-- A copy of `Finsupp.lhom_ext'` for `MonoidAlgebra`. -/ @[ext high] lemma lhom_ext' {N : Type*} [Semiring R] [Semiring k] [AddCommMonoid N] [Module R N] [Module R k] ⦃f g : MonoidAlgebra k G →ₗ[R] N⦄ (H : ∀ (x : G), LinearMap.comp f (lsingle x) = LinearMap.comp g (lsingle x)) : f = g := Finsupp.lhom_ext' H end ExtLemmas section MiscTheorems variable [Semiring k] theorem smul_of [MulOneClass G] (g : G) (r : k) : r • of k G g = single g r := by simp theorem liftNC_smul [MulOneClass G] {R : Type*} [Semiring R] (f : k →+* R) (g : G →* R) (c : k) (φ : MonoidAlgebra k G) : liftNC (f : k →+ R) g (c • φ) = f c * liftNC (f : k →+ R) g φ := by suffices (liftNC (↑f) g).comp (smulAddHom k (MonoidAlgebra k G) c) = (AddMonoidHom.mulLeft (f c)).comp (liftNC (↑f) g) from DFunLike.congr_fun this φ ext simp_rw [AddMonoidHom.comp_apply, singleAddHom_apply, smulAddHom_apply, AddMonoidHom.coe_mulLeft, smul_single', liftNC_single, AddMonoidHom.coe_coe, map_mul, mul_assoc] end MiscTheorems /-! #### Non-unital, non-associative algebra structure -/ section NonUnitalNonAssocAlgebra variable (k) [Semiring k] [DistribSMul R k] [Mul G] instance isScalarTower_self [IsScalarTower R k k] : IsScalarTower R (MonoidAlgebra k G) (MonoidAlgebra k G) where smul_assoc t a b := by ext -- Porting note: `refine` & `rw` are required because `simp` behaves differently. classical simp only [smul_eq_mul, mul_apply] rw [coe_smul] refine Eq.trans (sum_smul_index' (g := a) (b := t) ?_) ?_ <;> simp only [mul_apply, Finsupp.smul_sum, smul_ite, smul_mul_assoc, zero_mul, ite_self, imp_true_iff, sum_zero, Pi.smul_apply, smul_zero] /-- Note that if `k` is a `CommSemiring` then we have `SMulCommClass k k k` and so we can take `R = k` in the below. In other words, if the coefficients are commutative amongst themselves, they also commute with the algebra multiplication. -/ instance smulCommClass_self [SMulCommClass R k k] : SMulCommClass R (MonoidAlgebra k G) (MonoidAlgebra k G) where smul_comm t a b := by ext -- Porting note: `refine` & `rw` are required because `simp` behaves differently. classical simp only [smul_eq_mul, mul_apply] rw [coe_smul] refine Eq.symm (Eq.trans (congr_arg (sum a) (funext₂ fun a₁ b₁ => sum_smul_index' (g := b) (b := t) ?_)) ?_) <;> simp only [mul_apply, Finsupp.sum, Finset.smul_sum, smul_ite, mul_smul_comm, imp_true_iff, ite_eq_right_iff, Pi.smul_apply, mul_zero, smul_zero] instance smulCommClass_symm_self [SMulCommClass k R k] : SMulCommClass (MonoidAlgebra k G) R (MonoidAlgebra k G) := ⟨fun t a b => by haveI := SMulCommClass.symm k R k rw [← smul_comm]⟩ end NonUnitalNonAssocAlgebra section Submodule variable [CommSemiring k] [Monoid G] variable {V : Type*} [AddCommMonoid V] variable [Module k V] [Module (MonoidAlgebra k G) V] [IsScalarTower k (MonoidAlgebra k G) V] /-- A submodule over `k` which is stable under scalar multiplication by elements of `G` is a submodule over `MonoidAlgebra k G` -/ def submoduleOfSMulMem (W : Submodule k V) (h : ∀ (g : G) (v : V), v ∈ W → of k G g • v ∈ W) : Submodule (MonoidAlgebra k G) V where carrier := W zero_mem' := W.zero_mem' add_mem' := W.add_mem' smul_mem' := by intro f v hv rw [← Finsupp.sum_single f, Finsupp.sum, Finset.sum_smul] simp_rw [← smul_of, smul_assoc] exact Submodule.sum_smul_mem W _ fun g _ => h g v hv end Submodule end MonoidAlgebra /-! ### Additive monoids -/ namespace AddMonoidAlgebra variable {k G} section SMul variable {S : Type*} instance distribMulAction [Monoid R] [Semiring k] [DistribMulAction R k] : DistribMulAction R k[G] := Finsupp.distribMulAction G k instance faithfulSMul [Semiring k] [SMulZeroClass R k] [FaithfulSMul R k] [Nonempty G] : FaithfulSMul R k[G] := Finsupp.faithfulSMul instance module [Semiring R] [Semiring k] [Module R k] : Module R k[G] := Finsupp.module G k /-! It is hard to state the equivalent of `DistribMulAction G k[G]` because we've never discussed actions of additive groups. -/ end SMul /-! #### Semiring structure -/ section Semiring instance noZeroSMulDivisors [Zero R] [Semiring k] [SMulZeroClass R k] [NoZeroSMulDivisors R k] : NoZeroSMulDivisors R k[G] := Finsupp.noZeroSMulDivisors end Semiring /-! #### Copies of `ext` lemmas and bundled `single`s from `Finsupp` As `AddMonoidAlgebra` is a type synonym, `ext` will not unfold it to find `ext` lemmas. We need bundled version of `Finsupp.single` with the right types to state these lemmas. It is good practice to have those, regardless of the `ext` issue. -/ section ExtLemmas /-- A copy of `Finsupp.distribMulActionHom_ext'` for `AddMonoidAlgebra`. -/ @[ext] theorem distribMulActionHom_ext' {N : Type*} [Monoid R] [Semiring k] [AddMonoid N] [DistribMulAction R N] [DistribMulAction R k] {f g : AddMonoidAlgebra k G →+[R] N} (h : ∀ a : G, f.comp (DistribMulActionHom.single (M := k) a) = g.comp (DistribMulActionHom.single a)) : f = g := Finsupp.distribMulActionHom_ext' h /-- A copy of `Finsupp.lsingle` for `AddMonoidAlgebra`. -/ abbrev lsingle [Semiring R] [Semiring k] [Module R k] (a : G) : k →ₗ[R] AddMonoidAlgebra k G := Finsupp.lsingle a @[simp] lemma lsingle_apply [Semiring R] [Semiring k] [Module R k] (a : G) (b : k) : lsingle (R := R) a b = single a b := rfl /-- A copy of `Finsupp.lhom_ext'` for `AddMonoidAlgebra`. -/ @[ext high] lemma lhom_ext' {N : Type*} [Semiring R] [Semiring k] [AddCommMonoid N] [Module R N] [Module R k] ⦃f g : AddMonoidAlgebra k G →ₗ[R] N⦄ (H : ∀ (x : G), LinearMap.comp f (lsingle x) = LinearMap.comp g (lsingle x)) : f = g := Finsupp.lhom_ext' H end ExtLemmas section MiscTheorems variable [Semiring k] theorem liftNC_smul {R : Type*} [AddZeroClass G] [Semiring R] (f : k →+* R) (g : Multiplicative G →* R) (c : k) (φ : MonoidAlgebra k G) : liftNC (f : k →+ R) g (c • φ) = f c * liftNC (f : k →+ R) g φ := @MonoidAlgebra.liftNC_smul k (Multiplicative G) _ _ _ _ f g c φ end MiscTheorems end AddMonoidAlgebra namespace AddMonoidAlgebra variable {k G H} /-! #### Non-unital, non-associative algebra structure -/ section NonUnitalNonAssocAlgebra variable (k) [Semiring k] [DistribSMul R k] [Add G] instance isScalarTower_self [IsScalarTower R k k] : IsScalarTower R k[G] k[G] := @MonoidAlgebra.isScalarTower_self k (Multiplicative G) R _ _ _ _ /-- Note that if `k` is a `CommSemiring` then we have `SMulCommClass k k k` and so we can take `R = k` in the below. In other words, if the coefficients are commutative amongst themselves, they also commute with the algebra multiplication. -/ instance smulCommClass_self [SMulCommClass R k k] : SMulCommClass R k[G] k[G] := @MonoidAlgebra.smulCommClass_self k (Multiplicative G) R _ _ _ _ instance smulCommClass_symm_self [SMulCommClass k R k] : SMulCommClass k[G] R k[G] := @MonoidAlgebra.smulCommClass_symm_self k (Multiplicative G) R _ _ _ _ end NonUnitalNonAssocAlgebra end AddMonoidAlgebra
.lake/packages/mathlib/Mathlib/Algebra/MonoidAlgebra/MapDomain.lean
import Mathlib.Algebra.MonoidAlgebra.Lift /-! # MonoidAlgebra.mapDomain -/ assert_not_exists NonUnitalAlgHom AlgEquiv open Function open Finsupp hiding single mapDomain noncomputable section variable {R S M N O : Type*} /-! ### Multiplicative monoids -/ namespace MonoidAlgebra variable [Semiring R] [Semiring S] {f : M → N} {a : M} {r : R} /-- Given a function `f : M → N` between magmas, return the corresponding map `R[M] → R[N]` obtained by summing the coefficients along each fiber of `f`. -/ @[to_additive /-- Given a function `f : M → N` between magmas, return the corresponding map `R[M] → R[N]` obtained by summing the coefficients along each fiber of `f`. -/] abbrev mapDomain (f : M → N) (v : MonoidAlgebra R M) : MonoidAlgebra R N := Finsupp.mapDomain f v @[to_additive] lemma mapDomain_sum (f : M → N) (s : MonoidAlgebra S M) (v : M → S → MonoidAlgebra R M) : mapDomain f (s.sum v) = s.sum fun a b ↦ mapDomain f (v a b) := Finsupp.mapDomain_sum @[to_additive] lemma mapDomain_single : mapDomain f (single a r) = single (f a) r := Finsupp.mapDomain_single @[to_additive] lemma mapDomain_injective (hf : Injective f) : Injective (mapDomain (R := R) f) := Finsupp.mapDomain_injective hf @[to_additive (dont_translate := R) (attr := simp) mapDomain_one] theorem mapDomain_one [One M] [One N] {F : Type*} [FunLike F M N] [OneHomClass F M N] (f : F) : mapDomain f (1 : MonoidAlgebra R M) = (1 : MonoidAlgebra R N) := by simp [one_def] @[to_additive (dont_translate := R) mapDomain_mul] theorem mapDomain_mul [Mul M] [Mul N] {F : Type*} [FunLike F M N] [MulHomClass F M N] (f : F) (x y : MonoidAlgebra R M) : mapDomain f (x * y) = mapDomain f x * mapDomain f y := by simp [mul_def, mapDomain_sum, add_mul, mul_add, sum_mapDomain_index] variable [Monoid M] [Monoid N] [Monoid O] variable (R) in /-- If `f : G → H` is a multiplicative homomorphism between two monoids, then `Finsupp.mapDomain f` is a ring homomorphism between their monoid algebras. -/ @[to_additive (attr := simps) /-- If `f : G → H` is a multiplicative homomorphism between two monoids, then `Finsupp.mapDomain f` is a ring homomorphism between their monoid algebras. -/] def mapDomainRingHom {F : Type*} [FunLike F M N] [MonoidHomClass F M N] (f : F) : MonoidAlgebra R M →+* MonoidAlgebra R N where __ : MonoidAlgebra R M →+ MonoidAlgebra R N := Finsupp.mapDomain.addMonoidHom f map_one' := mapDomain_one f map_mul' := mapDomain_mul f attribute [local ext high] ringHom_ext @[to_additive (dont_translate := R) (attr := simp)] lemma mapDomainRingHom_id : mapDomainRingHom R (MonoidHom.id M) = .id (MonoidAlgebra R M) := by ext <;> simp @[to_additive (dont_translate := R) (attr := simp)] lemma mapDomainRingHom_comp (f : N →* O) (g : M →* N) : mapDomainRingHom R (f.comp g) = (mapDomainRingHom R f).comp (mapDomainRingHom R g) := by ext <;> simp @[to_additive (dont_translate := R S)] lemma mapRangeRingHom_comp_mapDomainRingHom (f : R →+* S) (g : M →* N) : (mapRangeRingHom N f).comp (mapDomainRingHom R g) = (mapDomainRingHom S g).comp (mapRangeRingHom M f) := by aesop end MonoidAlgebra /-! #### Conversions between `AddMonoidAlgebra` and `MonoidAlgebra` We have not defined `k[G] = MonoidAlgebra k (Multiplicative G)` because historically this caused problems; since the changes that have made `nsmul` definitional, this would be possible, but for now we just construct the ring isomorphisms using `RingEquiv.refl _`. -/ variable (k G) in /-- The equivalence between `AddMonoidAlgebra` and `MonoidAlgebra` in terms of `Multiplicative` -/ protected def AddMonoidAlgebra.toMultiplicative [Semiring k] [Add G] : AddMonoidAlgebra k G ≃+* MonoidAlgebra k (Multiplicative G) where __ := Finsupp.domCongr Multiplicative.ofAdd toFun := equivMapDomain Multiplicative.ofAdd map_mul' x y := by repeat' rw [equivMapDomain_eq_mapDomain (M := k)] dsimp [Multiplicative.ofAdd] exact MonoidAlgebra.mapDomain_mul (M := Multiplicative G) (MulHom.id (Multiplicative G)) x y variable (k G) in /-- The equivalence between `MonoidAlgebra` and `AddMonoidAlgebra` in terms of `Additive` -/ protected def MonoidAlgebra.toAdditive [Semiring k] [Mul G] : MonoidAlgebra k G ≃+* AddMonoidAlgebra k (Additive G) where __ := Finsupp.domCongr Additive.ofMul toFun := equivMapDomain Additive.ofMul map_mul' x y := by repeat' rw [equivMapDomain_eq_mapDomain (M := k)] dsimp [Additive.ofMul] convert MonoidAlgebra.mapDomain_mul (MulHom.id G) x y
.lake/packages/mathlib/Mathlib/Algebra/MonoidAlgebra/NoZeroDivisors.lean
import Mathlib.Algebra.Group.UniqueProds.Basic import Mathlib.Algebra.MonoidAlgebra.Opposite /-! # Variations on non-zero divisors in `AddMonoidAlgebra`s This file studies the interaction between typeclass assumptions on two Types `R` and `A` and whether `R[A]` has non-zero zero-divisors. For some background on related questions, see [Kaplansky's Conjectures](https://en.wikipedia.org/wiki/Kaplansky%27s_conjectures), especially the *zero divisor conjecture*. _Conjecture._ Let `K` be a field, and `G` a torsion-free group. The group ring `K[G]` does not contain nontrivial zero divisors, that is, it is a domain. In this file we show that if `R` satisfies `NoZeroDivisors` and `A` is a grading type satisfying `UniqueProds A` (resp. `UniqueSums A`), then `MonoidAlgebra R A` (resp. `R[A]`) also satisfies `NoZeroDivisors`. Because of the instances to `UniqueProds/Sums`, we obtain a formalization of the well-known result that if `R` is a field and `A` is a left-ordered group, then `R[A]` contains no non-zero zero-divisors. The actual assumptions on `R` are weaker. ## Main results * `MonoidAlgebra.mul_apply_mul_eq_mul_of_uniqueMul` and `AddMonoidAlgebra.mul_apply_add_eq_mul_of_uniqueAdd` general sufficient results stating that certain monomials in a product have as coefficient a product of coefficients of the factors. * The instance showing that `Semiring R, NoZeroDivisors R, Mul A, UniqueProds A` imply `NoZeroDivisors (MonoidAlgebra R A)`. * The instance showing that `Semiring R, NoZeroDivisors R, Add A, UniqueSums A` imply `NoZeroDivisors R[A]`. TODO: move the rest of the docs to UniqueProds? * `NoZeroDivisors.of_left_ordered` shows that if `R` is a semiring with no non-zero zero-divisors, `A` is a linearly ordered, add right cancel semigroup with strictly monotone left addition, then `R[A]` has no non-zero zero-divisors. * `NoZeroDivisors.of_right_ordered` shows that if `R` is a semiring with no non-zero zero-divisors, `A` is a linearly ordered, add left cancel semigroup with strictly monotone right addition, then `R[A]` has no non-zero zero-divisors. The conditions on `A` imposed in `NoZeroDivisors.of_left_ordered` are sometimes referred to as `left-ordered`. The conditions on `A` imposed in `NoZeroDivisors.of_right_ordered` are sometimes referred to as `right-ordered`. These conditions are sufficient, but not necessary. As mentioned above, *Kaplansky's Conjecture* asserts that `A` being torsion-free may be enough. -/ open Finsupp variable {R A : Type*} section Semiring variable [Semiring R] namespace MonoidAlgebra /-- The coefficient of a monomial in a product `f * g` that can be reached in at most one way as a product of monomials in the supports of `f` and `g` is a product. -/ theorem mul_apply_mul_eq_mul_of_uniqueMul [Mul A] {f g : MonoidAlgebra R A} {a0 b0 : A} (h : UniqueMul f.support g.support a0 b0) : (f * g) (a0 * b0) = f a0 * g b0 := by classical simp_rw [mul_apply, sum, ← Finset.sum_product'] refine (Finset.sum_eq_single (a0, b0) ?_ ?_).trans (if_pos rfl) <;> simp_rw [Finset.mem_product] · refine fun ab hab hne ↦ if_neg (fun he ↦ hne <| Prod.ext ?_ ?_) exacts [(h hab.1 hab.2 he).1, (h hab.1 hab.2 he).2] · refine fun hnotMem ↦ ite_eq_right_iff.mpr (fun _ ↦ ?_) rcases not_and_or.mp hnotMem with af | bg · rw [notMem_support_iff.mp af, zero_mul] · rw [notMem_support_iff.mp bg, mul_zero] instance [NoZeroDivisors R] [Mul A] [UniqueProds A] : NoZeroDivisors (MonoidAlgebra R A) where eq_zero_or_eq_zero_of_mul_eq_zero {a b} ab := by contrapose! ab obtain ⟨da, a0, db, b0, h⟩ := UniqueProds.uniqueMul_of_nonempty (support_nonempty_iff.mpr ab.1) (support_nonempty_iff.mpr ab.2) refine support_nonempty_iff.mp ⟨da * db, ?_⟩ rw [mem_support_iff] at a0 b0 ⊢ exact mul_apply_mul_eq_mul_of_uniqueMul h ▸ mul_ne_zero a0 b0 instance [IsCancelAdd R] [IsLeftCancelMulZero R] [Mul A] [UniqueProds A] : IsLeftCancelMulZero (MonoidAlgebra R A) where mul_left_cancel_of_ne_zero {f} hf {g₁ g₂} eq := by classical induction hg : g₁.support ∪ g₂.support using Finset.eraseInduction generalizing g₁ g₂ with | _ s ih => obtain h | h := s.eq_empty_or_nonempty <;> subst s · simp_rw [Finset.union_eq_empty, support_eq_empty] at h; exact h.1.trans h.2.symm have ⟨af, haf, ag, hag, uniq⟩ := UniqueProds.uniqueMul_of_nonempty (support_nonempty_iff.2 hf) h have h := mul_apply_mul_eq_mul_of_uniqueMul (uniq.mono subset_rfl Finset.subset_union_left) dsimp only at eq rw [eq, mul_apply_mul_eq_mul_of_uniqueMul (uniq.mono subset_rfl Finset.subset_union_right)] at h have := mul_left_cancel₀ (mem_support_iff.mp haf) h rw [← g₁.erase_add_single ag, ← g₂.erase_add_single ag, this] at eq ⊢ simp_rw [mul_add, add_right_cancel_iff] at eq rw [ih ag hag eq] simp_rw [support_erase, Finset.erase_union_distrib] instance [IsCancelAdd R] [IsRightCancelMulZero R] [Mul A] [UniqueProds A] : IsRightCancelMulZero (MonoidAlgebra R A) := MulOpposite.isLeftCancelMulZero_iff.mp <| MonoidAlgebra.opRingEquiv.injective.isLeftCancelMulZero _ (map_zero _) (map_mul _) instance [IsCancelAdd R] [IsCancelMulZero R] [Mul A] [UniqueProds A] : IsCancelMulZero (MonoidAlgebra R A) where instance [IsCancelAdd R] [IsDomain R] [Monoid A] [UniqueProds A] : IsDomain (MonoidAlgebra R A) where end MonoidAlgebra namespace AddMonoidAlgebra /-- The coefficient of a monomial in a product `f * g` that can be reached in at most one way as a product of monomials in the supports of `f` and `g` is a product. -/ theorem mul_apply_add_eq_mul_of_uniqueAdd [Add A] {f g : R[A]} {a0 b0 : A} (h : UniqueAdd f.support g.support a0 b0) : (f * g) (a0 + b0) = f a0 * g b0 := MonoidAlgebra.mul_apply_mul_eq_mul_of_uniqueMul (A := Multiplicative A) h instance [NoZeroDivisors R] [Add A] [UniqueSums A] : NoZeroDivisors R[A] := inferInstanceAs (NoZeroDivisors (MonoidAlgebra R (Multiplicative A))) instance [IsCancelAdd R] [IsLeftCancelMulZero R] [Add A] [UniqueSums A] : IsLeftCancelMulZero R[A] := inferInstanceAs (IsLeftCancelMulZero (MonoidAlgebra R (Multiplicative A))) instance [IsCancelAdd R] [IsRightCancelMulZero R] [Add A] [UniqueSums A] : IsRightCancelMulZero R[A] := inferInstanceAs (IsRightCancelMulZero (MonoidAlgebra R (Multiplicative A))) instance [IsCancelAdd R] [IsCancelMulZero R] [Add A] [UniqueSums A] : IsCancelMulZero R[A] where instance [IsCancelAdd R] [IsDomain R] [AddMonoid A] [UniqueSums A] : IsDomain R[A] where end AddMonoidAlgebra end Semiring
.lake/packages/mathlib/Mathlib/Algebra/MonoidAlgebra/Basic.lean
import Mathlib.Algebra.Algebra.Equiv import Mathlib.Algebra.Algebra.NonUnitalHom import Mathlib.Algebra.Algebra.Tower import Mathlib.Algebra.Module.BigOperators import Mathlib.Algebra.MonoidAlgebra.MapDomain import Mathlib.Algebra.MonoidAlgebra.Module import Mathlib.Data.Finsupp.SMul import Mathlib.LinearAlgebra.Finsupp.SumProd /-! # Monoid algebras -/ noncomputable section open Finset open Finsupp hiding single mapDomain variable {k G H R S T A B M N : Type*} /-! ### Multiplicative monoids -/ namespace MonoidAlgebra /-! #### Non-unital, non-associative algebra structure -/ section NonUnitalNonAssocAlgebra variable (k) [Semiring k] [DistribSMul R k] [Mul G] [NonUnitalNonAssocSemiring A] /-- A non_unital `k`-algebra homomorphism from `MonoidAlgebra k G` is uniquely defined by its values on the functions `single a 1`. -/ @[to_additive (dont_translate := k) /-- A non_unital `k`-algebra homomorphism from `k[G]` is uniquely defined by its values on the functions `single a 1`. -/] theorem nonUnitalAlgHom_ext [DistribMulAction k A] {φ₁ φ₂ : MonoidAlgebra k G →ₙₐ[k] A} (h : ∀ x, φ₁ (single x 1) = φ₂ (single x 1)) : φ₁ = φ₂ := NonUnitalAlgHom.to_distribMulActionHom_injective <| Finsupp.distribMulActionHom_ext' fun a => DistribMulActionHom.ext_ring (h a) /-- See note [partially-applied ext lemmas]. -/ @[ext high] theorem nonUnitalAlgHom_ext' [DistribMulAction k A] {φ₁ φ₂ : MonoidAlgebra k G →ₙₐ[k] A} (h : φ₁.toMulHom.comp (ofMagma k G) = φ₂.toMulHom.comp (ofMagma k G)) : φ₁ = φ₂ := nonUnitalAlgHom_ext k <| DFunLike.congr_fun h /-- The functor `G ↦ MonoidAlgebra k G`, from the category of magmas to the category of non-unital, non-associative algebras over `k` is adjoint to the forgetful functor in the other direction. -/ @[simps apply_apply symm_apply] def liftMagma [Module k A] [IsScalarTower k A A] [SMulCommClass k A A] : (G →ₙ* A) ≃ (MonoidAlgebra k G →ₙₐ[k] A) where toFun f := { liftAddHom fun x => (smulAddHom k A).flip (f x) with toFun := fun a => a.sum fun m t => t • f m map_smul' := fun t' a => by rw [Finsupp.smul_sum, sum_smul_index'] · simp_rw [smul_assoc, MonoidHom.id_apply] · intro m exact zero_smul k (f m) map_mul' := fun a₁ a₂ => by let g : G → k → A := fun m t => t • f m have h₁ : ∀ m, g m 0 = 0 := by intro m exact zero_smul k (f m) have h₂ : ∀ (m) (t₁ t₂ : k), g m (t₁ + t₂) = g m t₁ + g m t₂ := by intros rw [← add_smul] -- Porting note: `reducible` cannot be `local` so proof gets long. simp_rw [Finsupp.mul_sum, Finsupp.sum_mul, smul_mul_smul_comm, ← f.map_mul, mul_def, sum_comm a₂ a₁] rw [sum_sum_index h₁ h₂]; congr; ext rw [sum_sum_index h₁ h₂]; congr; ext rw [sum_single_index (h₁ _)] } invFun F := F.toMulHom.comp (ofMagma k G) left_inv f := by ext m simp only [NonUnitalAlgHom.coe_mk, ofMagma_apply, NonUnitalAlgHom.toMulHom_eq_coe, sum_single_index, Function.comp_apply, one_smul, zero_smul, MulHom.coe_comp, NonUnitalAlgHom.coe_to_mulHom] right_inv F := by ext m simp only [NonUnitalAlgHom.coe_mk, ofMagma_apply, NonUnitalAlgHom.toMulHom_eq_coe, sum_single_index, Function.comp_apply, one_smul, zero_smul, MulHom.coe_comp, NonUnitalAlgHom.coe_to_mulHom] end NonUnitalNonAssocAlgebra /-! #### Algebra structure -/ section Algebra /-- The instance `Algebra k (MonoidAlgebra A G)` whenever we have `Algebra k A`. In particular this provides the instance `Algebra k (MonoidAlgebra k G)`. -/ @[to_additive (dont_translate := k A) /-- The instance `Algebra R k[G]` whenever we have `Algebra R k`. In particular this provides the instance `Algebra k k[G]`. -/] instance algebra {A : Type*} [CommSemiring k] [Semiring A] [Algebra k A] [Monoid G] : Algebra k (MonoidAlgebra A G) where algebraMap := singleOneRingHom.comp (algebraMap k A) smul_def' := fun r a => by ext dsimp rw [single_one_mul_apply, Algebra.smul_def] commutes' := fun r f => by ext dsimp rw [single_one_mul_apply, mul_single_one_apply, Algebra.commutes] /-- `Finsupp.single 1` as an `AlgHom` -/ @[to_additive (dont_translate := k A) (attr := simps! apply) /-- `Finsupp.single 0` as an `AlgHom` -/] def singleOneAlgHom {A : Type*} [CommSemiring k] [Semiring A] [Algebra k A] [Monoid G] : A →ₐ[k] MonoidAlgebra A G := { singleOneRingHom with commutes' := fun r => by ext simp rfl } @[to_additive (attr := simp)] theorem coe_algebraMap {A : Type*} [CommSemiring k] [Semiring A] [Algebra k A] [Monoid G] : ⇑(algebraMap k (MonoidAlgebra A G)) = single 1 ∘ algebraMap k A := rfl theorem single_eq_algebraMap_mul_of [CommSemiring k] [Monoid G] (a : G) (b : k) : single a b = algebraMap k (MonoidAlgebra k G) b * of k G a := by simp theorem single_algebraMap_eq_algebraMap_mul_of {A : Type*} [CommSemiring k] [Semiring A] [Algebra k A] [Monoid G] (a : G) (b : k) : single a (algebraMap k A b) = algebraMap k (MonoidAlgebra A G) b * of A G a := by simp @[to_additive] instance isLocalHom_singleOneAlgHom {A : Type*} [CommSemiring k] [Semiring A] [Algebra k A] [Monoid G] : IsLocalHom (singleOneAlgHom : A →ₐ[k] MonoidAlgebra A G) where map_nonunit := isLocalHom_singleOneRingHom.map_nonunit @[to_additive (dont_translate := k)] instance isLocalHom_algebraMap {A : Type*} [CommSemiring k] [Semiring A] [Algebra k A] [Monoid G] [IsLocalHom (algebraMap k A)] : IsLocalHom (algebraMap k (MonoidAlgebra A G)) where map_nonunit _ hx := .of_map _ _ <| isLocalHom_singleOneAlgHom (k := k).map_nonunit _ hx end Algebra section lift variable [CommSemiring k] [Monoid G] [Monoid H] [Semiring A] [Algebra k A] [Semiring B] [Algebra k B] /-- `liftNCRingHom` as an `AlgHom`, for when `f` is an `AlgHom` -/ def liftNCAlgHom (f : A →ₐ[k] B) (g : G →* B) (h_comm : ∀ x y, Commute (f x) (g y)) : MonoidAlgebra A G →ₐ[k] B := { liftNCRingHom (f : A →+* B) g h_comm with commutes' := by simp [liftNCRingHom] } @[simp] lemma coe_liftNCAlgHom (f : A →ₐ[k] B) (g : G →* B) (h_comm) : ⇑(liftNCAlgHom f g h_comm) = liftNC f g := rfl /-- A `k`-algebra homomorphism from `MonoidAlgebra k G` is uniquely defined by its values on the functions `single a 1`. -/ @[to_additive (dont_translate := k) /-- A `k`-algebra homomorphism from `k[G]` is uniquely defined by its values on the functions `single a 1`. -/] theorem algHom_ext ⦃φ₁ φ₂ : MonoidAlgebra k G →ₐ[k] A⦄ (h : ∀ x, φ₁ (single x 1) = φ₂ (single x 1)) : φ₁ = φ₂ := AlgHom.toLinearMap_injective <| Finsupp.lhom_ext' fun a => LinearMap.ext_ring (h a) -- The priority must be `high`. /-- See note [partially-applied ext lemmas]. -/ @[ext high] theorem algHom_ext' ⦃φ₁ φ₂ : MonoidAlgebra k G →ₐ[k] A⦄ (h : (φ₁ : MonoidAlgebra k G →* A).comp (of k G) = (φ₂ : MonoidAlgebra k G →* A).comp (of k G)) : φ₁ = φ₂ := algHom_ext <| DFunLike.congr_fun h variable (k G A) in /-- Any monoid homomorphism `G →* A` can be lifted to an algebra homomorphism `MonoidAlgebra k G →ₐ[k] A`. -/ def lift : (G →* A) ≃ (MonoidAlgebra k G →ₐ[k] A) where invFun f := (f : MonoidAlgebra k G →* A).comp (of k G) toFun F := liftNCAlgHom (Algebra.ofId k A) F fun _ _ => Algebra.commutes _ _ left_inv f := by ext simp [liftNCAlgHom, liftNCRingHom] right_inv F := by ext simp [liftNCAlgHom, liftNCRingHom] theorem lift_apply' (F : G →* A) (f : MonoidAlgebra k G) : lift k G A F f = f.sum fun a b => algebraMap k A b * F a := rfl theorem lift_apply (F : G →* A) (f : MonoidAlgebra k G) : lift k G A F f = f.sum fun a b => b • F a := by simp only [lift_apply', Algebra.smul_def] theorem lift_def (F : G →* A) : ⇑(lift k G A F) = liftNC ((algebraMap k A : k →+* A) : k →+ A) F := rfl @[simp] theorem lift_symm_apply (F : MonoidAlgebra k G →ₐ[k] A) (x : G) : (lift k G A).symm F x = F (single x 1) := rfl @[simp] theorem lift_single (F : G →* A) (a b) : lift k G A F (single a b) = b • F a := by rw [lift_def, liftNC_single, Algebra.smul_def, AddMonoidHom.coe_coe] theorem lift_of (F : G →* A) (x) : lift k G A F (of k G x) = F x := by simp theorem lift_unique' (F : MonoidAlgebra k G →ₐ[k] A) : F = lift k G A ((F : MonoidAlgebra k G →* A).comp (of k G)) := ((lift k G A).apply_symm_apply F).symm /-- Decomposition of a `k`-algebra homomorphism from `MonoidAlgebra k G` by its values on `F (single a 1)`. -/ theorem lift_unique (F : MonoidAlgebra k G →ₐ[k] A) (f : MonoidAlgebra k G) : F f = f.sum fun a b => b • F (single a 1) := by conv_lhs => rw [lift_unique' F] simp [lift_apply] /-- If `f : G → H` is a homomorphism between two magmas, then `Finsupp.mapDomain f` is a non-unital algebra homomorphism between their magma algebras. -/ @[to_additive (dont_translate := k) (attr := simps apply) /-- If `f : G → H` is a homomorphism between two additive magmas, then `Finsupp.mapDomain f` is a non-unital algebra homomorphism between their additive magma algebras. -/] def mapDomainNonUnitalAlgHom (k A : Type*) [CommSemiring k] [Semiring A] [Algebra k A] {G H F : Type*} [Mul G] [Mul H] [FunLike F G H] [MulHomClass F G H] (f : F) : MonoidAlgebra A G →ₙₐ[k] MonoidAlgebra A H := { (Finsupp.mapDomain.addMonoidHom f : MonoidAlgebra A G →+ MonoidAlgebra A H) with map_mul' := fun x y => mapDomain_mul f x y map_smul' := fun r x => mapDomain_smul r x } variable (A) in @[to_additive] theorem mapDomain_algebraMap {F : Type*} [FunLike F G H] [MonoidHomClass F G H] (f : F) (r : k) : mapDomain f (algebraMap k (MonoidAlgebra A G) r) = algebraMap k (MonoidAlgebra A H) r := by simp only [coe_algebraMap, mapDomain_single, map_one, (· ∘ ·)] /-- If `f : G → H` is a multiplicative homomorphism between two monoids, then `Finsupp.mapDomain f` is an algebra homomorphism between their monoid algebras. -/ @[to_additive (attr := simps!) /-- If `f : G → H` is an additive homomorphism between two additive monoids, then `Finsupp.mapDomain f` is an algebra homomorphism between their additive monoid algebras. -/] def mapDomainAlgHom (k A : Type*) [CommSemiring k] [Semiring A] [Algebra k A] {H F : Type*} [Monoid H] [FunLike F G H] [MonoidHomClass F G H] (f : F) : MonoidAlgebra A G →ₐ[k] MonoidAlgebra A H := { mapDomainRingHom A f with commutes' := mapDomain_algebraMap A f } @[to_additive (attr := simp)] lemma mapDomainAlgHom_id (k A) [CommSemiring k] [Semiring A] [Algebra k A] : mapDomainAlgHom k A (MonoidHom.id G) = AlgHom.id k (MonoidAlgebra A G) := by ext; simp [MonoidHom.id, ← Function.id_def] @[to_additive (attr := simp)] lemma mapDomainAlgHom_comp (k A) {G₁ G₂ G₃} [CommSemiring k] [Semiring A] [Algebra k A] [Monoid G₁] [Monoid G₂] [Monoid G₃] (f : G₁ →* G₂) (g : G₂ →* G₃) : mapDomainAlgHom k A (g.comp f) = (mapDomainAlgHom k A g).comp (mapDomainAlgHom k A f) := by ext; simp [mapDomain_comp] variable (k A) /-- If `e : G ≃* H` is a multiplicative equivalence between two monoids, then `MonoidAlgebra.domCongr e` is an algebra equivalence between their monoid algebras. -/ @[to_additive /-- If `e : G ≃+ H` is an additive equivalence between two additive monoids, then `AddMonoidAlgebra.domCongr e` is an algebra equivalence between their additive monoid algebras. -/] def domCongr (e : G ≃* H) : MonoidAlgebra A G ≃ₐ[k] MonoidAlgebra A H := AlgEquiv.ofLinearEquiv (Finsupp.domLCongr e : (G →₀ A) ≃ₗ[k] (H →₀ A)) ((equivMapDomain_eq_mapDomain _ _).trans <| mapDomain_one e) (fun f g => (equivMapDomain_eq_mapDomain _ _).trans <| (mapDomain_mul e f g).trans <| congr_arg₂ _ (equivMapDomain_eq_mapDomain _ _).symm (equivMapDomain_eq_mapDomain _ _).symm) @[to_additive] theorem domCongr_toAlgHom (e : G ≃* H) : (domCongr k A e).toAlgHom = mapDomainAlgHom k A e := AlgHom.ext fun _ => equivMapDomain_eq_mapDomain _ _ @[to_additive (attr := simp)] theorem domCongr_apply (e : G ≃* H) (f : MonoidAlgebra A G) (h : H) : domCongr k A e f h = f (e.symm h) := rfl @[to_additive (attr := simp)] theorem domCongr_support (e : G ≃* H) (f : MonoidAlgebra A G) : (domCongr k A e f).support = f.support.map e := rfl @[to_additive (attr := simp)] theorem domCongr_single (e : G ≃* H) (g : G) (a : A) : domCongr k A e (single g a) = single (e g) a := Finsupp.equivMapDomain_single _ _ _ @[to_additive (attr := simp)] lemma domCongr_comp_lsingle (e : G ≃* H) (g : G) : (domCongr k A e).toLinearMap ∘ₗ lsingle g = lsingle (e g) := by ext; simp @[to_additive (attr := simp)] theorem domCongr_refl : domCongr k A (.refl G) = .refl := AlgEquiv.ext fun _ => Finsupp.ext fun _ => rfl @[to_additive (attr := simp)] theorem domCongr_symm (e : G ≃* H) : (domCongr k A e).symm = domCongr k A e.symm := rfl end lift section mapRange variable [CommSemiring R] [CommSemiring S] [Semiring A] [Algebra R A] [Semiring B] [Algebra R B] [Monoid M] [Monoid N] @[to_additive (attr := simp)] lemma mapDomainRingHom_comp_algebraMap (f : M →* N) : (mapDomainRingHom A f).comp (algebraMap R <| MonoidAlgebra A M) = algebraMap R (MonoidAlgebra A N) := by ext; simp @[to_additive (attr := simp)] lemma mapRangeRingHom_comp_algebraMap (f : R →+* S) : (mapRangeRingHom (M := M) f).comp (algebraMap _ _) = (algebraMap _ _).comp f := by ext; simp variable (M) in /-- The algebra homomorphism of monoid algebras induced by a homomorphism of the base algebras. -/ @[to_additive /-- The algebra homomorphism of additive monoid algebras induced by a homomorphism of the base algebras. -/] noncomputable def mapRangeAlgHom (f : A →ₐ[R] B) : MonoidAlgebra A M →ₐ[R] MonoidAlgebra B M where __ := mapRangeRingHom M f commutes' := by simp variable (M) in @[to_additive (attr := simp)] lemma toRingHom_mapRangeAlgHom (f : A →ₐ[R] B) : mapRangeAlgHom M f = mapRangeRingHom M f.toRingHom := rfl @[to_additive (attr := simp)] lemma mapRangeAlgHom_apply (f : A →ₐ[R] B) (x : MonoidAlgebra A M) (m : M) : mapRangeAlgHom M f x m = f (x m) := mapRangeRingHom_apply f.toRingHom x m @[to_additive (attr := simp)] lemma mapRangeAlgHom_single (f : A →ₐ[R] B) (m : M) (a : A) : mapRangeAlgHom M f (single m a) = single m (f a) := by classical ext; simp [single_apply, apply_ite f] variable (R M) in /-- The algebra isomorphism of monoid algebras induced by an isomorphism of the base algebras. -/ @[to_additive (attr := simps apply) /-- The algebra isomorphism of additive monoid algebras induced by an isomorphism of the base algebras. -/] noncomputable def mapRangeAlgEquiv (e : A ≃ₐ[R] B) : MonoidAlgebra A M ≃ₐ[R] MonoidAlgebra B M where __ := mapRangeAlgHom M e invFun := mapRangeAlgHom M (e.symm : B →ₐ[R] A) left_inv _ := by aesop right_inv _ := by aesop @[simp] lemma symm_mapRangeAlgEquiv (e : A ≃ₐ[R] B) : (mapRangeAlgEquiv R M e).symm = mapRangeAlgEquiv R M e.symm := rfl end mapRange section variable (k) in /-- When `V` is a `k[G]`-module, multiplication by a group element `g` is a `k`-linear map. -/ def GroupSMul.linearMap [Monoid G] [CommSemiring k] (V : Type*) [AddCommMonoid V] [Module k V] [Module (MonoidAlgebra k G) V] [IsScalarTower k (MonoidAlgebra k G) V] (g : G) : V →ₗ[k] V where toFun v := single g (1 : k) • v map_add' x y := smul_add (single g (1 : k)) x y map_smul' _c _x := smul_algebra_smul_comm _ _ _ variable (k) in @[simp] theorem GroupSMul.linearMap_apply [Monoid G] [CommSemiring k] (V : Type*) [AddCommMonoid V] [Module k V] [Module (MonoidAlgebra k G) V] [IsScalarTower k (MonoidAlgebra k G) V] (g : G) (v : V) : (GroupSMul.linearMap k V g) v = single g (1 : k) • v := rfl variable [Monoid G] [CommSemiring k] {V W : Type*} [AddCommMonoid V] [Module k V] [Module (MonoidAlgebra k G) V] [IsScalarTower k (MonoidAlgebra k G) V] [AddCommMonoid W] [Module k W] [Module (MonoidAlgebra k G) W] [IsScalarTower k (MonoidAlgebra k G) W] (f : V →ₗ[k] W) /-- Build a `k[G]`-linear map from a `k`-linear map and evidence that it is `G`-equivariant. -/ def equivariantOfLinearOfComm (h : ∀ (g : G) (v : V), f (single g (1 : k) • v) = single g (1 : k) • f v) : V →ₗ[MonoidAlgebra k G] W where toFun := f map_add' v v' := by simp map_smul' c v := by refine Finsupp.induction c ?_ ?_ · simp · intro g r c' _nm _nz w dsimp at * simp only [add_smul, f.map_add, w, single_eq_algebraMap_mul_of, ← smul_smul] rw [algebraMap_smul (MonoidAlgebra k G) r, algebraMap_smul (MonoidAlgebra k G) r, f.map_smul, of_apply, h g v] variable (h : ∀ (g : G) (v : V), f (single g (1 : k) • v) = single g (1 : k) • f v) @[simp] theorem equivariantOfLinearOfComm_apply (v : V) : (equivariantOfLinearOfComm f h) v = f v := rfl end variable [CommMonoid M] [CommSemiring R] [CommSemiring S] [Algebra R S] /-- If `S` is an `R`-algebra, then `MonoidAlgebra S M` is a `MonoidAlgebra R M` algebra. Warning: This produces a diamond for `Algebra (MonoidAlgebra R M) (MonoidAlgebra (MonoidAlgebra S M) M)` and another one fro `Algebra (MonoidAlgebra R M) (MonoidAlgebra R M)`. That's why it is not a global instance. -/ @[to_additive /-- If `S` is an `R`-algebra, then `S[M]` is an `R[M]`-algebra. Warning: This produces a diamond for `Algebra R[M] S[M][M]` and another one for `Algebra R[M] R[M]`. That's why it is not a global instance. -/] noncomputable abbrev algebraMonoidAlgebra : Algebra (MonoidAlgebra R M) (MonoidAlgebra S M) := (mapRangeRingHom M (algebraMap R S)).toAlgebra attribute [local instance] algebraMonoidAlgebra @[to_additive (attr := simp)] lemma algebraMap_def : algebraMap (MonoidAlgebra R M) (MonoidAlgebra S M) = mapRangeRingHom M (algebraMap R S) := rfl @[to_additive (dont_translate := R)] instance [CommSemiring T] [Algebra R T] [Algebra S T] [IsScalarTower R S T] : IsScalarTower R (MonoidAlgebra S M) (MonoidAlgebra T M) := .of_algebraMap_eq' (mapRangeAlgHom _ (IsScalarTower.toAlgHom R S T)).comp_algebraMap.symm end MonoidAlgebra namespace AddMonoidAlgebra /-! #### Non-unital, non-associative algebra structure -/ section NonUnitalNonAssocAlgebra variable (k) [Semiring k] [DistribSMul R k] [Add G] [NonUnitalNonAssocSemiring A] /-- See note [partially-applied ext lemmas]. -/ @[ext high] theorem nonUnitalAlgHom_ext' [DistribMulAction k A] {φ₁ φ₂ : k[G] →ₙₐ[k] A} (h : φ₁.toMulHom.comp (ofMagma k G) = φ₂.toMulHom.comp (ofMagma k G)) : φ₁ = φ₂ := @MonoidAlgebra.nonUnitalAlgHom_ext' k (Multiplicative G) _ _ _ _ _ φ₁ φ₂ h /-- The functor `G ↦ k[G]`, from the category of magmas to the category of non-unital, non-associative algebras over `k` is adjoint to the forgetful functor in the other direction. -/ @[simps apply_apply symm_apply] def liftMagma [Module k A] [IsScalarTower k A A] [SMulCommClass k A A] : (Multiplicative G →ₙ* A) ≃ (k[G] →ₙₐ[k] A) := { (MonoidAlgebra.liftMagma k : (Multiplicative G →ₙ* A) ≃ (_ →ₙₐ[k] A)) with toFun := fun f => { (MonoidAlgebra.liftMagma k f :) with toFun := fun a => sum a fun m t => t • f (Multiplicative.ofAdd m) } invFun := fun F => F.toMulHom.comp (ofMagma k G) } end NonUnitalNonAssocAlgebra /-! #### Algebra structure -/ section lift variable [CommSemiring k] [AddMonoid G] [Semiring A] [Algebra k A] [Semiring B] [Algebra k B] /-- `liftNCRingHom` as an `AlgHom`, for when `f` is an `AlgHom` -/ def liftNCAlgHom (f : A →ₐ[k] B) (g : Multiplicative G →* B) (h_comm : ∀ x y, Commute (f x) (g y)) : A[G] →ₐ[k] B := { liftNCRingHom (f : A →+* B) g h_comm with commutes' := by simp [liftNCRingHom] } @[simp] lemma coe_liftNCAlgHom (f : A →ₐ[k] B) (g : Multiplicative G →* B) (h_comm) : ⇑(liftNCAlgHom f g h_comm) = liftNC f g := rfl /-- See note [partially-applied ext lemmas]. -/ @[ext high] theorem algHom_ext' ⦃φ₁ φ₂ : k[G] →ₐ[k] A⦄ (h : (φ₁ : k[G] →* A).comp (of k G) = (φ₂ : k[G] →* A).comp (of k G)) : φ₁ = φ₂ := algHom_ext <| DFunLike.congr_fun h variable (k G A) in /-- Any monoid homomorphism `G →* A` can be lifted to an algebra homomorphism `k[G] →ₐ[k] A`. -/ def lift : (Multiplicative G →* A) ≃ (k[G] →ₐ[k] A) where __ := MonoidAlgebra.lift k (Multiplicative G) A invFun f := (f : k[G] →* A).comp (of k G) toFun F := { MonoidAlgebra.lift k (Multiplicative G) A F with toFun := liftNCAlgHom (Algebra.ofId k A) F fun _ _ => Algebra.commutes _ _ } theorem lift_apply' (F : Multiplicative G →* A) (f : MonoidAlgebra k G) : lift k G A F f = f.sum fun a b => algebraMap k A b * F (Multiplicative.ofAdd a) := rfl theorem lift_apply (F : Multiplicative G →* A) (f : MonoidAlgebra k G) : lift k G A F f = f.sum fun a b => b • F (Multiplicative.ofAdd a) := by simp only [lift_apply', Algebra.smul_def] theorem lift_def (F : Multiplicative G →* A) : ⇑(lift k G A F) = liftNC ((algebraMap k A : k →+* A) : k →+ A) F := rfl @[simp] theorem lift_symm_apply (F : k[G] →ₐ[k] A) (x : Multiplicative G) : (lift k G A).symm F x = F (single x.toAdd 1) := rfl theorem lift_of (F : Multiplicative G →* A) (x : Multiplicative G) : lift k G A F (of k G x) = F x := MonoidAlgebra.lift_of F x @[simp] theorem lift_single (F : Multiplicative G →* A) (a b) : lift k G A F (single a b) = b • F (Multiplicative.ofAdd a) := MonoidAlgebra.lift_single F (.ofAdd a) b lemma lift_of' (F : Multiplicative G →* A) (x : G) : lift k G A F (of' k G x) = F (Multiplicative.ofAdd x) := lift_of F x theorem lift_unique' (F : k[G] →ₐ[k] A) : F = lift k G A ((F : k[G] →* A).comp (of k G)) := ((lift k G A).apply_symm_apply F).symm /-- Decomposition of a `k`-algebra homomorphism from `MonoidAlgebra k G` by its values on `F (single a 1)`. -/ theorem lift_unique (F : k[G] →ₐ[k] A) (f : MonoidAlgebra k G) : F f = f.sum fun a b => b • F (single a 1) := by conv_lhs => rw [lift_unique' F] simp [lift_apply] theorem algHom_ext_iff {φ₁ φ₂ : k[G] →ₐ[k] A} : (∀ x, φ₁ (Finsupp.single x 1) = φ₂ (Finsupp.single x 1)) ↔ φ₁ = φ₂ := ⟨fun h => algHom_ext h, by rintro rfl _; rfl⟩ end lift end AddMonoidAlgebra variable [CommSemiring R] variable (k G) in /-- The algebra equivalence between `AddMonoidAlgebra` and `MonoidAlgebra` in terms of `Multiplicative`. -/ def AddMonoidAlgebra.toMultiplicativeAlgEquiv [Semiring k] [Algebra R k] [AddMonoid G] : AddMonoidAlgebra k G ≃ₐ[R] MonoidAlgebra k (Multiplicative G) := { AddMonoidAlgebra.toMultiplicative k G with commutes' := fun r => by simp [AddMonoidAlgebra.toMultiplicative] } variable (k G) in /-- The algebra equivalence between `MonoidAlgebra` and `AddMonoidAlgebra` in terms of `Additive`. -/ def MonoidAlgebra.toAdditiveAlgEquiv [Semiring k] [Algebra R k] [Monoid G] : MonoidAlgebra k G ≃ₐ[R] AddMonoidAlgebra k (Additive G) := { MonoidAlgebra.toAdditive k G with commutes' := fun r => by simp [MonoidAlgebra.toAdditive] }
.lake/packages/mathlib/Mathlib/Algebra/MonoidAlgebra/Ideal.lean
import Mathlib.RingTheory.Ideal.BigOperators import Mathlib.RingTheory.Ideal.Span import Mathlib.Algebra.MonoidAlgebra.Defs /-! # Lemmas about ideals of `MonoidAlgebra` and `AddMonoidAlgebra` -/ variable {k A G : Type*} /-- If `x` belongs to the ideal generated by generators in `s`, then every element of the support of `x` factors through an element of `s`. We could spell `∃ d, m = d * m` as `MulOpposite.op m' ∣ MulOpposite.op m` but this would be worse. -/ theorem MonoidAlgebra.mem_ideal_span_of_image [Monoid G] [Semiring k] {s : Set G} {x : MonoidAlgebra k G} : x ∈ Ideal.span (MonoidAlgebra.of k G '' s) ↔ ∀ m ∈ x.support, ∃ m' ∈ s, ∃ d, m = d * m' := by let RHS : Ideal (MonoidAlgebra k G) := { carrier := { p | ∀ m : G, m ∈ p.support → ∃ m' ∈ s, ∃ d, m = d * m' } add_mem' := fun {x y} hx hy m hm => by classical exact (Finset.mem_union.1 <| Finsupp.support_add hm).elim (hx m) (hy m) zero_mem' := fun m hm => by cases hm smul_mem' := fun x y hy m hm => by classical rw [smul_eq_mul, mul_def] at hm replace hm := Finset.mem_biUnion.mp (Finsupp.support_sum hm) obtain ⟨xm, -, hm⟩ := hm replace hm := Finset.mem_biUnion.mp (Finsupp.support_sum hm) obtain ⟨ym, hym, hm⟩ := hm obtain rfl := Finset.mem_singleton.mp (Finsupp.support_single_subset hm) refine (hy _ hym).imp fun sm p => And.imp_right ?_ p rintro ⟨d, rfl⟩ exact ⟨xm * d, (mul_assoc _ _ _).symm⟩ } change _ ↔ x ∈ RHS constructor · suffices Ideal.span (⇑(of k G) '' s) ≤ RHS from @this x rw [Ideal.span_le] rintro _ ⟨i, hi, rfl⟩ m hm refine ⟨_, hi, 1, ?_⟩ obtain rfl := Finset.mem_singleton.mp (Finsupp.support_single_subset hm) exact (one_mul _).symm · intro hx rw [← Finsupp.sum_single x] apply Ideal.sum_mem _ fun i hi ↦ ?_ obtain ⟨d, hd, d2, rfl⟩ := hx _ hi convert Ideal.mul_mem_left _ (id <| Finsupp.single d2 <| x (d2 * d) : MonoidAlgebra k G) _ pick_goal 3 · exact Ideal.subset_span ⟨_, hd, rfl⟩ rw [id, MonoidAlgebra.of_apply, MonoidAlgebra.single_mul_single, mul_one] /-- If `x` belongs to the ideal generated by generators in `s`, then every element of the support of `x` factors additively through an element of `s`. -/ theorem AddMonoidAlgebra.mem_ideal_span_of'_image [AddMonoid A] [Semiring k] {s : Set A} {x : AddMonoidAlgebra k A} : x ∈ Ideal.span (AddMonoidAlgebra.of' k A '' s) ↔ ∀ m ∈ x.support, ∃ m' ∈ s, ∃ d, m = d + m' := @MonoidAlgebra.mem_ideal_span_of_image k (Multiplicative A) _ _ _ _
.lake/packages/mathlib/Mathlib/Algebra/MonoidAlgebra/Grading.lean
import Mathlib.Algebra.DirectSum.Internal import Mathlib.Algebra.MonoidAlgebra.Basic import Mathlib.Algebra.MonoidAlgebra.Support import Mathlib.LinearAlgebra.Finsupp.SumProd import Mathlib.RingTheory.GradedAlgebra.Basic /-! # Internal grading of an `AddMonoidAlgebra` In this file, we show that an `AddMonoidAlgebra` has an internal direct sum structure. ## Main results * `AddMonoidAlgebra.gradeBy R f i`: the `i`th grade of an `R[M]` given by the degree function `f`. * `AddMonoidAlgebra.grade R i`: the `i`th grade of an `R[M]` when the degree function is the identity. * `AddMonoidAlgebra.gradeBy.gradedAlgebra`: `AddMonoidAlgebra` is an algebra graded by `AddMonoidAlgebra.gradeBy`. * `AddMonoidAlgebra.grade.gradedAlgebra`: `AddMonoidAlgebra` is an algebra graded by `AddMonoidAlgebra.grade`. * `AddMonoidAlgebra.gradeBy.isInternal`: propositionally, the statement that `AddMonoidAlgebra.gradeBy` defines an internal graded structure. * `AddMonoidAlgebra.grade.isInternal`: propositionally, the statement that `AddMonoidAlgebra.grade` defines an internal graded structure when the degree function is the identity. -/ noncomputable section namespace AddMonoidAlgebra variable {M : Type*} {ι : Type*} {R : Type*} section variable (R) [CommSemiring R] /-- The submodule corresponding to each grade given by the degree function `f`. -/ abbrev gradeBy (f : M → ι) (i : ι) : Submodule R R[M] where carrier := { a | ∀ m, m ∈ a.support → f m = i } zero_mem' m h := by cases h add_mem' {a b} ha hb m h := by classical exact (Finset.mem_union.mp (Finsupp.support_add h)).elim (ha m) (hb m) smul_mem' _ _ h := Set.Subset.trans Finsupp.support_smul h /-- The submodule corresponding to each grade. -/ abbrev grade (m : M) : Submodule R R[M] := gradeBy R id m theorem gradeBy_id : gradeBy R (id : M → M) = grade R := rfl theorem mem_gradeBy_iff (f : M → ι) (i : ι) (a : R[M]) : a ∈ gradeBy R f i ↔ (a.support : Set M) ⊆ f ⁻¹' {i} := by rfl theorem mem_grade_iff (m : M) (a : R[M]) : a ∈ grade R m ↔ a.support ⊆ {m} := by rw [← Finset.coe_subset, Finset.coe_singleton] rfl theorem mem_grade_iff' (m : M) (a : R[M]) : a ∈ grade R m ↔ a ∈ (LinearMap.range (Finsupp.lsingle m : R →ₗ[R] M →₀ R) : Submodule R R[M]) := by rw [mem_grade_iff, Finsupp.support_subset_singleton'] apply exists_congr intro r constructor <;> exact Eq.symm theorem grade_eq_lsingle_range (m : M) : grade R m = LinearMap.range (Finsupp.lsingle m : R →ₗ[R] M →₀ R) := Submodule.ext (mem_grade_iff' R m) theorem single_mem_gradeBy {R} [CommSemiring R] (f : M → ι) (m : M) (r : R) : Finsupp.single m r ∈ gradeBy R f (f m) := by intro x hx rw [Finset.mem_singleton.mp (Finsupp.support_single_subset hx)] theorem single_mem_grade {R} [CommSemiring R] (i : M) (r : R) : Finsupp.single i r ∈ grade R i := single_mem_gradeBy _ _ _ end open DirectSum instance gradeBy.gradedMonoid [AddMonoid M] [AddMonoid ι] [CommSemiring R] (f : M →+ ι) : SetLike.GradedMonoid (gradeBy R f : ι → Submodule R R[M]) where one_mem m h := by rw [one_def] at h obtain rfl : m = 0 := Finset.mem_singleton.1 <| Finsupp.support_single_subset h apply map_zero mul_mem i j a b ha hb c hc := by classical obtain ⟨ma, hma, mb, hmb, rfl⟩ : ∃ y ∈ a.support, ∃ z ∈ b.support, y + z = c := Finset.mem_add.1 <| support_mul a b hc rw [map_add, ha ma hma, hb mb hmb] instance grade.gradedMonoid [AddMonoid M] [CommSemiring R] : SetLike.GradedMonoid (grade R : M → Submodule R R[M]) := by apply gradeBy.gradedMonoid (AddMonoidHom.id _) variable [AddMonoid M] [DecidableEq ι] [AddMonoid ι] [CommSemiring R] (f : M →+ ι) /-- Auxiliary definition; the canonical grade decomposition, used to provide `DirectSum.decompose`. -/ def decomposeAux : R[M] →ₐ[R] ⨁ i : ι, gradeBy R f i := AddMonoidAlgebra.lift R M _ { toFun := fun m => DirectSum.of (fun i : ι => gradeBy R f i) (f m.toAdd) ⟨Finsupp.single m.toAdd 1, single_mem_gradeBy _ _ _⟩ map_one' := DirectSum.of_eq_of_gradedMonoid_eq (by congr 2 <;> simp) map_mul' := fun i j => by symm dsimp only [toAdd_one, Eq.ndrec, Set.mem_setOf_eq, ne_eq, OneHom.toFun_eq_coe, OneHom.coe_mk, toAdd_mul] convert DirectSum.of_mul_of (A := (fun i : ι => gradeBy R f i)) _ _ repeat { rw [AddMonoidHom.map_add] } simp only [SetLike.coe_gMul] exact Eq.trans (by rw [one_mul]) (single_mul_single ..).symm } theorem decomposeAux_single (m : M) (r : R) : decomposeAux f (Finsupp.single m r) = DirectSum.of (fun i : ι => gradeBy R f i) (f m) ⟨Finsupp.single m r, single_mem_gradeBy _ _ _⟩ := by refine (lift_single _ _ _).trans ?_ refine (DirectSum.of_smul R _ _ _).symm.trans ?_ apply DirectSum.of_eq_of_gradedMonoid_eq refine Sigma.subtype_ext rfl ?_ refine (smul_single' _ _ _).trans ?_ rw [mul_one] rfl theorem decomposeAux_coe {i : ι} (x : gradeBy R f i) : decomposeAux f ↑x = DirectSum.of (fun i => gradeBy R f i) i x := by classical obtain ⟨x, hx⟩ := x revert hx refine Finsupp.induction x ?_ ?_ · intro hx symm exact AddMonoidHom.map_zero _ · intro m b y hmy hb ih hmby have : Disjoint (Finsupp.single m b).support y.support := by simpa only [Finsupp.support_single_ne_zero _ hb, Finset.disjoint_singleton_left] rw [mem_gradeBy_iff, Finsupp.support_add_eq this, Finset.coe_union, Set.union_subset_iff] at hmby obtain ⟨h1, h2⟩ := hmby have : f m = i := by rwa [Finsupp.support_single_ne_zero _ hb, Finset.coe_singleton, Set.singleton_subset_iff] at h1 subst this simp only [map_add, decomposeAux_single f m] let ih' := ih h2 dsimp at ih' rw [ih', ← AddMonoidHom.map_add] apply DirectSum.of_eq_of_gradedMonoid_eq congr 2 instance gradeBy.gradedAlgebra : GradedAlgebra (gradeBy R f) := GradedAlgebra.ofAlgHom _ (decomposeAux f) (by ext : 4 dsimp rw [decomposeAux_single, DirectSum.coeAlgHom_of, Subtype.coe_mk]) fun i x => by rw [decomposeAux_coe f x] -- Lean can't find this later without us repeating it instance gradeBy.decomposition : DirectSum.Decomposition (gradeBy R f) := by infer_instance @[simp] theorem decomposeAux_eq_decompose : ⇑(decomposeAux f : R[M] →ₐ[R] ⨁ i : ι, gradeBy R f i) = DirectSum.decompose (gradeBy R f) := rfl theorem GradesBy.decompose_single (m : M) (r : R) : DirectSum.decompose (gradeBy R f) (Finsupp.single m r : R[M]) = DirectSum.of (fun i : ι => gradeBy R f i) (f m) ⟨Finsupp.single m r, single_mem_gradeBy _ _ _⟩ := decomposeAux_single _ _ _ instance grade.gradedAlgebra : GradedAlgebra (grade R : ι → Submodule _ _) := AddMonoidAlgebra.gradeBy.gradedAlgebra (AddMonoidHom.id _) -- Lean can't find this later without us repeating it instance grade.decomposition : DirectSum.Decomposition (grade R : ι → Submodule _ _) := by infer_instance theorem grade.decompose_single (i : ι) (r : R) : DirectSum.decompose (grade R : ι → Submodule _ _) (Finsupp.single i r : AddMonoidAlgebra _ _) = DirectSum.of (fun i : ι => grade R i) i ⟨Finsupp.single i r, single_mem_grade _ _⟩ := decomposeAux_single _ _ _ /-- `AddMonoidAlgebra.gradeBy` describe an internally graded algebra. -/ theorem gradeBy.isInternal : DirectSum.IsInternal (gradeBy R f) := DirectSum.Decomposition.isInternal _ /-- `AddMonoidAlgebra.grade` describe an internally graded algebra. -/ theorem grade.isInternal : DirectSum.IsInternal (grade R : ι → Submodule R _) := DirectSum.Decomposition.isInternal _ end AddMonoidAlgebra
.lake/packages/mathlib/Mathlib/Algebra/MonoidAlgebra/Division.lean
import Mathlib.Algebra.Group.Hom.End import Mathlib.Algebra.MonoidAlgebra.Defs /-! # Division of `AddMonoidAlgebra` by monomials This file is most important for when `G = ℕ` (polynomials) or `G = σ →₀ ℕ` (multivariate polynomials). In order to apply in maximal generality (such as for `LaurentPolynomial`s), this uses `∃ d, g' = g + d` in many places instead of `g ≤ g'`. ## Main definitions * `AddMonoidAlgebra.divOf x g`: divides `x` by the monomial `AddMonoidAlgebra.of k G g` * `AddMonoidAlgebra.modOf x g`: the remainder upon dividing `x` by the monomial `AddMonoidAlgebra.of k G g`. ## Main results * `AddMonoidAlgebra.divOf_add_modOf`, `AddMonoidAlgebra.modOf_add_divOf`: `divOf` and `modOf` are well-behaved as quotient and remainder operators. ## Implementation notes `∃ d, g' = g + d` is used as opposed to some other permutation up to commutativity in order to match the definition of `semigroupDvd`. The results in this file could be duplicated for `MonoidAlgebra` by using `g ∣ g'`, but this can't be done automatically, and in any case is not likely to be very useful. -/ variable {k G : Type*} [Semiring k] namespace AddMonoidAlgebra section variable [AddCommMonoid G] /-- Divide by `of' k G g`, discarding terms not divisible by this. -/ noncomputable def divOf [IsCancelAdd G] (x : k[G]) (g : G) : k[G] := -- note: comapping by `+ g` has the effect of subtracting `g` from every element in -- the support, and discarding the elements of the support from which `g` can't be subtracted. -- If `G` is an additive group, such as `ℤ` when used for `LaurentPolynomial`, -- then no discarding occurs. @Finsupp.comapDomain.addMonoidHom _ _ _ _ (g + ·) (add_right_injective g) x local infixl:70 " /ᵒᶠ " => divOf section divOf variable [IsCancelAdd G] @[simp] theorem divOf_apply (g : G) (x : k[G]) (g' : G) : (x /ᵒᶠ g) g' = x (g + g') := rfl @[simp] theorem support_divOf (g : G) (x : k[G]) : (x /ᵒᶠ g).support = x.support.preimage (g + ·) (Function.Injective.injOn (add_right_injective g)) := rfl @[simp] theorem zero_divOf (g : G) : (0 : k[G]) /ᵒᶠ g = 0 := map_zero (Finsupp.comapDomain.addMonoidHom _) @[simp] theorem divOf_zero (x : k[G]) : x /ᵒᶠ 0 = x := by ext simp only [AddMonoidAlgebra.divOf_apply, zero_add] theorem add_divOf (x y : k[G]) (g : G) : (x + y) /ᵒᶠ g = x /ᵒᶠ g + y /ᵒᶠ g := map_add (Finsupp.comapDomain.addMonoidHom _) _ _ theorem divOf_add (x : k[G]) (a b : G) : x /ᵒᶠ (a + b) = x /ᵒᶠ a /ᵒᶠ b := by ext simp only [AddMonoidAlgebra.divOf_apply, add_assoc] /-- A bundled version of `AddMonoidAlgebra.divOf`. -/ @[simps] noncomputable def divOfHom : Multiplicative G →* AddMonoid.End k[G] where toFun g := { toFun := fun x => divOf x g.toAdd map_zero' := zero_divOf _ map_add' := fun x y => add_divOf x y g.toAdd } map_one' := AddMonoidHom.ext divOf_zero map_mul' g₁ g₂ := AddMonoidHom.ext fun _x => (congr_arg _ (add_comm g₁.toAdd g₂.toAdd)).trans (divOf_add _ _ _) theorem of'_mul_divOf (a : G) (x : k[G]) : of' k G a * x /ᵒᶠ a = x := by ext rw [AddMonoidAlgebra.divOf_apply, of'_apply, single_mul_apply_aux, one_mul] intro c hc exact add_right_inj _ theorem mul_of'_divOf (x : k[G]) (a : G) : x * of' k G a /ᵒᶠ a = x := by ext rw [AddMonoidAlgebra.divOf_apply, of'_apply, mul_single_apply_aux, mul_one] intro c hc rw [add_comm] exact add_right_inj _ theorem of'_divOf (a : G) : of' k G a /ᵒᶠ a = 1 := by simpa only [one_mul] using mul_of'_divOf (1 : k[G]) a end divOf /-- The remainder upon division by `of' k G g`. -/ noncomputable def modOf (x : k[G]) (g : G) : k[G] := letI := Classical.decPred fun g₁ => ∃ g₂, g₁ = g + g₂ x.filter fun g₁ => ¬∃ g₂, g₁ = g + g₂ local infixl:70 " %ᵒᶠ " => modOf @[simp] theorem modOf_apply_of_not_exists_add (x : k[G]) (g : G) (g' : G) (h : ¬∃ d, g' = g + d) : (x %ᵒᶠ g) g' = x g' := by classical exact Finsupp.filter_apply_pos _ _ h @[simp] theorem modOf_apply_of_exists_add (x : k[G]) (g : G) (g' : G) (h : ∃ d, g' = g + d) : (x %ᵒᶠ g) g' = 0 := by classical exact Finsupp.filter_apply_neg _ _ <| by rwa [Classical.not_not] @[simp] theorem modOf_apply_add_self (x : k[G]) (g : G) (d : G) : (x %ᵒᶠ g) (d + g) = 0 := modOf_apply_of_exists_add _ _ _ ⟨_, add_comm _ _⟩ theorem modOf_apply_self_add (x : k[G]) (g : G) (d : G) : (x %ᵒᶠ g) (g + d) = 0 := modOf_apply_of_exists_add _ _ _ ⟨_, rfl⟩ theorem of'_mul_modOf (g : G) (x : k[G]) : of' k G g * x %ᵒᶠ g = 0 := by ext g' rw [Finsupp.zero_apply] obtain ⟨d, rfl⟩ | h := em (∃ d, g' = g + d) · rw [modOf_apply_self_add] · rw [modOf_apply_of_not_exists_add _ _ _ h, of'_apply, single_mul_apply_of_not_exists_add _ _ h] theorem mul_of'_modOf (x : k[G]) (g : G) : x * of' k G g %ᵒᶠ g = 0 := by ext g' rw [Finsupp.zero_apply] obtain ⟨d, rfl⟩ | h := em (∃ d, g' = g + d) · rw [modOf_apply_self_add] · rw [modOf_apply_of_not_exists_add _ _ _ h, of'_apply, mul_single_apply_of_not_exists_add] simpa only [add_comm] using h theorem of'_modOf (g : G) : of' k G g %ᵒᶠ g = 0 := by simpa only [one_mul] using mul_of'_modOf (1 : k[G]) g theorem divOf_add_modOf [IsCancelAdd G] (x : k[G]) (g : G) : of' k G g * (x /ᵒᶠ g) + x %ᵒᶠ g = x := by ext g' rw [Finsupp.add_apply] obtain ⟨d, rfl⟩ | h := em (∃ d, g' = g + d) swap · rw [modOf_apply_of_not_exists_add x _ _ h, of'_apply, single_mul_apply_of_not_exists_add _ _ h, zero_add] · rw [modOf_apply_self_add, add_zero] rw [of'_apply, single_mul_apply_aux, one_mul, divOf_apply] intro a ha exact add_right_inj _ theorem modOf_add_divOf [IsCancelAdd G] (x : k[G]) (g : G) : x %ᵒᶠ g + of' k G g * (x /ᵒᶠ g) = x := by rw [add_comm, divOf_add_modOf] theorem of'_dvd_iff_modOf_eq_zero [IsCancelAdd G] {x : k[G]} {g : G} : of' k G g ∣ x ↔ x %ᵒᶠ g = 0 := by constructor · rintro ⟨x, rfl⟩ rw [of'_mul_modOf] · intro h rw [← divOf_add_modOf x g, h, add_zero] exact dvd_mul_right _ _ end end AddMonoidAlgebra
.lake/packages/mathlib/Mathlib/Algebra/MonoidAlgebra/Defs.lean
import Mathlib.Algebra.Module.Defs import Mathlib.Data.Finsupp.Basic import Mathlib.Data.Finsupp.SMulWithZero /-! # Monoid algebras When the domain of a `Finsupp` has a multiplicative or additive structure, we can define a convolution product. To mathematicians this structure is known as the "monoid algebra", i.e. the finite formal linear combinations over a given semiring of elements of a monoid `M`. The "group ring" `ℤ[G]` or the "group algebra" `k[G]` are typical uses. In fact the construction of the "monoid algebra" makes sense when `M` is not even a monoid, but merely a magma, i.e., when `M` carries a multiplication which is not required to satisfy any conditions at all. In this case the construction yields a not-necessarily-unital, not-necessarily-associative algebra but it is still adjoint to the forgetful functor from such algebras to magmas, and we prove this as `MonoidAlgebra.liftMagma`. In this file we define `MonoidAlgebra R M := M →₀ R`, and `AddMonoidAlgebra R M` in the same way, and then define the convolution product on these. When the domain is additive, this is used to define polynomials: ``` Polynomial R := AddMonoidAlgebra R ℕ MvPolynomial σ α := AddMonoidAlgebra R (σ →₀ ℕ) ``` Note: `Polynomial R` is currently a wrapper around `AddMonoidAlgebra R ℕ` and not defeq to it. There is ongoing work to make it defeq. See https://github.com/leanprover-community/mathlib4/pull/25273 When the domain is multiplicative, e.g. a group, this will be used to define the group ring. ## Notation We introduce the notation `R[A]` for `AddMonoidAlgebra R A`. -/ assert_not_exists NonUnitalAlgHom AlgEquiv noncomputable section open Finsupp hiding single -- We make sure the additivisable argument comes first to avoid doing -- `to_additive (relevant_arg := 2)` everywhere. variable {G M N O ι R S : Type*} /-- The monoid algebra over a semiring `R` generated by the monoid `M`. It is the type of finite formal `R`-linear combinations of terms of `M`, endowed with the convolution product. -/ @[to_additive (relevant_arg := 2) /-- The additive monoid algebra over a semiring `R` generated by the additive monoid `M`. It is the type of finite formal `R`-linear combinations of terms of `M`, endowed with the convolution product. -/, to_additive_dont_translate] def MonoidAlgebra (R M : Type*) [Semiring R] : Type _ := M →₀ R namespace AddMonoidAlgebra @[inherit_doc AddMonoidAlgebra] scoped syntax:max (priority := high) term noWs "[" term "]" : term macro_rules | `($R[$M]) => `(AddMonoidAlgebra $R $M) /-- Unexpander for `AddMonoidAlgebra`. -/ @[scoped app_unexpander AddMonoidAlgebra] def unexpander : Lean.PrettyPrinter.Unexpander | `($_ $k $g) => `($k[$g]) | _ => throw () end AddMonoidAlgebra namespace MonoidAlgebra section Semiring variable [Semiring R] {x y : MonoidAlgebra R M} {r r₁ r₂ : R} {m m' m₁ m₂ : M} @[to_additive] instance inhabited : Inhabited (MonoidAlgebra R M) := inferInstanceAs <| Inhabited <| M →₀ R @[to_additive] instance nontrivial [Nontrivial R] [Nonempty M] : Nontrivial (MonoidAlgebra R M) := inferInstanceAs <| Nontrivial <| M →₀ R @[to_additive] instance unique [Subsingleton R] : Unique (MonoidAlgebra R M) := inferInstanceAs <| Unique <| M →₀ R @[to_additive] instance addCommMonoid : AddCommMonoid (MonoidAlgebra R M) := inferInstanceAs <| AddCommMonoid <| M →₀ R @[to_additive] instance instIsCancelAdd [IsCancelAdd R] : IsCancelAdd (MonoidAlgebra R M) := inferInstanceAs <| IsCancelAdd <| M →₀ R @[to_additive] instance instCoeFun : CoeFun (MonoidAlgebra R M) fun _ ↦ M → R := inferInstanceAs <| CoeFun (M →₀ R) fun _ ↦ M → R /-- A copy of `Finsupp.ext` for `MonoidAlgebra`. -/ @[to_additive (attr := ext) /-- A copy of `Finsupp.ext` for `AddMonoidAlgebra`. -/] lemma ext ⦃f g : MonoidAlgebra R M⦄ (hfg : ∀ m, f m = g m) : f = g := Finsupp.ext hfg -- TODO: This definition is very leaky, and we later have frequent problems conflating the two -- versions of `single`. Perhaps someone wants to try making this a `def` rather than an `abbrev`? -- In Mathlib 3 this was locally reducible. /-- `MonoidAlgebra.single m r` for `m : M`, `r : R` is the element `rm : MonoidAlgebra R M`. -/ @[to_additive /-- `AddMonoidAlgebra.single m r` for `m : M`, `r : R` is the element `rm : R[M]`. -/] abbrev single (m : M) (r : R) : MonoidAlgebra R M := Finsupp.single m r section SMul /-! ### Basic scalar multiplication instances This section collects instances needed for the algebraic structure of `Polynomial`, which is defined in terms of `MonoidAlgebra`. Further results on scalar multiplication can be found in `Mathlib/Algebra/MonoidAlgebra/Module.lean`. -/ variable {A : Type*} [SMulZeroClass A R] @[to_additive (dont_translate := A) smulZeroClass] instance smulZeroClass : SMulZeroClass A (MonoidAlgebra R M) := Finsupp.smulZeroClass @[to_additive (attr := simp) (dont_translate := A) coeff_smul] lemma smul_apply (a : A) (x : MonoidAlgebra R M) (m : M) : (a • x) m = a • x m := rfl @[to_additive (attr := simp) (dont_translate := A) smul_single] lemma smul_single (a : A) (m : M) (r : R) : a • single m r = single m (a • r) := by ext; simp [single, ← Finsupp.smul_single] @[to_additive (dont_translate := R) smul_single'] lemma smul_single' (r' : R) (m : M) (r : R) : r' • single m r = single m (r' * r) := smul_single .. @[to_additive (dont_translate := N) distribSMul] instance distribSMul [DistribSMul N R] : DistribSMul N (MonoidAlgebra R M) := Finsupp.distribSMul _ _ @[to_additive (dont_translate := N) isScalarTower] instance isScalarTower [SMulZeroClass N R] [SMulZeroClass O R] [SMul N O] [IsScalarTower N O R] : IsScalarTower N O (MonoidAlgebra R M) := Finsupp.isScalarTower .. @[to_additive (dont_translate := N) smulCommClass] instance smulCommClass [SMulZeroClass N R] [SMulZeroClass O R] [SMulCommClass N O R] : SMulCommClass N O (MonoidAlgebra R M) := Finsupp.smulCommClass .. @[to_additive (dont_translate := N) isCentralScalar] instance isCentralScalar [SMulZeroClass N R] [SMulZeroClass Nᵐᵒᵖ R] [IsCentralScalar N R] : IsCentralScalar N (MonoidAlgebra R M) := Finsupp.isCentralScalar .. end SMul @[to_additive (attr := simp, norm_cast)] lemma coe_add (f g : MonoidAlgebra R G) : ⇑(f + g) = f + g := rfl @[to_additive] lemma single_zero (m : M) : (single m 0 : MonoidAlgebra R M) = 0 := by simp [single] @[to_additive] lemma single_add (m : M) (r₁ r₂ : R) : single m (r₁ + r₂) = single m r₁ + single m r₂ := by simp [single] /-- `MonoidAlgebra.single` as an `AddMonoidHom`. TODO: Rename to `singleAddMonoidHom`. -/ @[to_additive (attr := simps) /-- `AddMonoidAlgebra.single` as an `AddMonoidHom`. TODO: Rename to `singleAddMonoidHom`. -/] def singleAddHom (m : M) : R →+ MonoidAlgebra R M where toFun := single m map_zero' := single_zero _ map_add' := single_add _ /-- If two additive homomorphisms from `MonoidAlgebra R M` are equal on each `single r m`, then they are equal. We formulate this using equality of `AddMonoidHom`s so that `ext` tactic can apply a type-specific extensionality lemma after this one. E.g., if the fiber `M` is `ℕ` or `ℤ`, then it suffices to verify `f (single a 1) = g (single a 1)`. TODO: Rename to `addMonoidHom_ext'`. -/ @[to_additive (attr := ext high) /-- If two additive homomorphisms from `R[M]` are equal on each `single r m`, then they are equal. We formulate this using equality of `AddMonoidHom`s so that `ext` tactic can apply a type-specific extensionality lemma after this one. E.g., if the fiber `M` is `ℕ` or `ℤ`, then it suffices to verify `f (single a 1) = g (single a 1)`. TODO: Rename to `addMonoidHom_ext'`. -/] lemma addHom_ext' {N : Type*} [AddZeroClass N] ⦃f g : MonoidAlgebra R M →+ N⦄ (hfg : ∀ m, f.comp (singleAddHom m) = g.comp (singleAddHom m)) : f = g := Finsupp.addHom_ext' hfg @[to_additive] lemma sum_single_index [AddCommMonoid N] {m : M} {r : R} {h : M → R → N} (h_zero : h m 0 = 0) : (single m r).sum h = h m r := by simp [h_zero] @[to_additive (attr := simp)] lemma sum_single (x : MonoidAlgebra R M) : x.sum single = x := Finsupp.sum_single _ @[to_additive] theorem single_apply {a a' : M} {b : R} [Decidable (a = a')] : single a b a' = if a = a' then b else 0 := Finsupp.single_apply @[to_additive (attr := simp)] lemma single_eq_zero : single m r = 0 ↔ r = 0 := Finsupp.single_eq_zero @[to_additive] lemma single_ne_zero : single m r ≠ 0 ↔ r ≠ 0 := by simp [single] @[to_additive (attr := elab_as_elim)] lemma induction_linear {p : MonoidAlgebra R M → Prop} (x : MonoidAlgebra R M) (zero : p 0) (add : ∀ x y : MonoidAlgebra R M, p x → p y → p (x + y)) (single : ∀ m r, p (single m r)) : p x := Finsupp.induction_linear x zero (fun _ _ ↦ add _ _) (fun _ _ ↦ single _ _) section One variable [One M] /-- The unit of the multiplication is `single 1 1`, i.e. the function that is `1` at `1` and `0` elsewhere. -/ @[to_additive (dont_translate := R) /-- The unit of the multiplication is `single 1 1`, i.e. the function that is `1` at `1` and `0` elsewhere. -/] instance one : One (MonoidAlgebra R M) where one := single 1 1 @[to_additive (dont_translate := R) one_def] lemma one_def : (1 : MonoidAlgebra R M) = single 1 1 := rfl end One section Mul variable [Mul M] /-- The multiplication in a monoid algebra. We make it irreducible so that Lean doesn't unfold it when trying to unify two different things. -/ @[irreducible] def mul' (x y : MonoidAlgebra R M) : MonoidAlgebra R M := x.sum fun m₁ r₁ ↦ y.sum fun m₂ r₂ ↦ single (m₁ * m₂) (r₁ * r₂) /-- The product of `f g : k[G]` is the finitely supported function whose value at `a` is the sum of `f x * g y` over all pairs `x, y` such that `x + y = a`. (Think of the product of multivariate polynomials where `α` is the additive monoid of monomial exponents.) -/ instance _root_.AddMonoidAlgebra.instMul [Add M] : Mul (AddMonoidAlgebra R M) where mul f g := MonoidAlgebra.mul' (M := Multiplicative M) f g /-- The product of `x y : MonoidAlgebra R M` is the finitely supported function whose value at `m` is the sum of `x m₁ * y m₂` over all pairs `m₁, m₂` such that `m₁ * m₂ = m`. (Think of the group ring of a group.) -/ @[to_additive existing instMul] instance instMul : Mul (MonoidAlgebra R M) where mul := mul' @[to_additive (dont_translate := R) mul_def] lemma mul_def (x y : MonoidAlgebra R M) : x * y = x.sum fun m₁ r₁ ↦ y.sum fun m₂ r₂ ↦ single (m₁ * m₂) (r₁ * r₂) := by with_unfolding_all rfl @[to_additive (dont_translate := R)] instance nonUnitalNonAssocSemiring : NonUnitalNonAssocSemiring (MonoidAlgebra R M) where zero_mul := by simp [mul_def] mul_zero := by simp [mul_def] -- Porting note: `refine` & `exact` are required because `simp` behaves differently. left_distrib f g h := by classical simp only [mul_def] refine Eq.trans (congr_arg (sum f) (funext₂ fun a₁ b₁ => sum_add_index ?_ ?_)) ?_ <;> simp only [mul_add, mul_zero, single_zero, single_add, forall_true_iff, sum_add] right_distrib f g h := by classical simp only [mul_def] refine Eq.trans (sum_add_index ?_ ?_) ?_ <;> simp only [add_mul, zero_mul, single_zero, single_add, forall_true_iff, sum_zero, sum_add] @[to_additive (dont_translate := R) mul_apply] lemma mul_apply [DecidableEq M] (x y : MonoidAlgebra R M) (m : M) : (x * y) m = x.sum fun m₁ r₁ ↦ y.sum fun m₂ r₂ ↦ if m₁ * m₂ = m then r₁ * r₂ else 0 := by -- Porting note: `reducible` cannot be `local` so proof gets long. rw [mul_def, Finsupp.sum_apply]; congr; ext rw [Finsupp.sum_apply]; congr; ext apply single_apply open Finset in @[to_additive (dont_translate := R) mul_apply_antidiagonal] lemma mul_apply_antidiagonal (x y : MonoidAlgebra R M) (m : M) (s : Finset (M × M)) (hs : ∀ {p}, p ∈ s ↔ p.1 * p.2 = m) : (x * y) m = ∑ p ∈ s, x p.1 * y p.2 := by classical let F (p : M × M) : R := if p.1 * p.2 = m then x p.1 * y p.2 else 0 calc (x * y) m = ∑ m₁ ∈ x.support, ∑ m₂ ∈ y.support, F (m₁, m₂) := mul_apply .. _ = ∑ p ∈ x.support ×ˢ y.support with p.1 * p.2 = m, x p.1 * y p.2 := by rw [Finset.sum_filter, Finset.sum_product] _ = ∑ p ∈ s with p.1 ∈ x.support ∧ p.2 ∈ y.support, x p.1 * y p.2 := by congr! 1; ext; simp only [mem_filter, mem_product, hs, and_comm] _ = ∑ p ∈ s, x p.1 * y p.2 := sum_subset (filter_subset _ _) fun p hps hp => by simp only [mem_filter, mem_support_iff, not_and, Classical.not_not] at hp ⊢ by_cases h1 : x p.1 = 0 · rw [h1, zero_mul] · rw [hp hps h1, mul_zero] @[to_additive (attr := simp) (dont_translate := R) single_mul_single] lemma single_mul_single (m₁ m₂ : M) (r₁ r₂ : R) : single m₁ r₁ * single m₂ r₂ = single (m₁ * m₂) (r₁ * r₂) := by simp [mul_def] @[to_additive (attr := simp) (dont_translate := R) single_commute_single] lemma single_commute_single (hm : Commute m₁ m₂) (hr : Commute r₁ r₂) : Commute (single m₁ r₁) (single m₂ r₂) := by simp [Commute, SemiconjBy, hm.eq, hr.eq] @[to_additive (attr := simp) (dont_translate := R) single_commute] lemma single_commute (hm : ∀ m', Commute m m') (hr : ∀ r', Commute r r') (x : MonoidAlgebra R M) : Commute (single m r) x := by have : AddMonoidHom.mulLeft (single m r) = AddMonoidHom.mulRight (single m r) := by ext m' r' : 2; exact single_commute_single (hm m') (hr r') exact congr($this x) @[to_additive (dont_translate := R) mul_single_apply_aux] lemma mul_single_apply_aux (H : ∀ m' ∈ x.support, m' * m = m₁ ↔ m' = m₂) : (x * single m r) m₁ = x m₂ * r := by classical calc (x * single m r) m₁ _ = x.sum fun m' r' ↦ if m' * m = m₁ then r' * r else 0 := by simp [mul_apply] _ = x.sum fun m' r' ↦ if m' = m₂ then r' * r else 0 := by dsimp [Finsupp.sum]; congr! 2; simp [*] _ = x m₂ * r := by simp +contextual [Finsupp.sum_eq_single m₂] @[to_additive (dont_translate := R) single_mul_apply_aux] lemma single_mul_apply_aux (H : ∀ m' ∈ x.support, m * m' = m₁ ↔ m' = m₂) : (single m r * x) m₁ = r * x m₂ := by classical calc (single m r * x) m₁ _ = x.sum fun m' r' ↦ if m * m' = m₁ then r * r' else 0 := by simp [mul_apply] _ = x.sum fun m' r' ↦ if m' = m₂ then r * r' else 0 := by dsimp [Finsupp.sum]; congr! 2; simp [*] _ = r * x m₂ := by simp +contextual [Finsupp.sum_eq_single m₂] @[to_additive (attr := simp) (dont_translate := R) mul_single_apply_of_not_exists_add] lemma mul_single_apply_of_not_exists_mul (r : R) (x : MonoidAlgebra R M) (h : ¬ ∃ d, m' = d * m) : (x * single m r) m' = 0 := by classical simp_all [mul_apply, eq_comm] @[to_additive (attr := simp) (dont_translate := R) single_mul_apply_of_not_exists_add] lemma single_mul_apply_of_not_exists_mul (r : R) (x : MonoidAlgebra R M) (h : ¬ ∃ d, m' = m * d) : (single m r * x) m' = 0 := by classical simp_all [mul_apply, eq_comm] variable (R M : Type*) [Semiring R] [Mul M] in /-- The embedding of a magma into its magma algebra. -/ @[simps] def ofMagma : M →ₙ* MonoidAlgebra R M where toFun a := single a 1 map_mul' a b := by simp only [mul_def, mul_one, sum_single_index, single_eq_zero, mul_zero] end Mul section Semigroup variable [Semigroup M] @[to_additive (dont_translate := R)] instance nonUnitalSemiring : NonUnitalSemiring (MonoidAlgebra R M) where mul_assoc f g h := by -- Porting note: `reducible` cannot be `local` so proof gets long. simp only [mul_def] rw [sum_sum_index] <;> congr; on_goal 1 => ext a₁ b₁ rw [sum_sum_index, sum_sum_index] <;> congr; on_goal 1 => ext a₂ b₂ rw [sum_sum_index, sum_single_index] <;> congr; on_goal 1 => ext a₃ b₃ on_goal 1 => rw [sum_single_index, mul_assoc, mul_assoc] all_goals simp only [single_zero, single_add, forall_true_iff, add_mul, mul_add, zero_mul, mul_zero, sum_zero, sum_add] end Semigroup section MulOneClass variable [MulOneClass M] @[to_additive (dont_translate := R)] instance nonAssocSemiring : NonAssocSemiring (MonoidAlgebra R M) where natCast n := single 1 n natCast_zero := by simp natCast_succ := by simp [one_def] one_mul := by simp [mul_def, one_def] mul_one := by simp [mul_def, one_def] @[to_additive (dont_translate := R)] lemma natCast_def (n : ℕ) : (n : MonoidAlgebra R M) = single (1 : M) (n : R) := rfl @[to_additive (dont_translate := R) mul_single_zero_apply] lemma mul_single_one_apply (x : MonoidAlgebra R M) (r : R) (m : M) : (x * single 1 r : MonoidAlgebra R M) m = x m * r := x.mul_single_apply_aux (by simp) @[to_additive (dont_translate := R) single_zero_mul_apply] lemma single_one_mul_apply (x : MonoidAlgebra R M) (r : R) (m : M) : (single 1 r * x : MonoidAlgebra R M) m = r * x m := x.single_mul_apply_aux (by simp) variable (R M : Type*) [Semiring R] [MulOneClass M] in /-- The embedding of a unital magma into its magma algebra. -/ @[simps] def of : M →* MonoidAlgebra R M where __ := ofMagma R M toFun m := single m 1 map_one' := rfl lemma of_injective [Nontrivial R] : Function.Injective (of R M) := fun a b h ↦ by simpa using (single_eq_single_iff _ _ _ _).mp h lemma of_commute (h : ∀ m', Commute m m') (f : MonoidAlgebra R M) : Commute (of R M m) f := single_commute h .one_left f /-- `MonoidAlgebra.single` as a `MonoidHom` from the product type into the monoid algebra. Note the order of the elements of the product are reversed compared to the arguments of `MonoidAlgebra.single`. -/ @[simps] def singleHom : R × M →* MonoidAlgebra R M where toFun a := single a.2 a.1 map_one' := rfl map_mul' _a _b := by simp /-- `MonoidAlgebra.single 1` as a `RingHom` -/ @[to_additive (attr := simps) (dont_translate := R) /-- `AddMonoidAlgebra.single 1` as a `RingHom` -/] def singleOneRingHom : R →+* MonoidAlgebra R M := { singleAddHom 1 with toFun := single 1 map_one' := rfl map_mul' := fun x y => by simp } /-- If two ring homomorphisms from `MonoidAlgebra R M` are equal on all `single m 1` and `single 1 r`, then they are equal. -/ @[to_additive (dont_translate := R S) /-- If two ring homomorphisms from `R[M]` are equal on all `single m 1` and `single 0 r`, then they are equal. -/] lemma ringHom_ext [Semiring S] {f g : MonoidAlgebra R M →+* S} (h₁ : ∀ r, f (single 1 r) = g (single 1 r)) (h_of : ∀ m, f (single m 1) = g (single m 1)) : f = g := RingHom.coe_addMonoidHom_injective <| addHom_ext fun m r ↦ by simpa [← map_mul] using congr($(h₁ r) * $(h_of m)) /-- If two ring homomorphisms from `MonoidAlgebra R M` are equal on all `single m 1` and `single 1 r`, then they are equal. See note [partially-applied ext lemmas]. -/ @[ext high] lemma ringHom_ext' [Semiring S] {f g : MonoidAlgebra R M →+* S} (h₁ : f.comp singleOneRingHom = g.comp singleOneRingHom) (h_of : (f : MonoidAlgebra R M →* S).comp (of R M) = (g : MonoidAlgebra R M →* S).comp (of R M)) : f = g := ringHom_ext (by simpa [DFunLike.ext_iff] using h₁) (by simpa [DFunLike.ext_iff] using h_of) end MulOneClass section Monoid variable [Monoid M] @[to_additive] instance semiring : Semiring (MonoidAlgebra R M) where @[to_additive (attr := simp) (dont_translate := R) single_pow] lemma single_pow (m : M) (r : R) : ∀ n : ℕ, single m r ^ n = single (m ^ n) (r ^ n) | 0 => by simp [one_def] | n + 1 => by simp [pow_succ, single_pow _ _ n] lemma induction_on {p : MonoidAlgebra R M → Prop} (x : MonoidAlgebra R M) (hM : ∀ m, p (of R M m)) (hadd : ∀ x y : MonoidAlgebra R M, p x → p y → p (x + y)) (hsmul : ∀ (r : R) (x), p x → p (r • x)) : p x := Finsupp.induction_linear x (by simpa using hsmul 0 (of R M 1) (hM 1)) (fun x y hf hg => hadd x y hf hg) fun m r ↦ by simpa using hsmul r (of R M m) (hM m) @[to_additive (dont_translate := R)] instance isLocalHom_singleOneRingHom : IsLocalHom (singleOneRingHom (R := R) (M := M)) where map_nonunit x hx := by obtain ⟨⟨x, xi, hx, hxi⟩, rfl⟩ := hx simp_rw [MonoidAlgebra.ext_iff, singleOneRingHom_apply] at hx hxi ⊢ specialize hx 1 specialize hxi 1 classical simp_rw [single_one_mul_apply, one_def, single_apply, if_pos] at hx simp_rw [mul_single_one_apply, one_def, single_apply, if_pos] at hxi exact ⟨⟨x, xi 1, hx, hxi⟩, rfl⟩ end Monoid section Group variable [Group G] @[to_additive (attr := simp) (dont_translate := R) mul_single_apply] lemma mul_single_apply (x : MonoidAlgebra R G) (r : R) (g h : G) : (x * single g r) h = x (h * g⁻¹) * r := mul_single_apply_aux <| by simp [eq_mul_inv_iff_mul_eq] @[to_additive (attr := simp) (dont_translate := R) single_mul_apply] lemma single_mul_apply (x : MonoidAlgebra R G) (r : R) (g h : G) : (single g r * x) h = r * x (g⁻¹ * h) := single_mul_apply_aux <| by simp [eq_inv_mul_iff_mul_eq] @[to_additive (dont_translate := R) mul_apply_left] lemma mul_apply_left (x y : MonoidAlgebra R G) (g : G) : (x * y) g = x.sum fun h r ↦ r * y (h⁻¹ * g) := by classical rw [mul_apply] dsimp [Finsupp.sum] congr! 1 simp +contextual [← eq_inv_mul_iff_mul_eq] @[to_additive (dont_translate := R) mul_apply_right] lemma mul_apply_right (x y : MonoidAlgebra R G) (g : G) : (x * y) g = y.sum fun h r ↦ x (g * h⁻¹) * r := by classical rw [mul_apply, Finsupp.sum_comm] dsimp [Finsupp.sum] congr! 1 simp +contextual [← eq_mul_inv_iff_mul_eq] end Group end Semiring section CommSemiring variable [CommSemiring R] @[to_additive (dont_translate := R)] instance nonUnitalCommSemiring [CommSemigroup M] : NonUnitalCommSemiring (MonoidAlgebra R M) where mul_comm f g := by simp [mul_def, Finsupp.sum, mul_comm, f.support.sum_comm] @[to_additive (dont_translate := R)] lemma single_one_comm [MulOneClass M] (r : R) (f : MonoidAlgebra R M) : single (1 : M) r * f = f * single (1 : M) r := single_commute .one_left (.all _) f section CommMonoid variable [CommMonoid M] @[to_additive (dont_translate := R)] instance commSemiring : CommSemiring (MonoidAlgebra R M) where open Finset in @[to_additive (dont_translate := R) prod_single] lemma prod_single (s : Finset ι) (m : ι → M) (r : ι → R) : ∏ i ∈ s, single (m i) (r i) = single (∏ i ∈ s, m i) (∏ i ∈ s, r i) := Finset.cons_induction_on s rfl fun i s hi ih ↦ by rw [prod_cons, ih, single_mul_single, prod_cons, prod_cons] end CommMonoid end CommSemiring section Ring variable [Ring R] @[to_additive (dont_translate := R)] instance addCommGroup : AddCommGroup (MonoidAlgebra R M) := inferInstanceAs <| AddCommGroup <| M →₀ R @[to_additive (attr := simp) (dont_translate := R)] lemma neg_apply (m : M) (x : MonoidAlgebra R M) : (-x) m = -x m := rfl @[to_additive (dont_translate := R)] lemma single_neg (m : M) (r : R) : single m (-r) = -single m r := by simp [single] @[to_additive (dont_translate := R)] instance nonUnitalNonAssocRing [Mul M] : NonUnitalNonAssocRing (MonoidAlgebra R M) where @[to_additive (dont_translate := R)] instance nonUnitalRing [Semigroup M] : NonUnitalRing (MonoidAlgebra R M) where @[to_additive (dont_translate := R)] instance nonAssocRing [MulOneClass M] : NonAssocRing (MonoidAlgebra R M) where intCast z := single 1 z intCast_ofNat n := by simp [natCast_def] intCast_negSucc n := by simp [natCast_def, one_def] @[to_additive (dont_translate := R)] lemma intCast_def [MulOneClass M] (z : ℤ) : (z : MonoidAlgebra R M) = single 1 (z : R) := rfl @[to_additive (dont_translate := R)] instance ring [Monoid M] : Ring (MonoidAlgebra R M) where end Ring section CommRing variable [CommRing R] @[to_additive (dont_translate := R)] instance nonUnitalCommRing [CommSemigroup M] : NonUnitalCommRing (MonoidAlgebra R M) where @[to_additive (dont_translate := R)] instance commRing [CommMonoid M] : CommRing (MonoidAlgebra R M) where end CommRing end MonoidAlgebra /-! ### Additive monoids -/ namespace AddMonoidAlgebra variable [Semiring R] section variable (R M : Type*) [Semiring R] /-- The embedding of an additive magma into its additive magma algebra. -/ @[simps] def ofMagma [Add M] : Multiplicative M →ₙ* R[M] where toFun a := single a 1 map_mul' a b := by simp only [mul_def, mul_one, sum_single_index, single_eq_zero, mul_zero]; rfl /-- Embedding of a magma with zero into its magma algebra. -/ def of [AddZeroClass M] : Multiplicative M →* R[M] := { ofMagma R M with toFun := fun a => single a 1 map_one' := rfl } /-- Embedding of a magma with zero `M`, into its magma algebra, having `M` as source. -/ def of' : M → R[M] := fun m => single m 1 end @[simp] theorem of_apply [AddZeroClass M] (a : Multiplicative M) : of R M a = single a.toAdd 1 := rfl @[simp] theorem of'_apply (a : M) : of' R M a = single a 1 := rfl theorem of'_eq_of [AddZeroClass M] (a : M) : of' R M a = of R M (.ofAdd a) := rfl theorem of_injective [Nontrivial R] [AddZeroClass M] : Function.Injective (of R M) := MonoidAlgebra.of_injective lemma of'_commute [AddZeroClass M] {a : M} (h : ∀ a', AddCommute a a') (f : AddMonoidAlgebra R M) : Commute (of' R M a) f := single_commute h .one_left f /-- `Finsupp.single` as a `MonoidHom` from the product type into the additive monoid algebra. Note the order of the elements of the product are reversed compared to the arguments of `Finsupp.single`. -/ @[simps] def singleHom [AddZeroClass M] : R × Multiplicative M →* R[M] where toFun a := single a.2.toAdd a.1 map_one' := rfl map_mul' _a _b := (single_mul_single ..).symm theorem induction_on [AddMonoid M] {p : R[M] → Prop} (x : R[M]) (hM : ∀ m, p (of R M <| .ofAdd m)) (hadd : ∀ x y : MonoidAlgebra R M, p x → p y → p (x + y)) (hsmul : ∀ (r : R) (x), p x → p (r • x)) : p x := Finsupp.induction_linear x (by simpa using hsmul 0 (of R M 1) (hM 0)) (fun x y hf hg ↦ hadd x y hf hg) fun m r ↦ by simpa using hsmul r (of R M m) (hM m) /-! #### Algebra structure -/ /-- If two ring homomorphisms from `R[M]` are equal on all `single m 1` and `single 0 r`, then they are equal. See note [partially-applied ext lemmas]. -/ @[ext high] theorem ringHom_ext' [Semiring S] [AddMonoid M] {f g : R[M] →+* S} (h₁ : f.comp singleZeroRingHom = g.comp singleZeroRingHom) (h_of : (f : R[M] →* S).comp (of R M) = (g : R[M] →* S).comp (of R M)) : f = g := ringHom_ext (RingHom.congr_fun h₁) (DFunLike.congr_fun h_of) end AddMonoidAlgebra
.lake/packages/mathlib/Mathlib/Algebra/MonoidAlgebra/Degree.lean
import Mathlib.Algebra.Group.Subsemigroup.Operations import Mathlib.Algebra.MonoidAlgebra.Support import Mathlib.Order.Filter.Extr /-! # Lemmas about the `sup` and `inf` of the support of `AddMonoidAlgebra` ## TODO The current plan is to state and prove lemmas about `Finset.sup (Finsupp.support f) D` with a "generic" degree/weight function `D` from the grading Type `A` to a somewhat ordered Type `B`. Next, the general lemmas get specialized for some yet-to-be-defined `degree`s. -/ variable {R R' A T B ι : Type*} namespace AddMonoidAlgebra /-! ## sup-degree and inf-degree of an `AddMonoidAlgebra` Let `R` be a semiring and let `A` be a `SemilatticeSup`. For an element `f : R[A]`, this file defines * `AddMonoidAlgebra.supDegree`: the sup-degree taking values in `WithBot A`, * `AddMonoidAlgebra.infDegree`: the inf-degree taking values in `WithTop A`. If the grading type `A` is a linearly ordered additive monoid, then these two notions of degree coincide with the standard one: * the sup-degree is the maximum of the exponents of the monomials that appear with non-zero coefficient in `f`, or `⊥`, if `f = 0`; * the inf-degree is the minimum of the exponents of the monomials that appear with non-zero coefficient in `f`, or `⊤`, if `f = 0`. The main results are * `AddMonoidAlgebra.supDegree_mul_le`: the sup-degree of a product is at most the sum of the sup-degrees, * `AddMonoidAlgebra.le_infDegree_mul`: the inf-degree of a product is at least the sum of the inf-degrees, * `AddMonoidAlgebra.supDegree_add_le`: the sup-degree of a sum is at most the sup of the sup-degrees, * `AddMonoidAlgebra.le_infDegree_add`: the inf-degree of a sum is at least the inf of the inf-degrees. ### Implementation notes The current plan is to state and prove lemmas about `Finset.sup (Finsupp.support f) D` with a "generic" degree/weight function `D` from the grading Type `A` to a somewhat ordered Type `B`. Next, the general lemmas get specialized twice: * once for `supDegree` (essentially a simple application) and * once for `infDegree` (a simple application, via `OrderDual`). These final lemmas are the ones that likely get used the most. The generic lemmas about `Finset.support.sup` may not be used directly much outside of this file. To see this in action, you can look at the triple `(sup_support_mul_le, maxDegree_mul_le, le_minDegree_mul)`. -/ section GeneralResultsAssumingSemilatticeSup variable [SemilatticeSup B] [OrderBot B] [SemilatticeInf T] [OrderTop T] section Semiring variable [Semiring R] section ExplicitDegrees /-! In this section, we use `degb` and `degt` to denote "degree functions" on `A` with values in a type with *b*ot or *t*op respectively. -/ variable (degb : A → B) (degt : A → T) (f g : R[A]) theorem sup_support_add_le : (f + g).support.sup degb ≤ f.support.sup degb ⊔ g.support.sup degb := by classical exact (Finset.sup_mono Finsupp.support_add).trans_eq Finset.sup_union theorem le_inf_support_add : f.support.inf degt ⊓ g.support.inf degt ≤ (f + g).support.inf degt := sup_support_add_le (fun a : A => OrderDual.toDual (degt a)) f g end ExplicitDegrees section AddOnly variable [Add A] [Add B] [Add T] [AddLeftMono B] [AddRightMono B] [AddLeftMono T] [AddRightMono T] theorem sup_support_mul_le {degb : A → B} (degbm : ∀ a b, degb (a + b) ≤ degb a + degb b) (f g : R[A]) : (f * g).support.sup degb ≤ f.support.sup degb + g.support.sup degb := by classical grw [support_mul, Finset.sup_add_le] rintro _fd fds _gd gds grw [degbm, ← Finset.le_sup fds, ← Finset.le_sup gds] theorem le_inf_support_mul {degt : A → T} (degtm : ∀ a b, degt a + degt b ≤ degt (a + b)) (f g : R[A]) : f.support.inf degt + g.support.inf degt ≤ (f * g).support.inf degt := sup_support_mul_le (B := Tᵒᵈ) degtm f g end AddOnly section AddMonoids variable [AddMonoid A] [AddMonoid B] [AddLeftMono B] [AddRightMono B] [AddMonoid T] [AddLeftMono T] [AddRightMono T] {degb : A → B} {degt : A → T} theorem sup_support_list_prod_le (degb0 : degb 0 ≤ 0) (degbm : ∀ a b, degb (a + b) ≤ degb a + degb b) : ∀ l : List R[A], l.prod.support.sup degb ≤ (l.map fun f : R[A] => f.support.sup degb).sum | [] => by rw [List.map_nil, Finset.sup_le_iff, List.prod_nil, List.sum_nil] exact fun a ha => by rwa [Finset.mem_singleton.mp (Finsupp.support_single_subset ha)] | f::fs => by rw [List.prod_cons, List.map_cons, List.sum_cons] grw [sup_support_mul_le degbm, sup_support_list_prod_le degb0 degbm] theorem le_inf_support_list_prod (degt0 : 0 ≤ degt 0) (degtm : ∀ a b, degt a + degt b ≤ degt (a + b)) (l : List R[A]) : (l.map fun f : R[A] => f.support.inf degt).sum ≤ l.prod.support.inf degt := by refine OrderDual.ofDual_le_ofDual.mpr ?_ refine sup_support_list_prod_le ?_ ?_ l · refine (OrderDual.ofDual_le_ofDual.mp ?_) exact degt0 · refine (fun a b => OrderDual.ofDual_le_ofDual.mp ?_) exact degtm a b theorem sup_support_pow_le (degb0 : degb 0 ≤ 0) (degbm : ∀ a b, degb (a + b) ≤ degb a + degb b) (n : ℕ) (f : R[A]) : (f ^ n).support.sup degb ≤ n • f.support.sup degb := by rw [← List.prod_replicate, ← List.sum_replicate] refine (sup_support_list_prod_le degb0 degbm _).trans_eq ?_ rw [List.map_replicate] theorem le_inf_support_pow (degt0 : 0 ≤ degt 0) (degtm : ∀ a b, degt a + degt b ≤ degt (a + b)) (n : ℕ) (f : R[A]) : n • f.support.inf degt ≤ (f ^ n).support.inf degt := by refine OrderDual.ofDual_le_ofDual.mpr <| sup_support_pow_le (OrderDual.ofDual_le_ofDual.mp ?_) (fun a b => OrderDual.ofDual_le_ofDual.mp ?_) n f · exact degt0 · exact degtm _ _ end AddMonoids end Semiring section CommutativeLemmas variable [CommSemiring R] [AddCommMonoid A] [AddCommMonoid B] [AddLeftMono B] [AddRightMono B] [AddCommMonoid T] [AddLeftMono T] [AddRightMono T] {degb : A → B} {degt : A → T} theorem sup_support_multiset_prod_le (degb0 : degb 0 ≤ 0) (degbm : ∀ a b, degb (a + b) ≤ degb a + degb b) (m : Multiset R[A]) : m.prod.support.sup degb ≤ (m.map fun f : R[A] => f.support.sup degb).sum := by induction m using Quot.inductionOn rw [Multiset.quot_mk_to_coe'', Multiset.map_coe, Multiset.sum_coe, Multiset.prod_coe] exact sup_support_list_prod_le degb0 degbm _ theorem le_inf_support_multiset_prod (degt0 : 0 ≤ degt 0) (degtm : ∀ a b, degt a + degt b ≤ degt (a + b)) (m : Multiset R[A]) : (m.map fun f : R[A] => f.support.inf degt).sum ≤ m.prod.support.inf degt := by refine OrderDual.ofDual_le_ofDual.mpr <| sup_support_multiset_prod_le (OrderDual.ofDual_le_ofDual.mp ?_) (fun a b => OrderDual.ofDual_le_ofDual.mp ?_) m · exact degt0 · exact degtm _ _ theorem sup_support_finset_prod_le (degb0 : degb 0 ≤ 0) (degbm : ∀ a b, degb (a + b) ≤ degb a + degb b) (s : Finset ι) (f : ι → R[A]) : (∏ i ∈ s, f i).support.sup degb ≤ ∑ i ∈ s, (f i).support.sup degb := (sup_support_multiset_prod_le degb0 degbm _).trans_eq <| congr_arg _ <| Multiset.map_map _ _ _ theorem le_inf_support_finset_prod (degt0 : 0 ≤ degt 0) (degtm : ∀ a b, degt a + degt b ≤ degt (a + b)) (s : Finset ι) (f : ι → R[A]) : (∑ i ∈ s, (f i).support.inf degt) ≤ (∏ i ∈ s, f i).support.inf degt := le_of_eq_of_le (by rw [Multiset.map_map]; rfl) (le_inf_support_multiset_prod degt0 degtm _) end CommutativeLemmas end GeneralResultsAssumingSemilatticeSup /-! ### Shorthands for special cases Note that these definitions are reducible, in order to make it easier to apply the more generic lemmas above. -/ section Degrees variable [Semiring R] [Ring R'] section SupDegree variable [SemilatticeSup B] [OrderBot B] (D : A → B) /-- Let `R` be a semiring, let `A` be an `AddZeroClass`, let `B` be an `OrderBot`, and let `D : A → B` be a "degree" function. For an element `f : R[A]`, the element `supDegree f : B` is the supremum of all the elements in the support of `f`, or `⊥` if `f` is zero. Often, the Type `B` is `WithBot A`, If, further, `A` has a linear order, then this notion coincides with the usual one, using the maximum of the exponents. If `A := σ →₀ ℕ` then `R[A] = MvPolynomial σ R`, and if we equip `σ` with a linear order then the induced linear order on `Lex A` equips `MvPolynomial` ring with a [monomial order](https://en.wikipedia.org/wiki/Monomial_order) (i.e. a linear order on `A`, the type of (monic) monomials in `R[A]`, that respects addition). We make use of this monomial order by taking `D := toLex`, and different monomial orders could be accessed via different type synonyms once they are added. -/ abbrev supDegree (f : R[A]) : B := f.support.sup D variable {D} theorem supDegree_add_le {f g : R[A]} : (f + g).supDegree D ≤ (f.supDegree D) ⊔ (g.supDegree D) := sup_support_add_le D f g @[simp] theorem supDegree_neg {f : R'[A]} : (-f).supDegree D = f.supDegree D := by rw [supDegree, supDegree, Finsupp.support_neg] theorem supDegree_sub_le {f g : R'[A]} : (f - g).supDegree D ≤ f.supDegree D ⊔ g.supDegree D := by rw [sub_eq_add_neg, ← supDegree_neg (f := g)]; apply supDegree_add_le theorem supDegree_sum_le {ι} {s : Finset ι} {f : ι → R[A]} : (∑ i ∈ s, f i).supDegree D ≤ s.sup (fun i => (f i).supDegree D) := by classical exact (Finset.sup_mono Finsupp.support_finset_sum).trans_eq (Finset.sup_biUnion _ _) theorem supDegree_single_ne_zero (a : A) {r : R} (hr : r ≠ 0) : (single a r).supDegree D = D a := by rw [supDegree, Finsupp.support_single_ne_zero a hr, Finset.sup_singleton] open Classical in theorem supDegree_single (a : A) (r : R) : (single a r).supDegree D = if r = 0 then ⊥ else D a := by split_ifs with hr <;> simp [supDegree_single_ne_zero, hr] theorem apply_eq_zero_of_not_le_supDegree {p : R[A]} {a : A} (hlt : ¬ D a ≤ p.supDegree D) : p a = 0 := by contrapose! hlt exact Finset.le_sup (Finsupp.mem_support_iff.2 hlt) theorem supDegree_withBot_some_comp {s : AddMonoidAlgebra R A} (hs : s.support.Nonempty) : supDegree (WithBot.some ∘ D) s = supDegree D s := by unfold AddMonoidAlgebra.supDegree rw [← Finset.coe_sup' hs, Finset.sup'_eq_sup] theorem supDegree_eq_of_isMaxOn {p : R[A]} {a : A} (hmem : a ∈ p.support) (hmax : IsMaxOn D p.support a) : p.supDegree D = D a := sup_eq_of_isMaxOn hmem hmax variable [AddZeroClass A] {p q : R[A]} @[simp] theorem supDegree_zero : (0 : R[A]).supDegree D = ⊥ := by simp [supDegree] theorem ne_zero_of_supDegree_ne_bot : p.supDegree D ≠ ⊥ → p ≠ 0 := mt (fun h => h ▸ supDegree_zero) theorem ne_zero_of_not_supDegree_le {b : B} (h : ¬ p.supDegree D ≤ b) : p ≠ 0 := ne_zero_of_supDegree_ne_bot (fun he => h <| he ▸ bot_le) theorem supDegree_eq_of_max {b : B} (hb : b ∈ Set.range D) (hmem : D.invFun b ∈ p.support) (hmax : ∀ a ∈ p.support, D a ≤ b) : p.supDegree D = b := sup_eq_of_max hb hmem hmax variable [Add B] theorem supDegree_mul_le (hadd : ∀ a1 a2, D (a1 + a2) = D a1 + D a2) [AddLeftMono B] [AddRightMono B] : (p * q).supDegree D ≤ p.supDegree D + q.supDegree D := sup_support_mul_le (fun {_ _} => (hadd _ _).le) p q theorem supDegree_prod_le {R A B : Type*} [CommSemiring R] [AddCommMonoid A] [AddCommMonoid B] [SemilatticeSup B] [OrderBot B] [AddLeftMono B] [AddRightMono B] {D : A → B} (hzero : D 0 = 0) (hadd : ∀ a1 a2, D (a1 + a2) = D a1 + D a2) {ι} {s : Finset ι} {f : ι → R[A]} : (∏ i ∈ s, f i).supDegree D ≤ ∑ i ∈ s, (f i).supDegree D := by classical refine s.induction ?_ ?_ · rw [Finset.prod_empty, Finset.sum_empty, one_def, supDegree_single] split_ifs; exacts [bot_le, hzero.le] · intro i s his ih rw [Finset.prod_insert his, Finset.sum_insert his] exact (supDegree_mul_le hadd).trans (by gcongr) theorem apply_add_of_supDegree_le (hadd : ∀ a1 a2, D (a1 + a2) = D a1 + D a2) [AddLeftStrictMono B] [AddRightStrictMono B] (hD : D.Injective) {ap aq : A} (hp : p.supDegree D ≤ D ap) (hq : q.supDegree D ≤ D aq) : (p * q) (ap + aq) = p ap * q aq := by classical simp_rw [mul_apply, Finsupp.sum] rw [Finset.sum_eq_single ap, Finset.sum_eq_single aq, if_pos rfl] · refine fun a ha hne => if_neg (fun he => ?_) apply_fun D at he; simp_rw [hadd] at he exact (add_lt_add_left (((Finset.le_sup ha).trans hq).lt_of_ne <| hD.ne_iff.2 hne) _).ne he · intro h; rw [if_pos rfl, Finsupp.notMem_support_iff.1 h, mul_zero] · refine fun a ha hne => Finset.sum_eq_zero (fun a' ha' => if_neg <| fun he => ?_) apply_fun D at he simp_rw [hadd] at he have := addLeftMono_of_addLeftStrictMono B exact (add_lt_add_of_lt_of_le (((Finset.le_sup ha).trans hp).lt_of_ne <| hD.ne_iff.2 hne) <| (Finset.le_sup ha').trans hq).ne he · refine fun h => Finset.sum_eq_zero (fun a _ => ite_eq_right_iff.mpr <| fun _ => ?_) rw [Finsupp.notMem_support_iff.mp h, zero_mul] end SupDegree section LinearOrder variable [LinearOrder B] [OrderBot B] {p q : R[A]} (D : A → B) /-- If `D` is an injection into a linear order `B`, the leading coefficient of `f : R[A]` is the nonzero coefficient of highest degree according to `D`, or 0 if `f = 0`. In general, it is defined to be the coefficient at an inverse image of `supDegree f` (if such exists). -/ noncomputable def leadingCoeff [Nonempty A] (f : R[A]) : R := f (D.invFun <| f.supDegree D) /-- An element `f : R[A]` is monic if its leading coefficient is one. -/ @[reducible] def Monic [Nonempty A] (f : R[A]) : Prop := f.leadingCoeff D = 1 variable {D} @[simp] theorem leadingCoeff_single [Nonempty A] (hD : D.Injective) (a : A) (r : R) : (single a r).leadingCoeff D = r := by classical rw [leadingCoeff, supDegree_single] split_ifs with hr · simp [hr] · rw [Function.leftInverse_invFun hD, single_apply, if_pos rfl] @[simp] theorem leadingCoeff_zero [Nonempty A] : (0 : R[A]).leadingCoeff D = 0 := rfl lemma Monic.ne_zero [Nonempty A] [Nontrivial R] (hp : p.Monic D) : p ≠ 0 := fun h => by simp_rw [Monic, h, leadingCoeff_zero, zero_ne_one] at hp @[simp] theorem monic_one [AddZeroClass A] (hD : D.Injective) : (1 : R[A]).Monic D := by rw [Monic, one_def, leadingCoeff_single hD] variable (D) in lemma exists_supDegree_mem_support (hp : p ≠ 0) : ∃ a ∈ p.support, p.supDegree D = D a := Finset.exists_mem_eq_sup _ (Finsupp.support_nonempty_iff.mpr hp) D variable (D) in lemma supDegree_mem_range (hp : p ≠ 0) : p.supDegree D ∈ Set.range D := by obtain ⟨a, -, he⟩ := exists_supDegree_mem_support D hp; exact ⟨a, he.symm⟩ variable {ι : Type*} {s : Finset ι} {i : ι} (hi : i ∈ s) {f : ι → R[A]} lemma supDegree_sum_lt (hs : s.Nonempty) {b : B} (h : ∀ i ∈ s, (f i).supDegree D < b) : (∑ i ∈ s, f i).supDegree D < b := by refine supDegree_sum_le.trans_lt ((Finset.sup_lt_iff ?_).mpr h) obtain ⟨i, hi⟩ := hs; exact bot_le.trans_lt (h i hi) variable [AddZeroClass A] open Finsupp in lemma supDegree_add_eq_left (h : q.supDegree D < p.supDegree D) : (p + q).supDegree D = p.supDegree D := by apply (supDegree_add_le.trans <| sup_le le_rfl h.le).antisymm obtain ⟨a, ha, he⟩ := exists_supDegree_mem_support D (ne_zero_of_not_supDegree_le h.not_ge) rw [he] at h ⊢ apply Finset.le_sup rw [mem_support_iff, add_apply, apply_eq_zero_of_not_le_supDegree h.not_ge, add_zero] exact mem_support_iff.mp ha lemma supDegree_add_eq_right (h : p.supDegree D < q.supDegree D) : (p + q).supDegree D = q.supDegree D := by rw [add_comm, supDegree_add_eq_left h] lemma leadingCoeff_add_eq_left (h : q.supDegree D < p.supDegree D) : (p + q).leadingCoeff D = p.leadingCoeff D := by obtain ⟨a, he⟩ := supDegree_mem_range D (ne_zero_of_not_supDegree_le h.not_ge) rw [leadingCoeff, supDegree_add_eq_left h, Finsupp.add_apply, ← leadingCoeff, apply_eq_zero_of_not_le_supDegree (D := D), add_zero] rw [← he, Function.apply_invFun_apply (f := D), he]; exact h.not_ge lemma leadingCoeff_add_eq_right (h : p.supDegree D < q.supDegree D) : (p + q).leadingCoeff D = q.leadingCoeff D := by rw [add_comm, leadingCoeff_add_eq_left h] lemma supDegree_mem_support (hD : D.Injective) (hp : p ≠ 0) : D.invFun (p.supDegree D) ∈ p.support := by obtain ⟨a, ha, he⟩ := exists_supDegree_mem_support D hp rwa [he, Function.leftInverse_invFun hD] @[simp] lemma leadingCoeff_eq_zero (hD : D.Injective) : p.leadingCoeff D = 0 ↔ p = 0 := by refine ⟨(fun h => ?_).mtr, fun h => h ▸ leadingCoeff_zero⟩ rw [leadingCoeff, ← Ne, ← Finsupp.mem_support_iff] exact supDegree_mem_support hD h lemma leadingCoeff_ne_zero (hD : D.Injective) : p.leadingCoeff D ≠ 0 ↔ p ≠ 0 := (leadingCoeff_eq_zero hD).ne lemma supDegree_sub_lt_of_leadingCoeff_eq (hD : D.Injective) {R} [Ring R] {p q : R[A]} (hd : p.supDegree D = q.supDegree D) (hc : p.leadingCoeff D = q.leadingCoeff D) : (p - q).supDegree D < p.supDegree D ∨ p = q := by rw [or_iff_not_imp_right] refine fun he => (supDegree_sub_le.trans ?_).lt_of_ne ?_ · rw [hd, sup_idem] · rw [← sub_eq_zero, ← leadingCoeff_eq_zero hD, leadingCoeff] at he refine fun h => he ?_ rwa [h, Finsupp.sub_apply, ← leadingCoeff, hd, ← leadingCoeff, sub_eq_zero] lemma supDegree_leadingCoeff_sum_eq (hi : i ∈ s) (hmax : ∀ j ∈ s, j ≠ i → (f j).supDegree D < (f i).supDegree D) : (∑ j ∈ s, f j).supDegree D = (f i).supDegree D ∧ (∑ j ∈ s, f j).leadingCoeff D = (f i).leadingCoeff D := by classical rw [← s.add_sum_erase _ hi] by_cases hs : s.erase i = ∅ · rw [hs, Finset.sum_empty, add_zero]; exact ⟨rfl, rfl⟩ suffices _ from ⟨supDegree_add_eq_left this, leadingCoeff_add_eq_left this⟩ refine supDegree_sum_lt ?_ (fun j hj => ?_) · rw [Finset.nonempty_iff_ne_empty]; exact hs · rw [Finset.mem_erase] at hj; exact hmax j hj.2 hj.1 open Finset in lemma sum_ne_zero_of_injOn_supDegree' (hs : ∃ i ∈ s, f i ≠ 0) (hd : (s : Set ι).InjOn (supDegree D ∘ f)) : ∑ i ∈ s, f i ≠ 0 := by obtain ⟨j, hj, hne⟩ := hs obtain ⟨i, hi, he⟩ := exists_mem_eq_sup _ ⟨j, hj⟩ (supDegree D ∘ f) by_cases! h : ∀ k ∈ s, k = i · refine (sum_eq_single_of_mem j hj (fun k hk hne => ?_)).trans_ne hne rw [h k hk, h j hj] at hne; exact hne.irrefl.elim obtain ⟨j, hj, hne⟩ := h apply ne_zero_of_supDegree_ne_bot (D := D) have (k) (hk : k ∈ s) (hne : k ≠ i) : supDegree D (f k) < supDegree D (f i) := ((le_sup hk).trans_eq he).lt_of_ne (hd.ne hk hi hne) rw [(supDegree_leadingCoeff_sum_eq hi this).1] exact (this j hj hne).ne_bot lemma sum_ne_zero_of_injOn_supDegree (hs : s ≠ ∅) (hf : ∀ i ∈ s, f i ≠ 0) (hd : (s : Set ι).InjOn (supDegree D ∘ f)) : ∑ i ∈ s, f i ≠ 0 := let ⟨i, hi⟩ := Finset.nonempty_iff_ne_empty.2 hs sum_ne_zero_of_injOn_supDegree' ⟨i, hi, hf i hi⟩ hd variable [Add B] variable [AddLeftStrictMono B] [AddRightStrictMono B] lemma apply_supDegree_add_supDegree (hD : D.Injective) (hadd : ∀ a1 a2, D (a1 + a2) = D a1 + D a2) : (p * q) (D.invFun (p.supDegree D + q.supDegree D)) = p.leadingCoeff D * q.leadingCoeff D := by obtain rfl | hp := eq_or_ne p 0 · simp_rw [leadingCoeff_zero, zero_mul, Finsupp.coe_zero, Pi.zero_apply] obtain rfl | hq := eq_or_ne q 0 · simp_rw [leadingCoeff_zero, mul_zero, Finsupp.coe_zero, Pi.zero_apply] obtain ⟨ap, -, hp⟩ := exists_supDegree_mem_support D hp obtain ⟨aq, -, hq⟩ := exists_supDegree_mem_support D hq simp_rw [leadingCoeff, hp, hq, ← hadd, Function.leftInverse_invFun hD _] exact apply_add_of_supDegree_le hadd hD hp.le hq.le lemma supDegree_mul (hD : D.Injective) (hadd : ∀ a1 a2, D (a1 + a2) = D a1 + D a2) (hpq : leadingCoeff D p * leadingCoeff D q ≠ 0) (hp : p ≠ 0) (hq : q ≠ 0) : (p * q).supDegree D = p.supDegree D + q.supDegree D := by apply supDegree_eq_of_max · rw [← AddSubsemigroup.coe_set_mk (Set.range D), ← AddHom.srange_mk _ hadd, SetLike.mem_coe] · exact add_mem (supDegree_mem_range D hp) (supDegree_mem_range D hq) · exact (AddHom.srange ⟨D, hadd⟩).add_mem · simp_rw [Finsupp.mem_support_iff, apply_supDegree_add_supDegree hD hadd] exact hpq · have := addLeftMono_of_addLeftStrictMono B have := addRightMono_of_addRightStrictMono B exact fun a ha => (Finset.le_sup ha).trans (supDegree_mul_le hadd) lemma Monic.supDegree_mul_of_ne_zero_left (hD : D.Injective) (hadd : ∀ a1 a2, D (a1 + a2) = D a1 + D a2) (hq : q.Monic D) (hp : p ≠ 0) : (p * q).supDegree D = p.supDegree D + q.supDegree D := by cases subsingleton_or_nontrivial R; · exact (hp (Subsingleton.elim _ _)).elim apply supDegree_mul hD hadd ?_ hp hq.ne_zero simp_rw [hq, mul_one, Ne, leadingCoeff_eq_zero hD, hp, not_false_eq_true] lemma Monic.supDegree_mul_of_ne_zero_right (hD : D.Injective) (hadd : ∀ a1 a2, D (a1 + a2) = D a1 + D a2) (hp : p.Monic D) (hq : q ≠ 0) : (p * q).supDegree D = p.supDegree D + q.supDegree D := by cases subsingleton_or_nontrivial R; · exact (hq (Subsingleton.elim _ _)).elim apply supDegree_mul hD hadd ?_ hp.ne_zero hq simp_rw [hp, one_mul, Ne, leadingCoeff_eq_zero hD, hq, not_false_eq_true] lemma Monic.supDegree_mul (hD : D.Injective) (hadd : ∀ a1 a2, D (a1 + a2) = D a1 + D a2) (hbot : (⊥ : B) + ⊥ = ⊥) (hp : p.Monic D) (hq : q.Monic D) : (p * q).supDegree D = p.supDegree D + q.supDegree D := by cases subsingleton_or_nontrivial R · simp_rw [Subsingleton.eq_zero p, Subsingleton.eq_zero q, mul_zero, supDegree_zero, hbot] exact hq.supDegree_mul_of_ne_zero_left hD hadd hp.ne_zero lemma leadingCoeff_mul [NoZeroDivisors R] (hD : D.Injective) (hadd : ∀ a1 a2, D (a1 + a2) = D a1 + D a2) : (p * q).leadingCoeff D = p.leadingCoeff D * q.leadingCoeff D := by obtain rfl | hp := eq_or_ne p 0 · simp_rw [leadingCoeff_zero, zero_mul, leadingCoeff_zero] obtain rfl | hq := eq_or_ne q 0 · simp_rw [leadingCoeff_zero, mul_zero, leadingCoeff_zero] rw [← apply_supDegree_add_supDegree hD hadd, ← supDegree_mul hD hadd ?_ hp hq, leadingCoeff] apply mul_ne_zero <;> rwa [Ne, leadingCoeff_eq_zero hD] lemma Monic.leadingCoeff_mul_eq_left (hD : D.Injective) (hadd : ∀ a1 a2, D (a1 + a2) = D a1 + D a2) (hq : q.Monic D) : (p * q).leadingCoeff D = p.leadingCoeff D := by obtain rfl | hp := eq_or_ne p 0 · rw [zero_mul] rw [leadingCoeff, hq.supDegree_mul_of_ne_zero_left hD hadd hp, apply_supDegree_add_supDegree hD hadd, hq, mul_one] lemma Monic.leadingCoeff_mul_eq_right (hD : D.Injective) (hadd : ∀ a1 a2, D (a1 + a2) = D a1 + D a2) (hp : p.Monic D) : (p * q).leadingCoeff D = q.leadingCoeff D := by obtain rfl | hq := eq_or_ne q 0 · rw [mul_zero] rw [leadingCoeff, hp.supDegree_mul_of_ne_zero_right hD hadd hq, apply_supDegree_add_supDegree hD hadd, hp, one_mul] lemma Monic.mul (hD : D.Injective) (hadd : ∀ a1 a2, D (a1 + a2) = D a1 + D a2) (hp : p.Monic D) (hq : q.Monic D) : (p * q).Monic D := by rw [Monic, hq.leadingCoeff_mul_eq_left hD hadd]; exact hp section AddMonoid variable {A B : Type*} [AddMonoid A] [AddMonoid B] [LinearOrder B] [OrderBot B] [AddLeftStrictMono B] [AddRightStrictMono B] {D : A → B} {p : R[A]} {n : ℕ} lemma Monic.pow (hadd : ∀ a1 a2, D (a1 + a2) = D a1 + D a2) (hD : D.Injective) (hp : p.Monic D) : (p ^ n).Monic D := by induction n with | zero => rw [pow_zero]; exact monic_one hD | succ n ih => rw [pow_succ']; exact hp.mul hD hadd ih lemma Monic.supDegree_pow (hzero : D 0 = 0) (hadd : ∀ a1 a2, D (a1 + a2) = D a1 + D a2) (hD : D.Injective) [Nontrivial R] (hp : p.Monic D) : (p ^ n).supDegree D = n • p.supDegree D := by induction n with | zero => rw [pow_zero, zero_nsmul, one_def, supDegree_single 0 1, if_neg one_ne_zero, hzero] | succ n ih => rw [pow_succ', (hp.pow hadd hD).supDegree_mul_of_ne_zero_left hD hadd hp.ne_zero, ih, succ_nsmul'] end AddMonoid end LinearOrder section InfDegree variable [SemilatticeInf T] [OrderTop T] (D : A → T) /-- Let `R` be a semiring, let `A` be an `AddZeroClass`, let `T` be an `OrderTop`, and let `D : A → T` be a "degree" function. For an element `f : R[A]`, the element `infDegree f : T` is the infimum of all the elements in the support of `f`, or `⊤` if `f` is zero. Often, the Type `T` is `WithTop A`, If, further, `A` has a linear order, then this notion coincides with the usual one, using the minimum of the exponents. -/ abbrev infDegree (f : R[A]) : T := f.support.inf D theorem le_infDegree_add (f g : R[A]) : (f.infDegree D) ⊓ (g.infDegree D) ≤ (f + g).infDegree D := le_inf_support_add D f g variable {D} in theorem infDegree_withTop_some_comp {s : AddMonoidAlgebra R A} (hs : s.support.Nonempty) : infDegree (WithTop.some ∘ D) s = infDegree D s := by unfold AddMonoidAlgebra.infDegree rw [← Finset.coe_inf' hs, Finset.inf'_eq_inf] theorem le_infDegree_mul [AddZeroClass A] [Add T] [AddLeftMono T] [AddRightMono T] (D : AddHom A T) (f g : R[A]) : f.infDegree D + g.infDegree D ≤ (f * g).infDegree D := le_inf_support_mul (fun {a b : A} => (map_add D a b).ge) _ _ end InfDegree end Degrees end AddMonoidAlgebra
.lake/packages/mathlib/Mathlib/Algebra/MonoidAlgebra/ToDirectSum.lean
import Mathlib.Algebra.DirectSum.Algebra import Mathlib.Algebra.MonoidAlgebra.Basic import Mathlib.Data.Finsupp.ToDFinsupp /-! # Conversion between `AddMonoidAlgebra` and homogeneous `DirectSum` This module provides conversions between `AddMonoidAlgebra` and `DirectSum`. The latter is essentially a dependent version of the former. Note that since `DirectSum.instMul` combines indices additively, there is no equivalent to `MonoidAlgebra`. ## Main definitions * `AddMonoidAlgebra.toDirectSum : AddMonoidAlgebra M ι → (⨁ i : ι, M)` * `DirectSum.toAddMonoidAlgebra : (⨁ i : ι, M) → AddMonoidAlgebra M ι` * Bundled equiv versions of the above: * `addMonoidAlgebraEquivDirectSum : AddMonoidAlgebra M ι ≃ (⨁ i : ι, M)` * `addMonoidAlgebraAddEquivDirectSum : AddMonoidAlgebra M ι ≃+ (⨁ i : ι, M)` * `addMonoidAlgebraRingEquivDirectSum R : AddMonoidAlgebra M ι ≃+* (⨁ i : ι, M)` * `addMonoidAlgebraAlgEquivDirectSum R : AddMonoidAlgebra A ι ≃ₐ[R] (⨁ i : ι, A)` ## Theorems The defining feature of these operations is that they map `Finsupp.single` to `DirectSum.of` and vice versa: * `AddMonoidAlgebra.toDirectSum_single` * `DirectSum.toAddMonoidAlgebra_of` as well as preserving arithmetic operations. For the bundled equivalences, we provide lemmas that they reduce to `AddMonoidAlgebra.toDirectSum`: * `addMonoidAlgebraAddEquivDirectSum_apply` * `add_monoid_algebra_lequiv_direct_sum_apply` * `addMonoidAlgebraAddEquivDirectSum_symm_apply` * `add_monoid_algebra_lequiv_direct_sum_symm_apply` ## Implementation notes This file largely just copies the API of `Mathlib/Data/Finsupp/ToDFinsupp.lean`, and reuses the proofs. Recall that `AddMonoidAlgebra M ι` is defeq to `ι →₀ M` and `⨁ i : ι, M` is defeq to `Π₀ i : ι, M`. Note that there is no `AddMonoidAlgebra` equivalent to `Finsupp.single`, so many statements still involve this definition. -/ variable {ι : Type*} {R : Type*} {M : Type*} {A : Type*} open DirectSum /-! ### Basic definitions and lemmas -/ section Defs /-- Interpret an `AddMonoidAlgebra` as a homogeneous `DirectSum`. -/ def AddMonoidAlgebra.toDirectSum [Semiring M] (f : AddMonoidAlgebra M ι) : ⨁ _ : ι, M := Finsupp.toDFinsupp f section variable [DecidableEq ι] [Semiring M] @[simp] theorem AddMonoidAlgebra.toDirectSum_single (i : ι) (m : M) : AddMonoidAlgebra.toDirectSum (Finsupp.single i m) = DirectSum.of _ i m := Finsupp.toDFinsupp_single i m variable [∀ m : M, Decidable (m ≠ 0)] /-- Interpret a homogeneous `DirectSum` as an `AddMonoidAlgebra`. -/ def DirectSum.toAddMonoidAlgebra (f : ⨁ _ : ι, M) : AddMonoidAlgebra M ι := DFinsupp.toFinsupp f @[simp] theorem DirectSum.toAddMonoidAlgebra_of (i : ι) (m : M) : (DirectSum.of _ i m : ⨁ _ : ι, M).toAddMonoidAlgebra = Finsupp.single i m := DFinsupp.toFinsupp_single i m @[simp] theorem AddMonoidAlgebra.toDirectSum_toAddMonoidAlgebra (f : AddMonoidAlgebra M ι) : f.toDirectSum.toAddMonoidAlgebra = f := Finsupp.toDFinsupp_toFinsupp f @[simp] theorem DirectSum.toAddMonoidAlgebra_toDirectSum (f : ⨁ _ : ι, M) : f.toAddMonoidAlgebra.toDirectSum = f := (DFinsupp.toFinsupp_toDFinsupp (show Π₀ _ : ι, M from f) :) end end Defs /-! ### Lemmas about arithmetic operations -/ section Lemmas namespace AddMonoidAlgebra @[simp] theorem toDirectSum_zero [Semiring M] : (0 : AddMonoidAlgebra M ι).toDirectSum = 0 := Finsupp.toDFinsupp_zero @[simp] theorem toDirectSum_add [Semiring M] (f g : AddMonoidAlgebra M ι) : (f + g).toDirectSum = f.toDirectSum + g.toDirectSum := Finsupp.toDFinsupp_add _ _ @[simp] theorem toDirectSum_natCast [DecidableEq ι] [AddMonoid ι] [Semiring M] (n : ℕ) : (n : AddMonoidAlgebra M ι).toDirectSum = n := Finsupp.toDFinsupp_single _ _ @[simp] theorem toDirectSum_ofNat [DecidableEq ι] [AddMonoid ι] [Semiring M] (n : ℕ) [n.AtLeastTwo] : (ofNat(n) : AddMonoidAlgebra M ι).toDirectSum = ofNat(n) := Finsupp.toDFinsupp_single _ _ @[simp] theorem toDirectSum_sub [Ring M] (f g : AddMonoidAlgebra M ι) : (f - g).toDirectSum = f.toDirectSum - g.toDirectSum := Finsupp.toDFinsupp_sub _ _ @[simp] theorem toDirectSum_neg [Ring M] (f : AddMonoidAlgebra M ι) : (- f).toDirectSum = - f.toDirectSum := Finsupp.toDFinsupp_neg _ @[simp] theorem toDirectSum_intCast [DecidableEq ι] [AddMonoid ι] [Ring M] (z : ℤ) : (Int.cast z : AddMonoidAlgebra M ι).toDirectSum = z := Finsupp.toDFinsupp_single _ _ @[simp] theorem toDirectSum_one [DecidableEq ι] [Zero ι] [Semiring M] : (1 : AddMonoidAlgebra M ι).toDirectSum = 1 := Finsupp.toDFinsupp_single _ _ @[simp] theorem toDirectSum_mul [DecidableEq ι] [AddMonoid ι] [Semiring M] (f g : AddMonoidAlgebra M ι) : (f * g).toDirectSum = f.toDirectSum * g.toDirectSum := by let to_hom : AddMonoidAlgebra M ι →+ ⨁ _ : ι, M := { toFun := toDirectSum map_zero' := toDirectSum_zero map_add' := toDirectSum_add } change to_hom (f * g) = to_hom f * to_hom g revert f g rw [AddMonoidHom.map_mul_iff] ext xi xv yi yv : 4 simp [to_hom, AddMonoidAlgebra.single_mul_single, DirectSum.of_mul_of] end AddMonoidAlgebra namespace DirectSum variable [DecidableEq ι] @[simp] theorem toAddMonoidAlgebra_zero [Semiring M] [∀ m : M, Decidable (m ≠ 0)] : toAddMonoidAlgebra 0 = (0 : AddMonoidAlgebra M ι) := DFinsupp.toFinsupp_zero @[simp] theorem toAddMonoidAlgebra_add [Semiring M] [∀ m : M, Decidable (m ≠ 0)] (f g : ⨁ _ : ι, M) : (f + g).toAddMonoidAlgebra = toAddMonoidAlgebra f + toAddMonoidAlgebra g := DFinsupp.toFinsupp_add _ _ @[simp] theorem toAddMonoidAlgebra_natCast [AddMonoid ι] [Semiring M] [∀ m : M, Decidable (m ≠ 0)] (n : ℕ) : (n : ⨁ _ : ι, M).toAddMonoidAlgebra = n := DFinsupp.toFinsupp_single _ _ @[simp] theorem toAddMonoidAlgebra_ofNat [AddMonoid ι] [Semiring M] [∀ m : M, Decidable (m ≠ 0)] (n : ℕ) [n.AtLeastTwo] : (ofNat(n) : ⨁ _ : ι, M).toAddMonoidAlgebra = ofNat(n) := DFinsupp.toFinsupp_single _ _ @[simp] theorem toAddMonoidAlgebra_sub [Ring M] [∀ m : M, Decidable (m ≠ 0)] (f g : ⨁ _ : ι, M) : (f - g).toAddMonoidAlgebra = toAddMonoidAlgebra f - toAddMonoidAlgebra g := DFinsupp.toFinsupp_sub _ _ @[simp] theorem toAddMonoidAlgebra_neg [Ring M] [∀ m : M, Decidable (m ≠ 0)] (f : ⨁ _ : ι, M) : (- f).toAddMonoidAlgebra = - toAddMonoidAlgebra f := DFinsupp.toFinsupp_neg _ @[simp] theorem toAddMonoidAlgebra_intCast [AddMonoid ι] [Ring M] [∀ m : M, Decidable (m ≠ 0)] (z : ℤ) : (z : ⨁ _ : ι, M).toAddMonoidAlgebra = z := DFinsupp.toFinsupp_single _ _ @[simp] theorem toAddMonoidAlgebra_one [Zero ι] [Semiring M] [∀ m : M, Decidable (m ≠ 0)] : (1 : ⨁ _ : ι, M).toAddMonoidAlgebra = 1 := DFinsupp.toFinsupp_single _ _ @[simp] theorem toAddMonoidAlgebra_mul [AddMonoid ι] [Semiring M] [∀ m : M, Decidable (m ≠ 0)] (f g : ⨁ _ : ι, M) : (f * g).toAddMonoidAlgebra = toAddMonoidAlgebra f * toAddMonoidAlgebra g := by apply_fun AddMonoidAlgebra.toDirectSum · simp · apply Function.LeftInverse.injective apply AddMonoidAlgebra.toDirectSum_toAddMonoidAlgebra end DirectSum end Lemmas /-! ### Bundled `Equiv`s -/ section Equivs /-- `AddMonoidAlgebra.toDirectSum` and `DirectSum.toAddMonoidAlgebra` together form an equiv. -/ @[simps -fullyApplied] def addMonoidAlgebraEquivDirectSum [DecidableEq ι] [Semiring M] [∀ m : M, Decidable (m ≠ 0)] : AddMonoidAlgebra M ι ≃ ⨁ _ : ι, M := { finsuppEquivDFinsupp with toFun := AddMonoidAlgebra.toDirectSum invFun := DirectSum.toAddMonoidAlgebra } /-- The additive version of `AddMonoidAlgebra.addMonoidAlgebraEquivDirectSum`. -/ @[simps -fullyApplied] def addMonoidAlgebraAddEquivDirectSum [DecidableEq ι] [Semiring M] [∀ m : M, Decidable (m ≠ 0)] : AddMonoidAlgebra M ι ≃+ ⨁ _ : ι, M := { addMonoidAlgebraEquivDirectSum with toFun := AddMonoidAlgebra.toDirectSum invFun := DirectSum.toAddMonoidAlgebra map_add' := AddMonoidAlgebra.toDirectSum_add } /-- The ring version of `AddMonoidAlgebra.addMonoidAlgebraEquivDirectSum`. -/ @[simps -fullyApplied] def addMonoidAlgebraRingEquivDirectSum [DecidableEq ι] [AddMonoid ι] [Semiring M] [∀ m : M, Decidable (m ≠ 0)] : AddMonoidAlgebra M ι ≃+* ⨁ _ : ι, M := { (addMonoidAlgebraAddEquivDirectSum : AddMonoidAlgebra M ι ≃+ ⨁ _ : ι, M) with toFun := AddMonoidAlgebra.toDirectSum invFun := DirectSum.toAddMonoidAlgebra map_mul' := AddMonoidAlgebra.toDirectSum_mul } /-- The algebra version of `AddMonoidAlgebra.addMonoidAlgebraEquivDirectSum`. -/ @[simps -fullyApplied] def addMonoidAlgebraAlgEquivDirectSum [DecidableEq ι] [AddMonoid ι] [CommSemiring R] [Semiring A] [Algebra R A] [∀ m : A, Decidable (m ≠ 0)] : AddMonoidAlgebra A ι ≃ₐ[R] ⨁ _ : ι, A := { (addMonoidAlgebraRingEquivDirectSum : AddMonoidAlgebra A ι ≃+* ⨁ _ : ι, A) with toFun := AddMonoidAlgebra.toDirectSum invFun := DirectSum.toAddMonoidAlgebra commutes' := fun _r => AddMonoidAlgebra.toDirectSum_single _ _ } @[simp] theorem AddMonoidAlgebra.toDirectSum_pow [DecidableEq ι] [AddMonoid ι] [Semiring M] (f : AddMonoidAlgebra M ι) (n : ℕ) : (f ^ n).toDirectSum = f.toDirectSum ^ n := by classical exact map_pow addMonoidAlgebraRingEquivDirectSum f n @[simp] theorem DirectSum.toAddMonoidAlgebra_pow [DecidableEq ι] [AddMonoid ι] [Semiring M] [∀ m : M, Decidable (m ≠ 0)] (f : ⨁ _ : ι, M) (n : ℕ): (f ^ n).toAddMonoidAlgebra = toAddMonoidAlgebra f ^ n := by classical exact map_pow addMonoidAlgebraRingEquivDirectSum.symm f n end Equivs
.lake/packages/mathlib/Mathlib/Algebra/MonoidAlgebra/Lift.lean
import Mathlib.Algebra.MonoidAlgebra.Defs /-! # Lifting monoid algebras This file defines `liftNC`. For the definition of `MonoidAlgebra.lift`, see `Mathlib/Algebra/MonoidAlgebra/Basic.lean`. ## Main results * `MonoidAlgebra.liftNC`, `AddMonoidAlgebra.liftNC`: lift a homomorphism `f : k →+ R` and a function `g : G → R` to a homomorphism `MonoidAlgebra k G →+ R`. -/ assert_not_exists NonUnitalAlgHom AlgEquiv noncomputable section open Finsupp hiding single universe u₁ u₂ u₃ u₄ variable (k : Type u₁) (G : Type u₂) (H : Type*) {R S T M : Type*} /-! ### Multiplicative monoids -/ namespace MonoidAlgebra variable {k G} section variable [Semiring k] [NonUnitalNonAssocSemiring R] /-- A non-commutative version of `MonoidAlgebra.lift`: given an additive homomorphism `f : k →+ R` and a homomorphism `g : G → R`, returns the additive homomorphism from `MonoidAlgebra k G` such that `liftNC f g (single a b) = f b * g a`. If `f` is a ring homomorphism and the range of either `f` or `g` is in center of `R`, then the result is a ring homomorphism. If `R` is a `k`-algebra and `f = algebraMap k R`, then the result is an algebra homomorphism called `MonoidAlgebra.lift`. -/ def liftNC (f : k →+ R) (g : G → R) : MonoidAlgebra k G →+ R := liftAddHom fun x : G => (AddMonoidHom.mulRight (g x)).comp f @[simp] theorem liftNC_single (f : k →+ R) (g : G → R) (a : G) (b : k) : liftNC f g (single a b) = f b * g a := liftAddHom_apply_single _ _ _ end section Mul variable [Semiring k] [Mul G] [Semiring R] theorem liftNC_mul {g_hom : Type*} [FunLike g_hom G R] [MulHomClass g_hom G R] (f : k →+* R) (g : g_hom) (a b : MonoidAlgebra k G) (h_comm : ∀ {x y}, y ∈ a.support → Commute (f (b x)) (g y)) : liftNC (f : k →+ R) g (a * b) = liftNC (f : k →+ R) g a * liftNC (f : k →+ R) g b := by conv_rhs => rw [← sum_single a, ← sum_single b] simp_rw [mul_def, map_finsuppSum, liftNC_single, Finsupp.sum_mul, Finsupp.mul_sum] refine Finset.sum_congr rfl fun y hy => Finset.sum_congr rfl fun x _hx => ?_ simp [mul_assoc, (h_comm hy).left_comm] end Mul section One variable [NonAssocSemiring R] [Semiring k] [One G] @[simp] theorem liftNC_one {g_hom : Type*} [FunLike g_hom G R] [OneHomClass g_hom G R] (f : k →+* R) (g : g_hom) : liftNC (f : k →+ R) g 1 = 1 := by simp [one_def] end One /-! #### Semiring structure -/ section Semiring variable [Semiring k] [Monoid G] [Semiring R] [Semiring S] [Semiring T] [Monoid M] /-- `liftNC` as a `RingHom`, for when `f x` and `g y` commute -/ def liftNCRingHom (f : k →+* R) (g : G →* R) (h_comm : ∀ x y, Commute (f x) (g y)) : MonoidAlgebra k G →+* R := { liftNC (f : k →+ R) g with map_one' := liftNC_one _ _ map_mul' := fun _a _b => liftNC_mul _ _ _ _ fun {_ _} _ => h_comm _ _ } @[simp] lemma liftNCRingHom_single (f : k →+* R) (g : G →* R) (h_comm) (a : G) (b : k) : liftNCRingHom f g h_comm (single a b) = f b * g a := liftNC_single _ _ _ _ variable (M) in /-- The ring homomorphism of monoid algebras induced by a homomorphism of the base rings. -/ noncomputable def mapRangeRingHom (f : R →+* S) : MonoidAlgebra R M →+* MonoidAlgebra S M := liftNCRingHom (singleOneRingHom.comp f) (of S M) fun x y ↦ by simp [commute_iff_eq] @[simp] lemma mapRangeRingHom_apply (f : R →+* S) (x : MonoidAlgebra R M) (m : M) : mapRangeRingHom M f x m = f (x m) := by classical induction x using induction_linear · simp · simp [*] · simp [mapRangeRingHom, single_apply, apply_ite (f := f)] @[simp] lemma mapRangeRingHom_single (f : R →+* S) (a : M) (b : R) : mapRangeRingHom M f (single a b) = single a (f b) := by classical ext; simp [single_apply, apply_ite f] @[simp] lemma mapRangeRingHom_id : mapRangeRingHom M (.id R) = .id (MonoidAlgebra R M) := by ext <;> simp @[simp] lemma mapRangeRingHom_comp (f : S →+* T) (g : R →+* S) : mapRangeRingHom M (f.comp g) = (mapRangeRingHom M f).comp (mapRangeRingHom M g) := by ext <;> simp end Semiring end MonoidAlgebra /-! ### Additive monoids -/ namespace AddMonoidAlgebra variable {k G} section variable [Semiring k] [NonUnitalNonAssocSemiring R] /-- A non-commutative version of `AddMonoidAlgebra.lift`: given an additive homomorphism `f : k →+ R` and a map `g : Multiplicative G → R`, returns the additive homomorphism from `k[G]` such that `liftNC f g (single a b) = f b * g a`. If `f` is a ring homomorphism and the range of either `f` or `g` is in center of `R`, then the result is a ring homomorphism. If `R` is a `k`-algebra and `f = algebraMap k R`, then the result is an algebra homomorphism called `AddMonoidAlgebra.lift`. -/ def liftNC (f : k →+ R) (g : Multiplicative G → R) : k[G] →+ R := liftAddHom fun x : G => (AddMonoidHom.mulRight (g <| Multiplicative.ofAdd x)).comp f @[simp] theorem liftNC_single (f : k →+ R) (g : Multiplicative G → R) (a : G) (b : k) : liftNC f g (single a b) = f b * g (Multiplicative.ofAdd a) := liftAddHom_apply_single _ _ _ end section Mul variable [Semiring k] [Add G] [Semiring R] theorem liftNC_mul {g_hom : Type*} [FunLike g_hom (Multiplicative G) R] [MulHomClass g_hom (Multiplicative G) R] (f : k →+* R) (g : g_hom) (a b : k[G]) (h_comm : ∀ {x y}, y ∈ a.support → Commute (f (b x)) (g <| Multiplicative.ofAdd y)) : liftNC (f : k →+ R) g (a * b) = liftNC (f : k →+ R) g a * liftNC (f : k →+ R) g b := MonoidAlgebra.liftNC_mul f g _ _ @h_comm end Mul section One variable [Semiring k] [Zero G] [NonAssocSemiring R] @[simp] theorem liftNC_one {g_hom : Type*} [FunLike g_hom (Multiplicative G) R] [OneHomClass g_hom (Multiplicative G) R] (f : k →+* R) (g : g_hom) : liftNC (f : k →+ R) g 1 = 1 := MonoidAlgebra.liftNC_one f g end One /-! #### Semiring structure -/ section Semiring variable [Semiring k] [AddMonoid G] [Semiring R] [Semiring S] [Semiring T] [AddMonoid M] /-- `liftNC` as a `RingHom`, for when `f` and `g` commute -/ def liftNCRingHom (f : k →+* R) (g : Multiplicative G →* R) (h_comm : ∀ x y, Commute (f x) (g y)) : k[G] →+* R := { liftNC (f : k →+ R) g with map_one' := liftNC_one _ _ map_mul' := fun _a _b => liftNC_mul _ _ _ _ fun {_ _} _ => h_comm _ _ } @[simp] lemma liftNCRingHom_single (f : k →+* R) (g : Multiplicative G →* R) (h_comm) (a : G) (b : k) : liftNCRingHom f g h_comm (single a b) = f b * g (.ofAdd a) := liftNC_single _ _ _ _ variable (M) in /-- The ring homomorphism of monoid algebras induced by a homomorphism of the base rings. -/ noncomputable def mapRangeRingHom (f : R →+* S) : R[M] →+* S[M] := liftNCRingHom (singleZeroRingHom.comp f) (of S M) fun x y ↦ by simp [commute_iff_eq] @[simp] lemma mapRangeRingHom_apply (f : R →+* S) (x : R[M]) (m : M) : mapRangeRingHom M f x m = f (x m) := by classical induction x using induction_linear · simp · simp [*] · simp [mapRangeRingHom, single_apply, apply_ite (f := f)] @[simp] lemma mapRangeRingHom_single (f : R →+* S) (a : M) (b : R) : mapRangeRingHom M f (single a b) = single a (f b) := by classical ext; simp [single_apply, apply_ite f] @[simp] lemma mapRangeRingHom_id : mapRangeRingHom M (.id R) = .id (R[M]) := by ext <;> simp @[simp] lemma mapRangeRingHom_comp (f : S →+* T) (g : R →+* S) : mapRangeRingHom M (f.comp g) = (mapRangeRingHom M f).comp (mapRangeRingHom M g) := by ext <;> simp -- `MonoidAlgebra.of` doesn't translate with `to_additive`, so instead -- we have to tag these declarations with `to_additive existing` set_option linter.existingAttributeWarning false in attribute [to_additive existing] MonoidAlgebra.mapRangeRingHom MonoidAlgebra.mapRangeRingHom_apply MonoidAlgebra.mapRangeRingHom_single MonoidAlgebra.mapRangeRingHom_id MonoidAlgebra.mapRangeRingHom_comp end Semiring end AddMonoidAlgebra
.lake/packages/mathlib/Mathlib/Algebra/Regular/Pow.lean
import Mathlib.Algebra.BigOperators.Group.Finset.Defs import Mathlib.Algebra.Regular.Basic /-! # Product of regular elements ## TODO Move to `Mathlib/Algebra/BigOperators/Group/Finset/Basic.lean`? -/ variable {R : Type*} {a b : R} section CommMonoid variable {ι R : Type*} [CommMonoid R] {s : Finset ι} {f : ι → R} lemma IsLeftRegular.prod (h : ∀ i ∈ s, IsLeftRegular (f i)) : IsLeftRegular (∏ i ∈ s, f i) := s.prod_induction _ _ (@IsLeftRegular.mul R _) isRegular_one.left h lemma IsRightRegular.prod (h : ∀ i ∈ s, IsRightRegular (f i)) : IsRightRegular (∏ i ∈ s, f i) := s.prod_induction _ _ (@IsRightRegular.mul R _) isRegular_one.right h lemma IsRegular.prod (h : ∀ i ∈ s, IsRegular (f i)) : IsRegular (∏ i ∈ s, f i) := ⟨IsLeftRegular.prod fun a ha ↦ (h a ha).left, IsRightRegular.prod fun a ha ↦ (h a ha).right⟩ end CommMonoid
.lake/packages/mathlib/Mathlib/Algebra/Regular/SMul.lean
import Mathlib.Algebra.Group.Units.Defs import Mathlib.Algebra.GroupWithZero.Action.Defs import Mathlib.Tactic.Convert import Mathlib.Tactic.Push /-! # Action of regular elements on a module We introduce `M`-regular elements, in the context of an `R`-module `M`. The corresponding predicate is called `IsSMulRegular`. There are very limited typeclass assumptions on `R` and `M`, but the "mathematical" case of interest is a commutative ring `R` acting on a module `M`. Since the properties are "multiplicative", there is no actual requirement of having an addition, but there is a zero in both `R` and `M`. SMultiplications involving `0` are, of course, all trivial. The defining property is that an element `a ∈ R` is `M`-regular if the smultiplication map `M → M`, defined by `m ↦ a • m`, is injective. This property is the direct generalization to modules of the property `IsLeftRegular` defined in `Algebra/Regular`. Lemma `isLeftRegular_iff` shows that indeed the two notions coincide. -/ variable {R S : Type*} (M : Type*) {a b : R} {s : S} /-- An `M`-regular element is an element `c` such that multiplication on the left by `c` is an injective map `M → M`. -/ def IsSMulRegular [SMul R M] (c : R) := Function.Injective ((c • ·) : M → M) theorem IsLeftRegular.isSMulRegular [Mul R] {c : R} (h : IsLeftRegular c) : IsSMulRegular R c := h /-- Left-regular multiplication on `R` is equivalent to `R`-regularity of `R` itself. -/ theorem isLeftRegular_iff [Mul R] {a : R} : IsLeftRegular a ↔ IsSMulRegular R a := Iff.rfl theorem IsRightRegular.isSMulRegular [Mul R] {c : R} (h : IsRightRegular c) : IsSMulRegular R (MulOpposite.op c) := h /-- Right-regular multiplication on `R` is equivalent to `Rᵐᵒᵖ`-regularity of `R` itself. -/ theorem isRightRegular_iff [Mul R] {a : R} : IsRightRegular a ↔ IsSMulRegular R (MulOpposite.op a) := Iff.rfl namespace IsSMulRegular variable {M} section SMul variable [SMul R M] [SMul R S] [SMul S M] [IsScalarTower R S M] /-- The product of `M`-regular elements is `M`-regular. -/ theorem smul (ra : IsSMulRegular M a) (rs : IsSMulRegular M s) : IsSMulRegular M (a • s) := fun _ _ ab => rs (ra ((smul_assoc _ _ _).symm.trans (ab.trans (smul_assoc _ _ _)))) /-- If an element `b` becomes `M`-regular after multiplying it on the left by an `M`-regular element, then `b` is `M`-regular. -/ theorem of_smul (a : R) (ab : IsSMulRegular M (a • s)) : IsSMulRegular M s := @Function.Injective.of_comp _ _ _ (fun m : M => a • m) _ fun c d cd => by dsimp only [Function.comp_def] at cd rw [← smul_assoc, ← smul_assoc] at cd exact ab cd /-- An element is `M`-regular if and only if multiplying it on the left by an `M`-regular element is `M`-regular. -/ @[simp] theorem smul_iff (b : S) (ha : IsSMulRegular M a) : IsSMulRegular M (a • b) ↔ IsSMulRegular M b := ⟨of_smul _, ha.smul⟩ theorem isLeftRegular [Mul R] {a : R} (h : IsSMulRegular R a) : IsLeftRegular a := h theorem isRightRegular [Mul R] {a : R} (h : IsSMulRegular R (MulOpposite.op a)) : IsRightRegular a := h theorem mul [Mul R] [IsScalarTower R R M] (ra : IsSMulRegular M a) (rb : IsSMulRegular M b) : IsSMulRegular M (a * b) := ra.smul rb theorem of_mul [Mul R] [IsScalarTower R R M] (ab : IsSMulRegular M (a * b)) : IsSMulRegular M b := by rw [← smul_eq_mul] at ab exact ab.of_smul _ @[simp] theorem mul_iff_right [Mul R] [IsScalarTower R R M] (ha : IsSMulRegular M a) : IsSMulRegular M (a * b) ↔ IsSMulRegular M b := ⟨of_mul, ha.mul⟩ /-- Two elements `a` and `b` are `M`-regular if and only if both products `a * b` and `b * a` are `M`-regular. -/ theorem mul_and_mul_iff [Mul R] [IsScalarTower R R M] : IsSMulRegular M (a * b) ∧ IsSMulRegular M (b * a) ↔ IsSMulRegular M a ∧ IsSMulRegular M b := by refine ⟨?_, ?_⟩ · rintro ⟨ab, ba⟩ exact ⟨ba.of_mul, ab.of_mul⟩ · rintro ⟨ha, hb⟩ exact ⟨ha.mul hb, hb.mul ha⟩ end SMul section Monoid variable [Monoid R] [MulAction R M] variable (M) /-- One is always `M`-regular. -/ @[simp] theorem one : IsSMulRegular M (1 : R) := fun a b ab => by dsimp only [Function.comp_def] at ab rw [one_smul, one_smul] at ab assumption variable {M} /-- An element of `R` admitting a left inverse is `M`-regular. -/ theorem of_mul_eq_one (h : a * b = 1) : IsSMulRegular M b := of_mul (a := a) (by rw [h]; exact one M) /-- Any power of an `M`-regular element is `M`-regular. -/ theorem pow (n : ℕ) (ra : IsSMulRegular M a) : IsSMulRegular M (a ^ n) := by induction n with | zero => rw [pow_zero]; simp only [one] | succ n hn => rw [pow_succ'] exact (ra.smul_iff (a ^ n)).mpr hn /-- An element `a` is `M`-regular if and only if a positive power of `a` is `M`-regular. -/ theorem pow_iff {n : ℕ} (n0 : 0 < n) : IsSMulRegular M (a ^ n) ↔ IsSMulRegular M a := by refine ⟨?_, pow n⟩ rw [← Nat.succ_pred_eq_of_pos n0, pow_succ, ← smul_eq_mul] exact of_smul _ end Monoid section MonoidSMul variable [Monoid S] [SMul R M] [SMul R S] [MulAction S M] [IsScalarTower R S M] /-- An element of `S` admitting a left inverse in `R` is `M`-regular. -/ theorem of_smul_eq_one (h : a • s = 1) : IsSMulRegular M s := of_smul a (by rw [h] exact one M) end MonoidSMul section MonoidWithZero variable [MonoidWithZero R] [Zero M] [MulActionWithZero R M] /-- The element `0` is `M`-regular if and only if `M` is trivial. -/ protected theorem subsingleton (h : IsSMulRegular M (0 : R)) : Subsingleton M := ⟨fun a b => h (by dsimp only [Function.comp_def]; repeat' rw [MulActionWithZero.zero_smul])⟩ /-- The element `0` is `M`-regular if and only if `M` is trivial. -/ theorem zero_iff_subsingleton : IsSMulRegular M (0 : R) ↔ Subsingleton M := ⟨fun h => h.subsingleton, fun H a b _ => @Subsingleton.elim _ H a b⟩ /-- The `0` element is not `M`-regular, on a non-trivial module. -/ theorem not_zero_iff : ¬IsSMulRegular M (0 : R) ↔ Nontrivial M := by rw [nontrivial_iff, not_iff_comm, zero_iff_subsingleton, subsingleton_iff] push_neg exact Iff.rfl /-- The element `0` is `M`-regular when `M` is trivial. -/ theorem zero [sM : Subsingleton M] : IsSMulRegular M (0 : R) := zero_iff_subsingleton.mpr sM /-- The `0` element is not `M`-regular, on a non-trivial module. -/ theorem not_zero [nM : Nontrivial M] : ¬IsSMulRegular M (0 : R) := not_zero_iff.mpr nM end MonoidWithZero section CommSemigroup variable [CommSemigroup R] [SMul R M] [IsScalarTower R R M] /-- A product is `M`-regular if and only if the factors are. -/ theorem mul_iff : IsSMulRegular M (a * b) ↔ IsSMulRegular M a ∧ IsSMulRegular M b := by rw [← mul_and_mul_iff] exact ⟨fun ab => ⟨ab, by rwa [mul_comm]⟩, fun rab => rab.1⟩ end CommSemigroup end IsSMulRegular section Group variable {G : Type*} [Group G] /-- An element of a group acting on a Type is regular. This relies on the availability of the inverse given by groups, since there is no `LeftCancelSMul` typeclass. -/ theorem isSMulRegular_of_group [MulAction G R] (g : G) : IsSMulRegular R g := by intro x y h convert congr_arg (g⁻¹ • ·) h using 1 <;> simp [← smul_assoc] end Group section Units variable [Monoid R] [MulAction R M] /-- Any element in `Rˣ` is `M`-regular. -/ theorem Units.isSMulRegular (a : Rˣ) : IsSMulRegular M (a : R) := IsSMulRegular.of_mul_eq_one a.inv_val /-- A unit is `M`-regular. -/ theorem IsUnit.isSMulRegular (ua : IsUnit a) : IsSMulRegular M a := by rcases ua with ⟨a, rfl⟩ exact a.isSMulRegular M end Units section SMulZeroClass variable {M} protected lemma IsSMulRegular.right_eq_zero_of_smul [Zero M] [SMulZeroClass R M] {r : R} {x : M} (h1 : IsSMulRegular M r) (h2 : r • x = 0) : x = 0 := h1 (h2.trans (smul_zero r).symm) end SMulZeroClass variable {M} in lemma isSMulRegular_iff_right_eq_zero_of_smul [AddGroup M] [DistribSMul R M] {r : R} : IsSMulRegular M r ↔ ∀ m : M, r • m = 0 → m = 0 where mp h _ := h.right_eq_zero_of_smul mpr h m₁ m₂ eq := sub_eq_zero.mp <| h _ <| by simp_rw [smul_sub, eq, sub_self] alias ⟨_, IsSMulRegular.of_right_eq_zero_of_smul⟩ := isSMulRegular_iff_right_eq_zero_of_smul @[deprecated (since := "2025-08-04")] alias IsSMulRegular.eq_zero_of_smul_eq_zero := IsSMulRegular.right_eq_zero_of_smul @[deprecated (since := "2025-08-04")] alias isSMulRegular_iff_smul_eq_zero_imp_eq_zero := isSMulRegular_iff_right_eq_zero_of_smul @[deprecated (since := "2025-08-04")] alias isSMulRegular_of_smul_eq_zero_imp_eq_zero := IsSMulRegular.of_right_eq_zero_of_smul lemma Equiv.isSMulRegular_congr {R S M M'} [SMul R M] [SMul S M'] {e : M ≃ M'} {r : R} {s : S} (h : ∀ x, e (r • x) = s • e x) : IsSMulRegular M r ↔ IsSMulRegular M' s := (e.comp_injective _).symm.trans <| (iff_of_eq <| congrArg _ <| funext h).trans <| e.injective_comp _
.lake/packages/mathlib/Mathlib/Algebra/Regular/Opposite.lean
import Mathlib.Algebra.Opposites /-! # Results about `IsRegular` and `MulOpposite` -/ variable {R} [Mul R] open MulOpposite @[to_additive (attr := simp)] theorem isLeftRegular_op {a : R} : IsLeftRegular (op a) ↔ IsRightRegular a := opEquiv.comp_injective _ |>.trans <| opEquiv.injective_comp _ |>.symm @[to_additive (attr := simp)] theorem isRightRegular_op {a : R} : IsRightRegular (op a) ↔ IsLeftRegular a := opEquiv.comp_injective _ |>.trans <| opEquiv.injective_comp _ |>.symm @[to_additive (attr := simp)] theorem isRegular_op {a : R} : IsRegular (op a) ↔ IsRegular a := by simp [isRegular_iff, and_comm] @[to_additive] protected alias ⟨_, IsLeftRegular.op⟩ := isLeftRegular_op @[to_additive] protected alias ⟨_, IsRightRegular.op⟩ := isRightRegular_op @[to_additive] protected alias ⟨_, IsRegular.op⟩ := isRegular_op @[to_additive (attr := simp)] theorem isLeftRegular_unop {a : Rᵐᵒᵖ} : IsLeftRegular a.unop ↔ IsRightRegular a := isRightRegular_op.symm @[to_additive (attr := simp)] theorem isRightRegular_unop {a : Rᵐᵒᵖ} : IsRightRegular a.unop ↔ IsLeftRegular a := isLeftRegular_op.symm @[to_additive (attr := simp)] theorem isRegular_unop {a : Rᵐᵒᵖ} : IsRegular a.unop ↔ IsRegular a := isRegular_op.symm @[to_additive] protected alias ⟨_, IsLeftRegular.unop⟩ := isLeftRegular_unop @[to_additive] protected alias ⟨_, IsRightRegular.unop⟩ := isRightRegular_unop @[to_additive] protected alias ⟨_, IsRegular.unop⟩ := isRegular_unop
.lake/packages/mathlib/Mathlib/Algebra/Regular/Prod.lean
import Mathlib.Algebra.Notation.Prod import Mathlib.Algebra.Regular.SMul /-! # Results about `IsRegular` and `Prod` -/ variable {α R S : Type*} section variable [Mul R] [Mul S] @[to_additive (attr := simp)] theorem Prod.isLeftRegular_mk {a : R} {b : S} : IsLeftRegular (a, b) ↔ IsLeftRegular a ∧ IsLeftRegular b := have : Nonempty R := ⟨a⟩; have : Nonempty S := ⟨b⟩; Prod.map_injective @[to_additive (attr := simp)] theorem Prod.isRightRegular_mk {a : R} {b : S} : IsRightRegular (a, b) ↔ IsRightRegular a ∧ IsRightRegular b := have : Nonempty R := ⟨a⟩; have : Nonempty S := ⟨b⟩; Iff.symm <| Prod.map_injective |>.symm @[to_additive (attr := simp)] theorem Prod.isRegular_mk {a : R} {b : S} : IsRegular (a, b) ↔ IsRegular a ∧ IsRegular b := by simp [isRegular_iff, and_and_and_comm] @[to_additive] theorem IsLeftRegular.prodMk {a : R} {b : S} (ha : IsLeftRegular a) (hb : IsLeftRegular b) : IsLeftRegular (a, b) := Prod.isLeftRegular_mk.2 ⟨ha, hb⟩ @[to_additive] theorem IsRightRegular.prodMk {a : R} {b : S} (ha : IsRightRegular a) (hb : IsRightRegular b) : IsRightRegular (a, b) := Prod.isRightRegular_mk.2 ⟨ha, hb⟩ @[to_additive] theorem IsRegular.prodMk {a : R} {b : S} (ha : IsRegular a) (hb : IsRegular b) : IsRegular (a, b) := Prod.isRegular_mk.2 ⟨ha, hb⟩ end @[simp] theorem Prod.isSMulRegular_iff [SMul α R] [SMul α S] {r : α} [Nonempty R] [Nonempty S] : IsSMulRegular (R × S) r ↔ IsSMulRegular R r ∧ IsSMulRegular S r := Prod.map_injective
.lake/packages/mathlib/Mathlib/Algebra/Regular/Pi.lean
import Mathlib.Algebra.Regular.SMul /-! # Results about `IsRegular` and pi types -/ variable {ι α : Type*} {R : ι → Type*} namespace Pi section variable [∀ i, Mul (R i)] @[to_additive (attr := simp)] theorem isLeftRegular_iff {a : ∀ i, R i} : IsLeftRegular a ↔ ∀ i, IsLeftRegular (a i) := have (i : _) : Nonempty (R i) := ⟨a i⟩; Pi.map_injective @[to_additive (attr := simp)] theorem isRightRegular_iff {a : ∀ i, R i} : IsRightRegular a ↔ ∀ i, IsRightRegular (a i) := have (i : _) : Nonempty (R i) := ⟨a i⟩; .symm <| Pi.map_injective.symm @[to_additive (attr := simp)] theorem isRegular_iff {a : ∀ i, R i} : IsRegular a ↔ ∀ i, IsRegular (a i) := by simp [_root_.isRegular_iff, forall_and] end @[simp] theorem isSMulRegular_iff [∀ i, SMul α (R i)] {r : α} [∀ i, Nonempty (R i)] : IsSMulRegular (∀ i, R i) r ↔ ∀ i, IsSMulRegular (R i) r := Pi.map_injective end Pi
.lake/packages/mathlib/Mathlib/Algebra/Regular/Basic.lean
import Mathlib.Algebra.Group.Basic import Mathlib.Algebra.Group.Commute.Defs import Mathlib.Algebra.Group.Units.Defs import Mathlib.Algebra.Regular.Defs /-! # Regular elements By definition, a regular element in a commutative ring is a non-zero divisor. Lemma `isRegular_of_ne_zero` implies that every non-zero element of an integral domain is regular. Since it assumes that the ring is a `CancelMonoidWithZero` it applies also, for instance, to `ℕ`. The lemmas in Section `MulZeroClass` show that the `0` element is (left/right-)regular if and only if the `MulZeroClass` is trivial. This is useful when figuring out stopping conditions for regular sequences: if `0` is ever an element of a regular sequence, then we can extend the sequence by adding one further `0`. The final goal is to develop part of the API to prove, eventually, results about non-zero-divisors. -/ variable {R : Type*} section Mul variable [Mul R] @[to_additive] theorem IsLeftRegular.right_of_commute {a : R} (ca : ∀ b, Commute a b) (h : IsLeftRegular a) : IsRightRegular a := fun x y xy => h <| (ca x).trans <| xy.trans <| (ca y).symm @[to_additive] theorem IsRightRegular.left_of_commute {a : R} (ca : ∀ b, Commute a b) (h : IsRightRegular a) : IsLeftRegular a := by simp only [@Commute.symm_iff R _ a] at ca exact fun x y xy => h <| (ca x).trans <| xy.trans <| (ca y).symm @[to_additive] theorem Commute.isRightRegular_iff {a : R} (ca : ∀ b, Commute a b) : IsRightRegular a ↔ IsLeftRegular a := ⟨IsRightRegular.left_of_commute ca, IsLeftRegular.right_of_commute ca⟩ @[to_additive] theorem Commute.isRegular_iff {a : R} (ca : ∀ b, Commute a b) : IsRegular a ↔ IsLeftRegular a := ⟨fun h => h.left, fun h => ⟨h, h.right_of_commute ca⟩⟩ end Mul section Semigroup variable [Semigroup R] {a b : R} /-- In a semigroup, the product of left-regular elements is left-regular. -/ @[to_additive /-- In an additive semigroup, the sum of add-left-regular elements is add-left.regular. -/] theorem IsLeftRegular.mul (lra : IsLeftRegular a) (lrb : IsLeftRegular b) : IsLeftRegular (a * b) := show Function.Injective (((a * b) * ·)) from comp_mul_left a b ▸ lra.comp lrb /-- In a semigroup, the product of right-regular elements is right-regular. -/ @[to_additive /-- In an additive semigroup, the sum of add-right-regular elements is add-right-regular. -/] theorem IsRightRegular.mul (rra : IsRightRegular a) (rrb : IsRightRegular b) : IsRightRegular (a * b) := show Function.Injective (· * (a * b)) from comp_mul_right b a ▸ rrb.comp rra /-- In a semigroup, the product of regular elements is regular. -/ @[to_additive /-- In an additive semigroup, the sum of add-regular elements is add-regular. -/] theorem IsRegular.mul (rra : IsRegular a) (rrb : IsRegular b) : IsRegular (a * b) := ⟨rra.left.mul rrb.left, rra.right.mul rrb.right⟩ /-- If an element `b` becomes left-regular after multiplying it on the left by a left-regular element, then `b` is left-regular. -/ @[to_additive /-- If an element `b` becomes add-left-regular after adding to it on the left an add-left-regular element, then `b` is add-left-regular. -/] theorem IsLeftRegular.of_mul (ab : IsLeftRegular (a * b)) : IsLeftRegular b := Function.Injective.of_comp (f := (a * ·)) (by rwa [comp_mul_left a b]) /-- An element is left-regular if and only if multiplying it on the left by a left-regular element is left-regular. -/ @[to_additive (attr := simp) /-- An element is add-left-regular if and only if adding to it on the left an add-left-regular element is add-left-regular. -/] theorem mul_isLeftRegular_iff (b : R) (ha : IsLeftRegular a) : IsLeftRegular (a * b) ↔ IsLeftRegular b := ⟨fun ab => IsLeftRegular.of_mul ab, fun ab => IsLeftRegular.mul ha ab⟩ /-- If an element `b` becomes right-regular after multiplying it on the right by a right-regular element, then `b` is right-regular. -/ @[to_additive /-- If an element `b` becomes add-right-regular after adding to it on the right an add-right-regular element, then `b` is add-right-regular. -/] theorem IsRightRegular.of_mul (ab : IsRightRegular (b * a)) : IsRightRegular b := by refine fun x y xy => ab (?_ : x * (b * a) = y * (b * a)) rw [← mul_assoc, ← mul_assoc] exact congr_arg (· * a) xy /-- An element is right-regular if and only if multiplying it on the right with a right-regular element is right-regular. -/ @[to_additive (attr := simp) /-- An element is add-right-regular if and only if adding it on the right to an add-right-regular element is add-right-regular. -/] theorem mul_isRightRegular_iff (b : R) (ha : IsRightRegular a) : IsRightRegular (b * a) ↔ IsRightRegular b := ⟨fun ab => IsRightRegular.of_mul ab, fun ab => IsRightRegular.mul ab ha⟩ /-- Two elements `a` and `b` are regular if and only if both products `a * b` and `b * a` are regular. -/ @[to_additive /-- Two elements `a` and `b` are add-regular if and only if both sums `a + b` and `b + a` are add-regular. -/] theorem isRegular_mul_and_mul_iff : IsRegular (a * b) ∧ IsRegular (b * a) ↔ IsRegular a ∧ IsRegular b := by refine ⟨?_, ?_⟩ · rintro ⟨ab, ba⟩ exact ⟨⟨IsLeftRegular.of_mul ba.left, IsRightRegular.of_mul ab.right⟩, ⟨IsLeftRegular.of_mul ab.left, IsRightRegular.of_mul ba.right⟩⟩ · rintro ⟨ha, hb⟩ exact ⟨ha.mul hb, hb.mul ha⟩ /-- The "most used" implication of `mul_and_mul_iff`, with split hypotheses, instead of `∧`. -/ @[to_additive /-- The "most used" implication of `add_and_add_iff`, with split hypotheses, instead of `∧`. -/] theorem IsRegular.and_of_mul_of_mul (ab : IsRegular (a * b)) (ba : IsRegular (b * a)) : IsRegular a ∧ IsRegular b := isRegular_mul_and_mul_iff.mp ⟨ab, ba⟩ end Semigroup section MulOneClass variable [MulOneClass R] /-- If multiplying by `1` on either side is the identity, `1` is regular. -/ @[to_additive /-- If adding `0` on either side is the identity, `0` is regular. -/] theorem isRegular_one : IsRegular (1 : R) := ⟨fun a b ab => (one_mul a).symm.trans (Eq.trans ab (one_mul b)), fun a b ab => (mul_one a).symm.trans (Eq.trans ab (mul_one b))⟩ end MulOneClass section CommSemigroup variable [CommSemigroup R] {a b : R} /-- A product is regular if and only if the factors are. -/ @[to_additive /-- A sum is add-regular if and only if the summands are. -/] theorem isRegular_mul_iff : IsRegular (a * b) ↔ IsRegular a ∧ IsRegular b := by refine Iff.trans ?_ isRegular_mul_and_mul_iff exact ⟨fun ab => ⟨ab, by rwa [mul_comm]⟩, fun rab => rab.1⟩ end CommSemigroup section Monoid variable [Monoid R] {a b : R} {n : ℕ} /-- An element admitting a left inverse is left-regular. -/ @[to_additive /-- An element admitting a left additive opposite is add-left-regular. -/] theorem isLeftRegular_of_mul_eq_one (h : b * a = 1) : IsLeftRegular a := IsLeftRegular.of_mul (a := b) (by rw [h]; exact isRegular_one.left) /-- An element admitting a right inverse is right-regular. -/ @[to_additive /-- An element admitting a right additive opposite is add-right-regular. -/] theorem isRightRegular_of_mul_eq_one (h : a * b = 1) : IsRightRegular a := IsRightRegular.of_mul (a := b) (by rw [h]; exact isRegular_one.right) /-- If `R` is a monoid, an element in `Rˣ` is regular. -/ @[to_additive /-- If `R` is an additive monoid, an element in `add_units R` is add-regular. -/] theorem Units.isRegular (a : Rˣ) : IsRegular (a : R) := ⟨isLeftRegular_of_mul_eq_one a.inv_mul, isRightRegular_of_mul_eq_one a.mul_inv⟩ /-- A unit in a monoid is regular. -/ @[to_additive /-- An additive unit in an additive monoid is add-regular. -/] theorem IsUnit.isRegular (ua : IsUnit a) : IsRegular a := by rcases ua with ⟨a, rfl⟩ exact Units.isRegular a /-- Any power of a left-regular element is left-regular. -/ @[to_additive] lemma IsLeftRegular.pow (n : ℕ) (rla : IsLeftRegular a) : IsLeftRegular (a ^ n) := by simp only [IsLeftRegular, ← mul_left_iterate, rla.iterate n] /-- Any power of a right-regular element is right-regular. -/ @[to_additive] lemma IsRightRegular.pow (n : ℕ) (rra : IsRightRegular a) : IsRightRegular (a ^ n) := by rw [IsRightRegular, ← mul_right_iterate] exact rra.iterate n /-- Any power of a regular element is regular. -/ @[to_additive] lemma IsRegular.pow (n : ℕ) (ra : IsRegular a) : IsRegular (a ^ n) := ⟨IsLeftRegular.pow n ra.left, IsRightRegular.pow n ra.right⟩ /-- An element `a` is left-regular if and only if a positive power of `a` is left-regular. -/ @[to_additive] lemma IsLeftRegular.pow_iff (n0 : 0 < n) : IsLeftRegular (a ^ n) ↔ IsLeftRegular a where mp := by rw [← Nat.succ_pred_eq_of_pos n0, pow_succ]; exact .of_mul mpr := .pow n /-- An element `a` is right-regular if and only if a positive power of `a` is right-regular. -/ @[to_additive] lemma IsRightRegular.pow_iff (n0 : 0 < n) : IsRightRegular (a ^ n) ↔ IsRightRegular a where mp := by rw [← Nat.succ_pred_eq_of_pos n0, pow_succ']; exact .of_mul mpr := .pow n /-- An element `a` is regular if and only if a positive power of `a` is regular. -/ @[to_additive] lemma IsRegular.pow_iff {n : ℕ} (n0 : 0 < n) : IsRegular (a ^ n) ↔ IsRegular a where mp h := ⟨(IsLeftRegular.pow_iff n0).mp h.left, (IsRightRegular.pow_iff n0).mp h.right⟩ mpr h := ⟨.pow n h.left, .pow n h.right⟩ @[to_additive (attr := simp)] lemma IsLeftRegular.mul_left_eq_self_iff (ha : IsLeftRegular a) : a * b = a ↔ b = 1 := ⟨fun h ↦ by rwa [← ha.eq_iff, mul_one], fun h ↦ by rw [h, mul_one]⟩ @[to_additive (attr := simp)] lemma IsRightRegular.mul_right_eq_self_iff (ha : IsRightRegular a) : b * a = a ↔ b = 1 := ⟨fun h ↦ by rwa [← ha.eq_iff, one_mul], fun h ↦ by rw [h, one_mul]⟩ end Monoid
.lake/packages/mathlib/Mathlib/Algebra/Regular/ULift.lean
import Mathlib.Algebra.Group.ULift import Mathlib.Algebra.Regular.SMul /-! # Results about `IsRegular` and `ULift` -/ universe u v variable {α} {R : Type v} namespace ULift section variable [Mul R] @[to_additive (attr := simp)] theorem isLeftRegular_up {a : R} : IsLeftRegular (ULift.up.{u} a) ↔ IsLeftRegular a := Equiv.ulift.symm.comp_injective _ |>.trans <| Equiv.ulift.symm.injective_comp _ |>.symm @[to_additive (attr := simp)] theorem isRightRegular_up {a : R} : IsRightRegular (ULift.up.{u} a) ↔ IsRightRegular a := Equiv.ulift.symm.comp_injective _ |>.trans <| Equiv.ulift.symm.injective_comp _ |>.symm @[to_additive (attr := simp)] theorem isRegular_up {a : R} : IsRegular (ULift.up.{u} a) ↔ IsRegular a := by simp [isRegular_iff] @[to_additive (attr := simp)] theorem isLeftRegular_down {a : ULift.{u} R} : IsLeftRegular a.down ↔ IsLeftRegular a := isLeftRegular_up.symm @[to_additive (attr := simp)] theorem isRightRegular_down {a : ULift.{u} R} : IsRightRegular a.down ↔ IsRightRegular a := isRightRegular_up.symm @[to_additive (attr := simp)] theorem isRegular_down {a : ULift.{u} R} : IsRegular a.down ↔ IsRegular a := isRegular_up.symm end @[simp] theorem isSMulRegular_iff [SMul α R] {r : α} : IsSMulRegular (ULift R) r ↔ IsSMulRegular R r := Equiv.ulift.symm.comp_injective _ |>.trans <| Equiv.ulift.symm.injective_comp _ |>.symm end ULift
.lake/packages/mathlib/Mathlib/Algebra/Regular/Defs.lean
import Mathlib.Algebra.Notation.Defs /-! # Regular elements We introduce left-regular, right-regular and regular elements, along with their `to_additive` analogues add-left-regular, add-right-regular and add-regular elements. For monoids where _every_ element is regular, see `IsCancelMul` and nearby typeclasses. -/ variable {R : Type*} [Mul R] /-- A left-regular element is an element `c` such that multiplication on the left by `c` is injective. -/ @[to_additive /-- An add-left-regular element is an element `c` such that addition on the left by `c` is injective. -/] def IsLeftRegular (c : R) := (c * ·).Injective /-- A right-regular element is an element `c` such that multiplication on the right by `c` is injective. -/ @[to_additive /-- An add-right-regular element is an element `c` such that addition on the right by `c` is injective. -/] def IsRightRegular (c : R) := (· * c).Injective /-- An add-regular element is an element `c` such that addition by `c` both on the left and on the right is injective. -/ structure IsAddRegular {R : Type*} [Add R] (c : R) : Prop where /-- An add-regular element `c` is left-regular -/ left : IsAddLeftRegular c /-- An add-regular element `c` is right-regular -/ right : IsAddRightRegular c /-- A regular element is an element `c` such that multiplication by `c` both on the left and on the right is injective. -/ structure IsRegular (c : R) : Prop where /-- A regular element `c` is left-regular -/ left : IsLeftRegular c /-- A regular element `c` is right-regular -/ right : IsRightRegular c attribute [simp] IsRegular.left IsRegular.right attribute [to_additive] IsRegular @[to_additive] theorem isRegular_iff {c : R} : IsRegular c ↔ IsLeftRegular c ∧ IsRightRegular c := ⟨fun ⟨h1, h2⟩ => ⟨h1, h2⟩, fun ⟨h1, h2⟩ => ⟨h1, h2⟩⟩
.lake/packages/mathlib/Mathlib/Algebra/Central/Matrix.lean
import Mathlib.Algebra.Central.Defs import Mathlib.Data.Matrix.Basis /-! # The matrix algebra is a central algebra -/ namespace Matrix variable {n R A : Type*} [CommSemiring R] [Semiring A] [Algebra R A] [Fintype n] [DecidableEq n] theorem subalgebraCenter_eq_scalarAlgHom_map : Subalgebra.center R (Matrix n n A) = (Subalgebra.center R A).map (scalarAlgHom n R) := SetLike.coe_injective center_eq_scalar_image end Matrix namespace Algebra.IsCentral variable (K D : Type*) [CommSemiring K] [Semiring D] [Algebra K D] [IsCentral K D] open Matrix in instance matrix (ι : Type*) [Fintype ι] [DecidableEq ι] : Algebra.IsCentral K (Matrix ι ι D) where out := subalgebraCenter_eq_scalarAlgHom_map.trans_le <| Subalgebra.map_mono out |>.trans_eq <| map_bot _ end Algebra.IsCentral
.lake/packages/mathlib/Mathlib/Algebra/Central/TensorProduct.lean
import Mathlib.Algebra.Central.Basic import Mathlib.RingTheory.Flat.Basic import Mathlib.LinearAlgebra.Basis.VectorSpace /-! # Lemmas about tensor products of central algebras In this file we prove for algebras `B` and `C` over a field `K` that if `B ⊗[K] C` is a central algebra and `B, C` nontrivial, then both `B` and `C` are central algebras. ## Main Results - `Algebra.IsCentral.left_of_tensor_of_field`: If `B` `C` are `K`-algebras where `K` is a field, `C` is nontrivial and `B ⊗[K] C` is a central algebra over `K`, then `B` is a central algebra over `K`. - `Algebra.IsCentral.right_of_tensor_of_field`: If `B` `C` are `K`-algebras where `K` is a field, `B` is nontrivial and `B ⊗[K] C` is a central algebra over `K`, then `C` is a central algebra over `K`. ## Tags Central Algebras, Central Simple Algebras, Noncommutative Algebra -/ universe u v open TensorProduct variable (K B C : Type*) [CommSemiring K] [Semiring B] [Semiring C] [Algebra K B] [Algebra K C] lemma Algebra.TensorProduct.includeLeft_map_center_le : (Subalgebra.center K B).map includeLeft ≤ Subalgebra.center K (B ⊗[K] C) := by intro x hx simp only [Subalgebra.mem_map, Subalgebra.mem_center_iff] at hx ⊢ obtain ⟨b, hb0, rfl⟩ := hx intro bc induction bc using TensorProduct.induction_on with | zero => simp | tmul b' c => simp [hb0] | add _ _ _ _ => simp_all [add_mul, mul_add] lemma Algebra.TensorProduct.includeRight_map_center_le : (Subalgebra.center K C).map includeRight ≤ Subalgebra.center K (B ⊗[K] C) := fun x hx ↦ by simp only [Subalgebra.mem_map, Subalgebra.mem_center_iff] at hx ⊢ obtain ⟨c, hc0, rfl⟩ := hx intro bc induction bc using TensorProduct.induction_on with | zero => simp | tmul b c' => simp [hc0] | add _ _ _ _ => simp_all [add_mul, mul_add] namespace Algebra.IsCentral open Algebra.TensorProduct in lemma left_of_tensor (inj : Function.Injective (algebraMap K C)) [Module.Flat K B] [hbc : Algebra.IsCentral K (B ⊗[K] C)] : IsCentral K B where out := (Subalgebra.map_le.mp ((includeLeft_map_center_le K B C).trans hbc.1)).trans fun _ ⟨k, hk⟩ ↦ ⟨k, includeLeft_injective (S := K) inj hk⟩ lemma right_of_tensor (inj : Function.Injective (algebraMap K B)) [Module.Flat K C] [Algebra.IsCentral K (B ⊗[K] C)] : IsCentral K C := have : IsCentral K (C ⊗[K] B) := IsCentral.of_algEquiv K _ _ <| Algebra.TensorProduct.comm _ _ _ left_of_tensor K C B inj /-- Let `B` and `C` be two algebras over a field `K`, if `B ⊗[K] C` is central and `C` is non-trivial, then `B` is central. -/ lemma left_of_tensor_of_field (K B C : Type*) [Field K] [Ring B] [Ring C] [Nontrivial C] [Algebra K B] [Algebra K C] [IsCentral K (B ⊗[K] C)] : IsCentral K B := left_of_tensor K B C <| FaithfulSMul.algebraMap_injective K C /-- Let `B` and `C` be two algebras over a field `K`, if `B ⊗[K] C` is central and `B` is non-trivial, then `C` is central. -/ lemma right_of_tensor_of_field (K B C : Type*) [Field K] [Ring B] [Ring C] [Nontrivial B] [Algebra K B] [Algebra K C] [IsCentral K (B ⊗[K] C)] : IsCentral K C := right_of_tensor K B C <| FaithfulSMul.algebraMap_injective K B end Algebra.IsCentral
.lake/packages/mathlib/Mathlib/Algebra/Central/Basic.lean
import Mathlib.Algebra.Central.Defs /-! # Central Algebras In this file, we prove some basic results about central algebras over a commutative ring. ## Main results - `Algebra.IsCentral.center_eq_bot`: the center of a central algebra over `K` is equal to `K`. - `Algebra.IsCentral.self`: a commutative ring is a central algebra over itself. - `Algebra.IsCentral.baseField_essentially_unique`: Let `D/K/k` be a tower of scalars where `K` and `k` are fields. If `D` is a nontrivial central algebra over `k`, `K` is isomorphic to `k`. -/ universe u v namespace Algebra.IsCentral variable (K : Type u) [CommSemiring K] (D D' : Type v) [Semiring D] [Algebra K D] [h : IsCentral K D] [Semiring D'] [Algebra K D'] @[simp] lemma center_eq_bot : Subalgebra.center K D = ⊥ := eq_bot_iff.2 IsCentral.out variable {D} in lemma mem_center_iff {x : D} : x ∈ Subalgebra.center K D ↔ ∃ (a : K), x = algebraMap K D a := by rw [center_eq_bot, Algebra.mem_bot] simp [eq_comm] instance self : IsCentral K K where out x := by simp [Algebra.mem_bot] lemma baseField_essentially_unique (k K D : Type*) [Field k] [Field K] [Ring D] [Nontrivial D] [Algebra k K] [Algebra K D] [Algebra k D] [IsScalarTower k K D] [IsCentral k D] : Function.Bijective (algebraMap k K) := by haveI : IsCentral K D := { out := fun x ↦ show x ∈ Subalgebra.center k D → _ by simp only [center_eq_bot, mem_bot, Set.mem_range, forall_exists_index] rintro x rfl exact ⟨algebraMap k K x, by simp [algebraMap_eq_smul_one, smul_assoc]⟩ } refine ⟨FaithfulSMul.algebraMap_injective k K, fun x => ?_⟩ have H : algebraMap K D x ∈ (Subalgebra.center K D : Set D) := Subalgebra.algebraMap_mem _ _ rw [show (Subalgebra.center K D : Set D) = Subalgebra.center k D by rfl] at H simp only [center_eq_bot, coe_bot, Set.mem_range] at H obtain ⟨x', H⟩ := H exact ⟨x', (algebraMap K D).injective <| by simp [← H, algebraMap_eq_smul_one]⟩ lemma of_algEquiv (e : D ≃ₐ[K] D') : IsCentral K D' where out x hx := have ⟨k, hk⟩ := h.1 ((MulEquivClass.apply_mem_center_iff e.symm).mpr hx) ⟨k, by simpa [ofId] using congr(e $hk)⟩ open MulOpposite in /-- Opposite algebra of a central algebra is central. This instance combined with the coming `IsSimpleRing` instance for the opposite of central simple algebra will be an inverse for an element in `BrauerGroup`, find out more about this in `Mathlib/Algebra/BrauerGroup/Basic.lean`. -/ instance : IsCentral K Dᵐᵒᵖ where out z hz := have ⟨k, hk⟩ := h.1 (MulOpposite.unop_mem_center_iff.mpr hz) ⟨k, by simpa using congr(op $hk)⟩ end Algebra.IsCentral
.lake/packages/mathlib/Mathlib/Algebra/Central/Defs.lean
import Mathlib.Algebra.Algebra.Subalgebra.Lattice /-! # Central Algebras In this file we define the predicate `Algebra.IsCentral K D` where `K` is a commutative ring and `D` is a (not necessarily commutative) `K`-algebra. ## Main definitions - `Algebra.IsCentral K D` : `D` is a central algebra over `K` iff the center of `D` is exactly `K`. ## Implementation notes We require the `K`-center of `D` to be smaller than or equal to the smallest subalgebra so that when we prove something is central, we don't need to prove `⊥ ≤ center K D` even though this direction is trivial. ### Central Simple Algebras To define central simple algebras, we could do the following: ```lean class Algebra.IsCentralSimple (K : Type u) [Field K] (D : Type v) [Ring D] [Algebra K D] where [is_central : IsCentral K D] [is_simple : IsSimpleRing D] ``` but an instance of `[Algebra.IsCentralSimple K D]` would not imply `[IsSimpleRing D]` because of synthesization orders (`K` cannot be inferred). Thus, to obtain a central simple `K`-algebra `D`, one should use `Algebra.IsCentral K D` and `IsSimpleRing D` separately. Note that the predicate `Algebra.IsCentral K D` and `IsSimpleRing D` makes sense just for `K` a `CommRing` but it doesn't give the right definition for central simple algebra; for a commutative ring base, one should use the theory of Azumaya algebras. In fact ideals of `K` immediately give rise to nontrivial quotients of `D` so there are no central simple algebras in this case according to our definition, if `K` is not a field. The theory of central simple algebras really is a theory over fields. Thus to declare a central simple algebra, one should use the following: ```lean variable (k D : Type*) [Field k] [Ring D] [Algebra k D] variable [Algebra.IsCentral k D] [IsSimpleRing D] variable [FiniteDimensional k D] ``` where `FiniteDimensional k D` is almost always assumed in most references, but some results do not need this assumption. ## Tags central algebra, center, simple ring, central simple algebra -/ universe u v /-- For a commutative ring `K` and a `K`-algebra `D`, we say that `D` is a central algebra over `K` if the center of `D` is the image of `K` in `D`. -/ class Algebra.IsCentral (K : Type u) [CommSemiring K] (D : Type v) [Semiring D] [Algebra K D] : Prop where out : Subalgebra.center K D ≤ ⊥
.lake/packages/mathlib/Mathlib/Algebra/CharP/Two.lean
import Mathlib.Algebra.CharP.Lemmas import Mathlib.GroupTheory.OrderOfElement /-! # Lemmas about rings of characteristic two This file contains results about `CharP R 2`, in the `CharTwo` namespace. The lemmas in this file with a `_sq` suffix are just special cases of the `_pow_char` lemmas elsewhere, with a shorter name for ease of discovery, and no need for a `[Fact (Prime 2)]` argument. -/ assert_not_exists Algebra LinearMap variable {R ι : Type*} namespace CharTwo section AddMonoidWithOne variable [AddMonoidWithOne R] theorem two_eq_zero [CharP R 2] : (2 : R) = 0 := by rw [← Nat.cast_two, CharP.cast_eq_zero] /-- The only hypotheses required to build a `CharP R 2` instance are `1 ≠ 0` and `2 = 0`. -/ theorem of_one_ne_zero_of_two_eq_zero (h₁ : (1 : R) ≠ 0) (h₂ : (2 : R) = 0) : CharP R 2 where cast_eq_zero_iff n := by obtain hn | hn := Nat.even_or_odd n · simp_rw [hn.two_dvd, iff_true] exact natCast_eq_zero_of_even_of_two_eq_zero hn h₂ · simp_rw [hn.not_two_dvd_nat, iff_false] rwa [natCast_eq_one_of_odd_of_two_eq_zero hn h₂] end AddMonoidWithOne section Semiring variable [Semiring R] [CharP R 2] @[scoped simp] theorem add_self_eq_zero (x : R) : x + x = 0 := by rw [← two_smul R x, two_eq_zero, zero_smul] @[scoped simp] protected theorem two_nsmul (x : R) : 2 • x = 0 := by rw [two_smul, add_self_eq_zero] @[scoped simp] protected theorem add_cancel_left (a b : R) : a + (a + b) = b := by rw [← add_assoc, add_self_eq_zero, zero_add] @[scoped simp] protected theorem add_cancel_right (a b : R) : a + b + b = a := by rw [add_assoc, add_self_eq_zero, add_zero] end Semiring section Ring variable [Ring R] [CharP R 2] @[scoped simp] theorem neg_eq (x : R) : -x = x := by rw [neg_eq_iff_add_eq_zero, add_self_eq_zero] theorem neg_eq' : Neg.neg = (id : R → R) := funext neg_eq @[scoped simp] theorem sub_eq_add (x y : R) : x - y = x + y := by rw [sub_eq_add_neg, neg_eq] theorem add_eq_iff_eq_add {a b c : R} : a + b = c ↔ a = c + b := by rw [← sub_eq_iff_eq_add, sub_eq_add] theorem eq_add_iff_add_eq {a b c : R} : a = b + c ↔ a + c = b := by rw [← eq_sub_iff_add_eq, sub_eq_add] @[scoped simp] protected theorem two_zsmul (x : R) : (2 : ℤ) • x = 0 := by rw [two_zsmul, add_self_eq_zero] protected theorem add_eq_zero {a b : R} : a + b = 0 ↔ a = b := by rw [← CharTwo.sub_eq_add, sub_eq_iff_eq_add, zero_add] end Ring section CommSemiring variable [CommSemiring R] [CharP R 2] theorem add_sq (x y : R) : (x + y) ^ 2 = x ^ 2 + y ^ 2 := add_pow_char _ _ _ theorem add_mul_self (x y : R) : (x + y) * (x + y) = x * x + y * y := by rw [← pow_two, ← pow_two, ← pow_two, add_sq] theorem list_sum_sq (l : List R) : l.sum ^ 2 = (l.map (· ^ 2)).sum := list_sum_pow_char _ _ theorem list_sum_mul_self (l : List R) : l.sum * l.sum = (List.map (fun x => x * x) l).sum := by simp_rw [← pow_two, list_sum_sq] theorem multiset_sum_sq (l : Multiset R) : l.sum ^ 2 = (l.map (· ^ 2)).sum := multiset_sum_pow_char _ _ theorem multiset_sum_mul_self (l : Multiset R) : l.sum * l.sum = (Multiset.map (fun x => x * x) l).sum := by simp_rw [← pow_two, multiset_sum_sq] theorem sum_sq (s : Finset ι) (f : ι → R) : (∑ i ∈ s, f i) ^ 2 = ∑ i ∈ s, f i ^ 2 := sum_pow_char _ _ _ theorem sum_mul_self (s : Finset ι) (f : ι → R) : ((∑ i ∈ s, f i) * ∑ i ∈ s, f i) = ∑ i ∈ s, f i * f i := by simp_rw [← pow_two, sum_sq] end CommSemiring namespace CommRing variable [CommRing R] [CharP R 2] [NoZeroDivisors R] theorem sq_injective : Function.Injective fun x : R ↦ x ^ 2 := by intro x y h rwa [← CharTwo.add_eq_zero, ← add_sq, pow_eq_zero_iff two_ne_zero, CharTwo.add_eq_zero] at h @[scoped simp] theorem sq_inj {x y : R} : x ^ 2 = y ^ 2 ↔ x = y := sq_injective.eq_iff end CommRing end CharTwo section ringChar variable [Ring R] theorem neg_one_eq_one_iff [Nontrivial R] : (-1 : R) = 1 ↔ ringChar R = 2 := by refine ⟨fun h => ?_, fun h => @CharTwo.neg_eq _ _ (ringChar.of_eq h) 1⟩ rw [eq_comm, ← sub_eq_zero, sub_neg_eq_add, ← Nat.cast_one, ← Nat.cast_add] at h exact ((Nat.dvd_prime Nat.prime_two).mp (ringChar.dvd h)).resolve_left CharP.ringChar_ne_one @[simp] theorem orderOf_neg_one [Nontrivial R] : orderOf (-1 : R) = if ringChar R = 2 then 1 else 2 := by split_ifs with h · rw [neg_one_eq_one_iff.2 h, orderOf_one] apply orderOf_eq_prime · simp simpa [neg_one_eq_one_iff] using h end ringChar section CharP variable [Ring R] lemma CharP.orderOf_eq_two_iff [Nontrivial R] [NoZeroDivisors R] (p : ℕ) (hp : p ≠ 2) [CharP R p] {x : R} : orderOf x = 2 ↔ x = -1 := by simp only [orderOf_eq_prime_iff, sq_eq_one_iff, ne_eq, or_and_right, and_not_self, false_or, and_iff_left_iff_imp] rintro rfl exact fun h ↦ hp ((ringChar.eq R p) ▸ (neg_one_eq_one_iff.1 h)) end CharP
.lake/packages/mathlib/Mathlib/Algebra/CharP/Lemmas.lean
import Mathlib.Algebra.CharP.Defs import Mathlib.Data.Nat.Multiplicity import Mathlib.Data.Nat.Choose.Sum /-! # Characteristic of semirings -/ assert_not_exists Algebra LinearMap orderOf open Finset variable {R S : Type*} namespace Commute variable [Semiring R] {p : ℕ} (hp : p.Prime) {x y : R} include hp protected lemma add_pow_prime_pow_eq' (h : Commute x y) (n : ℕ) : (x + y) ^ p ^ n = x ^ p ^ n + y ^ p ^ n + p * ∑ k ∈ Ioo 0 (p ^ n), x ^ k * y ^ (p ^ n - k) * ↑((p ^ n).choose k / p) := calc _ = ∑ k ∈ Icc 0 (p ^ n), x ^ k * y ^ (p ^ n - k) * (p ^ n).choose k := by rw [h.add_pow, ← Nat.Ico_zero_eq_range, Ico_add_one_right_eq_Icc] _ = x ^ p ^ n + y ^ p ^ n + ∑ k ∈ Ioo 0 (p ^ n), x ^ k * y ^ (p ^ n - k) * (p ^ n).choose k := by simp_rw [Icc_eq_cons_Ico (zero_le _), Ico_eq_cons_Ioo (pow_pos hp.pos _)] simp [-cons_eq_insert, add_assoc] _ = _ := by simp_rw [mul_sum] congr! 2 with k hk obtain ⟨hk₀, hk⟩ := mem_Ioo.1 hk -- The maths is over now. We just commute things to their place. rw [Nat.cast_comm, mul_assoc (_ * _)] norm_cast rw [Nat.div_mul_cancel (hp.dvd_choose_pow _ _)] <;> omega protected lemma add_pow_prime_pow_eq (h : Commute x y) (n : ℕ) : (x + y) ^ p ^ n = x ^ p ^ n + y ^ p ^ n + p * x * y * ∑ k ∈ Ioo 0 (p ^ n), x ^ (k - 1) * y ^ (p ^ n - k - 1) * ↑((p ^ n).choose k / p) := by rw [h.add_pow_prime_pow_eq' hp, mul_assoc _ x, mul_assoc, mul_sum _ _ (_ * _)] congr! 3 with k hk obtain ⟨hk₀, hk⟩ := mem_Ioo.1 hk rw [← mul_pow_sub_one (by omega), ← mul_pow_sub_one (n := p ^ n - k) (by omega)] rw [(h.pow_left _).mul_mul_mul_comm, mul_assoc (x * y)] protected lemma add_pow_prime_eq' (h : Commute x y) : (x + y) ^ p = x ^ p + y ^ p + p * ∑ k ∈ Ioo 0 p, x ^ k * y ^ (p - k) * ↑(p.choose k / p) := by simpa using h.add_pow_prime_pow_eq' hp 1 protected lemma add_pow_prime_eq (h : Commute x y) : (x + y) ^ p = x ^ p + y ^ p + p * x * y * ∑ k ∈ Ioo 0 p, x ^ (k - 1) * y ^ (p - k - 1) * ↑(p.choose k / p) := by simpa using h.add_pow_prime_pow_eq hp 1 protected theorem exists_add_pow_prime_pow_eq (h : Commute x y) (n : ℕ) : ∃ r, (x + y) ^ p ^ n = x ^ p ^ n + y ^ p ^ n + p * x * y * r := ⟨_, h.add_pow_prime_pow_eq hp n⟩ protected theorem exists_add_pow_prime_eq (h : Commute x y) : ∃ r, (x + y) ^ p = x ^ p + y ^ p + p * x * y * r := ⟨_, h.add_pow_prime_eq hp⟩ end Commute section CommSemiring variable [CommSemiring R] {p : ℕ} (hp : p.Prime) (x y : R) (n : ℕ) include hp lemma add_pow_prime_pow_eq' : (x + y) ^ p ^ n = x ^ p ^ n + y ^ p ^ n + p * ∑ k ∈ Ioo 0 (p ^ n), x ^ k * y ^ (p ^ n - k) * ↑((p ^ n).choose k / p) := (Commute.all x y).add_pow_prime_pow_eq' hp n lemma add_pow_prime_pow_eq : (x + y) ^ p ^ n = x ^ p ^ n + y ^ p ^ n + p * x * y * ∑ k ∈ Ioo 0 (p ^ n), x ^ (k - 1) * y ^ (p ^ n - k - 1) * ↑((p ^ n).choose k / p) := (Commute.all x y).add_pow_prime_pow_eq hp n lemma add_pow_prime_eq' : (x + y) ^ p = x ^ p + y ^ p + p * ∑ k ∈ Ioo 0 p, x ^ k * y ^ (p - k) * ↑(p.choose k / p) := (Commute.all x y).add_pow_prime_eq' hp theorem add_pow_prime_eq : (x + y) ^ p = x ^ p + y ^ p + p * x * y * ∑ k ∈ Ioo 0 p, x ^ (k - 1) * y ^ (p - k - 1) * ↑(p.choose k / p) := (Commute.all x y).add_pow_prime_eq hp theorem exists_add_pow_prime_pow_eq : ∃ r, (x + y) ^ p ^ n = x ^ p ^ n + y ^ p ^ n + p * x * y * r := (Commute.all x y).exists_add_pow_prime_pow_eq hp n theorem exists_add_pow_prime_eq : ∃ r, (x + y) ^ p = x ^ p + y ^ p + p * x * y * r := (Commute.all x y).exists_add_pow_prime_eq hp end CommSemiring section Semiring variable [Semiring R] {x y : R} (p n : ℕ) section ExpChar variable [hR : ExpChar R p] lemma add_pow_expChar_of_commute (h : Commute x y) : (x + y) ^ p = x ^ p + y ^ p := by obtain _ | hprime := hR · simp only [pow_one] · let ⟨r, hr⟩ := h.exists_add_pow_prime_eq hprime simp [hr] lemma add_pow_expChar_pow_of_commute (h : Commute x y) : (x + y) ^ p ^ n = x ^ p ^ n + y ^ p ^ n := by obtain _ | hprime := hR · simp only [one_pow, pow_one] · let ⟨r, hr⟩ := h.exists_add_pow_prime_pow_eq hprime n simp [hr] lemma add_pow_eq_mul_pow_add_pow_div_expChar_of_commute (h : Commute x y) : (x + y) ^ n = (x + y) ^ (n % p) * (x ^ p + y ^ p) ^ (n / p) := by rw [← add_pow_expChar_of_commute _ h, ← pow_mul, ← pow_add, Nat.mod_add_div] end ExpChar section CharP variable [hp : Fact p.Prime] [CharP R p] lemma add_pow_char_of_commute (h : Commute x y) : (x + y) ^ p = x ^ p + y ^ p := add_pow_expChar_of_commute _ h lemma add_pow_char_pow_of_commute (h : Commute x y) : (x + y) ^ p ^ n = x ^ p ^ n + y ^ p ^ n := add_pow_expChar_pow_of_commute _ _ h lemma add_pow_eq_mul_pow_add_pow_div_char_of_commute (h : Commute x y) : (x + y) ^ n = (x + y) ^ (n % p) * (x ^ p + y ^ p) ^ (n / p) := add_pow_eq_mul_pow_add_pow_div_expChar_of_commute _ _ h end CharP end Semiring section CommSemiring variable [CommSemiring R] (x y : R) (p n : ℕ) section ExpChar variable [hR : ExpChar R p] lemma add_pow_expChar : (x + y) ^ p = x ^ p + y ^ p := add_pow_expChar_of_commute _ <| .all .. lemma add_pow_expChar_pow : (x + y) ^ p ^ n = x ^ p ^ n + y ^ p ^ n := add_pow_expChar_pow_of_commute _ _ <| .all .. lemma add_pow_eq_mul_pow_add_pow_div_expChar : (x + y) ^ n = (x + y) ^ (n % p) * (x ^ p + y ^ p) ^ (n / p) := add_pow_eq_mul_pow_add_pow_div_expChar_of_commute _ _ <| .all .. end ExpChar section CharP variable [hp : Fact p.Prime] [CharP R p] lemma add_pow_char : (x + y) ^ p = x ^ p + y ^ p := add_pow_expChar .. lemma add_pow_char_pow : (x + y) ^ p ^ n = x ^ p ^ n + y ^ p ^ n := add_pow_expChar_pow .. lemma add_pow_eq_mul_pow_add_pow_div_char : (x + y) ^ n = (x + y) ^ (n % p) * (x ^ p + y ^ p) ^ (n / p) := add_pow_eq_mul_pow_add_pow_div_expChar .. end CharP end CommSemiring section Ring variable [Ring R] {x y : R} (p n : ℕ) section ExpChar variable [hR : ExpChar R p] include hR lemma sub_pow_expChar_of_commute (h : Commute x y) : (x - y) ^ p = x ^ p - y ^ p := by simp [eq_sub_iff_add_eq, ← add_pow_expChar_of_commute _ (h.sub_left rfl)] lemma sub_pow_expChar_pow_of_commute (h : Commute x y) : (x - y) ^ p ^ n = x ^ p ^ n - y ^ p ^ n := by simp [eq_sub_iff_add_eq, ← add_pow_expChar_pow_of_commute _ _ (h.sub_left rfl)] lemma sub_pow_eq_mul_pow_sub_pow_div_expChar_of_commute (h : Commute x y) : (x - y) ^ n = (x - y) ^ (n % p) * (x ^ p - y ^ p) ^ (n / p) := by rw [← sub_pow_expChar_of_commute _ h, ← pow_mul, ← pow_add, Nat.mod_add_div] variable (R) lemma neg_one_pow_expChar : (-1 : R) ^ p = -1 := by rw [eq_neg_iff_add_eq_zero] nth_rw 2 [← one_pow p] rw [← add_pow_expChar_of_commute _ (Commute.one_right _), neg_add_cancel, zero_pow (expChar_ne_zero R p)] lemma neg_one_pow_expChar_pow : (-1 : R) ^ p ^ n = -1 := by rw [eq_neg_iff_add_eq_zero] nth_rw 2 [← one_pow (p ^ n)] rw [← add_pow_expChar_pow_of_commute _ _ (Commute.one_right _), neg_add_cancel, zero_pow (pow_ne_zero _ <| expChar_ne_zero R p)] end ExpChar section CharP variable [hp : Fact p.Prime] [CharP R p] lemma sub_pow_char_of_commute (h : Commute x y) : (x - y) ^ p = x ^ p - y ^ p := sub_pow_expChar_of_commute _ h lemma sub_pow_char_pow_of_commute (h : Commute x y) : (x - y) ^ p ^ n = x ^ p ^ n - y ^ p ^ n := sub_pow_expChar_pow_of_commute _ _ h variable (R) lemma neg_one_pow_char : (-1 : R) ^ p = -1 := neg_one_pow_expChar .. lemma neg_one_pow_char_pow : (-1 : R) ^ p ^ n = -1 := neg_one_pow_expChar_pow .. lemma sub_pow_eq_mul_pow_sub_pow_div_char_of_commute (h : Commute x y) : (x - y) ^ n = (x - y) ^ (n % p) * (x ^ p - y ^ p) ^ (n / p) := sub_pow_eq_mul_pow_sub_pow_div_expChar_of_commute _ _ h end CharP end Ring section CommRing variable [CommRing R] (x y : R) (n : ℕ) {p : ℕ} section ExpChar variable [hR : ExpChar R p] lemma sub_pow_expChar : (x - y) ^ p = x ^ p - y ^ p := sub_pow_expChar_of_commute _ <| .all .. lemma sub_pow_expChar_pow : (x - y) ^ p ^ n = x ^ p ^ n - y ^ p ^ n := sub_pow_expChar_pow_of_commute _ _ <| .all .. lemma sub_pow_eq_mul_pow_sub_pow_div_expChar : (x - y) ^ n = (x - y) ^ (n % p) * (x ^ p - y ^ p) ^ (n / p) := sub_pow_eq_mul_pow_sub_pow_div_expChar_of_commute _ _ <| .all .. end ExpChar section CharP variable [hp : Fact p.Prime] [CharP R p] lemma sub_pow_char : (x - y) ^ p = x ^ p - y ^ p := sub_pow_expChar .. lemma sub_pow_char_pow : (x - y) ^ p ^ n = x ^ p ^ n - y ^ p ^ n := sub_pow_expChar_pow .. lemma sub_pow_eq_mul_pow_sub_pow_div_char : (x - y) ^ n = (x - y) ^ (n % p) * (x ^ p - y ^ p) ^ (n / p) := sub_pow_eq_mul_pow_sub_pow_div_expChar .. end CharP lemma Nat.Prime.dvd_add_pow_sub_pow_of_dvd (hpri : p.Prime) {r : R} (h₁ : r ∣ x ^ p) (h₂ : r ∣ p * x) : r ∣ (x + y) ^ p - y ^ p := by rw [add_pow_prime_eq hpri, add_right_comm, add_assoc, add_sub_assoc, add_sub_cancel_right] exact dvd_add h₁ (h₂.trans <| (dvd_mul_right ..).trans <| dvd_mul_right ..) end CommRing namespace CharP section variable (R) [NonAssocRing R] /-- The characteristic of a finite ring cannot be zero. -/ theorem char_ne_zero_of_finite (p : ℕ) [CharP R p] [Finite R] : p ≠ 0 := by rintro rfl haveI : CharZero R := charP_to_charZero R exact absurd Nat.cast_injective (not_injective_infinite_finite ((↑) : ℕ → R)) theorem ringChar_ne_zero_of_finite [Finite R] : ringChar R ≠ 0 := char_ne_zero_of_finite R (ringChar R) end section Ring variable (R) [Ring R] [NoZeroDivisors R] [Nontrivial R] [Finite R] theorem char_is_prime (p : ℕ) [CharP R p] : p.Prime := Or.resolve_right (char_is_prime_or_zero R p) (char_ne_zero_of_finite R p) lemma prime_ringChar : Nat.Prime (ringChar R) := by apply CharP.char_prime_of_ne_zero R exact CharP.ringChar_ne_zero_of_finite R end Ring end CharP /- Preliminary definitions and results for the Frobenius map. Necessary here for simple results about sums of `p`-powers that are used in files forbidding to import algebra-related definitions (see `Mathlib/Algebra/CharP/Two.lean`). -/ section Frobenius variable (R : Type*) [CommSemiring R] variable (p n : ℕ) [ExpChar R p] /-- The Frobenius map `x ↦ x ^ p`. -/ def frobenius : R →+* R where __ := powMonoidHom p map_zero' := zero_pow (expChar_pos R p).ne' map_add' _ _ := add_pow_expChar .. /-- The iterated Frobenius map `x ↦ x ^ p ^ n`. -/ def iterateFrobenius : R →+* R where __ := powMonoidHom (p ^ n) map_zero' := zero_pow (expChar_pow_pos R p n).ne' map_add' _ _ := add_pow_expChar_pow .. variable {R} lemma list_sum_pow_char (l : List R) : l.sum ^ p = (l.map (· ^ p : R → R)).sum := map_list_sum (frobenius R p) _ lemma multiset_sum_pow_char (s : Multiset R) : s.sum ^ p = (s.map (· ^ p : R → R)).sum := map_multiset_sum (frobenius R p) _ lemma sum_pow_char {ι : Type*} (s : Finset ι) (f : ι → R) : (∑ i ∈ s, f i) ^ p = ∑ i ∈ s, f i ^ p := map_sum (frobenius R p) _ _ lemma list_sum_pow_char_pow (l : List R) : l.sum ^ p ^ n = (l.map (· ^ p ^ n : R → R)).sum := map_list_sum (iterateFrobenius R p n) _ lemma multiset_sum_pow_char_pow (s : Multiset R) : s.sum ^ p ^ n = (s.map (· ^ p ^ n : R → R)).sum := map_multiset_sum (iterateFrobenius R p n) _ lemma sum_pow_char_pow {ι : Type*} (s : Finset ι) (f : ι → R) : (∑ i ∈ s, f i) ^ p ^ n = ∑ i ∈ s, f i ^ p ^ n := map_sum (iterateFrobenius R p n) _ _ end Frobenius
.lake/packages/mathlib/Mathlib/Algebra/CharP/LocalRing.lean
import Mathlib.Algebra.CharP.Defs import Mathlib.Algebra.IsPrimePow import Mathlib.Data.Nat.Factorization.Basic import Mathlib.RingTheory.LocalRing.ResidueField.Defs /-! # Characteristics of local rings ## Main result - `charP_zero_or_prime_power`: In a commutative local ring the characteristic is either zero or a prime power. -/ /-- In a local ring the characteristic is either zero or a prime power. -/ theorem charP_zero_or_prime_power (R : Type*) [CommRing R] [IsLocalRing R] (q : ℕ) [char_R_q : CharP R q] : q = 0 ∨ IsPrimePow q := by -- Assume `q := char(R)` is not zero. apply or_iff_not_imp_left.2 intro q_pos let K := IsLocalRing.ResidueField R haveI RM_char := ringChar.charP K let r := ringChar K let n := q.factorization r -- `r := char(R/m)` is either prime or zero: rcases CharP.char_is_prime_or_zero K r with r_prime | r_zero · let a := q / r ^ n -- If `r` is prime, we can write `q` as `a * r^n` ... have q_eq_a_mul_rn : q = r ^ n * a := by rw [Nat.mul_div_cancel' (Nat.ordProj_dvd q r)] have r_ne_dvd_a := Nat.not_dvd_ordCompl r_prime q_pos have rn_dvd_q : r ^ n ∣ q := ⟨a, q_eq_a_mul_rn⟩ rw [mul_comm] at q_eq_a_mul_rn -- ... where `a` is a unit. have a_unit : IsUnit (a : R) := by by_contra g rw [← mem_nonunits_iff] at g rw [← IsLocalRing.mem_maximalIdeal] at g have a_cast_zero := Ideal.Quotient.eq_zero_iff_mem.2 g rw [map_natCast] at a_cast_zero have r_dvd_a := (ringChar.spec K a).1 a_cast_zero exact absurd r_dvd_a r_ne_dvd_a have rn_cast_zero : ↑(r ^ n) = (0 : R) := by rw [← one_mul (↑(r ^ n) : R), ← a_unit.val_inv_mul, mul_assoc, ← Nat.cast_mul, ← q_eq_a_mul_rn, CharP.cast_eq_zero R q, mul_zero] have q_eq_rn := Nat.dvd_antisymm ((CharP.cast_eq_zero_iff R q (r ^ n)).mp rn_cast_zero) rn_dvd_q have n_pos : n ≠ 0 := fun n_zero => absurd (by simpa [n_zero] using q_eq_rn) (CharP.char_ne_one R q) -- Definition of prime power: `∃ r n, Prime r ∧ 0 < n ∧ r ^ n = q`. exact ⟨r, ⟨n, ⟨r_prime.prime, ⟨pos_iff_ne_zero.mpr n_pos, q_eq_rn.symm⟩⟩⟩⟩ · haveI K_char_p_0 := ringChar.of_eq r_zero haveI K_char_zero : CharZero K := CharP.charP_to_charZero K haveI R_char_zero := RingHom.charZero (IsLocalRing.residue R) -- Finally, `r = 0` would lead to a contradiction: have q_zero := CharP.eq R char_R_q (CharP.ofCharZero R) exact absurd q_zero q_pos
.lake/packages/mathlib/Mathlib/Algebra/CharP/MixedCharZero.lean
import Mathlib.Algebra.CharP.LocalRing import Mathlib.RingTheory.Ideal.Quotient.Basic import Mathlib.Tactic.FieldSimp /-! # Equal and mixed characteristic In commutative algebra, some statements are simpler when working over a `ℚ`-algebra `R`, in which case one also says that the ring has "equal characteristic zero". A ring that is not a `ℚ`-algebra has either positive characteristic or there exists a prime ideal `I ⊂ R` such that the quotient `R ⧸ I` has positive characteristic `p > 0`. In this case one speaks of "mixed characteristic `(0, p)`", where `p` is only unique if `R` is local. Examples of mixed characteristic rings are `ℤ` or the `p`-adic integers/numbers. This file provides the main theorem `split_by_characteristic` that splits any proposition `P` into the following three cases: 1) Positive characteristic: `CharP R p` (where `p ≠ 0`) 2) Equal characteristic zero: `Algebra ℚ R` 3) Mixed characteristic: `MixedCharZero R p` (where `p` is prime) ## Main definitions - `MixedCharZero` : A ring has mixed characteristic `(0, p)` if it has characteristic zero and there exists an ideal such that the quotient `R ⧸ I` has characteristic `p`. ## Main results - `split_equalCharZero_mixedCharZero` : Split a statement into equal/mixed characteristic zero. This main theorem has the following three corollaries which include the positive characteristic case for convenience: - `split_by_characteristic` : Generally consider positive char `p ≠ 0`. - `split_by_characteristic_domain` : In a domain we can assume that `p` is prime. - `split_by_characteristic_localRing` : In a local ring we can assume that `p` is a prime power. ## Implementation Notes We use the terms `EqualCharZero` and `AlgebraRat` despite not being such definitions in mathlib. The former refers to the statement `∀ I : Ideal R, I ≠ ⊤ → CharZero (R ⧸ I)`, the latter refers to the existence of an instance `[Algebra ℚ R]`. The two are shown to be equivalent conditions. ## TODO - Relate mixed characteristic in a local ring to p-adic numbers [NumberTheory.PAdics]. -/ variable (R : Type*) [CommRing R] /-! ### Mixed characteristic -/ /-- A ring of characteristic zero is of "mixed characteristic `(0, p)`" if there exists an ideal such that the quotient `R ⧸ I` has characteristic `p`. **Remark:** For `p = 0`, `MixedChar R 0` is a meaningless definition (i.e. satisfied by any ring) as `R ⧸ ⊥ ≅ R` has by definition always characteristic zero. One could require `(I ≠ ⊥)` in the definition, but then `MixedChar R 0` would mean something like `ℤ`-algebra of extension degree `≥ 1` and would be completely independent from whether something is a `ℚ`-algebra or not (e.g. `ℚ[X]` would satisfy it but `ℚ` wouldn't). -/ class MixedCharZero (p : ℕ) : Prop where [toCharZero : CharZero R] charP_quotient : ∃ I : Ideal R, I ≠ ⊤ ∧ CharP (R ⧸ I) p namespace MixedCharZero /-- Reduction to `p` prime: When proving any statement `P` about mixed characteristic rings we can always assume that `p` is prime. -/ theorem reduce_to_p_prime {P : Prop} : (∀ p > 0, MixedCharZero R p → P) ↔ ∀ p : ℕ, p.Prime → MixedCharZero R p → P := by constructor · intro h q q_prime q_mixedChar exact h q (Nat.Prime.pos q_prime) q_mixedChar · intro h q q_pos q_mixedChar rcases q_mixedChar.charP_quotient with ⟨I, hI_ne_top, _⟩ -- Krull's Thm: There exists a prime ideal `P` such that `I ≤ P` rcases Ideal.exists_le_maximal I hI_ne_top with ⟨M, hM_max, h_IM⟩ let r := ringChar (R ⧸ M) have r_pos : r ≠ 0 := by have q_zero := congr_arg (Ideal.Quotient.factor h_IM) (CharP.cast_eq_zero (R ⧸ I) q) simp only [map_natCast, map_zero] at q_zero apply ne_zero_of_dvd_ne_zero (ne_of_gt q_pos) exact (CharP.cast_eq_zero_iff (R ⧸ M) r q).mp q_zero have r_prime : Nat.Prime r := or_iff_not_imp_right.1 (CharP.char_is_prime_or_zero (R ⧸ M) r) r_pos apply h r r_prime have : CharZero R := q_mixedChar.toCharZero exact ⟨⟨M, hM_max.ne_top, ringChar.of_eq rfl⟩⟩ /-- Reduction to `I` prime ideal: When proving statements about mixed characteristic rings, after we reduced to `p` prime, we can assume that the ideal `I` in the definition is maximal. -/ theorem reduce_to_maximal_ideal {p : ℕ} (hp : Nat.Prime p) : (∃ I : Ideal R, I ≠ ⊤ ∧ CharP (R ⧸ I) p) ↔ ∃ I : Ideal R, I.IsMaximal ∧ CharP (R ⧸ I) p := by constructor · intro g rcases g with ⟨I, ⟨hI_not_top, _⟩⟩ -- Krull's Thm: There exists a prime ideal `M` such that `I ≤ M`. rcases Ideal.exists_le_maximal I hI_not_top with ⟨M, ⟨hM_max, hM_ge⟩⟩ use M constructor · exact hM_max · cases CharP.exists (R ⧸ M) with | intro r hr => convert hr have r_dvd_p : r ∣ p := by rw [← CharP.cast_eq_zero_iff (R ⧸ M) r p] convert congr_arg (Ideal.Quotient.factor hM_ge) (CharP.cast_eq_zero (R ⧸ I) p) symm apply (Nat.Prime.eq_one_or_self_of_dvd hp r r_dvd_p).resolve_left exact CharP.char_ne_one (R ⧸ M) r · intro ⟨I, hI_max, h_charP⟩ use I exact ⟨Ideal.IsMaximal.ne_top hI_max, h_charP⟩ end MixedCharZero /-! ### Equal characteristic zero A commutative ring `R` has "equal characteristic zero" if it satisfies one of the following equivalent properties: 1) `R` is a `ℚ`-algebra. 2) The quotient `R ⧸ I` has characteristic zero for any proper ideal `I ⊂ R`. 3) `R` has characteristic zero and does not have mixed characteristic for any prime `p`. We show `(1) ↔ (2) ↔ (3)`, and most of the following is concerned with constructing an explicit algebra map `ℚ →+* R` (given by `x ↦ (x.num : R) /ₚ ↑x.pnatDen`) for the direction `(1) ← (2)`. Note: Property `(2)` is denoted as `EqualCharZero` in the statement names below. -/ namespace EqualCharZero /-- `ℚ`-algebra implies equal characteristic. -/ theorem of_algebraRat [Algebra ℚ R] : ∀ I : Ideal R, I ≠ ⊤ → CharZero (R ⧸ I) := by intro I hI constructor intro a b h_ab contrapose! hI -- `↑a - ↑b` is a unit contained in `I`, which contradicts `I ≠ ⊤`. refine I.eq_top_of_isUnit_mem ?_ (IsUnit.map (algebraMap ℚ R) (IsUnit.mk0 (a - b : ℚ) ?_)) · simpa only [← Ideal.Quotient.eq_zero_iff_mem, map_sub, sub_eq_zero, map_natCast] simpa only [Ne, sub_eq_zero] using (@Nat.cast_injective ℚ _ _).ne hI section ConstructionAlgebraRat variable {R} /-- Internal: Not intended to be used outside this local construction. -/ theorem PNat.isUnit_natCast [h : Fact (∀ I : Ideal R, I ≠ ⊤ → CharZero (R ⧸ I))] (n : ℕ+) : IsUnit (n : R) := by -- `n : R` is a unit iff `(n)` is not a proper ideal in `R`. rw [← Ideal.span_singleton_eq_top] -- So by contrapositive, we should show the quotient does not have characteristic zero. apply not_imp_comm.mp (h.elim (Ideal.span {↑n})) intro h_char_zero -- In particular, the image of `n` in the quotient should be nonzero. apply h_char_zero.cast_injective.ne n.ne_zero -- But `n` generates the ideal, so its image is clearly zero. rw [← map_natCast (Ideal.Quotient.mk _), Nat.cast_zero, Ideal.Quotient.eq_zero_iff_mem] exact Ideal.subset_span (Set.mem_singleton _) @[coe] noncomputable def pnatCast [Fact (∀ I : Ideal R, I ≠ ⊤ → CharZero (R ⧸ I))] : ℕ+ → Rˣ := fun n => (PNat.isUnit_natCast n).unit /-- Internal: Not intended to be used outside this local construction. -/ noncomputable instance coePNatUnits [Fact (∀ I : Ideal R, I ≠ ⊤ → CharZero (R ⧸ I))] : Coe ℕ+ Rˣ := ⟨EqualCharZero.pnatCast⟩ /-- Internal: Not intended to be used outside this local construction. -/ @[simp] theorem pnatCast_one [Fact (∀ I : Ideal R, I ≠ ⊤ → CharZero (R ⧸ I))] : ((1 : ℕ+) : Rˣ) = 1 := by apply Units.ext rw [Units.val_one] change ((PNat.isUnit_natCast (R := R) 1).unit : R) = 1 rw [IsUnit.unit_spec (PNat.isUnit_natCast 1)] rw [PNat.one_coe, Nat.cast_one] /-- Internal: Not intended to be used outside this local construction. -/ @[simp] theorem pnatCast_eq_natCast [Fact (∀ I : Ideal R, I ≠ ⊤ → CharZero (R ⧸ I))] (n : ℕ+) : ((n : Rˣ) : R) = ↑n := by change ((PNat.isUnit_natCast (R := R) n).unit : R) = ↑n simp only [IsUnit.unit_spec] /-- Equal characteristic implies `ℚ`-algebra. -/ noncomputable def algebraRat (h : ∀ I : Ideal R, I ≠ ⊤ → CharZero (R ⧸ I)) : Algebra ℚ R := haveI : Fact (∀ I : Ideal R, I ≠ ⊤ → CharZero (R ⧸ I)) := ⟨h⟩ RingHom.toAlgebra { toFun := fun x => x.num /ₚ ↑x.pnatDen map_zero' := by simp [divp] map_one' := by simp map_mul' := by intro a b simp only [← divp_assoc, divp_mul_eq_mul_divp, divp_divp_eq_divp_mul, divp_eq_iff_mul_eq, pnatCast_eq_natCast, Rat.coe_pnatDen, Units.val_mul] trans (↑((a * b).num * a.den * b.den) : R) · simp_rw [Int.cast_mul, Int.cast_natCast] ring rw [Rat.mul_num_den' a b] simp map_add' := by intro a b simp only [Units.add_divp, pnatCast_eq_natCast, Rat.coe_pnatDen, divp_mul_eq_mul_divp, Units.divp_add, divp_divp_eq_divp_mul, divp_eq_iff_mul_eq, Units.val_mul] trans (↑((a + b).num * a.den * b.den) : R) · simp_rw [Int.cast_mul, Int.cast_natCast] ring rw [Rat.add_num_den' a b] simp } end ConstructionAlgebraRat /-- Not mixed characteristic implies equal characteristic. -/ theorem of_not_mixedCharZero [CharZero R] (h : ∀ p > 0, ¬MixedCharZero R p) : ∀ I : Ideal R, I ≠ ⊤ → CharZero (R ⧸ I) := by intro I hI_ne_top suffices CharP (R ⧸ I) 0 from CharP.charP_to_charZero _ cases CharP.exists (R ⧸ I) with | intro p hp => cases p with | zero => exact hp | succ p => have h_mixed : MixedCharZero R p.succ := ⟨⟨I, ⟨hI_ne_top, hp⟩⟩⟩ exact absurd h_mixed (h p.succ p.succ_pos) /-- Equal characteristic implies not mixed characteristic. -/ theorem to_not_mixedCharZero (h : ∀ I : Ideal R, I ≠ ⊤ → CharZero (R ⧸ I)) : ∀ p > 0, ¬MixedCharZero R p := by intro p p_pos by_contra hp_mixedChar rcases hp_mixedChar.charP_quotient with ⟨I, hI_ne_top, hI_p⟩ replace hI_zero : CharP (R ⧸ I) 0 := @CharP.ofCharZero _ _ (h I hI_ne_top) exact absurd (CharP.eq (R ⧸ I) hI_p hI_zero) (ne_of_gt p_pos) /-- A ring of characteristic zero has equal characteristic iff it does not have mixed characteristic for any `p`. -/ theorem iff_not_mixedCharZero [CharZero R] : (∀ I : Ideal R, I ≠ ⊤ → CharZero (R ⧸ I)) ↔ ∀ p > 0, ¬MixedCharZero R p := ⟨to_not_mixedCharZero R, of_not_mixedCharZero R⟩ /-- A ring is a `ℚ`-algebra iff it has equal characteristic zero. -/ theorem nonempty_algebraRat_iff : Nonempty (Algebra ℚ R) ↔ ∀ I : Ideal R, I ≠ ⊤ → CharZero (R ⧸ I) := by constructor · intro h_alg haveI h_alg' : Algebra ℚ R := h_alg.some apply of_algebraRat · intro h apply Nonempty.intro exact algebraRat h end EqualCharZero /-- A ring of characteristic zero is not a `ℚ`-algebra iff it has mixed characteristic for some `p`. -/ theorem isEmpty_algebraRat_iff_mixedCharZero [CharZero R] : IsEmpty (Algebra ℚ R) ↔ ∃ p > 0, MixedCharZero R p := by rw [← not_iff_not] push_neg rw [← EqualCharZero.iff_not_mixedCharZero] apply EqualCharZero.nonempty_algebraRat_iff /-! ### Splitting statements into different characteristic Statements to split a proof by characteristic. There are 3 theorems here that are very similar. They only differ in the assumptions we can make on the positive characteristic case: Generally we need to consider all `p ≠ 0`, but if `R` is a local ring, we can assume that `p` is a prime power. And if `R` is a domain, we can even assume that `p` is prime. -/ section MainStatements variable {P : Prop} /-- Split a `Prop` in characteristic zero into equal and mixed characteristic. -/ theorem split_equalCharZero_mixedCharZero [CharZero R] (h_equal : Algebra ℚ R → P) (h_mixed : ∀ p : ℕ, Nat.Prime p → MixedCharZero R p → P) : P := by by_cases h : ∃ p > 0, MixedCharZero R p · rcases h with ⟨p, ⟨H, hp⟩⟩ rw [← MixedCharZero.reduce_to_p_prime] at h_mixed exact h_mixed p H hp · apply h_equal rw [← isEmpty_algebraRat_iff_mixedCharZero, not_isEmpty_iff] at h exact h.some example (n : ℕ) (h : n ≠ 0) : 0 < n := zero_lt_iff.mpr h /-- Split any `Prop` over `R` into the three cases: - positive characteristic. - equal characteristic zero. - mixed characteristic `(0, p)`. -/ theorem split_by_characteristic (h_pos : ∀ p : ℕ, p ≠ 0 → CharP R p → P) (h_equal : Algebra ℚ R → P) (h_mixed : ∀ p : ℕ, Nat.Prime p → MixedCharZero R p → P) : P := by cases CharP.exists R with | intro p p_charP => by_cases h : p = 0 · rw [h] at p_charP haveI h0 : CharZero R := CharP.charP_to_charZero R exact split_equalCharZero_mixedCharZero R h_equal h_mixed · exact h_pos p h p_charP /-- In an `IsDomain R`, split any `Prop` over `R` into the three cases: - *prime* characteristic. - equal characteristic zero. - mixed characteristic `(0, p)`. -/ theorem split_by_characteristic_domain [IsDomain R] (h_pos : ∀ p : ℕ, Nat.Prime p → CharP R p → P) (h_equal : Algebra ℚ R → P) (h_mixed : ∀ p : ℕ, Nat.Prime p → MixedCharZero R p → P) : P := by refine split_by_characteristic R ?_ h_equal h_mixed intro p p_pos p_char have p_prime : Nat.Prime p := or_iff_not_imp_right.mp (CharP.char_is_prime_or_zero R p) p_pos exact h_pos p p_prime p_char /-- In a local ring `R`, split any predicate over `R` into the three cases: - *prime power* characteristic. - equal characteristic zero. - mixed characteristic `(0, p)`. -/ theorem split_by_characteristic_localRing [IsLocalRing R] (h_pos : ∀ p : ℕ, IsPrimePow p → CharP R p → P) (h_equal : Algebra ℚ R → P) (h_mixed : ∀ p : ℕ, Nat.Prime p → MixedCharZero R p → P) : P := by refine split_by_characteristic R ?_ h_equal h_mixed intro p p_pos p_char have p_ppow : IsPrimePow (p : ℕ) := or_iff_not_imp_left.mp (charP_zero_or_prime_power R p) p_pos exact h_pos p p_ppow p_char end MainStatements
.lake/packages/mathlib/Mathlib/Algebra/CharP/IntermediateField.lean
import Mathlib.Algebra.CharP.Algebra import Mathlib.FieldTheory.IntermediateField.Basic /-! # Characteristic of intermediate fields This file contains some convenient instances for determining the characteristic of intermediate fields. Some char zero instances are not provided, since they are already covered by `SubsemiringClass.instCharZero`. -/ variable {F E : Type*} [Field F] [Field E] [Algebra F E] namespace Subfield variable (L : Subfield F) (p : ℕ) instance charP [CharP F p] : CharP L p := RingHom.charP _ (algebraMap _ F).injective p instance expChar [ExpChar F p] : ExpChar L p := RingHom.expChar _ (algebraMap _ F).injective p end Subfield namespace IntermediateField variable (L : IntermediateField F E) (p : ℕ) instance charZero [CharZero F] : CharZero L := charZero_of_injective_algebraMap (algebraMap F _).injective instance charP [CharP F p] : CharP L p := charP_of_injective_algebraMap (algebraMap F _).injective p instance expChar [ExpChar F p] : ExpChar L p := expChar_of_injective_algebraMap (algebraMap F _).injective p instance charP' [CharP E p] : CharP L p := Subfield.charP L.toSubfield p instance expChar' [ExpChar E p] : ExpChar L p := Subfield.expChar L.toSubfield p end IntermediateField
.lake/packages/mathlib/Mathlib/Algebra/CharP/Invertible.lean
import Mathlib.Algebra.CharP.Defs import Mathlib.Algebra.Field.Defs import Mathlib.Algebra.Ring.Parity import Mathlib.Algebra.GroupWithZero.Invertible import Mathlib.Algebra.Ring.Int.Defs import Mathlib.Data.Int.GCD import Mathlib.Data.Nat.Cast.Commute /-! # Invertibility of elements given a characteristic This file includes some instances of `Invertible` for specific numbers in characteristic zero. Some more cases are given as a `def`, to be included only when needed. To construct instances for concrete numbers, `invertibleOfNonzero` is a useful definition. -/ variable {R K : Type*} /-- When two is invertible, every element is `Even`. -/ @[simp] theorem Even.all [Semiring R] [Invertible (2 : R)] (a : R) : Even a := .of_isUnit_two (isUnit_of_invertible _) _ /-- When two is invertible in a ring, every element is `Odd`. -/ @[simp low] theorem Odd.all [Ring R] [Invertible (2 : R)] (a : R) : Odd a := .of_isUnit_two (isUnit_of_invertible _) _ section Ring variable [Ring R] {p : ℕ} [CharP R p] theorem not_ringChar_dvd_of_invertible {t : ℕ} [Invertible (t : R)] [Nontrivial R] : ¬ringChar R ∣ t := by rw [← ringChar.spec, ← Ne] exact Invertible.ne_zero (t : R) theorem CharP.intCast_mul_natCast_gcdA_eq_gcd (n : ℕ) : (n * n.gcdA p : R) = n.gcd p := by suffices ↑(n * n.gcdA p + p * n.gcdB p : ℤ) = ((n.gcd p : ℤ) : R) by simpa using this rw [← Nat.gcd_eq_gcd_ab] theorem CharP.natCast_gcdA_mul_intCast_eq_gcd (n : ℕ) : (n.gcdA p * n : R) = n.gcd p := Nat.commute_cast _ _ |>.eq.trans <| CharP.intCast_mul_natCast_gcdA_eq_gcd n /-- In a ring of characteristic `p`, `(n : R)` is invertible when `n` is coprime with `p`, with inverse `n.gcdA p`. -/ def invertibleOfCoprime {n : ℕ} (h : n.Coprime p) : Invertible (n : R) where invOf := n.gcdA p invOf_mul_self := by rw [CharP.natCast_gcdA_mul_intCast_eq_gcd, h, Nat.cast_one] mul_invOf_self := by rw [CharP.intCast_mul_natCast_gcdA_eq_gcd, h, Nat.cast_one] theorem invOf_eq_of_coprime {n : ℕ} [Invertible (n : R)] (h : n.Coprime p) : ⅟(n : R) = n.gcdA p := by letI : Invertible (n : R) := invertibleOfCoprime h convert (rfl : ⅟(n : R) = _) theorem CharP.isUnit_natCast_iff {n : ℕ} (hp : p.Prime) : IsUnit (n : R) ↔ ¬p ∣ n where mp h := by have := CharP.nontrivial_of_char_ne_one (R := R) hp.ne_one rw [← CharP.cast_eq_zero_iff (R := R)] exact h.ne_zero mpr not_dvd := letI := invertibleOfCoprime (R := R) (hp.coprime_iff_not_dvd.2 not_dvd).symm isUnit_of_invertible _ theorem CharP.isUnit_ofNat_iff {n : ℕ} [n.AtLeastTwo] (hp : p.Prime) : IsUnit (ofNat(n) : R) ↔ ¬p ∣ ofNat(n) := CharP.isUnit_natCast_iff hp theorem CharP.isUnit_intCast_iff {z : ℤ} (hp : p.Prime) : IsUnit (z : R) ↔ ¬↑p ∣ z := by obtain ⟨n, rfl | rfl⟩ := z.eq_nat_or_neg · simp [CharP.isUnit_natCast_iff hp, Int.ofNat_dvd] · simp [CharP.isUnit_natCast_iff hp, Int.dvd_neg, Int.ofNat_dvd] end Ring section Semifield variable [Semifield K] /-- A natural number `t` is invertible in a semifield `K` if the characteristic of `K` does not divide `t`. -/ def invertibleOfRingCharNotDvd {t : ℕ} (not_dvd : ¬ringChar K ∣ t) : Invertible (t : K) := invertibleOfNonzero fun h => not_dvd ((ringChar.spec K t).mp h) /-- A natural number `t` is invertible in a semifield `K` of characteristic `p` if `p` does not divide `t`. -/ def invertibleOfCharPNotDvd {p : ℕ} [CharP K p] {t : ℕ} (not_dvd : ¬p ∣ t) : Invertible (t : K) := invertibleOfNonzero fun h => not_dvd ((CharP.cast_eq_zero_iff K p t).mp h) -- warning: this could potentially loop with `Invertible.ne_zero` - if there are weird type-class -- loops, watch out for that. instance invertibleOfPos [CharZero K] (n : ℕ) [NeZero n] : Invertible (n : K) := invertibleOfNonzero <| NeZero.out end Semifield section DivisionSemiring variable [DivisionSemiring K] [CharZero K] instance invertibleSucc (n : ℕ) : Invertible (n.succ : K) := invertibleOfNonzero (Nat.cast_ne_zero.mpr (Nat.succ_ne_zero _)) /-! A few `Invertible n` instances for small numerals `n`. Feel free to add your own number when you need its inverse. -/ instance invertibleTwo : Invertible (2 : K) := invertibleOfNonzero (mod_cast (by decide : 2 ≠ 0)) instance invertibleThree : Invertible (3 : K) := invertibleOfNonzero (mod_cast (by decide : 3 ≠ 0)) end DivisionSemiring
.lake/packages/mathlib/Mathlib/Algebra/CharP/Pi.lean
import Mathlib.Algebra.CharP.Defs import Mathlib.Algebra.Ring.Pi /-! # Characteristic of semirings of functions -/ universe u v namespace CharP instance pi (ι : Type u) [hi : Nonempty ι] (R : Type v) [Semiring R] (p : ℕ) [CharP R p] : CharP (ι → R) p := ⟨fun x => let ⟨i⟩ := hi Iff.symm <| (CharP.cast_eq_zero_iff R p x).symm.trans ⟨fun h => funext fun j => show Pi.evalRingHom (fun _ => R) j (↑x : ι → R) = 0 by rw [map_natCast, h], fun h => map_natCast (Pi.evalRingHom (fun _ : ι => R) i) x ▸ by rw [h, RingHom.map_zero]⟩⟩ -- diamonds instance pi' (ι : Type u) [Nonempty ι] (R : Type v) [CommRing R] (p : ℕ) [CharP R p] : CharP (ι → R) p := CharP.pi ι R p end CharP
.lake/packages/mathlib/Mathlib/Algebra/CharP/Subring.lean
import Mathlib.Algebra.CharP.Algebra /-! # Characteristic of subrings -/ universe u v namespace CharP instance subsemiring (R : Type u) [Semiring R] (p : ℕ) [CharP R p] (S : Subsemiring R) : CharP S p := ⟨fun x => Iff.symm <| (CharP.cast_eq_zero_iff R p x).symm.trans ⟨fun h => Subtype.eq <| show S.subtype x = 0 by rw [map_natCast, h], fun h => map_natCast S.subtype x ▸ by rw [h, RingHom.map_zero]⟩⟩ instance subring (R : Type u) [Ring R] (p : ℕ) [CharP R p] (S : Subring R) : CharP S p := ⟨fun x => Iff.symm <| (CharP.cast_eq_zero_iff R p x).symm.trans ⟨fun h => Subtype.eq <| show S.subtype x = 0 by rw [map_natCast, h], fun h => map_natCast S.subtype x ▸ by rw [h, RingHom.map_zero]⟩⟩ instance subring' (R : Type u) [CommRing R] (p : ℕ) [CharP R p] (S : Subring R) : CharP S p := CharP.subring R p S /-- The characteristic of a division ring is equal to the characteristic of its center -/ theorem charP_center_iff {R : Type u} [Ring R] {p : ℕ} : CharP (Subring.center R) p ↔ CharP R p := (algebraMap (Subring.center R) R).charP_iff Subtype.val_injective p end CharP namespace ExpChar theorem expChar_center_iff {R : Type u} [Ring R] {p : ℕ} : ExpChar (Subring.center R) p ↔ ExpChar R p := (algebraMap (Subring.center R) R).expChar_iff Subtype.val_injective p end ExpChar
.lake/packages/mathlib/Mathlib/Algebra/CharP/Basic.lean
import Mathlib.Algebra.CharP.Defs import Mathlib.Algebra.Group.Fin.Basic import Mathlib.Algebra.Ring.ULift import Mathlib.Algebra.Ring.Opposite import Mathlib.Data.Int.ModEq import Mathlib.Data.Nat.Cast.Prod import Mathlib.Data.ULift import Mathlib.Order.Interval.Set.Defs import Mathlib.Algebra.Ring.GrindInstances /-! # Characteristic of semirings This file collects some fundamental results on the characteristic of rings that don't need the extra imports of `CharP/Lemmas.lean`. As such, we can probably reorganize and find a better home for most of these lemmas. -/ assert_not_exists Finset TwoSidedIdeal open Set variable (R : Type*) namespace CharP section AddMonoidWithOne variable [AddMonoidWithOne R] (p : ℕ) variable [CharP R p] {a b : ℕ} lemma natCast_eq_natCast' (h : a ≡ b [MOD p]) : (a : R) = b := by wlog hle : a ≤ b · exact (this R p h.symm (le_of_not_ge hle)).symm rw [Nat.modEq_iff_dvd' hle] at h rw [← Nat.sub_add_cancel hle, Nat.cast_add, (cast_eq_zero_iff R p _).mpr h, zero_add] lemma natCast_eq_natCast_mod (a : ℕ) : (a : R) = a % p := natCast_eq_natCast' R p (Nat.mod_modEq a p).symm variable [IsRightCancelAdd R] lemma natCast_eq_natCast : (a : R) = b ↔ a ≡ b [MOD p] := by wlog hle : a ≤ b · rw [eq_comm, this R p (le_of_not_ge hle), Nat.ModEq.comm] rw [Nat.modEq_iff_dvd' hle, ← cast_eq_zero_iff R p (b - a), ← add_right_cancel_iff (G := R) (a := a) (b := b - a), zero_add, ← Nat.cast_add, Nat.sub_add_cancel hle, eq_comm] lemma natCast_injOn_Iio : (Set.Iio p).InjOn ((↑) : ℕ → R) := fun _a ha _b hb hab ↦ ((natCast_eq_natCast _ _).1 hab).eq_of_lt_of_lt ha hb end AddMonoidWithOne section AddGroupWithOne variable [AddGroupWithOne R] (p : ℕ) [CharP R p] {a b : ℤ} lemma intCast_eq_intCast : (a : R) = b ↔ a ≡ b [ZMOD p] := by rw [eq_comm, ← sub_eq_zero, ← Int.cast_sub, CharP.intCast_eq_zero_iff R p, Int.modEq_iff_dvd] lemma intCast_eq_intCast_mod : (a : R) = a % (p : ℤ) := (CharP.intCast_eq_intCast R p).mpr (Int.mod_modEq a p).symm lemma intCast_injOn_Ico [IsRightCancelAdd R] : InjOn (Int.cast : ℤ → R) (Ico 0 p) := by rintro a ⟨ha₀, ha⟩ b ⟨hb₀, hb⟩ hab lift a to ℕ using ha₀ lift b to ℕ using hb₀ norm_cast at * exact natCast_injOn_Iio _ _ ha hb hab end AddGroupWithOne end CharP namespace CharP section NonAssocSemiring variable {R} [NonAssocSemiring R] variable (R) in /-- If a ring `R` is of characteristic `p`, then for any prime number `q` different from `p`, it is not zero in `R`. -/ lemma cast_ne_zero_of_ne_of_prime [Nontrivial R] {p q : ℕ} [CharP R p] (hq : q.Prime) (hneq : p ≠ q) : (q : R) ≠ 0 := fun h ↦ by rw [cast_eq_zero_iff R p q] at h rcases hq.eq_one_or_self_of_dvd _ h with h | h · subst h exact false_of_nontrivial_of_char_one (R := R) · exact hneq h lemma ringChar_of_prime_eq_zero [Nontrivial R] {p : ℕ} (hprime : Nat.Prime p) (hp0 : (p : R) = 0) : ringChar R = p := Or.resolve_left ((Nat.dvd_prime hprime).1 (ringChar.dvd hp0)) ringChar_ne_one lemma charP_iff_prime_eq_zero [Nontrivial R] {p : ℕ} (hp : p.Prime) : CharP R p ↔ (p : R) = 0 := ⟨fun _ => cast_eq_zero R p, fun hp0 => (ringChar_of_prime_eq_zero hp hp0) ▸ inferInstance⟩ end NonAssocSemiring end CharP section /-- We have `2 ≠ 0` in a nontrivial ring whose characteristic is not `2`. -/ protected lemma Ring.two_ne_zero {R : Type*} [NonAssocSemiring R] [Nontrivial R] (hR : ringChar R ≠ 2) : (2 : R) ≠ 0 := by rw [Ne, (by norm_cast : (2 : R) = (2 : ℕ)), ringChar.spec, Nat.dvd_prime Nat.prime_two] exact mt (or_iff_left hR).mp CharP.ringChar_ne_one -- We have `CharP.neg_one_ne_one`, which assumes `[Ring R] (p : ℕ) [CharP R p] [Fact (2 < p)]`. -- This is a version using `ringChar` instead. /-- Characteristic `≠ 2` and nontrivial implies that `-1 ≠ 1`. -/ lemma Ring.neg_one_ne_one_of_char_ne_two {R : Type*} [NonAssocRing R] [Nontrivial R] (hR : ringChar R ≠ 2) : (-1 : R) ≠ 1 := fun h => Ring.two_ne_zero hR (one_add_one_eq_two (R := R) ▸ neg_eq_iff_add_eq_zero.mp h) /-- Characteristic `≠ 2` in a domain implies that `-a = a` iff `a = 0`. -/ lemma Ring.eq_self_iff_eq_zero_of_char_ne_two {R : Type*} [NonAssocRing R] [Nontrivial R] [NoZeroDivisors R] (hR : ringChar R ≠ 2) {a : R} : -a = a ↔ a = 0 := ⟨fun h => (mul_eq_zero.mp <| (two_mul a).trans <| neg_eq_iff_add_eq_zero.mp h).resolve_left (Ring.two_ne_zero hR), fun h => ((congr_arg (fun x => -x) h).trans neg_zero).trans h.symm⟩ end section Prod variable (S : Type*) [AddMonoidWithOne R] [AddMonoidWithOne S] (p q : ℕ) [CharP R p] /-- The characteristic of the product of rings is the least common multiple of the characteristics of the two rings. -/ instance Nat.lcm.charP [CharP S q] : CharP (R × S) (Nat.lcm p q) where cast_eq_zero_iff := by simp [Prod.ext_iff, CharP.cast_eq_zero_iff R p, CharP.cast_eq_zero_iff S q, Nat.lcm_dvd_iff] /-- The characteristic of the product of two rings of the same characteristic is the same as the characteristic of the rings -/ instance Prod.charP [CharP S p] : CharP (R × S) p := by convert Nat.lcm.charP R S p p; simp instance Prod.charZero_of_left [CharZero R] : CharZero (R × S) where cast_injective _ _ h := CharZero.cast_injective congr(Prod.fst $h) instance Prod.charZero_of_right [CharZero S] : CharZero (R × S) where cast_injective _ _ h := CharZero.cast_injective congr(Prod.snd $h) end Prod instance ULift.charP [AddMonoidWithOne R] (p : ℕ) [CharP R p] : CharP (ULift R) p where cast_eq_zero_iff n := Iff.trans ULift.ext_iff <| CharP.cast_eq_zero_iff R p n instance MulOpposite.charP [AddMonoidWithOne R] (p : ℕ) [CharP R p] : CharP Rᵐᵒᵖ p where cast_eq_zero_iff n := MulOpposite.unop_inj.symm.trans <| CharP.cast_eq_zero_iff R p n section /-- If two integers from `{0, 1, -1}` result in equal elements in a ring `R` that is nontrivial and of characteristic not `2`, then they are equal. -/ lemma Int.cast_injOn_of_ringChar_ne_two {R : Type*} [NonAssocRing R] [Nontrivial R] (hR : ringChar R ≠ 2) : ({0, 1, -1} : Set ℤ).InjOn ((↑) : ℤ → R) := by rintro _ (rfl | rfl | rfl) _ (rfl | rfl | rfl) h <;> simp only [cast_neg, cast_one, cast_zero, neg_eq_zero, one_ne_zero, zero_ne_one, zero_eq_neg] at h ⊢ · exact ((Ring.neg_one_ne_one_of_char_ne_two hR).symm h).elim · exact ((Ring.neg_one_ne_one_of_char_ne_two hR) h).elim end namespace CharZero lemma charZero_iff_forall_prime_ne_zero [NonAssocRing R] [NoZeroDivisors R] [Nontrivial R] : CharZero R ↔ ∀ p : ℕ, p.Prime → (p : R) ≠ 0 := by refine ⟨fun h p hp => by simp [hp.ne_zero], fun h => ?_⟩ let p := ringChar R cases CharP.char_is_prime_or_zero R p with | inl hp => simpa using h p hp | inr h => have : CharP R 0 := h ▸ inferInstance; exact CharP.charP_to_charZero R end CharZero namespace Fin open Fin.NatCast /-- The characteristic of `F_p` is `p`. -/ @[stacks 09FS "First part. We don't require `p` to be a prime in mathlib."] instance charP (n : ℕ) [NeZero n] : CharP (Fin n) n where cast_eq_zero_iff _ := natCast_eq_zero end Fin section AddMonoidWithOne variable [AddMonoidWithOne R] instance (S : Type*) [Semiring S] (p) [ExpChar R p] [ExpChar S p] : ExpChar (R × S) p := by obtain hp | ⟨hp⟩ := ‹ExpChar R p› · constructor obtain _ | _ := ‹ExpChar S p› · exact (Nat.not_prime_one hp).elim · have := Prod.charP R S p; exact .prime hp end AddMonoidWithOne section CommRing instance (α : Type*) [Semiring α] [IsLeftCancelAdd α] (n : ℕ) [CharP α n] : Lean.Grind.IsCharP α n where ofNat_ext_iff {a b} := by rw [Lean.Grind.Semiring.ofNat_eq_natCast, Lean.Grind.Semiring.ofNat_eq_natCast] exact CharP.cast_eq_iff_mod_eq α n end CommRing
.lake/packages/mathlib/Mathlib/Algebra/CharP/Frobenius.lean
import Mathlib.Algebra.Algebra.Defs import Mathlib.Algebra.CharP.Lemmas /-! ### The Frobenius endomorphism ## Tags Frobenius endomorphism ## Implementation notes The definitions of `frobenius` and `iterateFrobenius` ring homomorphisms are in `Mathlib/Algebra/CharP/Lemmas.lean` as they are needed for some results that in turn are used in files forbidding to import algebra-related definitions (see `Mathlib/Algebra/CharP/Two.lean`). -/ section CommSemiring variable {R : Type*} [CommSemiring R] {S : Type*} [CommSemiring S] variable (f : R →* S) (g : R →+* S) (p m n : ℕ) [ExpChar R p] [ExpChar S p] (x y : R) lemma frobenius_def : frobenius R p x = x ^ p := rfl lemma iterateFrobenius_def : iterateFrobenius R p n x = x ^ p ^ n := rfl lemma iterate_frobenius : (frobenius R p)^[n] x = x ^ p ^ n := congr_fun (pow_iterate p n) x variable (R) lemma iterateFrobenius_eq_pow : iterateFrobenius R p n = frobenius R p ^ n := by ext; simp [iterateFrobenius_def, iterate_frobenius] lemma coe_iterateFrobenius : iterateFrobenius R p n = (frobenius R p)^[n] := (pow_iterate p n).symm lemma iterateFrobenius_one_apply : iterateFrobenius R p 1 x = x ^ p := by rw [iterateFrobenius_def, pow_one] @[simp] lemma iterateFrobenius_one : iterateFrobenius R p 1 = frobenius R p := RingHom.ext (iterateFrobenius_one_apply R p) lemma iterateFrobenius_zero_apply : iterateFrobenius R p 0 x = x := by rw [iterateFrobenius_def, pow_zero, pow_one] @[simp] lemma iterateFrobenius_zero : iterateFrobenius R p 0 = RingHom.id R := RingHom.ext (iterateFrobenius_zero_apply R p) lemma iterateFrobenius_add_apply : iterateFrobenius R p (m + n) x = iterateFrobenius R p m (iterateFrobenius R p n x) := by simp_rw [iterateFrobenius_def, add_comm m n, pow_add, pow_mul] lemma iterateFrobenius_add : iterateFrobenius R p (m + n) = (iterateFrobenius R p m).comp (iterateFrobenius R p n) := RingHom.ext (iterateFrobenius_add_apply R p m n) lemma iterateFrobenius_mul_apply : iterateFrobenius R p (m * n) x = (iterateFrobenius R p m)^[n] x := by simp_rw [coe_iterateFrobenius, Function.iterate_mul] lemma coe_iterateFrobenius_mul : iterateFrobenius R p (m * n) = (iterateFrobenius R p m)^[n] := funext (iterateFrobenius_mul_apply R p m n) variable {R} @[deprecated map_mul (since := "2025-10-28")] lemma frobenius_mul : frobenius R p (x * y) = frobenius R p x * frobenius R p y := map_mul (frobenius R p) x y @[deprecated map_one (since := "2025-10-28")] lemma frobenius_one : frobenius R p 1 = 1 := one_pow _ lemma MonoidHom.map_frobenius : f (frobenius R p x) = frobenius S p (f x) := map_pow f x p lemma RingHom.map_frobenius : g (frobenius R p x) = frobenius S p (g x) := map_pow g x p lemma MonoidHom.map_iterate_frobenius (n : ℕ) : f ((frobenius R p)^[n] x) = (frobenius S p)^[n] (f x) := Function.Semiconj.iterate_right (f.map_frobenius p) n x lemma MonoidHom.map_iterateFrobenius (n : ℕ) : f (iterateFrobenius R p n x) = iterateFrobenius S p n (f x) := by simp [iterateFrobenius_def] lemma RingHom.map_iterate_frobenius (n : ℕ) : g ((frobenius R p)^[n] x) = (frobenius S p)^[n] (g x) := g.toMonoidHom.map_iterate_frobenius p x n lemma RingHom.map_iterateFrobenius (n : ℕ) : g (iterateFrobenius R p n x) = iterateFrobenius S p n (g x) := g.toMonoidHom.map_iterateFrobenius p x n lemma MonoidHom.iterate_map_frobenius (f : R →* R) (p : ℕ) [ExpChar R p] (n : ℕ) : f^[n] (frobenius R p x) = frobenius R p (f^[n] x) := iterate_map_pow f _ _ _ lemma RingHom.iterate_map_frobenius (f : R →+* R) (p : ℕ) [ExpChar R p] (n : ℕ) : f^[n] (frobenius R p x) = frobenius R p (f^[n] x) := iterate_map_pow f _ _ _ /-- The Frobenius endomorphism commutes with any ring homomorphism. -/ lemma RingHom.frobenius_comm : g.comp (frobenius R p) = (frobenius S p).comp g := ext <| map_frobenius g p /-- The iterated Frobenius endomorphism commutes with any ring homomorphism. -/ lemma RingHom.iterateFrobenius_comm (n : ℕ) : g.comp (iterateFrobenius R p n) = (iterateFrobenius S p n).comp g := ext fun x ↦ map_iterateFrobenius g p x n variable (R S) /-- The Frobenius map of an algebra as a Frobenius-semilinear map. -/ nonrec def LinearMap.frobenius [Algebra R S] : S →ₛₗ[frobenius R p] S where __ := frobenius S p map_smul' r s := show frobenius S p _ = _ by simp_rw [Algebra.smul_def, map_mul, ← (algebraMap R S).map_frobenius]; rfl /-- The iterated Frobenius map of an algebra as an iterated-Frobenius-semilinear map. -/ nonrec def LinearMap.iterateFrobenius [Algebra R S] : S →ₛₗ[iterateFrobenius R p n] S where __ := iterateFrobenius S p n map_smul' f s := show iterateFrobenius S p n _ = _ by simp_rw [iterateFrobenius_def, Algebra.smul_def, mul_pow, ← map_pow]; rfl theorem LinearMap.frobenius_def [Algebra R S] (x : S) : frobenius R S p x = x ^ p := rfl theorem LinearMap.iterateFrobenius_def [Algebra R S] (n : ℕ) (x : S) : iterateFrobenius R S p n x = x ^ p ^ n := rfl @[deprecated map_zero (since := "2025-10-28")] theorem frobenius_zero : frobenius R p 0 = 0 := (frobenius R p).map_zero @[deprecated map_add (since := "2025-10-28")] theorem frobenius_add : frobenius R p (x + y) = frobenius R p x + frobenius R p y := (frobenius R p).map_add x y @[deprecated map_natCast (since := "2025-10-28")] theorem frobenius_natCast (n : ℕ) : frobenius R p n = n := map_natCast (frobenius R p) n end CommSemiring section CommRing variable {R : Type*} [CommRing R] (p : ℕ) [ExpChar R p] (x y : R) @[deprecated map_neg (since := "2025-10-28")] lemma frobenius_neg : frobenius R p (-x) = -frobenius R p x := map_neg .. @[deprecated map_sub (since := "2025-10-28")] lemma frobenius_sub : frobenius R p (x - y) = frobenius R p x - frobenius R p y := map_sub .. end CommRing
.lake/packages/mathlib/Mathlib/Algebra/CharP/Quotient.lean
import Mathlib.GroupTheory.OrderOfElement import Mathlib.RingTheory.Ideal.Maps import Mathlib.RingTheory.Ideal.Nonunits import Mathlib.RingTheory.Ideal.Quotient.Defs /-! # Characteristic of quotient rings -/ theorem CharP.ker_intAlgebraMap_eq_span {R : Type*} [Ring R] (p : ℕ) [CharP R p] : RingHom.ker (algebraMap ℤ R) = Ideal.span {(p : ℤ)} := by ext a simp [CharP.intCast_eq_zero_iff R p, Ideal.mem_span_singleton] variable {R : Type*} [CommRing R] namespace CharP variable (R) in theorem quotient (p : ℕ) [hp1 : Fact p.Prime] (hp2 : ↑p ∈ nonunits R) : CharP (R ⧸ (Ideal.span ({(p : R)} : Set R) : Ideal R)) p := have hp0 : (p : R ⧸ (Ideal.span {(p : R)} : Ideal R)) = 0 := map_natCast (Ideal.Quotient.mk (Ideal.span {(p : R)} : Ideal R)) p ▸ Ideal.Quotient.eq_zero_iff_mem.2 (Ideal.subset_span <| Set.mem_singleton _) ringChar.of_eq <| Or.resolve_left ((Nat.dvd_prime hp1.1).1 <| ringChar.dvd hp0) fun h1 => hp2 <| isUnit_iff_dvd_one.2 <| Ideal.mem_span_singleton.1 <| Ideal.Quotient.eq_zero_iff_mem.1 <| @Subsingleton.elim _ (@CharOne.subsingleton _ _ (ringChar.of_eq h1)) _ _ /-- If an ideal does not contain any coercions of natural numbers other than zero, then its quotient inherits the characteristic of the underlying ring. -/ theorem quotient' (p : ℕ) [CharP R p] (I : Ideal R) (h : ∀ x : ℕ, (x : R) ∈ I → (x : R) = 0) : CharP (R ⧸ I) p where cast_eq_zero_iff x := by rw [← cast_eq_zero_iff R p x, ← map_natCast (Ideal.Quotient.mk I)] refine Ideal.Quotient.eq.trans (?_ : ↑x - 0 ∈ I ↔ _) rw [sub_zero] exact ⟨h x, fun h' => h'.symm ▸ I.zero_mem⟩ /-- `CharP.quotient'` as an `Iff`. -/ theorem quotient_iff (n : ℕ) [CharP R n] (I : Ideal R) : CharP (R ⧸ I) n ↔ ∀ x : ℕ, ↑x ∈ I → (x : R) = 0 := by refine ⟨fun _ x hx => ?_, CharP.quotient' n I⟩ rw [CharP.cast_eq_zero_iff R n, ← CharP.cast_eq_zero_iff (R ⧸ I) n _] exact (Submodule.Quotient.mk_eq_zero I).mpr hx /-- `CharP.quotient_iff`, but stated in terms of inclusions of ideals. -/ theorem quotient_iff_le_ker_natCast (n : ℕ) [CharP R n] (I : Ideal R) : CharP (R ⧸ I) n ↔ I.comap (Nat.castRingHom R) ≤ RingHom.ker (Nat.castRingHom R) := by rw [CharP.quotient_iff, RingHom.ker_eq_comap_bot]; rfl end CharP lemma Ideal.natCast_mem_of_charP_quotient (p : ℕ) (I : Ideal R) [CharP (R ⧸ I) p] : (p : R) ∈ I := Ideal.Quotient.eq_zero_iff_mem.mp <| by simp theorem Ideal.Quotient.index_eq_zero (I : Ideal R) : (↑I.toAddSubgroup.index : R ⧸ I) = 0 := by rw [AddSubgroup.index, Nat.card_eq] split_ifs with hq; swap · simp letI : Fintype (R ⧸ I) := @Fintype.ofFinite _ hq exact Nat.cast_card_eq_zero (R ⧸ I)
.lake/packages/mathlib/Mathlib/Algebra/CharP/Defs.lean
import Mathlib.Data.Nat.Cast.Basic import Mathlib.Data.Nat.Find import Mathlib.Data.Nat.Prime.Defs import Mathlib.Data.Int.Cast.Basic import Mathlib.Order.Lattice /-! # Characteristic of semirings ## Main definitions * `CharP R p` expresses that the ring (additive monoid with one) `R` has characteristic `p` * `ringChar`: the characteristic of a ring * `ExpChar R p` expresses that the ring (additive monoid with one) `R` has exponential characteristic `p` (which is `1` if `R` has characteristic 0, and `p` if it has prime characteristic `p`) -/ assert_not_exists Field Finset OrderHom variable (R : Type*) namespace CharP section AddMonoidWithOne variable [AddMonoidWithOne R] (p : ℕ) /-- The generator of the kernel of the unique homomorphism ℕ → R for a semiring R. *Warning*: for a semiring `R`, `CharP R 0` and `CharZero R` need not coincide. * `CharP R 0` asks that only `0 : ℕ` maps to `0 : R` under the map `ℕ → R`; * `CharZero R` requires an injection `ℕ ↪ R`. For instance, endowing `{0, 1}` with addition given by `max` (i.e. `1` is absorbing), shows that `CharZero {0, 1}` does not hold and yet `CharP {0, 1} 0` does. This example is formalized in `Counterexamples/CharPZeroNeCharZero.lean`. -/ @[mk_iff] class _root_.CharP (R : Type*) [AddMonoidWithOne R] (p : outParam ℕ) : Prop where cast_eq_zero_iff (R p) : ∀ x : ℕ, (x : R) = 0 ↔ p ∣ x variable [CharP R p] {a b : ℕ} lemma _root_.CharP.ofNat_eq_zero' (p : ℕ) [CharP R p] (a : ℕ) [a.AtLeastTwo] (h : p ∣ a) : (ofNat(a) : R) = 0 := by rwa [← CharP.cast_eq_zero_iff R p] at h variable {R} in lemma congr {q : ℕ} (h : p = q) : CharP R q := h ▸ ‹CharP R p› @[simp] lemma cast_eq_zero : (p : R) = 0 := (cast_eq_zero_iff R p p).2 dvd_rfl lemma cast_eq_mod (k : ℕ) : (k : R) = (k % p : ℕ) := have (a : ℕ) : ((p * a : ℕ) : R) = 0 := by rw [CharP.cast_eq_zero_iff R p] exact Nat.dvd_mul_right p a calc (k : R) = ↑(k % p + p * (k / p)) := by rw [Nat.mod_add_div] _ = ↑(k % p) := by simp [this] lemma cast_eq_iff_mod_eq [IsLeftCancelAdd R] : (a : R) = (b : R) ↔ a % p = b % p := by wlog hle : a ≤ b · simpa only [eq_comm] using (this _ _ (lt_of_not_ge hle).le) obtain ⟨c, rfl⟩ := Nat.exists_eq_add_of_le hle rw [Nat.cast_add, left_eq_add, CharP.cast_eq_zero_iff R p] constructor · simp +contextual [Nat.add_mod, Nat.dvd_iff_mod_eq_zero] intro h have := Nat.sub_mod_eq_zero_of_mod_eq h.symm simpa [Nat.dvd_iff_mod_eq_zero] using this -- TODO: This lemma needs to be `@[simp]` for confluence in the presence of `CharP.cast_eq_zero` and -- `Nat.cast_ofNat`, but with `no_index` on its entire LHS, it matches literally every expression so -- is too expensive. If https://github.com/leanprover/lean4/issues/2867 is fixed in a performant way, this can be made `@[simp]`. -- -- @[simp] lemma ofNat_eq_zero [p.AtLeastTwo] : (ofNat(p) : R) = 0 := cast_eq_zero R p lemma eq {p q : ℕ} (hp : CharP R p) (hq : CharP R q) : p = q := Nat.dvd_antisymm ((cast_eq_zero_iff (self := hp) R p q).1 (@cast_eq_zero _ _ _ hq)) ((cast_eq_zero_iff (self := hq) R q p).1 (@cast_eq_zero _ _ _ hp)) instance ofCharZero [CharZero R] : CharP R 0 where cast_eq_zero_iff x := by rw [zero_dvd_iff, ← Nat.cast_zero, Nat.cast_inj] end AddMonoidWithOne section AddGroupWithOne variable [AddGroupWithOne R] (p : ℕ) [CharP R p] {a b : ℤ} lemma intCast_eq_zero_iff (a : ℤ) : (a : R) = 0 ↔ (p : ℤ) ∣ a := by rcases lt_trichotomy a 0 with (h | rfl | h) · rw [← neg_eq_zero, ← Int.cast_neg, ← Int.dvd_neg] lift -a to ℕ using Int.neg_nonneg.mpr (le_of_lt h) with b rw [Int.cast_natCast, CharP.cast_eq_zero_iff R p, Int.natCast_dvd_natCast] · simp only [Int.cast_zero, Int.dvd_zero] · lift a to ℕ using le_of_lt h with b rw [Int.cast_natCast, CharP.cast_eq_zero_iff R p, Int.natCast_dvd_natCast] lemma charP_to_charZero [CharP R 0] : CharZero R := charZero_of_inj_zero fun n h0 => eq_zero_of_zero_dvd ((cast_eq_zero_iff R 0 n).mp h0) lemma charP_zero_iff_charZero : CharP R 0 ↔ CharZero R := ⟨fun _ ↦ charP_to_charZero R, fun _ ↦ ofCharZero R⟩ end AddGroupWithOne section NonAssocSemiring variable [NonAssocSemiring R] lemma «exists» : ∃ p, CharP R p := letI := Classical.decEq R by_cases (fun H : ∀ p : ℕ, (p : R) = 0 → p = 0 => ⟨0, ⟨fun x => by rw [zero_dvd_iff]; exact ⟨H x, by rintro rfl; simp⟩⟩⟩) fun H => ⟨Nat.find (not_forall.1 H), ⟨fun x => ⟨fun H1 => Nat.dvd_of_mod_eq_zero (by_contradiction fun H2 => Nat.find_min (not_forall.1 H) (Nat.mod_lt x <| Nat.pos_of_ne_zero <| not_of_not_imp <| Nat.find_spec (not_forall.1 H)) (not_imp_of_and_not ⟨by rwa [← Nat.mod_add_div x (Nat.find (not_forall.1 H)), Nat.cast_add, Nat.cast_mul, of_not_not (not_not_of_not_imp <| Nat.find_spec (not_forall.1 H)), zero_mul, add_zero] at H1, H2⟩)), fun H1 => by rw [← Nat.mul_div_cancel' H1, Nat.cast_mul, of_not_not (not_not_of_not_imp <| Nat.find_spec (not_forall.1 H)), zero_mul]⟩⟩⟩ lemma existsUnique : ∃! p, CharP R p := let ⟨c, H⟩ := CharP.exists R ⟨c, H, fun _y H2 => CharP.eq R H2 H⟩ end NonAssocSemiring end CharP /-- Noncomputable function that outputs the unique characteristic of a semiring. -/ noncomputable def ringChar [NonAssocSemiring R] : ℕ := Classical.choose (CharP.existsUnique R) namespace ringChar variable [NonAssocSemiring R] lemma spec : ∀ x : ℕ, (x : R) = 0 ↔ ringChar R ∣ x := by letI : CharP R (ringChar R) := (Classical.choose_spec (CharP.existsUnique R)).1 exact CharP.cast_eq_zero_iff R (ringChar R) lemma eq (p : ℕ) [C : CharP R p] : ringChar R = p := ((Classical.choose_spec (CharP.existsUnique R)).2 p C).symm instance (priority := low) charP : CharP R (ringChar R) := ⟨spec R⟩ variable {R} lemma of_eq {p : ℕ} (h : ringChar R = p) : CharP R p := CharP.congr (ringChar R) h lemma eq_iff {p : ℕ} : ringChar R = p ↔ CharP R p := ⟨of_eq, @eq R _ p⟩ lemma dvd {x : ℕ} (hx : (x : R) = 0) : ringChar R ∣ x := (spec R x).1 hx @[simp] lemma eq_zero [CharZero R] : ringChar R = 0 := eq R 0 lemma Nat.cast_ringChar : (ringChar R : R) = 0 := by rw [ringChar.spec] @[simp] lemma ringChar_eq_one : ringChar R = 1 ↔ Subsingleton R := by rw [← Nat.dvd_one, ← spec, eq_comm, Nat.cast_one, subsingleton_iff_zero_eq_one] @[nontriviality] lemma ringChar_subsingleton [Subsingleton R] : ringChar R = 1 := by simpa end ringChar lemma CharP.neg_one_ne_one [AddGroupWithOne R] (p : ℕ) [CharP R p] [Fact (2 < p)] : (-1 : R) ≠ (1 : R) := by rw [ne_comm, ← sub_ne_zero, sub_neg_eq_add, one_add_one_eq_two, ← Nat.cast_two, Ne, CharP.cast_eq_zero_iff R p 2] exact fun h ↦ (Fact.out : 2 < p).not_ge <| Nat.le_of_dvd Nat.zero_lt_two h namespace CharP section variable [NonAssocRing R] lemma ringChar_zero_iff_CharZero : ringChar R = 0 ↔ CharZero R := by rw [ringChar.eq_iff, charP_zero_iff_charZero] end section Semiring variable [NonAssocSemiring R] lemma char_ne_one [Nontrivial R] (p : ℕ) [hc : CharP R p] : p ≠ 1 := fun hp : p = 1 => have : (1 : R) = 0 := by simpa using (cast_eq_zero_iff R p 1).mpr (hp ▸ dvd_refl p) absurd this one_ne_zero section NoZeroDivisors variable [NoZeroDivisors R] lemma char_is_prime_of_two_le (p : ℕ) [CharP R p] (hp : 2 ≤ p) : Nat.Prime p := suffices ∀ (d) (_ : d ∣ p), d = 1 ∨ d = p from Nat.prime_def.mpr ⟨hp, this⟩ fun (d : ℕ) (hdvd : ∃ e, p = d * e) => let ⟨e, hmul⟩ := hdvd have : (p : R) = 0 := (cast_eq_zero_iff R p p).mpr (dvd_refl p) have : (d : R) * e = 0 := @Nat.cast_mul R _ d e ▸ hmul ▸ this Or.elim (eq_zero_or_eq_zero_of_mul_eq_zero this) (fun hd : (d : R) = 0 => have : p ∣ d := (cast_eq_zero_iff R p d).mp hd show d = 1 ∨ d = p from Or.inr (this.antisymm' ⟨e, hmul⟩)) fun he : (e : R) = 0 => have : p ∣ e := (cast_eq_zero_iff R p e).mp he have : e ∣ p := dvd_of_mul_left_eq d (Eq.symm hmul) have : e = p := ‹e ∣ p›.antisymm ‹p ∣ e› have h₀ : 0 < p := by grind have : d * p = 1 * p := by grind show d = 1 ∨ d = p from Or.inl (mul_right_cancel₀ h₀.ne' this) section Nontrivial variable [Nontrivial R] lemma char_is_prime_or_zero (p : ℕ) [hc : CharP R p] : Nat.Prime p ∨ p = 0 := match p, hc with | 0, _ => Or.inr rfl | 1, hc => absurd (Eq.refl (1 : ℕ)) (@char_ne_one R _ _ (1 : ℕ) hc) | m + 2, hc => Or.inl (@char_is_prime_of_two_le R _ _ (m + 2) hc (Nat.le_add_left 2 m)) /-- The characteristic is prime if it is non-zero. -/ lemma char_prime_of_ne_zero {p : ℕ} [CharP R p] (hp : p ≠ 0) : p.Prime := (CharP.char_is_prime_or_zero R p).resolve_right hp lemma exists' (R : Type*) [NonAssocRing R] [NoZeroDivisors R] [Nontrivial R] : CharZero R ∨ ∃ p : ℕ, Fact p.Prime ∧ CharP R p := by obtain ⟨p, hchar⟩ := CharP.exists R rcases char_is_prime_or_zero R p with h | rfl exacts [Or.inr ⟨p, Fact.mk h, hchar⟩, Or.inl (charP_to_charZero R)] lemma char_is_prime_of_pos (p : ℕ) [NeZero p] [CharP R p] : Fact p.Prime := ⟨(CharP.char_is_prime_or_zero R _).resolve_right <| NeZero.ne p⟩ end Nontrivial end NoZeroDivisors end Semiring section NonAssocSemiring variable {R} [NonAssocSemiring R] -- This lemma is not an instance, to make sure that trying to prove `α` is a subsingleton does -- not try to find a ring structure on `α`, which can be expensive. lemma CharOne.subsingleton [CharP R 1] : Subsingleton R := Subsingleton.intro <| suffices ∀ r : R, r = 0 from fun a b => show a = b by rw [this a, this b] fun r => calc r = 1 * r := by rw [one_mul] _ = (1 : ℕ) * r := by rw [Nat.cast_one] _ = 0 * r := by rw [CharP.cast_eq_zero] _ = 0 := by rw [zero_mul] lemma false_of_nontrivial_of_char_one [Nontrivial R] [CharP R 1] : False := by have : Subsingleton R := CharOne.subsingleton exact false_of_nontrivial_of_subsingleton R lemma ringChar_ne_one [Nontrivial R] : ringChar R ≠ 1 := by intro h apply zero_ne_one' R symm rw [← Nat.cast_one, ringChar.spec, h] lemma nontrivial_of_char_ne_one {v : ℕ} (hv : v ≠ 1) [hr : CharP R v] : Nontrivial R := ⟨⟨(1 : ℕ), 0, fun h => hv <| by rwa [CharP.cast_eq_zero_iff _ v, Nat.dvd_one] at h⟩⟩ end NonAssocSemiring end CharP namespace NeZero variable [AddMonoidWithOne R] {r : R} {n p : ℕ} lemma of_not_dvd [CharP R p] (h : ¬p ∣ n) : NeZero (n : R) := ⟨(CharP.cast_eq_zero_iff R p n).not.mpr h⟩ lemma not_char_dvd (p : ℕ) [CharP R p] (k : ℕ) [h : NeZero (k : R)] : ¬p ∣ k := by rwa [← CharP.cast_eq_zero_iff R p k, ← Ne, ← neZero_iff] end NeZero /-! ### Exponential characteristic This section defines the exponential characteristic, which is defined to be 1 for a ring with characteristic 0 and the same as the ordinary characteristic, if the ordinary characteristic is prime. This concept is useful to simplify some theorem statements. This file establishes a few basic results relating it to the (ordinary characteristic). The definition is stated for a semiring, but the actual results are for nontrivial rings (as far as exponential characteristic one is concerned), respectively a ring without zero-divisors (for prime characteristic). -/ section AddMonoidWithOne variable [AddMonoidWithOne R] /-- The definition of the exponential characteristic of a semiring. -/ class inductive ExpChar : ℕ → Prop | zero [CharZero R] : ExpChar 1 | prime {q : ℕ} (hprime : q.Prime) [hchar : CharP R q] : ExpChar q instance expChar_prime (p) [CharP R p] [Fact p.Prime] : ExpChar R p := ExpChar.prime Fact.out instance expChar_one [CharZero R] : ExpChar R 1 := ExpChar.zero lemma expChar_ne_zero (p : ℕ) [hR : ExpChar R p] : p ≠ 0 := by cases hR · exact one_ne_zero · exact ‹p.Prime›.ne_zero variable {R} in /-- The exponential characteristic is unique. -/ lemma ExpChar.eq {p q : ℕ} (hp : ExpChar R p) (hq : ExpChar R q) : p = q := by rcases hp with ⟨hp⟩ | ⟨hp'⟩ · rcases hq with hq | hq' exacts [rfl, False.elim (Nat.not_prime_zero (CharP.eq R ‹_› (CharP.ofCharZero R) ▸ hq'))] · rcases hq with hq | hq' exacts [False.elim (Nat.not_prime_zero (CharP.eq R ‹_› (CharP.ofCharZero R) ▸ hp')), CharP.eq R ‹_› ‹_›] lemma ExpChar.congr {p : ℕ} (q : ℕ) [hq : ExpChar R q] (h : q = p) : ExpChar R p := h ▸ hq /-- The exponential characteristic is one if the characteristic is zero. -/ lemma expChar_one_of_char_zero (q : ℕ) [hp : CharP R 0] [hq : ExpChar R q] : q = 1 := by rcases hq with q | hq_prime · rfl · exact False.elim <| hq_prime.ne_zero <| ‹CharP R q›.eq R hp /-- The characteristic equals the exponential characteristic iff the former is prime. -/ lemma char_eq_expChar_iff (p q : ℕ) [hp : CharP R p] [hq : ExpChar R q] : p = q ↔ p.Prime := by rcases hq with q | hq_prime · rw [(CharP.eq R hp (.ofCharZero R) : p = 0)] decide · exact ⟨fun hpq => hpq.symm ▸ hq_prime, fun _ => CharP.eq R hp ‹CharP R q›⟩ /-- The exponential characteristic is a prime number or one. See also `CharP.char_is_prime_or_zero`. -/ lemma expChar_is_prime_or_one (q : ℕ) [hq : ExpChar R q] : Nat.Prime q ∨ q = 1 := by cases hq with | zero => exact .inr rfl | prime hp => exact .inl hp /-- The exponential characteristic is positive. -/ lemma expChar_pos (q : ℕ) [ExpChar R q] : 0 < q := by rcases expChar_is_prime_or_one R q with h | rfl exacts [Nat.Prime.pos h, Nat.one_pos] /-- Any power of the exponential characteristic is positive. -/ lemma expChar_pow_pos (q : ℕ) [ExpChar R q] (n : ℕ) : 0 < q ^ n := Nat.pow_pos (expChar_pos R q) end AddMonoidWithOne section NonAssocSemiring variable [NonAssocSemiring R] /-- Noncomputable function that outputs the unique exponential characteristic of a semiring. -/ noncomputable def ringExpChar : ℕ := max (ringChar R) 1 lemma ringExpChar.eq (q : ℕ) [h : ExpChar R q] : ringExpChar R = q := by rcases h with _ | h · haveI := CharP.ofCharZero R rw [ringExpChar, ringChar.eq R 0]; rfl rw [ringExpChar, ringChar.eq R q] exact Nat.max_eq_left h.one_lt.le @[simp] lemma ringExpChar.eq_one [CharZero R] : ringExpChar R = 1 := by rw [ringExpChar, ringChar.eq_zero, max_eq_right (Nat.zero_le _)] section Nontrivial variable [Nontrivial R] /-- The exponential characteristic is one if the characteristic is zero. -/ lemma char_zero_of_expChar_one (p : ℕ) [hp : CharP R p] [hq : ExpChar R 1] : p = 0 := by cases hq · exact CharP.eq R hp (.ofCharZero R) · exact False.elim (CharP.char_ne_one R 1 rfl) -- This could be an instance, but there are no `ExpChar R 1` instances in mathlib. /-- The characteristic is zero if the exponential characteristic is one. -/ lemma charZero_of_expChar_one' [hq : ExpChar R 1] : CharZero R := by cases hq · assumption · exact False.elim (CharP.char_ne_one R 1 rfl) /-- The exponential characteristic is one iff the characteristic is zero. -/ lemma expChar_one_iff_char_zero (p q : ℕ) [CharP R p] [ExpChar R q] : q = 1 ↔ p = 0 := by constructor · rintro rfl exact char_zero_of_expChar_one R p · rintro rfl exact expChar_one_of_char_zero R q end Nontrivial end NonAssocSemiring lemma ExpChar.exists [Ring R] [IsDomain R] : ∃ q, ExpChar R q := by obtain _ | ⟨p, ⟨hp⟩, _⟩ := CharP.exists' R exacts [⟨1, .zero⟩, ⟨p, .prime hp⟩] lemma ExpChar.exists_unique [Ring R] [IsDomain R] : ∃! q, ExpChar R q := let ⟨q, H⟩ := ExpChar.exists R ⟨q, H, fun _ H2 ↦ ExpChar.eq H2 H⟩ instance ringExpChar.expChar [Ring R] [IsDomain R] : ExpChar R (ringExpChar R) := by obtain ⟨q, _⟩ := ExpChar.exists R rwa [ringExpChar.eq R q] variable {R} in lemma ringExpChar.of_eq [Ring R] [IsDomain R] {q : ℕ} (h : ringExpChar R = q) : ExpChar R q := h ▸ ringExpChar.expChar R variable {R} in lemma ringExpChar.eq_iff [Ring R] [IsDomain R] {q : ℕ} : ringExpChar R = q ↔ ExpChar R q := ⟨ringExpChar.of_eq, fun _ ↦ ringExpChar.eq R q⟩
.lake/packages/mathlib/Mathlib/Algebra/CharP/Algebra.lean
import Mathlib.Algebra.CharP.Defs import Mathlib.Algebra.FreeAlgebra import Mathlib.RingTheory.Localization.FractionRing import Mathlib.RingTheory.SimpleRing.Basic /-! # Characteristics of algebras In this file we describe the characteristic of `R`-algebras. In particular we are interested in the characteristic of free algebras over `R` and the fraction field `FractionRing R`. ## Main results - `charP_of_injective_algebraMap` If `R →+* A` is an injective algebra map then `A` has the same characteristic as `R`. Instances constructed from this result: - Any `FreeAlgebra R X` has the same characteristic as `R`. - The `FractionRing R` of an integral domain `R` has the same characteristic as `R`. -/ /-- Given `R →+* A`, then `char A ∣ char R`. -/ theorem CharP.dvd_of_ringHom {R A : Type*} [NonAssocSemiring R] [NonAssocSemiring A] (f : R →+* A) (p q : ℕ) [CharP R p] [CharP A q] : q ∣ p := by refine (CharP.cast_eq_zero_iff A q p).mp ?_ rw [← map_natCast f p, CharP.cast_eq_zero, map_zero] /-- Given `R →+* A`, where `R` is a domain with `char R > 0`, then `char A = char R`. -/ theorem CharP.of_ringHom_of_ne_zero {R A : Type*} [NonAssocSemiring R] [NoZeroDivisors R] [NonAssocSemiring A] [Nontrivial A] (f : R →+* A) (p : ℕ) (hp : p ≠ 0) [CharP R p] : CharP A p := by have := f.domain_nontrivial have H := (CharP.char_is_prime_or_zero R p).resolve_right hp obtain ⟨q, hq⟩ := CharP.exists A obtain ⟨k, e⟩ := dvd_of_ringHom f p q have := Nat.isUnit_iff.mp ((H.2 e).resolve_left (Nat.isUnit_iff.not.mpr (char_ne_one A q))) rw [this, mul_one] at e exact e ▸ hq /-- If a ring homomorphism `R →+* A` is injective then `A` has the same characteristic as `R`. -/ theorem charP_of_injective_ringHom {R A : Type*} [NonAssocSemiring R] [NonAssocSemiring A] {f : R →+* A} (h : Function.Injective f) (p : ℕ) [CharP R p] : CharP A p where cast_eq_zero_iff x := by rw [← CharP.cast_eq_zero_iff R p x, ← map_natCast f x, map_eq_zero_iff f h] /-- If the algebra map `R →+* A` is injective then `A` has the same characteristic as `R`. -/ theorem charP_of_injective_algebraMap {R A : Type*} [CommSemiring R] [Semiring A] [Algebra R A] (h : Function.Injective (algebraMap R A)) (p : ℕ) [CharP R p] : CharP A p := charP_of_injective_ringHom h p theorem charP_of_injective_algebraMap' (R : Type*) {A : Type*} [CommRing R] [Semiring A] [Algebra R A] [FaithfulSMul R A] (p : ℕ) [CharP R p] : CharP A p := charP_of_injective_ringHom (FaithfulSMul.algebraMap_injective R A) p /-- If a ring homomorphism `R →+* A` is injective and `R` has characteristic zero then so does `A`. -/ theorem charZero_of_injective_ringHom {R A : Type*} [NonAssocSemiring R] [NonAssocSemiring A] {f : R →+* A} (h : Function.Injective f) [CharZero R] : CharZero A where cast_injective _ _ _ := CharZero.cast_injective <| h <| by simpa only [map_natCast f] /-- If the algebra map `R →+* A` is injective and `R` has characteristic zero then so does `A`. -/ theorem charZero_of_injective_algebraMap {R A : Type*} [CommSemiring R] [Semiring A] [Algebra R A] (h : Function.Injective (algebraMap R A)) [CharZero R] : CharZero A := charZero_of_injective_ringHom h /-- If `R →+* A` is injective, and `A` is of characteristic `p`, then `R` is also of characteristic `p`. Similar to `RingHom.charZero`. -/ theorem RingHom.charP {R A : Type*} [NonAssocSemiring R] [NonAssocSemiring A] (f : R →+* A) (H : Function.Injective f) (p : ℕ) [CharP A p] : CharP R p := by obtain ⟨q, h⟩ := CharP.exists R exact CharP.eq _ (charP_of_injective_ringHom H q) ‹CharP A p› ▸ h /-- If `R →+* A` is injective, then `R` is of characteristic `p` if and only if `A` is also of characteristic `p`. Similar to `RingHom.charZero_iff`. -/ protected theorem RingHom.charP_iff {R A : Type*} [NonAssocSemiring R] [NonAssocSemiring A] (f : R →+* A) (H : Function.Injective f) (p : ℕ) : CharP R p ↔ CharP A p := ⟨fun _ ↦ charP_of_injective_ringHom H p, fun _ ↦ f.charP H p⟩ /-- If a ring homomorphism `R →+* A` is injective then `A` has the same exponential characteristic as `R`. -/ lemma expChar_of_injective_ringHom {R A : Type*} [NonAssocSemiring R] [NonAssocSemiring A] {f : R →+* A} (h : Function.Injective f) (q : ℕ) [hR : ExpChar R q] : ExpChar A q := by rcases hR with _ | hprime · haveI := charZero_of_injective_ringHom h; exact .zero haveI := charP_of_injective_ringHom h q; exact .prime hprime /-- If `R →+* A` is injective, and `A` is of exponential characteristic `p`, then `R` is also of exponential characteristic `p`. Similar to `RingHom.charZero`. -/ lemma RingHom.expChar {R A : Type*} [NonAssocSemiring R] [NonAssocSemiring A] (f : R →+* A) (H : Function.Injective f) (p : ℕ) [ExpChar A p] : ExpChar R p := by cases ‹ExpChar A p› with | zero => haveI := f.charZero; exact .zero | prime hp => haveI := f.charP H p; exact .prime hp /-- If `R →+* A` is injective, then `R` is of exponential characteristic `p` if and only if `A` is also of exponential characteristic `p`. Similar to `RingHom.charZero_iff`. -/ lemma RingHom.expChar_iff {R A : Type*} [NonAssocSemiring R] [NonAssocSemiring A] (f : R →+* A) (H : Function.Injective f) (p : ℕ) : ExpChar R p ↔ ExpChar A p := ⟨fun _ ↦ expChar_of_injective_ringHom H p, fun _ ↦ f.expChar H p⟩ /-- If the algebra map `R →+* A` is injective then `A` has the same exponential characteristic as `R`. -/ lemma expChar_of_injective_algebraMap {R A : Type*} [CommSemiring R] [Semiring A] [Algebra R A] (h : Function.Injective (algebraMap R A)) (q : ℕ) [ExpChar R q] : ExpChar A q := expChar_of_injective_ringHom h q theorem ExpChar.of_injective_algebraMap' (R : Type*) {A : Type*} [CommRing R] [CommRing A] [Algebra R A] [FaithfulSMul R A] (q : ℕ) [ExpChar R q] : ExpChar A q := expChar_of_injective_ringHom (FaithfulSMul.algebraMap_injective R A) q /-! As an application, a `ℚ`-algebra has characteristic zero. -/ -- `CharP.charP_to_charZero A _ (charP_of_injective_algebraMap h 0)` does not work -- here as it would require `Ring A`. section QAlgebra variable (R : Type*) [Nontrivial R] /-- A nontrivial `ℚ`-algebra has `CharP` equal to zero. This cannot be a (local) instance because it would immediately form a loop with the instance `DivisionRing.toRatAlgebra`. It's probably easier to go the other way: prove `CharZero R` and automatically receive an `Algebra ℚ R` instance. -/ theorem algebraRat.charP_zero [Semiring R] [Algebra ℚ R] : CharP R 0 := charP_of_injective_algebraMap (algebraMap ℚ R).injective 0 /-- A nontrivial `ℚ`-algebra has characteristic zero. This cannot be a (local) instance because it would immediately form a loop with the instance `DivisionRing.toRatAlgebra`. It's probably easier to go the other way: prove `CharZero R` and automatically receive an `Algebra ℚ R` instance. -/ theorem algebraRat.charZero [Ring R] [Algebra ℚ R] : CharZero R := @CharP.charP_to_charZero R _ (algebraRat.charP_zero R) end QAlgebra /-! An algebra over a field has the same characteristic as the field. -/ lemma RingHom.charP_iff_charP {K L : Type*} [DivisionRing K] [NonAssocSemiring L] [Nontrivial L] (f : K →+* L) (p : ℕ) : CharP K p ↔ CharP L p := by simp only [charP_iff, ← f.injective.eq_iff, map_natCast f, map_zero f] section variable (K L : Type*) [Field K] [CommSemiring L] [Nontrivial L] [Algebra K L] protected theorem Algebra.charP_iff (p : ℕ) : CharP K p ↔ CharP L p := (algebraMap K L).charP_iff_charP p theorem Algebra.ringChar_eq : ringChar K = ringChar L := by rw [ringChar.eq_iff, Algebra.charP_iff K L] apply ringChar.charP end namespace FreeAlgebra variable {R X : Type*} [CommSemiring R] (p : ℕ) /-- If `R` has characteristic `p`, then so does `FreeAlgebra R X`. -/ instance charP [CharP R p] : CharP (FreeAlgebra R X) p := charP_of_injective_algebraMap FreeAlgebra.algebraMap_leftInverse.injective p /-- If `R` has characteristic `0`, then so does `FreeAlgebra R X`. -/ instance charZero [CharZero R] : CharZero (FreeAlgebra R X) := charZero_of_injective_algebraMap FreeAlgebra.algebraMap_leftInverse.injective end FreeAlgebra namespace IsFractionRing variable (R : Type*) {K : Type*} [CommRing R] [Field K] [Algebra R K] [IsFractionRing R K] variable (p : ℕ) /-- If `R` has characteristic `p`, then so does Frac(R). -/ theorem charP_of_isFractionRing [CharP R p] : CharP K p := charP_of_injective_algebraMap (IsFractionRing.injective R K) p /-- If `R` has characteristic `0`, then so does Frac(R). -/ theorem charZero_of_isFractionRing [CharZero R] : CharZero K := @CharP.charP_to_charZero K _ (charP_of_isFractionRing R 0) variable [IsDomain R] /-- If `R` has characteristic `p`, then so does `FractionRing R`. -/ instance charP [CharP R p] : CharP (FractionRing R) p := charP_of_isFractionRing R p /-- If `R` has characteristic `0`, then so does `FractionRing R`. -/ instance charZero [CharZero R] : CharZero (FractionRing R) := charZero_of_isFractionRing R end IsFractionRing
.lake/packages/mathlib/Mathlib/Algebra/CharP/LinearMaps.lean
import Mathlib.Algebra.CharP.Algebra import Mathlib.Algebra.Module.Torsion.Basic /-! # Characteristic of the ring of linear Maps This file contains properties of the characteristic of the ring of linear maps. The characteristic of the ring of linear maps is determined by its base ring. ## Main Results - `Module.charP_end` : For a commutative semiring `R` and a `R`-module `M`, the characteristic of `R` is equal to the characteristic of the `R`-linear endomorphisms of `M` when `M` contains a non-torsion element `x`. ## Notation - `R` is a commutative semiring - `M` is a `R`-module ## Implementation Notes One can also deduce similar result via `charP_of_injective_ringHom` and `R → (M →ₗ[R] M) : r ↦ (fun (x : M) ↦ r • x)`. But this will require stronger condition compared to `Module.charP_end`. -/ namespace Module variable {R M : Type*} [CommSemiring R] [AddCommMonoid M] [Module R M] /-- For a commutative semiring `R` and a `R`-module `M`, if `M` contains an element `x` that is not torsion, then the characteristic of `R` is equal to the characteristic of the `R`-linear endomorphisms of `M`. -/ theorem charP_end {p : ℕ} [hchar : CharP R p] (htorsion : ∃ x : M, Ideal.torsionOf R M x = ⊥) : CharP (M →ₗ[R] M) p where cast_eq_zero_iff n := by have exact : (n : M →ₗ[R] M) = (n : R) • 1 := by simp only [Nat.cast_smul_eq_nsmul, nsmul_eq_mul, mul_one] rw [exact, LinearMap.ext_iff, ← hchar.1] exact ⟨fun h ↦ htorsion.casesOn fun x hx ↦ by simpa [← Ideal.mem_torsionOf_iff, hx] using h x, fun h ↦ (congrArg (fun t ↦ ∀ x, t • x = 0) h).mpr fun x ↦ zero_smul R x⟩ end Module /-- For a division ring `D` with center `k`, the ring of `k`-linear endomorphisms of `D` has the same characteristic as `D` -/ instance {D : Type*} [DivisionRing D] {p : ℕ} [CharP D p] : CharP (D →ₗ[(Subring.center D)] D) p := charP_of_injective_ringHom (Algebra.lmul (Subring.center D) D).toRingHom.injective p instance {D : Type*} [DivisionRing D] {p : ℕ} [ExpChar D p] : ExpChar (D →ₗ[Subring.center D] D) p := expChar_of_injective_ringHom (Algebra.lmul (Subring.center D) D).toRingHom.injective p
.lake/packages/mathlib/Mathlib/Algebra/CharP/CharAndCard.lean
import Mathlib.Algebra.CharP.Basic import Mathlib.Algebra.CharP.Lemmas import Mathlib.GroupTheory.Perm.Cycle.Type import Mathlib.RingTheory.Coprime.Lemmas /-! # Characteristic and cardinality We prove some results relating characteristic and cardinality of finite rings ## Tags characteristic, cardinality, ring -/ /-- A prime `p` is a unit in a commutative ring `R` of nonzero characteristic iff it does not divide the characteristic. -/ theorem isUnit_iff_not_dvd_char_of_ringChar_ne_zero (R : Type*) [CommRing R] (p : ℕ) [Fact p.Prime] (hR : ringChar R ≠ 0) : IsUnit (p : R) ↔ ¬p ∣ ringChar R := by have hch := CharP.cast_eq_zero R (ringChar R) have hp : p.Prime := Fact.out constructor · rintro h₁ ⟨q, hq⟩ rcases IsUnit.exists_left_inv h₁ with ⟨a, ha⟩ have h₃ : ¬ringChar R ∣ q := by rintro ⟨r, hr⟩ rw [hr, ← mul_assoc, mul_comm p, mul_assoc] at hq nth_rw 1 [← mul_one (ringChar R)] at hq exact Nat.Prime.not_dvd_one hp ⟨r, mul_left_cancel₀ hR hq⟩ have h₄ := mt (CharP.intCast_eq_zero_iff R (ringChar R) q).mp apply_fun ((↑) : ℕ → R) at hq apply_fun (· * ·) a at hq rw [Nat.cast_mul, hch, mul_zero, ← mul_assoc, ha, one_mul] at hq norm_cast at h₄ exact h₄ h₃ hq.symm · intro h rcases (hp.coprime_iff_not_dvd.mpr h).isCoprime with ⟨a, b, hab⟩ apply_fun ((↑) : ℤ → R) at hab push_cast at hab rw [hch, mul_zero, add_zero, mul_comm] at hab exact .of_mul_eq_one a hab /-- A prime `p` is a unit in a finite commutative ring `R` iff it does not divide the characteristic. -/ theorem isUnit_iff_not_dvd_char (R : Type*) [CommRing R] (p : ℕ) [Fact p.Prime] [Finite R] : IsUnit (p : R) ↔ ¬p ∣ ringChar R := isUnit_iff_not_dvd_char_of_ringChar_ne_zero R p <| CharP.char_ne_zero_of_finite R (ringChar R) /-- The prime divisors of the characteristic of a finite commutative ring are exactly the prime divisors of its cardinality. -/ theorem prime_dvd_char_iff_dvd_card {R : Type*} [CommRing R] [Fintype R] (p : ℕ) [Fact p.Prime] : p ∣ ringChar R ↔ p ∣ Fintype.card R := by refine ⟨fun h => h.trans <| Int.natCast_dvd_natCast.mp <| (CharP.intCast_eq_zero_iff R (ringChar R) (Fintype.card R)).mp <| mod_cast Nat.cast_card_eq_zero R, fun h => ?_⟩ by_contra h₀ rcases exists_prime_addOrderOf_dvd_card p h with ⟨r, hr⟩ have hr₁ := addOrderOf_nsmul_eq_zero r rw [hr, nsmul_eq_mul] at hr₁ rcases IsUnit.exists_left_inv ((isUnit_iff_not_dvd_char R p).mpr h₀) with ⟨u, hu⟩ apply_fun (· * ·) u at hr₁ rw [mul_zero, ← mul_assoc, hu, one_mul] at hr₁ exact mt AddMonoid.addOrderOf_eq_one_iff.mpr (ne_of_eq_of_ne hr (Nat.Prime.ne_one Fact.out)) hr₁ /-- A prime that divides the cardinality of a finite commutative ring `R` isn't a unit in `R`. -/ theorem not_isUnit_prime_of_dvd_card {R : Type*} [CommRing R] [Fintype R] {p : ℕ} [Fact p.Prime] (hp : p ∣ Fintype.card R) : ¬IsUnit (p : R) := mt (isUnit_iff_not_dvd_char R p).mp (Classical.not_not.mpr ((prime_dvd_char_iff_dvd_card p).mpr hp)) lemma charP_of_card_eq_prime {R : Type*} [NonAssocRing R] [Fintype R] {p : ℕ} [hp : Fact p.Prime] (hR : Fintype.card R = p) : CharP R p := have := Fintype.one_lt_card_iff_nontrivial.1 (hR ▸ hp.1.one_lt) (CharP.charP_iff_prime_eq_zero hp.1).2 (hR ▸ Nat.cast_card_eq_zero R) lemma charP_of_card_eq_prime_pow {R : Type*} [CommRing R] [IsDomain R] [Fintype R] {p f : ℕ} [hp : Fact p.Prime] (hR : Fintype.card R = p ^ f) : CharP R p := have hf : f ≠ 0 := fun h0 ↦ not_subsingleton R <| Fintype.card_le_one_iff_subsingleton.mp <| by simpa [h0] using hR.le (CharP.charP_iff_prime_eq_zero hp.out).mpr (by simpa [hf, hR] using Nat.cast_card_eq_zero R)
.lake/packages/mathlib/Mathlib/Algebra/CharP/Reduced.lean
import Mathlib.Algebra.CharP.Frobenius import Mathlib.RingTheory.Nilpotent.Defs /-! # Results about characteristic p reduced rings -/ open Finset section variable (R : Type*) [CommRing R] [IsReduced R] (p n : ℕ) [ExpChar R p] theorem iterateFrobenius_inj : Function.Injective (iterateFrobenius R p n) := fun x y H ↦ by rw [← sub_eq_zero] at H ⊢ simp_rw [iterateFrobenius_def, ← sub_pow_expChar_pow] at H exact IsReduced.eq_zero _ ⟨_, H⟩ theorem frobenius_inj : Function.Injective (frobenius R p) := iterateFrobenius_one (R := R) p ▸ iterateFrobenius_inj R p 1 end /-- If `ringChar R = 2`, where `R` is a finite reduced commutative ring, then every `a : R` is a square. -/ theorem isSquare_of_charTwo' {R : Type*} [Finite R] [CommRing R] [IsReduced R] [CharP R 2] (a : R) : IsSquare a := by cases nonempty_fintype R exact Exists.imp (fun b h => pow_two b ▸ Eq.symm h) (((Fintype.bijective_iff_injective_and_card _).mpr ⟨frobenius_inj R 2, rfl⟩).surjective a) variable {R : Type*} [CommRing R] [IsReduced R] @[simp] theorem ExpChar.pow_prime_pow_mul_eq_one_iff (p k m : ℕ) [ExpChar R p] (x : R) : x ^ (p ^ k * m) = 1 ↔ x ^ m = 1 := by rw [pow_mul'] convert ← (iterateFrobenius_inj R p k).eq_iff apply map_one
.lake/packages/mathlib/Mathlib/Algebra/NoZeroSMulDivisors/Prod.lean
import Mathlib.Algebra.NoZeroSMulDivisors.Defs import Mathlib.Algebra.Notation.Prod /-! # Prod instances for NoZeroSMulDivisors This file defines a NoZeroSMulDivisors instance for the binary product of actions. -/ variable {R M N : Type*} namespace Prod instance noZeroSMulDivisors [Zero R] [Zero M] [Zero N] [SMulWithZero R M] [SMulWithZero R N] [NoZeroSMulDivisors R M] [NoZeroSMulDivisors R N] : NoZeroSMulDivisors R (M × N) where eq_zero_or_eq_zero_of_smul_eq_zero {c xy} h := by simpa [Prod.ext_iff, or_and_left] using h end Prod
.lake/packages/mathlib/Mathlib/Algebra/NoZeroSMulDivisors/Pi.lean
import Mathlib.Algebra.NoZeroSMulDivisors.Defs import Mathlib.Algebra.Group.Action.Pi /-! # Pi instances for NoZeroSMulDivisors This file defines instances for NoZeroSMulDivisors on Pi types. -/ universe u v variable {I : Type u} -- The indexing type variable {f : I → Type v} instance Pi.noZeroSMulDivisors (α) [Zero α] [∀ i, Zero <| f i] [∀ i, SMulWithZero α <| f i] [∀ i, NoZeroSMulDivisors α <| f i] : NoZeroSMulDivisors α (∀ i : I, f i) := ⟨fun {_ _} h => or_iff_not_imp_left.mpr fun hc => funext fun i => (smul_eq_zero.mp (congr_fun h i)).resolve_left hc⟩ /-- A special case of `Pi.noZeroSMulDivisors` for non-dependent types. Lean struggles to synthesize this instance by itself elsewhere in the library. -/ instance _root_.Function.noZeroSMulDivisors {ι α β : Type*} [Zero α] [Zero β] [SMulWithZero α β] [NoZeroSMulDivisors α β] : NoZeroSMulDivisors α (ι → β) := Pi.noZeroSMulDivisors _
.lake/packages/mathlib/Mathlib/Algebra/NoZeroSMulDivisors/Basic.lean
import Mathlib.Algebra.GroupWithZero.Action.Units import Mathlib.Algebra.Module.End import Mathlib.Algebra.NoZeroSMulDivisors.Defs /-! # `NoZeroSMulDivisors` This file defines the `NoZeroSMulDivisors` class, and includes some tests for the vanishing of elements (especially in modules over division rings). -/ assert_not_exists Multiset Set.indicator Pi.single_smul₀ Field section NoZeroSMulDivisors variable {R M : Type*} section Module variable [Semiring R] variable (R M) /-- If `M` is an `R`-module with one and `M` has characteristic zero, then `R` has characteristic zero as well. Usually `M` is an `R`-algebra. -/ theorem CharZero.of_module (M) [AddCommMonoidWithOne M] [CharZero M] [Module R M] : CharZero R := by refine ⟨fun m n h => @Nat.cast_injective M _ _ _ _ ?_⟩ rw [← nsmul_one, ← nsmul_one, ← Nat.cast_smul_eq_nsmul R, ← Nat.cast_smul_eq_nsmul R, h] end Module section AddCommGroup -- `R` can still be a semiring here variable [Semiring R] [AddCommGroup M] [Module R M] section SMulInjective variable (M) in theorem smul_right_injective [NoZeroSMulDivisors R M] {c : R} (hc : c ≠ 0) : Function.Injective (c • · : M → M) := (injective_iff_map_eq_zero (smulAddHom R M c)).2 fun _ ha => (smul_eq_zero.mp ha).resolve_left hc theorem smul_right_inj [NoZeroSMulDivisors R M] {c : R} (hc : c ≠ 0) {x y : M} : c • x = c • y ↔ x = y := (smul_right_injective M hc).eq_iff end SMulInjective section Nat variable (R M) [CharZero R] [NoZeroSMulDivisors R M] include R in lemma IsAddTorsionFree.of_noZeroSMulDivisors : IsAddTorsionFree M where nsmul_right_injective n hn := by simp_rw [← Nat.cast_smul_eq_nsmul R]; apply smul_right_injective; simpa @[deprecated IsAddTorsionFree.of_noZeroSMulDivisors (since := "2025-10-19")] theorem Nat.noZeroSMulDivisors (R) (M) [Semiring R] [CharZero R] [AddCommMonoid M] [Module R M] [NoZeroSMulDivisors R M] : NoZeroSMulDivisors ℕ M where eq_zero_or_eq_zero_of_smul_eq_zero {c x} := by rw [← Nat.cast_smul_eq_nsmul R, smul_eq_zero]; simp end Nat end AddCommGroup section Module variable [Ring R] [AddCommGroup M] [Module R M] section SMulInjective variable (R) variable [NoZeroSMulDivisors R M] theorem smul_left_injective {x : M} (hx : x ≠ 0) : Function.Injective fun c : R => c • x := fun c d h => sub_eq_zero.mp ((smul_eq_zero.mp (calc (c - d) • x = c • x - d • x := sub_smul c d x _ = 0 := sub_eq_zero.mpr h )).resolve_right hx) end SMulInjective variable (R M) @[deprecated IsAddTorsionFree.of_noZeroSMulDivisors (since := "2025-10-19")] theorem NoZeroSMulDivisors.int_of_charZero (R) (M) [Ring R] [AddCommGroup M] [Module R M] [NoZeroSMulDivisors R M] [CharZero R] : NoZeroSMulDivisors ℤ M := ⟨fun {z x} h ↦ by simpa [← smul_one_smul R z x] using h⟩ /-- Only a ring of characteristic zero can have a non-trivial module without additive or scalar torsion. -/ theorem CharZero.of_noZeroSMulDivisors [Nontrivial M] [IsAddTorsionFree M] : CharZero R := by refine ⟨fun {n m h} ↦ ?_⟩ obtain ⟨x, hx⟩ := exists_ne (0 : M) replace h : (n : ℤ) • x = (m : ℤ) • x := by simp [← Nat.cast_smul_eq_nsmul R, h] simpa using smul_left_injective ℤ hx h end Module section GroupWithZero variable [GroupWithZero R] [AddMonoid M] [DistribMulAction R M] -- see note [lower instance priority] /-- This instance applies to `DivisionSemiring`s, in particular `NNReal` and `NNRat`. -/ instance (priority := 100) GroupWithZero.toNoZeroSMulDivisors : NoZeroSMulDivisors R M := ⟨fun {a _} h ↦ or_iff_not_imp_left.2 fun ha ↦ (smul_eq_zero_iff_eq <| Units.mk0 a ha).1 h⟩ end GroupWithZero end NoZeroSMulDivisors
.lake/packages/mathlib/Mathlib/Algebra/NoZeroSMulDivisors/Defs.lean
import Mathlib.Algebra.GroupWithZero.Action.Defs import Mathlib.Algebra.Group.Torsion import Mathlib.Tactic.Contrapose /-! # `NoZeroSMulDivisors` This file defines the `NoZeroSMulDivisors` class, and includes some tests for the vanishing of elements (especially in modules over division rings). ## TODO `NoZeroSMulDivisors` is mathematically incorrect for semimodules. Replace it with a new typeclass `Module.IsTorsionFree`, cf https://github.com/kbuzzard/ClassFieldTheory. Torsion-free monoids have seen the same change happen already. -/ assert_not_exists RelIso Multiset Set.indicator Pi.single_smul₀ Ring Module variable {R M G : Type*} /-- `NoZeroSMulDivisors R M` states that a scalar multiple is `0` only if either argument is `0`. This is a version of saying that `M` is torsion free, without assuming `R` is zero-divisor free. The main application of `NoZeroSMulDivisors R M`, when `M` is a module, is the result `smul_eq_zero`: a scalar multiple is `0` iff either argument is `0`. It is a generalization of the `NoZeroDivisors` class to heterogeneous multiplication. -/ @[mk_iff] class NoZeroSMulDivisors (R M : Type*) [Zero R] [Zero M] [SMul R M] : Prop where /-- If scalar multiplication yields zero, either the scalar or the vector was zero. -/ eq_zero_or_eq_zero_of_smul_eq_zero : ∀ {c : R} {x : M}, c • x = 0 → c = 0 ∨ x = 0 export NoZeroSMulDivisors (eq_zero_or_eq_zero_of_smul_eq_zero) /-- Pullback a `NoZeroSMulDivisors` instance along an injective function. -/ theorem Function.Injective.noZeroSMulDivisors {R M N : Type*} [Zero R] [Zero M] [Zero N] [SMul R M] [SMul R N] [NoZeroSMulDivisors R N] (f : M → N) (hf : Function.Injective f) (h0 : f 0 = 0) (hs : ∀ (c : R) (x : M), f (c • x) = c • f x) : NoZeroSMulDivisors R M := ⟨fun {_ _} h => Or.imp_right (@hf _ _) <| h0.symm ▸ eq_zero_or_eq_zero_of_smul_eq_zero (by rw [← hs, h, h0])⟩ -- See note [lower instance priority] instance (priority := 100) NoZeroDivisors.toNoZeroSMulDivisors [Zero R] [Mul R] [NoZeroDivisors R] : NoZeroSMulDivisors R R := ⟨fun {_ _} => eq_zero_or_eq_zero_of_mul_eq_zero⟩ theorem smul_ne_zero [Zero R] [Zero M] [SMul R M] [NoZeroSMulDivisors R M] {c : R} {x : M} (hc : c ≠ 0) (hx : x ≠ 0) : c • x ≠ 0 := fun h => (eq_zero_or_eq_zero_of_smul_eq_zero h).elim hc hx theorem noZeroSMulDivisors_iff_right_eq_zero_of_smul [Zero R] [Zero M] [SMul R M] : NoZeroSMulDivisors R M ↔ ∀ r : R, r ≠ 0 → ∀ m : M, r • m = 0 → m = 0 := by simp_rw [noZeroSMulDivisors_iff, or_iff_not_imp_left] exact ⟨fun h r hr m eq ↦ h eq hr, fun h r m eq hr ↦ h r hr m eq⟩ section SMulWithZero variable [Zero R] [Zero M] [SMulWithZero R M] [NoZeroSMulDivisors R M] {c : R} {x : M} @[simp] theorem smul_eq_zero : c • x = 0 ↔ c = 0 ∨ x = 0 := ⟨eq_zero_or_eq_zero_of_smul_eq_zero, fun h => h.elim (fun h => h.symm ▸ zero_smul R x) fun h => h.symm ▸ smul_zero c⟩ theorem smul_ne_zero_iff : c • x ≠ 0 ↔ c ≠ 0 ∧ x ≠ 0 := by rw [Ne, smul_eq_zero, not_or] lemma smul_eq_zero_iff_left (hx : x ≠ 0) : c • x = 0 ↔ c = 0 := by simp [hx] lemma smul_eq_zero_iff_right (hc : c ≠ 0) : c • x = 0 ↔ x = 0 := by simp [hc] lemma smul_ne_zero_iff_left (hx : x ≠ 0) : c • x ≠ 0 ↔ c ≠ 0 := by simp [hx] lemma smul_ne_zero_iff_right (hc : c ≠ 0) : c • x ≠ 0 ↔ x ≠ 0 := by simp [hc] end SMulWithZero instance IsAddTorsionFree.to_noZeroSMulDivisors_nat [AddMonoid M] [IsAddTorsionFree M] : NoZeroSMulDivisors ℕ M where eq_zero_or_eq_zero_of_smul_eq_zero {n x} hx := by contrapose! hx; simpa using (nsmul_right_injective hx.1).ne hx.2 instance IsAddTorsionFree.to_noZeroSMulDivisors_int [AddGroup G] [IsAddTorsionFree G] : NoZeroSMulDivisors ℤ G where eq_zero_or_eq_zero_of_smul_eq_zero {n x} hx := by contrapose! hx; simpa using (zsmul_right_injective hx.1).ne hx.2 @[simp] lemma noZeroSMulDivisors_nat_iff_isAddTorsionFree [AddCommGroup G] : NoZeroSMulDivisors ℕ G ↔ IsAddTorsionFree G where mp _ := by refine ⟨fun n hn a b hab ↦ ?_⟩ simp only [← sub_eq_zero (a := n • a), ← nsmul_sub] at hab simpa [sub_eq_zero] using (smul_eq_zero_iff_right hn).1 hab mpr _ := inferInstance @[simp] lemma noZeroSMulDivisors_int_iff_isAddTorsionFree [AddCommGroup G] : NoZeroSMulDivisors ℤ G ↔ IsAddTorsionFree G where mp _ := by refine ⟨fun n hn a b hab ↦ ?_⟩ simp only [← sub_eq_zero (a := (n : ℤ) • a), ← zsmul_sub, ← natCast_zsmul] at hab simpa [sub_eq_zero] using (smul_eq_zero_iff_right <| Int.natCast_ne_zero.2 hn).1 hab mpr _ := inferInstance alias ⟨IsAddTorsionFree.of_noZeroSMulDivisors_nat, _⟩ := noZeroSMulDivisors_nat_iff_isAddTorsionFree alias ⟨IsAddTorsionFree.of_noZeroSMulDivisors_int, _⟩ := noZeroSMulDivisors_int_iff_isAddTorsionFree
.lake/packages/mathlib/Mathlib/Algebra/Pointwise/Stabilizer.lean
import Mathlib.Algebra.Group.Action.Pointwise.Finset import Mathlib.GroupTheory.QuotientGroup.Defs import Mathlib.Order.ConditionallyCompleteLattice.Basic /-! # Stabilizer of a set under a pointwise action This file characterises the stabilizer of a set/finset under the pointwise action of a group. -/ open Function MulOpposite Set open scoped Pointwise namespace MulAction variable {G H α : Type*} /-! ### Stabilizer of a set -/ section Set section Group variable [Group G] [Group H] [MulAction G α] {a : G} {s t : Set α} @[to_additive (attr := simp)] lemma stabilizer_empty : stabilizer G (∅ : Set α) = ⊤ := Subgroup.coe_eq_univ.1 <| eq_univ_of_forall fun _a ↦ smul_set_empty @[to_additive (attr := simp)] lemma stabilizer_univ : stabilizer G (Set.univ : Set α) = ⊤ := by ext simp @[to_additive (attr := simp)] lemma stabilizer_singleton (b : α) : stabilizer G ({b} : Set α) = stabilizer G b := by ext; simp @[to_additive] lemma mem_stabilizer_set {s : Set α} : a ∈ stabilizer G s ↔ ∀ b, a • b ∈ s ↔ b ∈ s := by refine mem_stabilizer_iff.trans ⟨fun h b ↦ ?_, fun h ↦ ?_⟩ · rw [← (smul_mem_smul_set_iff : a • b ∈ _ ↔ _), h] simp_rw [Set.ext_iff, mem_smul_set_iff_inv_smul_mem] exact ((MulAction.toPerm a).forall_congr' <| by simp [Iff.comm]).1 h @[to_additive] lemma map_stabilizer_le (f : G →* H) (s : Set G) : (stabilizer G s).map f ≤ stabilizer H (f '' s) := by rintro a simp only [Subgroup.mem_map, mem_stabilizer_iff, forall_exists_index, and_imp] rintro a ha rfl rw [← image_smul_distrib, ha] @[to_additive (attr := simp)] lemma stabilizer_mul_self (s : Set G) : (stabilizer G s : Set G) * s = s := by ext refine ⟨?_, fun h ↦ ⟨_, (stabilizer G s).one_mem, _, h, one_mul _⟩⟩ rintro ⟨a, ha, b, hb, rfl⟩ rw [← mem_stabilizer_iff.1 ha] exact smul_mem_smul_set hb @[to_additive] lemma stabilizer_inf_stabilizer_le_stabilizer_apply₂ {f : Set α → Set α → Set α} (hf : ∀ a : G, a • f s t = f (a • s) (a • t)) : stabilizer G s ⊓ stabilizer G t ≤ stabilizer G (f s t) := by aesop (add simp [SetLike.le_def]) @[to_additive] lemma stabilizer_inf_stabilizer_le_stabilizer_union : stabilizer G s ⊓ stabilizer G t ≤ stabilizer G (s ∪ t) := stabilizer_inf_stabilizer_le_stabilizer_apply₂ fun _ ↦ smul_set_union @[to_additive] lemma stabilizer_inf_stabilizer_le_stabilizer_inter : stabilizer G s ⊓ stabilizer G t ≤ stabilizer G (s ∩ t) := stabilizer_inf_stabilizer_le_stabilizer_apply₂ fun _ ↦ smul_set_inter @[to_additive] lemma stabilizer_inf_stabilizer_le_stabilizer_sdiff : stabilizer G s ⊓ stabilizer G t ≤ stabilizer G (s \ t) := stabilizer_inf_stabilizer_le_stabilizer_apply₂ fun _ ↦ smul_set_sdiff @[to_additive] lemma stabilizer_union_eq_left (hdisj : Disjoint s t) (hstab : stabilizer G s ≤ stabilizer G t) (hstab_union : stabilizer G (s ∪ t) ≤ stabilizer G t) : stabilizer G (s ∪ t) = stabilizer G s := by refine le_antisymm ?_ ?_ · calc stabilizer G (s ∪ t) ≤ stabilizer G (s ∪ t) ⊓ stabilizer G t := by simpa _ ≤ stabilizer G ((s ∪ t) \ t) := stabilizer_inf_stabilizer_le_stabilizer_sdiff _ = stabilizer G s := by rw [union_diff_cancel_right]; simpa [← disjoint_iff_inter_eq_empty] · calc stabilizer G s ≤ stabilizer G s ⊓ stabilizer G t := by simpa _ ≤ stabilizer G (s ∪ t) := stabilizer_inf_stabilizer_le_stabilizer_union @[to_additive] lemma stabilizer_union_eq_right (hdisj : Disjoint s t) (hstab : stabilizer G t ≤ stabilizer G s) (hstab_union : stabilizer G (s ∪ t) ≤ stabilizer G s) : stabilizer G (s ∪ t) = stabilizer G t := by rw [union_comm, stabilizer_union_eq_left hdisj.symm hstab (union_comm .. ▸ hstab_union)] variable {s : Set G} open scoped RightActions in @[to_additive] lemma op_smul_set_stabilizer_subset (ha : a ∈ s) : (stabilizer G s : Set G) <• a ⊆ s := smul_set_subset_iff.2 fun b hb ↦ by rw [← hb]; exact smul_mem_smul_set ha @[to_additive] lemma stabilizer_subset_div_right (ha : a ∈ s) : ↑(stabilizer G s) ⊆ s / {a} := fun b hb ↦ ⟨_, by rwa [← smul_eq_mul, mem_stabilizer_set.1 hb], _, mem_singleton _, mul_div_cancel_right _ _⟩ @[to_additive] lemma stabilizer_finite (hs₀ : s.Nonempty) (hs : s.Finite) : (stabilizer G s : Set G).Finite := by obtain ⟨a, ha⟩ := hs₀ exact (hs.div <| finite_singleton _).subset <| stabilizer_subset_div_right ha end Group section CommGroup variable [CommGroup G] {s t : Set G} {a : G} @[to_additive] lemma smul_set_stabilizer_subset (ha : a ∈ s) : a • (stabilizer G s : Set G) ⊆ s := by simpa using op_smul_set_stabilizer_subset ha end CommGroup end Set variable [Group G] [Group H] [MulAction G α] {a : G} /-! ### Stabilizer of a subgroup -/ section Subgroup -- TODO: Is there a lemma that could unify the following three very similar lemmas? @[to_additive (attr := simp)] lemma stabilizer_subgroup (s : Subgroup G) : stabilizer G (s : Set G) = s := by simp_rw [SetLike.ext_iff, mem_stabilizer_set] refine fun a ↦ ⟨fun h ↦ ?_, fun ha b ↦ s.mul_mem_cancel_left ha⟩ simpa only [smul_eq_mul, SetLike.mem_coe, mul_one] using (h 1).2 s.one_mem @[to_additive (attr := simp)] lemma stabilizer_op_subgroup (s : Subgroup G) : stabilizer Gᵐᵒᵖ (s : Set G) = s.op := by simp_rw [SetLike.ext_iff, mem_stabilizer_set] simp only [smul_eq_mul_unop, SetLike.mem_coe, Subgroup.mem_op, «forall», unop_op] refine fun a ↦ ⟨fun h ↦ ?_, fun ha b ↦ s.mul_mem_cancel_right ha⟩ simpa only [op_smul_eq_mul, SetLike.mem_coe, one_mul] using (h 1).2 s.one_mem @[to_additive (attr := simp)] lemma stabilizer_subgroup_op (s : Subgroup Gᵐᵒᵖ) : stabilizer G (s : Set Gᵐᵒᵖ) = s.unop := by simp_rw [SetLike.ext_iff, mem_stabilizer_set] refine fun a ↦ ⟨fun h ↦ ?_, fun ha b ↦ s.mul_mem_cancel_right ha⟩ have : 1 * MulOpposite.op a ∈ s := (h 1).2 s.one_mem simpa only [op_smul_eq_mul, SetLike.mem_coe, one_mul] using this end Subgroup /-! ### Stabilizer of a finset -/ section Finset variable [DecidableEq α] @[to_additive (attr := simp, norm_cast)] lemma stabilizer_coe_finset (s : Finset α) : stabilizer G (s : Set α) = stabilizer G s := by ext; simp [← Finset.coe_inj] @[to_additive (attr := simp)] lemma stabilizer_finset_empty : stabilizer G (∅ : Finset α) = ⊤ := Subgroup.coe_eq_univ.1 <| eq_univ_of_forall Finset.smul_finset_empty @[to_additive (attr := simp)] lemma stabilizer_finset_univ [Fintype α] : stabilizer G (Finset.univ : Finset α) = ⊤ := by ext simp @[to_additive (attr := simp)] lemma stabilizer_finset_singleton (b : α) : stabilizer G ({b} : Finset α) = stabilizer G b := by ext; simp @[to_additive] lemma mem_stabilizer_finset {s : Finset α} : a ∈ stabilizer G s ↔ ∀ b, a • b ∈ s ↔ b ∈ s := by simp_rw [← stabilizer_coe_finset, mem_stabilizer_set, Finset.mem_coe] @[to_additive] lemma mem_stabilizer_finset_iff_subset_smul_finset {s : Finset α} : a ∈ stabilizer G s ↔ s ⊆ a • s := by rw [mem_stabilizer_iff, Finset.subset_iff_eq_of_card_le (Finset.card_smul_finset _ _).le, eq_comm] @[to_additive] lemma mem_stabilizer_finset_iff_smul_finset_subset {s : Finset α} : a ∈ stabilizer G s ↔ a • s ⊆ s := by rw [mem_stabilizer_iff, Finset.subset_iff_eq_of_card_le (Finset.card_smul_finset _ _).ge] @[to_additive] lemma mem_stabilizer_finset' {s : Finset α} : a ∈ stabilizer G s ↔ ∀ ⦃b⦄, b ∈ s → a • b ∈ s := by rw [← Subgroup.inv_mem_iff, mem_stabilizer_finset_iff_subset_smul_finset] simp_rw [← Finset.mem_inv_smul_finset_iff, Finset.subset_iff] end Finset /-! ### Stabilizer of a finite set -/ variable {s : Set α} @[to_additive] lemma mem_stabilizer_set_iff_subset_smul_set {s : Set α} (hs : s.Finite) : a ∈ stabilizer G s ↔ s ⊆ a • s := by lift s to Finset α using hs classical rw [stabilizer_coe_finset, mem_stabilizer_finset_iff_subset_smul_finset, ← Finset.coe_smul_finset, Finset.coe_subset] @[to_additive] lemma mem_stabilizer_set_iff_smul_set_subset {s : Set α} (hs : s.Finite) : a ∈ stabilizer G s ↔ a • s ⊆ s := by lift s to Finset α using hs classical rw [stabilizer_coe_finset, mem_stabilizer_finset_iff_smul_finset_subset, ← Finset.coe_smul_finset, Finset.coe_subset] @[to_additive] lemma mem_stabilizer_set' {s : Set α} (hs : s.Finite) : a ∈ stabilizer G s ↔ ∀ ⦃b⦄, b ∈ s → a • b ∈ s := by lift s to Finset α using hs classical simp [-mem_stabilizer_iff, mem_stabilizer_finset'] end MulAction /-! ### Stabilizer in a commutative group -/ namespace MulAction variable {G : Type*} [CommGroup G] (s : Set G) @[to_additive (attr := simp)] lemma mul_stabilizer_self : s * stabilizer G s = s := by rw [mul_comm, stabilizer_mul_self] local notation " Q " => G ⧸ stabilizer G s local notation " q " => ((↑) : G → Q) @[to_additive] lemma stabilizer_image_coe_quotient : stabilizer Q (q '' s) = ⊥ := by ext a induction a using QuotientGroup.induction_on with | _ a simp only [mem_stabilizer_iff, Subgroup.mem_bot, QuotientGroup.eq_one_iff] have : q a • q '' s = q '' (a • s) := (image_smul_distrib (QuotientGroup.mk' <| stabilizer G s) _ _).symm rw [this] refine ⟨fun h ↦ ?_, fun h ↦ by rw [h]⟩ rwa [QuotientGroup.image_coe_inj, mul_smul_comm, stabilizer_mul_self] at h end MulAction
.lake/packages/mathlib/Mathlib/Algebra/Polynomial/Div.lean
import Mathlib.Algebra.Field.IsField import Mathlib.Algebra.Polynomial.Inductions import Mathlib.Algebra.Polynomial.Monic import Mathlib.Algebra.Ring.Regular import Mathlib.RingTheory.Multiplicity import Mathlib.Data.Nat.Lattice /-! # Division of univariate polynomials The main defs are `divByMonic` and `modByMonic`. The compatibility between these is given by `modByMonic_add_div`. We also define `rootMultiplicity`. -/ noncomputable section open Polynomial open Finset namespace Polynomial universe u v w z variable {R : Type u} {S : Type v} {T : Type w} {A : Type z} {a b : R} {n : ℕ} section Semiring variable [Semiring R] theorem X_dvd_iff {f : R[X]} : X ∣ f ↔ f.coeff 0 = 0 := ⟨fun ⟨g, hfg⟩ => by rw [hfg, coeff_X_mul_zero], fun hf => ⟨f.divX, by rw [← add_zero (X * f.divX), ← C_0, ← hf, X_mul_divX_add]⟩⟩ theorem X_pow_dvd_iff {f : R[X]} {n : ℕ} : X ^ n ∣ f ↔ ∀ d < n, f.coeff d = 0 := ⟨fun ⟨g, hgf⟩ d hd => by simp only [hgf, coeff_X_pow_mul', ite_eq_right_iff, not_le_of_gt hd, IsEmpty.forall_iff], fun hd => by induction n with | zero => simp [pow_zero] | succ n hn => obtain ⟨g, hgf⟩ := hn fun d : ℕ => fun H : d < n => hd _ (Nat.lt_succ_of_lt H) have := coeff_X_pow_mul g n 0 rw [zero_add, ← hgf, hd n (Nat.lt_succ_self n)] at this obtain ⟨k, hgk⟩ := Polynomial.X_dvd_iff.mpr this.symm use k rwa [pow_succ, mul_assoc, ← hgk]⟩ variable {p q : R[X]} theorem finiteMultiplicity_of_degree_pos_of_monic (hp : (0 : WithBot ℕ) < degree p) (hmp : Monic p) (hq : q ≠ 0) : FiniteMultiplicity p q := have zn0 : (0 : R) ≠ 1 := haveI := Nontrivial.of_polynomial_ne hq zero_ne_one ⟨natDegree q, fun ⟨r, hr⟩ => by have hp0 : p ≠ 0 := fun hp0 => by simp [hp0] at hp have hr0 : r ≠ 0 := fun hr0 => by subst hr0; simp [hq] at hr have hpn1 : leadingCoeff p ^ (natDegree q + 1) = 1 := by simp [show _ = _ from hmp] have hpn0' : leadingCoeff p ^ (natDegree q + 1) ≠ 0 := hpn1.symm ▸ zn0.symm have hpnr0 : leadingCoeff (p ^ (natDegree q + 1)) * leadingCoeff r ≠ 0 := by simp only [leadingCoeff_pow' hpn0', leadingCoeff_eq_zero, hpn1, one_mul, Ne, hr0, not_false_eq_true] have hnp : 0 < natDegree p := Nat.cast_lt.1 <| by rw [← degree_eq_natDegree hp0]; exact hp have := congr_arg natDegree hr rw [natDegree_mul' hpnr0, natDegree_pow' hpn0', add_mul, add_assoc] at this exact ne_of_lt (lt_add_of_le_of_pos (le_mul_of_one_le_right (Nat.zero_le _) hnp) (add_pos_of_pos_of_nonneg (by rwa [one_mul]) (Nat.zero_le _))) this⟩ end Semiring section Ring variable [Ring R] {p q : R[X]} theorem div_wf_lemma (h : degree q ≤ degree p ∧ p ≠ 0) (hq : Monic q) : degree (p - q * (C (leadingCoeff p) * X ^ (natDegree p - natDegree q))) < degree p := have hp : leadingCoeff p ≠ 0 := mt leadingCoeff_eq_zero.1 h.2 have hq0 : q ≠ 0 := hq.ne_zero_of_polynomial_ne h.2 have hlt : natDegree q ≤ natDegree p := (Nat.cast_le (α := WithBot ℕ)).1 (by rw [← degree_eq_natDegree h.2, ← degree_eq_natDegree hq0]; exact h.1) degree_sub_lt (by rw [hq.degree_mul_comm, hq.degree_mul, degree_C_mul_X_pow _ hp, degree_eq_natDegree h.2, degree_eq_natDegree hq0, ← Nat.cast_add, tsub_add_cancel_of_le hlt]) h.2 (by rw [leadingCoeff_monic_mul hq, leadingCoeff_mul_X_pow, leadingCoeff_C]) /-- See `divByMonic`. -/ noncomputable def divModByMonicAux : ∀ (_p : R[X]) {q : R[X]}, Monic q → R[X] × R[X] | p, q, hq => letI := Classical.decEq R if h : degree q ≤ degree p ∧ p ≠ 0 then let z := C (leadingCoeff p) * X ^ (natDegree p - natDegree q) have _wf := div_wf_lemma h hq let dm := divModByMonicAux (p - q * z) hq ⟨z + dm.1, dm.2⟩ else ⟨0, p⟩ termination_by p => p /-- `divByMonic`, denoted as `p /ₘ q`, gives the quotient of `p` by a monic polynomial `q`. -/ def divByMonic (p q : R[X]) : R[X] := letI := Classical.decEq R if hq : Monic q then (divModByMonicAux p hq).1 else 0 /-- `modByMonic`, denoted as `p %ₘ q`, gives the remainder of `p` by a monic polynomial `q`. -/ def modByMonic (p q : R[X]) : R[X] := letI := Classical.decEq R if hq : Monic q then (divModByMonicAux p hq).2 else p @[inherit_doc] infixl:70 " /ₘ " => divByMonic @[inherit_doc] infixl:70 " %ₘ " => modByMonic theorem degree_modByMonic_lt [Nontrivial R] : ∀ (p : R[X]) {q : R[X]} (_hq : Monic q), degree (p %ₘ q) < degree q | p, q, hq => letI := Classical.decEq R if h : degree q ≤ degree p ∧ p ≠ 0 then by have _wf := div_wf_lemma ⟨h.1, h.2⟩ hq have := degree_modByMonic_lt (p - q * (C (leadingCoeff p) * X ^ (natDegree p - natDegree q))) hq grind [divModByMonicAux, modByMonic] else Or.casesOn (not_and_or.1 h) (by unfold modByMonic divModByMonicAux dsimp rw [dif_pos hq, if_neg h] exact lt_of_not_ge) (by intro hp unfold modByMonic divModByMonicAux dsimp rw [dif_pos hq, if_neg h, Classical.not_not.1 hp] exact lt_of_le_of_ne bot_le (Ne.symm (mt degree_eq_bot.1 hq.ne_zero))) termination_by p => p theorem natDegree_modByMonic_lt (p : R[X]) {q : R[X]} (hmq : Monic q) (hq : q ≠ 1) : natDegree (p %ₘ q) < q.natDegree := by by_cases hpq : p %ₘ q = 0 · rw [hpq, natDegree_zero, Nat.pos_iff_ne_zero] contrapose! hq exact eq_one_of_monic_natDegree_zero hmq hq · haveI := Nontrivial.of_polynomial_ne hpq exact natDegree_lt_natDegree hpq (degree_modByMonic_lt p hmq) @[simp] theorem zero_modByMonic (p : R[X]) : 0 %ₘ p = 0 := by grind [modByMonic, divModByMonicAux] @[simp] theorem zero_divByMonic (p : R[X]) : 0 /ₘ p = 0 := by grind [divByMonic, divModByMonicAux] @[simp] theorem modByMonic_zero (p : R[X]) : p %ₘ 0 = p := letI := Classical.decEq R if h : Monic (0 : R[X]) then by haveI := monic_zero_iff_subsingleton.mp h simp [eq_iff_true_of_subsingleton] else by unfold modByMonic divModByMonicAux; rw [dif_neg h] @[simp] theorem divByMonic_zero (p : R[X]) : p /ₘ 0 = 0 := letI := Classical.decEq R if h : Monic (0 : R[X]) then by haveI := monic_zero_iff_subsingleton.mp h simp [eq_iff_true_of_subsingleton] else by unfold divByMonic divModByMonicAux; rw [dif_neg h] theorem divByMonic_eq_of_not_monic (p : R[X]) (hq : ¬Monic q) : p /ₘ q = 0 := dif_neg hq theorem modByMonic_eq_of_not_monic (p : R[X]) (hq : ¬Monic q) : p %ₘ q = p := dif_neg hq theorem modByMonic_eq_self_iff [Nontrivial R] (hq : Monic q) : p %ₘ q = p ↔ degree p < degree q := ⟨fun h => h ▸ degree_modByMonic_lt _ hq, fun h => by classical have : ¬degree q ≤ degree p := not_le_of_gt h unfold modByMonic divModByMonicAux; dsimp; rw [dif_pos hq, if_neg (mt And.left this)]⟩ theorem degree_modByMonic_le (p : R[X]) {q : R[X]} (hq : Monic q) : degree (p %ₘ q) ≤ degree q := by nontriviality R exact (degree_modByMonic_lt _ hq).le theorem degree_modByMonic_le_left : degree (p %ₘ q) ≤ degree p := by nontriviality R by_cases hq : q.Monic · cases lt_or_ge (degree p) (degree q) · rw [(modByMonic_eq_self_iff hq).mpr ‹_›] · exact (degree_modByMonic_le p hq).trans ‹_› · rw [modByMonic_eq_of_not_monic p hq] theorem natDegree_modByMonic_le (p : Polynomial R) {g : Polynomial R} (hg : g.Monic) : natDegree (p %ₘ g) ≤ g.natDegree := natDegree_le_natDegree (degree_modByMonic_le p hg) theorem natDegree_modByMonic_le_left : natDegree (p %ₘ q) ≤ natDegree p := natDegree_le_natDegree degree_modByMonic_le_left theorem X_dvd_sub_C : X ∣ p - C (p.coeff 0) := by simp [X_dvd_iff, coeff_C] theorem modByMonic_eq_sub_mul_div : ∀ (p : R[X]) {q : R[X]} (_hq : Monic q), p %ₘ q = p - q * (p /ₘ q) | p, q, hq => letI := Classical.decEq R if h : degree q ≤ degree p ∧ p ≠ 0 then by have _wf := div_wf_lemma h hq have ih := modByMonic_eq_sub_mul_div (p - q * (C (leadingCoeff p) * X ^ (natDegree p - natDegree q))) hq unfold modByMonic divByMonic divModByMonicAux dsimp rw [dif_pos hq, if_pos h] rw [modByMonic, dif_pos hq] at ih refine ih.trans ?_ unfold divByMonic rw [dif_pos hq, dif_pos hq, if_pos h, mul_add, sub_add_eq_sub_sub] else by unfold modByMonic divByMonic divModByMonicAux dsimp rw [dif_pos hq, if_neg h, dif_pos hq, if_neg h, mul_zero, sub_zero] termination_by p => p theorem modByMonic_add_div (p : R[X]) {q : R[X]} (hq : Monic q) : p %ₘ q + q * (p /ₘ q) = p := eq_sub_iff_add_eq.1 (modByMonic_eq_sub_mul_div p hq) theorem divByMonic_eq_zero_iff [Nontrivial R] (hq : Monic q) : p /ₘ q = 0 ↔ degree p < degree q := ⟨fun h => by have := modByMonic_add_div p hq rwa [h, mul_zero, add_zero, modByMonic_eq_self_iff hq] at this, fun h => by classical have : ¬degree q ≤ degree p := not_le_of_gt h unfold divByMonic divModByMonicAux; dsimp; rw [dif_pos hq, if_neg (mt And.left this)]⟩ theorem degree_add_divByMonic (hq : Monic q) (h : degree q ≤ degree p) : degree q + degree (p /ₘ q) = degree p := by nontriviality R have hdiv0 : p /ₘ q ≠ 0 := by rwa [Ne, divByMonic_eq_zero_iff hq, not_lt] have hlc : leadingCoeff q * leadingCoeff (p /ₘ q) ≠ 0 := by rwa [Monic.def.1 hq, one_mul, Ne, leadingCoeff_eq_zero] have hmod : degree (p %ₘ q) < degree (q * (p /ₘ q)) := calc degree (p %ₘ q) < degree q := degree_modByMonic_lt _ hq _ ≤ _ := by rw [degree_mul' hlc, degree_eq_natDegree hq.ne_zero, degree_eq_natDegree hdiv0, ← Nat.cast_add, Nat.cast_le] exact Nat.le_add_right _ _ calc degree q + degree (p /ₘ q) = degree (q * (p /ₘ q)) := Eq.symm (degree_mul' hlc) _ = degree (p %ₘ q + q * (p /ₘ q)) := (degree_add_eq_right_of_degree_lt hmod).symm _ = _ := congr_arg _ (modByMonic_add_div _ hq) theorem degree_divByMonic_le (p q : R[X]) : degree (p /ₘ q) ≤ degree p := letI := Classical.decEq R if hp0 : p = 0 then by simp only [hp0, zero_divByMonic, le_refl] else if hq : Monic q then if h : degree q ≤ degree p then by haveI := Nontrivial.of_polynomial_ne hp0 rw [← degree_add_divByMonic hq h, degree_eq_natDegree hq.ne_zero, degree_eq_natDegree (mt (divByMonic_eq_zero_iff hq).1 (not_lt.2 h))] exact WithBot.coe_le_coe.2 (Nat.le_add_left _ _) else by unfold divByMonic divModByMonicAux simp [dif_pos hq, h, degree_zero, bot_le] else (divByMonic_eq_of_not_monic p hq).symm ▸ bot_le theorem degree_divByMonic_lt (p : R[X]) {q : R[X]} (hq : Monic q) (hp0 : p ≠ 0) (h0q : 0 < degree q) : degree (p /ₘ q) < degree p := if hpq : degree p < degree q then by haveI := Nontrivial.of_polynomial_ne hp0 rw [(divByMonic_eq_zero_iff hq).2 hpq, degree_eq_natDegree hp0] exact WithBot.bot_lt_coe _ else by haveI := Nontrivial.of_polynomial_ne hp0 rw [← degree_add_divByMonic hq (not_lt.1 hpq), degree_eq_natDegree hq.ne_zero, degree_eq_natDegree (mt (divByMonic_eq_zero_iff hq).1 hpq)] exact Nat.cast_lt.2 (Nat.lt_add_of_pos_left (Nat.cast_lt.1 <| by simpa [degree_eq_natDegree hq.ne_zero] using h0q)) theorem natDegree_divByMonic (f : R[X]) {g : R[X]} (hg : g.Monic) : natDegree (f /ₘ g) = natDegree f - natDegree g := by nontriviality R by_cases hfg : f /ₘ g = 0 · rw [hfg, natDegree_zero] rw [divByMonic_eq_zero_iff hg] at hfg rw [tsub_eq_zero_iff_le.mpr (natDegree_le_natDegree <| le_of_lt hfg)] have hgf := hfg rw [divByMonic_eq_zero_iff hg] at hgf push_neg at hgf have := degree_add_divByMonic hg hgf have hf : f ≠ 0 := by intro hf apply hfg rw [hf, zero_divByMonic] rw [degree_eq_natDegree hf, degree_eq_natDegree hg.ne_zero, degree_eq_natDegree hfg, ← Nat.cast_add, Nat.cast_inj] at this rw [← this, add_tsub_cancel_left] theorem div_modByMonic_unique {f g} (q r : R[X]) (hg : Monic g) (h : r + g * q = f ∧ degree r < degree g) : f /ₘ g = q ∧ f %ₘ g = r := by nontriviality R have h₁ : r - f %ₘ g = -g * (q - f /ₘ g) := eq_of_sub_eq_zero (by rw [← sub_eq_zero_of_eq (h.1.trans (modByMonic_add_div f hg).symm)] simp [mul_add, sub_eq_add_neg, add_comm, add_left_comm, add_assoc]) have h₂ : degree (r - f %ₘ g) = degree (g * (q - f /ₘ g)) := by simp [h₁] have h₄ : degree (r - f %ₘ g) < degree g := calc degree (r - f %ₘ g) ≤ max (degree r) (degree (f %ₘ g)) := degree_sub_le _ _ _ < degree g := max_lt_iff.2 ⟨h.2, degree_modByMonic_lt _ hg⟩ have h₅ : q - f /ₘ g = 0 := _root_.by_contradiction fun hqf => not_le_of_gt h₄ <| calc degree g ≤ degree g + degree (q - f /ₘ g) := by rw [degree_eq_natDegree hg.ne_zero, degree_eq_natDegree hqf] norm_cast exact Nat.le_add_right _ _ _ = degree (r - f %ₘ g) := by rw [h₂, degree_mul']; simpa [Monic.def.1 hg] exact ⟨Eq.symm <| eq_of_sub_eq_zero h₅, Eq.symm <| eq_of_sub_eq_zero <| by simpa [h₅] using h₁⟩ theorem map_mod_divByMonic [Ring S] (f : R →+* S) (hq : Monic q) : (p /ₘ q).map f = p.map f /ₘ q.map f ∧ (p %ₘ q).map f = p.map f %ₘ q.map f := by nontriviality S haveI : Nontrivial R := f.domain_nontrivial have : map f p /ₘ map f q = map f (p /ₘ q) ∧ map f p %ₘ map f q = map f (p %ₘ q) := div_modByMonic_unique ((p /ₘ q).map f) _ (hq.map f) ⟨Eq.symm <| by rw [← Polynomial.map_mul, ← Polynomial.map_add, modByMonic_add_div _ hq], calc _ ≤ degree (p %ₘ q) := degree_map_le _ < degree q := degree_modByMonic_lt _ hq _ = _ := Eq.symm <| degree_map_eq_of_leadingCoeff_ne_zero _ (by rw [Monic.def.1 hq, f.map_one]; exact one_ne_zero)⟩ exact ⟨this.1.symm, this.2.symm⟩ theorem map_divByMonic [Ring S] (f : R →+* S) (hq : Monic q) : (p /ₘ q).map f = p.map f /ₘ q.map f := (map_mod_divByMonic f hq).1 theorem map_modByMonic [Ring S] (f : R →+* S) (hq : Monic q) : (p %ₘ q).map f = p.map f %ₘ q.map f := (map_mod_divByMonic f hq).2 theorem modByMonic_eq_zero_iff_dvd (hq : Monic q) : p %ₘ q = 0 ↔ q ∣ p := ⟨fun h => by rw [← modByMonic_add_div p hq, h, zero_add]; exact dvd_mul_right _ _, fun h => by nontriviality R obtain ⟨r, hr⟩ := exists_eq_mul_right_of_dvd h by_contra hpq0 have hmod : p %ₘ q = q * (r - p /ₘ q) := by rw [modByMonic_eq_sub_mul_div _ hq, mul_sub, ← hr] have : degree (q * (r - p /ₘ q)) < degree q := hmod ▸ degree_modByMonic_lt _ hq have hrpq0 : leadingCoeff (r - p /ₘ q) ≠ 0 := fun h => hpq0 <| leadingCoeff_eq_zero.1 (by rw [hmod, leadingCoeff_eq_zero.1 h, mul_zero, leadingCoeff_zero]) have hlc : leadingCoeff q * leadingCoeff (r - p /ₘ q) ≠ 0 := by rwa [Monic.def.1 hq, one_mul] rw [degree_mul' hlc, degree_eq_natDegree hq.ne_zero, degree_eq_natDegree (mt leadingCoeff_eq_zero.2 hrpq0)] at this exact not_lt_of_ge (Nat.le_add_right _ _) (WithBot.coe_lt_coe.1 this)⟩ /-- See `Polynomial.mul_self_modByMonic` for the other multiplication order. That version, unlike this one, requires commutativity. -/ @[simp] lemma self_mul_modByMonic (hq : q.Monic) : (q * p) %ₘ q = 0 := by rw [modByMonic_eq_zero_iff_dvd hq] exact dvd_mul_right q p theorem map_dvd_map [Ring S] (f : R →+* S) (hf : Function.Injective f) {x y : R[X]} (hx : x.Monic) : x.map f ∣ y.map f ↔ x ∣ y := by rw [← modByMonic_eq_zero_iff_dvd hx, ← modByMonic_eq_zero_iff_dvd (hx.map f), ← map_modByMonic f hx] exact ⟨fun H => map_injective f hf <| by rw [H, Polynomial.map_zero], fun H => by rw [H, Polynomial.map_zero]⟩ @[simp] theorem modByMonic_one (p : R[X]) : p %ₘ 1 = 0 := (modByMonic_eq_zero_iff_dvd (by convert monic_one (R := R))).2 (one_dvd _) @[simp] theorem divByMonic_one (p : R[X]) : p /ₘ 1 = p := by conv_rhs => rw [← modByMonic_add_div p monic_one]; simp theorem sum_modByMonic_coeff (hq : q.Monic) {n : ℕ} (hn : q.degree ≤ n) : (∑ i : Fin n, monomial i ((p %ₘ q).coeff i)) = p %ₘ q := by nontriviality R exact (sum_fin (fun i c => monomial i c) (by simp) ((degree_modByMonic_lt _ hq).trans_le hn)).trans (sum_monomial_eq _) theorem mul_divByMonic_cancel_left (p : R[X]) {q : R[X]} (hmo : q.Monic) : q * p /ₘ q = p := by nontriviality R refine (div_modByMonic_unique _ 0 hmo ⟨by rw [zero_add], ?_⟩).1 rw [degree_zero] exact Ne.bot_lt fun h => hmo.ne_zero (degree_eq_bot.1 h) lemma coeff_divByMonic_X_sub_C_rec (p : R[X]) (a : R) (n : ℕ) : (p /ₘ (X - C a)).coeff n = coeff p (n + 1) + a * (p /ₘ (X - C a)).coeff (n + 1) := by nontriviality R have := monic_X_sub_C a set q := p /ₘ (X - C a) rw [← p.modByMonic_add_div this] have : degree (p %ₘ (X - C a)) < ↑(n + 1) := degree_X_sub_C a ▸ p.degree_modByMonic_lt this |>.trans_le <| WithBot.coe_le_coe.mpr le_add_self simp [q, sub_mul, add_sub, coeff_eq_zero_of_degree_lt this] theorem coeff_divByMonic_X_sub_C (p : R[X]) (a : R) (n : ℕ) : (p /ₘ (X - C a)).coeff n = ∑ i ∈ Icc (n + 1) p.natDegree, a ^ (i - (n + 1)) * p.coeff i := by wlog h : p.natDegree ≤ n generalizing n · refine Nat.decreasingInduction' (fun n hn _ ih ↦ ?_) (le_of_not_ge h) ?_ · rw [coeff_divByMonic_X_sub_C_rec, ih, eq_comm, Icc_eq_cons_Ioc (Nat.succ_le.mpr hn), sum_cons, Nat.sub_self, pow_zero, one_mul, mul_sum] congr 1; refine sum_congr ?_ fun i hi ↦ ?_ · ext; simp [Nat.succ_le] rw [← mul_assoc, ← pow_succ', eq_comm, i.sub_succ', Nat.sub_add_cancel] apply Nat.le_sub_of_add_le rw [add_comm]; exact (mem_Icc.mp hi).1 · exact this _ le_rfl rw [Icc_eq_empty (Nat.lt_succ.mpr h).not_ge, sum_empty] nontriviality R by_cases hp : p.natDegree = 0 · rw [(divByMonic_eq_zero_iff <| monic_X_sub_C a).mpr, coeff_zero] apply degree_lt_degree; rw [hp, natDegree_X_sub_C]; simp · apply coeff_eq_zero_of_natDegree_lt rw [natDegree_divByMonic p (monic_X_sub_C a), natDegree_X_sub_C] exact (Nat.pred_lt hp).trans_le h variable (R) in theorem not_isField : ¬IsField R[X] := by nontriviality R intro h letI := h.toField simpa using congr_arg natDegree (monic_X.eq_one_of_isUnit <| monic_X (R := R).ne_zero.isUnit) section multiplicity /-- An algorithm for deciding polynomial divisibility. The algorithm is "compute `p %ₘ q` and compare to `0`". See `Polynomial.modByMonic` for the algorithm that computes `%ₘ`. -/ def decidableDvdMonic [DecidableEq R] (p : R[X]) (hq : Monic q) : Decidable (q ∣ p) := decidable_of_iff (p %ₘ q = 0) (modByMonic_eq_zero_iff_dvd hq) theorem finiteMultiplicity_X_sub_C (a : R) (h0 : p ≠ 0) : FiniteMultiplicity (X - C a) p := by haveI := Nontrivial.of_polynomial_ne h0 refine finiteMultiplicity_of_degree_pos_of_monic ?_ (monic_X_sub_C _) h0 rw [degree_X_sub_C] decide /- TODO: stripping out classical for decidability instance parameter might make for better ergonomics -/ /-- The largest power of `X - C a` which divides `p`. This *could be* computable via the divisibility algorithm `Polynomial.decidableDvdMonic`, as shown by `Polynomial.rootMultiplicity_eq_nat_find_of_nonzero` which has a computable RHS. -/ def rootMultiplicity (a : R) (p : R[X]) : ℕ := letI := Classical.decEq R if h0 : p = 0 then 0 else let _ : DecidablePred fun n : ℕ => ¬(X - C a) ^ (n + 1) ∣ p := fun n => have := decidableDvdMonic p ((monic_X_sub_C a).pow (n + 1)) inferInstanceAs (Decidable ¬_) Nat.find (finiteMultiplicity_X_sub_C a h0) theorem rootMultiplicity_eq_nat_find_of_nonzero [DecidableEq R] {p : R[X]} (p0 : p ≠ 0) {a : R} : -- `decidableDvdMonic` can't be an instance, so we inline it here. letI : DecidablePred fun n : ℕ => ¬(X - C a) ^ (n + 1) ∣ p := fun n => have := decidableDvdMonic p ((monic_X_sub_C a).pow (n + 1)) inferInstanceAs (Decidable ¬_) rootMultiplicity a p = Nat.find (finiteMultiplicity_X_sub_C a p0) := by dsimp [rootMultiplicity] cases Subsingleton.elim ‹DecidableEq R› (Classical.decEq R) rw [dif_neg p0] theorem rootMultiplicity_eq_multiplicity [DecidableEq R] (p : R[X]) (a : R) : rootMultiplicity a p = if p = 0 then 0 else multiplicity (X - C a) p := by simp only [rootMultiplicity, multiplicity, emultiplicity] split · rfl rename_i h simp only [finiteMultiplicity_X_sub_C a h, ↓reduceDIte] rw [← ENat.some_eq_coe, WithTop.untopD_coe] congr @[simp] theorem rootMultiplicity_zero {x : R} : rootMultiplicity x 0 = 0 := dif_pos rfl @[simp] theorem rootMultiplicity_C (r a : R) : rootMultiplicity a (C r) = 0 := by cases subsingleton_or_nontrivial R · rw [Subsingleton.elim (C r) 0, rootMultiplicity_zero] classical rw [rootMultiplicity_eq_multiplicity] split_ifs with hr · rfl have h : natDegree (C r) < natDegree (X - C a) := by simp simp_rw [multiplicity_eq_zero.mpr ((monic_X_sub_C a).not_dvd_of_natDegree_lt hr h)] theorem pow_rootMultiplicity_dvd (p : R[X]) (a : R) : (X - C a) ^ rootMultiplicity a p ∣ p := letI := Classical.decEq R if h : p = 0 then by simp [h] else by classical rw [rootMultiplicity_eq_multiplicity, if_neg h]; apply pow_multiplicity_dvd theorem pow_mul_divByMonic_rootMultiplicity_eq (p : R[X]) (a : R) : (X - C a) ^ rootMultiplicity a p * (p /ₘ (X - C a) ^ rootMultiplicity a p) = p := by have : Monic ((X - C a) ^ rootMultiplicity a p) := (monic_X_sub_C _).pow _ conv_rhs => rw [← modByMonic_add_div p this, (modByMonic_eq_zero_iff_dvd this).2 (pow_rootMultiplicity_dvd _ _)] simp theorem exists_eq_pow_rootMultiplicity_mul_and_not_dvd (p : R[X]) (hp : p ≠ 0) (a : R) : ∃ q : R[X], p = (X - C a) ^ p.rootMultiplicity a * q ∧ ¬ (X - C a) ∣ q := by classical rw [rootMultiplicity_eq_multiplicity, if_neg hp] apply (finiteMultiplicity_X_sub_C a hp).exists_eq_pow_mul_and_not_dvd end multiplicity end Ring section CommRing variable [CommRing R] {p p₁ p₂ q : R[X]} @[simp] theorem modByMonic_X_sub_C_eq_C_eval (p : R[X]) (a : R) : p %ₘ (X - C a) = C (p.eval a) := by nontriviality R have h : (p %ₘ (X - C a)).eval a = p.eval a := by rw [modByMonic_eq_sub_mul_div _ (monic_X_sub_C a), eval_sub, eval_mul, eval_sub, eval_X, eval_C, sub_self, zero_mul, sub_zero] have : degree (p %ₘ (X - C a)) < 1 := degree_X_sub_C a ▸ degree_modByMonic_lt p (monic_X_sub_C a) have : degree (p %ₘ (X - C a)) ≤ 0 := by revert this cases degree (p %ₘ (X - C a)) · exact fun _ => bot_le · exact fun h => WithBot.coe_le_coe.2 (Nat.le_of_lt_succ (WithBot.coe_lt_coe.1 h)) rw [eq_C_of_degree_le_zero this, eval_C] at h rw [eq_C_of_degree_le_zero this, h] theorem mul_divByMonic_eq_iff_isRoot : (X - C a) * (p /ₘ (X - C a)) = p ↔ IsRoot p a := .trans ⟨fun h => by rw [← h, eval_mul, eval_sub, eval_X, eval_C, sub_self, zero_mul], fun h => by conv_rhs => rw [← modByMonic_add_div p (monic_X_sub_C a)] rw [modByMonic_X_sub_C_eq_C_eval, h, C_0, zero_add]⟩ IsRoot.def.symm theorem dvd_iff_isRoot : X - C a ∣ p ↔ IsRoot p a := ⟨fun h => by rwa [← modByMonic_eq_zero_iff_dvd (monic_X_sub_C _), modByMonic_X_sub_C_eq_C_eval, ← C_0, C_inj] at h, fun h => ⟨p /ₘ (X - C a), by rw [mul_divByMonic_eq_iff_isRoot.2 h]⟩⟩ theorem X_sub_C_dvd_sub_C_eval : X - C a ∣ p - C (p.eval a) := by rw [dvd_iff_isRoot, IsRoot, eval_sub, eval_C, sub_self] -- TODO: generalize this to Ring. In general, 0 can be replaced by any element in the center of R. theorem modByMonic_X (p : R[X]) : p %ₘ X = C (p.eval 0) := by rw [← modByMonic_X_sub_C_eq_C_eval, C_0, sub_zero] theorem eval₂_modByMonic_eq_self_of_root [CommRing S] {f : R →+* S} {p q : R[X]} (hq : q.Monic) {x : S} (hx : q.eval₂ f x = 0) : (p %ₘ q).eval₂ f x = p.eval₂ f x := by rw [modByMonic_eq_sub_mul_div p hq, eval₂_sub, eval₂_mul, hx, zero_mul, sub_zero] theorem sub_dvd_eval_sub (a b : R) (p : R[X]) : a - b ∣ p.eval a - p.eval b := by suffices X - C b ∣ p - C (p.eval b) by simpa only [coe_evalRingHom, eval_sub, eval_X, eval_C] using (_root_.map_dvd (evalRingHom a)) this simp [dvd_iff_isRoot] @[simp] theorem rootMultiplicity_eq_zero_iff {p : R[X]} {x : R} : rootMultiplicity x p = 0 ↔ IsRoot p x → p = 0 := by classical simp only [rootMultiplicity_eq_multiplicity, ite_eq_left_iff, multiplicity_eq_zero, dvd_iff_isRoot, not_imp_not] theorem rootMultiplicity_eq_zero {p : R[X]} {x : R} (h : ¬IsRoot p x) : rootMultiplicity x p = 0 := rootMultiplicity_eq_zero_iff.2 fun h' => (h h').elim @[simp] theorem rootMultiplicity_pos' {p : R[X]} {x : R} : 0 < rootMultiplicity x p ↔ p ≠ 0 ∧ IsRoot p x := by rw [pos_iff_ne_zero, Ne, rootMultiplicity_eq_zero_iff, Classical.not_imp, and_comm] theorem rootMultiplicity_pos {p : R[X]} (hp : p ≠ 0) {x : R} : 0 < rootMultiplicity x p ↔ IsRoot p x := rootMultiplicity_pos'.trans (and_iff_right hp) theorem eval_divByMonic_pow_rootMultiplicity_ne_zero {p : R[X]} (a : R) (hp : p ≠ 0) : eval a (p /ₘ (X - C a) ^ rootMultiplicity a p) ≠ 0 := by classical haveI : Nontrivial R := Nontrivial.of_polynomial_ne hp rw [Ne, ← IsRoot, ← dvd_iff_isRoot] rintro ⟨q, hq⟩ have := pow_mul_divByMonic_rootMultiplicity_eq p a rw [hq, ← mul_assoc, ← pow_succ, rootMultiplicity_eq_multiplicity, if_neg hp] at this exact (finiteMultiplicity_of_degree_pos_of_monic (show (0 : WithBot ℕ) < degree (X - C a) by rw [degree_X_sub_C]; decide) (monic_X_sub_C _) hp).not_pow_dvd_of_multiplicity_lt (Nat.lt_succ_self _) (dvd_of_mul_right_eq _ this) /-- See `Polynomial.self_mul_modByMonic` for the other multiplication order. This version, unlike that one, requires commutativity. -/ @[simp] lemma mul_self_modByMonic (hq : q.Monic) : (p * q) %ₘ q = 0 := by rw [modByMonic_eq_zero_iff_dvd hq] exact dvd_mul_left q p lemma modByMonic_eq_of_dvd_sub (hq : q.Monic) (h : q ∣ p₁ - p₂) : p₁ %ₘ q = p₂ %ₘ q := by nontriviality R obtain ⟨f, sub_eq⟩ := h refine (div_modByMonic_unique (p₂ /ₘ q + f) _ hq ⟨?_, degree_modByMonic_lt _ hq⟩).2 rw [sub_eq_iff_eq_add.mp sub_eq, mul_add, ← add_assoc, modByMonic_add_div _ hq, add_comm] lemma add_modByMonic (p₁ p₂ : R[X]) : (p₁ + p₂) %ₘ q = p₁ %ₘ q + p₂ %ₘ q := by by_cases hq : q.Monic · rcases subsingleton_or_nontrivial R with hR | hR · simp only [eq_iff_true_of_subsingleton] · exact (div_modByMonic_unique (p₁ /ₘ q + p₂ /ₘ q) _ hq ⟨by rw [mul_add, add_left_comm, add_assoc, modByMonic_add_div _ hq, ← add_assoc, add_comm (q * _), modByMonic_add_div _ hq], (degree_add_le _ _).trans_lt (max_lt (degree_modByMonic_lt _ hq) (degree_modByMonic_lt _ hq))⟩).2 · simp_rw [modByMonic_eq_of_not_monic _ hq] lemma neg_modByMonic (p q : R[X]) : (-p) %ₘ q = - (p %ₘ q) := by rw [eq_neg_iff_add_eq_zero, ← add_modByMonic, neg_add_cancel, zero_modByMonic] lemma sub_modByMonic (p₁ p₂ q : R[X]) : (p₁ - p₂) %ₘ q = p₁ %ₘ q - p₂ %ₘ q := by simp [sub_eq_add_neg, add_modByMonic, neg_modByMonic] lemma eval_divByMonic_eq_trailingCoeff_comp {p : R[X]} {t : R} : (p /ₘ (X - C t) ^ p.rootMultiplicity t).eval t = (p.comp (X + C t)).trailingCoeff := by obtain rfl | hp := eq_or_ne p 0 · rw [zero_divByMonic, eval_zero, zero_comp, trailingCoeff_zero] have mul_eq := p.pow_mul_divByMonic_rootMultiplicity_eq t set m := p.rootMultiplicity t set g := p /ₘ (X - C t) ^ m have : (g.comp (X + C t)).coeff 0 = g.eval t := by rw [coeff_zero_eq_eval_zero, eval_comp, eval_add, eval_X, eval_C, zero_add] rw [← congr_arg (comp · <| X + C t) mul_eq, mul_comp, pow_comp, sub_comp, X_comp, C_comp, add_sub_cancel_right, ← reverse_leadingCoeff, reverse_X_pow_mul, reverse_leadingCoeff, trailingCoeff, Nat.le_zero.1 (natTrailingDegree_le_of_ne_zero <| this ▸ eval_divByMonic_pow_rootMultiplicity_ne_zero t hp), this] /-- The multiplicity of `a` as root of a nonzero polynomial `p` is at least `n` iff `(X - a) ^ n` divides `p`. -/ lemma le_rootMultiplicity_iff (p0 : p ≠ 0) {a : R} {n : ℕ} : n ≤ rootMultiplicity a p ↔ (X - C a) ^ n ∣ p := by classical simp_rw [rootMultiplicity_eq_nat_find_of_nonzero p0, @Nat.le_find_iff _ (_), Classical.not_not] refine ⟨fun h => ?_, fun h m hm => (pow_dvd_pow _ hm).trans h⟩ rcases n with - | n · rw [pow_zero] apply one_dvd · exact h n n.lt_succ_self lemma rootMultiplicity_le_iff (p0 : p ≠ 0) (a : R) (n : ℕ) : rootMultiplicity a p ≤ n ↔ ¬(X - C a) ^ (n + 1) ∣ p := by rw [← (le_rootMultiplicity_iff p0).not, not_le, Nat.lt_add_one_iff] /-- The multiplicity of `p + q` is at least the minimum of the multiplicities. -/ lemma rootMultiplicity_add {p q : R[X]} (a : R) (hzero : p + q ≠ 0) : min (rootMultiplicity a p) (rootMultiplicity a q) ≤ rootMultiplicity a (p + q) := by rw [le_rootMultiplicity_iff hzero] exact min_pow_dvd_add (pow_rootMultiplicity_dvd p a) (pow_rootMultiplicity_dvd q a) lemma le_rootMultiplicity_mul {p q : R[X]} (x : R) (hpq : p * q ≠ 0) : rootMultiplicity x p + rootMultiplicity x q ≤ rootMultiplicity x (p * q) := by rw [le_rootMultiplicity_iff hpq, pow_add] gcongr <;> apply pow_rootMultiplicity_dvd lemma rootMultiplicity_le_rootMultiplicity_of_dvd {p q : R[X]} (hq : q ≠ 0) (hpq : p ∣ q) (x : R) : p.rootMultiplicity x ≤ q.rootMultiplicity x := by obtain ⟨_, rfl⟩ := hpq exact Nat.le_of_add_right_le <| le_rootMultiplicity_mul x hq lemma pow_rootMultiplicity_not_dvd (p0 : p ≠ 0) (a : R) : ¬(X - C a) ^ (rootMultiplicity a p + 1) ∣ p := by rw [← rootMultiplicity_le_iff p0] /-- See `Polynomial.rootMultiplicity_eq_natTrailingDegree` for the general case. -/ lemma rootMultiplicity_eq_natTrailingDegree' : p.rootMultiplicity 0 = p.natTrailingDegree := by by_cases h : p = 0 · simp only [h, rootMultiplicity_zero, natTrailingDegree_zero] refine le_antisymm ?_ ?_ · rw [rootMultiplicity_le_iff h, map_zero, sub_zero, X_pow_dvd_iff, not_forall] exact ⟨p.natTrailingDegree, fun h' ↦ trailingCoeff_nonzero_iff_nonzero.2 h <| h' <| Nat.lt.base _⟩ · rw [le_rootMultiplicity_iff h, map_zero, sub_zero, X_pow_dvd_iff] exact fun _ ↦ coeff_eq_zero_of_lt_natTrailingDegree /-- Division by a monic polynomial doesn't change the leading coefficient. -/ lemma leadingCoeff_divByMonic_of_monic (hmonic : q.Monic) (hdegree : q.degree ≤ p.degree) : (p /ₘ q).leadingCoeff = p.leadingCoeff := by nontriviality have h : q.leadingCoeff * (p /ₘ q).leadingCoeff ≠ 0 := by simpa [divByMonic_eq_zero_iff hmonic, hmonic.leadingCoeff, Nat.WithBot.one_le_iff_zero_lt] using hdegree nth_rw 2 [← modByMonic_add_div p hmonic] rw [leadingCoeff_add_of_degree_lt, leadingCoeff_monic_mul hmonic] rw [degree_mul' h, degree_add_divByMonic hmonic hdegree] exact (degree_modByMonic_lt p hmonic).trans_le hdegree variable [IsDomain R] lemma degree_eq_one_of_irreducible_of_root (hi : Irreducible p) {x : R} (hx : IsRoot p x) : degree p = 1 := let ⟨g, hg⟩ := dvd_iff_isRoot.2 hx have : IsUnit (X - C x) ∨ IsUnit g := hi.isUnit_or_isUnit hg this.elim (fun h => by have h₁ : degree (X - C x) = 1 := degree_X_sub_C x have h₂ : degree (X - C x) = 0 := degree_eq_zero_of_isUnit h rw [h₁] at h₂; exact absurd h₂ (by decide)) fun hgu => by rw [hg, degree_mul, degree_X_sub_C, degree_eq_zero_of_isUnit hgu, add_zero] lemma _root_.Irreducible.not_isRoot_of_natDegree_ne_one (hi : Irreducible p) (hdeg : p.natDegree ≠ 1) {x : R} : ¬p.IsRoot x := fun hr ↦ hdeg <| natDegree_eq_of_degree_eq_some <| degree_eq_one_of_irreducible_of_root hi hr lemma _root_.Irreducible.isRoot_eq_bot_of_natDegree_ne_one (hi : Irreducible p) (hdeg : p.natDegree ≠ 1) : p.IsRoot = ⊥ := le_bot_iff.mp fun _ ↦ hi.not_isRoot_of_natDegree_ne_one hdeg lemma _root_.Irreducible.subsingleton_isRoot [IsLeftCancelMulZero R] (hi : Irreducible p) : { x | p.IsRoot x }.Subsingleton := fun _ hx ↦ (subsingleton_isRoot_of_natDegree_eq_one <| natDegree_eq_of_degree_eq_some <| degree_eq_one_of_irreducible_of_root hi hx) hx lemma leadingCoeff_divByMonic_X_sub_C (p : R[X]) (hp : degree p ≠ 0) (a : R) : leadingCoeff (p /ₘ (X - C a)) = leadingCoeff p := by nontriviality rcases hp.lt_or_gt with hd | hd · rw [degree_eq_bot.mp <| Nat.WithBot.lt_zero_iff.mp hd, zero_divByMonic] refine leadingCoeff_divByMonic_of_monic (monic_X_sub_C a) ?_ rwa [degree_X_sub_C, Nat.WithBot.one_le_iff_zero_lt] lemma eq_of_dvd_of_natDegree_le_of_leadingCoeff {p q : R[X]} (hpq : p ∣ q) (h₁ : q.natDegree ≤ p.natDegree) (h₂ : p.leadingCoeff = q.leadingCoeff) : p = q := by rcases eq_or_ne q 0 with rfl | hq · simpa using h₂ replace h₁ := (natDegree_le_of_dvd hpq hq).antisymm h₁ obtain ⟨u, rfl⟩ := hpq rw [mul_ne_zero_iff] at hq rw [natDegree_mul hq.1 hq.2, left_eq_add] at h₁ rw [eq_C_of_natDegree_eq_zero h₁, leadingCoeff_mul, leadingCoeff_C, eq_comm, mul_eq_left₀ (leadingCoeff_ne_zero.mpr hq.1)] at h₂ rw [eq_C_of_natDegree_eq_zero h₁, h₂, map_one, mul_one] lemma associated_of_dvd_of_natDegree_le_of_leadingCoeff {p q : R[X]} (hpq : p ∣ q) (h₁ : q.natDegree ≤ p.natDegree) (h₂ : q.leadingCoeff ∣ p.leadingCoeff) : Associated p q := have ⟨r, hr⟩ := hpq have ⟨u, hu⟩ := associated_of_dvd_dvd ⟨leadingCoeff r, hr ▸ leadingCoeff_mul p r⟩ h₂ ⟨Units.map C.toMonoidHom u, eq_of_dvd_of_natDegree_le_of_leadingCoeff (by rwa [Units.mul_right_dvd]) (by simpa [natDegree_mul_C] using h₁) (by simpa using hu)⟩ lemma associated_of_dvd_of_natDegree_le {K} [Field K] {p q : K[X]} (hpq : p ∣ q) (hq : q ≠ 0) (h₁ : q.natDegree ≤ p.natDegree) : Associated p q := associated_of_dvd_of_natDegree_le_of_leadingCoeff hpq h₁ (IsUnit.dvd (by rwa [← leadingCoeff_ne_zero, ← isUnit_iff_ne_zero] at hq)) lemma associated_of_dvd_of_degree_eq {K} [Field K] {p q : K[X]} (hpq : p ∣ q) (h₁ : p.degree = q.degree) : Associated p q := (Classical.em (q = 0)).elim (fun hq ↦ (show p = q by simpa [hq] using h₁) ▸ Associated.refl p) (associated_of_dvd_of_natDegree_le hpq · (natDegree_le_natDegree h₁.ge)) lemma eq_leadingCoeff_mul_of_monic_of_dvd_of_natDegree_le {R} [CommSemiring R] {p q : R[X]} (hp : p.Monic) (hdiv : p ∣ q) (hdeg : q.natDegree ≤ p.natDegree) : q = C q.leadingCoeff * p := by obtain ⟨r, hr⟩ := hdiv obtain rfl | hq := eq_or_ne q 0; · simp have rzero : r ≠ 0 := fun h => by simp [h, hq] at hr rw [hr, natDegree_mul'] at hdeg; swap · rw [hp.leadingCoeff, one_mul, leadingCoeff_ne_zero] exact rzero rw [mul_comm, @eq_C_of_natDegree_eq_zero _ _ r] at hr · convert hr convert leadingCoeff_C (coeff r 0) using 1 rw [hr, leadingCoeff_mul_monic hp] · exact (add_right_inj _).1 (le_antisymm hdeg <| Nat.le.intro rfl) lemma eq_of_monic_of_dvd_of_natDegree_le {R} [CommSemiring R] {p q : R[X]} (hp : p.Monic) (hq : q.Monic) (hdiv : p ∣ q) (hdeg : q.natDegree ≤ p.natDegree) : q = p := by convert eq_leadingCoeff_mul_of_monic_of_dvd_of_natDegree_le hp hdiv hdeg rw [hq.leadingCoeff, C_1, one_mul] end CommRing end Polynomial
.lake/packages/mathlib/Mathlib/Algebra/Polynomial/CoeffList.lean
import Mathlib.Algebra.Polynomial.Degree.Definitions import Mathlib.Algebra.Polynomial.EraseLead import Mathlib.Data.List.Range /-! # A list of coefficients of a polynomial ## Definition * `coeffList f`: a `List` of the coefficients, from leading term down to constant term. * `coeffList 0` is defined to be `[]`. This is useful for talking about polynomials in terms of list operations. It is "redundant" data in the sense that `Polynomial` is already a `Finsupp` (of its coefficients), and `Polynomial.coeff` turns this into a function, and these have exactly the same data as `coeffList`. The difference is that `coeffList` is intended for working together with list operations: getting `List.head`, comparing adjacent coefficients with each other, or anything that involves induction on Polynomials by dropping the leading term (which is `Polynomial.eraseLead`). Note that `coeffList` _starts_ with the highest-degree terms and _ends_ with the constant term. This might seem backwards in the sense that `Polynomial.coeff` and `List.get!` are reversed to one another, but it means that induction on `List`s is the same as induction on `Polynomial.leadingCoeff`. The most significant theorem here is `coeffList_eraseLead`, which says that `coeffList P` can be written as `leadingCoeff P :: List.replicate k 0 ++ coeffList P.eraseLead`. That is, the list of coefficients starts with the leading coefficient, followed by some number of zeros, and then the coefficients of `P.eraseLead`. -/ namespace Polynomial variable {R : Type*} section Semiring variable [Semiring R] /-- The list of coefficients starting from the leading term down to the constant term. -/ def coeffList (P : R[X]) : List R := (List.range P.degree.succ).reverse.map P.coeff variable {P : R[X]} variable (R) in @[simp] theorem coeffList_zero : (0 : R[X]).coeffList = [] := by simp [coeffList] /-- Only the zero polynomial has no coefficients. -/ @[simp] theorem coeffList_eq_nil {P : R[X]} : P.coeffList = [] ↔ P = 0 := by simp [coeffList] @[simp] theorem coeffList_C {x : R} (h : x ≠ 0) : (C x).coeffList = [x] := by simp [coeffList, List.range_succ, degree_eq_natDegree (C_ne_zero.mpr h)] theorem coeffList_eq_cons_leadingCoeff (h : P ≠ 0) : ∃ ls, P.coeffList = P.leadingCoeff :: ls := by simp [coeffList, List.range_succ, withBotSucc_degree_eq_natDegree_add_one h] @[simp] theorem head?_coeffList (h : P ≠ 0) : P.coeffList.head? = P.leadingCoeff := (coeffList_eq_cons_leadingCoeff h).casesOn fun _ ↦ (Eq.symm · ▸ rfl) @[simp] theorem head_coeffList (P : R[X]) (hP) : P.coeffList.head hP = P.leadingCoeff := let h := coeffList_eq_nil.not.mp hP (coeffList_eq_cons_leadingCoeff h).casesOn fun _ _ ↦ Option.some.injEq _ _ ▸ List.head?_eq_head _ ▸ head?_coeffList h theorem length_coeffList_eq_withBotSucc_degree (P : R[X]) : P.coeffList.length = P.degree.succ := by simp [coeffList] @[simp] theorem length_coeffList_eq_ite [DecidableEq R] (P : R[X]) : P.coeffList.length = if P = 0 then 0 else P.natDegree + 1 := by by_cases h : P = 0 <;> simp [h, coeffList, withBotSucc_degree_eq_natDegree_add_one] theorem leadingCoeff_cons_eraseLead (h : P.nextCoeff ≠ 0) : P.leadingCoeff :: P.eraseLead.coeffList = P.coeffList := by have h₂ := ne_zero_of_natDegree_gt (natDegree_pos_of_nextCoeff_ne_zero h) have h₃ := mt nextCoeff_eq_zero_of_eraseLead_eq_zero h simpa [natDegree_eraseLead_add_one h, coeffList, withBotSucc_degree_eq_natDegree_add_one h₂, withBotSucc_degree_eq_natDegree_add_one h₃, List.range_succ] using (Polynomial.eraseLead_coeff_of_ne · ·.ne) @[simp] theorem coeffList_monomial {x : R} (hx : x ≠ 0) (n : ℕ) : (monomial n x).coeffList = x :: List.replicate n 0 := by have h := mt (Polynomial.monomial_eq_zero_iff x n).mp hx apply List.ext_get (by classical simp [hx]) rintro (_ | k) _ h₁ · exact (coeffList_eq_cons_leadingCoeff h).rec (by simp_all) · rw [List.length_cons, List.length_replicate] at h₁ have : ((monomial n) x).natDegree.succ = n + 1 := by simp [Polynomial.natDegree_monomial_eq n hx] simpa [coeffList, withBotSucc_degree_eq_natDegree_add_one h] using Polynomial.coeff_monomial_of_ne _ (by cutsat) /- Coefficients of a polynomial `P` are always the leading coefficient, some number of zeros, and then `coeffList P.eraseLead`. -/ theorem coeffList_eraseLead (h : P ≠ 0) : P.coeffList = P.leadingCoeff :: (.replicate (P.natDegree - P.eraseLead.degree.succ) 0 ++ P.eraseLead.coeffList) := by by_cases hdp : P.natDegree = 0 · rw [eq_C_of_natDegree_eq_zero hdp] at h ⊢ simp [coeffList_C (C_ne_zero.mp h)] by_cases hep : P.eraseLead = 0 · have h₂ : .monomial P.natDegree P.leadingCoeff = P := by simpa [hep] using P.eraseLead_add_monomial_natDegree_leadingCoeff nth_rewrite 1 [← h₂] simp [coeffList_monomial (Polynomial.leadingCoeff_ne_zero.mpr h), hep] have h₁ := withBotSucc_degree_eq_natDegree_add_one h have h₂ := withBotSucc_degree_eq_natDegree_add_one hep obtain ⟨n, hn, hn2⟩ : ∃ d, P.natDegree = P.eraseLead.natDegree + 1 + d ∧ d = P.natDegree - P.eraseLead.degree.succ := by use P.natDegree - P.eraseLead.natDegree - 1 have := eraseLead_natDegree_le P cutsat rw [← hn2]; clear hn2 apply List.ext_getElem? rintro (_ | k) · obtain ⟨w,h⟩ := (coeffList_eq_cons_leadingCoeff h) simp_all simp only [coeffList, List.map_reverse] by_cases! hkd : P.natDegree + 1 ≤ k + 1 · rw [List.getElem?_eq_none] <;> simpa [hep, h] using by cutsat obtain ⟨dk, hdk⟩ := exists_add_of_le (Nat.le_of_lt_succ hkd) rw [List.getElem?_reverse (by simpa [withBotSucc_degree_eq_natDegree_add_one h] using hkd), List.getElem?_cons_succ, List.length_map, List.length_range, List.getElem?_map, List.getElem?_range (by cutsat), Option.map_some] conv_lhs => arg 1; equals P.eraseLead.coeff dk => rw [eraseLead_coeff_of_ne (f := P) dk (by cutsat)] congr cutsat by_cases! hkn : k < n · simpa [List.getElem?_append, hkn] using coeff_eq_zero_of_natDegree_lt (by cutsat) · rw [List.getElem?_append_right (List.length_replicate ▸ hkn), List.length_replicate, List.getElem?_reverse, List.getElem?_map] · rw [List.length_map, List.length_range, List.getElem?_range (by cutsat), Option.map_some] congr 2 cutsat · simpa using by cutsat end Semiring section Ring variable [Ring R] (P : R[X]) @[simp] theorem coeffList_neg : (-P).coeffList = P.coeffList.map (-·) := by by_cases hp : P = 0 · rw [hp, coeffList_zero, neg_zero, coeffList_zero, List.map_nil] · simp [coeffList] end Ring section NoZeroDivisors variable [Semiring R] [NoZeroDivisors R] (P : R[X]) theorem coeffList_C_mul {x : R} (hx : x ≠ 0) : (C x * P).coeffList = P.coeffList.map (x * ·) := by by_cases hp : P = 0 · simp [hp] · simp [coeffList, Polynomial.degree_C hx] end NoZeroDivisors end Polynomial
.lake/packages/mathlib/Mathlib/Algebra/Polynomial/CoeffMem.lean
import Mathlib.Algebra.Algebra.Operations import Mathlib.Algebra.Polynomial.Div import Mathlib.RingTheory.Ideal.Span /-! # Bounding the coefficients of the quotient and remainder of polynomials This file proves that, for polynomials `p q : R[X]`, the coefficients of `p /ₘ q` and `p %ₘ q` can be written as sums of products of coefficients of `p` and `q`. Precisely, we show that each summand needs at most one coefficient of `p` and `deg p` coefficients of `q`. -/ namespace Polynomial variable {ι R S : Type*} [CommRing R] [Ring S] [Algebra R S] local notation "deg("p")" => natDegree p local notation3 "coeffs("p")" => Set.range (coeff p) local notation3 "spanCoeffs("p")" => 1 ⊔ Submodule.span R coeffs(p) open Submodule Set in lemma coeff_divModByMonicAux_mem_span_pow_mul_span : ∀ (p q : S[X]) (hq : q.Monic) (i), (p.divModByMonicAux hq).1.coeff i ∈ spanCoeffs(q) ^ deg(p) * spanCoeffs(p) ∧ (p.divModByMonicAux hq).2.coeff i ∈ spanCoeffs(q) ^ deg(p) * spanCoeffs(p) | p, q, hq, i => by rw [divModByMonicAux] have H₀ (i) : p.coeff i ∈ spanCoeffs(q) ^ deg(p) * spanCoeffs(p) := by refine SetLike.le_def.mp ?_ <| subset_span <| mem_range_self i calc span R coeffs(p) _ = 1 ^ deg(p) * span R coeffs(p) := by simp _ ≤ spanCoeffs(q) ^ deg(p) * spanCoeffs(p) := by gcongr; exacts [le_sup_left, le_sup_right] split_ifs with hpq; swap · simpa using H₀ _ simp only [coeff_add, coeff_C_mul, coeff_X_pow] generalize hr : (p - q * (C p.leadingCoeff * X ^ (deg(p) - deg(q)))) = r by_cases hr' : r = 0 · simp only [mul_ite, mul_one, mul_zero, hr', divModByMonicAux, degree_zero, le_bot_iff, degree_eq_bot, ne_eq, not_true_eq_false, and_false, ↓reduceDIte, coeff_zero, add_zero, Submodule.zero_mem, and_true] split_ifs exacts [H₀ _, zero_mem _] have H : span R coeffs(r) ≤ span R coeffs(p) ⊔ span R coeffs(q) * span R coeffs(p) := by rw [span_le, ← hr] rintro _ ⟨i, rfl⟩ rw [coeff_sub, ← mul_assoc, coeff_mul_X_pow', coeff_mul_C] apply sub_mem · exact SetLike.le_def.mp le_sup_left (subset_span (mem_range_self _)) · split_ifs · refine SetLike.le_def.mp le_sup_right (mul_mem_mul ?_ ?_) <;> exact subset_span ⟨_, rfl⟩ · exact zero_mem _ have deg_r_lt_deg_p : deg(r) < deg(p) := natDegree_lt_natDegree hr' (hr ▸ div_wf_lemma hpq hq) have H'' := calc spanCoeffs(q) ^ deg(r) * spanCoeffs(r) _ ≤ spanCoeffs(q) ^ deg(r) * (1 ⊔ (span R coeffs(p) ⊔ span R coeffs(q) * span R coeffs(p))) := by gcongr _ ≤ spanCoeffs(q) ^ deg(r) * (spanCoeffs(q) * spanCoeffs(p)) := by gcongr simp only [sup_le_iff] refine ⟨one_le_mul le_sup_left le_sup_left, ?_, mul_le_mul' le_sup_right le_sup_right⟩ rw [Submodule.sup_mul, one_mul] exact le_sup_of_le_left le_sup_right _ = spanCoeffs(q) ^ (deg(r) + 1) * spanCoeffs(p) := by rw [pow_succ, mul_assoc] _ ≤ spanCoeffs(q) ^ deg(p) * spanCoeffs(p) := by gcongr; exacts [le_sup_left, deg_r_lt_deg_p] refine ⟨add_mem ?_ ?_, ?_⟩ · split_ifs <;> simp only [mul_one, mul_zero] exacts [H₀ _, zero_mem _] · exact H'' (coeff_divModByMonicAux_mem_span_pow_mul_span r _ hq i).1 · exact H'' (coeff_divModByMonicAux_mem_span_pow_mul_span _ _ hq i).2 termination_by p => deg(p) /-- For polynomials `p q : R[X]`, the coefficients of `p %ₘ q` can be written as sums of products of coefficients of `p` and `q`. Precisely, each summand needs at most one coefficient of `p` and `deg p` coefficients of `q`. -/ lemma coeff_modByMonic_mem_pow_natDegree_mul (p q : S[X]) (Mp : Submodule R S) (hp : ∀ i, p.coeff i ∈ Mp) (hp' : 1 ∈ Mp) (Mq : Submodule R S) (hq : ∀ i, q.coeff i ∈ Mq) (hq' : 1 ∈ Mq) (i : ℕ) : (p %ₘ q).coeff i ∈ Mq ^ p.natDegree * Mp := by delta modByMonic split_ifs with H · refine SetLike.le_def.mp ?_ (coeff_divModByMonicAux_mem_span_pow_mul_span (R := R) p q H i).2 gcongr <;> exact sup_le (by simpa) (by simpa [Submodule.span_le, Set.range_subset_iff]) · rw [← one_mul (p.coeff i), ← one_pow p.natDegree] exact Submodule.mul_mem_mul (Submodule.pow_mem_pow Mq hq' _) (hp i) /-- For polynomials `p q : R[X]`, the coefficients of `p /ₘ q` can be written as sums of products of coefficients of `p` and `q`. Precisely, each summand needs at most one coefficient of `p` and `deg p` coefficients of `q`. -/ lemma coeff_divByMonic_mem_pow_natDegree_mul (p q : S[X]) (Mp : Submodule R S) (hp : ∀ i, p.coeff i ∈ Mp) (hp' : 1 ∈ Mp) (Mq : Submodule R S) (hq : ∀ i, q.coeff i ∈ Mq) (hq' : 1 ∈ Mq) (i : ℕ) : (p /ₘ q).coeff i ∈ Mq ^ p.natDegree * Mp := by delta divByMonic split_ifs with H · refine SetLike.le_def.mp ?_ (coeff_divModByMonicAux_mem_span_pow_mul_span (R := R) p q H i).1 gcongr <;> exact sup_le (by simpa) (by simpa [Submodule.span_le, Set.range_subset_iff]) · simp variable [DecidableEq ι] {i j : ι} open Function Ideal in lemma idealSpan_range_update_divByMonic (hij : i ≠ j) (v : ι → R[X]) (hi : (v i).Monic) : span (Set.range (Function.update v j (v j %ₘ v i))) = span (Set.range v) := by rw [modByMonic_eq_sub_mul_div _ hi, mul_comm, ← smul_eq_mul, Ideal.span, Ideal.span, Submodule.span_range_update_sub_smul hij] end Polynomial
.lake/packages/mathlib/Mathlib/Algebra/Polynomial/Derivation.lean
import Mathlib.Algebra.Polynomial.AlgebraMap import Mathlib.Algebra.Polynomial.Derivative import Mathlib.Algebra.Polynomial.Module.AEval import Mathlib.RingTheory.Adjoin.Polynomial import Mathlib.RingTheory.Derivation.Basic /-! # Derivations of univariate polynomials In this file we prove that an `R`-derivation of `Polynomial R` is determined by its value on `X`. We also provide a constructor `Polynomial.mkDerivation` that builds a derivation from its value on `X`, and a linear equivalence `Polynomial.mkDerivationEquiv` between `A` and `Derivation (Polynomial R) A`. -/ noncomputable section namespace Polynomial section CommSemiring variable {R A : Type*} [CommSemiring R] /-- `Polynomial.derivative` as a derivation. -/ @[simps] def derivative' : Derivation R R[X] R[X] where toFun := derivative map_add' _ _ := derivative_add map_smul' := derivative_smul map_one_eq_zero' := derivative_one leibniz' f g := by simp [mul_comm, add_comm, derivative_mul] variable [AddCommMonoid A] [Module R A] [Module (Polynomial R) A] @[simp] theorem derivation_C (D : Derivation R R[X] A) (a : R) : D (C a) = 0 := D.map_algebraMap a @[simp] theorem C_smul_derivation_apply (D : Derivation R R[X] A) (a : R) (f : R[X]) : C a • D f = a • D f := by have : C a • D f = D (C a * f) := by simp rw [this, C_mul', D.map_smul] @[ext] theorem derivation_ext {D₁ D₂ : Derivation R R[X] A} (h : D₁ X = D₂ X) : D₁ = D₂ := Derivation.ext fun f => Derivation.eqOn_adjoin (Set.eqOn_singleton.2 h) <| by simp only [adjoin_X, Algebra.coe_top, Set.mem_univ] variable [IsScalarTower R (Polynomial R) A] variable (R) /-- The derivation on `R[X]` that takes the value `a` on `X`. -/ def mkDerivation : A →ₗ[R] Derivation R R[X] A where toFun := fun a ↦ (LinearMap.toSpanSingleton R[X] A a).compDer derivative' map_add' := fun a b ↦ by ext; simp map_smul' := fun t a ↦ by ext; simp lemma mkDerivation_apply (a : A) (f : R[X]) : mkDerivation R a f = derivative f • a := by rfl @[simp] theorem mkDerivation_X (a : A) : mkDerivation R a X = a := by simp [mkDerivation_apply] lemma mkDerivation_one_eq_derivative' : mkDerivation R (1 : R[X]) = derivative' := by ext : 1 simp [derivative'] lemma mkDerivation_one_eq_derivative (f : R[X]) : mkDerivation R (1 : R[X]) f = derivative f := by rw [mkDerivation_one_eq_derivative'] rfl /-- `Polynomial.mkDerivation` as a linear equivalence. -/ def mkDerivationEquiv : A ≃ₗ[R] Derivation R R[X] A := LinearEquiv.symm <| { invFun := mkDerivation R toFun := fun D => D X map_add' := fun _ _ => rfl map_smul' := fun _ _ => rfl left_inv := fun _ => derivation_ext <| mkDerivation_X _ _ right_inv := fun _ => mkDerivation_X _ _ } @[simp] lemma mkDerivationEquiv_apply (a : A) : mkDerivationEquiv R a = mkDerivation R a := by rfl @[simp] lemma mkDerivationEquiv_symm_apply (D : Derivation R R[X] A) : (mkDerivationEquiv R).symm D = D X := rfl end CommSemiring end Polynomial namespace Derivation variable {R A M : Type*} [CommSemiring R] [CommSemiring A] [Algebra R A] [AddCommMonoid M] [Module A M] [Module R M] [IsScalarTower R A M] (d : Derivation R A M) (a : A) open Polynomial Module /-- For a derivation `d : A → M` and an element `a : A`, `d.compAEval a` is the derivation of `R[X]` which takes a polynomial `f` to `d(aeval a f)`. This derivation takes values in `Module.AEval R M a`, which is `M`, regarded as an `R[X]`-module, with the action of a polynomial `f` defined by `f • m = (aeval a f) • m`. -/ /- Note: `compAEval` is not defined using `Derivation.compAlgebraMap`. This because `A` is not an `R[X]` algebra and it would be messy to create an algebra instance within the definition. -/ @[simps] def compAEval : Derivation R R[X] <| AEval R M a where toFun f := AEval.of R M a (d (aeval a f)) map_add' := by simp map_smul' := by simp leibniz' := by simp [AEval.of_aeval_smul, -Derivation.map_aeval] map_one_eq_zero' := by simp /-- A form of the chain rule: if `f` is a polynomial over `R` and `d : A → M` is an `R`-derivation then for all `a : A` we have $$ d(f(a)) = f' (a) d a. $$ The equation is in the `R[X]`-module `Module.AEval R M a`. For the same equation in `M`, see `Derivation.compAEval_eq`. -/ theorem compAEval_eq (d : Derivation R A M) (f : R[X]) : d.compAEval a f = derivative f • (AEval.of R M a (d a)) := by rw [← mkDerivation_apply] congr apply derivation_ext simp /-- A form of the chain rule: if `f` is a polynomial over `R` and `d : A → M` is an `R`-derivation then for all `a : A` we have $$ d(f(a)) = f' (a) d a. $$ The equation is in `M`. For the same equation in `Module.AEval R M a`, see `Derivation.compAEval_eq`. -/ theorem comp_aeval_eq (d : Derivation R A M) (f : R[X]) : d (aeval a f) = aeval a (derivative f) • d a := calc _ = (AEval.of R M a).symm (d.compAEval a f) := rfl _ = _ := by simp [-compAEval_apply, compAEval_eq] end Derivation
.lake/packages/mathlib/Mathlib/Algebra/Polynomial/Reverse.lean
import Mathlib.Algebra.Polynomial.Degree.TrailingDegree import Mathlib.Algebra.Polynomial.EraseLead /-! # Reverse of a univariate polynomial The main definition is `reverse`. Applying `reverse` to a polynomial `f : R[X]` produces the polynomial with a reversed list of coefficients, equivalent to `X^f.natDegree * f(1/X)`. The main result is that `reverse (f * g) = reverse f * reverse g`, provided the leading coefficients of `f` and `g` do not multiply to zero. -/ namespace Polynomial open Finsupp Finset open scoped Polynomial section Semiring variable {R : Type*} [Semiring R] {f : R[X]} /-- If `i ≤ N`, then `revAtFun N i` returns `N - i`, otherwise it returns `i`. This is the map used by the embedding `revAt`. -/ def revAtFun (N i : ℕ) : ℕ := ite (i ≤ N) (N - i) i theorem revAtFun_invol {N i : ℕ} : revAtFun N (revAtFun N i) = i := by unfold revAtFun grind theorem revAtFun_inj {N : ℕ} : Function.Injective (revAtFun N) := by intro a b hab rw [← @revAtFun_invol N a, hab, revAtFun_invol] /-- If `i ≤ N`, then `revAt N i` returns `N - i`, otherwise it returns `i`. Essentially, this embedding is only used for `i ≤ N`. The advantage of `revAt N i` over `N - i` is that `revAt` is an involution. -/ def revAt (N : ℕ) : Function.Embedding ℕ ℕ where toFun i := ite (i ≤ N) (N - i) i inj' := revAtFun_inj /-- We prefer to use the bundled `revAt` over unbundled `revAtFun`. -/ @[simp] theorem revAtFun_eq (N i : ℕ) : revAtFun N i = revAt N i := rfl @[simp] theorem revAt_invol {N i : ℕ} : (revAt N) (revAt N i) = i := revAtFun_invol @[simp] theorem revAt_le {N i : ℕ} (H : i ≤ N) : revAt N i = N - i := if_pos H lemma revAt_eq_self_of_lt {N i : ℕ} (h : N < i) : revAt N i = i := by simp [revAt, Nat.not_le.mpr h] theorem revAt_add {N O n o : ℕ} (hn : n ≤ N) (ho : o ≤ O) : revAt (N + O) (n + o) = revAt N n + revAt O o := by rcases Nat.le.dest hn with ⟨n', rfl⟩ rcases Nat.le.dest ho with ⟨o', rfl⟩ repeat' rw [revAt_le (le_add_right rfl.le)] rw [add_assoc, add_left_comm n' o, ← add_assoc, revAt_le (le_add_right rfl.le)] repeat' rw [add_tsub_cancel_left] theorem revAt_zero (N : ℕ) : revAt N 0 = N := by simp /-- `reflect N f` is the polynomial such that `(reflect N f).coeff i = f.coeff (revAt N i)`. In other words, the terms with exponent `[0, ..., N]` now have exponent `[N, ..., 0]`. In practice, `reflect` is only used when `N` is at least as large as the degree of `f`. Eventually, it will be used with `N` exactly equal to the degree of `f`. -/ noncomputable def reflect (N : ℕ) : R[X] → R[X] | ⟨f⟩ => ⟨Finsupp.embDomain (revAt N) f⟩ theorem reflect_support (N : ℕ) (f : R[X]) : (reflect N f).support = Finset.image (revAt N) f.support := by rcases f with ⟨⟩ ext1 simp only [reflect, support_ofFinsupp, support_embDomain, Finset.mem_map, Finset.mem_image] @[simp] theorem coeff_reflect (N : ℕ) (f : R[X]) (i : ℕ) : coeff (reflect N f) i = f.coeff (revAt N i) := by rcases f with ⟨f⟩ simp only [reflect, coeff] calc Finsupp.embDomain (revAt N) f i = Finsupp.embDomain (revAt N) f (revAt N (revAt N i)) := by rw [revAt_invol] _ = f (revAt N i) := Finsupp.embDomain_apply _ _ _ @[simp] theorem reflect_zero {N : ℕ} : reflect N (0 : R[X]) = 0 := rfl @[simp] theorem reflect_eq_zero_iff {N : ℕ} {f : R[X]} : reflect N (f : R[X]) = 0 ↔ f = 0 := by rw [ofFinsupp_eq_zero, reflect, embDomain_eq_zero, ofFinsupp_eq_zero] @[simp] theorem reflect_add (f g : R[X]) (N : ℕ) : reflect N (f + g) = reflect N f + reflect N g := by ext simp only [coeff_add, coeff_reflect] @[simp] theorem reflect_C_mul (f : R[X]) (r : R) (N : ℕ) : reflect N (C r * f) = C r * reflect N f := by ext simp only [coeff_reflect, coeff_C_mul] theorem reflect_C_mul_X_pow (N n : ℕ) {c : R} : reflect N (C c * X ^ n) = C c * X ^ revAt N n := by ext rw [reflect_C_mul, coeff_C_mul, coeff_C_mul, coeff_X_pow, coeff_reflect] split_ifs with h · rw [h, revAt_invol, coeff_X_pow_self] · rw [notMem_support_iff.mp] intro a rw [← one_mul (X ^ n), ← C_1] at a apply h rw [← mem_support_C_mul_X_pow a, revAt_invol] @[simp] theorem reflect_C (r : R) (N : ℕ) : reflect N (C r) = C r * X ^ N := by conv_lhs => rw [← mul_one (C r), ← pow_zero X, reflect_C_mul_X_pow, revAt_zero] @[simp] theorem reflect_monomial (N n : ℕ) : reflect N ((X : R[X]) ^ n) = X ^ revAt N n := by rw [← one_mul (X ^ n), ← one_mul (X ^ revAt N n), ← C_1, reflect_C_mul_X_pow] @[simp] lemma reflect_one_X : reflect 1 (X : R[X]) = 1 := by simpa using reflect_monomial 1 1 (R := R) lemma reflect_map {S : Type*} [Semiring S] (f : R →+* S) (p : R[X]) (n : ℕ) : (p.map f).reflect n = (p.reflect n).map f := by ext; simp @[simp] lemma reflect_one (n : ℕ) : (1 : R[X]).reflect n = Polynomial.X ^ n := by rw [← C.map_one, reflect_C, map_one, one_mul] theorem reflect_mul_induction (cf cg : ℕ) (N O : ℕ) (f g : R[X]) (Cf : #f.support ≤ cf.succ) (Cg : #g.support ≤ cg.succ) (Nf : f.natDegree ≤ N) (Og : g.natDegree ≤ O) : reflect (N + O) (f * g) = reflect N f * reflect O g := by induction cf generalizing f with | zero => induction cg generalizing g with | zero => rw [← C_mul_X_pow_eq_self Cf, ← C_mul_X_pow_eq_self Cg] simp_rw [mul_assoc, X_pow_mul, mul_assoc, ← pow_add (X : R[X]), reflect_C_mul, reflect_monomial, add_comm, revAt_add Nf Og, mul_assoc, X_pow_mul, mul_assoc, ← pow_add (X : R[X]), add_comm] | succ cg hcg => by_cases g0 : g = 0 · rw [g0, reflect_zero, mul_zero, mul_zero, reflect_zero] rw [← eraseLead_add_C_mul_X_pow g, mul_add, reflect_add, reflect_add, mul_add, hcg, hcg] <;> try assumption · exact le_add_left card_support_C_mul_X_pow_le_one · exact le_trans (natDegree_C_mul_X_pow_le g.leadingCoeff g.natDegree) Og · exact Nat.lt_succ_iff.mp (lt_of_lt_of_le (eraseLead_support_card_lt g0) Cg) · exact le_trans eraseLead_natDegree_le_aux Og | succ cf hcf => by_cases f0 : f = 0 · rw [f0, reflect_zero, zero_mul, zero_mul, reflect_zero] rw [← eraseLead_add_C_mul_X_pow f, add_mul, reflect_add, reflect_add, add_mul, hcf, hcf] <;> try assumption · exact le_add_left card_support_C_mul_X_pow_le_one · exact le_trans (natDegree_C_mul_X_pow_le f.leadingCoeff f.natDegree) Nf · exact Nat.lt_succ_iff.mp (lt_of_lt_of_le (eraseLead_support_card_lt f0) Cf) · exact le_trans eraseLead_natDegree_le_aux Nf @[simp] theorem reflect_mul (f g : R[X]) {F G : ℕ} (Ff : f.natDegree ≤ F) (Gg : g.natDegree ≤ G) : reflect (F + G) (f * g) = reflect F f * reflect G g := reflect_mul_induction _ _ F G f g f.support.card.le_succ g.support.card.le_succ Ff Gg section Eval₂ variable {S : Type*} [CommSemiring S] theorem eval₂_reflect_mul_pow (i : R →+* S) (x : S) [Invertible x] (N : ℕ) (f : R[X]) (hf : f.natDegree ≤ N) : eval₂ i (⅟x) (reflect N f) * x ^ N = eval₂ i x f := by refine induction_with_natDegree_le (fun f => eval₂ i (⅟x) (reflect N f) * x ^ N = eval₂ i x f) _ ?_ ?_ ?_ f hf · simp · intro n r _ hnN simp only [revAt_le hnN, reflect_C_mul_X_pow, eval₂_X_pow, eval₂_C, eval₂_mul] conv in x ^ N => rw [← Nat.sub_add_cancel hnN] rw [pow_add, ← mul_assoc, mul_assoc (i r), ← mul_pow, invOf_mul_self, one_pow, mul_one] · intros simp [*, add_mul] theorem eval₂_reflect_eq_zero_iff (i : R →+* S) (x : S) [Invertible x] (N : ℕ) (f : R[X]) (hf : f.natDegree ≤ N) : eval₂ i (⅟x) (reflect N f) = 0 ↔ eval₂ i x f = 0 := by conv_rhs => rw [← eval₂_reflect_mul_pow i x N f hf] constructor · intro h rw [h, zero_mul] · intro h rw [← mul_one (eval₂ i (⅟x) _), ← one_pow N, ← mul_invOf_self x, mul_pow, ← mul_assoc, h, zero_mul] end Eval₂ /-- The reverse of a polynomial f is the polynomial obtained by "reading f backwards". Even though this is not the actual definition, `reverse f = f (1/X) * X ^ f.natDegree`. -/ noncomputable def reverse (f : R[X]) : R[X] := reflect f.natDegree f theorem coeff_reverse (f : R[X]) (n : ℕ) : f.reverse.coeff n = f.coeff (revAt f.natDegree n) := by rw [reverse, coeff_reflect] @[simp] theorem coeff_zero_reverse (f : R[X]) : coeff (reverse f) 0 = leadingCoeff f := by rw [coeff_reverse, revAt_le (zero_le f.natDegree), tsub_zero, leadingCoeff] @[simp] theorem reverse_zero : reverse (0 : R[X]) = 0 := rfl @[simp] theorem reverse_eq_zero : f.reverse = 0 ↔ f = 0 := by simp [reverse] theorem reverse_natDegree_le (f : R[X]) : f.reverse.natDegree ≤ f.natDegree := by rw [natDegree_le_iff_degree_le, degree_le_iff_coeff_zero] intro n hn rw [Nat.cast_lt] at hn rw [coeff_reverse, revAt, Function.Embedding.coeFn_mk, if_neg (not_le_of_gt hn), coeff_eq_zero_of_natDegree_lt hn] theorem natDegree_eq_reverse_natDegree_add_natTrailingDegree (f : R[X]) : f.natDegree = f.reverse.natDegree + f.natTrailingDegree := by by_cases hf : f = 0 · rw [hf, reverse_zero, natDegree_zero, natTrailingDegree_zero] apply le_antisymm · refine tsub_le_iff_right.mp ?_ apply le_natDegree_of_ne_zero rw [reverse, coeff_reflect, ← revAt_le f.natTrailingDegree_le_natDegree, revAt_invol] exact trailingCoeff_nonzero_iff_nonzero.mpr hf · rw [← le_tsub_iff_left f.reverse_natDegree_le] apply natTrailingDegree_le_of_ne_zero have key := mt leadingCoeff_eq_zero.mp (mt reverse_eq_zero.mp hf) rwa [leadingCoeff, coeff_reverse, revAt_le f.reverse_natDegree_le] at key theorem reverse_natDegree (f : R[X]) : f.reverse.natDegree = f.natDegree - f.natTrailingDegree := by rw [f.natDegree_eq_reverse_natDegree_add_natTrailingDegree, add_tsub_cancel_right] theorem reverse_leadingCoeff (f : R[X]) : f.reverse.leadingCoeff = f.trailingCoeff := by rw [leadingCoeff, reverse_natDegree, ← revAt_le f.natTrailingDegree_le_natDegree, coeff_reverse, revAt_invol, trailingCoeff] theorem natTrailingDegree_reverse (f : R[X]) : f.reverse.natTrailingDegree = 0 := by rw [natTrailingDegree_eq_zero, reverse_eq_zero, coeff_zero_reverse, leadingCoeff_ne_zero] exact eq_or_ne _ _ theorem reverse_trailingCoeff (f : R[X]) : f.reverse.trailingCoeff = f.leadingCoeff := by rw [trailingCoeff, natTrailingDegree_reverse, coeff_zero_reverse] theorem reverse_mul {f g : R[X]} (fg : f.leadingCoeff * g.leadingCoeff ≠ 0) : reverse (f * g) = reverse f * reverse g := by unfold reverse rw [natDegree_mul' fg, reflect_mul f g rfl.le rfl.le] @[simp] theorem reverse_mul_of_domain {R : Type*} [Semiring R] [NoZeroDivisors R] (f g : R[X]) : reverse (f * g) = reverse f * reverse g := by by_cases f0 : f = 0 · simp only [f0, zero_mul, reverse_zero] by_cases g0 : g = 0 · rw [g0, mul_zero, reverse_zero, mul_zero] simp [reverse_mul, *] theorem trailingCoeff_mul {R : Type*} [Semiring R] [NoZeroDivisors R] (p q : R[X]) : (p * q).trailingCoeff = p.trailingCoeff * q.trailingCoeff := by rw [← reverse_leadingCoeff, reverse_mul_of_domain, leadingCoeff_mul, reverse_leadingCoeff, reverse_leadingCoeff] @[simp] theorem coeff_one_reverse (f : R[X]) : coeff (reverse f) 1 = nextCoeff f := by rw [coeff_reverse, nextCoeff] split_ifs with hf · have : coeff f 1 = 0 := coeff_eq_zero_of_natDegree_lt (by simp only [hf, zero_lt_one]) simp [*, revAt] · rw [revAt_le] exact Nat.succ_le_iff.2 (pos_iff_ne_zero.2 hf) @[simp] lemma reverse_C (t : R) : reverse (C t) = C t := by simp [reverse] @[simp] lemma reverse_mul_X (p : R[X]) : reverse (p * X) = reverse p := by nontriviality R rcases eq_or_ne p 0 with rfl | hp · simp · simp [reverse, hp] @[simp] lemma reverse_X_mul (p : R[X]) : reverse (X * p) = reverse p := by rw [commute_X p, reverse_mul_X] @[simp] lemma reverse_mul_X_pow (p : R[X]) (n : ℕ) : reverse (p * X ^ n) = reverse p := by induction n with | zero => simp | succ n ih => rw [pow_succ, ← mul_assoc, reverse_mul_X, ih] @[simp] lemma reverse_X_pow_mul (p : R[X]) (n : ℕ) : reverse (X ^ n * p) = reverse p := by rw [commute_X_pow p, reverse_mul_X_pow] @[simp] lemma reverse_add_C (p : R[X]) (t : R) : reverse (p + C t) = reverse p + C t * X ^ p.natDegree := by simp [reverse] @[simp] lemma reverse_C_add (p : R[X]) (t : R) : reverse (C t + p) = C t * X ^ p.natDegree + reverse p := by rw [add_comm, reverse_add_C, add_comm] section Eval₂ variable {S : Type*} [CommSemiring S] theorem eval₂_reverse_mul_pow (i : R →+* S) (x : S) [Invertible x] (f : R[X]) : eval₂ i (⅟x) (reverse f) * x ^ f.natDegree = eval₂ i x f := eval₂_reflect_mul_pow i _ _ f le_rfl @[simp] theorem eval₂_reverse_eq_zero_iff (i : R →+* S) (x : S) [Invertible x] (f : R[X]) : eval₂ i (⅟x) (reverse f) = 0 ↔ eval₂ i x f = 0 := eval₂_reflect_eq_zero_iff i x _ _ le_rfl end Eval₂ end Semiring section Ring variable {R : Type*} [Ring R] @[simp] theorem reflect_neg (f : R[X]) (N : ℕ) : reflect N (-f) = -reflect N f := by rw [neg_eq_neg_one_mul, ← C_1, ← C_neg, reflect_C_mul, C_neg, C_1, ← neg_eq_neg_one_mul] @[simp] theorem reflect_sub (f g : R[X]) (N : ℕ) : reflect N (f - g) = reflect N f - reflect N g := by rw [sub_eq_add_neg, sub_eq_add_neg, reflect_add, reflect_neg] @[simp] theorem reverse_neg (f : R[X]) : reverse (-f) = -reverse f := by rw [reverse, reverse, reflect_neg, natDegree_neg] end Ring end Polynomial
.lake/packages/mathlib/Mathlib/Algebra/Polynomial/RuleOfSigns.lean
import Mathlib.Algebra.Polynomial.CoeffList import Mathlib.Algebra.Polynomial.Monic import Mathlib.Algebra.Polynomial.Roots import Mathlib.Data.List.Destutter import Mathlib.Data.Sign.Basic /-! # Descartes' Rule of Signs We define the "sign changes" in the coefficients of a polynomial, and prove Descartes' Rule of Signs: a real polynomial has at most as many positive roots as there are sign changes. A sign change is when there is a positive coefficient followed by a negative coefficient, or vice versa, with any number of zero coefficients in between. ## Main Definitions - `Polynomial.signVariations`: The number of sign changes in a polynomial's coefficients, where `0` coefficients are ignored. ## Main theorem - `Polynomial.roots_countP_pos_le_signVariations`. States that `P.roots.countP (0 < ·) ≤ P.signVariations`, so that positive roots are counted with multiplicity. It's currently proved for any `CommRing` with `IsStrictOrderedRing`. There is likely some correct statement in terms of a (noncommutative) `Ring`, but `Polynomial.roots` is only defined for commutative rings. ## Reference [Wikipedia: Descartes' Rule of Signs](https://en.wikipedia.org/wiki/Descartes%27_rule_of_signs) -/ namespace Polynomial section Semiring variable {R : Type*} [Semiring R] [LinearOrder R] (P : Polynomial R) /-- Counts the number of times that the coefficients in a polynomial change sign, with the convention that 0 can count as either sign. -/ def signVariations : ℕ := letI coeff_signs := (coeffList P).map SignType.sign letI nonzero_signs := coeff_signs.filter (· ≠ 0) (nonzero_signs.destutter (· ≠ ·)).length - 1 variable (R) in @[simp] theorem signVariations_zero : signVariations (0 : R[X]) = 0 := by simp [signVariations] /-- Sign variations of a monomial are always zero. -/ @[simp] theorem signVariations_monomial (d : ℕ) (c : R) : signVariations (monomial d c) = 0 := by by_cases hcz : c = 0 · simp [hcz] · simp [hcz, signVariations, coeffList_eraseLead (mt (monomial_eq_zero_iff c d).mp hcz)] /-- If the first two signs are the same, then sign_variations is unchanged by eraseLead -/ theorem signVariations_eraseLead (h : SignType.sign P.leadingCoeff = SignType.sign P.nextCoeff) : signVariations P.eraseLead = signVariations P := by by_cases hpz : P = 0 · simp_all · have h₂ : nextCoeff P ≠ 0 := by intro; simp_all obtain ⟨_, hl⟩ := coeffList_eq_cons_leadingCoeff (mt nextCoeff_eq_zero_of_eraseLead_eq_zero h₂) simp [signVariations, List.destutter, leadingCoeff_eraseLead_eq_nextCoeff h₂, hl, h, h₂, coeffList_eraseLead hpz] /-- If we drop the leading coefficient, the sign changes drop by 0 or 1 depending on whether the first two nonzero coeffients match. -/ theorem signVariations_eq_eraseLead_add_ite {P : Polynomial R} (h : P ≠ 0) : signVariations P = signVariations P.eraseLead + if SignType.sign P.leadingCoeff = -SignType.sign P.eraseLead.leadingCoeff then 1 else 0 := by by_cases hpz : P = 0 · simp_all have hsl : SignType.sign (leadingCoeff P) ≠ 0 := by simp_all rw [signVariations, signVariations, coeffList_eraseLead hpz] rw [List.map_cons, List.map_append, List.map_replicate] rcases h_eL : P.eraseLead.coeffList with _ | ⟨c, cs⟩ · simp [coeffList_eq_nil.mp h_eL, h] simp only [List.filter_append, List.filter_replicate, List.map_cons, List.filter, ne_eq, hsl] have h₁ : SignType.sign c ≠ 0 := by by_contra h₂ suffices eraseLead P = 0 by grind [coeffList_zero] by_contra h have := coeffList_eq_cons_leadingCoeff h grind [leadingCoeff_eq_zero, sign_eq_zero_iff] simp only [decide_not, sign_zero, List.destutter, Bool.false_eq_true, reduceIte, h₁, decide_false, Bool.not_false, List.nil_append, List.destutter', decide_true, Bool.not_true] obtain rfl : c = leadingCoeff P.eraseLead := by have h_eL : eraseLead P ≠ 0 := by simp [← coeffList_eq_nil, h_eL] obtain ⟨ls, hls⟩ := coeffList_eq_cons_leadingCoeff h_eL grind by_cases h₄ : SignType.sign P.leadingCoeff = SignType.sign P.eraseLead.leadingCoeff · grind [SignType.neg_eq_self_iff] rw [if_pos h₄, if_pos ?_] · grind [Nat.sub_add_cancel, List.length_pos_of_ne_nil, List.destutter'_ne_nil ] cases _ : SignType.sign P.leadingCoeff <;> cases _ : SignType.sign P.eraseLead.leadingCoeff <;> grind [= SignType.neg_eq_neg_one, SignType.zero_eq_zero, SignType.pos_eq_one, SignType.neg_eq_neg_one, neg_neg] /-- We can only lose, not gain, sign changes if we drop the leading coefficient. -/ theorem signVariations_eraseLead_le : signVariations P.eraseLead ≤ signVariations P := by by_cases hpz : P = 0 · simp [hpz] · grind [signVariations_eq_eraseLead_add_ite] /-- We can only lose at most one sign changes if we drop the leading coefficient. -/ theorem signVariations_le_eraseLead_succ : signVariations P ≤ signVariations P.eraseLead + 1 := by by_cases hpz : P = 0 · simp [hpz] · grind [signVariations_eq_eraseLead_add_ite] end Semiring section OrderedRing variable {R : Type*} [Ring R] [LinearOrder R] [IsOrderedRing R] (P : Polynomial R) {x : R} /-- The number of sign changes does not change if we negate. -/ @[simp] theorem signVariations_neg : signVariations (-P) = signVariations P := by rw [signVariations, signVariations, coeffList_neg] simp only [List.map_map, List.filter_map] have hsc : SignType.sign ∘ (fun (x:R) => -x) = (fun x => -x) ∘ SignType.sign := by grind [Left.sign_neg] have h_neg_destutter (l : List SignType) : (l.destutter (¬· = ·)).map (- ·) = (l.map (- ·)).destutter (¬· = ·) := by grind [List.map_destutter, neg_inj] rw [hsc, List.comp_map, ← h_neg_destutter, List.length_map] congr 5 funext simp [SignType.sign] end OrderedRing section StrictOrderedRing variable {R : Type*} [Ring R] [LinearOrder R] [IsStrictOrderedRing R] {P : Polynomial R} {η : R} /-- The number of sign changes does not change if we multiply by any nonzero scalar. -/ @[simp] theorem signVariations_C_mul (P : Polynomial R) (hx : η ≠ 0) : signVariations (C η * P) = signVariations P := by wlog hx2 : 0 < η · simpa [lt_of_le_of_ne (le_of_not_gt hx2), hx] using this (η := -η) (P := -P) rw [signVariations, signVariations] rw [coeffList_C_mul _ (lt_or_lt_iff_ne.mp (.inr hx2)), ← List.comp_map] congr 5 funext simp [hx2, sign_mul] /-- If P's coefficients start with signs `[+, -, ...]`, then multiplying by a binomial `X - η` commutes with `eraseLead` in the number of sign changes. This is because the product of `P` and `X - η` has the pattern `[+, -, ...]` as well, so then `P.eraseLead` starts with `[-,...]`, and multiplying by `X - η` gives `[-, ...]` too. -/ lemma signVariations_eraseLead_mul_X_sub_C (hη : 0 < η) (hP₀ : 0 < leadingCoeff P) (hc : P.nextCoeff < 0) : ((X - C η) * P).eraseLead.signVariations = ((X - C η) * P.eraseLead).signVariations := by obtain ⟨d, hd⟩ := Nat.exists_eq_add_one.mpr (natDegree_pos_of_nextCoeff_ne_zero hc.ne) have hndxP : natDegree ((X - C η) * P) = P.natDegree + 1 := by have hPn0 : P ≠ 0 := leadingCoeff_ne_zero.mp hP₀.ne' rw [natDegree_mul (X_sub_C_ne_zero η) hPn0, natDegree_X_sub_C, add_comm] have hndxeP : natDegree ((X - C η) * P.eraseLead) = P.natDegree := by have hePn0 : P.eraseLead ≠ 0 := mt nextCoeff_eq_zero_of_eraseLead_eq_zero hc.ne rw [natDegree_mul (X_sub_C_ne_zero η) hePn0, natDegree_X_sub_C, add_comm] exact natDegree_eraseLead_add_one hc.ne have hQ : ((X - C η) * P).nextCoeff = coeff P d - η * coeff P (d + 1) := by grind [nextCoeff_of_natDegree_pos, coeff_X_sub_C_mul] have hQ₁ : ((X - C η) * P).nextCoeff < 0 := by rw [hQ, sub_neg] trans 0 · grind [nextCoeff_of_natDegree_pos] · exact hd ▸ mul_pos hη hP₀ have hndexP0 : natDegree (eraseLead ((X - C η) * P)) = P.natDegree := by apply Nat.add_right_cancel (m := 1) rw [← hndxP, natDegree_eraseLead_add_one hQ₁.ne] --the theorem is true mainly because all the signs are the same; --in fact, the coefficients are all the same except the first. suffices eraseLead (eraseLead ((X - C η) * P)) = eraseLead ((X - C η) * P.eraseLead) by suffices (coeffList (eraseLead ((X - C η) * P))).map SignType.sign = (coeffList ((X - C η) * P.eraseLead)).map SignType.sign by rw [signVariations, signVariations, this] have : 0 < natDegree ((X - C η) * P.eraseLead) := by omega grind [leadingCoeff_mul, leadingCoeff_X_sub_C, one_mul, leadingCoeff_eraseLead_eq_nextCoeff, LT.lt.ne, sign_neg, coeffList_eraseLead, ne_zero_of_natDegree_gt, nextCoeff_eq_zero_of_eraseLead_eq_zero] rw [← self_sub_monomial_natDegree_leadingCoeff, leadingCoeff_eraseLead_eq_nextCoeff hQ₁.ne] rw [hndexP0, ← self_sub_monomial_natDegree_leadingCoeff, leadingCoeff_monic_mul (monic_X_sub_C η)] rw [← self_sub_monomial_natDegree_leadingCoeff, leadingCoeff_monic_mul (monic_X_sub_C η)] rw [hndxeP, hndxP] rw [leadingCoeff_eraseLead_eq_nextCoeff hc.ne, ← self_sub_monomial_natDegree_leadingCoeff] rw [hQ, mul_sub, sub_mul, sub_mul, X_mul_monomial, C_mul_monomial, monomial_sub] rw [leadingCoeff, nextCoeff_of_natDegree_pos (hd ▸ d.succ_pos), hd, Nat.add_sub_cancel] abel /-- This lemma is really a specialization of `succ_signVariations_le_sub_mul` to monomials. -/ lemma succ_signVariations_X_sub_C_mul_monomial {d c} (hc : c ≠ 0) (hη : 0 < η) : (monomial d c).signVariations + 1 ≤ ((X - C η) * monomial d c).signVariations := by have h₁ : nextCoeff ((X - C η) * monomial d c) = -(η * c) := by convert coeff_mul_monomial (X - C η) d 0 c using 1 · simp [hc, nextCoeff, natDegree_mul (X_sub_C_ne_zero η)] · simp have h₂ : eraseLead ((X - C η) * monomial d c) ≠ 0 := by apply mt nextCoeff_eq_zero_of_eraseLead_eq_zero simp [h₁, hc, hη.ne'] have h₃ : SignType.sign c ≠ SignType.sign (-(η * c)) := by simp [hη, hc, Left.sign_neg, sign_mul] simpa [h₁, h₂, h₃, hc, hη.ne', signVariations, List.destutter_cons_cons, ← leadingCoeff_cons_eraseLead, coeffList_eraseLead, leadingCoeff_eraseLead_eq_nextCoeff] using List.length_pos_of_ne_nil (List.destutter'_ne_nil _ _) private lemma exists_cons_of_leadingCoeff_pos (η) (h₁ : 0 < leadingCoeff P) (h₂ : P.nextCoeff ≠ 0) : ∃ c₀ cs, ((X - C η) * P).coeffList = P.leadingCoeff :: c₀ :: cs ∧ ((X - C η) * P.eraseLead).coeffList = P.nextCoeff :: cs := by have h₃ := leadingCoeff_ne_zero.mp h₁.ne' have h₄ := natDegree_eraseLead_add_one h₂ have h₅ : (X - C η) ≠ 0 := X_sub_C_ne_zero η have h₆ : P.eraseLead ≠ 0 := mt nextCoeff_eq_zero_of_eraseLead_eq_zero h₂ obtain ⟨d, hd⟩ := Nat.exists_eq_add_of_lt (natDegree_pos_of_nextCoeff_ne_zero h₂) apply leadingCoeff_eraseLead_eq_nextCoeff at h₂ have h_cons := coeffList_eraseLead (mul_ne_zero h₅ h₆) generalize ((X - C η) * P.eraseLead).natDegree - ((X - C η) * P.eraseLead).eraseLead.degree.succ = n at h_cons ⊢ use nextCoeff ((X - C η) * P), .replicate n 0 ++ coeffList ((X - C η) * P.eraseLead).eraseLead constructor · have h₇ : natDegree ((X - C η) * P) = P.natDegree + 1 := by rw [natDegree_mul h₅ h₃, natDegree_X_sub_C, add_comm] have h₈ : ((X - C η) * P.eraseLead).eraseLead = (X - C η) * P.eraseLead - monomial P.natDegree P.nextCoeff := by simp [← self_sub_monomial_natDegree_leadingCoeff (_ * _), natDegree_mul, h₅, h₆, h₂, h₄, add_comm 1] have : P.eraseLead.natDegree + 2 = ((X - C η) * P.eraseLead).coeffList.length := by simp [h₅, h₆, natDegree_mul, add_comm 1] have : P.natDegree + 2 = ((X - C η) * P).coeffList.length := by simp [X_sub_C_ne_zero, h₃, h₇] have := leadingCoeff_monic_mul (q := P) (monic_X_sub_C η) by_cases h₉ : ((X - C η) * P).nextCoeff = 0 · suffices ((X - C η) * P).eraseLead = ((X - C η) * P.eraseLead).eraseLead by have := coeffList_eraseLead (mul_ne_zero (X_sub_C_ne_zero η) h₃) #adaptation_note /-- Moving from `nightly-2025-10-13` to `nightly-2025-10-19` we now need to provide an intermediate step. -/ have : ((X - C η) * P).natDegree - ((X - C η) * P).eraseLead.degree.succ = n + 1 := by grind grind [leadingCoeff_mul, leadingCoeff_X_sub_C] suffices C η * monomial P.natDegree P.leadingCoeff = monomial P.natDegree P.nextCoeff by grind [X_mul_monomial, sub_mul, mul_sub, self_sub_monomial_natDegree_leadingCoeff] grind [leadingCoeff, nextCoeff_of_natDegree_pos, C_mul_monomial, eq_of_sub_eq_zero, coeff_X_sub_C_mul] · suffices ((X - C η) * P).eraseLead.eraseLead = ((X - C η) * P.eraseLead).eraseLead by have := leadingCoeff_cons_eraseLead h₉ have := coeffList_eraseLead (mt nextCoeff_eq_zero_of_eraseLead_eq_zero h₉) grind [leadingCoeff_eraseLead_eq_nextCoeff] suffices monomial P.natDegree ((X - C η) * P).nextCoeff = monomial P.natDegree P.nextCoeff - C η * monomial P.natDegree P.leadingCoeff by grind [X_mul_monomial, sub_mul, mul_sub, self_sub_monomial_natDegree_leadingCoeff, natDegree_eraseLead_add_one, leadingCoeff_eraseLead_eq_nextCoeff] grind [coeff_X_sub_C_mul, C_mul_monomial, nextCoeff_of_natDegree_pos, leadingCoeff] · rw [h_cons, leadingCoeff_mul, leadingCoeff_X_sub_C, one_mul, h₂] /-- If a polynomial starts with two positive coefficients, then the sign changes in the product `(X - η) * P` is the same as `(X - η) * P.eraseLead`. This lemma lets us do induction on the degree of P when P starts with matching coefficient signs. Of course this is also true when the first two coefficients of P are *negative*, but we just prove the case where they're positive since it's cleaner and sufficient for the later use. -/ lemma signVariations_X_sub_C_mul_eraseLead_le (h : 0 < P.leadingCoeff) (h₂ : 0 < P.nextCoeff) : signVariations ((X - C η) * P.eraseLead) ≤ signVariations ((X - C η) * P) := by obtain ⟨c₀, cs, ⟨hcs, hecs⟩⟩ := exists_cons_of_leadingCoeff_pos η h h₂.ne' simp +decide only [hcs, hecs, h, h₂, signVariations, List.destutter, List.map_cons, sign_pos, List.filter_cons_of_pos, tsub_le_iff_right, Nat.sub_add_cancel (List.length_pos_of_ne_nil (List.destutter'_ne_nil _ _))] rw [List.filter_cons] split; swap --does c₀ = 0? If so, the trailing nonzero coefficient lists are identical. · rfl rw [List.destutter'_cons] split; swap --does SignType.sign c₀ = 1? If so, the destutter doesn't care about it. · rfl rcases hcs : (cs.map SignType.sign).filter fun x ↦ decide (x ≠ 0) with _ | ⟨r, rs⟩ · simp · rw [← List.destutter_cons', ← List.destutter_cons'] grind [List.destutter_cons_cons] /-- Multiplying a polynomial by a linear term `X - η` adds at least one sign change. This is the basis for the induction in `roots_countP_pos_le_signVariations`. -/ theorem succ_signVariations_le_X_sub_C_mul (hη : 0 < η) (hP : P ≠ 0) : signVariations P + 1 ≤ signVariations ((X - C η) * P) := by -- do induction on the degree generalize hd : P.natDegree = d induction d using Nat.strong_induction_on generalizing P with | _ d ih => -- can assume it starts positive, otherwise negate P wlog h_lC : 0 < leadingCoeff P generalizing P with H · simpa using @H (-P) (by simpa) (by simpa) (by grind [leadingCoeff_eq_zero, leadingCoeff_neg]) --Adding a new root doesn't make the product zero, and increases degree by exactly one. have h_mul : (X - C η) * P ≠ 0 := mul_ne_zero (X_sub_C_ne_zero η) hP have h_deg_mul : natDegree ((X - C η) * P) = natDegree P + 1 := by rw [natDegree_mul (X_sub_C_ne_zero η) hP, natDegree_X_sub_C, add_comm] rcases d with _ | d · --P is zero degree, therefore a constant. have hcQ : 0 < coeff P 0 := by grind [leadingCoeff] have hxcQ : coeff ((X - C η) * P) 1 = coeff P 0 := by grind [coeff_X_sub_C_mul, mul_zero, coeff_eq_zero_of_natDegree_lt] dsimp [signVariations, coeffList] rw [withBotSucc_degree_eq_natDegree_add_one hP, withBotSucc_degree_eq_natDegree_add_one h_mul] simp [h_deg_mul, hxcQ, hη, hcQ, hd, List.range_succ] -- P is positive degree. Set up some temporary variables for signs for the nextCoeffs. generalize hs_nC : SignType.sign P.nextCoeff = s_nC generalize hs_nC_mul : SignType.sign ((X - C η) * P).nextCoeff = s_nC_mul --We're really doing induction on `P.eraseLead` in a sense have h_ih : P.eraseLead.natDegree < d + 1 := by grind [eraseLead_natDegree_le] have h_mul_lC : SignType.sign ((X - C η) * P).leadingCoeff = 1 := by simp [h_lC] have h_ηP : 0 < η * coeff P (d + 1) := by grind [leadingCoeff, mul_pos] rcases s_nC.trichotomy with rfl | rfl | rfl; rotate_left · -- P starts with [+,0,...] so (X-C)*P starts with [+,-,...]. obtain rfl : s_nC_mul = -1 := by have : coeff P d = 0 := by simpa [nextCoeff, hd] using hs_nC simp [*, ← hs_nC_mul, nextCoeff, coeff_X_sub_C_mul] /- We would like to just `have : eraseLead P ≠ 0`, so that we can use the inductive hypothesis on eraseLead P. but that isn't actually true: we could have P a monomial and then eraseLead P = 0, and then the inductive hypothesis doesn't hold. (It's only true as written for P ≠ 0.) So we need to do a case-split and handle this separately. -/ by_cases eraseLead P = 0 · grind [succ_signVariations_X_sub_C_mul_monomial, eraseLead_add_monomial_natDegree_leadingCoeff, zero_add] · /- Dropping the lead of the product exactly drops the first two of the eraseLead. This decreases the sign variations of the eraseLead by at least one, and of the product by at most one, so we can induct. -/ have : signVariations ((X - C η) * P).eraseLead + 1 = signVariations ((X - C η) * P) := by simp [-leadingCoeff_mul, ← sign_ne_zero, signVariations_eq_eraseLead_add_ite h_mul, leadingCoeff_eraseLead_eq_nextCoeff, hs_nC_mul, h_mul_lC] have : ((X - C η) * P.eraseLead).signVariations ≤ ((X - C η) * P).eraseLead.signVariations := by have := signVariations_eraseLead_le (eraseLead ((X - C η) * P)) rwa [← eraseLead_mul_eq_mul_eraseLead_of_nextCoeff_zero hη.ne'] grind [sign_eq_zero_iff] grind [signVariations_le_eraseLead_succ] all_goals ( have h₁ : nextCoeff P ≠ 0 := by simp [← sign_ne_zero, hs_nC] specialize ih _ h_ih (mt nextCoeff_eq_zero_of_eraseLead_eq_zero h₁) rfl have : P.signVariations = P.eraseLead.signVariations + ?_ := by simp [signVariations_eq_eraseLead_add_ite hP, leadingCoeff_eraseLead_eq_nextCoeff h₁, hs_nC, h_lC] exact rfl ) · /- P starts with [+,+,...]. (X-C)*P starts with [+,?,...]. After dropping the lead of P, this becomes [+,...] and [+,...]. So the sign variations on P are unchanged when we induct, while (X-C)*P can only lose at most one sign change. -/ grind [sign_eq_one_iff, signVariations_X_sub_C_mul_eraseLead_le] · /- P starts with [+,-,...], so (X-C)*P starts with [+,-,...]. After dropping the lead of P, this becomes [-,...] and [-,...]. Dropping the first one of each decreases (X-C)*P by one and P by one, so we can induct. -/ trans ((X - C η) * P).eraseLead.signVariations + 1 · grind [signVariations_eraseLead_mul_X_sub_C, sign_eq_neg_one_iff] · suffices SignType.sign ((X - C η) * P).nextCoeff = -1 by simp +decide [signVariations_eq_eraseLead_add_ite h_mul, h_lC, leadingCoeff_eraseLead_eq_nextCoeff, ← sign_eq_zero_iff, this] grind [← sign_eq_neg_one_iff, coeff_X_sub_C_mul, nextCoeff] end StrictOrderedRing section CommStrictOrderedRing variable {R : Type*} [CommRing R] [LinearOrder R] [IsStrictOrderedRing R] (P : Polynomial R) /-- **Descartes' Rule of Signs**: the number of positive roots is at most the number of sign variations. -/ theorem roots_countP_pos_le_signVariations : P.roots.countP (0 < ·) ≤ signVariations P := by generalize h : P.roots.countP (0 < ·) = num_pos_roots induction num_pos_roots generalizing P --Induct on number of roots. · exact zero_le _ rename_i ih have hp : P ≠ 0 := by grind [roots_zero, Multiset.countP_zero] -- we can take a positive root, η, because the number of roots is positive obtain ⟨η, η_root, η_pos⟩ : ∃ x, x ∈ P.roots ∧ 0 < x := by grind [Multiset.countP_pos] -- (X - η) divides P(X), so write P(X) = (X - η) * Q(X) obtain ⟨Q, rfl⟩ := dvd_iff_isRoot.mpr (isRoot_of_mem_roots η_root) -- P has at least num_roots sign variations grw [ih Q, succ_signVariations_le_X_sub_C_mul η_pos] · exact right_ne_zero_of_mul hp · simp [← h, roots_mul (ne_zero_of_mem_roots η_root), η_pos, ← Nat.succ.injEq] end CommStrictOrderedRing end Polynomial
.lake/packages/mathlib/Mathlib/Algebra/Polynomial/SpecificDegree.lean
import Mathlib.Algebra.Polynomial.Roots import Mathlib.Tactic.IntervalCases import Mathlib.Algebra.Polynomial.FieldDivision /-! # Polynomials of specific degree Facts about polynomials that have a specific integer degree. -/ namespace Polynomial section IsDomain variable {R : Type*} [CommRing R] [IsDomain R] /-- A polynomial of degree 2 or 3 is irreducible iff it doesn't have roots. -/ theorem Monic.irreducible_iff_roots_eq_zero_of_degree_le_three {p : R[X]} (hp : p.Monic) (hp2 : 2 ≤ p.natDegree) (hp3 : p.natDegree ≤ 3) : Irreducible p ↔ p.roots = 0 := by have hp0 : p ≠ 0 := hp.ne_zero have hp1 : p ≠ 1 := by rintro rfl; rw [natDegree_one] at hp2; cases hp2 rw [hp.irreducible_iff_lt_natDegree_lt hp1] simp_rw [show p.natDegree / 2 = 1 from (Nat.div_le_div_right hp3).antisymm (by apply Nat.div_le_div_right (c := 2) hp2), show Finset.Ioc 0 1 = {1} from rfl, Finset.mem_singleton, Multiset.eq_zero_iff_forall_notMem, mem_roots hp0, ← dvd_iff_isRoot] refine ⟨fun h r ↦ h _ (monic_X_sub_C r) (natDegree_X_sub_C r), fun h q hq hq1 ↦ ?_⟩ rw [hq.eq_X_add_C hq1, ← sub_neg_eq_add, ← C_neg] apply h end IsDomain section Field variable {K : Type*} [Field K] {p : K[X]} /-- A polynomial of degree 2 or 3 is irreducible iff it doesn't have roots. -/ theorem irreducible_iff_roots_eq_zero_of_degree_le_three (hp2 : 2 ≤ p.natDegree) (hp3 : p.natDegree ≤ 3) : Irreducible p ↔ p.roots = 0 := by have hp0 : p ≠ 0 := by rintro rfl; rw [natDegree_zero] at hp2; cases hp2 rw [← irreducible_mul_leadingCoeff_inv, (monic_mul_leadingCoeff_inv hp0).irreducible_iff_roots_eq_zero_of_degree_le_three, mul_comm, roots_C_mul] · exact inv_ne_zero (leadingCoeff_ne_zero.mpr hp0) · rwa [natDegree_mul_leadingCoeff_inv _ hp0] · rwa [natDegree_mul_leadingCoeff_inv _ hp0] lemma irreducible_of_degree_le_three_of_not_isRoot (hdeg : p.natDegree ∈ Finset.Icc 1 3) (hnot : ∀ x, ¬ IsRoot p x) : Irreducible p := by rw [Finset.mem_Icc] at hdeg by_cases hdeg2 : 2 ≤ p.natDegree · rw [Polynomial.irreducible_iff_roots_eq_zero_of_degree_le_three hdeg2 hdeg.2] apply Multiset.eq_zero_of_forall_notMem simp_all · apply Polynomial.irreducible_of_degree_eq_one rw [← Nat.cast_one, Polynomial.degree_eq_iff_natDegree_eq_of_pos (by simp)] exact le_antisymm (by rwa [not_le, Nat.lt_succ_iff] at hdeg2) hdeg.1 end Field end Polynomial
.lake/packages/mathlib/Mathlib/Algebra/Polynomial/Smeval.lean
import Mathlib.Algebra.Group.NatPowAssoc import Mathlib.Algebra.Polynomial.AlgebraMap import Mathlib.Algebra.Polynomial.Eval.SMul /-! # Scalar-multiple polynomial evaluation This file defines polynomial evaluation via scalar multiplication. Our polynomials have coefficients in a semiring `R`, and we evaluate at a weak form of `R`-algebra, namely an additive commutative monoid with an action of `R` and a notion of natural number power. This is a generalization of `Algebra.Polynomial.Eval`. ## Main definitions * `Polynomial.smeval`: function for evaluating a polynomial with coefficients in a `Semiring` `R` at an element `x` of an `AddCommMonoid` `S` that has natural number powers and an `R`-action. * `smeval.linearMap`: the `smeval` function as an `R`-linear map, when `S` is an `R`-module. * `smeval.algebraMap`: the `smeval` function as an `R`-algebra map, when `S` is an `R`-algebra. ## Main results * `smeval_monomial`: monomials evaluate as we expect. * `smeval_add`, `smeval_smul`: linearity of evaluation, given an `R`-module. * `smeval_mul`, `smeval_comp`: multiplicativity of evaluation, given power-associativity. * `eval₂_smulOneHom_eq_smeval`, `leval_eq_smeval.linearMap`, `aeval_eq_smeval`, etc.: comparisons ## TODO * `smeval_neg` and `smeval_intCast` for `R` a ring and `S` an `AddCommGroup`. * Nonunital evaluation for polynomials with vanishing constant term for `Pow S ℕ+` (different file?) -/ namespace Polynomial section MulActionWithZero variable {R : Type*} [Semiring R] (r : R) (p : R[X]) {S : Type*} [AddCommMonoid S] [Pow S ℕ] [MulActionWithZero R S] (x : S) /-- Scalar multiplication together with taking a natural number power. -/ def smul_pow : ℕ → R → S := fun n r => r • x ^ n /-- Evaluate a polynomial `p` in the scalar semiring `R` at an element `x` in the target `S` using scalar multiple `R`-action. -/ irreducible_def smeval : S := p.sum (smul_pow x) theorem smeval_eq_sum : p.smeval x = p.sum (smul_pow x) := by rw [smeval_def] @[simp] theorem smeval_C : (C r).smeval x = r • x ^ 0 := by simp only [smeval_eq_sum, smul_pow, zero_smul, sum_C_index] @[simp] theorem smeval_monomial (n : ℕ) : (monomial n r).smeval x = r • x ^ n := by simp only [smeval_eq_sum, smul_pow, zero_smul, sum_monomial_index] theorem eval_eq_smeval : p.eval r = p.smeval r := by rw [eval_eq_sum, smeval_eq_sum] rfl theorem eval₂_smulOneHom_eq_smeval (R : Type*) [Semiring R] {S : Type*} [Semiring S] [Module R S] [IsScalarTower R S S] (p : R[X]) (x : S) : p.eval₂ RingHom.smulOneHom x = p.smeval x := by rw [smeval_eq_sum, eval₂_eq_sum] congr 1 with e a simp only [RingHom.smulOneHom_apply, smul_one_mul, smul_pow] variable (R) @[simp] theorem smeval_zero : (0 : R[X]).smeval x = 0 := by simp only [smeval_eq_sum, sum_zero_index] @[simp] theorem smeval_one : (1 : R[X]).smeval x = 1 • x ^ 0 := by rw [← C_1, smeval_C] simp only [one_smul] @[simp] theorem smeval_X : (X : R[X]).smeval x = x ^ 1 := by simp only [smeval_eq_sum, smul_pow, zero_smul, sum_X_index, one_smul] @[simp] theorem smeval_X_pow {n : ℕ} : (X ^ n : R[X]).smeval x = x ^ n := by simp only [smeval_eq_sum, smul_pow, X_pow_eq_monomial, zero_smul, sum_monomial_index, one_smul] end MulActionWithZero section Module variable (R : Type*) [Semiring R] (p q : R[X]) {S : Type*} [AddCommMonoid S] [Pow S ℕ] [Module R S] (x : S) @[simp] theorem smeval_add : (p + q).smeval x = p.smeval x + q.smeval x := by simp only [smeval_eq_sum] refine sum_add_index p q (smul_pow x) (fun _ ↦ ?_) (fun _ _ _ ↦ ?_) · rw [smul_pow, zero_smul] · rw [smul_pow, smul_pow, smul_pow, add_smul] theorem smeval_natCast (n : ℕ) : (n : R[X]).smeval x = n • x ^ 0 := by induction n with | zero => simp only [smeval_zero, Nat.cast_zero, zero_smul] | succ n ih => rw [n.cast_succ, smeval_add, ih, smeval_one, ← add_nsmul] @[simp] theorem smeval_smul (r : R) : (r • p).smeval x = r • p.smeval x := by induction p using Polynomial.induction_on' with | add p q ph qh => rw [smul_add, smeval_add, ph, qh, ← smul_add, smeval_add] | monomial n a => rw [smul_monomial, smeval_monomial, smeval_monomial, smul_assoc] /-- `Polynomial.smeval` as a linear map. -/ def smeval.linearMap : R[X] →ₗ[R] S where toFun f := f.smeval x map_add' f g := by simp only [smeval_add] map_smul' c f := by simp only [smeval_smul, RingHom.id_apply] @[simp] theorem smeval.linearMap_apply : smeval.linearMap R x p = p.smeval x := rfl theorem leval_coe_eq_smeval {R : Type*} [Semiring R] (r : R) : ⇑(leval r) = fun p => p.smeval r := by rw [funext_iff] intro rw [leval_apply, smeval_def, eval_eq_sum] rfl theorem leval_eq_smeval.linearMap {R : Type*} [Semiring R] (r : R) : leval r = smeval.linearMap R r := by refine LinearMap.ext ?_ intro rw [leval_apply, smeval.linearMap_apply, eval_eq_smeval] end Module section Neg variable (R : Type*) [Ring R] {S : Type*} [AddCommGroup S] [Pow S ℕ] [Module R S] (p q : R[X]) (x : S) @[simp] theorem smeval_neg : (-p).smeval x = -p.smeval x := by rw [← add_eq_zero_iff_eq_neg, ← smeval_add, neg_add_cancel, smeval_zero] @[simp] theorem smeval_sub : (p - q).smeval x = p.smeval x - q.smeval x := by rw [sub_eq_add_neg, smeval_add, smeval_neg, sub_eq_add_neg] theorem smeval_neg_nat (S : Type*) [NonAssocRing S] [Pow S ℕ] [NatPowAssoc S] (q : ℕ[X]) (n : ℕ) : q.smeval (-(n : S)) = q.smeval (-n : ℤ) := by rw [smeval_eq_sum, smeval_eq_sum] simp only [Polynomial.smul_pow, sum_def] simp end Neg section NatPowAssoc /-! In the module docstring for algebras at `Mathlib/Algebra/Algebra/Basic.lean`, we see that `[CommSemiring R] [Semiring S] [Module R S] [IsScalarTower R S S] [SMulCommClass R S S]` is an equivalent way to express `[CommSemiring R] [Semiring S] [Algebra R S]` that allows one to relax the defining structures independently. For non-associative power-associative algebras (e.g., octonions), we replace the `[Semiring S]` with `[NonAssocSemiring S] [Pow S ℕ] [NatPowAssoc S]`. -/ variable (R : Type*) [Semiring R] (r : R) (p q : R[X]) {S : Type*} [NonAssocSemiring S] [Module R S] [Pow S ℕ] (x : S) theorem smeval_C_mul : (C r * p).smeval x = r • p.smeval x := by induction p using Polynomial.induction_on' with | add p q ph qh => simp only [mul_add, smeval_add, ph, qh, smul_add] | monomial n b => simp only [C_mul_monomial, smeval_monomial, mul_smul] variable [NatPowAssoc S] theorem smeval_at_natCast (q : ℕ[X]) : ∀ (n : ℕ), q.smeval (n : S) = q.smeval n := by induction q using Polynomial.induction_on' with | add p q ph qh => intro n simp only [smeval_add, ph, qh, Nat.cast_add] | monomial n a => intro n rw [smeval_monomial, smeval_monomial, nsmul_eq_mul, smul_eq_mul, Nat.cast_mul, Nat.cast_npow] theorem smeval_at_zero : p.smeval (0 : S) = (p.coeff 0) • (1 : S) := by induction p using Polynomial.induction_on' with | add p q ph qh => simp_all only [smeval_add, coeff_add, add_smul] | monomial n a => cases n with | zero => simp only [monomial_zero_left, smeval_C, npow_zero, coeff_C_zero] | succ n => rw [coeff_monomial_succ, smeval_monomial, npow_add, npow_one, mul_zero, zero_smul, smul_zero] section variable [SMulCommClass R S S] theorem smeval_X_mul : (X * p).smeval x = x * p.smeval x := by induction p using Polynomial.induction_on' with | add p q ph qh => simp only [smeval_add, ph, qh, mul_add] | monomial n a => rw [← monomial_one_one_eq_X, monomial_mul_monomial, smeval_monomial, one_mul, npow_add, npow_one, ← mul_smul_comm, smeval_monomial] theorem smeval_X_pow_assoc (m n : ℕ) : x ^ m * x ^ n * p.smeval x = x ^ m * (x ^ n * p.smeval x) := by induction p using Polynomial.induction_on' with | add p q ph qh => simp only [smeval_add, ph, qh, mul_add] | monomial n a => simp only [smeval_monomial, mul_smul_comm, npow_mul_assoc] theorem smeval_X_pow_mul : ∀ (n : ℕ), (X ^ n * p).smeval x = x ^ n * p.smeval x | 0 => by simp [npow_zero, one_mul] | n + 1 => by rw [add_comm, npow_add, mul_assoc, npow_one, smeval_X_mul, smeval_X_pow_mul n, npow_add, smeval_X_pow_assoc, npow_one] theorem smeval_monomial_mul (n : ℕ) : (monomial n r * p).smeval x = r • (x ^ n * p.smeval x) := by induction p using Polynomial.induction_on' with | add r s hr hs => simp only [smeval_add] rw [← C_mul_X_pow_eq_monomial, mul_assoc, smeval_C_mul, smeval_X_pow_mul, smeval_add] | monomial n a => rw [smeval_monomial, monomial_mul_monomial, smeval_monomial, npow_add, mul_smul, mul_smul_comm] end variable [IsScalarTower R S S] theorem smeval_mul_X : (p * X).smeval x = p.smeval x * x := by induction p using Polynomial.induction_on' with | add p q ph qh => simp only [add_mul, smeval_add, ph, qh] | monomial n a => simp only [← monomial_one_one_eq_X, monomial_mul_monomial, smeval_monomial, mul_one, npow_add, smul_mul_assoc, npow_one] theorem smeval_assoc_X_pow (m n : ℕ) : p.smeval x * x ^ m * x ^ n = p.smeval x * (x ^ m * x ^ n) := by induction p using Polynomial.induction_on' with | add p q ph qh => simp only [smeval_add, ph, qh, add_mul] | monomial n a => rw [smeval_monomial, smul_mul_assoc, smul_mul_assoc, npow_mul_assoc, ← smul_mul_assoc] theorem smeval_mul_X_pow : ∀ (n : ℕ), (p * X ^ n).smeval x = p.smeval x * x ^ n | 0 => by simp only [npow_zero, mul_one] | n + 1 => by rw [npow_add, ← mul_assoc, npow_one, smeval_mul_X, smeval_mul_X_pow n, npow_add, ← smeval_assoc_X_pow, npow_one] variable [SMulCommClass R S S] theorem smeval_mul : (p * q).smeval x = p.smeval x * q.smeval x := by induction p using Polynomial.induction_on' with | add r s hr hs => simp only [hr, hs, smeval_add, add_mul] | monomial n a => simp only [smeval_monomial, smeval_monomial_mul, smul_mul_assoc] theorem smeval_pow : ∀ (n : ℕ), (p ^ n).smeval x = (p.smeval x) ^ n | 0 => by simp only [npow_zero, smeval_one, one_smul] | n + 1 => by rw [npow_add, smeval_mul, smeval_pow n, pow_one, npow_add, npow_one] theorem smeval_comp : (p.comp q).smeval x = p.smeval (q.smeval x) := by induction p using Polynomial.induction_on' with | add r s hr hs => simp [add_comp, hr, hs, smeval_add] | monomial n a => simp [smeval_monomial, smeval_C_mul, smeval_pow] end NatPowAssoc section Commute variable (R : Type*) [Semiring R] (p q : R[X]) {S : Type*} [Semiring S] [Module R S] [IsScalarTower R S S] [SMulCommClass R S S] {x y : S} theorem smeval_commute_left (hc : Commute x y) : Commute (p.smeval x) y := by induction p using Polynomial.induction_on' with | add r s hr hs => exact (smeval_add R r s x) ▸ Commute.add_left hr hs | monomial n a => simp only [smeval_monomial] refine Commute.smul_left ?_ a induction n with | zero => simp only [npow_zero, Commute.one_left] | succ n ih => refine (commute_iff_eq (x ^ (n + 1)) y).mpr ?_ rw [commute_iff_eq (x ^ n) y] at ih rw [pow_succ, ← mul_assoc, ← ih] exact Commute.right_comm hc (x ^ n) theorem smeval_commute (hc : Commute x y) : Commute (p.smeval x) (q.smeval y) := by induction p using Polynomial.induction_on' with | add r s hr hs => exact (smeval_add R r s x) ▸ Commute.add_left hr hs | monomial n a => simp only [smeval_monomial] refine Commute.smul_left ?_ a induction n with | zero => simp only [npow_zero, Commute.one_left] | succ n ih => refine (commute_iff_eq (x ^ (n + 1)) (q.smeval y)).mpr ?_ rw [commute_iff_eq (x ^ n) (q.smeval y)] at ih have hxq : x * q.smeval y = q.smeval y * x := by refine (commute_iff_eq x (q.smeval y)).mp ?_ exact Commute.symm (smeval_commute_left R q (Commute.symm hc)) rw [pow_succ, ← mul_assoc, ← ih, mul_assoc, hxq, mul_assoc] end Commute section Algebra theorem aeval_eq_smeval {R : Type*} [CommSemiring R] {S : Type*} [Semiring S] [Algebra R S] (x : S) (p : R[X]) : aeval x p = p.smeval x := by rw [aeval_def, eval₂_def, Algebra.algebraMap_eq_smul_one', smeval_def] simp only [Algebra.smul_mul_assoc, one_mul] exact rfl theorem aeval_coe_eq_smeval {R : Type*} [CommSemiring R] {S : Type*} [Semiring S] [Algebra R S] (x : S) : ⇑(aeval x) = fun (p : R[X]) => p.smeval x := funext fun p => aeval_eq_smeval x p end Algebra end Polynomial
.lake/packages/mathlib/Mathlib/Algebra/Polynomial/Homogenize.lean
import Mathlib.Data.Finsupp.Notation import Mathlib.RingTheory.MvPolynomial.Homogeneous /-! # Homogenize a univariate polynomial In this file we define a function `Polynomial.homogenize p n` that takes a polynomial `p` and a natural number `n` and returns a homogeneous bivariate polynomial of degree `n`. If `n` is at least the degree of `p`, then `(homogenize p n).eval ![x, 1] = p.eval x`. We use `MvPolynomial (Fin 2) R` to represent bivariate polynomials instead of `R[X][Y]` (i.e., `Polynomial (Polynomial R)`), because Mathlib has a theory about homogeneous multivariate polynomials, but not about homogeneous bivariate polynomials encoded as `R[X][Y]`. -/ open Finset namespace Polynomial section CommSemiring variable {R : Type*} [CommSemiring R] /-- Given a polynomial `p` and a number `n ≥ natDegree p`, returns a homogeneous bivariate polynomial `q` of degree `n` such that `q(x, 1) = p(x)`. It is defined as `∑ k + l = n, a_k X_0^k X_1^l`, where `a_k` is the `k`th coefficient of `p`. -/ noncomputable def homogenize (p : R[X]) (n : ℕ) : MvPolynomial (Fin 2) R := ∑ kl ∈ antidiagonal n, .monomial (fun₀ | 0 => kl.1 | 1 => kl.2) (p.coeff kl.1) @[simp] lemma homogenize_zero (n : ℕ) : homogenize (0 : R[X]) n = 0 := by simp [homogenize] @[simp] lemma homogenize_add (p q : R[X]) (n : ℕ) : homogenize (p + q) n = homogenize p n + homogenize q n := by simp [homogenize, Finset.sum_add_distrib] @[simp] lemma homogenize_smul {S : Type*} [Semiring S] [Module S R] (c : S) (p : R[X]) (n : ℕ) : homogenize (c • p) n = c • homogenize p n := by simp [homogenize, Finset.smul_sum, MvPolynomial.smul_monomial] /-- `homogenize` as a bundled linear map. -/ @[simps] noncomputable def homogenizeLM (n : ℕ) : R[X] →ₗ[R] MvPolynomial (Fin 2) R where toFun p := homogenize p n map_add' := (homogenize_add · · n) map_smul' := (homogenize_smul · · n) @[simp] lemma homogenize_finsetSum {ι : Type*} (s : Finset ι) (p : ι → R[X]) (n : ℕ) : homogenize (∑ i ∈ s, p i) n = ∑ i ∈ s, homogenize (p i) n := _root_.map_sum (homogenizeLM n) p s lemma homogenize_map {S : Type*} [CommSemiring S] (f : R →+* S) (p : R[X]) (n : ℕ) : homogenize (p.map f) n = MvPolynomial.map f (homogenize p n) := by simp [homogenize] @[simp] lemma homogenize_C_mul (c : R) (p : R[X]) (n : ℕ) : homogenize (C c * p) n = .C c * homogenize p n := by simp only [C_mul', homogenize_smul, MvPolynomial.C_mul'] @[simp] lemma homogenize_monomial {m n : ℕ} (h : m ≤ n) (r : R) : homogenize (monomial m r) n = .monomial (fun₀ | 0 => m | 1 => n - m) r := by rw [homogenize, Finset.sum_eq_single (a := (m, n - m))] · simp · aesop (add simp coeff_monomial) · simp [h] lemma homogenize_monomial_of_lt {m n : ℕ} (h : n < m) (r : R) : homogenize (monomial m r) n = 0 := by rw [homogenize] apply Finset.sum_eq_zero aesop (add simp coeff_monomial) @[simp] lemma homogenize_X_pow {m n : ℕ} (h : m ≤ n) : homogenize (X ^ m : R[X]) n = .X 0 ^ m * .X 1 ^ (n - m) := by rw [X_pow_eq_monomial, homogenize_monomial h, Finsupp.update_eq_add_single (by simp), MvPolynomial.monomial_single_add, ← MvPolynomial.X_pow_eq_monomial] @[simp] lemma homogenize_X {n : ℕ} (hn : n ≠ 0) : homogenize (X : R[X]) n = .X 0 * .X 1 ^ (n - 1) := by rw [← pow_one X, homogenize_X_pow, pow_one] rwa [Nat.one_le_iff_ne_zero] @[simp] lemma homogenize_C (c : R) (n : ℕ) : homogenize (.C c) n = .C c * .X 1 ^ n := by simpa [MvPolynomial.C_mul_X_pow_eq_monomial] using homogenize_monomial (Nat.zero_le n) c @[simp] lemma homogenize_one (n : ℕ) : homogenize (1 : R[X]) n = .X 1 ^ n := by simpa using homogenize_C (1 : R) n lemma coeff_homogenize (p : R[X]) (n : ℕ) (m : Fin 2 →₀ ℕ) : (homogenize p n).coeff m = if m 0 + m 1 = n then coeff p (m 0) else 0 := by induction p using Polynomial.induction_on' with | add p q ihp ihq => simp [*, ite_add_ite] | monomial k c => rcases le_or_gt k n with hkn | hnk · rw [homogenize_monomial hkn, coeff_monomial, MvPolynomial.coeff_monomial] have : (fun₀ | 0 => m 0 | 1 => m 1) = m := by ext i; fin_cases i <;> simp aesop · aesop (add simp homogenize_monomial_of_lt) (add simp coeff_monomial) lemma eval₂_homogenize_of_eq_one {S : Type*} [CommSemiring S] {p : R[X]} {n : ℕ} (hn : natDegree p ≤ n) (f : R →+* S) (g : Fin 2 → S) (hg : g 1 = 1) : MvPolynomial.eval₂ f g (p.homogenize n) = p.eval₂ f (g 0) := by apply Polynomial.induction_with_natDegree_le (fun p ↦ MvPolynomial.eval₂ f g (p.homogenize n) = p.eval₂ f (g 0)) (N := n) · simp · simp +contextual [hg] · simp +contextual · assumption lemma aeval_homogenize_of_eq_one {A : Type*} [CommSemiring A] [Algebra R A] {p : R[X]} {n : ℕ} (hn : natDegree p ≤ n) (g : Fin 2 → A) (hg : g 1 = 1) : MvPolynomial.aeval g (p.homogenize n) = aeval (g 0) p := by apply eval₂_homogenize_of_eq_one <;> assumption /-- If `deg p ≤ n`, then `homogenize p n (x, 1) = p x`. -/ @[simp] lemma aeval_homogenize_X_one (p : R[X]) {n : ℕ} (hn : natDegree p ≤ n) : MvPolynomial.aeval ![X, 1] (p.homogenize n) = p := by rw [aeval_homogenize_of_eq_one] <;> simp [*] @[simp] lemma isHomogeneous_homogenize {n : ℕ} (p : R[X]) : (p.homogenize n).IsHomogeneous n := by refine MvPolynomial.IsHomogeneous.sum _ _ _ ?_ simp only [Prod.forall, mem_antidiagonal] rintro a b rfl apply MvPolynomial.isHomogeneous_monomial simp [Finsupp.update_eq_add_single] lemma homogenize_eq_of_isHomogeneous {p : R[X]} {n : ℕ} {q : MvPolynomial (Fin 2) R} (hq : q.IsHomogeneous n) (hpq : MvPolynomial.aeval ![X, 1] q = p) : p.homogenize n = q := by subst p rw [q.as_sum] simp only [MvPolynomial.aeval_sum, MvPolynomial.aeval_monomial, ← C_eq_algebraMap, homogenize_finsetSum, homogenize_C_mul] refine Finset.sum_congr rfl fun m hm ↦ ?_ rw [MvPolynomial.monomial_eq] congr 1 obtain rfl : m.weight 1 = n := hq <| by simpa using hm simp [Finsupp.prod_fintype, Finsupp.weight_apply, Finsupp.sum_fintype, Fin.prod_univ_two, Fin.sum_univ_two] lemma homogenize_mul (p q : R[X]) {m n : ℕ} (hm : natDegree p ≤ m) (hn : natDegree q ≤ n) : homogenize (p * q) (m + n) = homogenize p m * homogenize q n := by apply homogenize_eq_of_isHomogeneous · apply_rules [MvPolynomial.IsHomogeneous.mul, isHomogeneous_homogenize] · simp [*] lemma homogenize_finsetProd {ι : Type*} {s : Finset ι} {p : ι → R[X]} {n : ι → ℕ} (h : ∀ i ∈ s, (p i).natDegree ≤ n i) : homogenize (∏ i ∈ s, p i) (∑ i ∈ s, n i) = ∏ i ∈ s, homogenize (p i) (n i) := by induction s using Finset.cons_induction with | empty => simp | cons i s hi ihs => simp only [prod_cons, sum_cons, forall_mem_cons] at * rw [homogenize_mul _ _ h.1, ihs h.2] exact (natDegree_prod_le _ _).trans (sum_le_sum h.2) lemma homogenize_dvd [NoZeroDivisors R] {p q : R[X]} (h : p ∣ q) : homogenize p p.natDegree ∣ homogenize q q.natDegree := by rcases h with ⟨r, rfl⟩ obtain rfl | rfl | ⟨hp₀, hr₀⟩ : p = 0 ∨ r = 0 ∨ p ≠ 0 ∧ r ≠ 0 := by tauto · simp · simp · rw [natDegree_mul hp₀ hr₀, homogenize_mul _ _ le_rfl le_rfl] apply dvd_mul_right end CommSemiring section CommRing variable {R : Type*} [CommRing R] @[simp] lemma homogenize_neg (p : R[X]) (n : ℕ) : (-p).homogenize n = -p.homogenize n := map_neg (homogenizeLM n) p @[simp] lemma homogenize_sub (p q : R[X]) (n : ℕ) : (p - q).homogenize n = p.homogenize n - q.homogenize n := map_sub (homogenizeLM n) p q end CommRing variable {K : Type*} [Semifield K] lemma eval_homogenize {p : K[X]} {n : ℕ} (hn : p.natDegree ≤ n) (x : Fin 2 → K) (hx : x 1 ≠ 0) : MvPolynomial.eval x (p.homogenize n) = p.eval (x 0 / x 1) * x 1 ^ n := by simp only [homogenize, Polynomial.eval_eq_sum_range' (Nat.lt_succ.mpr hn), Finset.Nat.sum_antidiagonal_eq_sum_range_succ_mk, Finset.sum_mul, MvPolynomial.eval_sum] refine Finset.sum_congr rfl fun k hk ↦ ?_ rw [MvPolynomial.eval_monomial, Finsupp.update_eq_add_single, Finsupp.prod_add_index', Finsupp.prod_single_index, Finsupp.prod_single_index, pow_sub₀] · ring all_goals simp_all [pow_add, Nat.lt_add_one_iff] end Polynomial
.lake/packages/mathlib/Mathlib/Algebra/Polynomial/Expand.lean
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 [notMem_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
.lake/packages/mathlib/Mathlib/Algebra/Polynomial/Mirror.lean
import Mathlib.Algebra.BigOperators.NatAntidiagonal import Mathlib.Algebra.Polynomial.Reverse /-! # "Mirror" of a univariate polynomial In this file we define `Polynomial.mirror`, a variant of `Polynomial.reverse`. The difference between `reverse` and `mirror` is that `reverse` will decrease the degree if the polynomial is divisible by `X`. ## Main definitions - `Polynomial.mirror` ## Main results - `Polynomial.mirror_mul_of_domain`: `mirror` preserves multiplication. - `Polynomial.irreducible_of_mirror`: an irreducibility criterion involving `mirror` -/ namespace Polynomial section Semiring variable {R : Type*} [Semiring R] (p q : R[X]) /-- mirror of a polynomial: reverses the coefficients while preserving `Polynomial.natDegree` -/ noncomputable def mirror := p.reverse * X ^ p.natTrailingDegree @[simp] theorem mirror_zero : (0 : R[X]).mirror = 0 := by simp [mirror] theorem mirror_monomial (n : ℕ) (a : R) : (monomial n a).mirror = monomial n a := by classical by_cases ha : a = 0 · rw [ha, monomial_zero_right, mirror_zero] · rw [mirror, reverse, natDegree_monomial n a, if_neg ha, natTrailingDegree_monomial ha, ← C_mul_X_pow_eq_monomial, reflect_C_mul_X_pow, revAt_le (le_refl n), tsub_self, pow_zero, mul_one] theorem mirror_C (a : R) : (C a).mirror = C a := mirror_monomial 0 a theorem mirror_X : X.mirror = (X : R[X]) := mirror_monomial 1 (1 : R) theorem mirror_natDegree : p.mirror.natDegree = p.natDegree := by by_cases hp : p = 0 · rw [hp, mirror_zero] nontriviality R rw [mirror, natDegree_mul', reverse_natDegree, natDegree_X_pow, tsub_add_cancel_of_le p.natTrailingDegree_le_natDegree] rwa [leadingCoeff_X_pow, mul_one, reverse_leadingCoeff, Ne, trailingCoeff_eq_zero] theorem mirror_natTrailingDegree : p.mirror.natTrailingDegree = p.natTrailingDegree := by by_cases hp : p = 0 · rw [hp, mirror_zero] · rw [mirror, natTrailingDegree_mul_X_pow ((mt reverse_eq_zero.mp) hp), natTrailingDegree_reverse, zero_add] theorem coeff_mirror (n : ℕ) : p.mirror.coeff n = p.coeff (revAt (p.natDegree + p.natTrailingDegree) n) := by by_cases h2 : p.natDegree < n · rw [coeff_eq_zero_of_natDegree_lt (by rwa [mirror_natDegree])] by_cases h1 : n ≤ p.natDegree + p.natTrailingDegree · rw [revAt_le h1, coeff_eq_zero_of_lt_natTrailingDegree] exact (tsub_lt_iff_left h1).mpr (Nat.add_lt_add_right h2 _) · rw [← revAtFun_eq, revAtFun, if_neg h1, coeff_eq_zero_of_natDegree_lt h2] rw [not_lt] at h2 rw [revAt_le (h2.trans (Nat.le_add_right _ _))] by_cases h3 : p.natTrailingDegree ≤ n · rw [← tsub_add_eq_add_tsub h2, ← tsub_tsub_assoc h2 h3, mirror, coeff_mul_X_pow', if_pos h3, coeff_reverse, revAt_le (tsub_le_self.trans h2)] rw [not_le] at h3 rw [coeff_eq_zero_of_natDegree_lt (lt_tsub_iff_right.mpr (Nat.add_lt_add_left h3 _))] exact coeff_eq_zero_of_lt_natTrailingDegree (by rwa [mirror_natTrailingDegree]) --TODO: Extract `Finset.sum_range_rev_at` lemma. theorem mirror_eval_one : p.mirror.eval 1 = p.eval 1 := by simp_rw [eval_eq_sum_range, one_pow, mul_one, mirror_natDegree] refine Finset.sum_bij_ne_zero ?_ ?_ ?_ ?_ ?_ · exact fun n _ _ => revAt (p.natDegree + p.natTrailingDegree) n · intro n hn hp rw [Finset.mem_range_succ_iff] at * rw [revAt_le (hn.trans (Nat.le_add_right _ _))] rw [tsub_le_iff_tsub_le, add_comm, add_tsub_cancel_right, ← mirror_natTrailingDegree] exact natTrailingDegree_le_of_ne_zero hp · exact fun n₁ _ _ _ _ _ h => by rw [← @revAt_invol _ n₁, h, revAt_invol] · intro n hn hp use revAt (p.natDegree + p.natTrailingDegree) n refine ⟨?_, ?_, revAt_invol⟩ · rw [Finset.mem_range_succ_iff] at * rw [revAt_le (hn.trans (Nat.le_add_right _ _))] rw [tsub_le_iff_tsub_le, add_comm, add_tsub_cancel_right] exact natTrailingDegree_le_of_ne_zero hp · change p.mirror.coeff _ ≠ 0 rwa [coeff_mirror, revAt_invol] · exact fun n _ _ => p.coeff_mirror n theorem mirror_mirror : p.mirror.mirror = p := Polynomial.ext fun n => by rw [coeff_mirror, coeff_mirror, mirror_natDegree, mirror_natTrailingDegree, revAt_invol] variable {p q} theorem mirror_involutive : Function.Involutive (mirror : R[X] → R[X]) := mirror_mirror theorem mirror_eq_iff : p.mirror = q ↔ p = q.mirror := mirror_involutive.eq_iff @[simp] theorem mirror_inj : p.mirror = q.mirror ↔ p = q := mirror_involutive.injective.eq_iff @[simp] theorem mirror_eq_zero : p.mirror = 0 ↔ p = 0 := ⟨fun h => by rw [← p.mirror_mirror, h, mirror_zero], fun h => by rw [h, mirror_zero]⟩ variable (p q) @[simp] theorem mirror_trailingCoeff : p.mirror.trailingCoeff = p.leadingCoeff := by rw [leadingCoeff, trailingCoeff, mirror_natTrailingDegree, coeff_mirror, revAt_le (Nat.le_add_left _ _), add_tsub_cancel_right] @[simp] theorem mirror_leadingCoeff : p.mirror.leadingCoeff = p.trailingCoeff := by rw [← p.mirror_mirror, mirror_trailingCoeff, p.mirror_mirror] theorem coeff_mul_mirror : (p * p.mirror).coeff (p.natDegree + p.natTrailingDegree) = p.sum fun _ => (· ^ 2) := by rw [coeff_mul, Finset.Nat.sum_antidiagonal_eq_sum_range_succ_mk] refine (Finset.sum_congr rfl fun n hn => ?_).trans (p.sum_eq_of_subset (fun _ ↦ (· ^ 2)) (fun _ ↦ zero_pow two_ne_zero) fun n hn ↦ Finset.mem_range_succ_iff.mpr ((le_natDegree_of_mem_supp n hn).trans (Nat.le_add_right _ _))).symm rw [coeff_mirror, ← revAt_le (Finset.mem_range_succ_iff.mp hn), revAt_invol, ← sq] variable [NoZeroDivisors R] theorem natDegree_mul_mirror : (p * p.mirror).natDegree = 2 * p.natDegree := by by_cases hp : p = 0 · rw [hp, zero_mul, natDegree_zero, mul_zero] rw [natDegree_mul hp (mt mirror_eq_zero.mp hp), mirror_natDegree, two_mul] theorem natTrailingDegree_mul_mirror : (p * p.mirror).natTrailingDegree = 2 * p.natTrailingDegree := by by_cases hp : p = 0 · rw [hp, zero_mul, natTrailingDegree_zero, mul_zero] rw [natTrailingDegree_mul hp (mt mirror_eq_zero.mp hp), mirror_natTrailingDegree, two_mul] end Semiring section Ring variable {R : Type*} [Ring R] (p q : R[X]) theorem mirror_neg : (-p).mirror = -p.mirror := by rw [mirror, mirror, reverse_neg, natTrailingDegree_neg, neg_mul_eq_neg_mul] variable [NoZeroDivisors R] theorem mirror_mul_of_domain : (p * q).mirror = p.mirror * q.mirror := by by_cases hp : p = 0 · rw [hp, zero_mul, mirror_zero, zero_mul] by_cases hq : q = 0 · rw [hq, mul_zero, mirror_zero, mul_zero] rw [mirror, mirror, mirror, reverse_mul_of_domain, natTrailingDegree_mul hp hq, pow_add] rw [mul_assoc, ← mul_assoc q.reverse, ← X_pow_mul (p := reverse q)] repeat' rw [mul_assoc] theorem mirror_smul (a : R) : (a • p).mirror = a • p.mirror := by rw [← C_mul', ← C_mul', mirror_mul_of_domain, mirror_C] end Ring section CommRing variable {R : Type*} [CommRing R] [NoZeroDivisors R] {f : R[X]} theorem irreducible_of_mirror (h1 : ¬IsUnit f) (h2 : ∀ k, f * f.mirror = k * k.mirror → k = f ∨ k = -f ∨ k = f.mirror ∨ k = -f.mirror) (h3 : IsRelPrime f f.mirror) : Irreducible f := by constructor · exact h1 · intro g h fgh let k := g * h.mirror have key : f * f.mirror = k * k.mirror := by rw [fgh, mirror_mul_of_domain, mirror_mul_of_domain, mirror_mirror, mul_assoc, mul_comm h, mul_comm g.mirror, mul_assoc, ← mul_assoc] have g_dvd_f : g ∣ f := by rw [fgh] exact dvd_mul_right g h have h_dvd_f : h ∣ f := by rw [fgh] exact dvd_mul_left h g have g_dvd_k : g ∣ k := dvd_mul_right g h.mirror have h_dvd_k_rev : h ∣ k.mirror := by rw [mirror_mul_of_domain, mirror_mirror] exact dvd_mul_left h g.mirror have hk := h2 k key rcases hk with (hk | hk | hk | hk) · exact Or.inr (h3 h_dvd_f (by rwa [← hk])) · exact Or.inr (h3 h_dvd_f (by rwa [← neg_eq_iff_eq_neg.mpr hk, mirror_neg, dvd_neg])) · exact Or.inl (h3 g_dvd_f (by rwa [← hk])) · exact Or.inl (h3 g_dvd_f (by rwa [← neg_eq_iff_eq_neg.mpr hk, dvd_neg])) end CommRing end Polynomial
.lake/packages/mathlib/Mathlib/Algebra/Polynomial/Laurent.lean
import Mathlib.Algebra.Polynomial.AlgebraMap import Mathlib.Algebra.Polynomial.Reverse import Mathlib.Algebra.Polynomial.Inductions import Mathlib.RingTheory.Localization.Away.Basic /-! # Laurent polynomials We introduce Laurent polynomials over a semiring `R`. Mathematically, they are expressions of the form $$ \sum_{i \in \mathbb{Z}} a_i T ^ i $$ where the sum extends over a finite subset of `ℤ`. Thus, negative exponents are allowed. The coefficients come from the semiring `R` and the variable `T` commutes with everything. Since we are going to convert back and forth between polynomials and Laurent polynomials, we decided to maintain some distinction by using the symbol `T`, rather than `X`, as the variable for Laurent polynomials. ## Notation The symbol `R[T;T⁻¹]` stands for `LaurentPolynomial R`. We also define * `C : R →+* R[T;T⁻¹]` the inclusion of constant polynomials, analogous to the one for `R[X]`; * `T : ℤ → R[T;T⁻¹]` the sequence of powers of the variable `T`. ## Implementation notes We define Laurent polynomials as `AddMonoidAlgebra R ℤ`. Thus, they are essentially `Finsupp`s `ℤ →₀ R`. This choice differs from the current irreducible design of `Polynomial`, that instead shields away the implementation via `Finsupp`s. It is closer to the original definition of polynomials. As a consequence, `LaurentPolynomial` plays well with polynomials, but there is a little roughness in establishing the API, since the `Finsupp` implementation of `R[X]` is well-shielded. Unlike the case of polynomials, I felt that the exponent notation was not too easy to use, as only natural exponents would be allowed. Moreover, in the end, it seems likely that we should aim to perform computations on exponents in `ℤ` anyway and separating this via the symbol `T` seems convenient. I made a *heavy* use of `simp` lemmas, aiming to bring Laurent polynomials to the form `C a * T n`. Any comments or suggestions for improvements is greatly appreciated! ## Future work Lots is missing! -- (Riccardo) add inclusion into Laurent series. -- A "better" definition of `trunc` would be as an `R`-linear map. This works: -- ``` -- def trunc : R[T;T⁻¹] →[R] R[X] := -- refine (?_ : R[ℕ] →[R] R[X]).comp ?_ -- · exact ⟨(toFinsuppIso R).symm, by simp⟩ -- · refine ⟨fun r ↦ comapDomain _ r -- (Set.injOn_of_injective (fun _ _ ↦ Int.ofNat.inj) _), ?_⟩ -- exact fun r f ↦ comapDomain_smul .. -- ``` -- but it would make sense to bundle the maps better, for a smoother user experience. -- I (DT) did not have the strength to embark on this (possibly short!) journey, after getting to -- this stage of the Laurent process! -- This would likely involve adding a `comapDomain` analogue of -- `AddMonoidAlgebra.mapDomainAlgHom` and an `R`-linear version of -- `Polynomial.toFinsuppIso`. -- Add `degree, intDegree, intTrailingDegree, leadingCoeff, trailingCoeff,...`. -/ open Polynomial Function AddMonoidAlgebra Finsupp noncomputable section variable {R S : Type*} /-- The semiring of Laurent polynomials with coefficients in the semiring `R`. We denote it by `R[T;T⁻¹]`. The ring homomorphism `C : R →+* R[T;T⁻¹]` includes `R` as the constant polynomials. -/ abbrev LaurentPolynomial (R : Type*) [Semiring R] := AddMonoidAlgebra R ℤ @[nolint docBlame] scoped[LaurentPolynomial] notation:9000 R "[T;T⁻¹]" => LaurentPolynomial R open LaurentPolynomial @[ext] theorem LaurentPolynomial.ext [Semiring R] {p q : R[T;T⁻¹]} (h : ∀ a, p a = q a) : p = q := Finsupp.ext h /-- The ring homomorphism, taking a polynomial with coefficients in `R` to a Laurent polynomial with coefficients in `R`. -/ def Polynomial.toLaurent [Semiring R] : R[X] →+* R[T;T⁻¹] := (mapDomainRingHom R Int.ofNatHom).comp (toFinsuppIso R) /-- This is not a simp lemma, as it is usually preferable to use the lemmas about `C` and `X` instead. -/ theorem Polynomial.toLaurent_apply [Semiring R] (p : R[X]) : toLaurent p = p.toFinsupp.mapDomain (↑) := rfl /-- The `R`-algebra map, taking a polynomial with coefficients in `R` to a Laurent polynomial with coefficients in `R`. -/ def Polynomial.toLaurentAlg [CommSemiring R] : R[X] →ₐ[R] R[T;T⁻¹] := (mapDomainAlgHom R R Int.ofNatHom).comp (toFinsuppIsoAlg R).toAlgHom @[simp] lemma Polynomial.coe_toLaurentAlg [CommSemiring R] : (toLaurentAlg : R[X] → R[T;T⁻¹]) = toLaurent := rfl theorem Polynomial.toLaurentAlg_apply [CommSemiring R] (f : R[X]) : toLaurentAlg f = toLaurent f := rfl namespace LaurentPolynomial section Semiring variable [Semiring R] theorem single_zero_one_eq_one : (Finsupp.single 0 1 : R[T;T⁻¹]) = (1 : R[T;T⁻¹]) := rfl /-! ### The functions `C` and `T`. -/ /-- The ring homomorphism `C`, including `R` into the ring of Laurent polynomials over `R` as the constant Laurent polynomials. -/ def C : R →+* R[T;T⁻¹] := singleZeroRingHom theorem algebraMap_apply {R A : Type*} [CommSemiring R] [Semiring A] [Algebra R A] (r : R) : algebraMap R (LaurentPolynomial A) r = C (algebraMap R A r) := rfl /-- When we have `[CommSemiring R]`, the function `C` is the same as `algebraMap R R[T;T⁻¹]`. (But note that `C` is defined when `R` is not necessarily commutative, in which case `algebraMap` is not available.) -/ theorem C_eq_algebraMap {R : Type*} [CommSemiring R] (r : R) : C r = algebraMap R R[T;T⁻¹] r := rfl theorem single_eq_C (r : R) : Finsupp.single 0 r = C r := rfl @[simp] lemma C_apply (t : R) (n : ℤ) : C t n = if n = 0 then t else 0 := by rw [← single_eq_C, Finsupp.single_apply]; aesop /-- The function `n ↦ T ^ n`, implemented as a sequence `ℤ → R[T;T⁻¹]`. Using directly `T ^ n` does not work, since we want the exponents to be of Type `ℤ` and there is no `ℤ`-power defined on `R[T;T⁻¹]`. Using that `T` is a unit introduces extra coercions. For these reasons, the definition of `T` is as a sequence. -/ def T (n : ℤ) : R[T;T⁻¹] := Finsupp.single n 1 @[simp] lemma T_apply (m n : ℤ) : (T n : R[T;T⁻¹]) m = if n = m then 1 else 0 := Finsupp.single_apply @[simp] theorem T_zero : (T 0 : R[T;T⁻¹]) = 1 := rfl theorem T_add (m n : ℤ) : (T (m + n) : R[T;T⁻¹]) = T m * T n := by simp [T, single_mul_single] theorem T_sub (m n : ℤ) : (T (m - n) : R[T;T⁻¹]) = T m * T (-n) := by rw [← T_add, sub_eq_add_neg] @[simp] theorem T_pow (m : ℤ) (n : ℕ) : (T m ^ n : R[T;T⁻¹]) = T (n * m) := by rw [T, T, single_pow, one_pow, nsmul_eq_mul] /-- The `simp` version of `mul_assoc`, in the presence of `T`'s. -/ @[simp] theorem mul_T_assoc (f : R[T;T⁻¹]) (m n : ℤ) : f * T m * T n = f * T (m + n) := by simp [← T_add, mul_assoc] @[simp] theorem single_eq_C_mul_T (r : R) (n : ℤ) : (Finsupp.single n r : R[T;T⁻¹]) = (C r * T n : R[T;T⁻¹]) := by simp [C, T, single_mul_single] -- This lemma locks in the right changes and is what Lean proved directly. -- The actual `simp`-normal form of a Laurent monomial is `C a * T n`, whenever it can be reached. @[simp] theorem _root_.Polynomial.toLaurent_C_mul_T (n : ℕ) (r : R) : (toLaurent (Polynomial.monomial n r) : R[T;T⁻¹]) = C r * T n := show Finsupp.mapDomain (↑) (monomial n r).toFinsupp = (C r * T n : R[T;T⁻¹]) by rw [toFinsupp_monomial, Finsupp.mapDomain_single, single_eq_C_mul_T] @[simp] theorem _root_.Polynomial.toLaurent_C (r : R) : toLaurent (Polynomial.C r) = C r := by convert Polynomial.toLaurent_C_mul_T 0 r simp only [Int.ofNat_zero, T_zero, mul_one] @[simp] theorem _root_.Polynomial.toLaurent_comp_C : toLaurent (R := R) ∘ Polynomial.C = C := funext Polynomial.toLaurent_C @[simp] theorem _root_.Polynomial.toLaurent_X : (toLaurent Polynomial.X : R[T;T⁻¹]) = T 1 := by have : (Polynomial.X : R[X]) = monomial 1 1 := by simp [← C_mul_X_pow_eq_monomial] simp [this, Polynomial.toLaurent_C_mul_T] @[simp] theorem _root_.Polynomial.toLaurent_one : (Polynomial.toLaurent : R[X] → R[T;T⁻¹]) 1 = 1 := map_one Polynomial.toLaurent @[simp] theorem _root_.Polynomial.toLaurent_C_mul_eq (r : R) (f : R[X]) : toLaurent (Polynomial.C r * f) = C r * toLaurent f := by simp only [map_mul, Polynomial.toLaurent_C] @[simp] theorem _root_.Polynomial.toLaurent_X_pow (n : ℕ) : toLaurent (X ^ n : R[X]) = T n := by simp only [map_pow, Polynomial.toLaurent_X, T_pow, mul_one] theorem _root_.Polynomial.toLaurent_C_mul_X_pow (n : ℕ) (r : R) : toLaurent (Polynomial.C r * X ^ n) = C r * T n := by simp only [map_mul, Polynomial.toLaurent_C, Polynomial.toLaurent_X_pow] instance invertibleT (n : ℤ) : Invertible (T n : R[T;T⁻¹]) where invOf := T (-n) invOf_mul_self := by rw [← T_add, neg_add_cancel, T_zero] mul_invOf_self := by rw [← T_add, add_neg_cancel, T_zero] @[simp] theorem invOf_T (n : ℤ) : ⅟(T n : R[T;T⁻¹]) = T (-n) := rfl theorem isUnit_T (n : ℤ) : IsUnit (T n : R[T;T⁻¹]) := isUnit_of_invertible _ @[elab_as_elim] protected theorem induction_on {M : R[T;T⁻¹] → Prop} (p : R[T;T⁻¹]) (h_C : ∀ a, M (C a)) (h_add : ∀ {p q}, M p → M q → M (p + q)) (h_C_mul_T : ∀ (n : ℕ) (a : R), M (C a * T n) → M (C a * T (n + 1))) (h_C_mul_T_Z : ∀ (n : ℕ) (a : R), M (C a * T (-n)) → M (C a * T (-n - 1))) : M p := by have A : ∀ {n : ℤ} {a : R}, M (C a * T n) := by intro n a refine Int.induction_on n ?_ ?_ ?_ · simpa only [T_zero, mul_one] using h_C a · exact fun m => h_C_mul_T m a · exact fun m => h_C_mul_T_Z m a have B : ∀ s : Finset ℤ, M (s.sum fun n : ℤ => C (p n) * T n) := by apply Finset.induction · convert h_C 0 simp only [Finset.sum_empty, map_zero] · intro n s ns ih rw [Finset.sum_insert ns] exact h_add A ih convert B p.support ext a simp_rw [← single_eq_C_mul_T] rw [Finset.sum_apply', Finset.sum_eq_single a, single_eq_same] · intro b _ hb rw [single_eq_of_ne' hb] · intro ha rw [single_eq_same, notMem_support_iff.mp ha] /-- To prove something about Laurent polynomials, it suffices to show that * the condition is closed under taking sums, and * it holds for monomials. -/ @[elab_as_elim] protected theorem induction_on' {motive : R[T;T⁻¹] → Prop} (p : R[T;T⁻¹]) (add : ∀ p q, motive p → motive q → motive (p + q)) (C_mul_T : ∀ (n : ℤ) (a : R), motive (C a * T n)) : motive p := by refine p.induction_on (fun a => ?_) (fun {p q} => add p q) ?_ ?_ <;> try exact fun n f _ => C_mul_T _ f convert C_mul_T 0 a exact (mul_one _).symm theorem commute_T (n : ℤ) (f : R[T;T⁻¹]) : Commute (T n) f := f.induction_on' (fun _ _ Tp Tq => Commute.add_right Tp Tq) fun m a => show T n * _ = _ by rw [T, T, ← single_eq_C, single_mul_single, single_mul_single, single_mul_single] simp [add_comm] @[simp] theorem T_mul (n : ℤ) (f : R[T;T⁻¹]) : T n * f = f * T n := (commute_T n f).eq theorem smul_eq_C_mul (r : R) (f : R[T;T⁻¹]) : r • f = C r * f := by induction f using LaurentPolynomial.induction_on' with | add _ _ hp hq => rw [smul_add, mul_add, hp, hq] | C_mul_T n s => rw [← mul_assoc, ← smul_mul_assoc, mul_left_inj_of_invertible, ← map_mul, ← single_eq_C, Finsupp.smul_single', single_eq_C] /-- `trunc : R[T;T⁻¹] →+ R[X]` maps a Laurent polynomial `f` to the polynomial whose terms of nonnegative degree coincide with the ones of `f`. The terms of negative degree of `f` "vanish". `trunc` is a left-inverse to `Polynomial.toLaurent`. -/ def trunc : R[T;T⁻¹] →+ R[X] := (toFinsuppIso R).symm.toAddMonoidHom.comp <| comapDomain.addMonoidHom fun _ _ => Int.ofNat.inj @[simp] theorem trunc_C_mul_T (n : ℤ) (r : R) : trunc (C r * T n) = ite (0 ≤ n) (monomial n.toNat r) 0 := by apply (toFinsuppIso R).injective simp only [← single_eq_C_mul_T, trunc, AddMonoidHom.coe_comp, Function.comp_apply, RingHom.toAddMonoidHom_eq_coe, RingEquiv.toRingHom_eq_coe, Int.ofNat_eq_coe, AddMonoidHom.coe_coe, RingHom.coe_coe, RingEquiv.apply_symm_apply, toFinsuppIso_apply] -- We need `erw` to see through the identification of `Finsupp` with `LaurentSeries`. erw [comapDomain.addMonoidHom_apply Int.ofNat_injective] split_ifs with n0 · rw [toFinsupp_monomial] lift n to ℕ using n0 apply comapDomain_single · rw [toFinsupp_inj] ext a have : a ≠ n := by omega simp only [coeff_ofFinsupp, comapDomain_apply, Int.ofNat_eq_coe, coeff_zero, single_eq_of_ne this] @[simp] theorem leftInverse_trunc_toLaurent : Function.LeftInverse (trunc : R[T;T⁻¹] → R[X]) Polynomial.toLaurent := by refine fun f => f.induction_on' ?_ ?_ · intro f g hf hg simp only [hf, hg, map_add] · intro n r simp only [Polynomial.toLaurent_C_mul_T, trunc_C_mul_T, Int.natCast_nonneg, Int.toNat_natCast, if_true] @[simp] theorem _root_.Polynomial.trunc_toLaurent (f : R[X]) : trunc (toLaurent f) = f := leftInverse_trunc_toLaurent _ theorem _root_.Polynomial.toLaurent_injective : Function.Injective (Polynomial.toLaurent : R[X] → R[T;T⁻¹]) := leftInverse_trunc_toLaurent.injective @[simp] theorem _root_.Polynomial.toLaurent_inj (f g : R[X]) : toLaurent f = toLaurent g ↔ f = g := ⟨fun h => Polynomial.toLaurent_injective h, congr_arg _⟩ theorem _root_.Polynomial.toLaurent_ne_zero {f : R[X]} : toLaurent f ≠ 0 ↔ f ≠ 0 := map_ne_zero_iff _ Polynomial.toLaurent_injective @[simp] theorem _root_.Polynomial.toLaurent_eq_zero {f : R[X]} : toLaurent f = 0 ↔ f = 0 := map_eq_zero_iff _ Polynomial.toLaurent_injective theorem exists_T_pow (f : R[T;T⁻¹]) : ∃ (n : ℕ) (f' : R[X]), toLaurent f' = f * T n := by refine f.induction_on' ?_ fun n a => ?_ <;> clear f · rintro f g ⟨m, fn, hf⟩ ⟨n, gn, hg⟩ refine ⟨m + n, fn * X ^ n + gn * X ^ m, ?_⟩ simp only [hf, hg, add_mul, add_comm (n : ℤ), map_add, map_mul, Polynomial.toLaurent_X_pow, mul_T_assoc, Int.natCast_add] · rcases n with n | n · exact ⟨0, Polynomial.C a * X ^ n, by simp⟩ · refine ⟨n + 1, Polynomial.C a, ?_⟩ simp only [Int.negSucc_eq, Polynomial.toLaurent_C, Int.natCast_succ, mul_T_assoc, neg_add_cancel, T_zero, mul_one] /-- This is a version of `exists_T_pow` stated as an induction principle. -/ @[elab_as_elim] theorem induction_on_mul_T {motive : R[T;T⁻¹] → Prop} (f : R[T;T⁻¹]) (mul_T : ∀ (f : R[X]) (n : ℕ), motive (toLaurent f * T (-n))) : motive f := by rcases f.exists_T_pow with ⟨n, f', hf⟩ rw [← mul_one f, ← T_zero, ← Nat.cast_zero, ← Nat.sub_self n, Nat.cast_sub rfl.le, T_sub, ← mul_assoc, ← hf] exact mul_T .. /-- Suppose that `Q` is a statement about Laurent polynomials such that * `Q` is true on *ordinary* polynomials; * `Q (f * T)` implies `Q f`; it follow that `Q` is true on all Laurent polynomials. -/ theorem reduce_to_polynomial_of_mul_T (f : R[T;T⁻¹]) {Q : R[T;T⁻¹] → Prop} (Qf : ∀ f : R[X], Q (toLaurent f)) (QT : ∀ f, Q (f * T 1) → Q f) : Q f := by induction f using LaurentPolynomial.induction_on_mul_T with | _ f n induction n with | zero => simpa only [Nat.cast_zero, neg_zero, T_zero, mul_one] using Qf _ | succ n hn => convert QT _ _; simpa using hn section Support theorem support_C_mul_T (a : R) (n : ℤ) : Finsupp.support (C a * T n) ⊆ {n} := by rw [← single_eq_C_mul_T] exact support_single_subset theorem support_C_mul_T_of_ne_zero {a : R} (a0 : a ≠ 0) (n : ℤ) : Finsupp.support (C a * T n) = {n} := by rw [← single_eq_C_mul_T] exact support_single_ne_zero _ a0 /-- The support of a polynomial `f` is a finset in `ℕ`. The lemma `toLaurent_support f` shows that the support of `f.toLaurent` is the same finset, but viewed in `ℤ` under the natural inclusion `ℕ ↪ ℤ`. -/ theorem toLaurent_support (f : R[X]) : f.toLaurent.support = f.support.map Nat.castEmbedding := by generalize hd : f.support = s revert f refine Finset.induction_on s ?_ ?_ <;> clear s · intro f hf rw [Finset.map_empty, Finsupp.support_eq_empty, toLaurent_eq_zero] exact Polynomial.support_eq_empty.mp hf · intro a s as hf f fs have : (erase a f).toLaurent.support = s.map Nat.castEmbedding := by refine hf (f.erase a) ?_ simp only [fs, Finset.erase_eq_of_notMem as, Polynomial.support_erase, Finset.erase_insert_eq_erase] rw [← monomial_add_erase f a, Finset.map_insert, ← this, map_add, Polynomial.toLaurent_C_mul_T, support_add_eq, Finset.insert_eq] · congr exact support_C_mul_T_of_ne_zero (Polynomial.mem_support_iff.mp (by simp [fs])) _ · rw [this] exact Disjoint.mono_left (support_C_mul_T _ _) (by simpa) end Support section Degrees /-- The degree of a Laurent polynomial takes values in `WithBot ℤ`. If `f : R[T;T⁻¹]` is a Laurent polynomial, then `f.degree` is the maximum of its support of `f`, or `⊥`, if `f = 0`. -/ def degree (f : R[T;T⁻¹]) : WithBot ℤ := f.support.max @[simp] theorem degree_zero : degree (0 : R[T;T⁻¹]) = ⊥ := rfl @[simp] theorem degree_eq_bot_iff {f : R[T;T⁻¹]} : f.degree = ⊥ ↔ f = 0 := by refine ⟨fun h => ?_, fun h => by rw [h, degree_zero]⟩ ext n simp only [coe_zero, Pi.zero_apply] simp_rw [degree, Finset.max_eq_sup_withBot, Finset.sup_eq_bot_iff, Finsupp.mem_support_iff, Ne, WithBot.coe_ne_bot, imp_false, not_not] at h exact h n section ExactDegrees @[simp] theorem degree_C_mul_T (n : ℤ) (a : R) (a0 : a ≠ 0) : degree (C a * T n) = n := by rw [degree, support_C_mul_T_of_ne_zero a0 n] exact Finset.max_singleton theorem degree_C_mul_T_ite [DecidableEq R] (n : ℤ) (a : R) : degree (C a * T n) = if a = 0 then ⊥ else ↑n := by split_ifs with h <;> simp only [h, map_zero, zero_mul, degree_zero, degree_C_mul_T, Ne, not_false_iff] @[simp] theorem degree_T [Nontrivial R] (n : ℤ) : (T n : R[T;T⁻¹]).degree = n := by rw [← one_mul (T n), ← map_one C] exact degree_C_mul_T n 1 (one_ne_zero : (1 : R) ≠ 0) theorem degree_C {a : R} (a0 : a ≠ 0) : (C a).degree = 0 := by rw [← mul_one (C a), ← T_zero] exact degree_C_mul_T 0 a a0 theorem degree_C_ite [DecidableEq R] (a : R) : (C a).degree = if a = 0 then ⊥ else 0 := by split_ifs with h <;> simp only [h, map_zero, degree_zero, degree_C, Ne, not_false_iff] end ExactDegrees section DegreeBounds theorem degree_C_mul_T_le (n : ℤ) (a : R) : degree (C a * T n) ≤ n := by by_cases a0 : a = 0 · simp only [a0, map_zero, zero_mul, degree_zero, bot_le] · exact (degree_C_mul_T n a a0).le theorem degree_T_le (n : ℤ) : (T n : R[T;T⁻¹]).degree ≤ n := (le_of_eq (by rw [map_one, one_mul])).trans (degree_C_mul_T_le n (1 : R)) theorem degree_C_le (a : R) : (C a).degree ≤ 0 := (le_of_eq (by rw [T_zero, mul_one])).trans (degree_C_mul_T_le 0 a) end DegreeBounds end Degrees instance : Module R[X] R[T;T⁻¹] := Module.compHom _ Polynomial.toLaurent instance (R : Type*) [Semiring R] : IsScalarTower R[X] R[X] R[T;T⁻¹] where smul_assoc x y z := by dsimp; simp_rw [MulAction.mul_smul] end Semiring section CommSemiring variable [CommSemiring R] {S : Type*} [CommSemiring S] (f : R →+* S) (x : Sˣ) instance algebraPolynomial (R : Type*) [CommSemiring R] : Algebra R[X] R[T;T⁻¹] where algebraMap := Polynomial.toLaurent commutes' := fun f l => by simp [mul_comm] smul_def' := fun _ _ => rfl theorem algebraMap_X_pow (n : ℕ) : algebraMap R[X] R[T;T⁻¹] (X ^ n) = T n := Polynomial.toLaurent_X_pow n @[simp] theorem algebraMap_eq_toLaurent (f : R[X]) : algebraMap R[X] R[T;T⁻¹] f = toLaurent f := rfl instance isLocalization : IsLocalization.Away (X : R[X]) R[T;T⁻¹] := { map_units := fun ⟨t, ht⟩ => by obtain ⟨n, rfl⟩ := ht rw [algebraMap_eq_toLaurent, toLaurent_X_pow] exact isUnit_T ↑n surj f := by induction f using LaurentPolynomial.induction_on_mul_T with | _ f n have : X ^ n ∈ Submonoid.powers (X : R[X]) := ⟨n, rfl⟩ refine ⟨(f, ⟨_, this⟩), ?_⟩ simp only [algebraMap_eq_toLaurent, toLaurent_X_pow, mul_T_assoc, neg_add_cancel, T_zero, mul_one] exists_of_eq := fun {f g} => by rw [algebraMap_eq_toLaurent, algebraMap_eq_toLaurent, Polynomial.toLaurent_inj] rintro rfl exact ⟨1, rfl⟩ } theorem mk'_mul_T (p : R[X]) (n : ℕ) : IsLocalization.mk' R[T;T⁻¹] p (⟨X^n, n, rfl⟩ : Submonoid.powers (X : R[X])) * T n = toLaurent p := by rw [← toLaurent_X_pow, ← algebraMap_eq_toLaurent, IsLocalization.mk'_spec, algebraMap_eq_toLaurent] @[simp] theorem mk'_eq (p : R[X]) (n : ℕ) : IsLocalization.mk' R[T;T⁻¹] p (⟨X^n, n, rfl⟩ : Submonoid.powers (X : R[X])) = toLaurent p * T (-n) := by rw [← IsUnit.mul_left_inj (isUnit_T n), mul_T_assoc, neg_add_cancel, T_zero, mul_one] exact mk'_mul_T p n theorem mk'_one_X_pow (n : ℕ) : IsLocalization.mk' R[T;T⁻¹] 1 (⟨X^n, n, rfl⟩ : Submonoid.powers (X : R[X])) = T (-n) := by rw [mk'_eq 1 n, toLaurent_one, one_mul] @[simp] theorem mk'_one_X : IsLocalization.mk' R[T;T⁻¹] 1 (⟨X, 1, pow_one X⟩ : Submonoid.powers (X : R[X])) = T (-1) := by convert mk'_one_X_pow 1 exact (pow_one X).symm /-- Given a ring homomorphism `f : R →+* S` and a unit `x` in `S`, the induced homomorphism `R[T;T⁻¹] →+* S` sending `T` to `x` and `T⁻¹` to `x⁻¹`. -/ def eval₂ : R[T;T⁻¹] →+* S := IsLocalization.lift (M := Submonoid.powers (X : R[X])) (g := Polynomial.eval₂RingHom f x) <| by rintro ⟨y, n, rfl⟩ simpa only [coe_eval₂RingHom, eval₂_X_pow] using x.isUnit.pow n @[simp] theorem eval₂_toLaurent (p : R[X]) : eval₂ f x (toLaurent p) = Polynomial.eval₂ f x p := by unfold eval₂ rw [← algebraMap_eq_toLaurent, IsLocalization.lift_eq, coe_eval₂RingHom] theorem eval₂_T_n (n : ℕ) : eval₂ f x (T n) = x ^ n := by rw [← Polynomial.toLaurent_X_pow, eval₂_toLaurent, eval₂_X_pow] theorem eval₂_T_neg_n (n : ℕ) : eval₂ f x (T (-n)) = x⁻¹ ^ n := by rw [← mk'_one_X_pow] unfold eval₂ rw [IsLocalization.lift_mk'_spec, map_one, coe_eval₂RingHom, eval₂_X_pow, ← mul_pow, Units.mul_inv, one_pow] @[simp] theorem eval₂_T (n : ℤ) : eval₂ f x (T n) = (x ^ n).val := by by_cases! hn : 0 ≤ n · lift n to ℕ using hn apply eval₂_T_n · obtain ⟨m, rfl⟩ := Int.exists_eq_neg_ofNat hn.le rw [eval₂_T_neg_n, zpow_neg, zpow_natCast, ← inv_pow, Units.val_pow_eq_pow_val] @[simp] theorem eval₂_C (r : R) : eval₂ f x (C r) = f r := by rw [← toLaurent_C, eval₂_toLaurent, Polynomial.eval₂_C] theorem eval₂_C_mul_T_n (r : R) (n : ℕ) : eval₂ f x (C r * T n) = f r * x ^ n := by rw [← Polynomial.toLaurent_C_mul_T, eval₂_toLaurent, eval₂_monomial] theorem eval₂_C_mul_T_neg_n (r : R) (n : ℕ) : eval₂ f x (C r * T (-n)) = f r * x⁻¹ ^ n := by rw [map_mul, eval₂_T_neg_n, eval₂_C] @[simp] theorem eval₂_C_mul_T (r : R) (n : ℤ) : eval₂ f x (C r * T n) = f r * (x ^ n).val := by simp end CommSemiring section Inversion variable {R : Type*} [CommSemiring R] /-- The map which substitutes `T ↦ T⁻¹` into a Laurent polynomial. -/ def invert : R[T;T⁻¹] ≃ₐ[R] R[T;T⁻¹] := AddMonoidAlgebra.domCongr R R <| AddEquiv.neg _ @[simp] lemma invert_T (n : ℤ) : invert (T n : R[T;T⁻¹]) = T (-n) := AddMonoidAlgebra.domCongr_single _ _ _ _ _ @[simp] lemma invert_apply (f : R[T;T⁻¹]) (n : ℤ) : invert f n = f (-n) := rfl @[simp] lemma invert_comp_C : invert ∘ (@C R _) = C := by ext; simp @[simp] lemma invert_C (t : R) : invert (C t) = C t := by ext; simp lemma involutive_invert : Involutive (invert (R := R)) := fun _ ↦ by ext; simp @[simp] lemma invert_symm : (invert (R := R)).symm = invert := rfl lemma toLaurent_reverse (p : R[X]) : toLaurent p.reverse = invert (toLaurent p) * (T p.natDegree) := by nontriviality R induction p using Polynomial.recOnHorner with | M0 => simp | MC _ _ _ _ ih => simp [add_mul, ← ih] | MX _ hp => simpa [natDegree_mul_X hp] end Inversion section Smeval section SMulWithZero variable [Semiring R] [AddCommMonoid S] [SMulWithZero R S] [Monoid S] (f g : R[T;T⁻¹]) (x y : Sˣ) /-- Evaluate a Laurent polynomial at a unit, using scalar multiplication. -/ def smeval : S := Finsupp.sum f fun n r => r • (x ^ n).val theorem smeval_eq_sum : f.smeval x = Finsupp.sum f fun n r => r • (x ^ n).val := rfl theorem smeval_congr : f = g → x = y → f.smeval x = g.smeval y := by rintro rfl rfl; rfl @[simp] theorem smeval_zero : (0 : R[T;T⁻¹]).smeval x = (0 : S) := by simp only [smeval_eq_sum, Finsupp.sum_zero_index] theorem smeval_single (n : ℤ) (r : R) : smeval (Finsupp.single n r) x = r • (x ^ n).val := by simp only [smeval_eq_sum] rw [Finsupp.sum_single_index (zero_smul R (x ^ n).val)] @[simp] theorem smeval_C_mul_T_n (n : ℤ) (r : R) : (C r * T n).smeval x = r • (x ^ n).val := by rw [← single_eq_C_mul_T, smeval_single] @[simp] theorem smeval_C (r : R) : (C r).smeval x = r • 1 := by rw [← single_eq_C, smeval_single x (0 : ℤ) r, zpow_zero, Units.val_one] end SMulWithZero section MulActionWithZero variable [Semiring R] [AddCommMonoid S] [MulActionWithZero R S] [Monoid S] (f g : R[T;T⁻¹]) (x y : Sˣ) @[simp] theorem smeval_T_pow (n : ℤ) (x : Sˣ) : (T n : R[T;T⁻¹]).smeval x = (x ^ n).val := by rw [T, smeval_single, one_smul] @[simp] theorem smeval_one : (1 : R[T;T⁻¹]).smeval x = 1 := by rw [← T_zero, smeval_T_pow 0 x, zpow_zero, Units.val_eq_one] end MulActionWithZero section Module variable [Semiring R] [AddCommMonoid S] [Module R S] [Monoid S] (f g : R[T;T⁻¹]) (x y : Sˣ) @[simp] theorem smeval_add : (f + g).smeval x = f.smeval x + g.smeval x := by simp only [smeval_eq_sum] rw [Finsupp.sum_add_index (fun n _ => zero_smul R (x ^ n).val) (fun n _ r r' => add_smul r r' _)] @[simp] theorem smeval_C_mul (r : R) : (C r * f).smeval x = r • (f.smeval x) := by induction f using LaurentPolynomial.induction_on' with | add p q hp hq=> rw [mul_add, smeval_add, smeval_add, smul_add, hp, hq] | C_mul_T n s => rw [← mul_assoc, ← map_mul, smeval_C_mul_T_n, smeval_C_mul_T_n, mul_smul] variable (R) in /-- Evaluation as an `R`-linear map. -/ @[simps] def leval : R[T;T⁻¹] →ₗ[R] S where toFun f := f.smeval x map_add' f g := smeval_add f g x map_smul' r f := by simp [smul_eq_C_mul] end Module end Smeval end LaurentPolynomial
.lake/packages/mathlib/Mathlib/Algebra/Polynomial/Bivariate.lean
import Mathlib.RingTheory.AdjoinRoot /-! # Bivariate polynomials This file introduces the notation `R[X][Y]` for the polynomial ring `R[X][X]` in two variables, and the notation `Y` for the second variable, in the `Polynomial.Bivariate` scope. It also defines `Polynomial.evalEval` for the evaluation of a bivariate polynomial at a point on the affine plane, which is a ring homomorphism (`Polynomial.evalEvalRingHom`), as well as the abbreviation `CC` to view a constant in the base ring `R` as a bivariate polynomial. -/ /-- The notation `Y` for `X` in the `Polynomial` scope. -/ scoped[Polynomial.Bivariate] notation3:max "Y" => Polynomial.X (R := Polynomial _) /-- The notation `R[X][Y]` for `R[X][X]` in the `Polynomial` scope. -/ scoped[Polynomial.Bivariate] notation3:max R "[X][Y]" => Polynomial (Polynomial R) open scoped Polynomial.Bivariate namespace Polynomial noncomputable section variable {R S : Type*} section Semiring variable [Semiring R] /-- `evalEval x y p` is the evaluation `p(x,y)` of a two-variable polynomial `p : R[X][Y]`. -/ abbrev evalEval (x y : R) (p : R[X][Y]) : R := eval x (eval (C y) p) /-- A constant viewed as a polynomial in two variables. -/ abbrev CC (r : R) : R[X][Y] := C (C r) lemma evalEval_C (x y : R) (p : R[X]) : (C p).evalEval x y = p.eval x := by rw [evalEval, eval_C] @[simp] lemma evalEval_CC (x y : R) (p : R) : (CC p).evalEval x y = p := by rw [evalEval_C, eval_C] @[simp] lemma evalEval_zero (x y : R) : (0 : R[X][Y]).evalEval x y = 0 := by simp only [evalEval, eval_zero] @[simp] lemma evalEval_one (x y : R) : (1 : R[X][Y]).evalEval x y = 1 := by simp only [evalEval, eval_one] @[simp] lemma evalEval_natCast (x y : R) (n : ℕ) : (n : R[X][Y]).evalEval x y = n := by simp only [evalEval, eval_natCast] @[simp] lemma evalEval_X (x y : R) : X.evalEval x y = y := by rw [evalEval, eval_X, eval_C] @[simp] lemma evalEval_add (x y : R) (p q : R[X][Y]) : (p + q).evalEval x y = p.evalEval x y + q.evalEval x y := by simp only [evalEval, eval_add] lemma evalEval_sum (x y : R) (p : R[X]) (f : ℕ → R → R[X][Y]) : (p.sum f).evalEval x y = p.sum fun n a => (f n a).evalEval x y := by simp only [evalEval, eval, eval₂_sum] lemma evalEval_finset_sum {ι : Type*} (s : Finset ι) (x y : R) (f : ι → R[X][Y]) : (∑ i ∈ s, f i).evalEval x y = ∑ i ∈ s, (f i).evalEval x y := by simp only [evalEval, eval_finset_sum] @[simp] lemma evalEval_smul [DistribSMul S R] [IsScalarTower S R R] (x y : R) (s : S) (p : R[X][Y]) : (s • p).evalEval x y = s • p.evalEval x y := by simp only [evalEval, eval_smul] lemma evalEval_surjective (x y : R) : Function.Surjective <| evalEval x y := fun y => ⟨CC y, evalEval_CC ..⟩ end Semiring section Ring variable [Ring R] @[simp] lemma evalEval_neg (x y : R) (p : R[X][Y]) : (-p).evalEval x y = -p.evalEval x y := by simp only [evalEval, eval_neg] @[simp] lemma evalEval_sub (x y : R) (p q : R[X][Y]) : (p - q).evalEval x y = p.evalEval x y - q.evalEval x y := by simp only [evalEval, eval_sub] @[simp] lemma evalEval_intCast (x y : R) (n : ℤ) : (n : R[X][Y]).evalEval x y = n := by simp only [evalEval, eval_intCast] end Ring section CommSemiring variable [CommSemiring R] @[simp] lemma evalEval_mul (x y : R) (p q : R[X][Y]) : (p * q).evalEval x y = p.evalEval x y * q.evalEval x y := by simp only [evalEval, eval_mul] lemma evalEval_prod {ι : Type*} (s : Finset ι) (x y : R) (p : ι → R[X][Y]) : (∏ j ∈ s, p j).evalEval x y = ∏ j ∈ s, (p j).evalEval x y := by simp only [evalEval, eval_prod] lemma evalEval_list_prod (x y : R) (l : List R[X][Y]) : l.prod.evalEval x y = (l.map <| evalEval x y).prod := by simpa only [evalEval, eval_list_prod, List.map_map] using by rfl lemma evalEval_multiset_prod (x y : R) (l : Multiset R[X][Y]) : l.prod.evalEval x y = (l.map <| evalEval x y).prod := by simpa only [evalEval, eval_multiset_prod, Multiset.map_map] using by rfl @[simp] lemma evalEval_pow (x y : R) (p : R[X][Y]) (n : ℕ) : (p ^ n).evalEval x y = p.evalEval x y ^ n := by simp only [evalEval, eval_pow] lemma evalEval_dvd (x y : R) {p q : R[X][Y]} : p ∣ q → p.evalEval x y ∣ q.evalEval x y := eval_dvd ∘ eval_dvd lemma coe_algebraMap_eq_CC : algebraMap R R[X][Y] = CC (R := R) := rfl /-- `evalEval x y` as a ring homomorphism. -/ @[simps!] abbrev evalEvalRingHom (x y : R) : R[X][Y] →+* R := (evalRingHom x).comp (evalRingHom <| C y) lemma coe_evalEvalRingHom (x y : R) : evalEvalRingHom x y = evalEval x y := rfl lemma evalEvalRingHom_eq (x : R) : evalEvalRingHom x = eval₂RingHom (evalRingHom x) := by ext <;> simp lemma eval₂_evalRingHom (x : R) : eval₂ (evalRingHom x) = evalEval x := by ext1; rw [← coe_evalEvalRingHom, evalEvalRingHom_eq, coe_eval₂RingHom] lemma map_evalRingHom_eval (x y : R) (p : R[X][Y]) : (p.map <| evalRingHom x).eval y = p.evalEval x y := by rw [eval_map, eval₂_evalRingHom] end CommSemiring section variable [Semiring R] [Semiring S] (f : R →+* S) (p : R[X][Y]) (q : R[X]) lemma map_mapRingHom_eval_map : (p.map <| mapRingHom f).eval (q.map f) = (p.eval q).map f := by rw [eval_map, ← coe_mapRingHom, eval₂_hom] lemma map_mapRingHom_eval_map_eval (r : R) : ((p.map <| mapRingHom f).eval <| q.map f).eval (f r) = f ((p.eval q).eval r) := by rw [map_mapRingHom_eval_map, eval_map, eval₂_hom] lemma map_mapRingHom_evalEval (x y : R) : (p.map <| mapRingHom f).evalEval (f x) (f y) = f (p.evalEval x y) := by rw [evalEval, ← map_mapRingHom_eval_map_eval, map_C] end variable [CommSemiring R] [CommSemiring S] /-- Two equivalent ways to express the evaluation of a bivariate polynomial over `R` at a point in the affine plane over an `R`-algebra `S`. -/ lemma eval₂RingHom_eval₂RingHom (f : R →+* S) (x y : S) : eval₂RingHom (eval₂RingHom f x) y = (evalEvalRingHom x y).comp (mapRingHom <| mapRingHom f) := by ext <;> simp lemma eval₂_eval₂RingHom_apply (f : R →+* S) (x y : S) (p : R[X][Y]) : eval₂ (eval₂RingHom f x) y p = (p.map <| mapRingHom f).evalEval x y := congr($(eval₂RingHom_eval₂RingHom f x y) p) lemma eval_C_X_comp_eval₂_map_C_X : (evalRingHom (C X : R[X][Y])).comp (eval₂RingHom (mapRingHom <| algebraMap R R[X][Y]) (C Y)) = .id _ := by ext <;> simp /-- Viewing `R[X,Y,X']` as an `R[X']`-algebra, a polynomial `p : R[X',Y']` can be evaluated at `Y : R[X,Y,X']` (substitution of `Y'` by `Y`), obtaining another polynomial in `R[X,Y,X']`. When this polynomial is then evaluated at `X' = X`, the original polynomial `p` is recovered. -/ lemma eval_C_X_eval₂_map_C_X {p : R[X][Y]} : eval (C X) (eval₂ (mapRingHom <| algebraMap R R[X][Y]) (C Y) p) = p := congr($eval_C_X_comp_eval₂_map_C_X p) end section aevalAeval noncomputable section variable {R A : Type*} [CommRing R] [CommRing A] [Algebra R A] /-- `aevalAeval x y` is the `R`-algebra evaluation morphism sending a two-variable polynomial `p : R[X][Y]` to `p(x,y)`. -/ abbrev aevalAeval (x y : A) : R[X][Y] →ₐ[R] A := ((aeval x).restrictScalars R).comp (letI := Polynomial.algebra; (aeval (R := R[X]) (C y)).restrictScalars R) theorem coe_aevalAeval_eq_evalEval (x y : A) : ⇑(aevalAeval x y) = evalEval x y := by ext p; simp [aevalAeval, evalEval, aeval, Algebra.ofId] /-- The R-algebra automorphism given by `X ↦ Y` and `Y ↦ X`. -/ def Bivariate.swap : R[X][Y] ≃ₐ[R] R[X][Y] := by apply AlgEquiv.ofAlgHom (aevalAeval (Y : R[X][Y]) (C X)) (aevalAeval (Y : R[X][Y]) (C X)) <;> (ext n m <;> simp) @[simp] theorem Bivariate.swap_apply (p : R[X][Y]) : swap p = p.aevalAeval (A := R[X][Y]) Y (C X) := rfl theorem Bivariate.swap_X : swap (R := R) (C X) = Y := by simp theorem Bivariate.swap_Y : swap (R := R) Y = (C X) := by simp theorem Bivariate.swap_monomial_monomial (n m : ℕ) (r : R) : swap (monomial n (monomial m r)) = (monomial m (monomial n r)) := by simp [← C_mul_X_pow_eq_monomial]; ac_rfl /-- Evaluating `swap p` at `x`, `y` is the same as evaluating `p` at `y` `x`. -/ theorem Bivariate.aevalAeval_swap (x y : A) (p : R[X][Y]) : aevalAeval x y (swap p) = aevalAeval y x p := by induction p using Polynomial.induction_on' with | add => aesop | monomial n a => simp induction a using Polynomial.induction_on' <;> aesop (add norm add_mul) attribute [local instance] Polynomial.algebra in theorem Bivariate.aveal_eq_map_swap (x : A) (p : R[X][Y]) : aeval (C x) p = mapAlgHom (aeval x) (swap p) := by induction p using Polynomial.induction_on' with | add => aesop | monomial n a => simp induction a using Polynomial.induction_on' <;> aesop (add norm [add_mul, C_mul_X_pow_eq_monomial]) end end aevalAeval end Polynomial open Polynomial namespace AdjoinRoot variable {R : Type*} [CommRing R] {x y : R} {p : R[X][Y]} (h : p.evalEval x y = 0) /-- If the evaluation (`evalEval`) of a bivariate polynomial `p : R[X][Y]` at a point (x,y) is zero, then `Polynomial.evalEval x y` factors through `AdjoinRoot.evalEval`, a ring homomorphism from `AdjoinRoot p` to `R`. -/ @[simps!] noncomputable def evalEval : AdjoinRoot p →+* R := lift (evalRingHom x) y <| eval₂_evalRingHom x ▸ h lemma evalEval_mk (g : R[X][Y]) : evalEval h (mk p g) = g.evalEval x y := by rw [evalEval, lift_mk, eval₂_evalRingHom] end AdjoinRoot
.lake/packages/mathlib/Mathlib/Algebra/Polynomial/Factors.lean
import Mathlib.Algebra.Polynomial.FieldDivision /-! # Split polynomials A polynomial `f : R[X]` factors if it is a product of constant and monic linear polynomials. ## Main definitions * `Polynomial.Factors f`: A predicate on a polynomial `f` saying that `f` is a product of constant and monic linear polynomials. ## TODO - Redefine `Splits` in terms of `Factors` and then deprecate `Splits`. -/ variable {R : Type*} namespace Polynomial section Semiring variable [Semiring R] /-- A polynomial `Factors` if it is a product of constant and monic linear polynomials. This will eventually replace `Polynomial.Splits`. -/ def Factors (f : R[X]) : Prop := f ∈ Submonoid.closure ({C a | a : R} ∪ {X + C a | a : R}) @[simp, aesop safe apply] protected theorem Factors.C (a : R) : Factors (C a) := Submonoid.mem_closure_of_mem (Set.mem_union_left _ ⟨a, rfl⟩) @[simp, aesop safe apply] protected theorem Factors.zero : Factors (0 : R[X]) := by simpa using Factors.C (0 : R) @[simp, aesop safe apply] protected theorem Factors.one : Factors (1 : R[X]) := Factors.C (1 : R) @[simp, aesop safe apply] theorem Factors.X_add_C (a : R) : Factors (X + C a) := Submonoid.mem_closure_of_mem (Set.mem_union_right _ ⟨a, rfl⟩) @[simp, aesop safe apply] protected theorem Factors.X : Factors (X : R[X]) := by simpa using Factors.X_add_C (0 : R) @[simp, aesop safe apply] protected theorem Factors.mul {f g : R[X]} (hf : Factors f) (hg : Factors g) : Factors (f * g) := mul_mem hf hg protected theorem Factors.C_mul {f : R[X]} (hf : Factors f) (a : R) : Factors (C a * f) := (Factors.C a).mul hf @[simp, aesop safe apply] theorem Factors.listProd {l : List R[X]} (h : ∀ f ∈ l, Factors f) : Factors l.prod := list_prod_mem h @[simp, aesop safe apply] protected theorem Factors.pow {f : R[X]} (hf : Factors f) (n : ℕ) : Factors (f ^ n) := pow_mem hf n theorem Factors.X_pow (n : ℕ) : Factors (X ^ n : R[X]) := Factors.X.pow n theorem Factors.C_mul_X_pow (a : R) (n : ℕ) : Factors (C a * X ^ n) := (Factors.X_pow n).C_mul a @[simp, aesop safe apply] theorem Factors.monomial (n : ℕ) (a : R) : Factors (monomial n a) := by simp [← C_mul_X_pow_eq_monomial] protected theorem Factors.map {f : R[X]} (hf : Factors f) {S : Type*} [Semiring S] (i : R →+* S) : Factors (map i f) := by induction hf using Submonoid.closure_induction <;> aesop theorem factors_of_natDegree_eq_zero {f : R[X]} (hf : natDegree f = 0) : Factors f := (natDegree_eq_zero.mp hf).choose_spec ▸ by aesop theorem factors_of_degree_le_zero {f : R[X]} (hf : degree f ≤ 0) : Factors f := factors_of_natDegree_eq_zero (natDegree_eq_zero_iff_degree_le_zero.mpr hf) end Semiring section CommSemiring variable [CommSemiring R] @[simp, aesop safe apply] theorem Factors.multisetProd {m : Multiset R[X]} (hm : ∀ f ∈ m, Factors f) : Factors m.prod := multiset_prod_mem _ hm @[simp, aesop safe apply] protected theorem Factors.prod {ι : Type*} {f : ι → R[X]} {s : Finset ι} (h : ∀ i ∈ s, Factors (f i)) : Factors (∏ i ∈ s, f i) := prod_mem h /-- See `factors_iff_exists_multiset` for the version with subtraction. -/ theorem factors_iff_exists_multiset' {f : R[X]} : Factors f ↔ ∃ m : Multiset R, f = C f.leadingCoeff * (m.map (X + C ·)).prod := by refine ⟨fun hf ↦ ?_, ?_⟩ · let S : Submonoid R[X] := MonoidHom.mrange C have hS : S = {C a | a : R} := MonoidHom.coe_mrange C rw [Factors, Submonoid.closure_union, ← hS, Submonoid.closure_eq, Submonoid.mem_sup] at hf obtain ⟨-, ⟨a, rfl⟩, g, hg, rfl⟩ := hf obtain ⟨mg, hmg, rfl⟩ := Submonoid.exists_multiset_of_mem_closure hg choose! j hj using hmg have hmg : mg = (mg.map j).map (X + C ·) := by simp [Multiset.map_congr rfl hj] rw [hmg, leadingCoeff_mul_monic, leadingCoeff_C] · use mg.map j · rw [hmg] apply monic_multiset_prod_of_monic simp [monic_X_add_C] · rintro ⟨m, hm⟩ exact hm ▸ (Factors.C _).mul (.multisetProd (by simp [Factors.X_add_C])) theorem Factors.natDegree_le_one_of_irreducible {f : R[X]} (hf : Factors f) (h : Irreducible f) : natDegree f ≤ 1 := by nontriviality R obtain ⟨m, hm⟩ := factors_iff_exists_multiset'.mp hf rcases m.empty_or_exists_mem with rfl | ⟨a, ha⟩ · rw [hm] simp · obtain ⟨m, rfl⟩ := Multiset.exists_cons_of_mem ha rw [Multiset.map_cons, Multiset.prod_cons] at hm rw [hm] at h simp only [irreducible_mul_iff, IsUnit.mul_iff, not_isUnit_X_add_C, false_and, and_false, or_false, false_or, ← Multiset.prod_toList, List.prod_isUnit_iff] at h have : m = 0 := by simpa [not_isUnit_X_add_C, ← Multiset.eq_zero_iff_forall_notMem] using h.1.2 grw [hm, this, natDegree_mul_le] simp end CommSemiring section Ring variable [Ring R] @[simp, aesop safe apply] theorem Factors.X_sub_C (a : R) : Factors (X - C a) := by simpa using Factors.X_add_C (-a) @[aesop safe apply] protected theorem Factors.neg {f : R[X]} (hf : Factors f) : Factors (-f) := by rw [← neg_one_mul, ← C_1, ← C_neg] exact hf.C_mul (-1) @[simp] theorem factors_neg_iff {f : R[X]} : Factors (-f) ↔ Factors f := ⟨fun hf ↦ neg_neg f ▸ hf.neg, .neg⟩ end Ring section CommRing variable [CommRing R] {f g : R[X]} theorem factors_iff_exists_multiset : Factors f ↔ ∃ m : Multiset R, f = C f.leadingCoeff * (m.map (X - C ·)).prod := by refine factors_iff_exists_multiset'.trans ⟨?_, ?_⟩ <;> rintro ⟨m, hm⟩ <;> exact ⟨m.map (- ·), by simpa⟩ theorem Factors.exists_eval_eq_zero (hf : Factors f) (hf0 : degree f ≠ 0) : ∃ a, eval a f = 0 := by obtain ⟨m, hm⟩ := factors_iff_exists_multiset.mp hf by_cases hf₀ : f.leadingCoeff = 0 · simp [leadingCoeff_eq_zero.mp hf₀] obtain rfl | ⟨a, ha⟩ := m.empty_or_exists_mem · rw [hm, Multiset.map_zero, Multiset.prod_zero, mul_one, degree_C hf₀] at hf0 contradiction obtain ⟨m, rfl⟩ := Multiset.exists_cons_of_mem ha exact ⟨a, hm ▸ by simp⟩ variable [IsDomain R] theorem Factors.eq_prod_roots (hf : Factors f) : f = C f.leadingCoeff * (f.roots.map (X - C ·)).prod := by by_cases hf0 : f.leadingCoeff = 0 · simp [leadingCoeff_eq_zero.mp hf0] · obtain ⟨m, hm⟩ := factors_iff_exists_multiset.mp hf suffices hf : f.roots = m by rwa [hf] rw [hm, roots_C_mul _ hf0, roots_multiset_prod_X_sub_C] theorem Factors.natDegree_eq_card_roots (hf : Factors f) : f.natDegree = f.roots.card := by by_cases hf0 : f.leadingCoeff = 0 · simp [leadingCoeff_eq_zero.mp hf0] · conv_lhs => rw [hf.eq_prod_roots, natDegree_C_mul hf0, natDegree_multiset_prod_X_sub_C_eq_card] theorem Factors.roots_ne_zero (hf : Factors f) (hf0 : natDegree f ≠ 0) : f.roots ≠ 0 := by obtain ⟨a, ha⟩ := hf.exists_eval_eq_zero (degree_ne_of_natDegree_ne hf0) exact mt (· ▸ (mem_roots (by aesop)).mpr ha) (Multiset.notMem_zero a) theorem factors_X_sub_C_mul_iff {a : R} : Factors ((X - C a) * f) ↔ Factors f := by refine ⟨fun hf ↦ ?_, ((Factors.X_sub_C _).mul ·)⟩ by_cases hf₀ : f = 0 · aesop have := hf.eq_prod_roots rw [leadingCoeff_mul, leadingCoeff_X_sub_C, one_mul, roots_mul (mul_ne_zero (X_sub_C_ne_zero _) hf₀), roots_X_sub_C, Multiset.singleton_add, Multiset.map_cons, Multiset.prod_cons, mul_left_comm] at this rw [mul_left_cancel₀ (X_sub_C_ne_zero _) this] aesop theorem factors_mul_iff (hf₀ : f ≠ 0) (hg₀ : g ≠ 0) : Factors (f * g) ↔ Factors f ∧ Factors g := by refine ⟨fun h ↦ ?_, and_imp.mpr .mul⟩ generalize hp : f * g = p at * generalize hn : p.natDegree = n induction n generalizing p f g with | zero => rw [← hp, natDegree_mul hf₀ hg₀, Nat.add_eq_zero] at hn exact ⟨factors_of_natDegree_eq_zero hn.1, factors_of_natDegree_eq_zero hn.2⟩ | succ n ih => obtain ⟨a, ha⟩ := Factors.exists_eval_eq_zero h (degree_ne_of_natDegree_ne <| hn ▸ by aesop) have := dvd_iff_isRoot.mpr ha rw [← hp, (prime_X_sub_C a).dvd_mul] at this wlog hf : X - C a ∣ f with hf2 · exact .symm <| hf2 n ih hg₀ hf₀ p ((mul_comm g f).trans hp) h hn a ha this.symm <| this.resolve_left hf obtain ⟨f, rfl⟩ := hf rw [mul_assoc] at hp; subst hp rw [natDegree_mul (by aesop) (by aesop), natDegree_X_sub_C, add_comm, Nat.succ_inj] at hn have := ih (by aesop) hg₀ (f * g) rfl (factors_X_sub_C_mul_iff.mp h) hn aesop theorem Factors.of_dvd (hg : Factors g) (hg₀ : g ≠ 0) (hfg : f ∣ g) : Factors f := by obtain ⟨g, rfl⟩ := hfg exact ((factors_mul_iff (by aesop) (by aesop)).mp hg).1 -- Todo: Remove or fix name once `Splits` is gone. theorem Factors.splits (hf : Factors f) : f = 0 ∨ ∀ {g : R[X]}, Irreducible g → g ∣ f → degree g ≤ 1 := or_iff_not_imp_left.mpr fun hf0 _ hg hgf ↦ degree_le_of_natDegree_le <| (hf.of_dvd hf0 hgf).natDegree_le_one_of_irreducible hg end CommRing section DivisionSemiring variable [DivisionSemiring R] theorem Factors.of_natDegree_le_one {f : R[X]} (hf : natDegree f ≤ 1) : Factors f := by obtain ⟨a, b, rfl⟩ := exists_eq_X_add_C_of_natDegree_le_one hf by_cases ha : a = 0 · aesop · rw [← mul_inv_cancel_left₀ ha b, C_mul, ← mul_add] exact (X_add_C (a⁻¹ * b)).C_mul a theorem Factors.of_natDegree_eq_one {f : R[X]} (hf : natDegree f = 1) : Factors f := of_natDegree_le_one hf.le theorem Factors.of_degree_le_one {f : R[X]} (hf : degree f ≤ 1) : Factors f := of_natDegree_le_one (natDegree_le_of_degree_le hf) theorem Factors.of_degree_eq_one {f : R[X]} (hf : degree f = 1) : Factors f := of_degree_le_one hf.le end DivisionSemiring section Field variable [Field R] open UniqueFactorizationMonoid in -- Todo: Remove or fix name once `Splits` is gone. theorem factors_iff_splits {f : R[X]} : Factors f ↔ f = 0 ∨ ∀ {g : R[X]}, Irreducible g → g ∣ f → degree g = 1 := by refine ⟨fun hf ↦ hf.splits.imp_right (forall₃_imp fun g hg hgf ↦ (le_antisymm · (Nat.WithBot.one_le_iff_zero_lt.mpr hg.degree_pos))), ?_⟩ rintro (rfl | hf) · aesop classical by_cases hf0 : f = 0 · simp [hf0] obtain ⟨u, hu⟩ := factors_prod hf0 rw [← hu] refine (Factors.multisetProd fun g hg ↦ ?_).mul (factors_of_natDegree_eq_zero (natDegree_eq_zero_of_isUnit u.isUnit)) exact Factors.of_degree_eq_one (hf (irreducible_of_factor g hg) (dvd_of_mem_factors hg)) end Field end Polynomial
.lake/packages/mathlib/Mathlib/Algebra/Polynomial/Monomial.lean
import Mathlib.Algebra.Group.Nat.Hom import Mathlib.Algebra.Polynomial.Basic /-! # Univariate monomials -/ noncomputable section namespace Polynomial universe u variable {R : Type u} {a b : R} {m n : ℕ} variable [Semiring R] {p q r : R[X]} theorem monomial_one_eq_iff [Nontrivial R] {i j : ℕ} : (monomial i 1 : R[X]) = monomial j 1 ↔ i = j := by simp_rw [← ofFinsupp_single, ofFinsupp.injEq] exact AddMonoidAlgebra.of_injective.eq_iff instance infinite [Nontrivial R] : Infinite R[X] := Infinite.of_injective (fun i => monomial i 1) fun m n h => by simpa [monomial_one_eq_iff] using h theorem card_support_le_one_iff_monomial {f : R[X]} : Finset.card f.support ≤ 1 ↔ ∃ n a, f = monomial n a := by constructor · intro H rw [Finset.card_le_one_iff_subset_singleton] at H rcases H with ⟨n, hn⟩ refine ⟨n, f.coeff n, ?_⟩ ext i by_cases hi : i = n · simp [hi] · have : f.coeff i = 0 := by rw [← notMem_support_iff] exact fun hi' => hi (Finset.mem_singleton.1 (hn hi')) simp [this, Ne.symm hi, coeff_monomial] · rintro ⟨n, a, rfl⟩ rw [← Finset.card_singleton n] apply Finset.card_le_card exact support_monomial' _ _ theorem ringHom_ext {S} [Semiring S] {f g : R[X] →+* S} (h₁ : ∀ a, f (C a) = g (C a)) (h₂ : f X = g X) : f = g := by set f' := f.comp (toFinsuppIso R).symm.toRingHom with hf' set g' := g.comp (toFinsuppIso R).symm.toRingHom with hg' have A : f' = g' := by ext · simp [f', g', h₁, RingEquiv.toRingHom_eq_coe] simpa using h₂ have B : f = f'.comp (toFinsuppIso R) := by rw [hf', RingHom.comp_assoc] ext x simp only [RingEquiv.toRingHom_eq_coe, RingEquiv.symm_apply_apply, Function.comp_apply, RingHom.coe_comp, RingEquiv.coe_toRingHom] have C' : g = g'.comp (toFinsuppIso R) := by rw [hg', RingHom.comp_assoc] ext x simp only [RingEquiv.toRingHom_eq_coe, RingEquiv.symm_apply_apply, Function.comp_apply, RingHom.coe_comp, RingEquiv.coe_toRingHom] rw [B, C', A] @[ext high] theorem ringHom_ext' {S} [Semiring S] {f g : R[X] →+* S} (h₁ : f.comp C = g.comp C) (h₂ : f X = g X) : f = g := ringHom_ext (RingHom.congr_fun h₁) h₂ end Polynomial
.lake/packages/mathlib/Mathlib/Algebra/Polynomial/AlgebraMap.lean
import Mathlib.Algebra.Algebra.Pi import Mathlib.Algebra.Algebra.Prod import Mathlib.Algebra.Algebra.Subalgebra.Lattice import Mathlib.Algebra.Algebra.Tower import Mathlib.Algebra.MonoidAlgebra.Basic import Mathlib.Algebra.Polynomial.Eval.Algebra import Mathlib.Algebra.Polynomial.Eval.Degree import Mathlib.Algebra.Polynomial.Monomial /-! # Theory of univariate polynomials We show that `A[X]` is an R-algebra when `A` is an R-algebra. We promote `eval₂` to an algebra hom in `aeval`. -/ assert_not_exists Ideal noncomputable section open Finset open Polynomial namespace Polynomial universe u v w z variable {R : Type u} {S : Type v} {T : Type w} {A : Type z} {A' B : Type*} {a b : R} {n : ℕ} section CommSemiring variable [CommSemiring R] [Semiring A] [Semiring B] [Algebra R A] [Algebra R B] variable {p q r : R[X]} /-- Note that this instance also provides `Algebra R R[X]`. -/ instance algebraOfAlgebra : Algebra R A[X] where smul_def' r p := toFinsupp_injective <| by dsimp only [RingHom.toFun_eq_coe, RingHom.comp_apply] rw [toFinsupp_smul, toFinsupp_mul, toFinsupp_C] exact Algebra.smul_def' _ _ commutes' r p := toFinsupp_injective <| by dsimp only [RingHom.toFun_eq_coe, RingHom.comp_apply] simp_rw [toFinsupp_mul, toFinsupp_C] convert Algebra.commutes' r p.toFinsupp algebraMap := C.comp (algebraMap R A) @[simp] theorem algebraMap_apply (r : R) : algebraMap R A[X] r = C (algebraMap R A r) := rfl @[simp] theorem toFinsupp_algebraMap (r : R) : (algebraMap R A[X] r).toFinsupp = algebraMap R _ r := show toFinsupp (C (algebraMap _ _ r)) = _ by rw [toFinsupp_C] rfl theorem ofFinsupp_algebraMap (r : R) : (⟨algebraMap R _ r⟩ : A[X]) = algebraMap R A[X] r := toFinsupp_injective (toFinsupp_algebraMap _).symm /-- When we have `[CommSemiring R]`, the function `C` is the same as `algebraMap R R[X]`. (But note that `C` is defined when `R` is not necessarily commutative, in which case `algebraMap` is not available.) -/ theorem C_eq_algebraMap (r : R) : C r = algebraMap R R[X] r := rfl @[simp] theorem algebraMap_eq : algebraMap R R[X] = C := rfl /-- `Polynomial.C` as an `AlgHom`. -/ @[simps! apply] def CAlgHom : A →ₐ[R] A[X] where toRingHom := C commutes' _ := rfl /-- Extensionality lemma for algebra maps out of `A'[X]` over a smaller base ring than `A'` -/ @[ext 1100] theorem algHom_ext' {f g : A[X] →ₐ[R] B} (hC : f.comp CAlgHom = g.comp CAlgHom) (hX : f X = g X) : f = g := AlgHom.coe_ringHom_injective (ringHom_ext' (congr_arg AlgHom.toRingHom hC) hX) variable (R) in open AddMonoidAlgebra in /-- Algebra 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 toFinsuppIsoAlg : R[X] ≃ₐ[R] R[ℕ] := { toFinsuppIso R with commutes' := fun r => by dsimp } instance subalgebraNontrivial [Nontrivial A] : Nontrivial (Subalgebra R A[X]) := ⟨⟨⊥, ⊤, by rw [Ne, SetLike.ext_iff, not_forall] refine ⟨X, ?_⟩ simp only [Algebra.mem_bot, not_exists, Set.mem_range, iff_true, Algebra.mem_top, algebraMap_apply] intro x rw [ext_iff, not_forall] refine ⟨1, ?_⟩ simp⟩⟩ @[simp] theorem algHom_eval₂_algebraMap {R A B : Type*} [CommSemiring R] [Semiring A] [Semiring B] [Algebra R A] [Algebra R B] (p : R[X]) (f : A →ₐ[R] B) (a : A) : f (eval₂ (algebraMap R A) a p) = eval₂ (algebraMap R B) (f a) p := by simp only [eval₂_eq_sum, sum_def] simp only [map_sum, map_mul, map_pow, AlgHom.commutes] @[simp] theorem eval₂_algebraMap_X {R A : Type*} [CommSemiring R] [Semiring A] [Algebra R A] (p : R[X]) (f : R[X] →ₐ[R] A) : eval₂ (algebraMap R A) (f X) p = f p := by conv_rhs => rw [← Polynomial.sum_C_mul_X_pow_eq p] simp only [eval₂_eq_sum, sum_def] simp only [map_sum, map_mul, map_pow] simp [Polynomial.C_eq_algebraMap] -- these used to be about `algebraMap ℤ R`, but now the simp-normal form is `Int.castRingHom R`. @[simp] theorem ringHom_eval₂_intCastRingHom {R S : Type*} [Ring R] [Ring S] (p : ℤ[X]) (f : R →+* S) (r : R) : f (eval₂ (Int.castRingHom R) r p) = eval₂ (Int.castRingHom S) (f r) p := algHom_eval₂_algebraMap p f.toIntAlgHom r @[simp] theorem eval₂_intCastRingHom_X {R : Type*} [Ring R] (p : ℤ[X]) (f : ℤ[X] →+* R) : eval₂ (Int.castRingHom R) (f X) p = f p := eval₂_algebraMap_X p f.toIntAlgHom /-- `Polynomial.eval₂` as an `AlgHom` for noncommutative algebras. This is `Polynomial.eval₂RingHom'` for `AlgHom`s. -/ @[simps!] def eval₂AlgHom' (f : A →ₐ[R] B) (b : B) (hf : ∀ a, Commute (f a) b) : A[X] →ₐ[R] B where toRingHom := eval₂RingHom' f b hf commutes' _ := (eval₂_C _ _).trans (f.commutes _) section Map /-- `Polynomial.map` as an `AlgHom` for noncommutative algebras. This is the algebra version of `Polynomial.mapRingHom`. -/ def mapAlgHom (f : A →ₐ[R] B) : Polynomial A →ₐ[R] Polynomial B where toRingHom := mapRingHom f.toRingHom commutes' := by simp @[simp] theorem coe_mapAlgHom (f : A →ₐ[R] B) : ⇑(mapAlgHom f) = map f := rfl @[simp] theorem mapAlgHom_id : mapAlgHom (AlgHom.id R A) = AlgHom.id R (Polynomial A) := AlgHom.ext fun _x => map_id @[simp] theorem mapAlgHom_coe_ringHom (f : A →ₐ[R] B) : ↑(mapAlgHom f : _ →ₐ[R] Polynomial B) = (mapRingHom ↑f : Polynomial A →+* Polynomial B) := rfl @[simp] theorem mapAlgHom_comp (C : Type*) [Semiring C] [Algebra R C] (f : B →ₐ[R] C) (g : A →ₐ[R] B) : (mapAlgHom f).comp (mapAlgHom g) = mapAlgHom (f.comp g) := by ext <;> simp theorem mapAlgHom_eq_eval₂AlgHom'_CAlgHom (f : A →ₐ[R] B) : mapAlgHom f = eval₂AlgHom' (CAlgHom.comp f) X (fun a => (commute_X (C (f a))).symm) := by rfl /-- If `A` and `B` are isomorphic as `R`-algebras, then so are their polynomial rings -/ def mapAlgEquiv (f : A ≃ₐ[R] B) : Polynomial A ≃ₐ[R] Polynomial B := AlgEquiv.ofAlgHom (mapAlgHom f.toAlgHom) (mapAlgHom f.symm.toAlgHom) (by simp) (by simp) @[simp] theorem coe_mapAlgEquiv (f : A ≃ₐ[R] B) : ⇑(mapAlgEquiv f) = map f := rfl @[simp] theorem mapAlgEquiv_id : mapAlgEquiv (@AlgEquiv.refl R A _ _ _) = AlgEquiv.refl := AlgEquiv.ext fun _x => map_id @[simp] theorem mapAlgEquiv_coe_ringHom (f : A ≃ₐ[R] B) : ↑(mapAlgEquiv f : _ ≃ₐ[R] Polynomial B) = (mapRingHom ↑f : Polynomial A →+* Polynomial B) := rfl @[simp] theorem mapAlgEquiv_toAlgHom (f : A ≃ₐ[R] B) : (mapAlgEquiv f : Polynomial A →ₐ[R] Polynomial B) = mapAlgHom f := rfl @[simp] theorem mapAlgEquiv_comp (C : Type*) [Semiring C] [Algebra R C] (f : A ≃ₐ[R] B) (g : B ≃ₐ[R] C) : (mapAlgEquiv f).trans (mapAlgEquiv g) = mapAlgEquiv (f.trans g) := by ext simp end Map end CommSemiring section aeval variable [CommSemiring R] [Semiring A] [CommSemiring A'] [Semiring B] variable [Algebra R A] [Algebra R B] variable {p q : R[X]} (x : A) /-- Given a valuation `x` of the variable in an `R`-algebra `A`, `aeval R A x` is the unique `R`-algebra homomorphism from `R[X]` to `A` sending `X` to `x`. This is a stronger variant of the linear map `Polynomial.leval`. -/ def aeval : R[X] →ₐ[R] A := eval₂AlgHom' (Algebra.ofId _ _) x (Algebra.commutes · _) /-- The map `R[X] → S[X]` as an algebra homomorphism. -/ def mapAlg (R : Type u) [CommSemiring R] (S : Type v) [Semiring S] [Algebra R S] : R[X] →ₐ[R] S[X] := @aeval _ S[X] _ _ _ (X : S[X]) @[ext 1200] theorem algHom_ext {f g : R[X] →ₐ[R] B} (hX : f X = g X) : f = g := algHom_ext' (Subsingleton.elim _ _) hX theorem aeval_def (p : R[X]) : aeval x p = eval₂ (algebraMap R A) x p := rfl @[simp] lemma eval_map_algebraMap (P : R[X]) (b : B) : (map (algebraMap R B) P).eval b = aeval b P := by rw [aeval_def, eval_map] /-- `mapAlg` is the morphism induced by `R → S`. -/ theorem mapAlg_eq_map (S : Type v) [Semiring S] [Algebra R S] (p : R[X]) : mapAlg R S p = map (algebraMap R S) p := by rfl theorem aeval_zero : aeval x (0 : R[X]) = 0 := map_zero (aeval x) @[simp] theorem aeval_X : aeval x (X : R[X]) = x := eval₂_X _ x @[simp] theorem aeval_C (r : R) : aeval x (C r) = algebraMap R A r := eval₂_C _ x @[simp] theorem aeval_monomial {n : ℕ} {r : R} : aeval x (monomial n r) = algebraMap _ _ r * x ^ n := eval₂_monomial _ _ theorem aeval_X_pow {n : ℕ} : aeval x ((X : R[X]) ^ n) = x ^ n := eval₂_X_pow _ _ theorem aeval_add : aeval x (p + q) = aeval x p + aeval x q := map_add _ _ _ theorem aeval_one : aeval x (1 : R[X]) = 1 := map_one _ theorem aeval_natCast (n : ℕ) : aeval x (n : R[X]) = n := map_natCast _ _ theorem aeval_mul : aeval x (p * q) = aeval x p * aeval x q := map_mul _ _ _ theorem comp_eq_aeval : p.comp q = aeval q p := rfl theorem aeval_comp {A : Type*} [Semiring A] [Algebra R A] (x : A) : aeval x (p.comp q) = aeval (aeval x q) p := eval₂_comp' x p q section IsScalarTower variable {A : Type*} (B C : Type*) [CommSemiring A] [CommSemiring B] [Semiring C] [Algebra A B] [Algebra A C] [Algebra B C] [IsScalarTower A B C] theorem mapAlg_comp (p : A[X]) : (mapAlg A C) p = (mapAlg B C) (mapAlg A B p) := by simp [mapAlg_eq_map, map_map, IsScalarTower.algebraMap_eq A B C] theorem coeff_zero_of_isScalarTower (p : A[X]) : (algebraMap B C) ((algebraMap A B) (p.coeff 0)) = (mapAlg A C p).coeff 0 := by have h : algebraMap A C = (algebraMap B C).comp (algebraMap A B) := by ext a simp [Algebra.algebraMap_eq_smul_one, RingHom.coe_comp, Function.comp_apply] rw [mapAlg_eq_map, coeff_map, h, RingHom.comp_apply] end IsScalarTower /-- Two polynomials `p` and `q` such that `p(q(X))=X` and `q(p(X))=X` induces an automorphism of the polynomial algebra. -/ @[simps!] def algEquivOfCompEqX (p q : R[X]) (hpq : p.comp q = X) (hqp : q.comp p = X) : R[X] ≃ₐ[R] R[X] := by refine AlgEquiv.ofAlgHom (aeval p) (aeval q) ?_ ?_ <;> exact AlgHom.ext fun _ ↦ by simp [← comp_eq_aeval, comp_assoc, hpq, hqp] @[simp] theorem algEquivOfCompEqX_eq_iff (p q p' q' : R[X]) (hpq : p.comp q = X) (hqp : q.comp p = X) (hpq' : p'.comp q' = X) (hqp' : q'.comp p' = X) : algEquivOfCompEqX p q hpq hqp = algEquivOfCompEqX p' q' hpq' hqp' ↔ p = p' := ⟨fun h ↦ by simpa using congr($h X), fun h ↦ by ext1; simp [h]⟩ @[simp] theorem algEquivOfCompEqX_symm (p q : R[X]) (hpq : p.comp q = X) (hqp : q.comp p = X) : (algEquivOfCompEqX p q hpq hqp).symm = algEquivOfCompEqX q p hqp hpq := rfl /-- The automorphism of the polynomial algebra given by `p(X) ↦ p(a * X + b)`, with inverse `p(X) ↦ p(a⁻¹ * (X - b))`. -/ @[simps!] def algEquivCMulXAddC {R : Type*} [CommRing R] (a b : R) [Invertible a] : R[X] ≃ₐ[R] R[X] := algEquivOfCompEqX (C a * X + C b) (C ⅟a * (X - C b)) (by simp [← C_mul, ← mul_assoc]) (by simp [← C_mul, ← mul_assoc]) theorem algEquivCMulXAddC_symm_eq {R : Type*} [CommRing R] (a b : R) [Invertible a] : (algEquivCMulXAddC a b).symm = algEquivCMulXAddC (⅟a) (- ⅟a * b) := by ext p : 1 simp only [algEquivCMulXAddC_symm_apply, neg_mul, algEquivCMulXAddC_apply, map_neg, map_mul] congr simp [mul_add, sub_eq_add_neg] /-- The automorphism of the polynomial algebra given by `p(X) ↦ p(X+t)`, with inverse `p(X) ↦ p(X-t)`. -/ @[simps!] def algEquivAevalXAddC {R : Type*} [CommRing R] (t : R) : R[X] ≃ₐ[R] R[X] := algEquivOfCompEqX (X + C t) (X - C t) (by simp) (by simp) @[simp] theorem algEquivAevalXAddC_eq_iff {R : Type*} [CommRing R] (t t' : R) : algEquivAevalXAddC t = algEquivAevalXAddC t' ↔ t = t' := by simp [algEquivAevalXAddC] @[simp] theorem algEquivAevalXAddC_symm {R : Type*} [CommRing R] (t : R) : (algEquivAevalXAddC t).symm = algEquivAevalXAddC (-t) := by simp [algEquivAevalXAddC, sub_eq_add_neg] /-- The involutive automorphism of the polynomial algebra given by `p(X) ↦ p(-X)`. -/ @[simps!] def algEquivAevalNegX {R : Type*} [CommRing R] : R[X] ≃ₐ[R] R[X] := algEquivOfCompEqX (-X) (-X) (by simp) (by simp) theorem comp_neg_X_comp_neg_X {R : Type*} [CommRing R] (p : R[X]) : (p.comp (-X)).comp (-X) = p := by rw [comp_assoc] simp only [neg_comp, X_comp, neg_neg, comp_X] theorem aeval_algHom (f : A →ₐ[R] B) (x : A) : aeval (f x) = f.comp (aeval x) := algHom_ext <| by simp only [aeval_X, AlgHom.comp_apply] @[simp] theorem aeval_X_left : aeval (X : R[X]) = AlgHom.id R R[X] := algHom_ext <| aeval_X X theorem aeval_X_left_apply (p : R[X]) : aeval X p = p := AlgHom.congr_fun (@aeval_X_left R _) p lemma aeval_X_left_eq_map [CommSemiring S] [Algebra R S] (p : R[X]) : aeval X p = map (algebraMap R S) p := rfl theorem eval_unique (φ : R[X] →ₐ[R] A) (p) : φ p = eval₂ (algebraMap R A) (φ X) p := by rw [← aeval_def, aeval_algHom, aeval_X_left, AlgHom.comp_id] theorem aeval_algHom_apply {F : Type*} [FunLike F A B] [AlgHomClass F R A B] (f : F) (x : A) (p : R[X]) : aeval (f x) p = f (aeval x p) := by refine Polynomial.induction_on p (by simp [AlgHomClass.commutes]) (fun p q hp hq => ?_) (by simp [AlgHomClass.commutes]) rw [map_add, hp, hq, ← map_add, ← map_add] @[simp] lemma coe_aeval_mk_apply {S : Subalgebra R A} (h : x ∈ S) : (aeval (⟨x, h⟩ : S) p : A) = aeval x p := (aeval_algHom_apply S.val (⟨x, h⟩ : S) p).symm theorem aeval_algEquiv (f : A ≃ₐ[R] B) (x : A) : aeval (f x) = (f : A →ₐ[R] B).comp (aeval x) := aeval_algHom (f : A →ₐ[R] B) x theorem aeval_algebraMap_apply_eq_algebraMap_eval (x : R) (p : R[X]) : aeval (algebraMap R A x) p = algebraMap R A (p.eval x) := aeval_algHom_apply (Algebra.ofId R A) x p /-- Polynomial evaluation on a pair is a product of the evaluations on the components. -/ theorem aeval_prod (x : A × B) : aeval (R := R) x = (aeval x.1).prod (aeval x.2) := aeval_algHom (.fst R A B) x ▸ aeval_algHom (.snd R A B) x ▸ (aeval x).prod_comp (.fst R A B) (.snd R A B) /-- Polynomial evaluation on a pair is a pair of evaluations. -/ theorem aeval_prod_apply (x : A × B) (p : Polynomial R) : p.aeval x = (p.aeval x.1, p.aeval x.2) := by simp [aeval_prod] section Pi variable {I : Type*} {A : I → Type*} [∀ i, Semiring (A i)] [∀ i, Algebra R (A i)] variable (x : Π i, A i) (p : R[X]) /-- Polynomial evaluation on an indexed tuple is the indexed product of the evaluations on the components. Generalizes `Polynomial.aeval_prod` to indexed products. -/ theorem aeval_pi (x : Π i, A i) : aeval (R := R) x = Pi.algHom R A (fun i ↦ aeval (x i)) := (funext fun i ↦ aeval_algHom (Pi.evalAlgHom R A i) x) ▸ (Pi.algHom_comp R A (Pi.evalAlgHom R A) (aeval x)) theorem aeval_pi_apply₂ (j : I) : p.aeval x j = p.aeval (x j) := aeval_pi (R := R) x ▸ Pi.algHom_apply R A (fun i ↦ aeval (x i)) p j /-- Polynomial evaluation on an indexed tuple is the indexed tuple of the evaluations on the components. Generalizes `Polynomial.aeval_prod_apply` to indexed products. -/ theorem aeval_pi_apply : p.aeval x = fun j ↦ p.aeval (x j) := funext fun j ↦ aeval_pi_apply₂ x p j end Pi @[simp] theorem coe_aeval_eq_eval (r : R) : (aeval r : R[X] → R) = eval r := rfl @[simp] theorem coe_aeval_eq_evalRingHom (x : R) : ((aeval x : R[X] →ₐ[R] R) : R[X] →+* R) = evalRingHom x := rfl @[simp] theorem aeval_fn_apply {X : Type*} (g : R[X]) (f : X → R) (x : X) : ((aeval f) g) x = aeval (f x) g := (aeval_algHom_apply (Pi.evalAlgHom R (fun _ => R) x) f g).symm @[norm_cast] theorem aeval_subalgebra_coe (g : R[X]) {A : Type*} [Semiring A] [Algebra R A] (s : Subalgebra R A) (f : s) : (aeval f g : A) = aeval (f : A) g := (aeval_algHom_apply s.val f g).symm theorem coeff_zero_eq_aeval_zero (p : R[X]) : p.coeff 0 = aeval 0 p := by simp [coeff_zero_eq_eval_zero] theorem coeff_zero_eq_aeval_zero' (p : R[X]) : algebraMap R A (p.coeff 0) = aeval (0 : A) p := by simp [aeval_def] theorem map_aeval_eq_aeval_map {S T U : Type*} [Semiring S] [CommSemiring T] [Semiring U] [Algebra R S] [Algebra T U] {φ : R →+* T} {ψ : S →+* U} (h : (algebraMap T U).comp φ = ψ.comp (algebraMap R S)) (p : R[X]) (a : S) : ψ (aeval a p) = aeval (ψ a) (p.map φ) := by conv_rhs => rw [aeval_def, ← eval_map] rw [map_map, h, ← map_map, eval_map, eval₂_at_apply, aeval_def, eval_map] theorem aeval_eq_zero_of_dvd_aeval_eq_zero [CommSemiring S] [CommSemiring T] [Algebra S T] {p q : S[X]} (h₁ : p ∣ q) {a : T} (h₂ : aeval a p = 0) : aeval a q = 0 := by rw [aeval_def, ← eval_map] at h₂ ⊢ exact eval_eq_zero_of_dvd_of_eval_eq_zero (Polynomial.map_dvd (algebraMap S T) h₁) h₂ section Semiring variable [Semiring S] {f : R →+* S} theorem aeval_eq_sum_range [Algebra R S] {p : R[X]} (x : S) : aeval x p = ∑ i ∈ Finset.range (p.natDegree + 1), p.coeff i • x ^ i := by simp_rw [Algebra.smul_def] exact eval₂_eq_sum_range (algebraMap R S) x theorem aeval_eq_sum_range' [Algebra R S] {p : R[X]} {n : ℕ} (hn : p.natDegree < n) (x : S) : aeval x p = ∑ i ∈ Finset.range n, p.coeff i • x ^ i := by simp_rw [Algebra.smul_def] exact eval₂_eq_sum_range' (algebraMap R S) hn x theorem isRoot_of_eval₂_map_eq_zero (hf : Function.Injective f) {r : R} : eval₂ f (f r) p = 0 → p.IsRoot r := by intro h apply hf rw [← eval₂_hom, h, f.map_zero] theorem isRoot_of_aeval_algebraMap_eq_zero [Algebra R S] [FaithfulSMul R S] {p : R[X]} {r : R} (hr : p.aeval (algebraMap R S r) = 0) : p.IsRoot r := isRoot_of_eval₂_map_eq_zero (FaithfulSMul.algebraMap_injective _ _) hr end Semiring section CommSemiring section aevalTower variable [CommSemiring S] [Algebra S R] [Algebra S A'] [Algebra S B] /-- Version of `aeval` for defining algebra homs out of `R[X]` over a smaller base ring than `R`. -/ def aevalTower (f : R →ₐ[S] A') (x : A') : R[X] →ₐ[S] A' := eval₂AlgHom' f x fun _ => Commute.all _ _ variable (g : R →ₐ[S] A') (y : A') @[simp] theorem aevalTower_X : aevalTower g y X = y := eval₂_X _ _ @[simp] theorem aevalTower_C (x : R) : aevalTower g y (C x) = g x := eval₂_C _ _ @[simp] theorem aevalTower_comp_C : (aevalTower g y : R[X] →+* A').comp C = g := RingHom.ext <| aevalTower_C _ _ theorem aevalTower_algebraMap (x : R) : aevalTower g y (algebraMap R R[X] x) = g x := eval₂_C _ _ theorem aevalTower_comp_algebraMap : (aevalTower g y : R[X] →+* A').comp (algebraMap R R[X]) = g := aevalTower_comp_C _ _ theorem aevalTower_toAlgHom (x : R) : aevalTower g y (IsScalarTower.toAlgHom S R R[X] x) = g x := aevalTower_algebraMap _ _ _ @[simp] theorem aevalTower_comp_toAlgHom : (aevalTower g y).comp (IsScalarTower.toAlgHom S R R[X]) = g := AlgHom.coe_ringHom_injective <| aevalTower_comp_algebraMap _ _ @[simp] theorem aevalTower_id : aevalTower (AlgHom.id S S) = aeval := by ext s simp only [eval_X, aevalTower_X, coe_aeval_eq_eval] @[simp] theorem aevalTower_ofId : aevalTower (Algebra.ofId S A') = aeval := by ext simp only [aeval_X, aevalTower_X] end aevalTower end CommSemiring section CommRing variable [CommRing S] {f : R →+* S} theorem dvd_term_of_dvd_eval_of_dvd_terms {z p : S} {f : S[X]} (i : ℕ) (dvd_eval : p ∣ f.eval z) (dvd_terms : ∀ j ≠ i, p ∣ f.coeff j * z ^ j) : p ∣ f.coeff i * z ^ i := by by_cases hi : i ∈ f.support · rw [eval, eval₂_eq_sum, sum_def] at dvd_eval rw [← Finset.insert_erase hi, Finset.sum_insert (Finset.notMem_erase _ _)] at dvd_eval refine (dvd_add_left ?_).mp dvd_eval apply Finset.dvd_sum intro j hj exact dvd_terms j (Finset.ne_of_mem_erase hj) · convert dvd_zero p rw [notMem_support_iff] at hi simp [hi] theorem dvd_term_of_isRoot_of_dvd_terms {r p : S} {f : S[X]} (i : ℕ) (hr : f.IsRoot r) (h : ∀ j ≠ i, p ∣ f.coeff j * r ^ j) : p ∣ f.coeff i * r ^ i := dvd_term_of_dvd_eval_of_dvd_terms i (Eq.symm hr ▸ dvd_zero p) h end CommRing end aeval section Ring variable [Ring R] /-- The evaluation map is not generally multiplicative when the coefficient ring is noncommutative, but nevertheless any polynomial of the form `p * (X - monomial 0 r)` is sent to zero when evaluated at `r`. This is the key step in our proof of the Cayley-Hamilton theorem. -/ theorem eval_mul_X_sub_C {p : R[X]} (r : R) : (p * (X - C r)).eval r = 0 := by simp only [eval, eval₂_eq_sum, RingHom.id_apply] have bound := calc (p * (X - C r)).natDegree ≤ p.natDegree + (X - C r).natDegree := natDegree_mul_le _ ≤ p.natDegree + 1 := by grw [natDegree_X_sub_C_le] _ < p.natDegree + 2 := lt_add_one _ rw [sum_over_range' _ _ (p.natDegree + 2) bound] swap · simp rw [sum_range_succ'] conv_lhs => congr arg 2 simp [coeff_mul_X_sub_C, sub_mul, mul_assoc, ← pow_succ'] rw [sum_range_sub'] simp theorem not_isUnit_X_sub_C [Nontrivial R] (r : R) : ¬IsUnit (X - C r) := fun ⟨⟨_, g, _hfg, hgf⟩, rfl⟩ => zero_ne_one' R <| by rw [← eval_mul_X_sub_C, hgf, eval_one] end Ring section CommRing variable [CommRing R] {p : R[X]} {t : R} @[simp] theorem aeval_neg {p : R[X]} [Ring A] [Algebra R A] (x : A) : aeval x (- p) = - aeval x p := map_neg .. @[simp] theorem aeval_sub {p q : R[X]} [Ring A] [Algebra R A] (x : A) : aeval x (p - q) = aeval x p - aeval x q := map_sub .. theorem aeval_endomorphism {M : Type*} [AddCommGroup M] [Module R M] (f : M →ₗ[R] M) (v : M) (p : R[X]) : aeval f p v = p.sum fun n b => b • (f ^ n) v := by rw [aeval_def, eval₂_eq_sum] exact map_sum (LinearMap.applyₗ v) _ _ lemma X_sub_C_pow_dvd_iff {n : ℕ} : (X - C t) ^ n ∣ p ↔ X ^ n ∣ p.comp (X + C t) := by convert (map_dvd_iff <| algEquivAevalXAddC t).symm using 2 simp [C_eq_algebraMap] lemma comp_X_add_C_eq_zero_iff : p.comp (X + C t) = 0 ↔ p = 0 := EmbeddingLike.map_eq_zero_iff (f := algEquivAevalXAddC t) lemma comp_X_add_C_ne_zero_iff : p.comp (X + C t) ≠ 0 ↔ p ≠ 0 := comp_X_add_C_eq_zero_iff.not lemma dvd_comp_C_mul_X_add_C_iff (p q : R[X]) (a b : R) [Invertible a] : p ∣ q.comp (C a * X + C b) ↔ p.comp (C ⅟a * (X - C b)) ∣ q := by convert map_dvd_iff <| algEquivCMulXAddC a b using 2 simp [← comp_eq_aeval, comp_assoc, ← mul_assoc, ← C_mul] lemma dvd_comp_X_sub_C_iff (p q : R[X]) (a : R) : p ∣ q.comp (X - C a) ↔ p.comp (X + C a) ∣ q := by let _ := invertibleOne (α := R) simpa using dvd_comp_C_mul_X_add_C_iff p q 1 (-a) lemma dvd_comp_X_add_C_iff (p q : R[X]) (a : R) : p ∣ q.comp (X + C a) ↔ p.comp (X - C a) ∣ q := by simpa using dvd_comp_X_sub_C_iff p q (-a) lemma dvd_comp_neg_X_iff (p q : R[X]) : p ∣ q.comp (-X) ↔ p.comp (-X) ∣ q := by let _ := invertibleOne (α := R) let _ := invertibleNeg (R := R) 1 simpa using dvd_comp_C_mul_X_add_C_iff p q (-1) 0 variable [IsDomain R] lemma units_coeff_zero_smul (c : R[X]ˣ) (p : R[X]) : (c : R[X]).coeff 0 • p = c * p := by rw [← Polynomial.C_mul', ← Polynomial.eq_C_of_degree_eq_zero (degree_coe_units c)] end CommRing section StableSubmodule variable {M : Type*} [CommSemiring R] [AddCommMonoid M] [Module R M] {q : Submodule R M} {m : M} lemma aeval_apply_smul_mem_of_le_comap' [Semiring A] [Algebra R A] [Module A M] [IsScalarTower R A M] (hm : m ∈ q) (p : R[X]) (a : A) (hq : q ≤ q.comap (Algebra.lsmul R R M a)) : aeval a p • m ∈ q := by induction p using Polynomial.induction_on with | C a => simpa using SMulMemClass.smul_mem a hm | add f₁ f₂ h₁ h₂ => simp_rw [map_add, add_smul] exact Submodule.add_mem q h₁ h₂ | monomial n t hmq => dsimp only at hmq ⊢ rw [pow_succ', mul_left_comm, map_mul, aeval_X, mul_smul] solve_by_elim lemma aeval_apply_smul_mem_of_le_comap (hm : m ∈ q) (p : R[X]) (f : Module.End R M) (hq : q ≤ q.comap f) : aeval f p m ∈ q := aeval_apply_smul_mem_of_le_comap' hm p f hq end StableSubmodule section CommSemiring variable [CommSemiring R] {a p : R[X]} theorem eq_zero_of_mul_eq_zero_of_smul (P : R[X]) (h : ∀ r : R, r • P = 0 → r = 0) (Q : R[X]) (hQ : P * Q = 0) : Q = 0 := by suffices ∀ i, P.coeff i • Q = 0 by rw [← leadingCoeff_eq_zero] apply h simpa [ext_iff, mul_comm Q.leadingCoeff] using fun i ↦ congr_arg (·.coeff Q.natDegree) (this i) apply Nat.strong_decreasing_induction · use P.natDegree intro i hi rw [coeff_eq_zero_of_natDegree_lt hi, zero_smul] intro l IH obtain _ | hl := (natDegree_smul_le (P.coeff l) Q).lt_or_eq · apply eq_zero_of_mul_eq_zero_of_smul _ h (P.coeff l • Q) rw [smul_eq_C_mul, mul_left_comm, hQ, mul_zero] suffices P.coeff l * Q.leadingCoeff = 0 by rwa [← leadingCoeff_eq_zero, ← coeff_natDegree, coeff_smul, hl, coeff_natDegree, smul_eq_mul] let m := Q.natDegree suffices (P * Q).coeff (l + m) = P.coeff l * Q.leadingCoeff by rw [← this, hQ, coeff_zero] rw [coeff_mul] apply Finset.sum_eq_single (l, m) _ (by simp) simp only [Finset.mem_antidiagonal, ne_eq, Prod.forall, Prod.mk.injEq, not_and] intro i j hij H obtain hi | rfl | hi := lt_trichotomy i l · have hj : m < j := by omega rw [coeff_eq_zero_of_natDegree_lt hj, mul_zero] · cutsat · rw [← coeff_C_mul, ← smul_eq_C_mul, IH _ hi, coeff_zero] termination_by Q.natDegree open nonZeroDivisors /-- *McCoy theorem*: a polynomial `P : R[X]` is a zerodivisor if and only if there is `a : R` such that `a ≠ 0` and `a • P = 0`. -/ theorem notMem_nonZeroDivisors_iff {P : R[X]} : P ∉ R[X]⁰ ↔ ∃ a : R, a ≠ 0 ∧ a • P = 0 := by refine ⟨fun hP ↦ ?_, fun ⟨a, ha, h⟩ h1 ↦ ha <| C_eq_zero.1 <| (h1.2 _) <| smul_eq_C_mul a ▸ h⟩ by_contra! h obtain ⟨Q, hQ⟩ := notMem_nonZeroDivisors_iff_right.1 hP refine hQ.2 (eq_zero_of_mul_eq_zero_of_smul P (fun a ha ↦ ?_) Q (mul_comm P _ ▸ hQ.1)) contrapose! ha exact h a ha @[deprecated (since := "2025-05-24")] alias nmem_nonZeroDivisors_iff := notMem_nonZeroDivisors_iff protected lemma mem_nonZeroDivisors_iff {P : R[X]} : P ∈ R[X]⁰ ↔ ∀ a : R, a • P = 0 → a = 0 := by simpa [not_imp_not] using (notMem_nonZeroDivisors_iff (P := P)).not lemma mem_nonzeroDivisors_of_coeff_mem {p : R[X]} (n : ℕ) (hp : p.coeff n ∈ R⁰) : p ∈ R[X]⁰ := Polynomial.mem_nonZeroDivisors_iff.mpr fun r hr ↦ hp.2 _ (by simpa using congr(coeff $hr n)) lemma X_mem_nonzeroDivisors : X ∈ R[X]⁰ := mem_nonzeroDivisors_of_coeff_mem 1 (by simp [one_mem]) end CommSemiring end Polynomial
.lake/packages/mathlib/Mathlib/Algebra/Polynomial/SumIteratedDerivative.lean
import Mathlib.Algebra.Polynomial.AlgebraMap import Mathlib.Algebra.Polynomial.BigOperators import Mathlib.Algebra.Polynomial.Degree.Lemmas import Mathlib.Algebra.Polynomial.Derivative import Mathlib.Algebra.Polynomial.Eval.SMul /-! # Sum of iterated derivatives This file introduces `Polynomial.sumIDeriv`, the sum of the iterated derivatives of a polynomial, as a linear map. This is used in particular in the proof of the Lindemann-Weierstrass theorem (see https://github.com/leanprover-community/mathlib4/pull/6718). ## Main results * `Polynomial.sumIDeriv`: Sum of iterated derivatives of a polynomial, as a linear map * `Polynomial.sumIDeriv_apply`, `Polynomial.sumIDeriv_apply_of_lt`, `Polynomial.sumIDeriv_apply_of_le`: `Polynomial.sumIDeriv` expressed as a sum * `Polynomial.sumIDeriv_C`, `Polynomial.sumIDeriv_X`: `Polynomial.sumIDeriv` applied to simple polynomials * `Polynomial.sumIDeriv_map`: `Polynomial.sumIDeriv` commutes with `Polynomial.map` * `Polynomial.sumIDeriv_derivative`: `Polynomial.sumIDeriv` commutes with `Polynomial.derivative` * `Polynomial.sumIDeriv_eq_self_add`: `sumIDeriv p = p + derivative (sumIDeriv p)` * `Polynomial.exists_iterate_derivative_eq_factorial_smul`: the `k`-th iterated derivative of a polynomial has a common factor `k!` * `Polynomial.aeval_iterate_derivative_of_lt`, `Polynomial.aeval_iterate_derivative_self`, `Polynomial.aeval_iterate_derivative_of_ge`: applying `Polynomial.aeval` to iterated derivatives * `Polynomial.aeval_sumIDeriv`, `Polynomial.aeval_sumIDeriv_of_pos`: applying `Polynomial.aeval` to `Polynomial.sumIDeriv` -/ open Finset open scoped Nat namespace Polynomial variable {R S : Type*} section Semiring variable [Semiring R] [Semiring S] /-- Sum of iterated derivatives of a polynomial, as a linear map This definition does not allow different weights for the derivatives. It is likely that it could be extended to allow them, but this was not needed for the initial use case (the integration by parts of the integral $I_i$ in the [Lindemann-Weierstrass](https://en.wikipedia.org/wiki/Lindemann%E2%80%93Weierstrass_theorem) theorem). -/ noncomputable def sumIDeriv : R[X] →ₗ[R] R[X] := Finsupp.lsum ℕ (fun _ ↦ LinearMap.id) ∘ₗ derivativeFinsupp theorem sumIDeriv_apply (p : R[X]) : sumIDeriv p = ∑ i ∈ range (p.natDegree + 1), derivative^[i] p := by dsimp [sumIDeriv] exact Finsupp.sum_of_support_subset _ (by simp) _ (by simp) theorem sumIDeriv_apply_of_lt {p : R[X]} {n : ℕ} (hn : p.natDegree < n) : sumIDeriv p = ∑ i ∈ range n, derivative^[i] p := by dsimp [sumIDeriv] exact Finsupp.sum_of_support_subset _ (by simp [hn]) _ (by simp) theorem sumIDeriv_apply_of_le {p : R[X]} {n : ℕ} (hn : p.natDegree ≤ n) : sumIDeriv p = ∑ i ∈ range (n + 1), derivative^[i] p := by dsimp [sumIDeriv] exact Finsupp.sum_of_support_subset _ (by simp [Nat.lt_succ, hn]) _ (by simp) @[simp] theorem sumIDeriv_C (a : R) : sumIDeriv (C a) = C a := by rw [sumIDeriv_apply, natDegree_C, zero_add, sum_range_one, Function.iterate_zero_apply] @[simp] theorem sumIDeriv_X : sumIDeriv X = X + C 1 := by rw [sumIDeriv_apply, natDegree_X, sum_range_succ, sum_range_one, Function.iterate_zero_apply, Function.iterate_one, derivative_X, eq_natCast, Nat.cast_one] @[simp] theorem sumIDeriv_map (p : R[X]) (f : R →+* S) : sumIDeriv (p.map f) = (sumIDeriv p).map f := by let n := max (p.map f).natDegree p.natDegree rw [sumIDeriv_apply_of_le (le_max_left _ _ : _ ≤ n)] rw [sumIDeriv_apply_of_le (le_max_right _ _ : _ ≤ n)] simp_rw [Polynomial.map_sum, iterate_derivative_map p f] theorem sumIDeriv_derivative (p : R[X]) : sumIDeriv (derivative p) = derivative (sumIDeriv p) := by rw [sumIDeriv_apply_of_le ((natDegree_derivative_le p).trans tsub_le_self), sumIDeriv_apply, derivative_sum] simp_rw [← Function.iterate_succ_apply, Function.iterate_succ_apply'] theorem sumIDeriv_eq_self_add (p : R[X]) : sumIDeriv p = p + derivative (sumIDeriv p) := by rw [sumIDeriv_apply, derivative_sum, sum_range_succ', sum_range_succ, add_comm, ← add_zero (Finset.sum _ _)] simp_rw [← Function.iterate_succ_apply' derivative, Nat.succ_eq_add_one, Function.iterate_zero_apply, iterate_derivative_eq_zero (Nat.lt_succ_self _)] theorem exists_iterate_derivative_eq_factorial_smul (p : R[X]) (k : ℕ) : ∃ gp : R[X], gp.natDegree ≤ p.natDegree - k ∧ derivative^[k] p = k ! • gp := by refine ⟨_, (natDegree_sum_le _ _).trans ?_, iterate_derivative_eq_factorial_smul_sum p k⟩ rw [fold_max_le] refine ⟨Nat.zero_le _, fun i hi => ?_⟩ dsimp only [Function.comp] exact (natDegree_C_mul_le _ _).trans <| (natDegree_X_pow_le _).trans <| (le_natDegree_of_mem_supp _ hi).trans <| natDegree_iterate_derivative _ _ end Semiring section CommSemiring variable [CommSemiring R] {A : Type*} [CommRing A] [Algebra R A] theorem aeval_iterate_derivative_of_lt (p : R[X]) (q : ℕ) (r : A) {p' : A[X]} (hp : p.map (algebraMap R A) = (X - C r) ^ q * p') {k : ℕ} (hk : k < q) : aeval r (derivative^[k] p) = 0 := by have h (x) : (X - C r) ^ (q - (k - x)) = (X - C r) ^ 1 * (X - C r) ^ (q - (k - x) - 1) := by rw [← pow_add, add_tsub_cancel_of_le] rw [Nat.lt_iff_add_one_le] at hk exact (le_tsub_of_add_le_left hk).trans (tsub_le_tsub_left (tsub_le_self : _ ≤ k) _) rw [aeval_def, eval₂_eq_eval_map, ← iterate_derivative_map] simp_rw [hp, iterate_derivative_mul, iterate_derivative_X_sub_pow, ← smul_mul_assoc, smul_smul, h, ← mul_smul_comm, mul_assoc, ← mul_sum, eval_mul, pow_one, eval_sub, eval_X, eval_C, sub_self, zero_mul] theorem aeval_iterate_derivative_self (p : R[X]) (q : ℕ) (r : A) {p' : A[X]} (hp : p.map (algebraMap R A) = (X - C r) ^ q * p') : aeval r (derivative^[q] p) = q ! • p'.eval r := by have h (x) (h : 1 ≤ x) (h' : x ≤ q) : (X - C r) ^ (q - (q - x)) = (X - C r) ^ 1 * (X - C r) ^ (q - (q - x) - 1) := by rw [← pow_add, add_tsub_cancel_of_le] rwa [tsub_tsub_cancel_of_le h'] rw [aeval_def, eval₂_eq_eval_map, ← iterate_derivative_map] simp_rw [hp, iterate_derivative_mul, iterate_derivative_X_sub_pow, ← smul_mul_assoc, smul_smul] rw [sum_range_succ', Nat.choose_zero_right, one_mul, tsub_zero, Nat.descFactorial_self, tsub_self, pow_zero, smul_mul_assoc, one_mul, Function.iterate_zero_apply, eval_add, eval_smul] convert zero_add _ rw [eval_finset_sum] apply sum_eq_zero intro x hx rw [h (x + 1) le_add_self (Nat.add_one_le_iff.mpr (mem_range.mp hx)), pow_one, eval_mul, eval_smul, eval_mul, eval_sub, eval_X, eval_C, sub_self, zero_mul, smul_zero, zero_mul] variable (A) theorem aeval_iterate_derivative_of_ge (p : R[X]) (q : ℕ) {k : ℕ} (hk : q ≤ k) : ∃ gp : R[X], gp.natDegree ≤ p.natDegree - k ∧ ∀ r : A, aeval r (derivative^[k] p) = q ! • aeval r gp := by obtain ⟨p', p'_le, hp'⟩ := exists_iterate_derivative_eq_factorial_smul p k obtain ⟨k, rfl⟩ := Nat.exists_eq_add_of_le hk refine ⟨((q + k).descFactorial k : R[X]) * p', (natDegree_C_mul_le _ _).trans p'_le, fun r => ?_⟩ simp_rw [hp', nsmul_eq_mul, map_mul, map_natCast, ← mul_assoc, ← Nat.cast_mul, Nat.add_descFactorial_eq_ascFactorial, Nat.factorial_mul_ascFactorial] theorem aeval_sumIDeriv_eq_eval (p : R[X]) (r : A) : aeval r (sumIDeriv p) = eval r (sumIDeriv (map (algebraMap R A) p)) := by rw [aeval_def, eval, sumIDeriv_map, eval₂_map, RingHom.id_comp] theorem aeval_sumIDeriv (p : R[X]) (q : ℕ) : ∃ gp : R[X], gp.natDegree ≤ p.natDegree - q ∧ ∀ (r : A), (X - C r) ^ q ∣ p.map (algebraMap R A) → aeval r (sumIDeriv p) = q ! • aeval r gp := by have h (k) : ∃ gp : R[X], gp.natDegree ≤ p.natDegree - q ∧ ∀ (r : A), (X - C r) ^ q ∣ p.map (algebraMap R A) → aeval r (derivative^[k] p) = q ! • aeval r gp := by cases lt_or_ge k q with | inl hk => use 0 rw [natDegree_zero] use Nat.zero_le _ intro r ⟨p', hp⟩ rw [map_zero, smul_zero, aeval_iterate_derivative_of_lt p q r hp hk] | inr hk => obtain ⟨gp, gp_le, h⟩ := aeval_iterate_derivative_of_ge A p q hk exact ⟨gp, gp_le.trans (tsub_le_tsub_left hk _), fun r _ => h r⟩ choose c h using h choose c_le hc using h refine ⟨(range (p.natDegree + 1)).sum c, ?_, ?_⟩ · refine (natDegree_sum_le _ _).trans ?_ rw [fold_max_le] exact ⟨Nat.zero_le _, fun i _ => c_le i⟩ intro r ⟨p', hp⟩ rw [sumIDeriv_apply, map_sum]; simp_rw [hc _ r ⟨_, hp⟩, map_sum, smul_sum] theorem aeval_sumIDeriv_of_pos [Nontrivial A] [NoZeroDivisors A] (p : R[X]) {q : ℕ} (hq : 0 < q) (inj_amap : Function.Injective (algebraMap R A)) : ∃ gp : R[X], gp.natDegree ≤ p.natDegree - q ∧ ∀ (r : A) {p' : A[X]}, p.map (algebraMap R A) = (X - C r) ^ (q - 1) * p' → aeval r (sumIDeriv p) = (q - 1)! • p'.eval r + q ! • aeval r gp := by rcases eq_or_ne p 0 with (rfl | p0) · use 0 rw [natDegree_zero] use Nat.zero_le _ intro r p' hp rw [map_zero, map_zero, smul_zero, add_zero] rw [Polynomial.map_zero] at hp replace hp := (mul_eq_zero.mp hp.symm).resolve_left ?_ · rw [hp, eval_zero, smul_zero] exact fun h => X_sub_C_ne_zero r (eq_zero_of_pow_eq_zero h) let c k := if hk : q ≤ k then (aeval_iterate_derivative_of_ge A p q hk).choose else 0 have c_le (k) : (c k).natDegree ≤ p.natDegree - k := by dsimp only [c] split_ifs with h · exact (aeval_iterate_derivative_of_ge A p q h).choose_spec.1 · rw [natDegree_zero]; exact Nat.zero_le _ have hc (k) (hk : q ≤ k) : ∀ (r : A), aeval r (derivative^[k] p) = q ! • aeval r (c k) := by simp_rw [c, dif_pos hk] exact (aeval_iterate_derivative_of_ge A p q hk).choose_spec.2 refine ⟨∑ x ∈ Ico q (p.natDegree + 1), c x, ?_, ?_⟩ · refine (natDegree_sum_le _ _).trans ?_ rw [fold_max_le] exact ⟨Nat.zero_le _, fun i hi => (c_le i).trans (tsub_le_tsub_left (mem_Ico.mp hi).1 _)⟩ intro r p' hp have : range (p.natDegree + 1) = range q ∪ Ico q (p.natDegree + 1) := by rw [range_eq_Ico, Ico_union_Ico_eq_Ico hq.le] rw [← tsub_le_iff_right] calc q - 1 ≤ q - 1 + p'.natDegree := le_self_add _ = (p.map <| algebraMap R A).natDegree := by rw [hp, natDegree_mul, natDegree_pow, natDegree_X_sub_C, mul_one, ← Nat.sub_add_comm (Nat.one_le_of_lt hq)] · exact pow_ne_zero _ (X_sub_C_ne_zero r) · rintro rfl rw [mul_zero, Polynomial.map_eq_zero_iff inj_amap] at hp exact p0 hp _ ≤ p.natDegree := natDegree_map_le rw [← zero_add ((q - 1)! • p'.eval r)] rw [sumIDeriv_apply, map_sum, map_sum, this] have : range q = range (q - 1 + 1) := by rw [tsub_add_cancel_of_le (Nat.one_le_of_lt hq)] rw [sum_union, this, sum_range_succ] · congr 2 · apply sum_eq_zero exact fun x hx => aeval_iterate_derivative_of_lt p _ r hp (mem_range.mp hx) · rw [← aeval_iterate_derivative_self _ _ _ hp] · rw [smul_sum, sum_congr rfl] intro k hk exact hc k (mem_Ico.mp hk).1 r · rw [range_eq_Ico, disjoint_iff_inter_eq_empty, eq_empty_iff_forall_notMem] intro x hx rw [mem_inter, mem_Ico, mem_Ico] at hx exact hx.1.2.not_ge hx.2.1 end CommSemiring theorem eval_sumIDeriv_of_pos [CommRing R] [Nontrivial R] [NoZeroDivisors R] (p : R[X]) {q : ℕ} (hq : 0 < q) : ∃ gp : R[X], gp.natDegree ≤ p.natDegree - q ∧ ∀ (r : R) {p' : R[X]}, p = ((X : R[X]) - C r) ^ (q - 1) * p' → eval r (sumIDeriv p) = (q - 1)! • p'.eval r + q ! • eval r gp := by simpa using aeval_sumIDeriv_of_pos R p hq Function.injective_id end Polynomial
.lake/packages/mathlib/Mathlib/Algebra/Polynomial/Sequence.lean
import Mathlib.Algebra.Polynomial.Monic import Mathlib.LinearAlgebra.Basis.Basic /-! # Polynomial sequences We define polynomial sequences – sequences of polynomials `a₀, a₁, ...` such that the polynomial `aᵢ` has degree `i`. ## Main definitions * `Polynomial.Sequence R`: the type of polynomial sequences with coefficients in `R` ## Main statements * `Polynomial.Sequence.basis`: a sequence is a basis for `R[X]` ## TODO Generalize linear independence to: * `IsCancelAdd` semirings * just require coefficients are regular * arbitrary sets of polynomials which are pairwise different degree. -/ open Module Submodule open scoped Function variable (R : Type*) namespace Polynomial /-- A sequence of polynomials such that the polynomial at index `i` has degree `i`. -/ structure Sequence [Semiring R] where /-- The `i`-th element in the sequence. Use `S i` instead, defined via `CoeFun`. -/ protected elems' : ℕ → R[X] /-- The `i`-th element in the sequence has degree `i`. Use `S.degree_eq` instead. -/ protected degree_eq' (i : ℕ) : (elems' i).degree = i attribute [coe] Sequence.elems' namespace Sequence variable {R} /-- Make `S i` mean `S.elems' i`. -/ instance coeFun [Semiring R] : CoeFun (Sequence R) (fun _ ↦ ℕ → R[X]) := ⟨Sequence.elems'⟩ section Semiring variable [Semiring R] (S : Sequence R) /-- `S i` has degree `i`. -/ @[simp] lemma degree_eq (i : ℕ) : (S i).degree = i := S.degree_eq' i /-- `S i` has `natDegree` `i`. -/ @[simp] lemma natDegree_eq (i : ℕ) : (S i).natDegree = i := natDegree_eq_of_degree_eq_some <| S.degree_eq i /-- No polynomial in the sequence is zero. -/ @[simp] lemma ne_zero (i : ℕ) : S i ≠ 0 := degree_ne_bot.mp <| by simp [S.degree_eq i] /-- `S i` has strictly monotone degree. -/ lemma degree_strictMono : StrictMono <| degree ∘ S := fun _ _ ↦ by simp /-- `S i` has strictly monotone natural degree. -/ lemma natDegree_strictMono : StrictMono <| natDegree ∘ S := fun _ _ ↦ by simp end Semiring section Ring variable [Ring R] (S : Sequence R) /-- A polynomial sequence spans `R[X]` if all of its elements' leading coefficients are units. -/ protected lemma span (hCoeff : ∀ i, IsUnit (S i).leadingCoeff) : span R (Set.range S) = ⊤ := by rw [eq_top_iff'] intro P -- we proceed via strong induction on the degree `n`, after getting the 0 polynomial done nontriviality R using Subsingleton.eq_zero P generalize hp : P.natDegree = n induction n using Nat.strong_induction_on generalizing P with | h n ih => by_cases p_ne_zero : P = 0 · simp [p_ne_zero] -- let u be the inverse of `S n`'s leading coefficient obtain ⟨u, leftinv, rightinv⟩ := isUnit_iff_exists.mp <| hCoeff n -- We'll show `P` is the difference of two terms in the span: -- a polynomial whose leading term matches `P`'s and lower degree terms match `S n`'s let head := P.leadingCoeff • u • S n -- a polynomial whose leading term matches P's and whose -- and then an error correcting polynomial which gets us to `P`'s actual lower degree terms let tail := P - head -- `head` is in the span because it's a multiple of `S n` have head_mem_span : head ∈ span R (Set.range S) := by have in_span : S n ∈ span R (Set.range S) := subset_span (by simp) have smul_span := smul_mem (span R (Set.range S)) (P.leadingCoeff • u) in_span rwa [smul_assoc] at smul_span -- to show the tail is in the span we really need consider only when we needed to "correct" for -- some lower degree terms in `P` by_cases tail_eq_zero : tail = 0 · simp [head_mem_span, sub_eq_iff_eq_add.mp tail_eq_zero] -- we'll do so via the induction hypothesis, -- and once we show we can use it, `P` is a difference of two members of the span refine sub_mem_iff_left _ head_mem_span |>.mp <| -- so let's prove the tail has degree less than `n` ih tail.natDegree (natDegree_lt_iff_degree_lt tail_eq_zero |>.mpr ?_) _ rfl -- first we want that `P` and `head` have the same degree have isRightRegular_smul_leadingCoeff : IsRightRegular (u • S n).leadingCoeff := by simpa [leadingCoeff_smul_of_smul_regular, IsSMulRegular.of_mul_eq_one leftinv, rightinv] using isRegular_one.right have u_degree_same := degree_smul_of_isRightRegular_leadingCoeff (left_ne_zero_of_mul_eq_one rightinv) (hCoeff n).isRegular.right have head_degree_eq := degree_smul_of_isRightRegular_leadingCoeff (leadingCoeff_ne_zero.mpr p_ne_zero) isRightRegular_smul_leadingCoeff rw [u_degree_same, S.degree_eq n, ← hp, eq_comm, ← degree_eq_natDegree p_ne_zero, hp] at head_degree_eq -- and that this degree is also their `natDegree` have head_degree_eq_natDegree : head.degree = head.natDegree := degree_eq_natDegree <| by grind [degree_eq_bot] -- and that they have matching leading coefficients have hPhead : P.leadingCoeff = head.leadingCoeff := by rw [degree_eq_natDegree p_ne_zero, head_degree_eq_natDegree] at head_degree_eq nth_rw 2 [← coeff_natDegree] rw_mod_cast [← head_degree_eq, hp] dsimp [head] nth_rw 2 [← S.natDegree_eq n] rw [coeff_smul, coeff_smul, coeff_natDegree, smul_eq_mul, smul_eq_mul, rightinv, mul_one] -- which we can now combine to show that `P - head` must have strictly lower degree, -- as its leading term has been cancelled, completing our proof. have tail_degree_lt := P.degree_sub_lt head_degree_eq p_ne_zero hPhead rwa [degree_eq_natDegree p_ne_zero, hp] at tail_degree_lt section NoZeroDivisors variable [NoZeroDivisors R] /-- Polynomials in a polynomial sequence are linearly independent. -/ lemma linearIndependent : LinearIndependent R S := linearIndependent_iff'.mpr <| fun s g eqzero i hi ↦ by by_cases hsupzero : s.sup (fun i ↦ (g i • S i).degree) = ⊥ · have le_sup := Finset.le_sup hi (f := fun i ↦ (g i • S i).degree) exact (smul_eq_zero_iff_left (S.ne_zero i)).mp <| degree_eq_bot.mp (eq_bot_mono le_sup hsupzero) have hpairwise : {i | i ∈ s ∧ g i • S i ≠ 0}.Pairwise (Ne on fun i ↦ (g i • S i).degree) := by intro x ⟨_, hx⟩ y ⟨_, hy⟩ xney have zgx : g x ≠ 0 := (smul_ne_zero_iff.mp hx).1 have zgy : g y ≠ 0 := (smul_ne_zero_iff.mp hy).1 have rx : IsRightRegular (S x).leadingCoeff := isRegular_of_ne_zero (by simp) |>.right have ry : IsRightRegular (S y).leadingCoeff := isRegular_of_ne_zero (by simp) |>.right simp [degree_smul_of_isRightRegular_leadingCoeff, rx, ry, zgx, zgy, xney] obtain ⟨n, hn⟩ : ∃ n, (s.sup fun i ↦ (g i • S i).degree) = n := exists_eq' refine degree_ne_bot.mp ?_ eqzero |>.elim have hsum := degree_sum_eq_of_disjoint _ s hpairwise exact hsum.trans hn |>.trans_ne <| (ne_of_ne_of_eq (hsupzero ·.symm) hn).symm variable (hCoeff : ∀ i, IsUnit (S i).leadingCoeff) /-- Every polynomial sequence is a basis of `R[X]`. -/ noncomputable def basis : Basis ℕ R R[X] := Basis.mk S.linearIndependent <| eq_top_iff.mp <| S.span hCoeff /-- The `i`-th basis vector is the `i`-th polynomial in the sequence. -/ @[simp] lemma basis_eq_self (i : ℕ) : S.basis hCoeff i = S i := Basis.mk_apply _ _ _ /-- Basis elements have strictly monotone degree. -/ lemma basis_degree_strictMono : StrictMono <| degree ∘ (S.basis hCoeff) := fun _ _ ↦ by simp /-- Basis elements have strictly monotone natural degree. -/ lemma basis_natDegree_strictMono : StrictMono <| natDegree ∘ (S.basis hCoeff) := fun _ _ ↦ by simp end NoZeroDivisors end Ring end Sequence end Polynomial
.lake/packages/mathlib/Mathlib/Algebra/Polynomial/Basic.lean
import Mathlib.Algebra.Group.Submonoid.Operations import Mathlib.Algebra.MonoidAlgebra.Module import Mathlib.Algebra.MonoidAlgebra.NoZeroDivisors 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 cannot 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 :: /-- The coefficients `ℕ →₀ R` of a polynomial in `R[X]`. -/ 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 constructor /-! ### 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 rw [← ofFinsupp_add] @[simp] theorem toFinsupp_neg {R : Type u} [Ring R] (a : R[X]) : (-a).toFinsupp = -a.toFinsupp := by 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 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 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 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⟩ map_add' x y := by simp [← ofFinsupp_add] map_smul' r x := by simp [← ofFinsupp_smul] @[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 @[deprecated map_add (since := "2025-11-15")] 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_ofNat (n : ℕ) [n.AtLeastTwo] : C ofNat(n) = (ofNat(n) : R[X]) := 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 | 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, ofFinsupp.injEq] ext simp [AddMonoidAlgebra.mul_apply, 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 : m ≠ n) : (monomial n c).coeff m = 0 := Finsupp.single_eq_of_ne h @[simp] theorem coeff_zero (n : ℕ) : coeff (0 : R[X]) n = 0 := rfl @[aesop simp] 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] @[aesop simp] 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 notMem_support_iff : n ∉ p.support ↔ p.coeff n = 0 := by simp @[deprecated (since := "2025-05-23")] alias not_mem_support_iff := notMem_support_iff @[aesop 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) := (monomial_one_right_eq_X_pow n).symm @[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 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 simp only [erase_def] @[simp] theorem ofFinsupp_erase (p : R[ℕ]) (n : ℕ) : (⟨p.erase n⟩ : R[X]) = (⟨p⟩ : R[X]).erase n := by simp only [erase_def] @[simp] theorem support_erase (p : R[X]) (n : ℕ) : support (p.erase n) = (support p).erase n := by 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 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 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 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, mem_support_iff, Finset.mem_image, Ne] exact ⟨n, h, rfl⟩ @[simp] theorem coeffs_empty_iff {p : R[X]} : coeffs p = ∅ ↔ p = 0 := by refine ⟨?_, fun h ↦ by simp [h]⟩ contrapose! intro h rw [← support_nonempty] at h obtain ⟨n, hn⟩ := h rw [mem_support_iff] at hn rw [← nonempty_iff_ne_empty] exact ⟨p.coeff n, coeff_mem_coeffs hn⟩ @[simp] theorem coeffs_nonempty_iff {p : R[X]} : p.coeffs.Nonempty ↔ p ≠ 0 := by simp [Finset.nonempty_iff_ne_empty] 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, ← map_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, map_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 { toRing := Polynomial.ring mul_comm := mul_comm } section Semiring variable [Semiring R] instance nontrivial [Nontrivial R] : Nontrivial R[X] := by have h : Nontrivial R[ℕ] := by infer_instance rcases h.exists_pair_ne with ⟨x, y, hxy⟩ refine ⟨⟨⟨x⟩, ⟨y⟩, ?_⟩⟩ simp [hxy] @[simp] theorem X_ne_zero [Nontrivial R] : (X : R[X]) ≠ 0 := mt (congr_arg fun p => coeff p 1) (by simp) instance [NoZeroDivisors R] : NoZeroDivisors R[X] := (toFinsuppIso R).injective.noZeroDivisors _ (map_zero _) (map_mul _) instance [IsCancelAdd R] [IsLeftCancelMulZero R] : IsLeftCancelMulZero R[X] := (toFinsuppIso R).injective.isLeftCancelMulZero _ (map_zero _) (map_mul _) instance [IsCancelAdd R] [IsRightCancelMulZero R] : IsRightCancelMulZero R[X] := (toFinsuppIso R).injective.isRightCancelMulZero _ (map_zero _) (map_mul _) instance [IsCancelAdd R] [IsCancelMulZero R] : IsCancelMulZero R[X] where instance [IsCancelAdd R] [IsDomain R] : IsDomain R[X] where /-- See also `Polynomial.isCancelMulZero_iff`: in order for `R[X]` to have cancellative multiplication (stronger than `NoZeroDivisors` in general, but equivalent if `R` is a ring), `R` must have both cancellative multiplication and cancellative addition. -/ theorem noZeroDivisors_iff : NoZeroDivisors R[X] ↔ NoZeroDivisors R where mp _ := C_injective.noZeroDivisors _ C_0 fun _ _ ↦ C_mul mpr _ := inferInstance end Semiring section DivisionSemiring variable [DivisionSemiring R] lemma nnqsmul_eq_C_mul (q : ℚ≥0) (f : R[X]) : q • f = Polynomial.C (q : R) * f := by rw [← NNRat.smul_one_eq_cast, ← Polynomial.smul_C, C_1, smul_one_mul] end DivisionSemiring section DivisionRing variable [DivisionRing R] theorem qsmul_eq_C_mul (a : ℚ) (f : R[X]) : a • f = Polynomial.C (a : R) * f := by rw [← Rat.smul_one_eq_cast, ← Polynomial.smul_C, C_1, smul_one_mul] end DivisionRing @[simp] theorem nontrivial_iff [Semiring R] : Nontrivial R[X] ↔ Nontrivial R := ⟨fun h => let ⟨_r, _s, hrs⟩ := @exists_pair_ne _ h Nontrivial.of_polynomial_ne hrs, fun h => @Polynomial.nontrivial _ _ h⟩ section repr variable [Semiring R] protected instance repr [Repr R] [DecidableEq R] : Repr R[X] := ⟨fun p prec => let termPrecAndReprs : List (WithTop ℕ × Lean.Format) := List.map (fun | 0 => (max_prec, "C " ++ reprArg (coeff p 0)) | 1 => if coeff p 1 = 1 then (⊤, "X") else (70, "C " ++ reprArg (coeff p 1) ++ " * X") | n => if coeff p n = 1 then (80, "X ^ " ++ Nat.repr n) else (70, "C " ++ reprArg (coeff p n) ++ " * X ^ " ++ Nat.repr n)) p.support.sort match termPrecAndReprs with | [] => "0" | [(tprec, t)] => if prec ≥ tprec then Lean.Format.paren t else t | ts => -- multiple terms, use `+` precedence (if prec ≥ 65 then Lean.Format.paren else id) (Lean.Format.fill (Lean.Format.joinSep (ts.map Prod.snd) (" +" ++ Lean.Format.line)))⟩ end repr end Polynomial
.lake/packages/mathlib/Mathlib/Algebra/Polynomial/Derivative.lean
import Mathlib.Algebra.Polynomial.Degree.Domain import Mathlib.Algebra.Polynomial.Degree.Support import Mathlib.Algebra.Polynomial.Eval.Coeff import Mathlib.GroupTheory.GroupAction.Ring /-! # The derivative map on polynomials ## Main definitions * `Polynomial.derivative`: The formal derivative of polynomials, expressed as a linear map. * `Polynomial.derivativeFinsupp`: Iterated derivatives as a finite support function. -/ noncomputable section open Finset open Polynomial open scoped Nat namespace Polynomial universe u v w y z variable {R : Type u} {S : Type v} {T : Type w} {ι : Type y} {A : Type z} {a b : R} {n : ℕ} section Derivative section Semiring variable [Semiring R] /-- `derivative p` is the formal derivative of the polynomial `p` -/ def derivative : R[X] →ₗ[R] R[X] where toFun p := p.sum fun n a => C (a * n) * X ^ (n - 1) map_add' p q := by rw [sum_add_index] <;> simp only [add_mul, forall_const, RingHom.map_add, zero_mul, RingHom.map_zero] map_smul' a p := by dsimp; rw [sum_smul_index] <;> simp only [mul_sum, ← C_mul', mul_assoc, RingHom.map_mul, forall_const, zero_mul, RingHom.map_zero, sum] theorem derivative_apply (p : R[X]) : derivative p = p.sum fun n a => C (a * n) * X ^ (n - 1) := rfl theorem coeff_derivative (p : R[X]) (n : ℕ) : coeff (derivative p) n = coeff p (n + 1) * (n + 1) := by rw [derivative_apply] simp only [coeff_X_pow, coeff_sum, coeff_C_mul] rw [sum, Finset.sum_eq_single (n + 1)] · simp only [Nat.add_succ_sub_one, add_zero, mul_one, if_true]; norm_cast · intro b cases b · intros rw [Nat.cast_zero, mul_zero, zero_mul] · intro _ H rw [Nat.add_one_sub_one, if_neg (mt (congr_arg Nat.succ) H.symm), mul_zero] · simp_all @[simp] theorem derivative_zero : derivative (0 : R[X]) = 0 := derivative.map_zero theorem iterate_derivative_zero {k : ℕ} : derivative^[k] (0 : R[X]) = 0 := iterate_map_zero derivative k theorem derivative_monomial (a : R) (n : ℕ) : derivative (monomial n a) = monomial (n - 1) (a * n) := by rw [derivative_apply, sum_monomial_index, C_mul_X_pow_eq_monomial] simp @[simp] theorem derivative_monomial_succ (a : R) (n : ℕ) : derivative (monomial (n + 1) a) = monomial n (a * (n + 1)) := by rw [derivative_monomial, add_tsub_cancel_right, Nat.cast_add, Nat.cast_one] theorem derivative_C_mul_X (a : R) : derivative (C a * X) = C a := by simp [C_mul_X_eq_monomial, mul_one] theorem derivative_C_mul_X_pow (a : R) (n : ℕ) : derivative (C a * X ^ n) = C (a * n) * X ^ (n - 1) := by rw [C_mul_X_pow_eq_monomial, C_mul_X_pow_eq_monomial, derivative_monomial] theorem derivative_C_mul_X_sq (a : R) : derivative (C a * X ^ 2) = C (a * 2) * X := by rw [derivative_C_mul_X_pow, Nat.cast_two, pow_one] theorem derivative_X_pow (n : ℕ) : derivative (X ^ n : R[X]) = C (n : R) * X ^ (n - 1) := by convert derivative_C_mul_X_pow (1 : R) n <;> simp @[simp] theorem derivative_X_pow_succ (n : ℕ) : derivative (X ^ (n + 1) : R[X]) = C (n + 1 : R) * X ^ n := by simp [derivative_X_pow] theorem derivative_X_sq : derivative (X ^ 2 : R[X]) = C 2 * X := by rw [derivative_X_pow, Nat.cast_two, pow_one] @[simp] theorem derivative_C {a : R} : derivative (C a) = 0 := by simp [derivative_apply] theorem derivative_of_natDegree_zero {p : R[X]} (hp : p.natDegree = 0) : derivative p = 0 := by rw [eq_C_of_natDegree_eq_zero hp, derivative_C] @[simp] theorem derivative_X : derivative (X : R[X]) = 1 := (derivative_monomial _ _).trans <| by simp @[simp] theorem derivative_one : derivative (1 : R[X]) = 0 := derivative_C @[simp] theorem derivative_add {f g : R[X]} : derivative (f + g) = derivative f + derivative g := derivative.map_add f g theorem derivative_X_add_C (c : R) : derivative (X + C c) = 1 := by rw [derivative_add, derivative_X, derivative_C, add_zero] theorem derivative_sum {s : Finset ι} {f : ι → R[X]} : derivative (∑ b ∈ s, f b) = ∑ b ∈ s, derivative (f b) := map_sum .. theorem iterate_derivative_sum (k : ℕ) (s : Finset ι) (f : ι → R[X]) : derivative^[k] (∑ b ∈ s, f b) = ∑ b ∈ s, derivative^[k] (f b) := by simp_rw [← Module.End.pow_apply, map_sum] theorem derivative_smul {S : Type*} [SMulZeroClass S R] [IsScalarTower S R R] (s : S) (p : R[X]) : derivative (s • p) = s • derivative p := derivative.map_smul_of_tower s p @[simp] theorem iterate_derivative_smul {S : Type*} [SMulZeroClass S R] [IsScalarTower S R R] (s : S) (p : R[X]) (k : ℕ) : derivative^[k] (s • p) = s • derivative^[k] p := by induction k generalizing p with | zero => simp | succ k ih => simp [ih] @[simp] theorem iterate_derivative_C_mul (a : R) (p : R[X]) (k : ℕ) : derivative^[k] (C a * p) = C a * derivative^[k] p := by simp_rw [← smul_eq_C_mul, iterate_derivative_smul] theorem derivative_C_mul (a : R) (p : R[X]) : derivative (C a * p) = C a * derivative p := iterate_derivative_C_mul _ _ 1 theorem of_mem_support_derivative {p : R[X]} {n : ℕ} (h : n ∈ p.derivative.support) : n + 1 ∈ p.support := mem_support_iff.2 fun h1 : p.coeff (n + 1) = 0 => mem_support_iff.1 h <| show p.derivative.coeff n = 0 by rw [coeff_derivative, h1, zero_mul] theorem degree_derivative_lt {p : R[X]} (hp : p ≠ 0) : p.derivative.degree < p.degree := (Finset.sup_lt_iff <| bot_lt_iff_ne_bot.2 <| mt degree_eq_bot.1 hp).2 fun n hp => lt_of_lt_of_le (WithBot.coe_lt_coe.2 n.lt_succ_self) <| Finset.le_sup <| of_mem_support_derivative hp theorem degree_derivative_le {p : R[X]} : p.derivative.degree ≤ p.degree := letI := Classical.decEq R if H : p = 0 then le_of_eq <| by rw [H, derivative_zero] else (degree_derivative_lt H).le theorem natDegree_derivative_lt {p : R[X]} (hp : p.natDegree ≠ 0) : p.derivative.natDegree < p.natDegree := by rcases eq_or_ne (derivative p) 0 with hp' | hp' · rw [hp', Polynomial.natDegree_zero] exact hp.bot_lt · rw [natDegree_lt_natDegree_iff hp'] exact degree_derivative_lt fun h => hp (h.symm ▸ natDegree_zero) theorem natDegree_derivative_le (p : R[X]) : p.derivative.natDegree ≤ p.natDegree - 1 := by by_cases p0 : p.natDegree = 0 · simp [p0, derivative_of_natDegree_zero] · exact Nat.le_sub_one_of_lt (natDegree_derivative_lt p0) theorem natDegree_iterate_derivative (p : R[X]) (k : ℕ) : (derivative^[k] p).natDegree ≤ p.natDegree - k := by induction k with | zero => rw [Function.iterate_zero_apply, Nat.sub_zero] | succ d hd => rw [Function.iterate_succ_apply', Nat.sub_succ'] exact (natDegree_derivative_le _).trans <| Nat.sub_le_sub_right hd 1 @[simp] theorem derivative_natCast {n : ℕ} : derivative (n : R[X]) = 0 := by rw [← map_natCast C n] exact derivative_C @[simp] theorem derivative_ofNat (n : ℕ) [n.AtLeastTwo] : derivative (ofNat(n) : R[X]) = 0 := derivative_natCast theorem iterate_derivative_eq_zero {p : R[X]} {x : ℕ} (hx : p.natDegree < x) : Polynomial.derivative^[x] p = 0 := by induction h : p.natDegree using Nat.strong_induction_on generalizing p x with | _ _ ih subst h obtain ⟨t, rfl⟩ := Nat.exists_eq_succ_of_ne_zero (pos_of_gt hx).ne' rw [Function.iterate_succ_apply] by_cases hp : p.natDegree = 0 · rw [derivative_of_natDegree_zero hp, iterate_derivative_zero] have := natDegree_derivative_lt hp exact ih _ this (this.trans_le <| Nat.le_of_lt_succ hx) rfl @[simp] theorem iterate_derivative_C {k} (h : 0 < k) : derivative^[k] (C a : R[X]) = 0 := iterate_derivative_eq_zero <| (natDegree_C _).trans_lt h @[simp] theorem iterate_derivative_one {k} (h : 0 < k) : derivative^[k] (1 : R[X]) = 0 := iterate_derivative_C h @[simp] theorem iterate_derivative_X {k} (h : 1 < k) : derivative^[k] (X : R[X]) = 0 := iterate_derivative_eq_zero <| natDegree_X_le.trans_lt h theorem natDegree_eq_zero_of_derivative_eq_zero [IsAddTorsionFree R] {f : R[X]} (h : derivative f = 0) : f.natDegree = 0 := by rcases eq_or_ne f 0 with (rfl | hf) · exact natDegree_zero rw [natDegree_eq_zero_iff_degree_le_zero] by_contra! f_nat_degree_pos rw [← natDegree_pos_iff_degree_pos] at f_nat_degree_pos let m := f.natDegree - 1 have hm : m + 1 = f.natDegree := tsub_add_cancel_of_le f_nat_degree_pos have h2 := coeff_derivative f m rw [Polynomial.ext_iff] at h rw [h m, coeff_zero, ← Nat.cast_add_one, ← nsmul_eq_mul', eq_comm, smul_eq_zero] at h2 replace h2 := h2.resolve_left m.succ_ne_zero rw [hm, ← leadingCoeff, leadingCoeff_eq_zero] at h2 exact hf h2 theorem eq_C_of_derivative_eq_zero [IsAddTorsionFree R] {f : R[X]} (h : derivative f = 0) : f = C (f.coeff 0) := eq_C_of_natDegree_eq_zero <| natDegree_eq_zero_of_derivative_eq_zero h @[simp] theorem derivative_mul {f g : R[X]} : derivative (f * g) = derivative f * g + f * derivative g := by induction f using Polynomial.induction_on' with | add => simp only [add_mul, map_add, add_assoc, add_left_comm, *] | monomial m a => ?_ induction g using Polynomial.induction_on' with | add => simp only [mul_add, map_add, add_assoc, add_left_comm, *] | monomial n b => ?_ simp only [monomial_mul_monomial, derivative_monomial] simp only [mul_assoc, (Nat.cast_commute _ _).eq, Nat.cast_add, mul_add, map_add] cases m with | zero => simp only [zero_add, Nat.cast_zero, mul_zero, map_zero] | succ m => cases n with | zero => simp only [add_zero, Nat.cast_zero, mul_zero, map_zero] | succ n => grind theorem derivative_eval (p : R[X]) (x : R) : p.derivative.eval x = p.sum fun n a => a * n * x ^ (n - 1) := by simp_rw [derivative_apply, eval_sum, eval_mul_X_pow, eval_C] @[simp] theorem derivative_map [Semiring S] (p : R[X]) (f : R →+* S) : derivative (p.map f) = p.derivative.map f := by let n := max p.natDegree (map f p).natDegree rw [derivative_apply, derivative_apply] rw [sum_over_range' _ _ (n + 1) ((le_max_left _ _).trans_lt (lt_add_one _))] on_goal 1 => rw [sum_over_range' _ _ (n + 1) ((le_max_right _ _).trans_lt (lt_add_one _))] · simp only [Polynomial.map_sum, Polynomial.map_mul, Polynomial.map_C, map_mul, coeff_map, map_natCast, Polynomial.map_natCast, Polynomial.map_pow, map_X] all_goals intro n; rw [zero_mul, C_0, zero_mul] @[simp] theorem iterate_derivative_map [Semiring S] (p : R[X]) (f : R →+* S) (k : ℕ) : Polynomial.derivative^[k] (p.map f) = (Polynomial.derivative^[k] p).map f := by induction k generalizing p with | zero => simp | succ k ih => simp only [ih, Function.iterate_succ, Polynomial.derivative_map, Function.comp_apply] theorem derivative_natCast_mul {n : ℕ} {f : R[X]} : derivative ((n : R[X]) * f) = n * derivative f := by simp @[simp] theorem iterate_derivative_natCast_mul {n k : ℕ} {f : R[X]} : derivative^[k] ((n : R[X]) * f) = n * derivative^[k] f := by induction k generalizing f <;> simp [*] theorem mem_support_derivative [IsAddTorsionFree R] (p : R[X]) (n : ℕ) : n ∈ (derivative p).support ↔ n + 1 ∈ p.support := by suffices ¬p.coeff (n + 1) * (n + 1 : ℕ) = 0 ↔ coeff p (n + 1) ≠ 0 by simpa only [mem_support_iff, coeff_derivative, Ne, Nat.cast_succ] rw [← nsmul_eq_mul', smul_eq_zero] simp only [Nat.succ_ne_zero, false_or] @[simp] theorem degree_derivative_eq [IsAddTorsionFree R] (p : R[X]) (hp : 0 < natDegree p) : degree (derivative p) = (natDegree p - 1 : ℕ) := by apply le_antisymm · rw [derivative_apply] apply le_trans (degree_sum_le _ _) (Finset.sup_le _) intro n hn apply le_trans (degree_C_mul_X_pow_le _ _) (WithBot.coe_le_coe.2 (tsub_le_tsub_right _ _)) apply le_natDegree_of_mem_supp _ hn · refine le_sup ?_ rw [mem_support_derivative, tsub_add_cancel_of_le, mem_support_iff] · rw [coeff_natDegree, Ne, leadingCoeff_eq_zero] intro h rw [h, natDegree_zero] at hp exact hp.false exact hp theorem coeff_iterate_derivative {k} (p : R[X]) (m : ℕ) : (derivative^[k] p).coeff m = (m + k).descFactorial k • p.coeff (m + k) := by induction k generalizing m with | zero => simp | succ k ih => calc (derivative^[k + 1] p).coeff m _ = Nat.descFactorial (Nat.succ (m + k)) k • p.coeff (m + k.succ) * (m + 1) := by rw [Function.iterate_succ_apply', coeff_derivative, ih m.succ, Nat.succ_add, Nat.add_succ] _ = ((m + 1) * Nat.descFactorial (Nat.succ (m + k)) k) • p.coeff (m + k.succ) := by rw [← Nat.cast_add_one, ← nsmul_eq_mul', smul_smul] _ = Nat.descFactorial (m.succ + k) k.succ • p.coeff (m + k.succ) := by rw [← Nat.succ_add, Nat.descFactorial_succ, add_tsub_cancel_right] _ = Nat.descFactorial (m + k.succ) k.succ • p.coeff (m + k.succ) := by rw [Nat.succ_add_eq_add_succ] theorem iterate_derivative_eq_sum (p : R[X]) (k : ℕ) : derivative^[k] p = ∑ x ∈ (derivative^[k] p).support, C ((x + k).descFactorial k • p.coeff (x + k)) * X ^ x := by conv_lhs => rw [(derivative^[k] p).as_sum_support_C_mul_X_pow] refine sum_congr rfl fun i _ ↦ ?_ rw [coeff_iterate_derivative, Nat.descFactorial_eq_factorial_mul_choose] theorem iterate_derivative_eq_factorial_smul_sum (p : R[X]) (k : ℕ) : derivative^[k] p = k ! • ∑ x ∈ (derivative^[k] p).support, C ((x + k).choose k • p.coeff (x + k)) * X ^ x := by conv_lhs => rw [iterate_derivative_eq_sum] rw [smul_sum] refine sum_congr rfl fun i _ ↦ ?_ rw [← smul_mul_assoc, smul_C, smul_smul, Nat.descFactorial_eq_factorial_mul_choose] theorem iterate_derivative_mul {n} (p q : R[X]) : derivative^[n] (p * q) = ∑ k ∈ range n.succ, (n.choose k • (derivative^[n - k] p * derivative^[k] q)) := by induction n with | zero => simp [Finset.range] | succ n IH => calc derivative^[n + 1] (p * q) = derivative (∑ k ∈ range n.succ, n.choose k • (derivative^[n - k] p * derivative^[k] q)) := by rw [Function.iterate_succ_apply', IH] _ = (∑ k ∈ range n.succ, n.choose k • (derivative^[n - k + 1] p * derivative^[k] q)) + ∑ k ∈ range n.succ, n.choose k • (derivative^[n - k] p * derivative^[k + 1] q) := by simp_rw [derivative_sum, derivative_smul, derivative_mul, Function.iterate_succ_apply', smul_add, sum_add_distrib] _ = (∑ k ∈ range n.succ, n.choose k.succ • (derivative^[n - k] p * derivative^[k + 1] q)) + 1 • (derivative^[n + 1] p * derivative^[0] q) + ∑ k ∈ range n.succ, n.choose k • (derivative^[n - k] p * derivative^[k + 1] q) := ?_ _ = ((∑ k ∈ range n.succ, n.choose k • (derivative^[n - k] p * derivative^[k + 1] q)) + ∑ k ∈ range n.succ, n.choose k.succ • (derivative^[n - k] p * derivative^[k + 1] q)) + 1 • (derivative^[n + 1] p * derivative^[0] q) := by rw [add_comm, add_assoc] _ = (∑ i ∈ range n.succ, (n + 1).choose (i + 1) • (derivative^[n + 1 - (i + 1)] p * derivative^[i + 1] q)) + 1 • (derivative^[n + 1] p * derivative^[0] q) := by simp_rw [Nat.choose_succ_succ, Nat.succ_sub_succ, add_smul, sum_add_distrib] _ = ∑ k ∈ range n.succ.succ, n.succ.choose k • (derivative^[n.succ - k] p * derivative^[k] q) := by rw [sum_range_succ' _ n.succ, Nat.choose_zero_right, tsub_zero] congr refine (sum_range_succ' _ _).trans (congr_arg₂ (· + ·) ?_ ?_) · rw [sum_range_succ, Nat.choose_succ_self, zero_smul, add_zero] refine sum_congr rfl fun k hk => ?_ rw [mem_range] at hk congr cutsat · rw [Nat.choose_zero_right, tsub_zero] /-- Iterated derivatives as a finite support function. -/ @[simps! apply_toFun] noncomputable def derivativeFinsupp : R[X] →ₗ[R] ℕ →₀ R[X] where toFun p := .onFinset (range (p.natDegree + 1)) (derivative^[·] p) fun i ↦ by contrapose; simp_all [iterate_derivative_eq_zero, Nat.succ_le] map_add' _ _ := by ext; simp map_smul' _ _ := by ext; simp @[simp] theorem support_derivativeFinsupp_subset_range {p : R[X]} {n : ℕ} (h : p.natDegree < n) : (derivativeFinsupp p).support ⊆ range n := by dsimp [derivativeFinsupp] exact Finsupp.support_onFinset_subset.trans (Finset.range_subset_range.mpr h) @[simp] theorem derivativeFinsupp_C (r : R) : derivativeFinsupp (C r : R[X]) = .single 0 (C r) := by ext i : 1 match i with | 0 => simp | i + 1 => simp @[simp] theorem derivativeFinsupp_one : derivativeFinsupp (1 : R[X]) = .single 0 1 := by simpa using derivativeFinsupp_C (1 : R) @[simp] theorem derivativeFinsupp_X : derivativeFinsupp (X : R[X]) = .single 0 X + .single 1 1 := by ext i : 1 match i with | 0 => simp | 1 => simp | (n + 2) => simp theorem derivativeFinsupp_map [Semiring S] (p : R[X]) (f : R →+* S) : derivativeFinsupp (p.map f) = (derivativeFinsupp p).mapRange (·.map f) (by simp) := by ext i : 1 simp theorem derivativeFinsupp_derivative (p : R[X]) : derivativeFinsupp (derivative p) = (derivativeFinsupp p).comapDomain Nat.succ Nat.succ_injective.injOn := by ext i : 1 simp end Semiring section CommSemiring variable [CommSemiring R] theorem derivative_pow_succ (p : R[X]) (n : ℕ) : derivative (p ^ (n + 1)) = C (n + 1 : R) * p ^ n * derivative p := Nat.recOn n (by simp) fun n ih => by rw [pow_succ, derivative_mul, ih, Nat.add_one, mul_right_comm, C_add, add_mul, add_mul, pow_succ, ← mul_assoc, C_1, one_mul]; simp [add_mul] theorem derivative_pow (p : R[X]) (n : ℕ) : derivative (p ^ n) = C (n : R) * p ^ (n - 1) * derivative p := Nat.casesOn n (by rw [pow_zero, derivative_one, Nat.cast_zero, C_0, zero_mul, zero_mul]) fun n => by rw [p.derivative_pow_succ n, Nat.add_one_sub_one, n.cast_succ] theorem derivative_sq (p : R[X]) : derivative (p ^ 2) = C 2 * p * derivative p := by rw [derivative_pow_succ, Nat.cast_one, one_add_one_eq_two, pow_one] theorem pow_sub_one_dvd_derivative_of_pow_dvd {p q : R[X]} {n : ℕ} (dvd : q ^ n ∣ p) : q ^ (n - 1) ∣ derivative p := by obtain ⟨r, rfl⟩ := dvd rw [derivative_mul, derivative_pow] exact (((dvd_mul_left _ _).mul_right _).mul_right _).add ((pow_dvd_pow q n.pred_le).mul_right _) theorem pow_sub_dvd_iterate_derivative_of_pow_dvd {p q : R[X]} {n : ℕ} (m : ℕ) (dvd : q ^ n ∣ p) : q ^ (n - m) ∣ derivative^[m] p := by induction m generalizing p with | zero => simpa | succ m ih => rw [Nat.sub_succ, Function.iterate_succ'] exact pow_sub_one_dvd_derivative_of_pow_dvd (ih dvd) theorem pow_sub_dvd_iterate_derivative_pow (p : R[X]) (n m : ℕ) : p ^ (n - m) ∣ derivative^[m] (p ^ n) := pow_sub_dvd_iterate_derivative_of_pow_dvd m dvd_rfl theorem dvd_iterate_derivative_pow (f : R[X]) (n : ℕ) {m : ℕ} (c : R) (hm : m ≠ 0) : (n : R) ∣ eval c (derivative^[m] (f ^ n)) := by obtain ⟨m, rfl⟩ := Nat.exists_eq_succ_of_ne_zero hm rw [Function.iterate_succ_apply, derivative_pow, mul_assoc, C_eq_natCast, iterate_derivative_natCast_mul, eval_mul, eval_natCast] exact dvd_mul_right _ _ theorem iterate_derivative_X_pow_eq_natCast_mul (n k : ℕ) : derivative^[k] (X ^ n : R[X]) = ↑(Nat.descFactorial n k : R[X]) * X ^ (n - k) := by induction k with | zero => rw [Function.iterate_zero_apply, tsub_zero, Nat.descFactorial_zero, Nat.cast_one, one_mul] | succ k ih => rw [Function.iterate_succ_apply', ih, derivative_natCast_mul, derivative_X_pow, C_eq_natCast, Nat.descFactorial_succ, Nat.sub_sub, Nat.cast_mul] simp [mul_assoc, mul_left_comm] theorem iterate_derivative_X_pow_eq_C_mul (n k : ℕ) : derivative^[k] (X ^ n : R[X]) = C (Nat.descFactorial n k : R) * X ^ (n - k) := by rw [iterate_derivative_X_pow_eq_natCast_mul n k, C_eq_natCast] theorem iterate_derivative_X_pow_eq_smul (n : ℕ) (k : ℕ) : derivative^[k] (X ^ n : R[X]) = (Nat.descFactorial n k : R) • X ^ (n - k) := by rw [iterate_derivative_X_pow_eq_C_mul n k, smul_eq_C_mul] theorem derivative_X_add_C_pow (c : R) (m : ℕ) : derivative ((X + C c) ^ m) = C (m : R) * (X + C c) ^ (m - 1) := by rw [derivative_pow, derivative_X_add_C, mul_one] theorem derivative_X_add_C_sq (c : R) : derivative ((X + C c) ^ 2) = C 2 * (X + C c) := by rw [derivative_sq, derivative_X_add_C, mul_one] theorem iterate_derivative_X_add_pow (n k : ℕ) (c : R) : derivative^[k] ((X + C c) ^ n) = Nat.descFactorial n k • (X + C c) ^ (n - k) := by induction k with | zero => simp | succ k IH => rw [Nat.sub_succ', Function.iterate_succ_apply', IH, derivative_smul, derivative_X_add_C_pow, map_natCast, Nat.descFactorial_succ, nsmul_eq_mul, nsmul_eq_mul, Nat.cast_mul] ring theorem derivative_comp (p q : R[X]) : derivative (p.comp q) = derivative q * p.derivative.comp q := by induction p using Polynomial.induction_on' · simp [*, mul_add] · simp only [derivative_pow, derivative_mul, monomial_comp, derivative_monomial, derivative_C, zero_mul, C_eq_natCast, zero_add, RingHom.map_mul] ring /-- Chain rule for formal derivative of polynomials. -/ theorem derivative_eval₂_C (p q : R[X]) : derivative (p.eval₂ C q) = p.derivative.eval₂ C q * derivative q := Polynomial.induction_on p (fun r => by rw [eval₂_C, derivative_C, eval₂_zero, zero_mul]) (fun p₁ p₂ ih₁ ih₂ => by rw [eval₂_add, derivative_add, ih₁, ih₂, derivative_add, eval₂_add, add_mul]) fun n r ih => by rw [pow_succ, ← mul_assoc, eval₂_mul, eval₂_X, derivative_mul, ih, @derivative_mul _ _ _ X, derivative_X, mul_one, eval₂_add, @eval₂_mul _ _ _ _ X, eval₂_X, add_mul, mul_right_comm] theorem derivative_prod [DecidableEq ι] {s : Multiset ι} {f : ι → R[X]} : derivative (Multiset.map f s).prod = (Multiset.map (fun i => (Multiset.map f (s.erase i)).prod * derivative (f i)) s).sum := by refine Multiset.induction_on s (by simp) fun i s h => ?_ rw [Multiset.map_cons, Multiset.prod_cons, derivative_mul, Multiset.map_cons _ i s, Multiset.sum_cons, Multiset.erase_cons_head, mul_comm (derivative (f i))] congr rw [h, ← AddMonoidHom.coe_mulLeft, (AddMonoidHom.mulLeft (f i)).map_multiset_sum _, AddMonoidHom.coe_mulLeft] simp only [Function.comp_apply, Multiset.map_map] refine congr_arg _ (Multiset.map_congr rfl fun j hj => ?_) rw [← mul_assoc, ← Multiset.prod_cons, ← Multiset.map_cons] by_cases hij : i = j · simp [hij, Multiset.cons_erase hj] · simp [hij] end CommSemiring section Ring variable [Ring R] @[simp] theorem derivative_neg (f : R[X]) : derivative (-f) = -derivative f := LinearMap.map_neg derivative f theorem iterate_derivative_neg {f : R[X]} {k : ℕ} : derivative^[k] (-f) = -derivative^[k] f := iterate_map_neg derivative k f @[simp] theorem derivative_sub {f g : R[X]} : derivative (f - g) = derivative f - derivative g := LinearMap.map_sub derivative f g theorem derivative_X_sub_C (c : R) : derivative (X - C c) = 1 := by rw [derivative_sub, derivative_X, derivative_C, sub_zero] theorem iterate_derivative_sub {k : ℕ} {f g : R[X]} : derivative^[k] (f - g) = derivative^[k] f - derivative^[k] g := iterate_map_sub derivative k f g @[simp] theorem derivative_intCast {n : ℤ} : derivative (n : R[X]) = 0 := by rw [← C_eq_intCast n] exact derivative_C theorem derivative_intCast_mul {n : ℤ} {f : R[X]} : derivative ((n : R[X]) * f) = n * derivative f := by simp @[simp] theorem iterate_derivative_intCast_mul {n : ℤ} {k : ℕ} {f : R[X]} : derivative^[k] ((n : R[X]) * f) = n * derivative^[k] f := by induction k generalizing f <;> simp [*] end Ring section CommRing variable [CommRing R] theorem derivative_comp_one_sub_X (p : R[X]) : derivative (p.comp (1 - X)) = -p.derivative.comp (1 - X) := by simp [derivative_comp] @[simp] theorem iterate_derivative_comp_one_sub_X (p : R[X]) (k : ℕ) : derivative^[k] (p.comp (1 - X)) = (-1) ^ k * (derivative^[k] p).comp (1 - X) := by induction k generalizing p with | zero => simp | succ k ih => simp [ih (derivative p), derivative_comp, pow_succ] theorem eval_multiset_prod_X_sub_C_derivative [DecidableEq R] {S : Multiset R} {r : R} (hr : r ∈ S) : eval r (derivative (Multiset.map (fun a => X - C a) S).prod) = (Multiset.map (fun a => r - a) (S.erase r)).prod := by nth_rw 1 [← Multiset.cons_erase hr] have := (evalRingHom r).map_multiset_prod (Multiset.map (fun a => X - C a) (S.erase r)) simpa using this theorem derivative_X_sub_C_pow (c : R) (m : ℕ) : derivative ((X - C c) ^ m) = C (m : R) * (X - C c) ^ (m - 1) := by rw [derivative_pow, derivative_X_sub_C, mul_one] theorem derivative_X_sub_C_sq (c : R) : derivative ((X - C c) ^ 2) = C 2 * (X - C c) := by rw [derivative_sq, derivative_X_sub_C, mul_one] theorem iterate_derivative_X_sub_pow (n k : ℕ) (c : R) : derivative^[k] ((X - C c) ^ n) = n.descFactorial k • (X - C c) ^ (n - k) := by rw [sub_eq_add_neg, ← C_neg, iterate_derivative_X_add_pow] theorem iterate_derivative_X_sub_pow_self (n : ℕ) (c : R) : derivative^[n] ((X - C c) ^ n) = n.factorial := by rw [iterate_derivative_X_sub_pow, n.sub_self, pow_zero, nsmul_one, n.descFactorial_self] end CommRing section NoZeroDivisors variable [Semiring R] [NoZeroDivisors R] @[simp] theorem dvd_derivative_iff {P : R[X]} : P ∣ derivative P ↔ derivative P = 0 where mp h := by by_cases hP : P = 0 · simp only [hP, derivative_zero] exact eq_zero_of_dvd_of_degree_lt h (degree_derivative_lt hP) mpr h := by simp [h] end NoZeroDivisors section CommSemiringNoZeroDivisors variable [CommSemiring R] [NoZeroDivisors R] theorem derivative_pow_eq_zero {n : ℕ} (chn : (n : R) ≠ 0) {a : R[X]} : derivative (a ^ n) = 0 ↔ derivative a = 0 := by nontriviality R rw [← C_ne_zero, C_eq_natCast] at chn simp +contextual [derivative_pow, chn] end CommSemiringNoZeroDivisors end Derivative end Polynomial
.lake/packages/mathlib/Mathlib/Algebra/Polynomial/UnitTrinomial.lean
import Mathlib.Algebra.Polynomial.Mirror import Mathlib.Algebra.Ring.Regular import Mathlib.Data.Int.Order.Units import Mathlib.RingTheory.Coprime.Basic /-! # Unit Trinomials This file defines irreducible trinomials and proves an irreducibility criterion. ## Main definitions - `Polynomial.IsUnitTrinomial` ## Main results - `Polynomial.IsUnitTrinomial.irreducible_of_coprime`: An irreducibility criterion for unit trinomials. -/ assert_not_exists TopologicalSpace namespace Polynomial open scoped Polynomial open Finset section Semiring variable {R : Type*} [Semiring R] (k m n : ℕ) (u v w : R) /-- Shorthand for a trinomial -/ noncomputable def trinomial := C u * X ^ k + C v * X ^ m + C w * X ^ n theorem trinomial_def : trinomial k m n u v w = C u * X ^ k + C v * X ^ m + C w * X ^ n := rfl variable {k m n u v w} theorem trinomial_leading_coeff' (hkm : k < m) (hmn : m < n) : (trinomial k m n u v w).coeff n = w := by rw [trinomial_def, coeff_add, coeff_add, coeff_C_mul_X_pow, coeff_C_mul_X_pow, coeff_C_mul_X_pow, if_neg (hkm.trans hmn).ne', if_neg hmn.ne', if_pos rfl, zero_add, zero_add] theorem trinomial_middle_coeff (hkm : k < m) (hmn : m < n) : (trinomial k m n u v w).coeff m = v := by rw [trinomial_def, coeff_add, coeff_add, coeff_C_mul_X_pow, coeff_C_mul_X_pow, coeff_C_mul_X_pow, if_neg hkm.ne', if_pos rfl, if_neg hmn.ne, zero_add, add_zero] theorem trinomial_trailing_coeff' (hkm : k < m) (hmn : m < n) : (trinomial k m n u v w).coeff k = u := by rw [trinomial_def, coeff_add, coeff_add, coeff_C_mul_X_pow, coeff_C_mul_X_pow, coeff_C_mul_X_pow, if_pos rfl, if_neg hkm.ne, if_neg (hkm.trans hmn).ne, add_zero, add_zero] theorem trinomial_natDegree (hkm : k < m) (hmn : m < n) (hw : w ≠ 0) : (trinomial k m n u v w).natDegree = n := by refine natDegree_eq_of_degree_eq_some ((Finset.sup_le fun i h => ?_).antisymm <| le_degree_of_ne_zero <| by rwa [trinomial_leading_coeff' hkm hmn]) replace h := support_trinomial' k m n u v w h rw [mem_insert, mem_insert, mem_singleton] at h rcases h with (rfl | rfl | rfl) · exact WithBot.coe_le_coe.mpr (hkm.trans hmn).le · exact WithBot.coe_le_coe.mpr hmn.le · exact le_rfl theorem trinomial_natTrailingDegree (hkm : k < m) (hmn : m < n) (hu : u ≠ 0) : (trinomial k m n u v w).natTrailingDegree = k := by refine natTrailingDegree_eq_of_trailingDegree_eq_some ((Finset.le_inf fun i h => ?_).antisymm <| trailingDegree_le_of_ne_zero <| by rwa [trinomial_trailing_coeff' hkm hmn]).symm replace h := support_trinomial' k m n u v w h rw [mem_insert, mem_insert, mem_singleton] at h rcases h with (rfl | rfl | rfl) · exact le_rfl · exact WithTop.coe_le_coe.mpr hkm.le · exact WithTop.coe_le_coe.mpr (hkm.trans hmn).le theorem trinomial_leadingCoeff (hkm : k < m) (hmn : m < n) (hw : w ≠ 0) : (trinomial k m n u v w).leadingCoeff = w := by rw [leadingCoeff, trinomial_natDegree hkm hmn hw, trinomial_leading_coeff' hkm hmn] theorem trinomial_trailingCoeff (hkm : k < m) (hmn : m < n) (hu : u ≠ 0) : (trinomial k m n u v w).trailingCoeff = u := by rw [trailingCoeff, trinomial_natTrailingDegree hkm hmn hu, trinomial_trailing_coeff' hkm hmn] theorem trinomial_monic (hkm : k < m) (hmn : m < n) : (trinomial k m n u v 1).Monic := by nontriviality R exact trinomial_leadingCoeff hkm hmn one_ne_zero theorem trinomial_mirror (hkm : k < m) (hmn : m < n) (hu : u ≠ 0) (hw : w ≠ 0) : (trinomial k m n u v w).mirror = trinomial k (n - m + k) n w v u := by rw [mirror, trinomial_natTrailingDegree hkm hmn hu, reverse, trinomial_natDegree hkm hmn hw, trinomial_def, reflect_add, reflect_add, reflect_C_mul_X_pow, reflect_C_mul_X_pow, reflect_C_mul_X_pow, revAt_le (hkm.trans hmn).le, revAt_le hmn.le, revAt_le le_rfl, add_mul, add_mul, mul_assoc, mul_assoc, mul_assoc, ← pow_add, ← pow_add, ← pow_add, Nat.sub_add_cancel (hkm.trans hmn).le, Nat.sub_self, zero_add, add_comm, add_comm (C u * X ^ n), ← add_assoc, ← trinomial_def] theorem trinomial_support (hkm : k < m) (hmn : m < n) (hu : u ≠ 0) (hv : v ≠ 0) (hw : w ≠ 0) : (trinomial k m n u v w).support = {k, m, n} := support_trinomial hkm hmn hu hv hw end Semiring variable (p q : ℤ[X]) /-- A unit trinomial is a trinomial with unit coefficients. -/ def IsUnitTrinomial := ∃ (k m n : ℕ) (_ : k < m) (_ : m < n) (u v w : Units ℤ), p = trinomial k m n (u : ℤ) v w variable {p q} namespace IsUnitTrinomial theorem not_isUnit (hp : p.IsUnitTrinomial) : ¬IsUnit p := by obtain ⟨k, m, n, hkm, hmn, u, v, w, rfl⟩ := hp exact fun h => ne_zero_of_lt hmn ((trinomial_natDegree hkm hmn w.ne_zero).symm.trans (natDegree_eq_of_degree_eq_some (degree_eq_zero_of_isUnit h))) theorem card_support_eq_three (hp : p.IsUnitTrinomial) : #p.support = 3 := by obtain ⟨k, m, n, hkm, hmn, u, v, w, rfl⟩ := hp exact card_support_trinomial hkm hmn u.ne_zero v.ne_zero w.ne_zero theorem ne_zero (hp : p.IsUnitTrinomial) : p ≠ 0 := by rintro rfl simpa using hp.card_support_eq_three theorem coeff_isUnit (hp : p.IsUnitTrinomial) {k : ℕ} (hk : k ∈ p.support) : IsUnit (p.coeff k) := by obtain ⟨k, m, n, hkm, hmn, u, v, w, rfl⟩ := hp have := support_trinomial' k m n (u : ℤ) v w hk rw [mem_insert, mem_insert, mem_singleton] at this rcases this with (rfl | rfl | rfl) · refine ⟨u, by rw [trinomial_trailing_coeff' hkm hmn]⟩ · refine ⟨v, by rw [trinomial_middle_coeff hkm hmn]⟩ · refine ⟨w, by rw [trinomial_leading_coeff' hkm hmn]⟩ theorem leadingCoeff_isUnit (hp : p.IsUnitTrinomial) : IsUnit p.leadingCoeff := hp.coeff_isUnit (natDegree_mem_support_of_nonzero hp.ne_zero) theorem trailingCoeff_isUnit (hp : p.IsUnitTrinomial) : IsUnit p.trailingCoeff := hp.coeff_isUnit (natTrailingDegree_mem_support_of_nonzero hp.ne_zero) end IsUnitTrinomial theorem isUnitTrinomial_iff : p.IsUnitTrinomial ↔ #p.support = 3 ∧ ∀ k ∈ p.support, IsUnit (p.coeff k) := by refine ⟨fun hp => ⟨hp.card_support_eq_three, fun k => hp.coeff_isUnit⟩, fun hp => ?_⟩ obtain ⟨k, m, n, hkm, hmn, x, y, z, hx, hy, hz, rfl⟩ := card_support_eq_three.mp hp.1 rw [support_trinomial hkm hmn hx hy hz] at hp replace hx := hp.2 k (mem_insert_self k {m, n}) replace hy := hp.2 m (mem_insert_of_mem (mem_insert_self m {n})) replace hz := hp.2 n (mem_insert_of_mem (mem_insert_of_mem (mem_singleton_self n))) simp_rw [coeff_add, coeff_C_mul, coeff_X_pow_self, mul_one, coeff_X_pow] at hx hy hz rw [if_neg hkm.ne, if_neg (hkm.trans hmn).ne] at hx rw [if_neg hkm.ne', if_neg hmn.ne] at hy rw [if_neg (hkm.trans hmn).ne', if_neg hmn.ne'] at hz simp_rw [mul_zero, zero_add, add_zero] at hx hy hz exact ⟨k, m, n, hkm, hmn, hx.unit, hy.unit, hz.unit, rfl⟩ theorem isUnitTrinomial_iff' : p.IsUnitTrinomial ↔ (p * p.mirror).coeff (((p * p.mirror).natDegree + (p * p.mirror).natTrailingDegree) / 2) = 3 := by rw [natDegree_mul_mirror, natTrailingDegree_mul_mirror, ← mul_add, Nat.mul_div_right _ zero_lt_two, coeff_mul_mirror] refine ⟨?_, fun hp => ?_⟩ · rintro ⟨k, m, n, hkm, hmn, u, v, w, rfl⟩ rw [sum_def, trinomial_support hkm hmn u.ne_zero v.ne_zero w.ne_zero, sum_insert (mt mem_insert.mp (not_or_intro hkm.ne (mt mem_singleton.mp (hkm.trans hmn).ne))), sum_insert (mt mem_singleton.mp hmn.ne), sum_singleton, trinomial_leading_coeff' hkm hmn, trinomial_middle_coeff hkm hmn, trinomial_trailing_coeff' hkm hmn] simp_rw [← Units.val_pow_eq_pow_val, Int.units_sq, Units.val_one] decide · have key : ∀ k ∈ p.support, p.coeff k ^ 2 = 1 := fun k hk => Int.sq_eq_one_of_sq_le_three ((single_le_sum (fun k _ => sq_nonneg (p.coeff k)) hk).trans hp.le) (mem_support_iff.mp hk) refine isUnitTrinomial_iff.mpr ⟨?_, fun k hk => .of_pow_eq_one (key k hk) two_ne_zero⟩ rw [sum_def, sum_congr rfl key, sum_const, Nat.smul_one_eq_cast] at hp exact Nat.cast_injective hp theorem isUnitTrinomial_iff'' (h : p * p.mirror = q * q.mirror) : p.IsUnitTrinomial ↔ q.IsUnitTrinomial := by rw [isUnitTrinomial_iff', isUnitTrinomial_iff', h] namespace IsUnitTrinomial theorem irreducible_aux1 {k m n : ℕ} (hkm : k < m) (hmn : m < n) (u v w : Units ℤ) (hp : p = trinomial k m n (u : ℤ) v w) : C (v : ℤ) * (C (u : ℤ) * X ^ (m + n) + C (w : ℤ) * X ^ (n - m + k + n)) = ⟨Finsupp.filter (· ∈ Set.Ioo (k + n) (n + n)) (p * p.mirror).toFinsupp⟩ := by have key : n - m + k < n := by rwa [← lt_tsub_iff_right, tsub_lt_tsub_iff_left_of_le hmn.le] rw [hp, trinomial_mirror hkm hmn u.ne_zero w.ne_zero] simp_rw [trinomial_def, C_mul_X_pow_eq_monomial, add_mul, mul_add, monomial_mul_monomial, toFinsupp_add, toFinsupp_monomial, AddMonoidAlgebra, Finsupp.filter_add] rw [Finsupp.filter_single_of_neg, Finsupp.filter_single_of_neg, Finsupp.filter_single_of_neg, Finsupp.filter_single_of_neg, Finsupp.filter_single_of_neg, Finsupp.filter_single_of_pos, Finsupp.filter_single_of_neg, Finsupp.filter_single_of_pos, Finsupp.filter_single_of_neg] · simp only [add_zero, zero_add] -- Porting note: the next `rw` is needed to see through the defeq `Finsupp = AddMonoidAlgebra` rw [ofFinsupp_add] simp only [ofFinsupp_single] rw [C_mul_monomial, C_mul_monomial, mul_comm (v : ℤ) w, add_comm (n - m + k) n] · exact fun h => h.2.ne rfl · refine ⟨?_, by gcongr⟩ rwa [add_comm, add_lt_add_iff_left, lt_add_iff_pos_left, tsub_pos_iff_lt] · exact fun h => h.1.ne (add_comm k n) · constructor <;> gcongr · rw [← add_assoc, add_tsub_cancel_of_le hmn.le, add_comm] exact fun h => h.1.ne rfl · grind · exact fun h => h.1.ne rfl · exact fun h => asymm ((add_lt_add_iff_left k).mp h.1) key · exact fun h => asymm ((add_lt_add_iff_left k).mp h.1) (hkm.trans hmn) theorem irreducible_aux2 {k m m' n : ℕ} (hkm : k < m) (hmn : m < n) (hkm' : k < m') (hmn' : m' < n) (u v w : Units ℤ) (hp : p = trinomial k m n (u : ℤ) v w) (hq : q = trinomial k m' n (u : ℤ) v w) (h : p * p.mirror = q * q.mirror) : q = p ∨ q = p.mirror := by let f : ℤ[X] → ℤ[X] := fun p => ⟨Finsupp.filter (· ∈ Set.Ioo (k + n) (n + n)) p.toFinsupp⟩ replace h := congr_arg f h replace h := (irreducible_aux1 hkm hmn u v w hp).trans h replace h := h.trans (irreducible_aux1 hkm' hmn' u v w hq).symm rw [(isUnit_C.mpr v.isUnit).mul_right_inj] at h rw [binomial_eq_binomial u.ne_zero w.ne_zero] at h simp only [add_left_inj, Units.val_inj] at h rcases h with (⟨rfl, -⟩ | ⟨rfl, rfl, h⟩ | ⟨-, hm, hm'⟩) · exact Or.inl (hq.trans hp.symm) · refine Or.inr ?_ rw [← trinomial_mirror hkm' hmn' u.ne_zero u.ne_zero, eq_comm, mirror_eq_iff] at hp exact hq.trans hp · grind theorem irreducible_aux3 {k m m' n : ℕ} (hkm : k < m) (hmn : m < n) (hkm' : k < m') (hmn' : m' < n) (u v w x z : Units ℤ) (hp : p = trinomial k m n (u : ℤ) v w) (hq : q = trinomial k m' n (x : ℤ) v z) (h : p * p.mirror = q * q.mirror) : q = p ∨ q = p.mirror := by have hmul := congr_arg leadingCoeff h rw [leadingCoeff_mul, leadingCoeff_mul, mirror_leadingCoeff, mirror_leadingCoeff, hp, hq, trinomial_leadingCoeff hkm hmn w.ne_zero, trinomial_leadingCoeff hkm' hmn' z.ne_zero, trinomial_trailingCoeff hkm hmn u.ne_zero, trinomial_trailingCoeff hkm' hmn' x.ne_zero] at hmul have hadd := congr_arg (eval 1) h rw [eval_mul, eval_mul, mirror_eval_one, mirror_eval_one, ← sq, ← sq, hp, hq] at hadd simp only [eval_add, eval_C_mul, eval_X_pow, one_pow, mul_one, trinomial_def] at hadd rw [add_assoc, add_assoc, add_comm (u : ℤ), add_comm (x : ℤ), add_assoc, add_assoc] at hadd simp only [add_sq', add_assoc, add_right_inj, ← Units.val_pow_eq_pow_val, Int.units_sq] at hadd rw [mul_assoc, hmul, ← mul_assoc, add_right_inj, mul_right_inj' (show 2 * (v : ℤ) ≠ 0 from mul_ne_zero two_ne_zero v.ne_zero)] at hadd replace hadd := (Int.isUnit_add_isUnit_eq_isUnit_add_isUnit w.isUnit u.isUnit z.isUnit x.isUnit).mp hadd simp only [Units.val_inj] at hadd rcases hadd with (⟨rfl, rfl⟩ | ⟨rfl, rfl⟩) · exact irreducible_aux2 hkm hmn hkm' hmn' u v w hp hq h · rw [← mirror_inj, trinomial_mirror hkm' hmn' w.ne_zero u.ne_zero] at hq rw [mul_comm q, ← q.mirror_mirror, q.mirror.mirror_mirror] at h rw [← mirror_inj, or_comm, ← mirror_eq_iff] exact irreducible_aux2 hkm hmn (lt_add_of_pos_left k (tsub_pos_of_lt hmn')) (lt_tsub_iff_right.mp ((tsub_lt_tsub_iff_left_of_le hmn'.le).mpr hkm')) u v w hp hq h theorem irreducible_of_coprime (hp : p.IsUnitTrinomial) (h : IsRelPrime p p.mirror) : Irreducible p := by refine irreducible_of_mirror hp.not_isUnit (fun q hpq => ?_) h have hq : IsUnitTrinomial q := (isUnitTrinomial_iff'' hpq).mp hp obtain ⟨k, m, n, hkm, hmn, u, v, w, hp⟩ := hp obtain ⟨k', m', n', hkm', hmn', x, y, z, hq⟩ := hq have hk : k = k' := by rw [← mul_right_inj' (show 2 ≠ 0 from two_ne_zero), ← trinomial_natTrailingDegree hkm hmn u.ne_zero, ← hp, ← natTrailingDegree_mul_mirror, hpq, natTrailingDegree_mul_mirror, hq, trinomial_natTrailingDegree hkm' hmn' x.ne_zero] have hn : n = n' := by rw [← mul_right_inj' (show 2 ≠ 0 from two_ne_zero), ← trinomial_natDegree hkm hmn w.ne_zero, ← hp, ← natDegree_mul_mirror, hpq, natDegree_mul_mirror, hq, trinomial_natDegree hkm' hmn' z.ne_zero] subst hk subst hn rcases eq_or_eq_neg_of_sq_eq_sq (y : ℤ) (v : ℤ) ((Int.isUnit_sq y.isUnit).trans (Int.isUnit_sq v.isUnit).symm) with (h1 | h1) · rw [h1] at hq rcases irreducible_aux3 hkm hmn hkm' hmn' u v w x z hp hq hpq with (h2 | h2) · exact Or.inl h2 · exact Or.inr (Or.inr (Or.inl h2)) · rw [h1] at hq rw [trinomial_def] at hp rw [← neg_inj, neg_add, neg_add, ← neg_mul, ← neg_mul, ← neg_mul, ← C_neg, ← C_neg, ← C_neg] at hp rw [← neg_mul_neg, ← mirror_neg] at hpq rcases irreducible_aux3 hkm hmn hkm' hmn' (-u) (-v) (-w) x z hp hq hpq with (rfl | rfl) · exact Or.inr (Or.inl rfl) · exact Or.inr (Or.inr (Or.inr p.mirror_neg)) /-- A unit trinomial is irreducible if it is coprime with its mirror -/ theorem irreducible_of_isCoprime (hp : p.IsUnitTrinomial) (h : IsCoprime p p.mirror) : Irreducible p := irreducible_of_coprime hp fun _ => h.isUnit_of_dvd' end IsUnitTrinomial end Polynomial
.lake/packages/mathlib/Mathlib/Algebra/Polynomial/Lifts.lean
import Mathlib.Algebra.Polynomial.AlgebraMap import Mathlib.Algebra.Polynomial.Eval.Subring import Mathlib.Algebra.Polynomial.Monic /-! # Polynomials that lift Given semirings `R` and `S` with a morphism `f : R →+* S`, we define a subsemiring `lifts` of `S[X]` by the image of `RingHom.of (map f)`. Then, we prove that a polynomial that lifts can always be lifted to a polynomial of the same degree and that a monic polynomial that lifts can be lifted to a monic polynomial (of the same degree). ## Main definition * `lifts (f : R →+* S)` : the subsemiring of polynomials that lift. ## Main results * `lifts_and_degree_eq` : A polynomial lifts if and only if it can be lifted to a polynomial of the same degree. * `lifts_and_degree_eq_and_monic` : A monic polynomial lifts if and only if it can be lifted to a monic polynomial of the same degree. * `lifts_iff_alg` : if `R` is commutative, a polynomial lifts if and only if it is in the image of `mapAlg`, where `mapAlg : R[X] →ₐ[R] S[X]` is the only `R`-algebra map that sends `X` to `X`. ## Implementation details In general `R` and `S` are semiring, so `lifts` is a semiring. In the case of rings, see `lifts_iff_lifts_ring`. Since we do not assume `R` to be commutative, we cannot say in general that the set of polynomials that lift is a subalgebra. (By `lift_iff` this is true if `R` is commutative.) -/ open Polynomial noncomputable section namespace Polynomial universe u v w section Semiring variable {R : Type u} [Semiring R] {S : Type v} [Semiring S] {f : R →+* S} /-- We define the subsemiring of polynomials that lifts as the image of `RingHom.of (map f)`. -/ def lifts (f : R →+* S) : Subsemiring S[X] := RingHom.rangeS (mapRingHom f) theorem mem_lifts (p : S[X]) : p ∈ lifts f ↔ ∃ q : R[X], map f q = p := by simp only [coe_mapRingHom, lifts, RingHom.mem_rangeS] theorem lifts_iff_set_range (p : S[X]) : p ∈ lifts f ↔ p ∈ Set.range (map f) := by simp only [coe_mapRingHom, lifts, Set.mem_range, RingHom.mem_rangeS] theorem lifts_iff_ringHom_rangeS (p : S[X]) : p ∈ lifts f ↔ p ∈ (mapRingHom f).rangeS := by simp only [coe_mapRingHom, lifts, RingHom.mem_rangeS] theorem lifts_iff_coeff_lifts (p : S[X]) : p ∈ lifts f ↔ ∀ n : ℕ, p.coeff n ∈ Set.range f := by rw [lifts_iff_ringHom_rangeS, mem_map_rangeS f] rfl theorem lifts_iff_coeffs_subset_range (p : S[X]) : p ∈ lifts f ↔ (p.coeffs : Set S) ⊆ Set.range f := by rw [lifts_iff_coeff_lifts] constructor · intro h _ hc obtain ⟨n, ⟨-, hn⟩⟩ := mem_coeffs_iff.mp hc exact hn ▸ h n · intro h n by_cases hn : p.coeff n = 0 · exact ⟨0, by simp [hn]⟩ · exact h <| coeff_mem_coeffs hn /-- If `(r : R)`, then `C (f r)` lifts. -/ theorem C_mem_lifts (f : R →+* S) (r : R) : C (f r) ∈ lifts f := ⟨C r, by simp only [coe_mapRingHom, map_C]⟩ /-- If `(s : S)` is in the image of `f`, then `C s` lifts. -/ theorem C'_mem_lifts {f : R →+* S} {s : S} (h : s ∈ Set.range f) : C s ∈ lifts f := by obtain ⟨r, rfl⟩ := Set.mem_range.1 h use C r simp only [coe_mapRingHom, map_C] /-- The polynomial `X` lifts. -/ theorem X_mem_lifts (f : R →+* S) : (X : S[X]) ∈ lifts f := ⟨X, by simp only [coe_mapRingHom, map_X]⟩ /-- The polynomial `X ^ n` lifts. -/ theorem X_pow_mem_lifts (f : R →+* S) (n : ℕ) : (X ^ n : S[X]) ∈ lifts f := ⟨X ^ n, by simp only [coe_mapRingHom, map_pow, map_X]⟩ /-- If `p` lifts and `(r : R)` then `r * p` lifts. -/ theorem base_mul_mem_lifts {p : S[X]} (r : R) (hp : p ∈ lifts f) : C (f r) * p ∈ lifts f := by simp only [lifts, RingHom.mem_rangeS] at hp ⊢ obtain ⟨p₁, rfl⟩ := hp use C r * p₁ simp only [coe_mapRingHom, map_C, map_mul] /-- If `(s : S)` is in the image of `f`, then `monomial n s` lifts. -/ theorem monomial_mem_lifts {s : S} (n : ℕ) (h : s ∈ Set.range f) : monomial n s ∈ lifts f := by obtain ⟨r, rfl⟩ := Set.mem_range.1 h use monomial n r simp only [coe_mapRingHom, map_monomial] /-- If `p` lifts then `p.erase n` lifts. -/ theorem erase_mem_lifts {p : S[X]} (n : ℕ) (h : p ∈ lifts f) : p.erase n ∈ lifts f := by rw [lifts_iff_ringHom_rangeS, mem_map_rangeS] at h ⊢ intro k by_cases hk : k = n · use 0 simp only [hk, RingHom.map_zero, erase_same] obtain ⟨i, hi⟩ := h k use i simp only [hi, hk, erase_ne, Ne, not_false_iff] section LiftDeg theorem monomial_mem_lifts_and_degree_eq {s : S} {n : ℕ} (hl : monomial n s ∈ lifts f) : ∃ q : R[X], map f q = monomial n s ∧ q.degree = (monomial n s).degree := by rcases eq_or_ne s 0 with rfl | h · exact ⟨0, by simp⟩ obtain ⟨a, rfl⟩ := coeff_monomial_same n s ▸ (monomial n s).lifts_iff_coeff_lifts.mp hl n refine ⟨monomial n a, map_monomial f, ?_⟩ rw [degree_monomial, degree_monomial n h] exact mt (fun ha ↦ ha ▸ map_zero f) h /-- A polynomial lifts if and only if it can be lifted to a polynomial of the same degree. -/ theorem mem_lifts_and_degree_eq {p : S[X]} (hlifts : p ∈ lifts f) : ∃ q : R[X], map f q = p ∧ q.degree = p.degree := by rw [lifts_iff_coeff_lifts] at hlifts let g : ℕ → R := fun k ↦ (hlifts k).choose have hg : ∀ k, f (g k) = p.coeff k := fun k ↦ (hlifts k).choose_spec let q : R[X] := ∑ k ∈ p.support, monomial k (g k) have hq : map f q = p := by simp_rw [q, Polynomial.map_sum, map_monomial, hg, ← as_sum_support] have hq' : q.support = p.support := by simp_rw [Finset.ext_iff, mem_support_iff, q, finset_sum_coeff, coeff_monomial, Finset.sum_ite_eq', ite_ne_right_iff, mem_support_iff, and_iff_left_iff_imp, not_imp_not] exact fun k h ↦ by rw [← hg, h, map_zero] exact ⟨q, hq, congrArg Finset.max hq'⟩ end LiftDeg section Monic /-- A monic polynomial lifts if and only if it can be lifted to a monic polynomial of the same degree. -/ theorem lifts_and_degree_eq_and_monic [Nontrivial S] {p : S[X]} (hlifts : p ∈ lifts f) (hp : p.Monic) : ∃ q : R[X], map f q = p ∧ q.degree = p.degree ∧ q.Monic := by rw [lifts_iff_coeff_lifts] at hlifts let g : ℕ → R := fun k ↦ (hlifts k).choose have hg k : f (g k) = p.coeff k := (hlifts k).choose_spec let q : R[X] := X ^ p.natDegree + ∑ k ∈ Finset.range p.natDegree, C (g k) * X ^ k have hq : map f q = p := by simp_rw [q, Polynomial.map_add, Polynomial.map_sum, Polynomial.map_mul, Polynomial.map_pow, map_X, map_C, hg, ← hp.as_sum] have h : q.Monic := monic_X_pow_add (by simp_rw [← Fin.sum_univ_eq_sum_range, degree_sum_fin_lt]) exact ⟨q, hq, hq ▸ (h.degree_map f).symm, h⟩ theorem lifts_and_natDegree_eq_and_monic {p : S[X]} (hlifts : p ∈ lifts f) (hp : p.Monic) : ∃ q : R[X], map f q = p ∧ q.natDegree = p.natDegree ∧ q.Monic := by rcases subsingleton_or_nontrivial S with hR | hR · obtain rfl : p = 1 := Subsingleton.elim _ _ exact ⟨1, Subsingleton.elim _ _, by simp, by simp⟩ obtain ⟨p', h₁, h₂, h₃⟩ := lifts_and_degree_eq_and_monic hlifts hp exact ⟨p', h₁, natDegree_eq_of_degree_eq h₂, h₃⟩ end Monic end Semiring section Ring variable {R : Type u} [Ring R] {S : Type v} [Ring S] (f : R →+* S) /-- The subring of polynomials that lift. -/ def liftsRing (f : R →+* S) : Subring S[X] := RingHom.range (mapRingHom f) /-- If `R` and `S` are rings, `p` is in the subring of polynomials that lift if and only if it is in the subsemiring of polynomials that lift. -/ theorem lifts_iff_liftsRing (p : S[X]) : p ∈ lifts f ↔ p ∈ liftsRing f := by simp only [lifts, liftsRing, RingHom.mem_range, RingHom.mem_rangeS] end Ring section Algebra variable {R : Type u} [CommSemiring R] {S : Type v} [Semiring S] [Algebra R S] /-- A polynomial `p` lifts if and only if it is in the image of `mapAlg`. -/ theorem mem_lifts_iff_mem_alg (R : Type u) [CommSemiring R] {S : Type v} [Semiring S] [Algebra R S] (p : S[X]) : p ∈ lifts (algebraMap R S) ↔ p ∈ AlgHom.range (@mapAlg R _ S _ _) := by simp only [coe_mapRingHom, lifts, mapAlg_eq_map, AlgHom.mem_range, RingHom.mem_rangeS] /-- If `p` lifts and `(r : R)` then `r • p` lifts. -/ theorem smul_mem_lifts {p : S[X]} (r : R) (hp : p ∈ lifts (algebraMap R S)) : r • p ∈ lifts (algebraMap R S) := by rw [mem_lifts_iff_mem_alg] at hp ⊢ exact Subalgebra.smul_mem (mapAlg R S).range hp r theorem monic_of_monic_mapAlg [FaithfulSMul R S] {p : Polynomial R} (hp : (mapAlg R S p).Monic) : p.Monic := monic_of_injective (FaithfulSMul.algebraMap_injective R S) hp end Algebra end Polynomial