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

Commit 0f5a1f2

Browse files
author
Jon Eugster
committed
feat(data/rat): Add some lemmas to work with num/denom (#14456)
1 parent 95da649 commit 0f5a1f2

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

src/data/rat/basic.lean

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,26 @@ begin
661661
simp [division_def, coe_int_eq_mk, mul_def one_ne_zero d0]
662662
end
663663

664+
lemma mk_mul_mk_cancel {x : ℤ} (hx : x ≠ 0) (n d : ℤ) : (n /. x) * (x /. d) = n /. d :=
665+
begin
666+
by_cases hd : d = 0,
667+
{ rw hd,
668+
simp},
669+
rw [mul_def hx hd, mul_comm x, div_mk_div_cancel_left hx],
670+
end
671+
672+
lemma mk_div_mk_cancel_left {x : ℤ} (hx : x ≠ 0) (n d : ℤ) : (n /. x) / (d /. x) = n /. d :=
673+
by rw [div_eq_mul_inv, inv_def, mk_mul_mk_cancel hx]
674+
675+
lemma mk_div_mk_cancel_right {x : ℤ} (hx : x ≠ 0) (n d : ℤ) : (x /. n) / (x /. d) = d /. n :=
676+
by rw [div_eq_mul_inv, inv_def, mul_comm, mk_mul_mk_cancel hx]
677+
678+
@[simp] lemma coe_int_div_eq_mk {n d : ℤ} : (n : ℚ) / ↑d = n /. d :=
679+
begin
680+
repeat {rw coe_int_eq_mk},
681+
exact mk_div_mk_cancel_left one_ne_zero n d,
682+
end
683+
664684
@[simp]
665685
theorem num_div_denom (r : ℚ) : (r.num / r.denom : ℚ) = r :=
666686
by rw [← int.cast_coe_nat, ← mk_eq_div, num_denom]
@@ -672,6 +692,57 @@ begin
672692
exact rat.num_denom_mk d_ne_zero this
673693
end
674694

695+
lemma mul_num_denom' (q r : ℚ) :
696+
(q * r).num * q.denom * r.denom = q.num * r.num * (q * r).denom :=
697+
begin
698+
let s := (q.num * r.num) /. (q.denom * r.denom : ℤ),
699+
have hs : (q.denom * r.denom : ℤ) ≠ 0 := int.coe_nat_ne_zero_iff_pos.mpr (mul_pos q.pos r.pos),
700+
obtain ⟨c, ⟨c_mul_num, c_mul_denom⟩⟩ :=
701+
exists_eq_mul_div_num_and_eq_mul_div_denom (q.num * r.num) hs,
702+
rw [c_mul_num, mul_assoc, mul_comm],
703+
nth_rewrite 0 c_mul_denom,
704+
repeat {rw mul_assoc},
705+
apply mul_eq_mul_left_iff.2,
706+
rw or_iff_not_imp_right,
707+
intro c_pos,
708+
have h : _ = s := @mul_def q.num q.denom r.num r.denom
709+
(int.coe_nat_ne_zero_iff_pos.mpr q.pos)
710+
(int.coe_nat_ne_zero_iff_pos.mpr r.pos),
711+
rw [num_denom, num_denom] at h,
712+
rw h,
713+
rw mul_comm,
714+
apply rat.eq_iff_mul_eq_mul.mp,
715+
rw ←mk_eq_div,
716+
end
717+
718+
lemma add_num_denom' (q r : ℚ) :
719+
(q + r).num * q.denom * r.denom = (q.num * r.denom + r.num * q.denom) * (q + r).denom :=
720+
begin
721+
let s := mk (q.num * r.denom + r.num * q.denom) (q.denom * r.denom : ℤ),
722+
have hs : (q.denom * r.denom : ℤ) ≠ 0 := int.coe_nat_ne_zero_iff_pos.mpr (mul_pos q.pos r.pos),
723+
obtain ⟨c, ⟨c_mul_num, c_mul_denom⟩⟩ := exists_eq_mul_div_num_and_eq_mul_div_denom
724+
(q.num * r.denom + r.num * q.denom) hs,
725+
rw [c_mul_num, mul_assoc, mul_comm],
726+
nth_rewrite 0 c_mul_denom,
727+
repeat {rw mul_assoc},
728+
apply mul_eq_mul_left_iff.2,
729+
rw or_iff_not_imp_right,
730+
intro c_pos,
731+
have h : _ = s := @add_def q.num q.denom r.num r.denom
732+
(int.coe_nat_ne_zero_iff_pos.mpr q.pos)
733+
(int.coe_nat_ne_zero_iff_pos.mpr r.pos),
734+
rw [num_denom, num_denom] at h,
735+
rw h,
736+
rw mul_comm,
737+
apply rat.eq_iff_mul_eq_mul.mp,
738+
rw ←mk_eq_div,
739+
end
740+
741+
lemma substr_num_denom' (q r : ℚ) :
742+
(q - r).num * q.denom * r.denom = (q.num * r.denom - r.num * q.denom) * (q - r).denom :=
743+
by rw [sub_eq_add_neg, sub_eq_add_neg, ←neg_mul, ←num_neg_eq_neg_num, ←denom_neg_eq_denom r,
744+
add_num_denom' q (-r)]
745+
675746
theorem coe_int_eq_of_int (z : ℤ) : ↑z = of_int z :=
676747
(coe_int_eq_mk z).trans (of_int_eq_mk z).symm
677748

0 commit comments

Comments
 (0)