path
stringlengths
11
71
content
stringlengths
75
124k
Algebra\Order\GroupWithZero\WithZero.lean
/- Copyright (c) 2024 Kevin Buzzard. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Buzzard -/ import Mathlib.Algebra.Order.GroupWithZero.Canonical import Mathlib.Algebra.Order.GroupWithZero.Unbundled /-! # Covariant instances on `WithZero` Adding a zero to a type with a preorder and multiplication which satisfies some axiom, gives us a new type which satisfies some variant of the axiom. ## Example If `α` satisfies `b₁ < b₂ → a * b₁ < a * b₂` for all `a`, then `WithZero α` satisfies `b₁ < b₂ → a * b₁ < a * b₂` for all `a > 0`, which is `PosMulStrictMono (WithZero α)`. ## Application The type `ℤₘ₀ := WithZero (Multiplicative ℤ)` is used a lot in mathlib's valuation theory. These instances enable lemmas such as `mul_pos` to fire on `ℤₘ₀`. -/ assert_not_exists Ring -- this makes `mul_lt_mul_left`, `mul_pos` etc work on `ℤₘ₀` instance {α : Type*} [Mul α] [Preorder α] [CovariantClass α α (· * ·) (· < ·)] : PosMulStrictMono (WithZero α) where elim := @fun | ⟨(x : α), hx⟩, 0, (b : α), _ => by simpa only [mul_zero] using WithZero.zero_lt_coe _ | ⟨(x : α), hx⟩, (a : α), (b : α), h => by dsimp only norm_cast at h ⊢ exact mul_lt_mul_left' h x open Function in instance {α : Type*} [Mul α] [Preorder α] [CovariantClass α α (swap (· * ·)) (· < ·)] : MulPosStrictMono (WithZero α) where elim := @fun | ⟨(x : α), hx⟩, 0, (b : α), _ => by simpa only [mul_zero] using WithZero.zero_lt_coe _ | ⟨(x : α), hx⟩, (a : α), (b : α), h => by dsimp only norm_cast at h ⊢ exact mul_lt_mul_right' h x instance {α : Type*} [Mul α] [Preorder α] [CovariantClass α α (· * ·) (· ≤ ·)] : PosMulMono (WithZero α) where elim := @fun | ⟨0, _⟩, a, b, _ => by simp only [zero_mul, le_refl] | ⟨(x : α), _⟩, 0, _, _ => by simp only [mul_zero, WithZero.zero_le] | ⟨(x : α), hx⟩, (a : α), 0, h => (lt_irrefl 0 (lt_of_lt_of_le (WithZero.zero_lt_coe a) h)).elim | ⟨(x : α), hx⟩, (a : α), (b : α), h => by dsimp only norm_cast at h ⊢ exact mul_le_mul_left' h x -- This makes `lt_mul_of_le_of_one_lt'` work on `ℤₘ₀` open Function in instance {α : Type*} [Mul α] [Preorder α] [CovariantClass α α (swap (· * ·)) (· ≤ ·)] : MulPosMono (WithZero α) where elim := @fun | ⟨0, _⟩, a, b, _ => by simp only [mul_zero, le_refl] | ⟨(x : α), _⟩, 0, _, _ => by simp only [zero_mul, WithZero.zero_le] | ⟨(x : α), hx⟩, (a : α), 0, h => (lt_irrefl 0 (lt_of_lt_of_le (WithZero.zero_lt_coe a) h)).elim | ⟨(x : α), hx⟩, (a : α), (b : α), h => by dsimp only norm_cast at h ⊢ exact mul_le_mul_right' h x
Algebra\Order\Hom\Basic.lean
/- Copyright (c) 2022 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import Mathlib.Logic.Basic import Mathlib.Tactic.Positivity.Basic /-! # Algebraic order homomorphism classes This file defines hom classes for common properties at the intersection of order theory and algebra. ## Typeclasses Basic typeclasses * `NonnegHomClass`: Homs are nonnegative: `∀ f a, 0 ≤ f a` * `SubadditiveHomClass`: Homs are subadditive: `∀ f a b, f (a + b) ≤ f a + f b` * `SubmultiplicativeHomClass`: Homs are submultiplicative: `∀ f a b, f (a * b) ≤ f a * f b` * `MulLEAddHomClass`: `∀ f a b, f (a * b) ≤ f a + f b` * `NonarchimedeanHomClass`: `∀ a b, f (a + b) ≤ max (f a) (f b)` Group norms * `AddGroupSeminormClass`: Homs are nonnegative, subadditive, even and preserve zero. * `GroupSeminormClass`: Homs are nonnegative, respect `f (a * b) ≤ f a + f b`, `f a⁻¹ = f a` and preserve zero. * `AddGroupNormClass`: Homs are seminorms such that `f x = 0 → x = 0` for all `x`. * `GroupNormClass`: Homs are seminorms such that `f x = 0 → x = 1` for all `x`. Ring norms * `RingSeminormClass`: Homs are submultiplicative group norms. * `RingNormClass`: Homs are ring seminorms that are also additive group norms. * `MulRingSeminormClass`: Homs are ring seminorms that are multiplicative. * `MulRingNormClass`: Homs are ring norms that are multiplicative. ## Notes Typeclasses for seminorms are defined here while types of seminorms are defined in `Analysis.Normed.Group.Seminorm` and `Analysis.Normed.Ring.Seminorm` because absolute values are multiplicative ring norms but outside of this use we only consider real-valued seminorms. ## TODO Finitary versions of the current lemmas. -/ library_note "out-param inheritance"/-- Diamond inheritance cannot depend on `outParam`s in the following circumstances: * there are three classes `Top`, `Middle`, `Bottom` * all of these classes have a parameter `(α : outParam _)` * all of these classes have an instance parameter `[Root α]` that depends on this `outParam` * the `Root` class has two child classes: `Left` and `Right`, these are siblings in the hierarchy * the instance `Bottom.toMiddle` takes a `[Left α]` parameter * the instance `Middle.toTop` takes a `[Right α]` parameter * there is a `Leaf` class that inherits from both `Left` and `Right`. In that case, given instances `Bottom α` and `Leaf α`, Lean cannot synthesize a `Top α` instance, even though the hypotheses of the instances `Bottom.toMiddle` and `Middle.toTop` are satisfied. There are two workarounds: * You could replace the bundled inheritance implemented by the instance `Middle.toTop` with unbundled inheritance implemented by adding a `[Top α]` parameter to the `Middle` class. This is the preferred option since it is also more compatible with Lean 4, at the cost of being more work to implement and more verbose to use. * You could weaken the `Bottom.toMiddle` instance by making it depend on a subclass of `Middle.toTop`'s parameter, in this example replacing `[Left α]` with `[Leaf α]`. -/ open Function variable {ι F α β γ δ : Type*} /-! ### Basics -/ /-- `NonnegHomClass F α β` states that `F` is a type of nonnegative morphisms. -/ class NonnegHomClass (F α β : Type*) [Zero β] [LE β] [FunLike F α β] : Prop where /-- the image of any element is non negative. -/ apply_nonneg (f : F) : ∀ a, 0 ≤ f a /-- `SubadditiveHomClass F α β` states that `F` is a type of subadditive morphisms. -/ class SubadditiveHomClass (F α β : Type*) [Add α] [Add β] [LE β] [FunLike F α β] : Prop where /-- the image of a sum is less or equal than the sum of the images. -/ map_add_le_add (f : F) : ∀ a b, f (a + b) ≤ f a + f b /-- `SubmultiplicativeHomClass F α β` states that `F` is a type of submultiplicative morphisms. -/ @[to_additive SubadditiveHomClass] class SubmultiplicativeHomClass (F α β : Type*) [Mul α] [Mul β] [LE β] [FunLike F α β] : Prop where /-- the image of a product is less or equal than the product of the images. -/ map_mul_le_mul (f : F) : ∀ a b, f (a * b) ≤ f a * f b /-- `MulLEAddHomClass F α β` states that `F` is a type of subadditive morphisms. -/ @[to_additive SubadditiveHomClass] class MulLEAddHomClass (F α β : Type*) [Mul α] [Add β] [LE β] [FunLike F α β] : Prop where /-- the image of a product is less or equal than the sum of the images. -/ map_mul_le_add (f : F) : ∀ a b, f (a * b) ≤ f a + f b /-- `NonarchimedeanHomClass F α β` states that `F` is a type of non-archimedean morphisms. -/ class NonarchimedeanHomClass (F α β : Type*) [Add α] [LinearOrder β] [FunLike F α β] : Prop where /-- the image of a sum is less or equal than the maximum of the images. -/ map_add_le_max (f : F) : ∀ a b, f (a + b) ≤ max (f a) (f b) export NonnegHomClass (apply_nonneg) export SubadditiveHomClass (map_add_le_add) export SubmultiplicativeHomClass (map_mul_le_mul) export MulLEAddHomClass (map_mul_le_add) export NonarchimedeanHomClass (map_add_le_max) attribute [simp] apply_nonneg variable [FunLike F α β] @[to_additive] theorem le_map_mul_map_div [Group α] [CommSemigroup β] [LE β] [SubmultiplicativeHomClass F α β] (f : F) (a b : α) : f a ≤ f b * f (a / b) := by simpa only [mul_comm, div_mul_cancel] using map_mul_le_mul f (a / b) b @[to_additive existing] theorem le_map_add_map_div [Group α] [AddCommSemigroup β] [LE β] [MulLEAddHomClass F α β] (f : F) (a b : α) : f a ≤ f b + f (a / b) := by simpa only [add_comm, div_mul_cancel] using map_mul_le_add f (a / b) b -- Porting note (#11215): TODO: `to_additive` clashes @[to_additive] theorem le_map_div_mul_map_div [Group α] [CommSemigroup β] [LE β] [SubmultiplicativeHomClass F α β] (f : F) (a b c : α) : f (a / c) ≤ f (a / b) * f (b / c) := by simpa only [div_mul_div_cancel'] using map_mul_le_mul f (a / b) (b / c) @[to_additive existing] theorem le_map_div_add_map_div [Group α] [AddCommSemigroup β] [LE β] [MulLEAddHomClass F α β] (f : F) (a b c : α) : f (a / c) ≤ f (a / b) + f (b / c) := by simpa only [div_mul_div_cancel'] using map_mul_le_add f (a / b) (b / c) -- Porting note (#11215): TODO: `to_additive` clashes namespace Mathlib.Meta.Positivity open Lean Meta Qq Function /-- Extension for the `positivity` tactic: nonnegative maps take nonnegative values. -/ @[positivity DFunLike.coe _ _] def evalMap : PositivityExt where eval {_ β} _ _ e := do let .app (.app _ f) a ← whnfR e | throwError "not ↑f · where f is of NonnegHomClass" let pa ← mkAppOptM ``apply_nonneg #[none, none, β, none, none, none, none, f, a] pure (.nonnegative pa) end Mathlib.Meta.Positivity /-! ### Group (semi)norms -/ /-- `AddGroupSeminormClass F α` states that `F` is a type of `β`-valued seminorms on the additive group `α`. You should extend this class when you extend `AddGroupSeminorm`. -/ class AddGroupSeminormClass (F α β : Type*) [AddGroup α] [OrderedAddCommMonoid β] [FunLike F α β] extends SubadditiveHomClass F α β : Prop where /-- The image of zero is zero. -/ map_zero (f : F) : f 0 = 0 /-- The map is invariant under negation of its argument. -/ map_neg_eq_map (f : F) (a : α) : f (-a) = f a /-- `GroupSeminormClass F α` states that `F` is a type of `β`-valued seminorms on the group `α`. You should extend this class when you extend `GroupSeminorm`. -/ @[to_additive] class GroupSeminormClass (F α β : Type*) [Group α] [OrderedAddCommMonoid β] [FunLike F α β] extends MulLEAddHomClass F α β : Prop where /-- The image of one is zero. -/ map_one_eq_zero (f : F) : f 1 = 0 /-- The map is invariant under inversion of its argument. -/ map_inv_eq_map (f : F) (a : α) : f a⁻¹ = f a /-- `AddGroupNormClass F α` states that `F` is a type of `β`-valued norms on the additive group `α`. You should extend this class when you extend `AddGroupNorm`. -/ class AddGroupNormClass (F α β : Type*) [AddGroup α] [OrderedAddCommMonoid β] [FunLike F α β] extends AddGroupSeminormClass F α β : Prop where /-- The argument is zero if its image under the map is zero. -/ eq_zero_of_map_eq_zero (f : F) {a : α} : f a = 0 → a = 0 /-- `GroupNormClass F α` states that `F` is a type of `β`-valued norms on the group `α`. You should extend this class when you extend `GroupNorm`. -/ @[to_additive] class GroupNormClass (F α β : Type*) [Group α] [OrderedAddCommMonoid β] [FunLike F α β] extends GroupSeminormClass F α β : Prop where /-- The argument is one if its image under the map is zero. -/ eq_one_of_map_eq_zero (f : F) {a : α} : f a = 0 → a = 1 export AddGroupSeminormClass (map_neg_eq_map) export GroupSeminormClass (map_one_eq_zero map_inv_eq_map) export AddGroupNormClass (eq_zero_of_map_eq_zero) export GroupNormClass (eq_one_of_map_eq_zero) attribute [simp] map_one_eq_zero -- Porting note: `to_additive` translation already exists attribute [simp] map_neg_eq_map attribute [simp] map_inv_eq_map -- Porting note: `to_additive` translation already exists attribute [to_additive] GroupSeminormClass.toMulLEAddHomClass -- See note [lower instance priority] instance (priority := 100) AddGroupSeminormClass.toZeroHomClass [AddGroup α] [OrderedAddCommMonoid β] [AddGroupSeminormClass F α β] : ZeroHomClass F α β := { ‹AddGroupSeminormClass F α β› with } section GroupSeminormClass variable [Group α] [OrderedAddCommMonoid β] [GroupSeminormClass F α β] (f : F) (x y : α) @[to_additive] theorem map_div_le_add : f (x / y) ≤ f x + f y := by rw [div_eq_mul_inv, ← map_inv_eq_map f y] exact map_mul_le_add _ _ _ @[to_additive] theorem map_div_rev : f (x / y) = f (y / x) := by rw [← inv_div, map_inv_eq_map] @[to_additive] theorem le_map_add_map_div' : f x ≤ f y + f (y / x) := by simpa only [add_comm, map_div_rev, div_mul_cancel] using map_mul_le_add f (x / y) y end GroupSeminormClass example [OrderedAddCommGroup β] : OrderedAddCommMonoid β := inferInstance @[to_additive] theorem abs_sub_map_le_div [Group α] [LinearOrderedAddCommGroup β] [GroupSeminormClass F α β] (f : F) (x y : α) : |f x - f y| ≤ f (x / y) := by rw [abs_sub_le_iff, sub_le_iff_le_add', sub_le_iff_le_add'] exact ⟨le_map_add_map_div _ _ _, le_map_add_map_div' _ _ _⟩ -- See note [lower instance priority] @[to_additive] instance (priority := 100) GroupSeminormClass.toNonnegHomClass [Group α] [LinearOrderedAddCommMonoid β] [GroupSeminormClass F α β] : NonnegHomClass F α β := { ‹GroupSeminormClass F α β› with apply_nonneg := fun f a => (nsmul_nonneg_iff two_ne_zero).1 <| by rw [two_nsmul, ← map_one_eq_zero f, ← div_self' a] exact map_div_le_add _ _ _ } section GroupNormClass variable [Group α] [OrderedAddCommMonoid β] [GroupNormClass F α β] (f : F) {x : α} @[to_additive (attr := simp)] theorem map_eq_zero_iff_eq_one : f x = 0 ↔ x = 1 := ⟨eq_one_of_map_eq_zero _, by rintro rfl exact map_one_eq_zero _⟩ @[to_additive] theorem map_ne_zero_iff_ne_one : f x ≠ 0 ↔ x ≠ 1 := (map_eq_zero_iff_eq_one _).not end GroupNormClass @[to_additive] theorem map_pos_of_ne_one [Group α] [LinearOrderedAddCommMonoid β] [GroupNormClass F α β] (f : F) {x : α} (hx : x ≠ 1) : 0 < f x := (apply_nonneg _ _).lt_of_ne <| ((map_ne_zero_iff_ne_one _).2 hx).symm /-! ### Ring (semi)norms -/ /-- `RingSeminormClass F α` states that `F` is a type of `β`-valued seminorms on the ring `α`. You should extend this class when you extend `RingSeminorm`. -/ class RingSeminormClass (F α β : Type*) [NonUnitalNonAssocRing α] [OrderedSemiring β] [FunLike F α β] extends AddGroupSeminormClass F α β, SubmultiplicativeHomClass F α β : Prop /-- `RingNormClass F α` states that `F` is a type of `β`-valued norms on the ring `α`. You should extend this class when you extend `RingNorm`. -/ class RingNormClass (F α β : Type*) [NonUnitalNonAssocRing α] [OrderedSemiring β] [FunLike F α β] extends RingSeminormClass F α β, AddGroupNormClass F α β : Prop /-- `MulRingSeminormClass F α` states that `F` is a type of `β`-valued multiplicative seminorms on the ring `α`. You should extend this class when you extend `MulRingSeminorm`. -/ class MulRingSeminormClass (F α β : Type*) [NonAssocRing α] [OrderedSemiring β] [FunLike F α β] extends AddGroupSeminormClass F α β, MonoidWithZeroHomClass F α β : Prop -- Lower the priority of these instances since they require synthesizing an order structure. attribute [instance 50] MulRingSeminormClass.toMonoidHomClass MulRingSeminormClass.toMonoidWithZeroHomClass /-- `MulRingNormClass F α` states that `F` is a type of `β`-valued multiplicative norms on the ring `α`. You should extend this class when you extend `MulRingNorm`. -/ class MulRingNormClass (F α β : Type*) [NonAssocRing α] [OrderedSemiring β] [FunLike F α β] extends MulRingSeminormClass F α β, AddGroupNormClass F α β : Prop -- See note [out-param inheritance] -- See note [lower instance priority] instance (priority := 100) RingSeminormClass.toNonnegHomClass [NonUnitalNonAssocRing α] [LinearOrderedSemiring β] [RingSeminormClass F α β] : NonnegHomClass F α β := AddGroupSeminormClass.toNonnegHomClass -- See note [lower instance priority] instance (priority := 100) MulRingSeminormClass.toRingSeminormClass [NonAssocRing α] [OrderedSemiring β] [MulRingSeminormClass F α β] : RingSeminormClass F α β := { ‹MulRingSeminormClass F α β› with map_mul_le_mul := fun f a b => (map_mul _ _ _).le } -- See note [lower instance priority] instance (priority := 100) MulRingNormClass.toRingNormClass [NonAssocRing α] [OrderedSemiring β] [MulRingNormClass F α β] : RingNormClass F α β := { ‹MulRingNormClass F α β›, MulRingSeminormClass.toRingSeminormClass with }
Algebra\Order\Hom\Monoid.lean
/- Copyright (c) 2022 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import Mathlib.Algebra.GroupWithZero.Hom import Mathlib.Algebra.Order.Group.Instances import Mathlib.Algebra.Order.GroupWithZero.Canonical import Mathlib.Order.Hom.Basic /-! # Ordered monoid and group homomorphisms This file defines morphisms between (additive) ordered monoids. ## Types of morphisms * `OrderAddMonoidHom`: Ordered additive monoid homomorphisms. * `OrderMonoidHom`: Ordered monoid homomorphisms. * `OrderMonoidWithZeroHom`: Ordered monoid with zero homomorphisms. ## Notation * `→+o`: Bundled ordered additive monoid homs. Also use for additive groups homs. * `→*o`: Bundled ordered monoid homs. Also use for groups homs. * `→*₀o`: Bundled ordered monoid with zero homs. Also use for groups with zero homs. ## Implementation notes There's a coercion from bundled homs to fun, and the canonical notation is to use the bundled hom as a function via this coercion. There is no `OrderGroupHom` -- the idea is that `OrderMonoidHom` is used. The constructor for `OrderMonoidHom` needs a proof of `map_one` as well as `map_mul`; a separate constructor `OrderMonoidHom.mk'` will construct ordered group homs (i.e. ordered monoid homs between ordered groups) given only a proof that multiplication is preserved, Implicit `{}` brackets are often used instead of type class `[]` brackets. This is done when the instances can be inferred because they are implicit arguments to the type `OrderMonoidHom`. When they can be inferred from the type it is faster to use this method than to use type class inference. ### Removed typeclasses This file used to define typeclasses for order-preserving (additive) monoid homomorphisms: `OrderAddMonoidHomClass`, `OrderMonoidHomClass`, and `OrderMonoidWithZeroHomClass`. In #10544 we migrated from these typeclasses to assumptions like `[FunLike F M N] [MonoidHomClass F M N] [OrderHomClass F M N]`, making some definitions and lemmas irrelevant. ## Tags ordered monoid, ordered group, monoid with zero -/ open Function variable {F α β γ δ : Type*} section AddMonoid /-- `α →+o β` is the type of monotone functions `α → β` that preserve the `OrderedAddCommMonoid` structure. `OrderAddMonoidHom` is also used for ordered group homomorphisms. When possible, instead of parametrizing results over `(f : α →+o β)`, you should parametrize over `(F : Type*) [OrderAddMonoidHomClass F α β] (f : F)`. When you extend this structure, make sure to extend `OrderAddMonoidHomClass`. -/ structure OrderAddMonoidHom (α β : Type*) [Preorder α] [Preorder β] [AddZeroClass α] [AddZeroClass β] extends α →+ β where /-- An `OrderAddMonoidHom` is a monotone function. -/ monotone' : Monotone toFun /-- Infix notation for `OrderAddMonoidHom`. -/ infixr:25 " →+o " => OrderAddMonoidHom -- Instances and lemmas are defined below through `@[to_additive]`. end AddMonoid section Monoid /-- `α →*o β` is the type of functions `α → β` that preserve the `OrderedCommMonoid` structure. `OrderMonoidHom` is also used for ordered group homomorphisms. When possible, instead of parametrizing results over `(f : α →*o β)`, you should parametrize over `(F : Type*) [OrderMonoidHomClass F α β] (f : F)`. When you extend this structure, make sure to extend `OrderMonoidHomClass`. -/ @[to_additive] structure OrderMonoidHom (α β : Type*) [Preorder α] [Preorder β] [MulOneClass α] [MulOneClass β] extends α →* β where /-- An `OrderMonoidHom` is a monotone function. -/ monotone' : Monotone toFun /-- Infix notation for `OrderMonoidHom`. -/ infixr:25 " →*o " => OrderMonoidHom variable [Preorder α] [Preorder β] [MulOneClass α] [MulOneClass β] [FunLike F α β] /-- Turn an element of a type `F` satisfying `OrderHomClass F α β` and `MonoidHomClass F α β` into an actual `OrderMonoidHom`. This is declared as the default coercion from `F` to `α →*o β`. -/ @[to_additive (attr := coe) "Turn an element of a type `F` satisfying `OrderAddMonoidHomClass F α β` into an actual `OrderAddMonoidHom`. This is declared as the default coercion from `F` to `α →+o β`."] def OrderMonoidHomClass.toOrderMonoidHom [OrderHomClass F α β] [MonoidHomClass F α β] (f : F) : α →*o β := { (f : α →* β) with monotone' := OrderHomClass.monotone f } /-- Any type satisfying `OrderMonoidHomClass` can be cast into `OrderMonoidHom` via `OrderMonoidHomClass.toOrderMonoidHom`. -/ @[to_additive "Any type satisfying `OrderAddMonoidHomClass` can be cast into `OrderAddMonoidHom` via `OrderAddMonoidHomClass.toOrderAddMonoidHom`"] instance [OrderHomClass F α β] [MonoidHomClass F α β] : CoeTC F (α →*o β) := ⟨OrderMonoidHomClass.toOrderMonoidHom⟩ end Monoid section MonoidWithZero variable [Preorder α] [Preorder β] [MulZeroOneClass α] [MulZeroOneClass β] /-- `OrderMonoidWithZeroHom α β` is the type of functions `α → β` that preserve the `MonoidWithZero` structure. `OrderMonoidWithZeroHom` is also used for group homomorphisms. When possible, instead of parametrizing results over `(f : α →+ β)`, you should parametrize over `(F : Type*) [OrderMonoidWithZeroHomClass F α β] (f : F)`. When you extend this structure, make sure to extend `OrderMonoidWithZeroHomClass`. -/ structure OrderMonoidWithZeroHom (α β : Type*) [Preorder α] [Preorder β] [MulZeroOneClass α] [MulZeroOneClass β] extends α →*₀ β where /-- An `OrderMonoidWithZeroHom` is a monotone function. -/ monotone' : Monotone toFun /-- Infix notation for `OrderMonoidWithZeroHom`. -/ infixr:25 " →*₀o " => OrderMonoidWithZeroHom section variable [FunLike F α β] /-- Turn an element of a type `F` satisfying `OrderHomClass F α β` and `MonoidWithZeroHomClass F α β` into an actual `OrderMonoidWithZeroHom`. This is declared as the default coercion from `F` to `α →+*₀o β`. -/ @[coe] def OrderMonoidWithZeroHomClass.toOrderMonoidWithZeroHom [OrderHomClass F α β] [MonoidWithZeroHomClass F α β] (f : F) : α →*₀o β := { (f : α →*₀ β) with monotone' := OrderHomClass.monotone f } end variable [FunLike F α β] instance [OrderHomClass F α β] [MonoidWithZeroHomClass F α β] : CoeTC F (α →*₀o β) := ⟨OrderMonoidWithZeroHomClass.toOrderMonoidWithZeroHom⟩ end MonoidWithZero section OrderedZero variable [FunLike F α β] variable [Preorder α] [Zero α] [Preorder β] [Zero β] [OrderHomClass F α β] [ZeroHomClass F α β] (f : F) {a : α} /-- See also `NonnegHomClass.apply_nonneg`. -/ theorem map_nonneg (ha : 0 ≤ a) : 0 ≤ f a := by rw [← map_zero f] exact OrderHomClass.mono _ ha theorem map_nonpos (ha : a ≤ 0) : f a ≤ 0 := by rw [← map_zero f] exact OrderHomClass.mono _ ha end OrderedZero section OrderedAddCommGroup variable [OrderedAddCommGroup α] [OrderedAddCommMonoid β] [i : FunLike F α β] variable (f : F) theorem monotone_iff_map_nonneg [iamhc : AddMonoidHomClass F α β] : Monotone (f : α → β) ↔ ∀ a, 0 ≤ a → 0 ≤ f a := ⟨fun h a => by rw [← map_zero f] apply h, fun h a b hl => by rw [← sub_add_cancel b a, map_add f] exact le_add_of_nonneg_left (h _ <| sub_nonneg.2 hl)⟩ variable [iamhc : AddMonoidHomClass F α β] theorem antitone_iff_map_nonpos : Antitone (f : α → β) ↔ ∀ a, 0 ≤ a → f a ≤ 0 := monotone_toDual_comp_iff.symm.trans <| monotone_iff_map_nonneg (β := βᵒᵈ) (iamhc := iamhc) _ theorem monotone_iff_map_nonpos : Monotone (f : α → β) ↔ ∀ a ≤ 0, f a ≤ 0 := antitone_comp_ofDual_iff.symm.trans <| antitone_iff_map_nonpos (α := αᵒᵈ) (iamhc := iamhc) _ theorem antitone_iff_map_nonneg : Antitone (f : α → β) ↔ ∀ a ≤ 0, 0 ≤ f a := monotone_comp_ofDual_iff.symm.trans <| monotone_iff_map_nonneg (α := αᵒᵈ) (iamhc := iamhc) _ variable [CovariantClass β β (· + ·) (· < ·)] theorem strictMono_iff_map_pos : StrictMono (f : α → β) ↔ ∀ a, 0 < a → 0 < f a := by refine ⟨fun h a => ?_, fun h a b hl => ?_⟩ · rw [← map_zero f] apply h · rw [← sub_add_cancel b a, map_add f] exact lt_add_of_pos_left _ (h _ <| sub_pos.2 hl) theorem strictAnti_iff_map_neg : StrictAnti (f : α → β) ↔ ∀ a, 0 < a → f a < 0 := strictMono_toDual_comp_iff.symm.trans <| strictMono_iff_map_pos (β := βᵒᵈ) (iamhc := iamhc) _ theorem strictMono_iff_map_neg : StrictMono (f : α → β) ↔ ∀ a < 0, f a < 0 := strictAnti_comp_ofDual_iff.symm.trans <| strictAnti_iff_map_neg (α := αᵒᵈ) (iamhc := iamhc) _ theorem strictAnti_iff_map_pos : StrictAnti (f : α → β) ↔ ∀ a < 0, 0 < f a := strictMono_comp_ofDual_iff.symm.trans <| strictMono_iff_map_pos (α := αᵒᵈ) (iamhc := iamhc) _ end OrderedAddCommGroup namespace OrderMonoidHom section Preorder variable [Preorder α] [Preorder β] [Preorder γ] [Preorder δ] [MulOneClass α] [MulOneClass β] [MulOneClass γ] [MulOneClass δ] {f g : α →*o β} @[to_additive] instance : FunLike (α →*o β) α β where coe f := f.toFun coe_injective' f g h := by obtain ⟨⟨⟨_, _⟩⟩, _⟩ := f obtain ⟨⟨⟨_, _⟩⟩, _⟩ := g congr @[to_additive] instance : OrderHomClass (α →*o β) α β where map_rel f _ _ h := f.monotone' h @[to_additive] instance : MonoidHomClass (α →*o β) α β where map_mul f := f.map_mul' map_one f := f.map_one' -- Other lemmas should be accessed through the `FunLike` API @[to_additive (attr := ext)] theorem ext (h : ∀ a, f a = g a) : f = g := DFunLike.ext f g h @[to_additive] theorem toFun_eq_coe (f : α →*o β) : f.toFun = (f : α → β) := rfl @[to_additive (attr := simp)] theorem coe_mk (f : α →* β) (h) : (OrderMonoidHom.mk f h : α → β) = f := rfl @[to_additive (attr := simp)] theorem mk_coe (f : α →*o β) (h) : OrderMonoidHom.mk (f : α →* β) h = f := by ext rfl /-- Reinterpret an ordered monoid homomorphism as an order homomorphism. -/ @[to_additive "Reinterpret an ordered additive monoid homomorphism as an order homomorphism."] def toOrderHom (f : α →*o β) : α →o β := { f with } @[to_additive (attr := simp)] theorem coe_monoidHom (f : α →*o β) : ((f : α →* β) : α → β) = f := rfl @[to_additive (attr := simp)] theorem coe_orderHom (f : α →*o β) : ((f : α →o β) : α → β) = f := rfl @[to_additive] theorem toMonoidHom_injective : Injective (toMonoidHom : _ → α →* β) := fun f g h => ext <| by convert DFunLike.ext_iff.1 h using 0 @[to_additive] theorem toOrderHom_injective : Injective (toOrderHom : _ → α →o β) := fun f g h => ext <| by convert DFunLike.ext_iff.1 h using 0 /-- Copy of an `OrderMonoidHom` with a new `toFun` equal to the old one. Useful to fix definitional equalities. -/ @[to_additive "Copy of an `OrderAddMonoidHom` with a new `toFun` equal to the old one. Useful to fix definitional equalities."] protected def copy (f : α →*o β) (f' : α → β) (h : f' = f) : α →*o β := { f.toMonoidHom.copy f' h with toFun := f', monotone' := h.symm.subst f.monotone' } @[to_additive (attr := simp)] theorem coe_copy (f : α →*o β) (f' : α → β) (h : f' = f) : ⇑(f.copy f' h) = f' := rfl @[to_additive] theorem copy_eq (f : α →*o β) (f' : α → β) (h : f' = f) : f.copy f' h = f := DFunLike.ext' h variable (α) /-- The identity map as an ordered monoid homomorphism. -/ @[to_additive "The identity map as an ordered additive monoid homomorphism."] protected def id : α →*o α := { MonoidHom.id α, OrderHom.id with } @[to_additive (attr := simp)] theorem coe_id : ⇑(OrderMonoidHom.id α) = id := rfl @[to_additive] instance : Inhabited (α →*o α) := ⟨OrderMonoidHom.id α⟩ variable {α} /-- Composition of `OrderMonoidHom`s as an `OrderMonoidHom`. -/ @[to_additive "Composition of `OrderAddMonoidHom`s as an `OrderAddMonoidHom`"] def comp (f : β →*o γ) (g : α →*o β) : α →*o γ := { f.toMonoidHom.comp (g : α →* β), f.toOrderHom.comp (g : α →o β) with } @[to_additive (attr := simp)] theorem coe_comp (f : β →*o γ) (g : α →*o β) : (f.comp g : α → γ) = f ∘ g := rfl @[to_additive (attr := simp)] theorem comp_apply (f : β →*o γ) (g : α →*o β) (a : α) : (f.comp g) a = f (g a) := rfl @[to_additive] theorem coe_comp_monoidHom (f : β →*o γ) (g : α →*o β) : (f.comp g : α →* γ) = (f : β →* γ).comp g := rfl @[to_additive] theorem coe_comp_orderHom (f : β →*o γ) (g : α →*o β) : (f.comp g : α →o γ) = (f : β →o γ).comp g := rfl @[to_additive (attr := simp)] theorem comp_assoc (f : γ →*o δ) (g : β →*o γ) (h : α →*o β) : (f.comp g).comp h = f.comp (g.comp h) := rfl @[to_additive (attr := simp)] theorem comp_id (f : α →*o β) : f.comp (OrderMonoidHom.id α) = f := rfl @[to_additive (attr := simp)] theorem id_comp (f : α →*o β) : (OrderMonoidHom.id β).comp f = f := rfl @[to_additive (attr := simp)] theorem cancel_right {g₁ g₂ : β →*o γ} {f : α →*o β} (hf : Function.Surjective f) : g₁.comp f = g₂.comp f ↔ g₁ = g₂ := ⟨fun h => ext <| hf.forall.2 <| DFunLike.ext_iff.1 h, fun _ => by congr⟩ @[to_additive (attr := simp)] theorem cancel_left {g : β →*o γ} {f₁ f₂ : α →*o β} (hg : Function.Injective g) : g.comp f₁ = g.comp f₂ ↔ f₁ = f₂ := ⟨fun h => ext fun a => hg <| by rw [← comp_apply, h, comp_apply], congr_arg _⟩ /-- `1` is the homomorphism sending all elements to `1`. -/ @[to_additive "`0` is the homomorphism sending all elements to `0`."] instance : One (α →*o β) := ⟨{ (1 : α →* β) with monotone' := monotone_const }⟩ @[to_additive (attr := simp)] theorem coe_one : ⇑(1 : α →*o β) = 1 := rfl @[to_additive (attr := simp)] theorem one_apply (a : α) : (1 : α →*o β) a = 1 := rfl @[to_additive (attr := simp)] theorem one_comp (f : α →*o β) : (1 : β →*o γ).comp f = 1 := rfl @[to_additive (attr := simp)] theorem comp_one (f : β →*o γ) : f.comp (1 : α →*o β) = 1 := ext fun _ => map_one f end Preorder section Mul variable [OrderedCommMonoid α] [OrderedCommMonoid β] [OrderedCommMonoid γ] /-- For two ordered monoid morphisms `f` and `g`, their product is the ordered monoid morphism sending `a` to `f a * g a`. -/ @[to_additive "For two ordered additive monoid morphisms `f` and `g`, their product is the ordered additive monoid morphism sending `a` to `f a + g a`."] instance : Mul (α →*o β) := ⟨fun f g => { (f * g : α →* β) with monotone' := f.monotone'.mul' g.monotone' }⟩ @[to_additive (attr := simp)] theorem coe_mul (f g : α →*o β) : ⇑(f * g) = f * g := rfl @[to_additive (attr := simp)] theorem mul_apply (f g : α →*o β) (a : α) : (f * g) a = f a * g a := rfl @[to_additive] theorem mul_comp (g₁ g₂ : β →*o γ) (f : α →*o β) : (g₁ * g₂).comp f = g₁.comp f * g₂.comp f := rfl @[to_additive] theorem comp_mul (g : β →*o γ) (f₁ f₂ : α →*o β) : g.comp (f₁ * f₂) = g.comp f₁ * g.comp f₂ := ext fun _ => map_mul g _ _ end Mul section OrderedCommMonoid variable {hα : OrderedCommMonoid α} {hβ : OrderedCommMonoid β} @[to_additive (attr := simp)] theorem toMonoidHom_eq_coe (f : α →*o β) : f.toMonoidHom = f := rfl @[to_additive (attr := simp)] theorem toOrderHom_eq_coe (f : α →*o β) : f.toOrderHom = f := rfl end OrderedCommMonoid section OrderedCommGroup variable {hα : OrderedCommGroup α} {hβ : OrderedCommGroup β} /-- Makes an ordered group homomorphism from a proof that the map preserves multiplication. -/ @[to_additive "Makes an ordered additive group homomorphism from a proof that the map preserves addition."] def mk' (f : α → β) (hf : Monotone f) (map_mul : ∀ a b : α, f (a * b) = f a * f b) : α →*o β := { MonoidHom.mk' f map_mul with monotone' := hf } end OrderedCommGroup end OrderMonoidHom namespace OrderMonoidWithZeroHom section Preorder variable [Preorder α] [Preorder β] [Preorder γ] [Preorder δ] [MulZeroOneClass α] [MulZeroOneClass β] [MulZeroOneClass γ] [MulZeroOneClass δ] {f g : α →*₀o β} instance : FunLike (α →*₀o β) α β where coe f := f.toFun coe_injective' f g h := by obtain ⟨⟨⟨_, _⟩⟩, _⟩ := f obtain ⟨⟨⟨_, _⟩⟩, _⟩ := g congr instance : MonoidWithZeroHomClass (α →*₀o β) α β where map_mul f := f.map_mul' map_one f := f.map_one' map_zero f := f.map_zero' instance : OrderHomClass (α →*₀o β) α β where map_rel f _ _ h := f.monotone' h -- Other lemmas should be accessed through the `FunLike` API @[ext] theorem ext (h : ∀ a, f a = g a) : f = g := DFunLike.ext f g h theorem toFun_eq_coe (f : α →*₀o β) : f.toFun = (f : α → β) := rfl @[simp] theorem coe_mk (f : α →*₀ β) (h) : (OrderMonoidWithZeroHom.mk f h : α → β) = f := rfl @[simp] theorem mk_coe (f : α →*₀o β) (h) : OrderMonoidWithZeroHom.mk (f : α →*₀ β) h = f := rfl /-- Reinterpret an ordered monoid with zero homomorphism as an order monoid homomorphism. -/ def toOrderMonoidHom (f : α →*₀o β) : α →*o β := { f with } @[simp] theorem coe_monoidWithZeroHom (f : α →*₀o β) : ⇑(f : α →*₀ β) = f := rfl @[simp] theorem coe_orderMonoidHom (f : α →*₀o β) : ⇑(f : α →*o β) = f := rfl theorem toOrderMonoidHom_injective : Injective (toOrderMonoidHom : _ → α →*o β) := fun f g h => ext <| by convert DFunLike.ext_iff.1 h using 0 theorem toMonoidWithZeroHom_injective : Injective (toMonoidWithZeroHom : _ → α →*₀ β) := fun f g h => ext <| by convert DFunLike.ext_iff.1 h using 0 /-- Copy of an `OrderMonoidWithZeroHom` with a new `toFun` equal to the old one. Useful to fix definitional equalities. -/ protected def copy (f : α →*₀o β) (f' : α → β) (h : f' = f) : α →*o β := { f.toOrderMonoidHom.copy f' h, f.toMonoidWithZeroHom.copy f' h with toFun := f' } @[simp] theorem coe_copy (f : α →*₀o β) (f' : α → β) (h : f' = f) : ⇑(f.copy f' h) = f' := rfl theorem copy_eq (f : α →*₀o β) (f' : α → β) (h : f' = f) : f.copy f' h = f := DFunLike.ext' h variable (α) /-- The identity map as an ordered monoid with zero homomorphism. -/ protected def id : α →*₀o α := { MonoidWithZeroHom.id α, OrderHom.id with } @[simp] theorem coe_id : ⇑(OrderMonoidWithZeroHom.id α) = id := rfl instance : Inhabited (α →*₀o α) := ⟨OrderMonoidWithZeroHom.id α⟩ variable {α} /-- Composition of `OrderMonoidWithZeroHom`s as an `OrderMonoidWithZeroHom`. -/ def comp (f : β →*₀o γ) (g : α →*₀o β) : α →*₀o γ := { f.toMonoidWithZeroHom.comp (g : α →*₀ β), f.toOrderMonoidHom.comp (g : α →*o β) with } @[simp] theorem coe_comp (f : β →*₀o γ) (g : α →*₀o β) : (f.comp g : α → γ) = f ∘ g := rfl @[simp] theorem comp_apply (f : β →*₀o γ) (g : α →*₀o β) (a : α) : (f.comp g) a = f (g a) := rfl theorem coe_comp_monoidWithZeroHom (f : β →*₀o γ) (g : α →*₀o β) : (f.comp g : α →*₀ γ) = (f : β →*₀ γ).comp g := rfl theorem coe_comp_orderMonoidHom (f : β →*₀o γ) (g : α →*₀o β) : (f.comp g : α →*o γ) = (f : β →*o γ).comp g := rfl @[simp] theorem comp_assoc (f : γ →*₀o δ) (g : β →*₀o γ) (h : α →*₀o β) : (f.comp g).comp h = f.comp (g.comp h) := rfl @[simp] theorem comp_id (f : α →*₀o β) : f.comp (OrderMonoidWithZeroHom.id α) = f := rfl @[simp] theorem id_comp (f : α →*₀o β) : (OrderMonoidWithZeroHom.id β).comp f = f := rfl @[simp] theorem cancel_right {g₁ g₂ : β →*₀o γ} {f : α →*₀o β} (hf : Function.Surjective f) : g₁.comp f = g₂.comp f ↔ g₁ = g₂ := ⟨fun h => ext <| hf.forall.2 <| DFunLike.ext_iff.1 h, fun _ => by congr⟩ @[simp] theorem cancel_left {g : β →*₀o γ} {f₁ f₂ : α →*₀o β} (hg : Function.Injective g) : g.comp f₁ = g.comp f₂ ↔ f₁ = f₂ := ⟨fun h => ext fun a => hg <| by rw [← comp_apply, h, comp_apply], congr_arg _⟩ end Preorder section Mul variable [LinearOrderedCommMonoidWithZero α] [LinearOrderedCommMonoidWithZero β] [LinearOrderedCommMonoidWithZero γ] /-- For two ordered monoid morphisms `f` and `g`, their product is the ordered monoid morphism sending `a` to `f a * g a`. -/ instance : Mul (α →*₀o β) := ⟨fun f g => { (f * g : α →*₀ β) with monotone' := f.monotone'.mul' g.monotone' }⟩ @[simp] theorem coe_mul (f g : α →*₀o β) : ⇑(f * g) = f * g := rfl @[simp] theorem mul_apply (f g : α →*₀o β) (a : α) : (f * g) a = f a * g a := rfl theorem mul_comp (g₁ g₂ : β →*₀o γ) (f : α →*₀o β) : (g₁ * g₂).comp f = g₁.comp f * g₂.comp f := rfl theorem comp_mul (g : β →*₀o γ) (f₁ f₂ : α →*₀o β) : g.comp (f₁ * f₂) = g.comp f₁ * g.comp f₂ := ext fun _ => map_mul g _ _ end Mul section LinearOrderedCommMonoidWithZero variable {hα : Preorder α} {hα' : MulZeroOneClass α} {hβ : Preorder β} {hβ' : MulZeroOneClass β} @[simp] theorem toMonoidWithZeroHom_eq_coe (f : α →*₀o β) : f.toMonoidWithZeroHom = f := by rfl @[simp] theorem toOrderMonoidHom_eq_coe (f : α →*₀o β) : f.toOrderMonoidHom = f := rfl end LinearOrderedCommMonoidWithZero end OrderMonoidWithZeroHom /- See module docstring for details. -/
Algebra\Order\Hom\Ring.lean
/- Copyright (c) 2022 Alex J. Best, Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Alex J. Best, Yaël Dillies -/ import Mathlib.Algebra.Order.Hom.Monoid import Mathlib.Algebra.Ring.Equiv /-! # Ordered ring homomorphisms Homomorphisms between ordered (semi)rings that respect the ordering. ## Main definitions * `OrderRingHom` : Monotone semiring homomorphisms. * `OrderRingIso` : Monotone semiring isomorphisms. ## Notation * `→+*o`: Ordered ring homomorphisms. * `≃+*o`: Ordered ring isomorphisms. ## Implementation notes This file used to define typeclasses for order-preserving ring homomorphisms and isomorphisms. In #10544, we migrated from assumptions like `[FunLike F R S] [OrderRingHomClass F R S]` to assumptions like `[FunLike F R S] [OrderHomClass F R S] [RingHomClass F R S]`, making some typeclasses and instances irrelevant. ## Tags ordered ring homomorphism, order homomorphism -/ assert_not_exists FloorRing assert_not_exists Archimedean open Function variable {F α β γ δ : Type*} /-- `OrderRingHom α β` is the type of monotone semiring homomorphisms from `α` to `β`. When possible, instead of parametrizing results over `(f : OrderRingHom α β)`, you should parametrize over `(F : Type*) [OrderRingHomClass F α β] (f : F)`. When you extend this structure, make sure to extend `OrderRingHomClass`. -/ structure OrderRingHom (α β : Type*) [NonAssocSemiring α] [Preorder α] [NonAssocSemiring β] [Preorder β] extends α →+* β where /-- The proposition that the function preserves the order. -/ monotone' : Monotone toFun /-- Reinterpret an ordered ring homomorphism as a ring homomorphism. -/ add_decl_doc OrderRingHom.toRingHom @[inherit_doc] infixl:25 " →+*o " => OrderRingHom /- Porting note: Needed to reorder instance arguments below: `[Mul α] [Add α] [LE α] [Mul β] [Add β] [LE β]` to `[Mul α] [Mul β] [Add α] [Add β] [LE α] [LE β]` otherwise the [refl] attribute on `OrderRingIso.refl` complains. TODO: change back when `refl` attribute is fixed, github issue #2505 -/ /-- `OrderRingHom α β` is the type of order-preserving semiring isomorphisms between `α` and `β`. When possible, instead of parametrizing results over `(f : OrderRingIso α β)`, you should parametrize over `(F : Type*) [OrderRingIsoClass F α β] (f : F)`. When you extend this structure, make sure to extend `OrderRingIsoClass`. -/ structure OrderRingIso (α β : Type*) [Mul α] [Mul β] [Add α] [Add β] [LE α] [LE β] extends α ≃+* β where /-- The proposition that the function preserves the order bijectively. -/ map_le_map_iff' {a b : α} : toFun a ≤ toFun b ↔ a ≤ b @[inherit_doc] infixl:25 " ≃+*o " => OrderRingIso -- See module docstring for details section Hom variable [FunLike F α β] /-- Turn an element of a type `F` satisfying `OrderHomClass F α β` and `RingHomClass F α β` into an actual `OrderRingHom`. This is declared as the default coercion from `F` to `α →+*o β`. -/ @[coe] def OrderRingHomClass.toOrderRingHom [NonAssocSemiring α] [Preorder α] [NonAssocSemiring β] [Preorder β] [OrderHomClass F α β] [RingHomClass F α β] (f : F) : α →+*o β := { (f : α →+* β) with monotone' := OrderHomClass.monotone f} /-- Any type satisfying `OrderRingHomClass` can be cast into `OrderRingHom` via `OrderRingHomClass.toOrderRingHom`. -/ instance [NonAssocSemiring α] [Preorder α] [NonAssocSemiring β] [Preorder β] [OrderHomClass F α β] [RingHomClass F α β] : CoeTC F (α →+*o β) := ⟨OrderRingHomClass.toOrderRingHom⟩ end Hom section Equiv variable [EquivLike F α β] /-- Turn an element of a type `F` satisfying `OrderIsoClass F α β` and `RingEquivClass F α β` into an actual `OrderRingIso`. This is declared as the default coercion from `F` to `α ≃+*o β`. -/ @[coe] def OrderRingIsoClass.toOrderRingIso [Mul α] [Add α] [LE α] [Mul β] [Add β] [LE β] [OrderIsoClass F α β] [RingEquivClass F α β] (f : F) : α ≃+*o β := { (f : α ≃+* β) with map_le_map_iff' := map_le_map_iff f} /-- Any type satisfying `OrderRingIsoClass` can be cast into `OrderRingIso` via `OrderRingIsoClass.toOrderRingIso`. -/ instance [Mul α] [Add α] [LE α] [Mul β] [Add β] [LE β] [OrderIsoClass F α β] [RingEquivClass F α β] : CoeTC F (α ≃+*o β) := ⟨OrderRingIsoClass.toOrderRingIso⟩ end Equiv /-! ### Ordered ring homomorphisms -/ namespace OrderRingHom variable [NonAssocSemiring α] [Preorder α] section Preorder variable [NonAssocSemiring β] [Preorder β] [NonAssocSemiring γ] [Preorder γ] [NonAssocSemiring δ] [Preorder δ] /-- Reinterpret an ordered ring homomorphism as an ordered additive monoid homomorphism. -/ def toOrderAddMonoidHom (f : α →+*o β) : α →+o β := { f with } /-- Reinterpret an ordered ring homomorphism as an order homomorphism. -/ def toOrderMonoidWithZeroHom (f : α →+*o β) : α →*₀o β := { f with } instance : FunLike (α →+*o β) α β where coe f := f.toFun coe_injective' f g h := by obtain ⟨⟨_, _⟩, _⟩ := f; obtain ⟨⟨_, _⟩, _⟩ := g; congr -- Porting note: needed to add the following line exact DFunLike.coe_injective' h instance : OrderHomClass (α →+*o β) α β where map_rel f _ _ h := f.monotone' h instance : RingHomClass (α →+*o β) α β where map_mul f := f.map_mul' map_one f := f.map_one' map_add f := f.map_add' map_zero f := f.map_zero' theorem toFun_eq_coe (f : α →+*o β) : f.toFun = f := rfl @[ext] theorem ext {f g : α →+*o β} (h : ∀ a, f a = g a) : f = g := DFunLike.ext f g h @[simp] theorem toRingHom_eq_coe (f : α →+*o β) : f.toRingHom = f := RingHom.ext fun _ => rfl @[simp] theorem toOrderAddMonoidHom_eq_coe (f : α →+*o β) : f.toOrderAddMonoidHom = f := rfl @[simp] theorem toOrderMonoidWithZeroHom_eq_coe (f : α →+*o β) : f.toOrderMonoidWithZeroHom = f := rfl @[simp] theorem coe_coe_ringHom (f : α →+*o β) : ⇑(f : α →+* β) = f := rfl @[simp] theorem coe_coe_orderAddMonoidHom (f : α →+*o β) : ⇑(f : α →+o β) = f := rfl @[simp] theorem coe_coe_orderMonoidWithZeroHom (f : α →+*o β) : ⇑(f : α →*₀o β) = f := rfl @[norm_cast] theorem coe_ringHom_apply (f : α →+*o β) (a : α) : (f : α →+* β) a = f a := rfl @[norm_cast] theorem coe_orderAddMonoidHom_apply (f : α →+*o β) (a : α) : (f : α →+o β) a = f a := rfl @[norm_cast] theorem coe_orderMonoidWithZeroHom_apply (f : α →+*o β) (a : α) : (f : α →*₀o β) a = f a := rfl /-- Copy of an `OrderRingHom` with a new `toFun` equal to the old one. Useful to fix definitional equalities. -/ protected def copy (f : α →+*o β) (f' : α → β) (h : f' = f) : α →+*o β := { f.toRingHom.copy f' h, f.toOrderAddMonoidHom.copy f' h with } @[simp] theorem coe_copy (f : α →+*o β) (f' : α → β) (h : f' = f) : ⇑(f.copy f' h) = f' := rfl theorem copy_eq (f : α →+*o β) (f' : α → β) (h : f' = f) : f.copy f' h = f := DFunLike.ext' h variable (α) /-- The identity as an ordered ring homomorphism. -/ protected def id : α →+*o α := { RingHom.id _, OrderHom.id with } instance : Inhabited (α →+*o α) := ⟨OrderRingHom.id α⟩ @[simp] theorem coe_id : ⇑(OrderRingHom.id α) = id := rfl variable {α} @[simp] theorem id_apply (a : α) : OrderRingHom.id α a = a := rfl @[simp] theorem coe_ringHom_id : (OrderRingHom.id α : α →+* α) = RingHom.id α := rfl @[simp] theorem coe_orderAddMonoidHom_id : (OrderRingHom.id α : α →+o α) = OrderAddMonoidHom.id α := rfl @[simp] theorem coe_orderMonoidWithZeroHom_id : (OrderRingHom.id α : α →*₀o α) = OrderMonoidWithZeroHom.id α := rfl /-- Composition of two `OrderRingHom`s as an `OrderRingHom`. -/ protected def comp (f : β →+*o γ) (g : α →+*o β) : α →+*o γ := { f.toRingHom.comp g.toRingHom, f.toOrderAddMonoidHom.comp g.toOrderAddMonoidHom with } @[simp] theorem coe_comp (f : β →+*o γ) (g : α →+*o β) : ⇑(f.comp g) = f ∘ g := rfl @[simp] theorem comp_apply (f : β →+*o γ) (g : α →+*o β) (a : α) : f.comp g a = f (g a) := rfl theorem comp_assoc (f : γ →+*o δ) (g : β →+*o γ) (h : α →+*o β) : (f.comp g).comp h = f.comp (g.comp h) := rfl @[simp] theorem comp_id (f : α →+*o β) : f.comp (OrderRingHom.id α) = f := rfl @[simp] theorem id_comp (f : α →+*o β) : (OrderRingHom.id β).comp f = f := rfl @[simp] theorem cancel_right {f₁ f₂ : β →+*o γ} {g : α →+*o β} (hg : Surjective g) : f₁.comp g = f₂.comp g ↔ f₁ = f₂ := ⟨fun h => ext <| hg.forall.2 <| DFunLike.ext_iff.1 h, fun h => by rw [h]⟩ @[simp] theorem cancel_left {f : β →+*o γ} {g₁ g₂ : α →+*o β} (hf : Injective f) : f.comp g₁ = f.comp g₂ ↔ g₁ = g₂ := ⟨fun h => ext fun a => hf <| by rw [← comp_apply, h, comp_apply], congr_arg _⟩ end Preorder variable [NonAssocSemiring β] instance [Preorder β] : Preorder (OrderRingHom α β) := Preorder.lift ((⇑) : _ → α → β) instance [PartialOrder β] : PartialOrder (OrderRingHom α β) := PartialOrder.lift _ DFunLike.coe_injective end OrderRingHom /-! ### Ordered ring isomorphisms -/ namespace OrderRingIso section LE variable [Mul α] [Add α] [LE α] [Mul β] [Add β] [LE β] [Mul γ] [Add γ] [LE γ] [Mul δ] [Add δ] [LE δ] /-- Reinterpret an ordered ring isomorphism as an order isomorphism. -/ -- Porting note: Added @[coe] attribute @[coe] def toOrderIso (f : α ≃+*o β) : α ≃o β := ⟨f.toRingEquiv.toEquiv, f.map_le_map_iff'⟩ instance : EquivLike (α ≃+*o β) α β where coe f := f.toFun inv f := f.invFun coe_injective' f g h₁ h₂ := by obtain ⟨⟨⟨_, _⟩, _⟩, _⟩ := f obtain ⟨⟨⟨_, _⟩, _⟩, _⟩ := g congr left_inv f := f.left_inv right_inv f := f.right_inv instance : OrderIsoClass (α ≃+*o β) α β where map_le_map_iff f _ _ := f.map_le_map_iff' instance : RingEquivClass (α ≃+*o β) α β where map_mul f := f.map_mul' map_add f := f.map_add' theorem toFun_eq_coe (f : α ≃+*o β) : f.toFun = f := rfl @[ext] theorem ext {f g : α ≃+*o β} (h : ∀ a, f a = g a) : f = g := DFunLike.ext f g h @[simp] theorem coe_mk (e : α ≃+* β) (h) : ⇑(⟨e, h⟩ : α ≃+*o β) = e := rfl @[simp] theorem mk_coe (e : α ≃+*o β) (h) : (⟨e, h⟩ : α ≃+*o β) = e := ext fun _ => rfl @[simp] theorem toRingEquiv_eq_coe (f : α ≃+*o β) : f.toRingEquiv = f := RingEquiv.ext fun _ => rfl @[simp] theorem toOrderIso_eq_coe (f : α ≃+*o β) : f.toOrderIso = f := OrderIso.ext rfl @[simp, norm_cast] theorem coe_toRingEquiv (f : α ≃+*o β) : ⇑(f : α ≃+* β) = f := rfl -- Porting note: needed to add DFunLike.coe on the lhs, bad Equiv coercion otherwise @[simp, norm_cast] theorem coe_toOrderIso (f : α ≃+*o β) : DFunLike.coe (f : α ≃o β) = f := rfl variable (α) /-- The identity map as an ordered ring isomorphism. -/ @[refl] protected def refl : α ≃+*o α := ⟨RingEquiv.refl α, Iff.rfl⟩ instance : Inhabited (α ≃+*o α) := ⟨OrderRingIso.refl α⟩ @[simp] theorem refl_apply (x : α) : OrderRingIso.refl α x = x := by rfl @[simp] theorem coe_ringEquiv_refl : (OrderRingIso.refl α : α ≃+* α) = RingEquiv.refl α := rfl @[simp] theorem coe_orderIso_refl : (OrderRingIso.refl α : α ≃o α) = OrderIso.refl α := rfl variable {α} /-- The inverse of an ordered ring isomorphism as an ordered ring isomorphism. -/ @[symm] protected def symm (e : α ≃+*o β) : β ≃+*o α := ⟨e.toRingEquiv.symm, by intro a b erw [← map_le_map_iff e, e.1.apply_symm_apply, e.1.apply_symm_apply]⟩ /-- See Note [custom simps projection] -/ def Simps.symm_apply (e : α ≃+*o β) : β → α := e.symm @[simp] theorem symm_symm (e : α ≃+*o β) : e.symm.symm = e := ext fun _ => rfl /-- Composition of `OrderRingIso`s as an `OrderRingIso`. -/ @[trans] protected def trans (f : α ≃+*o β) (g : β ≃+*o γ) : α ≃+*o γ := ⟨f.toRingEquiv.trans g.toRingEquiv, (map_le_map_iff g).trans (map_le_map_iff f)⟩ /- Porting note: Used to be generated by [simps] on `trans`, but the lhs of this simplifies under simp, so problem with the simpNF linter. Removed [simps] attribute and added aux version below. -/ theorem trans_toRingEquiv (f : α ≃+*o β) (g : β ≃+*o γ) : (OrderRingIso.trans f g).toRingEquiv = RingEquiv.trans f.toRingEquiv g.toRingEquiv := rfl @[simp] theorem trans_toRingEquiv_aux (f : α ≃+*o β) (g : β ≃+*o γ) : RingEquivClass.toRingEquiv (OrderRingIso.trans f g) = RingEquiv.trans f.toRingEquiv g.toRingEquiv := rfl @[simp] theorem trans_apply (f : α ≃+*o β) (g : β ≃+*o γ) (a : α) : f.trans g a = g (f a) := rfl @[simp] theorem self_trans_symm (e : α ≃+*o β) : e.trans e.symm = OrderRingIso.refl α := ext e.left_inv @[simp] theorem symm_trans_self (e : α ≃+*o β) : e.symm.trans e = OrderRingIso.refl β := ext e.right_inv theorem symm_bijective : Bijective (OrderRingIso.symm : (α ≃+*o β) → β ≃+*o α) := Function.bijective_iff_has_inverse.mpr ⟨_, symm_symm, symm_symm⟩ end LE section NonAssocSemiring variable [NonAssocSemiring α] [Preorder α] [NonAssocSemiring β] [Preorder β] [NonAssocSemiring γ] [Preorder γ] /-- Reinterpret an ordered ring isomorphism as an ordered ring homomorphism. -/ def toOrderRingHom (f : α ≃+*o β) : α →+*o β := ⟨f.toRingEquiv.toRingHom, fun _ _ => (map_le_map_iff f).2⟩ @[simp] theorem toOrderRingHom_eq_coe (f : α ≃+*o β) : f.toOrderRingHom = f := rfl @[simp, norm_cast] theorem coe_toOrderRingHom (f : α ≃+*o β) : ⇑(f : α →+*o β) = f := rfl @[simp] theorem coe_toOrderRingHom_refl : (OrderRingIso.refl α : α →+*o α) = OrderRingHom.id α := rfl theorem toOrderRingHom_injective : Injective (toOrderRingHom : α ≃+*o β → α →+*o β) := fun f g h => DFunLike.coe_injective <| by convert DFunLike.ext'_iff.1 h using 0 end NonAssocSemiring end OrderRingIso
Algebra\Order\Interval\Basic.lean
/- Copyright (c) 2022 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import Mathlib.Algebra.Order.BigOperators.Group.Finset import Mathlib.Data.Set.Pointwise.Basic import Mathlib.Order.Interval.Basic /-! # Interval arithmetic This file defines arithmetic operations on intervals and prove their correctness. Note that this is full precision operations. The essentials of float operations can be found in `Data.FP.Basic`. We have not yet integrated these with the rest of the library. -/ open Function Set open scoped Pointwise universe u variable {ι α : Type*} /-! ### One/zero -/ section One section Preorder variable [Preorder α] [One α] @[to_additive] instance : One (NonemptyInterval α) := ⟨NonemptyInterval.pure 1⟩ namespace NonemptyInterval @[to_additive (attr := simp) toProd_zero] theorem toProd_one : (1 : NonemptyInterval α).toProd = 1 := rfl @[to_additive] theorem fst_one : (1 : NonemptyInterval α).fst = 1 := rfl @[to_additive] theorem snd_one : (1 : NonemptyInterval α).snd = 1 := rfl -- Porting note: Originally `@[simp, norm_cast, to_additive]` @[to_additive (attr := push_cast, simp)] theorem coe_one_interval : ((1 : NonemptyInterval α) : Interval α) = 1 := rfl @[to_additive (attr := simp)] theorem pure_one : pure (1 : α) = 1 := rfl end NonemptyInterval namespace Interval @[to_additive (attr := simp)] theorem pure_one : pure (1 : α) = 1 := rfl @[to_additive] lemma one_ne_bot : (1 : Interval α) ≠ ⊥ := pure_ne_bot @[to_additive] lemma bot_ne_one : (⊥ : Interval α) ≠ 1 := bot_ne_pure end Interval end Preorder section PartialOrder variable [PartialOrder α] [One α] namespace NonemptyInterval @[to_additive (attr := simp)] theorem coe_one : ((1 : NonemptyInterval α) : Set α) = 1 := coe_pure _ @[to_additive] theorem one_mem_one : (1 : α) ∈ (1 : NonemptyInterval α) := ⟨le_rfl, le_rfl⟩ end NonemptyInterval namespace Interval @[to_additive (attr := simp)] theorem coe_one : ((1 : Interval α) : Set α) = 1 := Icc_self _ @[to_additive] theorem one_mem_one : (1 : α) ∈ (1 : Interval α) := ⟨le_rfl, le_rfl⟩ end Interval end PartialOrder end One /-! ### Addition/multiplication Note that this multiplication does not apply to `ℚ` or `ℝ`. -/ section Mul variable [Preorder α] [Mul α] [CovariantClass α α (· * ·) (· ≤ ·)] [CovariantClass α α (swap (· * ·)) (· ≤ ·)] @[to_additive] instance : Mul (NonemptyInterval α) := ⟨fun s t => ⟨s.toProd * t.toProd, mul_le_mul' s.fst_le_snd t.fst_le_snd⟩⟩ @[to_additive] instance : Mul (Interval α) := ⟨Option.map₂ (· * ·)⟩ namespace NonemptyInterval variable (s t : NonemptyInterval α) (a b : α) @[to_additive (attr := simp) toProd_add] theorem toProd_mul : (s * t).toProd = s.toProd * t.toProd := rfl @[to_additive] theorem fst_mul : (s * t).fst = s.fst * t.fst := rfl @[to_additive] theorem snd_mul : (s * t).snd = s.snd * t.snd := rfl @[to_additive (attr := simp)] theorem coe_mul_interval : (↑(s * t) : Interval α) = s * t := rfl @[to_additive (attr := simp)] theorem pure_mul_pure : pure a * pure b = pure (a * b) := rfl end NonemptyInterval namespace Interval variable (s t : Interval α) @[to_additive (attr := simp)] theorem bot_mul : ⊥ * t = ⊥ := rfl @[to_additive] theorem mul_bot : s * ⊥ = ⊥ := Option.map₂_none_right _ _ -- Porting note: simp can prove `add_bot` attribute [simp] mul_bot end Interval end Mul /-! ### Powers -/ -- TODO: if `to_additive` gets improved sufficiently, derive this from `hasPow` instance NonemptyInterval.hasNSMul [AddMonoid α] [Preorder α] [CovariantClass α α (· + ·) (· ≤ ·)] [CovariantClass α α (swap (· + ·)) (· ≤ ·)] : SMul ℕ (NonemptyInterval α) := ⟨fun n s => ⟨(n • s.fst, n • s.snd), nsmul_le_nsmul_right s.fst_le_snd _⟩⟩ section Pow variable [Monoid α] [Preorder α] @[to_additive existing] instance NonemptyInterval.hasPow [CovariantClass α α (· * ·) (· ≤ ·)] [CovariantClass α α (swap (· * ·)) (· ≤ ·)] : Pow (NonemptyInterval α) ℕ := ⟨fun s n => ⟨s.toProd ^ n, pow_le_pow_left' s.fst_le_snd _⟩⟩ namespace NonemptyInterval variable [CovariantClass α α (· * ·) (· ≤ ·)] [CovariantClass α α (swap (· * ·)) (· ≤ ·)] variable (s : NonemptyInterval α) (a : α) (n : ℕ) @[to_additive (attr := simp) toProd_nsmul] theorem toProd_pow : (s ^ n).toProd = s.toProd ^ n := rfl @[to_additive] theorem fst_pow : (s ^ n).fst = s.fst ^ n := rfl @[to_additive] theorem snd_pow : (s ^ n).snd = s.snd ^ n := rfl @[to_additive (attr := simp)] theorem pure_pow : pure a ^ n = pure (a ^ n) := rfl end NonemptyInterval end Pow namespace NonemptyInterval @[to_additive] instance commMonoid [OrderedCommMonoid α] : CommMonoid (NonemptyInterval α) := NonemptyInterval.toProd_injective.commMonoid _ toProd_one toProd_mul toProd_pow end NonemptyInterval @[to_additive] instance Interval.mulOneClass [OrderedCommMonoid α] : MulOneClass (Interval α) where mul := (· * ·) one := 1 one_mul s := (Option.map₂_coe_left _ _ _).trans <| by simp_rw [one_mul, ← Function.id_def, Option.map_id, id] mul_one s := (Option.map₂_coe_right _ _ _).trans <| by simp_rw [mul_one, ← Function.id_def, Option.map_id, id] @[to_additive] instance Interval.commMonoid [OrderedCommMonoid α] : CommMonoid (Interval α) := { Interval.mulOneClass with mul_comm := fun _ _ => Option.map₂_comm mul_comm mul_assoc := fun _ _ _ => Option.map₂_assoc mul_assoc } namespace NonemptyInterval @[to_additive] theorem coe_pow_interval [OrderedCommMonoid α] (s : NonemptyInterval α) (n : ℕ) : ↑(s ^ n) = (s : Interval α) ^ n := map_pow (⟨⟨(↑), coe_one_interval⟩, coe_mul_interval⟩ : NonemptyInterval α →* Interval α) _ _ -- Porting note: simp can prove `coe_nsmul_interval` attribute [simp] coe_pow_interval end NonemptyInterval namespace Interval variable [OrderedCommMonoid α] (s : Interval α) {n : ℕ} @[to_additive] theorem bot_pow : ∀ {n : ℕ}, n ≠ 0 → (⊥ : Interval α) ^ n = ⊥ | 0, h => (h rfl).elim | Nat.succ n, _ => mul_bot (⊥ ^ n) end Interval /-! ### Subtraction Subtraction is defined more generally than division so that it applies to `ℕ` (and `OrderedDiv` is not a thing and probably should not become one). -/ section Sub variable [Preorder α] [AddCommSemigroup α] [Sub α] [OrderedSub α] [CovariantClass α α (· + ·) (· ≤ ·)] instance : Sub (NonemptyInterval α) := ⟨fun s t => ⟨(s.fst - t.snd, s.snd - t.fst), tsub_le_tsub s.fst_le_snd t.fst_le_snd⟩⟩ instance : Sub (Interval α) := ⟨Option.map₂ Sub.sub⟩ namespace NonemptyInterval variable (s t : NonemptyInterval α) {a b : α} @[simp] theorem fst_sub : (s - t).fst = s.fst - t.snd := rfl @[simp] theorem snd_sub : (s - t).snd = s.snd - t.fst := rfl @[simp] theorem coe_sub_interval : (↑(s - t) : Interval α) = s - t := rfl theorem sub_mem_sub (ha : a ∈ s) (hb : b ∈ t) : a - b ∈ s - t := ⟨tsub_le_tsub ha.1 hb.2, tsub_le_tsub ha.2 hb.1⟩ @[simp] theorem pure_sub_pure (a b : α) : pure a - pure b = pure (a - b) := rfl end NonemptyInterval namespace Interval variable (s t : Interval α) @[simp] theorem bot_sub : ⊥ - t = ⊥ := rfl @[simp] theorem sub_bot : s - ⊥ = ⊥ := Option.map₂_none_right _ _ end Interval end Sub /-! ### Division in ordered groups Note that this division does not apply to `ℚ` or `ℝ`. -/ section Div variable [Preorder α] [CommGroup α] [CovariantClass α α (· * ·) (· ≤ ·)] @[to_additive existing] instance : Div (NonemptyInterval α) := ⟨fun s t => ⟨(s.fst / t.snd, s.snd / t.fst), div_le_div'' s.fst_le_snd t.fst_le_snd⟩⟩ @[to_additive existing] instance : Div (Interval α) := ⟨Option.map₂ (· / ·)⟩ namespace NonemptyInterval variable (s t : NonemptyInterval α) (a b : α) @[to_additive existing (attr := simp)] theorem fst_div : (s / t).fst = s.fst / t.snd := rfl @[to_additive existing (attr := simp)] theorem snd_div : (s / t).snd = s.snd / t.fst := rfl @[to_additive existing (attr := simp)] theorem coe_div_interval : (↑(s / t) : Interval α) = s / t := rfl @[to_additive existing] theorem div_mem_div (ha : a ∈ s) (hb : b ∈ t) : a / b ∈ s / t := ⟨div_le_div'' ha.1 hb.2, div_le_div'' ha.2 hb.1⟩ @[to_additive existing (attr := simp)] theorem pure_div_pure : pure a / pure b = pure (a / b) := rfl end NonemptyInterval namespace Interval variable (s t : Interval α) @[to_additive existing (attr := simp)] theorem bot_div : ⊥ / t = ⊥ := rfl @[to_additive existing (attr := simp)] theorem div_bot : s / ⊥ = ⊥ := Option.map₂_none_right _ _ end Interval end Div /-! ### Negation/inversion -/ section Inv variable [OrderedCommGroup α] @[to_additive] instance : Inv (NonemptyInterval α) := ⟨fun s => ⟨(s.snd⁻¹, s.fst⁻¹), inv_le_inv' s.fst_le_snd⟩⟩ @[to_additive] instance : Inv (Interval α) := ⟨Option.map Inv.inv⟩ namespace NonemptyInterval variable (s t : NonemptyInterval α) (a : α) @[to_additive (attr := simp)] theorem fst_inv : s⁻¹.fst = s.snd⁻¹ := rfl @[to_additive (attr := simp)] theorem snd_inv : s⁻¹.snd = s.fst⁻¹ := rfl @[to_additive (attr := simp)] theorem coe_inv_interval : (↑(s⁻¹) : Interval α) = (↑s)⁻¹ := rfl @[to_additive] theorem inv_mem_inv (ha : a ∈ s) : a⁻¹ ∈ s⁻¹ := ⟨inv_le_inv' ha.2, inv_le_inv' ha.1⟩ @[to_additive (attr := simp)] theorem inv_pure : (pure a)⁻¹ = pure a⁻¹ := rfl end NonemptyInterval @[to_additive (attr := simp)] theorem Interval.inv_bot : (⊥ : Interval α)⁻¹ = ⊥ := rfl end Inv namespace NonemptyInterval variable [OrderedCommGroup α] {s t : NonemptyInterval α} @[to_additive] protected theorem mul_eq_one_iff : s * t = 1 ↔ ∃ a b, s = pure a ∧ t = pure b ∧ a * b = 1 := by refine ⟨fun h => ?_, ?_⟩ · rw [NonemptyInterval.ext_iff, Prod.ext_iff] at h have := (mul_le_mul_iff_of_ge s.fst_le_snd t.fst_le_snd).1 (h.2.trans h.1.symm).le refine ⟨s.fst, t.fst, ?_, ?_, h.1⟩ <;> apply NonemptyInterval.ext <;> dsimp [pure] · nth_rw 2 [this.1] · nth_rw 2 [this.2] · rintro ⟨b, c, rfl, rfl, h⟩ rw [pure_mul_pure, h, pure_one] instance subtractionCommMonoid {α : Type u} [OrderedAddCommGroup α] : SubtractionCommMonoid (NonemptyInterval α) := { NonemptyInterval.addCommMonoid with neg := Neg.neg sub := Sub.sub sub_eq_add_neg := fun s t => by refine NonemptyInterval.ext (Prod.ext ?_ ?_) <;> exact sub_eq_add_neg _ _ neg_neg := fun s => by apply NonemptyInterval.ext; exact neg_neg _ neg_add_rev := fun s t => by refine NonemptyInterval.ext (Prod.ext ?_ ?_) <;> exact neg_add_rev _ _ neg_eq_of_add := fun s t h => by obtain ⟨a, b, rfl, rfl, hab⟩ := NonemptyInterval.add_eq_zero_iff.1 h rw [neg_pure, neg_eq_of_add_eq_zero_right hab] -- TODO: use a better defeq zsmul := zsmulRec } @[to_additive existing NonemptyInterval.subtractionCommMonoid] instance divisionCommMonoid : DivisionCommMonoid (NonemptyInterval α) := { NonemptyInterval.commMonoid with inv := Inv.inv div := (· / ·) div_eq_mul_inv := fun s t => by refine NonemptyInterval.ext (Prod.ext ?_ ?_) <;> exact div_eq_mul_inv _ _ inv_inv := fun s => by apply NonemptyInterval.ext; exact inv_inv _ mul_inv_rev := fun s t => by refine NonemptyInterval.ext (Prod.ext ?_ ?_) <;> exact mul_inv_rev _ _ inv_eq_of_mul := fun s t h => by obtain ⟨a, b, rfl, rfl, hab⟩ := NonemptyInterval.mul_eq_one_iff.1 h rw [inv_pure, inv_eq_of_mul_eq_one_right hab] } end NonemptyInterval namespace Interval variable [OrderedCommGroup α] {s t : Interval α} @[to_additive] protected theorem mul_eq_one_iff : s * t = 1 ↔ ∃ a b, s = pure a ∧ t = pure b ∧ a * b = 1 := by cases s · simp cases t · simp · simp_rw [← NonemptyInterval.coe_mul_interval, ← NonemptyInterval.coe_one_interval, WithBot.coe_inj, NonemptyInterval.coe_eq_pure] exact NonemptyInterval.mul_eq_one_iff instance subtractionCommMonoid {α : Type u} [OrderedAddCommGroup α] : SubtractionCommMonoid (Interval α) := { Interval.addCommMonoid with neg := Neg.neg sub := Sub.sub sub_eq_add_neg := by rintro (_ | s) (_ | t) <;> first |rfl|exact congr_arg some (sub_eq_add_neg _ _) neg_neg := by rintro (_ | s) <;> first |rfl|exact congr_arg some (neg_neg _) neg_add_rev := by rintro (_ | s) (_ | t) <;> first |rfl|exact congr_arg some (neg_add_rev _ _) neg_eq_of_add := by rintro (_ | s) (_ | t) h <;> first | cases h | exact congr_arg some (neg_eq_of_add_eq_zero_right <| Option.some_injective _ h) -- TODO: use a better defeq zsmul := zsmulRec } @[to_additive existing Interval.subtractionCommMonoid] instance divisionCommMonoid : DivisionCommMonoid (Interval α) := { Interval.commMonoid with inv := Inv.inv div := (· / ·) div_eq_mul_inv := by rintro (_ | s) (_ | t) <;> first |rfl|exact congr_arg some (div_eq_mul_inv _ _) inv_inv := by rintro (_ | s) <;> first |rfl|exact congr_arg some (inv_inv _) mul_inv_rev := by rintro (_ | s) (_ | t) <;> first |rfl|exact congr_arg some (mul_inv_rev _ _) inv_eq_of_mul := by rintro (_ | s) (_ | t) h <;> first | cases h | exact congr_arg some (inv_eq_of_mul_eq_one_right <| Option.some_injective _ h) } end Interval section Length variable [OrderedAddCommGroup α] namespace NonemptyInterval variable (s t : NonemptyInterval α) (a : α) /-- The length of an interval is its first component minus its second component. This measures the accuracy of the approximation by an interval. -/ def length : α := s.snd - s.fst @[simp] theorem length_nonneg : 0 ≤ s.length := sub_nonneg_of_le s.fst_le_snd @[simp] theorem length_pure : (pure a).length = 0 := sub_self _ @[simp] theorem length_zero : (0 : NonemptyInterval α).length = 0 := length_pure _ @[simp] theorem length_neg : (-s).length = s.length := neg_sub_neg _ _ @[simp] theorem length_add : (s + t).length = s.length + t.length := add_sub_add_comm _ _ _ _ @[simp] theorem length_sub : (s - t).length = s.length + t.length := by simp [sub_eq_add_neg] @[simp] theorem length_sum (f : ι → NonemptyInterval α) (s : Finset ι) : (∑ i ∈ s, f i).length = ∑ i ∈ s, (f i).length := map_sum (⟨⟨length, length_zero⟩, length_add⟩ : NonemptyInterval α →+ α) _ _ end NonemptyInterval namespace Interval variable (s t : Interval α) (a : α) /-- The length of an interval is its first component minus its second component. This measures the accuracy of the approximation by an interval. -/ def length : Interval α → α | ⊥ => 0 | (s : NonemptyInterval α) => s.length @[simp] theorem length_nonneg : ∀ s : Interval α, 0 ≤ s.length | ⊥ => le_rfl | (s : NonemptyInterval α) => s.length_nonneg @[simp] theorem length_pure : (pure a).length = 0 := NonemptyInterval.length_pure _ @[simp] theorem length_zero : (0 : Interval α).length = 0 := length_pure _ @[simp] theorem length_neg : ∀ s : Interval α, (-s).length = s.length | ⊥ => rfl | (s : NonemptyInterval α) => s.length_neg theorem length_add_le : ∀ s t : Interval α, (s + t).length ≤ s.length + t.length | ⊥, _ => by simp | _, ⊥ => by simp | (s : NonemptyInterval α), (t : NonemptyInterval α) => (s.length_add t).le theorem length_sub_le : (s - t).length ≤ s.length + t.length := by simpa [sub_eq_add_neg] using length_add_le s (-t) theorem length_sum_le (f : ι → Interval α) (s : Finset ι) : (∑ i ∈ s, f i).length ≤ ∑ i ∈ s, (f i).length := by -- Porting note: Old proof was `:= Finset.le_sum_of_subadditive _ length_zero length_add_le _ _` apply Finset.le_sum_of_subadditive · exact length_zero · exact length_add_le end Interval end Length namespace Mathlib.Meta.Positivity open Lean Meta Qq /-- Extension for the `positivity` tactic: The length of an interval is always nonnegative. -/ @[positivity NonemptyInterval.length _] def evalNonemptyIntervalLength : PositivityExt where eval {u _α} _ _ e := do let ~q(@NonemptyInterval.length _ $inst $a) := e | throwError "not NonemptyInterval.length" assertInstancesCommute return .nonnegative q(NonemptyInterval.length_nonneg $a) /-- Extension for the `positivity` tactic: The length of an interval is always nonnegative. -/ @[positivity Interval.length _] def evalIntervalLength : PositivityExt where eval {u _α} _ _ e := do let ~q(@Interval.length _ $inst $a) := e | throwError "not Interval.length" assumeInstancesCommute return .nonnegative q(Interval.length_nonneg $a) end Mathlib.Meta.Positivity
Algebra\Order\Interval\Finset.lean
/- Copyright (c) 2021 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import Mathlib.Algebra.Order.Interval.Set.Monoid import Mathlib.Order.Interval.Finset.Defs /-! # Algebraic properties of finset intervals This file provides results about the interaction of algebra with `Finset.Ixx`. -/ open Function OrderDual variable {ι α : Type*} namespace Finset variable [OrderedCancelAddCommMonoid α] [ExistsAddOfLE α] [LocallyFiniteOrder α] @[simp] lemma map_add_left_Icc (a b c : α) : (Icc a b).map (addLeftEmbedding c) = Icc (c + a) (c + b) := by rw [← coe_inj, coe_map, coe_Icc, coe_Icc] exact Set.image_const_add_Icc _ _ _ @[simp] lemma map_add_right_Icc (a b c : α) : (Icc a b).map (addRightEmbedding c) = Icc (a + c) (b + c) := by rw [← coe_inj, coe_map, coe_Icc, coe_Icc] exact Set.image_add_const_Icc _ _ _ @[simp] lemma map_add_left_Ico (a b c : α) : (Ico a b).map (addLeftEmbedding c) = Ico (c + a) (c + b) := by rw [← coe_inj, coe_map, coe_Ico, coe_Ico] exact Set.image_const_add_Ico _ _ _ @[simp] lemma map_add_right_Ico (a b c : α) : (Ico a b).map (addRightEmbedding c) = Ico (a + c) (b + c) := by rw [← coe_inj, coe_map, coe_Ico, coe_Ico] exact Set.image_add_const_Ico _ _ _ @[simp] lemma map_add_left_Ioc (a b c : α) : (Ioc a b).map (addLeftEmbedding c) = Ioc (c + a) (c + b) := by rw [← coe_inj, coe_map, coe_Ioc, coe_Ioc] exact Set.image_const_add_Ioc _ _ _ @[simp] lemma map_add_right_Ioc (a b c : α) : (Ioc a b).map (addRightEmbedding c) = Ioc (a + c) (b + c) := by rw [← coe_inj, coe_map, coe_Ioc, coe_Ioc] exact Set.image_add_const_Ioc _ _ _ @[simp] lemma map_add_left_Ioo (a b c : α) : (Ioo a b).map (addLeftEmbedding c) = Ioo (c + a) (c + b) := by rw [← coe_inj, coe_map, coe_Ioo, coe_Ioo] exact Set.image_const_add_Ioo _ _ _ @[simp] lemma map_add_right_Ioo (a b c : α) : (Ioo a b).map (addRightEmbedding c) = Ioo (a + c) (b + c) := by rw [← coe_inj, coe_map, coe_Ioo, coe_Ioo] exact Set.image_add_const_Ioo _ _ _ variable [DecidableEq α] @[simp] lemma image_add_left_Icc (a b c : α) : (Icc a b).image (c + ·) = Icc (c + a) (c + b) := by rw [← map_add_left_Icc, map_eq_image, addLeftEmbedding, Embedding.coeFn_mk] @[simp] lemma image_add_left_Ico (a b c : α) : (Ico a b).image (c + ·) = Ico (c + a) (c + b) := by rw [← map_add_left_Ico, map_eq_image, addLeftEmbedding, Embedding.coeFn_mk] @[simp] lemma image_add_left_Ioc (a b c : α) : (Ioc a b).image (c + ·) = Ioc (c + a) (c + b) := by rw [← map_add_left_Ioc, map_eq_image, addLeftEmbedding, Embedding.coeFn_mk] @[simp] lemma image_add_left_Ioo (a b c : α) : (Ioo a b).image (c + ·) = Ioo (c + a) (c + b) := by rw [← map_add_left_Ioo, map_eq_image, addLeftEmbedding, Embedding.coeFn_mk] @[simp] lemma image_add_right_Icc (a b c : α) : (Icc a b).image (· + c) = Icc (a + c) (b + c) := by rw [← map_add_right_Icc, map_eq_image, addRightEmbedding, Embedding.coeFn_mk] @[simp] lemma image_add_right_Ico (a b c : α) : (Ico a b).image (· + c) = Ico (a + c) (b + c) := by rw [← map_add_right_Ico, map_eq_image, addRightEmbedding, Embedding.coeFn_mk] @[simp] lemma image_add_right_Ioc (a b c : α) : (Ioc a b).image (· + c) = Ioc (a + c) (b + c) := by rw [← map_add_right_Ioc, map_eq_image, addRightEmbedding, Embedding.coeFn_mk] @[simp] lemma image_add_right_Ioo (a b c : α) : (Ioo a b).image (· + c) = Ioo (a + c) (b + c) := by rw [← map_add_right_Ioo, map_eq_image, addRightEmbedding, Embedding.coeFn_mk] end Finset
Algebra\Order\Interval\Multiset.lean
/- Copyright (c) 2021 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import Mathlib.Algebra.Order.Interval.Finset import Mathlib.Order.Interval.Multiset /-! # Algebraic properties of multiset intervals This file provides results about the interaction of algebra with `Multiset.Ixx`. -/ variable {α : Type*} namespace Multiset variable [OrderedCancelAddCommMonoid α] [ExistsAddOfLE α] [LocallyFiniteOrder α] lemma map_add_left_Icc (a b c : α) : (Icc a b).map (c + ·) = Icc (c + a) (c + b) := by classical rw [Icc, Icc, ← Finset.image_add_left_Icc, Finset.image_val, ((Finset.nodup _).map <| add_right_injective c).dedup] lemma map_add_left_Ico (a b c : α) : (Ico a b).map (c + ·) = Ico (c + a) (c + b) := by classical rw [Ico, Ico, ← Finset.image_add_left_Ico, Finset.image_val, ((Finset.nodup _).map <| add_right_injective c).dedup] lemma map_add_left_Ioc (a b c : α) : (Ioc a b).map (c + ·) = Ioc (c + a) (c + b) := by classical rw [Ioc, Ioc, ← Finset.image_add_left_Ioc, Finset.image_val, ((Finset.nodup _).map <| add_right_injective c).dedup] lemma map_add_left_Ioo (a b c : α) : (Ioo a b).map (c + ·) = Ioo (c + a) (c + b) := by classical rw [Ioo, Ioo, ← Finset.image_add_left_Ioo, Finset.image_val, ((Finset.nodup _).map <| add_right_injective c).dedup] lemma map_add_right_Icc (a b c : α) : ((Icc a b).map fun x => x + c) = Icc (a + c) (b + c) := by simp_rw [add_comm _ c] exact map_add_left_Icc _ _ _ lemma map_add_right_Ico (a b c : α) : ((Ico a b).map fun x => x + c) = Ico (a + c) (b + c) := by simp_rw [add_comm _ c] exact map_add_left_Ico _ _ _ lemma map_add_right_Ioc (a b c : α) : ((Ioc a b).map fun x => x + c) = Ioc (a + c) (b + c) := by simp_rw [add_comm _ c] exact map_add_left_Ioc _ _ _ lemma map_add_right_Ioo (a b c : α) : ((Ioo a b).map fun x => x + c) = Ioo (a + c) (b + c) := by simp_rw [add_comm _ c] exact map_add_left_Ioo _ _ _ end Multiset
Algebra\Order\Interval\Set\Group.lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Patrick Massot, Yury Kudryashov, Rémy Degenne -/ import Mathlib.Algebra.Order.Group.Abs import Mathlib.Algebra.Order.Group.Basic import Mathlib.Algebra.Order.Ring.Defs import Mathlib.Data.Int.Cast.Lemmas import Mathlib.Order.Interval.Set.Basic import Mathlib.Logic.Pairwise /-! ### Lemmas about arithmetic operations and intervals. -/ variable {α : Type*} namespace Set section OrderedCommGroup variable [OrderedCommGroup α] {a b c d : α} /-! `inv_mem_Ixx_iff`, `sub_mem_Ixx_iff` -/ @[to_additive] theorem inv_mem_Icc_iff : a⁻¹ ∈ Set.Icc c d ↔ a ∈ Set.Icc d⁻¹ c⁻¹ := and_comm.trans <| and_congr inv_le' le_inv' @[to_additive] theorem inv_mem_Ico_iff : a⁻¹ ∈ Set.Ico c d ↔ a ∈ Set.Ioc d⁻¹ c⁻¹ := and_comm.trans <| and_congr inv_lt' le_inv' @[to_additive] theorem inv_mem_Ioc_iff : a⁻¹ ∈ Set.Ioc c d ↔ a ∈ Set.Ico d⁻¹ c⁻¹ := and_comm.trans <| and_congr inv_le' lt_inv' @[to_additive] theorem inv_mem_Ioo_iff : a⁻¹ ∈ Set.Ioo c d ↔ a ∈ Set.Ioo d⁻¹ c⁻¹ := and_comm.trans <| and_congr inv_lt' lt_inv' end OrderedCommGroup section OrderedAddCommGroup variable [OrderedAddCommGroup α] {a b c d : α} /-! `add_mem_Ixx_iff_left` -/ -- Porting note: instance search needs help `(α := α)` theorem add_mem_Icc_iff_left : a + b ∈ Set.Icc c d ↔ a ∈ Set.Icc (c - b) (d - b) := (and_congr (sub_le_iff_le_add (α := α)) (le_sub_iff_add_le (α := α))).symm theorem add_mem_Ico_iff_left : a + b ∈ Set.Ico c d ↔ a ∈ Set.Ico (c - b) (d - b) := (and_congr (sub_le_iff_le_add (α := α)) (lt_sub_iff_add_lt (α := α))).symm theorem add_mem_Ioc_iff_left : a + b ∈ Set.Ioc c d ↔ a ∈ Set.Ioc (c - b) (d - b) := (and_congr (sub_lt_iff_lt_add (α := α)) (le_sub_iff_add_le (α := α))).symm theorem add_mem_Ioo_iff_left : a + b ∈ Set.Ioo c d ↔ a ∈ Set.Ioo (c - b) (d - b) := (and_congr (sub_lt_iff_lt_add (α := α)) (lt_sub_iff_add_lt (α := α))).symm /-! `add_mem_Ixx_iff_right` -/ theorem add_mem_Icc_iff_right : a + b ∈ Set.Icc c d ↔ b ∈ Set.Icc (c - a) (d - a) := (and_congr sub_le_iff_le_add' le_sub_iff_add_le').symm theorem add_mem_Ico_iff_right : a + b ∈ Set.Ico c d ↔ b ∈ Set.Ico (c - a) (d - a) := (and_congr sub_le_iff_le_add' lt_sub_iff_add_lt').symm theorem add_mem_Ioc_iff_right : a + b ∈ Set.Ioc c d ↔ b ∈ Set.Ioc (c - a) (d - a) := (and_congr sub_lt_iff_lt_add' le_sub_iff_add_le').symm theorem add_mem_Ioo_iff_right : a + b ∈ Set.Ioo c d ↔ b ∈ Set.Ioo (c - a) (d - a) := (and_congr sub_lt_iff_lt_add' lt_sub_iff_add_lt').symm /-! `sub_mem_Ixx_iff_left` -/ theorem sub_mem_Icc_iff_left : a - b ∈ Set.Icc c d ↔ a ∈ Set.Icc (c + b) (d + b) := and_congr le_sub_iff_add_le sub_le_iff_le_add theorem sub_mem_Ico_iff_left : a - b ∈ Set.Ico c d ↔ a ∈ Set.Ico (c + b) (d + b) := and_congr le_sub_iff_add_le sub_lt_iff_lt_add theorem sub_mem_Ioc_iff_left : a - b ∈ Set.Ioc c d ↔ a ∈ Set.Ioc (c + b) (d + b) := and_congr lt_sub_iff_add_lt sub_le_iff_le_add theorem sub_mem_Ioo_iff_left : a - b ∈ Set.Ioo c d ↔ a ∈ Set.Ioo (c + b) (d + b) := and_congr lt_sub_iff_add_lt sub_lt_iff_lt_add /-! `sub_mem_Ixx_iff_right` -/ theorem sub_mem_Icc_iff_right : a - b ∈ Set.Icc c d ↔ b ∈ Set.Icc (a - d) (a - c) := and_comm.trans <| and_congr sub_le_comm le_sub_comm theorem sub_mem_Ico_iff_right : a - b ∈ Set.Ico c d ↔ b ∈ Set.Ioc (a - d) (a - c) := and_comm.trans <| and_congr sub_lt_comm le_sub_comm theorem sub_mem_Ioc_iff_right : a - b ∈ Set.Ioc c d ↔ b ∈ Set.Ico (a - d) (a - c) := and_comm.trans <| and_congr sub_le_comm lt_sub_comm theorem sub_mem_Ioo_iff_right : a - b ∈ Set.Ioo c d ↔ b ∈ Set.Ioo (a - d) (a - c) := and_comm.trans <| and_congr sub_lt_comm lt_sub_comm -- I think that symmetric intervals deserve attention and API: they arise all the time, -- for instance when considering metric balls in `ℝ`. theorem mem_Icc_iff_abs_le {R : Type*} [LinearOrderedAddCommGroup R] {x y z : R} : |x - y| ≤ z ↔ y ∈ Icc (x - z) (x + z) := abs_le.trans <| and_comm.trans <| and_congr sub_le_comm neg_le_sub_iff_le_add end OrderedAddCommGroup section LinearOrderedAddCommGroup variable [LinearOrderedAddCommGroup α] /-- If we remove a smaller interval from a larger, the result is nonempty -/ theorem nonempty_Ico_sdiff {x dx y dy : α} (h : dy < dx) (hx : 0 < dx) : Nonempty ↑(Ico x (x + dx) \ Ico y (y + dy)) := by cases' lt_or_le x y with h' h' · use x simp [*, not_le.2 h'] · use max x (x + dy) simp [*, le_refl] end LinearOrderedAddCommGroup /-! ### Lemmas about disjointness of translates of intervals -/ section PairwiseDisjoint section OrderedCommGroup variable [OrderedCommGroup α] (a b : α) @[to_additive] theorem pairwise_disjoint_Ioc_mul_zpow : Pairwise (Disjoint on fun n : ℤ => Ioc (a * b ^ n) (a * b ^ (n + 1))) := by simp (config := { unfoldPartialApp := true }) only [Function.onFun] simp_rw [Set.disjoint_iff] intro m n hmn x hx apply hmn have hb : 1 < b := by have : a * b ^ m < a * b ^ (m + 1) := hx.1.1.trans_le hx.1.2 rwa [mul_lt_mul_iff_left, ← mul_one (b ^ m), zpow_add_one, mul_lt_mul_iff_left] at this have i1 := hx.1.1.trans_le hx.2.2 have i2 := hx.2.1.trans_le hx.1.2 rw [mul_lt_mul_iff_left, zpow_lt_zpow_iff hb, Int.lt_add_one_iff] at i1 i2 exact le_antisymm i1 i2 @[to_additive] theorem pairwise_disjoint_Ico_mul_zpow : Pairwise (Disjoint on fun n : ℤ => Ico (a * b ^ n) (a * b ^ (n + 1))) := by simp (config := { unfoldPartialApp := true }) only [Function.onFun] simp_rw [Set.disjoint_iff] intro m n hmn x hx apply hmn have hb : 1 < b := by have : a * b ^ m < a * b ^ (m + 1) := hx.1.1.trans_lt hx.1.2 rwa [mul_lt_mul_iff_left, ← mul_one (b ^ m), zpow_add_one, mul_lt_mul_iff_left] at this have i1 := hx.1.1.trans_lt hx.2.2 have i2 := hx.2.1.trans_lt hx.1.2 rw [mul_lt_mul_iff_left, zpow_lt_zpow_iff hb, Int.lt_add_one_iff] at i1 i2 exact le_antisymm i1 i2 @[to_additive] theorem pairwise_disjoint_Ioo_mul_zpow : Pairwise (Disjoint on fun n : ℤ => Ioo (a * b ^ n) (a * b ^ (n + 1))) := fun _ _ hmn => (pairwise_disjoint_Ioc_mul_zpow a b hmn).mono Ioo_subset_Ioc_self Ioo_subset_Ioc_self @[to_additive] theorem pairwise_disjoint_Ioc_zpow : Pairwise (Disjoint on fun n : ℤ => Ioc (b ^ n) (b ^ (n + 1))) := by simpa only [one_mul] using pairwise_disjoint_Ioc_mul_zpow 1 b @[to_additive] theorem pairwise_disjoint_Ico_zpow : Pairwise (Disjoint on fun n : ℤ => Ico (b ^ n) (b ^ (n + 1))) := by simpa only [one_mul] using pairwise_disjoint_Ico_mul_zpow 1 b @[to_additive] theorem pairwise_disjoint_Ioo_zpow : Pairwise (Disjoint on fun n : ℤ => Ioo (b ^ n) (b ^ (n + 1))) := by simpa only [one_mul] using pairwise_disjoint_Ioo_mul_zpow 1 b end OrderedCommGroup section OrderedRing variable [OrderedRing α] (a : α) theorem pairwise_disjoint_Ioc_add_intCast : Pairwise (Disjoint on fun n : ℤ => Ioc (a + n) (a + n + 1)) := by simpa only [zsmul_one, Int.cast_add, Int.cast_one, ← add_assoc] using pairwise_disjoint_Ioc_add_zsmul a (1 : α) @[deprecated (since := "2024-04-17")] alias pairwise_disjoint_Ioc_add_int_cast := pairwise_disjoint_Ioc_add_intCast theorem pairwise_disjoint_Ico_add_intCast : Pairwise (Disjoint on fun n : ℤ => Ico (a + n) (a + n + 1)) := by simpa only [zsmul_one, Int.cast_add, Int.cast_one, ← add_assoc] using pairwise_disjoint_Ico_add_zsmul a (1 : α) @[deprecated (since := "2024-04-17")] alias pairwise_disjoint_Ico_add_int_cast := pairwise_disjoint_Ico_add_intCast theorem pairwise_disjoint_Ioo_add_intCast : Pairwise (Disjoint on fun n : ℤ => Ioo (a + n) (a + n + 1)) := by simpa only [zsmul_one, Int.cast_add, Int.cast_one, ← add_assoc] using pairwise_disjoint_Ioo_add_zsmul a (1 : α) @[deprecated (since := "2024-04-17")] alias pairwise_disjoint_Ioo_add_int_cast := pairwise_disjoint_Ioo_add_intCast variable (α) theorem pairwise_disjoint_Ico_intCast : Pairwise (Disjoint on fun n : ℤ => Ico (n : α) (n + 1)) := by simpa only [zero_add] using pairwise_disjoint_Ico_add_intCast (0 : α) @[deprecated (since := "2024-04-17")] alias pairwise_disjoint_Ico_int_cast := pairwise_disjoint_Ico_intCast theorem pairwise_disjoint_Ioo_intCast : Pairwise (Disjoint on fun n : ℤ => Ioo (n : α) (n + 1)) := by simpa only [zero_add] using pairwise_disjoint_Ioo_add_intCast (0 : α) @[deprecated (since := "2024-04-17")] alias pairwise_disjoint_Ioo_int_cast := pairwise_disjoint_Ioo_intCast theorem pairwise_disjoint_Ioc_intCast : Pairwise (Disjoint on fun n : ℤ => Ioc (n : α) (n + 1)) := by simpa only [zero_add] using pairwise_disjoint_Ioc_add_intCast (0 : α) @[deprecated (since := "2024-04-17")] alias pairwise_disjoint_Ioc_int_cast := pairwise_disjoint_Ioc_intCast end OrderedRing end PairwiseDisjoint end Set
Algebra\Order\Interval\Set\Instances.lean
/- Copyright (c) 2022 Stuart Presnell. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Stuart Presnell, Eric Wieser, Yaël Dillies, Patrick Massot, Scott Morrison -/ import Mathlib.Algebra.Order.Ring.Basic import Mathlib.Algebra.Ring.Regular import Mathlib.Order.Interval.Set.Basic /-! # Algebraic instances for unit intervals For suitably structured underlying type `α`, we exhibit the structure of the unit intervals (`Set.Icc`, `Set.Ioc`, `Set.Ioc`, and `Set.Ioo`) from `0` to `1`. Note: Instances for the interval `Ici 0` are dealt with in `Algebra/Order/Nonneg.lean`. ## Main definitions The strongest typeclass provided on each interval is: * `Set.Icc.cancelCommMonoidWithZero` * `Set.Ico.commSemigroup` * `Set.Ioc.commMonoid` * `Set.Ioo.commSemigroup` ## TODO * algebraic instances for intervals -1 to 1 * algebraic instances for `Ici 1` * algebraic instances for `(Ioo (-1) 1)ᶜ` * provide `distribNeg` instances where applicable * prove versions of `mul_le_{left,right}` for other intervals * prove versions of the lemmas in `Topology/UnitInterval` with `ℝ` generalized to some arbitrary ordered semiring -/ open Set variable {α : Type*} section OrderedSemiring variable [OrderedSemiring α] /-! ### Instances for `↥(Set.Icc 0 1)` -/ namespace Set.Icc instance zero : Zero (Icc (0 : α) 1) where zero := ⟨0, left_mem_Icc.2 zero_le_one⟩ instance one : One (Icc (0 : α) 1) where one := ⟨1, right_mem_Icc.2 zero_le_one⟩ @[simp, norm_cast] theorem coe_zero : ↑(0 : Icc (0 : α) 1) = (0 : α) := rfl @[simp, norm_cast] theorem coe_one : ↑(1 : Icc (0 : α) 1) = (1 : α) := rfl @[simp] theorem mk_zero (h : (0 : α) ∈ Icc (0 : α) 1) : (⟨0, h⟩ : Icc (0 : α) 1) = 0 := rfl @[simp] theorem mk_one (h : (1 : α) ∈ Icc (0 : α) 1) : (⟨1, h⟩ : Icc (0 : α) 1) = 1 := rfl @[simp, norm_cast] theorem coe_eq_zero {x : Icc (0 : α) 1} : (x : α) = 0 ↔ x = 0 := by symm exact Subtype.ext_iff theorem coe_ne_zero {x : Icc (0 : α) 1} : (x : α) ≠ 0 ↔ x ≠ 0 := not_iff_not.mpr coe_eq_zero @[simp, norm_cast] theorem coe_eq_one {x : Icc (0 : α) 1} : (x : α) = 1 ↔ x = 1 := by symm exact Subtype.ext_iff theorem coe_ne_one {x : Icc (0 : α) 1} : (x : α) ≠ 1 ↔ x ≠ 1 := not_iff_not.mpr coe_eq_one theorem coe_nonneg (x : Icc (0 : α) 1) : 0 ≤ (x : α) := x.2.1 theorem coe_le_one (x : Icc (0 : α) 1) : (x : α) ≤ 1 := x.2.2 /-- like `coe_nonneg`, but with the inequality in `Icc (0:α) 1`. -/ theorem nonneg {t : Icc (0 : α) 1} : 0 ≤ t := t.2.1 /-- like `coe_le_one`, but with the inequality in `Icc (0:α) 1`. -/ theorem le_one {t : Icc (0 : α) 1} : t ≤ 1 := t.2.2 instance mul : Mul (Icc (0 : α) 1) where mul p q := ⟨p * q, ⟨mul_nonneg p.2.1 q.2.1, mul_le_one p.2.2 q.2.1 q.2.2⟩⟩ instance pow : Pow (Icc (0 : α) 1) ℕ where pow p n := ⟨p.1 ^ n, ⟨pow_nonneg p.2.1 n, pow_le_one n p.2.1 p.2.2⟩⟩ @[simp, norm_cast] theorem coe_mul (x y : Icc (0 : α) 1) : ↑(x * y) = (x * y : α) := rfl @[simp, norm_cast] theorem coe_pow (x : Icc (0 : α) 1) (n : ℕ) : ↑(x ^ n) = ((x : α) ^ n) := rfl theorem mul_le_left {x y : Icc (0 : α) 1} : x * y ≤ x := (mul_le_mul_of_nonneg_left y.2.2 x.2.1).trans_eq (mul_one _) theorem mul_le_right {x y : Icc (0 : α) 1} : x * y ≤ y := (mul_le_mul_of_nonneg_right x.2.2 y.2.1).trans_eq (one_mul _) instance monoidWithZero : MonoidWithZero (Icc (0 : α) 1) := Subtype.coe_injective.monoidWithZero _ coe_zero coe_one coe_mul coe_pow instance commMonoidWithZero {α : Type*} [OrderedCommSemiring α] : CommMonoidWithZero (Icc (0 : α) 1) := Subtype.coe_injective.commMonoidWithZero _ coe_zero coe_one coe_mul coe_pow instance cancelMonoidWithZero {α : Type*} [OrderedRing α] [NoZeroDivisors α] : CancelMonoidWithZero (Icc (0 : α) 1) := @Function.Injective.cancelMonoidWithZero α _ NoZeroDivisors.toCancelMonoidWithZero _ _ _ _ (fun v => v.val) Subtype.coe_injective coe_zero coe_one coe_mul coe_pow instance cancelCommMonoidWithZero {α : Type*} [OrderedCommRing α] [NoZeroDivisors α] : CancelCommMonoidWithZero (Icc (0 : α) 1) := @Function.Injective.cancelCommMonoidWithZero α _ NoZeroDivisors.toCancelCommMonoidWithZero _ _ _ _ (fun v => v.val) Subtype.coe_injective coe_zero coe_one coe_mul coe_pow variable {β : Type*} [OrderedRing β] theorem one_sub_mem {t : β} (ht : t ∈ Icc (0 : β) 1) : 1 - t ∈ Icc (0 : β) 1 := by rw [mem_Icc] at * exact ⟨sub_nonneg.2 ht.2, (sub_le_self_iff _).2 ht.1⟩ theorem mem_iff_one_sub_mem {t : β} : t ∈ Icc (0 : β) 1 ↔ 1 - t ∈ Icc (0 : β) 1 := ⟨one_sub_mem, fun h => sub_sub_cancel 1 t ▸ one_sub_mem h⟩ theorem one_sub_nonneg (x : Icc (0 : β) 1) : 0 ≤ 1 - (x : β) := by simpa using x.2.2 theorem one_sub_le_one (x : Icc (0 : β) 1) : 1 - (x : β) ≤ 1 := by simpa using x.2.1 end Set.Icc /-! ### Instances for `↥(Set.Ico 0 1)` -/ namespace Set.Ico instance zero [Nontrivial α] : Zero (Ico (0 : α) 1) where zero := ⟨0, left_mem_Ico.2 zero_lt_one⟩ @[simp, norm_cast] theorem coe_zero [Nontrivial α] : ↑(0 : Ico (0 : α) 1) = (0 : α) := rfl @[simp] theorem mk_zero [Nontrivial α] (h : (0 : α) ∈ Ico (0 : α) 1) : (⟨0, h⟩ : Ico (0 : α) 1) = 0 := rfl @[simp, norm_cast] theorem coe_eq_zero [Nontrivial α] {x : Ico (0 : α) 1} : (x : α) = 0 ↔ x = 0 := by symm exact Subtype.ext_iff theorem coe_ne_zero [Nontrivial α] {x : Ico (0 : α) 1} : (x : α) ≠ 0 ↔ x ≠ 0 := not_iff_not.mpr coe_eq_zero theorem coe_nonneg (x : Ico (0 : α) 1) : 0 ≤ (x : α) := x.2.1 theorem coe_lt_one (x : Ico (0 : α) 1) : (x : α) < 1 := x.2.2 /-- like `coe_nonneg`, but with the inequality in `Ico (0:α) 1`. -/ theorem nonneg [Nontrivial α] {t : Ico (0 : α) 1} : 0 ≤ t := t.2.1 instance mul : Mul (Ico (0 : α) 1) where mul p q := ⟨p * q, ⟨mul_nonneg p.2.1 q.2.1, mul_lt_one_of_nonneg_of_lt_one_right p.2.2.le q.2.1 q.2.2⟩⟩ @[simp, norm_cast] theorem coe_mul (x y : Ico (0 : α) 1) : ↑(x * y) = (x * y : α) := rfl instance semigroup : Semigroup (Ico (0 : α) 1) := Subtype.coe_injective.semigroup _ coe_mul instance commSemigroup {α : Type*} [OrderedCommSemiring α] : CommSemigroup (Ico (0 : α) 1) := Subtype.coe_injective.commSemigroup _ coe_mul end Set.Ico end OrderedSemiring variable [StrictOrderedSemiring α] /-! ### Instances for `↥(Set.Ioc 0 1)` -/ namespace Set.Ioc instance one : One (Ioc (0 : α) 1) where one := ⟨1, ⟨zero_lt_one, le_refl 1⟩⟩ @[simp, norm_cast] theorem coe_one : ↑(1 : Ioc (0 : α) 1) = (1 : α) := rfl @[simp] theorem mk_one (h : (1 : α) ∈ Ioc (0 : α) 1) : (⟨1, h⟩ : Ioc (0 : α) 1) = 1 := rfl @[simp, norm_cast] theorem coe_eq_one {x : Ioc (0 : α) 1} : (x : α) = 1 ↔ x = 1 := by symm exact Subtype.ext_iff theorem coe_ne_one {x : Ioc (0 : α) 1} : (x : α) ≠ 1 ↔ x ≠ 1 := not_iff_not.mpr coe_eq_one theorem coe_pos (x : Ioc (0 : α) 1) : 0 < (x : α) := x.2.1 theorem coe_le_one (x : Ioc (0 : α) 1) : (x : α) ≤ 1 := x.2.2 /-- like `coe_le_one`, but with the inequality in `Ioc (0:α) 1`. -/ theorem le_one {t : Ioc (0 : α) 1} : t ≤ 1 := t.2.2 instance mul : Mul (Ioc (0 : α) 1) where mul p q := ⟨p.1 * q.1, ⟨mul_pos p.2.1 q.2.1, mul_le_one p.2.2 (le_of_lt q.2.1) q.2.2⟩⟩ instance pow : Pow (Ioc (0 : α) 1) ℕ where pow p n := ⟨p.1 ^ n, ⟨pow_pos p.2.1 n, pow_le_one n (le_of_lt p.2.1) p.2.2⟩⟩ @[simp, norm_cast] theorem coe_mul (x y : Ioc (0 : α) 1) : ↑(x * y) = (x * y : α) := rfl @[simp, norm_cast] theorem coe_pow (x : Ioc (0 : α) 1) (n : ℕ) : ↑(x ^ n) = ((x : α) ^ n) := rfl instance semigroup : Semigroup (Ioc (0 : α) 1) := Subtype.coe_injective.semigroup _ coe_mul instance monoid : Monoid (Ioc (0 : α) 1) := Subtype.coe_injective.monoid _ coe_one coe_mul coe_pow instance commSemigroup {α : Type*} [StrictOrderedCommSemiring α] : CommSemigroup (Ioc (0 : α) 1) := Subtype.coe_injective.commSemigroup _ coe_mul instance commMonoid {α : Type*} [StrictOrderedCommSemiring α] : CommMonoid (Ioc (0 : α) 1) := Subtype.coe_injective.commMonoid _ coe_one coe_mul coe_pow instance cancelMonoid {α : Type*} [StrictOrderedRing α] [IsDomain α] : CancelMonoid (Ioc (0 : α) 1) := { Set.Ioc.monoid with mul_left_cancel := fun a _ _ h => Subtype.ext <| mul_left_cancel₀ a.prop.1.ne' <| (congr_arg Subtype.val h : _) mul_right_cancel := fun _ b _ h => Subtype.ext <| mul_right_cancel₀ b.prop.1.ne' <| (congr_arg Subtype.val h : _) } instance cancelCommMonoid {α : Type*} [StrictOrderedCommRing α] [IsDomain α] : CancelCommMonoid (Ioc (0 : α) 1) := { Set.Ioc.cancelMonoid, Set.Ioc.commMonoid with } end Set.Ioc /-! ### Instances for `↥(Set.Ioo 0 1)` -/ namespace Set.Ioo theorem pos (x : Ioo (0 : α) 1) : 0 < (x : α) := x.2.1 theorem lt_one (x : Ioo (0 : α) 1) : (x : α) < 1 := x.2.2 instance mul : Mul (Ioo (0 : α) 1) where mul p q := ⟨p.1 * q.1, ⟨mul_pos p.2.1 q.2.1, mul_lt_one_of_nonneg_of_lt_one_right p.2.2.le q.2.1.le q.2.2⟩⟩ @[simp, norm_cast] theorem coe_mul (x y : Ioo (0 : α) 1) : ↑(x * y) = (x * y : α) := rfl instance semigroup : Semigroup (Ioo (0 : α) 1) := Subtype.coe_injective.semigroup _ coe_mul instance commSemigroup {α : Type*} [StrictOrderedCommSemiring α] : CommSemigroup (Ioo (0 : α) 1) := Subtype.coe_injective.commSemigroup _ coe_mul variable {β : Type*} [OrderedRing β] theorem one_sub_mem {t : β} (ht : t ∈ Ioo (0 : β) 1) : 1 - t ∈ Ioo (0 : β) 1 := by rw [mem_Ioo] at * refine ⟨sub_pos.2 ht.2, ?_⟩ exact lt_of_le_of_ne ((sub_le_self_iff 1).2 ht.1.le) (mt sub_eq_self.mp ht.1.ne') theorem mem_iff_one_sub_mem {t : β} : t ∈ Ioo (0 : β) 1 ↔ 1 - t ∈ Ioo (0 : β) 1 := ⟨one_sub_mem, fun h => sub_sub_cancel 1 t ▸ one_sub_mem h⟩ theorem one_minus_pos (x : Ioo (0 : β) 1) : 0 < 1 - (x : β) := by simpa using x.2.2 theorem one_minus_lt_one (x : Ioo (0 : β) 1) : 1 - (x : β) < 1 := by simpa using x.2.1 end Set.Ioo
Algebra\Order\Interval\Set\Monoid.lean
/- Copyright (c) 2020 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury G. Kudryashov, Patrick Massot -/ import Mathlib.Algebra.Group.Basic import Mathlib.Data.Set.Function import Mathlib.Order.Interval.Set.Basic import Mathlib.Algebra.Order.Monoid.Defs import Mathlib.Algebra.Order.Monoid.Unbundled.ExistsOfLE /-! # Images of intervals under `(+ d)` The lemmas in this file state that addition maps intervals bijectively. The typeclass `ExistsAddOfLE` is defined specifically to make them work when combined with `OrderedCancelAddCommMonoid`; the lemmas below therefore apply to all `OrderedAddCommGroup`, but also to `ℕ` and `ℝ≥0`, which are not groups. -/ namespace Set variable {M : Type*} [OrderedCancelAddCommMonoid M] [ExistsAddOfLE M] (a b c d : M) theorem Ici_add_bij : BijOn (· + d) (Ici a) (Ici (a + d)) := by refine ⟨fun x h => add_le_add_right (mem_Ici.mp h) _, (add_left_injective d).injOn, fun _ h => ?_⟩ obtain ⟨c, rfl⟩ := exists_add_of_le (mem_Ici.mp h) rw [mem_Ici, add_right_comm, add_le_add_iff_right] at h exact ⟨a + c, h, by rw [add_right_comm]⟩ theorem Ioi_add_bij : BijOn (· + d) (Ioi a) (Ioi (a + d)) := by refine ⟨fun x h => add_lt_add_right (mem_Ioi.mp h) _, fun _ _ _ _ h => add_right_cancel h, fun _ h => ?_⟩ obtain ⟨c, rfl⟩ := exists_add_of_le (mem_Ioi.mp h).le rw [mem_Ioi, add_right_comm, add_lt_add_iff_right] at h exact ⟨a + c, h, by rw [add_right_comm]⟩ theorem Icc_add_bij : BijOn (· + d) (Icc a b) (Icc (a + d) (b + d)) := by rw [← Ici_inter_Iic, ← Ici_inter_Iic] exact (Ici_add_bij a d).inter_mapsTo (fun x hx => add_le_add_right hx _) fun x hx => le_of_add_le_add_right hx.2 theorem Ioo_add_bij : BijOn (· + d) (Ioo a b) (Ioo (a + d) (b + d)) := by rw [← Ioi_inter_Iio, ← Ioi_inter_Iio] exact (Ioi_add_bij a d).inter_mapsTo (fun x hx => add_lt_add_right hx _) fun x hx => lt_of_add_lt_add_right hx.2 theorem Ioc_add_bij : BijOn (· + d) (Ioc a b) (Ioc (a + d) (b + d)) := by rw [← Ioi_inter_Iic, ← Ioi_inter_Iic] exact (Ioi_add_bij a d).inter_mapsTo (fun x hx => add_le_add_right hx _) fun x hx => le_of_add_le_add_right hx.2 theorem Ico_add_bij : BijOn (· + d) (Ico a b) (Ico (a + d) (b + d)) := by rw [← Ici_inter_Iio, ← Ici_inter_Iio] exact (Ici_add_bij a d).inter_mapsTo (fun x hx => add_lt_add_right hx _) fun x hx => lt_of_add_lt_add_right hx.2 /-! ### Images under `x ↦ x + a` -/ @[simp] theorem image_add_const_Ici : (fun x => x + a) '' Ici b = Ici (b + a) := (Ici_add_bij _ _).image_eq @[simp] theorem image_add_const_Ioi : (fun x => x + a) '' Ioi b = Ioi (b + a) := (Ioi_add_bij _ _).image_eq @[simp] theorem image_add_const_Icc : (fun x => x + a) '' Icc b c = Icc (b + a) (c + a) := (Icc_add_bij _ _ _).image_eq @[simp] theorem image_add_const_Ico : (fun x => x + a) '' Ico b c = Ico (b + a) (c + a) := (Ico_add_bij _ _ _).image_eq @[simp] theorem image_add_const_Ioc : (fun x => x + a) '' Ioc b c = Ioc (b + a) (c + a) := (Ioc_add_bij _ _ _).image_eq @[simp] theorem image_add_const_Ioo : (fun x => x + a) '' Ioo b c = Ioo (b + a) (c + a) := (Ioo_add_bij _ _ _).image_eq /-! ### Images under `x ↦ a + x` -/ @[simp] theorem image_const_add_Ici : (fun x => a + x) '' Ici b = Ici (a + b) := by simp only [add_comm a, image_add_const_Ici] @[simp] theorem image_const_add_Ioi : (fun x => a + x) '' Ioi b = Ioi (a + b) := by simp only [add_comm a, image_add_const_Ioi] @[simp] theorem image_const_add_Icc : (fun x => a + x) '' Icc b c = Icc (a + b) (a + c) := by simp only [add_comm a, image_add_const_Icc] @[simp] theorem image_const_add_Ico : (fun x => a + x) '' Ico b c = Ico (a + b) (a + c) := by simp only [add_comm a, image_add_const_Ico] @[simp] theorem image_const_add_Ioc : (fun x => a + x) '' Ioc b c = Ioc (a + b) (a + c) := by simp only [add_comm a, image_add_const_Ioc] @[simp] theorem image_const_add_Ioo : (fun x => a + x) '' Ioo b c = Ioo (a + b) (a + c) := by simp only [add_comm a, image_add_const_Ioo] end Set
Algebra\Order\Module\Algebra.lean
/- Copyright (c) 2023 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import Mathlib.Algebra.Algebra.Defs import Mathlib.Algebra.Order.Module.Defs /-! # Ordered algebras This file proves properties of algebras where both rings are ordered compatibly. ## TODO `positivity` extension for `algebraMap` -/ variable {α β : Type*} [OrderedCommSemiring α] section OrderedSemiring variable (β) variable [OrderedSemiring β] [Algebra α β] [SMulPosMono α β] {a : α} @[mono] lemma algebraMap_mono : Monotone (algebraMap α β) := fun a₁ a₂ ha ↦ by simpa only [Algebra.algebraMap_eq_smul_one] using smul_le_smul_of_nonneg_right ha zero_le_one /-- A version of `algebraMap_mono` for use by `gcongr` since it currently does not preprocess `Monotone` conclusions. -/ @[gcongr] protected lemma GCongr.algebraMap_le_algebraMap {a₁ a₂ : α} (ha : a₁ ≤ a₂) : algebraMap α β a₁ ≤ algebraMap α β a₂ := algebraMap_mono _ ha lemma algebraMap_nonneg (ha : 0 ≤ a) : 0 ≤ algebraMap α β a := by simpa using algebraMap_mono β ha end OrderedSemiring section StrictOrderedSemiring variable [StrictOrderedSemiring β] [Algebra α β] section SMulPosMono variable [SMulPosMono α β] [SMulPosReflectLE α β] {a₁ a₂ : α} @[simp] lemma algebraMap_le_algebraMap : algebraMap α β a₁ ≤ algebraMap α β a₂ ↔ a₁ ≤ a₂ := by simp [Algebra.algebraMap_eq_smul_one] end SMulPosMono section SMulPosStrictMono variable [SMulPosStrictMono α β] {a a₁ a₂ : α} variable (β) @[mono] lemma algebraMap_strictMono : StrictMono (algebraMap α β) := fun a₁ a₂ ha ↦ by simpa only [Algebra.algebraMap_eq_smul_one] using smul_lt_smul_of_pos_right ha zero_lt_one /-- A version of `algebraMap_strictMono` for use by `gcongr` since it currently does not preprocess `Monotone` conclusions. -/ @[gcongr] protected lemma GCongr.algebraMap_lt_algebraMap {a₁ a₂ : α} (ha : a₁ < a₂) : algebraMap α β a₁ < algebraMap α β a₂ := algebraMap_strictMono _ ha lemma algebraMap_pos (ha : 0 < a) : 0 < algebraMap α β a := by simpa using algebraMap_strictMono β ha variable {β} variable [SMulPosReflectLT α β] @[simp] lemma algebraMap_lt_algebraMap : algebraMap α β a₁ < algebraMap α β a₂ ↔ a₁ < a₂ := by simp [Algebra.algebraMap_eq_smul_one] end SMulPosStrictMono end StrictOrderedSemiring namespace Mathlib.Meta.Positivity open Lean Meta Qq Function /-- Extension for `algebraMap`. -/ @[positivity algebraMap _ _ _] def evalAlgebraMap : PositivityExt where eval {u β} _zβ _pβ e := do let ~q(@algebraMap $α _ $instα $instβ $instαβ $a) := e | throwError "not `algebraMap`" let pα ← synthInstanceQ (q(PartialOrder $α) : Q(Type u_1)) match ← core q(inferInstance) pα a with | .positive pa => let _instαring ← synthInstanceQ q(OrderedCommSemiring $α) try let _instβring ← synthInstanceQ q(StrictOrderedSemiring $β) let _instαβsmul ← synthInstanceQ q(SMulPosStrictMono $α $β) assertInstancesCommute return .positive q(algebraMap_pos $β $pa) catch _ => let _instβring ← synthInstanceQ q(OrderedSemiring $β) let _instαβsmul ← synthInstanceQ q(SMulPosMono $α $β) assertInstancesCommute return .nonnegative q(algebraMap_nonneg $β $ le_of_lt $pa) | .nonnegative pa => let _instαring ← synthInstanceQ q(OrderedCommSemiring $α) let _instβring ← synthInstanceQ q(OrderedSemiring $β) let _instαβsmul ← synthInstanceQ q(SMulPosMono $α $β) assertInstancesCommute return .nonnegative q(algebraMap_nonneg $β $pa) | _ => pure .none example [OrderedSemiring β] [Algebra α β] [SMulPosMono α β] {a : α} (ha : 0 ≤ a) : 0 ≤ algebraMap α β a := by positivity example [OrderedSemiring β] [Algebra α β] [SMulPosMono α β] {a : α} (ha : 0 < a) : 0 ≤ algebraMap α β a := by positivity example [StrictOrderedSemiring β] [Algebra α β] [SMulPosStrictMono α β] {a : α} (ha : 0 < a) : 0 < algebraMap α β a := by positivity end Mathlib.Meta.Positivity
Algebra\Order\Module\Defs.lean
/- Copyright (c) 2023 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Algebra.Order.GroupWithZero.Unbundled import Mathlib.Algebra.Order.Module.Synonym import Mathlib.GroupTheory.GroupAction.Group import Mathlib.Tactic.Positivity.Core import Mathlib.Algebra.Order.Field.Unbundled.Basic /-! # Monotonicity of scalar multiplication by positive elements This file defines typeclasses to reason about monotonicity of the operations * `b ↦ a • b`, "left scalar multiplication" * `a ↦ a • b`, "right scalar multiplication" We use eight typeclasses to encode the various properties we care about for those two operations. These typeclasses are meant to be mostly internal to this file, to set up each lemma in the appropriate generality. Less granular typeclasses like `OrderedAddCommMonoid`, `LinearOrderedField`, `OrderedSMul` should be enough for most purposes, and the system is set up so that they imply the correct granular typeclasses here. If those are enough for you, you may stop reading here! Else, beware that what follows is a bit technical. ## Definitions In all that follows, `α` and `β` are orders which have a `0` and such that `α` acts on `β` by scalar multiplication. Note however that we do not use lawfulness of this action in most of the file. Hence `•` should be considered here as a mostly arbitrary function `α → β → β`. We use the following four typeclasses to reason about left scalar multiplication (`b ↦ a • b`): * `PosSMulMono`: If `a ≥ 0`, then `b₁ ≤ b₂` implies `a • b₁ ≤ a • b₂`. * `PosSMulStrictMono`: If `a > 0`, then `b₁ < b₂` implies `a • b₁ < a • b₂`. * `PosSMulReflectLT`: If `a ≥ 0`, then `a • b₁ < a • b₂` implies `b₁ < b₂`. * `PosSMulReflectLE`: If `a > 0`, then `a • b₁ ≤ a • b₂` implies `b₁ ≤ b₂`. We use the following four typeclasses to reason about right scalar multiplication (`a ↦ a • b`): * `SMulPosMono`: If `b ≥ 0`, then `a₁ ≤ a₂` implies `a₁ • b ≤ a₂ • b`. * `SMulPosStrictMono`: If `b > 0`, then `a₁ < a₂` implies `a₁ • b < a₂ • b`. * `SMulPosReflectLT`: If `b ≥ 0`, then `a₁ • b < a₂ • b` implies `a₁ < a₂`. * `SMulPosReflectLE`: If `b > 0`, then `a₁ • b ≤ a₂ • b` implies `a₁ ≤ a₂`. ## Constructors The four typeclasses about nonnegativity can usually be checked only on positive inputs due to their condition becoming trivial when `a = 0` or `b = 0`. We therefore make the following constructors available: `PosSMulMono.of_pos`, `PosSMulReflectLT.of_pos`, `SMulPosMono.of_pos`, `SMulPosReflectLT.of_pos` ## Implications As `α` and `β` get more and more structure, those typeclasses end up being equivalent. The commonly used implications are: * When `α`, `β` are partial orders: * `PosSMulStrictMono → PosSMulMono` * `SMulPosStrictMono → SMulPosMono` * `PosSMulReflectLE → PosSMulReflectLT` * `SMulPosReflectLE → SMulPosReflectLT` * When `β` is a linear order: * `PosSMulStrictMono → PosSMulReflectLE` * `PosSMulReflectLT → PosSMulMono` (not registered as instance) * `SMulPosReflectLT → SMulPosMono` (not registered as instance) * `PosSMulReflectLE → PosSMulStrictMono` (not registered as instance) * `SMulPosReflectLE → SMulPosStrictMono` (not registered as instance) * When `α` is a linear order: * `SMulPosStrictMono → SMulPosReflectLE` * When `α` is an ordered ring, `β` an ordered group and also an `α`-module: * `PosSMulMono → SMulPosMono` * `PosSMulStrictMono → SMulPosStrictMono` * When `α` is an linear ordered semifield, `β` is an `α`-module: * `PosSMulStrictMono → PosSMulReflectLT` * `PosSMulMono → PosSMulReflectLE` * When `α` is a semiring, `β` is an `α`-module with `NoZeroSMulDivisors`: * `PosSMulMono → PosSMulStrictMono` (not registered as instance) * When `α` is a ring, `β` is an `α`-module with `NoZeroSMulDivisors`: * `SMulPosMono → SMulPosStrictMono` (not registered as instance) Further, the bundled non-granular typeclasses imply the granular ones like so: * `OrderedSMul → PosSMulStrictMono` * `OrderedSMul → PosSMulReflectLT` Unless otherwise stated, all these implications are registered as instances, which means that in practice you should not worry about these implications. However, if you encounter a case where you think a statement is true but not covered by the current implications, please bring it up on Zulip! ## Implementation notes This file uses custom typeclasses instead of abbreviations of `CovariantClass`/`ContravariantClass` because: * They get displayed as classes in the docs. In particular, one can see their list of instances, instead of their instances being invariably dumped to the `CovariantClass`/`ContravariantClass` list. * They don't pollute other typeclass searches. Having many abbreviations of the same typeclass for different purposes always felt like a performance issue (more instances with the same key, for no added benefit), and indeed making the classes here abbreviation previous creates timeouts due to the higher number of `CovariantClass`/`ContravariantClass` instances. * `SMulPosReflectLT`/`SMulPosReflectLE` do not fit in the framework since they relate `≤` on two different types. So we would have to generalise `CovariantClass`/`ContravariantClass` to three types and two relations. * Very minor, but the constructors let you work with `a : α`, `h : 0 ≤ a` instead of `a : {a : α // 0 ≤ a}`. This actually makes some instances surprisingly cleaner to prove. * The `CovariantClass`/`ContravariantClass` framework is only useful to automate very simple logic anyway. It is easily copied over. In the future, it would be good to make the corresponding typeclasses in `Mathlib.Algebra.Order.GroupWithZero.Unbundled` custom typeclasses too. ## TODO This file acts as a substitute for `Mathlib.Algebra.Order.SMul`. We now need to * finish the transition by deleting the duplicate lemmas * rearrange the non-duplicate lemmas into new files * generalise (most of) the lemmas from `Mathlib.Algebra.Order.Module` to here * rethink `OrderedSMul` -/ open OrderDual variable (α β : Type*) section Defs variable [SMul α β] [Preorder α] [Preorder β] section Left variable [Zero α] /-- Typeclass for monotonicity of scalar multiplication by nonnegative elements on the left, namely `b₁ ≤ b₂ → a • b₁ ≤ a • b₂` if `0 ≤ a`. You should usually not use this very granular typeclass directly, but rather a typeclass like `OrderedSMul`. -/ class PosSMulMono : Prop where /-- Do not use this. Use `smul_le_smul_of_nonneg_left` instead. -/ protected elim ⦃a : α⦄ (ha : 0 ≤ a) ⦃b₁ b₂ : β⦄ (hb : b₁ ≤ b₂) : a • b₁ ≤ a • b₂ /-- Typeclass for strict monotonicity of scalar multiplication by positive elements on the left, namely `b₁ < b₂ → a • b₁ < a • b₂` if `0 < a`. You should usually not use this very granular typeclass directly, but rather a typeclass like `OrderedSMul`. -/ class PosSMulStrictMono : Prop where /-- Do not use this. Use `smul_lt_smul_of_pos_left` instead. -/ protected elim ⦃a : α⦄ (ha : 0 < a) ⦃b₁ b₂ : β⦄ (hb : b₁ < b₂) : a • b₁ < a • b₂ /-- Typeclass for strict reverse monotonicity of scalar multiplication by nonnegative elements on the left, namely `a • b₁ < a • b₂ → b₁ < b₂` if `0 ≤ a`. You should usually not use this very granular typeclass directly, but rather a typeclass like `OrderedSMul`. -/ class PosSMulReflectLT : Prop where /-- Do not use this. Use `lt_of_smul_lt_smul_left` instead. -/ protected elim ⦃a : α⦄ (ha : 0 ≤ a) ⦃b₁ b₂ : β⦄ (hb : a • b₁ < a • b₂) : b₁ < b₂ /-- Typeclass for reverse monotonicity of scalar multiplication by positive elements on the left, namely `a • b₁ ≤ a • b₂ → b₁ ≤ b₂` if `0 < a`. You should usually not use this very granular typeclass directly, but rather a typeclass like `OrderedSMul`. -/ class PosSMulReflectLE : Prop where /-- Do not use this. Use `le_of_smul_lt_smul_left` instead. -/ protected elim ⦃a : α⦄ (ha : 0 < a) ⦃b₁ b₂ : β⦄ (hb : a • b₁ ≤ a • b₂) : b₁ ≤ b₂ end Left section Right variable [Zero β] /-- Typeclass for monotonicity of scalar multiplication by nonnegative elements on the left, namely `a₁ ≤ a₂ → a₁ • b ≤ a₂ • b` if `0 ≤ b`. You should usually not use this very granular typeclass directly, but rather a typeclass like `OrderedSMul`. -/ class SMulPosMono : Prop where /-- Do not use this. Use `smul_le_smul_of_nonneg_right` instead. -/ protected elim ⦃b : β⦄ (hb : 0 ≤ b) ⦃a₁ a₂ : α⦄ (ha : a₁ ≤ a₂) : a₁ • b ≤ a₂ • b /-- Typeclass for strict monotonicity of scalar multiplication by positive elements on the left, namely `a₁ < a₂ → a₁ • b < a₂ • b` if `0 < b`. You should usually not use this very granular typeclass directly, but rather a typeclass like `OrderedSMul`. -/ class SMulPosStrictMono : Prop where /-- Do not use this. Use `smul_lt_smul_of_pos_right` instead. -/ protected elim ⦃b : β⦄ (hb : 0 < b) ⦃a₁ a₂ : α⦄ (ha : a₁ < a₂) : a₁ • b < a₂ • b /-- Typeclass for strict reverse monotonicity of scalar multiplication by nonnegative elements on the left, namely `a₁ • b < a₂ • b → a₁ < a₂` if `0 ≤ b`. You should usually not use this very granular typeclass directly, but rather a typeclass like `OrderedSMul`. -/ class SMulPosReflectLT : Prop where /-- Do not use this. Use `lt_of_smul_lt_smul_right` instead. -/ protected elim ⦃b : β⦄ (hb : 0 ≤ b) ⦃a₁ a₂ : α⦄ (hb : a₁ • b < a₂ • b) : a₁ < a₂ /-- Typeclass for reverse monotonicity of scalar multiplication by positive elements on the left, namely `a₁ • b ≤ a₂ • b → a₁ ≤ a₂` if `0 < b`. You should usually not use this very granular typeclass directly, but rather a typeclass like `OrderedSMul`. -/ class SMulPosReflectLE : Prop where /-- Do not use this. Use `le_of_smul_lt_smul_right` instead. -/ protected elim ⦃b : β⦄ (hb : 0 < b) ⦃a₁ a₂ : α⦄ (hb : a₁ • b ≤ a₂ • b) : a₁ ≤ a₂ end Right end Defs variable {α β} {a a₁ a₂ : α} {b b₁ b₂ : β} section Mul variable [Zero α] [Mul α] [Preorder α] -- See note [lower instance priority] instance (priority := 100) PosMulMono.toPosSMulMono [PosMulMono α] : PosSMulMono α α where elim _a ha _b₁ _b₂ hb := mul_le_mul_of_nonneg_left hb ha -- See note [lower instance priority] instance (priority := 100) PosMulStrictMono.toPosSMulStrictMono [PosMulStrictMono α] : PosSMulStrictMono α α where elim _a ha _b₁ _b₂ hb := mul_lt_mul_of_pos_left hb ha -- See note [lower instance priority] instance (priority := 100) PosMulReflectLT.toPosSMulReflectLT [PosMulReflectLT α] : PosSMulReflectLT α α where elim _a ha _b₁ _b₂ h := lt_of_mul_lt_mul_left h ha -- See note [lower instance priority] instance (priority := 100) PosMulReflectLE.toPosSMulReflectLE [PosMulReflectLE α] : PosSMulReflectLE α α where elim _a ha _b₁ _b₂ h := le_of_mul_le_mul_left h ha -- See note [lower instance priority] instance (priority := 100) MulPosMono.toSMulPosMono [MulPosMono α] : SMulPosMono α α where elim _b hb _a₁ _a₂ ha := mul_le_mul_of_nonneg_right ha hb -- See note [lower instance priority] instance (priority := 100) MulPosStrictMono.toSMulPosStrictMono [MulPosStrictMono α] : SMulPosStrictMono α α where elim _b hb _a₁ _a₂ ha := mul_lt_mul_of_pos_right ha hb -- See note [lower instance priority] instance (priority := 100) MulPosReflectLT.toSMulPosReflectLT [MulPosReflectLT α] : SMulPosReflectLT α α where elim _b hb _a₁ _a₂ h := lt_of_mul_lt_mul_right h hb -- See note [lower instance priority] instance (priority := 100) MulPosReflectLE.toSMulPosReflectLE [MulPosReflectLE α] : SMulPosReflectLE α α where elim _b hb _a₁ _a₂ h := le_of_mul_le_mul_right h hb end Mul section SMul variable [SMul α β] section Preorder variable [Preorder α] [Preorder β] section Left variable [Zero α] lemma monotone_smul_left_of_nonneg [PosSMulMono α β] (ha : 0 ≤ a) : Monotone ((a • ·) : β → β) := PosSMulMono.elim ha lemma strictMono_smul_left_of_pos [PosSMulStrictMono α β] (ha : 0 < a) : StrictMono ((a • ·) : β → β) := PosSMulStrictMono.elim ha @[gcongr] lemma smul_le_smul_of_nonneg_left [PosSMulMono α β] (hb : b₁ ≤ b₂) (ha : 0 ≤ a) : a • b₁ ≤ a • b₂ := monotone_smul_left_of_nonneg ha hb @[gcongr] lemma smul_lt_smul_of_pos_left [PosSMulStrictMono α β] (hb : b₁ < b₂) (ha : 0 < a) : a • b₁ < a • b₂ := strictMono_smul_left_of_pos ha hb lemma lt_of_smul_lt_smul_left [PosSMulReflectLT α β] (h : a • b₁ < a • b₂) (ha : 0 ≤ a) : b₁ < b₂ := PosSMulReflectLT.elim ha h lemma le_of_smul_le_smul_left [PosSMulReflectLE α β] (h : a • b₁ ≤ a • b₂) (ha : 0 < a) : b₁ ≤ b₂ := PosSMulReflectLE.elim ha h alias lt_of_smul_lt_smul_of_nonneg_left := lt_of_smul_lt_smul_left alias le_of_smul_le_smul_of_pos_left := le_of_smul_le_smul_left @[simp] lemma smul_le_smul_iff_of_pos_left [PosSMulMono α β] [PosSMulReflectLE α β] (ha : 0 < a) : a • b₁ ≤ a • b₂ ↔ b₁ ≤ b₂ := ⟨fun h ↦ le_of_smul_le_smul_left h ha, fun h ↦ smul_le_smul_of_nonneg_left h ha.le⟩ @[simp] lemma smul_lt_smul_iff_of_pos_left [PosSMulStrictMono α β] [PosSMulReflectLT α β] (ha : 0 < a) : a • b₁ < a • b₂ ↔ b₁ < b₂ := ⟨fun h ↦ lt_of_smul_lt_smul_left h ha.le, fun hb ↦ smul_lt_smul_of_pos_left hb ha⟩ end Left section Right variable [Zero β] lemma monotone_smul_right_of_nonneg [SMulPosMono α β] (hb : 0 ≤ b) : Monotone ((· • b) : α → β) := SMulPosMono.elim hb lemma strictMono_smul_right_of_pos [SMulPosStrictMono α β] (hb : 0 < b) : StrictMono ((· • b) : α → β) := SMulPosStrictMono.elim hb @[gcongr] lemma smul_le_smul_of_nonneg_right [SMulPosMono α β] (ha : a₁ ≤ a₂) (hb : 0 ≤ b) : a₁ • b ≤ a₂ • b := monotone_smul_right_of_nonneg hb ha @[gcongr] lemma smul_lt_smul_of_pos_right [SMulPosStrictMono α β] (ha : a₁ < a₂) (hb : 0 < b) : a₁ • b < a₂ • b := strictMono_smul_right_of_pos hb ha lemma lt_of_smul_lt_smul_right [SMulPosReflectLT α β] (h : a₁ • b < a₂ • b) (hb : 0 ≤ b) : a₁ < a₂ := SMulPosReflectLT.elim hb h lemma le_of_smul_le_smul_right [SMulPosReflectLE α β] (h : a₁ • b ≤ a₂ • b) (hb : 0 < b) : a₁ ≤ a₂ := SMulPosReflectLE.elim hb h alias lt_of_smul_lt_smul_of_nonneg_right := lt_of_smul_lt_smul_right alias le_of_smul_le_smul_of_pos_right := le_of_smul_le_smul_right @[simp] lemma smul_le_smul_iff_of_pos_right [SMulPosMono α β] [SMulPosReflectLE α β] (hb : 0 < b) : a₁ • b ≤ a₂ • b ↔ a₁ ≤ a₂ := ⟨fun h ↦ le_of_smul_le_smul_right h hb, fun ha ↦ smul_le_smul_of_nonneg_right ha hb.le⟩ @[simp] lemma smul_lt_smul_iff_of_pos_right [SMulPosStrictMono α β] [SMulPosReflectLT α β] (hb : 0 < b) : a₁ • b < a₂ • b ↔ a₁ < a₂ := ⟨fun h ↦ lt_of_smul_lt_smul_right h hb.le, fun ha ↦ smul_lt_smul_of_pos_right ha hb⟩ end Right section LeftRight variable [Zero α] [Zero β] lemma smul_lt_smul_of_le_of_lt [PosSMulStrictMono α β] [SMulPosMono α β] (ha : a₁ ≤ a₂) (hb : b₁ < b₂) (h₁ : 0 < a₁) (h₂ : 0 ≤ b₂) : a₁ • b₁ < a₂ • b₂ := (smul_lt_smul_of_pos_left hb h₁).trans_le (smul_le_smul_of_nonneg_right ha h₂) lemma smul_lt_smul_of_le_of_lt' [PosSMulStrictMono α β] [SMulPosMono α β] (ha : a₁ ≤ a₂) (hb : b₁ < b₂) (h₂ : 0 < a₂) (h₁ : 0 ≤ b₁) : a₁ • b₁ < a₂ • b₂ := (smul_le_smul_of_nonneg_right ha h₁).trans_lt (smul_lt_smul_of_pos_left hb h₂) lemma smul_lt_smul_of_lt_of_le [PosSMulMono α β] [SMulPosStrictMono α β] (ha : a₁ < a₂) (hb : b₁ ≤ b₂) (h₁ : 0 ≤ a₁) (h₂ : 0 < b₂) : a₁ • b₁ < a₂ • b₂ := (smul_le_smul_of_nonneg_left hb h₁).trans_lt (smul_lt_smul_of_pos_right ha h₂) lemma smul_lt_smul_of_lt_of_le' [PosSMulMono α β] [SMulPosStrictMono α β] (ha : a₁ < a₂) (hb : b₁ ≤ b₂) (h₂ : 0 ≤ a₂) (h₁ : 0 < b₁) : a₁ • b₁ < a₂ • b₂ := (smul_lt_smul_of_pos_right ha h₁).trans_le (smul_le_smul_of_nonneg_left hb h₂) lemma smul_lt_smul [PosSMulStrictMono α β] [SMulPosStrictMono α β] (ha : a₁ < a₂) (hb : b₁ < b₂) (h₁ : 0 < a₁) (h₂ : 0 < b₂) : a₁ • b₁ < a₂ • b₂ := (smul_lt_smul_of_pos_left hb h₁).trans (smul_lt_smul_of_pos_right ha h₂) lemma smul_lt_smul' [PosSMulStrictMono α β] [SMulPosStrictMono α β] (ha : a₁ < a₂) (hb : b₁ < b₂) (h₂ : 0 < a₂) (h₁ : 0 < b₁) : a₁ • b₁ < a₂ • b₂ := (smul_lt_smul_of_pos_right ha h₁).trans (smul_lt_smul_of_pos_left hb h₂) lemma smul_le_smul [PosSMulMono α β] [SMulPosMono α β] (ha : a₁ ≤ a₂) (hb : b₁ ≤ b₂) (h₁ : 0 ≤ a₁) (h₂ : 0 ≤ b₂) : a₁ • b₁ ≤ a₂ • b₂ := (smul_le_smul_of_nonneg_left hb h₁).trans (smul_le_smul_of_nonneg_right ha h₂) lemma smul_le_smul' [PosSMulMono α β] [SMulPosMono α β] (ha : a₁ ≤ a₂) (hb : b₁ ≤ b₂) (h₂ : 0 ≤ a₂) (h₁ : 0 ≤ b₁) : a₁ • b₁ ≤ a₂ • b₂ := (smul_le_smul_of_nonneg_right ha h₁).trans (smul_le_smul_of_nonneg_left hb h₂) end LeftRight end Preorder section LinearOrder variable [Preorder α] [LinearOrder β] section Left variable [Zero α] -- See note [lower instance priority] instance (priority := 100) PosSMulStrictMono.toPosSMulReflectLE [PosSMulStrictMono α β] : PosSMulReflectLE α β where elim _a ha _b₁ _b₂ := (strictMono_smul_left_of_pos ha).le_iff_le.1 lemma PosSMulReflectLE.toPosSMulStrictMono [PosSMulReflectLE α β] : PosSMulStrictMono α β where elim _a ha _b₁ _b₂ hb := not_le.1 fun h ↦ hb.not_le <| le_of_smul_le_smul_left h ha lemma posSMulStrictMono_iff_PosSMulReflectLE : PosSMulStrictMono α β ↔ PosSMulReflectLE α β := ⟨fun _ ↦ inferInstance, fun _ ↦ PosSMulReflectLE.toPosSMulStrictMono⟩ instance PosSMulMono.toPosSMulReflectLT [PosSMulMono α β] : PosSMulReflectLT α β where elim _a ha _b₁ _b₂ := (monotone_smul_left_of_nonneg ha).reflect_lt lemma PosSMulReflectLT.toPosSMulMono [PosSMulReflectLT α β] : PosSMulMono α β where elim _a ha _b₁ _b₂ hb := not_lt.1 fun h ↦ hb.not_lt <| lt_of_smul_lt_smul_left h ha lemma posSMulMono_iff_posSMulReflectLT : PosSMulMono α β ↔ PosSMulReflectLT α β := ⟨fun _ ↦ PosSMulMono.toPosSMulReflectLT, fun _ ↦ PosSMulReflectLT.toPosSMulMono⟩ lemma smul_max_of_nonneg [PosSMulMono α β] (ha : 0 ≤ a) (b₁ b₂ : β) : a • max b₁ b₂ = max (a • b₁) (a • b₂) := (monotone_smul_left_of_nonneg ha).map_max lemma smul_min_of_nonneg [PosSMulMono α β] (ha : 0 ≤ a) (b₁ b₂ : β) : a • min b₁ b₂ = min (a • b₁) (a • b₂) := (monotone_smul_left_of_nonneg ha).map_min end Left section Right variable [Zero β] lemma SMulPosReflectLE.toSMulPosStrictMono [SMulPosReflectLE α β] : SMulPosStrictMono α β where elim _b hb _a₁ _a₂ ha := not_le.1 fun h ↦ ha.not_le <| le_of_smul_le_smul_of_pos_right h hb lemma SMulPosReflectLT.toSMulPosMono [SMulPosReflectLT α β] : SMulPosMono α β where elim _b hb _a₁ _a₂ ha := not_lt.1 fun h ↦ ha.not_lt <| lt_of_smul_lt_smul_right h hb end Right end LinearOrder section LinearOrder variable [LinearOrder α] [Preorder β] section Right variable [Zero β] -- See note [lower instance priority] instance (priority := 100) SMulPosStrictMono.toSMulPosReflectLE [SMulPosStrictMono α β] : SMulPosReflectLE α β where elim _b hb _a₁ _a₂ h := not_lt.1 fun ha ↦ h.not_lt <| smul_lt_smul_of_pos_right ha hb lemma SMulPosMono.toSMulPosReflectLT [SMulPosMono α β] : SMulPosReflectLT α β where elim _b hb _a₁ _a₂ h := not_le.1 fun ha ↦ h.not_le <| smul_le_smul_of_nonneg_right ha hb end Right end LinearOrder section LinearOrder variable [LinearOrder α] [LinearOrder β] section Right variable [Zero β] lemma smulPosStrictMono_iff_SMulPosReflectLE : SMulPosStrictMono α β ↔ SMulPosReflectLE α β := ⟨fun _ ↦ SMulPosStrictMono.toSMulPosReflectLE, fun _ ↦ SMulPosReflectLE.toSMulPosStrictMono⟩ lemma smulPosMono_iff_smulPosReflectLT : SMulPosMono α β ↔ SMulPosReflectLT α β := ⟨fun _ ↦ SMulPosMono.toSMulPosReflectLT, fun _ ↦ SMulPosReflectLT.toSMulPosMono⟩ end Right end LinearOrder end SMul section SMulZeroClass variable [Zero α] [Zero β] [SMulZeroClass α β] section Preorder variable [Preorder α] [Preorder β] lemma smul_pos [PosSMulStrictMono α β] (ha : 0 < a) (hb : 0 < b) : 0 < a • b := by simpa only [smul_zero] using smul_lt_smul_of_pos_left hb ha lemma smul_neg_of_pos_of_neg [PosSMulStrictMono α β] (ha : 0 < a) (hb : b < 0) : a • b < 0 := by simpa only [smul_zero] using smul_lt_smul_of_pos_left hb ha @[simp] lemma smul_pos_iff_of_pos_left [PosSMulStrictMono α β] [PosSMulReflectLT α β] (ha : 0 < a) : 0 < a • b ↔ 0 < b := by simpa only [smul_zero] using smul_lt_smul_iff_of_pos_left ha (b₁ := 0) (b₂ := b) lemma smul_neg_iff_of_pos_left [PosSMulStrictMono α β] [PosSMulReflectLT α β] (ha : 0 < a) : a • b < 0 ↔ b < 0 := by simpa only [smul_zero] using smul_lt_smul_iff_of_pos_left ha (b₂ := (0 : β)) lemma smul_nonneg [PosSMulMono α β] (ha : 0 ≤ a) (hb : 0 ≤ b₁) : 0 ≤ a • b₁ := by simpa only [smul_zero] using smul_le_smul_of_nonneg_left hb ha lemma smul_nonpos_of_nonneg_of_nonpos [PosSMulMono α β] (ha : 0 ≤ a) (hb : b ≤ 0) : a • b ≤ 0 := by simpa only [smul_zero] using smul_le_smul_of_nonneg_left hb ha lemma pos_of_smul_pos_left [PosSMulReflectLT α β] (h : 0 < a • b) (ha : 0 ≤ a) : 0 < b := lt_of_smul_lt_smul_left (by rwa [smul_zero]) ha lemma neg_of_smul_neg_left [PosSMulReflectLT α β] (h : a • b < 0) (ha : 0 ≤ a) : b < 0 := lt_of_smul_lt_smul_left (by rwa [smul_zero]) ha end Preorder end SMulZeroClass section SMulWithZero variable [Zero α] [Zero β] [SMulWithZero α β] section Preorder variable [Preorder α] [Preorder β] lemma smul_pos' [SMulPosStrictMono α β] (ha : 0 < a) (hb : 0 < b) : 0 < a • b := by simpa only [zero_smul] using smul_lt_smul_of_pos_right ha hb lemma smul_neg_of_neg_of_pos [SMulPosStrictMono α β] (ha : a < 0) (hb : 0 < b) : a • b < 0 := by simpa only [zero_smul] using smul_lt_smul_of_pos_right ha hb @[simp] lemma smul_pos_iff_of_pos_right [SMulPosStrictMono α β] [SMulPosReflectLT α β] (hb : 0 < b) : 0 < a • b ↔ 0 < a := by simpa only [zero_smul] using smul_lt_smul_iff_of_pos_right hb (a₁ := 0) (a₂ := a) lemma smul_nonneg' [SMulPosMono α β] (ha : 0 ≤ a) (hb : 0 ≤ b₁) : 0 ≤ a • b₁ := by simpa only [zero_smul] using smul_le_smul_of_nonneg_right ha hb lemma smul_nonpos_of_nonpos_of_nonneg [SMulPosMono α β] (ha : a ≤ 0) (hb : 0 ≤ b) : a • b ≤ 0 := by simpa only [zero_smul] using smul_le_smul_of_nonneg_right ha hb lemma pos_of_smul_pos_right [SMulPosReflectLT α β] (h : 0 < a • b) (hb : 0 ≤ b) : 0 < a := lt_of_smul_lt_smul_right (by rwa [zero_smul]) hb lemma neg_of_smul_neg_right [SMulPosReflectLT α β] (h : a • b < 0) (hb : 0 ≤ b) : a < 0 := lt_of_smul_lt_smul_right (by rwa [zero_smul]) hb lemma pos_iff_pos_of_smul_pos [PosSMulReflectLT α β] [SMulPosReflectLT α β] (hab : 0 < a • b) : 0 < a ↔ 0 < b := ⟨pos_of_smul_pos_left hab ∘ le_of_lt, pos_of_smul_pos_right hab ∘ le_of_lt⟩ end Preorder section PartialOrder variable [PartialOrder α] [Preorder β] /-- A constructor for `PosSMulMono` requiring you to prove `b₁ ≤ b₂ → a • b₁ ≤ a • b₂` only when `0 < a`-/ lemma PosSMulMono.of_pos (h₀ : ∀ a : α, 0 < a → ∀ b₁ b₂ : β, b₁ ≤ b₂ → a • b₁ ≤ a • b₂) : PosSMulMono α β where elim a ha b₁ b₂ h := by obtain ha | ha := ha.eq_or_lt · simp [← ha] · exact h₀ _ ha _ _ h /-- A constructor for `PosSMulReflectLT` requiring you to prove `a • b₁ < a • b₂ → b₁ < b₂` only when `0 < a`-/ lemma PosSMulReflectLT.of_pos (h₀ : ∀ a : α, 0 < a → ∀ b₁ b₂ : β, a • b₁ < a • b₂ → b₁ < b₂) : PosSMulReflectLT α β where elim a ha b₁ b₂ h := by obtain ha | ha := ha.eq_or_lt · simp [← ha] at h · exact h₀ _ ha _ _ h end PartialOrder section PartialOrder variable [Preorder α] [PartialOrder β] /-- A constructor for `SMulPosMono` requiring you to prove `a₁ ≤ a₂ → a₁ • b ≤ a₂ • b` only when `0 < b`-/ lemma SMulPosMono.of_pos (h₀ : ∀ b : β, 0 < b → ∀ a₁ a₂ : α, a₁ ≤ a₂ → a₁ • b ≤ a₂ • b) : SMulPosMono α β where elim b hb a₁ a₂ h := by obtain hb | hb := hb.eq_or_lt · simp [← hb] · exact h₀ _ hb _ _ h /-- A constructor for `SMulPosReflectLT` requiring you to prove `a₁ • b < a₂ • b → a₁ < a₂` only when `0 < b`-/ lemma SMulPosReflectLT.of_pos (h₀ : ∀ b : β, 0 < b → ∀ a₁ a₂ : α, a₁ • b < a₂ • b → a₁ < a₂) : SMulPosReflectLT α β where elim b hb a₁ a₂ h := by obtain hb | hb := hb.eq_or_lt · simp [← hb] at h · exact h₀ _ hb _ _ h end PartialOrder section PartialOrder variable [PartialOrder α] [PartialOrder β] -- See note [lower instance priority] instance (priority := 100) PosSMulStrictMono.toPosSMulMono [PosSMulStrictMono α β] : PosSMulMono α β := PosSMulMono.of_pos fun _a ha ↦ (strictMono_smul_left_of_pos ha).monotone -- See note [lower instance priority] instance (priority := 100) SMulPosStrictMono.toSMulPosMono [SMulPosStrictMono α β] : SMulPosMono α β := SMulPosMono.of_pos fun _b hb ↦ (strictMono_smul_right_of_pos hb).monotone -- See note [lower instance priority] instance (priority := 100) PosSMulReflectLE.toPosSMulReflectLT [PosSMulReflectLE α β] : PosSMulReflectLT α β := PosSMulReflectLT.of_pos fun a ha b₁ b₂ h ↦ (le_of_smul_le_smul_of_pos_left h.le ha).lt_of_ne <| by rintro rfl; simp at h -- See note [lower instance priority] instance (priority := 100) SMulPosReflectLE.toSMulPosReflectLT [SMulPosReflectLE α β] : SMulPosReflectLT α β := SMulPosReflectLT.of_pos fun b hb a₁ a₂ h ↦ (le_of_smul_le_smul_of_pos_right h.le hb).lt_of_ne <| by rintro rfl; simp at h lemma smul_eq_smul_iff_eq_and_eq_of_pos [PosSMulStrictMono α β] [SMulPosStrictMono α β] (ha : a₁ ≤ a₂) (hb : b₁ ≤ b₂) (h₁ : 0 < a₁) (h₂ : 0 < b₂) : a₁ • b₁ = a₂ • b₂ ↔ a₁ = a₂ ∧ b₁ = b₂ := by refine ⟨fun h ↦ ?_, by rintro ⟨rfl, rfl⟩; rfl⟩ simp only [eq_iff_le_not_lt, ha, hb, true_and] refine ⟨fun ha ↦ h.not_lt ?_, fun hb ↦ h.not_lt ?_⟩ · exact (smul_le_smul_of_nonneg_left hb h₁.le).trans_lt (smul_lt_smul_of_pos_right ha h₂) · exact (smul_lt_smul_of_pos_left hb h₁).trans_le (smul_le_smul_of_nonneg_right ha h₂.le) lemma smul_eq_smul_iff_eq_and_eq_of_pos' [PosSMulStrictMono α β] [SMulPosStrictMono α β] (ha : a₁ ≤ a₂) (hb : b₁ ≤ b₂) (h₂ : 0 < a₂) (h₁ : 0 < b₁) : a₁ • b₁ = a₂ • b₂ ↔ a₁ = a₂ ∧ b₁ = b₂ := by refine ⟨fun h ↦ ?_, by rintro ⟨rfl, rfl⟩; rfl⟩ simp only [eq_iff_le_not_lt, ha, hb, true_and] refine ⟨fun ha ↦ h.not_lt ?_, fun hb ↦ h.not_lt ?_⟩ · exact (smul_lt_smul_of_pos_right ha h₁).trans_le (smul_le_smul_of_nonneg_left hb h₂.le) · exact (smul_le_smul_of_nonneg_right ha h₁.le).trans_lt (smul_lt_smul_of_pos_left hb h₂) end PartialOrder section LinearOrder variable [LinearOrder α] [LinearOrder β] lemma pos_and_pos_or_neg_and_neg_of_smul_pos [PosSMulMono α β] [SMulPosMono α β] (hab : 0 < a • b) : 0 < a ∧ 0 < b ∨ a < 0 ∧ b < 0 := by obtain ha | rfl | ha := lt_trichotomy a 0 · refine Or.inr ⟨ha, lt_imp_lt_of_le_imp_le (fun hb ↦ ?_) hab⟩ exact smul_nonpos_of_nonpos_of_nonneg ha.le hb · rw [zero_smul] at hab exact hab.false.elim · refine Or.inl ⟨ha, lt_imp_lt_of_le_imp_le (fun hb ↦ ?_) hab⟩ exact smul_nonpos_of_nonneg_of_nonpos ha.le hb lemma neg_of_smul_pos_right [PosSMulMono α β] [SMulPosMono α β] (h : 0 < a • b) (ha : a ≤ 0) : b < 0 := ((pos_and_pos_or_neg_and_neg_of_smul_pos h).resolve_left fun h ↦ h.1.not_le ha).2 lemma neg_of_smul_pos_left [PosSMulMono α β] [SMulPosMono α β] (h : 0 < a • b) (ha : b ≤ 0) : a < 0 := ((pos_and_pos_or_neg_and_neg_of_smul_pos h).resolve_left fun h ↦ h.2.not_le ha).1 lemma neg_iff_neg_of_smul_pos [PosSMulMono α β] [SMulPosMono α β] (hab : 0 < a • b) : a < 0 ↔ b < 0 := ⟨neg_of_smul_pos_right hab ∘ le_of_lt, neg_of_smul_pos_left hab ∘ le_of_lt⟩ lemma neg_of_smul_neg_left' [SMulPosMono α β] (h : a • b < 0) (ha : 0 ≤ a) : b < 0 := lt_of_not_ge fun hb ↦ (smul_nonneg' ha hb).not_lt h lemma neg_of_smul_neg_right' [PosSMulMono α β] (h : a • b < 0) (hb : 0 ≤ b) : a < 0 := lt_of_not_ge fun ha ↦ (smul_nonneg ha hb).not_lt h end LinearOrder end SMulWithZero section MulAction variable [Monoid α] [Zero β] [MulAction α β] section Preorder variable [Preorder α] [Preorder β] @[simp] lemma le_smul_iff_one_le_left [SMulPosMono α β] [SMulPosReflectLE α β] (hb : 0 < b) : b ≤ a • b ↔ 1 ≤ a := Iff.trans (by rw [one_smul]) (smul_le_smul_iff_of_pos_right hb) @[simp] lemma lt_smul_iff_one_lt_left [SMulPosStrictMono α β] [SMulPosReflectLT α β] (hb : 0 < b) : b < a • b ↔ 1 < a := Iff.trans (by rw [one_smul]) (smul_lt_smul_iff_of_pos_right hb) @[simp] lemma smul_le_iff_le_one_left [SMulPosMono α β] [SMulPosReflectLE α β] (hb : 0 < b) : a • b ≤ b ↔ a ≤ 1 := Iff.trans (by rw [one_smul]) (smul_le_smul_iff_of_pos_right hb) @[simp] lemma smul_lt_iff_lt_one_left [SMulPosStrictMono α β] [SMulPosReflectLT α β] (hb : 0 < b) : a • b < b ↔ a < 1 := Iff.trans (by rw [one_smul]) (smul_lt_smul_iff_of_pos_right hb) lemma smul_le_of_le_one_left [SMulPosMono α β] (hb : 0 ≤ b) (h : a ≤ 1) : a • b ≤ b := by simpa only [one_smul] using smul_le_smul_of_nonneg_right h hb lemma le_smul_of_one_le_left [SMulPosMono α β] (hb : 0 ≤ b) (h : 1 ≤ a) : b ≤ a • b := by simpa only [one_smul] using smul_le_smul_of_nonneg_right h hb lemma smul_lt_of_lt_one_left [SMulPosStrictMono α β] (hb : 0 < b) (h : a < 1) : a • b < b := by simpa only [one_smul] using smul_lt_smul_of_pos_right h hb lemma lt_smul_of_one_lt_left [SMulPosStrictMono α β] (hb : 0 < b) (h : 1 < a) : b < a • b := by simpa only [one_smul] using smul_lt_smul_of_pos_right h hb end Preorder end MulAction section Semiring variable [Semiring α] [AddCommGroup β] [Module α β] [NoZeroSMulDivisors α β] section PartialOrder variable [Preorder α] [PartialOrder β] lemma PosSMulMono.toPosSMulStrictMono [PosSMulMono α β] : PosSMulStrictMono α β := ⟨fun _a ha _b₁ _b₂ hb ↦ (smul_le_smul_of_nonneg_left hb.le ha.le).lt_of_ne <| (smul_right_injective _ ha.ne').ne hb.ne⟩ instance PosSMulReflectLT.toPosSMulReflectLE [PosSMulReflectLT α β] : PosSMulReflectLE α β := ⟨fun _a ha _b₁ _b₂ h ↦ h.eq_or_lt.elim (fun h ↦ (smul_right_injective _ ha.ne' h).le) fun h' ↦ (lt_of_smul_lt_smul_left h' ha.le).le⟩ end PartialOrder section PartialOrder variable [PartialOrder α] [PartialOrder β] lemma posSMulMono_iff_posSMulStrictMono : PosSMulMono α β ↔ PosSMulStrictMono α β := ⟨fun _ ↦ PosSMulMono.toPosSMulStrictMono, fun _ ↦ inferInstance⟩ lemma PosSMulReflectLE_iff_posSMulReflectLT : PosSMulReflectLE α β ↔ PosSMulReflectLT α β := ⟨fun _ ↦ inferInstance, fun _ ↦ PosSMulReflectLT.toPosSMulReflectLE⟩ end PartialOrder end Semiring section Ring variable [Ring α] [AddCommGroup β] [Module α β] [NoZeroSMulDivisors α β] section PartialOrder variable [PartialOrder α] [PartialOrder β] lemma SMulPosMono.toSMulPosStrictMono [SMulPosMono α β] : SMulPosStrictMono α β := ⟨fun _b hb _a₁ _a₂ ha ↦ (smul_le_smul_of_nonneg_right ha.le hb.le).lt_of_ne <| (smul_left_injective _ hb.ne').ne ha.ne⟩ lemma smulPosMono_iff_smulPosStrictMono : SMulPosMono α β ↔ SMulPosStrictMono α β := ⟨fun _ ↦ SMulPosMono.toSMulPosStrictMono, fun _ ↦ inferInstance⟩ lemma SMulPosReflectLT.toSMulPosReflectLE [SMulPosReflectLT α β] : SMulPosReflectLE α β := ⟨fun _b hb _a₁ _a₂ h ↦ h.eq_or_lt.elim (fun h ↦ (smul_left_injective _ hb.ne' h).le) fun h' ↦ (lt_of_smul_lt_smul_right h' hb.le).le⟩ lemma SMulPosReflectLE_iff_smulPosReflectLT : SMulPosReflectLE α β ↔ SMulPosReflectLT α β := ⟨fun _ ↦ inferInstance, fun _ ↦ SMulPosReflectLT.toSMulPosReflectLE⟩ end PartialOrder end Ring section GroupWithZero variable [GroupWithZero α] [Preorder α] [Preorder β] [MulAction α β] lemma inv_smul_le_iff_of_pos [PosSMulMono α β] [PosSMulReflectLE α β] (ha : 0 < a) : a⁻¹ • b₁ ≤ b₂ ↔ b₁ ≤ a • b₂ := by rw [← smul_le_smul_iff_of_pos_left ha, smul_inv_smul₀ ha.ne'] lemma le_inv_smul_iff_of_pos [PosSMulMono α β] [PosSMulReflectLE α β] (ha : 0 < a) : b₁ ≤ a⁻¹ • b₂ ↔ a • b₁ ≤ b₂ := by rw [← smul_le_smul_iff_of_pos_left ha, smul_inv_smul₀ ha.ne'] lemma inv_smul_lt_iff_of_pos [PosSMulStrictMono α β] [PosSMulReflectLT α β] (ha : 0 < a) : a⁻¹ • b₁ < b₂ ↔ b₁ < a • b₂ := by rw [← smul_lt_smul_iff_of_pos_left ha, smul_inv_smul₀ ha.ne'] lemma lt_inv_smul_iff_of_pos [PosSMulStrictMono α β] [PosSMulReflectLT α β] (ha : 0 < a) : b₁ < a⁻¹ • b₂ ↔ a • b₁ < b₂ := by rw [← smul_lt_smul_iff_of_pos_left ha, smul_inv_smul₀ ha.ne'] /-- Right scalar multiplication as an order isomorphism. -/ @[simps!] def OrderIso.smulRight [PosSMulMono α β] [PosSMulReflectLE α β] {a : α} (ha : 0 < a) : β ≃o β where toEquiv := Equiv.smulRight ha.ne' map_rel_iff' := smul_le_smul_iff_of_pos_left ha end GroupWithZero namespace OrderDual section Left variable [Preorder α] [Preorder β] [SMul α β] [Zero α] instance instPosSMulMono [PosSMulMono α β] : PosSMulMono α βᵒᵈ where elim _a ha _b₁ _b₂ hb := smul_le_smul_of_nonneg_left (β := β) hb ha instance instPosSMulStrictMono [PosSMulStrictMono α β] : PosSMulStrictMono α βᵒᵈ where elim _a ha _b₁ _b₂ hb := smul_lt_smul_of_pos_left (β := β) hb ha instance instPosSMulReflectLT [PosSMulReflectLT α β] : PosSMulReflectLT α βᵒᵈ where elim _a ha _b₁ _b₂ h := lt_of_smul_lt_smul_of_nonneg_left (β := β) h ha instance instPosSMulReflectLE [PosSMulReflectLE α β] : PosSMulReflectLE α βᵒᵈ where elim _a ha _b₁ _b₂ h := le_of_smul_le_smul_of_pos_left (β := β) h ha end Left section Right variable [Preorder α] [Ring α] [OrderedAddCommGroup β] [Module α β] instance instSMulPosMono [SMulPosMono α β] : SMulPosMono α βᵒᵈ where elim _b hb a₁ a₂ ha := by rw [← neg_le_neg_iff, ← smul_neg, ← smul_neg] exact smul_le_smul_of_nonneg_right (β := β) ha <| neg_nonneg.2 hb instance instSMulPosStrictMono [SMulPosStrictMono α β] : SMulPosStrictMono α βᵒᵈ where elim _b hb a₁ a₂ ha := by rw [← neg_lt_neg_iff, ← smul_neg, ← smul_neg] exact smul_lt_smul_of_pos_right (β := β) ha <| neg_pos.2 hb instance instSMulPosReflectLT [SMulPosReflectLT α β] : SMulPosReflectLT α βᵒᵈ where elim _b hb a₁ a₂ h := by rw [← neg_lt_neg_iff, ← smul_neg, ← smul_neg] at h exact lt_of_smul_lt_smul_right (β := β) h <| neg_nonneg.2 hb instance instSMulPosReflectLE [SMulPosReflectLE α β] : SMulPosReflectLE α βᵒᵈ where elim _b hb a₁ a₂ h := by rw [← neg_le_neg_iff, ← smul_neg, ← smul_neg] at h exact le_of_smul_le_smul_right (β := β) h <| neg_pos.2 hb end Right end OrderDual section OrderedRing variable [OrderedRing α] section OrderedAddCommGroup variable [OrderedAddCommGroup β] [Module α β] section PosSMulMono variable [PosSMulMono α β] lemma smul_le_smul_of_nonpos_left (h : b₁ ≤ b₂) (ha : a ≤ 0) : a • b₂ ≤ a • b₁ := by rw [← neg_neg a, neg_smul, neg_smul (-a), neg_le_neg_iff] exact smul_le_smul_of_nonneg_left h (neg_nonneg_of_nonpos ha) lemma antitone_smul_left (ha : a ≤ 0) : Antitone ((a • ·) : β → β) := fun _ _ h ↦ smul_le_smul_of_nonpos_left h ha instance PosSMulMono.toSMulPosMono : SMulPosMono α β where elim _b hb a₁ a₂ ha := by rw [← sub_nonneg, ← sub_smul]; exact smul_nonneg (sub_nonneg.2 ha) hb end PosSMulMono section PosSMulStrictMono variable [PosSMulStrictMono α β] lemma smul_lt_smul_of_neg_left (hb : b₁ < b₂) (ha : a < 0) : a • b₂ < a • b₁ := by rw [← neg_neg a, neg_smul, neg_smul (-a), neg_lt_neg_iff] exact smul_lt_smul_of_pos_left hb (neg_pos_of_neg ha) lemma strictAnti_smul_left (ha : a < 0) : StrictAnti ((a • ·) : β → β) := fun _ _ h ↦ smul_lt_smul_of_neg_left h ha instance PosSMulStrictMono.toSMulPosStrictMono : SMulPosStrictMono α β where elim _b hb a₁ a₂ ha := by rw [← sub_pos, ← sub_smul]; exact smul_pos (sub_pos.2 ha) hb end PosSMulStrictMono lemma le_of_smul_le_smul_of_neg [PosSMulReflectLE α β] (h : a • b₁ ≤ a • b₂) (ha : a < 0) : b₂ ≤ b₁ := by rw [← neg_neg a, neg_smul, neg_smul (-a), neg_le_neg_iff] at h exact le_of_smul_le_smul_of_pos_left h <| neg_pos.2 ha lemma lt_of_smul_lt_smul_of_nonpos [PosSMulReflectLT α β] (h : a • b₁ < a • b₂) (ha : a ≤ 0) : b₂ < b₁ := by rw [← neg_neg a, neg_smul, neg_smul (-a), neg_lt_neg_iff] at h exact lt_of_smul_lt_smul_of_nonneg_left h (neg_nonneg_of_nonpos ha) lemma smul_nonneg_of_nonpos_of_nonpos [SMulPosMono α β] (ha : a ≤ 0) (hb : b ≤ 0) : 0 ≤ a • b := smul_nonpos_of_nonpos_of_nonneg (β := βᵒᵈ) ha hb lemma smul_le_smul_iff_of_neg_left [PosSMulMono α β] [PosSMulReflectLE α β] (ha : a < 0) : a • b₁ ≤ a • b₂ ↔ b₂ ≤ b₁ := by rw [← neg_neg a, neg_smul, neg_smul (-a), neg_le_neg_iff] exact smul_le_smul_iff_of_pos_left (neg_pos_of_neg ha) section PosSMulStrictMono variable [PosSMulStrictMono α β] [PosSMulReflectLT α β] lemma smul_lt_smul_iff_of_neg_left (ha : a < 0) : a • b₁ < a • b₂ ↔ b₂ < b₁ := by rw [← neg_neg a, neg_smul, neg_smul (-a), neg_lt_neg_iff] exact smul_lt_smul_iff_of_pos_left (neg_pos_of_neg ha) lemma smul_pos_iff_of_neg_left (ha : a < 0) : 0 < a • b ↔ b < 0 := by simpa only [smul_zero] using smul_lt_smul_iff_of_neg_left ha (b₁ := (0 : β)) alias ⟨_, smul_pos_of_neg_of_neg⟩ := smul_pos_iff_of_neg_left lemma smul_neg_iff_of_neg_left (ha : a < 0) : a • b < 0 ↔ 0 < b := by simpa only [smul_zero] using smul_lt_smul_iff_of_neg_left ha (b₂ := (0 : β)) end PosSMulStrictMono /-- Binary **rearrangement inequality**. -/ lemma smul_add_smul_le_smul_add_smul [PosSMulMono α β] {b₁ b₂ : α} {a d : β} (hab : b₁ ≤ b₂) (hcd : a ≤ d) : b₁ • d + b₂ • a ≤ b₁ • a + b₂ • d := by obtain ⟨b₂, rfl⟩ := exists_add_of_le hab obtain ⟨d, rfl⟩ := exists_add_of_le hcd rw [smul_add, add_right_comm, smul_add, ← add_assoc, add_smul _ _ d] rw [le_add_iff_nonneg_right] at hab hcd exact add_le_add_left (le_add_of_nonneg_right <| smul_nonneg hab hcd) _ /-- Binary **rearrangement inequality**. -/ lemma smul_add_smul_le_smul_add_smul' [PosSMulMono α β] {b₁ b₂ : α} {a d : β} (hba : b₂ ≤ b₁) (hdc : d ≤ a) : b₁ • d + b₂ • a ≤ b₁ • a + b₂ • d := by rw [add_comm (b₁ • d), add_comm (b₁ • a)] exact smul_add_smul_le_smul_add_smul hba hdc /-- Binary strict **rearrangement inequality**. -/ lemma smul_add_smul_lt_smul_add_smul [PosSMulStrictMono α β] {b₁ b₂ : α} {a d : β} (hab : b₁ < b₂) (hcd : a < d) : b₁ • d + b₂ • a < b₁ • a + b₂ • d := by obtain ⟨b₂, rfl⟩ := exists_add_of_le hab.le obtain ⟨d, rfl⟩ := exists_add_of_le hcd.le rw [smul_add, add_right_comm, smul_add, ← add_assoc, add_smul _ _ d] rw [lt_add_iff_pos_right] at hab hcd exact add_lt_add_left (lt_add_of_pos_right _ <| smul_pos hab hcd) _ /-- Binary strict **rearrangement inequality**. -/ lemma smul_add_smul_lt_smul_add_smul' [PosSMulStrictMono α β] {b₁ b₂ : α} {a d : β} (hba : b₂ < b₁) (hdc : d < a) : b₁ • d + b₂ • a < b₁ • a + b₂ • d := by rw [add_comm (b₁ • d), add_comm (b₁ • a)] exact smul_add_smul_lt_smul_add_smul hba hdc end OrderedAddCommGroup section LinearOrderedAddCommGroup variable [LinearOrderedAddCommGroup β] [Module α β] [PosSMulMono α β] {a : α} {b b₁ b₂ : β} lemma smul_max_of_nonpos (ha : a ≤ 0) (b₁ b₂ : β) : a • max b₁ b₂ = min (a • b₁) (a • b₂) := (antitone_smul_left ha : Antitone (_ : β → β)).map_max lemma smul_min_of_nonpos (ha : a ≤ 0) (b₁ b₂ : β) : a • min b₁ b₂ = max (a • b₁) (a • b₂) := (antitone_smul_left ha : Antitone (_ : β → β)).map_min end LinearOrderedAddCommGroup end OrderedRing section LinearOrderedRing variable [LinearOrderedRing α] [LinearOrderedAddCommGroup β] [Module α β] [PosSMulStrictMono α β] {a : α} {b : β} lemma nonneg_and_nonneg_or_nonpos_and_nonpos_of_smul_nonneg (hab : 0 ≤ a • b) : 0 ≤ a ∧ 0 ≤ b ∨ a ≤ 0 ∧ b ≤ 0 := by simp only [Decidable.or_iff_not_and_not, not_and, not_le] refine fun ab nab ↦ hab.not_lt ?_ obtain ha | rfl | ha := lt_trichotomy 0 a exacts [smul_neg_of_pos_of_neg ha (ab ha.le), ((ab le_rfl).asymm (nab le_rfl)).elim, smul_neg_of_neg_of_pos ha (nab ha.le)] lemma smul_nonneg_iff : 0 ≤ a • b ↔ 0 ≤ a ∧ 0 ≤ b ∨ a ≤ 0 ∧ b ≤ 0 := ⟨nonneg_and_nonneg_or_nonpos_and_nonpos_of_smul_nonneg, fun h ↦ h.elim (and_imp.2 smul_nonneg) (and_imp.2 smul_nonneg_of_nonpos_of_nonpos)⟩ lemma smul_nonpos_iff : a • b ≤ 0 ↔ 0 ≤ a ∧ b ≤ 0 ∨ a ≤ 0 ∧ 0 ≤ b := by rw [← neg_nonneg, ← smul_neg, smul_nonneg_iff, neg_nonneg, neg_nonpos] lemma smul_nonneg_iff_pos_imp_nonneg : 0 ≤ a • b ↔ (0 < a → 0 ≤ b) ∧ (0 < b → 0 ≤ a) := smul_nonneg_iff.trans <| by simp_rw [← not_le, ← or_iff_not_imp_left]; have := le_total a 0; have := le_total b 0; tauto lemma smul_nonneg_iff_neg_imp_nonpos : 0 ≤ a • b ↔ (a < 0 → b ≤ 0) ∧ (b < 0 → a ≤ 0) := by rw [← neg_smul_neg, smul_nonneg_iff_pos_imp_nonneg]; simp only [neg_pos, neg_nonneg] lemma smul_nonpos_iff_pos_imp_nonpos : a • b ≤ 0 ↔ (0 < a → b ≤ 0) ∧ (b < 0 → 0 ≤ a) := by rw [← neg_nonneg, ← smul_neg, smul_nonneg_iff_pos_imp_nonneg]; simp only [neg_pos, neg_nonneg] lemma smul_nonpos_iff_neg_imp_nonneg : a • b ≤ 0 ↔ (a < 0 → 0 ≤ b) ∧ (0 < b → a ≤ 0) := by rw [← neg_nonneg, ← neg_smul, smul_nonneg_iff_pos_imp_nonneg]; simp only [neg_pos, neg_nonneg] end LinearOrderedRing section LinearOrderedSemifield variable [LinearOrderedSemifield α] [AddCommGroup β] [PartialOrder β] -- See note [lower instance priority] instance (priority := 100) PosSMulMono.toPosSMulReflectLE [MulAction α β] [PosSMulMono α β] : PosSMulReflectLE α β where elim _a ha b₁ b₂ h := by simpa [ha.ne'] using smul_le_smul_of_nonneg_left h <| inv_nonneg (α := α) |>.2 ha.le -- See note [lower instance priority] instance (priority := 100) PosSMulStrictMono.toPosSMulReflectLT [MulActionWithZero α β] [PosSMulStrictMono α β] : PosSMulReflectLT α β := PosSMulReflectLT.of_pos fun a ha b₁ b₂ h ↦ by simpa [ha.ne'] using smul_lt_smul_of_pos_left h <| inv_pos (α := α) |>.2 ha end LinearOrderedSemifield section Field variable [LinearOrderedField α] [OrderedAddCommGroup β] [Module α β] {a : α} {b₁ b₂ : β} section PosSMulMono variable [PosSMulMono α β] lemma inv_smul_le_iff_of_neg (h : a < 0) : a⁻¹ • b₁ ≤ b₂ ↔ a • b₂ ≤ b₁ := by rw [← smul_le_smul_iff_of_neg_left h, smul_inv_smul₀ h.ne] lemma smul_inv_le_iff_of_neg (h : a < 0) : b₁ ≤ a⁻¹ • b₂ ↔ b₂ ≤ a • b₁ := by rw [← smul_le_smul_iff_of_neg_left h, smul_inv_smul₀ h.ne] variable (β) /-- Left scalar multiplication as an order isomorphism. -/ @[simps!] def OrderIso.smulRightDual (ha : a < 0) : β ≃o βᵒᵈ where toEquiv := (Equiv.smulRight ha.ne).trans toDual map_rel_iff' := (@OrderDual.toDual_le_toDual β).trans <| smul_le_smul_iff_of_neg_left ha end PosSMulMono variable [PosSMulStrictMono α β] lemma inv_smul_lt_iff_of_neg (h : a < 0) : a⁻¹ • b₁ < b₂ ↔ a • b₂ < b₁ := by rw [← smul_lt_smul_iff_of_neg_left h, smul_inv_smul₀ h.ne] lemma smul_inv_lt_iff_of_neg (h : a < 0) : b₁ < a⁻¹ • b₂ ↔ b₂ < a • b₁ := by rw [← smul_lt_smul_iff_of_neg_left h, smul_inv_smul₀ h.ne] end Field namespace Prod end Prod namespace Pi variable {ι : Type*} {β : ι → Type*} [Zero α] [∀ i, Zero (β i)] section SMulZeroClass variable [Preorder α] [∀ i, Preorder (β i)] [∀ i, SMulZeroClass α (β i)] instance instPosSMulMono [∀ i, PosSMulMono α (β i)] : PosSMulMono α (∀ i, β i) where elim _a ha _b₁ _b₂ hb i := smul_le_smul_of_nonneg_left (hb i) ha instance instSMulPosMono [∀ i, SMulPosMono α (β i)] : SMulPosMono α (∀ i, β i) where elim _b hb _a₁ _a₂ ha i := smul_le_smul_of_nonneg_right ha (hb i) instance instPosSMulReflectLE [∀ i, PosSMulReflectLE α (β i)] : PosSMulReflectLE α (∀ i, β i) where elim _a ha _b₁ _b₂ h i := le_of_smul_le_smul_left (h i) ha instance instSMulPosReflectLE [∀ i, SMulPosReflectLE α (β i)] : SMulPosReflectLE α (∀ i, β i) where elim _b hb _a₁ _a₂ h := by obtain ⟨-, i, hi⟩ := lt_def.1 hb; exact le_of_smul_le_smul_right (h _) hi end SMulZeroClass section SMulWithZero variable [PartialOrder α] [∀ i, PartialOrder (β i)] [∀ i, SMulWithZero α (β i)] instance instPosSMulStrictMono [∀ i, PosSMulStrictMono α (β i)] : PosSMulStrictMono α (∀ i, β i) where elim := by simp_rw [lt_def] rintro _a ha _b₁ _b₂ ⟨hb, i, hi⟩ exact ⟨smul_le_smul_of_nonneg_left hb ha.le, i, smul_lt_smul_of_pos_left hi ha⟩ instance instSMulPosStrictMono [∀ i, SMulPosStrictMono α (β i)] : SMulPosStrictMono α (∀ i, β i) where elim := by simp_rw [lt_def] rintro a ⟨ha, i, hi⟩ _b₁ _b₂ hb exact ⟨smul_le_smul_of_nonneg_right hb.le ha, i, smul_lt_smul_of_pos_right hb hi⟩ -- Note: There is no interesting instance for `PosSMulReflectLT α (∀ i, β i)` that's not already -- implied by the other instances instance instSMulPosReflectLT [∀ i, SMulPosReflectLT α (β i)] : SMulPosReflectLT α (∀ i, β i) where elim := by simp_rw [lt_def] rintro b hb _a₁ _a₂ ⟨-, i, hi⟩ exact lt_of_smul_lt_smul_right hi <| hb _ end SMulWithZero end Pi section Lift variable {γ : Type*} [Preorder α] [Preorder β] [Preorder γ] [SMul α β] [SMul α γ] (f : β → γ) section variable [Zero α] lemma PosSMulMono.lift [PosSMulMono α γ] (hf : ∀ {b₁ b₂}, f b₁ ≤ f b₂ ↔ b₁ ≤ b₂) (smul : ∀ (a : α) b, f (a • b) = a • f b) : PosSMulMono α β where elim a ha b₁ b₂ hb := by simp only [← hf, smul] at *; exact smul_le_smul_of_nonneg_left hb ha lemma PosSMulStrictMono.lift [PosSMulStrictMono α γ] (hf : ∀ {b₁ b₂}, f b₁ ≤ f b₂ ↔ b₁ ≤ b₂) (smul : ∀ (a : α) b, f (a • b) = a • f b) : PosSMulStrictMono α β where elim a ha b₁ b₂ hb := by simp only [← lt_iff_lt_of_le_iff_le' hf hf, smul] at *; exact smul_lt_smul_of_pos_left hb ha lemma PosSMulReflectLE.lift [PosSMulReflectLE α γ] (hf : ∀ {b₁ b₂}, f b₁ ≤ f b₂ ↔ b₁ ≤ b₂) (smul : ∀ (a : α) b, f (a • b) = a • f b) : PosSMulReflectLE α β where elim a ha b₁ b₂ h := hf.1 <| le_of_smul_le_smul_left (by simpa only [smul] using hf.2 h) ha lemma PosSMulReflectLT.lift [PosSMulReflectLT α γ] (hf : ∀ {b₁ b₂}, f b₁ ≤ f b₂ ↔ b₁ ≤ b₂) (smul : ∀ (a : α) b, f (a • b) = a • f b) : PosSMulReflectLT α β where elim a ha b₁ b₂ h := by simp only [← lt_iff_lt_of_le_iff_le' hf hf, smul] at *; exact lt_of_smul_lt_smul_left h ha end section variable [Zero β] [Zero γ] lemma SMulPosMono.lift [SMulPosMono α γ] (hf : ∀ {b₁ b₂}, f b₁ ≤ f b₂ ↔ b₁ ≤ b₂) (smul : ∀ (a : α) b, f (a • b) = a • f b) (zero : f 0 = 0) : SMulPosMono α β where elim b hb a₁ a₂ ha := by simp only [← hf, zero, smul] at *; exact smul_le_smul_of_nonneg_right ha hb lemma SMulPosStrictMono.lift [SMulPosStrictMono α γ] (hf : ∀ {b₁ b₂}, f b₁ ≤ f b₂ ↔ b₁ ≤ b₂) (smul : ∀ (a : α) b, f (a • b) = a • f b) (zero : f 0 = 0) : SMulPosStrictMono α β where elim b hb a₁ a₂ ha := by simp only [← lt_iff_lt_of_le_iff_le' hf hf, zero, smul] at * exact smul_lt_smul_of_pos_right ha hb lemma SMulPosReflectLE.lift [SMulPosReflectLE α γ] (hf : ∀ {b₁ b₂}, f b₁ ≤ f b₂ ↔ b₁ ≤ b₂) (smul : ∀ (a : α) b, f (a • b) = a • f b) (zero : f 0 = 0) : SMulPosReflectLE α β where elim b hb a₁ a₂ h := by simp only [← hf, ← lt_iff_lt_of_le_iff_le' hf hf, zero, smul] at * exact le_of_smul_le_smul_right h hb lemma SMulPosReflectLT.lift [SMulPosReflectLT α γ] (hf : ∀ {b₁ b₂}, f b₁ ≤ f b₂ ↔ b₁ ≤ b₂) (smul : ∀ (a : α) b, f (a • b) = a • f b) (zero : f 0 = 0) : SMulPosReflectLT α β where elim b hb a₁ a₂ h := by simp only [← hf, ← lt_iff_lt_of_le_iff_le' hf hf, zero, smul] at * exact lt_of_smul_lt_smul_right h hb end end Lift section Nat instance OrderedSemiring.toPosSMulMonoNat [OrderedSemiring α] : PosSMulMono ℕ α where elim _n _ _a _b hab := nsmul_le_nsmul_right hab _ instance OrderedSemiring.toSMulPosMonoNat [OrderedSemiring α] : SMulPosMono ℕ α where elim _a ha _m _n hmn := nsmul_le_nsmul_left ha hmn instance StrictOrderedSemiring.toPosSMulStrictMonoNat [StrictOrderedSemiring α] : PosSMulStrictMono ℕ α where elim _n hn _a _b hab := nsmul_right_strictMono hn.ne' hab instance StrictOrderedSemiring.toSMulPosStrictMonoNat [StrictOrderedSemiring α] : SMulPosStrictMono ℕ α where elim _a ha _m _n hmn := nsmul_lt_nsmul_left ha hmn end Nat -- TODO: Instances for `Int` and `Rat` namespace Mathlib.Meta.Positivity section OrderedSMul variable [Zero α] [Zero β] [SMulZeroClass α β] [Preorder α] [Preorder β] [PosSMulMono α β] {a : α} {b : β} private theorem smul_nonneg_of_pos_of_nonneg (ha : 0 < a) (hb : 0 ≤ b) : 0 ≤ a • b := smul_nonneg ha.le hb private theorem smul_nonneg_of_nonneg_of_pos (ha : 0 ≤ a) (hb : 0 < b) : 0 ≤ a • b := smul_nonneg ha hb.le end OrderedSMul section NoZeroSMulDivisors variable [Zero α] [Zero β] [SMul α β] [NoZeroSMulDivisors α β] {a : α} {b : β} private theorem smul_ne_zero_of_pos_of_ne_zero [Preorder α] (ha : 0 < a) (hb : b ≠ 0) : a • b ≠ 0 := smul_ne_zero ha.ne' hb private theorem smul_ne_zero_of_ne_zero_of_pos [Preorder β] (ha : a ≠ 0) (hb : 0 < b) : a • b ≠ 0 := smul_ne_zero ha hb.ne' end NoZeroSMulDivisors open Lean.Meta Qq /-- Positivity extension for HSMul, i.e. (_ • _). -/ @[positivity HSMul.hSMul _ _] def evalHSMul : PositivityExt where eval {_u α} zα pα (e : Q($α)) := do let .app (.app (.app (.app (.app (.app (.const ``HSMul.hSMul [u1, _, _]) (β : Q(Type u1))) _) _) _) (a : Q($β))) (b : Q($α)) ← whnfR e | throwError "failed to match hSMul" let zM : Q(Zero $β) ← synthInstanceQ q(Zero $β) let pM : Q(PartialOrder $β) ← synthInstanceQ q(PartialOrder $β) -- Using `q()` here would be impractical, as we would have to manually `synthInstanceQ` all the -- required typeclasses. Ideally we could tell `q()` to do this automatically. match ← core zM pM a, ← core zα pα b with | .positive pa, .positive pb => pure (.positive (← mkAppM ``smul_pos #[pa, pb])) | .positive pa, .nonnegative pb => pure (.nonnegative (← mkAppM ``smul_nonneg_of_pos_of_nonneg #[pa, pb])) | .nonnegative pa, .positive pb => pure (.nonnegative (← mkAppM ``smul_nonneg_of_nonneg_of_pos #[pa, pb])) | .nonnegative pa, .nonnegative pb => pure (.nonnegative (← mkAppM ``smul_nonneg #[pa, pb])) | .positive pa, .nonzero pb => pure (.nonzero (← mkAppM ``smul_ne_zero_of_pos_of_ne_zero #[pa, pb])) | .nonzero pa, .positive pb => pure (.nonzero (← mkAppM ``smul_ne_zero_of_ne_zero_of_pos #[pa, pb])) | .nonzero pa, .nonzero pb => pure (.nonzero (← mkAppM ``smul_ne_zero #[pa, pb])) | _, _ => pure .none end Mathlib.Meta.Positivity /-! ### Deprecated lemmas Those lemmas have been deprecated on 2023-12-23. -/ @[deprecated (since := "2023-12-23")] alias monotone_smul_left := monotone_smul_left_of_nonneg @[deprecated (since := "2023-12-23")] alias strict_mono_smul_left := strictMono_smul_left_of_pos @[deprecated (since := "2023-12-23")] alias smul_le_smul_of_nonneg := smul_le_smul_of_nonneg_left @[deprecated (since := "2023-12-23")] alias smul_lt_smul_of_pos := smul_lt_smul_of_pos_left @[deprecated (since := "2023-12-23")] alias lt_of_smul_lt_smul_of_nonneg := lt_of_smul_lt_smul_of_nonneg_left @[deprecated (since := "2023-12-23")] alias smul_le_smul_iff_of_pos := smul_le_smul_iff_of_pos_left @[deprecated (since := "2023-12-23")] alias smul_lt_smul_iff_of_pos := smul_lt_smul_iff_of_pos_left @[deprecated (since := "2023-12-23")] alias smul_max := smul_max_of_nonneg @[deprecated (since := "2023-12-23")] alias smul_min := smul_min_of_nonneg @[deprecated (since := "2023-12-23")] alias smul_pos_iff_of_pos := smul_pos_iff_of_pos_left @[deprecated (since := "2023-12-23")] alias inv_smul_le_iff := inv_smul_le_iff_of_pos @[deprecated (since := "2023-12-23")] alias le_inv_smul_iff := le_inv_smul_iff_of_pos @[deprecated (since := "2023-12-23")] alias inv_smul_lt_iff := inv_smul_lt_iff_of_pos @[deprecated (since := "2023-12-23")] alias lt_inv_smul_iff := lt_inv_smul_iff_of_pos @[deprecated (since := "2023-12-23")] alias OrderIso.smulLeft := OrderIso.smulRight @[deprecated (since := "2023-12-23")] alias OrderIso.smulLeft_symm_apply := OrderIso.smulRight_symm_apply @[deprecated (since := "2023-12-23")] alias OrderIso.smulLeft_apply := OrderIso.smulRight_apply @[deprecated (since := "2023-12-23")] alias smul_neg_iff_of_pos := smul_neg_iff_of_pos_left /-! Those lemmas have been deprecated on 2023-12-27. -/ @[deprecated (since := "2023-12-27")] alias strict_anti_smul_left := strictAnti_smul_left @[deprecated (since := "2023-12-27")] alias smul_le_smul_of_nonpos := smul_le_smul_of_nonpos_left @[deprecated (since := "2023-12-27")] alias smul_lt_smul_of_neg := smul_lt_smul_of_neg_left @[deprecated (since := "2023-12-27")] alias smul_pos_iff_of_neg := smul_pos_iff_of_neg_left @[deprecated (since := "2023-12-27")] alias smul_neg_iff_of_neg := smul_neg_iff_of_neg_left @[deprecated (since := "2023-12-27")] alias smul_le_smul_iff_of_neg := smul_le_smul_iff_of_neg_left @[deprecated (since := "2023-12-27")] alias smul_lt_smul_iff_of_neg := smul_lt_smul_iff_of_neg_left
Algebra\Order\Module\OrderedSMul.lean
/- Copyright (c) 2020 Frédéric Dupuis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Frédéric Dupuis -/ import Mathlib.Algebra.Module.Pi import Mathlib.Algebra.Module.Prod import Mathlib.Algebra.Order.Module.Defs import Mathlib.Algebra.Order.Monoid.Prod import Mathlib.Algebra.Order.Pi import Mathlib.Tactic.GCongr.Core /-! # Ordered scalar product In this file we define * `OrderedSMul R M` : an ordered additive commutative monoid `M` is an `OrderedSMul` over an `OrderedSemiring` `R` if the scalar product respects the order relation on the monoid and on the ring. There is a correspondence between this structure and convex cones, which is proven in `Analysis/Convex/Cone.lean`. ## Implementation notes * We choose to define `OrderedSMul` as a `Prop`-valued mixin, so that it can be used for actions, modules, and algebras (the axioms for an "ordered algebra" are exactly that the algebra is ordered as a module). * To get ordered modules and ordered vector spaces, it suffices to replace the `OrderedAddCommMonoid` and the `OrderedSemiring` as desired. ## TODO This file is now mostly useless. We should try deleting `OrderedSMul` ## References * https://en.wikipedia.org/wiki/Ordered_vector_space ## Tags ordered module, ordered scalar, ordered smul, ordered action, ordered vector space -/ /-- The ordered scalar product property is when an ordered additive commutative monoid with a partial order has a scalar multiplication which is compatible with the order. Note that this is different from `IsOrderedSMul`, which uses `≤`, has no semiring assumption, and has no positivity constraint on the defining conditions. -/ class OrderedSMul (R M : Type*) [OrderedSemiring R] [OrderedAddCommMonoid M] [SMulWithZero R M] : Prop where /-- Scalar multiplication by positive elements preserves the order. -/ protected smul_lt_smul_of_pos : ∀ {a b : M}, ∀ {c : R}, a < b → 0 < c → c • a < c • b /-- If `c • a < c • b` for some positive `c`, then `a < b`. -/ protected lt_of_smul_lt_smul_of_pos : ∀ {a b : M}, ∀ {c : R}, c • a < c • b → 0 < c → a < b variable {ι α β γ 𝕜 R M N : Type*} section OrderedSMul variable [OrderedSemiring R] [OrderedAddCommMonoid M] [SMulWithZero R M] [OrderedSMul R M] {s : Set M} {a b : M} {c : R} instance OrderedSMul.toPosSMulStrictMono : PosSMulStrictMono R M where elim _a ha _b₁ _b₂ hb := OrderedSMul.smul_lt_smul_of_pos hb ha instance OrderedSMul.toPosSMulReflectLT : PosSMulReflectLT R M := PosSMulReflectLT.of_pos fun _a ha _b₁ _b₂ h ↦ OrderedSMul.lt_of_smul_lt_smul_of_pos h ha instance OrderDual.instOrderedSMul : OrderedSMul R Mᵒᵈ where smul_lt_smul_of_pos := OrderedSMul.smul_lt_smul_of_pos (M := M) lt_of_smul_lt_smul_of_pos := OrderedSMul.lt_of_smul_lt_smul_of_pos (M := M) end OrderedSMul /-- To prove that a linear ordered monoid is an ordered module, it suffices to verify only the first axiom of `OrderedSMul`. -/ theorem OrderedSMul.mk'' [OrderedSemiring 𝕜] [LinearOrderedAddCommMonoid M] [SMulWithZero 𝕜 M] (h : ∀ ⦃c : 𝕜⦄, 0 < c → StrictMono fun a : M => c • a) : OrderedSMul 𝕜 M := { smul_lt_smul_of_pos := fun hab hc => h hc hab lt_of_smul_lt_smul_of_pos := fun hab hc => (h hc).lt_iff_lt.1 hab } instance Nat.orderedSMul [LinearOrderedCancelAddCommMonoid M] : OrderedSMul ℕ M := OrderedSMul.mk'' fun n hn a b hab => by cases n with | zero => cases hn | succ n => induction n with | zero => dsimp; rwa [one_nsmul, one_nsmul] | succ n ih => simp only [succ_nsmul _ n.succ, _root_.add_lt_add (ih n.succ_pos) hab] instance Int.orderedSMul [LinearOrderedAddCommGroup M] : OrderedSMul ℤ M := OrderedSMul.mk'' fun n hn => by cases n · simp only [Int.ofNat_eq_coe, Int.natCast_pos, natCast_zsmul] at hn ⊢ exact strictMono_smul_left_of_pos hn · cases (Int.negSucc_not_pos _).1 hn section LinearOrderedSemiring variable [LinearOrderedSemiring R] [LinearOrderedAddCommMonoid M] [SMulWithZero R M] [OrderedSMul R M] {a : R} -- TODO: `LinearOrderedField M → OrderedSMul ℚ M` instance LinearOrderedSemiring.toOrderedSMul : OrderedSMul R R := OrderedSMul.mk'' fun _ => strictMono_mul_left_of_pos end LinearOrderedSemiring section LinearOrderedSemifield variable [LinearOrderedSemifield 𝕜] [OrderedAddCommMonoid M] [OrderedAddCommMonoid N] [MulActionWithZero 𝕜 M] [MulActionWithZero 𝕜 N] /-- To prove that a vector space over a linear ordered field is ordered, it suffices to verify only the first axiom of `OrderedSMul`. -/ theorem OrderedSMul.mk' (h : ∀ ⦃a b : M⦄ ⦃c : 𝕜⦄, a < b → 0 < c → c • a ≤ c • b) : OrderedSMul 𝕜 M := by have hlt' : ∀ (a b : M) (c : 𝕜), a < b → 0 < c → c • a < c • b := by refine fun a b c hab hc => (h hab hc).lt_of_ne ?_ rw [Ne, hc.ne'.isUnit.smul_left_cancel] exact hab.ne refine ⟨fun {a b c} => hlt' a b c, fun {a b c hab hc} => ?_⟩ obtain ⟨c, rfl⟩ := hc.ne'.isUnit rw [← inv_smul_smul c a, ← inv_smul_smul c b] refine hlt' _ _ _ hab (pos_of_mul_pos_right ?_ hc.le) simp only [c.mul_inv, zero_lt_one] instance [OrderedSMul 𝕜 M] [OrderedSMul 𝕜 N] : OrderedSMul 𝕜 (M × N) := OrderedSMul.mk' fun _ _ _ h hc => ⟨smul_le_smul_of_nonneg_left h.1.1 hc.le, smul_le_smul_of_nonneg_left h.1.2 hc.le⟩ instance Pi.orderedSMul {M : ι → Type*} [∀ i, OrderedAddCommMonoid (M i)] [∀ i, MulActionWithZero 𝕜 (M i)] [∀ i, OrderedSMul 𝕜 (M i)] : OrderedSMul 𝕜 (∀ i, M i) := OrderedSMul.mk' fun _ _ _ h hc i => smul_le_smul_of_nonneg_left (h.le i) hc.le end LinearOrderedSemifield section Invertible variable (α : Type*) {β : Type*} variable [Semiring α] [Invertible (2 : α)] [Lattice β] [AddCommGroup β] [Module α β] [CovariantClass β β (· + ·) (· ≤ ·)] lemma inf_eq_half_smul_add_sub_abs_sub (x y : β) : x ⊓ y = (⅟2 : α) • (x + y - |y - x|) := by rw [← two_nsmul_inf_eq_add_sub_abs_sub x y, two_smul, ← two_smul α, smul_smul, invOf_mul_self, one_smul] lemma sup_eq_half_smul_add_add_abs_sub (x y : β) : x ⊔ y = (⅟2 : α) • (x + y + |y - x|) := by rw [← two_nsmul_sup_eq_add_add_abs_sub x y, two_smul, ← two_smul α, smul_smul, invOf_mul_self, one_smul] end Invertible section DivisionSemiring variable (α : Type*) {β : Type*} variable [DivisionSemiring α] [NeZero (2 : α)] [Lattice β] [AddCommGroup β] [Module α β] [CovariantClass β β (· + ·) (· ≤ ·)] lemma inf_eq_half_smul_add_sub_abs_sub' (x y : β) : x ⊓ y = (2⁻¹ : α) • (x + y - |y - x|) := by letI := invertibleOfNonzero (two_ne_zero' α) exact inf_eq_half_smul_add_sub_abs_sub α x y lemma sup_eq_half_smul_add_add_abs_sub' (x y : β) : x ⊔ y = (2⁻¹ : α) • (x + y + |y - x|) := by letI := invertibleOfNonzero (two_ne_zero' α) exact sup_eq_half_smul_add_add_abs_sub α x y end DivisionSemiring
Algebra\Order\Module\Pointwise.lean
/- Copyright (c) 2023 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import Mathlib.Algebra.Order.Module.Defs import Mathlib.Data.Set.Pointwise.SMul import Mathlib.Order.Bounds.OrderIso /-! # Bounds on scalar multiplication of set This file proves order properties of pointwise operations of sets. -/ open scoped Pointwise variable {α β : Type*} section PosSMulMono variable [SMul α β] [Preorder α] [Preorder β] [Zero α] [PosSMulMono α β] {a : α} {s : Set β} lemma smul_lowerBounds_subset_lowerBounds_smul_of_nonneg (ha : 0 ≤ a) : a • lowerBounds s ⊆ lowerBounds (a • s) := (monotone_smul_left_of_nonneg ha).image_lowerBounds_subset_lowerBounds_image lemma smul_upperBounds_subset_upperBounds_smul_of_nonneg (ha : 0 ≤ a) : a • upperBounds s ⊆ upperBounds (a • s) := (monotone_smul_left_of_nonneg ha).image_upperBounds_subset_upperBounds_image lemma BddBelow.smul_of_nonneg (hs : BddBelow s) (ha : 0 ≤ a) : BddBelow (a • s) := (monotone_smul_left_of_nonneg ha).map_bddBelow hs lemma BddAbove.smul_of_nonneg (hs : BddAbove s) (ha : 0 ≤ a) : BddAbove (a • s) := (monotone_smul_left_of_nonneg ha).map_bddAbove hs end PosSMulMono section variable [Preorder α] [Preorder β] [GroupWithZero α] [Zero β] [MulActionWithZero α β] [PosSMulMono α β] [PosSMulReflectLE α β] {s : Set β} {a : α} @[simp] lemma lowerBounds_smul_of_pos (ha : 0 < a) : lowerBounds (a • s) = a • lowerBounds s := (OrderIso.smulRight ha).lowerBounds_image @[simp] lemma upperBounds_smul_of_pos (ha : 0 < a) : upperBounds (a • s) = a • upperBounds s := (OrderIso.smulRight ha).upperBounds_image @[simp] lemma bddBelow_smul_iff_of_pos (ha : 0 < a) : BddBelow (a • s) ↔ BddBelow s := (OrderIso.smulRight ha).bddBelow_image @[simp] lemma bddAbove_smul_iff_of_pos (ha : 0 < a) : BddAbove (a • s) ↔ BddAbove s := (OrderIso.smulRight ha).bddAbove_image end section OrderedRing variable [OrderedRing α] [OrderedAddCommGroup β] [Module α β] [PosSMulMono α β] {s : Set β} {a : α} lemma smul_lowerBounds_subset_upperBounds_smul (ha : a ≤ 0) : a • lowerBounds s ⊆ upperBounds (a • s) := (antitone_smul_left ha).image_lowerBounds_subset_upperBounds_image lemma smul_upperBounds_subset_lowerBounds_smul (ha : a ≤ 0) : a • upperBounds s ⊆ lowerBounds (a • s) := (antitone_smul_left ha).image_upperBounds_subset_lowerBounds_image lemma BddBelow.smul_of_nonpos (ha : a ≤ 0) (hs : BddBelow s) : BddAbove (a • s) := (antitone_smul_left ha).map_bddBelow hs lemma BddAbove.smul_of_nonpos (ha : a ≤ 0) (hs : BddAbove s) : BddBelow (a • s) := (antitone_smul_left ha).map_bddAbove hs end OrderedRing section LinearOrderedField variable [LinearOrderedField α] [OrderedAddCommGroup β] [Module α β] [PosSMulMono α β] {s : Set β} {a : α} @[simp] lemma lowerBounds_smul_of_neg (ha : a < 0) : lowerBounds (a • s) = a • upperBounds s := (OrderIso.smulRightDual β ha).upperBounds_image @[simp] lemma upperBounds_smul_of_neg (ha : a < 0) : upperBounds (a • s) = a • lowerBounds s := (OrderIso.smulRightDual β ha).lowerBounds_image @[simp] lemma bddBelow_smul_iff_of_neg (ha : a < 0) : BddBelow (a • s) ↔ BddAbove s := (OrderIso.smulRightDual β ha).bddAbove_image @[simp] lemma bddAbove_smul_iff_of_neg (ha : a < 0) : BddAbove (a • s) ↔ BddBelow s := (OrderIso.smulRightDual β ha).bddBelow_image end LinearOrderedField
Algebra\Order\Module\Rat.lean
/- Copyright (c) 2024 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import Mathlib.Algebra.Order.Module.Defs import Mathlib.Data.Rat.Cast.Order /-! # Monotonicity of the action by rational numbers -/ variable {α : Type*} section LinearOrderedAddCommGroup variable [LinearOrderedAddCommGroup α] @[simp] lemma abs_nnqsmul [DistribMulAction ℚ≥0 α] [PosSMulMono ℚ≥0 α] (q : ℚ≥0) (a : α) : |q • a| = q • |a| := by obtain ha | ha := le_total a 0 <;> simp [*, abs_of_nonneg, abs_of_nonpos, smul_nonneg, smul_nonpos_of_nonneg_of_nonpos] @[simp] lemma abs_qsmul [Module ℚ α] [PosSMulMono ℚ α] (q : ℚ) (a : α) : |q • a| = |q| • |a| := by obtain ha | ha := le_total a 0 <;> obtain hq | hq := le_total q 0 <;> simp [*, abs_of_nonneg, abs_of_nonpos, smul_nonneg, smul_nonpos_of_nonneg_of_nonpos, smul_nonpos_of_nonpos_of_nonneg, smul_nonneg_of_nonpos_of_nonpos] end LinearOrderedAddCommGroup section LinearOrderedSemifield variable [LinearOrderedSemifield α] instance LinearOrderedSemifield.toPosSMulStrictMono_rat : PosSMulStrictMono ℚ≥0 α where elim q hq a b hab := by rw [NNRat.smul_def, NNRat.smul_def]; exact mul_lt_mul_of_pos_left hab $ NNRat.cast_pos.2 hq end LinearOrderedSemifield section LinearOrderedField variable [LinearOrderedField α] instance LinearOrderedField.toPosSMulStrictMono_rat : PosSMulStrictMono ℚ α where elim q hq a b hab := by rw [Rat.smul_def, Rat.smul_def]; exact mul_lt_mul_of_pos_left hab $ Rat.cast_pos.2 hq end LinearOrderedField
Algebra\Order\Module\Synonym.lean
/- Copyright (c) 2021 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import Mathlib.Algebra.Module.Defs import Mathlib.Algebra.Order.GroupWithZero.Synonym import Mathlib.Algebra.Order.Ring.Synonym /-! # Action instances for `OrderDual` This file provides instances of algebraic actions for `OrderDual`. Note that the `SMul` instances are already defined in `Mathlib.Algebra.Order.Group.Synonym`. ## See also * `Mathlib.Algebra.Order.Group.Synonym` * `Mathlib.Algebra.Order.Ring.Synonym` -/ namespace OrderDual variable {α β γ : Type*} instance instSMulWithZero [Zero α] [Zero β] [SMulWithZero α β] : SMulWithZero αᵒᵈ β where zero_smul := zero_smul α smul_zero := smul_zero (M := α) instance instSMulWithZero' [Zero α] [Zero β] [SMulWithZero α β] : SMulWithZero α βᵒᵈ where zero_smul := zero_smul _ (M := β) smul_zero := smul_zero (A := β) @[to_additive] instance instMulAction [Monoid α] [MulAction α β] : MulAction αᵒᵈ β where one_smul := one_smul α mul_smul := mul_smul (α := α) @[to_additive] instance instMulAction' [Monoid α] [MulAction α β] : MulAction α βᵒᵈ where one_smul := one_smul _ (α := β) mul_smul := mul_smul (β := β) @[to_additive] instance instSMulCommClass [SMul β γ] [SMul α γ] [SMulCommClass α β γ] : SMulCommClass αᵒᵈ β γ := ‹SMulCommClass α β γ› @[to_additive] instance instSMulCommClass' [SMul β γ] [SMul α γ] [SMulCommClass α β γ] : SMulCommClass α βᵒᵈ γ := ‹SMulCommClass α β γ› @[to_additive] instance instSMulCommClass'' [SMul β γ] [SMul α γ] [SMulCommClass α β γ] : SMulCommClass α β γᵒᵈ := ‹SMulCommClass α β γ› @[to_additive instVAddAssocClass] instance instIsScalarTower [SMul α β] [SMul β γ] [SMul α γ] [IsScalarTower α β γ] : IsScalarTower αᵒᵈ β γ := ‹IsScalarTower α β γ› @[to_additive instVAddAssocClass'] instance instIsScalarTower' [SMul α β] [SMul β γ] [SMul α γ] [IsScalarTower α β γ] : IsScalarTower α βᵒᵈ γ := ‹IsScalarTower α β γ› @[to_additive instVAddAssocClass''] instance instIsScalarTower'' [SMul α β] [SMul β γ] [SMul α γ] [IsScalarTower α β γ] : IsScalarTower α β γᵒᵈ := ‹IsScalarTower α β γ› instance instMulActionWithZero [MonoidWithZero α] [AddMonoid β] [MulActionWithZero α β] : MulActionWithZero αᵒᵈ β := { OrderDual.instMulAction, OrderDual.instSMulWithZero with } instance instMulActionWithZero' [MonoidWithZero α] [AddMonoid β] [MulActionWithZero α β] : MulActionWithZero α βᵒᵈ := { OrderDual.instMulAction', OrderDual.instSMulWithZero' with } instance instDistribMulAction [MonoidWithZero α] [AddMonoid β] [DistribMulAction α β] : DistribMulAction αᵒᵈ β where smul_add := smul_add (M := α) smul_zero := smul_zero (M := α) instance instDistribMulAction' [MonoidWithZero α] [AddMonoid β] [DistribMulAction α β] : DistribMulAction α βᵒᵈ where smul_add := smul_add (A := β) smul_zero := smul_zero (A := β) instance instModule [Semiring α] [AddCommMonoid β] [Module α β] : Module αᵒᵈ β where add_smul := add_smul (R := α) zero_smul := zero_smul _ instance instModule' [Semiring α] [AddCommMonoid β] [Module α β] : Module α βᵒᵈ where add_smul := add_smul (M := β) zero_smul := zero_smul _ end OrderDual
Algebra\Order\Monoid\Basic.lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro, Johannes Hölzl -/ import Mathlib.Algebra.Order.Monoid.Defs import Mathlib.Algebra.Group.InjSurj import Mathlib.Order.Hom.Basic /-! # Ordered monoids This file develops some additional material on ordered monoids. -/ open Function universe u variable {α : Type u} {β : Type*} /-- Pullback an `OrderedCommMonoid` under an injective map. See note [reducible non-instances]. -/ @[to_additive (attr := reducible) "Pullback an `OrderedAddCommMonoid` under an injective map."] def Function.Injective.orderedCommMonoid [OrderedCommMonoid α] {β : Type*} [One β] [Mul β] [Pow β ℕ] (f : β → α) (hf : Function.Injective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (npow : ∀ (x) (n : ℕ), f (x ^ n) = f x ^ n) : OrderedCommMonoid β where toCommMonoid := hf.commMonoid f one mul npow toPartialOrder := PartialOrder.lift f hf mul_le_mul_left a b ab c := show f (c * a) ≤ f (c * b) by rw [mul, mul]; apply mul_le_mul_left'; exact ab /-- Pullback an `OrderedCancelCommMonoid` under an injective map. See note [reducible non-instances]. -/ @[to_additive (attr := reducible) Function.Injective.orderedCancelAddCommMonoid "Pullback an `OrderedCancelAddCommMonoid` under an injective map."] def Function.Injective.orderedCancelCommMonoid [OrderedCancelCommMonoid α] [One β] [Mul β] [Pow β ℕ] (f : β → α) (hf : Injective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (npow : ∀ (x) (n : ℕ), f (x ^ n) = f x ^ n) : OrderedCancelCommMonoid β where toOrderedCommMonoid := hf.orderedCommMonoid f one mul npow le_of_mul_le_mul_left a b c (bc : f (a * b) ≤ f (a * c)) := (mul_le_mul_iff_left (f a)).1 (by rwa [← mul, ← mul]) /-- Pullback a `LinearOrderedCommMonoid` under an injective map. See note [reducible non-instances]. -/ @[to_additive (attr := reducible) "Pullback an `OrderedAddCommMonoid` under an injective map."] def Function.Injective.linearOrderedCommMonoid [LinearOrderedCommMonoid α] {β : Type*} [One β] [Mul β] [Pow β ℕ] [Sup β] [Inf β] (f : β → α) (hf : Function.Injective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (npow : ∀ (x) (n : ℕ), f (x ^ n) = f x ^ n) (sup : ∀ x y, f (x ⊔ y) = max (f x) (f y)) (inf : ∀ x y, f (x ⊓ y) = min (f x) (f y)) : LinearOrderedCommMonoid β where toOrderedCommMonoid := hf.orderedCommMonoid f one mul npow __ := LinearOrder.lift f hf sup inf /-- Pullback a `LinearOrderedCancelCommMonoid` under an injective map. See note [reducible non-instances]. -/ @[to_additive (attr := reducible) Function.Injective.linearOrderedCancelAddCommMonoid "Pullback a `LinearOrderedCancelAddCommMonoid` under an injective map."] def Function.Injective.linearOrderedCancelCommMonoid [LinearOrderedCancelCommMonoid α] [One β] [Mul β] [Pow β ℕ] [Sup β] [Inf β] (f : β → α) (hf : Injective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (npow : ∀ (x) (n : ℕ), f (x ^ n) = f x ^ n) (hsup : ∀ x y, f (x ⊔ y) = max (f x) (f y)) (hinf : ∀ x y, f (x ⊓ y) = min (f x) (f y)) : LinearOrderedCancelCommMonoid β where toOrderedCancelCommMonoid := hf.orderedCancelCommMonoid f one mul npow __ := hf.linearOrderedCommMonoid f one mul npow hsup hinf -- TODO find a better home for the next two constructions. /-- The order embedding sending `b` to `a * b`, for some fixed `a`. See also `OrderIso.mulLeft` when working in an ordered group. -/ @[to_additive (attr := simps!) "The order embedding sending `b` to `a + b`, for some fixed `a`. See also `OrderIso.addLeft` when working in an additive ordered group."] def OrderEmbedding.mulLeft {α : Type*} [Mul α] [LinearOrder α] [CovariantClass α α (· * ·) (· < ·)] (m : α) : α ↪o α := OrderEmbedding.ofStrictMono (fun n => m * n) fun _ _ w => mul_lt_mul_left' w m /-- The order embedding sending `b` to `b * a`, for some fixed `a`. See also `OrderIso.mulRight` when working in an ordered group. -/ @[to_additive (attr := simps!) "The order embedding sending `b` to `b + a`, for some fixed `a`. See also `OrderIso.addRight` when working in an additive ordered group."] def OrderEmbedding.mulRight {α : Type*} [Mul α] [LinearOrder α] [CovariantClass α α (swap (· * ·)) (· < ·)] (m : α) : α ↪o α := OrderEmbedding.ofStrictMono (fun n => n * m) fun _ _ w => mul_lt_mul_right' w m
Algebra\Order\Monoid\Defs.lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro, Johannes Hölzl -/ import Mathlib.Algebra.Order.Monoid.Unbundled.Basic /-! # Ordered monoids This file provides the definitions of ordered monoids. -/ open Function variable {α β : Type*} /-- An ordered (additive) commutative monoid is a commutative monoid with a partial order such that addition is monotone. -/ class OrderedAddCommMonoid (α : Type*) extends AddCommMonoid α, PartialOrder α where protected add_le_add_left : ∀ a b : α, a ≤ b → ∀ c, c + a ≤ c + b /-- An ordered commutative monoid is a commutative monoid with a partial order such that multiplication is monotone. -/ @[to_additive] class OrderedCommMonoid (α : Type*) extends CommMonoid α, PartialOrder α where protected mul_le_mul_left : ∀ a b : α, a ≤ b → ∀ c, c * a ≤ c * b section OrderedCommMonoid variable [OrderedCommMonoid α] @[to_additive] instance OrderedCommMonoid.toCovariantClassLeft : CovariantClass α α (· * ·) (· ≤ ·) where elim := fun a _ _ bc ↦ OrderedCommMonoid.mul_le_mul_left _ _ bc a @[to_additive] theorem OrderedCommMonoid.toCovariantClassRight (M : Type*) [OrderedCommMonoid M] : CovariantClass M M (swap (· * ·)) (· ≤ ·) := inferInstance end OrderedCommMonoid /-- An ordered cancellative additive commutative monoid is a partially ordered commutative additive monoid in which addition is cancellative and monotone. -/ class OrderedCancelAddCommMonoid (α : Type*) extends OrderedAddCommMonoid α where protected le_of_add_le_add_left : ∀ a b c : α, a + b ≤ a + c → b ≤ c /-- An ordered cancellative commutative monoid is a partially ordered commutative monoid in which multiplication is cancellative and monotone. -/ @[to_additive OrderedCancelAddCommMonoid] class OrderedCancelCommMonoid (α : Type*) extends OrderedCommMonoid α where protected le_of_mul_le_mul_left : ∀ a b c : α, a * b ≤ a * c → b ≤ c section OrderedCancelCommMonoid variable [OrderedCancelCommMonoid α] -- See note [lower instance priority] @[to_additive] instance (priority := 200) OrderedCancelCommMonoid.toContravariantClassLeLeft : ContravariantClass α α (· * ·) (· ≤ ·) := ⟨OrderedCancelCommMonoid.le_of_mul_le_mul_left⟩ @[to_additive] instance OrderedCancelCommMonoid.toContravariantClassLeft : ContravariantClass α α (· * ·) (· < ·) where elim := contravariant_lt_of_contravariant_le α α _ ContravariantClass.elim @[to_additive] theorem OrderedCancelCommMonoid.toContravariantClassRight : ContravariantClass α α (swap (· * ·)) (· < ·) := inferInstance -- See note [lower instance priority] @[to_additive OrderedCancelAddCommMonoid.toCancelAddCommMonoid] instance (priority := 100) OrderedCancelCommMonoid.toCancelCommMonoid : CancelCommMonoid α := { ‹OrderedCancelCommMonoid α› with mul_left_cancel := fun a b c h => (le_of_mul_le_mul_left' h.le).antisymm <| le_of_mul_le_mul_left' h.ge } end OrderedCancelCommMonoid /-- A linearly ordered additive commutative monoid. -/ class LinearOrderedAddCommMonoid (α : Type*) extends OrderedAddCommMonoid α, LinearOrder α /-- A linearly ordered commutative monoid. -/ @[to_additive] class LinearOrderedCommMonoid (α : Type*) extends OrderedCommMonoid α, LinearOrder α /-- A linearly ordered cancellative additive commutative monoid is an additive commutative monoid with a decidable linear order in which addition is cancellative and monotone. -/ class LinearOrderedCancelAddCommMonoid (α : Type*) extends OrderedCancelAddCommMonoid α, LinearOrderedAddCommMonoid α /-- A linearly ordered cancellative commutative monoid is a commutative monoid with a linear order in which multiplication is cancellative and monotone. -/ @[to_additive LinearOrderedCancelAddCommMonoid] class LinearOrderedCancelCommMonoid (α : Type*) extends OrderedCancelCommMonoid α, LinearOrderedCommMonoid α attribute [to_additive existing] LinearOrderedCancelCommMonoid.toLinearOrderedCommMonoid variable [LinearOrderedCommMonoid α] {a : α} @[to_additive (attr := simp)] theorem one_le_mul_self_iff : 1 ≤ a * a ↔ 1 ≤ a := ⟨(fun h ↦ by push_neg at h ⊢; exact mul_lt_one' h h).mtr, fun h ↦ one_le_mul h h⟩ @[to_additive (attr := simp)] theorem one_lt_mul_self_iff : 1 < a * a ↔ 1 < a := ⟨(fun h ↦ by push_neg at h ⊢; exact mul_le_one' h h).mtr, fun h ↦ one_lt_mul'' h h⟩ @[to_additive (attr := simp)] theorem mul_self_le_one_iff : a * a ≤ 1 ↔ a ≤ 1 := by simp [← not_iff_not] @[to_additive (attr := simp)] theorem mul_self_lt_one_iff : a * a < 1 ↔ a < 1 := by simp [← not_iff_not]
Algebra\Order\Monoid\NatCast.lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro, Johannes Hölzl, Yuyang Zhao -/ import Mathlib.Algebra.Order.Monoid.Unbundled.Basic import Mathlib.Algebra.Order.ZeroLEOne import Mathlib.Data.Nat.Cast.Defs /-! # Order of numerals in an `AddMonoidWithOne`. -/ variable {α : Type*} open Function lemma lt_add_one [One α] [AddZeroClass α] [PartialOrder α] [ZeroLEOneClass α] [NeZero (1 : α)] [CovariantClass α α (·+·) (·<·)] (a : α) : a < a + 1 := lt_add_of_pos_right _ zero_lt_one lemma lt_one_add [One α] [AddZeroClass α] [PartialOrder α] [ZeroLEOneClass α] [NeZero (1 : α)] [CovariantClass α α (swap (·+·)) (·<·)] (a : α) : a < 1 + a := lt_add_of_pos_left _ zero_lt_one variable [AddMonoidWithOne α] lemma zero_le_two [Preorder α] [ZeroLEOneClass α] [CovariantClass α α (·+·) (·≤·)] : (0 : α) ≤ 2 := by rw [← one_add_one_eq_two] exact add_nonneg zero_le_one zero_le_one lemma zero_le_three [Preorder α] [ZeroLEOneClass α] [CovariantClass α α (·+·) (·≤·)] : (0 : α) ≤ 3 := by rw [← two_add_one_eq_three] exact add_nonneg zero_le_two zero_le_one lemma zero_le_four [Preorder α] [ZeroLEOneClass α] [CovariantClass α α (·+·) (·≤·)] : (0 : α) ≤ 4 := by rw [← three_add_one_eq_four] exact add_nonneg zero_le_three zero_le_one lemma one_le_two [LE α] [ZeroLEOneClass α] [CovariantClass α α (·+·) (·≤·)] : (1 : α) ≤ 2 := calc (1 : α) = 1 + 0 := (add_zero 1).symm _ ≤ 1 + 1 := add_le_add_left zero_le_one _ _ = 2 := one_add_one_eq_two lemma one_le_two' [LE α] [ZeroLEOneClass α] [CovariantClass α α (swap (·+·)) (·≤·)] : (1 : α) ≤ 2 := calc (1 : α) = 0 + 1 := (zero_add 1).symm _ ≤ 1 + 1 := add_le_add_right zero_le_one _ _ = 2 := one_add_one_eq_two section variable [PartialOrder α] [ZeroLEOneClass α] [NeZero (1 : α)] section variable [CovariantClass α α (·+·) (·≤·)] /-- See `zero_lt_two'` for a version with the type explicit. -/ @[simp] lemma zero_lt_two : (0 : α) < 2 := zero_lt_one.trans_le one_le_two /-- See `zero_lt_three'` for a version with the type explicit. -/ @[simp] lemma zero_lt_three : (0 : α) < 3 := by rw [← two_add_one_eq_three] exact lt_add_of_lt_of_nonneg zero_lt_two zero_le_one /-- See `zero_lt_four'` for a version with the type explicit. -/ @[simp] lemma zero_lt_four : (0 : α) < 4 := by rw [← three_add_one_eq_four] exact lt_add_of_lt_of_nonneg zero_lt_three zero_le_one variable (α) /-- See `zero_lt_two` for a version with the type implicit. -/ lemma zero_lt_two' : (0 : α) < 2 := zero_lt_two /-- See `zero_lt_three` for a version with the type implicit. -/ lemma zero_lt_three' : (0 : α) < 3 := zero_lt_three /-- See `zero_lt_four` for a version with the type implicit. -/ lemma zero_lt_four' : (0 : α) < 4 := zero_lt_four instance ZeroLEOneClass.neZero.two : NeZero (2 : α) := ⟨zero_lt_two.ne'⟩ instance ZeroLEOneClass.neZero.three : NeZero (3 : α) := ⟨zero_lt_three.ne'⟩ instance ZeroLEOneClass.neZero.four : NeZero (4 : α) := ⟨zero_lt_four.ne'⟩ end lemma one_lt_two [CovariantClass α α (·+·) (·<·)] : (1 : α) < 2 := by rw [← one_add_one_eq_two] exact lt_add_one _ end alias two_pos := zero_lt_two alias three_pos := zero_lt_three alias four_pos := zero_lt_four
Algebra\Order\Monoid\OrderDual.lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro, Johannes Hölzl -/ import Mathlib.Algebra.Order.Group.Synonym import Mathlib.Algebra.Order.Monoid.Unbundled.OrderDual import Mathlib.Algebra.Order.Monoid.Defs /-! # Ordered monoid structures on the order dual. -/ universe u variable {α : Type u} open Function namespace OrderDual @[to_additive] instance orderedCommMonoid [OrderedCommMonoid α] : OrderedCommMonoid αᵒᵈ := { mul_le_mul_left := fun _ _ h c => mul_le_mul_left' h c } @[to_additive OrderDual.OrderedCancelAddCommMonoid.to_contravariantClass] instance OrderedCancelCommMonoid.to_contravariantClass [OrderedCancelCommMonoid α] : ContravariantClass αᵒᵈ αᵒᵈ HMul.hMul LE.le where elim a b c := OrderedCancelCommMonoid.le_of_mul_le_mul_left (α := α) a c b -- Porting note: Lean 3 to_additive name omits first namespace part @[to_additive] instance orderedCancelCommMonoid [OrderedCancelCommMonoid α] : OrderedCancelCommMonoid αᵒᵈ := { le_of_mul_le_mul_left := fun _ _ _ : α => le_of_mul_le_mul_left' } @[to_additive] instance linearOrderedCancelCommMonoid [LinearOrderedCancelCommMonoid α] : LinearOrderedCancelCommMonoid αᵒᵈ := { OrderDual.instLinearOrder α, OrderDual.orderedCancelCommMonoid with } @[to_additive] instance linearOrderedCommMonoid [LinearOrderedCommMonoid α] : LinearOrderedCommMonoid αᵒᵈ := { OrderDual.instLinearOrder α, OrderDual.orderedCommMonoid with } end OrderDual
Algebra\Order\Monoid\Prod.lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro, Johannes Hölzl -/ import Mathlib.Algebra.Group.Prod import Mathlib.Algebra.Order.Group.Synonym import Mathlib.Algebra.Order.Monoid.Canonical.Defs import Mathlib.Data.Prod.Lex /-! # Products of ordered monoids -/ assert_not_exists MonoidWithZero namespace Prod variable {α β : Type*} @[to_additive] instance [OrderedCommMonoid α] [OrderedCommMonoid β] : OrderedCommMonoid (α × β) where mul_le_mul_left _ _ h _ := ⟨mul_le_mul_left' h.1 _, mul_le_mul_left' h.2 _⟩ @[to_additive] instance instOrderedCancelCommMonoid [OrderedCancelCommMonoid α] [OrderedCancelCommMonoid β] : OrderedCancelCommMonoid (α × β) := { (inferInstance : OrderedCommMonoid (α × β)) with le_of_mul_le_mul_left := fun _ _ _ h ↦ ⟨le_of_mul_le_mul_left' h.1, le_of_mul_le_mul_left' h.2⟩ } @[to_additive] instance [LE α] [LE β] [Mul α] [Mul β] [ExistsMulOfLE α] [ExistsMulOfLE β] : ExistsMulOfLE (α × β) := ⟨fun h => let ⟨c, hc⟩ := exists_mul_of_le h.1 let ⟨d, hd⟩ := exists_mul_of_le h.2 ⟨(c, d), Prod.ext hc hd⟩⟩ @[to_additive] instance [CanonicallyOrderedCommMonoid α] [CanonicallyOrderedCommMonoid β] : CanonicallyOrderedCommMonoid (α × β) := { (inferInstance : OrderedCommMonoid _), (inferInstance : OrderBot _), (inferInstance : ExistsMulOfLE _) with le_self_mul := fun _ _ ↦ ⟨le_self_mul, le_self_mul⟩ } namespace Lex @[to_additive] instance orderedCommMonoid [OrderedCommMonoid α] [CovariantClass α α (· * ·) (· < ·)] [OrderedCommMonoid β] : OrderedCommMonoid (α ×ₗ β) where mul_le_mul_left _ _ hxy z := ((le_iff _ _).1 hxy).elim (fun hxy => left _ _ <| mul_lt_mul_left' hxy _) -- Note: the `congr_arg` used to be `rw [hxy.1]` before #8386 -- but the definition of `Mul.mul` got unfolded differently. (fun hxy => (le_iff _ _).2 <| Or.inr ⟨congr_arg (z.1 * ·) hxy.1, mul_le_mul_left' hxy.2 _⟩) @[to_additive] instance orderedCancelCommMonoid [OrderedCancelCommMonoid α] [OrderedCancelCommMonoid β] : OrderedCancelCommMonoid (α ×ₗ β) where mul_le_mul_left _ _ := mul_le_mul_left' le_of_mul_le_mul_left _ _ _ hxyz := ((le_iff _ _).1 hxyz).elim (fun hxy => left _ _ <| lt_of_mul_lt_mul_left' hxy) (fun hxy => (le_iff _ _).2 <| Or.inr ⟨mul_left_cancel hxy.1, le_of_mul_le_mul_left' hxy.2⟩) @[to_additive] instance linearOrderedCancelCommMonoid [LinearOrderedCancelCommMonoid α] [LinearOrderedCancelCommMonoid β] : LinearOrderedCancelCommMonoid (α ×ₗ β) where __ : LinearOrder (α ×ₗ β) := inferInstance __ : OrderedCancelCommMonoid (α ×ₗ β) := inferInstance end Lex end Prod
Algebra\Order\Monoid\Submonoid.lean
/- Copyright (c) 2021 Damiano Testa. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Damiano Testa -/ import Mathlib.Algebra.Group.Submonoid.Operations import Mathlib.Algebra.Order.GroupWithZero.Unbundled import Mathlib.Algebra.Order.Monoid.Basic import Mathlib.Algebra.Order.ZeroLEOne /-! # Ordered instances on submonoids -/ namespace SubmonoidClass variable {M S : Type*} [SetLike S M] -- Prefer subclasses of `Monoid` over subclasses of `SubmonoidClass`. /-- A submonoid of an `OrderedCommMonoid` is an `OrderedCommMonoid`. -/ @[to_additive "An `AddSubmonoid` of an `OrderedAddCommMonoid` is an `OrderedAddCommMonoid`."] instance (priority := 75) toOrderedCommMonoid [OrderedCommMonoid M] [SubmonoidClass S M] (s : S) : OrderedCommMonoid s := Subtype.coe_injective.orderedCommMonoid (↑) rfl (fun _ _ => rfl) fun _ _ => rfl -- Prefer subclasses of `Monoid` over subclasses of `SubmonoidClass`. /-- A submonoid of a `LinearOrderedCommMonoid` is a `LinearOrderedCommMonoid`. -/ @[to_additive "An `AddSubmonoid` of a `LinearOrderedAddCommMonoid` is a `LinearOrderedAddCommMonoid`."] instance (priority := 75) toLinearOrderedCommMonoid [LinearOrderedCommMonoid M] [SubmonoidClass S M] (s : S) : LinearOrderedCommMonoid s := Subtype.coe_injective.linearOrderedCommMonoid (↑) rfl (fun _ _ => rfl) (fun _ _ => rfl) (fun _ _ => rfl) fun _ _ => rfl -- Prefer subclasses of `Monoid` over subclasses of `SubmonoidClass`. /-- A submonoid of an `OrderedCancelCommMonoid` is an `OrderedCancelCommMonoid`. -/ @[to_additive AddSubmonoidClass.toOrderedCancelAddCommMonoid "An `AddSubmonoid` of an `OrderedCancelAddCommMonoid` is an `OrderedCancelAddCommMonoid`."] instance (priority := 75) toOrderedCancelCommMonoid [OrderedCancelCommMonoid M] [SubmonoidClass S M] (s : S) : OrderedCancelCommMonoid s := Subtype.coe_injective.orderedCancelCommMonoid (↑) rfl (fun _ _ => rfl) fun _ _ => rfl -- Prefer subclasses of `Monoid` over subclasses of `SubmonoidClass`. /-- A submonoid of a `LinearOrderedCancelCommMonoid` is a `LinearOrderedCancelCommMonoid`. -/ @[to_additive AddSubmonoidClass.toLinearOrderedCancelAddCommMonoid "An `AddSubmonoid` of a `LinearOrderedCancelAddCommMonoid` is a `LinearOrderedCancelAddCommMonoid`."] instance (priority := 75) toLinearOrderedCancelCommMonoid [LinearOrderedCancelCommMonoid M] [SubmonoidClass S M] (s : S) : LinearOrderedCancelCommMonoid s := Subtype.coe_injective.linearOrderedCancelCommMonoid (↑) rfl (fun _ _ => rfl) (fun _ _ => rfl) (fun _ _ => rfl) fun _ _ => rfl end SubmonoidClass namespace Submonoid variable {M : Type*} /-- A submonoid of an `OrderedCommMonoid` is an `OrderedCommMonoid`. -/ @[to_additive "An `AddSubmonoid` of an `OrderedAddCommMonoid` is an `OrderedAddCommMonoid`."] instance toOrderedCommMonoid [OrderedCommMonoid M] (S : Submonoid M) : OrderedCommMonoid S := Subtype.coe_injective.orderedCommMonoid (↑) rfl (fun _ _ => rfl) fun _ _ => rfl /-- A submonoid of a `LinearOrderedCommMonoid` is a `LinearOrderedCommMonoid`. -/ @[to_additive "An `AddSubmonoid` of a `LinearOrderedAddCommMonoid` is a `LinearOrderedAddCommMonoid`."] instance toLinearOrderedCommMonoid [LinearOrderedCommMonoid M] (S : Submonoid M) : LinearOrderedCommMonoid S := Subtype.coe_injective.linearOrderedCommMonoid (↑) rfl (fun _ _ => rfl) (fun _ _ => rfl) (fun _ _ => rfl) fun _ _ => rfl /-- A submonoid of an `OrderedCancelCommMonoid` is an `OrderedCancelCommMonoid`. -/ @[to_additive AddSubmonoid.toOrderedCancelAddCommMonoid "An `AddSubmonoid` of an `OrderedCancelAddCommMonoid` is an `OrderedCancelAddCommMonoid`."] instance toOrderedCancelCommMonoid [OrderedCancelCommMonoid M] (S : Submonoid M) : OrderedCancelCommMonoid S := Subtype.coe_injective.orderedCancelCommMonoid (↑) rfl (fun _ _ => rfl) fun _ _ => rfl /-- A submonoid of a `LinearOrderedCancelCommMonoid` is a `LinearOrderedCancelCommMonoid`. -/ @[to_additive AddSubmonoid.toLinearOrderedCancelAddCommMonoid "An `AddSubmonoid` of a `LinearOrderedCancelAddCommMonoid` is a `LinearOrderedCancelAddCommMonoid`."] instance toLinearOrderedCancelCommMonoid [LinearOrderedCancelCommMonoid M] (S : Submonoid M) : LinearOrderedCancelCommMonoid S := Subtype.coe_injective.linearOrderedCancelCommMonoid (↑) rfl (fun _ _ => rfl) (fun _ _ => rfl) (fun _ _ => rfl) fun _ _ => rfl section Preorder variable (M) variable [Monoid M] [Preorder M] [CovariantClass M M (· * ·) (· ≤ ·)] {a : M} /-- The submonoid of elements greater than `1`. -/ @[to_additive (attr := simps) nonneg "The submonoid of nonnegative elements."] def oneLE : Submonoid M where carrier := Set.Ici 1 mul_mem' := one_le_mul one_mem' := le_rfl variable {M} @[to_additive (attr := simp) mem_nonneg] lemma mem_oneLE : a ∈ oneLE M ↔ 1 ≤ a := Iff.rfl end Preorder section MulZeroClass variable (α) [MulZeroOneClass α] [PartialOrder α] [PosMulStrictMono α] [ZeroLEOneClass α] [NeZero (1 : α)] {a : α} /-- The submonoid of positive elements. -/ @[simps] def pos : Submonoid α where carrier := Set.Ioi 0 one_mem' := zero_lt_one mul_mem' := mul_pos variable {α} @[simp] lemma mem_pos : a ∈ pos α ↔ 0 < a := Iff.rfl end MulZeroClass end Submonoid
Algebra\Order\Monoid\ToMulBot.lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro, Johannes Hölzl -/ import Mathlib.Algebra.Order.GroupWithZero.Canonical import Mathlib.Algebra.Order.Monoid.TypeTags import Mathlib.Algebra.Group.Equiv.Basic import Mathlib.Algebra.Order.Monoid.Unbundled.WithTop /-! Making an additive monoid multiplicative then adding a zero is the same as adding a bottom element then making it multiplicative. -/ universe u variable {α : Type u} namespace WithZero variable [Add α] /-- Making an additive monoid multiplicative then adding a zero is the same as adding a bottom element then making it multiplicative. -/ def toMulBot : WithZero (Multiplicative α) ≃* Multiplicative (WithBot α) := MulEquiv.refl _ @[simp] theorem toMulBot_zero : toMulBot (0 : WithZero (Multiplicative α)) = Multiplicative.ofAdd ⊥ := rfl @[simp] theorem toMulBot_coe (x : Multiplicative α) : toMulBot ↑x = Multiplicative.ofAdd (↑(Multiplicative.toAdd x) : WithBot α) := rfl @[simp] theorem toMulBot_symm_bot : toMulBot.symm (Multiplicative.ofAdd (⊥ : WithBot α)) = 0 := rfl @[simp] theorem toMulBot_coe_ofAdd (x : α) : toMulBot.symm (Multiplicative.ofAdd (x : WithBot α)) = Multiplicative.ofAdd x := rfl variable [Preorder α] (a b : WithZero (Multiplicative α)) theorem toMulBot_strictMono : StrictMono (@toMulBot α _) := fun _ _ => id @[simp] theorem toMulBot_le : toMulBot a ≤ toMulBot b ↔ a ≤ b := Iff.rfl @[simp] theorem toMulBot_lt : toMulBot a < toMulBot b ↔ a < b := Iff.rfl end WithZero
Algebra\Order\Monoid\TypeTags.lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro, Johannes Hölzl -/ import Mathlib.Algebra.Group.TypeTags import Mathlib.Algebra.Order.Monoid.Defs import Mathlib.Algebra.Order.Monoid.Canonical.Defs /-! # Ordered monoid structures on `Multiplicative α` and `Additive α`. -/ variable {α : Type*} instance : ∀ [LE α], LE (Multiplicative α) := fun {inst} => inst instance : ∀ [LE α], LE (Additive α) := fun {inst} => inst instance : ∀ [LT α], LT (Multiplicative α) := fun {inst} => inst instance : ∀ [LT α], LT (Additive α) := fun {inst} => inst instance Multiplicative.preorder : ∀ [Preorder α], Preorder (Multiplicative α) := fun {inst} => inst instance Additive.preorder : ∀ [Preorder α], Preorder (Additive α) := fun {inst} => inst instance Multiplicative.partialOrder : ∀ [PartialOrder α], PartialOrder (Multiplicative α) := fun {inst} => inst instance Additive.partialOrder : ∀ [PartialOrder α], PartialOrder (Additive α) := fun {inst} => inst instance Multiplicative.linearOrder : ∀ [LinearOrder α], LinearOrder (Multiplicative α) := fun {inst} => inst instance Additive.linearOrder : ∀ [LinearOrder α], LinearOrder (Additive α) := fun {inst} => inst instance Multiplicative.orderBot [LE α] : ∀ [OrderBot α], OrderBot (Multiplicative α) := fun {inst} => inst instance Additive.orderBot [LE α] : ∀ [OrderBot α], OrderBot (Additive α) := fun {inst} => inst instance Multiplicative.orderTop [LE α] : ∀ [OrderTop α], OrderTop (Multiplicative α) := fun {inst} => inst instance Additive.orderTop [LE α] : ∀ [OrderTop α], OrderTop (Additive α) := fun {inst} => inst instance Multiplicative.boundedOrder [LE α] : ∀ [BoundedOrder α], BoundedOrder (Multiplicative α) := fun {inst} => inst instance Additive.boundedOrder [LE α] : ∀ [BoundedOrder α], BoundedOrder (Additive α) := fun {inst} => inst instance Multiplicative.orderedCommMonoid [OrderedAddCommMonoid α] : OrderedCommMonoid (Multiplicative α) := { Multiplicative.partialOrder, Multiplicative.commMonoid with mul_le_mul_left := @OrderedAddCommMonoid.add_le_add_left α _ } instance Additive.orderedAddCommMonoid [OrderedCommMonoid α] : OrderedAddCommMonoid (Additive α) := { Additive.partialOrder, Additive.addCommMonoid with add_le_add_left := @OrderedCommMonoid.mul_le_mul_left α _ } instance Multiplicative.orderedCancelAddCommMonoid [OrderedCancelAddCommMonoid α] : OrderedCancelCommMonoid (Multiplicative α) := { Multiplicative.orderedCommMonoid with le_of_mul_le_mul_left := @OrderedCancelAddCommMonoid.le_of_add_le_add_left α _ } instance Additive.orderedCancelAddCommMonoid [OrderedCancelCommMonoid α] : OrderedCancelAddCommMonoid (Additive α) := { Additive.orderedAddCommMonoid with le_of_add_le_add_left := @OrderedCancelCommMonoid.le_of_mul_le_mul_left α _ } instance Multiplicative.linearOrderedCommMonoid [LinearOrderedAddCommMonoid α] : LinearOrderedCommMonoid (Multiplicative α) := { Multiplicative.linearOrder, Multiplicative.orderedCommMonoid with } instance Additive.linearOrderedAddCommMonoid [LinearOrderedCommMonoid α] : LinearOrderedAddCommMonoid (Additive α) := { Additive.linearOrder, Additive.orderedAddCommMonoid with } instance Multiplicative.existsMulOfLe [Add α] [LE α] [ExistsAddOfLE α] : ExistsMulOfLE (Multiplicative α) := ⟨@exists_add_of_le α _ _ _⟩ instance Additive.existsAddOfLe [Mul α] [LE α] [ExistsMulOfLE α] : ExistsAddOfLE (Additive α) := ⟨@exists_mul_of_le α _ _ _⟩ instance Multiplicative.canonicallyOrderedCommMonoid [CanonicallyOrderedAddCommMonoid α] : CanonicallyOrderedCommMonoid (Multiplicative α) := { Multiplicative.orderedCommMonoid, Multiplicative.orderBot, Multiplicative.existsMulOfLe with le_self_mul := @le_self_add α _ } instance Additive.canonicallyOrderedAddCommMonoid [CanonicallyOrderedCommMonoid α] : CanonicallyOrderedAddCommMonoid (Additive α) := { Additive.orderedAddCommMonoid, Additive.orderBot, Additive.existsAddOfLe with le_self_add := @le_self_mul α _ } instance Multiplicative.canonicallyLinearOrderedCommMonoid [CanonicallyLinearOrderedAddCommMonoid α] : CanonicallyLinearOrderedCommMonoid (Multiplicative α) := { Multiplicative.canonicallyOrderedCommMonoid, Multiplicative.linearOrder with } instance [CanonicallyLinearOrderedCommMonoid α] : CanonicallyLinearOrderedAddCommMonoid (Additive α) := { Additive.canonicallyOrderedAddCommMonoid, Additive.linearOrder with } namespace Additive variable [Preorder α] @[simp] theorem ofMul_le {a b : α} : ofMul a ≤ ofMul b ↔ a ≤ b := Iff.rfl @[simp] theorem ofMul_lt {a b : α} : ofMul a < ofMul b ↔ a < b := Iff.rfl @[simp] theorem toMul_le {a b : Additive α} : toMul a ≤ toMul b ↔ a ≤ b := Iff.rfl @[simp] theorem toMul_lt {a b : Additive α} : toMul a < toMul b ↔ a < b := Iff.rfl end Additive namespace Multiplicative variable [Preorder α] @[simp] theorem ofAdd_le {a b : α} : ofAdd a ≤ ofAdd b ↔ a ≤ b := Iff.rfl @[simp] theorem ofAdd_lt {a b : α} : ofAdd a < ofAdd b ↔ a < b := Iff.rfl @[simp] theorem toAdd_le {a b : Multiplicative α} : toAdd a ≤ toAdd b ↔ a ≤ b := Iff.rfl @[simp] theorem toAdd_lt {a b : Multiplicative α} : toAdd a < toAdd b ↔ a < b := Iff.rfl end Multiplicative
Algebra\Order\Monoid\Units.lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro, Johannes Hölzl -/ import Mathlib.Order.Hom.Basic import Mathlib.Order.MinMax import Mathlib.Algebra.Group.Units /-! # Units in ordered monoids -/ namespace Units variable {α : Type*} @[to_additive] instance [Monoid α] [Preorder α] : Preorder αˣ := Preorder.lift val @[to_additive (attr := simp, norm_cast)] theorem val_le_val [Monoid α] [Preorder α] {a b : αˣ} : (a : α) ≤ b ↔ a ≤ b := Iff.rfl @[to_additive (attr := simp, norm_cast)] theorem val_lt_val [Monoid α] [Preorder α] {a b : αˣ} : (a : α) < b ↔ a < b := Iff.rfl @[to_additive] instance instPartialOrderUnits [Monoid α] [PartialOrder α] : PartialOrder αˣ := PartialOrder.lift val Units.ext @[to_additive] instance [Monoid α] [LinearOrder α] : LinearOrder αˣ := LinearOrder.lift' val Units.ext /-- `val : αˣ → α` as an order embedding. -/ @[to_additive (attr := simps (config := .asFn)) "`val : add_units α → α` as an order embedding."] def orderEmbeddingVal [Monoid α] [LinearOrder α] : αˣ ↪o α := ⟨⟨val, ext⟩, Iff.rfl⟩ @[to_additive (attr := simp, norm_cast)] theorem max_val [Monoid α] [LinearOrder α] {a b : αˣ} : (max a b).val = max a.val b.val := Monotone.map_max orderEmbeddingVal.monotone @[to_additive (attr := simp, norm_cast)] theorem min_val [Monoid α] [LinearOrder α] {a b : αˣ} : (min a b).val = min a.val b.val := Monotone.map_min orderEmbeddingVal.monotone end Units
Algebra\Order\Monoid\WithTop.lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro, Johannes Hölzl -/ import Mathlib.Algebra.Order.Monoid.Unbundled.WithTop import Mathlib.Algebra.Order.Monoid.Canonical.Defs /-! # Adjoining top/bottom elements to ordered monoids. -/ universe u v variable {α : Type u} {β : Type v} open Function namespace WithTop instance orderedAddCommMonoid [OrderedAddCommMonoid α] : OrderedAddCommMonoid (WithTop α) where add_le_add_left _ _ := add_le_add_left instance canonicallyOrderedAddCommMonoid [CanonicallyOrderedAddCommMonoid α] : CanonicallyOrderedAddCommMonoid (WithTop α) := { WithTop.orderBot, WithTop.orderedAddCommMonoid, WithTop.existsAddOfLE with le_self_add := fun a b => match a, b with | ⊤, ⊤ => le_rfl | (a : α), ⊤ => le_top | (a : α), (b : α) => WithTop.coe_le_coe.2 le_self_add | ⊤, (b : α) => le_rfl } instance [CanonicallyLinearOrderedAddCommMonoid α] : CanonicallyLinearOrderedAddCommMonoid (WithTop α) := { WithTop.canonicallyOrderedAddCommMonoid, WithTop.linearOrder with } end WithTop namespace WithBot instance orderedAddCommMonoid [OrderedAddCommMonoid α] : OrderedAddCommMonoid (WithBot α) := { WithBot.partialOrder, WithBot.addCommMonoid with add_le_add_left := fun _ _ h c => add_le_add_left h c } instance linearOrderedAddCommMonoid [LinearOrderedAddCommMonoid α] : LinearOrderedAddCommMonoid (WithBot α) := { WithBot.linearOrder, WithBot.orderedAddCommMonoid with } end WithBot
Algebra\Order\Monoid\Canonical\Defs.lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro, Johannes Hölzl -/ import Mathlib.Algebra.Order.Monoid.Defs import Mathlib.Algebra.Order.Monoid.Unbundled.ExistsOfLE import Mathlib.Algebra.NeZero import Mathlib.Order.BoundedOrder /-! # Canonically ordered monoids -/ universe u variable {α : Type u} /-- A canonically ordered additive monoid is an ordered commutative additive monoid in which the ordering coincides with the subtractibility relation, which is to say, `a ≤ b` iff there exists `c` with `b = a + c`. This is satisfied by the natural numbers, for example, but not the integers or other nontrivial `OrderedAddCommGroup`s. -/ class CanonicallyOrderedAddCommMonoid (α : Type*) extends OrderedAddCommMonoid α, OrderBot α where /-- For `a ≤ b`, there is a `c` so `b = a + c`. -/ protected exists_add_of_le : ∀ {a b : α}, a ≤ b → ∃ c, b = a + c /-- For any `a` and `b`, `a ≤ a + b` -/ protected le_self_add : ∀ a b : α, a ≤ a + b -- see Note [lower instance priority] attribute [instance 100] CanonicallyOrderedAddCommMonoid.toOrderBot /-- A canonically ordered monoid is an ordered commutative monoid in which the ordering coincides with the divisibility relation, which is to say, `a ≤ b` iff there exists `c` with `b = a * c`. Examples seem rare; it seems more likely that the `OrderDual` of a naturally-occurring lattice satisfies this than the lattice itself (for example, dual of the lattice of ideals of a PID or Dedekind domain satisfy this; collections of all things ≤ 1 seem to be more natural that collections of all things ≥ 1). -/ @[to_additive] class CanonicallyOrderedCommMonoid (α : Type*) extends OrderedCommMonoid α, OrderBot α where /-- For `a ≤ b`, there is a `c` so `b = a * c`. -/ protected exists_mul_of_le : ∀ {a b : α}, a ≤ b → ∃ c, b = a * c /-- For any `a` and `b`, `a ≤ a * b` -/ protected le_self_mul : ∀ a b : α, a ≤ a * b -- see Note [lower instance priority] attribute [instance 100] CanonicallyOrderedCommMonoid.toOrderBot -- see Note [lower instance priority] @[to_additive] instance (priority := 100) CanonicallyOrderedCommMonoid.existsMulOfLE (α : Type u) [h : CanonicallyOrderedCommMonoid α] : ExistsMulOfLE α := { h with } section CanonicallyOrderedCommMonoid variable [CanonicallyOrderedCommMonoid α] {a b c d : α} @[to_additive] theorem le_self_mul : a ≤ a * c := CanonicallyOrderedCommMonoid.le_self_mul _ _ @[to_additive] theorem le_mul_self : a ≤ b * a := by rw [mul_comm] exact le_self_mul @[to_additive (attr := simp)] theorem self_le_mul_right (a b : α) : a ≤ a * b := le_self_mul @[to_additive (attr := simp)] theorem self_le_mul_left (a b : α) : a ≤ b * a := le_mul_self @[to_additive] theorem le_of_mul_le_left : a * b ≤ c → a ≤ c := le_self_mul.trans @[to_additive] theorem le_of_mul_le_right : a * b ≤ c → b ≤ c := le_mul_self.trans @[to_additive] theorem le_mul_of_le_left : a ≤ b → a ≤ b * c := le_self_mul.trans' @[to_additive] theorem le_mul_of_le_right : a ≤ c → a ≤ b * c := le_mul_self.trans' @[to_additive] theorem le_iff_exists_mul : a ≤ b ↔ ∃ c, b = a * c := ⟨exists_mul_of_le, by rintro ⟨c, rfl⟩ exact le_self_mul⟩ @[to_additive] theorem le_iff_exists_mul' : a ≤ b ↔ ∃ c, b = c * a := by simp only [mul_comm _ a, le_iff_exists_mul] @[to_additive (attr := simp) zero_le] theorem one_le (a : α) : 1 ≤ a := le_iff_exists_mul.mpr ⟨a, (one_mul _).symm⟩ @[to_additive] theorem bot_eq_one : (⊥ : α) = 1 := le_antisymm bot_le (one_le ⊥) --TODO: This is a special case of `mul_eq_one`. We need the instance -- `CanonicallyOrderedCommMonoid α → Unique αˣ` @[to_additive (attr := simp)] theorem mul_eq_one_iff : a * b = 1 ↔ a = 1 ∧ b = 1 := mul_eq_one_iff' (one_le _) (one_le _) @[to_additive (attr := simp)] theorem le_one_iff_eq_one : a ≤ 1 ↔ a = 1 := (one_le a).le_iff_eq @[to_additive] theorem one_lt_iff_ne_one : 1 < a ↔ a ≠ 1 := (one_le a).lt_iff_ne.trans ne_comm @[to_additive] theorem eq_one_or_one_lt (a : α) : a = 1 ∨ 1 < a := (one_le a).eq_or_lt.imp_left Eq.symm @[to_additive (attr := simp) add_pos_iff] theorem one_lt_mul_iff : 1 < a * b ↔ 1 < a ∨ 1 < b := by simp only [one_lt_iff_ne_one, Ne, mul_eq_one_iff, not_and_or] @[to_additive] theorem exists_one_lt_mul_of_lt (h : a < b) : ∃ (c : _) (_ : 1 < c), a * c = b := by obtain ⟨c, hc⟩ := le_iff_exists_mul.1 h.le refine ⟨c, one_lt_iff_ne_one.2 ?_, hc.symm⟩ rintro rfl simp [hc, lt_irrefl] at h @[to_additive] theorem le_mul_left (h : a ≤ c) : a ≤ b * c := calc a = 1 * a := by simp _ ≤ b * c := mul_le_mul' (one_le _) h @[to_additive] theorem le_mul_right (h : a ≤ b) : a ≤ b * c := calc a = a * 1 := by simp _ ≤ b * c := mul_le_mul' h (one_le _) @[to_additive] theorem lt_iff_exists_mul [CovariantClass α α (· * ·) (· < ·)] : a < b ↔ ∃ c > 1, b = a * c := by rw [lt_iff_le_and_ne, le_iff_exists_mul, ← exists_and_right] apply exists_congr intro c rw [and_comm, and_congr_left_iff, gt_iff_lt] rintro rfl constructor · rw [one_lt_iff_ne_one] apply mt rintro rfl rw [mul_one] · rw [← (self_le_mul_right a c).lt_iff_ne] apply lt_mul_of_one_lt_right' end CanonicallyOrderedCommMonoid theorem pos_of_gt {M : Type*} [CanonicallyOrderedAddCommMonoid M] {n m : M} (h : n < m) : 0 < m := lt_of_le_of_lt (zero_le _) h namespace NeZero theorem pos {M} (a : M) [CanonicallyOrderedAddCommMonoid M] [NeZero a] : 0 < a := (zero_le a).lt_of_ne <| NeZero.out.symm theorem of_gt {M} [CanonicallyOrderedAddCommMonoid M] {x y : M} (h : x < y) : NeZero y := of_pos <| pos_of_gt h -- 1 < p is still an often-used `Fact`, due to `Nat.Prime` implying it, and it implying `Nontrivial` -- on `ZMod`'s ring structure. We cannot just set this to be any `x < y`, else that becomes a -- metavariable and it will hugely slow down typeclass inference. instance (priority := 10) of_gt' {M : Type*} [CanonicallyOrderedAddCommMonoid M] [One M] {y : M} -- Porting note: Fact.out has different type signature from mathlib3 [Fact (1 < y)] : NeZero y := of_gt <| @Fact.out (1 < y) _ end NeZero /-- A canonically linear-ordered additive monoid is a canonically ordered additive monoid whose ordering is a linear order. -/ class CanonicallyLinearOrderedAddCommMonoid (α : Type*) extends CanonicallyOrderedAddCommMonoid α, LinearOrderedAddCommMonoid α /-- A canonically linear-ordered monoid is a canonically ordered monoid whose ordering is a linear order. -/ @[to_additive] class CanonicallyLinearOrderedCommMonoid (α : Type*) extends CanonicallyOrderedCommMonoid α, LinearOrderedCommMonoid α attribute [to_additive existing] CanonicallyLinearOrderedCommMonoid.toLinearOrderedCommMonoid section CanonicallyLinearOrderedCommMonoid variable [CanonicallyLinearOrderedCommMonoid α] -- see Note [lower instance priority] @[to_additive] instance (priority := 100) CanonicallyLinearOrderedCommMonoid.semilatticeSup : SemilatticeSup α := { LinearOrder.toLattice with } @[to_additive] theorem min_mul_distrib (a b c : α) : min a (b * c) = min a (min a b * min a c) := by rcases le_total a b with hb | hb · simp [hb, le_mul_right] · rcases le_total a c with hc | hc · simp [hc, le_mul_left] · simp [hb, hc] @[to_additive] theorem min_mul_distrib' (a b c : α) : min (a * b) c = min (min a c * min b c) c := by simpa [min_comm _ c] using min_mul_distrib c a b -- Porting note (#10618): no longer `@[simp]`, as `simp` can prove this. @[to_additive] theorem one_min (a : α) : min 1 a = 1 := min_eq_left (one_le a) -- Porting note (#10618): no longer `@[simp]`, as `simp` can prove this. @[to_additive] theorem min_one (a : α) : min a 1 = 1 := min_eq_right (one_le a) /-- In a linearly ordered monoid, we are happy for `bot_eq_one` to be a `@[simp]` lemma. -/ @[to_additive (attr := simp) "In a linearly ordered monoid, we are happy for `bot_eq_zero` to be a `@[simp]` lemma"] theorem bot_eq_one' : (⊥ : α) = 1 := bot_eq_one end CanonicallyLinearOrderedCommMonoid
Algebra\Order\Monoid\Unbundled\Basic.lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro, Johannes Hölzl, Damiano Testa, Yuyang Zhao -/ import Mathlib.Algebra.Order.Monoid.Unbundled.Defs import Mathlib.Data.Ordering.Basic import Mathlib.Order.MinMax import Mathlib.Tactic.Contrapose import Mathlib.Tactic.Use /-! # Ordered monoids This file develops the basics of ordered monoids. ## Implementation details Unfortunately, the number of `'` appended to lemmas in this file may differ between the multiplicative and the additive version of a lemma. The reason is that we did not want to change existing names in the library. ## Remark Almost no monoid is actually present in this file: most assumptions have been generalized to `Mul` or `MulOneClass`. -/ -- TODO: If possible, uniformize lemma names, taking special care of `'`, -- after the `ordered`-refactor is done. open Function section Nat instance Nat.instCovariantClassMulLE : CovariantClass ℕ ℕ (· * ·) (· ≤ ·) where elim := fun _ _ _ h => mul_le_mul_left _ h end Nat section Int instance Int.instCovariantClassAddLE : CovariantClass ℤ ℤ ((· + ·)) (· ≤ ·) where elim := fun _ _ _ h => Int.add_le_add_left h _ end Int variable {α β : Type*} section Mul variable [Mul α] section LE variable [LE α] /- The prime on this lemma is present only on the multiplicative version. The unprimed version is taken by the analogous lemma for semiring, with an extra non-negativity assumption. -/ @[to_additive (attr := gcongr) add_le_add_left] theorem mul_le_mul_left' [CovariantClass α α (· * ·) (· ≤ ·)] {b c : α} (bc : b ≤ c) (a : α) : a * b ≤ a * c := CovariantClass.elim _ bc @[to_additive le_of_add_le_add_left] theorem le_of_mul_le_mul_left' [ContravariantClass α α (· * ·) (· ≤ ·)] {a b c : α} (bc : a * b ≤ a * c) : b ≤ c := ContravariantClass.elim _ bc /- The prime on this lemma is present only on the multiplicative version. The unprimed version is taken by the analogous lemma for semiring, with an extra non-negativity assumption. -/ @[to_additive (attr := gcongr) add_le_add_right] theorem mul_le_mul_right' [i : CovariantClass α α (swap (· * ·)) (· ≤ ·)] {b c : α} (bc : b ≤ c) (a : α) : b * a ≤ c * a := i.elim a bc @[to_additive le_of_add_le_add_right] theorem le_of_mul_le_mul_right' [i : ContravariantClass α α (swap (· * ·)) (· ≤ ·)] {a b c : α} (bc : b * a ≤ c * a) : b ≤ c := i.elim a bc @[to_additive (attr := simp)] theorem mul_le_mul_iff_left [CovariantClass α α (· * ·) (· ≤ ·)] [ContravariantClass α α (· * ·) (· ≤ ·)] (a : α) {b c : α} : a * b ≤ a * c ↔ b ≤ c := rel_iff_cov α α (· * ·) (· ≤ ·) a @[to_additive (attr := simp)] theorem mul_le_mul_iff_right [CovariantClass α α (swap (· * ·)) (· ≤ ·)] [ContravariantClass α α (swap (· * ·)) (· ≤ ·)] (a : α) {b c : α} : b * a ≤ c * a ↔ b ≤ c := rel_iff_cov α α (swap (· * ·)) (· ≤ ·) a end LE section LT variable [LT α] @[to_additive (attr := simp)] theorem mul_lt_mul_iff_left [CovariantClass α α (· * ·) (· < ·)] [ContravariantClass α α (· * ·) (· < ·)] (a : α) {b c : α} : a * b < a * c ↔ b < c := rel_iff_cov α α (· * ·) (· < ·) a @[to_additive (attr := simp)] theorem mul_lt_mul_iff_right [CovariantClass α α (swap (· * ·)) (· < ·)] [ContravariantClass α α (swap (· * ·)) (· < ·)] (a : α) {b c : α} : b * a < c * a ↔ b < c := rel_iff_cov α α (swap (· * ·)) (· < ·) a @[to_additive (attr := gcongr) add_lt_add_left] theorem mul_lt_mul_left' [CovariantClass α α (· * ·) (· < ·)] {b c : α} (bc : b < c) (a : α) : a * b < a * c := CovariantClass.elim _ bc @[to_additive lt_of_add_lt_add_left] theorem lt_of_mul_lt_mul_left' [ContravariantClass α α (· * ·) (· < ·)] {a b c : α} (bc : a * b < a * c) : b < c := ContravariantClass.elim _ bc @[to_additive (attr := gcongr) add_lt_add_right] theorem mul_lt_mul_right' [i : CovariantClass α α (swap (· * ·)) (· < ·)] {b c : α} (bc : b < c) (a : α) : b * a < c * a := i.elim a bc @[to_additive lt_of_add_lt_add_right] theorem lt_of_mul_lt_mul_right' [i : ContravariantClass α α (swap (· * ·)) (· < ·)] {a b c : α} (bc : b * a < c * a) : b < c := i.elim a bc end LT section Preorder variable [Preorder α] @[to_additive (attr := gcongr)] theorem mul_lt_mul_of_lt_of_lt [CovariantClass α α (· * ·) (· < ·)] [CovariantClass α α (swap (· * ·)) (· < ·)] {a b c d : α} (h₁ : a < b) (h₂ : c < d) : a * c < b * d := calc a * c < a * d := mul_lt_mul_left' h₂ a _ < b * d := mul_lt_mul_right' h₁ d alias add_lt_add := add_lt_add_of_lt_of_lt @[to_additive] theorem mul_lt_mul_of_le_of_lt [CovariantClass α α (· * ·) (· < ·)] [CovariantClass α α (swap (· * ·)) (· ≤ ·)] {a b c d : α} (h₁ : a ≤ b) (h₂ : c < d) : a * c < b * d := (mul_le_mul_right' h₁ _).trans_lt (mul_lt_mul_left' h₂ b) @[to_additive] theorem mul_lt_mul_of_lt_of_le [CovariantClass α α (· * ·) (· ≤ ·)] [CovariantClass α α (swap (· * ·)) (· < ·)] {a b c d : α} (h₁ : a < b) (h₂ : c ≤ d) : a * c < b * d := (mul_le_mul_left' h₂ _).trans_lt (mul_lt_mul_right' h₁ d) /-- Only assumes left strict covariance. -/ @[to_additive "Only assumes left strict covariance"] theorem Left.mul_lt_mul [CovariantClass α α (· * ·) (· < ·)] [CovariantClass α α (swap (· * ·)) (· ≤ ·)] {a b c d : α} (h₁ : a < b) (h₂ : c < d) : a * c < b * d := mul_lt_mul_of_le_of_lt h₁.le h₂ /-- Only assumes right strict covariance. -/ @[to_additive "Only assumes right strict covariance"] theorem Right.mul_lt_mul [CovariantClass α α (· * ·) (· ≤ ·)] [CovariantClass α α (swap (· * ·)) (· < ·)] {a b c d : α} (h₁ : a < b) (h₂ : c < d) : a * c < b * d := mul_lt_mul_of_lt_of_le h₁ h₂.le @[to_additive (attr := gcongr) add_le_add] theorem mul_le_mul' [CovariantClass α α (· * ·) (· ≤ ·)] [CovariantClass α α (swap (· * ·)) (· ≤ ·)] {a b c d : α} (h₁ : a ≤ b) (h₂ : c ≤ d) : a * c ≤ b * d := (mul_le_mul_left' h₂ _).trans (mul_le_mul_right' h₁ d) @[to_additive] theorem mul_le_mul_three [CovariantClass α α (· * ·) (· ≤ ·)] [CovariantClass α α (swap (· * ·)) (· ≤ ·)] {a b c d e f : α} (h₁ : a ≤ d) (h₂ : b ≤ e) (h₃ : c ≤ f) : a * b * c ≤ d * e * f := mul_le_mul' (mul_le_mul' h₁ h₂) h₃ @[to_additive] theorem mul_lt_of_mul_lt_left [CovariantClass α α (· * ·) (· ≤ ·)] {a b c d : α} (h : a * b < c) (hle : d ≤ b) : a * d < c := (mul_le_mul_left' hle a).trans_lt h @[to_additive] theorem mul_le_of_mul_le_left [CovariantClass α α (· * ·) (· ≤ ·)] {a b c d : α} (h : a * b ≤ c) (hle : d ≤ b) : a * d ≤ c := @act_rel_of_rel_of_act_rel _ _ _ (· ≤ ·) _ _ a _ _ _ hle h @[to_additive] theorem mul_lt_of_mul_lt_right [CovariantClass α α (swap (· * ·)) (· ≤ ·)] {a b c d : α} (h : a * b < c) (hle : d ≤ a) : d * b < c := (mul_le_mul_right' hle b).trans_lt h @[to_additive] theorem mul_le_of_mul_le_right [CovariantClass α α (swap (· * ·)) (· ≤ ·)] {a b c d : α} (h : a * b ≤ c) (hle : d ≤ a) : d * b ≤ c := (mul_le_mul_right' hle b).trans h @[to_additive] theorem lt_mul_of_lt_mul_left [CovariantClass α α (· * ·) (· ≤ ·)] {a b c d : α} (h : a < b * c) (hle : c ≤ d) : a < b * d := h.trans_le (mul_le_mul_left' hle b) @[to_additive] theorem le_mul_of_le_mul_left [CovariantClass α α (· * ·) (· ≤ ·)] {a b c d : α} (h : a ≤ b * c) (hle : c ≤ d) : a ≤ b * d := @rel_act_of_rel_of_rel_act _ _ _ (· ≤ ·) _ _ b _ _ _ hle h @[to_additive] theorem lt_mul_of_lt_mul_right [CovariantClass α α (swap (· * ·)) (· ≤ ·)] {a b c d : α} (h : a < b * c) (hle : b ≤ d) : a < d * c := h.trans_le (mul_le_mul_right' hle c) @[to_additive] theorem le_mul_of_le_mul_right [CovariantClass α α (swap (· * ·)) (· ≤ ·)] {a b c d : α} (h : a ≤ b * c) (hle : b ≤ d) : a ≤ d * c := h.trans (mul_le_mul_right' hle c) end Preorder section PartialOrder variable [PartialOrder α] @[to_additive] theorem mul_left_cancel'' [ContravariantClass α α (· * ·) (· ≤ ·)] {a b c : α} (h : a * b = a * c) : b = c := (le_of_mul_le_mul_left' h.le).antisymm (le_of_mul_le_mul_left' h.ge) @[to_additive] theorem mul_right_cancel'' [ContravariantClass α α (swap (· * ·)) (· ≤ ·)] {a b c : α} (h : a * b = c * b) : a = c := (le_of_mul_le_mul_right' h.le).antisymm (le_of_mul_le_mul_right' h.ge) @[to_additive] lemma mul_le_mul_iff_of_ge [CovariantClass α α (· * ·) (· < ·)] [CovariantClass α α (swap (· * ·)) (· < ·)] {a₁ a₂ b₁ b₂ : α} (ha : a₁ ≤ a₂) (hb : b₁ ≤ b₂) : a₂ * b₂ ≤ a₁ * b₁ ↔ a₁ = a₂ ∧ b₁ = b₂ := by haveI := covariantClass_le_of_lt α α (· * ·) haveI := covariantClass_le_of_lt α α (swap (· * ·)) refine ⟨fun h ↦ ?_, by rintro ⟨rfl, rfl⟩; rfl⟩ simp only [eq_iff_le_not_lt, ha, hb, true_and] refine ⟨fun ha ↦ h.not_lt ?_, fun hb ↦ h.not_lt ?_⟩ exacts [mul_lt_mul_of_lt_of_le ha hb, mul_lt_mul_of_le_of_lt ha hb] @[to_additive] theorem mul_eq_mul_iff_eq_and_eq [CovariantClass α α (· * ·) (· < ·)] [CovariantClass α α (swap (· * ·)) (· < ·)] {a b c d : α} (hac : a ≤ c) (hbd : b ≤ d) : a * b = c * d ↔ a = c ∧ b = d := by haveI := covariantClass_le_of_lt α α (· * ·) haveI := covariantClass_le_of_lt α α (swap (· * ·)) rw [le_antisymm_iff, eq_true (mul_le_mul' hac hbd), true_and, mul_le_mul_iff_of_ge hac hbd] end PartialOrder section LinearOrder variable [LinearOrder α] {a b c d : α} @[to_additive] lemma min_lt_max_of_mul_lt_mul [CovariantClass α α (· * ·) (· ≤ ·)] [CovariantClass α α (swap (· * ·)) (· ≤ ·)] (h : a * b < c * d) : min a b < max c d := by simp_rw [min_lt_iff, lt_max_iff]; contrapose! h; exact mul_le_mul' h.1.1 h.2.2 @[to_additive] lemma Left.min_le_max_of_mul_le_mul [CovariantClass α α (· * ·) (· < ·)] [CovariantClass α α (swap (· * ·)) (· ≤ ·)] (h : a * b ≤ c * d) : min a b ≤ max c d := by simp_rw [min_le_iff, le_max_iff]; contrapose! h; exact mul_lt_mul_of_le_of_lt h.1.1.le h.2.2 @[to_additive] lemma Right.min_le_max_of_mul_le_mul [CovariantClass α α (· * ·) (· ≤ ·)] [CovariantClass α α (swap (· * ·)) (· < ·)] (h : a * b ≤ c * d) : min a b ≤ max c d := by simp_rw [min_le_iff, le_max_iff]; contrapose! h; exact mul_lt_mul_of_lt_of_le h.1.1 h.2.2.le @[to_additive] lemma min_le_max_of_mul_le_mul [CovariantClass α α (· * ·) (· < ·)] [CovariantClass α α (swap (· * ·)) (· < ·)] (h : a * b ≤ c * d) : min a b ≤ max c d := let _ := covariantClass_le_of_lt α α (swap (· * ·)) Left.min_le_max_of_mul_le_mul h end LinearOrder section LinearOrder variable [LinearOrder α] [CovariantClass α α (· * ·) (· ≤ ·)] [CovariantClass α α (swap (· * ·)) (· ≤ ·)] {a b c d : α} @[to_additive max_add_add_le_max_add_max] theorem max_mul_mul_le_max_mul_max' : max (a * b) (c * d) ≤ max a c * max b d := max_le (mul_le_mul' (le_max_left _ _) <| le_max_left _ _) <| mul_le_mul' (le_max_right _ _) <| le_max_right _ _ @[to_additive min_add_min_le_min_add_add] theorem min_mul_min_le_min_mul_mul' : min a c * min b d ≤ min (a * b) (c * d) := le_min (mul_le_mul' (min_le_left _ _) <| min_le_left _ _) <| mul_le_mul' (min_le_right _ _) <| min_le_right _ _ end LinearOrder end Mul -- using one section MulOneClass variable [MulOneClass α] section LE variable [LE α] @[to_additive le_add_of_nonneg_right] theorem le_mul_of_one_le_right' [CovariantClass α α (· * ·) (· ≤ ·)] {a b : α} (h : 1 ≤ b) : a ≤ a * b := calc a = a * 1 := (mul_one a).symm _ ≤ a * b := mul_le_mul_left' h a @[to_additive add_le_of_nonpos_right] theorem mul_le_of_le_one_right' [CovariantClass α α (· * ·) (· ≤ ·)] {a b : α} (h : b ≤ 1) : a * b ≤ a := calc a * b ≤ a * 1 := mul_le_mul_left' h a _ = a := mul_one a @[to_additive le_add_of_nonneg_left] theorem le_mul_of_one_le_left' [CovariantClass α α (swap (· * ·)) (· ≤ ·)] {a b : α} (h : 1 ≤ b) : a ≤ b * a := calc a = 1 * a := (one_mul a).symm _ ≤ b * a := mul_le_mul_right' h a @[to_additive add_le_of_nonpos_left] theorem mul_le_of_le_one_left' [CovariantClass α α (swap (· * ·)) (· ≤ ·)] {a b : α} (h : b ≤ 1) : b * a ≤ a := calc b * a ≤ 1 * a := mul_le_mul_right' h a _ = a := one_mul a @[to_additive] theorem one_le_of_le_mul_right [ContravariantClass α α (· * ·) (· ≤ ·)] {a b : α} (h : a ≤ a * b) : 1 ≤ b := le_of_mul_le_mul_left' (a := a) <| by simpa only [mul_one] @[to_additive] theorem le_one_of_mul_le_right [ContravariantClass α α (· * ·) (· ≤ ·)] {a b : α} (h : a * b ≤ a) : b ≤ 1 := le_of_mul_le_mul_left' (a := a) <| by simpa only [mul_one] @[to_additive] theorem one_le_of_le_mul_left [ContravariantClass α α (swap (· * ·)) (· ≤ ·)] {a b : α} (h : b ≤ a * b) : 1 ≤ a := le_of_mul_le_mul_right' (a := b) <| by simpa only [one_mul] @[to_additive] theorem le_one_of_mul_le_left [ContravariantClass α α (swap (· * ·)) (· ≤ ·)] {a b : α} (h : a * b ≤ b) : a ≤ 1 := le_of_mul_le_mul_right' (a := b) <| by simpa only [one_mul] @[to_additive (attr := simp) le_add_iff_nonneg_right] theorem le_mul_iff_one_le_right' [CovariantClass α α (· * ·) (· ≤ ·)] [ContravariantClass α α (· * ·) (· ≤ ·)] (a : α) {b : α} : a ≤ a * b ↔ 1 ≤ b := Iff.trans (by rw [mul_one]) (mul_le_mul_iff_left a) @[to_additive (attr := simp) le_add_iff_nonneg_left] theorem le_mul_iff_one_le_left' [CovariantClass α α (swap (· * ·)) (· ≤ ·)] [ContravariantClass α α (swap (· * ·)) (· ≤ ·)] (a : α) {b : α} : a ≤ b * a ↔ 1 ≤ b := Iff.trans (by rw [one_mul]) (mul_le_mul_iff_right a) @[to_additive (attr := simp) add_le_iff_nonpos_right] theorem mul_le_iff_le_one_right' [CovariantClass α α (· * ·) (· ≤ ·)] [ContravariantClass α α (· * ·) (· ≤ ·)] (a : α) {b : α} : a * b ≤ a ↔ b ≤ 1 := Iff.trans (by rw [mul_one]) (mul_le_mul_iff_left a) @[to_additive (attr := simp) add_le_iff_nonpos_left] theorem mul_le_iff_le_one_left' [CovariantClass α α (swap (· * ·)) (· ≤ ·)] [ContravariantClass α α (swap (· * ·)) (· ≤ ·)] {a b : α} : a * b ≤ b ↔ a ≤ 1 := Iff.trans (by rw [one_mul]) (mul_le_mul_iff_right b) end LE section LT variable [LT α] @[to_additive lt_add_of_pos_right] theorem lt_mul_of_one_lt_right' [CovariantClass α α (· * ·) (· < ·)] (a : α) {b : α} (h : 1 < b) : a < a * b := calc a = a * 1 := (mul_one a).symm _ < a * b := mul_lt_mul_left' h a @[to_additive add_lt_of_neg_right] theorem mul_lt_of_lt_one_right' [CovariantClass α α (· * ·) (· < ·)] (a : α) {b : α} (h : b < 1) : a * b < a := calc a * b < a * 1 := mul_lt_mul_left' h a _ = a := mul_one a @[to_additive lt_add_of_pos_left] theorem lt_mul_of_one_lt_left' [CovariantClass α α (swap (· * ·)) (· < ·)] (a : α) {b : α} (h : 1 < b) : a < b * a := calc a = 1 * a := (one_mul a).symm _ < b * a := mul_lt_mul_right' h a @[to_additive add_lt_of_neg_left] theorem mul_lt_of_lt_one_left' [CovariantClass α α (swap (· * ·)) (· < ·)] (a : α) {b : α} (h : b < 1) : b * a < a := calc b * a < 1 * a := mul_lt_mul_right' h a _ = a := one_mul a @[to_additive] theorem one_lt_of_lt_mul_right [ContravariantClass α α (· * ·) (· < ·)] {a b : α} (h : a < a * b) : 1 < b := lt_of_mul_lt_mul_left' (a := a) <| by simpa only [mul_one] @[to_additive] theorem lt_one_of_mul_lt_right [ContravariantClass α α (· * ·) (· < ·)] {a b : α} (h : a * b < a) : b < 1 := lt_of_mul_lt_mul_left' (a := a) <| by simpa only [mul_one] @[to_additive] theorem one_lt_of_lt_mul_left [ContravariantClass α α (swap (· * ·)) (· < ·)] {a b : α} (h : b < a * b) : 1 < a := lt_of_mul_lt_mul_right' (a := b) <| by simpa only [one_mul] @[to_additive] theorem lt_one_of_mul_lt_left [ContravariantClass α α (swap (· * ·)) (· < ·)] {a b : α} (h : a * b < b) : a < 1 := lt_of_mul_lt_mul_right' (a := b) <| by simpa only [one_mul] @[to_additive (attr := simp) lt_add_iff_pos_right] theorem lt_mul_iff_one_lt_right' [CovariantClass α α (· * ·) (· < ·)] [ContravariantClass α α (· * ·) (· < ·)] (a : α) {b : α} : a < a * b ↔ 1 < b := Iff.trans (by rw [mul_one]) (mul_lt_mul_iff_left a) @[to_additive (attr := simp) lt_add_iff_pos_left] theorem lt_mul_iff_one_lt_left' [CovariantClass α α (swap (· * ·)) (· < ·)] [ContravariantClass α α (swap (· * ·)) (· < ·)] (a : α) {b : α} : a < b * a ↔ 1 < b := Iff.trans (by rw [one_mul]) (mul_lt_mul_iff_right a) @[to_additive (attr := simp) add_lt_iff_neg_left] theorem mul_lt_iff_lt_one_left' [CovariantClass α α (· * ·) (· < ·)] [ContravariantClass α α (· * ·) (· < ·)] {a b : α} : a * b < a ↔ b < 1 := Iff.trans (by rw [mul_one]) (mul_lt_mul_iff_left a) @[to_additive (attr := simp) add_lt_iff_neg_right] theorem mul_lt_iff_lt_one_right' [CovariantClass α α (swap (· * ·)) (· < ·)] [ContravariantClass α α (swap (· * ·)) (· < ·)] {a : α} (b : α) : a * b < b ↔ a < 1 := Iff.trans (by rw [one_mul]) (mul_lt_mul_iff_right b) end LT section Preorder variable [Preorder α] /-! Lemmas of the form `b ≤ c → a ≤ 1 → b * a ≤ c`, which assume left covariance. -/ @[to_additive] theorem mul_le_of_le_of_le_one [CovariantClass α α (· * ·) (· ≤ ·)] {a b c : α} (hbc : b ≤ c) (ha : a ≤ 1) : b * a ≤ c := calc b * a ≤ b * 1 := mul_le_mul_left' ha b _ = b := mul_one b _ ≤ c := hbc @[to_additive] theorem mul_lt_of_le_of_lt_one [CovariantClass α α (· * ·) (· < ·)] {a b c : α} (hbc : b ≤ c) (ha : a < 1) : b * a < c := calc b * a < b * 1 := mul_lt_mul_left' ha b _ = b := mul_one b _ ≤ c := hbc @[to_additive] theorem mul_lt_of_lt_of_le_one [CovariantClass α α (· * ·) (· ≤ ·)] {a b c : α} (hbc : b < c) (ha : a ≤ 1) : b * a < c := calc b * a ≤ b * 1 := mul_le_mul_left' ha b _ = b := mul_one b _ < c := hbc @[to_additive] theorem mul_lt_of_lt_of_lt_one [CovariantClass α α (· * ·) (· < ·)] {a b c : α} (hbc : b < c) (ha : a < 1) : b * a < c := calc b * a < b * 1 := mul_lt_mul_left' ha b _ = b := mul_one b _ < c := hbc @[to_additive] theorem mul_lt_of_lt_of_lt_one' [CovariantClass α α (· * ·) (· ≤ ·)] {a b c : α} (hbc : b < c) (ha : a < 1) : b * a < c := mul_lt_of_lt_of_le_one hbc ha.le /-- Assumes left covariance. The lemma assuming right covariance is `Right.mul_le_one`. -/ @[to_additive "Assumes left covariance. The lemma assuming right covariance is `Right.add_nonpos`."] theorem Left.mul_le_one [CovariantClass α α (· * ·) (· ≤ ·)] {a b : α} (ha : a ≤ 1) (hb : b ≤ 1) : a * b ≤ 1 := mul_le_of_le_of_le_one ha hb /-- Assumes left covariance. The lemma assuming right covariance is `Right.mul_lt_one_of_le_of_lt`. -/ @[to_additive Left.add_neg_of_nonpos_of_neg "Assumes left covariance. The lemma assuming right covariance is `Right.add_neg_of_nonpos_of_neg`."] theorem Left.mul_lt_one_of_le_of_lt [CovariantClass α α (· * ·) (· < ·)] {a b : α} (ha : a ≤ 1) (hb : b < 1) : a * b < 1 := mul_lt_of_le_of_lt_one ha hb /-- Assumes left covariance. The lemma assuming right covariance is `Right.mul_lt_one_of_lt_of_le`. -/ @[to_additive Left.add_neg_of_neg_of_nonpos "Assumes left covariance. The lemma assuming right covariance is `Right.add_neg_of_neg_of_nonpos`."] theorem Left.mul_lt_one_of_lt_of_le [CovariantClass α α (· * ·) (· ≤ ·)] {a b : α} (ha : a < 1) (hb : b ≤ 1) : a * b < 1 := mul_lt_of_lt_of_le_one ha hb /-- Assumes left covariance. The lemma assuming right covariance is `Right.mul_lt_one`. -/ @[to_additive "Assumes left covariance. The lemma assuming right covariance is `Right.add_neg`."] theorem Left.mul_lt_one [CovariantClass α α (· * ·) (· < ·)] {a b : α} (ha : a < 1) (hb : b < 1) : a * b < 1 := mul_lt_of_lt_of_lt_one ha hb /-- Assumes left covariance. The lemma assuming right covariance is `Right.mul_lt_one'`. -/ @[to_additive "Assumes left covariance. The lemma assuming right covariance is `Right.add_neg'`."] theorem Left.mul_lt_one' [CovariantClass α α (· * ·) (· ≤ ·)] {a b : α} (ha : a < 1) (hb : b < 1) : a * b < 1 := mul_lt_of_lt_of_lt_one' ha hb /-! Lemmas of the form `b ≤ c → 1 ≤ a → b ≤ c * a`, which assume left covariance. -/ @[to_additive] theorem le_mul_of_le_of_one_le [CovariantClass α α (· * ·) (· ≤ ·)] {a b c : α} (hbc : b ≤ c) (ha : 1 ≤ a) : b ≤ c * a := calc b ≤ c := hbc _ = c * 1 := (mul_one c).symm _ ≤ c * a := mul_le_mul_left' ha c @[to_additive] theorem lt_mul_of_le_of_one_lt [CovariantClass α α (· * ·) (· < ·)] {a b c : α} (hbc : b ≤ c) (ha : 1 < a) : b < c * a := calc b ≤ c := hbc _ = c * 1 := (mul_one c).symm _ < c * a := mul_lt_mul_left' ha c @[to_additive] theorem lt_mul_of_lt_of_one_le [CovariantClass α α (· * ·) (· ≤ ·)] {a b c : α} (hbc : b < c) (ha : 1 ≤ a) : b < c * a := calc b < c := hbc _ = c * 1 := (mul_one c).symm _ ≤ c * a := mul_le_mul_left' ha c @[to_additive] theorem lt_mul_of_lt_of_one_lt [CovariantClass α α (· * ·) (· < ·)] {a b c : α} (hbc : b < c) (ha : 1 < a) : b < c * a := calc b < c := hbc _ = c * 1 := (mul_one c).symm _ < c * a := mul_lt_mul_left' ha c @[to_additive] theorem lt_mul_of_lt_of_one_lt' [CovariantClass α α (· * ·) (· ≤ ·)] {a b c : α} (hbc : b < c) (ha : 1 < a) : b < c * a := lt_mul_of_lt_of_one_le hbc ha.le /-- Assumes left covariance. The lemma assuming right covariance is `Right.one_le_mul`. -/ @[to_additive Left.add_nonneg "Assumes left covariance. The lemma assuming right covariance is `Right.add_nonneg`."] theorem Left.one_le_mul [CovariantClass α α (· * ·) (· ≤ ·)] {a b : α} (ha : 1 ≤ a) (hb : 1 ≤ b) : 1 ≤ a * b := le_mul_of_le_of_one_le ha hb /-- Assumes left covariance. The lemma assuming right covariance is `Right.one_lt_mul_of_le_of_lt`. -/ @[to_additive Left.add_pos_of_nonneg_of_pos "Assumes left covariance. The lemma assuming right covariance is `Right.add_pos_of_nonneg_of_pos`."] theorem Left.one_lt_mul_of_le_of_lt [CovariantClass α α (· * ·) (· < ·)] {a b : α} (ha : 1 ≤ a) (hb : 1 < b) : 1 < a * b := lt_mul_of_le_of_one_lt ha hb /-- Assumes left covariance. The lemma assuming right covariance is `Right.one_lt_mul_of_lt_of_le`. -/ @[to_additive Left.add_pos_of_pos_of_nonneg "Assumes left covariance. The lemma assuming right covariance is `Right.add_pos_of_pos_of_nonneg`."] theorem Left.one_lt_mul_of_lt_of_le [CovariantClass α α (· * ·) (· ≤ ·)] {a b : α} (ha : 1 < a) (hb : 1 ≤ b) : 1 < a * b := lt_mul_of_lt_of_one_le ha hb /-- Assumes left covariance. The lemma assuming right covariance is `Right.one_lt_mul`. -/ @[to_additive Left.add_pos "Assumes left covariance. The lemma assuming right covariance is `Right.add_pos`."] theorem Left.one_lt_mul [CovariantClass α α (· * ·) (· < ·)] {a b : α} (ha : 1 < a) (hb : 1 < b) : 1 < a * b := lt_mul_of_lt_of_one_lt ha hb /-- Assumes left covariance. The lemma assuming right covariance is `Right.one_lt_mul'`. -/ @[to_additive Left.add_pos' "Assumes left covariance. The lemma assuming right covariance is `Right.add_pos'`."] theorem Left.one_lt_mul' [CovariantClass α α (· * ·) (· ≤ ·)] {a b : α} (ha : 1 < a) (hb : 1 < b) : 1 < a * b := lt_mul_of_lt_of_one_lt' ha hb /-! Lemmas of the form `a ≤ 1 → b ≤ c → a * b ≤ c`, which assume right covariance. -/ @[to_additive] theorem mul_le_of_le_one_of_le [CovariantClass α α (swap (· * ·)) (· ≤ ·)] {a b c : α} (ha : a ≤ 1) (hbc : b ≤ c) : a * b ≤ c := calc a * b ≤ 1 * b := mul_le_mul_right' ha b _ = b := one_mul b _ ≤ c := hbc @[to_additive] theorem mul_lt_of_lt_one_of_le [CovariantClass α α (swap (· * ·)) (· < ·)] {a b c : α} (ha : a < 1) (hbc : b ≤ c) : a * b < c := calc a * b < 1 * b := mul_lt_mul_right' ha b _ = b := one_mul b _ ≤ c := hbc @[to_additive] theorem mul_lt_of_le_one_of_lt [CovariantClass α α (swap (· * ·)) (· ≤ ·)] {a b c : α} (ha : a ≤ 1) (hb : b < c) : a * b < c := calc a * b ≤ 1 * b := mul_le_mul_right' ha b _ = b := one_mul b _ < c := hb @[to_additive] theorem mul_lt_of_lt_one_of_lt [CovariantClass α α (swap (· * ·)) (· < ·)] {a b c : α} (ha : a < 1) (hb : b < c) : a * b < c := calc a * b < 1 * b := mul_lt_mul_right' ha b _ = b := one_mul b _ < c := hb @[to_additive] theorem mul_lt_of_lt_one_of_lt' [CovariantClass α α (swap (· * ·)) (· ≤ ·)] {a b c : α} (ha : a < 1) (hbc : b < c) : a * b < c := mul_lt_of_le_one_of_lt ha.le hbc /-- Assumes right covariance. The lemma assuming left covariance is `Left.mul_le_one`. -/ @[to_additive "Assumes right covariance. The lemma assuming left covariance is `Left.add_nonpos`."] theorem Right.mul_le_one [CovariantClass α α (swap (· * ·)) (· ≤ ·)] {a b : α} (ha : a ≤ 1) (hb : b ≤ 1) : a * b ≤ 1 := mul_le_of_le_one_of_le ha hb /-- Assumes right covariance. The lemma assuming left covariance is `Left.mul_lt_one_of_lt_of_le`. -/ @[to_additive Right.add_neg_of_neg_of_nonpos "Assumes right covariance. The lemma assuming left covariance is `Left.add_neg_of_neg_of_nonpos`."] theorem Right.mul_lt_one_of_lt_of_le [CovariantClass α α (swap (· * ·)) (· < ·)] {a b : α} (ha : a < 1) (hb : b ≤ 1) : a * b < 1 := mul_lt_of_lt_one_of_le ha hb /-- Assumes right covariance. The lemma assuming left covariance is `Left.mul_lt_one_of_le_of_lt`. -/ @[to_additive Right.add_neg_of_nonpos_of_neg "Assumes right covariance. The lemma assuming left covariance is `Left.add_neg_of_nonpos_of_neg`."] theorem Right.mul_lt_one_of_le_of_lt [CovariantClass α α (swap (· * ·)) (· ≤ ·)] {a b : α} (ha : a ≤ 1) (hb : b < 1) : a * b < 1 := mul_lt_of_le_one_of_lt ha hb /-- Assumes right covariance. The lemma assuming left covariance is `Left.mul_lt_one`. -/ @[to_additive "Assumes right covariance. The lemma assuming left covariance is `Left.add_neg`."] theorem Right.mul_lt_one [CovariantClass α α (swap (· * ·)) (· < ·)] {a b : α} (ha : a < 1) (hb : b < 1) : a * b < 1 := mul_lt_of_lt_one_of_lt ha hb /-- Assumes right covariance. The lemma assuming left covariance is `Left.mul_lt_one'`. -/ @[to_additive "Assumes right covariance. The lemma assuming left covariance is `Left.add_neg'`."] theorem Right.mul_lt_one' [CovariantClass α α (swap (· * ·)) (· ≤ ·)] {a b : α} (ha : a < 1) (hb : b < 1) : a * b < 1 := mul_lt_of_lt_one_of_lt' ha hb /-! Lemmas of the form `1 ≤ a → b ≤ c → b ≤ a * c`, which assume right covariance. -/ @[to_additive] theorem le_mul_of_one_le_of_le [CovariantClass α α (swap (· * ·)) (· ≤ ·)] {a b c : α} (ha : 1 ≤ a) (hbc : b ≤ c) : b ≤ a * c := calc b ≤ c := hbc _ = 1 * c := (one_mul c).symm _ ≤ a * c := mul_le_mul_right' ha c @[to_additive] theorem lt_mul_of_one_lt_of_le [CovariantClass α α (swap (· * ·)) (· < ·)] {a b c : α} (ha : 1 < a) (hbc : b ≤ c) : b < a * c := calc b ≤ c := hbc _ = 1 * c := (one_mul c).symm _ < a * c := mul_lt_mul_right' ha c @[to_additive] theorem lt_mul_of_one_le_of_lt [CovariantClass α α (swap (· * ·)) (· ≤ ·)] {a b c : α} (ha : 1 ≤ a) (hbc : b < c) : b < a * c := calc b < c := hbc _ = 1 * c := (one_mul c).symm _ ≤ a * c := mul_le_mul_right' ha c @[to_additive] theorem lt_mul_of_one_lt_of_lt [CovariantClass α α (swap (· * ·)) (· < ·)] {a b c : α} (ha : 1 < a) (hbc : b < c) : b < a * c := calc b < c := hbc _ = 1 * c := (one_mul c).symm _ < a * c := mul_lt_mul_right' ha c @[to_additive] theorem lt_mul_of_one_lt_of_lt' [CovariantClass α α (swap (· * ·)) (· ≤ ·)] {a b c : α} (ha : 1 < a) (hbc : b < c) : b < a * c := lt_mul_of_one_le_of_lt ha.le hbc /-- Assumes right covariance. The lemma assuming left covariance is `Left.one_le_mul`. -/ @[to_additive Right.add_nonneg "Assumes right covariance. The lemma assuming left covariance is `Left.add_nonneg`."] theorem Right.one_le_mul [CovariantClass α α (swap (· * ·)) (· ≤ ·)] {a b : α} (ha : 1 ≤ a) (hb : 1 ≤ b) : 1 ≤ a * b := le_mul_of_one_le_of_le ha hb /-- Assumes right covariance. The lemma assuming left covariance is `Left.one_lt_mul_of_lt_of_le`. -/ @[to_additive Right.add_pos_of_pos_of_nonneg "Assumes right covariance. The lemma assuming left covariance is `Left.add_pos_of_pos_of_nonneg`."] theorem Right.one_lt_mul_of_lt_of_le [CovariantClass α α (swap (· * ·)) (· < ·)] {a b : α} (ha : 1 < a) (hb : 1 ≤ b) : 1 < a * b := lt_mul_of_one_lt_of_le ha hb /-- Assumes right covariance. The lemma assuming left covariance is `Left.one_lt_mul_of_le_of_lt`. -/ @[to_additive Right.add_pos_of_nonneg_of_pos "Assumes right covariance. The lemma assuming left covariance is `Left.add_pos_of_nonneg_of_pos`."] theorem Right.one_lt_mul_of_le_of_lt [CovariantClass α α (swap (· * ·)) (· ≤ ·)] {a b : α} (ha : 1 ≤ a) (hb : 1 < b) : 1 < a * b := lt_mul_of_one_le_of_lt ha hb /-- Assumes right covariance. The lemma assuming left covariance is `Left.one_lt_mul`. -/ @[to_additive Right.add_pos "Assumes right covariance. The lemma assuming left covariance is `Left.add_pos`."] theorem Right.one_lt_mul [CovariantClass α α (swap (· * ·)) (· < ·)] {a b : α} (ha : 1 < a) (hb : 1 < b) : 1 < a * b := lt_mul_of_one_lt_of_lt ha hb /-- Assumes right covariance. The lemma assuming left covariance is `Left.one_lt_mul'`. -/ @[to_additive Right.add_pos' "Assumes right covariance. The lemma assuming left covariance is `Left.add_pos'`."] theorem Right.one_lt_mul' [CovariantClass α α (swap (· * ·)) (· ≤ ·)] {a b : α} (ha : 1 < a) (hb : 1 < b) : 1 < a * b := lt_mul_of_one_lt_of_lt' ha hb alias mul_le_one' := Left.mul_le_one alias mul_lt_one_of_le_of_lt := Left.mul_lt_one_of_le_of_lt alias mul_lt_one_of_lt_of_le := Left.mul_lt_one_of_lt_of_le alias mul_lt_one := Left.mul_lt_one alias mul_lt_one' := Left.mul_lt_one' attribute [to_additive add_nonpos "**Alias** of `Left.add_nonpos`."] mul_le_one' attribute [to_additive add_neg_of_nonpos_of_neg "**Alias** of `Left.add_neg_of_nonpos_of_neg`."] mul_lt_one_of_le_of_lt attribute [to_additive add_neg_of_neg_of_nonpos "**Alias** of `Left.add_neg_of_neg_of_nonpos`."] mul_lt_one_of_lt_of_le attribute [to_additive "**Alias** of `Left.add_neg`."] mul_lt_one attribute [to_additive "**Alias** of `Left.add_neg'`."] mul_lt_one' alias one_le_mul := Left.one_le_mul alias one_lt_mul_of_le_of_lt' := Left.one_lt_mul_of_le_of_lt alias one_lt_mul_of_lt_of_le' := Left.one_lt_mul_of_lt_of_le alias one_lt_mul' := Left.one_lt_mul alias one_lt_mul'' := Left.one_lt_mul' attribute [to_additive add_nonneg "**Alias** of `Left.add_nonneg`."] one_le_mul attribute [to_additive add_pos_of_nonneg_of_pos "**Alias** of `Left.add_pos_of_nonneg_of_pos`."] one_lt_mul_of_le_of_lt' attribute [to_additive add_pos_of_pos_of_nonneg "**Alias** of `Left.add_pos_of_pos_of_nonneg`."] one_lt_mul_of_lt_of_le' attribute [to_additive add_pos "**Alias** of `Left.add_pos`."] one_lt_mul' attribute [to_additive add_pos' "**Alias** of `Left.add_pos'`."] one_lt_mul'' @[to_additive] theorem lt_of_mul_lt_of_one_le_left [CovariantClass α α (· * ·) (· ≤ ·)] {a b c : α} (h : a * b < c) (hle : 1 ≤ b) : a < c := (le_mul_of_one_le_right' hle).trans_lt h @[to_additive] theorem le_of_mul_le_of_one_le_left [CovariantClass α α (· * ·) (· ≤ ·)] {a b c : α} (h : a * b ≤ c) (hle : 1 ≤ b) : a ≤ c := (le_mul_of_one_le_right' hle).trans h @[to_additive] theorem lt_of_lt_mul_of_le_one_left [CovariantClass α α (· * ·) (· ≤ ·)] {a b c : α} (h : a < b * c) (hle : c ≤ 1) : a < b := h.trans_le (mul_le_of_le_one_right' hle) @[to_additive] theorem le_of_le_mul_of_le_one_left [CovariantClass α α (· * ·) (· ≤ ·)] {a b c : α} (h : a ≤ b * c) (hle : c ≤ 1) : a ≤ b := h.trans (mul_le_of_le_one_right' hle) @[to_additive] theorem lt_of_mul_lt_of_one_le_right [CovariantClass α α (swap (· * ·)) (· ≤ ·)] {a b c : α} (h : a * b < c) (hle : 1 ≤ a) : b < c := (le_mul_of_one_le_left' hle).trans_lt h @[to_additive] theorem le_of_mul_le_of_one_le_right [CovariantClass α α (swap (· * ·)) (· ≤ ·)] {a b c : α} (h : a * b ≤ c) (hle : 1 ≤ a) : b ≤ c := (le_mul_of_one_le_left' hle).trans h @[to_additive] theorem lt_of_lt_mul_of_le_one_right [CovariantClass α α (swap (· * ·)) (· ≤ ·)] {a b c : α} (h : a < b * c) (hle : b ≤ 1) : a < c := h.trans_le (mul_le_of_le_one_left' hle) @[to_additive] theorem le_of_le_mul_of_le_one_right [CovariantClass α α (swap (· * ·)) (· ≤ ·)] {a b c : α} (h : a ≤ b * c) (hle : b ≤ 1) : a ≤ c := h.trans (mul_le_of_le_one_left' hle) end Preorder section PartialOrder variable [PartialOrder α] @[to_additive] theorem mul_eq_one_iff' [CovariantClass α α (· * ·) (· ≤ ·)] [CovariantClass α α (swap (· * ·)) (· ≤ ·)] {a b : α} (ha : 1 ≤ a) (hb : 1 ≤ b) : a * b = 1 ↔ a = 1 ∧ b = 1 := Iff.intro (fun hab : a * b = 1 => have : a ≤ 1 := hab ▸ le_mul_of_le_of_one_le le_rfl hb have : a = 1 := le_antisymm this ha have : b ≤ 1 := hab ▸ le_mul_of_one_le_of_le ha le_rfl have : b = 1 := le_antisymm this hb And.intro ‹a = 1› ‹b = 1›) (by rintro ⟨rfl, rfl⟩; rw [mul_one]) -- Porting note: original proof of the second implication, -- `fun ⟨ha', hb'⟩ => by rw [ha', hb', mul_one]`, -- had its `to_additive`-ization fail due to some bug section Left variable [CovariantClass α α (· * ·) (· ≤ ·)] {a b : α} @[to_additive eq_zero_of_add_nonneg_left] theorem eq_one_of_one_le_mul_left (ha : a ≤ 1) (hb : b ≤ 1) (hab : 1 ≤ a * b) : a = 1 := ha.eq_of_not_lt fun h => hab.not_lt <| mul_lt_one_of_lt_of_le h hb @[to_additive] theorem eq_one_of_mul_le_one_left (ha : 1 ≤ a) (hb : 1 ≤ b) (hab : a * b ≤ 1) : a = 1 := ha.eq_of_not_gt fun h => hab.not_lt <| one_lt_mul_of_lt_of_le' h hb end Left section Right variable [CovariantClass α α (swap (· * ·)) (· ≤ ·)] {a b : α} @[to_additive eq_zero_of_add_nonneg_right] theorem eq_one_of_one_le_mul_right (ha : a ≤ 1) (hb : b ≤ 1) (hab : 1 ≤ a * b) : b = 1 := hb.eq_of_not_lt fun h => hab.not_lt <| Right.mul_lt_one_of_le_of_lt ha h @[to_additive] theorem eq_one_of_mul_le_one_right (ha : 1 ≤ a) (hb : 1 ≤ b) (hab : a * b ≤ 1) : b = 1 := hb.eq_of_not_gt fun h => hab.not_lt <| Right.one_lt_mul_of_le_of_lt ha h end Right end PartialOrder section LinearOrder variable [LinearOrder α] theorem exists_square_le [CovariantClass α α (· * ·) (· < ·)] (a : α) : ∃ b : α, b * b ≤ a := by by_cases h : a < 1 · use a have : a * a < a * 1 := mul_lt_mul_left' h a rw [mul_one] at this exact le_of_lt this · use 1 push_neg at h rwa [mul_one] end LinearOrder end MulOneClass section Semigroup variable [Semigroup α] section PartialOrder variable [PartialOrder α] /- This is not instance, since we want to have an instance from `LeftCancelSemigroup`s to the appropriate `CovariantClass`. -/ /-- A semigroup with a partial order and satisfying `LeftCancelSemigroup` (i.e. `a * c < b * c → a < b`) is a `left_cancel Semigroup`. -/ @[to_additive "An additive semigroup with a partial order and satisfying `AddLeftCancelSemigroup` (i.e. `c + a < c + b → a < b`) is a `left_cancel AddSemigroup`."] def Contravariant.toLeftCancelSemigroup [ContravariantClass α α (· * ·) (· ≤ ·)] : LeftCancelSemigroup α := { ‹Semigroup α› with mul_left_cancel := fun a b c => mul_left_cancel'' } /- This is not instance, since we want to have an instance from `RightCancelSemigroup`s to the appropriate `CovariantClass`. -/ /-- A semigroup with a partial order and satisfying `RightCancelSemigroup` (i.e. `a * c < b * c → a < b`) is a `right_cancel Semigroup`. -/ @[to_additive "An additive semigroup with a partial order and satisfying `AddRightCancelSemigroup` (`a + c < b + c → a < b`) is a `right_cancel AddSemigroup`."] def Contravariant.toRightCancelSemigroup [ContravariantClass α α (swap (· * ·)) (· ≤ ·)] : RightCancelSemigroup α := { ‹Semigroup α› with mul_right_cancel := fun a b c => mul_right_cancel'' } end PartialOrder end Semigroup section Mono variable [Mul α] [Preorder α] [Preorder β] {f g : β → α} {s : Set β} @[to_additive const_add] theorem Monotone.const_mul' [CovariantClass α α (· * ·) (· ≤ ·)] (hf : Monotone f) (a : α) : Monotone fun x => a * f x := fun _ _ h => mul_le_mul_left' (hf h) a @[to_additive const_add] theorem MonotoneOn.const_mul' [CovariantClass α α (· * ·) (· ≤ ·)] (hf : MonotoneOn f s) (a : α) : MonotoneOn (fun x => a * f x) s := fun _ hx _ hy h => mul_le_mul_left' (hf hx hy h) a @[to_additive const_add] theorem Antitone.const_mul' [CovariantClass α α (· * ·) (· ≤ ·)] (hf : Antitone f) (a : α) : Antitone fun x => a * f x := fun _ _ h => mul_le_mul_left' (hf h) a @[to_additive const_add] theorem AntitoneOn.const_mul' [CovariantClass α α (· * ·) (· ≤ ·)] (hf : AntitoneOn f s) (a : α) : AntitoneOn (fun x => a * f x) s := fun _ hx _ hy h => mul_le_mul_left' (hf hx hy h) a @[to_additive add_const] theorem Monotone.mul_const' [CovariantClass α α (swap (· * ·)) (· ≤ ·)] (hf : Monotone f) (a : α) : Monotone fun x => f x * a := fun _ _ h => mul_le_mul_right' (hf h) a @[to_additive add_const] theorem MonotoneOn.mul_const' [CovariantClass α α (swap (· * ·)) (· ≤ ·)] (hf : MonotoneOn f s) (a : α) : MonotoneOn (fun x => f x * a) s := fun _ hx _ hy h => mul_le_mul_right' (hf hx hy h) a @[to_additive add_const] theorem Antitone.mul_const' [CovariantClass α α (swap (· * ·)) (· ≤ ·)] (hf : Antitone f) (a : α) : Antitone fun x => f x * a := fun _ _ h => mul_le_mul_right' (hf h) a @[to_additive add_const] theorem AntitoneOn.mul_const' [CovariantClass α α (swap (· * ·)) (· ≤ ·)] (hf : AntitoneOn f s) (a : α) : AntitoneOn (fun x => f x * a) s := fun _ hx _ hy h => mul_le_mul_right' (hf hx hy h) a /-- The product of two monotone functions is monotone. -/ @[to_additive add "The sum of two monotone functions is monotone."] theorem Monotone.mul' [CovariantClass α α (· * ·) (· ≤ ·)] [CovariantClass α α (swap (· * ·)) (· ≤ ·)] (hf : Monotone f) (hg : Monotone g) : Monotone fun x => f x * g x := fun _ _ h => mul_le_mul' (hf h) (hg h) /-- The product of two monotone functions is monotone. -/ @[to_additive add "The sum of two monotone functions is monotone."] theorem MonotoneOn.mul' [CovariantClass α α (· * ·) (· ≤ ·)] [CovariantClass α α (swap (· * ·)) (· ≤ ·)] (hf : MonotoneOn f s) (hg : MonotoneOn g s) : MonotoneOn (fun x => f x * g x) s := fun _ hx _ hy h => mul_le_mul' (hf hx hy h) (hg hx hy h) /-- The product of two antitone functions is antitone. -/ @[to_additive add "The sum of two antitone functions is antitone."] theorem Antitone.mul' [CovariantClass α α (· * ·) (· ≤ ·)] [CovariantClass α α (swap (· * ·)) (· ≤ ·)] (hf : Antitone f) (hg : Antitone g) : Antitone fun x => f x * g x := fun _ _ h => mul_le_mul' (hf h) (hg h) /-- The product of two antitone functions is antitone. -/ @[to_additive add "The sum of two antitone functions is antitone."] theorem AntitoneOn.mul' [CovariantClass α α (· * ·) (· ≤ ·)] [CovariantClass α α (swap (· * ·)) (· ≤ ·)] (hf : AntitoneOn f s) (hg : AntitoneOn g s) : AntitoneOn (fun x => f x * g x) s := fun _ hx _ hy h => mul_le_mul' (hf hx hy h) (hg hx hy h) section Left variable [CovariantClass α α (· * ·) (· < ·)] @[to_additive const_add] theorem StrictMono.const_mul' (hf : StrictMono f) (c : α) : StrictMono fun x => c * f x := fun _ _ ab => mul_lt_mul_left' (hf ab) c @[to_additive const_add] theorem StrictMonoOn.const_mul' (hf : StrictMonoOn f s) (c : α) : StrictMonoOn (fun x => c * f x) s := fun _ ha _ hb ab => mul_lt_mul_left' (hf ha hb ab) c @[to_additive const_add] theorem StrictAnti.const_mul' (hf : StrictAnti f) (c : α) : StrictAnti fun x => c * f x := fun _ _ ab => mul_lt_mul_left' (hf ab) c @[to_additive const_add] theorem StrictAntiOn.const_mul' (hf : StrictAntiOn f s) (c : α) : StrictAntiOn (fun x => c * f x) s := fun _ ha _ hb ab => mul_lt_mul_left' (hf ha hb ab) c end Left section Right variable [CovariantClass α α (swap (· * ·)) (· < ·)] @[to_additive add_const] theorem StrictMono.mul_const' (hf : StrictMono f) (c : α) : StrictMono fun x => f x * c := fun _ _ ab => mul_lt_mul_right' (hf ab) c @[to_additive add_const] theorem StrictMonoOn.mul_const' (hf : StrictMonoOn f s) (c : α) : StrictMonoOn (fun x => f x * c) s := fun _ ha _ hb ab => mul_lt_mul_right' (hf ha hb ab) c @[to_additive add_const] theorem StrictAnti.mul_const' (hf : StrictAnti f) (c : α) : StrictAnti fun x => f x * c := fun _ _ ab => mul_lt_mul_right' (hf ab) c @[to_additive add_const] theorem StrictAntiOn.mul_const' (hf : StrictAntiOn f s) (c : α) : StrictAntiOn (fun x => f x * c) s := fun _ ha _ hb ab => mul_lt_mul_right' (hf ha hb ab) c end Right /-- The product of two strictly monotone functions is strictly monotone. -/ @[to_additive add "The sum of two strictly monotone functions is strictly monotone."] theorem StrictMono.mul' [CovariantClass α α (· * ·) (· < ·)] [CovariantClass α α (swap (· * ·)) (· < ·)] (hf : StrictMono f) (hg : StrictMono g) : StrictMono fun x => f x * g x := fun _ _ ab => mul_lt_mul_of_lt_of_lt (hf ab) (hg ab) /-- The product of two strictly monotone functions is strictly monotone. -/ @[to_additive add "The sum of two strictly monotone functions is strictly monotone."] theorem StrictMonoOn.mul' [CovariantClass α α (· * ·) (· < ·)] [CovariantClass α α (swap (· * ·)) (· < ·)] (hf : StrictMonoOn f s) (hg : StrictMonoOn g s) : StrictMonoOn (fun x => f x * g x) s := fun _ ha _ hb ab => mul_lt_mul_of_lt_of_lt (hf ha hb ab) (hg ha hb ab) /-- The product of two strictly antitone functions is strictly antitone. -/ @[to_additive add "The sum of two strictly antitone functions is strictly antitone."] theorem StrictAnti.mul' [CovariantClass α α (· * ·) (· < ·)] [CovariantClass α α (swap (· * ·)) (· < ·)] (hf : StrictAnti f) (hg : StrictAnti g) : StrictAnti fun x => f x * g x := fun _ _ ab => mul_lt_mul_of_lt_of_lt (hf ab) (hg ab) /-- The product of two strictly antitone functions is strictly antitone. -/ @[to_additive add "The sum of two strictly antitone functions is strictly antitone."] theorem StrictAntiOn.mul' [CovariantClass α α (· * ·) (· < ·)] [CovariantClass α α (swap (· * ·)) (· < ·)] (hf : StrictAntiOn f s) (hg : StrictAntiOn g s) : StrictAntiOn (fun x => f x * g x) s := fun _ ha _ hb ab => mul_lt_mul_of_lt_of_lt (hf ha hb ab) (hg ha hb ab) /-- The product of a monotone function and a strictly monotone function is strictly monotone. -/ @[to_additive add_strictMono "The sum of a monotone function and a strictly monotone function is strictly monotone."] theorem Monotone.mul_strictMono' [CovariantClass α α (· * ·) (· < ·)] [CovariantClass α α (swap (· * ·)) (· ≤ ·)] {f g : β → α} (hf : Monotone f) (hg : StrictMono g) : StrictMono fun x => f x * g x := fun _ _ h => mul_lt_mul_of_le_of_lt (hf h.le) (hg h) /-- The product of a monotone function and a strictly monotone function is strictly monotone. -/ @[to_additive add_strictMono "The sum of a monotone function and a strictly monotone function is strictly monotone."] theorem MonotoneOn.mul_strictMono' [CovariantClass α α (· * ·) (· < ·)] [CovariantClass α α (swap (· * ·)) (· ≤ ·)] {f g : β → α} (hf : MonotoneOn f s) (hg : StrictMonoOn g s) : StrictMonoOn (fun x => f x * g x) s := fun _ hx _ hy h => mul_lt_mul_of_le_of_lt (hf hx hy h.le) (hg hx hy h) /-- The product of an antitone function and a strictly antitone function is strictly antitone. -/ @[to_additive add_strictAnti "The sum of an antitone function and a strictly antitone function is strictly antitone."] theorem Antitone.mul_strictAnti' [CovariantClass α α (· * ·) (· < ·)] [CovariantClass α α (swap (· * ·)) (· ≤ ·)] {f g : β → α} (hf : Antitone f) (hg : StrictAnti g) : StrictAnti fun x => f x * g x := fun _ _ h => mul_lt_mul_of_le_of_lt (hf h.le) (hg h) /-- The product of an antitone function and a strictly antitone function is strictly antitone. -/ @[to_additive add_strictAnti "The sum of an antitone function and a strictly antitone function is strictly antitone."] theorem AntitoneOn.mul_strictAnti' [CovariantClass α α (· * ·) (· < ·)] [CovariantClass α α (swap (· * ·)) (· ≤ ·)] {f g : β → α} (hf : AntitoneOn f s) (hg : StrictAntiOn g s) : StrictAntiOn (fun x => f x * g x) s := fun _ hx _ hy h => mul_lt_mul_of_le_of_lt (hf hx hy h.le) (hg hx hy h) variable [CovariantClass α α (· * ·) (· ≤ ·)] [CovariantClass α α (swap (· * ·)) (· < ·)] /-- The product of a strictly monotone function and a monotone function is strictly monotone. -/ @[to_additive add_monotone "The sum of a strictly monotone function and a monotone function is strictly monotone."] theorem StrictMono.mul_monotone' (hf : StrictMono f) (hg : Monotone g) : StrictMono fun x => f x * g x := fun _ _ h => mul_lt_mul_of_lt_of_le (hf h) (hg h.le) /-- The product of a strictly monotone function and a monotone function is strictly monotone. -/ @[to_additive add_monotone "The sum of a strictly monotone function and a monotone function is strictly monotone."] theorem StrictMonoOn.mul_monotone' (hf : StrictMonoOn f s) (hg : MonotoneOn g s) : StrictMonoOn (fun x => f x * g x) s := fun _ hx _ hy h => mul_lt_mul_of_lt_of_le (hf hx hy h) (hg hx hy h.le) /-- The product of a strictly antitone function and an antitone function is strictly antitone. -/ @[to_additive add_antitone "The sum of a strictly antitone function and an antitone function is strictly antitone."] theorem StrictAnti.mul_antitone' (hf : StrictAnti f) (hg : Antitone g) : StrictAnti fun x => f x * g x := fun _ _ h => mul_lt_mul_of_lt_of_le (hf h) (hg h.le) /-- The product of a strictly antitone function and an antitone function is strictly antitone. -/ @[to_additive add_antitone "The sum of a strictly antitone function and an antitone function is strictly antitone."] theorem StrictAntiOn.mul_antitone' (hf : StrictAntiOn f s) (hg : AntitoneOn g s) : StrictAntiOn (fun x => f x * g x) s := fun _ hx _ hy h => mul_lt_mul_of_lt_of_le (hf hx hy h) (hg hx hy h.le) @[to_additive (attr := simp) cmp_add_left] theorem cmp_mul_left' {α : Type*} [Mul α] [LinearOrder α] [CovariantClass α α (· * ·) (· < ·)] (a b c : α) : cmp (a * b) (a * c) = cmp b c := (strictMono_id.const_mul' a).cmp_map_eq b c @[to_additive (attr := simp) cmp_add_right] theorem cmp_mul_right' {α : Type*} [Mul α] [LinearOrder α] [CovariantClass α α (swap (· * ·)) (· < ·)] (a b c : α) : cmp (a * c) (b * c) = cmp a b := (strictMono_id.mul_const' c).cmp_map_eq a b end Mono /-- An element `a : α` is `MulLECancellable` if `x ↦ a * x` is order-reflecting. We will make a separate version of many lemmas that require `[ContravariantClass α α (*) (≤)]` with `MulLECancellable` assumptions instead. These lemmas can then be instantiated to specific types, like `ENNReal`, where we can replace the assumption `AddLECancellable x` by `x ≠ ∞`. -/ @[to_additive "An element `a : α` is `AddLECancellable` if `x ↦ a + x` is order-reflecting. We will make a separate version of many lemmas that require `[ContravariantClass α α (+) (≤)]` with `AddLECancellable` assumptions instead. These lemmas can then be instantiated to specific types, like `ENNReal`, where we can replace the assumption `AddLECancellable x` by `x ≠ ∞`. "] def MulLECancellable [Mul α] [LE α] (a : α) : Prop := ∀ ⦃b c⦄, a * b ≤ a * c → b ≤ c @[to_additive] theorem Contravariant.MulLECancellable [Mul α] [LE α] [ContravariantClass α α (· * ·) (· ≤ ·)] {a : α} : MulLECancellable a := fun _ _ => le_of_mul_le_mul_left' @[to_additive] theorem mulLECancellable_one [Monoid α] [LE α] : MulLECancellable (1 : α) := fun a b => by simpa only [one_mul] using id namespace MulLECancellable @[to_additive] protected theorem Injective [Mul α] [PartialOrder α] {a : α} (ha : MulLECancellable a) : Injective (a * ·) := fun _ _ h => le_antisymm (ha h.le) (ha h.ge) @[to_additive] protected theorem inj [Mul α] [PartialOrder α] {a b c : α} (ha : MulLECancellable a) : a * b = a * c ↔ b = c := ha.Injective.eq_iff @[to_additive] protected theorem injective_left [Mul α] [i : IsSymmOp α α (· * ·)] [PartialOrder α] {a : α} (ha : MulLECancellable a) : Injective (· * a) := fun b c h => ha.Injective <| by dsimp; rwa [i.symm_op a, i.symm_op a] @[to_additive] protected theorem inj_left [Mul α] [IsSymmOp α α (· * ·)] [PartialOrder α] {a b c : α} (hc : MulLECancellable c) : a * c = b * c ↔ a = b := hc.injective_left.eq_iff variable [LE α] @[to_additive] protected theorem mul_le_mul_iff_left [Mul α] [CovariantClass α α (· * ·) (· ≤ ·)] {a b c : α} (ha : MulLECancellable a) : a * b ≤ a * c ↔ b ≤ c := ⟨fun h => ha h, fun h => mul_le_mul_left' h a⟩ @[to_additive] protected theorem mul_le_mul_iff_right [Mul α] [i : IsSymmOp α α (· * ·)] [CovariantClass α α (· * ·) (· ≤ ·)] {a b c : α} (ha : MulLECancellable a) : b * a ≤ c * a ↔ b ≤ c := by rw [i.symm_op b, i.symm_op c, ha.mul_le_mul_iff_left] @[to_additive] protected theorem le_mul_iff_one_le_right [MulOneClass α] [CovariantClass α α (· * ·) (· ≤ ·)] {a b : α} (ha : MulLECancellable a) : a ≤ a * b ↔ 1 ≤ b := Iff.trans (by rw [mul_one]) ha.mul_le_mul_iff_left @[to_additive] protected theorem mul_le_iff_le_one_right [MulOneClass α] [CovariantClass α α (· * ·) (· ≤ ·)] {a b : α} (ha : MulLECancellable a) : a * b ≤ a ↔ b ≤ 1 := Iff.trans (by rw [mul_one]) ha.mul_le_mul_iff_left @[to_additive] protected theorem le_mul_iff_one_le_left [MulOneClass α] [i : IsSymmOp α α (· * ·)] [CovariantClass α α (· * ·) (· ≤ ·)] {a b : α} (ha : MulLECancellable a) : a ≤ b * a ↔ 1 ≤ b := by rw [i.symm_op, ha.le_mul_iff_one_le_right] @[to_additive] protected theorem mul_le_iff_le_one_left [MulOneClass α] [i : IsSymmOp α α (· * ·)] [CovariantClass α α (· * ·) (· ≤ ·)] {a b : α} (ha : MulLECancellable a) : b * a ≤ a ↔ b ≤ 1 := by rw [i.symm_op, ha.mul_le_iff_le_one_right] end MulLECancellable
Algebra\Order\Monoid\Unbundled\Defs.lean
/- Copyright (c) 2021 Damiano Testa. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Damiano Testa -/ import Mathlib.Algebra.Group.Basic import Mathlib.Order.Basic import Mathlib.Order.Monotone.Basic /-! # Covariants and contravariants This file contains general lemmas and instances to work with the interactions between a relation and an action on a Type. The intended application is the splitting of the ordering from the algebraic assumptions on the operations in the `Ordered[...]` hierarchy. The strategy is to introduce two more flexible typeclasses, `CovariantClass` and `ContravariantClass`: * `CovariantClass` models the implication `a ≤ b → c * a ≤ c * b` (multiplication is monotone), * `ContravariantClass` models the implication `a * b < a * c → b < c`. Since `Co(ntra)variantClass` takes as input the operation (typically `(+)` or `(*)`) and the order relation (typically `(≤)` or `(<)`), these are the only two typeclasses that I have used. The general approach is to formulate the lemma that you are interested in and prove it, with the `Ordered[...]` typeclass of your liking. After that, you convert the single typeclass, say `[OrderedCancelMonoid M]`, into three typeclasses, e.g. `[CancelMonoid M] [PartialOrder M] [CovariantClass M M (Function.swap (*)) (≤)]` and have a go at seeing if the proof still works! Note that it is possible to combine several `Co(ntra)variantClass` assumptions together. Indeed, the usual ordered typeclasses arise from assuming the pair `[CovariantClass M M (*) (≤)] [ContravariantClass M M (*) (<)]` on top of order/algebraic assumptions. A formal remark is that normally `CovariantClass` uses the `(≤)`-relation, while `ContravariantClass` uses the `(<)`-relation. This need not be the case in general, but seems to be the most common usage. In the opposite direction, the implication ```lean [Semigroup α] [PartialOrder α] [ContravariantClass α α (*) (≤)] → LeftCancelSemigroup α ``` holds -- note the `Co*ntra*` assumption on the `(≤)`-relation. # Formalization notes We stick to the convention of using `Function.swap (*)` (or `Function.swap (+)`), for the typeclass assumptions, since `Function.swap` is slightly better behaved than `flip`. However, sometimes as a **non-typeclass** assumption, we prefer `flip (*)` (or `flip (+)`), as it is easier to use. -/ -- TODO: convert `ExistsMulOfLE`, `ExistsAddOfLE`? -- TODO: relationship with `Con/AddCon` -- TODO: include equivalence of `LeftCancelSemigroup` with -- `Semigroup PartialOrder ContravariantClass α α (*) (≤)`? -- TODO : use ⇒, as per Eric's suggestion? See -- https://leanprover.zulipchat.com/#narrow/stream/116395-maths/topic/ordered.20stuff/near/236148738 -- for a discussion. open Function section Variants variable {M N : Type*} (μ : M → N → N) (r : N → N → Prop) variable (M N) /-- `Covariant` is useful to formulate succinctly statements about the interactions between an action of a Type on another one and a relation on the acted-upon Type. See the `CovariantClass` doc-string for its meaning. -/ def Covariant : Prop := ∀ (m) {n₁ n₂}, r n₁ n₂ → r (μ m n₁) (μ m n₂) /-- `Contravariant` is useful to formulate succinctly statements about the interactions between an action of a Type on another one and a relation on the acted-upon Type. See the `ContravariantClass` doc-string for its meaning. -/ def Contravariant : Prop := ∀ (m) {n₁ n₂}, r (μ m n₁) (μ m n₂) → r n₁ n₂ /-- Given an action `μ` of a Type `M` on a Type `N` and a relation `r` on `N`, informally, the `CovariantClass` says that "the action `μ` preserves the relation `r`." More precisely, the `CovariantClass` is a class taking two Types `M N`, together with an "action" `μ : M → N → N` and a relation `r : N → N → Prop`. Its unique field `elim` is the assertion that for all `m ∈ M` and all elements `n₁, n₂ ∈ N`, if the relation `r` holds for the pair `(n₁, n₂)`, then, the relation `r` also holds for the pair `(μ m n₁, μ m n₂)`, obtained from `(n₁, n₂)` by acting upon it by `m`. If `m : M` and `h : r n₁ n₂`, then `CovariantClass.elim m h : r (μ m n₁) (μ m n₂)`. -/ class CovariantClass : Prop where /-- For all `m ∈ M` and all elements `n₁, n₂ ∈ N`, if the relation `r` holds for the pair `(n₁, n₂)`, then, the relation `r` also holds for the pair `(μ m n₁, μ m n₂)` -/ protected elim : Covariant M N μ r /-- Given an action `μ` of a Type `M` on a Type `N` and a relation `r` on `N`, informally, the `ContravariantClass` says that "if the result of the action `μ` on a pair satisfies the relation `r`, then the initial pair satisfied the relation `r`." More precisely, the `ContravariantClass` is a class taking two Types `M N`, together with an "action" `μ : M → N → N` and a relation `r : N → N → Prop`. Its unique field `elim` is the assertion that for all `m ∈ M` and all elements `n₁, n₂ ∈ N`, if the relation `r` holds for the pair `(μ m n₁, μ m n₂)` obtained from `(n₁, n₂)` by acting upon it by `m`, then, the relation `r` also holds for the pair `(n₁, n₂)`. If `m : M` and `h : r (μ m n₁) (μ m n₂)`, then `ContravariantClass.elim m h : r n₁ n₂`. -/ class ContravariantClass : Prop where /-- For all `m ∈ M` and all elements `n₁, n₂ ∈ N`, if the relation `r` holds for the pair `(μ m n₁, μ m n₂)` obtained from `(n₁, n₂)` by acting upon it by `m`, then, the relation `r` also holds for the pair `(n₁, n₂)`. -/ protected elim : Contravariant M N μ r theorem rel_iff_cov [CovariantClass M N μ r] [ContravariantClass M N μ r] (m : M) {a b : N} : r (μ m a) (μ m b) ↔ r a b := ⟨ContravariantClass.elim _, CovariantClass.elim _⟩ section flip variable {M N μ r} theorem Covariant.flip (h : Covariant M N μ r) : Covariant M N μ (flip r) := fun a _ _ ↦ h a theorem Contravariant.flip (h : Contravariant M N μ r) : Contravariant M N μ (flip r) := fun a _ _ ↦ h a end flip section Covariant variable {M N μ r} [CovariantClass M N μ r] theorem act_rel_act_of_rel (m : M) {a b : N} (ab : r a b) : r (μ m a) (μ m b) := CovariantClass.elim _ ab @[to_additive] theorem Group.covariant_iff_contravariant [Group N] : Covariant N N (· * ·) r ↔ Contravariant N N (· * ·) r := by refine ⟨fun h a b c bc ↦ ?_, fun h a b c bc ↦ ?_⟩ · rw [← inv_mul_cancel_left a b, ← inv_mul_cancel_left a c] exact h a⁻¹ bc · rw [← inv_mul_cancel_left a b, ← inv_mul_cancel_left a c] at bc exact h a⁻¹ bc @[to_additive] instance (priority := 100) Group.covconv [Group N] [CovariantClass N N (· * ·) r] : ContravariantClass N N (· * ·) r := ⟨Group.covariant_iff_contravariant.mp CovariantClass.elim⟩ @[to_additive] theorem Group.covariant_swap_iff_contravariant_swap [Group N] : Covariant N N (swap (· * ·)) r ↔ Contravariant N N (swap (· * ·)) r := by refine ⟨fun h a b c bc ↦ ?_, fun h a b c bc ↦ ?_⟩ · rw [← mul_inv_cancel_right b a, ← mul_inv_cancel_right c a] exact h a⁻¹ bc · rw [← mul_inv_cancel_right b a, ← mul_inv_cancel_right c a] at bc exact h a⁻¹ bc @[to_additive] instance (priority := 100) Group.covconv_swap [Group N] [CovariantClass N N (swap (· * ·)) r] : ContravariantClass N N (swap (· * ·)) r := ⟨Group.covariant_swap_iff_contravariant_swap.mp CovariantClass.elim⟩ section Trans variable [IsTrans N r] (m n : M) {a b c d : N} -- Lemmas with 3 elements. theorem act_rel_of_rel_of_act_rel (ab : r a b) (rl : r (μ m b) c) : r (μ m a) c := _root_.trans (act_rel_act_of_rel m ab) rl theorem rel_act_of_rel_of_rel_act (ab : r a b) (rr : r c (μ m a)) : r c (μ m b) := _root_.trans rr (act_rel_act_of_rel _ ab) end Trans end Covariant -- Lemma with 4 elements. section MEqN variable {M N μ r} {mu : N → N → N} [IsTrans N r] [i : CovariantClass N N mu r] [i' : CovariantClass N N (swap mu) r] {a b c d : N} theorem act_rel_act_of_rel_of_rel (ab : r a b) (cd : r c d) : r (mu a c) (mu b d) := _root_.trans (@act_rel_act_of_rel _ _ (swap mu) r _ c _ _ ab) (act_rel_act_of_rel b cd) end MEqN section Contravariant variable {M N μ r} [ContravariantClass M N μ r] theorem rel_of_act_rel_act (m : M) {a b : N} (ab : r (μ m a) (μ m b)) : r a b := ContravariantClass.elim _ ab section Trans variable [IsTrans N r] (m n : M) {a b c d : N} -- Lemmas with 3 elements. theorem act_rel_of_act_rel_of_rel_act_rel (ab : r (μ m a) b) (rl : r (μ m b) (μ m c)) : r (μ m a) c := _root_.trans ab (rel_of_act_rel_act m rl) theorem rel_act_of_act_rel_act_of_rel_act (ab : r (μ m a) (μ m b)) (rr : r b (μ m c)) : r a (μ m c) := _root_.trans (rel_of_act_rel_act m ab) rr end Trans end Contravariant section Monotone variable {α : Type*} {M N μ} [Preorder α] [Preorder N] variable {f : N → α} /-- The partial application of a constant to a covariant operator is monotone. -/ theorem Covariant.monotone_of_const [CovariantClass M N μ (· ≤ ·)] (m : M) : Monotone (μ m) := fun _ _ ↦ CovariantClass.elim m /-- A monotone function remains monotone when composed with the partial application of a covariant operator. E.g., `∀ (m : ℕ), Monotone f → Monotone (fun n ↦ f (m + n))`. -/ theorem Monotone.covariant_of_const [CovariantClass M N μ (· ≤ ·)] (hf : Monotone f) (m : M) : Monotone (f <| μ m ·) := hf.comp (Covariant.monotone_of_const m) /-- Same as `Monotone.covariant_of_const`, but with the constant on the other side of the operator. E.g., `∀ (m : ℕ), Monotone f → Monotone (fun n ↦ f (n + m))`. -/ theorem Monotone.covariant_of_const' {μ : N → N → N} [CovariantClass N N (swap μ) (· ≤ ·)] (hf : Monotone f) (m : N) : Monotone (f <| μ · m) := Monotone.covariant_of_const (μ := swap μ) hf m /-- Dual of `Monotone.covariant_of_const` -/ theorem Antitone.covariant_of_const [CovariantClass M N μ (· ≤ ·)] (hf : Antitone f) (m : M) : Antitone (f <| μ m ·) := hf.comp_monotone <| Covariant.monotone_of_const m /-- Dual of `Monotone.covariant_of_const'` -/ theorem Antitone.covariant_of_const' {μ : N → N → N} [CovariantClass N N (swap μ) (· ≤ ·)] (hf : Antitone f) (m : N) : Antitone (f <| μ · m) := Antitone.covariant_of_const (μ := swap μ) hf m end Monotone theorem covariant_le_of_covariant_lt [PartialOrder N] : Covariant M N μ (· < ·) → Covariant M N μ (· ≤ ·) := by intro h a b c bc rcases bc.eq_or_lt with (rfl | bc) · exact le_rfl · exact (h _ bc).le theorem covariantClass_le_of_lt [PartialOrder N] [CovariantClass M N μ (· < ·)] : CovariantClass M N μ (· ≤ ·) := ⟨covariant_le_of_covariant_lt _ _ _ CovariantClass.elim⟩ theorem contravariant_le_iff_contravariant_lt_and_eq [PartialOrder N] : Contravariant M N μ (· ≤ ·) ↔ Contravariant M N μ (· < ·) ∧ Contravariant M N μ (· = ·) := by refine ⟨fun h ↦ ⟨fun a b c bc ↦ ?_, fun a b c bc ↦ ?_⟩, fun h ↦ fun a b c bc ↦ ?_⟩ · exact (h a bc.le).lt_of_ne (by rintro rfl; exact lt_irrefl _ bc) · exact (h a bc.le).antisymm (h a bc.ge) · exact bc.lt_or_eq.elim (fun bc ↦ (h.1 a bc).le) (fun bc ↦ (h.2 a bc).le) theorem contravariant_lt_of_contravariant_le [PartialOrder N] : Contravariant M N μ (· ≤ ·) → Contravariant M N μ (· < ·) := And.left ∘ (contravariant_le_iff_contravariant_lt_and_eq M N μ).mp theorem covariant_le_iff_contravariant_lt [LinearOrder N] : Covariant M N μ (· ≤ ·) ↔ Contravariant M N μ (· < ·) := ⟨fun h _ _ _ bc ↦ not_le.mp fun k ↦ bc.not_le (h _ k), fun h _ _ _ bc ↦ not_lt.mp fun k ↦ bc.not_lt (h _ k)⟩ theorem covariant_lt_iff_contravariant_le [LinearOrder N] : Covariant M N μ (· < ·) ↔ Contravariant M N μ (· ≤ ·) := ⟨fun h _ _ _ bc ↦ not_lt.mp fun k ↦ bc.not_lt (h _ k), fun h _ _ _ bc ↦ not_le.mp fun k ↦ bc.not_le (h _ k)⟩ variable (mu : N → N → N) theorem covariant_flip_iff [IsSymmOp N N mu] : Covariant N N (flip mu) r ↔ Covariant N N mu r := by rw [IsSymmOp.flip_eq] theorem contravariant_flip_iff [IsSymmOp N N mu] : Contravariant N N (flip mu) r ↔ Contravariant N N mu r := by rw [IsSymmOp.flip_eq] instance contravariant_lt_of_covariant_le [LinearOrder N] [CovariantClass N N mu (· ≤ ·)] : ContravariantClass N N mu (· < ·) where elim := (covariant_le_iff_contravariant_lt N N mu).mp CovariantClass.elim instance covariant_lt_of_contravariant_le [LinearOrder N] [ContravariantClass N N mu (· ≤ ·)] : CovariantClass N N mu (· < ·) where elim := (covariant_lt_iff_contravariant_le N N mu).mpr ContravariantClass.elim @[to_additive] instance covariant_swap_mul_of_covariant_mul [CommSemigroup N] [CovariantClass N N (· * ·) r] : CovariantClass N N (swap (· * ·)) r where elim := (covariant_flip_iff N r (· * ·)).mpr CovariantClass.elim @[to_additive] instance contravariant_swap_mul_of_contravariant_mul [CommSemigroup N] [ContravariantClass N N (· * ·) r] : ContravariantClass N N (swap (· * ·)) r where elim := (contravariant_flip_iff N r (· * ·)).mpr ContravariantClass.elim theorem covariant_lt_of_covariant_le_of_contravariant_eq [ContravariantClass M N μ (· = ·)] [PartialOrder N] [CovariantClass M N μ (· ≤ ·)] : CovariantClass M N μ (· < ·) where elim a _ _ bc := (CovariantClass.elim a bc.le).lt_of_ne (bc.ne ∘ ContravariantClass.elim _) theorem contravariant_le_of_contravariant_eq_and_lt [PartialOrder N] [ContravariantClass M N μ (· = ·)] [ContravariantClass M N μ (· < ·)] : ContravariantClass M N μ (· ≤ ·) where elim := (contravariant_le_iff_contravariant_lt_and_eq M N μ).mpr ⟨ContravariantClass.elim, ContravariantClass.elim⟩ /- TODO: redefine `IsLeftCancel N mu` as abbrev of `ContravariantClass N N mu (· = ·)`, redefine `IsRightCancel N mu` as abbrev of `ContravariantClass N N (flip mu) (· = ·)`, redefine `IsLeftCancelMul` as abbrev of `IsLeftCancel`, then the following four instances (actually eight) can be removed in favor of the above two. -/ @[to_additive] instance IsLeftCancelMul.covariant_mul_lt_of_covariant_mul_le [Mul N] [IsLeftCancelMul N] [PartialOrder N] [CovariantClass N N (· * ·) (· ≤ ·)] : CovariantClass N N (· * ·) (· < ·) where elim a _ _ bc := (CovariantClass.elim a bc.le).lt_of_ne ((mul_ne_mul_right a).mpr bc.ne) @[to_additive] instance IsRightCancelMul.covariant_swap_mul_lt_of_covariant_swap_mul_le [Mul N] [IsRightCancelMul N] [PartialOrder N] [CovariantClass N N (swap (· * ·)) (· ≤ ·)] : CovariantClass N N (swap (· * ·)) (· < ·) where elim a _ _ bc := (CovariantClass.elim a bc.le).lt_of_ne ((mul_ne_mul_left a).mpr bc.ne) @[to_additive] instance IsLeftCancelMul.contravariant_mul_le_of_contravariant_mul_lt [Mul N] [IsLeftCancelMul N] [PartialOrder N] [ContravariantClass N N (· * ·) (· < ·)] : ContravariantClass N N (· * ·) (· ≤ ·) where elim := (contravariant_le_iff_contravariant_lt_and_eq N N _).mpr ⟨ContravariantClass.elim, fun _ ↦ mul_left_cancel⟩ @[to_additive] instance IsRightCancelMul.contravariant_swap_mul_le_of_contravariant_swap_mul_lt [Mul N] [IsRightCancelMul N] [PartialOrder N] [ContravariantClass N N (swap (· * ·)) (· < ·)] : ContravariantClass N N (swap (· * ·)) (· ≤ ·) where elim := (contravariant_le_iff_contravariant_lt_and_eq N N _).mpr ⟨ContravariantClass.elim, fun _ ↦ mul_right_cancel⟩ end Variants
Algebra\Order\Monoid\Unbundled\ExistsOfLE.lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro, Johannes Hölzl -/ import Mathlib.Algebra.Order.Monoid.Unbundled.Basic import Mathlib.Order.MinMax /-! # Unbundled and weaker forms of canonically ordered monoids -/ universe u variable {α : Type u} /-- An `OrderedCommMonoid` with one-sided 'division' in the sense that if `a ≤ b`, there is some `c` for which `a * c = b`. This is a weaker version of the condition on canonical orderings defined by `CanonicallyOrderedCommMonoid`. -/ class ExistsMulOfLE (α : Type u) [Mul α] [LE α] : Prop where /-- For `a ≤ b`, `a` left divides `b` -/ exists_mul_of_le : ∀ {a b : α}, a ≤ b → ∃ c : α, b = a * c /-- An `OrderedAddCommMonoid` with one-sided 'subtraction' in the sense that if `a ≤ b`, then there is some `c` for which `a + c = b`. This is a weaker version of the condition on canonical orderings defined by `CanonicallyOrderedAddCommMonoid`. -/ class ExistsAddOfLE (α : Type u) [Add α] [LE α] : Prop where /-- For `a ≤ b`, there is a `c` so `b = a + c`. -/ exists_add_of_le : ∀ {a b : α}, a ≤ b → ∃ c : α, b = a + c attribute [to_additive] ExistsMulOfLE export ExistsMulOfLE (exists_mul_of_le) export ExistsAddOfLE (exists_add_of_le) -- See note [lower instance priority] @[to_additive] instance (priority := 100) Group.existsMulOfLE (α : Type u) [Group α] [LE α] : ExistsMulOfLE α := ⟨fun {a b} _ => ⟨a⁻¹ * b, (mul_inv_cancel_left _ _).symm⟩⟩ section MulOneClass variable [MulOneClass α] [Preorder α] [ContravariantClass α α (· * ·) (· < ·)] [ExistsMulOfLE α] {a b : α} @[to_additive] theorem exists_one_lt_mul_of_lt' (h : a < b) : ∃ c, 1 < c ∧ a * c = b := by obtain ⟨c, rfl⟩ := exists_mul_of_le h.le exact ⟨c, one_lt_of_lt_mul_right h, rfl⟩ end MulOneClass section ExistsMulOfLE variable [LinearOrder α] [DenselyOrdered α] [Monoid α] [ExistsMulOfLE α] [CovariantClass α α (· * ·) (· < ·)] [ContravariantClass α α (· * ·) (· < ·)] {a b : α} @[to_additive] theorem le_of_forall_one_lt_le_mul (h : ∀ ε : α, 1 < ε → a ≤ b * ε) : a ≤ b := le_of_forall_le_of_dense fun x hxb => by obtain ⟨ε, rfl⟩ := exists_mul_of_le hxb.le exact h _ ((lt_mul_iff_one_lt_right' b).1 hxb) @[to_additive] theorem le_of_forall_one_lt_lt_mul' (h : ∀ ε : α, 1 < ε → a < b * ε) : a ≤ b := le_of_forall_one_lt_le_mul fun ε hε => (h ε hε).le @[to_additive] theorem le_iff_forall_one_lt_lt_mul' : a ≤ b ↔ ∀ ε, 1 < ε → a < b * ε := ⟨fun h _ => lt_mul_of_le_of_one_lt h, le_of_forall_one_lt_lt_mul'⟩ end ExistsMulOfLE
Algebra\Order\Monoid\Unbundled\MinMax.lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro, Johannes Hölzl -/ import Mathlib.Algebra.Order.Monoid.Unbundled.Basic /-! # Lemmas about `min` and `max` in an ordered monoid. -/ open Function variable {α β : Type*} /-! Some lemmas about types that have an ordering and a binary operation, with no rules relating them. -/ section CommSemigroup variable [LinearOrder α] [CommSemigroup β] @[to_additive] lemma fn_min_mul_fn_max (f : α → β) (a b : α) : f (min a b) * f (max a b) = f a * f b := by obtain h | h := le_total a b <;> simp [h, mul_comm] @[to_additive] lemma fn_max_mul_fn_min (f : α → β) (a b : α) : f (max a b) * f (min a b) = f a * f b := by obtain h | h := le_total a b <;> simp [h, mul_comm] variable [CommSemigroup α] @[to_additive (attr := simp)] lemma min_mul_max (a b : α) : min a b * max a b = a * b := fn_min_mul_fn_max id _ _ @[to_additive (attr := simp)] lemma max_mul_min (a b : α) : max a b * min a b = a * b := fn_max_mul_fn_min id _ _ end CommSemigroup section CovariantClassMulLe variable [LinearOrder α] section Mul variable [Mul α] section Left variable [CovariantClass α α (· * ·) (· ≤ ·)] @[to_additive] theorem min_mul_mul_left (a b c : α) : min (a * b) (a * c) = a * min b c := (monotone_id.const_mul' a).map_min.symm @[to_additive] theorem max_mul_mul_left (a b c : α) : max (a * b) (a * c) = a * max b c := (monotone_id.const_mul' a).map_max.symm end Left section Right variable [CovariantClass α α (Function.swap (· * ·)) (· ≤ ·)] @[to_additive] theorem min_mul_mul_right (a b c : α) : min (a * c) (b * c) = min a b * c := (monotone_id.mul_const' c).map_min.symm @[to_additive] theorem max_mul_mul_right (a b c : α) : max (a * c) (b * c) = max a b * c := (monotone_id.mul_const' c).map_max.symm end Right @[to_additive] theorem lt_or_lt_of_mul_lt_mul [CovariantClass α α (· * ·) (· ≤ ·)] [CovariantClass α α (Function.swap (· * ·)) (· ≤ ·)] {a₁ a₂ b₁ b₂ : α} : a₁ * b₁ < a₂ * b₂ → a₁ < a₂ ∨ b₁ < b₂ := by contrapose! exact fun h => mul_le_mul' h.1 h.2 @[to_additive] theorem le_or_lt_of_mul_le_mul [CovariantClass α α (· * ·) (· ≤ ·)] [CovariantClass α α (Function.swap (· * ·)) (· < ·)] {a₁ a₂ b₁ b₂ : α} : a₁ * b₁ ≤ a₂ * b₂ → a₁ ≤ a₂ ∨ b₁ < b₂ := by contrapose! exact fun h => mul_lt_mul_of_lt_of_le h.1 h.2 @[to_additive] theorem lt_or_le_of_mul_le_mul [CovariantClass α α (· * ·) (· < ·)] [CovariantClass α α (Function.swap (· * ·)) (· ≤ ·)] {a₁ a₂ b₁ b₂ : α} : a₁ * b₁ ≤ a₂ * b₂ → a₁ < a₂ ∨ b₁ ≤ b₂ := by contrapose! exact fun h => mul_lt_mul_of_le_of_lt h.1 h.2 @[to_additive] theorem le_or_le_of_mul_le_mul [CovariantClass α α (· * ·) (· < ·)] [CovariantClass α α (Function.swap (· * ·)) (· < ·)] {a₁ a₂ b₁ b₂ : α} : a₁ * b₁ ≤ a₂ * b₂ → a₁ ≤ a₂ ∨ b₁ ≤ b₂ := by contrapose! exact fun h => mul_lt_mul_of_lt_of_lt h.1 h.2 @[to_additive] theorem mul_lt_mul_iff_of_le_of_le [CovariantClass α α (· * ·) (· ≤ ·)] [CovariantClass α α (Function.swap (· * ·)) (· ≤ ·)] [CovariantClass α α (· * ·) (· < ·)] [CovariantClass α α (Function.swap (· * ·)) (· < ·)] {a₁ a₂ b₁ b₂ : α} (ha : a₁ ≤ a₂) (hb : b₁ ≤ b₂) : a₁ * b₁ < a₂ * b₂ ↔ a₁ < a₂ ∨ b₁ < b₂ := by refine ⟨lt_or_lt_of_mul_lt_mul, fun h => ?_⟩ cases' h with ha' hb' · exact mul_lt_mul_of_lt_of_le ha' hb · exact mul_lt_mul_of_le_of_lt ha hb' end Mul variable [MulOneClass α] @[to_additive] theorem min_le_mul_of_one_le_right [CovariantClass α α (· * ·) (· ≤ ·)] {a b : α} (hb : 1 ≤ b) : min a b ≤ a * b := min_le_iff.2 <| Or.inl <| le_mul_of_one_le_right' hb @[to_additive] theorem min_le_mul_of_one_le_left [CovariantClass α α (Function.swap (· * ·)) (· ≤ ·)] {a b : α} (ha : 1 ≤ a) : min a b ≤ a * b := min_le_iff.2 <| Or.inr <| le_mul_of_one_le_left' ha @[to_additive] theorem max_le_mul_of_one_le [CovariantClass α α (· * ·) (· ≤ ·)] [CovariantClass α α (Function.swap (· * ·)) (· ≤ ·)] {a b : α} (ha : 1 ≤ a) (hb : 1 ≤ b) : max a b ≤ a * b := max_le_iff.2 ⟨le_mul_of_one_le_right' hb, le_mul_of_one_le_left' ha⟩ end CovariantClassMulLe
Algebra\Order\Monoid\Unbundled\OrderDual.lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro, Johannes Hölzl -/ import Mathlib.Algebra.Order.Group.Synonym import Mathlib.Algebra.Order.Monoid.Unbundled.Defs /-! # Unbundled ordered monoid structures on the order dual. -/ universe u variable {α : Type u} open Function namespace OrderDual @[to_additive] instance contravariantClass_mul_le [LE α] [Mul α] [c : ContravariantClass α α (· * ·) (· ≤ ·)] : ContravariantClass αᵒᵈ αᵒᵈ (· * ·) (· ≤ ·) := ⟨c.1.flip⟩ @[to_additive] instance covariantClass_mul_le [LE α] [Mul α] [c : CovariantClass α α (· * ·) (· ≤ ·)] : CovariantClass αᵒᵈ αᵒᵈ (· * ·) (· ≤ ·) := ⟨c.1.flip⟩ @[to_additive] instance contravariantClass_swap_mul_le [LE α] [Mul α] [c : ContravariantClass α α (swap (· * ·)) (· ≤ ·)] : ContravariantClass αᵒᵈ αᵒᵈ (swap (· * ·)) (· ≤ ·) := ⟨c.1.flip⟩ @[to_additive] instance covariantClass_swap_mul_le [LE α] [Mul α] [c : CovariantClass α α (swap (· * ·)) (· ≤ ·)] : CovariantClass αᵒᵈ αᵒᵈ (swap (· * ·)) (· ≤ ·) := ⟨c.1.flip⟩ @[to_additive] instance contravariantClass_mul_lt [LT α] [Mul α] [c : ContravariantClass α α (· * ·) (· < ·)] : ContravariantClass αᵒᵈ αᵒᵈ (· * ·) (· < ·) := ⟨c.1.flip⟩ @[to_additive] instance covariantClass_mul_lt [LT α] [Mul α] [c : CovariantClass α α (· * ·) (· < ·)] : CovariantClass αᵒᵈ αᵒᵈ (· * ·) (· < ·) := ⟨c.1.flip⟩ @[to_additive] instance contravariantClass_swap_mul_lt [LT α] [Mul α] [c : ContravariantClass α α (swap (· * ·)) (· < ·)] : ContravariantClass αᵒᵈ αᵒᵈ (swap (· * ·)) (· < ·) := ⟨c.1.flip⟩ @[to_additive] instance covariantClass_swap_mul_lt [LT α] [Mul α] [c : CovariantClass α α (swap (· * ·)) (· < ·)] : CovariantClass αᵒᵈ αᵒᵈ (swap (· * ·)) (· < ·) := ⟨c.1.flip⟩
Algebra\Order\Monoid\Unbundled\Pow.lean
/- Copyright (c) 2015 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Robert Y. Lewis, Yury G. Kudryashov -/ import Mathlib.Algebra.Order.Monoid.Unbundled.Basic import Mathlib.Algebra.Order.Monoid.Unbundled.OrderDual import Mathlib.Tactic.Lift import Mathlib.Tactic.Monotonicity.Attr /-! # Lemmas about the interaction of power operations with order in terms of `CovariantClass` -/ open Function variable {β G M : Type*} section Monoid variable [Monoid M] section Preorder variable [Preorder M] section Left variable [CovariantClass M M (· * ·) (· ≤ ·)] {x : M} @[to_additive (attr := mono, gcongr) nsmul_le_nsmul_right] theorem pow_le_pow_left' [CovariantClass M M (swap (· * ·)) (· ≤ ·)] {a b : M} (hab : a ≤ b) : ∀ i : ℕ, a ^ i ≤ b ^ i | 0 => by simp | k + 1 => by rw [pow_succ, pow_succ] exact mul_le_mul' (pow_le_pow_left' hab k) hab @[to_additive nsmul_nonneg] theorem one_le_pow_of_one_le' {a : M} (H : 1 ≤ a) : ∀ n : ℕ, 1 ≤ a ^ n | 0 => by simp | k + 1 => by rw [pow_succ] exact one_le_mul (one_le_pow_of_one_le' H k) H @[to_additive nsmul_nonpos] theorem pow_le_one' {a : M} (H : a ≤ 1) (n : ℕ) : a ^ n ≤ 1 := one_le_pow_of_one_le' (M := Mᵒᵈ) H n @[to_additive (attr := gcongr) nsmul_le_nsmul_left] theorem pow_le_pow_right' {a : M} {n m : ℕ} (ha : 1 ≤ a) (h : n ≤ m) : a ^ n ≤ a ^ m := let ⟨k, hk⟩ := Nat.le.dest h calc a ^ n ≤ a ^ n * a ^ k := le_mul_of_one_le_right' (one_le_pow_of_one_le' ha _) _ = a ^ m := by rw [← hk, pow_add] @[to_additive nsmul_le_nsmul_left_of_nonpos] theorem pow_le_pow_right_of_le_one' {a : M} {n m : ℕ} (ha : a ≤ 1) (h : n ≤ m) : a ^ m ≤ a ^ n := pow_le_pow_right' (M := Mᵒᵈ) ha h @[to_additive nsmul_pos] theorem one_lt_pow' {a : M} (ha : 1 < a) {k : ℕ} (hk : k ≠ 0) : 1 < a ^ k := by rcases Nat.exists_eq_succ_of_ne_zero hk with ⟨l, rfl⟩ clear hk induction' l with l IH · rw [pow_succ]; simpa using ha · rw [pow_succ] exact one_lt_mul'' IH ha @[to_additive nsmul_neg] theorem pow_lt_one' {a : M} (ha : a < 1) {k : ℕ} (hk : k ≠ 0) : a ^ k < 1 := one_lt_pow' (M := Mᵒᵈ) ha hk @[to_additive (attr := gcongr) nsmul_lt_nsmul_left] theorem pow_lt_pow_right' [CovariantClass M M (· * ·) (· < ·)] {a : M} {n m : ℕ} (ha : 1 < a) (h : n < m) : a ^ n < a ^ m := by rcases Nat.le.dest h with ⟨k, rfl⟩; clear h rw [pow_add, pow_succ, mul_assoc, ← pow_succ'] exact lt_mul_of_one_lt_right' _ (one_lt_pow' ha k.succ_ne_zero) @[to_additive nsmul_left_strictMono] theorem pow_right_strictMono' [CovariantClass M M (· * ·) (· < ·)] {a : M} (ha : 1 < a) : StrictMono ((a ^ ·) : ℕ → M) := fun _ _ => pow_lt_pow_right' ha @[to_additive Left.pow_nonneg] theorem Left.one_le_pow_of_le (hx : 1 ≤ x) : ∀ {n : ℕ}, 1 ≤ x ^ n | 0 => (pow_zero x).ge | n + 1 => by rw [pow_succ] exact Left.one_le_mul (Left.one_le_pow_of_le hx) hx @[to_additive Left.pow_nonpos] theorem Left.pow_le_one_of_le (hx : x ≤ 1) : ∀ {n : ℕ}, x ^ n ≤ 1 | 0 => (pow_zero _).le | n + 1 => by rw [pow_succ] exact Left.mul_le_one (Left.pow_le_one_of_le hx) hx end Left section Right variable [CovariantClass M M (swap (· * ·)) (· ≤ ·)] {x : M} @[to_additive Right.pow_nonneg] theorem Right.one_le_pow_of_le (hx : 1 ≤ x) : ∀ {n : ℕ}, 1 ≤ x ^ n | 0 => (pow_zero _).ge | n + 1 => by rw [pow_succ] exact Right.one_le_mul (Right.one_le_pow_of_le hx) hx @[to_additive Right.pow_nonpos] theorem Right.pow_le_one_of_le (hx : x ≤ 1) : ∀ {n : ℕ}, x ^ n ≤ 1 | 0 => (pow_zero _).le | n + 1 => by rw [pow_succ] exact Right.mul_le_one (Right.pow_le_one_of_le hx) hx end Right section CovariantLTSwap variable [Preorder β] [CovariantClass M M (· * ·) (· < ·)] [CovariantClass M M (swap (· * ·)) (· < ·)] {f : β → M} {n : ℕ} @[to_additive StrictMono.const_nsmul] theorem StrictMono.pow_const (hf : StrictMono f) : ∀ {n : ℕ}, n ≠ 0 → StrictMono (f · ^ n) | 0, hn => (hn rfl).elim | 1, _ => by simpa | Nat.succ <| Nat.succ n, _ => by simpa only [pow_succ] using (hf.pow_const n.succ_ne_zero).mul' hf /-- See also `pow_left_strictMonoOn`. -/ @[to_additive nsmul_right_strictMono] -- Porting note: nolint to_additive_doc theorem pow_left_strictMono (hn : n ≠ 0) : StrictMono (· ^ n : M → M) := strictMono_id.pow_const hn @[to_additive (attr := mono, gcongr) nsmul_lt_nsmul_right] lemma pow_lt_pow_left' (hn : n ≠ 0) {a b : M} (hab : a < b) : a ^ n < b ^ n := pow_left_strictMono hn hab end CovariantLTSwap section CovariantLESwap variable [Preorder β] [CovariantClass M M (· * ·) (· ≤ ·)] [CovariantClass M M (swap (· * ·)) (· ≤ ·)] @[to_additive Monotone.const_nsmul] theorem Monotone.pow_const {f : β → M} (hf : Monotone f) : ∀ n : ℕ, Monotone fun a => f a ^ n | 0 => by simpa using monotone_const | n + 1 => by simp_rw [pow_succ] exact (Monotone.pow_const hf _).mul' hf @[to_additive nsmul_right_mono] theorem pow_left_mono (n : ℕ) : Monotone fun a : M => a ^ n := monotone_id.pow_const _ end CovariantLESwap @[to_additive Left.pow_neg] theorem Left.pow_lt_one_of_lt [CovariantClass M M (· * ·) (· < ·)] {n : ℕ} {x : M} (hn : 0 < n) (h : x < 1) : x ^ n < 1 := Nat.le_induction ((pow_one _).trans_lt h) (fun n _ ih => by rw [pow_succ] exact mul_lt_one ih h) _ (Nat.succ_le_iff.2 hn) @[to_additive Right.pow_neg] theorem Right.pow_lt_one_of_lt [CovariantClass M M (swap (· * ·)) (· < ·)] {n : ℕ} {x : M} (hn : 0 < n) (h : x < 1) : x ^ n < 1 := Nat.le_induction ((pow_one _).trans_lt h) (fun n _ ih => by rw [pow_succ] exact Right.mul_lt_one ih h) _ (Nat.succ_le_iff.2 hn) end Preorder section LinearOrder variable [LinearOrder M] section CovariantLE variable [CovariantClass M M (· * ·) (· ≤ ·)] -- This generalises to lattices. See `pow_two_semiclosed` @[to_additive nsmul_nonneg_iff] theorem one_le_pow_iff {x : M} {n : ℕ} (hn : n ≠ 0) : 1 ≤ x ^ n ↔ 1 ≤ x := ⟨le_imp_le_of_lt_imp_lt fun h => pow_lt_one' h hn, fun h => one_le_pow_of_one_le' h n⟩ @[to_additive] theorem pow_le_one_iff {x : M} {n : ℕ} (hn : n ≠ 0) : x ^ n ≤ 1 ↔ x ≤ 1 := one_le_pow_iff (M := Mᵒᵈ) hn @[to_additive nsmul_pos_iff] theorem one_lt_pow_iff {x : M} {n : ℕ} (hn : n ≠ 0) : 1 < x ^ n ↔ 1 < x := lt_iff_lt_of_le_iff_le (pow_le_one_iff hn) @[to_additive] theorem pow_lt_one_iff {x : M} {n : ℕ} (hn : n ≠ 0) : x ^ n < 1 ↔ x < 1 := lt_iff_lt_of_le_iff_le (one_le_pow_iff hn) @[to_additive] theorem pow_eq_one_iff {x : M} {n : ℕ} (hn : n ≠ 0) : x ^ n = 1 ↔ x = 1 := by simp only [le_antisymm_iff] rw [pow_le_one_iff hn, one_le_pow_iff hn] variable [CovariantClass M M (· * ·) (· < ·)] {a : M} {m n : ℕ} @[to_additive nsmul_le_nsmul_iff_left] theorem pow_le_pow_iff_right' (ha : 1 < a) : a ^ m ≤ a ^ n ↔ m ≤ n := (pow_right_strictMono' ha).le_iff_le @[to_additive nsmul_lt_nsmul_iff_left] theorem pow_lt_pow_iff_right' (ha : 1 < a) : a ^ m < a ^ n ↔ m < n := (pow_right_strictMono' ha).lt_iff_lt end CovariantLE section CovariantLESwap variable [CovariantClass M M (· * ·) (· ≤ ·)] [CovariantClass M M (swap (· * ·)) (· ≤ ·)] @[to_additive lt_of_nsmul_lt_nsmul_right] theorem lt_of_pow_lt_pow_left' {a b : M} (n : ℕ) : a ^ n < b ^ n → a < b := (pow_left_mono _).reflect_lt @[to_additive min_lt_of_add_lt_two_nsmul] theorem min_lt_of_mul_lt_sq {a b c : M} (h : a * b < c ^ 2) : min a b < c := by simpa using min_lt_max_of_mul_lt_mul (h.trans_eq <| pow_two _) @[to_additive lt_max_of_two_nsmul_lt_add] theorem lt_max_of_sq_lt_mul {a b c : M} (h : a ^ 2 < b * c) : a < max b c := by simpa using min_lt_max_of_mul_lt_mul ((pow_two _).symm.trans_lt h) end CovariantLESwap section CovariantLTSwap variable [CovariantClass M M (· * ·) (· < ·)] [CovariantClass M M (swap (· * ·)) (· < ·)] @[to_additive le_of_nsmul_le_nsmul_right] theorem le_of_pow_le_pow_left' {a b : M} {n : ℕ} (hn : n ≠ 0) : a ^ n ≤ b ^ n → a ≤ b := (pow_left_strictMono hn).le_iff_le.1 @[to_additive min_le_of_add_le_two_nsmul] theorem min_le_of_mul_le_sq {a b c : M} (h : a * b ≤ c ^ 2) : min a b ≤ c := by simpa using min_le_max_of_mul_le_mul (h.trans_eq <| pow_two _) @[to_additive le_max_of_two_nsmul_le_add] theorem le_max_of_sq_le_mul {a b c : M} (h : a ^ 2 ≤ b * c) : a ≤ max b c := by simpa using min_le_max_of_mul_le_mul ((pow_two _).symm.trans_le h) end CovariantLTSwap @[to_additive Left.nsmul_neg_iff] theorem Left.pow_lt_one_iff' [CovariantClass M M (· * ·) (· < ·)] {n : ℕ} {x : M} (hn : 0 < n) : x ^ n < 1 ↔ x < 1 := haveI := covariantClass_le_of_lt M M (· * ·) pow_lt_one_iff hn.ne' theorem Left.pow_lt_one_iff [CovariantClass M M (· * ·) (· < ·)] {n : ℕ} {x : M} (hn : 0 < n) : x ^ n < 1 ↔ x < 1 := Left.pow_lt_one_iff' hn @[to_additive] theorem Right.pow_lt_one_iff [CovariantClass M M (swap (· * ·)) (· < ·)] {n : ℕ} {x : M} (hn : 0 < n) : x ^ n < 1 ↔ x < 1 := ⟨fun H => not_le.mp fun k => haveI := covariantClass_le_of_lt M M (swap (· * ·)) H.not_le <| Right.one_le_pow_of_le k, Right.pow_lt_one_of_lt hn⟩ end LinearOrder end Monoid section DivInvMonoid variable [DivInvMonoid G] [Preorder G] [CovariantClass G G (· * ·) (· ≤ ·)] @[to_additive zsmul_nonneg] theorem one_le_zpow {x : G} (H : 1 ≤ x) {n : ℤ} (hn : 0 ≤ n) : 1 ≤ x ^ n := by lift n to ℕ using hn rw [zpow_natCast] apply one_le_pow_of_one_le' H end DivInvMonoid /-! ### Deprecated lemmas Those lemmas have been deprecated on 2023-12-23. -/ @[deprecated (since := "2023-12-23")] alias pow_le_pow_of_le_left' := pow_le_pow_left' @[deprecated (since := "2023-12-23")] alias nsmul_le_nsmul_of_le_right := nsmul_le_nsmul_right @[deprecated (since := "2023-12-23")] alias pow_lt_pow' := pow_lt_pow_right' @[deprecated (since := "2023-12-23")] alias nsmul_lt_nsmul := nsmul_lt_nsmul_left @[deprecated (since := "2023-12-23")] alias pow_strictMono_left := pow_right_strictMono' @[deprecated (since := "2023-12-23")] alias nsmul_strictMono_right := nsmul_left_strictMono @[deprecated (since := "2023-12-23")] alias StrictMono.pow_right' := StrictMono.pow_const @[deprecated (since := "2023-12-23")] alias StrictMono.nsmul_left := StrictMono.const_nsmul @[deprecated (since := "2023-12-23")] alias pow_strictMono_right' := pow_left_strictMono @[deprecated (since := "2023-12-23")] alias nsmul_strictMono_left := nsmul_right_strictMono @[deprecated (since := "2023-12-23")] alias Monotone.pow_right := Monotone.pow_const @[deprecated (since := "2023-12-23")] alias Monotone.nsmul_left := Monotone.const_nsmul @[deprecated (since := "2023-12-23")] alias lt_of_pow_lt_pow' := lt_of_pow_lt_pow_left' @[deprecated (since := "2023-12-23")] alias lt_of_nsmul_lt_nsmul := lt_of_nsmul_lt_nsmul_right @[deprecated (since := "2023-12-23")] alias pow_le_pow' := pow_le_pow_right' @[deprecated (since := "2023-12-23")] alias nsmul_le_nsmul := nsmul_le_nsmul_left @[deprecated (since := "2023-12-23")] alias pow_le_pow_of_le_one' := pow_le_pow_right_of_le_one' @[deprecated (since := "2023-12-23")] alias nsmul_le_nsmul_of_nonpos := nsmul_le_nsmul_left_of_nonpos @[deprecated (since := "2023-12-23")] alias le_of_pow_le_pow' := le_of_pow_le_pow_left' @[deprecated (since := "2023-12-23")] alias le_of_nsmul_le_nsmul := le_of_nsmul_le_nsmul_right @[deprecated (since := "2023-12-23")] alias pow_le_pow_iff' := pow_le_pow_iff_right' @[deprecated (since := "2023-12-23")] alias nsmul_le_nsmul_iff := nsmul_le_nsmul_iff_left @[deprecated (since := "2023-12-23")] alias pow_lt_pow_iff' := pow_lt_pow_iff_right' @[deprecated (since := "2023-12-23")] alias nsmul_lt_nsmul_iff := nsmul_lt_nsmul_iff_left @[deprecated (since := "2023-12-23")] alias pow_mono_right := pow_left_mono @[deprecated (since := "2023-12-23")] alias nsmul_mono_left := nsmul_right_mono
Algebra\Order\Monoid\Unbundled\WithTop.lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro, Johannes Hölzl -/ import Mathlib.Algebra.CharZero.Defs import Mathlib.Algebra.Group.Hom.Defs import Mathlib.Algebra.Order.ZeroLEOne import Mathlib.Data.Nat.Cast.Defs import Mathlib.Order.WithBot import Mathlib.Algebra.Order.Monoid.Unbundled.Basic import Mathlib.Algebra.Order.Monoid.Unbundled.ExistsOfLE import Mathlib.Algebra.Order.Monoid.Unbundled.OrderDual /-! # Adjoining top/bottom elements to ordered monoids. -/ universe u v variable {α : Type u} {β : Type v} open Function namespace WithTop section One variable [One α] {a : α} @[to_additive] instance one : One (WithTop α) := ⟨(1 : α)⟩ @[to_additive (attr := simp, norm_cast)] theorem coe_one : ((1 : α) : WithTop α) = 1 := rfl @[to_additive (attr := simp, norm_cast)] lemma coe_eq_one : (a : WithTop α) = 1 ↔ a = 1 := coe_eq_coe @[to_additive (attr := simp, norm_cast)] lemma one_eq_coe : 1 = (a : WithTop α) ↔ a = 1 := eq_comm.trans coe_eq_one @[to_additive (attr := simp)] lemma top_ne_one : (⊤ : WithTop α) ≠ 1 := top_ne_coe @[to_additive (attr := simp)] lemma one_ne_top : (1 : WithTop α) ≠ ⊤ := coe_ne_top @[to_additive (attr := simp)] theorem untop_one : (1 : WithTop α).untop coe_ne_top = 1 := rfl @[to_additive (attr := simp)] theorem untop_one' (d : α) : (1 : WithTop α).untop' d = 1 := rfl @[to_additive (attr := simp, norm_cast) coe_nonneg] theorem one_le_coe [LE α] {a : α} : 1 ≤ (a : WithTop α) ↔ 1 ≤ a := coe_le_coe @[to_additive (attr := simp, norm_cast) coe_le_zero] theorem coe_le_one [LE α] {a : α} : (a : WithTop α) ≤ 1 ↔ a ≤ 1 := coe_le_coe @[to_additive (attr := simp, norm_cast) coe_pos] theorem one_lt_coe [LT α] {a : α} : 1 < (a : WithTop α) ↔ 1 < a := coe_lt_coe @[to_additive (attr := simp, norm_cast) coe_lt_zero] theorem coe_lt_one [LT α] {a : α} : (a : WithTop α) < 1 ↔ a < 1 := coe_lt_coe @[to_additive (attr := simp)] protected theorem map_one {β} (f : α → β) : (1 : WithTop α).map f = (f 1 : WithTop β) := rfl instance zeroLEOneClass [Zero α] [LE α] [ZeroLEOneClass α] : ZeroLEOneClass (WithTop α) := ⟨coe_le_coe.2 zero_le_one⟩ end One section Add variable [Add α] {a b c d : WithTop α} {x y : α} instance add : Add (WithTop α) := ⟨Option.map₂ (· + ·)⟩ @[simp, norm_cast] lemma coe_add (a b : α) : ↑(a + b) = (a + b : WithTop α) := rfl @[simp] theorem top_add (a : WithTop α) : ⊤ + a = ⊤ := rfl @[simp] theorem add_top (a : WithTop α) : a + ⊤ = ⊤ := by cases a <;> rfl @[simp] theorem add_eq_top : a + b = ⊤ ↔ a = ⊤ ∨ b = ⊤ := by match a, b with | ⊤, _ => simp | _, ⊤ => simp | (a : α), (b : α) => simp only [← coe_add, coe_ne_top, or_false] theorem add_ne_top : a + b ≠ ⊤ ↔ a ≠ ⊤ ∧ b ≠ ⊤ := add_eq_top.not.trans not_or theorem add_lt_top [LT α] {a b : WithTop α} : a + b < ⊤ ↔ a < ⊤ ∧ b < ⊤ := by simp_rw [WithTop.lt_top_iff_ne_top, add_ne_top] theorem add_eq_coe : ∀ {a b : WithTop α} {c : α}, a + b = c ↔ ∃ a' b' : α, ↑a' = a ∧ ↑b' = b ∧ a' + b' = c | ⊤, b, c => by simp | some a, ⊤, c => by simp | some a, some b, c => by norm_cast; simp -- Porting note (#10618): simp can already prove this. -- @[simp] theorem add_coe_eq_top_iff {x : WithTop α} {y : α} : x + y = ⊤ ↔ x = ⊤ := by simp -- Porting note (#10618): simp can already prove this. -- @[simp] theorem coe_add_eq_top_iff {y : WithTop α} : ↑x + y = ⊤ ↔ y = ⊤ := by simp theorem add_right_cancel_iff [IsRightCancelAdd α] (ha : a ≠ ⊤) : b + a = c + a ↔ b = c := by lift a to α using ha obtain rfl | hb := eq_or_ne b ⊤ · rw [top_add, eq_comm, WithTop.add_coe_eq_top_iff, eq_comm] lift b to α using hb simp_rw [← WithTop.coe_add, eq_comm, WithTop.add_eq_coe, coe_eq_coe, exists_and_left, exists_eq_left, add_left_inj, exists_eq_right, eq_comm] theorem add_right_cancel [IsRightCancelAdd α] (ha : a ≠ ⊤) (h : b + a = c + a) : b = c := (WithTop.add_right_cancel_iff ha).1 h theorem add_left_cancel_iff [IsLeftCancelAdd α] (ha : a ≠ ⊤) : a + b = a + c ↔ b = c := by lift a to α using ha obtain rfl | hb := eq_or_ne b ⊤ · rw [add_top, eq_comm, WithTop.coe_add_eq_top_iff, eq_comm] lift b to α using hb simp_rw [← WithTop.coe_add, eq_comm, WithTop.add_eq_coe, eq_comm, coe_eq_coe, exists_and_left, exists_eq_left', add_right_inj, exists_eq_right'] theorem add_left_cancel [IsLeftCancelAdd α] (ha : a ≠ ⊤) (h : a + b = a + c) : b = c := (WithTop.add_left_cancel_iff ha).1 h instance covariantClass_add_le [LE α] [CovariantClass α α (· + ·) (· ≤ ·)] : CovariantClass (WithTop α) (WithTop α) (· + ·) (· ≤ ·) := ⟨fun a b c h => by cases a <;> cases c <;> try exact le_top rcases le_coe_iff.1 h with ⟨b, rfl, _⟩ exact coe_le_coe.2 (add_le_add_left (coe_le_coe.1 h) _)⟩ instance covariantClass_swap_add_le [LE α] [CovariantClass α α (swap (· + ·)) (· ≤ ·)] : CovariantClass (WithTop α) (WithTop α) (swap (· + ·)) (· ≤ ·) := ⟨fun a b c h => by cases a <;> cases c <;> try exact le_top rcases le_coe_iff.1 h with ⟨b, rfl, _⟩ exact coe_le_coe.2 (add_le_add_right (coe_le_coe.1 h) _)⟩ instance contravariantClass_add_lt [LT α] [ContravariantClass α α (· + ·) (· < ·)] : ContravariantClass (WithTop α) (WithTop α) (· + ·) (· < ·) := ⟨fun a b c h => by induction a; · exact (WithTop.not_top_lt _ h).elim induction b; · exact (WithTop.not_top_lt _ h).elim induction c · exact coe_lt_top _ · exact coe_lt_coe.2 (lt_of_add_lt_add_left <| coe_lt_coe.1 h)⟩ instance contravariantClass_swap_add_lt [LT α] [ContravariantClass α α (swap (· + ·)) (· < ·)] : ContravariantClass (WithTop α) (WithTop α) (swap (· + ·)) (· < ·) := ⟨fun a b c h => by cases a <;> cases b <;> try exact (WithTop.not_top_lt _ h).elim cases c · exact coe_lt_top _ · exact coe_lt_coe.2 (lt_of_add_lt_add_right <| coe_lt_coe.1 h)⟩ protected theorem le_of_add_le_add_left [LE α] [ContravariantClass α α (· + ·) (· ≤ ·)] (ha : a ≠ ⊤) (h : a + b ≤ a + c) : b ≤ c := by lift a to α using ha induction c · exact le_top · induction b · exact (not_top_le_coe _ h).elim · simp only [← coe_add, coe_le_coe] at h ⊢ exact le_of_add_le_add_left h protected theorem le_of_add_le_add_right [LE α] [ContravariantClass α α (swap (· + ·)) (· ≤ ·)] (ha : a ≠ ⊤) (h : b + a ≤ c + a) : b ≤ c := by lift a to α using ha cases c · exact le_top · cases b · exact (not_top_le_coe _ h).elim · exact coe_le_coe.2 (le_of_add_le_add_right <| coe_le_coe.1 h) protected theorem add_lt_add_left [LT α] [CovariantClass α α (· + ·) (· < ·)] (ha : a ≠ ⊤) (h : b < c) : a + b < a + c := by lift a to α using ha rcases lt_iff_exists_coe.1 h with ⟨b, rfl, h'⟩ cases c · exact coe_lt_top _ · exact coe_lt_coe.2 (add_lt_add_left (coe_lt_coe.1 h) _) protected theorem add_lt_add_right [LT α] [CovariantClass α α (swap (· + ·)) (· < ·)] (ha : a ≠ ⊤) (h : b < c) : b + a < c + a := by lift a to α using ha rcases lt_iff_exists_coe.1 h with ⟨b, rfl, h'⟩ cases c · exact coe_lt_top _ · exact coe_lt_coe.2 (add_lt_add_right (coe_lt_coe.1 h) _) protected theorem add_le_add_iff_left [LE α] [CovariantClass α α (· + ·) (· ≤ ·)] [ContravariantClass α α (· + ·) (· ≤ ·)] (ha : a ≠ ⊤) : a + b ≤ a + c ↔ b ≤ c := ⟨WithTop.le_of_add_le_add_left ha, fun h => add_le_add_left h a⟩ protected theorem add_le_add_iff_right [LE α] [CovariantClass α α (swap (· + ·)) (· ≤ ·)] [ContravariantClass α α (swap (· + ·)) (· ≤ ·)] (ha : a ≠ ⊤) : b + a ≤ c + a ↔ b ≤ c := ⟨WithTop.le_of_add_le_add_right ha, fun h => add_le_add_right h a⟩ protected theorem add_lt_add_iff_left [LT α] [CovariantClass α α (· + ·) (· < ·)] [ContravariantClass α α (· + ·) (· < ·)] (ha : a ≠ ⊤) : a + b < a + c ↔ b < c := ⟨lt_of_add_lt_add_left, WithTop.add_lt_add_left ha⟩ protected theorem add_lt_add_iff_right [LT α] [CovariantClass α α (swap (· + ·)) (· < ·)] [ContravariantClass α α (swap (· + ·)) (· < ·)] (ha : a ≠ ⊤) : b + a < c + a ↔ b < c := ⟨lt_of_add_lt_add_right, WithTop.add_lt_add_right ha⟩ protected theorem add_lt_add_of_le_of_lt [Preorder α] [CovariantClass α α (· + ·) (· < ·)] [CovariantClass α α (swap (· + ·)) (· ≤ ·)] (ha : a ≠ ⊤) (hab : a ≤ b) (hcd : c < d) : a + c < b + d := (WithTop.add_lt_add_left ha hcd).trans_le <| add_le_add_right hab _ protected theorem add_lt_add_of_lt_of_le [Preorder α] [CovariantClass α α (· + ·) (· ≤ ·)] [CovariantClass α α (swap (· + ·)) (· < ·)] (hc : c ≠ ⊤) (hab : a < b) (hcd : c ≤ d) : a + c < b + d := (WithTop.add_lt_add_right hc hab).trans_le <| add_le_add_left hcd _ -- There is no `WithTop.map_mul_of_mulHom`, since `WithTop` does not have a multiplication. @[simp] protected theorem map_add {F} [Add β] [FunLike F α β] [AddHomClass F α β] (f : F) (a b : WithTop α) : (a + b).map f = a.map f + b.map f := by induction a · exact (top_add _).symm · induction b · exact (add_top _).symm · rw [map_coe, map_coe, ← coe_add, ← coe_add, ← map_add] rfl end Add instance addSemigroup [AddSemigroup α] : AddSemigroup (WithTop α) := { WithTop.add with add_assoc := fun _ _ _ => Option.map₂_assoc add_assoc } instance addCommSemigroup [AddCommSemigroup α] : AddCommSemigroup (WithTop α) := { WithTop.addSemigroup with add_comm := fun _ _ => Option.map₂_comm add_comm } instance addZeroClass [AddZeroClass α] : AddZeroClass (WithTop α) := { WithTop.zero, WithTop.add with zero_add := Option.map₂_left_identity zero_add add_zero := Option.map₂_right_identity add_zero } section AddMonoid variable [AddMonoid α] instance addMonoid : AddMonoid (WithTop α) where __ := WithTop.addSemigroup __ := WithTop.addZeroClass nsmul n a := match a, n with | (a : α), n => ↑(n • a) | ⊤, 0 => 0 | ⊤, _n + 1 => ⊤ nsmul_zero a := by cases a <;> simp [zero_nsmul] nsmul_succ n a := by cases a <;> cases n <;> simp [succ_nsmul, coe_add] @[simp, norm_cast] lemma coe_nsmul (a : α) (n : ℕ) : ↑(n • a) = n • (a : WithTop α) := rfl /-- Coercion from `α` to `WithTop α` as an `AddMonoidHom`. -/ def addHom : α →+ WithTop α where toFun := WithTop.some map_zero' := rfl map_add' _ _ := rfl @[simp, norm_cast] lemma coe_addHom : ⇑(addHom : α →+ WithTop α) = WithTop.some := rfl end AddMonoid instance addCommMonoid [AddCommMonoid α] : AddCommMonoid (WithTop α) := { WithTop.addMonoid, WithTop.addCommSemigroup with } section AddMonoidWithOne variable [AddMonoidWithOne α] instance addMonoidWithOne : AddMonoidWithOne (WithTop α) := { WithTop.one, WithTop.addMonoid with natCast := fun n => ↑(n : α), natCast_zero := by simp only -- Porting note: Had to add this...? rw [Nat.cast_zero, WithTop.coe_zero], natCast_succ := fun n => by simp only -- Porting note: Had to add this...? rw [Nat.cast_add_one, WithTop.coe_add, WithTop.coe_one] } @[simp, norm_cast] lemma coe_natCast (n : ℕ) : ((n : α) : WithTop α) = n := rfl @[simp] lemma natCast_ne_top (n : ℕ) : (n : WithTop α) ≠ ⊤ := coe_ne_top @[simp] lemma top_ne_natCast (n : ℕ) : (⊤ : WithTop α) ≠ n := top_ne_coe @[deprecated (since := "2024-04-05")] alias coe_nat := coe_natCast @[deprecated (since := "2024-04-05")] alias nat_ne_top := natCast_ne_top @[deprecated (since := "2024-04-05")] alias top_ne_nat := top_ne_natCast -- See note [no_index around OfNat.ofNat] @[simp] lemma coe_ofNat (n : ℕ) [n.AtLeastTwo] : (no_index (OfNat.ofNat n : α) : WithTop α) = OfNat.ofNat n := rfl @[simp] lemma coe_eq_ofNat (n : ℕ) [n.AtLeastTwo] (m : α) : (m : WithTop α) = no_index (OfNat.ofNat n) ↔ m = OfNat.ofNat n := coe_eq_coe @[simp] lemma ofNat_eq_coe (n : ℕ) [n.AtLeastTwo] (m : α) : no_index (OfNat.ofNat n) = (m : WithTop α) ↔ OfNat.ofNat n = m := coe_eq_coe @[simp] lemma ofNat_ne_top (n : ℕ) [n.AtLeastTwo] : no_index (OfNat.ofNat n : WithTop α) ≠ ⊤ := natCast_ne_top n @[simp] lemma top_ne_ofNat (n : ℕ) [n.AtLeastTwo] : (⊤ : WithTop α) ≠ no_index (OfNat.ofNat n) := top_ne_natCast n end AddMonoidWithOne instance charZero [AddMonoidWithOne α] [CharZero α] : CharZero (WithTop α) := { cast_injective := Function.Injective.comp (f := Nat.cast (R := α)) (fun _ _ => WithTop.coe_eq_coe.1) Nat.cast_injective} instance addCommMonoidWithOne [AddCommMonoidWithOne α] : AddCommMonoidWithOne (WithTop α) := { WithTop.addMonoidWithOne, WithTop.addCommMonoid with } -- instance orderedAddCommMonoid [OrderedAddCommMonoid α] : OrderedAddCommMonoid (WithTop α) where -- add_le_add_left _ _ := add_le_add_left -- -- instance linearOrderedAddCommMonoidWithTop [LinearOrderedAddCommMonoid α] : -- LinearOrderedAddCommMonoidWithTop (WithTop α) := -- { WithTop.orderTop, WithTop.linearOrder, WithTop.orderedAddCommMonoid with -- top_add' := WithTop.top_add } -- instance existsAddOfLE [LE α] [Add α] [ExistsAddOfLE α] : ExistsAddOfLE (WithTop α) := ⟨fun {a} {b} => match a, b with | ⊤, ⊤ => by simp | (a : α), ⊤ => fun _ => ⟨⊤, rfl⟩ | (a : α), (b : α) => fun h => by obtain ⟨c, rfl⟩ := exists_add_of_le (WithTop.coe_le_coe.1 h) exact ⟨c, rfl⟩ | ⊤, (b : α) => fun h => (not_top_le_coe _ h).elim⟩ -- instance canonicallyOrderedAddCommMonoid [CanonicallyOrderedAddCommMonoid α] : -- CanonicallyOrderedAddCommMonoid (WithTop α) := -- { WithTop.orderBot, WithTop.orderedAddCommMonoid, WithTop.existsAddOfLE with -- le_self_add := fun a b => -- match a, b with -- | ⊤, ⊤ => le_rfl -- | (a : α), ⊤ => le_top -- | (a : α), (b : α) => WithTop.coe_le_coe.2 le_self_add -- | ⊤, (b : α) => le_rfl } -- -- instance [CanonicallyLinearOrderedAddCommMonoid α] : -- CanonicallyLinearOrderedAddCommMonoid (WithTop α) := -- { WithTop.canonicallyOrderedAddCommMonoid, WithTop.linearOrder with } @[simp] theorem zero_lt_top [Zero α] [LT α] : (0 : WithTop α) < ⊤ := coe_lt_top 0 -- Porting note (#10618): simp can already prove this. -- @[simp] @[norm_cast] theorem zero_lt_coe [Zero α] [LT α] (a : α) : (0 : WithTop α) < a ↔ 0 < a := coe_lt_coe /-- A version of `WithTop.map` for `OneHom`s. -/ @[to_additive (attr := simps (config := .asFn)) "A version of `WithTop.map` for `ZeroHom`s"] protected def _root_.OneHom.withTopMap {M N : Type*} [One M] [One N] (f : OneHom M N) : OneHom (WithTop M) (WithTop N) where toFun := WithTop.map f map_one' := by rw [WithTop.map_one, map_one, coe_one] /-- A version of `WithTop.map` for `AddHom`s. -/ @[simps (config := .asFn)] protected def _root_.AddHom.withTopMap {M N : Type*} [Add M] [Add N] (f : AddHom M N) : AddHom (WithTop M) (WithTop N) where toFun := WithTop.map f map_add' := WithTop.map_add f /-- A version of `WithTop.map` for `AddMonoidHom`s. -/ @[simps (config := .asFn)] protected def _root_.AddMonoidHom.withTopMap {M N : Type*} [AddZeroClass M] [AddZeroClass N] (f : M →+ N) : WithTop M →+ WithTop N := { ZeroHom.withTopMap f.toZeroHom, AddHom.withTopMap f.toAddHom with toFun := WithTop.map f } end WithTop namespace WithBot section One variable [One α] {a : α} @[to_additive] instance one : One (WithBot α) := WithTop.one @[to_additive (attr := simp, norm_cast)] lemma coe_one : ((1 : α) : WithBot α) = 1 := rfl @[to_additive (attr := simp, norm_cast)] lemma coe_eq_one : (a : WithBot α) = 1 ↔ a = 1 := coe_eq_coe @[to_additive (attr := simp, norm_cast)] lemma one_eq_coe : 1 = (a : WithBot α) ↔ a = 1 := eq_comm.trans coe_eq_one @[to_additive (attr := simp)] lemma bot_ne_one : (⊥ : WithBot α) ≠ 1 := bot_ne_coe @[to_additive (attr := simp)] lemma one_ne_bot : (1 : WithBot α) ≠ ⊥ := coe_ne_bot @[to_additive (attr := simp)] theorem unbot_one : (1 : WithBot α).unbot coe_ne_bot = 1 := rfl @[to_additive (attr := simp)] theorem unbot_one' (d : α) : (1 : WithBot α).unbot' d = 1 := rfl @[to_additive (attr := simp, norm_cast) coe_nonneg] theorem one_le_coe [LE α] : 1 ≤ (a : WithBot α) ↔ 1 ≤ a := coe_le_coe @[to_additive (attr := simp, norm_cast) coe_le_zero] theorem coe_le_one [LE α] : (a : WithBot α) ≤ 1 ↔ a ≤ 1 := coe_le_coe @[to_additive (attr := simp, norm_cast) coe_pos] theorem one_lt_coe [LT α] : 1 < (a : WithBot α) ↔ 1 < a := coe_lt_coe @[to_additive (attr := simp, norm_cast) coe_lt_zero] theorem coe_lt_one [LT α] : (a : WithBot α) < 1 ↔ a < 1 := coe_lt_coe @[to_additive (attr := simp)] protected theorem map_one {β} (f : α → β) : (1 : WithBot α).map f = (f 1 : WithBot β) := rfl instance zeroLEOneClass [Zero α] [LE α] [ZeroLEOneClass α] : ZeroLEOneClass (WithBot α) := ⟨coe_le_coe.2 zero_le_one⟩ end One instance add [Add α] : Add (WithBot α) := WithTop.add instance AddSemigroup [AddSemigroup α] : AddSemigroup (WithBot α) := WithTop.addSemigroup instance addCommSemigroup [AddCommSemigroup α] : AddCommSemigroup (WithBot α) := WithTop.addCommSemigroup instance addZeroClass [AddZeroClass α] : AddZeroClass (WithBot α) := WithTop.addZeroClass section AddMonoid variable [AddMonoid α] instance addMonoid : AddMonoid (WithBot α) := WithTop.addMonoid /-- Coercion from `α` to `WithBot α` as an `AddMonoidHom`. -/ def addHom : α →+ WithBot α where toFun := WithTop.some map_zero' := rfl map_add' _ _ := rfl @[simp, norm_cast] lemma coe_addHom : ⇑(addHom : α →+ WithBot α) = WithBot.some := rfl @[simp, norm_cast] lemma coe_nsmul (a : α) (n : ℕ) : ↑(n • a) = n • (a : WithBot α) := (addHom : α →+ WithBot α).map_nsmul _ _ end AddMonoid instance addCommMonoid [AddCommMonoid α] : AddCommMonoid (WithBot α) := WithTop.addCommMonoid section AddMonoidWithOne variable [AddMonoidWithOne α] instance addMonoidWithOne : AddMonoidWithOne (WithBot α) := WithTop.addMonoidWithOne @[norm_cast] lemma coe_natCast (n : ℕ) : ((n : α) : WithBot α) = n := rfl @[simp] lemma natCast_ne_bot (n : ℕ) : (n : WithBot α) ≠ ⊥ := coe_ne_bot @[simp] lemma bot_ne_natCast (n : ℕ) : (⊥ : WithBot α) ≠ n := bot_ne_coe @[deprecated (since := "2024-04-05")] alias coe_nat := coe_natCast @[deprecated (since := "2024-04-05")] alias nat_ne_bot := natCast_ne_bot @[deprecated (since := "2024-04-05")] alias bot_ne_nat := bot_ne_natCast -- See note [no_index around OfNat.ofNat] @[simp] lemma coe_ofNat (n : ℕ) [n.AtLeastTwo] : (no_index (OfNat.ofNat n : α) : WithBot α) = OfNat.ofNat n := rfl @[simp] lemma coe_eq_ofNat (n : ℕ) [n.AtLeastTwo] (m : α) : (m : WithBot α) = no_index (OfNat.ofNat n) ↔ m = OfNat.ofNat n := coe_eq_coe @[simp] lemma ofNat_eq_coe (n : ℕ) [n.AtLeastTwo] (m : α) : no_index (OfNat.ofNat n) = (m : WithBot α) ↔ OfNat.ofNat n = m := coe_eq_coe @[simp] lemma ofNat_ne_bot (n : ℕ) [n.AtLeastTwo] : no_index (OfNat.ofNat n : WithBot α) ≠ ⊥ := natCast_ne_bot n @[simp] lemma bot_ne_ofNat (n : ℕ) [n.AtLeastTwo] : (⊥ : WithBot α) ≠ no_index (OfNat.ofNat n) := bot_ne_natCast n end AddMonoidWithOne instance charZero [AddMonoidWithOne α] [CharZero α] : CharZero (WithBot α) := WithTop.charZero instance addCommMonoidWithOne [AddCommMonoidWithOne α] : AddCommMonoidWithOne (WithBot α) := WithTop.addCommMonoidWithOne section Add variable [Add α] {a b c d : WithBot α} {x y : α} @[simp, norm_cast] theorem coe_add (a b : α) : ((a + b : α) : WithBot α) = a + b := rfl @[simp] theorem bot_add (a : WithBot α) : ⊥ + a = ⊥ := rfl @[simp] theorem add_bot (a : WithBot α) : a + ⊥ = ⊥ := by cases a <;> rfl @[simp] theorem add_eq_bot : a + b = ⊥ ↔ a = ⊥ ∨ b = ⊥ := WithTop.add_eq_top theorem add_ne_bot : a + b ≠ ⊥ ↔ a ≠ ⊥ ∧ b ≠ ⊥ := WithTop.add_ne_top theorem bot_lt_add [LT α] {a b : WithBot α} : ⊥ < a + b ↔ ⊥ < a ∧ ⊥ < b := WithTop.add_lt_top (α := αᵒᵈ) theorem add_eq_coe : a + b = x ↔ ∃ a' b' : α, ↑a' = a ∧ ↑b' = b ∧ a' + b' = x := WithTop.add_eq_coe -- Porting note (#10618): simp can already prove this. -- @[simp] theorem add_coe_eq_bot_iff : a + y = ⊥ ↔ a = ⊥ := WithTop.add_coe_eq_top_iff -- Porting note (#10618): simp can already prove this. -- @[simp] theorem coe_add_eq_bot_iff : ↑x + b = ⊥ ↔ b = ⊥ := WithTop.coe_add_eq_top_iff theorem add_right_cancel_iff [IsRightCancelAdd α] (ha : a ≠ ⊥) : b + a = c + a ↔ b = c := WithTop.add_right_cancel_iff ha theorem add_right_cancel [IsRightCancelAdd α] (ha : a ≠ ⊥) (h : b + a = c + a) : b = c := WithTop.add_right_cancel ha h theorem add_left_cancel_iff [IsLeftCancelAdd α] (ha : a ≠ ⊥) : a + b = a + c ↔ b = c := WithTop.add_left_cancel_iff ha theorem add_left_cancel [IsLeftCancelAdd α] (ha : a ≠ ⊥) (h : a + b = a + c) : b = c := WithTop.add_left_cancel ha h -- There is no `WithBot.map_mul_of_mulHom`, since `WithBot` does not have a multiplication. @[simp] protected theorem map_add {F} [Add β] [FunLike F α β] [AddHomClass F α β] (f : F) (a b : WithBot α) : (a + b).map f = a.map f + b.map f := WithTop.map_add f a b /-- A version of `WithBot.map` for `OneHom`s. -/ @[to_additive (attr := simps (config := .asFn)) "A version of `WithBot.map` for `ZeroHom`s"] protected def _root_.OneHom.withBotMap {M N : Type*} [One M] [One N] (f : OneHom M N) : OneHom (WithBot M) (WithBot N) where toFun := WithBot.map f map_one' := by rw [WithBot.map_one, map_one, coe_one] /-- A version of `WithBot.map` for `AddHom`s. -/ @[simps (config := .asFn)] protected def _root_.AddHom.withBotMap {M N : Type*} [Add M] [Add N] (f : AddHom M N) : AddHom (WithBot M) (WithBot N) where toFun := WithBot.map f map_add' := WithBot.map_add f /-- A version of `WithBot.map` for `AddMonoidHom`s. -/ @[simps (config := .asFn)] protected def _root_.AddMonoidHom.withBotMap {M N : Type*} [AddZeroClass M] [AddZeroClass N] (f : M →+ N) : WithBot M →+ WithBot N := { ZeroHom.withBotMap f.toZeroHom, AddHom.withBotMap f.toAddHom with toFun := WithBot.map f } variable [Preorder α] instance covariantClass_add_le [CovariantClass α α (· + ·) (· ≤ ·)] : CovariantClass (WithBot α) (WithBot α) (· + ·) (· ≤ ·) := OrderDual.covariantClass_add_le (α := WithTop αᵒᵈ) instance covariantClass_swap_add_le [CovariantClass α α (swap (· + ·)) (· ≤ ·)] : CovariantClass (WithBot α) (WithBot α) (swap (· + ·)) (· ≤ ·) := OrderDual.covariantClass_swap_add_le (α := WithTop αᵒᵈ) instance contravariantClass_add_lt [ContravariantClass α α (· + ·) (· < ·)] : ContravariantClass (WithBot α) (WithBot α) (· + ·) (· < ·) := OrderDual.contravariantClass_add_lt (α := WithTop αᵒᵈ) instance contravariantClass_swap_add_lt [ContravariantClass α α (swap (· + ·)) (· < ·)] : ContravariantClass (WithBot α) (WithBot α) (swap (· + ·)) (· < ·) := OrderDual.contravariantClass_swap_add_lt (α := WithTop αᵒᵈ) protected theorem le_of_add_le_add_left [ContravariantClass α α (· + ·) (· ≤ ·)] (ha : a ≠ ⊥) (h : a + b ≤ a + c) : b ≤ c := WithTop.le_of_add_le_add_left (α := αᵒᵈ) ha h protected theorem le_of_add_le_add_right [ContravariantClass α α (swap (· + ·)) (· ≤ ·)] (ha : a ≠ ⊥) (h : b + a ≤ c + a) : b ≤ c := WithTop.le_of_add_le_add_right (α := αᵒᵈ) ha h protected theorem add_lt_add_left [CovariantClass α α (· + ·) (· < ·)] (ha : a ≠ ⊥) (h : b < c) : a + b < a + c := WithTop.add_lt_add_left (α := αᵒᵈ) ha h protected theorem add_lt_add_right [CovariantClass α α (swap (· + ·)) (· < ·)] (ha : a ≠ ⊥) (h : b < c) : b + a < c + a := WithTop.add_lt_add_right (α := αᵒᵈ) ha h protected theorem add_le_add_iff_left [CovariantClass α α (· + ·) (· ≤ ·)] [ContravariantClass α α (· + ·) (· ≤ ·)] (ha : a ≠ ⊥) : a + b ≤ a + c ↔ b ≤ c := ⟨WithBot.le_of_add_le_add_left ha, fun h => add_le_add_left h a⟩ protected theorem add_le_add_iff_right [CovariantClass α α (swap (· + ·)) (· ≤ ·)] [ContravariantClass α α (swap (· + ·)) (· ≤ ·)] (ha : a ≠ ⊥) : b + a ≤ c + a ↔ b ≤ c := ⟨WithBot.le_of_add_le_add_right ha, fun h => add_le_add_right h a⟩ protected theorem add_lt_add_iff_left [CovariantClass α α (· + ·) (· < ·)] [ContravariantClass α α (· + ·) (· < ·)] (ha : a ≠ ⊥) : a + b < a + c ↔ b < c := ⟨lt_of_add_lt_add_left, WithBot.add_lt_add_left ha⟩ protected theorem add_lt_add_iff_right [CovariantClass α α (swap (· + ·)) (· < ·)] [ContravariantClass α α (swap (· + ·)) (· < ·)] (ha : a ≠ ⊥) : b + a < c + a ↔ b < c := ⟨lt_of_add_lt_add_right, WithBot.add_lt_add_right ha⟩ protected theorem add_lt_add_of_le_of_lt [CovariantClass α α (· + ·) (· < ·)] [CovariantClass α α (swap (· + ·)) (· ≤ ·)] (hb : b ≠ ⊥) (hab : a ≤ b) (hcd : c < d) : a + c < b + d := WithTop.add_lt_add_of_le_of_lt (α := αᵒᵈ) hb hab hcd protected theorem add_lt_add_of_lt_of_le [CovariantClass α α (· + ·) (· ≤ ·)] [CovariantClass α α (swap (· + ·)) (· < ·)] (hd : d ≠ ⊥) (hab : a < b) (hcd : c ≤ d) : a + c < b + d := WithTop.add_lt_add_of_lt_of_le (α := αᵒᵈ) hd hab hcd end Add -- instance orderedAddCommMonoid [OrderedAddCommMonoid α] : OrderedAddCommMonoid (WithBot α) := -- { WithBot.partialOrder, WithBot.addCommMonoid with -- add_le_add_left := fun _ _ h c => add_le_add_left h c } -- -- instance linearOrderedAddCommMonoid [LinearOrderedAddCommMonoid α] : -- LinearOrderedAddCommMonoid (WithBot α) := -- { WithBot.linearOrder, WithBot.orderedAddCommMonoid with } end WithBot
Algebra\Order\Nonneg\Field.lean
/- Copyright (c) 2021 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import Mathlib.Algebra.Order.Field.Canonical.Defs import Mathlib.Algebra.Order.Field.InjSurj import Mathlib.Algebra.Order.Nonneg.Ring import Mathlib.Algebra.Order.Field.Unbundled.Basic import Mathlib.Data.Nat.Cast.Order.Ring /-! # Semifield structure on the type of nonnegative elements This file defines instances and prove some properties about the nonnegative elements `{x : α // 0 ≤ x}` of an arbitrary type `α`. This is used to derive algebraic structures on `ℝ≥0` and `ℚ≥0` automatically. ## Main declarations * `{x : α // 0 ≤ x}` is a `CanonicallyLinearOrderedSemifield` if `α` is a `LinearOrderedField`. -/ assert_not_exists abs_inv open Set variable {α : Type*} section NNRat variable [LinearOrderedSemifield α] {a : α} lemma NNRat.cast_nonneg (q : ℚ≥0) : 0 ≤ (q : α) := by rw [cast_def]; exact div_nonneg q.num.cast_nonneg q.den.cast_nonneg lemma nnqsmul_nonneg (q : ℚ≥0) (ha : 0 ≤ a) : 0 ≤ q • a := by rw [NNRat.smul_def]; exact mul_nonneg q.cast_nonneg ha end NNRat namespace Nonneg section LinearOrderedSemifield variable [LinearOrderedSemifield α] {x y : α} instance inv : Inv { x : α // 0 ≤ x } := ⟨fun x => ⟨x⁻¹, inv_nonneg (α := α) |>.2 x.2⟩⟩ @[simp, norm_cast] protected theorem coe_inv (a : { x : α // 0 ≤ x }) : ((a⁻¹ : { x : α // 0 ≤ x }) : α) = (a : α)⁻¹ := rfl @[simp] theorem inv_mk (hx : 0 ≤ x) : (⟨x, hx⟩ : { x : α // 0 ≤ x })⁻¹ = ⟨x⁻¹, inv_nonneg (α := α) |>.2 hx⟩ := rfl instance div : Div { x : α // 0 ≤ x } := ⟨fun x y => ⟨x / y, div_nonneg x.2 y.2⟩⟩ @[simp, norm_cast] protected theorem coe_div (a b : { x : α // 0 ≤ x }) : ((a / b : { x : α // 0 ≤ x }) : α) = a / b := rfl @[simp] theorem mk_div_mk (hx : 0 ≤ x) (hy : 0 ≤ y) : (⟨x, hx⟩ : { x : α // 0 ≤ x }) / ⟨y, hy⟩ = ⟨x / y, div_nonneg hx hy⟩ := rfl instance zpow : Pow { x : α // 0 ≤ x } ℤ := ⟨fun a n => ⟨(a : α) ^ n, zpow_nonneg a.2 _⟩⟩ @[simp, norm_cast] protected theorem coe_zpow (a : { x : α // 0 ≤ x }) (n : ℤ) : ((a ^ n : { x : α // 0 ≤ x }) : α) = (a : α) ^ n := rfl @[simp] theorem mk_zpow (hx : 0 ≤ x) (n : ℤ) : (⟨x, hx⟩ : { x : α // 0 ≤ x }) ^ n = ⟨x ^ n, zpow_nonneg hx n⟩ := rfl instance instNNRatCast : NNRatCast {x : α // 0 ≤ x} := ⟨fun q ↦ ⟨q, q.cast_nonneg⟩⟩ instance instNNRatSMul : SMul ℚ≥0 {x : α // 0 ≤ x} where smul q a := ⟨q • a, by rw [NNRat.smul_def]; exact mul_nonneg q.cast_nonneg a.2⟩ @[simp, norm_cast] lemma coe_nnratCast (q : ℚ≥0) : (q : {x : α // 0 ≤ x}) = (q : α) := rfl @[simp] lemma mk_nnratCast (q : ℚ≥0) : (⟨q, q.cast_nonneg⟩ : {x : α // 0 ≤ x}) = q := rfl @[simp, norm_cast] lemma coe_nnqsmul (q : ℚ≥0) (a : {x : α // 0 ≤ x}) : ↑(q • a) = (q • a : α) := rfl @[simp] lemma mk_nnqsmul (q : ℚ≥0) (a : α) (ha : 0 ≤ a) : (⟨q • a, by rw [NNRat.smul_def]; exact mul_nonneg q.cast_nonneg ha⟩ : {x : α // 0 ≤ x}) = q • a := rfl instance linearOrderedSemifield : LinearOrderedSemifield { x : α // 0 ≤ x } := Subtype.coe_injective.linearOrderedSemifield _ Nonneg.coe_zero Nonneg.coe_one Nonneg.coe_add Nonneg.coe_mul Nonneg.coe_inv Nonneg.coe_div (fun _ _ => rfl) coe_nnqsmul Nonneg.coe_pow Nonneg.coe_zpow Nonneg.coe_natCast coe_nnratCast (fun _ _ => rfl) fun _ _ => rfl end LinearOrderedSemifield instance canonicallyLinearOrderedSemifield [LinearOrderedField α] : CanonicallyLinearOrderedSemifield { x : α // 0 ≤ x } := { Nonneg.linearOrderedSemifield, Nonneg.canonicallyOrderedCommSemiring with } instance linearOrderedCommGroupWithZero [LinearOrderedField α] : LinearOrderedCommGroupWithZero { x : α // 0 ≤ x } := inferInstance end Nonneg
Algebra\Order\Nonneg\Floor.lean
/- Copyright (c) 2021 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import Mathlib.Algebra.Order.Floor import Mathlib.Algebra.Order.Ring.Unbundled.Nonneg /-! # Nonnegative elements are archimedean This file defines instances and prove some properties about the nonnegative elements `{x : α // 0 ≤ x}` of an arbitrary type `α`. This is used to derive algebraic structures on `ℝ≥0` and `ℚ≥0` automatically. ## Main declarations * `{x : α // 0 ≤ x}` is a `FloorSemiring` if `α` is. -/ namespace Nonneg variable {α : Type*} instance floorSemiring [OrderedSemiring α] [FloorSemiring α] : FloorSemiring { r : α // 0 ≤ r } where floor a := ⌊(a : α)⌋₊ ceil a := ⌈(a : α)⌉₊ floor_of_neg ha := FloorSemiring.floor_of_neg ha gc_floor ha := FloorSemiring.gc_floor (Subtype.coe_le_coe.2 ha) gc_ceil a n := FloorSemiring.gc_ceil (a : α) n @[norm_cast] theorem nat_floor_coe [OrderedSemiring α] [FloorSemiring α] (a : { r : α // 0 ≤ r }) : ⌊(a : α)⌋₊ = ⌊a⌋₊ := rfl @[norm_cast] theorem nat_ceil_coe [OrderedSemiring α] [FloorSemiring α] (a : { r : α // 0 ≤ r }) : ⌈(a : α)⌉₊ = ⌈a⌉₊ := rfl end Nonneg
Algebra\Order\Nonneg\Module.lean
/- Copyright (c) 2023 Apurva Nakade. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Apurva Nakade -/ import Mathlib.Algebra.Module.Defs import Mathlib.Algebra.Order.Module.OrderedSMul import Mathlib.Algebra.Order.Nonneg.Ring /-! # Modules over nonnegative elements This file defines instances and prove some properties about modules over nonnegative elements `{c : 𝕜 // 0 ≤ c}` of an arbitrary `OrderedSemiring 𝕜`. These instances are useful for working with `ConvexCone`. -/ variable {𝕜 𝕜' E : Type*} variable [OrderedSemiring 𝕜] local notation3 "𝕜≥0" => {c : 𝕜 // 0 ≤ c} namespace Nonneg section SMul variable [SMul 𝕜 𝕜'] instance instSMul : SMul 𝕜≥0 𝕜' where smul c x := c.val • x @[simp, norm_cast] lemma coe_smul (a : 𝕜≥0) (x : 𝕜') : (a : 𝕜) • x = a • x := rfl @[simp] lemma mk_smul (a) (ha) (x : 𝕜') : (⟨a, ha⟩ : 𝕜≥0) • x = a • x := rfl end SMul section IsScalarTower variable [SMul 𝕜 𝕜'] [SMul 𝕜 E] [SMul 𝕜' E] [IsScalarTower 𝕜 𝕜' E] instance instIsScalarTower : IsScalarTower 𝕜≥0 𝕜' E := SMul.comp.isScalarTower ↑Nonneg.coeRingHom end IsScalarTower section SMulWithZero variable [Zero 𝕜'] [SMulWithZero 𝕜 𝕜'] instance instSMulWithZero : SMulWithZero 𝕜≥0 𝕜' where smul_zero _ := smul_zero _ zero_smul _ := zero_smul _ _ end SMulWithZero section OrderedSMul variable [OrderedAddCommMonoid E] [SMulWithZero 𝕜 E] [hE : OrderedSMul 𝕜 E] instance instOrderedSMul : OrderedSMul 𝕜≥0 E := ⟨hE.1, hE.2⟩ end OrderedSMul section Module variable [AddCommMonoid E] [Module 𝕜 E] /-- A module over an ordered semiring is also a module over just the non-negative scalars. -/ instance instModule : Module 𝕜≥0 E := Module.compHom E Nonneg.coeRingHom end Module end Nonneg
Algebra\Order\Nonneg\Ring.lean
/- Copyright (c) 2021 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import Mathlib.Algebra.Order.GroupWithZero.Canonical import Mathlib.Algebra.Order.Ring.Unbundled.Nonneg import Mathlib.Algebra.Order.Monoid.Unbundled.Pow import Mathlib.Algebra.Order.Ring.Canonical import Mathlib.Algebra.Order.Ring.Defs import Mathlib.Algebra.Order.Ring.InjSurj import Mathlib.Order.CompleteLatticeIntervals import Mathlib.Order.LatticeIntervals /-! # Bundled ordered algebra instance on the type of nonnegative elements This file defines instances and prove some properties about the nonnegative elements `{x : α // 0 ≤ x}` of an arbitrary type `α`. Currently we only state instances and states some `simp`/`norm_cast` lemmas. When `α` is `ℝ`, this will give us some properties about `ℝ≥0`. ## Main declarations * `{x : α // 0 ≤ x}` is a `CanonicallyLinearOrderedAddCommMonoid` if `α` is a `LinearOrderedRing`. ## Implementation Notes Instead of `{x : α // 0 ≤ x}` we could also use `Set.Ici (0 : α)`, which is definitionally equal. However, using the explicit subtype has a big advantage: when writing an element explicitly with a proof of nonnegativity as `⟨x, hx⟩`, the `hx` is expected to have type `0 ≤ x`. If we would use `Ici 0`, then the type is expected to be `x ∈ Ici 0`. Although these types are definitionally equal, this often confuses the elaborator. Similar problems arise when doing cases on an element. The disadvantage is that we have to duplicate some instances about `Set.Ici` to this subtype. -/ open Set variable {α : Type*} namespace Nonneg instance orderedAddCommMonoid [OrderedAddCommMonoid α] : OrderedAddCommMonoid { x : α // 0 ≤ x } := Subtype.coe_injective.orderedAddCommMonoid _ Nonneg.coe_zero (fun _ _ => rfl) fun _ _ => rfl instance linearOrderedAddCommMonoid [LinearOrderedAddCommMonoid α] : LinearOrderedAddCommMonoid { x : α // 0 ≤ x } := Subtype.coe_injective.linearOrderedAddCommMonoid _ Nonneg.coe_zero (fun _ _ => rfl) (fun _ _ => rfl) (fun _ _ => rfl) fun _ _ => rfl instance orderedCancelAddCommMonoid [OrderedCancelAddCommMonoid α] : OrderedCancelAddCommMonoid { x : α // 0 ≤ x } := Subtype.coe_injective.orderedCancelAddCommMonoid _ Nonneg.coe_zero (fun _ _ => rfl) fun _ _ => rfl instance linearOrderedCancelAddCommMonoid [LinearOrderedCancelAddCommMonoid α] : LinearOrderedCancelAddCommMonoid { x : α // 0 ≤ x } := Subtype.coe_injective.linearOrderedCancelAddCommMonoid _ Nonneg.coe_zero (fun _ _ => rfl) (fun _ _ => rfl) (fun _ _ => rfl) fun _ _ => rfl instance orderedSemiring [OrderedSemiring α] : OrderedSemiring { x : α // 0 ≤ x } := Subtype.coe_injective.orderedSemiring _ Nonneg.coe_zero Nonneg.coe_one (fun _ _ => rfl) (fun _ _=> rfl) (fun _ _ => rfl) (fun _ _ => rfl) fun _ => rfl instance strictOrderedSemiring [StrictOrderedSemiring α] : StrictOrderedSemiring { x : α // 0 ≤ x } := Subtype.coe_injective.strictOrderedSemiring _ Nonneg.coe_zero Nonneg.coe_one (fun _ _ => rfl) (fun _ _ => rfl) (fun _ _ => rfl) (fun _ _ => rfl) fun _ => rfl instance orderedCommSemiring [OrderedCommSemiring α] : OrderedCommSemiring { x : α // 0 ≤ x } := Subtype.coe_injective.orderedCommSemiring _ Nonneg.coe_zero Nonneg.coe_one (fun _ _ => rfl) (fun _ _ => rfl) (fun _ _ => rfl) (fun _ _ => rfl) fun _ => rfl instance strictOrderedCommSemiring [StrictOrderedCommSemiring α] : StrictOrderedCommSemiring { x : α // 0 ≤ x } := Subtype.coe_injective.strictOrderedCommSemiring _ Nonneg.coe_zero Nonneg.coe_one (fun _ _ => rfl) (fun _ _ => rfl) (fun _ _ => rfl) (fun _ _ => rfl) fun _ => rfl instance nontrivial [LinearOrderedSemiring α] : Nontrivial { x : α // 0 ≤ x } := ⟨⟨0, 1, fun h => zero_ne_one (congr_arg Subtype.val h)⟩⟩ instance linearOrderedSemiring [LinearOrderedSemiring α] : LinearOrderedSemiring { x : α // 0 ≤ x } := Subtype.coe_injective.linearOrderedSemiring _ Nonneg.coe_zero Nonneg.coe_one (fun _ _ => rfl) (fun _ _ => rfl) (fun _ _ => rfl) (fun _ _ => rfl) (fun _ => rfl) (fun _ _ => rfl) fun _ _ => rfl instance linearOrderedCommMonoidWithZero [LinearOrderedCommRing α] : LinearOrderedCommMonoidWithZero { x : α // 0 ≤ x } := { Nonneg.linearOrderedSemiring, Nonneg.orderedCommSemiring with mul_le_mul_left := fun _ _ h c ↦ mul_le_mul_of_nonneg_left h c.prop } instance canonicallyOrderedAddCommMonoid [OrderedRing α] : CanonicallyOrderedAddCommMonoid { x : α // 0 ≤ x } := { Nonneg.orderedAddCommMonoid, Nonneg.orderBot with le_self_add := fun _ b => le_add_of_nonneg_right b.2 exists_add_of_le := fun {a b} h => ⟨⟨b - a, sub_nonneg_of_le h⟩, Subtype.ext (add_sub_cancel _ _).symm⟩ } instance canonicallyOrderedCommSemiring [OrderedCommRing α] [NoZeroDivisors α] : CanonicallyOrderedCommSemiring { x : α // 0 ≤ x } := { Nonneg.canonicallyOrderedAddCommMonoid, Nonneg.orderedCommSemiring with eq_zero_or_eq_zero_of_mul_eq_zero := by rintro ⟨a, ha⟩ ⟨b, hb⟩ simp only [mk_mul_mk, mk_eq_zero, mul_eq_zero, imp_self]} instance canonicallyLinearOrderedAddCommMonoid [LinearOrderedRing α] : CanonicallyLinearOrderedAddCommMonoid { x : α // 0 ≤ x } := { Subtype.instLinearOrder _, Nonneg.canonicallyOrderedAddCommMonoid with } instance orderedSub [LinearOrderedRing α] : OrderedSub { x : α // 0 ≤ x } := ⟨by rintro ⟨a, ha⟩ ⟨b, hb⟩ ⟨c, hc⟩ simp only [sub_le_iff_le_add, Subtype.mk_le_mk, mk_sub_mk, mk_add_mk, toNonneg_le, Subtype.coe_mk]⟩ end Nonneg
Algebra\Order\Positive\Field.lean
/- Copyright (c) 2022 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Algebra.Order.Positive.Ring import Mathlib.Algebra.Order.Field.Unbundled.Basic /-! # Algebraic structures on the set of positive numbers In this file we prove that the set of positive elements of a linear ordered field is a linear ordered commutative group. -/ variable {K : Type*} [LinearOrderedField K] namespace Positive instance Subtype.inv : Inv { x : K // 0 < x } := ⟨fun x => ⟨x⁻¹, inv_pos (α := K)|>.2 x.2⟩⟩ @[simp] theorem coe_inv (x : { x : K // 0 < x }) : ↑x⁻¹ = (x⁻¹ : K) := rfl instance : Pow { x : K // 0 < x } ℤ := ⟨fun x n => ⟨(x : K) ^ n, zpow_pos_of_pos x.2 _⟩⟩ @[simp] theorem coe_zpow (x : { x : K // 0 < x }) (n : ℤ) : ↑(x ^ n) = (x : K) ^ n := rfl instance : LinearOrderedCommGroup { x : K // 0 < x } := { Positive.Subtype.inv, Positive.linearOrderedCancelCommMonoid with mul_left_inv := fun a => Subtype.ext <| inv_mul_cancel a.2.ne' } end Positive
Algebra\Order\Positive\Ring.lean
/- Copyright (c) 2022 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import Mathlib.Algebra.Order.Ring.Defs import Mathlib.Algebra.Ring.InjSurj /-! # Algebraic structures on the set of positive numbers In this file we define various instances (`AddSemigroup`, `OrderedCommMonoid` etc) on the type `{x : R // 0 < x}`. In each case we try to require the weakest possible typeclass assumptions on `R` but possibly, there is a room for improvements. -/ open Function namespace Positive variable {M R K : Type*} section AddBasic variable [AddMonoid M] [Preorder M] [CovariantClass M M (· + ·) (· < ·)] instance : Add { x : M // 0 < x } := ⟨fun x y => ⟨x + y, add_pos x.2 y.2⟩⟩ @[simp, norm_cast] theorem coe_add (x y : { x : M // 0 < x }) : ↑(x + y) = (x + y : M) := rfl instance addSemigroup : AddSemigroup { x : M // 0 < x } := Subtype.coe_injective.addSemigroup _ coe_add instance addCommSemigroup {M : Type*} [AddCommMonoid M] [Preorder M] [CovariantClass M M (· + ·) (· < ·)] : AddCommSemigroup { x : M // 0 < x } := Subtype.coe_injective.addCommSemigroup _ coe_add instance addLeftCancelSemigroup {M : Type*} [AddLeftCancelMonoid M] [Preorder M] [CovariantClass M M (· + ·) (· < ·)] : AddLeftCancelSemigroup { x : M // 0 < x } := Subtype.coe_injective.addLeftCancelSemigroup _ coe_add instance addRightCancelSemigroup {M : Type*} [AddRightCancelMonoid M] [Preorder M] [CovariantClass M M (· + ·) (· < ·)] : AddRightCancelSemigroup { x : M // 0 < x } := Subtype.coe_injective.addRightCancelSemigroup _ coe_add instance covariantClass_add_lt : CovariantClass { x : M // 0 < x } { x : M // 0 < x } (· + ·) (· < ·) := ⟨fun _ y z hyz => Subtype.coe_lt_coe.1 <| add_lt_add_left (show (y : M) < z from hyz) _⟩ instance covariantClass_swap_add_lt [CovariantClass M M (swap (· + ·)) (· < ·)] : CovariantClass { x : M // 0 < x } { x : M // 0 < x } (swap (· + ·)) (· < ·) := ⟨fun _ y z hyz => Subtype.coe_lt_coe.1 <| add_lt_add_right (show (y : M) < z from hyz) _⟩ instance contravariantClass_add_lt [ContravariantClass M M (· + ·) (· < ·)] : ContravariantClass { x : M // 0 < x } { x : M // 0 < x } (· + ·) (· < ·) := ⟨fun _ _ _ h => Subtype.coe_lt_coe.1 <| lt_of_add_lt_add_left h⟩ instance contravariantClass_swap_add_lt [ContravariantClass M M (swap (· + ·)) (· < ·)] : ContravariantClass { x : M // 0 < x } { x : M // 0 < x } (swap (· + ·)) (· < ·) := ⟨fun _ _ _ h => Subtype.coe_lt_coe.1 <| lt_of_add_lt_add_right h⟩ instance contravariantClass_add_le [ContravariantClass M M (· + ·) (· ≤ ·)] : ContravariantClass { x : M // 0 < x } { x : M // 0 < x } (· + ·) (· ≤ ·) := ⟨fun _ _ _ h => Subtype.coe_le_coe.1 <| le_of_add_le_add_left h⟩ instance contravariantClass_swap_add_le [ContravariantClass M M (swap (· + ·)) (· ≤ ·)] : ContravariantClass { x : M // 0 < x } { x : M // 0 < x } (swap (· + ·)) (· ≤ ·) := ⟨fun _ _ _ h => Subtype.coe_le_coe.1 <| le_of_add_le_add_right h⟩ end AddBasic instance covariantClass_add_le [AddMonoid M] [PartialOrder M] [CovariantClass M M (· + ·) (· < ·)] : CovariantClass { x : M // 0 < x } { x : M // 0 < x } (· + ·) (· ≤ ·) := ⟨@fun _ _ _ h₁ => StrictMono.monotone (fun _ _ h => add_lt_add_left h _) h₁⟩ section Mul variable [StrictOrderedSemiring R] instance : Mul { x : R // 0 < x } := ⟨fun x y => ⟨x * y, mul_pos x.2 y.2⟩⟩ @[simp] theorem val_mul (x y : { x : R // 0 < x }) : ↑(x * y) = (x * y : R) := rfl instance : Pow { x : R // 0 < x } ℕ := ⟨fun x n => ⟨(x : R) ^ n , pow_pos x.2 n⟩⟩ @[simp] theorem val_pow (x : { x : R // 0 < x }) (n : ℕ) : ↑(x ^ n) = (x : R) ^ n := rfl instance : Semigroup { x : R // 0 < x } := Subtype.coe_injective.semigroup Subtype.val val_mul instance : Distrib { x : R // 0 < x } := Subtype.coe_injective.distrib _ coe_add val_mul instance : One { x : R // 0 < x } := ⟨⟨1, one_pos⟩⟩ @[simp] theorem val_one : ((1 : { x : R // 0 < x }) : R) = 1 := rfl instance : Monoid { x : R // 0 < x } := Subtype.coe_injective.monoid _ val_one val_mul val_pow end Mul section mul_comm instance orderedCommMonoid [StrictOrderedCommSemiring R] : OrderedCommMonoid { x : R // 0 < x } := { Subtype.partialOrder _, Subtype.coe_injective.commMonoid (M₂ := R) (Subtype.val) val_one val_mul val_pow with mul_le_mul_left := fun _ _ hxy c => Subtype.coe_le_coe.1 <| mul_le_mul_of_nonneg_left hxy c.2.le } /-- If `R` is a nontrivial linear ordered commutative semiring, then `{x : R // 0 < x}` is a linear ordered cancellative commutative monoid. -/ instance linearOrderedCancelCommMonoid [LinearOrderedCommSemiring R] : LinearOrderedCancelCommMonoid { x : R // 0 < x } := { Subtype.instLinearOrder _, Positive.orderedCommMonoid with le_of_mul_le_mul_left := fun a _ _ h => Subtype.coe_le_coe.1 <| (mul_le_mul_left a.2).1 h } end mul_comm end Positive
Algebra\Order\Ring\Abs.lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro -/ import Mathlib.Algebra.Order.Ring.Basic import Mathlib.Algebra.Order.Ring.Int import Mathlib.Algebra.Ring.Divisibility.Basic import Mathlib.Data.Nat.Cast.Order.Ring /-! # Absolute values in linear ordered rings. -/ variable {α : Type*} section LinearOrderedAddCommGroup variable [LinearOrderedCommGroup α] {a b : α} @[to_additive] lemma mabs_zpow (n : ℤ) (a : α) : |a ^ n|ₘ = |a|ₘ ^ |n| := by obtain n0 | n0 := le_total 0 n · obtain ⟨n, rfl⟩ := Int.eq_ofNat_of_zero_le n0 simp only [mabs_pow, zpow_natCast, Nat.abs_cast] · obtain ⟨m, h⟩ := Int.eq_ofNat_of_zero_le (neg_nonneg.2 n0) rw [← mabs_inv, ← zpow_neg, ← abs_neg, h, zpow_natCast, Nat.abs_cast, zpow_natCast] exact mabs_pow m _ end LinearOrderedAddCommGroup lemma odd_abs [LinearOrder α] [Ring α] {a : α} : Odd (abs a) ↔ Odd a := by cases' abs_choice a with h h <;> simp only [h, odd_neg] section LinearOrderedRing variable [LinearOrderedRing α] {n : ℕ} {a b c : α} @[simp] lemma abs_one : |(1 : α)| = 1 := abs_of_pos zero_lt_one lemma abs_two : |(2 : α)| = 2 := abs_of_pos zero_lt_two lemma abs_mul (a b : α) : |a * b| = |a| * |b| := by rw [abs_eq (mul_nonneg (abs_nonneg a) (abs_nonneg b))] rcases le_total a 0 with ha | ha <;> rcases le_total b 0 with hb | hb <;> simp only [abs_of_nonpos, abs_of_nonneg, true_or_iff, or_true_iff, eq_self_iff_true, neg_mul, mul_neg, neg_neg, *] /-- `abs` as a `MonoidWithZeroHom`. -/ def absHom : α →*₀ α where toFun := abs map_zero' := abs_zero map_one' := abs_one map_mul' := abs_mul @[simp] lemma abs_pow (a : α) (n : ℕ) : |a ^ n| = |a| ^ n := (absHom.toMonoidHom : α →* α).map_pow _ _ lemma pow_abs (a : α) (n : ℕ) : |a| ^ n = |a ^ n| := (abs_pow a n).symm lemma Even.pow_abs (hn : Even n) (a : α) : |a| ^ n = a ^ n := by rw [← abs_pow, abs_eq_self]; exact hn.pow_nonneg _ lemma abs_neg_one_pow (n : ℕ) : |(-1 : α) ^ n| = 1 := by rw [← pow_abs, abs_neg, abs_one, one_pow] lemma abs_pow_eq_one (a : α) (h : n ≠ 0) : |a ^ n| = 1 ↔ |a| = 1 := by convert pow_left_inj (abs_nonneg a) zero_le_one h exacts [(pow_abs _ _).symm, (one_pow _).symm] @[simp] lemma abs_mul_abs_self (a : α) : |a| * |a| = a * a := abs_by_cases (fun x => x * x = a * a) rfl (neg_mul_neg a a) @[simp] lemma abs_mul_self (a : α) : |a * a| = a * a := by rw [abs_mul, abs_mul_abs_self] lemma abs_eq_iff_mul_self_eq : |a| = |b| ↔ a * a = b * b := by rw [← abs_mul_abs_self, ← abs_mul_abs_self b] exact (mul_self_inj (abs_nonneg a) (abs_nonneg b)).symm lemma abs_lt_iff_mul_self_lt : |a| < |b| ↔ a * a < b * b := by rw [← abs_mul_abs_self, ← abs_mul_abs_self b] exact mul_self_lt_mul_self_iff (abs_nonneg a) (abs_nonneg b) lemma abs_le_iff_mul_self_le : |a| ≤ |b| ↔ a * a ≤ b * b := by rw [← abs_mul_abs_self, ← abs_mul_abs_self b] exact mul_self_le_mul_self_iff (abs_nonneg a) (abs_nonneg b) lemma abs_le_one_iff_mul_self_le_one : |a| ≤ 1 ↔ a * a ≤ 1 := by simpa only [abs_one, one_mul] using @abs_le_iff_mul_self_le α _ a 1 -- Porting note: added `simp` to replace `pow_bit0_abs` @[simp] lemma sq_abs (a : α) : |a| ^ 2 = a ^ 2 := by simpa only [sq] using abs_mul_abs_self a lemma abs_sq (x : α) : |x ^ 2| = x ^ 2 := by simpa only [sq] using abs_mul_self x lemma sq_lt_sq : a ^ 2 < b ^ 2 ↔ |a| < |b| := by simpa only [sq_abs] using (pow_left_strictMonoOn two_ne_zero).lt_iff_lt (abs_nonneg a) (abs_nonneg b) lemma sq_lt_sq' (h1 : -b < a) (h2 : a < b) : a ^ 2 < b ^ 2 := sq_lt_sq.2 (lt_of_lt_of_le (abs_lt.2 ⟨h1, h2⟩) (le_abs_self _)) lemma sq_le_sq : a ^ 2 ≤ b ^ 2 ↔ |a| ≤ |b| := by simpa only [sq_abs] using (pow_left_strictMonoOn two_ne_zero).le_iff_le (abs_nonneg a) (abs_nonneg b) lemma sq_le_sq' (h1 : -b ≤ a) (h2 : a ≤ b) : a ^ 2 ≤ b ^ 2 := sq_le_sq.2 (le_trans (abs_le.mpr ⟨h1, h2⟩) (le_abs_self _)) lemma abs_lt_of_sq_lt_sq (h : a ^ 2 < b ^ 2) (hb : 0 ≤ b) : |a| < b := by rwa [← abs_of_nonneg hb, ← sq_lt_sq] lemma abs_lt_of_sq_lt_sq' (h : a ^ 2 < b ^ 2) (hb : 0 ≤ b) : -b < a ∧ a < b := abs_lt.1 $ abs_lt_of_sq_lt_sq h hb lemma abs_le_of_sq_le_sq (h : a ^ 2 ≤ b ^ 2) (hb : 0 ≤ b) : |a| ≤ b := by rwa [← abs_of_nonneg hb, ← sq_le_sq] lemma abs_le_of_sq_le_sq' (h : a ^ 2 ≤ b ^ 2) (hb : 0 ≤ b) : -b ≤ a ∧ a ≤ b := abs_le.1 $ abs_le_of_sq_le_sq h hb lemma sq_eq_sq_iff_abs_eq_abs (a b : α) : a ^ 2 = b ^ 2 ↔ |a| = |b| := by simp only [le_antisymm_iff, sq_le_sq] @[simp] lemma sq_le_one_iff_abs_le_one (a : α) : a ^ 2 ≤ 1 ↔ |a| ≤ 1 := by simpa only [one_pow, abs_one] using @sq_le_sq _ _ a 1 @[simp] lemma sq_lt_one_iff_abs_lt_one (a : α) : a ^ 2 < 1 ↔ |a| < 1 := by simpa only [one_pow, abs_one] using @sq_lt_sq _ _ a 1 @[simp] lemma one_le_sq_iff_one_le_abs (a : α) : 1 ≤ a ^ 2 ↔ 1 ≤ |a| := by simpa only [one_pow, abs_one] using @sq_le_sq _ _ 1 a @[simp] lemma one_lt_sq_iff_one_lt_abs (a : α) : 1 < a ^ 2 ↔ 1 < |a| := by simpa only [one_pow, abs_one] using @sq_lt_sq _ _ 1 a lemma exists_abs_lt {α : Type*} [LinearOrderedRing α] (a : α) : ∃ b > 0, |a| < b := ⟨|a| + 1, lt_of_lt_of_le zero_lt_one <| by simp, lt_add_one |a|⟩ end LinearOrderedRing section LinearOrderedCommRing variable [LinearOrderedCommRing α] {a b c d : α} theorem abs_sub_sq (a b : α) : |a - b| * |a - b| = a * a + b * b - (1 + 1) * a * b := by rw [abs_mul_abs_self] simp only [mul_add, add_comm, add_left_comm, mul_comm, sub_eq_add_neg, mul_one, mul_neg, neg_add_rev, neg_neg, add_assoc] end LinearOrderedCommRing section variable [Ring α] [LinearOrder α] {a b : α} @[simp] theorem abs_dvd (a b : α) : |a| ∣ b ↔ a ∣ b := by cases' abs_choice a with h h <;> simp only [h, neg_dvd] theorem abs_dvd_self (a : α) : |a| ∣ a := (abs_dvd a a).mpr (dvd_refl a) @[simp] theorem dvd_abs (a b : α) : a ∣ |b| ↔ a ∣ b := by cases' abs_choice b with h h <;> simp only [h, dvd_neg] theorem self_dvd_abs (a : α) : a ∣ |a| := (dvd_abs a a).mpr (dvd_refl a) theorem abs_dvd_abs (a b : α) : |a| ∣ |b| ↔ a ∣ b := (abs_dvd _ _).trans (dvd_abs _ _) end open Nat section LinearOrderedRing variable {R : Type*} [LinearOrderedRing R] {a b : R} {n : ℕ} lemma pow_eq_pow_iff_of_ne_zero (hn : n ≠ 0) : a ^ n = b ^ n ↔ a = b ∨ a = -b ∧ Even n := match n.even_xor_odd with | .inl hne => by simp only [*, and_true, ← abs_eq_abs, ← pow_left_inj (abs_nonneg a) (abs_nonneg b) hn, hne.1.pow_abs] | .inr hn => by simp [hn, (hn.1.strictMono_pow (R := R)).injective.eq_iff] lemma pow_eq_pow_iff_cases : a ^ n = b ^ n ↔ n = 0 ∨ a = b ∨ a = -b ∧ Even n := by rcases eq_or_ne n 0 with rfl | hn <;> simp [pow_eq_pow_iff_of_ne_zero, *] lemma pow_eq_one_iff_of_ne_zero (hn : n ≠ 0) : a ^ n = 1 ↔ a = 1 ∨ a = -1 ∧ Even n := by simp [← pow_eq_pow_iff_of_ne_zero hn] lemma pow_eq_one_iff_cases : a ^ n = 1 ↔ n = 0 ∨ a = 1 ∨ a = -1 ∧ Even n := by simp [← pow_eq_pow_iff_cases] lemma pow_eq_neg_pow_iff (hb : b ≠ 0) : a ^ n = -b ^ n ↔ a = -b ∧ Odd n := match n.even_or_odd with | .inl he => suffices a ^ n > -b ^ n by simpa [he] using this.ne' lt_of_lt_of_le (by simp [he.pow_pos hb]) (he.pow_nonneg _) | .inr ho => by simp only [ho, and_true, ← ho.neg_pow, (ho.strictMono_pow (R := R)).injective.eq_iff] lemma pow_eq_neg_one_iff : a ^ n = -1 ↔ a = -1 ∧ Odd n := by simpa using pow_eq_neg_pow_iff (R := R) one_ne_zero end LinearOrderedRing variable {m n a : ℕ} /-- If `a` is even, then `n` is odd iff `n % a` is odd. -/ lemma Odd.mod_even_iff (ha : Even a) : Odd (n % a) ↔ Odd n := ((even_sub' <| mod_le n a).mp <| even_iff_two_dvd.mpr <| (even_iff_two_dvd.mp ha).trans <| dvd_sub_mod n).symm /-- If `a` is even, then `n` is even iff `n % a` is even. -/ lemma Even.mod_even_iff (ha : Even a) : Even (n % a) ↔ Even n := ((even_sub <| mod_le n a).mp <| even_iff_two_dvd.mpr <| (even_iff_two_dvd.mp ha).trans <| dvd_sub_mod n).symm /-- If `n` is odd and `a` is even, then `n % a` is odd. -/ lemma Odd.mod_even (hn : Odd n) (ha : Even a) : Odd (n % a) := (Odd.mod_even_iff ha).mpr hn /-- If `n` is even and `a` is even, then `n % a` is even. -/ lemma Even.mod_even (hn : Even n) (ha : Even a) : Even (n % a) := (Even.mod_even_iff ha).mpr hn lemma Odd.of_dvd_nat (hn : Odd n) (hm : m ∣ n) : Odd m := odd_iff_not_even.2 <| mt hm.even (odd_iff_not_even.1 hn) /-- `2` is not a factor of an odd natural number. -/ lemma Odd.ne_two_of_dvd_nat {m n : ℕ} (hn : Odd n) (hm : m ∣ n) : m ≠ 2 := by rintro rfl exact absurd (hn.of_dvd_nat hm) (by decide)
Algebra\Order\Ring\Basic.lean
/- Copyright (c) 2015 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Robert Y. Lewis -/ import Mathlib.Algebra.Order.Monoid.Unbundled.Pow import Mathlib.Algebra.Order.Ring.Defs import Mathlib.Algebra.Ring.Parity import Mathlib.Tactic.Bound.Attribute /-! # Basic lemmas about ordered rings -/ -- We should need only a minimal development of sets in order to get here. assert_not_exists Set.Subsingleton open Function Int variable {α M R : Type*} namespace MonoidHom variable [Ring R] [Monoid M] [LinearOrder M] [CovariantClass M M (· * ·) (· ≤ ·)] (f : R →* M) theorem map_neg_one : f (-1) = 1 := (pow_eq_one_iff (Nat.succ_ne_zero 1)).1 <| by rw [← map_pow, neg_one_sq, map_one] @[simp] theorem map_neg (x : R) : f (-x) = f x := by rw [← neg_one_mul, map_mul, map_neg_one, one_mul] theorem map_sub_swap (x y : R) : f (x - y) = f (y - x) := by rw [← map_neg, neg_sub] end MonoidHom section OrderedSemiring variable [OrderedSemiring R] {a b x y : R} {n m : ℕ} theorem zero_pow_le_one : ∀ n : ℕ, (0 : R) ^ n ≤ 1 | 0 => (pow_zero _).le | n + 1 => by rw [zero_pow n.succ_ne_zero]; exact zero_le_one theorem pow_add_pow_le (hx : 0 ≤ x) (hy : 0 ≤ y) (hn : n ≠ 0) : x ^ n + y ^ n ≤ (x + y) ^ n := by rcases Nat.exists_eq_add_one_of_ne_zero hn with ⟨k, rfl⟩ induction' k with k ih · simp only [zero_add, pow_one, le_refl] · let n := k.succ have h1 := add_nonneg (mul_nonneg hx (pow_nonneg hy n)) (mul_nonneg hy (pow_nonneg hx n)) have h2 := add_nonneg hx hy calc x ^ (n + 1) + y ^ (n + 1) ≤ x * x ^ n + y * y ^ n + (x * y ^ n + y * x ^ n) := by rw [pow_succ' _ n, pow_succ' _ n] exact le_add_of_nonneg_right h1 _ = (x + y) * (x ^ n + y ^ n) := by rw [add_mul, mul_add, mul_add, add_comm (y * x ^ n), ← add_assoc, ← add_assoc, add_assoc (x * x ^ n) (x * y ^ n), add_comm (x * y ^ n) (y * y ^ n), ← add_assoc] _ ≤ (x + y) ^ (n + 1) := by rw [pow_succ' _ n] exact mul_le_mul_of_nonneg_left (ih (Nat.succ_ne_zero k)) h2 @[bound] theorem pow_le_one : ∀ n : ℕ, 0 ≤ a → a ≤ 1 → a ^ n ≤ 1 | 0, _, _ => (pow_zero a).le | n + 1, h₀, h₁ => (pow_succ a n).le.trans (mul_le_one (pow_le_one n h₀ h₁) h₀ h₁) theorem pow_lt_one (h₀ : 0 ≤ a) (h₁ : a < 1) : ∀ {n : ℕ}, n ≠ 0 → a ^ n < 1 | 0, h => (h rfl).elim | n + 1, _ => by rw [pow_succ'] exact mul_lt_one_of_nonneg_of_lt_one_left h₀ h₁ (pow_le_one _ h₀ h₁.le) @[bound] theorem one_le_pow_of_one_le (H : 1 ≤ a) : ∀ n : ℕ, 1 ≤ a ^ n | 0 => by rw [pow_zero] | n + 1 => by rw [pow_succ'] simpa only [mul_one] using mul_le_mul H (one_le_pow_of_one_le H n) zero_le_one (le_trans zero_le_one H) theorem pow_right_mono (h : 1 ≤ a) : Monotone (a ^ ·) := monotone_nat_of_le_succ fun n => by rw [pow_succ'] exact le_mul_of_one_le_left (pow_nonneg (zero_le_one.trans h) _) h @[gcongr] theorem pow_le_pow_right (ha : 1 ≤ a) (h : n ≤ m) : a ^ n ≤ a ^ m := pow_right_mono ha h theorem le_self_pow (ha : 1 ≤ a) (h : m ≠ 0) : a ≤ a ^ m := by simpa only [pow_one] using pow_le_pow_right ha <| Nat.pos_iff_ne_zero.2 h /-- The `bound` tactic can't handle `m ≠ 0` goals yet, so we express as `0 < m` -/ @[bound] lemma Bound.le_self_pow_of_pos {m : ℕ} (ha : 1 ≤ a) (h : 0 < m) : a ≤ a ^ m := le_self_pow ha h.ne' @[mono, gcongr, bound] theorem pow_le_pow_left {a b : R} (ha : 0 ≤ a) (hab : a ≤ b) : ∀ n, a ^ n ≤ b ^ n | 0 => by simp | n + 1 => by simpa only [pow_succ'] using mul_le_mul hab (pow_le_pow_left ha hab _) (pow_nonneg ha _) (ha.trans hab) theorem one_lt_pow (ha : 1 < a) : ∀ {n : ℕ} (_ : n ≠ 0), 1 < a ^ n | 0, h => (h rfl).elim | n + 1, _ => by rw [pow_succ'] exact one_lt_mul_of_lt_of_le ha (one_le_pow_of_one_le ha.le _) lemma pow_add_pow_le' (ha : 0 ≤ a) (hb : 0 ≤ b) : a ^ n + b ^ n ≤ 2 * (a + b) ^ n := by rw [two_mul] exact add_le_add (pow_le_pow_left ha (le_add_of_nonneg_right hb) _) (pow_le_pow_left hb (le_add_of_nonneg_left ha) _) /-- `bound` lemma for branching on `1 ≤ a ∨ a ≤ 1` when proving `a ^ n ≤ a ^ m` -/ @[bound] lemma Bound.pow_le_pow_right_of_le_one_or_one_le (h : 1 ≤ a ∧ n ≤ m ∨ 0 ≤ a ∧ a ≤ 1 ∧ m ≤ n) : a ^ n ≤ a ^ m := by rcases h with ⟨a1, nm⟩ | ⟨a0, a1, mn⟩ · exact pow_le_pow_right a1 nm · exact pow_le_pow_of_le_one a0 a1 mn end OrderedSemiring section StrictOrderedSemiring variable [StrictOrderedSemiring R] {a x y : R} {n m : ℕ} @[gcongr, bound] theorem pow_lt_pow_left (h : x < y) (hx : 0 ≤ x) : ∀ {n : ℕ}, n ≠ 0 → x ^ n < y ^ n | 0, hn => by contradiction | n + 1, _ => by simpa only [pow_succ] using mul_lt_mul_of_le_of_lt_of_nonneg_of_pos (pow_le_pow_left hx h.le _) h hx (pow_pos (hx.trans_lt h) _) /-- See also `pow_left_strictMono` and `Nat.pow_left_strictMono`. -/ lemma pow_left_strictMonoOn (hn : n ≠ 0) : StrictMonoOn (· ^ n : R → R) {a | 0 ≤ a} := fun _a ha _b _ hab ↦ pow_lt_pow_left hab ha hn /-- See also `pow_right_strictMono'`. -/ lemma pow_right_strictMono (h : 1 < a) : StrictMono (a ^ ·) := have : 0 < a := zero_le_one.trans_lt h strictMono_nat_of_lt_succ fun n => by simpa only [one_mul, pow_succ'] using mul_lt_mul h (le_refl (a ^ n)) (pow_pos this _) this.le @[gcongr] theorem pow_lt_pow_right (h : 1 < a) (hmn : m < n) : a ^ m < a ^ n := pow_right_strictMono h hmn lemma pow_lt_pow_iff_right (h : 1 < a) : a ^ n < a ^ m ↔ n < m := (pow_right_strictMono h).lt_iff_lt lemma pow_le_pow_iff_right (h : 1 < a) : a ^ n ≤ a ^ m ↔ n ≤ m := (pow_right_strictMono h).le_iff_le theorem lt_self_pow (h : 1 < a) (hm : 1 < m) : a < a ^ m := by simpa only [pow_one] using pow_lt_pow_right h hm theorem pow_right_strictAnti (h₀ : 0 < a) (h₁ : a < 1) : StrictAnti (a ^ ·) := strictAnti_nat_of_succ_lt fun n => by simpa only [pow_succ', one_mul] using mul_lt_mul h₁ le_rfl (pow_pos h₀ n) zero_le_one theorem pow_lt_pow_iff_right_of_lt_one (h₀ : 0 < a) (h₁ : a < 1) : a ^ m < a ^ n ↔ n < m := (pow_right_strictAnti h₀ h₁).lt_iff_lt theorem pow_lt_pow_right_of_lt_one (h₀ : 0 < a) (h₁ : a < 1) (hmn : m < n) : a ^ n < a ^ m := (pow_lt_pow_iff_right_of_lt_one h₀ h₁).2 hmn theorem pow_lt_self_of_lt_one (h₀ : 0 < a) (h₁ : a < 1) (hn : 1 < n) : a ^ n < a := by simpa only [pow_one] using pow_lt_pow_right_of_lt_one h₀ h₁ hn theorem sq_pos_of_pos (ha : 0 < a) : 0 < a ^ 2 := pow_pos ha _ end StrictOrderedSemiring section StrictOrderedRing variable [StrictOrderedRing R] {a : R} lemma sq_pos_of_neg (ha : a < 0) : 0 < a ^ 2 := by rw [sq]; exact mul_pos_of_neg_of_neg ha ha end StrictOrderedRing section LinearOrderedSemiring variable [LinearOrderedSemiring R] {a b : R} {m n : ℕ} lemma pow_le_pow_iff_left (ha : 0 ≤ a) (hb : 0 ≤ b) (hn : n ≠ 0) : a ^ n ≤ b ^ n ↔ a ≤ b := (pow_left_strictMonoOn hn).le_iff_le ha hb lemma pow_lt_pow_iff_left (ha : 0 ≤ a) (hb : 0 ≤ b) (hn : n ≠ 0) : a ^ n < b ^ n ↔ a < b := (pow_left_strictMonoOn hn).lt_iff_lt ha hb @[simp] lemma pow_left_inj (ha : 0 ≤ a) (hb : 0 ≤ b) (hn : n ≠ 0) : a ^ n = b ^ n ↔ a = b := (pow_left_strictMonoOn hn).eq_iff_eq ha hb lemma pow_right_injective (ha₀ : 0 < a) (ha₁ : a ≠ 1) : Injective (a ^ ·) := by obtain ha₁ | ha₁ := ha₁.lt_or_lt · exact (pow_right_strictAnti ha₀ ha₁).injective · exact (pow_right_strictMono ha₁).injective @[simp] lemma pow_right_inj (ha₀ : 0 < a) (ha₁ : a ≠ 1) : a ^ m = a ^ n ↔ m = n := (pow_right_injective ha₀ ha₁).eq_iff theorem pow_le_one_iff_of_nonneg (ha : 0 ≤ a) (hn : n ≠ 0) : a ^ n ≤ 1 ↔ a ≤ 1 := by simpa only [one_pow] using pow_le_pow_iff_left ha zero_le_one hn theorem one_le_pow_iff_of_nonneg (ha : 0 ≤ a) (hn : n ≠ 0) : 1 ≤ a ^ n ↔ 1 ≤ a := by simpa only [one_pow] using pow_le_pow_iff_left (zero_le_one' R) ha hn theorem pow_lt_one_iff_of_nonneg (ha : 0 ≤ a) (hn : n ≠ 0) : a ^ n < 1 ↔ a < 1 := lt_iff_lt_of_le_iff_le (one_le_pow_iff_of_nonneg ha hn) theorem one_lt_pow_iff_of_nonneg (ha : 0 ≤ a) (hn : n ≠ 0) : 1 < a ^ n ↔ 1 < a := by simpa only [one_pow] using pow_lt_pow_iff_left (zero_le_one' R) ha hn lemma pow_eq_one_iff_of_nonneg (ha : 0 ≤ a) (hn : n ≠ 0) : a ^ n = 1 ↔ a = 1 := by simpa only [one_pow] using pow_left_inj ha zero_le_one hn theorem sq_le_one_iff {a : R} (ha : 0 ≤ a) : a ^ 2 ≤ 1 ↔ a ≤ 1 := pow_le_one_iff_of_nonneg ha (Nat.succ_ne_zero _) theorem sq_lt_one_iff {a : R} (ha : 0 ≤ a) : a ^ 2 < 1 ↔ a < 1 := pow_lt_one_iff_of_nonneg ha (Nat.succ_ne_zero _) theorem one_le_sq_iff {a : R} (ha : 0 ≤ a) : 1 ≤ a ^ 2 ↔ 1 ≤ a := one_le_pow_iff_of_nonneg ha (Nat.succ_ne_zero _) theorem one_lt_sq_iff {a : R} (ha : 0 ≤ a) : 1 < a ^ 2 ↔ 1 < a := one_lt_pow_iff_of_nonneg ha (Nat.succ_ne_zero _) theorem lt_of_pow_lt_pow_left (n : ℕ) (hb : 0 ≤ b) (h : a ^ n < b ^ n) : a < b := lt_of_not_ge fun hn => not_lt_of_ge (pow_le_pow_left hb hn _) h theorem le_of_pow_le_pow_left (hn : n ≠ 0) (hb : 0 ≤ b) (h : a ^ n ≤ b ^ n) : a ≤ b := le_of_not_lt fun h1 => not_le_of_lt (pow_lt_pow_left h1 hb hn) h @[simp] theorem sq_eq_sq {a b : R} (ha : 0 ≤ a) (hb : 0 ≤ b) : a ^ 2 = b ^ 2 ↔ a = b := pow_left_inj ha hb (by decide) theorem lt_of_mul_self_lt_mul_self (hb : 0 ≤ b) : a * a < b * b → a < b := by simp_rw [← sq] exact lt_of_pow_lt_pow_left _ hb /-! ### Lemmas for canonically linear ordered semirings or linear ordered rings The slightly unusual typeclass assumptions `[LinearOrderedSemiring R] [ExistsAddOfLE R]` cover two more familiar settings: * `[LinearOrderedRing R]`, eg `ℤ`, `ℚ` or `ℝ` * `[CanonicallyLinearOrderedSemiring R]` (although we don't actually have this typeclass), eg `ℕ`, `ℚ≥0` or `ℝ≥0` -/ variable [ExistsAddOfLE R] lemma add_sq_le : (a + b) ^ 2 ≤ 2 * (a ^ 2 + b ^ 2) := by calc (a + b) ^ 2 = a ^ 2 + b ^ 2 + (a * b + b * a) := by simp_rw [pow_succ', pow_zero, mul_one, add_mul, mul_add, add_comm (b * a), add_add_add_comm] _ ≤ a ^ 2 + b ^ 2 + (a * a + b * b) := add_le_add_left ?_ _ _ = _ := by simp_rw [pow_succ', pow_zero, mul_one, two_mul] cases le_total a b · exact mul_add_mul_le_mul_add_mul ‹_› ‹_› · exact mul_add_mul_le_mul_add_mul' ‹_› ‹_› -- TODO: Use `gcongr`, `positivity`, `ring` once those tactics are made available here lemma add_pow_le (ha : 0 ≤ a) (hb : 0 ≤ b) : ∀ n, (a + b) ^ n ≤ 2 ^ (n - 1) * (a ^ n + b ^ n) | 0 => by simp | 1 => by simp | n + 2 => by rw [pow_succ] calc _ ≤ 2 ^ n * (a ^ (n + 1) + b ^ (n + 1)) * (a + b) := mul_le_mul_of_nonneg_right (add_pow_le ha hb (n + 1)) $ add_nonneg ha hb _ = 2 ^ n * (a ^ (n + 2) + b ^ (n + 2) + (a ^ (n + 1) * b + b ^ (n + 1) * a)) := by rw [mul_assoc, mul_add, add_mul, add_mul, ← pow_succ, ← pow_succ, add_comm _ (b ^ _), add_add_add_comm, add_comm (_ * a)] _ ≤ 2 ^ n * (a ^ (n + 2) + b ^ (n + 2) + (a ^ (n + 1) * a + b ^ (n + 1) * b)) := mul_le_mul_of_nonneg_left (add_le_add_left ?_ _) $ pow_nonneg (zero_le_two (α := R)) _ _ = _ := by simp only [← pow_succ, ← two_mul, ← mul_assoc]; rfl · obtain hab | hba := le_total a b · exact mul_add_mul_le_mul_add_mul (pow_le_pow_left ha hab _) hab · exact mul_add_mul_le_mul_add_mul' (pow_le_pow_left hb hba _) hba protected lemma Even.add_pow_le (hn : Even n) : (a + b) ^ n ≤ 2 ^ (n - 1) * (a ^ n + b ^ n) := by obtain ⟨n, rfl⟩ := hn rw [← two_mul, pow_mul] calc _ ≤ (2 * (a ^ 2 + b ^ 2)) ^ n := pow_le_pow_left (sq_nonneg _) add_sq_le _ _ = 2 ^ n * (a ^ 2 + b ^ 2) ^ n := by -- TODO: Should be `Nat.cast_commute` rw [Commute.mul_pow]; simp [Commute, SemiconjBy, two_mul, mul_two] _ ≤ 2 ^ n * (2 ^ (n - 1) * ((a ^ 2) ^ n + (b ^ 2) ^ n)) := mul_le_mul_of_nonneg_left (add_pow_le (sq_nonneg _) (sq_nonneg _) _) $ pow_nonneg (zero_le_two (α := R)) _ _ = _ := by simp only [← mul_assoc, ← pow_add, ← pow_mul] cases n · rfl · simp [Nat.two_mul] lemma Even.pow_nonneg (hn : Even n) (a : R) : 0 ≤ a ^ n := by obtain ⟨k, rfl⟩ := hn; rw [pow_add]; exact mul_self_nonneg _ lemma Even.pow_pos (hn : Even n) (ha : a ≠ 0) : 0 < a ^ n := (hn.pow_nonneg _).lt_of_ne' (pow_ne_zero _ ha) lemma Even.pow_pos_iff (hn : Even n) (h₀ : n ≠ 0) : 0 < a ^ n ↔ a ≠ 0 := by obtain ⟨k, rfl⟩ := hn; rw [pow_add, mul_self_pos (α := R), pow_ne_zero_iff (by simpa using h₀)] lemma Odd.pow_neg_iff (hn : Odd n) : a ^ n < 0 ↔ a < 0 := by refine ⟨lt_imp_lt_of_le_imp_le (pow_nonneg · _), fun ha ↦ ?_⟩ obtain ⟨k, rfl⟩ := hn rw [pow_succ] exact mul_neg_of_pos_of_neg ((even_two_mul _).pow_pos ha.ne) ha lemma Odd.pow_nonneg_iff (hn : Odd n) : 0 ≤ a ^ n ↔ 0 ≤ a := le_iff_le_iff_lt_iff_lt.2 hn.pow_neg_iff lemma Odd.pow_nonpos_iff (hn : Odd n) : a ^ n ≤ 0 ↔ a ≤ 0 := by rw [le_iff_lt_or_eq, le_iff_lt_or_eq, hn.pow_neg_iff, pow_eq_zero_iff] rintro rfl; simp [Odd, eq_comm (a := 0)] at hn lemma Odd.pow_pos_iff (hn : Odd n) : 0 < a ^ n ↔ 0 < a := lt_iff_lt_of_le_iff_le hn.pow_nonpos_iff alias ⟨_, Odd.pow_nonpos⟩ := Odd.pow_nonpos_iff alias ⟨_, Odd.pow_neg⟩ := Odd.pow_neg_iff lemma Odd.strictMono_pow (hn : Odd n) : StrictMono fun a : R => a ^ n := by have hn₀ : n ≠ 0 := by rintro rfl; simp [Odd, eq_comm (a := 0)] at hn intro a b hab obtain ha | ha := le_total 0 a · exact pow_lt_pow_left hab ha hn₀ obtain hb | hb := lt_or_le 0 b · exact (hn.pow_nonpos ha).trans_lt (pow_pos hb _) obtain ⟨c, hac⟩ := exists_add_of_le ha obtain ⟨d, hbd⟩ := exists_add_of_le hb have hd := nonneg_of_le_add_right (hb.trans_eq hbd) refine lt_of_add_lt_add_right (a := c ^ n + d ^ n) ?_ dsimp calc a ^ n + (c ^ n + d ^ n) = d ^ n := by rw [← add_assoc, hn.pow_add_pow_eq_zero hac.symm, zero_add] _ < c ^ n := pow_lt_pow_left ?_ hd hn₀ _ = b ^ n + (c ^ n + d ^ n) := by rw [add_left_comm, hn.pow_add_pow_eq_zero hbd.symm, add_zero] refine lt_of_add_lt_add_right (a := a + b) ?_ rwa [add_rotate', ← hbd, add_zero, add_left_comm, ← add_assoc, ← hac, zero_add] lemma sq_pos_iff {a : R} : 0 < a ^ 2 ↔ a ≠ 0 := even_two.pow_pos_iff two_ne_zero alias ⟨_, sq_pos_of_ne_zero⟩ := sq_pos_iff alias pow_two_pos_of_ne_zero := sq_pos_of_ne_zero lemma pow_four_le_pow_two_of_pow_two_le (h : a ^ 2 ≤ b) : a ^ 4 ≤ b ^ 2 := (pow_mul a 2 2).symm ▸ pow_le_pow_left (sq_nonneg a) h 2 end LinearOrderedSemiring /-! ### Deprecated lemmas Those lemmas have been deprecated on 2023-12-23. -/ @[deprecated (since := "2023-12-23")] alias pow_mono := pow_right_mono @[deprecated (since := "2023-12-23")] alias pow_le_pow := pow_le_pow_right @[deprecated (since := "2023-12-23")] alias pow_le_pow_of_le_left := pow_le_pow_left @[deprecated (since := "2023-12-23")] alias pow_lt_pow_of_lt_left := pow_lt_pow_left @[deprecated (since := "2023-12-23")] alias strictMonoOn_pow := pow_left_strictMonoOn @[deprecated (since := "2023-12-23")] alias pow_strictMono_right := pow_right_strictMono @[deprecated (since := "2023-12-23")] alias pow_lt_pow := pow_lt_pow_right @[deprecated (since := "2023-12-23")] alias pow_lt_pow_iff := pow_lt_pow_iff_right @[deprecated (since := "2023-12-23")] alias pow_le_pow_iff := pow_le_pow_iff_right @[deprecated (since := "2023-12-23")] alias self_lt_pow := lt_self_pow @[deprecated (since := "2023-12-23")] alias strictAnti_pow := pow_right_strictAnti @[deprecated (since := "2023-12-23")] alias pow_lt_pow_iff_of_lt_one := pow_lt_pow_iff_right_of_lt_one @[deprecated (since := "2023-12-23")] alias pow_lt_pow_of_lt_one := pow_lt_pow_right_of_lt_one @[deprecated (since := "2023-12-23")] alias lt_of_pow_lt_pow := lt_of_pow_lt_pow_left @[deprecated (since := "2023-12-23")] alias le_of_pow_le_pow := le_of_pow_le_pow_left @[deprecated (since := "2023-12-23")] alias self_le_pow := le_self_pow @[deprecated (since := "2023-12-23")] alias Nat.pow_lt_pow_of_lt_right := pow_lt_pow_right @[deprecated (since := "2023-12-23")] protected alias Nat.pow_right_strictMono := pow_right_strictMono @[deprecated (since := "2023-12-23")] alias Nat.pow_le_iff_le_right := pow_le_pow_iff_right @[deprecated (since := "2023-12-23")] alias Nat.pow_lt_iff_lt_right := pow_lt_pow_iff_right
Algebra\Order\Ring\Canonical.lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro -/ import Mathlib.Algebra.Order.Ring.Defs import Mathlib.Algebra.Order.Sub.Canonical import Mathlib.Algebra.Ring.Parity /-! # Canonically ordered rings and semirings. * `CanonicallyOrderedCommSemiring` - `CanonicallyOrderedAddCommMonoid` & multiplication & `*` respects `≤` & no zero divisors - `CommSemiring` & `a ≤ b ↔ ∃ c, b = a + c` & no zero divisors ## TODO We're still missing some typeclasses, like * `CanonicallyOrderedSemiring` They have yet to come up in practice. -/ open Function universe u variable {α : Type u} {β : Type*} /-- A canonically ordered commutative semiring is an ordered, commutative semiring in which `a ≤ b` iff there exists `c` with `b = a + c`. This is satisfied by the natural numbers, for example, but not the integers or other ordered groups. -/ class CanonicallyOrderedCommSemiring (α : Type*) extends CanonicallyOrderedAddCommMonoid α, CommSemiring α where /-- No zero divisors. -/ protected eq_zero_or_eq_zero_of_mul_eq_zero : ∀ {a b : α}, a * b = 0 → a = 0 ∨ b = 0 section CanonicallyOrderedCommSemiring variable [CanonicallyOrderedCommSemiring α] {a b c d : α} -- this holds more generally in a `CanonicallyOrderedAddCommMonoid` if we refactor `Odd` to use -- either `2 • t` or `t + t` instead of `2 * t`. lemma Odd.pos [Nontrivial α] : Odd a → 0 < a := by rintro ⟨k, rfl⟩; simp [pos_iff_ne_zero] namespace CanonicallyOrderedCommSemiring -- see Note [lower instance priority] instance (priority := 100) toNoZeroDivisors : NoZeroDivisors α := ⟨CanonicallyOrderedCommSemiring.eq_zero_or_eq_zero_of_mul_eq_zero⟩ -- see Note [lower instance priority] instance (priority := 100) toCovariantClassMulLE : CovariantClass α α (· * ·) (· ≤ ·) := by refine ⟨fun a b c h => ?_⟩ rcases exists_add_of_le h with ⟨c, rfl⟩ rw [mul_add] apply self_le_add_right -- see Note [lower instance priority] instance (priority := 100) toOrderedCommMonoid : OrderedCommMonoid α where mul_le_mul_left := fun _ _ => mul_le_mul_left' -- see Note [lower instance priority] instance (priority := 100) toOrderedCommSemiring : OrderedCommSemiring α := { ‹CanonicallyOrderedCommSemiring α› with zero_le_one := zero_le _, mul_le_mul_of_nonneg_left := fun a b c h _ => mul_le_mul_left' h _, mul_le_mul_of_nonneg_right := fun a b c h _ => mul_le_mul_right' h _ } @[simp] protected theorem mul_pos : 0 < a * b ↔ 0 < a ∧ 0 < b := by simp only [pos_iff_ne_zero, ne_eq, mul_eq_zero, not_or] lemma pow_pos (ha : 0 < a) (n : ℕ) : 0 < a ^ n := pos_iff_ne_zero.2 <| pow_ne_zero _ ha.ne' protected lemma mul_lt_mul_of_lt_of_lt [PosMulStrictMono α] (hab : a < b) (hcd : c < d) : a * c < b * d := by -- TODO: This should be an instance but it currently times out have := posMulStrictMono_iff_mulPosStrictMono.1 ‹_› obtain rfl | hc := eq_zero_or_pos c · rw [mul_zero] exact mul_pos ((zero_le _).trans_lt hab) hcd · exact mul_lt_mul_of_pos' hab hcd hc ((zero_le _).trans_lt hab) end CanonicallyOrderedCommSemiring end CanonicallyOrderedCommSemiring section Sub variable [CanonicallyOrderedCommSemiring α] {a b c : α} variable [Sub α] [OrderedSub α] variable [IsTotal α (· ≤ ·)] namespace AddLECancellable protected theorem mul_tsub (h : AddLECancellable (a * c)) : a * (b - c) = a * b - a * c := by cases' total_of (· ≤ ·) b c with hbc hcb · rw [tsub_eq_zero_iff_le.2 hbc, mul_zero, tsub_eq_zero_iff_le.2 (mul_le_mul_left' hbc a)] · apply h.eq_tsub_of_add_eq rw [← mul_add, tsub_add_cancel_of_le hcb] protected theorem tsub_mul (h : AddLECancellable (b * c)) : (a - b) * c = a * c - b * c := by simp only [mul_comm _ c] at * exact h.mul_tsub end AddLECancellable variable [ContravariantClass α α (· + ·) (· ≤ ·)] theorem mul_tsub (a b c : α) : a * (b - c) = a * b - a * c := Contravariant.AddLECancellable.mul_tsub theorem tsub_mul (a b c : α) : (a - b) * c = a * c - b * c := Contravariant.AddLECancellable.tsub_mul lemma mul_tsub_one (a b : α) : a * (b - 1) = a * b - a := by rw [mul_tsub, mul_one] lemma tsub_one_mul (a b : α) : (a - 1) * b = a * b - b := by rw [tsub_mul, one_mul] /-- The `tsub` version of `mul_self_sub_mul_self`. Notably, this holds for `Nat` and `NNReal`. -/ theorem mul_self_tsub_mul_self (a b : α) : a * a - b * b = (a + b) * (a - b) := by rw [mul_tsub, add_mul, add_mul, tsub_add_eq_tsub_tsub, mul_comm b a, add_tsub_cancel_right] /-- The `tsub` version of `sq_sub_sq`. Notably, this holds for `Nat` and `NNReal`. -/ theorem sq_tsub_sq (a b : α) : a ^ 2 - b ^ 2 = (a + b) * (a - b) := by rw [sq, sq, mul_self_tsub_mul_self] theorem mul_self_tsub_one (a : α) : a * a - 1 = (a + 1) * (a - 1) := by rw [← mul_self_tsub_mul_self, mul_one] end Sub
Algebra\Order\Ring\Cast.lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Algebra.Order.Ring.Int import Mathlib.Data.Nat.Cast.Order.Ring /-! # Order properties of cast of integers This file proves additional properties about the *canonical* homomorphism from the integers into an additive group with a one (`Int.cast`), particularly results involving algebraic homomorphisms or the order structure on `ℤ` which were not available in the import dependencies of `Mathlib.Data.Int.Cast.Basic`. ## TODO Move order lemmas about `Nat.cast`, `Rat.cast`, `NNRat.cast` here. -/ open Function Nat variable {R : Type*} namespace Int section OrderedAddCommGroupWithOne variable [AddCommGroupWithOne R] [PartialOrder R] [CovariantClass R R (· + ·) (· ≤ ·)] variable [ZeroLEOneClass R] lemma cast_mono : Monotone (Int.cast : ℤ → R) := by intro m n h rw [← sub_nonneg] at h lift n - m to ℕ using h with k hk rw [← sub_nonneg, ← cast_sub, ← hk, cast_natCast] exact k.cast_nonneg' variable [NeZero (1 : R)] {m n : ℤ} @[simp] lemma cast_nonneg : ∀ {n : ℤ}, (0 : R) ≤ n ↔ 0 ≤ n | (n : ℕ) => by simp | -[n+1] => by have : -(n : R) < 1 := lt_of_le_of_lt (by simp) zero_lt_one simpa [(negSucc_lt_zero n).not_le, ← sub_eq_add_neg, le_neg] using this.not_le @[simp, norm_cast] lemma cast_le : (m : R) ≤ n ↔ m ≤ n := by rw [← sub_nonneg, ← cast_sub, cast_nonneg, sub_nonneg] lemma cast_strictMono : StrictMono (fun x : ℤ => (x : R)) := strictMono_of_le_iff_le fun _ _ => cast_le.symm @[simp, norm_cast] lemma cast_lt : (m : R) < n ↔ m < n := cast_strictMono.lt_iff_lt @[simp] lemma cast_nonpos : (n : R) ≤ 0 ↔ n ≤ 0 := by rw [← cast_zero, cast_le] @[simp] lemma cast_pos : (0 : R) < n ↔ 0 < n := by rw [← cast_zero, cast_lt] @[simp] lemma cast_lt_zero : (n : R) < 0 ↔ n < 0 := by rw [← cast_zero, cast_lt] end OrderedAddCommGroupWithOne section LinearOrderedRing variable [LinearOrderedRing R] {a b n : ℤ} {x : R} @[simp, norm_cast] lemma cast_min : ↑(min a b) = (min a b : R) := Monotone.map_min cast_mono @[simp, norm_cast] lemma cast_max : (↑(max a b) : R) = max (a : R) (b : R) := Monotone.map_max cast_mono @[simp, norm_cast] lemma cast_abs : (↑|a| : R) = |(a : R)| := by simp [abs_eq_max_neg] lemma cast_one_le_of_pos (h : 0 < a) : (1 : R) ≤ a := mod_cast Int.add_one_le_of_lt h lemma cast_le_neg_one_of_neg (h : a < 0) : (a : R) ≤ -1 := by rw [← Int.cast_one, ← Int.cast_neg, cast_le] exact Int.le_sub_one_of_lt h variable (R) in lemma cast_le_neg_one_or_one_le_cast_of_ne_zero (hn : n ≠ 0) : (n : R) ≤ -1 ∨ 1 ≤ (n : R) := hn.lt_or_lt.imp cast_le_neg_one_of_neg cast_one_le_of_pos lemma nneg_mul_add_sq_of_abs_le_one (n : ℤ) (hx : |x| ≤ 1) : (0 : R) ≤ n * x + n * n := by have hnx : 0 < n → 0 ≤ x + n := fun hn => by have := _root_.add_le_add (neg_le_of_abs_le hx) (cast_one_le_of_pos hn) rwa [add_left_neg] at this have hnx' : n < 0 → x + n ≤ 0 := fun hn => by have := _root_.add_le_add (le_of_abs_le hx) (cast_le_neg_one_of_neg hn) rwa [add_right_neg] at this rw [← mul_add, mul_nonneg_iff] rcases lt_trichotomy n 0 with (h | rfl | h) · exact Or.inr ⟨mod_cast h.le, hnx' h⟩ · simp [le_total 0 x] · exact Or.inl ⟨mod_cast h.le, hnx h⟩ lemma cast_natAbs : (n.natAbs : R) = |n| := by cases n · simp · rw [abs_eq_natAbs, natAbs_negSucc, cast_succ, cast_natCast, cast_succ] end LinearOrderedRing end Int /-! ### Order dual -/ open OrderDual namespace OrderDual instance instIntCast [IntCast R] : IntCast Rᵒᵈ := ‹_› instance instAddGroupWithOne [AddGroupWithOne R] : AddGroupWithOne Rᵒᵈ := ‹_› instance instAddCommGroupWithOne [AddCommGroupWithOne R] : AddCommGroupWithOne Rᵒᵈ := ‹_› end OrderDual @[simp] lemma toDual_intCast [IntCast R] (n : ℤ) : toDual (n : R) = n := rfl @[simp] lemma ofDual_intCast [IntCast R] (n : ℤ) : (ofDual n : R) = n := rfl /-! ### Lexicographic order -/ namespace Lex instance instIntCast [IntCast R] : IntCast (Lex R) := ‹_› instance instAddGroupWithOne [AddGroupWithOne R] : AddGroupWithOne (Lex R) := ‹_› instance instAddCommGroupWithOne [AddCommGroupWithOne R] : AddCommGroupWithOne (Lex R) := ‹_› end Lex @[simp] lemma toLex_intCast [IntCast R] (n : ℤ) : toLex (n : R) = n := rfl @[simp] lemma ofLex_intCast [IntCast R] (n : ℤ) : (ofLex n : R) = n := rfl
Algebra\Order\Ring\Cone.lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro -/ import Mathlib.Algebra.Order.Group.Cone import Mathlib.Algebra.Order.Ring.Defs /-! # Constructing an ordered ring from a ring with a specified positive cone. -/ /-! ### Positive cones -/ variable {α : Type*} [Ring α] [Nontrivial α] namespace Ring /-- A positive cone in a ring consists of a positive cone in underlying `AddCommGroup`, which contains `1` and such that the positive elements are closed under multiplication. -/ structure PositiveCone (α : Type*) [Ring α] extends AddCommGroup.PositiveCone α where /-- In a positive cone, `1` is `nonneg` -/ one_nonneg : nonneg 1 /-- In a positive cone, if `a` and `b` are `pos` then so is `a * b` -/ mul_pos : ∀ a b, pos a → pos b → pos (a * b) /-- Forget that a positive cone in a ring respects the multiplicative structure. -/ add_decl_doc PositiveCone.toPositiveCone /-- A total positive cone in a nontrivial ring induces a linear order. -/ structure TotalPositiveCone (α : Type*) [Ring α] extends PositiveCone α, AddCommGroup.TotalPositiveCone α /-- Forget that a `TotalPositiveCone` in a ring is total. -/ add_decl_doc TotalPositiveCone.toPositiveCone_1 /-- Forget that a `TotalPositiveCone` in a ring respects the multiplicative structure. -/ add_decl_doc TotalPositiveCone.toTotalPositiveCone theorem PositiveCone.one_pos (C : PositiveCone α) : C.pos 1 := (C.pos_iff _).2 ⟨C.one_nonneg, fun h => one_ne_zero <| C.nonneg_antisymm C.one_nonneg h⟩ end Ring open Ring /-- Construct a `StrictOrderedRing` by designating a positive cone in an existing `Ring`. -/ def StrictOrderedRing.mkOfPositiveCone (C : PositiveCone α) : StrictOrderedRing α := { ‹Ring α›, OrderedAddCommGroup.mkOfPositiveCone C.toPositiveCone with exists_pair_ne := ⟨0, 1, fun h => by simpa [← h, C.pos_iff] using C.one_pos⟩, zero_le_one := by change C.nonneg (1 - 0) convert C.one_nonneg simp, mul_pos := fun x y xp yp => by change C.pos (x * y - 0) -- Porting note: used to be convert, but it relied on unfolding definitions rw [sub_zero] exact C.mul_pos x y (by rwa [← sub_zero x]) (by rwa [← sub_zero y]) } /-- Construct a `LinearOrderedRing` by designating a positive cone in an existing `Ring`. -/ def LinearOrderedRing.mkOfPositiveCone (C : TotalPositiveCone α) : LinearOrderedRing α := { LinearOrderedAddCommGroup.mkOfPositiveCone C.toTotalPositiveCone, StrictOrderedRing.mkOfPositiveCone C.toPositiveCone_1 with }
Algebra\Order\Ring\Defs.lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro, Yaël Dillies -/ import Mathlib.Algebra.Order.Ring.Unbundled.Basic import Mathlib.Algebra.CharZero.Defs import Mathlib.Algebra.Order.Group.Defs import Mathlib.Algebra.Order.GroupWithZero.Unbundled import Mathlib.Algebra.Order.Monoid.NatCast import Mathlib.Algebra.Order.Monoid.Unbundled.MinMax import Mathlib.Algebra.Ring.Defs import Mathlib.Tactic.Tauto import Mathlib.Algebra.Order.Monoid.Unbundled.ExistsOfLE /-! # Ordered rings and semirings This file develops the basics of ordered (semi)rings. Each typeclass here comprises * an algebraic class (`Semiring`, `CommSemiring`, `Ring`, `CommRing`) * an order class (`PartialOrder`, `LinearOrder`) * assumptions on how both interact ((strict) monotonicity, canonicity) For short, * "`+` respects `≤`" means "monotonicity of addition" * "`+` respects `<`" means "strict monotonicity of addition" * "`*` respects `≤`" means "monotonicity of multiplication by a nonnegative number". * "`*` respects `<`" means "strict monotonicity of multiplication by a positive number". ## Typeclasses * `OrderedSemiring`: Semiring with a partial order such that `+` and `*` respect `≤`. * `StrictOrderedSemiring`: Nontrivial semiring with a partial order such that `+` and `*` respects `<`. * `OrderedCommSemiring`: Commutative semiring with a partial order such that `+` and `*` respect `≤`. * `StrictOrderedCommSemiring`: Nontrivial commutative semiring with a partial order such that `+` and `*` respect `<`. * `OrderedRing`: Ring with a partial order such that `+` respects `≤` and `*` respects `<`. * `OrderedCommRing`: Commutative ring with a partial order such that `+` respects `≤` and `*` respects `<`. * `LinearOrderedSemiring`: Nontrivial semiring with a linear order such that `+` respects `≤` and `*` respects `<`. * `LinearOrderedCommSemiring`: Nontrivial commutative semiring with a linear order such that `+` respects `≤` and `*` respects `<`. * `LinearOrderedRing`: Nontrivial ring with a linear order such that `+` respects `≤` and `*` respects `<`. * `LinearOrderedCommRing`: Nontrivial commutative ring with a linear order such that `+` respects `≤` and `*` respects `<`. * `CanonicallyOrderedCommSemiring`: Commutative semiring with a partial order such that `+` respects `≤`, `*` respects `<`, and `a ≤ b ↔ ∃ c, b = a + c`. ## Hierarchy The hardest part of proving order lemmas might be to figure out the correct generality and its corresponding typeclass. Here's an attempt at demystifying it. For each typeclass, we list its immediate predecessors and what conditions are added to each of them. * `OrderedSemiring` - `OrderedAddCommMonoid` & multiplication & `*` respects `≤` - `Semiring` & partial order structure & `+` respects `≤` & `*` respects `≤` * `StrictOrderedSemiring` - `OrderedCancelAddCommMonoid` & multiplication & `*` respects `<` & nontriviality - `OrderedSemiring` & `+` respects `<` & `*` respects `<` & nontriviality * `OrderedCommSemiring` - `OrderedSemiring` & commutativity of multiplication - `CommSemiring` & partial order structure & `+` respects `≤` & `*` respects `<` * `StrictOrderedCommSemiring` - `StrictOrderedSemiring` & commutativity of multiplication - `OrderedCommSemiring` & `+` respects `<` & `*` respects `<` & nontriviality * `OrderedRing` - `OrderedSemiring` & additive inverses - `OrderedAddCommGroup` & multiplication & `*` respects `<` - `Ring` & partial order structure & `+` respects `≤` & `*` respects `<` * `StrictOrderedRing` - `StrictOrderedSemiring` & additive inverses - `OrderedSemiring` & `+` respects `<` & `*` respects `<` & nontriviality * `OrderedCommRing` - `OrderedRing` & commutativity of multiplication - `OrderedCommSemiring` & additive inverses - `CommRing` & partial order structure & `+` respects `≤` & `*` respects `<` * `StrictOrderedCommRing` - `StrictOrderedCommSemiring` & additive inverses - `StrictOrderedRing` & commutativity of multiplication - `OrderedCommRing` & `+` respects `<` & `*` respects `<` & nontriviality * `LinearOrderedSemiring` - `StrictOrderedSemiring` & totality of the order - `LinearOrderedAddCommMonoid` & multiplication & nontriviality & `*` respects `<` * `LinearOrderedCommSemiring` - `StrictOrderedCommSemiring` & totality of the order - `LinearOrderedSemiring` & commutativity of multiplication * `LinearOrderedRing` - `StrictOrderedRing` & totality of the order - `LinearOrderedSemiring` & additive inverses - `LinearOrderedAddCommGroup` & multiplication & `*` respects `<` - `Ring` & `IsDomain` & linear order structure * `LinearOrderedCommRing` - `StrictOrderedCommRing` & totality of the order - `LinearOrderedRing` & commutativity of multiplication - `LinearOrderedCommSemiring` & additive inverses - `CommRing` & `IsDomain` & linear order structure -/ open Function universe u variable {α : Type u} {β : Type*} /-! Note that `OrderDual` does not satisfy any of the ordered ring typeclasses due to the `zero_le_one` field. -/ /-- An `OrderedSemiring` is a semiring with a partial order such that addition is monotone and multiplication by a nonnegative number is monotone. -/ class OrderedSemiring (α : Type u) extends Semiring α, OrderedAddCommMonoid α where /-- `0 ≤ 1` in any ordered semiring. -/ protected zero_le_one : (0 : α) ≤ 1 /-- In an ordered semiring, we can multiply an inequality `a ≤ b` on the left by a non-negative element `0 ≤ c` to obtain `c * a ≤ c * b`. -/ protected mul_le_mul_of_nonneg_left : ∀ a b c : α, a ≤ b → 0 ≤ c → c * a ≤ c * b /-- In an ordered semiring, we can multiply an inequality `a ≤ b` on the right by a non-negative element `0 ≤ c` to obtain `a * c ≤ b * c`. -/ protected mul_le_mul_of_nonneg_right : ∀ a b c : α, a ≤ b → 0 ≤ c → a * c ≤ b * c /-- An `OrderedCommSemiring` is a commutative semiring with a partial order such that addition is monotone and multiplication by a nonnegative number is monotone. -/ class OrderedCommSemiring (α : Type u) extends OrderedSemiring α, CommSemiring α where mul_le_mul_of_nonneg_right a b c ha hc := -- parentheses ensure this generates an `optParam` rather than an `autoParam` (by simpa only [mul_comm] using mul_le_mul_of_nonneg_left a b c ha hc) /-- An `OrderedRing` is a ring with a partial order such that addition is monotone and multiplication by a nonnegative number is monotone. -/ class OrderedRing (α : Type u) extends Ring α, OrderedAddCommGroup α where /-- `0 ≤ 1` in any ordered ring. -/ protected zero_le_one : 0 ≤ (1 : α) /-- The product of non-negative elements is non-negative. -/ protected mul_nonneg : ∀ a b : α, 0 ≤ a → 0 ≤ b → 0 ≤ a * b /-- An `OrderedCommRing` is a commutative ring with a partial order such that addition is monotone and multiplication by a nonnegative number is monotone. -/ class OrderedCommRing (α : Type u) extends OrderedRing α, CommRing α /-- A `StrictOrderedSemiring` is a nontrivial semiring with a partial order such that addition is strictly monotone and multiplication by a positive number is strictly monotone. -/ class StrictOrderedSemiring (α : Type u) extends Semiring α, OrderedCancelAddCommMonoid α, Nontrivial α where /-- In a strict ordered semiring, `0 ≤ 1`. -/ protected zero_le_one : (0 : α) ≤ 1 /-- Left multiplication by a positive element is strictly monotone. -/ protected mul_lt_mul_of_pos_left : ∀ a b c : α, a < b → 0 < c → c * a < c * b /-- Right multiplication by a positive element is strictly monotone. -/ protected mul_lt_mul_of_pos_right : ∀ a b c : α, a < b → 0 < c → a * c < b * c /-- A `StrictOrderedCommSemiring` is a commutative semiring with a partial order such that addition is strictly monotone and multiplication by a positive number is strictly monotone. -/ class StrictOrderedCommSemiring (α : Type u) extends StrictOrderedSemiring α, CommSemiring α /-- A `StrictOrderedRing` is a ring with a partial order such that addition is strictly monotone and multiplication by a positive number is strictly monotone. -/ class StrictOrderedRing (α : Type u) extends Ring α, OrderedAddCommGroup α, Nontrivial α where /-- In a strict ordered ring, `0 ≤ 1`. -/ protected zero_le_one : 0 ≤ (1 : α) /-- The product of two positive elements is positive. -/ protected mul_pos : ∀ a b : α, 0 < a → 0 < b → 0 < a * b /-- A `StrictOrderedCommRing` is a commutative ring with a partial order such that addition is strictly monotone and multiplication by a positive number is strictly monotone. -/ class StrictOrderedCommRing (α : Type*) extends StrictOrderedRing α, CommRing α /- It's not entirely clear we should assume `Nontrivial` at this point; it would be reasonable to explore changing this, but be warned that the instances involving `Domain` may cause typeclass search loops. -/ /-- A `LinearOrderedSemiring` is a nontrivial semiring with a linear order such that addition is monotone and multiplication by a positive number is strictly monotone. -/ class LinearOrderedSemiring (α : Type u) extends StrictOrderedSemiring α, LinearOrderedAddCommMonoid α /-- A `LinearOrderedCommSemiring` is a nontrivial commutative semiring with a linear order such that addition is monotone and multiplication by a positive number is strictly monotone. -/ class LinearOrderedCommSemiring (α : Type*) extends StrictOrderedCommSemiring α, LinearOrderedSemiring α /-- A `LinearOrderedRing` is a ring with a linear order such that addition is monotone and multiplication by a positive number is strictly monotone. -/ class LinearOrderedRing (α : Type u) extends StrictOrderedRing α, LinearOrder α /-- A `LinearOrderedCommRing` is a commutative ring with a linear order such that addition is monotone and multiplication by a positive number is strictly monotone. -/ class LinearOrderedCommRing (α : Type u) extends LinearOrderedRing α, CommMonoid α section OrderedSemiring variable [OrderedSemiring α] {a b c d : α} -- see Note [lower instance priority] instance (priority := 100) OrderedSemiring.zeroLEOneClass : ZeroLEOneClass α := { ‹OrderedSemiring α› with } -- see Note [lower instance priority] instance (priority := 200) OrderedSemiring.toPosMulMono : PosMulMono α := ⟨fun x _ _ h => OrderedSemiring.mul_le_mul_of_nonneg_left _ _ _ h x.2⟩ -- see Note [lower instance priority] instance (priority := 200) OrderedSemiring.toMulPosMono : MulPosMono α := ⟨fun x _ _ h => OrderedSemiring.mul_le_mul_of_nonneg_right _ _ _ h x.2⟩ end OrderedSemiring section OrderedRing variable [OrderedRing α] {a b c d : α} -- see Note [lower instance priority] instance (priority := 100) OrderedRing.toOrderedSemiring : OrderedSemiring α := { ‹OrderedRing α›, (Ring.toSemiring : Semiring α) with mul_le_mul_of_nonneg_left := fun a b c h hc => by simpa only [mul_sub, sub_nonneg] using OrderedRing.mul_nonneg _ _ hc (sub_nonneg.2 h), mul_le_mul_of_nonneg_right := fun a b c h hc => by simpa only [sub_mul, sub_nonneg] using OrderedRing.mul_nonneg _ _ (sub_nonneg.2 h) hc } end OrderedRing section OrderedCommRing variable [OrderedCommRing α] -- See note [lower instance priority] instance (priority := 100) OrderedCommRing.toOrderedCommSemiring : OrderedCommSemiring α := { OrderedRing.toOrderedSemiring, ‹OrderedCommRing α› with } end OrderedCommRing section StrictOrderedSemiring variable [StrictOrderedSemiring α] {a b c d : α} -- see Note [lower instance priority] instance (priority := 200) StrictOrderedSemiring.toPosMulStrictMono : PosMulStrictMono α := ⟨fun x _ _ h => StrictOrderedSemiring.mul_lt_mul_of_pos_left _ _ _ h x.prop⟩ -- see Note [lower instance priority] instance (priority := 200) StrictOrderedSemiring.toMulPosStrictMono : MulPosStrictMono α := ⟨fun x _ _ h => StrictOrderedSemiring.mul_lt_mul_of_pos_right _ _ _ h x.prop⟩ -- See note [reducible non-instances] /-- A choice-free version of `StrictOrderedSemiring.toOrderedSemiring` to avoid using choice in basic `Nat` lemmas. -/ abbrev StrictOrderedSemiring.toOrderedSemiring' [@DecidableRel α (· ≤ ·)] : OrderedSemiring α := { ‹StrictOrderedSemiring α› with mul_le_mul_of_nonneg_left := fun a b c hab hc => by obtain rfl | hab := Decidable.eq_or_lt_of_le hab · rfl obtain rfl | hc := Decidable.eq_or_lt_of_le hc · simp · exact (mul_lt_mul_of_pos_left hab hc).le, mul_le_mul_of_nonneg_right := fun a b c hab hc => by obtain rfl | hab := Decidable.eq_or_lt_of_le hab · rfl obtain rfl | hc := Decidable.eq_or_lt_of_le hc · simp · exact (mul_lt_mul_of_pos_right hab hc).le } -- see Note [lower instance priority] instance (priority := 100) StrictOrderedSemiring.toOrderedSemiring : OrderedSemiring α := { ‹StrictOrderedSemiring α› with mul_le_mul_of_nonneg_left := fun _ _ _ => letI := @StrictOrderedSemiring.toOrderedSemiring' α _ (Classical.decRel _) mul_le_mul_of_nonneg_left, mul_le_mul_of_nonneg_right := fun _ _ _ => letI := @StrictOrderedSemiring.toOrderedSemiring' α _ (Classical.decRel _) mul_le_mul_of_nonneg_right } -- see Note [lower instance priority] instance (priority := 100) StrictOrderedSemiring.toCharZero [StrictOrderedSemiring α] : CharZero α where cast_injective := (strictMono_nat_of_lt_succ fun n ↦ by rw [Nat.cast_succ]; apply lt_add_one).injective -- see Note [lower instance priority] instance (priority := 100) StrictOrderedSemiring.toNoMaxOrder : NoMaxOrder α := ⟨fun a => ⟨a + 1, lt_add_of_pos_right _ one_pos⟩⟩ end StrictOrderedSemiring section StrictOrderedCommSemiring variable [StrictOrderedCommSemiring α] -- See note [reducible non-instances] /-- A choice-free version of `StrictOrderedCommSemiring.toOrderedCommSemiring'` to avoid using choice in basic `Nat` lemmas. -/ abbrev StrictOrderedCommSemiring.toOrderedCommSemiring' [@DecidableRel α (· ≤ ·)] : OrderedCommSemiring α := { ‹StrictOrderedCommSemiring α›, StrictOrderedSemiring.toOrderedSemiring' with } -- see Note [lower instance priority] instance (priority := 100) StrictOrderedCommSemiring.toOrderedCommSemiring : OrderedCommSemiring α := { ‹StrictOrderedCommSemiring α›, StrictOrderedSemiring.toOrderedSemiring with } end StrictOrderedCommSemiring section StrictOrderedRing variable [StrictOrderedRing α] {a b c : α} -- see Note [lower instance priority] instance (priority := 100) StrictOrderedRing.toStrictOrderedSemiring : StrictOrderedSemiring α := { ‹StrictOrderedRing α›, (Ring.toSemiring : Semiring α) with le_of_add_le_add_left := @le_of_add_le_add_left α _ _ _, mul_lt_mul_of_pos_left := fun a b c h hc => by simpa only [mul_sub, sub_pos] using StrictOrderedRing.mul_pos _ _ hc (sub_pos.2 h), mul_lt_mul_of_pos_right := fun a b c h hc => by simpa only [sub_mul, sub_pos] using StrictOrderedRing.mul_pos _ _ (sub_pos.2 h) hc } -- See note [reducible non-instances] /-- A choice-free version of `StrictOrderedRing.toOrderedRing` to avoid using choice in basic `Int` lemmas. -/ abbrev StrictOrderedRing.toOrderedRing' [@DecidableRel α (· ≤ ·)] : OrderedRing α := { ‹StrictOrderedRing α›, (Ring.toSemiring : Semiring α) with mul_nonneg := fun a b ha hb => by obtain ha | ha := Decidable.eq_or_lt_of_le ha · rw [← ha, zero_mul] obtain hb | hb := Decidable.eq_or_lt_of_le hb · rw [← hb, mul_zero] · exact (StrictOrderedRing.mul_pos _ _ ha hb).le } -- see Note [lower instance priority] instance (priority := 100) StrictOrderedRing.toOrderedRing : OrderedRing α where __ := ‹StrictOrderedRing α› mul_nonneg := fun _ _ => mul_nonneg end StrictOrderedRing section StrictOrderedCommRing variable [StrictOrderedCommRing α] -- See note [reducible non-instances] /-- A choice-free version of `StrictOrderedCommRing.toOrderedCommRing` to avoid using choice in basic `Int` lemmas. -/ abbrev StrictOrderedCommRing.toOrderedCommRing' [@DecidableRel α (· ≤ ·)] : OrderedCommRing α := { ‹StrictOrderedCommRing α›, StrictOrderedRing.toOrderedRing' with } -- See note [lower instance priority] instance (priority := 100) StrictOrderedCommRing.toStrictOrderedCommSemiring : StrictOrderedCommSemiring α := { ‹StrictOrderedCommRing α›, StrictOrderedRing.toStrictOrderedSemiring with } -- See note [lower instance priority] instance (priority := 100) StrictOrderedCommRing.toOrderedCommRing : OrderedCommRing α := { ‹StrictOrderedCommRing α›, StrictOrderedRing.toOrderedRing with } end StrictOrderedCommRing section LinearOrderedSemiring variable [LinearOrderedSemiring α] {a b c d : α} -- see Note [lower instance priority] instance (priority := 200) LinearOrderedSemiring.toPosMulReflectLT : PosMulReflectLT α := ⟨fun a _ _ => (monotone_mul_left_of_nonneg a.2).reflect_lt⟩ -- see Note [lower instance priority] instance (priority := 200) LinearOrderedSemiring.toMulPosReflectLT : MulPosReflectLT α := ⟨fun a _ _ => (monotone_mul_right_of_nonneg a.2).reflect_lt⟩ attribute [local instance] LinearOrderedSemiring.decidableLE LinearOrderedSemiring.decidableLT variable [ExistsAddOfLE α] -- See note [lower instance priority] instance (priority := 100) LinearOrderedSemiring.noZeroDivisors : NoZeroDivisors α where eq_zero_or_eq_zero_of_mul_eq_zero {a b} hab := by contrapose! hab obtain ha | ha := hab.1.lt_or_lt <;> obtain hb | hb := hab.2.lt_or_lt exacts [(mul_pos_of_neg_of_neg ha hb).ne', (mul_neg_of_neg_of_pos ha hb).ne, (mul_neg_of_pos_of_neg ha hb).ne, (mul_pos ha hb).ne'] -- Note that we can't use `NoZeroDivisors.to_isDomain` since we are merely in a semiring. -- See note [lower instance priority] instance (priority := 100) LinearOrderedRing.isDomain : IsDomain α where mul_left_cancel_of_ne_zero {a b c} ha h := by obtain ha | ha := ha.lt_or_lt exacts [(strictAnti_mul_left ha).injective h, (strictMono_mul_left_of_pos ha).injective h] mul_right_cancel_of_ne_zero {b a c} ha h := by obtain ha | ha := ha.lt_or_lt exacts [(strictAnti_mul_right ha).injective h, (strictMono_mul_right_of_pos ha).injective h] end LinearOrderedSemiring section LinearOrderedCommSemiring variable [LinearOrderedCommSemiring α] {a b c d : α} -- See note [lower instance priority] instance (priority := 100) LinearOrderedCommSemiring.toLinearOrderedCancelAddCommMonoid : LinearOrderedCancelAddCommMonoid α where __ := ‹LinearOrderedCommSemiring α› end LinearOrderedCommSemiring section LinearOrderedRing variable [LinearOrderedRing α] {a b c : α} attribute [local instance] LinearOrderedRing.decidableLE LinearOrderedRing.decidableLT -- see Note [lower instance priority] instance (priority := 100) LinearOrderedRing.toLinearOrderedSemiring : LinearOrderedSemiring α := { ‹LinearOrderedRing α›, StrictOrderedRing.toStrictOrderedSemiring with } -- see Note [lower instance priority] instance (priority := 100) LinearOrderedRing.toLinearOrderedAddCommGroup : LinearOrderedAddCommGroup α where __ := ‹LinearOrderedRing α› end LinearOrderedRing -- see Note [lower instance priority] instance (priority := 100) LinearOrderedCommRing.toStrictOrderedCommRing [d : LinearOrderedCommRing α] : StrictOrderedCommRing α := { d with } -- see Note [lower instance priority] instance (priority := 100) LinearOrderedCommRing.toLinearOrderedCommSemiring [d : LinearOrderedCommRing α] : LinearOrderedCommSemiring α := { d, LinearOrderedRing.toLinearOrderedSemiring with } assert_not_exists MonoidHom
Algebra\Order\Ring\Finset.lean
/- Copyright (c) 2022 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import Mathlib.Algebra.Order.Ring.Defs import Mathlib.Data.Finset.Lattice /-! # Algebraic properties of finitary supremum -/ namespace Finset variable {ι R : Type*} [LinearOrderedSemiring R] {a b : ι → R} theorem sup_mul_le_mul_sup_of_nonneg [OrderBot R] (s : Finset ι) (ha : ∀ i ∈ s, 0 ≤ a i) (hb : ∀ i ∈ s, 0 ≤ b i) : s.sup (a * b) ≤ s.sup a * s.sup b := Finset.sup_le fun _i hi ↦ mul_le_mul (le_sup hi) (le_sup hi) (hb _ hi) ((ha _ hi).trans <| le_sup hi) theorem mul_inf_le_inf_mul_of_nonneg [OrderTop R] (s : Finset ι) (ha : ∀ i ∈ s, 0 ≤ a i) (hb : ∀ i ∈ s, 0 ≤ b i) : s.inf a * s.inf b ≤ s.inf (a * b) := Finset.le_inf fun i hi ↦ mul_le_mul (inf_le hi) (inf_le hi) (Finset.le_inf hb) (ha i hi) theorem sup'_mul_le_mul_sup'_of_nonneg (s : Finset ι) (H : s.Nonempty) (ha : ∀ i ∈ s, 0 ≤ a i) (hb : ∀ i ∈ s, 0 ≤ b i) : s.sup' H (a * b) ≤ s.sup' H a * s.sup' H b := (sup'_le _ _) fun _i hi ↦ mul_le_mul (le_sup' _ hi) (le_sup' _ hi) (hb _ hi) ((ha _ hi).trans <| le_sup' _ hi) theorem inf'_mul_le_mul_inf'_of_nonneg (s : Finset ι) (H : s.Nonempty) (ha : ∀ i ∈ s, 0 ≤ a i) (hb : ∀ i ∈ s, 0 ≤ b i) : s.inf' H a * s.inf' H b ≤ s.inf' H (a * b) := (le_inf' _ _) fun _i hi ↦ mul_le_mul (inf'_le _ hi) (inf'_le _ hi) (le_inf' _ _ hb) (ha _ hi) end Finset
Algebra\Order\Ring\InjSurj.lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro -/ import Mathlib.Algebra.Order.Group.InjSurj import Mathlib.Algebra.Order.Ring.Defs import Mathlib.Algebra.Ring.InjSurj /-! # Pulling back ordered rings along injective maps -/ open Function variable {α β : Type*} namespace Function.Injective variable [Zero β] [One β] [Add β] [Mul β] [Neg β] [Sub β] [SMul ℕ β] [SMul ℤ β] [Pow β ℕ] [NatCast β] [IntCast β] [Sup β] [Inf β] (f : β → α) (hf : Injective f) /-- Pullback an `OrderedSemiring` under an injective map. -/ -- See note [reducible non-instances] protected abbrev orderedSemiring [OrderedSemiring α] (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) (nsmul : ∀ (n : ℕ) (x), f (n • x) = n • f x) (npow : ∀ (x) (n : ℕ), f (x ^ n) = f x ^ n) (natCast : ∀ n : ℕ, f n = n) : OrderedSemiring β where toSemiring := hf.semiring f zero one add mul nsmul npow natCast __ := hf.orderedAddCommMonoid f zero add (swap nsmul) zero_le_one := show f 0 ≤ f 1 by simp only [zero, one, zero_le_one] mul_le_mul_of_nonneg_left a b c h hc := show f (c * a) ≤ f (c * b) by rw [mul, mul]; refine mul_le_mul_of_nonneg_left h ?_; rwa [← zero] mul_le_mul_of_nonneg_right a b c h hc := show f (a * c) ≤ f (b * c) by rw [mul, mul]; refine mul_le_mul_of_nonneg_right h ?_; rwa [← zero] /-- Pullback an `OrderedCommSemiring` under an injective map. -/ -- See note [reducible non-instances] protected abbrev orderedCommSemiring [OrderedCommSemiring α] (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) (nsmul : ∀ (n : ℕ) (x), f (n • x) = n • f x) (npow : ∀ (x) (n : ℕ), f (x ^ n) = f x ^ n) (natCast : ∀ n : ℕ, f n = n) : OrderedCommSemiring β where toOrderedSemiring := hf.orderedSemiring f zero one add mul nsmul npow natCast __ := hf.commSemiring f zero one add mul nsmul npow natCast /-- Pullback an `OrderedRing` under an injective map. -/ -- See note [reducible non-instances] protected abbrev orderedRing [OrderedRing α] (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) (neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y) (nsmul : ∀ (n : ℕ) (x), f (n • x) = n • f x) (zsmul : ∀ (n : ℤ) (x), f (n • x) = n • f x) (npow : ∀ (x) (n : ℕ), f (x ^ n) = f x ^ n) (natCast : ∀ n : ℕ, f n = n) (intCast : ∀ n : ℤ, f n = n) : OrderedRing β where toRing := hf.ring f zero one add mul neg sub nsmul zsmul npow natCast intCast __ := hf.orderedAddCommGroup f zero add neg sub (swap nsmul) (swap zsmul) __ := hf.orderedSemiring f zero one add mul nsmul npow natCast mul_nonneg a b ha hb := show f 0 ≤ f (a * b) by rw [zero, mul]; apply mul_nonneg <;> rwa [← zero] /-- Pullback an `OrderedCommRing` under an injective map. -/ -- See note [reducible non-instances] protected abbrev orderedCommRing [OrderedCommRing α] (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) (neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y) (nsmul : ∀ (n : ℕ) (x), f (n • x) = n • f x) (zsmul : ∀ (n : ℤ) (x), f (n • x) = n • f x) (npow : ∀ (x) (n : ℕ), f (x ^ n) = f x ^ n) (natCast : ∀ n : ℕ, f n = n) (intCast : ∀ n : ℤ, f n = n) : OrderedCommRing β where toOrderedRing := hf.orderedRing f zero one add mul neg sub nsmul zsmul npow natCast intCast __ := hf.commRing f zero one add mul neg sub nsmul zsmul npow natCast intCast /-- Pullback a `StrictOrderedSemiring` under an injective map. -/ -- See note [reducible non-instances] protected abbrev strictOrderedSemiring [StrictOrderedSemiring α] (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) (nsmul : ∀ (n : ℕ) (x), f (n • x) = n • f x) (npow : ∀ (x) (n : ℕ), f (x ^ n) = f x ^ n) (natCast : ∀ n : ℕ, f n = n) : StrictOrderedSemiring β where toSemiring := hf.semiring f zero one add mul nsmul npow natCast __ := hf.orderedCancelAddCommMonoid f zero add (swap nsmul) __ := pullback_nonzero f zero one __ := hf.orderedSemiring f zero one add mul nsmul npow natCast mul_lt_mul_of_pos_left a b c h hc := show f (c * a) < f (c * b) by simpa only [mul, zero] using mul_lt_mul_of_pos_left ‹f a < f b› (by rwa [← zero]) mul_lt_mul_of_pos_right a b c h hc := show f (a * c) < f (b * c) by simpa only [mul, zero] using mul_lt_mul_of_pos_right ‹f a < f b› (by rwa [← zero]) /-- Pullback a `strictOrderedCommSemiring` under an injective map. -/ -- See note [reducible non-instances] protected abbrev strictOrderedCommSemiring [StrictOrderedCommSemiring α] (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) (nsmul : ∀ (n : ℕ) (x), f (n • x) = n • f x) (npow : ∀ (x) (n : ℕ), f (x ^ n) = f x ^ n) (natCast : ∀ n : ℕ, f n = n) : StrictOrderedCommSemiring β where toStrictOrderedSemiring := hf.strictOrderedSemiring f zero one add mul nsmul npow natCast __ := hf.commSemiring f zero one add mul nsmul npow natCast /-- Pullback a `StrictOrderedRing` under an injective map. -/ -- See note [reducible non-instances] protected abbrev strictOrderedRing [StrictOrderedRing α] (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) (neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y) (nsmul : ∀ (n : ℕ) (x), f (n • x) = n • f x) (zsmul : ∀ (n : ℤ) (x), f (n • x) = n • f x) (npow : ∀ (x) (n : ℕ), f (x ^ n) = f x ^ n) (natCast : ∀ n : ℕ, f n = n) (intCast : ∀ n : ℤ, f n = n) : StrictOrderedRing β where toRing := hf.ring f zero one add mul neg sub nsmul zsmul npow natCast intCast __ := hf.orderedAddCommGroup f zero add neg sub (swap nsmul) (swap zsmul) __ := hf.strictOrderedSemiring f zero one add mul nsmul npow natCast mul_pos a b ha hb := show f 0 < f (a * b) by rw [zero, mul]; apply mul_pos <;> rwa [← zero] /-- Pullback a `StrictOrderedCommRing` under an injective map. -/ -- See note [reducible non-instances] protected abbrev strictOrderedCommRing [StrictOrderedCommRing α] (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) (neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y) (nsmul : ∀ (n : ℕ) (x), f (n • x) = n • f x) (zsmul : ∀ (n : ℤ) (x), f (n • x) = n • f x) (npow : ∀ (x) (n : ℕ), f (x ^ n) = f x ^ n) (natCast : ∀ n : ℕ, f n = n) (intCast : ∀ n : ℤ, f n = n) : StrictOrderedCommRing β where toStrictOrderedRing := hf.strictOrderedRing f zero one add mul neg sub nsmul zsmul npow natCast intCast __ := hf.commRing f zero one add mul neg sub nsmul zsmul npow natCast intCast /-- Pullback a `LinearOrderedSemiring` under an injective map. -/ -- See note [reducible non-instances] protected abbrev linearOrderedSemiring [LinearOrderedSemiring α] (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) (nsmul : ∀ (n : ℕ) (x), f (n • x) = n • f x) (npow : ∀ (x) (n : ℕ), f (x ^ n) = f x ^ n) (natCast : ∀ n : ℕ, f n = n) (sup : ∀ x y, f (x ⊔ y) = max (f x) (f y)) (inf : ∀ x y, f (x ⊓ y) = min (f x) (f y)) : LinearOrderedSemiring β where toStrictOrderedSemiring := hf.strictOrderedSemiring f zero one add mul nsmul npow natCast __ := hf.linearOrderedAddCommMonoid f zero add (swap nsmul) sup inf /-- Pullback a `LinearOrderedSemiring` under an injective map. -/ -- -- See note [reducible non-instances] protected abbrev linearOrderedCommSemiring [LinearOrderedCommSemiring α] (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) (nsmul : ∀ (n : ℕ) (x), f (n • x) = n • f x) (npow : ∀ (x) (n : ℕ), f (x ^ n) = f x ^ n) (natCast : ∀ n : ℕ, f n = n) (hsup : ∀ x y, f (x ⊔ y) = max (f x) (f y)) (hinf : ∀ x y, f (x ⊓ y) = min (f x) (f y)) : LinearOrderedCommSemiring β where toStrictOrderedCommSemiring := hf.strictOrderedCommSemiring f zero one add mul nsmul npow natCast __ := hf.linearOrderedSemiring f zero one add mul nsmul npow natCast hsup hinf /-- Pullback a `LinearOrderedRing` under an injective map. -/ -- See note [reducible non-instances] abbrev linearOrderedRing [LinearOrderedRing α] (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) (neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y) (nsmul : ∀ (n : ℕ) (x), f (n • x) = n • f x) (zsmul : ∀ (n : ℤ) (x), f (n • x) = n • f x) (npow : ∀ (x) (n : ℕ), f (x ^ n) = f x ^ n) (natCast : ∀ n : ℕ, f n = n) (intCast : ∀ n : ℤ, f n = n) (hsup : ∀ x y, f (x ⊔ y) = max (f x) (f y)) (hinf : ∀ x y, f (x ⊓ y) = min (f x) (f y)) : LinearOrderedRing β where toStrictOrderedRing := hf.strictOrderedRing f zero one add mul neg sub nsmul zsmul npow natCast intCast __ := LinearOrder.lift f hf hsup hinf /-- Pullback a `LinearOrderedCommRing` under an injective map. -/ -- See note [reducible non-instances] protected abbrev linearOrderedCommRing [LinearOrderedCommRing α] (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) (neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y) (nsmul : ∀ (n : ℕ) (x), f (n • x) = n • f x) (zsmul : ∀ (n : ℤ) (x), f (n • x) = n • f x) (npow : ∀ (x) (n : ℕ), f (x ^ n) = f x ^ n) (natCast : ∀ n : ℕ, f n = n) (intCast : ∀ n : ℤ, f n = n) (sup : ∀ x y, f (x ⊔ y) = max (f x) (f y)) (inf : ∀ x y, f (x ⊓ y) = min (f x) (f y)) : LinearOrderedCommRing β where toLinearOrderedRing := hf.linearOrderedRing f zero one add mul neg sub nsmul zsmul npow natCast intCast sup inf __ := hf.commMonoid f one mul npow end Function.Injective
Algebra\Order\Ring\Int.lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad -/ import Mathlib.Algebra.Ring.Int import Mathlib.Algebra.Order.Group.Int import Mathlib.Algebra.Order.Ring.Defs import Mathlib.Data.Set.Basic /-! # The integers form a linear ordered ring This file contains: * instances on `ℤ`. The stronger one is `Int.instLinearOrderedCommRing`. * basic lemmas about integers that involve order properties. ## Recursors * `Int.rec`: Sign disjunction. Something is true/defined on `ℤ` if it's true/defined for nonnegative and for negative values. (Defined in core Lean 3) * `Int.inductionOn`: Simple growing induction on positive numbers, plus simple decreasing induction on negative numbers. Note that this recursor is currently only `Prop`-valued. * `Int.inductionOn'`: Simple growing induction for numbers greater than `b`, plus simple decreasing induction on numbers less than `b`. -/ -- We should need only a minimal development of sets in order to get here. assert_not_exists Set.Subsingleton open Function Nat namespace Int instance instLinearOrderedCommRing : LinearOrderedCommRing ℤ where __ := instCommRing __ := instLinearOrder add_le_add_left := @Int.add_le_add_left mul_pos := @Int.mul_pos zero_le_one := le_of_lt Int.zero_lt_one /-! ### Extra instances to short-circuit type class resolution These also prevent non-computable instances being used to construct these instances non-computably. -/ instance instOrderedCommRing : OrderedCommRing ℤ := StrictOrderedCommRing.toOrderedCommRing' instance instOrderedRing : OrderedRing ℤ := StrictOrderedRing.toOrderedRing' /-! ### Miscellaneous lemmas -/ lemma isCompl_even_odd : IsCompl { n : ℤ | Even n } { n | Odd n } := by simp [← Set.compl_setOf, isCompl_compl] lemma _root_.Nat.cast_natAbs {α : Type*} [AddGroupWithOne α] (n : ℤ) : (n.natAbs : α) = |n| := by rw [← natCast_natAbs, Int.cast_natCast] /-- Note this holds in marginally more generality than `Int.cast_mul` -/ lemma cast_mul_eq_zsmul_cast {α : Type*} [AddCommGroupWithOne α] : ∀ 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] lemma two_le_iff_pos_of_even {m : ℤ} (even : Even m) : 2 ≤ m ↔ 0 < m := le_iff_pos_of_dvd (by decide) even.two_dvd lemma add_two_le_iff_lt_of_even_sub {m n : ℤ} (even : Even (n - m)) : m + 2 ≤ n ↔ m < n := by rw [add_comm]; exact le_add_iff_lt_of_dvd_sub (by decide) even.two_dvd end Int
Algebra\Order\Ring\Nat.lean
/- Copyright (c) 2014 Floris van Doorn (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Leonardo de Moura, Jeremy Avigad, Mario Carneiro -/ import Mathlib.Algebra.Order.Group.Nat import Mathlib.Algebra.Order.GroupWithZero.Canonical import Mathlib.Algebra.Order.Ring.Canonical import Mathlib.Algebra.Ring.Nat import Mathlib.Data.Set.Basic import Mathlib.Init.Data.Nat.Lemmas /-! # The natural numbers form an ordered semiring This file contains the commutative linear orderded semiring instance on the natural numbers. See note [foundational algebra order theory]. -/ namespace Nat /-! ### Instances -/ instance instLinearOrderedCommSemiring : LinearOrderedCommSemiring ℕ where __ := instCommSemiring __ := instLinearOrder add_le_add_left := @Nat.add_le_add_left le_of_add_le_add_left := @Nat.le_of_add_le_add_left zero_le_one := Nat.le_of_lt (Nat.zero_lt_succ 0) mul_lt_mul_of_pos_left := @Nat.mul_lt_mul_of_pos_left mul_lt_mul_of_pos_right := @Nat.mul_lt_mul_of_pos_right exists_pair_ne := ⟨0, 1, ne_of_lt Nat.zero_lt_one⟩ instance instLinearOrderedCommMonoidWithZero : LinearOrderedCommMonoidWithZero ℕ where __ := instLinearOrderedCommSemiring __ : CommMonoidWithZero ℕ := inferInstance mul_le_mul_left _ _ h c := Nat.mul_le_mul_left c h instance instCanonicallyOrderedCommSemiring : CanonicallyOrderedCommSemiring ℕ where __ := instLinearOrderedCommSemiring exists_add_of_le h := (Nat.le.dest h).imp fun _ => Eq.symm le_self_add := Nat.le_add_right eq_zero_or_eq_zero_of_mul_eq_zero := Nat.eq_zero_of_mul_eq_zero /-! ### Extra instances to short-circuit type class resolution These also prevent non-computable instances being used to construct these instances non-computably. -/ instance instLinearOrderedSemiring : LinearOrderedSemiring ℕ := inferInstance instance instStrictOrderedSemiring : StrictOrderedSemiring ℕ := inferInstance instance instStrictOrderedCommSemiring : StrictOrderedCommSemiring ℕ := inferInstance instance instOrderedSemiring : OrderedSemiring ℕ := StrictOrderedSemiring.toOrderedSemiring' instance instOrderedCommSemiring : OrderedCommSemiring ℕ := StrictOrderedCommSemiring.toOrderedCommSemiring' /-! ### Miscellaneous lemmas -/ lemma isCompl_even_odd : IsCompl { n : ℕ | Even n } { n | Odd n } := by simp only [← Set.compl_setOf, isCompl_compl, odd_iff_not_even] end Nat
Algebra\Order\Ring\Pow.lean
/- Copyright (c) 2019 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import Mathlib.Data.Nat.Cast.Commute import Mathlib.Data.Nat.Cast.Order.Ring /-! # Bernoulli's inequality -/ variable {R : Type*} section OrderedSemiring variable [OrderedSemiring R] {a : R} /-- **Bernoulli's inequality**. This version works for semirings but requires additional hypotheses `0 ≤ a * a` and `0 ≤ (1 + a) * (1 + a)`. -/ lemma one_add_mul_le_pow' (Hsq : 0 ≤ a * a) (Hsq' : 0 ≤ (1 + a) * (1 + a)) (H : 0 ≤ 2 + a) : ∀ n : ℕ, 1 + n * a ≤ (1 + a) ^ n | 0 => by simp | 1 => by simp | n + 2 => have : 0 ≤ n * (a * a * (2 + a)) + a * a := add_nonneg (mul_nonneg n.cast_nonneg (mul_nonneg Hsq H)) Hsq calc _ ≤ 1 + ↑(n + 2) * a + (n * (a * a * (2 + a)) + a * a) := le_add_of_nonneg_right this _ = (1 + a) * (1 + a) * (1 + n * a) := by simp only [Nat.cast_add, add_mul, mul_add, one_mul, mul_one, ← one_add_one_eq_two, Nat.cast_one, add_assoc, add_right_inj] simp only [← add_assoc, add_comm _ (↑n * a)] simp only [add_assoc, (n.cast_commute (_ : R)).left_comm] simp only [add_comm, add_left_comm] _ ≤ (1 + a) * (1 + a) * (1 + a) ^ n := mul_le_mul_of_nonneg_left (one_add_mul_le_pow' Hsq Hsq' H _) Hsq' _ = (1 + a) ^ (n + 2) := by simp only [pow_succ', mul_assoc] end OrderedSemiring section LinearOrderedRing variable [LinearOrderedRing R] {a : R} {n : ℕ} /-- **Bernoulli's inequality** for `n : ℕ`, `-2 ≤ a`. -/ lemma one_add_mul_le_pow (H : -2 ≤ a) (n : ℕ) : 1 + n * a ≤ (1 + a) ^ n := one_add_mul_le_pow' (mul_self_nonneg _) (mul_self_nonneg _) (neg_le_iff_add_nonneg'.1 H) _ /-- **Bernoulli's inequality** reformulated to estimate `a^n`. -/ lemma one_add_mul_sub_le_pow (H : -1 ≤ a) (n : ℕ) : 1 + n * (a - 1) ≤ a ^ n := by have : -2 ≤ a - 1 := by rwa [← one_add_one_eq_two, neg_add, ← sub_eq_add_neg, sub_le_sub_iff_right] simpa only [add_sub_cancel] using one_add_mul_le_pow this n end LinearOrderedRing
Algebra\Order\Ring\Prod.lean
/- Copyright (c) 2022 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import Mathlib.Algebra.Order.Group.Prod import Mathlib.Algebra.Order.Ring.Defs import Mathlib.Algebra.Ring.Prod /-! # Products of ordered rings -/ variable {α β : Type*} instance [OrderedSemiring α] [OrderedSemiring β] : OrderedSemiring (α × β) := { inferInstanceAs (Semiring (α × β)), inferInstanceAs (OrderedAddCommMonoid (α × β)) with zero_le_one := ⟨zero_le_one, zero_le_one⟩ mul_le_mul_of_nonneg_left := fun _ _ _ hab hc => ⟨mul_le_mul_of_nonneg_left hab.1 hc.1, mul_le_mul_of_nonneg_left hab.2 hc.2⟩ mul_le_mul_of_nonneg_right := fun _ _ _ hab hc => ⟨mul_le_mul_of_nonneg_right hab.1 hc.1, mul_le_mul_of_nonneg_right hab.2 hc.2⟩ } instance [OrderedCommSemiring α] [OrderedCommSemiring β] : OrderedCommSemiring (α × β) := { inferInstanceAs (OrderedSemiring (α × β)), inferInstanceAs (CommSemiring (α × β)) with } -- Porting note: compile fails with `inferInstanceAs (OrderedSemiring (α × β))` instance [OrderedRing α] [OrderedRing β] : OrderedRing (α × β) := { inferInstanceAs (Ring (α × β)), inferInstanceAs (OrderedAddCommGroup (α × β)) with zero_le_one := ⟨zero_le_one, zero_le_one⟩ mul_nonneg := fun _ _ ha hb => ⟨mul_nonneg ha.1 hb.1, mul_nonneg ha.2 hb.2⟩ } instance [OrderedCommRing α] [OrderedCommRing β] : OrderedCommRing (α × β) := { inferInstanceAs (OrderedRing (α × β)), inferInstanceAs (CommRing (α × β)) with }
Algebra\Order\Ring\Rat.lean
/- Copyright (c) 2019 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Ring.Int import Mathlib.Algebra.Ring.Rat /-! # The rational numbers form a linear ordered field This file constructs the order on `ℚ` and proves that `ℚ` is a discrete, linearly ordered commutative ring. `ℚ` is in fact a linearly ordered field, but this fact is located in `Data.Rat.Field` instead of here because we need the order on `ℚ` to define `ℚ≥0`, which we itself need to define `Field`. ## Tags rat, rationals, field, ℚ, numerator, denominator, num, denom, order, ordering -/ assert_not_exists Field assert_not_exists Finset assert_not_exists Set.Icc assert_not_exists GaloisConnection namespace Rat variable {a b c p q : ℚ} @[simp] lemma divInt_nonneg_iff_of_pos_right {a b : ℤ} (hb : 0 < b) : 0 ≤ a /. b ↔ 0 ≤ a := by cases' hab : a /. b with n d hd hnd rw [mk'_eq_divInt, divInt_eq_iff hb.ne' (mod_cast hd)] at hab rw [← num_nonneg, ← mul_nonneg_iff_of_pos_right hb, ← hab, mul_nonneg_iff_of_pos_right (mod_cast Nat.pos_of_ne_zero hd)] @[simp] lemma divInt_nonneg {a b : ℤ} (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a /. b := by obtain rfl | hb := hb.eq_or_lt · simp rfl rwa [divInt_nonneg_iff_of_pos_right hb] @[simp] lemma mkRat_nonneg {a : ℤ} (ha : 0 ≤ a) (b : ℕ) : 0 ≤ mkRat a b := by simpa using divInt_nonneg ha (Int.natCast_nonneg _) theorem ofScientific_nonneg (m : ℕ) (s : Bool) (e : ℕ) : 0 ≤ Rat.ofScientific m s e := by rw [Rat.ofScientific] cases s · rw [if_neg (by decide)] refine num_nonneg.mp ?_ rw [num_natCast] exact Int.natCast_nonneg _ · rw [if_pos rfl, normalize_eq_mkRat] exact Rat.mkRat_nonneg (Int.natCast_nonneg _) _ instance _root_.NNRatCast.toOfScientific {K} [NNRatCast K] : OfScientific K where ofScientific (m : ℕ) (b : Bool) (d : ℕ) := NNRat.cast ⟨Rat.ofScientific m b d, ofScientific_nonneg m b d⟩ /-- Casting a scientific literal via `ℚ≥0` is the same as casting directly. -/ @[simp, norm_cast] theorem _root_.NNRat.cast_ofScientific {K} [NNRatCast K] (m : ℕ) (s : Bool) (e : ℕ) : (OfScientific.ofScientific m s e : ℚ≥0) = (OfScientific.ofScientific m s e : K) := rfl protected lemma add_nonneg : 0 ≤ a → 0 ≤ b → 0 ≤ a + b := numDenCasesOn' a fun n₁ d₁ h₁ ↦ numDenCasesOn' b fun n₂ d₂ h₂ ↦ by have d₁0 : 0 < (d₁ : ℤ) := mod_cast Nat.pos_of_ne_zero h₁ have d₂0 : 0 < (d₂ : ℤ) := mod_cast Nat.pos_of_ne_zero h₂ simp only [d₁0, d₂0, h₁, h₂, mul_pos, divInt_nonneg_iff_of_pos_right, divInt_add_divInt, Ne, Nat.cast_eq_zero, not_false_iff] intro n₁0 n₂0 apply add_nonneg <;> apply mul_nonneg <;> · first |assumption|apply Int.ofNat_zero_le protected lemma mul_nonneg : 0 ≤ a → 0 ≤ b → 0 ≤ a * b := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun n₂ d₂ h₂ => by have d₁0 : 0 < (d₁ : ℤ) := mod_cast Nat.pos_of_ne_zero h₁ have d₂0 : 0 < (d₂ : ℤ) := mod_cast Nat.pos_of_ne_zero h₂ simp only [d₁0, d₂0, mul_pos, divInt_nonneg_iff_of_pos_right, divInt_mul_divInt _ _ d₁0.ne' d₂0.ne'] apply mul_nonneg -- Porting note (#11215): TODO can this be shortened? protected theorem le_iff_sub_nonneg (a b : ℚ) : a ≤ b ↔ 0 ≤ b - a := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le, ← num_nonneg] split_ifs with h h' · rw [Rat.sub_def] simp only [false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' · rw [sub_neg] apply lt_of_lt_of_le · apply mul_neg_of_neg_of_pos h.1 rwa [Int.natCast_pos, Nat.pos_iff_ne_zero] · apply mul_nonneg h.2 (Int.natCast_nonneg _) · simp only [Int.natCast_pos, Nat.pos_iff_ne_zero] exact Nat.gcd_ne_zero_right (Nat.mul_ne_zero hb ha) · simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h'] · simp only [Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ · refine Int.ediv_nonneg ?_ (Int.natCast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' · apply H intro H' exact (hb.trans H').false.elim · obtain ha|ha := le_or_lt na 0 · apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Int.natCast_nonneg _) exact mul_nonneg hb.le (Int.natCast_nonneg _) · exact H (fun _ => ha) · rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Int.natCast_pos, Nat.pos_iff_ne_zero] exact Nat.gcd_ne_zero_right (Nat.mul_ne_zero hb ha) protected lemma divInt_le_divInt {a b c d : ℤ} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≤ c /. d ↔ a * d ≤ c * b := by rw [Rat.le_iff_sub_nonneg, ← sub_nonneg (α := ℤ)] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] protected lemma le_total : a ≤ b ∨ b ≤ a := by simpa only [← Rat.le_iff_sub_nonneg, neg_sub] using Rat.nonneg_total (b - a) protected theorem not_le {a b : ℚ} : ¬a ≤ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder ℚ where le_refl a := by rw [Rat.le_iff_sub_nonneg, ← num_nonneg]; simp le_trans a b c hab hbc := by rw [Rat.le_iff_sub_nonneg] at hab hbc have := Rat.add_nonneg hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rwa [Rat.le_iff_sub_nonneg] le_antisymm a b hab hba := by rw [Rat.le_iff_sub_nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this le_total _ _ := Rat.le_total decidableEq := inferInstance decidableLE := inferInstance decidableLT := inferInstance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp Rat.le_total.resolve_left] /-! ### Extra instances to short-circuit type class resolution These also prevent non-computable instances being used to construct these instances non-computably. -/ instance instDistribLattice : DistribLattice ℚ := inferInstance instance instLattice : Lattice ℚ := inferInstance instance instSemilatticeInf : SemilatticeInf ℚ := inferInstance instance instSemilatticeSup : SemilatticeSup ℚ := inferInstance instance instInf : Inf ℚ := inferInstance instance instSup : Sup ℚ := inferInstance instance instPartialOrder : PartialOrder ℚ := inferInstance instance instPreorder : Preorder ℚ := inferInstance /-! ### Miscellaneous lemmas -/ protected lemma le_def : p ≤ q ↔ p.num * q.den ≤ q.num * p.den := by rw [← num_divInt_den q, ← num_divInt_den p] conv_rhs => simp only [num_divInt_den] exact Rat.divInt_le_divInt (mod_cast p.pos) (mod_cast q.pos) protected lemma lt_def : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def] suffices p ≠ q ↔ p.num * q.den ≠ q.num * p.den by constructor <;> intro h · exact lt_iff_le_and_ne.mpr ⟨h.left, this.mp h.right⟩ · have tmp := lt_iff_le_and_ne.mp h exact ⟨tmp.left, this.mpr tmp.right⟩ exact not_iff_not.mpr eq_iff_mul_eq_mul protected theorem add_le_add_left {a b c : ℚ} : c + a ≤ c + b ↔ a ≤ b := by rw [Rat.le_iff_sub_nonneg, add_sub_add_left_eq_sub, ← Rat.le_iff_sub_nonneg] instance instLinearOrderedCommRing : LinearOrderedCommRing ℚ where __ := Rat.linearOrder __ := Rat.commRing zero_le_one := by decide add_le_add_left := fun a b ab c => Rat.add_le_add_left.2 ab mul_pos a b ha hb := (Rat.mul_nonneg ha.le hb.le).lt_of_ne' (mul_ne_zero ha.ne' hb.ne') -- Extra instances to short-circuit type class resolution instance : LinearOrderedRing ℚ := by infer_instance instance : OrderedRing ℚ := by infer_instance instance : LinearOrderedSemiring ℚ := by infer_instance instance : OrderedSemiring ℚ := by infer_instance instance : LinearOrderedAddCommGroup ℚ := by infer_instance instance : OrderedAddCommGroup ℚ := by infer_instance instance : OrderedCancelAddCommMonoid ℚ := by infer_instance instance : OrderedAddCommMonoid ℚ := by infer_instance @[simp] lemma num_nonpos {a : ℚ} : a.num ≤ 0 ↔ a ≤ 0 := by simpa using @num_nonneg (-a) @[simp] lemma num_pos {a : ℚ} : 0 < a.num ↔ 0 < a := lt_iff_lt_of_le_iff_le num_nonpos @[simp] lemma num_neg {a : ℚ} : a.num < 0 ↔ a < 0 := lt_iff_lt_of_le_iff_le num_nonneg @[deprecated (since := "2024-02-16")] alias num_nonneg_iff_zero_le := num_nonneg @[deprecated (since := "2024-02-16")] alias num_pos_iff_pos := num_pos theorem div_lt_div_iff_mul_lt_mul {a b c d : ℤ} (b_pos : 0 < b) (d_pos : 0 < d) : (a : ℚ) / b < c / d ↔ a * d < c * b := by simp only [lt_iff_le_not_le] apply and_congr · simp [div_def', Rat.divInt_le_divInt b_pos d_pos] · apply not_congr simp [div_def', Rat.divInt_le_divInt d_pos b_pos] theorem lt_one_iff_num_lt_denom {q : ℚ} : q < 1 ↔ q.num < q.den := by simp [Rat.lt_def] theorem abs_def (q : ℚ) : |q| = q.num.natAbs /. q.den := by rcases le_total q 0 with hq | hq · rw [abs_of_nonpos hq] rw [← num_divInt_den q, ← zero_divInt, Rat.divInt_le_divInt (mod_cast q.pos) zero_lt_one, mul_one, zero_mul] at hq rw [Int.ofNat_natAbs_of_nonpos hq, ← neg_def] · rw [abs_of_nonneg hq] rw [← num_divInt_den q, ← zero_divInt, Rat.divInt_le_divInt zero_lt_one (mod_cast q.pos), mul_one, zero_mul] at hq rw [Int.natAbs_of_nonneg hq, num_divInt_den] end Rat
Algebra\Order\Ring\Star.lean
/- Copyright (c) 2023 Jireh Loreaux. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jireh Loreaux -/ import Mathlib.Algebra.Order.Ring.Defs import Mathlib.Algebra.Star.Order /-! # Commutative star-ordered rings are ordered rings A noncommutative star-ordered ring is generally not an ordered ring. Indeed, in a star-ordered ring, nonnegative elements are self-adjoint, but the product of self-adjoint elements is self-adjoint if and only if they commute. Therefore, a necessary condition for a star-ordered ring to be an ordered ring is that all nonnegative elements commute. Consequently, if a star-ordered ring is spanned by it nonnegative elements (as is the case for C⋆-algebras) and it is also an ordered ring, then it is commutative. In this file we prove the converse: a *commutative* star-ordered ring is an ordered ring. -/ namespace StarOrderedRing /- This example shows that nonnegative elements in a ordered semiring which is also star-ordered must commute. We provide this only as an example as opposed to a lemma because we never expect the type class assumptions to be satisfied without a `CommSemiring` intance already in scope; not that it is impossible, only that it shouldn't occur in practice. -/ example {R : Type*} [OrderedSemiring R] [StarRing R] [StarOrderedRing R] {x y : R} (hx : 0 ≤ x) (hy : 0 ≤ y) : x * y = y * x := by rw [← IsSelfAdjoint.of_nonneg (mul_nonneg hy hx), star_mul, IsSelfAdjoint.of_nonneg hx, IsSelfAdjoint.of_nonneg hy] /- This will be implied by the instance below, we only prove it to avoid duplicating the argument in the instance below for `mul_le_mul_of_nonneg_right`. -/ private lemma mul_le_mul_of_nonneg_left {R : Type*} [CommSemiring R] [PartialOrder R] [StarRing R] [StarOrderedRing R] {a b c : R} (hab : a ≤ b) (hc : 0 ≤ c) : c * a ≤ c * b := by rw [StarOrderedRing.nonneg_iff] at hc induction hc using AddSubmonoid.closure_induction' with | mem _ h => obtain ⟨x, rfl⟩ := h simp_rw [mul_assoc, mul_comm x, ← mul_assoc] exact conjugate_le_conjugate hab x | one => simp | mul x hx y hy => simp only [← nonneg_iff, add_mul] at hx hy ⊢ apply add_le_add <;> aesop /-- A commutative star-ordered semiring is an ordered semiring. This is not registered as an instance because it introduces a type class loop between `CommSemiring` and `OrderedCommSemiring`, and it seem loops still cause issues sometimes. See note [reducible non-instances]. -/ abbrev toOrderedCommSemiring (R : Type*) [CommSemiring R] [PartialOrder R] [StarRing R] [StarOrderedRing R] : OrderedCommSemiring R where add_le_add_left _ _ := add_le_add_left zero_le_one := zero_le_one mul_comm := mul_comm mul_le_mul_of_nonneg_left _ _ _ := mul_le_mul_of_nonneg_left mul_le_mul_of_nonneg_right a b c := by simpa only [mul_comm _ c] using mul_le_mul_of_nonneg_left /-- A commutative star-ordered ring is an ordered ring. This is not registered as an instance because it introduces a type class loop between `CommSemiring` and `OrderedCommSemiring`, and it seem loops still cause issues sometimes. See note [reducible non-instances]. -/ abbrev toOrderedCommRing (R : Type*) [CommRing R] [PartialOrder R] [StarRing R] [StarOrderedRing R] : OrderedCommRing R where add_le_add_left _ _ := add_le_add_left zero_le_one := zero_le_one mul_comm := mul_comm mul_nonneg _ _ := let _ := toOrderedCommSemiring R; mul_nonneg end StarOrderedRing
Algebra\Order\Ring\Synonym.lean
/- Copyright (c) 2021 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov, Yaël Dillies -/ import Mathlib.Algebra.Order.Group.Synonym import Mathlib.Algebra.Ring.Defs /-! # Ring structure on the order type synonyms Transfer algebraic instances from `α` to `αᵒᵈ` and `Lex α`. -/ variable {α : Type*} /-! ### Order dual -/ instance [h : Distrib α] : Distrib αᵒᵈ := h instance [Mul α] [Add α] [h : LeftDistribClass α] : LeftDistribClass αᵒᵈ := h instance [Mul α] [Add α] [h : RightDistribClass α] : RightDistribClass αᵒᵈ := h instance [h : NonUnitalNonAssocSemiring α] : NonUnitalNonAssocSemiring αᵒᵈ := h instance [h : NonUnitalSemiring α] : NonUnitalSemiring αᵒᵈ := h instance [h : NonAssocSemiring α] : NonAssocSemiring αᵒᵈ := h instance [h : Semiring α] : Semiring αᵒᵈ := h instance [h : NonUnitalCommSemiring α] : NonUnitalCommSemiring αᵒᵈ := h instance [h : CommSemiring α] : CommSemiring αᵒᵈ := h instance [Mul α] [h : HasDistribNeg α] : HasDistribNeg αᵒᵈ := h instance [h : NonUnitalNonAssocRing α] : NonUnitalNonAssocRing αᵒᵈ := h instance [h : NonUnitalRing α] : NonUnitalRing αᵒᵈ := h instance [h : NonAssocRing α] : NonAssocRing αᵒᵈ := h instance [h : Ring α] : Ring αᵒᵈ := h instance [h : NonUnitalCommRing α] : NonUnitalCommRing αᵒᵈ := h instance [h : CommRing α] : CommRing αᵒᵈ := h instance [Ring α] [h : IsDomain α] : IsDomain αᵒᵈ := h /-! ### Lexicographical order -/ instance [h : Distrib α] : Distrib (Lex α) := h instance [Mul α] [Add α] [h : LeftDistribClass α] : LeftDistribClass (Lex α) := h instance [Mul α] [Add α] [h : RightDistribClass α] : RightDistribClass (Lex α) := h instance [h : NonUnitalNonAssocSemiring α] : NonUnitalNonAssocSemiring (Lex α) := h instance [h : NonUnitalSemiring α] : NonUnitalSemiring (Lex α) := h instance [h : NonAssocSemiring α] : NonAssocSemiring (Lex α) := h instance [h : Semiring α] : Semiring (Lex α) := h instance [h : NonUnitalCommSemiring α] : NonUnitalCommSemiring (Lex α) := h instance [h : CommSemiring α] : CommSemiring (Lex α) := h instance [Mul α] [h : HasDistribNeg α] : HasDistribNeg (Lex α) := h instance [h : NonUnitalNonAssocRing α] : NonUnitalNonAssocRing (Lex α) := h instance [h : NonUnitalRing α] : NonUnitalRing (Lex α) := h instance [h : NonAssocRing α] : NonAssocRing (Lex α) := h instance [h : Ring α] : Ring (Lex α) := h instance [h : NonUnitalCommRing α] : NonUnitalCommRing (Lex α) := h instance [h : CommRing α] : CommRing (Lex α) := h instance [Ring α] [h : IsDomain α] : IsDomain (Lex α) := h
Algebra\Order\Ring\WithTop.lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro -/ import Mathlib.Algebra.Order.GroupWithZero.Synonym import Mathlib.Algebra.Order.Ring.Canonical import Mathlib.Algebra.Ring.Hom.Defs import Mathlib.Algebra.Order.Monoid.WithTop /-! # Structures involving `*` and `0` on `WithTop` and `WithBot` The main results of this section are `WithTop.canonicallyOrderedCommSemiring` and `WithBot.orderedCommSemiring`. -/ variable {α : Type*} namespace WithTop variable [DecidableEq α] section MulZeroClass variable [MulZeroClass α] {a b : WithTop α} instance instMulZeroClass : MulZeroClass (WithTop α) where zero := 0 mul a b := match a, b with | (a : α), (b : α) => ↑(a * b) | (a : α), ⊤ => if a = 0 then 0 else ⊤ | ⊤, (b : α) => if b = 0 then 0 else ⊤ | ⊤, ⊤ => ⊤ mul_zero a := match a with | (a : α) => congr_arg some $ mul_zero _ | ⊤ => if_pos rfl zero_mul b := match b with | (b : α) => congr_arg some $ zero_mul _ | ⊤ => if_pos rfl @[simp, norm_cast] lemma coe_mul (a b : α) : (↑(a * b) : WithTop α) = a * b := rfl lemma mul_top' : ∀ (a : WithTop α), a * ⊤ = if a = 0 then 0 else ⊤ | (a : α) => if_congr coe_eq_zero.symm rfl rfl | ⊤ => (if_neg top_ne_zero).symm @[simp] lemma mul_top (h : a ≠ 0) : a * ⊤ = ⊤ := by rw [mul_top', if_neg h] lemma top_mul' : ∀ (b : WithTop α), ⊤ * b = if b = 0 then 0 else ⊤ | (b : α) => if_congr coe_eq_zero.symm rfl rfl | ⊤ => (if_neg top_ne_zero).symm @[simp] lemma top_mul (hb : b ≠ 0) : ⊤ * b = ⊤ := by rw [top_mul', if_neg hb] @[simp] lemma top_mul_top : (⊤ * ⊤ : WithTop α) = ⊤ := rfl lemma mul_def (a b : WithTop α) : a * b = if a = 0 ∨ b = 0 then 0 else WithTop.map₂ (· * ·) a b := by cases a <;> cases b <;> aesop lemma mul_eq_top_iff : a * b = ⊤ ↔ a ≠ 0 ∧ b = ⊤ ∨ a = ⊤ ∧ b ≠ 0 := by rw [mul_def]; aesop lemma mul_coe_eq_bind {b : α} (hb : b ≠ 0) : ∀ a, (a * b : WithTop α) = a.bind fun a ↦ ↑(a * b) | ⊤ => by simp [top_mul, hb]; rfl | (a : α) => rfl lemma coe_mul_eq_bind {a : α} (ha : a ≠ 0) : ∀ b, (a * b : WithTop α) = b.bind fun b ↦ ↑(a * b) | ⊤ => by simp [top_mul, ha]; rfl | (b : α) => rfl @[simp] lemma untop'_zero_mul (a b : WithTop α) : (a * b).untop' 0 = a.untop' 0 * b.untop' 0 := by by_cases ha : a = 0; · rw [ha, zero_mul, ← coe_zero, untop'_coe, zero_mul] by_cases hb : b = 0; · rw [hb, mul_zero, ← coe_zero, untop'_coe, mul_zero] induction a; · rw [top_mul hb, untop'_top, zero_mul] induction b; · rw [mul_top ha, untop'_top, mul_zero] rw [← coe_mul, untop'_coe, untop'_coe, untop'_coe] theorem mul_lt_top' [LT α] {a b : WithTop α} (ha : a < ⊤) (hb : b < ⊤) : a * b < ⊤ := by rw [WithTop.lt_top_iff_ne_top] at * simp only [Ne, mul_eq_top_iff, *, and_false, false_and, or_self, not_false_eq_true] theorem mul_lt_top [LT α] {a b : WithTop α} (ha : a ≠ ⊤) (hb : b ≠ ⊤) : a * b < ⊤ := mul_lt_top' (WithTop.lt_top_iff_ne_top.2 ha) (WithTop.lt_top_iff_ne_top.2 hb) instance instNoZeroDivisors [NoZeroDivisors α] : NoZeroDivisors (WithTop α) := by refine ⟨fun h₁ => Decidable.by_contradiction fun h₂ => ?_⟩ rw [mul_def, if_neg h₂] at h₁ rcases Option.mem_map₂_iff.1 h₁ with ⟨a, b, (rfl : _ = _), (rfl : _ = _), hab⟩ exact h₂ ((eq_zero_or_eq_zero_of_mul_eq_zero hab).imp (congr_arg some) (congr_arg some)) end MulZeroClass /-- `Nontrivial α` is needed here as otherwise we have `1 * ⊤ = ⊤` but also `0 * ⊤ = 0`. -/ instance instMulZeroOneClass [MulZeroOneClass α] [Nontrivial α] : MulZeroOneClass (WithTop α) where __ := instMulZeroClass one_mul a := match a with | ⊤ => mul_top (mt coe_eq_coe.1 one_ne_zero) | (a : α) => by rw [← coe_one, ← coe_mul, one_mul] mul_one a := match a with | ⊤ => top_mul (mt coe_eq_coe.1 one_ne_zero) | (a : α) => by rw [← coe_one, ← coe_mul, mul_one] /-- A version of `WithTop.map` for `MonoidWithZeroHom`s. -/ @[simps (config := .asFn)] protected def _root_.MonoidWithZeroHom.withTopMap {R S : Type*} [MulZeroOneClass R] [DecidableEq R] [Nontrivial R] [MulZeroOneClass S] [DecidableEq S] [Nontrivial S] (f : R →*₀ S) (hf : Function.Injective f) : WithTop R →*₀ WithTop S := { f.toZeroHom.withTopMap, f.toMonoidHom.toOneHom.withTopMap with toFun := WithTop.map f map_mul' := fun x y => by have : ∀ z, map f z = 0 ↔ z = 0 := fun z => (Option.map_injective hf).eq_iff' f.toZeroHom.withTopMap.map_zero rcases Decidable.eq_or_ne x 0 with (rfl | hx) · simp rcases Decidable.eq_or_ne y 0 with (rfl | hy) · simp induction' x with x · simp [hy, this] induction' y with y · have : (f x : WithTop S) ≠ 0 := by simpa [hf.eq_iff' (map_zero f)] using hx simp [mul_top hx, mul_top this] · -- Porting note (#11215): TODO: `simp [← coe_mul]` times out simp only [map_coe, ← coe_mul, map_mul] } instance instSemigroupWithZero [SemigroupWithZero α] [NoZeroDivisors α] : SemigroupWithZero (WithTop α) where __ := instMulZeroClass mul_assoc a b c := by rcases eq_or_ne a 0 with (rfl | ha); · simp only [zero_mul] rcases eq_or_ne b 0 with (rfl | hb); · simp only [zero_mul, mul_zero] rcases eq_or_ne c 0 with (rfl | hc); · simp only [mul_zero] -- Porting note: below needed to be rewritten due to changed `simp` behaviour for `coe` induction' a with a; · simp [hb, hc] induction' b with b; · simp [mul_top ha, top_mul hc] induction' c with c · rw [mul_top hb, mul_top ha] rw [← coe_zero, ne_eq, coe_eq_coe] at ha hb simp [ha, hb] simp only [← coe_mul, mul_assoc] section MonoidWithZero variable [MonoidWithZero α] [NoZeroDivisors α] [Nontrivial α] instance instMonoidWithZero : MonoidWithZero (WithTop α) where __ := instMulZeroOneClass __ := instSemigroupWithZero npow n a := match a, n with | (a : α), n => ↑(a ^ n) | ⊤, 0 => 1 | ⊤, _n + 1 => ⊤ npow_zero a := by cases a <;> simp npow_succ n a := by cases n <;> cases a <;> simp [pow_succ] @[simp, norm_cast] lemma coe_pow (a : α) (n : ℕ) : (↑(a ^ n) : WithTop α) = a ^ n := rfl theorem top_pow {n : ℕ} (n_pos : 0 < n) : (⊤ : WithTop α) ^ n = ⊤ := Nat.le_induction (pow_one _) (fun m _ hm => by rw [pow_succ, hm, top_mul_top]) _ (Nat.succ_le_of_lt n_pos) end MonoidWithZero instance instCommMonoidWithZero [CommMonoidWithZero α] [NoZeroDivisors α] [Nontrivial α] : CommMonoidWithZero (WithTop α) where __ := instMonoidWithZero mul_comm a b := by simp_rw [mul_def]; exact if_congr or_comm rfl (Option.map₂_comm mul_comm) variable [CanonicallyOrderedCommSemiring α] private theorem distrib' (a b c : WithTop α) : (a + b) * c = a * c + b * c := by induction' c with c · by_cases ha : a = 0 <;> simp [ha] · by_cases hc : c = 0 · simp [hc] simp only [mul_coe_eq_bind hc] cases a <;> cases b repeat' first | rfl |exact congr_arg some (add_mul _ _ _) /-- This instance requires `CanonicallyOrderedCommSemiring` as it is the smallest class that derives from both `NonAssocNonUnitalSemiring` and `CanonicallyOrderedAddCommMonoid`, both of which are required for distributivity. -/ instance commSemiring [Nontrivial α] : CommSemiring (WithTop α) := { addCommMonoidWithOne, instCommMonoidWithZero with right_distrib := distrib' left_distrib := fun a b c => by rw [mul_comm, distrib', mul_comm b, mul_comm c] } instance [Nontrivial α] : CanonicallyOrderedCommSemiring (WithTop α) := { WithTop.commSemiring, WithTop.canonicallyOrderedAddCommMonoid with eq_zero_or_eq_zero_of_mul_eq_zero := eq_zero_or_eq_zero_of_mul_eq_zero} /-- A version of `WithTop.map` for `RingHom`s. -/ @[simps (config := .asFn)] protected def _root_.RingHom.withTopMap {R S : Type*} [CanonicallyOrderedCommSemiring R] [DecidableEq R] [Nontrivial R] [CanonicallyOrderedCommSemiring S] [DecidableEq S] [Nontrivial S] (f : R →+* S) (hf : Function.Injective f) : WithTop R →+* WithTop S := {MonoidWithZeroHom.withTopMap f.toMonoidWithZeroHom hf, f.toAddMonoidHom.withTopMap with} end WithTop namespace WithBot variable [DecidableEq α] section MulZeroClass variable [MulZeroClass α] {a b : WithBot α} instance : MulZeroClass (WithBot α) := WithTop.instMulZeroClass @[simp, norm_cast] lemma coe_mul (a b : α) : (↑(a * b) : WithBot α) = a * b := rfl lemma mul_bot' : ∀ (a : WithBot α), a * ⊥ = if a = 0 then 0 else ⊥ | (a : α) => if_congr coe_eq_zero.symm rfl rfl | ⊥ => (if_neg bot_ne_zero).symm @[simp] lemma mul_bot (h : a ≠ 0) : a * ⊥ = ⊥ := by rw [mul_bot', if_neg h] lemma bot_mul' : ∀ (b : WithBot α), ⊥ * b = if b = 0 then 0 else ⊥ | (b : α) => if_congr coe_eq_zero.symm rfl rfl | ⊥ => (if_neg bot_ne_zero).symm @[simp] lemma bot_mul (hb : b ≠ 0) : ⊥ * b = ⊥ := by rw [bot_mul', if_neg hb] @[simp] lemma bot_mul_bot : (⊥ * ⊥ : WithBot α) = ⊥ := rfl lemma mul_def (a b : WithBot α) : a * b = if a = 0 ∨ b = 0 then 0 else WithBot.map₂ (· * ·) a b := by cases a <;> cases b <;> aesop lemma mul_eq_bot_iff : a * b = ⊥ ↔ a ≠ 0 ∧ b = ⊥ ∨ a = ⊥ ∧ b ≠ 0 := by rw [mul_def]; aesop lemma mul_coe_eq_bind {b : α} (hb : b ≠ 0) : ∀ a, (a * b : WithBot α) = a.bind fun a ↦ ↑(a * b) | ⊥ => by simp only [ne_eq, coe_eq_zero, hb, not_false_eq_true, bot_mul]; rfl | (a : α) => rfl lemma coe_mul_eq_bind {a : α} (ha : a ≠ 0) : ∀ b, (a * b : WithBot α) = b.bind fun b ↦ ↑(a * b) | ⊥ => by simp only [ne_eq, coe_eq_zero, ha, not_false_eq_true, mul_bot]; rfl | (b : α) => rfl @[simp] lemma unbot'_zero_mul (a b : WithBot α) : (a * b).unbot' 0 = a.unbot' 0 * b.unbot' 0 := by by_cases ha : a = 0; · rw [ha, zero_mul, ← coe_zero, unbot'_coe, zero_mul] by_cases hb : b = 0; · rw [hb, mul_zero, ← coe_zero, unbot'_coe, mul_zero] induction a; · rw [bot_mul hb, unbot'_bot, zero_mul] induction b; · rw [mul_bot ha, unbot'_bot, mul_zero] rw [← coe_mul, unbot'_coe, unbot'_coe, unbot'_coe] theorem bot_lt_mul' [LT α] {a b : WithBot α} (ha : ⊥ < a) (hb : ⊥ < b) : ⊥ < a * b := WithTop.mul_lt_top' (α := αᵒᵈ) ha hb theorem bot_lt_mul [LT α] {a b : WithBot α} (ha : a ≠ ⊥) (hb : b ≠ ⊥) : ⊥ < a * b := WithTop.mul_lt_top (α := αᵒᵈ) ha hb instance instNoZeroDivisors [NoZeroDivisors α] : NoZeroDivisors (WithBot α) := WithTop.instNoZeroDivisors end MulZeroClass /-- `Nontrivial α` is needed here as otherwise we have `1 * ⊥ = ⊥` but also `= 0 * ⊥ = 0`. -/ instance instMulZeroOneClass [MulZeroOneClass α] [Nontrivial α] : MulZeroOneClass (WithBot α) := WithTop.instMulZeroOneClass instance instSemigroupWithZero [SemigroupWithZero α] [NoZeroDivisors α] : SemigroupWithZero (WithBot α) := WithTop.instSemigroupWithZero section MonoidWithZero variable [MonoidWithZero α] [NoZeroDivisors α] [Nontrivial α] instance instMonoidWithZero : MonoidWithZero (WithBot α) := WithTop.instMonoidWithZero @[simp, norm_cast] lemma coe_pow (a : α) (n : ℕ) : (↑(a ^ n) : WithBot α) = a ^ n := rfl end MonoidWithZero instance commMonoidWithZero [CommMonoidWithZero α] [NoZeroDivisors α] [Nontrivial α] : CommMonoidWithZero (WithBot α) := WithTop.instCommMonoidWithZero instance commSemiring [CanonicallyOrderedCommSemiring α] [Nontrivial α] : CommSemiring (WithBot α) := WithTop.commSemiring instance [MulZeroClass α] [Preorder α] [PosMulMono α] : PosMulMono (WithBot α) := ⟨by intro ⟨x, x0⟩ a b h simp only [Subtype.coe_mk] rcases eq_or_ne x 0 with rfl | x0' · simp lift x to α · rintro rfl exact (WithBot.bot_lt_coe (0 : α)).not_le x0 induction a · simp_rw [mul_bot x0', bot_le] induction b · exact absurd h (bot_lt_coe _).not_le simp only [← coe_mul, coe_le_coe] at * norm_cast at x0 exact mul_le_mul_of_nonneg_left h x0 ⟩ instance [MulZeroClass α] [Preorder α] [MulPosMono α] : MulPosMono (WithBot α) := ⟨by intro ⟨x, x0⟩ a b h simp only [Subtype.coe_mk] rcases eq_or_ne x 0 with rfl | x0' · simp lift x to α · rintro rfl exact (WithBot.bot_lt_coe (0 : α)).not_le x0 induction a · simp_rw [bot_mul x0', bot_le] induction b · exact absurd h (bot_lt_coe _).not_le simp only [← coe_mul, coe_le_coe] at * norm_cast at x0 exact mul_le_mul_of_nonneg_right h x0 ⟩ instance [MulZeroClass α] [Preorder α] [PosMulStrictMono α] : PosMulStrictMono (WithBot α) := ⟨by intro ⟨x, x0⟩ a b h simp only [Subtype.coe_mk] lift x to α using x0.ne_bot induction b · exact absurd h not_lt_bot induction a · simp_rw [mul_bot x0.ne.symm, ← coe_mul, bot_lt_coe] simp only [← coe_mul, coe_lt_coe] at * norm_cast at x0 exact mul_lt_mul_of_pos_left h x0 ⟩ instance [MulZeroClass α] [Preorder α] [MulPosStrictMono α] : MulPosStrictMono (WithBot α) := ⟨by intro ⟨x, x0⟩ a b h simp only [Subtype.coe_mk] lift x to α using x0.ne_bot induction b · exact absurd h not_lt_bot induction a · simp_rw [bot_mul x0.ne.symm, ← coe_mul, bot_lt_coe] simp only [← coe_mul, coe_lt_coe] at * norm_cast at x0 exact mul_lt_mul_of_pos_right h x0 ⟩ instance [MulZeroClass α] [Preorder α] [PosMulReflectLT α] : PosMulReflectLT (WithBot α) := ⟨by intro ⟨x, x0⟩ a b h simp only [Subtype.coe_mk] at h rcases eq_or_ne x 0 with rfl | x0' · simp at h lift x to α · rintro rfl exact (WithBot.bot_lt_coe (0 : α)).not_le x0 induction b · rw [mul_bot x0'] at h exact absurd h bot_le.not_lt induction a · exact WithBot.bot_lt_coe _ simp only [← coe_mul, coe_lt_coe] at * norm_cast at x0 exact lt_of_mul_lt_mul_left h x0 ⟩ instance [MulZeroClass α] [Preorder α] [MulPosReflectLT α] : MulPosReflectLT (WithBot α) := ⟨by intro ⟨x, x0⟩ a b h simp only [Subtype.coe_mk] at h rcases eq_or_ne x 0 with rfl | x0' · simp at h lift x to α · rintro rfl exact (WithBot.bot_lt_coe (0 : α)).not_le x0 induction b · rw [bot_mul x0'] at h exact absurd h bot_le.not_lt induction a · exact WithBot.bot_lt_coe _ simp only [← coe_mul, coe_lt_coe] at * norm_cast at x0 exact lt_of_mul_lt_mul_right h x0 ⟩ instance [MulZeroClass α] [Preorder α] [PosMulReflectLE α] : PosMulReflectLE (WithBot α) := ⟨by intro ⟨x, x0⟩ a b h simp only [Subtype.coe_mk] at h lift x to α using x0.ne_bot induction a · exact bot_le induction b · rw [mul_bot x0.ne.symm, ← coe_mul] at h exact absurd h (bot_lt_coe _).not_le simp only [← coe_mul, coe_le_coe] at * norm_cast at x0 exact le_of_mul_le_mul_left h x0 ⟩ instance [MulZeroClass α] [Preorder α] [MulPosReflectLE α] : MulPosReflectLE (WithBot α) := ⟨by intro ⟨x, x0⟩ a b h simp only [Subtype.coe_mk] at h lift x to α using x0.ne_bot induction a · exact bot_le induction b · rw [bot_mul x0.ne.symm, ← coe_mul] at h exact absurd h (bot_lt_coe _).not_le simp only [← coe_mul, coe_le_coe] at * norm_cast at x0 exact le_of_mul_le_mul_right h x0 ⟩ instance orderedCommSemiring [CanonicallyOrderedCommSemiring α] [Nontrivial α] : OrderedCommSemiring (WithBot α) := { WithBot.zeroLEOneClass, WithBot.orderedAddCommMonoid, WithBot.commSemiring with mul_le_mul_of_nonneg_left := fun _ _ _ => mul_le_mul_of_nonneg_left mul_le_mul_of_nonneg_right := fun _ _ _ => mul_le_mul_of_nonneg_right } end WithBot
Algebra\Order\Ring\Unbundled\Basic.lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro, Yaël Dillies -/ import Mathlib.Algebra.Group.Pi.Basic import Mathlib.Algebra.Group.Units import Mathlib.Algebra.GroupWithZero.NeZero import Mathlib.Algebra.Order.Group.Unbundled.Basic import Mathlib.Algebra.Order.GroupWithZero.Unbundled import Mathlib.Algebra.Order.Monoid.Unbundled.ExistsOfLE import Mathlib.Algebra.Order.Monoid.NatCast import Mathlib.Algebra.Order.Monoid.Unbundled.MinMax import Mathlib.Algebra.Ring.Defs import Mathlib.Tactic.Tauto /-! # Basic facts for ordered rings and semirings This file develops the basics of ordered (semi)rings in an unbundled fashion for later use with the bundled classes from `Algebra.Order.Ring.Defs`. The set of typeclass variables here comprises * an algebraic class (`Semiring`, `CommSemiring`, `Ring`, `CommRing`) * an order class (`PartialOrder`, `LinearOrder`) * assumptions on how both interact ((strict) monotonicity, canonicity) For short, * "`+` respects `≤`" means "monotonicity of addition" * "`+` respects `<`" means "strict monotonicity of addition" * "`*` respects `≤`" means "monotonicity of multiplication by a nonnegative number". * "`*` respects `<`" means "strict monotonicity of multiplication by a positive number". ## Typeclasses found in `Algebra.Order.Ring.Defs` * `OrderedSemiring`: Semiring with a partial order such that `+` and `*` respect `≤`. * `StrictOrderedSemiring`: Nontrivial semiring with a partial order such that `+` and `*` respects `<`. * `OrderedCommSemiring`: Commutative semiring with a partial order such that `+` and `*` respect `≤`. * `StrictOrderedCommSemiring`: Nontrivial commutative semiring with a partial order such that `+` and `*` respect `<`. * `OrderedRing`: Ring with a partial order such that `+` respects `≤` and `*` respects `<`. * `OrderedCommRing`: Commutative ring with a partial order such that `+` respects `≤` and `*` respects `<`. * `LinearOrderedSemiring`: Nontrivial semiring with a linear order such that `+` respects `≤` and `*` respects `<`. * `LinearOrderedCommSemiring`: Nontrivial commutative semiring with a linear order such that `+` respects `≤` and `*` respects `<`. * `LinearOrderedRing`: Nontrivial ring with a linear order such that `+` respects `≤` and `*` respects `<`. * `LinearOrderedCommRing`: Nontrivial commutative ring with a linear order such that `+` respects `≤` and `*` respects `<`. * `CanonicallyOrderedCommSemiring`: Commutative semiring with a partial order such that `+` respects `≤`, `*` respects `<`, and `a ≤ b ↔ ∃ c, b = a + c`. ## Hierarchy The hardest part of proving order lemmas might be to figure out the correct generality and its corresponding typeclass. Here's an attempt at demystifying it. For each typeclass, we list its immediate predecessors and what conditions are added to each of them. * `OrderedSemiring` - `OrderedAddCommMonoid` & multiplication & `*` respects `≤` - `Semiring` & partial order structure & `+` respects `≤` & `*` respects `≤` * `StrictOrderedSemiring` - `OrderedCancelAddCommMonoid` & multiplication & `*` respects `<` & nontriviality - `OrderedSemiring` & `+` respects `<` & `*` respects `<` & nontriviality * `OrderedCommSemiring` - `OrderedSemiring` & commutativity of multiplication - `CommSemiring` & partial order structure & `+` respects `≤` & `*` respects `<` * `StrictOrderedCommSemiring` - `StrictOrderedSemiring` & commutativity of multiplication - `OrderedCommSemiring` & `+` respects `<` & `*` respects `<` & nontriviality * `OrderedRing` - `OrderedSemiring` & additive inverses - `OrderedAddCommGroup` & multiplication & `*` respects `<` - `Ring` & partial order structure & `+` respects `≤` & `*` respects `<` * `StrictOrderedRing` - `StrictOrderedSemiring` & additive inverses - `OrderedSemiring` & `+` respects `<` & `*` respects `<` & nontriviality * `OrderedCommRing` - `OrderedRing` & commutativity of multiplication - `OrderedCommSemiring` & additive inverses - `CommRing` & partial order structure & `+` respects `≤` & `*` respects `<` * `StrictOrderedCommRing` - `StrictOrderedCommSemiring` & additive inverses - `StrictOrderedRing` & commutativity of multiplication - `OrderedCommRing` & `+` respects `<` & `*` respects `<` & nontriviality * `LinearOrderedSemiring` - `StrictOrderedSemiring` & totality of the order - `LinearOrderedAddCommMonoid` & multiplication & nontriviality & `*` respects `<` * `LinearOrderedCommSemiring` - `StrictOrderedCommSemiring` & totality of the order - `LinearOrderedSemiring` & commutativity of multiplication * `LinearOrderedRing` - `StrictOrderedRing` & totality of the order - `LinearOrderedSemiring` & additive inverses - `LinearOrderedAddCommGroup` & multiplication & `*` respects `<` - `Ring` & `IsDomain` & linear order structure * `LinearOrderedCommRing` - `StrictOrderedCommRing` & totality of the order - `LinearOrderedRing` & commutativity of multiplication - `LinearOrderedCommSemiring` & additive inverses - `CommRing` & `IsDomain` & linear order structure ## Generality Each section is labelled with a corresponding bundled ordered ring typeclass in mind. Mixin's for relating the order structures and ring structures are added as needed. TODO: the mixin assumptiosn can be relaxed in most cases -/ assert_not_exists OrderedCommMonoid assert_not_exists MonoidHom open Function universe u variable {α : Type u} {β : Type*} /-! Note that `OrderDual` does not satisfy any of the ordered ring typeclasses due to the `zero_le_one` field. -/ theorem add_one_le_two_mul [LE α] [Semiring α] [CovariantClass α α (· + ·) (· ≤ ·)] {a : α} (a1 : 1 ≤ a) : a + 1 ≤ 2 * a := calc a + 1 ≤ a + a := add_le_add_left a1 a _ = 2 * a := (two_mul _).symm section OrderedSemiring variable [Semiring α] [Preorder α] {a b c d : α} @[simp] theorem pow_nonneg [ZeroLEOneClass α] [PosMulMono α] (H : 0 ≤ a) : ∀ n : ℕ, 0 ≤ a ^ n | 0 => by rw [pow_zero] exact zero_le_one | n + 1 => by rw [pow_succ] exact mul_nonneg (pow_nonneg H _) H lemma pow_le_pow_of_le_one [ZeroLEOneClass α] [PosMulMono α] [MulPosMono α] (ha₀ : 0 ≤ a) (ha₁ : a ≤ 1) : ∀ {m n : ℕ}, m ≤ n → a ^ n ≤ a ^ m | _, _, Nat.le.refl => le_rfl | _, _, Nat.le.step h => by rw [pow_succ'] exact (mul_le_of_le_one_left (pow_nonneg ha₀ _) ha₁).trans $ pow_le_pow_of_le_one ha₀ ha₁ h lemma pow_le_of_le_one [ZeroLEOneClass α] [PosMulMono α] [MulPosMono α] (h₀ : 0 ≤ a) (h₁ : a ≤ 1) {n : ℕ} (hn : n ≠ 0) : a ^ n ≤ a := (pow_one a).subst (pow_le_pow_of_le_one h₀ h₁ (Nat.pos_of_ne_zero hn)) lemma sq_le [ZeroLEOneClass α] [PosMulMono α] [MulPosMono α] (h₀ : 0 ≤ a) (h₁ : a ≤ 1) : a ^ 2 ≤ a := pow_le_of_le_one h₀ h₁ two_ne_zero -- Porting note: it's unfortunate we need to write `(@one_le_two α)` here. theorem add_le_mul_two_add [ZeroLEOneClass α] [MulPosMono α] [CovariantClass α α (· + ·) (· ≤ ·)] (a2 : 2 ≤ a) (b0 : 0 ≤ b) : a + (2 + b) ≤ a * (2 + b) := calc a + (2 + b) ≤ a + (a + a * b) := add_le_add_left (add_le_add a2 <| le_mul_of_one_le_left b0 <| (@one_le_two α).trans a2) a _ ≤ a * (2 + b) := by rw [mul_add, mul_two, add_assoc] theorem one_le_mul_of_one_le_of_one_le [ZeroLEOneClass α] [PosMulMono α] (ha : 1 ≤ a) (hb : 1 ≤ b) : (1 : α) ≤ a * b := Left.one_le_mul_of_le_of_le ha hb <| zero_le_one.trans ha section Monotone variable [Preorder β] {f g : β → α} theorem monotone_mul_left_of_nonneg [PosMulMono α] (ha : 0 ≤ a) : Monotone fun x => a * x := fun _ _ h => mul_le_mul_of_nonneg_left h ha theorem monotone_mul_right_of_nonneg [MulPosMono α] (ha : 0 ≤ a) : Monotone fun x => x * a := fun _ _ h => mul_le_mul_of_nonneg_right h ha theorem Monotone.mul_const [MulPosMono α] (hf : Monotone f) (ha : 0 ≤ a) : Monotone fun x => f x * a := (monotone_mul_right_of_nonneg ha).comp hf theorem Monotone.const_mul [PosMulMono α] (hf : Monotone f) (ha : 0 ≤ a) : Monotone fun x => a * f x := (monotone_mul_left_of_nonneg ha).comp hf theorem Antitone.mul_const [MulPosMono α] (hf : Antitone f) (ha : 0 ≤ a) : Antitone fun x => f x * a := (monotone_mul_right_of_nonneg ha).comp_antitone hf theorem Antitone.const_mul [PosMulMono α] (hf : Antitone f) (ha : 0 ≤ a) : Antitone fun x => a * f x := (monotone_mul_left_of_nonneg ha).comp_antitone hf theorem Monotone.mul [PosMulMono α] [MulPosMono α] (hf : Monotone f) (hg : Monotone g) (hf₀ : ∀ x, 0 ≤ f x) (hg₀ : ∀ x, 0 ≤ g x) : Monotone (f * g) := fun _ _ h => mul_le_mul (hf h) (hg h) (hg₀ _) (hf₀ _) end Monotone theorem mul_le_one [ZeroLEOneClass α] [PosMulMono α] [MulPosMono α] (ha : a ≤ 1) (hb' : 0 ≤ b) (hb : b ≤ 1) : a * b ≤ 1 := one_mul (1 : α) ▸ mul_le_mul ha hb hb' zero_le_one theorem one_lt_mul_of_le_of_lt [ZeroLEOneClass α] [MulPosMono α] (ha : 1 ≤ a) (hb : 1 < b) : 1 < a * b := hb.trans_le <| le_mul_of_one_le_left (zero_le_one.trans hb.le) ha theorem one_lt_mul_of_lt_of_le [ZeroLEOneClass α] [PosMulMono α] (ha : 1 < a) (hb : 1 ≤ b) : 1 < a * b := ha.trans_le <| le_mul_of_one_le_right (zero_le_one.trans ha.le) hb alias one_lt_mul := one_lt_mul_of_le_of_lt theorem mul_lt_one_of_nonneg_of_lt_one_left [PosMulMono α] (ha₀ : 0 ≤ a) (ha : a < 1) (hb : b ≤ 1) : a * b < 1 := (mul_le_of_le_one_right ha₀ hb).trans_lt ha theorem mul_lt_one_of_nonneg_of_lt_one_right [MulPosMono α] (ha : a ≤ 1) (hb₀ : 0 ≤ b) (hb : b < 1) : a * b < 1 := (mul_le_of_le_one_left hb₀ ha).trans_lt hb theorem mul_le_mul_of_nonpos_left [ExistsAddOfLE α] [PosMulMono α] [CovariantClass α α (swap (· + ·)) (· ≤ ·)] [ContravariantClass α α (swap (· + ·)) (· ≤ ·)] (h : b ≤ a) (hc : c ≤ 0) : c * a ≤ c * b := by obtain ⟨d, hcd⟩ := exists_add_of_le hc refine le_of_add_le_add_right (a := d * b + d * a) ?_ calc _ = d * b := by rw [add_left_comm, ← add_mul, ← hcd, zero_mul, add_zero] _ ≤ d * a := mul_le_mul_of_nonneg_left h <| hcd.trans_le <| add_le_of_nonpos_left hc _ = _ := by rw [← add_assoc, ← add_mul, ← hcd, zero_mul, zero_add] theorem mul_le_mul_of_nonpos_right [ExistsAddOfLE α] [MulPosMono α] [CovariantClass α α (swap (· + ·)) (· ≤ ·)] [ContravariantClass α α (swap (· + ·)) (· ≤ ·)] (h : b ≤ a) (hc : c ≤ 0) : a * c ≤ b * c := by obtain ⟨d, hcd⟩ := exists_add_of_le hc refine le_of_add_le_add_right (a := b * d + a * d) ?_ calc _ = b * d := by rw [add_left_comm, ← mul_add, ← hcd, mul_zero, add_zero] _ ≤ a * d := mul_le_mul_of_nonneg_right h <| hcd.trans_le <| add_le_of_nonpos_left hc _ = _ := by rw [← add_assoc, ← mul_add, ← hcd, mul_zero, zero_add] theorem mul_nonneg_of_nonpos_of_nonpos [ExistsAddOfLE α] [MulPosMono α] [CovariantClass α α (swap (· + ·)) (· ≤ ·)] [ContravariantClass α α (swap (· + ·)) (· ≤ ·)] (ha : a ≤ 0) (hb : b ≤ 0) : 0 ≤ a * b := by simpa only [zero_mul] using mul_le_mul_of_nonpos_right ha hb theorem mul_le_mul_of_nonneg_of_nonpos [ExistsAddOfLE α] [MulPosMono α] [PosMulMono α] [CovariantClass α α (swap (· + ·)) (· ≤ ·)] [ContravariantClass α α (swap (· + ·)) (· ≤ ·)] (hca : c ≤ a) (hbd : b ≤ d) (hc : 0 ≤ c) (hb : b ≤ 0) : a * b ≤ c * d := (mul_le_mul_of_nonpos_right hca hb).trans <| mul_le_mul_of_nonneg_left hbd hc theorem mul_le_mul_of_nonneg_of_nonpos' [ExistsAddOfLE α] [PosMulMono α] [MulPosMono α] [CovariantClass α α (swap (· + ·)) (· ≤ ·)] [ContravariantClass α α (swap (· + ·)) (· ≤ ·)] (hca : c ≤ a) (hbd : b ≤ d) (ha : 0 ≤ a) (hd : d ≤ 0) : a * b ≤ c * d := (mul_le_mul_of_nonneg_left hbd ha).trans <| mul_le_mul_of_nonpos_right hca hd theorem mul_le_mul_of_nonpos_of_nonneg [ExistsAddOfLE α] [MulPosMono α] [PosMulMono α] [CovariantClass α α (swap (· + ·)) (· ≤ ·)] [ContravariantClass α α (swap (· + ·)) (· ≤ ·)] (hac : a ≤ c) (hdb : d ≤ b) (hc : c ≤ 0) (hb : 0 ≤ b) : a * b ≤ c * d := (mul_le_mul_of_nonneg_right hac hb).trans <| mul_le_mul_of_nonpos_left hdb hc theorem mul_le_mul_of_nonpos_of_nonneg' [ExistsAddOfLE α] [PosMulMono α] [MulPosMono α] [CovariantClass α α (swap (· + ·)) (· ≤ ·)] [ContravariantClass α α (swap (· + ·)) (· ≤ ·)] (hca : c ≤ a) (hbd : b ≤ d) (ha : 0 ≤ a) (hd : d ≤ 0) : a * b ≤ c * d := (mul_le_mul_of_nonneg_left hbd ha).trans <| mul_le_mul_of_nonpos_right hca hd theorem mul_le_mul_of_nonpos_of_nonpos [ExistsAddOfLE α] [MulPosMono α] [PosMulMono α] [CovariantClass α α (swap (· + ·)) (· ≤ ·)] [ContravariantClass α α (swap (· + ·)) (· ≤ ·)] (hca : c ≤ a) (hdb : d ≤ b) (hc : c ≤ 0) (hb : b ≤ 0) : a * b ≤ c * d := (mul_le_mul_of_nonpos_right hca hb).trans <| mul_le_mul_of_nonpos_left hdb hc theorem mul_le_mul_of_nonpos_of_nonpos' [ExistsAddOfLE α] [PosMulMono α] [MulPosMono α] [CovariantClass α α (swap (· + ·)) (· ≤ ·)] [ContravariantClass α α (swap (· + ·)) (· ≤ ·)] (hca : c ≤ a) (hdb : d ≤ b) (ha : a ≤ 0) (hd : d ≤ 0) : a * b ≤ c * d := (mul_le_mul_of_nonpos_left hdb ha).trans <| mul_le_mul_of_nonpos_right hca hd /-- Variant of `mul_le_of_le_one_left` for `b` non-positive instead of non-negative. -/ theorem le_mul_of_le_one_left [ExistsAddOfLE α] [MulPosMono α] [CovariantClass α α (swap (· + ·)) (· ≤ ·)] [ContravariantClass α α (swap (· + ·)) (· ≤ ·)] (hb : b ≤ 0) (h : a ≤ 1) : b ≤ a * b := by simpa only [one_mul] using mul_le_mul_of_nonpos_right h hb /-- Variant of `le_mul_of_one_le_left` for `b` non-positive instead of non-negative. -/ theorem mul_le_of_one_le_left [ExistsAddOfLE α] [MulPosMono α] [CovariantClass α α (swap (· + ·)) (· ≤ ·)] [ContravariantClass α α (swap (· + ·)) (· ≤ ·)] (hb : b ≤ 0) (h : 1 ≤ a) : a * b ≤ b := by simpa only [one_mul] using mul_le_mul_of_nonpos_right h hb /-- Variant of `mul_le_of_le_one_right` for `a` non-positive instead of non-negative. -/ theorem le_mul_of_le_one_right [ExistsAddOfLE α] [PosMulMono α] [CovariantClass α α (swap (· + ·)) (· ≤ ·)] [ContravariantClass α α (swap (· + ·)) (· ≤ ·)] (ha : a ≤ 0) (h : b ≤ 1) : a ≤ a * b := by simpa only [mul_one] using mul_le_mul_of_nonpos_left h ha /-- Variant of `le_mul_of_one_le_right` for `a` non-positive instead of non-negative. -/ theorem mul_le_of_one_le_right [ExistsAddOfLE α] [PosMulMono α] [CovariantClass α α (swap (· + ·)) (· ≤ ·)] [ContravariantClass α α (swap (· + ·)) (· ≤ ·)] (ha : a ≤ 0) (h : 1 ≤ b) : a * b ≤ a := by simpa only [mul_one] using mul_le_mul_of_nonpos_left h ha section Monotone variable [Preorder β] {f g : β → α} theorem antitone_mul_left [ExistsAddOfLE α] [PosMulMono α] [CovariantClass α α (swap (· + ·)) (· ≤ ·)] [ContravariantClass α α (swap (· + ·)) (· ≤ ·)] {a : α} (ha : a ≤ 0) : Antitone (a * ·) := fun _ _ b_le_c => mul_le_mul_of_nonpos_left b_le_c ha theorem antitone_mul_right [ExistsAddOfLE α] [MulPosMono α] [CovariantClass α α (swap (· + ·)) (· ≤ ·)] [ContravariantClass α α (swap (· + ·)) (· ≤ ·)] {a : α} (ha : a ≤ 0) : Antitone fun x => x * a := fun _ _ b_le_c => mul_le_mul_of_nonpos_right b_le_c ha theorem Monotone.const_mul_of_nonpos [ExistsAddOfLE α] [PosMulMono α] [CovariantClass α α (swap (· + ·)) (· ≤ ·)] [ContravariantClass α α (swap (· + ·)) (· ≤ ·)] (hf : Monotone f) (ha : a ≤ 0) : Antitone fun x => a * f x := (antitone_mul_left ha).comp_monotone hf theorem Monotone.mul_const_of_nonpos [ExistsAddOfLE α] [MulPosMono α] [CovariantClass α α (swap (· + ·)) (· ≤ ·)] [ContravariantClass α α (swap (· + ·)) (· ≤ ·)] (hf : Monotone f) (ha : a ≤ 0) : Antitone fun x => f x * a := (antitone_mul_right ha).comp_monotone hf theorem Antitone.const_mul_of_nonpos [ExistsAddOfLE α] [PosMulMono α] [CovariantClass α α (swap (· + ·)) (· ≤ ·)] [ContravariantClass α α (swap (· + ·)) (· ≤ ·)] (hf : Antitone f) (ha : a ≤ 0) : Monotone fun x => a * f x := (antitone_mul_left ha).comp hf theorem Antitone.mul_const_of_nonpos [ExistsAddOfLE α] [MulPosMono α] [CovariantClass α α (swap (· + ·)) (· ≤ ·)] [ContravariantClass α α (swap (· + ·)) (· ≤ ·)] (hf : Antitone f) (ha : a ≤ 0) : Monotone fun x => f x * a := (antitone_mul_right ha).comp hf theorem Antitone.mul_monotone [ExistsAddOfLE α] [PosMulMono α] [MulPosMono α] [CovariantClass α α (swap (· + ·)) (· ≤ ·)] [ContravariantClass α α (swap (· + ·)) (· ≤ ·)] (hf : Antitone f) (hg : Monotone g) (hf₀ : ∀ x, f x ≤ 0) (hg₀ : ∀ x, 0 ≤ g x) : Antitone (f * g) := fun _ _ h => mul_le_mul_of_nonpos_of_nonneg (hf h) (hg h) (hf₀ _) (hg₀ _) theorem Monotone.mul_antitone [ExistsAddOfLE α] [PosMulMono α] [MulPosMono α] [CovariantClass α α (swap (· + ·)) (· ≤ ·)] [ContravariantClass α α (swap (· + ·)) (· ≤ ·)] (hf : Monotone f) (hg : Antitone g) (hf₀ : ∀ x, 0 ≤ f x) (hg₀ : ∀ x, g x ≤ 0) : Antitone (f * g) := fun _ _ h => mul_le_mul_of_nonneg_of_nonpos (hf h) (hg h) (hf₀ _) (hg₀ _) theorem Antitone.mul [ExistsAddOfLE α] [PosMulMono α] [MulPosMono α] [CovariantClass α α (swap (· + ·)) (· ≤ ·)] [ContravariantClass α α (swap (· + ·)) (· ≤ ·)] (hf : Antitone f) (hg : Antitone g) (hf₀ : ∀ x, f x ≤ 0) (hg₀ : ∀ x, g x ≤ 0) : Monotone (f * g) := fun _ _ h => mul_le_mul_of_nonpos_of_nonpos (hf h) (hg h) (hf₀ _) (hg₀ _) end Monotone lemma le_iff_exists_nonneg_add [ExistsAddOfLE α] [ContravariantClass α α (· + ·) (· ≤ ·)] [CovariantClass α α (· + ·) (· ≤ ·)] (a b : α) : a ≤ b ↔ ∃ c ≥ 0, b = a + c := by refine ⟨fun h ↦ ?_, ?_⟩ · obtain ⟨c, rfl⟩ := exists_add_of_le h exact ⟨c, nonneg_of_le_add_right h, rfl⟩ · rintro ⟨c, hc, rfl⟩ exact le_add_of_nonneg_right hc end OrderedSemiring section OrderedCommRing section StrictOrderedSemiring variable [Semiring α] [PartialOrder α] {a b c d : α} @[simp] theorem pow_pos [ZeroLEOneClass α] [PosMulStrictMono α] (H : 0 < a) : ∀ n : ℕ, 0 < a ^ n | 0 => by nontriviality rw [pow_zero] exact zero_lt_one | n + 1 => by rw [pow_succ] exact mul_pos (pow_pos H _) H theorem mul_self_lt_mul_self [PosMulStrictMono α] [MulPosMono α] (h1 : 0 ≤ a) (h2 : a < b) : a * a < b * b := mul_lt_mul' h2.le h2 h1 <| h1.trans_lt h2 -- In the next lemma, we used to write `Set.Ici 0` instead of `{x | 0 ≤ x}`. -- As this lemma is not used outside this file, -- and the import for `Set.Ici` is not otherwise needed until later, -- we choose not to use it here. theorem strictMonoOn_mul_self [PosMulStrictMono α] [MulPosMono α] : StrictMonoOn (fun x : α => x * x) { x | 0 ≤ x } := fun _ hx _ _ hxy => mul_self_lt_mul_self hx hxy -- See Note [decidable namespace] protected theorem Decidable.mul_lt_mul'' [PosMulMono α] [PosMulStrictMono α] [MulPosStrictMono α] [@DecidableRel α (· ≤ ·)] (h1 : a < c) (h2 : b < d) (h3 : 0 ≤ a) (h4 : 0 ≤ b) : a * b < c * d := h4.lt_or_eq_dec.elim (fun b0 => mul_lt_mul h1 h2.le b0 <| h3.trans h1.le) fun b0 => by rw [← b0, mul_zero]; exact mul_pos (h3.trans_lt h1) (h4.trans_lt h2) theorem lt_mul_left [MulPosStrictMono α] (hn : 0 < a) (hm : 1 < b) : a < b * a := by convert mul_lt_mul_of_pos_right hm hn rw [one_mul] theorem lt_mul_right [PosMulStrictMono α] (hn : 0 < a) (hm : 1 < b) : a < a * b := by convert mul_lt_mul_of_pos_left hm hn rw [mul_one] theorem lt_mul_self [ZeroLEOneClass α] [MulPosStrictMono α] (hn : 1 < a) : a < a * a := lt_mul_left (hn.trans_le' zero_le_one) hn section Monotone variable [Preorder β] {f g : β → α} theorem strictMono_mul_left_of_pos [PosMulStrictMono α] (ha : 0 < a) : StrictMono fun x => a * x := fun _ _ b_lt_c => mul_lt_mul_of_pos_left b_lt_c ha theorem strictMono_mul_right_of_pos [MulPosStrictMono α] (ha : 0 < a) : StrictMono fun x => x * a := fun _ _ b_lt_c => mul_lt_mul_of_pos_right b_lt_c ha theorem StrictMono.mul_const [MulPosStrictMono α] (hf : StrictMono f) (ha : 0 < a) : StrictMono fun x => f x * a := (strictMono_mul_right_of_pos ha).comp hf theorem StrictMono.const_mul [PosMulStrictMono α] (hf : StrictMono f) (ha : 0 < a) : StrictMono fun x => a * f x := (strictMono_mul_left_of_pos ha).comp hf theorem StrictAnti.mul_const [MulPosStrictMono α] (hf : StrictAnti f) (ha : 0 < a) : StrictAnti fun x => f x * a := (strictMono_mul_right_of_pos ha).comp_strictAnti hf theorem StrictAnti.const_mul [PosMulStrictMono α] (hf : StrictAnti f) (ha : 0 < a) : StrictAnti fun x => a * f x := (strictMono_mul_left_of_pos ha).comp_strictAnti hf theorem StrictMono.mul_monotone [PosMulMono α] [MulPosStrictMono α] (hf : StrictMono f) (hg : Monotone g) (hf₀ : ∀ x, 0 ≤ f x) (hg₀ : ∀ x, 0 < g x) : StrictMono (f * g) := fun _ _ h => mul_lt_mul (hf h) (hg h.le) (hg₀ _) (hf₀ _) theorem Monotone.mul_strictMono [PosMulStrictMono α] [MulPosMono α] (hf : Monotone f) (hg : StrictMono g) (hf₀ : ∀ x, 0 < f x) (hg₀ : ∀ x, 0 ≤ g x) : StrictMono (f * g) := fun _ _ h => mul_lt_mul' (hf h.le) (hg h) (hg₀ _) (hf₀ _) theorem StrictMono.mul [PosMulStrictMono α] [MulPosStrictMono α] (hf : StrictMono f) (hg : StrictMono g) (hf₀ : ∀ x, 0 ≤ f x) (hg₀ : ∀ x, 0 ≤ g x) : StrictMono (f * g) := fun _ _ h => mul_lt_mul'' (hf h) (hg h) (hf₀ _) (hg₀ _) end Monotone theorem lt_two_mul_self [ZeroLEOneClass α] [MulPosStrictMono α] [NeZero (R := α) 1] [CovariantClass α α (· + ·) (· < ·)] (ha : 0 < a) : a < 2 * a := lt_mul_of_one_lt_left ha one_lt_two theorem mul_lt_mul_of_neg_left [ExistsAddOfLE α] [PosMulStrictMono α] [CovariantClass α α (swap (· + ·)) (· < ·)] [ContravariantClass α α (swap (· + ·)) (· < ·)] (h : b < a) (hc : c < 0) : c * a < c * b := by obtain ⟨d, hcd⟩ := exists_add_of_le hc.le refine (add_lt_add_iff_right (d * b + d * a)).1 ?_ calc _ = d * b := by rw [add_left_comm, ← add_mul, ← hcd, zero_mul, add_zero] _ < d * a := mul_lt_mul_of_pos_left h <| hcd.trans_lt <| add_lt_of_neg_left _ hc _ = _ := by rw [← add_assoc, ← add_mul, ← hcd, zero_mul, zero_add] theorem mul_lt_mul_of_neg_right [ExistsAddOfLE α] [MulPosStrictMono α] [CovariantClass α α (swap (· + ·)) (· < ·)] [ContravariantClass α α (swap (· + ·)) (· < ·)] (h : b < a) (hc : c < 0) : a * c < b * c := by obtain ⟨d, hcd⟩ := exists_add_of_le hc.le refine (add_lt_add_iff_right (b * d + a * d)).1 ?_ calc _ = b * d := by rw [add_left_comm, ← mul_add, ← hcd, mul_zero, add_zero] _ < a * d := mul_lt_mul_of_pos_right h <| hcd.trans_lt <| add_lt_of_neg_left _ hc _ = _ := by rw [← add_assoc, ← mul_add, ← hcd, mul_zero, zero_add] theorem mul_pos_of_neg_of_neg [ExistsAddOfLE α] [MulPosStrictMono α] [CovariantClass α α (swap (· + ·)) (· < ·)] [ContravariantClass α α (swap (· + ·)) (· < ·)] {a b : α} (ha : a < 0) (hb : b < 0) : 0 < a * b := by simpa only [zero_mul] using mul_lt_mul_of_neg_right ha hb /-- Variant of `mul_lt_of_lt_one_left` for `b` negative instead of positive. -/ theorem lt_mul_of_lt_one_left [ExistsAddOfLE α] [MulPosStrictMono α] [CovariantClass α α (swap (· + ·)) (· < ·)] [ContravariantClass α α (swap (· + ·)) (· < ·)] (hb : b < 0) (h : a < 1) : b < a * b := by simpa only [one_mul] using mul_lt_mul_of_neg_right h hb /-- Variant of `lt_mul_of_one_lt_left` for `b` negative instead of positive. -/ theorem mul_lt_of_one_lt_left [ExistsAddOfLE α] [MulPosStrictMono α] [CovariantClass α α (swap (· + ·)) (· < ·)] [ContravariantClass α α (swap (· + ·)) (· < ·)] (hb : b < 0) (h : 1 < a) : a * b < b := by simpa only [one_mul] using mul_lt_mul_of_neg_right h hb /-- Variant of `mul_lt_of_lt_one_right` for `a` negative instead of positive. -/ theorem lt_mul_of_lt_one_right [ExistsAddOfLE α] [PosMulStrictMono α] [CovariantClass α α (swap (· + ·)) (· < ·)] [ContravariantClass α α (swap (· + ·)) (· < ·)] (ha : a < 0) (h : b < 1) : a < a * b := by simpa only [mul_one] using mul_lt_mul_of_neg_left h ha /-- Variant of `lt_mul_of_lt_one_right` for `a` negative instead of positive. -/ theorem mul_lt_of_one_lt_right [ExistsAddOfLE α] [PosMulStrictMono α] [CovariantClass α α (swap (· + ·)) (· < ·)] [ContravariantClass α α (swap (· + ·)) (· < ·)] (ha : a < 0) (h : 1 < b) : a * b < a := by simpa only [mul_one] using mul_lt_mul_of_neg_left h ha section Monotone variable [Preorder β] {f g : β → α} theorem strictAnti_mul_left [ExistsAddOfLE α] [PosMulStrictMono α] [CovariantClass α α (swap (· + ·)) (· < ·)] [ContravariantClass α α (swap (· + ·)) (· < ·)] {a : α} (ha : a < 0) : StrictAnti (a * ·) := fun _ _ b_lt_c => mul_lt_mul_of_neg_left b_lt_c ha theorem strictAnti_mul_right [ExistsAddOfLE α] [MulPosStrictMono α] [CovariantClass α α (swap (· + ·)) (· < ·)] [ContravariantClass α α (swap (· + ·)) (· < ·)] {a : α} (ha : a < 0) : StrictAnti fun x => x * a := fun _ _ b_lt_c => mul_lt_mul_of_neg_right b_lt_c ha theorem StrictMono.const_mul_of_neg [ExistsAddOfLE α] [PosMulStrictMono α] [CovariantClass α α (swap (· + ·)) (· < ·)] [ContravariantClass α α (swap (· + ·)) (· < ·)] (hf : StrictMono f) (ha : a < 0) : StrictAnti fun x => a * f x := (strictAnti_mul_left ha).comp_strictMono hf theorem StrictMono.mul_const_of_neg [ExistsAddOfLE α] [MulPosStrictMono α] [CovariantClass α α (swap (· + ·)) (· < ·)] [ContravariantClass α α (swap (· + ·)) (· < ·)] (hf : StrictMono f) (ha : a < 0) : StrictAnti fun x => f x * a := (strictAnti_mul_right ha).comp_strictMono hf theorem StrictAnti.const_mul_of_neg [ExistsAddOfLE α] [PosMulStrictMono α] [CovariantClass α α (swap (· + ·)) (· < ·)] [ContravariantClass α α (swap (· + ·)) (· < ·)] (hf : StrictAnti f) (ha : a < 0) : StrictMono fun x => a * f x := (strictAnti_mul_left ha).comp hf theorem StrictAnti.mul_const_of_neg [ExistsAddOfLE α] [MulPosStrictMono α] [CovariantClass α α (swap (· + ·)) (· < ·)] [ContravariantClass α α (swap (· + ·)) (· < ·)] (hf : StrictAnti f) (ha : a < 0) : StrictMono fun x => f x * a := (strictAnti_mul_right ha).comp hf end Monotone /-- Binary **rearrangement inequality**. -/ lemma mul_add_mul_le_mul_add_mul [ExistsAddOfLE α] [MulPosMono α] [CovariantClass α α (· + ·) (· ≤ ·)] [ContravariantClass α α (· + ·) (· ≤ ·)] (hab : a ≤ b) (hcd : c ≤ d) : a * d + b * c ≤ a * c + b * d := by obtain ⟨b, rfl⟩ := exists_add_of_le hab obtain ⟨d, rfl⟩ := exists_add_of_le hcd rw [mul_add, add_right_comm, mul_add, ← add_assoc] exact add_le_add_left (mul_le_mul_of_nonneg_right hab <| (le_add_iff_nonneg_right _).1 hcd) _ /-- Binary **rearrangement inequality**. -/ lemma mul_add_mul_le_mul_add_mul' [ExistsAddOfLE α] [MulPosMono α] [CovariantClass α α (· + ·) (· ≤ ·)] [ContravariantClass α α (· + ·) (· ≤ ·)] (hba : b ≤ a) (hdc : d ≤ c) : a * d + b * c ≤ a * c + b * d := by rw [add_comm (a * d), add_comm (a * c)]; exact mul_add_mul_le_mul_add_mul hba hdc variable [ContravariantClass α α (· + ·) (· < ·)] /-- Binary strict **rearrangement inequality**. -/ lemma mul_add_mul_lt_mul_add_mul [ExistsAddOfLE α] [MulPosStrictMono α] [CovariantClass α α (· + ·) (· < ·)] (hab : a < b) (hcd : c < d) : a * d + b * c < a * c + b * d := by obtain ⟨b, rfl⟩ := exists_add_of_le hab.le obtain ⟨d, rfl⟩ := exists_add_of_le hcd.le rw [mul_add, add_right_comm, mul_add, ← add_assoc] exact add_lt_add_left (mul_lt_mul_of_pos_right hab <| (lt_add_iff_pos_right _).1 hcd) _ /-- Binary **rearrangement inequality**. -/ lemma mul_add_mul_lt_mul_add_mul' [ExistsAddOfLE α] [MulPosStrictMono α] [CovariantClass α α (· + ·) (· < ·)] [ContravariantClass α α (· + ·) (· < ·)] (hba : b < a) (hdc : d < c) : a * d + b * c < a * c + b * d := by rw [add_comm (a * d), add_comm (a * c)] exact mul_add_mul_lt_mul_add_mul hba hdc end StrictOrderedSemiring section LinearOrderedSemiring variable [Semiring α] [LinearOrder α] {a b c d : α} theorem nonneg_and_nonneg_or_nonpos_and_nonpos_of_mul_nonneg [MulPosStrictMono α] [PosMulStrictMono α] (hab : 0 ≤ a * b) : 0 ≤ a ∧ 0 ≤ b ∨ a ≤ 0 ∧ b ≤ 0 := by refine Decidable.or_iff_not_and_not.2 ?_ simp only [not_and, not_le]; intro ab nab; apply not_lt_of_le hab _ rcases lt_trichotomy 0 a with (ha | rfl | ha) · exact mul_neg_of_pos_of_neg ha (ab ha.le) · exact ((ab le_rfl).asymm (nab le_rfl)).elim · exact mul_neg_of_neg_of_pos ha (nab ha.le) theorem nonneg_of_mul_nonneg_left [MulPosStrictMono α] (h : 0 ≤ a * b) (hb : 0 < b) : 0 ≤ a := le_of_not_gt fun ha => (mul_neg_of_neg_of_pos ha hb).not_le h theorem nonneg_of_mul_nonneg_right [PosMulStrictMono α] (h : 0 ≤ a * b) (ha : 0 < a) : 0 ≤ b := le_of_not_gt fun hb => (mul_neg_of_pos_of_neg ha hb).not_le h theorem nonpos_of_mul_nonpos_left [PosMulStrictMono α] (h : a * b ≤ 0) (hb : 0 < b) : a ≤ 0 := le_of_not_gt fun ha : a > 0 => (mul_pos ha hb).not_le h theorem nonpos_of_mul_nonpos_right [PosMulStrictMono α] (h : a * b ≤ 0) (ha : 0 < a) : b ≤ 0 := le_of_not_gt fun hb : b > 0 => (mul_pos ha hb).not_le h @[simp] theorem mul_nonneg_iff_of_pos_left [PosMulStrictMono α] (h : 0 < c) : 0 ≤ c * b ↔ 0 ≤ b := by convert mul_le_mul_left h simp @[simp] theorem mul_nonneg_iff_of_pos_right [MulPosStrictMono α] (h : 0 < c) : 0 ≤ b * c ↔ 0 ≤ b := by simpa using (mul_le_mul_right h : 0 * c ≤ b * c ↔ 0 ≤ b) theorem add_le_mul_of_left_le_right [ZeroLEOneClass α] [NeZero (R := α) 1] [MulPosStrictMono α] [CovariantClass α α (· + ·) (· ≤ ·)] (a2 : 2 ≤ a) (ab : a ≤ b) : a + b ≤ a * b := have : 0 < b := calc 0 < 2 := zero_lt_two _ ≤ a := a2 _ ≤ b := ab calc a + b ≤ b + b := add_le_add_right ab b _ = 2 * b := (two_mul b).symm _ ≤ a * b := (mul_le_mul_right this).mpr a2 -- Porting note: we used to not need the type annotation on `(0 : α)` at the start of the `calc`. theorem add_le_mul_of_right_le_left [ZeroLEOneClass α] [NeZero (R := α) 1] [CovariantClass α α (· + ·) (· ≤ ·)] [PosMulStrictMono α] (b2 : 2 ≤ b) (ba : b ≤ a) : a + b ≤ a * b := have : 0 < a := calc (0 : α) _ < 2 := zero_lt_two _ ≤ b := b2 _ ≤ a := ba calc a + b ≤ a + a := add_le_add_left ba a _ = a * 2 := (mul_two a).symm _ ≤ a * b := (mul_le_mul_left this).mpr b2 theorem add_le_mul [ZeroLEOneClass α] [NeZero (R := α) 1] [MulPosStrictMono α] [PosMulStrictMono α] [CovariantClass α α (· + ·) (· ≤ ·)] (a2 : 2 ≤ a) (b2 : 2 ≤ b) : a + b ≤ a * b := if hab : a ≤ b then add_le_mul_of_left_le_right a2 hab else add_le_mul_of_right_le_left b2 (le_of_not_le hab) theorem add_le_mul' [ZeroLEOneClass α] [NeZero (R := α) 1] [MulPosStrictMono α] [PosMulStrictMono α] [CovariantClass α α (· + ·) (· ≤ ·)] (a2 : 2 ≤ a) (b2 : 2 ≤ b) : a + b ≤ b * a := (le_of_eq (add_comm _ _)).trans (add_le_mul b2 a2) theorem mul_nonneg_iff_right_nonneg_of_pos [PosMulStrictMono α] (ha : 0 < a) : 0 ≤ a * b ↔ 0 ≤ b := ⟨fun h => nonneg_of_mul_nonneg_right h ha, mul_nonneg ha.le⟩ theorem mul_nonneg_iff_left_nonneg_of_pos [PosMulStrictMono α] [MulPosStrictMono α] (hb : 0 < b) : 0 ≤ a * b ↔ 0 ≤ a := ⟨fun h => nonneg_of_mul_nonneg_left h hb, fun h => mul_nonneg h hb.le⟩ theorem nonpos_of_mul_nonneg_left [PosMulStrictMono α] (h : 0 ≤ a * b) (hb : b < 0) : a ≤ 0 := le_of_not_gt fun ha => absurd h (mul_neg_of_pos_of_neg ha hb).not_le theorem nonpos_of_mul_nonneg_right [MulPosStrictMono α] (h : 0 ≤ a * b) (ha : a < 0) : b ≤ 0 := le_of_not_gt fun hb => absurd h (mul_neg_of_neg_of_pos ha hb).not_le @[simp] theorem Units.inv_pos [ZeroLEOneClass α] [NeZero (R := α) 1] [PosMulStrictMono α] {u : αˣ} : (0 : α) < ↑u⁻¹ ↔ (0 : α) < u := have : ∀ {u : αˣ}, (0 : α) < u → (0 : α) < ↑u⁻¹ := @fun u h => (mul_pos_iff_of_pos_left h).mp <| u.mul_inv.symm ▸ zero_lt_one ⟨this, this⟩ @[simp] theorem Units.inv_neg [ZeroLEOneClass α] [NeZero (R := α) 1] [MulPosMono α] [PosMulMono α] {u : αˣ} : ↑u⁻¹ < (0 : α) ↔ ↑u < (0 : α) := have : ∀ {u : αˣ}, ↑u < (0 : α) → ↑u⁻¹ < (0 : α) := @fun u h => neg_of_mul_pos_right (u.mul_inv.symm ▸ zero_lt_one) h.le ⟨this, this⟩ theorem cmp_mul_pos_left [PosMulStrictMono α] (ha : 0 < a) (b c : α) : cmp (a * b) (a * c) = cmp b c := (strictMono_mul_left_of_pos ha).cmp_map_eq b c theorem cmp_mul_pos_right [MulPosStrictMono α] (ha : 0 < a) (b c : α) : cmp (b * a) (c * a) = cmp b c := (strictMono_mul_right_of_pos ha).cmp_map_eq b c theorem mul_max_of_nonneg [PosMulMono α] (b c : α) (ha : 0 ≤ a) : a * max b c = max (a * b) (a * c) := (monotone_mul_left_of_nonneg ha).map_max theorem mul_min_of_nonneg [PosMulMono α] (b c : α) (ha : 0 ≤ a) : a * min b c = min (a * b) (a * c) := (monotone_mul_left_of_nonneg ha).map_min theorem max_mul_of_nonneg [MulPosMono α] (a b : α) (hc : 0 ≤ c) : max a b * c = max (a * c) (b * c) := (monotone_mul_right_of_nonneg hc).map_max theorem min_mul_of_nonneg [MulPosMono α] (a b : α) (hc : 0 ≤ c) : min a b * c = min (a * c) (b * c) := (monotone_mul_right_of_nonneg hc).map_min theorem le_of_mul_le_of_one_le [ZeroLEOneClass α] [NeZero (R := α) 1] [MulPosStrictMono α] [PosMulMono α] {a b c : α} (h : a * c ≤ b) (hb : 0 ≤ b) (hc : 1 ≤ c) : a ≤ b := le_of_mul_le_mul_right (h.trans <| le_mul_of_one_le_right hb hc) <| zero_lt_one.trans_le hc theorem nonneg_le_nonneg_of_sq_le_sq [PosMulStrictMono α] [MulPosMono α] {a b : α} (hb : 0 ≤ b) (h : a * a ≤ b * b) : a ≤ b := le_of_not_gt fun hab => (mul_self_lt_mul_self hb hab).not_le h theorem mul_self_le_mul_self_iff [PosMulStrictMono α] [MulPosMono α] {a b : α} (h1 : 0 ≤ a) (h2 : 0 ≤ b) : a ≤ b ↔ a * a ≤ b * b := ⟨mul_self_le_mul_self h1, nonneg_le_nonneg_of_sq_le_sq h2⟩ theorem mul_self_lt_mul_self_iff [PosMulStrictMono α] [MulPosMono α] {a b : α} (h1 : 0 ≤ a) (h2 : 0 ≤ b) : a < b ↔ a * a < b * b := ((@strictMonoOn_mul_self α _).lt_iff_lt h1 h2).symm theorem mul_self_inj [PosMulStrictMono α] [MulPosMono α] {a b : α} (h1 : 0 ≤ a) (h2 : 0 ≤ b) : a * a = b * b ↔ a = b := (@strictMonoOn_mul_self α _).eq_iff_eq h1 h2 lemma sign_cases_of_C_mul_pow_nonneg [PosMulStrictMono α] (h : ∀ n, 0 ≤ a * b ^ n) : a = 0 ∨ 0 < a ∧ 0 ≤ b := by have : 0 ≤ a := by simpa only [pow_zero, mul_one] using h 0 refine this.eq_or_gt.imp_right fun ha ↦ ⟨ha, nonneg_of_mul_nonneg_right ?_ ha⟩ simpa only [pow_one] using h 1 theorem mul_pos_iff [ExistsAddOfLE α] [PosMulStrictMono α] [MulPosStrictMono α] [CovariantClass α α (· + ·) (· < ·)] [ContravariantClass α α (· + ·) (· < ·)] : 0 < a * b ↔ 0 < a ∧ 0 < b ∨ a < 0 ∧ b < 0 := ⟨pos_and_pos_or_neg_and_neg_of_mul_pos, fun h => h.elim (and_imp.2 mul_pos) (and_imp.2 mul_pos_of_neg_of_neg)⟩ theorem mul_nonneg_iff [ExistsAddOfLE α] [MulPosStrictMono α] [PosMulStrictMono α] [ContravariantClass α α (· + ·) (· ≤ ·)] [CovariantClass α α (· + ·) (· ≤ ·)]: 0 ≤ a * b ↔ 0 ≤ a ∧ 0 ≤ b ∨ a ≤ 0 ∧ b ≤ 0 := ⟨nonneg_and_nonneg_or_nonpos_and_nonpos_of_mul_nonneg, fun h => h.elim (and_imp.2 mul_nonneg) (and_imp.2 mul_nonneg_of_nonpos_of_nonpos)⟩ /-- Out of three elements of a `LinearOrderedRing`, two must have the same sign. -/ theorem mul_nonneg_of_three [ExistsAddOfLE α] [MulPosStrictMono α] [PosMulStrictMono α] [CovariantClass α α (· + ·) (· ≤ ·)] [ContravariantClass α α (· + ·) (· ≤ ·)] (a b c : α) : 0 ≤ a * b ∨ 0 ≤ b * c ∨ 0 ≤ c * a := by iterate 3 rw [mul_nonneg_iff] have or_a := le_total 0 a have or_b := le_total 0 b have or_c := le_total 0 c -- Porting note used to be by `itauto` from here exact Or.elim or_c (fun (h0 : 0 ≤ c) => Or.elim or_b (fun (h1 : 0 ≤ b) => Or.elim or_a (fun (h2 : 0 ≤ a) => Or.inl (Or.inl ⟨h2, h1⟩)) (fun (_ : a ≤ 0) => Or.inr (Or.inl (Or.inl ⟨h1, h0⟩)))) (fun (h1 : b ≤ 0) => Or.elim or_a (fun (h3 : 0 ≤ a) => Or.inr (Or.inr (Or.inl ⟨h0, h3⟩))) (fun (h3 : a ≤ 0) => Or.inl (Or.inr ⟨h3, h1⟩)))) (fun (h0 : c ≤ 0) => Or.elim or_b (fun (h4 : 0 ≤ b) => Or.elim or_a (fun (h5 : 0 ≤ a) => Or.inl (Or.inl ⟨h5, h4⟩)) (fun (h5 : a ≤ 0) => Or.inr (Or.inr (Or.inr ⟨h0, h5⟩)))) (fun (h4 : b ≤ 0) => Or.elim or_a (fun (_ : 0 ≤ a) => Or.inr (Or.inl (Or.inr ⟨h4, h0⟩))) (fun (h6 : a ≤ 0) => Or.inl (Or.inr ⟨h6, h4⟩)))) lemma mul_nonneg_iff_pos_imp_nonneg [ExistsAddOfLE α] [PosMulStrictMono α] [MulPosStrictMono α] [CovariantClass α α (· + ·) (· ≤ ·)] [ContravariantClass α α (· + ·) (· ≤ ·)] : 0 ≤ a * b ↔ (0 < a → 0 ≤ b) ∧ (0 < b → 0 ≤ a) := by refine mul_nonneg_iff.trans ?_ simp_rw [← not_le, ← or_iff_not_imp_left] have := le_total a 0 have := le_total b 0 tauto @[simp] theorem mul_le_mul_left_of_neg [ExistsAddOfLE α] [PosMulStrictMono α] [CovariantClass α α (swap (· + ·)) (· ≤ ·)] [ContravariantClass α α (swap (· + ·)) (· ≤ ·)] {a b c : α} (h : c < 0) : c * a ≤ c * b ↔ b ≤ a := (strictAnti_mul_left h).le_iff_le @[simp] theorem mul_le_mul_right_of_neg [ExistsAddOfLE α] [MulPosStrictMono α] [CovariantClass α α (swap (· + ·)) (· ≤ ·)] [ContravariantClass α α (swap (· + ·)) (· ≤ ·)] {a b c : α} (h : c < 0) : a * c ≤ b * c ↔ b ≤ a := (strictAnti_mul_right h).le_iff_le @[simp] theorem mul_lt_mul_left_of_neg [ExistsAddOfLE α] [PosMulStrictMono α] [CovariantClass α α (swap (· + ·)) (· < ·)] [ContravariantClass α α (swap (· + ·)) (· < ·)] {a b c : α} (h : c < 0) : c * a < c * b ↔ b < a := (strictAnti_mul_left h).lt_iff_lt @[simp] theorem mul_lt_mul_right_of_neg [ExistsAddOfLE α] [MulPosStrictMono α] [CovariantClass α α (swap (· + ·)) (· < ·)] [ContravariantClass α α (swap (· + ·)) (· < ·)] {a b c : α} (h : c < 0) : a * c < b * c ↔ b < a := (strictAnti_mul_right h).lt_iff_lt theorem lt_of_mul_lt_mul_of_nonpos_left [ExistsAddOfLE α] [PosMulMono α] [CovariantClass α α (swap (· + ·)) (· ≤ ·)] [ContravariantClass α α (swap (· + ·)) (· ≤ ·)] (h : c * a < c * b) (hc : c ≤ 0) : b < a := (antitone_mul_left hc).reflect_lt h theorem lt_of_mul_lt_mul_of_nonpos_right [ExistsAddOfLE α] [MulPosMono α] [CovariantClass α α (swap (· + ·)) (· ≤ ·)] [ContravariantClass α α (swap (· + ·)) (· ≤ ·)] (h : a * c < b * c) (hc : c ≤ 0) : b < a := (antitone_mul_right hc).reflect_lt h theorem cmp_mul_neg_left [ExistsAddOfLE α] [PosMulStrictMono α] [ContravariantClass α α (swap (· + ·)) (· < ·)] [CovariantClass α α (swap (· + ·)) (· < ·)] {a : α} (ha : a < 0) (b c : α) : cmp (a * b) (a * c) = cmp c b := (strictAnti_mul_left ha).cmp_map_eq b c theorem cmp_mul_neg_right [ExistsAddOfLE α] [MulPosStrictMono α] [ContravariantClass α α (swap (· + ·)) (· < ·)] [CovariantClass α α (swap (· + ·)) (· < ·)] {a : α} (ha : a < 0) (b c : α) : cmp (b * a) (c * a) = cmp c b := (strictAnti_mul_right ha).cmp_map_eq b c @[simp] theorem mul_self_pos [ExistsAddOfLE α] [PosMulStrictMono α] [MulPosStrictMono α] [CovariantClass α α (· + ·) (· < ·)] [ContravariantClass α α (· + ·) (· < ·)] {a : α} : 0 < a * a ↔ a ≠ 0 := by constructor · rintro h rfl rw [mul_zero] at h exact h.false · intro h cases' h.lt_or_lt with h h exacts [mul_pos_of_neg_of_neg h h, mul_pos h h] theorem nonneg_of_mul_nonpos_left [ExistsAddOfLE α] [MulPosStrictMono α] [CovariantClass α α (swap (· + ·)) (· ≤ ·)] [ContravariantClass α α (swap (· + ·)) (· ≤ ·)] {a b : α} (h : a * b ≤ 0) (hb : b < 0) : 0 ≤ a := le_of_not_gt fun ha => absurd h (mul_pos_of_neg_of_neg ha hb).not_le theorem nonneg_of_mul_nonpos_right [ExistsAddOfLE α] [MulPosStrictMono α] [CovariantClass α α (swap (· + ·)) (· ≤ ·)] [ContravariantClass α α (swap (· + ·)) (· ≤ ·)] {a b : α} (h : a * b ≤ 0) (ha : a < 0) : 0 ≤ b := le_of_not_gt fun hb => absurd h (mul_pos_of_neg_of_neg ha hb).not_le theorem pos_of_mul_neg_left [ExistsAddOfLE α] [MulPosMono α] [CovariantClass α α (swap (· + ·)) (· ≤ ·)] [ContravariantClass α α (swap (· + ·)) (· ≤ ·)] {a b : α} (h : a * b < 0) (hb : b ≤ 0) : 0 < a := lt_of_not_ge fun ha => absurd h (mul_nonneg_of_nonpos_of_nonpos ha hb).not_lt theorem pos_of_mul_neg_right [ExistsAddOfLE α] [MulPosMono α] [CovariantClass α α (swap (· + ·)) (· ≤ ·)] [ContravariantClass α α (swap (· + ·)) (· ≤ ·)] {a b : α} (h : a * b < 0) (ha : a ≤ 0) : 0 < b := lt_of_not_ge fun hb => absurd h (mul_nonneg_of_nonpos_of_nonpos ha hb).not_lt theorem neg_iff_pos_of_mul_neg [ExistsAddOfLE α] [PosMulMono α] [MulPosMono α] [CovariantClass α α (swap (· + ·)) (· ≤ ·)] [ContravariantClass α α (swap (· + ·)) (· ≤ ·)] (hab : a * b < 0) : a < 0 ↔ 0 < b := ⟨pos_of_mul_neg_right hab ∘ le_of_lt, neg_of_mul_neg_left hab ∘ le_of_lt⟩ theorem pos_iff_neg_of_mul_neg [ExistsAddOfLE α] [PosMulMono α] [MulPosMono α] [CovariantClass α α (swap (· + ·)) (· ≤ ·)] [ContravariantClass α α (swap (· + ·)) (· ≤ ·)] (hab : a * b < 0) : 0 < a ↔ b < 0 := ⟨neg_of_mul_neg_right hab ∘ le_of_lt, pos_of_mul_neg_left hab ∘ le_of_lt⟩ lemma sq_nonneg [IsRightCancelAdd α] [ZeroLEOneClass α] [ExistsAddOfLE α] [PosMulMono α] [CovariantClass α α (· + ·) (· < ·)] (a : α) : 0 ≤ a ^ 2 := by obtain ha | ha := le_total 0 a · exact pow_nonneg ha _ obtain ⟨b, hab⟩ := exists_add_of_le ha calc 0 ≤ b ^ 2 := pow_nonneg (not_lt.1 fun hb ↦ hab.not_gt <| add_neg_of_nonpos_of_neg ha hb) _ _ = a ^ 2 := add_left_injective (a * b) ?_ calc b ^ 2 + a * b = (a + b) * b := by rw [add_comm, sq, add_mul] _ = a * (a + b) := by simp [← hab] _ = a ^ 2 + a * b := by rw [sq, mul_add] alias pow_two_nonneg := sq_nonneg lemma mul_self_nonneg [IsRightCancelAdd α] [ZeroLEOneClass α] [ExistsAddOfLE α] [PosMulMono α] [CovariantClass α α (· + ·) (· < ·)] (a : α) : 0 ≤ a * a := by simpa only [sq] using sq_nonneg a /-- The sum of two squares is zero iff both elements are zero. -/ lemma mul_self_add_mul_self_eq_zero [IsRightCancelAdd α] [NoZeroDivisors α] [ZeroLEOneClass α] [ExistsAddOfLE α] [PosMulMono α] [CovariantClass α α (· + ·) (· ≤ ·)] [CovariantClass α α (· + ·) (· < ·)] : a * a + b * b = 0 ↔ a = 0 ∧ b = 0 := by rw [add_eq_zero_iff', mul_self_eq_zero (M₀ := α), mul_self_eq_zero (M₀ := α)] <;> apply mul_self_nonneg lemma eq_zero_of_mul_self_add_mul_self_eq_zero [IsRightCancelAdd α] [NoZeroDivisors α] [ZeroLEOneClass α] [ExistsAddOfLE α] [PosMulMono α] [CovariantClass α α (· + ·) (· ≤ ·)] [CovariantClass α α (· + ·) (· < ·)] (h : a * a + b * b = 0) : a = 0 := (mul_self_add_mul_self_eq_zero.mp h).left end LinearOrderedSemiring section LinearOrderedCommSemiring variable [CommSemiring α] [LinearOrder α] {a b c d : α} lemma max_mul_mul_le_max_mul_max [PosMulMono α] [MulPosMono α] (b c : α) (ha : 0 ≤ a) (hd : 0 ≤ d) : max (a * b) (d * c) ≤ max a c * max d b := have ba : b * a ≤ max d b * max c a := mul_le_mul (le_max_right d b) (le_max_right c a) ha (le_trans hd (le_max_left d b)) have cd : c * d ≤ max a c * max b d := mul_le_mul (le_max_right a c) (le_max_right b d) hd (le_trans ha (le_max_left a c)) max_le (by simpa [mul_comm, max_comm] using ba) (by simpa [mul_comm, max_comm] using cd) /-- Binary **arithmetic mean-geometric mean inequality** (aka AM-GM inequality) for linearly ordered commutative semirings. -/ lemma two_mul_le_add_sq [ExistsAddOfLE α] [MulPosStrictMono α] [ContravariantClass α α (· + ·) (· ≤ ·)] [CovariantClass α α (· + ·) (· ≤ ·)] (a b : α) : 2 * a * b ≤ a ^ 2 + b ^ 2 := by simpa [fn_min_add_fn_max (fun x ↦ x * x), sq, two_mul, add_mul] using mul_add_mul_le_mul_add_mul (@min_le_max _ _ a b) (@min_le_max _ _ a b) alias two_mul_le_add_pow_two := two_mul_le_add_sq end LinearOrderedCommSemiring section LinearOrderedRing variable [Ring α] [LinearOrder α] {a b : α} -- TODO: Can the following five lemmas be generalised to -- `[Semiring α] [LinearOrder α] [ExistsAddOfLE α] ..`? lemma mul_neg_iff [PosMulStrictMono α] [MulPosStrictMono α] [ContravariantClass α α (· + ·) (· < ·)] [CovariantClass α α (· + ·) (· < ·)] : a * b < 0 ↔ 0 < a ∧ b < 0 ∨ a < 0 ∧ 0 < b := by rw [← neg_pos, neg_mul_eq_mul_neg, mul_pos_iff (α := α), neg_pos, neg_lt_zero] lemma mul_nonpos_iff [MulPosStrictMono α] [PosMulStrictMono α] [ContravariantClass α α (· + ·) (· ≤ ·)] [CovariantClass α α (· + ·) (· ≤ ·)] : a * b ≤ 0 ↔ 0 ≤ a ∧ b ≤ 0 ∨ a ≤ 0 ∧ 0 ≤ b := by rw [← neg_nonneg, neg_mul_eq_mul_neg, mul_nonneg_iff (α := α), neg_nonneg, neg_nonpos] lemma mul_nonneg_iff_neg_imp_nonpos [PosMulStrictMono α] [MulPosStrictMono α] [CovariantClass α α (· + ·) (· ≤ ·)] [ContravariantClass α α (· + ·) (· ≤ ·)] : 0 ≤ a * b ↔ (a < 0 → b ≤ 0) ∧ (b < 0 → a ≤ 0) := by rw [← neg_mul_neg, mul_nonneg_iff_pos_imp_nonneg (α := α)]; simp only [neg_pos, neg_nonneg] lemma mul_nonpos_iff_pos_imp_nonpos [PosMulStrictMono α] [MulPosStrictMono α] [CovariantClass α α (· + ·) (· ≤ ·)] [ContravariantClass α α (· + ·) (· ≤ ·)] : a * b ≤ 0 ↔ (0 < a → b ≤ 0) ∧ (b < 0 → 0 ≤ a) := by rw [← neg_nonneg, ← mul_neg, mul_nonneg_iff_pos_imp_nonneg (α := α)] simp only [neg_pos, neg_nonneg] lemma mul_nonpos_iff_neg_imp_nonneg [PosMulStrictMono α] [MulPosStrictMono α] [CovariantClass α α (· + ·) (· ≤ ·)] [ContravariantClass α α (· + ·) (· ≤ ·)] : a * b ≤ 0 ↔ (a < 0 → 0 ≤ b) ∧ (0 < b → a ≤ 0) := by rw [← neg_nonneg, ← neg_mul, mul_nonneg_iff_pos_imp_nonneg (α := α)] simp only [neg_pos, neg_nonneg] lemma neg_one_lt_zero [ZeroLEOneClass α] [NeZero (R := α) 1] [CovariantClass α α (· + ·) (· < ·)] : -1 < (0 : α) := neg_lt_zero.2 zero_lt_one lemma sub_one_lt [ZeroLEOneClass α] [NeZero (R := α) 1] [CovariantClass α α (· + ·) (· < ·)] (a : α) : a - 1 < a := sub_lt_iff_lt_add.2 <| lt_add_one a lemma mul_self_le_mul_self_of_le_of_neg_le [MulPosMono α] [PosMulMono α] [CovariantClass α α (· + ·) (· ≤ ·)] (h₁ : a ≤ b) (h₂ : -a ≤ b) : a * a ≤ b * b := (le_total 0 a).elim (mul_self_le_mul_self · h₁) fun h ↦ (neg_mul_neg a a).symm.trans_le <| mul_le_mul h₂ h₂ (neg_nonneg.2 h) <| (neg_nonneg.2 h).trans h₂ end LinearOrderedRing @[deprecated (since := "2023-12-23")] alias zero_le_mul_left := mul_nonneg_iff_of_pos_left @[deprecated (since := "2023-12-23")] alias zero_le_mul_right := mul_nonneg_iff_of_pos_right @[deprecated (since := "2023-12-23")] alias zero_lt_mul_left := mul_pos_iff_of_pos_left @[deprecated (since := "2023-12-23")] alias zero_lt_mul_right := mul_pos_iff_of_pos_right
Algebra\Order\Ring\Unbundled\Nonneg.lean
/- Copyright (c) 2021 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import Mathlib.Algebra.Order.GroupWithZero.Unbundled import Mathlib.Algebra.Order.Monoid.Unbundled.Pow import Mathlib.Algebra.Order.ZeroLEOne import Mathlib.Algebra.Ring.Defs import Mathlib.Algebra.Ring.InjSurj import Mathlib.Data.Nat.Cast.Order.Basic import Mathlib.Order.CompleteLatticeIntervals import Mathlib.Order.LatticeIntervals /-! # The type of nonnegative elements This file defines instances and prove some properties about the nonnegative elements `{x : α // 0 ≤ x}` of an arbitrary type `α`. Currently we only state instances and states some `simp`/`norm_cast` lemmas. When `α` is `ℝ`, this will give us some properties about `ℝ≥0`. ## Implementation Notes Instead of `{x : α // 0 ≤ x}` we could also use `Set.Ici (0 : α)`, which is definitionally equal. However, using the explicit subtype has a big advantage: when writing an element explicitly with a proof of nonnegativity as `⟨x, hx⟩`, the `hx` is expected to have type `0 ≤ x`. If we would use `Ici 0`, then the type is expected to be `x ∈ Ici 0`. Although these types are definitionally equal, this often confuses the elaborator. Similar problems arise when doing cases on an element. The disadvantage is that we have to duplicate some instances about `Set.Ici` to this subtype. -/ assert_not_exists OrderedCommMonoid open Set variable {α : Type*} namespace Nonneg /-- This instance uses data fields from `Subtype.partialOrder` to help type-class inference. The `Set.Ici` data fields are definitionally equal, but that requires unfolding semireducible definitions, so type-class inference won't see this. -/ instance orderBot [Preorder α] {a : α} : OrderBot { x : α // a ≤ x } := { Set.Ici.orderBot with } theorem bot_eq [Preorder α] {a : α} : (⊥ : { x : α // a ≤ x }) = ⟨a, le_rfl⟩ := rfl instance noMaxOrder [PartialOrder α] [NoMaxOrder α] {a : α} : NoMaxOrder { x : α // a ≤ x } := show NoMaxOrder (Ici a) by infer_instance instance semilatticeSup [SemilatticeSup α] {a : α} : SemilatticeSup { x : α // a ≤ x } := Set.Ici.semilatticeSup instance semilatticeInf [SemilatticeInf α] {a : α} : SemilatticeInf { x : α // a ≤ x } := Set.Ici.semilatticeInf instance distribLattice [DistribLattice α] {a : α} : DistribLattice { x : α // a ≤ x } := Set.Ici.distribLattice instance instDenselyOrdered [Preorder α] [DenselyOrdered α] {a : α} : DenselyOrdered { x : α // a ≤ x } := show DenselyOrdered (Ici a) from Set.instDenselyOrdered /-- If `sSup ∅ ≤ a` then `{x : α // a ≤ x}` is a `ConditionallyCompleteLinearOrder`. -/ protected noncomputable abbrev conditionallyCompleteLinearOrder [ConditionallyCompleteLinearOrder α] {a : α} : ConditionallyCompleteLinearOrder { x : α // a ≤ x } := { @ordConnectedSubsetConditionallyCompleteLinearOrder α (Set.Ici a) _ ⟨⟨a, le_rfl⟩⟩ _ with } /-- If `sSup ∅ ≤ a` then `{x : α // a ≤ x}` is a `ConditionallyCompleteLinearOrderBot`. This instance uses data fields from `Subtype.linearOrder` to help type-class inference. The `Set.Ici` data fields are definitionally equal, but that requires unfolding semireducible definitions, so type-class inference won't see this. -/ protected noncomputable abbrev conditionallyCompleteLinearOrderBot [ConditionallyCompleteLinearOrder α] (a : α) : ConditionallyCompleteLinearOrderBot { x : α // a ≤ x } := { Nonneg.orderBot, Nonneg.conditionallyCompleteLinearOrder with csSup_empty := by rw [@subset_sSup_def α (Set.Ici a) _ _ ⟨⟨a, le_rfl⟩⟩]; simp [bot_eq] } instance inhabited [Preorder α] {a : α} : Inhabited { x : α // a ≤ x } := ⟨⟨a, le_rfl⟩⟩ instance zero [Zero α] [Preorder α] : Zero { x : α // 0 ≤ x } := ⟨⟨0, le_rfl⟩⟩ @[simp, norm_cast] protected theorem coe_zero [Zero α] [Preorder α] : ((0 : { x : α // 0 ≤ x }) : α) = 0 := rfl @[simp] theorem mk_eq_zero [Zero α] [Preorder α] {x : α} (hx : 0 ≤ x) : (⟨x, hx⟩ : { x : α // 0 ≤ x }) = 0 ↔ x = 0 := Subtype.ext_iff instance add [AddZeroClass α] [Preorder α] [CovariantClass α α (· + ·) (· ≤ ·)] : Add { x : α // 0 ≤ x } := ⟨fun x y => ⟨x + y, add_nonneg x.2 y.2⟩⟩ @[simp] theorem mk_add_mk [AddZeroClass α] [Preorder α] [CovariantClass α α (· + ·) (· ≤ ·)] {x y : α} (hx : 0 ≤ x) (hy : 0 ≤ y) : (⟨x, hx⟩ : { x : α // 0 ≤ x }) + ⟨y, hy⟩ = ⟨x + y, add_nonneg hx hy⟩ := rfl @[simp, norm_cast] protected theorem coe_add [AddZeroClass α] [Preorder α] [CovariantClass α α (· + ·) (· ≤ ·)] (a b : { x : α // 0 ≤ x }) : ((a + b : { x : α // 0 ≤ x }) : α) = a + b := rfl instance nsmul [AddMonoid α] [Preorder α] [CovariantClass α α (· + ·) (· ≤ ·)] : SMul ℕ { x : α // 0 ≤ x } := ⟨fun n x => ⟨n • (x : α), nsmul_nonneg x.prop n⟩⟩ @[simp] theorem nsmul_mk [AddMonoid α] [Preorder α] [CovariantClass α α (· + ·) (· ≤ ·)] (n : ℕ) {x : α} (hx : 0 ≤ x) : (n • (⟨x, hx⟩ : { x : α // 0 ≤ x })) = ⟨n • x, nsmul_nonneg hx n⟩ := rfl @[simp, norm_cast] protected theorem coe_nsmul [AddMonoid α] [Preorder α] [CovariantClass α α (· + ·) (· ≤ ·)] (n : ℕ) (a : { x : α // 0 ≤ x }) : ((n • a : { x : α // 0 ≤ x }) : α) = n • (a : α) := rfl section One variable [Zero α] [One α] [LE α] [ZeroLEOneClass α] instance one : One { x : α // 0 ≤ x } where one := ⟨1, zero_le_one⟩ @[simp, norm_cast] protected theorem coe_one : ((1 : { x : α // 0 ≤ x }) : α) = 1 := rfl @[simp] theorem mk_eq_one {x : α} (hx : 0 ≤ x) : (⟨x, hx⟩ : { x : α // 0 ≤ x }) = 1 ↔ x = 1 := Subtype.ext_iff end One section Mul variable [MulZeroClass α] [Preorder α] [PosMulMono α] instance mul : Mul { x : α // 0 ≤ x } where mul x y := ⟨x * y, mul_nonneg x.2 y.2⟩ @[simp, norm_cast] protected theorem coe_mul (a b : { x : α // 0 ≤ x }) : ((a * b : { x : α // 0 ≤ x }) : α) = a * b := rfl @[simp] theorem mk_mul_mk {x y : α} (hx : 0 ≤ x) (hy : 0 ≤ y) : (⟨x, hx⟩ : { x : α // 0 ≤ x }) * ⟨y, hy⟩ = ⟨x * y, mul_nonneg hx hy⟩ := rfl end Mul section AddMonoid variable [AddMonoid α] [Preorder α] [CovariantClass α α (· + ·) (· ≤ ·)] instance addMonoid : AddMonoid { x : α // 0 ≤ x } := Subtype.coe_injective.addMonoid _ Nonneg.coe_zero (fun _ _ => rfl) fun _ _ => rfl /-- Coercion `{x : α // 0 ≤ x} → α` as an `AddMonoidHom`. -/ def coeAddMonoidHom : { x : α // 0 ≤ x } →+ α := { toFun := ((↑) : { x : α // 0 ≤ x } → α) map_zero' := Nonneg.coe_zero map_add' := Nonneg.coe_add } @[norm_cast] theorem nsmul_coe (n : ℕ) (r : { x : α // 0 ≤ x }) : ↑(n • r) = n • (r : α) := Nonneg.coeAddMonoidHom.map_nsmul _ _ end AddMonoid section AddCommMonoid variable [AddCommMonoid α] [Preorder α] [CovariantClass α α (· + ·) (· ≤ ·)] instance addCommMonoid : AddCommMonoid { x : α // 0 ≤ x } := Subtype.coe_injective.addCommMonoid _ Nonneg.coe_zero (fun _ _ => rfl) (fun _ _ => rfl) end AddCommMonoid section AddMonoidWithOne variable [AddMonoidWithOne α] [PartialOrder α] variable [CovariantClass α α (· + ·) (· ≤ ·)] [ZeroLEOneClass α] instance natCast : NatCast { x : α // 0 ≤ x } := ⟨fun n => ⟨n, Nat.cast_nonneg' n⟩⟩ @[simp, norm_cast] protected theorem coe_natCast (n : ℕ) : ((↑n : { x : α // 0 ≤ x }) : α) = n := rfl @[deprecated (since := "2024-04-17")] alias coe_nat_cast := Nonneg.coe_natCast @[simp] theorem mk_natCast (n : ℕ) : (⟨n, n.cast_nonneg'⟩ : { x : α // 0 ≤ x }) = n := rfl @[deprecated (since := "2024-04-17")] alias mk_nat_cast := mk_natCast instance addMonoidWithOne : AddMonoidWithOne { x : α // 0 ≤ x } := { Nonneg.one (α := α) with toNatCast := Nonneg.natCast natCast_zero := by ext; simp natCast_succ := fun _ => by ext; simp } end AddMonoidWithOne section Pow variable [MonoidWithZero α] [Preorder α] [ZeroLEOneClass α] [PosMulMono α] @[simp] theorem pow_nonneg {a : α} (H : 0 ≤ a) : ∀ n : ℕ, 0 ≤ a ^ n | 0 => by rw [pow_zero] exact zero_le_one | n + 1 => by rw [pow_succ] exact mul_nonneg (pow_nonneg H _) H instance pow : Pow { x : α // 0 ≤ x } ℕ where pow x n := ⟨(x : α) ^ n, pow_nonneg x.2 n⟩ @[simp, norm_cast] protected theorem coe_pow (a : { x : α // 0 ≤ x }) (n : ℕ) : (↑(a ^ n) : α) = (a : α) ^ n := rfl @[simp] theorem mk_pow {x : α} (hx : 0 ≤ x) (n : ℕ) : (⟨x, hx⟩ : { x : α // 0 ≤ x }) ^ n = ⟨x ^ n, pow_nonneg hx n⟩ := rfl end Pow section Semiring variable [Semiring α] [PartialOrder α] [ZeroLEOneClass α] [CovariantClass α α (· + ·) (· ≤ ·)] [PosMulMono α] instance semiring : Semiring { x : α // 0 ≤ x } := Subtype.coe_injective.semiring _ Nonneg.coe_zero Nonneg.coe_one (fun _ _ => rfl) (fun _ _=> rfl) (fun _ _ => rfl) (fun _ _ => rfl) fun _ => rfl instance monoidWithZero : MonoidWithZero { x : α // 0 ≤ x } := by infer_instance /-- Coercion `{x : α // 0 ≤ x} → α` as a `RingHom`. -/ def coeRingHom : { x : α // 0 ≤ x } →+* α := { toFun := ((↑) : { x : α // 0 ≤ x } → α) map_one' := Nonneg.coe_one map_mul' := Nonneg.coe_mul map_zero' := Nonneg.coe_zero, map_add' := Nonneg.coe_add } end Semiring section Nontrivial variable [Semiring α] [PartialOrder α] [ZeroLEOneClass α] [NeZero 1] [CovariantClass α α (· + ·) (· ≤ ·)] [PosMulMono α] end Nontrivial section CommSemiring variable [CommSemiring α] [PartialOrder α] [ZeroLEOneClass α] [CovariantClass α α (· + ·) (· ≤ ·)] [PosMulMono α] instance commSemiring : CommSemiring { x : α // 0 ≤ x } := Subtype.coe_injective.commSemiring _ Nonneg.coe_zero Nonneg.coe_one (fun _ _ => rfl) (fun _ _ => rfl) (fun _ _ => rfl) (fun _ _ => rfl) fun _ => rfl instance commMonoidWithZero : CommMonoidWithZero { x : α // 0 ≤ x } := inferInstance end CommSemiring section LinearOrder variable [Zero α] [LinearOrder α] /-- The function `a ↦ max a 0` of type `α → {x : α // 0 ≤ x}`. -/ def toNonneg (a : α) : { x : α // 0 ≤ x } := ⟨max a 0, le_max_right _ _⟩ @[simp] theorem coe_toNonneg {a : α} : (toNonneg a : α) = max a 0 := rfl @[simp] theorem toNonneg_of_nonneg {a : α} (h : 0 ≤ a) : toNonneg a = ⟨a, h⟩ := by simp [toNonneg, h] @[simp] theorem toNonneg_coe {a : { x : α // 0 ≤ x }} : toNonneg (a : α) = a := toNonneg_of_nonneg a.2 @[simp] theorem toNonneg_le {a : α} {b : { x : α // 0 ≤ x }} : toNonneg a ≤ b ↔ a ≤ b := by cases' b with b hb simp [toNonneg, hb] @[simp] theorem toNonneg_lt {a : { x : α // 0 ≤ x }} {b : α} : a < toNonneg b ↔ ↑a < b := by cases' a with a ha simp [toNonneg, ha.not_lt] instance sub [Sub α] : Sub { x : α // 0 ≤ x } := ⟨fun x y => toNonneg (x - y)⟩ @[simp] theorem mk_sub_mk [Sub α] {x y : α} (hx : 0 ≤ x) (hy : 0 ≤ y) : (⟨x, hx⟩ : { x : α // 0 ≤ x }) - ⟨y, hy⟩ = toNonneg (x - y) := rfl end LinearOrder end Nonneg
Algebra\Order\Sub\Basic.lean
/- Copyright (c) 2021 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import Mathlib.Algebra.Group.Equiv.Basic import Mathlib.Algebra.Ring.Basic import Mathlib.Algebra.Order.Sub.Defs import Mathlib.Order.Hom.Basic /-! # Additional results about ordered Subtraction -/ variable {α β : Type*} section Add variable [Preorder α] [Add α] [Sub α] [OrderedSub α] {a b c d : α} theorem AddHom.le_map_tsub [Preorder β] [Add β] [Sub β] [OrderedSub β] (f : AddHom α β) (hf : Monotone f) (a b : α) : f a - f b ≤ f (a - b) := by rw [tsub_le_iff_right, ← f.map_add] exact hf le_tsub_add theorem le_mul_tsub {R : Type*} [Distrib R] [Preorder R] [Sub R] [OrderedSub R] [CovariantClass R R (· * ·) (· ≤ ·)] {a b c : R} : a * b - a * c ≤ a * (b - c) := (AddHom.mulLeft a).le_map_tsub (monotone_id.const_mul' a) _ _ theorem le_tsub_mul {R : Type*} [CommSemiring R] [Preorder R] [Sub R] [OrderedSub R] [CovariantClass R R (· * ·) (· ≤ ·)] {a b c : R} : a * c - b * c ≤ (a - b) * c := by simpa only [mul_comm _ c] using le_mul_tsub end Add /-- An order isomorphism between types with ordered subtraction preserves subtraction provided that it preserves addition. -/ theorem OrderIso.map_tsub {M N : Type*} [Preorder M] [Add M] [Sub M] [OrderedSub M] [PartialOrder N] [Add N] [Sub N] [OrderedSub N] (e : M ≃o N) (h_add : ∀ a b, e (a + b) = e a + e b) (a b : M) : e (a - b) = e a - e b := by let e_add : M ≃+ N := { e with map_add' := h_add } refine le_antisymm ?_ (e_add.toAddHom.le_map_tsub e.monotone a b) suffices e (e.symm (e a) - e.symm (e b)) ≤ e (e.symm (e a - e b)) by simpa exact e.monotone (e_add.symm.toAddHom.le_map_tsub e.symm.monotone _ _) /-! ### Preorder -/ section Preorder variable [Preorder α] variable [AddCommMonoid α] [Sub α] [OrderedSub α] {a b c d : α} theorem AddMonoidHom.le_map_tsub [Preorder β] [AddCommMonoid β] [Sub β] [OrderedSub β] (f : α →+ β) (hf : Monotone f) (a b : α) : f a - f b ≤ f (a - b) := f.toAddHom.le_map_tsub hf a b end Preorder
Algebra\Order\Sub\Canonical.lean
/- Copyright (c) 2021 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import Mathlib.Algebra.Group.Even import Mathlib.Algebra.Order.Monoid.Canonical.Defs import Mathlib.Algebra.Order.Sub.Defs /-! # Lemmas about subtraction in canonically ordered monoids -/ variable {α : Type*} section ExistsAddOfLE variable [AddCommSemigroup α] [PartialOrder α] [ExistsAddOfLE α] [CovariantClass α α (· + ·) (· ≤ ·)] [Sub α] [OrderedSub α] {a b c d : α} @[simp] theorem add_tsub_cancel_of_le (h : a ≤ b) : a + (b - a) = b := by refine le_antisymm ?_ le_add_tsub obtain ⟨c, rfl⟩ := exists_add_of_le h exact add_le_add_left add_tsub_le_left a theorem tsub_add_cancel_of_le (h : a ≤ b) : b - a + a = b := by rw [add_comm] exact add_tsub_cancel_of_le h theorem add_le_of_le_tsub_right_of_le (h : b ≤ c) (h2 : a ≤ c - b) : a + b ≤ c := (add_le_add_right h2 b).trans_eq <| tsub_add_cancel_of_le h theorem add_le_of_le_tsub_left_of_le (h : a ≤ c) (h2 : b ≤ c - a) : a + b ≤ c := (add_le_add_left h2 a).trans_eq <| add_tsub_cancel_of_le h theorem tsub_le_tsub_iff_right (h : c ≤ b) : a - c ≤ b - c ↔ a ≤ b := by rw [tsub_le_iff_right, tsub_add_cancel_of_le h] theorem tsub_left_inj (h1 : c ≤ a) (h2 : c ≤ b) : a - c = b - c ↔ a = b := by simp_rw [le_antisymm_iff, tsub_le_tsub_iff_right h1, tsub_le_tsub_iff_right h2] theorem tsub_inj_left (h₁ : a ≤ b) (h₂ : a ≤ c) : b - a = c - a → b = c := (tsub_left_inj h₁ h₂).1 /-- See `lt_of_tsub_lt_tsub_right` for a stronger statement in a linear order. -/ theorem lt_of_tsub_lt_tsub_right_of_le (h : c ≤ b) (h2 : a - c < b - c) : a < b := by refine ((tsub_le_tsub_iff_right h).mp h2.le).lt_of_ne ?_ rintro rfl exact h2.false theorem tsub_add_tsub_cancel (hab : b ≤ a) (hcb : c ≤ b) : a - b + (b - c) = a - c := by convert tsub_add_cancel_of_le (tsub_le_tsub_right hab c) using 2 rw [tsub_tsub, add_tsub_cancel_of_le hcb] theorem tsub_tsub_tsub_cancel_right (h : c ≤ b) : a - c - (b - c) = a - b := by rw [tsub_tsub, add_tsub_cancel_of_le h] /-! #### Lemmas that assume that an element is `AddLECancellable`. -/ namespace AddLECancellable protected theorem eq_tsub_iff_add_eq_of_le (hc : AddLECancellable c) (h : c ≤ b) : a = b - c ↔ a + c = b := ⟨by rintro rfl exact tsub_add_cancel_of_le h, hc.eq_tsub_of_add_eq⟩ protected theorem tsub_eq_iff_eq_add_of_le (hb : AddLECancellable b) (h : b ≤ a) : a - b = c ↔ a = c + b := by rw [eq_comm, hb.eq_tsub_iff_add_eq_of_le h, eq_comm] protected theorem add_tsub_assoc_of_le (hc : AddLECancellable c) (h : c ≤ b) (a : α) : a + b - c = a + (b - c) := by conv_lhs => rw [← add_tsub_cancel_of_le h, add_comm c, ← add_assoc, hc.add_tsub_cancel_right] protected theorem tsub_add_eq_add_tsub (hb : AddLECancellable b) (h : b ≤ a) : a - b + c = a + c - b := by rw [add_comm a, hb.add_tsub_assoc_of_le h, add_comm] protected theorem tsub_tsub_assoc (hbc : AddLECancellable (b - c)) (h₁ : b ≤ a) (h₂ : c ≤ b) : a - (b - c) = a - b + c := hbc.tsub_eq_of_eq_add <| by rw [add_assoc, add_tsub_cancel_of_le h₂, tsub_add_cancel_of_le h₁] protected theorem tsub_add_tsub_comm (hb : AddLECancellable b) (hd : AddLECancellable d) (hba : b ≤ a) (hdc : d ≤ c) : a - b + (c - d) = a + c - (b + d) := by rw [hb.tsub_add_eq_add_tsub hba, ← hd.add_tsub_assoc_of_le hdc, tsub_tsub, add_comm d] protected theorem le_tsub_iff_left (ha : AddLECancellable a) (h : a ≤ c) : b ≤ c - a ↔ a + b ≤ c := ⟨add_le_of_le_tsub_left_of_le h, ha.le_tsub_of_add_le_left⟩ protected theorem le_tsub_iff_right (ha : AddLECancellable a) (h : a ≤ c) : b ≤ c - a ↔ b + a ≤ c := by rw [add_comm] exact ha.le_tsub_iff_left h protected theorem tsub_lt_iff_left (hb : AddLECancellable b) (hba : b ≤ a) : a - b < c ↔ a < b + c := by refine ⟨hb.lt_add_of_tsub_lt_left, ?_⟩ intro h; refine (tsub_le_iff_left.mpr h.le).lt_of_ne ?_ rintro rfl; exact h.ne' (add_tsub_cancel_of_le hba) protected theorem tsub_lt_iff_right (hb : AddLECancellable b) (hba : b ≤ a) : a - b < c ↔ a < c + b := by rw [add_comm] exact hb.tsub_lt_iff_left hba protected theorem tsub_lt_iff_tsub_lt (hb : AddLECancellable b) (hc : AddLECancellable c) (h₁ : b ≤ a) (h₂ : c ≤ a) : a - b < c ↔ a - c < b := by rw [hb.tsub_lt_iff_left h₁, hc.tsub_lt_iff_right h₂] protected theorem le_tsub_iff_le_tsub (ha : AddLECancellable a) (hc : AddLECancellable c) (h₁ : a ≤ b) (h₂ : c ≤ b) : a ≤ b - c ↔ c ≤ b - a := by rw [ha.le_tsub_iff_left h₁, hc.le_tsub_iff_right h₂] protected theorem lt_tsub_iff_right_of_le (hc : AddLECancellable c) (h : c ≤ b) : a < b - c ↔ a + c < b := by refine ⟨fun h' => (add_le_of_le_tsub_right_of_le h h'.le).lt_of_ne ?_, hc.lt_tsub_of_add_lt_right⟩ rintro rfl exact h'.ne' hc.add_tsub_cancel_right protected theorem lt_tsub_iff_left_of_le (hc : AddLECancellable c) (h : c ≤ b) : a < b - c ↔ c + a < b := by rw [add_comm] exact hc.lt_tsub_iff_right_of_le h protected theorem tsub_inj_right (hab : AddLECancellable (a - b)) (h₁ : b ≤ a) (h₂ : c ≤ a) (h₃ : a - b = a - c) : b = c := by rw [← hab.inj] rw [tsub_add_cancel_of_le h₁, h₃, tsub_add_cancel_of_le h₂] protected theorem lt_of_tsub_lt_tsub_left_of_le [ContravariantClass α α (· + ·) (· < ·)] (hb : AddLECancellable b) (hca : c ≤ a) (h : a - b < a - c) : c < b := by conv_lhs at h => rw [← tsub_add_cancel_of_le hca] exact lt_of_add_lt_add_left (hb.lt_add_of_tsub_lt_right h) protected theorem tsub_lt_tsub_left_of_le (hab : AddLECancellable (a - b)) (h₁ : b ≤ a) (h : c < b) : a - b < a - c := (tsub_le_tsub_left h.le _).lt_of_ne fun h' => h.ne' <| hab.tsub_inj_right h₁ (h.le.trans h₁) h' protected theorem tsub_lt_tsub_right_of_le (hc : AddLECancellable c) (h : c ≤ a) (h2 : a < b) : a - c < b - c := by apply hc.lt_tsub_of_add_lt_left rwa [add_tsub_cancel_of_le h] protected theorem tsub_lt_tsub_iff_left_of_le_of_le [ContravariantClass α α (· + ·) (· < ·)] (hb : AddLECancellable b) (hab : AddLECancellable (a - b)) (h₁ : b ≤ a) (h₂ : c ≤ a) : a - b < a - c ↔ c < b := ⟨hb.lt_of_tsub_lt_tsub_left_of_le h₂, hab.tsub_lt_tsub_left_of_le h₁⟩ @[simp] protected theorem add_tsub_tsub_cancel (hac : AddLECancellable (a - c)) (h : c ≤ a) : a + b - (a - c) = b + c := hac.tsub_eq_of_eq_add <| by rw [add_assoc, add_tsub_cancel_of_le h, add_comm] protected theorem tsub_tsub_cancel_of_le (hba : AddLECancellable (b - a)) (h : a ≤ b) : b - (b - a) = a := hba.tsub_eq_of_eq_add (add_tsub_cancel_of_le h).symm protected theorem tsub_tsub_tsub_cancel_left (hab : AddLECancellable (a - b)) (h : b ≤ a) : a - c - (a - b) = b - c := by rw [tsub_right_comm, hab.tsub_tsub_cancel_of_le h] end AddLECancellable section Contra /-! ### Lemmas where addition is order-reflecting. -/ variable [ContravariantClass α α (· + ·) (· ≤ ·)] theorem eq_tsub_iff_add_eq_of_le (h : c ≤ b) : a = b - c ↔ a + c = b := Contravariant.AddLECancellable.eq_tsub_iff_add_eq_of_le h theorem tsub_eq_iff_eq_add_of_le (h : b ≤ a) : a - b = c ↔ a = c + b := Contravariant.AddLECancellable.tsub_eq_iff_eq_add_of_le h /-- See `add_tsub_le_assoc` for an inequality. -/ theorem add_tsub_assoc_of_le (h : c ≤ b) (a : α) : a + b - c = a + (b - c) := Contravariant.AddLECancellable.add_tsub_assoc_of_le h a theorem tsub_add_eq_add_tsub (h : b ≤ a) : a - b + c = a + c - b := Contravariant.AddLECancellable.tsub_add_eq_add_tsub h theorem tsub_tsub_assoc (h₁ : b ≤ a) (h₂ : c ≤ b) : a - (b - c) = a - b + c := Contravariant.AddLECancellable.tsub_tsub_assoc h₁ h₂ theorem tsub_add_tsub_comm (hba : b ≤ a) (hdc : d ≤ c) : a - b + (c - d) = a + c - (b + d) := Contravariant.AddLECancellable.tsub_add_tsub_comm Contravariant.AddLECancellable hba hdc theorem le_tsub_iff_left (h : a ≤ c) : b ≤ c - a ↔ a + b ≤ c := Contravariant.AddLECancellable.le_tsub_iff_left h theorem le_tsub_iff_right (h : a ≤ c) : b ≤ c - a ↔ b + a ≤ c := Contravariant.AddLECancellable.le_tsub_iff_right h theorem tsub_lt_iff_left (hbc : b ≤ a) : a - b < c ↔ a < b + c := Contravariant.AddLECancellable.tsub_lt_iff_left hbc theorem tsub_lt_iff_right (hbc : b ≤ a) : a - b < c ↔ a < c + b := Contravariant.AddLECancellable.tsub_lt_iff_right hbc theorem tsub_lt_iff_tsub_lt (h₁ : b ≤ a) (h₂ : c ≤ a) : a - b < c ↔ a - c < b := Contravariant.AddLECancellable.tsub_lt_iff_tsub_lt Contravariant.AddLECancellable h₁ h₂ theorem le_tsub_iff_le_tsub (h₁ : a ≤ b) (h₂ : c ≤ b) : a ≤ b - c ↔ c ≤ b - a := Contravariant.AddLECancellable.le_tsub_iff_le_tsub Contravariant.AddLECancellable h₁ h₂ /-- See `lt_tsub_iff_right` for a stronger statement in a linear order. -/ theorem lt_tsub_iff_right_of_le (h : c ≤ b) : a < b - c ↔ a + c < b := Contravariant.AddLECancellable.lt_tsub_iff_right_of_le h /-- See `lt_tsub_iff_left` for a stronger statement in a linear order. -/ theorem lt_tsub_iff_left_of_le (h : c ≤ b) : a < b - c ↔ c + a < b := Contravariant.AddLECancellable.lt_tsub_iff_left_of_le h /-- See `lt_of_tsub_lt_tsub_left` for a stronger statement in a linear order. -/ theorem lt_of_tsub_lt_tsub_left_of_le [ContravariantClass α α (· + ·) (· < ·)] (hca : c ≤ a) (h : a - b < a - c) : c < b := Contravariant.AddLECancellable.lt_of_tsub_lt_tsub_left_of_le hca h theorem tsub_lt_tsub_left_of_le : b ≤ a → c < b → a - b < a - c := Contravariant.AddLECancellable.tsub_lt_tsub_left_of_le theorem tsub_lt_tsub_right_of_le (h : c ≤ a) (h2 : a < b) : a - c < b - c := Contravariant.AddLECancellable.tsub_lt_tsub_right_of_le h h2 theorem tsub_inj_right (h₁ : b ≤ a) (h₂ : c ≤ a) (h₃ : a - b = a - c) : b = c := Contravariant.AddLECancellable.tsub_inj_right h₁ h₂ h₃ /-- See `tsub_lt_tsub_iff_left_of_le` for a stronger statement in a linear order. -/ theorem tsub_lt_tsub_iff_left_of_le_of_le [ContravariantClass α α (· + ·) (· < ·)] (h₁ : b ≤ a) (h₂ : c ≤ a) : a - b < a - c ↔ c < b := Contravariant.AddLECancellable.tsub_lt_tsub_iff_left_of_le_of_le Contravariant.AddLECancellable h₁ h₂ @[simp] theorem add_tsub_tsub_cancel (h : c ≤ a) : a + b - (a - c) = b + c := Contravariant.AddLECancellable.add_tsub_tsub_cancel h /-- See `tsub_tsub_le` for an inequality. -/ theorem tsub_tsub_cancel_of_le (h : a ≤ b) : b - (b - a) = a := Contravariant.AddLECancellable.tsub_tsub_cancel_of_le h theorem tsub_tsub_tsub_cancel_left (h : b ≤ a) : a - c - (a - b) = b - c := Contravariant.AddLECancellable.tsub_tsub_tsub_cancel_left h -- note: not generalized to `AddLECancellable` because `add_tsub_add_eq_tsub_left` isn't /-- The `tsub` version of `sub_sub_eq_add_sub`. -/ theorem tsub_tsub_eq_add_tsub_of_le (h : c ≤ b) : a - (b - c) = a + c - b := by obtain ⟨d, rfl⟩ := exists_add_of_le h rw [add_tsub_cancel_left c, add_comm a c, add_tsub_add_eq_tsub_left] end Contra end ExistsAddOfLE /-! ### Lemmas in a canonically ordered monoid. -/ section CanonicallyOrderedAddCommMonoid variable [CanonicallyOrderedAddCommMonoid α] [Sub α] [OrderedSub α] {a b c d : α} theorem add_tsub_cancel_iff_le : a + (b - a) = b ↔ a ≤ b := ⟨fun h => le_iff_exists_add.mpr ⟨b - a, h.symm⟩, add_tsub_cancel_of_le⟩ theorem tsub_add_cancel_iff_le : b - a + a = b ↔ a ≤ b := by rw [add_comm] exact add_tsub_cancel_iff_le -- This was previously a `@[simp]` lemma, but it is not necessarily a good idea, e.g. in -- `example (h : n - m = 0) : a + (n - m) = a := by simp_all` theorem tsub_eq_zero_iff_le : a - b = 0 ↔ a ≤ b := by rw [← nonpos_iff_eq_zero, tsub_le_iff_left, add_zero] alias ⟨_, tsub_eq_zero_of_le⟩ := tsub_eq_zero_iff_le attribute [simp] tsub_eq_zero_of_le theorem tsub_self (a : α) : a - a = 0 := tsub_eq_zero_of_le le_rfl theorem tsub_le_self : a - b ≤ a := tsub_le_iff_left.mpr <| le_add_left le_rfl theorem zero_tsub (a : α) : 0 - a = 0 := tsub_eq_zero_of_le <| zero_le a theorem tsub_self_add (a b : α) : a - (a + b) = 0 := tsub_eq_zero_of_le <| self_le_add_right _ _ theorem tsub_pos_iff_not_le : 0 < a - b ↔ ¬a ≤ b := by rw [pos_iff_ne_zero, Ne, tsub_eq_zero_iff_le] theorem tsub_pos_of_lt (h : a < b) : 0 < b - a := tsub_pos_iff_not_le.mpr h.not_le theorem tsub_lt_of_lt (h : a < b) : a - c < b := lt_of_le_of_lt tsub_le_self h namespace AddLECancellable protected theorem tsub_le_tsub_iff_left (ha : AddLECancellable a) (hc : AddLECancellable c) (h : c ≤ a) : a - b ≤ a - c ↔ c ≤ b := by refine ⟨?_, fun h => tsub_le_tsub_left h a⟩ rw [tsub_le_iff_left, ← hc.add_tsub_assoc_of_le h, hc.le_tsub_iff_right (h.trans le_add_self), add_comm b] apply ha protected theorem tsub_right_inj (ha : AddLECancellable a) (hb : AddLECancellable b) (hc : AddLECancellable c) (hba : b ≤ a) (hca : c ≤ a) : a - b = a - c ↔ b = c := by simp_rw [le_antisymm_iff, ha.tsub_le_tsub_iff_left hb hba, ha.tsub_le_tsub_iff_left hc hca, and_comm] end AddLECancellable /-! #### Lemmas where addition is order-reflecting. -/ section Contra variable [ContravariantClass α α (· + ·) (· ≤ ·)] theorem tsub_le_tsub_iff_left (h : c ≤ a) : a - b ≤ a - c ↔ c ≤ b := Contravariant.AddLECancellable.tsub_le_tsub_iff_left Contravariant.AddLECancellable h theorem tsub_right_inj (hba : b ≤ a) (hca : c ≤ a) : a - b = a - c ↔ b = c := Contravariant.AddLECancellable.tsub_right_inj Contravariant.AddLECancellable Contravariant.AddLECancellable hba hca variable (α) /-- A `CanonicallyOrderedAddCommMonoid` with ordered subtraction and order-reflecting addition is cancellative. This is not an instance as it would form a typeclass loop. See note [reducible non-instances]. -/ abbrev CanonicallyOrderedAddCommMonoid.toAddCancelCommMonoid : AddCancelCommMonoid α := { (by infer_instance : AddCommMonoid α) with add_left_cancel := fun a b c h => by simpa only [add_tsub_cancel_left] using congr_arg (fun x => x - a) h } end Contra end CanonicallyOrderedAddCommMonoid /-! ### Lemmas in a linearly canonically ordered monoid. -/ section CanonicallyLinearOrderedAddCommMonoid variable [CanonicallyLinearOrderedAddCommMonoid α] [Sub α] [OrderedSub α] {a b c d : α} @[simp] theorem tsub_pos_iff_lt : 0 < a - b ↔ b < a := by rw [tsub_pos_iff_not_le, not_le] theorem tsub_eq_tsub_min (a b : α) : a - b = a - min a b := by rcases le_total a b with h | h · rw [min_eq_left h, tsub_self, tsub_eq_zero_of_le h] · rw [min_eq_right h] namespace AddLECancellable protected theorem lt_tsub_iff_right (hc : AddLECancellable c) : a < b - c ↔ a + c < b := ⟨lt_imp_lt_of_le_imp_le tsub_le_iff_right.mpr, hc.lt_tsub_of_add_lt_right⟩ protected theorem lt_tsub_iff_left (hc : AddLECancellable c) : a < b - c ↔ c + a < b := ⟨lt_imp_lt_of_le_imp_le tsub_le_iff_left.mpr, hc.lt_tsub_of_add_lt_left⟩ protected theorem tsub_lt_tsub_iff_right (hc : AddLECancellable c) (h : c ≤ a) : a - c < b - c ↔ a < b := by rw [hc.lt_tsub_iff_left, add_tsub_cancel_of_le h] protected theorem tsub_lt_self (ha : AddLECancellable a) (h₁ : 0 < a) (h₂ : 0 < b) : a - b < a := by refine tsub_le_self.lt_of_ne fun h => ?_ rw [← h, tsub_pos_iff_lt] at h₁ exact h₂.not_le (ha.add_le_iff_nonpos_left.1 <| add_le_of_le_tsub_left_of_le h₁.le h.ge) protected theorem tsub_lt_self_iff (ha : AddLECancellable a) : a - b < a ↔ 0 < a ∧ 0 < b := by refine ⟨fun h => ⟨(zero_le _).trans_lt h, (zero_le b).lt_of_ne ?_⟩, fun h => ha.tsub_lt_self h.1 h.2⟩ rintro rfl rw [tsub_zero] at h exact h.false /-- See `lt_tsub_iff_left_of_le_of_le` for a weaker statement in a partial order. -/ protected theorem tsub_lt_tsub_iff_left_of_le (ha : AddLECancellable a) (hb : AddLECancellable b) (h : b ≤ a) : a - b < a - c ↔ c < b := lt_iff_lt_of_le_iff_le <| ha.tsub_le_tsub_iff_left hb h end AddLECancellable section Contra variable [ContravariantClass α α (· + ·) (· ≤ ·)] /-- This lemma also holds for `ENNReal`, but we need a different proof for that. -/ theorem tsub_lt_tsub_iff_right (h : c ≤ a) : a - c < b - c ↔ a < b := Contravariant.AddLECancellable.tsub_lt_tsub_iff_right h theorem tsub_lt_self : 0 < a → 0 < b → a - b < a := Contravariant.AddLECancellable.tsub_lt_self @[simp] theorem tsub_lt_self_iff : a - b < a ↔ 0 < a ∧ 0 < b := Contravariant.AddLECancellable.tsub_lt_self_iff /-- See `lt_tsub_iff_left_of_le_of_le` for a weaker statement in a partial order. -/ theorem tsub_lt_tsub_iff_left_of_le (h : b ≤ a) : a - b < a - c ↔ c < b := Contravariant.AddLECancellable.tsub_lt_tsub_iff_left_of_le Contravariant.AddLECancellable h lemma tsub_tsub_eq_min (a b : α) : a - (a - b) = min a b := by rw [tsub_eq_tsub_min _ b, tsub_tsub_cancel_of_le (min_le_left a _)] end Contra /-! ### Lemmas about `max` and `min`. -/ theorem tsub_add_eq_max : a - b + b = max a b := by rcases le_total a b with h | h · rw [max_eq_right h, tsub_eq_zero_of_le h, zero_add] · rw [max_eq_left h, tsub_add_cancel_of_le h] theorem add_tsub_eq_max : a + (b - a) = max a b := by rw [add_comm, max_comm, tsub_add_eq_max] theorem tsub_min : a - min a b = a - b := by rcases le_total a b with h | h · rw [min_eq_left h, tsub_self, tsub_eq_zero_of_le h] · rw [min_eq_right h] theorem tsub_add_min : a - b + min a b = a := by rw [← tsub_min, @tsub_add_cancel_of_le] apply min_le_left -- `Odd.tsub` requires `CanonicallyLinearOrderedSemiring`, which we don't have lemma Even.tsub [ContravariantClass α α (· + ·) (· ≤ ·)] {m n : α} (hm : Even m) (hn : Even n) : Even (m - n) := by obtain ⟨a, rfl⟩ := hm obtain ⟨b, rfl⟩ := hn refine ⟨a - b, ?_⟩ obtain h | h := le_total a b · rw [tsub_eq_zero_of_le h, tsub_eq_zero_of_le (add_le_add h h), add_zero] · exact (tsub_add_tsub_comm h h).symm end CanonicallyLinearOrderedAddCommMonoid
Algebra\Order\Sub\Defs.lean
/- Copyright (c) 2021 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import Mathlib.Algebra.Group.Basic import Mathlib.Algebra.Order.Monoid.Unbundled.Basic import Mathlib.Order.Lattice /-! # Ordered Subtraction This file proves lemmas relating (truncated) subtraction with an order. We provide a class `OrderedSub` stating that `a - b ≤ c ↔ a ≤ c + b`. The subtraction discussed here could both be normal subtraction in an additive group or truncated subtraction on a canonically ordered monoid (`ℕ`, `Multiset`, `PartENat`, `ENNReal`, ...) ## Implementation details `OrderedSub` is a mixin type-class, so that we can use the results in this file even in cases where we don't have a `CanonicallyOrderedAddCommMonoid` instance (even though that is our main focus). Conversely, this means we can use `CanonicallyOrderedAddCommMonoid` without necessarily having to define a subtraction. The results in this file are ordered by the type-class assumption needed to prove it. This means that similar results might not be close to each other. Furthermore, we don't prove implications if a bi-implication can be proven under the same assumptions. Lemmas using this class are named using `tsub` instead of `sub` (short for "truncated subtraction"). This is to avoid naming conflicts with similar lemmas about ordered groups. We provide a second version of most results that require `[ContravariantClass α α (+) (≤)]`. In the second version we replace this type-class assumption by explicit `AddLECancellable` assumptions. TODO: maybe we should make a multiplicative version of this, so that we can replace some identical lemmas about subtraction/division in `Ordered[Add]CommGroup` with these. TODO: generalize `Nat.le_of_le_of_sub_le_sub_right`, `Nat.sub_le_sub_right_iff`, `Nat.mul_self_sub_mul_self_eq` -/ variable {α β : Type*} /-- `OrderedSub α` means that `α` has a subtraction characterized by `a - b ≤ c ↔ a ≤ c + b`. In other words, `a - b` is the least `c` such that `a ≤ b + c`. This is satisfied both by the subtraction in additive ordered groups and by truncated subtraction in canonically ordered monoids on many specific types. -/ class OrderedSub (α : Type*) [LE α] [Add α] [Sub α] : Prop where /-- `a - b` provides a lower bound on `c` such that `a ≤ c + b`. -/ tsub_le_iff_right : ∀ a b c : α, a - b ≤ c ↔ a ≤ c + b section Add @[simp] theorem tsub_le_iff_right [LE α] [Add α] [Sub α] [OrderedSub α] {a b c : α} : a - b ≤ c ↔ a ≤ c + b := OrderedSub.tsub_le_iff_right a b c variable [Preorder α] [Add α] [Sub α] [OrderedSub α] {a b c d : α} /-- See `add_tsub_cancel_right` for the equality if `ContravariantClass α α (+) (≤)`. -/ theorem add_tsub_le_right : a + b - b ≤ a := tsub_le_iff_right.mpr le_rfl theorem le_tsub_add : b ≤ b - a + a := tsub_le_iff_right.mp le_rfl end Add /-! ### Preorder -/ section OrderedAddCommSemigroup section Preorder variable [Preorder α] section AddCommSemigroup variable [AddCommSemigroup α] [Sub α] [OrderedSub α] {a b c d : α} /- TODO: Most results can be generalized to [Add α] [IsSymmOp α α (· + ·)] -/ theorem tsub_le_iff_left : a - b ≤ c ↔ a ≤ b + c := by rw [tsub_le_iff_right, add_comm] theorem le_add_tsub : a ≤ b + (a - b) := tsub_le_iff_left.mp le_rfl /-- See `add_tsub_cancel_left` for the equality if `ContravariantClass α α (+) (≤)`. -/ theorem add_tsub_le_left : a + b - a ≤ b := tsub_le_iff_left.mpr le_rfl @[gcongr] theorem tsub_le_tsub_right (h : a ≤ b) (c : α) : a - c ≤ b - c := tsub_le_iff_left.mpr <| h.trans le_add_tsub theorem tsub_le_iff_tsub_le : a - b ≤ c ↔ a - c ≤ b := by rw [tsub_le_iff_left, tsub_le_iff_right] /-- See `tsub_tsub_cancel_of_le` for the equality. -/ theorem tsub_tsub_le : b - (b - a) ≤ a := tsub_le_iff_right.mpr le_add_tsub section Cov variable [CovariantClass α α (· + ·) (· ≤ ·)] @[gcongr] theorem tsub_le_tsub_left (h : a ≤ b) (c : α) : c - b ≤ c - a := tsub_le_iff_left.mpr <| le_add_tsub.trans <| add_le_add_right h _ @[gcongr] theorem tsub_le_tsub (hab : a ≤ b) (hcd : c ≤ d) : a - d ≤ b - c := (tsub_le_tsub_right hab _).trans <| tsub_le_tsub_left hcd _ theorem antitone_const_tsub : Antitone fun x => c - x := fun _ _ hxy => tsub_le_tsub rfl.le hxy /-- See `add_tsub_assoc_of_le` for the equality. -/ theorem add_tsub_le_assoc : a + b - c ≤ a + (b - c) := by rw [tsub_le_iff_left, add_left_comm] exact add_le_add_left le_add_tsub a /-- See `tsub_add_eq_add_tsub` for the equality. -/ theorem add_tsub_le_tsub_add : a + b - c ≤ a - c + b := by rw [add_comm, add_comm _ b] exact add_tsub_le_assoc theorem add_le_add_add_tsub : a + b ≤ a + c + (b - c) := by rw [add_assoc] exact add_le_add_left le_add_tsub a theorem le_tsub_add_add : a + b ≤ a - c + (b + c) := by rw [add_comm a, add_comm (a - c)] exact add_le_add_add_tsub theorem tsub_le_tsub_add_tsub : a - c ≤ a - b + (b - c) := by rw [tsub_le_iff_left, ← add_assoc, add_right_comm] exact le_add_tsub.trans (add_le_add_right le_add_tsub _) theorem tsub_tsub_tsub_le_tsub : c - a - (c - b) ≤ b - a := by rw [tsub_le_iff_left, tsub_le_iff_left, add_left_comm] exact le_tsub_add.trans (add_le_add_left le_add_tsub _) theorem tsub_tsub_le_tsub_add {a b c : α} : a - (b - c) ≤ a - b + c := tsub_le_iff_right.2 <| calc a ≤ a - b + b := le_tsub_add _ ≤ a - b + (c + (b - c)) := add_le_add_left le_add_tsub _ _ = a - b + c + (b - c) := (add_assoc _ _ _).symm /-- See `tsub_add_tsub_comm` for the equality. -/ theorem add_tsub_add_le_tsub_add_tsub : a + b - (c + d) ≤ a - c + (b - d) := by rw [add_comm c, tsub_le_iff_left, add_assoc, ← tsub_le_iff_left, ← tsub_le_iff_left] refine (tsub_le_tsub_right add_tsub_le_assoc c).trans ?_ rw [add_comm a, add_comm (a - c)] exact add_tsub_le_assoc /-- See `add_tsub_add_eq_tsub_left` for the equality. -/ theorem add_tsub_add_le_tsub_left : a + b - (a + c) ≤ b - c := by rw [tsub_le_iff_left, add_assoc] exact add_le_add_left le_add_tsub _ /-- See `add_tsub_add_eq_tsub_right` for the equality. -/ theorem add_tsub_add_le_tsub_right : a + c - (b + c) ≤ a - b := by rw [tsub_le_iff_left, add_right_comm] exact add_le_add_right le_add_tsub c end Cov /-! #### Lemmas that assume that an element is `AddLECancellable` -/ namespace AddLECancellable protected theorem le_add_tsub_swap (hb : AddLECancellable b) : a ≤ b + a - b := hb le_add_tsub protected theorem le_add_tsub (hb : AddLECancellable b) : a ≤ a + b - b := by rw [add_comm] exact hb.le_add_tsub_swap protected theorem le_tsub_of_add_le_left (ha : AddLECancellable a) (h : a + b ≤ c) : b ≤ c - a := ha <| h.trans le_add_tsub protected theorem le_tsub_of_add_le_right (hb : AddLECancellable b) (h : a + b ≤ c) : a ≤ c - b := hb.le_tsub_of_add_le_left <| by rwa [add_comm] end AddLECancellable /-! ### Lemmas where addition is order-reflecting -/ section Contra variable [ContravariantClass α α (· + ·) (· ≤ ·)] theorem le_add_tsub_swap : a ≤ b + a - b := Contravariant.AddLECancellable.le_add_tsub_swap theorem le_add_tsub' : a ≤ a + b - b := Contravariant.AddLECancellable.le_add_tsub theorem le_tsub_of_add_le_left (h : a + b ≤ c) : b ≤ c - a := Contravariant.AddLECancellable.le_tsub_of_add_le_left h theorem le_tsub_of_add_le_right (h : a + b ≤ c) : a ≤ c - b := Contravariant.AddLECancellable.le_tsub_of_add_le_right h end Contra end AddCommSemigroup variable [AddCommMonoid α] [Sub α] [OrderedSub α] {a b c d : α} theorem tsub_nonpos : a - b ≤ 0 ↔ a ≤ b := by rw [tsub_le_iff_left, add_zero] alias ⟨_, tsub_nonpos_of_le⟩ := tsub_nonpos end Preorder /-! ### Partial order -/ variable [PartialOrder α] [AddCommSemigroup α] [Sub α] [OrderedSub α] {a b c d : α} theorem tsub_tsub (b a c : α) : b - a - c = b - (a + c) := by apply le_antisymm · rw [tsub_le_iff_left, tsub_le_iff_left, ← add_assoc, ← tsub_le_iff_left] · rw [tsub_le_iff_left, add_assoc, ← tsub_le_iff_left, ← tsub_le_iff_left] theorem tsub_add_eq_tsub_tsub (a b c : α) : a - (b + c) = a - b - c := (tsub_tsub _ _ _).symm theorem tsub_add_eq_tsub_tsub_swap (a b c : α) : a - (b + c) = a - c - b := by rw [add_comm] apply tsub_add_eq_tsub_tsub theorem tsub_right_comm : a - b - c = a - c - b := by rw [← tsub_add_eq_tsub_tsub, tsub_add_eq_tsub_tsub_swap] /-! ### Lemmas that assume that an element is `AddLECancellable`. -/ namespace AddLECancellable protected theorem tsub_eq_of_eq_add (hb : AddLECancellable b) (h : a = c + b) : a - b = c := le_antisymm (tsub_le_iff_right.mpr h.le) <| by rw [h] exact hb.le_add_tsub protected theorem eq_tsub_of_add_eq (hc : AddLECancellable c) (h : a + c = b) : a = b - c := (hc.tsub_eq_of_eq_add h.symm).symm protected theorem tsub_eq_of_eq_add_rev (hb : AddLECancellable b) (h : a = b + c) : a - b = c := hb.tsub_eq_of_eq_add <| by rw [add_comm, h] @[simp] protected theorem add_tsub_cancel_right (hb : AddLECancellable b) : a + b - b = a := hb.tsub_eq_of_eq_add <| by rw [add_comm] @[simp] protected theorem add_tsub_cancel_left (ha : AddLECancellable a) : a + b - a = b := ha.tsub_eq_of_eq_add <| add_comm a b protected theorem lt_add_of_tsub_lt_left (hb : AddLECancellable b) (h : a - b < c) : a < b + c := by rw [lt_iff_le_and_ne, ← tsub_le_iff_left] refine ⟨h.le, ?_⟩ rintro rfl simp [hb] at h protected theorem lt_add_of_tsub_lt_right (hc : AddLECancellable c) (h : a - c < b) : a < b + c := by rw [lt_iff_le_and_ne, ← tsub_le_iff_right] refine ⟨h.le, ?_⟩ rintro rfl simp [hc] at h protected theorem lt_tsub_of_add_lt_right (hc : AddLECancellable c) (h : a + c < b) : a < b - c := (hc.le_tsub_of_add_le_right h.le).lt_of_ne <| by rintro rfl exact h.not_le le_tsub_add protected theorem lt_tsub_of_add_lt_left (ha : AddLECancellable a) (h : a + c < b) : c < b - a := ha.lt_tsub_of_add_lt_right <| by rwa [add_comm] end AddLECancellable /-! #### Lemmas where addition is order-reflecting. -/ section Contra variable [ContravariantClass α α (· + ·) (· ≤ ·)] theorem tsub_eq_of_eq_add (h : a = c + b) : a - b = c := Contravariant.AddLECancellable.tsub_eq_of_eq_add h theorem eq_tsub_of_add_eq (h : a + c = b) : a = b - c := Contravariant.AddLECancellable.eq_tsub_of_add_eq h theorem tsub_eq_of_eq_add_rev (h : a = b + c) : a - b = c := Contravariant.AddLECancellable.tsub_eq_of_eq_add_rev h @[simp] theorem add_tsub_cancel_right (a b : α) : a + b - b = a := Contravariant.AddLECancellable.add_tsub_cancel_right @[simp] theorem add_tsub_cancel_left (a b : α) : a + b - a = b := Contravariant.AddLECancellable.add_tsub_cancel_left /-- A more general version of the reverse direction of `sub_eq_sub_iff_add_eq_add` -/ theorem tsub_eq_tsub_of_add_eq_add (h : a + d = c + b) : a - b = c - d := by calc a - b = a + d - d - b := by rw [add_tsub_cancel_right] _ = c + b - b - d := by rw [h, tsub_right_comm] _ = c - d := by rw [add_tsub_cancel_right] theorem lt_add_of_tsub_lt_left (h : a - b < c) : a < b + c := Contravariant.AddLECancellable.lt_add_of_tsub_lt_left h theorem lt_add_of_tsub_lt_right (h : a - c < b) : a < b + c := Contravariant.AddLECancellable.lt_add_of_tsub_lt_right h /-- This lemma (and some of its corollaries) also holds for `ENNReal`, but this proof doesn't work for it. Maybe we should add this lemma as field to `OrderedSub`? -/ theorem lt_tsub_of_add_lt_left : a + c < b → c < b - a := Contravariant.AddLECancellable.lt_tsub_of_add_lt_left theorem lt_tsub_of_add_lt_right : a + c < b → a < b - c := Contravariant.AddLECancellable.lt_tsub_of_add_lt_right end Contra section Both variable [CovariantClass α α (· + ·) (· ≤ ·)] [ContravariantClass α α (· + ·) (· ≤ ·)] theorem add_tsub_add_eq_tsub_right (a c b : α) : a + c - (b + c) = a - b := by refine add_tsub_add_le_tsub_right.antisymm (tsub_le_iff_right.2 <| ?_) apply le_of_add_le_add_right rw [add_assoc] exact le_tsub_add theorem add_tsub_add_eq_tsub_left (a b c : α) : a + b - (a + c) = b - c := by rw [add_comm a b, add_comm a c, add_tsub_add_eq_tsub_right] end Both end OrderedAddCommSemigroup /-! ### Lemmas in a linearly ordered monoid. -/ section LinearOrder variable {a b c d : α} [LinearOrder α] [AddCommSemigroup α] [Sub α] [OrderedSub α] /-- See `lt_of_tsub_lt_tsub_right_of_le` for a weaker statement in a partial order. -/ theorem lt_of_tsub_lt_tsub_right (h : a - c < b - c) : a < b := lt_imp_lt_of_le_imp_le (fun h => tsub_le_tsub_right h c) h /-- See `lt_tsub_iff_right_of_le` for a weaker statement in a partial order. -/ theorem lt_tsub_iff_right : a < b - c ↔ a + c < b := lt_iff_lt_of_le_iff_le tsub_le_iff_right /-- See `lt_tsub_iff_left_of_le` for a weaker statement in a partial order. -/ theorem lt_tsub_iff_left : a < b - c ↔ c + a < b := lt_iff_lt_of_le_iff_le tsub_le_iff_left theorem lt_tsub_comm : a < b - c ↔ c < b - a := lt_tsub_iff_left.trans lt_tsub_iff_right.symm section Cov variable [CovariantClass α α (· + ·) (· ≤ ·)] /-- See `lt_of_tsub_lt_tsub_left_of_le` for a weaker statement in a partial order. -/ theorem lt_of_tsub_lt_tsub_left (h : a - b < a - c) : c < b := lt_imp_lt_of_le_imp_le (fun h => tsub_le_tsub_left h a) h end Cov end LinearOrder section OrderedAddCommMonoid variable [PartialOrder α] [AddCommMonoid α] [Sub α] [OrderedSub α] @[simp] theorem tsub_zero (a : α) : a - 0 = a := AddLECancellable.tsub_eq_of_eq_add addLECancellable_zero (add_zero _).symm end OrderedAddCommMonoid
Algebra\Order\Sub\Prod.lean
/- Copyright (c) 2023 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import Mathlib.Algebra.Group.Prod import Mathlib.Algebra.Order.Sub.Defs /-! # Products of `OrderedSub` types. -/ assert_not_exists MonoidWithZero variable {α β : Type*} instance Prod.orderedSub [Preorder α] [Add α] [Sub α] [OrderedSub α] [Sub β] [Preorder β] [Add β] [OrderedSub β] : OrderedSub (α × β) where tsub_le_iff_right _ _ _ := ⟨fun w ↦ ⟨tsub_le_iff_right.mp w.1, tsub_le_iff_right.mp w.2⟩, fun w ↦ ⟨tsub_le_iff_right.mpr w.1, tsub_le_iff_right.mpr w.2⟩⟩ instance Pi.orderedSub {ι : Type*} {α : ι → Type*} [∀ i, Preorder (α i)] [∀ i, Add (α i)] [∀ i, Sub (α i)] [∀ i, OrderedSub (α i)] : OrderedSub ((i : ι) → α i) where tsub_le_iff_right _ _ _ := ⟨fun w i ↦ tsub_le_iff_right.mp (w i), fun w i ↦ tsub_le_iff_right.mpr (w i)⟩
Algebra\Order\Sub\WithTop.lean
/- Copyright (c) 2021 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import Mathlib.Algebra.Order.Sub.Defs import Mathlib.Algebra.Order.Monoid.Canonical.Defs import Mathlib.Algebra.Order.Monoid.Unbundled.WithTop /-! # Lemma about subtraction in ordered monoids with a top element adjoined. This file introduces a subtraction on `WithTop α` when `α` has a subtraction and a bottom element, given by `x - ⊤ = ⊥` and `⊤ - x = ⊤`. This will be instantiated mostly for `ℕ∞` and `ℝ≥0∞`, where the bottom element is zero. Note that there is another subtraction on objects of the form `WithTop α` in the file `Mathlib.Algebra.Order.Group.WithTop`, setting `-⊤ = ⊤` as this corresponds to the additivization of the usual convention `0⁻¹ = 0` and is relevant in valuation theory. Since this other instance is only registered for `LinearOrderedAddCommGroup α` (which doesn't have a bottom element, unless the group is trivial), this shouldn't create diamonds. -/ variable {α β : Type*} namespace WithTop section variable [Sub α] [Bot α] /-- If `α` has a subtraction and a bottom element, we can extend the subtraction to `WithTop α`, by setting `x - ⊤ = ⊥` and `⊤ - x = ⊤`. -/ protected def sub : ∀ _ _ : WithTop α, WithTop α | _, ⊤ => (⊥ : α) | ⊤, (x : α) => ⊤ | (x : α), (y : α) => (x - y : α) instance : Sub (WithTop α) := ⟨WithTop.sub⟩ @[simp, norm_cast] theorem coe_sub {a b : α} : (↑(a - b) : WithTop α) = ↑a - ↑b := rfl @[simp] theorem top_sub_coe {a : α} : (⊤ : WithTop α) - a = ⊤ := rfl @[simp] theorem sub_top {a : WithTop α} : a - ⊤ = (⊥ : α) := by cases a <;> rfl @[simp] theorem sub_eq_top_iff {a b : WithTop α} : a - b = ⊤ ↔ a = ⊤ ∧ b ≠ ⊤ := by induction a <;> induction b <;> simp only [← coe_sub, coe_ne_top, sub_top, zero_ne_top, top_sub_coe, false_and, Ne, not_true_eq_false, not_false_eq_true, and_false, and_self] theorem map_sub [Sub β] [Bot β] {f : α → β} (h : ∀ x y, f (x - y) = f x - f y) (h₀ : f ⊥ = ⊥) : ∀ x y : WithTop α, (x - y).map f = x.map f - y.map f | _, ⊤ => by simp only [sub_top, map_coe, h₀, map_top] | ⊤, (x : α) => rfl | (x : α), (y : α) => by simp only [← coe_sub, map_coe, h] end variable [CanonicallyOrderedAddCommMonoid α] [Sub α] [OrderedSub α] instance : OrderedSub (WithTop α) := by constructor rintro x y z cases y · cases z <;> simp cases x · simp cases z · simp norm_cast; exact tsub_le_iff_right end WithTop
Algebra\Pointwise\Stabilizer.lean
/- Copyright (c) 2023 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import Mathlib.Data.Finset.Pointwise import Mathlib.GroupTheory.QuotientGroup /-! # 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*} [Group G] [Group H] [MulAction G α] {a : G} /-! ### Stabilizer of a set -/ section 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, exists_prop, 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 end Set /-! ### 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? says simp only [smul_eq_mul_unop, SetLike.mem_coe, Subgroup.mem_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)] 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 -/ @[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
Algebra\Polynomial\AlgebraMap.lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker -/ import Mathlib.Algebra.Algebra.Pi import Mathlib.Algebra.Polynomial.Eval import Mathlib.RingTheory.Adjoin.Basic /-! # 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`. -/ 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 toRingHom := 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) 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 } variable {R} 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_iff, Algebra.mem_top, algebraMap_apply, not_forall] intro x rw [ext_iff, not_forall] refine ⟨1, ?_⟩ simp [coeff_C]⟩⟩ @[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, eq_intCast, map_intCast, 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, eq_intCast, map_intCast] 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 @[deprecated (since := "2024-05-27")] alias ringHom_eval₂_cast_int_ringHom := ringHom_eval₂_intCastRingHom @[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 @[deprecated (since := "2024-04-17")] alias eval₂_int_castRingHom_X := eval₂_intCastRingHom_X /-- `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 z) [Semiring C] [Algebra R C] (f : B →ₐ[R] C) (g : A →ₐ[R] B) : (mapAlgHom f).comp (mapAlgHom g) = mapAlgHom (f.comp g) := by apply AlgHom.ext intro x simp [AlgHom.comp_algebraMap, map_map] congr 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 apply AlgHom.ext intro x congr /-- 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_comp (C : Type z) [Semiring C] [Algebra R C] (f : A ≃ₐ[R] B) (g : B ≃ₐ[R] C) : (mapAlgEquiv f).trans (mapAlgEquiv g) = mapAlgEquiv (f.trans g) := by apply AlgEquiv.ext intro x simp [AlgEquiv.trans_apply, map_map] congr 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 · _) @[simp] theorem adjoin_X : Algebra.adjoin R ({X} : Set R[X]) = ⊤ := by refine top_unique fun p _hp => ?_ set S := Algebra.adjoin R ({X} : Set R[X]) rw [← sum_monomial_eq p]; simp only [← smul_X_eq_monomial, Sum] exact S.sum_mem fun n _hn => S.smul_mem (S.pow_mem (Algebra.subset_adjoin rfl) _) _ @[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 -- Porting note: removed `@[simp]` because `simp` can prove this 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 _ _ -- Porting note: removed `@[simp]` because `simp` can prove this theorem aeval_X_pow {n : ℕ} : aeval x ((X : R[X]) ^ n) = x ^ n := eval₂_X_pow _ _ -- Porting note: removed `@[simp]` because `simp` can prove this theorem aeval_add : aeval x (p + q) = aeval x p + aeval x q := map_add _ _ _ -- Porting note: removed `@[simp]` because `simp` can prove this theorem aeval_one : aeval x (1 : R[X]) = 1 := map_one _ -- Porting note: removed `@[simp]` because `simp` can prove this theorem aeval_natCast (n : ℕ) : aeval x (n : R[X]) = n := map_natCast _ _ @[deprecated (since := "2024-04-17")] alias aeval_nat_cast := aeval_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 /-- 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] /-- The automorphism of the polynomial algebra given by `p(X) ↦ p(X+t)`, with inverse `p(X) ↦ p(X-t)`. -/ @[simps!] def algEquivAevalXAddC {R} [CommRing R] (t : R) : R[X] ≃ₐ[R] R[X] := algEquivOfCompEqX (X + C t) (X - C t) (by simp) (by simp) 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 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 @[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₂ variable (R) theorem _root_.Algebra.adjoin_singleton_eq_range_aeval (x : A) : Algebra.adjoin R {x} = (Polynomial.aeval x).range := by rw [← Algebra.map_top, ← adjoin_X, AlgHom.map_adjoin, Set.image_singleton, aeval_X] @[simp] theorem aeval_mem_adjoin_singleton : aeval x p ∈ Algebra.adjoin R {x} := by simpa only [Algebra.adjoin_singleton_eq_range_aeval] using Set.mem_range_self p instance instCommSemiringAdjoinSingleton : CommSemiring <| Algebra.adjoin R {x} := { mul_comm := fun ⟨p, hp⟩ ⟨q, hq⟩ ↦ by obtain ⟨p', rfl⟩ := Algebra.adjoin_singleton_eq_range_aeval R x ▸ hp obtain ⟨q', rfl⟩ := Algebra.adjoin_singleton_eq_range_aeval R x ▸ hq simp only [AlgHom.toRingHom_eq_coe, RingHom.coe_coe, Submonoid.mk_mul_mk, ← map_mul, mul_comm p' q'] } instance instCommRingAdjoinSingleton {R A : Type*} [CommRing R] [Ring A] [Algebra R A] (x : A) : CommRing <| Algebra.adjoin R {x} := { mul_comm := mul_comm } variable {R} 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] {p : R[X]} (inj : Function.Injective (algebraMap R S)) {r : R} (hr : aeval (algebraMap R S r) p = 0) : p.IsRoot r := isRoot_of_eval₂_map_eq_zero inj 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.not_mem_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 [not_mem_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 := add_le_add_left (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 [coeff_monomial] theorem not_isUnit_X_sub_C [Nontrivial R] (r : R) : ¬IsUnit (X - C r) := fun ⟨⟨_, g, _hfg, hgf⟩, rfl⟩ => zero_ne_one' R <| by erw [← eval_mul_X_sub_C, hgf, eval_one] end Ring theorem aeval_endomorphism {M : Type*} [CommRing R] [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) _ _ 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 refine p.induction_on (M := fun f ↦ aeval a f • m ∈ q) (by simpa) (fun f₁ f₂ h₁ h₂ ↦ ?_) (fun n t hmq ↦ ?_) · simp_rw [map_add, add_smul] exact Submodule.add_mem q h₁ h₂ · dsimp only at hmq ⊢ rw [pow_succ', mul_left_comm, map_mul, aeval_X, mul_smul] rw [← q.map_le_iff_le_comap] at hq exact hq ⟨_, hmq, rfl⟩ 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 end Polynomial
Algebra\Polynomial\Basic.lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker -/ import Mathlib.Algebra.GroupWithZero.Divisibility import Mathlib.Algebra.MonoidAlgebra.Basic import Mathlib.Data.Finset.Sort /-! # Theory of univariate polynomials This file defines `Polynomial R`, the type of univariate polynomials over the semiring `R`, builds a semiring structure on it, and gives basic definitions that are expanded in other files in this directory. ## Main definitions * `monomial n a` is the polynomial `a X^n`. Note that `monomial n` is defined as an `R`-linear map. * `C a` is the constant polynomial `a`. Note that `C` is defined as a ring homomorphism. * `X` is the polynomial `X`, i.e., `monomial 1 1`. * `p.sum f` is `∑ n ∈ p.support, f n (p.coeff n)`, i.e., one sums the values of functions applied to coefficients of the polynomial `p`. * `p.erase n` is the polynomial `p` in which one removes the `c X^n` term. There are often two natural variants of lemmas involving sums, depending on whether one acts on the polynomials, or on the function. The naming convention is that one adds `index` when acting on the polynomials. For instance, * `sum_add_index` states that `(p + q).sum f = p.sum f + q.sum f`; * `sum_add` states that `p.sum (fun n x ↦ f n x + g n x) = p.sum f + p.sum g`. * Notation to refer to `Polynomial R`, as `R[X]` or `R[t]`. ## Implementation Polynomials are defined using `R[ℕ]`, where `R` is a semiring. The variable `X` commutes with every polynomial `p`: lemma `X_mul` proves the identity `X * p = p * X`. The relationship to `R[ℕ]` is through a structure to make polynomials irreducible from the point of view of the kernel. Most operations are irreducible since Lean can not compute anyway with `AddMonoidAlgebra`. There are two exceptions that we make semireducible: * The zero polynomial, so that its coefficients are definitionally equal to `0`. * The scalar action, to permit typeclass search to unfold it to resolve potential instance diamonds. The raw implementation of the equivalence between `R[X]` and `R[ℕ]` is done through `ofFinsupp` and `toFinsupp` (or, equivalently, `rcases p` when `p` is a polynomial gives an element `q` of `R[ℕ]`, and conversely `⟨q⟩` gives back `p`). The equivalence is also registered as a ring equiv in `Polynomial.toFinsuppIso`. These should in general not be used once the basic API for polynomials is constructed. -/ noncomputable section /-- `Polynomial R` is the type of univariate polynomials over `R`. Polynomials should be seen as (semi-)rings with the additional constructor `X`. The embedding from `R` is called `C`. -/ structure Polynomial (R : Type*) [Semiring R] where ofFinsupp :: toFinsupp : AddMonoidAlgebra R ℕ @[inherit_doc] scoped[Polynomial] notation:9000 R "[X]" => Polynomial R open AddMonoidAlgebra open Finsupp hiding single open Function hiding Commute open Polynomial namespace Polynomial universe u variable {R : Type u} {a b : R} {m n : ℕ} section Semiring variable [Semiring R] {p q : R[X]} theorem forall_iff_forall_finsupp (P : R[X] → Prop) : (∀ p, P p) ↔ ∀ q : R[ℕ], P ⟨q⟩ := ⟨fun h q => h ⟨q⟩, fun h ⟨p⟩ => h p⟩ theorem exists_iff_exists_finsupp (P : R[X] → Prop) : (∃ p, P p) ↔ ∃ q : R[ℕ], P ⟨q⟩ := ⟨fun ⟨⟨p⟩, hp⟩ => ⟨p, hp⟩, fun ⟨q, hq⟩ => ⟨⟨q⟩, hq⟩⟩ @[simp] theorem eta (f : R[X]) : Polynomial.ofFinsupp f.toFinsupp = f := by cases f; rfl /-! ### Conversions to and from `AddMonoidAlgebra` Since `R[X]` is not defeq to `R[ℕ]`, but instead is a structure wrapping it, we have to copy across all the arithmetic operators manually, along with the lemmas about how they unfold around `Polynomial.ofFinsupp` and `Polynomial.toFinsupp`. -/ section AddMonoidAlgebra private irreducible_def add : R[X] → R[X] → R[X] | ⟨a⟩, ⟨b⟩ => ⟨a + b⟩ private irreducible_def neg {R : Type u} [Ring R] : R[X] → R[X] | ⟨a⟩ => ⟨-a⟩ private irreducible_def mul : R[X] → R[X] → R[X] | ⟨a⟩, ⟨b⟩ => ⟨a * b⟩ instance zero : Zero R[X] := ⟨⟨0⟩⟩ instance one : One R[X] := ⟨⟨1⟩⟩ instance add' : Add R[X] := ⟨add⟩ instance neg' {R : Type u} [Ring R] : Neg R[X] := ⟨neg⟩ instance sub {R : Type u} [Ring R] : Sub R[X] := ⟨fun a b => a + -b⟩ instance mul' : Mul R[X] := ⟨mul⟩ -- If the private definitions are accidentally exposed, simplify them away. @[simp] theorem add_eq_add : add p q = p + q := rfl @[simp] theorem mul_eq_mul : mul p q = p * q := rfl instance 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) -- 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_smul {S : Type*} [SMulZeroClass S R] (a : S) (b) : (⟨a • b⟩ : R[X]) = (a • ⟨b⟩ : R[X]) := rfl @[simp] theorem ofFinsupp_pow (a) (n : ℕ) : (⟨a ^ n⟩ : R[X]) = ⟨a⟩ ^ n := by change _ = npowRec n _ induction n with | zero => simp [npowRec] | succ n n_ih => simp [npowRec, n_ih, pow_succ] @[simp] theorem toFinsupp_zero : (0 : R[X]).toFinsupp = 0 := rfl @[simp] theorem toFinsupp_one : (1 : R[X]).toFinsupp = 1 := rfl @[simp] theorem toFinsupp_add (a b : R[X]) : (a + b).toFinsupp = a.toFinsupp + b.toFinsupp := by cases a cases b rw [← ofFinsupp_add] @[simp] theorem toFinsupp_neg {R : Type u} [Ring R] (a : R[X]) : (-a).toFinsupp = -a.toFinsupp := by cases a rw [← ofFinsupp_neg] @[simp] theorem toFinsupp_sub {R : Type u} [Ring R] (a b : R[X]) : (a - b).toFinsupp = a.toFinsupp - b.toFinsupp := by rw [sub_eq_add_neg, ← toFinsupp_neg, ← toFinsupp_add] rfl @[simp] theorem toFinsupp_mul (a b : R[X]) : (a * b).toFinsupp = a.toFinsupp * b.toFinsupp := by cases a cases b rw [← ofFinsupp_mul] @[simp] theorem toFinsupp_smul {S : Type*} [SMulZeroClass S R] (a : S) (b : R[X]) : (a • b).toFinsupp = a • b.toFinsupp := rfl @[simp] theorem toFinsupp_pow (a : R[X]) (n : ℕ) : (a ^ n).toFinsupp = a.toFinsupp ^ n := by cases a rw [← ofFinsupp_pow] theorem _root_.IsSMulRegular.polynomial {S : Type*} [Monoid S] [DistribMulAction 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 instance semiring : Semiring R[X] := --TODO: add reference to library note in PR #7432 { Function.Injective.semiring toFinsupp toFinsupp_injective toFinsupp_zero toFinsupp_one toFinsupp_add toFinsupp_mul (fun _ _ => toFinsupp_smul _ _) toFinsupp_pow fun _ => rfl with toAdd := Polynomial.add' toMul := Polynomial.mul' toZero := Polynomial.zero toOne := Polynomial.one nsmul := (· • ·) npow := fun n x => (x ^ n) } instance distribSMul {S} [DistribSMul S R] : DistribSMul S R[X] := --TODO: add reference to library note in PR #7432 { Function.Injective.distribSMul ⟨⟨toFinsupp, toFinsupp_zero⟩, toFinsupp_add⟩ toFinsupp_injective toFinsupp_smul with toSMulZeroClass := Polynomial.smulZeroClass } instance distribMulAction {S} [Monoid S] [DistribMulAction S R] : DistribMulAction S R[X] := --TODO: add reference to library note in PR #7432 { Function.Injective.distribMulAction ⟨⟨toFinsupp, toFinsupp_zero (R := R)⟩, toFinsupp_add⟩ toFinsupp_injective toFinsupp_smul with toSMul := Polynomial.smulZeroClass.toSMul } 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] := --TODO: add reference to library note in PR #7432 { Function.Injective.module _ ⟨⟨toFinsupp, toFinsupp_zero⟩, toFinsupp_add⟩ toFinsupp_injective toFinsupp_smul with toDistribMulAction := Polynomial.distribMulAction } instance smulCommClass {S₁ S₂} [SMulZeroClass S₁ R] [SMulZeroClass S₂ R] [SMulCommClass S₁ S₂ R] : SMulCommClass S₁ S₂ R[X] := ⟨by rintro m n ⟨f⟩ simp_rw [← ofFinsupp_smul, smul_comm m n f]⟩ instance isScalarTower {S₁ S₂} [SMul S₁ S₂] [SMulZeroClass S₁ R] [SMulZeroClass S₂ R] [IsScalarTower S₁ S₂ R] : IsScalarTower S₁ S₂ R[X] := ⟨by rintro _ _ ⟨⟩ simp_rw [← ofFinsupp_smul, smul_assoc]⟩ instance isScalarTower_right {α K : Type*} [Semiring K] [DistribSMul α K] [IsScalarTower α K K] : IsScalarTower α K[X] K[X] := ⟨by rintro _ ⟨⟩ ⟨⟩ simp_rw [smul_eq_mul, ← ofFinsupp_smul, ← ofFinsupp_mul, ← ofFinsupp_smul, smul_mul_assoc]⟩ instance isCentralScalar {S} [SMulZeroClass S R] [SMulZeroClass Sᵐᵒᵖ R] [IsCentralScalar S R] : IsCentralScalar S R[X] := ⟨by rintro _ ⟨⟩ simp_rw [← ofFinsupp_smul, op_smul_eq_smul]⟩ instance unique [Subsingleton R] : Unique R[X] := { Polynomial.inhabited with uniq := by rintro ⟨x⟩ apply congr_arg ofFinsupp simp [eq_iff_true_of_subsingleton] } variable (R) /-- Ring isomorphism between `R[X]` and `R[ℕ]`. This is just an implementation detail, but it can be useful to transfer results from `Finsupp` to polynomials. -/ @[simps apply symm_apply] def toFinsuppIso : R[X] ≃+* R[ℕ] where toFun := toFinsupp invFun := ofFinsupp left_inv := fun ⟨_p⟩ => rfl right_inv _p := rfl map_mul' := toFinsupp_mul map_add' := toFinsupp_add instance [DecidableEq R] : DecidableEq R[X] := @Equiv.decidableEq R[X] _ (toFinsuppIso R).toEquiv (Finsupp.instDecidableEq) 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. -/ -- @[simp] -- Porting note: The original generated theorem is same to `support_ofFinsupp` and -- the new generated theorem is different, so this attribute should be -- removed. 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.card = 0 ↔ p = 0 := by simp /-- `monomial s a` is the monomial `a * X^s` -/ def monomial (n : ℕ) : R →ₗ[R] R[X] where toFun t := ⟨Finsupp.single n t⟩ -- porting note (#10745): was `simp`. map_add' x y := by simp; rw [ofFinsupp_add] -- porting note (#10745): was `simp [← ofFinsupp_smul]`. map_smul' r x := by simp; rw [← ofFinsupp_smul, smul_single'] @[simp] theorem toFinsupp_monomial (n : ℕ) (r : R) : (monomial n r).toFinsupp = Finsupp.single n r := by simp [monomial] @[simp] theorem ofFinsupp_single (n : ℕ) (r : R) : (⟨Finsupp.single n r⟩ : R[X]) = monomial n r := by simp [monomial] -- @[simp] -- Porting note (#10618): simp can prove this theorem monomial_zero_right (n : ℕ) : monomial n (0 : R) = 0 := (monomial n).map_zero -- This is not a `simp` lemma as `monomial_zero_left` is more general. theorem monomial_zero_one : monomial 0 (1 : R) = 1 := rfl -- TODO: can't we just delete this one? theorem monomial_add (n : ℕ) (r s : R) : monomial n (r + s) = monomial n r + monomial n s := (monomial n).map_add _ _ theorem monomial_mul_monomial (n m : ℕ) (r s : R) : monomial n r * monomial m s = monomial (n + m) (r * s) := toFinsupp_injective <| by simp only [toFinsupp_monomial, toFinsupp_mul, AddMonoidAlgebra.single_mul_single] @[simp] theorem monomial_pow (n : ℕ) (r : R) (k : ℕ) : monomial n r ^ k = monomial (n * k) (r ^ k) := by induction' k with k ih · simp [pow_zero, monomial_zero_one] · 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 <| by simp; rw [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 support_add : (p + q).support ⊆ p.support ∪ q.support := by simpa [support] using Finsupp.support_add /-- `C a` is the constant polynomial `a`. `C` is provided as a ring homomorphism. -/ def C : R →+* R[X] := { monomial 0 with map_one' := by simp [monomial_zero_one] map_mul' := by simp [monomial_mul_monomial] map_zero' := by simp } @[simp] theorem monomial_zero_left (a : R) : monomial 0 a = C a := rfl @[simp] theorem toFinsupp_C (a : R) : (C a).toFinsupp = single 0 a := rfl theorem C_0 : C (0 : R) = 0 := by simp theorem C_1 : C (1 : R) = 1 := rfl theorem C_mul : C (a * b) = C a * C b := C.map_mul a b theorem C_add : C (a + b) = C a + C b := C.map_add a b @[simp] theorem smul_C {S} [SMulZeroClass S R] (s : S) (r : R) : s • C r = C (s • r) := smul_monomial _ _ r theorem C_pow : C (a ^ n) = C a ^ n := C.map_pow a n -- @[simp] -- Porting note (#10618): simp can prove this theorem C_eq_natCast (n : ℕ) : C (n : R) = (n : R[X]) := map_natCast C n @[deprecated (since := "2024-04-17")] alias C_eq_nat_cast := C_eq_natCast @[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 n ih · simp [monomial_zero_one] · 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 /-- `X` commutes with everything, even when the coefficients are noncommutative. -/ theorem X_mul : X * p = p * X := by rcases p with ⟨⟩ -- Porting note: `ofFinsupp.injEq` is required. simp only [X, ← ofFinsupp_single, ← ofFinsupp_mul, LinearMap.coe_mk, ofFinsupp.injEq] -- Porting note: Was `ext`. refine Finsupp.ext fun _ => ?_ simp [AddMonoidAlgebra.mul_apply, AddMonoidAlgebra.sum_single_index, add_comm] theorem X_pow_mul {n : ℕ} : X ^ n * p = p * X ^ n := by induction' n with n ih · simp · 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 erw [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 k ih · simp · 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`. -/ -- @[simp] -- Porting note: The original generated theorem is same to `coeff_ofFinsupp` and -- the new generated theorem is different, so this attribute should be -- removed. 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⟩ -- Porting note: `ofFinsupp.injEq` is required. 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_zero (n : ℕ) : coeff (0 : R[X]) n = 0 := rfl theorem coeff_one {n : ℕ} : coeff (1 : R[X]) n = if n = 0 then 1 else 0 := by simp_rw [eq_comm (a := n) (b := 0)] exact coeff_monomial @[simp] theorem coeff_one_zero : coeff (1 : R[X]) 0 = 1 := by simp [coeff_one] @[simp] theorem coeff_X_one : coeff (X : R[X]) 1 = 1 := coeff_monomial @[simp] theorem coeff_X_zero : coeff (X : R[X]) 0 = 0 := coeff_monomial @[simp] theorem coeff_monomial_succ : coeff (monomial (n + 1) a) 0 = 0 := by simp [coeff_monomial] theorem coeff_X : coeff (X : R[X]) n = if 1 = n then 1 else 0 := coeff_monomial theorem coeff_X_of_ne_one {n : ℕ} (hn : n ≠ 1) : coeff (X : R[X]) n = 0 := by rw [coeff_X, if_neg hn.symm] @[simp] theorem mem_support_iff : n ∈ p.support ↔ p.coeff n ≠ 0 := by rcases p with ⟨⟩ simp theorem not_mem_support_iff : n ∉ p.support ↔ p.coeff n = 0 := by simp theorem coeff_C : coeff (C a) n = ite (n = 0) a 0 := by convert coeff_monomial (a := a) (m := n) (n := 0) using 2 simp [eq_comm] @[simp] theorem coeff_C_zero : coeff (C a) 0 = a := coeff_monomial theorem coeff_C_ne_zero (h : n ≠ 0) : (C a).coeff n = 0 := by rw [coeff_C, if_neg h] @[simp] lemma coeff_C_succ {r : R} {n : ℕ} : coeff (C r) (n + 1) = 0 := by simp [coeff_C] @[simp] theorem coeff_natCast_ite : (Nat.cast m : R[X]).coeff n = ite (n = 0) m 0 := by simp only [← C_eq_natCast, coeff_C, Nat.cast_ite, Nat.cast_zero] @[deprecated (since := "2024-04-17")] alias coeff_nat_cast_ite := coeff_natCast_ite -- See note [no_index around OfNat.ofNat] @[simp] theorem coeff_ofNat_zero (a : ℕ) [a.AtLeastTwo] : coeff (no_index (OfNat.ofNat a : R[X])) 0 = OfNat.ofNat a := coeff_monomial -- See note [no_index around OfNat.ofNat] @[simp] theorem coeff_ofNat_succ (a n : ℕ) [h : a.AtLeastTwo] : coeff (no_index (OfNat.ofNat a : R[X])) (n + 1) = 0 := by rw [← Nat.cast_eq_ofNat] simp 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⟩ -- porting note (#10745): was `simp [coeff, DFunLike.ext_iff]` 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*} [AddMonoid 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*} [AddMonoid 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_mul_X {c : R} (h : c ≠ 0) : Polynomial.support (C c * X) = singleton 1 := by rw [C_mul_X_eq_monomial, support_monomial 1 h] theorem support_C_mul_X' (c : R) : Polynomial.support (C c * X) ⊆ singleton 1 := by simpa only [C_mul_X_eq_monomial] using support_monomial' 1 c theorem support_C_mul_X_pow (n : ℕ) {c : R} (h : c ≠ 0) : Polynomial.support (C c * X ^ n) = singleton n := by rw [C_mul_X_pow_eq_monomial, support_monomial n h] theorem support_C_mul_X_pow' (n : ℕ) (c : R) : Polynomial.support (C c * X ^ n) ⊆ singleton n := by simpa only [C_mul_X_pow_eq_monomial] using support_monomial' n c open Finset theorem support_binomial' (k m : ℕ) (x y : R) : Polynomial.support (C x * X ^ k + C y * X ^ m) ⊆ {k, m} := support_add.trans (union_subset ((support_C_mul_X_pow' k x).trans (singleton_subset_iff.mpr (mem_insert_self k {m}))) ((support_C_mul_X_pow' m y).trans (singleton_subset_iff.mpr (mem_insert_of_mem (mem_singleton_self m))))) theorem support_trinomial' (k m n : ℕ) (x y z : R) : Polynomial.support (C x * X ^ k + C y * X ^ m + C z * X ^ n) ⊆ {k, m, n} := support_add.trans (union_subset (support_add.trans (union_subset ((support_C_mul_X_pow' k x).trans (singleton_subset_iff.mpr (mem_insert_self k {m, n}))) ((support_C_mul_X_pow' m y).trans (singleton_subset_iff.mpr (mem_insert_of_mem (mem_insert_self m {n})))))) ((support_C_mul_X_pow' n z).trans (singleton_subset_iff.mpr (mem_insert_of_mem (mem_insert_of_mem (mem_singleton_self n)))))) end Fewnomials theorem X_pow_eq_monomial (n) : X ^ n = monomial n (1 : R) := by induction' n with n hn · rw [pow_zero, monomial_zero_one] · rw [pow_succ, hn, X, monomial_mul_monomial, one_mul] @[simp high] theorem toFinsupp_X_pow (n : ℕ) : (X ^ n).toFinsupp = Finsupp.single n (1 : R) := by rw [X_pow_eq_monomial, toFinsupp_monomial] theorem smul_X_eq_monomial {n} : a • X ^ n = monomial n (a : R) := by rw [X_pow_eq_monomial, smul_monomial, smul_eq_mul, mul_one] theorem support_X_pow (H : ¬(1 : R) = 0) (n : ℕ) : (X ^ n : R[X]).support = singleton n := by convert support_monomial n H exact X_pow_eq_monomial n theorem support_X_empty (H : (1 : R) = 0) : (X : R[X]).support = ∅ := by rw [X, H, monomial_zero_right, support_zero] theorem support_X (H : ¬(1 : R) = 0) : (X : R[X]).support = singleton 1 := by rw [← pow_one X, support_X_pow H 1] theorem monomial_left_inj {a : R} (ha : a ≠ 0) {i j : ℕ} : monomial i a = monomial j a ↔ i = j := by simp only [← ofFinsupp_single, ofFinsupp.injEq, Finsupp.single_left_inj ha] theorem binomial_eq_binomial {k l m n : ℕ} {u v : R} (hu : u ≠ 0) (hv : v ≠ 0) : C u * X ^ k + C v * X ^ l = C u * X ^ m + C v * X ^ n ↔ k = m ∧ l = n ∨ u = v ∧ k = n ∧ l = m ∨ u + v = 0 ∧ k = l ∧ m = n := by simp_rw [C_mul_X_pow_eq_monomial, ← toFinsupp_inj, toFinsupp_add, toFinsupp_monomial] exact Finsupp.single_add_single_eq_single_add_single hu hv theorem natCast_mul (n : ℕ) (p : R[X]) : (n : R[X]) * p = n • p := (nsmul_eq_mul _ _).symm @[deprecated (since := "2024-04-17")] alias nat_cast_mul := natCast_mul /-- Summing the values of a function applied to the coefficients of a polynomial -/ def sum {S : Type*} [AddCommMonoid S] (p : R[X]) (f : ℕ → R → S) : S := ∑ n ∈ p.support, f n (p.coeff n) theorem sum_def {S : Type*} [AddCommMonoid S] (p : R[X]) (f : ℕ → R → S) : p.sum f = ∑ n ∈ p.support, f n (p.coeff n) := rfl theorem sum_eq_of_subset {S : Type*} [AddCommMonoid S] {p : R[X]} (f : ℕ → R → S) (hf : ∀ i, f i 0 = 0) {s : Finset ℕ} (hs : p.support ⊆ s) : p.sum f = ∑ n ∈ s, f n (p.coeff n) := Finsupp.sum_of_support_subset _ hs f (fun i _ ↦ hf i) /-- Expressing the product of two polynomials as a double sum. -/ theorem mul_eq_sum_sum : p * q = ∑ i ∈ p.support, q.sum fun j a => (monomial (i + j)) (p.coeff i * a) := by apply toFinsupp_injective rcases p with ⟨⟩; rcases q with ⟨⟩ simp_rw [sum, coeff, toFinsupp_sum, support, toFinsupp_mul, toFinsupp_monomial, AddMonoidAlgebra.mul_def, Finsupp.sum] @[simp] theorem sum_zero_index {S : Type*} [AddCommMonoid S] (f : ℕ → R → S) : (0 : R[X]).sum f = 0 := by simp [sum] @[simp] theorem sum_monomial_index {S : Type*} [AddCommMonoid S] {n : ℕ} (a : R) (f : ℕ → R → S) (hf : f n 0 = 0) : (monomial n a : R[X]).sum f = f n a := Finsupp.sum_single_index hf @[simp] theorem sum_C_index {a} {β} [AddCommMonoid β] {f : ℕ → R → β} (h : f 0 0 = 0) : (C a).sum f = f 0 a := sum_monomial_index a f h -- the assumption `hf` is only necessary when the ring is trivial @[simp] theorem sum_X_index {S : Type*} [AddCommMonoid S] {f : ℕ → R → S} (hf : f 1 0 = 0) : (X : R[X]).sum f = f 1 1 := sum_monomial_index 1 f hf theorem sum_add_index {S : Type*} [AddCommMonoid S] (p q : R[X]) (f : ℕ → R → S) (hf : ∀ i, f i 0 = 0) (h_add : ∀ a b₁ b₂, f a (b₁ + b₂) = f a b₁ + f a b₂) : (p + q).sum f = p.sum f + q.sum f := by rw [show p + q = ⟨p.toFinsupp + q.toFinsupp⟩ from add_def p q] exact Finsupp.sum_add_index (fun i _ ↦ hf i) (fun a _ b₁ b₂ ↦ h_add a b₁ b₂) theorem sum_add' {S : Type*} [AddCommMonoid S] (p : R[X]) (f g : ℕ → R → S) : p.sum (f + g) = p.sum f + p.sum g := by simp [sum_def, Finset.sum_add_distrib] theorem sum_add {S : Type*} [AddCommMonoid S] (p : R[X]) (f g : ℕ → R → S) : (p.sum fun n x => f n x + g n x) = p.sum f + p.sum g := sum_add' _ _ _ theorem sum_smul_index {S : Type*} [AddCommMonoid S] (p : R[X]) (b : R) (f : ℕ → R → S) (hf : ∀ i, f i 0 = 0) : (b • p).sum f = p.sum fun n a => f n (b * a) := Finsupp.sum_smul_index hf theorem sum_smul_index' {S T : Type*} [DistribSMul T R] [AddCommMonoid S] (p : R[X]) (b : T) (f : ℕ → R → S) (hf : ∀ i, f i 0 = 0) : (b • p).sum f = p.sum fun n a => f n (b • a) := Finsupp.sum_smul_index' hf protected theorem smul_sum {S T : Type*} [AddCommMonoid S] [DistribSMul T S] (p : R[X]) (b : T) (f : ℕ → R → S) : b • p.sum f = p.sum fun n a => b • f n a := Finsupp.smul_sum @[simp] theorem sum_monomial_eq : ∀ p : R[X], (p.sum fun n a => monomial n a) = p | ⟨_p⟩ => (ofFinsupp_sum _ _).symm.trans (congr_arg _ <| Finsupp.sum_single _) theorem sum_C_mul_X_pow_eq (p : R[X]) : (p.sum fun n a => C a * X ^ n) = p := by simp_rw [C_mul_X_pow_eq_monomial, sum_monomial_eq] /-- `erase p n` is the polynomial `p` in which the `X^n` term has been erased. -/ irreducible_def erase (n : ℕ) : R[X] → R[X] | ⟨p⟩ => ⟨p.erase n⟩ @[simp] theorem toFinsupp_erase (p : R[X]) (n : ℕ) : toFinsupp (p.erase n) = p.toFinsupp.erase n := by rcases p with ⟨⟩ simp only [erase_def] @[simp] theorem ofFinsupp_erase (p : R[ℕ]) (n : ℕ) : (⟨p.erase n⟩ : R[X]) = (⟨p⟩ : R[X]).erase n := by rcases p with ⟨⟩ simp only [erase_def] @[simp] theorem support_erase (p : R[X]) (n : ℕ) : support (p.erase n) = (support p).erase n := by rcases p with ⟨⟩ simp only [support, erase_def, Finsupp.support_erase] theorem monomial_add_erase (p : R[X]) (n : ℕ) : monomial n (coeff p n) + p.erase n = p := toFinsupp_injective <| by rcases p with ⟨⟩ rw [toFinsupp_add, toFinsupp_monomial, toFinsupp_erase, coeff] exact Finsupp.single_add_erase _ _ theorem coeff_erase (p : R[X]) (n i : ℕ) : (p.erase n).coeff i = if i = n then 0 else p.coeff i := by rcases p with ⟨⟩ simp only [erase_def, coeff] -- Porting note: Was `convert rfl`. exact ite_congr rfl (fun _ => rfl) (fun _ => rfl) @[simp] theorem erase_zero (n : ℕ) : (0 : R[X]).erase n = 0 := toFinsupp_injective <| by simp @[simp] theorem erase_monomial {n : ℕ} {a : R} : erase n (monomial n a) = 0 := toFinsupp_injective <| by simp @[simp] theorem erase_same (p : R[X]) (n : ℕ) : coeff (p.erase n) n = 0 := by simp [coeff_erase] @[simp] theorem erase_ne (p : R[X]) (n i : ℕ) (h : i ≠ n) : coeff (p.erase n) i = coeff p i := by simp [coeff_erase, h] section Update /-- Replace the coefficient of a `p : R[X]` at a given degree `n : ℕ` by a given value `a : R`. If `a = 0`, this is equal to `p.erase n` If `p.natDegree < n` and `a ≠ 0`, this increases the degree to `n`. -/ def update (p : R[X]) (n : ℕ) (a : R) : R[X] := Polynomial.ofFinsupp (p.toFinsupp.update n a) theorem coeff_update (p : R[X]) (n : ℕ) (a : R) : (p.update n a).coeff = Function.update p.coeff n a := by ext cases p simp only [coeff, update, Function.update_apply, coe_update] theorem coeff_update_apply (p : R[X]) (n : ℕ) (a : R) (i : ℕ) : (p.update n a).coeff i = if i = n then a else p.coeff i := by rw [coeff_update, Function.update_apply] @[simp] theorem coeff_update_same (p : R[X]) (n : ℕ) (a : R) : (p.update n a).coeff n = a := by rw [p.coeff_update_apply, if_pos rfl] theorem coeff_update_ne (p : R[X]) {n : ℕ} (a : R) {i : ℕ} (h : i ≠ n) : (p.update n a).coeff i = p.coeff i := by rw [p.coeff_update_apply, if_neg h] @[simp] theorem update_zero_eq_erase (p : R[X]) (n : ℕ) : p.update n 0 = p.erase n := by ext rw [coeff_update_apply, coeff_erase] theorem support_update (p : R[X]) (n : ℕ) (a : R) [Decidable (a = 0)] : support (p.update n a) = if a = 0 then p.support.erase n else insert n p.support := by classical cases p simp only [support, update, Finsupp.support_update] congr theorem support_update_zero (p : R[X]) (n : ℕ) : support (p.update n 0) = p.support.erase n := by rw [update_zero_eq_erase, support_erase] theorem support_update_ne_zero (p : R[X]) (n : ℕ) {a : R} (ha : a ≠ 0) : support (p.update n a) = insert n p.support := by classical rw [support_update, if_neg ha] end Update end Semiring section CommSemiring variable [CommSemiring R] instance commSemiring : CommSemiring R[X] := { Function.Injective.commSemigroup toFinsupp toFinsupp_injective toFinsupp_mul with toSemiring := Polynomial.semiring } end CommSemiring section Ring variable [Ring R] instance instIntCast : IntCast R[X] where intCast n := ofFinsupp n instance ring : Ring R[X] := --TODO: add reference to library note in PR #7432 { Function.Injective.ring toFinsupp toFinsupp_injective (toFinsupp_zero (R := R)) toFinsupp_one toFinsupp_add toFinsupp_mul toFinsupp_neg toFinsupp_sub (fun _ _ => toFinsupp_smul _ _) (fun _ _ => toFinsupp_smul _ _) toFinsupp_pow (fun _ => rfl) fun _ => rfl with toSemiring := Polynomial.semiring, toNeg := Polynomial.neg' toSub := Polynomial.sub zsmul := ((· • ·) : ℤ → R[X] → R[X]) } @[simp] theorem coeff_neg (p : R[X]) (n : ℕ) : coeff (-p) n = -coeff p n := by rcases p with ⟨⟩ -- Porting note: The last rule should be `apply`ed. rw [← ofFinsupp_neg, coeff, coeff]; apply 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 ⟨⟩ -- Porting note: The last rule should be `apply`ed. rw [← ofFinsupp_sub, coeff, coeff, coeff]; apply Finsupp.sub_apply -- @[simp] -- Porting note (#10618): simp can prove this theorem monomial_neg (n : ℕ) (a : R) : monomial n (-a) = -monomial n a := by rw [eq_neg_iff_add_eq_zero, ← monomial_add, neg_add_self, monomial_zero_right] theorem monomial_sub (n : ℕ) : monomial n (a - b) = monomial n a - monomial n b := by rw [sub_eq_add_neg, monomial_add, monomial_neg] rfl @[simp] theorem support_neg {p : R[X]} : (-p).support = p.support := by rcases p with ⟨⟩ -- Porting note: The last rule should be `apply`ed. rw [← ofFinsupp_neg, support, support]; apply Finsupp.support_neg theorem C_eq_intCast (n : ℤ) : C (n : R) = n := by simp @[deprecated (since := "2024-04-17")] alias C_eq_int_cast := C_eq_intCast 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 #7432 { toRing := Polynomial.ring mul_comm := mul_comm } section NonzeroSemiring 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) end NonzeroSemiring 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
Algebra\Polynomial\BigOperators.lean
/- Copyright (c) 2020 Aaron Anderson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson, Jalex Stark -/ import Mathlib.Algebra.Polynomial.Monic /-! # Lemmas for the interaction between polynomials and `∑` and `∏`. Recall that `∑` and `∏` are notation for `Finset.sum` and `Finset.prod` respectively. ## Main results - `Polynomial.natDegree_prod_of_monic` : the degree of a product of monic polynomials is the product of degrees. We prove this only for `[CommSemiring R]`, but it ought to be true for `[Semiring R]` and `List.prod`. - `Polynomial.natDegree_prod` : for polynomials over an integral domain, the degree of the product is the sum of degrees. - `Polynomial.leadingCoeff_prod` : for polynomials over an integral domain, the leading coefficient is the product of leading coefficients. - `Polynomial.prod_X_sub_C_coeff_card_pred` carries most of the content for computing the second coefficient of the characteristic polynomial. -/ open Finset open Multiset open Polynomial universe u w variable {R : Type u} {ι : Type w} namespace Polynomial variable (s : Finset ι) section Semiring variable {S : Type*} [Semiring S] set_option backward.isDefEq.lazyProjDelta false in -- See https://github.com/leanprover-community/mathlib4/issues/12535 theorem natDegree_list_sum_le (l : List S[X]) : natDegree l.sum ≤ (l.map natDegree).foldr max 0 := List.sum_le_foldr_max natDegree (by simp) natDegree_add_le _ theorem natDegree_multiset_sum_le (l : Multiset S[X]) : natDegree l.sum ≤ (l.map natDegree).foldr max max_left_comm 0 := Quotient.inductionOn l (by simpa using natDegree_list_sum_le) theorem natDegree_sum_le (f : ι → S[X]) : natDegree (∑ i ∈ s, f i) ≤ s.fold max 0 (natDegree ∘ f) := by simpa using natDegree_multiset_sum_le (s.val.map f) lemma natDegree_sum_le_of_forall_le {n : ℕ} (f : ι → S[X]) (h : ∀ i ∈ s, natDegree (f i) ≤ n) : natDegree (∑ i ∈ s, f i) ≤ n := le_trans (natDegree_sum_le s f) <| (Finset.fold_max_le n).mpr <| by simpa theorem degree_list_sum_le (l : List S[X]) : degree l.sum ≤ (l.map natDegree).maximum := by by_cases h : l.sum = 0 · simp [h] · rw [degree_eq_natDegree h] suffices (l.map natDegree).maximum = ((l.map natDegree).foldr max 0 : ℕ) by rw [this] simpa using natDegree_list_sum_le l rw [← List.foldr_max_of_ne_nil] · congr contrapose! h rw [List.map_eq_nil] at h simp [h] theorem natDegree_list_prod_le (l : List S[X]) : natDegree l.prod ≤ (l.map natDegree).sum := by induction' l with hd tl IH · simp · simpa using natDegree_mul_le.trans (add_le_add_left IH _) theorem degree_list_prod_le (l : List S[X]) : degree l.prod ≤ (l.map degree).sum := by induction' l with hd tl IH · simp · simpa using (degree_mul_le _ _).trans (add_le_add_left IH _) theorem coeff_list_prod_of_natDegree_le (l : List S[X]) (n : ℕ) (hl : ∀ p ∈ l, natDegree p ≤ n) : coeff (List.prod l) (l.length * n) = (l.map fun p => coeff p n).prod := by induction' l with hd tl IH · simp · have hl' : ∀ p ∈ tl, natDegree p ≤ n := fun p hp => hl p (List.mem_cons_of_mem _ hp) simp only [List.prod_cons, List.map, List.length] rw [add_mul, one_mul, add_comm, ← IH hl', mul_comm tl.length] have h : natDegree tl.prod ≤ n * tl.length := by refine (natDegree_list_prod_le _).trans ?_ rw [← tl.length_map natDegree, mul_comm] refine List.sum_le_card_nsmul _ _ ?_ simpa using hl' have hdn : natDegree hd ≤ n := hl _ (List.mem_cons_self _ _) rcases hdn.eq_or_lt with (rfl | hdn') · rcases h.eq_or_lt with h' | h' · rw [← h', coeff_mul_degree_add_degree, leadingCoeff, leadingCoeff] · rw [coeff_eq_zero_of_natDegree_lt, coeff_eq_zero_of_natDegree_lt h', mul_zero] exact natDegree_mul_le.trans_lt (add_lt_add_left h' _) · rw [coeff_eq_zero_of_natDegree_lt hdn', coeff_eq_zero_of_natDegree_lt, zero_mul] exact natDegree_mul_le.trans_lt (add_lt_add_of_lt_of_le hdn' h) end Semiring section CommSemiring variable [CommSemiring R] (f : ι → R[X]) (t : Multiset R[X]) theorem natDegree_multiset_prod_le : t.prod.natDegree ≤ (t.map natDegree).sum := Quotient.inductionOn t (by simpa using natDegree_list_prod_le) theorem natDegree_prod_le : (∏ i ∈ s, f i).natDegree ≤ ∑ i ∈ s, (f i).natDegree := by simpa using natDegree_multiset_prod_le (s.1.map f) /-- The degree of a product of polynomials is at most the sum of the degrees, where the degree of the zero polynomial is ⊥. -/ theorem degree_multiset_prod_le : t.prod.degree ≤ (t.map Polynomial.degree).sum := Quotient.inductionOn t (by simpa using degree_list_prod_le) theorem degree_prod_le : (∏ i ∈ s, f i).degree ≤ ∑ i ∈ s, (f i).degree := by simpa only [Multiset.map_map] using degree_multiset_prod_le (s.1.map f) /-- The leading coefficient of a product of polynomials is equal to the product of the leading coefficients, provided that this product is nonzero. See `Polynomial.leadingCoeff_multiset_prod` (without the `'`) for a version for integral domains, where this condition is automatically satisfied. -/ theorem leadingCoeff_multiset_prod' (h : (t.map leadingCoeff).prod ≠ 0) : t.prod.leadingCoeff = (t.map leadingCoeff).prod := by induction' t using Multiset.induction_on with a t ih; · simp simp only [Multiset.map_cons, Multiset.prod_cons] at h ⊢ rw [Polynomial.leadingCoeff_mul'] · rw [ih] simp only [ne_eq] apply right_ne_zero_of_mul h · rw [ih] · exact h simp only [ne_eq, not_false_eq_true] apply right_ne_zero_of_mul h /-- The leading coefficient of a product of polynomials is equal to the product of the leading coefficients, provided that this product is nonzero. See `Polynomial.leadingCoeff_prod` (without the `'`) for a version for integral domains, where this condition is automatically satisfied. -/ theorem leadingCoeff_prod' (h : (∏ i ∈ s, (f i).leadingCoeff) ≠ 0) : (∏ i ∈ s, f i).leadingCoeff = ∏ i ∈ s, (f i).leadingCoeff := by simpa using leadingCoeff_multiset_prod' (s.1.map f) (by simpa using h) /-- The degree of a product of polynomials is equal to the sum of the degrees, provided that the product of leading coefficients is nonzero. See `Polynomial.natDegree_multiset_prod` (without the `'`) for a version for integral domains, where this condition is automatically satisfied. -/ theorem natDegree_multiset_prod' (h : (t.map fun f => leadingCoeff f).prod ≠ 0) : t.prod.natDegree = (t.map fun f => natDegree f).sum := by revert h refine Multiset.induction_on t ?_ fun a t ih ht => ?_; · simp rw [Multiset.map_cons, Multiset.prod_cons] at ht ⊢ rw [Multiset.sum_cons, Polynomial.natDegree_mul', ih] · apply right_ne_zero_of_mul ht · rwa [Polynomial.leadingCoeff_multiset_prod'] apply right_ne_zero_of_mul ht /-- The degree of a product of polynomials is equal to the sum of the degrees, provided that the product of leading coefficients is nonzero. See `Polynomial.natDegree_prod` (without the `'`) for a version for integral domains, where this condition is automatically satisfied. -/ theorem natDegree_prod' (h : (∏ i ∈ s, (f i).leadingCoeff) ≠ 0) : (∏ i ∈ s, f i).natDegree = ∑ i ∈ s, (f i).natDegree := by simpa using natDegree_multiset_prod' (s.1.map f) (by simpa using h) theorem natDegree_multiset_prod_of_monic (h : ∀ f ∈ t, Monic f) : t.prod.natDegree = (t.map natDegree).sum := by nontriviality R apply natDegree_multiset_prod' suffices (t.map fun f => leadingCoeff f).prod = 1 by rw [this] simp convert prod_replicate (Multiset.card t) (1 : R) · simp only [eq_replicate, Multiset.card_map, eq_self_iff_true, true_and_iff] rintro i hi obtain ⟨i, hi, rfl⟩ := Multiset.mem_map.mp hi apply h assumption · simp theorem natDegree_prod_of_monic (h : ∀ i ∈ s, (f i).Monic) : (∏ i ∈ s, f i).natDegree = ∑ i ∈ s, (f i).natDegree := by simpa using natDegree_multiset_prod_of_monic (s.1.map f) (by simpa using h) theorem coeff_multiset_prod_of_natDegree_le (n : ℕ) (hl : ∀ p ∈ t, natDegree p ≤ n) : coeff t.prod ((Multiset.card t) * n) = (t.map fun p => coeff p n).prod := by induction t using Quotient.inductionOn simpa using coeff_list_prod_of_natDegree_le _ _ hl theorem coeff_prod_of_natDegree_le (f : ι → R[X]) (n : ℕ) (h : ∀ p ∈ s, natDegree (f p) ≤ n) : coeff (∏ i ∈ s, f i) (s.card * n) = ∏ i ∈ s, coeff (f i) n := by cases' s with l hl convert coeff_multiset_prod_of_natDegree_le (l.map f) n ?_ · simp · simp · simpa using h theorem coeff_zero_multiset_prod : t.prod.coeff 0 = (t.map fun f => coeff f 0).prod := by refine Multiset.induction_on t ?_ fun a t ht => ?_; · simp rw [Multiset.prod_cons, Multiset.map_cons, Multiset.prod_cons, Polynomial.mul_coeff_zero, ht] theorem coeff_zero_prod : (∏ i ∈ s, f i).coeff 0 = ∏ i ∈ s, (f i).coeff 0 := by simpa using coeff_zero_multiset_prod (s.1.map f) end CommSemiring section CommRing variable [CommRing R] open Monic -- Eventually this can be generalized with Vieta's formulas -- plus the connection between roots and factorization. theorem multiset_prod_X_sub_C_nextCoeff (t : Multiset R) : nextCoeff (t.map fun x => X - C x).prod = -t.sum := by rw [nextCoeff_multiset_prod] · simp only [nextCoeff_X_sub_C] exact t.sum_hom (-AddMonoidHom.id R) · intros apply monic_X_sub_C theorem prod_X_sub_C_nextCoeff {s : Finset ι} (f : ι → R) : nextCoeff (∏ i ∈ s, (X - C (f i))) = -∑ i ∈ s, f i := by simpa using multiset_prod_X_sub_C_nextCoeff (s.1.map f) theorem multiset_prod_X_sub_C_coeff_card_pred (t : Multiset R) (ht : 0 < Multiset.card t) : (t.map fun x => X - C x).prod.coeff ((Multiset.card t) - 1) = -t.sum := by nontriviality R convert multiset_prod_X_sub_C_nextCoeff (by assumption) rw [nextCoeff, if_neg] swap · rw [natDegree_multiset_prod_of_monic] swap · simp only [Multiset.mem_map] rintro _ ⟨_, _, rfl⟩ apply monic_X_sub_C simp_rw [Multiset.sum_eq_zero_iff, Multiset.mem_map] obtain ⟨x, hx⟩ := card_pos_iff_exists_mem.mp ht exact fun h => one_ne_zero <| h 1 ⟨_, ⟨x, hx, rfl⟩, natDegree_X_sub_C _⟩ congr; rw [natDegree_multiset_prod_of_monic] <;> · simp [natDegree_X_sub_C, monic_X_sub_C] theorem prod_X_sub_C_coeff_card_pred (s : Finset ι) (f : ι → R) (hs : 0 < s.card) : (∏ i ∈ s, (X - C (f i))).coeff (s.card - 1) = -∑ i ∈ s, f i := by simpa using multiset_prod_X_sub_C_coeff_card_pred (s.1.map f) (by simpa using hs) end CommRing section NoZeroDivisors section Semiring variable [Semiring R] [NoZeroDivisors R] /-- The degree of a product of polynomials is equal to the sum of the degrees, where the degree of the zero polynomial is ⊥. `[Nontrivial R]` is needed, otherwise for `l = []` we have `⊥` in the LHS and `0` in the RHS. -/ theorem degree_list_prod [Nontrivial R] (l : List R[X]) : l.prod.degree = (l.map degree).sum := map_list_prod (@degreeMonoidHom R _ _ _) l end Semiring section CommSemiring variable [CommSemiring R] [NoZeroDivisors R] (f : ι → R[X]) (t : Multiset R[X]) /-- The degree of a product of polynomials is equal to the sum of the degrees. See `Polynomial.natDegree_prod'` (with a `'`) for a version for commutative semirings, where additionally, the product of the leading coefficients must be nonzero. -/ theorem natDegree_prod (h : ∀ i ∈ s, f i ≠ 0) : (∏ i ∈ s, f i).natDegree = ∑ i ∈ s, (f i).natDegree := by nontriviality R apply natDegree_prod' rw [prod_ne_zero_iff] intro x hx; simp [h x hx] theorem natDegree_multiset_prod (h : (0 : R[X]) ∉ t) : natDegree t.prod = (t.map natDegree).sum := by nontriviality R rw [natDegree_multiset_prod'] simp_rw [Ne, Multiset.prod_eq_zero_iff, Multiset.mem_map, leadingCoeff_eq_zero] rintro ⟨_, h, rfl⟩ contradiction /-- The degree of a product of polynomials is equal to the sum of the degrees, where the degree of the zero polynomial is ⊥. -/ theorem degree_multiset_prod [Nontrivial R] : t.prod.degree = (t.map fun f => degree f).sum := map_multiset_prod (@degreeMonoidHom R _ _ _) _ /-- The degree of a product of polynomials is equal to the sum of the degrees, where the degree of the zero polynomial is ⊥. -/ theorem degree_prod [Nontrivial R] : (∏ i ∈ s, f i).degree = ∑ i ∈ s, (f i).degree := map_prod (@degreeMonoidHom R _ _ _) _ _ /-- The leading coefficient of a product of polynomials is equal to the product of the leading coefficients. See `Polynomial.leadingCoeff_multiset_prod'` (with a `'`) for a version for commutative semirings, where additionally, the product of the leading coefficients must be nonzero. -/ theorem leadingCoeff_multiset_prod : t.prod.leadingCoeff = (t.map fun f => leadingCoeff f).prod := by rw [← leadingCoeffHom_apply, MonoidHom.map_multiset_prod] rfl /-- The leading coefficient of a product of polynomials is equal to the product of the leading coefficients. See `Polynomial.leadingCoeff_prod'` (with a `'`) for a version for commutative semirings, where additionally, the product of the leading coefficients must be nonzero. -/ theorem leadingCoeff_prod : (∏ i ∈ s, f i).leadingCoeff = ∏ i ∈ s, (f i).leadingCoeff := by simpa using leadingCoeff_multiset_prod (s.1.map f) end CommSemiring end NoZeroDivisors end Polynomial
Algebra\Polynomial\Bivariate.lean
/- Copyright (c) 2024 Junyan Xu. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Junyan Xu -/ 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` 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] notation3:max "Y" => Polynomial.X (R := Polynomial _) /-- The notation `R[X][Y]` for `R[X][X]` in the `Polynomial` scope. -/ scoped[Polynomial] notation3:max R "[X][Y]" => Polynomial (Polynomial R) 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] lemma evalEval_X (x y : R) : X.evalEval x y = y := by rw [evalEval, eval_X, eval_C] end Semiring section CommSemiring variable [CommSemiring R] 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 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!] 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
Algebra\Polynomial\CancelLeads.lean
/- Copyright (c) 2020 Aaron Anderson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson -/ import Mathlib.Algebra.Polynomial.Degree.Definitions import Mathlib.Algebra.Polynomial.Degree.Lemmas import Mathlib.Tactic.ComputeDegree /-! # Cancel the leading terms of two polynomials ## Definition * `cancelLeads p q`: the polynomial formed by multiplying `p` and `q` by monomials so that they have the same leading term, and then subtracting. ## Main Results The degree of `cancelLeads` is less than that of the larger of the two polynomials being cancelled. Thus it is useful for induction or minimal-degree arguments. -/ namespace Polynomial noncomputable section open Polynomial variable {R : Type*} section Ring variable [Ring R] (p q : R[X]) /-- `cancelLeads p q` is formed by multiplying `p` and `q` by monomials so that they have the same leading term, and then subtracting. -/ def cancelLeads : R[X] := C p.leadingCoeff * X ^ (p.natDegree - q.natDegree) * q - C q.leadingCoeff * X ^ (q.natDegree - p.natDegree) * p variable {p q} @[simp] theorem neg_cancelLeads : -p.cancelLeads q = q.cancelLeads p := neg_sub _ _ theorem natDegree_cancelLeads_lt_of_natDegree_le_natDegree_of_comm (comm : p.leadingCoeff * q.leadingCoeff = q.leadingCoeff * p.leadingCoeff) (h : p.natDegree ≤ q.natDegree) (hq : 0 < q.natDegree) : (p.cancelLeads q).natDegree < q.natDegree := by by_cases hp : p = 0 · convert hq simp [hp, cancelLeads] rw [cancelLeads, sub_eq_add_neg, tsub_eq_zero_iff_le.mpr h, pow_zero, mul_one] by_cases h0 : C p.leadingCoeff * q + -(C q.leadingCoeff * X ^ (q.natDegree - p.natDegree) * p) = 0 · exact (le_of_eq (by simp only [h0, natDegree_zero])).trans_lt hq apply lt_of_le_of_ne · compute_degree! rwa [Nat.sub_add_cancel] · contrapose! h0 rw [← leadingCoeff_eq_zero, leadingCoeff, h0, mul_assoc, X_pow_mul, ← tsub_add_cancel_of_le h, add_comm _ p.natDegree] simp only [coeff_mul_X_pow, coeff_neg, coeff_C_mul, add_tsub_cancel_left, coeff_add] rw [add_comm p.natDegree, tsub_add_cancel_of_le h, ← leadingCoeff, ← leadingCoeff, comm, add_right_neg] end Ring section CommRing variable [CommRing R] {p q : R[X]} theorem dvd_cancelLeads_of_dvd_of_dvd {r : R[X]} (pq : p ∣ q) (pr : p ∣ r) : p ∣ q.cancelLeads r := dvd_sub (pr.trans (Dvd.intro_left _ rfl)) (pq.trans (Dvd.intro_left _ rfl)) theorem natDegree_cancelLeads_lt_of_natDegree_le_natDegree (h : p.natDegree ≤ q.natDegree) (hq : 0 < q.natDegree) : (p.cancelLeads q).natDegree < q.natDegree := natDegree_cancelLeads_lt_of_natDegree_le_natDegree_of_comm (mul_comm _ _) h hq end CommRing end end Polynomial
Algebra\Polynomial\Cardinal.lean
/- Copyright (c) 2021 Chris Hughes, Junyan Xu. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Junyan Xu -/ import Mathlib.Algebra.Polynomial.Basic import Mathlib.SetTheory.Cardinal.Ordinal /-! # Cardinality of Polynomial Ring The result in this file is that the cardinality of `R[X]` is at most the maximum of `#R` and `ℵ₀`. -/ universe u open Cardinal Polynomial open Cardinal namespace Polynomial @[simp] theorem cardinal_mk_eq_max {R : Type u} [Semiring R] [Nontrivial R] : #(R[X]) = max #R ℵ₀ := (toFinsuppIso R).toEquiv.cardinal_eq.trans <| by rw [AddMonoidAlgebra, mk_finsupp_lift_of_infinite, lift_uzero, max_comm] rfl theorem cardinal_mk_le_max {R : Type u} [Semiring R] : #(R[X]) ≤ max #R ℵ₀ := by cases subsingleton_or_nontrivial R · exact (mk_eq_one _).trans_le (le_max_of_le_right one_le_aleph0) · exact cardinal_mk_eq_max.le end Polynomial
Algebra\Polynomial\Coeff.lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker -/ import Mathlib.Algebra.MonoidAlgebra.Support import Mathlib.Algebra.Polynomial.Basic import Mathlib.Algebra.Regular.Basic import Mathlib.Data.Nat.Choose.Sum /-! # Theory of univariate polynomials The theorems include formulas for computing coefficients, such as `coeff_add`, `coeff_sum`, `coeff_mul` -/ noncomputable section open Finsupp Finset AddMonoidAlgebra open Polynomial namespace Polynomial universe u v variable {R : Type u} {S : Type v} {a b : R} {n m : ℕ} variable [Semiring R] {p q r : R[X]} section Coeff @[simp] theorem coeff_add (p q : R[X]) (n : ℕ) : coeff (p + q) n = coeff p n + coeff q n := by rcases p with ⟨⟩ rcases q with ⟨⟩ simp_rw [← ofFinsupp_add, coeff] exact Finsupp.add_apply _ _ _ @[simp] theorem coeff_smul [SMulZeroClass S R] (r : S) (p : R[X]) (n : ℕ) : coeff (r • p) n = r • coeff p n := by rcases p with ⟨⟩ simp_rw [← ofFinsupp_smul, coeff] exact Finsupp.smul_apply _ _ _ theorem support_smul [SMulZeroClass S R] (r : S) (p : R[X]) : support (r • p) ⊆ support p := by intro i hi simp? [mem_support_iff] at hi ⊢ says simp only [mem_support_iff, coeff_smul, ne_eq] at hi ⊢ contrapose! hi simp [hi] open scoped Pointwise in theorem card_support_mul_le : (p * q).support.card ≤ p.support.card * q.support.card := by calc (p * q).support.card _ = (p.toFinsupp * q.toFinsupp).support.card := by rw [← support_toFinsupp, toFinsupp_mul] _ ≤ (p.toFinsupp.support + q.toFinsupp.support).card := Finset.card_le_card (AddMonoidAlgebra.support_mul p.toFinsupp q.toFinsupp) _ ≤ p.support.card * q.support.card := Finset.card_image₂_le .. /-- `Polynomial.sum` as a linear map. -/ @[simps] def lsum {R A M : Type*} [Semiring R] [Semiring A] [AddCommMonoid M] [Module R A] [Module R M] (f : ℕ → A →ₗ[R] M) : A[X] →ₗ[R] M where toFun p := p.sum (f · ·) map_add' p q := sum_add_index p q _ (fun n => (f n).map_zero) fun n _ _ => (f n).map_add _ _ map_smul' c p := by -- Porting note: added `dsimp only`; `beta_reduce` alone is not sufficient dsimp only rw [sum_eq_of_subset (f · ·) (fun n => (f n).map_zero) (support_smul c p)] simp only [sum_def, Finset.smul_sum, coeff_smul, LinearMap.map_smul, RingHom.id_apply] variable (R) /-- The nth coefficient, as a linear map. -/ def lcoeff (n : ℕ) : R[X] →ₗ[R] R where toFun p := coeff p n map_add' p q := coeff_add p q n map_smul' r p := coeff_smul r p n variable {R} @[simp] theorem lcoeff_apply (n : ℕ) (f : R[X]) : lcoeff R n f = coeff f n := rfl @[simp] theorem finset_sum_coeff {ι : Type*} (s : Finset ι) (f : ι → R[X]) (n : ℕ) : coeff (∑ b ∈ s, f b) n = ∑ b ∈ s, coeff (f b) n := map_sum (lcoeff R n) _ _ lemma coeff_list_sum (l : List R[X]) (n : ℕ) : l.sum.coeff n = (l.map (lcoeff R n)).sum := map_list_sum (lcoeff R n) _ lemma coeff_list_sum_map {ι : Type*} (l : List ι) (f : ι → R[X]) (n : ℕ) : (l.map f).sum.coeff n = (l.map (fun a => (f a).coeff n)).sum := by simp_rw [coeff_list_sum, List.map_map, Function.comp, lcoeff_apply] theorem coeff_sum [Semiring S] (n : ℕ) (f : ℕ → R → S[X]) : coeff (p.sum f) n = p.sum fun a b => coeff (f a b) n := by rcases p with ⟨⟩ -- porting note (#10745): was `simp [Polynomial.sum, support, coeff]`. simp [Polynomial.sum, support_ofFinsupp, coeff_ofFinsupp] /-- Decomposes the coefficient of the product `p * q` as a sum over `antidiagonal`. A version which sums over `range (n + 1)` can be obtained by using `Finset.Nat.sum_antidiagonal_eq_sum_range_succ`. -/ theorem coeff_mul (p q : R[X]) (n : ℕ) : coeff (p * q) n = ∑ x ∈ antidiagonal n, coeff p x.1 * coeff q x.2 := by rcases p with ⟨p⟩; rcases q with ⟨q⟩ simp_rw [← ofFinsupp_mul, coeff] exact AddMonoidAlgebra.mul_apply_antidiagonal p q n _ Finset.mem_antidiagonal @[simp] theorem mul_coeff_zero (p q : R[X]) : coeff (p * q) 0 = coeff p 0 * coeff q 0 := by simp [coeff_mul] /-- `constantCoeff p` returns the constant term of the polynomial `p`, defined as `coeff p 0`. This is a ring homomorphism. -/ @[simps] def constantCoeff : R[X] →+* R where toFun p := coeff p 0 map_one' := coeff_one_zero map_mul' := mul_coeff_zero map_zero' := coeff_zero 0 map_add' p q := coeff_add p q 0 theorem isUnit_C {x : R} : IsUnit (C x) ↔ IsUnit x := ⟨fun h => (congr_arg IsUnit coeff_C_zero).mp (h.map <| @constantCoeff R _), fun h => h.map C⟩ theorem coeff_mul_X_zero (p : R[X]) : coeff (p * X) 0 = 0 := by simp theorem coeff_X_mul_zero (p : R[X]) : coeff (X * p) 0 = 0 := by simp theorem coeff_C_mul_X_pow (x : R) (k n : ℕ) : coeff (C x * X ^ k : R[X]) n = if n = k then x else 0 := by rw [C_mul_X_pow_eq_monomial, coeff_monomial] congr 1 simp [eq_comm] theorem coeff_C_mul_X (x : R) (n : ℕ) : coeff (C x * X : R[X]) n = if n = 1 then x else 0 := by rw [← pow_one X, coeff_C_mul_X_pow] @[simp] theorem coeff_C_mul (p : R[X]) : coeff (C a * p) n = a * coeff p n := by rcases p with ⟨p⟩ simp_rw [← monomial_zero_left, ← ofFinsupp_single, ← ofFinsupp_mul, coeff] exact AddMonoidAlgebra.single_zero_mul_apply p a n theorem C_mul' (a : R) (f : R[X]) : C a * f = a • f := by ext rw [coeff_C_mul, coeff_smul, smul_eq_mul] @[simp] theorem coeff_mul_C (p : R[X]) (n : ℕ) (a : R) : coeff (p * C a) n = coeff p n * a := by rcases p with ⟨p⟩ simp_rw [← monomial_zero_left, ← ofFinsupp_single, ← ofFinsupp_mul, coeff] exact AddMonoidAlgebra.mul_single_zero_apply p a n @[simp] lemma coeff_mul_natCast {a k : ℕ} : coeff (p * (a : R[X])) k = coeff p k * (↑a : R) := coeff_mul_C _ _ _ @[simp] lemma coeff_natCast_mul {a k : ℕ} : coeff ((a : R[X]) * p) k = a * coeff p k := coeff_C_mul _ -- See note [no_index around OfNat.ofNat] @[simp] lemma coeff_mul_ofNat {a k : ℕ} [Nat.AtLeastTwo a] : coeff (p * (no_index (OfNat.ofNat a) : R[X])) k = coeff p k * OfNat.ofNat a := coeff_mul_C _ _ _ -- See note [no_index around OfNat.ofNat] @[simp] lemma coeff_ofNat_mul {a k : ℕ} [Nat.AtLeastTwo a] : coeff ((no_index (OfNat.ofNat a) : R[X]) * p) k = OfNat.ofNat a * coeff p k := coeff_C_mul _ @[simp] lemma coeff_mul_intCast [Ring S] {p : S[X]} {a : ℤ} {k : ℕ} : coeff (p * (a : S[X])) k = coeff p k * (↑a : S) := coeff_mul_C _ _ _ @[simp] lemma coeff_intCast_mul [Ring S] {p : S[X]} {a : ℤ} {k : ℕ} : coeff ((a : S[X]) * p) k = a * coeff p k := coeff_C_mul _ @[simp] theorem coeff_X_pow (k n : ℕ) : coeff (X ^ k : R[X]) n = if n = k then 1 else 0 := by simp only [one_mul, RingHom.map_one, ← coeff_C_mul_X_pow] theorem coeff_X_pow_self (n : ℕ) : coeff (X ^ n : R[X]) n = 1 := by simp section Fewnomials open Finset theorem support_binomial {k m : ℕ} (hkm : k ≠ m) {x y : R} (hx : x ≠ 0) (hy : y ≠ 0) : support (C x * X ^ k + C y * X ^ m) = {k, m} := by apply subset_antisymm (support_binomial' k m x y) simp_rw [insert_subset_iff, singleton_subset_iff, mem_support_iff, coeff_add, coeff_C_mul, coeff_X_pow_self, mul_one, coeff_X_pow, if_neg hkm, if_neg hkm.symm, mul_zero, zero_add, add_zero, Ne, hx, hy, not_false_eq_true, and_true] theorem support_trinomial {k m n : ℕ} (hkm : k < m) (hmn : m < n) {x y z : R} (hx : x ≠ 0) (hy : y ≠ 0) (hz : z ≠ 0) : support (C x * X ^ k + C y * X ^ m + C z * X ^ n) = {k, m, n} := by apply subset_antisymm (support_trinomial' k m n x y z) simp_rw [insert_subset_iff, singleton_subset_iff, mem_support_iff, coeff_add, coeff_C_mul, coeff_X_pow_self, mul_one, coeff_X_pow, if_neg hkm.ne, if_neg hkm.ne', if_neg hmn.ne, if_neg hmn.ne', if_neg (hkm.trans hmn).ne, if_neg (hkm.trans hmn).ne', mul_zero, add_zero, zero_add, Ne, hx, hy, hz, not_false_eq_true, and_true] theorem card_support_binomial {k m : ℕ} (h : k ≠ m) {x y : R} (hx : x ≠ 0) (hy : y ≠ 0) : card (support (C x * X ^ k + C y * X ^ m)) = 2 := by rw [support_binomial h hx hy, card_insert_of_not_mem (mt mem_singleton.mp h), card_singleton] theorem card_support_trinomial {k m n : ℕ} (hkm : k < m) (hmn : m < n) {x y z : R} (hx : x ≠ 0) (hy : y ≠ 0) (hz : z ≠ 0) : card (support (C x * X ^ k + C y * X ^ m + C z * X ^ n)) = 3 := by rw [support_trinomial hkm hmn hx hy hz, card_insert_of_not_mem (mt mem_insert.mp (not_or_of_not hkm.ne (mt mem_singleton.mp (hkm.trans hmn).ne))), card_insert_of_not_mem (mt mem_singleton.mp hmn.ne), card_singleton] end Fewnomials @[simp] theorem coeff_mul_X_pow (p : R[X]) (n d : ℕ) : coeff (p * Polynomial.X ^ n) (d + n) = coeff p d := by rw [coeff_mul, Finset.sum_eq_single (d, n), coeff_X_pow, if_pos rfl, mul_one] · rintro ⟨i, j⟩ h1 h2 rw [coeff_X_pow, if_neg, mul_zero] rintro rfl apply h2 rw [mem_antidiagonal, add_right_cancel_iff] at h1 subst h1 rfl · exact fun h1 => (h1 (mem_antidiagonal.2 rfl)).elim @[simp] theorem coeff_X_pow_mul (p : R[X]) (n d : ℕ) : coeff (Polynomial.X ^ n * p) (d + n) = coeff p d := by rw [(commute_X_pow p n).eq, coeff_mul_X_pow] theorem coeff_mul_X_pow' (p : R[X]) (n d : ℕ) : (p * X ^ n).coeff d = ite (n ≤ d) (p.coeff (d - n)) 0 := by split_ifs with h · rw [← tsub_add_cancel_of_le h, coeff_mul_X_pow, add_tsub_cancel_right] · refine (coeff_mul _ _ _).trans (Finset.sum_eq_zero fun x hx => ?_) rw [coeff_X_pow, if_neg, mul_zero] exact ((le_of_add_le_right (mem_antidiagonal.mp hx).le).trans_lt <| not_le.mp h).ne theorem coeff_X_pow_mul' (p : R[X]) (n d : ℕ) : (X ^ n * p).coeff d = ite (n ≤ d) (p.coeff (d - n)) 0 := by rw [(commute_X_pow p n).eq, coeff_mul_X_pow'] @[simp] theorem coeff_mul_X (p : R[X]) (n : ℕ) : coeff (p * X) (n + 1) = coeff p n := by simpa only [pow_one] using coeff_mul_X_pow p 1 n @[simp] theorem coeff_X_mul (p : R[X]) (n : ℕ) : coeff (X * p) (n + 1) = coeff p n := by rw [(commute_X p).eq, coeff_mul_X] theorem coeff_mul_monomial (p : R[X]) (n d : ℕ) (r : R) : coeff (p * monomial n r) (d + n) = coeff p d * r := by rw [← C_mul_X_pow_eq_monomial, ← X_pow_mul, ← mul_assoc, coeff_mul_C, coeff_mul_X_pow] theorem coeff_monomial_mul (p : R[X]) (n d : ℕ) (r : R) : coeff (monomial n r * p) (d + n) = r * coeff p d := by rw [← C_mul_X_pow_eq_monomial, mul_assoc, coeff_C_mul, X_pow_mul, coeff_mul_X_pow] -- This can already be proved by `simp`. theorem coeff_mul_monomial_zero (p : R[X]) (d : ℕ) (r : R) : coeff (p * monomial 0 r) d = coeff p d * r := coeff_mul_monomial p 0 d r -- This can already be proved by `simp`. theorem coeff_monomial_zero_mul (p : R[X]) (d : ℕ) (r : R) : coeff (monomial 0 r * p) d = r * coeff p d := coeff_monomial_mul p 0 d r theorem mul_X_pow_eq_zero {p : R[X]} {n : ℕ} (H : p * X ^ n = 0) : p = 0 := ext fun k => (coeff_mul_X_pow p n k).symm.trans <| ext_iff.1 H (k + n) theorem isRegular_X_pow (n : ℕ) : IsRegular (X ^ n : R[X]) := by suffices IsLeftRegular (X^n : R[X]) from ⟨this, this.right_of_commute (fun p => commute_X_pow p n)⟩ intro P Q (hPQ : X^n * P = X^n * Q) ext i rw [← coeff_X_pow_mul P n i, hPQ, coeff_X_pow_mul Q n i] @[simp] theorem isRegular_X : IsRegular (X : R[X]) := pow_one (X : R[X]) ▸ isRegular_X_pow 1 theorem coeff_X_add_C_pow (r : R) (n k : ℕ) : ((X + C r) ^ n).coeff k = r ^ (n - k) * (n.choose k : R) := by rw [(commute_X (C r : R[X])).add_pow, ← lcoeff_apply, map_sum] simp only [one_pow, mul_one, lcoeff_apply, ← C_eq_natCast, ← C_pow, coeff_mul_C, Nat.cast_id] rw [Finset.sum_eq_single k, coeff_X_pow_self, one_mul] · intro _ _ h simp [coeff_X_pow, h.symm] · simp only [coeff_X_pow_self, one_mul, not_lt, Finset.mem_range] intro h rw [Nat.choose_eq_zero_of_lt h, Nat.cast_zero, mul_zero] theorem coeff_X_add_one_pow (R : Type*) [Semiring R] (n k : ℕ) : ((X + 1) ^ n).coeff k = (n.choose k : R) := by rw [← C_1, coeff_X_add_C_pow, one_pow, one_mul] theorem coeff_one_add_X_pow (R : Type*) [Semiring R] (n k : ℕ) : ((1 + X) ^ n).coeff k = (n.choose k : R) := by rw [add_comm _ X, coeff_X_add_one_pow] theorem C_dvd_iff_dvd_coeff (r : R) (φ : R[X]) : C r ∣ φ ↔ ∀ i, r ∣ φ.coeff i := by constructor · rintro ⟨φ, rfl⟩ c rw [coeff_C_mul] apply dvd_mul_right · intro h choose c hc using h classical let c' : ℕ → R := fun i => if i ∈ φ.support then c i else 0 let ψ : R[X] := ∑ i ∈ φ.support, monomial i (c' i) use ψ ext i simp only [c', ψ, coeff_C_mul, mem_support_iff, coeff_monomial, finset_sum_coeff, Finset.sum_ite_eq'] split_ifs with hi · rw [hc] · rw [Classical.not_not] at hi rwa [mul_zero] theorem smul_eq_C_mul (a : R) : a • p = C a * p := by simp [ext_iff] theorem update_eq_add_sub_coeff {R : Type*} [Ring R] (p : R[X]) (n : ℕ) (a : R) : p.update n a = p + Polynomial.C (a - p.coeff n) * Polynomial.X ^ n := by ext rw [coeff_update_apply, coeff_add, coeff_C_mul_X_pow] split_ifs with h <;> simp [h] end Coeff section cast theorem natCast_coeff_zero {n : ℕ} {R : Type*} [Semiring R] : (n : R[X]).coeff 0 = n := by simp only [coeff_natCast_ite, ite_true] @[deprecated (since := "2024-04-17")] alias nat_cast_coeff_zero := natCast_coeff_zero @[norm_cast] -- @[simp] -- Porting note (#10618): simp can prove this theorem natCast_inj {m n : ℕ} {R : Type*} [Semiring R] [CharZero R] : (↑m : R[X]) = ↑n ↔ m = n := by constructor · intro h apply_fun fun p => p.coeff 0 at h simpa using h · rintro rfl rfl @[deprecated (since := "2024-04-17")] alias nat_cast_inj := natCast_inj @[simp] theorem intCast_coeff_zero {i : ℤ} {R : Type*} [Ring R] : (i : R[X]).coeff 0 = i := by cases i <;> simp @[deprecated (since := "2024-04-17")] alias int_cast_coeff_zero := intCast_coeff_zero @[norm_cast] -- @[simp] -- Porting note (#10618): simp can prove this theorem intCast_inj {m n : ℤ} {R : Type*} [Ring R] [CharZero R] : (↑m : R[X]) = ↑n ↔ m = n := by constructor · intro h apply_fun fun p => p.coeff 0 at h simpa using h · rintro rfl rfl @[deprecated (since := "2024-04-17")] alias int_cast_inj := intCast_inj end cast instance charZero [CharZero R] : CharZero R[X] where cast_injective _x _y := natCast_inj.mp end Polynomial
Algebra\Polynomial\DenomsClearable.lean
/- Copyright (c) 2020 Damiano Testa. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Damiano Testa -/ import Mathlib.Algebra.Polynomial.EraseLead import Mathlib.Algebra.Polynomial.Eval /-! # Denominators of evaluation of polynomials at ratios Let `i : R → K` be a homomorphism of semirings. Assume that `K` is commutative. If `a` and `b` are elements of `R` such that `i b ∈ K` is invertible, then for any polynomial `f ∈ R[X]` the "mathematical" expression `b ^ f.natDegree * f (a / b) ∈ K` is in the image of the homomorphism `i`. -/ open Polynomial Finset open Polynomial section DenomsClearable variable {R K : Type*} [Semiring R] [CommSemiring K] {i : R →+* K} variable {a b : R} {bi : K} -- TODO: use hypothesis (ub : IsUnit (i b)) to work with localizations. /-- `denomsClearable` formalizes the property that `b ^ N * f (a / b)` does not have denominators, if the inequality `f.natDegree ≤ N` holds. The definition asserts the existence of an element `D` of `R` and an element `bi = 1 / i b` of `K` such that clearing the denominators of the fraction equals `i D`. -/ def DenomsClearable (a b : R) (N : ℕ) (f : R[X]) (i : R →+* K) : Prop := ∃ (D : R) (bi : K), bi * i b = 1 ∧ i D = i b ^ N * eval (i a * bi) (f.map i) theorem denomsClearable_zero (N : ℕ) (a : R) (bu : bi * i b = 1) : DenomsClearable a b N 0 i := ⟨0, bi, bu, by simp only [eval_zero, RingHom.map_zero, mul_zero, Polynomial.map_zero]⟩ theorem denomsClearable_C_mul_X_pow {N : ℕ} (a : R) (bu : bi * i b = 1) {n : ℕ} (r : R) (nN : n ≤ N) : DenomsClearable a b N (C r * X ^ n) i := by refine ⟨r * a ^ n * b ^ (N - n), bi, bu, ?_⟩ rw [C_mul_X_pow_eq_monomial, map_monomial, ← C_mul_X_pow_eq_monomial, eval_mul, eval_pow, eval_C] rw [RingHom.map_mul, RingHom.map_mul, RingHom.map_pow, RingHom.map_pow, eval_X, mul_comm] rw [← tsub_add_cancel_of_le nN] conv_lhs => rw [← mul_one (i a), ← bu] simp [mul_assoc, mul_comm, mul_left_comm, pow_add, mul_pow] theorem DenomsClearable.add {N : ℕ} {f g : R[X]} : DenomsClearable a b N f i → DenomsClearable a b N g i → DenomsClearable a b N (f + g) i := fun ⟨Df, bf, bfu, Hf⟩ ⟨Dg, bg, bgu, Hg⟩ => ⟨Df + Dg, bf, bfu, by rw [RingHom.map_add, Polynomial.map_add, eval_add, mul_add, Hf, Hg] congr refine @inv_unique K _ (i b) bg bf ?_ ?_ <;> rwa [mul_comm]⟩ theorem denomsClearable_of_natDegree_le (N : ℕ) (a : R) (bu : bi * i b = 1) : ∀ f : R[X], f.natDegree ≤ N → DenomsClearable a b N f i := induction_with_natDegree_le _ N (denomsClearable_zero N a bu) (fun _ r _ => denomsClearable_C_mul_X_pow a bu r) fun _ _ _ _ df dg => df.add dg /-- If `i : R → K` is a ring homomorphism, `f` is a polynomial with coefficients in `R`, `a, b` are elements of `R`, with `i b` invertible, then there is a `D ∈ R` such that `b ^ f.natDegree * f (a / b)` equals `i D`. -/ theorem denomsClearable_natDegree (i : R →+* K) (f : R[X]) (a : R) (bu : bi * i b = 1) : DenomsClearable a b f.natDegree f i := denomsClearable_of_natDegree_le f.natDegree a bu f le_rfl end DenomsClearable open RingHom /-- Evaluating a polynomial with integer coefficients at a rational number and clearing denominators, yields a number greater than or equal to one. The target can be any `LinearOrderedField K`. The assumption on `K` could be weakened to `LinearOrderedCommRing` assuming that the image of the denominator is invertible in `K`. -/ theorem one_le_pow_mul_abs_eval_div {K : Type*} [LinearOrderedField K] {f : ℤ[X]} {a b : ℤ} (b0 : 0 < b) (fab : eval ((a : K) / b) (f.map (algebraMap ℤ K)) ≠ 0) : (1 : K) ≤ (b : K) ^ f.natDegree * |eval ((a : K) / b) (f.map (algebraMap ℤ K))| := by obtain ⟨ev, bi, bu, hF⟩ := denomsClearable_natDegree (b := b) (algebraMap ℤ K) f a (by rw [eq_intCast, one_div_mul_cancel] rw [Int.cast_ne_zero] exact b0.ne.symm) obtain Fa := congr_arg abs hF rw [eq_one_div_of_mul_eq_one_left bu, eq_intCast, eq_intCast, abs_mul] at Fa rw [abs_of_pos (pow_pos (Int.cast_pos.mpr b0) _ : 0 < (b : K) ^ _), one_div, eq_intCast] at Fa rw [div_eq_mul_inv, ← Fa, ← Int.cast_abs, ← Int.cast_one, Int.cast_le] refine Int.le_of_lt_add_one ((lt_add_iff_pos_left 1).mpr (abs_pos.mpr fun F0 => fab ?_)) rw [eq_one_div_of_mul_eq_one_left bu, F0, one_div, eq_intCast, Int.cast_zero, zero_eq_mul] at hF cases' hF with hF hF · exact (not_le.mpr b0 (le_of_eq (Int.cast_eq_zero.mp (pow_eq_zero hF)))).elim · rwa [div_eq_mul_inv]
Algebra\Polynomial\Derivation.lean
/- Copyright (c) 2023 Kevin Buzzard. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Buzzard, Richard Hill -/ import Mathlib.Algebra.Polynomial.AlgebraMap import Mathlib.Algebra.Polynomial.Derivative import Mathlib.Algebra.Polynomial.Module.AEval 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
Algebra\Polynomial\Derivative.lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker -/ import Mathlib.Algebra.GroupPower.IterateHom import Mathlib.Algebra.Polynomial.Eval import Mathlib.GroupTheory.GroupAction.Ring /-! # The derivative map on polynomials ## Main definitions * `Polynomial.derivative`: The formal derivative of polynomials, expressed as a linear map. -/ noncomputable section open Finset open Polynomial 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 dsimp only rw [sum_add_index] <;> simp only [add_mul, forall_const, RingHom.map_add, eq_self_iff_true, zero_mul, RingHom.map_zero] map_smul' a p := by dsimp; rw [sum_smul_index] <;> simp only [mul_sum, ← C_mul', mul_assoc, coeff_C_mul, 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, eq_self_iff_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] · rw [if_pos (add_tsub_cancel_right n 1).symm, mul_one, Nat.cast_add, Nat.cast_one, mem_support_iff] intro h push_neg at h simp [h] -- Porting note (#10618): removed `simp`: `simp` can prove it. 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 @[simp] 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 theorem derivative_C_mul_X (a : R) : derivative (C a * X) = C a := by simp [C_mul_X_eq_monomial, derivative_monomial, Nat.cast_one, 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] @[simp] 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 -- Porting note (#10618): removed `simp`: `simp` can prove it. 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 -- Porting note (#10618): removed `simp`: `simp` can prove it. theorem derivative_add {f g : R[X]} : derivative (f + g) = derivative f + derivative g := derivative.map_add f g -- Porting note (#10618): removed `simp`: `simp` can prove it. theorem derivative_X_add_C (c : R) : derivative (X + C c) = 1 := by rw [derivative_add, derivative_X, derivative_C, add_zero] -- Porting note (#10618): removed `simp`: `simp` can prove it. theorem derivative_sum {s : Finset ι} {f : ι → R[X]} : derivative (∑ b ∈ s, f b) = ∑ b ∈ s, derivative (f b) := map_sum .. -- Porting note (#10618): removed `simp`: `simp` can prove it. theorem derivative_smul {S : Type*} [Monoid S] [DistribMulAction 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*} [Monoid S] [DistribMulAction S R] [IsScalarTower S R R] (s : S) (p : R[X]) (k : ℕ) : derivative^[k] (s • p) = s • derivative^[k] p := by induction' k with k ih generalizing p · simp · 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 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 @[deprecated (since := "2024-04-17")] alias derivative_nat_cast := derivative_natCast @[simp] theorem derivative_ofNat (n : ℕ) [n.AtLeastTwo] : derivative (no_index (OfNat.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 with _ ih generalizing p x 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 [NoZeroSMulDivisors ℕ 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 [NoZeroSMulDivisors ℕ 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 | h_add => simp only [add_mul, map_add, add_assoc, add_left_comm, *] | h_monomial m a => induction g using Polynomial.induction_on' with | h_add => simp only [mul_add, map_add, add_assoc, add_left_comm, *] | h_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 => simp only [Nat.add_succ_sub_one, add_tsub_cancel_right] rw [add_assoc, add_comm n 1] 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 with k ih generalizing p · simp · 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 @[deprecated (since := "2024-04-17")] alias derivative_nat_cast_mul := derivative_natCast_mul @[simp] theorem iterate_derivative_natCast_mul {n k : ℕ} {f : R[X]} : derivative^[k] ((n : R[X]) * f) = n * derivative^[k] f := by induction' k with k ih generalizing f <;> simp [*] @[deprecated (since := "2024-04-17")] alias iterate_derivative_nat_cast_mul := iterate_derivative_natCast_mul theorem mem_support_derivative [NoZeroSMulDivisors ℕ 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_iff] @[simp] theorem degree_derivative_eq [NoZeroSMulDivisors ℕ 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_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 n IH · simp [Finset.range] 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 omega · rw [Nat.choose_zero_right, tsub_zero] 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 k ih · erw [Function.iterate_zero_apply, tsub_zero, Nat.descFactorial_zero, Nat.cast_one, one_mul] · 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_comm, mul_assoc, mul_left_comm] @[deprecated (since := "2024-04-17")] alias iterate_derivative_X_pow_eq_nat_cast_mul := iterate_derivative_X_pow_eq_natCast_mul 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.prod_cons, ← Multiset.map_cons, Multiset.cons_erase hj] · simp [hij] end CommSemiring section Ring variable [Ring R] -- Porting note (#10618): removed `simp`: `simp` can prove it. 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 -- Porting note (#10618): removed `simp`: `simp` can prove it. theorem derivative_sub {f g : R[X]} : derivative (f - g) = derivative f - derivative g := LinearMap.map_sub derivative f g -- Porting note (#10618): removed `simp`: `simp` can prove it. 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 @[deprecated (since := "2024-04-17")] alias derivative_int_cast := derivative_intCast theorem derivative_intCast_mul {n : ℤ} {f : R[X]} : derivative ((n : R[X]) * f) = n * derivative f := by simp @[deprecated (since := "2024-04-17")] alias derivative_int_cast_mul := derivative_intCast_mul @[simp] theorem iterate_derivative_intCast_mul {n : ℤ} {k : ℕ} {f : R[X]} : derivative^[k] ((n : R[X]) * f) = n * derivative^[k] f := by induction' k with k ih generalizing f <;> simp [*] @[deprecated (since := "2024-04-17")] alias iterate_derivative_int_cast_mul := iterate_derivative_intCast_mul 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 with k ih generalizing p · simp · simp [ih (derivative p), iterate_derivative_neg, 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 end Derivative end Polynomial
Algebra\Polynomial\Div.lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker -/ import Mathlib.Algebra.Polynomial.Inductions import Mathlib.Algebra.Polynomial.Monic import Mathlib.RingTheory.Multiplicity import Mathlib.RingTheory.Ideal.Maps /-! # 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_lt hd, IsEmpty.forall_iff], fun hd => by induction' n with n hn · simp [pow_zero, one_dvd] · 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 multiplicity_finite_of_degree_pos_of_monic (hp : (0 : WithBot ℕ) < degree p) (hmp : Monic p) (hq : q ≠ 0) : multiplicity.Finite 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_pow, 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` 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` 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 unfold modByMonic at this ⊢ unfold divModByMonicAux dsimp rw [dif_pos hq] at this ⊢ rw [if_pos h] exact this 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 classical unfold modByMonic divModByMonicAux dsimp by_cases hp : Monic p · rw [dif_pos hp, if_neg (mt And.right (not_not_intro rfl))] · rw [dif_neg hp] @[simp] theorem zero_divByMonic (p : R[X]) : 0 /ₘ p = 0 := by classical unfold divByMonic divModByMonicAux dsimp by_cases hp : Monic p · rw [dif_pos hp, if_neg (mt And.right (not_not_intro rfl))] · rw [dif_neg hp] @[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 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 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, false_and_iff, if_false, 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, mul_comm, 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 erw [degree_eq_natDegree hg.ne_zero, degree_eq_natDegree hqf, WithBot.coe_le_coe] 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)⟩ @[deprecated (since := "2024-03-23")] alias dvd_iff_modByMonic_eq_zero := modByMonic_eq_zero_iff_dvd /-- See `Polynomial.mul_left_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) @[deprecated (since := "2024-06-30")] alias mul_div_mod_by_monic_cancel_left := mul_divByMonic_cancel_left 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 [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_le 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_le, 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]; norm_num · 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 multiplicity_X_sub_C_finite (a : R) (h0 : p ≠ 0) : multiplicity.Finite (X - C a) p := by haveI := Nontrivial.of_polynomial_ne h0 refine multiplicity_finite_of_degree_pos_of_monic ?_ (monic_X_sub_C _) h0 rw [degree_X_sub_C] decide /- Porting note: 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 => @Not.decidable _ (decidableDvdMonic p ((monic_X_sub_C a).pow (n + 1))) Nat.find (multiplicity_X_sub_C_finite a h0) /- Porting note: added the following due to diamond with decidableProp and decidableDvdMonic see also [Zulip] (https://leanprover.zulipchat.com/#narrow/stream/287929-mathlib4/topic/non-defeq.20aliased.20instance) -/ theorem rootMultiplicity_eq_nat_find_of_nonzero [DecidableEq R] {p : R[X]} (p0 : p ≠ 0) {a : R} : letI : DecidablePred fun n : ℕ => ¬(X - C a) ^ (n + 1) ∣ p := fun n => @Not.decidable _ (decidableDvdMonic p ((monic_X_sub_C a).pow (n + 1))) rootMultiplicity a p = Nat.find (multiplicity_X_sub_C_finite a p0) := by dsimp [rootMultiplicity] cases Subsingleton.elim ‹DecidableEq R› (Classical.decEq R) rw [dif_neg p0] theorem rootMultiplicity_eq_multiplicity [DecidableEq R] [@DecidableRel R[X] (· ∣ ·)] (p : R[X]) (a : R) : rootMultiplicity a p = if h0 : p = 0 then 0 else (multiplicity (X - C a) p).get (multiplicity_X_sub_C_finite a h0) := by simp [multiplicity, rootMultiplicity, Part.Dom]; congr; funext; 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.multiplicity_eq_zero.mpr ((monic_X_sub_C a).not_dvd_of_natDegree_lt hr h), PartENat.get_zero] 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, dif_neg h]; exact multiplicity.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, dif_neg hp] apply multiplicity.exists_eq_pow_mul_and_not_dvd end multiplicity end Ring section CommRing variable [CommRing R] {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] theorem mem_span_C_X_sub_C_X_sub_C_iff_eval_eval_eq_zero {b : R[X]} {P : R[X][X]} : P ∈ Ideal.span {C (X - C a), X - C b} ↔ (P.eval b).eval a = 0 := by rw [Ideal.mem_span_pair] constructor <;> intro h · rcases h with ⟨_, _, rfl⟩ simp only [eval_C, eval_X, eval_add, eval_sub, eval_mul, add_zero, mul_zero, sub_self] · rcases dvd_iff_isRoot.mpr h with ⟨p, hp⟩ rcases @X_sub_C_dvd_sub_C_eval _ b _ P with ⟨q, hq⟩ exact ⟨C p, q, by rw [mul_comm, mul_comm q, eq_add_of_sub_eq' hq, hp, C_mul]⟩ -- 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 (evalRingHom a).map_dvd this simp [dvd_iff_isRoot] theorem ker_evalRingHom (x : R) : RingHom.ker (evalRingHom x) = Ideal.span {X - C x} := by ext y simp [Ideal.mem_span_singleton, dvd_iff_isRoot, RingHom.mem_ker] @[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, dite_eq_left_iff, PartENat.get_eq_iff_eq_coe, Nat.cast_zero, multiplicity.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, dif_neg hp] at this exact multiplicity.is_greatest' (multiplicity_finite_of_degree_pos_of_monic (show (0 : WithBot ℕ) < degree (X - C a) by rw [degree_X_sub_C]; decide) (monic_X_sub_C _) hp) (Nat.lt_succ_self _) (dvd_of_mul_right_eq _ this) /-- See `Polynomial.mul_right_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 end CommRing end Polynomial
Algebra\Polynomial\EraseLead.lean
/- Copyright (c) 2020 Damiano Testa. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Damiano Testa, Alex Meiburg -/ import Mathlib.Algebra.BigOperators.Fin import Mathlib.Algebra.Polynomial.Degree.Lemmas /-! # Erase the leading term of a univariate polynomial ## Definition * `eraseLead f`: the polynomial `f - leading term of f` `eraseLead` serves as reduction step in an induction, shaving off one monomial from a polynomial. The definition is set up so that it does not mention subtraction in the definition, and thus works for polynomials over semirings as well as rings. -/ noncomputable section open Polynomial open Polynomial Finset namespace Polynomial variable {R : Type*} [Semiring R] {f : R[X]} /-- `eraseLead f` for a polynomial `f` is the polynomial obtained by subtracting from `f` the leading term of `f`. -/ def eraseLead (f : R[X]) : R[X] := Polynomial.erase f.natDegree f section EraseLead theorem eraseLead_support (f : R[X]) : f.eraseLead.support = f.support.erase f.natDegree := by simp only [eraseLead, support_erase] theorem eraseLead_coeff (i : ℕ) : f.eraseLead.coeff i = if i = f.natDegree then 0 else f.coeff i := by simp only [eraseLead, coeff_erase] @[simp] theorem eraseLead_coeff_natDegree : f.eraseLead.coeff f.natDegree = 0 := by simp [eraseLead_coeff] theorem eraseLead_coeff_of_ne (i : ℕ) (hi : i ≠ f.natDegree) : f.eraseLead.coeff i = f.coeff i := by simp [eraseLead_coeff, hi] @[simp] theorem eraseLead_zero : eraseLead (0 : R[X]) = 0 := by simp only [eraseLead, erase_zero] @[simp] theorem eraseLead_add_monomial_natDegree_leadingCoeff (f : R[X]) : f.eraseLead + monomial f.natDegree f.leadingCoeff = f := (add_comm _ _).trans (f.monomial_add_erase _) @[simp] theorem eraseLead_add_C_mul_X_pow (f : R[X]) : f.eraseLead + C f.leadingCoeff * X ^ f.natDegree = f := by rw [C_mul_X_pow_eq_monomial, eraseLead_add_monomial_natDegree_leadingCoeff] @[simp] theorem self_sub_monomial_natDegree_leadingCoeff {R : Type*} [Ring R] (f : R[X]) : f - monomial f.natDegree f.leadingCoeff = f.eraseLead := (eq_sub_iff_add_eq.mpr (eraseLead_add_monomial_natDegree_leadingCoeff f)).symm @[simp] theorem self_sub_C_mul_X_pow {R : Type*} [Ring R] (f : R[X]) : f - C f.leadingCoeff * X ^ f.natDegree = f.eraseLead := by rw [C_mul_X_pow_eq_monomial, self_sub_monomial_natDegree_leadingCoeff] theorem eraseLead_ne_zero (f0 : 2 ≤ f.support.card) : eraseLead f ≠ 0 := by rw [Ne, ← card_support_eq_zero, eraseLead_support] exact (zero_lt_one.trans_le <| (tsub_le_tsub_right f0 1).trans Finset.pred_card_le_card_erase).ne.symm theorem lt_natDegree_of_mem_eraseLead_support {a : ℕ} (h : a ∈ (eraseLead f).support) : a < f.natDegree := by rw [eraseLead_support, mem_erase] at h exact (le_natDegree_of_mem_supp a h.2).lt_of_ne h.1 theorem ne_natDegree_of_mem_eraseLead_support {a : ℕ} (h : a ∈ (eraseLead f).support) : a ≠ f.natDegree := (lt_natDegree_of_mem_eraseLead_support h).ne theorem natDegree_not_mem_eraseLead_support : f.natDegree ∉ (eraseLead f).support := fun h => ne_natDegree_of_mem_eraseLead_support h rfl theorem eraseLead_support_card_lt (h : f ≠ 0) : (eraseLead f).support.card < f.support.card := by rw [eraseLead_support] exact card_lt_card (erase_ssubset <| natDegree_mem_support_of_nonzero h) theorem card_support_eraseLead_add_one (h : f ≠ 0) : f.eraseLead.support.card + 1 = f.support.card := by set c := f.support.card with hc cases h₁ : c case zero => by_contra exact h (card_support_eq_zero.mp h₁) case succ => rw [eraseLead_support, card_erase_of_mem (natDegree_mem_support_of_nonzero h), ← hc, h₁] rfl @[simp] theorem card_support_eraseLead : f.eraseLead.support.card = f.support.card - 1 := by by_cases hf : f = 0 · rw [hf, eraseLead_zero, support_zero, card_empty] · rw [← card_support_eraseLead_add_one hf, add_tsub_cancel_right] theorem card_support_eraseLead' {c : ℕ} (fc : f.support.card = c + 1) : f.eraseLead.support.card = c := by rw [card_support_eraseLead, fc, add_tsub_cancel_right] theorem card_support_eq_one_of_eraseLead_eq_zero (h₀ : f ≠ 0) (h₁ : f.eraseLead = 0) : f.support.card = 1 := (card_support_eq_zero.mpr h₁ ▸ card_support_eraseLead_add_one h₀).symm theorem card_support_le_one_of_eraseLead_eq_zero (h : f.eraseLead = 0) : f.support.card ≤ 1 := by by_cases hpz : f = 0 case pos => simp [hpz] case neg => exact le_of_eq (card_support_eq_one_of_eraseLead_eq_zero hpz h) @[simp] theorem eraseLead_monomial (i : ℕ) (r : R) : eraseLead (monomial i r) = 0 := by classical by_cases hr : r = 0 · subst r simp only [monomial_zero_right, eraseLead_zero] · rw [eraseLead, natDegree_monomial, if_neg hr, erase_monomial] @[simp] theorem eraseLead_C (r : R) : eraseLead (C r) = 0 := eraseLead_monomial _ _ @[simp] theorem eraseLead_X : eraseLead (X : R[X]) = 0 := eraseLead_monomial _ _ @[simp] theorem eraseLead_X_pow (n : ℕ) : eraseLead (X ^ n : R[X]) = 0 := by rw [X_pow_eq_monomial, eraseLead_monomial] @[simp] theorem eraseLead_C_mul_X_pow (r : R) (n : ℕ) : eraseLead (C r * X ^ n) = 0 := by rw [C_mul_X_pow_eq_monomial, eraseLead_monomial] @[simp] lemma eraseLead_C_mul_X (r : R) : eraseLead (C r * X) = 0 := by simpa using eraseLead_C_mul_X_pow _ 1 theorem eraseLead_add_of_natDegree_lt_left {p q : R[X]} (pq : q.natDegree < p.natDegree) : (p + q).eraseLead = p.eraseLead + q := by ext n by_cases nd : n = p.natDegree · rw [nd, eraseLead_coeff, if_pos (natDegree_add_eq_left_of_natDegree_lt pq).symm] simpa using (coeff_eq_zero_of_natDegree_lt pq).symm · rw [eraseLead_coeff, coeff_add, coeff_add, eraseLead_coeff, if_neg, if_neg nd] rintro rfl exact nd (natDegree_add_eq_left_of_natDegree_lt pq) theorem eraseLead_add_of_natDegree_lt_right {p q : R[X]} (pq : p.natDegree < q.natDegree) : (p + q).eraseLead = p + q.eraseLead := by ext n by_cases nd : n = q.natDegree · rw [nd, eraseLead_coeff, if_pos (natDegree_add_eq_right_of_natDegree_lt pq).symm] simpa using (coeff_eq_zero_of_natDegree_lt pq).symm · rw [eraseLead_coeff, coeff_add, coeff_add, eraseLead_coeff, if_neg, if_neg nd] rintro rfl exact nd (natDegree_add_eq_right_of_natDegree_lt pq) theorem eraseLead_degree_le : (eraseLead f).degree ≤ f.degree := f.degree_erase_le _ theorem eraseLead_natDegree_le_aux : (eraseLead f).natDegree ≤ f.natDegree := natDegree_le_natDegree eraseLead_degree_le theorem eraseLead_natDegree_lt (f0 : 2 ≤ f.support.card) : (eraseLead f).natDegree < f.natDegree := lt_of_le_of_ne eraseLead_natDegree_le_aux <| ne_natDegree_of_mem_eraseLead_support <| natDegree_mem_support_of_nonzero <| eraseLead_ne_zero f0 theorem natDegree_pos_of_eraseLead_ne_zero (h : f.eraseLead ≠ 0) : 0 < f.natDegree := by by_contra h₂ rw [eq_C_of_natDegree_eq_zero (Nat.eq_zero_of_not_pos h₂)] at h simp at h theorem eraseLead_natDegree_lt_or_eraseLead_eq_zero (f : R[X]) : (eraseLead f).natDegree < f.natDegree ∨ f.eraseLead = 0 := by by_cases h : f.support.card ≤ 1 · right rw [← C_mul_X_pow_eq_self h] simp · left apply eraseLead_natDegree_lt (lt_of_not_ge h) theorem eraseLead_natDegree_le (f : R[X]) : (eraseLead f).natDegree ≤ f.natDegree - 1 := by rcases f.eraseLead_natDegree_lt_or_eraseLead_eq_zero with (h | h) · exact Nat.le_sub_one_of_lt h · simp only [h, natDegree_zero, zero_le] lemma natDegree_eraseLead (h : f.nextCoeff ≠ 0) : f.eraseLead.natDegree = f.natDegree - 1 := by have := natDegree_pos_of_nextCoeff_ne_zero h refine f.eraseLead_natDegree_le.antisymm $ le_natDegree_of_ne_zero ?_ rwa [eraseLead_coeff_of_ne _ (tsub_lt_self _ _).ne, ← nextCoeff_of_natDegree_pos] all_goals positivity lemma natDegree_eraseLead_add_one (h : f.nextCoeff ≠ 0) : f.eraseLead.natDegree + 1 = f.natDegree := by rw [natDegree_eraseLead h, tsub_add_cancel_of_le] exact natDegree_pos_of_nextCoeff_ne_zero h theorem natDegree_eraseLead_le_of_nextCoeff_eq_zero (h : f.nextCoeff = 0) : f.eraseLead.natDegree ≤ f.natDegree - 2 := by refine natDegree_le_pred (n := f.natDegree - 1) (eraseLead_natDegree_le f) ?_ rw [nextCoeff_eq_zero, natDegree_eq_zero] at h obtain ⟨a, rfl⟩ | ⟨hf, h⟩ := h · simp rw [eraseLead_coeff_of_ne _ (tsub_lt_self hf zero_lt_one).ne, ← nextCoeff_of_natDegree_pos hf] simp [nextCoeff_eq_zero, h, eq_zero_or_pos] lemma two_le_natDegree_of_nextCoeff_eraseLead (hlead : f.eraseLead ≠ 0) (hnext : f.nextCoeff = 0) : 2 ≤ f.natDegree := by contrapose! hlead rw [Nat.lt_succ_iff, Nat.le_one_iff_eq_zero_or_eq_one, natDegree_eq_zero, natDegree_eq_one] at hlead obtain ⟨a, rfl⟩ | ⟨a, ha, b, rfl⟩ := hlead · simp · rw [nextCoeff_C_mul_X_add_C ha] at hnext subst b simp theorem leadingCoeff_eraseLead_eq_nextCoeff (h : f.nextCoeff ≠ 0) : f.eraseLead.leadingCoeff = f.nextCoeff := by have := natDegree_pos_of_nextCoeff_ne_zero h rw [leadingCoeff, nextCoeff, natDegree_eraseLead h, if_neg, eraseLead_coeff_of_ne _ (tsub_lt_self _ _).ne] all_goals positivity theorem nextCoeff_eq_zero_of_eraseLead_eq_zero (h : f.eraseLead = 0) : f.nextCoeff = 0 := by by_contra h₂ exact leadingCoeff_ne_zero.mp (leadingCoeff_eraseLead_eq_nextCoeff h₂ ▸ h₂) h end EraseLead /-- An induction lemma for polynomials. It takes a natural number `N` as a parameter, that is required to be at least as big as the `nat_degree` of the polynomial. This is useful to prove results where you want to change each term in a polynomial to something else depending on the `nat_degree` of the polynomial itself and not on the specific `nat_degree` of each term. -/ theorem induction_with_natDegree_le (P : R[X] → Prop) (N : ℕ) (P_0 : P 0) (P_C_mul_pow : ∀ n : ℕ, ∀ r : R, r ≠ 0 → n ≤ N → P (C r * X ^ n)) (P_C_add : ∀ f g : R[X], f.natDegree < g.natDegree → g.natDegree ≤ N → P f → P g → P (f + g)) : ∀ f : R[X], f.natDegree ≤ N → P f := by intro f df generalize hd : card f.support = c revert f induction' c with c hc · intro f _ f0 convert P_0 simpa [support_eq_empty, card_eq_zero] using f0 · intro f df f0 rw [← eraseLead_add_C_mul_X_pow f] cases c · convert P_C_mul_pow f.natDegree f.leadingCoeff ?_ df using 1 · convert zero_add (C (leadingCoeff f) * X ^ f.natDegree) rw [← card_support_eq_zero, card_support_eraseLead' f0] · rw [leadingCoeff_ne_zero, Ne, ← card_support_eq_zero, f0] exact zero_ne_one.symm refine P_C_add f.eraseLead _ ?_ ?_ ?_ ?_ · refine (eraseLead_natDegree_lt ?_).trans_le (le_of_eq ?_) · exact (Nat.succ_le_succ (Nat.succ_le_succ (Nat.zero_le _))).trans f0.ge · rw [natDegree_C_mul_X_pow _ _ (leadingCoeff_ne_zero.mpr _)] rintro rfl simp at f0 · exact (natDegree_C_mul_X_pow_le f.leadingCoeff f.natDegree).trans df · exact hc _ (eraseLead_natDegree_le_aux.trans df) (card_support_eraseLead' f0) · refine P_C_mul_pow _ _ ?_ df rw [Ne, leadingCoeff_eq_zero, ← card_support_eq_zero, f0] exact Nat.succ_ne_zero _ /-- Let `φ : R[x] → S[x]` be an additive map, `k : ℕ` a bound, and `fu : ℕ → ℕ` a "sufficiently monotone" map. Assume also that * `φ` maps to `0` all monomials of degree less than `k`, * `φ` maps each monomial `m` in `R[x]` to a polynomial `φ m` of degree `fu (deg m)`. Then, `φ` maps each polynomial `p` in `R[x]` to a polynomial of degree `fu (deg p)`. -/ theorem mono_map_natDegree_eq {S F : Type*} [Semiring S] [FunLike F R[X] S[X]] [AddMonoidHomClass F R[X] S[X]] {φ : F} {p : R[X]} (k : ℕ) (fu : ℕ → ℕ) (fu0 : ∀ {n}, n ≤ k → fu n = 0) (fc : ∀ {n m}, k ≤ n → n < m → fu n < fu m) (φ_k : ∀ {f : R[X]}, f.natDegree < k → φ f = 0) (φ_mon_nat : ∀ n c, c ≠ 0 → (φ (monomial n c)).natDegree = fu n) : (φ p).natDegree = fu p.natDegree := by refine induction_with_natDegree_le (fun p => (φ p).natDegree = fu p.natDegree) p.natDegree (by simp [fu0]) ?_ ?_ _ rfl.le · intro n r r0 _ rw [natDegree_C_mul_X_pow _ _ r0, C_mul_X_pow_eq_monomial, φ_mon_nat _ _ r0] · intro f g fg _ fk gk rw [natDegree_add_eq_right_of_natDegree_lt fg, _root_.map_add] by_cases FG : k ≤ f.natDegree · rw [natDegree_add_eq_right_of_natDegree_lt, gk] rw [fk, gk] exact fc FG fg · cases k · exact (FG (Nat.zero_le _)).elim · rwa [φ_k (not_le.mp FG), zero_add] theorem map_natDegree_eq_sub {S F : Type*} [Semiring S] [FunLike F R[X] S[X]] [AddMonoidHomClass F R[X] S[X]] {φ : F} {p : R[X]} {k : ℕ} (φ_k : ∀ f : R[X], f.natDegree < k → φ f = 0) (φ_mon : ∀ n c, c ≠ 0 → (φ (monomial n c)).natDegree = n - k) : (φ p).natDegree = p.natDegree - k := mono_map_natDegree_eq k (fun j => j - k) (by simp_all) (@fun m n h => (tsub_lt_tsub_iff_right h).mpr) (φ_k _) φ_mon theorem map_natDegree_eq_natDegree {S F : Type*} [Semiring S] [FunLike F R[X] S[X]] [AddMonoidHomClass F R[X] S[X]] {φ : F} (p) (φ_mon_nat : ∀ n c, c ≠ 0 → (φ (monomial n c)).natDegree = n) : (φ p).natDegree = p.natDegree := (map_natDegree_eq_sub (fun f h => (Nat.not_lt_zero _ h).elim) (by simpa)).trans p.natDegree.sub_zero theorem card_support_eq' {n : ℕ} (k : Fin n → ℕ) (x : Fin n → R) (hk : Function.Injective k) (hx : ∀ i, x i ≠ 0) : (∑ i, C (x i) * X ^ k i).support.card = n := by suffices (∑ i, C (x i) * X ^ k i).support = image k univ by rw [this, univ.card_image_of_injective hk, card_fin] simp_rw [Finset.ext_iff, mem_support_iff, finset_sum_coeff, coeff_C_mul_X_pow, mem_image, mem_univ, true_and] refine fun i => ⟨fun h => ?_, ?_⟩ · obtain ⟨j, _, h⟩ := exists_ne_zero_of_sum_ne_zero h exact ⟨j, (ite_ne_right_iff.mp h).1.symm⟩ · rintro ⟨j, _, rfl⟩ rw [sum_eq_single_of_mem j (mem_univ j), if_pos rfl] · exact hx j · exact fun m _ hmj => if_neg fun h => hmj.symm (hk h) theorem card_support_eq {n : ℕ} : f.support.card = n ↔ ∃ (k : Fin n → ℕ) (x : Fin n → R) (hk : StrictMono k) (hx : ∀ i, x i ≠ 0), f = ∑ i, C (x i) * X ^ k i := by refine ⟨?_, fun ⟨k, x, hk, hx, hf⟩ => hf.symm ▸ card_support_eq' k x hk.injective hx⟩ induction' n with n hn generalizing f · exact fun hf => ⟨0, 0, fun x => x.elim0, fun x => x.elim0, card_support_eq_zero.mp hf⟩ · intro h obtain ⟨k, x, hk, hx, hf⟩ := hn (card_support_eraseLead' h) have H : ¬∃ k : Fin n, Fin.castSucc k = Fin.last n := by rintro ⟨i, hi⟩ exact i.castSucc_lt_last.ne hi refine ⟨Function.extend Fin.castSucc k fun _ => f.natDegree, Function.extend Fin.castSucc x fun _ => f.leadingCoeff, ?_, ?_, ?_⟩ · intro i j hij have hi : i ∈ Set.range (Fin.castSucc : Fin n → Fin (n + 1)) := by rw [Fin.range_castSucc, Set.mem_def] exact lt_of_lt_of_le hij (Nat.lt_succ_iff.mp j.2) obtain ⟨i, rfl⟩ := hi rw [Fin.strictMono_castSucc.injective.extend_apply] by_cases hj : ∃ j₀, Fin.castSucc j₀ = j · obtain ⟨j, rfl⟩ := hj rwa [Fin.strictMono_castSucc.injective.extend_apply, hk.lt_iff_lt, ← Fin.castSucc_lt_castSucc_iff] · rw [Function.extend_apply' _ _ _ hj] apply lt_natDegree_of_mem_eraseLead_support rw [mem_support_iff, hf, finset_sum_coeff] rw [sum_eq_single, coeff_C_mul, coeff_X_pow_self, mul_one] · exact hx i · intro j _ hji rw [coeff_C_mul, coeff_X_pow, if_neg (hk.injective.ne hji.symm), mul_zero] · exact fun hi => (hi (mem_univ i)).elim · intro i by_cases hi : ∃ i₀, Fin.castSucc i₀ = i · obtain ⟨i, rfl⟩ := hi rw [Fin.strictMono_castSucc.injective.extend_apply] exact hx i · rw [Function.extend_apply' _ _ _ hi, Ne, leadingCoeff_eq_zero, ← card_support_eq_zero, h] exact n.succ_ne_zero · rw [Fin.sum_univ_castSucc] simp only [Fin.strictMono_castSucc.injective.extend_apply] rw [← hf, Function.extend_apply', Function.extend_apply', eraseLead_add_C_mul_X_pow] all_goals exact H theorem card_support_eq_one : f.support.card = 1 ↔ ∃ (k : ℕ) (x : R) (hx : x ≠ 0), f = C x * X ^ k := by refine ⟨fun h => ?_, ?_⟩ · obtain ⟨k, x, _, hx, rfl⟩ := card_support_eq.mp h exact ⟨k 0, x 0, hx 0, Fin.sum_univ_one _⟩ · rintro ⟨k, x, hx, rfl⟩ rw [support_C_mul_X_pow k hx, card_singleton] theorem card_support_eq_two : f.support.card = 2 ↔ ∃ (k m : ℕ) (hkm : k < m) (x y : R) (hx : x ≠ 0) (hy : y ≠ 0), f = C x * X ^ k + C y * X ^ m := by refine ⟨fun h => ?_, ?_⟩ · obtain ⟨k, x, hk, hx, rfl⟩ := card_support_eq.mp h refine ⟨k 0, k 1, hk Nat.zero_lt_one, x 0, x 1, hx 0, hx 1, ?_⟩ rw [Fin.sum_univ_castSucc, Fin.sum_univ_one] rfl · rintro ⟨k, m, hkm, x, y, hx, hy, rfl⟩ exact card_support_binomial hkm.ne hx hy theorem card_support_eq_three : f.support.card = 3 ↔ ∃ (k m n : ℕ) (hkm : k < m) (hmn : m < n) (x y z : R) (hx : x ≠ 0) (hy : y ≠ 0) (hz : z ≠ 0), f = C x * X ^ k + C y * X ^ m + C z * X ^ n := by refine ⟨fun h => ?_, ?_⟩ · obtain ⟨k, x, hk, hx, rfl⟩ := card_support_eq.mp h refine ⟨k 0, k 1, k 2, hk Nat.zero_lt_one, hk (Nat.lt_succ_self 1), x 0, x 1, x 2, hx 0, hx 1, hx 2, ?_⟩ rw [Fin.sum_univ_castSucc, Fin.sum_univ_castSucc, Fin.sum_univ_one] rfl · rintro ⟨k, m, n, hkm, hmn, x, y, z, hx, hy, hz, rfl⟩ exact card_support_trinomial hkm hmn hx hy hz end Polynomial
Algebra\Polynomial\Eval.lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker -/ import Mathlib.Algebra.Polynomial.Degree.Definitions import Mathlib.Algebra.Polynomial.Induction /-! # Theory of univariate polynomials The main defs here are `eval₂`, `eval`, and `map`. We give several lemmas about their interaction with each other and with module operations. -/ noncomputable section open Finset AddMonoidAlgebra open Polynomial namespace Polynomial universe u v w y variable {R : Type u} {S : Type v} {T : Type w} {ι : Type y} {a b : R} {m n : ℕ} section Semiring variable [Semiring R] {p q r : R[X]} section variable [Semiring S] variable (f : R →+* S) (x : S) /-- Evaluate a polynomial `p` given a ring hom `f` from the scalar ring to the target and a value `x` for the variable in the target -/ irreducible_def eval₂ (p : R[X]) : S := p.sum fun e a => f a * x ^ e theorem eval₂_eq_sum {f : R →+* S} {x : S} : p.eval₂ f x = p.sum fun e a => f a * x ^ e := by rw [eval₂_def] theorem eval₂_congr {R S : Type*} [Semiring R] [Semiring S] {f g : R →+* S} {s t : S} {φ ψ : R[X]} : f = g → s = t → φ = ψ → eval₂ f s φ = eval₂ g t ψ := by rintro rfl rfl rfl; rfl @[simp] theorem eval₂_at_zero : p.eval₂ f 0 = f (coeff p 0) := by simp (config := { contextual := true }) only [eval₂_eq_sum, zero_pow_eq, mul_ite, mul_zero, mul_one, sum, Classical.not_not, mem_support_iff, sum_ite_eq', ite_eq_left_iff, RingHom.map_zero, imp_true_iff, eq_self_iff_true] @[simp] theorem eval₂_zero : (0 : R[X]).eval₂ f x = 0 := by simp [eval₂_eq_sum] @[simp] theorem eval₂_C : (C a).eval₂ f x = f a := by simp [eval₂_eq_sum] @[simp] theorem eval₂_X : X.eval₂ f x = x := by simp [eval₂_eq_sum] @[simp] theorem eval₂_monomial {n : ℕ} {r : R} : (monomial n r).eval₂ f x = f r * x ^ n := by simp [eval₂_eq_sum] @[simp] theorem eval₂_X_pow {n : ℕ} : (X ^ n).eval₂ f x = x ^ n := by rw [X_pow_eq_monomial] convert eval₂_monomial f x (n := n) (r := 1) simp @[simp] theorem eval₂_add : (p + q).eval₂ f x = p.eval₂ f x + q.eval₂ f x := by simp only [eval₂_eq_sum] apply sum_add_index <;> simp [add_mul] @[simp] theorem eval₂_one : (1 : R[X]).eval₂ f x = 1 := by rw [← C_1, eval₂_C, f.map_one] @[simp] theorem eval₂_smul (g : R →+* S) (p : R[X]) (x : S) {s : R} : eval₂ g x (s • p) = g s * eval₂ g x p := by have A : p.natDegree < p.natDegree.succ := Nat.lt_succ_self _ have B : (s • p).natDegree < p.natDegree.succ := (natDegree_smul_le _ _).trans_lt A rw [eval₂_eq_sum, eval₂_eq_sum, sum_over_range' _ _ _ A, sum_over_range' _ _ _ B] <;> simp [mul_sum, mul_assoc] @[simp] theorem eval₂_C_X : eval₂ C X p = p := Polynomial.induction_on' p (fun p q hp hq => by simp [hp, hq]) fun n x => by rw [eval₂_monomial, ← smul_X_eq_monomial, C_mul'] /-- `eval₂AddMonoidHom (f : R →+* S) (x : S)` is the `AddMonoidHom` from `R[X]` to `S` obtained by evaluating the pushforward of `p` along `f` at `x`. -/ @[simps] def eval₂AddMonoidHom : R[X] →+ S where toFun := eval₂ f x map_zero' := eval₂_zero _ _ map_add' _ _ := eval₂_add _ _ @[simp] theorem eval₂_natCast (n : ℕ) : (n : R[X]).eval₂ f x = n := by induction' n with n ih -- Porting note: `Nat.zero_eq` is required. · simp only [eval₂_zero, Nat.cast_zero, Nat.zero_eq] · rw [n.cast_succ, eval₂_add, ih, eval₂_one, n.cast_succ] @[deprecated (since := "2024-04-17")] alias eval₂_nat_cast := eval₂_natCast -- See note [no_index around OfNat.ofNat] @[simp] lemma eval₂_ofNat {S : Type*} [Semiring S] (n : ℕ) [n.AtLeastTwo] (f : R →+* S) (a : S) : (no_index (OfNat.ofNat n : R[X])).eval₂ f a = OfNat.ofNat n := by simp [OfNat.ofNat] variable [Semiring T] theorem eval₂_sum (p : T[X]) (g : ℕ → T → R[X]) (x : S) : (p.sum g).eval₂ f x = p.sum fun n a => (g n a).eval₂ f x := by let T : R[X] →+ S := { toFun := eval₂ f x map_zero' := eval₂_zero _ _ map_add' := fun p q => eval₂_add _ _ } have A : ∀ y, eval₂ f x y = T y := fun y => rfl simp only [A] rw [sum, map_sum, sum] theorem eval₂_list_sum (l : List R[X]) (x : S) : eval₂ f x l.sum = (l.map (eval₂ f x)).sum := map_list_sum (eval₂AddMonoidHom f x) l theorem eval₂_multiset_sum (s : Multiset R[X]) (x : S) : eval₂ f x s.sum = (s.map (eval₂ f x)).sum := map_multiset_sum (eval₂AddMonoidHom f x) s theorem eval₂_finset_sum (s : Finset ι) (g : ι → R[X]) (x : S) : (∑ i ∈ s, g i).eval₂ f x = ∑ i ∈ s, (g i).eval₂ f x := map_sum (eval₂AddMonoidHom f x) _ _ theorem eval₂_ofFinsupp {f : R →+* S} {x : S} {p : R[ℕ]} : eval₂ f x (⟨p⟩ : R[X]) = liftNC (↑f) (powersHom S x) p := by simp only [eval₂_eq_sum, sum, toFinsupp_sum, support, coeff] rfl theorem eval₂_mul_noncomm (hf : ∀ k, Commute (f <| q.coeff k) x) : eval₂ f x (p * q) = eval₂ f x p * eval₂ f x q := by rcases p with ⟨p⟩; rcases q with ⟨q⟩ simp only [coeff] at hf simp only [← ofFinsupp_mul, eval₂_ofFinsupp] exact liftNC_mul _ _ p q fun {k n} _hn => (hf k).pow_right n @[simp] theorem eval₂_mul_X : eval₂ f x (p * X) = eval₂ f x p * x := by refine _root_.trans (eval₂_mul_noncomm _ _ fun k => ?_) (by rw [eval₂_X]) rcases em (k = 1) with (rfl | hk) · simp · simp [coeff_X_of_ne_one hk] @[simp] theorem eval₂_X_mul : eval₂ f x (X * p) = eval₂ f x p * x := by rw [X_mul, eval₂_mul_X] theorem eval₂_mul_C' (h : Commute (f a) x) : eval₂ f x (p * C a) = eval₂ f x p * f a := by rw [eval₂_mul_noncomm, eval₂_C] intro k by_cases hk : k = 0 · simp only [hk, h, coeff_C_zero, coeff_C_ne_zero] · simp only [coeff_C_ne_zero hk, RingHom.map_zero, Commute.zero_left] theorem eval₂_list_prod_noncomm (ps : List R[X]) (hf : ∀ p ∈ ps, ∀ (k), Commute (f <| coeff p k) x) : eval₂ f x ps.prod = (ps.map (Polynomial.eval₂ f x)).prod := by induction' ps using List.reverseRecOn with ps p ihp · simp · simp only [List.forall_mem_append, List.forall_mem_singleton] at hf simp [eval₂_mul_noncomm _ _ hf.2, ihp hf.1] /-- `eval₂` as a `RingHom` for noncommutative rings -/ @[simps] def eval₂RingHom' (f : R →+* S) (x : S) (hf : ∀ a, Commute (f a) x) : R[X] →+* S where toFun := eval₂ f x map_add' _ _ := eval₂_add _ _ map_zero' := eval₂_zero _ _ map_mul' _p q := eval₂_mul_noncomm f x fun k => hf <| coeff q k map_one' := eval₂_one _ _ end /-! We next prove that eval₂ is multiplicative as long as target ring is commutative (even if the source ring is not). -/ section Eval₂ section variable [Semiring S] (f : R →+* S) (x : S) theorem eval₂_eq_sum_range : p.eval₂ f x = ∑ i ∈ Finset.range (p.natDegree + 1), f (p.coeff i) * x ^ i := _root_.trans (congr_arg _ p.as_sum_range) (_root_.trans (eval₂_finset_sum f _ _ x) (congr_arg _ (by simp))) theorem eval₂_eq_sum_range' (f : R →+* S) {p : R[X]} {n : ℕ} (hn : p.natDegree < n) (x : S) : eval₂ f x p = ∑ i ∈ Finset.range n, f (p.coeff i) * x ^ i := by rw [eval₂_eq_sum, p.sum_over_range' _ _ hn] intro i rw [f.map_zero, zero_mul] end section variable [CommSemiring S] (f : R →+* S) (x : S) @[simp] theorem eval₂_mul : (p * q).eval₂ f x = p.eval₂ f x * q.eval₂ f x := eval₂_mul_noncomm _ _ fun _k => Commute.all _ _ theorem eval₂_mul_eq_zero_of_left (q : R[X]) (hp : p.eval₂ f x = 0) : (p * q).eval₂ f x = 0 := by rw [eval₂_mul f x] exact mul_eq_zero_of_left hp (q.eval₂ f x) theorem eval₂_mul_eq_zero_of_right (p : R[X]) (hq : q.eval₂ f x = 0) : (p * q).eval₂ f x = 0 := by rw [eval₂_mul f x] exact mul_eq_zero_of_right (p.eval₂ f x) hq /-- `eval₂` as a `RingHom` -/ def eval₂RingHom (f : R →+* S) (x : S) : R[X] →+* S := { eval₂AddMonoidHom f x with map_one' := eval₂_one _ _ map_mul' := fun _ _ => eval₂_mul _ _ } @[simp] theorem coe_eval₂RingHom (f : R →+* S) (x) : ⇑(eval₂RingHom f x) = eval₂ f x := rfl theorem eval₂_pow (n : ℕ) : (p ^ n).eval₂ f x = p.eval₂ f x ^ n := (eval₂RingHom _ _).map_pow _ _ theorem eval₂_dvd : p ∣ q → eval₂ f x p ∣ eval₂ f x q := (eval₂RingHom f x).map_dvd theorem eval₂_eq_zero_of_dvd_of_eval₂_eq_zero (h : p ∣ q) (h0 : eval₂ f x p = 0) : eval₂ f x q = 0 := zero_dvd_iff.mp (h0 ▸ eval₂_dvd f x h) theorem eval₂_list_prod (l : List R[X]) (x : S) : eval₂ f x l.prod = (l.map (eval₂ f x)).prod := map_list_prod (eval₂RingHom f x) l end end Eval₂ section Eval variable {x : R} /-- `eval x p` is the evaluation of the polynomial `p` at `x` -/ def eval : R → R[X] → R := eval₂ (RingHom.id _) theorem eval_eq_sum : p.eval x = p.sum fun e a => a * x ^ e := by rw [eval, eval₂_eq_sum] rfl theorem eval_eq_sum_range {p : R[X]} (x : R) : p.eval x = ∑ i ∈ Finset.range (p.natDegree + 1), p.coeff i * x ^ i := by rw [eval_eq_sum, sum_over_range]; simp theorem eval_eq_sum_range' {p : R[X]} {n : ℕ} (hn : p.natDegree < n) (x : R) : p.eval x = ∑ i ∈ Finset.range n, p.coeff i * x ^ i := by rw [eval_eq_sum, p.sum_over_range' _ _ hn]; simp @[simp] theorem eval₂_at_apply {S : Type*} [Semiring S] (f : R →+* S) (r : R) : p.eval₂ f (f r) = f (p.eval r) := by rw [eval₂_eq_sum, eval_eq_sum, sum, sum, map_sum f] simp only [f.map_mul, f.map_pow] @[simp] theorem eval₂_at_one {S : Type*} [Semiring S] (f : R →+* S) : p.eval₂ f 1 = f (p.eval 1) := by convert eval₂_at_apply (p := p) f 1 simp @[simp] theorem eval₂_at_natCast {S : Type*} [Semiring S] (f : R →+* S) (n : ℕ) : p.eval₂ f n = f (p.eval n) := by convert eval₂_at_apply (p := p) f n simp @[deprecated (since := "2024-04-17")] alias eval₂_at_nat_cast := eval₂_at_natCast -- See note [no_index around OfNat.ofNat] @[simp] theorem eval₂_at_ofNat {S : Type*} [Semiring S] (f : R →+* S) (n : ℕ) [n.AtLeastTwo] : p.eval₂ f (no_index (OfNat.ofNat n)) = f (p.eval (OfNat.ofNat n)) := by simp [OfNat.ofNat] @[simp] theorem eval_C : (C a).eval x = a := eval₂_C _ _ @[simp] theorem eval_natCast {n : ℕ} : (n : R[X]).eval x = n := by simp only [← C_eq_natCast, eval_C] @[deprecated (since := "2024-04-17")] alias eval_nat_cast := eval_natCast -- See note [no_index around OfNat.ofNat] @[simp] lemma eval_ofNat (n : ℕ) [n.AtLeastTwo] (a : R) : (no_index (OfNat.ofNat n : R[X])).eval a = OfNat.ofNat n := by simp only [OfNat.ofNat, eval_natCast] @[simp] theorem eval_X : X.eval x = x := eval₂_X _ _ @[simp] theorem eval_monomial {n a} : (monomial n a).eval x = a * x ^ n := eval₂_monomial _ _ @[simp] theorem eval_zero : (0 : R[X]).eval x = 0 := eval₂_zero _ _ @[simp] theorem eval_add : (p + q).eval x = p.eval x + q.eval x := eval₂_add _ _ @[simp] theorem eval_one : (1 : R[X]).eval x = 1 := eval₂_one _ _ @[simp] theorem eval_smul [Monoid S] [DistribMulAction S R] [IsScalarTower S R R] (s : S) (p : R[X]) (x : R) : (s • p).eval x = s • p.eval x := by rw [← smul_one_smul R s p, eval, eval₂_smul, RingHom.id_apply, smul_one_mul] @[simp] theorem eval_C_mul : (C a * p).eval x = a * p.eval x := by induction p using Polynomial.induction_on' with | h_add p q ph qh => simp only [mul_add, eval_add, ph, qh] | h_monomial n b => simp only [mul_assoc, C_mul_monomial, eval_monomial] /-- A reformulation of the expansion of (1 + y)^d: $$(d + 1) (1 + y)^d - (d + 1)y^d = \sum_{i = 0}^d {d + 1 \choose i} \cdot i \cdot y^{i - 1}.$$ -/ theorem eval_monomial_one_add_sub [CommRing S] (d : ℕ) (y : S) : eval (1 + y) (monomial d (d + 1 : S)) - eval y (monomial d (d + 1 : S)) = ∑ x_1 ∈ range (d + 1), ↑((d + 1).choose x_1) * (↑x_1 * y ^ (x_1 - 1)) := by have cast_succ : (d + 1 : S) = ((d.succ : ℕ) : S) := by simp only [Nat.cast_succ] rw [cast_succ, eval_monomial, eval_monomial, add_comm, add_pow] -- Porting note: `apply_congr` hadn't been ported yet, so `congr` & `ext` is used. conv_lhs => congr · congr · skip · congr · skip · ext rw [one_pow, mul_one, mul_comm] rw [sum_range_succ, mul_add, Nat.choose_self, Nat.cast_one, one_mul, add_sub_cancel_right, mul_sum, sum_range_succ', Nat.cast_zero, zero_mul, mul_zero, add_zero] refine sum_congr rfl fun y _hy => ?_ rw [← mul_assoc, ← mul_assoc, ← Nat.cast_mul, Nat.succ_mul_choose_eq, Nat.cast_mul, Nat.add_sub_cancel] /-- `Polynomial.eval` as linear map -/ @[simps] def leval {R : Type*} [Semiring R] (r : R) : R[X] →ₗ[R] R where toFun f := f.eval r map_add' _f _g := eval_add map_smul' c f := eval_smul c f r @[simp] theorem eval_natCast_mul {n : ℕ} : ((n : R[X]) * p).eval x = n * p.eval x := by rw [← C_eq_natCast, eval_C_mul] @[deprecated (since := "2024-04-17")] alias eval_nat_cast_mul := eval_natCast_mul @[simp] theorem eval_mul_X : (p * X).eval x = p.eval x * x := by induction p using Polynomial.induction_on' with | h_add p q ph qh => simp only [add_mul, eval_add, ph, qh] | h_monomial n a => simp only [← monomial_one_one_eq_X, monomial_mul_monomial, eval_monomial, mul_one, pow_succ, mul_assoc] @[simp] theorem eval_mul_X_pow {k : ℕ} : (p * X ^ k).eval x = p.eval x * x ^ k := by induction' k with k ih · simp · simp [pow_succ, ← mul_assoc, ih] theorem eval_sum (p : R[X]) (f : ℕ → R → R[X]) (x : R) : (p.sum f).eval x = p.sum fun n a => (f n a).eval x := eval₂_sum _ _ _ _ theorem eval_finset_sum (s : Finset ι) (g : ι → R[X]) (x : R) : (∑ i ∈ s, g i).eval x = ∑ i ∈ s, (g i).eval x := eval₂_finset_sum _ _ _ _ /-- `IsRoot p x` implies `x` is a root of `p`. The evaluation of `p` at `x` is zero -/ def IsRoot (p : R[X]) (a : R) : Prop := p.eval a = 0 instance IsRoot.decidable [DecidableEq R] : Decidable (IsRoot p a) := by unfold IsRoot; infer_instance @[simp] theorem IsRoot.def : IsRoot p a ↔ p.eval a = 0 := Iff.rfl theorem IsRoot.eq_zero (h : IsRoot p x) : eval x p = 0 := h theorem coeff_zero_eq_eval_zero (p : R[X]) : coeff p 0 = p.eval 0 := calc coeff p 0 = coeff p 0 * 0 ^ 0 := by simp _ = p.eval 0 := by symm rw [eval_eq_sum] exact Finset.sum_eq_single _ (fun b _ hb => by simp [zero_pow hb]) (by simp) theorem zero_isRoot_of_coeff_zero_eq_zero {p : R[X]} (hp : p.coeff 0 = 0) : IsRoot p 0 := by rwa [coeff_zero_eq_eval_zero] at hp theorem IsRoot.dvd {R : Type*} [CommSemiring R] {p q : R[X]} {x : R} (h : p.IsRoot x) (hpq : p ∣ q) : q.IsRoot x := by rwa [IsRoot, eval, eval₂_eq_zero_of_dvd_of_eval₂_eq_zero _ _ hpq] theorem not_isRoot_C (r a : R) (hr : r ≠ 0) : ¬IsRoot (C r) a := by simpa using hr theorem eval_surjective (x : R) : Function.Surjective <| eval x := fun y => ⟨C y, eval_C⟩ end Eval section Comp /-- The composition of polynomials as a polynomial. -/ def comp (p q : R[X]) : R[X] := p.eval₂ C q theorem comp_eq_sum_left : p.comp q = p.sum fun e a => C a * q ^ e := by rw [comp, eval₂_eq_sum] @[simp] theorem comp_X : p.comp X = p := by simp only [comp, eval₂_def, C_mul_X_pow_eq_monomial] exact sum_monomial_eq _ @[simp] theorem X_comp : X.comp p = p := eval₂_X _ _ @[simp] theorem comp_C : p.comp (C a) = C (p.eval a) := by simp [comp, map_sum (C : R →+* _)] @[simp] theorem C_comp : (C a).comp p = C a := eval₂_C _ _ @[simp] theorem natCast_comp {n : ℕ} : (n : R[X]).comp p = n := by rw [← C_eq_natCast, C_comp] @[deprecated (since := "2024-04-17")] alias nat_cast_comp := natCast_comp @[simp] theorem ofNat_comp (n : ℕ) [n.AtLeastTwo] : (no_index (OfNat.ofNat n) : R[X]).comp p = n := natCast_comp @[simp] theorem comp_zero : p.comp (0 : R[X]) = C (p.eval 0) := by rw [← C_0, comp_C] @[simp] theorem zero_comp : comp (0 : R[X]) p = 0 := by rw [← C_0, C_comp] @[simp] theorem comp_one : p.comp 1 = C (p.eval 1) := by rw [← C_1, comp_C] @[simp] theorem one_comp : comp (1 : R[X]) p = 1 := by rw [← C_1, C_comp] @[simp] theorem add_comp : (p + q).comp r = p.comp r + q.comp r := eval₂_add _ _ @[simp] theorem monomial_comp (n : ℕ) : (monomial n a).comp p = C a * p ^ n := eval₂_monomial _ _ @[simp] theorem mul_X_comp : (p * X).comp r = p.comp r * r := by induction p using Polynomial.induction_on' with | h_add p q hp hq => simp only [hp, hq, add_mul, add_comp] | h_monomial n b => simp only [pow_succ, mul_assoc, monomial_mul_X, monomial_comp] @[simp] theorem X_pow_comp {k : ℕ} : (X ^ k).comp p = p ^ k := by induction' k with k ih · simp · simp [pow_succ, mul_X_comp, ih] @[simp] theorem mul_X_pow_comp {k : ℕ} : (p * X ^ k).comp r = p.comp r * r ^ k := by induction' k with k ih · simp · simp [ih, pow_succ, ← mul_assoc, mul_X_comp] @[simp] theorem C_mul_comp : (C a * p).comp r = C a * p.comp r := by induction p using Polynomial.induction_on' with | h_add p q hp hq => simp [hp, hq, mul_add] | h_monomial n b => simp [mul_assoc] @[simp] theorem natCast_mul_comp {n : ℕ} : ((n : R[X]) * p).comp r = n * p.comp r := by rw [← C_eq_natCast, C_mul_comp] @[deprecated (since := "2024-04-17")] alias nat_cast_mul_comp := natCast_mul_comp theorem mul_X_add_natCast_comp {n : ℕ} : (p * (X + (n : R[X]))).comp q = p.comp q * (q + n) := by rw [mul_add, add_comp, mul_X_comp, ← Nat.cast_comm, natCast_mul_comp, Nat.cast_comm, mul_add] @[deprecated (since := "2024-04-17")] alias mul_X_add_nat_cast_comp := mul_X_add_natCast_comp @[simp] theorem mul_comp {R : Type*} [CommSemiring R] (p q r : R[X]) : (p * q).comp r = p.comp r * q.comp r := eval₂_mul _ _ @[simp] theorem pow_comp {R : Type*} [CommSemiring R] (p q : R[X]) (n : ℕ) : (p ^ n).comp q = p.comp q ^ n := (MonoidHom.mk (OneHom.mk (fun r : R[X] => r.comp q) one_comp) fun r s => mul_comp r s q).map_pow p n @[simp] theorem smul_comp [Monoid S] [DistribMulAction S R] [IsScalarTower S R R] (s : S) (p q : R[X]) : (s • p).comp q = s • p.comp q := by rw [← smul_one_smul R s p, comp, comp, eval₂_smul, ← smul_eq_C_mul, smul_assoc, one_smul] theorem comp_assoc {R : Type*} [CommSemiring R] (φ ψ χ : R[X]) : (φ.comp ψ).comp χ = φ.comp (ψ.comp χ) := by refine Polynomial.induction_on φ ?_ ?_ ?_ <;> · intros simp_all only [add_comp, mul_comp, C_comp, X_comp, pow_succ, ← mul_assoc] theorem coeff_comp_degree_mul_degree (hqd0 : natDegree q ≠ 0) : coeff (p.comp q) (natDegree p * natDegree q) = leadingCoeff p * leadingCoeff q ^ natDegree p := by rw [comp, eval₂_def, coeff_sum] -- Porting note: `convert` → `refine` refine Eq.trans (Finset.sum_eq_single p.natDegree ?h₀ ?h₁) ?h₂ case h₂ => simp only [coeff_natDegree, coeff_C_mul, coeff_pow_mul_natDegree] case h₀ => intro b hbs hbp refine coeff_eq_zero_of_natDegree_lt (natDegree_mul_le.trans_lt ?_) rw [natDegree_C, zero_add] refine natDegree_pow_le.trans_lt ((mul_lt_mul_right (pos_iff_ne_zero.mpr hqd0)).mpr ?_) exact lt_of_le_of_ne (le_natDegree_of_mem_supp _ hbs) hbp case h₁ => simp (config := { contextual := true }) @[simp] lemma sum_comp (s : Finset ι) (p : ι → R[X]) (q : R[X]) : (∑ i ∈ s, p i).comp q = ∑ i ∈ s, (p i).comp q := Polynomial.eval₂_finset_sum _ _ _ _ end Comp section Map variable [Semiring S] variable (f : R →+* S) /-- `map f p` maps a polynomial `p` across a ring hom `f` -/ def map : R[X] → S[X] := eval₂ (C.comp f) X @[simp] theorem map_C : (C a).map f = C (f a) := eval₂_C _ _ @[simp] theorem map_X : X.map f = X := eval₂_X _ _ @[simp] theorem map_monomial {n a} : (monomial n a).map f = monomial n (f a) := by dsimp only [map] rw [eval₂_monomial, ← C_mul_X_pow_eq_monomial]; rfl @[simp] protected theorem map_zero : (0 : R[X]).map f = 0 := eval₂_zero _ _ @[simp] protected theorem map_add : (p + q).map f = p.map f + q.map f := eval₂_add _ _ @[simp] protected theorem map_one : (1 : R[X]).map f = 1 := eval₂_one _ _ @[simp] protected theorem map_mul : (p * q).map f = p.map f * q.map f := by rw [map, eval₂_mul_noncomm] exact fun k => (commute_X _).symm @[simp] protected theorem map_smul (r : R) : (r • p).map f = f r • p.map f := by rw [map, eval₂_smul, RingHom.comp_apply, C_mul'] -- `map` is a ring-hom unconditionally, and theoretically the definition could be replaced, -- but this turns out not to be easy because `p.map f` does not resolve to `Polynomial.map` -- if `map` is a `RingHom` instead of a plain function; the elaborator does not try to coerce -- to a function before trying field (dot) notation (this may be technically infeasible); -- the relevant code is (both lines): https://github.com/leanprover-community/ -- lean/blob/487ac5d7e9b34800502e1ddf3c7c806c01cf9d51/src/frontends/lean/elaborator.cpp#L1876-L1913 /-- `Polynomial.map` as a `RingHom`. -/ def mapRingHom (f : R →+* S) : R[X] →+* S[X] where toFun := Polynomial.map f map_add' _ _ := Polynomial.map_add f map_zero' := Polynomial.map_zero f map_mul' _ _ := Polynomial.map_mul f map_one' := Polynomial.map_one f @[simp] theorem coe_mapRingHom (f : R →+* S) : ⇑(mapRingHom f) = map f := rfl -- This is protected to not clash with the global `map_natCast`. @[simp] protected theorem map_natCast (n : ℕ) : (n : R[X]).map f = n := map_natCast (mapRingHom f) n @[deprecated (since := "2024-04-17")] alias map_nat_cast := map_natCast -- See note [no_index around OfNat.ofNat] @[simp] protected theorem map_ofNat (n : ℕ) [n.AtLeastTwo] : (no_index (OfNat.ofNat n) : R[X]).map f = OfNat.ofNat n := show (n : R[X]).map f = n by rw [Polynomial.map_natCast] --TODO rename to `map_dvd_map` theorem map_dvd (f : R →+* S) {x y : R[X]} : x ∣ y → x.map f ∣ y.map f := (mapRingHom f).map_dvd @[simp] theorem coeff_map (n : ℕ) : coeff (p.map f) n = f (coeff p n) := by rw [map, eval₂_def, coeff_sum, sum] conv_rhs => rw [← sum_C_mul_X_pow_eq p, coeff_sum, sum, map_sum] refine Finset.sum_congr rfl fun x _hx => ?_ simp only [RingHom.coe_comp, Function.comp, coeff_C_mul_X_pow] split_ifs <;> simp [f.map_zero] /-- If `R` and `S` are isomorphic, then so are their polynomial rings. -/ @[simps!] def mapEquiv (e : R ≃+* S) : R[X] ≃+* S[X] := RingEquiv.ofHomInv (mapRingHom (e : R →+* S)) (mapRingHom (e.symm : S →+* R)) (by ext <;> simp) (by ext <;> simp) theorem map_map [Semiring T] (g : S →+* T) (p : R[X]) : (p.map f).map g = p.map (g.comp f) := ext (by simp [coeff_map]) @[simp] theorem map_id : p.map (RingHom.id _) = p := by simp [Polynomial.ext_iff, coeff_map] /-- The polynomial ring over a finite product of rings is isomorphic to the product of polynomial rings over individual rings. -/ def piEquiv {ι} [Finite ι] (R : ι → Type*) [∀ i, Semiring (R i)] : (∀ i, R i)[X] ≃+* ∀ i, (R i)[X] := .ofBijective (Pi.ringHom fun i ↦ mapRingHom (Pi.evalRingHom R i)) ⟨fun p q h ↦ by ext n i; simpa using congr_arg (fun p ↦ coeff (p i) n) h, fun p ↦ ⟨.ofFinsupp (.ofSupportFinite (fun n i ↦ coeff (p i) n) <| (Set.finite_iUnion fun i ↦ (p i).support.finite_toSet).subset fun n hn ↦ by simp only [Set.mem_iUnion, Finset.mem_coe, mem_support_iff, Function.mem_support] at hn ⊢ contrapose! hn; exact funext hn), by ext i n; exact coeff_map _ _⟩⟩ theorem eval₂_eq_eval_map {x : S} : p.eval₂ f x = (p.map f).eval x := by induction p using Polynomial.induction_on' with | h_add p q hp hq => simp [hp, hq] | h_monomial n r => simp theorem map_injective (hf : Function.Injective f) : Function.Injective (map f) := fun p q h => ext fun m => hf <| by rw [← coeff_map f, ← coeff_map f, h] theorem map_surjective (hf : Function.Surjective f) : Function.Surjective (map f) := fun p => Polynomial.induction_on' p (fun p q hp hq => let ⟨p', hp'⟩ := hp let ⟨q', hq'⟩ := hq ⟨p' + q', by rw [Polynomial.map_add f, hp', hq']⟩) fun n s => let ⟨r, hr⟩ := hf s ⟨monomial n r, by rw [map_monomial f, hr]⟩ theorem degree_map_le (p : R[X]) : degree (p.map f) ≤ degree p := by refine (degree_le_iff_coeff_zero _ _).2 fun m hm => ?_ rw [degree_lt_iff_coeff_zero] at hm simp [hm m le_rfl] theorem natDegree_map_le (p : R[X]) : natDegree (p.map f) ≤ natDegree p := natDegree_le_natDegree (degree_map_le f p) variable {f} protected theorem map_eq_zero_iff (hf : Function.Injective f) : p.map f = 0 ↔ p = 0 := map_eq_zero_iff (mapRingHom f) (map_injective f hf) protected theorem map_ne_zero_iff (hf : Function.Injective f) : p.map f ≠ 0 ↔ p ≠ 0 := (Polynomial.map_eq_zero_iff hf).not theorem map_monic_eq_zero_iff (hp : p.Monic) : p.map f = 0 ↔ ∀ x, f x = 0 := ⟨fun hfp x => calc f x = f x * f p.leadingCoeff := by simp only [mul_one, hp.leadingCoeff, f.map_one] _ = f x * (p.map f).coeff p.natDegree := congr_arg _ (coeff_map _ _).symm _ = 0 := by simp only [hfp, mul_zero, coeff_zero] , fun h => ext fun n => by simp only [h, coeff_map, coeff_zero]⟩ theorem map_monic_ne_zero (hp : p.Monic) [Nontrivial S] : p.map f ≠ 0 := fun h => f.map_one_ne_zero ((map_monic_eq_zero_iff hp).mp h _) theorem degree_map_eq_of_leadingCoeff_ne_zero (f : R →+* S) (hf : f (leadingCoeff p) ≠ 0) : degree (p.map f) = degree p := le_antisymm (degree_map_le f _) <| by have hp0 : p ≠ 0 := leadingCoeff_ne_zero.mp fun hp0 => hf (_root_.trans (congr_arg _ hp0) f.map_zero) rw [degree_eq_natDegree hp0] refine le_degree_of_ne_zero ?_ rw [coeff_map] exact hf theorem natDegree_map_of_leadingCoeff_ne_zero (f : R →+* S) (hf : f (leadingCoeff p) ≠ 0) : natDegree (p.map f) = natDegree p := natDegree_eq_of_degree_eq (degree_map_eq_of_leadingCoeff_ne_zero f hf) theorem leadingCoeff_map_of_leadingCoeff_ne_zero (f : R →+* S) (hf : f (leadingCoeff p) ≠ 0) : leadingCoeff (p.map f) = f (leadingCoeff p) := by unfold leadingCoeff rw [coeff_map, natDegree_map_of_leadingCoeff_ne_zero f hf] variable (f) @[simp] theorem mapRingHom_id : mapRingHom (RingHom.id R) = RingHom.id R[X] := RingHom.ext fun _x => map_id @[simp] theorem mapRingHom_comp [Semiring T] (f : S →+* T) (g : R →+* S) : (mapRingHom f).comp (mapRingHom g) = mapRingHom (f.comp g) := RingHom.ext <| Polynomial.map_map g f protected theorem map_list_prod (L : List R[X]) : L.prod.map f = (L.map <| map f).prod := Eq.symm <| List.prod_hom _ (mapRingHom f).toMonoidHom @[simp] protected theorem map_pow (n : ℕ) : (p ^ n).map f = p.map f ^ n := (mapRingHom f).map_pow _ _ theorem mem_map_rangeS {p : S[X]} : p ∈ (mapRingHom f).rangeS ↔ ∀ n, p.coeff n ∈ f.rangeS := by constructor · rintro ⟨p, rfl⟩ n rw [coe_mapRingHom, coeff_map] exact Set.mem_range_self _ · intro h rw [p.as_sum_range_C_mul_X_pow] refine (mapRingHom f).rangeS.sum_mem ?_ intro i _hi rcases h i with ⟨c, hc⟩ use C c * X ^ i rw [coe_mapRingHom, Polynomial.map_mul, map_C, hc, Polynomial.map_pow, map_X] theorem mem_map_range {R S : Type*} [Ring R] [Ring S] (f : R →+* S) {p : S[X]} : p ∈ (mapRingHom f).range ↔ ∀ n, p.coeff n ∈ f.range := mem_map_rangeS f theorem eval₂_map [Semiring T] (g : S →+* T) (x : T) : (p.map f).eval₂ g x = p.eval₂ (g.comp f) x := by rw [eval₂_eq_eval_map, eval₂_eq_eval_map, map_map] theorem eval_map (x : S) : (p.map f).eval x = p.eval₂ f x := (eval₂_eq_eval_map f).symm protected theorem map_sum {ι : Type*} (g : ι → R[X]) (s : Finset ι) : (∑ i ∈ s, g i).map f = ∑ i ∈ s, (g i).map f := map_sum (mapRingHom f) _ _ theorem map_comp (p q : R[X]) : map f (p.comp q) = (map f p).comp (map f q) := Polynomial.induction_on p (by simp only [map_C, forall_const, C_comp, eq_self_iff_true]) (by simp (config := { contextual := true }) only [Polynomial.map_add, add_comp, forall_const, imp_true_iff, eq_self_iff_true]) (by simp (config := { contextual := true }) only [pow_succ, ← mul_assoc, comp, forall_const, eval₂_mul_X, imp_true_iff, eq_self_iff_true, map_X, Polynomial.map_mul]) @[simp] theorem eval_zero_map (f : R →+* S) (p : R[X]) : (p.map f).eval 0 = f (p.eval 0) := by simp [← coeff_zero_eq_eval_zero] @[simp] theorem eval_one_map (f : R →+* S) (p : R[X]) : (p.map f).eval 1 = f (p.eval 1) := by induction p using Polynomial.induction_on' with | h_add p q hp hq => simp only [hp, hq, Polynomial.map_add, RingHom.map_add, eval_add] | h_monomial n r => simp only [one_pow, mul_one, eval_monomial, map_monomial] @[simp] theorem eval_natCast_map (f : R →+* S) (p : R[X]) (n : ℕ) : (p.map f).eval (n : S) = f (p.eval n) := by induction p using Polynomial.induction_on' with | h_add p q hp hq => simp only [hp, hq, Polynomial.map_add, RingHom.map_add, eval_add] | h_monomial n r => simp only [map_natCast f, eval_monomial, map_monomial, f.map_pow, f.map_mul] @[deprecated (since := "2024-04-17")] alias eval_nat_cast_map := eval_natCast_map @[simp] theorem eval_intCast_map {R S : Type*} [Ring R] [Ring S] (f : R →+* S) (p : R[X]) (i : ℤ) : (p.map f).eval (i : S) = f (p.eval i) := by induction p using Polynomial.induction_on' with | h_add p q hp hq => simp only [hp, hq, Polynomial.map_add, RingHom.map_add, eval_add] | h_monomial n r => simp only [map_intCast, eval_monomial, map_monomial, map_pow, map_mul] @[deprecated (since := "2024-04-17")] alias eval_int_cast_map := eval_intCast_map end Map /-! we have made `eval₂` irreducible from the start. Perhaps we can make also `eval`, `comp`, and `map` irreducible too? -/ section HomEval₂ variable [Semiring S] [Semiring T] (f : R →+* S) (g : S →+* T) (p) theorem hom_eval₂ (x : S) : g (p.eval₂ f x) = p.eval₂ (g.comp f) (g x) := by rw [← eval₂_map, eval₂_at_apply, eval_map] end HomEval₂ end Semiring section CommSemiring section Eval section variable [Semiring R] {p q : R[X]} {x : R} [Semiring S] (f : R →+* S) theorem eval₂_hom (x : R) : p.eval₂ f (f x) = f (p.eval x) := RingHom.comp_id f ▸ (hom_eval₂ p (RingHom.id R) f x).symm end section variable [Semiring R] {p q : R[X]} {x : R} [CommSemiring S] (f : R →+* S) theorem eval₂_comp {x : S} : eval₂ f x (p.comp q) = eval₂ f (eval₂ f x q) p := by rw [comp, p.as_sum_range]; simp [eval₂_finset_sum, eval₂_pow] @[simp] theorem iterate_comp_eval₂ (k : ℕ) (t : S) : eval₂ f t (p.comp^[k] q) = (fun x => eval₂ f x p)^[k] (eval₂ f t q) := by induction' k with k IH · simp · rw [Function.iterate_succ_apply', Function.iterate_succ_apply', eval₂_comp, IH] end section Algebra variable [CommSemiring R] [Semiring S] [Algebra R S] (x : S) (p q : R[X]) @[simp] theorem eval₂_mul' : (p * q).eval₂ (algebraMap R S) x = p.eval₂ (algebraMap R S) x * q.eval₂ (algebraMap R S) x := by exact eval₂_mul_noncomm _ _ fun k => Algebra.commute_algebraMap_left (coeff q k) x @[simp] theorem eval₂_pow' (n : ℕ) : (p ^ n).eval₂ (algebraMap R S) x = (p.eval₂ (algebraMap R S) x) ^ n := by induction n with | zero => simp only [Nat.zero_eq, pow_zero, eval₂_one] | succ n ih => rw [pow_succ, pow_succ, eval₂_mul', ih] @[simp] theorem eval₂_comp' : eval₂ (algebraMap R S) x (p.comp q) = eval₂ (algebraMap R S) (eval₂ (algebraMap R S) x q) p := by induction p using Polynomial.induction_on' with | h_add r s hr hs => simp only [add_comp, eval₂_add, hr, hs] | h_monomial n a => simp only [monomial_comp, eval₂_mul', eval₂_C, eval₂_monomial, eval₂_pow'] end Algebra section variable [CommSemiring R] {p q : R[X]} {x : R} [CommSemiring S] (f : R →+* S) @[simp] theorem eval_mul : (p * q).eval x = p.eval x * q.eval x := eval₂_mul _ _ /-- `eval r`, regarded as a ring homomorphism from `R[X]` to `R`. -/ def evalRingHom : R → R[X] →+* R := eval₂RingHom (RingHom.id _) @[simp] theorem coe_evalRingHom (r : R) : (evalRingHom r : R[X] → R) = eval r := rfl theorem evalRingHom_zero : evalRingHom 0 = constantCoeff := DFunLike.ext _ _ fun p => p.coeff_zero_eq_eval_zero.symm @[simp] theorem eval_pow (n : ℕ) : (p ^ n).eval x = p.eval x ^ n := eval₂_pow _ _ _ @[simp] theorem eval_comp : (p.comp q).eval x = p.eval (q.eval x) := by induction p using Polynomial.induction_on' with | h_add r s hr hs => simp [add_comp, hr, hs] | h_monomial n a => simp @[simp] theorem iterate_comp_eval : ∀ (k : ℕ) (t : R), (p.comp^[k] q).eval t = (fun x => p.eval x)^[k] (q.eval t) := iterate_comp_eval₂ _ lemma isRoot_comp {R} [CommSemiring R] {p q : R[X]} {r : R} : (p.comp q).IsRoot r ↔ p.IsRoot (q.eval r) := by simp_rw [IsRoot, eval_comp] /-- `comp p`, regarded as a ring homomorphism from `R[X]` to itself. -/ def compRingHom : R[X] → R[X] →+* R[X] := eval₂RingHom C @[simp] theorem coe_compRingHom (q : R[X]) : (compRingHom q : R[X] → R[X]) = fun p => comp p q := rfl theorem coe_compRingHom_apply (p q : R[X]) : (compRingHom q : R[X] → R[X]) p = comp p q := rfl theorem root_mul_left_of_isRoot (p : R[X]) {q : R[X]} : IsRoot q a → IsRoot (p * q) a := fun H => by rw [IsRoot, eval_mul, IsRoot.def.1 H, mul_zero] theorem root_mul_right_of_isRoot {p : R[X]} (q : R[X]) : IsRoot p a → IsRoot (p * q) a := fun H => by rw [IsRoot, eval_mul, IsRoot.def.1 H, zero_mul] theorem eval₂_multiset_prod (s : Multiset R[X]) (x : S) : eval₂ f x s.prod = (s.map (eval₂ f x)).prod := map_multiset_prod (eval₂RingHom f x) s theorem eval₂_finset_prod (s : Finset ι) (g : ι → R[X]) (x : S) : (∏ i ∈ s, g i).eval₂ f x = ∏ i ∈ s, (g i).eval₂ f x := map_prod (eval₂RingHom f x) _ _ /-- Polynomial evaluation commutes with `List.prod` -/ theorem eval_list_prod (l : List R[X]) (x : R) : eval x l.prod = (l.map (eval x)).prod := map_list_prod (evalRingHom x) l /-- Polynomial evaluation commutes with `Multiset.prod` -/ theorem eval_multiset_prod (s : Multiset R[X]) (x : R) : eval x s.prod = (s.map (eval x)).prod := (evalRingHom x).map_multiset_prod s /-- Polynomial evaluation commutes with `Finset.prod` -/ theorem eval_prod {ι : Type*} (s : Finset ι) (p : ι → R[X]) (x : R) : eval x (∏ j ∈ s, p j) = ∏ j ∈ s, eval x (p j) := map_prod (evalRingHom x) _ _ theorem list_prod_comp (l : List R[X]) (q : R[X]) : l.prod.comp q = (l.map fun p : R[X] => p.comp q).prod := map_list_prod (compRingHom q) _ theorem multiset_prod_comp (s : Multiset R[X]) (q : R[X]) : s.prod.comp q = (s.map fun p : R[X] => p.comp q).prod := map_multiset_prod (compRingHom q) _ theorem prod_comp {ι : Type*} (s : Finset ι) (p : ι → R[X]) (q : R[X]) : (∏ j ∈ s, p j).comp q = ∏ j ∈ s, (p j).comp q := map_prod (compRingHom q) _ _ theorem isRoot_prod {R} [CommRing R] [IsDomain R] {ι : Type*} (s : Finset ι) (p : ι → R[X]) (x : R) : IsRoot (∏ j ∈ s, p j) x ↔ ∃ i ∈ s, IsRoot (p i) x := by simp only [IsRoot, eval_prod, Finset.prod_eq_zero_iff] theorem eval_dvd : p ∣ q → eval x p ∣ eval x q := eval₂_dvd _ _ theorem eval_eq_zero_of_dvd_of_eval_eq_zero : p ∣ q → eval x p = 0 → eval x q = 0 := eval₂_eq_zero_of_dvd_of_eval₂_eq_zero _ _ @[simp] theorem eval_geom_sum {R} [CommSemiring R] {n : ℕ} {x : R} : eval x (∑ i ∈ range n, X ^ i) = ∑ i ∈ range n, x ^ i := by simp [eval_finset_sum] end end Eval section Map theorem support_map_subset [Semiring R] [Semiring S] (f : R →+* S) (p : R[X]) : (map f p).support ⊆ p.support := by intro x contrapose! simp (config := { contextual := true }) theorem support_map_of_injective [Semiring R] [Semiring S] (p : R[X]) {f : R →+* S} (hf : Function.Injective f) : (map f p).support = p.support := by simp_rw [Finset.ext_iff, mem_support_iff, coeff_map, ← map_zero f, hf.ne_iff, forall_const] variable [CommSemiring R] [CommSemiring S] (f : R →+* S) protected theorem map_multiset_prod (m : Multiset R[X]) : m.prod.map f = (m.map <| map f).prod := Eq.symm <| Multiset.prod_hom _ (mapRingHom f).toMonoidHom protected theorem map_prod {ι : Type*} (g : ι → R[X]) (s : Finset ι) : (∏ i ∈ s, g i).map f = ∏ i ∈ s, (g i).map f := map_prod (mapRingHom f) _ _ theorem IsRoot.map {f : R →+* S} {x : R} {p : R[X]} (h : IsRoot p x) : IsRoot (p.map f) (f x) := by rw [IsRoot, eval_map, eval₂_hom, h.eq_zero, f.map_zero] theorem IsRoot.of_map {R} [CommRing R] {f : R →+* S} {x : R} {p : R[X]} (h : IsRoot (p.map f) (f x)) (hf : Function.Injective f) : IsRoot p x := by rwa [IsRoot, ← (injective_iff_map_eq_zero' f).mp hf, ← eval₂_hom, ← eval_map] theorem isRoot_map_iff {R : Type*} [CommRing R] {f : R →+* S} {x : R} {p : R[X]} (hf : Function.Injective f) : IsRoot (p.map f) (f x) ↔ IsRoot p x := ⟨fun h => h.of_map hf, fun h => h.map⟩ end Map end CommSemiring section Ring variable [Ring R] {p q r : R[X]} @[simp] protected theorem map_sub {S} [Ring S] (f : R →+* S) : (p - q).map f = p.map f - q.map f := (mapRingHom f).map_sub p q @[simp] protected theorem map_neg {S} [Ring S] (f : R →+* S) : (-p).map f = -p.map f := (mapRingHom f).map_neg p @[simp] protected lemma map_intCast {S} [Ring S] (f : R →+* S) (n : ℤ) : map f ↑n = ↑n := map_intCast (mapRingHom f) n @[deprecated (since := "2024-04-17")] alias map_int_cast := map_intCast @[simp] theorem eval_intCast {n : ℤ} {x : R} : (n : R[X]).eval x = n := by simp only [← C_eq_intCast, eval_C] @[deprecated (since := "2024-04-17")] alias eval_int_cast := eval_intCast @[simp] theorem eval₂_neg {S} [Ring S] (f : R →+* S) {x : S} : (-p).eval₂ f x = -p.eval₂ f x := by rw [eq_neg_iff_add_eq_zero, ← eval₂_add, add_left_neg, eval₂_zero] @[simp] theorem eval₂_sub {S} [Ring S] (f : R →+* S) {x : S} : (p - q).eval₂ f x = p.eval₂ f x - q.eval₂ f x := by rw [sub_eq_add_neg, eval₂_add, eval₂_neg, sub_eq_add_neg] @[simp] theorem eval_neg (p : R[X]) (x : R) : (-p).eval x = -p.eval x := eval₂_neg _ @[simp] theorem eval_sub (p q : R[X]) (x : R) : (p - q).eval x = p.eval x - q.eval x := eval₂_sub _ theorem root_X_sub_C : IsRoot (X - C a) b ↔ a = b := by rw [IsRoot.def, eval_sub, eval_X, eval_C, sub_eq_zero, eq_comm] @[simp] theorem neg_comp : (-p).comp q = -p.comp q := eval₂_neg _ @[simp] theorem sub_comp : (p - q).comp r = p.comp r - q.comp r := eval₂_sub _ @[simp] theorem intCast_comp (i : ℤ) : comp (i : R[X]) p = i := by cases i <;> simp @[deprecated (since := "2024-05-27")] alias cast_int_comp := intCast_comp @[simp] theorem eval₂_at_intCast {S : Type*} [Ring S] (f : R →+* S) (n : ℤ) : p.eval₂ f n = f (p.eval n) := by convert eval₂_at_apply (p := p) f n simp @[deprecated (since := "2024-04-17")] alias eval₂_at_int_cast := eval₂_at_intCast theorem mul_X_sub_intCast_comp {n : ℕ} : (p * (X - (n : R[X]))).comp q = p.comp q * (q - n) := by rw [mul_sub, sub_comp, mul_X_comp, ← Nat.cast_comm, natCast_mul_comp, Nat.cast_comm, mul_sub] @[deprecated (since := "2024-04-17")] alias mul_X_sub_int_cast_comp := mul_X_sub_intCast_comp end Ring end Polynomial
Algebra\Polynomial\Expand.lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import Mathlib.RingTheory.Polynomial.Basic import Mathlib.RingTheory.LocalRing.RingHom.Basic /-! # Expand a polynomial by a factor of p, so `∑ aₙ xⁿ` becomes `∑ aₙ xⁿᵖ`. ## Main definitions * `Polynomial.expand R p f`: expand the polynomial `f` with coefficients in a commutative semiring `R` by a factor of p, so `expand R p (∑ aₙ xⁿ)` is `∑ aₙ xⁿᵖ`. * `Polynomial.contract p f`: the opposite of `expand`, so it sends `∑ aₙ xⁿᵖ` to `∑ aₙ xⁿ`. -/ universe u v w open Polynomial open Finset namespace Polynomial section CommSemiring variable (R : Type u) [CommSemiring R] {S : Type v} [CommSemiring S] (p q : ℕ) /-- Expand the polynomial by a factor of p, so `∑ aₙ xⁿ` becomes `∑ aₙ xⁿᵖ`. -/ noncomputable def expand : R[X] →ₐ[R] R[X] := { (eval₂RingHom C (X ^ p) : R[X] →+* R[X]) with commutes' := fun _ => eval₂_C _ _ } theorem coe_expand : (expand R p : R[X] → R[X]) = eval₂ C (X ^ p) := rfl variable {R} theorem expand_eq_comp_X_pow {f : R[X]} : expand R p f = f.comp (X ^ p) := rfl theorem expand_eq_sum {f : R[X]} : expand R p f = f.sum fun e a => C a * (X ^ p) ^ e := by simp [expand, eval₂] @[simp] theorem expand_C (r : R) : expand R p (C r) = C r := eval₂_C _ _ @[simp] theorem expand_X : expand R p X = X ^ p := eval₂_X _ _ @[simp] theorem expand_monomial (r : R) : expand R p (monomial q r) = monomial (q * p) r := by simp_rw [← smul_X_eq_monomial, map_smul, map_pow, expand_X, mul_comm, pow_mul] theorem expand_expand (f : R[X]) : expand R p (expand R q f) = expand R (p * q) f := Polynomial.induction_on f (fun r => by simp_rw [expand_C]) (fun f g ihf ihg => by simp_rw [map_add, ihf, ihg]) fun n r _ => by simp_rw [map_mul, expand_C, map_pow, expand_X, map_pow, expand_X, pow_mul] theorem expand_mul (f : R[X]) : expand R (p * q) f = expand R p (expand R q f) := (expand_expand p q f).symm @[simp] theorem expand_zero (f : R[X]) : expand R 0 f = C (eval 1 f) := by simp [expand] @[simp] theorem expand_one (f : R[X]) : expand R 1 f = f := Polynomial.induction_on f (fun r => by rw [expand_C]) (fun f g ihf ihg => by rw [map_add, ihf, ihg]) fun n r _ => by rw [map_mul, expand_C, map_pow, expand_X, pow_one] theorem expand_pow (f : R[X]) : expand R (p ^ q) f = (expand R p)^[q] f := Nat.recOn q (by rw [pow_zero, expand_one, Function.iterate_zero, id]) fun n ih => by rw [Function.iterate_succ_apply', pow_succ', expand_mul, ih] theorem derivative_expand (f : R[X]) : Polynomial.derivative (expand R p f) = expand R p (Polynomial.derivative f) * (p * (X ^ (p - 1) : R[X])) := by rw [coe_expand, derivative_eval₂_C, derivative_pow, C_eq_natCast, derivative_X, mul_one] theorem coeff_expand {p : ℕ} (hp : 0 < p) (f : R[X]) (n : ℕ) : (expand R p f).coeff n = if p ∣ n then f.coeff (n / p) else 0 := by simp only [expand_eq_sum] simp_rw [coeff_sum, ← pow_mul, C_mul_X_pow_eq_monomial, coeff_monomial, sum] split_ifs with h · rw [Finset.sum_eq_single (n / p), Nat.mul_div_cancel' h, if_pos rfl] · intro b _ hb2 rw [if_neg] intro hb3 apply hb2 rw [← hb3, Nat.mul_div_cancel_left b hp] · intro hn rw [not_mem_support_iff.1 hn] split_ifs <;> rfl · rw [Finset.sum_eq_zero] intro k _ rw [if_neg] exact fun hkn => h ⟨k, hkn.symm⟩ @[simp] theorem coeff_expand_mul {p : ℕ} (hp : 0 < p) (f : R[X]) (n : ℕ) : (expand R p f).coeff (n * p) = f.coeff n := by rw [coeff_expand hp, if_pos (dvd_mul_left _ _), Nat.mul_div_cancel _ hp] @[simp] theorem coeff_expand_mul' {p : ℕ} (hp : 0 < p) (f : R[X]) (n : ℕ) : (expand R p f).coeff (p * n) = f.coeff n := by rw [mul_comm, coeff_expand_mul hp] /-- Expansion is injective. -/ theorem expand_injective {n : ℕ} (hn : 0 < n) : Function.Injective (expand R n) := fun g g' H => ext fun k => by rw [← coeff_expand_mul hn, H, coeff_expand_mul hn] theorem expand_inj {p : ℕ} (hp : 0 < p) {f g : R[X]} : expand R p f = expand R p g ↔ f = g := (expand_injective hp).eq_iff theorem expand_eq_zero {p : ℕ} (hp : 0 < p) {f : R[X]} : expand R p f = 0 ↔ f = 0 := (expand_injective hp).eq_iff' (map_zero _) theorem expand_ne_zero {p : ℕ} (hp : 0 < p) {f : R[X]} : expand R p f ≠ 0 ↔ f ≠ 0 := (expand_eq_zero hp).not theorem expand_eq_C {p : ℕ} (hp : 0 < p) {f : R[X]} {r : R} : expand R p f = C r ↔ f = C r := by rw [← expand_C, expand_inj hp, expand_C] theorem natDegree_expand (p : ℕ) (f : R[X]) : (expand R p f).natDegree = f.natDegree * p := by rcases p.eq_zero_or_pos with hp | hp · rw [hp, coe_expand, pow_zero, mul_zero, ← C_1, eval₂_hom, natDegree_C] by_cases hf : f = 0 · rw [hf, map_zero, natDegree_zero, zero_mul] have hf1 : expand R p f ≠ 0 := mt (expand_eq_zero hp).1 hf rw [← WithBot.coe_eq_coe] convert (degree_eq_natDegree hf1).symm -- Porting note: was `rw [degree_eq_natDegree hf1]` symm 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 erw [WithBot.coe_le_coe, ← Nat.div_mul_cancel hpn] exact Nat.mul_le_mul_right p hn · rfl · refine le_degree_of_ne_zero ?_ erw [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] 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] · cases' n with n · exact absurd (dvd_zero p) h have := coeff_derivative f n rw [hf, coeff_zero, zero_eq_mul] at this cases' 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 _ n_ih · simp [RingHom.one_def] 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 isLocalRingHom_expand {p : ℕ} (hp : 0 < p) : IsLocalRingHom (↑(expand R p) : R[X] →+* R[X]) := by refine ⟨fun f hf1 => ?_⟩; norm_cast at 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 _ := isLocalRingHom_expand R hp.bot_lt of_irreducible_map (↑(expand R p)) hf 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
Algebra\Polynomial\FieldDivision.lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker -/ import Mathlib.Algebra.Polynomial.Derivative import Mathlib.Algebra.Polynomial.Roots import Mathlib.RingTheory.EuclideanDomain /-! # Theory of univariate polynomials This file starts looking like the ring theory of $R[X]$ -/ noncomputable section open Polynomial namespace Polynomial universe u v w y z variable {R : Type u} {S : Type v} {k : Type y} {A : Type z} {a b : R} {n : ℕ} section CommRing variable [CommRing R] theorem rootMultiplicity_sub_one_le_derivative_rootMultiplicity_of_ne_zero (p : R[X]) (t : R) (hnezero : derivative p ≠ 0) : p.rootMultiplicity t - 1 ≤ p.derivative.rootMultiplicity t := (le_rootMultiplicity_iff hnezero).2 <| pow_sub_one_dvd_derivative_of_pow_dvd (p.pow_rootMultiplicity_dvd t) theorem derivative_rootMultiplicity_of_root_of_mem_nonZeroDivisors {p : R[X]} {t : R} (hpt : Polynomial.IsRoot p t) (hnzd : (p.rootMultiplicity t : R) ∈ nonZeroDivisors R) : (derivative p).rootMultiplicity t = p.rootMultiplicity t - 1 := by by_cases h : p = 0 · simp only [h, map_zero, rootMultiplicity_zero] obtain ⟨g, hp, hndvd⟩ := p.exists_eq_pow_rootMultiplicity_mul_and_not_dvd h t set m := p.rootMultiplicity t have hm : m - 1 + 1 = m := Nat.sub_add_cancel <| (rootMultiplicity_pos h).2 hpt have hndvd : ¬(X - C t) ^ m ∣ derivative p := by rw [hp, derivative_mul, dvd_add_left (dvd_mul_right _ _), derivative_X_sub_C_pow, ← hm, pow_succ, hm, mul_comm (C _), mul_assoc, dvd_cancel_left_mem_nonZeroDivisors (monic_X_sub_C t |>.pow _ |>.mem_nonZeroDivisors)] rw [dvd_iff_isRoot, IsRoot] at hndvd ⊢ rwa [eval_mul, eval_C, mul_left_mem_nonZeroDivisors_eq_zero_iff hnzd] have hnezero : derivative p ≠ 0 := fun h ↦ hndvd (by rw [h]; exact dvd_zero _) exact le_antisymm (by rwa [rootMultiplicity_le_iff hnezero, hm]) (rootMultiplicity_sub_one_le_derivative_rootMultiplicity_of_ne_zero _ t hnezero) theorem isRoot_iterate_derivative_of_lt_rootMultiplicity {p : R[X]} {t : R} {n : ℕ} (hn : n < p.rootMultiplicity t) : (derivative^[n] p).IsRoot t := dvd_iff_isRoot.mp <| (dvd_pow_self _ <| Nat.sub_ne_zero_of_lt hn).trans (pow_sub_dvd_iterate_derivative_of_pow_dvd _ <| p.pow_rootMultiplicity_dvd t) open Finset in theorem eval_iterate_derivative_rootMultiplicity {p : R[X]} {t : R} : (derivative^[p.rootMultiplicity t] p).eval t = (p.rootMultiplicity t).factorial • (p /ₘ (X - C t) ^ p.rootMultiplicity t).eval t := by set m := p.rootMultiplicity t with hm conv_lhs => rw [← p.pow_mul_divByMonic_rootMultiplicity_eq t, ← hm] rw [iterate_derivative_mul, eval_finset_sum, sum_eq_single_of_mem _ (mem_range.mpr m.succ_pos)] · rw [m.choose_zero_right, one_smul, eval_mul, m.sub_zero, iterate_derivative_X_sub_pow_self, eval_natCast, nsmul_eq_mul]; rfl · intro b hb hb0 rw [iterate_derivative_X_sub_pow, eval_smul, eval_mul, eval_smul, eval_pow, Nat.sub_sub_self (mem_range_succ_iff.mp hb), eval_sub, eval_X, eval_C, sub_self, zero_pow hb0, smul_zero, zero_mul, smul_zero] theorem lt_rootMultiplicity_of_isRoot_iterate_derivative_of_mem_nonZeroDivisors {p : R[X]} {t : R} {n : ℕ} (h : p ≠ 0) (hroot : ∀ m ≤ n, (derivative^[m] p).IsRoot t) (hnzd : (n.factorial : R) ∈ nonZeroDivisors R) : n < p.rootMultiplicity t := by by_contra! h' replace hroot := hroot _ h' simp only [IsRoot, eval_iterate_derivative_rootMultiplicity] at hroot obtain ⟨q, hq⟩ := Nat.cast_dvd_cast (α := R) <| Nat.factorial_dvd_factorial h' rw [hq, mul_mem_nonZeroDivisors] at hnzd rw [nsmul_eq_mul, mul_left_mem_nonZeroDivisors_eq_zero_iff hnzd.1] at hroot exact eval_divByMonic_pow_rootMultiplicity_ne_zero t h hroot theorem lt_rootMultiplicity_of_isRoot_iterate_derivative_of_mem_nonZeroDivisors' {p : R[X]} {t : R} {n : ℕ} (h : p ≠ 0) (hroot : ∀ m ≤ n, (derivative^[m] p).IsRoot t) (hnzd : ∀ m ≤ n, m ≠ 0 → (m : R) ∈ nonZeroDivisors R) : n < p.rootMultiplicity t := by apply lt_rootMultiplicity_of_isRoot_iterate_derivative_of_mem_nonZeroDivisors h hroot clear hroot induction' n with n ih · simp only [Nat.zero_eq, Nat.factorial_zero, Nat.cast_one] exact Submonoid.one_mem _ · rw [Nat.factorial_succ, Nat.cast_mul, mul_mem_nonZeroDivisors] exact ⟨hnzd _ le_rfl n.succ_ne_zero, ih fun m h ↦ hnzd m (h.trans n.le_succ)⟩ theorem lt_rootMultiplicity_iff_isRoot_iterate_derivative_of_mem_nonZeroDivisors {p : R[X]} {t : R} {n : ℕ} (h : p ≠ 0) (hnzd : (n.factorial : R) ∈ nonZeroDivisors R) : n < p.rootMultiplicity t ↔ ∀ m ≤ n, (derivative^[m] p).IsRoot t := ⟨fun hn _ hm ↦ isRoot_iterate_derivative_of_lt_rootMultiplicity <| hm.trans_lt hn, fun hr ↦ lt_rootMultiplicity_of_isRoot_iterate_derivative_of_mem_nonZeroDivisors h hr hnzd⟩ theorem lt_rootMultiplicity_iff_isRoot_iterate_derivative_of_mem_nonZeroDivisors' {p : R[X]} {t : R} {n : ℕ} (h : p ≠ 0) (hnzd : ∀ m ≤ n, m ≠ 0 → (m : R) ∈ nonZeroDivisors R) : n < p.rootMultiplicity t ↔ ∀ m ≤ n, (derivative^[m] p).IsRoot t := ⟨fun hn _ hm ↦ isRoot_iterate_derivative_of_lt_rootMultiplicity <| Nat.lt_of_le_of_lt hm hn, fun hr ↦ lt_rootMultiplicity_of_isRoot_iterate_derivative_of_mem_nonZeroDivisors' h hr hnzd⟩ theorem one_lt_rootMultiplicity_iff_isRoot_iterate_derivative {p : R[X]} {t : R} (h : p ≠ 0) : 1 < p.rootMultiplicity t ↔ ∀ m ≤ 1, (derivative^[m] p).IsRoot t := lt_rootMultiplicity_iff_isRoot_iterate_derivative_of_mem_nonZeroDivisors h (by rw [Nat.factorial_one, Nat.cast_one]; exact Submonoid.one_mem _) theorem one_lt_rootMultiplicity_iff_isRoot {p : R[X]} {t : R} (h : p ≠ 0) : 1 < p.rootMultiplicity t ↔ p.IsRoot t ∧ (derivative p).IsRoot t := by rw [one_lt_rootMultiplicity_iff_isRoot_iterate_derivative h] refine ⟨fun h ↦ ⟨h 0 (by norm_num), h 1 (by norm_num)⟩, fun ⟨h0, h1⟩ m hm ↦ ?_⟩ obtain (_|_|m) := m exacts [h0, h1, by omega] end CommRing section IsDomain variable [CommRing R] [IsDomain R] theorem one_lt_rootMultiplicity_iff_isRoot_gcd [GCDMonoid R[X]] {p : R[X]} {t : R} (h : p ≠ 0) : 1 < p.rootMultiplicity t ↔ (gcd p (derivative p)).IsRoot t := by simp_rw [one_lt_rootMultiplicity_iff_isRoot h, ← dvd_iff_isRoot, dvd_gcd_iff] theorem derivative_rootMultiplicity_of_root [CharZero R] {p : R[X]} {t : R} (hpt : p.IsRoot t) : p.derivative.rootMultiplicity t = p.rootMultiplicity t - 1 := by by_cases h : p = 0 · rw [h, map_zero, rootMultiplicity_zero] exact derivative_rootMultiplicity_of_root_of_mem_nonZeroDivisors hpt <| mem_nonZeroDivisors_of_ne_zero <| Nat.cast_ne_zero.2 ((rootMultiplicity_pos h).2 hpt).ne' theorem rootMultiplicity_sub_one_le_derivative_rootMultiplicity [CharZero R] (p : R[X]) (t : R) : p.rootMultiplicity t - 1 ≤ p.derivative.rootMultiplicity t := by by_cases h : p.IsRoot t · exact (derivative_rootMultiplicity_of_root h).symm.le · rw [rootMultiplicity_eq_zero h, zero_tsub] exact zero_le _ theorem lt_rootMultiplicity_of_isRoot_iterate_derivative [CharZero R] {p : R[X]} {t : R} {n : ℕ} (h : p ≠ 0) (hroot : ∀ m ≤ n, (derivative^[m] p).IsRoot t) : n < p.rootMultiplicity t := lt_rootMultiplicity_of_isRoot_iterate_derivative_of_mem_nonZeroDivisors h hroot <| mem_nonZeroDivisors_of_ne_zero <| Nat.cast_ne_zero.2 <| Nat.factorial_ne_zero n theorem lt_rootMultiplicity_iff_isRoot_iterate_derivative [CharZero R] {p : R[X]} {t : R} {n : ℕ} (h : p ≠ 0) : n < p.rootMultiplicity t ↔ ∀ m ≤ n, (derivative^[m] p).IsRoot t := ⟨fun hn _ hm ↦ isRoot_iterate_derivative_of_lt_rootMultiplicity <| Nat.lt_of_le_of_lt hm hn, fun hr ↦ lt_rootMultiplicity_of_isRoot_iterate_derivative h hr⟩ section NormalizationMonoid variable [NormalizationMonoid R] instance instNormalizationMonoid : NormalizationMonoid R[X] where normUnit p := ⟨C ↑(normUnit p.leadingCoeff), C ↑(normUnit p.leadingCoeff)⁻¹, by rw [← RingHom.map_mul, Units.mul_inv, C_1], by rw [← RingHom.map_mul, Units.inv_mul, C_1]⟩ normUnit_zero := Units.ext (by simp) normUnit_mul hp0 hq0 := Units.ext (by dsimp rw [Ne, ← leadingCoeff_eq_zero] at * rw [leadingCoeff_mul, normUnit_mul hp0 hq0, Units.val_mul, C_mul]) normUnit_coe_units u := Units.ext (by dsimp rw [← mul_one u⁻¹, Units.val_mul, Units.eq_inv_mul_iff_mul_eq] rcases Polynomial.isUnit_iff.1 ⟨u, rfl⟩ with ⟨_, ⟨w, rfl⟩, h2⟩ rw [← h2, leadingCoeff_C, normUnit_coe_units, ← C_mul, Units.mul_inv, C_1] rfl) @[simp] theorem coe_normUnit {p : R[X]} : (normUnit p : R[X]) = C ↑(normUnit p.leadingCoeff) := by simp [normUnit] theorem leadingCoeff_normalize (p : R[X]) : leadingCoeff (normalize p) = normalize (leadingCoeff p) := by simp theorem Monic.normalize_eq_self {p : R[X]} (hp : p.Monic) : normalize p = p := by simp only [Polynomial.coe_normUnit, normalize_apply, hp.leadingCoeff, normUnit_one, Units.val_one, Polynomial.C.map_one, mul_one] theorem roots_normalize {p : R[X]} : (normalize p).roots = p.roots := by rw [normalize_apply, mul_comm, coe_normUnit, roots_C_mul _ (normUnit (leadingCoeff p)).ne_zero] theorem normUnit_X : normUnit (X : Polynomial R) = 1 := by have := coe_normUnit (R := R) (p := X) rwa [leadingCoeff_X, normUnit_one, Units.val_one, map_one, Units.val_eq_one] at this theorem X_eq_normalize : (X : Polynomial R) = normalize X := by simp only [normalize_apply, normUnit_X, Units.val_one, mul_one] end NormalizationMonoid end IsDomain section DivisionRing variable [DivisionRing R] {p q : R[X]} theorem degree_pos_of_ne_zero_of_nonunit (hp0 : p ≠ 0) (hp : ¬IsUnit p) : 0 < degree p := lt_of_not_ge fun h => by rw [eq_C_of_degree_le_zero h] at hp0 hp exact hp (IsUnit.map C (IsUnit.mk0 (coeff p 0) (mt C_inj.2 (by simpa using hp0)))) @[simp] theorem map_eq_zero [Semiring S] [Nontrivial S] (f : R →+* S) : p.map f = 0 ↔ p = 0 := by simp only [Polynomial.ext_iff] congr! simp [map_eq_zero, coeff_map, coeff_zero] theorem map_ne_zero [Semiring S] [Nontrivial S] {f : R →+* S} (hp : p ≠ 0) : p.map f ≠ 0 := mt (map_eq_zero f).1 hp @[simp] theorem degree_map [Semiring S] [Nontrivial S] (p : R[X]) (f : R →+* S) : degree (p.map f) = degree p := p.degree_map_eq_of_injective f.injective @[simp] theorem natDegree_map [Semiring S] [Nontrivial S] (f : R →+* S) : natDegree (p.map f) = natDegree p := natDegree_eq_of_degree_eq (degree_map _ f) @[simp] theorem leadingCoeff_map [Semiring S] [Nontrivial S] (f : R →+* S) : leadingCoeff (p.map f) = f (leadingCoeff p) := by simp only [← coeff_natDegree, coeff_map f, natDegree_map] theorem monic_map_iff [Semiring S] [Nontrivial S] {f : R →+* S} {p : R[X]} : (p.map f).Monic ↔ p.Monic := by rw [Monic, leadingCoeff_map, ← f.map_one, Function.Injective.eq_iff f.injective, Monic] end DivisionRing section Field variable [Field R] {p q : R[X]} theorem isUnit_iff_degree_eq_zero : IsUnit p ↔ degree p = 0 := ⟨degree_eq_zero_of_isUnit, fun h => have : degree p ≤ 0 := by simp [*, le_refl] have hc : coeff p 0 ≠ 0 := fun hc => by rw [eq_C_of_degree_le_zero this, hc] at h; simp only [map_zero] at h; contradiction isUnit_iff_dvd_one.2 ⟨C (coeff p 0)⁻¹, by conv in p => rw [eq_C_of_degree_le_zero this] rw [← C_mul, _root_.mul_inv_cancel hc, C_1]⟩⟩ /-- Division of polynomials. See `Polynomial.divByMonic` for more details. -/ def div (p q : R[X]) := C (leadingCoeff q)⁻¹ * (p /ₘ (q * C (leadingCoeff q)⁻¹)) /-- Remainder of polynomial division. See `Polynomial.modByMonic` for more details. -/ def mod (p q : R[X]) := p %ₘ (q * C (leadingCoeff q)⁻¹) private theorem quotient_mul_add_remainder_eq_aux (p q : R[X]) : q * div p q + mod p q = p := by by_cases h : q = 0 · simp only [h, zero_mul, mod, modByMonic_zero, zero_add] · conv => rhs rw [← modByMonic_add_div p (monic_mul_leadingCoeff_inv h)] rw [div, mod, add_comm, mul_assoc] private theorem remainder_lt_aux (p : R[X]) (hq : q ≠ 0) : degree (mod p q) < degree q := by rw [← degree_mul_leadingCoeff_inv q hq] exact degree_modByMonic_lt p (monic_mul_leadingCoeff_inv hq) instance : Div R[X] := ⟨div⟩ instance : Mod R[X] := ⟨mod⟩ theorem div_def : p / q = C (leadingCoeff q)⁻¹ * (p /ₘ (q * C (leadingCoeff q)⁻¹)) := rfl theorem mod_def : p % q = p %ₘ (q * C (leadingCoeff q)⁻¹) := rfl theorem modByMonic_eq_mod (p : R[X]) (hq : Monic q) : p %ₘ q = p % q := show p %ₘ q = p %ₘ (q * C (leadingCoeff q)⁻¹) by simp only [Monic.def.1 hq, inv_one, mul_one, C_1] theorem divByMonic_eq_div (p : R[X]) (hq : Monic q) : p /ₘ q = p / q := show p /ₘ q = C (leadingCoeff q)⁻¹ * (p /ₘ (q * C (leadingCoeff q)⁻¹)) by simp only [Monic.def.1 hq, inv_one, C_1, one_mul, mul_one] theorem mod_X_sub_C_eq_C_eval (p : R[X]) (a : R) : p % (X - C a) = C (p.eval a) := modByMonic_eq_mod p (monic_X_sub_C a) ▸ modByMonic_X_sub_C_eq_C_eval _ _ theorem mul_div_eq_iff_isRoot : (X - C a) * (p / (X - C a)) = p ↔ IsRoot p a := divByMonic_eq_div p (monic_X_sub_C a) ▸ mul_divByMonic_eq_iff_isRoot instance instEuclideanDomain : EuclideanDomain R[X] := { Polynomial.commRing, Polynomial.nontrivial with quotient := (· / ·) quotient_zero := by simp [div_def] remainder := (· % ·) r := _ r_wellFounded := degree_lt_wf quotient_mul_add_remainder_eq := quotient_mul_add_remainder_eq_aux remainder_lt := fun p q hq => remainder_lt_aux _ hq mul_left_not_lt := fun p q hq => not_lt_of_ge (degree_le_mul_left _ hq) } theorem mod_eq_self_iff (hq0 : q ≠ 0) : p % q = p ↔ degree p < degree q := ⟨fun h => h ▸ EuclideanDomain.mod_lt _ hq0, fun h => by classical have : ¬degree (q * C (leadingCoeff q)⁻¹) ≤ degree p := not_le_of_gt <| by rwa [degree_mul_leadingCoeff_inv q hq0] rw [mod_def, modByMonic, dif_pos (monic_mul_leadingCoeff_inv hq0)] unfold divModByMonicAux dsimp simp only [this, false_and_iff, if_false]⟩ theorem div_eq_zero_iff (hq0 : q ≠ 0) : p / q = 0 ↔ degree p < degree q := ⟨fun h => by have := EuclideanDomain.div_add_mod p q rwa [h, mul_zero, zero_add, mod_eq_self_iff hq0] at this, fun h => by have hlt : degree p < degree (q * C (leadingCoeff q)⁻¹) := by rwa [degree_mul_leadingCoeff_inv q hq0] have hm : Monic (q * C (leadingCoeff q)⁻¹) := monic_mul_leadingCoeff_inv hq0 rw [div_def, (divByMonic_eq_zero_iff hm).2 hlt, mul_zero]⟩ theorem degree_add_div (hq0 : q ≠ 0) (hpq : degree q ≤ degree p) : degree q + degree (p / q) = degree p := by have : degree (p % q) < degree (q * (p / q)) := calc degree (p % q) < degree q := EuclideanDomain.mod_lt _ hq0 _ ≤ _ := degree_le_mul_left _ (mt (div_eq_zero_iff hq0).1 (not_lt_of_ge hpq)) conv_rhs => rw [← EuclideanDomain.div_add_mod p q, degree_add_eq_left_of_degree_lt this, degree_mul] theorem degree_div_le (p q : R[X]) : degree (p / q) ≤ degree p := by by_cases hq : q = 0 · simp [hq] · rw [div_def, mul_comm, degree_mul_leadingCoeff_inv _ hq]; exact degree_divByMonic_le _ _ theorem degree_div_lt (hp : p ≠ 0) (hq : 0 < degree q) : degree (p / q) < degree p := by have hq0 : q ≠ 0 := fun hq0 => by simp [hq0] at hq rw [div_def, mul_comm, degree_mul_leadingCoeff_inv _ hq0] exact degree_divByMonic_lt _ (monic_mul_leadingCoeff_inv hq0) hp (by rw [degree_mul_leadingCoeff_inv _ hq0]; exact hq) theorem isUnit_map [Field k] (f : R →+* k) : IsUnit (p.map f) ↔ IsUnit p := by simp_rw [isUnit_iff_degree_eq_zero, degree_map] theorem map_div [Field k] (f : R →+* k) : (p / q).map f = p.map f / q.map f := by if hq0 : q = 0 then simp [hq0] else rw [div_def, div_def, Polynomial.map_mul, map_divByMonic f (monic_mul_leadingCoeff_inv hq0), Polynomial.map_mul, map_C, leadingCoeff_map, map_inv₀] theorem map_mod [Field k] (f : R →+* k) : (p % q).map f = p.map f % q.map f := by by_cases hq0 : q = 0 · simp [hq0] · rw [mod_def, mod_def, leadingCoeff_map f, ← map_inv₀ f, ← map_C f, ← Polynomial.map_mul f, map_modByMonic f (monic_mul_leadingCoeff_inv hq0)] section open EuclideanDomain theorem gcd_map [Field k] [DecidableEq R] [DecidableEq k] (f : R →+* k) : gcd (p.map f) (q.map f) = (gcd p q).map f := GCD.induction p q (fun x => by simp_rw [Polynomial.map_zero, EuclideanDomain.gcd_zero_left]) fun x y _ ih => by rw [gcd_val, ← map_mod, ih, ← gcd_val] end theorem eval₂_gcd_eq_zero [CommSemiring k] [DecidableEq R] {ϕ : R →+* k} {f g : R[X]} {α : k} (hf : f.eval₂ ϕ α = 0) (hg : g.eval₂ ϕ α = 0) : (EuclideanDomain.gcd f g).eval₂ ϕ α = 0 := by rw [EuclideanDomain.gcd_eq_gcd_ab f g, Polynomial.eval₂_add, Polynomial.eval₂_mul, Polynomial.eval₂_mul, hf, hg, zero_mul, zero_mul, zero_add] theorem eval_gcd_eq_zero [DecidableEq R] {f g : R[X]} {α : R} (hf : f.eval α = 0) (hg : g.eval α = 0) : (EuclideanDomain.gcd f g).eval α = 0 := eval₂_gcd_eq_zero hf hg theorem root_left_of_root_gcd [CommSemiring k] [DecidableEq R] {ϕ : R →+* k} {f g : R[X]} {α : k} (hα : (EuclideanDomain.gcd f g).eval₂ ϕ α = 0) : f.eval₂ ϕ α = 0 := by cases' EuclideanDomain.gcd_dvd_left f g with p hp rw [hp, Polynomial.eval₂_mul, hα, zero_mul] theorem root_right_of_root_gcd [CommSemiring k] [DecidableEq R] {ϕ : R →+* k} {f g : R[X]} {α : k} (hα : (EuclideanDomain.gcd f g).eval₂ ϕ α = 0) : g.eval₂ ϕ α = 0 := by cases' EuclideanDomain.gcd_dvd_right f g with p hp rw [hp, Polynomial.eval₂_mul, hα, zero_mul] theorem root_gcd_iff_root_left_right [CommSemiring k] [DecidableEq R] {ϕ : R →+* k} {f g : R[X]} {α : k} : (EuclideanDomain.gcd f g).eval₂ ϕ α = 0 ↔ f.eval₂ ϕ α = 0 ∧ g.eval₂ ϕ α = 0 := ⟨fun h => ⟨root_left_of_root_gcd h, root_right_of_root_gcd h⟩, fun h => eval₂_gcd_eq_zero h.1 h.2⟩ theorem isRoot_gcd_iff_isRoot_left_right [DecidableEq R] {f g : R[X]} {α : R} : (EuclideanDomain.gcd f g).IsRoot α ↔ f.IsRoot α ∧ g.IsRoot α := root_gcd_iff_root_left_right theorem isCoprime_map [Field k] (f : R →+* k) : IsCoprime (p.map f) (q.map f) ↔ IsCoprime p q := by classical rw [← EuclideanDomain.gcd_isUnit_iff, ← EuclideanDomain.gcd_isUnit_iff, gcd_map, isUnit_map] theorem mem_roots_map [CommRing k] [IsDomain k] {f : R →+* k} {x : k} (hp : p ≠ 0) : x ∈ (p.map f).roots ↔ p.eval₂ f x = 0 := by rw [mem_roots (map_ne_zero hp), IsRoot, Polynomial.eval_map] theorem rootSet_monomial [CommRing S] [IsDomain S] [Algebra R S] {n : ℕ} (hn : n ≠ 0) {a : R} (ha : a ≠ 0) : (monomial n a).rootSet S = {0} := by classical rw [rootSet, aroots_monomial ha, Multiset.toFinset_nsmul _ _ hn, Multiset.toFinset_singleton, Finset.coe_singleton] theorem rootSet_C_mul_X_pow [CommRing S] [IsDomain S] [Algebra R S] {n : ℕ} (hn : n ≠ 0) {a : R} (ha : a ≠ 0) : rootSet (C a * X ^ n) S = {0} := by rw [C_mul_X_pow_eq_monomial, rootSet_monomial hn ha] theorem rootSet_X_pow [CommRing S] [IsDomain S] [Algebra R S] {n : ℕ} (hn : n ≠ 0) : (X ^ n : R[X]).rootSet S = {0} := by rw [← one_mul (X ^ n : R[X]), ← C_1, rootSet_C_mul_X_pow hn] exact one_ne_zero theorem rootSet_prod [CommRing S] [IsDomain S] [Algebra R S] {ι : Type*} (f : ι → R[X]) (s : Finset ι) (h : s.prod f ≠ 0) : (s.prod f).rootSet S = ⋃ i ∈ s, (f i).rootSet S := by classical simp only [rootSet, aroots, ← Finset.mem_coe] rw [Polynomial.map_prod, roots_prod, Finset.bind_toFinset, s.val_toFinset, Finset.coe_biUnion] rwa [← Polynomial.map_prod, Ne, map_eq_zero] theorem exists_root_of_degree_eq_one (h : degree p = 1) : ∃ x, IsRoot p x := ⟨-(p.coeff 0 / p.coeff 1), by have : p.coeff 1 ≠ 0 := by have h' := natDegree_eq_of_degree_eq_some h change natDegree p = 1 at h'; rw [← h'] exact mt leadingCoeff_eq_zero.1 fun h0 => by simp [h0] at h conv in p => rw [eq_X_add_C_of_degree_le_one (show degree p ≤ 1 by rw [h])] simp [IsRoot, mul_div_cancel₀ _ this]⟩ theorem coeff_inv_units (u : R[X]ˣ) (n : ℕ) : ((↑u : R[X]).coeff n)⁻¹ = (↑u⁻¹ : R[X]).coeff n := by rw [eq_C_of_degree_eq_zero (degree_coe_units u), eq_C_of_degree_eq_zero (degree_coe_units u⁻¹), coeff_C, coeff_C, inv_eq_one_div] split_ifs · rw [div_eq_iff_mul_eq (coeff_coe_units_zero_ne_zero u), coeff_zero_eq_eval_zero, coeff_zero_eq_eval_zero, ← eval_mul, ← Units.val_mul, inv_mul_self] simp · simp theorem monic_normalize [DecidableEq R] (hp0 : p ≠ 0) : Monic (normalize p) := by rw [Ne, ← leadingCoeff_eq_zero, ← Ne, ← isUnit_iff_ne_zero] at hp0 rw [Monic, leadingCoeff_normalize, normalize_eq_one] apply hp0 theorem leadingCoeff_div (hpq : q.degree ≤ p.degree) : (p / q).leadingCoeff = p.leadingCoeff / q.leadingCoeff := by by_cases hq : q = 0 · simp [hq] rw [div_def, leadingCoeff_mul, leadingCoeff_C, leadingCoeff_divByMonic_of_monic (monic_mul_leadingCoeff_inv hq) _, mul_comm, div_eq_mul_inv] rwa [degree_mul_leadingCoeff_inv q hq] theorem div_C_mul : p / (C a * q) = C a⁻¹ * (p / q) := by by_cases ha : a = 0 · simp [ha] simp only [div_def, leadingCoeff_mul, mul_inv, leadingCoeff_C, C.map_mul, mul_assoc] congr 3 rw [mul_left_comm q, ← mul_assoc, ← C.map_mul, mul_inv_cancel ha, C.map_one, one_mul] theorem C_mul_dvd (ha : a ≠ 0) : C a * p ∣ q ↔ p ∣ q := ⟨fun h => dvd_trans (dvd_mul_left _ _) h, fun ⟨r, hr⟩ => ⟨C a⁻¹ * r, by rw [mul_assoc, mul_left_comm p, ← mul_assoc, ← C.map_mul, _root_.mul_inv_cancel ha, C.map_one, one_mul, hr]⟩⟩ theorem dvd_C_mul (ha : a ≠ 0) : p ∣ Polynomial.C a * q ↔ p ∣ q := ⟨fun ⟨r, hr⟩ => ⟨C a⁻¹ * r, by rw [mul_left_comm p, ← hr, ← mul_assoc, ← C.map_mul, _root_.inv_mul_cancel ha, C.map_one, one_mul]⟩, fun h => dvd_trans h (dvd_mul_left _ _)⟩ theorem coe_normUnit_of_ne_zero [DecidableEq R] (hp : p ≠ 0) : (normUnit p : R[X]) = C p.leadingCoeff⁻¹ := by have : p.leadingCoeff ≠ 0 := mt leadingCoeff_eq_zero.mp hp simp [CommGroupWithZero.coe_normUnit _ this] theorem normalize_monic [DecidableEq R] (h : Monic p) : normalize p = p := by simp [h] theorem map_dvd_map' [Field k] (f : R →+* k) {x y : R[X]} : x.map f ∣ y.map f ↔ x ∣ y := by by_cases H : x = 0 · rw [H, Polynomial.map_zero, zero_dvd_iff, zero_dvd_iff, map_eq_zero] · classical rw [← normalize_dvd_iff, ← @normalize_dvd_iff R[X], normalize_apply, normalize_apply, coe_normUnit_of_ne_zero H, coe_normUnit_of_ne_zero (mt (map_eq_zero f).1 H), leadingCoeff_map, ← map_inv₀ f, ← map_C, ← Polynomial.map_mul, map_dvd_map _ f.injective (monic_mul_leadingCoeff_inv H)] theorem degree_normalize [DecidableEq R] : degree (normalize p) = degree p := by simp theorem prime_of_degree_eq_one (hp1 : degree p = 1) : Prime p := by classical have : Prime (normalize p) := Monic.prime_of_degree_eq_one (hp1 ▸ degree_normalize) (monic_normalize fun hp0 => absurd hp1 (hp0.symm ▸ by simp [degree_zero])) exact (normalize_associated _).prime this theorem irreducible_of_degree_eq_one (hp1 : degree p = 1) : Irreducible p := (prime_of_degree_eq_one hp1).irreducible theorem not_irreducible_C (x : R) : ¬Irreducible (C x) := by by_cases H : x = 0 · rw [H, C_0] exact not_irreducible_zero · exact fun hx => Irreducible.not_unit hx <| isUnit_C.2 <| isUnit_iff_ne_zero.2 H theorem degree_pos_of_irreducible (hp : Irreducible p) : 0 < p.degree := lt_of_not_ge fun hp0 => have := eq_C_of_degree_le_zero hp0 not_irreducible_C (p.coeff 0) <| this ▸ hp /- Porting note: factored out a have statement from isCoprime_of_is_root_of_eval_derivative_ne_zero into multiple decls because the original proof was timing out -/ theorem X_sub_C_mul_divByMonic_eq_sub_modByMonic {K : Type*} [Field K] (f : K[X]) (a : K) : (X - C a) * (f /ₘ (X - C a)) = f - f %ₘ (X - C a) := by rw [eq_sub_iff_add_eq, ← eq_sub_iff_add_eq', modByMonic_eq_sub_mul_div] exact monic_X_sub_C a /- Porting note: factored out a have statement from isCoprime_of_is_root_of_eval_derivative_ne_zero because the original proof was timing out -/ theorem divByMonic_add_X_sub_C_mul_derivate_divByMonic_eq_derivative {K : Type*} [Field K] (f : K[X]) (a : K) : f /ₘ (X - C a) + (X - C a) * derivative (f /ₘ (X - C a)) = derivative f := by have key := by apply congrArg derivative <| X_sub_C_mul_divByMonic_eq_sub_modByMonic f a rw [modByMonic_X_sub_C_eq_C_eval] at key rw [derivative_mul,derivative_sub,derivative_X,derivative_sub] at key rw [derivative_C,sub_zero,one_mul] at key rw [derivative_C,sub_zero] at key assumption /- Porting note: factored out another have statement from isCoprime_of_is_root_of_eval_derivative_ne_zero because the original proof was timing out -/ theorem X_sub_C_dvd_derivative_of_X_sub_C_dvd_divByMonic {K : Type*} [Field K] (f : K[X]) {a : K} (hf : (X - C a) ∣ f /ₘ (X - C a)) : X - C a ∣ derivative f := by have key := divByMonic_add_X_sub_C_mul_derivate_divByMonic_eq_derivative f a have ⟨u,hu⟩ := hf rw [← key, hu, ← mul_add (X - C a) u _] use (u + derivative ((X - C a) * u)) /-- If `f` is a polynomial over a field, and `a : K` satisfies `f' a ≠ 0`, then `f / (X - a)` is coprime with `X - a`. Note that we do not assume `f a = 0`, because `f / (X - a) = (f - f a) / (X - a)`. -/ theorem isCoprime_of_is_root_of_eval_derivative_ne_zero {K : Type*} [Field K] (f : K[X]) (a : K) (hf' : f.derivative.eval a ≠ 0) : IsCoprime (X - C a : K[X]) (f /ₘ (X - C a)) := by classical refine Or.resolve_left (EuclideanDomain.dvd_or_coprime (X - C a) (f /ₘ (X - C a)) (irreducible_of_degree_eq_one (Polynomial.degree_X_sub_C a))) ?_ contrapose! hf' with h have : X - C a ∣ derivative f := X_sub_C_dvd_derivative_of_X_sub_C_dvd_divByMonic f h rw [← modByMonic_eq_zero_iff_dvd (monic_X_sub_C _), modByMonic_X_sub_C_eq_C_eval] at this rwa [← C_inj, C_0] /-- To check a polynomial over a field is irreducible, it suffices to check only for divisors that have smaller degree. See also: `Polynomial.Monic.irreducible_iff_natDegree`. -/ theorem irreducible_iff_degree_lt (p : R[X]) (hp0 : p ≠ 0) (hpu : ¬ IsUnit p) : Irreducible p ↔ ∀ q, q.degree ≤ ↑(natDegree p / 2) → q ∣ p → IsUnit q := by rw [← irreducible_mul_leadingCoeff_inv, (monic_mul_leadingCoeff_inv hp0).irreducible_iff_degree_lt] · simp [hp0, natDegree_mul_leadingCoeff_inv] · contrapose! hpu exact isUnit_of_mul_eq_one _ _ hpu /-- To check a polynomial `p` over a field is irreducible, it suffices to check there are no divisors of degree `0 < d ≤ degree p / 2`. See also: `Polynomial.Monic.irreducible_iff_natDegree'`. -/ theorem irreducible_iff_lt_natDegree_lt {p : R[X]} (hp0 : p ≠ 0) (hpu : ¬ IsUnit p) : Irreducible p ↔ ∀ q, Monic q → natDegree q ∈ Finset.Ioc 0 (natDegree p / 2) → ¬ q ∣ p := by have : p * C (leadingCoeff p)⁻¹ ≠ 1 := by contrapose! hpu exact isUnit_of_mul_eq_one _ _ hpu rw [← irreducible_mul_leadingCoeff_inv, (monic_mul_leadingCoeff_inv hp0).irreducible_iff_lt_natDegree_lt this, natDegree_mul_leadingCoeff_inv _ hp0] simp only [IsUnit.dvd_mul_right (isUnit_C.mpr (IsUnit.mk0 (leadingCoeff p)⁻¹ (inv_ne_zero (leadingCoeff_ne_zero.mpr hp0))))] end Field end Polynomial /-- An irreducible polynomial over a field must have positive degree. -/ theorem Irreducible.natDegree_pos {F : Type*} [Field F] {f : F[X]} (h : Irreducible f) : 0 < f.natDegree := Nat.pos_of_ne_zero fun H ↦ by obtain ⟨x, hf⟩ := natDegree_eq_zero.1 H by_cases hx : x = 0 · rw [← hf, hx, map_zero] at h; exact not_irreducible_zero h exact h.1 (hf ▸ isUnit_C.2 (Ne.isUnit hx))
Algebra\Polynomial\GroupRingAction.lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import Mathlib.Algebra.Polynomial.AlgebraMap import Mathlib.Algebra.Polynomial.Monic import Mathlib.Algebra.Ring.Action.Basic import Mathlib.GroupTheory.GroupAction.Hom import Mathlib.GroupTheory.GroupAction.Quotient /-! # Group action on rings applied to polynomials This file contains instances and definitions relating `MulSemiringAction` to `Polynomial`. -/ variable (M : Type*) [Monoid M] open Polynomial namespace Polynomial variable (R : Type*) [Semiring R] variable {M} -- Porting note: changed `(· • ·) m` to `HSMul.hSMul m` theorem smul_eq_map [MulSemiringAction M R] (m : M) : HSMul.hSMul m = map (MulSemiringAction.toRingHom M R m) := by suffices DistribMulAction.toAddMonoidHom R[X] m = (mapRingHom (MulSemiringAction.toRingHom M R m)).toAddMonoidHom by ext1 r exact DFunLike.congr_fun this r ext n r : 2 change m • monomial n r = map (MulSemiringAction.toRingHom M R m) (monomial n r) rw [Polynomial.map_monomial, Polynomial.smul_monomial, MulSemiringAction.toRingHom_apply] variable (M) noncomputable instance [MulSemiringAction M R] : MulSemiringAction M R[X] := { Polynomial.distribMulAction with smul_one := fun m ↦ smul_eq_map R m ▸ Polynomial.map_one (MulSemiringAction.toRingHom M R m) smul_mul := fun m _ _ ↦ smul_eq_map R m ▸ Polynomial.map_mul (MulSemiringAction.toRingHom M R m) } variable {M R} variable [MulSemiringAction M R] @[simp] theorem smul_X (m : M) : (m • X : R[X]) = X := (smul_eq_map R m).symm ▸ map_X _ variable (S : Type*) [CommSemiring S] [MulSemiringAction M S] theorem smul_eval_smul (m : M) (f : S[X]) (x : S) : (m • f).eval (m • x) = m • f.eval x := Polynomial.induction_on f (fun r ↦ by rw [smul_C, eval_C, eval_C]) (fun f g ihf ihg ↦ by rw [smul_add, eval_add, ihf, ihg, eval_add, smul_add]) fun n r _ ↦ by rw [smul_mul', smul_pow', smul_C, smul_X, eval_mul, eval_C, eval_pow, eval_X, eval_mul, eval_C, eval_pow, eval_X, smul_mul', smul_pow'] variable (G : Type*) [Group G] theorem eval_smul' [MulSemiringAction G S] (g : G) (f : S[X]) (x : S) : f.eval (g • x) = g • (g⁻¹ • f).eval x := by rw [← smul_eval_smul, smul_inv_smul] theorem smul_eval [MulSemiringAction G S] (g : G) (f : S[X]) (x : S) : (g • f).eval x = g • f.eval (g⁻¹ • x) := by rw [← smul_eval_smul, smul_inv_smul] end Polynomial section CommRing variable (G : Type*) [Group G] [Fintype G] variable (R : Type*) [CommRing R] [MulSemiringAction G R] open MulAction /-- the product of `(X - g • x)` over distinct `g • x`. -/ noncomputable def prodXSubSMul (x : R) : R[X] := letI := Classical.decEq R (Finset.univ : Finset (G ⧸ MulAction.stabilizer G x)).prod fun g ↦ Polynomial.X - Polynomial.C (ofQuotientStabilizer G x g) theorem prodXSubSMul.monic (x : R) : (prodXSubSMul G R x).Monic := Polynomial.monic_prod_of_monic _ _ fun _ _ ↦ Polynomial.monic_X_sub_C _ theorem prodXSubSMul.eval (x : R) : (prodXSubSMul G R x).eval x = 0 := letI := Classical.decEq R (map_prod ((Polynomial.aeval x).toRingHom.toMonoidHom : R[X] →* R) _ _).trans <| Finset.prod_eq_zero (Finset.mem_univ <| QuotientGroup.mk 1) <| by simp theorem prodXSubSMul.smul (x : R) (g : G) : g • prodXSubSMul G R x = prodXSubSMul G R x := letI := Classical.decEq R Finset.smul_prod.trans <| Fintype.prod_bijective _ (MulAction.bijective g) _ _ fun g' ↦ by rw [ofQuotientStabilizer_smul, smul_sub, Polynomial.smul_X, Polynomial.smul_C] theorem prodXSubSMul.coeff (x : R) (g : G) (n : ℕ) : g • (prodXSubSMul G R x).coeff n = (prodXSubSMul G R x).coeff n := by rw [← Polynomial.coeff_smul, prodXSubSMul.smul] end CommRing namespace MulSemiringActionHom variable {M} variable {P : Type*} [CommSemiring P] [MulSemiringAction M P] variable {Q : Type*} [CommSemiring Q] [MulSemiringAction M Q] open Polynomial /-- An equivariant map induces an equivariant map on polynomials. -/ protected noncomputable def polynomial (g : P →+*[M] Q) : P[X] →+*[M] Q[X] where toFun := map g map_smul' m p := Polynomial.induction_on p (fun b ↦ by rw [MonoidHom.id_apply, smul_C, map_C, coe_fn_coe, g.map_smul, map_C, coe_fn_coe, smul_C]) (fun p q ihp ihq ↦ by rw [smul_add, Polynomial.map_add, ihp, ihq, Polynomial.map_add, smul_add]) fun n b _ ↦ by rw [MonoidHom.id_apply, smul_mul', smul_C, smul_pow', smul_X, Polynomial.map_mul, map_C, Polynomial.map_pow, map_X, coe_fn_coe, g.map_smul, Polynomial.map_mul, map_C, Polynomial.map_pow, map_X, smul_mul', smul_C, smul_pow', smul_X, coe_fn_coe] -- Porting note: added `.toRingHom` map_zero' := Polynomial.map_zero g.toRingHom map_add' p q := Polynomial.map_add g.toRingHom map_one' := Polynomial.map_one g.toRingHom map_mul' p q := Polynomial.map_mul g.toRingHom @[simp] theorem coe_polynomial (g : P →+*[M] Q) : (g.polynomial : P[X] → Q[X]) = map g := rfl end MulSemiringActionHom
Algebra\Polynomial\HasseDeriv.lean
/- Copyright (c) 2021 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import Mathlib.Algebra.Polynomial.BigOperators import Mathlib.Algebra.Polynomial.Derivative import Mathlib.Data.Nat.Choose.Cast import Mathlib.Data.Nat.Choose.Vandermonde import Mathlib.Tactic.FieldSimp import Mathlib.Tactic.Positivity /-! # Hasse derivative of polynomials The `k`th Hasse derivative of a polynomial `∑ a_i X^i` is `∑ (i.choose k) a_i X^(i-k)`. It is a variant of the usual derivative, and satisfies `k! * (hasseDeriv k f) = derivative^[k] f`. The main benefit is that is gives an atomic way of talking about expressions such as `(derivative^[k] f).eval r / k!`, that occur in Taylor expansions, for example. ## Main declarations In the following, we write `D k` for the `k`-th Hasse derivative `hasse_deriv k`. * `Polynomial.hasseDeriv`: the `k`-th Hasse derivative of a polynomial * `Polynomial.hasseDeriv_zero`: the `0`th Hasse derivative is the identity * `Polynomial.hasseDeriv_one`: the `1`st Hasse derivative is the usual derivative * `Polynomial.factorial_smul_hasseDeriv`: the identity `k! • (D k f) = derivative^[k] f` * `Polynomial.hasseDeriv_comp`: the identity `(D k).comp (D l) = (k+l).choose k • D (k+l)` * `Polynomial.hasseDeriv_mul`: the "Leibniz rule" `D k (f * g) = ∑ ij ∈ antidiagonal k, D ij.1 f * D ij.2 g` For the identity principle, see `Polynomial.eq_zero_of_hasseDeriv_eq_zero` in `Data/Polynomial/Taylor.lean`. ## Reference https://math.fontein.de/2009/08/12/the-hasse-derivative/ -/ noncomputable section namespace Polynomial open Nat Polynomial open Function variable {R : Type*} [Semiring R] (k : ℕ) (f : R[X]) /-- The `k`th Hasse derivative of a polynomial `∑ a_i X^i` is `∑ (i.choose k) a_i X^(i-k)`. It satisfies `k! * (hasse_deriv k f) = derivative^[k] f`. -/ def hasseDeriv (k : ℕ) : R[X] →ₗ[R] R[X] := lsum fun i => monomial (i - k) ∘ₗ DistribMulAction.toLinearMap R R (i.choose k) theorem hasseDeriv_apply : hasseDeriv k f = f.sum fun i r => monomial (i - k) (↑(i.choose k) * r) := by dsimp [hasseDeriv] congr; ext; congr apply nsmul_eq_mul theorem hasseDeriv_coeff (n : ℕ) : (hasseDeriv k f).coeff n = (n + k).choose k * f.coeff (n + k) := by rw [hasseDeriv_apply, coeff_sum, sum_def, Finset.sum_eq_single (n + k), coeff_monomial] · simp only [if_true, add_tsub_cancel_right, eq_self_iff_true] · intro i _hi hink rw [coeff_monomial] by_cases hik : i < k · simp only [Nat.choose_eq_zero_of_lt hik, ite_self, Nat.cast_zero, zero_mul] · push_neg at hik rw [if_neg] contrapose! hink exact (tsub_eq_iff_eq_add_of_le hik).mp hink · intro h simp only [not_mem_support_iff.mp h, monomial_zero_right, mul_zero, coeff_zero] theorem hasseDeriv_zero' : hasseDeriv 0 f = f := by simp only [hasseDeriv_apply, tsub_zero, Nat.choose_zero_right, Nat.cast_one, one_mul, sum_monomial_eq] @[simp] theorem hasseDeriv_zero : @hasseDeriv R _ 0 = LinearMap.id := LinearMap.ext <| hasseDeriv_zero' theorem hasseDeriv_eq_zero_of_lt_natDegree (p : R[X]) (n : ℕ) (h : p.natDegree < n) : hasseDeriv n p = 0 := by rw [hasseDeriv_apply, sum_def] refine Finset.sum_eq_zero fun x hx => ?_ simp [Nat.choose_eq_zero_of_lt ((le_natDegree_of_mem_supp _ hx).trans_lt h)] theorem hasseDeriv_one' : hasseDeriv 1 f = derivative f := by simp only [hasseDeriv_apply, derivative_apply, ← C_mul_X_pow_eq_monomial, Nat.choose_one_right, (Nat.cast_commute _ _).eq] @[simp] theorem hasseDeriv_one : @hasseDeriv R _ 1 = derivative := LinearMap.ext <| hasseDeriv_one' @[simp] theorem hasseDeriv_monomial (n : ℕ) (r : R) : hasseDeriv k (monomial n r) = monomial (n - k) (↑(n.choose k) * r) := by ext i simp only [hasseDeriv_coeff, coeff_monomial] by_cases hnik : n = i + k · rw [if_pos hnik, if_pos, ← hnik] apply tsub_eq_of_eq_add_rev rwa [add_comm] · rw [if_neg hnik, mul_zero] by_cases hkn : k ≤ n · rw [← tsub_eq_iff_eq_add_of_le hkn] at hnik rw [if_neg hnik] · push_neg at hkn rw [Nat.choose_eq_zero_of_lt hkn, Nat.cast_zero, zero_mul, ite_self] theorem hasseDeriv_C (r : R) (hk : 0 < k) : hasseDeriv k (C r) = 0 := by rw [← monomial_zero_left, hasseDeriv_monomial, Nat.choose_eq_zero_of_lt hk, Nat.cast_zero, zero_mul, monomial_zero_right] theorem hasseDeriv_apply_one (hk : 0 < k) : hasseDeriv k (1 : R[X]) = 0 := by rw [← C_1, hasseDeriv_C k _ hk] theorem hasseDeriv_X (hk : 1 < k) : hasseDeriv k (X : R[X]) = 0 := by rw [← monomial_one_one_eq_X, hasseDeriv_monomial, Nat.choose_eq_zero_of_lt hk, Nat.cast_zero, zero_mul, monomial_zero_right] theorem factorial_smul_hasseDeriv : ⇑(k ! • @hasseDeriv R _ k) = (@derivative R _)^[k] := by induction' k with k ih · rw [hasseDeriv_zero, factorial_zero, iterate_zero, one_smul, LinearMap.id_coe] ext f n : 2 rw [iterate_succ_apply', ← ih] simp only [LinearMap.smul_apply, coeff_smul, LinearMap.map_smul_of_tower, coeff_derivative, hasseDeriv_coeff, ← @choose_symm_add _ k] simp only [nsmul_eq_mul, factorial_succ, mul_assoc, succ_eq_add_one, ← add_assoc, add_right_comm n 1 k, ← cast_succ] rw [← (cast_commute (n + 1) (f.coeff (n + k + 1))).eq] simp only [← mul_assoc] norm_cast congr 2 rw [mul_comm (k+1) _, mul_assoc, mul_assoc] congr 1 have : n + k + 1 = n + (k + 1) := by apply add_assoc rw [← choose_symm_of_eq_add this, choose_succ_right_eq, mul_comm] congr rw [add_assoc, add_tsub_cancel_left] theorem hasseDeriv_comp (k l : ℕ) : (@hasseDeriv R _ k).comp (hasseDeriv l) = (k + l).choose k • hasseDeriv (k + l) := by ext i : 2 simp only [LinearMap.smul_apply, comp_apply, LinearMap.coe_comp, smul_monomial, hasseDeriv_apply, mul_one, monomial_eq_zero_iff, sum_monomial_index, mul_zero, ← tsub_add_eq_tsub_tsub, add_comm l k] rw_mod_cast [nsmul_eq_mul] rw [← Nat.cast_mul] congr 2 by_cases hikl : i < k + l · rw [choose_eq_zero_of_lt hikl, mul_zero] by_cases hil : i < l · rw [choose_eq_zero_of_lt hil, mul_zero] · push_neg at hil rw [← tsub_lt_iff_right hil] at hikl rw [choose_eq_zero_of_lt hikl, zero_mul] push_neg at hikl apply @cast_injective ℚ have h1 : l ≤ i := le_of_add_le_right hikl have h2 : k ≤ i - l := le_tsub_of_add_le_right hikl have h3 : k ≤ k + l := le_self_add push_cast rw [cast_choose ℚ h1, cast_choose ℚ h2, cast_choose ℚ h3, cast_choose ℚ hikl] rw [show i - (k + l) = i - l - k by rw [add_comm]; apply tsub_add_eq_tsub_tsub] simp only [add_tsub_cancel_left] field_simp; ring theorem natDegree_hasseDeriv_le (p : R[X]) (n : ℕ) : natDegree (hasseDeriv n p) ≤ natDegree p - n := by classical rw [hasseDeriv_apply, sum_def] refine (natDegree_sum_le _ _).trans ?_ simp_rw [Function.comp, natDegree_monomial] rw [Finset.fold_ite, Finset.fold_const] · simp only [ite_self, max_eq_right, zero_le', Finset.fold_max_le, true_and_iff, and_imp, tsub_le_iff_right, mem_support_iff, Ne, Finset.mem_filter] intro x hx hx' have hxp : x ≤ p.natDegree := le_natDegree_of_ne_zero hx have hxn : n ≤ x := by contrapose! hx' simp [Nat.choose_eq_zero_of_lt hx'] rwa [tsub_add_cancel_of_le (hxn.trans hxp)] · simp theorem natDegree_hasseDeriv [NoZeroSMulDivisors ℕ R] (p : R[X]) (n : ℕ) : natDegree (hasseDeriv n p) = natDegree p - n := by cases' lt_or_le p.natDegree n with hn hn · simpa [hasseDeriv_eq_zero_of_lt_natDegree, hn] using (tsub_eq_zero_of_le hn.le).symm · refine map_natDegree_eq_sub ?_ ?_ · exact fun h => hasseDeriv_eq_zero_of_lt_natDegree _ _ · classical simp only [ite_eq_right_iff, Ne, natDegree_monomial, hasseDeriv_monomial] intro k c c0 hh -- this is where we use the `smul_eq_zero` from `NoZeroSMulDivisors` rw [← nsmul_eq_mul, smul_eq_zero, Nat.choose_eq_zero_iff] at hh exact (tsub_eq_zero_of_le (Or.resolve_right hh c0).le).symm section open AddMonoidHom Finset.Nat open Finset (antidiagonal mem_antidiagonal) theorem hasseDeriv_mul (f g : R[X]) : hasseDeriv k (f * g) = ∑ ij ∈ antidiagonal k, hasseDeriv ij.1 f * hasseDeriv ij.2 g := by let D k := (@hasseDeriv R _ k).toAddMonoidHom let Φ := @AddMonoidHom.mul R[X] _ show (compHom (D k)).comp Φ f g = ∑ ij ∈ antidiagonal k, ((compHom.comp ((compHom Φ) (D ij.1))).flip (D ij.2) f) g simp only [← finset_sum_apply] congr 2 clear f g ext m r n s : 4 simp only [Φ, D, finset_sum_apply, coe_mulLeft, coe_comp, flip_apply, Function.comp_apply, hasseDeriv_monomial, LinearMap.toAddMonoidHom_coe, compHom_apply_apply, coe_mul, monomial_mul_monomial] have aux : ∀ x : ℕ × ℕ, x ∈ antidiagonal k → monomial (m - x.1 + (n - x.2)) (↑(m.choose x.1) * r * (↑(n.choose x.2) * s)) = monomial (m + n - k) (↑(m.choose x.1) * ↑(n.choose x.2) * (r * s)) := by intro x hx rw [mem_antidiagonal] at hx subst hx by_cases hm : m < x.1 · simp only [Nat.choose_eq_zero_of_lt hm, Nat.cast_zero, zero_mul, monomial_zero_right] by_cases hn : n < x.2 · simp only [Nat.choose_eq_zero_of_lt hn, Nat.cast_zero, zero_mul, mul_zero, monomial_zero_right] push_neg at hm hn rw [tsub_add_eq_add_tsub hm, ← add_tsub_assoc_of_le hn, ← tsub_add_eq_tsub_tsub, add_comm x.2 x.1, mul_assoc, ← mul_assoc r, ← (Nat.cast_commute _ r).eq, mul_assoc, mul_assoc] rw [Finset.sum_congr rfl aux] rw [← map_sum, ← Finset.sum_mul] congr rw_mod_cast [← Nat.add_choose_eq] end end Polynomial
Algebra\Polynomial\Identities.lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker -/ import Mathlib.Algebra.Polynomial.Derivative import Mathlib.Tactic.LinearCombination import Mathlib.Tactic.Ring /-! # Theory of univariate polynomials The main def is `Polynomial.binomExpansion`. -/ noncomputable section namespace Polynomial open Polynomial universe u v w x y z variable {R : Type u} {S : Type v} {T : Type w} {ι : Type x} {k : Type y} {A : Type z} {a b : R} {m n : ℕ} section Identities /- @TODO: `powAddExpansion` and `powSubPowFactor` are not specific to polynomials. These belong somewhere else. But not in group_power because they depend on tactic.ring_exp Maybe use `Data.Nat.Choose` to prove it. -/ /-- `(x + y)^n` can be expressed as `x^n + n*x^(n-1)*y + k * y^2` for some `k` in the ring. -/ def powAddExpansion {R : Type*} [CommSemiring R] (x y : R) : ∀ n : ℕ, { k // (x + y) ^ n = x ^ n + n * x ^ (n - 1) * y + k * y ^ 2 } | 0 => ⟨0, by simp⟩ | 1 => ⟨0, by simp⟩ | n + 2 => by cases' (powAddExpansion x y (n + 1)) with z hz exists x * z + (n + 1) * x ^ n + z * y calc (x + y) ^ (n + 2) = (x + y) * (x + y) ^ (n + 1) := by ring _ = (x + y) * (x ^ (n + 1) + ↑(n + 1) * x ^ (n + 1 - 1) * y + z * y ^ 2) := by rw [hz] _ = x ^ (n + 2) + ↑(n + 2) * x ^ (n + 1) * y + (x * z + (n + 1) * x ^ n + z * y) * y ^ 2 := by push_cast ring! variable [CommRing R] private def polyBinomAux1 (x y : R) (e : ℕ) (a : R) : { k : R // a * (x + y) ^ e = a * (x ^ e + e * x ^ (e - 1) * y + k * y ^ 2) } := by exists (powAddExpansion x y e).val congr apply (powAddExpansion _ _ _).property private theorem poly_binom_aux2 (f : R[X]) (x y : R) : f.eval (x + y) = f.sum fun e a => a * (x ^ e + e * x ^ (e - 1) * y + (polyBinomAux1 x y e a).val * y ^ 2) := by unfold eval; rw [eval₂_eq_sum]; congr with (n z) apply (polyBinomAux1 x y _ _).property private theorem poly_binom_aux3 (f : R[X]) (x y : R) : f.eval (x + y) = ((f.sum fun e a => a * x ^ e) + f.sum fun e a => a * e * x ^ (e - 1) * y) + f.sum fun e a => a * (polyBinomAux1 x y e a).val * y ^ 2 := by rw [poly_binom_aux2] simp [left_distrib, sum_add, mul_assoc] /-- A polynomial `f` evaluated at `x + y` can be expressed as the evaluation of `f` at `x`, plus `y` times the (polynomial) derivative of `f` at `x`, plus some element `k : R` times `y^2`. -/ def binomExpansion (f : R[X]) (x y : R) : { k : R // f.eval (x + y) = f.eval x + f.derivative.eval x * y + k * y ^ 2 } := by exists f.sum fun e a => a * (polyBinomAux1 x y e a).val rw [poly_binom_aux3] congr · rw [← eval_eq_sum] · rw [derivative_eval] exact (Finset.sum_mul ..).symm · exact (Finset.sum_mul ..).symm /-- `x^n - y^n` can be expressed as `z * (x - y)` for some `z` in the ring. -/ def powSubPowFactor (x y : R) : ∀ i : ℕ, { z : R // x ^ i - y ^ i = z * (x - y) } | 0 => ⟨0, by simp⟩ | 1 => ⟨1, by simp⟩ | k + 2 => by cases' @powSubPowFactor x y (k + 1) with z hz exists z * x + y ^ (k + 1) linear_combination (norm := ring) x * hz /-- For any polynomial `f`, `f.eval x - f.eval y` can be expressed as `z * (x - y)` for some `z` in the ring. -/ def evalSubFactor (f : R[X]) (x y : R) : { z : R // f.eval x - f.eval y = z * (x - y) } := by refine ⟨f.sum fun i r => r * (powSubPowFactor x y i).val, ?_⟩ delta eval; rw [eval₂_eq_sum, eval₂_eq_sum] simp only [sum, ← Finset.sum_sub_distrib, Finset.sum_mul] dsimp congr with i rw [mul_assoc, ← (powSubPowFactor x y _).prop, mul_sub] end Identities end Polynomial
Algebra\Polynomial\Induction.lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker -/ import Mathlib.Algebra.Polynomial.Basic import Mathlib.RingTheory.Ideal.Basic /-! # Induction on polynomials This file contains lemmas dealing with different flavours of induction on polynomials. See also `Data/Polynomial/Inductions.lean` (with an `s`!). The main result is `Polynomial.induction_on`. -/ noncomputable section open Finsupp Finset namespace Polynomial open Polynomial universe u v w x y z variable {R : Type u} {S : Type v} {T : Type w} {ι : Type x} {k : Type y} {A : Type z} {a b : R} {m n : ℕ} section Semiring variable [Semiring R] {p q r : R[X]} @[elab_as_elim] protected theorem induction_on {M : R[X] → Prop} (p : R[X]) (h_C : ∀ a, M (C a)) (h_add : ∀ p q, M p → M q → M (p + q)) (h_monomial : ∀ (n : ℕ) (a : R), M (C a * X ^ n) → M (C a * X ^ (n + 1))) : M p := by have A : ∀ {n : ℕ} {a}, M (C a * X ^ n) := by intro n a induction' n with n ih · rw [pow_zero, mul_one]; exact h_C a · exact h_monomial _ _ ih have B : ∀ s : Finset ℕ, M (s.sum fun n : ℕ => C (p.coeff n) * X ^ n) := by apply Finset.induction · convert h_C 0 exact C_0.symm · intro n s ns ih rw [sum_insert ns] exact h_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' {M : R[X] → Prop} (p : R[X]) (h_add : ∀ p q, M p → M q → M (p + q)) (h_monomial : ∀ (n : ℕ) (a : R), M (monomial n a)) : M p := Polynomial.induction_on p (h_monomial 0) h_add fun n a _h => by rw [C_mul_X_pow_eq_monomial]; exact h_monomial _ _ open Submodule Polynomial Set variable {f : R[X]} {I : Ideal R[X]} /-- If the coefficients of a polynomial belong to an ideal, then that ideal contains the ideal spanned by the coefficients of the polynomial. -/ theorem span_le_of_C_coeff_mem (cf : ∀ i : ℕ, C (f.coeff i) ∈ I) : Ideal.span { g | ∃ i, g = C (f.coeff i) } ≤ I := by simp only [@eq_comm _ _ (C _)] exact (Ideal.span_le.trans range_subset_iff).mpr cf theorem mem_span_C_coeff : f ∈ Ideal.span { g : R[X] | ∃ i : ℕ, g = C (coeff f i) } := by let p := Ideal.span { g : R[X] | ∃ i : ℕ, g = C (coeff f i) } nth_rw 1 [(sum_C_mul_X_pow_eq f).symm] refine Submodule.sum_mem _ fun n _hn => ?_ dsimp have : C (coeff f n) ∈ p := by apply subset_span rw [mem_setOf_eq] use n have : monomial n (1 : R) • C (coeff f n) ∈ p := p.smul_mem _ this convert this using 1 simp only [monomial_mul_C, one_mul, smul_eq_mul] rw [← C_mul_X_pow_eq_monomial] theorem exists_C_coeff_not_mem : f ∉ I → ∃ i : ℕ, C (coeff f i) ∉ I := Not.imp_symm fun cf => span_le_of_C_coeff_mem (not_exists_not.mp cf) mem_span_C_coeff end Semiring end Polynomial
Algebra\Polynomial\Inductions.lean
/- Copyright (c) 2021 Damiano Testa. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Damiano Testa, Jens Wagemaker -/ import Mathlib.Algebra.MonoidAlgebra.Division import Mathlib.Algebra.Polynomial.Degree.Definitions import Mathlib.Algebra.Polynomial.Induction import Mathlib.Algebra.Polynomial.EraseLead import Mathlib.Order.Interval.Finset.Nat /-! # Induction on polynomials This file contains lemmas dealing with different flavours of induction on polynomials. -/ 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] {p q : R[X]} /-- `divX p` returns a polynomial `q` such that `q * X + C (p.coeff 0) = p`. It can be used in a semiring where the usual division algorithm is not possible -/ def divX (p : R[X]) : R[X] := ⟨AddMonoidAlgebra.divOf p.toFinsupp 1⟩ @[simp] theorem coeff_divX : (divX p).coeff n = p.coeff (n + 1) := by rw [add_comm]; cases p; rfl theorem divX_mul_X_add (p : R[X]) : divX p * X + C (p.coeff 0) = p := ext <| by rintro ⟨_ | _⟩ <;> simp [coeff_C, Nat.succ_ne_zero, coeff_mul_X] @[simp] theorem X_mul_divX_add (p : R[X]) : X * divX p + C (p.coeff 0) = p := ext <| by rintro ⟨_ | _⟩ <;> simp [coeff_C, Nat.succ_ne_zero, coeff_mul_X] @[simp] theorem divX_C (a : R) : divX (C a) = 0 := ext fun n => by simp [coeff_divX, coeff_C, Finsupp.single_eq_of_ne _] theorem divX_eq_zero_iff : divX p = 0 ↔ p = C (p.coeff 0) := ⟨fun h => by simpa [eq_comm, h] using divX_mul_X_add p, fun h => by rw [h, divX_C]⟩ theorem divX_add : divX (p + q) = divX p + divX q := ext <| by simp @[simp] theorem divX_zero : divX (0 : R[X]) = 0 := leadingCoeff_eq_zero.mp rfl @[simp] theorem divX_one : divX (1 : R[X]) = 0 := by ext simpa only [coeff_divX, coeff_zero] using coeff_one @[simp] theorem divX_C_mul : divX (C a * p) = C a * divX p := by ext simp theorem divX_X_pow : divX (X ^ n : R[X]) = if (n = 0) then 0 else X ^ (n - 1) := by cases n · simp · ext n simp [coeff_X_pow] /-- `divX` as an additive homomorphism. -/ noncomputable def divX_hom : R[X] →+ R[X] := { toFun := divX map_zero' := divX_zero map_add' := fun _ _ => divX_add } @[simp] theorem divX_hom_toFun : divX_hom p = divX p := rfl theorem natDegree_divX_eq_natDegree_tsub_one : p.divX.natDegree = p.natDegree - 1 := by apply map_natDegree_eq_sub (φ := divX_hom) · intro f simpa [divX_hom, divX_eq_zero_iff] using eq_C_of_natDegree_eq_zero · intros n c c0 rw [← C_mul_X_pow_eq_monomial, divX_hom_toFun, divX_C_mul, divX_X_pow] split_ifs with n0 · simp [n0] · exact natDegree_C_mul_X_pow (n - 1) c c0 theorem natDegree_divX_le : p.divX.natDegree ≤ p.natDegree := natDegree_divX_eq_natDegree_tsub_one.trans_le (Nat.pred_le _) theorem divX_C_mul_X_pow : divX (C a * X ^ n) = if n = 0 then 0 else C a * X ^ (n - 1) := by simp only [divX_C_mul, divX_X_pow, mul_ite, mul_zero] theorem degree_divX_lt (hp0 : p ≠ 0) : (divX p).degree < p.degree := by haveI := Nontrivial.of_polynomial_ne hp0 calc degree (divX p) < (divX p * X + C (p.coeff 0)).degree := if h : degree p ≤ 0 then by have h' : C (p.coeff 0) ≠ 0 := by rwa [← eq_C_of_degree_le_zero h] rw [eq_C_of_degree_le_zero h, divX_C, degree_zero, zero_mul, zero_add] exact lt_of_le_of_ne bot_le (Ne.symm (mt degree_eq_bot.1 <| by simpa using h')) else by have hXp0 : divX p ≠ 0 := by simpa [divX_eq_zero_iff, -not_le, degree_le_zero_iff] using h have : leadingCoeff (divX p) * leadingCoeff X ≠ 0 := by simpa have : degree (C (p.coeff 0)) < degree (divX p * X) := calc degree (C (p.coeff 0)) ≤ 0 := degree_C_le _ < 1 := by decide _ = degree (X : R[X]) := degree_X.symm _ ≤ degree (divX p * X) := by rw [← zero_add (degree X), degree_mul' this] exact add_le_add (by rw [zero_le_degree_iff, Ne, divX_eq_zero_iff] exact fun h0 => h (h0.symm ▸ degree_C_le)) le_rfl rw [degree_add_eq_left_of_degree_lt this]; exact degree_lt_degree_mul_X hXp0 _ = degree p := congr_arg _ (divX_mul_X_add _) /-- An induction principle for polynomials, valued in Sort* instead of Prop. -/ @[elab_as_elim] noncomputable def recOnHorner {M : R[X] → Sort*} (p : R[X]) (M0 : M 0) (MC : ∀ p a, coeff p 0 = 0 → a ≠ 0 → M p → M (p + C a)) (MX : ∀ p, p ≠ 0 → M p → M (p * X)) : M p := letI := Classical.decEq R if hp : p = 0 then hp ▸ M0 else by have wf : degree (divX p) < degree p := degree_divX_lt hp rw [← divX_mul_X_add p] at * exact if hcp0 : coeff p 0 = 0 then by rw [hcp0, C_0, add_zero] exact MX _ (fun h : divX p = 0 => by simp [h, hcp0] at hp) (recOnHorner (divX p) M0 MC MX) else MC _ _ (coeff_mul_X_zero _) hcp0 (if hpX0 : divX p = 0 then show M (divX p * X) by rw [hpX0, zero_mul]; exact M0 else MX (divX p) hpX0 (recOnHorner _ M0 MC MX)) termination_by p.degree /-- A property holds for all polynomials of positive `degree` with coefficients in a semiring `R` if it holds for * `a * X`, with `a ∈ R`, * `p * X`, with `p ∈ R[X]`, * `p + a`, with `a ∈ R`, `p ∈ R[X]`, with appropriate restrictions on each term. See `natDegree_ne_zero_induction_on` for a similar statement involving no explicit multiplication. -/ @[elab_as_elim] theorem degree_pos_induction_on {P : R[X] → Prop} (p : R[X]) (h0 : 0 < degree p) (hC : ∀ {a}, a ≠ 0 → P (C a * X)) (hX : ∀ {p}, 0 < degree p → P p → P (p * X)) (hadd : ∀ {p} {a}, 0 < degree p → P p → P (p + C a)) : P p := recOnHorner p (fun h => by rw [degree_zero] at h; exact absurd h (by decide)) (fun p a heq0 _ ih h0 => (have : 0 < degree p := (lt_of_not_ge fun h => not_lt_of_ge (degree_C_le (a := a)) <| by rwa [eq_C_of_degree_le_zero h, ← C_add,heq0,zero_add] at h0) hadd this (ih this))) (fun p _ ih h0' => if h0 : 0 < degree p then hX h0 (ih h0) else by rw [eq_C_of_degree_le_zero (le_of_not_gt h0)] at h0' ⊢ exact hC fun h : coeff p 0 = 0 => by simp [h, Nat.not_lt_zero] at h0') h0 /-- A property holds for all polynomials of non-zero `natDegree` with coefficients in a semiring `R` if it holds for * `p + a`, with `a ∈ R`, `p ∈ R[X]`, * `p + q`, with `p, q ∈ R[X]`, * monomials with nonzero coefficient and non-zero exponent, with appropriate restrictions on each term. Note that multiplication is "hidden" in the assumption on monomials, so there is no explicit multiplication in the statement. See `degree_pos_induction_on` for a similar statement involving more explicit multiplications. -/ @[elab_as_elim] theorem natDegree_ne_zero_induction_on {M : R[X] → Prop} {f : R[X]} (f0 : f.natDegree ≠ 0) (h_C_add : ∀ {a p}, M p → M (C a + p)) (h_add : ∀ {p q}, M p → M q → M (p + q)) (h_monomial : ∀ {n : ℕ} {a : R}, a ≠ 0 → n ≠ 0 → M (monomial n a)) : M f := by suffices f.natDegree = 0 ∨ M f from Or.recOn this (fun h => (f0 h).elim) id refine Polynomial.induction_on f ?_ ?_ ?_ · exact fun a => Or.inl (natDegree_C _) · rintro p q (hp | hp) (hq | hq) · refine Or.inl ?_ rw [eq_C_of_natDegree_eq_zero hp, eq_C_of_natDegree_eq_zero hq, ← C_add, natDegree_C] · refine Or.inr ?_ rw [eq_C_of_natDegree_eq_zero hp] exact h_C_add hq · refine Or.inr ?_ rw [eq_C_of_natDegree_eq_zero hq, add_comm] exact h_C_add hp · exact Or.inr (h_add hp hq) · intro n a _ by_cases a0 : a = 0 · exact Or.inl (by rw [a0, C_0, zero_mul, natDegree_zero]) · refine Or.inr ?_ rw [C_mul_X_pow_eq_monomial] exact h_monomial a0 n.succ_ne_zero end Semiring end Polynomial
Algebra\Polynomial\Laurent.lean
/- Copyright (c) 2022 Damiano Testa. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Damiano Testa -/ import Mathlib.Algebra.Polynomial.AlgebraMap import Mathlib.Algebra.Polynomial.Reverse import Mathlib.Algebra.Polynomial.Inductions import Mathlib.RingTheory.Localization.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. -- (Riccardo) giving a morphism (as `R`-alg, so in the commutative case) from `R[T,T⁻¹]` to `S` is the same as choosing a unit of `S`. -- 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, int_degree, int_trailing_degree, leading_coeff, trailing_coeff,...`. -/ open Polynomial Function AddMonoidAlgebra Finsupp noncomputable section variable {R : 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 -- Porting note: `ext` no longer applies `Finsupp.ext` automatically @[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 -- Porting note: was `convert single_mul_single.symm` 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 n, 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 -- Porting note: was `convert single_mul_single.symm` 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] -- Porting note (#10618): simp can prove this theorem _root_.Polynomial.toLaurent_one : (Polynomial.toLaurent : R[X] → R[T;T⁻¹]) 1 = 1 := map_one Polynomial.toLaurent -- @[simp] -- Porting note (#10618): simp can prove this theorem _root_.Polynomial.toLaurent_C_mul_eq (r : R) (f : R[X]) : toLaurent (Polynomial.C r * f) = C r * toLaurent f := by simp only [_root_.map_mul, Polynomial.toLaurent_C] -- @[simp] -- Porting note (#10618): simp can prove this 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] -- @[simp] -- Porting note (#10618): simp can prove this theorem _root_.Polynomial.toLaurent_C_mul_X_pow (n : ℕ) (r : R) : toLaurent (Polynomial.C r * X ^ n) = C r * T n := by simp only [_root_.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, add_left_neg, T_zero] mul_invOf_self := by rw [← T_add, add_right_neg, 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.toFun n) * T n) := by apply Finset.induction · convert h_C 0 simp only [Finset.sum_empty, _root_.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] -- Porting note: did not make progress in `simp_rw` rw [Finset.sum_apply'] simp_rw [Finsupp.single_apply, Finset.sum_ite_eq'] split_ifs with h · rfl · exact Finsupp.not_mem_support_iff.mp h /-- 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' {M : R[T;T⁻¹] → Prop} (p : R[T;T⁻¹]) (h_add : ∀ p q, M p → M q → M (p + q)) (h_C_mul_T : ∀ (n : ℤ) (a : R), M (C a * T n)) : M p := by refine p.induction_on (fun a => ?_) (fun {p q} => h_add p q) ?_ ?_ <;> try exact fun n f _ => h_C_mul_T _ f convert h_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 p q 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 /-- `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 rw [← single_eq_C_mul_T, trunc, AddMonoidHom.coe_comp, Function.comp_apply] -- Porting note (#10691): was `rw` erw [comapDomain.addMonoidHom_apply Int.ofNat_injective] rw [toFinsuppIso_apply] -- Porting note: rewrote proof below relative to mathlib3. by_cases n0 : 0 ≤ n · lift n to ℕ using n0 erw [comapDomain_single] simp only [Nat.cast_nonneg, Int.toNat_ofNat, ite_true, toFinsupp_monomial] · lift -n to ℕ using (neg_pos.mpr (not_le.mp n0)).le with m rw [toFinsupp_inj, if_neg n0] ext a have := ((not_le.mp n0).trans_le (Int.ofNat_zero_le a)).ne 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, _root_.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]} : f ≠ 0 ↔ toLaurent f ≠ 0 := (map_ne_zero_iff _ Polynomial.toLaurent_injective).symm 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.ofNat_add] · cases' 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.ofNat_succ, mul_T_assoc, add_left_neg, 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 {Q : R[T;T⁻¹] → Prop} (f : R[T;T⁻¹]) (Qf : ∀ {f : R[X]} {n : ℕ}, Q (toLaurent f * T (-n))) : Q 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 Qf /-- 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 n hn · simpa only [Nat.zero_eq, Nat.cast_zero, neg_zero, T_zero, mul_one] using Qf _ · convert QT _ _ simpa using hn section Support theorem support_C_mul_T (a : R) (n : ℤ) : Finsupp.support (C a * T n) ⊆ {n} := by -- Porting note: was -- simpa only [← single_eq_C_mul_T] using support_single_subset 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 · simp (config := { contextual := true }) only [Polynomial.support_eq_empty, map_zero, Finsupp.support_zero, eq_self_iff_true, imp_true_iff, Finset.map_empty, Finsupp.support_eq_empty] · 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_not_mem 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]⟩ rw [degree, Finset.max_eq_sup_withBot] at h ext n refine not_not.mp fun f0 => ?_ simp_rw [Finset.sup_eq_bot_iff, Finsupp.mem_support_iff, Ne, WithBot.coe_ne_bot] at h exact h n f0 section ExactDegrees @[simp] theorem degree_C_mul_T (n : ℤ) (a : R) (a0 : a ≠ 0) : degree (C a * T n) = n := by rw [degree] -- Porting note: was `convert Finset.max_singleton` have : Finsupp.support (C a * T n) = {n} := by refine support_eq_singleton.mpr ?_ rw [← single_eq_C_mul_T] simp only [single_eq_same, a0, Ne, not_false_iff, eq_self_iff_true, and_self_iff] rw [this] 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] instance algebraPolynomial (R : Type*) [CommSemiring R] : Algebra R[X] R[T;T⁻¹] := { Polynomial.toLaurent with commutes' := fun f l => by simp [mul_comm] smul_def' := fun f l => 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 theorem isLocalization : IsLocalization (Submonoid.closure ({X} : Set R[X])) R[T;T⁻¹] := { map_units' := fun t => by cases' t with t ht rcases Submonoid.mem_closure_singleton.mp ht with ⟨n, rfl⟩ simp only [isUnit_T n, algebraMap_eq_toLaurent, Polynomial.toLaurent_X_pow] surj' := fun f => by induction' f using LaurentPolynomial.induction_on_mul_T with f n have := (Submonoid.closure ({X} : Set R[X])).pow_mem Submonoid.mem_closure_singleton_self n refine ⟨(f, ⟨_, this⟩), ?_⟩ simp only [algebraMap_eq_toLaurent, Polynomial.toLaurent_X_pow, mul_T_assoc, add_left_neg, 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⟩ } 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 p t _ _ ih p hp ih · simp · simp [add_mul, ← ih] · simpa [natDegree_mul_X hp] end Inversion end LaurentPolynomial
Algebra\Polynomial\Lifts.lean
/- Copyright (c) 2020 Riccardo Brasca. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Riccardo Brasca -/ import Mathlib.Algebra.Polynomial.AlgebraMap 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, Set.mem_range, 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 /-- 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, Set.mem_univ, Subsemiring.coe_top, eq_self_iff_true, and_self_iff]⟩ /-- 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, Set.mem_univ, Subsemiring.coe_top, eq_self_iff_true, and_self_iff] /-- The polynomial `X` lifts. -/ theorem X_mem_lifts (f : R →+* S) : (X : S[X]) ∈ lifts f := ⟨X, by simp only [coe_mapRingHom, Set.mem_univ, Subsemiring.coe_top, eq_self_iff_true, map_X, and_self_iff]⟩ /-- 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, Set.mem_univ, Subsemiring.coe_top, eq_self_iff_true, map_X, and_self_iff]⟩ /-- 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, Set.mem_univ, map_monomial, Subsemiring.coe_top, eq_self_iff_true, and_self_iff] /-- 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 by_cases hzero : s = 0 · use 0 simp only [hzero, degree_zero, eq_self_iff_true, and_self_iff, monomial_zero_right, Polynomial.map_zero] rw [lifts_iff_set_range] at hl obtain ⟨q, hq⟩ := hl replace hq := (ext_iff.1 hq) n have hcoeff : f (q.coeff n) = s := by simp? [coeff_monomial] at hq says simp only [coeff_map, coeff_monomial, ↓reduceIte] at hq exact hq use monomial n (q.coeff n) constructor · simp only [hcoeff, map_monomial] have hqzero : q.coeff n ≠ 0 := by intro habs simp only [habs, RingHom.map_zero] at hcoeff exact hzero hcoeff.symm rw [← C_mul_X_pow_eq_monomial] rw [← C_mul_X_pow_eq_monomial] simp only [hzero, hqzero, Ne, not_false_iff, degree_C_mul_X_pow] /-- 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 generalize hd : p.natDegree = d revert hd p induction' d using Nat.strong_induction_on with n hn intros p hlifts hdeg by_cases erase_zero : p.eraseLead = 0 · rw [← eraseLead_add_monomial_natDegree_leadingCoeff p, erase_zero, zero_add, leadingCoeff] exact monomial_mem_lifts_and_degree_eq (monomial_mem_lifts p.natDegree ((lifts_iff_coeff_lifts p).1 hlifts p.natDegree)) have deg_erase := Or.resolve_right (eraseLead_natDegree_lt_or_eraseLead_eq_zero p) erase_zero have pzero : p ≠ 0 := by intro habs exfalso rw [habs, eraseLead_zero, eq_self_iff_true, not_true] at erase_zero exact erase_zero have lead_zero : p.coeff p.natDegree ≠ 0 := by rw [← leadingCoeff, Ne, leadingCoeff_eq_zero]; exact pzero obtain ⟨lead, hlead⟩ := monomial_mem_lifts_and_degree_eq (monomial_mem_lifts p.natDegree ((lifts_iff_coeff_lifts p).1 hlifts p.natDegree)) have deg_lead : lead.degree = p.natDegree := by rw [hlead.2, ← C_mul_X_pow_eq_monomial, degree_C_mul_X_pow p.natDegree lead_zero] rw [hdeg] at deg_erase obtain ⟨erase, herase⟩ := hn p.eraseLead.natDegree deg_erase (erase_mem_lifts p.natDegree hlifts) (refl p.eraseLead.natDegree) use erase + lead constructor · simp only [hlead, herase, Polynomial.map_add] rw [← eraseLead, ← leadingCoeff] rw [eraseLead_add_monomial_natDegree_leadingCoeff p] rw [degree_eq_natDegree pzero, ← deg_lead] apply degree_add_eq_right_of_degree_lt rw [herase.2, deg_lead, ← degree_eq_natDegree pzero] exact degree_erase_lt pzero 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 cases' subsingleton_or_nontrivial R with hR hR · obtain ⟨q, hq⟩ := mem_lifts_and_degree_eq hlifts exact ⟨q, hq.1, hq.2, monic_of_subsingleton _⟩ have H : erase p.natDegree p + X ^ p.natDegree = p := by simpa only [hp.leadingCoeff, C_1, one_mul, eraseLead] using eraseLead_add_C_mul_X_pow p by_cases h0 : erase p.natDegree p = 0 · rw [← H, h0, zero_add] refine ⟨X ^ p.natDegree, ?_, ?_, monic_X_pow p.natDegree⟩ · rw [Polynomial.map_pow, map_X] · rw [degree_X_pow, degree_X_pow] obtain ⟨q, hq⟩ := mem_lifts_and_degree_eq (erase_mem_lifts p.natDegree hlifts) have p_neq_0 : p ≠ 0 := by intro hp; apply h0; rw [hp]; simp only [natDegree_zero, erase_zero] have hdeg : q.degree < ((X : R[X]) ^ p.natDegree).degree := by rw [@degree_X_pow R, hq.2, ← degree_eq_natDegree p_neq_0] exact degree_erase_lt p_neq_0 refine ⟨q + X ^ p.natDegree, ?_, ?_, (monic_X_pow _).add_of_right hdeg⟩ · rw [Polynomial.map_add, hq.1, Polynomial.map_pow, map_X, H] · rw [degree_add_eq_right_of_degree_lt hdeg, degree_X_pow, degree_eq_natDegree hp.ne_zero] 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 cases' 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] /-- 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]) /-- `mapAlg` is the morphism induced by `R → S`. -/ theorem mapAlg_eq_map (p : R[X]) : mapAlg R S p = map (algebraMap R S) p := by simp only [mapAlg, aeval_def, eval₂_eq_sum, map, algebraMap_apply, RingHom.coe_comp] ext; congr /-- 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 end Algebra end Polynomial
Algebra\Polynomial\Mirror.lean
/- Copyright (c) 2020 Thomas Browning. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Thomas Browning -/ import Mathlib.Algebra.BigOperators.NatAntidiagonal import Mathlib.Algebra.Polynomial.RingDivision /-! # "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 open 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 n => (· ^ 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
Algebra\Polynomial\Monic.lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker -/ import Mathlib.Algebra.Polynomial.Reverse import Mathlib.Algebra.Regular.SMul /-! # Theory of monic polynomials We give several tools for proving that polynomials are monic, e.g. `Monic.mul`, `Monic.map`, `Monic.pow`. -/ noncomputable section open Finset open Polynomial namespace Polynomial universe u v y variable {R : Type u} {S : Type v} {a b : R} {m n : ℕ} {ι : Type y} section Semiring variable [Semiring R] {p q r : R[X]} theorem monic_zero_iff_subsingleton : Monic (0 : R[X]) ↔ Subsingleton R := subsingleton_iff_zero_eq_one theorem not_monic_zero_iff : ¬Monic (0 : R[X]) ↔ (0 : R) ≠ 1 := (monic_zero_iff_subsingleton.trans subsingleton_iff_zero_eq_one.symm).not theorem monic_zero_iff_subsingleton' : Monic (0 : R[X]) ↔ (∀ f g : R[X], f = g) ∧ ∀ a b : R, a = b := Polynomial.monic_zero_iff_subsingleton.trans ⟨by intro simp [eq_iff_true_of_subsingleton], fun h => subsingleton_iff.mpr h.2⟩ theorem Monic.as_sum (hp : p.Monic) : p = X ^ p.natDegree + ∑ i ∈ range p.natDegree, C (p.coeff i) * X ^ i := by conv_lhs => rw [p.as_sum_range_C_mul_X_pow, sum_range_succ_comm] suffices C (p.coeff p.natDegree) = 1 by rw [this, one_mul] exact congr_arg C hp theorem ne_zero_of_ne_zero_of_monic (hp : p ≠ 0) (hq : Monic q) : q ≠ 0 := by rintro rfl rw [Monic.def, leadingCoeff_zero] at hq rw [← mul_one p, ← C_1, ← hq, C_0, mul_zero] at hp exact hp rfl theorem Monic.map [Semiring S] (f : R →+* S) (hp : Monic p) : Monic (p.map f) := by unfold Monic nontriviality have : f p.leadingCoeff ≠ 0 := by rw [show _ = _ from hp, f.map_one] exact one_ne_zero rw [Polynomial.leadingCoeff, coeff_map] suffices p.coeff (p.map f).natDegree = 1 by simp [this] rwa [natDegree_eq_of_degree_eq (degree_map_eq_of_leadingCoeff_ne_zero f this)] theorem monic_C_mul_of_mul_leadingCoeff_eq_one {b : R} (hp : b * p.leadingCoeff = 1) : Monic (C b * p) := by unfold Monic nontriviality rw [leadingCoeff_mul' _] <;> simp [leadingCoeff_C b, hp] theorem monic_mul_C_of_leadingCoeff_mul_eq_one {b : R} (hp : p.leadingCoeff * b = 1) : Monic (p * C b) := by unfold Monic nontriviality rw [leadingCoeff_mul' _] <;> simp [leadingCoeff_C b, hp] theorem monic_of_degree_le (n : ℕ) (H1 : degree p ≤ n) (H2 : coeff p n = 1) : Monic p := Decidable.byCases (fun H : degree p < n => eq_of_zero_eq_one (H2 ▸ (coeff_eq_zero_of_degree_lt H).symm) _ _) fun H : ¬degree p < n => by rwa [Monic, Polynomial.leadingCoeff, natDegree, (lt_or_eq_of_le H1).resolve_left H] theorem monic_X_pow_add {n : ℕ} (H : degree p ≤ n) : Monic (X ^ (n + 1) + p) := have H1 : degree p < (n + 1 : ℕ) := lt_of_le_of_lt H (WithBot.coe_lt_coe.2 (Nat.lt_succ_self n)) monic_of_degree_le (n + 1) (le_trans (degree_add_le _ _) (max_le (degree_X_pow_le _) (le_of_lt H1))) (by rw [coeff_add, coeff_X_pow, if_pos rfl, coeff_eq_zero_of_degree_lt H1, add_zero]) variable (a) in theorem monic_X_pow_add_C {n : ℕ} (h : n ≠ 0) : (X ^ n + C a).Monic := by obtain ⟨k, rfl⟩ := Nat.exists_eq_succ_of_ne_zero h exact monic_X_pow_add <| degree_C_le.trans Nat.WithBot.coe_nonneg theorem monic_X_add_C (x : R) : Monic (X + C x) := pow_one (X : R[X]) ▸ monic_X_pow_add_C x one_ne_zero theorem Monic.mul (hp : Monic p) (hq : Monic q) : Monic (p * q) := letI := Classical.decEq R if h0 : (0 : R) = 1 then haveI := subsingleton_of_zero_eq_one h0 Subsingleton.elim _ _ else by have : p.leadingCoeff * q.leadingCoeff ≠ 0 := by simp [Monic.def.1 hp, Monic.def.1 hq, Ne.symm h0] rw [Monic.def, leadingCoeff_mul' this, Monic.def.1 hp, Monic.def.1 hq, one_mul] theorem Monic.pow (hp : Monic p) : ∀ n : ℕ, Monic (p ^ n) | 0 => monic_one | n + 1 => by rw [pow_succ] exact (Monic.pow hp n).mul hp theorem Monic.add_of_left (hp : Monic p) (hpq : degree q < degree p) : Monic (p + q) := by rwa [Monic, add_comm, leadingCoeff_add_of_degree_lt hpq] theorem Monic.add_of_right (hq : Monic q) (hpq : degree p < degree q) : Monic (p + q) := by rwa [Monic, leadingCoeff_add_of_degree_lt hpq] theorem Monic.of_mul_monic_left (hp : p.Monic) (hpq : (p * q).Monic) : q.Monic := by contrapose! hpq rw [Monic.def] at hpq ⊢ rwa [leadingCoeff_monic_mul hp] theorem Monic.of_mul_monic_right (hq : q.Monic) (hpq : (p * q).Monic) : p.Monic := by contrapose! hpq rw [Monic.def] at hpq ⊢ rwa [leadingCoeff_mul_monic hq] namespace Monic @[simp] theorem natDegree_eq_zero_iff_eq_one (hp : p.Monic) : p.natDegree = 0 ↔ p = 1 := by constructor <;> intro h swap · rw [h] exact natDegree_one have : p = C (p.coeff 0) := by rw [← Polynomial.degree_le_zero_iff] rwa [Polynomial.natDegree_eq_zero_iff_degree_le_zero] at h rw [this] rw [← h, ← Polynomial.leadingCoeff, Monic.def.1 hp, C_1] @[simp] theorem degree_le_zero_iff_eq_one (hp : p.Monic) : p.degree ≤ 0 ↔ p = 1 := by rw [← hp.natDegree_eq_zero_iff_eq_one, natDegree_eq_zero_iff_degree_le_zero] theorem natDegree_mul (hp : p.Monic) (hq : q.Monic) : (p * q).natDegree = p.natDegree + q.natDegree := by nontriviality R apply natDegree_mul' simp [hp.leadingCoeff, hq.leadingCoeff] theorem degree_mul_comm (hp : p.Monic) (q : R[X]) : (p * q).degree = (q * p).degree := by by_cases h : q = 0 · simp [h] rw [degree_mul', hp.degree_mul] · exact add_comm _ _ · rwa [hp.leadingCoeff, one_mul, leadingCoeff_ne_zero] nonrec theorem natDegree_mul' (hp : p.Monic) (hq : q ≠ 0) : (p * q).natDegree = p.natDegree + q.natDegree := by rw [natDegree_mul'] simpa [hp.leadingCoeff, leadingCoeff_ne_zero] theorem natDegree_mul_comm (hp : p.Monic) (q : R[X]) : (p * q).natDegree = (q * p).natDegree := by by_cases h : q = 0 · simp [h] rw [hp.natDegree_mul' h, Polynomial.natDegree_mul', add_comm] simpa [hp.leadingCoeff, leadingCoeff_ne_zero] theorem not_dvd_of_natDegree_lt (hp : Monic p) (h0 : q ≠ 0) (hl : natDegree q < natDegree p) : ¬p ∣ q := by rintro ⟨r, rfl⟩ rw [hp.natDegree_mul' <| right_ne_zero_of_mul h0] at hl exact hl.not_le (Nat.le_add_right _ _) theorem not_dvd_of_degree_lt (hp : Monic p) (h0 : q ≠ 0) (hl : degree q < degree p) : ¬p ∣ q := Monic.not_dvd_of_natDegree_lt hp h0 <| natDegree_lt_natDegree h0 hl theorem nextCoeff_mul (hp : Monic p) (hq : Monic q) : nextCoeff (p * q) = nextCoeff p + nextCoeff q := by nontriviality simp only [← coeff_one_reverse] rw [reverse_mul] <;> simp [coeff_mul, antidiagonal, hp.leadingCoeff, hq.leadingCoeff, add_comm, show Nat.succ 0 = 1 from rfl] theorem nextCoeff_pow (hp : p.Monic) (n : ℕ) : (p ^ n).nextCoeff = n • p.nextCoeff := by induction n with | zero => rw [pow_zero, zero_smul, ← map_one (f := C), nextCoeff_C_eq_zero] | succ n ih => rw [pow_succ, (hp.pow n).nextCoeff_mul hp, ih, succ_nsmul] theorem eq_one_of_map_eq_one {S : Type*} [Semiring S] [Nontrivial S] (f : R →+* S) (hp : p.Monic) (map_eq : p.map f = 1) : p = 1 := by nontriviality R have hdeg : p.degree = 0 := by rw [← degree_map_eq_of_leadingCoeff_ne_zero f _, map_eq, degree_one] · rw [hp.leadingCoeff, f.map_one] exact one_ne_zero have hndeg : p.natDegree = 0 := WithBot.coe_eq_coe.mp ((degree_eq_natDegree hp.ne_zero).symm.trans hdeg) convert eq_C_of_degree_eq_zero hdeg rw [← hndeg, ← Polynomial.leadingCoeff, hp.leadingCoeff, C.map_one] theorem natDegree_pow (hp : p.Monic) (n : ℕ) : (p ^ n).natDegree = n * p.natDegree := by induction' n with n hn · simp · rw [pow_succ, (hp.pow n).natDegree_mul hp, hn, Nat.succ_mul, add_comm] end Monic @[simp] theorem natDegree_pow_X_add_C [Nontrivial R] (n : ℕ) (r : R) : ((X + C r) ^ n).natDegree = n := by rw [(monic_X_add_C r).natDegree_pow, natDegree_X_add_C, mul_one] theorem Monic.eq_one_of_isUnit (hm : Monic p) (hpu : IsUnit p) : p = 1 := by nontriviality R obtain ⟨q, h⟩ := hpu.exists_right_inv have := hm.natDegree_mul' (right_ne_zero_of_mul_eq_one h) rw [h, natDegree_one, eq_comm, add_eq_zero_iff] at this exact hm.natDegree_eq_zero_iff_eq_one.mp this.1 theorem Monic.isUnit_iff (hm : p.Monic) : IsUnit p ↔ p = 1 := ⟨hm.eq_one_of_isUnit, fun h => h.symm ▸ isUnit_one⟩ theorem eq_of_monic_of_associated (hp : p.Monic) (hq : q.Monic) (hpq : Associated p q) : p = q := by obtain ⟨u, rfl⟩ := hpq rw [(hp.of_mul_monic_left hq).eq_one_of_isUnit u.isUnit, mul_one] end Semiring section CommSemiring variable [CommSemiring R] {p : R[X]} theorem monic_multiset_prod_of_monic (t : Multiset ι) (f : ι → R[X]) (ht : ∀ i ∈ t, Monic (f i)) : Monic (t.map f).prod := by revert ht refine t.induction_on ?_ ?_; · simp intro a t ih ht rw [Multiset.map_cons, Multiset.prod_cons] exact (ht _ (Multiset.mem_cons_self _ _)).mul (ih fun _ hi => ht _ (Multiset.mem_cons_of_mem hi)) theorem monic_prod_of_monic (s : Finset ι) (f : ι → R[X]) (hs : ∀ i ∈ s, Monic (f i)) : Monic (∏ i ∈ s, f i) := monic_multiset_prod_of_monic s.1 f hs theorem Monic.nextCoeff_multiset_prod (t : Multiset ι) (f : ι → R[X]) (h : ∀ i ∈ t, Monic (f i)) : nextCoeff (t.map f).prod = (t.map fun i => nextCoeff (f i)).sum := by revert h refine Multiset.induction_on t ?_ fun a t ih ht => ?_ · simp only [Multiset.not_mem_zero, forall_prop_of_true, forall_prop_of_false, Multiset.map_zero, Multiset.prod_zero, Multiset.sum_zero, not_false_iff, forall_true_iff] rw [← C_1] rw [nextCoeff_C_eq_zero] · rw [Multiset.map_cons, Multiset.prod_cons, Multiset.map_cons, Multiset.sum_cons, Monic.nextCoeff_mul, ih] exacts [fun i hi => ht i (Multiset.mem_cons_of_mem hi), ht a (Multiset.mem_cons_self _ _), monic_multiset_prod_of_monic _ _ fun b bs => ht _ (Multiset.mem_cons_of_mem bs)] theorem Monic.nextCoeff_prod (s : Finset ι) (f : ι → R[X]) (h : ∀ i ∈ s, Monic (f i)) : nextCoeff (∏ i ∈ s, f i) = ∑ i ∈ s, nextCoeff (f i) := Monic.nextCoeff_multiset_prod s.1 f h end CommSemiring section Semiring variable [Semiring R] @[simp] theorem Monic.natDegree_map [Semiring S] [Nontrivial S] {P : R[X]} (hmo : P.Monic) (f : R →+* S) : (P.map f).natDegree = P.natDegree := by refine le_antisymm (natDegree_map_le _ _) (le_natDegree_of_ne_zero ?_) rw [coeff_map, Monic.coeff_natDegree hmo, RingHom.map_one] exact one_ne_zero @[simp] theorem Monic.degree_map [Semiring S] [Nontrivial S] {P : R[X]} (hmo : P.Monic) (f : R →+* S) : (P.map f).degree = P.degree := by by_cases hP : P = 0 · simp [hP] · refine le_antisymm (degree_map_le _ _) ?_ rw [degree_eq_natDegree hP] refine le_degree_of_ne_zero ?_ rw [coeff_map, Monic.coeff_natDegree hmo, RingHom.map_one] exact one_ne_zero section Injective open Function variable [Semiring S] {f : R →+* S} theorem degree_map_eq_of_injective (hf : Injective f) (p : R[X]) : degree (p.map f) = degree p := letI := Classical.decEq R if h : p = 0 then by simp [h] else degree_map_eq_of_leadingCoeff_ne_zero _ (by rw [← f.map_zero]; exact mt hf.eq_iff.1 (mt leadingCoeff_eq_zero.1 h)) theorem natDegree_map_eq_of_injective (hf : Injective f) (p : R[X]) : natDegree (p.map f) = natDegree p := natDegree_eq_of_degree_eq (degree_map_eq_of_injective hf p) theorem leadingCoeff_map' (hf : Injective f) (p : R[X]) : leadingCoeff (p.map f) = f (leadingCoeff p) := by unfold leadingCoeff rw [coeff_map, natDegree_map_eq_of_injective hf p] theorem nextCoeff_map (hf : Injective f) (p : R[X]) : (p.map f).nextCoeff = f p.nextCoeff := by unfold nextCoeff rw [natDegree_map_eq_of_injective hf] split_ifs <;> simp [*] theorem leadingCoeff_of_injective (hf : Injective f) (p : R[X]) : leadingCoeff (p.map f) = f (leadingCoeff p) := by delta leadingCoeff rw [coeff_map f, natDegree_map_eq_of_injective hf p] theorem monic_of_injective (hf : Injective f) {p : R[X]} (hp : (p.map f).Monic) : p.Monic := by apply hf rw [← leadingCoeff_of_injective hf, hp.leadingCoeff, f.map_one] theorem _root_.Function.Injective.monic_map_iff (hf : Injective f) {p : R[X]} : p.Monic ↔ (p.map f).Monic := ⟨Monic.map _, Polynomial.monic_of_injective hf⟩ end Injective end Semiring section Ring variable [Ring R] {p : R[X]} theorem monic_X_sub_C (x : R) : Monic (X - C x) := by simpa only [sub_eq_add_neg, C_neg] using monic_X_add_C (-x) theorem monic_X_pow_sub {n : ℕ} (H : degree p ≤ n) : Monic (X ^ (n + 1) - p) := by simpa [sub_eq_add_neg] using monic_X_pow_add (show degree (-p) ≤ n by rwa [← degree_neg p] at H) /-- `X ^ n - a` is monic. -/ theorem monic_X_pow_sub_C {R : Type u} [Ring R] (a : R) {n : ℕ} (h : n ≠ 0) : (X ^ n - C a).Monic := by simpa only [map_neg, ← sub_eq_add_neg] using monic_X_pow_add_C (-a) h theorem not_isUnit_X_pow_sub_one (R : Type*) [CommRing R] [Nontrivial R] (n : ℕ) : ¬IsUnit (X ^ n - 1 : R[X]) := by intro h rcases eq_or_ne n 0 with (rfl | hn) · simp at h apply hn rw [← @natDegree_one R, ← (monic_X_pow_sub_C _ hn).eq_one_of_isUnit h, natDegree_X_pow_sub_C] theorem Monic.sub_of_left {p q : R[X]} (hp : Monic p) (hpq : degree q < degree p) : Monic (p - q) := by rw [sub_eq_add_neg] apply hp.add_of_left rwa [degree_neg] theorem Monic.sub_of_right {p q : R[X]} (hq : q.leadingCoeff = -1) (hpq : degree p < degree q) : Monic (p - q) := by have : (-q).coeff (-q).natDegree = 1 := by rw [natDegree_neg, coeff_neg, show q.coeff q.natDegree = -1 from hq, neg_neg] rw [sub_eq_add_neg] apply Monic.add_of_right this rwa [degree_neg] end Ring section NonzeroSemiring variable [Semiring R] [Nontrivial R] {p q : R[X]} @[simp] theorem not_monic_zero : ¬Monic (0 : R[X]) := not_monic_zero_iff.mp zero_ne_one end NonzeroSemiring section NotZeroDivisor -- TODO: using gh-8537, rephrase lemmas that involve commutation around `*` using the op-ring variable [Semiring R] {p : R[X]} theorem Monic.mul_left_ne_zero (hp : Monic p) {q : R[X]} (hq : q ≠ 0) : q * p ≠ 0 := by by_cases h : p = 1 · simpa [h] rw [Ne, ← degree_eq_bot, hp.degree_mul, WithBot.add_eq_bot, not_or, degree_eq_bot] refine ⟨hq, ?_⟩ rw [← hp.degree_le_zero_iff_eq_one, not_le] at h refine (lt_trans ?_ h).ne' simp theorem Monic.mul_right_ne_zero (hp : Monic p) {q : R[X]} (hq : q ≠ 0) : p * q ≠ 0 := by by_cases h : p = 1 · simpa [h] rw [Ne, ← degree_eq_bot, hp.degree_mul_comm, hp.degree_mul, WithBot.add_eq_bot, not_or, degree_eq_bot] refine ⟨hq, ?_⟩ rw [← hp.degree_le_zero_iff_eq_one, not_le] at h refine (lt_trans ?_ h).ne' simp theorem Monic.mul_natDegree_lt_iff (h : Monic p) {q : R[X]} : (p * q).natDegree < p.natDegree ↔ p ≠ 1 ∧ q = 0 := by by_cases hq : q = 0 · suffices 0 < p.natDegree ↔ p.natDegree ≠ 0 by simpa [hq, ← h.natDegree_eq_zero_iff_eq_one] exact ⟨fun h => h.ne', fun h => lt_of_le_of_ne (Nat.zero_le _) h.symm⟩ · simp [h.natDegree_mul', hq] theorem Monic.mul_right_eq_zero_iff (h : Monic p) {q : R[X]} : p * q = 0 ↔ q = 0 := by by_cases hq : q = 0 <;> simp [h.mul_right_ne_zero, hq] theorem Monic.mul_left_eq_zero_iff (h : Monic p) {q : R[X]} : q * p = 0 ↔ q = 0 := by by_cases hq : q = 0 <;> simp [h.mul_left_ne_zero, hq] theorem Monic.isRegular {R : Type*} [Ring R] {p : R[X]} (hp : Monic p) : IsRegular p := by constructor · intro q r h dsimp only at h rw [← sub_eq_zero, ← hp.mul_right_eq_zero_iff, mul_sub, h, sub_self] · intro q r h simp only at h rw [← sub_eq_zero, ← hp.mul_left_eq_zero_iff, sub_mul, h, sub_self] theorem degree_smul_of_smul_regular {S : Type*} [Monoid S] [DistribMulAction S R] {k : S} (p : R[X]) (h : IsSMulRegular R k) : (k • p).degree = p.degree := by refine le_antisymm ?_ ?_ · rw [degree_le_iff_coeff_zero] intro m hm rw [degree_lt_iff_coeff_zero] at hm simp [hm m le_rfl] · rw [degree_le_iff_coeff_zero] intro m hm rw [degree_lt_iff_coeff_zero] at hm refine h ?_ simpa using hm m le_rfl theorem natDegree_smul_of_smul_regular {S : Type*} [Monoid S] [DistribMulAction S R] {k : S} (p : R[X]) (h : IsSMulRegular R k) : (k • p).natDegree = p.natDegree := by by_cases hp : p = 0 · simp [hp] rw [← Nat.cast_inj (R := WithBot ℕ), ← degree_eq_natDegree hp, ← degree_eq_natDegree, degree_smul_of_smul_regular p h] contrapose! hp rw [← smul_zero k] at hp exact h.polynomial hp theorem leadingCoeff_smul_of_smul_regular {S : Type*} [Monoid S] [DistribMulAction S R] {k : S} (p : R[X]) (h : IsSMulRegular R k) : (k • p).leadingCoeff = k • p.leadingCoeff := by rw [Polynomial.leadingCoeff, Polynomial.leadingCoeff, coeff_smul, natDegree_smul_of_smul_regular p h] theorem monic_of_isUnit_leadingCoeff_inv_smul (h : IsUnit p.leadingCoeff) : Monic (h.unit⁻¹ • p) := by rw [Monic.def, leadingCoeff_smul_of_smul_regular _ (isSMulRegular_of_group _), Units.smul_def] obtain ⟨k, hk⟩ := h simp only [← hk, smul_eq_mul, ← Units.val_mul, Units.val_eq_one, inv_mul_eq_iff_eq_mul] simp [Units.ext_iff, IsUnit.unit_spec] theorem isUnit_leadingCoeff_mul_right_eq_zero_iff (h : IsUnit p.leadingCoeff) {q : R[X]} : p * q = 0 ↔ q = 0 := by constructor · intro hp rw [← smul_eq_zero_iff_eq h.unit⁻¹] at hp have : h.unit⁻¹ • (p * q) = h.unit⁻¹ • p * q := by ext simp only [Units.smul_def, coeff_smul, coeff_mul, smul_eq_mul, mul_sum] refine sum_congr rfl fun x _ => ?_ rw [← mul_assoc] rwa [this, Monic.mul_right_eq_zero_iff] at hp exact monic_of_isUnit_leadingCoeff_inv_smul _ · rintro rfl simp theorem isUnit_leadingCoeff_mul_left_eq_zero_iff (h : IsUnit p.leadingCoeff) {q : R[X]} : q * p = 0 ↔ q = 0 := by constructor · intro hp replace hp := congr_arg (· * C ↑h.unit⁻¹) hp simp only [zero_mul] at hp rwa [mul_assoc, Monic.mul_left_eq_zero_iff] at hp refine monic_mul_C_of_leadingCoeff_mul_eq_one ?_ simp [Units.mul_inv_eq_iff_eq_mul, IsUnit.unit_spec] · rintro rfl rw [zero_mul] end NotZeroDivisor end Polynomial
Algebra\Polynomial\Monomial.lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker -/ import Mathlib.Algebra.Polynomial.Basic /-! # Univariate monomials Preparatory lemmas for degree_basic. -/ noncomputable section namespace Polynomial open 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 -- Porting note: `ofFinsupp.injEq` is required. 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, coeff_monomial] · have : f.coeff i = 0 := by rw [← not_mem_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 -- Porting note: Was `ext; simp [..]; simpa [..] using h₂`. ext : 1 · ext simp [f', g', h₁, RingEquiv.toRingHom_eq_coe] · refine MonoidHom.ext_mnat ?_ simpa [RingEquiv.toRingHom_eq_coe] 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
Algebra\Polynomial\PartialFractions.lean
/- Copyright (c) 2023 Sidharth Hariharan. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Buzzard, Sidharth Hariharan -/ import Mathlib.Algebra.Polynomial.Div import Mathlib.Logic.Function.Basic import Mathlib.RingTheory.Localization.FractionRing import Mathlib.Tactic.FieldSimp import Mathlib.Tactic.LinearCombination /-! # Partial fractions These results were formalised by the Xena Project, at the suggestion of Patrick Massot. # The main theorem * `div_eq_quo_add_sum_rem_div`: General partial fraction decomposition theorem for polynomials over an integral domain R : If f, g₁, g₂, ..., gₙ ∈ R[X] and the gᵢs are all monic and pairwise coprime, then ∃ q, r₁, ..., rₙ ∈ R[X] such that f / g₁g₂...gₙ = q + r₁/g₁ + ... + rₙ/gₙ and for all i, deg(rᵢ) < deg(gᵢ). * The result is formalized here in slightly more generality, using finsets. That is, if ι is an arbitrary index type, g denotes a map from ι to R[X], and if s is an arbitrary finite subset of ι, with g i monic for all i ∈ s and for all i,j ∈ s, i ≠ j → g i is coprime to g j, then we have ∃ q ∈ R[X] , r : ι → R[X] such that ∀ i ∈ s, deg(r i) < deg(g i) and f / ∏ g i = q + ∑ (r i) / (g i), where the product and sum are over s. * The proof is done by proving the two-denominator case and then performing finset induction for an arbitrary (finite) number of denominators. ## Scope for Expansion * Proving uniqueness of the decomposition -/ variable (R : Type) [CommRing R] [IsDomain R] open Polynomial variable (K : Type) [Field K] [Algebra R[X] K] [IsFractionRing R[X] K] section TwoDenominators -- Porting note: added for scoped `Algebra.cast` instance open algebraMap /-- Let R be an integral domain and f, g₁, g₂ ∈ R[X]. Let g₁ and g₂ be monic and coprime. Then, ∃ q, r₁, r₂ ∈ R[X] such that f / g₁g₂ = q + r₁/g₁ + r₂/g₂ and deg(r₁) < deg(g₁) and deg(r₂) < deg(g₂). -/ theorem div_eq_quo_add_rem_div_add_rem_div (f : R[X]) {g₁ g₂ : R[X]} (hg₁ : g₁.Monic) (hg₂ : g₂.Monic) (hcoprime : IsCoprime g₁ g₂) : ∃ q r₁ r₂ : R[X], r₁.degree < g₁.degree ∧ r₂.degree < g₂.degree ∧ (f : K) / (↑g₁ * ↑g₂) = ↑q + ↑r₁ / ↑g₁ + ↑r₂ / ↑g₂ := by rcases hcoprime with ⟨c, d, hcd⟩ refine ⟨f * d /ₘ g₁ + f * c /ₘ g₂, f * d %ₘ g₁, f * c %ₘ g₂, degree_modByMonic_lt _ hg₁, degree_modByMonic_lt _ hg₂, ?_⟩ have hg₁' : (↑g₁ : K) ≠ 0 := by norm_cast exact hg₁.ne_zero have hg₂' : (↑g₂ : K) ≠ 0 := by norm_cast exact hg₂.ne_zero have hfc := modByMonic_add_div (f * c) hg₂ have hfd := modByMonic_add_div (f * d) hg₁ field_simp norm_cast linear_combination -1 * f * hcd + -1 * g₁ * hfc + -1 * g₂ * hfd end TwoDenominators section NDenominators -- Porting note: added for scoped `Algebra.cast` instance open algebraMap /-- Let R be an integral domain and f ∈ R[X]. Let s be a finite index set. Then, a fraction of the form f / ∏ (g i) can be rewritten as q + ∑ (r i) / (g i), where deg(r i) < deg(g i), provided that the g i are monic and pairwise coprime. -/ theorem div_eq_quo_add_sum_rem_div (f : R[X]) {ι : Type*} {g : ι → R[X]} {s : Finset ι} (hg : ∀ i ∈ s, (g i).Monic) (hcop : Set.Pairwise ↑s fun i j => IsCoprime (g i) (g j)) : ∃ (q : R[X]) (r : ι → R[X]), (∀ i ∈ s, (r i).degree < (g i).degree) ∧ ((↑f : K) / ∏ i ∈ s, ↑(g i)) = ↑q + ∑ i ∈ s, (r i : K) / (g i : K) := by classical induction' s using Finset.induction_on with a b hab Hind f generalizing f · refine ⟨f, fun _ : ι => (0 : R[X]), fun i => ?_, by simp⟩ rintro ⟨⟩ obtain ⟨q₀, r₁, r₂, hdeg₁, _, hf : (↑f : K) / _ = _⟩ := div_eq_quo_add_rem_div_add_rem_div R K f (hg a (b.mem_insert_self a) : Monic (g a)) (monic_prod_of_monic _ _ fun i hi => hg i (Finset.mem_insert_of_mem hi) : Monic (∏ i ∈ b, g i)) (IsCoprime.prod_right fun i hi => hcop (Finset.mem_coe.2 (b.mem_insert_self a)) (Finset.mem_coe.2 (Finset.mem_insert_of_mem hi)) (by rintro rfl; exact hab hi)) obtain ⟨q, r, hrdeg, IH⟩ := Hind _ (fun i hi => hg i (Finset.mem_insert_of_mem hi)) (Set.Pairwise.mono (Finset.coe_subset.2 fun i hi => Finset.mem_insert_of_mem hi) hcop) refine ⟨q₀ + q, fun i => if i = a then r₁ else r i, ?_, ?_⟩ · intro i dsimp only split_ifs with h1 · cases h1 intro exact hdeg₁ · intro hi exact hrdeg i (Finset.mem_of_mem_insert_of_ne hi h1) norm_cast at hf IH ⊢ rw [Finset.prod_insert hab, hf, IH, Finset.sum_insert hab, if_pos rfl] trans (↑(q₀ + q : R[X]) : K) + (↑r₁ / ↑(g a) + ∑ i ∈ b, (r i : K) / (g i : K)) · push_cast ring congr 2 refine Finset.sum_congr rfl fun x hxb => ?_ rw [if_neg] rintro rfl exact hab hxb end NDenominators
Algebra\Polynomial\Reverse.lean
/- Copyright (c) 2020 Damiano Testa. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Damiano Testa -/ import Mathlib.Algebra.Polynomial.Degree.TrailingDegree import Mathlib.Algebra.Polynomial.EraseLead import Mathlib.Algebra.Polynomial.Eval /-! # 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 Polynomial Finsupp Finset open 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 split_ifs with h j · exact tsub_tsub_cancel_of_le h · exfalso apply j exact Nat.sub_le N i · rfl 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] -- @[simp] -- Porting note (#10618): simp can prove this 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] -- @[simp] -- Porting note (#10618): simp can prove this (once `reflect_monomial` is in simp scope) 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 [not_mem_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) theorem reflect_mul_induction (cf cg : ℕ) : ∀ N O : ℕ, ∀ f g : R[X], f.support.card ≤ cf.succ → g.support.card ≤ cg.succ → f.natDegree ≤ N → g.natDegree ≤ O → reflect (N + O) (f * g) = reflect N f * reflect O g := by induction' cf with cf hcf --first induction (left): base case · induction' cg with cg hcg -- second induction (right): base case · intro N O f g Cf Cg Nf Og 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] -- second induction (right): induction step · intro N O f g Cf Cg Nf Og 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 (gt_of_ge_of_gt Cg (eraseLead_support_card_lt g0)) · exact le_trans eraseLead_natDegree_le_aux Og --first induction (left): induction step · intro N O f g Cf Cg Nf Og 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 (gt_of_ge_of_gt Cf (eraseLead_support_card_lt f0)) · 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*} [Ring 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*} [Ring 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 n ih · simp 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
Algebra\Polynomial\RingDivision.lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker, Johan Commelin -/ import Mathlib.Algebra.Polynomial.AlgebraMap import Mathlib.Algebra.Polynomial.BigOperators import Mathlib.Algebra.Polynomial.Degree.Lemmas import Mathlib.Algebra.Polynomial.Div /-! # Theory of univariate polynomials We prove basic results about univariate polynomials. -/ noncomputable section open Polynomial open Finset namespace Polynomial universe u v w z variable {R : Type u} {S : Type v} {T : Type w} {a b : R} {n : ℕ} section CommRing variable [CommRing R] {p q : R[X]} section variable [Semiring S] theorem natDegree_pos_of_aeval_root [Algebra R S] {p : R[X]} (hp : p ≠ 0) {z : S} (hz : aeval z p = 0) (inj : ∀ x : R, algebraMap R S x = 0 → x = 0) : 0 < p.natDegree := natDegree_pos_of_eval₂_root hp (algebraMap R S) hz inj theorem degree_pos_of_aeval_root [Algebra R S] {p : R[X]} (hp : p ≠ 0) {z : S} (hz : aeval z p = 0) (inj : ∀ x : R, algebraMap R S x = 0 → x = 0) : 0 < p.degree := natDegree_pos_iff_degree_pos.mp (natDegree_pos_of_aeval_root hp hz inj) theorem modByMonic_eq_of_dvd_sub (hq : q.Monic) {p₁ p₂ : R[X]} (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] theorem add_modByMonic (p₁ p₂ : R[X]) : (p₁ + p₂) %ₘ q = p₁ %ₘ q + p₂ %ₘ q := by by_cases hq : q.Monic · cases' 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] theorem smul_modByMonic (c : R) (p : R[X]) : c • p %ₘ q = c • (p %ₘ q) := by by_cases hq : q.Monic · cases' subsingleton_or_nontrivial R with hR hR · simp only [eq_iff_true_of_subsingleton] · exact (div_modByMonic_unique (c • (p /ₘ q)) (c • (p %ₘ q)) hq ⟨by rw [mul_smul_comm, ← smul_add, modByMonic_add_div p hq], (degree_smul_le _ _).trans_lt (degree_modByMonic_lt _ hq)⟩).2 · simp_rw [modByMonic_eq_of_not_monic _ hq] /-- `_ %ₘ q` as an `R`-linear map. -/ @[simps] def modByMonicHom (q : R[X]) : R[X] →ₗ[R] R[X] where toFun p := p %ₘ q map_add' := add_modByMonic map_smul' := smul_modByMonic theorem neg_modByMonic (p mod : R[X]) : (-p) %ₘ mod = - (p %ₘ mod) := (modByMonicHom mod).map_neg p theorem sub_modByMonic (a b mod : R[X]) : (a - b) %ₘ mod = a %ₘ mod - b %ₘ mod := (modByMonicHom mod).map_sub a b end section variable [Ring S] theorem aeval_modByMonic_eq_self_of_root [Algebra R S] {p q : R[X]} (hq : q.Monic) {x : S} (hx : aeval x q = 0) : aeval x (p %ₘ q) = aeval x p := by --`eval₂_modByMonic_eq_self_of_root` doesn't work here as it needs commutativity rw [modByMonic_eq_sub_mul_div p hq, _root_.map_sub, _root_.map_mul, hx, zero_mul, sub_zero] end end CommRing section NoZeroDivisors variable [Semiring R] [NoZeroDivisors R] {p q : R[X]} instance : NoZeroDivisors R[X] where eq_zero_or_eq_zero_of_mul_eq_zero h := by rw [← leadingCoeff_eq_zero, ← leadingCoeff_eq_zero] refine eq_zero_or_eq_zero_of_mul_eq_zero ?_ rw [← leadingCoeff_zero, ← leadingCoeff_mul, h] theorem natDegree_mul (hp : p ≠ 0) (hq : q ≠ 0) : (p*q).natDegree = p.natDegree + q.natDegree := by rw [← Nat.cast_inj (R := WithBot ℕ), ← degree_eq_natDegree (mul_ne_zero hp hq), Nat.cast_add, ← degree_eq_natDegree hp, ← degree_eq_natDegree hq, degree_mul] theorem trailingDegree_mul : (p * q).trailingDegree = p.trailingDegree + q.trailingDegree := by by_cases hp : p = 0 · rw [hp, zero_mul, trailingDegree_zero, top_add] by_cases hq : q = 0 · rw [hq, mul_zero, trailingDegree_zero, add_top] · rw [trailingDegree_eq_natTrailingDegree hp, trailingDegree_eq_natTrailingDegree hq, trailingDegree_eq_natTrailingDegree (mul_ne_zero hp hq), natTrailingDegree_mul hp hq] apply WithTop.coe_add @[simp] theorem natDegree_pow (p : R[X]) (n : ℕ) : natDegree (p ^ n) = n * natDegree p := by classical obtain rfl | hp := eq_or_ne p 0 · obtain rfl | hn := eq_or_ne n 0 <;> simp [*] exact natDegree_pow' $ by rw [← leadingCoeff_pow, Ne, leadingCoeff_eq_zero]; exact pow_ne_zero _ hp theorem degree_le_mul_left (p : R[X]) (hq : q ≠ 0) : degree p ≤ degree (p * q) := by classical exact if hp : p = 0 then by simp only [hp, zero_mul, le_refl] else by rw [degree_mul, degree_eq_natDegree hp, degree_eq_natDegree hq] exact WithBot.coe_le_coe.2 (Nat.le_add_right _ _) theorem natDegree_le_of_dvd {p q : R[X]} (h1 : p ∣ q) (h2 : q ≠ 0) : p.natDegree ≤ q.natDegree := by rcases h1 with ⟨q, rfl⟩; rw [mul_ne_zero_iff] at h2 rw [natDegree_mul h2.1 h2.2]; exact Nat.le_add_right _ _ theorem degree_le_of_dvd {p q : R[X]} (h1 : p ∣ q) (h2 : q ≠ 0) : degree p ≤ degree q := by rcases h1 with ⟨q, rfl⟩; rw [mul_ne_zero_iff] at h2 exact degree_le_mul_left p h2.2 theorem eq_zero_of_dvd_of_degree_lt {p q : R[X]} (h₁ : p ∣ q) (h₂ : degree q < degree p) : q = 0 := by by_contra hc exact (lt_iff_not_ge _ _).mp h₂ (degree_le_of_dvd h₁ hc) theorem eq_zero_of_dvd_of_natDegree_lt {p q : R[X]} (h₁ : p ∣ q) (h₂ : natDegree q < natDegree p) : q = 0 := by by_contra hc exact (lt_iff_not_ge _ _).mp h₂ (natDegree_le_of_dvd h₁ hc) theorem not_dvd_of_degree_lt {p q : R[X]} (h0 : q ≠ 0) (hl : q.degree < p.degree) : ¬p ∣ q := by by_contra hcontra exact h0 (eq_zero_of_dvd_of_degree_lt hcontra hl) theorem not_dvd_of_natDegree_lt {p q : R[X]} (h0 : q ≠ 0) (hl : q.natDegree < p.natDegree) : ¬p ∣ q := by by_contra hcontra exact h0 (eq_zero_of_dvd_of_natDegree_lt hcontra hl) /-- This lemma is useful for working with the `intDegree` of a rational function. -/ theorem natDegree_sub_eq_of_prod_eq {p₁ p₂ q₁ q₂ : R[X]} (hp₁ : p₁ ≠ 0) (hq₁ : q₁ ≠ 0) (hp₂ : p₂ ≠ 0) (hq₂ : q₂ ≠ 0) (h_eq : p₁ * q₂ = p₂ * q₁) : (p₁.natDegree : ℤ) - q₁.natDegree = (p₂.natDegree : ℤ) - q₂.natDegree := by rw [sub_eq_sub_iff_add_eq_add] norm_cast rw [← natDegree_mul hp₁ hq₂, ← natDegree_mul hp₂ hq₁, h_eq] theorem natDegree_eq_zero_of_isUnit (h : IsUnit p) : natDegree p = 0 := by nontriviality R obtain ⟨q, hq⟩ := h.exists_right_inv have := natDegree_mul (left_ne_zero_of_mul_eq_one hq) (right_ne_zero_of_mul_eq_one hq) rw [hq, natDegree_one, eq_comm, add_eq_zero_iff] at this exact this.1 theorem degree_eq_zero_of_isUnit [Nontrivial R] (h : IsUnit p) : degree p = 0 := (natDegree_eq_zero_iff_degree_le_zero.mp <| natDegree_eq_zero_of_isUnit h).antisymm (zero_le_degree_iff.mpr h.ne_zero) @[simp] theorem degree_coe_units [Nontrivial R] (u : R[X]ˣ) : degree (u : R[X]) = 0 := degree_eq_zero_of_isUnit ⟨u, rfl⟩ /-- Characterization of a unit of a polynomial ring over an integral domain `R`. See `Polynomial.isUnit_iff_coeff_isUnit_isNilpotent` when `R` is a commutative ring. -/ theorem isUnit_iff : IsUnit p ↔ ∃ r : R, IsUnit r ∧ C r = p := ⟨fun hp => ⟨p.coeff 0, let h := eq_C_of_natDegree_eq_zero (natDegree_eq_zero_of_isUnit hp) ⟨isUnit_C.1 (h ▸ hp), h.symm⟩⟩, fun ⟨_, hr, hrp⟩ => hrp ▸ isUnit_C.2 hr⟩ theorem not_isUnit_of_degree_pos (p : R[X]) (hpl : 0 < p.degree) : ¬ IsUnit p := by cases subsingleton_or_nontrivial R · simp [Subsingleton.elim p 0] at hpl intro h simp [degree_eq_zero_of_isUnit h] at hpl theorem not_isUnit_of_natDegree_pos (p : R[X]) (hpl : 0 < p.natDegree) : ¬ IsUnit p := not_isUnit_of_degree_pos _ (natDegree_pos_iff_degree_pos.mp hpl) variable [CharZero R] end NoZeroDivisors section NoZeroDivisors variable [CommSemiring R] [NoZeroDivisors R] {p q : R[X]} theorem irreducible_of_monic (hp : p.Monic) (hp1 : p ≠ 1) : Irreducible p ↔ ∀ f g : R[X], f.Monic → g.Monic → f * g = p → f = 1 ∨ g = 1 := by refine ⟨fun h f g hf hg hp => (h.2 f g hp.symm).imp hf.eq_one_of_isUnit hg.eq_one_of_isUnit, fun h => ⟨hp1 ∘ hp.eq_one_of_isUnit, fun f g hfg => (h (g * C f.leadingCoeff) (f * C g.leadingCoeff) ?_ ?_ ?_).symm.imp (isUnit_of_mul_eq_one f _) (isUnit_of_mul_eq_one g _)⟩⟩ · rwa [Monic, leadingCoeff_mul, leadingCoeff_C, ← leadingCoeff_mul, mul_comm, ← hfg, ← Monic] · rwa [Monic, leadingCoeff_mul, leadingCoeff_C, ← leadingCoeff_mul, ← hfg, ← Monic] · rw [mul_mul_mul_comm, ← C_mul, ← leadingCoeff_mul, ← hfg, hp.leadingCoeff, C_1, mul_one, mul_comm, ← hfg] theorem Monic.irreducible_iff_natDegree (hp : p.Monic) : Irreducible p ↔ p ≠ 1 ∧ ∀ f g : R[X], f.Monic → g.Monic → f * g = p → f.natDegree = 0 ∨ g.natDegree = 0 := by by_cases hp1 : p = 1; · simp [hp1] rw [irreducible_of_monic hp hp1, and_iff_right hp1] refine forall₄_congr fun a b ha hb => ?_ rw [ha.natDegree_eq_zero_iff_eq_one, hb.natDegree_eq_zero_iff_eq_one] theorem Monic.irreducible_iff_natDegree' (hp : p.Monic) : Irreducible p ↔ p ≠ 1 ∧ ∀ f g : R[X], f.Monic → g.Monic → f * g = p → g.natDegree ∉ Ioc 0 (p.natDegree / 2) := by simp_rw [hp.irreducible_iff_natDegree, mem_Ioc, Nat.le_div_iff_mul_le zero_lt_two, mul_two] apply and_congr_right' constructor <;> intro h f g hf hg he <;> subst he · rw [hf.natDegree_mul hg, add_le_add_iff_right] exact fun ha => (h f g hf hg rfl).elim (ha.1.trans_le ha.2).ne' ha.1.ne' · simp_rw [hf.natDegree_mul hg, pos_iff_ne_zero] at h contrapose! h obtain hl | hl := le_total f.natDegree g.natDegree · exact ⟨g, f, hg, hf, mul_comm g f, h.1, add_le_add_left hl _⟩ · exact ⟨f, g, hf, hg, rfl, h.2, add_le_add_right hl _⟩ /-- Alternate phrasing of `Polynomial.Monic.irreducible_iff_natDegree'` where we only have to check one divisor at a time. -/ theorem Monic.irreducible_iff_lt_natDegree_lt {p : R[X]} (hp : p.Monic) (hp1 : p ≠ 1) : Irreducible p ↔ ∀ q, Monic q → natDegree q ∈ Finset.Ioc 0 (natDegree p / 2) → ¬ q ∣ p := by rw [hp.irreducible_iff_natDegree', and_iff_right hp1] constructor · rintro h g hg hdg ⟨f, rfl⟩ exact h f g (hg.of_mul_monic_left hp) hg (mul_comm f g) hdg · rintro h f g - hg rfl hdg exact h g hg hdg (dvd_mul_left g f) theorem Monic.not_irreducible_iff_exists_add_mul_eq_coeff (hm : p.Monic) (hnd : p.natDegree = 2) : ¬Irreducible p ↔ ∃ c₁ c₂, p.coeff 0 = c₁ * c₂ ∧ p.coeff 1 = c₁ + c₂ := by cases subsingleton_or_nontrivial R · simp [natDegree_of_subsingleton] at hnd rw [hm.irreducible_iff_natDegree', and_iff_right, hnd] · push_neg constructor · rintro ⟨a, b, ha, hb, rfl, hdb⟩ simp only [zero_lt_two, Nat.div_self, Nat.Ioc_succ_singleton, zero_add, mem_singleton] at hdb have hda := hnd rw [ha.natDegree_mul hb, hdb] at hda use a.coeff 0, b.coeff 0, mul_coeff_zero a b simpa only [nextCoeff, hnd, add_right_cancel hda, hdb] using ha.nextCoeff_mul hb · rintro ⟨c₁, c₂, hmul, hadd⟩ refine ⟨X + C c₁, X + C c₂, monic_X_add_C _, monic_X_add_C _, ?_, ?_⟩ · rw [p.as_sum_range_C_mul_X_pow, hnd, Finset.sum_range_succ, Finset.sum_range_succ, Finset.sum_range_one, ← hnd, hm.coeff_natDegree, hnd, hmul, hadd, C_mul, C_add, C_1] ring · rw [mem_Ioc, natDegree_X_add_C _] simp · rintro rfl simp [natDegree_one] at hnd theorem root_mul : IsRoot (p * q) a ↔ IsRoot p a ∨ IsRoot q a := by simp_rw [IsRoot, eval_mul, mul_eq_zero] theorem root_or_root_of_root_mul (h : IsRoot (p * q) a) : IsRoot p a ∨ IsRoot q a := root_mul.1 h end NoZeroDivisors section Ring variable [Ring R] [IsDomain R] {p q : R[X]} instance : IsDomain R[X] := NoZeroDivisors.to_isDomain _ end Ring section CommSemiring variable [CommSemiring R] {a p : R[X]} section Monic variable (hp : p.Monic) theorem Monic.C_dvd_iff_isUnit {a : R} : C a ∣ p ↔ IsUnit a := ⟨fun h => isUnit_iff_dvd_one.mpr <| hp.coeff_natDegree ▸ (C_dvd_iff_dvd_coeff _ _).mp h p.natDegree, fun ha => (ha.map C).dvd⟩ theorem Monic.natDegree_pos : 0 < natDegree p ↔ p ≠ 1 := Nat.pos_iff_ne_zero.trans hp.natDegree_eq_zero.not theorem Monic.degree_pos : 0 < degree p ↔ p ≠ 1 := natDegree_pos_iff_degree_pos.symm.trans hp.natDegree_pos theorem Monic.degree_pos_of_not_isUnit (hu : ¬IsUnit p) : 0 < degree p := hp.degree_pos.mpr (fun hp' ↦ (hp' ▸ hu) isUnit_one) theorem Monic.natDegree_pos_of_not_isUnit (hu : ¬IsUnit p) : 0 < natDegree p := hp.natDegree_pos.mpr (fun hp' ↦ (hp' ▸ hu) isUnit_one) theorem degree_pos_of_not_isUnit_of_dvd_monic (ha : ¬IsUnit a) (hap : a ∣ p) : 0 < degree a := by contrapose! ha with h rw [Polynomial.eq_C_of_degree_le_zero h] at hap ⊢ simpa [hp.C_dvd_iff_isUnit, isUnit_C] using hap theorem natDegree_pos_of_not_isUnit_of_dvd_monic (ha : ¬IsUnit a) (hap : a ∣ p) : 0 < natDegree a := natDegree_pos_iff_degree_pos.mpr <| degree_pos_of_not_isUnit_of_dvd_monic hp ha hap end Monic theorem eq_zero_of_mul_eq_zero_of_smul (P : R[X]) (h : ∀ r : R, r • P = 0 → r = 0) : ∀ (Q : R[X]), P * Q = 0 → Q = 0 := by intro Q hQ 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] · omega · rw [← coeff_C_mul, ← smul_eq_C_mul, IH _ hi, coeff_zero] termination_by Q => Q.natDegree open nonZeroDivisors in /-- *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 nmem_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 _) <| smul_eq_C_mul a ▸ h⟩ by_contra! h obtain ⟨Q, hQ⟩ := _root_.nmem_nonZeroDivisors_iff.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 open nonZeroDivisors in protected lemma mem_nonZeroDivisors_iff {P : R[X]} : P ∈ R[X]⁰ ↔ ∀ a : R, a • P = 0 → a = 0 := by simpa [not_imp_not] using (nmem_nonZeroDivisors_iff (P := P)).not end CommSemiring section CommRing variable [CommRing R] /- Porting note: the ML3 proof no longer worked because of a conflict in the inferred type and synthesized type for `DecidableRel` when using `Nat.le_find_iff` from `Mathlib.Algebra.Polynomial.Div` After some discussion on [Zulip] (https://leanprover.zulipchat.com/#narrow/stream/287929-mathlib4/topic/decidability.20leakage) introduced `Polynomial.rootMultiplicity_eq_nat_find_of_nonzero` to contain the issue -/ /-- The multiplicity of `a` as root of a nonzero polynomial `p` is at least `n` iff `(X - a) ^ n` divides `p`. -/ theorem le_rootMultiplicity_iff {p : R[X]} (p0 : p ≠ 0) {a : R} {n : ℕ} : n ≤ rootMultiplicity a p ↔ (X - C a) ^ n ∣ p := by classical rw [rootMultiplicity_eq_nat_find_of_nonzero p0, @Nat.le_find_iff _ (_)] simp_rw [Classical.not_not] refine ⟨fun h => ?_, fun h m hm => (pow_dvd_pow _ hm).trans h⟩ cases' n with n · rw [pow_zero] apply one_dvd · exact h n n.lt_succ_self theorem rootMultiplicity_le_iff {p : R[X]} (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] theorem pow_rootMultiplicity_not_dvd {p : R[X]} (p0 : p ≠ 0) (a : R) : ¬(X - C a) ^ (rootMultiplicity a p + 1) ∣ p := by rw [← rootMultiplicity_le_iff p0] theorem X_sub_C_pow_dvd_iff {p : R[X]} {t : R} {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] theorem comp_X_add_C_eq_zero_iff {p : R[X]} (t : R) : p.comp (X + C t) = 0 ↔ p = 0 := AddEquivClass.map_eq_zero_iff (algEquivAevalXAddC t) theorem comp_X_add_C_ne_zero_iff {p : R[X]} (t : R) : p.comp (X + C t) ≠ 0 ↔ p ≠ 0 := Iff.not <| comp_X_add_C_eq_zero_iff t theorem rootMultiplicity_eq_rootMultiplicity {p : R[X]} {t : R} : p.rootMultiplicity t = (p.comp (X + C t)).rootMultiplicity 0 := by classical simp_rw [rootMultiplicity_eq_multiplicity, comp_X_add_C_eq_zero_iff] congr; ext; congr 1 rw [C_0, sub_zero] convert (multiplicity.multiplicity_map_eq <| algEquivAevalXAddC t).symm using 2 simp [C_eq_algebraMap] theorem rootMultiplicity_eq_natTrailingDegree' {p : R[X]} : 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 theorem rootMultiplicity_eq_natTrailingDegree {p : R[X]} {t : R} : p.rootMultiplicity t = (p.comp (X + C t)).natTrailingDegree := rootMultiplicity_eq_rootMultiplicity.trans rootMultiplicity_eq_natTrailingDegree' theorem 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] section nonZeroDivisors open scoped nonZeroDivisors theorem Monic.mem_nonZeroDivisors {p : R[X]} (h : p.Monic) : p ∈ R[X]⁰ := mem_nonZeroDivisors_iff.2 fun _ hx ↦ (mul_left_eq_zero_iff h).1 hx theorem mem_nonZeroDivisors_of_leadingCoeff {p : R[X]} (h : p.leadingCoeff ∈ R⁰) : p ∈ R[X]⁰ := by refine mem_nonZeroDivisors_iff.2 fun x hx ↦ leadingCoeff_eq_zero.1 ?_ by_contra hx' rw [← mul_right_mem_nonZeroDivisors_eq_zero_iff h] at hx' simp only [← leadingCoeff_mul' hx', hx, leadingCoeff_zero, not_true] at hx' end nonZeroDivisors theorem rootMultiplicity_mul_X_sub_C_pow {p : R[X]} {a : R} {n : ℕ} (h : p ≠ 0) : (p * (X - C a) ^ n).rootMultiplicity a = p.rootMultiplicity a + n := by have h2 := monic_X_sub_C a |>.pow n |>.mul_left_ne_zero h refine le_antisymm ?_ ?_ · rw [rootMultiplicity_le_iff h2, add_assoc, add_comm n, ← add_assoc, pow_add, dvd_cancel_right_mem_nonZeroDivisors (monic_X_sub_C a |>.pow n |>.mem_nonZeroDivisors)] exact pow_rootMultiplicity_not_dvd h a · rw [le_rootMultiplicity_iff h2, pow_add] exact mul_dvd_mul_right (pow_rootMultiplicity_dvd p a) _ /-- The multiplicity of `a` as root of `(X - a) ^ n` is `n`. -/ theorem rootMultiplicity_X_sub_C_pow [Nontrivial R] (a : R) (n : ℕ) : rootMultiplicity a ((X - C a) ^ n) = n := by have := rootMultiplicity_mul_X_sub_C_pow (a := a) (n := n) C.map_one_ne_zero rwa [rootMultiplicity_C, map_one, one_mul, zero_add] at this theorem rootMultiplicity_X_sub_C_self [Nontrivial R] {x : R} : rootMultiplicity x (X - C x) = 1 := pow_one (X - C x) ▸ rootMultiplicity_X_sub_C_pow x 1 -- Porting note: swapped instance argument order theorem rootMultiplicity_X_sub_C [Nontrivial R] [DecidableEq R] {x y : R} : rootMultiplicity x (X - C y) = if x = y then 1 else 0 := by split_ifs with hxy · rw [hxy] exact rootMultiplicity_X_sub_C_self exact rootMultiplicity_eq_zero (mt root_X_sub_C.mp (Ne.symm hxy)) /-- The multiplicity of `p + q` is at least the minimum of the multiplicities. -/ theorem 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) theorem 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] exact mul_dvd_mul (pow_rootMultiplicity_dvd p x) (pow_rootMultiplicity_dvd q x) theorem rootMultiplicity_mul' {p q : R[X]} {x : R} (hpq : (p /ₘ (X - C x) ^ p.rootMultiplicity x).eval x * (q /ₘ (X - C x) ^ q.rootMultiplicity x).eval x ≠ 0) : rootMultiplicity x (p * q) = rootMultiplicity x p + rootMultiplicity x q := by simp_rw [eval_divByMonic_eq_trailingCoeff_comp] at hpq simp_rw [rootMultiplicity_eq_natTrailingDegree, mul_comp, natTrailingDegree_mul' hpq] variable [IsDomain R] {p q : R[X]} @[simp] theorem natDegree_coe_units (u : R[X]ˣ) : natDegree (u : R[X]) = 0 := natDegree_eq_of_degree_eq_some (degree_coe_units u) theorem coeff_coe_units_zero_ne_zero (u : R[X]ˣ) : coeff (u : R[X]) 0 ≠ 0 := by conv in 0 => rw [← natDegree_coe_units u] rw [← leadingCoeff, Ne, leadingCoeff_eq_zero] exact Units.ne_zero _ theorem degree_eq_degree_of_associated (h : Associated p q) : degree p = degree q := by let ⟨u, hu⟩ := h simp [hu.symm] theorem 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] /-- Division by a monic polynomial doesn't change the leading coefficient. -/ theorem leadingCoeff_divByMonic_of_monic {R : Type u} [CommRing R] {p q : R[X]} (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 theorem leadingCoeff_divByMonic_X_sub_C (p : R[X]) (hp : degree p ≠ 0) (a : R) : leadingCoeff (p /ₘ (X - C a)) = leadingCoeff p := by nontriviality cases' hp.lt_or_lt 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] theorem 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 by_cases hq : q = 0 · rwa [hq, leadingCoeff_zero, leadingCoeff_eq_zero, ← hq] at h₂ replace h₁ := (natDegree_le_of_dvd hpq hq).antisymm h₁ obtain ⟨u, rfl⟩ := hpq replace hq := mul_ne_zero_iff.mp hq rw [natDegree_mul hq.1 hq.2, self_eq_add_right] 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] theorem 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)⟩ theorem 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)) theorem 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)) theorem eq_leadingCoeff_mul_of_monic_of_dvd_of_natDegree_le {R} [CommRing 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) theorem eq_of_monic_of_dvd_of_natDegree_le {R} [CommRing 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] theorem prime_X_sub_C (r : R) : Prime (X - C r) := ⟨X_sub_C_ne_zero r, not_isUnit_X_sub_C r, fun _ _ => by simp_rw [dvd_iff_isRoot, IsRoot.def, eval_mul, mul_eq_zero] exact id⟩ theorem prime_X : Prime (X : R[X]) := by convert prime_X_sub_C (0 : R) simp theorem Monic.prime_of_degree_eq_one (hp1 : degree p = 1) (hm : Monic p) : Prime p := have : p = X - C (-p.coeff 0) := by simpa [hm.leadingCoeff] using eq_X_add_C_of_degree_eq_one hp1 this.symm ▸ prime_X_sub_C _ theorem irreducible_X_sub_C (r : R) : Irreducible (X - C r) := (prime_X_sub_C r).irreducible theorem irreducible_X : Irreducible (X : R[X]) := Prime.irreducible prime_X theorem Monic.irreducible_of_degree_eq_one (hp1 : degree p = 1) (hm : Monic p) : Irreducible p := (hm.prime_of_degree_eq_one hp1).irreducible @[simp] theorem natDegree_multiset_prod_X_sub_C_eq_card (s : Multiset R) : (s.map fun a => X - C a).prod.natDegree = Multiset.card s := by rw [natDegree_multiset_prod_of_monic, Multiset.map_map] · simp only [(· ∘ ·), natDegree_X_sub_C, Multiset.map_const', Multiset.sum_replicate, smul_eq_mul, mul_one] · exact Multiset.forall_mem_map_iff.2 fun a _ => monic_X_sub_C a theorem Monic.comp (hp : p.Monic) (hq : q.Monic) (h : q.natDegree ≠ 0) : (p.comp q).Monic := by rw [Monic.def, leadingCoeff_comp h, Monic.def.1 hp, Monic.def.1 hq, one_pow, one_mul] theorem Monic.comp_X_add_C (hp : p.Monic) (r : R) : (p.comp (X + C r)).Monic := by refine hp.comp (monic_X_add_C _) fun ha => ?_ rw [natDegree_X_add_C] at ha exact one_ne_zero ha theorem Monic.comp_X_sub_C (hp : p.Monic) (r : R) : (p.comp (X - C r)).Monic := by simpa using hp.comp_X_add_C (-r) theorem 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)] theorem comp_eq_zero_iff : p.comp q = 0 ↔ p = 0 ∨ p.eval (q.coeff 0) = 0 ∧ q = C (q.coeff 0) := by constructor · intro h have key : p.natDegree = 0 ∨ q.natDegree = 0 := by rw [← mul_eq_zero, ← natDegree_comp, h, natDegree_zero] replace key := Or.imp eq_C_of_natDegree_eq_zero eq_C_of_natDegree_eq_zero key cases' key with key key · rw [key, C_comp] at h exact Or.inl (key.trans h) · rw [key, comp_C, C_eq_zero] at h exact Or.inr ⟨h, key⟩ · exact fun h => Or.rec (fun h => by rw [h, zero_comp]) (fun h => by rw [h.2, comp_C, h.1, C_0]) h lemma aeval_ne_zero_of_isCoprime [CommSemiring R] [Nontrivial S] [Semiring S] [Algebra R S] {p q : R[X]} (h : IsCoprime p q) (s : S) : aeval s p ≠ 0 ∨ aeval s q ≠ 0 := by by_contra! hpq rcases h with ⟨_, _, h⟩ apply_fun aeval s at h simp only [map_add, map_mul, map_one, hpq.left, hpq.right, mul_zero, add_zero, zero_ne_one] at h theorem isCoprime_X_sub_C_of_isUnit_sub {R} [CommRing R] {a b : R} (h : IsUnit (a - b)) : IsCoprime (X - C a) (X - C b) := ⟨-C h.unit⁻¹.val, C h.unit⁻¹.val, by rw [neg_mul_comm, ← left_distrib, neg_add_eq_sub, sub_sub_sub_cancel_left, ← C_sub, ← C_mul] rw [← C_1] congr exact h.val_inv_mul⟩ theorem pairwise_coprime_X_sub_C {K} [Field K] {I : Type v} {s : I → K} (H : Function.Injective s) : Pairwise (IsCoprime on fun i : I => X - C (s i)) := fun _ _ hij => isCoprime_X_sub_C_of_isUnit_sub (sub_ne_zero_of_ne <| H.ne hij).isUnit theorem rootMultiplicity_mul {p q : R[X]} {x : R} (hpq : p * q ≠ 0) : rootMultiplicity x (p * q) = rootMultiplicity x p + rootMultiplicity x q := by classical have hp : p ≠ 0 := left_ne_zero_of_mul hpq have hq : q ≠ 0 := right_ne_zero_of_mul hpq rw [rootMultiplicity_eq_multiplicity (p * q), dif_neg hpq, rootMultiplicity_eq_multiplicity p, dif_neg hp, rootMultiplicity_eq_multiplicity q, dif_neg hq, multiplicity.mul' (prime_X_sub_C x)] open Multiset in theorem exists_multiset_roots [DecidableEq R] : ∀ {p : R[X]} (_ : p ≠ 0), ∃ s : Multiset R, (Multiset.card s : WithBot ℕ) ≤ degree p ∧ ∀ a, s.count a = rootMultiplicity a p | p, hp => haveI := Classical.propDecidable (∃ x, IsRoot p x) if h : ∃ x, IsRoot p x then let ⟨x, hx⟩ := h have hpd : 0 < degree p := degree_pos_of_root hp hx have hd0 : p /ₘ (X - C x) ≠ 0 := fun h => by rw [← mul_divByMonic_eq_iff_isRoot.2 hx, h, mul_zero] at hp; exact hp rfl have wf : degree (p /ₘ (X - C x)) < degree p := degree_divByMonic_lt _ (monic_X_sub_C x) hp ((degree_X_sub_C x).symm ▸ by decide) let ⟨t, htd, htr⟩ := @exists_multiset_roots _ (p /ₘ (X - C x)) hd0 have hdeg : degree (X - C x) ≤ degree p := by rw [degree_X_sub_C, degree_eq_natDegree hp] rw [degree_eq_natDegree hp] at hpd exact WithBot.coe_le_coe.2 (WithBot.coe_lt_coe.1 hpd) have hdiv0 : p /ₘ (X - C x) ≠ 0 := mt (divByMonic_eq_zero_iff (monic_X_sub_C x)).1 <| not_lt.2 hdeg ⟨x ::ₘ t, calc (card (x ::ₘ t) : WithBot ℕ) = Multiset.card t + 1 := by congr exact mod_cast Multiset.card_cons _ _ _ ≤ degree p := by rw [← degree_add_divByMonic (monic_X_sub_C x) hdeg, degree_X_sub_C, add_comm] exact add_le_add (le_refl (1 : WithBot ℕ)) htd, by change ∀ (a : R), count a (x ::ₘ t) = rootMultiplicity a p intro a conv_rhs => rw [← mul_divByMonic_eq_iff_isRoot.mpr hx] rw [rootMultiplicity_mul (mul_ne_zero (X_sub_C_ne_zero x) hdiv0), rootMultiplicity_X_sub_C, ← htr a] split_ifs with ha · rw [ha, count_cons_self, add_comm] · rw [count_cons_of_ne ha, zero_add]⟩ else ⟨0, (degree_eq_natDegree hp).symm ▸ WithBot.coe_le_coe.2 (Nat.zero_le _), by intro a rw [count_zero, rootMultiplicity_eq_zero (not_exists.mp h a)]⟩ termination_by p => natDegree p decreasing_by { simp_wf apply (Nat.cast_lt (α := WithBot ℕ)).mp simp only [degree_eq_natDegree hp, degree_eq_natDegree hd0] at wf assumption} end CommRing section variable [Semiring R] [CommRing S] [IsDomain S] (φ : R →+* S) theorem isUnit_of_isUnit_leadingCoeff_of_isUnit_map {f : R[X]} (hf : IsUnit f.leadingCoeff) (H : IsUnit (map φ f)) : IsUnit f := by have dz := degree_eq_zero_of_isUnit H rw [degree_map_eq_of_leadingCoeff_ne_zero] at dz · rw [eq_C_of_degree_eq_zero dz] refine IsUnit.map C ?_ convert hf change coeff f 0 = coeff f (natDegree f) rw [(degree_eq_iff_natDegree_eq _).1 dz] · rfl rintro rfl simp at H · intro h have u : IsUnit (φ f.leadingCoeff) := IsUnit.map φ hf rw [h] at u simp at u end section variable [CommRing R] [IsDomain R] [CommRing S] [IsDomain S] (φ : R →+* S) /-- A polynomial over an integral domain `R` is irreducible if it is monic and irreducible after mapping into an integral domain `S`. A special case of this lemma is that a polynomial over `ℤ` is irreducible if it is monic and irreducible over `ℤ/pℤ` for some prime `p`. -/ theorem Monic.irreducible_of_irreducible_map (f : R[X]) (h_mon : Monic f) (h_irr : Irreducible (Polynomial.map φ f)) : Irreducible f := by refine ⟨h_irr.not_unit ∘ IsUnit.map (mapRingHom φ), fun a b h => ?_⟩ dsimp [Monic] at h_mon have q := (leadingCoeff_mul a b).symm rw [← h, h_mon] at q refine (h_irr.isUnit_or_isUnit <| (congr_arg (Polynomial.map φ) h).trans (Polynomial.map_mul φ)).imp ?_ ?_ <;> apply isUnit_of_isUnit_leadingCoeff_of_isUnit_map <;> apply isUnit_of_mul_eq_one · exact q · rw [mul_comm] exact q end end Polynomial
Algebra\Polynomial\Roots.lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker, Johan Commelin -/ import Mathlib.Algebra.Polynomial.RingDivision import Mathlib.RingTheory.Localization.FractionRing /-! # Theory of univariate polynomials We define the multiset of roots of a polynomial, and prove basic results about it. ## Main definitions * `Polynomial.roots p`: The multiset containing all the roots of `p`, including their multiplicities. * `Polynomial.rootSet p E`: The set of distinct roots of `p` in an algebra `E`. ## Main statements * `Polynomial.C_leadingCoeff_mul_prod_multiset_X_sub_C`: If a polynomial has as many roots as its degree, it can be written as the product of its leading coefficient with `∏ (X - a)` where `a` ranges through its roots. -/ noncomputable section namespace Polynomial universe u v w z variable {R : Type u} {S : Type v} {T : Type w} {a b : R} {n : ℕ} section CommRing variable [CommRing R] [IsDomain R] {p q : R[X]} section Roots open Multiset Finset /-- `roots p` noncomputably gives a multiset containing all the roots of `p`, including their multiplicities. -/ noncomputable def roots (p : R[X]) : Multiset R := haveI := Classical.decEq R haveI := Classical.dec (p = 0) if h : p = 0 then ∅ else Classical.choose (exists_multiset_roots h) theorem roots_def [DecidableEq R] (p : R[X]) [Decidable (p = 0)] : p.roots = if h : p = 0 then ∅ else Classical.choose (exists_multiset_roots h) := by -- porting noteL `‹_›` doesn't work for instance arguments rename_i iR ip0 obtain rfl := Subsingleton.elim iR (Classical.decEq R) obtain rfl := Subsingleton.elim ip0 (Classical.dec (p = 0)) rfl @[simp] theorem roots_zero : (0 : R[X]).roots = 0 := dif_pos rfl theorem card_roots (hp0 : p ≠ 0) : (Multiset.card (roots p) : WithBot ℕ) ≤ degree p := by classical unfold roots rw [dif_neg hp0] exact (Classical.choose_spec (exists_multiset_roots hp0)).1 theorem card_roots' (p : R[X]) : Multiset.card p.roots ≤ natDegree p := by by_cases hp0 : p = 0 · simp [hp0] exact WithBot.coe_le_coe.1 (le_trans (card_roots hp0) (le_of_eq <| degree_eq_natDegree hp0)) theorem card_roots_sub_C {p : R[X]} {a : R} (hp0 : 0 < degree p) : (Multiset.card (p - C a).roots : WithBot ℕ) ≤ degree p := calc (Multiset.card (p - C a).roots : WithBot ℕ) ≤ degree (p - C a) := card_roots <| mt sub_eq_zero.1 fun h => not_le_of_gt hp0 <| h.symm ▸ degree_C_le _ = degree p := by rw [sub_eq_add_neg, ← C_neg]; exact degree_add_C hp0 theorem card_roots_sub_C' {p : R[X]} {a : R} (hp0 : 0 < degree p) : Multiset.card (p - C a).roots ≤ natDegree p := WithBot.coe_le_coe.1 (le_trans (card_roots_sub_C hp0) (le_of_eq <| degree_eq_natDegree fun h => by simp_all [lt_irrefl])) @[simp] theorem count_roots [DecidableEq R] (p : R[X]) : p.roots.count a = rootMultiplicity a p := by classical by_cases hp : p = 0 · simp [hp] rw [roots_def, dif_neg hp] exact (Classical.choose_spec (exists_multiset_roots hp)).2 a @[simp] theorem mem_roots' : a ∈ p.roots ↔ p ≠ 0 ∧ IsRoot p a := by classical rw [← count_pos, count_roots p, rootMultiplicity_pos'] theorem mem_roots (hp : p ≠ 0) : a ∈ p.roots ↔ IsRoot p a := mem_roots'.trans <| and_iff_right hp theorem ne_zero_of_mem_roots (h : a ∈ p.roots) : p ≠ 0 := (mem_roots'.1 h).1 theorem isRoot_of_mem_roots (h : a ∈ p.roots) : IsRoot p a := (mem_roots'.1 h).2 -- Porting note: added during port. lemma mem_roots_iff_aeval_eq_zero {x : R} (w : p ≠ 0) : x ∈ roots p ↔ aeval x p = 0 := by rw [mem_roots w, IsRoot.def, aeval_def, eval₂_eq_eval_map] simp theorem card_le_degree_of_subset_roots {p : R[X]} {Z : Finset R} (h : Z.val ⊆ p.roots) : Z.card ≤ p.natDegree := (Multiset.card_le_card (Finset.val_le_iff_val_subset.2 h)).trans (Polynomial.card_roots' p) theorem finite_setOf_isRoot {p : R[X]} (hp : p ≠ 0) : Set.Finite { x | IsRoot p x } := by classical simpa only [← Finset.setOf_mem, Multiset.mem_toFinset, mem_roots hp] using p.roots.toFinset.finite_toSet theorem eq_zero_of_infinite_isRoot (p : R[X]) (h : Set.Infinite { x | IsRoot p x }) : p = 0 := not_imp_comm.mp finite_setOf_isRoot h theorem exists_max_root [LinearOrder R] (p : R[X]) (hp : p ≠ 0) : ∃ x₀, ∀ x, p.IsRoot x → x ≤ x₀ := Set.exists_upper_bound_image _ _ <| finite_setOf_isRoot hp theorem exists_min_root [LinearOrder R] (p : R[X]) (hp : p ≠ 0) : ∃ x₀, ∀ x, p.IsRoot x → x₀ ≤ x := Set.exists_lower_bound_image _ _ <| finite_setOf_isRoot hp theorem eq_of_infinite_eval_eq (p q : R[X]) (h : Set.Infinite { x | eval x p = eval x q }) : p = q := by rw [← sub_eq_zero] apply eq_zero_of_infinite_isRoot simpa only [IsRoot, eval_sub, sub_eq_zero] theorem roots_mul {p q : R[X]} (hpq : p * q ≠ 0) : (p * q).roots = p.roots + q.roots := by classical exact Multiset.ext.mpr fun r => by rw [count_add, count_roots, count_roots, count_roots, rootMultiplicity_mul hpq] theorem roots.le_of_dvd (h : q ≠ 0) : p ∣ q → roots p ≤ roots q := by rintro ⟨k, rfl⟩ exact Multiset.le_iff_exists_add.mpr ⟨k.roots, roots_mul h⟩ theorem mem_roots_sub_C' {p : R[X]} {a x : R} : x ∈ (p - C a).roots ↔ p ≠ C a ∧ p.eval x = a := by rw [mem_roots', IsRoot.def, sub_ne_zero, eval_sub, sub_eq_zero, eval_C] theorem mem_roots_sub_C {p : R[X]} {a x : R} (hp0 : 0 < degree p) : x ∈ (p - C a).roots ↔ p.eval x = a := mem_roots_sub_C'.trans <| and_iff_right fun hp => hp0.not_le <| hp.symm ▸ degree_C_le @[simp] theorem roots_X_sub_C (r : R) : roots (X - C r) = {r} := by classical ext s rw [count_roots, rootMultiplicity_X_sub_C, count_singleton] @[simp] theorem roots_X : roots (X : R[X]) = {0} := by rw [← roots_X_sub_C, C_0, sub_zero] @[simp] theorem roots_C (x : R) : (C x).roots = 0 := by classical exact if H : x = 0 then by rw [H, C_0, roots_zero] else Multiset.ext.mpr fun r => (by rw [count_roots, count_zero, rootMultiplicity_eq_zero (not_isRoot_C _ _ H)]) @[simp] theorem roots_one : (1 : R[X]).roots = ∅ := roots_C 1 @[simp] theorem roots_C_mul (p : R[X]) (ha : a ≠ 0) : (C a * p).roots = p.roots := by by_cases hp : p = 0 <;> simp only [roots_mul, *, Ne, mul_eq_zero, C_eq_zero, or_self_iff, not_false_iff, roots_C, zero_add, mul_zero] @[simp] theorem roots_smul_nonzero (p : R[X]) (ha : a ≠ 0) : (a • p).roots = p.roots := by rw [smul_eq_C_mul, roots_C_mul _ ha] @[simp] lemma roots_neg (p : R[X]) : (-p).roots = p.roots := by rw [← neg_one_smul R p, roots_smul_nonzero p (neg_ne_zero.mpr one_ne_zero)] theorem roots_list_prod (L : List R[X]) : (0 : R[X]) ∉ L → L.prod.roots = (L : Multiset R[X]).bind roots := List.recOn L (fun _ => roots_one) fun hd tl ih H => by rw [List.mem_cons, not_or] at H rw [List.prod_cons, roots_mul (mul_ne_zero (Ne.symm H.1) <| List.prod_ne_zero H.2), ← Multiset.cons_coe, Multiset.cons_bind, ih H.2] theorem roots_multiset_prod (m : Multiset R[X]) : (0 : R[X]) ∉ m → m.prod.roots = m.bind roots := by rcases m with ⟨L⟩ simpa only [Multiset.prod_coe, quot_mk_to_coe''] using roots_list_prod L theorem roots_prod {ι : Type*} (f : ι → R[X]) (s : Finset ι) : s.prod f ≠ 0 → (s.prod f).roots = s.val.bind fun i => roots (f i) := by rcases s with ⟨m, hm⟩ simpa [Multiset.prod_eq_zero_iff, Multiset.bind_map] using roots_multiset_prod (m.map f) @[simp] theorem roots_pow (p : R[X]) (n : ℕ) : (p ^ n).roots = n • p.roots := by induction' n with n ihn · rw [pow_zero, roots_one, zero_smul, empty_eq_zero] · rcases eq_or_ne p 0 with (rfl | hp) · rw [zero_pow n.succ_ne_zero, roots_zero, smul_zero] · rw [pow_succ, roots_mul (mul_ne_zero (pow_ne_zero _ hp) hp), ihn, add_smul, one_smul] theorem roots_X_pow (n : ℕ) : (X ^ n : R[X]).roots = n • ({0} : Multiset R) := by rw [roots_pow, roots_X] theorem roots_C_mul_X_pow (ha : a ≠ 0) (n : ℕ) : Polynomial.roots (C a * X ^ n) = n • ({0} : Multiset R) := by rw [roots_C_mul _ ha, roots_X_pow] @[simp] theorem roots_monomial (ha : a ≠ 0) (n : ℕ) : (monomial n a).roots = n • ({0} : Multiset R) := by rw [← C_mul_X_pow_eq_monomial, roots_C_mul_X_pow ha] theorem roots_prod_X_sub_C (s : Finset R) : (s.prod fun a => X - C a).roots = s.val := by apply (roots_prod (fun a => X - C a) s ?_).trans · simp_rw [roots_X_sub_C] rw [Multiset.bind_singleton, Multiset.map_id'] · refine prod_ne_zero_iff.mpr (fun a _ => X_sub_C_ne_zero a) @[simp] theorem roots_multiset_prod_X_sub_C (s : Multiset R) : (s.map fun a => X - C a).prod.roots = s := by rw [roots_multiset_prod, Multiset.bind_map] · simp_rw [roots_X_sub_C] rw [Multiset.bind_singleton, Multiset.map_id'] · rw [Multiset.mem_map] rintro ⟨a, -, h⟩ exact X_sub_C_ne_zero a h theorem card_roots_X_pow_sub_C {n : ℕ} (hn : 0 < n) (a : R) : Multiset.card (roots ((X : R[X]) ^ n - C a)) ≤ n := WithBot.coe_le_coe.1 <| calc (Multiset.card (roots ((X : R[X]) ^ n - C a)) : WithBot ℕ) ≤ degree ((X : R[X]) ^ n - C a) := card_roots (X_pow_sub_C_ne_zero hn a) _ = n := degree_X_pow_sub_C hn a section NthRoots /-- `nthRoots n a` noncomputably returns the solutions to `x ^ n = a`-/ def nthRoots (n : ℕ) (a : R) : Multiset R := roots ((X : R[X]) ^ n - C a) @[simp] theorem mem_nthRoots {n : ℕ} (hn : 0 < n) {a x : R} : x ∈ nthRoots n a ↔ x ^ n = a := by rw [nthRoots, mem_roots (X_pow_sub_C_ne_zero hn a), IsRoot.def, eval_sub, eval_C, eval_pow, eval_X, sub_eq_zero] @[simp] theorem nthRoots_zero (r : R) : nthRoots 0 r = 0 := by simp only [empty_eq_zero, pow_zero, nthRoots, ← C_1, ← C_sub, roots_C] @[simp] theorem nthRoots_zero_right {R} [CommRing R] [IsDomain R] (n : ℕ) : nthRoots n (0 : R) = Multiset.replicate n 0 := by rw [nthRoots, C.map_zero, sub_zero, roots_pow, roots_X, Multiset.nsmul_singleton] theorem card_nthRoots (n : ℕ) (a : R) : Multiset.card (nthRoots n a) ≤ n := by classical exact (if hn : n = 0 then if h : (X : R[X]) ^ n - C a = 0 then by simp [Nat.zero_le, nthRoots, roots, h, dif_pos rfl, empty_eq_zero, Multiset.card_zero] else WithBot.coe_le_coe.1 (le_trans (card_roots h) (by rw [hn, pow_zero, ← C_1, ← RingHom.map_sub] exact degree_C_le)) else by rw [← Nat.cast_le (α := WithBot ℕ)] rw [← degree_X_pow_sub_C (Nat.pos_of_ne_zero hn) a] exact card_roots (X_pow_sub_C_ne_zero (Nat.pos_of_ne_zero hn) a)) @[simp] theorem nthRoots_two_eq_zero_iff {r : R} : nthRoots 2 r = 0 ↔ ¬IsSquare r := by simp_rw [isSquare_iff_exists_sq, eq_zero_iff_forall_not_mem, mem_nthRoots (by norm_num : 0 < 2), ← not_exists, eq_comm] /-- The multiset `nthRoots ↑n (1 : R)` as a Finset. -/ def nthRootsFinset (n : ℕ) (R : Type*) [CommRing R] [IsDomain R] : Finset R := haveI := Classical.decEq R Multiset.toFinset (nthRoots n (1 : R)) lemma nthRootsFinset_def (n : ℕ) (R : Type*) [CommRing R] [IsDomain R] [DecidableEq R] : nthRootsFinset n R = Multiset.toFinset (nthRoots n (1 : R)) := by unfold nthRootsFinset convert rfl @[simp] theorem mem_nthRootsFinset {n : ℕ} (h : 0 < n) {x : R} : x ∈ nthRootsFinset n R ↔ x ^ (n : ℕ) = 1 := by classical rw [nthRootsFinset_def, mem_toFinset, mem_nthRoots h] @[simp] theorem nthRootsFinset_zero : nthRootsFinset 0 R = ∅ := by classical simp [nthRootsFinset_def] theorem mul_mem_nthRootsFinset {η₁ η₂ : R} (hη₁ : η₁ ∈ nthRootsFinset n R) (hη₂ : η₂ ∈ nthRootsFinset n R) : η₁ * η₂ ∈ nthRootsFinset n R := by cases n with | zero => simp only [Nat.zero_eq, nthRootsFinset_zero, not_mem_empty] at hη₁ | succ n => rw [mem_nthRootsFinset n.succ_pos] at hη₁ hη₂ ⊢ rw [mul_pow, hη₁, hη₂, one_mul] theorem ne_zero_of_mem_nthRootsFinset {η : R} (hη : η ∈ nthRootsFinset n R) : η ≠ 0 := by nontriviality R rintro rfl cases n with | zero => simp only [Nat.zero_eq, nthRootsFinset_zero, not_mem_empty] at hη | succ n => rw [mem_nthRootsFinset n.succ_pos, zero_pow n.succ_ne_zero] at hη exact zero_ne_one hη theorem one_mem_nthRootsFinset (hn : 0 < n) : 1 ∈ nthRootsFinset n R := by rw [mem_nthRootsFinset hn, one_pow] end NthRoots theorem zero_of_eval_zero [Infinite R] (p : R[X]) (h : ∀ x, p.eval x = 0) : p = 0 := by classical by_contra hp refine @Fintype.false R _ ?_ exact ⟨p.roots.toFinset, fun x => Multiset.mem_toFinset.mpr ((mem_roots hp).mpr (h _))⟩ theorem funext [Infinite R] {p q : R[X]} (ext : ∀ r : R, p.eval r = q.eval r) : p = q := by rw [← sub_eq_zero] apply zero_of_eval_zero intro x rw [eval_sub, sub_eq_zero, ext] variable [CommRing T] /-- Given a polynomial `p` with coefficients in a ring `T` and a `T`-algebra `S`, `aroots p S` is the multiset of roots of `p` regarded as a polynomial over `S`. -/ noncomputable abbrev aroots (p : T[X]) (S) [CommRing S] [IsDomain S] [Algebra T S] : Multiset S := (p.map (algebraMap T S)).roots theorem aroots_def (p : T[X]) (S) [CommRing S] [IsDomain S] [Algebra T S] : p.aroots S = (p.map (algebraMap T S)).roots := rfl theorem mem_aroots' [CommRing S] [IsDomain S] [Algebra T S] {p : T[X]} {a : S} : a ∈ p.aroots S ↔ p.map (algebraMap T S) ≠ 0 ∧ aeval a p = 0 := by rw [mem_roots', IsRoot.def, ← eval₂_eq_eval_map, aeval_def] theorem mem_aroots [CommRing S] [IsDomain S] [Algebra T S] [NoZeroSMulDivisors T S] {p : T[X]} {a : S} : a ∈ p.aroots S ↔ p ≠ 0 ∧ aeval a p = 0 := by rw [mem_aroots', Polynomial.map_ne_zero_iff] exact NoZeroSMulDivisors.algebraMap_injective T S theorem aroots_mul [CommRing S] [IsDomain S] [Algebra T S] [NoZeroSMulDivisors T S] {p q : T[X]} (hpq : p * q ≠ 0) : (p * q).aroots S = p.aroots S + q.aroots S := by suffices map (algebraMap T S) p * map (algebraMap T S) q ≠ 0 by rw [aroots_def, Polynomial.map_mul, roots_mul this] rwa [← Polynomial.map_mul, Polynomial.map_ne_zero_iff (NoZeroSMulDivisors.algebraMap_injective T S)] @[simp] theorem aroots_X_sub_C [CommRing S] [IsDomain S] [Algebra T S] (r : T) : aroots (X - C r) S = {algebraMap T S r} := by rw [aroots_def, Polynomial.map_sub, map_X, map_C, roots_X_sub_C] @[simp] theorem aroots_X [CommRing S] [IsDomain S] [Algebra T S] : aroots (X : T[X]) S = {0} := by rw [aroots_def, map_X, roots_X] @[simp] theorem aroots_C [CommRing S] [IsDomain S] [Algebra T S] (a : T) : (C a).aroots S = 0 := by rw [aroots_def, map_C, roots_C] @[simp] theorem aroots_zero (S) [CommRing S] [IsDomain S] [Algebra T S] : (0 : T[X]).aroots S = 0 := by rw [← C_0, aroots_C] @[simp] theorem aroots_one [CommRing S] [IsDomain S] [Algebra T S] : (1 : T[X]).aroots S = 0 := aroots_C 1 @[simp] theorem aroots_neg [CommRing S] [IsDomain S] [Algebra T S] (p : T[X]) : (-p).aroots S = p.aroots S := by rw [aroots, Polynomial.map_neg, roots_neg] @[simp] theorem aroots_C_mul [CommRing S] [IsDomain S] [Algebra T S] [NoZeroSMulDivisors T S] {a : T} (p : T[X]) (ha : a ≠ 0) : (C a * p).aroots S = p.aroots S := by rw [aroots_def, Polynomial.map_mul, map_C, roots_C_mul] rwa [map_ne_zero_iff] exact NoZeroSMulDivisors.algebraMap_injective T S @[simp] theorem aroots_smul_nonzero [CommRing S] [IsDomain S] [Algebra T S] [NoZeroSMulDivisors T S] {a : T} (p : T[X]) (ha : a ≠ 0) : (a • p).aroots S = p.aroots S := by rw [smul_eq_C_mul, aroots_C_mul _ ha] @[simp] theorem aroots_pow [CommRing S] [IsDomain S] [Algebra T S] (p : T[X]) (n : ℕ) : (p ^ n).aroots S = n • p.aroots S := by rw [aroots_def, Polynomial.map_pow, roots_pow] theorem aroots_X_pow [CommRing S] [IsDomain S] [Algebra T S] (n : ℕ) : (X ^ n : T[X]).aroots S = n • ({0} : Multiset S) := by rw [aroots_pow, aroots_X] theorem aroots_C_mul_X_pow [CommRing S] [IsDomain S] [Algebra T S] [NoZeroSMulDivisors T S] {a : T} (ha : a ≠ 0) (n : ℕ) : (C a * X ^ n : T[X]).aroots S = n • ({0} : Multiset S) := by rw [aroots_C_mul _ ha, aroots_X_pow] @[simp] theorem aroots_monomial [CommRing S] [IsDomain S] [Algebra T S] [NoZeroSMulDivisors T S] {a : T} (ha : a ≠ 0) (n : ℕ) : (monomial n a).aroots S = n • ({0} : Multiset S) := by rw [← C_mul_X_pow_eq_monomial, aroots_C_mul_X_pow ha] /-- The set of distinct roots of `p` in `S`. If you have a non-separable polynomial, use `Polynomial.aroots` for the multiset where multiple roots have the appropriate multiplicity. -/ def rootSet (p : T[X]) (S) [CommRing S] [IsDomain S] [Algebra T S] : Set S := haveI := Classical.decEq S (p.aroots S).toFinset theorem rootSet_def (p : T[X]) (S) [CommRing S] [IsDomain S] [Algebra T S] [DecidableEq S] : p.rootSet S = (p.aroots S).toFinset := by rw [rootSet] convert rfl @[simp] theorem rootSet_C [CommRing S] [IsDomain S] [Algebra T S] (a : T) : (C a).rootSet S = ∅ := by classical rw [rootSet_def, aroots_C, Multiset.toFinset_zero, Finset.coe_empty] @[simp] theorem rootSet_zero (S) [CommRing S] [IsDomain S] [Algebra T S] : (0 : T[X]).rootSet S = ∅ := by rw [← C_0, rootSet_C] @[simp] theorem rootSet_one (S) [CommRing S] [IsDomain S] [Algebra T S] : (1 : T[X]).rootSet S = ∅ := by rw [← C_1, rootSet_C] @[simp] theorem rootSet_neg (p : T[X]) (S) [CommRing S] [IsDomain S] [Algebra T S] : (-p).rootSet S = p.rootSet S := by rw [rootSet, aroots_neg, rootSet] instance rootSetFintype (p : T[X]) (S : Type*) [CommRing S] [IsDomain S] [Algebra T S] : Fintype (p.rootSet S) := FinsetCoe.fintype _ theorem rootSet_finite (p : T[X]) (S : Type*) [CommRing S] [IsDomain S] [Algebra T S] : (p.rootSet S).Finite := Set.toFinite _ /-- The set of roots of all polynomials of bounded degree and having coefficients in a finite set is finite. -/ theorem bUnion_roots_finite {R S : Type*} [Semiring R] [CommRing S] [IsDomain S] [DecidableEq S] (m : R →+* S) (d : ℕ) {U : Set R} (h : U.Finite) : (⋃ (f : R[X]) (_ : f.natDegree ≤ d ∧ ∀ i, f.coeff i ∈ U), ((f.map m).roots.toFinset.toSet : Set S)).Finite := Set.Finite.biUnion (by -- We prove that the set of polynomials under consideration is finite because its -- image by the injective map `π` is finite let π : R[X] → Fin (d + 1) → R := fun f i => f.coeff i refine ((Set.Finite.pi fun _ => h).subset <| ?_).of_finite_image (?_ : Set.InjOn π _) · exact Set.image_subset_iff.2 fun f hf i _ => hf.2 i · refine fun x hx y hy hxy => (ext_iff_natDegree_le hx.1 hy.1).2 fun i hi => ?_ exact id congr_fun hxy ⟨i, Nat.lt_succ_of_le hi⟩) fun i _ => Finset.finite_toSet _ theorem mem_rootSet' {p : T[X]} {S : Type*} [CommRing S] [IsDomain S] [Algebra T S] {a : S} : a ∈ p.rootSet S ↔ p.map (algebraMap T S) ≠ 0 ∧ aeval a p = 0 := by classical rw [rootSet_def, Finset.mem_coe, mem_toFinset, mem_aroots'] theorem mem_rootSet {p : T[X]} {S : Type*} [CommRing S] [IsDomain S] [Algebra T S] [NoZeroSMulDivisors T S] {a : S} : a ∈ p.rootSet S ↔ p ≠ 0 ∧ aeval a p = 0 := by rw [mem_rootSet', Polynomial.map_ne_zero_iff (NoZeroSMulDivisors.algebraMap_injective T S)] theorem mem_rootSet_of_ne {p : T[X]} {S : Type*} [CommRing S] [IsDomain S] [Algebra T S] [NoZeroSMulDivisors T S] (hp : p ≠ 0) {a : S} : a ∈ p.rootSet S ↔ aeval a p = 0 := mem_rootSet.trans <| and_iff_right hp theorem rootSet_maps_to' {p : T[X]} {S S'} [CommRing S] [IsDomain S] [Algebra T S] [CommRing S'] [IsDomain S'] [Algebra T S'] (hp : p.map (algebraMap T S') = 0 → p.map (algebraMap T S) = 0) (f : S →ₐ[T] S') : (p.rootSet S).MapsTo f (p.rootSet S') := fun x hx => by rw [mem_rootSet'] at hx ⊢ rw [aeval_algHom, AlgHom.comp_apply, hx.2, _root_.map_zero] exact ⟨mt hp hx.1, rfl⟩ theorem ne_zero_of_mem_rootSet {p : T[X]} [CommRing S] [IsDomain S] [Algebra T S] {a : S} (h : a ∈ p.rootSet S) : p ≠ 0 := fun hf => by rwa [hf, rootSet_zero] at h theorem aeval_eq_zero_of_mem_rootSet {p : T[X]} [CommRing S] [IsDomain S] [Algebra T S] {a : S} (hx : a ∈ p.rootSet S) : aeval a p = 0 := (mem_rootSet'.1 hx).2 theorem rootSet_mapsTo {p : T[X]} {S S'} [CommRing S] [IsDomain S] [Algebra T S] [CommRing S'] [IsDomain S'] [Algebra T S'] [NoZeroSMulDivisors T S'] (f : S →ₐ[T] S') : (p.rootSet S).MapsTo f (p.rootSet S') := by refine rootSet_maps_to' (fun h₀ => ?_) f obtain rfl : p = 0 := map_injective _ (NoZeroSMulDivisors.algebraMap_injective T S') (by rwa [Polynomial.map_zero]) exact Polynomial.map_zero _ end Roots lemma eq_zero_of_natDegree_lt_card_of_eval_eq_zero {R} [CommRing R] [IsDomain R] (p : R[X]) {ι} [Fintype ι] {f : ι → R} (hf : Function.Injective f) (heval : ∀ i, p.eval (f i) = 0) (hcard : natDegree p < Fintype.card ι) : p = 0 := by classical by_contra hp apply not_lt_of_le (le_refl (Finset.card p.roots.toFinset)) calc Finset.card p.roots.toFinset ≤ Multiset.card p.roots := Multiset.toFinset_card_le _ _ ≤ natDegree p := Polynomial.card_roots' p _ < Fintype.card ι := hcard _ = Fintype.card (Set.range f) := (Set.card_range_of_injective hf).symm _ = Finset.card (Finset.univ.image f) := by rw [← Set.toFinset_card, Set.toFinset_range] _ ≤ Finset.card p.roots.toFinset := Finset.card_mono ?_ intro _ simp only [Finset.mem_image, Finset.mem_univ, true_and, Multiset.mem_toFinset, mem_roots', ne_eq, IsRoot.def, forall_exists_index, hp, not_false_eq_true] rintro x rfl exact heval _ lemma eq_zero_of_natDegree_lt_card_of_eval_eq_zero' {R} [CommRing R] [IsDomain R] (p : R[X]) (s : Finset R) (heval : ∀ i ∈ s, p.eval i = 0) (hcard : natDegree p < s.card) : p = 0 := eq_zero_of_natDegree_lt_card_of_eval_eq_zero p Subtype.val_injective (fun i : s ↦ heval i i.prop) (hcard.trans_eq (Fintype.card_coe s).symm) open Cardinal in lemma eq_zero_of_forall_eval_zero_of_natDegree_lt_card (f : R[X]) (hf : ∀ r, f.eval r = 0) (hfR : f.natDegree < #R) : f = 0 := by obtain hR|hR := finite_or_infinite R · have := Fintype.ofFinite R apply eq_zero_of_natDegree_lt_card_of_eval_eq_zero f Function.injective_id hf simpa only [mk_fintype, Nat.cast_lt] using hfR · exact zero_of_eval_zero _ hf open Cardinal in lemma exists_eval_ne_zero_of_natDegree_lt_card (f : R[X]) (hf : f ≠ 0) (hfR : f.natDegree < #R) : ∃ r, f.eval r ≠ 0 := by contrapose! hf exact eq_zero_of_forall_eval_zero_of_natDegree_lt_card f hf hfR theorem monic_prod_multiset_X_sub_C : Monic (p.roots.map fun a => X - C a).prod := monic_multiset_prod_of_monic _ _ fun a _ => monic_X_sub_C a theorem prod_multiset_root_eq_finset_root [DecidableEq R] : (p.roots.map fun a => X - C a).prod = p.roots.toFinset.prod fun a => (X - C a) ^ rootMultiplicity a p := by simp only [count_roots, Finset.prod_multiset_map_count] /-- The product `∏ (X - a)` for `a` inside the multiset `p.roots` divides `p`. -/ theorem prod_multiset_X_sub_C_dvd (p : R[X]) : (p.roots.map fun a => X - C a).prod ∣ p := by classical rw [← map_dvd_map _ (IsFractionRing.injective R <| FractionRing R) monic_prod_multiset_X_sub_C] rw [prod_multiset_root_eq_finset_root, Polynomial.map_prod] refine Finset.prod_dvd_of_coprime (fun a _ b _ h => ?_) fun a _ => ?_ · simp_rw [Polynomial.map_pow, Polynomial.map_sub, map_C, map_X] exact (pairwise_coprime_X_sub_C (IsFractionRing.injective R <| FractionRing R) h).pow · exact Polynomial.map_dvd _ (pow_rootMultiplicity_dvd p a) /-- A Galois connection. -/ theorem _root_.Multiset.prod_X_sub_C_dvd_iff_le_roots {p : R[X]} (hp : p ≠ 0) (s : Multiset R) : (s.map fun a => X - C a).prod ∣ p ↔ s ≤ p.roots := by classical exact ⟨fun h => Multiset.le_iff_count.2 fun r => by rw [count_roots, le_rootMultiplicity_iff hp, ← Multiset.prod_replicate, ← Multiset.map_replicate fun a => X - C a, ← Multiset.filter_eq] exact (Multiset.prod_dvd_prod_of_le <| Multiset.map_le_map <| s.filter_le _).trans h, fun h => (Multiset.prod_dvd_prod_of_le <| Multiset.map_le_map h).trans p.prod_multiset_X_sub_C_dvd⟩ theorem exists_prod_multiset_X_sub_C_mul (p : R[X]) : ∃ q, (p.roots.map fun a => X - C a).prod * q = p ∧ Multiset.card p.roots + q.natDegree = p.natDegree ∧ q.roots = 0 := by obtain ⟨q, he⟩ := p.prod_multiset_X_sub_C_dvd use q, he.symm obtain rfl | hq := eq_or_ne q 0 · rw [mul_zero] at he subst he simp constructor · conv_rhs => rw [he] rw [monic_prod_multiset_X_sub_C.natDegree_mul' hq, natDegree_multiset_prod_X_sub_C_eq_card] · replace he := congr_arg roots he.symm rw [roots_mul, roots_multiset_prod_X_sub_C] at he exacts [add_right_eq_self.1 he, mul_ne_zero monic_prod_multiset_X_sub_C.ne_zero hq] /-- A polynomial `p` that has as many roots as its degree can be written `p = p.leadingCoeff * ∏(X - a)`, for `a` in `p.roots`. -/ theorem C_leadingCoeff_mul_prod_multiset_X_sub_C (hroots : Multiset.card p.roots = p.natDegree) : C p.leadingCoeff * (p.roots.map fun a => X - C a).prod = p := (eq_leadingCoeff_mul_of_monic_of_dvd_of_natDegree_le monic_prod_multiset_X_sub_C p.prod_multiset_X_sub_C_dvd ((natDegree_multiset_prod_X_sub_C_eq_card _).trans hroots).ge).symm /-- A monic polynomial `p` that has as many roots as its degree can be written `p = ∏(X - a)`, for `a` in `p.roots`. -/ theorem prod_multiset_X_sub_C_of_monic_of_roots_card_eq (hp : p.Monic) (hroots : Multiset.card p.roots = p.natDegree) : (p.roots.map fun a => X - C a).prod = p := by convert C_leadingCoeff_mul_prod_multiset_X_sub_C hroots rw [hp.leadingCoeff, C_1, one_mul] theorem Monic.isUnit_leadingCoeff_of_dvd {a p : R[X]} (hp : Monic p) (hap : a ∣ p) : IsUnit a.leadingCoeff := isUnit_of_dvd_one (by simpa only [hp.leadingCoeff] using leadingCoeff_dvd_leadingCoeff hap) /-- To check a monic polynomial is irreducible, it suffices to check only for divisors that have smaller degree. See also: `Polynomial.Monic.irreducible_iff_natDegree`. -/ theorem Monic.irreducible_iff_degree_lt {p : R[X]} (p_monic : Monic p) (p_1 : p ≠ 1) : Irreducible p ↔ ∀ q, degree q ≤ ↑(p.natDegree / 2) → q ∣ p → IsUnit q := by simp only [p_monic.irreducible_iff_lt_natDegree_lt p_1, Finset.mem_Ioc, and_imp, natDegree_pos_iff_degree_pos, natDegree_le_iff_degree_le] constructor · rintro h q deg_le dvd by_contra q_unit have := degree_pos_of_not_isUnit_of_dvd_monic p_monic q_unit dvd have hu := p_monic.isUnit_leadingCoeff_of_dvd dvd refine (h _ (monic_of_isUnit_leadingCoeff_inv_smul hu) ?_ ?_ (dvd_trans ?_ dvd)).elim · rwa [degree_smul_of_smul_regular _ (isSMulRegular_of_group _)] · rwa [degree_smul_of_smul_regular _ (isSMulRegular_of_group _)] · rw [Units.smul_def, Polynomial.smul_eq_C_mul, (isUnit_C.mpr (Units.isUnit _)).mul_left_dvd] · rintro h q _ deg_pos deg_le dvd exact deg_pos.ne' <| degree_eq_zero_of_isUnit (h q deg_le dvd) end CommRing section variable {A B : Type*} [CommRing A] [CommRing B] theorem le_rootMultiplicity_map {p : A[X]} {f : A →+* B} (hmap : map f p ≠ 0) (a : A) : rootMultiplicity a p ≤ rootMultiplicity (f a) (p.map f) := by rw [le_rootMultiplicity_iff hmap] refine _root_.trans ?_ ((mapRingHom f).map_dvd (pow_rootMultiplicity_dvd p a)) rw [map_pow, map_sub, coe_mapRingHom, map_X, map_C] theorem eq_rootMultiplicity_map {p : A[X]} {f : A →+* B} (hf : Function.Injective f) (a : A) : rootMultiplicity a p = rootMultiplicity (f a) (p.map f) := by by_cases hp0 : p = 0; · simp only [hp0, rootMultiplicity_zero, Polynomial.map_zero] apply le_antisymm (le_rootMultiplicity_map ((Polynomial.map_ne_zero_iff hf).mpr hp0) a) rw [le_rootMultiplicity_iff hp0, ← map_dvd_map f hf ((monic_X_sub_C a).pow _), Polynomial.map_pow, Polynomial.map_sub, map_X, map_C] apply pow_rootMultiplicity_dvd theorem count_map_roots [IsDomain A] [DecidableEq B] {p : A[X]} {f : A →+* B} (hmap : map f p ≠ 0) (b : B) : (p.roots.map f).count b ≤ rootMultiplicity b (p.map f) := by rw [le_rootMultiplicity_iff hmap, ← Multiset.prod_replicate, ← Multiset.map_replicate fun a => X - C a] rw [← Multiset.filter_eq] refine (Multiset.prod_dvd_prod_of_le <| Multiset.map_le_map <| Multiset.filter_le (Eq b) _).trans ?_ convert Polynomial.map_dvd f p.prod_multiset_X_sub_C_dvd simp only [Polynomial.map_multiset_prod, Multiset.map_map] congr; ext1 simp only [Function.comp_apply, Polynomial.map_sub, map_X, map_C] theorem count_map_roots_of_injective [IsDomain A] [DecidableEq B] (p : A[X]) {f : A →+* B} (hf : Function.Injective f) (b : B) : (p.roots.map f).count b ≤ rootMultiplicity b (p.map f) := by by_cases hp0 : p = 0 · simp only [hp0, roots_zero, Multiset.map_zero, Multiset.count_zero, Polynomial.map_zero, rootMultiplicity_zero, le_refl] · exact count_map_roots ((Polynomial.map_ne_zero_iff hf).mpr hp0) b theorem map_roots_le [IsDomain A] [IsDomain B] {p : A[X]} {f : A →+* B} (h : p.map f ≠ 0) : p.roots.map f ≤ (p.map f).roots := by classical exact Multiset.le_iff_count.2 fun b => by rw [count_roots] apply count_map_roots h theorem map_roots_le_of_injective [IsDomain A] [IsDomain B] (p : A[X]) {f : A →+* B} (hf : Function.Injective f) : p.roots.map f ≤ (p.map f).roots := by by_cases hp0 : p = 0 · simp only [hp0, roots_zero, Multiset.map_zero, Polynomial.map_zero, le_rfl] exact map_roots_le ((Polynomial.map_ne_zero_iff hf).mpr hp0) theorem card_roots_le_map [IsDomain A] [IsDomain B] {p : A[X]} {f : A →+* B} (h : p.map f ≠ 0) : Multiset.card p.roots ≤ Multiset.card (p.map f).roots := by rw [← p.roots.card_map f] exact Multiset.card_le_card (map_roots_le h) theorem card_roots_le_map_of_injective [IsDomain A] [IsDomain B] {p : A[X]} {f : A →+* B} (hf : Function.Injective f) : Multiset.card p.roots ≤ Multiset.card (p.map f).roots := by by_cases hp0 : p = 0 · simp only [hp0, roots_zero, Polynomial.map_zero, Multiset.card_zero, le_rfl] exact card_roots_le_map ((Polynomial.map_ne_zero_iff hf).mpr hp0) theorem roots_map_of_injective_of_card_eq_natDegree [IsDomain A] [IsDomain B] {p : A[X]} {f : A →+* B} (hf : Function.Injective f) (hroots : Multiset.card p.roots = p.natDegree) : p.roots.map f = (p.map f).roots := by apply Multiset.eq_of_le_of_card_le (map_roots_le_of_injective p hf) simpa only [Multiset.card_map, hroots] using (card_roots' _).trans (natDegree_map_le f p) end end Polynomial
Algebra\Polynomial\Smeval.lean
/- Copyright (c) 2023 Scott Carnahan. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Carnahan -/ import Mathlib.Algebra.Group.NatPowAssoc import Mathlib.Algebra.Polynomial.AlgebraMap /-! # 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₂_eq_smeval`, `leval_eq_smeval.linearMap`, `aeval = smeval.algebraMap`, 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₂_eq_smeval (R : Type*) [Semiring R] {S : Type*} [Semiring S] (f : R →+* S) (p : R[X]) (x : S) : letI : Module R S := RingHom.toModule f p.eval₂ f x = p.smeval x := by letI : Module R S := RingHom.toModule f rw [smeval_eq_sum, eval₂_eq_sum] rfl variable (R) @[simp] theorem smeval_zero : (0 : R[X]).smeval x = 0 := by simp only [smeval_eq_sum, smul_pow, sum_zero_index] @[simp] theorem smeval_one : (1 : R[X]).smeval x = 1 • x ^ 0 := by rw [← C_1, smeval_C] simp only [Nat.cast_one, 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, smul_pow] 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 n ih · simp only [smeval_zero, Nat.cast_zero, Nat.zero_eq, zero_smul] · rw [n.cast_succ, smeval_add, ih, smeval_one, ← add_nsmul] @[deprecated (since := "2024-04-17")] alias smeval_nat_cast := smeval_natCast @[simp] theorem smeval_smul (r : R) : (r • p).smeval x = r • p.smeval x := by induction p using Polynomial.induction_on' with | h_add p q ph qh => rw [smul_add, smeval_add, ph, qh, ← smul_add, smeval_add] | h_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, smul_eq_mul, 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 [Function.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, add_left_neg, 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, Int.cast_sum, Int.cast_mul, Int.cast_npow] refine Finset.sum_congr rfl ?_ intro k _ rw [show -(n : S) = (-n : ℤ) by simp only [Int.cast_neg, Int.cast_natCast], nsmul_eq_mul, ← AddGroupWithOne.intCast_ofNat, ← Int.cast_npow, ← Int.cast_mul, ← nsmul_eq_mul] end Neg section NatPowAssoc /-! In the module docstring for algebras at `Mathlib.Algebra.Algebra.Basic`, 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] {p : R[X]} (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 | h_add p q ph qh => simp only [mul_add, smeval_add, ph, qh, smul_add] | h_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 | h_add p q ph qh => intro n simp only [add_mul, smeval_add, ph, qh, Nat.cast_add] | h_monomial n a => intro n rw [smeval_monomial, smeval_monomial, nsmul_eq_mul, smul_eq_mul, Nat.cast_mul, Nat.cast_npow] @[deprecated (since := "2024-04-17")] alias smeval_at_nat_cast := smeval_at_natCast theorem smeval_at_zero : p.smeval (0 : S) = (p.coeff 0) • (1 : S) := by induction p using Polynomial.induction_on' with | h_add p q ph qh => simp_all only [smeval_add, coeff_add, add_smul] | h_monomial n a => cases n with | zero => simp only [Nat.zero_eq, 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 | h_add p q ph qh => simp only [smeval_add, ph, qh, mul_add] | h_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 | h_add p q ph qh => simp only [smeval_add, ph, qh, mul_add] | h_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 | h_add r s hr hs => simp only [add_comp, hr, hs, smeval_add, add_mul] rw [← C_mul_X_pow_eq_monomial, mul_assoc, smeval_C_mul, smeval_X_pow_mul, smeval_add] | h_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 | h_add p q ph qh => simp only [add_mul, smeval_add, ph, qh] | h_monomial n a => simp only [← monomial_one_one_eq_X, monomial_mul_monomial, smeval_monomial, mul_one, pow_succ', mul_assoc, 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 | h_add p q ph qh => simp only [smeval_add, ph, qh, add_mul] | h_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 | h_add r s hr hs => simp only [add_comp, hr, hs, smeval_add, add_mul] | h_monomial n a => simp only [smeval_monomial, smeval_C_mul, smeval_mul_X_pow, 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 | h_add r s hr hs => simp [add_comp, hr, hs, smeval_add] | h_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 | h_add r s hr hs => exact (smeval_add R r s x) ▸ Commute.add_left hr hs | h_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 | h_add r s hr hs => exact (smeval_add R r s x) ▸ Commute.add_left hr hs | h_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
Algebra\Polynomial\SpecificDegree.lean
/- Copyright (c) 2024 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen, Alex J. Best -/ import Mathlib.Algebra.Polynomial.Roots import Mathlib.Tactic.IntervalCases /-! # 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_not_mem, 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] /-- 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 {p : K[X]} (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] end Field end Polynomial
Algebra\Polynomial\Splits.lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import Mathlib.Algebra.Polynomial.FieldDivision import Mathlib.Algebra.Polynomial.Lifts import Mathlib.Data.List.Prime /-! # Split polynomials A polynomial `f : K[X]` splits over a field extension `L` of `K` if it is zero or all of its irreducible factors over `L` have degree `1`. ## Main definitions * `Polynomial.Splits i f`: A predicate on a homomorphism `i : K →+* L` from a commutative ring to a field and a polynomial `f` saying that `f.map i` is zero or all of its irreducible factors over `L` have degree `1`. -/ noncomputable section open Polynomial universe u v w variable {R : Type*} {F : Type u} {K : Type v} {L : Type w} namespace Polynomial open Polynomial section Splits section CommRing variable [CommRing K] [Field L] [Field F] variable (i : K →+* L) /-- A polynomial `Splits` iff it is zero or all of its irreducible factors have `degree` 1. -/ def Splits (f : K[X]) : Prop := f.map i = 0 ∨ ∀ {g : L[X]}, Irreducible g → g ∣ f.map i → degree g = 1 @[simp] theorem splits_zero : Splits i (0 : K[X]) := Or.inl (Polynomial.map_zero i) theorem splits_of_map_eq_C {f : K[X]} {a : L} (h : f.map i = C a) : Splits i f := letI := Classical.decEq L if ha : a = 0 then Or.inl (h.trans (ha.symm ▸ C_0)) else Or.inr fun hg ⟨p, hp⟩ => absurd hg.1 <| Classical.not_not.2 <| isUnit_iff_degree_eq_zero.2 <| by have := congr_arg degree hp rw [h, degree_C ha, degree_mul, @eq_comm (WithBot ℕ) 0, Nat.WithBot.add_eq_zero_iff] at this exact this.1 @[simp] theorem splits_C (a : K) : Splits i (C a) := splits_of_map_eq_C i (map_C i) theorem splits_of_map_degree_eq_one {f : K[X]} (hf : degree (f.map i) = 1) : Splits i f := Or.inr fun hg ⟨p, hp⟩ => by have := congr_arg degree hp simp [Nat.WithBot.add_eq_one_iff, hf, @eq_comm (WithBot ℕ) 1, mt isUnit_iff_degree_eq_zero.2 hg.1] at this tauto theorem splits_of_degree_le_one {f : K[X]} (hf : degree f ≤ 1) : Splits i f := if hif : degree (f.map i) ≤ 0 then splits_of_map_eq_C i (degree_le_zero_iff.mp hif) else by push_neg at hif rw [← Order.succ_le_iff, ← WithBot.coe_zero, WithBot.succ_coe, Nat.succ_eq_succ] at hif exact splits_of_map_degree_eq_one i (le_antisymm ((degree_map_le i _).trans hf) hif) theorem splits_of_degree_eq_one {f : K[X]} (hf : degree f = 1) : Splits i f := splits_of_degree_le_one i hf.le theorem splits_of_natDegree_le_one {f : K[X]} (hf : natDegree f ≤ 1) : Splits i f := splits_of_degree_le_one i (degree_le_of_natDegree_le hf) theorem splits_of_natDegree_eq_one {f : K[X]} (hf : natDegree f = 1) : Splits i f := splits_of_natDegree_le_one i (le_of_eq hf) theorem splits_mul {f g : K[X]} (hf : Splits i f) (hg : Splits i g) : Splits i (f * g) := letI := Classical.decEq L if h : (f * g).map i = 0 then Or.inl h else Or.inr @fun p hp hpf => ((irreducible_iff_prime.1 hp).2.2 _ _ (show p ∣ map i f * map i g by convert hpf; rw [Polynomial.map_mul])).elim (hf.resolve_left (fun hf => by simp [hf] at h) hp) (hg.resolve_left (fun hg => by simp [hg] at h) hp) theorem splits_of_splits_mul' {f g : K[X]} (hfg : (f * g).map i ≠ 0) (h : Splits i (f * g)) : Splits i f ∧ Splits i g := ⟨Or.inr @fun g hgi hg => Or.resolve_left h hfg hgi (by rw [Polynomial.map_mul]; exact hg.trans (dvd_mul_right _ _)), Or.inr @fun g hgi hg => Or.resolve_left h hfg hgi (by rw [Polynomial.map_mul]; exact hg.trans (dvd_mul_left _ _))⟩ theorem splits_map_iff (j : L →+* F) {f : K[X]} : Splits j (f.map i) ↔ Splits (j.comp i) f := by simp [Splits, Polynomial.map_map] theorem splits_one : Splits i 1 := splits_C i 1 theorem splits_of_isUnit [IsDomain K] {u : K[X]} (hu : IsUnit u) : u.Splits i := (isUnit_iff.mp hu).choose_spec.2 ▸ splits_C _ _ theorem splits_X_sub_C {x : K} : (X - C x).Splits i := splits_of_degree_le_one _ <| degree_X_sub_C_le _ theorem splits_X : X.Splits i := splits_of_degree_le_one _ degree_X_le theorem splits_prod {ι : Type u} {s : ι → K[X]} {t : Finset ι} : (∀ j ∈ t, (s j).Splits i) → (∏ x ∈ t, s x).Splits i := by classical refine Finset.induction_on t (fun _ => splits_one i) fun a t hat ih ht => ?_ rw [Finset.forall_mem_insert] at ht; rw [Finset.prod_insert hat] exact splits_mul i ht.1 (ih ht.2) theorem splits_pow {f : K[X]} (hf : f.Splits i) (n : ℕ) : (f ^ n).Splits i := by rw [← Finset.card_range n, ← Finset.prod_const] exact splits_prod i fun j _ => hf theorem splits_X_pow (n : ℕ) : (X ^ n).Splits i := splits_pow i (splits_X i) n theorem splits_id_iff_splits {f : K[X]} : (f.map i).Splits (RingHom.id L) ↔ f.Splits i := by rw [splits_map_iff, RingHom.id_comp] theorem exists_root_of_splits' {f : K[X]} (hs : Splits i f) (hf0 : degree (f.map i) ≠ 0) : ∃ x, eval₂ i x f = 0 := letI := Classical.decEq L if hf0' : f.map i = 0 then by simp [eval₂_eq_eval_map, hf0'] else let ⟨g, hg⟩ := WfDvdMonoid.exists_irreducible_factor (show ¬IsUnit (f.map i) from mt isUnit_iff_degree_eq_zero.1 hf0) hf0' let ⟨x, hx⟩ := exists_root_of_degree_eq_one (hs.resolve_left hf0' hg.1 hg.2) let ⟨i, hi⟩ := hg.2 ⟨x, by rw [← eval_map, hi, eval_mul, show _ = _ from hx, zero_mul]⟩ theorem roots_ne_zero_of_splits' {f : K[X]} (hs : Splits i f) (hf0 : natDegree (f.map i) ≠ 0) : (f.map i).roots ≠ 0 := let ⟨x, hx⟩ := exists_root_of_splits' i hs fun h => hf0 <| natDegree_eq_of_degree_eq_some h fun h => by rw [← eval_map] at hx have : f.map i ≠ 0 := by intro; simp_all cases h.subst ((mem_roots this).2 hx) /-- Pick a root of a polynomial that splits. See `rootOfSplits` for polynomials over a field which has simpler assumptions. -/ def rootOfSplits' {f : K[X]} (hf : f.Splits i) (hfd : (f.map i).degree ≠ 0) : L := Classical.choose <| exists_root_of_splits' i hf hfd theorem map_rootOfSplits' {f : K[X]} (hf : f.Splits i) (hfd) : f.eval₂ i (rootOfSplits' i hf hfd) = 0 := Classical.choose_spec <| exists_root_of_splits' i hf hfd theorem natDegree_eq_card_roots' {p : K[X]} {i : K →+* L} (hsplit : Splits i p) : (p.map i).natDegree = Multiset.card (p.map i).roots := by by_cases hp : p.map i = 0 · rw [hp, natDegree_zero, roots_zero, Multiset.card_zero] obtain ⟨q, he, hd, hr⟩ := exists_prod_multiset_X_sub_C_mul (p.map i) rw [← splits_id_iff_splits, ← he] at hsplit rw [← he] at hp have hq : q ≠ 0 := fun h => hp (by rw [h, mul_zero]) rw [← hd, add_right_eq_self] by_contra h have h' : (map (RingHom.id L) q).natDegree ≠ 0 := by simp [h] have := roots_ne_zero_of_splits' (RingHom.id L) (splits_of_splits_mul' _ ?_ hsplit).2 h' · rw [map_id] at this exact this hr · rw [map_id] exact mul_ne_zero monic_prod_multiset_X_sub_C.ne_zero hq theorem degree_eq_card_roots' {p : K[X]} {i : K →+* L} (p_ne_zero : p.map i ≠ 0) (hsplit : Splits i p) : (p.map i).degree = Multiset.card (p.map i).roots := by simp [degree_eq_natDegree p_ne_zero, natDegree_eq_card_roots' hsplit] end CommRing variable [CommRing R] [Field K] [Field L] [Field F] variable (i : K →+* L) /-- This lemma is for polynomials over a field. -/ theorem splits_iff (f : K[X]) : Splits i f ↔ f = 0 ∨ ∀ {g : L[X]}, Irreducible g → g ∣ f.map i → degree g = 1 := by rw [Splits, map_eq_zero] /-- This lemma is for polynomials over a field. -/ theorem Splits.def {i : K →+* L} {f : K[X]} (h : Splits i f) : f = 0 ∨ ∀ {g : L[X]}, Irreducible g → g ∣ f.map i → degree g = 1 := (splits_iff i f).mp h theorem splits_of_splits_mul {f g : K[X]} (hfg : f * g ≠ 0) (h : Splits i (f * g)) : Splits i f ∧ Splits i g := splits_of_splits_mul' i (map_ne_zero hfg) h theorem splits_of_splits_of_dvd {f g : K[X]} (hf0 : f ≠ 0) (hf : Splits i f) (hgf : g ∣ f) : Splits i g := by obtain ⟨f, rfl⟩ := hgf exact (splits_of_splits_mul i hf0 hf).1 theorem splits_of_splits_gcd_left [DecidableEq K] {f g : K[X]} (hf0 : f ≠ 0) (hf : Splits i f) : Splits i (EuclideanDomain.gcd f g) := Polynomial.splits_of_splits_of_dvd i hf0 hf (EuclideanDomain.gcd_dvd_left f g) theorem splits_of_splits_gcd_right [DecidableEq K] {f g : K[X]} (hg0 : g ≠ 0) (hg : Splits i g) : Splits i (EuclideanDomain.gcd f g) := Polynomial.splits_of_splits_of_dvd i hg0 hg (EuclideanDomain.gcd_dvd_right f g) theorem splits_mul_iff {f g : K[X]} (hf : f ≠ 0) (hg : g ≠ 0) : (f * g).Splits i ↔ f.Splits i ∧ g.Splits i := ⟨splits_of_splits_mul i (mul_ne_zero hf hg), fun ⟨hfs, hgs⟩ => splits_mul i hfs hgs⟩ theorem splits_prod_iff {ι : Type u} {s : ι → K[X]} {t : Finset ι} : (∀ j ∈ t, s j ≠ 0) → ((∏ x ∈ t, s x).Splits i ↔ ∀ j ∈ t, (s j).Splits i) := by classical refine Finset.induction_on t (fun _ => ⟨fun _ _ h => by simp only [Finset.not_mem_empty] at h, fun _ => splits_one i⟩) fun a t hat ih ht => ?_ rw [Finset.forall_mem_insert] at ht ⊢ rw [Finset.prod_insert hat, splits_mul_iff i ht.1 (Finset.prod_ne_zero_iff.2 ht.2), ih ht.2] theorem degree_eq_one_of_irreducible_of_splits {p : K[X]} (hp : Irreducible p) (hp_splits : Splits (RingHom.id K) p) : p.degree = 1 := by rcases hp_splits with ⟨⟩ | hp_splits · exfalso simp_all · apply hp_splits hp simp theorem exists_root_of_splits {f : K[X]} (hs : Splits i f) (hf0 : degree f ≠ 0) : ∃ x, eval₂ i x f = 0 := exists_root_of_splits' i hs ((f.degree_map i).symm ▸ hf0) theorem roots_ne_zero_of_splits {f : K[X]} (hs : Splits i f) (hf0 : natDegree f ≠ 0) : (f.map i).roots ≠ 0 := roots_ne_zero_of_splits' i hs (ne_of_eq_of_ne (natDegree_map i) hf0) /-- Pick a root of a polynomial that splits. This version is for polynomials over a field and has simpler assumptions. -/ def rootOfSplits {f : K[X]} (hf : f.Splits i) (hfd : f.degree ≠ 0) : L := rootOfSplits' i hf ((f.degree_map i).symm ▸ hfd) /-- `rootOfSplits'` is definitionally equal to `rootOfSplits`. -/ theorem rootOfSplits'_eq_rootOfSplits {f : K[X]} (hf : f.Splits i) (hfd) : rootOfSplits' i hf hfd = rootOfSplits i hf (f.degree_map i ▸ hfd) := rfl theorem map_rootOfSplits {f : K[X]} (hf : f.Splits i) (hfd) : f.eval₂ i (rootOfSplits i hf hfd) = 0 := map_rootOfSplits' i hf (ne_of_eq_of_ne (degree_map f i) hfd) theorem natDegree_eq_card_roots {p : K[X]} {i : K →+* L} (hsplit : Splits i p) : p.natDegree = Multiset.card (p.map i).roots := (natDegree_map i).symm.trans <| natDegree_eq_card_roots' hsplit theorem degree_eq_card_roots {p : K[X]} {i : K →+* L} (p_ne_zero : p ≠ 0) (hsplit : Splits i p) : p.degree = Multiset.card (p.map i).roots := by rw [degree_eq_natDegree p_ne_zero, natDegree_eq_card_roots hsplit] theorem roots_map {f : K[X]} (hf : f.Splits <| RingHom.id K) : (f.map i).roots = f.roots.map i := (roots_map_of_injective_of_card_eq_natDegree i.injective <| by convert (natDegree_eq_card_roots hf).symm rw [map_id]).symm theorem image_rootSet [Algebra R K] [Algebra R L] {p : R[X]} (h : p.Splits (algebraMap R K)) (f : K →ₐ[R] L) : f '' p.rootSet K = p.rootSet L := by classical rw [rootSet, ← Finset.coe_image, ← Multiset.toFinset_map, ← f.coe_toRingHom, ← roots_map _ ((splits_id_iff_splits (algebraMap R K)).mpr h), map_map, f.comp_algebraMap, ← rootSet] theorem adjoin_rootSet_eq_range [Algebra R K] [Algebra R L] {p : R[X]} (h : p.Splits (algebraMap R K)) (f : K →ₐ[R] L) : Algebra.adjoin R (p.rootSet L) = f.range ↔ Algebra.adjoin R (p.rootSet K) = ⊤ := by rw [← image_rootSet h f, Algebra.adjoin_image, ← Algebra.map_top] exact (Subalgebra.map_injective f.toRingHom.injective).eq_iff theorem eq_prod_roots_of_splits {p : K[X]} {i : K →+* L} (hsplit : Splits i p) : p.map i = C (i p.leadingCoeff) * ((p.map i).roots.map fun a => X - C a).prod := by rw [← leadingCoeff_map]; symm apply C_leadingCoeff_mul_prod_multiset_X_sub_C rw [natDegree_map]; exact (natDegree_eq_card_roots hsplit).symm theorem eq_prod_roots_of_splits_id {p : K[X]} (hsplit : Splits (RingHom.id K) p) : p = C p.leadingCoeff * (p.roots.map fun a => X - C a).prod := by simpa using eq_prod_roots_of_splits hsplit theorem eq_prod_roots_of_monic_of_splits_id {p : K[X]} (m : Monic p) (hsplit : Splits (RingHom.id K) p) : p = (p.roots.map fun a => X - C a).prod := by convert eq_prod_roots_of_splits_id hsplit simp [m] theorem eq_X_sub_C_of_splits_of_single_root {x : K} {h : K[X]} (h_splits : Splits i h) (h_roots : (h.map i).roots = {i x}) : h = C h.leadingCoeff * (X - C x) := by apply Polynomial.map_injective _ i.injective rw [eq_prod_roots_of_splits h_splits, h_roots] simp variable (R) in theorem mem_lift_of_splits_of_roots_mem_range [Algebra R K] {f : K[X]} (hs : f.Splits (RingHom.id K)) (hm : f.Monic) (hr : ∀ a ∈ f.roots, a ∈ (algebraMap R K).range) : f ∈ Polynomial.lifts (algebraMap R K) := by rw [eq_prod_roots_of_monic_of_splits_id hm hs, lifts_iff_liftsRing] refine Subring.multiset_prod_mem _ _ fun P hP => ?_ obtain ⟨b, hb, rfl⟩ := Multiset.mem_map.1 hP exact Subring.sub_mem _ (X_mem_lifts _) (C'_mem_lifts (hr _ hb)) section UFD attribute [local instance] PrincipalIdealRing.to_uniqueFactorizationMonoid local infixl:50 " ~ᵤ " => Associated open UniqueFactorizationMonoid Associates theorem splits_of_exists_multiset {f : K[X]} {s : Multiset L} (hs : f.map i = C (i f.leadingCoeff) * (s.map fun a : L => X - C a).prod) : Splits i f := letI := Classical.decEq K if hf0 : f = 0 then hf0.symm ▸ splits_zero i else Or.inr @fun p hp hdp => by rw [irreducible_iff_prime] at hp rw [hs, ← Multiset.prod_toList] at hdp obtain hd | hd := hp.2.2 _ _ hdp · refine (hp.2.1 <| isUnit_of_dvd_unit hd ?_).elim exact isUnit_C.2 ((leadingCoeff_ne_zero.2 hf0).isUnit.map i) · obtain ⟨q, hq, hd⟩ := hp.dvd_prod_iff.1 hd obtain ⟨a, _, rfl⟩ := Multiset.mem_map.1 (Multiset.mem_toList.1 hq) rw [degree_eq_degree_of_associated ((hp.dvd_prime_iff_associated <| prime_X_sub_C a).1 hd)] exact degree_X_sub_C a theorem splits_of_splits_id {f : K[X]} : Splits (RingHom.id K) f → Splits i f := UniqueFactorizationMonoid.induction_on_prime f (fun _ => splits_zero _) (fun _ hu _ => splits_of_degree_le_one _ ((isUnit_iff_degree_eq_zero.1 hu).symm ▸ by decide)) fun a p ha0 hp ih hfi => splits_mul _ (splits_of_degree_eq_one _ ((splits_of_splits_mul _ (mul_ne_zero hp.1 ha0) hfi).1.def.resolve_left hp.1 hp.irreducible (by rw [map_id]))) (ih (splits_of_splits_mul _ (mul_ne_zero hp.1 ha0) hfi).2) end UFD theorem splits_iff_exists_multiset {f : K[X]} : Splits i f ↔ ∃ s : Multiset L, f.map i = C (i f.leadingCoeff) * (s.map fun a : L => X - C a).prod := ⟨fun hf => ⟨(f.map i).roots, eq_prod_roots_of_splits hf⟩, fun ⟨_, hs⟩ => splits_of_exists_multiset i hs⟩ theorem splits_of_comp (j : L →+* F) {f : K[X]} (h : Splits (j.comp i) f) (roots_mem_range : ∀ a ∈ (f.map (j.comp i)).roots, a ∈ j.range) : Splits i f := by choose lift lift_eq using roots_mem_range rw [splits_iff_exists_multiset] refine ⟨(f.map (j.comp i)).roots.pmap lift fun _ ↦ id, map_injective _ j.injective ?_⟩ conv_lhs => rw [Polynomial.map_map, eq_prod_roots_of_splits h] simp_rw [Polynomial.map_mul, Polynomial.map_multiset_prod, Multiset.map_pmap, Polynomial.map_sub, map_C, map_X, lift_eq, Multiset.pmap_eq_map] rfl theorem splits_id_of_splits {f : K[X]} (h : Splits i f) (roots_mem_range : ∀ a ∈ (f.map i).roots, a ∈ i.range) : Splits (RingHom.id K) f := splits_of_comp (RingHom.id K) i h roots_mem_range theorem splits_comp_of_splits (i : R →+* K) (j : K →+* L) {f : R[X]} (h : Splits i f) : Splits (j.comp i) f := (splits_map_iff i j).mp (splits_of_splits_id _ <| (splits_map_iff i <| .id K).mpr h) variable [Algebra R K] [Algebra R L] theorem splits_of_algHom {f : R[X]} (h : Splits (algebraMap R K) f) (e : K →ₐ[R] L) : Splits (algebraMap R L) f := by rw [← e.comp_algebraMap_of_tower R]; exact splits_comp_of_splits _ _ h variable (L) in theorem splits_of_isScalarTower {f : R[X]} [Algebra K L] [IsScalarTower R K L] (h : Splits (algebraMap R K) f) : Splits (algebraMap R L) f := splits_of_algHom h (IsScalarTower.toAlgHom R K L) /-- A polynomial splits if and only if it has as many roots as its degree. -/ theorem splits_iff_card_roots {p : K[X]} : Splits (RingHom.id K) p ↔ Multiset.card p.roots = p.natDegree := by constructor · intro H rw [natDegree_eq_card_roots H, map_id] · intro hroots rw [splits_iff_exists_multiset (RingHom.id K)] use p.roots simp only [RingHom.id_apply, map_id] exact (C_leadingCoeff_mul_prod_multiset_X_sub_C hroots).symm theorem aeval_root_derivative_of_splits [Algebra K L] [DecidableEq L] {P : K[X]} (hmo : P.Monic) (hP : P.Splits (algebraMap K L)) {r : L} (hr : r ∈ P.aroots L) : aeval r (Polynomial.derivative P) = (((P.aroots L).erase r).map fun a => r - a).prod := by replace hmo := hmo.map (algebraMap K L) replace hP := (splits_id_iff_splits (algebraMap K L)).2 hP rw [aeval_def, ← eval_map, ← derivative_map] nth_rw 1 [eq_prod_roots_of_monic_of_splits_id hmo hP] rw [eval_multiset_prod_X_sub_C_derivative hr] /-- If `P` is a monic polynomial that splits, then `coeff P 0` equals the product of the roots. -/ theorem prod_roots_eq_coeff_zero_of_monic_of_split {P : K[X]} (hmo : P.Monic) (hP : P.Splits (RingHom.id K)) : coeff P 0 = (-1) ^ P.natDegree * P.roots.prod := by nth_rw 1 [eq_prod_roots_of_monic_of_splits_id hmo hP] rw [coeff_zero_eq_eval_zero, eval_multiset_prod, Multiset.map_map] simp_rw [Function.comp_apply, eval_sub, eval_X, zero_sub, eval_C] conv_lhs => congr congr ext rw [neg_eq_neg_one_mul] simp only [splits_iff_card_roots.1 hP, neg_mul, one_mul, Multiset.prod_map_neg] /-- If `P` is a monic polynomial that splits, then `P.nextCoeff` equals the sum of the roots. -/ theorem sum_roots_eq_nextCoeff_of_monic_of_split {P : K[X]} (hmo : P.Monic) (hP : P.Splits (RingHom.id K)) : P.nextCoeff = -P.roots.sum := by nth_rw 1 [eq_prod_roots_of_monic_of_splits_id hmo hP] rw [Monic.nextCoeff_multiset_prod _ _ fun a ha => _] · simp_rw [nextCoeff_X_sub_C, Multiset.sum_map_neg'] · simp only [monic_X_sub_C, implies_true] end Splits end Polynomial
Algebra\Polynomial\Taylor.lean
/- Copyright (c) 2021 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import Mathlib.Algebra.Polynomial.AlgebraMap import Mathlib.Algebra.Polynomial.Degree.Lemmas import Mathlib.Algebra.Polynomial.HasseDeriv /-! # Taylor expansions of polynomials ## Main declarations * `Polynomial.taylor`: the Taylor expansion of the polynomial `f` at `r` * `Polynomial.taylor_coeff`: the `k`th coefficient of `taylor r f` is `(Polynomial.hasseDeriv k f).eval r` * `Polynomial.eq_zero_of_hasseDeriv_eq_zero`: the identity principle: a polynomial is 0 iff all its Hasse derivatives are zero -/ noncomputable section namespace Polynomial open Polynomial variable {R : Type*} [Semiring R] (r : R) (f : R[X]) /-- The Taylor expansion of a polynomial `f` at `r`. -/ def taylor (r : R) : R[X] →ₗ[R] R[X] where toFun f := f.comp (X + C r) map_add' f g := add_comp map_smul' c f := by simp only [smul_eq_C_mul, C_mul_comp, RingHom.id_apply] theorem taylor_apply : taylor r f = f.comp (X + C r) := rfl @[simp] theorem taylor_X : taylor r X = X + C r := by simp only [taylor_apply, X_comp] @[simp] theorem taylor_C (x : R) : taylor r (C x) = C x := by simp only [taylor_apply, C_comp] @[simp] theorem taylor_zero' : taylor (0 : R) = LinearMap.id := by ext simp only [taylor_apply, add_zero, comp_X, _root_.map_zero, LinearMap.id_comp, Function.comp_apply, LinearMap.coe_comp] theorem taylor_zero (f : R[X]) : taylor 0 f = f := by rw [taylor_zero', LinearMap.id_apply] @[simp] theorem taylor_one : taylor r (1 : R[X]) = C 1 := by rw [← C_1, taylor_C] @[simp] theorem taylor_monomial (i : ℕ) (k : R) : taylor r (monomial i k) = C k * (X + C r) ^ i := by simp [taylor_apply] /-- The `k`th coefficient of `Polynomial.taylor r f` is `(Polynomial.hasseDeriv k f).eval r`. -/ theorem taylor_coeff (n : ℕ) : (taylor r f).coeff n = (hasseDeriv n f).eval r := show (lcoeff R n).comp (taylor r) f = (leval r).comp (hasseDeriv n) f by congr 1; clear! f; ext i simp only [leval_apply, mul_one, one_mul, eval_monomial, LinearMap.comp_apply, coeff_C_mul, hasseDeriv_monomial, taylor_apply, monomial_comp, C_1, (commute_X (C r)).add_pow i, map_sum] simp only [lcoeff_apply, ← C_eq_natCast, mul_assoc, ← C_pow, ← C_mul, coeff_mul_C, (Nat.cast_commute _ _).eq, coeff_X_pow, boole_mul, Finset.sum_ite_eq, Finset.mem_range] split_ifs with h; · rfl push_neg at h; rw [Nat.choose_eq_zero_of_lt h, Nat.cast_zero, mul_zero] @[simp] theorem taylor_coeff_zero : (taylor r f).coeff 0 = f.eval r := by rw [taylor_coeff, hasseDeriv_zero, LinearMap.id_apply] @[simp] theorem taylor_coeff_one : (taylor r f).coeff 1 = f.derivative.eval r := by rw [taylor_coeff, hasseDeriv_one] @[simp] theorem natDegree_taylor (p : R[X]) (r : R) : natDegree (taylor r p) = natDegree p := by refine map_natDegree_eq_natDegree _ ?_ nontriviality R intro n c c0 simp [taylor_monomial, natDegree_C_mul_eq_of_mul_ne_zero, natDegree_pow_X_add_C, c0] @[simp] theorem taylor_mul {R} [CommSemiring R] (r : R) (p q : R[X]) : taylor r (p * q) = taylor r p * taylor r q := by simp only [taylor_apply, mul_comp] /-- `Polynomial.taylor` as an `AlgHom` for commutative semirings -/ @[simps!] def taylorAlgHom {R} [CommSemiring R] (r : R) : R[X] →ₐ[R] R[X] := AlgHom.ofLinearMap (taylor r) (taylor_one r) (taylor_mul r) theorem taylor_taylor {R} [CommSemiring R] (f : R[X]) (r s : R) : taylor r (taylor s f) = taylor (r + s) f := by simp only [taylor_apply, comp_assoc, map_add, add_comp, X_comp, C_comp, C_add, add_assoc] theorem taylor_eval {R} [CommSemiring R] (r : R) (f : R[X]) (s : R) : (taylor r f).eval s = f.eval (s + r) := by simp only [taylor_apply, eval_comp, eval_C, eval_X, eval_add] theorem taylor_eval_sub {R} [CommRing R] (r : R) (f : R[X]) (s : R) : (taylor r f).eval (s - r) = f.eval s := by rw [taylor_eval, sub_add_cancel] theorem taylor_injective {R} [CommRing R] (r : R) : Function.Injective (taylor r) := by intro f g h apply_fun taylor (-r) at h simpa only [taylor_apply, comp_assoc, add_comp, X_comp, C_comp, C_neg, neg_add_cancel_right, comp_X] using h theorem eq_zero_of_hasseDeriv_eq_zero {R} [CommRing R] (f : R[X]) (r : R) (h : ∀ k, (hasseDeriv k f).eval r = 0) : f = 0 := by apply taylor_injective r rw [LinearMap.map_zero] ext k simp only [taylor_coeff, h, coeff_zero] /-- Taylor's formula. -/ theorem sum_taylor_eq {R} [CommRing R] (f : R[X]) (r : R) : ((taylor r f).sum fun i a => C a * (X - C r) ^ i) = f := by rw [← comp_eq_sum_left, sub_eq_add_neg, ← C_neg, ← taylor_apply, taylor_taylor, neg_add_self, taylor_zero] end Polynomial
Algebra\Polynomial\UnitTrinomial.lean
/- Copyright (c) 2022 Thomas Browning. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Thomas Browning -/ import Mathlib.Algebra.Polynomial.Mirror import Mathlib.Data.Int.Order.Units /-! # 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.card = 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.card = 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_of_not 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 => isUnit_ofPowEqOne (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] -- Porting note: added next line (less powerful `simp`). rw [Finsupp.filter_add, Finsupp.filter_add, Finsupp.filter_add, Finsupp.filter_add, Finsupp.filter_add, Finsupp.filter_add, Finsupp.filter_add, 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, ofFinsupp_add, ofFinsupp_single] -- Porting note: added next two lines (less powerful `simp`). 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 ⟨?_, add_lt_add_left key n⟩ 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) · exact ⟨add_lt_add_right hkm n, add_lt_add_right hmn n⟩ · rw [← add_assoc, add_tsub_cancel_of_le hmn.le, add_comm] exact fun h => h.1.ne rfl · intro h have := h.1 rw [add_comm, add_lt_add_iff_right] at this exact asymm this hmn · 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.eq_iff] 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 · suffices m = m' by rw [this] at hp exact Or.inl (hq.trans hp.symm) rw [tsub_add_eq_add_tsub hmn.le, eq_tsub_iff_add_eq_of_le, ← two_mul] at hm · rw [tsub_add_eq_add_tsub hmn'.le, eq_tsub_iff_add_eq_of_le, ← two_mul] at hm' · exact mul_left_cancel₀ two_ne_zero (hm.trans hm'.symm) · exact hmn'.le.trans (Nat.le_add_right n k) · exact hmn.le.trans (Nat.le_add_right n k) 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_pow, eval_X, 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.eq_iff] 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) · -- Porting note: `rw [h1] at *` rewrites at `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)) · -- Porting note: `rw [h1] at *` rewrites at `h1` 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
Algebra\Polynomial\Degree\CardPowDegree.lean
/- Copyright (c) 2021 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen -/ import Mathlib.Algebra.Order.EuclideanAbsoluteValue import Mathlib.Algebra.Order.Group.Basic import Mathlib.Algebra.Order.Ring.Basic import Mathlib.Algebra.Polynomial.FieldDivision /-! # Absolute value on polynomials over a finite field. Let `𝔽_q` be a finite field of cardinality `q`, then the map sending a polynomial `p` to `q ^ degree p` (where `q ^ degree 0 = 0`) is an absolute value. ## Main definitions * `Polynomial.cardPowDegree` is an absolute value on `𝔽_q[t]`, the ring of polynomials over a finite field of cardinality `q`, mapping a polynomial `p` to `q ^ degree p` (where `q ^ degree 0 = 0`) ## Main results * `Polynomial.cardPowDegree_isEuclidean`: `cardPowDegree` respects the Euclidean domain structure on the ring of polynomials -/ namespace Polynomial variable {Fq : Type*} [Field Fq] [Fintype Fq] open AbsoluteValue open Polynomial /-- `cardPowDegree` is the absolute value on `𝔽_q[t]` sending `f` to `q ^ degree f`. `cardPowDegree 0` is defined to be `0`. -/ noncomputable def cardPowDegree : AbsoluteValue Fq[X] ℤ := have card_pos : 0 < Fintype.card Fq := Fintype.card_pos_iff.mpr inferInstance have pow_pos : ∀ n, 0 < (Fintype.card Fq : ℤ) ^ n := fun n => pow_pos (Int.natCast_pos.mpr card_pos) n letI := Classical.decEq Fq { toFun := fun p => if p = 0 then 0 else (Fintype.card Fq : ℤ) ^ p.natDegree nonneg' := fun p => by dsimp split_ifs · rfl exact pow_nonneg (Int.ofNat_zero_le _) _ eq_zero' := fun p => ite_eq_left_iff.trans <| ⟨fun h => by contrapose! h exact ⟨h, (pow_pos _).ne'⟩, absurd⟩ add_le' := fun p q => by by_cases hp : p = 0; · simp [hp] by_cases hq : q = 0; · simp [hq] by_cases hpq : p + q = 0 · simp only [hpq, hp, hq, eq_self_iff_true, if_true, if_false] exact add_nonneg (pow_pos _).le (pow_pos _).le simp only [hpq, hp, hq, if_false] refine le_trans (pow_le_pow_right (by omega) (Polynomial.natDegree_add_le _ _)) ?_ refine le_trans (le_max_iff.mpr ?_) (max_le_add_of_nonneg (pow_nonneg (by omega) _) (pow_nonneg (by omega) _)) exact (max_choice p.natDegree q.natDegree).imp (fun h => by rw [h]) fun h => by rw [h] map_mul' := fun p q => by by_cases hp : p = 0; · simp [hp] by_cases hq : q = 0; · simp [hq] have hpq : p * q ≠ 0 := mul_ne_zero hp hq simp only [hpq, hp, hq, eq_self_iff_true, if_true, if_false, Polynomial.natDegree_mul hp hq, pow_add] } theorem cardPowDegree_apply [DecidableEq Fq] (p : Fq[X]) : cardPowDegree p = if p = 0 then 0 else (Fintype.card Fq : ℤ) ^ natDegree p := by rw [cardPowDegree] dsimp convert rfl @[simp] theorem cardPowDegree_zero : cardPowDegree (0 : Fq[X]) = 0 := rfl @[simp] theorem cardPowDegree_nonzero (p : Fq[X]) (hp : p ≠ 0) : cardPowDegree p = (Fintype.card Fq : ℤ) ^ p.natDegree := if_neg hp theorem cardPowDegree_isEuclidean : IsEuclidean (cardPowDegree : AbsoluteValue Fq[X] ℤ) := have card_pos : 0 < Fintype.card Fq := Fintype.card_pos_iff.mpr inferInstance have pow_pos : ∀ n, 0 < (Fintype.card Fq : ℤ) ^ n := fun n => pow_pos (Int.natCast_pos.mpr card_pos) n { map_lt_map_iff' := fun {p q} => by classical show cardPowDegree p < cardPowDegree q ↔ degree p < degree q simp only [cardPowDegree_apply] split_ifs with hp hq hq · simp only [hp, hq, lt_self_iff_false] · simp only [hp, hq, degree_zero, Ne, bot_lt_iff_ne_bot, degree_eq_bot, pow_pos, not_false_iff] · simp only [hp, hq, degree_zero, not_lt_bot, (pow_pos _).not_lt] · rw [degree_eq_natDegree hp, degree_eq_natDegree hq, Nat.cast_lt, pow_lt_pow_iff_right] exact mod_cast @Fintype.one_lt_card Fq _ _ } end Polynomial
Algebra\Polynomial\Degree\Definitions.lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker -/ import Mathlib.Algebra.MonoidAlgebra.Degree import Mathlib.Algebra.Polynomial.Coeff import Mathlib.Algebra.Polynomial.Monomial import Mathlib.Data.Fintype.BigOperators import Mathlib.Data.Nat.WithBot import Mathlib.Data.Nat.Cast.WithTop import Mathlib.Data.Nat.SuccPred /-! # Theory of univariate polynomials The definitions include `degree`, `Monic`, `leadingCoeff` Results include - `degree_mul` : The degree of the product is the sum of degrees - `leadingCoeff_add_of_degree_eq` and `leadingCoeff_add_of_degree_lt` : The leading_coefficient of a sum is determined by the leading coefficients and degrees -/ -- Porting note: `Mathlib.Data.Nat.Cast.WithTop` should be imported for `Nat.cast_withBot`. noncomputable section open Finsupp Finset open Polynomial namespace Polynomial universe u v variable {R : Type u} {S : Type v} {a b c d : R} {n m : ℕ} section Semiring variable [Semiring R] {p q r : R[X]} /-- `degree p` is the degree of the polynomial `p`, i.e. the largest `X`-exponent in `p`. `degree p = some n` when `p ≠ 0` and `n` is the highest power of `X` that appears in `p`, otherwise `degree 0 = ⊥`. -/ def degree (p : R[X]) : WithBot ℕ := p.support.max theorem supDegree_eq_degree (p : R[X]) : p.toFinsupp.supDegree WithBot.some = p.degree := max_eq_sup_coe theorem degree_lt_wf : WellFounded fun p q : R[X] => degree p < degree q := InvImage.wf degree wellFounded_lt instance : WellFoundedRelation R[X] := ⟨_, degree_lt_wf⟩ /-- `natDegree p` forces `degree p` to ℕ, by defining `natDegree 0 = 0`. -/ def natDegree (p : R[X]) : ℕ := (degree p).unbot' 0 /-- `leadingCoeff p` gives the coefficient of the highest power of `X` in `p`-/ def leadingCoeff (p : R[X]) : R := coeff p (natDegree p) /-- a polynomial is `Monic` if its leading coefficient is 1 -/ def Monic (p : R[X]) := leadingCoeff p = (1 : R) @[nontriviality] theorem monic_of_subsingleton [Subsingleton R] (p : R[X]) : Monic p := Subsingleton.elim _ _ theorem Monic.def : Monic p ↔ leadingCoeff p = 1 := Iff.rfl instance Monic.decidable [DecidableEq R] : Decidable (Monic p) := by unfold Monic; infer_instance @[simp] theorem Monic.leadingCoeff {p : R[X]} (hp : p.Monic) : leadingCoeff p = 1 := hp theorem Monic.coeff_natDegree {p : R[X]} (hp : p.Monic) : p.coeff p.natDegree = 1 := hp @[simp] theorem degree_zero : degree (0 : R[X]) = ⊥ := rfl @[simp] theorem natDegree_zero : natDegree (0 : R[X]) = 0 := rfl @[simp] theorem coeff_natDegree : coeff p (natDegree p) = leadingCoeff p := rfl @[simp] theorem degree_eq_bot : degree p = ⊥ ↔ p = 0 := ⟨fun h => support_eq_empty.1 (Finset.max_eq_bot.1 h), fun h => h.symm ▸ rfl⟩ theorem degree_ne_bot : degree p ≠ ⊥ ↔ p ≠ 0 := degree_eq_bot.not @[nontriviality] theorem degree_of_subsingleton [Subsingleton R] : degree p = ⊥ := by rw [Subsingleton.elim p 0, degree_zero] @[nontriviality] theorem natDegree_of_subsingleton [Subsingleton R] : natDegree p = 0 := by rw [Subsingleton.elim p 0, natDegree_zero] theorem degree_eq_natDegree (hp : p ≠ 0) : degree p = (natDegree p : WithBot ℕ) := by let ⟨n, hn⟩ := not_forall.1 (mt Option.eq_none_iff_forall_not_mem.2 (mt degree_eq_bot.1 hp)) have hn : degree p = some n := Classical.not_not.1 hn rw [natDegree, hn]; rfl theorem supDegree_eq_natDegree (p : R[X]) : p.toFinsupp.supDegree id = p.natDegree := by obtain rfl|h := eq_or_ne p 0 · simp apply WithBot.coe_injective rw [← AddMonoidAlgebra.supDegree_withBot_some_comp, Function.comp_id, supDegree_eq_degree, degree_eq_natDegree h, Nat.cast_withBot] rwa [support_toFinsupp, nonempty_iff_ne_empty, Ne, support_eq_empty] theorem degree_eq_iff_natDegree_eq {p : R[X]} {n : ℕ} (hp : p ≠ 0) : p.degree = n ↔ p.natDegree = n := by rw [degree_eq_natDegree hp]; exact WithBot.coe_eq_coe theorem degree_eq_iff_natDegree_eq_of_pos {p : R[X]} {n : ℕ} (hn : 0 < n) : p.degree = n ↔ p.natDegree = n := by obtain rfl|h := eq_or_ne p 0 · simp [hn.ne] · exact degree_eq_iff_natDegree_eq h theorem natDegree_eq_of_degree_eq_some {p : R[X]} {n : ℕ} (h : degree p = n) : natDegree p = n := by -- Porting note: `Nat.cast_withBot` is required. rw [natDegree, h, Nat.cast_withBot, WithBot.unbot'_coe] theorem degree_ne_of_natDegree_ne {n : ℕ} : p.natDegree ≠ n → degree p ≠ n := mt natDegree_eq_of_degree_eq_some @[simp] theorem degree_le_natDegree : degree p ≤ natDegree p := WithBot.giUnbot'Bot.gc.le_u_l _ theorem natDegree_eq_of_degree_eq [Semiring S] {q : S[X]} (h : degree p = degree q) : natDegree p = natDegree q := by unfold natDegree; rw [h] theorem le_degree_of_ne_zero (h : coeff p n ≠ 0) : (n : WithBot ℕ) ≤ degree p := by rw [Nat.cast_withBot] exact Finset.le_sup (mem_support_iff.2 h) theorem le_natDegree_of_ne_zero (h : coeff p n ≠ 0) : n ≤ natDegree p := by rw [← Nat.cast_le (α := WithBot ℕ), ← degree_eq_natDegree] · exact le_degree_of_ne_zero h · rintro rfl exact h rfl theorem le_natDegree_of_mem_supp (a : ℕ) : a ∈ p.support → a ≤ natDegree p := le_natDegree_of_ne_zero ∘ mem_support_iff.mp theorem degree_eq_of_le_of_coeff_ne_zero (pn : p.degree ≤ n) (p1 : p.coeff n ≠ 0) : p.degree = n := pn.antisymm (le_degree_of_ne_zero p1) theorem natDegree_eq_of_le_of_coeff_ne_zero (pn : p.natDegree ≤ n) (p1 : p.coeff n ≠ 0) : p.natDegree = n := pn.antisymm (le_natDegree_of_ne_zero p1) theorem degree_mono [Semiring S] {f : R[X]} {g : S[X]} (h : f.support ⊆ g.support) : f.degree ≤ g.degree := Finset.sup_mono h theorem supp_subset_range (h : natDegree p < m) : p.support ⊆ Finset.range m := fun _n hn => mem_range.2 <| (le_natDegree_of_mem_supp _ hn).trans_lt h theorem supp_subset_range_natDegree_succ : p.support ⊆ Finset.range (natDegree p + 1) := supp_subset_range (Nat.lt_succ_self _) theorem degree_le_degree (h : coeff q (natDegree p) ≠ 0) : degree p ≤ degree q := by by_cases hp : p = 0 · rw [hp, degree_zero] exact bot_le · rw [degree_eq_natDegree hp] exact le_degree_of_ne_zero h theorem natDegree_le_iff_degree_le {n : ℕ} : natDegree p ≤ n ↔ degree p ≤ n := WithBot.unbot'_le_iff (fun _ ↦ bot_le) theorem natDegree_lt_iff_degree_lt (hp : p ≠ 0) : p.natDegree < n ↔ p.degree < ↑n := WithBot.unbot'_lt_iff (absurd · (degree_eq_bot.not.mpr hp)) alias ⟨degree_le_of_natDegree_le, natDegree_le_of_degree_le⟩ := natDegree_le_iff_degree_le theorem natDegree_le_natDegree [Semiring S] {q : S[X]} (hpq : p.degree ≤ q.degree) : p.natDegree ≤ q.natDegree := WithBot.giUnbot'Bot.gc.monotone_l hpq theorem natDegree_lt_natDegree {p q : R[X]} (hp : p ≠ 0) (hpq : p.degree < q.degree) : p.natDegree < q.natDegree := by by_cases hq : q = 0 · exact (not_lt_bot <| hq ▸ hpq).elim rwa [degree_eq_natDegree hp, degree_eq_natDegree hq, Nat.cast_lt] at hpq @[simp] theorem degree_C (ha : a ≠ 0) : degree (C a) = (0 : WithBot ℕ) := by rw [degree, ← monomial_zero_left, support_monomial 0 ha, max_eq_sup_coe, sup_singleton, WithBot.coe_zero] theorem degree_C_le : degree (C a) ≤ 0 := by by_cases h : a = 0 · rw [h, C_0] exact bot_le · rw [degree_C h] theorem degree_C_lt : degree (C a) < 1 := degree_C_le.trans_lt <| WithBot.coe_lt_coe.mpr zero_lt_one theorem degree_one_le : degree (1 : R[X]) ≤ (0 : WithBot ℕ) := by rw [← C_1]; exact degree_C_le @[simp] theorem natDegree_C (a : R) : natDegree (C a) = 0 := by by_cases ha : a = 0 · have : C a = 0 := by rw [ha, C_0] rw [natDegree, degree_eq_bot.2 this, WithBot.unbot'_bot] · rw [natDegree, degree_C ha, WithBot.unbot_zero'] @[simp] theorem natDegree_one : natDegree (1 : R[X]) = 0 := natDegree_C 1 @[simp] theorem natDegree_natCast (n : ℕ) : natDegree (n : R[X]) = 0 := by simp only [← C_eq_natCast, natDegree_C] @[deprecated (since := "2024-04-17")] alias natDegree_nat_cast := natDegree_natCast theorem degree_natCast_le (n : ℕ) : degree (n : R[X]) ≤ 0 := degree_le_of_natDegree_le (by simp) @[deprecated (since := "2024-04-17")] alias degree_nat_cast_le := degree_natCast_le @[simp] theorem degree_monomial (n : ℕ) (ha : a ≠ 0) : degree (monomial n a) = n := by rw [degree, support_monomial n ha, max_singleton, Nat.cast_withBot] @[simp] theorem degree_C_mul_X_pow (n : ℕ) (ha : a ≠ 0) : degree (C a * X ^ n) = n := by rw [C_mul_X_pow_eq_monomial, degree_monomial n ha] theorem degree_C_mul_X (ha : a ≠ 0) : degree (C a * X) = 1 := by simpa only [pow_one] using degree_C_mul_X_pow 1 ha theorem degree_monomial_le (n : ℕ) (a : R) : degree (monomial n a) ≤ n := letI := Classical.decEq R if h : a = 0 then by rw [h, (monomial n).map_zero, degree_zero]; exact bot_le else le_of_eq (degree_monomial n h) theorem degree_C_mul_X_pow_le (n : ℕ) (a : R) : degree (C a * X ^ n) ≤ n := by rw [C_mul_X_pow_eq_monomial] apply degree_monomial_le theorem degree_C_mul_X_le (a : R) : degree (C a * X) ≤ 1 := by simpa only [pow_one] using degree_C_mul_X_pow_le 1 a @[simp] theorem natDegree_C_mul_X_pow (n : ℕ) (a : R) (ha : a ≠ 0) : natDegree (C a * X ^ n) = n := natDegree_eq_of_degree_eq_some (degree_C_mul_X_pow n ha) @[simp] theorem natDegree_C_mul_X (a : R) (ha : a ≠ 0) : natDegree (C a * X) = 1 := by simpa only [pow_one] using natDegree_C_mul_X_pow 1 a ha @[simp] theorem natDegree_monomial [DecidableEq R] (i : ℕ) (r : R) : natDegree (monomial i r) = if r = 0 then 0 else i := by split_ifs with hr · simp [hr] · rw [← C_mul_X_pow_eq_monomial, natDegree_C_mul_X_pow i r hr] theorem natDegree_monomial_le (a : R) {m : ℕ} : (monomial m a).natDegree ≤ m := by classical rw [Polynomial.natDegree_monomial] split_ifs exacts [Nat.zero_le _, le_rfl] theorem natDegree_monomial_eq (i : ℕ) {r : R} (r0 : r ≠ 0) : (monomial i r).natDegree = i := letI := Classical.decEq R Eq.trans (natDegree_monomial _ _) (if_neg r0) theorem coeff_eq_zero_of_degree_lt (h : degree p < n) : coeff p n = 0 := Classical.not_not.1 (mt le_degree_of_ne_zero (not_le_of_gt h)) theorem coeff_eq_zero_of_natDegree_lt {p : R[X]} {n : ℕ} (h : p.natDegree < n) : p.coeff n = 0 := by apply coeff_eq_zero_of_degree_lt by_cases hp : p = 0 · subst hp exact WithBot.bot_lt_coe n · rwa [degree_eq_natDegree hp, Nat.cast_lt] theorem ext_iff_natDegree_le {p q : R[X]} {n : ℕ} (hp : p.natDegree ≤ n) (hq : q.natDegree ≤ n) : p = q ↔ ∀ i ≤ n, p.coeff i = q.coeff i := by refine Iff.trans Polynomial.ext_iff ?_ refine forall_congr' fun i => ⟨fun h _ => h, fun h => ?_⟩ refine (le_or_lt i n).elim h fun k => ?_ exact (coeff_eq_zero_of_natDegree_lt (hp.trans_lt k)).trans (coeff_eq_zero_of_natDegree_lt (hq.trans_lt k)).symm theorem ext_iff_degree_le {p q : R[X]} {n : ℕ} (hp : p.degree ≤ n) (hq : q.degree ≤ n) : p = q ↔ ∀ i ≤ n, p.coeff i = q.coeff i := ext_iff_natDegree_le (natDegree_le_of_degree_le hp) (natDegree_le_of_degree_le hq) @[simp] theorem coeff_natDegree_succ_eq_zero {p : R[X]} : p.coeff (p.natDegree + 1) = 0 := coeff_eq_zero_of_natDegree_lt (lt_add_one _) -- We need the explicit `Decidable` argument here because an exotic one shows up in a moment! theorem ite_le_natDegree_coeff (p : R[X]) (n : ℕ) (I : Decidable (n < 1 + natDegree p)) : @ite _ (n < 1 + natDegree p) I (coeff p n) 0 = coeff p n := by split_ifs with h · rfl · exact (coeff_eq_zero_of_natDegree_lt (not_le.1 fun w => h (Nat.lt_one_add_iff.2 w))).symm theorem as_sum_support (p : R[X]) : p = ∑ i ∈ p.support, monomial i (p.coeff i) := (sum_monomial_eq p).symm theorem as_sum_support_C_mul_X_pow (p : R[X]) : p = ∑ i ∈ p.support, C (p.coeff i) * X ^ i := _root_.trans p.as_sum_support <| by simp only [C_mul_X_pow_eq_monomial] /-- We can reexpress a sum over `p.support` as a sum over `range n`, for any `n` satisfying `p.natDegree < n`. -/ theorem sum_over_range' [AddCommMonoid S] (p : R[X]) {f : ℕ → R → S} (h : ∀ n, f n 0 = 0) (n : ℕ) (w : p.natDegree < n) : p.sum f = ∑ a ∈ range n, f a (coeff p a) := by rcases p with ⟨⟩ have := supp_subset_range w simp only [Polynomial.sum, support, coeff, natDegree, degree] at this ⊢ exact Finsupp.sum_of_support_subset _ this _ fun n _hn => h n /-- We can reexpress a sum over `p.support` as a sum over `range (p.natDegree + 1)`. -/ theorem sum_over_range [AddCommMonoid S] (p : R[X]) {f : ℕ → R → S} (h : ∀ n, f n 0 = 0) : p.sum f = ∑ a ∈ range (p.natDegree + 1), f a (coeff p a) := sum_over_range' p h (p.natDegree + 1) (lt_add_one _) -- TODO this is essentially a duplicate of `sum_over_range`, and should be removed. theorem sum_fin [AddCommMonoid S] (f : ℕ → R → S) (hf : ∀ i, f i 0 = 0) {n : ℕ} {p : R[X]} (hn : p.degree < n) : (∑ i : Fin n, f i (p.coeff i)) = p.sum f := by by_cases hp : p = 0 · rw [hp, sum_zero_index, Finset.sum_eq_zero] intro i _ exact hf i rw [sum_over_range' _ hf n ((natDegree_lt_iff_degree_lt hp).mpr hn), Fin.sum_univ_eq_sum_range fun i => f i (p.coeff i)] theorem as_sum_range' (p : R[X]) (n : ℕ) (w : p.natDegree < n) : p = ∑ i ∈ range n, monomial i (coeff p i) := p.sum_monomial_eq.symm.trans <| p.sum_over_range' monomial_zero_right _ w theorem as_sum_range (p : R[X]) : p = ∑ i ∈ range (p.natDegree + 1), monomial i (coeff p i) := p.sum_monomial_eq.symm.trans <| p.sum_over_range <| monomial_zero_right theorem as_sum_range_C_mul_X_pow (p : R[X]) : p = ∑ i ∈ range (p.natDegree + 1), C (coeff p i) * X ^ i := p.as_sum_range.trans <| by simp only [C_mul_X_pow_eq_monomial] theorem coeff_ne_zero_of_eq_degree (hn : degree p = n) : coeff p n ≠ 0 := fun h => mem_support_iff.mp (mem_of_max hn) h theorem eq_X_add_C_of_degree_le_one (h : degree p ≤ 1) : p = C (p.coeff 1) * X + C (p.coeff 0) := ext fun n => Nat.casesOn n (by simp) fun n => Nat.casesOn n (by simp [coeff_C]) fun m => by -- Porting note: `by decide` → `Iff.mpr ..` have : degree p < m.succ.succ := lt_of_le_of_lt h (Iff.mpr WithBot.coe_lt_coe <| Nat.succ_lt_succ <| Nat.zero_lt_succ m) simp [coeff_eq_zero_of_degree_lt this, coeff_C, Nat.succ_ne_zero, coeff_X, Nat.succ_inj', @eq_comm ℕ 0] theorem eq_X_add_C_of_degree_eq_one (h : degree p = 1) : p = C p.leadingCoeff * X + C (p.coeff 0) := (eq_X_add_C_of_degree_le_one h.le).trans (by rw [← Nat.cast_one] at h; rw [leadingCoeff, natDegree_eq_of_degree_eq_some h]) theorem eq_X_add_C_of_natDegree_le_one (h : natDegree p ≤ 1) : p = C (p.coeff 1) * X + C (p.coeff 0) := eq_X_add_C_of_degree_le_one <| degree_le_of_natDegree_le h theorem Monic.eq_X_add_C (hm : p.Monic) (hnd : p.natDegree = 1) : p = X + C (p.coeff 0) := by rw [← one_mul X, ← C_1, ← hm.coeff_natDegree, hnd, ← eq_X_add_C_of_natDegree_le_one hnd.le] theorem exists_eq_X_add_C_of_natDegree_le_one (h : natDegree p ≤ 1) : ∃ a b, p = C a * X + C b := ⟨p.coeff 1, p.coeff 0, eq_X_add_C_of_natDegree_le_one h⟩ theorem degree_X_pow_le (n : ℕ) : degree (X ^ n : R[X]) ≤ n := by simpa only [C_1, one_mul] using degree_C_mul_X_pow_le n (1 : R) theorem degree_X_le : degree (X : R[X]) ≤ 1 := degree_monomial_le _ _ theorem natDegree_X_le : (X : R[X]).natDegree ≤ 1 := natDegree_le_of_degree_le degree_X_le theorem mem_support_C_mul_X_pow {n a : ℕ} {c : R} (h : a ∈ support (C c * X ^ n)) : a = n := mem_singleton.1 <| support_C_mul_X_pow' n c h theorem card_support_C_mul_X_pow_le_one {c : R} {n : ℕ} : card (support (C c * X ^ n)) ≤ 1 := by rw [← card_singleton n] apply card_le_card (support_C_mul_X_pow' n c) theorem card_supp_le_succ_natDegree (p : R[X]) : p.support.card ≤ p.natDegree + 1 := by rw [← Finset.card_range (p.natDegree + 1)] exact Finset.card_le_card supp_subset_range_natDegree_succ theorem le_degree_of_mem_supp (a : ℕ) : a ∈ p.support → ↑a ≤ degree p := le_degree_of_ne_zero ∘ mem_support_iff.mp theorem nonempty_support_iff : p.support.Nonempty ↔ p ≠ 0 := by rw [Ne, nonempty_iff_ne_empty, Ne, ← support_eq_empty] end Semiring section NonzeroSemiring variable [Semiring R] [Nontrivial R] {p q : R[X]} @[simp] theorem degree_one : degree (1 : R[X]) = (0 : WithBot ℕ) := degree_C one_ne_zero @[simp] theorem degree_X : degree (X : R[X]) = 1 := degree_monomial _ one_ne_zero @[simp] theorem natDegree_X : (X : R[X]).natDegree = 1 := natDegree_eq_of_degree_eq_some degree_X end NonzeroSemiring section Ring variable [Ring R] theorem coeff_mul_X_sub_C {p : R[X]} {r : R} {a : ℕ} : coeff (p * (X - C r)) (a + 1) = coeff p a - coeff p (a + 1) * r := by simp [mul_sub] @[simp] theorem degree_neg (p : R[X]) : degree (-p) = degree p := by unfold degree; rw [support_neg] theorem degree_neg_le_of_le {a : WithBot ℕ} {p : R[X]} (hp : degree p ≤ a) : degree (-p) ≤ a := p.degree_neg.le.trans hp @[simp] theorem natDegree_neg (p : R[X]) : natDegree (-p) = natDegree p := by simp [natDegree] theorem natDegree_neg_le_of_le {p : R[X]} (hp : natDegree p ≤ m) : natDegree (-p) ≤ m := (natDegree_neg p).le.trans hp @[simp] theorem natDegree_intCast (n : ℤ) : natDegree (n : R[X]) = 0 := by rw [← C_eq_intCast, natDegree_C] @[deprecated (since := "2024-04-17")] alias natDegree_int_cast := natDegree_intCast theorem degree_intCast_le (n : ℤ) : degree (n : R[X]) ≤ 0 := degree_le_of_natDegree_le (by simp) @[deprecated (since := "2024-04-17")] alias degree_int_cast_le := degree_intCast_le @[simp] theorem leadingCoeff_neg (p : R[X]) : (-p).leadingCoeff = -p.leadingCoeff := by rw [leadingCoeff, leadingCoeff, natDegree_neg, coeff_neg] end Ring section Semiring variable [Semiring R] {p : R[X]} /-- The second-highest coefficient, or 0 for constants -/ def nextCoeff (p : R[X]) : R := if p.natDegree = 0 then 0 else p.coeff (p.natDegree - 1) lemma nextCoeff_eq_zero : p.nextCoeff = 0 ↔ p.natDegree = 0 ∨ 0 < p.natDegree ∧ p.coeff (p.natDegree - 1) = 0 := by simp [nextCoeff, or_iff_not_imp_left, pos_iff_ne_zero]; aesop lemma nextCoeff_ne_zero : p.nextCoeff ≠ 0 ↔ p.natDegree ≠ 0 ∧ p.coeff (p.natDegree - 1) ≠ 0 := by simp [nextCoeff] @[simp] theorem nextCoeff_C_eq_zero (c : R) : nextCoeff (C c) = 0 := by rw [nextCoeff] simp theorem nextCoeff_of_natDegree_pos (hp : 0 < p.natDegree) : nextCoeff p = p.coeff (p.natDegree - 1) := by rw [nextCoeff, if_neg] contrapose! hp simpa variable {p q : R[X]} {ι : Type*} theorem coeff_natDegree_eq_zero_of_degree_lt (h : degree p < degree q) : coeff p (natDegree q) = 0 := coeff_eq_zero_of_degree_lt (lt_of_lt_of_le h degree_le_natDegree) theorem ne_zero_of_degree_gt {n : WithBot ℕ} (h : n < degree p) : p ≠ 0 := mt degree_eq_bot.2 h.ne_bot theorem ne_zero_of_degree_ge_degree (hpq : p.degree ≤ q.degree) (hp : p ≠ 0) : q ≠ 0 := Polynomial.ne_zero_of_degree_gt (lt_of_lt_of_le (bot_lt_iff_ne_bot.mpr (by rwa [Ne, Polynomial.degree_eq_bot])) hpq : q.degree > ⊥) theorem ne_zero_of_natDegree_gt {n : ℕ} (h : n < natDegree p) : p ≠ 0 := fun H => by simp [H, Nat.not_lt_zero] at h theorem degree_lt_degree (h : natDegree p < natDegree q) : degree p < degree q := by by_cases hp : p = 0 · simp [hp] rw [bot_lt_iff_ne_bot] intro hq simp [hp, degree_eq_bot.mp hq, lt_irrefl] at h · rwa [degree_eq_natDegree hp, degree_eq_natDegree <| ne_zero_of_natDegree_gt h, Nat.cast_lt] theorem natDegree_lt_natDegree_iff (hp : p ≠ 0) : natDegree p < natDegree q ↔ degree p < degree q := ⟨degree_lt_degree, fun h ↦ by have hq : q ≠ 0 := ne_zero_of_degree_gt h rwa [degree_eq_natDegree hp, degree_eq_natDegree hq, Nat.cast_lt] at h⟩ theorem eq_C_of_degree_le_zero (h : degree p ≤ 0) : p = C (coeff p 0) := by ext (_ | n) · simp rw [coeff_C, if_neg (Nat.succ_ne_zero _), coeff_eq_zero_of_degree_lt] exact h.trans_lt (WithBot.coe_lt_coe.2 n.succ_pos) theorem eq_C_of_degree_eq_zero (h : degree p = 0) : p = C (coeff p 0) := eq_C_of_degree_le_zero h.le theorem degree_le_zero_iff : degree p ≤ 0 ↔ p = C (coeff p 0) := ⟨eq_C_of_degree_le_zero, fun h => h.symm ▸ degree_C_le⟩ theorem degree_add_le (p q : R[X]) : degree (p + q) ≤ max (degree p) (degree q) := by simpa only [degree, ← support_toFinsupp, toFinsupp_add] using AddMonoidAlgebra.sup_support_add_le _ _ _ theorem degree_add_le_of_degree_le {p q : R[X]} {n : ℕ} (hp : degree p ≤ n) (hq : degree q ≤ n) : degree (p + q) ≤ n := (degree_add_le p q).trans <| max_le hp hq theorem degree_add_le_of_le {a b : WithBot ℕ} (hp : degree p ≤ a) (hq : degree q ≤ b) : degree (p + q) ≤ max a b := (p.degree_add_le q).trans <| max_le_max ‹_› ‹_› theorem natDegree_add_le (p q : R[X]) : natDegree (p + q) ≤ max (natDegree p) (natDegree q) := by cases' le_max_iff.1 (degree_add_le p q) with h h <;> simp [natDegree_le_natDegree h] theorem natDegree_add_le_of_degree_le {p q : R[X]} {n : ℕ} (hp : natDegree p ≤ n) (hq : natDegree q ≤ n) : natDegree (p + q) ≤ n := (natDegree_add_le p q).trans <| max_le hp hq theorem natDegree_add_le_of_le (hp : natDegree p ≤ m) (hq : natDegree q ≤ n) : natDegree (p + q) ≤ max m n := (p.natDegree_add_le q).trans <| max_le_max ‹_› ‹_› @[simp] theorem leadingCoeff_zero : leadingCoeff (0 : R[X]) = 0 := rfl @[simp] theorem leadingCoeff_eq_zero : leadingCoeff p = 0 ↔ p = 0 := ⟨fun h => Classical.by_contradiction fun hp => mt mem_support_iff.1 (Classical.not_not.2 h) (mem_of_max (degree_eq_natDegree hp)), fun h => h.symm ▸ leadingCoeff_zero⟩ theorem leadingCoeff_ne_zero : leadingCoeff p ≠ 0 ↔ p ≠ 0 := by rw [Ne, leadingCoeff_eq_zero] theorem leadingCoeff_eq_zero_iff_deg_eq_bot : leadingCoeff p = 0 ↔ degree p = ⊥ := by rw [leadingCoeff_eq_zero, degree_eq_bot] lemma natDegree_le_pred (hf : p.natDegree ≤ n) (hn : p.coeff n = 0) : p.natDegree ≤ n - 1 := by obtain _ | n := n · exact hf · refine (Nat.le_succ_iff_eq_or_le.1 hf).resolve_left fun h ↦ ?_ rw [← Nat.succ_eq_add_one, ← h, coeff_natDegree, leadingCoeff_eq_zero] at hn aesop theorem natDegree_mem_support_of_nonzero (H : p ≠ 0) : p.natDegree ∈ p.support := by rw [mem_support_iff] exact (not_congr leadingCoeff_eq_zero).mpr H theorem natDegree_eq_support_max' (h : p ≠ 0) : p.natDegree = p.support.max' (nonempty_support_iff.mpr h) := (le_max' _ _ <| natDegree_mem_support_of_nonzero h).antisymm <| max'_le _ _ _ le_natDegree_of_mem_supp theorem natDegree_C_mul_X_pow_le (a : R) (n : ℕ) : natDegree (C a * X ^ n) ≤ n := natDegree_le_iff_degree_le.2 <| degree_C_mul_X_pow_le _ _ theorem degree_add_eq_left_of_degree_lt (h : degree q < degree p) : degree (p + q) = degree p := le_antisymm (max_eq_left_of_lt h ▸ degree_add_le _ _) <| degree_le_degree <| by rw [coeff_add, coeff_natDegree_eq_zero_of_degree_lt h, add_zero] exact mt leadingCoeff_eq_zero.1 (ne_zero_of_degree_gt h) theorem degree_add_eq_right_of_degree_lt (h : degree p < degree q) : degree (p + q) = degree q := by rw [add_comm, degree_add_eq_left_of_degree_lt h] theorem natDegree_add_eq_left_of_natDegree_lt (h : natDegree q < natDegree p) : natDegree (p + q) = natDegree p := natDegree_eq_of_degree_eq (degree_add_eq_left_of_degree_lt (degree_lt_degree h)) theorem natDegree_add_eq_right_of_natDegree_lt (h : natDegree p < natDegree q) : natDegree (p + q) = natDegree q := natDegree_eq_of_degree_eq (degree_add_eq_right_of_degree_lt (degree_lt_degree h)) theorem degree_add_C (hp : 0 < degree p) : degree (p + C a) = degree p := add_comm (C a) p ▸ degree_add_eq_right_of_degree_lt <| lt_of_le_of_lt degree_C_le hp @[simp] theorem natDegree_add_C {a : R} : (p + C a).natDegree = p.natDegree := by rcases eq_or_ne p 0 with rfl | hp · simp by_cases hpd : p.degree ≤ 0 · rw [eq_C_of_degree_le_zero hpd, ← C_add, natDegree_C, natDegree_C] · rw [not_le, degree_eq_natDegree hp, Nat.cast_pos, ← natDegree_C a] at hpd exact natDegree_add_eq_left_of_natDegree_lt hpd @[simp] theorem natDegree_C_add {a : R} : (C a + p).natDegree = p.natDegree := by simp [add_comm _ p] theorem degree_add_eq_of_leadingCoeff_add_ne_zero (h : leadingCoeff p + leadingCoeff q ≠ 0) : degree (p + q) = max p.degree q.degree := le_antisymm (degree_add_le _ _) <| match lt_trichotomy (degree p) (degree q) with | Or.inl hlt => by rw [degree_add_eq_right_of_degree_lt hlt, max_eq_right_of_lt hlt] | Or.inr (Or.inl HEq) => le_of_not_gt fun hlt : max (degree p) (degree q) > degree (p + q) => h <| show leadingCoeff p + leadingCoeff q = 0 by rw [HEq, max_self] at hlt rw [leadingCoeff, leadingCoeff, natDegree_eq_of_degree_eq HEq, ← coeff_add] exact coeff_natDegree_eq_zero_of_degree_lt hlt | Or.inr (Or.inr hlt) => by rw [degree_add_eq_left_of_degree_lt hlt, max_eq_left_of_lt hlt] lemma natDegree_eq_of_natDegree_add_lt_left (p q : R[X]) (H : natDegree (p + q) < natDegree p) : natDegree p = natDegree q := by by_contra h cases Nat.lt_or_lt_of_ne h with | inl h => exact lt_asymm h (by rwa [natDegree_add_eq_right_of_natDegree_lt h] at H) | inr h => rw [natDegree_add_eq_left_of_natDegree_lt h] at H exact LT.lt.false H lemma natDegree_eq_of_natDegree_add_lt_right (p q : R[X]) (H : natDegree (p + q) < natDegree q) : natDegree p = natDegree q := (natDegree_eq_of_natDegree_add_lt_left q p (add_comm p q ▸ H)).symm lemma natDegree_eq_of_natDegree_add_eq_zero (p q : R[X]) (H : natDegree (p + q) = 0) : natDegree p = natDegree q := by by_cases h₁ : natDegree p = 0; on_goal 1 => by_cases h₂ : natDegree q = 0 · exact h₁.trans h₂.symm · apply natDegree_eq_of_natDegree_add_lt_right; rwa [H, Nat.pos_iff_ne_zero] · apply natDegree_eq_of_natDegree_add_lt_left; rwa [H, Nat.pos_iff_ne_zero] theorem degree_erase_le (p : R[X]) (n : ℕ) : degree (p.erase n) ≤ degree p := by rcases p with ⟨p⟩ simp only [erase_def, degree, coeff, support] -- Porting note: simpler convert-free proof to be explicit about definition unfolding apply sup_mono rw [Finsupp.support_erase] apply Finset.erase_subset theorem degree_erase_lt (hp : p ≠ 0) : degree (p.erase (natDegree p)) < degree p := by apply lt_of_le_of_ne (degree_erase_le _ _) rw [degree_eq_natDegree hp, degree, support_erase] exact fun h => not_mem_erase _ _ (mem_of_max h) theorem degree_update_le (p : R[X]) (n : ℕ) (a : R) : degree (p.update n a) ≤ max (degree p) n := by classical rw [degree, support_update] split_ifs · exact (Finset.max_mono (erase_subset _ _)).trans (le_max_left _ _) · rw [max_insert, max_comm] exact le_rfl theorem degree_sum_le (s : Finset ι) (f : ι → R[X]) : degree (∑ i ∈ s, f i) ≤ s.sup fun b => degree (f b) := Finset.cons_induction_on s (by simp only [sum_empty, sup_empty, degree_zero, le_refl]) fun a s has ih => calc degree (∑ i ∈ cons a s has, f i) ≤ max (degree (f a)) (degree (∑ i ∈ s, f i)) := by rw [Finset.sum_cons]; exact degree_add_le _ _ _ ≤ _ := by rw [sup_cons, sup_eq_max]; exact max_le_max le_rfl ih theorem degree_mul_le (p q : R[X]) : degree (p * q) ≤ degree p + degree q := by simpa only [degree, ← support_toFinsupp, toFinsupp_mul] using AddMonoidAlgebra.sup_support_mul_le (WithBot.coe_add _ _).le _ _ theorem degree_mul_le_of_le {a b : WithBot ℕ} (hp : degree p ≤ a) (hq : degree q ≤ b) : degree (p * q) ≤ a + b := (p.degree_mul_le _).trans <| add_le_add ‹_› ‹_› theorem degree_pow_le (p : R[X]) : ∀ n : ℕ, degree (p ^ n) ≤ n • degree p | 0 => by rw [pow_zero, zero_nsmul]; exact degree_one_le | n + 1 => calc degree (p ^ (n + 1)) ≤ degree (p ^ n) + degree p := by rw [pow_succ]; exact degree_mul_le _ _ _ ≤ _ := by rw [succ_nsmul]; exact add_le_add_right (degree_pow_le _ _) _ theorem degree_pow_le_of_le {a : WithBot ℕ} (b : ℕ) (hp : degree p ≤ a) : degree (p ^ b) ≤ b * a := by induction b with | zero => simp [degree_one_le] | succ n hn => rw [Nat.cast_succ, add_mul, one_mul, pow_succ] exact degree_mul_le_of_le hn hp @[simp] theorem leadingCoeff_monomial (a : R) (n : ℕ) : leadingCoeff (monomial n a) = a := by classical by_cases ha : a = 0 · simp only [ha, (monomial n).map_zero, leadingCoeff_zero] · rw [leadingCoeff, natDegree_monomial, if_neg ha, coeff_monomial] simp theorem leadingCoeff_C_mul_X_pow (a : R) (n : ℕ) : leadingCoeff (C a * X ^ n) = a := by rw [C_mul_X_pow_eq_monomial, leadingCoeff_monomial] theorem leadingCoeff_C_mul_X (a : R) : leadingCoeff (C a * X) = a := by simpa only [pow_one] using leadingCoeff_C_mul_X_pow a 1 @[simp] theorem leadingCoeff_C (a : R) : leadingCoeff (C a) = a := leadingCoeff_monomial a 0 -- @[simp] -- Porting note (#10618): simp can prove this theorem leadingCoeff_X_pow (n : ℕ) : leadingCoeff ((X : R[X]) ^ n) = 1 := by simpa only [C_1, one_mul] using leadingCoeff_C_mul_X_pow (1 : R) n -- @[simp] -- Porting note (#10618): simp can prove this theorem leadingCoeff_X : leadingCoeff (X : R[X]) = 1 := by simpa only [pow_one] using @leadingCoeff_X_pow R _ 1 @[simp] theorem monic_X_pow (n : ℕ) : Monic (X ^ n : R[X]) := leadingCoeff_X_pow n @[simp] theorem monic_X : Monic (X : R[X]) := leadingCoeff_X -- @[simp] -- Porting note (#10618): simp can prove this theorem leadingCoeff_one : leadingCoeff (1 : R[X]) = 1 := leadingCoeff_C 1 @[simp] theorem monic_one : Monic (1 : R[X]) := leadingCoeff_C _ theorem Monic.ne_zero {R : Type*} [Semiring R] [Nontrivial R] {p : R[X]} (hp : p.Monic) : p ≠ 0 := by rintro rfl simp [Monic] at hp theorem Monic.ne_zero_of_ne (h : (0 : R) ≠ 1) {p : R[X]} (hp : p.Monic) : p ≠ 0 := by nontriviality R exact hp.ne_zero theorem monic_of_natDegree_le_of_coeff_eq_one (n : ℕ) (pn : p.natDegree ≤ n) (p1 : p.coeff n = 1) : Monic p := by unfold Monic nontriviality refine (congr_arg _ <| natDegree_eq_of_le_of_coeff_ne_zero pn ?_).trans p1 exact ne_of_eq_of_ne p1 one_ne_zero theorem monic_of_degree_le_of_coeff_eq_one (n : ℕ) (pn : p.degree ≤ n) (p1 : p.coeff n = 1) : Monic p := monic_of_natDegree_le_of_coeff_eq_one n (natDegree_le_of_degree_le pn) p1 theorem Monic.ne_zero_of_polynomial_ne {r} (hp : Monic p) (hne : q ≠ r) : p ≠ 0 := haveI := Nontrivial.of_polynomial_ne hne hp.ne_zero theorem leadingCoeff_add_of_degree_lt (h : degree p < degree q) : leadingCoeff (p + q) = leadingCoeff q := by have : coeff p (natDegree q) = 0 := coeff_natDegree_eq_zero_of_degree_lt h simp only [leadingCoeff, natDegree_eq_of_degree_eq (degree_add_eq_right_of_degree_lt h), this, coeff_add, zero_add] theorem leadingCoeff_add_of_degree_lt' (h : degree q < degree p) : leadingCoeff (p + q) = leadingCoeff p := by rw [add_comm] exact leadingCoeff_add_of_degree_lt h theorem leadingCoeff_add_of_degree_eq (h : degree p = degree q) (hlc : leadingCoeff p + leadingCoeff q ≠ 0) : leadingCoeff (p + q) = leadingCoeff p + leadingCoeff q := by have : natDegree (p + q) = natDegree p := by apply natDegree_eq_of_degree_eq rw [degree_add_eq_of_leadingCoeff_add_ne_zero hlc, h, max_self] simp only [leadingCoeff, this, natDegree_eq_of_degree_eq h, coeff_add] @[simp] theorem coeff_mul_degree_add_degree (p q : R[X]) : coeff (p * q) (natDegree p + natDegree q) = leadingCoeff p * leadingCoeff q := calc coeff (p * q) (natDegree p + natDegree q) = ∑ x ∈ antidiagonal (natDegree p + natDegree q), coeff p x.1 * coeff q x.2 := coeff_mul _ _ _ _ = coeff p (natDegree p) * coeff q (natDegree q) := by refine Finset.sum_eq_single (natDegree p, natDegree q) ?_ ?_ · rintro ⟨i, j⟩ h₁ h₂ rw [mem_antidiagonal] at h₁ by_cases H : natDegree p < i · rw [coeff_eq_zero_of_degree_lt (lt_of_le_of_lt degree_le_natDegree (WithBot.coe_lt_coe.2 H)), zero_mul] · rw [not_lt_iff_eq_or_lt] at H cases' H with H H · subst H rw [add_left_cancel_iff] at h₁ dsimp at h₁ subst h₁ exact (h₂ rfl).elim · suffices natDegree q < j by rw [coeff_eq_zero_of_degree_lt (lt_of_le_of_lt degree_le_natDegree (WithBot.coe_lt_coe.2 this)), mul_zero] by_contra! H' exact ne_of_lt (Nat.lt_of_lt_of_le (Nat.add_lt_add_right H j) (Nat.add_le_add_left H' _)) h₁ · intro H exfalso apply H rw [mem_antidiagonal] theorem degree_mul' (h : leadingCoeff p * leadingCoeff q ≠ 0) : degree (p * q) = degree p + degree q := have hp : p ≠ 0 := by refine mt ?_ h; exact fun hp => by rw [hp, leadingCoeff_zero, zero_mul] have hq : q ≠ 0 := by refine mt ?_ h; exact fun hq => by rw [hq, leadingCoeff_zero, mul_zero] le_antisymm (degree_mul_le _ _) (by rw [degree_eq_natDegree hp, degree_eq_natDegree hq] refine le_degree_of_ne_zero (n := natDegree p + natDegree q) ?_ rwa [coeff_mul_degree_add_degree]) theorem Monic.degree_mul (hq : Monic q) : degree (p * q) = degree p + degree q := letI := Classical.decEq R if hp : p = 0 then by simp [hp] else degree_mul' <| by rwa [hq.leadingCoeff, mul_one, Ne, leadingCoeff_eq_zero] theorem natDegree_mul' (h : leadingCoeff p * leadingCoeff q ≠ 0) : natDegree (p * q) = natDegree p + natDegree q := have hp : p ≠ 0 := mt leadingCoeff_eq_zero.2 fun h₁ => h <| by rw [h₁, zero_mul] have hq : q ≠ 0 := mt leadingCoeff_eq_zero.2 fun h₁ => h <| by rw [h₁, mul_zero] natDegree_eq_of_degree_eq_some <| by rw [degree_mul' h, Nat.cast_add, degree_eq_natDegree hp, degree_eq_natDegree hq] theorem leadingCoeff_mul' (h : leadingCoeff p * leadingCoeff q ≠ 0) : leadingCoeff (p * q) = leadingCoeff p * leadingCoeff q := by unfold leadingCoeff rw [natDegree_mul' h, coeff_mul_degree_add_degree] rfl theorem monomial_natDegree_leadingCoeff_eq_self (h : p.support.card ≤ 1) : monomial p.natDegree p.leadingCoeff = p := by classical rcases card_support_le_one_iff_monomial.1 h with ⟨n, a, rfl⟩ by_cases ha : a = 0 <;> simp [ha] theorem C_mul_X_pow_eq_self (h : p.support.card ≤ 1) : C p.leadingCoeff * X ^ p.natDegree = p := by rw [C_mul_X_pow_eq_monomial, monomial_natDegree_leadingCoeff_eq_self h] theorem leadingCoeff_pow' : leadingCoeff p ^ n ≠ 0 → leadingCoeff (p ^ n) = leadingCoeff p ^ n := Nat.recOn n (by simp) fun n ih h => by have h₁ : leadingCoeff p ^ n ≠ 0 := fun h₁ => h <| by rw [pow_succ, h₁, zero_mul] have h₂ : leadingCoeff p * leadingCoeff (p ^ n) ≠ 0 := by rwa [pow_succ', ← ih h₁] at h rw [pow_succ', pow_succ', leadingCoeff_mul' h₂, ih h₁] theorem degree_pow' : ∀ {n : ℕ}, leadingCoeff p ^ n ≠ 0 → degree (p ^ n) = n • degree p | 0 => fun h => by rw [pow_zero, ← C_1] at *; rw [degree_C h, zero_nsmul] | n + 1 => fun h => by have h₁ : leadingCoeff p ^ n ≠ 0 := fun h₁ => h <| by rw [pow_succ, h₁, zero_mul] have h₂ : leadingCoeff (p ^ n) * leadingCoeff p ≠ 0 := by rwa [pow_succ, ← leadingCoeff_pow' h₁] at h rw [pow_succ, degree_mul' h₂, succ_nsmul, degree_pow' h₁] theorem natDegree_pow' {n : ℕ} (h : leadingCoeff p ^ n ≠ 0) : natDegree (p ^ n) = n * natDegree p := letI := Classical.decEq R if hp0 : p = 0 then if hn0 : n = 0 then by simp [*] else by rw [hp0, zero_pow hn0]; simp else have hpn : p ^ n ≠ 0 := fun hpn0 => by have h1 := h rw [← leadingCoeff_pow' h1, hpn0, leadingCoeff_zero] at h; exact h rfl Option.some_inj.1 <| show (natDegree (p ^ n) : WithBot ℕ) = (n * natDegree p : ℕ) by rw [← degree_eq_natDegree hpn, degree_pow' h, degree_eq_natDegree hp0]; simp theorem leadingCoeff_monic_mul {p q : R[X]} (hp : Monic p) : leadingCoeff (p * q) = leadingCoeff q := by rcases eq_or_ne q 0 with (rfl | H) · simp · rw [leadingCoeff_mul', hp.leadingCoeff, one_mul] rwa [hp.leadingCoeff, one_mul, Ne, leadingCoeff_eq_zero] theorem leadingCoeff_mul_monic {p q : R[X]} (hq : Monic q) : leadingCoeff (p * q) = leadingCoeff p := letI := Classical.decEq R Decidable.byCases (fun H : leadingCoeff p = 0 => by rw [H, leadingCoeff_eq_zero.1 H, zero_mul, leadingCoeff_zero]) fun H : leadingCoeff p ≠ 0 => by rw [leadingCoeff_mul', hq.leadingCoeff, mul_one] rwa [hq.leadingCoeff, mul_one] @[simp] theorem leadingCoeff_mul_X_pow {p : R[X]} {n : ℕ} : leadingCoeff (p * X ^ n) = leadingCoeff p := leadingCoeff_mul_monic (monic_X_pow n) @[simp] theorem leadingCoeff_mul_X {p : R[X]} : leadingCoeff (p * X) = leadingCoeff p := leadingCoeff_mul_monic monic_X theorem natDegree_mul_le {p q : R[X]} : natDegree (p * q) ≤ natDegree p + natDegree q := by apply natDegree_le_of_degree_le apply le_trans (degree_mul_le p q) rw [Nat.cast_add] apply add_le_add <;> apply degree_le_natDegree theorem natDegree_mul_le_of_le (hp : natDegree p ≤ m) (hg : natDegree q ≤ n) : natDegree (p * q) ≤ m + n := natDegree_mul_le.trans <| add_le_add ‹_› ‹_› theorem natDegree_pow_le {p : R[X]} {n : ℕ} : (p ^ n).natDegree ≤ n * p.natDegree := by induction' n with i hi · simp · rw [pow_succ, Nat.succ_mul] apply le_trans natDegree_mul_le exact add_le_add_right hi _ theorem natDegree_pow_le_of_le (n : ℕ) (hp : natDegree p ≤ m) : natDegree (p ^ n) ≤ n * m := natDegree_pow_le.trans (Nat.mul_le_mul le_rfl ‹_›) @[simp] theorem coeff_pow_mul_natDegree (p : R[X]) (n : ℕ) : (p ^ n).coeff (n * p.natDegree) = p.leadingCoeff ^ n := by induction' n with i hi · simp · rw [pow_succ, pow_succ, Nat.succ_mul] by_cases hp1 : p.leadingCoeff ^ i = 0 · rw [hp1, zero_mul] by_cases hp2 : p ^ i = 0 · rw [hp2, zero_mul, coeff_zero] · apply coeff_eq_zero_of_natDegree_lt have h1 : (p ^ i).natDegree < i * p.natDegree := by refine lt_of_le_of_ne natDegree_pow_le fun h => hp2 ?_ rw [← h, hp1] at hi exact leadingCoeff_eq_zero.mp hi calc (p ^ i * p).natDegree ≤ (p ^ i).natDegree + p.natDegree := natDegree_mul_le _ < i * p.natDegree + p.natDegree := add_lt_add_right h1 _ · rw [← natDegree_pow' hp1, ← leadingCoeff_pow' hp1] exact coeff_mul_degree_add_degree _ _ theorem coeff_mul_add_eq_of_natDegree_le {df dg : ℕ} {f g : R[X]} (hdf : natDegree f ≤ df) (hdg : natDegree g ≤ dg) : (f * g).coeff (df + dg) = f.coeff df * g.coeff dg := by rw [coeff_mul, Finset.sum_eq_single_of_mem (df, dg)] · rw [mem_antidiagonal] rintro ⟨df', dg'⟩ hmem hne obtain h | hdf' := lt_or_le df df' · rw [coeff_eq_zero_of_natDegree_lt (hdf.trans_lt h), zero_mul] obtain h | hdg' := lt_or_le dg dg' · rw [coeff_eq_zero_of_natDegree_lt (hdg.trans_lt h), mul_zero] obtain ⟨rfl, rfl⟩ := (add_eq_add_iff_eq_and_eq hdf' hdg').mp (mem_antidiagonal.1 hmem) exact (hne rfl).elim theorem zero_le_degree_iff : 0 ≤ degree p ↔ p ≠ 0 := by rw [← not_lt, Nat.WithBot.lt_zero_iff, degree_eq_bot] theorem natDegree_eq_zero_iff_degree_le_zero : p.natDegree = 0 ↔ p.degree ≤ 0 := by rw [← nonpos_iff_eq_zero, natDegree_le_iff_degree_le, Nat.cast_zero] theorem degree_zero_le : degree (0 : R[X]) ≤ 0 := natDegree_eq_zero_iff_degree_le_zero.mp rfl theorem degree_le_iff_coeff_zero (f : R[X]) (n : WithBot ℕ) : degree f ≤ n ↔ ∀ m : ℕ, n < m → coeff f m = 0 := by -- Porting note: `Nat.cast_withBot` is required. simp only [degree, Finset.max, Finset.sup_le_iff, mem_support_iff, Ne, ← not_le, not_imp_comm, Nat.cast_withBot] theorem degree_lt_iff_coeff_zero (f : R[X]) (n : ℕ) : degree f < n ↔ ∀ m : ℕ, n ≤ m → coeff f m = 0 := by simp only [degree, Finset.sup_lt_iff (WithBot.bot_lt_coe n), mem_support_iff, WithBot.coe_lt_coe, ← @not_le ℕ, max_eq_sup_coe, Nat.cast_withBot, Ne, not_imp_not] theorem degree_smul_le (a : R) (p : R[X]) : degree (a • p) ≤ degree p := by refine (degree_le_iff_coeff_zero _ _).2 fun m hm => ?_ rw [degree_lt_iff_coeff_zero] at hm simp [hm m le_rfl] theorem natDegree_smul_le (a : R) (p : R[X]) : natDegree (a • p) ≤ natDegree p := natDegree_le_natDegree (degree_smul_le a p) theorem degree_lt_degree_mul_X (hp : p ≠ 0) : p.degree < (p * X).degree := by haveI := Nontrivial.of_polynomial_ne hp have : leadingCoeff p * leadingCoeff X ≠ 0 := by simpa erw [degree_mul' this, degree_eq_natDegree hp, degree_X, ← WithBot.coe_one, ← WithBot.coe_add, WithBot.coe_lt_coe]; exact Nat.lt_succ_self _ theorem natDegree_pos_iff_degree_pos : 0 < natDegree p ↔ 0 < degree p := lt_iff_lt_of_le_iff_le natDegree_le_iff_degree_le theorem eq_C_of_natDegree_le_zero (h : natDegree p ≤ 0) : p = C (coeff p 0) := eq_C_of_degree_le_zero <| degree_le_of_natDegree_le h theorem eq_C_of_natDegree_eq_zero (h : natDegree p = 0) : p = C (coeff p 0) := eq_C_of_natDegree_le_zero h.le lemma natDegree_eq_zero {p : R[X]} : p.natDegree = 0 ↔ ∃ x, C x = p := ⟨fun h ↦ ⟨_, (eq_C_of_natDegree_eq_zero h).symm⟩, by aesop⟩ theorem eq_C_coeff_zero_iff_natDegree_eq_zero : p = C (p.coeff 0) ↔ p.natDegree = 0 := ⟨fun h ↦ by rw [h, natDegree_C], eq_C_of_natDegree_eq_zero⟩ theorem eq_one_of_monic_natDegree_zero (hf : p.Monic) (hfd : p.natDegree = 0) : p = 1 := by rw [Monic.def, leadingCoeff, hfd] at hf rw [eq_C_of_natDegree_eq_zero hfd, hf, map_one] theorem Monic.natDegree_eq_zero (hf : p.Monic) : p.natDegree = 0 ↔ p = 1 := ⟨eq_one_of_monic_natDegree_zero hf, by rintro rfl; simp⟩ theorem ne_zero_of_coe_le_degree (hdeg : ↑n ≤ p.degree) : p ≠ 0 := zero_le_degree_iff.mp <| (WithBot.coe_le_coe.mpr n.zero_le).trans hdeg theorem le_natDegree_of_coe_le_degree (hdeg : ↑n ≤ p.degree) : n ≤ p.natDegree := -- Porting note: `.. ▸ ..` → `rwa [..] at ..` WithBot.coe_le_coe.mp <| by rwa [degree_eq_natDegree <| ne_zero_of_coe_le_degree hdeg] at hdeg theorem degree_sum_fin_lt {n : ℕ} (f : Fin n → R) : degree (∑ i : Fin n, C (f i) * X ^ (i : ℕ)) < n := (degree_sum_le _ _).trans_lt <| (Finset.sup_lt_iff <| WithBot.bot_lt_coe n).2 fun k _hk => (degree_C_mul_X_pow_le _ _).trans_lt <| WithBot.coe_lt_coe.2 k.is_lt theorem degree_linear_le : degree (C a * X + C b) ≤ 1 := degree_add_le_of_degree_le (degree_C_mul_X_le _) <| le_trans degree_C_le Nat.WithBot.coe_nonneg theorem degree_linear_lt : degree (C a * X + C b) < 2 := degree_linear_le.trans_lt <| WithBot.coe_lt_coe.mpr one_lt_two theorem degree_C_lt_degree_C_mul_X (ha : a ≠ 0) : degree (C b) < degree (C a * X) := by simpa only [degree_C_mul_X ha] using degree_C_lt @[simp] theorem degree_linear (ha : a ≠ 0) : degree (C a * X + C b) = 1 := by rw [degree_add_eq_left_of_degree_lt <| degree_C_lt_degree_C_mul_X ha, degree_C_mul_X ha] theorem natDegree_linear_le : natDegree (C a * X + C b) ≤ 1 := natDegree_le_of_degree_le degree_linear_le theorem natDegree_linear (ha : a ≠ 0) : natDegree (C a * X + C b) = 1 := by rw [natDegree_add_C, natDegree_C_mul_X a ha] @[simp] theorem leadingCoeff_linear (ha : a ≠ 0) : leadingCoeff (C a * X + C b) = a := by rw [add_comm, leadingCoeff_add_of_degree_lt (degree_C_lt_degree_C_mul_X ha), leadingCoeff_C_mul_X] theorem degree_quadratic_le : degree (C a * X ^ 2 + C b * X + C c) ≤ 2 := by simpa only [add_assoc] using degree_add_le_of_degree_le (degree_C_mul_X_pow_le 2 a) (le_trans degree_linear_le <| WithBot.coe_le_coe.mpr one_le_two) theorem degree_quadratic_lt : degree (C a * X ^ 2 + C b * X + C c) < 3 := degree_quadratic_le.trans_lt <| WithBot.coe_lt_coe.mpr <| lt_add_one 2 theorem degree_linear_lt_degree_C_mul_X_sq (ha : a ≠ 0) : degree (C b * X + C c) < degree (C a * X ^ 2) := by simpa only [degree_C_mul_X_pow 2 ha] using degree_linear_lt @[simp] theorem degree_quadratic (ha : a ≠ 0) : degree (C a * X ^ 2 + C b * X + C c) = 2 := by rw [add_assoc, degree_add_eq_left_of_degree_lt <| degree_linear_lt_degree_C_mul_X_sq ha, degree_C_mul_X_pow 2 ha] rfl theorem natDegree_quadratic_le : natDegree (C a * X ^ 2 + C b * X + C c) ≤ 2 := natDegree_le_of_degree_le degree_quadratic_le theorem natDegree_quadratic (ha : a ≠ 0) : natDegree (C a * X ^ 2 + C b * X + C c) = 2 := natDegree_eq_of_degree_eq_some <| degree_quadratic ha @[simp] theorem leadingCoeff_quadratic (ha : a ≠ 0) : leadingCoeff (C a * X ^ 2 + C b * X + C c) = a := by rw [add_assoc, add_comm, leadingCoeff_add_of_degree_lt <| degree_linear_lt_degree_C_mul_X_sq ha, leadingCoeff_C_mul_X_pow] theorem degree_cubic_le : degree (C a * X ^ 3 + C b * X ^ 2 + C c * X + C d) ≤ 3 := by simpa only [add_assoc] using degree_add_le_of_degree_le (degree_C_mul_X_pow_le 3 a) (le_trans degree_quadratic_le <| WithBot.coe_le_coe.mpr <| Nat.le_succ 2) theorem degree_cubic_lt : degree (C a * X ^ 3 + C b * X ^ 2 + C c * X + C d) < 4 := degree_cubic_le.trans_lt <| WithBot.coe_lt_coe.mpr <| lt_add_one 3 theorem degree_quadratic_lt_degree_C_mul_X_cb (ha : a ≠ 0) : degree (C b * X ^ 2 + C c * X + C d) < degree (C a * X ^ 3) := by simpa only [degree_C_mul_X_pow 3 ha] using degree_quadratic_lt @[simp] theorem degree_cubic (ha : a ≠ 0) : degree (C a * X ^ 3 + C b * X ^ 2 + C c * X + C d) = 3 := by rw [add_assoc, add_assoc, ← add_assoc (C b * X ^ 2), degree_add_eq_left_of_degree_lt <| degree_quadratic_lt_degree_C_mul_X_cb ha, degree_C_mul_X_pow 3 ha] rfl theorem natDegree_cubic_le : natDegree (C a * X ^ 3 + C b * X ^ 2 + C c * X + C d) ≤ 3 := natDegree_le_of_degree_le degree_cubic_le theorem natDegree_cubic (ha : a ≠ 0) : natDegree (C a * X ^ 3 + C b * X ^ 2 + C c * X + C d) = 3 := natDegree_eq_of_degree_eq_some <| degree_cubic ha @[simp] theorem leadingCoeff_cubic (ha : a ≠ 0) : leadingCoeff (C a * X ^ 3 + C b * X ^ 2 + C c * X + C d) = a := by rw [add_assoc, add_assoc, ← add_assoc (C b * X ^ 2), add_comm, leadingCoeff_add_of_degree_lt <| degree_quadratic_lt_degree_C_mul_X_cb ha, leadingCoeff_C_mul_X_pow] end Semiring section NontrivialSemiring variable [Semiring R] [Nontrivial R] {p q : R[X]} (n : ℕ) @[simp] theorem degree_X_pow : degree ((X : R[X]) ^ n) = n := by rw [X_pow_eq_monomial, degree_monomial _ (one_ne_zero' R)] @[simp] theorem natDegree_X_pow : natDegree ((X : R[X]) ^ n) = n := natDegree_eq_of_degree_eq_some (degree_X_pow n) @[simp] lemma natDegree_mul_X (hp : p ≠ 0) : natDegree (p * X) = natDegree p + 1 := by rw [natDegree_mul' (by simpa), natDegree_X] @[simp] lemma natDegree_X_mul (hp : p ≠ 0) : natDegree (X * p) = natDegree p + 1 := by rw [commute_X p, natDegree_mul_X hp] @[simp] lemma natDegree_mul_X_pow (hp : p ≠ 0) : natDegree (p * X ^ n) = natDegree p + n := by rw [natDegree_mul' (by simpa), natDegree_X_pow] @[simp] lemma natDegree_X_pow_mul (hp : p ≠ 0) : natDegree (X ^ n * p) = natDegree p + n := by rw [commute_X_pow, natDegree_mul_X_pow n hp] -- This lemma explicitly does not require the `Nontrivial R` assumption. theorem natDegree_X_pow_le {R : Type*} [Semiring R] (n : ℕ) : (X ^ n : R[X]).natDegree ≤ n := by nontriviality R rw [Polynomial.natDegree_X_pow] theorem not_isUnit_X : ¬IsUnit (X : R[X]) := fun ⟨⟨_, g, _hfg, hgf⟩, rfl⟩ => zero_ne_one' R <| by rw [← coeff_one_zero, ← hgf] simp @[simp] theorem degree_mul_X : degree (p * X) = degree p + 1 := by simp [monic_X.degree_mul] @[simp] theorem degree_mul_X_pow : degree (p * X ^ n) = degree p + n := by simp [(monic_X_pow n).degree_mul] end NontrivialSemiring section Ring variable [Ring R] {p q : R[X]} theorem degree_sub_C (hp : 0 < degree p) : degree (p - C a) = degree p := by rw [sub_eq_add_neg, ← C_neg, degree_add_C hp] @[simp] theorem natDegree_sub_C {a : R} : natDegree (p - C a) = natDegree p := by rw [sub_eq_add_neg, ← C_neg, natDegree_add_C] theorem degree_sub_le (p q : R[X]) : degree (p - q) ≤ max (degree p) (degree q) := by simpa only [degree_neg q] using degree_add_le p (-q) theorem degree_sub_le_of_le {a b : WithBot ℕ} (hp : degree p ≤ a) (hq : degree q ≤ b) : degree (p - q) ≤ max a b := (p.degree_sub_le q).trans <| max_le_max ‹_› ‹_› theorem leadingCoeff_sub_of_degree_lt (h : Polynomial.degree q < Polynomial.degree p) : (p - q).leadingCoeff = p.leadingCoeff := by rw [← q.degree_neg] at h rw [sub_eq_add_neg, leadingCoeff_add_of_degree_lt' h] theorem leadingCoeff_sub_of_degree_lt' (h : Polynomial.degree p < Polynomial.degree q) : (p - q).leadingCoeff = -q.leadingCoeff := by rw [← q.degree_neg] at h rw [sub_eq_add_neg, leadingCoeff_add_of_degree_lt h, leadingCoeff_neg] theorem leadingCoeff_sub_of_degree_eq (h : degree p = degree q) (hlc : leadingCoeff p ≠ leadingCoeff q) : leadingCoeff (p - q) = leadingCoeff p - leadingCoeff q := by replace h : degree p = degree (-q) := by rwa [q.degree_neg] replace hlc : leadingCoeff p + leadingCoeff (-q) ≠ 0 := by rwa [← sub_ne_zero, sub_eq_add_neg, ← q.leadingCoeff_neg] at hlc rw [sub_eq_add_neg, leadingCoeff_add_of_degree_eq h hlc, leadingCoeff_neg, sub_eq_add_neg] theorem natDegree_sub_le (p q : R[X]) : natDegree (p - q) ≤ max (natDegree p) (natDegree q) := by simpa only [← natDegree_neg q] using natDegree_add_le p (-q) theorem natDegree_sub_le_of_le (hp : natDegree p ≤ m) (hq : natDegree q ≤ n) : natDegree (p - q) ≤ max m n := (p.natDegree_sub_le q).trans <| max_le_max ‹_› ‹_› theorem degree_sub_lt (hd : degree p = degree q) (hp0 : p ≠ 0) (hlc : leadingCoeff p = leadingCoeff q) : degree (p - q) < degree p := have hp : monomial (natDegree p) (leadingCoeff p) + p.erase (natDegree p) = p := monomial_add_erase _ _ have hq : monomial (natDegree q) (leadingCoeff q) + q.erase (natDegree q) = q := monomial_add_erase _ _ have hd' : natDegree p = natDegree q := by unfold natDegree; rw [hd] have hq0 : q ≠ 0 := mt degree_eq_bot.2 (hd ▸ mt degree_eq_bot.1 hp0) calc degree (p - q) = degree (erase (natDegree q) p + -erase (natDegree q) q) := by conv => lhs rw [← hp, ← hq, hlc, hd', add_sub_add_left_eq_sub, sub_eq_add_neg] _ ≤ max (degree (erase (natDegree q) p)) (degree (erase (natDegree q) q)) := (degree_neg (erase (natDegree q) q) ▸ degree_add_le _ _) _ < degree p := max_lt_iff.2 ⟨hd' ▸ degree_erase_lt hp0, hd.symm ▸ degree_erase_lt hq0⟩ theorem degree_X_sub_C_le (r : R) : (X - C r).degree ≤ 1 := (degree_sub_le _ _).trans (max_le degree_X_le (degree_C_le.trans zero_le_one)) theorem natDegree_X_sub_C_le (r : R) : (X - C r).natDegree ≤ 1 := natDegree_le_iff_degree_le.2 <| degree_X_sub_C_le r theorem degree_sub_eq_left_of_degree_lt (h : degree q < degree p) : degree (p - q) = degree p := by rw [← degree_neg q] at h rw [sub_eq_add_neg, degree_add_eq_left_of_degree_lt h] theorem degree_sub_eq_right_of_degree_lt (h : degree p < degree q) : degree (p - q) = degree q := by rw [← degree_neg q] at h rw [sub_eq_add_neg, degree_add_eq_right_of_degree_lt h, degree_neg] theorem natDegree_sub_eq_left_of_natDegree_lt (h : natDegree q < natDegree p) : natDegree (p - q) = natDegree p := natDegree_eq_of_degree_eq (degree_sub_eq_left_of_degree_lt (degree_lt_degree h)) theorem natDegree_sub_eq_right_of_natDegree_lt (h : natDegree p < natDegree q) : natDegree (p - q) = natDegree q := natDegree_eq_of_degree_eq (degree_sub_eq_right_of_degree_lt (degree_lt_degree h)) end Ring section NonzeroRing variable [Nontrivial R] section Semiring variable [Semiring R] @[simp] theorem degree_X_add_C (a : R) : degree (X + C a) = 1 := by have : degree (C a) < degree (X : R[X]) := calc degree (C a) ≤ 0 := degree_C_le _ < 1 := WithBot.coe_lt_coe.mpr zero_lt_one _ = degree X := degree_X.symm rw [degree_add_eq_left_of_degree_lt this, degree_X] theorem natDegree_X_add_C (x : R) : (X + C x).natDegree = 1 := natDegree_eq_of_degree_eq_some <| degree_X_add_C x @[simp] theorem nextCoeff_X_add_C [Semiring S] (c : S) : nextCoeff (X + C c) = c := by nontriviality S simp [nextCoeff_of_natDegree_pos] theorem degree_X_pow_add_C {n : ℕ} (hn : 0 < n) (a : R) : degree ((X : R[X]) ^ n + C a) = n := by have : degree (C a) < degree ((X : R[X]) ^ n) := degree_C_le.trans_lt <| by rwa [degree_X_pow, Nat.cast_pos] rw [degree_add_eq_left_of_degree_lt this, degree_X_pow] theorem X_pow_add_C_ne_zero {n : ℕ} (hn : 0 < n) (a : R) : (X : R[X]) ^ n + C a ≠ 0 := mt degree_eq_bot.2 (show degree ((X : R[X]) ^ n + C a) ≠ ⊥ by rw [degree_X_pow_add_C hn a]; exact WithBot.coe_ne_bot) theorem X_add_C_ne_zero (r : R) : X + C r ≠ 0 := pow_one (X : R[X]) ▸ X_pow_add_C_ne_zero zero_lt_one r theorem zero_nmem_multiset_map_X_add_C {α : Type*} (m : Multiset α) (f : α → R) : (0 : R[X]) ∉ m.map fun a => X + C (f a) := fun mem => let ⟨_a, _, ha⟩ := Multiset.mem_map.mp mem X_add_C_ne_zero _ ha theorem natDegree_X_pow_add_C {n : ℕ} {r : R} : (X ^ n + C r).natDegree = n := by by_cases hn : n = 0 · rw [hn, pow_zero, ← C_1, ← RingHom.map_add, natDegree_C] · exact natDegree_eq_of_degree_eq_some (degree_X_pow_add_C (pos_iff_ne_zero.mpr hn) r) theorem X_pow_add_C_ne_one {n : ℕ} (hn : 0 < n) (a : R) : (X : R[X]) ^ n + C a ≠ 1 := fun h => hn.ne' <| by simpa only [natDegree_X_pow_add_C, natDegree_one] using congr_arg natDegree h theorem X_add_C_ne_one (r : R) : X + C r ≠ 1 := pow_one (X : R[X]) ▸ X_pow_add_C_ne_one zero_lt_one r end Semiring end NonzeroRing section Semiring variable [Semiring R] @[simp] theorem leadingCoeff_X_pow_add_C {n : ℕ} (hn : 0 < n) {r : R} : (X ^ n + C r).leadingCoeff = 1 := by nontriviality R rw [leadingCoeff, natDegree_X_pow_add_C, coeff_add, coeff_X_pow_self, coeff_C, if_neg (pos_iff_ne_zero.mp hn), add_zero] @[simp] theorem leadingCoeff_X_add_C [Semiring S] (r : S) : (X + C r).leadingCoeff = 1 := by rw [← pow_one (X : S[X]), leadingCoeff_X_pow_add_C zero_lt_one] @[simp] theorem leadingCoeff_X_pow_add_one {n : ℕ} (hn : 0 < n) : (X ^ n + 1 : R[X]).leadingCoeff = 1 := leadingCoeff_X_pow_add_C hn @[simp] theorem leadingCoeff_pow_X_add_C (r : R) (i : ℕ) : leadingCoeff ((X + C r) ^ i) = 1 := by nontriviality rw [leadingCoeff_pow'] <;> simp end Semiring section Ring variable [Ring R] @[simp] theorem leadingCoeff_X_pow_sub_C {n : ℕ} (hn : 0 < n) {r : R} : (X ^ n - C r).leadingCoeff = 1 := by rw [sub_eq_add_neg, ← map_neg C r, leadingCoeff_X_pow_add_C hn] @[simp] theorem leadingCoeff_X_pow_sub_one {n : ℕ} (hn : 0 < n) : (X ^ n - 1 : R[X]).leadingCoeff = 1 := leadingCoeff_X_pow_sub_C hn variable [Nontrivial R] @[simp] theorem degree_X_sub_C (a : R) : degree (X - C a) = 1 := by rw [sub_eq_add_neg, ← map_neg C a, degree_X_add_C] theorem natDegree_X_sub_C (x : R) : (X - C x).natDegree = 1 := by rw [natDegree_sub_C, natDegree_X] @[simp] theorem nextCoeff_X_sub_C [Ring S] (c : S) : nextCoeff (X - C c) = -c := by rw [sub_eq_add_neg, ← map_neg C c, nextCoeff_X_add_C] theorem degree_X_pow_sub_C {n : ℕ} (hn : 0 < n) (a : R) : degree ((X : R[X]) ^ n - C a) = n := by rw [sub_eq_add_neg, ← map_neg C a, degree_X_pow_add_C hn] theorem X_pow_sub_C_ne_zero {n : ℕ} (hn : 0 < n) (a : R) : (X : R[X]) ^ n - C a ≠ 0 := by rw [sub_eq_add_neg, ← map_neg C a] exact X_pow_add_C_ne_zero hn _ theorem X_sub_C_ne_zero (r : R) : X - C r ≠ 0 := pow_one (X : R[X]) ▸ X_pow_sub_C_ne_zero zero_lt_one r theorem zero_nmem_multiset_map_X_sub_C {α : Type*} (m : Multiset α) (f : α → R) : (0 : R[X]) ∉ m.map fun a => X - C (f a) := fun mem => let ⟨_a, _, ha⟩ := Multiset.mem_map.mp mem X_sub_C_ne_zero _ ha theorem natDegree_X_pow_sub_C {n : ℕ} {r : R} : (X ^ n - C r).natDegree = n := by rw [sub_eq_add_neg, ← map_neg C r, natDegree_X_pow_add_C] @[simp] theorem leadingCoeff_X_sub_C [Ring S] (r : S) : (X - C r).leadingCoeff = 1 := by rw [sub_eq_add_neg, ← map_neg C r, leadingCoeff_X_add_C] end Ring section NoZeroDivisors variable [Semiring R] [NoZeroDivisors R] {p q : R[X]} @[simp] theorem degree_mul : degree (p * q) = degree p + degree q := letI := Classical.decEq R if hp0 : p = 0 then by simp only [hp0, degree_zero, zero_mul, WithBot.bot_add] else if hq0 : q = 0 then by simp only [hq0, degree_zero, mul_zero, WithBot.add_bot] else degree_mul' <| mul_ne_zero (mt leadingCoeff_eq_zero.1 hp0) (mt leadingCoeff_eq_zero.1 hq0) /-- `degree` as a monoid homomorphism between `R[X]` and `Multiplicative (WithBot ℕ)`. This is useful to prove results about multiplication and degree. -/ def degreeMonoidHom [Nontrivial R] : R[X] →* Multiplicative (WithBot ℕ) where toFun := degree map_one' := degree_one map_mul' _ _ := degree_mul @[simp] theorem degree_pow [Nontrivial R] (p : R[X]) (n : ℕ) : degree (p ^ n) = n • degree p := map_pow (@degreeMonoidHom R _ _ _) _ _ @[simp] theorem leadingCoeff_mul (p q : R[X]) : leadingCoeff (p * q) = leadingCoeff p * leadingCoeff q := by by_cases hp : p = 0 · simp only [hp, zero_mul, leadingCoeff_zero] · by_cases hq : q = 0 · simp only [hq, mul_zero, leadingCoeff_zero] · rw [leadingCoeff_mul'] exact mul_ne_zero (mt leadingCoeff_eq_zero.1 hp) (mt leadingCoeff_eq_zero.1 hq) /-- `Polynomial.leadingCoeff` bundled as a `MonoidHom` when `R` has `NoZeroDivisors`, and thus `leadingCoeff` is multiplicative -/ def leadingCoeffHom : R[X] →* R where toFun := leadingCoeff map_one' := by simp map_mul' := leadingCoeff_mul @[simp] theorem leadingCoeffHom_apply (p : R[X]) : leadingCoeffHom p = leadingCoeff p := rfl @[simp] theorem leadingCoeff_pow (p : R[X]) (n : ℕ) : leadingCoeff (p ^ n) = leadingCoeff p ^ n := (leadingCoeffHom : R[X] →* R).map_pow p n theorem leadingCoeff_dvd_leadingCoeff {a p : R[X]} (hap : a ∣ p) : a.leadingCoeff ∣ p.leadingCoeff := map_dvd leadingCoeffHom hap end NoZeroDivisors end Polynomial