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

Commit 2af0836

Browse files
committed
refactor(number_theory/zsqrtd): replace zsqrtd.conj with star (#18572)
This allows more existing lemmas to be used; notably, `unitary (zqsrt d)` becomes something we can talk about.
1 parent c985ae9 commit 2af0836

3 files changed

Lines changed: 36 additions & 46 deletions

File tree

src/number_theory/pell_matiyasevic.lean

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Released under Apache 2.0 license as described in the file LICENSE.
44
Authors: Mario Carneiro
55
-/
66

7+
import algebra.star.unitary
78
import data.nat.modeq
89
import number_theory.zsqrtd.basic
910

@@ -121,15 +122,18 @@ theorem is_pell_nat {x y : ℕ} : is_pell ⟨x, y⟩ ↔ x*x - d*y*y = 1 :=
121122
λh, show ((x*x : ℕ) - (d*y*y:ℕ) : ℤ) = 1,
122123
by rw [← int.coe_nat_sub $ le_of_lt $ nat.lt_of_sub_eq_succ h, h]; refl⟩
123124

124-
theorem is_pell_norm : Π {b : ℤ√d}, is_pell b ↔ b * b.conj = 1
125+
theorem is_pell_norm : Π {b : ℤ√d}, is_pell b ↔ b * star b = 1
125126
| ⟨x, y⟩ := by simp [zsqrtd.ext, is_pell, mul_comm]; ring_nf
126127

128+
theorem is_pell_iff_mem_unitary : Π {b : ℤ√d}, is_pell b ↔ b ∈ unitary ℤ√d
129+
| ⟨x, y⟩ := by rw [unitary.mem_iff, is_pell_norm, mul_comm (star _), and_self]
130+
127131
theorem is_pell_mul {b c : ℤ√d} (hb : is_pell b) (hc : is_pell c) : is_pell (b * c) :=
128132
is_pell_norm.2 (by simp [mul_comm, mul_left_comm,
129-
zsqrtd.conj_mul, pell.is_pell_norm.1 hb, pell.is_pell_norm.1 hc])
133+
star_mul, pell.is_pell_norm.1 hb, pell.is_pell_norm.1 hc])
130134

131-
theorem is_pell_conj : ∀ {b : ℤ√d}, is_pell b ↔ is_pell b.conj | ⟨x, y⟩ :=
132-
by simp [is_pell, zsqrtd.conj]
135+
theorem is_pell_star : ∀ {b : ℤ√d}, is_pell b ↔ is_pell (star b) | ⟨x, y⟩ :=
136+
by simp [is_pell, zsqrtd.star_mk]
133137

