source stringlengths 17 118 | lean4 stringlengths 0 335k |
|---|---|
.lake/packages/mathlib/Mathlib/Algebra/Group/Pointwise/Set/ListOfFn.lean | import Mathlib.Data.List.OfFn
import Mathlib.Algebra.BigOperators.Group.List.Defs
import Mathlib.Algebra.Group.Pointwise.Set.Basic
/-!
# Pointwise operations with lists of sets
This file proves some lemmas about pointwise algebraic operations with lists of sets.
-/
namespace Set
variable {α : Type*} [Monoid α] {s : Set α} {n : ℕ}
open Pointwise
@[to_additive]
theorem mem_prod_list_ofFn {a : α} {s : Fin n → Set α} :
a ∈ (List.ofFn s).prod ↔ ∃ f : ∀ i : Fin n, s i, (List.ofFn fun i ↦ (f i : α)).prod = a := by
induction n generalizing a with
| zero => simp_rw [List.ofFn_zero, List.prod_nil, Fin.exists_fin_zero_pi, eq_comm, Set.mem_one]
| succ n ih =>
simp_rw [List.ofFn_succ, List.prod_cons, Fin.exists_fin_succ_pi, Fin.cons_zero, Fin.cons_succ,
mem_mul, @ih, exists_exists_eq_and, SetCoe.exists, exists_prop]
@[to_additive]
theorem mem_list_prod {l : List (Set α)} {a : α} :
a ∈ l.prod ↔
∃ l' : List (Σ s : Set α, ↥s),
List.prod (l'.map fun x ↦ (Sigma.snd x : α)) = a ∧ l'.map Sigma.fst = l := by
induction l using List.ofFnRec with | _ n f
simp only [mem_prod_list_ofFn, List.exists_iff_exists_tuple, List.map_ofFn, List.ofFn_inj',
Sigma.mk.inj_iff, and_left_comm, exists_and_left, exists_eq_left, heq_eq_eq]
constructor
· rintro ⟨fi, rfl⟩
exact ⟨fun i ↦ ⟨_, fi i⟩, rfl, rfl⟩
· rintro ⟨fi, rfl, rfl⟩
exact ⟨fun i ↦ _, rfl⟩
@[to_additive]
theorem mem_pow {a : α} {n : ℕ} :
a ∈ s ^ n ↔ ∃ f : Fin n → s, (List.ofFn fun i ↦ (f i : α)).prod = a := by
rw [← mem_prod_list_ofFn, List.ofFn_const, List.prod_replicate]
end Set |
.lake/packages/mathlib/Mathlib/Algebra/Group/Pointwise/Set/Scalar.lean | import Mathlib.Algebra.Group.Pointwise.Set.Basic
import Mathlib.Algebra.Opposites
import Mathlib.Algebra.Notation.Pi.Defs
import Mathlib.Data.Set.NAry
/-!
# Pointwise scalar operations of sets
This file defines pointwise scalar-flavored algebraic operations on sets.
## Main declarations
For sets `s` and `t` and scalar `a`:
* `s • t`: Scalar multiplication, set of all `x • y` where `x ∈ s` and `y ∈ t`.
* `s +ᵥ t`: Scalar addition, set of all `x +ᵥ y` where `x ∈ s` and `y ∈ t`.
* `s -ᵥ t`: Scalar subtraction, set of all `x -ᵥ y` where `x ∈ s` and `y ∈ t`.
* `a • s`: Scaling, set of all `a • x` where `x ∈ s`.
* `a +ᵥ s`: Translation, set of all `a +ᵥ x` where `x ∈ s`.
For `α` a semigroup/monoid, `Set α` is a semigroup/monoid.
As an unfortunate side effect, this means that `n • s`, where `n : ℕ`, is ambiguous between
pointwise scaling and repeated pointwise addition; the former has `(2 : ℕ) • {1, 2} = {2, 4}`, while
the latter has `(2 : ℕ) • {1, 2} = {2, 3, 4}`. See note [pointwise nat action].
Appropriate definitions and results are also transported to the additive theory via `to_additive`.
## Implementation notes
* The following expressions are considered in simp-normal form in a group:
`(fun h ↦ h * g) ⁻¹' s`, `(fun h ↦ g * h) ⁻¹' s`, `(fun h ↦ h * g⁻¹) ⁻¹' s`,
`(fun h ↦ g⁻¹ * h) ⁻¹' s`, `s * t`, `s⁻¹`, `(1 : Set _)` (and similarly for additive variants).
Expressions equal to one of these will be simplified.
* We put all instances in the scope `Pointwise`, so that these instances are not available by
default. Note that we do not mark them as reducible (as argued by note [reducible non-instances])
since we expect the scope to be open whenever the instances are actually used (and making the
instances reducible changes the behavior of `simp`.
## Tags
set multiplication, set addition, pointwise addition, pointwise multiplication,
pointwise subtraction
-/
assert_not_exists Set.iUnion MulAction MonoidWithZero IsOrderedMonoid
open Function MulOpposite
variable {F α β γ : Type*}
namespace Set
open Pointwise
/-! ### Translation/scaling of sets -/
section SMul
/-- The dilation of set `x • s` is defined as `{x • y | y ∈ s}` in scope `Pointwise`. -/
@[to_additive
/-- The translation of set `x +ᵥ s` is defined as `{x +ᵥ y | y ∈ s}` in scope `Pointwise`. -/]
protected def smulSet [SMul α β] : SMul α (Set β) where smul a := image (a • ·)
/-- The pointwise scalar multiplication of sets `s • t` is defined as `{x • y | x ∈ s, y ∈ t}` in
scope `Pointwise`. -/
@[to_additive
/-- The pointwise scalar addition of sets `s +ᵥ t` is defined as `{x +ᵥ y | x ∈ s, y ∈ t}` in locale
`Pointwise`. -/]
protected def smul [SMul α β] : SMul (Set α) (Set β) where smul := image2 (· • ·)
scoped[Pointwise] attribute [instance] Set.smulSet Set.smul
scoped[Pointwise] attribute [instance] Set.vaddSet Set.vadd
section SMul
variable {ι : Sort*} {κ : ι → Sort*} [SMul α β] {s s₁ s₂ : Set α} {t t₁ t₂ u : Set β} {a : α}
{b : β}
@[to_additive (attr := simp)] lemma image2_smul : image2 (· • ·) s t = s • t := rfl
@[to_additive vadd_image_prod]
lemma image_smul_prod : (fun x : α × β ↦ x.fst • x.snd) '' s ×ˢ t = s • t := image_prod _
@[to_additive] lemma mem_smul : b ∈ s • t ↔ ∃ x ∈ s, ∃ y ∈ t, x • y = b := Iff.rfl
@[to_additive] lemma smul_mem_smul : a ∈ s → b ∈ t → a • b ∈ s • t := mem_image2_of_mem
@[to_additive (attr := simp)] lemma empty_smul : (∅ : Set α) • t = ∅ := image2_empty_left
@[to_additive (attr := simp)] lemma smul_empty : s • (∅ : Set β) = ∅ := image2_empty_right
@[to_additive (attr := simp)] lemma smul_eq_empty : s • t = ∅ ↔ s = ∅ ∨ t = ∅ := image2_eq_empty_iff
@[to_additive (attr := simp)]
lemma smul_nonempty : (s • t).Nonempty ↔ s.Nonempty ∧ t.Nonempty := image2_nonempty_iff
@[to_additive] lemma Nonempty.smul : s.Nonempty → t.Nonempty → (s • t).Nonempty := .image2
@[to_additive] lemma Nonempty.of_smul_left : (s • t).Nonempty → s.Nonempty := .of_image2_left
@[to_additive] lemma Nonempty.of_smul_right : (s • t).Nonempty → t.Nonempty := .of_image2_right
@[to_additive (attr := simp low + 1)]
lemma smul_singleton : s • ({b} : Set β) = (· • b) '' s := image2_singleton_right
@[to_additive (attr := simp low + 1)]
lemma singleton_smul : ({a} : Set α) • t = a • t := image2_singleton_left
@[to_additive (attr := simp high)]
lemma singleton_smul_singleton : ({a} : Set α) • ({b} : Set β) = {a • b} := image2_singleton
@[to_additive (attr := mono, gcongr)]
lemma smul_subset_smul : s₁ ⊆ s₂ → t₁ ⊆ t₂ → s₁ • t₁ ⊆ s₂ • t₂ := image2_subset
@[to_additive]
lemma smul_subset_smul_left : t₁ ⊆ t₂ → s • t₁ ⊆ s • t₂ := image2_subset_left
@[to_additive]
lemma smul_subset_smul_right : s₁ ⊆ s₂ → s₁ • t ⊆ s₂ • t := image2_subset_right
@[to_additive] lemma smul_subset_iff : s • t ⊆ u ↔ ∀ a ∈ s, ∀ b ∈ t, a • b ∈ u := image2_subset_iff
@[to_additive] lemma union_smul : (s₁ ∪ s₂) • t = s₁ • t ∪ s₂ • t := image2_union_left
@[to_additive] lemma smul_union : s • (t₁ ∪ t₂) = s • t₁ ∪ s • t₂ := image2_union_right
@[to_additive]
lemma inter_smul_subset : (s₁ ∩ s₂) • t ⊆ s₁ • t ∩ s₂ • t := image2_inter_subset_left
@[to_additive]
lemma smul_inter_subset : s • (t₁ ∩ t₂) ⊆ s • t₁ ∩ s • t₂ := image2_inter_subset_right
@[to_additive]
lemma inter_smul_union_subset_union : (s₁ ∩ s₂) • (t₁ ∪ t₂) ⊆ s₁ • t₁ ∪ s₂ • t₂ :=
image2_inter_union_subset_union
@[to_additive]
lemma union_smul_inter_subset_union : (s₁ ∪ s₂) • (t₁ ∩ t₂) ⊆ s₁ • t₁ ∪ s₂ • t₂ :=
image2_union_inter_subset_union
@[to_additive]
lemma smul_set_subset_smul {s : Set α} : a ∈ s → a • t ⊆ s • t := image_subset_image2_right
end SMul
section SMulSet
variable {ι : Sort*} {κ : ι → Sort*} [SMul α β] {s t t₁ t₂ : Set β} {a : α} {b : β} {x y : β}
@[to_additive] lemma image_smul : (fun x ↦ a • x) '' t = a • t := rfl
scoped[Pointwise] attribute [simp] Set.image_smul Set.image_vadd
@[to_additive] lemma mem_smul_set : x ∈ a • t ↔ ∃ y, y ∈ t ∧ a • y = x := Iff.rfl
@[to_additive] lemma smul_mem_smul_set : b ∈ s → a • b ∈ a • s := mem_image_of_mem _
@[to_additive (attr := simp)] lemma smul_set_empty : a • (∅ : Set β) = ∅ := image_empty _
@[to_additive (attr := simp)] lemma smul_set_eq_empty : a • s = ∅ ↔ s = ∅ := image_eq_empty
@[to_additive (attr := simp)]
lemma smul_set_nonempty : (a • s).Nonempty ↔ s.Nonempty := image_nonempty
@[to_additive (attr := simp)]
lemma smul_set_singleton : a • ({b} : Set β) = {a • b} := image_singleton
@[to_additive (attr := gcongr)] lemma smul_set_mono : s ⊆ t → a • s ⊆ a • t := image_mono
@[to_additive]
lemma smul_set_subset_iff : a • s ⊆ t ↔ ∀ ⦃b⦄, b ∈ s → a • b ∈ t :=
image_subset_iff
@[to_additive]
lemma smul_set_union : a • (t₁ ∪ t₂) = a • t₁ ∪ a • t₂ :=
image_union ..
@[to_additive]
lemma smul_set_insert (a : α) (b : β) (s : Set β) : a • insert b s = insert (a • b) (a • s) :=
image_insert_eq ..
@[to_additive]
lemma smul_set_inter_subset : a • (t₁ ∩ t₂) ⊆ a • t₁ ∩ a • t₂ :=
image_inter_subset ..
@[to_additive] lemma Nonempty.smul_set : s.Nonempty → (a • s).Nonempty := Nonempty.image _
end SMulSet
section Pi
variable {M ι : Type*} {π : ι → Type*} [∀ i, SMul M (π i)]
@[to_additive]
theorem smul_set_pi_of_surjective (c : M) (I : Set ι) (s : ∀ i, Set (π i))
(hsurj : ∀ i ∉ I, Function.Surjective (c • · : π i → π i)) : c • I.pi s = I.pi (c • s) :=
piMap_image_pi hsurj s
@[to_additive]
theorem smul_set_univ_pi (c : M) (s : ∀ i, Set (π i)) : c • univ.pi s = univ.pi (c • s) :=
piMap_image_univ_pi _ s
end Pi
variable {s : Set α} {t : Set β} {a : α} {b : β}
@[to_additive]
lemma range_smul_range {ι κ : Type*} [SMul α β] (b : ι → α) (c : κ → β) :
range b • range c = range fun p : ι × κ ↦ b p.1 • c p.2 :=
image2_range ..
@[to_additive]
lemma smul_set_range [SMul α β] {ι : Sort*} (a : α) (f : ι → β) :
a • range f = range fun i ↦ a • f i :=
(range_comp ..).symm
@[to_additive] lemma range_smul [SMul α β] {ι : Sort*} (a : α) (f : ι → β) :
range (fun i ↦ a • f i) = a • range f := (smul_set_range ..).symm
end SMul
section VSub
variable {ι : Sort*} {κ : ι → Sort*} [VSub α β] {s s₁ s₂ t t₁ t₂ : Set β} {u : Set α} {a : α}
{b c : β}
instance vsub : VSub (Set α) (Set β) where vsub := image2 (· -ᵥ ·)
@[simp] lemma image2_vsub : image2 (· -ᵥ ·) s t = s -ᵥ t := rfl
lemma image_vsub_prod : (fun x : β × β ↦ x.fst -ᵥ x.snd) '' s ×ˢ t = s -ᵥ t := image_prod _
lemma mem_vsub : a ∈ s -ᵥ t ↔ ∃ x ∈ s, ∃ y ∈ t, x -ᵥ y = a := Iff.rfl
lemma vsub_mem_vsub (hb : b ∈ s) (hc : c ∈ t) : b -ᵥ c ∈ s -ᵥ t := mem_image2_of_mem hb hc
@[simp] lemma empty_vsub (t : Set β) : ∅ -ᵥ t = ∅ := image2_empty_left
@[simp] lemma vsub_empty (s : Set β) : s -ᵥ ∅ = ∅ := image2_empty_right
@[simp] lemma vsub_eq_empty : s -ᵥ t = ∅ ↔ s = ∅ ∨ t = ∅ := image2_eq_empty_iff
@[simp]
lemma vsub_nonempty : (s -ᵥ t : Set α).Nonempty ↔ s.Nonempty ∧ t.Nonempty := image2_nonempty_iff
lemma Nonempty.vsub : s.Nonempty → t.Nonempty → (s -ᵥ t : Set α).Nonempty := .image2
lemma Nonempty.of_vsub_left : (s -ᵥ t : Set α).Nonempty → s.Nonempty := .of_image2_left
lemma Nonempty.of_vsub_right : (s -ᵥ t : Set α).Nonempty → t.Nonempty := .of_image2_right
@[simp low + 1]
lemma vsub_singleton (s : Set β) (b : β) : s -ᵥ {b} = (· -ᵥ b) '' s := image2_singleton_right
@[simp low + 1]
lemma singleton_vsub (t : Set β) (b : β) : {b} -ᵥ t = (b -ᵥ ·) '' t := image2_singleton_left
@[simp high] lemma singleton_vsub_singleton : ({b} : Set β) -ᵥ {c} = {b -ᵥ c} := image2_singleton
@[mono, gcongr] lemma vsub_subset_vsub : s₁ ⊆ s₂ → t₁ ⊆ t₂ → s₁ -ᵥ t₁ ⊆ s₂ -ᵥ t₂ := image2_subset
lemma vsub_subset_vsub_left : t₁ ⊆ t₂ → s -ᵥ t₁ ⊆ s -ᵥ t₂ := image2_subset_left
lemma vsub_subset_vsub_right : s₁ ⊆ s₂ → s₁ -ᵥ t ⊆ s₂ -ᵥ t := image2_subset_right
lemma vsub_subset_iff : s -ᵥ t ⊆ u ↔ ∀ x ∈ s, ∀ y ∈ t, x -ᵥ y ∈ u := image2_subset_iff
lemma vsub_self_mono (h : s ⊆ t) : s -ᵥ s ⊆ t -ᵥ t := vsub_subset_vsub h h
lemma union_vsub : s₁ ∪ s₂ -ᵥ t = s₁ -ᵥ t ∪ (s₂ -ᵥ t) := image2_union_left
lemma vsub_union : s -ᵥ (t₁ ∪ t₂) = s -ᵥ t₁ ∪ (s -ᵥ t₂) := image2_union_right
lemma inter_vsub_subset : s₁ ∩ s₂ -ᵥ t ⊆ (s₁ -ᵥ t) ∩ (s₂ -ᵥ t) := image2_inter_subset_left
lemma vsub_inter_subset : s -ᵥ t₁ ∩ t₂ ⊆ (s -ᵥ t₁) ∩ (s -ᵥ t₂) := image2_inter_subset_right
lemma inter_vsub_union_subset_union : s₁ ∩ s₂ -ᵥ (t₁ ∪ t₂) ⊆ s₁ -ᵥ t₁ ∪ (s₂ -ᵥ t₂) :=
image2_inter_union_subset_union
lemma union_vsub_inter_subset_union : s₁ ∪ s₂ -ᵥ t₁ ∩ t₂ ⊆ s₁ -ᵥ t₁ ∪ (s₂ -ᵥ t₂) :=
image2_union_inter_subset_union
end VSub
@[to_additive]
lemma image_smul_comm [SMul α β] [SMul α γ] (f : β → γ) (a : α) (s : Set β) :
(∀ b, f (a • b) = a • f b) → f '' (a • s) = a • f '' s := image_comm
section SMul
variable [SMul αᵐᵒᵖ β] [SMul β γ] [SMul α γ]
-- TODO: replace hypothesis and conclusion with a typeclass
@[to_additive]
lemma op_smul_set_smul_eq_smul_smul_set (a : α) (s : Set β) (t : Set γ)
(h : ∀ (a : α) (b : β) (c : γ), (op a • b) • c = b • a • c) : (op a • s) • t = s • a • t := by
ext; simp [mem_smul, mem_smul_set, h]
end SMul
end Set |
.lake/packages/mathlib/Mathlib/Algebra/Group/Pointwise/Set/Basic.lean | import Mathlib.Algebra.Group.Equiv.Basic
import Mathlib.Algebra.Group.Prod
import Mathlib.Algebra.Order.Monoid.Unbundled.Pow
import Mathlib.Data.Set.NAry
/-!
# Pointwise operations of sets
This file defines pointwise algebraic operations on sets.
## Main declarations
For sets `s` and `t` and scalar `a`:
* `s * t`: Multiplication, set of all `x * y` where `x ∈ s` and `y ∈ t`.
* `s + t`: Addition, set of all `x + y` where `x ∈ s` and `y ∈ t`.
* `s⁻¹`: Inversion, set of all `x⁻¹` where `x ∈ s`.
* `-s`: Negation, set of all `-x` where `x ∈ s`.
* `s / t`: Division, set of all `x / y` where `x ∈ s` and `y ∈ t`.
* `s - t`: Subtraction, set of all `x - y` where `x ∈ s` and `y ∈ t`.
For `α` a semigroup/monoid, `Set α` is a semigroup/monoid.
As an unfortunate side effect, this means that `n • s`, where `n : ℕ`, is ambiguous between
pointwise scaling and repeated pointwise addition; the former has `(2 : ℕ) • {1, 2} = {2, 4}`, while
the latter has `(2 : ℕ) • {1, 2} = {2, 3, 4}`. See note [pointwise nat action].
Appropriate definitions and results are also transported to the additive theory via `to_additive`.
## Implementation notes
* The following expressions are considered in simp-normal form in a group:
`(fun h ↦ h * g) ⁻¹' s`, `(fun h ↦ g * h) ⁻¹' s`, `(fun h ↦ h * g⁻¹) ⁻¹' s`,
`(fun h ↦ g⁻¹ * h) ⁻¹' s`, `s * t`, `s⁻¹`, `(1 : Set _)` (and similarly for additive variants).
Expressions equal to one of these will be simplified.
* We put all instances in the scope `Pointwise`, so that these instances are not available by
default. Note that we do not mark them as reducible (as argued by note [reducible non-instances])
since we expect the scope to be open whenever the instances are actually used (and making the
instances reducible changes the behavior of `simp`.
## Tags
set multiplication, set addition, pointwise addition, pointwise multiplication,
pointwise subtraction
-/
assert_not_exists Set.iUnion MulAction MonoidWithZero IsOrderedMonoid
library_note2 «pointwise nat action» /--
Pointwise monoids (`Set`, `Finset`, `Filter`) have derived pointwise actions of the form
`SMul α β → SMul α (Set β)`. When `α` is `ℕ` or `ℤ`, this action conflicts with the
nat or int action coming from `Set β` being a `Monoid` or `DivInvMonoid`. For example,
`2 • {a, b}` can both be `{2 • a, 2 • b}` (pointwise action, pointwise repeated addition,
`Set.smulSet`) and `{a + a, a + b, b + a, b + b}` (nat or int action, repeated pointwise
addition, `Set.NSMul`).
Because the pointwise action can easily be spelled out in such cases, we give higher priority to the
nat and int actions.
-/
open Function MulOpposite
variable {F α β γ : Type*}
namespace Set
/-! ### `0`/`1` as sets -/
section One
variable [One α] {s : Set α} {a : α}
/-- The set `1 : Set α` is defined as `{1}` in scope `Pointwise`. -/
@[to_additive /-- The set `0 : Set α` is defined as `{0}` in scope `Pointwise`. -/]
protected def one : One (Set α) :=
⟨{1}⟩
scoped[Pointwise] attribute [instance] Set.one Set.zero
open Pointwise
-- TODO: This would be a good simp lemma scoped to `Pointwise`, but it seems `@[simp]` can't be
-- scoped
@[to_additive]
theorem singleton_one : ({1} : Set α) = 1 :=
rfl
@[to_additive (attr := simp)]
theorem mem_one : a ∈ (1 : Set α) ↔ a = 1 :=
Iff.rfl
@[to_additive]
theorem one_mem_one : (1 : α) ∈ (1 : Set α) :=
Eq.refl _
@[to_additive (attr := simp)]
theorem one_subset : 1 ⊆ s ↔ (1 : α) ∈ s :=
singleton_subset_iff
@[to_additive (attr := simp)]
theorem one_nonempty : (1 : Set α).Nonempty :=
⟨1, rfl⟩
@[to_additive (attr := simp)]
theorem image_one {f : α → β} : f '' 1 = {f 1} :=
image_singleton
@[to_additive]
theorem subset_one_iff_eq : s ⊆ 1 ↔ s = ∅ ∨ s = 1 :=
subset_singleton_iff_eq
@[to_additive]
theorem Nonempty.subset_one_iff (h : s.Nonempty) : s ⊆ 1 ↔ s = 1 :=
h.subset_singleton_iff
/-- The singleton operation as a `OneHom`. -/
@[to_additive /-- The singleton operation as a `ZeroHom`. -/]
def singletonOneHom : OneHom α (Set α) where
toFun := singleton; map_one' := singleton_one
@[to_additive (attr := simp)]
theorem coe_singletonOneHom : (singletonOneHom : α → Set α) = singleton :=
rfl
@[to_additive] lemma image_op_one : (1 : Set α).image op = 1 := image_singleton
@[to_additive (attr := simp) zero_prod_zero]
lemma one_prod_one [One β] : (1 ×ˢ 1 : Set (α × β)) = 1 := by ext; simp [Prod.ext_iff]
end One
/-! ### Set negation/inversion -/
section Inv
/-- The pointwise inversion of set `s⁻¹` is defined as `{x | x⁻¹ ∈ s}` in scope `Pointwise`. It is
equal to `{x⁻¹ | x ∈ s}`, see `Set.image_inv_eq_inv`. -/
@[to_additive
/-- The pointwise negation of set `-s` is defined as `{x | -x ∈ s}` in scope `Pointwise`.
It is equal to `{-x | x ∈ s}`, see `Set.image_neg_eq_neg`. -/]
protected def inv [Inv α] : Inv (Set α) :=
⟨preimage Inv.inv⟩
scoped[Pointwise] attribute [instance] Set.inv Set.neg
open Pointwise
section Inv
variable {ι : Sort*} [Inv α] {s t : Set α} {a : α}
@[to_additive (attr := simp)]
theorem inv_setOf (p : α → Prop) : {x | p x}⁻¹ = {x | p x⁻¹} :=
rfl
@[to_additive (attr := simp)]
theorem mem_inv : a ∈ s⁻¹ ↔ a⁻¹ ∈ s :=
Iff.rfl
@[to_additive (attr := simp)]
theorem inv_preimage : Inv.inv ⁻¹' s = s⁻¹ :=
rfl
@[to_additive (attr := simp)]
theorem inv_empty : (∅ : Set α)⁻¹ = ∅ :=
rfl
@[to_additive (attr := simp)]
theorem inv_univ : (univ : Set α)⁻¹ = univ :=
rfl
@[to_additive (attr := simp)]
theorem inter_inv : (s ∩ t)⁻¹ = s⁻¹ ∩ t⁻¹ :=
preimage_inter
@[to_additive (attr := simp)]
theorem union_inv : (s ∪ t)⁻¹ = s⁻¹ ∪ t⁻¹ :=
preimage_union
@[to_additive (attr := simp)]
theorem compl_inv : sᶜ⁻¹ = s⁻¹ᶜ :=
preimage_compl
@[to_additive (attr := simp) neg_prod]
lemma inv_prod [Inv β] (s : Set α) (t : Set β) : (s ×ˢ t)⁻¹ = s⁻¹ ×ˢ t⁻¹ := rfl
end Inv
section InvolutiveInv
variable [InvolutiveInv α] {s t : Set α} {a : α}
@[to_additive]
theorem inv_mem_inv : a⁻¹ ∈ s⁻¹ ↔ a ∈ s := by simp only [mem_inv, inv_inv]
@[to_additive (attr := simp)]
theorem nonempty_inv : s⁻¹.Nonempty ↔ s.Nonempty :=
inv_involutive.surjective.nonempty_preimage
@[to_additive]
theorem Nonempty.inv (h : s.Nonempty) : s⁻¹.Nonempty :=
nonempty_inv.2 h
@[to_additive (attr := simp)]
theorem image_inv_eq_inv : (·⁻¹) '' s = s⁻¹ :=
congr_fun (image_eq_preimage_of_inverse inv_involutive.leftInverse inv_involutive.rightInverse) _
@[to_additive (attr := simp)]
theorem inv_eq_empty : s⁻¹ = ∅ ↔ s = ∅ := by
rw [← image_inv_eq_inv, image_eq_empty]
@[to_additive (attr := simp)]
instance involutiveInv : InvolutiveInv (Set α) where
inv_inv s := by simp only [← inv_preimage, preimage_preimage, inv_inv, preimage_id']
@[to_additive (attr := simp)]
theorem inv_subset_inv : s⁻¹ ⊆ t⁻¹ ↔ s ⊆ t :=
(Equiv.inv α).surjective.preimage_subset_preimage_iff
@[to_additive]
theorem inv_subset : s⁻¹ ⊆ t ↔ s ⊆ t⁻¹ := by rw [← inv_subset_inv, inv_inv]
@[to_additive (attr := simp)]
theorem inv_singleton (a : α) : ({a} : Set α)⁻¹ = {a⁻¹} := by
rw [← image_inv_eq_inv, image_singleton]
@[to_additive (attr := simp)]
theorem inv_insert (a : α) (s : Set α) : (insert a s)⁻¹ = insert a⁻¹ s⁻¹ := by
rw [insert_eq, union_inv, inv_singleton, insert_eq]
@[to_additive]
theorem inv_range {ι : Sort*} {f : ι → α} : (range f)⁻¹ = range fun i => (f i)⁻¹ := by
rw [← image_inv_eq_inv]
exact (range_comp ..).symm
open MulOpposite
@[to_additive]
theorem image_op_inv : op '' s⁻¹ = (op '' s)⁻¹ := by
simp_rw [← image_inv_eq_inv, Function.Semiconj.set_image op_inv s]
end InvolutiveInv
end Inv
open Pointwise
/-! ### Set addition/multiplication -/
section Mul
variable {ι : Sort*} {κ : ι → Sort*} [Mul α] {s s₁ s₂ t t₁ t₂ u : Set α} {a b : α}
/-- The pointwise multiplication of sets `s * t` and `t` is defined as `{x * y | x ∈ s, y ∈ t}` in
scope `Pointwise`. -/
@[to_additive
/-- The pointwise addition of sets `s + t` is defined as `{x + y | x ∈ s, y ∈ t}` in locale
`Pointwise`. -/]
protected def mul : Mul (Set α) :=
⟨image2 (· * ·)⟩
scoped[Pointwise] attribute [instance] Set.mul Set.add
@[to_additive (attr := simp)]
theorem image2_mul : image2 (· * ·) s t = s * t :=
rfl
@[to_additive]
theorem mem_mul : a ∈ s * t ↔ ∃ x ∈ s, ∃ y ∈ t, x * y = a :=
Iff.rfl
@[to_additive]
theorem mul_mem_mul : a ∈ s → b ∈ t → a * b ∈ s * t :=
mem_image2_of_mem
@[to_additive add_image_prod]
theorem image_mul_prod : (fun x : α × α => x.fst * x.snd) '' s ×ˢ t = s * t :=
image_prod _
@[to_additive (attr := simp)]
theorem empty_mul : ∅ * s = ∅ :=
image2_empty_left
@[to_additive (attr := simp)]
theorem mul_empty : s * ∅ = ∅ :=
image2_empty_right
@[to_additive (attr := simp)]
theorem mul_eq_empty : s * t = ∅ ↔ s = ∅ ∨ t = ∅ :=
image2_eq_empty_iff
@[to_additive (attr := simp)]
theorem mul_nonempty : (s * t).Nonempty ↔ s.Nonempty ∧ t.Nonempty :=
image2_nonempty_iff
@[to_additive]
theorem Nonempty.mul : s.Nonempty → t.Nonempty → (s * t).Nonempty :=
Nonempty.image2
@[to_additive]
theorem Nonempty.of_mul_left : (s * t).Nonempty → s.Nonempty :=
Nonempty.of_image2_left
@[to_additive]
theorem Nonempty.of_mul_right : (s * t).Nonempty → t.Nonempty :=
Nonempty.of_image2_right
@[to_additive (attr := simp)]
theorem mul_singleton : s * {b} = (· * b) '' s :=
image2_singleton_right
@[to_additive (attr := simp)]
theorem singleton_mul : {a} * t = (a * ·) '' t :=
image2_singleton_left
@[to_additive]
theorem singleton_mul_singleton : ({a} : Set α) * {b} = {a * b} :=
image2_singleton
@[to_additive (attr := mono, gcongr)]
theorem mul_subset_mul : s₁ ⊆ t₁ → s₂ ⊆ t₂ → s₁ * s₂ ⊆ t₁ * t₂ :=
image2_subset
@[to_additive]
theorem mul_subset_mul_left : t₁ ⊆ t₂ → s * t₁ ⊆ s * t₂ :=
image2_subset_left
@[to_additive]
theorem mul_subset_mul_right : s₁ ⊆ s₂ → s₁ * t ⊆ s₂ * t :=
image2_subset_right
@[to_additive] instance : MulLeftMono (Set α) where elim _s _t₁ _t₂ := mul_subset_mul_left
@[to_additive] instance : MulRightMono (Set α) where elim _t _s₁ _s₂ := mul_subset_mul_right
@[to_additive]
theorem mul_subset_iff : s * t ⊆ u ↔ ∀ x ∈ s, ∀ y ∈ t, x * y ∈ u :=
image2_subset_iff
@[to_additive]
theorem union_mul : (s₁ ∪ s₂) * t = s₁ * t ∪ s₂ * t :=
image2_union_left
@[to_additive]
theorem mul_union : s * (t₁ ∪ t₂) = s * t₁ ∪ s * t₂ :=
image2_union_right
@[to_additive]
theorem inter_mul_subset : s₁ ∩ s₂ * t ⊆ s₁ * t ∩ (s₂ * t) :=
image2_inter_subset_left
@[to_additive]
theorem mul_inter_subset : s * (t₁ ∩ t₂) ⊆ s * t₁ ∩ (s * t₂) :=
image2_inter_subset_right
@[to_additive]
theorem inter_mul_union_subset_union : s₁ ∩ s₂ * (t₁ ∪ t₂) ⊆ s₁ * t₁ ∪ s₂ * t₂ :=
image2_inter_union_subset_union
@[to_additive]
theorem union_mul_inter_subset_union : (s₁ ∪ s₂) * (t₁ ∩ t₂) ⊆ s₁ * t₁ ∪ s₂ * t₂ :=
image2_union_inter_subset_union
/-- The singleton operation as a `MulHom`. -/
@[to_additive /-- The singleton operation as an `AddHom`. -/]
def singletonMulHom : α →ₙ* Set α where
toFun := singleton
map_mul' _ _ := singleton_mul_singleton.symm
@[to_additive (attr := simp)]
theorem coe_singletonMulHom : (singletonMulHom : α → Set α) = singleton :=
rfl
@[to_additive (attr := simp)]
theorem singletonMulHom_apply (a : α) : singletonMulHom a = {a} :=
rfl
open MulOpposite
@[to_additive (attr := simp)]
theorem image_op_mul : op '' (s * t) = op '' t * op '' s :=
image_image2_antidistrib op_mul
@[to_additive (attr := simp) prod_add_prod_comm]
lemma prod_mul_prod_comm [Mul β] (s₁ s₂ : Set α) (t₁ t₂ : Set β) :
(s₁ ×ˢ t₁) * (s₂ ×ˢ t₂) = (s₁ * s₂) ×ˢ (t₁ * t₂) := by ext; simp [mem_mul]; aesop
end Mul
/-! ### Set subtraction/division -/
section Div
variable {ι : Sort*} {κ : ι → Sort*} [Div α] {s s₁ s₂ t t₁ t₂ u : Set α} {a b : α}
/-- The pointwise division of sets `s / t` is defined as `{x / y | x ∈ s, y ∈ t}` in locale
`Pointwise`. -/
@[to_additive
/-- The pointwise subtraction of sets `s - t` is defined as `{x - y | x ∈ s, y ∈ t}` in locale
`Pointwise`. -/]
protected def div : Div (Set α) :=
⟨image2 (· / ·)⟩
scoped[Pointwise] attribute [instance] Set.div Set.sub
@[to_additive (attr := simp)]
theorem image2_div : image2 (· / ·) s t = s / t :=
rfl
@[to_additive]
theorem mem_div : a ∈ s / t ↔ ∃ x ∈ s, ∃ y ∈ t, x / y = a :=
Iff.rfl
@[to_additive]
theorem div_mem_div : a ∈ s → b ∈ t → a / b ∈ s / t :=
mem_image2_of_mem
@[to_additive sub_image_prod]
theorem image_div_prod : (fun x : α × α => x.fst / x.snd) '' s ×ˢ t = s / t :=
image_prod _
@[to_additive (attr := simp)]
theorem empty_div : ∅ / s = ∅ :=
image2_empty_left
@[to_additive (attr := simp)]
theorem div_empty : s / ∅ = ∅ :=
image2_empty_right
@[to_additive (attr := simp)]
theorem div_eq_empty : s / t = ∅ ↔ s = ∅ ∨ t = ∅ :=
image2_eq_empty_iff
@[to_additive (attr := simp)]
theorem div_nonempty : (s / t).Nonempty ↔ s.Nonempty ∧ t.Nonempty :=
image2_nonempty_iff
@[to_additive]
theorem Nonempty.div : s.Nonempty → t.Nonempty → (s / t).Nonempty :=
Nonempty.image2
@[to_additive]
theorem Nonempty.of_div_left : (s / t).Nonempty → s.Nonempty :=
Nonempty.of_image2_left
@[to_additive]
theorem Nonempty.of_div_right : (s / t).Nonempty → t.Nonempty :=
Nonempty.of_image2_right
@[to_additive (attr := simp)]
theorem div_singleton : s / {b} = (· / b) '' s :=
image2_singleton_right
@[to_additive (attr := simp)]
theorem singleton_div : {a} / t = (· / ·) a '' t :=
image2_singleton_left
@[to_additive]
theorem singleton_div_singleton : ({a} : Set α) / {b} = {a / b} :=
image2_singleton
@[to_additive (attr := mono, gcongr)]
theorem div_subset_div : s₁ ⊆ t₁ → s₂ ⊆ t₂ → s₁ / s₂ ⊆ t₁ / t₂ :=
image2_subset
@[to_additive]
theorem div_subset_div_left : t₁ ⊆ t₂ → s / t₁ ⊆ s / t₂ :=
image2_subset_left
@[to_additive]
theorem div_subset_div_right : s₁ ⊆ s₂ → s₁ / t ⊆ s₂ / t :=
image2_subset_right
@[to_additive]
theorem div_subset_iff : s / t ⊆ u ↔ ∀ x ∈ s, ∀ y ∈ t, x / y ∈ u :=
image2_subset_iff
@[to_additive]
theorem union_div : (s₁ ∪ s₂) / t = s₁ / t ∪ s₂ / t :=
image2_union_left
@[to_additive]
theorem div_union : s / (t₁ ∪ t₂) = s / t₁ ∪ s / t₂ :=
image2_union_right
@[to_additive]
theorem inter_div_subset : s₁ ∩ s₂ / t ⊆ s₁ / t ∩ (s₂ / t) :=
image2_inter_subset_left
@[to_additive]
theorem div_inter_subset : s / (t₁ ∩ t₂) ⊆ s / t₁ ∩ (s / t₂) :=
image2_inter_subset_right
@[to_additive]
theorem inter_div_union_subset_union : s₁ ∩ s₂ / (t₁ ∪ t₂) ⊆ s₁ / t₁ ∪ s₂ / t₂ :=
image2_inter_union_subset_union
@[to_additive]
theorem union_div_inter_subset_union : (s₁ ∪ s₂) / (t₁ ∩ t₂) ⊆ s₁ / t₁ ∪ s₂ / t₂ :=
image2_union_inter_subset_union
end Div
/-- Repeated pointwise addition (not the same as pointwise repeated addition!) of a `Set`. See
note [pointwise nat action]. -/
protected def NSMul [Zero α] [Add α] : SMul ℕ (Set α) :=
⟨nsmulRec⟩
/-- Repeated pointwise multiplication (not the same as pointwise repeated multiplication!) of a
`Set`. See note [pointwise nat action]. -/
@[to_additive existing]
protected def NPow [One α] [Mul α] : Pow (Set α) ℕ :=
⟨fun s n => npowRec n s⟩
/-- Repeated pointwise addition/subtraction (not the same as pointwise repeated
addition/subtraction!) of a `Set`. See note [pointwise nat action]. -/
protected def ZSMul [Zero α] [Add α] [Neg α] : SMul ℤ (Set α) :=
⟨zsmulRec⟩
/-- Repeated pointwise multiplication/division (not the same as pointwise repeated
multiplication/division!) of a `Set`. See note [pointwise nat action]. -/
@[to_additive existing]
protected def ZPow [One α] [Mul α] [Inv α] : Pow (Set α) ℤ :=
⟨fun s n => zpowRec npowRec n s⟩
scoped[Pointwise] attribute [instance] Set.NSMul Set.NPow Set.ZSMul Set.ZPow
/-- `Set α` is a `Semigroup` under pointwise operations if `α` is. -/
@[to_additive /-- `Set α` is an `AddSemigroup` under pointwise operations if `α` is. -/]
protected def semigroup [Semigroup α] : Semigroup (Set α) :=
{ Set.mul with mul_assoc := fun _ _ _ => image2_assoc mul_assoc }
section CommSemigroup
variable [CommSemigroup α] {s t : Set α}
/-- `Set α` is a `CommSemigroup` under pointwise operations if `α` is. -/
@[to_additive /-- `Set α` is an `AddCommSemigroup` under pointwise operations if `α` is. -/]
protected def commSemigroup : CommSemigroup (Set α) :=
{ Set.semigroup with mul_comm := fun _ _ => image2_comm mul_comm }
@[to_additive]
theorem inter_mul_union_subset : s ∩ t * (s ∪ t) ⊆ s * t :=
image2_inter_union_subset mul_comm
@[to_additive]
theorem union_mul_inter_subset : (s ∪ t) * (s ∩ t) ⊆ s * t :=
image2_union_inter_subset mul_comm
end CommSemigroup
section MulOneClass
variable [MulOneClass α]
/-- `Set α` is a `MulOneClass` under pointwise operations if `α` is. -/
@[to_additive /-- `Set α` is an `AddZeroClass` under pointwise operations if `α` is. -/]
protected def mulOneClass : MulOneClass (Set α) :=
{ Set.one, Set.mul with
mul_one := image2_right_identity mul_one
one_mul := image2_left_identity one_mul }
scoped[Pointwise]
attribute [instance]
Set.mulOneClass Set.addZeroClass Set.semigroup Set.addSemigroup Set.commSemigroup
Set.addCommSemigroup
@[to_additive]
theorem subset_mul_left (s : Set α) {t : Set α} (ht : (1 : α) ∈ t) : s ⊆ s * t := fun x hx =>
⟨x, hx, 1, ht, mul_one _⟩
@[to_additive]
theorem subset_mul_right {s : Set α} (t : Set α) (hs : (1 : α) ∈ s) : t ⊆ s * t := fun x hx =>
⟨1, hs, x, hx, one_mul _⟩
/-- The singleton operation as a `MonoidHom`. -/
@[to_additive /-- The singleton operation as an `AddMonoidHom`. -/]
def singletonMonoidHom : α →* Set α :=
{ singletonMulHom, singletonOneHom with }
@[to_additive (attr := simp)]
theorem coe_singletonMonoidHom : (singletonMonoidHom : α → Set α) = singleton :=
rfl
@[to_additive (attr := simp)]
theorem singletonMonoidHom_apply (a : α) : singletonMonoidHom a = {a} :=
rfl
end MulOneClass
section Monoid
variable [Monoid α] {s t : Set α} {a : α} {m n : ℕ}
/-- `Set α` is a `Monoid` under pointwise operations if `α` is. -/
@[to_additive /-- `Set α` is an `AddMonoid` under pointwise operations if `α` is. -/]
protected def monoid : Monoid (Set α) :=
{ Set.semigroup, Set.mulOneClass, @Set.NPow α _ _ with }
scoped[Pointwise] attribute [instance] Set.monoid Set.addMonoid
-- `Set.pow_left_monotone` doesn't exist since it would syntactically be a special case of
-- `pow_left_mono`
@[to_additive]
protected lemma pow_right_monotone (hs : 1 ∈ s) : Monotone (s ^ ·) :=
pow_right_monotone <| one_subset.2 hs
@[to_additive (attr := gcongr)]
lemma pow_subset_pow_left (hst : s ⊆ t) : s ^ n ⊆ t ^ n := pow_left_mono _ hst
@[to_additive]
lemma pow_subset_pow_right (hs : 1 ∈ s) (hmn : m ≤ n) : s ^ m ⊆ s ^ n :=
Set.pow_right_monotone hs hmn
@[to_additive (attr := gcongr)]
lemma pow_subset_pow (hst : s ⊆ t) (ht : 1 ∈ t) (hmn : m ≤ n) : s ^ m ⊆ t ^ n :=
(pow_subset_pow_left hst).trans (pow_subset_pow_right ht hmn)
@[to_additive]
lemma subset_pow (hs : 1 ∈ s) (hn : n ≠ 0) : s ⊆ s ^ n := by
simpa using pow_subset_pow_right hs <| Nat.one_le_iff_ne_zero.2 hn
@[to_additive]
lemma pow_subset_pow_mul_of_sq_subset_mul (hst : s ^ 2 ⊆ t * s) (hn : n ≠ 0) :
s ^ n ⊆ t ^ (n - 1) * s := pow_le_pow_mul_of_sq_le_mul hst hn
@[to_additive (attr := simp) nsmul_empty]
lemma empty_pow (hn : n ≠ 0) : (∅ : Set α) ^ n = ∅ := match n with | n + 1 => by simp [pow_succ]
@[to_additive]
lemma Nonempty.pow (hs : s.Nonempty) : ∀ {n}, (s ^ n).Nonempty
| 0 => by simp
| n + 1 => by rw [pow_succ]; exact hs.pow.mul hs
set_option push_neg.use_distrib true in
@[to_additive (attr := simp)] lemma pow_eq_empty : s ^ n = ∅ ↔ s = ∅ ∧ n ≠ 0 := by
constructor
· contrapose!
rintro (hs | rfl)
· exact hs.pow
· simp
· rintro ⟨rfl, hn⟩
exact empty_pow hn
@[to_additive (attr := simp) nsmul_singleton]
lemma singleton_pow (a : α) : ∀ n, ({a} : Set α) ^ n = {a ^ n}
| 0 => by simp [singleton_one]
| n + 1 => by simp [pow_succ, singleton_pow _ n]
@[to_additive] lemma pow_mem_pow (ha : a ∈ s) : a ^ n ∈ s ^ n := by
simpa using pow_subset_pow_left (singleton_subset_iff.2 ha)
@[to_additive] lemma one_mem_pow (hs : 1 ∈ s) : 1 ∈ s ^ n := by simpa using pow_mem_pow hs
@[to_additive]
lemma inter_pow_subset : (s ∩ t) ^ n ⊆ s ^ n ∩ t ^ n := by apply subset_inter <;> gcongr <;> simp
@[to_additive]
theorem mul_univ_of_one_mem (hs : (1 : α) ∈ s) : s * univ = univ :=
eq_univ_iff_forall.2 fun _ => mem_mul.2 ⟨_, hs, _, mem_univ _, one_mul _⟩
@[to_additive]
theorem univ_mul_of_one_mem (ht : (1 : α) ∈ t) : univ * t = univ :=
eq_univ_iff_forall.2 fun _ => mem_mul.2 ⟨_, mem_univ _, _, ht, mul_one _⟩
@[to_additive (attr := simp)]
theorem univ_mul_univ : (univ : Set α) * univ = univ :=
mul_univ_of_one_mem <| mem_univ _
@[to_additive (attr := simp) nsmul_univ]
theorem univ_pow : ∀ {n : ℕ}, n ≠ 0 → (univ : Set α) ^ n = univ
| 0 => fun h => (h rfl).elim
| 1 => fun _ => pow_one _
| n + 2 => fun _ => by rw [pow_succ, univ_pow n.succ_ne_zero, univ_mul_univ]
@[to_additive]
protected theorem _root_.IsUnit.set : IsUnit a → IsUnit ({a} : Set α) :=
IsUnit.map (singletonMonoidHom : α →* Set α)
@[to_additive nsmul_prod]
lemma prod_pow [Monoid β] (s : Set α) (t : Set β) : ∀ n, (s ×ˢ t) ^ n = (s ^ n) ×ˢ (t ^ n)
| 0 => by simp
| n + 1 => by simp [pow_succ, prod_pow _ _ n]
end Monoid
section IsLeftCancelMul
variable [Mul α] [IsLeftCancelMul α] {s t : Set α}
@[to_additive]
lemma Nontrivial.mul_left : t.Nontrivial → s.Nonempty → (s * t).Nontrivial := by
rintro ⟨a, ha, b, hb, hab⟩ ⟨c, hc⟩
exact ⟨c * a, mul_mem_mul hc ha, c * b, mul_mem_mul hc hb, by simpa⟩
@[to_additive]
lemma Nontrivial.mul (hs : s.Nontrivial) (ht : t.Nontrivial) : (s * t).Nontrivial :=
ht.mul_left hs.nonempty
end IsLeftCancelMul
section IsRightCancelMul
variable [Mul α] [IsRightCancelMul α] {s t : Set α}
@[to_additive]
lemma Nontrivial.mul_right : s.Nontrivial → t.Nonempty → (s * t).Nontrivial := by
rintro ⟨a, ha, b, hb, hab⟩ ⟨c, hc⟩
exact ⟨a * c, mul_mem_mul ha hc, b * c, mul_mem_mul hb hc, by simpa⟩
end IsRightCancelMul
section CancelMonoid
variable [CancelMonoid α] {s t : Set α} {a : α} {n : ℕ}
@[to_additive]
lemma Nontrivial.pow (hs : s.Nontrivial) : ∀ {n}, n ≠ 0 → (s ^ n).Nontrivial
| 1, _ => by simpa
| n + 2, _ => by simpa [pow_succ] using (hs.pow n.succ_ne_zero).mul hs
end CancelMonoid
/-- `Set α` is a `CommMonoid` under pointwise operations if `α` is. -/
@[to_additive /-- `Set α` is an `AddCommMonoid` under pointwise operations if `α` is. -/]
protected def commMonoid [CommMonoid α] : CommMonoid (Set α) :=
{ Set.monoid, Set.commSemigroup with }
scoped[Pointwise] attribute [instance] Set.commMonoid Set.addCommMonoid
open Pointwise
section DivisionMonoid
variable [DivisionMonoid α] {s t : Set α} {n : ℤ}
@[to_additive]
protected theorem mul_eq_one_iff : s * t = 1 ↔ ∃ a b, s = {a} ∧ t = {b} ∧ a * b = 1 := by
refine ⟨fun h => ?_, ?_⟩
· have hst : (s * t).Nonempty := h.symm.subst one_nonempty
obtain ⟨a, ha⟩ := hst.of_image2_left
obtain ⟨b, hb⟩ := hst.of_image2_right
have H : ∀ {a b}, a ∈ s → b ∈ t → a * b = (1 : α) := fun {a b} ha hb =>
h.subset <| mem_image2_of_mem ha hb
refine ⟨a, b, ?_, ?_, H ha hb⟩ <;> refine eq_singleton_iff_unique_mem.2 ⟨‹_›, fun x hx => ?_⟩
· exact (eq_inv_of_mul_eq_one_left <| H hx hb).trans (inv_eq_of_mul_eq_one_left <| H ha hb)
· exact (eq_inv_of_mul_eq_one_right <| H ha hx).trans (inv_eq_of_mul_eq_one_right <| H ha hb)
· rintro ⟨b, c, rfl, rfl, h⟩
rw [singleton_mul_singleton, h, singleton_one]
/-- `Set α` is a division monoid under pointwise operations if `α` is. -/
@[to_additive
/-- `Set α` is a subtraction monoid under pointwise operations if `α` is. -/]
protected def divisionMonoid : DivisionMonoid (Set α) :=
{ Set.monoid, Set.involutiveInv, Set.div, @Set.ZPow α _ _ _ with
mul_inv_rev := fun s t => by
simp_rw [← image_inv_eq_inv]
exact image_image2_antidistrib mul_inv_rev
inv_eq_of_mul := fun s t h => by
obtain ⟨a, b, rfl, rfl, hab⟩ := Set.mul_eq_one_iff.1 h
rw [inv_singleton, inv_eq_of_mul_eq_one_right hab]
div_eq_mul_inv := fun s t => by
rw [← image_id (s / t), ← image_inv_eq_inv]
exact image_image2_distrib_right div_eq_mul_inv }
scoped[Pointwise] attribute [instance] Set.divisionMonoid Set.subtractionMonoid
@[to_additive (attr := simp 500)]
theorem isUnit_iff : IsUnit s ↔ ∃ a, s = {a} ∧ IsUnit a := by
constructor
· rintro ⟨u, rfl⟩
obtain ⟨a, b, ha, hb, h⟩ := Set.mul_eq_one_iff.1 u.mul_inv
refine ⟨a, ha, ⟨a, b, h, singleton_injective ?_⟩, rfl⟩
rw [← singleton_mul_singleton, ← ha, ← hb]
exact u.inv_mul
· rintro ⟨a, rfl, ha⟩
exact ha.set
@[to_additive (attr := simp)]
lemma univ_div_univ : (univ / univ : Set α) = univ := by simp [div_eq_mul_inv]
@[to_additive] lemma subset_div_left (ht : 1 ∈ t) : s ⊆ s / t := by
rw [div_eq_mul_inv]; exact subset_mul_left _ <| by simpa
@[to_additive] lemma inv_subset_div_right (hs : 1 ∈ s) : t⁻¹ ⊆ s / t := by
rw [div_eq_mul_inv]; exact subset_mul_right _ hs
@[to_additive (attr := simp) zsmul_empty]
lemma empty_zpow (hn : n ≠ 0) : (∅ : Set α) ^ n = ∅ := by cases n <;> aesop
@[to_additive]
lemma Nonempty.zpow (hs : s.Nonempty) : ∀ {n : ℤ}, (s ^ n).Nonempty
| (n : ℕ) => hs.pow
| .negSucc n => by simpa using hs.pow
set_option push_neg.use_distrib true in
@[to_additive (attr := simp)] lemma zpow_eq_empty : s ^ n = ∅ ↔ s = ∅ ∧ n ≠ 0 := by
constructor
· contrapose!
rintro (hs | rfl)
· exact hs.zpow
· simp
· rintro ⟨rfl, hn⟩
exact empty_zpow hn
@[to_additive (attr := simp) zsmul_singleton]
lemma singleton_zpow (a : α) (n : ℤ) : ({a} : Set α) ^ n = {a ^ n} := by cases n <;> simp
end DivisionMonoid
/-- `Set α` is a commutative division monoid under pointwise operations if `α` is. -/
@[to_additive subtractionCommMonoid
/-- `Set α` is a commutative subtraction monoid under pointwise operations if `α` is. -/]
protected def divisionCommMonoid [DivisionCommMonoid α] :
DivisionCommMonoid (Set α) :=
{ Set.divisionMonoid, Set.commSemigroup with }
scoped[Pointwise] attribute [instance] Set.divisionCommMonoid Set.subtractionCommMonoid
section Group
variable [Group α] {s t : Set α} {a b : α}
/-! Note that `Set` is not a `Group` because `s / s ≠ 1` in general. -/
@[to_additive (attr := simp)]
theorem one_mem_div_iff : (1 : α) ∈ s / t ↔ ¬Disjoint s t := by
simp [not_disjoint_iff_nonempty_inter, mem_div, div_eq_one, Set.Nonempty]
@[to_additive (attr := simp)]
lemma one_mem_inv_mul_iff : (1 : α) ∈ t⁻¹ * s ↔ ¬Disjoint s t := by
aesop (add simp [not_disjoint_iff_nonempty_inter, mem_mul, mul_eq_one_iff_eq_inv, Set.Nonempty])
@[to_additive]
theorem one_notMem_div_iff : (1 : α) ∉ s / t ↔ Disjoint s t :=
one_mem_div_iff.not_left
@[deprecated (since := "2025-05-23")] alias not_zero_mem_sub_iff := zero_notMem_sub_iff
@[to_additive existing, deprecated (since := "2025-05-23")]
alias not_one_mem_div_iff := one_notMem_div_iff
@[to_additive]
lemma one_notMem_inv_mul_iff : (1 : α) ∉ t⁻¹ * s ↔ Disjoint s t := one_mem_inv_mul_iff.not_left
@[deprecated (since := "2025-05-23")]
alias not_zero_mem_neg_add_iff := zero_notMem_neg_add_iff
@[to_additive existing, deprecated (since := "2025-05-23")]
alias not_one_mem_inv_mul_iff := one_notMem_inv_mul_iff
alias ⟨_, _root_.Disjoint.one_notMem_div_set⟩ := one_notMem_div_iff
attribute [to_additive] Disjoint.one_notMem_div_set
@[deprecated (since := "2025-05-23")]
alias _root_.Disjoint.zero_not_mem_sub_set := Disjoint.zero_notMem_sub_set
@[to_additive existing, deprecated (since := "2025-05-23")]
alias _root_.Disjoint.one_not_mem_div_set := Disjoint.one_notMem_div_set
@[to_additive]
theorem Nonempty.one_mem_div (h : s.Nonempty) : (1 : α) ∈ s / s :=
let ⟨a, ha⟩ := h
mem_div.2 ⟨a, ha, a, ha, div_self' _⟩
@[to_additive]
theorem isUnit_singleton (a : α) : IsUnit ({a} : Set α) :=
(Group.isUnit a).set
@[to_additive (attr := simp)]
theorem isUnit_iff_singleton : IsUnit s ↔ ∃ a, s = {a} := by
simp only [isUnit_iff, Group.isUnit, and_true]
@[to_additive (attr := simp)]
theorem image_mul_left : (a * ·) '' t = (a⁻¹ * ·) ⁻¹' t := by
rw [image_eq_preimage_of_inverse] <;> intro c <;> simp
@[to_additive (attr := simp)]
theorem image_mul_right : (· * b) '' t = (· * b⁻¹) ⁻¹' t := by
rw [image_eq_preimage_of_inverse] <;> intro c <;> simp
@[to_additive]
theorem image_mul_left' : (a⁻¹ * ·) '' t = (a * ·) ⁻¹' t := by simp
@[to_additive]
theorem image_mul_right' : (· * b⁻¹) '' t = (· * b) ⁻¹' t := by simp
@[to_additive (attr := simp)]
theorem preimage_mul_left_singleton : (a * ·) ⁻¹' {b} = {a⁻¹ * b} := by
rw [← image_mul_left', image_singleton]
@[to_additive (attr := simp)]
theorem preimage_mul_right_singleton : (· * a) ⁻¹' {b} = {b * a⁻¹} := by
rw [← image_mul_right', image_singleton]
@[to_additive (attr := simp)]
theorem preimage_mul_left_one : (a * ·) ⁻¹' 1 = {a⁻¹} := by
rw [← image_mul_left', image_one, mul_one]
@[to_additive (attr := simp)]
theorem preimage_mul_right_one : (· * b) ⁻¹' 1 = {b⁻¹} := by
rw [← image_mul_right', image_one, one_mul]
@[to_additive]
theorem preimage_mul_left_one' : (a⁻¹ * ·) ⁻¹' 1 = {a} := by simp
@[to_additive]
theorem preimage_mul_right_one' : (· * b⁻¹) ⁻¹' 1 = {b} := by simp
@[to_additive (attr := simp)]
theorem mul_univ (hs : s.Nonempty) : s * (univ : Set α) = univ :=
let ⟨a, ha⟩ := hs
eq_univ_of_forall fun b => ⟨a, ha, a⁻¹ * b, trivial, mul_inv_cancel_left ..⟩
@[to_additive (attr := simp)]
theorem univ_mul (ht : t.Nonempty) : (univ : Set α) * t = univ :=
let ⟨a, ha⟩ := ht
eq_univ_of_forall fun b => ⟨b * a⁻¹, trivial, a, ha, inv_mul_cancel_right ..⟩
@[to_additive]
lemma image_inv [DivisionMonoid β] [FunLike F α β] [MonoidHomClass F α β] (f : F) (s : Set α) :
f '' s⁻¹ = (f '' s)⁻¹ := by
rw [← image_inv_eq_inv, ← image_inv_eq_inv]; exact image_comm (map_inv _)
end Group
section Mul
variable [Mul α] [Mul β] [FunLike F α β] [MulHomClass F α β] (m : F) {s t : Set α}
@[to_additive]
theorem image_mul : m '' (s * t) = m '' s * m '' t :=
image_image2_distrib <| map_mul m
@[to_additive]
lemma mul_subset_range {s t : Set β} (hs : s ⊆ range m) (ht : t ⊆ range m) : s * t ⊆ range m := by
rintro _ ⟨a, ha, b, hb, rfl⟩
obtain ⟨a, rfl⟩ := hs ha
obtain ⟨b, rfl⟩ := ht hb
exact ⟨a * b, map_mul ..⟩
@[to_additive]
theorem preimage_mul_preimage_subset {s t : Set β} : m ⁻¹' s * m ⁻¹' t ⊆ m ⁻¹' (s * t) := by
rintro _ ⟨_, _, _, _, rfl⟩
exact ⟨_, ‹_›, _, ‹_›, (map_mul m ..).symm⟩
@[to_additive]
lemma preimage_mul (hm : Injective m) {s t : Set β} (hs : s ⊆ range m) (ht : t ⊆ range m) :
m ⁻¹' (s * t) = m ⁻¹' s * m ⁻¹' t :=
hm.image_injective <| by
rw [image_mul, image_preimage_eq_iff.2 hs, image_preimage_eq_iff.2 ht,
image_preimage_eq_iff.2 (mul_subset_range m hs ht)]
end Mul
section Monoid
variable [Monoid α] [Monoid β] [FunLike F α β]
@[to_additive]
lemma image_pow_of_ne_zero [MulHomClass F α β] :
∀ {n}, n ≠ 0 → ∀ (f : F) (s : Set α), f '' (s ^ n) = (f '' s) ^ n
| 1, _ => by simp
| n + 2, _ => by simp [image_mul, pow_succ _ n.succ, image_pow_of_ne_zero]
@[to_additive]
lemma image_pow [MonoidHomClass F α β] (f : F) (s : Set α) : ∀ n, f '' (s ^ n) = (f '' s) ^ n
| 0 => by simp [singleton_one]
| n + 1 => image_pow_of_ne_zero n.succ_ne_zero ..
end Monoid
section Group
variable [Group α] [DivisionMonoid β] [FunLike F α β] [MonoidHomClass F α β] (m : F) {s t : Set α}
@[to_additive]
theorem image_div : m '' (s / t) = m '' s / m '' t :=
image_image2_distrib <| map_div m
@[to_additive]
lemma div_subset_range {s t : Set β} (hs : s ⊆ range m) (ht : t ⊆ range m) : s / t ⊆ range m := by
rintro _ ⟨a, ha, b, hb, rfl⟩
obtain ⟨a, rfl⟩ := hs ha
obtain ⟨b, rfl⟩ := ht hb
exact ⟨a / b, map_div ..⟩
@[to_additive]
theorem preimage_div_preimage_subset {s t : Set β} : m ⁻¹' s / m ⁻¹' t ⊆ m ⁻¹' (s / t) := by
rintro _ ⟨_, _, _, _, rfl⟩
exact ⟨_, ‹_›, _, ‹_›, (map_div m ..).symm⟩
@[to_additive]
lemma preimage_div (hm : Injective m) {s t : Set β} (hs : s ⊆ range m) (ht : t ⊆ range m) :
m ⁻¹' (s / t) = m ⁻¹' s / m ⁻¹' t :=
hm.image_injective <| by
rw [image_div, image_preimage_eq_iff.2 hs, image_preimage_eq_iff.2 ht,
image_preimage_eq_iff.2 (div_subset_range m hs ht)]
end Group
section Pi
variable {ι : Type*} {α : ι → Type*} [∀ i, Inv (α i)]
@[to_additive (attr := simp)]
lemma inv_pi (s : Set ι) (t : ∀ i, Set (α i)) : (s.pi t)⁻¹ = s.pi fun i ↦ (t i)⁻¹ := by ext x; simp
end Pi
section Pointwise
open scoped Pointwise
@[to_additive]
lemma MapsTo.mul [Mul β] {A : Set α} {B₁ B₂ : Set β} {f₁ f₂ : α → β}
(h₁ : MapsTo f₁ A B₁) (h₂ : MapsTo f₂ A B₂) : MapsTo (f₁ * f₂) A (B₁ * B₂) :=
fun _ h => mul_mem_mul (h₁ h) (h₂ h)
@[to_additive]
lemma MapsTo.inv [InvolutiveInv β] {A : Set α} {B : Set β} {f : α → β} (h : MapsTo f A B) :
MapsTo (f⁻¹) A (B⁻¹) :=
fun _ ha => inv_mem_inv.2 (h ha)
@[to_additive]
lemma MapsTo.div [Div β] {A : Set α} {B₁ B₂ : Set β} {f₁ f₂ : α → β}
(h₁ : MapsTo f₁ A B₁) (h₂ : MapsTo f₂ A B₂) : MapsTo (f₁ / f₂) A (B₁ / B₂) :=
fun _ ha => div_mem_div (h₁ ha) (h₂ ha)
end Pointwise
end Set |
.lake/packages/mathlib/Mathlib/Algebra/Group/Pointwise/Set/Small.lean | import Mathlib.Algebra.Group.Pointwise.Set.Basic
import Mathlib.Logic.Small.Set
/-!
# Small instances for pointwise operations
-/
universe u
variable {α β : Type*} (s t : Set α)
open Pointwise
instance small_set_zero [Zero α] : Small.{u} (0 : Set α) := small_single _
instance small_set_one [One α] : Small.{u} (1 : Set α) := small_single _
instance small_neg [InvolutiveNeg α] [Small.{u} s] : Small.{u} (-s :) := by
rw [← Set.image_neg_eq_neg]
infer_instance
instance small_add [Add α] [Small.{u} s] [Small.{u} t] : Small.{u} (s + t) := small_image2 ..
instance small_sub [Sub α] [Small.{u} s] [Small.{u} t] : Small.{u} (s - t) := small_image2 ..
instance small_mul [Mul α] [Small.{u} s] [Small.{u} t] : Small.{u} (s * t) := small_image2 ..
instance small_div [Div α] [Small.{u} s] [Small.{u} t] : Small.{u} (s / t) := small_image2 .. |
.lake/packages/mathlib/Mathlib/Algebra/Group/Pointwise/Set/BigOperators.lean | import Mathlib.Algebra.BigOperators.Group.Finset.Piecewise
import Mathlib.Algebra.Group.Pointwise.Set.Basic
import Mathlib.Data.Fintype.Card
/-!
# Results about pointwise operations on sets and big operators.
-/
namespace Set
open Pointwise Function
variable {ι α β F : Type*} [FunLike F α β]
section Monoid
variable [Monoid α] [Monoid β] [MonoidHomClass F α β]
@[to_additive]
theorem image_list_prod (f : F) :
∀ l : List (Set α), (f : α → β) '' l.prod = (l.map fun s => f '' s).prod
| [] => image_one.trans <| congr_arg singleton (map_one f)
| a :: as => by rw [List.map_cons, List.prod_cons, List.prod_cons, image_mul, image_list_prod _ _]
end Monoid
section CommMonoid
variable [CommMonoid α] [CommMonoid β] [MonoidHomClass F α β]
@[to_additive]
theorem image_multiset_prod (f : F) :
∀ m : Multiset (Set α), (f : α → β) '' m.prod = (m.map fun s => f '' s).prod :=
Quotient.ind <| by
simpa only [Multiset.quot_mk_to_coe, Multiset.prod_coe, Multiset.map_coe] using
image_list_prod f
@[to_additive]
theorem image_finset_prod (f : F) (m : Finset ι) (s : ι → Set α) :
((f : α → β) '' ∏ i ∈ m, s i) = ∏ i ∈ m, f '' s i :=
(image_multiset_prod f _).trans <| congr_arg Multiset.prod <| Multiset.map_map _ _ _
/-- The n-ary version of `Set.mem_mul`. -/
@[to_additive /-- The n-ary version of `Set.mem_add`. -/]
theorem mem_finset_prod (t : Finset ι) (f : ι → Set α) (a : α) :
(a ∈ ∏ i ∈ t, f i) ↔ ∃ (g : ι → α) (_ : ∀ {i}, i ∈ t → g i ∈ f i), ∏ i ∈ t, g i = a := by
classical
induction t using Finset.induction_on generalizing a with
| empty =>
simp_rw [Finset.prod_empty, Set.mem_one]
exact ⟨fun h ↦ ⟨fun _ ↦ a, fun hi ↦ False.elim (Finset.notMem_empty _ hi), h.symm⟩,
fun ⟨_, _, hf⟩ ↦ hf.symm⟩
| insert i is hi ih => ?_
rw [Finset.prod_insert hi, Set.mem_mul]
simp_rw [Finset.prod_insert hi]
simp_rw [ih]
constructor
· rintro ⟨x, y, hx, ⟨g, hg, rfl⟩, rfl⟩
refine ⟨Function.update g i x, ?_, ?_⟩
· intro j hj
obtain rfl | hj := Finset.mem_insert.mp hj
· rwa [Function.update_self]
· rw [update_of_ne (ne_of_mem_of_not_mem hj hi)]
exact hg hj
· rw [Finset.prod_update_of_notMem hi, Function.update_self]
· rintro ⟨g, hg, rfl⟩
exact ⟨g i, hg (is.mem_insert_self _), is.prod g,
⟨⟨g, fun hi ↦ hg (Finset.mem_insert_of_mem hi), rfl⟩, rfl⟩⟩
@[to_additive]
lemma mem_pow_iff_prod {n : ℕ} {s : Set α} {a : α} :
a ∈ s ^ n ↔ ∃ f : Fin n → α, (∀ i, f i ∈ s) ∧ ∏ i, f i = a := by
simpa using mem_finset_prod (t := .univ) (f := fun _ : Fin n ↦ s) _
/-- A version of `Set.mem_finset_prod` with a simpler RHS for products over a Fintype. -/
@[to_additive /-- A version of `Set.mem_finset_sum` with a simpler RHS for sums over a Fintype. -/]
theorem mem_fintype_prod [Fintype ι] (f : ι → Set α) (a : α) :
(a ∈ ∏ i, f i) ↔ ∃ (g : ι → α) (_ : ∀ i, g i ∈ f i), ∏ i, g i = a := by
rw [mem_finset_prod]
simp
/-- An n-ary version of `Set.mul_mem_mul`. -/
@[to_additive /-- An n-ary version of `Set.add_mem_add`. -/]
theorem list_prod_mem_list_prod (t : List ι) (f : ι → Set α) (g : ι → α) (hg : ∀ i ∈ t, g i ∈ f i) :
(t.map g).prod ∈ (t.map f).prod := by
induction t with
| nil => simp_rw [List.map_nil, List.prod_nil, Set.mem_one]
| cons h tl ih =>
simp_rw [List.map_cons, List.prod_cons]
exact mul_mem_mul (hg h List.mem_cons_self)
(ih fun i hi ↦ hg i <| List.mem_cons_of_mem _ hi)
/-- An n-ary version of `Set.mul_subset_mul`. -/
@[to_additive /-- An n-ary version of `Set.add_subset_add`. -/]
theorem list_prod_subset_list_prod (t : List ι) (f₁ f₂ : ι → Set α) (hf : ∀ i ∈ t, f₁ i ⊆ f₂ i) :
(t.map f₁).prod ⊆ (t.map f₂).prod := by
induction t with
| nil => rfl
| cons h tl ih =>
simp_rw [List.map_cons, List.prod_cons]
exact mul_subset_mul (hf h List.mem_cons_self)
(ih fun i hi ↦ hf i <| List.mem_cons_of_mem _ hi)
@[to_additive]
theorem list_prod_singleton {M : Type*} [Monoid M] (s : List M) :
(s.map fun i ↦ ({i} : Set M)).prod = {s.prod} :=
(map_list_prod (singletonMonoidHom : M →* Set M) _).symm
/-- An n-ary version of `Set.mul_mem_mul`. -/
@[to_additive /-- An n-ary version of `Set.add_mem_add`. -/]
theorem multiset_prod_mem_multiset_prod (t : Multiset ι) (f : ι → Set α) (g : ι → α)
(hg : ∀ i ∈ t, g i ∈ f i) : (t.map g).prod ∈ (t.map f).prod := by
induction t using Quotient.inductionOn
simp_rw [Multiset.quot_mk_to_coe, Multiset.map_coe, Multiset.prod_coe]
exact list_prod_mem_list_prod _ _ _ hg
/-- An n-ary version of `Set.mul_subset_mul`. -/
@[to_additive /-- An n-ary version of `Set.add_subset_add`. -/]
theorem multiset_prod_subset_multiset_prod (t : Multiset ι) (f₁ f₂ : ι → Set α)
(hf : ∀ i ∈ t, f₁ i ⊆ f₂ i) : (t.map f₁).prod ⊆ (t.map f₂).prod := by
induction t using Quotient.inductionOn
simp_rw [Multiset.quot_mk_to_coe, Multiset.map_coe, Multiset.prod_coe]
exact list_prod_subset_list_prod _ _ _ hf
@[to_additive]
theorem multiset_prod_singleton {M : Type*} [CommMonoid M] (s : Multiset M) :
(s.map fun i ↦ ({i} : Set M)).prod = {s.prod} :=
(map_multiset_prod (singletonMonoidHom : M →* Set M) _).symm
/-- An n-ary version of `Set.mul_mem_mul`. -/
@[to_additive /-- An n-ary version of `Set.add_mem_add`. -/]
theorem finset_prod_mem_finset_prod (t : Finset ι) (f : ι → Set α) (g : ι → α)
(hg : ∀ i ∈ t, g i ∈ f i) : (∏ i ∈ t, g i) ∈ ∏ i ∈ t, f i :=
multiset_prod_mem_multiset_prod _ _ _ hg
/-- An n-ary version of `Set.mul_subset_mul`. -/
@[to_additive /-- An n-ary version of `Set.add_subset_add`. -/]
theorem finset_prod_subset_finset_prod (t : Finset ι) (f₁ f₂ : ι → Set α)
(hf : ∀ i ∈ t, f₁ i ⊆ f₂ i) : ∏ i ∈ t, f₁ i ⊆ ∏ i ∈ t, f₂ i :=
multiset_prod_subset_multiset_prod _ _ _ hf
@[to_additive]
theorem finset_prod_singleton {M ι : Type*} [CommMonoid M] (s : Finset ι) (I : ι → M) :
∏ i ∈ s, ({I i} : Set M) = {∏ i ∈ s, I i} :=
(map_prod (singletonMonoidHom : M →* Set M) _ _).symm
/-- The n-ary version of `Set.image_mul_prod`. -/
@[to_additive /-- The n-ary version of `Set.add_image_prod`. -/]
theorem image_finset_prod_pi (l : Finset ι) (S : ι → Set α) :
(fun f : ι → α => ∏ i ∈ l, f i) '' (l : Set ι).pi S = ∏ i ∈ l, S i := by
ext
simp_rw [mem_finset_prod, mem_image, mem_pi, exists_prop, Finset.mem_coe]
/-- A special case of `Set.image_finset_prod_pi` for `Finset.univ`. -/
@[to_additive /-- A special case of `Set.image_finset_sum_pi` for `Finset.univ`. -/]
theorem image_fintype_prod_pi [Fintype ι] (S : ι → Set α) :
(fun f : ι → α => ∏ i, f i) '' univ.pi S = ∏ i, S i := by
simpa only [Finset.coe_univ] using image_finset_prod_pi Finset.univ S
end CommMonoid
/-! TODO: define `decidable_mem_finset_prod` and `decidable_mem_finset_sum`. -/
end Set |
.lake/packages/mathlib/Mathlib/Algebra/Group/Pointwise/Set/Lattice.lean | import Mathlib.Algebra.Group.Pointwise.Set.Scalar
import Mathlib.Data.Set.Lattice.Image
import Mathlib.Algebra.Group.Pointwise.Set.Basic
/-!
# Indexed unions and intersections of pointwise operations of sets
This file contains lemmas on taking the union and intersection over pointwise algebraic operations
on sets.
## Tags
set multiplication, set addition, pointwise addition, pointwise multiplication,
pointwise subtraction
-/
assert_not_exists MulAction MonoidWithZero
open Function MulOpposite
variable {F α β γ : Type*}
namespace Set
/-! ### Set negation/inversion -/
section Inv
open Pointwise
variable {ι : Sort*} [Inv α]
@[to_additive (attr := simp)]
theorem iInter_inv (s : ι → Set α) : (⋂ i, s i)⁻¹ = ⋂ i, (s i)⁻¹ :=
preimage_iInter
@[to_additive (attr := simp)]
theorem sInter_inv (S : Set (Set α)) : (⋂₀ S)⁻¹ = ⋂ s ∈ S, s⁻¹ :=
preimage_sInter
@[to_additive (attr := simp)]
theorem iUnion_inv (s : ι → Set α) : (⋃ i, s i)⁻¹ = ⋃ i, (s i)⁻¹ :=
preimage_iUnion
@[to_additive (attr := simp)]
theorem sUnion_inv (S : Set (Set α)) : (⋃₀ S)⁻¹ = ⋃ s ∈ S, s⁻¹ :=
preimage_sUnion
end Inv
open Pointwise
/-! ### Set addition/multiplication -/
section Mul
variable {ι : Sort*} {κ : ι → Sort*} [Mul α] {s s₁ s₂ t t₁ t₂ u : Set α} {a b : α}
@[to_additive]
theorem iUnion_mul_left_image : ⋃ a ∈ s, (a * ·) '' t = s * t :=
iUnion_image_left _
@[to_additive]
theorem iUnion_mul_right_image : ⋃ a ∈ t, (· * a) '' s = s * t :=
iUnion_image_right _
@[to_additive]
theorem iUnion_mul (s : ι → Set α) (t : Set α) : (⋃ i, s i) * t = ⋃ i, s i * t :=
image2_iUnion_left ..
@[to_additive]
theorem mul_iUnion (s : Set α) (t : ι → Set α) : (s * ⋃ i, t i) = ⋃ i, s * t i :=
image2_iUnion_right ..
@[to_additive]
theorem sUnion_mul (S : Set (Set α)) (t : Set α) : ⋃₀ S * t = ⋃ s ∈ S, s * t :=
image2_sUnion_left ..
@[to_additive]
theorem mul_sUnion (s : Set α) (T : Set (Set α)) : s * ⋃₀ T = ⋃ t ∈ T, s * t :=
image2_sUnion_right ..
@[to_additive]
theorem iUnion₂_mul (s : ∀ i, κ i → Set α) (t : Set α) :
(⋃ (i) (j), s i j) * t = ⋃ (i) (j), s i j * t :=
image2_iUnion₂_left ..
@[to_additive]
theorem mul_iUnion₂ (s : Set α) (t : ∀ i, κ i → Set α) :
(s * ⋃ (i) (j), t i j) = ⋃ (i) (j), s * t i j :=
image2_iUnion₂_right ..
@[to_additive]
theorem iInter_mul_subset (s : ι → Set α) (t : Set α) : (⋂ i, s i) * t ⊆ ⋂ i, s i * t :=
Set.image2_iInter_subset_left ..
@[to_additive]
theorem mul_iInter_subset (s : Set α) (t : ι → Set α) : (s * ⋂ i, t i) ⊆ ⋂ i, s * t i :=
image2_iInter_subset_right ..
@[to_additive]
lemma mul_sInter_subset (s : Set α) (T : Set (Set α)) :
s * ⋂₀ T ⊆ ⋂ t ∈ T, s * t := image2_sInter_right_subset s T (fun a b => a * b)
@[to_additive]
lemma sInter_mul_subset (S : Set (Set α)) (t : Set α) :
⋂₀ S * t ⊆ ⋂ s ∈ S, s * t := image2_sInter_left_subset S t (fun a b => a * b)
@[to_additive]
theorem iInter₂_mul_subset (s : ∀ i, κ i → Set α) (t : Set α) :
(⋂ (i) (j), s i j) * t ⊆ ⋂ (i) (j), s i j * t :=
image2_iInter₂_subset_left ..
@[to_additive]
theorem mul_iInter₂_subset (s : Set α) (t : ∀ i, κ i → Set α) :
(s * ⋂ (i) (j), t i j) ⊆ ⋂ (i) (j), s * t i j :=
image2_iInter₂_subset_right ..
end Mul
/-! ### Set subtraction/division -/
section Div
variable {ι : Sort*} {κ : ι → Sort*} [Div α] {s s₁ s₂ t t₁ t₂ u : Set α} {a b : α}
@[to_additive]
theorem iUnion_div_left_image : ⋃ a ∈ s, (a / ·) '' t = s / t :=
iUnion_image_left _
@[to_additive]
theorem iUnion_div_right_image : ⋃ a ∈ t, (· / a) '' s = s / t :=
iUnion_image_right _
@[to_additive]
theorem iUnion_div (s : ι → Set α) (t : Set α) : (⋃ i, s i) / t = ⋃ i, s i / t :=
image2_iUnion_left ..
@[to_additive]
theorem div_iUnion (s : Set α) (t : ι → Set α) : (s / ⋃ i, t i) = ⋃ i, s / t i :=
image2_iUnion_right ..
@[to_additive]
theorem sUnion_div (S : Set (Set α)) (t : Set α) : ⋃₀ S / t = ⋃ s ∈ S, s / t :=
image2_sUnion_left ..
@[to_additive]
theorem div_sUnion (s : Set α) (T : Set (Set α)) : s / ⋃₀ T = ⋃ t ∈ T, s / t :=
image2_sUnion_right ..
@[to_additive]
theorem iUnion₂_div (s : ∀ i, κ i → Set α) (t : Set α) :
(⋃ (i) (j), s i j) / t = ⋃ (i) (j), s i j / t :=
image2_iUnion₂_left ..
@[to_additive]
theorem div_iUnion₂ (s : Set α) (t : ∀ i, κ i → Set α) :
(s / ⋃ (i) (j), t i j) = ⋃ (i) (j), s / t i j :=
image2_iUnion₂_right ..
@[to_additive]
theorem iInter_div_subset (s : ι → Set α) (t : Set α) : (⋂ i, s i) / t ⊆ ⋂ i, s i / t :=
image2_iInter_subset_left ..
@[to_additive]
theorem div_iInter_subset (s : Set α) (t : ι → Set α) : (s / ⋂ i, t i) ⊆ ⋂ i, s / t i :=
image2_iInter_subset_right ..
@[to_additive]
theorem sInter_div_subset (S : Set (Set α)) (t : Set α) : ⋂₀ S / t ⊆ ⋂ s ∈ S, s / t :=
image2_sInter_subset_left ..
@[to_additive]
theorem div_sInter_subset (s : Set α) (T : Set (Set α)) : s / ⋂₀ T ⊆ ⋂ t ∈ T, s / t :=
image2_sInter_subset_right ..
@[to_additive]
theorem iInter₂_div_subset (s : ∀ i, κ i → Set α) (t : Set α) :
(⋂ (i) (j), s i j) / t ⊆ ⋂ (i) (j), s i j / t :=
image2_iInter₂_subset_left ..
@[to_additive]
theorem div_iInter₂_subset (s : Set α) (t : ∀ i, κ i → Set α) :
(s / ⋂ (i) (j), t i j) ⊆ ⋂ (i) (j), s / t i j :=
image2_iInter₂_subset_right ..
end Div
/-! ### Translation/scaling of sets -/
section SMul
variable {ι : Sort*} {κ : ι → Sort*} [SMul α β] {s s₁ s₂ : Set α} {t t₁ t₂ u : Set β} {a : α}
{b : β}
@[to_additive] lemma iUnion_smul_left_image : ⋃ a ∈ s, a • t = s • t := iUnion_image_left _
@[to_additive]
lemma iUnion_smul_right_image : ⋃ a ∈ t, (· • a) '' s = s • t := iUnion_image_right _
@[to_additive]
lemma iUnion_smul (s : ι → Set α) (t : Set β) : (⋃ i, s i) • t = ⋃ i, s i • t :=
image2_iUnion_left ..
@[to_additive]
lemma smul_iUnion (s : Set α) (t : ι → Set β) : (s • ⋃ i, t i) = ⋃ i, s • t i :=
image2_iUnion_right ..
@[to_additive]
lemma sUnion_smul (S : Set (Set α)) (t : Set β) : ⋃₀ S • t = ⋃ s ∈ S, s • t :=
image2_sUnion_left ..
@[to_additive]
lemma smul_sUnion (s : Set α) (T : Set (Set β)) : s • ⋃₀ T = ⋃ t ∈ T, s • t :=
image2_sUnion_right ..
@[to_additive]
lemma iUnion₂_smul (s : ∀ i, κ i → Set α) (t : Set β) :
(⋃ i, ⋃ j, s i j) • t = ⋃ i, ⋃ j, s i j • t := image2_iUnion₂_left ..
@[to_additive]
lemma smul_iUnion₂ (s : Set α) (t : ∀ i, κ i → Set β) :
(s • ⋃ i, ⋃ j, t i j) = ⋃ i, ⋃ j, s • t i j := image2_iUnion₂_right ..
@[to_additive]
lemma iInter_smul_subset (s : ι → Set α) (t : Set β) : (⋂ i, s i) • t ⊆ ⋂ i, s i • t :=
image2_iInter_subset_left ..
@[to_additive]
lemma smul_iInter_subset (s : Set α) (t : ι → Set β) : (s • ⋂ i, t i) ⊆ ⋂ i, s • t i :=
image2_iInter_subset_right ..
@[to_additive]
lemma sInter_smul_subset (S : Set (Set α)) (t : Set β) : ⋂₀ S • t ⊆ ⋂ s ∈ S, s • t :=
image2_sInter_left_subset S t (fun a x => a • x)
@[to_additive]
lemma smul_sInter_subset (s : Set α) (T : Set (Set β)) : s • ⋂₀ T ⊆ ⋂ t ∈ T, s • t :=
image2_sInter_right_subset s T (fun a x => a • x)
@[to_additive]
lemma iInter₂_smul_subset (s : ∀ i, κ i → Set α) (t : Set β) :
(⋂ i, ⋂ j, s i j) • t ⊆ ⋂ i, ⋂ j, s i j • t := image2_iInter₂_subset_left ..
@[to_additive]
lemma smul_iInter₂_subset (s : Set α) (t : ∀ i, κ i → Set β) :
(s • ⋂ i, ⋂ j, t i j) ⊆ ⋂ i, ⋂ j, s • t i j := image2_iInter₂_subset_right ..
@[to_additive (attr := simp)]
lemma iUnion_smul_set (s : Set α) (t : Set β) : ⋃ a ∈ s, a • t = s • t := iUnion_image_left _
end SMul
section SMulSet
variable {ι : Sort*} {κ : ι → Sort*} [SMul α β] {s t t₁ t₂ : Set β} {a : α} {b : β} {x y : β}
@[to_additive]
lemma smul_set_iUnion (a : α) (s : ι → Set β) : a • ⋃ i, s i = ⋃ i, a • s i :=
image_iUnion
@[to_additive]
lemma smul_set_iUnion₂ (a : α) (s : ∀ i, κ i → Set β) :
a • ⋃ i, ⋃ j, s i j = ⋃ i, ⋃ j, a • s i j := image_iUnion₂ ..
@[to_additive]
lemma smul_set_sUnion (a : α) (S : Set (Set β)) : a • ⋃₀ S = ⋃ s ∈ S, a • s := by
rw [sUnion_eq_biUnion, smul_set_iUnion₂]
@[to_additive]
lemma smul_set_iInter_subset (a : α) (t : ι → Set β) : a • ⋂ i, t i ⊆ ⋂ i, a • t i :=
image_iInter_subset ..
@[to_additive]
lemma smul_set_sInter_subset (a : α) (S : Set (Set β)) :
a • ⋂₀ S ⊆ ⋂ s ∈ S, a • s := image_sInter_subset ..
@[to_additive]
lemma smul_set_iInter₂_subset (a : α) (t : ∀ i, κ i → Set β) :
a • ⋂ i, ⋂ j, t i j ⊆ ⋂ i, ⋂ j, a • t i j := image_iInter₂_subset ..
end SMulSet
variable {s : Set α} {t : Set β} {a : α} {b : β}
section VSub
variable {ι : Sort*} {κ : ι → Sort*} [VSub α β] {s s₁ s₂ t t₁ t₂ : Set β} {u : Set α} {a : α}
{b c : β}
lemma iUnion_vsub_left_image : ⋃ a ∈ s, (a -ᵥ ·) '' t = s -ᵥ t := iUnion_image_left _
lemma iUnion_vsub_right_image : ⋃ a ∈ t, (· -ᵥ a) '' s = s -ᵥ t := iUnion_image_right _
lemma iUnion_vsub (s : ι → Set β) (t : Set β) : (⋃ i, s i) -ᵥ t = ⋃ i, s i -ᵥ t :=
image2_iUnion_left ..
lemma vsub_iUnion (s : Set β) (t : ι → Set β) : (s -ᵥ ⋃ i, t i) = ⋃ i, s -ᵥ t i :=
image2_iUnion_right ..
lemma sUnion_vsub (S : Set (Set β)) (t : Set β) : ⋃₀ S -ᵥ t = ⋃ s ∈ S, s -ᵥ t :=
image2_sUnion_left ..
lemma vsub_sUnion (s : Set β) (T : Set (Set β)) : s -ᵥ ⋃₀ T = ⋃ t ∈ T, s -ᵥ t :=
image2_sUnion_right ..
lemma iUnion₂_vsub (s : ∀ i, κ i → Set β) (t : Set β) :
(⋃ i, ⋃ j, s i j) -ᵥ t = ⋃ i, ⋃ j, s i j -ᵥ t := image2_iUnion₂_left ..
lemma vsub_iUnion₂ (s : Set β) (t : ∀ i, κ i → Set β) :
(s -ᵥ ⋃ i, ⋃ j, t i j) = ⋃ i, ⋃ j, s -ᵥ t i j := image2_iUnion₂_right ..
lemma iInter_vsub_subset (s : ι → Set β) (t : Set β) : (⋂ i, s i) -ᵥ t ⊆ ⋂ i, s i -ᵥ t :=
image2_iInter_subset_left ..
lemma vsub_iInter_subset (s : Set β) (t : ι → Set β) : (s -ᵥ ⋂ i, t i) ⊆ ⋂ i, s -ᵥ t i :=
image2_iInter_subset_right ..
lemma sInter_vsub_subset (S : Set (Set β)) (t : Set β) : ⋂₀ S -ᵥ t ⊆ ⋂ s ∈ S, s -ᵥ t :=
image2_sInter_subset_left ..
lemma vsub_sInter_subset (s : Set β) (T : Set (Set β)) : s -ᵥ ⋂₀ T ⊆ ⋂ t ∈ T, s -ᵥ t :=
image2_sInter_subset_right ..
lemma iInter₂_vsub_subset (s : ∀ i, κ i → Set β) (t : Set β) :
(⋂ i, ⋂ j, s i j) -ᵥ t ⊆ ⋂ i, ⋂ j, s i j -ᵥ t := image2_iInter₂_subset_left ..
lemma vsub_iInter₂_subset (s : Set β) (t : ∀ i, κ i → Set β) :
s -ᵥ ⋂ i, ⋂ j, t i j ⊆ ⋂ i, ⋂ j, s -ᵥ t i j := image2_iInter₂_subset_right ..
end VSub
end Set |
.lake/packages/mathlib/Mathlib/Algebra/Group/WithOne/Map.lean | import Mathlib.Algebra.Group.WithOne.Defs
import Mathlib.Data.Option.NAry
/-!
# Adjoining a zero/one to semigroups and mapping
-/
variable {α β γ : Type*}
namespace WithOne
/-- Lift a map `f : α → β` to `WithOne α → WithOne β`. Implemented using `Option.map`.
Note: the definition previously known as `WithOne.map` is now called `WithOne.mapMulHom`. -/
@[to_additive
/-- Lift a map `f : α → β` to `WithZero α → WithZero β`. Implemented using `Option.map`.
Note: the definition previously known as `WithZero.map` is now called `WithZero.mapAddHom`. -/]
def map (f : α → β) : WithOne α → WithOne β := Option.map f
@[to_additive (attr := simp)]
theorem map_bot (f : α → β) : map f 1 = 1 :=
rfl
@[to_additive (attr := simp)]
theorem map_coe (f : α → β) (a : α) : map f a = f a :=
rfl
/-- The image of a binary function `f : α → β → γ` as a function
`WithOne α → WithOne β → WithOne γ`.
Mathematically this should be thought of as the image of the corresponding function `α × β → γ`. -/
@[to_additive
/-- The image of a binary function `f : α → β → γ` as a function
`WithZero α → WithZero β → WithZero γ`.
Mathematically this should be thought of as the image of the corresponding function `α × β → γ`. -/]
def map₂ : (α → β → γ) → WithOne α → WithOne β → WithOne γ := Option.map₂
@[to_additive]
lemma map₂_coe_coe (f : α → β → γ) (a : α) (b : β) : map₂ f a b = f a b := rfl
@[to_additive (attr := simp)]
lemma map₂_bot_left (f : α → β → γ) (b) : map₂ f 1 b = 1 := rfl
@[to_additive (attr := simp)]
lemma map₂_bot_right (f : α → β → γ) (a) : map₂ f a 1 = 1 := by cases a <;> rfl
@[to_additive (attr := simp)]
lemma map₂_coe_left (f : α → β → γ) (a : α) (b) : map₂ f a b = b.map fun b ↦ f a b := rfl
@[to_additive (attr := simp)]
lemma map₂_coe_right (f : α → β → γ) (a) (b : β) : map₂ f a b = a.map (f · b) := by
cases a <;> rfl
@[to_additive (attr := simp)]
lemma map₂_eq_bot_iff {f : α → β → γ} {a : WithOne α} {b : WithOne β} :
map₂ f a b = 1 ↔ a = 1 ∨ b = 1 := Option.map₂_eq_none_iff
end WithOne |
.lake/packages/mathlib/Mathlib/Algebra/Group/WithOne/Basic.lean | import Mathlib.Algebra.Group.Basic
import Mathlib.Algebra.Group.Equiv.Defs
import Mathlib.Algebra.Group.WithOne.Defs
/-!
# More operations on `WithOne` and `WithZero`
This file defines various bundled morphisms on `WithOne` and `WithZero`
that were not available in `Algebra/Group/WithOne/Defs`.
## Main definitions
* `WithOne.lift`, `WithZero.lift`
* `WithOne.map`, `WithZero.map`
-/
assert_not_exists MonoidWithZero DenselyOrdered
universe u v w
variable {α : Type u} {β : Type v} {γ : Type w}
namespace WithOne
@[to_additive]
instance instInvolutiveInv [InvolutiveInv α] : InvolutiveInv (WithOne α) where
inv_inv a := (Option.map_map _ _ _).trans <| by simp_rw [inv_comp_inv, Option.map_id, id]
section
/-- `WithOne.coe` as a bundled morphism -/
@[to_additive (attr := simps apply) /-- `WithZero.coe` as a bundled morphism -/]
def coeMulHom [Mul α] : α →ₙ* WithOne α where
toFun := coe
map_mul' _ _ := rfl
end
section lift
variable [Mul α] [MulOneClass β]
/-- Lift a semigroup homomorphism `f` to a bundled monoid homomorphism. -/
@[to_additive /--
Lift an additive semigroup homomorphism `f` to a bundled additive monoid homomorphism. -/]
def lift : (α →ₙ* β) ≃ (WithOne α →* β) where
toFun f :=
{ toFun := fun x => Option.casesOn x 1 f, map_one' := rfl,
map_mul' := fun x y => WithOne.cases_on x (by rw [one_mul]; exact (one_mul _).symm)
(fun x => WithOne.cases_on y (by rw [mul_one]; exact (mul_one _).symm)
(fun y => f.map_mul x y)) }
invFun F := F.toMulHom.comp coeMulHom
right_inv F := MonoidHom.ext fun x => WithOne.cases_on x F.map_one.symm (fun _ => rfl)
variable (f : α →ₙ* β)
@[to_additive (attr := simp)]
theorem lift_coe (x : α) : lift f x = f x :=
rfl
@[to_additive (attr := simp)]
theorem lift_one : lift f 1 = 1 :=
rfl
@[to_additive]
theorem lift_unique (f : WithOne α →* β) : f = lift (f.toMulHom.comp coeMulHom) :=
(lift.apply_symm_apply f).symm
end lift
section Map
variable [Mul α] [Mul β] [Mul γ]
/-- Given a multiplicative map from `α → β` returns a monoid homomorphism
from `WithOne α` to `WithOne β` -/
@[to_additive /-- Given an additive map from `α → β` returns an additive monoid homomorphism from
`WithZero α` to `WithZero β` -/]
def mapMulHom (f : α →ₙ* β) : WithOne α →* WithOne β :=
lift (coeMulHom.comp f)
@[to_additive (attr := simp)]
theorem mapMulHom_coe (f : α →ₙ* β) (a : α) : mapMulHom f (a : WithOne α) = f a :=
rfl
@[to_additive (attr := simp)]
theorem mapMulHom_id : mapMulHom (MulHom.id α) = MonoidHom.id (WithOne α) := by
ext x
induction x <;> rfl
@[deprecated (since := "2025-08-26")]
alias map_id := mapMulHom_id
@[deprecated (since := "2025-08-26")]
alias _root_.WithZero.map_id := WithZero.mapAddHom_id
@[to_additive]
theorem mapMulHom_injective {f : α →ₙ* β} (hf : Function.Injective f) :
Function.Injective (mapMulHom f)
| none, none, _ => rfl
| (a₁ : α), (a₂ : α), H => by simpa [hf.eq_iff] using H
@[deprecated (since := "2025-08-26")]
alias map_injective := mapMulHom_injective
@[deprecated (since := "2025-08-26")]
alias _root_.WithZero.map_injective := WithZero.mapAddHom_injective
@[to_additive]
theorem mapMulHom_injective' :
Function.Injective (WithOne.mapMulHom (α := α) (β := β)) :=
fun f g h ↦ MulHom.ext fun x ↦ coe_injective <| by simp only [← mapMulHom_coe, h]
@[deprecated (since := "2025-08-26")]
alias map_injective' := mapMulHom_injective'
@[deprecated (since := "2025-08-26")]
alias _root_.WithZero.map_injective' := WithZero.mapAddHom_injective'
@[to_additive (attr := simp)]
theorem mapMulHom_inj {f g : α →ₙ* β} : mapMulHom f = mapMulHom g ↔ f = g :=
mapMulHom_injective'.eq_iff
@[deprecated (since := "2025-08-26")]
alias map_inj := mapMulHom_inj
@[deprecated (since := "2025-08-26")]
alias _root_.WithZero.map_inj := WithZero.mapAddHom_inj
@[to_additive]
theorem mapMulHom_mapMulHom (f : α →ₙ* β) (g : β →ₙ* γ) (x) :
mapMulHom g (mapMulHom f x) = mapMulHom (g.comp f) x := by
induction x <;> rfl
@[deprecated (since := "2025-08-26")]
alias map_map := mapMulHom_mapMulHom
@[deprecated (since := "2025-08-26")]
alias _root_.WithZero.map_map := WithZero.mapAddHom_mapAddHom
@[to_additive (attr := simp)]
theorem mapMulHom_comp (f : α →ₙ* β) (g : β →ₙ* γ) :
mapMulHom (g.comp f) = (mapMulHom g).comp (mapMulHom f) :=
MonoidHom.ext fun x => (mapMulHom_mapMulHom f g x).symm
@[deprecated (since := "2025-08-26")]
alias map_comp := mapMulHom_comp
@[deprecated (since := "2025-08-26")]
alias _root_.WithZero.map_comp := WithZero.mapAddHom_comp
/-- A version of `Equiv.optionCongr` for `WithOne`. -/
@[to_additive (attr := simps apply) /-- A version of `Equiv.optionCongr` for `WithZero`. -/]
def _root_.MulEquiv.withOneCongr (e : α ≃* β) : WithOne α ≃* WithOne β :=
{ mapMulHom e.toMulHom with
toFun := mapMulHom e.toMulHom, invFun := mapMulHom e.symm.toMulHom,
left_inv := (by induction · <;> simp)
right_inv := (by induction · <;> simp) }
@[to_additive (attr := simp)]
theorem _root_.MulEquiv.withOneCongr_refl : (MulEquiv.refl α).withOneCongr = MulEquiv.refl _ :=
MulEquiv.toMonoidHom_injective mapMulHom_id
@[to_additive (attr := simp)]
theorem _root_.MulEquiv.withOneCongr_symm (e : α ≃* β) :
e.withOneCongr.symm = e.symm.withOneCongr :=
rfl
@[to_additive (attr := simp)]
theorem _root_.MulEquiv.withOneCongr_trans (e₁ : α ≃* β) (e₂ : β ≃* γ) :
e₁.withOneCongr.trans e₂.withOneCongr = (e₁.trans e₂).withOneCongr :=
MulEquiv.toMonoidHom_injective (mapMulHom_comp _ _).symm
end Map
end WithOne |
.lake/packages/mathlib/Mathlib/Algebra/Group/WithOne/Defs.lean | import Mathlib.Algebra.Group.Defs
import Mathlib.Data.Option.Basic
import Mathlib.Logic.Nontrivial.Basic
import Mathlib.Tactic.Common
/-!
# Adjoining a zero/one to semigroups and related algebraic structures
This file contains different results about adjoining an element to an algebraic structure which then
behaves like a zero or a one. An example is adjoining a one to a semigroup to obtain a monoid. That
this provides an example of an adjunction is proved in
`Mathlib/Algebra/Category/MonCat/Adjunctions.lean`.
Another result says that adjoining to a group an element `zero` gives a `GroupWithZero`. For more
information about these structures (which are not that standard in informal mathematics, see
`Mathlib/Algebra/GroupWithZero/Basic.lean`)
## TODO
`WithOne.coe_mul` and `WithZero.coe_mul` have inconsistent use of implicit parameters
-/
-- Check that we haven't needed to import all the basic lemmas about groups,
-- by asserting a random sample don't exist here:
assert_not_exists inv_involutive div_right_inj pow_ite MonoidWithZero DenselyOrdered
universe u v w
variable {α : Type u}
/-- Add an extra element `1` to a type -/
@[to_additive /-- Add an extra element `0` to a type -/]
def WithOne (α) :=
Option α
instance WithZero.instRepr [Repr α] : Repr (WithZero α) :=
⟨fun o _ =>
match o with
| none => "0"
| some a => "↑" ++ repr a⟩
namespace WithOne
@[to_additive existing]
instance [Repr α] : Repr (WithOne α) :=
⟨fun o _ =>
match o with
| none => "1"
| some a => "↑" ++ repr a⟩
@[to_additive]
instance instMonad : Monad WithOne :=
instMonadOption
@[to_additive]
instance instOne : One (WithOne α) :=
⟨none⟩
@[to_additive]
instance instMul [Mul α] : Mul (WithOne α) :=
⟨Option.merge (· * ·)⟩
@[to_additive]
instance instInv [Inv α] : Inv (WithOne α) :=
⟨fun a => Option.map Inv.inv a⟩
@[to_additive]
instance instInvOneClass [Inv α] : InvOneClass (WithOne α) :=
{ WithOne.instOne, WithOne.instInv with inv_one := rfl }
@[to_additive]
instance inhabited : Inhabited (WithOne α) :=
⟨1⟩
@[to_additive]
instance instNontrivial [Nonempty α] : Nontrivial (WithOne α) :=
Option.nontrivial
/-- The canonical map from `α` into `WithOne α` -/
@[to_additive (attr := coe, match_pattern) /-- The canonical map from `α` into `WithZero α` -/]
def coe : α → WithOne α :=
Option.some
@[to_additive]
instance instCoeTC : CoeTC α (WithOne α) :=
⟨coe⟩
@[to_additive]
lemma «forall» {p : WithZero α → Prop} : (∀ x, p x) ↔ p 0 ∧ ∀ a : α, p a := Option.forall
@[to_additive]
lemma «exists» {p : WithZero α → Prop} : (∃ x, p x) ↔ p 0 ∨ ∃ a : α, p a := Option.exists
/-- Recursor for `WithZero` using the preferred forms `0` and `↑a`. -/
@[elab_as_elim, induction_eliminator, cases_eliminator]
def _root_.WithZero.recZeroCoe {motive : WithZero α → Sort*} (zero : motive 0)
(coe : ∀ a : α, motive a) : ∀ n : WithZero α, motive n
| Option.none => zero
| Option.some x => coe x
/-- Recursor for `WithOne` using the preferred forms `1` and `↑a`. -/
@[to_additive existing, elab_as_elim, induction_eliminator, cases_eliminator]
def recOneCoe {motive : WithOne α → Sort*} (one : motive 1) (coe : ∀ a : α, motive a) :
∀ n : WithOne α, motive n
| Option.none => one
| Option.some x => coe x
@[to_additive (attr := simp)]
lemma recOneCoe_one {motive : WithOne α → Sort*} (h₁ h₂) :
recOneCoe h₁ h₂ (1 : WithOne α) = (h₁ : motive 1) :=
rfl
@[to_additive (attr := simp)]
lemma recOneCoe_coe {motive : WithOne α → Sort*} (h₁ h₂) (a : α) :
recOneCoe h₁ h₂ (a : WithOne α) = (h₂ : ∀ a : α, motive a) a :=
rfl
/-- Deconstruct an `x : WithOne α` to the underlying value in `α`, given a proof that `x ≠ 1`. -/
@[to_additive unzero
/-- Deconstruct an `x : WithZero α` to the underlying value in `α`, given a proof that `x ≠ 0`. -/]
def unone : ∀ {x : WithOne α}, x ≠ 1 → α | (x : α), _ => x
@[to_additive (attr := simp) unzero_coe]
theorem unone_coe {x : α} (hx : (x : WithOne α) ≠ 1) : unone hx = x :=
rfl
@[to_additive (attr := simp) coe_unzero]
lemma coe_unone : ∀ {x : WithOne α} (hx : x ≠ 1), unone hx = x
| (x : α), _ => rfl
@[to_additive (attr := simp)]
theorem coe_ne_one {a : α} : (a : WithOne α) ≠ (1 : WithOne α) :=
Option.some_ne_none a
@[to_additive (attr := simp)]
theorem one_ne_coe {a : α} : (1 : WithOne α) ≠ a :=
coe_ne_one.symm
@[to_additive]
theorem ne_one_iff_exists {x : WithOne α} : x ≠ 1 ↔ ∃ a : α, ↑a = x :=
Option.ne_none_iff_exists
@[to_additive]
instance instCanLift : CanLift (WithOne α) α (↑) fun a => a ≠ 1 where
prf _ := ne_one_iff_exists.1
@[to_additive (attr := simp, norm_cast)]
theorem coe_inj {a b : α} : (a : WithOne α) = b ↔ a = b :=
Option.some_inj
@[to_additive]
lemma coe_injective : Function.Injective (coe : α → WithOne α) :=
Option.some_injective _
@[to_additive (attr := elab_as_elim)]
protected theorem cases_on {P : WithOne α → Prop} : ∀ x : WithOne α, P 1 → (∀ a : α, P a) → P x :=
Option.casesOn
@[to_additive]
instance instMulOneClass [Mul α] : MulOneClass (WithOne α) where
one_mul := (Option.lawfulIdentity_merge _).left_id
mul_one := (Option.lawfulIdentity_merge _).right_id
@[to_additive (attr := simp, norm_cast)]
lemma coe_mul [Mul α] (a b : α) : (↑(a * b) : WithOne α) = a * b := rfl
@[to_additive]
instance instMonoid [Semigroup α] : Monoid (WithOne α) where
__ := instMulOneClass
mul_assoc
| 1, b, c => by simp
| (a : α), 1, c => by simp
| (a : α), (b : α), 1 => by simp
| (a : α), (b : α), (c : α) => by simp_rw [← coe_mul, mul_assoc]
@[to_additive]
instance instCommMonoid [CommSemigroup α] : CommMonoid (WithOne α) where
mul_comm
| (a : α), (b : α) => congr_arg some (mul_comm a b)
| (_ : α), 1 => rfl
| 1, (_ : α) => rfl
| 1, 1 => rfl
@[to_additive (attr := simp, norm_cast)]
theorem coe_inv [Inv α] (a : α) : ((a⁻¹ : α) : WithOne α) = (a : WithOne α)⁻¹ :=
rfl
end WithOne |
.lake/packages/mathlib/Mathlib/Algebra/Group/Subsemigroup/Basic.lean | import Mathlib.Algebra.Group.Subsemigroup.Defs
import Mathlib.Data.Set.Lattice.Image
/-!
# Subsemigroups: `CompleteLattice` structure
This file defines a `CompleteLattice` structure on `Subsemigroup`s,
and define the closure of a set as the minimal subsemigroup that includes this set.
## Main definitions
For each of the following definitions in the `Subsemigroup` namespace, there is a corresponding
definition in the `AddSubsemigroup` namespace.
* `Subsemigroup.copy` : copy of a subsemigroup with `carrier` replaced by a set that is equal but
possibly not definitionally equal to the carrier of the original `Subsemigroup`.
* `Subsemigroup.closure` : semigroup closure of a set, i.e.,
the least subsemigroup that includes the set.
* `Subsemigroup.gi` : `closure : Set M → Subsemigroup M` and coercion `coe : Subsemigroup M → Set M`
form a `GaloisInsertion`;
## Implementation notes
Subsemigroup inclusion is denoted `≤` rather than `⊆`, although `∈` is defined as
membership of a subsemigroup's underlying set.
Note that `Subsemigroup M` does not actually require `Semigroup M`,
instead requiring only the weaker `Mul M`.
This file is designed to have very few dependencies. In particular, it should not use natural
numbers.
## Tags
subsemigroup, subsemigroups
-/
assert_not_exists MonoidWithZero
-- Only needed for notation
variable {M : Type*} {N : Type*}
section NonAssoc
variable [Mul M] {s : Set M}
namespace Subsemigroup
variable (S : Subsemigroup M)
@[to_additive]
instance : InfSet (Subsemigroup M) :=
⟨fun s =>
{ carrier := ⋂ t ∈ s, ↑t
mul_mem' := fun hx hy =>
Set.mem_biInter fun i h =>
i.mul_mem (by apply Set.mem_iInter₂.1 hx i h) (by apply Set.mem_iInter₂.1 hy i h) }⟩
@[to_additive (attr := simp, norm_cast)]
theorem coe_sInf (S : Set (Subsemigroup M)) : ((sInf S : Subsemigroup M) : Set M) = ⋂ s ∈ S, ↑s :=
rfl
@[to_additive]
theorem mem_sInf {S : Set (Subsemigroup M)} {x : M} : x ∈ sInf S ↔ ∀ p ∈ S, x ∈ p :=
Set.mem_iInter₂
@[to_additive]
theorem mem_iInf {ι : Sort*} {S : ι → Subsemigroup M} {x : M} : (x ∈ ⨅ i, S i) ↔ ∀ i, x ∈ S i := by
simp only [iInf, mem_sInf, Set.forall_mem_range]
@[to_additive (attr := simp, norm_cast)]
theorem coe_iInf {ι : Sort*} {S : ι → Subsemigroup M} : (↑(⨅ i, S i) : Set M) = ⋂ i, S i := by
simp only [iInf, coe_sInf, Set.biInter_range]
/-- subsemigroups of a monoid form a complete lattice. -/
@[to_additive /-- The `AddSubsemigroup`s of an `AddMonoid` form a complete lattice. -/]
instance : CompleteLattice (Subsemigroup M) :=
{ completeLatticeOfInf (Subsemigroup M) fun _ =>
IsGLB.of_image SetLike.coe_subset_coe isGLB_biInf with
le := (· ≤ ·)
lt := (· < ·)
bot := ⊥
bot_le := fun _ _ hx => (notMem_bot hx).elim
top := ⊤
le_top := fun _ x _ => mem_top x
inf := (· ⊓ ·)
sInf := InfSet.sInf
le_inf := fun _ _ _ ha hb _ hx => ⟨ha hx, hb hx⟩
inf_le_left := fun _ _ _ => And.left
inf_le_right := fun _ _ _ => And.right }
/-- The `Subsemigroup` generated by a set. -/
@[to_additive /-- The `AddSubsemigroup` generated by a set -/]
def closure (s : Set M) : Subsemigroup M :=
sInf { S | s ⊆ S }
@[to_additive]
theorem mem_closure {x : M} : x ∈ closure s ↔ ∀ S : Subsemigroup M, s ⊆ S → x ∈ S :=
mem_sInf
/-- The subsemigroup generated by a set includes the set. -/
@[to_additive (attr := simp, aesop safe 20 (rule_sets := [SetLike]))
/-- The `AddSubsemigroup` generated by a set includes the set. -/]
theorem subset_closure : s ⊆ closure s := fun _ hx => mem_closure.2 fun _ hS => hS hx
@[to_additive (attr := aesop 80% (rule_sets := [SetLike]))]
theorem mem_closure_of_mem {s : Set M} {x : M} (hx : x ∈ s) : x ∈ closure s := subset_closure hx
@[to_additive]
theorem notMem_of_notMem_closure {P : M} (hP : P ∉ closure s) : P ∉ s := fun h =>
hP (subset_closure h)
@[deprecated (since := "2025-05-23")]
alias _root_.AddSubsemigroup.not_mem_of_not_mem_closure := AddSubsemigroup.notMem_of_notMem_closure
@[to_additive existing, deprecated (since := "2025-05-23")]
alias not_mem_of_not_mem_closure := notMem_of_notMem_closure
variable {S}
open Set
/-- A subsemigroup `S` includes `closure s` if and only if it includes `s`. -/
@[to_additive (attr := simp)
/-- An additive subsemigroup `S` includes `closure s` if and only if it includes `s` -/]
theorem closure_le : closure s ≤ S ↔ s ⊆ S :=
⟨Subset.trans subset_closure, fun h => sInf_le h⟩
/-- subsemigroup closure of a set is monotone in its argument: if `s ⊆ t`,
then `closure s ≤ closure t`. -/
@[to_additive (attr := gcongr) /-- Additive subsemigroup closure of a set is monotone in its
argument: if `s ⊆ t`, then `closure s ≤ closure t` -/]
theorem closure_mono ⦃s t : Set M⦄ (h : s ⊆ t) : closure s ≤ closure t :=
closure_le.2 <| Subset.trans h subset_closure
@[to_additive]
theorem closure_eq_of_le (h₁ : s ⊆ S) (h₂ : S ≤ closure s) : closure s = S :=
le_antisymm (closure_le.2 h₁) h₂
variable (S)
/-- An induction principle for closure membership. If `p` holds for all elements of `s`, and
is preserved under multiplication, then `p` holds for all elements of the closure of `s`. -/
@[to_additive (attr := elab_as_elim) /-- An induction principle for additive closure membership. If
`p` holds for all elements of `s`, and is preserved under addition, then `p` holds for all
elements of the additive closure of `s`. -/]
theorem closure_induction {p : (x : M) → x ∈ closure s → Prop}
(mem : ∀ (x) (h : x ∈ s), p x (subset_closure h))
(mul : ∀ x y hx hy, p x hx → p y hy → p (x * y) (mul_mem hx hy)) {x} (hx : x ∈ closure s) :
p x hx :=
let S : Subsemigroup M :=
{ carrier := { x | ∃ hx, p x hx }
mul_mem' := fun ⟨_, hpx⟩ ⟨_, hpy⟩ ↦ ⟨_, mul _ _ _ _ hpx hpy⟩ }
closure_le (S := S) |>.mpr (fun y hy ↦ ⟨subset_closure hy, mem y hy⟩) hx |>.elim fun _ ↦ id
/-- An induction principle for closure membership for predicates with two arguments. -/
@[to_additive (attr := elab_as_elim) /-- An induction principle for additive closure membership for
predicates with two arguments. -/]
theorem closure_induction₂ {p : (x y : M) → x ∈ closure s → y ∈ closure s → Prop}
(mem : ∀ (x) (y) (hx : x ∈ s) (hy : y ∈ s), p x y (subset_closure hx) (subset_closure hy))
(mul_left : ∀ x y z hx hy hz, p x z hx hz → p y z hy hz → p (x * y) z (mul_mem hx hy) hz)
(mul_right : ∀ x y z hx hy hz, p z x hz hx → p z y hz hy → p z (x * y) hz (mul_mem hx hy))
{x y : M} (hx : x ∈ closure s) (hy : y ∈ closure s) : p x y hx hy := by
induction hx using closure_induction with
| mem z hz => induction hy using closure_induction with
| mem _ h => exact mem _ _ hz h
| mul _ _ _ _ h₁ h₂ => exact mul_right _ _ _ _ _ _ h₁ h₂
| mul _ _ _ _ h₁ h₂ => exact mul_left _ _ _ _ _ hy h₁ h₂
/-- If `s` is a dense set in a magma `M`, `Subsemigroup.closure s = ⊤`, then in order to prove that
some predicate `p` holds for all `x : M` it suffices to verify `p x` for `x ∈ s`,
and verify that `p x` and `p y` imply `p (x * y)`. -/
@[to_additive (attr := elab_as_elim) /-- If `s` is a dense set in an additive monoid `M`,
`AddSubsemigroup.closure s = ⊤`, then in order to prove that some predicate `p` holds
for all `x : M` it suffices to verify `p x` for `x ∈ s`, and verify that `p x` and `p y` imply
`p (x + y)`. -/]
theorem dense_induction {p : M → Prop} (s : Set M) (closure : closure s = ⊤)
(mem : ∀ x ∈ s, p x) (mul : ∀ x y, p x → p y → p (x * y)) (x : M) :
p x := by
induction closure.symm ▸ mem_top x using closure_induction with
| mem _ h => exact mem _ h
| mul _ _ _ _ h₁ h₂ => exact mul _ _ h₁ h₂
/- The argument `s : Set M` is explicit in `Subsemigroup.dense_induction` because the type of the
induction variable, namely `x : M`, does not reference `x`. Making `s` explicit allows the user
to apply the induction principle while deferring the proof of `closure s = ⊤` without creating
metavariables, as in the following example. -/
example {p : M → Prop} (s : Set M) (closure : closure s = ⊤)
(mem : ∀ x ∈ s, p x) (mul : ∀ x y, p x → p y → p (x * y)) (x : M) :
p x := by
induction x using dense_induction s with
| closure => exact closure
| mem x hx => exact mem x hx
| mul _ _ h₁ h₂ => exact mul _ _ h₁ h₂
variable (M)
/-- `closure` forms a Galois insertion with the coercion to set. -/
@[to_additive /-- `closure` forms a Galois insertion with the coercion to set. -/]
protected def gi : GaloisInsertion (@closure M _) SetLike.coe :=
GaloisConnection.toGaloisInsertion (fun _ _ => closure_le) fun _ => subset_closure
variable {M}
/-- Closure of a subsemigroup `S` equals `S`. -/
@[to_additive (attr := simp) /-- Additive closure of an additive subsemigroup `S` equals `S` -/]
theorem closure_eq : closure (S : Set M) = S :=
(Subsemigroup.gi M).l_u_eq S
@[to_additive (attr := simp)]
theorem closure_empty : closure (∅ : Set M) = ⊥ :=
(Subsemigroup.gi M).gc.l_bot
@[to_additive (attr := simp)]
theorem closure_univ : closure (univ : Set M) = ⊤ :=
@coe_top M _ ▸ closure_eq ⊤
@[to_additive]
theorem closure_union (s t : Set M) : closure (s ∪ t) = closure s ⊔ closure t :=
(Subsemigroup.gi M).gc.l_sup
@[to_additive]
theorem closure_iUnion {ι} (s : ι → Set M) : closure (⋃ i, s i) = ⨆ i, closure (s i) :=
(Subsemigroup.gi M).gc.l_iSup
@[to_additive]
theorem closure_singleton_le_iff_mem (m : M) (p : Subsemigroup M) : closure {m} ≤ p ↔ m ∈ p := by
rw [closure_le, singleton_subset_iff, SetLike.mem_coe]
@[to_additive]
theorem mem_iSup {ι : Sort*} (p : ι → Subsemigroup M) {m : M} :
(m ∈ ⨆ i, p i) ↔ ∀ N, (∀ i, p i ≤ N) → m ∈ N := by
rw [← closure_singleton_le_iff_mem, le_iSup_iff]
simp only [closure_singleton_le_iff_mem]
@[to_additive]
theorem iSup_eq_closure {ι : Sort*} (p : ι → Subsemigroup M) :
⨆ i, p i = Subsemigroup.closure (⋃ i, (p i : Set M)) := by
simp_rw [Subsemigroup.closure_iUnion, Subsemigroup.closure_eq]
end Subsemigroup
namespace MulHom
variable [Mul N]
open Subsemigroup
/-- If two mul homomorphisms are equal on a set, then they are equal on its subsemigroup closure. -/
@[to_additive /-- If two add homomorphisms are equal on a set,
then they are equal on its additive subsemigroup closure. -/]
theorem eqOn_closure {f g : M →ₙ* N} {s : Set M} (h : Set.EqOn f g s) :
Set.EqOn f g (closure s) :=
show closure s ≤ f.eqLocus g from closure_le.2 h
@[to_additive]
theorem eq_of_eqOn_dense {s : Set M} (hs : closure s = ⊤) {f g : M →ₙ* N} (h : s.EqOn f g) :
f = g :=
eq_of_eqOn_top <| hs ▸ eqOn_closure h
end MulHom
end NonAssoc
section Assoc
namespace MulHom
open Subsemigroup
/-- Let `s` be a subset of a semigroup `M` such that the closure of `s` is the whole semigroup.
Then `MulHom.ofDense` defines a mul homomorphism from `M` asking for a proof
of `f (x * y) = f x * f y` only for `y ∈ s`. -/
@[to_additive]
def ofDense {M N} [Semigroup M] [Semigroup N] {s : Set M} (f : M → N) (hs : closure s = ⊤)
(hmul : ∀ (x), ∀ y ∈ s, f (x * y) = f x * f y) :
M →ₙ* N where
toFun := f
map_mul' x y :=
dense_induction _ hs (fun y hy x => hmul x y hy)
(fun y₁ y₂ h₁ h₂ x => by simp only [← mul_assoc, h₁, h₂]) y x
/-- Let `s` be a subset of an additive semigroup `M` such that the closure of `s` is the whole
semigroup. Then `AddHom.ofDense` defines an additive homomorphism from `M` asking for a proof
of `f (x + y) = f x + f y` only for `y ∈ s`. -/
add_decl_doc AddHom.ofDense
@[to_additive (attr := simp, norm_cast)]
theorem coe_ofDense [Semigroup M] [Semigroup N] {s : Set M} (f : M → N) (hs : closure s = ⊤)
(hmul) : (ofDense f hs hmul : M → N) = f :=
rfl
end MulHom
end Assoc |
.lake/packages/mathlib/Mathlib/Algebra/Group/Subsemigroup/Operations.lean | import Mathlib.Algebra.Group.Prod
import Mathlib.Algebra.Group.Subsemigroup.Basic
import Mathlib.Algebra.Group.TypeTags.Basic
/-!
# Operations on `Subsemigroup`s
In this file we define various operations on `Subsemigroup`s and `MulHom`s.
## Main definitions
### Conversion between multiplicative and additive definitions
* `Subsemigroup.toAddSubsemigroup`, `Subsemigroup.toAddSubsemigroup'`,
`AddSubsemigroup.toSubsemigroup`, `AddSubsemigroup.toSubsemigroup'`:
convert between multiplicative and additive subsemigroups of `M`,
`Multiplicative M`, and `Additive M`. These are stated as `OrderIso`s.
### (Commutative) semigroup structure on a subsemigroup
* `Subsemigroup.toSemigroup`, `Subsemigroup.toCommSemigroup`: a subsemigroup inherits a
(commutative) semigroup structure.
### Operations on subsemigroups
* `Subsemigroup.comap`: preimage of a subsemigroup under a semigroup homomorphism as a subsemigroup
of the domain;
* `Subsemigroup.map`: image of a subsemigroup under a semigroup homomorphism as a subsemigroup of
the codomain;
* `Subsemigroup.prod`: product of two subsemigroups `s : Subsemigroup M` and `t : Subsemigroup N`
as a subsemigroup of `M × N`;
### Semigroup homomorphisms between subsemigroups
* `Subsemigroup.subtype`: embedding of a subsemigroup into the ambient semigroup.
* `Subsemigroup.inclusion`: given two subsemigroups `S`, `T` such that `S ≤ T`, `S.inclusion T` is
the inclusion of `S` into `T` as a semigroup homomorphism;
* `MulEquiv.subsemigroupCongr`: converts a proof of `S = T` into a semigroup isomorphism between
`S` and `T`.
* `Subsemigroup.prodEquiv`: semigroup isomorphism between `s.prod t` and `s × t`;
### Operations on `MulHom`s
* `MulHom.srange`: range of a semigroup homomorphism as a subsemigroup of the codomain;
* `MulHom.restrict`: restrict a semigroup homomorphism to a subsemigroup;
* `MulHom.codRestrict`: restrict the codomain of a semigroup homomorphism to a subsemigroup;
* `MulHom.srangeRestrict`: restrict a semigroup homomorphism to its range;
### Implementation notes
This file follows closely `GroupTheory/Submonoid/Operations.lean`, omitting only that which is
necessary.
## Tags
subsemigroup, range, product, map, comap
-/
assert_not_exists MonoidWithZero
variable {M N P σ : Type*}
/-!
### Conversion to/from `Additive`/`Multiplicative`
-/
section
variable [Mul M]
/-- Subsemigroups of semigroup `M` are isomorphic to additive subsemigroups of `Additive M`. -/
@[simps]
def Subsemigroup.toAddSubsemigroup : Subsemigroup M ≃o AddSubsemigroup (Additive M) where
toFun S :=
{ carrier := Additive.toMul ⁻¹' S
add_mem' := S.mul_mem' }
invFun S :=
{ carrier := Additive.ofMul ⁻¹' S
mul_mem' := S.add_mem' }
map_rel_iff' := Iff.rfl
/-- Additive subsemigroups of an additive semigroup `Additive M` are isomorphic to subsemigroups
of `M`. -/
abbrev AddSubsemigroup.toSubsemigroup' : AddSubsemigroup (Additive M) ≃o Subsemigroup M :=
Subsemigroup.toAddSubsemigroup.symm
theorem Subsemigroup.toAddSubsemigroup_closure (S : Set M) :
Subsemigroup.toAddSubsemigroup (Subsemigroup.closure S) =
AddSubsemigroup.closure (Additive.toMul ⁻¹' S) :=
le_antisymm
(Subsemigroup.toAddSubsemigroup.le_symm_apply.1 <|
Subsemigroup.closure_le.2 (AddSubsemigroup.subset_closure (M := Additive M)))
(AddSubsemigroup.closure_le.2 (Subsemigroup.subset_closure (M := M)))
theorem AddSubsemigroup.toSubsemigroup'_closure (S : Set (Additive M)) :
AddSubsemigroup.toSubsemigroup' (AddSubsemigroup.closure S) =
Subsemigroup.closure (Additive.ofMul ⁻¹' S) :=
le_antisymm
(AddSubsemigroup.toSubsemigroup'.le_symm_apply.1 <|
AddSubsemigroup.closure_le.2 (Subsemigroup.subset_closure (M := M)))
(Subsemigroup.closure_le.2 <| AddSubsemigroup.subset_closure (M := Additive M))
end
section
variable {A : Type*} [Add A]
/-- Additive subsemigroups of an additive semigroup `A` are isomorphic to
multiplicative subsemigroups of `Multiplicative A`. -/
@[simps]
def AddSubsemigroup.toSubsemigroup : AddSubsemigroup A ≃o Subsemigroup (Multiplicative A) where
toFun S :=
{ carrier := Multiplicative.toAdd ⁻¹' S
mul_mem' := S.add_mem' }
invFun S :=
{ carrier := Multiplicative.ofAdd ⁻¹' S
add_mem' := S.mul_mem' }
map_rel_iff' := Iff.rfl
/-- Subsemigroups of a semigroup `Multiplicative A` are isomorphic to additive subsemigroups
of `A`. -/
abbrev Subsemigroup.toAddSubsemigroup' : Subsemigroup (Multiplicative A) ≃o AddSubsemigroup A :=
AddSubsemigroup.toSubsemigroup.symm
theorem AddSubsemigroup.toSubsemigroup_closure (S : Set A) :
AddSubsemigroup.toSubsemigroup (AddSubsemigroup.closure S) =
Subsemigroup.closure (Multiplicative.toAdd ⁻¹' S) :=
le_antisymm
(AddSubsemigroup.toSubsemigroup.to_galoisConnection.l_le <|
AddSubsemigroup.closure_le.2 <| Subsemigroup.subset_closure (M := Multiplicative A))
(Subsemigroup.closure_le.2 <| AddSubsemigroup.subset_closure (M := A))
theorem Subsemigroup.toAddSubsemigroup'_closure (S : Set (Multiplicative A)) :
Subsemigroup.toAddSubsemigroup' (Subsemigroup.closure S) =
AddSubsemigroup.closure (Multiplicative.ofAdd ⁻¹' S) :=
le_antisymm
(Subsemigroup.toAddSubsemigroup'.to_galoisConnection.l_le <|
Subsemigroup.closure_le.2 <| AddSubsemigroup.subset_closure (M := A))
(AddSubsemigroup.closure_le.2 <| Subsemigroup.subset_closure (M := Multiplicative A))
end
namespace Subsemigroup
open Set
/-!
### `comap` and `map`
-/
variable [Mul M] [Mul N] [Mul P] (S : Subsemigroup M)
/-- The preimage of a subsemigroup along a semigroup homomorphism is a subsemigroup. -/
@[to_additive
/-- The preimage of an `AddSubsemigroup` along an `AddSemigroup` homomorphism is an
`AddSubsemigroup`. -/]
def comap (f : M →ₙ* N) (S : Subsemigroup N) :
Subsemigroup M where
carrier := f ⁻¹' S
mul_mem' ha hb := show f (_ * _) ∈ S by rw [map_mul]; exact mul_mem ha hb
@[to_additive (attr := simp)]
theorem coe_comap (S : Subsemigroup N) (f : M →ₙ* N) : (S.comap f : Set M) = f ⁻¹' S :=
rfl
@[to_additive (attr := simp)]
theorem mem_comap {S : Subsemigroup N} {f : M →ₙ* N} {x : M} : x ∈ S.comap f ↔ f x ∈ S :=
Iff.rfl
@[to_additive]
theorem comap_comap (S : Subsemigroup P) (g : N →ₙ* P) (f : M →ₙ* N) :
(S.comap g).comap f = S.comap (g.comp f) :=
rfl
@[to_additive (attr := simp)]
theorem comap_id (S : Subsemigroup P) : S.comap (MulHom.id _) = S :=
ext (by simp)
/-- The image of a subsemigroup along a semigroup homomorphism is a subsemigroup. -/
@[to_additive
/-- The image of an `AddSubsemigroup` along an `AddSemigroup` homomorphism is
an `AddSubsemigroup`. -/]
def map (f : M →ₙ* N) (S : Subsemigroup M) : Subsemigroup N where
carrier := f '' S
mul_mem' := by
rintro _ _ ⟨x, hx, rfl⟩ ⟨y, hy, rfl⟩
exact ⟨x * y, @mul_mem (Subsemigroup M) M _ _ _ _ _ _ hx hy, by rw [map_mul]⟩
@[to_additive (attr := simp)]
theorem coe_map (f : M →ₙ* N) (S : Subsemigroup M) : (S.map f : Set N) = f '' S :=
rfl
@[to_additive (attr := simp)]
theorem mem_map {f : M →ₙ* N} {S : Subsemigroup M} {y : N} : y ∈ S.map f ↔ ∃ x ∈ S, f x = y :=
mem_image _ _ _
@[to_additive]
theorem mem_map_of_mem (f : M →ₙ* N) {S : Subsemigroup M} {x : M} (hx : x ∈ S) : f x ∈ S.map f :=
mem_image_of_mem f hx
@[to_additive]
theorem apply_coe_mem_map (f : M →ₙ* N) (S : Subsemigroup M) (x : S) : f x ∈ S.map f :=
mem_map_of_mem f x.prop
@[to_additive]
theorem map_map (g : N →ₙ* P) (f : M →ₙ* N) : (S.map f).map g = S.map (g.comp f) :=
SetLike.coe_injective <| image_image _ _ _
@[to_additive (attr := simp high)]
theorem mem_map_iff_mem {f : M →ₙ* N} (hf : Function.Injective f) {S : Subsemigroup M} {x : M} :
f x ∈ S.map f ↔ x ∈ S :=
hf.mem_set_image
@[to_additive]
theorem map_le_iff_le_comap {f : M →ₙ* N} {S : Subsemigroup M} {T : Subsemigroup N} :
S.map f ≤ T ↔ S ≤ T.comap f :=
image_subset_iff
@[to_additive]
theorem gc_map_comap (f : M →ₙ* N) : GaloisConnection (map f) (comap f) := fun _ _ =>
map_le_iff_le_comap
@[to_additive]
theorem map_le_of_le_comap {T : Subsemigroup N} {f : M →ₙ* N} : S ≤ T.comap f → S.map f ≤ T :=
(gc_map_comap f).l_le
@[to_additive]
theorem le_comap_of_map_le {T : Subsemigroup N} {f : M →ₙ* N} : S.map f ≤ T → S ≤ T.comap f :=
(gc_map_comap f).le_u
@[to_additive]
theorem le_comap_map {f : M →ₙ* N} : S ≤ (S.map f).comap f :=
(gc_map_comap f).le_u_l _
@[to_additive]
theorem map_comap_le {S : Subsemigroup N} {f : M →ₙ* N} : (S.comap f).map f ≤ S :=
(gc_map_comap f).l_u_le _
@[to_additive]
theorem monotone_map {f : M →ₙ* N} : Monotone (map f) :=
(gc_map_comap f).monotone_l
@[to_additive]
theorem monotone_comap {f : M →ₙ* N} : Monotone (comap f) :=
(gc_map_comap f).monotone_u
@[to_additive (attr := simp)]
theorem map_comap_map {f : M →ₙ* N} : ((S.map f).comap f).map f = S.map f :=
(gc_map_comap f).l_u_l_eq_l _
@[to_additive (attr := simp)]
theorem comap_map_comap {S : Subsemigroup N} {f : M →ₙ* N} :
((S.comap f).map f).comap f = S.comap f :=
(gc_map_comap f).u_l_u_eq_u _
@[to_additive]
theorem map_sup (S T : Subsemigroup M) (f : M →ₙ* N) : (S ⊔ T).map f = S.map f ⊔ T.map f :=
(gc_map_comap f).l_sup
@[to_additive]
theorem map_iSup {ι : Sort*} (f : M →ₙ* N) (s : ι → Subsemigroup M) :
(iSup s).map f = ⨆ i, (s i).map f :=
(gc_map_comap f).l_iSup
@[to_additive]
theorem map_inf (S T : Subsemigroup M) (f : M →ₙ* N) (hf : Function.Injective f) :
(S ⊓ T).map f = S.map f ⊓ T.map f := SetLike.coe_injective (Set.image_inter hf)
@[to_additive]
theorem map_iInf {ι : Sort*} [Nonempty ι] (f : M →ₙ* N) (hf : Function.Injective f)
(s : ι → Subsemigroup M) : (iInf s).map f = ⨅ i, (s i).map f := by
apply SetLike.coe_injective
simpa using (Set.injOn_of_injective hf).image_iInter_eq (s := SetLike.coe ∘ s)
@[to_additive]
theorem comap_inf (S T : Subsemigroup N) (f : M →ₙ* N) : (S ⊓ T).comap f = S.comap f ⊓ T.comap f :=
(gc_map_comap f).u_inf
@[to_additive]
theorem comap_iInf {ι : Sort*} (f : M →ₙ* N) (s : ι → Subsemigroup N) :
(iInf s).comap f = ⨅ i, (s i).comap f :=
(gc_map_comap f).u_iInf
@[to_additive (attr := simp)]
theorem map_bot (f : M →ₙ* N) : (⊥ : Subsemigroup M).map f = ⊥ :=
(gc_map_comap f).l_bot
@[to_additive (attr := simp)]
theorem comap_top (f : M →ₙ* N) : (⊤ : Subsemigroup N).comap f = ⊤ :=
(gc_map_comap f).u_top
@[to_additive (attr := simp)]
theorem map_id (S : Subsemigroup M) : S.map (MulHom.id M) = S :=
ext fun _ => ⟨fun ⟨_, h, rfl⟩ => h, fun h => ⟨_, h, rfl⟩⟩
section GaloisCoinsertion
variable {ι : Type*} {f : M →ₙ* N}
/-- `map f` and `comap f` form a `GaloisCoinsertion` when `f` is injective. -/
@[to_additive /-- `map f` and `comap f` form a `GaloisCoinsertion` when `f` is injective. -/]
def gciMapComap (hf : Function.Injective f) : GaloisCoinsertion (map f) (comap f) :=
(gc_map_comap f).toGaloisCoinsertion fun S x => by simp [mem_comap, mem_map, hf.eq_iff]
variable (hf : Function.Injective f)
include hf
@[to_additive]
theorem comap_map_eq_of_injective (S : Subsemigroup M) : (S.map f).comap f = S :=
(gciMapComap hf).u_l_eq _
@[to_additive]
theorem comap_surjective_of_injective : Function.Surjective (comap f) :=
(gciMapComap hf).u_surjective
@[to_additive]
theorem map_injective_of_injective : Function.Injective (map f) :=
(gciMapComap hf).l_injective
@[to_additive]
theorem comap_inf_map_of_injective (S T : Subsemigroup M) : (S.map f ⊓ T.map f).comap f = S ⊓ T :=
(gciMapComap hf).u_inf_l _ _
@[to_additive]
theorem comap_iInf_map_of_injective (S : ι → Subsemigroup M) :
(⨅ i, (S i).map f).comap f = iInf S :=
(gciMapComap hf).u_iInf_l _
@[to_additive]
theorem comap_sup_map_of_injective (S T : Subsemigroup M) : (S.map f ⊔ T.map f).comap f = S ⊔ T :=
(gciMapComap hf).u_sup_l _ _
@[to_additive]
theorem comap_iSup_map_of_injective (S : ι → Subsemigroup M) :
(⨆ i, (S i).map f).comap f = iSup S :=
(gciMapComap hf).u_iSup_l _
@[to_additive]
theorem map_le_map_iff_of_injective {S T : Subsemigroup M} : S.map f ≤ T.map f ↔ S ≤ T :=
(gciMapComap hf).l_le_l_iff
@[to_additive]
theorem map_strictMono_of_injective : StrictMono (map f) :=
(gciMapComap hf).strictMono_l
end GaloisCoinsertion
section GaloisInsertion
variable {ι : Type*} {f : M →ₙ* N} (hf : Function.Surjective f)
include hf
/-- `map f` and `comap f` form a `GaloisInsertion` when `f` is surjective. -/
@[to_additive /-- `map f` and `comap f` form a `GaloisInsertion` when `f` is surjective. -/]
def giMapComap : GaloisInsertion (map f) (comap f) :=
(gc_map_comap f).toGaloisInsertion fun S x h =>
let ⟨y, hy⟩ := hf x
mem_map.2 ⟨y, by simp [hy, h]⟩
@[to_additive]
theorem map_comap_eq_of_surjective (S : Subsemigroup N) : (S.comap f).map f = S :=
(giMapComap hf).l_u_eq _
@[to_additive]
theorem map_surjective_of_surjective : Function.Surjective (map f) :=
(giMapComap hf).l_surjective
@[to_additive]
theorem comap_injective_of_surjective : Function.Injective (comap f) :=
(giMapComap hf).u_injective
@[to_additive]
theorem map_inf_comap_of_surjective (S T : Subsemigroup N) :
(S.comap f ⊓ T.comap f).map f = S ⊓ T :=
(giMapComap hf).l_inf_u _ _
@[to_additive]
theorem map_iInf_comap_of_surjective (S : ι → Subsemigroup N) :
(⨅ i, (S i).comap f).map f = iInf S :=
(giMapComap hf).l_iInf_u _
@[to_additive]
theorem map_sup_comap_of_surjective (S T : Subsemigroup N) :
(S.comap f ⊔ T.comap f).map f = S ⊔ T :=
(giMapComap hf).l_sup_u _ _
@[to_additive]
theorem map_iSup_comap_of_surjective (S : ι → Subsemigroup N) :
(⨆ i, (S i).comap f).map f = iSup S :=
(giMapComap hf).l_iSup_u _
@[to_additive]
theorem comap_le_comap_iff_of_surjective {S T : Subsemigroup N} : S.comap f ≤ T.comap f ↔ S ≤ T :=
(giMapComap hf).u_le_u_iff
@[to_additive]
theorem comap_strictMono_of_surjective : StrictMono (comap f) :=
(giMapComap hf).strictMono_u
end GaloisInsertion
end Subsemigroup
namespace Subsemigroup
variable [Mul M] [Mul N] [Mul P] (S : Subsemigroup M)
/-- The top subsemigroup is isomorphic to the semigroup. -/
@[to_additive (attr := simps)
/-- The top additive subsemigroup is isomorphic to the additive semigroup. -/]
def topEquiv : (⊤ : Subsemigroup M) ≃* M where
toFun x := x
invFun x := ⟨x, mem_top x⟩
left_inv x := x.eta _
map_mul' _ _ := rfl
@[to_additive (attr := simp)]
theorem topEquiv_toMulHom :
((topEquiv : _ ≃* M) : _ →ₙ* M) = MulMemClass.subtype (⊤ : Subsemigroup M) :=
rfl
/-- A subsemigroup is isomorphic to its image under an injective function -/
@[to_additive
/-- An additive subsemigroup is isomorphic to its image under an injective function -/]
noncomputable def equivMapOfInjective (f : M →ₙ* N) (hf : Function.Injective f) : S ≃* S.map f :=
{ Equiv.Set.image f S hf with map_mul' := fun _ _ => Subtype.ext (map_mul f _ _) }
@[to_additive (attr := simp)]
theorem coe_equivMapOfInjective_apply (f : M →ₙ* N) (hf : Function.Injective f) (x : S) :
(equivMapOfInjective S f hf x : N) = f x :=
rfl
@[to_additive (attr := simp)]
theorem closure_closure_coe_preimage {s : Set M} :
closure ((Subtype.val : closure s → M) ⁻¹' s) = ⊤ :=
eq_top_iff.2 fun x _ ↦ Subtype.recOn x fun _ hx' ↦
closure_induction (fun _ h ↦ subset_closure h) (fun _ _ _ _ ↦ mul_mem) hx'
/-- Given `Subsemigroup`s `s`, `t` of semigroups `M`, `N` respectively, `s × t` as a subsemigroup
of `M × N`. -/
@[to_additive prod
/-- Given `AddSubsemigroup`s `s`, `t` of `AddSemigroup`s `A`, `B` respectively,
`s × t` as an `AddSubsemigroup` of `A × B`. -/]
def prod (s : Subsemigroup M) (t : Subsemigroup N) : Subsemigroup (M × N) where
carrier := s ×ˢ t
mul_mem' hp hq := ⟨s.mul_mem hp.1 hq.1, t.mul_mem hp.2 hq.2⟩
@[to_additive (attr := norm_cast) coe_prod]
theorem coe_prod (s : Subsemigroup M) (t : Subsemigroup N) :
(s.prod t : Set (M × N)) = (s : Set M) ×ˢ (t : Set N) :=
rfl
@[to_additive mem_prod]
theorem mem_prod {s : Subsemigroup M} {t : Subsemigroup N} {p : M × N} :
p ∈ s.prod t ↔ p.1 ∈ s ∧ p.2 ∈ t :=
Iff.rfl
@[to_additive prod_mono]
theorem prod_mono {s₁ s₂ : Subsemigroup M} {t₁ t₂ : Subsemigroup N} (hs : s₁ ≤ s₂) (ht : t₁ ≤ t₂) :
s₁.prod t₁ ≤ s₂.prod t₂ :=
Set.prod_mono hs ht
@[to_additive prod_top]
theorem prod_top (s : Subsemigroup M) : s.prod (⊤ : Subsemigroup N) = s.comap (MulHom.fst M N) :=
ext fun x => by simp [mem_prod, MulHom.coe_fst]
@[to_additive top_prod]
theorem top_prod (s : Subsemigroup N) : (⊤ : Subsemigroup M).prod s = s.comap (MulHom.snd M N) :=
ext fun x => by simp [mem_prod, MulHom.coe_snd]
@[to_additive (attr := simp) top_prod_top]
theorem top_prod_top : (⊤ : Subsemigroup M).prod (⊤ : Subsemigroup N) = ⊤ :=
(top_prod _).trans <| comap_top _
@[to_additive bot_prod_bot]
theorem bot_prod_bot : (⊥ : Subsemigroup M).prod (⊥ : Subsemigroup N) = ⊥ :=
SetLike.coe_injective <| by simp [coe_prod]
/-- The product of subsemigroups is isomorphic to their product as semigroups. -/
@[to_additive prodEquiv
/-- The product of additive subsemigroups is isomorphic to their product as additive semigroups -/]
def prodEquiv (s : Subsemigroup M) (t : Subsemigroup N) : s.prod t ≃* s × t :=
{ (Equiv.Set.prod (s : Set M) (t : Set N)) with
map_mul' := fun _ _ => rfl }
open MulHom
@[to_additive]
theorem mem_map_equiv {f : M ≃* N} {K : Subsemigroup M} {x : N} :
x ∈ K.map (f : M →ₙ* N) ↔ f.symm x ∈ K :=
@Set.mem_image_equiv _ _ (K : Set M) f.toEquiv x
@[to_additive]
theorem map_equiv_eq_comap_symm (f : M ≃* N) (K : Subsemigroup M) :
K.map (f : M →ₙ* N) = K.comap (f.symm : N →ₙ* M) :=
SetLike.coe_injective (f.toEquiv.image_eq_preimage_symm K)
@[to_additive]
theorem comap_equiv_eq_map_symm (f : N ≃* M) (K : Subsemigroup M) :
K.comap (f : N →ₙ* M) = K.map (f.symm : M →ₙ* N) :=
(map_equiv_eq_comap_symm f.symm K).symm
@[to_additive (attr := simp)]
theorem map_equiv_top (f : M ≃* N) : (⊤ : Subsemigroup M).map (f : M →ₙ* N) = ⊤ :=
SetLike.coe_injective <| Set.image_univ.trans f.surjective.range_eq
@[to_additive le_prod_iff]
theorem le_prod_iff {s : Subsemigroup M} {t : Subsemigroup N} {u : Subsemigroup (M × N)} :
u ≤ s.prod t ↔ u.map (fst M N) ≤ s ∧ u.map (snd M N) ≤ t := by
constructor
· intro h
constructor
· rintro x ⟨⟨y1, y2⟩, ⟨hy1, rfl⟩⟩
exact (h hy1).1
· rintro x ⟨⟨y1, y2⟩, ⟨hy1, rfl⟩⟩
exact (h hy1).2
· rintro ⟨hH, hK⟩ ⟨x1, x2⟩ h
exact ⟨hH ⟨_, h, rfl⟩, hK ⟨_, h, rfl⟩⟩
end Subsemigroup
namespace MulHom
open Subsemigroup
variable [Mul M] [Mul N] [Mul P] (S : Subsemigroup M)
/-- The range of a semigroup homomorphism is a subsemigroup. See Note [range copy pattern]. -/
@[to_additive /-- The range of an `AddHom` is an `AddSubsemigroup`. -/]
def srange (f : M →ₙ* N) : Subsemigroup N :=
((⊤ : Subsemigroup M).map f).copy (Set.range f) Set.image_univ.symm
@[to_additive (attr := simp)]
theorem coe_srange (f : M →ₙ* N) : (f.srange : Set N) = Set.range f :=
rfl
@[to_additive (attr := simp)]
theorem mem_srange {f : M →ₙ* N} {y : N} : y ∈ f.srange ↔ ∃ x, f x = y :=
Iff.rfl
@[to_additive]
private theorem srange_mk_aux_mul {f : M → N} (hf : ∀ (x y : M), f (x * y) = f x * f y)
{x y : N} (hx : x ∈ Set.range f) (hy : y ∈ Set.range f) :
x * y ∈ Set.range f :=
(srange ⟨f, hf⟩).mul_mem hx hy
@[to_additive (attr := simp)] theorem srange_mk (f : M → N) (hf) :
srange ⟨f, hf⟩ = ⟨Set.range f, srange_mk_aux_mul hf⟩ := rfl
@[to_additive]
theorem srange_eq_map (f : M →ₙ* N) : f.srange = (⊤ : Subsemigroup M).map f :=
copy_eq _
@[to_additive]
theorem map_srange (g : N →ₙ* P) (f : M →ₙ* N) : f.srange.map g = (g.comp f).srange := by
simpa only [srange_eq_map] using (⊤ : Subsemigroup M).map_map g f
@[to_additive]
theorem srange_eq_top_iff_surjective {N} [Mul N] {f : M →ₙ* N} :
f.srange = (⊤ : Subsemigroup N) ↔ Function.Surjective f :=
SetLike.ext'_iff.trans <| Iff.trans (by rw [coe_srange, coe_top]) Set.range_eq_univ
/-- The range of a surjective semigroup hom is the whole of the codomain. -/
@[to_additive (attr := simp)
/-- The range of a surjective `AddSemigroup` hom is the whole of the codomain. -/]
theorem srange_eq_top_of_surjective {N} [Mul N] (f : M →ₙ* N) (hf : Function.Surjective f) :
f.srange = (⊤ : Subsemigroup N) :=
srange_eq_top_iff_surjective.2 hf
@[to_additive]
theorem mclosure_preimage_le (f : M →ₙ* N) (s : Set N) : closure (f ⁻¹' s) ≤ (closure s).comap f :=
closure_le.2 fun _ hx => SetLike.mem_coe.2 <| mem_comap.2 <| subset_closure hx
/-- The image under a semigroup hom of the subsemigroup generated by a set equals the subsemigroup
generated by the image of the set. -/
@[to_additive
/-- The image under an `AddSemigroup` hom of the `AddSubsemigroup` generated by a set
equals the `AddSubsemigroup` generated by the image of the set. -/]
theorem map_mclosure (f : M →ₙ* N) (s : Set M) : (closure s).map f = closure (f '' s) :=
Set.image_preimage.l_comm_of_u_comm (gc_map_comap f) (Subsemigroup.gi N).gc (Subsemigroup.gi M).gc
fun _ ↦ rfl
/-- Restriction of a semigroup hom to a subsemigroup of the domain. -/
@[to_additive /-- Restriction of an AddSemigroup hom to an `AddSubsemigroup` of the domain. -/]
def restrict {N : Type*} [Mul N] [SetLike σ M] [MulMemClass σ M] (f : M →ₙ* N) (S : σ) : S →ₙ* N :=
f.comp (MulMemClass.subtype S)
@[to_additive (attr := simp)]
theorem restrict_apply {N : Type*} [Mul N] [SetLike σ M] [MulMemClass σ M] (f : M →ₙ* N) {S : σ}
(x : S) : f.restrict S x = f x :=
rfl
/-- Restriction of a semigroup hom to a subsemigroup of the codomain. -/
@[to_additive (attr := simps)
/-- Restriction of an `AddSemigroup` hom to an `AddSubsemigroup` of the codomain. -/]
def codRestrict [SetLike σ N] [MulMemClass σ N] (f : M →ₙ* N) (S : σ) (h : ∀ x, f x ∈ S) :
M →ₙ* S where
toFun n := ⟨f n, h n⟩
map_mul' x y := Subtype.eq (map_mul f x y)
/-- Restriction of a semigroup hom to its range interpreted as a subsemigroup. -/
@[to_additive
/-- Restriction of an `AddSemigroup` hom to its range interpreted as a subsemigroup. -/]
def srangeRestrict {N} [Mul N] (f : M →ₙ* N) : M →ₙ* f.srange :=
(f.codRestrict f.srange) fun x => ⟨x, rfl⟩
@[to_additive (attr := simp)]
theorem coe_srangeRestrict {N} [Mul N] (f : M →ₙ* N) (x : M) : (f.srangeRestrict x : N) = f x :=
rfl
@[to_additive]
theorem srangeRestrict_surjective (f : M →ₙ* N) : Function.Surjective f.srangeRestrict :=
fun ⟨_, ⟨x, rfl⟩⟩ => ⟨x, rfl⟩
@[to_additive prod_map_comap_prod']
theorem prod_map_comap_prod' {M' : Type*} {N' : Type*} [Mul M'] [Mul N'] (f : M →ₙ* N)
(g : M' →ₙ* N') (S : Subsemigroup N) (S' : Subsemigroup N') :
(S.prod S').comap (prodMap f g) = (S.comap f).prod (S'.comap g) :=
SetLike.coe_injective <| Set.preimage_prod_map_prod f g _ _
/-- The `MulHom` from the preimage of a subsemigroup to itself. -/
@[to_additive (attr := simps)
/-- The `AddHom` from the preimage of an additive subsemigroup to itself. -/]
def subsemigroupComap (f : M →ₙ* N) (N' : Subsemigroup N) :
N'.comap f →ₙ* N' where
toFun x := ⟨f x, x.prop⟩
map_mul' x y := Subtype.eq <| map_mul (M := M) (N := N) f x y
/-- The `MulHom` from a subsemigroup to its image.
See `MulEquiv.subsemigroupMap` for a variant for `MulEquiv`s. -/
@[to_additive (attr := simps)
/-- the `AddHom` from an additive subsemigroup to its image. See
`AddEquiv.addSubsemigroupMap` for a variant for `AddEquiv`s. -/]
def subsemigroupMap (f : M →ₙ* N) (M' : Subsemigroup M) :
M' →ₙ* M'.map f where
toFun x := ⟨f x, ⟨x, x.prop, rfl⟩⟩
map_mul' x y := Subtype.eq <| map_mul (M := M) (N := N) f x y
@[to_additive]
theorem subsemigroupMap_surjective (f : M →ₙ* N) (M' : Subsemigroup M) :
Function.Surjective (f.subsemigroupMap M') := by
rintro ⟨_, x, hx, rfl⟩
exact ⟨⟨x, hx⟩, rfl⟩
end MulHom
namespace Subsemigroup
open MulHom
variable [Mul M] [Mul N] [Mul P] (S : Subsemigroup M)
@[to_additive (attr := simp)]
theorem srange_fst [Nonempty N] : (fst M N).srange = ⊤ :=
(fst M N).srange_eq_top_of_surjective <| Prod.fst_surjective
@[to_additive (attr := simp)]
theorem srange_snd [Nonempty M] : (snd M N).srange = ⊤ :=
(snd M N).srange_eq_top_of_surjective <| Prod.snd_surjective
@[to_additive prod_eq_top_iff]
theorem prod_eq_top_iff [Nonempty M] [Nonempty N] {s : Subsemigroup M} {t : Subsemigroup N} :
s.prod t = ⊤ ↔ s = ⊤ ∧ t = ⊤ := by
simp only [eq_top_iff, le_prod_iff, ← srange_eq_map, srange_fst, srange_snd]
/-- The semigroup hom associated to an inclusion of subsemigroups. -/
@[to_additive /-- The `AddSemigroup` hom associated to an inclusion of subsemigroups. -/]
def inclusion {S T : Subsemigroup M} (h : S ≤ T) : S →ₙ* T :=
(MulMemClass.subtype S).codRestrict _ fun x => h x.2
@[to_additive (attr := simp)]
theorem range_subtype (s : Subsemigroup M) : (MulMemClass.subtype s).srange = s :=
SetLike.coe_injective <| (coe_srange _).trans <| Subtype.range_coe
@[to_additive]
theorem eq_top_iff' : S = ⊤ ↔ ∀ x : M, x ∈ S :=
eq_top_iff.trans ⟨fun h m => h <| mem_top m, fun h m _ => h m⟩
end Subsemigroup
namespace MulEquiv
variable [Mul M] [Mul N] {S T : Subsemigroup M}
/-- Makes the identity isomorphism from a proof that two subsemigroups of a multiplicative
semigroup are equal. -/
@[to_additive
/-- Makes the identity additive isomorphism from a proof two
subsemigroups of an additive semigroup are equal. -/]
def subsemigroupCongr (h : S = T) : S ≃* T :=
{ Equiv.setCongr <| congr_arg _ h with map_mul' := fun _ _ => rfl }
-- this name is primed so that the version to `f.range` instead of `f.srange` can be unprimed.
/-- A semigroup homomorphism `f : M →ₙ* N` with a left-inverse `g : N → M` defines a multiplicative
equivalence between `M` and `f.srange`.
This is a bidirectional version of `MulHom.srangeRestrict`. -/
@[to_additive (attr := simps +simpRhs)
/-- An additive semigroup homomorphism `f : M →+ N` with a left-inverse
`g : N → M` defines an additive equivalence between `M` and `f.srange`.
This is a bidirectional version of `AddHom.srangeRestrict`. -/]
def ofLeftInverse (f : M →ₙ* N) {g : N → M} (h : Function.LeftInverse g f) : M ≃* f.srange :=
{ f.srangeRestrict with
toFun := f.srangeRestrict
invFun := g ∘ MulMemClass.subtype f.srange
left_inv := h
right_inv := fun x =>
Subtype.ext <|
let ⟨x', hx'⟩ := MulHom.mem_srange.mp x.prop
show f (g x) = x by rw [← hx', h x'] }
/-- A `MulEquiv` `φ` between two semigroups `M` and `N` induces a `MulEquiv` between
a subsemigroup `S ≤ M` and the subsemigroup `φ(S) ≤ N`.
See `MulHom.subsemigroupMap` for a variant for `MulHom`s. -/
@[to_additive (attr := simps)
/-- An `AddEquiv` `φ` between two additive semigroups `M` and `N` induces an `AddEquiv`
between a subsemigroup `S ≤ M` and the subsemigroup `φ(S) ≤ N`.
See `AddHom.addSubsemigroupMap` for a variant for `AddHom`s. -/]
def subsemigroupMap (e : M ≃* N) (S : Subsemigroup M) : S ≃* S.map (e : M →ₙ* N) :=
{ -- we restate this for `simps` to avoid `⇑e.symm.toEquiv x`
(e : M →ₙ* N).subsemigroupMap S,
(e : M ≃ N).image S with
toFun := fun x => ⟨e x, _⟩
invFun := fun x => ⟨e.symm x, _⟩ }
end MulEquiv
namespace Subsemigroup
variable [Mul M] [Mul N]
@[to_additive]
theorem map_comap_eq (f : M →ₙ* N) (S : Subsemigroup N) : (S.comap f).map f = S ⊓ f.srange :=
SetLike.coe_injective Set.image_preimage_eq_inter_range
@[to_additive]
theorem map_comap_eq_self {f : M →ₙ* N} {S : Subsemigroup N} (h : S ≤ f.srange) :
(S.comap f).map f = S := by
simpa only [inf_of_le_left h] using map_comap_eq f S
end Subsemigroup |
.lake/packages/mathlib/Mathlib/Algebra/Group/Subsemigroup/Defs.lean | import Mathlib.Algebra.Group.Hom.Defs
import Mathlib.Algebra.Group.InjSurj
import Mathlib.Data.SetLike.Basic
import Mathlib.Tactic.FastInstance
/-!
# Subsemigroups: definition
This file defines bundled multiplicative and additive subsemigroups.
## Main definitions
* `Subsemigroup M`: the type of bundled subsemigroup of a magma `M`; the underlying set is given in
the `carrier` field of the structure, and should be accessed through coercion as in `(S : Set M)`.
* `AddSubsemigroup M` : the type of bundled subsemigroups of an additive magma `M`.
For each of the following definitions in the `Subsemigroup` namespace, there is a corresponding
definition in the `AddSubsemigroup` namespace.
* `Subsemigroup.copy` : copy of a subsemigroup with `carrier` replaced by a set that is equal but
possibly not definitionally equal to the carrier of the original `Subsemigroup`.
Similarly, for each of these definitions in the `MulHom` namespace, there is a corresponding
definition in the `AddHom` namespace.
* `MulHom.eqLocus f g`: the subsemigroup of those `x` such that `f x = g x`
## Implementation notes
Subsemigroup inclusion is denoted `≤` rather than `⊆`, although `∈` is defined as
membership of a subsemigroup's underlying set.
Note that `Subsemigroup M` does not actually require `Semigroup M`,
instead requiring only the weaker `Mul M`.
This file is designed to have very few dependencies. In particular, it should not use natural
numbers.
## Tags
subsemigroup, subsemigroups
-/
assert_not_exists RelIso CompleteLattice MonoidWithZero
variable {M : Type*} {N : Type*}
section NonAssoc
variable [Mul M] {s : Set M}
/-- `MulMemClass S M` says `S` is a type of sets `s : Set M` that are closed under `(*)` -/
class MulMemClass (S : Type*) (M : outParam Type*) [Mul M] [SetLike S M] : Prop where
/-- A substructure satisfying `MulMemClass` is closed under multiplication. -/
mul_mem : ∀ {s : S} {a b : M}, a ∈ s → b ∈ s → a * b ∈ s
export MulMemClass (mul_mem)
/-- `AddMemClass S M` says `S` is a type of sets `s : Set M` that are closed under `(+)` -/
class AddMemClass (S : Type*) (M : outParam Type*) [Add M] [SetLike S M] : Prop where
/-- A substructure satisfying `AddMemClass` is closed under addition. -/
add_mem : ∀ {s : S} {a b : M}, a ∈ s → b ∈ s → a + b ∈ s
export AddMemClass (add_mem)
attribute [to_additive] MulMemClass
attribute [aesop 90% (rule_sets := [SetLike])] mul_mem add_mem
/-- A subsemigroup of a magma `M` is a subset closed under multiplication. -/
structure Subsemigroup (M : Type*) [Mul M] where
/-- The carrier of a subsemigroup. -/
carrier : Set M
/-- The product of two elements of a subsemigroup belongs to the subsemigroup. -/
mul_mem' {a b} : a ∈ carrier → b ∈ carrier → a * b ∈ carrier
/-- An additive subsemigroup of an additive magma `M` is a subset closed under addition. -/
structure AddSubsemigroup (M : Type*) [Add M] where
/-- The carrier of an additive subsemigroup. -/
carrier : Set M
/-- The sum of two elements of an additive subsemigroup belongs to the subsemigroup. -/
add_mem' {a b} : a ∈ carrier → b ∈ carrier → a + b ∈ carrier
attribute [to_additive AddSubsemigroup] Subsemigroup
namespace Subsemigroup
@[to_additive]
instance : SetLike (Subsemigroup M) M :=
⟨Subsemigroup.carrier, fun p q h => by cases p; cases q; congr⟩
initialize_simps_projections Subsemigroup (carrier → coe, as_prefix coe)
initialize_simps_projections AddSubsemigroup (carrier → coe, as_prefix coe)
/-- The actual `Subsemigroup` obtained from an element of a `MulMemClass`. -/
@[to_additive (attr := simps) /-- The actual `AddSubsemigroup` obtained from an element of a
`AddMemClass` -/]
def ofClass {S M : Type*} [Mul M] [SetLike S M] [MulMemClass S M] (s : S) : Subsemigroup M :=
⟨s, MulMemClass.mul_mem⟩
@[to_additive]
instance (priority := 100) : CanLift (Set M) (Subsemigroup M) (↑)
(fun s ↦ ∀ {x y}, x ∈ s → y ∈ s → x * y ∈ s) where
prf s h := ⟨{ carrier := s, mul_mem' := h }, rfl⟩
@[to_additive]
instance : MulMemClass (Subsemigroup M) M where mul_mem := fun {_ _ _} => Subsemigroup.mul_mem' _
@[to_additive (attr := simp)]
theorem mem_carrier {s : Subsemigroup M} {x : M} : x ∈ s.carrier ↔ x ∈ s :=
Iff.rfl
@[to_additive (attr := simp)]
theorem mem_mk {s : Set M} {x : M} (h_mul) : x ∈ mk s h_mul ↔ x ∈ s :=
Iff.rfl
@[to_additive (attr := simp, norm_cast)]
theorem coe_set_mk (s : Set M) (h_mul) : (mk s h_mul : Set M) = s :=
rfl
@[to_additive (attr := simp)]
theorem mk_le_mk {s t : Set M} (h_mul) (h_mul') : mk s h_mul ≤ mk t h_mul' ↔ s ⊆ t :=
Iff.rfl
/-- Two subsemigroups are equal if they have the same elements. -/
@[to_additive (attr := ext) /-- Two `AddSubsemigroup`s are equal if they have the same elements. -/]
theorem ext {S T : Subsemigroup M} (h : ∀ x, x ∈ S ↔ x ∈ T) : S = T :=
SetLike.ext h
/-- Copy a subsemigroup replacing `carrier` with a set that is equal to it. -/
@[to_additive
/-- Copy an additive subsemigroup replacing `carrier` with a set that is equal to it. -/]
protected def copy (S : Subsemigroup M) (s : Set M) (hs : s = S) :
Subsemigroup M where
carrier := s
mul_mem' := hs.symm ▸ S.mul_mem'
variable {S : Subsemigroup M}
@[to_additive (attr := simp, norm_cast)]
theorem coe_copy {s : Set M} (hs : s = S) : (S.copy s hs : Set M) = s :=
rfl
@[to_additive]
theorem copy_eq {s : Set M} (hs : s = S) : S.copy s hs = S :=
SetLike.coe_injective hs
variable (S)
/-- A subsemigroup is closed under multiplication. -/
@[to_additive /-- An `AddSubsemigroup` is closed under addition. -/]
protected theorem mul_mem {x y : M} : x ∈ S → y ∈ S → x * y ∈ S :=
Subsemigroup.mul_mem' S
/-- The subsemigroup `M` of the magma `M`. -/
@[to_additive /-- The additive subsemigroup `M` of the magma `M`. -/]
instance : Top (Subsemigroup M) :=
⟨{ carrier := Set.univ
mul_mem' := fun _ _ => Set.mem_univ _ }⟩
/-- The trivial subsemigroup `∅` of a magma `M`. -/
@[to_additive /-- The trivial `AddSubsemigroup` `∅` of an additive magma `M`. -/]
instance : Bot (Subsemigroup M) :=
⟨{ carrier := ∅
mul_mem' := False.elim }⟩
@[to_additive]
instance : Inhabited (Subsemigroup M) :=
⟨⊥⟩
@[to_additive]
theorem notMem_bot {x : M} : x ∉ (⊥ : Subsemigroup M) :=
Set.notMem_empty x
@[deprecated (since := "2025-05-23")]
alias _root_.AddSubsemigroup.not_mem_bot := AddSubsemigroup.notMem_bot
@[to_additive existing, deprecated (since := "2025-05-23")] alias not_mem_bot := notMem_bot
@[to_additive (attr := simp)]
theorem mem_top (x : M) : x ∈ (⊤ : Subsemigroup M) :=
Set.mem_univ x
@[to_additive (attr := simp, norm_cast)]
theorem coe_top : ((⊤ : Subsemigroup M) : Set M) = Set.univ :=
rfl
@[to_additive (attr := simp, norm_cast)]
theorem coe_bot : ((⊥ : Subsemigroup M) : Set M) = ∅ :=
rfl
/-- The inf of two subsemigroups is their intersection. -/
@[to_additive /-- The inf of two `AddSubsemigroup`s is their intersection. -/]
instance : Min (Subsemigroup M) :=
⟨fun S₁ S₂ =>
{ carrier := S₁ ∩ S₂
mul_mem' := fun ⟨hx, hx'⟩ ⟨hy, hy'⟩ => ⟨S₁.mul_mem hx hy, S₂.mul_mem hx' hy'⟩ }⟩
@[to_additive (attr := simp, norm_cast)]
theorem coe_inf (p p' : Subsemigroup M) : ((p ⊓ p' : Subsemigroup M) : Set M) = (p : Set M) ∩ p' :=
rfl
@[to_additive (attr := simp)]
theorem mem_inf {p p' : Subsemigroup M} {x : M} : x ∈ p ⊓ p' ↔ x ∈ p ∧ x ∈ p' :=
Iff.rfl
@[to_additive]
theorem subsingleton_of_subsingleton [Subsingleton (Subsemigroup M)] : Subsingleton M := by
constructor; intro x y
have : ∀ a : M, a ∈ (⊥ : Subsemigroup M) := by simp [Subsingleton.elim (⊥ : Subsemigroup M) ⊤]
exact absurd (this x) notMem_bot
@[to_additive]
instance [hn : Nonempty M] : Nontrivial (Subsemigroup M) :=
⟨⟨⊥, ⊤, fun h => by
obtain ⟨x⟩ := id hn
refine absurd (?_ : x ∈ ⊥) notMem_bot
simp [h]⟩⟩
end Subsemigroup
namespace MulHom
variable [Mul N]
open Subsemigroup
/-- The subsemigroup of elements `x : M` such that `f x = g x` -/
@[to_additive /-- The additive subsemigroup of elements `x : M` such that `f x = g x` -/]
def eqLocus (f g : M →ₙ* N) : Subsemigroup M where
carrier := { x | f x = g x }
mul_mem' (hx : _ = _) (hy : _ = _) := by simp [*]
@[to_additive (attr := simp)]
theorem mem_eqLocus {f g : M →ₙ* N} {x : M} : x ∈ f.eqLocus g ↔ f x = g x := Iff.rfl
@[to_additive]
theorem eq_of_eqOn_top {f g : M →ₙ* N} (h : Set.EqOn f g (⊤ : Subsemigroup M)) : f = g :=
ext fun _ => h trivial
end MulHom
end NonAssoc
namespace MulMemClass
variable {A : Type*} [Mul M] [SetLike A M] [hA : MulMemClass A M] (S' : A)
-- lower priority so other instances are found first
/-- A submagma of a magma inherits a multiplication. -/
@[to_additive /-- An additive submagma of an additive magma inherits an addition. -/]
instance (priority := 900) mul : Mul S' :=
⟨fun a b => ⟨a.1 * b.1, mul_mem a.2 b.2⟩⟩
-- lower priority so later simp lemmas are used first; to appease simp_nf
@[to_additive (attr := simp low, norm_cast)]
theorem coe_mul (x y : S') : (↑(x * y) : M) = ↑x * ↑y :=
rfl
-- lower priority so later simp lemmas are used first; to appease simp_nf
@[to_additive (attr := simp low)]
theorem mk_mul_mk (x y : M) (hx : x ∈ S') (hy : y ∈ S') :
(⟨x, hx⟩ : S') * ⟨y, hy⟩ = ⟨x * y, mul_mem hx hy⟩ :=
rfl
@[to_additive]
theorem mul_def (x y : S') : x * y = ⟨x * y, mul_mem x.2 y.2⟩ :=
rfl
/-- A subsemigroup of a semigroup inherits a semigroup structure. -/
@[to_additive
/-- An `AddSubsemigroup` of an `AddSemigroup` inherits an `AddSemigroup` structure. -/]
instance toSemigroup {M : Type*} [Semigroup M] {A : Type*} [SetLike A M] [MulMemClass A M]
(S : A) : Semigroup S := fast_instance%
Subtype.coe_injective.semigroup Subtype.val fun _ _ => rfl
/-- A subsemigroup of a `CommSemigroup` is a `CommSemigroup`. -/
@[to_additive /-- An `AddSubsemigroup` of an `AddCommSemigroup` is an `AddCommSemigroup`. -/]
instance toCommSemigroup {M} [CommSemigroup M] {A : Type*} [SetLike A M] [MulMemClass A M]
(S : A) : CommSemigroup S := fast_instance%
Subtype.coe_injective.commSemigroup Subtype.val fun _ _ => rfl
/-- A submagma of a left cancellative magma inherits left cancellation. -/
@[to_additive
/-- An additive submagma of a left cancellative additive magma inherits left cancellation. -/]
instance isLeftCancelMul [IsLeftCancelMul M] (S : A) : IsLeftCancelMul S :=
Subtype.coe_injective.isLeftCancelMul Subtype.val fun _ _ => rfl
/-- A submagma of a right cancellative magma inherits right cancellation. -/
@[to_additive
/-- An additive submagma of a right cancellative additive magma inherits right cancellation. -/]
instance isRightCancelMul [IsRightCancelMul M] (S : A) : IsRightCancelMul S :=
Subtype.coe_injective.isRightCancelMul Subtype.val fun _ _ => rfl
/-- A submagma of a cancellative magma inherits cancellation. -/
@[to_additive /-- An additive submagma of a cancellative additive magma inherits cancellation. -/]
instance isCancelMul [IsCancelMul M] (S : A) : IsCancelMul S where
/-- The natural semigroup hom from a subsemigroup of semigroup `M` to `M`. -/
@[to_additive /-- The natural semigroup hom from an `AddSubsemigroup` of
`AddSubsemigroup` `M` to `M`. -/]
def subtype : S' →ₙ* M where
toFun := Subtype.val; map_mul' := fun _ _ => rfl
variable {S'} in
@[to_additive (attr := simp)]
lemma subtype_apply (x : S') :
MulMemClass.subtype S' x = x := rfl
@[to_additive]
lemma subtype_injective :
Function.Injective (MulMemClass.subtype S') :=
Subtype.coe_injective
@[to_additive (attr := simp)]
theorem coe_subtype : (MulMemClass.subtype S' : S' → M) = Subtype.val :=
rfl
end MulMemClass |
.lake/packages/mathlib/Mathlib/Algebra/Group/Subsemigroup/Membership.lean | import Mathlib.Algebra.Group.Subsemigroup.Basic
/-!
# Subsemigroups: membership criteria
In this file we prove various facts about membership in a subsemigroup.
The intent is to mimic `GroupTheory/Submonoid/Membership`, but currently this file is mostly a
stub and only provides rudimentary support.
* `mem_iSup_of_directed`, `coe_iSup_of_directed`, `mem_sSup_of_directed_on`,
`coe_sSup_of_directed_on`: the supremum of a directed collection of subsemigroup is their union.
## TODO
* Define the `FreeSemigroup` generated by a set. This might require some rather substantial
additions to low-level API. For example, developing the subtype of nonempty lists, then defining
a product on nonempty lists, powers where the exponent is a positive natural, et cetera.
Another option would be to define the `FreeSemigroup` as the subsemigroup (pushed to be a
semigroup) of the `FreeMonoid` consisting of non-identity elements.
## Tags
subsemigroup
-/
assert_not_exists MonoidWithZero
variable {ι : Sort*} {M : Type*}
section NonAssoc
variable [Mul M]
open Set Function
namespace Subsemigroup
@[to_additive]
theorem mem_biSup_of_directedOn {ι} {p : ι → Prop} {K : ι → Subsemigroup M}
(hK : DirectedOn ((· ≤ ·) on K) {i | p i})
{x : M} : x ∈ (⨆ i, ⨆ (_h : p i), K i) ↔ ∃ i, p i ∧ x ∈ K i := by
refine ⟨?_, fun ⟨i, hi', hi⟩ ↦ ?_⟩
· suffices x ∈ closure (⋃ i, ⋃ (_ : p i), (K i : Set M)) → ∃ i, p i ∧ x ∈ K i by
simpa only [closure_iUnion, closure_eq (K _)] using this
refine fun hx ↦ closure_induction (fun _ ↦ ?_) ?_ hx
· simp
· rintro x y _ _ ⟨i, hip, hi⟩ ⟨j, hjp, hj⟩
rcases hK i hip j hjp with ⟨k, hk, hki, hkj⟩
exact ⟨k, hk, mul_mem (hki hi) (hkj hj)⟩
· apply le_iSup (fun i ↦ ⨆ (_ : p i), K i) i
simp [hi, hi']
-- TODO: this section can be generalized to `[MulMemClass B M] [CompleteLattice B]`
-- such that `complete_lattice.le` coincides with `set_like.le`
@[to_additive]
theorem mem_iSup_of_directed {S : ι → Subsemigroup M} (hS : Directed (· ≤ ·) S) {x : M} :
(x ∈ ⨆ i, S i) ↔ ∃ i, x ∈ S i := by
have : iSup S = ⨆ i : PLift ι, ⨆ (_ : True), S i.down := by simp [iSup_plift_down]
rw [this, mem_biSup_of_directedOn]
· simp
· simp only [setOf_true]
rw [directedOn_onFun_iff, Set.image_univ, ← directedOn_range]
-- `Directed.mono_comp` and much of the Set API requires `Type u` instead of `Sort u`
intro i
simp only [PLift.exists]
intro j
refine (hS i.down j.down).imp ?_
simp
@[to_additive (attr := simp)]
theorem mem_iSup_prop {p : Prop} {S : p → Subsemigroup M} {x : M} :
x ∈ ⨆ (h : p), S h ↔ ∃ (h : p), x ∈ S h := by
by_cases h : p
· simp +contextual [h]
· simpa [h] using id
@[to_additive]
theorem coe_iSup_of_directed {S : ι → Subsemigroup M} (hS : Directed (· ≤ ·) S) :
((⨆ i, S i : Subsemigroup M) : Set M) = ⋃ i, S i :=
Set.ext fun x => by simp [mem_iSup_of_directed hS]
@[to_additive]
theorem mem_sSup_of_directed_on {S : Set (Subsemigroup M)} (hS : DirectedOn (· ≤ ·) S) {x : M} :
x ∈ sSup S ↔ ∃ s ∈ S, x ∈ s := by
simp only [sSup_eq_iSup', mem_iSup_of_directed hS.directed_val, SetCoe.exists, exists_prop]
@[to_additive]
theorem coe_sSup_of_directed_on {S : Set (Subsemigroup M)} (hS : DirectedOn (· ≤ ·) S) :
(↑(sSup S) : Set M) = ⋃ s ∈ S, ↑s :=
Set.ext fun x => by simp [mem_sSup_of_directed_on hS]
@[to_additive]
theorem mem_sup_left {S T : Subsemigroup M} : ∀ {x : M}, x ∈ S → x ∈ S ⊔ T := by
have : S ≤ S ⊔ T := le_sup_left
tauto
@[to_additive]
theorem mem_sup_right {S T : Subsemigroup M} : ∀ {x : M}, x ∈ T → x ∈ S ⊔ T := by
have : T ≤ S ⊔ T := le_sup_right
tauto
@[to_additive]
theorem mul_mem_sup {S T : Subsemigroup M} {x y : M} (hx : x ∈ S) (hy : y ∈ T) : x * y ∈ S ⊔ T :=
mul_mem (mem_sup_left hx) (mem_sup_right hy)
@[to_additive]
theorem mem_iSup_of_mem {S : ι → Subsemigroup M} (i : ι) : ∀ {x : M}, x ∈ S i → x ∈ iSup S := by
have : S i ≤ iSup S := le_iSup _ _
tauto
@[to_additive]
theorem mem_sSup_of_mem {S : Set (Subsemigroup M)} {s : Subsemigroup M} (hs : s ∈ S) :
∀ {x : M}, x ∈ s → x ∈ sSup S := by
have : s ≤ sSup S := le_sSup hs
tauto
/-- An induction principle for elements of `⨆ i, S i`.
If `C` holds all elements of `S i` for all `i`, and is preserved under multiplication,
then it holds for all elements of the supremum of `S`. -/
@[to_additive (attr := elab_as_elim)
/-- An induction principle for elements of `⨆ i, S i`. If `C` holds all
elements of `S i` for all `i`, and is preserved under addition, then it holds for all elements of
the supremum of `S`. -/]
theorem iSup_induction (S : ι → Subsemigroup M) {C : M → Prop} {x₁ : M} (hx₁ : x₁ ∈ ⨆ i, S i)
(mem : ∀ i, ∀ x₂ ∈ S i, C x₂) (mul : ∀ x y, C x → C y → C (x * y)) : C x₁ := by
rw [iSup_eq_closure] at hx₁
refine closure_induction (fun x₂ hx₂ => ?_) (fun x y _ _ ↦ mul x y) hx₁
obtain ⟨i, hi⟩ := Set.mem_iUnion.mp hx₂
exact mem _ _ hi
/-- A dependent version of `Subsemigroup.iSup_induction`. -/
@[to_additive (attr := elab_as_elim)
/-- A dependent version of `AddSubsemigroup.iSup_induction`. -/]
theorem iSup_induction' (S : ι → Subsemigroup M) {C : ∀ x, (x ∈ ⨆ i, S i) → Prop}
(mem : ∀ (i) (x) (hxS : x ∈ S i), C x (mem_iSup_of_mem i ‹_›))
(mul : ∀ x y hx hy, C x hx → C y hy → C (x * y) (mul_mem ‹_› ‹_›)) {x₁ : M}
(hx₁ : x₁ ∈ ⨆ i, S i) : C x₁ hx₁ := by
refine Exists.elim ?_ fun (hx₁' : x₁ ∈ ⨆ i, S i) (hc : C x₁ hx₁') => hc
refine @iSup_induction _ _ _ S (fun x' => ∃ hx'', C x' hx'') _ hx₁
(fun i x₂ hx₂ => ?_) fun x₃ y => ?_
· exact ⟨_, mem _ _ hx₂⟩
· rintro ⟨_, Cx⟩ ⟨_, Cy⟩
exact ⟨_, mul _ _ _ _ Cx Cy⟩
end Subsemigroup
end NonAssoc |
.lake/packages/mathlib/Mathlib/Algebra/Group/TypeTags/Finite.lean | import Mathlib.Algebra.Group.TypeTags.Basic
import Mathlib.Data.Finite.Defs
import Mathlib.Data.Fintype.Card
/-!
# `Finite`, `Infinite` and `Fintype` are preserved by `Additive` and `Multiplicative`.
-/
assert_not_exists MonoidWithZero MulAction
universe u
variable {α : Type u}
instance [Finite α] : Finite (Additive α) :=
Finite.of_equiv α (by rfl)
instance [Finite α] : Finite (Multiplicative α) :=
Finite.of_equiv α (by rfl)
instance [h : Infinite α] : Infinite (Additive α) := h
instance [h : Infinite α] : Infinite (Multiplicative α) := h
instance Additive.fintype : ∀ [Fintype α], Fintype (Additive α) :=
Fintype.ofEquiv α Additive.ofMul
instance Multiplicative.fintype : ∀ [Fintype α], Fintype (Multiplicative α) :=
Fintype.ofEquiv α Multiplicative.ofAdd
@[simp] lemma Fintype.card_multiplicative (α : Type*) [Fintype α] :
card (Multiplicative α) = card α := Finset.card_map _
@[simp] lemma Fintype.card_additive (α : Type*) [Fintype α] : card (Additive α) = card α :=
Finset.card_map _ |
.lake/packages/mathlib/Mathlib/Algebra/Group/TypeTags/Basic.lean | import Mathlib.Algebra.Group.Torsion
import Mathlib.Algebra.Notation.Pi.Basic
import Mathlib.Data.FunLike.Basic
import Mathlib.Logic.Function.Iterate
import Mathlib.Logic.Equiv.Defs
import Mathlib.Tactic.Set
import Mathlib.Util.AssertExists
import Mathlib.Logic.Nontrivial.Basic
/-!
# Type tags that turn additive structures into multiplicative, and vice versa
We define two type tags:
* `Additive α`: turns any multiplicative structure on `α` into the corresponding
additive structure on `Additive α`;
* `Multiplicative α`: turns any additive structure on `α` into the corresponding
multiplicative structure on `Multiplicative α`.
We also define instances `Additive.*` and `Multiplicative.*` that actually transfer the structures.
## See also
This file is similar to `Order.Synonym`.
-/
assert_not_exists MonoidWithZero DenselyOrdered MonoidHom Finite
universe u v
variable {α : Type u} {β : Type v}
/-- If `α` carries some multiplicative structure, then `Additive α` carries the corresponding
additive structure. -/
def Additive (α : Type*) := α
/-- If `α` carries some additive structure, then `Multiplicative α` carries the corresponding
multiplicative structure. -/
def Multiplicative (α : Type*) := α
namespace Additive
/-- Reinterpret `x : α` as an element of `Additive α`. -/
def ofMul : α ≃ Additive α :=
⟨fun x => x, fun x => x, fun _ => rfl, fun _ => rfl⟩
/-- Reinterpret `x : Additive α` as an element of `α`. -/
def toMul : Additive α ≃ α := ofMul.symm
@[simp]
theorem ofMul_symm_eq : (@ofMul α).symm = toMul :=
rfl
@[simp]
theorem toMul_symm_eq : (@toMul α).symm = ofMul :=
rfl
@[ext] lemma ext {a b : Additive α} (hab : a.toMul = b.toMul) : a = b := hab
@[simp]
protected lemma «forall» {p : Additive α → Prop} : (∀ a, p a) ↔ ∀ a, p (ofMul a) := Iff.rfl
@[simp]
protected lemma «exists» {p : Additive α → Prop} : (∃ a, p a) ↔ ∃ a, p (ofMul a) := Iff.rfl
/-- Recursion principle for `Additive`, supported by `cases` and `induction`. -/
@[elab_as_elim, cases_eliminator, induction_eliminator]
def rec {motive : Additive α → Sort*} (ofMul : ∀ a, motive (ofMul a)) : ∀ a, motive a :=
fun a => ofMul (a.toMul)
end Additive
namespace Multiplicative
/-- Reinterpret `x : α` as an element of `Multiplicative α`. -/
def ofAdd : α ≃ Multiplicative α :=
⟨fun x => x, fun x => x, fun _ => rfl, fun _ => rfl⟩
/-- Reinterpret `x : Multiplicative α` as an element of `α`. -/
def toAdd : Multiplicative α ≃ α := ofAdd.symm
@[simp]
theorem ofAdd_symm_eq : (@ofAdd α).symm = toAdd :=
rfl
@[simp]
theorem toAdd_symm_eq : (@toAdd α).symm = ofAdd :=
rfl
@[ext] lemma ext {a b : Multiplicative α} (hab : a.toAdd = b.toAdd) : a = b := hab
@[simp]
protected lemma «forall» {p : Multiplicative α → Prop} : (∀ a, p a) ↔ ∀ a, p (ofAdd a) := Iff.rfl
@[simp]
protected lemma «exists» {p : Multiplicative α → Prop} : (∃ a, p a) ↔ ∃ a, p (ofAdd a) := Iff.rfl
/-- Recursion principle for `Multiplicative`, supported by `cases` and `induction`. -/
@[elab_as_elim, cases_eliminator, induction_eliminator]
def rec {motive : Multiplicative α → Sort*} (ofAdd : ∀ a, motive (ofAdd a)) : ∀ a, motive a :=
fun a => ofAdd (a.toAdd)
end Multiplicative
open Additive (ofMul toMul)
open Multiplicative (ofAdd toAdd)
@[simp]
theorem toAdd_ofAdd (x : α) : (ofAdd x).toAdd = x :=
rfl
@[simp]
theorem ofAdd_toAdd (x : Multiplicative α) : ofAdd x.toAdd = x :=
rfl
@[simp]
theorem toMul_ofMul (x : α) : (ofMul x).toMul = x :=
rfl
@[simp]
theorem ofMul_toMul (x : Additive α) : ofMul x.toMul = x :=
rfl
instance [Subsingleton α] : Subsingleton (Additive α) := toMul.injective.subsingleton
instance [Subsingleton α] : Subsingleton (Multiplicative α) := toAdd.injective.subsingleton
instance [Inhabited α] : Inhabited (Additive α) :=
⟨ofMul default⟩
instance [Inhabited α] : Inhabited (Multiplicative α) :=
⟨ofAdd default⟩
instance [Unique α] : Unique (Additive α) := toMul.unique
instance [Unique α] : Unique (Multiplicative α) := toAdd.unique
instance [h : DecidableEq α] : DecidableEq (Multiplicative α) := h
instance [h : DecidableEq α] : DecidableEq (Additive α) := h
instance Additive.instNontrivial [Nontrivial α] : Nontrivial (Additive α) :=
ofMul.injective.nontrivial
instance Multiplicative.instNontrivial [Nontrivial α] : Nontrivial (Multiplicative α) :=
ofAdd.injective.nontrivial
instance Additive.add [Mul α] : Add (Additive α) where
add x y := ofMul (x.toMul * y.toMul)
instance Multiplicative.mul [Add α] : Mul (Multiplicative α) where
mul x y := ofAdd (x.toAdd + y.toAdd)
@[simp]
theorem ofAdd_add [Add α] (x y : α) : ofAdd (x + y) = ofAdd x * ofAdd y := rfl
@[simp]
theorem toAdd_mul [Add α] (x y : Multiplicative α) : (x * y).toAdd = x.toAdd + y.toAdd := rfl
@[simp]
theorem ofMul_mul [Mul α] (x y : α) : ofMul (x * y) = ofMul x + ofMul y := rfl
@[simp]
theorem toMul_add [Mul α] (x y : Additive α) : (x + y).toMul = x.toMul * y.toMul := rfl
instance Additive.addSemigroup [Semigroup α] : AddSemigroup (Additive α) :=
{ Additive.add with add_assoc := @mul_assoc α _ }
instance Multiplicative.semigroup [AddSemigroup α] : Semigroup (Multiplicative α) :=
{ Multiplicative.mul with mul_assoc := @add_assoc α _ }
instance Additive.addCommSemigroup [CommSemigroup α] : AddCommSemigroup (Additive α) :=
{ Additive.addSemigroup with add_comm := @mul_comm α _ }
instance Multiplicative.commSemigroup [AddCommSemigroup α] : CommSemigroup (Multiplicative α) :=
{ Multiplicative.semigroup with mul_comm := @add_comm α _ }
instance Additive.isLeftCancelAdd [Mul α] [IsLeftCancelMul α] : IsLeftCancelAdd (Additive α) :=
⟨@mul_left_cancel α _ _⟩
instance Multiplicative.isLeftCancelMul [Add α] [IsLeftCancelAdd α] :
IsLeftCancelMul (Multiplicative α) :=
⟨@add_left_cancel α _ _⟩
instance Additive.isRightCancelAdd [Mul α] [IsRightCancelMul α] : IsRightCancelAdd (Additive α) :=
⟨fun _ _ _ ↦ mul_right_cancel (G := α)⟩
instance Multiplicative.isRightCancelMul [Add α] [IsRightCancelAdd α] :
IsRightCancelMul (Multiplicative α) :=
⟨fun _ _ _ ↦ add_right_cancel (G := α)⟩
instance Additive.isCancelAdd [Mul α] [IsCancelMul α] : IsCancelAdd (Additive α) :=
⟨⟩
instance Multiplicative.isCancelMul [Add α] [IsCancelAdd α] : IsCancelMul (Multiplicative α) :=
⟨⟩
instance Additive.addLeftCancelSemigroup [LeftCancelSemigroup α] :
AddLeftCancelSemigroup (Additive α) :=
{ Additive.addSemigroup, Additive.isLeftCancelAdd with }
instance Multiplicative.leftCancelSemigroup [AddLeftCancelSemigroup α] :
LeftCancelSemigroup (Multiplicative α) :=
{ Multiplicative.semigroup, Multiplicative.isLeftCancelMul with }
instance Additive.addRightCancelSemigroup [RightCancelSemigroup α] :
AddRightCancelSemigroup (Additive α) :=
{ Additive.addSemigroup, Additive.isRightCancelAdd with }
instance Multiplicative.rightCancelSemigroup [AddRightCancelSemigroup α] :
RightCancelSemigroup (Multiplicative α) :=
{ Multiplicative.semigroup, Multiplicative.isRightCancelMul with }
instance [One α] : Zero (Additive α) :=
⟨Additive.ofMul 1⟩
@[simp]
theorem ofMul_one [One α] : @Additive.ofMul α 1 = 0 := rfl
@[simp]
theorem ofMul_eq_zero {A : Type*} [One A] {x : A} : Additive.ofMul x = 0 ↔ x = 1 := Iff.rfl
@[simp]
theorem toMul_zero [One α] : (0 : Additive α).toMul = 1 := rfl
@[simp]
lemma toMul_eq_one {α : Type*} [One α] {x : Additive α} :
x.toMul = 1 ↔ x = 0 :=
Iff.rfl
instance [Zero α] : One (Multiplicative α) :=
⟨Multiplicative.ofAdd 0⟩
@[simp]
theorem ofAdd_zero [Zero α] : @Multiplicative.ofAdd α 0 = 1 :=
rfl
@[simp]
theorem ofAdd_eq_one {A : Type*} [Zero A] {x : A} : Multiplicative.ofAdd x = 1 ↔ x = 0 :=
Iff.rfl
@[simp]
theorem toAdd_one [Zero α] : (1 : Multiplicative α).toAdd = 0 :=
rfl
@[simp]
lemma toAdd_eq_zero {α : Type*} [Zero α] {x : Multiplicative α} :
x.toAdd = 0 ↔ x = 1 :=
Iff.rfl
instance Additive.addZeroClass [MulOneClass α] : AddZeroClass (Additive α) where
zero_add := @one_mul α _
add_zero := @mul_one α _
instance Multiplicative.mulOneClass [AddZeroClass α] : MulOneClass (Multiplicative α) where
one_mul := @zero_add α _
mul_one := @add_zero α _
instance Additive.addMonoid [h : Monoid α] : AddMonoid (Additive α) :=
{ Additive.addZeroClass, Additive.addSemigroup with
nsmul := @Monoid.npow α h
nsmul_zero := @Monoid.npow_zero α h
nsmul_succ := @Monoid.npow_succ α h }
instance Multiplicative.monoid [h : AddMonoid α] : Monoid (Multiplicative α) :=
{ Multiplicative.mulOneClass, Multiplicative.semigroup with
npow := @AddMonoid.nsmul α h
npow_zero := @AddMonoid.nsmul_zero α h
npow_succ := @AddMonoid.nsmul_succ α h }
@[simp]
theorem ofMul_pow [Monoid α] (n : ℕ) (a : α) : ofMul (a ^ n) = n • ofMul a :=
rfl
@[simp]
theorem toMul_nsmul [Monoid α] (n : ℕ) (a : Additive α) : (n • a).toMul = a.toMul ^ n :=
rfl
@[simp]
theorem ofAdd_nsmul [AddMonoid α] (n : ℕ) (a : α) : ofAdd (n • a) = ofAdd a ^ n :=
rfl
@[simp]
theorem toAdd_pow [AddMonoid α] (a : Multiplicative α) (n : ℕ) : (a ^ n).toAdd = n • a.toAdd :=
rfl
instance Additive.addLeftCancelMonoid [LeftCancelMonoid α] : AddLeftCancelMonoid (Additive α) :=
{ Additive.addMonoid, Additive.addLeftCancelSemigroup with }
instance Multiplicative.leftCancelMonoid [AddLeftCancelMonoid α] :
LeftCancelMonoid (Multiplicative α) :=
{ Multiplicative.monoid, Multiplicative.leftCancelSemigroup with }
instance Additive.addRightCancelMonoid [RightCancelMonoid α] : AddRightCancelMonoid (Additive α) :=
{ Additive.addMonoid, Additive.addRightCancelSemigroup with }
instance Multiplicative.rightCancelMonoid [AddRightCancelMonoid α] :
RightCancelMonoid (Multiplicative α) :=
{ Multiplicative.monoid, Multiplicative.rightCancelSemigroup with }
instance Additive.addCommMonoid [CommMonoid α] : AddCommMonoid (Additive α) :=
{ Additive.addMonoid, Additive.addCommSemigroup with }
instance Multiplicative.commMonoid [AddCommMonoid α] : CommMonoid (Multiplicative α) :=
{ Multiplicative.monoid, Multiplicative.commSemigroup with }
instance Additive.instAddCancelCommMonoid [CancelCommMonoid α] :
AddCancelCommMonoid (Additive α) where
instance Multiplicative.instCancelCommMonoid [AddCancelCommMonoid α] :
CancelCommMonoid (Multiplicative α) where
instance Additive.neg [Inv α] : Neg (Additive α) :=
⟨fun x => ofAdd x.toMul⁻¹⟩
@[simp]
theorem ofMul_inv [Inv α] (x : α) : ofMul x⁻¹ = -ofMul x :=
rfl
@[simp]
theorem toMul_neg [Inv α] (x : Additive α) : (-x).toMul = x.toMul⁻¹ :=
rfl
instance Multiplicative.inv [Neg α] : Inv (Multiplicative α) :=
⟨fun x => ofMul (-x.toAdd)⟩
@[simp]
theorem ofAdd_neg [Neg α] (x : α) : ofAdd (-x) = (ofAdd x)⁻¹ :=
rfl
@[simp]
theorem toAdd_inv [Neg α] (x : Multiplicative α) : x⁻¹.toAdd = -x.toAdd :=
rfl
instance Additive.sub [Div α] : Sub (Additive α) where
sub x y := ofMul (x.toMul / y.toMul)
instance Multiplicative.div [Sub α] : Div (Multiplicative α) where
div x y := ofAdd (x.toAdd - y.toAdd)
@[simp]
theorem ofAdd_sub [Sub α] (x y : α) : ofAdd (x - y) = ofAdd x / ofAdd y :=
rfl
@[simp]
theorem toAdd_div [Sub α] (x y : Multiplicative α) : (x / y).toAdd = x.toAdd - y.toAdd :=
rfl
@[simp]
theorem ofMul_div [Div α] (x y : α) : ofMul (x / y) = ofMul x - ofMul y :=
rfl
@[simp]
theorem toMul_sub [Div α] (x y : Additive α) : (x - y).toMul = x.toMul / y.toMul :=
rfl
instance Additive.involutiveNeg [InvolutiveInv α] : InvolutiveNeg (Additive α) :=
{ Additive.neg with neg_neg := @inv_inv α _ }
instance Multiplicative.involutiveInv [InvolutiveNeg α] : InvolutiveInv (Multiplicative α) :=
{ Multiplicative.inv with inv_inv := @neg_neg α _ }
instance Additive.subNegMonoid [DivInvMonoid α] : SubNegMonoid (Additive α) :=
{ Additive.neg, Additive.sub, Additive.addMonoid with
sub_eq_add_neg := @div_eq_mul_inv α _
zsmul := @DivInvMonoid.zpow α _
zsmul_zero' := @DivInvMonoid.zpow_zero' α _
zsmul_succ' := @DivInvMonoid.zpow_succ' α _
zsmul_neg' := @DivInvMonoid.zpow_neg' α _ }
instance Multiplicative.divInvMonoid [SubNegMonoid α] : DivInvMonoid (Multiplicative α) :=
{ Multiplicative.inv, Multiplicative.div, Multiplicative.monoid with
div_eq_mul_inv := @sub_eq_add_neg α _
zpow := @SubNegMonoid.zsmul α _
zpow_zero' := @SubNegMonoid.zsmul_zero' α _
zpow_succ' := @SubNegMonoid.zsmul_succ' α _
zpow_neg' := @SubNegMonoid.zsmul_neg' α _ }
@[simp]
theorem ofMul_zpow [DivInvMonoid α] (z : ℤ) (a : α) : ofMul (a ^ z) = z • ofMul a :=
rfl
@[simp]
theorem toMul_zsmul [DivInvMonoid α] (z : ℤ) (a : Additive α) : (z • a).toMul = a.toMul ^ z :=
rfl
@[simp]
theorem ofAdd_zsmul [SubNegMonoid α] (z : ℤ) (a : α) : ofAdd (z • a) = ofAdd a ^ z :=
rfl
@[simp]
theorem toAdd_zpow [SubNegMonoid α] (a : Multiplicative α) (z : ℤ) : (a ^ z).toAdd = z • a.toAdd :=
rfl
instance Additive.subtractionMonoid [DivisionMonoid α] : SubtractionMonoid (Additive α) :=
{ Additive.subNegMonoid, Additive.involutiveNeg with
neg_add_rev := @mul_inv_rev α _
neg_eq_of_add := @inv_eq_of_mul_eq_one_right α _ }
instance Multiplicative.divisionMonoid [SubtractionMonoid α] : DivisionMonoid (Multiplicative α) :=
{ Multiplicative.divInvMonoid, Multiplicative.involutiveInv with
mul_inv_rev := @neg_add_rev α _
inv_eq_of_mul := @neg_eq_of_add_eq_zero_right α _ }
instance Additive.subtractionCommMonoid [DivisionCommMonoid α] :
SubtractionCommMonoid (Additive α) :=
{ Additive.subtractionMonoid, Additive.addCommSemigroup with }
instance Multiplicative.divisionCommMonoid [SubtractionCommMonoid α] :
DivisionCommMonoid (Multiplicative α) :=
{ Multiplicative.divisionMonoid, Multiplicative.commSemigroup with }
instance Additive.addGroup [Group α] : AddGroup (Additive α) :=
{ Additive.subNegMonoid with neg_add_cancel := @inv_mul_cancel α _ }
instance Multiplicative.group [AddGroup α] : Group (Multiplicative α) :=
{ Multiplicative.divInvMonoid with inv_mul_cancel := @neg_add_cancel α _ }
instance Additive.addCommGroup [CommGroup α] : AddCommGroup (Additive α) :=
{ Additive.addGroup, Additive.addCommMonoid with }
instance Multiplicative.commGroup [AddCommGroup α] : CommGroup (Multiplicative α) :=
{ Multiplicative.group, Multiplicative.commMonoid with }
instance [Monoid α] [IsMulTorsionFree α] : IsAddTorsionFree (Additive α) where
nsmul_right_injective _ := pow_left_injective (M := α)
instance [AddMonoid α] [IsAddTorsionFree α] : IsMulTorsionFree (Multiplicative α) where
pow_left_injective _ := nsmul_right_injective (M := α)
/-- If `α` has some multiplicative structure and coerces to a function,
then `Additive α` should also coerce to the same function.
This allows `Additive` to be used on bundled function types with a multiplicative structure, which
is often used for composition, without affecting the behavior of the function itself.
-/
instance Additive.coeToFun {α : Type*} {β : α → Sort*} [CoeFun α β] :
CoeFun (Additive α) fun a => β a.toMul :=
⟨fun a => CoeFun.coe a.toMul⟩
/-- If `α` has some additive structure and coerces to a function,
then `Multiplicative α` should also coerce to the same function.
This allows `Multiplicative` to be used on bundled function types with an additive structure, which
is often used for composition, without affecting the behavior of the function itself.
-/
instance Multiplicative.coeToFun {α : Type*} {β : α → Sort*} [CoeFun α β] :
CoeFun (Multiplicative α) fun a => β a.toAdd :=
⟨fun a => CoeFun.coe a.toAdd⟩
lemma Pi.mulSingle_multiplicativeOfAdd_eq {ι : Type*} [DecidableEq ι] {M : ι → Type*}
[(i : ι) → AddMonoid (M i)] (i : ι) (a : M i) (j : ι) :
Pi.mulSingle (M := fun i ↦ Multiplicative (M i)) i (.ofAdd a) j = .ofAdd (Pi.single i a j) := by
rcases eq_or_ne j i with rfl | h
· simp only [mulSingle_eq_same, single_eq_same]
· simp only [mulSingle, ne_eq, h, not_false_eq_true, Function.update_of_ne, one_apply, single,
zero_apply, ofAdd_zero]
lemma Pi.single_additiveOfMul_eq {ι : Type*} [DecidableEq ι] {M : ι → Type*}
[(i : ι) → Monoid (M i)] (i : ι) (a : M i) (j : ι) :
Pi.single (M := fun i ↦ Additive (M i)) i (.ofMul a) j = .ofMul (Pi.mulSingle i a j) := by
rcases eq_or_ne j i with rfl | h
· simp only [mulSingle_eq_same, single_eq_same]
· simp only [single, ne_eq, h, not_false_eq_true, Function.update_of_ne, zero_apply, mulSingle,
one_apply, ofMul_one] |
.lake/packages/mathlib/Mathlib/Algebra/Group/TypeTags/Hom.lean | import Mathlib.Algebra.Group.Equiv.Defs
import Mathlib.Algebra.Group.Hom.Basic
import Mathlib.Algebra.Group.TypeTags.Basic
/-!
# Transport algebra morphisms between additive and multiplicative types.
-/
open Additive (ofMul toMul)
open Multiplicative (ofAdd toAdd)
variable {M N α β : Type*}
/-- Reinterpret `α →+ β` as `Multiplicative α →* Multiplicative β`. -/
@[simps]
def AddMonoidHom.toMultiplicative [AddZeroClass α] [AddZeroClass β] :
(α →+ β) ≃ (Multiplicative α →* Multiplicative β) where
toFun f := {
toFun := fun a => ofAdd (f a.toAdd)
map_mul' := f.map_add
map_one' := f.map_zero
}
invFun f := {
toFun := fun a => f (ofAdd a) |>.toAdd
map_add' := f.map_mul
map_zero' := f.map_one
}
@[simp, norm_cast]
lemma AddMonoidHom.coe_toMultiplicative [AddZeroClass α] [AddZeroClass β] (f : α →+ β) :
⇑(toMultiplicative f) = ofAdd ∘ f ∘ toAdd := rfl
@[simp]
lemma AddMonoidHom.toMultiplicative_id [AddZeroClass α] : (id α).toMultiplicative = .id _ := rfl
/-- Reinterpret `α →* β` as `Additive α →+ Additive β`. -/
@[simps]
def MonoidHom.toAdditive [MulOneClass α] [MulOneClass β] :
(α →* β) ≃ (Additive α →+ Additive β) where
toFun f := {
toFun := fun a => ofMul (f a.toMul)
map_add' := f.map_mul
map_zero' := f.map_one
}
invFun f := {
toFun := fun a => (f (ofMul a)).toMul
map_mul' := f.map_add
map_one' := f.map_zero
}
@[simp, norm_cast]
lemma MonoidHom.coe_toAdditive [MulOneClass α] [MulOneClass β] (f : α →* β) :
⇑(toAdditive f) = ofMul ∘ f ∘ toMul := rfl
@[deprecated (since := "2025-11-07")]
alias MonoidHom.coe_toMultiplicative := MonoidHom.coe_toAdditive
@[simp] lemma MonoidHom.toAdditive_id [MulOneClass α] : (id α).toAdditive = .id _ := rfl
/-- Reinterpret `Additive α →+ β` as `α →* Multiplicative β`. -/
@[simps]
def AddMonoidHom.toMultiplicativeRight [MulOneClass α] [AddZeroClass β] :
(Additive α →+ β) ≃ (α →* Multiplicative β) where
toFun f := {
toFun := fun a => ofAdd (f (ofMul a))
map_mul' := f.map_add
map_one' := f.map_zero
}
invFun f := {
toFun := fun a => (f a.toMul).toAdd
map_add' := f.map_mul
map_zero' := f.map_one
}
@[deprecated (since := "2025-09-19")]
alias AddMonoidHom.toMultiplicative' := AddMonoidHom.toMultiplicativeRight
@[simp, norm_cast]
lemma AddMonoidHom.coe_toMultiplicativeRight [MulOneClass α] [AddZeroClass β]
(f : Additive α →+ β) : ⇑(toMultiplicativeRight f) = ofAdd ∘ f ∘ ofMul := rfl
@[deprecated (since := "2025-09-19")]
alias AddMonoidHom.coe_toMultiplicative' := AddMonoidHom.coe_toMultiplicativeRight
/-- Reinterpret `α →* Multiplicative β` as `Additive α →+ β`. -/
@[simps!]
def MonoidHom.toAdditiveLeft [MulOneClass α] [AddZeroClass β] :
(α →* Multiplicative β) ≃ (Additive α →+ β) :=
AddMonoidHom.toMultiplicativeRight.symm
@[deprecated (since := "2025-09-19")] alias MonoidHom.toAdditive' := MonoidHom.toAdditiveLeft
@[simp, norm_cast]
lemma MonoidHom.coe_toAdditiveLeft [MulOneClass α] [AddZeroClass β] (f : α →* Multiplicative β) :
⇑(toAdditiveLeft f) = toAdd ∘ f ∘ toMul := rfl
@[deprecated (since := "2025-09-19")]
alias MonoidHom.coe_toAdditive' := MonoidHom.coe_toAdditiveLeft
/-- Reinterpret `α →+ Additive β` as `Multiplicative α →* β`. -/
@[simps]
def AddMonoidHom.toMultiplicativeLeft [AddZeroClass α] [MulOneClass β] :
(α →+ Additive β) ≃ (Multiplicative α →* β) where
toFun f := {
toFun := fun a => (f a.toAdd).toMul
map_mul' := f.map_add
map_one' := f.map_zero
}
invFun f := {
toFun := fun a => ofMul (f (ofAdd a))
map_add' := f.map_mul
map_zero' := f.map_one
}
@[deprecated (since := "2025-09-19")]
alias AddMonoidHom.toMultiplicative'' := AddMonoidHom.toMultiplicativeLeft
@[simp, norm_cast]
lemma AddMonoidHom.coe_toMultiplicativeLeft [AddZeroClass α] [MulOneClass β] (f : α →+ Additive β) :
⇑(toMultiplicativeLeft f) = toMul ∘ f ∘ toAdd := rfl
@[deprecated (since := "2025-09-19")]
alias AddMonoidHom.coe_toMultiplicative'' := AddMonoidHom.coe_toMultiplicativeLeft
/-- Reinterpret `Multiplicative α →* β` as `α →+ Additive β`. -/
@[simps!]
def MonoidHom.toAdditiveRight [AddZeroClass α] [MulOneClass β] :
(Multiplicative α →* β) ≃ (α →+ Additive β) :=
AddMonoidHom.toMultiplicativeLeft.symm
@[deprecated (since := "2025-09-19")] alias MonoidHom.toAdditive'' := MonoidHom.toAdditiveRight
@[simp, norm_cast]
lemma MonoidHom.coe_toAdditiveRight [AddZeroClass α] [MulOneClass β] (f : Multiplicative α →* β) :
⇑(toAdditiveRight f) = ofMul ∘ f ∘ ofAdd := rfl
@[deprecated (since := "2025-09-19")]
alias MonoidHom.coe_toAdditive'' := MonoidHom.coe_toAdditiveRight
/-- This ext lemma moves the type tag to the codomain, since most ext lemmas act on the domain.
WARNING: This has the potential to send `ext` into a loop if someone locally adds the inverse ext
lemma proving equality in `α →+ Additive β` from equality in `Multiplicative α →* β`. -/
@[ext]
lemma Multiplicative.monoidHom_ext [AddZeroClass α] [MulOneClass β]
(f g : Multiplicative α →* β) (h : f.toAdditiveRight = g.toAdditiveRight) : f = g :=
MonoidHom.toAdditiveRight.injective h
/-- This ext lemma moves the type tag to the codomain, since most ext lemmas act on the domain.
WARNING: This has the potential to send `ext` into a loop if someone locally adds the inverse ext
lemma proving equality in `α →* Multiplicative β` from equality in `Additive α →+ β`. -/
@[ext]
lemma Additive.addMonoidHom_ext [MulOneClass α] [AddZeroClass β]
(f g : Additive α →+ β) (h : f.toMultiplicativeRight = g.toMultiplicativeRight) : f = g :=
AddMonoidHom.toMultiplicativeRight.injective h
section AddCommMonoid
variable [AddMonoid M] [AddCommMonoid N]
@[simp]
lemma AddMonoidHom.toMultiplicative_add (f g : M →+ N) :
(f + g).toMultiplicative = f.toMultiplicative * g.toMultiplicative := rfl
end AddCommMonoid
/-- `AddMonoidHom.toMultiplicativeLeft` as an `AddEquiv`. -/
def AddMonoidHom.toMultiplicativeLeftAddEquiv [AddMonoid M] [CommMonoid N] :
(M →+ Additive N) ≃+ Additive (Multiplicative M →* N) where
toEquiv := AddMonoidHom.toMultiplicativeLeft.trans Additive.ofMul
map_add' _ _ := rfl
/-- `AddMonoidHom.toMultiplicativeRight` as an `AddEquiv`. -/
def AddMonoidHom.toMultiplicativeRightAddEquiv [Monoid M] [AddCommMonoid N] :
(Additive M →+ N) ≃+ Additive (M →* Multiplicative N) where
toEquiv := AddMonoidHom.toMultiplicativeRight.trans Additive.ofMul
map_add' _ _ := rfl
/-- `MonoidHom.toAdditiveLeft` as a `MulEquiv`. -/
def MonoidHom.toAdditiveLeftMulEquiv [Monoid M] [AddCommMonoid N] :
(M →* Multiplicative N) ≃* Multiplicative (Additive M →+ N) where
toEquiv := MonoidHom.toAdditiveLeft.trans Multiplicative.ofAdd
map_mul' _ _ := rfl
/-- `MonoidHom.toAdditiveRight` as a `MulEquiv`. -/
def MonoidHom.toAdditiveRightMulEquiv [AddMonoid M] [CommMonoid N] :
(Multiplicative M →* N) ≃* Multiplicative (M →+ Additive N) where
toEquiv := MonoidHom.toAdditiveRight.trans Multiplicative.ofAdd
map_mul' _ _ := rfl |
.lake/packages/mathlib/Mathlib/Algebra/MvPolynomial/SchwartzZippel.lean | import Mathlib.Algebra.BigOperators.Field
import Mathlib.Algebra.MvPolynomial.Equiv
import Mathlib.Algebra.MvPolynomial.Variables
import Mathlib.Algebra.Order.GroupWithZero.Finset
import Mathlib.Algebra.Order.Ring.Finset
import Mathlib.Algebra.Polynomial.Roots
import Mathlib.Data.Fin.Tuple.Finset
import Mathlib.Tactic.Positivity.Finset
import Mathlib.Tactic.GCongr
/-!
# The Schwartz-Zippel lemma
This file contains a proof of the
[Schwartz-Zippel](https://en.wikipedia.org/wiki/Schwartz%E2%80%93Zippel_lemma) lemma.
This lemma tells us that the probability that a nonzero multivariable polynomial over an integral
domain evaluates to zero at a random point is bounded by the degree of the polynomial over the size
of the field, or more generally, that a nonzero multivariable polynomial over any integral domain
has a low probability of being zero when evaluated at points drawn at random from some finite subset
of the field. This lemma is useful as a probabilistic polynomial identity test.
## Main results
- `MvPolynomial.schwartz_zippel_sup_sum`:
Sharper version of Schwartz-Zippel for a dependent product of sets `S i`, with the RHS being
the supremum of `∑ i, degᵢ s / #(S i)` ranging over monomials `s` of the polynomial.
- `MvPolynomial.schwartz_zippel_sum_degreeOf`:
Schwartz-Zippel for a dependent product of sets `S i`,
with the RHS being the sum of `degᵢ p / #(S i)`.
- `MvPolynomial.schwartz_zippel_totalDegree`:
Nondependent version of `schwartz_zippel_sup_sum`, with the RHS being `p.totalDegree / #S`.
## TODO
* Generalize to polynomials over arbitrary variable types
* Prove the stronger statement that one can replace the degrees of `p` in the RHS by the degrees of
the maximal monomial of `p` in some lexicographic order.
* Write a tactic to apply this lemma to a given polynomial
## References
* [demillo_lipton_1978]
* [schwartz_1980]
* [zippel_1979]
-/
open Fin Finset Fintype
local notation:70 s:70 " ^^ " n:71 => piFinset fun i : Fin n ↦ s i
namespace MvPolynomial
variable {R : Type*} [CommRing R] [IsDomain R] [DecidableEq R]
-- A user should be able to provide `hp` as a named argument
-- regardless of whether one has used pattern-matching or induction to prove the lemma.
set_option linter.unusedVariables false in
/-- The **Schwartz-Zippel lemma**
For a nonzero multivariable polynomial `p` over an integral domain, the probability that `p`
evaluates to zero at points drawn at random from a product of finite subsets `S i` of the integral
domain is bounded by the supremum of `∑ i, degᵢ s / #(S i)` ranging over monomials `s` of `p`. -/
lemma schwartz_zippel_sup_sum :
∀ {n} {p : MvPolynomial (Fin n) R} (hp : p ≠ 0) (S : Fin n → Finset R),
#{x ∈ S ^^ n | eval x p = 0} / ∏ i, (#(S i) : ℚ≥0) ≤
p.support.sup fun s ↦ ∑ i, (s i / #(S i) : ℚ≥0)
| 0, p, hp, S => by
-- Because `p` is a polynomial over zero variables, it is constant.
rw [p.eq_C_of_isEmpty] at *
simp [C_ne_zero.mp hp]
-- Now, assume that the theorem holds for all polynomials in `n` variables.
| n + 1, p, hp, S => by
-- We can consider `p` to be a polynomial over multivariable polynomials in one fewer variables.
set p' : Polynomial (MvPolynomial (Fin n) R) := finSuccEquiv R n p with hp'
-- Since `p` is not identically zero, there is some `k` such that `pₖ` is not identically zero.
-- WLOG `k` is the largest such.
set k := p'.natDegree with hk
set pₖ := p'.leadingCoeff with hpₖ
have hp'₀ : p' ≠ 0 := EmbeddingLike.map_ne_zero_iff.2 hp
have hpₖ₀ : pₖ ≠ 0 := by simpa [pₖ, k]
calc
-- We split the set of possible zeros into a union of two cases.
#{x ∈ S ^^ (n + 1) | eval x p = 0} / ∏ i, (#(S i) : ℚ≥0)
-- In the first case, `pₖ` evaluates to `0`.
= #{x ∈ S ^^ (n + 1) | eval x p = 0 ∧ eval (tail x) pₖ = 0} / ∏ i, (#(S i) : ℚ≥0)
-- In the second case, `pₖ` does not evaluate to `0`.
+ #{x ∈ S ^^ (n + 1) | eval x p = 0 ∧ eval (tail x) pₖ ≠ 0} / ∏ i, (#(S i) : ℚ≥0) := by
rw [← add_div, ← Nat.cast_add, ← card_union_add_card_inter, filter_union_right,
← filter_and]
simp [← and_or_left, em, and_and_and_comm]
_ ≤ (pₖ.support.sup fun s ↦ ∑ i, (s i / #(S i.succ) : ℚ≥0)) + p.degreeOf 0 / #(S 0) := ?_
_ ≤ p.support.sup fun s ↦ ∑ i, (s i / #(S i) : ℚ≥0) := ?_
· gcongr ?_ + ?_
· -- We bound the size of the first set by induction
calc
#{x ∈ S ^^ (n + 1) | eval x p = 0 ∧ eval (tail x) pₖ = 0} / ∏ i, (#(S i) : ℚ≥0)
≤ #{x ∈ S ^^ (n + 1) | eval (tail x) pₖ = 0} / ∏ i, (#(S i) : ℚ≥0) := by
gcongr with x; exact And.right
_ = #(S 0) * #{xₜ ∈ tail S ^^ n | eval xₜ pₖ = 0}
/ (#(S 0) * (∏ i, #(S (.succ i)) : ℚ≥0)) := by
rw [card_consEquiv_filter_piFinset S fun x ↦ eval x pₖ = 0, prod_univ_succ, tail_def]
norm_cast
_ ≤ #{xₜ ∈ tail S ^^ n | eval xₜ pₖ = 0} / ∏ i, (#(S (.succ i)) : ℚ≥0) :=
mul_div_mul_left_le (by positivity)
_ ≤ (pₖ.support.sup fun s ↦ ∑ i, (s i / #(S (.succ i)) : ℚ≥0)) :=
schwartz_zippel_sup_sum hpₖ₀ _
· -- We bound the second set by noting that if `x` is in it, then `x₀` is the root of
-- the univariate polynomial`pₓ` obtained by evaluating each (multivariate polynomial)
-- coefficient at `xₜ`. Since `pₓ` has degree `k`, there are at most `k` such `x₀` for
-- each `xₜ`, which gives the result.
calc
#{x ∈ S ^^ (n + 1) | eval x p = 0 ∧ eval (tail x) pₖ ≠ 0} / ∏ i, (#(S i) : ℚ≥0)
≤ ↑(p.degreeOf 0 * ∏ i, #(S (.succ i))) / ∏ i, (#(S i) : ℚ≥0) := ?_
_ = p.degreeOf 0 * (∏ i, #(S (.succ i))) / (#(S 0) * ∏ i, #(S (.succ i))) := by
norm_cast; rw [prod_univ_succ]
_ ≤ (p.degreeOf 0 / #(S 0) : ℚ≥0) := mul_div_mul_right_le (by positivity)
gcongr
calc
#{x ∈ S ^^ (n + 1) | eval x p = 0 ∧ eval (tail x) pₖ ≠ 0}
= #{x ∈ S ^^ (n + 1) | eval (tail x) pₖ ≠ 0 ∧ eval x p = 0} := by simp_rw [and_comm]
_ = #({xₜ ∈ tail S ^^ n | eval xₜ pₖ ≠ 0}.biUnion fun xₜ ↦ image (fun x₀ ↦ (x₀, xₜ))
{x₀ ∈ S 0 | eval (cons x₀ xₜ) p = 0}) := by
rw [← filter_filter, filter_piFinset_eq_map_consEquiv S (fun r ↦ eval r pₖ ≠ 0),
filter_map, card_map, product_eq_biUnion_right, filter_biUnion]
simp [Function.comp_def, filter_image]
rfl
_ ≤ ∑ xₜ ∈ tail S ^^ n with eval xₜ pₖ ≠ 0,
#(image (fun x₀ ↦ (x₀, xₜ)) {x₀ ∈ S 0 | eval (cons x₀ xₜ) p = 0}) :=
card_biUnion_le
_ ≤ ∑ xₜ ∈ tail S ^^ n with eval xₜ pₖ ≠ 0, #{x₀ ∈ S 0 | eval (cons x₀ xₜ) p = 0} := by
gcongr; exact card_image_le
_ ≤ ∑ xₜ ∈ tail S ^^ n with eval xₜ pₖ ≠ 0, p.degreeOf 0 := ?_
_ ≤ ∑ _xₜ ∈ tail S ^^ n, p.degreeOf 0 := by gcongr; exact filter_subset ..
_ = p.degreeOf 0 * ∏ i, #(S (.succ i)) := by simp [mul_comm, tail]
gcongr with xₜ hxₜ
set pₓ := p'.map (eval xₜ) with hpₓ
have hpₓdeg : pₓ.natDegree = k := by
rw [hpₓ, hk, Polynomial.natDegree_map_of_leadingCoeff_ne_zero _ (mem_filter.1 hxₜ).2]
have hpₓ₀ : pₓ ≠ 0 := fun h ↦ (mem_filter.1 hxₜ).2 <| by
rw [hpₖ, Polynomial.leadingCoeff, ← hk, ← hpₓdeg, h, Polynomial.natDegree_zero,
← Polynomial.coeff_map, ← hpₓ, h, Polynomial.coeff_zero]
calc
#{x₀ ∈ S 0 | eval (cons x₀ xₜ) p = 0} ≤ #pₓ.roots.toFinset := by
gcongr
simp +contextual [subset_iff, eval_eq_eval_mv_eval', pₓ, hpₓ₀, p']
_ ≤ Multiset.card pₓ.roots := pₓ.roots.toFinset_card_le
_ ≤ pₓ.natDegree := pₓ.card_roots'
_ = k := hpₓdeg
_ ≤ p.degreeOf 0 := by
have :
(ofLex (AddMonoidAlgebra.supDegree toLex p'.leadingCoeff)).cons k ∈ p.support := by
rwa [← support_coeff_finSuccEquiv, mem_support_iff, ← hp', hk,
← Polynomial.leadingCoeff, ← hpₖ, ← leadingCoeff_toLex,
AddMonoidAlgebra.leadingCoeff_ne_zero toLex.injective]
simpa using monomial_le_degreeOf 0 this
· rw [Finset.sup_add (support_nonempty.mpr hpₖ₀)]
apply Finset.sup_le
rintro i hi
refine le_sup_of_le (mem_support_coeff_finSuccEquiv.mp hi) ?_
rw [Fin.sum_univ_succ, add_comm]
dsimp
gcongr
simp [natDegree_finSuccEquiv, p']
/-- The **Schwartz-Zippel lemma**
For a nonzero multivariable polynomial `p` over an integral domain, the probability that `p`
evaluates to zero at points drawn at random from a product of finite subsets `S i` of the integral
domain is bounded by the sum of `degᵢ p / #(S i)`. -/
lemma schwartz_zippel_sum_degreeOf {n} {p : MvPolynomial (Fin n) R} (hp : p ≠ 0)
(S : Fin n → Finset R) :
#{x ∈ S ^^ n | eval x p = 0} / ∏ i, (#(S i) : ℚ≥0) ≤ ∑ i, (p.degreeOf i / #(S i) : ℚ≥0) := by
calc
_ ≤ p.support.sup fun s ↦ ∑ i, (s i / #(S i) : ℚ≥0) := schwartz_zippel_sup_sum hp S
_ ≤ ∑ i, (p.degreeOf i / #(S i) : ℚ≥0) := Finset.sup_le fun s hs ↦ by
gcongr with i; exact monomial_le_degreeOf i hs
/-- The **Schwartz-Zippel lemma**
For a nonzero multivariable polynomial `p` over an integral domain, the probability that `p`
evaluates to zero at points drawn at random from some finite subset `S` of the integral domain is
bounded by the degree of `p` over `#S`. This version presents this lemma in terms of `Finset`. -/
lemma schwartz_zippel_totalDegree {n} {p : MvPolynomial (Fin n) R} (hp : p ≠ 0) (S : Finset R) :
#{f ∈ piFinset fun _ ↦ S | eval f p = 0} / (#S ^ n : ℚ≥0) ≤ p.totalDegree / #S :=
calc
_ = #{f ∈ piFinset fun _ ↦ S | eval f p = 0} / (∏ i : Fin n, #S : ℚ≥0) := by simp
_ ≤ p.support.sup fun s ↦ ∑ i, (s i / #S : ℚ≥0) := schwartz_zippel_sup_sum hp _
_ = p.totalDegree / #S := by
obtain rfl | hs := S.eq_empty_or_nonempty
· simp
simp only [← _root_.bot_eq_zero, sup_bot]
simp_rw [totalDegree, Nat.cast_finsetSup]
rw [sup_div₀ (ha := show 0 < (#S : ℚ≥0) by positivity)]
simp [← sum_div, Finsupp.sum_fintype]
end MvPolynomial |
.lake/packages/mathlib/Mathlib/Algebra/MvPolynomial/PDeriv.lean | import Mathlib.Algebra.MvPolynomial.Derivation
import Mathlib.Algebra.MvPolynomial.Variables
/-!
# Partial derivatives of polynomials
This file defines the notion of the formal *partial derivative* of a polynomial,
the derivative with respect to a single variable.
This derivative is not connected to the notion of derivative from analysis.
It is based purely on the polynomial exponents and coefficients.
## Main declarations
* `MvPolynomial.pderiv i p` : the partial derivative of `p` with respect to `i`, as a bundled
derivation of `MvPolynomial σ R`.
## Notation
As in other polynomial files, we typically use the notation:
+ `σ : Type*` (indexing the variables)
+ `R : Type*` `[CommRing R]` (the coefficients)
+ `s : σ →₀ ℕ`, a function from `σ` to `ℕ` which is zero away from a finite set.
This will give rise to a monomial in `MvPolynomial σ R` which mathematicians might call `X^s`
+ `a : R`
+ `i : σ`, with corresponding monomial `X i`, often denoted `X_i` by mathematicians
+ `p : MvPolynomial σ R`
-/
noncomputable section
universe u v
namespace MvPolynomial
open Set Function Finsupp
variable {R : Type u} {σ : Type v} {a a' a₁ a₂ : R} {s : σ →₀ ℕ}
section PDeriv
variable [CommSemiring R]
/-- `pderiv i p` is the partial derivative of `p` with respect to `i` -/
def pderiv (i : σ) : Derivation R (MvPolynomial σ R) (MvPolynomial σ R) :=
letI := Classical.decEq σ
mkDerivation R <| Pi.single i 1
theorem pderiv_def [DecidableEq σ] (i : σ) : pderiv i = mkDerivation R (Pi.single i 1) := by
unfold pderiv; congr!
@[simp]
theorem pderiv_monomial {i : σ} :
pderiv i (monomial s a) = monomial (s - single i 1) (a * s i) := by
classical
simp only [pderiv_def, mkDerivation_monomial, Finsupp.smul_sum, smul_eq_mul, ← smul_mul_assoc,
← (monomial _).map_smul]
refine (Finset.sum_eq_single i (fun j _ hne => ?_) fun hi => ?_).trans ?_
· simp [Pi.single_eq_of_ne hne]
· rw [Finsupp.notMem_support_iff] at hi; simp [hi]
· simp
lemma X_mul_pderiv_monomial {i : σ} {m : σ →₀ ℕ} {r : R} :
X i * pderiv i (monomial m r) = m i • monomial m r := by
rw [pderiv_monomial, X, monomial_mul, smul_monomial]
by_cases h : m i = 0
· simp_rw [h, Nat.cast_zero, mul_zero, zero_smul, monomial_zero]
rw [one_mul, mul_comm, nsmul_eq_mul, add_comm, sub_add_single_one_cancel h]
theorem pderiv_C {i : σ} : pderiv i (C a) = 0 :=
derivation_C _ _
theorem pderiv_one {i : σ} : pderiv i (1 : MvPolynomial σ R) = 0 := pderiv_C
@[simp]
theorem pderiv_X [DecidableEq σ] (i j : σ) :
pderiv i (X j : MvPolynomial σ R) = Pi.single (M := fun _ => _) i 1 j := by
rw [pderiv_def, mkDerivation_X]
@[simp]
theorem pderiv_X_self (i : σ) : pderiv i (X i : MvPolynomial σ R) = 1 := by classical simp
@[simp]
theorem pderiv_X_of_ne {i j : σ} (h : j ≠ i) : pderiv i (X j : MvPolynomial σ R) = 0 := by
classical simp [h]
theorem pderiv_eq_zero_of_notMem_vars {i : σ} {f : MvPolynomial σ R} (h : i ∉ f.vars) :
pderiv i f = 0 :=
derivation_eq_zero_of_forall_mem_vars fun _ hj => pderiv_X_of_ne <| ne_of_mem_of_not_mem hj h
@[deprecated (since := "2025-05-23")]
alias pderiv_eq_zero_of_not_mem_vars := pderiv_eq_zero_of_notMem_vars
theorem pderiv_monomial_single {i : σ} {n : ℕ} : pderiv i (monomial (single i n) a) =
monomial (single i (n - 1)) (a * n) := by simp
theorem pderiv_mul {i : σ} {f g : MvPolynomial σ R} :
pderiv i (f * g) = pderiv i f * g + f * pderiv i g := by
simp only [(pderiv i).leibniz f g, smul_eq_mul, mul_comm, add_comm]
theorem pderiv_pow {i : σ} {f : MvPolynomial σ R} {n : ℕ} :
pderiv i (f ^ n) = n * f ^ (n - 1) * pderiv i f := by
rw [(pderiv i).leibniz_pow f n, nsmul_eq_mul, smul_eq_mul, mul_assoc]
theorem pderiv_C_mul {f : MvPolynomial σ R} {i : σ} : pderiv i (C a * f) = C a * pderiv i f := by
rw [C_mul', Derivation.map_smul, C_mul']
theorem pderiv_map {S} [CommSemiring S] {φ : R →+* S} {f : MvPolynomial σ R} {i : σ} :
pderiv i (map φ f) = map φ (pderiv i f) := by
apply induction_on f (fun r ↦ by simp) (fun p q hp hq ↦ by simp [hp, hq]) fun p j eq ↦ ?_
obtain rfl | h := eq_or_ne j i
· simp [eq]
· simp [eq, h]
lemma pderiv_rename {τ : Type*} {f : σ → τ} (hf : Function.Injective f)
(x : σ) (p : MvPolynomial σ R) :
pderiv (f x) (rename f p) = rename f (pderiv x p) := by
classical
induction p using MvPolynomial.induction_on with
| C a => simp
| add p q hp hq => simp [hp, hq]
| mul_X p a h =>
simp only [map_mul, MvPolynomial.rename_X, Derivation.leibniz, MvPolynomial.pderiv_X,
Pi.single_apply, hf.eq_iff, smul_eq_mul, mul_ite, mul_one, mul_zero, h, map_add]
split_ifs <;> simp
lemma aeval_sumElim_pderiv_inl {S τ : Type*} [CommRing S] [Algebra R S]
(p : MvPolynomial (σ ⊕ τ) R) (f : τ → S) (j : σ) :
aeval (Sum.elim X (C ∘ f)) ((pderiv (Sum.inl j)) p) =
(pderiv j) ((aeval (Sum.elim X (C ∘ f))) p) := by
classical
induction p using MvPolynomial.induction_on with
| C a => simp
| add p q hp hq => simp [hp, hq]
| mul_X p q h =>
simp only [Derivation.leibniz, pderiv_X, smul_eq_mul, map_add, map_mul, aeval_X, h]
cases q <;> simp [Pi.single_apply]
end PDeriv
end MvPolynomial |
.lake/packages/mathlib/Mathlib/Algebra/MvPolynomial/Derivation.lean | import Mathlib.Algebra.MvPolynomial.Supported
import Mathlib.RingTheory.Derivation.Basic
/-!
# Derivations of multivariate polynomials
In this file we prove that a derivation of `MvPolynomial σ R` is determined by its values on all
monomials `MvPolynomial.X i`. We also provide a constructor `MvPolynomial.mkDerivation` that
builds a derivation from its values on `X i`s and a linear equivalence
`MvPolynomial.mkDerivationEquiv` between `σ → A` and `Derivation (MvPolynomial σ R) A`.
-/
namespace MvPolynomial
noncomputable section
variable {σ R A : Type*} [CommSemiring R] [AddCommMonoid A] [Module R A]
[Module (MvPolynomial σ R) A]
section
variable (R)
/-- The derivation on `MvPolynomial σ R` that takes value `f i` on `X i`, as a linear map.
Use `MvPolynomial.mkDerivation` instead. -/
def mkDerivationₗ (f : σ → A) : MvPolynomial σ R →ₗ[R] A :=
Finsupp.lsum R fun xs : σ →₀ ℕ =>
(LinearMap.ringLmapEquivSelf R R A).symm <|
xs.sum fun i k => monomial (xs - Finsupp.single i 1) (k : R) • f i
end
theorem mkDerivationₗ_monomial (f : σ → A) (s : σ →₀ ℕ) (r : R) :
mkDerivationₗ R f (monomial s r) =
r • s.sum fun i k => monomial (s - Finsupp.single i 1) (k : R) • f i :=
sum_monomial_eq <| LinearMap.map_zero _
theorem mkDerivationₗ_C (f : σ → A) (r : R) : mkDerivationₗ R f (C r) = 0 :=
(mkDerivationₗ_monomial f _ _).trans (smul_zero _)
theorem mkDerivationₗ_X (f : σ → A) (i : σ) : mkDerivationₗ R f (X i) = f i :=
(mkDerivationₗ_monomial f _ _).trans <| by simp [tsub_self]
@[simp]
theorem derivation_C (D : Derivation R (MvPolynomial σ R) A) (a : R) : D (C a) = 0 :=
D.map_algebraMap a
@[simp]
theorem derivation_C_mul (D : Derivation R (MvPolynomial σ R) A) (a : R) (f : MvPolynomial σ R) :
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]
/-- If two derivations agree on `X i`, `i ∈ s`, then they agree on all polynomials from
`MvPolynomial.supported R s`. -/
theorem derivation_eqOn_supported {D₁ D₂ : Derivation R (MvPolynomial σ R) A} {s : Set σ}
(h : Set.EqOn (D₁ ∘ X) (D₂ ∘ X) s) {f : MvPolynomial σ R} (hf : f ∈ supported R s) :
D₁ f = D₂ f :=
Derivation.eqOn_adjoin (Set.forall_mem_image.2 h) hf
theorem derivation_eq_of_forall_mem_vars {D₁ D₂ : Derivation R (MvPolynomial σ R) A}
{f : MvPolynomial σ R} (h : ∀ i ∈ f.vars, D₁ (X i) = D₂ (X i)) : D₁ f = D₂ f :=
derivation_eqOn_supported h f.mem_supported_vars
theorem derivation_eq_zero_of_forall_mem_vars {D : Derivation R (MvPolynomial σ R) A}
{f : MvPolynomial σ R} (h : ∀ i ∈ f.vars, D (X i) = 0) : D f = 0 :=
show D f = (0 : Derivation R (MvPolynomial σ R) A) f from derivation_eq_of_forall_mem_vars h
@[ext]
theorem derivation_ext {D₁ D₂ : Derivation R (MvPolynomial σ R) A} (h : ∀ i, D₁ (X i) = D₂ (X i)) :
D₁ = D₂ :=
Derivation.ext fun _ => derivation_eq_of_forall_mem_vars fun i _ => h i
variable [IsScalarTower R (MvPolynomial σ R) A]
theorem leibniz_iff_X (D : MvPolynomial σ R →ₗ[R] A) (h₁ : D 1 = 0) :
(∀ p q, D (p * q) = p • D q + q • D p) ↔ ∀ s i, D (monomial s 1 * X i) =
(monomial s 1 : MvPolynomial σ R) • D (X i) + (X i : MvPolynomial σ R) • D (monomial s 1) := by
refine ⟨fun H p i => H _ _, fun H => ?_⟩
have hC : ∀ r, D (C r) = 0 := by intro r; rw [C_eq_smul_one, D.map_smul, h₁, smul_zero]
have : ∀ p i, D (p * X i) = p • D (X i) + (X i : MvPolynomial σ R) • D p := by
intro p i
induction p using MvPolynomial.induction_on' with
| monomial s r =>
rw [← mul_one r, ← C_mul_monomial, mul_assoc, C_mul', D.map_smul, H, C_mul', smul_assoc,
smul_add, D.map_smul, smul_comm r (X i)]
| add p q hp hq => rw [add_mul, map_add, map_add, hp, hq, add_smul, smul_add, add_add_add_comm]
intro p q
induction q using MvPolynomial.induction_on with
| C c =>
rw [mul_comm, C_mul', hC, smul_zero, zero_add, D.map_smul, C_eq_smul_one, smul_one_smul]
| add q₁ q₂ h₁ h₂ => simp only [mul_add, map_add, h₁, h₂, smul_add, add_smul]; abel
| mul_X q i hq =>
simp only [this, ← mul_assoc, hq, mul_smul, smul_add, add_assoc]
rw [smul_comm (X i), smul_comm (X i)]
variable (R)
/-- The derivation on `MvPolynomial σ R` that takes value `f i` on `X i`. -/
def mkDerivation (f : σ → A) : Derivation R (MvPolynomial σ R) A where
toLinearMap := mkDerivationₗ R f
map_one_eq_zero' := mkDerivationₗ_C _ 1
leibniz' :=
(leibniz_iff_X (mkDerivationₗ R f) (mkDerivationₗ_C _ 1)).2 fun s i => by
simp only [mkDerivationₗ_monomial, X, monomial_mul, one_smul, one_mul]
rw [Finsupp.sum_add_index'] <;>
[skip; simp; (intros; simp only [Nat.cast_add, (monomial _).map_add, add_smul])]
rw [Finsupp.sum_single_index, Finsupp.sum_single_index] <;> [skip; simp; simp]
rw [tsub_self, add_tsub_cancel_right, Nat.cast_one, ← C_apply, C_1, one_smul, add_comm,
Finsupp.smul_sum]
refine congr_arg₂ (· + ·) rfl (Finset.sum_congr rfl fun j hj => ?_); dsimp only
rw [smul_smul, monomial_mul, one_mul, add_comm s, add_tsub_assoc_of_le]
rwa [Finsupp.single_le_iff, Nat.succ_le_iff, pos_iff_ne_zero, ← Finsupp.mem_support_iff]
@[simp]
theorem mkDerivation_X (f : σ → A) (i : σ) : mkDerivation R f (X i) = f i :=
mkDerivationₗ_X f i
theorem mkDerivation_monomial (f : σ → A) (s : σ →₀ ℕ) (r : R) :
mkDerivation R f (monomial s r) =
r • s.sum fun i k => monomial (s - Finsupp.single i 1) (k : R) • f i :=
mkDerivationₗ_monomial f s r
/-- `MvPolynomial.mkDerivation` as a linear equivalence. -/
def mkDerivationEquiv : (σ → A) ≃ₗ[R] Derivation R (MvPolynomial σ R) A :=
LinearEquiv.symm <|
{ invFun := mkDerivation R
toFun := fun D i => D (X i)
map_add' := fun _ _ => rfl
map_smul' := fun _ _ => rfl
left_inv := fun _ => derivation_ext <| mkDerivation_X _ _
right_inv := fun _ => funext <| mkDerivation_X _ _ }
end
end MvPolynomial |
.lake/packages/mathlib/Mathlib/Algebra/MvPolynomial/Nilpotent.lean | import Mathlib.RingTheory.MvPolynomial.Homogeneous
import Mathlib.RingTheory.Polynomial.Nilpotent
/-!
# Nilpotents and units in multivariate polynomial rings
We prove that
- `MvPolynomial.isNilpotent_iff`:
A multivariate polynomial is nilpotent iff all its coefficients are.
- `MvPolynomial.isUnit_iff`:
A multivariate polynomial is invertible iff its constant term is invertible
and its other coefficients are nilpotent.
-/
namespace MvPolynomial
variable {σ R : Type*} [CommRing R] {P : MvPolynomial σ R}
-- Subsumed by `isNilpotent_iff` below.
private theorem isNilpotent_iff_of_fintype [Fintype σ] :
IsNilpotent P ↔ ∀ i, IsNilpotent (P.coeff i) := by
classical
refine Fintype.induction_empty_option ?_ ?_ ?_ σ P
· intro α β _ e h₁ P
rw [← IsNilpotent.map_iff (rename_injective _ e.symm.injective), h₁,
(Finsupp.equivCongrLeft e).forall_congr_left]
simp [Finsupp.equivMapDomain_eq_mapDomain, coeff_rename_mapDomain _ e.symm.injective]
· intro P
simp [Unique.forall_iff, ← IsNilpotent.map_iff (isEmptyRingEquiv R PEmpty).injective,
-isEmptyRingEquiv_apply, isEmptyRingEquiv_eq_coeff_zero]
rfl
· intro α _ H P
obtain ⟨P, rfl⟩ := (optionEquivLeft _ _).symm.surjective P
simp [IsNilpotent.map_iff (optionEquivLeft _ _).symm.injective,
Polynomial.isNilpotent_iff, H, Finsupp.optionEquiv.forall_congr_left,
← optionEquivLeft_coeff_coeff, Finsupp.coe_update]
theorem isNilpotent_iff : IsNilpotent P ↔ ∀ i, IsNilpotent (P.coeff i) := by
obtain ⟨n, f, hf, P, rfl⟩ := P.exists_fin_rename
rw [IsNilpotent.map_iff (rename_injective _ hf), MvPolynomial.isNilpotent_iff_of_fintype]
lift f to Fin n ↪ σ using hf
refine ⟨fun H i ↦ ?_, fun H i ↦ by simpa using H (i.embDomain f)⟩
by_cases H : i ∈ Set.range (Finsupp.embDomain f)
· aesop
· rw [coeff_rename_eq_zero] <;> aesop (add simp Finsupp.embDomain_eq_mapDomain)
instance [IsReduced R] : IsReduced (MvPolynomial σ R) := by
simp [isReduced_iff, isNilpotent_iff, MvPolynomial.ext_iff]
theorem isUnit_iff : IsUnit P ↔ IsUnit (P.coeff 0) ∧ ∀ i ≠ 0, IsNilpotent (P.coeff i) := by
classical
refine ⟨fun H ↦ ⟨H.map constantCoeff, ?_⟩, fun ⟨h₁, h₂⟩ ↦ ?_⟩
· intro n hn
obtain ⟨i, hi⟩ : ∃ i, n i ≠ 0 := by simpa [Finsupp.ext_iff] using hn
let e := (optionEquivLeft _ _).symm.trans (renameEquiv R (Equiv.optionSubtypeNe i))
have H := (Polynomial.coeff_isUnit_isNilpotent_of_isUnit (H.map e.symm)).2 (n i) hi
simp only [ne_eq, isNilpotent_iff] at H
convert ← H (n.equivMapDomain (Equiv.optionSubtypeNe i).symm).some
refine (optionEquivLeft_coeff_coeff _ _ _ _).trans ?_
simp [Finsupp.equivMapDomain_eq_mapDomain,
coeff_rename_mapDomain _ (Equiv.optionSubtypeNe i).symm.injective]
· have : IsNilpotent (P - C (P.coeff 0)) := by
simp +contextual [isNilpotent_iff, apply_ite, eq_comm, h₂]
simpa using this.isUnit_add_right_of_commute (h₁.map C) (.all _ _)
instance : IsLocalHom (C : _ →+* MvPolynomial σ R) where
map_nonunit := by classical simp +contextual [isUnit_iff, coeff_C, apply_ite]
instance : IsLocalHom (algebraMap R (MvPolynomial σ R)) :=
inferInstanceAs (IsLocalHom C)
theorem isUnit_iff_totalDegree_of_isReduced [IsReduced R] :
IsUnit P ↔ IsUnit (P.coeff 0) ∧ P.totalDegree = 0 := by
convert isUnit_iff (P := P)
rw [totalDegree_eq_zero_iff]
simp [not_imp_comm (a := _ = (0 : R)), Finsupp.ext_iff]
theorem isUnit_iff_eq_C_of_isReduced [IsReduced R] :
IsUnit P ↔ ∃ r, IsUnit r ∧ P = C r := by
rw [isUnit_iff_totalDegree_of_isReduced, totalDegree_eq_zero_iff_eq_C]
refine ⟨fun H ↦ ⟨_, H⟩, ?_⟩
rintro ⟨r, hr, rfl⟩
simpa
end MvPolynomial |
.lake/packages/mathlib/Mathlib/Algebra/MvPolynomial/Comap.lean | import Mathlib.Algebra.MvPolynomial.Rename
/-!
# `comap` operation on `MvPolynomial`
This file defines the `comap` function on `MvPolynomial`.
`MvPolynomial.comap` is a low-tech example of a map of "algebraic varieties," modulo the fact that
`mathlib` does not yet define varieties.
## Notation
As in other polynomial files, we typically use the notation:
+ `σ : Type*` (indexing the variables)
+ `R : Type*` `[CommSemiring R]` (the coefficients)
-/
namespace MvPolynomial
variable {σ : Type*} {τ : Type*} {υ : Type*} {R : Type*} [CommSemiring R]
/-- Given an algebra hom `f : MvPolynomial σ R →ₐ[R] MvPolynomial τ R`
and a variable evaluation `v : τ → R`,
`comap f v` produces a variable evaluation `σ → R`.
-/
noncomputable def comap (f : MvPolynomial σ R →ₐ[R] MvPolynomial τ R) : (τ → R) → σ → R :=
fun x i => aeval x (f (X i))
@[simp]
theorem comap_apply (f : MvPolynomial σ R →ₐ[R] MvPolynomial τ R) (x : τ → R) (i : σ) :
comap f x i = aeval x (f (X i)) :=
rfl
@[simp]
theorem comap_id_apply (x : σ → R) : comap (AlgHom.id R (MvPolynomial σ R)) x = x := by
funext i
simp only [comap, AlgHom.id_apply, aeval_X]
variable (σ R)
theorem comap_id : comap (AlgHom.id R (MvPolynomial σ R)) = id := by
funext x
exact comap_id_apply x
variable {σ R}
theorem comap_comp_apply (f : MvPolynomial σ R →ₐ[R] MvPolynomial τ R)
(g : MvPolynomial τ R →ₐ[R] MvPolynomial υ R) (x : υ → R) :
comap (g.comp f) x = comap f (comap g x) := by
funext i
trans aeval x (aeval (fun i => g (X i)) (f (X i)))
· apply eval₂Hom_congr rfl rfl
rw [AlgHom.comp_apply]
suffices g = aeval fun i => g (X i) by rw [← this]
exact aeval_unique g
· simp only [comap, aeval_eq_eval₂Hom, map_eval₂Hom]
refine eval₂Hom_congr ?_ rfl rfl
ext r
apply aeval_C
theorem comap_comp (f : MvPolynomial σ R →ₐ[R] MvPolynomial τ R)
(g : MvPolynomial τ R →ₐ[R] MvPolynomial υ R) : comap (g.comp f) = comap f ∘ comap g := by
funext x
exact comap_comp_apply _ _ _
theorem comap_eq_id_of_eq_id (f : MvPolynomial σ R →ₐ[R] MvPolynomial σ R) (hf : ∀ φ, f φ = φ)
(x : σ → R) : comap f x = x := by
convert comap_id_apply x
ext1 φ
simp [hf, AlgHom.id_apply]
theorem comap_rename (f : σ → τ) (x : τ → R) : comap (rename f) x = x ∘ f := by
funext
simp [rename_X, comap_apply, aeval_X]
/-- If two polynomial types over the same coefficient ring `R` are equivalent,
there is a bijection between the types of functions from their variable types to `R`.
-/
noncomputable def comapEquiv (f : MvPolynomial σ R ≃ₐ[R] MvPolynomial τ R) : (τ → R) ≃ (σ → R) where
toFun := comap f
invFun := comap f.symm
left_inv := by
intro x
rw [← comap_comp_apply]
apply comap_eq_id_of_eq_id
intro
simp only [AlgHom.id_apply, AlgEquiv.comp_symm]
right_inv := by
intro x
rw [← comap_comp_apply]
apply comap_eq_id_of_eq_id
intro
simp only [AlgHom.id_apply, AlgEquiv.symm_comp]
@[simp]
theorem comapEquiv_coe (f : MvPolynomial σ R ≃ₐ[R] MvPolynomial τ R) :
(comapEquiv f : (τ → R) → σ → R) = comap f :=
rfl
@[simp]
theorem comapEquiv_symm_coe (f : MvPolynomial σ R ≃ₐ[R] MvPolynomial τ R) :
((comapEquiv f).symm : (σ → R) → τ → R) = comap f.symm :=
rfl
end MvPolynomial |
.lake/packages/mathlib/Mathlib/Algebra/MvPolynomial/Variables.lean | import Mathlib.Data.Finsupp.Lex
import Mathlib.Algebra.MvPolynomial.Degrees
/-!
# Variables of polynomials
This file establishes many results about the variable sets of a multivariate polynomial.
The *variable set* of a polynomial $P \in R[X]$ is a `Finset` containing each $x \in X$
that appears in a monomial in $P$.
## Main declarations
* `MvPolynomial.vars p` : the finset of variables occurring in `p`.
For example if `p = x⁴y+yz` then `vars p = {x, y, z}`
## Notation
As in other polynomial files, we typically use the notation:
+ `σ τ : Type*` (indexing the variables)
+ `R : Type*` `[CommSemiring R]` (the coefficients)
+ `s : σ →₀ ℕ`, a function from `σ` to `ℕ` which is zero away from a finite set.
This will give rise to a monomial in `MvPolynomial σ R` which mathematicians might call `X^s`
+ `r : R`
+ `i : σ`, with corresponding monomial `X i`, often denoted `X_i` by mathematicians
+ `p : MvPolynomial σ R`
-/
noncomputable section
open Set Function Finsupp AddMonoidAlgebra
universe u v w
variable {R : Type u} {S : Type v}
namespace MvPolynomial
variable {σ τ : Type*} {r : R} {e : ℕ} {n m : σ} {s : σ →₀ ℕ}
section CommSemiring
variable [CommSemiring R] {p q : MvPolynomial σ R}
section Vars
/-! ### `vars` -/
/-- `vars p` is the set of variables appearing in the polynomial `p` -/
def vars (p : MvPolynomial σ R) : Finset σ :=
letI := Classical.decEq σ
p.degrees.toFinset
theorem vars_def [DecidableEq σ] (p : MvPolynomial σ R) : p.vars = p.degrees.toFinset := by
rw [vars]
convert rfl
@[simp]
theorem vars_0 : (0 : MvPolynomial σ R).vars = ∅ := by
classical rw [vars_def, degrees_zero, Multiset.toFinset_zero]
@[simp]
theorem vars_monomial (h : r ≠ 0) : (monomial s r).vars = s.support := by
classical rw [vars_def, degrees_monomial_eq _ _ h, Finsupp.toFinset_toMultiset]
@[simp]
theorem vars_C : (C r : MvPolynomial σ R).vars = ∅ := by
classical rw [vars_def, degrees_C, Multiset.toFinset_zero]
@[simp]
theorem vars_X [Nontrivial R] : (X n : MvPolynomial σ R).vars = {n} := by
rw [X, vars_monomial (one_ne_zero' R), Finsupp.support_single_ne_zero _ (one_ne_zero' ℕ)]
theorem mem_vars (i : σ) : i ∈ p.vars ↔ ∃ d ∈ p.support, i ∈ d.support := by
classical simp only [vars_def, Multiset.mem_toFinset, mem_degrees, mem_support_iff]
theorem mem_support_notMem_vars_zero {f : MvPolynomial σ R} {x : σ →₀ ℕ} (H : x ∈ f.support)
{v : σ} (h : v ∉ vars f) : x v = 0 := by
contrapose! h
exact (mem_vars v).mpr ⟨x, H, Finsupp.mem_support_iff.mpr h⟩
@[deprecated (since := "2025-05-23")]
alias mem_support_not_mem_vars_zero := mem_support_notMem_vars_zero
theorem vars_add_subset [DecidableEq σ] (p q : MvPolynomial σ R) :
(p + q).vars ⊆ p.vars ∪ q.vars := by
intro x hx
simp only [vars_def, Finset.mem_union, Multiset.mem_toFinset] at hx ⊢
simpa using Multiset.mem_of_le degrees_add_le hx
theorem vars_add_of_disjoint [DecidableEq σ] (h : Disjoint p.vars q.vars) :
(p + q).vars = p.vars ∪ q.vars := by
refine (vars_add_subset p q).antisymm fun x hx => ?_
simp only [vars_def, Multiset.disjoint_toFinset] at h hx ⊢
rwa [degrees_add_of_disjoint h, Multiset.toFinset_union]
section Mul
theorem vars_mul [DecidableEq σ] (φ ψ : MvPolynomial σ R) : (φ * ψ).vars ⊆ φ.vars ∪ ψ.vars := by
simp_rw [vars_def, ← Multiset.toFinset_add, Multiset.toFinset_subset]
exact Multiset.subset_of_le degrees_mul_le
@[simp]
theorem vars_one : (1 : MvPolynomial σ R).vars = ∅ :=
vars_C
theorem vars_pow (φ : MvPolynomial σ R) (n : ℕ) : (φ ^ n).vars ⊆ φ.vars := by
classical
induction n with
| zero => simp
| succ n ih =>
rw [pow_succ']
apply Finset.Subset.trans (vars_mul _ _)
exact Finset.union_subset (Finset.Subset.refl _) ih
/-- The variables of the product of a family of polynomials
are a subset of the union of the sets of variables of each polynomial.
-/
theorem vars_prod {ι : Type*} [DecidableEq σ] {s : Finset ι} (f : ι → MvPolynomial σ R) :
(∏ i ∈ s, f i).vars ⊆ s.biUnion fun i => (f i).vars := by
classical
induction s using Finset.induction_on with
| empty => simp
| insert _ _ hs hsub =>
simp only [hs, Finset.biUnion_insert, Finset.prod_insert, not_false_iff]
apply Finset.Subset.trans (vars_mul _ _)
exact Finset.union_subset_union (Finset.Subset.refl _) hsub
section IsDomain
variable {A : Type*} [CommRing A] [NoZeroDivisors A]
theorem vars_C_mul (a : A) (ha : a ≠ 0) (φ : MvPolynomial σ A) :
(C a * φ : MvPolynomial σ A).vars = φ.vars := by
ext1 i
simp only [mem_vars, mem_support_iff]
apply exists_congr
intro d
rw [coeff_C_mul, mul_ne_zero_iff, eq_true ha, true_and]
end IsDomain
end Mul
section Sum
variable {ι : Type*} (t : Finset ι) (φ : ι → MvPolynomial σ R)
theorem vars_sum_subset [DecidableEq σ] :
(∑ i ∈ t, φ i).vars ⊆ Finset.biUnion t fun i => (φ i).vars := by
classical
induction t using Finset.induction_on with
| empty => simp
| insert _ _ has hsum =>
rw [Finset.biUnion_insert, Finset.sum_insert has]
refine Finset.Subset.trans
(vars_add_subset _ _) (Finset.union_subset_union (Finset.Subset.refl _) ?_)
assumption
theorem vars_sum_of_disjoint [DecidableEq σ] (h : Pairwise <| (Disjoint on fun i => (φ i).vars)) :
(∑ i ∈ t, φ i).vars = Finset.biUnion t fun i => (φ i).vars := by
classical
induction t using Finset.induction_on with
| empty => simp
| insert _ _ has hsum =>
rw [Finset.biUnion_insert, Finset.sum_insert has, vars_add_of_disjoint, hsum]
unfold Pairwise onFun at h
simp only [Finset.disjoint_iff_ne] at h ⊢
grind
end Sum
section Map
variable [CommSemiring S] (f : R →+* S)
variable (p)
theorem vars_map : (map f p).vars ⊆ p.vars := by
classical simp [vars_def, Multiset.subset_of_le degrees_map_le]
variable {f}
theorem vars_map_of_injective (hf : Injective f) : (map f p).vars = p.vars := by
simp [vars, degrees_map_of_injective _ hf]
theorem vars_monomial_single (i : σ) {e : ℕ} {r : R} (he : e ≠ 0) (hr : r ≠ 0) :
(monomial (Finsupp.single i e) r).vars = {i} := by
rw [vars_monomial hr, Finsupp.support_single_ne_zero _ he]
theorem vars_eq_support_biUnion_support [DecidableEq σ] :
p.vars = p.support.biUnion Finsupp.support := by
ext i
rw [mem_vars, Finset.mem_biUnion]
end Map
end Vars
section EvalVars
/-! ### `vars` and `eval` -/
variable [CommSemiring S]
theorem eval₂Hom_eq_constantCoeff_of_vars (f : R →+* S) {g : σ → S} {p : MvPolynomial σ R}
(hp : ∀ i ∈ p.vars, g i = 0) : eval₂Hom f g p = f (constantCoeff p) := by
conv_lhs => rw [p.as_sum]
simp only [map_sum, eval₂Hom_monomial]
by_cases h0 : constantCoeff p = 0
on_goal 1 =>
rw [h0, f.map_zero, Finset.sum_eq_zero]
intro d hd
on_goal 2 =>
rw [Finset.sum_eq_single (0 : σ →₀ ℕ)]
· rw [Finsupp.prod_zero_index, mul_one]
rfl
on_goal 1 => intro d hd hd0
on_goal 3 =>
rw [constantCoeff_eq, coeff, ← Ne, ← Finsupp.mem_support_iff] at h0
intro
contradiction
repeat'
obtain ⟨i, hi⟩ : Finset.Nonempty (Finsupp.support d) := by
rw [constantCoeff_eq, coeff, ← Finsupp.notMem_support_iff] at h0
rw [Finset.nonempty_iff_ne_empty, Ne, Finsupp.support_eq_empty]
rintro rfl
contradiction
rw [Finsupp.prod, Finset.prod_eq_zero hi, mul_zero]
rw [hp, zero_pow (Finsupp.mem_support_iff.1 hi)]
rw [mem_vars]
exact ⟨d, hd, hi⟩
theorem aeval_eq_constantCoeff_of_vars [Algebra R S] {g : σ → S} {p : MvPolynomial σ R}
(hp : ∀ i ∈ p.vars, g i = 0) : aeval g p = algebraMap _ _ (constantCoeff p) :=
eval₂Hom_eq_constantCoeff_of_vars _ hp
theorem eval₂Hom_congr' {f₁ f₂ : R →+* S} {g₁ g₂ : σ → S} {p₁ p₂ : MvPolynomial σ R} :
f₁ = f₂ →
(∀ i, i ∈ p₁.vars → i ∈ p₂.vars → g₁ i = g₂ i) →
p₁ = p₂ → eval₂Hom f₁ g₁ p₁ = eval₂Hom f₂ g₂ p₂ := by
rintro rfl h rfl
rw [p₁.as_sum]
simp only [map_sum, eval₂Hom_monomial]
apply Finset.sum_congr rfl
intro d hd
congr 1
simp only [Finsupp.prod]
apply Finset.prod_congr rfl
intro i hi
have : i ∈ p₁.vars := by
rw [mem_vars]
exact ⟨d, hd, hi⟩
rw [h i this this]
/-- If `f₁` and `f₂` are ring homs out of the polynomial ring and `p₁` and `p₂` are polynomials,
then `f₁ p₁ = f₂ p₂` if `p₁ = p₂` and `f₁` and `f₂` are equal on `R` and on the variables
of `p₁`. -/
theorem hom_congr_vars {f₁ f₂ : MvPolynomial σ R →+* S} {p₁ p₂ : MvPolynomial σ R}
(hC : f₁.comp C = f₂.comp C) (hv : ∀ i, i ∈ p₁.vars → i ∈ p₂.vars → f₁ (X i) = f₂ (X i))
(hp : p₁ = p₂) : f₁ p₁ = f₂ p₂ :=
calc
f₁ p₁ = eval₂Hom (f₁.comp C) (f₁ ∘ X) p₁ := RingHom.congr_fun (by ext <;> simp) _
_ = eval₂Hom (f₂.comp C) (f₂ ∘ X) p₂ := eval₂Hom_congr' hC hv hp
_ = f₂ p₂ := RingHom.congr_fun (by ext <;> simp) _
theorem exists_rename_eq_of_vars_subset_range (p : MvPolynomial σ R) (f : τ → σ) (hfi : Injective f)
(hf : ↑p.vars ⊆ Set.range f) : ∃ q : MvPolynomial τ R, rename f q = p :=
⟨aeval (fun i : σ => Option.elim' 0 X <| partialInv f i) p,
by
change (rename f).toRingHom.comp _ p = RingHom.id _ p
refine hom_congr_vars ?_ ?_ ?_
· ext1
simp [algebraMap_eq]
· intro i hip _
rcases hf hip with ⟨i, rfl⟩
simp [partialInv_left hfi]
· rfl⟩
theorem vars_rename [DecidableEq τ] (f : σ → τ) (φ : MvPolynomial σ R) :
(rename f φ).vars ⊆ φ.vars.image f := by
classical
intro i hi
simp only [vars_def, Multiset.mem_toFinset, Finset.mem_image] at hi ⊢
simpa only [Multiset.mem_map] using degrees_rename _ _ hi
theorem mem_vars_rename (f : σ → τ) (φ : MvPolynomial σ R) {j : τ} (h : j ∈ (rename f φ).vars) :
∃ i : σ, i ∈ φ.vars ∧ f i = j := by
classical
simpa only [exists_prop, Finset.mem_image] using vars_rename f φ h
lemma aeval_ite_mem_eq_self (q : MvPolynomial σ R) {s : Set σ} (hs : (q.vars : Set σ) ⊆ s)
[∀ i, Decidable (i ∈ s)] :
MvPolynomial.aeval (fun i ↦ if i ∈ s then .X i else 0) q = q := by
rw [MvPolynomial.as_sum q, MvPolynomial.aeval_sum]
refine Finset.sum_congr rfl fun u hu ↦ ?_
rw [MvPolynomial.aeval_monomial, MvPolynomial.monomial_eq]
congr 1
exact Finsupp.prod_congr (fun i hi ↦ by simp [hs ((MvPolynomial.mem_vars _).mpr ⟨u, hu, hi⟩)])
end EvalVars
section Lex
variable [LinearOrder σ]
lemma leadingCoeff_toLex : p.leadingCoeff toLex = p.coeff (ofLex <| p.supDegree toLex) := by
rw [leadingCoeff]
apply congr_arg p.coeff
apply toLex.injective
rw [Function.rightInverse_invFun toLex.surjective, toLex_ofLex]
lemma supDegree_toLex_C (r : R) : supDegree toLex (C (σ := σ) r) = 0 := by
classical
exact (supDegree_single _ r).trans (ite_eq_iff'.mpr ⟨fun _ => rfl, fun _ => rfl⟩)
lemma leadingCoeff_toLex_C (r : R) : leadingCoeff toLex (C (σ := σ) r) = r :=
leadingCoeff_single toLex.injective _ r
end Lex
end CommSemiring
end MvPolynomial |
.lake/packages/mathlib/Mathlib/Algebra/MvPolynomial/Expand.lean | import Mathlib.Algebra.MvPolynomial.Monad
/-!
## Expand multivariate polynomials
Given a multivariate polynomial `φ`, one may replace every occurrence of `X i` by `X i ^ n`,
for some natural number `n`.
This operation is called `MvPolynomial.expand` and it is an algebra homomorphism.
### Main declaration
* `MvPolynomial.expand`: expand a polynomial by a factor of p, so `∑ aₙ xⁿ` becomes `∑ aₙ xⁿᵖ`.
-/
namespace MvPolynomial
variable {σ τ R S : Type*} [CommSemiring R] [CommSemiring S]
/-- Expand the polynomial by a factor of p, so `∑ aₙ xⁿ` becomes `∑ aₙ xⁿᵖ`.
See also `Polynomial.expand`. -/
noncomputable def expand (p : ℕ) : MvPolynomial σ R →ₐ[R] MvPolynomial σ R :=
bind₁ fun i ↦ X i ^ p
theorem expand_C (p : ℕ) (r : R) : expand p (C r : MvPolynomial σ R) = C r :=
eval₂Hom_C _ _ _
@[simp]
theorem expand_X (p : ℕ) (i : σ) : expand p (X i : MvPolynomial σ R) = X i ^ p :=
eval₂Hom_X' _ _ _
@[simp]
theorem expand_monomial (p : ℕ) (d : σ →₀ ℕ) (r : R) :
expand p (monomial d r) = monomial (p • d) r := by
rw [expand, bind₁_monomial, monomial_eq, Finsupp.prod_of_support_subset _ Finsupp.support_smul]
· simp [pow_mul]
· simp
@[simp]
lemma expand_zero :
expand 0 (σ := σ) (R := R) = .comp (Algebra.ofId R _) (MvPolynomial.aeval (1 : σ → R)) := by
ext1 i
simp
lemma expand_zero_apply (p : MvPolynomial σ R) : expand 0 p = .C (MvPolynomial.eval 1 p) := by
simp
@[simp]
theorem expand_one : expand 1 = AlgHom.id R (MvPolynomial σ R) := by
ext1 i
simp
theorem expand_one_apply (f : MvPolynomial σ R) : expand 1 f = f := by simp
theorem expand_comp_bind₁ (p : ℕ) (f : σ → MvPolynomial τ R) :
(expand p).comp (bind₁ f) = bind₁ fun i ↦ expand p (f i) := by
ext1 i
simp
theorem expand_bind₁ (p : ℕ) (f : σ → MvPolynomial τ R) (φ : MvPolynomial σ R) :
expand p (bind₁ f φ) = bind₁ (fun i ↦ expand p (f i)) φ := by
rw [← AlgHom.comp_apply, expand_comp_bind₁]
@[simp]
theorem map_expand (f : R →+* S) (p : ℕ) (φ : MvPolynomial σ R) :
map f (expand p φ) = expand p (map f φ) := by simp [expand, map_bind₁]
@[simp]
theorem rename_comp_expand (f : σ → τ) (p : ℕ) :
(rename f).comp (expand p) =
(expand p).comp (rename f : MvPolynomial σ R →ₐ[R] MvPolynomial τ R) := by
ext1 i
simp
@[simp]
theorem rename_expand (f : σ → τ) (p : ℕ) (φ : MvPolynomial σ R) :
rename f (expand p φ) = expand p (rename f φ) :=
DFunLike.congr_fun (rename_comp_expand f p) φ
lemma eval₂Hom_comp_expand (f : R →+* S) (g : σ → S) (p : ℕ) :
(eval₂Hom f g).comp (expand p (σ := σ) (R := R) : MvPolynomial σ R →+* MvPolynomial σ R) =
eval₂Hom f (g ^ p) := by
ext <;> simp
@[simp]
lemma eval₂_expand (f : R →+* S) (g : σ → S) (φ : MvPolynomial σ R) (p : ℕ) :
eval₂ f g (expand p φ) = eval₂ f (g ^ p) φ :=
DFunLike.congr_fun (eval₂Hom_comp_expand f g p) φ
@[simp]
lemma aeval_comp_expand {A : Type*} [CommSemiring A] [Algebra R A] (f : σ → A) (p : ℕ) :
(aeval f).comp (expand p) = aeval (R := R) (f ^ p) := by
ext; simp
@[simp]
lemma aeval_expand {A : Type*} [CommSemiring A] [Algebra R A]
(f : σ → A) (φ : MvPolynomial σ R) (p : ℕ) :
aeval f (expand p φ) = aeval (f ^ p) φ :=
eval₂_expand ..
@[simp]
lemma eval_expand (f : σ → R) (φ : MvPolynomial σ R) (p : ℕ) :
eval f (expand p φ) = eval (f ^ p) φ :=
eval₂_expand ..
theorem expand_mul_eq_comp (p q : ℕ) :
expand (σ := σ) (R := R) (p * q) = (expand p).comp (expand q) := by
ext1 i
simp [pow_mul]
theorem expand_mul (p q : ℕ) (φ : MvPolynomial σ R) : φ.expand (p * q) = (φ.expand q).expand p :=
DFunLike.congr_fun (expand_mul_eq_comp p q) φ
@[simp]
lemma coeff_expand_smul (φ : MvPolynomial σ R) {p : ℕ} (hp : p ≠ 0) (m : σ →₀ ℕ) :
(expand p φ).coeff (p • m) = φ.coeff m := by
classical
induction φ using induction_on' <;> simp [*, nsmul_right_inj hp]
lemma support_expand_subset [DecidableEq σ] (φ : MvPolynomial σ R) (p : ℕ) :
(expand p φ).support ⊆ φ.support.image (p • ·) := by
conv_lhs => rw [φ.as_sum]
simp only [map_sum, expand_monomial]
refine MvPolynomial.support_sum.trans ?_
aesop (add simp Finset.subset_iff)
lemma coeff_expand_of_not_dvd (φ : MvPolynomial σ R) {p : ℕ} {m : σ →₀ ℕ} {i : σ} (h : ¬(p ∣ m i)) :
(expand p φ).coeff m = 0 := by
classical
contrapose! h
grw [← mem_support_iff, support_expand_subset, Finset.mem_image] at h
rcases h with ⟨a, -, rfl⟩
exact ⟨a i, by simp⟩
lemma support_expand [DecidableEq σ] (φ : MvPolynomial σ R) {p : ℕ} (hp : p ≠ 0) :
(expand p φ).support = φ.support.image (p • ·) := by
refine (support_expand_subset φ p).antisymm ?_
simp [Finset.image_subset_iff, hp]
end MvPolynomial |
.lake/packages/mathlib/Mathlib/Algebra/MvPolynomial/Rename.lean | import Mathlib.Algebra.MvPolynomial.Eval
/-!
# Renaming variables of polynomials
This file establishes the `rename` operation on multivariate polynomials,
which modifies the set of variables.
## Main declarations
* `MvPolynomial.rename`
* `MvPolynomial.renameEquiv`
## Notation
As in other polynomial files, we typically use the notation:
+ `σ τ α : Type*` (indexing the variables)
+ `R S : Type*` `[CommSemiring R]` `[CommSemiring S]` (the coefficients)
+ `s : σ →₀ ℕ`, a function from `σ` to `ℕ` which is zero away from a finite set.
This will give rise to a monomial in `MvPolynomial σ R` which mathematicians might call `X^s`
+ `r : R` elements of the coefficient ring
+ `i : σ`, with corresponding monomial `X i`, often denoted `X_i` by mathematicians
+ `p : MvPolynomial σ α`
-/
noncomputable section
open Set Function Finsupp AddMonoidAlgebra
variable {σ τ α R S : Type*} [CommSemiring R] [CommSemiring S]
namespace MvPolynomial
section Rename
/-- Rename all the variables in a multivariable polynomial. -/
def rename (f : σ → τ) : MvPolynomial σ R →ₐ[R] MvPolynomial τ R :=
aeval (X ∘ f)
theorem rename_C (f : σ → τ) (r : R) : rename f (C r) = C r :=
eval₂_C _ _ _
@[simp]
theorem rename_X (f : σ → τ) (i : σ) : rename f (X i : MvPolynomial σ R) = X (f i) :=
eval₂_X _ _ _
theorem map_rename (f : R →+* S) (g : σ → τ) (p : MvPolynomial σ R) :
map f (rename g p) = rename g (map f p) := by
apply MvPolynomial.induction_on p
(fun a => by simp only [map_C, rename_C])
(fun p q hp hq => by simp only [hp, hq, map_add]) fun p n hp => by
simp only [hp, rename_X, map_X, map_mul]
lemma map_comp_rename (f : R →+* S) (g : σ → τ) :
(map f).comp (rename g).toRingHom = (rename g).toRingHom.comp (map f) :=
RingHom.ext fun p ↦ map_rename f g p
@[simp]
theorem rename_rename (f : σ → τ) (g : τ → α) (p : MvPolynomial σ R) :
rename g (rename f p) = rename (g ∘ f) p := by
nth_rw 2 [rename]
simp_rw [aeval_def, algebraMap_eq, rename, aeval_eq_eval₂Hom]
rw [eval₂_comp_left (eval₂Hom (algebraMap R (MvPolynomial α R)) (X ∘ g)) C (X ∘ f) p]
simp only [comp_def, eval₂Hom_X']
refine eval₂Hom_congr ?_ rfl rfl
ext1; simp only [comp_apply, RingHom.coe_comp, eval₂Hom_C]
lemma rename_comp_rename (f : σ → τ) (g : τ → α) :
(rename (R := R) g).comp (rename f) = rename (g ∘ f) :=
AlgHom.ext fun p ↦ rename_rename f g p
@[simp]
theorem rename_id : rename id = AlgHom.id R (MvPolynomial σ R) :=
AlgHom.ext fun p ↦ eval₂_eta p
lemma rename_id_apply (p : MvPolynomial σ R) : rename id p = p := by
simp
theorem rename_monomial (f : σ → τ) (d : σ →₀ ℕ) (r : R) :
rename f (monomial d r) = monomial (d.mapDomain f) r := by
rw [rename, aeval_monomial, monomial_eq (s := Finsupp.mapDomain f d),
Finsupp.prod_mapDomain_index, algebraMap_eq]
· simp_rw [Function.comp_apply]
· exact fun n => pow_zero _
· exact fun n i₁ i₂ => pow_add _ _ _
theorem rename_eq (f : σ → τ) (p : MvPolynomial σ R) :
rename f p = Finsupp.mapDomain (Finsupp.mapDomain f) p := by
simp_rw [rename, aeval_def, eval₂, Finsupp.mapDomain, algebraMap_eq, comp_apply,
X_pow_eq_monomial, ← monomial_finsupp_sum_index, ← single_eq_monomial]
theorem rename_injective (f : σ → τ) (hf : Function.Injective f) :
Function.Injective (rename f : MvPolynomial σ R → MvPolynomial τ R) := by
have :
(rename f : MvPolynomial σ R → MvPolynomial τ R) = Finsupp.mapDomain (Finsupp.mapDomain f) :=
funext (rename_eq f)
rw [this]
exact Finsupp.mapDomain_injective (Finsupp.mapDomain_injective hf)
theorem rename_leftInverse {f : σ → τ} {g : τ → σ} (hf : Function.LeftInverse f g) :
Function.LeftInverse (rename f : MvPolynomial σ R → MvPolynomial τ R) (rename g) := by
intro x
simp [hf.comp_eq_id]
theorem rename_rightInverse {f : σ → τ} {g : τ → σ} (hf : Function.RightInverse f g) :
Function.RightInverse (rename f : MvPolynomial σ R → MvPolynomial τ R) (rename g) :=
rename_leftInverse hf
theorem rename_surjective (f : σ → τ) (hf : Function.Surjective f) :
Function.Surjective (rename f : MvPolynomial σ R → MvPolynomial τ R) :=
let ⟨_, hf⟩ := hf.hasRightInverse; rename_rightInverse hf |>.surjective
section
variable {f : σ → τ} (hf : Function.Injective f)
open Classical in
/-- Given a function between sets of variables `f : σ → τ` that is injective with proof `hf`,
`MvPolynomial.killCompl hf` is the `AlgHom` from `R[τ]` to `R[σ]` that is left inverse to
`rename f : R[σ] → R[τ]` and sends the variables in the complement of the range of `f` to `0`. -/
def killCompl : MvPolynomial τ R →ₐ[R] MvPolynomial σ R :=
aeval fun i => if h : i ∈ Set.range f then X <| (Equiv.ofInjective f hf).symm ⟨i, h⟩ else 0
theorem killCompl_C (r : R) : killCompl hf (C r) = C r := algHom_C _ _
theorem killCompl_comp_rename : (killCompl hf).comp (rename f) = AlgHom.id R _ :=
algHom_ext fun i => by
dsimp
rw [rename, killCompl, aeval_X, comp_apply, aeval_X, dif_pos ⟨i, rfl⟩,
Equiv.ofInjective_symm_apply]
@[simp]
theorem killCompl_rename_app (p : MvPolynomial σ R) : killCompl hf (rename f p) = p :=
AlgHom.congr_fun (killCompl_comp_rename hf) p
end
section
variable (R)
/-- `MvPolynomial.rename e` is an equivalence when `e` is. -/
@[simps apply]
def renameEquiv (f : σ ≃ τ) : MvPolynomial σ R ≃ₐ[R] MvPolynomial τ R :=
{ rename f with
toFun := rename f
invFun := rename f.symm
left_inv := fun p => by rw [rename_rename, f.symm_comp_self, rename_id_apply]
right_inv := fun p => by rw [rename_rename, f.self_comp_symm, rename_id_apply] }
@[simp]
theorem renameEquiv_refl : renameEquiv R (Equiv.refl σ) = AlgEquiv.refl :=
AlgEquiv.ext (by simp)
@[simp]
theorem renameEquiv_symm (f : σ ≃ τ) : (renameEquiv R f).symm = renameEquiv R f.symm :=
rfl
@[simp]
theorem renameEquiv_trans (e : σ ≃ τ) (f : τ ≃ α) :
(renameEquiv R e).trans (renameEquiv R f) = renameEquiv R (e.trans f) :=
AlgEquiv.ext (rename_rename e f)
end
section
variable (f : R →+* S) (k : σ → τ) (g : τ → S) (p : MvPolynomial σ R)
theorem eval₂_rename : (rename k p).eval₂ f g = p.eval₂ f (g ∘ k) := by
apply MvPolynomial.induction_on p <;>
· intros
simp [*]
theorem eval_rename (g : τ → R) (p : MvPolynomial σ R) : eval g (rename k p) = eval (g ∘ k) p :=
eval₂_rename _ _ _ _
theorem eval₂Hom_rename : eval₂Hom f g (rename k p) = eval₂Hom f (g ∘ k) p :=
eval₂_rename _ _ _ _
theorem aeval_rename [Algebra R S] : aeval g (rename k p) = aeval (g ∘ k) p :=
eval₂Hom_rename _ _ _ _
lemma aeval_comp_rename [Algebra R S] :
(aeval (R := R) g).comp (rename k) = MvPolynomial.aeval (g ∘ k) :=
AlgHom.ext fun p ↦ aeval_rename k g p
theorem rename_eval₂ (g : τ → MvPolynomial σ R) :
rename k (p.eval₂ C (g ∘ k)) = (rename k p).eval₂ C (rename k ∘ g) := by
apply MvPolynomial.induction_on p <;>
· intros
simp [*]
theorem rename_prod_mk_eval₂ (j : τ) (g : σ → MvPolynomial σ R) :
rename (Prod.mk j) (p.eval₂ C g) = p.eval₂ C fun x => rename (Prod.mk j) (g x) := by
apply MvPolynomial.induction_on p <;>
· intros
simp [*]
theorem eval₂_rename_prod_mk (g : σ × τ → S) (i : σ) (p : MvPolynomial τ R) :
(rename (Prod.mk i) p).eval₂ f g = eval₂ f (fun j => g (i, j)) p := by
apply MvPolynomial.induction_on p <;>
· intros
simp [*]
theorem eval_rename_prod_mk (g : σ × τ → R) (i : σ) (p : MvPolynomial τ R) :
eval g (rename (Prod.mk i) p) = eval (fun j => g (i, j)) p :=
eval₂_rename_prod_mk (RingHom.id _) _ _ _
end
/-- Every polynomial is a polynomial in finitely many variables. -/
theorem exists_finset_rename (p : MvPolynomial σ R) :
∃ (s : Finset σ) (q : MvPolynomial { x // x ∈ s } R), p = rename (↑) q := by
classical
apply induction_on p
· intro r
exact ⟨∅, C r, by rw [rename_C]⟩
· rintro p q ⟨s, p, rfl⟩ ⟨t, q, rfl⟩
refine ⟨s ∪ t, ⟨?_, ?_⟩⟩
· refine rename (Subtype.map id ?_) p + rename (Subtype.map id ?_) q <;>
simp +contextual only [id, true_or, or_true,
Finset.mem_union, forall_true_iff]
· simp only [rename_rename, map_add]
rfl
· rintro p n ⟨s, p, rfl⟩
refine ⟨insert n s, ⟨?_, ?_⟩⟩
· refine rename (Subtype.map id ?_) p * X ⟨n, s.mem_insert_self n⟩
simp +contextual only [id, or_true, Finset.mem_insert, forall_true_iff]
· simp only [rename_rename, rename_X, map_mul]
rfl
/-- `exists_finset_rename` for two polynomials at once: for any two polynomials `p₁`, `p₂` in a
polynomial semiring `R[σ]` of possibly infinitely many variables, `exists_finset_rename₂` yields
a finite subset `s` of `σ` such that both `p₁` and `p₂` are contained in the polynomial semiring
`R[s]` of finitely many variables. -/
theorem exists_finset_rename₂ (p₁ p₂ : MvPolynomial σ R) :
∃ (s : Finset σ) (q₁ q₂ : MvPolynomial s R), p₁ = rename (↑) q₁ ∧ p₂ = rename (↑) q₂ := by
obtain ⟨s₁, q₁, rfl⟩ := exists_finset_rename p₁
obtain ⟨s₂, q₂, rfl⟩ := exists_finset_rename p₂
classical
use s₁ ∪ s₂
use rename (Set.inclusion s₁.subset_union_left) q₁
use rename (Set.inclusion s₁.subset_union_right) q₂
constructor <;> simp [Function.comp_def]
/-- Every polynomial is a polynomial in finitely many variables. -/
theorem exists_fin_rename (p : MvPolynomial σ R) :
∃ (n : ℕ) (f : Fin n → σ) (_hf : Injective f) (q : MvPolynomial (Fin n) R), p = rename f q := by
obtain ⟨s, q, rfl⟩ := exists_finset_rename p
let n := Fintype.card { x // x ∈ s }
let e := Fintype.equivFin { x // x ∈ s }
refine ⟨n, (↑) ∘ e.symm, Subtype.val_injective.comp e.symm.injective, rename e q, ?_⟩
rw [← rename_rename, rename_rename e]
simp only [Function.comp_def, Equiv.symm_apply_apply, rename_rename]
end Rename
theorem eval₂_cast_comp (f : σ → τ) (c : ℤ →+* R) (g : τ → R) (p : MvPolynomial σ ℤ) :
eval₂ c (g ∘ f) p = eval₂ c g (rename f p) := (eval₂_rename c f g p).symm
section Coeff
@[simp]
theorem coeff_rename_mapDomain (f : σ → τ) (hf : Injective f) (φ : MvPolynomial σ R) (d : σ →₀ ℕ) :
(rename f φ).coeff (d.mapDomain f) = φ.coeff d := by
classical
apply φ.induction_on' (P := fun ψ => coeff (Finsupp.mapDomain f d) ((rename f) ψ) = coeff d ψ)
-- Lean could no longer infer the motive
· intro u r
rw [rename_monomial, coeff_monomial, coeff_monomial]
simp only [(Finsupp.mapDomain_injective hf).eq_iff]
· intros
simp only [*, map_add, coeff_add]
@[simp]
theorem coeff_rename_embDomain (f : σ ↪ τ) (φ : MvPolynomial σ R) (d : σ →₀ ℕ) :
(rename f φ).coeff (d.embDomain f) = φ.coeff d := by
rw [Finsupp.embDomain_eq_mapDomain f, coeff_rename_mapDomain f f.injective]
theorem coeff_rename_eq_zero (f : σ → τ) (φ : MvPolynomial σ R) (d : τ →₀ ℕ)
(h : ∀ u : σ →₀ ℕ, u.mapDomain f = d → φ.coeff u = 0) : (rename f φ).coeff d = 0 := by
classical
rw [rename_eq, ← notMem_support_iff]
intro H
replace H := mapDomain_support H
rw [Finset.mem_image] at H
obtain ⟨u, hu, rfl⟩ := H
specialize h u rfl
rw [Finsupp.mem_support_iff] at hu
contradiction
theorem coeff_rename_ne_zero (f : σ → τ) (φ : MvPolynomial σ R) (d : τ →₀ ℕ)
(h : (rename f φ).coeff d ≠ 0) : ∃ u : σ →₀ ℕ, u.mapDomain f = d ∧ φ.coeff u ≠ 0 := by
contrapose! h
apply coeff_rename_eq_zero _ _ _ h
@[simp]
theorem constantCoeff_rename {τ : Type*} (f : σ → τ) (φ : MvPolynomial σ R) :
constantCoeff (rename f φ) = constantCoeff φ := by
apply φ.induction_on
· intro a
simp only [constantCoeff_C, rename_C]
· intro p q hp hq
simp only [hp, hq, map_add]
· intro p n hp
simp only [hp, rename_X, constantCoeff_X, map_mul]
end Coeff
section Support
theorem support_rename_of_injective {p : MvPolynomial σ R} {f : σ → τ} [DecidableEq τ]
(h : Function.Injective f) :
(rename f p).support = Finset.image (Finsupp.mapDomain f) p.support := by
rw [rename_eq]
exact Finsupp.mapDomain_support_of_injective (Finsupp.mapDomain_injective h) _
end Support
end MvPolynomial |
.lake/packages/mathlib/Mathlib/Algebra/MvPolynomial/Eval.lean | import Mathlib.Algebra.MvPolynomial.Basic
/-!
# Multivariate polynomials
This file defines functions for evaluating multivariate polynomials.
These include generically evaluating a polynomial given a valuation of all its variables,
and more advanced evaluations that allow one to map the coefficients to different rings.
### Notation
In the definitions below, we use the following notation:
+ `σ : Type*` (indexing the variables)
+ `R : Type*` `[CommSemiring R]` (the coefficients)
+ `s : σ →₀ ℕ`, a function from `σ` to `ℕ` which is zero away from a finite set.
This will give rise to a monomial in `MvPolynomial σ R` which mathematicians might call `X^s`
+ `a : R`
+ `i : σ`, with corresponding monomial `X i`, often denoted `X_i` by mathematicians
+ `p : MvPolynomial σ R`
### Definitions
* `eval₂ (f : R → S₁) (g : σ → S₁) p` : given a semiring homomorphism from `R` to another
semiring `S₁`, and a map `σ → S₁`, evaluates `p` at this valuation, returning a term of type `S₁`.
Note that `eval₂` can be made using `eval` and `map` (see below), and it has been suggested
that sticking to `eval` and `map` might make the code less brittle.
* `eval (g : σ → R) p` : given a map `σ → R`, evaluates `p` at this valuation,
returning a term of type `R`
* `map (f : R → S₁) p` : returns the multivariate polynomial obtained from `p` by the change of
coefficient semiring corresponding to `f`
* `aeval (g : σ → S₁) p` : evaluates the multivariate polynomial obtained from `p` by the change
of coefficient semiring corresponding to `g` (`a` stands for `Algebra`)
-/
noncomputable section
open Set Function Finsupp AddMonoidAlgebra
open scoped Pointwise
universe u v w x
variable {R : Type u} {S₁ : Type v} {S₂ : Type w} {S₃ : Type x}
namespace MvPolynomial
variable {σ : Type*} {a a' a₁ a₂ : R} {e : ℕ} {n m : σ} {s : σ →₀ ℕ}
section CommSemiring
variable [CommSemiring R] [CommSemiring S₁] {p q : MvPolynomial σ R}
section Eval₂
variable (f : R →+* S₁) (g : σ → S₁)
/-- Evaluate a polynomial `p` given a valuation `g` of all the variables
and a ring hom `f` from the scalar ring to the target -/
def eval₂ (p : MvPolynomial σ R) : S₁ :=
p.sum fun s a => f a * s.prod fun n e => g n ^ e
theorem eval₂_eq (g : R →+* S₁) (X : σ → S₁) (f : MvPolynomial σ R) :
f.eval₂ g X = ∑ d ∈ f.support, g (f.coeff d) * ∏ i ∈ d.support, X i ^ d i :=
rfl
theorem eval₂_eq' [Fintype σ] (g : R →+* S₁) (X : σ → S₁) (f : MvPolynomial σ R) :
f.eval₂ g X = ∑ d ∈ f.support, g (f.coeff d) * ∏ i, X i ^ d i := by
simp only [eval₂_eq, ← Finsupp.prod_pow]
rfl
@[simp]
theorem eval₂_zero : (0 : MvPolynomial σ R).eval₂ f g = 0 :=
Finsupp.sum_zero_index
section
@[simp]
theorem eval₂_add : (p + q).eval₂ f g = p.eval₂ f g + q.eval₂ f g := by
classical exact Finsupp.sum_add_index (by simp [f.map_zero]) (by simp [add_mul, f.map_add])
@[simp]
theorem eval₂_monomial : (monomial s a).eval₂ f g = f a * s.prod fun n e => g n ^ e :=
Finsupp.sum_single_index (by simp [f.map_zero])
@[simp]
theorem eval₂_C (a) : (C a).eval₂ f g = f a := by
rw [C_apply, eval₂_monomial, prod_zero_index, mul_one]
@[simp]
theorem eval₂_one : (1 : MvPolynomial σ R).eval₂ f g = 1 :=
(eval₂_C _ _ _).trans f.map_one
@[simp] theorem eval₂_natCast (n : Nat) : (n : MvPolynomial σ R).eval₂ f g = n :=
(eval₂_C _ _ _).trans (map_natCast f n)
@[simp] theorem eval₂_ofNat (n : Nat) [n.AtLeastTwo] :
(ofNat(n) : MvPolynomial σ R).eval₂ f g = ofNat(n) :=
eval₂_natCast f g n
@[simp]
theorem eval₂_X (n) : (X n).eval₂ f g = g n := by
simp [eval₂_monomial, f.map_one, X, prod_single_index, pow_one]
theorem eval₂_mul_monomial :
∀ {s a}, (p * monomial s a).eval₂ f g = p.eval₂ f g * f a * s.prod fun n e => g n ^ e := by
classical
apply MvPolynomial.induction_on p
· intro a' s a
simp [C_mul_monomial, eval₂_monomial, f.map_mul]
· intro p q ih_p ih_q
simp [add_mul, eval₂_add, ih_p, ih_q]
· intro p n ih s a
exact
calc (p * X n * monomial s a).eval₂ f g
_ = (p * monomial (Finsupp.single n 1 + s) a).eval₂ f g := by
rw [monomial_single_add, pow_one, mul_assoc]
_ = (p * monomial (Finsupp.single n 1) 1).eval₂ f g * f a * s.prod fun n e => g n ^ e := by
simp [ih, prod_single_index, prod_add_index, pow_one, pow_add, mul_assoc, mul_left_comm,
f.map_one]
theorem eval₂_mul_C : (p * C a).eval₂ f g = p.eval₂ f g * f a :=
(eval₂_mul_monomial _ _).trans <| by simp
@[simp]
theorem eval₂_mul : ∀ {p}, (p * q).eval₂ f g = p.eval₂ f g * q.eval₂ f g := by
apply MvPolynomial.induction_on q
· simp [eval₂_C, eval₂_mul_C]
· simp +contextual [mul_add, eval₂_add]
· simp +contextual [X, eval₂_mul_monomial, ← mul_assoc]
@[simp]
theorem eval₂_pow {p : MvPolynomial σ R} : ∀ {n : ℕ}, (p ^ n).eval₂ f g = p.eval₂ f g ^ n
| 0 => by
rw [pow_zero, pow_zero]
exact eval₂_one _ _
| n + 1 => by rw [pow_add, pow_one, pow_add, pow_one, eval₂_mul, eval₂_pow]
/-- `MvPolynomial.eval₂` as a `RingHom`. -/
def eval₂Hom (f : R →+* S₁) (g : σ → S₁) : MvPolynomial σ R →+* S₁ where
toFun := eval₂ f g
map_one' := eval₂_one _ _
map_mul' _ _ := eval₂_mul _ _
map_zero' := eval₂_zero f g
map_add' _ _ := eval₂_add _ _
@[gcongr]
lemma eval₂_dvd (f : R →+* S₁) (g : σ → S₁) {p q : MvPolynomial σ R} (h : p ∣ q) :
p.eval₂ f g ∣ q.eval₂ f g :=
map_dvd (eval₂Hom f g) h
@[simp]
theorem coe_eval₂Hom (f : R →+* S₁) (g : σ → S₁) : ⇑(eval₂Hom f g) = eval₂ f g :=
rfl
theorem eval₂Hom_congr {f₁ f₂ : R →+* S₁} {g₁ g₂ : σ → S₁} {p₁ p₂ : MvPolynomial σ R} :
f₁ = f₂ → g₁ = g₂ → p₁ = p₂ → eval₂Hom f₁ g₁ p₁ = eval₂Hom f₂ g₂ p₂ := by
rintro rfl rfl rfl; rfl
end
@[simp]
theorem eval₂Hom_C (f : R →+* S₁) (g : σ → S₁) (r : R) : eval₂Hom f g (C r) = f r :=
eval₂_C f g r
@[simp]
theorem eval₂Hom_X' (f : R →+* S₁) (g : σ → S₁) (i : σ) : eval₂Hom f g (X i) = g i :=
eval₂_X f g i
@[simp]
theorem comp_eval₂Hom [CommSemiring S₂] (f : R →+* S₁) (g : σ → S₁) (φ : S₁ →+* S₂) :
φ.comp (eval₂Hom f g) = eval₂Hom (φ.comp f) fun i => φ (g i) := by
ext <;> simp
theorem map_eval₂Hom [CommSemiring S₂] (f : R →+* S₁) (g : σ → S₁) (φ : S₁ →+* S₂)
(p : MvPolynomial σ R) : φ (eval₂Hom f g p) = eval₂Hom (φ.comp f) (fun i => φ (g i)) p := by
rw [← comp_eval₂Hom]
rfl
theorem eval₂Hom_monomial (f : R →+* S₁) (g : σ → S₁) (d : σ →₀ ℕ) (r : R) :
eval₂Hom f g (monomial d r) = f r * d.prod fun i k => g i ^ k := by
simp only [monomial_eq, RingHom.map_mul, eval₂Hom_C, Finsupp.prod, map_prod,
RingHom.map_pow, eval₂Hom_X']
section
theorem eval₂_comp_left {S₂} [CommSemiring S₂] (k : S₁ →+* S₂) (f : R →+* S₁) (g : σ → S₁) (p) :
k (eval₂ f g p) = eval₂ (k.comp f) (k ∘ g) p := by
apply MvPolynomial.induction_on p <;>
simp +contextual [eval₂_add, k.map_add, eval₂_mul, k.map_mul]
end
@[simp]
theorem eval₂_eta (p : MvPolynomial σ R) : eval₂ C X p = p := by
apply MvPolynomial.induction_on p <;>
simp +contextual [eval₂_add, eval₂_mul]
theorem eval₂_congr (g₁ g₂ : σ → S₁)
(h : ∀ {i : σ} {c : σ →₀ ℕ}, i ∈ c.support → coeff c p ≠ 0 → g₁ i = g₂ i) :
p.eval₂ f g₁ = p.eval₂ f g₂ := by
apply Finset.sum_congr rfl
intro C hc; dsimp; congr 1
apply Finset.prod_congr rfl
intro i hi; dsimp; congr 1
apply h hi
rwa [Finsupp.mem_support_iff] at hc
@[simp] theorem eval₂_sum (s : Finset S₂) (p : S₂ → MvPolynomial σ R) :
eval₂ f g (∑ x ∈ s, p x) = ∑ x ∈ s, eval₂ f g (p x) :=
map_sum (eval₂Hom f g) _ s
@[simp] theorem eval₂_prod (s : Finset S₂) (p : S₂ → MvPolynomial σ R) :
eval₂ f g (∏ x ∈ s, p x) = ∏ x ∈ s, eval₂ f g (p x) :=
map_prod (eval₂Hom f g) _ s
theorem eval₂_assoc (q : S₂ → MvPolynomial σ R) (p : MvPolynomial S₂ R) :
eval₂ f (fun t => eval₂ f g (q t)) p = eval₂ f g (eval₂ C q p) := by
change _ = eval₂Hom f g (eval₂ C q p)
rw [eval₂_comp_left (eval₂Hom f g)]; congr with a; simp
end Eval₂
section Eval
variable {f : σ → R}
/-- Evaluate a polynomial `p` given a valuation `f` of all the variables -/
def eval (f : σ → R) : MvPolynomial σ R →+* R :=
eval₂Hom (RingHom.id _) f
theorem eval_eq (X : σ → R) (f : MvPolynomial σ R) :
eval X f = ∑ d ∈ f.support, f.coeff d * ∏ i ∈ d.support, X i ^ d i :=
rfl
theorem eval_eq' [Fintype σ] (X : σ → R) (f : MvPolynomial σ R) :
eval X f = ∑ d ∈ f.support, f.coeff d * ∏ i, X i ^ d i :=
eval₂_eq' (RingHom.id R) X f
theorem eval_monomial : eval f (monomial s a) = a * s.prod fun n e => f n ^ e :=
eval₂_monomial _ _
@[simp]
theorem eval_C : ∀ a, eval f (C a) = a :=
eval₂_C _ _
@[simp]
theorem eval_X : ∀ n, eval f (X n) = f n :=
eval₂_X _ _
@[simp] theorem eval_ofNat (n : Nat) [n.AtLeastTwo] :
(ofNat(n) : MvPolynomial σ R).eval f = ofNat(n) :=
map_ofNat _ n
@[simp]
theorem smul_eval (x) (p : MvPolynomial σ R) (s) : eval x (s • p) = s * eval x p := by
rw [smul_eq_C_mul, (eval x).map_mul, eval_C]
theorem eval_add : eval f (p + q) = eval f p + eval f q :=
eval₂_add _ _
theorem eval_mul : eval f (p * q) = eval f p * eval f q :=
eval₂_mul _ _
theorem eval_pow : ∀ n, eval f (p ^ n) = eval f p ^ n :=
fun _ => eval₂_pow _ _
theorem eval_sum {ι : Type*} (s : Finset ι) (f : ι → MvPolynomial σ R) (g : σ → R) :
eval g (∑ i ∈ s, f i) = ∑ i ∈ s, eval g (f i) :=
map_sum (eval g) _ _
theorem eval_prod {ι : Type*} (s : Finset ι) (f : ι → MvPolynomial σ R) (g : σ → R) :
eval g (∏ i ∈ s, f i) = ∏ i ∈ s, eval g (f i) :=
map_prod (eval g) _ _
theorem eval_assoc {τ} (f : σ → MvPolynomial τ R) (g : τ → R) (p : MvPolynomial σ R) :
eval (eval g ∘ f) p = eval g (eval₂ C f p) := by
rw [eval₂_comp_left (eval g)]
unfold eval; simp only [coe_eval₂Hom]
congr with a; simp
@[simp]
theorem eval₂_id {g : σ → R} (p : MvPolynomial σ R) : eval₂ (RingHom.id _) g p = eval g p :=
rfl
theorem eval_eval₂ {S τ : Type*} {x : τ → S} [CommSemiring S]
(f : R →+* MvPolynomial τ S) (g : σ → MvPolynomial τ S) (p : MvPolynomial σ R) :
eval x (eval₂ f g p) = eval₂ ((eval x).comp f) (fun s => eval x (g s)) p := by
apply induction_on p
· simp
· intro p q hp hq
simp [hp, hq]
· intro p n hp
simp [hp]
end Eval
section Map
variable (f : R →+* S₁)
/-- `map f p` maps a polynomial `p` across a ring hom `f` -/
def map : MvPolynomial σ R →+* MvPolynomial σ S₁ :=
eval₂Hom (C.comp f) X
@[simp]
theorem map_monomial (s : σ →₀ ℕ) (a : R) : map f (monomial s a) = monomial s (f a) :=
(eval₂_monomial _ _).trans monomial_eq.symm
@[simp]
theorem map_C : ∀ a : R, map f (C a : MvPolynomial σ R) = C (f a) :=
map_monomial _ _
@[simp] protected theorem map_ofNat (n : Nat) [n.AtLeastTwo] :
(ofNat(n) : MvPolynomial σ R).map f = ofNat(n) :=
_root_.map_ofNat _ _
@[simp]
theorem map_X : ∀ n : σ, map f (X n : MvPolynomial σ R) = X n :=
eval₂_X _ _
theorem map_id : ∀ p : MvPolynomial σ R, map (RingHom.id R) p = p :=
eval₂_eta
theorem map_map [CommSemiring S₂] (g : S₁ →+* S₂) (p : MvPolynomial σ R) :
map g (map f p) = map (g.comp f) p :=
(eval₂_comp_left (map g) (C.comp f) X p).trans <| by
congr
· ext1 a
simp only [map_C, comp_apply, RingHom.coe_comp]
· ext1 n
simp only [map_X, comp_apply]
theorem eval₂_eq_eval_map (g : σ → S₁) (p : MvPolynomial σ R) : p.eval₂ f g = eval g (map f p) := by
unfold map eval; simp only [coe_eval₂Hom]
have h := eval₂_comp_left (eval₂Hom (RingHom.id S₁) g) (C.comp f) X p
dsimp only [coe_eval₂Hom] at h
rw [h]
congr
· ext1 a
simp only [coe_eval₂Hom, RingHom.id_apply, comp_apply, eval₂_C, RingHom.coe_comp]
· ext1 n
simp only [comp_apply, eval₂_X]
theorem eval₂_comp_right {S₂} [CommSemiring S₂] (k : S₁ →+* S₂) (f : R →+* S₁) (g : σ → S₁) (p) :
k (eval₂ f g p) = eval₂ k (k ∘ g) (map f p) := by
apply MvPolynomial.induction_on p
· intro r
rw [eval₂_C, map_C, eval₂_C]
· intro p q hp hq
rw [eval₂_add, k.map_add, (map f).map_add, eval₂_add, hp, hq]
· intro p s hp
rw [eval₂_mul, k.map_mul, (map f).map_mul, eval₂_mul, map_X, hp, eval₂_X, eval₂_X, comp_apply]
theorem map_eval₂ (f : R →+* S₁) (g : S₂ → MvPolynomial S₃ R) (p : MvPolynomial S₂ R) :
map f (eval₂ C g p) = eval₂ C (map f ∘ g) (map f p) := by
apply MvPolynomial.induction_on p
· intro r
rw [eval₂_C, map_C, map_C, eval₂_C]
· intro p q hp hq
rw [eval₂_add, (map f).map_add, hp, hq, (map f).map_add, eval₂_add]
· intro p s hp
rw [eval₂_mul, (map f).map_mul, hp, (map f).map_mul, map_X, eval₂_mul, eval₂_X, eval₂_X,
comp_apply]
theorem coeff_map (p : MvPolynomial σ R) : ∀ m : σ →₀ ℕ, coeff m (map f p) = f (coeff m p) := by
classical
apply MvPolynomial.induction_on p <;> clear p
· intro r m
simp_rw [map_C, coeff_C, apply_ite f, f.map_zero]
· intro p q hp hq m
simp only [hp, hq, (map f).map_add, coeff_add, f.map_add]
· intro p i hp m
simp only [(map f).map_mul, map_X, hp, coeff_mul_X', f.map_zero, apply_ite f]
theorem map_injective (hf : Function.Injective f) :
Function.Injective (map f : MvPolynomial σ R → MvPolynomial σ S₁) := by
intro p q h
simp only [MvPolynomial.ext_iff, coeff_map] at h ⊢
intro m
exact hf (h m)
theorem map_injective_iff : Function.Injective (map (σ := σ) f) ↔ Function.Injective f :=
⟨fun h r r' eq ↦ by simpa using h (a₁ := C r) (a₂ := C r') (by simpa), map_injective f⟩
theorem map_surjective (hf : Function.Surjective f) :
Function.Surjective (map f : MvPolynomial σ R → MvPolynomial σ S₁) := fun p => by
induction p using MvPolynomial.induction_on' with
| monomial i fr =>
obtain ⟨r, rfl⟩ := hf fr
exact ⟨monomial i r, map_monomial _ _ _⟩
| add a b ha hb =>
obtain ⟨a, rfl⟩ := ha
obtain ⟨b, rfl⟩ := hb
exact ⟨a + b, RingHom.map_add _ _ _⟩
theorem map_surjective_iff : Function.Surjective (map (σ := σ) f) ↔ Function.Surjective f :=
⟨fun h s ↦ let ⟨p, h⟩ := h (C s); ⟨p.coeff 0, by simpa [coeff_map] using congr(coeff 0 $h)⟩,
map_surjective f⟩
/-- If `f` is a left-inverse of `g` then `map f` is a left-inverse of `map g`. -/
theorem map_leftInverse {f : R →+* S₁} {g : S₁ →+* R} (hf : Function.LeftInverse f g) :
Function.LeftInverse (map f : MvPolynomial σ R → MvPolynomial σ S₁) (map g) := fun X => by
rw [map_map, (RingHom.ext hf : f.comp g = RingHom.id _), map_id]
/-- If `f` is a right-inverse of `g` then `map f` is a right-inverse of `map g`. -/
theorem map_rightInverse {f : R →+* S₁} {g : S₁ →+* R} (hf : Function.RightInverse f g) :
Function.RightInverse (map f : MvPolynomial σ R → MvPolynomial σ S₁) (map g) :=
(map_leftInverse hf.leftInverse).rightInverse
@[simp]
theorem eval_map (f : R →+* S₁) (g : σ → S₁) (p : MvPolynomial σ R) :
eval g (map f p) = eval₂ f g p := by
apply MvPolynomial.induction_on p <;> · simp +contextual
theorem eval₂_comp (f : R →+* S₁) (g : σ → R) (p : MvPolynomial σ R) :
f (eval g p) = eval₂ f (f ∘ g) p := by
rw [← p.map_id, eval_map, eval₂_comp_right]
@[simp]
theorem eval₂_map [CommSemiring S₂] (f : R →+* S₁) (g : σ → S₂) (φ : S₁ →+* S₂)
(p : MvPolynomial σ R) : eval₂ φ g (map f p) = eval₂ (φ.comp f) g p := by
rw [← eval_map, ← eval_map, map_map]
@[simp]
theorem eval₂Hom_map_hom [CommSemiring S₂] (f : R →+* S₁) (g : σ → S₂) (φ : S₁ →+* S₂)
(p : MvPolynomial σ R) : eval₂Hom φ g (map f p) = eval₂Hom (φ.comp f) g p :=
eval₂_map f g φ p
@[simp]
theorem constantCoeff_map (f : R →+* S₁) (φ : MvPolynomial σ R) :
constantCoeff (MvPolynomial.map f φ) = f (constantCoeff φ) :=
coeff_map f φ 0
theorem constantCoeff_comp_map (f : R →+* S₁) :
(constantCoeff : MvPolynomial σ S₁ →+* S₁).comp (MvPolynomial.map f) = f.comp constantCoeff :=
by ext <;> simp
theorem support_map_subset (p : MvPolynomial σ R) : (map f p).support ⊆ p.support := by
simp only [Finset.subset_iff, mem_support_iff]
intro x hx
contrapose! hx
rw [coeff_map, hx, RingHom.map_zero]
theorem support_map_of_injective (p : MvPolynomial σ R) {f : R →+* S₁} (hf : Injective f) :
(map f p).support = p.support := by
apply Finset.Subset.antisymm
· exact MvPolynomial.support_map_subset _ _
simp only [Finset.subset_iff, mem_support_iff]
intro x hx
contrapose! hx
rw [coeff_map, ← f.map_zero] at hx
exact hf hx
theorem C_dvd_iff_map_hom_eq_zero (q : R →+* S₁) (r : R) (hr : ∀ r' : R, q r' = 0 ↔ r ∣ r')
(φ : MvPolynomial σ R) : C r ∣ φ ↔ map q φ = 0 := by
rw [C_dvd_iff_dvd_coeff, MvPolynomial.ext_iff]
simp only [coeff_map, coeff_zero, hr]
theorem map_mapRange_eq_iff (f : R →+* S₁) (g : S₁ → R) (hg : g 0 = 0) (φ : MvPolynomial σ S₁) :
map f (Finsupp.mapRange g hg φ) = φ ↔ ∀ d, f (g (coeff d φ)) = coeff d φ := by
simp_rw [MvPolynomial.ext_iff, coeff_map, coeff_mapRange]
lemma coeffs_map (f : R →+* S₁) (p : MvPolynomial σ R) [DecidableEq S₁] :
(map f p).coeffs ⊆ p.coeffs.image f := by
classical
induction p using induction_on'' with
| C a => aesop (add simp coeffs_C)
| mul_X p n ih => simpa
| monomial_add a s p ha hs hp ih =>
rw [coeffs_add (disjoint_support_monomial ha hs), map_add, coeffs_add]
· rw [Finset.image_union, Finset.union_subset_iff]
exact ⟨ih.trans (by simp), hp.trans (by simp)⟩
· exact Finset.disjoint_of_subset_left (support_map_subset _ _) <|
Finset.disjoint_of_subset_right (support_map_subset _ _) <|
disjoint_support_monomial ha hs
@[simp]
lemma coe_coeffs_map (f : R →+* S₁) (p : MvPolynomial σ R) :
((map f p).coeffs : Set S₁) ⊆ f '' p.coeffs := by
classical
exact mod_cast coeffs_map f p
lemma mem_range_map_iff_coeffs_subset {f : R →+* S₁} {x : MvPolynomial σ S₁} :
x ∈ Set.range (MvPolynomial.map f) ↔ (x.coeffs : Set _) ⊆ .range f := by
classical
refine ⟨fun hx ↦ ?_, fun hx ↦ ?_⟩
· obtain ⟨p, rfl⟩ := hx
exact subset_trans (coe_coeffs_map f p) (by simp)
· induction x using induction_on'' with
| C a =>
by_cases h : a = 0
· subst h
exact ⟨0, by simp⟩
· simp only [coeffs_C, h, reduceIte, Finset.coe_singleton, Set.singleton_subset_iff] at hx
obtain ⟨b, rfl⟩ := hx
exact ⟨C b, by simp⟩
| mul_X p n ih =>
rw [coeffs_mul_X] at hx
obtain ⟨q, rfl⟩ := ih hx
exact ⟨q * X n, by simp⟩
| monomial_add a s p ha hs hp ih =>
rw [coeffs_add (disjoint_support_monomial ha hs)] at hx
simp only [Finset.coe_union, Set.union_subset_iff] at hx
obtain ⟨q, hq⟩ := ih hx.1
obtain ⟨u, hu⟩ := hp hx.2
exact ⟨q + u, by simp [hq, hu]⟩
/-- If `f : S₁ →ₐ[R] S₂` is a morphism of `R`-algebras, then so is `MvPolynomial.map f`. -/
@[simps!]
def mapAlgHom [CommSemiring S₂] [Algebra R S₁] [Algebra R S₂] (f : S₁ →ₐ[R] S₂) :
MvPolynomial σ S₁ →ₐ[R] MvPolynomial σ S₂ :=
{ map (↑f : S₁ →+* S₂) with
commutes' r := by simp }
@[simp]
theorem mapAlgHom_id [Algebra R S₁] :
mapAlgHom (AlgHom.id R S₁) = AlgHom.id R (MvPolynomial σ S₁) :=
AlgHom.ext map_id
@[simp]
theorem mapAlgHom_coe_ringHom [CommSemiring S₂] [Algebra R S₁] [Algebra R S₂] (f : S₁ →ₐ[R] S₂) :
↑(mapAlgHom f : _ →ₐ[R] MvPolynomial σ S₂) =
(map ↑f : MvPolynomial σ S₁ →+* MvPolynomial σ S₂) :=
RingHom.mk_coe _ _ _ _ _
lemma range_mapAlgHom [CommSemiring S₂] [Algebra R S₁] [Algebra R S₂] (f : S₁ →ₐ[R] S₂) :
(mapAlgHom f).range.toSubmodule = coeffsIn σ f.range.toSubmodule := by
ext
rw [Subalgebra.mem_toSubmodule, ← SetLike.mem_coe, AlgHom.coe_range, mapAlgHom, AlgHom.coe_mk,
mem_range_map_iff_coeffs_subset, mem_coeffsIn_iff_coeffs_subset]
simp [Set.subset_def]
end Map
section Aeval
/-! ### The algebra of multivariate polynomials -/
variable [Algebra R S₁] [CommSemiring S₂]
variable (f : σ → S₁)
/-- A map `σ → S₁` where `S₁` is an algebra over `R` generates an `R`-algebra homomorphism
from multivariate polynomials over `σ` to `S₁`. -/
def aeval : MvPolynomial σ R →ₐ[R] S₁ :=
{ eval₂Hom (algebraMap R S₁) f with commutes' := fun _r => eval₂_C _ _ _ }
theorem aeval_def (p : MvPolynomial σ R) : aeval f p = eval₂ (algebraMap R S₁) f p :=
rfl
theorem aeval_eq_eval₂Hom (p : MvPolynomial σ R) : aeval f p = eval₂Hom (algebraMap R S₁) f p :=
rfl
@[simp]
lemma coe_aeval_eq_eval :
RingHomClass.toRingHom (aeval f : MvPolynomial σ S₁ →ₐ[S₁] S₁) = eval f :=
rfl
@[simp]
lemma aeval_eq_eval : (aeval f : MvPolynomial σ S₁ → S₁) = eval f := rfl
@[simp]
theorem aeval_X (s : σ) : aeval f (X s : MvPolynomial σ R) = f s :=
eval₂_X _ _ _
theorem aeval_C (r : R) : aeval f (C r) = algebraMap R S₁ r :=
eval₂_C _ _ _
@[simp] theorem aeval_ofNat (n : Nat) [n.AtLeastTwo] :
aeval f (ofNat(n) : MvPolynomial σ R) = ofNat(n) :=
map_ofNat _ _
theorem aeval_unique (φ : MvPolynomial σ R →ₐ[R] S₁) : φ = aeval (φ ∘ X) := by
ext i
simp
theorem aeval_X_left : aeval X = AlgHom.id R (MvPolynomial σ R) :=
(aeval_unique (AlgHom.id R _)).symm
theorem aeval_X_left_apply (p : MvPolynomial σ R) : aeval X p = p :=
AlgHom.congr_fun aeval_X_left p
theorem comp_aeval {B : Type*} [CommSemiring B] [Algebra R B] (φ : S₁ →ₐ[R] B) :
φ.comp (aeval f) = aeval fun i => φ (f i) := by
ext i
simp
lemma comp_aeval_apply {B : Type*} [CommSemiring B] [Algebra R B] (φ : S₁ →ₐ[R] B)
(p : MvPolynomial σ R) :
φ (aeval f p) = aeval (fun i ↦ φ (f i)) p := by
rw [← comp_aeval, AlgHom.coe_comp, comp_apply]
@[simp]
theorem map_aeval {B : Type*} [CommSemiring B] (g : σ → S₁) (φ : S₁ →+* B) (p : MvPolynomial σ R) :
φ (aeval g p) = eval₂Hom (φ.comp (algebraMap R S₁)) (fun i => φ (g i)) p := by
rw [← comp_eval₂Hom]
rfl
@[simp]
theorem eval₂Hom_zero (f : R →+* S₂) : eval₂Hom f (0 : σ → S₂) = f.comp constantCoeff := by
ext <;> simp
@[simp]
theorem eval₂Hom_zero' (f : R →+* S₂) : eval₂Hom f (fun _ => 0 : σ → S₂) = f.comp constantCoeff :=
eval₂Hom_zero f
theorem eval₂Hom_zero_apply (f : R →+* S₂) (p : MvPolynomial σ R) :
eval₂Hom f (0 : σ → S₂) p = f (constantCoeff p) :=
RingHom.congr_fun (eval₂Hom_zero f) p
theorem eval₂Hom_zero'_apply (f : R →+* S₂) (p : MvPolynomial σ R) :
eval₂Hom f (fun _ => 0 : σ → S₂) p = f (constantCoeff p) :=
eval₂Hom_zero_apply f p
@[simp]
theorem eval₂_zero_apply (f : R →+* S₂) (p : MvPolynomial σ R) :
eval₂ f (0 : σ → S₂) p = f (constantCoeff p) :=
eval₂Hom_zero_apply _ _
@[simp]
theorem eval₂_zero'_apply (f : R →+* S₂) (p : MvPolynomial σ R) :
eval₂ f (fun _ => 0 : σ → S₂) p = f (constantCoeff p) :=
eval₂_zero_apply f p
@[simp]
theorem aeval_zero (p : MvPolynomial σ R) :
aeval (0 : σ → S₁) p = algebraMap _ _ (constantCoeff p) :=
eval₂Hom_zero_apply (algebraMap R S₁) p
@[simp]
theorem aeval_zero' (p : MvPolynomial σ R) :
aeval (fun _ => 0 : σ → S₁) p = algebraMap _ _ (constantCoeff p) :=
aeval_zero p
@[simp]
theorem eval_zero : eval (0 : σ → R) = constantCoeff :=
eval₂Hom_zero _
@[simp]
theorem eval_zero' : eval (fun _ => 0 : σ → R) = constantCoeff :=
eval₂Hom_zero _
theorem aeval_monomial (g : σ → S₁) (d : σ →₀ ℕ) (r : R) :
aeval g (monomial d r) = algebraMap _ _ r * d.prod fun i k => g i ^ k :=
eval₂Hom_monomial _ _ _ _
theorem eval₂Hom_eq_zero (f : R →+* S₂) (g : σ → S₂) (φ : MvPolynomial σ R)
(h : ∀ d, φ.coeff d ≠ 0 → ∃ i ∈ d.support, g i = 0) : eval₂Hom f g φ = 0 := by
rw [φ.as_sum, map_sum]
refine Finset.sum_eq_zero fun d hd => ?_
obtain ⟨i, hi, hgi⟩ : ∃ i ∈ d.support, g i = 0 := h d (Finsupp.mem_support_iff.mp hd)
rw [eval₂Hom_monomial, Finsupp.prod, Finset.prod_eq_zero hi, mul_zero]
rw [hgi, zero_pow]
rwa [← Finsupp.mem_support_iff]
theorem aeval_eq_zero [Algebra R S₂] (f : σ → S₂) (φ : MvPolynomial σ R)
(h : ∀ d, φ.coeff d ≠ 0 → ∃ i ∈ d.support, f i = 0) : aeval f φ = 0 :=
eval₂Hom_eq_zero _ _ _ h
theorem aeval_sum {ι : Type*} (s : Finset ι) (φ : ι → MvPolynomial σ R) :
aeval f (∑ i ∈ s, φ i) = ∑ i ∈ s, aeval f (φ i) :=
map_sum (MvPolynomial.aeval f) _ _
theorem aeval_prod {ι : Type*} (s : Finset ι) (φ : ι → MvPolynomial σ R) :
aeval f (∏ i ∈ s, φ i) = ∏ i ∈ s, aeval f (φ i) :=
map_prod (MvPolynomial.aeval f) _ _
variable (R)
theorem _root_.Algebra.adjoin_range_eq_range_aeval :
Algebra.adjoin R (Set.range f) = (MvPolynomial.aeval f).range := by
simp only [← Algebra.map_top, ← MvPolynomial.adjoin_range_X, AlgHom.map_adjoin, ← Set.range_comp,
Function.comp_def, MvPolynomial.aeval_X]
theorem _root_.Algebra.adjoin_eq_range (s : Set S₁) :
Algebra.adjoin R s = (MvPolynomial.aeval ((↑) : s → S₁)).range := by
rw [← Algebra.adjoin_range_eq_range_aeval, Subtype.range_coe]
end Aeval
section AevalTower
variable {S A B : Type*} [CommSemiring S] [CommSemiring A] [CommSemiring B]
variable [Algebra S R] [Algebra S A] [Algebra S B]
/-- Version of `aeval` for defining algebra homs out of `MvPolynomial σ R` over a smaller base ring
than `R`. -/
def aevalTower (f : R →ₐ[S] A) (X : σ → A) : MvPolynomial σ R →ₐ[S] A :=
{ eval₂Hom (↑f) X with
commutes' := fun r => by
simp [IsScalarTower.algebraMap_eq S R (MvPolynomial σ R), algebraMap_eq] }
variable (g : R →ₐ[S] A) (y : σ → A)
@[simp]
theorem aevalTower_X (i : σ) : aevalTower g y (X i) = y i :=
eval₂_X _ _ _
@[simp]
theorem aevalTower_C (x : R) : aevalTower g y (C x) = g x :=
eval₂_C _ _ _
@[simp]
theorem aevalTower_ofNat (n : Nat) [n.AtLeastTwo] :
aevalTower g y (ofNat(n) : MvPolynomial σ R) = ofNat(n) :=
_root_.map_ofNat _ _
@[simp]
theorem aevalTower_comp_C : (aevalTower g y : MvPolynomial σ R →+* A).comp C = g :=
RingHom.ext <| aevalTower_C _ _
theorem aevalTower_algebraMap (x : R) : aevalTower g y (algebraMap R (MvPolynomial σ R) x) = g x :=
eval₂_C _ _ _
theorem aevalTower_comp_algebraMap :
(aevalTower g y : MvPolynomial σ R →+* A).comp (algebraMap R (MvPolynomial σ R)) = g :=
aevalTower_comp_C _ _
theorem aevalTower_toAlgHom (x : R) :
aevalTower g y (IsScalarTower.toAlgHom S R (MvPolynomial σ R) x) = g x :=
aevalTower_algebraMap _ _ _
@[simp]
theorem aevalTower_comp_toAlgHom :
(aevalTower g y).comp (IsScalarTower.toAlgHom S R (MvPolynomial σ R)) = g :=
AlgHom.coe_ringHom_injective <| aevalTower_comp_algebraMap _ _
@[simp]
theorem aevalTower_id :
aevalTower (AlgHom.id S S) = (aeval : (σ → S) → MvPolynomial σ S →ₐ[S] S) := by
ext
simp only [aevalTower_X, aeval_X]
@[simp]
theorem aevalTower_ofId :
aevalTower (Algebra.ofId S A) = (aeval : (σ → A) → MvPolynomial σ S →ₐ[S] A) := by
ext
simp only [aeval_X, aevalTower_X]
end AevalTower
section EvalMem
variable {S subS : Type*} [CommSemiring S] [SetLike subS S] [SubsemiringClass subS S]
theorem eval₂_mem {f : R →+* S} {p : MvPolynomial σ R} {s : subS}
(hs : ∀ i ∈ p.support, f (p.coeff i) ∈ s) {v : σ → S} (hv : ∀ i, v i ∈ s) :
MvPolynomial.eval₂ f v p ∈ s := by
classical
replace hs : ∀ i, f (p.coeff i) ∈ s := by
intro i
by_cases hi : i ∈ p.support
· exact hs i hi
· rw [MvPolynomial.notMem_support_iff.1 hi, f.map_zero]
exact zero_mem s
induction p using MvPolynomial.monomial_add_induction_on with
| C a =>
simpa using hs 0
| monomial_add a b f ha _ ih =>
rw [eval₂_add, eval₂_monomial]
refine add_mem (mul_mem ?_ <| prod_mem fun i _ => pow_mem (hv _) _) (ih fun i => ?_)
· simpa [MvPolynomial.notMem_support_iff.1 ha] using hs a
have := hs i
rw [coeff_add, coeff_monomial] at this
split_ifs at this with h
· subst h
rw [MvPolynomial.notMem_support_iff.1 ha, map_zero]
exact zero_mem _
· rwa [zero_add] at this
theorem eval_mem {p : MvPolynomial σ S} {s : subS} (hs : ∀ i ∈ p.support, p.coeff i ∈ s) {v : σ → S}
(hv : ∀ i, v i ∈ s) : MvPolynomial.eval v p ∈ s :=
eval₂_mem hs hv
end EvalMem
variable {S T : Type*} [CommSemiring S] [Algebra R S] [CommSemiring T] [Algebra R T] [Algebra S T]
[IsScalarTower R S T]
lemma aeval_sumElim {σ τ : Type*} (p : MvPolynomial (σ ⊕ τ) R) (f : τ → S) (g : σ → T) :
(aeval (Sum.elim g (algebraMap S T ∘ f))) p =
(aeval g) ((aeval (Sum.elim X (C ∘ f))) p) := by
induction p using MvPolynomial.induction_on with
| C r => simp [← IsScalarTower.algebraMap_apply]
| add p q hp hq => simp [hp, hq]
| mul_X p i h => cases i <;> simp [h]
end CommSemiring
section Algebra
variable {R S σ : Type*} [CommSemiring R] [CommSemiring S] [Algebra R S]
/--
If `S` is an `R`-algebra, then `MvPolynomial σ S` is a `MvPolynomial σ R` algebra.
Warning: This produces a diamond for
`Algebra (MvPolynomial σ R) (MvPolynomial σ (MvPolynomial σ S))`. That's why it is not a
global instance.
-/
noncomputable def algebraMvPolynomial : Algebra (MvPolynomial σ R) (MvPolynomial σ S) :=
(MvPolynomial.map (algebraMap R S)).toAlgebra
attribute [local instance] algebraMvPolynomial
@[simp]
lemma algebraMap_def :
algebraMap (MvPolynomial σ R) (MvPolynomial σ S) = MvPolynomial.map (algebraMap R S) :=
rfl
instance : IsScalarTower R (MvPolynomial σ R) (MvPolynomial σ S) :=
IsScalarTower.of_algebraMap_eq' (by ext; simp)
instance [FaithfulSMul R S] : FaithfulSMul (MvPolynomial σ R) (MvPolynomial σ S) :=
(faithfulSMul_iff_algebraMap_injective ..).mpr
(map_injective _ <| FaithfulSMul.algebraMap_injective ..)
end Algebra
end MvPolynomial |
.lake/packages/mathlib/Mathlib/Algebra/MvPolynomial/Degrees.lean | import Mathlib.Algebra.MonoidAlgebra.Degree
import Mathlib.Algebra.MvPolynomial.Rename
/-!
# Degrees of polynomials
This file establishes many results about the degree of a multivariate polynomial.
The *degree set* of a polynomial $P \in R[X]$ is a `Multiset` containing, for each $x$ in the
variable set, $n$ copies of $x$, where $n$ is the maximum number of copies of $x$ appearing in a
monomial of $P$.
## Main declarations
* `MvPolynomial.degrees p` : the multiset of variables representing the union of the multisets
corresponding to each non-zero monomial in `p`.
For example if `7 ≠ 0` in `R` and `p = x²y+7y³` then `degrees p = {x, x, y, y, y}`
* `MvPolynomial.degreeOf n p : ℕ` : the total degree of `p` with respect to the variable `n`.
For example if `p = x⁴y+yz` then `degreeOf y p = 1`.
* `MvPolynomial.totalDegree p : ℕ` :
the max of the sizes of the multisets `s` whose monomials `X^s` occur in `p`.
For example if `p = x⁴y+yz` then `totalDegree p = 5`.
## Notation
As in other polynomial files, we typically use the notation:
+ `σ τ : Type*` (indexing the variables)
+ `R : Type*` `[CommSemiring R]` (the coefficients)
+ `s : σ →₀ ℕ`, a function from `σ` to `ℕ` which is zero away from a finite set.
This will give rise to a monomial in `MvPolynomial σ R` which mathematicians might call `X^s`
+ `r : R`
+ `i : σ`, with corresponding monomial `X i`, often denoted `X_i` by mathematicians
+ `p : MvPolynomial σ R`
-/
noncomputable section
open Set Function Finsupp AddMonoidAlgebra
universe u v w
variable {R : Type u} {S : Type v}
namespace MvPolynomial
variable {σ τ : Type*} {r : R} {e : ℕ} {n m : σ} {s : σ →₀ ℕ}
section CommSemiring
variable [CommSemiring R] {p q : MvPolynomial σ R}
section Degrees
/-! ### `degrees` -/
/-- The maximal degrees of each variable in a multi-variable polynomial, expressed as a multiset.
(For example, `degrees (x^2 * y + y^3)` would be `{x, x, y, y, y}`.)
-/
def degrees (p : MvPolynomial σ R) : Multiset σ :=
letI := Classical.decEq σ
p.support.sup fun s : σ →₀ ℕ => toMultiset s
theorem degrees_def [DecidableEq σ] (p : MvPolynomial σ R) :
p.degrees = p.support.sup fun s : σ →₀ ℕ => Finsupp.toMultiset s := by rw [degrees]; convert rfl
theorem degrees_monomial (s : σ →₀ ℕ) (a : R) : degrees (monomial s a) ≤ toMultiset s := by
classical
refine (supDegree_single s a).trans_le ?_
split_ifs
exacts [bot_le, le_rfl]
theorem degrees_monomial_eq (s : σ →₀ ℕ) (a : R) (ha : a ≠ 0) :
degrees (monomial s a) = toMultiset s := by
classical
exact (supDegree_single s a).trans (if_neg ha)
theorem degrees_C (a : R) : degrees (C a : MvPolynomial σ R) = 0 :=
Multiset.le_zero.1 <| degrees_monomial _ _
theorem degrees_X' (n : σ) : degrees (X n : MvPolynomial σ R) ≤ {n} :=
le_trans (degrees_monomial _ _) <| le_of_eq <| toMultiset_single _ _
@[simp]
theorem degrees_X [Nontrivial R] (n : σ) : degrees (X n : MvPolynomial σ R) = {n} :=
(degrees_monomial_eq _ (1 : R) one_ne_zero).trans (toMultiset_single _ _)
@[simp]
theorem degrees_zero : degrees (0 : MvPolynomial σ R) = 0 := by
rw [← C_0]
exact degrees_C 0
@[simp]
theorem degrees_one : degrees (1 : MvPolynomial σ R) = 0 :=
degrees_C 1
theorem degrees_add_le [DecidableEq σ] {p q : MvPolynomial σ R} :
(p + q).degrees ≤ p.degrees ⊔ q.degrees := by
simp_rw [degrees_def]; exact supDegree_add_le
theorem degrees_sum_le {ι : Type*} [DecidableEq σ] (s : Finset ι) (f : ι → MvPolynomial σ R) :
(∑ i ∈ s, f i).degrees ≤ s.sup fun i => (f i).degrees := by
simp_rw [degrees_def]; exact supDegree_sum_le
theorem degrees_mul_le {p q : MvPolynomial σ R} : (p * q).degrees ≤ p.degrees + q.degrees := by
classical
simp_rw [degrees_def]
exact supDegree_mul_le (map_add _)
theorem degrees_prod_le {ι : Type*} {s : Finset ι} {f : ι → MvPolynomial σ R} :
(∏ i ∈ s, f i).degrees ≤ ∑ i ∈ s, (f i).degrees := by
classical exact supDegree_prod_le (map_zero _) (map_add _)
theorem degrees_pow_le {p : MvPolynomial σ R} {n : ℕ} : (p ^ n).degrees ≤ n • p.degrees := by
simpa using degrees_prod_le (s := .range n) (f := fun _ ↦ p)
theorem mem_degrees {p : MvPolynomial σ R} {i : σ} :
i ∈ p.degrees ↔ ∃ d, p.coeff d ≠ 0 ∧ i ∈ d.support := by
classical
simp only [degrees_def, Multiset.mem_sup, ← mem_support_iff, Finsupp.mem_toMultiset]
theorem le_degrees_add_left (h : Disjoint p.degrees q.degrees) : p.degrees ≤ (p + q).degrees := by
classical
apply Finset.sup_le
intro d hd
rw [Multiset.disjoint_iff_ne] at h
obtain rfl | h0 := eq_or_ne d 0
· rw [toMultiset_zero]; apply Multiset.zero_le
· refine Finset.le_sup_of_le (b := d) ?_ le_rfl
rw [mem_support_iff, coeff_add]
suffices q.coeff d = 0 by rwa [this, add_zero, coeff, ← Finsupp.mem_support_iff]
rw [Ne, ← Finsupp.support_eq_empty, ← Ne, ← Finset.nonempty_iff_ne_empty] at h0
obtain ⟨j, hj⟩ := h0
contrapose! h
rw [mem_support_iff] at hd
refine ⟨j, ?_, j, ?_, rfl⟩
all_goals rw [mem_degrees]; refine ⟨d, ?_, hj⟩; assumption
lemma le_degrees_add_right (h : Disjoint p.degrees q.degrees) : q.degrees ≤ (p + q).degrees := by
simpa [add_comm] using le_degrees_add_left h.symm
theorem degrees_add_of_disjoint [DecidableEq σ] (h : Disjoint p.degrees q.degrees) :
(p + q).degrees = p.degrees ∪ q.degrees :=
degrees_add_le.antisymm <| Multiset.union_le (le_degrees_add_left h) (le_degrees_add_right h)
lemma degrees_map_le [CommSemiring S] {f : R →+* S} : (map f p).degrees ≤ p.degrees := by
classical exact Finset.sup_mono <| support_map_subset ..
theorem degrees_rename (f : σ → τ) (φ : MvPolynomial σ R) :
(rename f φ).degrees ⊆ φ.degrees.map f := by
classical
intro i
rw [mem_degrees, Multiset.mem_map]
rintro ⟨d, hd, hi⟩
obtain ⟨x, rfl, hx⟩ := coeff_rename_ne_zero _ _ _ hd
simp only [Finsupp.mapDomain, Finsupp.mem_support_iff] at hi
rw [sum_apply, Finsupp.sum] at hi
contrapose! hi
rw [Finset.sum_eq_zero]
intro j hj
simp only [mem_degrees] at hi
specialize hi j ⟨x, hx, hj⟩
rw [Finsupp.single_apply, if_neg hi]
theorem degrees_map_of_injective [CommSemiring S] (p : MvPolynomial σ R) {f : R →+* S}
(hf : Injective f) : (map f p).degrees = p.degrees := by
simp only [degrees, MvPolynomial.support_map_of_injective _ hf]
theorem degrees_rename_of_injective {p : MvPolynomial σ R} {f : σ → τ} (h : Function.Injective f) :
degrees (rename f p) = (degrees p).map f := by
classical
simp only [degrees, Multiset.map_finset_sup p.support Finsupp.toMultiset f h,
support_rename_of_injective h, Finset.sup_image]
refine Finset.sup_congr rfl fun x _ => ?_
exact (Finsupp.toMultiset_map _ _).symm
end Degrees
section DegreeOf
/-! ### `degreeOf` -/
/-- `degreeOf n p` gives the highest power of X_n that appears in `p` -/
def degreeOf (n : σ) (p : MvPolynomial σ R) : ℕ :=
letI := Classical.decEq σ
p.degrees.count n
theorem degreeOf_def [DecidableEq σ] (n : σ) (p : MvPolynomial σ R) :
p.degreeOf n = p.degrees.count n := by rw [degreeOf]; convert rfl
theorem degreeOf_eq_sup (n : σ) (f : MvPolynomial σ R) :
degreeOf n f = f.support.sup fun m => m n := by
classical
rw [degreeOf_def, degrees, Multiset.count_finset_sup]
congr
ext
simp only [count_toMultiset]
theorem degreeOf_lt_iff {n : σ} {f : MvPolynomial σ R} {d : ℕ} (h : 0 < d) :
degreeOf n f < d ↔ ∀ m : σ →₀ ℕ, m ∈ f.support → m n < d := by
rwa [degreeOf_eq_sup, Finset.sup_lt_iff]
lemma degreeOf_le_iff {n : σ} {f : MvPolynomial σ R} {d : ℕ} :
degreeOf n f ≤ d ↔ ∀ m ∈ support f, m n ≤ d := by
rw [degreeOf_eq_sup, Finset.sup_le_iff]
@[simp]
theorem degreeOf_zero (n : σ) : degreeOf n (0 : MvPolynomial σ R) = 0 := by
classical simp only [degreeOf_def, degrees_zero, Multiset.count_zero]
@[simp]
theorem degreeOf_C (a : R) (x : σ) : degreeOf x (C a : MvPolynomial σ R) = 0 := by
classical simp [degreeOf_def, degrees_C]
theorem degreeOf_X [DecidableEq σ] (i j : σ) [Nontrivial R] :
degreeOf i (X j : MvPolynomial σ R) = if i = j then 1 else 0 := by
classical
by_cases c : i = j
· simp only [c, if_true, degreeOf_def, degrees_X, Multiset.count_singleton]
simp [c, degreeOf_def, degrees_X]
theorem degreeOf_add_le (n : σ) (f g : MvPolynomial σ R) :
degreeOf n (f + g) ≤ max (degreeOf n f) (degreeOf n g) := by
simp_rw [degreeOf_eq_sup]; exact supDegree_add_le
theorem monomial_le_degreeOf (i : σ) {f : MvPolynomial σ R} {m : σ →₀ ℕ} (h_m : m ∈ f.support) :
m i ≤ degreeOf i f := by
rw [degreeOf_eq_sup i]
apply Finset.le_sup h_m
lemma degreeOf_monomial_eq (s : σ →₀ ℕ) (i : σ) {a : R} (ha : a ≠ 0) :
(monomial s a).degreeOf i = s i := by
classical rw [degreeOf_def, degrees_monomial_eq _ _ ha, Finsupp.count_toMultiset]
-- TODO we can prove equality with `NoZeroDivisors R`
theorem degreeOf_mul_le (i : σ) (f g : MvPolynomial σ R) :
degreeOf i (f * g) ≤ degreeOf i f + degreeOf i g := by
classical
simp only [degreeOf]
convert Multiset.count_le_of_le i degrees_mul_le
rw [Multiset.count_add]
theorem degreeOf_sum_le {ι : Type*} (i : σ) (s : Finset ι) (f : ι → MvPolynomial σ R) :
degreeOf i (∑ j ∈ s, f j) ≤ s.sup fun j => degreeOf i (f j) := by
simp_rw [degreeOf_eq_sup]
exact supDegree_sum_le
-- TODO we can prove equality with `NoZeroDivisors R`
theorem degreeOf_prod_le {ι : Type*} (i : σ) (s : Finset ι) (f : ι → MvPolynomial σ R) :
degreeOf i (∏ j ∈ s, f j) ≤ ∑ j ∈ s, (f j).degreeOf i := by
simp_rw [degreeOf_eq_sup]
exact supDegree_prod_le (by simp only [coe_zero, Pi.zero_apply]) (by simp)
-- TODO we can prove equality with `NoZeroDivisors R`
theorem degreeOf_pow_le (i : σ) (p : MvPolynomial σ R) (n : ℕ) :
degreeOf i (p ^ n) ≤ n * degreeOf i p := by
simpa using degreeOf_prod_le i (Finset.range n) (fun _ => p)
theorem degreeOf_mul_X_of_ne {i j : σ} (f : MvPolynomial σ R) (h : i ≠ j) :
degreeOf i (f * X j) = degreeOf i f := by
classical
simp only [degreeOf_eq_sup i, support_mul_X, Finset.sup_map]
congr
ext
simp only [Finsupp.single, add_eq_left, addRightEmbedding_apply, coe_mk,
Pi.add_apply, comp_apply, Finsupp.coe_add, Pi.single_eq_of_ne h]
theorem degreeOf_mul_X_self (j : σ) (f : MvPolynomial σ R) :
degreeOf j (f * X j) ≤ degreeOf j f + 1 := by
classical
simp only [degreeOf]
apply (Multiset.count_le_of_le j degrees_mul_le).trans
simp only [Multiset.count_add, add_le_add_iff_left]
convert Multiset.count_le_of_le j <| degrees_X' j
rw [Multiset.count_singleton_self]
theorem degreeOf_mul_X_eq_degreeOf_add_one_iff (j : σ) (f : MvPolynomial σ R) :
degreeOf j (f * X j) = degreeOf j f + 1 ↔ f ≠ 0 := by
refine ⟨fun h => by by_contra ha; simp [ha] at h, fun h => ?_⟩
apply Nat.le_antisymm (degreeOf_mul_X_self j f)
have : (f.support.sup fun m ↦ m j) + 1 = (f.support.sup fun m ↦ (m j + 1)) :=
Finset.comp_sup_eq_sup_comp_of_nonempty @Nat.succ_le_succ (support_nonempty.mpr h)
simp only [degreeOf_eq_sup, support_mul_X, this]
apply Finset.sup_le
intro x hx
simp only [Finset.sup_map, bot_eq_zero', add_pos_iff, zero_lt_one, or_true, Finset.le_sup_iff]
use x
simpa using mem_support_iff.mp hx
theorem degreeOf_C_mul_le (p : MvPolynomial σ R) (i : σ) (c : R) :
(C c * p).degreeOf i ≤ p.degreeOf i := by
unfold degreeOf
convert Multiset.count_le_of_le i degrees_mul_le
simp only [degrees_C, zero_add]
theorem degreeOf_mul_C_le (p : MvPolynomial σ R) (i : σ) (c : R) :
(p * C c).degreeOf i ≤ p.degreeOf i := by
unfold degreeOf
convert Multiset.count_le_of_le i degrees_mul_le
simp only [degrees_C, add_zero]
theorem degreeOf_rename_of_injective {p : MvPolynomial σ R} {f : σ → τ} (h : Function.Injective f)
(i : σ) : degreeOf (f i) (rename f p) = degreeOf i p := by
classical
simp only [degreeOf, degrees_rename_of_injective h, Multiset.count_map_eq_count' f p.degrees h]
end DegreeOf
section TotalDegree
/-! ### `totalDegree` -/
/-- `totalDegree p` gives the maximum |s| over the monomials X^s in `p` -/
def totalDegree (p : MvPolynomial σ R) : ℕ :=
p.support.sup fun s => s.sum fun _ e => e
theorem totalDegree_eq (p : MvPolynomial σ R) :
p.totalDegree = p.support.sup fun m => Multiset.card (toMultiset m) := by
rw [totalDegree]
congr; funext m
exact (Finsupp.card_toMultiset _).symm
theorem le_totalDegree {p : MvPolynomial σ R} {s : σ →₀ ℕ} (h : s ∈ p.support) :
(s.sum fun _ e => e) ≤ totalDegree p :=
Finset.le_sup (α := ℕ) (f := fun s => sum s fun _ e => e) h
theorem totalDegree_le_degrees_card (p : MvPolynomial σ R) :
p.totalDegree ≤ Multiset.card p.degrees := by
classical
rw [totalDegree_eq]
exact Finset.sup_le fun s hs => Multiset.card_le_card <| Finset.le_sup hs
theorem totalDegree_le_of_support_subset (h : p.support ⊆ q.support) :
totalDegree p ≤ totalDegree q :=
Finset.sup_mono h
@[simp]
theorem totalDegree_C (a : R) : (C a : MvPolynomial σ R).totalDegree = 0 :=
(supDegree_single 0 a).trans <| by rw [sum_zero_index, bot_eq_zero', ite_self]
@[simp]
theorem totalDegree_zero : (0 : MvPolynomial σ R).totalDegree = 0 := by
rw [← C_0]; exact totalDegree_C (0 : R)
@[simp]
theorem totalDegree_one : (1 : MvPolynomial σ R).totalDegree = 0 :=
totalDegree_C (1 : R)
@[simp]
theorem totalDegree_X {R} [CommSemiring R] [Nontrivial R] (s : σ) :
(X s : MvPolynomial σ R).totalDegree = 1 := by
rw [totalDegree, support_X]
simp only [Finset.sup, Finsupp.sum_single_index, Finset.fold_singleton, sup_bot_eq]
theorem totalDegree_add (a b : MvPolynomial σ R) :
(a + b).totalDegree ≤ max a.totalDegree b.totalDegree :=
sup_support_add_le _ _ _
theorem totalDegree_add_eq_left_of_totalDegree_lt {p q : MvPolynomial σ R}
(h : q.totalDegree < p.totalDegree) : (p + q).totalDegree = p.totalDegree := by
classical
apply le_antisymm
· rw [← max_eq_left_of_lt h]
exact totalDegree_add p q
by_cases hp : p = 0
· simp [hp]
obtain ⟨b, hb₁, hb₂⟩ :=
p.support.exists_mem_eq_sup (Finsupp.support_nonempty_iff.mpr hp) fun m : σ →₀ ℕ =>
Multiset.card (toMultiset m)
have hb : b ∉ q.support := by
contrapose! h
rw [totalDegree_eq p, hb₂, totalDegree_eq]
apply Finset.le_sup h
have hbb : b ∈ (p + q).support := by
apply support_sdiff_support_subset_support_add
rw [Finset.mem_sdiff]
exact ⟨hb₁, hb⟩
rw [totalDegree_eq, hb₂, totalDegree_eq]
exact Finset.le_sup (f := fun m => Multiset.card (Finsupp.toMultiset m)) hbb
theorem totalDegree_add_eq_right_of_totalDegree_lt {p q : MvPolynomial σ R}
(h : q.totalDegree < p.totalDegree) : (q + p).totalDegree = p.totalDegree := by
rw [add_comm, totalDegree_add_eq_left_of_totalDegree_lt h]
theorem totalDegree_mul (a b : MvPolynomial σ R) :
(a * b).totalDegree ≤ a.totalDegree + b.totalDegree :=
sup_support_mul_le (fun _ _ ↦ (Finsupp.sum_add_index' (fun _ => rfl) (fun _ _ _ => rfl)).le) _ _
theorem totalDegree_smul_le [CommSemiring S] [DistribMulAction R S] (a : R) (f : MvPolynomial σ S) :
(a • f).totalDegree ≤ f.totalDegree :=
Finset.sup_mono support_smul
theorem totalDegree_pow (a : MvPolynomial σ R) (n : ℕ) :
(a ^ n).totalDegree ≤ n * a.totalDegree := by
rw [Finset.pow_eq_prod_const, ← Nat.nsmul_eq_mul, Finset.nsmul_eq_sum_const]
refine supDegree_prod_le rfl (fun _ _ => ?_)
exact Finsupp.sum_add_index' (fun _ => rfl) (fun _ _ _ => rfl)
@[simp]
theorem totalDegree_monomial (s : σ →₀ ℕ) {c : R} (hc : c ≠ 0) :
(monomial s c : MvPolynomial σ R).totalDegree = s.sum fun _ e => e := by
classical simp [totalDegree, support_monomial, if_neg hc]
theorem totalDegree_monomial_le (s : σ →₀ ℕ) (c : R) :
(monomial s c).totalDegree ≤ s.sum fun _ ↦ id := by
if hc : c = 0 then
simp only [hc, map_zero, totalDegree_zero, zero_le]
else
rw [totalDegree_monomial _ hc, Function.id_def]
@[simp]
theorem totalDegree_X_pow [Nontrivial R] (s : σ) (n : ℕ) :
(X s ^ n : MvPolynomial σ R).totalDegree = n := by simp [X_pow_eq_monomial, one_ne_zero]
theorem totalDegree_list_prod (l : List (MvPolynomial σ R)) :
l.prod.totalDegree ≤ (l.map MvPolynomial.totalDegree).sum :=
l.apply_prod_le_sum_map _ totalDegree_one.le totalDegree_mul
theorem totalDegree_multiset_prod (s : Multiset (MvPolynomial σ R)) :
s.prod.totalDegree ≤ (s.map MvPolynomial.totalDegree).sum :=
s.apply_prod_le_sum_map _ totalDegree_one.le totalDegree_mul
theorem totalDegree_finset_prod {ι : Type*} (s : Finset ι) (f : ι → MvPolynomial σ R) :
(s.prod f).totalDegree ≤ ∑ i ∈ s, (f i).totalDegree :=
s.apply_prod_le_sum_apply _ totalDegree_one.le totalDegree_mul
theorem totalDegree_finset_sum {ι : Type*} (s : Finset ι) (f : ι → MvPolynomial σ R) :
(s.sum f).totalDegree ≤ Finset.sup s fun i => (f i).totalDegree := by
induction s using Finset.cons_induction with
| empty => exact zero_le _
| cons a s has hind =>
rw [Finset.sum_cons, Finset.sup_cons]
exact (MvPolynomial.totalDegree_add _ _).trans (max_le_max le_rfl hind)
lemma totalDegree_finsetSum_le {ι : Type*} {s : Finset ι} {f : ι → MvPolynomial σ R} {d : ℕ}
(hf : ∀ i ∈ s, (f i).totalDegree ≤ d) : (s.sum f).totalDegree ≤ d :=
(totalDegree_finset_sum ..).trans <| Finset.sup_le hf
lemma degreeOf_le_totalDegree (f : MvPolynomial σ R) (i : σ) : f.degreeOf i ≤ f.totalDegree :=
degreeOf_le_iff.mpr fun d hd ↦ (eq_or_ne (d i) 0).elim (by cutsat) fun h ↦
(Finset.single_le_sum (by cutsat) <| Finsupp.mem_support_iff.mpr h).trans
(le_totalDegree hd)
theorem exists_degree_lt [Fintype σ] (f : MvPolynomial σ R) (n : ℕ)
(h : f.totalDegree < n * Fintype.card σ) {d : σ →₀ ℕ} (hd : d ∈ f.support) : ∃ i, d i < n := by
contrapose! h
calc
n * Fintype.card σ = ∑ _s : σ, n := by
rw [Finset.sum_const, Nat.nsmul_eq_mul, mul_comm, Finset.card_univ]
_ ≤ ∑ s, d s := Finset.sum_le_sum fun s _ => h s
_ ≤ d.sum fun _ e => e := by
rw [Finsupp.sum_fintype]
intros
rfl
_ ≤ f.totalDegree := le_totalDegree hd
theorem coeff_eq_zero_of_totalDegree_lt {f : MvPolynomial σ R} {d : σ →₀ ℕ}
(h : f.totalDegree < ∑ i ∈ d.support, d i) : coeff d f = 0 := by
classical
rw [totalDegree, Finset.sup_lt_iff] at h
· specialize h d
rw [mem_support_iff] at h
refine not_not.mp (mt h ?_)
exact lt_irrefl _
· exact lt_of_le_of_lt (Nat.zero_le _) h
theorem totalDegree_eq_zero_iff_eq_C {p : MvPolynomial σ R} :
p.totalDegree = 0 ↔ p = C (p.coeff 0) := by
constructor <;> intro h
· ext m; classical rw [coeff_C]; split_ifs with hm; · rw [← hm]
apply coeff_eq_zero_of_totalDegree_lt; rw [h]
exact Finset.sum_pos (fun i hi ↦ Nat.pos_of_ne_zero <| Finsupp.mem_support_iff.mp hi)
(Finsupp.support_nonempty_iff.mpr <| Ne.symm hm)
· rw [h, totalDegree_C]
theorem totalDegree_rename_le (f : σ → τ) (p : MvPolynomial σ R) :
(rename f p).totalDegree ≤ p.totalDegree :=
Finset.sup_le fun b => by
classical
intro h
rw [rename_eq] at h
have h' := Finsupp.mapDomain_support h
rw [Finset.mem_image] at h'
rcases h' with ⟨s, hs, rfl⟩
exact (sum_mapDomain_index (fun _ => rfl) (fun _ _ _ => rfl)).trans_le (le_totalDegree hs)
lemma totalDegree_renameEquiv (f : σ ≃ τ) (p : MvPolynomial σ R) :
(renameEquiv R f p).totalDegree = p.totalDegree :=
(totalDegree_rename_le f p).antisymm (le_trans (by simp) (totalDegree_rename_le f.symm _))
end TotalDegree
section degreesLE
variable {s t : Multiset σ}
variable (R σ s) in
/-- The submodule of multivariate polynomials of degrees bounded by a monomial `s`. -/
def degreesLE : Submodule R (MvPolynomial σ R) where
carrier := {p | p.degrees ≤ s}
add_mem' {a b} ha hb := by classical exact degrees_add_le.trans (sup_le ha hb)
zero_mem' := by simp
smul_mem' c {x} hx := by
dsimp
rw [Algebra.smul_def]
refine degrees_mul_le.trans ?_
simpa [degrees_C] using hx
@[simp] lemma mem_degreesLE : p ∈ degreesLE R σ s ↔ p.degrees ≤ s := Iff.rfl
variable (s t) in
lemma degreesLE_add : degreesLE R σ (s + t) = degreesLE R σ s * degreesLE R σ t := by
classical
rw [le_antisymm_iff, Submodule.mul_le]
refine ⟨fun x hx ↦ x.as_sum ▸ sum_mem fun i hi ↦ ?_,
fun x hx y hy ↦ degrees_mul_le.trans (add_le_add hx hy)⟩
replace hi : i.toMultiset ≤ s + t := (Finset.le_sup hi).trans hx
let a := (i.toMultiset - t).toFinsupp
let b := (i.toMultiset ⊓ t).toFinsupp
have : a + b = i := Multiset.toFinsupp.symm.injective (by simp [a, b, Multiset.sub_add_inter])
have ha : a.toMultiset ≤ s := by simpa [a, add_comm (a := t)] using hi
have hb : b.toMultiset ≤ t := by simp [b, Multiset.inter_le_right]
rw [show monomial i (x.coeff i) = monomial a (x.coeff i) * monomial b 1 by simp [this]]
exact Submodule.mul_mem_mul ((degrees_monomial _ _).trans ha) ((degrees_monomial _ _).trans hb)
@[simp] lemma degreesLE_zero : degreesLE R σ 0 = 1 := by
refine le_antisymm (fun x hx ↦ ?_) (by simp)
simp only [mem_degreesLE, nonpos_iff_eq_zero] at hx
have := (totalDegree_eq_zero_iff_eq_C (p := x)).mp
(Nat.eq_zero_of_le_zero (x.totalDegree_le_degrees_card.trans (by simp [hx])))
exact ⟨x.coeff 0, by simp [Algebra.smul_def, ← this]⟩
variable (s) in
lemma degreesLE_nsmul : ∀ n, degreesLE R σ (n • s) = degreesLE R σ s ^ n
| 0 => by simp
| k + 1 => by simp only [pow_succ, degreesLE_nsmul, degreesLE_add, add_smul, one_smul]
end degreesLE
end CommSemiring
end MvPolynomial |
.lake/packages/mathlib/Mathlib/Algebra/MvPolynomial/Invertible.lean | import Mathlib.Algebra.MvPolynomial.Basic
import Mathlib.RingTheory.AlgebraTower
/-!
# Invertible polynomials
This file is a stub containing some basic facts about
invertible elements in the ring of polynomials.
-/
open MvPolynomial
noncomputable instance MvPolynomial.invertibleC (σ : Type*) {R : Type*} [CommSemiring R] (r : R)
[Invertible r] : Invertible (C r : MvPolynomial σ R) :=
Invertible.map (C : R →+* MvPolynomial σ R) _
/-- A natural number that is invertible when coerced to a commutative semiring `R`
is also invertible when coerced to any polynomial ring with rational coefficients.
Short-cut for typeclass resolution. -/
noncomputable instance MvPolynomial.invertibleCoeNat (σ R : Type*) (p : ℕ) [CommSemiring R]
[Invertible (p : R)] : Invertible (p : MvPolynomial σ R) :=
IsScalarTower.invertibleAlgebraCoeNat R _ _ |
.lake/packages/mathlib/Mathlib/Algebra/MvPolynomial/Monad.lean | import Mathlib.Algebra.MvPolynomial.Rename
import Mathlib.Algebra.MvPolynomial.Variables
/-!
# Monad operations on `MvPolynomial`
This file defines two monadic operations on `MvPolynomial`. Given `p : MvPolynomial σ R`,
* `MvPolynomial.bind₁` and `MvPolynomial.join₁` operate on the variable type `σ`.
* `MvPolynomial.bind₂` and `MvPolynomial.join₂` operate on the coefficient type `R`.
- `MvPolynomial.bind₁ f φ` with `f : σ → MvPolynomial τ R` and `φ : MvPolynomial σ R`,
is the polynomial `φ(f 1, ..., f i, ...) : MvPolynomial τ R`.
- `MvPolynomial.join₁ φ` with `φ : MvPolynomial (MvPolynomial σ R) R` collapses `φ` to
a `MvPolynomial σ R`, by evaluating `φ` under the map `X f ↦ f` for `f : MvPolynomial σ R`.
In other words, if you have a polynomial `φ` in a set of variables indexed by a polynomial ring,
you evaluate the polynomial in these indexing polynomials.
- `MvPolynomial.bind₂ f φ` with `f : R →+* MvPolynomial σ S` and `φ : MvPolynomial σ R`
is the `MvPolynomial σ S` obtained from `φ` by mapping the coefficients of `φ` through `f`
and considering the resulting polynomial as polynomial expression in `MvPolynomial σ R`.
- `MvPolynomial.join₂ φ` with `φ : MvPolynomial σ (MvPolynomial σ R)` collapses `φ` to
a `MvPolynomial σ R`, by considering `φ` as polynomial expression in `MvPolynomial σ R`.
These operations themselves have algebraic structure: `MvPolynomial.bind₁`
and `MvPolynomial.join₁` are algebra homs and
`MvPolynomial.bind₂` and `MvPolynomial.join₂` are ring homs.
They interact in convenient ways with `MvPolynomial.rename`, `MvPolynomial.map`,
`MvPolynomial.vars`, and other polynomial operations.
Indeed, `MvPolynomial.rename` is the "map" operation for the (`bind₁`, `join₁`) pair,
whereas `MvPolynomial.map` is the "map" operation for the other pair.
## Implementation notes
We add a `LawfulMonad` instance for the (`bind₁`, `join₁`) pair.
The second pair cannot be instantiated as a `Monad`,
since it is not a monad in `Type` but in `CommRingCat` (or rather `CommSemiRingCat`).
-/
noncomputable section
namespace MvPolynomial
open Finsupp
variable {σ : Type*} {τ : Type*}
variable {R S T : Type*} [CommSemiring R] [CommSemiring S] [CommSemiring T]
/--
`bind₁` is the "left-hand side" bind operation on `MvPolynomial`, operating on the variable type.
Given a polynomial `p : MvPolynomial σ R` and a map `f : σ → MvPolynomial τ R` taking variables
in `p` to polynomials in the variable type `τ`, `bind₁ f p` replaces each variable in `p` with
its value under `f`, producing a new polynomial in `τ`. The coefficient type remains the same.
This operation is an algebra hom.
-/
def bind₁ (f : σ → MvPolynomial τ R) : MvPolynomial σ R →ₐ[R] MvPolynomial τ R :=
aeval f
/-- `bind₂` is the "right-hand side" bind operation on `MvPolynomial`,
operating on the coefficient type.
Given a polynomial `p : MvPolynomial σ R` and
a map `f : R → MvPolynomial σ S` taking coefficients in `p` to polynomials over a new ring `S`,
`bind₂ f p` replaces each coefficient in `p` with its value under `f`,
producing a new polynomial over `S`.
The variable type remains the same. This operation is a ring hom.
-/
def bind₂ (f : R →+* MvPolynomial σ S) : MvPolynomial σ R →+* MvPolynomial σ S :=
eval₂Hom f X
/--
`join₁` is the monadic join operation corresponding to `MvPolynomial.bind₁`. Given a polynomial `p`
with coefficients in `R` whose variables are polynomials in `σ` with coefficients in `R`,
`join₁ p` collapses `p` to a polynomial with variables in `σ` and coefficients in `R`.
This operation is an algebra hom.
-/
def join₁ : MvPolynomial (MvPolynomial σ R) R →ₐ[R] MvPolynomial σ R :=
aeval id
/--
`join₂` is the monadic join operation corresponding to `MvPolynomial.bind₂`. Given a polynomial `p`
with variables in `σ` whose coefficients are polynomials in `σ` with coefficients in `R`,
`join₂ p` collapses `p` to a polynomial with variables in `σ` and coefficients in `R`.
This operation is a ring hom.
-/
def join₂ : MvPolynomial σ (MvPolynomial σ R) →+* MvPolynomial σ R :=
eval₂Hom (RingHom.id _) X
@[simp]
theorem aeval_eq_bind₁ (f : σ → MvPolynomial τ R) : aeval f = bind₁ f :=
rfl
@[simp]
theorem eval₂Hom_C_eq_bind₁ (f : σ → MvPolynomial τ R) : eval₂Hom C f = bind₁ f :=
rfl
@[simp]
theorem eval₂Hom_eq_bind₂ (f : R →+* MvPolynomial σ S) : eval₂Hom f X = bind₂ f :=
rfl
section
variable (σ R)
@[simp]
theorem aeval_id_eq_join₁ : aeval id = @join₁ σ R _ :=
rfl
theorem eval₂Hom_C_id_eq_join₁ (φ : MvPolynomial (MvPolynomial σ R) R) :
eval₂Hom C id φ = join₁ φ :=
rfl
@[simp]
theorem eval₂Hom_id_X_eq_join₂ : eval₂Hom (RingHom.id _) X = @join₂ σ R _ :=
rfl
end
-- In this file, we don't want to use these simp lemmas,
-- because we first need to show how these new definitions interact
-- and the proofs fall back on unfolding the definitions and call simp afterwards
attribute [-simp]
aeval_eq_bind₁ eval₂Hom_C_eq_bind₁ eval₂Hom_eq_bind₂ aeval_id_eq_join₁ eval₂Hom_id_X_eq_join₂
@[simp]
theorem bind₁_X_right (f : σ → MvPolynomial τ R) (i : σ) : bind₁ f (X i) = f i :=
aeval_X f i
@[simp]
theorem bind₂_X_right (f : R →+* MvPolynomial σ S) (i : σ) : bind₂ f (X i) = X i :=
eval₂Hom_X' f X i
@[simp]
theorem bind₁_X_left : bind₁ (X : σ → MvPolynomial σ R) = AlgHom.id R _ := by
ext1 i
simp
variable (f : σ → MvPolynomial τ R)
theorem bind₁_C_right (f : σ → MvPolynomial τ R) (x) : bind₁ f (C x) = C x := algHom_C _ _
@[simp]
theorem bind₂_C_right (f : R →+* MvPolynomial σ S) (r : R) : bind₂ f (C r) = f r :=
eval₂Hom_C f X r
@[simp]
theorem bind₂_C_left : bind₂ (C : R →+* MvPolynomial σ R) = RingHom.id _ := by ext : 2 <;> simp
@[simp]
theorem bind₂_comp_C (f : R →+* MvPolynomial σ S) : (bind₂ f).comp C = f :=
RingHom.ext <| bind₂_C_right _
@[simp]
theorem join₂_map (f : R →+* MvPolynomial σ S) (φ : MvPolynomial σ R) :
join₂ (map f φ) = bind₂ f φ := by simp only [join₂, bind₂, eval₂Hom_map_hom, RingHom.id_comp]
@[simp]
theorem join₂_comp_map (f : R →+* MvPolynomial σ S) : join₂.comp (map f) = bind₂ f :=
RingHom.ext <| join₂_map _
theorem aeval_id_rename (f : σ → MvPolynomial τ R) (p : MvPolynomial σ R) :
aeval id (rename f p) = aeval f p := by rw [aeval_rename, Function.id_comp]
@[simp]
theorem join₁_rename (f : σ → MvPolynomial τ R) (φ : MvPolynomial σ R) :
join₁ (rename f φ) = bind₁ f φ :=
aeval_id_rename _ _
@[simp]
theorem bind₁_id : bind₁ (@id (MvPolynomial σ R)) = join₁ :=
rfl
@[simp]
theorem bind₂_id : bind₂ (RingHom.id (MvPolynomial σ R)) = join₂ :=
rfl
theorem bind₁_bind₁ {υ : Type*} (f : σ → MvPolynomial τ R) (g : τ → MvPolynomial υ R)
(φ : MvPolynomial σ R) : (bind₁ g) (bind₁ f φ) = bind₁ (fun i => bind₁ g (f i)) φ := by
simp [bind₁, ← comp_aeval]
theorem bind₁_comp_bind₁ {υ : Type*} (f : σ → MvPolynomial τ R) (g : τ → MvPolynomial υ R) :
(bind₁ g).comp (bind₁ f) = bind₁ fun i => bind₁ g (f i) := by
ext1
apply bind₁_bind₁
theorem bind₂_comp_bind₂ (f : R →+* MvPolynomial σ S) (g : S →+* MvPolynomial σ T) :
(bind₂ g).comp (bind₂ f) = bind₂ ((bind₂ g).comp f) := by ext : 2 <;> simp
theorem bind₂_bind₂ (f : R →+* MvPolynomial σ S) (g : S →+* MvPolynomial σ T)
(φ : MvPolynomial σ R) : (bind₂ g) (bind₂ f φ) = bind₂ ((bind₂ g).comp f) φ :=
RingHom.congr_fun (bind₂_comp_bind₂ f g) φ
theorem rename_comp_bind₁ {υ : Type*} (f : σ → MvPolynomial τ R) (g : τ → υ) :
(rename g).comp (bind₁ f) = bind₁ fun i => rename g <| f i := by
ext1 i
simp
theorem rename_bind₁ {υ : Type*} (f : σ → MvPolynomial τ R) (g : τ → υ) (φ : MvPolynomial σ R) :
rename g (bind₁ f φ) = bind₁ (fun i => rename g <| f i) φ :=
AlgHom.congr_fun (rename_comp_bind₁ f g) φ
theorem map_bind₂ (f : R →+* MvPolynomial σ S) (g : S →+* T) (φ : MvPolynomial σ R) :
map g (bind₂ f φ) = bind₂ ((map g).comp f) φ := by
simp only [bind₂, eval₂_comp_right, coe_eval₂Hom, eval₂_map]
congr 1 with : 1
simp only [Function.comp_apply, map_X]
theorem bind₁_comp_rename {υ : Type*} (f : τ → MvPolynomial υ R) (g : σ → τ) :
(bind₁ f).comp (rename g) = bind₁ (f ∘ g) := by
ext1 i
simp
theorem bind₁_rename {υ : Type*} (f : τ → MvPolynomial υ R) (g : σ → τ) (φ : MvPolynomial σ R) :
bind₁ f (rename g φ) = bind₁ (f ∘ g) φ :=
AlgHom.congr_fun (bind₁_comp_rename f g) φ
theorem bind₂_map (f : S →+* MvPolynomial σ T) (g : R →+* S) (φ : MvPolynomial σ R) :
bind₂ f (map g φ) = bind₂ (f.comp g) φ := by simp [bind₂]
@[simp]
theorem map_comp_C (f : R →+* S) : (map f).comp (C : R →+* MvPolynomial σ R) = C.comp f := by
ext1
apply map_C
-- mixing the two monad structures
theorem hom_bind₁ (f : MvPolynomial τ R →+* S) (g : σ → MvPolynomial τ R) (φ : MvPolynomial σ R) :
f (bind₁ g φ) = eval₂Hom (f.comp C) (fun i => f (g i)) φ := by
rw [bind₁, map_aeval, algebraMap_eq]
theorem map_bind₁ (f : R →+* S) (g : σ → MvPolynomial τ R) (φ : MvPolynomial σ R) :
map f (bind₁ g φ) = bind₁ (fun i : σ => (map f) (g i)) (map f φ) := by
rw [hom_bind₁, map_comp_C, ← eval₂Hom_map_hom]
rfl
@[simp]
theorem eval₂Hom_comp_C (f : R →+* S) (g : σ → S) : (eval₂Hom f g).comp C = f := by
ext1 r
exact eval₂_C f g r
theorem eval₂Hom_bind₁ (f : R →+* S) (g : τ → S) (h : σ → MvPolynomial τ R) (φ : MvPolynomial σ R) :
eval₂Hom f g (bind₁ h φ) = eval₂Hom f (fun i => eval₂Hom f g (h i)) φ := by
rw [hom_bind₁, eval₂Hom_comp_C]
theorem aeval_bind₁ [Algebra R S] (f : τ → S) (g : σ → MvPolynomial τ R) (φ : MvPolynomial σ R) :
aeval f (bind₁ g φ) = aeval (fun i => aeval f (g i)) φ :=
eval₂Hom_bind₁ _ _ _ _
theorem aeval_comp_bind₁ [Algebra R S] (f : τ → S) (g : σ → MvPolynomial τ R) :
(aeval f).comp (bind₁ g) = aeval fun i => aeval f (g i) := by
ext1
apply aeval_bind₁
theorem eval₂Hom_comp_bind₂ (f : S →+* T) (g : σ → T) (h : R →+* MvPolynomial σ S) :
(eval₂Hom f g).comp (bind₂ h) = eval₂Hom ((eval₂Hom f g).comp h) g := by ext : 2 <;> simp
theorem eval₂Hom_bind₂ (f : S →+* T) (g : σ → T) (h : R →+* MvPolynomial σ S)
(φ : MvPolynomial σ R) : eval₂Hom f g (bind₂ h φ) = eval₂Hom ((eval₂Hom f g).comp h) g φ :=
RingHom.congr_fun (eval₂Hom_comp_bind₂ f g h) φ
theorem aeval_bind₂ [Algebra S T] (f : σ → T) (g : R →+* MvPolynomial σ S) (φ : MvPolynomial σ R) :
aeval f (bind₂ g φ) = eval₂Hom ((↑(aeval f : _ →ₐ[S] _) : _ →+* _).comp g) f φ :=
eval₂Hom_bind₂ _ _ _ _
alias eval₂Hom_C_left := eval₂Hom_C_eq_bind₁
theorem bind₁_monomial (f : σ → MvPolynomial τ R) (d : σ →₀ ℕ) (r : R) :
bind₁ f (monomial d r) = C r * ∏ i ∈ d.support, f i ^ d i := by
simp only [monomial_eq, map_mul, bind₁_C_right, Finsupp.prod, map_prod,
map_pow, bind₁_X_right]
theorem bind₂_monomial (f : R →+* MvPolynomial σ S) (d : σ →₀ ℕ) (r : R) :
bind₂ f (monomial d r) = f r * monomial d 1 := by
simp only [monomial_eq, RingHom.map_mul, bind₂_C_right, Finsupp.prod, map_prod,
map_pow, bind₂_X_right, C_1, one_mul]
@[simp]
theorem bind₂_monomial_one (f : R →+* MvPolynomial σ S) (d : σ →₀ ℕ) :
bind₂ f (monomial d 1) = monomial d 1 := by rw [bind₂_monomial, f.map_one, one_mul]
section
theorem vars_bind₁ [DecidableEq τ] (f : σ → MvPolynomial τ R) (φ : MvPolynomial σ R) :
(bind₁ f φ).vars ⊆ φ.vars.biUnion fun i => (f i).vars := by
calc (bind₁ f φ).vars
_ = (φ.support.sum fun x : σ →₀ ℕ => (bind₁ f) (monomial x (coeff x φ))).vars := by
rw [← map_sum, ← φ.as_sum]
_ ≤ φ.support.biUnion fun i : σ →₀ ℕ => ((bind₁ f) (monomial i (coeff i φ))).vars :=
(vars_sum_subset _ _)
_ = φ.support.biUnion fun d : σ →₀ ℕ => vars (C (coeff d φ) * ∏ i ∈ d.support, f i ^ d i) := by
simp only [bind₁_monomial]
_ ≤ φ.support.biUnion fun d : σ →₀ ℕ => d.support.biUnion fun i => vars (f i) := ?_
-- proof below
_ ≤ φ.vars.biUnion fun i : σ => vars (f i) := ?_
-- proof below
· apply Finset.biUnion_mono
intro d _hd
calc
vars (C (coeff d φ) * ∏ i ∈ d.support, f i ^ d i) ≤
(C (coeff d φ)).vars ∪ (∏ i ∈ d.support, f i ^ d i).vars :=
vars_mul _ _
_ ≤ (∏ i ∈ d.support, f i ^ d i).vars := by
simp only [Finset.empty_union, vars_C, Finset.le_iff_subset, Finset.Subset.refl]
_ ≤ d.support.biUnion fun i : σ => vars (f i ^ d i) := vars_prod _
_ ≤ d.support.biUnion fun i : σ => (f i).vars := ?_
apply Finset.biUnion_mono
intro i _hi
apply vars_pow
· intro j
simp_rw [Finset.mem_biUnion]
rintro ⟨d, hd, ⟨i, hi, hj⟩⟩
exact ⟨i, (mem_vars _).mpr ⟨d, hd, hi⟩, hj⟩
end
theorem mem_vars_bind₁ (f : σ → MvPolynomial τ R) (φ : MvPolynomial σ R) {j : τ}
(h : j ∈ (bind₁ f φ).vars) : ∃ i : σ, i ∈ φ.vars ∧ j ∈ (f i).vars := by
classical
simpa only [exists_prop, Finset.mem_biUnion, mem_support_iff, Ne] using vars_bind₁ f φ h
instance monad : Monad fun σ => MvPolynomial σ R where
map f p := rename f p
pure := X
bind p f := bind₁ f p
instance lawfulFunctor : LawfulFunctor fun σ => MvPolynomial σ R where
map_const := by intros; rfl
id_map := by intros; simp [(· <$> ·)]
comp_map := by intros; simp [(· <$> ·)]
instance lawfulMonad : LawfulMonad fun σ => MvPolynomial σ R where
pure_bind := by intros; simp [pure, bind]
bind_assoc := by intros; simp [bind, ← bind₁_comp_bind₁]
seqLeft_eq := by intros; simp [SeqLeft.seqLeft, Seq.seq, (· <$> ·), bind₁_rename]; rfl
seqRight_eq := by intros; simp [SeqRight.seqRight, Seq.seq, (· <$> ·), bind₁_rename]; rfl
pure_seq := by intros; simp [(· <$> ·), pure, Seq.seq]
bind_pure_comp := by aesop
bind_map := by aesop
/-
Possible TODO for the future:
Enable the following definitions, and write a lot of supporting lemmas.
def bind (f : R →+* mv_polynomial τ S) (g : σ → mv_polynomial τ S) :
mv_polynomial σ R →+* mv_polynomial τ S :=
eval₂_hom f g
def join (f : R →+* S) : mv_polynomial (mv_polynomial σ R) S →ₐ[S] mv_polynomial σ S :=
aeval (map f)
def ajoin [algebra R S] : mv_polynomial (mv_polynomial σ R) S →ₐ[S] mv_polynomial σ S :=
join (algebra_map R S)
-/
end MvPolynomial |
.lake/packages/mathlib/Mathlib/Algebra/MvPolynomial/CommRing.lean | import Mathlib.Algebra.MvPolynomial.Variables
/-!
# Multivariate polynomials over a ring
Many results about polynomials hold when the coefficient ring is a commutative semiring.
Some stronger results can be derived when we assume this semiring is a ring.
This file does not define any new operations, but proves some of these stronger results.
## Notation
As in other polynomial files, we typically use the notation:
+ `σ : Type*` (indexing the variables)
+ `R : Type*` `[CommRing R]` (the coefficients)
+ `s : σ →₀ ℕ`, a function from `σ` to `ℕ` which is zero away from a finite set.
This will give rise to a monomial in `MvPolynomial σ R` which mathematicians might call `X^s`
+ `a : R`
+ `i : σ`, with corresponding monomial `X i`, often denoted `X_i` by mathematicians
+ `p : MvPolynomial σ R`
-/
noncomputable section
open Set Function Finsupp AddMonoidAlgebra
universe u v
variable {R : Type u} {S : Type v}
namespace MvPolynomial
variable {σ : Type*} {a a' a₁ a₂ : R} {e : ℕ} {n m : σ} {s : σ →₀ ℕ}
section CommRing
variable [CommRing R]
variable {p q : MvPolynomial σ R}
instance instCommRingMvPolynomial : CommRing (MvPolynomial σ R) :=
AddMonoidAlgebra.commRing
variable (σ a a')
@[simp]
theorem C_sub : (C (a - a') : MvPolynomial σ R) = C a - C a' :=
RingHom.map_sub _ _ _
@[simp]
theorem C_neg : (C (-a) : MvPolynomial σ R) = -C a :=
RingHom.map_neg _ _
@[simp]
theorem coeff_neg (m : σ →₀ ℕ) (p : MvPolynomial σ R) : coeff m (-p) = -coeff m p :=
Finsupp.neg_apply _ _
@[simp]
theorem coeff_sub (m : σ →₀ ℕ) (p q : MvPolynomial σ R) : coeff m (p - q) = coeff m p - coeff m q :=
Finsupp.sub_apply _ _ _
@[simp]
theorem support_neg : (-p).support = p.support :=
Finsupp.support_neg p
theorem support_sub [DecidableEq σ] (p q : MvPolynomial σ R) :
(p - q).support ⊆ p.support ∪ q.support :=
Finsupp.support_sub
variable {σ} (p)
section Degrees
@[simp]
theorem degrees_neg (p : MvPolynomial σ R) : (-p).degrees = p.degrees := by
rw [degrees, support_neg]; rfl
theorem degrees_sub_le [DecidableEq σ] {p q : MvPolynomial σ R} :
(p - q).degrees ≤ p.degrees ∪ q.degrees := by
simpa [degrees_def] using AddMonoidAlgebra.supDegree_sub_le
end Degrees
section Degrees
@[simp]
theorem degreeOf_neg (i : σ) (p : MvPolynomial σ R) : degreeOf i (-p) = degreeOf i p := by
rw [degreeOf, degreeOf, degrees_neg]
theorem degreeOf_sub_le (i : σ) (p q : MvPolynomial σ R) :
degreeOf i (p - q) ≤ max (degreeOf i p) (degreeOf i q) := by
simpa only [sub_eq_add_neg, degreeOf_neg] using degreeOf_add_le i p (-q)
end Degrees
section Vars
@[simp]
theorem vars_neg : (-p).vars = p.vars := by simp [vars, degrees_neg]
theorem vars_sub_subset [DecidableEq σ] : (p - q).vars ⊆ p.vars ∪ q.vars := by
convert vars_add_subset p (-q) using 2 <;> simp [sub_eq_add_neg]
@[simp]
theorem vars_sub_of_disjoint [DecidableEq σ] (hpq : Disjoint p.vars q.vars) :
(p - q).vars = p.vars ∪ q.vars := by
rw [← vars_neg q] at hpq
convert vars_add_of_disjoint hpq using 2 <;> simp [sub_eq_add_neg]
end Vars
section Eval
variable [CommRing S]
variable (f : R →+* S) (g : σ → S)
@[simp]
theorem eval₂_sub : (p - q).eval₂ f g = p.eval₂ f g - q.eval₂ f g :=
(eval₂Hom f g).map_sub _ _
theorem eval_sub (f : σ → R) : eval f (p - q) = eval f p - eval f q :=
eval₂_sub _ _ _
@[simp]
theorem eval₂_neg : (-p).eval₂ f g = -p.eval₂ f g :=
(eval₂Hom f g).map_neg _
theorem eval_neg (f : σ → R) : eval f (-p) = -eval f p :=
eval₂_neg _ _ _
theorem hom_C (f : MvPolynomial σ ℤ →+* S) (n : ℤ) : f (C n) = (n : S) :=
eq_intCast (f.comp C) n
/-- A ring homomorphism `f : Z[X_1, X_2, ...] → R`
is determined by the evaluations `f(X_1)`, `f(X_2)`, ... -/
@[simp]
theorem eval₂Hom_X {R : Type u} (c : ℤ →+* S) (f : MvPolynomial R ℤ →+* S) (x : MvPolynomial R ℤ) :
eval₂ c (f ∘ X) x = f x := by
apply MvPolynomial.induction_on x
(fun n => by
rw [hom_C f, eval₂_C]
exact eq_intCast c n)
(fun p q hp hq => by
rw [eval₂_add, hp, hq]
exact (f.map_add _ _).symm)
(fun p n hp => by
rw [eval₂_mul, eval₂_X, hp]
exact (f.map_mul _ _).symm)
/-- Ring homomorphisms out of integer polynomials on a type `σ` are the same as
functions out of the type `σ`. -/
def homEquiv : (MvPolynomial σ ℤ →+* S) ≃ (σ → S) where
toFun f := f ∘ X
invFun f := eval₂Hom (Int.castRingHom S) f
left_inv _ := RingHom.ext <| eval₂Hom_X _ _
right_inv f := funext fun x => by simp only [coe_eval₂Hom, Function.comp_apply, eval₂_X]
end Eval
section DegreeOf
theorem degreeOf_sub_lt {x : σ} {f g : MvPolynomial σ R} {k : ℕ} (h : 0 < k)
(hf : ∀ m : σ →₀ ℕ, m ∈ f.support → k ≤ m x → coeff m f = coeff m g)
(hg : ∀ m : σ →₀ ℕ, m ∈ g.support → k ≤ m x → coeff m f = coeff m g) :
degreeOf x (f - g) < k := by
rw [degreeOf_lt_iff h]
grind [degreeOf_lt_iff, mem_support_iff, coeff_sub]
end DegreeOf
section TotalDegree
@[simp]
theorem totalDegree_neg (a : MvPolynomial σ R) : (-a).totalDegree = a.totalDegree := by
simp only [totalDegree, support_neg]
theorem totalDegree_sub (a b : MvPolynomial σ R) :
(a - b).totalDegree ≤ max a.totalDegree b.totalDegree :=
calc
(a - b).totalDegree = (a + -b).totalDegree := by rw [sub_eq_add_neg]
_ ≤ max a.totalDegree (-b).totalDegree := totalDegree_add a (-b)
_ = max a.totalDegree b.totalDegree := by rw [totalDegree_neg]
theorem totalDegree_sub_C_le (p : MvPolynomial σ R) (r : R) :
totalDegree (p - C r) ≤ totalDegree p :=
(totalDegree_sub _ _).trans_eq <| by rw [totalDegree_C, Nat.max_zero]
end TotalDegree
end CommRing
end MvPolynomial |
.lake/packages/mathlib/Mathlib/Algebra/MvPolynomial/Basic.lean | import Mathlib.Algebra.Algebra.Subalgebra.Lattice
import Mathlib.Algebra.Algebra.Tower
import Mathlib.Algebra.GroupWithZero.Divisibility
import Mathlib.Algebra.MonoidAlgebra.Basic
import Mathlib.Algebra.MonoidAlgebra.NoZeroDivisors
import Mathlib.Algebra.MonoidAlgebra.Support
import Mathlib.Algebra.Regular.Pow
import Mathlib.Data.Finsupp.Antidiagonal
import Mathlib.Data.Finsupp.Order
import Mathlib.Order.SymmDiff
/-!
# Multivariate polynomials
This file defines polynomial rings over a base ring (or even semiring),
with variables from a general type `σ` (which could be infinite).
## Important definitions
Let `R` be a commutative ring (or a semiring) and let `σ` be an arbitrary
type. This file creates the type `MvPolynomial σ R`, which mathematicians
might denote $R[X_i : i \in σ]$. It is the type of multivariate
(a.k.a. multivariable) polynomials, with variables
corresponding to the terms in `σ`, and coefficients in `R`.
### Notation
In the definitions below, we use the following notation:
+ `σ : Type*` (indexing the variables)
+ `R : Type*` `[CommSemiring R]` (the coefficients)
+ `s : σ →₀ ℕ`, a function from `σ` to `ℕ` which is zero away from a finite set.
This will give rise to a monomial in `MvPolynomial σ R` which mathematicians might call `X^s`
+ `a : R`
+ `i : σ`, with corresponding monomial `X i`, often denoted `X_i` by mathematicians
+ `p : MvPolynomial σ R`
### Definitions
* `MvPolynomial σ R` : the type of polynomials with variables of type `σ` and coefficients
in the commutative semiring `R`
* `monomial s a` : the monomial which mathematically would be denoted `a * X^s`
* `C a` : the constant polynomial with value `a`
* `X i` : the degree one monomial corresponding to i; mathematically this might be denoted `Xᵢ`.
* `coeff s p` : the coefficient of `s` in `p`.
## Implementation notes
Recall that if `Y` has a zero, then `X →₀ Y` is the type of functions from `X` to `Y` with finite
support, i.e. such that only finitely many elements of `X` get sent to non-zero terms in `Y`.
The definition of `MvPolynomial σ R` is `(σ →₀ ℕ) →₀ R`; here `σ →₀ ℕ` denotes the space of all
monomials in the variables, and the function to `R` sends a monomial to its coefficient in
the polynomial being represented.
## Tags
polynomial, multivariate polynomial, multivariable polynomial
-/
noncomputable section
open Set Function Finsupp AddMonoidAlgebra
open scoped Pointwise
universe u v w x
variable {R : Type u} {S₁ : Type v} {S₂ : Type w} {S₃ : Type x}
/-- Multivariate polynomial, where `σ` is the index set of the variables and
`R` is the coefficient ring -/
def MvPolynomial (σ : Type*) (R : Type*) [CommSemiring R] :=
AddMonoidAlgebra R (σ →₀ ℕ)
namespace MvPolynomial
variable {σ : Type*} {a a' a₁ a₂ : R} {e : ℕ} {n m : σ} {s : σ →₀ ℕ}
section CommSemiring
section Instances
instance decidableEqMvPolynomial [CommSemiring R] [DecidableEq σ] [DecidableEq R] :
DecidableEq (MvPolynomial σ R) :=
Finsupp.instDecidableEq
instance commSemiring [CommSemiring R] : CommSemiring (MvPolynomial σ R) :=
AddMonoidAlgebra.commSemiring
instance inhabited [CommSemiring R] : Inhabited (MvPolynomial σ R) :=
⟨0⟩
instance distribuMulAction [Monoid R] [CommSemiring S₁] [DistribMulAction R S₁] :
DistribMulAction R (MvPolynomial σ S₁) :=
AddMonoidAlgebra.distribMulAction
instance smulZeroClass [CommSemiring S₁] [SMulZeroClass R S₁] :
SMulZeroClass R (MvPolynomial σ S₁) :=
AddMonoidAlgebra.smulZeroClass
instance faithfulSMul [CommSemiring S₁] [SMulZeroClass R S₁] [FaithfulSMul R S₁] :
FaithfulSMul R (MvPolynomial σ S₁) :=
AddMonoidAlgebra.faithfulSMul
instance module [Semiring R] [CommSemiring S₁] [Module R S₁] : Module R (MvPolynomial σ S₁) :=
AddMonoidAlgebra.module
instance isScalarTower [CommSemiring S₂] [SMul R S₁] [SMulZeroClass R S₂] [SMulZeroClass S₁ S₂]
[IsScalarTower R S₁ S₂] : IsScalarTower R S₁ (MvPolynomial σ S₂) :=
AddMonoidAlgebra.isScalarTower
instance smulCommClass [CommSemiring S₂] [SMulZeroClass R S₂] [SMulZeroClass S₁ S₂]
[SMulCommClass R S₁ S₂] : SMulCommClass R S₁ (MvPolynomial σ S₂) :=
AddMonoidAlgebra.smulCommClass
instance isCentralScalar [CommSemiring S₁] [SMulZeroClass R S₁] [SMulZeroClass Rᵐᵒᵖ S₁]
[IsCentralScalar R S₁] : IsCentralScalar R (MvPolynomial σ S₁) :=
AddMonoidAlgebra.isCentralScalar
instance algebra [CommSemiring R] [CommSemiring S₁] [Algebra R S₁] :
Algebra R (MvPolynomial σ S₁) :=
AddMonoidAlgebra.algebra
instance isScalarTower_right [CommSemiring S₁] [DistribSMul R S₁] [IsScalarTower R S₁ S₁] :
IsScalarTower R (MvPolynomial σ S₁) (MvPolynomial σ S₁) :=
AddMonoidAlgebra.isScalarTower_self _
instance smulCommClass_right [CommSemiring S₁] [DistribSMul R S₁] [SMulCommClass R S₁ S₁] :
SMulCommClass R (MvPolynomial σ S₁) (MvPolynomial σ S₁) :=
AddMonoidAlgebra.smulCommClass_self _
/-- If `R` is a subsingleton, then `MvPolynomial σ R` has a unique element -/
instance unique [CommSemiring R] [Subsingleton R] : Unique (MvPolynomial σ R) :=
AddMonoidAlgebra.unique
end Instances
variable [CommSemiring R] [CommSemiring S₁] {p q : MvPolynomial σ R}
/-- `monomial s a` is the monomial with coefficient `a` and exponents given by `s` -/
def monomial (s : σ →₀ ℕ) : R →ₗ[R] MvPolynomial σ R :=
AddMonoidAlgebra.lsingle s
theorem one_def : (1 : MvPolynomial σ R) = monomial 0 1 := rfl
theorem single_eq_monomial (s : σ →₀ ℕ) (a : R) : Finsupp.single s a = monomial s a :=
rfl
theorem mul_def : p * q = p.sum fun m a => q.sum fun n b => monomial (m + n) (a * b) :=
AddMonoidAlgebra.mul_def ..
/-- `C a` is the constant polynomial with value `a` -/
def C : R →+* MvPolynomial σ R :=
{ singleZeroRingHom with toFun := monomial 0 }
variable (R σ)
@[simp]
theorem algebraMap_eq : algebraMap R (MvPolynomial σ R) = C :=
rfl
variable {R σ}
@[simp]
theorem algebraMap_apply [Algebra R S₁] (r : R) :
algebraMap R (MvPolynomial σ S₁) r = C (algebraMap R S₁ r) :=
rfl
/-- `X n` is the degree `1` monomial $X_n$. -/
def X (n : σ) : MvPolynomial σ R :=
monomial (Finsupp.single n 1) 1
theorem monomial_left_injective {r : R} (hr : r ≠ 0) :
Function.Injective fun s : σ →₀ ℕ => monomial s r :=
Finsupp.single_left_injective hr
@[simp]
theorem monomial_left_inj {s t : σ →₀ ℕ} {r : R} (hr : r ≠ 0) :
monomial s r = monomial t r ↔ s = t :=
Finsupp.single_left_inj hr
theorem C_apply : (C a : MvPolynomial σ R) = monomial 0 a :=
rfl
@[simp]
theorem C_0 : C 0 = (0 : MvPolynomial σ R) := map_zero _
@[simp]
theorem C_1 : C 1 = (1 : MvPolynomial σ R) :=
rfl
theorem C_mul_monomial : C a * monomial s a' = monomial s (a * a') := by
-- Porting note: this `change` feels like defeq abuse, but I can't find the appropriate lemmas
change AddMonoidAlgebra.single _ _ * AddMonoidAlgebra.single _ _ = AddMonoidAlgebra.single _ _
simp [single_mul_single]
@[simp]
theorem C_add : (C (a + a') : MvPolynomial σ R) = C a + C a' :=
Finsupp.single_add _ _ _
@[simp]
theorem C_mul : (C (a * a') : MvPolynomial σ R) = C a * C a' :=
C_mul_monomial.symm
@[simp]
theorem C_pow (a : R) (n : ℕ) : (C (a ^ n) : MvPolynomial σ R) = C a ^ n :=
map_pow _ _ _
theorem C_injective (σ : Type*) (R : Type*) [CommSemiring R] :
Function.Injective (C : R → MvPolynomial σ R) :=
Finsupp.single_injective _
theorem C_surjective {R : Type*} [CommSemiring R] (σ : Type*) [IsEmpty σ] :
Function.Surjective (C : R → MvPolynomial σ R) := by
refine fun p => ⟨p.toFun 0, Finsupp.ext fun a => ?_⟩
simp only [C_apply, ← single_eq_monomial, (Finsupp.ext isEmptyElim (α := σ) : a = 0),
single_eq_same]
rfl
@[simp]
theorem C_inj {σ : Type*} (R : Type*) [CommSemiring R] (r s : R) :
(C r : MvPolynomial σ R) = C s ↔ r = s :=
(C_injective σ R).eq_iff
@[simp] lemma C_eq_zero : (C a : MvPolynomial σ R) = 0 ↔ a = 0 := by rw [← map_zero C, C_inj]
lemma C_ne_zero : (C a : MvPolynomial σ R) ≠ 0 ↔ a ≠ 0 :=
C_eq_zero.ne
instance nontrivial_of_nontrivial (σ : Type*) (R : Type*) [CommSemiring R] [Nontrivial R] :
Nontrivial (MvPolynomial σ R) :=
inferInstanceAs (Nontrivial <| AddMonoidAlgebra R (σ →₀ ℕ))
instance infinite_of_infinite (σ : Type*) (R : Type*) [CommSemiring R] [Infinite R] :
Infinite (MvPolynomial σ R) :=
Infinite.of_injective C (C_injective _ _)
instance infinite_of_nonempty (σ : Type*) (R : Type*) [Nonempty σ] [CommSemiring R]
[Nontrivial R] : Infinite (MvPolynomial σ R) :=
Infinite.of_injective ((fun s : σ →₀ ℕ => monomial s 1) ∘ Finsupp.single (Classical.arbitrary σ))
<| (monomial_left_injective one_ne_zero).comp (Finsupp.single_injective _)
instance [CommSemiring R] [NoZeroDivisors R] : NoZeroDivisors (MvPolynomial σ R) :=
inferInstanceAs (NoZeroDivisors (AddMonoidAlgebra ..))
instance [CommSemiring R] [IsCancelAdd R] [IsCancelMulZero R] :
IsCancelMulZero (MvPolynomial σ R) :=
inferInstanceAs (IsCancelMulZero (AddMonoidAlgebra ..))
/-- The multivariate polynomial ring over an integral domain is an integral domain. -/
instance [CommSemiring R] [IsCancelAdd R] [IsDomain R] : IsDomain (MvPolynomial σ R) where
theorem C_eq_coe_nat (n : ℕ) : (C ↑n : MvPolynomial σ R) = n := by
induction n <;> simp [*]
theorem C_mul' : MvPolynomial.C a * p = a • p :=
(Algebra.smul_def a p).symm
theorem smul_eq_C_mul (p : MvPolynomial σ R) (a : R) : a • p = C a * p :=
C_mul'.symm
theorem C_eq_smul_one : (C a : MvPolynomial σ R) = a • (1 : MvPolynomial σ R) := by
rw [← C_mul', mul_one]
theorem smul_monomial {S₁ : Type*} [SMulZeroClass S₁ R] (r : S₁) :
r • monomial s a = monomial s (r • a) :=
Finsupp.smul_single _ _ _
theorem X_injective [Nontrivial R] : Function.Injective (X : σ → MvPolynomial σ R) :=
(monomial_left_injective one_ne_zero).comp (Finsupp.single_left_injective one_ne_zero)
@[simp]
theorem X_inj [Nontrivial R] (m n : σ) : X m = (X n : MvPolynomial σ R) ↔ m = n :=
X_injective.eq_iff
theorem monomial_pow : monomial s a ^ e = monomial (e • s) (a ^ e) :=
AddMonoidAlgebra.single_pow ..
@[simp]
theorem monomial_mul {s s' : σ →₀ ℕ} {a b : R} :
monomial s a * monomial s' b = monomial (s + s') (a * b) :=
AddMonoidAlgebra.single_mul_single ..
variable (σ R)
/-- `fun s ↦ monomial s 1` as a homomorphism. -/
def monomialOneHom : Multiplicative (σ →₀ ℕ) →* MvPolynomial σ R :=
AddMonoidAlgebra.of _ _
variable {σ R}
@[simp]
theorem monomialOneHom_apply : monomialOneHom R σ s = (monomial s 1 : MvPolynomial σ R) :=
rfl
theorem X_pow_eq_monomial : X n ^ e = monomial (Finsupp.single n e) (1 : R) := by
simp [X, monomial_pow]
theorem monomial_add_single : monomial (s + Finsupp.single n e) a = monomial s a * X n ^ e := by
rw [X_pow_eq_monomial, monomial_mul, mul_one]
theorem monomial_single_add : monomial (Finsupp.single n e + s) a = X n ^ e * monomial s a := by
rw [X_pow_eq_monomial, monomial_mul, one_mul]
theorem C_mul_X_pow_eq_monomial {s : σ} {a : R} {n : ℕ} :
C a * X s ^ n = monomial (Finsupp.single s n) a := by
rw [← zero_add (Finsupp.single s n), monomial_add_single, C_apply]
theorem C_mul_X_eq_monomial {s : σ} {a : R} : C a * X s = monomial (Finsupp.single s 1) a := by
rw [← C_mul_X_pow_eq_monomial, pow_one]
@[simp]
theorem monomial_zero {s : σ →₀ ℕ} : monomial s (0 : R) = 0 :=
Finsupp.single_zero _
@[simp]
theorem monomial_zero' : (monomial (0 : σ →₀ ℕ) : R → MvPolynomial σ R) = C :=
rfl
@[simp]
theorem monomial_eq_zero {s : σ →₀ ℕ} {b : R} : monomial s b = 0 ↔ b = 0 :=
Finsupp.single_eq_zero
@[simp]
theorem sum_monomial_eq {A : Type*} [AddCommMonoid A] {u : σ →₀ ℕ} {r : R} {b : (σ →₀ ℕ) → R → A}
(w : b u 0 = 0) : sum (monomial u r) b = b u r :=
Finsupp.sum_single_index w
@[simp]
theorem sum_C {A : Type*} [AddCommMonoid A] {b : (σ →₀ ℕ) → R → A} (w : b 0 0 = 0) :
sum (C a) b = b 0 a :=
sum_monomial_eq w
theorem monomial_sum_one {α : Type*} (s : Finset α) (f : α → σ →₀ ℕ) :
(monomial (∑ i ∈ s, f i) 1 : MvPolynomial σ R) = ∏ i ∈ s, monomial (f i) 1 :=
map_prod (monomialOneHom R σ) (fun i => Multiplicative.ofAdd (f i)) s
theorem monomial_sum_index {α : Type*} (s : Finset α) (f : α → σ →₀ ℕ) (a : R) :
monomial (∑ i ∈ s, f i) a = C a * ∏ i ∈ s, monomial (f i) 1 := by
rw [← monomial_sum_one, C_mul', ← (monomial _).map_smul, smul_eq_mul, mul_one]
theorem monomial_finsupp_sum_index {α β : Type*} [Zero β] (f : α →₀ β) (g : α → β → σ →₀ ℕ)
(a : R) : monomial (f.sum g) a = C a * f.prod fun a b => monomial (g a b) 1 :=
monomial_sum_index _ _ _
theorem monomial_eq_monomial_iff {α : Type*} (a₁ a₂ : α →₀ ℕ) (b₁ b₂ : R) :
monomial a₁ b₁ = monomial a₂ b₂ ↔ a₁ = a₂ ∧ b₁ = b₂ ∨ b₁ = 0 ∧ b₂ = 0 :=
Finsupp.single_eq_single_iff _ _ _ _
theorem monomial_eq : monomial s a = C a * (s.prod fun n e => X n ^ e : MvPolynomial σ R) := by
simp only [X_pow_eq_monomial, ← monomial_finsupp_sum_index, Finsupp.sum_single]
@[simp]
lemma prod_X_pow_eq_monomial : ∏ x ∈ s.support, X x ^ s x = monomial s (1 : R) := by
simp only [monomial_eq, map_one, one_mul, Finsupp.prod]
@[elab_as_elim]
theorem induction_on_monomial {motive : MvPolynomial σ R → Prop}
(C : ∀ a, motive (C a))
(mul_X : ∀ p n, motive p → motive (p * X n)) : ∀ s a, motive (monomial s a) := by
intro s a
apply @Finsupp.induction σ ℕ _ _ s
· change motive (monomial 0 a)
exact C a
· intro n e p _hpn _he ih
have : ∀ e : ℕ, motive (monomial p a * X n ^ e) := by
intro e
induction e with
| zero => simp [ih]
| succ e e_ih => simp [pow_succ, (mul_assoc _ _ _).symm, mul_X, e_ih]
simp [add_comm, monomial_add_single, this]
/-- Analog of `Polynomial.induction_on'`.
To prove something about mv_polynomials,
it suffices to show the condition is closed under taking sums,
and it holds for monomials. -/
@[elab_as_elim]
theorem induction_on' {P : MvPolynomial σ R → Prop} (p : MvPolynomial σ R)
(monomial : ∀ (u : σ →₀ ℕ) (a : R), P (monomial u a))
(add : ∀ p q : MvPolynomial σ R, P p → P q → P (p + q)) : P p :=
Finsupp.induction p
(suffices P (MvPolynomial.monomial 0 0) by rwa [monomial_zero] at this
show P (MvPolynomial.monomial 0 0) from monomial 0 0)
fun _ _ _ _ha _hb hPf => add _ _ (monomial _ _) hPf
/--
Similar to `MvPolynomial.induction_on` but only a weak form of `h_add` is required.
In particular, this version only requires us to show
that `motive` is closed under addition of nontrivial monomials not present in the support.
-/
@[elab_as_elim]
theorem monomial_add_induction_on {motive : MvPolynomial σ R → Prop} (p : MvPolynomial σ R)
(C : ∀ a, motive (C a))
(monomial_add :
∀ (a : σ →₀ ℕ) (b : R) (f : MvPolynomial σ R),
a ∉ f.support → b ≠ 0 → motive f → motive ((monomial a b) + f)) :
motive p :=
Finsupp.induction p (C_0.rec <| C 0) monomial_add
/--
Similar to `MvPolynomial.induction_on` but only a yet weaker form of `h_add` is required.
In particular, this version only requires us to show
that `motive` is closed under addition of monomials not present in the support
for which `motive` is already known to hold.
-/
theorem induction_on'' {motive : MvPolynomial σ R → Prop} (p : MvPolynomial σ R)
(C : ∀ a, motive (C a))
(monomial_add :
∀ (a : σ →₀ ℕ) (b : R) (f : MvPolynomial σ R),
a ∉ f.support → b ≠ 0 → motive f → motive (monomial a b) →
motive ((monomial a b) + f))
(mul_X : ∀ (p : MvPolynomial σ R) (n : σ), motive p → motive (p * MvPolynomial.X n)) :
motive p :=
monomial_add_induction_on p C fun a b f ha hb hf =>
monomial_add a b f ha hb hf <| induction_on_monomial C mul_X a b
/--
Analog of `Polynomial.induction_on`.
If a property holds for any constant polynomial
and is preserved under addition and multiplication by variables
then it holds for all multivariate polynomials.
-/
@[recursor 5]
theorem induction_on {motive : MvPolynomial σ R → Prop} (p : MvPolynomial σ R)
(C : ∀ a, motive (C a))
(add : ∀ p q, motive p → motive q → motive (p + q))
(mul_X : ∀ p n, motive p → motive (p * X n)) : motive p :=
induction_on'' p C (fun a b f _ha _hb hf hm => add (monomial a b) f hm hf) mul_X
theorem ringHom_ext {A : Type*} [Semiring A] {f g : MvPolynomial σ R →+* A}
(hC : ∀ r, f (C r) = g (C r)) (hX : ∀ i, f (X i) = g (X i)) : f = g := by
refine AddMonoidAlgebra.ringHom_ext' ?_ ?_
-- Porting note (https://github.com/leanprover-community/mathlib4/issues/11041): this has high priority, but Lean still chooses `RingHom.ext`, why?
-- probably because of the type synonym
· ext x
exact hC _
· apply Finsupp.mulHom_ext'; intro x
-- Porting note (https://github.com/leanprover-community/mathlib4/issues/11041): `Finsupp.mulHom_ext'` needs to have increased priority
apply MonoidHom.ext_mnat
exact hX _
/-- See note [partially-applied ext lemmas]. -/
@[ext 1100]
theorem ringHom_ext' {A : Type*} [Semiring A] {f g : MvPolynomial σ R →+* A}
(hC : f.comp C = g.comp C) (hX : ∀ i, f (X i) = g (X i)) : f = g :=
ringHom_ext (RingHom.ext_iff.1 hC) hX
theorem hom_eq_hom [Semiring S₂] (f g : MvPolynomial σ R →+* S₂) (hC : f.comp C = g.comp C)
(hX : ∀ n : σ, f (X n) = g (X n)) (p : MvPolynomial σ R) : f p = g p :=
RingHom.congr_fun (ringHom_ext' hC hX) p
theorem is_id (f : MvPolynomial σ R →+* MvPolynomial σ R) (hC : f.comp C = C)
(hX : ∀ n : σ, f (X n) = X n) (p : MvPolynomial σ R) : f p = p :=
hom_eq_hom f (RingHom.id _) hC hX p
@[ext 1100]
theorem algHom_ext' {A B : Type*} [CommSemiring A] [CommSemiring B] [Algebra R A] [Algebra R B]
{f g : MvPolynomial σ A →ₐ[R] B}
(h₁ :
f.comp (IsScalarTower.toAlgHom R A (MvPolynomial σ A)) =
g.comp (IsScalarTower.toAlgHom R A (MvPolynomial σ A)))
(h₂ : ∀ i, f (X i) = g (X i)) : f = g :=
AlgHom.coe_ringHom_injective (MvPolynomial.ringHom_ext' (congr_arg AlgHom.toRingHom h₁) h₂)
@[ext 1200]
theorem algHom_ext {A : Type*} [Semiring A] [Algebra R A] {f g : MvPolynomial σ R →ₐ[R] A}
(hf : ∀ i : σ, f (X i) = g (X i)) : f = g :=
AddMonoidAlgebra.algHom_ext' (mulHom_ext' fun X : σ => MonoidHom.ext_mnat (hf X))
@[simp]
theorem algHom_C {A : Type*} [Semiring A] [Algebra R A] (f : MvPolynomial σ R →ₐ[R] A) (r : R) :
f (C r) = algebraMap R A r :=
f.commutes r
@[simp]
theorem adjoin_range_X : Algebra.adjoin R (range (X : σ → MvPolynomial σ R)) = ⊤ := by
set S := Algebra.adjoin R (range (X : σ → MvPolynomial σ R))
refine top_unique fun p hp => ?_; clear hp
induction p using MvPolynomial.induction_on with
| C => exact S.algebraMap_mem _
| add p q hp hq => exact S.add_mem hp hq
| mul_X p i hp => exact S.mul_mem hp (Algebra.subset_adjoin <| mem_range_self _)
@[ext]
theorem linearMap_ext {M : Type*} [AddCommMonoid M] [Module R M] {f g : MvPolynomial σ R →ₗ[R] M}
(h : ∀ s, f ∘ₗ monomial s = g ∘ₗ monomial s) : f = g :=
Finsupp.lhom_ext' h
section Support
/-- The finite set of all `m : σ →₀ ℕ` such that `X^m` has a non-zero coefficient. -/
def support (p : MvPolynomial σ R) : Finset (σ →₀ ℕ) :=
Finsupp.support p
theorem finsupp_support_eq_support (p : MvPolynomial σ R) : Finsupp.support p = p.support :=
rfl
theorem support_monomial [h : Decidable (a = 0)] :
(monomial s a).support = if a = 0 then ∅ else {s} := by
rw [← Subsingleton.elim (Classical.decEq R a 0) h]
rfl
theorem support_monomial_subset : (monomial s a).support ⊆ {s} :=
support_single_subset
theorem support_add [DecidableEq σ] : (p + q).support ⊆ p.support ∪ q.support :=
Finsupp.support_add
theorem support_X [Nontrivial R] : (X n : MvPolynomial σ R).support = {Finsupp.single n 1} := by
classical rw [X, support_monomial, if_neg]; exact one_ne_zero
theorem support_X_pow [Nontrivial R] (s : σ) (n : ℕ) :
(X s ^ n : MvPolynomial σ R).support = {Finsupp.single s n} := by
classical
rw [X_pow_eq_monomial, support_monomial, if_neg (one_ne_zero' R)]
@[simp]
theorem support_zero : (0 : MvPolynomial σ R).support = ∅ :=
rfl
theorem support_smul {S₁ : Type*} [SMulZeroClass S₁ R] {a : S₁} {f : MvPolynomial σ R} :
(a • f).support ⊆ f.support :=
Finsupp.support_smul
theorem support_sum {α : Type*} [DecidableEq σ] {s : Finset α} {f : α → MvPolynomial σ R} :
(∑ x ∈ s, f x).support ⊆ s.biUnion fun x => (f x).support :=
Finsupp.support_finset_sum
end Support
section Coeff
/-- The coefficient of the monomial `m` in the multi-variable polynomial `p`. -/
def coeff (m : σ →₀ ℕ) (p : MvPolynomial σ R) : R :=
@DFunLike.coe ((σ →₀ ℕ) →₀ R) _ _ _ p m
@[simp]
theorem mem_support_iff {p : MvPolynomial σ R} {m : σ →₀ ℕ} : m ∈ p.support ↔ p.coeff m ≠ 0 := by
simp [support, coeff]
theorem notMem_support_iff {p : MvPolynomial σ R} {m : σ →₀ ℕ} : m ∉ p.support ↔ p.coeff m = 0 :=
by simp
@[deprecated (since := "2025-05-23")] alias not_mem_support_iff := notMem_support_iff
theorem sum_def {A} [AddCommMonoid A] {p : MvPolynomial σ R} {b : (σ →₀ ℕ) → R → A} :
p.sum b = ∑ m ∈ p.support, b m (p.coeff m) := by simp [support, Finsupp.sum, coeff]
theorem support_mul [DecidableEq σ] (p q : MvPolynomial σ R) :
(p * q).support ⊆ p.support + q.support :=
AddMonoidAlgebra.support_mul p q
lemma disjoint_support_monomial {a : σ →₀ ℕ} {p : MvPolynomial σ R} {s : R}
(ha : a ∉ p.support) (hs : s ≠ 0) : Disjoint (monomial a s).support p.support := by
classical
simpa [support_monomial, hs] using notMem_support_iff.mp ha
@[ext]
theorem ext (p q : MvPolynomial σ R) : (∀ m, coeff m p = coeff m q) → p = q :=
Finsupp.ext
@[simp]
theorem coeff_add (m : σ →₀ ℕ) (p q : MvPolynomial σ R) : coeff m (p + q) = coeff m p + coeff m q :=
add_apply p q m
@[simp]
theorem coeff_smul {S₁ : Type*} [SMulZeroClass S₁ R] (m : σ →₀ ℕ) (C : S₁) (p : MvPolynomial σ R) :
coeff m (C • p) = C • coeff m p :=
smul_apply C p m
@[simp]
theorem coeff_zero (m : σ →₀ ℕ) : coeff m (0 : MvPolynomial σ R) = 0 :=
rfl
@[simp]
theorem coeff_zero_X (i : σ) : coeff 0 (X i : MvPolynomial σ R) = 0 :=
single_eq_of_ne' fun h => by cases Finsupp.single_eq_zero.1 h
@[simp]
theorem coeff_mapRange (g : S₁ → R) (hg : g 0 = 0) (φ : MvPolynomial σ S₁) (m) :
coeff m (mapRange g hg φ) = g (coeff m φ) := by
simp [mapRange, coeff]
/-- `MvPolynomial.coeff m` but promoted to an `AddMonoidHom`. -/
@[simps]
def coeffAddMonoidHom (m : σ →₀ ℕ) : MvPolynomial σ R →+ R where
toFun := coeff m
map_zero' := coeff_zero m
map_add' := coeff_add m
variable (R) in
/-- `MvPolynomial.coeff m` but promoted to a `LinearMap`. -/
@[simps]
def lcoeff (m : σ →₀ ℕ) : MvPolynomial σ R →ₗ[R] R where
toFun := coeff m
map_add' := coeff_add m
map_smul' := coeff_smul m
theorem coeff_sum {X : Type*} (s : Finset X) (f : X → MvPolynomial σ R) (m : σ →₀ ℕ) :
coeff m (∑ x ∈ s, f x) = ∑ x ∈ s, coeff m (f x) :=
map_sum (@coeffAddMonoidHom R σ _ _) _ s
theorem monic_monomial_eq (m) :
monomial m (1 : R) = (m.prod fun n e => X n ^ e : MvPolynomial σ R) := by simp [monomial_eq]
@[simp]
theorem coeff_monomial [DecidableEq σ] (m n) (a) :
coeff m (monomial n a : MvPolynomial σ R) = if n = m then a else 0 :=
Finsupp.single_apply
@[simp]
theorem coeff_C [DecidableEq σ] (m) (a) :
coeff m (C a : MvPolynomial σ R) = if 0 = m then a else 0 :=
Finsupp.single_apply
lemma eq_C_of_isEmpty [IsEmpty σ] (p : MvPolynomial σ R) :
p = C (p.coeff 0) := by
obtain ⟨x, rfl⟩ := C_surjective σ p
simp
theorem coeff_one [DecidableEq σ] (m) : coeff m (1 : MvPolynomial σ R) = if 0 = m then 1 else 0 :=
coeff_C m 1
@[simp]
theorem coeff_zero_C (a) : coeff 0 (C a : MvPolynomial σ R) = a :=
single_eq_same
@[simp]
theorem coeff_zero_one : coeff 0 (1 : MvPolynomial σ R) = 1 :=
coeff_zero_C 1
theorem coeff_X_pow [DecidableEq σ] (i : σ) (m) (k : ℕ) :
coeff m (X i ^ k : MvPolynomial σ R) = if Finsupp.single i k = m then 1 else 0 := by
have := coeff_monomial m (Finsupp.single i k) (1 : R)
rwa [@monomial_eq _ _ (1 : R) (Finsupp.single i k) _, C_1, one_mul, Finsupp.prod_single_index]
at this
exact pow_zero _
theorem coeff_X' [DecidableEq σ] (i : σ) (m) :
coeff m (X i : MvPolynomial σ R) = if Finsupp.single i 1 = m then 1 else 0 := by
rw [← coeff_X_pow, pow_one]
@[simp]
theorem coeff_X (i : σ) : coeff (Finsupp.single i 1) (X i : MvPolynomial σ R) = 1 := by
classical rw [coeff_X', if_pos rfl]
@[simp]
theorem coeff_C_mul (m) (a : R) (p : MvPolynomial σ R) : coeff m (C a * p) = a * coeff m p := by
classical
rw [mul_def, sum_C]
· simp +contextual [sum_def, coeff_sum]
simp
theorem coeff_mul [DecidableEq σ] (p q : MvPolynomial σ R) (n : σ →₀ ℕ) :
coeff n (p * q) = ∑ x ∈ Finset.antidiagonal n, coeff x.1 p * coeff x.2 q :=
AddMonoidAlgebra.mul_apply_antidiagonal p q _ _ Finset.mem_antidiagonal
@[simp]
theorem coeff_mul_monomial (m) (s : σ →₀ ℕ) (r : R) (p : MvPolynomial σ R) :
coeff (m + s) (p * monomial s r) = coeff m p * r :=
AddMonoidAlgebra.mul_single_apply_aux fun _a _ => add_left_inj _
@[simp]
theorem coeff_monomial_mul (m) (s : σ →₀ ℕ) (r : R) (p : MvPolynomial σ R) :
coeff (s + m) (monomial s r * p) = r * coeff m p :=
AddMonoidAlgebra.single_mul_apply_aux fun _a _ => add_right_inj _
@[simp]
theorem coeff_mul_X (m) (s : σ) (p : MvPolynomial σ R) :
coeff (m + Finsupp.single s 1) (p * X s) = coeff m p :=
(coeff_mul_monomial _ _ _ _).trans (mul_one _)
@[simp]
theorem coeff_X_mul (m) (s : σ) (p : MvPolynomial σ R) :
coeff (Finsupp.single s 1 + m) (X s * p) = coeff m p :=
(coeff_monomial_mul _ _ _ _).trans (one_mul _)
lemma coeff_single_X_pow [DecidableEq σ] (s s' : σ) (n n' : ℕ) :
(X (R := R) s ^ n).coeff (Finsupp.single s' n')
= if s = s' ∧ n = n' ∨ n = 0 ∧ n' = 0 then 1 else 0 := by
simp only [coeff_X_pow, single_eq_single_iff]
@[simp]
lemma coeff_single_X [DecidableEq σ] (s s' : σ) (n : ℕ) :
(X s).coeff (R := R) (Finsupp.single s' n) = if n = 1 ∧ s = s' then 1 else 0 := by
simpa [eq_comm, and_comm] using coeff_single_X_pow s s' 1 n
@[simp]
theorem support_mul_X (s : σ) (p : MvPolynomial σ R) :
(p * X s).support = p.support.map (addRightEmbedding (Finsupp.single s 1)) :=
AddMonoidAlgebra.support_mul_single p _ (by simp) _
@[simp]
theorem support_X_mul (s : σ) (p : MvPolynomial σ R) :
(X s * p).support = p.support.map (addLeftEmbedding (Finsupp.single s 1)) :=
AddMonoidAlgebra.support_single_mul p _ (by simp) _
@[simp]
theorem support_smul_eq {S₁ : Type*} [Semiring S₁] [Module S₁ R] [NoZeroSMulDivisors S₁ R] {a : S₁}
(h : a ≠ 0) (p : MvPolynomial σ R) : (a • p).support = p.support :=
Finsupp.support_smul_eq h
theorem support_sdiff_support_subset_support_add [DecidableEq σ] (p q : MvPolynomial σ R) :
p.support \ q.support ⊆ (p + q).support := by
intro m hm
simp only [Classical.not_not, mem_support_iff, Finset.mem_sdiff, Ne] at hm
simp [hm.2, hm.1]
open scoped symmDiff in
theorem support_symmDiff_support_subset_support_add [DecidableEq σ] (p q : MvPolynomial σ R) :
p.support ∆ q.support ⊆ (p + q).support := by
rw [symmDiff_def, Finset.sup_eq_union]
apply Finset.union_subset
· exact support_sdiff_support_subset_support_add p q
· rw [add_comm]
exact support_sdiff_support_subset_support_add q p
theorem coeff_mul_monomial' (m) (s : σ →₀ ℕ) (r : R) (p : MvPolynomial σ R) :
coeff m (p * monomial s r) = if s ≤ m then coeff (m - s) p * r else 0 := by
classical
split_ifs with h
· conv_rhs => rw [← coeff_mul_monomial _ s]
rw [tsub_add_cancel_of_le h]
· contrapose! h
rw [← mem_support_iff] at h
obtain ⟨j, -, rfl⟩ : ∃ j ∈ support p, j + s = m := by
simpa [Finset.mem_add]
using Finset.add_subset_add_left support_monomial_subset <| support_mul _ _ h
exact le_add_left le_rfl
theorem coeff_monomial_mul' (m) (s : σ →₀ ℕ) (r : R) (p : MvPolynomial σ R) :
coeff m (monomial s r * p) = if s ≤ m then r * coeff (m - s) p else 0 := by
-- note that if we allow `R` to be non-commutative we will have to duplicate the proof above.
rw [mul_comm, mul_comm r]
exact coeff_mul_monomial' _ _ _ _
theorem coeff_mul_X' [DecidableEq σ] (m) (s : σ) (p : MvPolynomial σ R) :
coeff m (p * X s) = if s ∈ m.support then coeff (m - Finsupp.single s 1) p else 0 := by
refine (coeff_mul_monomial' _ _ _ _).trans ?_
simp_rw [Finsupp.single_le_iff, Finsupp.mem_support_iff, Nat.succ_le_iff, pos_iff_ne_zero,
mul_one]
theorem coeff_X_mul' [DecidableEq σ] (m) (s : σ) (p : MvPolynomial σ R) :
coeff m (X s * p) = if s ∈ m.support then coeff (m - Finsupp.single s 1) p else 0 := by
refine (coeff_monomial_mul' _ _ _ _).trans ?_
simp_rw [Finsupp.single_le_iff, Finsupp.mem_support_iff, Nat.succ_le_iff, pos_iff_ne_zero,
one_mul]
theorem eq_zero_iff {p : MvPolynomial σ R} : p = 0 ↔ ∀ d, coeff d p = 0 := by
rw [MvPolynomial.ext_iff]
simp only [coeff_zero]
theorem ne_zero_iff {p : MvPolynomial σ R} : p ≠ 0 ↔ ∃ d, coeff d p ≠ 0 := by
rw [Ne, eq_zero_iff]
push_neg
rfl
@[simp]
theorem X_ne_zero [Nontrivial R] (s : σ) :
X (R := R) s ≠ 0 := by
rw [ne_zero_iff]
use Finsupp.single s 1
simp only [coeff_X, ne_eq, one_ne_zero, not_false_eq_true]
@[simp]
theorem support_eq_empty {p : MvPolynomial σ R} : p.support = ∅ ↔ p = 0 :=
Finsupp.support_eq_empty
@[simp]
lemma support_nonempty {p : MvPolynomial σ R} : p.support.Nonempty ↔ p ≠ 0 := by
rw [Finset.nonempty_iff_ne_empty, ne_eq, support_eq_empty]
theorem exists_coeff_ne_zero {p : MvPolynomial σ R} (h : p ≠ 0) : ∃ d, coeff d p ≠ 0 :=
ne_zero_iff.mp h
theorem C_dvd_iff_dvd_coeff (r : R) (φ : MvPolynomial σ R) : C r ∣ φ ↔ ∀ i, r ∣ φ.coeff i := by
constructor
· rintro ⟨φ, rfl⟩ c
rw [coeff_C_mul]
apply dvd_mul_right
· intro h
choose C hc using h
classical
let c' : (σ →₀ ℕ) → R := fun i => if i ∈ φ.support then C i else 0
let ψ : MvPolynomial σ R := ∑ i ∈ φ.support, monomial i (c' i)
use ψ
apply MvPolynomial.ext
intro i
simp only [ψ, c', coeff_C_mul, coeff_sum, coeff_monomial, Finset.sum_ite_eq']
split_ifs with hi
· rw [hc]
· rw [notMem_support_iff] at hi
rwa [mul_zero]
@[simp] lemma isRegular_X : IsRegular (X n : MvPolynomial σ R) := by
suffices IsLeftRegular (X n : MvPolynomial σ R) from
⟨this, this.right_of_commute <| Commute.all _⟩
intro P Q (hPQ : (X n) * P = (X n) * Q)
ext i
rw [← coeff_X_mul i n P, hPQ, coeff_X_mul i n Q]
@[simp] lemma isRegular_X_pow (k : ℕ) : IsRegular (X n ^ k : MvPolynomial σ R) := isRegular_X.pow k
@[simp] lemma isRegular_prod_X (s : Finset σ) :
IsRegular (∏ n ∈ s, X n : MvPolynomial σ R) :=
IsRegular.prod fun _ _ ↦ isRegular_X
/-- The finset of nonzero coefficients of a multivariate polynomial. -/
def coeffs (p : MvPolynomial σ R) : Finset R :=
letI := Classical.decEq R
Finset.image p.coeff p.support
@[simp]
lemma coeffs_zero : coeffs (0 : MvPolynomial σ R) = ∅ :=
rfl
lemma coeffs_one : coeffs (1 : MvPolynomial σ R) ⊆ {1} := by
classical
rw [coeffs, Finset.image_subset_iff]
simp_all [coeff_one]
@[nontriviality]
lemma coeffs_eq_empty_of_subsingleton [Subsingleton R] (p : MvPolynomial σ R) : p.coeffs = ∅ := by
simpa [coeffs] using Subsingleton.eq_zero p
@[simp]
lemma coeffs_one_of_nontrivial [Nontrivial R] : coeffs (1 : MvPolynomial σ R) = {1} := by
apply Finset.Subset.antisymm coeffs_one
simp only [coeffs, Finset.singleton_subset_iff, Finset.mem_image]
exact ⟨0, by simp⟩
lemma mem_coeffs_iff {p : MvPolynomial σ R} {c : R} :
c ∈ p.coeffs ↔ ∃ n ∈ p.support, c = p.coeff n := by
simp [coeffs, eq_comm, (Finset.mem_image)]
lemma coeff_mem_coeffs {p : MvPolynomial σ R} (m : σ →₀ ℕ)
(h : p.coeff m ≠ 0) : p.coeff m ∈ p.coeffs :=
letI := Classical.decEq R
Finset.mem_image_of_mem p.coeff (mem_support_iff.mpr h)
lemma zero_notMem_coeffs (p : MvPolynomial σ R) : 0 ∉ p.coeffs := by
intro hz
obtain ⟨n, hnsupp, hn⟩ := mem_coeffs_iff.mp hz
exact (mem_support_iff.mp hnsupp) hn.symm
@[deprecated (since := "2025-05-23")] alias zero_not_mem_coeffs := zero_notMem_coeffs
lemma coeffs_C [DecidableEq R] (r : R) : (C (σ := σ) r).coeffs = if r = 0 then ∅ else {r} := by
classical
aesop (add simp mem_coeffs_iff)
lemma coeffs_C_subset (r : R) : (C (σ := σ) r).coeffs ⊆ {r} := by
classical
rw [coeffs_C]
split <;> simp
@[simp]
lemma coeffs_mul_X (p : MvPolynomial σ R) (n : σ) : (p * X n).coeffs = p.coeffs := by
classical
aesop (add simp mem_coeffs_iff)
@[simp]
lemma coeffs_X_mul (p : MvPolynomial σ R) (n : σ) : (X n * p).coeffs = p.coeffs := by
classical
aesop (add simp mem_coeffs_iff)
lemma coeffs_add [DecidableEq R] {p q : MvPolynomial σ R} (h : Disjoint p.support q.support) :
(p + q).coeffs = p.coeffs ∪ q.coeffs := by
ext r
simp only [mem_coeffs_iff, mem_support_iff, coeff_add, ne_eq, Finset.mem_union]
have hl (n : σ →₀ ℕ) (hne : p.coeff n ≠ 0) : q.coeff n = 0 :=
notMem_support_iff.mp <| h.notMem_of_mem_left_finset (mem_support_iff.mpr hne)
have hr (n : σ →₀ ℕ) (hne : q.coeff n ≠ 0) : p.coeff n = 0 :=
notMem_support_iff.mp <| h.notMem_of_mem_right_finset (mem_support_iff.mpr hne)
have hor (n) (h : ¬coeff n p + coeff n q = 0) : coeff n p ≠ 0 ∨ coeff n q ≠ 0 := by
by_cases hp : coeff n p = 0 <;> simp_all
refine ⟨fun ⟨n, hn1, hn2⟩ ↦ ?_, ?_⟩
· obtain (h | h) := hor n hn1
· exact Or.inl ⟨n, by simp [h, hn2, hl n h]⟩
· exact Or.inr ⟨n, by simp [h, hn2, hr n h]⟩
· rintro (⟨n, hn, rfl⟩ | ⟨n, hn, rfl⟩)
· exact ⟨n, by simp [hl n hn, hn]⟩
· exact ⟨n, by simp [hr n hn, hn]⟩
end Coeff
section ConstantCoeff
/-- `constantCoeff p` returns the constant term of the polynomial `p`, defined as `coeff 0 p`.
This is a ring homomorphism.
-/
def constantCoeff : MvPolynomial σ R →+* R where
toFun := coeff 0
map_one' := by simp
map_mul' := by classical simp [coeff_mul]
map_zero' := coeff_zero _
map_add' := coeff_add _
theorem constantCoeff_eq : (constantCoeff : MvPolynomial σ R → R) = coeff 0 :=
rfl
variable (σ) in
@[simp]
theorem constantCoeff_C (r : R) : constantCoeff (C r : MvPolynomial σ R) = r := by
classical simp [constantCoeff_eq]
variable (R) in
@[simp]
theorem constantCoeff_X (i : σ) : constantCoeff (X i : MvPolynomial σ R) = 0 := by
simp [constantCoeff_eq]
@[simp]
theorem constantCoeff_smul {R : Type*} [SMulZeroClass R S₁] (a : R) (f : MvPolynomial σ S₁) :
constantCoeff (a • f) = a • constantCoeff f :=
rfl
theorem constantCoeff_monomial [DecidableEq σ] (d : σ →₀ ℕ) (r : R) :
constantCoeff (monomial d r) = if d = 0 then r else 0 := by
rw [constantCoeff_eq, coeff_monomial]
variable (σ R)
@[simp]
theorem constantCoeff_comp_C : constantCoeff.comp (C : R →+* MvPolynomial σ R) = RingHom.id R := by
ext x
exact constantCoeff_C σ x
theorem constantCoeff_comp_algebraMap :
constantCoeff.comp (algebraMap R (MvPolynomial σ R)) = RingHom.id R :=
constantCoeff_comp_C _ _
end ConstantCoeff
section AsSum
@[simp]
theorem support_sum_monomial_coeff (p : MvPolynomial σ R) :
(∑ v ∈ p.support, monomial v (coeff v p)) = p :=
Finsupp.sum_single p
theorem as_sum (p : MvPolynomial σ R) : p = ∑ v ∈ p.support, monomial v (coeff v p) :=
(support_sum_monomial_coeff p).symm
end AsSum
section coeffsIn
variable {R S σ : Type*} [CommSemiring R] [CommSemiring S]
section Module
variable [Module R S] {M N : Submodule R S} {p : MvPolynomial σ S} {s : σ} {i : σ →₀ ℕ} {x : S}
{n : ℕ}
variable (σ M) in
/-- The `R`-submodule of multivariate polynomials whose coefficients lie in a `R`-submodule `M`. -/
@[simps]
def coeffsIn : Submodule R (MvPolynomial σ S) where
carrier := {p | ∀ i, p.coeff i ∈ M}
add_mem' := by simp+contextual [add_mem]
zero_mem' := by simp
smul_mem' := by simp+contextual [Submodule.smul_mem]
lemma mem_coeffsIn : p ∈ coeffsIn σ M ↔ ∀ i, p.coeff i ∈ M := .rfl
@[simp]
lemma monomial_mem_coeffsIn : monomial i x ∈ coeffsIn σ M ↔ x ∈ M := by
classical
simp only [mem_coeffsIn, coeff_monomial]
exact ⟨fun h ↦ by simpa using h i, fun hs j ↦ by split <;> simp [hs]⟩
@[simp]
lemma C_mem_coeffsIn : C x ∈ coeffsIn σ M ↔ x ∈ M := by simpa using monomial_mem_coeffsIn (i := 0)
@[simp]
lemma one_coeffsIn : 1 ∈ coeffsIn σ M ↔ 1 ∈ M := by simpa using C_mem_coeffsIn (x := (1 : S))
@[simp]
lemma mul_monomial_mem_coeffsIn : p * monomial i 1 ∈ coeffsIn σ M ↔ p ∈ coeffsIn σ M := by
classical
simp only [mem_coeffsIn, coeff_mul_monomial']
constructor
· rintro hp j
simpa using hp (j + i)
· rintro hp i
split <;> simp [hp]
@[simp]
lemma monomial_mul_mem_coeffsIn : monomial i 1 * p ∈ coeffsIn σ M ↔ p ∈ coeffsIn σ M := by
simp [mul_comm]
@[simp]
lemma mul_X_mem_coeffsIn : p * X s ∈ coeffsIn σ M ↔ p ∈ coeffsIn σ M := by
simpa [-mul_monomial_mem_coeffsIn] using mul_monomial_mem_coeffsIn (i := .single s 1)
@[simp]
lemma X_mul_mem_coeffsIn : X s * p ∈ coeffsIn σ M ↔ p ∈ coeffsIn σ M := by simp [mul_comm]
variable (M) in
lemma coeffsIn_eq_span_monomial : coeffsIn σ M = .span R {monomial i m | (m ∈ M) (i : σ →₀ ℕ)} := by
classical
refine le_antisymm ?_ <| Submodule.span_le.2 ?_
· rintro p hp
rw [p.as_sum]
exact sum_mem fun i hi ↦ Submodule.subset_span ⟨_, hp i, _, rfl⟩
· rintro _ ⟨m, hm, s, n, rfl⟩ i
simp
split <;> simp [hm]
lemma coeffsIn_le {N : Submodule R (MvPolynomial σ S)} :
coeffsIn σ M ≤ N ↔ ∀ m ∈ M, ∀ i, monomial i m ∈ N := by
simp [coeffsIn_eq_span_monomial, Submodule.span_le, Set.subset_def,
forall_swap (α := MvPolynomial σ S)]
lemma mem_coeffsIn_iff_coeffs_subset : p ∈ coeffsIn σ M ↔ (p.coeffs : Set S) ⊆ M := by
simp only [mem_coeffsIn, coeffs, Finset.coe_image, image_subset_iff]
refine ⟨fun h x _ ↦ h x, fun h i ↦ ?_⟩
by_cases hp : i ∈ p.support
· exact h hp
· convert M.zero_mem
simpa using hp
end Module
section Algebra
variable [Algebra R S] {M : Submodule R S}
lemma coeffsIn_mul (M N : Submodule R S) : coeffsIn σ (M * N) = coeffsIn σ M * coeffsIn σ N := by
classical
refine le_antisymm (coeffsIn_le.2 ?_) ?_
· intro r hr s
induction hr using Submodule.mul_induction_on' with
| mem_mul_mem m hm n hn =>
rw [← add_zero s, ← monomial_mul]
apply Submodule.mul_mem_mul <;> simpa
| add x _ y _ hx hy =>
simpa [map_add] using add_mem hx hy
· rw [Submodule.mul_le]
intro x hx y hy k
rw [MvPolynomial.coeff_mul]
exact sum_mem fun c hc ↦ Submodule.mul_mem_mul (hx _) (hy _)
lemma coeffsIn_pow : ∀ {n}, n ≠ 0 → ∀ M : Submodule R S, coeffsIn σ (M ^ n) = coeffsIn σ M ^ n
| 1, _, M => by simp
| n + 2, _, M => by rw [pow_succ, coeffsIn_mul, coeffsIn_pow, ← pow_succ]; exact n.succ_ne_zero
lemma le_coeffsIn_pow : ∀ {n}, coeffsIn σ M ^ n ≤ coeffsIn σ (M ^ n)
| 0 => by simpa using ⟨1, map_one _⟩
| n + 1 => (coeffsIn_pow n.succ_ne_zero _).ge
end Algebra
end coeffsIn
end CommSemiring
end MvPolynomial |
.lake/packages/mathlib/Mathlib/Algebra/MvPolynomial/Counit.lean | import Mathlib.Algebra.MvPolynomial.Eval
/-!
## Counit morphisms for multivariate polynomials
One may consider the ring of multivariate polynomials `MvPolynomial A R` with coefficients in `R`
and variables indexed by `A`. If `A` is not just a type, but an algebra over `R`,
then there is a natural surjective algebra homomorphism `MvPolynomial A R →ₐ[R] A`
obtained by `X a ↦ a`.
### Main declarations
* `MvPolynomial.ACounit R A` is the natural surjective algebra homomorphism
`MvPolynomial A R →ₐ[R] A` obtained by `X a ↦ a`
* `MvPolynomial.counit` is an “absolute” variant with `R = ℤ`
* `MvPolynomial.counitNat` is an “absolute” variant with `R = ℕ`
-/
namespace MvPolynomial
open Function
variable (A B R : Type*) [CommSemiring A] [CommSemiring B] [CommRing R] [Algebra A B]
/-- `MvPolynomial.ACounit A B` is the natural surjective algebra homomorphism
`MvPolynomial B A →ₐ[A] B` obtained by `X a ↦ a`.
See `MvPolynomial.counit` for the “absolute” variant with `A = ℤ`,
and `MvPolynomial.counitNat` for the “absolute” variant with `A = ℕ`. -/
noncomputable def ACounit : MvPolynomial B A →ₐ[A] B :=
aeval id
variable {B}
@[simp]
theorem ACounit_X (b : B) : ACounit A B (X b) = b :=
aeval_X _ b
variable {A} (B)
theorem ACounit_C (a : A) : ACounit A B (C a) = algebraMap A B a :=
aeval_C _ a
variable (A)
theorem ACounit_surjective : Surjective (ACounit A B) := fun b => ⟨X b, ACounit_X A b⟩
/-- `MvPolynomial.counit R` is the natural surjective ring homomorphism
`MvPolynomial R ℤ →+* R` obtained by `X r ↦ r`.
See `MvPolynomial.ACounit` for a “relative” variant for algebras over a base ring,
and `MvPolynomial.counitNat` for the “absolute” variant with `R = ℕ`. -/
noncomputable def counit : MvPolynomial R ℤ →+* R :=
(ACounit ℤ R).toRingHom
/-- `MvPolynomial.counitNat A` is the natural surjective ring homomorphism
`MvPolynomial A ℕ →+* A` obtained by `X a ↦ a`.
See `MvPolynomial.ACounit` for a “relative” variant for algebras over a base ring
and `MvPolynomial.counit` for the “absolute” variant with `A = ℤ`. -/
noncomputable def counitNat : MvPolynomial A ℕ →+* A :=
ACounit ℕ A
theorem counit_surjective : Surjective (counit R) :=
ACounit_surjective ℤ R
theorem counitNat_surjective : Surjective (counitNat A) :=
ACounit_surjective ℕ A
theorem counit_C (n : ℤ) : counit R (C n) = n :=
ACounit_C _ _
theorem counitNat_C (n : ℕ) : counitNat A (C n) = n :=
ACounit_C _ _
variable {R A}
@[simp]
theorem counit_X (r : R) : counit R (X r) = r :=
ACounit_X _ _
@[simp]
theorem counitNat_X (a : A) : counitNat A (X a) = a :=
ACounit_X _ _
end MvPolynomial |
.lake/packages/mathlib/Mathlib/Algebra/MvPolynomial/Division.lean | import Mathlib.Algebra.MonoidAlgebra.Division
import Mathlib.Algebra.MvPolynomial.Basic
/-!
# Division of `MvPolynomial` by monomials
## Main definitions
* `MvPolynomial.divMonomial x s`: divides `x` by the monomial `MvPolynomial.monomial 1 s`
* `MvPolynomial.modMonomial x s`: the remainder upon dividing `x` by the monomial
`MvPolynomial.monomial 1 s`.
## Main results
* `MvPolynomial.divMonomial_add_modMonomial`, `MvPolynomial.modMonomial_add_divMonomial`:
`divMonomial` and `modMonomial` are well-behaved as quotient and remainder operators.
## Implementation notes
Where possible, the results in this file should be first proved in the generality of
`AddMonoidAlgebra`, and then the versions specialized to `MvPolynomial` proved in terms of these.
-/
variable {σ R : Type*} [CommSemiring R]
namespace MvPolynomial
section CopiedDeclarations
/-! Please ensure the declarations in this section are direct translations of `AddMonoidAlgebra`
results. -/
/-- Divide by `monomial 1 s`, discarding terms not divisible by this. -/
noncomputable def divMonomial (p : MvPolynomial σ R) (s : σ →₀ ℕ) : MvPolynomial σ R :=
AddMonoidAlgebra.divOf p s
local infixl:70 " /ᵐᵒⁿᵒᵐⁱᵃˡ " => divMonomial
@[simp]
theorem coeff_divMonomial (s : σ →₀ ℕ) (x : MvPolynomial σ R) (s' : σ →₀ ℕ) :
coeff s' (x /ᵐᵒⁿᵒᵐⁱᵃˡ s) = coeff (s + s') x :=
rfl
@[simp]
theorem support_divMonomial (s : σ →₀ ℕ) (x : MvPolynomial σ R) :
(x /ᵐᵒⁿᵒᵐⁱᵃˡ s).support = x.support.preimage _ (add_right_injective s).injOn :=
rfl
@[simp]
theorem zero_divMonomial (s : σ →₀ ℕ) : (0 : MvPolynomial σ R) /ᵐᵒⁿᵒᵐⁱᵃˡ s = 0 :=
AddMonoidAlgebra.zero_divOf _
theorem divMonomial_zero (x : MvPolynomial σ R) : x /ᵐᵒⁿᵒᵐⁱᵃˡ 0 = x :=
x.divOf_zero
theorem add_divMonomial (x y : MvPolynomial σ R) (s : σ →₀ ℕ) :
(x + y) /ᵐᵒⁿᵒᵐⁱᵃˡ s = x /ᵐᵒⁿᵒᵐⁱᵃˡ s + y /ᵐᵒⁿᵒᵐⁱᵃˡ s :=
map_add (N := _ →₀ _) _ _ _
theorem divMonomial_add (a b : σ →₀ ℕ) (x : MvPolynomial σ R) :
x /ᵐᵒⁿᵒᵐⁱᵃˡ (a + b) = x /ᵐᵒⁿᵒᵐⁱᵃˡ a /ᵐᵒⁿᵒᵐⁱᵃˡ b :=
x.divOf_add _ _
@[simp]
theorem divMonomial_monomial_mul (a : σ →₀ ℕ) (x : MvPolynomial σ R) :
monomial a 1 * x /ᵐᵒⁿᵒᵐⁱᵃˡ a = x :=
x.of'_mul_divOf _
@[simp]
theorem divMonomial_mul_monomial (a : σ →₀ ℕ) (x : MvPolynomial σ R) :
x * monomial a 1 /ᵐᵒⁿᵒᵐⁱᵃˡ a = x :=
x.mul_of'_divOf _
@[simp]
theorem divMonomial_monomial (a : σ →₀ ℕ) : monomial a 1 /ᵐᵒⁿᵒᵐⁱᵃˡ a = (1 : MvPolynomial σ R) :=
AddMonoidAlgebra.of'_divOf _
/-- The remainder upon division by `monomial 1 s`. -/
noncomputable def modMonomial (x : MvPolynomial σ R) (s : σ →₀ ℕ) : MvPolynomial σ R :=
x.modOf s
local infixl:70 " %ᵐᵒⁿᵒᵐⁱᵃˡ " => modMonomial
@[simp]
theorem coeff_modMonomial_of_not_le {s' s : σ →₀ ℕ} (x : MvPolynomial σ R) (h : ¬s ≤ s') :
coeff s' (x %ᵐᵒⁿᵒᵐⁱᵃˡ s) = coeff s' x :=
x.modOf_apply_of_not_exists_add s s'
(by
rintro ⟨d, rfl⟩
exact h le_self_add)
@[simp]
theorem coeff_modMonomial_of_le {s' s : σ →₀ ℕ} (x : MvPolynomial σ R) (h : s ≤ s') :
coeff s' (x %ᵐᵒⁿᵒᵐⁱᵃˡ s) = 0 :=
x.modOf_apply_of_exists_add _ _ <| exists_add_of_le h
@[simp]
theorem monomial_mul_modMonomial (s : σ →₀ ℕ) (x : MvPolynomial σ R) :
monomial s 1 * x %ᵐᵒⁿᵒᵐⁱᵃˡ s = 0 :=
x.of'_mul_modOf _
@[simp]
theorem mul_monomial_modMonomial (s : σ →₀ ℕ) (x : MvPolynomial σ R) :
x * monomial s 1 %ᵐᵒⁿᵒᵐⁱᵃˡ s = 0 :=
x.mul_of'_modOf _
@[simp]
theorem monomial_modMonomial (s : σ →₀ ℕ) : monomial s (1 : R) %ᵐᵒⁿᵒᵐⁱᵃˡ s = 0 :=
AddMonoidAlgebra.of'_modOf _
theorem divMonomial_add_modMonomial (x : MvPolynomial σ R) (s : σ →₀ ℕ) :
monomial s 1 * (x /ᵐᵒⁿᵒᵐⁱᵃˡ s) + x %ᵐᵒⁿᵒᵐⁱᵃˡ s = x :=
AddMonoidAlgebra.divOf_add_modOf x s
theorem modMonomial_add_divMonomial (x : MvPolynomial σ R) (s : σ →₀ ℕ) :
x %ᵐᵒⁿᵒᵐⁱᵃˡ s + monomial s 1 * (x /ᵐᵒⁿᵒᵐⁱᵃˡ s) = x :=
AddMonoidAlgebra.modOf_add_divOf x s
theorem monomial_one_dvd_iff_modMonomial_eq_zero {i : σ →₀ ℕ} {x : MvPolynomial σ R} :
monomial i (1 : R) ∣ x ↔ x %ᵐᵒⁿᵒᵐⁱᵃˡ i = 0 :=
AddMonoidAlgebra.of'_dvd_iff_modOf_eq_zero
end CopiedDeclarations
section XLemmas
local infixl:70 " /ᵐᵒⁿᵒᵐⁱᵃˡ " => divMonomial
local infixl:70 " %ᵐᵒⁿᵒᵐⁱᵃˡ " => modMonomial
@[simp]
theorem X_mul_divMonomial (i : σ) (x : MvPolynomial σ R) :
X i * x /ᵐᵒⁿᵒᵐⁱᵃˡ Finsupp.single i 1 = x :=
divMonomial_monomial_mul _ _
@[simp]
theorem X_divMonomial (i : σ) : (X i : MvPolynomial σ R) /ᵐᵒⁿᵒᵐⁱᵃˡ Finsupp.single i 1 = 1 :=
divMonomial_monomial (Finsupp.single i 1)
@[simp]
theorem mul_X_divMonomial (x : MvPolynomial σ R) (i : σ) :
x * X i /ᵐᵒⁿᵒᵐⁱᵃˡ Finsupp.single i 1 = x :=
divMonomial_mul_monomial _ _
@[simp]
theorem X_mul_modMonomial (i : σ) (x : MvPolynomial σ R) :
X i * x %ᵐᵒⁿᵒᵐⁱᵃˡ Finsupp.single i 1 = 0 :=
monomial_mul_modMonomial _ _
@[simp]
theorem mul_X_modMonomial (x : MvPolynomial σ R) (i : σ) :
x * X i %ᵐᵒⁿᵒᵐⁱᵃˡ Finsupp.single i 1 = 0 :=
mul_monomial_modMonomial _ _
@[simp]
theorem modMonomial_X (i : σ) : (X i : MvPolynomial σ R) %ᵐᵒⁿᵒᵐⁱᵃˡ Finsupp.single i 1 = 0 :=
monomial_modMonomial _
theorem divMonomial_add_modMonomial_single (x : MvPolynomial σ R) (i : σ) :
X i * (x /ᵐᵒⁿᵒᵐⁱᵃˡ Finsupp.single i 1) + x %ᵐᵒⁿᵒᵐⁱᵃˡ Finsupp.single i 1 = x :=
divMonomial_add_modMonomial _ _
theorem modMonomial_add_divMonomial_single (x : MvPolynomial σ R) (i : σ) :
x %ᵐᵒⁿᵒᵐⁱᵃˡ Finsupp.single i 1 + X i * (x /ᵐᵒⁿᵒᵐⁱᵃˡ Finsupp.single i 1) = x :=
modMonomial_add_divMonomial _ _
theorem X_dvd_iff_modMonomial_eq_zero {i : σ} {x : MvPolynomial σ R} :
X i ∣ x ↔ x %ᵐᵒⁿᵒᵐⁱᵃˡ Finsupp.single i 1 = 0 :=
monomial_one_dvd_iff_modMonomial_eq_zero
end XLemmas
/-! ### Some results about dvd (`∣`) on `monomial` and `X` -/
theorem monomial_dvd_monomial {r s : R} {i j : σ →₀ ℕ} :
monomial i r ∣ monomial j s ↔ (s = 0 ∨ i ≤ j) ∧ r ∣ s := by
constructor
· rintro ⟨x, hx⟩
rw [MvPolynomial.ext_iff] at hx
have hj := hx j
have hi := hx i
classical
simp_rw [coeff_monomial, if_pos] at hj hi
simp_rw [coeff_monomial_mul'] at hi hj
split_ifs at hj with hi
· exact ⟨Or.inr hi, _, hj⟩
· exact ⟨Or.inl hj, hj.symm ▸ dvd_zero _⟩
· rintro ⟨h | hij, d, rfl⟩
· simp_rw [h, monomial_zero, dvd_zero]
· refine ⟨monomial (j - i) d, ?_⟩
rw [monomial_mul, add_tsub_cancel_of_le hij]
@[simp]
theorem monomial_one_dvd_monomial_one [Nontrivial R] {i j : σ →₀ ℕ} :
monomial i (1 : R) ∣ monomial j 1 ↔ i ≤ j := by
rw [monomial_dvd_monomial]
simp_rw [one_ne_zero, false_or, dvd_rfl, and_true]
@[simp]
theorem X_dvd_X [Nontrivial R] {i j : σ} :
(X i : MvPolynomial σ R) ∣ (X j : MvPolynomial σ R) ↔ i = j := by
refine monomial_one_dvd_monomial_one.trans ?_
simp_rw [Finsupp.single_le_iff, Nat.one_le_iff_ne_zero, Finsupp.single_apply_ne_zero,
ne_eq, reduceCtorEq, not_false_eq_true, and_true]
@[simp]
theorem X_dvd_monomial {i : σ} {j : σ →₀ ℕ} {r : R} :
(X i : MvPolynomial σ R) ∣ monomial j r ↔ r = 0 ∨ j i ≠ 0 := by
refine monomial_dvd_monomial.trans ?_
simp_rw [one_dvd, and_true, Finsupp.single_le_iff, Nat.one_le_iff_ne_zero]
end MvPolynomial |
.lake/packages/mathlib/Mathlib/Algebra/MvPolynomial/Supported.lean | import Mathlib.Algebra.MvPolynomial.Variables
/-!
# Polynomials supported by a set of variables
This file contains the definition and lemmas about `MvPolynomial.supported`.
## Main definitions
* `MvPolynomial.supported` : Given a set `s : Set σ`, `supported R s` is the subalgebra of
`MvPolynomial σ R` consisting of polynomials whose set of variables is contained in `s`.
This subalgebra is isomorphic to `MvPolynomial s R`.
## Tags
variables, polynomial, vars
-/
universe u v w
namespace MvPolynomial
variable {σ : Type*} {R : Type u}
section CommSemiring
variable [CommSemiring R] {p : MvPolynomial σ R}
variable (R) in
/-- The set of polynomials whose variables are contained in `s` as a `Subalgebra` over `R`. -/
noncomputable def supported (s : Set σ) : Subalgebra R (MvPolynomial σ R) :=
Algebra.adjoin R (X '' s)
open Algebra
theorem supported_eq_range_rename (s : Set σ) : supported R s = (rename ((↑) : s → σ)).range := by
rw [supported, Set.image_eq_range, adjoin_range_eq_range_aeval, rename]
congr
/-- The isomorphism between the subalgebra of polynomials supported by `s` and
`MvPolynomial s R`. -/
noncomputable def supportedEquivMvPolynomial (s : Set σ) : supported R s ≃ₐ[R] MvPolynomial s R :=
(Subalgebra.equivOfEq _ _ (supported_eq_range_rename s)).trans
(AlgEquiv.ofInjective (rename ((↑) : s → σ)) (rename_injective _ Subtype.val_injective)).symm
@[simp]
theorem supportedEquivMvPolynomial_symm_C (s : Set σ) (x : R) :
(supportedEquivMvPolynomial s).symm (C x) = algebraMap R (supported R s) x := by
ext1
simp [supportedEquivMvPolynomial, MvPolynomial.algebraMap_eq]
@[simp]
theorem supportedEquivMvPolynomial_symm_X (s : Set σ) (i : s) :
(↑((supportedEquivMvPolynomial s).symm (X i : MvPolynomial s R)) : MvPolynomial σ R) = X ↑i :=
by simp [supportedEquivMvPolynomial]
variable {s t : Set σ}
theorem mem_supported : p ∈ supported R s ↔ ↑p.vars ⊆ s := by
classical
rw [supported_eq_range_rename, AlgHom.mem_range]
constructor
· rintro ⟨p, rfl⟩
refine _root_.trans (Finset.coe_subset.2 (vars_rename _ _)) ?_
simp
· intro hs
exact exists_rename_eq_of_vars_subset_range p ((↑) : s → σ) Subtype.val_injective (by simpa)
theorem supported_eq_vars_subset : (supported R s : Set (MvPolynomial σ R)) = { p | ↑p.vars ⊆ s } :=
Set.ext fun _ ↦ mem_supported
@[simp]
theorem mem_supported_vars (p : MvPolynomial σ R) : p ∈ supported R (↑p.vars : Set σ) := by
rw [mem_supported]
variable (s)
theorem supported_eq_adjoin_X : supported R s = Algebra.adjoin R (X '' s) := rfl
@[simp]
theorem supported_univ : supported R (Set.univ : Set σ) = ⊤ := by
simp [Algebra.eq_top_iff, mem_supported]
@[simp]
theorem supported_empty : supported R (∅ : Set σ) = ⊥ := by simp [supported_eq_adjoin_X]
variable {s}
theorem supported_mono (st : s ⊆ t) : supported R s ≤ supported R t :=
Algebra.adjoin_mono (Set.image_mono st)
@[simp]
theorem X_mem_supported [Nontrivial R] {i : σ} : X i ∈ supported R s ↔ i ∈ s := by
simp [mem_supported]
@[simp]
theorem supported_le_supported_iff [Nontrivial R] : supported R s ≤ supported R t ↔ s ⊆ t := by
constructor
· intro h i
simpa using @h (X i)
· exact supported_mono
theorem supported_strictMono [Nontrivial R] :
StrictMono (supported R : Set σ → Subalgebra R (MvPolynomial σ R)) :=
strictMono_of_le_iff_le fun _ _ ↦ supported_le_supported_iff.symm
theorem exists_restrict_to_vars (R : Type*) [CommRing R] {F : MvPolynomial σ ℤ}
(hF : ↑F.vars ⊆ s) : ∃ f : (s → R) → R, ∀ x : σ → R, f (x ∘ (↑) : s → R) = aeval x F := by
rw [← mem_supported, supported_eq_range_rename, AlgHom.mem_range] at hF
obtain ⟨F', hF'⟩ := hF
use fun z ↦ aeval z F'
intro x
simp only [← hF', aeval_rename]
end CommSemiring
end MvPolynomial |
.lake/packages/mathlib/Mathlib/Algebra/MvPolynomial/Equiv.lean | import Mathlib.Algebra.BigOperators.Finsupp.Fin
import Mathlib.Algebra.MvPolynomial.Degrees
import Mathlib.Algebra.MvPolynomial.Rename
import Mathlib.Algebra.Polynomial.AlgebraMap
import Mathlib.Algebra.Polynomial.Degree.Lemmas
import Mathlib.Data.Finsupp.Option
import Mathlib.Logic.Equiv.Fin.Basic
/-!
# Equivalences between polynomial rings
This file establishes a number of equivalences between polynomial rings,
based on equivalences between the underlying types.
## Notation
As in other polynomial files, we typically use the notation:
+ `σ : Type*` (indexing the variables)
+ `R : Type*` `[CommSemiring R]` (the coefficients)
+ `s : σ →₀ ℕ`, a function from `σ` to `ℕ` which is zero away from a finite set.
This will give rise to a monomial in `MvPolynomial σ R` which mathematicians might call `X^s`
+ `a : R`
+ `i : σ`, with corresponding monomial `X i`, often denoted `X_i` by mathematicians
+ `p : MvPolynomial σ R`
## Tags
equivalence, isomorphism, morphism, ring hom, hom
-/
noncomputable section
open Polynomial Set Function Finsupp AddMonoidAlgebra
universe u v w x
variable {R : Type u} {S₁ : Type v} {S₂ : Type w} {S₃ : Type x}
namespace MvPolynomial
variable {σ : Type*} {a a' a₁ a₂ : R} {e : ℕ} {s : σ →₀ ℕ}
section Equiv
variable (R) [CommSemiring R]
/-- The ring isomorphism between multivariable polynomials in a single variable and
polynomials over the ground ring.
-/
@[simps]
def pUnitAlgEquiv : MvPolynomial PUnit R ≃ₐ[R] R[X] where
toFun := eval₂ Polynomial.C fun _ => Polynomial.X
invFun := Polynomial.eval₂ MvPolynomial.C (X PUnit.unit)
left_inv := by
let f : R[X] →+* MvPolynomial PUnit R := Polynomial.eval₂RingHom MvPolynomial.C (X PUnit.unit)
let g : MvPolynomial PUnit R →+* R[X] := eval₂Hom Polynomial.C fun _ => Polynomial.X
change ∀ p, f.comp g p = p
apply is_id
· ext a
dsimp [f, g]
rw [eval₂_C, Polynomial.eval₂_C]
· rintro ⟨⟩
dsimp [f, g]
rw [eval₂_X, Polynomial.eval₂_X]
right_inv p :=
Polynomial.induction_on p (fun a => by rw [Polynomial.eval₂_C, MvPolynomial.eval₂_C])
(fun p q hp hq => by rw [Polynomial.eval₂_add, MvPolynomial.eval₂_add, hp, hq]) fun p n _ => by
rw [Polynomial.eval₂_mul, Polynomial.eval₂_pow, Polynomial.eval₂_X, Polynomial.eval₂_C,
eval₂_mul, eval₂_C, eval₂_pow, eval₂_X]
map_mul' _ _ := eval₂_mul _ _
map_add' _ _ := eval₂_add _ _
commutes' _ := eval₂_C _ _ _
theorem pUnitAlgEquiv_monomial {d : PUnit →₀ ℕ} {r : R} :
MvPolynomial.pUnitAlgEquiv R (MvPolynomial.monomial d r)
= Polynomial.monomial (d ()) r := by
simp [Polynomial.C_mul_X_pow_eq_monomial]
theorem pUnitAlgEquiv_symm_monomial {d : PUnit →₀ ℕ} {r : R} :
(MvPolynomial.pUnitAlgEquiv R).symm (Polynomial.monomial (d ()) r)
= MvPolynomial.monomial d r := by
simp [MvPolynomial.monomial_eq]
section Map
variable {R} (σ)
/-- If `e : A ≃+* B` is an isomorphism of rings, then so is `map e`. -/
@[simps apply]
def mapEquiv [CommSemiring S₁] [CommSemiring S₂] (e : S₁ ≃+* S₂) :
MvPolynomial σ S₁ ≃+* MvPolynomial σ S₂ :=
{ map (e : S₁ →+* S₂) with
toFun := map (e : S₁ →+* S₂)
invFun := map (e.symm : S₂ →+* S₁)
left_inv := map_leftInverse e.left_inv
right_inv := map_rightInverse e.right_inv }
@[simp]
theorem mapEquiv_refl : mapEquiv σ (RingEquiv.refl R) = RingEquiv.refl _ :=
RingEquiv.ext map_id
@[simp]
theorem mapEquiv_symm [CommSemiring S₁] [CommSemiring S₂] (e : S₁ ≃+* S₂) :
(mapEquiv σ e).symm = mapEquiv σ e.symm :=
rfl
@[simp]
theorem mapEquiv_trans [CommSemiring S₁] [CommSemiring S₂] [CommSemiring S₃] (e : S₁ ≃+* S₂)
(f : S₂ ≃+* S₃) : (mapEquiv σ e).trans (mapEquiv σ f) = mapEquiv σ (e.trans f) :=
RingEquiv.ext fun p => by
simp only [RingEquiv.coe_trans, comp_apply, mapEquiv_apply, RingEquiv.coe_ringHom_trans,
map_map]
variable {A₁ A₂ A₃ : Type*} [CommSemiring A₁] [CommSemiring A₂] [CommSemiring A₃]
variable [Algebra R A₁] [Algebra R A₂] [Algebra R A₃]
/-- If `e : A ≃ₐ[R] B` is an isomorphism of `R`-algebras, then so is `map e`. -/
@[simps apply]
def mapAlgEquiv (e : A₁ ≃ₐ[R] A₂) : MvPolynomial σ A₁ ≃ₐ[R] MvPolynomial σ A₂ :=
{ mapAlgHom (e : A₁ →ₐ[R] A₂), mapEquiv σ (e : A₁ ≃+* A₂) with toFun := map (e : A₁ →+* A₂) }
@[simp]
theorem mapAlgEquiv_refl : mapAlgEquiv σ (AlgEquiv.refl : A₁ ≃ₐ[R] A₁) = AlgEquiv.refl :=
AlgEquiv.ext map_id
@[simp]
theorem mapAlgEquiv_symm (e : A₁ ≃ₐ[R] A₂) : (mapAlgEquiv σ e).symm = mapAlgEquiv σ e.symm :=
rfl
@[simp]
theorem mapAlgEquiv_trans (e : A₁ ≃ₐ[R] A₂) (f : A₂ ≃ₐ[R] A₃) :
(mapAlgEquiv σ e).trans (mapAlgEquiv σ f) = mapAlgEquiv σ (e.trans f) := by
ext
simp only [AlgEquiv.trans_apply, mapAlgEquiv_apply, map_map]
rfl
end Map
section Eval
variable {R S : Type*} [CommSemiring R] [CommSemiring S]
theorem eval₂_pUnitAlgEquiv_symm {f : Polynomial R} {φ : R →+* S} {a : Unit → S} :
((MvPolynomial.pUnitAlgEquiv R).symm f : MvPolynomial Unit R).eval₂ φ a =
f.eval₂ φ (a ()) := by
simp only [MvPolynomial.pUnitAlgEquiv_symm_apply]
induction f using Polynomial.induction_on' with
| add f g hf hg => simp [hf, hg]
| monomial n r => simp
theorem eval₂_const_pUnitAlgEquiv_symm {f : Polynomial R} {φ : R →+* S} {a : S} :
((MvPolynomial.pUnitAlgEquiv R).symm f : MvPolynomial Unit R).eval₂ φ (fun _ ↦ a) =
f.eval₂ φ a := by
rw [eval₂_pUnitAlgEquiv_symm]
theorem eval₂_pUnitAlgEquiv {f : MvPolynomial PUnit R} {φ : R →+* S} {a : PUnit → S} :
((MvPolynomial.pUnitAlgEquiv R) f : Polynomial R).eval₂ φ (a default) = f.eval₂ φ a := by
simp only [MvPolynomial.pUnitAlgEquiv_apply]
induction f using MvPolynomial.induction_on' with
| monomial d r => simp
| add f g hf hg => simp [hf, hg]
theorem eval₂_const_pUnitAlgEquiv {f : MvPolynomial PUnit R} {φ : R →+* S} {a : S} :
((MvPolynomial.pUnitAlgEquiv R) f : Polynomial R).eval₂ φ a = f.eval₂ φ (fun _ ↦ a) := by
rw [← eval₂_pUnitAlgEquiv]
end Eval
section
variable (S₁ S₂ S₃)
/-- The function from multivariable polynomials in a sum of two types,
to multivariable polynomials in one of the types,
with coefficients in multivariable polynomials in the other type.
See `sumRingEquiv` for the ring isomorphism.
-/
def sumToIter : MvPolynomial (S₁ ⊕ S₂) R →+* MvPolynomial S₁ (MvPolynomial S₂ R) :=
eval₂Hom (C.comp C) fun bc => Sum.recOn bc X (C ∘ X)
@[simp]
theorem sumToIter_C (a : R) : sumToIter R S₁ S₂ (C a) = C (C a) :=
eval₂_C _ _ a
@[simp]
theorem sumToIter_Xl (b : S₁) : sumToIter R S₁ S₂ (X (Sum.inl b)) = X b :=
eval₂_X _ _ (Sum.inl b)
@[simp]
theorem sumToIter_Xr (c : S₂) : sumToIter R S₁ S₂ (X (Sum.inr c)) = C (X c) :=
eval₂_X _ _ (Sum.inr c)
/-- The function from multivariable polynomials in one type,
with coefficients in multivariable polynomials in another type,
to multivariable polynomials in the sum of the two types.
See `sumRingEquiv` for the ring isomorphism.
-/
def iterToSum : MvPolynomial S₁ (MvPolynomial S₂ R) →+* MvPolynomial (S₁ ⊕ S₂) R :=
eval₂Hom (eval₂Hom C (X ∘ Sum.inr)) (X ∘ Sum.inl)
@[simp]
theorem iterToSum_C_C (a : R) : iterToSum R S₁ S₂ (C (C a)) = C a :=
Eq.trans (eval₂_C _ _ (C a)) (eval₂_C _ _ _)
@[simp]
theorem iterToSum_X (b : S₁) : iterToSum R S₁ S₂ (X b) = X (Sum.inl b) :=
eval₂_X _ _ _
@[simp]
theorem iterToSum_C_X (c : S₂) : iterToSum R S₁ S₂ (C (X c)) = X (Sum.inr c) :=
Eq.trans (eval₂_C _ _ (X c)) (eval₂_X _ _ _)
section isEmptyRingEquiv
variable [IsEmpty σ]
variable (σ) in
/-- The algebra isomorphism between multivariable polynomials in no variables
and the ground ring. -/
@[simps! apply]
def isEmptyAlgEquiv : MvPolynomial σ R ≃ₐ[R] R :=
.ofAlgHom (aeval isEmptyElim) (Algebra.ofId _ _) (by ext) (by ext i m; exact isEmptyElim i)
variable {R S₁} in
@[simp]
lemma aeval_injective_iff_of_isEmpty [CommSemiring S₁] [Algebra R S₁] {f : σ → S₁} :
Function.Injective (aeval f : MvPolynomial σ R →ₐ[R] S₁) ↔
Function.Injective (algebraMap R S₁) := by
have : aeval f = (Algebra.ofId R S₁).comp (@isEmptyAlgEquiv R σ _ _).toAlgHom := by
ext i
exact IsEmpty.elim' ‹IsEmpty σ› i
rw [this, ← Injective.of_comp_iff' _ (@isEmptyAlgEquiv R σ _ _).bijective]
rfl
variable (σ) in
/-- The ring isomorphism between multivariable polynomials in no variables
and the ground ring. -/
@[simps! apply]
def isEmptyRingEquiv : MvPolynomial σ R ≃+* R := (isEmptyAlgEquiv R σ).toRingEquiv
lemma isEmptyRingEquiv_symm_toRingHom : (isEmptyRingEquiv R σ).symm.toRingHom = C := rfl
@[simp] lemma isEmptyRingEquiv_symm_apply (r : R) : (isEmptyRingEquiv R σ).symm r = C r := rfl
lemma isEmptyRingEquiv_eq_coeff_zero {σ R : Type*} [CommSemiring R] [IsEmpty σ] {x} :
isEmptyRingEquiv R σ x = x.coeff 0 := by
obtain ⟨x, rfl⟩ := (isEmptyRingEquiv R σ).symm.surjective x; simp
end isEmptyRingEquiv
/-- A helper function for `sumRingEquiv`. -/
@[simps]
def mvPolynomialEquivMvPolynomial [CommSemiring S₃] (f : MvPolynomial S₁ R →+* MvPolynomial S₂ S₃)
(g : MvPolynomial S₂ S₃ →+* MvPolynomial S₁ R) (hfgC : (f.comp g).comp C = C)
(hfgX : ∀ n, f (g (X n)) = X n) (hgfC : (g.comp f).comp C = C) (hgfX : ∀ n, g (f (X n)) = X n) :
MvPolynomial S₁ R ≃+* MvPolynomial S₂ S₃ where
toFun := f
invFun := g
left_inv := is_id (RingHom.comp _ _) hgfC hgfX
right_inv := is_id (RingHom.comp _ _) hfgC hfgX
map_mul' := f.map_mul
map_add' := f.map_add
/-- The ring isomorphism between multivariable polynomials in a sum of two types,
and multivariable polynomials in one of the types,
with coefficients in multivariable polynomials in the other type.
-/
def sumRingEquiv : MvPolynomial (S₁ ⊕ S₂) R ≃+* MvPolynomial S₁ (MvPolynomial S₂ R) := by
apply mvPolynomialEquivMvPolynomial R (S₁ ⊕ S₂) _ _ (sumToIter R S₁ S₂) (iterToSum R S₁ S₂)
· refine RingHom.ext (hom_eq_hom _ _ ?hC ?hX)
case hC => ext1; simp only [RingHom.comp_apply, iterToSum_C_C, sumToIter_C]
case hX => intro; simp only [RingHom.comp_apply, iterToSum_C_X, sumToIter_Xr]
· simp [iterToSum_X, sumToIter_Xl]
· ext1; simp only [RingHom.comp_apply, sumToIter_C, iterToSum_C_C]
· rintro ⟨⟩ <;> simp only [sumToIter_Xl, iterToSum_X, sumToIter_Xr, iterToSum_C_X]
/-- The algebra isomorphism between multivariable polynomials in a sum of two types,
and multivariable polynomials in one of the types,
with coefficients in multivariable polynomials in the other type.
-/
@[simps!]
def sumAlgEquiv : MvPolynomial (S₁ ⊕ S₂) R ≃ₐ[R] MvPolynomial S₁ (MvPolynomial S₂ R) :=
{ sumRingEquiv R S₁ S₂ with
commutes' := by
intro r
have A : algebraMap R (MvPolynomial S₁ (MvPolynomial S₂ R)) r = (C (C r) :) := rfl
have B : algebraMap R (MvPolynomial (S₁ ⊕ S₂) R) r = C r := rfl
simp only [sumRingEquiv, mvPolynomialEquivMvPolynomial, Equiv.toFun_as_coe,
Equiv.coe_fn_mk, B, sumToIter_C, A] }
lemma sumAlgEquiv_comp_rename_inr :
(sumAlgEquiv R S₁ S₂).toAlgHom.comp (rename Sum.inr) = IsScalarTower.toAlgHom R
(MvPolynomial S₂ R) (MvPolynomial S₁ (MvPolynomial S₂ R)) := by
ext i
simp
lemma sumAlgEquiv_comp_rename_inl :
(sumAlgEquiv R S₁ S₂).toAlgHom.comp (rename Sum.inl) =
MvPolynomial.mapAlgHom (Algebra.ofId _ _) := by
ext i
simp
section commAlgEquiv
variable {R S₁ S₂ : Type*} [CommSemiring R]
variable (R S₁ S₂) in
/-- The algebra isomorphism between multivariable polynomials in variables `S₁` of multivariable
polynomials in variables `S₂` and multivariable polynomials in variables `S₂` of multivariable
polynomials in variables `S₁`. -/
noncomputable
def commAlgEquiv : MvPolynomial S₁ (MvPolynomial S₂ R) ≃ₐ[R] MvPolynomial S₂ (MvPolynomial S₁ R) :=
(sumAlgEquiv R S₁ S₂).symm.trans <| (renameEquiv _ (.sumComm S₁ S₂)).trans (sumAlgEquiv R S₂ S₁)
@[simp] lemma commAlgEquiv_C (p) : commAlgEquiv R S₁ S₂ (.C p) = .map C p := by
suffices (commAlgEquiv R S₁ S₂).toAlgHom.comp
(IsScalarTower.toAlgHom R (MvPolynomial S₂ R) _) = mapAlgHom (Algebra.ofId _ _) by
exact DFunLike.congr_fun this p
ext x : 1
simp [commAlgEquiv]
lemma commAlgEquiv_C_X (i) : commAlgEquiv R S₁ S₂ (.C (.X i)) = .X i := by simp
@[simp] lemma commAlgEquiv_X (i) : commAlgEquiv R S₁ S₂ (.X i) = .C (.X i) := by simp [commAlgEquiv]
end commAlgEquiv
section
-- this speeds up typeclass search in the lemma below
attribute [local instance] IsScalarTower.right
/-- The algebra isomorphism between multivariable polynomials in `Option S₁` and
polynomials with coefficients in `MvPolynomial S₁ R`.
-/
@[simps! -isSimp]
def optionEquivLeft : MvPolynomial (Option S₁) R ≃ₐ[R] Polynomial (MvPolynomial S₁ R) :=
AlgEquiv.ofAlgHom (MvPolynomial.aeval fun o => o.elim Polynomial.X fun s => Polynomial.C (X s))
(Polynomial.aevalTower (MvPolynomial.rename some) (X none))
(by ext : 2 <;> simp) (by ext i : 2; cases i <;> simp)
lemma optionEquivLeft_X_some (x : S₁) : optionEquivLeft R S₁ (X (some x)) = Polynomial.C (X x) := by
simp [optionEquivLeft_apply, aeval_X]
lemma optionEquivLeft_X_none : optionEquivLeft R S₁ (X none) = Polynomial.X := by
simp [optionEquivLeft_apply, aeval_X]
lemma optionEquivLeft_C (r : R) : optionEquivLeft R S₁ (C r) = Polynomial.C (C r) := by
simp only [optionEquivLeft_apply, aeval_C, Polynomial.algebraMap_apply, algebraMap_eq]
theorem optionEquivLeft_monomial (m : Option S₁ →₀ ℕ) (r : R) :
optionEquivLeft R S₁ (monomial m r) = .monomial (m none) (monomial m.some r) := by
rw [optionEquivLeft_apply, aeval_monomial, prod_option_index]
· rw [MvPolynomial.monomial_eq, ← Polynomial.C_mul_X_pow_eq_monomial]
simp only [Polynomial.algebraMap_apply, algebraMap_eq, Option.elim_none, Option.elim_some,
map_mul, mul_assoc]
simp only [mul_comm, map_finsuppProd, map_pow]
· simp
· intros; rw [pow_add]
/-- The coefficient of `n.some` in the `n none`-th coefficient of `optionEquivLeft R S₁ f`
equals the coefficient of `n` in `f` -/
theorem optionEquivLeft_coeff_coeff (n : Option S₁ →₀ ℕ) (f : MvPolynomial (Option S₁) R) :
coeff n.some (Polynomial.coeff (optionEquivLeft R S₁ f) (n none)) = coeff n f := by
induction f using MvPolynomial.induction_on' generalizing n with
| monomial j r =>
rw [optionEquivLeft_monomial]
classical
simp only [Polynomial.coeff_monomial, MvPolynomial.coeff_monomial, apply_ite]
simp only [coeff_zero]
by_cases hj : j = n
· simp [hj]
· rw [if_neg hj]
simp only [ite_eq_right_iff]
intro hj_none hj_some
apply False.elim (hj _)
simp only [Finsupp.ext_iff, Option.forall, hj_none, true_and]
simpa only [Finsupp.ext_iff] using hj_some
| add p q hp hq => simp only [map_add, Polynomial.coeff_add, coeff_add, hp, hq]
theorem optionEquivLeft_elim_eval (s : S₁ → R) (y : R) (f : MvPolynomial (Option S₁) R) :
eval (fun x ↦ Option.elim x y s) f =
Polynomial.eval y (Polynomial.map (eval s) (optionEquivLeft R S₁ f)) := by
-- turn this into a def `Polynomial.mapAlgHom`
let φ : (MvPolynomial S₁ R)[X] →ₐ[R] R[X] :=
{ Polynomial.mapRingHom (eval s) with
commutes' := fun r => by
convert Polynomial.map_C (eval s)
exact (eval_C _).symm }
change
aeval (fun x ↦ Option.elim x y s) f =
(Polynomial.aeval y).comp (φ.comp (optionEquivLeft _ _).toAlgHom) f
congr 2
apply MvPolynomial.algHom_ext
rw [Option.forall]
simp only [aeval_X, Option.elim_none, AlgEquiv.toAlgHom_eq_coe, AlgHom.coe_comp,
Polynomial.coe_aeval_eq_eval, AlgHom.coe_mk, coe_mapRingHom, AlgHom.coe_coe, comp_apply,
optionEquivLeft_apply, Polynomial.map_X, Polynomial.eval_X, Option.elim_some, Polynomial.map_C,
eval_X, Polynomial.eval_C, implies_true, and_self, φ]
@[simp]
lemma natDegree_optionEquivLeft (p : MvPolynomial (Option S₁) R) :
(optionEquivLeft R S₁ p).natDegree = p.degreeOf .none := by
apply le_antisymm
· rw [Polynomial.natDegree_le_iff_coeff_eq_zero]
intro N hN
ext σ
trans p.coeff (σ.embDomain .some + .single .none N)
· simpa using optionEquivLeft_coeff_coeff R S₁ (σ.embDomain .some + .single .none N) p
simp only [coeff_zero, ← notMem_support_iff]
intro H
simpa using (degreeOf_lt_iff ((zero_le _).trans_lt hN)).mp hN _ H
· rw [degreeOf_le_iff]
intro σ hσ
refine Polynomial.le_natDegree_of_ne_zero fun H ↦ ?_
have := optionEquivLeft_coeff_coeff R S₁ σ p
rw [H, coeff_zero, eq_comm, ← notMem_support_iff] at this
exact this hσ
lemma totalDegree_coeff_optionEquivLeft_add_le
(p : MvPolynomial (Option S₁) R) (i : ℕ) (hi : i ≤ p.totalDegree) :
((optionEquivLeft R S₁ p).coeff i).totalDegree + i ≤ p.totalDegree := by
classical
by_cases hpi : (optionEquivLeft R S₁ p).coeff i = 0
· rw [hpi]; simpa
rw [totalDegree, add_comm, Finset.add_sup (by simpa only [support_nonempty]), Finset.sup_le_iff]
intro σ hσ
refine le_trans ?_ (Finset.le_sup (b := σ.embDomain .some + .single .none i) ?_)
· simp [Finsupp.sum_add_index, Finsupp.sum_embDomain, add_comm i]
· simpa [mem_support_iff, ← optionEquivLeft_coeff_coeff R S₁] using hσ
lemma totalDegree_coeff_optionEquivLeft_le
(p : MvPolynomial (Option S₁) R) (i : ℕ) :
((optionEquivLeft R S₁ p).coeff i).totalDegree ≤ p.totalDegree := by
classical
by_cases hpi : (optionEquivLeft R S₁ p).coeff i = 0
· rw [hpi]; simp
rw [totalDegree, Finset.sup_le_iff]
intro σ hσ
refine le_trans ?_ (Finset.le_sup (b := σ.embDomain .some + .single .none i) ?_)
· simp [Finsupp.sum_add_index, Finsupp.sum_embDomain]
· simpa [mem_support_iff, ← optionEquivLeft_coeff_coeff R S₁] using hσ
end
/-- The algebra isomorphism between multivariable polynomials in `Option S₁` and
multivariable polynomials with coefficients in polynomials.
-/
@[simps!]
def optionEquivRight : MvPolynomial (Option S₁) R ≃ₐ[R] MvPolynomial S₁ R[X] :=
AlgEquiv.ofAlgHom (MvPolynomial.aeval fun o => o.elim (C Polynomial.X) X)
(MvPolynomial.aevalTower (Polynomial.aeval (X none)) fun i => X (Option.some i))
(by
ext : 2 <;>
simp only [MvPolynomial.algebraMap_eq, Option.elim, AlgHom.coe_comp, AlgHom.id_comp,
IsScalarTower.coe_toAlgHom', comp_apply, aevalTower_C, Polynomial.aeval_X, aeval_X,
aevalTower_X, AlgHom.coe_id, id])
(by
ext ⟨i⟩ : 2 <;>
simp only [Option.elim, AlgHom.coe_comp, comp_apply, aeval_X, aevalTower_C,
Polynomial.aeval_X, AlgHom.coe_id, id, aevalTower_X])
lemma optionEquivRight_X_some (x : S₁) : optionEquivRight R S₁ (X (some x)) = X x := by
simp [optionEquivRight_apply, aeval_X]
lemma optionEquivRight_X_none : optionEquivRight R S₁ (X none) = C Polynomial.X := by
simp [optionEquivRight_apply, aeval_X]
lemma optionEquivRight_C (r : R) : optionEquivRight R S₁ (C r) = C (Polynomial.C r) := by
simp only [optionEquivRight_apply, aeval_C, algebraMap_apply, Polynomial.algebraMap_eq]
variable (n : ℕ)
/-- The algebra isomorphism between multivariable polynomials in `Fin (n + 1)` and
polynomials over multivariable polynomials in `Fin n`.
-/
def finSuccEquiv : MvPolynomial (Fin (n + 1)) R ≃ₐ[R] Polynomial (MvPolynomial (Fin n) R) :=
(renameEquiv R (_root_.finSuccEquiv n)).trans (optionEquivLeft R (Fin n))
theorem finSuccEquiv_eq :
(finSuccEquiv R n : MvPolynomial (Fin (n + 1)) R →+* Polynomial (MvPolynomial (Fin n) R)) =
eval₂Hom (Polynomial.C.comp (C : R →+* MvPolynomial (Fin n) R)) fun i : Fin (n + 1) =>
Fin.cases Polynomial.X (fun k => Polynomial.C (X k)) i := by
ext i : 2
· simp only [finSuccEquiv, optionEquivLeft_apply, aeval_C, AlgEquiv.coe_trans, RingHom.coe_coe,
coe_eval₂Hom, comp_apply, renameEquiv_apply, eval₂_C, RingHom.coe_comp, rename_C]
rfl
· refine Fin.cases ?_ ?_ i <;> simp [optionEquivLeft_apply, finSuccEquiv]
theorem finSuccEquiv_apply (p : MvPolynomial (Fin (n + 1)) R) :
finSuccEquiv R n p =
eval₂Hom (Polynomial.C.comp (C : R →+* MvPolynomial (Fin n) R))
(fun i : Fin (n + 1) => Fin.cases Polynomial.X (fun k => Polynomial.C (X k)) i) p := by
rw [← finSuccEquiv_eq, RingHom.coe_coe]
theorem finSuccEquiv_comp_C_eq_C {R : Type u} [CommSemiring R] (n : ℕ) :
(↑(MvPolynomial.finSuccEquiv R n).symm : Polynomial (MvPolynomial (Fin n) R) →+* _).comp
(Polynomial.C.comp MvPolynomial.C) =
(MvPolynomial.C : R →+* MvPolynomial (Fin n.succ) R) := by
refine RingHom.ext fun x => ?_
rw [RingHom.comp_apply]
refine
(MvPolynomial.finSuccEquiv R n).injective
(Trans.trans ((MvPolynomial.finSuccEquiv R n).apply_symm_apply _) ?_)
simp only [MvPolynomial.finSuccEquiv_apply, MvPolynomial.eval₂Hom_C]
variable {n} {R}
theorem finSuccEquiv_X_zero : finSuccEquiv R n (X 0) = Polynomial.X := by simp [finSuccEquiv_apply]
theorem finSuccEquiv_X_succ {j : Fin n} : finSuccEquiv R n (X j.succ) = Polynomial.C (X j) := by
simp [finSuccEquiv_apply]
/-- The coefficient of `m` in the `i`-th coefficient of `finSuccEquiv R n f` equals the
coefficient of `Finsupp.cons i m` in `f`. -/
theorem finSuccEquiv_coeff_coeff (m : Fin n →₀ ℕ) (f : MvPolynomial (Fin (n + 1)) R) (i : ℕ) :
coeff m (Polynomial.coeff (finSuccEquiv R n f) i) = coeff (m.cons i) f := by
induction f using MvPolynomial.induction_on' generalizing i m with
| add p q hp hq => simp only [map_add, Polynomial.coeff_add, coeff_add, hp, hq]
| monomial j r =>
simp only [finSuccEquiv_apply, coe_eval₂Hom, eval₂_monomial, RingHom.coe_comp, Finsupp.prod_pow,
Polynomial.coeff_C_mul, coeff_C_mul, coeff_monomial, Fin.prod_univ_succ, Fin.cases_zero,
Fin.cases_succ, ← map_prod, ← RingHom.map_pow, Function.comp_apply]
rw [← mul_boole, mul_comm (Polynomial.X ^ j 0), Polynomial.coeff_C_mul_X_pow]; congr 1
obtain rfl | hjmi := eq_or_ne j (m.cons i)
· simpa only [cons_zero, cons_succ, if_pos rfl, monomial_eq, C_1, one_mul,
Finsupp.prod_pow] using coeff_monomial m m (1 : R)
· simp only [hjmi, if_false]
obtain hij | rfl := ne_or_eq i (j 0)
· simp only [hij, if_false, coeff_zero]
simp only [if_true]
have hmj : m ≠ j.tail := by
rintro rfl
rw [cons_tail] at hjmi
contradiction
simpa only [monomial_eq, C_1, one_mul, Finsupp.prod_pow, tail_apply, if_neg hmj.symm] using
coeff_monomial m j.tail (1 : R)
theorem eval_eq_eval_mv_eval' (s : Fin n → R) (y : R) (f : MvPolynomial (Fin (n + 1)) R) :
eval (Fin.cons y s : Fin (n + 1) → R) f =
Polynomial.eval y (Polynomial.map (eval s) (finSuccEquiv R n f)) := by
-- turn this into a def `Polynomial.mapAlgHom`
let φ : (MvPolynomial (Fin n) R)[X] →ₐ[R] R[X] :=
{ Polynomial.mapRingHom (eval s) with
commutes' := fun r => by
convert Polynomial.map_C (eval s)
exact (eval_C _).symm }
change
aeval (Fin.cons y s : Fin (n + 1) → R) f =
(Polynomial.aeval y).comp (φ.comp (finSuccEquiv R n).toAlgHom) f
congr 2
apply MvPolynomial.algHom_ext
rw [Fin.forall_iff_succ]
simp only [φ, aeval_X, Fin.cons_zero, AlgEquiv.toAlgHom_eq_coe, AlgHom.coe_comp,
Polynomial.coe_aeval_eq_eval, Polynomial.map_C, AlgHom.coe_mk,
Polynomial.coe_mapRingHom, comp_apply, finSuccEquiv_apply, eval₂Hom_X',
Fin.cases_zero, Polynomial.map_X, Polynomial.eval_X, Fin.cons_succ,
Fin.cases_succ, eval_X, Polynomial.eval_C, AlgHom.coe_coe, implies_true, and_self]
theorem coeff_eval_eq_eval_coeff (s' : S₁ → R) (f : Polynomial (MvPolynomial S₁ R))
(i : ℕ) : Polynomial.coeff (Polynomial.map (eval s') f) i = eval s' (Polynomial.coeff f i) := by
simp only [Polynomial.coeff_map]
theorem support_coeff_finSuccEquiv {f : MvPolynomial (Fin (n + 1)) R} {i : ℕ} {m : Fin n →₀ ℕ} :
m ∈ ((finSuccEquiv R n f).coeff i).support ↔ m.cons i ∈ f.support := by
apply Iff.intro
· intro h
simpa [← finSuccEquiv_coeff_coeff] using h
· intro h
simpa [mem_support_iff, ← finSuccEquiv_coeff_coeff m f i] using h
/--
The `totalDegree` of a multivariable polynomial `p` is at least `i` more than the `totalDegree` of
the `i`th coefficient of `finSuccEquiv` applied to `p`, if this is nonzero.
-/
lemma totalDegree_coeff_finSuccEquiv_add_le (f : MvPolynomial (Fin (n + 1)) R) (i : ℕ)
(hi : (finSuccEquiv R n f).coeff i ≠ 0) :
totalDegree ((finSuccEquiv R n f).coeff i) + i ≤ totalDegree f := by
have hf'_sup : ((finSuccEquiv R n f).coeff i).support.Nonempty := by
rw [Finset.nonempty_iff_ne_empty, ne_eq, support_eq_empty]
exact hi
-- Let σ be a monomial index of ((finSuccEquiv R n p).coeff i) of maximal total degree
have ⟨σ, hσ1, hσ2⟩ := Finset.exists_mem_eq_sup (support _) hf'_sup
(fun s => Finsupp.sum s fun _ e => e)
-- Then cons i σ is a monomial index of p with total degree equal to the desired bound
let σ' : Fin (n + 1) →₀ ℕ := cons i σ
convert le_totalDegree (s := σ') _
· rw [totalDegree, hσ2, sum_cons, add_comm]
· rw [← support_coeff_finSuccEquiv]
exact hσ1
theorem support_finSuccEquiv (f : MvPolynomial (Fin (n + 1)) R) :
(finSuccEquiv R n f).support = Finset.image (fun m : Fin (n + 1) →₀ ℕ => m 0) f.support := by
ext i
rw [Polynomial.mem_support_iff, Finset.mem_image, Finsupp.ne_iff]
constructor
· rintro ⟨m, hm⟩
refine ⟨cons i m, ?_, cons_zero _ _⟩
rw [← support_coeff_finSuccEquiv]
simpa using hm
· rintro ⟨m, h, rfl⟩
refine ⟨tail m, ?_⟩
rwa [← coeff, zero_apply, ← mem_support_iff, support_coeff_finSuccEquiv, cons_tail]
theorem mem_support_finSuccEquiv {f : MvPolynomial (Fin (n + 1)) R} {x} :
x ∈ (finSuccEquiv R n f).support ↔ x ∈ (fun m : Fin (n + 1) →₀ _ ↦ m 0) '' f.support := by
simpa using congr(x ∈ $(support_finSuccEquiv f))
theorem image_support_finSuccEquiv {f : MvPolynomial (Fin (n + 1)) R} {i : ℕ} :
((finSuccEquiv R n f).coeff i).support.image (Finsupp.cons i) = {m ∈ f.support | m 0 = i} := by
ext m
rw [Finset.mem_filter, Finset.mem_image, mem_support_iff]
conv_lhs =>
congr
ext
rw [mem_support_iff, finSuccEquiv_coeff_coeff, Ne]
constructor
· grind [cons_zero]
· intro h
use tail m
rw [← h.2, cons_tail]
simp [h.1]
lemma mem_image_support_coeff_finSuccEquiv {f : MvPolynomial (Fin (n + 1)) R} {i : ℕ} {x} :
x ∈ Finsupp.cons i '' ((finSuccEquiv R n f).coeff i).support ↔
x ∈ f.support ∧ x 0 = i := by
simpa using congr(x ∈ $image_support_finSuccEquiv)
lemma mem_support_coeff_finSuccEquiv {f : MvPolynomial (Fin (n + 1)) R} {i : ℕ} {x} :
x ∈ ((finSuccEquiv R n f).coeff i).support ↔ x.cons i ∈ f.support := by
rw [← (Finsupp.cons_right_injective i).mem_finset_image (a := x),
image_support_finSuccEquiv]
simp only [Finset.mem_filter, mem_support_iff, ne_eq, cons_zero, and_true]
-- TODO: generalize `finSuccEquiv R n` to an arbitrary ZeroHom
theorem support_finSuccEquiv_nonempty {f : MvPolynomial (Fin (n + 1)) R} (h : f ≠ 0) :
(finSuccEquiv R n f).support.Nonempty := by
rwa [Polynomial.support_nonempty, EmbeddingLike.map_ne_zero_iff]
theorem degree_finSuccEquiv {f : MvPolynomial (Fin (n + 1)) R} (h : f ≠ 0) :
(finSuccEquiv R n f).degree = degreeOf 0 f := by
-- TODO: these should be lemmas
have h₀ : ∀ {α β : Type _} (f : α → β), (fun x => x) ∘ f = f := fun f => rfl
have h₁ : ∀ {α β : Type _} (f : α → β), f ∘ (fun x => x) = f := fun f => rfl
have h' : ((finSuccEquiv R n f).support.sup fun x => x) = degreeOf 0 f := by
rw [degreeOf_eq_sup, support_finSuccEquiv, Finset.sup_image, h₀]
rw [Polynomial.degree, ← h', Nat.cast_withBot,
Finset.coe_sup_of_nonempty (support_finSuccEquiv_nonempty h), Finset.max_eq_sup_coe, h₁]
theorem natDegree_finSuccEquiv (f : MvPolynomial (Fin (n + 1)) R) :
(finSuccEquiv R n f).natDegree = degreeOf 0 f := by
by_cases c : f = 0
· rw [c, map_zero, Polynomial.natDegree_zero, degreeOf_zero]
· rw [Polynomial.natDegree, degree_finSuccEquiv c, Nat.cast_withBot, WithBot.unbotD_coe]
theorem degreeOf_coeff_finSuccEquiv (p : MvPolynomial (Fin (n + 1)) R) (j : Fin n) (i : ℕ) :
degreeOf j (Polynomial.coeff (finSuccEquiv R n p) i) ≤ degreeOf j.succ p := by
rw [degreeOf_eq_sup, degreeOf_eq_sup, Finset.sup_le_iff]
intro m hm
rw [← Finsupp.cons_succ j i m]
exact Finset.le_sup
(f := fun (g : Fin (Nat.succ n) →₀ ℕ) => g (Fin.succ j))
(support_coeff_finSuccEquiv.1 hm)
/-- Consider a multivariate polynomial `φ` whose variables are indexed by `Option σ`,
and suppose that `σ ≃ Fin n`.
Then one may view `φ` as a polynomial over `MvPolynomial (Fin n) R`, by
1. renaming the variables via `Option σ ≃ Fin (n+1)`, and then singling out the `0`-th variable
via `MvPolynomial.finSuccEquiv`;
2. first viewing it as polynomial over `MvPolynomial σ R` via `MvPolynomial.optionEquivLeft`,
and then renaming the variables.
This lemma shows that both constructions are the same. -/
lemma finSuccEquiv_rename_finSuccEquiv (e : σ ≃ Fin n) (φ : MvPolynomial (Option σ) R) :
((finSuccEquiv R n) ((rename ((Equiv.optionCongr e).trans (_root_.finSuccEquiv n).symm)) φ)) =
Polynomial.map (rename e).toRingHom (optionEquivLeft R σ φ) := by
suffices (finSuccEquiv R n).toRingEquiv.toRingHom.comp (rename ((Equiv.optionCongr e).trans
(_root_.finSuccEquiv n).symm)).toRingHom =
(Polynomial.mapRingHom (rename e).toRingHom).comp (optionEquivLeft R σ) by
exact DFunLike.congr_fun this φ
apply ringHom_ext
· simp [Polynomial.algebraMap_apply, algebraMap_eq, finSuccEquiv_apply, optionEquivLeft_apply]
· rintro (i | i) <;> simp [finSuccEquiv_apply, optionEquivLeft_apply]
end
@[simp]
theorem rename_polynomial_aeval_X {σ τ : Type*} (f : σ → τ) (i : σ) (p : R[X]) :
rename f (Polynomial.aeval (X i) p) = Polynomial.aeval (X (f i) : MvPolynomial τ R) p := by
rw [← aeval_algHom_apply, rename_X]
end Equiv
end MvPolynomial
section toMvPolynomial
variable {R S σ τ : Type*} [CommSemiring R] [CommSemiring S] [Algebra R S]
/-- The embedding of `R[X]` into `R[Xᵢ]` as an `R`-algebra homomorphism. -/
noncomputable def Polynomial.toMvPolynomial (i : σ) : R[X] →ₐ[R] MvPolynomial σ R :=
aeval (MvPolynomial.X i)
@[simp]
lemma Polynomial.toMvPolynomial_C (i : σ) (r : R) : (C r).toMvPolynomial i = MvPolynomial.C r := by
simp [toMvPolynomial]
@[simp]
lemma Polynomial.toMvPolynomial_X (i : σ) : X.toMvPolynomial i = MvPolynomial.X (R := R) i := by
simp [toMvPolynomial]
lemma Polynomial.toMvPolynomial_eq_rename_comp (i : σ) :
toMvPolynomial (R := R) i =
(MvPolynomial.rename (fun _ : Unit ↦ i)).comp (MvPolynomial.pUnitAlgEquiv R).symm := by
ext
simp
lemma Polynomial.toMvPolynomial_injective (i : σ) :
Function.Injective (toMvPolynomial (R := R) i) := by
simp only [toMvPolynomial_eq_rename_comp, AlgHom.coe_comp, AlgHom.coe_coe,
EquivLike.injective_comp]
exact MvPolynomial.rename_injective (fun x ↦ i) fun _ _ _ ↦ rfl
@[simp]
lemma MvPolynomial.eval_comp_toMvPolynomial (f : σ → R) (i : σ) :
(eval f).comp (toMvPolynomial (R := R) i) = Polynomial.evalRingHom (f i) := by
ext <;> simp
@[simp]
lemma MvPolynomial.eval_toMvPolynomial (f : σ → R) (i : σ) (p : R[X]) :
eval f (p.toMvPolynomial i) = Polynomial.eval (f i) p :=
DFunLike.congr_fun (eval_comp_toMvPolynomial ..) p
@[simp]
lemma MvPolynomial.aeval_comp_toMvPolynomial (f : σ → S) (i : σ) :
(aeval (R := R) f).comp (toMvPolynomial i) = Polynomial.aeval (f i) := by
ext
simp
@[simp]
lemma MvPolynomial.aeval_toMvPolynomial (f : σ → S) (i : σ) (p : R[X]) :
aeval f (p.toMvPolynomial i) = Polynomial.aeval (f i) p :=
DFunLike.congr_fun (aeval_comp_toMvPolynomial ..) p
@[simp]
lemma MvPolynomial.rename_comp_toMvPolynomial (f : σ → τ) (a : σ) :
(rename (R := R) f).comp (Polynomial.toMvPolynomial a) = Polynomial.toMvPolynomial (f a) := by
ext
simp
@[simp]
lemma MvPolynomial.rename_toMvPolynomial (f : σ → τ) (a : σ) (p : R[X]) :
(rename (R := R) f) (p.toMvPolynomial a) = p.toMvPolynomial (f a) :=
DFunLike.congr_fun (rename_comp_toMvPolynomial ..) p
end toMvPolynomial |
.lake/packages/mathlib/Mathlib/Algebra/MvPolynomial/Polynomial.lean | import Mathlib.Algebra.MvPolynomial.Equiv
/-!
# Some lemmas relating polynomials and multivariable polynomials.
-/
namespace MvPolynomial
variable {R S σ : Type*}
theorem polynomial_eval_eval₂ [CommSemiring R] [CommSemiring S]
{x : S} (f : R →+* Polynomial S) (g : σ → Polynomial S) (p : MvPolynomial σ R) :
Polynomial.eval x (eval₂ f g p) =
eval₂ ((Polynomial.evalRingHom x).comp f) (fun s => Polynomial.eval x (g s)) p := by
apply induction_on p
· simp
· intro p q hp hq
simp [hp, hq]
· intro p n hp
simp [hp]
theorem eval_polynomial_eval_finSuccEquiv {n : ℕ} {x : Fin n → R}
[CommSemiring R] (f : MvPolynomial (Fin (n + 1)) R) (q : MvPolynomial (Fin n) R) :
(eval x) (Polynomial.eval q (finSuccEquiv R n f)) = eval (Fin.cases (eval x q) x) f := by
simp only [finSuccEquiv_apply, coe_eval₂Hom, polynomial_eval_eval₂, eval_eval₂]
conv in RingHom.comp _ _ =>
refine @RingHom.ext _ _ _ _ _ (RingHom.id _) fun r => ?_
simp
simp only [eval₂_id]
congr
funext i
refine Fin.cases (by simp) (by simp) i
end MvPolynomial |
.lake/packages/mathlib/Mathlib/Algebra/MvPolynomial/Cardinal.lean | import Mathlib.Algebra.MvPolynomial.Equiv
import Mathlib.Data.Finsupp.Fintype
import Mathlib.SetTheory.Cardinal.Finsupp
/-!
# Cardinality of Multivariate Polynomial Ring
The main result in this file is `MvPolynomial.cardinalMk_le_max`, which says that
the cardinality of `MvPolynomial σ R` is bounded above by the maximum of `#R`, `#σ`
and `ℵ₀`.
-/
universe u v
open Cardinal
namespace MvPolynomial
section TwoUniverses
variable {σ : Type u} {R : Type v} [CommSemiring R]
@[simp]
theorem cardinalMk_eq_max_lift [Nonempty σ] [Nontrivial R] :
#(MvPolynomial σ R) = max (max (Cardinal.lift.{u} #R) <| Cardinal.lift.{v} #σ) ℵ₀ :=
(mk_finsupp_lift_of_infinite _ R).trans <| by
rw [mk_finsupp_nat, max_assoc, lift_max, lift_aleph0, max_comm]
@[simp]
theorem cardinalMk_eq_lift [IsEmpty σ] : #(MvPolynomial σ R) = Cardinal.lift.{u} #R :=
((isEmptyRingEquiv R σ).toEquiv.trans Equiv.ulift.{u}.symm).cardinal_eq
@[nontriviality]
theorem cardinalMk_eq_one [Subsingleton R] : #(MvPolynomial σ R) = 1 := mk_eq_one _
theorem cardinalMk_le_max_lift {σ : Type u} {R : Type v} [CommSemiring R] : #(MvPolynomial σ R) ≤
max (max (Cardinal.lift.{u} #R) <| Cardinal.lift.{v} #σ) ℵ₀ := by
cases subsingleton_or_nontrivial R
· exact (mk_eq_one _).trans_le (le_max_of_le_right one_le_aleph0)
cases isEmpty_or_nonempty σ
· exact cardinalMk_eq_lift.trans_le (le_max_of_le_left <| le_max_left _ _)
· exact cardinalMk_eq_max_lift.le
end TwoUniverses
variable {σ R : Type u} [CommSemiring R]
theorem cardinalMk_eq_max [Nonempty σ] [Nontrivial R] :
#(MvPolynomial σ R) = max (max #R #σ) ℵ₀ := by simp
theorem cardinalMk_eq [IsEmpty σ] : #(MvPolynomial σ R) = #R := by simp
/-- The cardinality of the multivariate polynomial ring, `MvPolynomial σ R` is at most the maximum
of `#R`, `#σ` and `ℵ₀` -/
theorem cardinalMk_le_max : #(MvPolynomial σ R) ≤ max (max #R #σ) ℵ₀ :=
cardinalMk_le_max_lift.trans <| by rw [lift_id, lift_id]
end MvPolynomial |
.lake/packages/mathlib/Mathlib/Algebra/MvPolynomial/Funext.lean | import Mathlib.Algebra.Polynomial.RingDivision
import Mathlib.Algebra.Polynomial.Roots
import Mathlib.Algebra.MvPolynomial.CommRing
import Mathlib.Algebra.MvPolynomial.Polynomial
import Mathlib.Algebra.MvPolynomial.Rename
/-!
## Function extensionality for multivariate polynomials
In this file we show that two multivariate polynomials over an infinite integral domain are equal
if they are equal upon evaluating them on an arbitrary assignment of the variables.
# Main declaration
* `MvPolynomial.funext`: two polynomials `φ ψ : MvPolynomial σ R`
over an infinite integral domain `R` are equal if `eval x φ = eval x ψ` for all `x : σ → R`.
-/
namespace MvPolynomial
variable {R : Type*} [CommRing R] [IsDomain R]
private theorem funext_fin {n : ℕ} {p : MvPolynomial (Fin n) R}
(s : Fin n → Set R) (hs : ∀ i, (s i).Infinite)
(h : ∀ x ∈ Set.pi .univ s, eval x p = 0) : p = 0 := by
induction n with
| zero =>
apply (MvPolynomial.isEmptyRingEquiv R (Fin 0)).injective
rw [RingEquiv.map_zero]
convert h _ finZeroElim
| succ n ih =>
apply (finSuccEquiv R n).injective
rw [map_zero]
apply Polynomial.eq_zero_of_infinite_isRoot
apply ((hs 0).image (C_injective ..).injOn).mono
rintro _ ⟨r, hr, rfl⟩
refine ih (s ·.succ) (fun _ ↦ hs _) fun x hx ↦ ?_
rw [eval_polynomial_eval_finSuccEquiv]
exact h _ fun i _ ↦ i.cases (by simpa using hr) (by simpa using hx)
section
variable {σ : Type*} {p q : MvPolynomial σ R} (s : σ → Set R) (hs : ∀ i, (s i).Infinite)
include hs
/-- Two multivariate polynomials over an integral domain are equal
if they are equal when evaluated anywhere in a box with infinite sides. -/
theorem funext_set (h : ∀ x ∈ Set.pi .univ s, eval x p = eval x q) :
p = q := by
suffices ∀ p, (∀ x ∈ Set.pi .univ s, eval x p = 0) → p = 0 by
rw [← sub_eq_zero, this (p - q)]
intro x hx
simp_rw [map_sub, h x hx, sub_self]
intro p h
obtain ⟨n, f, hf, p, rfl⟩ := exists_fin_rename p
suffices p = 0 by rw [this, map_zero]
refine funext_fin (s ∘ f) (fun _ ↦ hs _) fun x hx ↦ ?_
choose g hg using fun i ↦ (hs i).nonempty
convert h (Function.extend f x g) fun i _ ↦ ?_
· simp only [eval, eval₂Hom_rename, Function.extend_comp hf]
obtain ⟨i, rfl⟩ | nex := em (∃ x, f x = i)
· rw [hf.extend_apply]; exact hx _ ⟨⟩
· simp_rw [Function.extend, dif_neg nex, hg]
theorem funext_set_iff : p = q ↔ (∀ x ∈ Set.pi .univ s, eval x p = eval x q) :=
⟨by rintro rfl _ _; rfl, funext_set s hs⟩
end
variable [Infinite R]
/-- Two multivariate polynomials over an infinite integral domain are equal
if they are equal upon evaluating them on an arbitrary assignment of the variables. -/
theorem funext {σ : Type*} {p q : MvPolynomial σ R}
(h : ∀ x : σ → R, eval x p = eval x q) :
p = q :=
funext_set _ (fun _ ↦ Set.infinite_univ) fun _ _ ↦ h _
theorem funext_iff {σ : Type*} {p q : MvPolynomial σ R} :
p = q ↔ ∀ x : σ → R, eval x p = eval x q :=
⟨by rintro rfl; simp only [forall_const], funext⟩
end MvPolynomial |
.lake/packages/mathlib/Mathlib/Algebra/GCDMonoid/PUnit.lean | import Mathlib.Algebra.GCDMonoid.Basic
import Mathlib.Algebra.Ring.PUnit
/-!
# `PUnit` is a GCD monoid
This file collects facts about algebraic structures on the one-element type, e.g. that it is has a
GCD.
-/
namespace PUnit
-- This is too high-powered and should be split off also
instance normalizedGCDMonoid : NormalizedGCDMonoid PUnit where
gcd _ _ := unit
lcm _ _ := unit
normUnit _ := 1
normUnit_zero := rfl
normUnit_mul := by subsingleton
normUnit_coe_units := by subsingleton
gcd_dvd_left _ _ := ⟨unit, by subsingleton⟩
gcd_dvd_right _ _ := ⟨unit, by subsingleton⟩
dvd_gcd {_ _} _ _ _ := ⟨unit, by subsingleton⟩
gcd_mul_lcm _ _ := ⟨1, by subsingleton⟩
lcm_zero_left := by subsingleton
lcm_zero_right := by subsingleton
normalize_gcd := by subsingleton
normalize_lcm := by subsingleton
@[simp]
theorem gcd_eq {x y : PUnit} : gcd x y = unit :=
rfl
@[simp]
theorem lcm_eq {x y : PUnit} : lcm x y = unit :=
rfl
@[simp]
theorem norm_unit_eq {x : PUnit} : normUnit x = 1 :=
rfl
end PUnit |
.lake/packages/mathlib/Mathlib/Algebra/GCDMonoid/Nat.lean | import Mathlib.Algebra.GCDMonoid.Basic
import Mathlib.Algebra.Order.Group.Unbundled.Int
import Mathlib.Algebra.Ring.Int.Units
import Mathlib.Algebra.GroupWithZero.Nat
/-!
# ℕ and ℤ are normalized GCD monoids.
## Main statements
* ℕ is a `GCDMonoid`
* ℕ is a `NormalizedGCDMonoid`
* ℤ is a `NormalizationMonoid`
* ℤ is a `GCDMonoid`
* ℤ is a `NormalizedGCDMonoid`
## Tags
natural numbers, integers, normalization monoid, gcd monoid, greatest common divisor
-/
assert_not_exists IsOrderedMonoid
/-- `ℕ` is a gcd_monoid. -/
instance : GCDMonoid ℕ where
gcd := Nat.gcd
lcm := Nat.lcm
gcd_dvd_left := Nat.gcd_dvd_left
gcd_dvd_right := Nat.gcd_dvd_right
dvd_gcd := Nat.dvd_gcd
gcd_mul_lcm a b := by rw [Nat.gcd_mul_lcm]; rfl
lcm_zero_left := Nat.lcm_zero_left
lcm_zero_right := Nat.lcm_zero_right
theorem gcd_eq_nat_gcd (m n : ℕ) : gcd m n = Nat.gcd m n :=
rfl
theorem lcm_eq_nat_lcm (m n : ℕ) : lcm m n = Nat.lcm m n :=
rfl
instance : NormalizedGCDMonoid ℕ :=
{ (inferInstance : GCDMonoid ℕ),
(inferInstance : NormalizationMonoid ℕ) with
normalize_gcd := fun _ _ => normalize_eq _
normalize_lcm := fun _ _ => normalize_eq _ }
namespace Int
section NormalizationMonoid
instance normalizationMonoid : NormalizationMonoid ℤ where
normUnit a := if 0 ≤ a then 1 else -1
normUnit_zero := if_pos le_rfl
normUnit_mul {a b} hna hnb := by
rcases hna.lt_or_gt with ha | ha <;> rcases hnb.lt_or_gt with hb | hb <;>
simp [Int.mul_nonneg_iff, ha.le, ha.not_ge, hb.le, hb.not_ge]
normUnit_coe_units u :=
(units_eq_one_or u).elim (fun eq => eq.symm ▸ if_pos Int.one_nonneg) fun eq =>
eq.symm ▸ if_neg (not_le_of_gt <| show (-1 : ℤ) < 0 by decide)
theorem normUnit_eq (z : ℤ) : normUnit z = if 0 ≤ z then 1 else -1 := rfl
theorem normalize_of_nonneg {z : ℤ} (h : 0 ≤ z) : normalize z = z := by
rw [normalize_apply, normUnit_eq, if_pos h, Units.val_one, mul_one]
theorem normalize_of_nonpos {z : ℤ} (h : z ≤ 0) : normalize z = -z := by
obtain rfl | h := h.eq_or_lt
· simp
· rw [normalize_apply, normUnit_eq, if_neg (not_le_of_gt h), Units.val_neg, Units.val_one,
mul_neg_one]
theorem normalize_coe_nat (n : ℕ) : normalize (n : ℤ) = n :=
normalize_of_nonneg (ofNat_le_ofNat_of_le <| Nat.zero_le n)
theorem abs_eq_normalize (z : ℤ) : |z| = normalize z := by
cases le_total 0 z <;>
simp [abs_of_nonneg, abs_of_nonpos, normalize_of_nonneg, normalize_of_nonpos, *]
theorem nonneg_of_normalize_eq_self {z : ℤ} (hz : normalize z = z) : 0 ≤ z := by
by_cases! h : 0 ≤ z
· exact h
· rw [normalize_of_nonpos h.le] at hz
cutsat
theorem nonneg_iff_normalize_eq_self (z : ℤ) : normalize z = z ↔ 0 ≤ z :=
⟨nonneg_of_normalize_eq_self, normalize_of_nonneg⟩
theorem eq_of_associated_of_nonneg {a b : ℤ} (h : Associated a b) (ha : 0 ≤ a) (hb : 0 ≤ b) :
a = b :=
dvd_antisymm_of_normalize_eq (normalize_of_nonneg ha) (normalize_of_nonneg hb) h.dvd h.symm.dvd
end NormalizationMonoid
section GCDMonoid
instance : GCDMonoid ℤ where
gcd a b := Int.gcd a b
lcm a b := Int.lcm a b
gcd_dvd_left := Int.gcd_dvd_left
gcd_dvd_right := Int.gcd_dvd_right
dvd_gcd := dvd_coe_gcd
gcd_mul_lcm a b := by
rw [← Int.natCast_mul, gcd_mul_lcm, ← natAbs_mul, natCast_natAbs, abs_eq_normalize]
exact normalize_associated (a * b)
lcm_zero_left _ := natCast_eq_zero.2 <| Nat.lcm_zero_left _
lcm_zero_right _ := natCast_eq_zero.2 <| Nat.lcm_zero_right _
instance : NormalizedGCDMonoid ℤ :=
{ Int.normalizationMonoid,
(inferInstance : GCDMonoid ℤ) with
normalize_gcd := fun _ _ => normalize_coe_nat _
normalize_lcm := fun _ _ => normalize_coe_nat _ }
theorem coe_gcd (i j : ℤ) : ↑(Int.gcd i j) = GCDMonoid.gcd i j :=
rfl
theorem coe_lcm (i j : ℤ) : ↑(Int.lcm i j) = GCDMonoid.lcm i j :=
rfl
theorem natAbs_gcd (i j : ℤ) : natAbs (GCDMonoid.gcd i j) = Int.gcd i j :=
rfl
theorem natAbs_lcm (i j : ℤ) : natAbs (GCDMonoid.lcm i j) = Int.lcm i j :=
rfl
end GCDMonoid
theorem exists_unit_of_abs (a : ℤ) : ∃ (u : ℤ) (_ : IsUnit u), (Int.natAbs a : ℤ) = u * a := by
rcases natAbs_eq a with h | h
· use 1, isUnit_one
rw [← h, one_mul]
· use -1, isUnit_one.neg
rw [← neg_eq_iff_eq_neg.mpr h]
simp only [neg_mul, one_mul]
theorem gcd_eq_natAbs {a b : ℤ} : Int.gcd a b = Nat.gcd a.natAbs b.natAbs :=
rfl
end Int
/-- Maps an associate class of integers consisting of `-n, n` to `n : ℕ` -/
def associatesIntEquivNat : Associates ℤ ≃ ℕ := by
refine ⟨(·.out.natAbs), (Associates.mk ·), ?_, fun n ↦ ?_⟩
· refine Associates.forall_associated.2 fun a ↦ ?_
refine Associates.mk_eq_mk_iff_associated.2 <| Associated.symm <| ⟨normUnit a, ?_⟩
simp [Int.natCast_natAbs, Int.abs_eq_normalize, normalize_apply]
· dsimp only [Associates.out_mk]
rw [← Int.abs_eq_normalize, Int.natAbs_abs, Int.natAbs_natCast]
theorem Int.associated_natAbs (k : ℤ) : Associated k k.natAbs :=
associated_of_dvd_dvd (Int.dvd_natCast.mpr dvd_rfl) (Int.natAbs_dvd.mpr dvd_rfl)
theorem Int.associated_iff_natAbs {a b : ℤ} : Associated a b ↔ a.natAbs = b.natAbs := by
rw [← dvd_dvd_iff_associated, ← Int.natAbs_dvd_natAbs, ← Int.natAbs_dvd_natAbs,
dvd_dvd_iff_associated]
exact associated_iff_eq
theorem Int.associated_iff {a b : ℤ} : Associated a b ↔ a = b ∨ a = -b := by
rw [Int.associated_iff_natAbs]
exact Int.natAbs_eq_natAbs_iff |
.lake/packages/mathlib/Mathlib/Algebra/GCDMonoid/IntegrallyClosed.lean | import Mathlib.Algebra.GCDMonoid.Basic
import Mathlib.RingTheory.IntegralClosure.IntegrallyClosed
import Mathlib.RingTheory.Polynomial.Eisenstein.Basic
/-!
# GCD domains are integrally closed
-/
open scoped Polynomial
variable {R A : Type*} [CommRing R] [IsDomain R] [CommRing A] [Algebra R A]
theorem IsLocalization.surj_of_gcd_domain [GCDMonoid R] (M : Submonoid R) [IsLocalization M A]
(z : A) : ∃ a b : R, IsUnit (gcd a b) ∧ z * algebraMap R A b = algebraMap R A a := by
obtain ⟨x, ⟨y, hy⟩, rfl⟩ := IsLocalization.exists_mk'_eq M z
obtain ⟨x', y', hx', hy', hu⟩ := extract_gcd x y
use x', y', hu
rw [mul_comm, IsLocalization.mul_mk'_eq_mk'_of_mul]
convert IsLocalization.mk'_mul_cancel_left (M := M) (S := A) _ _ using 2
grind
instance (priority := 100) GCDMonoid.toIsIntegrallyClosed
[h : Nonempty (GCDMonoid R)] : IsIntegrallyClosed R :=
(isIntegrallyClosed_iff (FractionRing R)).mpr fun {X} ⟨p, hp₁, hp₂⟩ => by
cases h
obtain ⟨x, y, hg, he⟩ := IsLocalization.surj_of_gcd_domain (nonZeroDivisors R) X
have :=
Polynomial.dvd_pow_natDegree_of_eval₂_eq_zero (IsFractionRing.injective R <| FractionRing R)
hp₁ y x _ hp₂ (by rw [mul_comm, he])
have : IsUnit y := by
rw [isUnit_iff_dvd_one, ← one_pow]
exact
(dvd_gcd this <| dvd_refl y).trans
(gcd_pow_left_dvd_pow_gcd.trans <| pow_dvd_pow_of_dvd (isUnit_iff_dvd_one.1 hg) _)
use x * (this.unit⁻¹ :)
rw [map_mul]
have coe_map_inv :=
Units.coe_map_inv ((algebraMap R (FractionRing R) : R →* FractionRing R)) this.unit
simp only [MonoidHom.coe_coe] at coe_map_inv
rw [← coe_map_inv, eq_comm, Units.eq_mul_inv_iff_mul_eq]
exact he |
.lake/packages/mathlib/Mathlib/Algebra/GCDMonoid/Finset.lean | import Mathlib.Data.Finset.Fold
import Mathlib.Algebra.GCDMonoid.Multiset
/-!
# GCD and LCM operations on finsets
## Main definitions
- `Finset.gcd` - the greatest common denominator of a `Finset` of elements of a `GCDMonoid`
- `Finset.lcm` - the least common multiple of a `Finset` of elements of a `GCDMonoid`
## Implementation notes
Many of the proofs use the lemmas `gcd_def` and `lcm_def`, which relate `Finset.gcd`
and `Finset.lcm` to `Multiset.gcd` and `Multiset.lcm`.
TODO: simplify with a tactic and `Data.Finset.Lattice`
## Tags
finset, gcd
-/
variable {ι α β γ : Type*}
namespace Finset
open Multiset
variable [CancelCommMonoidWithZero α] [NormalizedGCDMonoid α]
/-! ### lcm -/
section lcm
/-- Least common multiple of a finite set -/
def lcm (s : Finset β) (f : β → α) : α :=
s.fold GCDMonoid.lcm 1 f
variable {s s₁ s₂ : Finset β} {f : β → α}
theorem lcm_def : s.lcm f = (s.1.map f).lcm :=
rfl
@[simp]
theorem lcm_empty : (∅ : Finset β).lcm f = 1 :=
rfl
@[simp]
theorem lcm_dvd_iff {a : α} : s.lcm f ∣ a ↔ ∀ b ∈ s, f b ∣ a := by
apply Iff.trans Multiset.lcm_dvd
simp only [Multiset.mem_map, and_imp, exists_imp]
exact ⟨fun k b hb ↦ k _ _ hb rfl, fun k a' b hb h ↦ h ▸ k _ hb⟩
theorem lcm_dvd {a : α} : (∀ b ∈ s, f b ∣ a) → s.lcm f ∣ a :=
lcm_dvd_iff.2
theorem dvd_lcm {b : β} (hb : b ∈ s) : f b ∣ s.lcm f :=
lcm_dvd_iff.1 dvd_rfl _ hb
@[simp]
theorem lcm_insert [DecidableEq β] {b : β} :
(insert b s : Finset β).lcm f = GCDMonoid.lcm (f b) (s.lcm f) := by
by_cases h : b ∈ s
· rw [insert_eq_of_mem h,
(lcm_eq_right_iff (f b) (s.lcm f) (Multiset.normalize_lcm (s.1.map f))).2 (dvd_lcm h)]
apply fold_insert h
@[simp]
theorem lcm_singleton {b : β} : ({b} : Finset β).lcm f = normalize (f b) :=
Multiset.lcm_singleton
@[local simp] -- This will later be provable by other `simp` lemmas.
theorem normalize_lcm : normalize (s.lcm f) = s.lcm f := by simp [lcm_def]
theorem lcm_union [DecidableEq β] : (s₁ ∪ s₂).lcm f = GCDMonoid.lcm (s₁.lcm f) (s₂.lcm f) :=
Finset.induction_on s₁ (by rw [empty_union, lcm_empty, lcm_one_left, normalize_lcm])
fun a s _ ih ↦ by rw [insert_union, lcm_insert, lcm_insert, ih, lcm_assoc]
theorem lcm_congr {f g : β → α} (hs : s₁ = s₂) (hfg : ∀ a ∈ s₂, f a = g a) :
s₁.lcm f = s₂.lcm g := by
subst hs
exact Finset.fold_congr hfg
theorem lcm_mono_fun {g : β → α} (h : ∀ b ∈ s, f b ∣ g b) : s.lcm f ∣ s.lcm g :=
lcm_dvd fun b hb ↦ (h b hb).trans (dvd_lcm hb)
theorem lcm_mono (h : s₁ ⊆ s₂) : s₁.lcm f ∣ s₂.lcm f :=
lcm_dvd fun _ hb ↦ dvd_lcm (h hb)
theorem lcm_image [DecidableEq β] {g : γ → β} (s : Finset γ) :
(s.image g).lcm f = s.lcm (f ∘ g) := by
classical induction s using Finset.induction <;> simp [*]
theorem lcm_eq_lcm_image [DecidableEq α] : s.lcm f = (s.image f).lcm id :=
Eq.symm <| lcm_image _
@[simp]
theorem lcm_eq_zero_iff [Nontrivial α] : s.lcm f = 0 ↔ ∃ x ∈ s, f x = 0 := by
simp only [lcm_def, Multiset.lcm_eq_zero_iff, Multiset.mem_map, mem_val]
theorem lcm_ne_zero_iff [Nontrivial α] : s.lcm f ≠ 0 ↔ ∀ x ∈ s, f x ≠ 0 := by
simp [lcm_eq_zero_iff]
end lcm
/-! ### gcd -/
section gcd
/-- Greatest common divisor of a finite set -/
def gcd (s : Finset β) (f : β → α) : α :=
s.fold GCDMonoid.gcd 0 f
variable {s s₁ s₂ : Finset β} {f : β → α}
theorem gcd_def : s.gcd f = (s.1.map f).gcd :=
rfl
@[simp]
theorem gcd_empty : (∅ : Finset β).gcd f = 0 :=
rfl
theorem dvd_gcd_iff {a : α} : a ∣ s.gcd f ↔ ∀ b ∈ s, a ∣ f b := by
apply Iff.trans Multiset.dvd_gcd
simp only [Multiset.mem_map, and_imp, exists_imp]
exact ⟨fun k b hb ↦ k _ _ hb rfl, fun k a' b hb h ↦ h ▸ k _ hb⟩
theorem gcd_dvd {b : β} (hb : b ∈ s) : s.gcd f ∣ f b :=
dvd_gcd_iff.1 dvd_rfl _ hb
theorem dvd_gcd {a : α} : (∀ b ∈ s, a ∣ f b) → a ∣ s.gcd f :=
dvd_gcd_iff.2
@[simp]
theorem gcd_insert [DecidableEq β] {b : β} :
(insert b s : Finset β).gcd f = GCDMonoid.gcd (f b) (s.gcd f) := by
by_cases h : b ∈ s
· rw [insert_eq_of_mem h,
(gcd_eq_right_iff (f b) (s.gcd f) (Multiset.normalize_gcd (s.1.map f))).2 (gcd_dvd h)]
apply fold_insert h
@[simp]
theorem gcd_singleton {b : β} : ({b} : Finset β).gcd f = normalize (f b) :=
Multiset.gcd_singleton
@[local simp] -- This will later be provable by other `simp` lemmas.
theorem normalize_gcd : normalize (s.gcd f) = s.gcd f := by simp [gcd_def]
theorem gcd_union [DecidableEq β] : (s₁ ∪ s₂).gcd f = GCDMonoid.gcd (s₁.gcd f) (s₂.gcd f) :=
Finset.induction_on s₁ (by rw [empty_union, gcd_empty, gcd_zero_left, normalize_gcd])
fun a s _ ih ↦ by rw [insert_union, gcd_insert, gcd_insert, ih, gcd_assoc]
theorem gcd_congr {f g : β → α} (hs : s₁ = s₂) (hfg : ∀ a ∈ s₂, f a = g a) :
s₁.gcd f = s₂.gcd g := by
subst hs
exact Finset.fold_congr hfg
theorem gcd_mono_fun {g : β → α} (h : ∀ b ∈ s, f b ∣ g b) : s.gcd f ∣ s.gcd g :=
dvd_gcd fun b hb ↦ (gcd_dvd hb).trans (h b hb)
theorem gcd_mono (h : s₁ ⊆ s₂) : s₂.gcd f ∣ s₁.gcd f :=
dvd_gcd fun _ hb ↦ gcd_dvd (h hb)
theorem gcd_image [DecidableEq β] {g : γ → β} (s : Finset γ) :
(s.image g).gcd f = s.gcd (f ∘ g) := by
classical induction s using Finset.induction <;> simp [*]
theorem gcd_eq_gcd_image [DecidableEq α] : s.gcd f = (s.image f).gcd id :=
Eq.symm <| gcd_image _
theorem gcd_eq_zero_iff : s.gcd f = 0 ↔ ∀ x ∈ s, f x = 0 := by
rw [gcd_def, Multiset.gcd_eq_zero_iff]
constructor <;> intro h
· intro b bs
apply h (f b)
simp only [Multiset.mem_map]
use b
simp only [mem_def.1 bs, and_self]
· intro a as
rw [Multiset.mem_map] at as
rcases as with ⟨b, ⟨bs, rfl⟩⟩
apply h b (mem_def.1 bs)
theorem gcd_ne_zero_iff : s.gcd f ≠ 0 ↔ ∃ x ∈ s, f x ≠ 0 := by
simp [gcd_eq_zero_iff]
theorem gcd_eq_gcd_filter_ne_zero [DecidablePred fun x : β ↦ f x = 0] :
s.gcd f = {x ∈ s | f x ≠ 0}.gcd f := by
classical
trans ({x ∈ s | f x = 0} ∪ {x ∈ s | f x ≠ 0}).gcd f
· rw [filter_union_filter_neg_eq]
rw [gcd_union]
refine Eq.trans (?_ : _ = GCDMonoid.gcd (0 : α) ?_) (?_ : GCDMonoid.gcd (0 : α) _ = _)
· exact gcd {x ∈ s | f x ≠ 0} f
· refine congr (congr rfl <| s.induction_on ?_ ?_) (by simp)
· simp
· intro a s _ h
rw [filter_insert]
split_ifs with h1 <;> simp [h, h1]
simp only [gcd_zero_left, normalize_gcd]
nonrec theorem gcd_mul_left {a : α} : (s.gcd fun x ↦ a * f x) = normalize a * s.gcd f := by
classical
refine s.induction_on ?_ ?_
· simp
· intro b t _ h
rw [gcd_insert, gcd_insert, h, ← gcd_mul_left]
apply ((normalize_associated a).mul_right _).gcd_eq_right
nonrec theorem gcd_mul_right {a : α} : (s.gcd fun x ↦ f x * a) = s.gcd f * normalize a := by
classical
refine s.induction_on ?_ ?_
· simp
· intro b t _ h
rw [gcd_insert, gcd_insert, h, ← gcd_mul_right]
apply ((normalize_associated a).mul_left _).gcd_eq_right
theorem extract_gcd' (f g : β → α) (hs : ∃ x, x ∈ s ∧ f x ≠ 0)
(hg : ∀ b ∈ s, f b = s.gcd f * g b) : s.gcd g = 1 :=
((@mul_right_eq_self₀ _ _ (s.gcd f) _).1 <| by
conv_lhs => rw [← normalize_gcd, ← gcd_mul_left, ← gcd_congr rfl hg]).resolve_right <| by
contrapose! hs
exact gcd_eq_zero_iff.1 hs
theorem extract_gcd (f : β → α) (hs : s.Nonempty) :
∃ g : β → α, (∀ b ∈ s, f b = s.gcd f * g b) ∧ s.gcd g = 1 := by
classical
by_cases! h : ∀ x ∈ s, f x = (0 : α)
· refine ⟨fun _ ↦ 1, fun b hb ↦ by rw [h b hb, gcd_eq_zero_iff.2 h, mul_one], ?_⟩
rw [gcd_eq_gcd_image, image_const hs, gcd_singleton, id, normalize_one]
· choose g' hg using @gcd_dvd _ _ _ _ s f
refine ⟨fun b ↦ if hb : b ∈ s then g' hb else 0, fun b hb ↦ ?_,
extract_gcd' f _ h fun b hb ↦ ?_⟩
· simp only [hb, hg, dite_true]
rw [dif_pos hb, hg hb]
variable [Div α] [MulDivCancelClass α] {f : ι → α} {s : Finset ι} {i : ι}
/-- Given a nonempty Finset `s` and a function `f` from `s` to `ℕ`, if `d = s.gcd`,
then the `gcd` of `(f i) / d` is equal to `1`. -/
lemma gcd_div_eq_one (his : i ∈ s) (hfi : f i ≠ 0) : s.gcd (fun j ↦ f j / s.gcd f) = 1 := by
obtain ⟨g, he, hg⟩ := Finset.extract_gcd f ⟨i, his⟩
refine (Finset.gcd_congr rfl fun a ha ↦ ?_).trans hg
rw [he a ha, mul_div_cancel_left₀]
exact mt Finset.gcd_eq_zero_iff.1 fun h ↦ hfi <| h i his
lemma gcd_div_id_eq_one {s : Finset α} {a : α} (has : a ∈ s) (ha : a ≠ 0) :
s.gcd (fun b ↦ b / s.gcd id) = 1 := gcd_div_eq_one has ha
end gcd
end Finset
namespace Finset
section IsDomain
variable [CommRing α] [IsDomain α] [NormalizedGCDMonoid α]
theorem gcd_eq_of_dvd_sub {s : Finset β} {f g : β → α} {a : α}
(h : ∀ x : β, x ∈ s → a ∣ f x - g x) :
GCDMonoid.gcd a (s.gcd f) = GCDMonoid.gcd a (s.gcd g) := by
classical
revert h
refine s.induction_on ?_ ?_
· simp
intro b s _ hi h
rw [gcd_insert, gcd_insert, gcd_comm (f b), ← gcd_assoc,
hi fun x hx ↦ h _ (mem_insert_of_mem hx), gcd_comm a, gcd_assoc,
gcd_comm a (GCDMonoid.gcd _ _), gcd_comm (g b), gcd_assoc _ _ a, gcd_comm _ a]
exact congr_arg _ (gcd_eq_of_dvd_sub_right (h _ (mem_insert_self _ _)))
end IsDomain
end Finset |
.lake/packages/mathlib/Mathlib/Algebra/GCDMonoid/Basic.lean | import Mathlib.Algebra.Ring.Associated
import Mathlib.Algebra.Ring.Regular
/-!
# Monoids with normalization functions, `gcd`, and `lcm`
This file defines extra structures on `CancelCommMonoidWithZero`s, including `IsDomain`s.
## Main Definitions
* `NormalizationMonoid`
* `GCDMonoid`
* `NormalizedGCDMonoid`
* `gcdMonoidOfGCD`, `gcdMonoidOfExistsGCD`, `normalizedGCDMonoidOfGCD`,
`normalizedGCDMonoidOfExistsGCD`
* `gcdMonoidOfLCM`, `gcdMonoidOfExistsLCM`, `normalizedGCDMonoidOfLCM`,
`normalizedGCDMonoidOfExistsLCM`
For the `NormalizedGCDMonoid` instances on `ℕ` and `ℤ`, see `Mathlib/Algebra/GCDMonoid/Nat.lean`.
## Implementation Notes
* `NormalizationMonoid` is defined by assigning to each element a `normUnit` such that multiplying
by that unit normalizes the monoid, and `normalize` is an idempotent monoid homomorphism. This
definition as currently implemented does casework on `0`.
* `GCDMonoid` contains the definitions of `gcd` and `lcm` with the usual properties. They are
both determined up to a unit.
* `NormalizedGCDMonoid` extends `NormalizationMonoid`, so the `gcd` and `lcm` are always
normalized. This makes `gcd`s of polynomials easier to work with, but excludes Euclidean domains,
and monoids without zero.
* `gcdMonoidOfGCD` and `normalizedGCDMonoidOfGCD` noncomputably construct a `GCDMonoid`
(resp. `NormalizedGCDMonoid`) structure just from the `gcd` and its properties.
* `gcdMonoidOfExistsGCD` and `normalizedGCDMonoidOfExistsGCD` noncomputably construct a
`GCDMonoid` (resp. `NormalizedGCDMonoid`) structure just from a proof that any two elements
have a (not necessarily normalized) `gcd`.
* `gcdMonoidOfLCM` and `normalizedGCDMonoidOfLCM` noncomputably construct a `GCDMonoid`
(resp. `NormalizedGCDMonoid`) structure just from the `lcm` and its properties.
* `gcdMonoidOfExistsLCM` and `normalizedGCDMonoidOfExistsLCM` noncomputably construct a
`GCDMonoid` (resp. `NormalizedGCDMonoid`) structure just from a proof that any two elements
have a (not necessarily normalized) `lcm`.
## TODO
* Port GCD facts about nats, definition of coprime
* Generalize normalization monoids to commutative (cancellative) monoids with or without zero
## Tags
divisibility, gcd, lcm, normalize
-/
variable {α : Type*}
/-- Normalization monoid: multiplying with `normUnit` gives a normal form for associated
elements. -/
class NormalizationMonoid (α : Type*) [CancelCommMonoidWithZero α] where
/-- `normUnit` assigns to each element of the monoid a unit of the monoid. -/
normUnit : α → αˣ
/-- The proposition that `normUnit` maps `0` to the identity. -/
normUnit_zero : normUnit 0 = 1
/-- The proposition that `normUnit` respects multiplication of non-zero elements. -/
normUnit_mul : ∀ {a b}, a ≠ 0 → b ≠ 0 → normUnit (a * b) = normUnit a * normUnit b
/-- The proposition that `normUnit` maps units to their inverses. -/
normUnit_coe_units : ∀ u : αˣ, normUnit u = u⁻¹
export NormalizationMonoid (normUnit normUnit_zero normUnit_mul normUnit_coe_units)
attribute [simp] normUnit_coe_units normUnit_zero normUnit_mul
section NormalizationMonoid
variable [CancelCommMonoidWithZero α] [NormalizationMonoid α]
@[simp]
theorem normUnit_one : normUnit (1 : α) = 1 :=
normUnit_coe_units 1
/-- Chooses an element of each associate class, by multiplying by `normUnit` -/
def normalize : α →*₀ α where
toFun x := x * normUnit x
map_zero' := by
simp only [normUnit_zero]
exact mul_one (0 : α)
map_one' := by rw [normUnit_one, one_mul]; rfl
map_mul' x y :=
(by_cases fun hx : x = 0 => by rw [hx, zero_mul, zero_mul, zero_mul]) fun hx =>
(by_cases fun hy : y = 0 => by rw [hy, mul_zero, zero_mul, mul_zero]) fun hy => by
simp only [normUnit_mul hx hy, Units.val_mul]; simp only [mul_assoc, mul_left_comm y]
theorem associated_normalize (x : α) : Associated x (normalize x) :=
⟨_, rfl⟩
theorem normalize_associated (x : α) : Associated (normalize x) x :=
(associated_normalize _).symm
theorem associated_normalize_iff {x y : α} : Associated x (normalize y) ↔ Associated x y :=
⟨fun h => h.trans (normalize_associated y), fun h => h.trans (associated_normalize y)⟩
theorem normalize_associated_iff {x y : α} : Associated (normalize x) y ↔ Associated x y :=
⟨fun h => (associated_normalize _).trans h, fun h => (normalize_associated _).trans h⟩
theorem Associates.mk_normalize (x : α) : Associates.mk (normalize x) = Associates.mk x :=
Associates.mk_eq_mk_iff_associated.2 (normalize_associated _)
theorem normalize_apply (x : α) : normalize x = x * normUnit x :=
rfl
theorem normalize_zero : normalize (0 : α) = 0 :=
normalize.map_zero
theorem normalize_one : normalize (1 : α) = 1 :=
normalize.map_one
theorem normalize_coe_units (u : αˣ) : normalize (u : α) = 1 := by simp [normalize_apply]
@[simp]
theorem normalize_eq_zero {x : α} : normalize x = 0 ↔ x = 0 :=
⟨fun hx => (associated_zero_iff_eq_zero x).1 <| hx ▸ associated_normalize _, by
rintro rfl; exact normalize_zero⟩
theorem normalize_eq_one {x : α} : normalize x = 1 ↔ IsUnit x :=
⟨fun hx => isUnit_iff_exists_inv.2 ⟨_, hx⟩, fun ⟨u, hu⟩ => hu ▸ normalize_coe_units u⟩
@[simp]
theorem normUnit_mul_normUnit (a : α) : normUnit (a * normUnit a) = 1 := by
nontriviality α using Subsingleton.elim a 0
obtain rfl | h := eq_or_ne a 0
· rw [normUnit_zero, zero_mul, normUnit_zero]
· rw [normUnit_mul h (Units.ne_zero _), normUnit_coe_units, mul_inv_eq_one]
@[simp]
theorem normalize_idem (x : α) : normalize (normalize x) = normalize x := by simp [normalize_apply]
theorem normalize_eq_normalize {a b : α} (hab : a ∣ b) (hba : b ∣ a) :
normalize a = normalize b := by
nontriviality α
rcases associated_of_dvd_dvd hab hba with ⟨u, rfl⟩
refine by_cases (by rintro rfl; simp only [zero_mul]) fun ha : a ≠ 0 => ?_
suffices a * ↑(normUnit a) = a * ↑u * ↑(normUnit a) * ↑u⁻¹ by
simpa only [normalize_apply, mul_assoc, normUnit_mul ha u.ne_zero, normUnit_coe_units]
calc
a * ↑(normUnit a) = a * ↑(normUnit a) * ↑u * ↑u⁻¹ := (Units.mul_inv_cancel_right _ _).symm
_ = a * ↑u * ↑(normUnit a) * ↑u⁻¹ := by rw [mul_right_comm a]
theorem normalize_eq_normalize_iff {x y : α} : normalize x = normalize y ↔ x ∣ y ∧ y ∣ x :=
⟨fun h => ⟨Units.dvd_mul_right.1 ⟨_, h.symm⟩, Units.dvd_mul_right.1 ⟨_, h⟩⟩, fun ⟨hxy, hyx⟩ =>
normalize_eq_normalize hxy hyx⟩
theorem normalize_eq_normalize_iff_associated {x y : α} :
normalize x = normalize y ↔ Associated x y := by
rw [normalize_eq_normalize_iff, dvd_dvd_iff_associated]
theorem dvd_antisymm_of_normalize_eq {a b : α} (ha : normalize a = a) (hb : normalize b = b)
(hab : a ∣ b) (hba : b ∣ a) : a = b :=
ha ▸ hb ▸ normalize_eq_normalize hab hba
theorem Associated.eq_of_normalized
{a b : α} (h : Associated a b) (ha : normalize a = a) (hb : normalize b = b) :
a = b :=
dvd_antisymm_of_normalize_eq ha hb h.dvd h.dvd'
@[simp]
theorem dvd_normalize_iff {a b : α} : a ∣ normalize b ↔ a ∣ b :=
Units.dvd_mul_right
@[simp]
theorem normalize_dvd_iff {a b : α} : normalize a ∣ b ↔ a ∣ b :=
Units.mul_right_dvd
end NormalizationMonoid
namespace Associates
variable [CancelCommMonoidWithZero α] [NormalizationMonoid α]
/-- Maps an element of `Associates` back to the normalized element of its associate class -/
protected def out : Associates α → α :=
(Quotient.lift (normalize : α → α)) fun a _ ⟨_, hu⟩ =>
hu ▸ normalize_eq_normalize ⟨_, rfl⟩ (Units.mul_right_dvd.2 <| dvd_refl a)
@[simp]
theorem out_mk (a : α) : (Associates.mk a).out = normalize a :=
rfl
@[simp]
theorem out_one : (1 : Associates α).out = 1 :=
normalize_one
theorem out_mul (a b : Associates α) : (a * b).out = a.out * b.out :=
Quotient.inductionOn₂ a b fun _ _ => by
simp only [Associates.quotient_mk_eq_mk, out_mk, mk_mul_mk, normalize.map_mul]
theorem dvd_out_iff (a : α) (b : Associates α) : a ∣ b.out ↔ Associates.mk a ≤ b :=
Quotient.inductionOn b <| by
simp [Associates.out_mk, Associates.quotient_mk_eq_mk, mk_le_mk_iff_dvd]
theorem out_dvd_iff (a : α) (b : Associates α) : b.out ∣ a ↔ b ≤ Associates.mk a :=
Quotient.inductionOn b <| by
simp [Associates.out_mk, Associates.quotient_mk_eq_mk, mk_le_mk_iff_dvd]
@[simp]
theorem out_top : (⊤ : Associates α).out = 0 :=
normalize_zero
@[simp]
theorem normalize_out (a : Associates α) : normalize a.out = a.out :=
Quotient.inductionOn a normalize_idem
@[simp]
theorem mk_out (a : Associates α) : Associates.mk a.out = a :=
Quotient.inductionOn a mk_normalize
theorem out_injective : Function.Injective (Associates.out : _ → α) :=
Function.LeftInverse.injective mk_out
@[simp]
theorem out_eq_zero_iff {a : Associates α} : a.out = 0 ↔ a = 0 :=
Quotient.inductionOn a (by simp)
theorem out_zero : (0 : Associates α).out = 0 := by
simp
end Associates
/-- GCD monoid: a `CancelCommMonoidWithZero` with `gcd` (greatest common divisor) and
`lcm` (least common multiple) operations, determined up to a unit. The type class focuses on `gcd`
and we derive the corresponding `lcm` facts from `gcd`.
-/
class GCDMonoid (α : Type*) [CancelCommMonoidWithZero α] where
/-- The greatest common divisor between two elements. -/
gcd : α → α → α
/-- The least common multiple between two elements. -/
lcm : α → α → α
/-- The GCD is a divisor of the first element. -/
gcd_dvd_left : ∀ a b, gcd a b ∣ a
/-- The GCD is a divisor of the second element. -/
gcd_dvd_right : ∀ a b, gcd a b ∣ b
/-- Any common divisor of both elements is a divisor of the GCD. -/
dvd_gcd : ∀ {a b c}, a ∣ c → a ∣ b → a ∣ gcd c b
/-- The product of two elements is `Associated` with the product of their GCD and LCM. -/
gcd_mul_lcm : ∀ a b, Associated (gcd a b * lcm a b) (a * b)
/-- `0` is left-absorbing. -/
lcm_zero_left : ∀ a, lcm 0 a = 0
/-- `0` is right-absorbing. -/
lcm_zero_right : ∀ a, lcm a 0 = 0
/-- Normalized GCD monoid: a `CancelCommMonoidWithZero` with normalization and `gcd`
(greatest common divisor) and `lcm` (least common multiple) operations. In this setting `gcd` and
`lcm` form a bounded lattice on the associated elements where `gcd` is the infimum, `lcm` is the
supremum, `1` is bottom, and `0` is top. The type class focuses on `gcd` and we derive the
corresponding `lcm` facts from `gcd`.
-/
class NormalizedGCDMonoid (α : Type*) [CancelCommMonoidWithZero α] extends NormalizationMonoid α,
GCDMonoid α where
/-- The GCD is normalized to itself. -/
normalize_gcd : ∀ a b, normalize (gcd a b) = gcd a b
/-- The LCM is normalized to itself. -/
normalize_lcm : ∀ a b, normalize (lcm a b) = lcm a b
export GCDMonoid (gcd lcm gcd_dvd_left gcd_dvd_right dvd_gcd lcm_zero_left lcm_zero_right)
attribute [simp] lcm_zero_left lcm_zero_right
section GCDMonoid
variable [CancelCommMonoidWithZero α]
instance [NormalizationMonoid α] : Nonempty (NormalizationMonoid α) := ⟨‹_›⟩
instance [GCDMonoid α] : Nonempty (GCDMonoid α) := ⟨‹_›⟩
instance [NormalizedGCDMonoid α] : Nonempty (NormalizedGCDMonoid α) := ⟨‹_›⟩
instance [h : Nonempty (NormalizedGCDMonoid α)] : Nonempty (NormalizationMonoid α) :=
h.elim fun _ ↦ inferInstance
instance [h : Nonempty (NormalizedGCDMonoid α)] : Nonempty (GCDMonoid α) :=
h.elim fun _ ↦ inferInstance
theorem gcd_isUnit_iff_isRelPrime [GCDMonoid α] {a b : α} :
IsUnit (gcd a b) ↔ IsRelPrime a b :=
⟨fun h _ ha hb ↦ isUnit_of_dvd_unit (dvd_gcd ha hb) h, (· (gcd_dvd_left a b) (gcd_dvd_right a b))⟩
@[simp]
theorem normalize_gcd [NormalizedGCDMonoid α] : ∀ a b : α, normalize (gcd a b) = gcd a b :=
NormalizedGCDMonoid.normalize_gcd
theorem gcd_mul_lcm [GCDMonoid α] : ∀ a b : α, Associated (gcd a b * lcm a b) (a * b) :=
GCDMonoid.gcd_mul_lcm
section GCD
theorem dvd_gcd_iff [GCDMonoid α] (a b c : α) : a ∣ gcd b c ↔ a ∣ b ∧ a ∣ c :=
Iff.intro (fun h => ⟨h.trans (gcd_dvd_left _ _), h.trans (gcd_dvd_right _ _)⟩) fun ⟨hab, hac⟩ =>
dvd_gcd hab hac
theorem gcd_comm [NormalizedGCDMonoid α] (a b : α) : gcd a b = gcd b a :=
dvd_antisymm_of_normalize_eq (normalize_gcd _ _) (normalize_gcd _ _)
(dvd_gcd (gcd_dvd_right _ _) (gcd_dvd_left _ _))
(dvd_gcd (gcd_dvd_right _ _) (gcd_dvd_left _ _))
theorem gcd_comm' [GCDMonoid α] (a b : α) : Associated (gcd a b) (gcd b a) :=
associated_of_dvd_dvd (dvd_gcd (gcd_dvd_right _ _) (gcd_dvd_left _ _))
(dvd_gcd (gcd_dvd_right _ _) (gcd_dvd_left _ _))
theorem gcd_assoc [NormalizedGCDMonoid α] (m n k : α) : gcd (gcd m n) k = gcd m (gcd n k) :=
dvd_antisymm_of_normalize_eq (normalize_gcd _ _) (normalize_gcd _ _)
(dvd_gcd ((gcd_dvd_left (gcd m n) k).trans (gcd_dvd_left m n))
(dvd_gcd ((gcd_dvd_left (gcd m n) k).trans (gcd_dvd_right m n)) (gcd_dvd_right (gcd m n) k)))
(dvd_gcd
(dvd_gcd (gcd_dvd_left m (gcd n k)) ((gcd_dvd_right m (gcd n k)).trans (gcd_dvd_left n k)))
((gcd_dvd_right m (gcd n k)).trans (gcd_dvd_right n k)))
theorem gcd_assoc' [GCDMonoid α] (m n k : α) : Associated (gcd (gcd m n) k) (gcd m (gcd n k)) :=
associated_of_dvd_dvd
(dvd_gcd ((gcd_dvd_left (gcd m n) k).trans (gcd_dvd_left m n))
(dvd_gcd ((gcd_dvd_left (gcd m n) k).trans (gcd_dvd_right m n)) (gcd_dvd_right (gcd m n) k)))
(dvd_gcd
(dvd_gcd (gcd_dvd_left m (gcd n k)) ((gcd_dvd_right m (gcd n k)).trans (gcd_dvd_left n k)))
((gcd_dvd_right m (gcd n k)).trans (gcd_dvd_right n k)))
instance [NormalizedGCDMonoid α] : Std.Commutative (α := α) gcd where
comm := gcd_comm
instance [NormalizedGCDMonoid α] : Std.Associative (α := α) gcd where
assoc := gcd_assoc
theorem gcd_eq_normalize [NormalizedGCDMonoid α] {a b c : α} (habc : gcd a b ∣ c)
(hcab : c ∣ gcd a b) : gcd a b = normalize c :=
normalize_gcd a b ▸ normalize_eq_normalize habc hcab
@[simp]
theorem gcd_zero_left [NormalizedGCDMonoid α] (a : α) : gcd 0 a = normalize a :=
gcd_eq_normalize (gcd_dvd_right 0 a) (dvd_gcd (dvd_zero _) (dvd_refl a))
theorem gcd_zero_left' [GCDMonoid α] (a : α) : Associated (gcd 0 a) a :=
associated_of_dvd_dvd (gcd_dvd_right 0 a) (dvd_gcd (dvd_zero _) (dvd_refl a))
@[simp]
theorem gcd_zero_right [NormalizedGCDMonoid α] (a : α) : gcd a 0 = normalize a :=
gcd_eq_normalize (gcd_dvd_left a 0) (dvd_gcd (dvd_refl a) (dvd_zero _))
theorem gcd_zero_right' [GCDMonoid α] (a : α) : Associated (gcd a 0) a :=
associated_of_dvd_dvd (gcd_dvd_left a 0) (dvd_gcd (dvd_refl a) (dvd_zero _))
@[simp]
theorem gcd_eq_zero_iff [GCDMonoid α] (a b : α) : gcd a b = 0 ↔ a = 0 ∧ b = 0 :=
Iff.intro
(fun h => by
let ⟨ca, ha⟩ := gcd_dvd_left a b
let ⟨cb, hb⟩ := gcd_dvd_right a b
rw [h, zero_mul] at ha hb
exact ⟨ha, hb⟩)
fun ⟨ha, hb⟩ => by
rw [ha, hb, ← zero_dvd_iff]
apply dvd_gcd <;> rfl
theorem gcd_ne_zero_of_left [GCDMonoid α] {a b : α} (ha : a ≠ 0) : gcd a b ≠ 0 := by
simp_all
theorem gcd_ne_zero_of_right [GCDMonoid α] {a b : α} (hb : b ≠ 0) : gcd a b ≠ 0 := by
simp_all
@[simp]
theorem gcd_one_left [NormalizedGCDMonoid α] (a : α) : gcd 1 a = 1 :=
dvd_antisymm_of_normalize_eq (normalize_gcd _ _) normalize_one (gcd_dvd_left _ _) (one_dvd _)
@[simp]
theorem isUnit_gcd_one_left [GCDMonoid α] (a : α) : IsUnit (gcd 1 a) :=
isUnit_of_dvd_one (gcd_dvd_left _ _)
theorem gcd_one_left' [GCDMonoid α] (a : α) : Associated (gcd 1 a) 1 := by simp
@[simp]
theorem gcd_one_right [NormalizedGCDMonoid α] (a : α) : gcd a 1 = 1 :=
dvd_antisymm_of_normalize_eq (normalize_gcd _ _) normalize_one (gcd_dvd_right _ _) (one_dvd _)
@[simp]
theorem isUnit_gcd_one_right [GCDMonoid α] (a : α) : IsUnit (gcd a 1) :=
isUnit_of_dvd_one (gcd_dvd_right _ _)
theorem gcd_one_right' [GCDMonoid α] (a : α) : Associated (gcd a 1) 1 := by simp
@[gcongr]
theorem gcd_dvd_gcd [GCDMonoid α] {a b c d : α} (hab : a ∣ b) (hcd : c ∣ d) : gcd a c ∣ gcd b d :=
dvd_gcd ((gcd_dvd_left _ _).trans hab) ((gcd_dvd_right _ _).trans hcd)
protected theorem Associated.gcd [GCDMonoid α]
{a₁ a₂ b₁ b₂ : α} (ha : Associated a₁ a₂) (hb : Associated b₁ b₂) :
Associated (gcd a₁ b₁) (gcd a₂ b₂) :=
associated_of_dvd_dvd (gcd_dvd_gcd ha.dvd hb.dvd) (gcd_dvd_gcd ha.dvd' hb.dvd')
@[simp]
theorem gcd_same [NormalizedGCDMonoid α] (a : α) : gcd a a = normalize a :=
gcd_eq_normalize (gcd_dvd_left _ _) (dvd_gcd (dvd_refl a) (dvd_refl a))
@[simp]
theorem gcd_mul_left [NormalizedGCDMonoid α] (a b c : α) :
gcd (a * b) (a * c) = normalize a * gcd b c :=
(by_cases (by rintro rfl; simp only [zero_mul, gcd_zero_left, normalize_zero]))
fun ha : a ≠ 0 =>
suffices gcd (a * b) (a * c) = normalize (a * gcd b c) by simpa
let ⟨d, eq⟩ := dvd_gcd (dvd_mul_right a b) (dvd_mul_right a c)
gcd_eq_normalize
(eq.symm ▸ mul_dvd_mul_left a
(show d ∣ gcd b c from
dvd_gcd ((mul_dvd_mul_iff_left ha).1 <| eq ▸ gcd_dvd_left _ _)
((mul_dvd_mul_iff_left ha).1 <| eq ▸ gcd_dvd_right _ _)))
(dvd_gcd (mul_dvd_mul_left a <| gcd_dvd_left _ _) (mul_dvd_mul_left a <| gcd_dvd_right _ _))
theorem gcd_mul_left' [GCDMonoid α] (a b c : α) :
Associated (gcd (a * b) (a * c)) (a * gcd b c) := by
obtain rfl | ha := eq_or_ne a 0
· simp only [zero_mul, gcd_zero_left']
obtain ⟨d, eq⟩ := dvd_gcd (dvd_mul_right a b) (dvd_mul_right a c)
apply associated_of_dvd_dvd
· rw [eq]
gcongr
exact
dvd_gcd ((mul_dvd_mul_iff_left ha).1 <| eq ▸ gcd_dvd_left _ _)
((mul_dvd_mul_iff_left ha).1 <| eq ▸ gcd_dvd_right _ _)
· exact dvd_gcd (mul_dvd_mul_left a <| gcd_dvd_left _ _) (mul_dvd_mul_left a <| gcd_dvd_right _ _)
@[simp]
theorem gcd_mul_right [NormalizedGCDMonoid α] (a b c : α) :
gcd (b * a) (c * a) = gcd b c * normalize a := by simp only [mul_comm, gcd_mul_left]
@[simp]
theorem gcd_mul_right' [GCDMonoid α] (a b c : α) :
Associated (gcd (b * a) (c * a)) (gcd b c * a) := by
simp only [mul_comm, gcd_mul_left']
theorem gcd_eq_left_iff [NormalizedGCDMonoid α] (a b : α) (h : normalize a = a) :
gcd a b = a ↔ a ∣ b :=
(Iff.intro fun eq => eq ▸ gcd_dvd_right _ _) fun hab =>
dvd_antisymm_of_normalize_eq (normalize_gcd _ _) h (gcd_dvd_left _ _) (dvd_gcd (dvd_refl a) hab)
theorem gcd_eq_right_iff [NormalizedGCDMonoid α] (a b : α) (h : normalize b = b) :
gcd a b = b ↔ b ∣ a := by simpa only [gcd_comm a b] using gcd_eq_left_iff b a h
theorem gcd_dvd_gcd_mul_left [GCDMonoid α] (m n k : α) : gcd m n ∣ gcd (k * m) n := by
grw [← dvd_mul_left]
theorem gcd_dvd_gcd_mul_right [GCDMonoid α] (m n k : α) : gcd m n ∣ gcd (m * k) n := by
grw [← dvd_mul_right]
theorem gcd_dvd_gcd_mul_left_right [GCDMonoid α] (m n k : α) : gcd m n ∣ gcd m (k * n) := by
grw [← dvd_mul_left]
theorem gcd_dvd_gcd_mul_right_right [GCDMonoid α] (m n k : α) : gcd m n ∣ gcd m (n * k) := by
grw [← dvd_mul_right]
theorem Associated.gcd_eq_left [NormalizedGCDMonoid α] {m n : α} (h : Associated m n) (k : α) :
gcd m k = gcd n k :=
dvd_antisymm_of_normalize_eq (normalize_gcd _ _) (normalize_gcd _ _) (gcd_dvd_gcd h.dvd dvd_rfl)
(gcd_dvd_gcd h.symm.dvd dvd_rfl)
theorem Associated.gcd_eq_right [NormalizedGCDMonoid α] {m n : α} (h : Associated m n) (k : α) :
gcd k m = gcd k n :=
dvd_antisymm_of_normalize_eq (normalize_gcd _ _) (normalize_gcd _ _) (gcd_dvd_gcd dvd_rfl h.dvd)
(gcd_dvd_gcd dvd_rfl h.symm.dvd)
theorem dvd_gcd_mul_of_dvd_mul [GCDMonoid α] {m n k : α} (H : k ∣ m * n) : k ∣ gcd k m * n :=
(dvd_gcd (dvd_mul_right _ n) H).trans (gcd_mul_right' n k m).dvd
theorem dvd_gcd_mul_iff_dvd_mul [GCDMonoid α] {m n k : α} : k ∣ gcd k m * n ↔ k ∣ m * n :=
⟨fun h => h.trans (mul_dvd_mul (gcd_dvd_right k m) dvd_rfl), dvd_gcd_mul_of_dvd_mul⟩
theorem dvd_mul_gcd_of_dvd_mul [GCDMonoid α] {m n k : α} (H : k ∣ m * n) : k ∣ m * gcd k n := by
rw [mul_comm] at H ⊢
exact dvd_gcd_mul_of_dvd_mul H
theorem dvd_mul_gcd_iff_dvd_mul [GCDMonoid α] {m n k : α} : k ∣ m * gcd k n ↔ k ∣ m * n :=
⟨fun h => h.trans (mul_dvd_mul dvd_rfl (gcd_dvd_right k n)), dvd_mul_gcd_of_dvd_mul⟩
/-- Represent a divisor of `m * n` as a product of a divisor of `m` and a divisor of `n`.
Note: In general, this representation is highly non-unique.
See `Nat.dvdProdDvdOfDvdProd` for a constructive version on `ℕ`. -/
instance [h : Nonempty (GCDMonoid α)] : DecompositionMonoid α where
primal k m n H := by
cases h
by_cases h0 : gcd k m = 0
· rw [gcd_eq_zero_iff] at h0
rcases h0 with ⟨rfl, rfl⟩
exact ⟨0, n, dvd_refl 0, dvd_refl n, by simp⟩
· obtain ⟨a, ha⟩ := gcd_dvd_left k m
refine ⟨gcd k m, a, gcd_dvd_right _ _, ?_, ha⟩
rw [← mul_dvd_mul_iff_left h0, ← ha]
exact dvd_gcd_mul_of_dvd_mul H
theorem gcd_mul_dvd_mul_gcd [GCDMonoid α] (k m n : α) : gcd k (m * n) ∣ gcd k m * gcd k n := by
obtain ⟨m', n', hm', hn', h⟩ := exists_dvd_and_dvd_of_dvd_mul (gcd_dvd_right k (m * n))
replace h : gcd k (m * n) = m' * n' := h
rw [h]
have hm'n' : m' * n' ∣ k := h ▸ gcd_dvd_left _ _
apply mul_dvd_mul
· have hm'k : m' ∣ k := (dvd_mul_right m' n').trans hm'n'
exact dvd_gcd hm'k hm'
· have hn'k : n' ∣ k := (dvd_mul_left n' m').trans hm'n'
exact dvd_gcd hn'k hn'
theorem gcd_pow_right_dvd_pow_gcd [GCDMonoid α] {a b : α} {k : ℕ} :
gcd a (b ^ k) ∣ gcd a b ^ k := by
by_cases hg : gcd a b = 0
· rw [gcd_eq_zero_iff] at hg
rcases hg with ⟨rfl, rfl⟩
exact
(gcd_zero_left' (0 ^ k : α)).dvd.trans
(pow_dvd_pow_of_dvd (gcd_zero_left' (0 : α)).symm.dvd _)
· induction k with
| zero => rw [pow_zero, pow_zero]; exact (gcd_one_right' a).dvd
| succ k hk =>
rw [pow_succ', pow_succ']
trans gcd a b * gcd a (b ^ k)
· exact gcd_mul_dvd_mul_gcd a b (b ^ k)
· exact (mul_dvd_mul_iff_left hg).mpr hk
theorem gcd_pow_left_dvd_pow_gcd [GCDMonoid α] {a b : α} {k : ℕ} : gcd (a ^ k) b ∣ gcd a b ^ k :=
calc
gcd (a ^ k) b ∣ gcd b (a ^ k) := (gcd_comm' _ _).dvd
_ ∣ gcd b a ^ k := gcd_pow_right_dvd_pow_gcd
_ ∣ gcd a b ^ k := pow_dvd_pow_of_dvd (gcd_comm' _ _).dvd _
theorem pow_dvd_of_mul_eq_pow [GCDMonoid α] {a b c d₁ d₂ : α} (ha : a ≠ 0) (hab : IsUnit (gcd a b))
{k : ℕ} (h : a * b = c ^ k) (hc : c = d₁ * d₂) (hd₁ : d₁ ∣ a) : d₁ ^ k ≠ 0 ∧ d₁ ^ k ∣ a := by
have h1 : IsUnit (gcd (d₁ ^ k) b) := by
apply isUnit_of_dvd_one
trans gcd d₁ b ^ k
· exact gcd_pow_left_dvd_pow_gcd
· apply IsUnit.dvd
apply IsUnit.pow
apply isUnit_of_dvd_one
grw [hd₁, hab.dvd]
have h2 : d₁ ^ k ∣ a * b := by
use d₂ ^ k
rw [h, hc]
exact mul_pow d₁ d₂ k
rw [mul_comm] at h2
have h3 : d₁ ^ k ∣ a := by
apply (dvd_gcd_mul_of_dvd_mul h2).trans
rw [h1.mul_left_dvd]
have h4 : d₁ ^ k ≠ 0 := by
intro hdk
rw [hdk] at h3
apply absurd (zero_dvd_iff.mp h3) ha
exact ⟨h4, h3⟩
theorem exists_associated_pow_of_mul_eq_pow [GCDMonoid α] {a b c : α} (hab : IsUnit (gcd a b))
{k : ℕ} (h : a * b = c ^ k) : ∃ d : α, Associated (d ^ k) a := by
cases subsingleton_or_nontrivial α
· use 0
rw [Subsingleton.elim a (0 ^ k)]
by_cases ha : a = 0
· use 0
obtain rfl | hk := eq_or_ne k 0
· simp [ha] at h
· rw [ha, zero_pow hk]
by_cases hb : b = 0
· use 1
rw [one_pow]
apply (associated_one_iff_isUnit.mpr hab).symm.trans
rw [hb]
exact gcd_zero_right' a
obtain rfl | hk := k.eq_zero_or_pos
· use 1
rw [pow_zero] at h ⊢
use Units.mkOfMulEqOne _ _ h
rw [Units.val_mkOfMulEqOne, one_mul]
have hc : c ∣ a * b := by
rw [h]
exact dvd_pow_self _ hk.ne'
obtain ⟨d₁, d₂, hd₁, hd₂, hc⟩ := exists_dvd_and_dvd_of_dvd_mul hc
use d₁
obtain ⟨h0₁, ⟨a', ha'⟩⟩ := pow_dvd_of_mul_eq_pow ha hab h hc hd₁
rw [mul_comm] at h hc
rw [(gcd_comm' a b).isUnit_iff] at hab
obtain ⟨h0₂, ⟨b', hb'⟩⟩ := pow_dvd_of_mul_eq_pow hb hab h hc hd₂
rw [ha', hb', hc, mul_pow] at h
have h' : a' * b' = 1 := by
apply (mul_right_inj' h0₁).mp
rw [mul_one]
apply (mul_right_inj' h0₂).mp
rw [← h]
rw [mul_assoc, mul_comm a', ← mul_assoc _ b', ← mul_assoc b', mul_comm b']
use Units.mkOfMulEqOne _ _ h'
rw [Units.val_mkOfMulEqOne, ha']
theorem exists_eq_pow_of_mul_eq_pow [GCDMonoid α] [Subsingleton αˣ]
{a b c : α} (hab : IsUnit (gcd a b)) {k : ℕ} (h : a * b = c ^ k) : ∃ d : α, a = d ^ k :=
let ⟨d, hd⟩ := exists_associated_pow_of_mul_eq_pow hab h
⟨d, (associated_iff_eq.mp hd).symm⟩
theorem gcd_greatest {α : Type*} [CancelCommMonoidWithZero α] [NormalizedGCDMonoid α] {a b d : α}
(hda : d ∣ a) (hdb : d ∣ b) (hd : ∀ e : α, e ∣ a → e ∣ b → e ∣ d) :
GCDMonoid.gcd a b = normalize d :=
haveI h := hd _ (GCDMonoid.gcd_dvd_left a b) (GCDMonoid.gcd_dvd_right a b)
gcd_eq_normalize h (GCDMonoid.dvd_gcd hda hdb)
theorem gcd_greatest_associated {α : Type*} [CancelCommMonoidWithZero α] [GCDMonoid α] {a b d : α}
(hda : d ∣ a) (hdb : d ∣ b) (hd : ∀ e : α, e ∣ a → e ∣ b → e ∣ d) :
Associated d (GCDMonoid.gcd a b) :=
haveI h := hd _ (GCDMonoid.gcd_dvd_left a b) (GCDMonoid.gcd_dvd_right a b)
associated_of_dvd_dvd (GCDMonoid.dvd_gcd hda hdb) h
theorem isUnit_gcd_of_eq_mul_gcd {α : Type*} [CancelCommMonoidWithZero α] [GCDMonoid α]
{x y x' y' : α} (ex : x = gcd x y * x') (ey : y = gcd x y * y') (h : gcd x y ≠ 0) :
IsUnit (gcd x' y') := by
rw [← associated_one_iff_isUnit]
refine Associated.of_mul_left ?_ (Associated.refl <| gcd x y) h
convert (gcd_mul_left' (gcd x y) x' y').symm using 1
rw [← ex, ← ey, mul_one]
theorem extract_gcd {α : Type*} [CancelCommMonoidWithZero α] [GCDMonoid α] (x y : α) :
∃ x' y', x = gcd x y * x' ∧ y = gcd x y * y' ∧ IsUnit (gcd x' y') := by
by_cases h : gcd x y = 0
· obtain ⟨rfl, rfl⟩ := (gcd_eq_zero_iff x y).1 h
simp_rw [← associated_one_iff_isUnit]
exact ⟨1, 1, by rw [h, zero_mul], by rw [h, zero_mul], gcd_one_left' 1⟩
obtain ⟨x', ex⟩ := gcd_dvd_left x y
obtain ⟨y', ey⟩ := gcd_dvd_right x y
exact ⟨x', y', ex, ey, isUnit_gcd_of_eq_mul_gcd ex ey h⟩
theorem associated_gcd_left_iff [GCDMonoid α] {x y : α} : Associated x (gcd x y) ↔ x ∣ y :=
⟨fun hx => hx.dvd.trans (gcd_dvd_right x y),
fun hxy => associated_of_dvd_dvd (dvd_gcd dvd_rfl hxy) (gcd_dvd_left x y)⟩
theorem associated_gcd_right_iff [GCDMonoid α] {x y : α} : Associated y (gcd x y) ↔ y ∣ x :=
⟨fun hx => hx.dvd.trans (gcd_dvd_left x y),
fun hxy => associated_of_dvd_dvd (dvd_gcd hxy dvd_rfl) (gcd_dvd_right x y)⟩
theorem Irreducible.isUnit_gcd_iff [GCDMonoid α] {x y : α} (hx : Irreducible x) :
IsUnit (gcd x y) ↔ ¬(x ∣ y) := by
rw [hx.isUnit_iff_not_associated_of_dvd (gcd_dvd_left x y), not_iff_not, associated_gcd_left_iff]
theorem Irreducible.gcd_eq_one_iff [NormalizedGCDMonoid α] {x y : α} (hx : Irreducible x) :
gcd x y = 1 ↔ ¬(x ∣ y) := by
rw [← hx.isUnit_gcd_iff, ← normalize_eq_one, NormalizedGCDMonoid.normalize_gcd]
section Neg
variable [HasDistribNeg α]
lemma gcd_neg' [GCDMonoid α] {a b : α} : Associated (gcd a (-b)) (gcd a b) :=
Associated.gcd .rfl (.neg_left .rfl)
lemma gcd_neg [NormalizedGCDMonoid α] {a b : α} : gcd a (-b) = gcd a b :=
gcd_neg'.eq_of_normalized (normalize_gcd _ _) (normalize_gcd _ _)
lemma neg_gcd' [GCDMonoid α] {a b : α} : Associated (gcd (-a) b) (gcd a b) :=
Associated.gcd (.neg_left .rfl) .rfl
lemma neg_gcd [NormalizedGCDMonoid α] {a b : α} : gcd (-a) b = gcd a b :=
neg_gcd'.eq_of_normalized (normalize_gcd _ _) (normalize_gcd _ _)
end Neg
end GCD
section LCM
theorem lcm_dvd_iff [GCDMonoid α] {a b c : α} : lcm a b ∣ c ↔ a ∣ c ∧ b ∣ c := by
by_cases h : a = 0 ∨ b = 0
· rcases h with (rfl | rfl) <;>
simp +contextual only [iff_def, lcm_zero_left, lcm_zero_right,
zero_dvd_iff, dvd_zero, and_true, imp_true_iff]
· obtain ⟨h1, h2⟩ := not_or.1 h
have h : gcd a b ≠ 0 := fun H => h1 ((gcd_eq_zero_iff _ _).1 H).1
rw [← mul_dvd_mul_iff_left h, (gcd_mul_lcm a b).dvd_iff_dvd_left, ←
(gcd_mul_right' c a b).dvd_iff_dvd_right, dvd_gcd_iff, mul_comm b c, mul_dvd_mul_iff_left h1,
mul_dvd_mul_iff_right h2, and_comm]
theorem dvd_lcm_left [GCDMonoid α] (a b : α) : a ∣ lcm a b :=
(lcm_dvd_iff.1 (dvd_refl (lcm a b))).1
theorem dvd_lcm_right [GCDMonoid α] (a b : α) : b ∣ lcm a b :=
(lcm_dvd_iff.1 (dvd_refl (lcm a b))).2
theorem lcm_dvd [GCDMonoid α] {a b c : α} (hab : a ∣ b) (hcb : c ∣ b) : lcm a c ∣ b :=
lcm_dvd_iff.2 ⟨hab, hcb⟩
@[simp]
theorem lcm_eq_zero_iff [GCDMonoid α] (a b : α) : lcm a b = 0 ↔ a = 0 ∨ b = 0 :=
Iff.intro
(fun h : lcm a b = 0 => by
have : Associated (a * b) 0 := (gcd_mul_lcm a b).symm.trans <| by rw [h, mul_zero]
rwa [← mul_eq_zero, ← associated_zero_iff_eq_zero])
(by rintro (rfl | rfl) <;> [apply lcm_zero_left; apply lcm_zero_right])
@[simp]
theorem normalize_lcm [NormalizedGCDMonoid α] (a b : α) : normalize (lcm a b) = lcm a b :=
NormalizedGCDMonoid.normalize_lcm a b
theorem lcm_comm [NormalizedGCDMonoid α] (a b : α) : lcm a b = lcm b a :=
dvd_antisymm_of_normalize_eq (normalize_lcm _ _) (normalize_lcm _ _)
(lcm_dvd (dvd_lcm_right _ _) (dvd_lcm_left _ _))
(lcm_dvd (dvd_lcm_right _ _) (dvd_lcm_left _ _))
theorem lcm_comm' [GCDMonoid α] (a b : α) : Associated (lcm a b) (lcm b a) :=
associated_of_dvd_dvd (lcm_dvd (dvd_lcm_right _ _) (dvd_lcm_left _ _))
(lcm_dvd (dvd_lcm_right _ _) (dvd_lcm_left _ _))
theorem lcm_assoc [NormalizedGCDMonoid α] (m n k : α) : lcm (lcm m n) k = lcm m (lcm n k) :=
dvd_antisymm_of_normalize_eq (normalize_lcm _ _) (normalize_lcm _ _)
(lcm_dvd (lcm_dvd (dvd_lcm_left _ _) ((dvd_lcm_left _ _).trans (dvd_lcm_right _ _)))
((dvd_lcm_right _ _).trans (dvd_lcm_right _ _)))
(lcm_dvd ((dvd_lcm_left _ _).trans (dvd_lcm_left _ _))
(lcm_dvd ((dvd_lcm_right _ _).trans (dvd_lcm_left _ _)) (dvd_lcm_right _ _)))
theorem lcm_assoc' [GCDMonoid α] (m n k : α) : Associated (lcm (lcm m n) k) (lcm m (lcm n k)) :=
associated_of_dvd_dvd
(lcm_dvd (lcm_dvd (dvd_lcm_left _ _) ((dvd_lcm_left _ _).trans (dvd_lcm_right _ _)))
((dvd_lcm_right _ _).trans (dvd_lcm_right _ _)))
(lcm_dvd ((dvd_lcm_left _ _).trans (dvd_lcm_left _ _))
(lcm_dvd ((dvd_lcm_right _ _).trans (dvd_lcm_left _ _)) (dvd_lcm_right _ _)))
instance [NormalizedGCDMonoid α] : Std.Commutative (α := α) lcm where
comm := lcm_comm
instance [NormalizedGCDMonoid α] : Std.Associative (α := α) lcm where
assoc := lcm_assoc
theorem lcm_eq_normalize [NormalizedGCDMonoid α] {a b c : α} (habc : lcm a b ∣ c)
(hcab : c ∣ lcm a b) : lcm a b = normalize c :=
normalize_lcm a b ▸ normalize_eq_normalize habc hcab
theorem lcm_dvd_lcm [GCDMonoid α] {a b c d : α} (hab : a ∣ b) (hcd : c ∣ d) : lcm a c ∣ lcm b d :=
lcm_dvd (hab.trans (dvd_lcm_left _ _)) (hcd.trans (dvd_lcm_right _ _))
protected theorem Associated.lcm [GCDMonoid α]
{a₁ a₂ b₁ b₂ : α} (ha : Associated a₁ a₂) (hb : Associated b₁ b₂) :
Associated (lcm a₁ b₁) (lcm a₂ b₂) :=
associated_of_dvd_dvd (lcm_dvd_lcm ha.dvd hb.dvd) (lcm_dvd_lcm ha.dvd' hb.dvd')
@[simp]
theorem lcm_units_coe_left [NormalizedGCDMonoid α] (u : αˣ) (a : α) : lcm (↑u) a = normalize a :=
lcm_eq_normalize (lcm_dvd Units.coe_dvd dvd_rfl) (dvd_lcm_right _ _)
@[simp]
theorem lcm_units_coe_right [NormalizedGCDMonoid α] (a : α) (u : αˣ) : lcm a ↑u = normalize a :=
(lcm_comm a u).trans <| lcm_units_coe_left _ _
@[simp]
theorem lcm_one_left [NormalizedGCDMonoid α] (a : α) : lcm 1 a = normalize a :=
lcm_units_coe_left 1 a
@[simp]
theorem lcm_one_right [NormalizedGCDMonoid α] (a : α) : lcm a 1 = normalize a :=
lcm_units_coe_right a 1
@[simp]
theorem lcm_same [NormalizedGCDMonoid α] (a : α) : lcm a a = normalize a :=
lcm_eq_normalize (lcm_dvd dvd_rfl dvd_rfl) (dvd_lcm_left _ _)
@[simp]
theorem lcm_eq_one_iff [NormalizedGCDMonoid α] (a b : α) : lcm a b = 1 ↔ a ∣ 1 ∧ b ∣ 1 :=
Iff.intro (fun eq => eq ▸ ⟨dvd_lcm_left _ _, dvd_lcm_right _ _⟩) fun ⟨⟨c, hc⟩, ⟨d, hd⟩⟩ =>
show lcm (Units.mkOfMulEqOne a c hc.symm : α) (Units.mkOfMulEqOne b d hd.symm) = 1 by
rw [lcm_units_coe_left, normalize_coe_units]
@[simp]
theorem lcm_mul_left [NormalizedGCDMonoid α] (a b c : α) :
lcm (a * b) (a * c) = normalize a * lcm b c :=
(by_cases (by rintro rfl; simp only [zero_mul, lcm_zero_left, normalize_zero]))
fun ha : a ≠ 0 =>
suffices lcm (a * b) (a * c) = normalize (a * lcm b c) by simpa
have : a ∣ lcm (a * b) (a * c) := (dvd_mul_right _ _).trans (dvd_lcm_left _ _)
let ⟨_, eq⟩ := this
lcm_eq_normalize
(lcm_dvd (mul_dvd_mul_left a (dvd_lcm_left _ _)) (mul_dvd_mul_left a (dvd_lcm_right _ _)))
(eq.symm ▸
(mul_dvd_mul_left a <|
lcm_dvd ((mul_dvd_mul_iff_left ha).1 <| eq ▸ dvd_lcm_left _ _)
((mul_dvd_mul_iff_left ha).1 <| eq ▸ dvd_lcm_right _ _)))
@[simp]
theorem lcm_mul_right [NormalizedGCDMonoid α] (a b c : α) :
lcm (b * a) (c * a) = lcm b c * normalize a := by simp only [mul_comm, lcm_mul_left]
theorem lcm_eq_left_iff [NormalizedGCDMonoid α] (a b : α) (h : normalize a = a) :
lcm a b = a ↔ b ∣ a :=
(Iff.intro fun eq => eq ▸ dvd_lcm_right _ _) fun hab =>
dvd_antisymm_of_normalize_eq (normalize_lcm _ _) h (lcm_dvd (dvd_refl a) hab) (dvd_lcm_left _ _)
theorem lcm_eq_right_iff [NormalizedGCDMonoid α] (a b : α) (h : normalize b = b) :
lcm a b = b ↔ a ∣ b := by simpa only [lcm_comm b a] using lcm_eq_left_iff b a h
theorem lcm_dvd_lcm_mul_left [GCDMonoid α] (m n k : α) : lcm m n ∣ lcm (k * m) n :=
lcm_dvd_lcm (dvd_mul_left _ _) dvd_rfl
theorem lcm_dvd_lcm_mul_right [GCDMonoid α] (m n k : α) : lcm m n ∣ lcm (m * k) n :=
lcm_dvd_lcm (dvd_mul_right _ _) dvd_rfl
theorem lcm_dvd_lcm_mul_left_right [GCDMonoid α] (m n k : α) : lcm m n ∣ lcm m (k * n) :=
lcm_dvd_lcm dvd_rfl (dvd_mul_left _ _)
theorem lcm_dvd_lcm_mul_right_right [GCDMonoid α] (m n k : α) : lcm m n ∣ lcm m (n * k) :=
lcm_dvd_lcm dvd_rfl (dvd_mul_right _ _)
theorem lcm_eq_of_associated_left [NormalizedGCDMonoid α] {m n : α} (h : Associated m n) (k : α) :
lcm m k = lcm n k :=
dvd_antisymm_of_normalize_eq (normalize_lcm _ _) (normalize_lcm _ _) (lcm_dvd_lcm h.dvd dvd_rfl)
(lcm_dvd_lcm h.symm.dvd dvd_rfl)
theorem lcm_eq_of_associated_right [NormalizedGCDMonoid α] {m n : α} (h : Associated m n) (k : α) :
lcm k m = lcm k n :=
dvd_antisymm_of_normalize_eq (normalize_lcm _ _) (normalize_lcm _ _) (lcm_dvd_lcm dvd_rfl h.dvd)
(lcm_dvd_lcm dvd_rfl h.symm.dvd)
section Divisibility
variable [GCDMonoid α] {m n a b c : α}
variable (m n) in
@[simp] theorem lcm_dvd_mul : lcm m n ∣ m * n :=
lcm_dvd (by simp) (by simp)
theorem dvd_lcm_of_dvd_left (h : a ∣ b) (c : α) : a ∣ lcm b c :=
h.trans (dvd_lcm_left b c)
alias Dvd.dvd.lcm_right := dvd_lcm_of_dvd_left
theorem dvd_of_lcm_right_dvd (h : lcm a b ∣ c) : a ∣ c :=
(dvd_lcm_left a b).trans h
theorem dvd_lcm_of_dvd_right (h : a ∣ b) (c : α) : a ∣ lcm c b :=
h.trans (dvd_lcm_right c b)
alias Dvd.dvd.lcm_left := dvd_lcm_of_dvd_right
theorem dvd_of_lcm_left_dvd (h : lcm a b ∣ c) : b ∣ c :=
(dvd_lcm_right a b).trans h
namespace Prime
variable {p : α} (hp : Prime p)
include hp
theorem dvd_or_dvd_of_dvd_lcm (h : p ∣ lcm a b) : p ∣ a ∨ p ∣ b :=
dvd_or_dvd hp (h.trans (lcm_dvd_mul a b))
theorem dvd_lcm : p ∣ lcm a b ↔ p ∣ a ∨ p ∣ b :=
⟨hp.dvd_or_dvd_of_dvd_lcm, (Or.elim · (dvd_lcm_of_dvd_left · _) (dvd_lcm_of_dvd_right · _))⟩
theorem not_dvd_lcm (ha : ¬ p ∣ a) (hb : ¬ p ∣ b) : ¬ p ∣ lcm a b :=
hp.dvd_lcm.not.mpr <| not_or.mpr ⟨ha, hb⟩
end Prime
end Divisibility
end LCM
end GCDMonoid
section UniqueUnit
variable [CancelCommMonoidWithZero α] [Subsingleton αˣ]
-- see Note [lower instance priority]
instance (priority := 100) NormalizationMonoid.ofUniqueUnits : NormalizationMonoid α where
normUnit _ := 1
normUnit_zero := rfl
normUnit_mul _ _ := (mul_one 1).symm
normUnit_coe_units _ := Subsingleton.elim _ _
instance uniqueNormalizationMonoidOfUniqueUnits : Unique (NormalizationMonoid α) where
default := .ofUniqueUnits
uniq := fun ⟨u, _, _, _⟩ => by congr; simp [eq_iff_true_of_subsingleton]
instance subsingleton_gcdMonoid_of_unique_units : Subsingleton (GCDMonoid α) :=
⟨fun g₁ g₂ => by
have hgcd : g₁.gcd = g₂.gcd := by
ext a b
refine associated_iff_eq.mp (associated_of_dvd_dvd ?_ ?_) <;>
apply_rules +allowSynthFailures [dvd_gcd, gcd_dvd_left, gcd_dvd_right]
have hlcm : g₁.lcm = g₂.lcm := by
ext a b
refine associated_iff_eq.mp (associated_of_dvd_dvd ?_ ?_) <;>
apply_rules +allowSynthFailures [lcm_dvd, dvd_lcm_left, dvd_lcm_right]
cases g₁
cases g₂
dsimp only at hgcd hlcm
simp only [hgcd, hlcm]⟩
instance subsingleton_normalizedGCDMonoid_of_unique_units : Subsingleton (NormalizedGCDMonoid α) :=
⟨by
intro a b
cases a; rename_i a_norm a_gcd _ _
cases b; rename_i b_norm b_gcd _ _
have := Subsingleton.elim a_gcd b_gcd
subst this
have := Subsingleton.elim a_norm b_norm
subst this
rfl⟩
@[simp]
theorem normUnit_eq_one (x : α) : normUnit x = 1 :=
rfl
@[simp]
theorem normalize_eq (x : α) : normalize x = x :=
mul_one x
/-- If a monoid's only unit is `1`, then it is isomorphic to its associates. -/
@[simps]
def associatesEquivOfUniqueUnits : Associates α ≃* α where
toFun := Associates.out
invFun := Associates.mk
left_inv := Associates.mk_out
right_inv _ := (Associates.out_mk _).trans <| normalize_eq _
map_mul' := Associates.out_mul
end UniqueUnit
section IsDomain
variable [CommRing α] [IsDomain α] [NormalizedGCDMonoid α]
theorem gcd_eq_of_dvd_sub_right {a b c : α} (h : a ∣ b - c) : gcd a b = gcd a c := by
apply dvd_antisymm_of_normalize_eq (normalize_gcd _ _) (normalize_gcd _ _) <;>
rw [dvd_gcd_iff] <;>
refine ⟨gcd_dvd_left _ _, ?_⟩
· rcases h with ⟨d, hd⟩
rcases gcd_dvd_right a b with ⟨e, he⟩
rcases gcd_dvd_left a b with ⟨f, hf⟩
use e - f * d
rw [mul_sub, ← he, ← mul_assoc, ← hf, ← hd, sub_sub_cancel]
· rcases h with ⟨d, hd⟩
rcases gcd_dvd_right a c with ⟨e, he⟩
rcases gcd_dvd_left a c with ⟨f, hf⟩
use e + f * d
rw [mul_add, ← he, ← mul_assoc, ← hf, ← hd, ← add_sub_assoc, add_comm c b, add_sub_cancel_right]
theorem gcd_eq_of_dvd_sub_left {a b c : α} (h : a ∣ b - c) : gcd b a = gcd c a := by
rw [gcd_comm _ a, gcd_comm _ a, gcd_eq_of_dvd_sub_right h]
end IsDomain
noncomputable section Constructors
open Associates
variable [CancelCommMonoidWithZero α]
private theorem map_mk_unit_aux [DecidableEq α] {f : Associates α →* α}
(hinv : Function.RightInverse f Associates.mk) (a : α) :
a * ↑(Classical.choose (associated_map_mk hinv a)) = f (Associates.mk a) :=
Classical.choose_spec (associated_map_mk hinv a)
/-- Define `NormalizationMonoid` on a structure from a `MonoidHom` inverse to `Associates.mk`. -/
def normalizationMonoidOfMonoidHomRightInverse [DecidableEq α] (f : Associates α →* α)
(hinv : Function.RightInverse f Associates.mk) :
NormalizationMonoid α where
normUnit a :=
if a = 0 then 1
else Classical.choose (Associates.mk_eq_mk_iff_associated.1 (hinv (Associates.mk a)).symm)
normUnit_zero := if_pos rfl
normUnit_mul {a b} ha hb := by
simp_rw [if_neg (mul_ne_zero ha hb), if_neg ha, if_neg hb, Units.ext_iff, Units.val_mul]
suffices a * b * ↑(Classical.choose (associated_map_mk hinv (a * b))) =
a * ↑(Classical.choose (associated_map_mk hinv a)) *
(b * ↑(Classical.choose (associated_map_mk hinv b))) by
apply mul_left_cancel₀ (mul_ne_zero ha hb) _
simpa only [mul_assoc, mul_comm, mul_left_comm] using this
rw [map_mk_unit_aux hinv a, map_mk_unit_aux hinv (a * b), map_mk_unit_aux hinv b, ←
MonoidHom.map_mul, Associates.mk_mul_mk]
normUnit_coe_units u := by
nontriviality α
simp_rw [if_neg (Units.ne_zero u), Units.ext_iff]
apply mul_left_cancel₀ (Units.ne_zero u)
rw [Units.mul_inv, map_mk_unit_aux hinv u,
Associates.mk_eq_mk_iff_associated.2 (associated_one_iff_isUnit.2 ⟨u, rfl⟩),
Associates.mk_one, MonoidHom.map_one]
/-- Define `GCDMonoid` on a structure just from the `gcd` and its properties. -/
noncomputable def gcdMonoidOfGCD [DecidableEq α] (gcd : α → α → α)
(gcd_dvd_left : ∀ a b, gcd a b ∣ a) (gcd_dvd_right : ∀ a b, gcd a b ∣ b)
(dvd_gcd : ∀ {a b c}, a ∣ c → a ∣ b → a ∣ gcd c b) : GCDMonoid α :=
{ gcd
gcd_dvd_left
gcd_dvd_right
dvd_gcd := fun {_ _ _} => dvd_gcd
lcm := fun a b =>
if a = 0 then 0 else Classical.choose ((gcd_dvd_left a b).trans (Dvd.intro b rfl))
gcd_mul_lcm := fun a b => by
split_ifs with a0
· rw [mul_zero, a0, zero_mul]
· rw [← Classical.choose_spec ((gcd_dvd_left a b).trans (Dvd.intro b rfl))]
lcm_zero_left := fun _ => if_pos rfl
lcm_zero_right := fun a => by
split_ifs with a0
· rfl
have h := (Classical.choose_spec ((gcd_dvd_left a 0).trans (Dvd.intro 0 rfl))).symm
have a0' : gcd a 0 ≠ 0 := by
contrapose! a0
rw [← associated_zero_iff_eq_zero, ← a0]
exact associated_of_dvd_dvd (dvd_gcd (dvd_refl a) (dvd_zero a)) (gcd_dvd_left _ _)
apply Or.resolve_left (mul_eq_zero.1 _) a0'
rw [h, mul_zero] }
/-- Define `NormalizedGCDMonoid` on a structure just from the `gcd` and its properties. -/
noncomputable def normalizedGCDMonoidOfGCD [NormalizationMonoid α] [DecidableEq α] (gcd : α → α → α)
(gcd_dvd_left : ∀ a b, gcd a b ∣ a) (gcd_dvd_right : ∀ a b, gcd a b ∣ b)
(dvd_gcd : ∀ {a b c}, a ∣ c → a ∣ b → a ∣ gcd c b)
(normalize_gcd : ∀ a b, normalize (gcd a b) = gcd a b) : NormalizedGCDMonoid α :=
{ (inferInstance : NormalizationMonoid α) with
gcd
gcd_dvd_left
gcd_dvd_right
dvd_gcd := fun {_ _ _} => dvd_gcd
normalize_gcd
lcm := fun a b =>
if a = 0 then 0
else Classical.choose (dvd_normalize_iff.2 ((gcd_dvd_left a b).trans (Dvd.intro b rfl)))
normalize_lcm := fun a b => by
dsimp [normalize]
split_ifs with a0
· exact @normalize_zero α _ _
· have := (Classical.choose_spec
(dvd_normalize_iff.2 ((gcd_dvd_left a b).trans (Dvd.intro b rfl)))).symm
set l := Classical.choose (dvd_normalize_iff.2 ((gcd_dvd_left a b).trans (Dvd.intro b rfl)))
obtain rfl | hb := eq_or_ne b 0
· rw [mul_zero a, normalize_zero, mul_eq_zero] at this
obtain ha | hl := this
· apply (a0 _).elim
rw [← zero_dvd_iff, ← ha]
exact gcd_dvd_left _ _
· rw [hl, zero_mul]
have h1 : gcd a b ≠ 0 := by
have hab : a * b ≠ 0 := mul_ne_zero a0 hb
contrapose! hab
rw [← normalize_eq_zero, ← this, hab, zero_mul]
have h2 : normalize (gcd a b * l) = gcd a b * l := by rw [this, normalize_idem]
rw [← normalize_gcd] at this
rwa [normalize.map_mul, normalize_gcd, mul_right_inj' h1] at h2
gcd_mul_lcm := fun a b => by
split_ifs with a0
· rw [mul_zero, a0, zero_mul]
· rw [← Classical.choose_spec (dvd_normalize_iff.2 ((gcd_dvd_left a b).trans (.intro b rfl)))]
exact normalize_associated (a * b)
lcm_zero_left := fun _ => if_pos rfl
lcm_zero_right := fun a => by
split_ifs with a0
· rfl
rw [← normalize_eq_zero] at a0
have h :=
(Classical.choose_spec (dvd_normalize_iff.2 ((gcd_dvd_left a 0).trans (.intro 0 rfl)))).symm
have gcd0 : gcd a 0 = normalize a := by
rw [← normalize_gcd]
exact normalize_eq_normalize (gcd_dvd_left _ _) (dvd_gcd (dvd_refl a) (dvd_zero a))
rw [← gcd0] at a0
apply Or.resolve_left (mul_eq_zero.1 _) a0
rw [h, mul_zero, normalize_zero] }
/-- Define `GCDMonoid` on a structure just from the `lcm` and its properties. -/
noncomputable def gcdMonoidOfLCM [DecidableEq α] (lcm : α → α → α)
(dvd_lcm_left : ∀ a b, a ∣ lcm a b) (dvd_lcm_right : ∀ a b, b ∣ lcm a b)
(lcm_dvd : ∀ {a b c}, c ∣ a → b ∣ a → lcm c b ∣ a) : GCDMonoid α :=
let exists_gcd a b := lcm_dvd (Dvd.intro b rfl) (Dvd.intro_left a rfl)
{ lcm
gcd := fun a b => if a = 0 then b else if b = 0 then a else Classical.choose (exists_gcd a b)
gcd_mul_lcm := fun a b => by
split_ifs with h h_1
· rw [h, eq_zero_of_zero_dvd (dvd_lcm_left _ _), mul_zero, zero_mul]
· rw [h_1, eq_zero_of_zero_dvd (dvd_lcm_right _ _)]
rw [mul_comm, ← Classical.choose_spec (exists_gcd a b)]
lcm_zero_left := fun _ => eq_zero_of_zero_dvd (dvd_lcm_left _ _)
lcm_zero_right := fun _ => eq_zero_of_zero_dvd (dvd_lcm_right _ _)
gcd_dvd_left := fun a b => by
split_ifs with h h_1
· rw [h]
apply dvd_zero
· exact dvd_rfl
have h0 : lcm a b ≠ 0 := by
intro con
have h := lcm_dvd (Dvd.intro b rfl) (Dvd.intro_left a rfl)
rw [con, zero_dvd_iff, mul_eq_zero] at h
cases h
· exact absurd ‹a = 0› h
· exact absurd ‹b = 0› h_1
rw [← mul_dvd_mul_iff_left h0, ← Classical.choose_spec (exists_gcd a b), mul_comm,
mul_dvd_mul_iff_right h]
apply dvd_lcm_right
gcd_dvd_right := fun a b => by
split_ifs with h h_1
· exact dvd_rfl
· rw [h_1]
apply dvd_zero
have h0 : lcm a b ≠ 0 := by
intro con
have h := lcm_dvd (Dvd.intro b rfl) (Dvd.intro_left a rfl)
rw [con, zero_dvd_iff, mul_eq_zero] at h
cases h
· exact absurd ‹a = 0› h
· exact absurd ‹b = 0› h_1
rw [← mul_dvd_mul_iff_left h0, ← Classical.choose_spec (exists_gcd a b),
mul_dvd_mul_iff_right h_1]
apply dvd_lcm_left
dvd_gcd := fun {a b c} ac ab => by
split_ifs with h h_1
· exact ab
· exact ac
have h0 : lcm c b ≠ 0 := by
intro con
have h := lcm_dvd (Dvd.intro b rfl) (Dvd.intro_left c rfl)
rw [con, zero_dvd_iff, mul_eq_zero] at h
cases h
· exact absurd ‹c = 0› h
· exact absurd ‹b = 0› h_1
rw [← mul_dvd_mul_iff_left h0, ← Classical.choose_spec (exists_gcd c b)]
rcases ab with ⟨d, rfl⟩
rw [mul_eq_zero] at ‹a * d ≠ 0›
push_neg at h_1
rw [mul_comm a, ← mul_assoc, mul_dvd_mul_iff_right h_1.1]
apply lcm_dvd (Dvd.intro d rfl)
rw [mul_comm, mul_dvd_mul_iff_right h_1.2]
apply ac }
/-- Define `NormalizedGCDMonoid` on a structure just from the `lcm` and its properties. -/
noncomputable def normalizedGCDMonoidOfLCM [NormalizationMonoid α] [DecidableEq α] (lcm : α → α → α)
(dvd_lcm_left : ∀ a b, a ∣ lcm a b) (dvd_lcm_right : ∀ a b, b ∣ lcm a b)
(lcm_dvd : ∀ {a b c}, c ∣ a → b ∣ a → lcm c b ∣ a)
(normalize_lcm : ∀ a b, normalize (lcm a b) = lcm a b) : NormalizedGCDMonoid α :=
let exists_gcd a b := dvd_normalize_iff.2 (lcm_dvd (Dvd.intro b rfl) (Dvd.intro_left a rfl))
{ (inferInstance : NormalizationMonoid α) with
lcm
gcd := fun a b =>
if a = 0 then normalize b
else if b = 0 then normalize a else Classical.choose (exists_gcd a b)
gcd_mul_lcm := fun a b => by
split_ifs with h h_1
· rw [h, eq_zero_of_zero_dvd (dvd_lcm_left _ _), mul_zero, zero_mul]
· rw [h_1, eq_zero_of_zero_dvd (dvd_lcm_right _ _), mul_zero, mul_zero]
rw [mul_comm, ← Classical.choose_spec (exists_gcd a b)]
exact normalize_associated (a * b)
normalize_lcm
normalize_gcd := fun a b => by
dsimp [normalize]
split_ifs with h h_1
· apply normalize_idem
· apply normalize_idem
have h0 : lcm a b ≠ 0 := by
intro con
have h := lcm_dvd (Dvd.intro b rfl) (Dvd.intro_left a rfl)
rw [con, zero_dvd_iff, mul_eq_zero] at h
cases h
· exact absurd ‹a = 0› h
· exact absurd ‹b = 0› h_1
apply mul_left_cancel₀ h0
refine _root_.trans ?_ (Classical.choose_spec (exists_gcd a b))
conv_lhs =>
congr
rw [← normalize_lcm a b]
rw [← normalize_apply, ← normalize.map_mul,
← Classical.choose_spec (exists_gcd a b), normalize_idem]
lcm_zero_left := fun _ => eq_zero_of_zero_dvd (dvd_lcm_left _ _)
lcm_zero_right := fun _ => eq_zero_of_zero_dvd (dvd_lcm_right _ _)
gcd_dvd_left := fun a b => by
split_ifs with h h_1
· rw [h]
apply dvd_zero
· exact (normalize_associated _).dvd
have h0 : lcm a b ≠ 0 := by
intro con
have h := lcm_dvd (Dvd.intro b rfl) (Dvd.intro_left a rfl)
rw [con, zero_dvd_iff, mul_eq_zero] at h
cases h
· exact absurd ‹a = 0› h
· exact absurd ‹b = 0› h_1
rw [← mul_dvd_mul_iff_left h0, ← Classical.choose_spec (exists_gcd a b), normalize_dvd_iff,
mul_comm, mul_dvd_mul_iff_right h]
apply dvd_lcm_right
gcd_dvd_right := fun a b => by
split_ifs with h h_1
· exact (normalize_associated _).dvd
· rw [h_1]
apply dvd_zero
have h0 : lcm a b ≠ 0 := by
intro con
have h := lcm_dvd (Dvd.intro b rfl) (Dvd.intro_left a rfl)
rw [con, zero_dvd_iff, mul_eq_zero] at h
cases h
· exact absurd ‹a = 0› h
· exact absurd ‹b = 0› h_1
rw [← mul_dvd_mul_iff_left h0, ← Classical.choose_spec (exists_gcd a b), normalize_dvd_iff,
mul_dvd_mul_iff_right h_1]
apply dvd_lcm_left
dvd_gcd := fun {a b c} ac ab => by
split_ifs with h h_1
· apply dvd_normalize_iff.2 ab
· apply dvd_normalize_iff.2 ac
have h0 : lcm c b ≠ 0 := by
intro con
have h := lcm_dvd (Dvd.intro b rfl) (Dvd.intro_left c rfl)
rw [con, zero_dvd_iff, mul_eq_zero] at h
cases h
· exact absurd ‹c = 0› h
· exact absurd ‹b = 0› h_1
rw [← mul_dvd_mul_iff_left h0, ← Classical.choose_spec
(dvd_normalize_iff.2 (lcm_dvd (Dvd.intro b rfl) (Dvd.intro_left c rfl))),
dvd_normalize_iff]
rcases ab with ⟨d, rfl⟩
rw [mul_eq_zero] at h_1
push_neg at h_1
rw [mul_comm a, ← mul_assoc, mul_dvd_mul_iff_right h_1.1]
apply lcm_dvd (Dvd.intro d rfl)
rw [mul_comm, mul_dvd_mul_iff_right h_1.2]
apply ac }
/-- Define a `GCDMonoid` structure on a monoid just from the existence of a `gcd`. -/
noncomputable def gcdMonoidOfExistsGCD [DecidableEq α]
(h : ∀ a b : α, ∃ c : α, ∀ d : α, d ∣ a ∧ d ∣ b ↔ d ∣ c) : GCDMonoid α :=
gcdMonoidOfGCD (fun a b => Classical.choose (h a b))
(fun a b => ((Classical.choose_spec (h a b) (Classical.choose (h a b))).2 dvd_rfl).1)
(fun a b => ((Classical.choose_spec (h a b) (Classical.choose (h a b))).2 dvd_rfl).2)
fun {a b c} ac ab => (Classical.choose_spec (h c b) a).1 ⟨ac, ab⟩
/-- Define a `NormalizedGCDMonoid` structure on a monoid just from the existence of a `gcd`. -/
noncomputable def normalizedGCDMonoidOfExistsGCD [NormalizationMonoid α] [DecidableEq α]
(h : ∀ a b : α, ∃ c : α, ∀ d : α, d ∣ a ∧ d ∣ b ↔ d ∣ c) : NormalizedGCDMonoid α :=
normalizedGCDMonoidOfGCD (fun a b => normalize (Classical.choose (h a b)))
(fun a b =>
normalize_dvd_iff.2 ((Classical.choose_spec (h a b) (Classical.choose (h a b))).2 dvd_rfl).1)
(fun a b =>
normalize_dvd_iff.2 ((Classical.choose_spec (h a b) (Classical.choose (h a b))).2 dvd_rfl).2)
(fun {a b c} ac ab => dvd_normalize_iff.2 ((Classical.choose_spec (h c b) a).1 ⟨ac, ab⟩))
fun _ _ => normalize_idem _
/-- Define a `GCDMonoid` structure on a monoid just from the existence of an `lcm`. -/
noncomputable def gcdMonoidOfExistsLCM [DecidableEq α]
(h : ∀ a b : α, ∃ c : α, ∀ d : α, a ∣ d ∧ b ∣ d ↔ c ∣ d) : GCDMonoid α :=
gcdMonoidOfLCM (fun a b => Classical.choose (h a b))
(fun a b => ((Classical.choose_spec (h a b) (Classical.choose (h a b))).2 dvd_rfl).1)
(fun a b => ((Classical.choose_spec (h a b) (Classical.choose (h a b))).2 dvd_rfl).2)
fun {a b c} ac ab => (Classical.choose_spec (h c b) a).1 ⟨ac, ab⟩
/-- Define a `NormalizedGCDMonoid` structure on a monoid just from the existence of an `lcm`. -/
noncomputable def normalizedGCDMonoidOfExistsLCM [NormalizationMonoid α] [DecidableEq α]
(h : ∀ a b : α, ∃ c : α, ∀ d : α, a ∣ d ∧ b ∣ d ↔ c ∣ d) : NormalizedGCDMonoid α :=
normalizedGCDMonoidOfLCM (fun a b => normalize (Classical.choose (h a b)))
(fun a b =>
dvd_normalize_iff.2 ((Classical.choose_spec (h a b) (Classical.choose (h a b))).2 dvd_rfl).1)
(fun a b =>
dvd_normalize_iff.2 ((Classical.choose_spec (h a b) (Classical.choose (h a b))).2 dvd_rfl).2)
(fun {a b c} ac ab => normalize_dvd_iff.2 ((Classical.choose_spec (h c b) a).1 ⟨ac, ab⟩))
fun _ _ => normalize_idem _
end Constructors
namespace CommGroupWithZero
variable (G₀ : Type*) [CommGroupWithZero G₀] [DecidableEq G₀]
-- see Note [lower instance priority]
instance (priority := 100) : NormalizedGCDMonoid G₀ where
normUnit x := if h : x = 0 then 1 else (Units.mk0 x h)⁻¹
normUnit_zero := dif_pos rfl
normUnit_mul {x y} x0 y0 := Units.ext <| by simp [x0, y0, mul_comm]
normUnit_coe_units u := by simp
gcd a b := if a = 0 ∧ b = 0 then 0 else 1
lcm a b := if a = 0 ∨ b = 0 then 0 else 1
gcd_dvd_left a b := by simp +contextual
gcd_dvd_right a b := by simp +contextual
dvd_gcd {a b c} hac hab := by simp_all
gcd_mul_lcm a b := by
split_ifs <;> simp_all [Associated.comm]
lcm_zero_left _ := if_pos (Or.inl rfl)
lcm_zero_right _ := if_pos (Or.inr rfl)
-- `split_ifs` wants to split `normalize`, so handle the cases manually
normalize_gcd a b := if h : a = 0 ∧ b = 0 then by simp [if_pos h] else by simp [if_neg h]
normalize_lcm a b := if h : a = 0 ∨ b = 0 then by simp [if_pos h] else by simp [if_neg h]
@[simp]
theorem coe_normUnit {a : G₀} (h0 : a ≠ 0) : (↑(normUnit a) : G₀) = a⁻¹ := by simp [normUnit, h0]
theorem normalize_eq_one {a : G₀} (h0 : a ≠ 0) : normalize a = 1 := by simp [normalize_apply, h0]
end CommGroupWithZero
namespace Associates
variable [CancelCommMonoidWithZero α] [GCDMonoid α]
instance instGCDMonoid : GCDMonoid (Associates α) where
gcd := Quotient.map₂ gcd fun _ _ (ha : Associated _ _) _ _ (hb : Associated _ _) => ha.gcd hb
lcm := Quotient.map₂ lcm fun _ _ (ha : Associated _ _) _ _ (hb : Associated _ _) => ha.lcm hb
gcd_dvd_left := by rintro ⟨a⟩ ⟨b⟩; exact mk_le_mk_of_dvd (gcd_dvd_left _ _)
gcd_dvd_right := by rintro ⟨a⟩ ⟨b⟩; exact mk_le_mk_of_dvd (gcd_dvd_right _ _)
dvd_gcd := by
rintro ⟨a⟩ ⟨b⟩ ⟨c⟩ hac hbc
exact mk_le_mk_of_dvd (dvd_gcd (dvd_of_mk_le_mk hac) (dvd_of_mk_le_mk hbc))
gcd_mul_lcm := by
rintro ⟨a⟩ ⟨b⟩
rw [associated_iff_eq]
exact Quotient.sound <| gcd_mul_lcm _ _
lcm_zero_left := by rintro ⟨a⟩; exact congr_arg Associates.mk <| lcm_zero_left _
lcm_zero_right := by rintro ⟨a⟩; exact congr_arg Associates.mk <| lcm_zero_right _
theorem gcd_mk_mk {a b : α} : gcd (Associates.mk a) (Associates.mk b) = Associates.mk (gcd a b) :=
rfl
theorem lcm_mk_mk {a b : α} : lcm (Associates.mk a) (Associates.mk b) = Associates.mk (lcm a b) :=
rfl
end Associates |
.lake/packages/mathlib/Mathlib/Algebra/GCDMonoid/Multiset.lean | import Mathlib.Algebra.GCDMonoid.Basic
import Mathlib.Algebra.Order.Group.Multiset
import Mathlib.Data.Multiset.FinsetOps
import Mathlib.Data.Multiset.Fold
/-!
# GCD and LCM operations on multisets
## Main definitions
- `Multiset.gcd` - the greatest common denominator of a `Multiset` of elements of a `GCDMonoid`
- `Multiset.lcm` - the least common multiple of a `Multiset` of elements of a `GCDMonoid`
## Implementation notes
TODO: simplify with a tactic and `Data.Multiset.Lattice`
## Tags
multiset, gcd
-/
namespace Multiset
variable {α : Type*} [CancelCommMonoidWithZero α] [NormalizedGCDMonoid α]
/-! ### LCM -/
section lcm
/-- Least common multiple of a multiset -/
def lcm (s : Multiset α) : α :=
s.fold GCDMonoid.lcm 1
@[simp]
theorem lcm_zero : (0 : Multiset α).lcm = 1 :=
fold_zero _ _
@[simp]
theorem lcm_cons (a : α) (s : Multiset α) : (a ::ₘ s).lcm = GCDMonoid.lcm a s.lcm :=
fold_cons_left _ _ _ _
@[simp]
theorem lcm_singleton {a : α} : ({a} : Multiset α).lcm = normalize a :=
(fold_singleton _ _ _).trans <| lcm_one_right _
@[simp]
theorem lcm_add (s₁ s₂ : Multiset α) : (s₁ + s₂).lcm = GCDMonoid.lcm s₁.lcm s₂.lcm :=
Eq.trans (by simp [lcm]) (fold_add _ _ _ _ _)
theorem lcm_dvd {s : Multiset α} {a : α} : s.lcm ∣ a ↔ ∀ b ∈ s, b ∣ a :=
Multiset.induction_on s (by simp)
(by simp +contextual [or_imp, forall_and, lcm_dvd_iff])
theorem dvd_lcm {s : Multiset α} {a : α} (h : a ∈ s) : a ∣ s.lcm :=
lcm_dvd.1 dvd_rfl _ h
theorem lcm_mono {s₁ s₂ : Multiset α} (h : s₁ ⊆ s₂) : s₁.lcm ∣ s₂.lcm :=
lcm_dvd.2 fun _ hb ↦ dvd_lcm (h hb)
@[simp]
theorem normalize_lcm (s : Multiset α) : normalize s.lcm = s.lcm :=
Multiset.induction_on s (by simp) fun a s _ ↦ by simp
@[simp]
nonrec theorem lcm_eq_zero_iff [Nontrivial α] (s : Multiset α) : s.lcm = 0 ↔ 0 ∈ s := by
induction s using Multiset.induction_on with
| empty => simp only [lcm_zero, one_ne_zero, notMem_zero]
| cons a s ihs => simp only [mem_cons, lcm_cons, lcm_eq_zero_iff, ihs, @eq_comm _ a]
theorem lcm_ne_zero_iff [Nontrivial α] (s : Multiset α) : s.lcm ≠ 0 ↔ 0 ∉ s :=
not_congr (lcm_eq_zero_iff s)
variable [DecidableEq α]
@[simp]
theorem lcm_dedup (s : Multiset α) : (dedup s).lcm = s.lcm :=
Multiset.induction_on s (by simp) fun a s IH ↦ by
by_cases h : a ∈ s; swap; · simp [IH, h]
simp only [h, dedup_cons_of_mem, IH, lcm_cons]
unfold lcm
rw [← cons_erase h, fold_cons_left, ← lcm_assoc, lcm_same]
apply lcm_eq_of_associated_left (associated_normalize _)
@[simp]
theorem lcm_ndunion (s₁ s₂ : Multiset α) : (ndunion s₁ s₂).lcm = GCDMonoid.lcm s₁.lcm s₂.lcm := by
rw [← lcm_dedup, dedup_ext.2, lcm_dedup, lcm_add]
simp
@[simp]
theorem lcm_union (s₁ s₂ : Multiset α) : (s₁ ∪ s₂).lcm = GCDMonoid.lcm s₁.lcm s₂.lcm := by
rw [← lcm_dedup, dedup_ext.2, lcm_dedup, lcm_add]
simp
@[simp]
theorem lcm_ndinsert (a : α) (s : Multiset α) : (ndinsert a s).lcm = GCDMonoid.lcm a s.lcm := by
rw [← lcm_dedup, dedup_ext.2, lcm_dedup, lcm_cons]
simp
end lcm
/-! ### GCD -/
section gcd
/-- Greatest common divisor of a multiset -/
def gcd (s : Multiset α) : α :=
s.fold GCDMonoid.gcd 0
@[simp]
theorem gcd_zero : (0 : Multiset α).gcd = 0 :=
fold_zero _ _
@[simp]
theorem gcd_cons (a : α) (s : Multiset α) : (a ::ₘ s).gcd = GCDMonoid.gcd a s.gcd :=
fold_cons_left _ _ _ _
@[simp]
theorem gcd_singleton {a : α} : ({a} : Multiset α).gcd = normalize a :=
(fold_singleton _ _ _).trans <| gcd_zero_right _
@[simp]
theorem gcd_add (s₁ s₂ : Multiset α) : (s₁ + s₂).gcd = GCDMonoid.gcd s₁.gcd s₂.gcd :=
Eq.trans (by simp [gcd]) (fold_add _ _ _ _ _)
theorem dvd_gcd {s : Multiset α} {a : α} : a ∣ s.gcd ↔ ∀ b ∈ s, a ∣ b :=
Multiset.induction_on s (by simp)
(by simp +contextual [or_imp, forall_and, dvd_gcd_iff])
theorem gcd_dvd {s : Multiset α} {a : α} (h : a ∈ s) : s.gcd ∣ a :=
dvd_gcd.1 dvd_rfl _ h
theorem gcd_mono {s₁ s₂ : Multiset α} (h : s₁ ⊆ s₂) : s₂.gcd ∣ s₁.gcd :=
dvd_gcd.2 fun _ hb ↦ gcd_dvd (h hb)
@[simp]
theorem normalize_gcd (s : Multiset α) : normalize s.gcd = s.gcd :=
Multiset.induction_on s (by simp) fun a s _ ↦ by simp
theorem gcd_eq_zero_iff (s : Multiset α) : s.gcd = 0 ↔ ∀ x ∈ s, x = 0 := by
constructor
· intro h x hx
apply eq_zero_of_zero_dvd
rw [← h]
apply gcd_dvd hx
· refine s.induction_on ?_ ?_
· simp
intro a s sgcd h
simp [h a (mem_cons_self a s), sgcd fun x hx ↦ h x (mem_cons_of_mem hx)]
theorem gcd_ne_zero_iff (s : Multiset α) : s.gcd ≠ 0 ↔ ∃ x ∈ s, x ≠ 0 := by
simp [gcd_eq_zero_iff]
theorem gcd_map_mul (a : α) (s : Multiset α) : (s.map (a * ·)).gcd = normalize a * s.gcd := by
refine s.induction_on ?_ fun b s ih ↦ ?_
· simp_rw [map_zero, gcd_zero, mul_zero]
· simp_rw [map_cons, gcd_cons, ← gcd_mul_left]
rw [ih]
apply ((normalize_associated a).mul_right _).gcd_eq_right
section
variable [DecidableEq α]
@[simp]
theorem gcd_dedup (s : Multiset α) : (dedup s).gcd = s.gcd :=
Multiset.induction_on s (by simp) fun a s IH ↦ by
by_cases h : a ∈ s; swap; · simp [IH, h]
simp only [h, dedup_cons_of_mem, IH, gcd_cons]
unfold gcd
rw [← cons_erase h, fold_cons_left, ← gcd_assoc, gcd_same]
apply (associated_normalize _).gcd_eq_left
@[simp]
theorem gcd_ndunion (s₁ s₂ : Multiset α) : (ndunion s₁ s₂).gcd = GCDMonoid.gcd s₁.gcd s₂.gcd := by
rw [← gcd_dedup, dedup_ext.2, gcd_dedup, gcd_add]
simp
@[simp]
theorem gcd_union (s₁ s₂ : Multiset α) : (s₁ ∪ s₂).gcd = GCDMonoid.gcd s₁.gcd s₂.gcd := by
rw [← gcd_dedup, dedup_ext.2, gcd_dedup, gcd_add]
simp
@[simp]
theorem gcd_ndinsert (a : α) (s : Multiset α) : (ndinsert a s).gcd = GCDMonoid.gcd a s.gcd := by
rw [← gcd_dedup, dedup_ext.2, gcd_dedup, gcd_cons]
simp
end
theorem extract_gcd' (s t : Multiset α) (hs : ∃ x, x ∈ s ∧ x ≠ (0 : α))
(ht : s = t.map (s.gcd * ·)) : t.gcd = 1 :=
((@mul_right_eq_self₀ _ _ s.gcd _).1 <| by
conv_lhs => rw [← normalize_gcd, ← gcd_map_mul, ← ht]).resolve_right <| by
contrapose! hs
exact s.gcd_eq_zero_iff.1 hs
theorem extract_gcd (s : Multiset α) (hs : s ≠ 0) :
∃ t : Multiset α, s = t.map (s.gcd * ·) ∧ t.gcd = 1 := by
classical
by_cases! h : ∀ x ∈ s, x = (0 : α)
· use replicate (card s) 1
rw [map_replicate, eq_replicate, mul_one, s.gcd_eq_zero_iff.2 h, ← nsmul_singleton,
← gcd_dedup, dedup_nsmul (card_pos.2 hs).ne', dedup_singleton, gcd_singleton]
exact ⟨⟨rfl, h⟩, normalize_one⟩
· choose f hf using @gcd_dvd _ _ _ s
refine ⟨s.pmap @f fun _ ↦ id, ?_, extract_gcd' s _ h ?_⟩ <;>
· rw [map_pmap]
conv_lhs => rw [← s.map_id, ← s.pmap_eq_map _ _ fun _ ↦ id]
congr with (x hx)
rw [id, ← hf hx]
end gcd
end Multiset |
.lake/packages/mathlib/Mathlib/Algebra/BigOperators/Finprod.lean | import Mathlib.Algebra.BigOperators.Pi
import Mathlib.Algebra.Group.Indicator
import Mathlib.Algebra.Group.Support
import Mathlib.Algebra.NoZeroSMulDivisors.Basic
import Mathlib.Algebra.Notation.FiniteSupport
import Mathlib.Algebra.Order.BigOperators.Group.Finset
import Mathlib.Algebra.Order.Ring.Defs
import Mathlib.Data.Set.Finite.Lattice
/-!
# Finite products and sums over types and sets
We define products and sums over types and subsets of types, with no finiteness hypotheses.
All infinite products and sums are defined to be junk values (i.e. one or zero).
This approach is sometimes easier to use than `Finset.sum`,
when issues arise with `Finset` and `Fintype` being data.
## Main definitions
We use the following variables:
* `α`, `β` - types with no structure;
* `s`, `t` - sets
* `M`, `N` - additive or multiplicative commutative monoids
* `f`, `g` - functions
Definitions in this file:
* `finsum f : M` : the sum of `f x` as `x` ranges over the support of `f`, if it's finite.
Zero otherwise.
* `finprod f : M` : the product of `f x` as `x` ranges over the multiplicative support of `f`, if
it's finite. One otherwise.
## Notation
* `∑ᶠ i, f i` and `∑ᶠ i : α, f i` for `finsum f`
* `∏ᶠ i, f i` and `∏ᶠ i : α, f i` for `finprod f`
This notation works for functions `f : p → M`, where `p : Prop`, so the following works:
* `∑ᶠ i ∈ s, f i`, where `f : α → M`, `s : Set α` : sum over the set `s`;
* `∑ᶠ n < 5, f n`, where `f : ℕ → M` : same as `f 0 + f 1 + f 2 + f 3 + f 4`;
* `∏ᶠ (n >= -2) (hn : n < 3), f n`, where `f : ℤ → M` : same as `f (-2) * f (-1) * f 0 * f 1 * f 2`.
## Implementation notes
`finsum` and `finprod` is "yet another way of doing finite sums and products in Lean". However
experiments in the wild (e.g. with matroids) indicate that it is a helpful approach in settings
where the user is not interested in computability and wants to do reasoning without running into
typeclass diamonds caused by the constructive finiteness used in definitions such as `Finset` and
`Fintype`. By sticking solely to `Set.Finite` we avoid these problems. We are aware that there are
other solutions but for beginner mathematicians this approach is easier in practice.
Another application is the construction of a partition of unity from a collection of “bump”
functions. In this case the finite set depends on the point and it's convenient to have a definition
that does not mention the set explicitly.
The first arguments in all definitions and lemmas is the codomain of the function of the big
operator. This is necessary for the heuristic in `@[to_additive]`.
See the documentation of `to_additive.attr` for more information.
We did not add `IsFinite (X : Type) : Prop`, because it is simply `Nonempty (Fintype X)`.
## Tags
finsum, finprod, finite sum, finite product
-/
open Function Set
/-!
### Definition and relation to `Finset.sum` and `Finset.prod`
-/
section sort
variable {G M N : Type*} {α β ι : Sort*} [CommMonoid M] [CommMonoid N]
section
/- Note: we use classical logic only for these definitions, to ensure that we do not write lemmas
with `Classical.dec` in their statement. -/
open Classical in
/-- Sum of `f x` as `x` ranges over the elements of the support of `f`, if it's finite. Zero
otherwise. -/
noncomputable irreducible_def finsum (lemma := finsum_def') [AddCommMonoid M] (f : α → M) : M :=
if h : (support (f ∘ PLift.down)).Finite then ∑ i ∈ h.toFinset, f i.down else 0
open Classical in
/-- Product of `f x` as `x` ranges over the elements of the multiplicative support of `f`, if it's
finite. One otherwise. -/
@[to_additive existing]
noncomputable irreducible_def finprod (lemma := finprod_def') (f : α → M) : M :=
if h : (mulSupport (f ∘ PLift.down)).Finite then ∏ i ∈ h.toFinset, f i.down else 1
attribute [to_additive existing] finprod_def'
end
open Batteries.ExtendedBinder
/-- `∑ᶠ x, f x` is notation for `finsum f`. It is the sum of `f x`, where `x` ranges over the
support of `f`, if it's finite, zero otherwise. Taking the sum over multiple arguments or
conditions is possible, e.g. `∏ᶠ (x) (y), f x y` and `∏ᶠ (x) (h: x ∈ s), f x` -/
notation3"∑ᶠ " (...) ", " r:67:(scoped f => finsum f) => r
/-- `∏ᶠ x, f x` is notation for `finprod f`. It is the product of `f x`, where `x` ranges over the
multiplicative support of `f`, if it's finite, one otherwise. Taking the product over multiple
arguments or conditions is possible, e.g. `∏ᶠ (x) (y), f x y` and `∏ᶠ (x) (h: x ∈ s), f x` -/
notation3"∏ᶠ " (...) ", " r:67:(scoped f => finprod f) => r
-- Porting note: The following ports the lean3 notation for this file, but is currently very fickle.
-- syntax (name := bigfinsum) "∑ᶠ" extBinders ", " term:67 : term
-- macro_rules (kind := bigfinsum)
-- | `(∑ᶠ $x:ident, $p) => `(finsum (fun $x:ident ↦ $p))
-- | `(∑ᶠ $x:ident : $t, $p) => `(finsum (fun $x:ident : $t ↦ $p))
-- | `(∑ᶠ $x:ident $b:binderPred, $p) =>
-- `(finsum fun $x => (finsum (α := satisfies_binder_pred% $x $b) (fun _ => $p)))
-- | `(∑ᶠ ($x:ident) ($h:ident : $t), $p) =>
-- `(finsum fun ($x) => finsum (α := $t) (fun $h => $p))
-- | `(∑ᶠ ($x:ident : $_) ($h:ident : $t), $p) =>
-- `(finsum fun ($x) => finsum (α := $t) (fun $h => $p))
-- | `(∑ᶠ ($x:ident) ($y:ident), $p) =>
-- `(finsum fun $x => (finsum fun $y => $p))
-- | `(∑ᶠ ($x:ident) ($y:ident) ($h:ident : $t), $p) =>
-- `(finsum fun $x => (finsum fun $y => (finsum (α := $t) fun $h => $p)))
-- | `(∑ᶠ ($x:ident) ($y:ident) ($z:ident), $p) =>
-- `(finsum fun $x => (finsum fun $y => (finsum fun $z => $p)))
-- | `(∑ᶠ ($x:ident) ($y:ident) ($z:ident) ($h:ident : $t), $p) =>
-- `(finsum fun $x => (finsum fun $y => (finsum fun $z => (finsum (α := $t) fun $h => $p))))
--
--
-- syntax (name := bigfinprod) "∏ᶠ " extBinders ", " term:67 : term
-- macro_rules (kind := bigfinprod)
-- | `(∏ᶠ $x:ident, $p) => `(finprod (fun $x:ident ↦ $p))
-- | `(∏ᶠ $x:ident : $t, $p) => `(finprod (fun $x:ident : $t ↦ $p))
-- | `(∏ᶠ $x:ident $b:binderPred, $p) =>
-- `(finprod fun $x => (finprod (α := satisfies_binder_pred% $x $b) (fun _ => $p)))
-- | `(∏ᶠ ($x:ident) ($h:ident : $t), $p) =>
-- `(finprod fun ($x) => finprod (α := $t) (fun $h => $p))
-- | `(∏ᶠ ($x:ident : $_) ($h:ident : $t), $p) =>
-- `(finprod fun ($x) => finprod (α := $t) (fun $h => $p))
-- | `(∏ᶠ ($x:ident) ($y:ident), $p) =>
-- `(finprod fun $x => (finprod fun $y => $p))
-- | `(∏ᶠ ($x:ident) ($y:ident) ($h:ident : $t), $p) =>
-- `(finprod fun $x => (finprod fun $y => (finprod (α := $t) fun $h => $p)))
-- | `(∏ᶠ ($x:ident) ($y:ident) ($z:ident), $p) =>
-- `(finprod fun $x => (finprod fun $y => (finprod fun $z => $p)))
-- | `(∏ᶠ ($x:ident) ($y:ident) ($z:ident) ($h:ident : $t), $p) =>
-- `(finprod fun $x => (finprod fun $y => (finprod fun $z =>
-- (finprod (α := $t) fun $h => $p))))
@[to_additive]
theorem finprod_eq_prod_plift_of_mulSupport_toFinset_subset {f : α → M}
(hf : (mulSupport (f ∘ PLift.down)).Finite) {s : Finset (PLift α)} (hs : hf.toFinset ⊆ s) :
∏ᶠ i, f i = ∏ i ∈ s, f i.down := by
rw [finprod, dif_pos hf]
refine Finset.prod_subset hs fun x _ hxf => ?_
rwa [hf.mem_toFinset, notMem_mulSupport] at hxf
@[to_additive]
theorem finprod_eq_prod_plift_of_mulSupport_subset {f : α → M} {s : Finset (PLift α)}
(hs : mulSupport (f ∘ PLift.down) ⊆ s) : ∏ᶠ i, f i = ∏ i ∈ s, f i.down :=
finprod_eq_prod_plift_of_mulSupport_toFinset_subset (s.finite_toSet.subset hs) fun x hx => by
rw [Finite.mem_toFinset] at hx
exact hs hx
@[to_additive (attr := simp)]
theorem finprod_one : (∏ᶠ _ : α, (1 : M)) = 1 := by
have : (mulSupport fun x : PLift α => (fun _ => 1 : α → M) x.down) ⊆ (∅ : Finset (PLift α)) :=
fun x h => by simp at h
rw [finprod_eq_prod_plift_of_mulSupport_subset this, Finset.prod_empty]
@[to_additive]
theorem finprod_of_isEmpty [IsEmpty α] (f : α → M) : ∏ᶠ i, f i = 1 := by
rw [← finprod_one]
congr
simp [eq_iff_true_of_subsingleton]
@[to_additive (attr := simp)]
theorem finprod_false (f : False → M) : ∏ᶠ i, f i = 1 :=
finprod_of_isEmpty _
@[to_additive]
theorem finprod_eq_single (f : α → M) (a : α) (ha : ∀ x, x ≠ a → f x = 1) :
∏ᶠ x, f x = f a := by
have : mulSupport (f ∘ PLift.down) ⊆ ({PLift.up a} : Finset (PLift α)) := by
intro x
contrapose
simpa [PLift.eq_up_iff_down_eq] using ha x.down
rw [finprod_eq_prod_plift_of_mulSupport_subset this, Finset.prod_singleton]
@[to_additive]
theorem finprod_unique [Unique α] (f : α → M) : ∏ᶠ i, f i = f default :=
finprod_eq_single f default fun _x hx => (hx <| Unique.eq_default _).elim
@[to_additive (attr := simp)]
theorem finprod_true (f : True → M) : ∏ᶠ i, f i = f trivial :=
@finprod_unique M True _ ⟨⟨trivial⟩, fun _ => rfl⟩ f
@[to_additive]
theorem finprod_eq_dif {p : Prop} [Decidable p] (f : p → M) :
∏ᶠ i, f i = if h : p then f h else 1 := by
split_ifs with h
· haveI : Unique p := ⟨⟨h⟩, fun _ => rfl⟩
exact finprod_unique f
· haveI : IsEmpty p := ⟨h⟩
exact finprod_of_isEmpty f
@[to_additive]
theorem finprod_eq_if {p : Prop} [Decidable p] {x : M} : ∏ᶠ _ : p, x = if p then x else 1 :=
finprod_eq_dif fun _ => x
@[to_additive]
theorem finprod_congr {f g : α → M} (h : ∀ x, f x = g x) : finprod f = finprod g :=
congr_arg _ <| funext h
@[to_additive (attr := congr)]
theorem finprod_congr_Prop {p q : Prop} {f : p → M} {g : q → M} (hpq : p = q)
(hfg : ∀ h : q, f (hpq.mpr h) = g h) : finprod f = finprod g := by
subst q
exact finprod_congr hfg
/-- To prove a property of a finite product, it suffices to prove that the property is
multiplicative and holds on the factors. -/
@[to_additive
/-- To prove a property of a finite sum, it suffices to prove that the property is
additive and holds on the summands. -/]
theorem finprod_induction {f : α → M} (p : M → Prop) (hp₀ : p 1)
(hp₁ : ∀ x y, p x → p y → p (x * y)) (hp₂ : ∀ i, p (f i)) : p (∏ᶠ i, f i) := by
rw [finprod]
split_ifs
exacts [Finset.prod_induction _ _ hp₁ hp₀ fun i _ => hp₂ _, hp₀]
theorem finprod_nonneg {R : Type*} [CommSemiring R] [PartialOrder R] [IsOrderedRing R]
{f : α → R} (hf : ∀ x, 0 ≤ f x) :
0 ≤ ∏ᶠ x, f x :=
finprod_induction (fun x => 0 ≤ x) zero_le_one (fun _ _ => mul_nonneg) hf
@[to_additive finsum_nonneg]
theorem one_le_finprod' {M : Type*} [CommMonoid M] [PartialOrder M] [IsOrderedMonoid M]
{f : α → M} (hf : ∀ i, 1 ≤ f i) :
1 ≤ ∏ᶠ i, f i :=
finprod_induction _ le_rfl (fun _ _ => one_le_mul) hf
@[to_additive]
theorem MonoidHom.map_finprod_plift (f : M →* N) (g : α → M)
(h : (mulSupport <| g ∘ PLift.down).Finite) : f (∏ᶠ x, g x) = ∏ᶠ x, f (g x) := by
rw [finprod_eq_prod_plift_of_mulSupport_subset h.coe_toFinset.ge,
finprod_eq_prod_plift_of_mulSupport_subset, map_prod]
rw [h.coe_toFinset]
exact mulSupport_comp_subset f.map_one (g ∘ PLift.down)
@[to_additive]
theorem MonoidHom.map_finprod_Prop {p : Prop} (f : M →* N) (g : p → M) :
f (∏ᶠ x, g x) = ∏ᶠ x, f (g x) :=
f.map_finprod_plift g (Set.toFinite _)
@[to_additive]
theorem MonoidHom.map_finprod_of_preimage_one (f : M →* N) (hf : ∀ x, f x = 1 → x = 1) (g : α → M) :
f (∏ᶠ i, g i) = ∏ᶠ i, f (g i) := by
by_cases hg : (mulSupport <| g ∘ PLift.down).Finite; · exact f.map_finprod_plift g hg
rw [finprod, dif_neg, f.map_one, finprod, dif_neg]
exacts [Infinite.mono (fun x hx => mt (hf (g x.down)) hx) hg, hg]
@[to_additive]
theorem MonoidHom.map_finprod_of_injective (g : M →* N) (hg : Injective g) (f : α → M) :
g (∏ᶠ i, f i) = ∏ᶠ i, g (f i) :=
g.map_finprod_of_preimage_one (fun _ => (hg.eq_iff' g.map_one).mp) f
@[to_additive]
theorem MulEquiv.map_finprod (g : M ≃* N) (f : α → M) : g (∏ᶠ i, f i) = ∏ᶠ i, g (f i) :=
g.toMonoidHom.map_finprod_of_injective (EquivLike.injective g) f
@[to_additive]
theorem MulEquivClass.map_finprod {F : Type*} [EquivLike F M N] [MulEquivClass F M N] (g : F)
(f : α → M) : g (∏ᶠ i, f i) = ∏ᶠ i, g (f i) :=
MulEquiv.map_finprod (MulEquivClass.toMulEquiv g) f
/-- The `NoZeroSMulDivisors` makes sure that the result holds even when the support of `f` is
infinite. For a more usual version assuming `(support f).Finite` instead, see `finsum_smul'`. -/
theorem finsum_smul {R M : Type*} [Ring R] [AddCommGroup M] [Module R M] [NoZeroSMulDivisors R M]
(f : ι → R) (x : M) : (∑ᶠ i, f i) • x = ∑ᶠ i, f i • x := by
rcases eq_or_ne x 0 with (rfl | hx)
· simp
· exact ((smulAddHom R M).flip x).map_finsum_of_injective (smul_left_injective R hx) _
/-- The `NoZeroSMulDivisors` makes sure that the result holds even when the support of `f` is
infinite. For a more usual version assuming `(support f).Finite` instead, see `smul_finsum'`. -/
theorem smul_finsum {R M : Type*} [Semiring R] [AddCommGroup M] [Module R M]
[NoZeroSMulDivisors R M] (c : R) (f : ι → M) : (c • ∑ᶠ i, f i) = ∑ᶠ i, c • f i := by
rcases eq_or_ne c 0 with (rfl | hc)
· simp
· exact (smulAddHom R M c).map_finsum_of_injective (smul_right_injective M hc) _
@[to_additive]
theorem finprod_inv_distrib [DivisionCommMonoid G] (f : α → G) : (∏ᶠ x, (f x)⁻¹) = (∏ᶠ x, f x)⁻¹ :=
((MulEquiv.inv G).map_finprod f).symm
end sort
section type
variable {α β ι G M N : Type*} [CommMonoid M] [CommMonoid N]
@[to_additive]
theorem finprod_eq_mulIndicator_apply (s : Set α) (f : α → M) (a : α) :
∏ᶠ _ : a ∈ s, f a = mulIndicator s f a := by
classical convert finprod_eq_if (M := M) (p := a ∈ s) (x := f a)
@[to_additive (attr := simp)]
theorem finprod_apply_ne_one (f : α → M) (a : α) : ∏ᶠ _ : f a ≠ 1, f a = f a := by
rw [← mem_mulSupport, finprod_eq_mulIndicator_apply, mulIndicator_mulSupport]
@[to_additive]
theorem finprod_mem_def (s : Set α) (f : α → M) : ∏ᶠ a ∈ s, f a = ∏ᶠ a, mulIndicator s f a :=
finprod_congr <| finprod_eq_mulIndicator_apply s f
@[to_additive]
lemma finprod_mem_mulSupport (f : α → M) : ∏ᶠ a ∈ mulSupport f, f a = ∏ᶠ a, f a := by
rw [finprod_mem_def, mulIndicator_mulSupport]
@[to_additive]
theorem finprod_eq_prod_of_mulSupport_subset (f : α → M) {s : Finset α} (h : mulSupport f ⊆ s) :
∏ᶠ i, f i = ∏ i ∈ s, f i := by
have A : mulSupport (f ∘ PLift.down) = Equiv.plift.symm '' mulSupport f := by
rw [mulSupport_comp_eq_preimage]
exact (Equiv.plift.symm.image_eq_preimage_symm _).symm
have : mulSupport (f ∘ PLift.down) ⊆ s.map Equiv.plift.symm.toEmbedding := by
rw [A, Finset.coe_map]
exact image_mono h
rw [finprod_eq_prod_plift_of_mulSupport_subset this]
simp only [Finset.prod_map, Equiv.coe_toEmbedding]
congr
@[to_additive]
theorem finprod_eq_prod_of_mulSupport_toFinset_subset (f : α → M) (hf : (mulSupport f).Finite)
{s : Finset α} (h : hf.toFinset ⊆ s) : ∏ᶠ i, f i = ∏ i ∈ s, f i :=
finprod_eq_prod_of_mulSupport_subset _ fun _ hx => h <| hf.mem_toFinset.2 hx
@[to_additive]
theorem finprod_eq_finset_prod_of_mulSupport_subset (f : α → M) {s : Finset α}
(h : mulSupport f ⊆ (s : Set α)) : ∏ᶠ i, f i = ∏ i ∈ s, f i :=
haveI h' : (s.finite_toSet.subset h).toFinset ⊆ s := by
simpa [← Finset.coe_subset, Set.coe_toFinset]
finprod_eq_prod_of_mulSupport_toFinset_subset _ _ h'
@[to_additive]
theorem finprod_def (f : α → M) [Decidable (mulSupport f).Finite] :
∏ᶠ i : α, f i = if h : (mulSupport f).Finite then ∏ i ∈ h.toFinset, f i else 1 := by
split_ifs with h
· exact finprod_eq_prod_of_mulSupport_toFinset_subset _ h (Finset.Subset.refl _)
· rw [finprod, dif_neg]
rw [mulSupport_comp_eq_preimage]
exact mt (fun hf => hf.of_preimage Equiv.plift.surjective) h
@[to_additive]
theorem finprod_of_infinite_mulSupport {f : α → M} (hf : (mulSupport f).Infinite) :
∏ᶠ i, f i = 1 := by classical rw [finprod_def, dif_neg hf]
@[to_additive]
theorem finprod_eq_prod (f : α → M) (hf : (mulSupport f).Finite) :
∏ᶠ i : α, f i = ∏ i ∈ hf.toFinset, f i := by classical rw [finprod_def, dif_pos hf]
@[to_additive]
theorem finprod_eq_prod_of_fintype [Fintype α] (f : α → M) : ∏ᶠ i : α, f i = ∏ i, f i :=
finprod_eq_prod_of_mulSupport_toFinset_subset _ (Set.toFinite _) <| Finset.subset_univ _
@[to_additive]
theorem map_finset_prod {α F : Type*} [Fintype α] [EquivLike F M N] [MulEquivClass F M N] (f : F)
(g : α → M) : f (∏ i : α, g i) = ∏ i : α, f (g i) := by
simp [← finprod_eq_prod_of_fintype, MulEquivClass.map_finprod]
@[to_additive]
theorem finprod_cond_eq_prod_of_cond_iff (f : α → M) {p : α → Prop} {t : Finset α}
(h : ∀ {x}, f x ≠ 1 → (p x ↔ x ∈ t)) : (∏ᶠ (i) (_ : p i), f i) = ∏ i ∈ t, f i := by
set s := { x | p x }
change ∏ᶠ (i : α) (_ : i ∈ s), f i = ∏ i ∈ t, f i
have : mulSupport (s.mulIndicator f) ⊆ t := by
rw [Set.mulSupport_mulIndicator]
intro x hx
exact (h hx.2).1 hx.1
rw [finprod_mem_def, finprod_eq_prod_of_mulSupport_subset _ this]
refine Finset.prod_congr rfl fun x hx => mulIndicator_apply_eq_self.2 fun hxs => ?_
contrapose! hxs
exact (h hxs).2 hx
@[to_additive]
theorem finprod_cond_ne (f : α → M) (a : α) [DecidableEq α] (hf : (mulSupport f).Finite) :
(∏ᶠ (i) (_ : i ≠ a), f i) = ∏ i ∈ hf.toFinset.erase a, f i := by
apply finprod_cond_eq_prod_of_cond_iff
intro x hx
rw [Finset.mem_erase, Finite.mem_toFinset, mem_mulSupport]
grind
@[to_additive]
theorem finprod_mem_eq_prod_of_inter_mulSupport_eq (f : α → M) {s : Set α} {t : Finset α}
(h : s ∩ mulSupport f = ↑t ∩ mulSupport f) : ∏ᶠ i ∈ s, f i = ∏ i ∈ t, f i :=
finprod_cond_eq_prod_of_cond_iff _ <| by
intro x hxf
rw [← mem_mulSupport] at hxf
refine ⟨fun hx => ?_, fun hx => ?_⟩
· refine ((mem_inter_iff x t (mulSupport f)).mp ?_).1
rw [← Set.ext_iff.mp h x, mem_inter_iff]
exact ⟨hx, hxf⟩
· refine ((mem_inter_iff x s (mulSupport f)).mp ?_).1
rw [Set.ext_iff.mp h x, mem_inter_iff]
exact ⟨hx, hxf⟩
@[to_additive]
theorem finprod_mem_eq_prod_of_subset (f : α → M) {s : Set α} {t : Finset α}
(h₁ : s ∩ mulSupport f ⊆ t) (h₂ : ↑t ⊆ s) : ∏ᶠ i ∈ s, f i = ∏ i ∈ t, f i :=
finprod_cond_eq_prod_of_cond_iff _ fun hx => ⟨fun h => h₁ ⟨h, hx⟩, fun h => h₂ h⟩
@[to_additive]
theorem finprod_mem_eq_prod (f : α → M) {s : Set α} (hf : (s ∩ mulSupport f).Finite) :
∏ᶠ i ∈ s, f i = ∏ i ∈ hf.toFinset, f i :=
finprod_mem_eq_prod_of_inter_mulSupport_eq _ <| by simp [inter_assoc]
@[to_additive]
theorem finprod_mem_eq_prod_filter (f : α → M) (s : Set α) [DecidablePred (· ∈ s)]
(hf : (mulSupport f).Finite) :
∏ᶠ i ∈ s, f i = ∏ i ∈ hf.toFinset with i ∈ s, f i :=
finprod_mem_eq_prod_of_inter_mulSupport_eq _ <| by
ext x
simp [and_comm]
@[to_additive]
theorem finprod_mem_eq_toFinset_prod (f : α → M) (s : Set α) [Fintype s] :
∏ᶠ i ∈ s, f i = ∏ i ∈ s.toFinset, f i :=
finprod_mem_eq_prod_of_inter_mulSupport_eq _ <| by simp_rw [coe_toFinset s]
@[to_additive]
theorem finprod_mem_eq_finite_toFinset_prod (f : α → M) {s : Set α} (hs : s.Finite) :
∏ᶠ i ∈ s, f i = ∏ i ∈ hs.toFinset, f i :=
finprod_mem_eq_prod_of_inter_mulSupport_eq _ <| by rw [hs.coe_toFinset]
@[to_additive]
theorem finprod_mem_finset_eq_prod (f : α → M) (s : Finset α) : ∏ᶠ i ∈ s, f i = ∏ i ∈ s, f i :=
finprod_mem_eq_prod_of_inter_mulSupport_eq _ rfl
@[to_additive]
theorem finprod_mem_coe_finset (f : α → M) (s : Finset α) :
(∏ᶠ i ∈ (s : Set α), f i) = ∏ i ∈ s, f i :=
finprod_mem_eq_prod_of_inter_mulSupport_eq _ rfl
@[to_additive]
theorem finprod_mem_eq_one_of_infinite {f : α → M} {s : Set α} (hs : (s ∩ mulSupport f).Infinite) :
∏ᶠ i ∈ s, f i = 1 := by
rw [finprod_mem_def]
apply finprod_of_infinite_mulSupport
rwa [← mulSupport_mulIndicator] at hs
@[to_additive]
theorem finprod_mem_eq_one_of_forall_eq_one {f : α → M} {s : Set α} (h : ∀ x ∈ s, f x = 1) :
∏ᶠ i ∈ s, f i = 1 := by simp +contextual [h]
@[to_additive]
theorem finprod_mem_inter_mulSupport (f : α → M) (s : Set α) :
∏ᶠ i ∈ s ∩ mulSupport f, f i = ∏ᶠ i ∈ s, f i := by
rw [finprod_mem_def, finprod_mem_def, mulIndicator_inter_mulSupport]
@[to_additive]
theorem finprod_mem_inter_mulSupport_eq (f : α → M) (s t : Set α)
(h : s ∩ mulSupport f = t ∩ mulSupport f) : ∏ᶠ i ∈ s, f i = ∏ᶠ i ∈ t, f i := by
rw [← finprod_mem_inter_mulSupport, h, finprod_mem_inter_mulSupport]
@[to_additive]
theorem finprod_mem_inter_mulSupport_eq' (f : α → M) (s t : Set α)
(h : ∀ x ∈ mulSupport f, x ∈ s ↔ x ∈ t) : ∏ᶠ i ∈ s, f i = ∏ᶠ i ∈ t, f i := by
apply finprod_mem_inter_mulSupport_eq
ext x
exact and_congr_left (h x)
@[to_additive]
theorem finprod_mem_univ (f : α → M) : ∏ᶠ i ∈ @Set.univ α, f i = ∏ᶠ i : α, f i :=
finprod_congr fun _ => finprod_true _
variable {f g : α → M} {a b : α} {s t : Set α}
@[to_additive]
theorem finprod_mem_congr (h₀ : s = t) (h₁ : ∀ x ∈ t, f x = g x) :
∏ᶠ i ∈ s, f i = ∏ᶠ i ∈ t, g i :=
h₀.symm ▸ finprod_congr fun i => finprod_congr_Prop rfl (h₁ i)
@[to_additive]
theorem finprod_eq_one_of_forall_eq_one {f : α → M} (h : ∀ x, f x = 1) : ∏ᶠ i, f i = 1 := by
simp +contextual [h]
@[to_additive finsum_pos']
theorem one_lt_finprod' {M : Type*} [CommMonoid M] [PartialOrder M] [IsOrderedCancelMonoid M]
{f : ι → M}
(h : ∀ i, 1 ≤ f i) (h' : ∃ i, 1 < f i) (hf : (mulSupport f).Finite) : 1 < ∏ᶠ i, f i := by
rcases h' with ⟨i, hi⟩
rw [finprod_eq_prod _ hf]
refine Finset.one_lt_prod' (fun i _ ↦ h i) ⟨i, ?_, hi⟩
simpa only [Finite.mem_toFinset, mem_mulSupport] using ne_of_gt hi
/-!
### Distributivity w.r.t. addition, subtraction, and (scalar) multiplication
-/
/-- If the multiplicative supports of `f` and `g` are finite, then the product of `f i * g i` equals
the product of `f i` multiplied by the product of `g i`. -/
@[to_additive
/-- If the additive supports of `f` and `g` are finite, then the sum of `f i + g i`
equals the sum of `f i` plus the sum of `g i`. -/]
theorem finprod_mul_distrib (hf : (mulSupport f).Finite) (hg : (mulSupport g).Finite) :
∏ᶠ i, f i * g i = (∏ᶠ i, f i) * ∏ᶠ i, g i := by
classical
rw [finprod_eq_prod_of_mulSupport_toFinset_subset f hf Finset.subset_union_left,
finprod_eq_prod_of_mulSupport_toFinset_subset g hg Finset.subset_union_right, ←
Finset.prod_mul_distrib]
refine finprod_eq_prod_of_mulSupport_subset _ ?_
simp only [Finset.coe_union, Finite.coe_toFinset, mulSupport_subset_iff,
mem_union, mem_mulSupport]
intro x
contrapose!
rintro ⟨hf, hg⟩
simp [hf, hg]
/-- If the multiplicative supports of `f` and `g` are finite, then the product of `f i / g i`
equals the product of `f i` divided by the product of `g i`. -/
@[to_additive
/-- If the additive supports of `f` and `g` are finite, then the sum of `f i - g i`
equals the sum of `f i` minus the sum of `g i`. -/]
theorem finprod_div_distrib [DivisionCommMonoid G] {f g : α → G} (hf : (mulSupport f).Finite)
(hg : (mulSupport g).Finite) : ∏ᶠ i, f i / g i = (∏ᶠ i, f i) / ∏ᶠ i, g i := by
simp only [div_eq_mul_inv, finprod_mul_distrib hf ((mulSupport_fun_inv g).symm.rec hg),
finprod_inv_distrib]
/-- A more general version of `finprod_mem_mul_distrib` that only requires `s ∩ mulSupport f` and
`s ∩ mulSupport g` rather than `s` to be finite. -/
@[to_additive
/-- A more general version of `finsum_mem_add_distrib` that only requires `s ∩ support f`
and `s ∩ support g` rather than `s` to be finite. -/]
theorem finprod_mem_mul_distrib' (hf : (s ∩ mulSupport f).Finite) (hg : (s ∩ mulSupport g).Finite) :
∏ᶠ i ∈ s, f i * g i = (∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ s, g i := by
rw [← mulSupport_mulIndicator] at hf hg
simp only [finprod_mem_def, mulIndicator_mul, finprod_mul_distrib hf hg]
/-- The product of the constant function `1` over any set equals `1`. -/
@[to_additive /-- The sum of the constant function `0` over any set equals `0`. -/]
theorem finprod_mem_one (s : Set α) : (∏ᶠ i ∈ s, (1 : M)) = 1 := by simp
/-- If a function `f` equals `1` on a set `s`, then the product of `f i` over `i ∈ s` equals `1`. -/
@[to_additive
/-- If a function `f` equals `0` on a set `s`, then the sum of `f i` over `i ∈ s`
equals `0`. -/]
theorem finprod_mem_of_eqOn_one (hf : s.EqOn f 1) : ∏ᶠ i ∈ s, f i = 1 := by
rw [← finprod_mem_one s]
exact finprod_mem_congr rfl hf
/-- If the product of `f i` over `i ∈ s` is not equal to `1`, then there is some `x ∈ s` such that
`f x ≠ 1`. -/
@[to_additive
/-- If the sum of `f i` over `i ∈ s` is not equal to `0`, then there is some `x ∈ s`
such that `f x ≠ 0`. -/]
theorem exists_ne_one_of_finprod_mem_ne_one (h : ∏ᶠ i ∈ s, f i ≠ 1) : ∃ x ∈ s, f x ≠ 1 := by
by_contra! h'
exact h (finprod_mem_of_eqOn_one h')
/-- Given a finite set `s`, the product of `f i * g i` over `i ∈ s` equals the product of `f i`
over `i ∈ s` times the product of `g i` over `i ∈ s`. -/
@[to_additive
/-- Given a finite set `s`, the sum of `f i + g i` over `i ∈ s` equals the sum of `f i`
over `i ∈ s` plus the sum of `g i` over `i ∈ s`. -/]
theorem finprod_mem_mul_distrib (hs : s.Finite) :
∏ᶠ i ∈ s, f i * g i = (∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ s, g i :=
finprod_mem_mul_distrib' (hs.inter_of_left _) (hs.inter_of_left _)
@[to_additive]
theorem MonoidHom.map_finprod {f : α → M} (g : M →* N) (hf : (mulSupport f).Finite) :
g (∏ᶠ i, f i) = ∏ᶠ i, g (f i) :=
g.map_finprod_plift f <| hf.preimage Equiv.plift.injective.injOn
@[to_additive]
theorem finprod_pow (hf : (mulSupport f).Finite) (n : ℕ) : (∏ᶠ i, f i) ^ n = ∏ᶠ i, f i ^ n :=
(powMonoidHom n).map_finprod hf
/-- See also `finsum_smul` for a version that works even when the support of `f` is not finite,
but with slightly stronger typeclass requirements. -/
theorem finsum_smul' {R M : Type*} [Semiring R] [AddCommMonoid M] [Module R M] {f : ι → R}
(hf : (support f).Finite) (x : M) : (∑ᶠ i, f i) • x = ∑ᶠ i, f i • x :=
((smulAddHom R M).flip x).map_finsum hf
/-- See also `smul_finsum` for a version that works even when the support of `f` is not finite,
but with slightly stronger typeclass requirements. -/
theorem smul_finsum' {R M : Type*} [Monoid R] [AddCommMonoid M] [DistribMulAction R M] (c : R)
{f : ι → M} (hf : (support f).Finite) : (c • ∑ᶠ i, f i) = ∑ᶠ i, c • f i :=
(DistribMulAction.toAddMonoidHom M c).map_finsum hf
/-- A more general version of `MonoidHom.map_finprod_mem` that requires `s ∩ mulSupport f` rather
than `s` to be finite. -/
@[to_additive
/-- A more general version of `AddMonoidHom.map_finsum_mem` that requires
`s ∩ support f` rather than `s` to be finite. -/]
theorem MonoidHom.map_finprod_mem' {f : α → M} (g : M →* N) (h₀ : (s ∩ mulSupport f).Finite) :
g (∏ᶠ j ∈ s, f j) = ∏ᶠ i ∈ s, g (f i) := by
rw [g.map_finprod]
· simp only [g.map_finprod_Prop]
· simpa only [finprod_eq_mulIndicator_apply, mulSupport_mulIndicator]
/-- Given a monoid homomorphism `g : M →* N` and a function `f : α → M`, the value of `g` at the
product of `f i` over `i ∈ s` equals the product of `g (f i)` over `s`. -/
@[to_additive
/-- Given an additive monoid homomorphism `g : M →* N` and a function `f : α → M`, the
value of `g` at the sum of `f i` over `i ∈ s` equals the sum of `g (f i)` over `s`. -/]
theorem MonoidHom.map_finprod_mem (f : α → M) (g : M →* N) (hs : s.Finite) :
g (∏ᶠ j ∈ s, f j) = ∏ᶠ i ∈ s, g (f i) :=
g.map_finprod_mem' (hs.inter_of_left _)
@[to_additive]
theorem MulEquiv.map_finprod_mem (g : M ≃* N) (f : α → M) {s : Set α} (hs : s.Finite) :
g (∏ᶠ i ∈ s, f i) = ∏ᶠ i ∈ s, g (f i) :=
g.toMonoidHom.map_finprod_mem f hs
@[to_additive]
theorem finprod_mem_inv_distrib [DivisionCommMonoid G] (f : α → G) (hs : s.Finite) :
(∏ᶠ x ∈ s, (f x)⁻¹) = (∏ᶠ x ∈ s, f x)⁻¹ :=
((MulEquiv.inv G).map_finprod_mem f hs).symm
/-- Given a finite set `s`, the product of `f i / g i` over `i ∈ s` equals the product of `f i`
over `i ∈ s` divided by the product of `g i` over `i ∈ s`. -/
@[to_additive
/-- Given a finite set `s`, the sum of `f i / g i` over `i ∈ s` equals the sum of `f i`
over `i ∈ s` minus the sum of `g i` over `i ∈ s`. -/]
theorem finprod_mem_div_distrib [DivisionCommMonoid G] (f g : α → G) (hs : s.Finite) :
∏ᶠ i ∈ s, f i / g i = (∏ᶠ i ∈ s, f i) / ∏ᶠ i ∈ s, g i := by
simp only [div_eq_mul_inv, finprod_mem_mul_distrib hs, finprod_mem_inv_distrib g hs]
/-!
### `∏ᶠ x ∈ s, f x` and set operations
-/
/-- The product of any function over an empty set is `1`. -/
@[to_additive /-- The sum of any function over an empty set is `0`. -/]
theorem finprod_mem_empty : (∏ᶠ i ∈ (∅ : Set α), f i) = 1 := by simp
/-- A set `s` is nonempty if the product of some function over `s` is not equal to `1`. -/
@[to_additive
/-- A set `s` is nonempty if the sum of some function over `s` is not equal to `0`. -/]
theorem nonempty_of_finprod_mem_ne_one (h : ∏ᶠ i ∈ s, f i ≠ 1) : s.Nonempty :=
nonempty_iff_ne_empty.2 fun h' => h <| h'.symm ▸ finprod_mem_empty
/-- Given finite sets `s` and `t`, the product of `f i` over `i ∈ s ∪ t` times the product of
`f i` over `i ∈ s ∩ t` equals the product of `f i` over `i ∈ s` times the product of `f i`
over `i ∈ t`. -/
@[to_additive
/-- Given finite sets `s` and `t`, the sum of `f i` over `i ∈ s ∪ t` plus the sum of
`f i` over `i ∈ s ∩ t` equals the sum of `f i` over `i ∈ s` plus the sum of `f i`
over `i ∈ t`. -/]
theorem finprod_mem_union_inter (hs : s.Finite) (ht : t.Finite) :
((∏ᶠ i ∈ s ∪ t, f i) * ∏ᶠ i ∈ s ∩ t, f i) = (∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ t, f i := by
lift s to Finset α using hs; lift t to Finset α using ht
classical
rw [← Finset.coe_union, ← Finset.coe_inter]
simp only [finprod_mem_coe_finset, Finset.prod_union_inter]
/-- A more general version of `finprod_mem_union_inter` that requires `s ∩ mulSupport f` and
`t ∩ mulSupport f` rather than `s` and `t` to be finite. -/
@[to_additive
/-- A more general version of `finsum_mem_union_inter` that requires `s ∩ support f` and
`t ∩ support f` rather than `s` and `t` to be finite. -/]
theorem finprod_mem_union_inter' (hs : (s ∩ mulSupport f).Finite) (ht : (t ∩ mulSupport f).Finite) :
((∏ᶠ i ∈ s ∪ t, f i) * ∏ᶠ i ∈ s ∩ t, f i) = (∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ t, f i := by
rw [← finprod_mem_inter_mulSupport f s, ← finprod_mem_inter_mulSupport f t, ←
finprod_mem_union_inter hs ht, ← union_inter_distrib_right, finprod_mem_inter_mulSupport, ←
finprod_mem_inter_mulSupport f (s ∩ t)]
rw [inter_left_comm, inter_assoc, inter_assoc, inter_self, inter_left_comm]
/-- A more general version of `finprod_mem_union` that requires `s ∩ mulSupport f` and
`t ∩ mulSupport f` rather than `s` and `t` to be finite. -/
@[to_additive
/-- A more general version of `finsum_mem_union` that requires `s ∩ support f` and
`t ∩ support f` rather than `s` and `t` to be finite. -/]
theorem finprod_mem_union' (hst : Disjoint s t) (hs : (s ∩ mulSupport f).Finite)
(ht : (t ∩ mulSupport f).Finite) : ∏ᶠ i ∈ s ∪ t, f i = (∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ t, f i := by
rw [← finprod_mem_union_inter' hs ht, disjoint_iff_inter_eq_empty.1 hst, finprod_mem_empty,
mul_one]
/-- Given two finite disjoint sets `s` and `t`, the product of `f i` over `i ∈ s ∪ t` equals the
product of `f i` over `i ∈ s` times the product of `f i` over `i ∈ t`. -/
@[to_additive
/-- Given two finite disjoint sets `s` and `t`, the sum of `f i` over `i ∈ s ∪ t` equals
the sum of `f i` over `i ∈ s` plus the sum of `f i` over `i ∈ t`. -/]
theorem finprod_mem_union (hst : Disjoint s t) (hs : s.Finite) (ht : t.Finite) :
∏ᶠ i ∈ s ∪ t, f i = (∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ t, f i :=
finprod_mem_union' hst (hs.inter_of_left _) (ht.inter_of_left _)
/-- A more general version of `finprod_mem_union'` that requires `s ∩ mulSupport f` and
`t ∩ mulSupport f` rather than `s` and `t` to be disjoint -/
@[to_additive
/-- A more general version of `finsum_mem_union'` that requires `s ∩ support f` and
`t ∩ support f` rather than `s` and `t` to be disjoint -/]
theorem finprod_mem_union'' (hst : Disjoint (s ∩ mulSupport f) (t ∩ mulSupport f))
(hs : (s ∩ mulSupport f).Finite) (ht : (t ∩ mulSupport f).Finite) :
∏ᶠ i ∈ s ∪ t, f i = (∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ t, f i := by
rw [← finprod_mem_inter_mulSupport f s, ← finprod_mem_inter_mulSupport f t, ←
finprod_mem_union hst hs ht, ← union_inter_distrib_right, finprod_mem_inter_mulSupport]
/-- The product of `f i` over `i ∈ {a}` equals `f a`. -/
@[to_additive /-- The sum of `f i` over `i ∈ {a}` equals `f a`. -/]
theorem finprod_mem_singleton : (∏ᶠ i ∈ ({a} : Set α), f i) = f a := by
rw [← Finset.coe_singleton, finprod_mem_coe_finset, Finset.prod_singleton]
@[to_additive (attr := simp)]
theorem finprod_cond_eq_left : (∏ᶠ (i) (_ : i = a), f i) = f a :=
finprod_mem_singleton
@[to_additive (attr := simp)]
theorem finprod_cond_eq_right : (∏ᶠ (i) (_ : a = i), f i) = f a := by simp [@eq_comm _ a]
/-- A more general version of `finprod_mem_insert` that requires `s ∩ mulSupport f` rather than `s`
to be finite. -/
@[to_additive
/-- A more general version of `finsum_mem_insert` that requires `s ∩ support f` rather
than `s` to be finite. -/]
theorem finprod_mem_insert' (f : α → M) (h : a ∉ s) (hs : (s ∩ mulSupport f).Finite) :
∏ᶠ i ∈ insert a s, f i = f a * ∏ᶠ i ∈ s, f i := by
rw [insert_eq, finprod_mem_union' _ _ hs, finprod_mem_singleton]
· rwa [disjoint_singleton_left]
· exact (finite_singleton a).inter_of_left _
/-- Given a finite set `s` and an element `a ∉ s`, the product of `f i` over `i ∈ insert a s` equals
`f a` times the product of `f i` over `i ∈ s`. -/
@[to_additive
/-- Given a finite set `s` and an element `a ∉ s`, the sum of `f i` over `i ∈ insert a s`
equals `f a` plus the sum of `f i` over `i ∈ s`. -/]
theorem finprod_mem_insert (f : α → M) (h : a ∉ s) (hs : s.Finite) :
∏ᶠ i ∈ insert a s, f i = f a * ∏ᶠ i ∈ s, f i :=
finprod_mem_insert' f h <| hs.inter_of_left _
/-- If `f a = 1` when `a ∉ s`, then the product of `f i` over `i ∈ insert a s` equals the product of
`f i` over `i ∈ s`. -/
@[to_additive
/-- If `f a = 0` when `a ∉ s`, then the sum of `f i` over `i ∈ insert a s` equals the sum
of `f i` over `i ∈ s`. -/]
theorem finprod_mem_insert_of_eq_one_if_notMem (h : a ∉ s → f a = 1) :
∏ᶠ i ∈ insert a s, f i = ∏ᶠ i ∈ s, f i := by
refine finprod_mem_inter_mulSupport_eq' _ _ _ fun x hx => ⟨?_, Or.inr⟩
rintro (rfl | hxs)
exacts [not_imp_comm.1 h hx, hxs]
@[deprecated (since := "2025-05-23")]
alias finsum_mem_insert_of_eq_zero_if_not_mem := finsum_mem_insert_of_eq_zero_if_notMem
@[to_additive existing, deprecated (since := "2025-05-23")]
alias finprod_mem_insert_of_eq_one_if_not_mem := finprod_mem_insert_of_eq_one_if_notMem
/-- If `f a = 1`, then the product of `f i` over `i ∈ insert a s` equals the product of `f i` over
`i ∈ s`. -/
@[to_additive
/-- If `f a = 0`, then the sum of `f i` over `i ∈ insert a s` equals the sum of `f i`
over `i ∈ s`. -/]
theorem finprod_mem_insert_one (h : f a = 1) : ∏ᶠ i ∈ insert a s, f i = ∏ᶠ i ∈ s, f i :=
finprod_mem_insert_of_eq_one_if_notMem fun _ => h
/-- If the multiplicative support of `f` is finite, then for every `x` in the domain of `f`, `f x`
divides `finprod f`. -/
theorem finprod_mem_dvd {f : α → N} (a : α) (hf : (mulSupport f).Finite) : f a ∣ finprod f := by
by_cases ha : a ∈ mulSupport f
· rw [finprod_eq_prod_of_mulSupport_toFinset_subset f hf (Set.Subset.refl _)]
exact Finset.dvd_prod_of_mem f ((Finite.mem_toFinset hf).mpr ha)
· rw [notMem_mulSupport.mp ha]
exact one_dvd (finprod f)
/-- The product of `f i` over `i ∈ {a, b}`, `a ≠ b`, is equal to `f a * f b`. -/
@[to_additive /-- The sum of `f i` over `i ∈ {a, b}`, `a ≠ b`, is equal to `f a + f b`. -/]
theorem finprod_mem_pair (h : a ≠ b) : (∏ᶠ i ∈ ({a, b} : Set α), f i) = f a * f b := by
rw [finprod_mem_insert, finprod_mem_singleton]
exacts [h, finite_singleton b]
/-- The product of `f y` over `y ∈ g '' s` equals the product of `f (g i)` over `s`
provided that `g` is injective on `s ∩ mulSupport (f ∘ g)`. -/
@[to_additive
/-- The sum of `f y` over `y ∈ g '' s` equals the sum of `f (g i)` over `s` provided that
`g` is injective on `s ∩ support (f ∘ g)`. -/]
theorem finprod_mem_image' {s : Set β} {g : β → α} (hg : (s ∩ mulSupport (f ∘ g)).InjOn g) :
∏ᶠ i ∈ g '' s, f i = ∏ᶠ j ∈ s, f (g j) := by
classical
by_cases hs : (s ∩ mulSupport (f ∘ g)).Finite
· have hg : ∀ x ∈ hs.toFinset, ∀ y ∈ hs.toFinset, g x = g y → x = y := by
simpa only [hs.mem_toFinset]
have := finprod_mem_eq_prod (comp f g) hs
unfold Function.comp at this
rw [this, ← Finset.prod_image hg]
refine finprod_mem_eq_prod_of_inter_mulSupport_eq f ?_
rw [Finset.coe_image, hs.coe_toFinset, ← image_inter_mulSupport_eq, inter_assoc, inter_self]
· unfold Function.comp at hs
rw [finprod_mem_eq_one_of_infinite hs, finprod_mem_eq_one_of_infinite]
rwa [image_inter_mulSupport_eq, infinite_image_iff hg]
/-- The product of `f y` over `y ∈ g '' s` equals the product of `f (g i)` over `s` provided that
`g` is injective on `s`. -/
@[to_additive
/-- The sum of `f y` over `y ∈ g '' s` equals the sum of `f (g i)` over `s` provided that
`g` is injective on `s`. -/]
theorem finprod_mem_image {s : Set β} {g : β → α} (hg : s.InjOn g) :
∏ᶠ i ∈ g '' s, f i = ∏ᶠ j ∈ s, f (g j) :=
finprod_mem_image' <| hg.mono inter_subset_left
/-- The product of `f y` over `y ∈ Set.range g` equals the product of `f (g i)` over all `i`
provided that `g` is injective on `mulSupport (f ∘ g)`. -/
@[to_additive
/-- The sum of `f y` over `y ∈ Set.range g` equals the sum of `f (g i)` over all `i`
provided that `g` is injective on `support (f ∘ g)`. -/]
theorem finprod_mem_range' {g : β → α} (hg : (mulSupport (f ∘ g)).InjOn g) :
∏ᶠ i ∈ range g, f i = ∏ᶠ j, f (g j) := by
rw [← image_univ, finprod_mem_image', finprod_mem_univ]
rwa [univ_inter]
/-- The product of `f y` over `y ∈ Set.range g` equals the product of `f (g i)` over all `i`
provided that `g` is injective. -/
@[to_additive
/-- The sum of `f y` over `y ∈ Set.range g` equals the sum of `f (g i)` over all `i`
provided that `g` is injective. -/]
theorem finprod_mem_range {g : β → α} (hg : Injective g) : ∏ᶠ i ∈ range g, f i = ∏ᶠ j, f (g j) :=
finprod_mem_range' hg.injOn
/-- See also `Finset.prod_bij`. -/
@[to_additive /-- See also `Finset.sum_bij`. -/]
theorem finprod_mem_eq_of_bijOn {s : Set α} {t : Set β} {f : α → M} {g : β → M} (e : α → β)
(he₀ : s.BijOn e t) (he₁ : ∀ x ∈ s, f x = g (e x)) : ∏ᶠ i ∈ s, f i = ∏ᶠ j ∈ t, g j := by
rw [← Set.BijOn.image_eq he₀, finprod_mem_image he₀.2.1]
exact finprod_mem_congr rfl he₁
/-- See `finprod_comp`, `Fintype.prod_bijective` and `Finset.prod_bij`. -/
@[to_additive /-- See `finsum_comp`, `Fintype.sum_bijective` and `Finset.sum_bij`. -/]
theorem finprod_eq_of_bijective {f : α → M} {g : β → M} (e : α → β) (he₀ : Bijective e)
(he₁ : ∀ x, f x = g (e x)) : ∏ᶠ i, f i = ∏ᶠ j, g j := by
rw [← finprod_mem_univ f, ← finprod_mem_univ g]
exact finprod_mem_eq_of_bijOn _ he₀.bijOn_univ fun x _ => he₁ x
/-- See also `finprod_eq_of_bijective`, `Fintype.prod_bijective` and `Finset.prod_bij`. -/
@[to_additive
/-- See also `finsum_eq_of_bijective`, `Fintype.sum_bijective` and `Finset.sum_bij`. -/]
theorem finprod_comp {g : β → M} (e : α → β) (he₀ : Function.Bijective e) :
(∏ᶠ i, g (e i)) = ∏ᶠ j, g j :=
finprod_eq_of_bijective e he₀ fun _ => rfl
@[to_additive]
theorem finprod_comp_equiv (e : α ≃ β) {f : β → M} : (∏ᶠ i, f (e i)) = ∏ᶠ i', f i' :=
finprod_comp e e.bijective
@[to_additive]
theorem finprod_set_coe_eq_finprod_mem (s : Set α) : ∏ᶠ j : s, f j = ∏ᶠ i ∈ s, f i := by
rw [← finprod_mem_range, Subtype.range_coe]
exact Subtype.coe_injective
@[to_additive]
theorem finprod_subtype_eq_finprod_cond (p : α → Prop) :
∏ᶠ j : Subtype p, f j = ∏ᶠ (i) (_ : p i), f i :=
finprod_set_coe_eq_finprod_mem { i | p i }
@[to_additive]
theorem finprod_mem_inter_mul_diff' (t : Set α) (h : (s ∩ mulSupport f).Finite) :
((∏ᶠ i ∈ s ∩ t, f i) * ∏ᶠ i ∈ s \ t, f i) = ∏ᶠ i ∈ s, f i := by
rw [← finprod_mem_union', inter_union_diff]
· rw [disjoint_iff_inf_le]
exact fun x hx => hx.2.2 hx.1.2
exacts [h.subset fun x hx => ⟨hx.1.1, hx.2⟩, h.subset fun x hx => ⟨hx.1.1, hx.2⟩]
@[to_additive]
theorem finprod_mem_inter_mul_diff (t : Set α) (h : s.Finite) :
((∏ᶠ i ∈ s ∩ t, f i) * ∏ᶠ i ∈ s \ t, f i) = ∏ᶠ i ∈ s, f i :=
finprod_mem_inter_mul_diff' _ <| h.inter_of_left _
/-- A more general version of `finprod_mem_mul_diff` that requires `t ∩ mulSupport f` rather than
`t` to be finite. -/
@[to_additive
/-- A more general version of `finsum_mem_add_diff` that requires `t ∩ support f` rather
than `t` to be finite. -/]
theorem finprod_mem_mul_diff' (hst : s ⊆ t) (ht : (t ∩ mulSupport f).Finite) :
((∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ t \ s, f i) = ∏ᶠ i ∈ t, f i := by
rw [← finprod_mem_inter_mul_diff' _ ht, inter_eq_self_of_subset_right hst]
/-- Given a finite set `t` and a subset `s` of `t`, the product of `f i` over `i ∈ s`
times the product of `f i` over `t \ s` equals the product of `f i` over `i ∈ t`. -/
@[to_additive
/-- Given a finite set `t` and a subset `s` of `t`, the sum of `f i` over `i ∈ s` plus
the sum of `f i` over `t \ s` equals the sum of `f i` over `i ∈ t`. -/]
theorem finprod_mem_mul_diff (hst : s ⊆ t) (ht : t.Finite) :
((∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ t \ s, f i) = ∏ᶠ i ∈ t, f i :=
finprod_mem_mul_diff' hst (ht.inter_of_left _)
/-- Given a family of pairwise disjoint finite sets `t i` indexed by a finite type, the product of
`f a` over the union `⋃ i, t i` is equal to the product over all indexes `i` of the products of
`f a` over `a ∈ t i`. -/
@[to_additive
/-- Given a family of pairwise disjoint finite sets `t i` indexed by a finite type, the
sum of `f a` over the union `⋃ i, t i` is equal to the sum over all indexes `i` of the
sums of `f a` over `a ∈ t i`. -/]
theorem finprod_mem_iUnion [Finite ι] {t : ι → Set α} (h : Pairwise (Disjoint on t))
(ht : ∀ i, (t i).Finite) : ∏ᶠ a ∈ ⋃ i : ι, t i, f a = ∏ᶠ i, ∏ᶠ a ∈ t i, f a := by
cases nonempty_fintype ι
lift t to ι → Finset α using ht
classical
rw [← biUnion_univ, ← Finset.coe_univ, ← Finset.coe_biUnion, finprod_mem_coe_finset,
Finset.prod_biUnion]
· simp only [finprod_mem_coe_finset, finprod_eq_prod_of_fintype]
· exact fun x _ y _ hxy => Finset.disjoint_coe.1 (h hxy)
/-- Given a family of sets `t : ι → Set α`, a finite set `I` in the index type such that all sets
`t i`, `i ∈ I`, are finite, if all `t i`, `i ∈ I`, are pairwise disjoint, then the product of `f a`
over `a ∈ ⋃ i ∈ I, t i` is equal to the product over `i ∈ I` of the products of `f a` over
`a ∈ t i`. -/
@[to_additive
/-- Given a family of sets `t : ι → Set α`, a finite set `I` in the index type such that
all sets `t i`, `i ∈ I`, are finite, if all `t i`, `i ∈ I`, are pairwise disjoint, then the
sum of `f a` over `a ∈ ⋃ i ∈ I, t i` is equal to the sum over `i ∈ I` of the sums of `f a`
over `a ∈ t i`. -/]
theorem finprod_mem_biUnion {I : Set ι} {t : ι → Set α} (h : I.PairwiseDisjoint t) (hI : I.Finite)
(ht : ∀ i ∈ I, (t i).Finite) : ∏ᶠ a ∈ ⋃ x ∈ I, t x, f a = ∏ᶠ i ∈ I, ∏ᶠ j ∈ t i, f j := by
haveI := hI.fintype
rw [biUnion_eq_iUnion, finprod_mem_iUnion, ← finprod_set_coe_eq_finprod_mem]
exacts [fun x y hxy => h x.2 y.2 (Subtype.coe_injective.ne hxy), fun b => ht b b.2]
/-- If `t` is a finite set of pairwise disjoint finite sets, then the product of `f a`
over `a ∈ ⋃₀ t` is the product over `s ∈ t` of the products of `f a` over `a ∈ s`. -/
@[to_additive
/-- If `t` is a finite set of pairwise disjoint finite sets, then the sum of `f a` over
`a ∈ ⋃₀ t` is the sum over `s ∈ t` of the sums of `f a` over `a ∈ s`. -/]
theorem finprod_mem_sUnion {t : Set (Set α)} (h : t.PairwiseDisjoint id) (ht₀ : t.Finite)
(ht₁ : ∀ x ∈ t, Set.Finite x) : ∏ᶠ a ∈ ⋃₀ t, f a = ∏ᶠ s ∈ t, ∏ᶠ a ∈ s, f a := by
rw [Set.sUnion_eq_biUnion]
exact finprod_mem_biUnion h ht₀ ht₁
@[to_additive]
lemma finprod_option {f : Option α → M} (hf : (mulSupport (f ∘ some)).Finite) :
∏ᶠ o, f o = f none * ∏ᶠ a, f (some a) := by
replace hf : (mulSupport f).Finite := by simpa [finite_option]
convert finprod_mem_insert' f (show none ∉ Set.range Option.some by simp)
(hf.subset inter_subset_right)
· simp
· rw [finprod_mem_range]
exact Option.some_injective _
@[to_additive]
lemma finprod_mem_powerset_insert {f : Set α → M} {s : Set α} {a : α} (hs : s.Finite)
(has : a ∉ s) : ∏ᶠ t ∈ 𝒫 insert a s, f t = (∏ᶠ t ∈ 𝒫 s, f t) * ∏ᶠ t ∈ 𝒫 s, f (insert a t) := by
rw [Set.powerset_insert,
finprod_mem_union (disjoint_powerset_insert has) hs.powerset (hs.powerset.image (insert a)),
finprod_mem_image (powerset_insert_injOn has)]
@[to_additive]
lemma finprod_mem_powerset_diff_elem {f : Set α → M} {s : Set α} {a : α} (hs : s.Finite)
(has : a ∈ s) : ∏ᶠ t ∈ 𝒫 s, f t = (∏ᶠ t ∈ 𝒫 (s \ {a}), f t)
* ∏ᶠ t ∈ 𝒫 (s \ {a}), f (insert a t) := by
nth_rw 1 2 [← Set.insert_diff_self_of_mem has] -- second appearance hidden by notation
exact finprod_mem_powerset_insert (hs.subset Set.diff_subset)
(notMem_diff_of_mem (Set.mem_singleton a))
@[to_additive]
theorem mul_finprod_cond_ne (a : α) (hf : (mulSupport f).Finite) :
(f a * ∏ᶠ (i) (_ : i ≠ a), f i) = ∏ᶠ i, f i := by
classical
rw [finprod_eq_prod _ hf]
have h : ∀ x : α, f x ≠ 1 → (x ≠ a ↔ x ∈ hf.toFinset \ {a}) := by
intro x hx
rw [Finset.mem_sdiff, Finset.mem_singleton, Finite.mem_toFinset, mem_mulSupport]
grind
rw [finprod_cond_eq_prod_of_cond_iff f (fun hx => h _ hx), Finset.sdiff_singleton_eq_erase]
by_cases ha : a ∈ mulSupport f
· apply Finset.mul_prod_erase _ _ ((Finite.mem_toFinset _).mpr ha)
· rw [mem_mulSupport, not_not] at ha
rw [ha, one_mul]
apply Finset.prod_erase _ ha
/-- If `s : Set α` and `t : Set β` are finite sets, then taking the product over `s` commutes with
taking the product over `t`. -/
@[to_additive
/-- If `s : Set α` and `t : Set β` are finite sets, then summing over `s` commutes with
summing over `t`. -/]
theorem finprod_mem_comm {s : Set α} {t : Set β} (f : α → β → M) (hs : s.Finite) (ht : t.Finite) :
(∏ᶠ i ∈ s, ∏ᶠ j ∈ t, f i j) = ∏ᶠ j ∈ t, ∏ᶠ i ∈ s, f i j := by
lift s to Finset α using hs; lift t to Finset β using ht
simp only [finprod_mem_coe_finset]
exact Finset.prod_comm
/-- To prove a property of a finite product, it suffices to prove that the property is
multiplicative and holds on factors. -/
@[to_additive
/-- To prove a property of a finite sum, it suffices to prove that the property is
additive and holds on summands. -/]
theorem finprod_mem_induction (p : M → Prop) (hp₀ : p 1) (hp₁ : ∀ x y, p x → p y → p (x * y))
(hp₂ : ∀ x ∈ s, p <| f x) : p (∏ᶠ i ∈ s, f i) :=
finprod_induction _ hp₀ hp₁ fun x => finprod_induction _ hp₀ hp₁ <| hp₂ x
theorem finprod_cond_nonneg {R : Type*} [CommSemiring R] [PartialOrder R] [IsOrderedRing R]
{p : α → Prop} {f : α → R}
(hf : ∀ x, p x → 0 ≤ f x) : 0 ≤ ∏ᶠ (x) (_ : p x), f x :=
finprod_nonneg fun x => finprod_nonneg <| hf x
@[to_additive]
theorem single_le_finprod {M : Type*} [CommMonoid M] [PartialOrder M] [IsOrderedMonoid M]
(i : α) {f : α → M}
(hf : (mulSupport f).Finite) (h : ∀ j, 1 ≤ f j) : f i ≤ ∏ᶠ j, f j := by
classical calc
f i ≤ ∏ j ∈ insert i hf.toFinset, f j :=
Finset.single_le_prod' (fun j _ => h j) (Finset.mem_insert_self _ _)
_ = ∏ᶠ j, f j :=
(finprod_eq_prod_of_mulSupport_toFinset_subset _ hf (Finset.subset_insert _ _)).symm
theorem finprod_eq_zero {M₀ : Type*} [CommMonoidWithZero M₀] (f : α → M₀) (x : α) (hx : f x = 0)
(hf : (mulSupport f).Finite) : ∏ᶠ x, f x = 0 := by
nontriviality
rw [finprod_eq_prod f hf]
refine Finset.prod_eq_zero (hf.mem_toFinset.2 ?_) hx
simp [hx]
@[to_additive]
theorem finprod_prod_comm (s : Finset β) (f : α → β → M)
(h : ∀ b ∈ s, (mulSupport fun a => f a b).Finite) :
(∏ᶠ a : α, ∏ b ∈ s, f a b) = ∏ b ∈ s, ∏ᶠ a : α, f a b := by
have hU :
(mulSupport fun a => ∏ b ∈ s, f a b) ⊆
(s.finite_toSet.biUnion fun b hb => h b (Finset.mem_coe.1 hb)).toFinset := by
rw [Finite.coe_toFinset]
intro x hx
simp only [exists_prop, mem_iUnion, Ne, mem_mulSupport, Finset.mem_coe]
contrapose! hx
rw [mem_mulSupport, not_not, Finset.prod_congr rfl hx, Finset.prod_const_one]
rw [finprod_eq_prod_of_mulSupport_subset _ hU, Finset.prod_comm]
refine Finset.prod_congr rfl fun b hb => (finprod_eq_prod_of_mulSupport_subset _ ?_).symm
intro a ha
simp only [Finite.coe_toFinset, mem_iUnion]
exact ⟨b, hb, ha⟩
@[to_additive]
theorem prod_finprod_comm (s : Finset α) (f : α → β → M) (h : ∀ a ∈ s, (mulSupport (f a)).Finite) :
(∏ a ∈ s, ∏ᶠ b : β, f a b) = ∏ᶠ b : β, ∏ a ∈ s, f a b :=
(finprod_prod_comm s (fun b a => f a b) h).symm
/--
For functions with finite support, multiplication commutes with finsums. See `mul_finsum` for a
statement assuming that `R` has no zero divisors.
-/
theorem mul_finsum' {R : Type*} [NonUnitalNonAssocSemiring R] (f : α → R) (r : R)
(h : (support f).Finite) : (r * ∑ᶠ a : α, f a) = ∑ᶠ a : α, r * f a :=
(AddMonoidHom.mulLeft r).map_finsum h
/--
For finite sets, multiplication commutes with `finsum_mem`. See `mul_finsum_mem'` for a statement
assuming finiteness of support.
-/
theorem mul_finsum_mem' {R : Type*} [NonUnitalNonAssocSemiring R] {s : Set α} (f : α → R) (r : R)
(hs : s.Finite) : (r * ∑ᶠ a ∈ s, f a) = ∑ᶠ a ∈ s, r * f a :=
(AddMonoidHom.mulLeft r).map_finsum_mem f hs
/--
For functions with finite support, multiplication commutes with finsums. See `finsum_mul` for a
statement assuming that `R` has no zero divisors.
-/
theorem finsum_mul' {R : Type*} [NonUnitalNonAssocSemiring R] (f : α → R) (r : R)
(h : (support f).Finite) : (∑ᶠ a : α, f a) * r = ∑ᶠ a : α, f a * r :=
(AddMonoidHom.mulRight r).map_finsum h
/--
For finite sets, multiplication commutes with `finsum_mem`. See `finsum_mem_mul'` for a statement
assuming finiteness of support.
-/
theorem finsum_mem_mul' {R : Type*} [NonUnitalNonAssocSemiring R] {s : Set α} (f : α → R) (r : R)
(hs : s.Finite) : (∑ᶠ a ∈ s, f a) * r = ∑ᶠ a ∈ s, f a * r :=
(AddMonoidHom.mulRight r).map_finsum_mem f hs
open Classical in
/--
If `R` has no zero divisors, then multiplication commutes with finsums. See `mul_finsum'` for a
statement assuming finiteness of support.
-/
theorem mul_finsum {R : Type*} [NonUnitalNonAssocSemiring R] [NoZeroDivisors R] (f : α → R)
(r : R) :
(r * ∑ᶠ a : α, f a) = ∑ᶠ a : α, r * f a := by
by_cases hr : r = 0
· simp_all
by_cases h : f.support.Finite
· exact mul_finsum' f r h
simp [finsum_def, h, (by aesop : (r * f ·).support = f.support)]
/--
If `R` has no zero divisors, then multiplication commutes with `finsum_mem`. See `mul_finsum_mem'`
for a statement assuming finiteness of support.
-/
theorem mul_finsum_mem {R : Type*} [NonUnitalNonAssocSemiring R] [NoZeroDivisors R] {s : Set α}
(f : α → R) (r : R) :
(r * ∑ᶠ a ∈ s, f a) = ∑ᶠ a ∈ s, r * f a := by
rw [mul_finsum]
congr
ext a
by_cases h : a ∈ s <;> simp_all
open Classical in
/--
If `R` has no zero divisors, then multiplication commutes with finsums. See `finsum_mul'` for a
statement assuming finiteness of support.
-/
theorem finsum_mul {R : Type*} [NonUnitalNonAssocSemiring R] [NoZeroDivisors R] (f : α → R)
(r : R) :
(∑ᶠ a : α, f a) * r = ∑ᶠ a : α, f a * r := by
by_cases hr : r = 0
· simp_all
by_cases h : f.support.Finite
· exact finsum_mul' f r h
simp [finsum_def, h, (by aesop : (f · * r).support = f.support)]
/--
If `R` has no zero divisors, then multiplication commutes with `finsum_mem`. See `finsum_mem_mul'`
for a statement assuming finiteness of support.
-/
theorem finsum_mem_mul {R : Type*} [NonUnitalNonAssocSemiring R] [NoZeroDivisors R] {s : Set α}
(f : α → R) (r : R) :
(∑ᶠ a ∈ s, f a) * r = ∑ᶠ a ∈ s, f a * r := by
rw [finsum_mul]
congr
ext a
by_cases h : a ∈ s <;> simp_all
@[to_additive (attr := simp)]
lemma finprod_apply {α ι : Type*} {f : ι → α → N} (hf : (mulSupport f).Finite) (a : α) :
(∏ᶠ i, f i) a = ∏ᶠ i, f i a := by
classical
have hf' : (mulSupport fun i ↦ f i a).Finite := hf.subset (by aesop)
simp only [finprod_def, dif_pos, hf, hf', Finset.prod_apply]
symm
apply Finset.prod_subset <;> aesop
@[to_additive]
theorem Finset.mulSupport_of_fiberwise_prod_subset_image [DecidableEq β] (s : Finset α) (f : α → M)
(g : α → β) : (mulSupport fun b => ∏ a ∈ s with g a = b, f a) ⊆ s.image g := by
simp only [Finset.coe_image]
intro b h
suffices {a ∈ s | g a = b}.Nonempty by
simpa only [fiber_nonempty_iff_mem_image, Finset.mem_image, exists_prop]
exact Finset.nonempty_of_prod_ne_one h
/-- Note that `b ∈ (s.filter (fun ab => Prod.fst ab = a)).image Prod.snd` iff `(a, b) ∈ s` so
we can simplify the right-hand side of this lemma. However the form stated here is more useful for
iterating this lemma, e.g., if we have `f : α × β × γ → M`. -/
@[to_additive
/-- Note that `b ∈ (s.filter (fun ab => Prod.fst ab = a)).image Prod.snd` iff `(a, b) ∈ s` so
we can simplify the right-hand side of this lemma. However the form stated here is more
useful for iterating this lemma, e.g., if we have `f : α × β × γ → M`. -/]
theorem finprod_mem_finset_product' [DecidableEq α] [DecidableEq β] (s : Finset (α × β))
(f : α × β → M) :
(∏ᶠ (ab) (_ : ab ∈ s), f ab) =
∏ᶠ (a) (b) (_ : b ∈ (s.filter fun ab => Prod.fst ab = a).image Prod.snd), f (a, b) := by
have (a : _) :
∏ i ∈ (s.filter fun ab => Prod.fst ab = a).image Prod.snd, f (a, i) =
(s.filter (Prod.fst · = a)).prod f := by
refine Finset.prod_nbij' (fun b ↦ (a, b)) Prod.snd ?_ ?_ ?_ ?_ ?_ <;> aesop
rw [finprod_mem_finset_eq_prod]
simp_rw [finprod_mem_finset_eq_prod, this]
rw [finprod_eq_prod_of_mulSupport_subset _
(s.mulSupport_of_fiberwise_prod_subset_image f Prod.fst),
← Finset.prod_fiberwise_of_maps_to (t := Finset.image Prod.fst s) _ f]
-- `finish` could close the goal here
simp only [Finset.mem_image]
exact fun x hx => ⟨x, hx, rfl⟩
/-- See also `finprod_mem_finset_product'`. -/
@[to_additive /-- See also `finsum_mem_finset_product'`. -/]
theorem finprod_mem_finset_product (s : Finset (α × β)) (f : α × β → M) :
(∏ᶠ (ab) (_ : ab ∈ s), f ab) = ∏ᶠ (a) (b) (_ : (a, b) ∈ s), f (a, b) := by
classical
rw [finprod_mem_finset_product']
simp
@[to_additive]
theorem finprod_mem_finset_product₃ {γ : Type*} (s : Finset (α × β × γ)) (f : α × β × γ → M) :
(∏ᶠ (abc) (_ : abc ∈ s), f abc) = ∏ᶠ (a) (b) (c) (_ : (a, b, c) ∈ s), f (a, b, c) := by
classical
rw [finprod_mem_finset_product']
simp_rw [finprod_mem_finset_product']
simp
@[to_additive]
theorem finprod_curry (f : α × β → M) (hf : (mulSupport f).Finite) :
∏ᶠ ab, f ab = ∏ᶠ (a) (b), f (a, b) := by
have h₁ : ∀ a, ∏ᶠ _ : a ∈ hf.toFinset, f a = f a := by simp
have h₂ : ∏ᶠ a, f a = ∏ᶠ (a) (_ : a ∈ hf.toFinset), f a := by simp
simp_rw [h₂, finprod_mem_finset_product, h₁]
@[to_additive]
theorem finprod_curry₃ {γ : Type*} (f : α × β × γ → M) (h : (mulSupport f).Finite) :
∏ᶠ abc, f abc = ∏ᶠ (a) (b) (c), f (a, b, c) := by
rw [finprod_curry f h]
congr
ext a
rw [finprod_curry]
simp [h]
@[to_additive]
theorem finprod_dmem {s : Set α} [DecidablePred (· ∈ s)] (f : ∀ a : α, a ∈ s → M) :
(∏ᶠ (a : α) (h : a ∈ s), f a h) = ∏ᶠ (a : α) (_ : a ∈ s), if h' : a ∈ s then f a h' else 1 :=
finprod_congr fun _ => finprod_congr fun ha => (dif_pos ha).symm
@[to_additive]
theorem finprod_emb_domain' {f : α → β} (hf : Injective f) [DecidablePred (· ∈ Set.range f)]
(g : α → M) :
(∏ᶠ b : β, if h : b ∈ Set.range f then g (Classical.choose h) else 1) = ∏ᶠ a : α, g a := by
simp_rw [← finprod_eq_dif]
rw [finprod_dmem, finprod_mem_range hf, finprod_congr fun a => _]
intro a
rw [dif_pos (Set.mem_range_self a), hf (Classical.choose_spec (Set.mem_range_self a))]
@[to_additive]
theorem finprod_emb_domain (f : α ↪ β) [DecidablePred (· ∈ Set.range f)] (g : α → M) :
(∏ᶠ b : β, if h : b ∈ Set.range f then g (Classical.choose h) else 1) = ∏ᶠ a : α, g a :=
finprod_emb_domain' f.injective g
@[simp, norm_cast]
lemma Nat.cast_finprod [Finite ι] {R : Type*} [CommSemiring R] (f : ι → ℕ) :
↑(∏ᶠ x, f x : ℕ) = ∏ᶠ x, (f x : R) :=
(Nat.castRingHom R).map_finprod f.mulSupport.toFinite
@[simp, norm_cast]
lemma Nat.cast_finprod_mem {s : Set ι} (hs : s.Finite) {R : Type*} [CommSemiring R] (f : ι → ℕ) :
↑(∏ᶠ x ∈ s, f x : ℕ) = ∏ᶠ x ∈ s, (f x : R) :=
(Nat.castRingHom R).map_finprod_mem _ hs
@[simp, norm_cast]
lemma Nat.cast_finsum [Finite ι] {M : Type*} [AddCommMonoidWithOne M]
(f : ι → ℕ) : ↑(∑ᶠ x, f x : ℕ) = ∑ᶠ x, (f x : M) :=
(Nat.castAddMonoidHom M).map_finsum f.support.toFinite
@[simp, norm_cast]
lemma Nat.cast_finsum_mem {s : Set ι} (hs : s.Finite) {M : Type*}
[AddCommMonoidWithOne M] (f : ι → ℕ) : ↑(∑ᶠ x ∈ s, f x : ℕ) = ∑ᶠ x ∈ s, (f x : M) :=
(Nat.castAddMonoidHom M).map_finsum_mem _ hs
end type |
.lake/packages/mathlib/Mathlib/Algebra/BigOperators/WithTop.lean | import Mathlib.Algebra.Order.Ring.WithTop
import Mathlib.Algebra.BigOperators.Group.Finset.Basic
/-!
# Sums in `WithTop`
This file proves results about finite sums over monoids extended by a bottom or top element.
-/
open Finset
variable {ι M M₀ : Type*}
namespace WithTop
section AddCommMonoid
variable [AddCommMonoid M] {s : Finset ι} {f : ι → WithTop M}
@[simp, norm_cast] lemma coe_sum (s : Finset ι) (f : ι → M) :
∑ i ∈ s, f i = ∑ i ∈ s, (f i : WithTop M) := map_sum addHom f s
/-- A sum is infinite iff one term is infinite. -/
@[simp] lemma sum_eq_top : ∑ i ∈ s, f i = ⊤ ↔ ∃ i ∈ s, f i = ⊤ := by
induction s using Finset.cons_induction <;> simp [*]
/-- A sum is finite iff all terms are finite. -/
lemma sum_ne_top : ∑ i ∈ s, f i ≠ ⊤ ↔ ∀ i ∈ s, f i ≠ ⊤ := by simp
variable [LT M]
/-- A sum is finite iff all terms are finite. -/
@[simp] lemma sum_lt_top : ∑ i ∈ s, f i < ⊤ ↔ ∀ i ∈ s, f i < ⊤ := by
simp [WithTop.lt_top_iff_ne_top]
end AddCommMonoid
section CommMonoidWithZero
variable [CommMonoidWithZero M₀] [NoZeroDivisors M₀] [Nontrivial M₀] [DecidableEq M₀]
{s : Finset ι} {f : ι → WithTop M₀}
/-- A product of finite terms is finite. -/
lemma prod_ne_top (h : ∀ i ∈ s, f i ≠ ⊤) : ∏ i ∈ s, f i ≠ ⊤ :=
prod_induction f (· ≠ ⊤) (fun _ _ ↦ mul_ne_top) coe_ne_top h
/-- A product of finite terms is finite. -/
lemma prod_lt_top [LT M₀] (h : ∀ i ∈ s, f i < ⊤) : ∏ i ∈ s, f i < ⊤ :=
prod_induction f (· < ⊤) (fun _ _ ↦ mul_lt_top) (coe_lt_top _) h
end CommMonoidWithZero
end WithTop
namespace WithBot
section AddCommMonoid
variable [AddCommMonoid M] {s : Finset ι} {f : ι → WithBot M}
@[simp, norm_cast] lemma coe_sum (s : Finset ι) (f : ι → M) :
∑ i ∈ s, f i = ∑ i ∈ s, (f i : WithBot M) := map_sum addHom f s
/-- A sum is infinite iff one term is infinite. -/
lemma sum_eq_bot_iff : ∑ i ∈ s, f i = ⊥ ↔ ∃ i ∈ s, f i = ⊥ := by
induction s using Finset.cons_induction <;> simp [*]
variable [LT M]
/-- A sum is finite iff all terms are finite. -/
lemma bot_lt_sum_iff : ⊥ < ∑ i ∈ s, f i ↔ ∀ i ∈ s, ⊥ < f i := by
simp only [WithBot.bot_lt_iff_ne_bot, ne_eq, sum_eq_bot_iff, not_exists, not_and]
/-- A sum of finite terms is finite. -/
lemma sum_lt_bot (h : ∀ i ∈ s, f i ≠ ⊥) : ⊥ < ∑ i ∈ s, f i :=
bot_lt_sum_iff.2 fun i hi ↦ WithBot.bot_lt_iff_ne_bot.2 (h i hi)
end AddCommMonoid
section CommMonoidWithZero
variable [CommMonoidWithZero M₀] [NoZeroDivisors M₀] [Nontrivial M₀] [DecidableEq M₀]
{s : Finset ι} {f : ι → WithBot M₀}
/-- A product of finite terms is finite. -/
lemma prod_ne_bot (h : ∀ i ∈ s, f i ≠ ⊥) : ∏ i ∈ s, f i ≠ ⊥ :=
prod_induction f (· ≠ ⊥) (fun _ _ ↦ mul_ne_bot) coe_ne_bot h
/-- A product of finite terms is finite. -/
lemma bot_lt_prod [LT M₀] (h : ∀ i ∈ s, ⊥ < f i) : ⊥ < ∏ i ∈ s, f i :=
prod_induction f (⊥ < ·) (fun _ _ ↦ bot_lt_mul) (bot_lt_coe _) h
end CommMonoidWithZero
end WithBot |
.lake/packages/mathlib/Mathlib/Algebra/BigOperators/RingEquiv.lean | import Mathlib.Algebra.Ring.Equiv
import Mathlib.Algebra.Ring.Opposite
import Mathlib.Algebra.BigOperators.Group.Finset.Defs
/-!
# Results about mapping big operators across ring equivalences
-/
namespace RingEquiv
variable {α R S : Type*}
protected theorem map_list_prod [Semiring R] [Semiring S] (f : R ≃+* S) (l : List R) :
f l.prod = (l.map f).prod := map_list_prod f l
protected theorem map_list_sum [NonUnitalNonAssocSemiring R] [NonUnitalNonAssocSemiring S]
(f : R ≃+* S) (l : List R) : f l.sum = (l.map f).sum := map_list_sum f l
/-- An isomorphism into the opposite ring acts on the product by acting on the reversed elements -/
protected theorem unop_map_list_prod [Semiring R] [Semiring S] (f : R ≃+* Sᵐᵒᵖ) (l : List R) :
MulOpposite.unop (f l.prod) = (l.map (MulOpposite.unop ∘ f)).reverse.prod :=
unop_map_list_prod f l
protected theorem map_multiset_prod [CommSemiring R] [CommSemiring S] (f : R ≃+* S)
(s : Multiset R) : f s.prod = (s.map f).prod :=
map_multiset_prod f s
protected theorem map_multiset_sum [NonUnitalNonAssocSemiring R] [NonUnitalNonAssocSemiring S]
(f : R ≃+* S) (s : Multiset R) : f s.sum = (s.map f).sum :=
map_multiset_sum f s
protected theorem map_prod [CommSemiring R] [CommSemiring S] (g : R ≃+* S) (f : α → R)
(s : Finset α) : g (∏ x ∈ s, f x) = ∏ x ∈ s, g (f x) :=
map_prod g f s
protected theorem map_sum [NonUnitalNonAssocSemiring R] [NonUnitalNonAssocSemiring S] (g : R ≃+* S)
(f : α → R) (s : Finset α) : g (∑ x ∈ s, f x) = ∑ x ∈ s, g (f x) :=
map_sum g f s
end RingEquiv |
.lake/packages/mathlib/Mathlib/Algebra/BigOperators/Fin.lean | import Mathlib.Algebra.BigOperators.Ring.Finset
import Mathlib.Algebra.Group.Action.Pi
import Mathlib.Data.Fintype.BigOperators
import Mathlib.Data.Fintype.Fin
import Mathlib.Logic.Equiv.Fin.Basic
/-!
# Big operators and `Fin`
Some results about products and sums over the type `Fin`.
The most important results are the induction formulas `Fin.prod_univ_castSucc`
and `Fin.prod_univ_succ`, and the formula `Fin.prod_const` for the product of a
constant function. These results have variants for sums instead of products.
## Main declarations
* `finFunctionFinEquiv`: An explicit equivalence between `Fin n → Fin m` and `Fin (m ^ n)`.
-/
assert_not_exists Field
open Finset
variable {ι M : Type*}
namespace Finset
@[to_additive]
theorem prod_range [CommMonoid M] {n : ℕ} (f : ℕ → M) :
∏ i ∈ Finset.range n, f i = ∏ i : Fin n, f i :=
(Fin.prod_univ_eq_prod_range _ _).symm
end Finset
namespace Fin
section CommMonoid
variable [CommMonoid M] {n : ℕ}
@[to_additive]
theorem prod_ofFn (f : Fin n → M) : (List.ofFn f).prod = ∏ i, f i := by
simp [prod_eq_multiset_prod]
@[to_additive]
theorem prod_univ_def (f : Fin n → M) : ∏ i, f i = ((List.finRange n).map f).prod := by
rw [← List.ofFn_eq_map, prod_ofFn]
/-- A product of a function `f : Fin 0 → M` is `1` because `Fin 0` is empty -/
@[to_additive /-- A sum of a function `f : Fin 0 → M` is `0` because `Fin 0` is empty -/]
theorem prod_univ_zero (f : Fin 0 → M) : ∏ i, f i = 1 :=
rfl
/-- A product of a function `f : Fin (n + 1) → M` over all `Fin (n + 1)`
is the product of `f x`, for some `x : Fin (n + 1)` times the remaining product -/
@[to_additive /-- A sum of a function `f : Fin (n + 1) → M` over all `Fin (n + 1)` is the sum of
`f x`, for some `x : Fin (n + 1)` plus the remaining sum -/]
theorem prod_univ_succAbove (f : Fin (n + 1) → M) (x : Fin (n + 1)) :
∏ i, f i = f x * ∏ i : Fin n, f (x.succAbove i) := by
rw [univ_succAbove n x, prod_cons, Finset.prod_map, coe_succAboveEmb]
/-- A product of a function `f : Fin (n + 1) → M` over all `Fin (n + 1)`
is the product of `f 0` times the remaining product -/
@[to_additive /-- A sum of a function `f : Fin (n + 1) → M` over all `Fin (n + 1)` is the sum of
`f 0` plus the remaining sum -/]
theorem prod_univ_succ (f : Fin (n + 1) → M) :
∏ i, f i = f 0 * ∏ i : Fin n, f i.succ :=
prod_univ_succAbove f 0
/-- A product of a function `f : Fin (n + 1) → M` over all `Fin (n + 1)`
is the product of `f (Fin.last n)` times the remaining product -/
@[to_additive /-- A sum of a function `f : Fin (n + 1) → M` over all `Fin (n + 1)` is the sum of
`f (Fin.last n)` plus the remaining sum -/]
theorem prod_univ_castSucc (f : Fin (n + 1) → M) :
∏ i, f i = (∏ i : Fin n, f (Fin.castSucc i)) * f (last n) := by
simpa [mul_comm] using prod_univ_succAbove f (last n)
@[to_additive (attr := simp)]
theorem prod_univ_getElem (l : List M) : ∏ i : Fin l.length, l[i.1] = l.prod := by
simp [Finset.prod_eq_multiset_prod]
@[to_additive (attr := simp)]
theorem prod_univ_fun_getElem (l : List ι) (f : ι → M) :
∏ i : Fin l.length, f l[i.1] = (l.map f).prod := by
simp [Finset.prod_eq_multiset_prod]
@[to_additive (attr := simp)]
theorem prod_cons (x : M) (f : Fin n → M) :
(∏ i : Fin n.succ, (cons x f : Fin n.succ → M) i) = x * ∏ i : Fin n, f i := by
simp_rw [prod_univ_succ, cons_zero, cons_succ]
@[to_additive (attr := simp)]
theorem prod_snoc (x : M) (f : Fin n → M) :
(∏ i : Fin n.succ, (snoc f x : Fin n.succ → M) i) = (∏ i : Fin n, f i) * x := by
simp [prod_univ_castSucc]
@[to_additive sum_univ_one]
theorem prod_univ_one (f : Fin 1 → M) : ∏ i, f i = f 0 := by simp
@[to_additive (attr := simp)]
theorem prod_univ_two (f : Fin 2 → M) : ∏ i, f i = f 0 * f 1 := by
simp [prod_univ_succ]
@[to_additive]
theorem prod_univ_two' (f : ι → M) (a b : ι) : ∏ i, f (![a, b] i) = f a * f b :=
prod_univ_two _
@[to_additive]
theorem prod_univ_three (f : Fin 3 → M) : ∏ i, f i = f 0 * f 1 * f 2 := by
rw [prod_univ_castSucc, prod_univ_two]
rfl
@[to_additive]
theorem prod_univ_four (f : Fin 4 → M) : ∏ i, f i = f 0 * f 1 * f 2 * f 3 := by
rw [prod_univ_castSucc, prod_univ_three]
rfl
@[to_additive]
theorem prod_univ_five (f : Fin 5 → M) : ∏ i, f i = f 0 * f 1 * f 2 * f 3 * f 4 := by
rw [prod_univ_castSucc, prod_univ_four]
rfl
@[to_additive]
theorem prod_univ_six (f : Fin 6 → M) : ∏ i, f i = f 0 * f 1 * f 2 * f 3 * f 4 * f 5 := by
rw [prod_univ_castSucc, prod_univ_five]
rfl
@[to_additive]
theorem prod_univ_seven (f : Fin 7 → M) : ∏ i, f i = f 0 * f 1 * f 2 * f 3 * f 4 * f 5 * f 6 := by
rw [prod_univ_castSucc, prod_univ_six]
rfl
@[to_additive]
theorem prod_univ_eight (f : Fin 8 → M) :
∏ i, f i = f 0 * f 1 * f 2 * f 3 * f 4 * f 5 * f 6 * f 7 := by
rw [prod_univ_castSucc, prod_univ_seven]
rfl
@[to_additive]
theorem prod_const (n : ℕ) (x : M) : ∏ _i : Fin n, x = x ^ n := by simp
@[to_additive]
theorem prod_congr' {a b : ℕ} (f : Fin b → M) (h : a = b) :
(∏ i : Fin a, f (i.cast h)) = ∏ i : Fin b, f i := by
subst h
congr
@[to_additive]
theorem prod_univ_add {a b : ℕ} (f : Fin (a + b) → M) :
(∏ i : Fin (a + b), f i) = (∏ i : Fin a, f (castAdd b i)) * ∏ i : Fin b, f (natAdd a i) := by
rw [Fintype.prod_equiv finSumFinEquiv.symm f fun i => f (finSumFinEquiv.toFun i)]
· apply Fintype.prod_sum_type
· intro x
simp only [Equiv.toFun_as_coe, Equiv.apply_symm_apply]
@[to_additive]
theorem prod_trunc {a b : ℕ} (f : Fin (a + b) → M) (hf : ∀ j : Fin b, f (natAdd a j) = 1) :
(∏ i : Fin (a + b), f i) = ∏ i : Fin a, f (castAdd b i) := by
rw [prod_univ_add, Fintype.prod_eq_one _ hf, mul_one]
/-!
### Products over intervals: `Fin.cast`
-/
section cast
variable {m : ℕ}
@[to_additive]
theorem prod_Icc_cast (h : n = m) (f : Fin m → M) (a b : Fin n) :
∏ i ∈ Icc (a.cast h) (b.cast h), f i = ∏ i ∈ Icc a b, f (i.cast h) := by
simp [← map_finCongr_Icc]
@[to_additive]
theorem prod_Ico_cast (h : n = m) (f : Fin m → M) (a b : Fin n) :
∏ i ∈ Ico (a.cast h) (b.cast h), f i = ∏ i ∈ Ico a b, f (i.cast h) := by
simp [← map_finCongr_Ico]
@[to_additive]
theorem prod_Ioc_cast (h : n = m) (f : Fin m → M) (a b : Fin n) :
∏ i ∈ Ioc (a.cast h) (b.cast h), f i = ∏ i ∈ Ioc a b, f (i.cast h) := by
simp [← map_finCongr_Ioc]
@[to_additive]
theorem prod_Ioo_cast (h : n = m) (f : Fin m → M) (a b : Fin n) :
∏ i ∈ Ioo (a.cast h) (b.cast h), f i = ∏ i ∈ Ioo a b, f (i.cast h) := by
simp [← map_finCongr_Ioo]
@[to_additive]
theorem prod_uIcc_cast (h : n = m) (f : Fin m → M) (a b : Fin n) :
∏ i ∈ uIcc (a.cast h) (b.cast h), f i = ∏ i ∈ uIcc a b, f (i.cast h) := by
simp [← map_finCongr_uIcc]
@[to_additive]
theorem prod_Ici_cast (h : n = m) (f : Fin m → M) (a : Fin n) :
∏ i ≥ a.cast h, f i = ∏ i ≥ a, f (i.cast h) := by
simp [← map_finCongr_Ici]
@[to_additive]
theorem prod_Ioi_cast (h : n = m) (f : Fin m → M) (a : Fin n) :
∏ i > a.cast h, f i = ∏ i > a, f (i.cast h) := by
simp [← map_finCongr_Ioi]
@[to_additive]
theorem prod_Iic_cast (h : n = m) (f : Fin m → M) (a : Fin n) :
∏ i ≤ a.cast h, f i = ∏ i ≤ a, f (i.cast h) := by
simp [← map_finCongr_Iic]
@[to_additive]
theorem prod_Iio_cast (h : n = m) (f : Fin m → M) (a : Fin n) :
∏ i < a.cast h, f i = ∏ i < a, f (i.cast h) := by
simp [← map_finCongr_Iio]
end cast
/-!
### Products over intervals: `Fin.castLE`
-/
section castLE
variable {m : ℕ}
@[to_additive]
theorem prod_Icc_castLE (h : n ≤ m) (f : Fin m → M) (a b : Fin n) :
∏ i ∈ Icc (a.castLE h) (b.castLE h), f i = ∏ i ∈ Icc a b, f (i.castLE h) := by
simp [← map_castLEEmb_Icc]
@[to_additive]
theorem prod_Ico_castLE (h : n ≤ m) (f : Fin m → M) (a b : Fin n) :
∏ i ∈ Ico (a.castLE h) (b.castLE h), f i = ∏ i ∈ Ico a b, f (i.castLE h) := by
simp [← map_castLEEmb_Ico]
@[to_additive]
theorem prod_Ioc_castLE (h : n ≤ m) (f : Fin m → M) (a b : Fin n) :
∏ i ∈ Ioc (a.castLE h) (b.castLE h), f i = ∏ i ∈ Ioc a b, f (i.castLE h) := by
simp [← map_castLEEmb_Ioc]
@[to_additive]
theorem prod_Ioo_castLE (h : n ≤ m) (f : Fin m → M) (a b : Fin n) :
∏ i ∈ Ioo (a.castLE h) (b.castLE h), f i = ∏ i ∈ Ioo a b, f (i.castLE h) := by
simp [← map_castLEEmb_Ioo]
@[to_additive]
theorem prod_uIcc_castLE (h : n ≤ m) (f : Fin m → M) (a b : Fin n) :
∏ i ∈ uIcc (a.castLE h) (b.castLE h), f i = ∏ i ∈ uIcc a b, f (i.castLE h) := by
simp [← map_castLEEmb_uIcc]
@[to_additive]
theorem prod_Iic_castLE (h : n ≤ m) (f : Fin m → M) (a : Fin n) :
∏ i ≤ a.castLE h, f i = ∏ i ≤ a, f (i.castLE h) := by
simp [← map_castLEEmb_Iic]
@[to_additive]
theorem prod_Iio_castLE (h : n ≤ m) (f : Fin m → M) (a : Fin n) :
∏ i < a.castLE h, f i = ∏ i < a, f (i.castLE h) := by
simp [← map_castLEEmb_Iio]
end castLE
/-!
### Products over intervals: `Fin.castAdd`
-/
section castAdd
@[to_additive]
theorem prod_Icc_castAdd (m : ℕ) (f : Fin (n + m) → M) (a b : Fin n) :
∏ i ∈ Icc (a.castAdd m) (b.castAdd m), f i = ∏ i ∈ Icc a b, f (i.castAdd m) := by
simp [← map_castAddEmb_Icc]
@[to_additive]
theorem prod_Ico_castAdd (m : ℕ) (f : Fin (n + m) → M) (a b : Fin n) :
∏ i ∈ Ico (a.castAdd m) (b.castAdd m), f i = ∏ i ∈ Ico a b, f (i.castAdd m) := by
simp [← map_castAddEmb_Ico]
@[to_additive]
theorem prod_Ioc_castAdd (m : ℕ) (f : Fin (n + m) → M) (a b : Fin n) :
∏ i ∈ Ioc (a.castAdd m) (b.castAdd m), f i = ∏ i ∈ Ioc a b, f (i.castAdd m) := by
simp [← map_castAddEmb_Ioc]
@[to_additive]
theorem prod_Ioo_castAdd (m : ℕ) (f : Fin (n + m) → M) (a b : Fin n) :
∏ i ∈ Ioo (a.castAdd m) (b.castAdd m), f i = ∏ i ∈ Ioo a b, f (i.castAdd m) := by
simp [← map_castAddEmb_Ioo]
@[to_additive]
theorem prod_uIcc_castAdd (m : ℕ) (f : Fin (n + m) → M) (a b : Fin n) :
∏ i ∈ uIcc (a.castAdd m) (b.castAdd m), f i = ∏ i ∈ uIcc a b, f (i.castAdd m) := by
simp [← map_castAddEmb_uIcc]
@[to_additive]
theorem prod_Iic_castAdd (m : ℕ) (f : Fin (n + m) → M) (a : Fin n) :
∏ i ≤ a.castAdd m, f i = ∏ i ≤ a, f (i.castAdd m) := by
simp [← map_castAddEmb_Iic]
@[to_additive]
theorem prod_Iio_castAdd (m : ℕ) (f : Fin (n + m) → M) (a : Fin n) :
∏ i < a.castAdd m, f i = ∏ i < a, f (i.castAdd m) := by
simp [← map_castAddEmb_Iio]
end castAdd
/-!
### Products over intervals: `Fin.castSucc`
-/
section castSucc
@[to_additive]
theorem prod_Icc_castSucc (f : Fin (n + 1) → M) (a b : Fin n) :
∏ i ∈ Icc a.castSucc b.castSucc, f i = ∏ i ∈ Icc a b, f i.castSucc := by
simp [← map_castSuccEmb_Icc]
@[to_additive]
theorem prod_Ico_castSucc (f : Fin (n + 1) → M) (a b : Fin n) :
∏ i ∈ Ico a.castSucc b.castSucc, f i = ∏ i ∈ Ico a b, f i.castSucc := by
simp [← map_castSuccEmb_Ico]
@[to_additive]
theorem prod_Ioc_castSucc (f : Fin (n + 1) → M) (a b : Fin n) :
∏ i ∈ Ioc a.castSucc b.castSucc, f i = ∏ i ∈ Ioc a b, f i.castSucc := by
simp [← map_castSuccEmb_Ioc]
@[to_additive]
theorem prod_Ioo_castSucc (f : Fin (n + 1) → M) (a b : Fin n) :
∏ i ∈ Ioo a.castSucc b.castSucc, f i = ∏ i ∈ Ioo a b, f i.castSucc := by
simp [← map_castSuccEmb_Ioo]
@[to_additive]
theorem prod_uIcc_castSucc (f : Fin (n + 1) → M) (a b : Fin n) :
∏ i ∈ uIcc a.castSucc b.castSucc, f i = ∏ i ∈ uIcc a b, f i.castSucc := by
simp [← map_castSuccEmb_uIcc]
@[to_additive]
theorem prod_Iic_castSucc (f : Fin (n + 1) → M) (a : Fin n) :
∏ i ≤ a.castSucc, f i = ∏ i ≤ a, f i.castSucc := by
simp [← map_castSuccEmb_Iic]
@[to_additive]
theorem prod_Iio_castSucc (f : Fin (n + 1) → M) (a : Fin n) :
∏ i < a.castSucc, f i = ∏ i < a, f i.castSucc := by
simp [← map_castSuccEmb_Iio]
end castSucc
/-!
### Products over intervals: `Fin.succ`
-/
section succ
@[to_additive]
theorem prod_Icc_succ (f : Fin (n + 1) → M) (a b : Fin n) :
∏ i ∈ Icc a.succ b.succ, f i = ∏ i ∈ Icc a b, f i.succ := by
simp [← map_succEmb_Icc]
@[to_additive]
theorem prod_Ico_succ (f : Fin (n + 1) → M) (a b : Fin n) :
∏ i ∈ Ico a.succ b.succ, f i = ∏ i ∈ Ico a b, f i.succ := by
simp [← map_succEmb_Ico]
@[to_additive]
theorem prod_Ioc_succ (f : Fin (n + 1) → M) (a b : Fin n) :
∏ i ∈ Ioc a.succ b.succ, f i = ∏ i ∈ Ioc a b, f i.succ := by
simp [← map_succEmb_Ioc]
@[to_additive]
theorem prod_Ioo_succ (f : Fin (n + 1) → M) (a b : Fin n) :
∏ i ∈ Ioo a.succ b.succ, f i = ∏ i ∈ Ioo a b, f i.succ := by
simp [← map_succEmb_Ioo]
@[to_additive]
theorem prod_uIcc_succ (f : Fin (n + 1) → M) (a b : Fin n) :
∏ i ∈ uIcc a.succ b.succ, f i = ∏ i ∈ uIcc a b, f i.succ := by
simp [← map_succEmb_uIcc]
@[to_additive]
theorem prod_Ici_succ (f : Fin (n + 1) → M) (a : Fin n) :
∏ i ≥ a.succ, f i = ∏ i ≥ a, f i.succ := by
simp [← map_succEmb_Ici]
@[to_additive (attr := simp)]
theorem prod_Ioi_succ (f : Fin (n + 1) → M) (a : Fin n) :
∏ i > a.succ, f i = ∏ i > a, f i.succ := by
simp [← map_succEmb_Ioi]
@[to_additive]
theorem prod_Ioi_zero (f : Fin (n + 1) → M) :
∏ i > 0, f i = ∏ j : Fin n, f j.succ := by
simp [Ioi_zero_eq_map]
end succ
/-- The product of `g i j` over `i j : Fin (n + 1)`, `i ≠ j`,
is equal to the product of `g i j * g j i` over `i < j`.
The additive version of this lemma is useful for some proofs about differential forms.
In this application, the function has the signature `f : Fin (n + 1) → Fin n → M`,
where `f i j` means `g i (Fin.succAbove i j)` in the informal statements.
Similarly, the product of `g i j * g j i` over `i < j`
is written as `f (Fin.castSucc i) j * f (Fin.succ j) i` over `i j : Fin n`, `j ≥ i`.
-/
@[to_additive /-- The sum of `g i j` over `i j : Fin (n + 1)`, `i ≠ j`,
is equal to the sum of `g i j + g j i` over `i < j`.
This lemma is useful for some proofs about differential forms.
In this application, the function has the signature `f : Fin (n + 1) → Fin n → M`,
where `f i j` means `g i (Fin.succAbove i j)` in the informal statements.
Similarly, the sum of `g i j + g j i` over `i < j`
is written as `f (Fin.castSucc i) j + f (Fin.succ j) i` over `i j : Fin n`, `j ≥ i`.
-/]
theorem prod_prod_eq_prod_triangle_mul (f : Fin (n + 1) → Fin n → M) :
∏ i, ∏ j, f i j = ∏ i : Fin n, ∏ j ≥ i, (f i.castSucc j * f j.succ i) := calc
_ = (∏ i, ∏ j with i ≤ j.castSucc, f i j) * ∏ i, ∏ j with j.castSucc < i, f i j := by
simp only [← Finset.prod_mul_distrib, ← not_le, Finset.prod_filter_mul_prod_filter_not]
_ = (∏ i, ∏ j ≥ i, f i.castSucc j) * ∏ i, ∏ j ≤ i, f i.succ j := by
rw [Fin.prod_univ_castSucc, Fin.prod_univ_succ]
simp [Finset.filter_le_eq_Ici, Finset.filter_ge_eq_Iic]
_ = (∏ i, ∏ j ≥ i, f i.castSucc j) * ∏ i, ∏ j ≥ i, f j.succ i := by
congr 1
apply Finset.prod_comm'
simp
_ = ∏ i : Fin n, ∏ j ≥ i, (f i.castSucc j * f j.succ i) := by
simp only [Finset.prod_mul_distrib]
end CommMonoid
theorem sum_pow_mul_eq_add_pow {n : ℕ} {R : Type*} [CommSemiring R] (a b : R) :
(∑ s : Finset (Fin n), a ^ s.card * b ^ (n - s.card)) = (a + b) ^ n := by
simpa using Fintype.sum_pow_mul_eq_add_pow (Fin n) a b
lemma sum_neg_one_pow (R : Type*) [Ring R] (m : ℕ) :
(∑ n : Fin m, (-1) ^ n.1 : R) = if Even m then 0 else 1 := by
induction m with
| zero => simp
| succ n IH =>
simp only [Fin.sum_univ_castSucc, Fin.coe_castSucc, IH, Fin.val_last, Nat.even_add_one, ite_not]
split_ifs with h
· simp [*]
· simp [(Nat.not_even_iff_odd.mp h).neg_pow]
section PartialProd
variable [Monoid M] {n : ℕ}
/-- For `f = (a₁, ..., aₙ)` in `αⁿ`, `partialProd f` is `(1, a₁, a₁a₂, ..., a₁...aₙ)` in `αⁿ⁺¹`. -/
@[to_additive /-- For `f = (a₁, ..., aₙ)` in `αⁿ`, `partialSum f` is
`(0, a₁, a₁ + a₂, ..., a₁ + ... + aₙ)` in `αⁿ⁺¹`. -/]
def partialProd (f : Fin n → M) (i : Fin (n + 1)) : M :=
((List.ofFn f).take i).prod
@[to_additive (attr := simp)]
theorem partialProd_zero (f : Fin n → M) : partialProd f 0 = 1 := by simp [partialProd]
@[to_additive]
theorem partialProd_succ (f : Fin n → M) (j : Fin n) :
partialProd f j.succ = partialProd f (Fin.castSucc j) * f j := by
simp [partialProd, List.take_succ]
@[to_additive]
theorem partialProd_succ' (f : Fin (n + 1) → M) (j : Fin (n + 1)) :
partialProd f j.succ = f 0 * partialProd (Fin.tail f) j := by
simp [partialProd]
rfl
@[to_additive]
lemma partialProd_init {f : Fin (n + 1) → M} (i : Fin (n + 1)) :
partialProd (init f) i = partialProd f i.castSucc :=
i.inductionOn (by simp) fun i hi => by simp_all [init, partialProd_succ]
@[to_additive]
theorem partialProd_left_inv {G : Type*} [Group G] (f : Fin (n + 1) → G) :
(f 0 • partialProd fun i : Fin n => (f i.castSucc)⁻¹ * f i.succ) = f :=
funext fun x => Fin.inductionOn x (by simp) fun x hx => by
simp only [Pi.smul_apply, smul_eq_mul] at hx ⊢
rw [partialProd_succ, ← mul_assoc, hx, mul_inv_cancel_left]
@[to_additive]
theorem partialProd_right_inv {G : Type*} [Group G] (f : Fin n → G) (i : Fin n) :
(partialProd f (Fin.castSucc i))⁻¹ * partialProd f i.succ = f i := by
rw [partialProd_succ, inv_mul_cancel_left]
@[to_additive]
lemma partialProd_contractNth {G : Type*} [Monoid G] {n : ℕ}
(g : Fin (n + 1) → G) (a : Fin (n + 1)) :
partialProd (contractNth a (· * ·) g) = partialProd g ∘ a.succ.succAbove := by
ext i
refine inductionOn i ?_ ?_
· simp only [partialProd_zero, Function.comp_apply, succ_succAbove_zero]
· intro i hi
simp only [Function.comp_apply, succ_succAbove_succ] at *
rw [partialProd_succ, partialProd_succ, hi]
rcases lt_trichotomy (i : ℕ) a with (h | h | h)
· rw [succAbove_of_castSucc_lt, contractNth_apply_of_lt _ _ _ _ h,
succAbove_of_castSucc_lt] <;>
simp only [lt_def, coe_castSucc, val_succ] <;>
omega
· rw [succAbove_of_castSucc_lt, contractNth_apply_of_eq _ _ _ _ h,
succAbove_of_le_castSucc, castSucc_fin_succ, partialProd_succ, mul_assoc] <;>
simp only [castSucc_lt_succ_iff, le_def, coe_castSucc] <;>
omega
· rw [succAbove_of_le_castSucc, succAbove_of_le_castSucc, contractNth_apply_of_gt _ _ _ _ h,
castSucc_fin_succ] <;>
simp only [le_def, val_succ, coe_castSucc] <;>
omega
/-- Let `(g₀, g₁, ..., gₙ)` be a tuple of elements in `Gⁿ⁺¹`.
Then if `k < j`, this says `(g₀g₁...gₖ₋₁)⁻¹ * g₀g₁...gₖ = gₖ`.
If `k = j`, it says `(g₀g₁...gₖ₋₁)⁻¹ * g₀g₁...gₖ₊₁ = gₖgₖ₊₁`.
If `k > j`, it says `(g₀g₁...gₖ)⁻¹ * g₀g₁...gₖ₊₁ = gₖ₊₁.`
Useful for defining group cohomology. -/
@[to_additive
/-- Let `(g₀, g₁, ..., gₙ)` be a tuple of elements in `Gⁿ⁺¹`.
Then if `k < j`, this says `-(g₀ + g₁ + ... + gₖ₋₁) + (g₀ + g₁ + ... + gₖ) = gₖ`.
If `k = j`, it says `-(g₀ + g₁ + ... + gₖ₋₁) + (g₀ + g₁ + ... + gₖ₊₁) = gₖ + gₖ₊₁`.
If `k > j`, it says `-(g₀ + g₁ + ... + gₖ) + (g₀ + g₁ + ... + gₖ₊₁) = gₖ₊₁.`
Useful for defining group cohomology. -/]
theorem inv_partialProd_mul_eq_contractNth {G : Type*} [Group G] (g : Fin (n + 1) → G)
(j : Fin (n + 1)) (k : Fin n) :
(partialProd g (j.succ.succAbove (Fin.castSucc k)))⁻¹ * partialProd g (j.succAbove k).succ =
j.contractNth (· * ·) g k := by
rcases lt_trichotomy (k : ℕ) j with (h | h | h)
· rwa [succAbove_of_castSucc_lt, succAbove_of_castSucc_lt, partialProd_right_inv,
contractNth_apply_of_lt]
· assumption
· rw [castSucc_lt_iff_succ_le, succ_le_succ_iff, le_iff_val_le_val]
exact le_of_lt h
· rwa [succAbove_of_castSucc_lt, succAbove_of_le_castSucc, partialProd_succ,
castSucc_fin_succ, ← mul_assoc,
partialProd_right_inv, contractNth_apply_of_eq]
· simp [le_iff_val_le_val, ← h]
· rw [castSucc_lt_iff_succ_le, succ_le_succ_iff, le_iff_val_le_val]
exact le_of_eq h
· rwa [succAbove_of_le_castSucc, succAbove_of_le_castSucc, partialProd_succ, partialProd_succ,
castSucc_fin_succ, partialProd_succ, inv_mul_cancel_left, contractNth_apply_of_gt]
· exact le_iff_val_le_val.2 (le_of_lt h)
· rw [le_iff_val_le_val, val_succ]
exact Nat.succ_le_of_lt h
end PartialProd
end Fin
/-- Equivalence between `Fin n → Fin m` and `Fin (m ^ n)`. -/
@[simps!]
def finFunctionFinEquiv {m n : ℕ} : (Fin n → Fin m) ≃ Fin (m ^ n) :=
Equiv.ofRightInverseOfCardLE (le_of_eq <| by simp_rw [Fintype.card_fun, Fintype.card_fin])
(fun f => ⟨∑ i, f i * m ^ (i : ℕ), by
induction n with
| zero => simp
| succ n ih =>
cases m
· exact isEmptyElim (f <| Fin.last _)
simp_rw [Fin.sum_univ_castSucc, Fin.coe_castSucc, Fin.val_last]
refine (Nat.add_lt_add_of_lt_of_le (ih _) <| Nat.mul_le_mul_right _
(Fin.is_le _)).trans_eq ?_
rw [← one_add_mul (_ : ℕ), add_comm, pow_succ']⟩)
(fun a b => ⟨a / m ^ (b : ℕ) % m, by
rcases n with - | n
· exact b.elim0
rcases m with - | m
· rw [zero_pow n.succ_ne_zero] at a
exact a.elim0
· exact Nat.mod_lt _ m.succ_pos⟩)
fun a => by
dsimp
induction n with
| zero => subsingleton [(finCongr <| pow_zero _).subsingleton]
| succ n ih =>
simp_rw [Fin.forall_iff, Fin.ext_iff] at ih
ext
simp_rw [Fin.sum_univ_succ, Fin.val_zero, Fin.val_succ, pow_zero, Nat.div_one,
mul_one, pow_succ', ← Nat.div_div_eq_div_mul, mul_left_comm _ m, ← mul_sum]
rw [ih _ (Nat.div_lt_of_lt_mul (a.is_lt.trans_eq (pow_succ' _ _))), Nat.mod_add_div]
theorem finFunctionFinEquiv_apply {m n : ℕ} (f : Fin n → Fin m) :
(finFunctionFinEquiv f : ℕ) = ∑ i : Fin n, ↑(f i) * m ^ (i : ℕ) :=
rfl
theorem finFunctionFinEquiv_single {m n : ℕ} [NeZero m] (i : Fin n) (j : Fin m) :
(finFunctionFinEquiv (Pi.single i j) : ℕ) = j * m ^ (i : ℕ) := by
rw [finFunctionFinEquiv_apply, Fintype.sum_eq_single i, Pi.single_eq_same]
rintro x hx
rw [Pi.single_eq_of_ne hx, Fin.val_zero, zero_mul]
/-- Equivalence between `∀ i : Fin m, Fin (n i)` and `Fin (∏ i : Fin m, n i)`. -/
def finPiFinEquiv {m : ℕ} {n : Fin m → ℕ} : (∀ i : Fin m, Fin (n i)) ≃ Fin (∏ i : Fin m, n i) :=
Equiv.ofRightInverseOfCardLE (le_of_eq <| by simp_rw [Fintype.card_pi, Fintype.card_fin])
(fun f => ⟨∑ i, f i * ∏ j, n (Fin.castLE i.is_lt.le j), by
induction m with
| zero => simp
| succ m ih =>
rw [Fin.prod_univ_castSucc, Fin.sum_univ_castSucc]
suffices
∀ (n : Fin m → ℕ) (nn : ℕ) (f : ∀ i : Fin m, Fin (n i)) (fn : Fin nn),
((∑ i : Fin m, ↑(f i) * ∏ j : Fin i, n (Fin.castLE i.prop.le j)) + ↑fn * ∏ j, n j) <
(∏ i : Fin m, n i) * nn by
solve_by_elim
intro n nn f fn
cases nn
· exact isEmptyElim fn
refine (Nat.add_lt_add_of_lt_of_le (ih _) <| Nat.mul_le_mul_right _ (Fin.is_le _)).trans_eq ?_
rw [← one_add_mul (_ : ℕ), mul_comm, add_comm]⟩)
(fun a b => ⟨(a / ∏ j : Fin b, n (Fin.castLE b.is_lt.le j)) % n b, by
cases m
· exact b.elim0
rcases h : n b with nb | nb
· rw [prod_eq_zero (Finset.mem_univ _) h] at a
exact isEmptyElim a
exact Nat.mod_lt _ nb.succ_pos⟩)
(by
intro a; revert a; dsimp only [Fin.val_mk]
refine Fin.consInduction ?_ ?_ n
· intro a
have : Subsingleton (Fin (∏ i : Fin 0, i.elim0)) :=
(finCongr <| prod_empty).subsingleton
subsingleton
· intro n x xs ih a
simp_rw [Fin.forall_iff, Fin.ext_iff] at ih
ext
simp_rw [Fin.sum_univ_succ, Fin.cons_succ]
have := fun i : Fin n =>
Fintype.prod_equiv (finCongr <| Fin.val_succ i)
(fun j => (Fin.cons x xs : _ → ℕ) (Fin.castLE (Fin.is_lt _).le j))
(fun j => (Fin.cons x xs : _ → ℕ) (Fin.castLE (Nat.succ_le_succ (Fin.is_lt _).le) j))
fun j => rfl
simp_rw [this]
clear this
simp_rw [Fin.val_zero, Fintype.prod_empty, Nat.div_one, mul_one, Fin.cons_zero,
Fin.prod_univ_succ, Fin.castLE_zero, Fin.cons_zero, ← Nat.div_div_eq_div_mul,
mul_left_comm (_ % _ : ℕ), ← mul_sum]
convert Nat.mod_add_div _ _
exact ih (a / x) (Nat.div_lt_of_lt_mul <| a.is_lt.trans_eq (Fin.prod_univ_succ _)))
theorem finPiFinEquiv_apply {m : ℕ} {n : Fin m → ℕ} (f : ∀ i : Fin m, Fin (n i)) :
(finPiFinEquiv f : ℕ) = ∑ i, f i * ∏ j, n (Fin.castLE i.is_lt.le j) := rfl
theorem finPiFinEquiv_single {m : ℕ} {n : Fin m → ℕ} [∀ i, NeZero (n i)] (i : Fin m)
(j : Fin (n i)) :
(finPiFinEquiv (Pi.single i j : ∀ i : Fin m, Fin (n i)) : ℕ) =
j * ∏ j, n (Fin.castLE i.is_lt.le j) := by
rw [finPiFinEquiv_apply, Fintype.sum_eq_single i, Pi.single_eq_same]
rintro x hx
rw [Pi.single_eq_of_ne hx, Fin.val_zero, zero_mul]
/-- Equivalence between the Sigma type `(i : Fin m) × Fin (n i)` and `Fin (∑ i : Fin m, n i)`. -/
def finSigmaFinEquiv {m : ℕ} {n : Fin m → ℕ} : (i : Fin m) × Fin (n i) ≃ Fin (∑ i : Fin m, n i) :=
match m with
| 0 => @Equiv.equivOfIsEmpty _ _ _ (by simpa using Fin.isEmpty')
| Nat.succ m =>
calc _ ≃ _ := (@finSumFinEquiv m 1).sigmaCongrLeft.symm
_ ≃ _ := Equiv.sumSigmaDistrib _
_ ≃ _ := finSigmaFinEquiv.sumCongr (Equiv.uniqueSigma _)
_ ≃ _ := finSumFinEquiv
_ ≃ _ := finCongr (Fin.sum_univ_castSucc n).symm
@[simp]
theorem finSigmaFinEquiv_apply {m : ℕ} {n : Fin m → ℕ} (k : (i : Fin m) × Fin (n i)) :
(finSigmaFinEquiv k : ℕ) = ∑ i : Fin k.1, n (Fin.castLE k.1.2.le i) + k.2 := by
induction m with
| zero => exact k.fst.elim0
| succ m ih =>
rcases k with ⟨⟨iv, hi⟩, j⟩
rw [finSigmaFinEquiv]
unfold finSumFinEquiv
simp only [Equiv.coe_fn_mk, Equiv.sigmaCongrLeft, Equiv.coe_fn_symm_mk, Equiv.trans_def,
Equiv.trans_apply, finCongr_apply, Fin.coe_cast]
by_cases him : iv < m
· conv in Sigma.mk _ _ =>
equals ⟨Sum.inl ⟨iv, him⟩, j⟩ => simp [Fin.addCases, him]
simpa using ih _
· replace him := Nat.eq_of_lt_succ_of_not_lt hi him
subst him
conv in Sigma.mk _ _ =>
equals ⟨Sum.inr 0, j⟩ => simp [Fin.addCases, Fin.natAdd]
simp
rfl
/-- `finSigmaFinEquiv` on `Fin 1 × f` is just `f` -/
theorem finSigmaFinEquiv_one {n : Fin 1 → ℕ} (ij : (i : Fin 1) × Fin (n i)) :
(finSigmaFinEquiv ij : ℕ) = ij.2 := by
rw [finSigmaFinEquiv_apply, add_eq_right]
apply @Finset.sum_of_isEmpty _ _ _ _ (by simpa using Fin.isEmpty')
namespace List
section CommMonoid
variable [CommMonoid M]
@[to_additive]
theorem prod_take_ofFn {n : ℕ} (f : Fin n → M) (i : ℕ) :
((ofFn f).take i).prod = ∏ j with j.val < i, f j := by
induction i with
| zero =>
simp
| succ i IH =>
by_cases h : i < n
· have : i < length (ofFn f) := by rwa [length_ofFn]
rw [prod_take_succ _ _ this]
have A : ({j | j.val < i + 1} : Finset (Fin n)) =
insert ⟨i, h⟩ ({j | Fin.val j < i} : Finset (Fin n)) := by
ext ⟨_, _⟩
simp [Nat.lt_succ_iff_lt_or_eq, or_comm]
rw [A, prod_insert (by simp), IH, mul_comm]
simp
· have A : (ofFn f).take i = (ofFn f).take i.succ := by
rw [← length_ofFn (f := f)] at h
have : length (ofFn f) ≤ i := not_lt.mp h
rw [take_of_length_le this, take_of_length_le (le_trans this (Nat.le_succ _))]
have B : ∀ j : Fin n, ((j : ℕ) < i.succ) = ((j : ℕ) < i) := by
intro j
have : (j : ℕ) < i := lt_of_lt_of_le j.2 (not_lt.mp h)
simp [this, lt_trans this (Nat.lt_succ_self _)]
simp [← A, B, IH]
@[to_additive]
theorem prod_ofFn {n : ℕ} {f : Fin n → M} : (ofFn f).prod = ∏ i, f i :=
Fin.prod_ofFn f
end CommMonoid
@[to_additive]
theorem alternatingProd_eq_finset_prod {G : Type*} [DivisionCommMonoid G] :
∀ (L : List G), alternatingProd L = ∏ i : Fin L.length, L[i] ^ (-1 : ℤ) ^ (i : ℕ)
| [] => by
rw [alternatingProd, Finset.prod_eq_one]
rintro ⟨i, ⟨⟩⟩
| g::[] => by
change g = ∏ i : Fin 1, [g][i] ^ (-1 : ℤ) ^ (i : ℕ)
rw [Fin.prod_univ_succ]; simp
| g::h::L =>
calc g * h⁻¹ * L.alternatingProd
= g * h⁻¹ * ∏ i : Fin L.length, L[i] ^ (-1 : ℤ) ^ (i : ℕ) :=
congr_arg _ (alternatingProd_eq_finset_prod _)
_ = ∏ i : Fin (L.length + 2), (g::h::L)[i] ^ (-1 : ℤ) ^ (i : ℕ) := by
{ rw [Fin.prod_univ_succ, Fin.prod_univ_succ, mul_assoc]
simp [pow_add]}
end List |
.lake/packages/mathlib/Mathlib/Algebra/BigOperators/Option.lean | import Mathlib.Data.Finset.Option
import Mathlib.Algebra.BigOperators.Group.Finset.Basic
/-!
# Lemmas about products and sums over finite sets in `Option α`
In this file we prove formulas for products and sums over `Finset.insertNone s` and
`Finset.eraseNone s`.
-/
open Function
namespace Finset
variable {α M : Type*} [CommMonoid M]
@[to_additive (attr := simp)]
theorem prod_insertNone (f : Option α → M) (s : Finset α) :
∏ x ∈ insertNone s, f x = f none * ∏ x ∈ s, f (some x) := by simp [insertNone]
@[to_additive]
theorem mul_prod_eq_prod_insertNone (f : α → M) (x : M) (s : Finset α) :
x * ∏ i ∈ s, f i = ∏ i ∈ insertNone s, i.elim x f :=
(prod_insertNone (fun i => i.elim x f) _).symm
@[to_additive]
theorem prod_eraseNone (f : α → M) (s : Finset (Option α)) :
∏ x ∈ eraseNone s, f x = ∏ x ∈ s, Option.elim' 1 f x := by
classical calc
∏ x ∈ eraseNone s, f x = ∏ x ∈ (eraseNone s).map Embedding.some, Option.elim' 1 f x :=
(prod_map (eraseNone s) Embedding.some <| Option.elim' 1 f).symm
_ = ∏ x ∈ s.erase none, Option.elim' 1 f x := by rw [map_some_eraseNone]
_ = ∏ x ∈ s, Option.elim' 1 f x := prod_erase _ rfl
end Finset |
.lake/packages/mathlib/Mathlib/Algebra/BigOperators/Expect.lean | import Mathlib.Algebra.Algebra.Rat
import Mathlib.Algebra.BigOperators.GroupWithZero.Action
import Mathlib.Algebra.BigOperators.Pi
import Mathlib.Algebra.BigOperators.Ring.Finset
import Mathlib.Algebra.Module.Pi
import Mathlib.Data.Finset.Density
import Mathlib.Data.Fintype.BigOperators
import Mathlib.Algebra.Group.Pointwise.Finset.Basic
/-!
# Average over a finset
This file defines `Finset.expect`, the average (aka expectation) of a function over a finset.
## Notation
* `𝔼 i ∈ s, f i` is notation for `Finset.expect s f`. It is the expectation of `f i` where `i`
ranges over the finite set `s` (either a `Finset` or a `Set` with a `Fintype` instance).
* `𝔼 i, f i` is notation for `Finset.expect Finset.univ f`. It is the expectation of `f i` where `i`
ranges over the finite domain of `f`.
* `𝔼 i ∈ s with p i, f i` is notation for `Finset.expect (Finset.filter p s) f`. This is referred to
as `expectWith` in lemma names.
* `𝔼 (i ∈ s) (j ∈ t), f i j` is notation for `Finset.expect (s ×ˢ t) (fun ⟨i, j⟩ ↦ f i j)`.
## Implementation notes
This definition is a special case of the general convex combination operator in a convex space.
However:
1. We don't yet have general convex spaces.
2. The uniform weights case is an overwhelmingly useful special case which should have its own API.
When convex spaces are finally defined, we should redefine `Finset.expect` in terms of that convex
combination operator.
## TODO
* Connect `Finset.expect` with the expectation over `s` in the probability theory sense.
* Give a formulation of Jensen's inequality in this language.
-/
open Finset Function
open Fintype (card)
open scoped Pointwise
variable {ι κ M N : Type*}
local notation a " /ℚ " q => (q : ℚ≥0)⁻¹ • a
/-- Average of a function over a finset. If the finset is empty, this is equal to zero. -/
def Finset.expect [AddCommMonoid M] [Module ℚ≥0 M] (s : Finset ι) (f : ι → M) : M :=
(#s : ℚ≥0)⁻¹ • ∑ i ∈ s, f i
namespace BigOperators
open Batteries.ExtendedBinder Lean Meta
/--
* `𝔼 i ∈ s, f i` is notation for `Finset.expect s f`. It is the expectation of `f i` where `i`
ranges over the finite set `s` (either a `Finset` or a `Set` with a `Fintype` instance).
* `𝔼 i, f i` is notation for `Finset.expect Finset.univ f`. It is the expectation of `f i` where `i`
ranges over the finite domain of `f`.
* `𝔼 i ∈ s with p i, f i` is notation for `Finset.expect (Finset.filter p s) f`.
* `𝔼 (i ∈ s) (j ∈ t), f i j` is notation for `Finset.expect (s ×ˢ t) (fun ⟨i, j⟩ ↦ f i j)`.
These support destructuring, for example `𝔼 ⟨i, j⟩ ∈ s ×ˢ t, f i j`.
Notation: `"𝔼" bigOpBinders* ("with" term)? "," term` -/
scoped syntax (name := bigexpect) "𝔼 " bigOpBinders ("with " term)? ", " term:67 : term
scoped macro_rules (kind := bigexpect)
| `(𝔼 $bs:bigOpBinders $[with $p?]?, $v) => do
let processed ← processBigOpBinders bs
let i ← bigOpBindersPattern processed
let s ← bigOpBindersProd processed
match p? with
| some p => `(Finset.expect (Finset.filter (fun $i ↦ $p) $s) (fun $i ↦ $v))
| none => `(Finset.expect $s (fun $i ↦ $v))
open Lean Meta Parser.Term PrettyPrinter.Delaborator SubExpr
open Batteries.ExtendedBinder
/-- Delaborator for `Finset.expect`. The `pp.funBinderTypes` option controls whether
to show the domain type when the expect is over `Finset.univ`. -/
@[scoped app_delab Finset.expect] def delabFinsetExpect : Delab :=
whenPPOption getPPNotation <| withOverApp 6 <| do
let #[_, _, _, _, s, f] := (← getExpr).getAppArgs | failure
guard <| f.isLambda
let ppDomain ← getPPOption getPPFunBinderTypes
let (i, body) ← withAppArg <| withBindingBodyUnusedName fun i => do
return (i, ← delab)
if s.isAppOfArity ``Finset.univ 2 then
let binder ←
if ppDomain then
let ty ← withNaryArg 0 delab
`(bigOpBinder| $(.mk i):ident : $ty)
else
`(bigOpBinder| $(.mk i):ident)
`(𝔼 $binder:bigOpBinder, $body)
else
let ss ← withNaryArg 4 <| delab
`(𝔼 $(.mk i):ident ∈ $ss, $body)
end BigOperators
open scoped BigOperators
namespace Finset
section AddCommMonoid
variable [AddCommMonoid M] [Module ℚ≥0 M] [AddCommMonoid N] [Module ℚ≥0 N] {s t : Finset ι}
{f g : ι → M} {p q : ι → Prop} [DecidablePred p] [DecidablePred q]
lemma expect_univ [Fintype ι] : 𝔼 i, f i = (∑ i, f i) /ℚ Fintype.card ι := by
rw [expect, card_univ]
@[simp] lemma expect_empty (f : ι → M) : 𝔼 i ∈ ∅, f i = 0 := by simp [expect]
@[simp] lemma expect_singleton (f : ι → M) (i : ι) : 𝔼 j ∈ {i}, f j = f i := by simp [expect]
@[simp] lemma expect_const_zero (s : Finset ι) : 𝔼 _i ∈ s, (0 : M) = 0 := by simp [expect]
@[congr]
lemma expect_congr {t : Finset ι} (hst : s = t) (h : ∀ i ∈ t, f i = g i) :
𝔼 i ∈ s, f i = 𝔼 i ∈ t, g i := by rw [expect, expect, sum_congr hst h, hst]
lemma expectWith_congr (hst : s = t) (hpq : ∀ i ∈ t, p i ↔ q i) (h : ∀ i ∈ t, q i → f i = g i) :
𝔼 i ∈ s with p i, f i = 𝔼 i ∈ t with q i, g i :=
expect_congr (by rw [hst, filter_inj'.2 hpq]) <| by simpa using h
lemma expect_sum_comm (s : Finset ι) (t : Finset κ) (f : ι → κ → M) :
𝔼 i ∈ s, ∑ j ∈ t, f i j = ∑ j ∈ t, 𝔼 i ∈ s, f i j := by
simpa only [expect, smul_sum] using sum_comm
lemma expect_comm (s : Finset ι) (t : Finset κ) (f : ι → κ → M) :
𝔼 i ∈ s, 𝔼 j ∈ t, f i j = 𝔼 j ∈ t, 𝔼 i ∈ s, f i j := by
rw [expect, expect, ← expect_sum_comm, ← expect_sum_comm, expect, expect, smul_comm, sum_comm]
lemma expect_eq_zero (h : ∀ i ∈ s, f i = 0) : 𝔼 i ∈ s, f i = 0 :=
(expect_congr rfl h).trans s.expect_const_zero
lemma exists_ne_zero_of_expect_ne_zero (h : 𝔼 i ∈ s, f i ≠ 0) : ∃ i ∈ s, f i ≠ 0 := by
contrapose! h; exact expect_eq_zero h
lemma expect_add_distrib (s : Finset ι) (f g : ι → M) :
𝔼 i ∈ s, (f i + g i) = 𝔼 i ∈ s, f i + 𝔼 i ∈ s, g i := by
simp [expect, sum_add_distrib]
lemma expect_add_expect_comm (f₁ f₂ g₁ g₂ : ι → M) :
𝔼 i ∈ s, (f₁ i + f₂ i) + 𝔼 i ∈ s, (g₁ i + g₂ i) =
𝔼 i ∈ s, (f₁ i + g₁ i) + 𝔼 i ∈ s, (f₂ i + g₂ i) := by
simp_rw [expect_add_distrib, add_add_add_comm]
lemma expect_eq_single_of_mem (i : ι) (hi : i ∈ s) (h : ∀ j ∈ s, j ≠ i → f j = 0) :
𝔼 i ∈ s, f i = f i /ℚ #s := by rw [expect, sum_eq_single_of_mem _ hi h]
lemma expect_ite_zero (s : Finset ι) (p : ι → Prop) [DecidablePred p]
(h : ∀ i ∈ s, ∀ j ∈ s, p i → p j → i = j) (a : M) :
𝔼 i ∈ s, ite (p i) a 0 = ite (∃ i ∈ s, p i) (a /ℚ #s) 0 := by
split_ifs <;> simp [expect, sum_ite_zero _ _ h, *]
section DecidableEq
variable [DecidableEq ι]
lemma expect_ite_mem (s t : Finset ι) (f : ι → M) :
𝔼 i ∈ s, (if i ∈ t then f i else 0) = (#(s ∩ t) / #s : ℚ≥0) • 𝔼 i ∈ s ∩ t, f i := by
obtain hst | hst := (s ∩ t).eq_empty_or_nonempty
· simp [expect, hst]
· simp [expect, smul_smul, ← inv_mul_eq_div, hst.card_ne_zero]
@[simp] lemma expect_dite_eq (i : ι) (f : ∀ j, i = j → M) :
𝔼 j ∈ s, (if h : i = j then f j h else 0) = if i ∈ s then f i rfl /ℚ #s else 0 := by
split_ifs <;> simp [expect, *]
@[simp] lemma expect_dite_eq' (i : ι) (f : ∀ j, j = i → M) :
𝔼 j ∈ s, (if h : j = i then f j h else 0) = if i ∈ s then f i rfl /ℚ #s else 0 := by
split_ifs <;> simp [expect, *]
@[simp] lemma expect_ite_eq (i : ι) (f : ι → M) :
𝔼 j ∈ s, (if i = j then f j else 0) = if i ∈ s then f i /ℚ #s else 0 := by
split_ifs <;> simp [expect, *]
@[simp] lemma expect_ite_eq' (i : ι) (f : ι → M) :
𝔼 j ∈ s, (if j = i then f j else 0) = if i ∈ s then f i /ℚ #s else 0 := by
split_ifs <;> simp [expect, *]
end DecidableEq
section bij
variable {t : Finset κ} {g : κ → M}
/-- Reorder an average.
The difference with `Finset.expect_bij'` is that the bijection is specified as a surjective
injection, rather than by an inverse function.
The difference with `Finset.expect_nbij` is that the bijection is allowed to use membership of the
domain of the average, rather than being a non-dependent function. -/
lemma expect_bij (i : ∀ a ∈ s, κ) (hi : ∀ a ha, i a ha ∈ t) (h : ∀ a ha, f a = g (i a ha))
(i_inj : ∀ a₁ ha₁ a₂ ha₂, i a₁ ha₁ = i a₂ ha₂ → a₁ = a₂)
(i_surj : ∀ b ∈ t, ∃ a ha, i a ha = b) : 𝔼 i ∈ s, f i = 𝔼 i ∈ t, g i := by
simp_rw [expect, card_bij i hi i_inj i_surj, sum_bij i hi i_inj i_surj h]
/-- Reorder an average.
The difference with `Finset.expect_bij` is that the bijection is specified with an inverse, rather
than as a surjective injection.
The difference with `Finset.expect_nbij'` is that the bijection and its inverse are allowed to use
membership of the domains of the averages, rather than being non-dependent functions. -/
lemma expect_bij' (i : ∀ a ∈ s, κ) (j : ∀ a ∈ t, ι) (hi : ∀ a ha, i a ha ∈ t)
(hj : ∀ a ha, j a ha ∈ s) (left_inv : ∀ a ha, j (i a ha) (hi a ha) = a)
(right_inv : ∀ a ha, i (j a ha) (hj a ha) = a) (h : ∀ a ha, f a = g (i a ha)) :
𝔼 i ∈ s, f i = 𝔼 i ∈ t, g i := by
simp_rw [expect, card_bij' i j hi hj left_inv right_inv, sum_bij' i j hi hj left_inv right_inv h]
/-- Reorder an average.
The difference with `Finset.expect_nbij'` is that the bijection is specified as a surjective
injection, rather than by an inverse function.
The difference with `Finset.expect_bij` is that the bijection is a non-dependent function, rather
than being allowed to use membership of the domain of the average. -/
lemma expect_nbij (i : ι → κ) (hi : ∀ a ∈ s, i a ∈ t) (h : ∀ a ∈ s, f a = g (i a))
(i_inj : (s : Set ι).InjOn i) (i_surj : (s : Set ι).SurjOn i t) :
𝔼 i ∈ s, f i = 𝔼 i ∈ t, g i := by
simp_rw [expect, card_nbij i hi i_inj i_surj, sum_nbij i hi i_inj i_surj h]
/-- Reorder an average.
The difference with `Finset.expect_nbij` is that the bijection is specified with an inverse, rather
than as a surjective injection.
The difference with `Finset.expect_bij'` is that the bijection and its inverse are non-dependent
functions, rather than being allowed to use membership of the domains of the averages.
The difference with `Finset.expect_equiv` is that bijectivity is only required to hold on the
domains of the averages, rather than on the entire types. -/
lemma expect_nbij' (i : ι → κ) (j : κ → ι) (hi : ∀ a ∈ s, i a ∈ t) (hj : ∀ a ∈ t, j a ∈ s)
(left_inv : ∀ a ∈ s, j (i a) = a) (right_inv : ∀ a ∈ t, i (j a) = a)
(h : ∀ a ∈ s, f a = g (i a)) : 𝔼 i ∈ s, f i = 𝔼 i ∈ t, g i := by
simp_rw [expect, card_nbij' i j hi hj left_inv right_inv,
sum_nbij' i j hi hj left_inv right_inv h]
/-- `Finset.expect_equiv` is a specialization of `Finset.expect_bij` that automatically fills in
most arguments. -/
lemma expect_equiv (e : ι ≃ κ) (hst : ∀ i, i ∈ s ↔ e i ∈ t) (hfg : ∀ i ∈ s, f i = g (e i)) :
𝔼 i ∈ s, f i = 𝔼 i ∈ t, g i := by simp_rw [expect, card_equiv e hst, sum_equiv e hst hfg]
/-- Expectation over a product set equals the expectation of the fiberwise expectations.
For rewriting in the reverse direction, use `Finset.expect_product'`. -/
lemma expect_product (s : Finset ι) (t : Finset κ) (f : ι × κ → M) :
𝔼 x ∈ s ×ˢ t, f x = 𝔼 i ∈ s, 𝔼 j ∈ t, f (i, j) := by
simp only [expect, card_product, sum_product, smul_sum, mul_inv, mul_smul, Nat.cast_mul]
/-- Expectation over a product set equals the expectation of the fiberwise expectations.
For rewriting in the reverse direction, use `Finset.expect_product`. -/
lemma expect_product' (s : Finset ι) (t : Finset κ) (f : ι → κ → M) :
𝔼 i ∈ s ×ˢ t, f i.1 i.2 = 𝔼 i ∈ s, 𝔼 j ∈ t, f i j := by
simp only [expect, card_product, sum_product', smul_sum, mul_inv, mul_smul, Nat.cast_mul]
@[simp]
lemma expect_image [DecidableEq ι] {m : κ → ι} (hm : (t : Set κ).InjOn m) :
𝔼 i ∈ t.image m, f i = 𝔼 i ∈ t, f (m i) := by
simp_rw [expect, card_image_of_injOn hm, sum_image hm]
end bij
@[simp] lemma expect_inv_index [DecidableEq ι] [InvolutiveInv ι] (s : Finset ι) (f : ι → M) :
𝔼 i ∈ s⁻¹, f i = 𝔼 i ∈ s, f i⁻¹ := expect_image inv_injective.injOn
@[simp] lemma expect_neg_index [DecidableEq ι] [InvolutiveNeg ι] (s : Finset ι) (f : ι → M) :
𝔼 i ∈ -s, f i = 𝔼 i ∈ s, f (-i) := expect_image neg_injective.injOn
lemma _root_.map_expect {F : Type*} [FunLike F M N] [LinearMapClass F ℚ≥0 M N]
(g : F) (f : ι → M) (s : Finset ι) :
g (𝔼 i ∈ s, f i) = 𝔼 i ∈ s, g (f i) := by simp only [expect, map_smul, map_sum]
@[simp]
lemma card_smul_expect (s : Finset ι) (f : ι → M) : #s • 𝔼 i ∈ s, f i = ∑ i ∈ s, f i := by
obtain rfl | hs := s.eq_empty_or_nonempty
· simp
· rw [expect, ← Nat.cast_smul_eq_nsmul ℚ≥0, smul_inv_smul₀]
exact mod_cast hs.card_ne_zero
@[simp] lemma _root_.Fintype.card_smul_expect [Fintype ι] (f : ι → M) :
Fintype.card ι • 𝔼 i, f i = ∑ i, f i := Finset.card_smul_expect _ _
@[simp] lemma expect_const (hs : s.Nonempty) (a : M) : 𝔼 _i ∈ s, a = a := by
rw [expect, sum_const, ← Nat.cast_smul_eq_nsmul ℚ≥0, inv_smul_smul₀]
exact mod_cast hs.card_ne_zero
lemma smul_expect {G : Type*} [DistribSMul G M] [SMulCommClass G ℚ≥0 M] (a : G)
(s : Finset ι) (f : ι → M) : a • 𝔼 i ∈ s, f i = 𝔼 i ∈ s, a • f i := by
simp only [expect, smul_sum, smul_comm]
end AddCommMonoid
section AddCommGroup
variable [AddCommGroup M] [Module ℚ≥0 M]
lemma expect_sub_distrib (s : Finset ι) (f g : ι → M) :
𝔼 i ∈ s, (f i - g i) = 𝔼 i ∈ s, f i - 𝔼 i ∈ s, g i := by
simp only [expect, sum_sub_distrib, smul_sub]
@[simp]
lemma expect_neg_distrib (s : Finset ι) (f : ι → M) : 𝔼 i ∈ s, -f i = -𝔼 i ∈ s, f i := by
simp [expect]
end AddCommGroup
section Semiring
variable [Semiring M] [Module ℚ≥0 M]
@[simp] lemma card_mul_expect (s : Finset ι) (f : ι → M) :
#s * 𝔼 i ∈ s, f i = ∑ i ∈ s, f i := by rw [← nsmul_eq_mul, card_smul_expect]
@[simp] lemma _root_.Fintype.card_mul_expect [Fintype ι] (f : ι → M) :
Fintype.card ι * 𝔼 i, f i = ∑ i, f i := Finset.card_mul_expect _ _
lemma expect_mul [IsScalarTower ℚ≥0 M M] (s : Finset ι) (f : ι → M) (a : M) :
(𝔼 i ∈ s, f i) * a = 𝔼 i ∈ s, f i * a := by rw [expect, expect, smul_mul_assoc, sum_mul]
lemma mul_expect [SMulCommClass ℚ≥0 M M] (s : Finset ι) (f : ι → M) (a : M) :
a * 𝔼 i ∈ s, f i = 𝔼 i ∈ s, a * f i := by rw [expect, expect, mul_smul_comm, mul_sum]
lemma expect_mul_expect [IsScalarTower ℚ≥0 M M] [SMulCommClass ℚ≥0 M M] (s : Finset ι)
(t : Finset κ) (f : ι → M) (g : κ → M) :
(𝔼 i ∈ s, f i) * 𝔼 j ∈ t, g j = 𝔼 i ∈ s, 𝔼 j ∈ t, f i * g j := by
simp_rw [expect_mul, mul_expect]
end Semiring
section CommSemiring
variable [CommSemiring M] [Module ℚ≥0 M] [IsScalarTower ℚ≥0 M M] [SMulCommClass ℚ≥0 M M]
lemma expect_pow (s : Finset ι) (f : ι → M) (n : ℕ) :
(𝔼 i ∈ s, f i) ^ n = 𝔼 p ∈ Fintype.piFinset fun _ : Fin n ↦ s, ∏ i, f (p i) := by
classical
rw [expect, smul_pow, sum_pow', expect, Fintype.card_piFinset_const, inv_pow, Nat.cast_pow]
end CommSemiring
section Semifield
variable [Semifield M] [CharZero M]
lemma expect_boole_mul [Fintype ι] [Nonempty ι] [DecidableEq ι] (f : ι → M) (i : ι) :
𝔼 j, ite (i = j) (Fintype.card ι : M) 0 * f j = f i := by
simp_rw [expect_univ, ite_mul, zero_mul, sum_ite_eq, if_pos (mem_univ _)]
rw [← @NNRat.cast_natCast M, ← NNRat.smul_def, inv_smul_smul₀]
simp [Fintype.card_ne_zero]
lemma expect_boole_mul' [Fintype ι] [Nonempty ι] [DecidableEq ι] (f : ι → M) (i : ι) :
𝔼 j, ite (j = i) (Fintype.card ι : M) 0 * f j = f i := by
simp_rw [@eq_comm _ _ i, expect_boole_mul]
lemma expect_eq_sum_div_card (s : Finset ι) (f : ι → M) :
𝔼 i ∈ s, f i = (∑ i ∈ s, f i) / #s := by
rw [expect, NNRat.smul_def, div_eq_inv_mul, NNRat.cast_inv, NNRat.cast_natCast]
lemma _root_.Fintype.expect_eq_sum_div_card [Fintype ι] (f : ι → M) :
𝔼 i, f i = (∑ i, f i) / Fintype.card ι := Finset.expect_eq_sum_div_card _ _
lemma expect_div (s : Finset ι) (f : ι → M) (a : M) : (𝔼 i ∈ s, f i) / a = 𝔼 i ∈ s, f i / a := by
simp_rw [div_eq_mul_inv, expect_mul]
end Semifield
@[simp] lemma expect_apply {α : Type*} {π : α → Type*} [∀ a, CommSemiring (π a)]
[∀ a, Module ℚ≥0 (π a)] (s : Finset ι) (f : ι → ∀ a, π a) (a : α) :
(𝔼 i ∈ s, f i) a = 𝔼 i ∈ s, f i a := by simp [expect]
end Finset
namespace algebraMap
variable [Semifield M] [CharZero M] [Semifield N] [CharZero N] [Algebra M N]
@[simp, norm_cast]
lemma coe_expect (s : Finset ι) (f : ι → M) : 𝔼 i ∈ s, f i = 𝔼 i ∈ s, (f i : N) :=
map_expect (algebraMap _ _) _ _
end algebraMap
namespace Fintype
variable [Fintype ι] [Fintype κ]
section AddCommMonoid
variable [AddCommMonoid M] [Module ℚ≥0 M]
/-- `Fintype.expect_bijective` is a variant of `Finset.expect_bij` that accepts
`Function.Bijective`.
See `Function.Bijective.expect_comp` for a version without `h`. -/
lemma expect_bijective (e : ι → κ) (he : Bijective e) (f : ι → M) (g : κ → M)
(h : ∀ i, f i = g (e i)) : 𝔼 i, f i = 𝔼 i, g i :=
expect_nbij e (fun _ _ ↦ mem_univ _) (fun i _ ↦ h i) he.injective.injOn <| by
simpa using he.surjective
/-- `Fintype.expect_equiv` is a specialization of `Finset.expect_bij` that automatically fills in
most arguments.
See `Equiv.expect_comp` for a version without `h`. -/
lemma expect_equiv (e : ι ≃ κ) (f : ι → M) (g : κ → M) (h : ∀ i, f i = g (e i)) :
𝔼 i, f i = 𝔼 i, g i := expect_bijective _ e.bijective f g h
lemma expect_const [Nonempty ι] (a : M) : 𝔼 _i : ι, a = a := Finset.expect_const univ_nonempty _
lemma expect_ite_zero (p : ι → Prop) [DecidablePred p] (h : ∀ i j, p i → p j → i = j) (a : M) :
𝔼 i, ite (p i) a 0 = ite (∃ i, p i) (a /ℚ Fintype.card ι) 0 := by
simp [univ.expect_ite_zero p (by simpa using h)]
variable [DecidableEq ι]
@[simp] lemma expect_ite_mem (s : Finset ι) (f : ι → M) :
𝔼 i, (if i ∈ s then f i else 0) = s.dens • 𝔼 i ∈ s, f i := by
simp [Finset.expect_ite_mem, dens]
lemma expect_dite_eq (i : ι) (f : ∀ j, i = j → M) :
𝔼 j, (if h : i = j then f j h else 0) = f i rfl /ℚ card ι := by simp
lemma expect_dite_eq' (i : ι) (f : ∀ j, j = i → M) :
𝔼 j, (if h : j = i then f j h else 0) = f i rfl /ℚ card ι := by simp
lemma expect_ite_eq (i : ι) (f : ι → M) :
𝔼 j, (if i = j then f j else 0) = f i /ℚ card ι := by simp
lemma expect_ite_eq' (i : ι) (f : ι → M) :
𝔼 j, (if j = i then f j else 0) = f i /ℚ card ι := by simp
end AddCommMonoid
section Semiring
variable [Semiring M] [Module ℚ≥0 M]
lemma expect_one [Nonempty ι] : 𝔼 _i : ι, (1 : M) = 1 := expect_const _
lemma expect_mul_expect [IsScalarTower ℚ≥0 M M] [SMulCommClass ℚ≥0 M M] (f : ι → M)
(g : κ → M) : (𝔼 i, f i) * 𝔼 j, g j = 𝔼 i, 𝔼 j, f i * g j :=
Finset.expect_mul_expect ..
end Semiring
end Fintype |
.lake/packages/mathlib/Mathlib/Algebra/BigOperators/Module.lean | import Mathlib.Algebra.BigOperators.Intervals
import Mathlib.Algebra.Module.Defs
import Mathlib.Tactic.Abel
/-!
# Summation by parts
-/
namespace Finset
variable {R M : Type*} [Ring R] [AddCommGroup M] [Module R M] (f : ℕ → R) (g : ℕ → M) {m n : ℕ}
-- The partial sum of `g`, starting from zero
local notation "G " n:80 => ∑ i ∈ range n, g i
/-- **Summation by parts**, also known as **Abel's lemma** or an **Abel transformation** -/
theorem sum_Ico_by_parts (hmn : m < n) :
∑ i ∈ Ico m n, f i • g i =
f (n - 1) • G n - f m • G m - ∑ i ∈ Ico m (n - 1), (f (i + 1) - f i) • G (i + 1) := by
have h₁ : (∑ i ∈ Ico (m + 1) n, f i • G i) = ∑ i ∈ Ico m (n - 1), f (i + 1) • G (i + 1) := by
rw [← Nat.sub_add_cancel (Nat.one_le_of_lt hmn), ← sum_Ico_add']
simp only [add_tsub_cancel_right]
have h₂ :
(∑ i ∈ Ico (m + 1) n, f i • G (i + 1)) =
(∑ i ∈ Ico m (n - 1), f i • G (i + 1)) + f (n - 1) • G n - f m • G (m + 1) := by
rw [← sum_Ico_sub_bot _ hmn, ← sum_Ico_succ_sub_top _ (Nat.le_sub_one_of_lt hmn),
Nat.sub_add_cancel (pos_of_gt hmn), sub_add_cancel]
rw [sum_eq_sum_Ico_succ_bot hmn]
conv in (occs := 3) (f _ • g _) => rw [← sum_range_succ_sub_sum g]
simp_rw [smul_sub, sum_sub_distrib, h₂, h₁]
conv_lhs => congr; rfl; rw [← add_sub, add_comm, ← add_sub, ← sum_sub_distrib]
have : ∀ i, f i • G (i + 1) - f (i + 1) • G (i + 1) = -((f (i + 1) - f i) • G (i + 1)) := by
intro i
rw [sub_smul]
abel
simp_rw [this, sum_neg_distrib, sum_range_succ, smul_add]
abel
theorem sum_Ioc_by_parts (hmn : m < n) :
∑ i ∈ Ioc m n, f i • g i =
f n • G (n + 1) - f (m + 1) • G (m + 1)
- ∑ i ∈ Ioc m (n - 1), (f (i + 1) - f i) • G (i + 1) := by
simpa only [← Ico_add_one_add_one_eq_Ioc, Nat.sub_add_cancel (Nat.one_le_of_lt hmn),
add_tsub_cancel_right] using sum_Ico_by_parts f g (Nat.succ_lt_succ hmn)
variable (n)
/-- **Summation by parts** for ranges -/
theorem sum_range_by_parts :
∑ i ∈ range n, f i • g i =
f (n - 1) • G n - ∑ i ∈ range (n - 1), (f (i + 1) - f i) • G (i + 1) := by
by_cases hn : n = 0
· simp [hn]
· rw [range_eq_Ico, sum_Ico_by_parts f g (Nat.pos_of_ne_zero hn), sum_range_zero, smul_zero,
sub_zero, range_eq_Ico]
end Finset |
.lake/packages/mathlib/Mathlib/Algebra/BigOperators/Pi.lean | import Mathlib.Algebra.BigOperators.Group.Finset.Lemmas
import Mathlib.Algebra.BigOperators.Group.Finset.Piecewise
import Mathlib.Algebra.BigOperators.GroupWithZero.Finset
import Mathlib.Algebra.Group.Action.Pi
import Mathlib.Algebra.Notation.Indicator
import Mathlib.Algebra.Ring.Pi
import Mathlib.Data.Finset.Lattice.Fold
import Mathlib.Data.Fintype.Basic
/-!
# Big operators for Pi Types
This file contains theorems relevant to big operators in binary and arbitrary product
of monoids and groups
-/
open scoped Finset
variable {ι κ M N R α : Type*}
namespace Pi
@[to_additive]
theorem list_prod_apply {α : Type*} {M : α → Type*} [∀ a, Monoid (M a)] (a : α)
(l : List (∀ a, M a)) : l.prod a = (l.map fun f : ∀ a, M a ↦ f a).prod :=
map_list_prod (evalMonoidHom M a) _
@[to_additive]
theorem multiset_prod_apply {α : Type*} {M : α → Type*} [∀ a, CommMonoid (M a)] (a : α)
(s : Multiset (∀ a, M a)) : s.prod a = (s.map fun f : ∀ a, M a ↦ f a).prod :=
(evalMonoidHom M a).map_multiset_prod _
end Pi
@[to_additive (attr := simp)]
theorem Finset.prod_apply {α : Type*} {M : α → Type*} [∀ a, CommMonoid (M a)] (a : α)
(s : Finset ι) (g : ι → ∀ a, M a) : (∏ c ∈ s, g c) a = ∏ c ∈ s, g c a :=
map_prod (Pi.evalMonoidHom M a) _ _
/-- An 'unapplied' analogue of `Finset.prod_apply`. -/
@[to_additive /-- An 'unapplied' analogue of `Finset.sum_apply`. -/]
theorem Finset.prod_fn {α : Type*} {M : α → Type*} {ι} [∀ a, CommMonoid (M a)] (s : Finset ι)
(g : ι → ∀ a, M a) : ∏ c ∈ s, g c = fun a ↦ ∏ c ∈ s, g c a :=
funext fun _ ↦ Finset.prod_apply _ _ _
@[to_additive]
theorem Fintype.prod_apply {α : Type*} {M : α → Type*} [Fintype ι] [∀ a, CommMonoid (M a)] (a : α)
(g : ι → ∀ a, M a) : (∏ c, g c) a = ∏ c, g c a :=
Finset.prod_apply a Finset.univ g
@[to_additive prod_mk_sum]
theorem prod_mk_prod [CommMonoid M] [CommMonoid N] (s : Finset ι) (f : ι → M) (g : ι → N) :
(∏ x ∈ s, f x, ∏ x ∈ s, g x) = ∏ x ∈ s, (f x, g x) :=
haveI := Classical.decEq ι
Finset.induction_on s rfl (by simp +contextual [Prod.ext_iff])
/-- decomposing `x : ι → R` as a sum along the canonical basis -/
theorem pi_eq_sum_univ {ι : Type*} [Fintype ι] [DecidableEq ι] {R : Type*} [NonAssocSemiring R]
(x : ι → R) : x = ∑ i, (x i) • fun j => if i = j then (1 : R) else 0 := by
ext
simp
/-- Decomposing `x : ι → R` as a sum along the canonical basis `Pi.single i 1` for `i : ι`. -/
theorem pi_eq_sum_univ' {ι : Type*} [Fintype ι] [DecidableEq ι] {R : Type*} [NonAssocSemiring R]
(x : ι → R) : x = ∑ i, (x i) • Pi.single (M := fun _ ↦ R) i 1 := by
convert pi_eq_sum_univ x
aesop
section CommSemiring
variable [CommSemiring R]
lemma prod_indicator_apply (s : Finset ι) (f : ι → Set κ) (g : ι → κ → R) (j : κ) :
∏ i ∈ s, (f i).indicator (g i) j = (⋂ x ∈ s, f x).indicator (∏ i ∈ s, g i) j := by
rw [Set.indicator]
split_ifs with hj
· rw [Finset.prod_apply]
congr! 1 with i hi
simp only [Set.mem_iInter] at hj
exact Set.indicator_of_mem (hj _ hi) _
· obtain ⟨i, hi, hj⟩ := by simpa using hj
exact Finset.prod_eq_zero hi <| Set.indicator_of_notMem hj _
lemma prod_indicator (s : Finset ι) (f : ι → Set κ) (g : ι → κ → R) :
∏ i ∈ s, (f i).indicator (g i) = (⋂ x ∈ s, f x).indicator (∏ i ∈ s, g i) := by
ext a; simpa using prod_indicator_apply ..
lemma prod_indicator_const_apply (s : Finset ι) (f : ι → Set κ) (g : κ → R) (j : κ) :
∏ i ∈ s, (f i).indicator g j = (⋂ x ∈ s, f x).indicator (g ^ #s) j := by
simp [prod_indicator_apply]
lemma prod_indicator_const (s : Finset ι) (f : ι → Set κ) (g : κ → R) :
∏ i ∈ s, (f i).indicator g = (⋂ x ∈ s, f x).indicator (g ^ #s) := by simp [prod_indicator]
end CommSemiring
section MulSingle
variable {I : Type*} [DecidableEq I] {M : I → Type*}
variable [∀ i, CommMonoid (M i)]
@[to_additive]
theorem Finset.univ_prod_mulSingle [Fintype I] (f : ∀ i, M i) :
(∏ i, Pi.mulSingle i (f i)) = f := by
ext a
simp
@[to_additive]
theorem MonoidHom.functions_ext [Finite I] (N : Type*) [CommMonoid N] (g h : (∀ i, M i) →* N)
(H : ∀ i x, g (Pi.mulSingle i x) = h (Pi.mulSingle i x)) : g = h := by
cases nonempty_fintype I
ext k
rw [← Finset.univ_prod_mulSingle k, map_prod, map_prod]
simp only [H]
/-- This is used as the ext lemma instead of `MonoidHom.functions_ext` for reasons explained in
note [partially-applied ext lemmas]. -/
@[to_additive (attr := ext)
/-- This is used as the ext lemma instead of `AddMonoidHom.functions_ext` for reasons
explained in note [partially-applied ext lemmas]. -/]
theorem MonoidHom.functions_ext' [Finite I] (N : Type*) [CommMonoid N] (g h : (∀ i, M i) →* N)
(H : ∀ i, g.comp (MonoidHom.mulSingle M i) = h.comp (MonoidHom.mulSingle M i)) : g = h :=
g.functions_ext N h fun i => DFunLike.congr_fun (H i)
end MulSingle
section RingHom
open Pi
variable {I : Type*} [DecidableEq I] {R : I → Type*}
variable [∀ i, NonAssocSemiring (R i)]
@[ext]
theorem RingHom.functions_ext [Finite I] (S : Type*) [NonAssocSemiring S] (g h : (∀ i, R i) →+* S)
(H : ∀ (i : I) (x : R i), g (single i x) = h (single i x)) : g = h :=
RingHom.coe_addMonoidHom_injective <|
@AddMonoidHom.functions_ext I _ R _ _ S _ (g : (∀ i, R i) →+ S) h H
end RingHom
namespace Prod
variable [CommMonoid M] [CommMonoid N] {s : Finset ι} {f : ι → M × N}
@[to_additive]
theorem fst_prod : (∏ c ∈ s, f c).1 = ∏ c ∈ s, (f c).1 :=
map_prod (MonoidHom.fst ..) f s
@[to_additive]
theorem snd_prod : (∏ c ∈ s, f c).2 = ∏ c ∈ s, (f c).2 :=
map_prod (MonoidHom.snd ..) f s
end Prod
section MulEquiv
/-- The canonical isomorphism between the monoid of homomorphisms from a finite product of
commutative monoids to another commutative monoid and the product of the homomorphism monoids. -/
@[to_additive /-- The canonical isomorphism between the additive monoid of homomorphisms from
a finite product of additive commutative monoids to another additive commutative monoid and
the product of the homomorphism monoids. -/]
def Pi.monoidHomMulEquiv {ι : Type*} [Fintype ι] [DecidableEq ι] (M : ι → Type*)
[(i : ι) → CommMonoid (M i)] (M' : Type*) [CommMonoid M'] :
(((i : ι) → M i) →* M') ≃* ((i : ι) → (M i →* M')) where
toFun φ i := φ.comp <| MonoidHom.mulSingle M i
invFun φ := ∏ (i : ι), (φ i).comp (Pi.evalMonoidHom M i)
left_inv φ := by
ext
simp only [MonoidHom.finset_prod_apply, MonoidHom.coe_comp, Function.comp_apply,
evalMonoidHom_apply, MonoidHom.mulSingle_apply, ← map_prod]
refine congrArg _ <| funext fun _ ↦ ?_
rw [Fintype.prod_apply]
exact Fintype.prod_pi_mulSingle ..
right_inv φ := by
ext i m
simp only [MonoidHom.coe_comp, Function.comp_apply, MonoidHom.mulSingle_apply,
MonoidHom.finset_prod_apply, evalMonoidHom_apply, ]
let φ' i : M i → M' := ⇑(φ i)
conv =>
enter [1, 2, j]
rw [show φ j = φ' j from rfl, Pi.apply_mulSingle φ' (fun i ↦ map_one (φ i))]
rw [show φ' i = φ i from rfl]
exact Fintype.prod_pi_mulSingle' ..
map_mul' φ ψ := by
ext
simp only [MonoidHom.coe_comp, Function.comp_apply, MonoidHom.mulSingle_apply,
MonoidHom.mul_apply, mul_apply]
end MulEquiv
variable [Finite ι] [DecidableEq ι] {M : Type*}
-- manually additivized to fix variable names
-- See https://github.com/leanprover-community/mathlib4/issues/11462
lemma Pi.single_induction [AddCommMonoid M] (p : (ι → M) → Prop) (f : ι → M)
(zero : p 0) (add : ∀ f g, p f → p g → p (f + g))
(single : ∀ i m, p (Pi.single i m)) : p f := by
cases nonempty_fintype ι
rw [← Finset.univ_sum_single f]
exact Finset.sum_induction _ _ add zero (by simp [single])
@[to_additive existing (attr := elab_as_elim)]
lemma Pi.mulSingle_induction [CommMonoid M] (p : (ι → M) → Prop) (f : ι → M)
(one : p 1) (mul : ∀ f g, p f → p g → p (f * g))
(mulSingle : ∀ i m, p (Pi.mulSingle i m)) : p f := by
cases nonempty_fintype ι
rw [← Finset.univ_prod_mulSingle f]
exact Finset.prod_induction _ _ mul one (by simp [mulSingle])
section EqOn
@[to_additive]
theorem eqOn_finsetProd {ι α β : Type*} [CommMonoid α]
{s : Set β} {f f' : ι → β → α} (h : ∀ (i : ι), Set.EqOn (f i) (f' i) s) (v : Finset ι) :
Set.EqOn (∏ i ∈ v, f i) (∏ i ∈ v, f' i) s :=
fun t ht => by simp [funext fun i ↦ h i ht]
@[to_additive]
theorem eqOn_fun_finsetProd {ι α β : Type*} [CommMonoid α]
{s : Set β} {f f' : ι → β → α} (h : ∀ (i : ι), Set.EqOn (f i) (f' i) s) (v : Finset ι) :
Set.EqOn (fun b ↦ ∏ i ∈ v, f i b) (fun b ↦ ∏ i ∈ v, f' i b) s := by
convert eqOn_finsetProd h v <;> simp
end EqOn |
.lake/packages/mathlib/Mathlib/Algebra/BigOperators/Balance.lean | import Mathlib.Algebra.BigOperators.Expect
/-!
# Balancing a function
This file defines the balancing of a function `f`, defined as `f` minus its average.
This is the unique function `g` such that `f a - f b = g a - g b` for all `a` and `b`, and
`∑ a, g a = 0`. This is particularly useful in Fourier analysis as `f` and `g` then have the same
Fourier transform, except in the `0`-th frequency where the Fourier transform of `g` vanishes.
-/
open Finset Function
open scoped BigOperators
variable {ι H F G : Type*}
namespace Fintype
section AddCommGroup
variable [Fintype ι] [AddCommGroup G] [Module ℚ≥0 G] [AddCommGroup H] [Module ℚ≥0 H]
/-- The balancing of a function, namely the function minus its average. -/
def balance (f : ι → G) : ι → G := f - Function.const _ (𝔼 y, f y)
lemma balance_apply (f : ι → G) (x : ι) : balance f x = f x - 𝔼 y, f y := rfl
@[simp] lemma balance_zero : balance (0 : ι → G) = 0 := by simp [balance]
@[simp] lemma balance_add (f g : ι → G) : balance (f + g) = balance f + balance g := by
simp only [balance, expect_add_distrib, ← const_add, add_sub_add_comm, Pi.add_apply]
@[simp] lemma balance_sub (f g : ι → G) : balance (f - g) = balance f - balance g := by
simp only [balance, expect_sub_distrib, const_sub, sub_sub_sub_comm, Pi.sub_apply]
@[simp] lemma balance_neg (f : ι → G) : balance (-f) = -balance f := by
simp only [balance, expect_neg_distrib, const_neg, neg_sub', Pi.neg_apply]
@[simp] lemma sum_balance (f : ι → G) : ∑ x, balance f x = 0 := by
cases isEmpty_or_nonempty ι <;> simp [balance_apply]
@[simp] lemma expect_balance (f : ι → G) : 𝔼 x, balance f x = 0 := by simp [expect]
@[simp] lemma balance_idem (f : ι → G) : balance (balance f) = balance f := by
cases isEmpty_or_nonempty ι <;> ext x <;> simp [balance, expect_sub_distrib, univ_nonempty]
@[simp] lemma map_balance [FunLike F G H] [LinearMapClass F ℚ≥0 G H] (g : F) (f : ι → G) (a : ι) :
g (balance f a) = balance (g ∘ f) a := by simp [balance, map_expect]
end AddCommGroup
end Fintype |
.lake/packages/mathlib/Mathlib/Algebra/BigOperators/Field.lean | import Mathlib.Algebra.BigOperators.Ring.Finset
import Mathlib.Algebra.Field.Defs
import Mathlib.Data.Finset.Density
/-!
# Results about big operators with values in a field
-/
open Fintype
variable {ι K : Type*} [DivisionSemiring K]
lemma Multiset.sum_map_div (s : Multiset ι) (f : ι → K) (a : K) :
(s.map (fun x ↦ f x / a)).sum = (s.map f).sum / a := by
simp only [div_eq_mul_inv, Multiset.sum_map_mul_right]
lemma Finset.sum_div (s : Finset ι) (f : ι → K) (a : K) :
(∑ i ∈ s, f i) / a = ∑ i ∈ s, f i / a := by simp only [div_eq_mul_inv, sum_mul]
-- TODO: Move these to `Algebra.BigOperators.Group.Finset.Basic`, next to the corresponding `card`
-- lemmas, once `Finset.dens` doesn't depend on `Field` anymore.
namespace Finset
variable {α β : Type*} [Fintype β]
@[simp]
lemma dens_disjiUnion (s : Finset α) (t : α → Finset β) (h) :
(s.disjiUnion t h).dens = ∑ a ∈ s, (t a).dens := by simp [dens, sum_div]
variable {s : Finset α} {t : α → Finset β}
lemma dens_biUnion [DecidableEq β] (h : (s : Set α).PairwiseDisjoint t) :
(s.biUnion t).dens = ∑ u ∈ s, (t u).dens := by
simp [dens, card_biUnion h, sum_div]
lemma dens_biUnion_le [DecidableEq β] : (s.biUnion t).dens ≤ ∑ a ∈ s, (t a).dens := by
simp only [dens, ← sum_div]
gcongr
exact mod_cast card_biUnion_le
lemma dens_eq_sum_dens_fiberwise [DecidableEq α] {f : β → α} {t : Finset β}
(h : (t : Set β).MapsTo f s) : t.dens = ∑ a ∈ s, {b ∈ t | f b = a}.dens := by
simp [dens, ← sum_div, card_eq_sum_card_fiberwise h]
lemma dens_eq_sum_dens_image [DecidableEq α] (f : β → α) (t : Finset β) :
t.dens = ∑ a ∈ t.image f, {b ∈ t | f b = a}.dens :=
dens_eq_sum_dens_fiberwise fun _ ↦ mem_image_of_mem _
end Finset |
.lake/packages/mathlib/Mathlib/Algebra/BigOperators/NatAntidiagonal.lean | import Mathlib.Data.Finset.NatAntidiagonal
import Mathlib.Algebra.BigOperators.Group.Finset.Basic
/-!
# Big operators for `NatAntidiagonal`
This file contains theorems relevant to big operators over `Finset.NatAntidiagonal`.
-/
variable {M N : Type*} [CommMonoid M] [AddCommMonoid N]
namespace Finset
namespace Nat
theorem prod_antidiagonal_succ {n : ℕ} {f : ℕ × ℕ → M} :
(∏ p ∈ antidiagonal (n + 1), f p)
= f (0, n + 1) * ∏ p ∈ antidiagonal n, f (p.1 + 1, p.2) := by
rw [antidiagonal_succ, prod_cons, prod_map]; rfl
theorem sum_antidiagonal_succ {n : ℕ} {f : ℕ × ℕ → N} :
(∑ p ∈ antidiagonal (n + 1), f p) = f (0, n + 1) + ∑ p ∈ antidiagonal n, f (p.1 + 1, p.2) :=
@prod_antidiagonal_succ (Multiplicative N) _ _ _
@[to_additive]
theorem prod_antidiagonal_swap {n : ℕ} {f : ℕ × ℕ → M} :
∏ p ∈ antidiagonal n, f p.swap = ∏ p ∈ antidiagonal n, f p := by
conv_lhs => rw [← map_swap_antidiagonal, Finset.prod_map]
rfl
theorem prod_antidiagonal_succ' {n : ℕ} {f : ℕ × ℕ → M} : (∏ p ∈ antidiagonal (n + 1), f p) =
f (n + 1, 0) * ∏ p ∈ antidiagonal n, f (p.1, p.2 + 1) := by
rw [← prod_antidiagonal_swap, prod_antidiagonal_succ, ← prod_antidiagonal_swap]
rfl
theorem sum_antidiagonal_succ' {n : ℕ} {f : ℕ × ℕ → N} :
(∑ p ∈ antidiagonal (n + 1), f p) = f (n + 1, 0) + ∑ p ∈ antidiagonal n, f (p.1, p.2 + 1) :=
@prod_antidiagonal_succ' (Multiplicative N) _ _ _
@[to_additive]
theorem prod_antidiagonal_subst {n : ℕ} {f : ℕ × ℕ → ℕ → M} :
∏ p ∈ antidiagonal n, f p n = ∏ p ∈ antidiagonal n, f p (p.1 + p.2) :=
prod_congr rfl fun p hp ↦ by rw [mem_antidiagonal.mp hp]
@[to_additive]
theorem prod_antidiagonal_eq_prod_range_succ_mk {M : Type*} [CommMonoid M] (f : ℕ × ℕ → M)
(n : ℕ) : ∏ ij ∈ antidiagonal n, f ij = ∏ k ∈ range n.succ, f (k, n - k) :=
Finset.prod_map (range n.succ) ⟨fun i ↦ (i, n - i), fun _ _ h ↦ (Prod.mk.inj h).1⟩ f
/-- This lemma matches more generally than `Finset.Nat.prod_antidiagonal_eq_prod_range_succ_mk` when
using `rw ← `. -/
@[to_additive /-- This lemma matches more generally than
`Finset.Nat.sum_antidiagonal_eq_sum_range_succ_mk` when using `rw ← `. -/]
theorem prod_antidiagonal_eq_prod_range_succ {M : Type*} [CommMonoid M] (f : ℕ → ℕ → M) (n : ℕ) :
∏ ij ∈ antidiagonal n, f ij.1 ij.2 = ∏ k ∈ range n.succ, f k (n - k) :=
prod_antidiagonal_eq_prod_range_succ_mk _ _
end Nat
end Finset |
.lake/packages/mathlib/Mathlib/Algebra/BigOperators/Associated.lean | import Mathlib.Algebra.BigOperators.Finsupp.Basic
import Mathlib.Algebra.Group.Submonoid.Membership
import Mathlib.Algebra.GroupWithZero.Associated
/-!
# Products of associated, prime, and irreducible elements.
This file contains some theorems relating definitions in `Algebra.Associated`
and products of multisets, finsets, and finsupps.
-/
assert_not_exists Field
variable {ι M M₀ : Type*}
-- the same local notation used in `Algebra.Associated`
local infixl:50 " ~ᵤ " => Associated
namespace Prime
variable [CommMonoidWithZero M₀] {p : M₀}
theorem exists_mem_multiset_dvd (hp : Prime p) {s : Multiset M₀} : p ∣ s.prod → ∃ a ∈ s, p ∣ a :=
Multiset.induction_on s (fun h => (hp.not_dvd_one h).elim) fun a s ih h =>
have : p ∣ a * s.prod := by simpa using h
match hp.dvd_or_dvd this with
| Or.inl h => ⟨a, Multiset.mem_cons_self a s, h⟩
| Or.inr h =>
let ⟨a, has, h⟩ := ih h
⟨a, Multiset.mem_cons_of_mem has, h⟩
theorem exists_mem_multiset_map_dvd (hp : Prime p) {s : Multiset ι} {f : ι → M₀} :
p ∣ (s.map f).prod → ∃ a ∈ s, p ∣ f a := fun h => by
simpa only [exists_prop, Multiset.mem_map, exists_exists_and_eq_and] using
hp.exists_mem_multiset_dvd h
theorem exists_mem_finset_dvd (hp : Prime p) {s : Finset ι} {f : ι → M₀} :
p ∣ s.prod f → ∃ i ∈ s, p ∣ f i :=
hp.exists_mem_multiset_map_dvd
end Prime
theorem Prod.associated_iff {M N : Type*} [Monoid M] [Monoid N] {x z : M × N} :
x ~ᵤ z ↔ x.1 ~ᵤ z.1 ∧ x.2 ~ᵤ z.2 :=
⟨fun ⟨u, hu⟩ => ⟨⟨(MulEquiv.prodUnits.toFun u).1, (Prod.eq_iff_fst_eq_snd_eq.1 hu).1⟩,
⟨(MulEquiv.prodUnits.toFun u).2, (Prod.eq_iff_fst_eq_snd_eq.1 hu).2⟩⟩,
fun ⟨⟨u₁, h₁⟩, ⟨u₂, h₂⟩⟩ =>
⟨MulEquiv.prodUnits.invFun (u₁, u₂), Prod.eq_iff_fst_eq_snd_eq.2 ⟨h₁, h₂⟩⟩⟩
theorem Associated.prod {M : Type*} [CommMonoid M] {ι : Type*} (s : Finset ι) (f : ι → M)
(g : ι → M) (h : ∀ i, i ∈ s → (f i) ~ᵤ (g i)) : (∏ i ∈ s, f i) ~ᵤ (∏ i ∈ s, g i) := by
induction s using Finset.induction with
| empty =>
simp only [Finset.prod_empty]
rfl
| insert j s hjs IH =>
classical
convert_to (∏ i ∈ insert j s, f i) ~ᵤ (∏ i ∈ insert j s, g i)
grind [Associated.mul_mul]
theorem exists_associated_mem_of_dvd_prod [CancelCommMonoidWithZero M₀] {p : M₀} (hp : Prime p)
{s : Multiset M₀} : (∀ r ∈ s, Prime r) → p ∣ s.prod → ∃ q ∈ s, p ~ᵤ q :=
Multiset.induction_on s (by simp [mt isUnit_iff_dvd_one.2 hp.not_unit]) fun a s ih hs hps => by
rw [Multiset.prod_cons] at hps
rcases hp.dvd_or_dvd hps with h | h
· have hap := hs a (Multiset.mem_cons.2 (Or.inl rfl))
exact ⟨a, Multiset.mem_cons_self a _, hp.associated_of_dvd hap h⟩
· rcases ih (fun r hr => hs _ (Multiset.mem_cons.2 (Or.inr hr))) h with ⟨q, hq₁, hq₂⟩
exact ⟨q, Multiset.mem_cons.2 (Or.inr hq₁), hq₂⟩
open Submonoid in
/-- Let x, y ∈ M₀. If x * y can be written as a product of units and prime elements, then x can be
written as a product of units and prime elements. -/
theorem divisor_closure_eq_closure [CancelCommMonoidWithZero M₀]
(x y : M₀) (hxy : x * y ∈ closure { r : M₀ | IsUnit r ∨ Prime r}) :
x ∈ closure { r : M₀ | IsUnit r ∨ Prime r} := by
obtain ⟨m, hm, hprod⟩ := exists_multiset_of_mem_closure hxy
induction m using Multiset.induction generalizing x y with
| empty =>
apply subset_closure
simp only [Set.mem_setOf]
simp only [Multiset.prod_zero] at hprod
left; exact .of_mul_eq_one _ hprod.symm
| cons c s hind =>
simp only [Multiset.mem_cons, forall_eq_or_imp, Set.mem_setOf] at hm
simp only [Multiset.prod_cons] at hprod
simp only [Set.mem_setOf_eq] at hind
obtain ⟨ha₁ | ha₂, hs⟩ := hm
· rcases ha₁.exists_right_inv with ⟨k, hk⟩
refine hind x (y * k) ?_ hs ?_
· simp only [← mul_assoc, ← hprod, ← Multiset.prod_cons, mul_comm]
refine multiset_prod_mem _ _ (Multiset.forall_mem_cons.2 ⟨subset_closure ?_,
Multiset.forall_mem_cons.2 ⟨subset_closure ?_, fun t ht => subset_closure (hs t ht)⟩⟩)
· left; exact .of_mul_eq_one_right _ hk
· left; exact ha₁
· rw [← mul_one s.prod, ← hk, ← mul_assoc, ← mul_assoc, mul_eq_mul_right_iff, mul_comm]
left; exact hprod
· rcases ha₂.dvd_mul.1 (Dvd.intro _ hprod) with ⟨c, hc⟩ | ⟨c, hc⟩
· rw [hc]; rw [hc, mul_assoc] at hprod
refine Submonoid.mul_mem _ (subset_closure ?_)
(hind _ _ ?_ hs (mul_left_cancel₀ ha₂.ne_zero hprod))
· right; exact ha₂
rw [← mul_left_cancel₀ ha₂.ne_zero hprod]
exact multiset_prod_mem _ _ (fun t ht => subset_closure (hs t ht))
rw [hc, mul_comm x _, mul_assoc, mul_comm c _] at hprod
refine hind x c ?_ hs (mul_left_cancel₀ ha₂.ne_zero hprod)
rw [← mul_left_cancel₀ ha₂.ne_zero hprod]
exact multiset_prod_mem _ _ (fun t ht => subset_closure (hs t ht))
theorem Multiset.prod_primes_dvd [CancelCommMonoidWithZero M₀]
[∀ a : M₀, DecidablePred (Associated a)] {s : Multiset M₀} (n : M₀) (h : ∀ a ∈ s, Prime a)
(div : ∀ a ∈ s, a ∣ n) (uniq : ∀ a, s.countP (Associated a) ≤ 1) : s.prod ∣ n := by
induction s using Multiset.induction_on generalizing n with
| empty => simp only [Multiset.prod_zero, one_dvd]
| cons a s induct =>
rw [Multiset.prod_cons]
obtain ⟨k, rfl⟩ : a ∣ n := div a (Multiset.mem_cons_self a s)
gcongr
refine induct _ (fun a ha => h a (Multiset.mem_cons_of_mem ha)) (fun b b_in_s => ?_)
fun a => (Multiset.countP_le_of_le _ (Multiset.le_cons_self _ _)).trans (uniq a)
have b_div_n := div b (Multiset.mem_cons_of_mem b_in_s)
have a_prime := h a (Multiset.mem_cons_self a s)
have b_prime := h b (Multiset.mem_cons_of_mem b_in_s)
refine (b_prime.dvd_or_dvd b_div_n).resolve_left fun b_div_a => ?_
have assoc := b_prime.associated_of_dvd a_prime b_div_a
have := uniq a
rw [Multiset.countP_cons_of_pos _ (Associated.refl _), Nat.succ_le_succ_iff, ← not_lt,
Multiset.countP_pos] at this
exact this ⟨b, b_in_s, assoc.symm⟩
theorem Finset.prod_primes_dvd [CancelCommMonoidWithZero M₀] [Subsingleton M₀ˣ] {s : Finset M₀}
(n : M₀) (h : ∀ a ∈ s, Prime a) (div : ∀ a ∈ s, a ∣ n) : ∏ p ∈ s, p ∣ n := by
classical
exact
Multiset.prod_primes_dvd n (by simpa only [Multiset.map_id', Finset.mem_def] using h)
(by simpa only [Multiset.map_id', Finset.mem_def] using div)
(by
simp only [Multiset.map_id', associated_eq_eq, Multiset.countP_eq_card_filter,
← s.val.count_eq_card_filter_eq, ← Multiset.nodup_iff_count_le_one, s.nodup])
namespace Associates
section CommMonoid
variable [CommMonoid M]
theorem prod_mk {p : Multiset M} : (p.map Associates.mk).prod = Associates.mk p.prod :=
Multiset.induction_on p (by simp) fun a s ih => by simp [ih, Associates.mk_mul_mk]
theorem finset_prod_mk {p : Finset ι} {f : ι → M} :
(∏ i ∈ p, Associates.mk (f i)) = Associates.mk (∏ i ∈ p, f i) := by
rw [Finset.prod_eq_multiset_prod, ← Function.comp_def, ← Multiset.map_map, prod_mk,
← Finset.prod_eq_multiset_prod]
theorem rel_associated_iff_map_eq_map {p q : Multiset M} :
Multiset.Rel Associated p q ↔ p.map Associates.mk = q.map Associates.mk := by
rw [← Multiset.rel_eq, Multiset.rel_map]
simp only [mk_eq_mk_iff_associated]
theorem prod_eq_one_iff {p : Multiset (Associates M)} :
p.prod = 1 ↔ ∀ a ∈ p, (a : Associates M) = 1 :=
Multiset.induction_on p (by simp)
(by simp +contextual [mul_eq_one, or_imp, forall_and])
theorem prod_le_prod {p q : Multiset (Associates M)} (h : p ≤ q) : p.prod ≤ q.prod := by
haveI := Classical.decEq (Associates M)
suffices p.prod ≤ (p + (q - p)).prod by rwa [add_tsub_cancel_of_le h] at this
suffices p.prod * 1 ≤ p.prod * (q - p).prod by simpa
exact mul_mono (le_refl p.prod) one_le
end CommMonoid
section CancelCommMonoidWithZero
variable [CancelCommMonoidWithZero M₀]
theorem exists_mem_multiset_le_of_prime {s : Multiset (Associates M₀)} {p : Associates M₀}
(hp : Prime p) : p ≤ s.prod → ∃ a ∈ s, p ≤ a :=
Multiset.induction_on s (fun ⟨_, eq⟩ => (hp.ne_one (mul_eq_one.1 eq.symm).1).elim)
fun a s ih h =>
have : p ≤ a * s.prod := by simpa using h
match Prime.le_or_le hp this with
| Or.inl h => ⟨a, Multiset.mem_cons_self a s, h⟩
| Or.inr h =>
let ⟨a, has, h⟩ := ih h
⟨a, Multiset.mem_cons_of_mem has, h⟩
end CancelCommMonoidWithZero
end Associates
namespace Multiset
theorem prod_ne_zero_of_prime [CancelCommMonoidWithZero M₀] [Nontrivial M₀] (s : Multiset M₀)
(h : ∀ x ∈ s, Prime x) : s.prod ≠ 0 :=
Multiset.prod_ne_zero fun h0 => Prime.ne_zero (h 0 h0) rfl
end Multiset
open Finset Finsupp
section CommMonoidWithZero
variable {M : Type*} [CommMonoidWithZero M]
theorem Prime.dvd_finset_prod_iff {S : Finset M₀} {p : M} (pp : Prime p) (g : M₀ → M) :
p ∣ S.prod g ↔ ∃ a ∈ S, p ∣ g a :=
⟨pp.exists_mem_finset_dvd, fun ⟨_, ha1, ha2⟩ => dvd_trans ha2 (dvd_prod_of_mem g ha1)⟩
theorem Prime.not_dvd_finset_prod {S : Finset M₀} {p : M} (pp : Prime p) {g : M₀ → M}
(hS : ∀ a ∈ S, ¬p ∣ g a) : ¬p ∣ S.prod g := by
exact mt (Prime.dvd_finset_prod_iff pp _).1 <| not_exists.2 fun a => not_and.2 (hS a)
theorem Prime.dvd_finsuppProd_iff {f : M₀ →₀ M} {g : M₀ → M → ℕ} {p : ℕ} (pp : Prime p) :
p ∣ f.prod g ↔ ∃ a ∈ f.support, p ∣ g a (f a) :=
Prime.dvd_finset_prod_iff pp _
theorem Prime.not_dvd_finsuppProd {f : M₀ →₀ M} {g : M₀ → M → ℕ} {p : ℕ} (pp : Prime p)
(hS : ∀ a ∈ f.support, ¬p ∣ g a (f a)) : ¬p ∣ f.prod g :=
Prime.not_dvd_finset_prod pp hS
end CommMonoidWithZero |
.lake/packages/mathlib/Mathlib/Algebra/BigOperators/Intervals.lean | import Mathlib.Algebra.Order.BigOperators.Group.LocallyFinite
import Mathlib.Algebra.Order.Interval.Finset.Basic
import Mathlib.Algebra.Order.Sub.Basic
import Mathlib.Data.Nat.Factorial.Basic
/-!
# Results about big operators over intervals
We prove results about big operators over intervals.
-/
open Nat
variable {α G M : Type*}
namespace Finset
section Generic
variable [CommMonoid M] {s₂ s₁ s : Finset α} {a : α} {g f : α → M}
@[to_additive]
theorem prod_Ico_add' [AddCommMonoid α] [PartialOrder α] [IsOrderedCancelAddMonoid α]
[ExistsAddOfLE α] [LocallyFiniteOrder α]
(f : α → M) (a b c : α) : (∏ x ∈ Ico a b, f (x + c)) = ∏ x ∈ Ico (a + c) (b + c), f x := by
rw [← map_add_right_Ico, prod_map]
rfl
@[to_additive]
theorem prod_Ico_add [AddCommMonoid α] [PartialOrder α] [IsOrderedCancelAddMonoid α]
[ExistsAddOfLE α] [LocallyFiniteOrder α]
(f : α → M) (a b c : α) : (∏ x ∈ Ico a b, f (c + x)) = ∏ x ∈ Ico (a + c) (b + c), f x := by
convert prod_Ico_add' f a b c using 2
rw [add_comm]
@[to_additive (attr := simp)]
theorem prod_Ico_add_right_sub_eq [AddCommMonoid α] [PartialOrder α] [IsOrderedCancelAddMonoid α]
[ExistsAddOfLE α] [LocallyFiniteOrder α] [Sub α] [OrderedSub α] (a b c : α) :
∏ x ∈ Ico (a + c) (b + c), f (x - c) = ∏ x ∈ Ico a b, f x := by
simp only [← map_add_right_Ico, prod_map, addRightEmbedding_apply, add_tsub_cancel_right]
@[to_additive]
theorem prod_Ico_succ_top {a b : ℕ} (hab : a ≤ b) (f : ℕ → M) :
(∏ k ∈ Ico a (b + 1), f k) = (∏ k ∈ Ico a b, f k) * f b := by
rw [← Finset.insert_Ico_right_eq_Ico_add_one hab, prod_insert right_notMem_Ico, mul_comm]
@[to_additive]
theorem prod_Ico_consecutive (f : ℕ → M) {m n k : ℕ} (hmn : m ≤ n) (hnk : n ≤ k) :
((∏ i ∈ Ico m n, f i) * ∏ i ∈ Ico n k, f i) = ∏ i ∈ Ico m k, f i :=
Ico_union_Ico_eq_Ico hmn hnk ▸ Eq.symm (prod_union (Ico_disjoint_Ico_consecutive m n k))
@[to_additive]
theorem prod_Ioc_consecutive (f : ℕ → M) {m n k : ℕ} (hmn : m ≤ n) (hnk : n ≤ k) :
((∏ i ∈ Ioc m n, f i) * ∏ i ∈ Ioc n k, f i) = ∏ i ∈ Ioc m k, f i := by
rw [← Ioc_union_Ioc_eq_Ioc hmn hnk, prod_union]
apply disjoint_left.2 fun x hx h'x => _
intro x hx h'x
exact lt_irrefl _ ((mem_Ioc.1 h'x).1.trans_le (mem_Ioc.1 hx).2)
@[to_additive]
theorem prod_Ioc_succ_top {a b : ℕ} (hab : a ≤ b) (f : ℕ → M) :
(∏ k ∈ Ioc a (b + 1), f k) = (∏ k ∈ Ioc a b, f k) * f (b + 1) := by
rw [← prod_Ioc_consecutive _ hab (Nat.le_succ b), Nat.Ioc_succ_singleton, prod_singleton]
@[to_additive]
theorem prod_Icc_succ_top {a b : ℕ} (hab : a ≤ b + 1) (f : ℕ → M) :
(∏ k ∈ Icc a (b + 1), f k) = (∏ k ∈ Icc a b, f k) * f (b + 1) := by
rw [← Ico_add_one_right_eq_Icc, prod_Ico_succ_top hab, Ico_add_one_right_eq_Icc]
@[to_additive]
theorem prod_range_mul_prod_Ico (f : ℕ → M) {m n : ℕ} (h : m ≤ n) :
((∏ k ∈ range m, f k) * ∏ k ∈ Ico m n, f k) = ∏ k ∈ range n, f k :=
Nat.Ico_zero_eq_range ▸ Nat.Ico_zero_eq_range ▸ prod_Ico_consecutive f m.zero_le h
@[to_additive]
theorem prod_range_eq_mul_Ico (f : ℕ → M) {n : ℕ} (hn : 0 < n) :
∏ x ∈ Finset.range n, f x = f 0 * ∏ x ∈ Ico 1 n, f x :=
Finset.range_eq_Ico ▸ Finset.prod_eq_prod_Ico_succ_bot hn f
@[to_additive]
theorem prod_Ico_eq_mul_inv {δ : Type*} [CommGroup δ] (f : ℕ → δ) {m n : ℕ} (h : m ≤ n) :
∏ k ∈ Ico m n, f k = (∏ k ∈ range n, f k) * (∏ k ∈ range m, f k)⁻¹ :=
eq_mul_inv_iff_mul_eq.2 <| by (rw [mul_comm]; exact prod_range_mul_prod_Ico f h)
@[to_additive]
theorem prod_Ico_eq_div {δ : Type*} [CommGroup δ] (f : ℕ → δ) {m n : ℕ} (h : m ≤ n) :
∏ k ∈ Ico m n, f k = (∏ k ∈ range n, f k) / ∏ k ∈ range m, f k := by
simpa only [div_eq_mul_inv] using prod_Ico_eq_mul_inv f h
@[to_additive]
theorem prod_range_div_prod_range {G : Type*} [CommGroup G] {f : ℕ → G} {n m : ℕ} (hnm : n ≤ m) :
((∏ k ∈ range m, f k) / ∏ k ∈ range n, f k) = ∏ k ∈ range m with n ≤ k, f k := by
rw [← prod_Ico_eq_div f hnm]
congr
apply Finset.ext
simp only [mem_Ico, mem_filter, mem_range, *]
tauto
/-- The two ways of summing over `(i, j)` in the range `a ≤ i ≤ j < b` are equal. -/
theorem sum_Ico_Ico_comm {M : Type*} [AddCommMonoid M] (a b : ℕ) (f : ℕ → ℕ → M) :
(∑ i ∈ Finset.Ico a b, ∑ j ∈ Finset.Ico i b, f i j) =
∑ j ∈ Finset.Ico a b, ∑ i ∈ Finset.Ico a (j + 1), f i j := by
rw [Finset.sum_sigma', Finset.sum_sigma']
refine sum_nbij' (fun x ↦ ⟨x.2, x.1⟩) (fun x ↦ ⟨x.2, x.1⟩) ?_ ?_ (fun _ _ ↦ rfl) (fun _ _ ↦ rfl)
(fun _ _ ↦ rfl) <;>
simp only [Finset.mem_Ico, Sigma.forall, Finset.mem_sigma] <;>
omega
/-- The two ways of summing over `(i, j)` in the range `a ≤ i < j < b` are equal. -/
theorem sum_Ico_Ico_comm' {M : Type*} [AddCommMonoid M] (a b : ℕ) (f : ℕ → ℕ → M) :
(∑ i ∈ Finset.Ico a b, ∑ j ∈ Finset.Ico (i + 1) b, f i j) =
∑ j ∈ Finset.Ico a b, ∑ i ∈ Finset.Ico a j, f i j := by
rw [Finset.sum_sigma', Finset.sum_sigma']
refine sum_nbij' (fun x ↦ ⟨x.2, x.1⟩) (fun x ↦ ⟨x.2, x.1⟩) ?_ ?_ (fun _ _ ↦ rfl) (fun _ _ ↦ rfl)
(fun _ _ ↦ rfl) <;>
simp only [Finset.mem_Ico, Sigma.forall, Finset.mem_sigma] <;>
omega
@[to_additive]
theorem prod_Ico_eq_prod_range (f : ℕ → M) (m n : ℕ) :
∏ k ∈ Ico m n, f k = ∏ k ∈ range (n - m), f (m + k) := by
by_cases! h : m ≤ n
· rw [← Nat.Ico_zero_eq_range, prod_Ico_add, zero_add, tsub_add_cancel_of_le h]
· replace h := h.le
rw [Ico_eq_empty_of_le h, tsub_eq_zero_iff_le.mpr h, range_zero, prod_empty, prod_empty]
theorem prod_Ico_reflect (f : ℕ → M) (k : ℕ) {m n : ℕ} (h : m ≤ n + 1) :
(∏ j ∈ Ico k m, f (n - j)) = ∏ j ∈ Ico (n + 1 - m) (n + 1 - k), f j := by
have : ∀ i < m, i ≤ n := by
intro i hi
exact (add_le_add_iff_right 1).1 (le_trans (Nat.lt_iff_add_one_le.1 hi) h)
rcases lt_or_ge k m with hkm | hkm
· rw [← Nat.Ico_image_const_sub_eq_Ico (this _ hkm)]
refine (prod_image ?_).symm
simp only [mem_Ico, Set.InjOn, mem_coe]
rintro i ⟨_, im⟩ j ⟨_, jm⟩ Hij
rw [← tsub_tsub_cancel_of_le (this _ im), Hij, tsub_tsub_cancel_of_le (this _ jm)]
· have : n + 1 - k ≤ n + 1 - m := by
rw [tsub_le_tsub_iff_left h]
exact hkm
simp only [hkm, Ico_eq_empty_of_le, prod_empty, Ico_eq_empty_of_le this]
theorem sum_Ico_reflect {δ : Type*} [AddCommMonoid δ] (f : ℕ → δ) (k : ℕ) {m n : ℕ}
(h : m ≤ n + 1) : (∑ j ∈ Ico k m, f (n - j)) = ∑ j ∈ Ico (n + 1 - m) (n + 1 - k), f j :=
@prod_Ico_reflect (Multiplicative δ) _ f k m n h
theorem prod_range_reflect (f : ℕ → M) (n : ℕ) :
(∏ j ∈ range n, f (n - 1 - j)) = ∏ j ∈ range n, f j := by
cases n
· simp
· simp only [← Nat.Ico_zero_eq_range, Nat.succ_sub_succ_eq_sub, tsub_zero]
rw [prod_Ico_reflect _ _ le_rfl]
simp
theorem sum_range_reflect {δ : Type*} [AddCommMonoid δ] (f : ℕ → δ) (n : ℕ) :
(∑ j ∈ range n, f (n - 1 - j)) = ∑ j ∈ range n, f j :=
@prod_range_reflect (Multiplicative δ) _ f n
@[simp]
theorem prod_Ico_id_eq_factorial : ∀ n : ℕ, (∏ x ∈ Ico 1 (n + 1), x) = n !
| 0 => rfl
| n + 1 => by
rw [prod_Ico_succ_top <| Nat.succ_le_succ <| Nat.zero_le n, Nat.factorial_succ,
prod_Ico_id_eq_factorial n, Nat.succ_eq_add_one, mul_comm]
section GaussSum
/-- Gauss' summation formula -/
theorem sum_range_id_mul_two (n : ℕ) : (∑ i ∈ range n, i) * 2 = n * (n - 1) :=
calc
(∑ i ∈ range n, i) * 2 = (∑ i ∈ range n, i) + ∑ i ∈ range n, (n - 1 - i) := by
rw [sum_range_reflect (fun i => i) n, mul_two]
_ = ∑ i ∈ range n, (i + (n - 1 - i)) := sum_add_distrib.symm
_ = ∑ _ ∈ range n, (n - 1) :=
sum_congr rfl fun _ hi => add_tsub_cancel_of_le <| Nat.le_sub_one_of_lt <| mem_range.1 hi
_ = n * (n - 1) := by rw [sum_const, card_range, Nat.nsmul_eq_mul]
/-- Gauss' summation formula -/
theorem sum_range_id (n : ℕ) : ∑ i ∈ range n, i = n * (n - 1) / 2 := by
rw [← sum_range_id_mul_two n, Nat.mul_div_cancel _ Nat.zero_lt_two]
end GaussSum
@[to_additive]
lemma prod_range_diag_flip (n : ℕ) (f : ℕ → ℕ → M) :
(∏ m ∈ range n, ∏ k ∈ range (m + 1), f k (m - k)) =
∏ m ∈ range n, ∏ k ∈ range (n - m), f m k := by
rw [prod_sigma', prod_sigma']
refine prod_nbij' (fun a ↦ ⟨a.2, a.1 - a.2⟩) (fun a ↦ ⟨a.1 + a.2, a.1⟩) ?_ ?_ ?_ ?_ ?_ <;>
simp +contextual only [mem_sigma, mem_range, lt_tsub_iff_left,
Nat.lt_succ_iff, le_add_iff_nonneg_right, Nat.zero_le, and_true, and_imp, implies_true,
Sigma.forall, add_tsub_cancel_of_le, add_tsub_cancel_left]
exact fun a b han hba ↦ lt_of_le_of_lt hba han
end Generic
section Nat
variable {M : Type*}
variable (f g : ℕ → M) {m n : ℕ}
section Group
variable [CommGroup M]
@[to_additive]
theorem prod_range_succ_div_prod : ((∏ i ∈ range (n + 1), f i) / ∏ i ∈ range n, f i) = f n :=
div_eq_iff_eq_mul'.mpr <| prod_range_succ f n
@[to_additive]
theorem prod_range_succ_div_top : (∏ i ∈ range (n + 1), f i) / f n = ∏ i ∈ range n, f i :=
div_eq_iff_eq_mul.mpr <| prod_range_succ f n
@[to_additive]
theorem prod_Ico_div_bot (hmn : m < n) : (∏ i ∈ Ico m n, f i) / f m = ∏ i ∈ Ico (m + 1) n, f i :=
div_eq_iff_eq_mul'.mpr <| prod_eq_prod_Ico_succ_bot hmn _
@[to_additive]
theorem prod_Ico_succ_div_top (hmn : m ≤ n) :
(∏ i ∈ Ico m (n + 1), f i) / f n = ∏ i ∈ Ico m n, f i :=
div_eq_iff_eq_mul.mpr <| prod_Ico_succ_top hmn _
@[to_additive]
theorem prod_Ico_div (hmn : m ≤ n) : ∏ i ∈ Ico m n, f (i + 1) / f i = f n / f m := by
rw [prod_Ico_eq_div _ hmn, prod_range_div, prod_range_div, div_div_div_cancel_right]
end Group
end Nat
end Finset |
.lake/packages/mathlib/Mathlib/Algebra/BigOperators/Sym.lean | import Mathlib.Data.Finset.Sym
import Mathlib.Data.Sym.Sym2.Order
import Mathlib.Algebra.BigOperators.Group.Finset.Basic
/-!
# Lemmas on `Finset.sum` and `Finset.prod` involving `Finset.sym2` or `Finset.sym`.
-/
namespace Finset
open Multiset
theorem sum_sym2_filter_not_isDiag {ι M} [LinearOrder ι] [AddCommMonoid M]
(s : Finset ι) (p : Sym2 ι → M) :
∑ i ∈ s.sym2 with ¬ i.IsDiag, p i = ∑ i ∈ s.offDiag with i.1 < i.2, p s(i.1, i.2) := by
rw [Finset.offDiag_filter_lt_eq_filter_le]
conv_rhs => rw [← Finset.sum_subtype_eq_sum_filter]
refine (Finset.sum_equiv Sym2.sortEquiv.symm ?_ ?_).symm
all_goals aesop
theorem sum_count_of_mem_sym {α} [DecidableEq α] {m : ℕ} {k : Sym α m} {s : Finset α}
(hk : k ∈ s.sym m) : (∑ i ∈ s, count i k) = m := by
simp_all
end Finset |
.lake/packages/mathlib/Mathlib/Algebra/BigOperators/Finsupp/Fin.lean | import Mathlib.Algebra.BigOperators.Fin
import Mathlib.Algebra.BigOperators.Finsupp.Basic
import Mathlib.Data.Finsupp.Fin
/-!
# `Finsupp.sum` and `Finsupp.prod` over `Fin`
This file contains theorems relevant to big operators on finitely supported functions over `Fin`.
-/
variable {M N : Type*}
namespace Finsupp
lemma sum_cons [AddCommMonoid M] (n : ℕ) (σ : Fin n →₀ M) (i : M) :
(sum (cons i σ) fun _ e ↦ e) = i + sum σ (fun _ e ↦ e) := by
rw [sum_fintype _ _ (fun _ => rfl), sum_fintype _ _ (fun _ => rfl)]
exact Fin.sum_cons i σ
lemma sum_cons' [Zero M] [AddCommMonoid N] (n : ℕ) (σ : Fin n →₀ M) (i : M)
(f : Fin (n + 1) → M → N) (h : ∀ x, f x 0 = 0) :
(sum (Finsupp.cons i σ) f) = f 0 i + sum σ (Fin.tail f) := by
rw [sum_fintype _ _ (fun _ => by apply h), sum_fintype _ _ (fun _ => by apply h)]
simp_rw [Fin.sum_univ_succ, cons_zero, cons_succ]
congr
theorem ofSupportFinite_fin_two_eq (n : Fin 2 →₀ ℕ) :
ofSupportFinite ![n 0, n 1] (Set.toFinite _) = n := by
rw [Finsupp.ext_iff, Fin.forall_fin_two]
exact ⟨rfl, rfl⟩
end Finsupp
section Fin2
variable (M) in
/-- The space of finitely supported functions `Fin 2 →₀ α` is equivalent to `α × α`.
See also `finTwoArrowEquiv`. -/
@[simps! apply symm_apply]
noncomputable def finTwoArrowEquiv' [Zero M] : (Fin 2 →₀ M) ≃ M × M :=
Finsupp.equivFunOnFinite.trans (finTwoArrowEquiv M)
theorem finTwoArrowEquiv'_sum_eq {d : M × M} [AddCommMonoid M] :
(((finTwoArrowEquiv' M).symm d).sum fun _ n ↦ n) = d.1 + d.2 := by
apply (Finsupp.equivFunOnFinite_symm_sum _).trans
simp
end Fin2 |
.lake/packages/mathlib/Mathlib/Algebra/BigOperators/Finsupp/Basic.lean | import Mathlib.Algebra.BigOperators.Group.Finset.Sigma
import Mathlib.Algebra.BigOperators.Pi
import Mathlib.Algebra.BigOperators.Ring.Finset
import Mathlib.Algebra.Group.Submonoid.BigOperators
import Mathlib.Data.Finsupp.Ext
import Mathlib.Data.Finsupp.Indicator
/-!
# Big operators for finsupps
This file contains theorems relevant to big operators in finitely supported functions.
-/
assert_not_exists Field
noncomputable section
open Finset Function
variable {α ι γ A B C : Type*} [AddCommMonoid A] [AddCommMonoid B] [AddCommMonoid C]
variable {t : ι → A → C}
variable {s : Finset α} {f : α → ι →₀ A} (i : ι)
variable (g : ι →₀ A) (k : ι → A → γ → B) (x : γ)
variable {β M M' N P G H R S : Type*}
namespace Finsupp
/-!
### Declarations about `Finsupp.sum` and `Finsupp.prod`
In most of this section, the domain `β` is assumed to be an `AddMonoid`.
-/
section SumProd
/-- `prod f g` is the product of `g a (f a)` over the support of `f`. -/
@[to_additive /-- `sum f g` is the sum of `g a (f a)` over the support of `f`. -/]
def prod [Zero M] [CommMonoid N] (f : α →₀ M) (g : α → M → N) : N :=
∏ a ∈ f.support, g a (f a)
variable [Zero M] [Zero M'] [CommMonoid N]
@[to_additive]
theorem prod_of_support_subset (f : α →₀ M) {s : Finset α} (hs : f.support ⊆ s) (g : α → M → N)
(h : ∀ i ∈ s, g i 0 = 1) : f.prod g = ∏ x ∈ s, g x (f x) := by
refine Finset.prod_subset hs fun x hxs hx => h x hxs ▸ (congr_arg (g x) ?_)
exact notMem_support_iff.1 hx
@[to_additive]
theorem prod_fintype [Fintype α] (f : α →₀ M) (g : α → M → N) (h : ∀ i, g i 0 = 1) :
f.prod g = ∏ i, g i (f i) :=
f.prod_of_support_subset (subset_univ _) g fun x _ => h x
@[to_additive (attr := simp)]
theorem prod_single_index {a : α} {b : M} {h : α → M → N} (h_zero : h a 0 = 1) :
(single a b).prod h = h a b :=
calc
(single a b).prod h = ∏ x ∈ {a}, h x (single a b x) :=
prod_of_support_subset _ support_single_subset h fun _ hx =>
(mem_singleton.1 hx).symm ▸ h_zero
_ = h a b := by simp
@[to_additive]
theorem prod_mapRange_index {f : M → M'} {hf : f 0 = 0} {g : α →₀ M} {h : α → M' → N}
(h0 : ∀ a, h a 0 = 1) : (mapRange f hf g).prod h = g.prod fun a b => h a (f b) :=
Finset.prod_subset support_mapRange fun _ _ H => by rw [notMem_support_iff.1 H, h0]
@[to_additive (attr := simp)]
theorem prod_zero_index {h : α → M → N} : (0 : α →₀ M).prod h = 1 :=
rfl
@[to_additive]
theorem prod_comm (f : α →₀ M) (g : β →₀ M') (h : α → M → β → M' → N) :
(f.prod fun x v => g.prod fun x' v' => h x v x' v') =
g.prod fun x' v' => f.prod fun x v => h x v x' v' :=
Finset.prod_comm
@[to_additive]
theorem prod_finsetProd_comm {s : Finset β} (f : α →₀ M) (h : α → M → β → N) :
(f.prod fun a m => ∏ b ∈ s, h a m b) = ∏ b ∈ s, f.prod fun a m => h a m b := Finset.prod_comm
@[to_additive (attr := simp)]
theorem prod_ite_eq [DecidableEq α] (f : α →₀ M) (a : α) (b : α → M → N) :
(f.prod fun x v => ite (a = x) (b x v) 1) = ite (a ∈ f.support) (b a (f a)) 1 := by
dsimp [Finsupp.prod]
rw [f.support.prod_ite_eq]
theorem sum_ite_self_eq [DecidableEq α] {N : Type*} [AddCommMonoid N] (f : α →₀ N) (a : α) :
(f.sum fun x v => ite (a = x) v 0) = f a := by
simp_all
/--
The left-hand side of `sum_ite_self_eq` simplifies; this is the variant that is useful for `simp`.
-/
@[simp]
theorem if_mem_support [DecidableEq α] {N : Type*} [Zero N] (f : α →₀ N) (a : α) :
(if a ∈ f.support then f a else 0) = f a := by
simp only [mem_support_iff, ne_eq, ite_eq_left_iff, not_not]
exact fun h ↦ h.symm
/-- A restatement of `prod_ite_eq` with the equality test reversed. -/
@[to_additive (attr := simp) /-- A restatement of `sum_ite_eq` with the equality test reversed. -/]
theorem prod_ite_eq' [DecidableEq α] (f : α →₀ M) (a : α) (b : α → M → N) :
(f.prod fun x v => ite (x = a) (b x v) 1) = ite (a ∈ f.support) (b a (f a)) 1 := by
dsimp [Finsupp.prod]
rw [f.support.prod_ite_eq']
/-- A restatement of `sum_ite_self_eq` with the equality test reversed. -/
theorem sum_ite_self_eq' [DecidableEq α] {N : Type*} [AddCommMonoid N] (f : α →₀ N) (a : α) :
(f.sum fun x v => ite (x = a) v 0) = f a := by
simp
@[to_additive (attr := simp)]
theorem prod_pow [Fintype α] (f : α →₀ ℕ) (g : α → N) :
(f.prod fun a b => g a ^ b) = ∏ a, g a ^ f a :=
f.prod_fintype _ fun _ ↦ pow_zero _
@[to_additive (attr := simp)]
theorem prod_zpow {N} [DivisionCommMonoid N] [Fintype α] (f : α →₀ ℤ) (g : α → N) :
(f.prod fun a b => g a ^ b) = ∏ a, g a ^ f a :=
f.prod_fintype _ fun _ ↦ zpow_zero _
/-- If `g` maps a second argument of 0 to 1, then multiplying it over the
result of `onFinset` is the same as multiplying it over the original `Finset`. -/
@[to_additive
/-- If `g` maps a second argument of 0 to 0, summing it over the
result of `onFinset` is the same as summing it over the original `Finset`. -/]
theorem onFinset_prod {s : Finset α} {f : α → M} {g : α → M → N} (hf : ∀ a, f a ≠ 0 → a ∈ s)
(hg : ∀ a, g a 0 = 1) : (onFinset s f hf).prod g = ∏ a ∈ s, g a (f a) :=
Finset.prod_subset support_onFinset_subset <| by simp +contextual [*]
/-- Taking a product over `f : α →₀ M` is the same as multiplying the value on a single element
`y ∈ f.support` by the product over `erase y f`. -/
@[to_additive
/-- Taking a sum over `f : α →₀ M` is the same as adding the value on a
single element `y ∈ f.support` to the sum over `erase y f`. -/]
theorem mul_prod_erase (f : α →₀ M) (y : α) (g : α → M → N) (hyf : y ∈ f.support) :
g y (f y) * (erase y f).prod g = f.prod g := by
classical
rw [Finsupp.prod, Finsupp.prod, ← Finset.mul_prod_erase _ _ hyf, Finsupp.support_erase,
Finset.prod_congr rfl]
intro h hx
rw [Finsupp.erase_ne (ne_of_mem_erase hx)]
/-- Generalization of `Finsupp.mul_prod_erase`: if `g` maps a second argument of 0 to 1,
then its product over `f : α →₀ M` is the same as multiplying the value on any element
`y : α` by the product over `erase y f`. -/
@[to_additive
/-- Generalization of `Finsupp.add_sum_erase`: if `g` maps a second argument of 0
to 0, then its sum over `f : α →₀ M` is the same as adding the value on any element
`y : α` to the sum over `erase y f`. -/]
theorem mul_prod_erase' (f : α →₀ M) (y : α) (g : α → M → N) (hg : ∀ i : α, g i 0 = 1) :
g y (f y) * (erase y f).prod g = f.prod g := by
classical
by_cases hyf : y ∈ f.support
· exact Finsupp.mul_prod_erase f y g hyf
· rw [notMem_support_iff.mp hyf, hg y, erase_of_notMem_support hyf, one_mul]
@[to_additive]
theorem _root_.SubmonoidClass.finsuppProd_mem {S : Type*} [SetLike S N] [SubmonoidClass S N]
(s : S) (f : α →₀ M) (g : α → M → N) (h : ∀ c, f c ≠ 0 → g c (f c) ∈ s) : f.prod g ∈ s :=
prod_mem fun _i hi => h _ (Finsupp.mem_support_iff.mp hi)
@[to_additive]
theorem prod_congr {f : α →₀ M} {g1 g2 : α → M → N} (h : ∀ x ∈ f.support, g1 x (f x) = g2 x (f x)) :
f.prod g1 = f.prod g2 :=
Finset.prod_congr rfl h
@[to_additive]
theorem prod_eq_single {f : α →₀ M} (a : α) {g : α → M → N}
(h₀ : ∀ b, f b ≠ 0 → b ≠ a → g b (f b) = 1) (h₁ : f a = 0 → g a 0 = 1) :
f.prod g = g a (f a) := by
refine Finset.prod_eq_single a (fun b hb₁ hb₂ => ?_) (fun h => ?_)
· exact h₀ b (mem_support_iff.mp hb₁) hb₂
· simp only [notMem_support_iff] at h
rw [h]
exact h₁ h
end SumProd
section CommMonoidWithZero
variable [Zero α] [CommMonoidWithZero β] [Nontrivial β] [NoZeroDivisors β]
{f : ι →₀ α} (a : α) {g : ι → α → β}
@[simp]
lemma prod_eq_zero_iff : f.prod g = 0 ↔ ∃ i ∈ f.support, g i (f i) = 0 := Finset.prod_eq_zero_iff
lemma prod_ne_zero_iff : f.prod g ≠ 0 ↔ ∀ i ∈ f.support, g i (f i) ≠ 0 := Finset.prod_ne_zero_iff
end CommMonoidWithZero
end Finsupp
@[to_additive]
theorem map_finsuppProd [Zero M] [CommMonoid N] [CommMonoid P] {H : Type*}
[FunLike H N P] [MonoidHomClass H N P]
(h : H) (f : α →₀ M) (g : α → M → N) : h (f.prod g) = f.prod fun a b => h (g a b) :=
map_prod h _ _
@[to_additive]
theorem MonoidHom.coe_finsuppProd [Zero β] [MulOneClass N] [CommMonoid P] (f : α →₀ β)
(g : α → β → N →* P) : ⇑(f.prod g) = f.prod fun i fi => ⇑(g i fi) :=
MonoidHom.coe_finset_prod _ _
@[to_additive (attr := simp)]
theorem MonoidHom.finsuppProd_apply [Zero β] [MulOneClass N] [CommMonoid P] (f : α →₀ β)
(g : α → β → N →* P) (x : N) : f.prod g x = f.prod fun i fi => g i fi x :=
MonoidHom.finset_prod_apply _ _ _
namespace Finsupp
theorem single_multiset_sum [AddCommMonoid M] (s : Multiset M) (a : α) :
single a s.sum = (s.map (single a)).sum :=
Multiset.induction_on s (single_zero _) fun a s ih => by
rw [Multiset.sum_cons, single_add, ih, Multiset.map_cons, Multiset.sum_cons]
theorem single_finset_sum [AddCommMonoid M] (s : Finset ι) (f : ι → M) (a : α) :
single a (∑ b ∈ s, f b) = ∑ b ∈ s, single a (f b) := by
trans
· apply single_multiset_sum
· rw [Multiset.map_map]
rfl
theorem single_sum [Zero M] [AddCommMonoid N] (s : ι →₀ M) (f : ι → M → N) (a : α) :
single a (s.sum f) = s.sum fun d c => single a (f d c) :=
single_finset_sum _ _ _
@[to_additive]
theorem prod_neg_index [SubtractionMonoid G] [CommMonoid M] {g : α →₀ G} {h : α → G → M}
(h0 : ∀ a, h a 0 = 1) : (-g).prod h = g.prod fun a b => h a (-b) :=
prod_mapRange_index h0
theorem finset_sum_apply [AddCommMonoid N] (S : Finset ι) (f : ι → α →₀ N) (a : α) :
(∑ i ∈ S, f i) a = ∑ i ∈ S, f i a :=
map_sum (applyAddHom a) _ _
@[simp]
theorem sum_apply [Zero M] [AddCommMonoid N] {f : α →₀ M} {g : α → M → β →₀ N} {a₂ : β} :
(f.sum g) a₂ = f.sum fun a₁ b => g a₁ b a₂ :=
finset_sum_apply _ _ _
@[simp, norm_cast] theorem coe_finset_sum [AddCommMonoid N] (S : Finset ι) (f : ι → α →₀ N) :
⇑(∑ i ∈ S, f i) = ∑ i ∈ S, ⇑(f i) :=
map_sum (coeFnAddHom : (α →₀ N) →+ _) _ _
@[simp, norm_cast] theorem coe_sum [Zero M] [AddCommMonoid N] (f : α →₀ M) (g : α → M → β →₀ N) :
⇑(f.sum g) = f.sum fun a₁ b => ⇑(g a₁ b) :=
coe_finset_sum _ _
theorem support_sum [DecidableEq β] [Zero M] [AddCommMonoid N] {f : α →₀ M} {g : α → M → β →₀ N} :
(f.sum g).support ⊆ f.support.biUnion fun a => (g a (f a)).support := by
have : ∀ c, (f.sum fun a b => g a b c) ≠ 0 → ∃ a, f a ≠ 0 ∧ ¬(g a (f a)) c = 0 := fun a₁ h =>
let ⟨a, ha, ne⟩ := Finset.exists_ne_zero_of_sum_ne_zero h
⟨a, mem_support_iff.mp ha, ne⟩
simpa only [Finset.subset_iff, mem_support_iff, Finset.mem_biUnion, sum_apply, exists_prop]
theorem support_finset_sum [DecidableEq β] [AddCommMonoid M] {s : Finset α} {f : α → β →₀ M} :
(Finset.sum s f).support ⊆ s.biUnion fun x => (f x).support := by
rw [← Finset.sup_eq_biUnion]
induction s using Finset.cons_induction_on with
| empty => rfl
| cons _ _ _ ih =>
rw [Finset.sum_cons, Finset.sup_cons]
exact support_add.trans (Finset.union_subset_union (Finset.Subset.refl _) ih)
@[simp]
theorem sum_zero [Zero M] [AddCommMonoid N] {f : α →₀ M} : (f.sum fun _ _ => (0 : N)) = 0 :=
Finset.sum_const_zero
theorem sum_eq_one_iff (d : α →₀ ℕ) : sum d (fun _ n ↦ n) = 1 ↔ ∃ a, d = single a 1 := by
classical
refine ⟨fun h1 ↦ ?_, ?_⟩
· have hd0 : d ≠ 0 := (by simp [·] at h1)
obtain ⟨a, ha⟩ := ne_iff.mp hd0
obtain ⟨hda, hda'⟩ : d a = 1 ∧ ∀ i ≠ a, d i = 0 := by
rw [← add_sum_erase' _ a _ (fun _ ↦ rfl), Nat.add_eq_one_iff, or_iff_not_imp_left] at h1
simp_all +contextual [sum, support_erase, sum_eq_zero_iff, mem_erase, erase_ne]
use a
ext b
by_cases hb : b = a
· rw [hb, single_eq_same, hda]
· simpa only [single_eq_of_ne hb] using hda' b hb
· rintro ⟨a, rfl⟩
rw [sum_eq_single a ?_ (fun _ ↦ rfl), single_eq_same]
exact fun _ _ hba ↦ single_eq_of_ne hba
@[to_additive (attr := simp)]
theorem prod_mul [Zero M] [CommMonoid N] {f : α →₀ M} {h₁ h₂ : α → M → N} :
(f.prod fun a b => h₁ a b * h₂ a b) = f.prod h₁ * f.prod h₂ :=
Finset.prod_mul_distrib
@[to_additive (attr := simp)]
theorem prod_inv [Zero M] [CommGroup G] {f : α →₀ M} {h : α → M → G} :
(f.prod fun a b => (h a b)⁻¹) = (f.prod h)⁻¹ :=
(map_prod (MonoidHom.id G)⁻¹ _ _).symm
@[simp]
theorem sum_sub [Zero M] [SubtractionCommMonoid G] {f : α →₀ M} {h₁ h₂ : α → M → G} :
(f.sum fun a b => h₁ a b - h₂ a b) = f.sum h₁ - f.sum h₂ :=
Finset.sum_sub_distrib ..
/-- Taking the product under `h` is an additive-to-multiplicative homomorphism of finsupps,
if `h` is an additive-to-multiplicative homomorphism on the support.
This is a more general version of `Finsupp.prod_add_index'`; the latter has simpler hypotheses. -/
@[to_additive
/-- Taking the product under `h` is an additive homomorphism of finsupps, if `h` is an
additive homomorphism on the support. This is a more general version of
`Finsupp.sum_add_index'`; the latter has simpler hypotheses. -/]
theorem prod_add_index [DecidableEq α] [AddZeroClass M] [CommMonoid N] {f g : α →₀ M}
{h : α → M → N} (h_zero : ∀ a ∈ f.support ∪ g.support, h a 0 = 1)
(h_add : ∀ a ∈ f.support ∪ g.support, ∀ (b₁ b₂), h a (b₁ + b₂) = h a b₁ * h a b₂) :
(f + g).prod h = f.prod h * g.prod h := by
rw [Finsupp.prod_of_support_subset f subset_union_left h h_zero,
Finsupp.prod_of_support_subset g subset_union_right h h_zero, ←
Finset.prod_mul_distrib, Finsupp.prod_of_support_subset (f + g) Finsupp.support_add h h_zero]
exact Finset.prod_congr rfl fun x hx => by apply h_add x hx
/-- Taking the product under `h` is an additive-to-multiplicative homomorphism of finsupps,
if `h` is an additive-to-multiplicative homomorphism.
This is a more specialized version of `Finsupp.prod_add_index` with simpler hypotheses. -/
@[to_additive
/-- Taking the sum under `h` is an additive homomorphism of finsupps,if `h` is an additive
homomorphism. This is a more specific version of `Finsupp.sum_add_index` with simpler
hypotheses. -/]
theorem prod_add_index' [AddZeroClass M] [CommMonoid N] {f g : α →₀ M} {h : α → M → N}
(h_zero : ∀ a, h a 0 = 1) (h_add : ∀ a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂) :
(f + g).prod h = f.prod h * g.prod h := by
classical exact prod_add_index (fun a _ => h_zero a) fun a _ => h_add a
@[simp]
theorem sum_hom_add_index [AddZeroClass M] [AddCommMonoid N] {f g : α →₀ M} (h : α → M →+ N) :
((f + g).sum fun x => h x) = (f.sum fun x => h x) + g.sum fun x => h x :=
sum_add_index' (fun a => (h a).map_zero) fun a => (h a).map_add
@[simp]
theorem prod_hom_add_index [AddZeroClass M] [CommMonoid N] {f g : α →₀ M}
(h : α → Multiplicative M →* N) :
((f + g).prod fun a b => h a (Multiplicative.ofAdd b)) =
(f.prod fun a b => h a (Multiplicative.ofAdd b)) *
g.prod fun a b => h a (Multiplicative.ofAdd b) :=
prod_add_index' (fun a => (h a).map_one) fun a => (h a).map_mul
/-- The canonical isomorphism between families of additive monoid homomorphisms `α → (M →+ N)`
and monoid homomorphisms `(α →₀ M) →+ N`. -/
def liftAddHom [AddZeroClass M] [AddCommMonoid N] : (α → M →+ N) ≃+ ((α →₀ M) →+ N) where
toFun F :=
{ toFun := fun f ↦ f.sum fun x ↦ F x
map_zero' := Finset.sum_empty
map_add' := fun _ _ => sum_add_index' (fun x => (F x).map_zero) fun x => (F x).map_add }
invFun F x := F.comp (singleAddHom x)
left_inv F := by
ext
simp [singleAddHom]
right_inv F := by
ext
simp [singleAddHom, AddMonoidHom.comp, Function.comp_def]
map_add' F G := by
ext x
exact sum_add
@[simp]
theorem liftAddHom_apply [AddZeroClass M] [AddCommMonoid N] (F : α → M →+ N) (f : α →₀ M) :
(liftAddHom (α := α) (M := M) (N := N)) F f = f.sum fun x => F x :=
rfl
@[simp]
theorem liftAddHom_symm_apply [AddZeroClass M] [AddCommMonoid N] (F : (α →₀ M) →+ N) (x : α) :
(liftAddHom (α := α) (M := M) (N := N)).symm F x = F.comp (singleAddHom x) :=
rfl
theorem liftAddHom_symm_apply_apply [AddZeroClass M] [AddCommMonoid N] (F : (α →₀ M) →+ N) (x : α)
(y : M) : (liftAddHom (α := α) (M := M) (N := N)).symm F x y = F (single x y) :=
rfl
@[simp]
theorem liftAddHom_singleAddHom [AddCommMonoid M] :
(liftAddHom (α := α) (M := M) (N := α →₀ M)) (singleAddHom : α → M →+ α →₀ M) =
AddMonoidHom.id _ :=
liftAddHom.toEquiv.apply_eq_iff_eq_symm_apply.2 rfl
@[simp]
theorem sum_single [AddCommMonoid M] (f : α →₀ M) : f.sum single = f :=
DFunLike.congr_fun liftAddHom_singleAddHom f
/-- The `Finsupp` version of `Finset.univ_sum_single` -/
@[simp]
theorem univ_sum_single [Fintype α] [AddCommMonoid M] (f : α →₀ M) :
∑ a : α, single a (f a) = f := by
classical
refine DFunLike.coe_injective ?_
simp_rw [coe_finset_sum, single_eq_pi_single, Finset.univ_sum_single]
@[simp]
theorem univ_sum_single_apply [AddCommMonoid M] [Fintype α] (i : α) (m : M) :
∑ j : α, single i m j = m := by
classical rw [single, coe_mk, Finset.sum_pi_single']
simp
@[simp]
theorem univ_sum_single_apply' [AddCommMonoid M] [Fintype α] (i : α) (m : M) :
∑ j : α, single j m i = m := by
simp_rw [single, coe_mk]
classical rw [Finset.sum_pi_single]
simp
lemma sum_single_add_single (f₁ f₂ : ι) (g₁ g₂ : A) (F : ι → A → B) (H : f₁ ≠ f₂)
(HF : ∀ f, F f 0 = 0) :
sum (single f₁ g₁ + single f₂ g₂) F = F f₁ g₁ + F f₂ g₂ := by
classical
simp [sum_of_support_subset _ support_single_add_single_subset, single_apply, H, HF, H.symm]
theorem equivFunOnFinite_symm_eq_sum [Fintype α] [AddCommMonoid M] (f : α → M) :
equivFunOnFinite.symm f = ∑ a, single a (f a) :=
(univ_sum_single _).symm
theorem coe_univ_sum_single [Fintype α] [AddCommMonoid M] (f : α → M) :
⇑(∑ a : α, single a (f a)) = f :=
congrArg _ (equivFunOnFinite_symm_eq_sum f).symm
theorem equivFunOnFinite_symm_sum [Fintype α] [AddCommMonoid M] (f : α → M) :
((equivFunOnFinite.symm f).sum fun _ n ↦ n) = ∑ a, f a := by
rw [equivFunOnFinite_symm_eq_sum, sum_fintype _ _ fun _ ↦ rfl, coe_univ_sum_single]
theorem liftAddHom_apply_single [AddZeroClass M] [AddCommMonoid N] (f : α → M →+ N) (a : α)
(b : M) : (liftAddHom (α := α) (M := M) (N := N)) f (single a b) = f a b :=
sum_single_index (f a).map_zero
@[simp]
theorem liftAddHom_comp_single [AddZeroClass M] [AddCommMonoid N] (f : α → M →+ N) (a : α) :
((liftAddHom (α := α) (M := M) (N := N)) f).comp (singleAddHom a) = f a :=
AddMonoidHom.ext fun b => liftAddHom_apply_single f a b
theorem comp_liftAddHom [AddZeroClass M] [AddCommMonoid N] [AddCommMonoid P] (g : N →+ P)
(f : α → M →+ N) :
g.comp ((liftAddHom (α := α) (M := M) (N := N)) f) =
(liftAddHom (α := α) (M := M) (N := P)) fun a => g.comp (f a) :=
liftAddHom.symm_apply_eq.1 <|
funext fun a => by
rw [liftAddHom_symm_apply, AddMonoidHom.comp_assoc, liftAddHom_comp_single]
theorem sum_sub_index [AddGroup β] [AddCommGroup γ] {f g : α →₀ β} {h : α → β → γ}
(h_sub : ∀ a b₁ b₂, h a (b₁ - b₂) = h a b₁ - h a b₂) : (f - g).sum h = f.sum h - g.sum h :=
((liftAddHom (α := α) (M := β) (N := γ)) fun a =>
AddMonoidHom.ofMapSub (h a) (h_sub a)).map_sub f g
@[to_additive]
theorem prod_embDomain [Zero M] [CommMonoid N] {v : α →₀ M} {f : α ↪ β} {g : β → M → N} :
(v.embDomain f).prod g = v.prod fun a b => g (f a) b := by
rw [prod, prod, support_embDomain, Finset.prod_map]
simp_rw [embDomain_apply]
@[to_additive]
theorem prod_finset_sum_index [AddCommMonoid M] [CommMonoid N] {s : Finset ι} {g : ι → α →₀ M}
{h : α → M → N} (h_zero : ∀ a, h a 0 = 1) (h_add : ∀ a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂) :
(∏ i ∈ s, (g i).prod h) = (∑ i ∈ s, g i).prod h :=
Finset.cons_induction_on s rfl fun a s has ih => by
rw [prod_cons, ih, sum_cons, prod_add_index' h_zero h_add]
@[to_additive]
theorem prod_sum_index [Zero M] [AddCommMonoid N] [CommMonoid P] {f : α →₀ M}
{g : α → M → β →₀ N} {h : β → N → P} (h_zero : ∀ a, h a 0 = 1)
(h_add : ∀ a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂) :
(f.sum g).prod h = f.prod fun a b => (g a b).prod h :=
(prod_finset_sum_index h_zero h_add).symm
theorem multiset_sum_sum_index [AddCommMonoid M] [AddCommMonoid N] (f : Multiset (α →₀ M))
(h : α → M → N) (h₀ : ∀ a, h a 0 = 0)
(h₁ : ∀ (a : α) (b₁ b₂ : M), h a (b₁ + b₂) = h a b₁ + h a b₂) :
f.sum.sum h = (f.map fun g : α →₀ M => g.sum h).sum :=
Multiset.induction_on f rfl fun a s ih => by
rw [Multiset.sum_cons, Multiset.map_cons, Multiset.sum_cons, sum_add_index' h₀ h₁, ih]
theorem support_sum_eq_biUnion {α : Type*} {ι : Type*} {M : Type*} [DecidableEq α]
[AddCommMonoid M] {g : ι → α →₀ M} (s : Finset ι)
(h : ∀ i₁ i₂, i₁ ≠ i₂ → Disjoint (g i₁).support (g i₂).support) :
(∑ i ∈ s, g i).support = s.biUnion fun i => (g i).support := by
classical
refine Finset.induction_on s ?_ ?_
· simp
· intro i s hi
simp only [hi, sum_insert, not_false_iff, biUnion_insert]
intro hs
rw [Finsupp.support_add_eq, hs]
rw [hs, Finset.disjoint_biUnion_right]
intro j hj
exact h _ _ (ne_of_mem_of_not_mem hj hi).symm
theorem multiset_map_sum [Zero M] {f : α →₀ M} {m : β → γ} {h : α → M → Multiset β} :
Multiset.map m (f.sum h) = f.sum fun a b => (h a b).map m :=
map_sum (Multiset.mapAddMonoidHom m) _ f.support
theorem multiset_sum_sum [Zero M] [AddCommMonoid N] {f : α →₀ M} {h : α → M → Multiset N} :
Multiset.sum (f.sum h) = f.sum fun a b => Multiset.sum (h a b) :=
map_sum Multiset.sumAddMonoidHom _ f.support
/-- For disjoint `f1` and `f2`, and function `g`, the product of the products of `g`
over `f1` and `f2` equals the product of `g` over `f1 + f2` -/
@[to_additive
/-- For disjoint `f1` and `f2`, and function `g`, the sum of the sums of `g`
over `f1` and `f2` equals the sum of `g` over `f1 + f2` -/]
theorem prod_add_index_of_disjoint [AddCommMonoid M] {f1 f2 : α →₀ M}
(hd : Disjoint f1.support f2.support) {β : Type*} [CommMonoid β] (g : α → M → β) :
(f1 + f2).prod g = f1.prod g * f2.prod g := by
have :
∀ {f1 f2 : α →₀ M},
Disjoint f1.support f2.support → (∏ x ∈ f1.support, g x (f1 x + f2 x)) = f1.prod g :=
fun hd =>
Finset.prod_congr rfl fun x hx => by
simp only [notMem_support_iff.mp (disjoint_left.mp hd hx), add_zero]
classical simp_rw [← this hd, ← this hd.symm, add_comm (f2 _), Finsupp.prod, support_add_eq hd,
prod_union hd, add_apply]
theorem prod_dvd_prod_of_subset_of_dvd [Zero M] [CommMonoid N] {f1 f2 : α →₀ M}
{g1 g2 : α → M → N} (h1 : f1.support ⊆ f2.support)
(h2 : ∀ a : α, a ∈ f1.support → g1 a (f1 a) ∣ g2 a (f2 a)) : f1.prod g1 ∣ f2.prod g2 := by
classical
simp only [Finsupp.prod]
rw [← sdiff_union_of_subset h1, prod_union sdiff_disjoint]
apply dvd_mul_of_dvd_right
apply prod_dvd_prod_of_dvd
exact h2
lemma indicator_eq_sum_attach_single [AddCommMonoid M] {s : Finset α} (f : ∀ a ∈ s, M) :
indicator s f = ∑ x ∈ s.attach, single ↑x (f x x.2) := by
rw [← sum_single (indicator s f), sum, sum_subset (support_indicator_subset _ _), ← sum_attach]
· refine Finset.sum_congr rfl (fun _ _ => ?_)
rw [indicator_of_mem]
· intro i _ hi
rw [notMem_support_iff.mp hi, single_zero]
lemma indicator_eq_sum_single [AddCommMonoid M] (s : Finset α) (f : α → M) :
indicator s (fun x _ ↦ f x) = ∑ x ∈ s, single x (f x) :=
(indicator_eq_sum_attach_single _).trans <| sum_attach _ fun x ↦ single x (f x)
@[to_additive (attr := simp)]
lemma prod_indicator_index_eq_prod_attach [Zero M] [CommMonoid N]
{s : Finset α} (f : ∀ a ∈ s, M) {h : α → M → N} (h_zero : ∀ a ∈ s, h a 0 = 1) :
(indicator s f).prod h = ∏ x ∈ s.attach, h ↑x (f x x.2) := by
rw [prod_of_support_subset _ (support_indicator_subset _ _) h h_zero, ← prod_attach]
refine Finset.prod_congr rfl (fun _ _ => ?_)
rw [indicator_of_mem]
@[to_additive (attr := simp)]
lemma prod_attach_index [CommMonoid N] {s : Finset α} (f : α → M) {h : α → M → N} :
∏ x ∈ s.attach, h x (f x) = ∏ x ∈ s, h x (f x) :=
prod_attach _ fun x ↦ h x (f x)
@[to_additive]
lemma prod_indicator_index [Zero M] [CommMonoid N]
{s : Finset α} (f : α → M) {h : α → M → N} (h_zero : ∀ a ∈ s, h a 0 = 1) :
(indicator s (fun x _ ↦ f x)).prod h = ∏ x ∈ s, h x (f x) := by
simp +contextual [h_zero]
@[to_additive]
lemma prod_mul_eq_prod_mul_of_exists [Zero M] [CommMonoid N]
{f : α →₀ M} {g : α → M → N} {n₁ n₂ : N}
(a : α) (ha : a ∈ f.support)
(h : g a (f a) * n₁ = g a (f a) * n₂) :
f.prod g * n₁ = f.prod g * n₂ := by
classical
exact Finset.prod_mul_eq_prod_mul_of_exists a ha h
end Finsupp
theorem Finset.sum_apply' : (∑ k ∈ s, f k) i = ∑ k ∈ s, f k i :=
map_sum (Finsupp.applyAddHom i) f s
theorem Finsupp.sum_apply' : g.sum k x = g.sum fun i b => k i b x :=
Finset.sum_apply _ _ _
/-- Version of `Finsupp.sum_apply'` that applies in large generality to linear combinations
of functions in any `FunLike` type on which addition is defined pointwise.
At the time of writing Mathlib does not have a typeclass to express the condition
that addition on a `FunLike` type is pointwise; hence this is asserted via explicit hypotheses. -/
theorem Finsupp.sum_apply'' {A F : Type*} [AddZeroClass A] [AddCommMonoid F] [FunLike F γ B]
(g : ι →₀ A) (k : ι → A → F) (x : γ)
(hg0 : ∀ (i : ι), k i 0 = 0) (hgadd : ∀ (i : ι) (a₁ a₂ : A), k i (a₁ + a₂) = k i a₁ + k i a₂)
(h0 : (0 : F) x = 0) (hadd : ∀ (f g : F), (f + g : F) x = f x + g x) :
g.sum k x = g.sum (fun i a ↦ k i a x) := by
induction g using Finsupp.induction with
| zero => simp [h0]
| single_add i a f hf ha ih =>
rw [Finsupp.sum_add_index' hg0 hgadd, Finsupp.sum_add_index', hadd, ih]
· congr 1
rw [Finsupp.sum_single_index (hg0 i), Finsupp.sum_single_index]
simp [hg0, h0]
· simp [hg0, h0]
· simp [hgadd, hadd]
@[deprecated "use instead `sum_finset_sum_index` (with equality reversed)" (since := "2025-11-07")]
theorem Finsupp.sum_sum_index' (h0 : ∀ i, t i 0 = 0) (h1 : ∀ i x y, t i (x + y) = t i x + t i y) :
(∑ x ∈ s, f x).sum t = ∑ x ∈ s, (f x).sum t := (sum_finset_sum_index h0 h1).symm
section
variable [NonUnitalNonAssocSemiring R] [NonUnitalNonAssocSemiring S]
theorem Finsupp.sum_mul (b : S) (s : α →₀ R) {f : α → R → S} :
s.sum f * b = s.sum fun a c => f a c * b := by simp only [Finsupp.sum, Finset.sum_mul]
theorem Finsupp.mul_sum (b : S) (s : α →₀ R) {f : α → R → S} :
b * s.sum f = s.sum fun a c => b * f a c := by simp only [Finsupp.sum, Finset.mul_sum]
end
@[simp] lemma Multiset.card_finsuppSum [Zero M] (f : ι →₀ M) (g : ι → M → Multiset α) :
card (f.sum g) = f.sum fun i m ↦ card (g i m) := map_finsuppSum cardHom ..
namespace Nat
/-- If `0 : ℕ` is not in the support of `f : ℕ →₀ ℕ` then `0 < ∏ x ∈ f.support, x ^ (f x)`. -/
theorem prod_pow_pos_of_zero_notMem_support {f : ℕ →₀ ℕ} (nhf : 0 ∉ f.support) :
0 < f.prod (· ^ ·) :=
Nat.pos_iff_ne_zero.mpr <| Finset.prod_ne_zero_iff.mpr fun _ hf =>
pow_ne_zero _ fun H => by subst H; exact nhf hf
@[deprecated (since := "2025-05-23")]
alias prod_pow_pos_of_zero_not_mem_support := prod_pow_pos_of_zero_notMem_support
end Nat |
.lake/packages/mathlib/Mathlib/Algebra/BigOperators/Group/Finset/Indicator.lean | import Mathlib.Algebra.BigOperators.Group.Finset.Basic
import Mathlib.Algebra.Group.Indicator
import Mathlib.Order.CompleteLattice.Finset
/-!
# Interaction of big operators with indicator functions
-/
namespace Finset
variable {ι κ α β : Type*} [CommMonoid β]
open Set
/-- Consider a product of `g i (f i)` over a finset. Suppose `g` is a function such as
`n ↦ (· ^ n)`, which maps a second argument of `1` to `1`. Then if `f` is replaced by the
corresponding multiplicative indicator function, the finset may be replaced by a possibly larger
finset without changing the value of the product. -/
@[to_additive /-- Consider a sum of `g i (f i)` over a finset. Suppose `g` is a function such as
`n ↦ (n • ·)`, which maps a second argument of `0` to `0` (or a weighted sum of `f i * h i` or
`f i • h i`, where `f` gives the weights that are multiplied by some other function `h`). Then if
`f` is replaced by the corresponding indicator function, the finset may be replaced by a possibly
larger finset without changing the value of the sum. -/]
lemma prod_mulIndicator_subset_of_eq_one [One α] (f : ι → α) (g : ι → α → β) {s t : Finset ι}
(h : s ⊆ t) (hg : ∀ a, g a 1 = 1) :
∏ i ∈ t, g i (mulIndicator ↑s f i) = ∏ i ∈ s, g i (f i) := by
calc
_ = ∏ i ∈ s, g i (mulIndicator ↑s f i) := by rw [prod_subset h fun i _ hn ↦ by simp [hn, hg]]
_ = _ := prod_congr rfl fun i hi ↦ congr_arg _ <| mulIndicator_of_mem hi f
/-- Taking the product of an indicator function over a possibly larger finset is the same as
taking the original function over the original finset. -/
@[to_additive /-- Summing an indicator function over a possibly larger `Finset` is the same as
summing the original function over the original finset. -/]
lemma prod_mulIndicator_subset (f : ι → β) {s t : Finset ι} (h : s ⊆ t) :
∏ i ∈ t, mulIndicator (↑s) f i = ∏ i ∈ s, f i :=
prod_mulIndicator_subset_of_eq_one _ (fun _ ↦ id) h fun _ ↦ rfl
@[to_additive]
lemma prod_mulIndicator_eq_prod_filter (s : Finset ι) (f : ι → κ → β) (t : ι → Set κ) (g : ι → κ)
[DecidablePred fun i ↦ g i ∈ t i] :
∏ i ∈ s, mulIndicator (t i) (f i) (g i) = ∏ i ∈ s with g i ∈ t i, f i (g i) := by
refine (prod_filter_mul_prod_filter_not s (fun i ↦ g i ∈ t i) _).symm.trans <|
Eq.trans (congr_arg₂ (· * ·) ?_ ?_) (mul_one _)
· exact prod_congr rfl fun x hx ↦ mulIndicator_of_mem (mem_filter.1 hx).2 _
· exact prod_eq_one fun x hx ↦ mulIndicator_of_notMem (mem_filter.1 hx).2 _
@[to_additive]
lemma prod_mulIndicator_eq_prod_inter [DecidableEq ι] (s t : Finset ι) (f : ι → β) :
∏ i ∈ s, (t : Set ι).mulIndicator f i = ∏ i ∈ s ∩ t, f i := by
rw [← filter_mem_eq_inter, prod_mulIndicator_eq_prod_filter]; rfl
@[to_additive]
lemma mulIndicator_prod (s : Finset ι) (t : Set κ) (f : ι → κ → β) :
mulIndicator t (∏ i ∈ s, f i) = ∏ i ∈ s, mulIndicator t (f i) :=
map_prod (mulIndicatorHom _ _) _ _
@[to_additive]
lemma mulIndicator_biUnion (s : Finset ι) (t : ι → Set κ) {f : κ → β}
(hs : (s : Set ι).PairwiseDisjoint t) :
mulIndicator (⋃ i ∈ s, t i) f = fun a ↦ ∏ i ∈ s, mulIndicator (t i) f a := by
induction s using Finset.cons_induction with
| empty => simp
| cons i s hi ih =>
ext j
rw [coe_cons, Set.pairwiseDisjoint_insert_of_notMem (Finset.mem_coe.not.2 hi)] at hs
classical
rw [prod_cons, cons_eq_insert, set_biUnion_insert, mulIndicator_union_of_notMem_inter, ih hs.1]
exact (Set.disjoint_iff.mp (Set.disjoint_iUnion₂_right.mpr hs.2) ·)
@[to_additive]
lemma mulIndicator_biUnion_apply (s : Finset ι) (t : ι → Set κ) {f : κ → β}
(h : (s : Set ι).PairwiseDisjoint t) (x : κ) :
mulIndicator (⋃ i ∈ s, t i) f x = ∏ i ∈ s, mulIndicator (t i) f x := by
rw [mulIndicator_biUnion s t h]
end Finset |
.lake/packages/mathlib/Mathlib/Algebra/BigOperators/Group/Finset/Lemmas.lean | import Mathlib.Algebra.BigOperators.Group.Finset.Basic
import Mathlib.Algebra.Group.Even
import Mathlib.Algebra.Group.Pi.Lemmas
import Mathlib.Algebra.Notation.Support
/-!
# Miscellaneous lemmas on big operators
The lemmas in this file have been moved out of
`Mathlib/Algebra/BigOperators/Group/Finset/Basic.lean` to reduce its imports.
-/
variable {ι κ M N β : Type*}
@[to_additive]
theorem MonoidHom.coe_finset_prod [MulOneClass M] [CommMonoid N] (f : ι → M →* N) (s : Finset ι) :
⇑(∏ x ∈ s, f x) = ∏ x ∈ s, ⇑(f x) :=
map_prod (MonoidHom.coeFn M N) _ _
/-- See also `Finset.prod_apply`, with the same conclusion but with the weaker hypothesis
`f : α → M → N` -/
@[to_additive (attr := simp)
/-- See also `Finset.sum_apply`, with the same conclusion but with the weaker hypothesis
`f : α → M → N` -/]
theorem MonoidHom.finset_prod_apply [MulOneClass M] [CommMonoid N] (f : ι → M →* N) (s : Finset ι)
(b : M) : (∏ x ∈ s, f x) b = ∏ x ∈ s, f x b :=
map_prod (MonoidHom.eval b) _ _
namespace Finset
variable [CommMonoid M]
open Function in
@[to_additive]
lemma mulSupport_prod (s : Finset ι) (f : ι → κ → M) :
mulSupport (fun x ↦ ∏ i ∈ s, f i x) ⊆ ⋃ i ∈ s, mulSupport (f i) := by
simp only [mulSupport_subset_iff', Set.mem_iUnion, not_exists, notMem_mulSupport]
exact fun x ↦ prod_eq_one
@[to_additive]
lemma isSquare_prod {s : Finset ι} (f : ι → M) (h : ∀ c ∈ s, IsSquare (f c)) :
IsSquare (∏ i ∈ s, f i) := by
rw [isSquare_iff_exists_sq]
use (∏ (x : s), ((isSquare_iff_exists_sq _).mp (h _ x.2)).choose)
rw [@sq, ← Finset.prod_mul_distrib, ← Finset.prod_coe_sort]
congr
ext i
rw [← @sq]
exact ((isSquare_iff_exists_sq _).mp (h _ i.2)).choose_spec
end Finset |
.lake/packages/mathlib/Mathlib/Algebra/BigOperators/Group/Finset/Piecewise.lean | import Mathlib.Algebra.BigOperators.Group.Finset.Basic
import Mathlib.Data.Finset.Piecewise
/-!
# Interaction of big operators with piecewise functions
This file proves lemmas on the sum and product of piecewise functions, including `ite` and `dite`.
-/
variable {ι κ M β γ : Type*} {s : Finset ι}
namespace Finset
section CommMonoid
variable [CommMonoid M]
@[to_additive]
theorem prod_apply_dite {p : ι → Prop} [DecidablePred p]
[DecidablePred fun x => ¬p x] (f : ∀ x : ι, p x → γ) (g : ∀ x : ι, ¬p x → γ) (h : γ → M) :
(∏ x ∈ s, h (if hx : p x then f x hx else g x hx)) =
(∏ x : {x ∈ s | p x}, h (f x.1 <| by simpa using (mem_filter.mp x.2).2)) *
∏ x : {x ∈ s | ¬p x}, h (g x.1 <| by simpa using (mem_filter.mp x.2).2) :=
calc
(∏ x ∈ s, h (if hx : p x then f x hx else g x hx)) =
(∏ x ∈ s with p x, h (if hx : p x then f x hx else g x hx)) *
∏ x ∈ s with ¬p x, h (if hx : p x then f x hx else g x hx) :=
(prod_filter_mul_prod_filter_not s p _).symm
_ = (∏ x : {x ∈ s | p x}, h (if hx : p x.1 then f x.1 hx else g x.1 hx)) *
∏ x : {x ∈ s | ¬p x}, h (if hx : p x.1 then f x.1 hx else g x.1 hx) :=
congr_arg₂ _ (prod_attach _ _).symm (prod_attach _ _).symm
_ = (∏ x : {x ∈ s | p x}, h (f x.1 <| by simpa using (mem_filter.mp x.2).2)) *
∏ x : {x ∈ s | ¬p x}, h (g x.1 <| by simpa using (mem_filter.mp x.2).2) :=
congr_arg₂ _ (prod_congr rfl fun x _hx ↦
congr_arg h (dif_pos <| by simpa using (mem_filter.mp x.2).2))
(prod_congr rfl fun x _hx => congr_arg h (dif_neg <| by simpa using (mem_filter.mp x.2).2))
@[to_additive]
theorem prod_apply_ite {s : Finset ι} {p : ι → Prop} [DecidablePred p] (f g : ι → γ)
(h : γ → M) :
(∏ x ∈ s, h (if p x then f x else g x)) =
(∏ x ∈ s with p x, h (f x)) * ∏ x ∈ s with ¬p x, h (g x) :=
(prod_apply_dite _ _ _).trans <| congr_arg₂ _ (prod_attach _ (h ∘ f)) (prod_attach _ (h ∘ g))
@[to_additive]
theorem prod_dite {s : Finset ι} {p : ι → Prop} [DecidablePred p] (f : ∀ x : ι, p x → M)
(g : ∀ x : ι, ¬p x → M) :
∏ x ∈ s, (if hx : p x then f x hx else g x hx) =
(∏ x : {x ∈ s | p x}, f x.1 (by simpa using (mem_filter.mp x.2).2)) *
∏ x : {x ∈ s | ¬p x}, g x.1 (by simpa using (mem_filter.mp x.2).2) := by
simp [prod_apply_dite _ _ fun x => x]
@[to_additive]
theorem prod_ite {s : Finset ι} {p : ι → Prop} [DecidablePred p] (f g : ι → M) :
∏ x ∈ s, (if p x then f x else g x) = (∏ x ∈ s with p x, f x) * ∏ x ∈ s with ¬p x, g x := by
simp [prod_apply_ite _ _ fun x => x]
@[to_additive]
lemma prod_dite_of_false {p : ι → Prop} [DecidablePred p] (h : ∀ i ∈ s, ¬ p i)
(f : ∀ i, p i → M) (g : ∀ i, ¬ p i → M) :
∏ i ∈ s, (if hi : p i then f i hi else g i hi) = ∏ i : s, g i.1 (h _ i.2) := by
refine prod_bij' (fun x hx => ⟨x, hx⟩) (fun x _ ↦ x) ?_ ?_ ?_ ?_ ?_ <;> aesop
@[to_additive]
lemma prod_ite_of_false {p : ι → Prop} [DecidablePred p] (h : ∀ x ∈ s, ¬p x) (f g : ι → M) :
∏ x ∈ s, (if p x then f x else g x) = ∏ x ∈ s, g x :=
(prod_dite_of_false h _ _).trans (prod_attach _ _)
@[to_additive]
lemma prod_dite_of_true {p : ι → Prop} [DecidablePred p] (h : ∀ i ∈ s, p i) (f : ∀ i, p i → M)
(g : ∀ i, ¬ p i → M) :
∏ i ∈ s, (if hi : p i then f i hi else g i hi) = ∏ i : s, f i.1 (h _ i.2) := by
refine prod_bij' (fun x hx => ⟨x, hx⟩) (fun x _ ↦ x) ?_ ?_ ?_ ?_ ?_ <;> aesop
@[to_additive]
lemma prod_ite_of_true {p : ι → Prop} [DecidablePred p] (h : ∀ x ∈ s, p x) (f g : ι → M) :
∏ x ∈ s, (if p x then f x else g x) = ∏ x ∈ s, f x :=
(prod_dite_of_true h _ _).trans (prod_attach _ _)
@[to_additive]
theorem prod_apply_ite_of_false {p : ι → Prop} [DecidablePred p] (f g : ι → γ) (k : γ → M)
(h : ∀ x ∈ s, ¬p x) : (∏ x ∈ s, k (if p x then f x else g x)) = ∏ x ∈ s, k (g x) := by
simp_rw [apply_ite k]
exact prod_ite_of_false h _ _
@[to_additive]
theorem prod_apply_ite_of_true {p : ι → Prop} [DecidablePred p] (f g : ι → γ) (k : γ → M)
(h : ∀ x ∈ s, p x) : (∏ x ∈ s, k (if p x then f x else g x)) = ∏ x ∈ s, k (f x) := by
simp_rw [apply_ite k]
exact prod_ite_of_true h _ _
@[to_additive (attr := simp)]
theorem prod_ite_mem [DecidableEq ι] (s t : Finset ι) (f : ι → M) :
∏ i ∈ s, (if i ∈ t then f i else 1) = ∏ i ∈ s ∩ t, f i := by
rw [← Finset.prod_filter, Finset.filter_mem_eq_inter]
@[to_additive]
lemma prod_attach_eq_prod_dite [Fintype ι] (s : Finset ι) (f : s → M) [DecidablePred (· ∈ s)] :
∏ i ∈ s.attach, f i = ∏ i, if h : i ∈ s then f ⟨i, h⟩ else 1 := by
rw [Finset.prod_dite, Finset.univ_eq_attach, Finset.prod_const_one, mul_one]
congr
· simp
· ext; simp
· apply Function.hfunext <;> simp +contextual [Subtype.heq_iff_coe_eq]
@[to_additive (attr := simp)]
theorem prod_dite_eq [DecidableEq ι] (s : Finset ι) (a : ι) (b : ∀ x : ι, a = x → M) :
∏ x ∈ s, (if h : a = x then b x h else 1) = ite (a ∈ s) (b a rfl) 1 := by
split_ifs with h
· rw [Finset.prod_eq_single a, dif_pos rfl]
· intro _ _ h
rw [dif_neg]
exact h.symm
· simp [h]
· rw [Finset.prod_eq_one]
grind
@[to_additive (attr := simp)]
theorem prod_dite_eq' [DecidableEq ι] (s : Finset ι) (a : ι) (b : ∀ x : ι, x = a → M) :
∏ x ∈ s, (if h : x = a then b x h else 1) = ite (a ∈ s) (b a rfl) 1 := by
split_ifs with h
· rw [Finset.prod_eq_single a, dif_pos rfl]
· intro _ _ h
rw [dif_neg]
exact h
· simp [h]
· rw [Finset.prod_eq_one]
grind
@[to_additive (attr := simp)]
theorem prod_ite_eq [DecidableEq ι] (s : Finset ι) (a : ι) (b : ι → M) :
(∏ x ∈ s, ite (a = x) (b x) 1) = ite (a ∈ s) (b a) 1 :=
prod_dite_eq s a fun x _ => b x
/-- A product taken over a conditional whose condition is an equality test on the index and whose
alternative is `1` has value either the term at that index or `1`.
The difference with `Finset.prod_ite_eq` is that the arguments to `Eq` are swapped. -/
@[to_additive (attr := simp) /-- A sum taken over a conditional whose condition is an equality
test on the index and whose alternative is `0` has value either the term at that index or `0`.
The difference with `Finset.sum_ite_eq` is that the arguments to `Eq` are swapped. -/]
theorem prod_ite_eq' [DecidableEq ι] (s : Finset ι) (a : ι) (b : ι → M) :
(∏ x ∈ s, ite (x = a) (b x) 1) = ite (a ∈ s) (b a) 1 :=
prod_dite_eq' s a fun x _ => b x
@[to_additive]
theorem prod_ite_eq_of_mem [DecidableEq ι] (s : Finset ι) (a : ι) (b : ι → M) (h : a ∈ s) :
(∏ x ∈ s, if a = x then b x else 1) = b a := by
simp only [prod_ite_eq, if_pos h]
/-- The difference with `Finset.prod_ite_eq_of_mem` is that the arguments to `Eq` are swapped. -/
@[to_additive]
theorem prod_ite_eq_of_mem' [DecidableEq ι] (s : Finset ι) (a : ι) (b : ι → M) (h : a ∈ s) :
(∏ x ∈ s, if x = a then b x else 1) = b a := by
simp only [prod_ite_eq', if_pos h]
@[to_additive (attr := simp)]
theorem prod_pi_mulSingle' [DecidableEq ι] (a : ι) (x : M) (s : Finset ι) :
∏ a' ∈ s, Pi.mulSingle a x a' = if a ∈ s then x else 1 :=
prod_dite_eq' _ _ _
@[to_additive (attr := simp)]
theorem prod_pi_mulSingle {M : ι → Type*} [DecidableEq ι] [∀ a, CommMonoid (M a)] (a : ι)
(f : ∀ a, M a) (s : Finset ι) :
(∏ a' ∈ s, Pi.mulSingle a' (f a') a) = if a ∈ s then f a else 1 :=
prod_dite_eq _ _ _
@[to_additive]
theorem prod_piecewise [DecidableEq ι] (s t : Finset ι) (f g : ι → M) :
(∏ x ∈ s, (t.piecewise f g) x) = (∏ x ∈ s ∩ t, f x) * ∏ x ∈ s \ t, g x := by
simp only [piecewise]
rw [prod_ite, filter_mem_eq_inter, ← sdiff_eq_filter]
@[to_additive]
theorem prod_inter_mul_prod_diff [DecidableEq ι] (s t : Finset ι) (f : ι → M) :
(∏ x ∈ s ∩ t, f x) * ∏ x ∈ s \ t, f x = ∏ x ∈ s, f x := by
convert (s.prod_piecewise t f f).symm
simp +unfoldPartialApp [Finset.piecewise]
@[to_additive]
theorem prod_eq_mul_prod_diff_singleton [DecidableEq ι] {s : Finset ι} {i : ι} (h : i ∈ s)
(f : ι → M) : ∏ x ∈ s, f x = f i * ∏ x ∈ s \ {i}, f x := by
convert (s.prod_inter_mul_prod_diff {i} f).symm
simp [h]
@[to_additive]
theorem prod_eq_prod_diff_singleton_mul [DecidableEq ι] {s : Finset ι} {i : ι} (h : i ∈ s)
(f : ι → M) : ∏ x ∈ s, f x = (∏ x ∈ s \ {i}, f x) * f i := by
rw [prod_eq_mul_prod_diff_singleton h, mul_comm]
@[to_additive]
theorem _root_.Fintype.prod_eq_mul_prod_compl [DecidableEq ι] [Fintype ι] (a : ι) (f : ι → M) :
∏ i, f i = f a * ∏ i ∈ {a}ᶜ, f i :=
prod_eq_mul_prod_diff_singleton (mem_univ a) f
@[to_additive]
theorem _root_.Fintype.prod_eq_prod_compl_mul [DecidableEq ι] [Fintype ι] (a : ι) (f : ι → M) :
∏ i, f i = (∏ i ∈ {a}ᶜ, f i) * f a :=
prod_eq_prod_diff_singleton_mul (mem_univ a) f
theorem dvd_prod_of_mem (f : ι → M) {a : ι} {s : Finset ι} (ha : a ∈ s) : f a ∣ ∏ i ∈ s, f i := by
classical
rw [Finset.prod_eq_mul_prod_diff_singleton ha]
exact dvd_mul_right _ _
@[to_additive]
theorem prod_update_of_notMem [DecidableEq ι] {s : Finset ι} {i : ι} (h : i ∉ s) (f : ι → M)
(b : M) : ∏ x ∈ s, Function.update f i b x = ∏ x ∈ s, f x := by
apply prod_congr rfl
intro j hj
have : j ≠ i := by
rintro rfl
exact h hj
simp [this]
@[deprecated (since := "2025-05-23")] alias sum_update_of_not_mem := sum_update_of_notMem
@[to_additive existing, deprecated (since := "2025-05-23")]
alias prod_update_of_not_mem := prod_update_of_notMem
@[to_additive]
theorem prod_update_of_mem [DecidableEq ι] {s : Finset ι} {i : ι} (h : i ∈ s) (f : ι → M) (b : M) :
∏ x ∈ s, Function.update f i b x = b * ∏ x ∈ s \ singleton i, f x := by
rw [update_eq_piecewise, prod_piecewise]
simp [h]
/-- See also `Finset.prod_ite_zero`. -/
@[to_additive /-- See also `Finset.sum_boole`. -/]
theorem prod_ite_one (s : Finset ι) (p : ι → Prop) [DecidablePred p]
(h : ∀ i ∈ s, ∀ j ∈ s, p i → p j → i = j) (a : M) :
∏ i ∈ s, ite (p i) a 1 = ite (∃ i ∈ s, p i) a 1 := by
split_ifs with h
· obtain ⟨i, hi, hpi⟩ := h
rw [prod_eq_single_of_mem _ hi, if_pos hpi]
exact fun j hj hji ↦ if_neg fun hpj ↦ hji <| h _ hj _ hi hpj hpi
· push_neg at h
rw [prod_eq_one]
exact fun i hi => if_neg (h i hi)
@[to_additive sum_boole_nsmul]
theorem prod_pow_boole [DecidableEq ι] (s : Finset ι) (f : ι → M) (a : ι) :
(∏ x ∈ s, f x ^ ite (a = x) 1 0) = ite (a ∈ s) (f a) 1 := by simp
end CommMonoid
lemma card_filter (p) [DecidablePred p] (s : Finset ι) :
#{i ∈ s | p i} = ∑ i ∈ s, ite (p i) 1 0 := by simp [sum_ite]
end Finset
namespace Fintype
open Finset
variable [CommMonoid M] [Fintype ι]
@[to_additive]
lemma prod_ite_eq_ite_exists (p : ι → Prop) [DecidablePred p] (h : ∀ i j, p i → p j → i = j)
(a : M) : ∏ i, ite (p i) a 1 = ite (∃ i, p i) a 1 := by
simp [prod_ite_one univ p (by simpa using h)]
variable [DecidableEq ι]
@[to_additive]
lemma prod_ite_mem (s : Finset ι) (f : ι → M) : ∏ i, (if i ∈ s then f i else 1) = ∏ i ∈ s, f i := by
simp
/-- See also `Finset.prod_dite_eq`. -/
@[to_additive /-- See also `Finset.sum_dite_eq`. -/]
lemma prod_dite_eq (i : ι) (f : ∀ j, i = j → M) :
∏ j, (if h : i = j then f j h else 1) = f i rfl := by
rw [Finset.prod_dite_eq, if_pos (mem_univ _)]
/-- See also `Finset.prod_dite_eq'`. -/
@[to_additive /-- See also `Finset.sum_dite_eq'`. -/]
lemma prod_dite_eq' (i : ι) (f : ∀ j, j = i → M) :
∏ j, (if h : j = i then f j h else 1) = f i rfl := by
rw [Finset.prod_dite_eq', if_pos (mem_univ _)]
/-- See also `Finset.prod_ite_eq`. -/
@[to_additive /-- See also `Finset.sum_ite_eq`. -/]
lemma prod_ite_eq (i : ι) (f : ι → M) : ∏ j, (if i = j then f j else 1) = f i := by
rw [Finset.prod_ite_eq, if_pos (mem_univ _)]
/-- See also `Finset.prod_ite_eq'`. -/
@[to_additive /-- See also `Finset.sum_ite_eq'`. -/]
lemma prod_ite_eq' (i : ι) (f : ι → M) : ∏ j, (if j = i then f j else 1) = f i := by
rw [Finset.prod_ite_eq', if_pos (mem_univ _)]
/-- See also `Finset.prod_pi_mulSingle`. -/
@[to_additive /-- See also `Finset.sum_pi_single`. -/]
lemma prod_pi_mulSingle {M : ι → Type*} [∀ i, CommMonoid (M i)] (i : ι) (f : ∀ i, M i) :
∏ j, Pi.mulSingle j (f j) i = f i := prod_dite_eq _ _
/-- See also `Finset.prod_pi_mulSingle'`. -/
@[to_additive /-- See also `Finset.sum_pi_single'`. -/]
lemma prod_pi_mulSingle' (i : ι) (a : M) : ∏ j, Pi.mulSingle i a j = a := prod_dite_eq' _ _
end Fintype |
.lake/packages/mathlib/Mathlib/Algebra/BigOperators/Group/Finset/Interval.lean | import Mathlib.Algebra.CharP.Defs
import Mathlib.Algebra.Group.EvenFunction
import Mathlib.Data.Int.Interval
import Mathlib.Tactic.Zify
/-!
# Sums/products over integer intervals
This file contains some lemmas about sums and products over integer intervals `Ixx`.
-/
namespace Finset
@[to_additive]
lemma prod_Icc_of_even_eq_range {α : Type*} [CommGroup α] {f : ℤ → α} (hf : f.Even) (N : ℕ) :
∏ m ∈ Icc (-N : ℤ) N, f m = (∏ m ∈ range (N + 1), f m) ^ 2 / f 0 := by
induction N with
| zero => simp [sq]
| succ N ih =>
rw [Nat.cast_add, Nat.cast_one, Icc_succ_succ, prod_union (by simp), prod_pair (by omega), ih,
prod_range_succ _ (N + 1), hf, ← pow_two, div_mul_eq_mul_div, ← mul_pow, Nat.cast_succ]
@[to_additive]
lemma prod_Icc_eq_prod_Ico_mul {α : Type*} [CommMonoid α] (f : ℤ → α) {l u : ℤ}
(h : l ≤ u) : ∏ m ∈ Icc l u, f m = (∏ m ∈ Ico l u, f m) * f u := by
simp [Icc_eq_cons_Ico h, mul_comm]
@[to_additive]
lemma prod_Icc_succ_eq_mul_endpoints {R : Type*} [CommGroup R] (f : ℤ → R) {N : ℕ} :
∏ m ∈ Icc (-(N + 1) : ℤ) (N + 1), f m =
f (N + 1) * f (-(N + 1) : ℤ) * ∏ m ∈ Icc (-N : ℤ) N, f m := by
induction N
· rw [Icc_succ_succ]
simp only [CharP.cast_eq_zero, neg_zero, Icc_self, zero_add, Int.reduceNeg, union_insert,
union_singleton, mem_insert, reduceCtorEq, mem_singleton, neg_eq_zero, one_ne_zero, or_self,
not_false_eq_true, prod_insert, prod_singleton]
grind
· rw [Icc_succ_succ, prod_union (by simp)]
grind
end Finset |
.lake/packages/mathlib/Mathlib/Algebra/BigOperators/Group/Finset/Sigma.lean | import Mathlib.Data.Finset.Sigma
import Mathlib.Algebra.BigOperators.Group.Finset.Basic
/-!
# Product and sums indexed by finite sets in sigma types.
-/
variable {ι κ α β γ : Type*}
open Fin Function
variable {s s₁ s₂ : Finset α} {a : α} {f g : α → β}
namespace Finset
section CommMonoid
variable [CommMonoid β]
/-- The product over a sigma type equals the product of the fiberwise products.
For rewriting in the reverse direction, use `Finset.prod_sigma'`.
See also `Fintype.prod_sigma` for the product over the whole type. -/
@[to_additive /-- The sum over a sigma type equals the sum of the fiberwise sums. For rewriting
in the reverse direction, use `Finset.sum_sigma'`.
See also `Fintype.sum_sigma` for the sum over the whole type. -/]
theorem prod_sigma {σ : α → Type*} (s : Finset α) (t : ∀ a, Finset (σ a)) (f : Sigma σ → β) :
∏ x ∈ s.sigma t, f x = ∏ a ∈ s, ∏ s ∈ t a, f ⟨a, s⟩ := by
simp_rw [← disjiUnion_map_sigma_mk, prod_disjiUnion, prod_map, Function.Embedding.sigmaMk_apply]
/-- The product over a sigma type equals the product of the fiberwise products. For rewriting
in the reverse direction, use `Finset.prod_sigma`. -/
@[to_additive /-- The sum over a sigma type equals the sum of the fiberwise sums. For rewriting
in the reverse direction, use `Finset.sum_sigma` -/]
theorem prod_sigma' {σ : α → Type*} (s : Finset α) (t : ∀ a, Finset (σ a)) (f : ∀ a, σ a → β) :
(∏ a ∈ s, ∏ s ∈ t a, f a s) = ∏ x ∈ s.sigma t, f x.1 x.2 :=
Eq.symm <| prod_sigma s t fun x => f x.1 x.2
@[to_additive]
theorem prod_finset_product (r : Finset (γ × α)) (s : Finset γ) (t : γ → Finset α)
(h : ∀ p : γ × α, p ∈ r ↔ p.1 ∈ s ∧ p.2 ∈ t p.1) {f : γ × α → β} :
∏ p ∈ r, f p = ∏ c ∈ s, ∏ a ∈ t c, f (c, a) := by
refine Eq.trans ?_ (prod_sigma s t fun p => f (p.1, p.2))
apply prod_equiv (Equiv.sigmaEquivProd _ _).symm <;> simp [h]
@[to_additive]
theorem prod_finset_product' (r : Finset (γ × α)) (s : Finset γ) (t : γ → Finset α)
(h : ∀ p : γ × α, p ∈ r ↔ p.1 ∈ s ∧ p.2 ∈ t p.1) {f : γ → α → β} :
∏ p ∈ r, f p.1 p.2 = ∏ c ∈ s, ∏ a ∈ t c, f c a :=
prod_finset_product r s t h
@[to_additive]
theorem prod_finset_product_right (r : Finset (α × γ)) (s : Finset γ) (t : γ → Finset α)
(h : ∀ p : α × γ, p ∈ r ↔ p.2 ∈ s ∧ p.1 ∈ t p.2) {f : α × γ → β} :
∏ p ∈ r, f p = ∏ c ∈ s, ∏ a ∈ t c, f (a, c) := by
refine Eq.trans ?_ (prod_sigma s t fun p => f (p.2, p.1))
apply prod_equiv ((Equiv.prodComm _ _).trans (Equiv.sigmaEquivProd _ _).symm) <;> simp [h]
@[to_additive]
theorem prod_finset_product_right' (r : Finset (α × γ)) (s : Finset γ) (t : γ → Finset α)
(h : ∀ p : α × γ, p ∈ r ↔ p.2 ∈ s ∧ p.1 ∈ t p.2) {f : α → γ → β} :
∏ p ∈ r, f p.1 p.2 = ∏ c ∈ s, ∏ a ∈ t c, f a c :=
prod_finset_product_right r s t h
/-- The product over a product set equals the product of the fiberwise products. For rewriting
in the reverse direction, use `Finset.prod_product'`. -/
@[to_additive /-- The sum over a product set equals the sum of the fiberwise sums. For rewriting
in the reverse direction, use `Finset.sum_product'` -/]
theorem prod_product (s : Finset γ) (t : Finset α) (f : γ × α → β) :
∏ x ∈ s ×ˢ t, f x = ∏ x ∈ s, ∏ y ∈ t, f (x, y) :=
prod_finset_product (s ×ˢ t) s (fun _a => t) fun _p => mem_product
/-- The product over a product set equals the product of the fiberwise products. For rewriting
in the reverse direction, use `Finset.prod_product`. -/
@[to_additive /-- The sum over a product set equals the sum of the fiberwise sums. For rewriting
in the reverse direction, use `Finset.sum_product` -/]
theorem prod_product' (s : Finset γ) (t : Finset α) (f : γ → α → β) :
∏ x ∈ s ×ˢ t, f x.1 x.2 = ∏ x ∈ s, ∏ y ∈ t, f x y :=
prod_product ..
@[to_additive]
theorem prod_product_right (s : Finset γ) (t : Finset α) (f : γ × α → β) :
∏ x ∈ s ×ˢ t, f x = ∏ y ∈ t, ∏ x ∈ s, f (x, y) :=
prod_finset_product_right (s ×ˢ t) t (fun _a => s) fun _p => mem_product.trans and_comm
/-- An uncurried version of `Finset.prod_product_right`. -/
@[to_additive /-- An uncurried version of `Finset.sum_product_right` -/]
theorem prod_product_right' (s : Finset γ) (t : Finset α) (f : γ → α → β) :
∏ x ∈ s ×ˢ t, f x.1 x.2 = ∏ y ∈ t, ∏ x ∈ s, f x y :=
prod_product_right ..
/-- Generalization of `Finset.prod_comm` to the case when the inner `Finset`s depend on the outer
variable. -/
@[to_additive /-- Generalization of `Finset.sum_comm` to the case when the inner `Finset`s depend on
the outer variable. -/]
theorem prod_comm' {s : Finset γ} {t : γ → Finset α} {t' : Finset α} {s' : α → Finset γ}
(h : ∀ x y, x ∈ s ∧ y ∈ t x ↔ x ∈ s' y ∧ y ∈ t') {f : γ → α → β} :
(∏ x ∈ s, ∏ y ∈ t x, f x y) = ∏ y ∈ t', ∏ x ∈ s' y, f x y := by
classical
have : ∀ z : γ × α, (z ∈ s.biUnion fun x => (t x).map <| Function.Embedding.sectR x _) ↔
z.1 ∈ s ∧ z.2 ∈ t z.1 := by
rintro ⟨x, y⟩
simp only [mem_biUnion, mem_map, Function.Embedding.sectR_apply, Prod.mk.injEq,
exists_eq_right, ← and_assoc]
exact
(prod_finset_product' _ _ _ this).symm.trans
((prod_finset_product_right' _ _ _) fun ⟨x, y⟩ => (this _).trans ((h x y).trans and_comm))
@[to_additive]
theorem prod_comm {s : Finset γ} {t : Finset α} {f : γ → α → β} :
(∏ x ∈ s, ∏ y ∈ t, f x y) = ∏ y ∈ t, ∏ x ∈ s, f x y :=
prod_comm' fun _ _ => Iff.rfl
/-- Cyclically permute 3 nested instances of `Finset.prod`. -/
@[to_additive]
theorem prod_comm_cycle {s : Finset γ} {t : Finset α} {u : Finset κ} {f : γ → α → κ → β} :
(∏ x ∈ s, ∏ y ∈ t, ∏ z ∈ u, f x y z) = ∏ z ∈ u, ∏ x ∈ s, ∏ y ∈ t, f x y z := by
simp_rw [prod_comm (s := t), prod_comm (s := s)]
end CommMonoid
@[simp]
theorem card_sigma {σ : α → Type*} (s : Finset α) (t : ∀ a, Finset (σ a)) :
#(s.sigma t) = ∑ a ∈ s, #(t a) :=
Multiset.card_sigma _ _
end Finset |
.lake/packages/mathlib/Mathlib/Algebra/BigOperators/Group/Finset/Pi.lean | import Mathlib.Data.Fintype.Pi
import Mathlib.Algebra.BigOperators.Group.Finset.Defs
/-!
# Products over `univ.pi`
-/
assert_not_exists MonoidWithZero MulAction IsOrderedMonoid
variable {ι β : Type*}
open Fin Function
namespace Finset
variable [CommMonoid β]
/-- Taking a product over `univ.pi t` is the same as taking the product over `Fintype.piFinset t`.
`univ.pi t` and `Fintype.piFinset t` are essentially the same `Finset`, but differ
in the type of their element, `univ.pi t` is a `Finset (Π a ∈ univ, t a)` and
`Fintype.piFinset t` is a `Finset (Π a, t a)`. -/
@[to_additive /-- Taking a sum over `univ.pi t` is the same as taking the sum over
`Fintype.piFinset t`. `univ.pi t` and `Fintype.piFinset t` are essentially the same `Finset`,
but differ in the type of their element, `univ.pi t` is a `Finset (Π a ∈ univ, t a)` and
`Fintype.piFinset t` is a `Finset (Π a, t a)`. -/]
lemma prod_univ_pi [DecidableEq ι] [Fintype ι] {κ : ι → Type*} (t : ∀ i, Finset (κ i))
(f : (∀ i ∈ (univ : Finset ι), κ i) → β) :
∏ x ∈ univ.pi t, f x = ∏ x ∈ Fintype.piFinset t, f fun a _ ↦ x a := by
apply prod_nbij' (fun x i ↦ x i <| mem_univ _) (fun x i _ ↦ x i) <;> simp
end Finset |
.lake/packages/mathlib/Mathlib/Algebra/BigOperators/Group/Finset/Basic.lean | import Mathlib.Algebra.BigOperators.Group.Finset.Defs
import Mathlib.Data.Finset.Prod
import Mathlib.Data.Finset.Sum
/-!
# Big operators
In this file we prove theorems about products and sums indexed by a `Finset`.
-/
-- TODO: assert_not_exists AddCommMonoidWithOne
assert_not_exists MonoidWithZero MulAction IsOrderedMonoid
assert_not_exists Finset.preimage Finset.sigma Fintype.piFinset
assert_not_exists Finset.piecewise Set.indicator MonoidHom.coeFn Function.support IsSquare
open Fin Function
variable {ι κ G M : Type*} {s s₁ s₂ : Finset ι} {a : ι}
namespace Finset
section CommMonoid
variable [CommMonoid M] {f g : ι → M}
@[to_additive]
lemma prod_eq_fold (s : Finset ι) (f : ι → M) : ∏ i ∈ s, f i = s.fold (β := M) (· * ·) 1 f := rfl
@[to_additive (attr := simp)]
theorem prod_cons (h : a ∉ s) : ∏ x ∈ cons a s h, f x = f a * ∏ x ∈ s, f x :=
fold_cons h
/-- Variant of `prod_cons` not applied to a function. -/
@[to_additive (attr := grind =)]
theorem prod_cons' (h : a ∉ s) :
Finset.prod (cons a s h) = fun (f : ι → M) => f a * ∏ x ∈ s, f x := by
funext f
rw [Finset.prod_cons h]
@[to_additive (attr := simp)]
theorem prod_insert [DecidableEq ι] : a ∉ s → ∏ x ∈ insert a s, f x = f a * ∏ x ∈ s, f x :=
fold_insert
/-- Variant of `prod_insert` not applied to a function. -/
@[to_additive (attr := grind =)]
theorem prod_insert' [DecidableEq ι] (h : a ∉ s) :
Finset.prod (insert a s) = fun (f : ι → M) => f a * ∏ x ∈ s, f x := by
funext f
rw [Finset.prod_insert h]
/-- The product of `f` over `insert a s` is the same as
the product over `s`, as long as `a` is in `s` or `f a = 1`. -/
@[to_additive (attr := simp) /-- The sum of `f` over `insert a s` is the same as
the sum over `s`, as long as `a` is in `s` or `f a = 0`. -/]
theorem prod_insert_of_eq_one_if_notMem [DecidableEq ι] (h : a ∉ s → f a = 1) :
∏ x ∈ insert a s, f x = ∏ x ∈ s, f x := by
by_cases hm : a ∈ s
· simp_rw [insert_eq_of_mem hm]
· rw [prod_insert hm, h hm, one_mul]
@[deprecated (since := "2025-05-23")]
alias sum_insert_of_eq_zero_if_not_mem := sum_insert_of_eq_zero_if_notMem
@[to_additive existing, deprecated (since := "2025-05-23")]
alias prod_insert_of_eq_one_if_not_mem := prod_insert_of_eq_one_if_notMem
/-- The product of `f` over `insert a s` is the same as
the product over `s`, as long as `f a = 1`. -/
@[to_additive /-- The sum of `f` over `insert a s` is the same as
the sum over `s`, as long as `f a = 0`. -/]
theorem prod_insert_one [DecidableEq ι] (h : f a = 1) : ∏ x ∈ insert a s, f x = ∏ x ∈ s, f x := by
simp [h]
@[to_additive (attr := simp)]
theorem prod_singleton (f : ι → M) (a : ι) : ∏ x ∈ singleton a, f x = f a :=
Eq.trans fold_singleton <| mul_one _
/-- Variant of `prod_singleton` not applied to a function. -/
@[to_additive (attr := grind =)]
theorem prod_singleton' (a : ι) :
Finset.prod (singleton a) = fun (f : ι → M) => f a := by
funext f
simp
@[to_additive]
theorem prod_pair [DecidableEq ι] {a b : ι} (h : a ≠ b) :
(∏ x ∈ ({a, b} : Finset ι), f x) = f a * f b := by
rw [prod_insert (notMem_singleton.2 h), prod_singleton]
@[to_additive (attr := simp)]
theorem prod_image [DecidableEq ι] {s : Finset κ} {g : κ → ι} :
Set.InjOn g s → ∏ x ∈ s.image g, f x = ∏ x ∈ s, f (g x) :=
fold_image
@[to_additive]
lemma prod_attach (s : Finset ι) (f : ι → M) : ∏ x ∈ s.attach, f x = ∏ x ∈ s, f x := by
classical rw [← prod_image Subtype.coe_injective.injOn, attach_image_val]
@[to_additive (attr := congr)]
theorem prod_congr (h : s₁ = s₂) : (∀ x ∈ s₂, f x = g x) → s₁.prod f = s₂.prod g := by
rw [h]; exact fold_congr
@[to_additive]
theorem prod_eq_one (h : ∀ x ∈ s, f x = 1) : ∏ x ∈ s, f x = 1 := calc
∏ x ∈ s, f x = ∏ _x ∈ s, 1 := prod_congr rfl h
_ = 1 := prod_const_one
/-- In a monoid whose only unit is `1`, a product is equal to `1` iff all factors are `1`. -/
@[to_additive (attr := simp)
/-- In an additive monoid whose only unit is `0`, a sum is equal to `0` iff all terms are `0`. -/]
lemma prod_eq_one_iff [Subsingleton Mˣ] : ∏ i ∈ s, f i = 1 ↔ ∀ i ∈ s, f i = 1 := by
induction s using Finset.cons_induction <;> simp [*]
@[to_additive]
theorem prod_disjUnion (h) :
∏ x ∈ s₁.disjUnion s₂ h, f x = (∏ x ∈ s₁, f x) * ∏ x ∈ s₂, f x := by
refine Eq.trans ?_ (fold_disjUnion h)
rw [one_mul]
rfl
@[to_additive]
theorem prod_disjiUnion (s : Finset κ) (t : κ → Finset ι) (h) :
∏ x ∈ s.disjiUnion t h, f x = ∏ i ∈ s, ∏ x ∈ t i, f x := by
refine Eq.trans ?_ (fold_disjiUnion h)
dsimp [Finset.prod, Multiset.prod, Multiset.fold, Finset.disjUnion, Finset.fold]
congr
exact prod_const_one.symm
@[to_additive]
theorem prod_union_inter [DecidableEq ι] :
(∏ x ∈ s₁ ∪ s₂, f x) * ∏ x ∈ s₁ ∩ s₂, f x = (∏ x ∈ s₁, f x) * ∏ x ∈ s₂, f x :=
fold_union_inter
@[to_additive]
theorem prod_union [DecidableEq ι] (h : Disjoint s₁ s₂) :
∏ x ∈ s₁ ∪ s₂, f x = (∏ x ∈ s₁, f x) * ∏ x ∈ s₂, f x := by
rw [← prod_union_inter, disjoint_iff_inter_eq_empty.mp h]; exact (mul_one _).symm
@[to_additive]
theorem prod_filter_mul_prod_filter_not
(s : Finset ι) (p : ι → Prop) [DecidablePred p] [∀ x, Decidable (¬p x)] (f : ι → M) :
(∏ x ∈ s with p x, f x) * ∏ x ∈ s with ¬p x, f x = ∏ x ∈ s, f x := by
have := Classical.decEq ι
rw [← prod_union (disjoint_filter_filter_neg s s p), filter_union_filter_neg_eq]
@[to_additive]
lemma prod_filter_not_mul_prod_filter (s : Finset ι) (p : ι → Prop) [DecidablePred p]
[∀ x, Decidable (¬p x)] (f : ι → M) :
(∏ x ∈ s with ¬p x, f x) * ∏ x ∈ s with p x, f x = ∏ x ∈ s, f x := by
rw [mul_comm, prod_filter_mul_prod_filter_not]
@[to_additive]
theorem prod_filter_xor (p q : ι → Prop) [DecidablePred p] [DecidablePred q] :
(∏ x ∈ s with (Xor' (p x) (q x)), f x) =
(∏ x ∈ s with (p x ∧ ¬ q x), f x) * (∏ x ∈ s with (q x ∧ ¬ p x), f x) := by
classical rw [← prod_union (disjoint_filter_and_not_filter _ _), ← filter_or]
simp only [Xor']
@[to_additive]
theorem _root_.IsCompl.prod_mul_prod [Fintype ι] {s t : Finset ι} (h : IsCompl s t) (f : ι → M) :
(∏ i ∈ s, f i) * ∏ i ∈ t, f i = ∏ i, f i :=
(Finset.prod_disjUnion h.disjoint).symm.trans <| by
classical rw [Finset.disjUnion_eq_union, ← Finset.sup_eq_union, h.sup_eq_top]; rfl
/-- Multiplying the products of a function over `s` and over `sᶜ` gives the whole product.
For a version expressed with subtypes, see `Fintype.prod_subtype_mul_prod_subtype`. -/
@[to_additive /-- Adding the sums of a function over `s` and over `sᶜ` gives the whole sum.
For a version expressed with subtypes, see `Fintype.sum_subtype_add_sum_subtype`. -/]
lemma prod_mul_prod_compl [Fintype ι] [DecidableEq ι] (s : Finset ι) (f : ι → M) :
(∏ i ∈ s, f i) * ∏ i ∈ sᶜ, f i = ∏ i, f i :=
IsCompl.prod_mul_prod isCompl_compl f
@[to_additive]
lemma prod_compl_mul_prod [Fintype ι] [DecidableEq ι] (s : Finset ι) (f : ι → M) :
(∏ i ∈ sᶜ, f i) * ∏ i ∈ s, f i = ∏ i, f i :=
(@isCompl_compl _ s _).symm.prod_mul_prod f
@[to_additive]
theorem prod_sdiff [DecidableEq ι] (h : s₁ ⊆ s₂) :
(∏ x ∈ s₂ \ s₁, f x) * ∏ x ∈ s₁, f x = ∏ x ∈ s₂, f x := by
rw [← prod_union sdiff_disjoint, sdiff_union_of_subset h]
@[to_additive]
theorem prod_subset_one_on_sdiff [DecidableEq ι] (h : s₁ ⊆ s₂) (hg : ∀ x ∈ s₂ \ s₁, g x = 1)
(hfg : ∀ x ∈ s₁, f x = g x) : ∏ i ∈ s₁, f i = ∏ i ∈ s₂, g i := by
rw [← prod_sdiff h, prod_eq_one hg, one_mul]
exact prod_congr rfl hfg
@[to_additive]
theorem prod_subset (h : s₁ ⊆ s₂) (hf : ∀ x ∈ s₂, x ∉ s₁ → f x = 1) :
∏ x ∈ s₁, f x = ∏ x ∈ s₂, f x :=
haveI := Classical.decEq ι
prod_subset_one_on_sdiff h (by simpa) fun _ _ => rfl
@[to_additive (attr := simp)]
theorem prod_disjSum (s : Finset ι) (t : Finset κ) (f : ι ⊕ κ → M) :
∏ x ∈ s.disjSum t, f x = (∏ x ∈ s, f (Sum.inl x)) * ∏ x ∈ t, f (Sum.inr x) := by
rw [← map_inl_disjUnion_map_inr, prod_disjUnion, prod_map, prod_map]
rfl
@[deprecated (since := "2025-06-11")]
alias sum_disj_sum := sum_disjSum
@[to_additive existing, deprecated (since := "2025-06-11")]
alias prod_disj_sum := prod_disjSum
@[to_additive]
lemma prod_sum_eq_prod_toLeft_mul_prod_toRight (s : Finset (ι ⊕ κ)) (f : ι ⊕ κ → M) :
∏ x ∈ s, f x = (∏ x ∈ s.toLeft, f (Sum.inl x)) * ∏ x ∈ s.toRight, f (Sum.inr x) := by
rw [← Finset.toLeft_disjSum_toRight (u := s), Finset.prod_disjSum, Finset.toLeft_disjSum,
Finset.toRight_disjSum]
@[to_additive]
theorem prod_sumElim (s : Finset ι) (t : Finset κ) (f : ι → M) (g : κ → M) :
∏ x ∈ s.disjSum t, Sum.elim f g x = (∏ x ∈ s, f x) * ∏ x ∈ t, g x := by simp
@[to_additive]
theorem prod_biUnion [DecidableEq ι] {s : Finset κ} {t : κ → Finset ι}
(hs : Set.PairwiseDisjoint (↑s) t) : ∏ x ∈ s.biUnion t, f x = ∏ x ∈ s, ∏ i ∈ t x, f i := by
rw [← disjiUnion_eq_biUnion _ _ hs, prod_disjiUnion]
section bij
variable {s : Finset ι} {t : Finset κ} {f : ι → M} {g : κ → M}
@[to_additive]
lemma prod_of_injOn (e : ι → κ) (he : Set.InjOn e s) (hest : Set.MapsTo e s t)
(h' : ∀ i ∈ t, i ∉ e '' s → g i = 1) (h : ∀ i ∈ s, f i = g (e i)) :
∏ i ∈ s, f i = ∏ j ∈ t, g j := by
classical
exact (prod_nbij e (fun a ↦ mem_image_of_mem e) he (by simp [Set.surjOn_image]) h).trans <|
prod_subset (image_subset_iff.2 hest) <| by simpa using h'
variable [DecidableEq κ]
@[to_additive]
lemma prod_fiberwise_eq_prod_filter (s : Finset ι) (t : Finset κ) (g : ι → κ) (f : ι → M) :
∏ j ∈ t, ∏ i ∈ s with g i = j, f i = ∏ i ∈ s with g i ∈ t, f i := by
rw [← prod_disjiUnion, disjiUnion_filter_eq]
#adaptation_note /-- 2025-09-12 (kmill) copied from private lemma pairwiseDisjoint_fibers -/
intro x' hx y' hy hne
simp_rw [disjoint_left, mem_filter]; rintro i ⟨_, rfl⟩ ⟨_, rfl⟩; exact hne rfl
@[to_additive]
lemma prod_fiberwise_eq_prod_filter' (s : Finset ι) (t : Finset κ) (g : ι → κ) (f : κ → M) :
∏ j ∈ t, ∏ i ∈ s with g i = j, f j = ∏ i ∈ s with g i ∈ t, f (g i) := by
calc
_ = ∏ j ∈ t, ∏ i ∈ s with g i = j, f (g i) :=
prod_congr rfl fun j _ ↦ prod_congr rfl fun i hi ↦ by rw [(mem_filter.1 hi).2]
_ = _ := prod_fiberwise_eq_prod_filter _ _ _ _
@[to_additive]
lemma prod_fiberwise_of_maps_to {g : ι → κ} (h : ∀ i ∈ s, g i ∈ t) (f : ι → M) :
∏ j ∈ t, ∏ i ∈ s with g i = j, f i = ∏ i ∈ s, f i := by
rw [← prod_disjiUnion, disjiUnion_filter_eq_of_maps_to h]
#adaptation_note /-- 2025-09-12 (kmill) copied from private lemma pairwiseDisjoint_fibers -/
intro x' hx y' hy hne
simp_rw [disjoint_left, mem_filter]; rintro i ⟨_, rfl⟩ ⟨_, rfl⟩; exact hne rfl
@[to_additive]
lemma prod_fiberwise_of_maps_to' {g : ι → κ} (h : ∀ i ∈ s, g i ∈ t) (f : κ → M) :
∏ j ∈ t, ∏ i ∈ s with g i = j, f j = ∏ i ∈ s, f (g i) := by
calc
_ = ∏ j ∈ t, ∏ i ∈ s with g i = j, f (g i) :=
prod_congr rfl fun y _ ↦ prod_congr rfl fun x hx ↦ by rw [(mem_filter.1 hx).2]
_ = _ := prod_fiberwise_of_maps_to h _
variable [Fintype κ]
@[to_additive]
lemma prod_fiberwise (s : Finset ι) (g : ι → κ) (f : ι → M) :
∏ j, ∏ i ∈ s with g i = j, f i = ∏ i ∈ s, f i :=
prod_fiberwise_of_maps_to (fun _ _ ↦ mem_univ _) _
@[to_additive]
lemma prod_fiberwise' (s : Finset ι) (g : ι → κ) (f : κ → M) :
∏ j, ∏ i ∈ s with g i = j, f j = ∏ i ∈ s, f (g i) :=
prod_fiberwise_of_maps_to' (fun _ _ ↦ mem_univ _) _
end bij
@[to_additive (attr := simp)]
lemma prod_diag [DecidableEq ι] (s : Finset ι) (f : ι × ι → M) :
∏ i ∈ s.diag, f i = ∏ i ∈ s, f (i, i) := by
apply prod_nbij' Prod.fst (fun i ↦ (i, i)) <;> simp
@[to_additive]
theorem prod_image' [DecidableEq ι] {s : Finset κ} {g : κ → ι} (h : κ → M)
(eq : ∀ i ∈ s, f (g i) = ∏ j ∈ s with g j = g i, h j) :
∏ a ∈ s.image g, f a = ∏ i ∈ s, h i :=
calc
∏ a ∈ s.image g, f a = ∏ a ∈ s.image g, ∏ j ∈ s with g j = a, h j :=
(prod_congr rfl) fun _a hx =>
let ⟨i, his, hi⟩ := mem_image.1 hx
hi ▸ eq i his
_ = ∏ i ∈ s, h i := prod_fiberwise_of_maps_to (fun _ => mem_image_of_mem g) _
@[to_additive]
theorem prod_mul_distrib : ∏ x ∈ s, f x * g x = (∏ x ∈ s, f x) * ∏ x ∈ s, g x :=
Eq.trans (by rw [one_mul]; rfl) fold_op_distrib
@[to_additive]
lemma prod_mul_prod_comm (f g h i : ι → M) :
(∏ a ∈ s, f a * g a) * ∏ a ∈ s, h a * i a = (∏ a ∈ s, f a * h a) * ∏ a ∈ s, g a * i a := by
simp_rw [prod_mul_distrib, mul_mul_mul_comm]
@[to_additive]
theorem prod_filter_of_ne {p : ι → Prop} [DecidablePred p] (hp : ∀ x ∈ s, f x ≠ 1 → p x) :
∏ x ∈ s with p x, f x = ∏ x ∈ s, f x :=
(prod_subset (filter_subset _ _)) fun x => by
classical
rw [not_imp_comm, mem_filter]
exact fun h₁ h₂ => ⟨h₁, by simpa using hp _ h₁ h₂⟩
-- If we use `[DecidableEq M]` here, some rewrites fail because they find a wrong `Decidable`
-- instance first; `{∀ x, Decidable (f x ≠ 1)}` doesn't work with `rw ← prod_filter_ne_one`
@[to_additive]
theorem prod_filter_ne_one (s : Finset ι) [∀ x, Decidable (f x ≠ 1)] :
∏ x ∈ s with f x ≠ 1, f x = ∏ x ∈ s, f x :=
prod_filter_of_ne fun _ _ => id
@[to_additive]
theorem prod_filter (p : ι → Prop) [DecidablePred p] (f : ι → M) :
∏ a ∈ s with p a, f a = ∏ a ∈ s, if p a then f a else 1 :=
calc
∏ a ∈ s with p a, f a = ∏ a ∈ s with p a, if p a then f a else 1 :=
prod_congr rfl fun a h => by rw [if_pos]; simpa using (mem_filter.1 h).2
_ = ∏ a ∈ s, if p a then f a else 1 := by
{ refine prod_subset (filter_subset _ s) fun x hs h => ?_
rw [mem_filter, not_and] at h
exact if_neg (by simpa using h hs) }
@[to_additive]
theorem prod_eq_single_of_mem {s : Finset ι} {f : ι → M} (a : ι) (h : a ∈ s)
(h₀ : ∀ b ∈ s, b ≠ a → f b = 1) : ∏ x ∈ s, f x = f a := by
calc
∏ x ∈ s, f x = ∏ x ∈ {a}, f x := by
{ refine (prod_subset ?_ ?_).symm
· intro _ H
rwa [mem_singleton.1 H]
· simpa only [mem_singleton] }
_ = f a := prod_singleton _ _
@[to_additive]
theorem prod_eq_single {s : Finset ι} {f : ι → M} (a : ι) (h₀ : ∀ b ∈ s, b ≠ a → f b = 1)
(h₁ : a ∉ s → f a = 1) : ∏ x ∈ s, f x = f a :=
haveI := Classical.decEq ι
by_cases (prod_eq_single_of_mem a · h₀) fun this =>
(prod_congr rfl fun b hb => h₀ b hb <| by rintro rfl; exact this hb).trans <|
prod_const_one.trans (h₁ this).symm
@[to_additive]
lemma prod_eq_ite [DecidableEq ι] {s : Finset ι} {f : ι → M} (a : ι)
(h₀ : ∀ b ∈ s, b ≠ a → f b = 1) :
∏ x ∈ s, f x = if a ∈ s then f a else 1 := by
by_cases h : a ∈ s
· simp [Finset.prod_eq_single_of_mem a h h₀, h]
· replace h₀ : ∀ b ∈ s, f b = 1 := by aesop
simp +contextual [h₀]
@[to_additive]
lemma prod_union_eq_left [DecidableEq ι] (hs : ∀ a ∈ s₂, a ∉ s₁ → f a = 1) :
∏ a ∈ s₁ ∪ s₂, f a = ∏ a ∈ s₁, f a :=
Eq.symm <|
prod_subset subset_union_left fun _a ha ha' ↦ hs _ ((mem_union.1 ha).resolve_left ha') ha'
@[to_additive]
lemma prod_union_eq_right [DecidableEq ι] (hs : ∀ a ∈ s₁, a ∉ s₂ → f a = 1) :
∏ a ∈ s₁ ∪ s₂, f a = ∏ a ∈ s₂, f a := by rw [union_comm, prod_union_eq_left hs]
/-- The products of two functions `f g : ι → M` over finite sets `s₁ s₂ : Finset ι`
are equal if the functions agree on `s₁ ∩ s₂`, `f = 1` and `g = 1` on the respective
set differences. -/
@[to_additive /-- The sum of two functions `f g : ι → M` over finite sets `s₁ s₂ : Finset ι`
are equal if the functions agree on `s₁ ∩ s₂`, `f = 0` and `g = 0` on the respective
set differences. -/]
lemma prod_congr_of_eq_on_inter {ι M : Type*} {s₁ s₂ : Finset ι} {f g : ι → M} [CommMonoid M]
(h₁ : ∀ a ∈ s₁, a ∉ s₂ → f a = 1) (h₂ : ∀ a ∈ s₂, a ∉ s₁ → g a = 1)
(h : ∀ a ∈ s₁, a ∈ s₂ → f a = g a) :
∏ a ∈ s₁, f a = ∏ a ∈ s₂, g a := by
classical
conv_lhs => rw [← sdiff_union_inter s₁ s₂, prod_union_eq_right (by simp_all)]
conv_rhs => rw [← sdiff_union_inter s₂ s₁, prod_union_eq_right (by simp_all), inter_comm]
exact prod_congr rfl (by simpa)
@[to_additive]
theorem prod_eq_mul_of_mem {s : Finset ι} {f : ι → M} (a b : ι) (ha : a ∈ s) (hb : b ∈ s)
(hn : a ≠ b) (h₀ : ∀ c ∈ s, c ≠ a ∧ c ≠ b → f c = 1) : ∏ x ∈ s, f x = f a * f b := by
haveI := Classical.decEq ι; let s' := ({a, b} : Finset ι)
have hu : s' ⊆ s := by grind
have hf : ∀ c ∈ s, c ∉ s' → f c = 1 := by grind
rw [← Finset.prod_subset hu hf]
exact Finset.prod_pair hn
@[to_additive]
theorem prod_eq_mul {s : Finset ι} {f : ι → M} (a b : ι) (hn : a ≠ b)
(h₀ : ∀ c ∈ s, c ≠ a ∧ c ≠ b → f c = 1) (ha : a ∉ s → f a = 1) (hb : b ∉ s → f b = 1) :
∏ x ∈ s, f x = f a * f b := by
haveI := Classical.decEq ι; by_cases h₁ : a ∈ s <;> by_cases h₂ : b ∈ s
· exact prod_eq_mul_of_mem a b h₁ h₂ hn h₀
· rw [hb h₂, mul_one]
apply prod_eq_single_of_mem a h₁
exact fun c hc hca => h₀ c hc ⟨hca, ne_of_mem_of_not_mem hc h₂⟩
· rw [ha h₁, one_mul]
apply prod_eq_single_of_mem b h₂
exact fun c hc hcb => h₀ c hc ⟨ne_of_mem_of_not_mem hc h₁, hcb⟩
· rw [ha h₁, hb h₂, mul_one]
exact
_root_.trans
(prod_congr rfl fun c hc =>
h₀ c hc ⟨ne_of_mem_of_not_mem hc h₁, ne_of_mem_of_not_mem hc h₂⟩)
prod_const_one
/-- A product over `s.subtype p` equals one over `{x ∈ s | p x}`. -/
@[to_additive (attr := simp)
/-- A sum over `s.subtype p` equals one over `{x ∈ s | p x}`. -/]
theorem prod_subtype_eq_prod_filter (f : ι → M) {p : ι → Prop} [DecidablePred p] :
∏ x ∈ s.subtype p, f x = ∏ x ∈ s with p x, f x := by
have := prod_map (s.subtype p) (Function.Embedding.subtype _) f
simp_all
/-- If all elements of a `Finset` satisfy the predicate `p`, a product
over `s.subtype p` equals that product over `s`. -/
@[to_additive /-- If all elements of a `Finset` satisfy the predicate `p`, a sum
over `s.subtype p` equals that sum over `s`. -/]
theorem prod_subtype_of_mem (f : ι → M) {p : ι → Prop} [DecidablePred p] (h : ∀ x ∈ s, p x) :
∏ x ∈ s.subtype p, f x = ∏ x ∈ s, f x := by
rw [prod_subtype_eq_prod_filter, filter_true_of_mem]
simpa using h
/-- A product of a function over a `Finset` in a subtype equals a
product in the main type of a function that agrees with the first
function on that `Finset`. -/
@[to_additive /-- A sum of a function over a `Finset` in a subtype equals a
sum in the main type of a function that agrees with the first
function on that `Finset`. -/]
theorem prod_subtype_map_embedding {p : ι → Prop} {s : Finset { x // p x }} {f : { x // p x } → M}
{g : ι → M} (h : ∀ x : { x // p x }, x ∈ s → g x = f x) :
(∏ x ∈ s.map (Function.Embedding.subtype _), g x) = ∏ x ∈ s, f x := by
rw [Finset.prod_map]
exact Finset.prod_congr rfl h
variable (f s)
@[to_additive]
theorem prod_coe_sort : ∏ i : s, f i = ∏ i ∈ s, f i := prod_attach _ _
@[to_additive]
theorem prod_finset_coe (f : ι → M) (s : Finset ι) : (∏ i : (s : Set ι), f i) = ∏ i ∈ s, f i :=
prod_coe_sort s f
variable {f s}
@[to_additive]
theorem prod_subtype {p : ι → Prop} {F : Fintype (Subtype p)} (s : Finset ι) (h : ∀ x, x ∈ s ↔ p x)
(f : ι → M) : ∏ a ∈ s, f a = ∏ a : Subtype p, f a := by
have : (· ∈ s) = p := Set.ext h
subst p
rw [← prod_coe_sort]
congr!
@[to_additive]
theorem prod_set_coe (s : Set ι) [Fintype s] : (∏ i : s, f i) = ∏ i ∈ s.toFinset, f i :=
(Finset.prod_subtype s.toFinset (fun _ ↦ Set.mem_toFinset) f).symm
/-- The product of a function `g` defined only on a set `s` is equal to
the product of a function `f` defined everywhere,
as long as `f` and `g` agree on `s`, and `f = 1` off `s`. -/
@[to_additive /-- The sum of a function `g` defined only on a set `s` is equal to
the sum of a function `f` defined everywhere,
as long as `f` and `g` agree on `s`, and `f = 0` off `s`. -/]
theorem prod_congr_set [Fintype ι] (s : Set ι) [DecidablePred (· ∈ s)] (f : ι → M) (g : s → M)
(w : ∀ x (hx : x ∈ s), f x = g ⟨x, hx⟩) (w' : ∀ x ∉ s, f x = 1) : ∏ i, f i = ∏ i, g i := by
rw [← prod_subset s.toFinset.subset_univ (by simpa), prod_subtype (p := (· ∈ s)) _ (by simp)]
congr! with ⟨x, h⟩
exact w x h
@[to_additive]
theorem prod_extend_by_one [DecidableEq ι] (s : Finset ι) (f : ι → M) :
∏ i ∈ s, (if i ∈ s then f i else 1) = ∏ i ∈ s, f i :=
(prod_congr rfl) fun _i hi => if_pos hi
@[to_additive]
theorem prod_eq_prod_extend (f : s → M) : ∏ x, f x = ∏ x ∈ s, Subtype.val.extend f 1 x := by
rw [univ_eq_attach, ← Finset.prod_attach s]
congr with ⟨x, hx⟩
rw [Subtype.val_injective.extend_apply]
@[to_additive]
theorem prod_bij_ne_one {s : Finset ι} {t : Finset κ} {f : ι → M} {g : κ → M}
(i : ∀ a ∈ s, f a ≠ 1 → κ) (hi : ∀ a h₁ h₂, i a h₁ h₂ ∈ t)
(i_inj : ∀ a₁ h₁₁ h₁₂ a₂ h₂₁ h₂₂, i a₁ h₁₁ h₁₂ = i a₂ h₂₁ h₂₂ → a₁ = a₂)
(i_surj : ∀ b ∈ t, g b ≠ 1 → ∃ a h₁ h₂, i a h₁ h₂ = b) (h : ∀ a h₁ h₂, f a = g (i a h₁ h₂)) :
∏ x ∈ s, f x = ∏ x ∈ t, g x := by
classical
calc
∏ x ∈ s, f x = ∏ x ∈ s with f x ≠ 1, f x := by rw [prod_filter_ne_one]
_ = ∏ x ∈ t with g x ≠ 1, g x :=
prod_bij (fun a ha => i a (mem_filter.mp ha).1 <| by simpa using (mem_filter.mp ha).2)
?_ ?_ ?_ ?_
_ = ∏ x ∈ t, g x := prod_filter_ne_one _
· grind
· solve_by_elim
· intro b hb
refine (mem_filter.mp hb).elim fun h₁ h₂ ↦ ?_
obtain ⟨a, ha₁, ha₂, eq⟩ := i_surj b h₁ fun H ↦ by rw [H] at h₂; simp at h₂
exact ⟨a, mem_filter.mpr ⟨ha₁, ha₂⟩, eq⟩
· solve_by_elim
@[to_additive]
theorem exists_ne_one_of_prod_ne_one (h : ∏ x ∈ s, f x ≠ 1) : ∃ a ∈ s, f a ≠ 1 := by
classical
rw [← prod_filter_ne_one] at h
rcases nonempty_of_prod_ne_one h with ⟨x, hx⟩
exact ⟨x, (mem_filter.1 hx).1, by simpa using (mem_filter.1 hx).2⟩
@[to_additive]
theorem prod_range_succ_comm (f : ℕ → M) (n : ℕ) :
(∏ x ∈ range (n + 1), f x) = f n * ∏ x ∈ range n, f x := by
rw [range_add_one, prod_insert notMem_range_self]
@[to_additive]
theorem prod_range_succ (f : ℕ → M) (n : ℕ) :
(∏ x ∈ range (n + 1), f x) = (∏ x ∈ range n, f x) * f n := by
simp only [mul_comm, prod_range_succ_comm]
@[to_additive]
theorem prod_range_succ' (f : ℕ → M) :
∀ n : ℕ, (∏ k ∈ range (n + 1), f k) = (∏ k ∈ range n, f (k + 1)) * f 0
| 0 => prod_range_succ _ _
| n + 1 => by rw [prod_range_succ _ n, mul_right_comm, ← prod_range_succ' _ n, prod_range_succ]
@[to_additive]
theorem eventually_constant_prod {u : ℕ → M} {N : ℕ} (hu : ∀ n ≥ N, u n = 1) {n : ℕ} (hn : N ≤ n) :
(∏ k ∈ range n, u k) = ∏ k ∈ range N, u k := by
obtain ⟨m, rfl : n = N + m⟩ := Nat.exists_eq_add_of_le hn
clear hn
induction m with
| zero => simp
| succ m hm => simp [← add_assoc, prod_range_succ, hm, hu]
@[to_additive]
theorem prod_range_add (f : ℕ → M) (n m : ℕ) :
(∏ x ∈ range (n + m), f x) = (∏ x ∈ range n, f x) * ∏ x ∈ range m, f (n + x) := by
induction m with
| zero => simp
| succ m hm => rw [Nat.add_succ, prod_range_succ, prod_range_succ, hm, mul_assoc]
@[to_additive sum_range_one]
theorem prod_range_one (f : ℕ → M) : ∏ k ∈ range 1, f k = f 0 := by
rw [range_one, prod_singleton]
open List
@[to_additive]
theorem prod_list_map_count [DecidableEq ι] (l : List ι) (f : ι → M) :
(l.map f).prod = ∏ m ∈ l.toFinset, f m ^ l.count m := by
induction l with
| nil => simp only [map_nil, prod_nil, count_nil, pow_zero, prod_const_one]
| cons a s IH =>
simp only [List.map, List.prod_cons, toFinset_cons, IH]
by_cases has : a ∈ s.toFinset
· rw [insert_eq_of_mem has, ← insert_erase has, prod_insert (notMem_erase _ _),
prod_insert (notMem_erase _ _), ← mul_assoc, count_cons_self, pow_succ']
congr 1
refine prod_congr rfl fun x hx => ?_
rw [count_cons_of_ne (ne_of_mem_erase hx).symm]
rw [prod_insert has, count_cons_self, count_eq_zero_of_not_mem (mt mem_toFinset.2 has), pow_one]
grind [Finset.prod_congr]
@[to_additive]
theorem prod_list_count [DecidableEq M] (s : List M) :
s.prod = ∏ m ∈ s.toFinset, m ^ s.count m := by simpa using prod_list_map_count s id
@[to_additive]
theorem prod_list_count_of_subset [DecidableEq M] (m : List M) (s : Finset M)
(hs : m.toFinset ⊆ s) : m.prod = ∏ i ∈ s, i ^ m.count i := by
rw [prod_list_count]
refine prod_subset hs fun x _ hx => ?_
rw [mem_toFinset] at hx
rw [count_eq_zero_of_not_mem hx, pow_zero]
open Multiset
@[to_additive]
theorem prod_multiset_map_count [DecidableEq ι] (s : Multiset ι) {M : Type*} [CommMonoid M]
(f : ι → M) : (s.map f).prod = ∏ m ∈ s.toFinset, f m ^ s.count m := by
refine Quot.induction_on s fun l => ?_
simp [prod_list_map_count l f]
@[to_additive]
theorem prod_multiset_count [DecidableEq M] (s : Multiset M) :
s.prod = ∏ m ∈ s.toFinset, m ^ s.count m := by
convert prod_multiset_map_count s id
rw [Multiset.map_id]
@[to_additive]
theorem prod_multiset_count_of_subset [DecidableEq M] (m : Multiset M) (s : Finset M)
(hs : m.toFinset ⊆ s) : m.prod = ∏ i ∈ s, i ^ m.count i := by
revert hs
refine Quot.induction_on m fun l => ?_
simp only [quot_mk_to_coe'', prod_coe, coe_count]
apply prod_list_count_of_subset l s
/-- For any product along `{0, ..., n - 1}` of a commutative-monoid-valued function, we can verify
that it's equal to a different function just by checking ratios of adjacent terms up to `n`.
This is a multiplicative discrete analogue of the fundamental theorem of calculus. -/
@[to_additive /-- For any sum along `{0, ..., n - 1}` of a commutative-monoid-valued function, we
can verify that it's equal to a different function just by checking differences of adjacent terms
up to `n`.
This is a discrete analogue of the fundamental theorem of calculus. -/]
theorem prod_range_induction (f s : ℕ → M) (base : s 0 = 1)
(n : ℕ) (step : ∀ k < n, s (k + 1) = s k * f k) :
∏ k ∈ Finset.range n, f k = s n := by
induction n with
| zero => rw [Finset.prod_range_zero, base]
| succ k hk =>
rw [Finset.prod_range_succ, step _ (Nat.lt_succ_self _), hk]
exact fun _ hl ↦ step _ (Nat.lt_succ_of_lt hl)
@[to_additive (attr := simp)]
theorem prod_const (b : M) : ∏ _x ∈ s, b = b ^ #s :=
(congr_arg _ <| s.val.map_const b).trans <| Multiset.prod_replicate #s b
@[to_additive sum_eq_card_nsmul]
theorem prod_eq_pow_card {b : M} (hf : ∀ a ∈ s, f a = b) : ∏ a ∈ s, f a = b ^ #s :=
(prod_congr rfl hf).trans <| prod_const _
@[to_additive card_nsmul_add_sum]
theorem pow_card_mul_prod {b : M} : b ^ #s * ∏ a ∈ s, f a = ∏ a ∈ s, b * f a :=
(Finset.prod_const b).symm ▸ prod_mul_distrib.symm
@[to_additive sum_add_card_nsmul]
theorem prod_mul_pow_card {b : M} : (∏ a ∈ s, f a) * b ^ #s = ∏ a ∈ s, f a * b :=
(Finset.prod_const b).symm ▸ prod_mul_distrib.symm
@[to_additive]
theorem pow_eq_prod_const (b : M) : ∀ n, b ^ n = ∏ _k ∈ range n, b := by simp
@[to_additive sum_nsmul_assoc]
lemma prod_pow_eq_pow_sum (s : Finset ι) (f : ι → ℕ) (a : M) :
∏ i ∈ s, a ^ f i = a ^ ∑ i ∈ s, f i :=
cons_induction (by simp) (fun _ _ _ _ ↦ by simp [prod_cons, sum_cons, pow_add, *]) s
@[to_additive]
theorem prod_flip {n : ℕ} (f : ℕ → M) :
(∏ r ∈ range (n + 1), f (n - r)) = ∏ k ∈ range (n + 1), f k := by
induction n with
| zero => rw [prod_range_one, prod_range_one]
| succ n ih =>
rw [prod_range_succ', prod_range_succ _ (Nat.succ n)]
simp [← ih]
/-- The difference with `Finset.prod_ninvolution` is that the involution is allowed to use
membership of the domain of the product, rather than being a non-dependent function. -/
@[to_additive /-- The difference with `Finset.sum_ninvolution` is that the involution is allowed to
use membership of the domain of the sum, rather than being a non-dependent function. -/]
lemma prod_involution (g : ∀ a ∈ s, ι) (hg₁ : ∀ a ha, f a * f (g a ha) = 1)
(hg₃ : ∀ a ha, f a ≠ 1 → g a ha ≠ a)
(g_mem : ∀ a ha, g a ha ∈ s) (hg₄ : ∀ a ha, g (g a ha) (g_mem a ha) = a) :
∏ x ∈ s, f x = 1 := by
classical
induction s using Finset.strongInduction with | H s ih => ?_
obtain rfl | ⟨x, hx⟩ := s.eq_empty_or_nonempty
· simp
have : {x, g x hx} ⊆ s := by simp [insert_subset_iff, hx, g_mem]
suffices h : ∏ x ∈ s \ {x, g x hx}, f x = 1 by
rw [← prod_sdiff this, h, one_mul]
cases eq_or_ne (g x hx) x with
| inl hx' => simpa [hx'] using hg₃ x hx
| inr hx' => grind
suffices h₃ : ∀ a (ha : a ∈ s \ {x, g x hx}), g a (sdiff_subset ha) ∈ s \ {x, g x hx} from
ih (s \ {x, g x hx}) (ssubset_iff.2 ⟨x, by simp [insert_subset_iff, hx]⟩) _
(by simp [hg₁]) (fun _ _ => hg₃ _ _) h₃ (fun _ _ => hg₄ _ _)
grind
/-- The difference with `Finset.prod_involution` is that the involution is a non-dependent function,
rather than being allowed to use membership of the domain of the product. -/
@[to_additive /-- The difference with `Finset.sum_involution` is that the involution is a
non-dependent function, rather than being allowed to use membership of the domain of the sum. -/]
lemma prod_ninvolution (g : ι → ι) (hg₁ : ∀ a, f a * f (g a) = 1) (hg₂ : ∀ a, f a ≠ 1 → g a ≠ a)
(g_mem : ∀ a, g a ∈ s) (hg₃ : ∀ a, g (g a) = a) : ∏ x ∈ s, f x = 1 :=
prod_involution (fun i _ => g i) (fun i _ => hg₁ i) (fun _ _ hi => hg₂ _ hi)
(fun i _ => g_mem i) (fun i _ => hg₃ i)
/-- The product of the composition of functions `f` and `g`, is the product over `b ∈ s.image g` of
`f b` to the power of the cardinality of the fibre of `b`. See also `Finset.prod_image`. -/
@[to_additive /-- The sum of the composition of functions `f` and `g`, is the sum over
`b ∈ s.image g` of `f b` times of the cardinality of the fibre of `b`. See also
`Finset.sum_image`. -/]
theorem prod_comp [DecidableEq κ] (f : κ → M) (g : ι → κ) :
∏ a ∈ s, f (g a) = ∏ b ∈ s.image g, f b ^ #{a ∈ s | g a = b} := by
simp_rw [← prod_const, prod_fiberwise_of_maps_to' fun _ ↦ mem_image_of_mem _]
/-- A product can be partitioned into a product of products, each equivalent under a setoid. -/
@[to_additive /-- A sum can be partitioned into a sum of sums, each equivalent under a setoid. -/]
theorem prod_partition (R : Setoid ι) [DecidableRel R.r] :
∏ x ∈ s, f x = ∏ xbar ∈ s.image (Quotient.mk _), ∏ y ∈ s with ⟦y⟧ = xbar, f y := by
refine (Finset.prod_image' f fun x _hx => ?_).symm
rfl
/-- If we can partition a product into subsets that cancel out, then the whole product cancels. -/
@[to_additive /-- If we can partition a sum into subsets that cancel out, then the whole sum
cancels. -/]
theorem prod_cancels_of_partition_cancels (R : Setoid ι) [DecidableRel R]
(h : ∀ x ∈ s, ∏ a ∈ s with R a x, f a = 1) : ∏ x ∈ s, f x = 1 := by
rw [prod_partition R, ← Finset.prod_eq_one]
intro xbar xbar_in_s
obtain ⟨x, x_in_s, rfl⟩ := mem_image.mp xbar_in_s
simp only [← Quotient.eq] at h
exact h x x_in_s
/-- If a product of a `Finset` of size at most 1 has a given value, so
do the terms in that product. -/
@[to_additive eq_of_card_le_one_of_sum_eq /-- If a sum of a `Finset` of size at most 1 has a given
value, so do the terms in that sum. -/]
theorem eq_of_card_le_one_of_prod_eq {s : Finset ι} (hc : #s ≤ 1) {f : ι → M} {b : M}
(h : ∏ x ∈ s, f x = b) : ∀ x ∈ s, f x = b := by
intro x hx
by_cases hc0 : #s = 0
· exact False.elim (card_ne_zero_of_mem hx hc0)
· have h1 : #s = 1 := le_antisymm hc (Nat.one_le_of_lt (Nat.pos_of_ne_zero hc0))
rw [card_eq_one] at h1
grind
/-- Taking a product over `s : Finset ι` is the same as multiplying the value on a single element
`f a` by the product of `s.erase a`.
See `Multiset.prod_map_erase` for the `Multiset` version. -/
@[to_additive /-- Taking a sum over `s : Finset ι` is the same as adding the value on a single
element `f a` to the sum over `s.erase a`.
See `Multiset.sum_map_erase` for the `Multiset` version. -/]
theorem mul_prod_erase [DecidableEq ι] (s : Finset ι) (f : ι → M) {a : ι} (h : a ∈ s) :
(f a * ∏ x ∈ s.erase a, f x) = ∏ x ∈ s, f x := by
rw [← prod_insert (notMem_erase a s), insert_erase h]
/-- A variant of `Finset.mul_prod_erase` with the multiplication swapped. -/
@[to_additive /-- A variant of `Finset.add_sum_erase` with the addition swapped. -/]
theorem prod_erase_mul [DecidableEq ι] (s : Finset ι) (f : ι → M) {a : ι} (h : a ∈ s) :
(∏ x ∈ s.erase a, f x) * f a = ∏ x ∈ s, f x := by rw [mul_comm, mul_prod_erase s f h]
/-- If a function applied at a point is 1, a product is unchanged by
removing that point, if present, from a `Finset`. -/
@[to_additive /-- If a function applied at a point is 0, a sum is unchanged by
removing that point, if present, from a `Finset`. -/]
theorem prod_erase [DecidableEq ι] (s : Finset ι) {f : ι → M} {a : ι} (h : f a = 1) :
∏ x ∈ s.erase a, f x = ∏ x ∈ s, f x := by
rw [← sdiff_singleton_eq_erase]
refine prod_subset sdiff_subset fun x hx hnx => ?_
grind
@[to_additive]
theorem prod_erase_lt_of_one_lt {κ : Type*} [DecidableEq ι] [CommMonoid κ] [LT κ]
[MulLeftStrictMono κ] {s : Finset ι} {d : ι} (hd : d ∈ s) {f : ι → κ}
(hdf : 1 < f d) : ∏ m ∈ s.erase d, f m < ∏ m ∈ s, f m := by
conv in ∏ m ∈ s, f m => rw [← Finset.insert_erase hd]
rw [Finset.prod_insert (Finset.notMem_erase d s)]
exact lt_mul_of_one_lt_left' _ hdf
/-- If a product is 1 and the function is 1 except possibly at one
point, it is 1 everywhere on the `Finset`. -/
@[to_additive /-- If a sum is 0 and the function is 0 except possibly at one
point, it is 0 everywhere on the `Finset`. -/]
theorem eq_one_of_prod_eq_one {s : Finset ι} {f : ι → M} {a : ι} (hp : ∏ x ∈ s, f x = 1)
(h1 : ∀ x ∈ s, x ≠ a → f x = 1) : ∀ x ∈ s, f x = 1 := by
intro x hx
classical
by_cases h : x = a
· rw [h]
rw [h] at hx
rw [← prod_subset (singleton_subset_iff.2 hx) fun t ht ha => h1 t ht (notMem_singleton.1 ha),
prod_singleton] at hp
exact hp
· exact h1 x hx h
@[to_additive]
lemma prod_mul_eq_prod_mul_of_exists {s : Finset ι} {f : ι → M} {b₁ b₂ : M}
(a : ι) (ha : a ∈ s) (h : f a * b₁ = f a * b₂) :
(∏ a ∈ s, f a) * b₁ = (∏ a ∈ s, f a) * b₂ := by
classical
rw [← insert_erase ha]
simp only [mem_erase, ne_eq, not_true_eq_false, false_and, not_false_eq_true, prod_insert]
rw [mul_assoc, mul_comm, mul_assoc, mul_comm b₁, h, ← mul_assoc, mul_comm _ (f a)]
@[to_additive]
lemma prod_filter_of_pairwise_eq_one [DecidableEq ι] {f : κ → ι} {g : ι → M} {n : κ} {I : Finset κ}
(hn : n ∈ I) (hf : (I : Set κ).Pairwise fun i j ↦ f i = f j → g (f i) = 1) :
∏ j ∈ I with f j = f n, g (f j) = g (f n) := by
classical
have h j (hj : j ∈ {i ∈ I | f i = f n}.erase n) : g (f j) = 1 := by
simp only [mem_erase, mem_filter] at hj
exact hf hj.2.1 hn hj.1 hj.2.2
rw [← mul_one (g (f n)), ← prod_eq_one h,
← mul_prod_erase {i ∈ I | f i = f n} (fun i ↦ g (f i)) <| mem_filter.mpr ⟨hn, by rfl⟩]
/-- A version of `Finset.prod_map` and `Finset.prod_image`, but we do not assume that `f` is
injective. Rather, we assume that the image of `f` on `I` only overlaps where `g (f i) = 1`.
The conclusion is the same as in `prod_image`. -/
@[to_additive (attr := simp)
/-- A version of `Finset.sum_map` and `Finset.sum_image`, but we do not assume that `f` is
injective. Rather, we assume that the image of `f` on `I` only overlaps where `g (f i) = 0`.
The conclusion is the same as in `sum_image`. -/]
lemma prod_image_of_pairwise_eq_one [DecidableEq ι] {f : κ → ι} {g : ι → M} {I : Finset κ}
(hf : (I : Set κ).Pairwise fun i j ↦ f i = f j → g (f i) = 1) :
∏ s ∈ I.image f, g s = ∏ i ∈ I, g (f i) := by
rw [prod_image']
exact fun n hnI => (prod_filter_of_pairwise_eq_one hnI hf).symm
/-- A version of `Finset.prod_map` and `Finset.prod_image`, but we do not assume that `f` is
injective. Rather, we assume that the images of `f` are disjoint on `I`, and `g ⊥ = 1`. The
conclusion is the same as in `prod_image`. -/
@[to_additive (attr := simp)
/-- A version of `Finset.sum_map` and `Finset.sum_image`, but we do not assume that `f` is
injective. Rather, we assume that the images of `f` are disjoint on `I`, and `g ⊥ = 0`. The
conclusion is the same as in `sum_image`. -/]
lemma prod_image_of_disjoint [DecidableEq ι] [PartialOrder ι] [OrderBot ι] {f : κ → ι} {g : ι → M}
(hg_bot : g ⊥ = 1) {I : Finset κ} (hf_disj : (I : Set κ).PairwiseDisjoint f) :
∏ s ∈ I.image f, g s = ∏ i ∈ I, g (f i) := by
refine prod_image_of_pairwise_eq_one <| hf_disj.imp fun i j hdisj hfij ↦ ?_
rw [Function.onFun, ← hfij, disjoint_self] at hdisj
rw [hdisj, hg_bot]
@[to_additive]
theorem prod_unique_nonempty [Unique ι] (s : Finset ι) (f : ι → M) (h : s.Nonempty) :
∏ x ∈ s, f x = f default := by
rw [h.eq_singleton_default, Finset.prod_singleton]
lemma prod_dvd_prod_of_dvd (f g : ι → M) (h : ∀ i ∈ s, f i ∣ g i) :
∏ i ∈ s, f i ∣ ∏ i ∈ s, g i :=
Multiset.prod_dvd_prod_of_dvd _ _ h
@[to_additive]
theorem prod_map_equiv (e : ι ≃ κ) : (s.map e).prod (f ∘ e.symm) = s.prod f := by simp
@[to_additive]
theorem prod_comp_equiv {f : κ → M} (e : ι ≃ κ) : s.prod (f ∘ e) = (s.map e).prod f := by simp
end CommMonoid
section CancelCommMonoid
variable [DecidableEq ι] [CancelCommMonoid M] {s t : Finset ι} {f : ι → M}
@[to_additive]
lemma prod_sdiff_eq_prod_sdiff_iff :
∏ i ∈ s \ t, f i = ∏ i ∈ t \ s, f i ↔ ∏ i ∈ s, f i = ∏ i ∈ t, f i :=
eq_comm.trans <| eq_iff_eq_of_mul_eq_mul <| by
rw [← prod_union disjoint_sdiff_self_left, ← prod_union disjoint_sdiff_self_left,
sdiff_union_self_eq_union, sdiff_union_self_eq_union, union_comm]
@[to_additive]
lemma prod_sdiff_ne_prod_sdiff_iff :
∏ i ∈ s \ t, f i ≠ ∏ i ∈ t \ s, f i ↔ ∏ i ∈ s, f i ≠ ∏ i ∈ t, f i :=
prod_sdiff_eq_prod_sdiff_iff.not
end CancelCommMonoid
section CommGroup
variable [CommGroup G] [DecidableEq ι] {f : ι → G}
@[to_additive]
lemma prod_insert_div (ha : a ∉ s) (f : ι → G) :
(∏ x ∈ insert a s, f x) / f a = ∏ x ∈ s, f x := by simp [ha]
@[to_additive (attr := simp)]
theorem prod_erase_eq_div {a : ι} (h : a ∈ s) : ∏ x ∈ s.erase a, f x = (∏ x ∈ s, f x) / f a := by
rw [eq_div_iff_mul_eq', prod_erase_mul _ _ h]
/-- A telescoping product along `{0, ..., n - 1}` of a commutative-group-valued function reduces to
the ratio of the last and first factors. -/
@[to_additive /-- A telescoping sum along `{0, ..., n - 1}` of a function valued in a commutative
additive group reduces to the difference of the last and first terms. -/]
lemma prod_range_div (f : ℕ → G) (n : ℕ) : (∏ i ∈ range n, f (i + 1) / f i) = f n / f 0 := by
apply prod_range_induction <;> simp
@[to_additive]
lemma prod_range_div' (f : ℕ → G) (n : ℕ) : (∏ i ∈ range n, f i / f (i + 1)) = f 0 / f n := by
apply prod_range_induction <;> simp
@[to_additive]
lemma eq_prod_range_div (f : ℕ → G) (n : ℕ) : f n = f 0 * ∏ i ∈ range n, f (i + 1) / f i := by
rw [prod_range_div, mul_div_cancel]
@[to_additive]
lemma eq_prod_range_div' (f : ℕ → G) (n : ℕ) :
f n = ∏ i ∈ range (n + 1), if i = 0 then f 0 else f i / f (i - 1) := by
conv_lhs => rw [Finset.eq_prod_range_div f]
simp [Finset.prod_range_succ', mul_comm]
@[to_additive]
lemma prod_range_add_div_prod_range (f : ℕ → G) (n m : ℕ) :
(∏ k ∈ range (n + m), f k) / ∏ k ∈ range n, f k = ∏ k ∈ Finset.range m, f (n + k) :=
div_eq_of_eq_mul' (prod_range_add f n m)
@[to_additive (attr := simp)]
lemma prod_sdiff_eq_div (h : s₁ ⊆ s₂) : ∏ x ∈ s₂ \ s₁, f x = (∏ x ∈ s₂, f x) / ∏ x ∈ s₁, f x := by
rw [eq_div_iff_mul_eq', prod_sdiff h]
@[to_additive]
theorem prod_sdiff_div_prod_sdiff :
(∏ x ∈ s₂ \ s₁, f x) / ∏ x ∈ s₁ \ s₂, f x = (∏ x ∈ s₂, f x) / ∏ x ∈ s₁, f x := by
simp [← Finset.prod_sdiff (@inf_le_left _ _ s₁ s₂), ← Finset.prod_sdiff (@inf_le_right _ _ s₁ s₂)]
end CommGroup
section OrderedSub
variable [AddCommMonoid M] [PartialOrder M] [Sub M] [OrderedSub M] [AddLeftMono M]
[AddLeftReflectLE M] [ExistsAddOfLE M]
/-- A telescoping sum along `{0, ..., n-1}` of an `ℕ`-valued function reduces to the difference of
the last and first terms when the function we are summing is monotone. -/
lemma sum_range_tsub {f : ℕ → M} (h : Monotone f) (n : ℕ) :
∑ i ∈ range n, (f (i + 1) - f i) = f n - f 0 := by
apply sum_range_induction
case base => apply tsub_eq_of_eq_add; rw [zero_add]
case step =>
intro n _
have h₁ : f n ≤ f (n + 1) := h (Nat.le_succ _)
have h₂ : f 0 ≤ f n := h (Nat.zero_le _)
rw [tsub_add_eq_add_tsub h₂, add_tsub_cancel_of_le h₁]
lemma sum_tsub_distrib (s : Finset ι) {f g : ι → M} (hfg : ∀ x ∈ s, g x ≤ f x) :
∑ x ∈ s, (f x - g x) = ∑ x ∈ s, f x - ∑ x ∈ s, g x := Multiset.sum_map_tsub _ hfg
end OrderedSub
section Nat
lemma card_eq_sum_ones (s : Finset ι) : #s = ∑ _ ∈ s, 1 := by simp
theorem sum_const_nat {m : ℕ} {f : ι → ℕ} (h₁ : ∀ x ∈ s, f x = m) : ∑ x ∈ s, f x = #s * m := by
rw [← Nat.nsmul_eq_mul, ← sum_const]
apply sum_congr rfl h₁
lemma sum_card_fiberwise_eq_card_filter {κ : Type*} [DecidableEq κ] (s : Finset ι) (t : Finset κ)
(g : ι → κ) : ∑ j ∈ t, #{i ∈ s | g i = j} = #{i ∈ s | g i ∈ t} := by
simpa only [card_eq_sum_ones] using sum_fiberwise_eq_sum_filter _ _ _ _
@[simp]
theorem card_disjiUnion (s : Finset ι) (t : ι → Finset M) (h) :
#(s.disjiUnion t h) = ∑ a ∈ s, #(t a) :=
Multiset.card_bind _ _
theorem card_biUnion [DecidableEq M] {t : ι → Finset M} (h : (s : Set ι).PairwiseDisjoint t) :
#(s.biUnion t) = ∑ u ∈ s, #(t u) := by simpa using sum_biUnion h (M := ℕ) (f := 1)
theorem card_biUnion_le [DecidableEq M] {s : Finset ι} {t : ι → Finset M} :
#(s.biUnion t) ≤ ∑ a ∈ s, #(t a) :=
haveI := Classical.decEq ι
Finset.induction_on s (by simp) fun a s has ih =>
calc
#((insert a s).biUnion t) ≤ #(t a) + #(s.biUnion t) := by
rw [biUnion_insert]; exact card_union_le ..
_ ≤ ∑ a ∈ insert a s, #(t a) := by grind
theorem card_eq_sum_card_fiberwise [DecidableEq M] {f : ι → M} {s : Finset ι} {t : Finset M}
(H : (s : Set ι).MapsTo f t) : #s = ∑ b ∈ t, #{a ∈ s | f a = b} := by
simp only [card_eq_sum_ones, sum_fiberwise_of_maps_to H]
theorem card_eq_sum_card_image [DecidableEq M] (f : ι → M) (s : Finset ι) :
#s = ∑ b ∈ s.image f, #{a ∈ s | f a = b} :=
card_eq_sum_card_fiberwise fun _ => mem_image_of_mem _
end Nat
end Finset
namespace Fintype
variable {ι κ ι : Type*} [Fintype ι] [Fintype κ]
open Finset
section CommMonoid
variable [CommMonoid M]
@[to_additive]
lemma prod_of_injective (e : ι → κ) (he : Injective e) (f : ι → M) (g : κ → M)
(h' : ∀ i ∉ Set.range e, g i = 1) (h : ∀ i, f i = g (e i)) : ∏ i, f i = ∏ j, g j :=
prod_of_injOn e he.injOn (by simp) (by simpa using h') (fun i _ ↦ h i)
@[to_additive]
lemma prod_fiberwise [DecidableEq κ] (g : ι → κ) (f : ι → M) :
∏ j, ∏ i : {i // g i = j}, f i = ∏ i, f i := by
rw [← Finset.prod_fiberwise _ g f]
congr with j
exact (prod_subtype _ (by simp) _).symm
@[to_additive]
lemma prod_fiberwise' [DecidableEq κ] (g : ι → κ) (f : κ → M) :
∏ j, ∏ _i : {i // g i = j}, f j = ∏ i, f (g i) := by
rw [← Finset.prod_fiberwise' _ g f]
congr with j
exact (prod_subtype _ (by simp) fun _ ↦ _).symm
@[to_additive]
theorem prod_unique [Unique ι] (f : ι → M) : ∏ x : ι, f x = f default := by
rw [univ_unique, prod_singleton]
@[to_additive]
theorem prod_subsingleton [Subsingleton ι] (f : ι → M) (a : ι) : ∏ x : ι, f x = f a := by
have : Unique ι := uniqueOfSubsingleton a
rw [prod_unique f, Subsingleton.elim default a]
@[to_additive] theorem prod_Prop (f : Prop → M) : ∏ p, f p = f True * f False := by simp
@[to_additive]
theorem prod_subtype_mul_prod_subtype (p : ι → Prop) (f : ι → M) [DecidablePred p] :
(∏ i : { x // p x }, f i) * ∏ i : { x // ¬p x }, f i = ∏ i, f i := by
classical
let s := { x | p x }.toFinset
rw [← Finset.prod_subtype s, ← Finset.prod_subtype sᶜ]
· exact Finset.prod_mul_prod_compl _ _
· simp [s]
· simp [s]
@[to_additive] lemma prod_subset {s : Finset ι} {f : ι → M} (h : ∀ i, f i ≠ 1 → i ∈ s) :
∏ i ∈ s, f i = ∏ i, f i :=
Finset.prod_subset s.subset_univ <| by simpa [not_imp_comm (a := _ ∈ s)]
end CommMonoid
end Fintype
namespace List
@[to_additive]
theorem prod_toFinset {M : Type*} [DecidableEq ι] [CommMonoid M] (f : ι → M) :
∀ {l : List ι} (_hl : l.Nodup), l.toFinset.prod f = (l.map f).prod
| [], _ => by simp
| a :: l, hl => by
let ⟨notMem, hl⟩ := List.nodup_cons.mp hl
simp [Finset.prod_insert (mt List.mem_toFinset.mp notMem), prod_toFinset _ hl]
@[simp]
theorem sum_toFinset_count_eq_length [DecidableEq ι] (l : List ι) :
∑ a ∈ l.toFinset, l.count a = l.length := by
simpa [List.map_const'] using (Finset.sum_list_map_count l fun _ => (1 : ℕ)).symm
end List
namespace Multiset
@[simp]
lemma mem_sum {a : M} {s : Finset ι} {m : ι → Multiset M} :
a ∈ ∑ i ∈ s, m i ↔ ∃ i ∈ s, a ∈ m i := by
induction s using Finset.cons_induction with grind
@[deprecated Multiset.mem_sum (since := "2025-08-24")]
theorem _root_.Finset.mem_sum {f : ι → Multiset M} (s : Finset ι) (b : M) :
(b ∈ ∑ x ∈ s, f x) ↔ ∃ a ∈ s, b ∈ f a := Multiset.mem_sum
variable [DecidableEq ι]
theorem toFinset_sum_count_eq (s : Multiset ι) : ∑ a ∈ s.toFinset, s.count a = card s := by
simpa using (Finset.sum_multiset_map_count s (fun _ => (1 : ℕ))).symm
@[simp] lemma sum_count_eq_card {s : Finset ι} {m : Multiset ι} (hms : ∀ a ∈ m, a ∈ s) :
∑ a ∈ s, m.count a = card m := by
rw [← toFinset_sum_count_eq, ← Finset.sum_filter_ne_zero]
congr with a
simpa using hms a
@[simp]
theorem toFinset_sum_count_nsmul_eq (s : Multiset ι) :
∑ a ∈ s.toFinset, s.count a • {a} = s := by
rw [← Finset.sum_multiset_map_count, Multiset.sum_map_singleton]
theorem exists_smul_of_dvd_count (s : Multiset ι) {k : ℕ}
(h : ∀ a : ι, a ∈ s → k ∣ Multiset.count a s) : ∃ u : Multiset ι, s = k • u := by
use ∑ a ∈ s.toFinset, (s.count a / k) • {a}
have h₂ :
(∑ x ∈ s.toFinset, k • (count x s / k) • ({x} : Multiset ι)) =
∑ x ∈ s.toFinset, count x s • {x} := by
apply Finset.sum_congr rfl
intro x hx
rw [← mul_nsmul', Nat.mul_div_cancel' (h x (mem_toFinset.mp hx))]
rw [← Finset.sum_nsmul, h₂, toFinset_sum_count_nsmul_eq]
@[to_additive]
theorem prod_sum {ι : Type*} [CommMonoid M] (f : ι → Multiset M) (s : Finset ι) :
(∑ x ∈ s, f x).prod = ∏ x ∈ s, (f x).prod := by
induction s using Finset.cons_induction with grind
end Multiset
@[to_additive (attr := simp)]
lemma IsUnit.prod_iff [CommMonoid M] {f : ι → M} :
IsUnit (∏ a ∈ s, f a) ↔ ∀ a ∈ s, IsUnit (f a) := by
induction s using Finset.cons_induction with grind
@[to_additive]
lemma IsUnit.prod_univ_iff [Fintype ι] [CommMonoid M] {f : ι → M} :
IsUnit (∏ a, f a) ↔ ∀ a, IsUnit (f a) := by simp
theorem nat_abs_sum_le (s : Finset ι) (f : ι → ℤ) :
(∑ i ∈ s, f i).natAbs ≤ ∑ i ∈ s, (f i).natAbs := by
induction s using Finset.cons_induction with grind |
.lake/packages/mathlib/Mathlib/Algebra/BigOperators/Group/Finset/Powerset.lean | import Mathlib.Algebra.BigOperators.Group.Finset.Basic
import Mathlib.Data.Finset.Powerset
/-!
# Big operators
In this file we prove theorems about products and sums over a `Finset.powerset`.
-/
variable {α β γ : Type*}
variable {s : Finset α} {a : α}
namespace Finset
variable [CommMonoid β]
/-- A product over all subsets of `s ∪ {x}` is obtained by multiplying the product over all subsets
of `s`, and over all subsets of `s` to which one adds `x`. -/
@[to_additive /-- A sum over all subsets of `s ∪ {x}` is obtained by summing the sum over all
subsets of `s`, and over all subsets of `s` to which one adds `x`. -/]
lemma prod_powerset_insert [DecidableEq α] (ha : a ∉ s) (f : Finset α → β) :
∏ t ∈ (insert a s).powerset, f t =
(∏ t ∈ s.powerset, f t) * ∏ t ∈ s.powerset, f (insert a t) := by
rw [powerset_insert, prod_union, prod_image]
· exact insert_erase_invOn.2.injOn.mono fun t ht ↦ notMem_mono (mem_powerset.1 ht) ha
· aesop (add simp [disjoint_left, insert_subset_iff])
/-- A product over all subsets of `s ∪ {x}` is obtained by multiplying the product over all subsets
of `s`, and over all subsets of `s` to which one adds `x`. -/
@[to_additive /-- A sum over all subsets of `s ∪ {x}` is obtained by summing the sum over all
subsets of `s`, and over all subsets of `s` to which one adds `x`. -/]
lemma prod_powerset_cons (ha : a ∉ s) (f : Finset α → β) :
∏ t ∈ (s.cons a ha).powerset, f t = (∏ t ∈ s.powerset, f t) *
∏ t ∈ s.powerset.attach, f (cons a t <| notMem_mono (mem_powerset.1 t.2) ha) := by
classical
simp_rw [cons_eq_insert]
rw [prod_powerset_insert ha, prod_attach _ fun t ↦ f (insert a t)]
/-- A product over `powerset s` is equal to the double product over sets of subsets of `s` with
`#s = k`, for `k = 0, ..., #s`. -/
@[to_additive /-- A sum over `powerset s` is equal to the double sum over sets of subsets of `s`
with `#s = k`, for `k = 0, ..., #s` -/]
lemma prod_powerset (s : Finset α) (f : Finset α → β) :
∏ t ∈ powerset s, f t = ∏ j ∈ range (#s + 1), ∏ t ∈ powersetCard j s, f t := by
rw [powerset_card_disjiUnion, prod_disjiUnion]
/-- A product over `Finset.powersetCard` which only depends on the size of the sets is constant. -/
@[to_additive
/-- A sum over `Finset.powersetCard` which only depends on the size of the sets is constant. -/]
lemma prod_powersetCard (n : ℕ) (s : Finset α) (f : ℕ → β) :
∏ t ∈ powersetCard n s, f #t = f n ^ (#s).choose n := by
rw [prod_eq_pow_card, card_powersetCard]; rintro a ha; rw [(mem_powersetCard.1 ha).2]
end Finset |
.lake/packages/mathlib/Mathlib/Algebra/BigOperators/Group/Finset/Defs.lean | import Mathlib.Algebra.Group.Equiv.Opposite
import Mathlib.Algebra.Group.TypeTags.Basic
import Mathlib.Algebra.BigOperators.Group.Multiset.Defs
import Mathlib.Data.Fintype.Sets
import Mathlib.Data.Multiset.Bind
/-!
# Big operators
In this file we define products and sums indexed by finite sets (specifically, `Finset`).
## Notation
We introduce the following notation.
Let `s` be a `Finset ι`, and `f : ι → β` a function.
* `∏ x ∈ s, f x` is notation for `Finset.prod s f` (assuming `β` is a `CommMonoid`)
* `∑ x ∈ s, f x` is notation for `Finset.sum s f` (assuming `β` is an `AddCommMonoid`)
* `∏ x, f x` is notation for `Finset.prod Finset.univ f`
(assuming `ι` is a `Fintype` and `β` is a `CommMonoid`)
* `∑ x, f x` is notation for `Finset.sum Finset.univ f`
(assuming `ι` is a `Fintype` and `β` is an `AddCommMonoid`)
* `∏ x ∈ s with p x, f x` is notation for `Finset.prod (Finset.filter p s) f`.
* `∑ x ∈ s with p x, f x` is notation for `Finset.sum (Finset.filter p s) f`.
* `∏ (x ∈ s) (y ∈ t), f x y` is notation for `Finset.prod (s ×ˢ t) (fun ⟨x, y⟩ ↦ f x y)`.
* `∑ (x ∈ s) (y ∈ t), f x y` is notation for `Finset.sum (s ×ˢ t) (fun ⟨x, y⟩ ↦ f x y)`.
* Other supported binders: `x < n`, `x > n`, `x ≤ n`, `x ≥ n`, `x ≠ n`, `x ∉ s`, `x + y = n`
## Implementation Notes
The first arguments in all definitions and lemmas is the codomain of the function of the big
operator. This is necessary for the heuristic in `@[to_additive]`.
See the documentation of `to_additive.attr` for more information.
-/
-- TODO
-- assert_not_exists AddCommMonoidWithOne
assert_not_exists MonoidWithZero
assert_not_exists MulAction
assert_not_exists IsOrderedMonoid
variable {ι κ M N G α : Type*}
open Fin Function
namespace Finset
/-- `∏ x ∈ s, f x` is the product of `f x` as `x` ranges over the elements of the finite set `s`.
When the index type is a `Fintype`, the notation `∏ x, f x`, is a shorthand for
`∏ x ∈ Finset.univ, f x`. -/
@[to_additive /-- `∑ x ∈ s, f x` is the sum of `f x` as `x` ranges over the elements
of the finite set `s`.
When the index type is a `Fintype`, the notation `∑ x, f x`, is a shorthand for
`∑ x ∈ Finset.univ, f x`. -/]
protected def prod [CommMonoid M] (s : Finset ι) (f : ι → M) : M :=
(s.1.map f).prod
@[to_additive (attr := simp)]
theorem prod_mk [CommMonoid M] (s : Multiset ι) (hs : s.Nodup) (f : ι → M) :
(⟨s, hs⟩ : Finset ι).prod f = (s.map f).prod :=
rfl
@[to_additive (attr := simp)]
theorem prod_val [CommMonoid M] (s : Finset M) : s.1.prod = s.prod id := by
rw [Finset.prod, Multiset.map_id]
end Finset
library_note2 «operator precedence of big operators» /--
There is no established mathematical convention
for the operator precedence of big operators like `∏` and `∑`.
We will have to make a choice.
Online discussions, such as https://math.stackexchange.com/q/185538/30839
seem to suggest that `∏` and `∑` should have the same precedence,
and that this should be somewhere between `*` and `+`.
The latter have precedence levels `70` and `65` respectively,
and we therefore choose the level `67`.
In practice, this means that parentheses should be placed as follows:
```lean
∑ k ∈ K, (a k + b k) = ∑ k ∈ K, a k + ∑ k ∈ K, b k →
∏ k ∈ K, a k * b k = (∏ k ∈ K, a k) * (∏ k ∈ K, b k)
```
(Example taken from page 490 of Knuth's *Concrete Mathematics*.)
-/
namespace BigOperators
open Batteries.ExtendedBinder Lean Meta
-- TODO: contribute this modification back to `extBinder`
/-- A `bigOpBinder` is like an `extBinder` and has the form `x`, `x : ty`, or `x pred`
where `pred` is a `binderPred` like `< 2`.
Unlike `extBinder`, `x` is a term. -/
syntax bigOpBinder := term:max ((" : " term) <|> binderPred)?
/-- A BigOperator binder in parentheses -/
syntax bigOpBinderParenthesized := " (" bigOpBinder ")"
/-- A list of parenthesized binders -/
syntax bigOpBinderCollection := bigOpBinderParenthesized+
/-- A single (unparenthesized) binder, or a list of parenthesized binders -/
syntax bigOpBinders := bigOpBinderCollection <|> (ppSpace bigOpBinder)
/-- Collects additional binder/Finset pairs for the given `bigOpBinder`.
Note: this is not extensible at the moment, unlike the usual `bigOpBinder` expansions. -/
def processBigOpBinder (processed : (Array (Term × Term))) (binder : TSyntax ``bigOpBinder) :
MacroM (Array (Term × Term)) :=
set_option hygiene false in
withRef binder do
match binder with
| `(bigOpBinder| $x:term) =>
match x with
| `(($a + $b = $n)) => -- Maybe this is too cute.
return processed |>.push (← `(⟨$a, $b⟩), ← `(Finset.Nat.antidiagonal $n))
| _ => return processed |>.push (x, ← ``(Finset.univ))
| `(bigOpBinder| $x : $t) => return processed |>.push (x, ← ``((Finset.univ : Finset $t)))
| `(bigOpBinder| $x ∈ $s) => return processed |>.push (x, ← `(finset% $s))
| `(bigOpBinder| $x ∉ $s) => return processed |>.push (x, ← `(finset% $sᶜ))
| `(bigOpBinder| $x ≠ $n) => return processed |>.push (x, ← `(Finset.univ.erase $n))
| `(bigOpBinder| $x < $n) => return processed |>.push (x, ← `(Finset.Iio $n))
| `(bigOpBinder| $x ≤ $n) => return processed |>.push (x, ← `(Finset.Iic $n))
| `(bigOpBinder| $x > $n) => return processed |>.push (x, ← `(Finset.Ioi $n))
| `(bigOpBinder| $x ≥ $n) => return processed |>.push (x, ← `(Finset.Ici $n))
| _ => Macro.throwUnsupported
/-- Collects the binder/Finset pairs for the given `bigOpBinders`. -/
def processBigOpBinders (binders : TSyntax ``bigOpBinders) :
MacroM (Array (Term × Term)) :=
match binders with
| `(bigOpBinders| $b:bigOpBinder) => processBigOpBinder #[] b
| `(bigOpBinders| $[($bs:bigOpBinder)]*) => bs.foldlM processBigOpBinder #[]
| _ => Macro.throwUnsupported
/-- Collects the binderIdents into a `⟨...⟩` expression. -/
def bigOpBindersPattern (processed : Array (Term × Term)) : MacroM Term := do
let ts := processed.map Prod.fst
if h : ts.size = 1 then
return ts[0]
else
`(⟨$ts,*⟩)
/-- Collects the terms into a product of sets. -/
def bigOpBindersProd (processed : Array (Term × Term)) : MacroM Term := do
if h₀ : processed.size = 0 then
`((Finset.univ : Finset Unit))
else if h₁ : processed.size = 1 then
return processed[0].2
else
processed.foldrM (fun s p => `(SProd.sprod $(s.2) $p)) processed.back.2
(start := processed.size - 1)
/--
- `∑ x, f x` is notation for `Finset.sum Finset.univ f`. It is the sum of `f x`,
where `x` ranges over the finite domain of `f`.
- `∑ x ∈ s, f x` is notation for `Finset.sum s f`. It is the sum of `f x`,
where `x` ranges over the finite set `s` (either a `Finset` or a `Set` with a `Fintype` instance).
- `∑ x ∈ s with p x, f x` is notation for `Finset.sum (Finset.filter p s) f`.
- `∑ x ∈ s with h : p x, f x h` is notation for `Finset.sum s fun x ↦ if h : p x then f x h else 0`.
- `∑ (x ∈ s) (y ∈ t), f x y` is notation for `Finset.sum (s ×ˢ t) (fun ⟨x, y⟩ ↦ f x y)`.
These support destructuring, for example `∑ ⟨x, y⟩ ∈ s ×ˢ t, f x y`.
Notation: `"∑" bigOpBinders* (" with" (ident ":")? term)? "," term` -/
syntax (name := bigsum)
"∑ " bigOpBinders (" with " atomic(binderIdent " : ")? term)? ", " term:67 : term
/--
- `∏ x, f x` is notation for `Finset.prod Finset.univ f`. It is the product of `f x`,
where `x` ranges over the finite domain of `f`.
- `∏ x ∈ s, f x` is notation for `Finset.prod s f`. It is the product of `f x`,
where `x` ranges over the finite set `s` (either a `Finset` or a `Set` with a `Fintype` instance).
- `∏ x ∈ s with p x, f x` is notation for `Finset.prod (Finset.filter p s) f`.
- `∏ x ∈ s with h : p x, f x h` is notation for
`Finset.prod s fun x ↦ if h : p x then f x h else 1`.
- `∏ (x ∈ s) (y ∈ t), f x y` is notation for `Finset.prod (s ×ˢ t) (fun ⟨x, y⟩ ↦ f x y)`.
These support destructuring, for example `∏ ⟨x, y⟩ ∈ s ×ˢ t, f x y`.
Notation: `"∏" bigOpBinders* ("with" (ident ":")? term)? "," term` -/
syntax (name := bigprod)
"∏ " bigOpBinders (" with " atomic(binderIdent " : ")? term)? ", " term:67 : term
macro_rules (kind := bigsum)
| `(∑ $bs:bigOpBinders $[with $[$hx??:binderIdent :]? $p?:term]?, $v) => do
let processed ← processBigOpBinders bs
let x ← bigOpBindersPattern processed
let s ← bigOpBindersProd processed
-- `a` is interpreted as the filtering proposition, unless `b` exists, in which case `a` is the
-- proof and `b` is the filtering proposition
match hx??, p? with
| some (some hx), some p =>
`(Finset.sum $s fun $x ↦ if $hx : $p then $v else 0)
| _, some p => `(Finset.sum (Finset.filter (fun $x ↦ $p) $s) (fun $x ↦ $v))
| _, none => `(Finset.sum $s (fun $x ↦ $v))
macro_rules (kind := bigprod)
| `(∏ $bs:bigOpBinders $[with $[$hx??:binderIdent :]? $p?:term]?, $v) => do
let processed ← processBigOpBinders bs
let x ← bigOpBindersPattern processed
let s ← bigOpBindersProd processed
-- `a` is interpreted as the filtering proposition, unless `b` exists, in which case `a` is the
-- proof and `b` is the filtering proposition
match hx??, p? with
| some (some hx), some p =>
`(Finset.prod $s fun $x ↦ if $hx : $p then $v else 1)
| _, some p => `(Finset.prod (Finset.filter (fun $x ↦ $p) $s) (fun $x ↦ $v))
| _, none => `(Finset.prod $s (fun $x ↦ $v))
open PrettyPrinter.Delaborator SubExpr
open scoped Batteries.ExtendedBinder
/-- The possibilities we distinguish to delaborate the finset indexing a big operator:
* `finset s` corresponds to `∑ x ∈ s, f x`
* `univ` corresponds to `∑ x, f x`
* `filter s p` corresponds to `∑ x ∈ s with p x, f x`
* `filterUniv p` corresponds to `∑ x with p x, f x`
-/
private inductive FinsetResult where
| finset (s : Term)
| univ
| filter (s : Term) (p : Term)
| filterUniv (p : Term)
/-- Delaborates a finset indexing a big operator. In case it is a `Finset.filter`, `i` is used for
the binder name. -/
private def delabFinsetArg (i : Ident) : DelabM FinsetResult := do
let s ← getExpr
if s.isAppOfArity ``Finset.univ 2 then
return .univ
else if s.isAppOfArity ``Finset.filter 4 then
let #[_, _, _, t] := s.getAppArgs | failure
let p ←
withNaryArg 1 do
if (← getExpr).isLambda then
withBindingBody i.getId delab
else
let p ← delab
return (← `($p $i))
if t.isAppOfArity ``Finset.univ 2 then
return .filterUniv p
else
let ss ← withNaryArg 3 delab
return .filter ss p
else
let ss ← delab
return .finset ss
/-- Delaborator for `Finset.prod`. The `pp.funBinderTypes` option controls whether
to show the domain type when the product is over `Finset.univ`. -/
@[app_delab Finset.prod] def delabFinsetProd : Delab :=
whenPPOption getPPNotation <| withOverApp 5 <| do
let #[_, _, _, _, f] := (← getExpr).getAppArgs | failure
guard f.isLambda
let ppDomain ← getPPOption getPPFunBinderTypes
let (i, body) ← withAppArg <| withBindingBodyUnusedName fun i => do
return (⟨i⟩, ← delab)
let res ← withNaryArg 3 <| delabFinsetArg i
match res with
| .finset ss => `(∏ $i:ident ∈ $ss, $body)
| .univ =>
let binder ←
if ppDomain then
let ty ← withNaryArg 0 delab
`(bigOpBinder| $i:ident : $ty)
else
`(bigOpBinder| $i:ident)
`(∏ $binder:bigOpBinder, $body)
| .filter ss p =>
`(∏ $i:ident ∈ $ss with $p, $body)
| .filterUniv p =>
let binder ←
if ppDomain then
let ty ← withNaryArg 0 delab
`(bigOpBinder| $i:ident : $ty)
else
`(bigOpBinder| $i:ident)
`(∏ $binder:bigOpBinder with $p, $body)
/-- Delaborator for `Finset.sum`. The `pp.funBinderTypes` option controls whether
to show the domain type when the sum is over `Finset.univ`. -/
@[app_delab Finset.sum] def delabFinsetSum : Delab :=
whenPPOption getPPNotation <| withOverApp 5 <| do
let #[_, _, _, _, f] := (← getExpr).getAppArgs | failure
guard f.isLambda
let ppDomain ← getPPOption getPPFunBinderTypes
let (i, body) ← withAppArg <| withBindingBodyUnusedName fun i => do
return ((⟨i⟩ : Ident), ← delab)
let res ← withNaryArg 3 <| delabFinsetArg i
match res with
| .finset ss => `(∑ $i:ident ∈ $ss, $body)
| .univ =>
let binder ←
if ppDomain then
let ty ← withNaryArg 0 delab
`(bigOpBinder| $i:ident : $ty)
else
`(bigOpBinder| $i:ident)
`(∑ $binder:bigOpBinder, $body)
| .filter ss p =>
`(∑ $i:ident ∈ $ss with $p, $body)
| .filterUniv p =>
let binder ←
if ppDomain then
let ty ← withNaryArg 0 delab
`(bigOpBinder| $i:ident : $ty)
else
`(bigOpBinder| $i:ident)
`(∑ $binder:bigOpBinder with $p, $body)
end BigOperators
namespace Finset
variable {s s₁ s₂ : Finset ι} {a : ι} {f g : ι → M}
@[to_additive]
theorem prod_eq_multiset_prod [CommMonoid M] (s : Finset ι) (f : ι → M) :
∏ x ∈ s, f x = (s.1.map f).prod :=
rfl
@[to_additive (attr := simp)]
lemma prod_map_val [CommMonoid M] (s : Finset ι) (f : ι → M) : (s.1.map f).prod = ∏ a ∈ s, f a :=
rfl
@[simp]
theorem sum_multiset_singleton (s : Finset ι) : ∑ a ∈ s, {a} = s.val := by
simp only [sum_eq_multiset_sum, Multiset.sum_map_singleton]
end Finset
@[to_additive (attr := simp)]
theorem map_prod [CommMonoid M] [CommMonoid N] {G : Type*} [FunLike G M N] [MonoidHomClass G M N]
(g : G) (f : ι → M) (s : Finset ι) : g (∏ x ∈ s, f x) = ∏ x ∈ s, g (f x) := by
simp only [Finset.prod_eq_multiset_prod, map_multiset_prod, Multiset.map_map]; rfl
variable {s s₁ s₂ : Finset ι} {a : ι} {f g : ι → M}
namespace Finset
section CommMonoid
variable [CommMonoid M]
@[to_additive (attr := simp)]
theorem prod_empty : ∏ x ∈ ∅, f x = 1 :=
rfl
/-- Variant of `prod_empty` not applied to a function. -/
@[to_additive (attr := grind =)]
theorem prod_empty' : Finset.prod (∅ : Finset ι) = fun (_ : ι → M) => 1 :=
rfl
@[to_additive]
theorem prod_of_isEmpty [IsEmpty ι] (s : Finset ι) : ∏ i ∈ s, f i = 1 := by
rw [eq_empty_of_isEmpty s, prod_empty]
@[to_additive (attr := simp)]
theorem prod_const_one : (∏ _x ∈ s, (1 : M)) = 1 := by
simp only [Finset.prod, Multiset.map_const', Multiset.prod_replicate, one_pow]
@[to_additive (attr := simp)]
theorem prod_map (s : Finset ι) (e : ι ↪ κ) (f : κ → M) :
∏ x ∈ s.map e, f x = ∏ x ∈ s, f (e x) := by
rw [Finset.prod, Finset.map_val, Multiset.map_map]; rfl
/-- Variant of `prod_map` not applied to a function. -/
@[to_additive (attr := grind =)]
theorem prod_map' (s : Finset ι) (e : ι ↪ κ) :
Finset.prod (s.map e) = fun (f : κ → M) => ∏ x ∈ s, f (e x) := by
funext f
simp
section ToList
@[to_additive (attr := simp, grind =)]
theorem prod_map_toList (s : Finset ι) (f : ι → M) : (s.toList.map f).prod = s.prod f := by
rw [Finset.prod, ← Multiset.prod_coe, ← Multiset.map_coe, Finset.coe_toList]
@[to_additive (attr := simp, grind =)]
theorem prod_toList {M : Type*} [CommMonoid M] (s : Finset M) :
s.toList.prod = ∏ x ∈ s, x := by
simpa using s.prod_map_toList id
end ToList
@[to_additive]
theorem _root_.Equiv.Perm.prod_comp (σ : Equiv.Perm ι) (s : Finset ι) (f : ι → M)
(hs : { a | σ a ≠ a } ⊆ s) : (∏ x ∈ s, f (σ x)) = ∏ x ∈ s, f x := by
convert (prod_map s σ.toEmbedding f).symm
exact (map_perm hs).symm
@[to_additive]
theorem _root_.Equiv.Perm.prod_comp' (σ : Equiv.Perm ι) (s : Finset ι) (f : ι → ι → M)
(hs : { a | σ a ≠ a } ⊆ s) : (∏ x ∈ s, f (σ x) x) = ∏ x ∈ s, f x (σ.symm x) := by
convert σ.prod_comp s (fun x => f x (σ.symm x)) hs
rw [Equiv.symm_apply_apply]
end CommMonoid
end Finset
namespace Finset
section CommMonoid
variable [CommMonoid M]
section bij
variable {s : Finset ι} {t : Finset κ} {f : ι → M} {g : κ → M}
/-- Reorder a product.
The difference with `Finset.prod_bij'` is that the bijection is specified as a surjective injection,
rather than by an inverse function.
The difference with `Finset.prod_nbij` is that the bijection is allowed to use membership of the
domain of the product, rather than being a non-dependent function. -/
@[to_additive /-- Reorder a sum.
The difference with `Finset.sum_bij'` is that the bijection is specified as a surjective injection,
rather than by an inverse function.
The difference with `Finset.sum_nbij` is that the bijection is allowed to use membership of the
domain of the sum, rather than being a non-dependent function. -/]
theorem prod_bij (i : ∀ a ∈ s, κ) (hi : ∀ a ha, i a ha ∈ t)
(i_inj : ∀ a₁ ha₁ a₂ ha₂, i a₁ ha₁ = i a₂ ha₂ → a₁ = a₂)
(i_surj : ∀ b ∈ t, ∃ a ha, i a ha = b) (h : ∀ a ha, f a = g (i a ha)) :
∏ x ∈ s, f x = ∏ x ∈ t, g x :=
congr_arg Multiset.prod (Multiset.map_eq_map_of_bij_of_nodup f g s.2 t.2 i hi i_inj i_surj h)
/-- Reorder a product.
The difference with `Finset.prod_bij` is that the bijection is specified with an inverse, rather
than as a surjective injection.
The difference with `Finset.prod_nbij'` is that the bijection and its inverse are allowed to use
membership of the domains of the products, rather than being non-dependent functions. -/
@[to_additive /-- Reorder a sum.
The difference with `Finset.sum_bij` is that the bijection is specified with an inverse, rather than
as a surjective injection.
The difference with `Finset.sum_nbij'` is that the bijection and its inverse are allowed to use
membership of the domains of the sums, rather than being non-dependent functions. -/]
theorem prod_bij' (i : ∀ a ∈ s, κ) (j : ∀ a ∈ t, ι) (hi : ∀ a ha, i a ha ∈ t)
(hj : ∀ a ha, j a ha ∈ s) (left_inv : ∀ a ha, j (i a ha) (hi a ha) = a)
(right_inv : ∀ a ha, i (j a ha) (hj a ha) = a) (h : ∀ a ha, f a = g (i a ha)) :
∏ x ∈ s, f x = ∏ x ∈ t, g x := by
refine prod_bij i hi (fun a1 h1 a2 h2 eq ↦ ?_) (fun b hb ↦ ⟨_, hj b hb, right_inv b hb⟩) h
rw [← left_inv a1 h1, ← left_inv a2 h2]
simp only [eq]
/-- Reorder a product.
The difference with `Finset.prod_nbij'` is that the bijection is specified as a surjective
injection, rather than by an inverse function.
The difference with `Finset.prod_bij` is that the bijection is a non-dependent function, rather than
being allowed to use membership of the domain of the product. -/
@[to_additive /-- Reorder a sum.
The difference with `Finset.sum_nbij'` is that the bijection is specified as a surjective injection,
rather than by an inverse function.
The difference with `Finset.sum_bij` is that the bijection is a non-dependent function, rather than
being allowed to use membership of the domain of the sum. -/]
lemma prod_nbij (i : ι → κ) (hi : ∀ a ∈ s, i a ∈ t) (i_inj : (s : Set ι).InjOn i)
(i_surj : (s : Set ι).SurjOn i t) (h : ∀ a ∈ s, f a = g (i a)) :
∏ x ∈ s, f x = ∏ x ∈ t, g x :=
prod_bij (fun a _ ↦ i a) hi i_inj (by simpa using i_surj) h
/-- Reorder a product.
The difference with `Finset.prod_nbij` is that the bijection is specified with an inverse, rather
than as a surjective injection.
The difference with `Finset.prod_bij'` is that the bijection and its inverse are non-dependent
functions, rather than being allowed to use membership of the domains of the products.
The difference with `Finset.prod_equiv` is that bijectivity is only required to hold on the domains
of the products, rather than on the entire types.
-/
@[to_additive /-- Reorder a sum.
The difference with `Finset.sum_nbij` is that the bijection is specified with an inverse, rather
than as a surjective injection.
The difference with `Finset.sum_bij'` is that the bijection and its inverse are non-dependent
functions, rather than being allowed to use membership of the domains of the sums.
The difference with `Finset.sum_equiv` is that bijectivity is only required to hold on the domains
of the sums, rather than on the entire types. -/]
lemma prod_nbij' (i : ι → κ) (j : κ → ι) (hi : ∀ a ∈ s, i a ∈ t) (hj : ∀ a ∈ t, j a ∈ s)
(left_inv : ∀ a ∈ s, j (i a) = a) (right_inv : ∀ a ∈ t, i (j a) = a)
(h : ∀ a ∈ s, f a = g (i a)) : ∏ x ∈ s, f x = ∏ x ∈ t, g x :=
prod_bij' (fun a _ ↦ i a) (fun b _ ↦ j b) hi hj left_inv right_inv h
/-- Specialization of `Finset.prod_nbij'` that automatically fills in most arguments.
See `Fintype.prod_equiv` for the version where `s` and `t` are `univ`. -/
@[to_additive /-- Specialization of `Finset.sum_nbij'` that automatically fills in most arguments.
See `Fintype.sum_equiv` for the version where `s` and `t` are `univ`. -/]
lemma prod_equiv (e : ι ≃ κ) (hst : ∀ i, i ∈ s ↔ e i ∈ t) (hfg : ∀ i ∈ s, f i = g (e i)) :
∏ i ∈ s, f i = ∏ i ∈ t, g i := by refine prod_nbij' e e.symm ?_ ?_ ?_ ?_ hfg <;> simp [hst]
/-- Specialization of `Finset.prod_bij` that automatically fills in most arguments.
See `Fintype.prod_bijective` for the version where `s` and `t` are `univ`. -/
@[to_additive /-- Specialization of `Finset.sum_bij` that automatically fills in most arguments.
See `Fintype.sum_bijective` for the version where `s` and `t` are `univ`. -/]
lemma prod_bijective (e : ι → κ) (he : e.Bijective) (hst : ∀ i, i ∈ s ↔ e i ∈ t)
(hfg : ∀ i ∈ s, f i = g (e i)) :
∏ i ∈ s, f i = ∏ i ∈ t, g i := prod_equiv (.ofBijective e he) hst hfg
end bij
@[to_additive]
theorem prod_hom_rel [CommMonoid N] {r : M → N → Prop} {f : ι → M} {g : ι → N} {s : Finset ι}
(h₁ : r 1 1) (h₂ : ∀ a b c, r b c → r (f a * b) (g a * c)) :
r (∏ x ∈ s, f x) (∏ x ∈ s, g x) := by
delta Finset.prod
apply Multiset.prod_hom_rel <;> assumption
variable (f s)
@[to_additive]
theorem prod_coe_sort_eq_attach (f : s → M) : ∏ i : s, f i = ∏ i ∈ s.attach, f i :=
rfl
variable {f s}
@[to_additive]
theorem prod_ite_index (p : Prop) [Decidable p] (s t : Finset ι) (f : ι → M) :
∏ x ∈ if p then s else t, f x = if p then ∏ x ∈ s, f x else ∏ x ∈ t, f x :=
apply_ite (fun s => ∏ x ∈ s, f x) _ _ _
@[to_additive (attr := simp)]
theorem prod_ite_irrel (p : Prop) [Decidable p] (s : Finset ι) (f g : ι → M) :
∏ x ∈ s, (if p then f x else g x) = if p then ∏ x ∈ s, f x else ∏ x ∈ s, g x := by
split_ifs with h <;> rfl
@[to_additive (attr := simp)]
theorem prod_dite_irrel (p : Prop) [Decidable p] (s : Finset ι) (f : p → ι → M) (g : ¬p → ι → M) :
∏ x ∈ s, (if h : p then f h x else g h x) =
if h : p then ∏ x ∈ s, f h x else ∏ x ∈ s, g h x := by
split_ifs with h <;> rfl
@[to_additive]
theorem ite_prod_one (p : Prop) [Decidable p] (s : Finset ι) (f : ι → M) :
(if p then (∏ x ∈ s, f x) else 1) = ∏ x ∈ s, if p then f x else 1 := by
simp only [prod_ite_irrel, prod_const_one]
@[to_additive]
theorem ite_one_prod (p : Prop) [Decidable p] (s : Finset ι) (f : ι → M) :
(if p then 1 else (∏ x ∈ s, f x)) = ∏ x ∈ s, if p then 1 else f x := by
simp only [prod_ite_irrel, prod_const_one]
@[to_additive]
theorem nonempty_of_prod_ne_one (h : ∏ x ∈ s, f x ≠ 1) : s.Nonempty :=
s.eq_empty_or_nonempty.elim (fun H => False.elim <| h <| H.symm ▸ prod_empty) id
@[to_additive]
theorem prod_range_zero (f : ℕ → M) : ∏ k ∈ range 0, f k = 1 := by rw [range_zero, prod_empty]
open List
theorem sum_filter_count_eq_countP [DecidableEq ι] (p : ι → Prop) [DecidablePred p] (l : List ι) :
∑ x ∈ l.toFinset with p x, l.count x = l.countP p := by
simp [Finset.sum, sum_map_count_dedup_filter_eq_countP p l]
open Multiset
@[to_additive]
theorem prod_mem_multiset [DecidableEq ι] (m : Multiset ι) (f : { x // x ∈ m } → M) (g : ι → M)
(hfg : ∀ x, f x = g x) : ∏ x : { x // x ∈ m }, f x = ∏ x ∈ m.toFinset, g x := by
refine prod_bij' (fun x _ ↦ x) (fun x hx ↦ ⟨x, Multiset.mem_toFinset.1 hx⟩) ?_ ?_ ?_ ?_ ?_ <;>
simp [hfg]
/-- To prove a property of a product, it suffices to prove that
the property is multiplicative and holds on factors. -/
@[to_additive /-- To prove a property of a sum, it suffices to prove that
the property is additive and holds on summands. -/]
theorem prod_induction {M : Type*} [CommMonoid M] (f : ι → M) (p : M → Prop)
(hom : ∀ a b, p a → p b → p (a * b)) (unit : p 1) (base : ∀ x ∈ s, p <| f x) :
p <| ∏ x ∈ s, f x :=
Multiset.prod_induction _ _ hom unit (Multiset.forall_mem_map_iff.mpr base)
/-- To prove a property of a product, it suffices to prove that
the property is multiplicative and holds on factors. -/
@[to_additive /-- To prove a property of a sum, it suffices to prove that
the property is additive and holds on summands. -/]
theorem prod_induction_nonempty {M : Type*} [CommMonoid M] (f : ι → M) (p : M → Prop)
(hom : ∀ a b, p a → p b → p (a * b)) (nonempty : s.Nonempty) (base : ∀ x ∈ s, p <| f x) :
p <| ∏ x ∈ s, f x :=
Multiset.prod_induction_nonempty p hom (by simp [nonempty_iff_ne_empty.mp nonempty])
(Multiset.forall_mem_map_iff.mpr base)
@[to_additive]
theorem prod_pow (s : Finset ι) (n : ℕ) (f : ι → M) : ∏ x ∈ s, f x ^ n = (∏ x ∈ s, f x) ^ n :=
Multiset.prod_map_pow
theorem prod_dvd_prod_of_subset {ι M : Type*} [CommMonoid M] (s t : Finset ι) (f : ι → M)
(h : s ⊆ t) : (∏ i ∈ s, f i) ∣ ∏ i ∈ t, f i :=
Multiset.prod_dvd_prod_of_le <| Multiset.map_le_map <| by simpa
end CommMonoid
section Opposite
open MulOpposite
/-- Moving to the opposite additive commutative monoid commutes with summing. -/
@[simp]
theorem op_sum [AddCommMonoid M] {s : Finset ι} (f : ι → M) :
op (∑ x ∈ s, f x) = ∑ x ∈ s, op (f x) :=
map_sum (opAddEquiv : M ≃+ Mᵐᵒᵖ) _ _
@[simp]
theorem unop_sum [AddCommMonoid M] {s : Finset ι} (f : ι → Mᵐᵒᵖ) :
unop (∑ x ∈ s, f x) = ∑ x ∈ s, unop (f x) :=
map_sum (opAddEquiv : M ≃+ Mᵐᵒᵖ).symm _ _
end Opposite
section DivisionCommMonoid
variable [DivisionCommMonoid G]
@[to_additive (attr := simp)]
theorem prod_inv_distrib (f : ι → G) : (∏ x ∈ s, (f x)⁻¹) = (∏ x ∈ s, f x)⁻¹ :=
Multiset.prod_map_inv
@[to_additive (attr := simp)]
theorem prod_div_distrib (f g : ι → G) : ∏ x ∈ s, f x / g x = (∏ x ∈ s, f x) / ∏ x ∈ s, g x :=
Multiset.prod_map_div
@[to_additive]
theorem prod_zpow (f : ι → G) (s : Finset ι) (n : ℤ) : ∏ a ∈ s, f a ^ n = (∏ a ∈ s, f a) ^ n :=
Multiset.prod_map_zpow
end DivisionCommMonoid
theorem sum_nat_mod (s : Finset ι) (n : ℕ) (f : ι → ℕ) :
(∑ i ∈ s, f i) % n = (∑ i ∈ s, f i % n) % n :=
(Multiset.sum_nat_mod _ _).trans <| by rw [Finset.sum, Multiset.map_map]; rfl
theorem prod_nat_mod (s : Finset ι) (n : ℕ) (f : ι → ℕ) :
(∏ i ∈ s, f i) % n = (∏ i ∈ s, f i % n) % n :=
(Multiset.prod_nat_mod _ _).trans <| by rw [Finset.prod, Multiset.map_map]; rfl
theorem sum_int_mod (s : Finset ι) (n : ℤ) (f : ι → ℤ) :
(∑ i ∈ s, f i) % n = (∑ i ∈ s, f i % n) % n :=
(Multiset.sum_int_mod _ _).trans <| by rw [Finset.sum, Multiset.map_map]; rfl
theorem prod_int_mod (s : Finset ι) (n : ℤ) (f : ι → ℤ) :
(∏ i ∈ s, f i) % n = (∏ i ∈ s, f i % n) % n :=
(Multiset.prod_int_mod _ _).trans <| by rw [Finset.prod, Multiset.map_map]; rfl
end Finset
namespace Fintype
variable [Fintype ι] [Fintype κ]
open Finset
section CommMonoid
variable [CommMonoid M]
/-- `Fintype.prod_bijective` is a variant of `Finset.prod_bij` that accepts `Function.Bijective`.
See `Function.Bijective.prod_comp` for a version without `h`. -/
@[to_additive /-- `Fintype.sum_bijective` is a variant of `Finset.sum_bij` that accepts
`Function.Bijective`.
See `Function.Bijective.sum_comp` for a version without `h`. -/]
lemma prod_bijective (e : ι → κ) (he : e.Bijective) (f : ι → M) (g : κ → M)
(h : ∀ x, f x = g (e x)) : ∏ x, f x = ∏ x, g x :=
prod_equiv (.ofBijective e he) (by simp) (by simp [h])
@[to_additive] alias _root_.Function.Bijective.finset_prod := prod_bijective
/-- `Fintype.prod_equiv` is a specialization of `Finset.prod_bij` that
automatically fills in most arguments.
See `Equiv.prod_comp` for a version without `h`.
-/
@[to_additive /-- `Fintype.sum_equiv` is a specialization of `Finset.sum_bij` that
automatically fills in most arguments.
See `Equiv.sum_comp` for a version without `h`. -/]
lemma prod_equiv (e : ι ≃ κ) (f : ι → M) (g : κ → M) (h : ∀ x, f x = g (e x)) :
∏ x, f x = ∏ x, g x := prod_bijective _ e.bijective _ _ h
@[to_additive]
lemma _root_.Function.Bijective.prod_comp {e : ι → κ} (he : e.Bijective) (g : κ → M) :
∏ i, g (e i) = ∏ i, g i := prod_bijective _ he _ _ fun _ ↦ rfl
@[to_additive]
lemma _root_.Equiv.prod_comp (e : ι ≃ κ) (g : κ → M) : ∏ i, g (e i) = ∏ i, g i :=
prod_equiv e _ _ fun _ ↦ rfl
@[to_additive]
theorem prod_empty [IsEmpty ι] (f : ι → M) : ∏ x : ι, f x = 1 := prod_of_isEmpty _
end CommMonoid
end Fintype
namespace Finset
variable [CommMonoid M]
@[to_additive (attr := simp)]
lemma prod_attach_univ [Fintype ι] (f : {i // i ∈ @univ ι _} → M) :
∏ i ∈ univ.attach, f i = ∏ i, f ⟨i, mem_univ _⟩ :=
Fintype.prod_equiv (Equiv.subtypeUnivEquiv mem_univ) _ _ <| by simp
@[to_additive]
theorem prod_erase_attach [DecidableEq ι] {s : Finset ι} (f : ι → M) (i : ↑s) :
∏ j ∈ s.attach.erase i, f ↑j = ∏ j ∈ s.erase ↑i, f j := by
rw [← Function.Embedding.coe_subtype, ← prod_map]
simp [attach_map_val]
end Finset
namespace Multiset
@[simp]
lemma card_sum (s : Finset ι) (f : ι → Multiset α) : card (∑ i ∈ s, f i) = ∑ i ∈ s, card (f i) :=
map_sum cardHom ..
theorem disjoint_list_sum_left {a : Multiset α} {l : List (Multiset α)} :
Disjoint l.sum a ↔ ∀ b ∈ l, Disjoint b a := by
induction l with
| nil =>
simp only [zero_disjoint, List.not_mem_nil, IsEmpty.forall_iff, forall_const, List.sum_nil]
| cons b bs ih =>
simp [ih]
theorem disjoint_list_sum_right {a : Multiset α} {l : List (Multiset α)} :
Disjoint a l.sum ↔ ∀ b ∈ l, Disjoint a b := by
simpa only [disjoint_comm (a := a)] using disjoint_list_sum_left
theorem disjoint_sum_left {a : Multiset α} {i : Multiset (Multiset α)} :
Disjoint i.sum a ↔ ∀ b ∈ i, Disjoint b a :=
Quotient.inductionOn i fun l => by
rw [quot_mk_to_coe, Multiset.sum_coe]
exact disjoint_list_sum_left
theorem disjoint_sum_right {a : Multiset α} {i : Multiset (Multiset α)} :
Disjoint a i.sum ↔ ∀ b ∈ i, Disjoint a b := by
simpa only [disjoint_comm (a := a)] using disjoint_sum_left
theorem disjoint_finset_sum_left {i : Finset ι} {f : ι → Multiset α} {a : Multiset α} :
Disjoint (i.sum f) a ↔ ∀ b ∈ i, Disjoint (f b) a := by
convert @disjoint_sum_left _ a (map f i.val)
simp
theorem disjoint_finset_sum_right {i : Finset ι} {f : ι → Multiset α}
{a : Multiset α} : Disjoint a (i.sum f) ↔ ∀ b ∈ i, Disjoint a (f b) := by
simpa only [disjoint_comm] using disjoint_finset_sum_left
variable [DecidableEq α]
theorem count_sum' {s : Finset ι} {a : α} {f : ι → Multiset α} :
count a (∑ x ∈ s, f x) = ∑ x ∈ s, count a (f x) := by
dsimp only [Finset.sum]
rw [count_sum]
theorem toFinset_prod_dvd_prod [DecidableEq M] [CommMonoid M] (S : Multiset M) :
S.toFinset.prod id ∣ S.prod := by
rw [Finset.prod_eq_multiset_prod]
refine Multiset.prod_dvd_prod_of_le ?_
simp [Multiset.dedup_le S]
end Multiset
@[simp, norm_cast]
theorem Units.coe_prod [CommMonoid M] (f : α → Mˣ) (s : Finset α) :
(↑(∏ i ∈ s, f i) : M) = ∏ i ∈ s, (f i : M) :=
map_prod (Units.coeHom M) _ _
/-! ### `Additive`, `Multiplicative` -/
open Additive Multiplicative
section Monoid
variable [Monoid M]
@[simp]
theorem ofMul_list_prod (s : List M) : ofMul s.prod = (s.map ofMul).sum := by simp [ofMul]; rfl
@[simp]
theorem toMul_list_sum (s : List (Additive M)) : s.sum.toMul = (s.map toMul).prod := by
simp [toMul, ofMul]; rfl
end Monoid
section AddMonoid
variable [AddMonoid M]
@[simp]
theorem ofAdd_list_prod (s : List M) : ofAdd s.sum = (s.map ofAdd).prod := by simp [ofAdd]; rfl
@[simp]
theorem toAdd_list_sum (s : List (Multiplicative M)) : s.prod.toAdd = (s.map toAdd).sum := by
simp [toAdd, ofAdd]; rfl
end AddMonoid
section CommMonoid
variable [CommMonoid M]
@[simp]
theorem ofMul_multiset_prod (s : Multiset M) : ofMul s.prod = (s.map ofMul).sum := by
simp [ofMul]; rfl
@[simp]
theorem toMul_multiset_sum (s : Multiset (Additive M)) : s.sum.toMul = (s.map toMul).prod := by
simp [toMul, ofMul]; rfl
@[simp]
theorem ofMul_prod (s : Finset ι) (f : ι → M) : ofMul (∏ i ∈ s, f i) = ∑ i ∈ s, ofMul (f i) :=
rfl
@[simp]
theorem toMul_sum (s : Finset ι) (f : ι → Additive M) :
(∑ i ∈ s, f i).toMul = ∏ i ∈ s, (f i).toMul :=
rfl
end CommMonoid
section AddCommMonoid
variable [AddCommMonoid M]
@[simp]
theorem ofAdd_multiset_prod (s : Multiset M) : ofAdd s.sum = (s.map ofAdd).prod := by
simp [ofAdd]; rfl
@[simp]
theorem toAdd_multiset_sum (s : Multiset (Multiplicative M)) :
s.prod.toAdd = (s.map toAdd).sum := by
simp [toAdd, ofAdd]; rfl
@[simp]
theorem ofAdd_sum (s : Finset ι) (f : ι → M) : ofAdd (∑ i ∈ s, f i) = ∏ i ∈ s, ofAdd (f i) :=
rfl
@[simp]
theorem toAdd_prod (s : Finset ι) (f : ι → Multiplicative M) :
(∏ i ∈ s, f i).toAdd = ∑ i ∈ s, (f i).toAdd :=
rfl
end AddCommMonoid |
.lake/packages/mathlib/Mathlib/Algebra/BigOperators/Group/Finset/Preimage.lean | import Mathlib.Data.Finset.Preimage
import Mathlib.Algebra.BigOperators.Group.Finset.Basic
/-!
# Sums and products over preimages of finite sets.
-/
assert_not_exists MonoidWithZero MulAction IsOrderedMonoid
variable {ι κ β : Type*}
open Fin Function
namespace Finset
variable [CommMonoid β]
@[to_additive]
lemma prod_preimage' (f : ι → κ) [DecidablePred (· ∈ Set.range f)] (s : Finset κ) (hf) (g : κ → β) :
∏ x ∈ s.preimage f hf, g (f x) = ∏ x ∈ s with x ∈ Set.range f, g x := by
classical
calc
∏ x ∈ preimage s f hf, g (f x) = ∏ x ∈ image f (preimage s f hf), g x :=
Eq.symm <| prod_image <| by simpa [mem_preimage, Set.InjOn] using hf
_ = ∏ x ∈ s with x ∈ Set.range f, g x := by rw [image_preimage]
@[to_additive]
lemma prod_preimage (f : ι → κ) (s : Finset κ) (hf) (g : κ → β)
(hg : ∀ x ∈ s, x ∉ Set.range f → g x = 1) :
∏ x ∈ s.preimage f hf, g (f x) = ∏ x ∈ s, g x := by
classical rw [prod_preimage', prod_filter_of_ne]; exact fun x hx ↦ Not.imp_symm (hg x hx)
@[to_additive]
lemma prod_preimage_of_bij (f : ι → κ) (s : Finset κ) (hf : Set.BijOn f (f ⁻¹' ↑s) ↑s) (g : κ → β) :
∏ x ∈ s.preimage f hf.injOn, g (f x) = ∏ x ∈ s, g x :=
prod_preimage _ _ hf.injOn g fun _ hs h_f ↦ (h_f <| hf.subset_range hs).elim
end Finset |
.lake/packages/mathlib/Mathlib/Algebra/BigOperators/Group/List/Lemmas.lean | import Mathlib.Algebra.BigOperators.Group.List.Basic
import Mathlib.Algebra.Divisibility.Basic
import Mathlib.Algebra.Group.Int.Units
import Mathlib.Data.List.Dedup
import Mathlib.Data.List.Flatten
import Mathlib.Data.List.Pairwise
import Mathlib.Data.List.Perm.Basic
import Mathlib.Data.List.Range
import Mathlib.Data.List.Rotate
import Mathlib.Data.List.ProdSigma
import Mathlib.Algebra.Group.Opposite
/-!
# Sums and products from lists
This file provides further results about `List.prod`, `List.sum`,
which calculate the product and sum of elements of a list
and `List.alternatingProd`, `List.alternatingSum`, their alternating counterparts.
-/
assert_not_imported Mathlib.Algebra.Order.Group.Nat
variable {ι α β M N P G : Type*}
namespace List
section Monoid
variable [Monoid M] [Monoid N] [Monoid P] {l l₁ l₂ : List M} {a : M}
@[to_additive]
theorem prod_isUnit : ∀ {L : List M}, (∀ m ∈ L, IsUnit m) → IsUnit L.prod
| [], _ => by simp
| h :: t, u => by
simp only [List.prod_cons]
exact IsUnit.mul (u h mem_cons_self) (prod_isUnit fun m mt => u m (mem_cons_of_mem h mt))
@[to_additive]
theorem prod_isUnit_iff {M : Type*} [CommMonoid M] {L : List M} :
IsUnit L.prod ↔ ∀ m ∈ L, IsUnit m := by
refine ⟨fun h => ?_, prod_isUnit⟩
induction L with
| nil => exact fun m' h' => False.elim (not_mem_nil h')
| cons m L ih =>
rw [prod_cons, IsUnit.mul_iff] at h
exact fun m' h' ↦ Or.elim (eq_or_mem_of_mem_cons h') (fun H => H.substr h.1) fun H => ih h.2 _ H
/-- If elements of a list commute with each other, then their product does not
depend on the order of elements. -/
@[to_additive /-- If elements of a list additively commute with each other, then their sum does not
depend on the order of elements. -/]
lemma Perm.prod_eq' (h : l₁ ~ l₂) (hc : l₁.Pairwise Commute) : l₁.prod = l₂.prod := by
refine h.foldr_eq' ?_ _
apply Pairwise.forall_of_forall
· intro x y h z
exact (h z).symm
· intros; rfl
· apply hc.imp
intro a b h z
rw [← mul_assoc, ← mul_assoc, h]
end Monoid
section Group
variable [Group G]
lemma prod_rotate_eq_one_of_prod_eq_one :
∀ {l : List G} (_ : l.prod = 1) (n : ℕ), (l.rotate n).prod = 1
| [], _, _ => by simp
| a :: l, hl, n => by
have : n % List.length (a :: l) ≤ List.length (a :: l) := le_of_lt (Nat.mod_lt _ (by simp))
rw [← List.take_append_drop (n % List.length (a :: l)) (a :: l)] at hl
rw [← rotate_mod, rotate_eq_drop_append_take this, List.prod_append, mul_eq_one_iff_inv_eq,
← one_mul (List.prod _)⁻¹, ← hl, List.prod_append, mul_assoc, mul_inv_cancel, mul_one]
end Group
variable [DecidableEq α]
/-- Summing the count of `x` over a list filtered by some `p` is just `countP` applied to `p` -/
theorem sum_map_count_dedup_filter_eq_countP (p : α → Bool) (l : List α) :
((l.dedup.filter p).map fun x => l.count x).sum = l.countP p := by
induction l with
| nil => simp
| cons a as h =>
simp_rw [List.countP_cons, List.count_cons, List.sum_map_add]
congr 1
· refine _root_.trans ?_ h
by_cases ha : a ∈ as
· simp [dedup_cons_of_mem ha]
· simp only [dedup_cons_of_notMem ha, List.filter]
match p a with
| true => simp only [List.map_cons, List.sum_cons, List.count_eq_zero.2 ha, zero_add]
| false => simp only
· simp only [beq_iff_eq]
by_cases hp : p a
· refine _root_.trans (sum_map_eq_nsmul_single a _ fun _ h _ => by simp [h.symm]) ?_
simp [hp, count_dedup]
· exact _root_.trans (List.sum_eq_zero fun n hn => by grind) (by simp [hp])
theorem sum_map_count_dedup_eq_length (l : List α) :
(l.dedup.map fun x => l.count x).sum = l.length := by
simpa using sum_map_count_dedup_filter_eq_countP (fun _ => True) l
end List
namespace List
lemma length_sigma {σ : α → Type*} (l₁ : List α) (l₂ : ∀ a, List (σ a)) :
length (l₁.sigma l₂) = (l₁.map fun a ↦ length (l₂ a)).sum := by
induction l₁ with
| nil => rfl
| cons x l₁ IH => simp only [sigma_cons, length_append, length_map, IH, map, sum_cons]
lemma ranges_flatten : ∀ (l : List ℕ), l.ranges.flatten = range l.sum
| [] => rfl
| a :: l => by simp [ranges, ← map_flatten, ranges_flatten, range_add]
/-- The members of `l.ranges` have no duplicates -/
theorem ranges_nodup {l s : List ℕ} (hs : s ∈ ranges l) : s.Nodup :=
(List.pairwise_flatten.mp <| by rw [ranges_flatten]; exact nodup_range).1 s hs
/-- Any entry of any member of `l.ranges` is strictly smaller than `l.sum`. -/
lemma mem_mem_ranges_iff_lt_sum (l : List ℕ) {n : ℕ} :
(∃ s ∈ l.ranges, n ∈ s) ↔ n < l.sum := by
rw [← mem_range, ← ranges_flatten, mem_flatten]
/-- In a flatten of sublists, taking the slice between the indices `A` and `B - 1` gives back the
original sublist of index `i` if `A` is the sum of the lengths of sublists of index `< i`, and
`B` is the sum of the lengths of sublists of index `≤ i`. -/
lemma drop_take_succ_flatten_eq_getElem (L : List (List α)) (i : Nat) (h : i < L.length) :
(L.flatten.take ((L.map length).take (i + 1)).sum).drop ((L.map length).take i).sum = L[i] := by
have : (L.map length).take i = ((L.take (i + 1)).map length).take i := by
simp [map_take, take_take, Nat.min_eq_left]
simp only [this, take_sum_flatten, drop_sum_flatten,
drop_take_succ_eq_cons_getElem, h, flatten_nil, flatten_cons, append_nil]
end List
namespace List
/-- If a product of integers is `-1`, then at least one factor must be `-1`. -/
theorem neg_one_mem_of_prod_eq_neg_one {l : List ℤ} (h : l.prod = -1) : (-1 : ℤ) ∈ l := by
obtain ⟨x, h₁, h₂⟩ := exists_mem_ne_one_of_prod_ne_one (ne_of_eq_of_ne h (by decide))
exact Or.resolve_left
(Int.isUnit_iff.mp (prod_isUnit_iff.mp
(h.symm ▸ ⟨⟨-1, -1, by decide, by decide⟩, rfl⟩ : IsUnit l.prod) x h₁)) h₂ ▸ h₁
theorem dvd_prod [CommMonoid M] {a} {l : List M} (ha : a ∈ l) : a ∣ l.prod := by
let ⟨s, t, h⟩ := append_of_mem ha
rw [h, prod_append, prod_cons, mul_left_comm]
exact dvd_mul_right _ _
theorem Sublist.prod_dvd_prod [CommMonoid M] {l₁ l₂ : List M} (h : l₁ <+ l₂) :
l₁.prod ∣ l₂.prod := by
obtain ⟨l, hl⟩ := h.exists_perm_append
rw [hl.prod_eq, prod_append]
exact dvd_mul_right _ _
section Alternating
variable [CommGroup G]
@[to_additive]
theorem alternatingProd_append :
∀ l₁ l₂ : List G,
alternatingProd (l₁ ++ l₂) = alternatingProd l₁ * alternatingProd l₂ ^ (-1 : ℤ) ^ length l₁
| [], l₂ => by simp
| a :: l₁, l₂ => by
simp_rw [cons_append, alternatingProd_cons, alternatingProd_append, length_cons, pow_succ',
Int.neg_mul, one_mul, zpow_neg, ← div_eq_mul_inv, div_div]
@[to_additive]
theorem alternatingProd_reverse :
∀ l : List G, alternatingProd (reverse l) = alternatingProd l ^ (-1 : ℤ) ^ (length l + 1)
| [] => by simp only [alternatingProd_nil, one_zpow, reverse_nil]
| a :: l => by
simp_rw [reverse_cons, alternatingProd_append, alternatingProd_reverse,
alternatingProd_singleton, alternatingProd_cons, length_reverse, length, pow_succ',
Int.neg_mul, one_mul, zpow_neg, inv_inv]
rw [mul_comm, ← div_eq_mul_inv, div_zpow]
end Alternating
end List
open List
namespace MulOpposite
variable [Monoid M]
lemma op_list_prod : ∀ l : List M, op l.prod = (l.map op).reverse.prod := by
intro l; induction l with
| nil => rfl
| cons x xs ih =>
rw [List.prod_cons, List.map_cons, List.reverse_cons', List.prod_concat, op_mul, ih]
lemma unop_list_prod (l : List Mᵐᵒᵖ) : l.prod.unop = (l.map unop).reverse.prod := by
rw [← op_inj, op_unop, MulOpposite.op_list_prod, map_reverse, map_map, reverse_reverse,
op_comp_unop, map_id]
end MulOpposite
section MonoidHom
variable [Monoid M] [Monoid N]
/-- A morphism into the opposite monoid acts on the product by acting on the reversed elements. -/
lemma unop_map_list_prod {F : Type*} [FunLike F M Nᵐᵒᵖ] [MonoidHomClass F M Nᵐᵒᵖ]
(f : F) (l : List M) :
(f l.prod).unop = (l.map (MulOpposite.unop ∘ f)).reverse.prod := by
rw [map_list_prod f l, MulOpposite.unop_list_prod, List.map_map]
end MonoidHom |
.lake/packages/mathlib/Mathlib/Algebra/BigOperators/Group/List/Basic.lean | import Mathlib.Algebra.Divisibility.Basic
import Mathlib.Algebra.Group.Hom.Defs
import Mathlib.Algebra.BigOperators.Group.List.Defs
import Mathlib.Order.RelClasses
import Mathlib.Data.List.TakeDrop
import Mathlib.Data.List.Forall2
import Mathlib.Data.List.Perm.Basic
import Mathlib.Algebra.Group.Basic
import Mathlib.Algebra.Group.Commute.Defs
import Mathlib.Algebra.Group.Nat.Defs
import Mathlib.Algebra.Group.Int.Defs
/-!
# Sums and products from lists
This file provides basic results about `List.prod`, `List.sum`, which calculate the product and sum
of elements of a list and `List.alternatingProd`, `List.alternatingSum`, their alternating
counterparts.
-/
assert_not_imported Mathlib.Algebra.Order.Group.Nat
variable {ι α β M N P G : Type*}
namespace List
section Monoid
variable [Monoid M] [Monoid N] [Monoid P] {l l₁ l₂ : List M} {a : M}
open scoped Relator in
@[to_additive]
theorem rel_prod {R : M → N → Prop} (h : R 1 1) (hf : (R ⇒ R ⇒ R) (· * ·) (· * ·)) :
(Forall₂ R ⇒ R) prod prod :=
rel_foldr hf h
@[to_additive]
theorem prod_hom_nonempty {l : List M} {F : Type*} [FunLike F M N] [MulHomClass F M N] (f : F)
(hl : l ≠ []) : (l.map f).prod = f l.prod :=
match l, hl with | x :: xs, hl => by induction xs generalizing x <;> simp_all
@[to_additive]
theorem prod_hom (l : List M) {F : Type*} [FunLike F M N] [MonoidHomClass F M N] (f : F) :
(l.map f).prod = f l.prod := by
simp only [prod, foldr_map, ← map_one f]
exact l.foldr_hom f (fun x y => (map_mul f x y).symm)
@[to_additive]
theorem prod_hom₂_nonempty {l : List ι} (f : M → N → P)
(hf : ∀ a b c d, f (a * b) (c * d) = f a c * f b d) (f₁ : ι → M) (f₂ : ι → N) (hl : l ≠ []) :
(l.map fun i => f (f₁ i) (f₂ i)).prod = f (l.map f₁).prod (l.map f₂).prod := by
match l, hl with | x :: xs, hl => induction xs generalizing x <;> simp_all
@[to_additive]
theorem prod_hom₂ (l : List ι) (f : M → N → P) (hf : ∀ a b c d, f (a * b) (c * d) = f a c * f b d)
(hf' : f 1 1 = 1) (f₁ : ι → M) (f₂ : ι → N) :
(l.map fun i => f (f₁ i) (f₂ i)).prod = f (l.map f₁).prod (l.map f₂).prod := by
simp only [prod_eq_foldr, foldr_map]
rw [← foldr_hom₂ l f _ _ ((fun x y => f (f₁ x) (f₂ x) * y) ) _ _ (by simp [hf]), hf']
@[to_additive (attr := simp)]
theorem prod_map_mul {M : Type*} [CommMonoid M] {l : List ι} {f g : ι → M} :
(l.map fun i => f i * g i).prod = (l.map f).prod * (l.map g).prod :=
l.prod_hom₂ (· * ·) mul_mul_mul_comm (mul_one _) _ _
@[to_additive]
theorem prod_map_hom (L : List ι) (f : ι → M) {G : Type*} [FunLike G M N] [MonoidHomClass G M N]
(g : G) :
(L.map (g ∘ f)).prod = g (L.map f).prod := by rw [← prod_hom, map_map]
@[to_additive (attr := simp)]
theorem prod_take_mul_prod_drop (L : List M) (i : ℕ) :
(L.take i).prod * (L.drop i).prod = L.prod := by
simp [← prod_append]
@[to_additive (attr := simp)]
theorem prod_take_succ (L : List M) (i : ℕ) (p : i < L.length) :
(L.take (i + 1)).prod = (L.take i).prod * L[i] := by
rw [← take_concat_get' _ _ p, prod_append]
simp
/-- A list with product not one must have positive length. -/
@[to_additive /-- A list with sum not zero must have positive length. -/]
theorem length_pos_of_prod_ne_one (L : List M) (h : L.prod ≠ 1) : 0 < L.length := by
cases L
· simp at h
· simp
/-- A list with product greater than one must have positive length. -/
@[to_additive length_pos_of_sum_pos /-- A list with positive sum must have positive length. -/]
theorem length_pos_of_one_lt_prod [Preorder M] (L : List M) (h : 1 < L.prod) : 0 < L.length :=
length_pos_of_prod_ne_one L h.ne'
/-- A list with product less than one must have positive length. -/
@[to_additive /-- A list with negative sum must have positive length. -/]
theorem length_pos_of_prod_lt_one [Preorder M] (L : List M) (h : L.prod < 1) : 0 < L.length :=
length_pos_of_prod_ne_one L h.ne
@[to_additive]
theorem prod_set :
∀ (L : List M) (n : ℕ) (a : M),
(L.set n a).prod =
((L.take n).prod * if n < L.length then a else 1) * (L.drop (n + 1)).prod
| x :: xs, 0, a => by simp [set]
| x :: xs, i + 1, a => by
simp [set, prod_set xs i a, mul_assoc, Nat.add_lt_add_iff_right]
| [], _, _ => by simp [set, (Nat.zero_le _).not_gt]
/-- We'd like to state this as `L.headI * L.tail.prod = L.prod`, but because `L.headI` relies on an
inhabited instance to return a garbage value on the empty list, this is not possible.
Instead, we write the statement in terms of `L[0]?.getD 1`.
-/
@[to_additive /-- We'd like to state this as `L.headI + L.tail.sum = L.sum`, but because `L.headI`
relies on an inhabited instance to return a garbage value on the empty list, this is not possible.
Instead, we write the statement in terms of `L[0]?.getD 0`. -/]
theorem getElem?_zero_mul_tail_prod (l : List M) : l[0]?.getD 1 * l.tail.prod = l.prod := by
cases l <;> simp
/-- Same as `get?_zero_mul_tail_prod`, but avoiding the `List.headI` garbage complication by
requiring the list to be nonempty. -/
@[to_additive /-- Same as `get?_zero_add_tail_sum`, but avoiding the `List.headI` garbage
complication by requiring the list to be nonempty. -/]
theorem headI_mul_tail_prod_of_ne_nil [Inhabited M] (l : List M) (h : l ≠ []) :
l.headI * l.tail.prod = l.prod := by cases l <;> [contradiction; simp]
@[to_additive]
theorem _root_.Commute.list_prod_right (l : List M) (y : M) (h : ∀ x ∈ l, Commute y x) :
Commute y l.prod := by
induction l with
| nil => simp
| cons z l IH =>
rw [List.forall_mem_cons] at h
rw [List.prod_cons]
exact Commute.mul_right h.1 (IH h.2)
@[to_additive]
theorem _root_.Commute.list_prod_left (l : List M) (y : M) (h : ∀ x ∈ l, Commute x y) :
Commute l.prod y :=
((Commute.list_prod_right _ _) fun _ hx => (h _ hx).symm).symm
@[to_additive] lemma prod_range_succ (f : ℕ → M) (n : ℕ) :
((range n.succ).map f).prod = ((range n).map f).prod * f n := by
rw [range_succ, map_append, map_singleton, prod_append, prod_cons, prod_nil, mul_one]
/-- A variant of `prod_range_succ` which pulls off the first term in the product rather than the
last. -/
@[to_additive /-- A variant of `sum_range_succ` which pulls off the first term in the sum rather
than the last. -/]
lemma prod_range_succ' (f : ℕ → M) (n : ℕ) :
((range n.succ).map f).prod = f 0 * ((range n).map fun i ↦ f i.succ).prod := by
rw [range_succ_eq_map]
simp [Function.comp_def]
@[to_additive] lemma prod_eq_one (hl : ∀ x ∈ l, x = 1) : l.prod = 1 := by
induction l with
| nil => rfl
| cons i l hil =>
rw [List.prod_cons, hil fun x hx ↦ hl _ (mem_cons_of_mem i hx),
hl _ mem_cons_self, one_mul]
@[to_additive] lemma exists_mem_ne_one_of_prod_ne_one (h : l.prod ≠ 1) :
∃ x ∈ l, x ≠ (1 : M) := by simpa only [not_forall, exists_prop] using mt prod_eq_one h
@[to_additive]
lemma prod_erase_of_comm [DecidableEq M] (ha : a ∈ l) (comm : ∀ x ∈ l, ∀ y ∈ l, x * y = y * x) :
a * (l.erase a).prod = l.prod := by
induction l with
| nil => simp only [not_mem_nil] at ha
| cons b l ih =>
obtain rfl | ⟨ne, h⟩ := List.eq_or_ne_mem_of_mem ha
· simp only [erase_cons_head, prod_cons]
rw [List.erase, beq_false_of_ne ne.symm, List.prod_cons, List.prod_cons, ← mul_assoc,
comm a ha b mem_cons_self, mul_assoc,
ih h fun x hx y hy ↦ comm _ (List.mem_cons_of_mem b hx) _ (List.mem_cons_of_mem b hy)]
@[to_additive]
lemma prod_map_eq_pow_single [DecidableEq α] {l : List α} (a : α) (f : α → M)
(hf : ∀ a', a' ≠ a → a' ∈ l → f a' = 1) : (l.map f).prod = f a ^ l.count a := by
induction l generalizing a with
| nil => rw [map_nil, prod_nil, count_nil, _root_.pow_zero]
| cons a' as h =>
specialize h a fun a' ha' hfa' => hf a' ha' (mem_cons_of_mem _ hfa')
rw [List.map_cons, List.prod_cons, count_cons, h]
simp only [beq_iff_eq]
split_ifs with ha'
· rw [ha', _root_.pow_succ']
· rw [hf a' ha' mem_cons_self, one_mul, add_zero]
@[to_additive]
lemma prod_eq_pow_single [DecidableEq M] (a : M) (h : ∀ a', a' ≠ a → a' ∈ l → a' = 1) :
l.prod = a ^ l.count a :=
_root_.trans (by rw [map_id]) (prod_map_eq_pow_single a id h)
end Monoid
section CommMonoid
variable [CommMonoid M] {a : M} {l l₁ l₂ : List M}
@[to_additive (attr := simp)]
lemma prod_erase [DecidableEq M] (ha : a ∈ l) : a * (l.erase a).prod = l.prod :=
prod_erase_of_comm ha fun x _ y _ ↦ mul_comm x y
@[to_additive (attr := simp)]
lemma prod_map_erase [DecidableEq α] (f : α → M) {a} :
∀ {l : List α}, a ∈ l → f a * ((l.erase a).map f).prod = (l.map f).prod
| b :: l, h => by
obtain rfl | ⟨ne, h⟩ := List.eq_or_ne_mem_of_mem h
· simp only [map, erase_cons_head, prod_cons]
· simp only [map, erase_cons_tail (not_beq_of_ne ne.symm), prod_cons, prod_map_erase _ h,
mul_left_comm (f a) (f b)]
@[to_additive] lemma Perm.prod_eq (h : Perm l₁ l₂) : prod l₁ = prod l₂ := h.foldr_op_eq
@[to_additive (attr := simp)]
lemma prod_reverse (l : List M) : prod l.reverse = prod l := (reverse_perm l).prod_eq
@[to_additive]
lemma prod_mul_prod_eq_prod_zipWith_mul_prod_drop :
∀ l l' : List M,
l.prod * l'.prod =
(zipWith (· * ·) l l').prod * (l.drop l'.length).prod * (l'.drop l.length).prod
| [], ys => by simp
| xs, [] => by simp
| x :: xs, y :: ys => by
simp only [drop, length, zipWith_cons_cons, prod_cons]
conv =>
lhs; rw [mul_assoc]; right; rw [mul_comm, mul_assoc]; right
rw [mul_comm, prod_mul_prod_eq_prod_zipWith_mul_prod_drop xs ys]
simp [mul_assoc]
@[to_additive]
lemma prod_mul_prod_eq_prod_zipWith_of_length_eq (l l' : List M) (h : l.length = l'.length) :
l.prod * l'.prod = (zipWith (· * ·) l l').prod := by
apply (prod_mul_prod_eq_prod_zipWith_mul_prod_drop l l').trans
rw [← h, drop_length, h, drop_length, prod_nil, mul_one, mul_one]
@[to_additive]
lemma prod_map_ite (p : α → Prop) [DecidablePred p] (f g : α → M) (l : List α) :
(l.map fun a => if p a then f a else g a).prod =
((l.filter p).map f).prod * ((l.filter fun a ↦ ¬p a).map g).prod := by
induction l with
| nil => simp
| cons x xs ih =>
simp only [map_cons, filter_cons, prod_cons] at ih ⊢
rw [ih]
clear ih
by_cases hx : p x
· simp only [hx, ↓reduceIte, decide_not, decide_true, map_cons, prod_cons, not_true_eq_false,
decide_false, Bool.false_eq_true, mul_assoc]
· simp only [hx, ↓reduceIte, decide_not, decide_false, Bool.false_eq_true, not_false_eq_true,
decide_true, map_cons, prod_cons, mul_left_comm]
@[to_additive]
lemma prod_map_filter_mul_prod_map_filter_not (p : α → Prop) [DecidablePred p] (f : α → M)
(l : List α) :
((l.filter p).map f).prod * ((l.filter fun x => ¬p x).map f).prod = (l.map f).prod := by
rw [← prod_map_ite]
simp only [ite_self]
end CommMonoid
@[to_additive]
lemma eq_of_prod_take_eq [LeftCancelMonoid M] {L L' : List M} (h : L.length = L'.length)
(h' : ∀ i ≤ L.length, (L.take i).prod = (L'.take i).prod) : L = L' := by
refine ext_get h fun i h₁ h₂ => ?_
have : (L.take (i + 1)).prod = (L'.take (i + 1)).prod := h' _ (Nat.succ_le_of_lt h₁)
rw [prod_take_succ L i h₁, prod_take_succ L' i h₂, h' i (le_of_lt h₁)] at this
convert mul_left_cancel this
section Group
variable [Group G]
/-- This is the `List.prod` version of `mul_inv_rev` -/
@[to_additive /-- This is the `List.sum` version of `add_neg_rev` -/]
theorem prod_inv_reverse : ∀ L : List G, L.prod⁻¹ = (L.map fun x => x⁻¹).reverse.prod
| [] => by simp
| x :: xs => by simp [prod_append, prod_inv_reverse xs]
/-- A non-commutative variant of `List.prod_reverse` -/
@[to_additive /-- A non-commutative variant of `List.sum_reverse` -/]
theorem prod_reverse_noncomm : ∀ L : List G, L.reverse.prod = (L.map fun x => x⁻¹).prod⁻¹ := by
simp [prod_inv_reverse]
/-- Counterpart to `List.prod_take_succ` when we have an inverse operation -/
@[to_additive (attr := simp)
/-- Counterpart to `List.sum_take_succ` when we have a negation operation -/]
theorem prod_drop_succ :
∀ (L : List G) (i : ℕ) (p : i < L.length), (L.drop (i + 1)).prod = L[i]⁻¹ * (L.drop i).prod
| [], _, p => False.elim (Nat.not_lt_zero _ p)
| _ :: _, 0, _ => by simp
| _ :: xs, i + 1, p => prod_drop_succ xs i (Nat.lt_of_succ_lt_succ p)
/-- Cancellation of a telescoping product. -/
@[to_additive /-- Cancellation of a telescoping sum. -/]
theorem prod_range_div' (n : ℕ) (f : ℕ → G) :
((range n).map fun k ↦ f k / f (k + 1)).prod = f 0 / f n := by
induction n with
| zero => exact (div_self' (f 0)).symm
| succ n h => simp [range_succ, prod_append, map_append, h]
end Group
section CommGroup
variable [CommGroup G]
/-- This is the `List.prod` version of `mul_inv` -/
@[to_additive /-- This is the `List.sum` version of `add_neg` -/]
theorem prod_inv {K : Type*} [DivisionCommMonoid K] :
∀ L : List K, L.prod⁻¹ = (L.map fun x => x⁻¹).prod
| [] => by simp
| x :: xs => by simp [mul_comm, prod_inv xs]
/-- Cancellation of a telescoping product. -/
@[to_additive /-- Cancellation of a telescoping sum. -/]
theorem prod_range_div (n : ℕ) (f : ℕ → G) :
((range n).map fun k ↦ f (k + 1) / f k).prod = f n / f 0 := by
have h : ((·⁻¹) ∘ fun k ↦ f (k + 1) / f k) = fun k ↦ f k / f (k + 1) := by ext; apply inv_div
rw [← inv_inj, prod_inv, map_map, inv_div, h, prod_range_div']
/-- Alternative version of `List.prod_set` when the list is over a group -/
@[to_additive /-- Alternative version of `List.sum_set` when the list is over a group -/]
theorem prod_set' (L : List G) (n : ℕ) (a : G) :
(L.set n a).prod = L.prod * if hn : n < L.length then L[n]⁻¹ * a else 1 := by
refine (prod_set L n a).trans ?_
split_ifs with hn
· rw [mul_comm _ a, mul_assoc a, prod_drop_succ L n hn, mul_comm _ (drop n L).prod, ←
mul_assoc (take n L).prod, prod_take_mul_prod_drop, mul_comm a, mul_assoc]
· simp only [take_of_length_le (le_of_not_gt hn), prod_nil, mul_one,
drop_eq_nil_of_le ((le_of_not_gt hn).trans n.le_succ)]
@[to_additive]
lemma prod_map_ite_eq {A : Type*} [DecidableEq A] (l : List A) (f g : A → G) (a : A) :
(l.map fun x => if x = a then f x else g x).prod
= (f a / g a) ^ (l.count a) * (l.map g).prod := by
induction l with
| nil => simp
| cons x xs ih =>
simp only [map_cons, prod_cons, count_cons] at ih ⊢
rw [ih]
clear ih
by_cases hx : x = a
· simp only [hx, ite_true, pow_add, pow_one, div_eq_mul_inv, mul_assoc, mul_comm, mul_left_comm,
mul_inv_cancel_left, beq_self_eq_true]
· simp only [hx, ite_false, add_zero, mul_assoc, mul_comm (g x) _, beq_iff_eq]
end CommGroup
theorem sum_const_nat (m n : ℕ) : sum (replicate m n) = m * n :=
sum_replicate m n
/-!
Several lemmas about sum/head/tail for `List ℕ`.
These are hard to generalize well, as they rely on the fact that `default ℕ = 0`.
If desired, we could add a class stating that `default = 0`.
-/
/-- This relies on `default ℕ = 0`. -/
theorem headI_add_tail_sum (L : List ℕ) : L.headI + L.tail.sum = L.sum := by
cases L <;> simp
/-- This relies on `default ℕ = 0`. -/
theorem headI_le_sum (L : List ℕ) : L.headI ≤ L.sum :=
Nat.le.intro (headI_add_tail_sum L)
/-- This relies on `default ℕ = 0`. -/
theorem tail_sum (L : List ℕ) : L.tail.sum = L.sum - L.headI := by
rw [← headI_add_tail_sum L, add_comm, Nat.add_sub_cancel_right]
section Alternating
section
variable [One G] [Mul G] [Inv G]
@[to_additive (attr := simp)]
theorem alternatingProd_nil : alternatingProd ([] : List G) = 1 :=
rfl
@[to_additive (attr := simp)]
theorem alternatingProd_singleton (a : G) : alternatingProd [a] = a :=
rfl
@[to_additive]
theorem alternatingProd_cons_cons' (a b : G) (l : List G) :
alternatingProd (a :: b :: l) = a * b⁻¹ * alternatingProd l :=
rfl
end
@[to_additive]
theorem alternatingProd_cons_cons [DivInvMonoid G] (a b : G) (l : List G) :
alternatingProd (a :: b :: l) = a / b * alternatingProd l := by
rw [div_eq_mul_inv, alternatingProd_cons_cons']
variable [CommGroup G]
@[to_additive]
theorem alternatingProd_cons' :
∀ (a : G) (l : List G), alternatingProd (a :: l) = a * (alternatingProd l)⁻¹
| a, [] => by rw [alternatingProd_nil, inv_one, mul_one, alternatingProd_singleton]
| a, b :: l => by
rw [alternatingProd_cons_cons', alternatingProd_cons' b l, mul_inv, inv_inv, mul_assoc]
@[to_additive (attr := simp)]
theorem alternatingProd_cons (a : G) (l : List G) :
alternatingProd (a :: l) = a / alternatingProd l := by
rw [div_eq_mul_inv, alternatingProd_cons']
end Alternating
lemma sum_nat_mod (l : List ℕ) (n : ℕ) : l.sum % n = (l.map (· % n)).sum % n := by
induction l with
| nil => simp only [map_nil]
| cons a l ih =>
simpa only [map_cons, sum_cons, Nat.mod_add_mod, Nat.add_mod_mod] using congr((a + $ih) % n)
lemma prod_nat_mod (l : List ℕ) (n : ℕ) : l.prod % n = (l.map (· % n)).prod % n := by
induction l with
| nil => simp only [map_nil]
| cons a l ih =>
simpa only [prod_cons, map_cons, Nat.mod_mul_mod, Nat.mul_mod_mod] using congr((a * $ih) % n)
lemma sum_int_mod (l : List ℤ) (n : ℤ) : l.sum % n = (l.map (· % n)).sum % n := by
induction l <;> simp [Int.add_emod, *]
lemma prod_int_mod (l : List ℤ) (n : ℤ) : l.prod % n = (l.map (· % n)).prod % n := by
induction l <;> simp [Int.mul_emod, *]
end List
section MonoidHom
variable [Monoid M] [Monoid N]
@[to_additive]
theorem map_list_prod {F : Type*} [FunLike F M N] [MonoidHomClass F M N] (f : F) (l : List M) :
f l.prod = (l.map f).prod :=
(l.prod_hom f).symm
namespace MonoidHom
@[to_additive]
protected theorem map_list_prod (f : M →* N) (l : List M) : f l.prod = (l.map f).prod :=
map_list_prod f l
end MonoidHom
end MonoidHom
namespace List
theorem prod_zpow {β : Type*} [DivisionCommMonoid β] {r : ℤ} {l : List β} :
l.prod ^ r = (map (fun x ↦ x ^ r) l).prod :=
let fr : β →* β := ⟨⟨fun b ↦ b ^ r, one_zpow r⟩, (mul_zpow · · r)⟩
map_list_prod fr l
/-- In a flatten, taking the first elements up to an index which is the sum of the lengths of the
first `i` sublists, is the same as taking the flatten of the first `i` sublists. -/
lemma take_sum_flatten (L : List (List α)) (i : ℕ) :
L.flatten.take ((L.map length).take i).sum = (L.take i).flatten := by
induction L generalizing i
· simp
· cases i <;> simp [take_length_add_append, *]
/-- In a flatten, dropping all the elements up to an index which is the sum of the lengths of the
first `i` sublists, is the same as taking the join after dropping the first `i` sublists. -/
lemma drop_sum_flatten (L : List (List α)) (i : ℕ) :
L.flatten.drop ((L.map length).take i).sum = (L.drop i).flatten := by
induction L generalizing i
· simp
· cases i <;> simp [*]
end List
namespace List
/-- If all elements in a list are bounded below by `1`, then the length of the list is bounded
by the sum of the elements. -/
theorem length_le_sum_of_one_le (L : List ℕ) (h : ∀ i ∈ L, 1 ≤ i) : L.length ≤ L.sum := by
induction L with
| nil => simp
| cons j L IH =>
rw [sum_cons, length, add_comm]
exact Nat.add_le_add (h _ mem_cons_self) (IH fun i hi => h i (mem_cons.2 (Or.inr hi)))
end List |
.lake/packages/mathlib/Mathlib/Algebra/BigOperators/Group/List/Defs.lean | import Mathlib.Algebra.Group.Defs
/-!
# Sums and products from lists
This file provides basic definitions for `List.prod`, `List.sum`,
which calculate the product and sum of elements of a list
and `List.alternatingProd`, `List.alternatingSum`, their alternating counterparts.
-/
variable {ι M N : Type*}
namespace List
section Defs
set_option linter.existingAttributeWarning false in
attribute [to_additive existing] prod prod_nil prod_cons prod_one_cons prod_append prod_concat
prod_flatten prod_eq_foldl
/-- The alternating sum of a list. -/
def alternatingSum {G : Type*} [Zero G] [Add G] [Neg G] : List G → G
| [] => 0
| g :: [] => g
| g :: h :: t => g + -h + alternatingSum t
/-- The alternating product of a list. -/
@[to_additive existing]
def alternatingProd {G : Type*} [One G] [Mul G] [Inv G] : List G → G
| [] => 1
| g :: [] => g
| g :: h :: t => g * h⁻¹ * alternatingProd t
end Defs
section Mul
variable [Mul M] [One M] {a : M} {l : List M}
@[to_additive]
lemma prod_induction
(p : M → Prop) (hom : ∀ a b, p a → p b → p (a * b)) (unit : p 1) (base : ∀ x ∈ l, p x) :
p l.prod := by
induction l with
| nil => simpa
| cons a l ih =>
rw [List.prod_cons]
simp only [mem_cons, forall_eq_or_imp] at base
exact hom _ _ (base.1) (ih base.2)
end Mul
section MulOneClass
variable [MulOneClass M] {l : List M} {a : M}
@[to_additive]
theorem prod_map_one {l : List ι} :
(l.map fun _ => (1 : M)).prod = 1 := by
induction l with simp [*]
@[to_additive]
lemma prod_induction_nonempty
(p : M → Prop) (hom : ∀ a b, p a → p b → p (a * b)) (hl : l ≠ []) (base : ∀ x ∈ l, p x) :
p l.prod := by
induction l with
| nil => simp at hl
| cons a l ih =>
by_cases hl_empty : l = []
· simp [*]
rw [List.prod_cons]
simp only [mem_cons, forall_eq_or_imp] at base
exact hom _ _ (base.1) (ih hl_empty base.2)
end MulOneClass
section Monoid
variable [Monoid M] [Monoid N]
@[to_additive (attr := simp)]
theorem prod_replicate (n : ℕ) (a : M) : (replicate n a).prod = a ^ n := by
induction n with
| zero => rw [pow_zero, replicate_zero, prod_nil]
| succ n ih => rw [replicate_succ, prod_cons, ih, pow_succ']
@[to_additive sum_eq_card_nsmul]
theorem prod_eq_pow_card (l : List M) (m : M) (h : ∀ x ∈ l, x = m) : l.prod = m ^ l.length := by
rw [← prod_replicate, ← List.eq_replicate_iff.mpr ⟨rfl, h⟩]
@[to_additive]
theorem prod_hom_rel (l : List ι) {r : M → N → Prop} {f : ι → M} {g : ι → N} (h₁ : r 1 1)
(h₂ : ∀ ⦃i a b⦄, r a b → r (f i * a) (g i * b)) : r (l.map f).prod (l.map g).prod :=
List.recOn l h₁ fun a l hl => by simp only [map_cons, prod_cons, h₂ hl]
end Monoid
end List |
.lake/packages/mathlib/Mathlib/Algebra/BigOperators/Group/Multiset/Basic.lean | import Mathlib.Algebra.BigOperators.Group.List.Lemmas
import Mathlib.Algebra.BigOperators.Group.Multiset.Defs
import Mathlib.Algebra.Group.Prod
import Mathlib.Algebra.Order.Group.Multiset
import Mathlib.Algebra.Order.Sub.Unbundled.Basic
/-!
# Sums and products over multisets
In this file we define products and sums indexed by multisets. This is later used to define products
and sums indexed by finite sets.
## Main declarations
* `Multiset.prod`: `s.prod f` is the product of `f i` over all `i ∈ s`. Not to be mistaken with
the Cartesian product `Multiset.product`.
* `Multiset.sum`: `s.sum f` is the sum of `f i` over all `i ∈ s`.
-/
assert_not_exists MonoidWithZero
variable {F ι κ G M N O : Type*}
namespace Multiset
section CommMonoid
variable [CommMonoid M] [CommMonoid N] {s t : Multiset M} {a : M} {m : Multiset ι} {f g : ι → M}
@[to_additive (attr := simp)]
theorem prod_erase [DecidableEq M] (h : a ∈ s) : a * (s.erase a).prod = s.prod := by
rw [← s.coe_toList, coe_erase, prod_coe, prod_coe, List.prod_erase (mem_toList.2 h)]
@[to_additive (attr := simp)]
theorem prod_map_erase [DecidableEq ι] {a : ι} (h : a ∈ m) :
f a * ((m.erase a).map f).prod = (m.map f).prod := by
rw [← m.coe_toList, coe_erase, map_coe, map_coe, prod_coe, prod_coe,
List.prod_map_erase f (mem_toList.2 h)]
@[to_additive (attr := simp, grind =)]
theorem prod_add (s t : Multiset M) : prod (s + t) = prod s * prod t :=
Quotient.inductionOn₂ s t fun l₁ l₂ => by simp [List.prod_append]
@[to_additive]
theorem prod_nsmul (m : Multiset M) : ∀ n : ℕ, (n • m).prod = m.prod ^ n
| 0 => by
rw [zero_nsmul, pow_zero]
rfl
| n + 1 => by rw [add_nsmul, one_nsmul, pow_add, pow_one, prod_add, prod_nsmul m n]
@[to_additive]
theorem prod_filter_mul_prod_filter_not (p) [DecidablePred p] :
(s.filter p).prod * (s.filter (fun a ↦ ¬ p a)).prod = s.prod := by
rw [← prod_add, filter_add_not]
@[to_additive]
theorem prod_map_eq_pow_single [DecidableEq ι] (i : ι)
(hf : ∀ i' ≠ i, i' ∈ m → f i' = 1) : (m.map f).prod = f i ^ m.count i := by
induction m using Quotient.inductionOn
simp [List.prod_map_eq_pow_single i f hf]
@[to_additive]
theorem prod_eq_pow_single [DecidableEq M] (a : M) (h : ∀ a' ≠ a, a' ∈ s → a' = 1) :
s.prod = a ^ s.count a := by
induction s using Quotient.inductionOn; simp [List.prod_eq_pow_single a h]
@[to_additive]
lemma prod_eq_one (h : ∀ x ∈ s, x = (1 : M)) : s.prod = 1 := by
induction s using Quotient.inductionOn; simp [List.prod_eq_one h]
@[to_additive]
theorem prod_hom_ne_zero {s : Multiset M} (hs : s ≠ 0) {F : Type*} [FunLike F M N]
[MulHomClass F M N] (f : F) :
(s.map f).prod = f s.prod := by
induction s using Quot.inductionOn; aesop (add simp List.prod_hom_nonempty)
@[to_additive]
theorem prod_hom (s : Multiset M) {F : Type*} [FunLike F M N]
[MonoidHomClass F M N] (f : F) :
(s.map f).prod = f s.prod :=
Quotient.inductionOn s fun l => by simp only [l.prod_hom f, quot_mk_to_coe, map_coe, prod_coe]
@[to_additive]
theorem prod_hom' (s : Multiset ι) {F : Type*} [FunLike F M N]
[MonoidHomClass F M N] (f : F)
(g : ι → M) : (s.map fun i => f <| g i).prod = f (s.map g).prod := by
convert (s.map g).prod_hom f
exact (map_map _ _ _).symm
@[to_additive]
theorem prod_hom₂_ne_zero [CommMonoid O] {s : Multiset ι} (hs : s ≠ 0) (f : M → N → O)
(hf : ∀ a b c d, f (a * b) (c * d) = f a c * f b d) (f₁ : ι → M) (f₂ : ι → N) :
(s.map fun i => f (f₁ i) (f₂ i)).prod = f (s.map f₁).prod (s.map f₂).prod := by
induction s using Quotient.inductionOn; aesop (add simp List.prod_hom₂_nonempty)
@[to_additive]
theorem prod_hom₂ [CommMonoid O] (s : Multiset ι) (f : M → N → O)
(hf : ∀ a b c d, f (a * b) (c * d) = f a c * f b d) (hf' : f 1 1 = 1) (f₁ : ι → M)
(f₂ : ι → N) : (s.map fun i => f (f₁ i) (f₂ i)).prod = f (s.map f₁).prod (s.map f₂).prod :=
Quotient.inductionOn s fun l => by
simp only [l.prod_hom₂ f hf hf', quot_mk_to_coe, map_coe, prod_coe]
@[to_additive (attr := simp)]
theorem prod_map_mul : (m.map fun i => f i * g i).prod = (m.map f).prod * (m.map g).prod :=
m.prod_hom₂ (· * ·) mul_mul_mul_comm (mul_one _) _ _
@[to_additive]
theorem prod_map_pow {n : ℕ} : (m.map fun i => f i ^ n).prod = (m.map f).prod ^ n :=
m.prod_hom' (powMonoidHom n : M →* M) f
@[to_additive]
theorem prod_map_prod_map (m : Multiset ι) (n : Multiset κ) {f : ι → κ → M} :
prod (m.map fun a => prod <| n.map fun b => f a b) =
prod (n.map fun b => prod <| m.map fun a => f a b) :=
Multiset.induction_on m (by simp) fun a m ih => by simp [ih]
theorem prod_dvd_prod_of_le (h : s ≤ t) : s.prod ∣ t.prod := by
obtain ⟨z, rfl⟩ := exists_add_of_le h
simp only [prod_add, dvd_mul_right]
@[to_additive]
lemma _root_.map_multiset_prod [FunLike F M N] [MonoidHomClass F M N] (f : F) (s : Multiset M) :
f s.prod = (s.map f).prod := (s.prod_hom f).symm
@[to_additive]
lemma _root_.map_multiset_ne_zero_prod [FunLike F M N] [MulHomClass F M N] (f : F)
{s : Multiset M} (hs : s ≠ 0) :
f s.prod = (s.map f).prod := (s.prod_hom_ne_zero hs f).symm
@[to_additive]
protected lemma _root_.MonoidHom.map_multiset_prod (f : M →* N) (s : Multiset M) :
f s.prod = (s.map f).prod := (s.prod_hom f).symm
@[to_additive]
protected lemma _root_.MulHom.map_multiset_ne_zero_prod (f : M →ₙ* N) (s : Multiset M)
(hs : s ≠ 0) : f s.prod = (s.map f).prod := (s.prod_hom_ne_zero hs f).symm
lemma dvd_prod : a ∈ s → a ∣ s.prod :=
Quotient.inductionOn s (fun l a h ↦ by simpa using List.dvd_prod h) a
@[to_additive] lemma fst_prod (s : Multiset (M × N)) : s.prod.1 = (s.map Prod.fst).prod :=
map_multiset_prod (MonoidHom.fst _ _) _
@[to_additive] lemma snd_prod (s : Multiset (M × N)) : s.prod.2 = (s.map Prod.snd).prod :=
map_multiset_prod (MonoidHom.snd _ _) _
end CommMonoid
theorem prod_dvd_prod_of_dvd [CommMonoid N] {S : Multiset M} (g1 g2 : M → N)
(h : ∀ a ∈ S, g1 a ∣ g2 a) : (Multiset.map g1 S).prod ∣ (Multiset.map g2 S).prod := by
apply Multiset.induction_on' S
· simp
intro a T haS _ IH
simp [mul_dvd_mul (h a haS) IH]
section AddCommMonoid
variable [AddCommMonoid M]
/-- `Multiset.sum`, the sum of the elements of a multiset, promoted to a morphism of
`AddCommMonoid`s. -/
def sumAddMonoidHom : Multiset M →+ M where
toFun := sum
map_zero' := sum_zero
map_add' := sum_add
@[simp]
theorem coe_sumAddMonoidHom : (sumAddMonoidHom : Multiset M → M) = sum :=
rfl
end AddCommMonoid
section DivisionCommMonoid
variable [DivisionCommMonoid G] {m : Multiset ι} {f g : ι → G}
@[to_additive]
theorem prod_map_inv' (m : Multiset G) : (m.map Inv.inv).prod = m.prod⁻¹ :=
m.prod_hom (invMonoidHom : G →* G)
@[to_additive (attr := simp)]
theorem prod_map_inv : (m.map fun i => (f i)⁻¹).prod = (m.map f).prod⁻¹ := by
rw [← (m.map f).prod_map_inv', map_map, Function.comp_def]
@[to_additive (attr := simp)]
theorem prod_map_div : (m.map fun i => f i / g i).prod = (m.map f).prod / (m.map g).prod :=
m.prod_hom₂ (· / ·) mul_div_mul_comm (div_one _) _ _
@[to_additive]
theorem prod_map_zpow {n : ℤ} : (m.map fun i => f i ^ n).prod = (m.map f).prod ^ n := by
convert (m.map f).prod_hom (zpowGroupHom n : G →* G)
simp only [map_map, Function.comp_apply, zpowGroupHom_apply]
end DivisionCommMonoid
@[simp]
theorem sum_map_singleton (s : Multiset M) : (s.map fun a => ({a} : Multiset M)).sum = s :=
Multiset.induction_on s (by simp) (by simp)
theorem sum_nat_mod (s : Multiset ℕ) (n : ℕ) : s.sum % n = (s.map (· % n)).sum % n := by
induction s using Multiset.induction <;> simp [Nat.add_mod, *]
theorem prod_nat_mod (s : Multiset ℕ) (n : ℕ) : s.prod % n = (s.map (· % n)).prod % n := by
induction s using Multiset.induction <;> simp [Nat.mul_mod, *]
theorem sum_int_mod (s : Multiset ℤ) (n : ℤ) : s.sum % n = (s.map (· % n)).sum % n := by
induction s using Multiset.induction <;> simp [Int.add_emod, *]
theorem prod_int_mod (s : Multiset ℤ) (n : ℤ) : s.prod % n = (s.map (· % n)).prod % n := by
induction s using Multiset.induction <;> simp [Int.mul_emod, *]
section OrderedSub
theorem sum_map_tsub [AddCommMonoid M] [PartialOrder M] [ExistsAddOfLE M]
[AddLeftMono M] [AddLeftReflectLE M] [Sub M]
[OrderedSub M] (l : Multiset ι) {f g : ι → M} (hfg : ∀ x ∈ l, g x ≤ f x) :
(l.map fun x ↦ f x - g x).sum = (l.map f).sum - (l.map g).sum :=
eq_tsub_of_add_eq <| by
rw [← sum_map_add]
congr 1
exact map_congr rfl fun x hx => tsub_add_cancel_of_le <| hfg _ hx
end OrderedSub
instance {M : Type*} : IsAddTorsionFree (Multiset M) :=
⟨fun n hn x y h ↦ open Classical in Multiset.ext' fun _ ↦
(Nat.mul_right_inj hn).mp <| by simp only [← Multiset.count_nsmul, h]⟩
end Multiset |
.lake/packages/mathlib/Mathlib/Algebra/BigOperators/Group/Multiset/Defs.lean | import Mathlib.Algebra.BigOperators.Group.List.Defs
import Mathlib.Algebra.Group.Basic
import Mathlib.Data.Multiset.Basic
import Mathlib.Data.Multiset.Filter
/-!
# Sums and products over multisets
In this file we define products and sums indexed by multisets. This is later used to define products
and sums indexed by finite sets.
## Main declarations
* `Multiset.prod`: `s.prod f` is the product of `f i` over all `i ∈ s`. Not to be mistaken with
the Cartesian product `Multiset.product`.
* `Multiset.sum`: `s.sum f` is the sum of `f i` over all `i ∈ s`.
-/
assert_not_exists MonoidWithZero
variable {F ι M N : Type*}
namespace Multiset
section CommMonoid
variable [CommMonoid M] [CommMonoid N] {s t : Multiset M} {a : M} {m : Multiset ι} {f g : ι → M}
/-- Product of a multiset given a commutative monoid structure on `M`.
`prod {a, b, c} = a * b * c` -/
@[to_additive
/-- Sum of a multiset given a commutative additive monoid structure on `M`.
`sum {a, b, c} = a + b + c` -/]
def prod : Multiset M → M :=
foldr (· * ·) 1
@[to_additive]
theorem prod_eq_foldr (s : Multiset M) :
prod s = foldr (· * ·) 1 s :=
rfl
@[to_additive]
theorem prod_eq_foldl (s : Multiset M) :
prod s = foldl (· * ·) 1 s :=
(foldr_swap _ _ _).trans (by simp [mul_comm])
@[to_additive (attr := simp, norm_cast)]
theorem prod_coe (l : List M) : prod ↑l = l.prod := rfl
@[to_additive (attr := simp)]
theorem prod_toList (s : Multiset M) : s.toList.prod = s.prod := by
conv_rhs => rw [← coe_toList s]
rw [prod_coe]
@[to_additive (attr := simp, grind =)]
theorem prod_map_toList (s : Multiset ι) (f : ι → M) : (s.toList.map f).prod = (s.map f).prod := by
rw [← Multiset.prod_coe, ← Multiset.map_coe, coe_toList]
@[to_additive (attr := simp, grind =)]
theorem prod_zero : @prod M _ 0 = 1 :=
rfl
@[to_additive (attr := simp)]
theorem prod_cons (a : M) (s) : prod (a ::ₘ s) = a * prod s :=
foldr_cons _ _ _ _
@[to_additive (attr := simp)]
theorem prod_singleton (a : M) : prod {a} = a := by
simp only [mul_one, prod_cons, ← cons_zero, prod_zero]
@[to_additive]
theorem prod_pair (a b : M) : ({a, b} : Multiset M).prod = a * b := by
rw [insert_eq_cons, prod_cons, prod_singleton]
@[to_additive (attr := simp)]
theorem prod_replicate (n : ℕ) (a : M) : (replicate n a).prod = a ^ n := by
simp [replicate, List.prod_replicate]
@[to_additive]
theorem pow_count [DecidableEq M] (a : M) : a ^ s.count a = (s.filter (Eq a)).prod := by
rw [filter_eq, prod_replicate]
@[to_additive]
theorem prod_hom_rel (s : Multiset ι) {r : M → N → Prop} {f : ι → M} {g : ι → N}
(h₁ : r 1 1) (h₂ : ∀ ⦃a b c⦄, r b c → r (f a * b) (g a * c)) :
r (s.map f).prod (s.map g).prod :=
Quotient.inductionOn s fun l => by
simp only [l.prod_hom_rel h₁ h₂, quot_mk_to_coe, map_coe, prod_coe]
@[to_additive]
theorem prod_map_one : prod (m.map fun _ => (1 : M)) = 1 := by
rw [map_const', prod_replicate, one_pow]
@[to_additive]
theorem prod_induction (p : M → Prop) (s : Multiset M) (p_mul : ∀ a b, p a → p b → p (a * b))
(p_one : p 1) (p_s : ∀ a ∈ s, p a) : p s.prod := by
rw [prod_eq_foldr]
exact foldr_induction (· * ·) 1 p s p_mul p_one p_s
@[to_additive]
theorem prod_induction_nonempty (p : M → Prop) (p_mul : ∀ a b, p a → p b → p (a * b)) (hs : s ≠ ∅)
(p_s : ∀ a ∈ s, p a) : p s.prod := by
induction s using Multiset.induction_on with
| empty => simp at hs
| cons a s hsa =>
rw [prod_cons]
by_cases hs_empty : s = ∅
· simp [hs_empty, p_s a]
have hps : ∀ x, x ∈ s → p x := fun x hxs => p_s x (mem_cons_of_mem hxs)
exact p_mul a s.prod (p_s a (mem_cons_self a s)) (hsa hs_empty hps)
end CommMonoid
end Multiset |
.lake/packages/mathlib/Mathlib/Algebra/BigOperators/GroupWithZero/Finset.lean | import Mathlib.Algebra.BigOperators.Group.Finset.Basic
import Mathlib.Algebra.GroupWithZero.Units.Basic
import Mathlib.Algebra.Notation.Indicator
import Mathlib.Data.Set.Lattice
/-!
# Big operators on a finset in groups with zero
This file contains the results concerning the interaction of finset big operators with groups with
zero.
-/
open Function
variable {ι κ G₀ M₀ : Type*} {α : ι → Type*}
namespace Finset
variable [CommMonoidWithZero M₀] {p : ι → Prop} [DecidablePred p] {f : ι → M₀} {s : Finset ι}
{i : ι}
lemma prod_eq_zero (hi : i ∈ s) (h : f i = 0) : ∏ j ∈ s, f j = 0 := by
classical rw [← prod_erase_mul _ _ hi, h, mul_zero]
lemma prod_ite_zero :
(∏ i ∈ s, if p i then f i else 0) = if ∀ i ∈ s, p i then ∏ i ∈ s, f i else 0 := by
split_ifs with h
· exact prod_congr rfl fun i hi => by simp [h i hi]
· push_neg at h
rcases h with ⟨i, hi, hq⟩
exact prod_eq_zero hi (by simp [hq])
lemma prod_boole : ∏ i ∈ s, (ite (p i) 1 0 : M₀) = ite (∀ i ∈ s, p i) 1 0 := by
rw [prod_ite_zero, prod_const_one]
lemma support_prod_subset (s : Finset ι) (f : ι → κ → M₀) :
support (fun x ↦ ∏ i ∈ s, f i x) ⊆ ⋂ i ∈ s, support (f i) :=
fun _ hx ↦ Set.mem_iInter₂.2 fun _ hi H ↦ hx <| prod_eq_zero hi H
@[simp] lemma _root_.Set.indicator_pi_one_apply (s : Finset ι) (t : ∀ i, Set (α i)) (f : ∀ i, α i) :
((s : Set ι).pi t).indicator 1 f = ∏ i ∈ s, (t i).indicator (M := M₀) 1 (f i) := by
classical simp [Set.indicator, prod_boole]
variable [Nontrivial M₀] [NoZeroDivisors M₀]
lemma prod_eq_zero_iff : ∏ x ∈ s, f x = 0 ↔ ∃ a ∈ s, f a = 0 := by
classical
induction s using Finset.induction_on with
| empty => exact ⟨Not.elim one_ne_zero, fun ⟨_, H, _⟩ => by simp at H⟩
| insert _ _ ha ih => rw [prod_insert ha, mul_eq_zero, exists_mem_insert, ih]
lemma prod_ne_zero_iff : ∏ x ∈ s, f x ≠ 0 ↔ ∀ a ∈ s, f a ≠ 0 := by
rw [Ne, prod_eq_zero_iff]
push_neg; rfl
lemma support_prod (s : Finset ι) (f : ι → κ → M₀) :
support (fun j ↦ ∏ i ∈ s, f i j) = ⋂ i ∈ s, support (f i) :=
Set.ext fun x ↦ by simp [support, prod_eq_zero_iff]
end Finset
namespace Fintype
variable [Fintype ι] [CommMonoidWithZero M₀] {p : ι → Prop} [DecidablePred p] {f : ι → M₀}
lemma prod_ite_zero : (∏ i, if p i then f i else 0) = if ∀ i, p i then ∏ i, f i else 0 := by
simp [Finset.prod_ite_zero]
lemma prod_boole : ∏ i, (ite (p i) 1 0 : M₀) = ite (∀ i, p i) 1 0 := by simp [Finset.prod_boole]
end Fintype
lemma Units.mk0_prod [CommGroupWithZero G₀] (s : Finset ι) (f : ι → G₀) (h) :
Units.mk0 (∏ i ∈ s, f i) h =
∏ i ∈ s.attach, Units.mk0 (f i) fun hh ↦ h (Finset.prod_eq_zero i.2 hh) := by
induction s using Finset.cons_induction_on <;> simp [*] |
.lake/packages/mathlib/Mathlib/Algebra/BigOperators/GroupWithZero/Action.lean | import Mathlib.Algebra.BigOperators.Finprod
import Mathlib.Algebra.GroupWithZero.Action.Defs
import Mathlib.Algebra.Order.Group.Multiset
import Mathlib.Data.Finset.Basic
import Mathlib.Algebra.Group.Action.Basic
/-!
# Lemmas about group actions on big operators
This file contains results about two kinds of actions:
* sums over `DistribSMul`: `r • ∑ x ∈ s, f x = ∑ x ∈ s, r • f x`
* products over `MulDistribMulAction` (with primed name): `r • ∏ x ∈ s, f x = ∏ x ∈ s, r • f x`
* products over `SMulCommClass` (with unprimed name):
`b ^ s.card • ∏ x ∈ s, f x = ∏ x ∈ s, b • f x`
Note that analogous lemmas for `Module`s like `Finset.sum_smul` appear in other files.
-/
variable {M N γ : Type*}
section
variable [AddMonoid N] [DistribSMul M N]
theorem List.smul_sum {r : M} {l : List N} : r • l.sum = (l.map (r • ·)).sum :=
map_list_sum (DistribSMul.toAddMonoidHom N r) l
end
section
variable [Monoid M] [Monoid N] [MulDistribMulAction M N]
theorem List.smul_prod' {r : M} {l : List N} : r • l.prod = (l.map (r • ·)).prod :=
map_list_prod (MulDistribMulAction.toMonoidHom N r) l
end
section
variable [AddCommMonoid N] [DistribSMul M N] {r : M}
theorem Multiset.smul_sum {s : Multiset N} : r • s.sum = (s.map (r • ·)).sum :=
(DistribSMul.toAddMonoidHom N r).map_multiset_sum s
theorem Finset.smul_sum {f : γ → N} {s : Finset γ} :
(r • ∑ x ∈ s, f x) = ∑ x ∈ s, r • f x :=
map_sum (DistribSMul.toAddMonoidHom N r) f s
theorem smul_finsum_mem {f : γ → N} {s : Set γ} (hs : s.Finite) :
r • ∑ᶠ x ∈ s, f x = ∑ᶠ x ∈ s, r • f x :=
(DistribSMul.toAddMonoidHom N r).map_finsum_mem f hs
end
section
variable [Monoid M] [CommMonoid N] [MulDistribMulAction M N]
theorem Multiset.smul_prod' {r : M} {s : Multiset N} : r • s.prod = (s.map (r • ·)).prod :=
(MulDistribMulAction.toMonoidHom N r).map_multiset_prod s
theorem Finset.smul_prod' {r : M} {f : γ → N} {s : Finset γ} :
(r • ∏ x ∈ s, f x) = ∏ x ∈ s, r • f x :=
map_prod (MulDistribMulAction.toMonoidHom N r) f s
theorem smul_finprod' {ι : Sort*} [Finite ι] {f : ι → N} (r : M) :
r • ∏ᶠ x : ι, f x = ∏ᶠ x : ι, r • (f x) := by
cases nonempty_fintype (PLift ι)
simp only [finprod_eq_prod_plift_of_mulSupport_subset (s := Finset.univ) (by simp),
Finset.smul_prod']
variable {G : Type*} [Group G] [MulDistribMulAction G N]
theorem Finset.smul_prod_perm [Fintype G] (b : N) (g : G) :
(g • ∏ h : G, h • b) = ∏ h : G, h • b := by
simp only [smul_prod', smul_smul]
exact Finset.prod_bijective (g * ·) (Group.mulLeft_bijective g) (by simp) (fun _ _ ↦ rfl)
theorem smul_finprod_perm [Finite G] (b : N) (g : G) :
(g • ∏ᶠ h : G, h • b) = ∏ᶠ h : G, h • b := by
cases nonempty_fintype G
simp only [finprod_eq_prod_of_fintype, Finset.smul_prod_perm]
end
namespace List
@[to_additive]
theorem smul_prod [Monoid M] [MulOneClass N] [MulAction M N] [IsScalarTower M N N]
[SMulCommClass M N N] (l : List N) (m : M) :
m ^ l.length • l.prod = (l.map (m • ·)).prod := by
induction l with
| nil => simp
| cons head tail ih => simp [← ih, smul_mul_smul_comm, pow_succ']
end List
namespace Multiset
@[to_additive]
theorem smul_prod [Monoid M] [CommMonoid N] [MulAction M N] [IsScalarTower M N N]
[SMulCommClass M N N] (s : Multiset N) (b : M) :
b ^ card s • s.prod = (s.map (b • ·)).prod :=
Quot.induction_on s <| by simp [List.smul_prod]
end Multiset
namespace Finset
theorem smul_prod
[CommMonoid N] [Monoid M] [MulAction M N] [IsScalarTower M N N] [SMulCommClass M N N]
(s : Finset N) (b : M) (f : N → N) :
b ^ s.card • ∏ x ∈ s, f x = ∏ x ∈ s, b • f x := by
have : Multiset.map (fun (x : N) ↦ b • f x) s.val =
Multiset.map (fun x ↦ b • x) (Multiset.map f s.val) := by
simp only [Multiset.map_map, Function.comp_apply]
simp_rw [prod_eq_multiset_prod, card_def, this, ← Multiset.smul_prod _ b, Multiset.card_map]
theorem prod_smul
[CommMonoid N] [CommMonoid M] [MulAction M N] [IsScalarTower M N N] [SMulCommClass M N N]
(s : Finset N) (b : N → M) (f : N → N) :
∏ i ∈ s, b i • f i = (∏ i ∈ s, b i) • ∏ i ∈ s, f i := by
induction s using Finset.cons_induction_on with
| empty => simp
| cons _ _ hj ih => rw [prod_cons, ih, smul_mul_smul_comm, ← prod_cons hj, ← prod_cons hj]
end Finset |
.lake/packages/mathlib/Mathlib/Algebra/BigOperators/Ring/List.lean | import Mathlib.Algebra.GroupWithZero.Commute
import Mathlib.Algebra.GroupWithZero.Divisibility
import Mathlib.Algebra.Ring.Basic
import Mathlib.Algebra.Ring.Divisibility.Basic
import Mathlib.Algebra.Ring.Commute
import Mathlib.Algebra.BigOperators.Group.List.Basic
/-!
# Big operators on a list in rings
This file contains the results concerning the interaction of list big operators with rings.
-/
open MulOpposite List
variable {ι κ M M₀ R : Type*}
namespace Commute
variable [NonUnitalNonAssocSemiring R]
lemma list_sum_right (a : R) (l : List R) (h : ∀ b ∈ l, Commute a b) : Commute a l.sum := by
induction l with
| nil => exact Commute.zero_right _
| cons x xs ih =>
rw [List.sum_cons]
exact (h _ mem_cons_self).add_right (ih fun j hj ↦ h _ <| mem_cons_of_mem _ hj)
lemma list_sum_left (b : R) (l : List R) (h : ∀ a ∈ l, Commute a b) : Commute l.sum b :=
((Commute.list_sum_right _ _) fun _x hx ↦ (h _ hx).symm).symm
end Commute
namespace List
section HasDistribNeg
variable [Monoid M] [HasDistribNeg M]
@[simp]
lemma prod_map_neg (l : List M) :
(l.map Neg.neg).prod = (-1) ^ l.length * l.prod := by
induction l <;> simp [*, pow_succ, ((Commute.neg_one_left _).pow_left _).left_comm]
end HasDistribNeg
section MonoidWithZero
variable [MonoidWithZero M₀] {l : List M₀}
/-- If zero is an element of a list `l`, then `List.prod l = 0`. If the domain is a nontrivial
monoid with zero with no zero divisors, then this implication becomes an `iff`, see
`List.prod_eq_zero_iff`. -/
lemma prod_eq_zero : ∀ {l : List M₀}, (0 : M₀) ∈ l → l.prod = 0
-- | absurd h (not_mem_nil _)
| a :: l, h => by
rw [prod_cons]
rcases mem_cons.1 h with ha | hl
exacts [mul_eq_zero_of_left ha.symm _, mul_eq_zero_of_right _ (prod_eq_zero hl)]
variable [Nontrivial M₀] [NoZeroDivisors M₀]
/-- Product of elements of a list `l` equals zero if and only if `0 ∈ l`. See also
`List.prod_eq_zero` for an implication that needs weaker typeclass assumptions. -/
@[simp] lemma prod_eq_zero_iff : ∀ {l : List M₀}, l.prod = 0 ↔ (0 : M₀) ∈ l
| [] => by simp
| a :: l => by rw [prod_cons, mul_eq_zero, prod_eq_zero_iff, mem_cons, eq_comm]
lemma prod_ne_zero (hL : (0 : M₀) ∉ l) : l.prod ≠ 0 := mt prod_eq_zero_iff.1 hL
end MonoidWithZero
section NonUnitalNonAssocSemiring
variable [NonUnitalNonAssocSemiring R] (l : List ι) (f : ι → R) (r : R)
lemma sum_map_mul_left : (l.map fun b ↦ r * f b).sum = r * (l.map f).sum :=
sum_map_hom l f <| AddMonoidHom.mulLeft r
lemma sum_map_mul_right : (l.map fun b ↦ f b * r).sum = (l.map f).sum * r :=
sum_map_hom l f <| AddMonoidHom.mulRight r
end NonUnitalNonAssocSemiring
lemma dvd_sum [NonUnitalSemiring R] {a} {l : List R} (h : ∀ x ∈ l, a ∣ x) : a ∣ l.sum := by
induction l with
| nil => exact dvd_zero _
| cons x l ih =>
rw [List.sum_cons]
exact dvd_add (h _ mem_cons_self) (ih fun x hx ↦ h x (mem_cons_of_mem _ hx))
@[simp] lemma sum_zipWith_distrib_left [NonUnitalNonAssocSemiring R] (f : ι → κ → R) (a : R) :
∀ (l₁ : List ι) (l₂ : List κ),
(zipWith (fun i j ↦ a * f i j) l₁ l₂).sum = a * (zipWith f l₁ l₂).sum
| [], _ => by simp
| _, [] => by simp
| i :: l₁, j :: l₂ => by simp [sum_zipWith_distrib_left, mul_add]
end List |
.lake/packages/mathlib/Mathlib/Algebra/BigOperators/Ring/Nat.lean | import Mathlib.Algebra.BigOperators.Group.Finset.Lemmas
import Mathlib.Data.Set.Finite.Lattice
import Mathlib.SetTheory.Cardinal.Finite
/-!
# Big operators on a finset in the natural numbers
This file contains the results concerning the interaction of finset big operators with natural
numbers.
-/
variable {ι : Type*}
namespace Finset
lemma even_sum_iff_even_card_odd {s : Finset ι} (f : ι → ℕ) :
Even (∑ i ∈ s, f i) ↔ Even #{x ∈ s | Odd (f x)} := by
rw [← Finset.sum_filter_add_sum_filter_not _ (fun x ↦ Even (f x)), Nat.even_add]
simp only [Finset.mem_filter, and_imp, imp_self, implies_true, Finset.even_sum, true_iff]
rw [Nat.even_iff, Finset.sum_nat_mod, Finset.sum_filter]
simp +contextual only [Nat.not_even_iff_odd, Nat.odd_iff.mp]
simp_rw [← Finset.sum_filter, ← Nat.even_iff, Finset.card_eq_sum_ones]
lemma odd_sum_iff_odd_card_odd {s : Finset ι} (f : ι → ℕ) :
Odd (∑ i ∈ s, f i) ↔ Odd #{x ∈ s | Odd (f x)} := by
simp only [← Nat.not_even_iff_odd, even_sum_iff_even_card_odd]
theorem card_preimage_eq_sum_card_image_eq {M : Type*} {f : ι → M} {s : Finset M}
(hb : ∀ b ∈ s, Set.Finite {a | f a = b}) :
Nat.card (f ⁻¹' s) = ∑ b ∈ s, Nat.card {a // f a = b} := by
classical
-- `t = s ∩ Set.range f` as a `Finset`
let t := (Set.finite_coe_iff.mp (Finite.Set.finite_inter_of_left ↑s (Set.range f))).toFinset
rw [show Nat.card (f ⁻¹' s) = Nat.card (f ⁻¹' t) by simp [t]]
rw [show ∑ b ∈ s, Nat.card {a //f a = b} = ∑ b ∈ t, Nat.card {a | f a = b} by
exact (Finset.sum_subset (by simp [t]) (by aesop)).symm]
have ht : Set.Finite (f ⁻¹' t) := Set.Finite.preimage' (finite_toSet t) (by aesop)
rw [Nat.card_eq_card_finite_toFinset ht, Finset.card_eq_sum_card_image (f := f)]
refine Finset.sum_congr ?_ fun m hm ↦ ?_
· simpa [← Finset.coe_inj, t] using Set.image_preimage_eq_inter_range
· rw [Nat.card_eq_card_finite_toFinset (hb _ (by aesop))]
suffices {a | f a = m} ⊆ ht.toFinset from
congr_arg (Finset.card ·) (Finset.ext_iff.mpr fun a ↦ by simpa using fun h ↦ this h)
intro _ h
simpa using by rwa [h]
end Finset |
.lake/packages/mathlib/Mathlib/Algebra/BigOperators/Ring/Finset.lean | import Mathlib.Algebra.BigOperators.Group.Finset.Pi
import Mathlib.Algebra.BigOperators.Group.Finset.Piecewise
import Mathlib.Algebra.BigOperators.GroupWithZero.Finset
import Mathlib.Algebra.BigOperators.Ring.Multiset
import Mathlib.Data.Finset.Max
import Mathlib.Data.Fintype.Powerset
import Mathlib.Data.Int.Cast.Lemmas
/-!
# Results about big operators with values in a (semi)ring
We prove results about big operators that involve some interaction between
multiplicative and additive structures on the values being combined.
-/
assert_not_exists Field
open Fintype
variable {ι κ M R : Type*} {s s₁ s₂ : Finset ι} {i : ι}
namespace Finset
lemma prod_neg [CommMonoid M] [HasDistribNeg M] (f : ι → M) :
∏ x ∈ s, -f x = (-1) ^ #s * ∏ x ∈ s, f x := by
simpa using (s.1.map f).prod_map_neg
section AddCommMonoidWithOne
variable [AddCommMonoidWithOne R]
lemma natCast_card_filter (p) [DecidablePred p] (s : Finset ι) :
(#{x ∈ s | p x} : R) = ∑ a ∈ s, if p a then (1 : R) else 0 := by
rw [sum_ite, sum_const_zero, add_zero, sum_const, nsmul_one]
@[simp] lemma sum_boole (p) [DecidablePred p] (s : Finset ι) :
(∑ x ∈ s, if p x then 1 else 0 : R) = #{x ∈ s | p x} :=
(natCast_card_filter _ _).symm
end AddCommMonoidWithOne
section NonUnitalNonAssocSemiring
variable [NonUnitalNonAssocSemiring R]
lemma sum_mul (s : Finset ι) (f : ι → R) (a : R) :
(∑ i ∈ s, f i) * a = ∑ i ∈ s, f i * a := map_sum (AddMonoidHom.mulRight a) _ s
lemma mul_sum (s : Finset ι) (f : ι → R) (a : R) :
a * ∑ i ∈ s, f i = ∑ i ∈ s, a * f i := map_sum (AddMonoidHom.mulLeft a) _ s
lemma sum_mul_sum (s : Finset ι) (t : Finset κ) (f : ι → R) (g : κ → R) :
(∑ i ∈ s, f i) * ∑ j ∈ t, g j = ∑ i ∈ s, ∑ j ∈ t, f i * g j := by
simp_rw [sum_mul, ← mul_sum]
lemma _root_.Fintype.sum_mul_sum [Fintype ι] [Fintype κ] (f : ι → R) (g : κ → R) :
(∑ i, f i) * ∑ j, g j = ∑ i, ∑ j, f i * g j :=
Finset.sum_mul_sum _ _ _ _
lemma _root_.Commute.sum_right (s : Finset ι) (f : ι → R) (b : R)
(h : ∀ i ∈ s, Commute b (f i)) : Commute b (∑ i ∈ s, f i) :=
(Commute.multiset_sum_right _ _) fun b hb => by
obtain ⟨i, hi, rfl⟩ := Multiset.mem_map.mp hb
exact h _ hi
lemma _root_.Commute.sum_left (s : Finset ι) (f : ι → R) (b : R)
(h : ∀ i ∈ s, Commute (f i) b) : Commute (∑ i ∈ s, f i) b :=
((Commute.sum_right _ _ _) fun _i hi => (h _ hi).symm).symm
lemma sum_range_succ_mul_sum_range_succ (m n : ℕ) (f g : ℕ → R) :
(∑ i ∈ range (m + 1), f i) * ∑ i ∈ range (n + 1), g i =
(∑ i ∈ range m, f i) * ∑ i ∈ range n, g i +
f m * ∑ i ∈ range n, g i + (∑ i ∈ range m, f i) * g n + f m * g n := by
simp only [add_mul, mul_add, add_assoc, sum_range_succ]
end NonUnitalNonAssocSemiring
section NonUnitalSemiring
variable [NonUnitalSemiring R] {f : ι → R} {a : R}
lemma dvd_sum (h : ∀ i ∈ s, a ∣ f i) : a ∣ ∑ i ∈ s, f i :=
Multiset.dvd_sum fun y hy => by rcases Multiset.mem_map.1 hy with ⟨x, hx, rfl⟩; exact h x hx
end NonUnitalSemiring
section NonAssocSemiring
variable [NonAssocSemiring R] [DecidableEq ι]
lemma sum_mul_boole (s : Finset ι) (f : ι → R) (i : ι) :
∑ j ∈ s, f j * ite (i = j) 1 0 = ite (i ∈ s) (f i) 0 := by simp
lemma sum_boole_mul (s : Finset ι) (f : ι → R) (i : ι) :
∑ j ∈ s, ite (i = j) 1 0 * f j = ite (i ∈ s) (f i) 0 := by simp
end NonAssocSemiring
section CommSemiring
variable [CommSemiring R]
/-- If `f = g = h` everywhere but at `i`, where `f i = g i + h i`, then the product of `f` over `s`
is the sum of the products of `g` and `h`. -/
theorem prod_add_prod_eq {s : Finset ι} {i : ι} {f g h : ι → R} (hi : i ∈ s)
(h1 : g i + h i = f i) (h2 : ∀ j ∈ s, j ≠ i → g j = f j) (h3 : ∀ j ∈ s, j ≠ i → h j = f j) :
(∏ i ∈ s, g i) + ∏ i ∈ s, h i = ∏ i ∈ s, f i := by
classical
simp_rw [prod_eq_mul_prod_diff_singleton hi, ← h1, right_distrib]
congr 2 <;> apply prod_congr rfl <;> simpa
section DecidableEq
variable [DecidableEq ι]
/-- The product over a sum can be written as a sum over the product of sets, `Finset.Pi`.
`Finset.prod_univ_sum` is an alternative statement when the product is over `univ`. -/
lemma prod_sum {κ : ι → Type*} (s : Finset ι) (t : ∀ i, Finset (κ i)) (f : ∀ i, κ i → R) :
∏ a ∈ s, ∑ b ∈ t a, f a b = ∑ p ∈ s.pi t, ∏ x ∈ s.attach, f x.1 (p x.1 x.2) := by
classical
induction s using Finset.induction with
| empty => simp
| insert a s ha ih =>
have h₁ : ∀ x ∈ t a, ∀ y ∈ t a, x ≠ y →
Disjoint (image (Pi.cons s a x) (pi s t)) (image (Pi.cons s a y) (pi s t)) := by
intro x _ y _ h
simp only [disjoint_iff_ne, mem_image]
rintro _ ⟨p₂, _, eq₂⟩ _ ⟨p₃, _, eq₃⟩ eq
have : Pi.cons s a x p₂ a (mem_insert_self _ _)
= Pi.cons s a y p₃ a (mem_insert_self _ _) := by rw [eq₂, eq₃, eq]
rw [Pi.cons_same, Pi.cons_same] at this
exact h this
rw [prod_insert ha, pi_insert ha, ih, sum_mul, sum_biUnion h₁]
refine sum_congr rfl fun b _ => ?_
have h₂ : ∀ p₁ ∈ pi s t, ∀ p₂ ∈ pi s t, Pi.cons s a b p₁ = Pi.cons s a b p₂ → p₁ = p₂ :=
fun p₁ _ p₂ _ eq => Pi.cons_injective ha eq
rw [sum_image h₂, mul_sum]
refine sum_congr rfl fun g _ => ?_
rw [attach_insert, prod_insert, prod_image]
· simp only [Pi.cons_same]
congr with ⟨v, hv⟩
congr
exact (Pi.cons_ne (by rintro rfl; exact ha hv)).symm
· exact fun _ _ _ _ => Subtype.eq ∘ Subtype.mk.inj
· simpa only [mem_image, mem_attach, Subtype.mk.injEq, true_and,
Subtype.exists, exists_prop, exists_eq_right] using ha
/-- The product over `univ` of a sum can be written as a sum over the product of sets,
`Fintype.piFinset`. `Finset.prod_sum` is an alternative statement when the product is not
over `univ`. -/
lemma prod_univ_sum {κ : ι → Type*} [Fintype ι] (t : ∀ i, Finset (κ i)) (f : ∀ i, κ i → R) :
∏ i, ∑ j ∈ t i, f i j = ∑ x ∈ piFinset t, ∏ i, f i (x i) := by
simp only [prod_attach_univ, prod_sum, Finset.sum_univ_pi]
lemma sum_prod_piFinset [Fintype ι] (s : Finset κ) (g : ι → κ → R) :
∑ f ∈ piFinset fun _ : ι ↦ s, ∏ i, g i (f i) = ∏ i, ∑ j ∈ s, g i j := by
rw [← prod_univ_sum]
lemma sum_pow' (s : Finset κ) (f : κ → R) (n : ℕ) :
(∑ a ∈ s, f a) ^ n = ∑ p ∈ piFinset fun _i : Fin n ↦ s, ∏ i, f (p i) := by
convert @prod_univ_sum (Fin n) _ _ _ _ _ (fun _i ↦ s) fun _i d ↦ f d; simp
/-- The product of `f a + g a` over all of `s` is the sum over the powerset of `s` of the product of
`f` over a subset `t` times the product of `g` over the complement of `t` -/
theorem prod_add (f g : ι → R) (s : Finset ι) :
∏ i ∈ s, (f i + g i) = ∑ t ∈ s.powerset, (∏ i ∈ t, f i) * ∏ i ∈ s \ t, g i := by
classical
calc
∏ i ∈ s, (f i + g i) =
∏ i ∈ s, ∑ p ∈ ({True, False} : Finset Prop), if p then f i else g i := by simp
_ = ∑ p ∈ (s.pi fun _ => {True, False} : Finset (∀ a ∈ s, Prop)),
∏ a ∈ s.attach, if p a.1 a.2 then f a.1 else g a.1 := prod_sum _ _ _
_ = ∑ t ∈ s.powerset, (∏ a ∈ t, f a) * ∏ a ∈ s \ t, g a :=
sum_bij'
(fun f _ ↦ {a ∈ s | ∃ h : a ∈ s, f a h})
(fun t _ a _ => a ∈ t)
(by simp)
(by simp [Classical.em])
(by simp_rw [mem_filter, funext_iff, eq_iff_iff, mem_pi, mem_insert]; tauto)
(by simp_rw [Finset.ext_iff, @mem_filter _ _ (id _), mem_powerset]; tauto)
(fun a _ ↦ by
simp only [prod_ite, filter_attach', prod_map, Function.Embedding.coeFn_mk,
Subtype.map_coe, id_eq, prod_attach]
congr 2 with x
simp only [mem_filter, mem_sdiff, not_and, not_exists, and_congr_right_iff]
tauto)
end DecidableEq
theorem prod_one_add {f : ι → R} (s : Finset ι) :
∏ i ∈ s, (1 + f i) = ∑ t ∈ s.powerset, ∏ i ∈ t, f i := by
classical simp only [add_comm (1 : R), prod_add, prod_const_one, mul_one]
theorem prod_add_one {f : ι → R} (s : Finset ι) :
∏ i ∈ s, (f i + 1) = ∑ t ∈ s.powerset, ∏ i ∈ t, f i := by
classical simp only [prod_add, prod_const_one, mul_one]
/-- `∏ i, (f i + g i) = (∏ i, f i) + ∑ i, g i * (∏ j < i, f j + g j) * (∏ j > i, f j)`. -/
theorem prod_add_ordered [LinearOrder ι] (s : Finset ι) (f g : ι → R) :
∏ i ∈ s, (f i + g i) =
(∏ i ∈ s, f i) +
∑ i ∈ s, g i * (∏ j ∈ s with j < i, (f j + g j)) * ∏ j ∈ s with i < j, f j := by
refine Finset.induction_on_max s (by simp) ?_
clear s
intro a s ha ihs
have ha' : a ∉ s := fun ha' => lt_irrefl a (ha a ha')
rw [prod_insert ha', prod_insert ha', sum_insert ha', filter_insert, if_neg (lt_irrefl a),
filter_true_of_mem ha, ihs, add_mul, mul_add, mul_add, add_assoc]
congr 1
rw [add_comm]
congr 1
· rw [filter_false_of_mem, prod_empty, mul_one]
exact (forall_mem_insert _ _ _).2 ⟨lt_irrefl a, fun i hi => (ha i hi).not_gt⟩
· rw [mul_sum]
refine sum_congr rfl fun i hi => ?_
rw [filter_insert, if_neg (ha i hi).not_gt, filter_insert, if_pos (ha i hi), prod_insert,
mul_left_comm]
exact mt (fun ha => (mem_filter.1 ha).1) ha'
/-- Summing `a ^ #t * b ^ (n - #t)` over all finite subsets `t` of a finset `s`
gives `(a + b) ^ #s`. -/
theorem sum_pow_mul_eq_add_pow (a b : R) (s : Finset ι) :
(∑ t ∈ s.powerset, a ^ #t * b ^ (#s - #t)) = (a + b) ^ #s := by
classical
rw [← prod_const, prod_add]
refine Finset.sum_congr rfl fun t ht => ?_
rw [prod_const, prod_const, ← card_sdiff_of_subset (mem_powerset.1 ht)]
/-- Summing `a^#s * b^(n-#s)` over all finite subsets `s` of a fintype of cardinality `n`
gives `(a + b)^n`. The "good" proof involves expanding along all coordinates using the fact that
`x^n` is multilinear, but multilinear maps are only available now over rings, so we give instead
a proof reducing to the usual binomial theorem to have a result over semirings. -/
lemma _root_.Fintype.sum_pow_mul_eq_add_pow (ι : Type*) [Fintype ι] (a b : R) :
∑ s : Finset ι, a ^ #s * b ^ (Fintype.card ι - #s) = (a + b) ^ Fintype.card ι :=
Finset.sum_pow_mul_eq_add_pow _ _ _
@[norm_cast]
theorem prod_natCast (s : Finset ι) (f : ι → ℕ) : ↑(∏ i ∈ s, f i : ℕ) = ∏ i ∈ s, (f i : R) :=
map_prod (Nat.castRingHom R) f s
end CommSemiring
section CommRing
variable [CommRing R]
/-- The product of `f i - g i` over all of `s` is the sum over the powerset of `s` of the product of
`g` over a subset `t` times the product of `f` over the complement of `t` times `(-1) ^ #t`. -/
lemma prod_sub [DecidableEq ι] (f g : ι → R) (s : Finset ι) :
∏ i ∈ s, (f i - g i) = ∑ t ∈ s.powerset, (-1) ^ #t * (∏ i ∈ s \ t, f i) * ∏ i ∈ t, g i := by
simp [sub_eq_neg_add, prod_add, prod_neg, mul_right_comm]
/-- `∏ i, (f i - g i) = (∏ i, f i) - ∑ i, g i * (∏ j < i, f j - g j) * (∏ j > i, f j)`. -/
lemma prod_sub_ordered [LinearOrder ι] (s : Finset ι) (f g : ι → R) :
∏ i ∈ s, (f i - g i) =
(∏ i ∈ s, f i) -
∑ i ∈ s, g i * (∏ j ∈ s with j < i, (f j - g j)) * ∏ j ∈ s with i < j, f j := by
simp only [sub_eq_add_neg]
convert prod_add_ordered s f fun i => -g i
simp
/-- `∏ i, (1 - f i) = 1 - ∑ i, f i * (∏ j < i, 1 - f j)`. This formula is useful in construction of
a partition of unity from a collection of “bump” functions. -/
theorem prod_one_sub_ordered [LinearOrder ι] (s : Finset ι) (f : ι → R) :
∏ i ∈ s, (1 - f i) = 1 - ∑ i ∈ s, f i * ∏ j ∈ s with j < i, (1 - f j) := by
rw [prod_sub_ordered]
simp
theorem prod_range_natCast_sub (n k : ℕ) :
∏ i ∈ range k, (n - i : R) = (∏ i ∈ range k, (n - i) : ℕ) := by
rw [prod_natCast]
rcases le_or_gt k n with hkn | hnk
· exact prod_congr rfl fun i hi => (Nat.cast_sub <| (mem_range.1 hi).le.trans hkn).symm
· rw [← mem_range] at hnk
rw [prod_eq_zero hnk, prod_eq_zero hnk] <;> simp
end CommRing
end Finset
open Finset
namespace Fintype
variable {ι κ R : Type*} [Fintype ι] [Fintype κ] [CommSemiring R]
lemma sum_pow (f : ι → R) (n : ℕ) : (∑ a, f a) ^ n = ∑ p : Fin n → ι, ∏ i, f (p i) := by
simp [sum_pow']
variable [DecidableEq ι]
/-- A product of sums can be written as a sum of products. -/
lemma prod_sum {κ : ι → Type*} [∀ i, Fintype (κ i)] (f : ∀ i, κ i → R) :
∏ i, ∑ j, f i j = ∑ x : ∀ i, κ i, ∏ i, f i (x i) := Finset.prod_univ_sum _ _
lemma prod_add (f g : ι → R) : ∏ a, (f a + g a) = ∑ t, (∏ a ∈ t, f a) * ∏ a ∈ tᶜ, g a := by
simpa [compl_eq_univ_sdiff] using Finset.prod_add f g univ
end Fintype
namespace Nat
variable {ι : Type*} {s : Finset ι} {f : ι → ℕ} {n : ℕ}
protected lemma sum_div (hf : ∀ i ∈ s, n ∣ f i) : (∑ i ∈ s, f i) / n = ∑ i ∈ s, f i / n := by
obtain rfl | hn := n.eq_zero_or_pos
· simp
rw [Nat.div_eq_iff_eq_mul_left hn (dvd_sum hf), sum_mul]
refine sum_congr rfl fun s hs ↦ ?_
rw [Nat.div_mul_cancel (hf _ hs)]
@[simp, norm_cast]
lemma cast_list_sum [AddMonoidWithOne R] (s : List ℕ) : (↑s.sum : R) = (s.map (↑)).sum :=
map_list_sum (castAddMonoidHom R) _
@[simp, norm_cast]
lemma cast_list_prod [Semiring R] (s : List ℕ) : (↑s.prod : R) = (s.map (↑)).prod :=
map_list_prod (castRingHom R) _
@[simp, norm_cast]
lemma cast_multiset_sum [AddCommMonoidWithOne R] (s : Multiset ℕ) :
(↑s.sum : R) = (s.map (↑)).sum :=
map_multiset_sum (castAddMonoidHom R) _
@[simp, norm_cast]
lemma cast_multiset_prod [CommSemiring R] (s : Multiset ℕ) : (↑s.prod : R) = (s.map (↑)).prod :=
map_multiset_prod (castRingHom R) _
@[simp, norm_cast]
lemma cast_sum [AddCommMonoidWithOne R] (s : Finset ι) (f : ι → ℕ) :
↑(∑ x ∈ s, f x : ℕ) = ∑ x ∈ s, (f x : R) :=
map_sum (castAddMonoidHom R) _ _
@[simp, norm_cast]
lemma cast_prod [CommSemiring R] (f : ι → ℕ) (s : Finset ι) :
(↑(∏ i ∈ s, f i) : R) = ∏ i ∈ s, (f i : R) :=
map_prod (castRingHom R) _ _
end Nat
namespace Int
variable {ι : Type*} {s : Finset ι} {f : ι → ℤ} {n : ℤ}
protected lemma sum_div (hf : ∀ i ∈ s, n ∣ f i) : (∑ i ∈ s, f i) / n = ∑ i ∈ s, f i / n := by
obtain rfl | hn := eq_or_ne n 0
· simp
rw [Int.ediv_eq_iff_eq_mul_left hn (dvd_sum hf), sum_mul]
refine sum_congr rfl fun s hs ↦ ?_
rw [Int.ediv_mul_cancel (hf _ hs)]
@[simp, norm_cast]
lemma cast_list_sum [AddGroupWithOne R] (s : List ℤ) : (↑s.sum : R) = (s.map (↑)).sum :=
map_list_sum (castAddHom R) _
@[simp, norm_cast]
lemma cast_list_prod [Ring R] (s : List ℤ) : (↑s.prod : R) = (s.map (↑)).prod :=
map_list_prod (castRingHom R) _
@[simp, norm_cast]
lemma cast_multiset_sum [AddCommGroupWithOne R] (s : Multiset ℤ) :
(↑s.sum : R) = (s.map (↑)).sum :=
map_multiset_sum (castAddHom R) _
@[simp, norm_cast]
lemma cast_multiset_prod {R : Type*} [CommRing R] (s : Multiset ℤ) :
(↑s.prod : R) = (s.map (↑)).prod :=
map_multiset_prod (castRingHom R) _
@[simp, norm_cast]
lemma cast_sum [AddCommGroupWithOne R] (s : Finset ι) (f : ι → ℤ) :
↑(∑ x ∈ s, f x : ℤ) = ∑ x ∈ s, (f x : R) :=
map_sum (castAddHom R) _ _
@[simp, norm_cast]
lemma cast_prod {R : Type*} [CommRing R] (f : ι → ℤ) (s : Finset ι) :
(↑(∏ i ∈ s, f i) : R) = ∏ i ∈ s, (f i : R) :=
map_prod (Int.castRingHom R) _ _
end Int |
.lake/packages/mathlib/Mathlib/Algebra/BigOperators/Ring/Multiset.lean | import Mathlib.Algebra.BigOperators.Group.Multiset.Basic
import Mathlib.Algebra.BigOperators.Ring.List
import Mathlib.Data.Multiset.Antidiagonal
import Mathlib.Data.Multiset.Sections
/-! # Lemmas about `Multiset.sum` and `Multiset.prod` requiring extra algebra imports -/
variable {ι M M₀ R : Type*}
namespace Multiset
section CommMonoid
variable [CommMonoid M] [HasDistribNeg M]
@[simp] lemma prod_map_neg (s : Multiset M) : (s.map Neg.neg).prod = (-1) ^ card s * s.prod :=
Quotient.inductionOn s (by simp)
end CommMonoid
section CommMonoidWithZero
variable [CommMonoidWithZero M₀] {s : Multiset M₀}
lemma prod_eq_zero (h : (0 : M₀) ∈ s) : s.prod = 0 := by
rcases Multiset.exists_cons_of_mem h with ⟨s', hs'⟩; simp [hs', Multiset.prod_cons]
variable [NoZeroDivisors M₀] [Nontrivial M₀] {s : Multiset M₀}
@[simp] lemma prod_eq_zero_iff : s.prod = 0 ↔ (0 : M₀) ∈ s :=
Quotient.inductionOn s fun l ↦ by rw [quot_mk_to_coe, prod_coe]; exact List.prod_eq_zero_iff
lemma prod_ne_zero (h : (0 : M₀) ∉ s) : s.prod ≠ 0 := mt prod_eq_zero_iff.1 h
end CommMonoidWithZero
section NonUnitalNonAssocSemiring
variable [NonUnitalNonAssocSemiring R] {a : R} {s : Multiset ι} {f : ι → R}
lemma sum_map_mul_left : sum (s.map fun i ↦ a * f i) = a * sum (s.map f) :=
Multiset.induction_on s (by simp) fun i s ih => by simp [ih, mul_add]
lemma sum_map_mul_right : sum (s.map fun i ↦ f i * a) = sum (s.map f) * a :=
Multiset.induction_on s (by simp) fun a s ih => by simp [ih, add_mul]
end NonUnitalNonAssocSemiring
section NonUnitalSemiring
variable [NonUnitalSemiring R] {s : Multiset R} {a : R}
lemma dvd_sum : (∀ x ∈ s, a ∣ x) → a ∣ s.sum :=
Multiset.induction_on s (fun _ ↦ dvd_zero _) fun x s ih h ↦ by
rw [sum_cons]
exact dvd_add (h _ (mem_cons_self _ _)) (ih fun y hy ↦ h _ <| mem_cons.2 <| Or.inr hy)
end NonUnitalSemiring
section CommSemiring
variable [CommSemiring R]
lemma prod_map_sum {s : Multiset (Multiset R)} :
prod (s.map sum) = sum ((Sections s).map prod) :=
Multiset.induction_on s (by simp) fun a s ih ↦ by
simp [ih, map_bind, sum_map_mul_left, sum_map_mul_right]
lemma prod_map_add {s : Multiset ι} {f g : ι → R} :
prod (s.map fun i ↦ f i + g i) =
sum ((antidiagonal s).map fun p ↦ (p.1.map f).prod * (p.2.map g).prod) := by
refine s.induction_on ?_ fun a s ih ↦ ?_
· simp only [map_zero, prod_zero, antidiagonal_zero, map_singleton, mul_one, sum_singleton]
· simp only [map_cons, prod_cons, ih, sum_map_mul_left.symm, add_mul, mul_left_comm (f a),
mul_left_comm (g a), sum_map_add, antidiagonal_cons, Prod.map_fst, Prod.map_snd,
id_eq, map_add, map_map, Function.comp_apply, mul_assoc, sum_add]
exact add_comm _ _
end CommSemiring
end Multiset
open Multiset
namespace Commute
variable [NonUnitalNonAssocSemiring R] (s : Multiset R)
theorem multiset_sum_right (a : R) (h : ∀ b ∈ s, Commute a b) : Commute a s.sum := by
induction s using Quotient.inductionOn
rw [quot_mk_to_coe, sum_coe]
exact Commute.list_sum_right _ _ h
theorem multiset_sum_left (b : R) (h : ∀ a ∈ s, Commute a b) : Commute s.sum b :=
((Commute.multiset_sum_right _ _) fun _ ha => (h _ ha).symm).symm
end Commute |
.lake/packages/mathlib/Mathlib/Algebra/Module/GradedModule.lean | import Mathlib.RingTheory.GradedAlgebra.Basic
import Mathlib.Algebra.GradedMulAction
import Mathlib.Algebra.DirectSum.Decomposition
import Mathlib.Algebra.Module.BigOperators
/-!
# Graded Module
Given an `R`-algebra `A` graded by `𝓐`, a graded `A`-module `M` is expressed as
`DirectSum.Decomposition 𝓜` and `SetLike.GradedSMul 𝓐 𝓜`.
Then `⨁ i, 𝓜 i` is an `A`-module and is isomorphic to `M`.
## Tags
graded module
-/
section
open DirectSum
variable {ιA ιB : Type*} (A : ιA → Type*) (M : ιB → Type*)
namespace DirectSum
open GradedMonoid
/-- A graded version of `DistribMulAction`. -/
class GdistribMulAction [AddMonoid ιA] [VAdd ιA ιB] [GMonoid A] [∀ i, AddMonoid (M i)]
extends GMulAction A M where
smul_add {i j} (a : A i) (b c : M j) : smul a (b + c) = smul a b + smul a c
smul_zero {i j} (a : A i) : smul a (0 : M j) = 0
/-- A graded version of `Module`. -/
class Gmodule [AddMonoid ιA] [VAdd ιA ιB] [∀ i, AddMonoid (A i)] [∀ i, AddMonoid (M i)] [GMonoid A]
extends GdistribMulAction A M where
add_smul {i j} (a a' : A i) (b : M j) : smul (a + a') b = smul a b + smul a' b
zero_smul {i j} (b : M j) : smul (0 : A i) b = 0
/-- A graded version of `Semiring.toModule`. -/
instance GSemiring.toGmodule [AddMonoid ιA] [∀ i : ιA, AddCommMonoid (A i)]
[h : GSemiring A] : Gmodule A A :=
{ GMonoid.toGMulAction A with
smul_add := fun _ _ _ => h.mul_add _ _ _
smul_zero := fun _ => h.mul_zero _
add_smul := fun _ _ => h.add_mul _ _
zero_smul := fun _ => h.zero_mul _ }
variable [AddMonoid ιA] [VAdd ιA ιB] [∀ i : ιA, AddCommMonoid (A i)] [∀ i, AddCommMonoid (M i)]
/-- The piecewise multiplication from the `Mul` instance, as a bundled homomorphism. -/
@[simps]
def gsmulHom [GMonoid A] [Gmodule A M] {i j} : A i →+ M j →+ M (i +ᵥ j) where
toFun a :=
{ toFun := fun b => GSMul.smul a b
map_zero' := GdistribMulAction.smul_zero _
map_add' := GdistribMulAction.smul_add _ }
map_zero' := AddMonoidHom.ext fun a => Gmodule.zero_smul a
map_add' _a₁ _a₂ := AddMonoidHom.ext fun _b => Gmodule.add_smul _ _ _
namespace Gmodule
/-- For graded monoid `A` and a graded module `M` over `A`. `Gmodule.smulAddMonoidHom` is the
`⨁ᵢ Aᵢ`-scalar multiplication on `⨁ᵢ Mᵢ` induced by `gsmul_hom`. -/
def smulAddMonoidHom [DecidableEq ιA] [DecidableEq ιB] [GMonoid A] [Gmodule A M] :
(⨁ i, A i) →+ (⨁ i, M i) →+ ⨁ i, M i :=
toAddMonoid fun _i =>
AddMonoidHom.flip <|
toAddMonoid fun _j => AddMonoidHom.flip <| (of M _).compHom.comp <| gsmulHom A M
section
open GradedMonoid DirectSum Gmodule
instance [DecidableEq ιA] [DecidableEq ιB] [GMonoid A] [Gmodule A M] :
SMul (⨁ i, A i) (⨁ i, M i) where
smul x y := smulAddMonoidHom A M x y
@[simp]
theorem smul_def [DecidableEq ιA] [DecidableEq ιB] [GMonoid A] [Gmodule A M]
(x : ⨁ i, A i) (y : ⨁ i, M i) :
x • y = smulAddMonoidHom _ _ x y := rfl
@[simp]
theorem smulAddMonoidHom_apply_of_of [DecidableEq ιA] [DecidableEq ιB] [GMonoid A] [Gmodule A M]
{i j} (x : A i) (y : M j) :
smulAddMonoidHom A M (DirectSum.of A i x) (of M j y) = of M (i +ᵥ j) (GSMul.smul x y) := by
simp [smulAddMonoidHom]
theorem of_smul_of [DecidableEq ιA] [DecidableEq ιB] [GMonoid A] [Gmodule A M]
{i j} (x : A i) (y : M j) :
DirectSum.of A i x • of M j y = of M (i +ᵥ j) (GSMul.smul x y) := by simp
open AddMonoidHom
-- Almost identical to the proof of `direct_sum.one_mul`
private theorem one_smul' [DecidableEq ιA] [DecidableEq ιB] [GMonoid A] [Gmodule A M]
(x : ⨁ i, M i) :
(1 : ⨁ i, A i) • x = x := by
suffices smulAddMonoidHom A M 1 = AddMonoidHom.id (⨁ i, M i) from DFunLike.congr_fun this x
apply DirectSum.addHom_ext; intro i xi
rw [show (1 : DirectSum ιA fun i => A i) = (of A 0) GOne.one by rfl]
rw [smulAddMonoidHom_apply_of_of]
exact DirectSum.of_eq_of_gradedMonoid_eq (one_smul (GradedMonoid A) <| GradedMonoid.mk i xi)
-- Almost identical to the proof of `direct_sum.mul_assoc`
private theorem mul_smul' [DecidableEq ιA] [DecidableEq ιB] [GSemiring A] [Gmodule A M]
(a b : ⨁ i, A i)
(c : ⨁ i, M i) : (a * b) • c = a • b • c := by
suffices
(-- `fun a b c ↦ (a * b) • c` as a bundled hom
smulAddMonoidHom
A M).compHom.comp
(DirectSum.mulHom A) =
(AddMonoidHom.compHom AddMonoidHom.flipHom <|
(smulAddMonoidHom A M).flip.compHom.comp <| smulAddMonoidHom A M).flip
from-- `fun a b c ↦ a • (b • c)` as a bundled hom
DFunLike.congr_fun (DFunLike.congr_fun (DFunLike.congr_fun this a) b) c
ext ai ax bi bx ci cx : 6
dsimp only [coe_comp, Function.comp_apply, compHom_apply_apply, flip_apply, flipHom_apply]
rw [smulAddMonoidHom_apply_of_of, smulAddMonoidHom_apply_of_of, DirectSum.mulHom_of_of,
smulAddMonoidHom_apply_of_of]
exact
DirectSum.of_eq_of_gradedMonoid_eq
(mul_smul (GradedMonoid.mk ai ax) (GradedMonoid.mk bi bx) (GradedMonoid.mk ci cx))
/-- The `Module` derived from `gmodule A M`. -/
instance module [DecidableEq ιA] [DecidableEq ιB] [GSemiring A] [Gmodule A M] :
Module (⨁ i, A i) (⨁ i, M i) where
one_smul := one_smul' _ _
mul_smul := mul_smul' _ _
smul_add r := (smulAddMonoidHom A M r).map_add
smul_zero r := (smulAddMonoidHom A M r).map_zero
add_smul r s x := by simp only [smul_def, map_add, AddMonoidHom.add_apply]
zero_smul x := by simp only [smul_def, map_zero, AddMonoidHom.zero_apply]
end
end Gmodule
end DirectSum
end
open DirectSum
variable {ιA ιM R A M σ σ' : Type*}
variable [AddMonoid ιA] [AddAction ιA ιM] [CommSemiring R] [Semiring A] [Algebra R A]
variable (𝓐 : ιA → σ') [SetLike σ' A]
variable (𝓜 : ιM → σ)
namespace SetLike
instance gmulAction [AddMonoid M] [DistribMulAction A M] [SetLike σ M] [SetLike.GradedMonoid 𝓐]
[SetLike.GradedSMul 𝓐 𝓜] : GradedMonoid.GMulAction (fun i => 𝓐 i) fun i => 𝓜 i :=
{ SetLike.toGSMul 𝓐 𝓜 with
one_smul := fun ⟨_i, _m⟩ => Sigma.subtype_ext (zero_vadd _ _) (one_smul _ _)
mul_smul := fun ⟨_i, _a⟩ ⟨_j, _a'⟩ ⟨_k, _b⟩ =>
Sigma.subtype_ext (add_vadd _ _ _) (mul_smul _ _ _) }
instance gdistribMulAction [AddMonoid M] [DistribMulAction A M] [SetLike σ M]
[AddSubmonoidClass σ M] [SetLike.GradedMonoid 𝓐] [SetLike.GradedSMul 𝓐 𝓜] :
DirectSum.GdistribMulAction (fun i => 𝓐 i) fun i => 𝓜 i :=
{ SetLike.gmulAction 𝓐 𝓜 with
smul_add := fun _a _b _c => Subtype.ext <| smul_add _ _ _
smul_zero := fun _a => Subtype.ext <| smul_zero _ }
variable [AddCommMonoid M] [Module A M] [SetLike σ M] [AddSubmonoidClass σ' A]
[AddSubmonoidClass σ M] [SetLike.GradedMonoid 𝓐] [SetLike.GradedSMul 𝓐 𝓜]
/-- `[SetLike.GradedMonoid 𝓐] [SetLike.GradedSMul 𝓐 𝓜]` is the internal version of graded
module, the internal version can be translated into the external version `gmodule`. -/
instance gmodule : DirectSum.Gmodule (fun i => 𝓐 i) fun i => 𝓜 i :=
{ SetLike.gdistribMulAction 𝓐 𝓜 with
smul := fun x y => ⟨(x : A) • (y : M), SetLike.GradedSMul.smul_mem x.2 y.2⟩
add_smul := fun _a _a' _b => Subtype.ext <| add_smul _ _ _
zero_smul := fun _b => Subtype.ext <| zero_smul _ _ }
end SetLike
namespace GradedModule
variable [AddCommMonoid M] [Module A M] [SetLike σ M] [AddSubmonoidClass σ' A]
[AddSubmonoidClass σ M] [SetLike.GradedMonoid 𝓐] [SetLike.GradedSMul 𝓐 𝓜]
/-- The smul multiplication of `A` on `⨁ i, 𝓜 i` from `(⨁ i, 𝓐 i) →+ (⨁ i, 𝓜 i) →+ ⨁ i, 𝓜 i`
turns `⨁ i, 𝓜 i` into an `A`-module
-/
def isModule [DecidableEq ιA] [DecidableEq ιM] [GradedRing 𝓐] : Module A (⨁ i, 𝓜 i) :=
{ Module.compHom _ (DirectSum.decomposeRingEquiv 𝓐 : A ≃+* ⨁ i, 𝓐 i).toRingHom with
smul := fun a b => DirectSum.decompose 𝓐 a • b }
/-- `⨁ i, 𝓜 i` and `M` are isomorphic as `A`-modules.
"The internal version" and "the external version" are isomorphism as `A`-modules.
-/
def linearEquiv [DecidableEq ιA] [DecidableEq ιM] [GradedRing 𝓐] [DirectSum.Decomposition 𝓜] :
@LinearEquiv A A _ _ (RingHom.id A) (RingHom.id A) _ _ M (⨁ i, 𝓜 i) _
_ _ (by letI := isModule 𝓐 𝓜; infer_instance) := by
letI h := isModule 𝓐 𝓜
refine ⟨⟨(DirectSum.decomposeAddEquiv 𝓜).toAddHom, ?_⟩,
(DirectSum.decomposeAddEquiv 𝓜).symm.toFun, (DirectSum.decomposeAddEquiv 𝓜).left_inv,
(DirectSum.decomposeAddEquiv 𝓜).right_inv⟩
intro x y
classical
rw [AddHom.toFun_eq_coe, ← DirectSum.sum_support_decompose 𝓐 x, map_sum, Finset.sum_smul,
AddEquiv.coe_toAddHom, map_sum, Finset.sum_smul]
refine Finset.sum_congr rfl (fun i _hi => ?_)
rw [RingHom.id_apply, ← DirectSum.sum_support_decompose 𝓜 y, map_sum, Finset.smul_sum, map_sum,
Finset.smul_sum]
refine Finset.sum_congr rfl (fun j _hj => ?_)
rw [show (decompose 𝓐 x i : A) • (decomposeAddEquiv 𝓜 ↑(decompose 𝓜 y j) : (⨁ i, 𝓜 i)) =
DirectSum.Gmodule.smulAddMonoidHom _ _ (decompose 𝓐 ↑(decompose 𝓐 x i))
(decomposeAddEquiv 𝓜 ↑(decompose 𝓜 y j)) from DirectSum.Gmodule.smul_def _ _ _ _]
simp only [decomposeAddEquiv_apply, decompose_coe, Gmodule.smulAddMonoidHom_apply_of_of]
convert DirectSum.decompose_coe 𝓜 _
rfl
end GradedModule |
.lake/packages/mathlib/Mathlib/Algebra/Module/SpanRank.lean | import Mathlib.Data.Set.Card
import Mathlib.Data.ENat.Lattice
import Mathlib.RingTheory.Finiteness.Defs
import Mathlib.LinearAlgebra.FreeModule.Basic
import Mathlib.LinearAlgebra.Dimension.StrongRankCondition
/-!
# Minimum Cardinality of generating set of a submodule
In this file, we define the minimum cardinality of a generating set for a submodule, which is
implemented as `spanFinrank` and `spanRank`.
`spanFinrank` takes value in `ℕ` and equals `0` when no finite generating set exists.
`spanRank` takes value as a cardinal.
## Main Definitions
* `spanFinrank`: The minimum cardinality of a generating set of a submodule as a natural
number. If no finite generating set exists, it is defined to be `0`.
* `spanRank`: The minimum cardinality of a generating set of a submodule as a cardinal.
* `FG.generators`: For a finitely generated submodule, get a set of generating elements with minimal
cardinality.
## Main Results
* `FG.exists_span_set_card_eq_spanFinrank` : Any submodule has a generating set of cardinality equal
to `spanRank`.
* `rank_eq_spanRank_of_free` : For a ring `R` (not necessarily commutative) satisfying
`StrongRankCondition R`, if `M` is a free `R`-module, then the `spanRank` of `M` equals to the
rank of M.
* `rank_le_spanRank` : For a ring `R` (not necessarily commutative) satisfying
`StrongRankCondition R`, if `M` is an `R`-module, then the `spanRank` of `M` is less than or equal
to the rank of M.
## Tags
submodule, generating subset, span rank
## Remark
Note that the corresponding API - `Module.rank` is only defined for a module rather than a
submodule, so there is some asymmetry here. Further refactoring might be needed if this difference
creates a friction later on.
-/
namespace Submodule
section Defs
universe u
variable {R : Type*} {M : Type u} [Semiring R] [AddCommMonoid M] [Module R M]
open Cardinal
/-- The minimum cardinality of a generating set of a submodule as a cardinal. -/
noncomputable def spanRank (p : Submodule R M) : Cardinal := ⨅ (s : {s : Set M // span R s = p}), #s
/-- The minimum cardinality of a generating set of a submodule as a natural number. If no finite
generating set exists, the span rank is defined to be `0`. -/
noncomputable def spanFinrank (p : Submodule R M) : ℕ := (spanRank p).toNat
instance (p : Submodule R M) : Nonempty {s : Set M // span R s = p} := ⟨⟨p, by simp⟩⟩
lemma spanRank_toENat_eq_iInf_encard (p : Submodule R M) : p.spanRank.toENat =
(⨅ (s : Set M) (_ : span R s = p), s.encard) := by
rw [spanRank]
apply le_antisymm
· refine le_iInf₂ (fun s hs ↦ ?_)
rw [Set.encard, ENat.card]
exact toENat.monotone' (ciInf_le' _ (⟨s, hs⟩ : {s : Set M // span R s = p}))
· have := congrFun toENat_comp_ofENat.{u}.symm (⨅ (s : Set M) (_ : span R s = p), s.encard)
rw [id_eq] at this; rw [this]
refine toENat.monotone' (le_ciInf fun s ↦ ?_)
have : ofENat.{u} (⨅ (s' : Set M), ⨅ (_ : span R s' = p), s'.encard) ≤ ofENat s.1.encard :=
ofENatHom.monotone' (le_trans (ciInf_le' _ s.1) (ciInf_le' _ s.2))
apply le_trans this
rw [Set.encard, ENat.card]
exact Cardinal.ofENat_toENat_le _
lemma spanRank_toENat_eq_iInf_finset_card (p : Submodule R M) :
p.spanRank.toENat = ⨅ (s : {s : Finset M // span R s = p}), (s.1.card : ℕ∞) := by
rw [spanRank_toENat_eq_iInf_encard]
rcases eq_or_ne (⨅ (s : Set M) (_ : span R s = p), s.encard) ⊤ with (h1 | h2)
· rw [h1, eq_comm]; simp_rw [iInf_eq_top] at h1 ⊢
exact fun s ↦ False.elim (Set.encard_ne_top_iff.mpr s.1.finite_toSet (h1 s.1 s.2))
· simp_rw [← Set.encard_coe_eq_coe_finsetCard]
apply le_antisymm
· exact le_iInf fun s ↦ iInf₂_le (s.1 : Set M) s.2
· refine le_iInf fun s ↦ le_iInf fun h ↦ ?_
by_cases hs : s.Finite
· exact iInf_le_of_le ⟨hs.toFinset, by simpa⟩ (by simp)
· rw [Set.Infinite.encard_eq hs]
exact OrderTop.le_top _
lemma spanFinrank_eq_iInf (p : Submodule R M) :
p.spanFinrank = ⨅ (s : {s : Finset M // span R s = p}), s.1.card := by
simp [spanFinrank, Cardinal.toNat, spanRank_toENat_eq_iInf_finset_card, ENat.iInf_toNat]
/-- A submodule's `spanRank` is finite if and only if it is finitely generated. -/
@[simp]
lemma spanRank_finite_iff_fg {p : Submodule R M} : p.spanRank < aleph0 ↔ p.FG := by
rw [spanRank, Submodule.fg_def]
constructor
· rintro h
obtain ⟨s, hs⟩ : ⨅ (s : {s : Set M // span R s = p}), #s ∈
Set.range (fun (s : {s : Set M // span R s = p}) ↦ #s) := csInf_mem ⟨#p, ⟨⟨p, by simp⟩, rfl⟩⟩
refine ⟨s.1, ?_, s.2⟩
simpa [← hs] using h
· rintro ⟨s, hs₁, hs₂⟩
exact (ciInf_le' _ ⟨s, hs₂⟩).trans_lt (by simpa)
/-- A submodule is finitely generated if and only if its `spanRank` is equal to its `spanFinrank`.
-/
lemma fg_iff_spanRank_eq_spanFinrank {p : Submodule R M} : p.spanRank = p.spanFinrank ↔ p.FG := by
rw [spanFinrank, ← spanRank_finite_iff_fg, eq_comm]
exact cast_toNat_eq_iff_lt_aleph0
lemma spanRank_span_le_card (s : Set M) : (Submodule.span R s).spanRank ≤ #s := by
rw [spanRank]
let s' : {s1 : Set M // span R s1 = span R s} := ⟨s, rfl⟩
exact ciInf_le' _ s'
lemma spanRank_span_range_of_linearIndependent [RankCondition R] {ι : Type u} {v : ι → M}
(hv : v.Injective) (hs : LinearIndependent R v) :
(span R (.range v)).spanRank = #ι := by
refine le_antisymm (le_trans (spanRank_span_le_card _) mk_range_le) (le_ciInf fun x ↦ ?_)
have : #x.1 = #((Subtype.val : span R (.range v) → _) ⁻¹' x.1) :=
(mk_preimage_of_injective_of_subset_range _ _ Subtype.val_injective (by simp [← x.2])).symm
rw [this]
refine le_trans ?_ ((Module.Basis.span hs).le_span (R := R) (J := Subtype.val ⁻¹' x.1) ?_)
· rw [mk_range_eq]
exact .of_comp (f := Subtype.val) (by convert hv; ext; simp [Module.Basis.span_apply])
· apply map_injective_of_injective (f := (span R _).subtype) (injective_subtype _)
simp [map_span, Set.image_preimage_eq_inter_range, Set.inter_eq_self_of_subset_left, ← x.2]
lemma spanRank_span_of_linearIndepOn [RankCondition R] (s : Set M) (hs : LinearIndepOn R id s) :
(span R s).spanRank = #s := by
simp [← spanRank_span_range_of_linearIndependent Subtype.val_injective hs]
lemma spanFinrank_span_le_encard (s : Set M) : (span R s).spanFinrank ≤ s.encard := by
rw [spanFinrank, Set.encard, ENat.card]
exact le_trans (by simp) ((toENat).monotone' (spanRank_span_le_card (R := R) s))
lemma spanFinrank_span_le_ncard_of_finite {s : Set M} (hs : s.Finite) :
(span R s).spanFinrank ≤ s.ncard := by
rw [← Nat.cast_le (α := ℕ∞)]
exact le_trans (spanFinrank_span_le_encard _) hs.cast_ncard_eq.ge
/-- Constructs a generating set with cardinality equal to the `spanRank` of the submodule -/
theorem exists_span_set_card_eq_spanRank (p : Submodule R M) :
∃ s : Set M, #s = p.spanRank ∧ span R s = p := by
rw [spanRank]
obtain ⟨s, hs⟩ : ⨅ (s : {s : Set M // span R s = p}), #s ∈
Set.range (fun (s : {s : Set M // span R s = p}) ↦ #s) := csInf_mem ⟨#p, ⟨⟨p, by simp⟩, rfl⟩⟩
exact ⟨s.1, ⟨hs, s.2⟩⟩
/-- Constructs a generating set with cardinality equal to the `spanFinrank` of the submodule when
the submodule is finitely generated. -/
theorem FG.exists_span_set_encard_eq_spanFinrank {p : Submodule R M} (h : p.FG) :
∃ s : Set M, s.encard = p.spanFinrank ∧ span R s = p := by
obtain ⟨s, ⟨hs₁, hs₂⟩⟩ := exists_span_set_card_eq_spanRank p
refine ⟨s, ⟨?_, hs₂⟩⟩
have := fg_iff_spanRank_eq_spanFinrank.mpr h
rw [Set.encard, ENat.card, spanFinrank, hs₁, this]
simp
/-- For a finitely generated submodule, its spanRank is less than or equal to a cardinal `a`
if and only if there is a generating subset with cardinality less than or equal to `a`. -/
lemma FG.spanRank_le_iff_exists_span_set_card_le (p : Submodule R M) {a : Cardinal} :
p.spanRank ≤ a ↔ ∃ s : Set M, #s ≤ a ∧ span R s = p := by
constructor
· intro h
obtain ⟨s, ⟨hs₁, hs₂⟩⟩ := exists_span_set_card_eq_spanRank p
exact ⟨s, ⟨hs₁ ▸ h, hs₂⟩⟩
· exact (fun ⟨s, ⟨hs₁, hs₂⟩⟩ ↦ hs₂.symm ▸ (le_trans (spanRank_span_le_card s) hs₁))
@[simp]
lemma spanRank_eq_zero_iff_eq_bot {I : Submodule R M} : I.spanRank = 0 ↔ I = ⊥ := by
constructor
· intro h
obtain ⟨s, ⟨hs₁, hs₂⟩⟩ :=
(FG.spanRank_le_iff_exists_span_set_card_le I (a := 0)).mp (by rw [h])
simp only [nonpos_iff_eq_zero, mk_eq_zero_iff, Set.isEmpty_coe_sort] at hs₁
simp_all
· rintro rfl; rw [spanRank]
exact Cardinal.iInf_eq_zero_iff.mpr (Or.inr ⟨⟨∅, by simp⟩, by simp⟩)
@[simp]
lemma spanRank_bot : (⊥ : Ideal R).spanRank = 0 := Submodule.spanRank_eq_zero_iff_eq_bot.mpr rfl
@[simp]
lemma spanFinrank_bot : (⊥ : Submodule R M).spanFinrank = 0 := by simp [spanFinrank]
/-- Generating elements for the submodule of minimum cardinality. -/
noncomputable def generators (p : Submodule R M) : Set M :=
Classical.choose (exists_span_set_card_eq_spanRank p)
lemma generators_card (p : Submodule R M) : #(generators p) = spanRank p :=
(Classical.choose_spec (exists_span_set_card_eq_spanRank p)).1
lemma FG.generators_ncard {p : Submodule R M} (h : p.FG) :
(generators p).ncard = spanFinrank p := by
rw [← Nat.cast_inj (R := Cardinal), ← fg_iff_spanRank_eq_spanFinrank.mpr h, Set.ncard, Set.encard,
ENat.card, generators_card, toNat_toENat, ← spanFinrank]
exact (fg_iff_spanRank_eq_spanFinrank.mpr h).symm
lemma FG.finite_generators {p : Submodule R M} (hp : p.FG) :
p.generators.Finite := by
rw [← Cardinal.lt_aleph0_iff_set_finite, Submodule.generators_card]
exact spanRank_finite_iff_fg.mpr hp
/-- The span of the generators equals the submodule. -/
lemma span_generators (p : Submodule R M) : span R (generators p) = p :=
(Classical.choose_spec (exists_span_set_card_eq_spanRank p)).2
/-- The elements of the generators are in the submodule. -/
lemma FG.generators_mem (p : Submodule R M) : generators p ⊆ p := by
nth_rw 2 [← span_generators p]
exact subset_span (s := generators p)
lemma spanRank_sup_le_sum_spanRank {p q : Submodule R M} :
(p ⊔ q).spanRank ≤ p.spanRank + q.spanRank := by
apply (FG.spanRank_le_iff_exists_span_set_card_le (p ⊔ q)).mpr
obtain ⟨sp, ⟨hp₁, rfl⟩⟩ := exists_span_set_card_eq_spanRank p
obtain ⟨sq, ⟨hq₁, rfl⟩⟩ := exists_span_set_card_eq_spanRank q
exact ⟨sp ∪ sq, ⟨hp₁ ▸ hq₁ ▸ (Cardinal.mk_union_le sp sq), span_union sp sq⟩⟩
lemma spanFinrank_eq_zero_iff_eq_bot {p : Submodule R M} (h : p.FG) :
p.spanFinrank = 0 ↔ p = ⊥ := by
refine ⟨fun heq ↦ ?_, fun h ↦ h ▸ by simp⟩
rw [← Submodule.FG.generators_ncard h, Set.ncard_eq_zero h.finite_generators] at heq
rw [← p.span_generators, heq, span_empty]
lemma spanFinrank_singleton {m : M} (hm : m ≠ 0) : (span R {m}).spanFinrank = 1 := by
apply le_antisymm ?_ ?_
· exact le_trans (Submodule.spanFinrank_span_le_ncard_of_finite (by simp)) (by simp)
· by_contra!
simp [Submodule.spanFinrank_eq_zero_iff_eq_bot (fg_span_singleton m), hm] at this
end Defs
end Submodule
section rank
open Cardinal Module Submodule
universe u v w
variable {R : Type u} {M : Type v} [Semiring R] [AddCommMonoid M] [Module R M]
lemma Module.Basis.mk_eq_spanRank [RankCondition R] {ι : Type*} (v : Basis ι R M) :
#(Set.range v) = (⊤ : Submodule R M).spanRank := by
rw [← v.span_eq, spanRank_span_of_linearIndepOn]
exact v.linearIndependent.linearIndepOn_id
theorem Submodule.rank_eq_spanRank_of_free [Module.Free R M] [StrongRankCondition R] :
Module.rank R M = (⊤ : Submodule R M).spanRank := by
haveI := nontrivial_of_invariantBasisNumber R
obtain ⟨I, B⟩ := ‹Module.Free R M›
rw [← Basis.mk_eq_rank'' B, ← Basis.mk_eq_spanRank B, ← Cardinal.lift_id #(Set.range B),
Cardinal.mk_range_eq_of_injective B.injective, Cardinal.lift_id _]
theorem Submodule.rank_le_spanRank [StrongRankCondition R] :
Module.rank R M ≤ (⊤ : Submodule R M).spanRank := by
rw [Module.rank, Submodule.spanRank]
refine ciSup_le' (fun ι ↦ (le_ciInf fun s ↦ ?_))
have := linearIndependent_le_span'' ι.2 s.1 s.2
simpa
end rank |
.lake/packages/mathlib/Mathlib/Algebra/Module/Card.lean | import Mathlib.Algebra.NoZeroSMulDivisors.Basic
import Mathlib.SetTheory.Cardinal.Basic
/-!
# Cardinality of a module
This file proves that the cardinality of a module without zero divisors is at least the cardinality
of its base ring.
-/
open Function
universe u v
namespace Cardinal
/-- The cardinality of a nontrivial module over a ring is at least the cardinality of the ring if
there are no zero divisors (for instance if the ring is a field) -/
theorem mk_le_of_module (R : Type u) (E : Type v)
[AddCommGroup E] [Ring R] [Module R E] [Nontrivial E] [NoZeroSMulDivisors R E] :
Cardinal.lift.{v} (#R) ≤ Cardinal.lift.{u} (#E) := by
obtain ⟨x, hx⟩ : ∃ (x : E), x ≠ 0 := exists_ne 0
have : Injective (fun k ↦ k • x) := smul_left_injective R hx
exact lift_mk_le_lift_mk_of_injective this
end Cardinal |
.lake/packages/mathlib/Mathlib/Algebra/Module/Opposite.lean | import Mathlib.Algebra.GroupWithZero.Action.Opposite
import Mathlib.Algebra.Module.Defs
import Mathlib.Algebra.Ring.Opposite
/-!
# Module operations on `Mᵐᵒᵖ`
This file contains definitions that build on top of the group action definitions in
`Mathlib/Algebra/GroupWithZero/Action/Opposite.lean`.
-/
assert_not_exists LinearMap
section
variable {R M : Type*} [Semiring R] [AddCommMonoid M]
-- see Note [lower instance priority]
/-- Like `Semiring.toModule`, but multiplies on the right. -/
instance (priority := 910) Semiring.toOppositeModule [Semiring R] : Module Rᵐᵒᵖ R :=
{ MonoidWithZero.toOppositeMulActionWithZero R with
smul_add := fun _ _ _ => add_mul _ _ _
add_smul := fun _ _ _ => mul_add _ _ _ }
end
namespace MulOpposite
universe u v
variable (R : Type u) {M : Type v} [Semiring R] [AddCommMonoid M] [Module R M]
/-- `MulOpposite.distribMulAction` extends to a `Module` -/
instance instModule : Module R Mᵐᵒᵖ where
add_smul _ _ _ := unop_injective <| add_smul _ _ _
zero_smul _ := unop_injective <| zero_smul _ _
end MulOpposite |
.lake/packages/mathlib/Mathlib/Algebra/Module/DedekindDomain.lean | import Mathlib.Algebra.Module.Torsion.Basic
import Mathlib.RingTheory.DedekindDomain.Ideal.Lemmas
/-!
# Modules over a Dedekind domain
Over a Dedekind domain, an `I`-torsion module is the internal direct sum of its `p i ^ e i`-torsion
submodules, where `I = ∏ i, p i ^ e i` is its unique decomposition in prime ideals.
Therefore, as any finitely generated torsion module is `I`-torsion for some `I`, it is an internal
direct sum of its `p i ^ e i`-torsion submodules for some prime ideals `p i` and numbers `e i`.
-/
universe u v
variable {R : Type u} [CommRing R] [IsDomain R] {M : Type v} [AddCommGroup M] [Module R M]
open scoped DirectSum
namespace Submodule
variable [IsDedekindDomain R]
open UniqueFactorizationMonoid
/-- Over a Dedekind domain, an `I`-torsion module is the internal direct sum of its `p i ^ e i`-
torsion submodules, where `I = ∏ i, p i ^ e i` is its unique decomposition in prime ideals. -/
theorem isInternal_prime_power_torsion_of_is_torsion_by_ideal [DecidableEq (Ideal R)]
{I : Ideal R} (hI : I ≠ ⊥) (hM : Module.IsTorsionBySet R M I) :
DirectSum.IsInternal fun p : (factors I).toFinset =>
torsionBySet R M (p ^ (factors I).count ↑p : Ideal R) := by
let P := factors I
have prime_of_mem := fun p (hp : p ∈ P.toFinset) =>
prime_of_factor p (Multiset.mem_toFinset.mp hp)
apply torsionBySet_isInternal (p := fun p => p ^ P.count p) _
· convert hM
rw [← Finset.inf_eq_iInf, IsDedekindDomain.inf_prime_pow_eq_prod, ← Finset.prod_multiset_count,
← associated_iff_eq]
· exact factors_prod hI
· exact prime_of_mem
· exact fun _ _ _ _ ij => ij
· intro p hp q hq pq; dsimp
rw [irreducible_pow_sup]
· suffices (normalizedFactors _).count p = 0 by rw [this, zero_min, pow_zero, Ideal.one_eq_top]
rw [Multiset.count_eq_zero,
normalizedFactors_of_irreducible_pow (prime_of_mem q hq).irreducible,
Multiset.mem_replicate]
exact fun H => pq <| H.2.trans <| normalize_eq q
· rw [← Ideal.zero_eq_bot]; apply pow_ne_zero; exact (prime_of_mem q hq).ne_zero
· exact (prime_of_mem p hp).irreducible
/-- A finitely generated torsion module over a Dedekind domain is an internal direct sum of its
`p i ^ e i`-torsion submodules where `p i` are factors of `(⊤ : Submodule R M).annihilator` and
`e i` are their multiplicities. -/
theorem isInternal_prime_power_torsion [DecidableEq (Ideal R)] [Module.Finite R M]
(hM : Module.IsTorsion R M) :
DirectSum.IsInternal fun p : (factors (⊤ : Submodule R M).annihilator).toFinset =>
torsionBySet R M (p ^ (factors (⊤ : Submodule R M).annihilator).count ↑p : Ideal R) := by
have hM' := Module.isTorsionBySet_annihilator_top R M
have hI := Submodule.annihilator_top_inter_nonZeroDivisors hM
refine isInternal_prime_power_torsion_of_is_torsion_by_ideal ?_ hM'
rw [← Set.nonempty_iff_ne_empty] at hI; rw [Submodule.ne_bot_iff]
obtain ⟨x, H, hx⟩ := hI; exact ⟨x, H, nonZeroDivisors.ne_zero hx⟩
/-- A finitely generated torsion module over a Dedekind domain is an internal direct sum of its
`p i ^ e i`-torsion submodules for some prime ideals `p i` and numbers `e i`. -/
theorem exists_isInternal_prime_power_torsion [Module.Finite R M] (hM : Module.IsTorsion R M) :
∃ (P : Finset <| Ideal R) (_ : DecidableEq P) (_ : ∀ p ∈ P, Prime p) (e : P → ℕ),
DirectSum.IsInternal fun p : P => torsionBySet R M (p ^ e p : Ideal R) := by
classical
exact ⟨_, _, fun p hp => prime_of_factor p (Multiset.mem_toFinset.mp hp), _,
isInternal_prime_power_torsion hM⟩
end Submodule |
.lake/packages/mathlib/Mathlib/Algebra/Module/NatInt.lean | import Mathlib.Algebra.Module.Defs
import Mathlib.Data.Int.Cast.Lemmas
/-!
# Modules over `ℕ` and `ℤ`
This file concerns modules where the scalars are the natural numbers or the integers.
## Main definitions
* `AddCommMonoid.toNatModule`: any `AddCommMonoid` is (uniquely) a module over the naturals.
* `AddCommGroup.toIntModule`: any `AddCommGroup` is a module over the integers.
## Main results
* `AddCommMonoid.uniqueNatModule`: there is an unique `AddCommMonoid ℕ M` structure for any `M`
## Tags
semimodule, module, vector space
-/
assert_not_exists RelIso Field Invertible Multiset Pi.single_smul₀ Set.indicator
open Function Set
universe u v
variable {R S M M₂ : Type*}
section AddCommMonoid
variable [AddCommMonoid M]
instance AddCommMonoid.toNatModule : Module ℕ M where
one_smul := one_nsmul
mul_smul m n a := mul_nsmul' a m n
smul_add n a b := nsmul_add a b n
smul_zero := nsmul_zero
zero_smul := zero_nsmul
add_smul r s x := add_nsmul x r s
end AddCommMonoid
section AddCommGroup
variable (M) [AddCommGroup M]
instance AddCommGroup.toIntModule : Module ℤ M where
one_smul := one_zsmul
mul_smul m n a := mul_zsmul a m n
smul_add n a b := zsmul_add a b n
smul_zero := zsmul_zero
zero_smul := zero_zsmul
add_smul r s x := add_zsmul x r s
end AddCommGroup
variable (R) in
/-- An `AddCommMonoid` that is a `Module` over a `Ring` carries a natural `AddCommGroup`
structure.
See note [reducible non-instances]. -/
abbrev Module.addCommMonoidToAddCommGroup
[Ring R] [AddCommMonoid M] [Module R M] : AddCommGroup M :=
{ (inferInstance : AddCommMonoid M) with
neg := fun a => (-1 : R) • a
neg_add_cancel := fun a =>
show (-1 : R) • a + a = 0 by
nth_rw 2 [← one_smul R a]
rw [← add_smul, neg_add_cancel, zero_smul]
zsmul := fun z a => (z : R) • a
zsmul_zero' := fun a => by simpa only [Int.cast_zero] using zero_smul R a
zsmul_succ' := fun z a => by simp [add_comm, add_smul]
zsmul_neg' := fun z a => by simp [← smul_assoc] }
section AddCommMonoid
variable [Semiring R] [AddCommMonoid M] [Module R M]
section
variable (R)
/-- `nsmul` is equal to any other module structure via a cast. -/
@[norm_cast]
lemma Nat.cast_smul_eq_nsmul (n : ℕ) (b : M) : (n : R) • b = n • b := by
induction n with
| zero => rw [Nat.cast_zero, zero_smul, zero_smul]
| succ n ih => rw [Nat.cast_succ, add_smul, add_smul, one_smul, ih, one_smul]
/-- `nsmul` is equal to any other module structure via a cast. -/
lemma ofNat_smul_eq_nsmul (n : ℕ) [n.AtLeastTwo] (b : M) :
(ofNat(n) : R) • b = ofNat(n) • b := Nat.cast_smul_eq_nsmul ..
end
/-- Convert back any exotic `ℕ`-smul to the canonical instance. This should not be needed since in
mathlib all `AddCommMonoid`s should normally have exactly one `ℕ`-module structure by design.
-/
theorem nat_smul_eq_nsmul (h : Module ℕ M) (n : ℕ) (x : M) : h.smul n x = n • x :=
Nat.cast_smul_eq_nsmul ..
/-- All `ℕ`-module structures are equal. Not an instance since in mathlib all `AddCommMonoid`
should normally have exactly one `ℕ`-module structure by design. -/
def AddCommMonoid.uniqueNatModule : Unique (Module ℕ M) where
default := inferInstance
uniq P := (Module.ext' P _) fun n => by convert nat_smul_eq_nsmul P n
/-- All `ℕ`-module structures are equal. See also `AddCommMoniod.uniqueNatModule`. -/
instance AddCommMonoid.subsingletonNatModule : Subsingleton (Module ℕ M) :=
AddCommMonoid.uniqueNatModule.instSubsingleton
instance AddCommMonoid.nat_isScalarTower : IsScalarTower ℕ R M where
smul_assoc n x y := by
induction n with
| zero => simp only [zero_smul]
| succ n ih => simp only [add_smul, one_smul, ih]
end AddCommMonoid
theorem map_natCast_smul [AddCommMonoid M] [AddCommMonoid M₂] {F : Type*} [FunLike F M M₂]
[AddMonoidHomClass F M M₂] (f : F) (R S : Type*) [Semiring R] [Semiring S] [Module R M]
[Module S M₂] (x : ℕ) (a : M) : f ((x : R) • a) = (x : S) • f a := by
simp only [Nat.cast_smul_eq_nsmul, map_nsmul]
theorem Nat.smul_one_eq_cast {R : Type*} [NonAssocSemiring R] (m : ℕ) : m • (1 : R) = ↑m := by
rw [nsmul_eq_mul, mul_one]
theorem Int.smul_one_eq_cast {R : Type*} [NonAssocRing R] (m : ℤ) : m • (1 : R) = ↑m := by
rw [zsmul_eq_mul, mul_one]
section AddCommGroup
variable [Ring R] [AddCommGroup M] [Module R M]
section
variable (R)
/-- `zsmul` is equal to any other module structure via a cast. -/
@[norm_cast]
lemma Int.cast_smul_eq_zsmul (n : ℤ) (b : M) : (n : R) • b = n • b := by
cases n with
| ofNat => simp [Nat.cast_smul_eq_nsmul]
| negSucc => simp [add_smul, Nat.cast_smul_eq_nsmul]
end
/-- Convert back any exotic `ℤ`-smul to the canonical instance. This should not be needed since in
mathlib all `AddCommGroup`s should normally have exactly one `ℤ`-module structure by design. -/
theorem int_smul_eq_zsmul (h : Module ℤ M) (n : ℤ) (x : M) : h.smul n x = n • x :=
Int.cast_smul_eq_zsmul ..
/-- All `ℤ`-module structures are equal. Not an instance since in mathlib all `AddCommGroup`
should normally have exactly one `ℤ`-module structure by design. -/
def AddCommGroup.uniqueIntModule : Unique (Module ℤ M) where
default := inferInstance
uniq P := (Module.ext' P _) fun n => by convert int_smul_eq_zsmul P n
end AddCommGroup
/-- All `ℤ`-module structures are equal. See also `AddCommGroup.uniqueIntModule`. -/
instance AddCommMonoid.subsingletonIntModule [AddCommMonoid M] : Subsingleton (Module ℤ M) where
allEq a b :=
let : AddCommGroup M := Module.addCommMonoidToAddCommGroup ℤ
AddCommGroup.uniqueIntModule.instSubsingleton.allEq a b
theorem map_intCast_smul [AddCommGroup M] [AddCommGroup M₂] {F : Type*} [FunLike F M M₂]
[AddMonoidHomClass F M M₂] (f : F) (R S : Type*) [Ring R] [Ring S] [Module R M] [Module S M₂]
(x : ℤ) (a : M) :
f ((x : R) • a) = (x : S) • f a := by simp only [Int.cast_smul_eq_zsmul, map_zsmul]
instance AddCommGroup.intIsScalarTower {R : Type u} {M : Type v} [Ring R] [AddCommGroup M]
[Module R M] : IsScalarTower ℤ R M where
smul_assoc n x y := by
cases n with
| ofNat => simp [mul_smul, Nat.cast_smul_eq_nsmul]
| negSucc => simp [mul_smul, add_smul, Nat.cast_smul_eq_nsmul] |
.lake/packages/mathlib/Mathlib/Algebra/Module/Rat.lean | import Mathlib.Algebra.Module.Basic
import Mathlib.Algebra.Module.End
import Mathlib.Algebra.Field.Rat
/-!
# Basic results about modules over the rationals.
-/
universe u v
variable {M M₂ : Type*}
theorem map_nnratCast_smul [AddCommMonoid M] [AddCommMonoid M₂] {F : Type*} [FunLike F M M₂]
[AddMonoidHomClass F M M₂] (f : F) (R S : Type*) [DivisionSemiring R] [DivisionSemiring S]
[Module R M] [Module S M₂] (c : ℚ≥0) (x : M) :
f ((c : R) • x) = (c : S) • f x := by
rw [NNRat.cast_def, NNRat.cast_def, div_eq_mul_inv, div_eq_mul_inv, mul_smul, mul_smul,
map_natCast_smul f R S, map_inv_natCast_smul f R S]
theorem map_ratCast_smul [AddCommGroup M] [AddCommGroup M₂] {F : Type*} [FunLike F M M₂]
[AddMonoidHomClass F M M₂] (f : F) (R S : Type*) [DivisionRing R] [DivisionRing S] [Module R M]
[Module S M₂] (c : ℚ) (x : M) :
f ((c : R) • x) = (c : S) • f x := by
rw [Rat.cast_def, Rat.cast_def, div_eq_mul_inv, div_eq_mul_inv, mul_smul, mul_smul,
map_intCast_smul f R S, map_inv_natCast_smul f R S]
theorem map_nnrat_smul [AddCommMonoid M] [AddCommMonoid M₂]
[_instM : Module ℚ≥0 M] [_instM₂ : Module ℚ≥0 M₂]
{F : Type*} [FunLike F M M₂] [AddMonoidHomClass F M M₂]
(f : F) (c : ℚ≥0) (x : M) : f (c • x) = c • f x :=
map_nnratCast_smul f ℚ≥0 ℚ≥0 c x
theorem map_rat_smul [AddCommGroup M] [AddCommGroup M₂]
[_instM : Module ℚ M] [_instM₂ : Module ℚ M₂]
{F : Type*} [FunLike F M M₂] [AddMonoidHomClass F M M₂]
(f : F) (c : ℚ) (x : M) : f (c • x) = c • f x :=
map_ratCast_smul f ℚ ℚ c x
/-- There can be at most one `Module ℚ≥0 E` structure on an additive commutative monoid. -/
instance subsingleton_nnrat_module (E : Type*) [AddCommMonoid E] : Subsingleton (Module ℚ≥0 E) :=
⟨fun P Q => (Module.ext' P Q) fun r x =>
map_nnrat_smul (_instM := P) (_instM₂ := Q) (AddMonoidHom.id E) r x⟩
/-- There can be at most one `Module ℚ E` structure on an additive commutative group. -/
instance subsingleton_rat_module (E : Type*) [AddCommGroup E] : Subsingleton (Module ℚ E) :=
⟨fun P Q => (Module.ext' P Q) fun r x =>
map_rat_smul (_instM := P) (_instM₂ := Q) (AddMonoidHom.id E) r x⟩
/-- If `E` is a vector space over two division semirings `R` and `S`, then scalar multiplications
agree on non-negative rational numbers in `R` and `S`. -/
theorem nnratCast_smul_eq {E : Type*} (R S : Type*) [AddCommMonoid E] [DivisionSemiring R]
[DivisionSemiring S] [Module R E] [Module S E] (r : ℚ≥0) (x : E) : (r : R) • x = (r : S) • x :=
map_nnratCast_smul (AddMonoidHom.id E) R S r x
/-- If `E` is a vector space over two division rings `R` and `S`, then scalar multiplications
agree on rational numbers in `R` and `S`. -/
theorem ratCast_smul_eq {E : Type*} (R S : Type*) [AddCommGroup E] [DivisionRing R]
[DivisionRing S] [Module R E] [Module S E] (r : ℚ) (x : E) : (r : R) • x = (r : S) • x :=
map_ratCast_smul (AddMonoidHom.id E) R S r x
instance IsScalarTower.nnrat {R : Type u} {M : Type v} [Semiring R] [AddCommMonoid M] [Module R M]
[Module ℚ≥0 R] [Module ℚ≥0 M] : IsScalarTower ℚ≥0 R M where
smul_assoc r x y := map_nnrat_smul ((smulAddHom R M).flip y) r x
instance IsScalarTower.rat {R : Type u} {M : Type v} [Ring R] [AddCommGroup M] [Module R M]
[Module ℚ R] [Module ℚ M] : IsScalarTower ℚ R M where
smul_assoc r x y := map_rat_smul ((smulAddHom R M).flip y) r x
/-- `nnqsmul` is equal to any other module structure via a cast. -/
lemma NNRat.cast_smul_eq_nnqsmul (R : Type*) [DivisionSemiring R]
[MulAction R M] [MulAction ℚ≥0 M] [IsScalarTower ℚ≥0 R M]
(q : ℚ≥0) (x : M) : (q : R) • x = q • x := by
rw [← one_smul R x, ← smul_assoc, ← smul_assoc]; simp
/-- `qsmul` is equal to any other module structure via a cast. -/
lemma Rat.cast_smul_eq_qsmul (R : Type*) [DivisionRing R]
[MulAction R M] [MulAction ℚ M] [IsScalarTower ℚ R M]
(q : ℚ) (x : M) : (q : R) • x = q • x := by
rw [← one_smul R x, ← smul_assoc, ← smul_assoc]; simp
section
variable {α : Type u} {M : Type v}
instance SMulCommClass.nnrat [Monoid α] [AddCommMonoid M] [DistribMulAction α M] [Module ℚ≥0 M] :
SMulCommClass ℚ≥0 α M where
smul_comm r x y := (map_nnrat_smul (DistribMulAction.toAddMonoidHom M x) r y).symm
instance SMulCommClass.rat [Monoid α] [AddCommGroup M] [DistribMulAction α M] [Module ℚ M] :
SMulCommClass ℚ α M where
smul_comm r x y := (map_rat_smul (DistribMulAction.toAddMonoidHom M x) r y).symm
instance SMulCommClass.nnrat' [Monoid α] [AddCommMonoid M] [DistribMulAction α M] [Module ℚ≥0 M] :
SMulCommClass α ℚ≥0 M :=
SMulCommClass.symm _ _ _
instance SMulCommClass.rat' [Monoid α] [AddCommGroup M] [DistribMulAction α M] [Module ℚ M] :
SMulCommClass α ℚ M :=
SMulCommClass.symm _ _ _
end
variable (M) in
/-- A `ℚ≥0`-module is torsion-free as a group.
This instance will fire for any monoid `M`, so is local unless needed elsewhere. -/
lemma IsAddTorsionFree.of_module_nnrat [AddCommMonoid M] [Module ℚ≥0 M] : IsAddTorsionFree M where
nsmul_right_injective n hn x y hxy := by
simpa [← Nat.cast_smul_eq_nsmul ℚ≥0 n, *] using congr((n⁻¹ : ℚ≥0) • $hxy)
variable (M) in
/-- A `ℚ≥0`-module is torsion-free as a group.
This instance will fire for any monoid `M`, so is local unless needed elsewhere. -/
lemma IsAddTorsionFree.of_module_rat [AddCommGroup M] [Module ℚ M] : IsAddTorsionFree M where
nsmul_right_injective n hn x y hxy := by
simpa [← Nat.cast_smul_eq_nsmul ℚ n, *] using congr((n⁻¹ : ℚ) • $hxy) |
.lake/packages/mathlib/Mathlib/Algebra/Module/TransferInstance.lean | import Mathlib.Algebra.GroupWithZero.Action.TransferInstance
import Mathlib.Algebra.Module.Equiv.Defs
/-!
# Transfer algebraic structures across `Equiv`s
This continues the pattern set in `Mathlib/Algebra/Group/TransferInstance.lean`.
-/
assert_not_exists Algebra
universe u v
variable {R α β : Type*} [Semiring R]
namespace Equiv
variable (e : α ≃ β)
variable (R) in
/-- Transfer `Module` across an `Equiv` -/
protected abbrev module (e : α ≃ β) [AddCommMonoid β] :
let _ := Equiv.addCommMonoid e
∀ [Module R β], Module R α := by
intros
exact
({ Equiv.distribMulAction R e with
zero_smul := by simp [smul_def, zero_smul, zero_def]
add_smul := by simp [add_def, smul_def, add_smul] } :
Module R α)
variable (R) in
/-- An equivalence `e : α ≃ β` gives a linear equivalence `α ≃ₗ[R] β`
where the `R`-module structure on `α` is
the one obtained by transporting an `R`-module structure on `β` back along `e`.
-/
def linearEquiv (e : α ≃ β) [AddCommMonoid β] [Module R β] : by
let addCommMonoid := Equiv.addCommMonoid e
let module := Equiv.module R e
exact α ≃ₗ[R] β := by
intros
exact
{ Equiv.addEquiv e with
map_smul' := fun r x => by
apply e.symm.injective
simp only [toFun_as_coe, RingHom.id_apply, EmbeddingLike.apply_eq_iff_eq]
exact Iff.mpr (apply_eq_iff_eq_symm_apply _) rfl }
end Equiv
variable (A) [Semiring A] [Module R A] [AddCommMonoid α] [AddCommMonoid β] [Module A β]
/-- Transport a module instance via an isomorphism of the underlying abelian groups.
This has better definitional properties than `Equiv.module` since here
the abelian group structure remains unmodified. -/
abbrev AddEquiv.module (e : α ≃+ β) : Module A α where
toSMul := e.toEquiv.smul A
one_smul := by simp [Equiv.smul_def]
mul_smul := by simp [Equiv.smul_def, mul_smul]
smul_zero := by simp [Equiv.smul_def]
smul_add := by simp [Equiv.smul_def]
add_smul := by simp [Equiv.smul_def, add_smul]
zero_smul := by simp [Equiv.smul_def]
/-- The module instance from `AddEquiv.module` is compatible with the `R`-module structures,
if the `AddEquiv` is induced by an `R`-module isomorphism. -/
lemma LinearEquiv.isScalarTower [Module R α] [Module R β] [IsScalarTower R A β]
(e : α ≃ₗ[R] β) :
letI := e.toAddEquiv.module A
IsScalarTower R A α := by
letI := e.toAddEquiv.module A
constructor
intro x y z
simp only [Equiv.smul_def, smul_assoc]
apply e.symm.map_smul |
.lake/packages/mathlib/Mathlib/Algebra/Module/PUnit.lean | import Mathlib.Algebra.Module.Defs
import Mathlib.Algebra.Ring.Action.Basic
import Mathlib.Algebra.Ring.PUnit
/-!
# Instances on PUnit
This file collects facts about module structures on the one-element type
-/
namespace PUnit
variable {R S : Type*}
@[to_additive]
instance smul : SMul R PUnit :=
⟨fun _ _ => unit⟩
@[to_additive (attr := simp)]
theorem smul_eq {R : Type*} (y : PUnit) (r : R) : r • y = unit :=
rfl
@[to_additive]
instance : IsCentralScalar R PUnit :=
⟨fun _ _ => rfl⟩
@[to_additive]
instance : SMulCommClass R S PUnit :=
⟨fun _ _ _ => rfl⟩
@[to_additive]
instance instIsScalarTowerOfSMul [SMul R S] : IsScalarTower R S PUnit :=
⟨fun _ _ _ => rfl⟩
instance smulWithZero [Zero R] : SMulWithZero R PUnit where
__ := PUnit.smul
smul_zero := by subsingleton
zero_smul := by subsingleton
instance mulAction [Monoid R] : MulAction R PUnit where
__ := PUnit.smul
one_smul := by subsingleton
mul_smul := by subsingleton
instance distribMulAction [Monoid R] : DistribMulAction R PUnit where
__ := PUnit.mulAction
smul_zero := by subsingleton
smul_add := by subsingleton
instance mulDistribMulAction [Monoid R] : MulDistribMulAction R PUnit where
__ := PUnit.mulAction
smul_mul := by subsingleton
smul_one := by subsingleton
instance mulSemiringAction [Semiring R] : MulSemiringAction R PUnit :=
{ PUnit.distribMulAction, PUnit.mulDistribMulAction with }
instance mulActionWithZero [MonoidWithZero R] : MulActionWithZero R PUnit :=
{ PUnit.mulAction, PUnit.smulWithZero with }
instance module [Semiring R] : Module R PUnit where
__ := PUnit.distribMulAction
add_smul := by subsingleton
zero_smul := by subsingleton
@[to_additive]
instance : SMul PUnit R where smul _ x := x
/-- The one-element type acts trivially on every element. -/
@[to_additive (attr := simp)]
lemma smul_eq' (r : PUnit) (a : R) : r • a = a := rfl
@[to_additive] instance [SMul R S] : SMulCommClass PUnit R S := ⟨by simp⟩
instance [SMul R S] : IsScalarTower PUnit R S := ⟨by simp⟩
instance : MulAction PUnit R where
__ := inferInstanceAs (SMul PUnit R)
one_smul _ := rfl
mul_smul _ _ _ := rfl
instance [Zero R] : SMulZeroClass PUnit R where
__ := inferInstanceAs (SMul PUnit R)
smul_zero _ := rfl
instance [AddMonoid R] : DistribMulAction PUnit R where
__ := inferInstanceAs (MulAction PUnit R)
__ := inferInstanceAs (SMulZeroClass PUnit R)
smul_add _ _ _ := rfl
end PUnit |
.lake/packages/mathlib/Mathlib/Algebra/Module/Bimodule.lean | import Mathlib.RingTheory.TensorProduct.Basic
/-!
# Bimodules
One frequently encounters situations in which several sets of scalars act on a single space, subject
to compatibility condition(s). A distinguished instance of this is the theory of bimodules: one has
two rings `R`, `S` acting on an additive group `M`, with `R` acting covariantly ("on the left")
and `S` acting contravariantly ("on the right"). The compatibility condition is just:
`(r • m) • s = r • (m • s)` for all `r : R`, `s : S`, `m : M`.
This situation can be set up in Mathlib as:
```lean
variable (R S M : Type*) [Ring R] [Ring S]
variable [AddCommGroup M] [Module R M] [Module Sᵐᵒᵖ M] [SMulCommClass R Sᵐᵒᵖ M]
```
The key fact is:
```lean
example : Module (R ⊗[ℕ] Sᵐᵒᵖ) M := TensorProduct.Algebra.module
```
Note that the corresponding result holds for the canonically isomorphic ring `R ⊗[ℤ] Sᵐᵒᵖ` but it is
preferable to use the `R ⊗[ℕ] Sᵐᵒᵖ` instance since it works without additive inverses.
Bimodules are thus just a special case of `Module`s and most of their properties follow from the
theory of `Module`s. In particular a two-sided Submodule of a bimodule is simply a term of type
`Submodule (R ⊗[ℕ] Sᵐᵒᵖ) M`.
This file is a place to collect results which are specific to bimodules.
## Main definitions
* `Subbimodule.mk`
* `Subbimodule.smul_mem`
* `Subbimodule.smul_mem'`
* `Subbimodule.toSubmodule`
* `Subbimodule.toSubmodule'`
## Implementation details
For many definitions and lemmas it is preferable to set things up without opposites, i.e., as:
`[Module S M] [SMulCommClass R S M]` rather than `[Module Sᵐᵒᵖ M] [SMulCommClass R Sᵐᵒᵖ M]`.
The corresponding results for opposites then follow automatically and do not require taking
advantage of the fact that `(Sᵐᵒᵖ)ᵐᵒᵖ` is defeq to `S`.
## TODO
Develop the theory of two-sided ideals, which have type `Submodule (R ⊗[ℕ] Rᵐᵒᵖ) R`.
-/
open TensorProduct
attribute [local instance] TensorProduct.Algebra.module
namespace Subbimodule
section Algebra
variable {R A B M : Type*}
variable [CommSemiring R] [AddCommMonoid M] [Module R M]
variable [Semiring A] [Semiring B] [Module A M] [Module B M]
variable [Algebra R A] [Algebra R B]
variable [IsScalarTower R A M] [IsScalarTower R B M]
variable [SMulCommClass A B M]
/-- A constructor for a subbimodule which demands closure under the two sets of scalars
individually, rather than jointly via their tensor product.
Note that `R` plays no role but it is convenient to make this generalisation to support the cases
`R = ℕ` and `R = ℤ` which both show up naturally. See also `Subbimodule.baseChange`. -/
@[simps]
def mk (p : AddSubmonoid M) (hA : ∀ (a : A) {m : M}, m ∈ p → a • m ∈ p)
(hB : ∀ (b : B) {m : M}, m ∈ p → b • m ∈ p) : Submodule (A ⊗[R] B) M :=
{ p with
carrier := p
smul_mem' := fun ab m =>
TensorProduct.induction_on ab (fun _ => by simp only [zero_smul, SetLike.mem_coe, zero_mem])
(fun a b hm => by simpa only [TensorProduct.Algebra.smul_def] using hA a (hB b hm))
fun z w hz hw hm => by simpa only [add_smul] using p.add_mem (hz hm) (hw hm) }
theorem smul_mem (p : Submodule (A ⊗[R] B) M) (a : A) {m : M} (hm : m ∈ p) : a • m ∈ p := by
suffices a • m = a ⊗ₜ[R] (1 : B) • m by exact this.symm ▸ p.smul_mem _ hm
simp [TensorProduct.Algebra.smul_def]
theorem smul_mem' (p : Submodule (A ⊗[R] B) M) (b : B) {m : M} (hm : m ∈ p) : b • m ∈ p := by
suffices b • m = (1 : A) ⊗ₜ[R] b • m by exact this.symm ▸ p.smul_mem _ hm
simp [TensorProduct.Algebra.smul_def]
/-- If `A` and `B` are also `Algebra`s over yet another set of scalars `S` then we may "base change"
from `R` to `S`. -/
@[simps!]
def baseChange (S : Type*) [CommSemiring S] [Module S M] [Algebra S A] [Algebra S B]
[IsScalarTower S A M] [IsScalarTower S B M] (p : Submodule (A ⊗[R] B) M) :
Submodule (A ⊗[S] B) M :=
mk p.toAddSubmonoid (smul_mem p) (smul_mem' p)
/-- Forgetting the `B` action, a `Submodule` over `A ⊗[R] B` is just a `Submodule` over `A`. -/
@[simps]
def toSubmodule (p : Submodule (A ⊗[R] B) M) : Submodule A M :=
{ p with
carrier := p
smul_mem' := smul_mem p }
/-- Forgetting the `A` action, a `Submodule` over `A ⊗[R] B` is just a `Submodule` over `B`. -/
@[simps]
def toSubmodule' (p : Submodule (A ⊗[R] B) M) : Submodule B M :=
{ p with
carrier := p
smul_mem' := smul_mem' p }
end Algebra
section Ring
variable (R S M : Type*) [Ring R] [Ring S]
variable [AddCommGroup M] [Module R M] [Module S M] [SMulCommClass R S M]
/-- A `Submodule` over `R ⊗[ℕ] S` is naturally also a `Submodule` over the canonically-isomorphic
ring `R ⊗[ℤ] S`. -/
@[simps!]
def toSubbimoduleInt (p : Submodule (R ⊗[ℕ] S) M) : Submodule (R ⊗[ℤ] S) M :=
baseChange ℤ p
/-- A `Submodule` over `R ⊗[ℤ] S` is naturally also a `Submodule` over the canonically-isomorphic
ring `R ⊗[ℕ] S`. -/
@[simps!]
def toSubbimoduleNat (p : Submodule (R ⊗[ℤ] S) M) : Submodule (R ⊗[ℕ] S) M :=
baseChange ℕ p
end Ring
end Subbimodule |
.lake/packages/mathlib/Mathlib/Algebra/Module/Prod.lean | import Mathlib.Algebra.GroupWithZero.Action.Prod
import Mathlib.Algebra.Module.Defs
/-!
# Prod instances for module and multiplicative actions
This file defines instances for binary product of modules
-/
variable {R : Type*} {M : Type*} {N : Type*}
namespace Prod
instance instModule [Semiring R] [AddCommMonoid M] [AddCommMonoid N] [Module R M] [Module R N] :
Module R (M × N) where
add_smul _ _ _ := by ext <;> exact add_smul ..
zero_smul _ := by ext <;> exact zero_smul ..
end Prod |
.lake/packages/mathlib/Mathlib/Algebra/Module/PID.lean | import Mathlib.Algebra.Module.DedekindDomain
import Mathlib.LinearAlgebra.FreeModule.PID
import Mathlib.Algebra.Module.Projective
import Mathlib.Algebra.Category.ModuleCat.Biproducts
import Mathlib.RingTheory.SimpleModule.Basic
/-!
# Structure of finitely generated modules over a PID
## Main statements
* `Module.equiv_directSum_of_isTorsion` : A finitely generated torsion module over a PID is
isomorphic to a direct sum of some `R ⧸ R ∙ (p i ^ e i)` where the `p i ^ e i` are prime powers.
* `Module.equiv_free_prod_directSum` : A finitely generated module over a PID is isomorphic to the
product of a free module (its torsion free part) and a direct sum of the form above (its torsion
submodule).
## Notation
* `R` is a PID and `M` is a (finitely generated for main statements) `R`-module, with additional
torsion hypotheses in the intermediate lemmas.
* `p` is an irreducible element of `R` or a tuple of these.
## Implementation details
We first prove (`Submodule.isInternal_prime_power_torsion_of_pid`) that a finitely generated
torsion module is the internal direct sum of its `p i ^ e i`-torsion submodules for some
(finitely many) prime powers `p i ^ e i`. This is proved in more generality for a Dedekind domain
at `Submodule.isInternal_prime_power_torsion`.
Then we treat the case of a `p ^ ∞`-torsion module (that is, a module where all elements are
cancelled by scalar multiplication by some power of `p`) and apply it to the `p i ^ e i`-torsion
submodules (that are `p i ^ ∞`-torsion) to get the result for torsion modules.
Then we get the general result using that a torsion free module is free (which has been proved at
`Module.free_of_finite_type_torsion_free'` at `LinearAlgebra.FreeModule.PID`.)
## Tags
Finitely generated module, principal ideal domain, classification, structure theorem
-/
-- We shouldn't need to know about topology to prove
-- the structure theorem for finitely generated modules over a PID.
assert_not_exists TopologicalSpace
universe u v
variable {R : Type u} [CommRing R] [IsPrincipalIdealRing R]
variable {M : Type v} [AddCommGroup M] [Module R M]
open scoped DirectSum
open Submodule
open UniqueFactorizationMonoid
theorem Submodule.isSemisimple_torsionBy_of_irreducible {a : R} (h : Irreducible a) :
IsSemisimpleModule R (torsionBy R M a) :=
haveI := PrincipalIdealRing.isMaximal_of_irreducible h
letI := Ideal.Quotient.field (R ∙ a)
(isSemisimpleModule_iff ..).mpr (submodule_torsionBy_orderIso a).complementedLattice
variable [IsDomain R]
/-- A finitely generated torsion module over a PID is an internal direct sum of its
`p i ^ e i`-torsion submodules for some primes `p i` and numbers `e i`. -/
theorem Submodule.isInternal_prime_power_torsion_of_pid [DecidableEq (Ideal R)] [Module.Finite R M]
(hM : Module.IsTorsion R M) :
DirectSum.IsInternal fun p : (factors (⊤ : Submodule R M).annihilator).toFinset =>
torsionBy R M
(IsPrincipal.generator (p : Ideal R) ^
(factors (⊤ : Submodule R M).annihilator).count ↑p) := by
convert isInternal_prime_power_torsion hM
rw [← torsionBySet_span_singleton_eq, Ideal.submodule_span_eq, ← Ideal.span_singleton_pow,
Ideal.span_singleton_generator]
/-- A finitely generated torsion module over a PID is an internal direct sum of its
`p i ^ e i`-torsion submodules for some primes `p i` and numbers `e i`. -/
theorem Submodule.exists_isInternal_prime_power_torsion_of_pid [Module.Finite R M]
(hM : Module.IsTorsion R M) :
∃ (ι : Type u) (_ : Fintype ι) (_ : DecidableEq ι) (p : ι → R) (_ : ∀ i, Irreducible <| p i)
(e : ι → ℕ), DirectSum.IsInternal fun i => torsionBy R M <| p i ^ e i := by
classical
refine ⟨_, ?_, _, _, ?_, _, Submodule.isInternal_prime_power_torsion_of_pid hM⟩
· exact Finset.fintypeCoeSort _
· rintro ⟨p, hp⟩
have hP := prime_of_factor p (Multiset.mem_toFinset.mp hp)
haveI := Ideal.isPrime_of_prime hP
exact (IsPrincipal.prime_generator_of_isPrime p hP.ne_zero).irreducible
namespace Module
section PTorsion
variable {p : R} (hp : Irreducible p) (hM : Module.IsTorsion' M (Submonoid.powers p))
variable [dec : ∀ x : M, Decidable (x = 0)]
open Ideal Submodule.IsPrincipal
include hp
theorem _root_.Ideal.torsionOf_eq_span_pow_pOrder (x : M) :
torsionOf R M x = span {p ^ pOrder hM x} := by
classical
dsimp only [pOrder]
rw [← (torsionOf R M x).span_singleton_generator, Ideal.span_singleton_eq_span_singleton, ←
Associates.mk_eq_mk_iff_associated, Associates.mk_pow]
have prop :
(fun n : ℕ => p ^ n • x = 0) = fun n : ℕ =>
(Associates.mk <| generator <| torsionOf R M x) ∣ Associates.mk p ^ n := by
ext n; rw [← Associates.mk_pow, Associates.mk_dvd_mk, ← mem_iff_generator_dvd]; rfl
have := (isTorsion'_powers_iff p).mp hM x; rw [prop] at this
convert Associates.eq_pow_find_of_dvd_irreducible_pow (Associates.irreducible_mk.mpr hp)
this.choose_spec
theorem p_pow_smul_lift {x y : M} {k : ℕ} (hM' : Module.IsTorsionBy R M (p ^ pOrder hM y))
(h : p ^ k • x ∈ R ∙ y) : ∃ a : R, p ^ k • x = p ^ k • a • y := by
by_cases! hk : k ≤ pOrder hM y
· let f :=
((R ∙ p ^ (pOrder hM y - k) * p ^ k).quotEquivOfEq _ ?_).trans
(quotTorsionOfEquivSpanSingleton R M y)
· have : f.symm ⟨p ^ k • x, h⟩ ∈
R ∙ Ideal.Quotient.mk (R ∙ p ^ (pOrder hM y - k) * p ^ k) (p ^ k) := by
rw [← Quotient.torsionBy_eq_span_singleton, mem_torsionBy_iff, ← f.symm.map_smul]
· convert f.symm.map_zero; ext
rw [coe_smul_of_tower, coe_mk, coe_zero, smul_smul, ← pow_add, Nat.sub_add_cancel hk,
@hM' x]
· exact mem_nonZeroDivisors_of_ne_zero (pow_ne_zero _ hp.ne_zero)
rw [Submodule.mem_span_singleton] at this; obtain ⟨a, ha⟩ := this; use a
rw [f.eq_symm_apply, ← Ideal.Quotient.mk_eq_mk, ← Quotient.mk_smul] at ha
dsimp only [smul_eq_mul, LinearEquiv.trans_apply, Submodule.quotEquivOfEq_mk,
quotTorsionOfEquivSpanSingleton_apply_mk] at ha
rw [smul_smul, mul_comm]; exact congr_arg ((↑) : _ → M) ha.symm
· symm; convert Ideal.torsionOf_eq_span_pow_pOrder hp hM y
rw [← pow_add, Nat.sub_add_cancel hk]
· use 0
rw [zero_smul, smul_zero, ← Nat.sub_add_cancel hk.le, pow_add, mul_smul, hM',
smul_zero]
open Submodule.Quotient
theorem exists_smul_eq_zero_and_mk_eq {z : M} (hz : Module.IsTorsionBy R M (p ^ pOrder hM z))
{k : ℕ} (f : (R ⧸ R ∙ p ^ k) →ₗ[R] M ⧸ R ∙ z) :
∃ x : M, p ^ k • x = 0 ∧ Submodule.Quotient.mk (p := span R {z}) x = f 1 := by
have f1 := mk_surjective (R ∙ z) (f 1)
have : p ^ k • f1.choose ∈ R ∙ z := by
rw [← Quotient.mk_eq_zero, mk_smul, f1.choose_spec, ← f.map_smul]
convert f.map_zero; change _ • Submodule.Quotient.mk _ = _
rw [← mk_smul, Quotient.mk_eq_zero, Algebra.id.smul_eq_mul, mul_one]
exact Submodule.mem_span_singleton_self _
obtain ⟨a, ha⟩ := p_pow_smul_lift hp hM hz this
refine ⟨f1.choose - a • z, by rw [smul_sub, sub_eq_zero, ha], ?_⟩
rw [mk_sub, mk_smul, (Quotient.mk_eq_zero _).mpr <| Submodule.mem_span_singleton_self _,
smul_zero, sub_zero, f1.choose_spec]
open Finset Multiset
omit dec in
/-- A finitely generated `p ^ ∞`-torsion module over a PID is isomorphic to a direct sum of some
`R ⧸ R ∙ (p ^ e i)` for some `e i`. -/
theorem torsion_by_prime_power_decomposition (hM : Module.IsTorsion' M (Submonoid.powers p))
[h' : Module.Finite R M] :
∃ (d : ℕ) (k : Fin d → ℕ), Nonempty <| M ≃ₗ[R] ⨁ i : Fin d, R ⧸ R ∙ p ^ (k i : ℕ) := by
obtain ⟨d, s, hs⟩ := @Module.Finite.exists_fin _ _ _ _ _ h'; use d; clear h'
induction d generalizing M with
| zero =>
use finZeroElim
rw [Set.range_eq_empty, Submodule.span_empty] at hs
haveI : Unique M :=
⟨⟨0⟩, fun x => by dsimp; rw [← Submodule.mem_bot R, hs]; exact Submodule.mem_top⟩
exact ⟨0⟩
| succ d IH =>
have : ∀ x : M, Decidable (x = 0) := fun _ => by classical infer_instance
obtain ⟨j, hj⟩ := exists_isTorsionBy hM d.succ d.succ_ne_zero s hs
let s' : Fin d → M ⧸ R ∙ s j := Submodule.Quotient.mk ∘ s ∘ j.succAbove
-- Porting note(https://github.com/leanprover-community/mathlib4/issues/5732):
-- `obtain` doesn't work with placeholders.
have := IH ?_ s' ?_
· obtain ⟨k, ⟨f⟩⟩ := this
clear IH
have : ∀ i : Fin d,
∃ x : M, p ^ k i • x = 0 ∧ f (Submodule.Quotient.mk x) = DirectSum.lof R _ _ i 1 := by
intro i
let fi := f.symm.toLinearMap.comp (DirectSum.lof _ _ _ i)
obtain ⟨x, h0, h1⟩ := exists_smul_eq_zero_and_mk_eq hp hM hj fi; refine ⟨x, h0, ?_⟩; rw [h1]
simp only [fi, LinearMap.coe_comp, f.symm.coe_toLinearMap, f.apply_symm_apply,
Function.comp_apply]
refine ⟨?_, ⟨?_⟩⟩
· exact fun a => (fun i => (Option.rec (pOrder hM (s j)) k i : ℕ)) (finSuccEquiv d a)
· refine
(lequivProdOfRightSplitExact
(g := f.toLinearMap.comp <| mkQ _)
(f := (DirectSum.toModule _ _ _ fun i => (liftQSpanSingleton (p ^ k i)
(LinearMap.toSpanSingleton _ _ _) (this i).choose_spec.left : R ⧸ _ →ₗ[R] _)))
(R ∙ s j).injective_subtype ?_ ?_).symm ≪≫ₗ
(((quotTorsionOfEquivSpanSingleton R M (s j)).symm ≪≫ₗ
(quotEquivOfEq (torsionOf R M (s j)) _
(Ideal.torsionOf_eq_span_pow_pOrder hp hM (s j)))).prodCongr (.refl _ _)) ≪≫ₗ
(@DirectSum.lequivProdDirectSum R _ _
(fun i => R ⧸ R ∙ p ^ @Option.rec _ (fun _ => ℕ) (pOrder hM <| s j) k i) _ _).symm ≪≫ₗ
(DirectSum.lequivCongrLeft R (finSuccEquiv d).symm)
· rw [range_subtype, LinearEquiv.ker_comp, ker_mkQ]
· rw [LinearMap.comp_assoc]
ext i : 3
simp only [LinearMap.coe_comp, Function.comp_apply, mkQ_apply]
rw [LinearEquiv.coe_toLinearMap, LinearMap.id_apply, DirectSum.toModule_lof,
liftQSpanSingleton_apply, LinearMap.toSpanSingleton_one, Ideal.Quotient.mk_eq_mk,
map_one (Ideal.Quotient.mk _), (this i).choose_spec.right]
· exact (mk_surjective _).forall.mpr fun x =>
⟨(@hM x).choose, by rw [← Quotient.mk_smul, (@hM x).choose_spec, Quotient.mk_zero]⟩
· have hs' := congr_arg (Submodule.map <| mkQ <| R ∙ s j) hs
rw [Submodule.map_span, Submodule.map_top, range_mkQ] at hs'; simp only [mkQ_apply] at hs'
simp only [s']; rw [← Function.comp_assoc, Set.range_comp (_ ∘ s), Fin.range_succAbove]
rw [← Set.range_comp, ← Set.insert_image_compl_eq_range _ j, Function.comp_apply,
(Quotient.mk_eq_zero _).mpr (Submodule.mem_span_singleton_self _),
Submodule.span_insert_zero] at hs'
exact hs'
end PTorsion
/-- A finitely generated torsion module over a PID is isomorphic to a direct sum of some
`R ⧸ R ∙ (p i ^ e i)` where the `p i ^ e i` are prime powers. -/
theorem equiv_directSum_of_isTorsion [h' : Module.Finite R M] (hM : Module.IsTorsion R M) :
∃ (ι : Type u) (_ : Fintype ι) (p : ι → R) (_ : ∀ i, Irreducible <| p i) (e : ι → ℕ),
Nonempty <| M ≃ₗ[R] ⨁ i : ι, R ⧸ R ∙ p i ^ e i := by
obtain ⟨I, fI, _, p, hp, e, h⟩ := Submodule.exists_isInternal_prime_power_torsion_of_pid hM
have :
∀ i,
∃ (d : ℕ) (k : Fin d → ℕ),
Nonempty <| torsionBy R M (p i ^ e i) ≃ₗ[R] ⨁ j, R ⧸ R ∙ p i ^ k j := by
exact fun i =>
torsion_by_prime_power_decomposition.{u, v} (hp i)
((isTorsion'_powers_iff <| p i).mpr fun x => ⟨e i, smul_torsionBy _ _⟩)
classical
refine
⟨Σ i, Fin (this i).choose, inferInstance, fun ⟨i, _⟩ => p i, fun ⟨i, _⟩ => hp i, fun ⟨i, j⟩ =>
(this i).choose_spec.choose j,
⟨(LinearEquiv.ofBijective (DirectSum.coeLinearMap _) h).symm.trans <|
(DFinsupp.mapRange.linearEquiv fun i => (this i).choose_spec.choose_spec.some).trans <|
(DirectSum.sigmaLcurryEquiv R).symm.trans
(DFinsupp.mapRange.linearEquiv fun i => quotEquivOfEq _ _ ?_)⟩⟩
simp only
variable (R M)
/-- **Structure theorem of finitely generated modules over a PID** : A finitely generated
module over a PID is isomorphic to the product of a free module and a direct sum of some
`R ⧸ R ∙ (p i ^ e i)` where the `p i ^ e i` are prime powers. -/
theorem equiv_free_prod_directSum [h' : Module.Finite R M] :
∃ (n : ℕ) (ι : Type u) (_ : Fintype ι) (p : ι → R) (_ : ∀ i, Irreducible <| p i) (e : ι → ℕ),
Nonempty <| M ≃ₗ[R] (Fin n →₀ R) × ⨁ i : ι, R ⧸ R ∙ p i ^ e i := by
obtain ⟨I, fI, p, hp, e, ⟨h⟩⟩ :=
equiv_directSum_of_isTorsion.{u, v} (@torsion_isTorsion R M _ _ _)
obtain ⟨n, ⟨g⟩⟩ := @Module.basisOfFiniteTypeTorsionFree' R _ (M ⧸ torsion R M) _ _ _ _ _ _
obtain ⟨f, hf⟩ := Module.projective_lifting_property _ LinearMap.id (torsion R M).mkQ_surjective
refine
⟨n, I, fI, p, hp, e,
⟨(lequivProdOfRightSplitExact (torsion R M).injective_subtype ?_ hf).symm.trans <|
(h.prodCongr g).trans <| LinearEquiv.prodComm.{u, u} R _ (Fin n →₀ R) ⟩⟩
rw [range_subtype, ker_mkQ]
open LinearMap in
theorem exists_ker_toSpanSingleton_eq_annihilator [Module.Finite R M] :
∃ x : M, ker (toSpanSingleton R _ x) = annihilator R M := by
have ⟨m, ι, _, p, irr, n, ⟨e⟩⟩ := equiv_free_prod_directSum (R := R) (M := M)
refine ⟨e.symm (Finsupp.equivFunOnFinite.symm fun _ ↦ 1, DFinsupp.equivFunOnFintype.symm
fun _ ↦ mkQ _ 1), le_antisymm (fun x h ↦ ?_) fun x h ↦ mem_annihilator.mp h _⟩
rw [mem_ker, toSpanSingleton_apply, ← map_smul,
e.symm.map_eq_zero_iff, Prod.ext_iff, Finsupp.ext_iff, DFinsupp.ext_iff] at h
obtain _ | m := m
· rw [← mul_one x, ← smul_eq_mul, e.annihilator_eq, annihilator_prod]
simp_rw [annihilator_eq_top_iff.mpr inferInstance, DirectSum, annihilator_dfinsupp,
top_inf_eq, mem_iInf, Ideal.annihilator_quotient, ← Quotient.mk_eq_zero]
exact h.2
· rw [show x = 0 by simpa using h.1 0]
exact zero_mem _
end Module |
.lake/packages/mathlib/Mathlib/Algebra/Module/Pi.lean | import Mathlib.Algebra.GroupWithZero.Action.Pi
import Mathlib.Algebra.Module.Defs
import Mathlib.Algebra.Regular.SMul
import Mathlib.Algebra.Ring.Pi
/-!
# Pi instances for modules
This file defines instances for module and related structures on Pi Types
-/
universe u v w
variable {I : Type u}
-- The indexing type
variable {f : I → Type v}
namespace Pi
theorem _root_.IsSMulRegular.pi {α : Type*} [∀ i, SMul α <| f i] {k : α}
(hk : ∀ i, IsSMulRegular (f i) k) : IsSMulRegular (∀ i, f i) k := fun _ _ h =>
funext fun i => hk i (congr_fun h i :)
variable (I f)
instance module (α) {r : Semiring α} {m : ∀ i, AddCommMonoid <| f i} [∀ i, Module α <| f i] :
@Module α (∀ i : I, f i) r (@Pi.addCommMonoid I f m) :=
{ Pi.distribMulAction _ with
add_smul := fun _ _ _ => funext fun _ => add_smul _ _ _
zero_smul := fun _ => funext fun _ => zero_smul α _ }
/- Extra instance to short-circuit type class resolution.
For unknown reasons, this is necessary for certain inference problems. E.g., for this to succeed:
```lean
example (β X : Type*) [NormedAddCommGroup β] [NormedSpace ℝ β] : Module ℝ (X → β) := inferInstance
```
See: https://leanprover.zulipchat.com/#narrow/stream/113488-general/topic/Typeclass.20resolution.20under.20binders/near/281296989
-/
/-- A special case of `Pi.module` for non-dependent types. Lean struggles to elaborate
definitions elsewhere in the library without this. -/
instance Function.module (α β : Type*) [Semiring α] [AddCommMonoid β] [Module α β] :
Module α (I → β) :=
Pi.module _ _ _
variable {I f}
instance module' {g : I → Type*} {r : ∀ i, Semiring (f i)} {m : ∀ i, AddCommMonoid (g i)}
[∀ i, Module (f i) (g i)] : Module (∀ i, f i) (∀ i, g i) where
add_smul := by
intros
ext1
apply add_smul
zero_smul := by
intros
ext1
rw [zero_smul]
end Pi |
.lake/packages/mathlib/Mathlib/Algebra/Module/Basic.lean | import Mathlib.Algebra.Field.Defs
import Mathlib.Algebra.Group.Action.Pi
import Mathlib.Algebra.Notation.Indicator
import Mathlib.Algebra.GroupWithZero.Action.Units
import Mathlib.Algebra.Module.NatInt
import Mathlib.Algebra.NoZeroSMulDivisors.Defs
import Mathlib.Algebra.Ring.Invertible
/-!
# Further basic results about modules.
-/
assert_not_exists Nonneg.inv Multiset
open Function Set
universe u v
variable {α R M M₂ : Type*}
@[simp]
theorem Units.neg_smul [Ring R] [AddCommGroup M] [Module R M] (u : Rˣ) (x : M) :
-u • x = -(u • x) := by
rw [Units.smul_def, Units.val_neg, _root_.neg_smul, Units.smul_def]
@[simp]
theorem invOf_two_smul_add_invOf_two_smul (R) [Semiring R] [AddCommMonoid M] [Module R M]
[Invertible (2 : R)] (x : M) :
(⅟2 : R) • x + (⅟2 : R) • x = x :=
Convex.combo_self invOf_two_add_invOf_two _
theorem map_inv_natCast_smul [AddCommMonoid M] [AddCommMonoid M₂] {F : Type*} [FunLike F M M₂]
[AddMonoidHomClass F M M₂] (f : F) (R S : Type*)
[DivisionSemiring R] [DivisionSemiring S] [Module R M]
[Module S M₂] (n : ℕ) (x : M) : f ((n⁻¹ : R) • x) = (n⁻¹ : S) • f x := by
by_cases hR : (n : R) = 0 <;> by_cases hS : (n : S) = 0
· simp [hR, hS, map_zero f]
· suffices ∀ y, f y = 0 by rw [this, this, smul_zero]
clear x
intro x
rw [← inv_smul_smul₀ hS (f x), ← map_natCast_smul f R S]
simp [hR, map_zero f]
· suffices ∀ y, f y = 0 by simp [this]
clear x
intro x
rw [← smul_inv_smul₀ hR x, map_natCast_smul f R S, hS, zero_smul]
· rw [← inv_smul_smul₀ hS (f _), ← map_natCast_smul f R S, smul_inv_smul₀ hR]
theorem map_inv_intCast_smul [AddCommGroup M] [AddCommGroup M₂] {F : Type*} [FunLike F M M₂]
[AddMonoidHomClass F M M₂] (f : F) (R S : Type*) [DivisionRing R] [DivisionRing S] [Module R M]
[Module S M₂] (z : ℤ) (x : M) : f ((z⁻¹ : R) • x) = (z⁻¹ : S) • f x := by
obtain ⟨n, rfl | rfl⟩ := z.eq_nat_or_neg
· rw [Int.cast_natCast, Int.cast_natCast, map_inv_natCast_smul _ R S]
· simp_rw [Int.cast_neg, Int.cast_natCast, inv_neg, neg_smul, map_neg,
map_inv_natCast_smul _ R S]
/-- If `E` is a vector space over two division semirings `R` and `S`, then scalar multiplications
agree on inverses of natural numbers in `R` and `S`. -/
theorem inv_natCast_smul_eq {E : Type*} (R S : Type*) [AddCommMonoid E] [DivisionSemiring R]
[DivisionSemiring S] [Module R E] [Module S E] (n : ℕ) (x : E) :
(n⁻¹ : R) • x = (n⁻¹ : S) • x :=
map_inv_natCast_smul (AddMonoidHom.id E) R S n x
/-- If `E` is a vector space over two division rings `R` and `S`, then scalar multiplications
agree on inverses of integer numbers in `R` and `S`. -/
theorem inv_intCast_smul_eq {E : Type*} (R S : Type*) [AddCommGroup E] [DivisionRing R]
[DivisionRing S] [Module R E] [Module S E] (n : ℤ) (x : E) : (n⁻¹ : R) • x = (n⁻¹ : S) • x :=
map_inv_intCast_smul (AddMonoidHom.id E) R S n x
/-- If `E` is a vector space over a division semiring `R` and has a monoid action by `α`, then that
action commutes by scalar multiplication of inverses of natural numbers in `R`. -/
theorem inv_natCast_smul_comm {α E : Type*} (R : Type*) [AddCommMonoid E] [DivisionSemiring R]
[Monoid α] [Module R E] [DistribMulAction α E] (n : ℕ) (s : α) (x : E) :
(n⁻¹ : R) • s • x = s • (n⁻¹ : R) • x :=
(map_inv_natCast_smul (DistribMulAction.toAddMonoidHom E s) R R n x).symm
/-- If `E` is a vector space over a division ring `R` and has a monoid action by `α`, then that
action commutes by scalar multiplication of inverses of integers in `R` -/
theorem inv_intCast_smul_comm {α E : Type*} (R : Type*) [AddCommGroup E] [DivisionRing R]
[Monoid α] [Module R E] [DistribMulAction α E] (n : ℤ) (s : α) (x : E) :
(n⁻¹ : R) • s • x = s • (n⁻¹ : R) • x :=
(map_inv_intCast_smul (DistribMulAction.toAddMonoidHom E s) R R n x).symm
namespace Function
lemma support_smul_subset_left [Zero R] [Zero M] [SMulWithZero R M] (f : α → R) (g : α → M) :
support (f • g) ⊆ support f := fun x hfg hf ↦
hfg <| by rw [Pi.smul_apply', hf, zero_smul]
-- Changed (2024-01-21): this lemma was generalised;
-- the old version is now called `support_const_smul_subset`.
lemma support_smul_subset_right [Zero M] [SMulZeroClass R M] (f : α → R) (g : α → M) :
support (f • g) ⊆ support g :=
fun x hbf hf ↦ hbf <| by rw [Pi.smul_apply', hf, smul_zero]
lemma support_const_smul_of_ne_zero [Zero R] [Zero M] [SMulWithZero R M] [NoZeroSMulDivisors R M]
(c : R) (g : α → M) (hc : c ≠ 0) : support (c • g) = support g :=
ext fun x ↦ by simp only [hc, mem_support, Pi.smul_apply, Ne, smul_eq_zero, false_or]
lemma support_smul [Zero R] [Zero M] [SMulWithZero R M] [NoZeroSMulDivisors R M] (f : α → R)
(g : α → M) : support (f • g) = support f ∩ support g :=
ext fun _ => smul_ne_zero_iff
lemma support_const_smul_subset [Zero M] [SMulZeroClass R M] (a : R) (f : α → M) :
support (a • f) ⊆ support f := support_smul_subset_right (fun _ ↦ a) f
end Function
namespace Set
section SMulZeroClass
variable [Zero M] [SMulZeroClass R M]
lemma indicator_smul_apply (s : Set α) (r : α → R) (f : α → M) (a : α) :
indicator s (fun a ↦ r a • f a) a = r a • indicator s f a := by
dsimp only [indicator]
split_ifs
exacts [rfl, (smul_zero (r a)).symm]
lemma indicator_smul (s : Set α) (r : α → R) (f : α → M) :
indicator s (fun a ↦ r a • f a) = fun a ↦ r a • indicator s f a :=
funext <| indicator_smul_apply s r f
lemma indicator_const_smul_apply (s : Set α) (r : R) (f : α → M) (a : α) :
indicator s (r • f ·) a = r • indicator s f a :=
indicator_smul_apply s (fun _ ↦ r) f a
lemma indicator_const_smul (s : Set α) (r : R) (f : α → M) :
indicator s (r • f ·) = (r • indicator s f ·) :=
funext <| indicator_const_smul_apply s r f
end SMulZeroClass
section SMulWithZero
variable [Zero R] [Zero M] [SMulWithZero R M]
lemma indicator_smul_apply_left (s : Set α) (r : α → R) (f : α → M) (a : α) :
indicator s (fun a ↦ r a • f a) a = indicator s r a • f a := by
dsimp only [indicator]
split_ifs
exacts [rfl, (zero_smul _ (f a)).symm]
lemma indicator_smul_left (s : Set α) (r : α → R) (f : α → M) :
indicator s (fun a ↦ r a • f a) = fun a ↦ indicator s r a • f a :=
funext <| indicator_smul_apply_left _ _ _
lemma indicator_smul_const_apply (s : Set α) (r : α → R) (m : M) (a : α) :
indicator s (r · • m) a = indicator s r a • m := indicator_smul_apply_left _ _ _ _
lemma indicator_smul_const (s : Set α) (r : α → R) (m : M) :
indicator s (r · • m) = (indicator s r · • m) :=
funext <| indicator_smul_const_apply _ _ _
end SMulWithZero
section MulZeroOneClass
variable [MulZeroOneClass R]
lemma smul_indicator_one_apply (s : Set α) (r : R) (a : α) :
r • s.indicator (1 : α → R) a = s.indicator (fun _ ↦ r) a := by
simp_rw [← indicator_const_smul_apply, Pi.one_apply, smul_eq_mul, mul_one]
end MulZeroOneClass
end Set |
.lake/packages/mathlib/Mathlib/Algebra/Module/ULift.lean | import Mathlib.Algebra.GroupWithZero.ULift
import Mathlib.Algebra.Ring.ULift
import Mathlib.Algebra.Module.Equiv.Defs
import Mathlib.Data.ULift
/-!
# `ULift` instances for module and multiplicative actions
This file defines instances for module, mul_action and related structures on `ULift` types.
(Recall `ULift α` is just a "copy" of a type `α` in a higher universe.)
We also provide `ULift.moduleEquiv : ULift M ≃ₗ[R] M`.
-/
namespace ULift
universe u v w
variable {R : Type u} {M : Type v} {N : Type w}
@[to_additive]
instance smulLeft [SMul R M] : SMul (ULift R) M :=
⟨fun s x => s.down • x⟩
@[to_additive (attr := simp)]
theorem smul_def [SMul R M] (s : ULift R) (x : M) : s • x = s.down • x :=
rfl
instance isScalarTower [SMul R M] [SMul M N] [SMul R N] [IsScalarTower R M N] :
IsScalarTower (ULift R) M N :=
⟨fun x y z => show (x.down • y) • z = x.down • y • z from smul_assoc _ _ _⟩
instance isScalarTower' [SMul R M] [SMul M N] [SMul R N] [IsScalarTower R M N] :
IsScalarTower R (ULift M) N :=
⟨fun x y z => show (x • y.down) • z = x • y.down • z from smul_assoc _ _ _⟩
instance isScalarTower'' [SMul R M] [SMul M N] [SMul R N] [IsScalarTower R M N] :
IsScalarTower R M (ULift N) :=
⟨fun x y z => show up ((x • y) • z.down) = ⟨x • y • z.down⟩ by rw [smul_assoc]⟩
instance [SMul R M] [SMul Rᵐᵒᵖ M] [IsCentralScalar R M] : IsCentralScalar R (ULift M) :=
⟨fun r m => congr_arg up <| op_smul_eq_smul r m.down⟩
@[to_additive]
instance mulAction [Monoid R] [MulAction R M] : MulAction (ULift R) M where
mul_smul _ _ := mul_smul _ _
one_smul := one_smul _
@[to_additive]
instance mulAction' [Monoid R] [MulAction R M] : MulAction R (ULift M) where
mul_smul := fun _ _ _ => congr_arg ULift.up <| mul_smul _ _ _
one_smul := fun _ => congr_arg ULift.up <| one_smul _ _
instance smulZeroClass [Zero M] [SMulZeroClass R M] : SMulZeroClass (ULift R) M :=
{ ULift.smulLeft with smul_zero := fun _ => smul_zero _ }
instance smulZeroClass' [Zero M] [SMulZeroClass R M] : SMulZeroClass R (ULift M) where
smul_zero c := by { ext; simp [smul_zero] }
instance distribSMul [AddZeroClass M] [DistribSMul R M] : DistribSMul (ULift R) M where
smul_add _ := smul_add _
instance distribSMul' [AddZeroClass M] [DistribSMul R M] : DistribSMul R (ULift M) where
smul_add c f g := by
ext
simp [smul_add]
instance distribMulAction [Monoid R] [AddMonoid M] [DistribMulAction R M] :
DistribMulAction (ULift R) M :=
{ ULift.mulAction, ULift.distribSMul with }
instance distribMulAction' [Monoid R] [AddMonoid M] [DistribMulAction R M] :
DistribMulAction R (ULift M) :=
{ ULift.mulAction', ULift.distribSMul' with }
instance mulDistribMulAction [Monoid R] [Monoid M] [MulDistribMulAction R M] :
MulDistribMulAction (ULift R) M where
smul_one _ := smul_one _
smul_mul _ := smul_mul' _
instance mulDistribMulAction' [Monoid R] [Monoid M] [MulDistribMulAction R M] :
MulDistribMulAction R (ULift M) :=
{ ULift.mulAction' with
smul_one := fun _ => by
ext
simp [smul_one]
smul_mul := fun _ _ _ => by
ext
simp [smul_mul'] }
instance smulWithZero [Zero R] [Zero M] [SMulWithZero R M] : SMulWithZero (ULift R) M :=
{ ULift.smulLeft with
smul_zero := fun _ => smul_zero _
zero_smul := zero_smul _ }
instance smulWithZero' [Zero R] [Zero M] [SMulWithZero R M] : SMulWithZero R (ULift M) where
smul_zero _ := ULift.ext _ _ <| smul_zero _
zero_smul _ := ULift.ext _ _ <| zero_smul _ _
instance mulActionWithZero [MonoidWithZero R] [Zero M] [MulActionWithZero R M] :
MulActionWithZero (ULift R) M :=
{ ULift.smulWithZero with
one_smul := one_smul _
mul_smul := mul_smul }
instance mulActionWithZero' [MonoidWithZero R] [Zero M] [MulActionWithZero R M] :
MulActionWithZero R (ULift M) :=
{ ULift.smulWithZero' with
one_smul := one_smul _
mul_smul := mul_smul }
instance module [Semiring R] [AddCommMonoid M] [Module R M] : Module (ULift R) M :=
{ ULift.smulWithZero with
add_smul := fun _ _ => add_smul _ _
smul_add := smul_add
one_smul := one_smul _
mul_smul := mul_smul }
instance module' [Semiring R] [AddCommMonoid M] [Module R M] : Module R (ULift M) :=
{ ULift.smulWithZero' with
add_smul := fun _ _ _ => ULift.ext _ _ <| add_smul _ _ _
one_smul := one_smul _
mul_smul := mul_smul
smul_add := smul_add }
/-- The `R`-linear equivalence between `ULift M` and `M`.
This is a linear version of `AddEquiv.ulift`. -/
@[simps apply symm_apply]
def moduleEquiv [Semiring R] [AddCommMonoid M] [Module R M] : ULift.{w} M ≃ₗ[R] M where
toFun := ULift.down
invFun := ULift.up
map_smul' _ _ := rfl
__ := AddEquiv.ulift
end ULift |
.lake/packages/mathlib/Mathlib/Algebra/Module/End.lean | import Mathlib.Algebra.Group.Hom.End
import Mathlib.Algebra.Module.NatInt
/-!
# Module structure and endomorphisms
In this file, we define `Module.toAddMonoidEnd`, which is `(•)` as a monoid homomorphism.
We use this to prove some results on scalar multiplication by integers.
-/
assert_not_exists RelIso Multiset Set.indicator Pi.single_smul₀ Field
open Function Set
universe u v
variable {R S M M₂ : Type*}
section AddCommMonoid
variable [Semiring R] [AddCommMonoid M] [Module R M] (r s : R) (x : M)
theorem AddMonoid.End.natCast_def (n : ℕ) :
(↑n : AddMonoid.End M) = DistribMulAction.toAddMonoidEnd ℕ M n :=
rfl
variable (R M)
/-- `(•)` as an `AddMonoidHom`.
This is a stronger version of `DistribMulAction.toAddMonoidEnd` -/
@[simps! apply_apply]
def Module.toAddMonoidEnd : R →+* AddMonoid.End M :=
{ DistribMulAction.toAddMonoidEnd R M with
map_zero' := AddMonoidHom.ext fun r => by simp
map_add' x y :=
AddMonoidHom.ext fun r => by simp [(AddMonoidHom.add_apply), add_smul] }
/-- A convenience alias for `Module.toAddMonoidEnd` as an `AddMonoidHom`, usually to allow the
use of `AddMonoidHom.flip`. -/
def smulAddHom : R →+ M →+ M :=
(Module.toAddMonoidEnd R M).toAddMonoidHom
variable {R M}
@[simp]
theorem smulAddHom_apply : smulAddHom R M r x = r • x :=
rfl
variable {x}
lemma IsAddUnit.smul_left [DistribSMul S M] (hx : IsAddUnit x) (s : S) :
IsAddUnit (s • x) :=
hx.map (DistribSMul.toAddMonoidHom M s)
variable {r} (x)
lemma IsAddUnit.smul_right (hr : IsAddUnit r) : IsAddUnit (r • x) :=
hr.map (AddMonoidHom.flip (smulAddHom R M) x)
end AddCommMonoid
section AddCommGroup
variable (R M) [Semiring R] [AddCommGroup M]
theorem AddMonoid.End.intCast_def (z : ℤ) :
(↑z : AddMonoid.End M) = DistribMulAction.toAddMonoidEnd ℤ M z :=
rfl
end AddCommGroup |
.lake/packages/mathlib/Mathlib/Algebra/Module/RingHom.lean | import Mathlib.Algebra.GroupWithZero.Action.End
import Mathlib.Algebra.Module.Defs
import Mathlib.Algebra.Ring.Hom.Defs
/-!
# Composing modules with a ring hom
## Main definitions
* `Module.compHom`: compose a `Module` with a `RingHom`, with action `f s • m`.
* `RingHom.toModule`: a `RingHom` defines a module structure by `r • x = f r * x`.
## Tags
semimodule, module, vector space
-/
assert_not_exists Field Invertible Multiset Pi.single_smul₀ Set.indicator
open Function Set
universe u v
variable {R S M M₂ : Type*}
section AddCommMonoid
variable [Semiring R] [AddCommMonoid M] [Module R M] (r s : R) (x : M)
variable (R)
/-- Push forward the action of `R` on `M` along a compatible surjective map `f : R →+* S`.
See also `Function.Surjective.mulActionLeft` and `Function.Surjective.distribMulActionLeft`.
-/
abbrev Function.Surjective.moduleLeft {R S M : Type*} [Semiring R] [AddCommMonoid M] [Module R M]
[Semiring S] [SMul S M] (f : R →+* S) (hf : Function.Surjective f)
(hsmul : ∀ (c) (x : M), f c • x = c • x) : Module S M :=
{ hf.distribMulActionLeft f.toMonoidHom hsmul with
zero_smul := fun x => by rw [← f.map_zero, hsmul, zero_smul]
add_smul := hf.forall₂.mpr fun a b x => by simp only [← f.map_add, hsmul, add_smul] }
variable {R} (M)
/-- Compose a `Module` with a `RingHom`, with action `f s • m`.
See note [reducible non-instances]. -/
abbrev Module.compHom [Semiring S] (f : S →+* R) : Module S M :=
{ MulActionWithZero.compHom M f.toMonoidWithZeroHom, DistribMulAction.compHom M (f : S →* R) with
-- Porting note: the `show f (r + s) • x = f r • x + f s • x` wasn't needed in mathlib3.
-- Somehow, now that `SMul` is heterogeneous, it can't unfold earlier fields of a definition for
-- use in later fields. See
-- https://leanprover.zulipchat.com/#narrow/stream/287929-mathlib4/topic/Heterogeneous.20scalar.20multiplication
-- TODO(jmc): there should be a rw-lemma `smul_comp` close to `SMulZeroClass.compFun`
add_smul := fun r s x => show f (r + s) • x = f r • x + f s • x by simp [add_smul] }
variable {M}
end AddCommMonoid
/-- A ring homomorphism `f : R →+* M` defines a module structure by `r • x = f r * x`.
See note [reducible non-instances]. -/
abbrev RingHom.toModule [Semiring R] [Semiring S] (f : R →+* S) : Module R S :=
Module.compHom S f
/-- If the module action of `R` on `S` is compatible with multiplication on `S`, then
`fun x ↦ x • 1` is a ring homomorphism from `R` to `S`.
This is the `RingHom` version of `MonoidHom.smulOneHom`.
When `R` is commutative, usually `algebraMap` should be preferred. -/
@[simps!] def RingHom.smulOneHom
[Semiring R] [NonAssocSemiring S] [Module R S] [IsScalarTower R S S] : R →+* S where
__ := MonoidHom.smulOneHom
map_zero' := zero_smul R 1
map_add' := (add_smul · · 1)
/-- A homomorphism between semirings R and S can be equivalently specified by a R-module
structure on S such that S/S/R is a scalar tower. -/
def ringHomEquivModuleIsScalarTower [Semiring R] [Semiring S] :
(R →+* S) ≃ {_inst : Module R S // IsScalarTower R S S} where
toFun f := ⟨Module.compHom S f, SMul.comp.isScalarTower _⟩
invFun := fun ⟨_, _⟩ ↦ RingHom.smulOneHom
left_inv f := RingHom.ext fun r ↦ mul_one (f r)
right_inv := fun ⟨_, _⟩ ↦ Subtype.ext <| Module.ext <| funext₂ <| smul_one_smul S |
.lake/packages/mathlib/Mathlib/Algebra/Module/PointwisePi.lean | import Mathlib.Algebra.GroupWithZero.Units.Basic
import Mathlib.Algebra.Group.Action.Basic
import Mathlib.Algebra.Group.Pointwise.Set.Scalar
/-!
# Pointwise actions on sets in Pi types
This file contains lemmas about pointwise actions on sets in Pi types.
## Tags
set multiplication, set addition, pointwise addition, pointwise multiplication, pi
-/
open Pointwise
open Set
variable {K ι : Type*} {R : ι → Type*}
@[to_additive]
theorem smul_pi_subset [∀ i, SMul K (R i)] (r : K) (s : Set ι) (t : ∀ i, Set (R i)) :
r • pi s t ⊆ pi s (r • t) :=
piMap_image_pi_subset _
@[to_additive]
theorem smul_univ_pi [∀ i, SMul K (R i)] (r : K) (t : ∀ i, Set (R i)) :
r • pi (univ : Set ι) t = pi (univ : Set ι) (r • t) :=
piMap_image_univ_pi _ _
@[to_additive]
theorem smul_pi [Group K] [∀ i, MulAction K (R i)] (r : K) (S : Set ι) (t : ∀ i, Set (R i)) :
r • S.pi t = S.pi (r • t) :=
piMap_image_pi (fun _ _ => MulAction.surjective _) _
theorem smul_pi₀ [GroupWithZero K] [∀ i, MulAction K (R i)] {r : K} (S : Set ι) (t : ∀ i, Set (R i))
(hr : r ≠ 0) : r • S.pi t = S.pi (r • t) :=
smul_pi (Units.mk0 r hr) S t |
.lake/packages/mathlib/Mathlib/Algebra/Module/Defs.lean | import Mathlib.Algebra.GroupWithZero.Action.Defs
import Mathlib.Algebra.Ring.Defs
/-!
# Modules over a ring
In this file we define
* `Module R M` : an additive commutative monoid `M` is a `Module` over a
`Semiring R` if for `r : R` and `x : M` their "scalar multiplication" `r • x : M` is defined, and
the operation `•` satisfies some natural associativity and distributivity axioms similar to those
on a ring.
## Implementation notes
In typical mathematical usage, our definition of `Module` corresponds to "semimodule", and the
word "module" is reserved for `Module R M` where `R` is a `Ring` and `M` an `AddCommGroup`.
If `R` is a `Field` and `M` an `AddCommGroup`, `M` would be called an `R`-vector space.
Since those assumptions can be made by changing the typeclasses applied to `R` and `M`,
without changing the axioms in `Module`, mathlib calls everything a `Module`.
In older versions of mathlib3, we had separate abbreviations for semimodules and vector spaces.
This caused inference issues in some cases, while not providing any real advantages, so we decided
to use a canonical `Module` typeclass throughout.
## Tags
semimodule, module, vector space
-/
assert_not_exists Field Invertible Pi.single_smul₀ RingHom Set.indicator Multiset Units
open Function Set
universe u v
variable {R S M M₂ : Type*}
/-- A module is a generalization of vector spaces to a scalar semiring.
It consists of a scalar semiring `R` and an additive monoid of "vectors" `M`,
connected by a "scalar multiplication" operation `r • x : M`
(where `r : R` and `x : M`) with some natural associativity and
distributivity axioms similar to those on a ring. -/
@[ext]
class Module (R : Type u) (M : Type v) [Semiring R] [AddCommMonoid M] extends
DistribMulAction R M where
/-- Scalar multiplication distributes over addition from the right. -/
protected add_smul : ∀ (r s : R) (x : M), (r + s) • x = r • x + s • x
/-- Scalar multiplication by zero gives zero. -/
protected zero_smul : ∀ x : M, (0 : R) • x = 0
section AddCommMonoid
variable [Semiring R] [AddCommMonoid M] [Module R M] (r s : R) (x : M)
-- see Note [lower instance priority]
/-- A module over a semiring automatically inherits a `MulActionWithZero` structure. -/
instance (priority := 100) Module.toMulActionWithZero
{R M} {_ : Semiring R} {_ : AddCommMonoid M} [Module R M] : MulActionWithZero R M :=
{ (inferInstance : MulAction R M) with
smul_zero := smul_zero
zero_smul := Module.zero_smul }
theorem add_smul : (r + s) • x = r • x + s • x :=
Module.add_smul r s x
theorem Convex.combo_self {a b : R} (h : a + b = 1) (x : M) : a • x + b • x = x := by
rw [← add_smul, h, one_smul]
variable (R)
theorem two_smul : (2 : R) • x = x + x := by rw [← one_add_one_eq_two, add_smul, one_smul]
/-- Pullback a `Module` structure along an injective additive monoid homomorphism.
See note [reducible non-instances]. -/
protected abbrev Function.Injective.module [AddCommMonoid M₂] [SMul R M₂] (f : M₂ →+ M)
(hf : Injective f) (smul : ∀ (c : R) (x), f (c • x) = c • f x) : Module R M₂ :=
{ hf.distribMulAction f smul with
add_smul := fun c₁ c₂ x => hf <| by simp only [smul, f.map_add, add_smul]
zero_smul := fun x => hf <| by simp only [smul, zero_smul, f.map_zero] }
/-- Pushforward a `Module` structure along a surjective additive monoid homomorphism.
See note [reducible non-instances]. -/
protected abbrev Function.Surjective.module [AddCommMonoid M₂] [SMul R M₂] (f : M →+ M₂)
(hf : Surjective f) (smul : ∀ (c : R) (x), f (c • x) = c • f x) : Module R M₂ :=
{ toDistribMulAction := hf.distribMulAction f smul
add_smul := fun c₁ c₂ x => by
rcases hf x with ⟨x, rfl⟩
simp only [add_smul, ← smul, ← f.map_add]
zero_smul := fun x => by
rcases hf x with ⟨x, rfl⟩
rw [← f.map_zero, ← smul, zero_smul] }
variable {R}
theorem Module.eq_zero_of_zero_eq_one (zero_eq_one : (0 : R) = 1) : x = 0 := by
rw [← one_smul R x, ← zero_eq_one, zero_smul]
@[simp]
theorem smul_add_one_sub_smul {R : Type*} [Ring R] [Module R M] {r : R} {m : M} :
r • m + (1 - r) • m = m := by rw [← add_smul, add_sub_cancel, one_smul]
end AddCommMonoid
section AddCommGroup
variable [Semiring R] [AddCommGroup M]
theorem Convex.combo_eq_smul_sub_add [Module R M] {x y : M} {a b : R} (h : a + b = 1) :
a • x + b • y = b • (y - x) + x :=
calc
a • x + b • y = b • y - b • x + (a • x + b • x) := by rw [sub_add_add_cancel, add_comm]
_ = b • (y - x) + x := by rw [smul_sub, Convex.combo_self h]
end AddCommGroup
-- We'll later use this to show `Module ℕ M` and `Module ℤ M` are subsingletons.
/-- A variant of `Module.ext` that's convenient for term-mode. -/
theorem Module.ext' {R : Type*} [Semiring R] {M : Type*} [AddCommMonoid M] (P Q : Module R M)
(w : ∀ (r : R) (m : M), (haveI := P; r • m) = (haveI := Q; r • m)) :
P = Q := by
ext
exact w _ _
section Module
variable [Ring R] [AddCommGroup M] [Module R M] (r : R) (x : M)
@[simp]
theorem neg_smul : -r • x = -(r • x) :=
eq_neg_of_add_eq_zero_left <| by rw [← add_smul, neg_add_cancel, zero_smul]
theorem neg_smul_neg : -r • -x = r • x := by rw [neg_smul, smul_neg, neg_neg]
variable (R)
theorem neg_one_smul (x : M) : (-1 : R) • x = -x := by simp
variable {R}
theorem sub_smul (r s : R) (y : M) : (r - s) • y = r • y - s • y := by
simp [add_smul, sub_eq_add_neg]
end Module
/-- A module over a `Subsingleton` semiring is a `Subsingleton`. We cannot register this
as an instance because Lean has no way to guess `R`. -/
protected theorem Module.subsingleton (R M : Type*) [MonoidWithZero R] [Subsingleton R] [Zero M]
[MulActionWithZero R M] : Subsingleton M :=
MulActionWithZero.subsingleton R M
/-- A semiring is `Nontrivial` provided that there exists a nontrivial module over this semiring. -/
protected theorem Module.nontrivial (R M : Type*) [MonoidWithZero R] [Nontrivial M] [Zero M]
[MulActionWithZero R M] : Nontrivial R :=
MulActionWithZero.nontrivial R M
-- see Note [lower instance priority]
instance (priority := 910) Semiring.toModule [Semiring R] : Module R R where
smul_add := mul_add
add_smul := add_mul
zero_smul := zero_mul
smul_zero := mul_zero
instance [NonUnitalNonAssocSemiring R] : DistribSMul R R where
smul_add := left_distrib |
.lake/packages/mathlib/Mathlib/Algebra/Module/CharacterModule.lean | import Mathlib.Algebra.Category.ModuleCat.Basic
import Mathlib.Algebra.Category.Grp.Injective
import Mathlib.Topology.Instances.AddCircle.Defs
import Mathlib.LinearAlgebra.Isomorphisms
/-!
# Character module of a module
For commutative ring `R` and an `R`-module `M` and an injective module `D`, its character module
`M⋆` is defined to be `R`-linear maps `M ⟶ D`.
`M⋆` also has an `R`-module structure given by `(r • f) m = f (r • m)`.
## Main results
- `CharacterModuleFunctor` : the contravariant functor of `R`-modules where `M ↦ M⋆` and
an `R`-linear map `l : M ⟶ N` induces an `R`-linear map `l⋆ : f ↦ f ∘ l` where `f : N⋆`.
- `LinearMap.dual_surjective_of_injective` : If `l` is injective then `l⋆` is surjective,
in another word taking character module as a functor sends monos to epis.
- `CharacterModule.homEquiv` : there is a bijection between linear map `Hom(N, M⋆)` and
`(N ⊗ M)⋆` given by `curry` and `uncurry`.
-/
open CategoryTheory
universe uR uA uB
variable (R : Type uR) [CommRing R]
variable (A : Type uA) [AddCommGroup A]
variable (A' : Type*) [AddCommGroup A']
variable (B : Type uB) [AddCommGroup B]
/--
The character module of an abelian group `A` in the unit rational circle is `A⋆ := Hom_ℤ(A, ℚ ⧸ ℤ)`.
-/
def CharacterModule : Type uA := A →+ AddCircle (1 : ℚ)
namespace CharacterModule
instance : FunLike (CharacterModule A) A (AddCircle (1 : ℚ)) where
coe c := c.toFun
coe_injective' _ _ _ := by simp_all
instance : LinearMapClass (CharacterModule A) ℤ A (AddCircle (1 : ℚ)) where
map_add _ _ _ := by rw [AddMonoidHom.map_add]
map_smulₛₗ _ _ _ := by rw [AddMonoidHom.map_zsmul, RingHom.id_apply]
instance : AddCommGroup (CharacterModule A) :=
inferInstanceAs (AddCommGroup (A →+ _))
@[ext] theorem ext {c c' : CharacterModule A} (h : ∀ x, c x = c' x) : c = c' := DFunLike.ext _ _ h
section module
variable [Module R A] [Module R A'] [Module R B]
instance : Module R (CharacterModule A) :=
Module.compHom (A →+ _) (RingEquiv.toOpposite _ |>.toRingHom : R →+* Rᵈᵐᵃ)
variable {R A B}
@[simp] lemma smul_apply (c : CharacterModule A) (r : R) (a : A) : (r • c) a = c (r • a) := rfl
/--
Given an abelian group homomorphism `f : A → B`, `f⋆(L) := L ∘ f` defines a linear map
from `B⋆` to `A⋆`.
-/
@[simps] def dual (f : A →ₗ[R] B) : CharacterModule B →ₗ[R] CharacterModule A where
toFun L := L.comp f.toAddMonoidHom
map_add' := by aesop
map_smul' r c := by ext x; exact congr(c $(f.map_smul r x)).symm
@[simp]
lemma dual_zero : dual (0 : A →ₗ[R] B) = 0 := by
ext f
exact map_zero f
lemma dual_comp {C : Type*} [AddCommGroup C] [Module R C] (f : A →ₗ[R] B) (g : B →ₗ[R] C) :
dual (g.comp f) = (dual f).comp (dual g) := by
ext
rfl
lemma dual_injective_of_surjective (f : A →ₗ[R] B) (hf : Function.Surjective f) :
Function.Injective (dual f) := by
intro φ ψ eq
ext x
obtain ⟨y, rfl⟩ := hf x
change (dual f) φ _ = (dual f) ψ _
rw [eq]
lemma dual_surjective_of_injective (f : A →ₗ[R] B) (hf : Function.Injective f) :
Function.Surjective (dual f) :=
(Module.Baer.of_divisible _).extension_property_addMonoidHom _ hf
/--
Two isomorphic modules have isomorphic character modules.
-/
def congr (e : A ≃ₗ[R] B) : CharacterModule A ≃ₗ[R] CharacterModule B :=
.ofLinear (dual e.symm) (dual e)
(by ext c _; exact congr(c $(e.right_inv _)))
(by ext c _; exact congr(c $(e.left_inv _)))
open TensorProduct
/--
Any linear map `L : A → B⋆` induces a character in `(A ⊗ B)⋆` by `a ⊗ b ↦ L a b`.
-/
@[simps] noncomputable def uncurry :
(A →ₗ[R] CharacterModule B) →ₗ[R] CharacterModule (A ⊗[R] B) where
toFun c := TensorProduct.liftAddHom c.toAddMonoidHom fun r a b ↦ congr($(c.map_smul r a) b)
map_add' c c' := DFunLike.ext _ _ fun x ↦ by refine x.induction_on ?_ ?_ ?_ <;> aesop
map_smul' r c := DFunLike.ext _ _ fun x ↦ x.induction_on
(by simp_rw [map_zero]) (fun a b ↦ congr($(c.map_smul r a) b).symm) (by aesop)
/--
Any character `c` in `(A ⊗ B)⋆` induces a linear map `A → B⋆` by `a ↦ b ↦ c (a ⊗ b)`.
-/
@[simps] noncomputable def curry :
CharacterModule (A ⊗[R] B) →ₗ[R] (A →ₗ[R] CharacterModule B) where
toFun c :=
{ toFun := (c.comp <| TensorProduct.mk R A B ·)
map_add' := fun _ _ ↦ DFunLike.ext _ _ fun b ↦
congr(c <| $(map_add (mk R A B) _ _) b).trans (c.map_add _ _)
map_smul' := fun r a ↦ by ext; exact congr(c $(TensorProduct.tmul_smul _ _ _)).symm }
map_add' _ _ := rfl
map_smul' r c := by ext; exact congr(c $(TensorProduct.tmul_smul _ _ _)).symm
/--
Linear maps into a character module are exactly characters of the tensor product.
-/
@[simps!] noncomputable def homEquiv :
(A →ₗ[R] CharacterModule B) ≃ₗ[R] CharacterModule (A ⊗[R] B) :=
.ofLinear uncurry curry (by ext _ z; refine z.induction_on ?_ ?_ ?_ <;> aesop) (by aesop)
theorem dual_rTensor_conj_homEquiv (f : A →ₗ[R] A') :
homEquiv.symm.toLinearMap ∘ₗ dual (f.rTensor B) ∘ₗ homEquiv.toLinearMap = f.lcomp R _ := rfl
end module
/--
`ℤ⋆`, the character module of `ℤ` in the unit rational circle.
-/
protected abbrev int : Type := CharacterModule ℤ
/-- Given `n : ℕ`, the map `m ↦ m / n`. -/
protected abbrev int.divByNat (n : ℕ) : CharacterModule.int :=
LinearMap.toSpanSingleton ℤ _ (QuotientAddGroup.mk (n : ℚ)⁻¹) |>.toAddMonoidHom
protected lemma int.divByNat_self (n : ℕ) :
int.divByNat n n = 0 := by
obtain rfl | h0 := eq_or_ne n 0
· apply map_zero
exact (AddCircle.coe_eq_zero_iff _).mpr
⟨1, by simp [mul_inv_cancel₀ (Nat.cast_ne_zero (R := ℚ).mpr h0)]⟩
variable {A}
/-- The `ℤ`-submodule spanned by a single element `a` is isomorphic to the quotient of `ℤ`
by the ideal generated by the order of `a`. -/
@[simps!] noncomputable def intSpanEquivQuotAddOrderOf (a : A) :
(ℤ ∙ a) ≃ₗ[ℤ] ℤ ⧸ Ideal.span {(addOrderOf a : ℤ)} :=
LinearEquiv.ofEq _ _ (LinearMap.span_singleton_eq_range ℤ A a) ≪≫ₗ
(LinearMap.quotKerEquivRange <| LinearMap.toSpanSingleton ℤ A a).symm ≪≫ₗ
Submodule.quotEquivOfEq _ _ (by
ext1 x
rw [Ideal.mem_span_singleton, addOrderOf_dvd_iff_zsmul_eq_zero, LinearMap.mem_ker,
LinearMap.toSpanSingleton_apply])
lemma intSpanEquivQuotAddOrderOf_apply_self (a : A) :
intSpanEquivQuotAddOrderOf a ⟨a, Submodule.mem_span_singleton_self a⟩ =
Submodule.Quotient.mk 1 :=
(LinearEquiv.eq_symm_apply _).mp <| Subtype.ext (one_zsmul _).symm
/--
For an abelian group `A` and an element `a ∈ A`, there is a character `c : ℤ ∙ a → ℚ ⧸ ℤ` given by
`m • a ↦ m / n` where `n` is the smallest positive integer such that `n • a = 0` and when such `n`
does not exist, `c` is defined by `m • a ↦ m / 2`.
-/
noncomputable def ofSpanSingleton (a : A) : CharacterModule (ℤ ∙ a) :=
let l : ℤ ⧸ Ideal.span {(addOrderOf a : ℤ)} →ₗ[ℤ] AddCircle (1 : ℚ) :=
Submodule.liftQSpanSingleton _
(CharacterModule.int.divByNat <|
if addOrderOf a = 0 then 2 else addOrderOf a).toIntLinearMap <| by
split_ifs with h
· rw [h, Nat.cast_zero, map_zero]
· apply CharacterModule.int.divByNat_self
l ∘ₗ intSpanEquivQuotAddOrderOf a |>.toAddMonoidHom
lemma eq_zero_of_ofSpanSingleton_apply_self (a : A)
(h : ofSpanSingleton a ⟨a, Submodule.mem_span_singleton_self a⟩ = 0) : a = 0 := by
erw [ofSpanSingleton, LinearMap.toAddMonoidHom_coe, LinearMap.comp_apply,
intSpanEquivQuotAddOrderOf_apply_self, Submodule.liftQSpanSingleton_apply,
AddMonoidHom.coe_toIntLinearMap, int.divByNat, LinearMap.toSpanSingleton_one,
AddCircle.coe_eq_zero_iff] at h
rcases h with ⟨n, hn⟩
apply_fun Rat.den at hn
rw [zsmul_one, Rat.den_intCast, Rat.inv_natCast_den_of_pos] at hn
· split_ifs at hn
· cases hn
· rwa [eq_comm, AddMonoid.addOrderOf_eq_one_iff] at hn
· grind
lemma exists_character_apply_ne_zero_of_ne_zero {a : A} (ne_zero : a ≠ 0) :
∃ (c : CharacterModule A), c a ≠ 0 :=
have ⟨c, hc⟩ := dual_surjective_of_injective _ (Submodule.injective_subtype _) (ofSpanSingleton a)
⟨c, fun h ↦ ne_zero <| eq_zero_of_ofSpanSingleton_apply_self a <| by rwa [← hc]⟩
lemma eq_zero_of_character_apply {a : A} (h : ∀ c : CharacterModule A, c a = 0) : a = 0 := by
contrapose! h; exact exists_character_apply_ne_zero_of_ne_zero h
variable [Module R A] [Module R A'] [Module R B] {R A' B}
lemma dual_surjective_iff_injective {f : A →ₗ[R] A'} :
Function.Surjective (dual f) ↔ Function.Injective f :=
⟨fun h ↦ (injective_iff_map_eq_zero _).2 fun a h0 ↦ eq_zero_of_character_apply fun c ↦ by
obtain ⟨c, rfl⟩ := h c; exact congr(c $h0).trans c.map_zero,
dual_surjective_of_injective f⟩
theorem _root_.rTensor_injective_iff_lcomp_surjective {f : A →ₗ[R] A'} :
Function.Injective (f.rTensor B) ↔ Function.Surjective (f.lcomp R <| CharacterModule B) := by
simp [← dual_rTensor_conj_homEquiv, dual_surjective_iff_injective]
lemma surjective_of_dual_injective (f : A →ₗ[R] A') (hf : Function.Injective (dual f)) :
Function.Surjective f := by
rw [← LinearMap.range_eq_top, ← Submodule.unique_quotient_iff_eq_top]
refine ⟨Unique.mk inferInstance fun a ↦ eq_zero_of_character_apply fun c ↦ ?_⟩
obtain ⟨b, rfl⟩ := QuotientAddGroup.mk'_surjective _ a
suffices eq : dual (Submodule.mkQ _) c = 0 from congr($eq b)
refine hf ?_
rw [← LinearMap.comp_apply, ← dual_comp, LinearMap.range_mkQ_comp, dual_zero,
LinearMap.zero_apply, dual_apply, AddMonoidHom.zero_comp]
lemma dual_injective_iff_surjective {f : A →ₗ[R] A'} :
Function.Injective (dual f) ↔ Function.Surjective f :=
⟨fun h ↦ surjective_of_dual_injective f h, fun h ↦ dual_injective_of_surjective f h⟩
lemma dual_bijective_iff_bijective {f : A →ₗ[R] A'} :
Function.Bijective (dual f) ↔ Function.Bijective f :=
⟨fun h ↦ ⟨dual_surjective_iff_injective.mp h.2, dual_injective_iff_surjective.mp h.1⟩,
fun h ↦ ⟨dual_injective_iff_surjective.mpr h.2, dual_surjective_iff_injective.mpr h.1⟩⟩
end CharacterModule |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.