Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.

Commit a8d37c1

Browse files
feat(data/nat/factorization): Defining factorization (#10540)
Defining `factorization` as a `finsupp`, as discussed in https://leanprover.zulipchat.com/#narrow/stream/113489-new-members/topic/Prime.20factorizations and https://leanprover.zulipchat.com/#narrow/stream/113489-new-members/topic/Proof.20of.20Euler's.20product.20formula.20for.20totient This is the first of a series of PRs to build up the infrastructure needed for the proof of Euler's product formula for the totient function.
1 parent 3b02ad7 commit a8d37c1

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed

src/data/nat/factorization.lean

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/-
2+
Copyright (c) 2021 Stuart Presnell. All rights reserved.
3+
Released under Apache 2.0 license as described in the file LICENSE.
4+
Authors: Stuart Presnell
5+
-/
6+
import data.nat.prime
7+
import data.nat.mul_ind
8+
9+
/-!
10+
# Prime factorizations
11+
12+
`n.factorization` is the finitely supported function `ℕ →₀ ℕ`
13+
mapping each prime factor of `n` to its multiplicity in `n`. For example, since 2000 = 2^4 * 5^3,
14+
* `factorization 2000 2` is 4
15+
* `factorization 2000 5` is 3
16+
* `factorization 2000 k` is 0 for all other `k : ℕ`.
17+
18+
## TODO
19+
20+
* As discussed in this Zulip thread:
21+
https://leanprover.zulipchat.com/#narrow/stream/217875/topic/Multiplicity.20in.20the.20naturals
22+
We have lots of disparate ways of talking about the multiplicity of a prime
23+
in a natural number, including `factors.count`, `padic_val_nat`, `multiplicity`,
24+
and the material in `data/pnat/factors`. Move some of this material to this file,
25+
prove results about the relationships between these definitions,
26+
and (where appropriate) choose a uniform canonical way of expressing these ideas.
27+
28+
* Moreover, the results here should be generalised to an arbitrary unique factorization monoid
29+
with a normalization function, and then deduplicated. The basics of this have been started in
30+
`ring_theory/unique_factorization_domain`.
31+
32+
-/
33+
34+
open nat finset list finsupp
35+
open_locale big_operators
36+
37+
namespace nat
38+
39+
/-- `n.factorization` is the finitely supported function `ℕ →₀ ℕ`
40+
mapping each prime factor of `n` to its multiplicity in `n`. -/
41+
noncomputable def factorization (n : ℕ) : ℕ →₀ ℕ := (n.factors : multiset ℕ).to_finsupp
42+
43+
lemma factorization_eq_count {n p : ℕ} : n.factorization p = n.factors.count p :=
44+
by simp [factorization]
45+
-- TODO: As part of the unification mentioned in the TODO above,
46+
-- consider making this a [simp] lemma from `n.factors.count` to `n.factorization`
47+
48+
/-- Every nonzero natural number has a unique prime factorization -/
49+
lemma factorization_inj : set.inj_on factorization { x : ℕ | x ≠ 0 } :=
50+
λ a ha b hb h, eq_of_count_factors_eq
51+
(zero_lt_iff.mpr ha) (zero_lt_iff.mpr hb) (λ p, by simp [←factorization_eq_count, h])
52+
53+
@[simp] lemma factorization_zero : factorization 0 = 0 :=
54+
by simp [factorization]
55+
56+
@[simp] lemma factorization_one : factorization 1 = 0 :=
57+
by simp [factorization]
58+
59+
/-- The support of `n.factorization` is exactly `n.factors.to_finset` -/
60+
@[simp] lemma support_factorization {n : ℕ} :
61+
n.factorization.support = n.factors.to_finset :=
62+
by simpa [factorization, multiset.to_finsupp_support]
63+
64+
lemma factor_iff_mem_factorization {n p : ℕ} : p ∈ n.factorization.support ↔ p ∈ n.factors :=
65+
by simp only [support_factorization, list.mem_to_finset]
66+
67+
/-- The only numbers with empty prime factorization are `0` and `1` -/
68+
lemma factorization_eq_zero_iff (n : ℕ) : n.factorization = 0 ↔ n = 0 ∨ n = 1 :=
69+
by simp [factorization, add_equiv.map_eq_zero_iff, multiset.coe_eq_zero]
70+
71+
/-- For nonzero `a` and `b`, the power of `p` in `a * b` is the sum of the powers in `a` and `b` -/
72+
@[simp] lemma factorization_mul {a b : ℕ} (ha : a ≠ 0) (hb : b ≠ 0) :
73+
(a * b).factorization = a.factorization + b.factorization :=
74+
by { ext p, simp only [add_apply, factorization_eq_count,
75+
count_factors_mul_of_pos (zero_lt_iff.mpr ha) (zero_lt_iff.mpr hb)] }
76+
77+
/-- For any `p`, the power of `p` in `n^k` is `k` times the power in `n` -/
78+
lemma factorization_pow {n k : ℕ} :
79+
factorization (n^k) = k • n.factorization :=
80+
by { ext p, simp [factorization_eq_count, factors_count_pow] }
81+
82+
/-- The only prime factor of prime `p` is `p` itself, with multiplicity `1` -/
83+
@[simp] lemma prime.factorization {p : ℕ} (hp : prime p) :
84+
p.factorization = single p 1 :=
85+
begin
86+
ext q,
87+
rw [factorization_eq_count, factors_prime hp, single_apply, count_singleton', if_congr eq_comm];
88+
refl,
89+
end
90+
91+
/-- For prime `p` the only prime factor of `p^k` is `p` with multiplicity `k` -/
92+
@[simp] lemma prime.factorization_pow {p k : ℕ} (hp : prime p) :
93+
factorization (p^k) = single p k :=
94+
by simp [factorization_pow, hp.factorization]
95+
96+
end nat

src/ring_theory/unique_factorization_domain.lean

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,3 +1421,46 @@ begin
14211421
end
14221422

14231423
end unique_factorization_monoid
1424+
1425+
section finsupp
1426+
variables [cancel_comm_monoid_with_zero α] [unique_factorization_monoid α]
1427+
variables [normalization_monoid α] [decidable_eq α]
1428+
1429+
open unique_factorization_monoid
1430+
1431+
/-- This returns the multiset of irreducible factors as a `finsupp` -/
1432+
noncomputable def factorization (n : α) : α →₀ ℕ := (normalized_factors n).to_finsupp
1433+
1434+
lemma factorization_eq_count {n p : α} :
1435+
factorization n p = multiset.count p (normalized_factors n) :=
1436+
by simp [factorization]
1437+
1438+
@[simp] lemma factorization_zero : factorization (0 : α) = 0 := by simp [factorization]
1439+
1440+
@[simp] lemma factorization_one : factorization (1 : α) = 0 := by simp [factorization]
1441+
1442+
/-- The support of `factorization n` is exactly the finset of normalized factors -/
1443+
@[simp] lemma support_factorization {n : α} :
1444+
(factorization n).support = (normalized_factors n).to_finset :=
1445+
by simp [factorization, multiset.to_finsupp_support]
1446+
1447+
/-- For nonzero `a` and `b`, the power of `p` in `a * b` is the sum of the powers in `a` and `b` -/
1448+
@[simp] lemma factorization_mul {a b : α} (ha : a ≠ 0) (hb : b ≠ 0) :
1449+
factorization (a * b) = factorization a + factorization b :=
1450+
by simp [factorization, normalized_factors_mul ha hb]
1451+
1452+
/-- For any `p`, the power of `p` in `x^n` is `n` times the power in `x` -/
1453+
lemma factorization_pow {x : α} {n : ℕ} :
1454+
factorization (x^n) = n • factorization x :=
1455+
by { ext, simp [factorization] }
1456+
1457+
lemma associated_of_factorization_eq (a b: α) (ha: a ≠ 0) (hb: b ≠ 0)
1458+
(h: factorization a = factorization b) : associated a b :=
1459+
begin
1460+
simp only [factorization, add_equiv.apply_eq_iff_eq] at h,
1461+
have ha' := normalized_factors_prod ha,
1462+
rw h at ha',
1463+
exact associated.trans ha'.symm (normalized_factors_prod hb),
1464+
end
1465+
1466+
end finsupp

0 commit comments

Comments
 (0)