Skip to content

Commit 37351aa

Browse files
feat(Rat): Numerator and denominator of q ^ n (#12506)
Prove `(q ^ n).num = q.num ^ n` and `(q ^ n).den = q.den ^ n` by making those defeq. It is somewhat painful. `(q ^ n).den = q.den ^ n` is also defeq for `NNRat`, but not `(q ^ n).num = q.num ^ n` due to the `Int.natAbs` in the definition of `NNRat.num`. Co-authored-by: Eric Wieser <wieser.eric@gmail.com>
1 parent 333b54a commit 37351aa

File tree

3 files changed

+57
-11
lines changed

3 files changed

+57
-11
lines changed

Mathlib/Data/NNRat/Defs.lean

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ theorem coe_mul (p q : ℚ≥0) : ((p * q : ℚ≥0) : ℚ) = p * q :=
121121
rfl
122122
#align nnrat.coe_mul NNRat.coe_mul
123123

124+
@[simp, norm_cast] lemma coe_pow (q : ℚ≥0) (n : ℕ) : (↑(q ^ n) : ℚ) = (q : ℚ) ^ n := rfl
125+
#align nnrat.coe_pow NNRat.coe_pow
126+
127+
@[simp] lemma num_pow (q : ℚ≥0) (n : ℕ) : (q ^ n).num = q.num ^ n := by simp [num, Int.natAbs_pow]
128+
@[simp] lemma den_pow (q : ℚ≥0) (n : ℕ) : (q ^ n).den = q.den ^ n := rfl
129+
124130
-- Porting note: `bit0` `bit1` are deprecated, so remove these theorems.
125131
#noalign nnrat.coe_bit0
126132
#noalign nnrat.coe_bit1
@@ -202,10 +208,6 @@ theorem coe_coeHom : ⇑coeHom = ((↑) : ℚ≥0 → ℚ) :=
202208
rfl
203209
#align nnrat.coe_coe_hom NNRat.coe_coeHom
204210

205-
@[simp, norm_cast]
206-
theorem coe_pow (q : ℚ≥0) (n : ℕ) : (↑(q ^ n) : ℚ) = (q : ℚ) ^ n :=
207-
coeHom.map_pow _ _
208-
#align nnrat.coe_pow NNRat.coe_pow
209211
@[norm_cast]
210212
theorem nsmul_coe (q : ℚ≥0) (n : ℕ) : ↑(n • q) = n • (q : ℚ) :=
211213
coeHom.toAddMonoidHom.map_nsmul _ _

Mathlib/Data/Rat/Defs.lean

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Released under Apache 2.0 license as described in the file LICENSE.
44
Authors: Johannes Hölzl, Mario Carneiro
55
-/
66
import Mathlib.Algebra.Ring.Basic
7-
import Mathlib.Algebra.GroupWithZero.Basic
7+
import Mathlib.Algebra.GroupWithZero.Units.Basic
88
import Mathlib.Algebra.Order.Ring.Int
99
import Mathlib.Data.Int.Cast.Defs
1010
import Mathlib.Data.Rat.Init
@@ -44,6 +44,8 @@ theorem pos (a : ℚ) : 0 < a.den := Nat.pos_of_ne_zero a.den_nz
4444

4545
#align rat.of_int Rat.ofInt
4646

47+
lemma mk'_num_den (q : ℚ) : mk' q.num q.den q.den_nz q.reduced = q := rfl
48+
4749
@[simp]
4850
theorem ofInt_eq_cast (n : ℤ) : ofInt n = Int.cast n :=
4951
rfl
@@ -218,6 +220,12 @@ lemma divInt_mul_divInt' (n₁ d₁ n₂ d₂ : ℤ) : (n₁ /. d₁) * (n₂ /.
218220

219221
attribute [simp] mkRat_mul_mkRat
220222

223+
lemma mk'_mul_mk' (n₁ n₂ : ℤ) (d₁ d₂ : ℕ) (hd₁ hd₂ hnd₁ hnd₂) (h₁₂ : n₁.natAbs.Coprime d₂)
224+
(h₂₁ : n₂.natAbs.Coprime d₁) :
225+
mk' n₁ d₁ hd₁ hnd₁ * mk' n₂ d₂ hd₂ hnd₂ = mk' (n₁ * n₂) (d₁ * d₂) (Nat.mul_ne_zero hd₁ hd₂) (by
226+
rw [Int.natAbs_mul]; exact (hnd₁.mul h₂₁).mul_right (h₁₂.mul hnd₂)) := by
227+
rw [mul_def]; dsimp; rw [mk_eq_normalize]
228+
221229
lemma mul_eq_mkRat (q r : ℚ) : q * r = mkRat (q.num * r.num) (q.den * r.den) := by
222230
rw [mul_def, normalize_eq_mkRat]
223231

@@ -227,6 +235,28 @@ alias divInt_eq_divInt := divInt_eq_iff
227235
@[deprecated] alias mul_num_den := mul_eq_mkRat
228236
#align rat.mul_num_denom Rat.mul_eq_mkRat
229237

