Skip to content

Commit 4acdb59

Browse files
int-y1Komyyy
andcommitted
feat: port Data.Real.GoldenRatio (#4332)
Co-authored-by: Komyyy <pol_tta@outlook.jp>
1 parent 15f033d commit 4acdb59

File tree

2 files changed

+238
-0
lines changed

2 files changed

+238
-0
lines changed

Mathlib.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,6 +1392,7 @@ import Mathlib.Data.Real.ConjugateExponents
13921392
import Mathlib.Data.Real.ENNReal
13931393
import Mathlib.Data.Real.ENatENNReal
13941394
import Mathlib.Data.Real.EReal
1395+
import Mathlib.Data.Real.GoldenRatio
13951396
import Mathlib.Data.Real.Hyperreal
13961397
import Mathlib.Data.Real.Irrational
13971398
import Mathlib.Data.Real.NNReal

Mathlib/Data/Real/GoldenRatio.lean

Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
/-
2+
Copyright (c) 2020 Anatole Dedecker. All rights reserved.
3+
Released under Apache 2.0 license as described in the file LICENSE.
4+
Authors: Anatole Dedecker, Alexey Soloyev, Junyan Xu
5+
6+
! This file was ported from Lean 3 source module data.real.golden_ratio
7+
! leanprover-community/mathlib commit 2196ab363eb097c008d4497125e0dde23fb36db2
8+
! Please do not edit these lines, except to modify the commit id
9+
! if you have ported upstream changes.
10+
-/
11+
import Mathlib.Data.Real.Irrational
12+
import Mathlib.Data.Nat.Fib
13+
import Mathlib.Data.Nat.PrimeNormNum
14+
import Mathlib.Data.Fin.VecNotation
15+
import Mathlib.Algebra.LinearRecurrence
16+
17+
/-!
18+
# The golden ratio and its conjugate
19+
20+
This file defines the golden ratio `φ := (1 + √5)/2` and its conjugate
21+
`ψ := (1 - √5)/2`, which are the two real roots of `X² - X - 1`.
22+
23+
Along with various computational facts about them, we prove their
24+
irrationality, and we link them to the Fibonacci sequence by proving
25+
Binet's formula.
26+
-/
27+
28+
29+
noncomputable section
30+
31+
open Polynomial
32+
33+
/-- The golden ratio `φ := (1 + √5)/2`. -/
34+
@[reducible]
35+
def goldenRatio :=
36+
(1 + Real.sqrt 5) / 2
37+
#align golden_ratio goldenRatio
38+
39+
/-- The conjugate of the golden ratio `ψ := (1 - √5)/2`. -/
40+
@[reducible]
41+
def goldenConj :=
42+
(1 - Real.sqrt 5) / 2
43+
#align golden_conj goldenConj
44+
45+
@[inherit_doc goldenRatio] scoped[Real] notation "φ" => goldenRatio
46+
@[inherit_doc goldenConj] scoped[Real] notation "ψ" => goldenConj
47+
open Real
48+
49+
/-- The inverse of the golden ratio is the opposite of its conjugate. -/
50+
theorem inv_gold : φ⁻¹ = -ψ := by
51+
have : 1 + Real.sqrt 50 := ne_of_gt (add_pos (by norm_num) <| Real.sqrt_pos.mpr (by norm_num))
52+
field_simp [sub_mul, mul_add]
53+
norm_num
54+
#align inv_gold inv_gold
55+
56+
/-- The opposite of the golden ratio is the inverse of its conjugate. -/
57+
theorem inv_goldConj : ψ⁻¹ = -φ := by
58+
rw [inv_eq_iff_eq_inv, ← neg_inv, ← neg_eq_iff_eq_neg]
59+
exact inv_gold.symm
60+
#align inv_gold_conj inv_goldConj
61+
62+
@[simp]
63+
theorem gold_mul_goldConj : φ * ψ = -1 := by
64+
field_simp
65+
rw [← sq_sub_sq]
66+
norm_num
67+
#align gold_mul_gold_conj gold_mul_goldConj
68+
69+
@[simp]
70+
theorem goldConj_mul_gold : ψ * φ = -1 := by
71+
rw [mul_comm]
72+
exact gold_mul_goldConj
73+
#align gold_conj_mul_gold goldConj_mul_gold
74+
75+
@[simp]
76+
theorem gold_add_goldConj : φ + ψ = 1 := by
77+
rw [goldenRatio, goldenConj]
78+
ring
79+
#align gold_add_gold_conj gold_add_goldConj
80+
81+
theorem one_sub_goldConj : 1 - φ = ψ := by
82+
linarith [gold_add_goldConj]
83+
#align one_sub_gold_conj one_sub_goldConj
84+
85+
theorem one_sub_gold : 1 - ψ = φ := by
86+
linarith [gold_add_goldConj]
87+
#align one_sub_gold one_sub_gold
88+
89+
@[simp]
90+
theorem gold_sub_goldConj : φ - ψ = Real.sqrt 5 := by
91+
rw [goldenRatio, goldenConj]
92+
ring
93+
#align gold_sub_gold_conj gold_sub_goldConj
94+
95+
@[simp 1200]
96+
theorem gold_sq : φ ^ 2 = φ + 1 := by
97+
rw [goldenRatio, ← sub_eq_zero]
98+
ring_nf
99+
rw [Real.sq_sqrt] <;> norm_num
100+
#align gold_sq gold_sq
101+
102+
@[simp 1200]
103+
theorem goldConj_sq : ψ ^ 2 = ψ + 1 := by
104+
rw [goldenConj, ← sub_eq_zero]
105+
ring_nf
106+
rw [Real.sq_sqrt] <;> norm_num
107+
#align gold_conj_sq goldConj_sq
108+
109+
theorem gold_pos : 0 < φ :=
110+
mul_pos (by apply add_pos <;> norm_num) <| inv_pos.2 zero_lt_two
111+
#align gold_pos gold_pos
112+
113+
theorem gold_ne_zero : φ ≠ 0 :=
114+
ne_of_gt gold_pos
115+
#align gold_ne_zero gold_ne_zero
116+
117+
theorem one_lt_gold : 1 < φ := by
118+
refine' lt_of_mul_lt_mul_left _ (le_of_lt gold_pos)
119+
simp [← sq, gold_pos, zero_lt_one, - div_pow] -- Porting note: Added `- div_pow`
120+
#align one_lt_gold one_lt_gold
121+
122+
theorem goldConj_neg : ψ < 0 := by
123+
linarith [one_sub_goldConj, one_lt_gold]
124+
#align gold_conj_neg goldConj_neg
125+
126+
theorem goldConj_ne_zero : ψ ≠ 0 :=
127+
ne_of_lt goldConj_neg
128+
#align gold_conj_ne_zero goldConj_ne_zero
129+
130+
theorem neg_one_lt_goldConj : -1 < ψ := by
131+
rw [neg_lt, ← inv_gold]
132+
exact inv_lt_one one_lt_gold
133+
#align neg_one_lt_gold_conj neg_one_lt_goldConj
134+
135+
/-!
136+
## Irrationality
137+
-/
138+
139+
140+
/-- The golden ratio is irrational. -/
141+
theorem gold_irrational : Irrational φ := by
142+
have := Nat.Prime.irrational_sqrt (show Nat.Prime 5 by norm_num)
143+
have := this.rat_add 1
144+
have := this.rat_mul (show (0.5 : ℚ) ≠ 0 by norm_num)
145+
convert this
146+
norm_num
147+
field_simp
148+
#align gold_irrational gold_irrational
149+
150+
/-- The conjugate of the golden ratio is irrational. -/
151+
theorem goldConj_irrational : Irrational ψ := by
152+
have := Nat.Prime.irrational_sqrt (show Nat.Prime 5 by norm_num)
153+
have := this.rat_sub 1
154+
have := this.rat_mul (show (0.5 : ℚ) ≠ 0 by norm_num)
155+
convert this
156+
norm_num
157+
field_simp
158+
#align gold_conj_irrational goldConj_irrational
159+
160+
/-!
161+
## Links with Fibonacci sequence
162+
-/
163+
164+
165+
section Fibrec
166+
167+
variable {α : Type _} [CommSemiring α]
168+
169+
/-- The recurrence relation satisfied by the Fibonacci sequence. -/
170+
def fibRec : LinearRecurrence α where
171+
order := 2
172+
coeffs := ![1, 1]
173+
#align fib_rec fibRec
174+
175+
section Poly
176+
177+
open Polynomial
178+
179+
/-- The characteristic polynomial of `fibRec` is `X² - (X + 1)`. -/
180+
theorem fibRec_charPoly_eq {β : Type _} [CommRing β] :
181+
fibRec.charPoly = X ^ 2 - (X + (1 : β[X])) := by
182+
rw [fibRec, LinearRecurrence.charPoly]
183+
simp [Finset.sum_fin_eq_sum_range, Finset.sum_range_succ', ← smul_X_eq_monomial]
184+
#align fib_rec_char_poly_eq fibRec_charPoly_eq
185+
186+
end Poly
187+
188+
/-- As expected, the Fibonacci sequence is a solution of `fibRec`. -/
189+
theorem fib_isSol_fibRec : fibRec.IsSolution (fun x => x.fib : ℕ → α) := by
190+
rw [fibRec]
191+
intro n
192+
simp only
193+
rw [Nat.fib_add_two, add_comm]
194+
simp [Finset.sum_fin_eq_sum_range, Finset.sum_range_succ']
195+
#align fib_is_sol_fib_rec fib_isSol_fibRec
196+
197+
/-- The geometric sequence `fun n ↦ φ^n` is a solution of `fibRec`. -/
198+
theorem geom_gold_isSol_fibRec : fibRec.IsSolution (φ ^ ·) := by
199+
rw [fibRec.geom_sol_iff_root_charPoly, fibRec_charPoly_eq]
200+
simp [sub_eq_zero, - div_pow] -- Porting note: Added `- div_pow`
201+
#align geom_gold_is_sol_fib_rec geom_gold_isSol_fibRec
202+
203+
/-- The geometric sequence `fun n ↦ ψ^n` is a solution of `fibRec`. -/
204+
theorem geom_goldConj_isSol_fibRec : fibRec.IsSolution (ψ ^ ·) := by
205+
rw [fibRec.geom_sol_iff_root_charPoly, fibRec_charPoly_eq]
206+
simp [sub_eq_zero, - div_pow] -- Porting note: Added `- div_pow`
207+
#align geom_gold_conj_is_sol_fib_rec geom_goldConj_isSol_fibRec
208+
209+
end Fibrec
210+
211+
/-- Binet's formula as a function equality. -/
212+
theorem Real.coe_fib_eq' :
213+
(fun n => Nat.fib n : ℕ → ℝ) = fun n => (φ ^ n - ψ ^ n) / Real.sqrt 5 := by
214+
rw [fibRec.sol_eq_of_eq_init]
215+
· intro i hi
216+
norm_cast at hi
217+
fin_cases hi
218+
· simp
219+
· simp only [goldenRatio, goldenConj]
220+
ring_nf
221+
rw [mul_inv_cancel]; norm_num
222+
· exact fib_isSol_fibRec
223+
· -- Porting note: Rewrote this proof
224+
suffices LinearRecurrence.IsSolution fibRec
225+
((fun n ↦ (sqrt 5)⁻¹ * φ ^ n) - (fun n ↦ (sqrt 5)⁻¹ * ψ ^ n)) by
226+
convert this
227+
rw [Pi.sub_apply]
228+
ring
229+
apply (@fibRec ℝ _).solSpace.sub_mem
230+
· exact Submodule.smul_mem fibRec.solSpace (Real.sqrt 5)⁻¹ geom_gold_isSol_fibRec
231+
· exact Submodule.smul_mem fibRec.solSpace (Real.sqrt 5)⁻¹ geom_goldConj_isSol_fibRec
232+
#align real.coe_fib_eq' Real.coe_fib_eq'
233+
234+
/-- Binet's formula as a dependent equality. -/
235+
theorem Real.coe_fib_eq : ∀ n, (Nat.fib n : ℝ) = (φ ^ n - ψ ^ n) / Real.sqrt 5 := by
236+
rw [← Function.funext_iff, Real.coe_fib_eq']
237+
#align real.coe_fib_eq Real.coe_fib_eq

0 commit comments

Comments
 (0)