134138
@[simp] theorem pell_zd_succ (n : ℕ) : pell_zd (n+1) = pell_zd n * ⟨a, 1⟩ :=
135139
by simp [zsqrtd.ext]
@@ -187,7 +191,7 @@ lemma eq_pell_lem : ∀n (b:ℤ√d), 1 ≤ b → is_pell b → b ≤ pell_zd n
187191
if ha : (⟨↑a, 1⟩ : ℤ√d) ≤ b then
188192
let ⟨m, e⟩ := eq_pell_lem n (b * ⟨a, -1⟩)
189193
(by rw ← a1m; exact mul_le_mul_of_nonneg_right ha am1p)
190-
(is_pell_mul hp (is_pell_conj.1 is_pell_one))
194+
(is_pell_mul hp (is_pell_star.1 is_pell_one))
191195
(by have t := mul_le_mul_of_nonneg_right h am1p;
192196
rwa [pell_zd_succ, mul_assoc, a1m, mul_one] at t) in
193197
⟨m+1, by rw [show b = b * ⟨a, -1⟩ * ⟨a, 1⟩, by rw [mul_assoc, eq.trans (mul_comm _ _) a1m];
@@ -245,7 +249,7 @@ by injection (pell_zd_add _ m n) with _ h;
245249
repeat {rw ← int.coe_nat_add at h <|> rw ← int.coe_nat_mul at h};
246250
exact int.coe_nat_inj h
247251

248-
theorem pell_zd_sub {m n} (h : n ≤ m) : pell_zd (m - n) = pell_zd m * (pell_zd n).conj :=
252+
theorem pell_zd_sub {m n} (h : n ≤ m) : pell_zd (m - n) = pell_zd m * star (pell_zd n) :=
249253
let t := pell_zd_add n (m - n) in
250254
by rw [add_tsub_cancel_of_le h] at t;
251255
rw [t, mul_comm (pell_zd _ n) _, mul_assoc, (is_pell_norm _).1 (is_pell_pell_zd _ _), mul_one]

src/number_theory/zsqrtd/basic.lean

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -122,33 +122,16 @@ instance : ring ℤ√d := by apply_instance
122122
instance : distrib ℤ√d := by apply_instance
123123

124124
/-- Conjugation in `ℤ√d`. The conjugate of `a + b √d` is `a - b √d`. -/
125-
def conj (z : ℤ√d) : ℤ√d := ⟨z.1, -z.2
126-
@[simp] lemma conj_re (z : ℤ√d) : (conj z).re = z.re := rfl
127-
@[simp] lemma conj_im (z : ℤ√d) : (conj z).im = -z.im := rfl
125+
instance : has_star ℤ√d :=
126+
{ star := λ z, ⟨z.1, -z.2⟩ }
127+
@[simp] lemma star_mk (x y : ℤ) : star (⟨x, y⟩ : ℤ√d) = ⟨x, -y⟩ := rfl
128+
@[simp] lemma star_re (z : ℤ√d) : (star z).re = z.re := rfl
129+
@[simp] lemma star_im (z : ℤ√d) : (star z).im = -z.im := rfl
128130

129-
/-- `conj` as an `add_monoid_hom`. -/
130-
def conj_hom : ℤ√d →+ ℤ√d :=
131-
{ to_fun := conj,
132-
map_add' := λ ⟨a, ai⟩ ⟨b, bi⟩, ext.mpr ⟨rfl, neg_add _ _⟩,
133-
map_zero' := ext.mpr ⟨rfl, neg_zero⟩ }
134-
135-
@[simp] lemma conj_zero : conj (0 : ℤ√d) = 0 :=
136-
conj_hom.map_zero
137-
138-
@[simp] lemma conj_one : conj (1 : ℤ√d) = 1 :=
139-
by simp only [zsqrtd.ext, zsqrtd.conj_re, zsqrtd.conj_im, zsqrtd.one_im, neg_zero, eq_self_iff_true,
140-
and_self]
141-
142-
@[simp] lemma conj_neg (x : ℤ√d) : (-x).conj = -x.conj := rfl
143-
144-
@[simp] lemma conj_add (x y : ℤ√d) : (x + y).conj = x.conj + y.conj :=
145-
conj_hom.map_add x y
146-
147-
@[simp] lemma conj_sub (x y : ℤ√d) : (x - y).conj = x.conj - y.conj :=
148-
conj_hom.map_sub x y
149-
150-
@[simp] lemma conj_conj {d : ℤ} (x : ℤ√d) : x.conj.conj = x :=
151-
by simp only [ext, true_and, conj_re, eq_self_iff_true, neg_neg, conj_im]
131+
instance : star_ring ℤ√d :=
132+
{ star_involutive := λ x, ext.mpr ⟨rfl, neg_neg _⟩,
133+
star_mul := λ a b, ext.mpr ⟨by simp; ring, by simp; ring⟩,
134+
star_add := λ a b, ext.mpr ⟨rfl, neg_add _ _⟩ }
152135

153136
instance : nontrivial ℤ√d :=
154137
⟨⟨0, 1, dec_trivial⟩⟩
@@ -188,12 +171,9 @@ by simp [ext]
188171
theorem decompose {x y : ℤ} : (⟨x, y⟩ : ℤ√d) = x + sqrtd * y :=
189172
by simp [ext]
190173

191-
theorem mul_conj {x y : ℤ} : (⟨x, y⟩ * conj ⟨x, y⟩ : ℤ√d) = x * x - d * y * y :=
174+
theorem mul_star {x y : ℤ} : (⟨x, y⟩ * star ⟨x, y⟩ : ℤ√d) = x * x - d * y * y :=
192175
by simp [ext, sub_eq_add_neg, mul_comm]
193176

194-
theorem conj_mul {a b : ℤ√d} : conj (a * b) = conj a * conj b :=
195-
by { simp [ext], ring }
196-
197177
protected lemma coe_int_add (m n : ℤ) : (↑(m + n) : ℤ√d) = ↑m + ↑n :=
198178
(int.cast_ring_hom _).map_add _ _
199179
protected lemma coe_int_sub (m n : ℤ) : (↑(m - n) : ℤ√d) = ↑m - ↑n :=
@@ -376,15 +356,15 @@ def norm_monoid_hom : ℤ√d →* ℤ :=
376356
map_mul' := norm_mul,
377357
map_one' := norm_one }
378358

379-
lemma norm_eq_mul_conj (n : ℤ√d) : (norm n : ℤ√d) = n * n.conj :=
380-
by cases n; simp [norm, conj, zsqrtd.ext, mul_comm, sub_eq_add_neg]
359+
lemma norm_eq_mul_conj (n : ℤ√d) : (norm n : ℤ√d) = n * star n :=
360+
by cases n; simp [norm, star, zsqrtd.ext, mul_comm, sub_eq_add_neg]
381361

382362
@[simp] lemma norm_neg (x : ℤ√d) : (-x).norm = x.norm :=
383-
coe_int_inj $ by simp only [norm_eq_mul_conj, conj_neg, neg_mul,
363+
coe_int_inj $ by simp only [norm_eq_mul_conj, star_neg, neg_mul,
384364
mul_neg, neg_neg]
385365

386-
@[simp] lemma norm_conj (x : ℤ√d) : x.conj.norm = x.norm :=
387-
coe_int_inj $ by simp only [norm_eq_mul_conj, conj_conj, mul_comm]
366+
@[simp] lemma norm_conj (x : ℤ√d) : (star x).norm = x.norm :=
367+
coe_int_inj $ by simp only [norm_eq_mul_conj, star_star, mul_comm]
388368

389369
lemma norm_nonneg (hd : d ≤ 0) (n : ℤ√d) : 0 ≤ n.norm :=
390370
add_nonneg (mul_self_nonneg _)
@@ -394,10 +374,10 @@ add_nonneg (mul_self_nonneg _)
394374
lemma norm_eq_one_iff {x : ℤ√d} : x.norm.nat_abs = 1 ↔ is_unit x :=
395375
⟨λ h, is_unit_iff_dvd_one.2 $
396376
(le_total 0 (norm x)).cases_on
397-
(λ hx, show x ∣ 1, fromx.conj,
377+
(λ hx, show x ∣ 1, fromstar x,
398378
by rwa [← int.coe_nat_inj', int.nat_abs_of_nonneg hx,
399379
← @int.cast_inj (ℤ√d) _ _, norm_eq_mul_conj, eq_comm] at h⟩)
400-
(λ hx, show x ∣ 1, from ⟨- x.conj,
380+
(λ hx, show x ∣ 1, from ⟨- star x,
401381
by rwa [← int.coe_nat_inj', int.of_nat_nat_abs_of_nonpos hx,
402382
← @int.cast_inj (ℤ√d) _ _, int.cast_neg, norm_eq_mul_conj, neg_mul_eq_mul_neg,
403383
eq_comm] at h⟩),

src/number_theory/zsqrtd/gaussian_int.lean

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ and definitions about `zsqrtd` can easily be used.
3636
-/
3737

3838
open zsqrtd complex
39+
open_locale complex_conjugate
3940

4041
/-- The Gaussian integers, defined as `ℤ√(-1)`. -/
4142
@[reducible] def gaussian_int : Type := zsqrtd (-1)
@@ -75,6 +76,11 @@ by apply complex.ext; simp [to_complex_def]
7576
@[simp] lemma to_complex_zero : ((0 : ℤ[i]) : ℂ) = 0 := to_complex.map_zero
7677
@[simp] lemma to_complex_neg (x : ℤ[i]) : ((-x : ℤ[i]) : ℂ) = -x := to_complex.map_neg _
7778
@[simp] lemma to_complex_sub (x y : ℤ[i]) : ((x - y : ℤ[i]) : ℂ) = x - y := to_complex.map_sub _ _
79+
@[simp] lemma to_complex_star (x : ℤ[i]) : ((star x : ℤ[i]) : ℂ) = conj (x : ℂ) :=
80+
begin
81+
rw [to_complex_def₂, to_complex_def₂],
82+
exact congr_arg2 _ rfl (int.cast_neg _),
83+
end
7884

7985
@[simp] lemma to_complex_inj {x y : ℤ[i]} : (x : ℂ) = y ↔ x = y :=
8086
by cases x; cases y; simp [to_complex_def₂]
@@ -108,11 +114,11 @@ lemma nat_abs_norm_eq (x : ℤ[i]) : x.norm.nat_abs =
108114
int.coe_nat_inj $ begin simp, simp [zsqrtd.norm] end
109115

110116
instance : has_div ℤ[i] :=
111-
⟨λ x y, let n := (norm y : ℚ)⁻¹, c := y.conj in
117+
⟨λ x y, let n := (norm y : ℚ)⁻¹, c := star y in
112118
⟨round ((x * c).re * n : ℚ), round ((x * c).im * n : ℚ)⟩⟩
113119

114-
lemma div_def (x y : ℤ[i]) : x / y = ⟨round ((x * conj y).re / norm y : ℚ),
115-
round ((x * conj y).im / norm y : ℚ)⟩ :=
120+
lemma div_def (x y : ℤ[i]) : x / y = ⟨round ((x * star y).re / norm y : ℚ),
121+
round ((x * star y).im / norm y : ℚ)⟩ :=
116122
show zsqrtd.mk _ _ = _, by simp [div_eq_mul_inv]
117123

118124
lemma to_complex_div_re (x y : ℤ[i]) : ((x / y : ℤ[i]) : ℂ).re = round ((x / y : ℂ).re) :=

0 commit comments

Comments
 (0)