238+
instance instPowNat : Pow ℚ ℕ where
239+
pow q n := ⟨q.num ^ n, q.den ^ n, by simp [Nat.pow_eq_zero], by
240+
rw [Int.natAbs_pow]; exact q.reduced.pow _ _⟩
241+
242+
lemma pow_def (q : ℚ) (n : ℕ) :
243+
q ^ n = ⟨q.num ^ n, q.den ^ n,
244+
by simp [Nat.pow_eq_zero],
245+
by rw [Int.natAbs_pow]; exact q.reduced.pow _ _⟩ := rfl
246+
247+
lemma pow_eq_mkRat (q : ℚ) (n : ℕ) : q ^ n = mkRat (q.num ^ n) (q.den ^ n) := by
248+
rw [pow_def, mk_eq_mkRat]
249+
250+
lemma pow_eq_divInt (q : ℚ) (n : ℕ) : q ^ n = q.num ^ n /. q.den ^ n := by
251+
rw [pow_def, mk_eq_divInt, Int.natCast_pow]
252+
253+
@[simp] lemma num_pow (q : ℚ) (n : ℕ) : (q ^ n).num = q.num ^ n := rfl
254+
@[simp] lemma den_pow (q : ℚ) (n : ℕ) : (q ^ n).den = q.den ^ n := rfl
255+
256+
@[simp] lemma mk'_pow (num : ℤ) (den : ℕ) (hd hdn) (n : ℕ) :
257+
mk' num den hd hdn ^ n = mk' (num ^ n) (den ^ n)
258+
(by simp [Nat.pow_eq_zero, hd]) (by rw [Int.natAbs_pow]; exact hdn.pow _ _) := rfl
259+
230260
#align rat.inv Rat.inv
231261

232262
instance : Inv ℚ :=
@@ -350,6 +380,16 @@ instance commRing : CommRing ℚ where
350380
one_mul := Rat.one_mul
351381
mul_comm := Rat.mul_comm
352382
mul_assoc := Rat.mul_assoc
383+
npow n q := q ^ n
384+
npow_zero := by intros; apply Rat.ext <;> simp
385+
npow_succ n q := by
386+
dsimp
387+
rw [← q.mk'_num_den, mk'_pow, mk'_mul_mk']
388+
congr
389+
· rw [mk'_pow, Int.natAbs_pow]
390+
exact q.reduced.pow_left _
391+
· rw [mk'_pow]
392+
exact q.reduced.pow_right _
353393
zero_mul := Rat.zero_mul
354394
mul_zero := Rat.mul_zero
355395
left_distrib := Rat.mul_add
@@ -481,6 +521,9 @@ protected theorem add_divInt (a b c : ℤ) : (a + b) /. c = a /. c + b /. c :=
481521
theorem divInt_eq_div (n d : ℤ) : n /. d = (n : ℚ) / d := by simp [div_def']
482522
#align rat.mk_eq_div Rat.divInt_eq_div
483523

524+
lemma intCast_div_eq_divInt (n d : ℤ) : (n : ℚ) / (d) = n /. d := by rw [divInt_eq_div]
525+
#align rat.coe_int_div_eq_mk Rat.intCast_div_eq_divInt
526+
484527
theorem divInt_mul_divInt_cancel {x : ℤ} (hx : x ≠ 0) (n d : ℤ) : n /. x * (x /. d) = n /. d := by
485528
by_cases hd : d = 0
486529
· rw [hd]
@@ -498,16 +541,17 @@ theorem divInt_div_divInt_cancel_right {x : ℤ} (hx : x ≠ 0) (n d : ℤ) :
498541
rw [div_eq_mul_inv, inv_divInt', mul_comm, divInt_mul_divInt_cancel hx]
499542
#align rat.mk_div_mk_cancel_right Rat.divInt_div_divInt_cancel_right
500543

501-
theorem intCast_div_eq_divInt {n d : ℤ} : (n : ℚ) / (d) = n /. d := by
502-
repeat rw [intCast_eq_divInt]
503-
exact divInt_div_divInt_cancel_left one_ne_zero n d
504-
#align rat.coe_int_div_eq_mk Rat.intCast_div_eq_divInt
505-
506544
-- Porting note: see porting note above about `Int.cast`@[simp]
507545
theorem num_div_den (r : ℚ) : (r.num : ℚ) / (r.den : ℚ) = r := by
508546
rw [← Int.cast_natCast]; erw [← divInt_eq_div, num_divInt_den]
509547
#align rat.num_div_denom Rat.num_div_den
510548

549+
@[simp] lemma divInt_pow (num : ℕ) (den : ℤ) (n : ℕ) : (num /. den) ^ n = num ^ n /. den ^ n := by
550+
simp [divInt_eq_div, div_pow]
551+
552+
@[simp] lemma mkRat_pow (num den : ℕ) (n : ℕ) : mkRat num den ^ n = mkRat (num ^ n) (den ^ n) := by
553+
rw [mkRat_eq_divInt, mkRat_eq_divInt, divInt_pow, Int.natCast_pow]
554+
511555
theorem coe_int_num_of_den_eq_one {q : ℚ} (hq : q.den = 1) : (q.num : ℚ) = q := by
512556
conv_rhs => rw [← num_divInt_den q, hq]
513557
rw [intCast_eq_divInt]

Mathlib/NumberTheory/DiophantineApproximation.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ theorem exists_rat_abs_sub_le_and_den_le (ξ : ℝ) {n : ℕ} (n_pos : 0 < n) :
156156
have hk₀' : (0 : ℝ) < k := Int.cast_pos.mpr hk₀
157157
have hden : ((j / k : ℚ).den : ℤ) ≤ k := by
158158
convert le_of_dvd hk₀ (Rat.den_dvd j k)
159-
exact Rat.intCast_div_eq_divInt
159+
exact Rat.intCast_div_eq_divInt _ _
160160
refine' ⟨j / k, _, Nat.cast_le.mp (hden.trans hk₁)⟩
161161
rw [← div_div, le_div_iff (Nat.cast_pos.mpr <| Rat.pos _ : (0 : ℝ) < _)]
162162
refine' (mul_le_mul_of_nonneg_left (Int.cast_le.mpr hden : _ ≤ (k : ℝ)) (abs_nonneg _)).trans _

0 commit comments

Comments
 (0)