Mathlib.Data.PNat.Xgcd
83 declarations. Mathlib docs · Source
Euclidean algorithm for ℕ
This file sets up a version of the Euclidean algorithm that only works with natural numbers.
Given 0 < a, b, it computes the unique (w, x, y, z, d) such that the following identities hold:
a = (w + x) db = (y + z) dw * z = x * y + 1
d is then the gcd of a and b, and a' := a / d = w + x and b' := b / d = y + z are coprime.
This story is closely related to the structure of SL₂(ℕ) (as a free monoid on two generators) and the theory of continued fractions.
Main declarations
XgcdType: Helper type in defining the gcd. Encapsulates(wp, x, y, zp, ap, bp). wherewpzp,ap,bpare the variables getting changed through the algorithm.IsSpecial: Stateswp * zp = x * y + 1IsReduced: Statesap = a ∧ bp = b
Notes
See Nat.Xgcd for a very similar algorithm allowing values in ℤ.
theorem PNat.XgcdType.mk.inj E1 #
∀ {wp x y zp ap bp wp_1 x_1 y_1 zp_1 ap_1 bp_1 : ℕ},
{ wp := wp, x := x, y := y, zp := zp, ap := ap, bp := bp } =
{ wp := wp_1, x := x_1, y := y_1, zp := zp_1, ap := ap_1, bp := bp_1 } →
wp = wp_1 ∧ x = x_1 ∧ y = y_1 ∧ zp = zp_1 ∧ ap = ap_1 ∧ bp = bp_1
def PNat.XgcdType.ctorIdx E1 #
PNat.XgcdType → ℕ
theorem PNat.XgcdType.mk.injEq E0 #
∀ (wp x y zp ap bp wp_1 x_1 y_1 zp_1 ap_1 bp_1 : ℕ),
({ wp := wp, x := x, y := y, zp := zp, ap := ap, bp := bp } =
{ wp := wp_1, x := x_1, y := y_1, zp := zp_1, ap := ap_1, bp := bp_1 }) =
(wp = wp_1 ∧ x = x_1 ∧ y = y_1 ∧ zp = zp_1 ∧ ap = ap_1 ∧ bp = bp_1)
def PNat.instInhabitedXgcdType.default E1 #
PNat.XgcdType
structure PNat.XgcdType E3 #
Type
A term of XgcdType is a system of six naturals. They should
be thought of as representing the matrix
[[w, x], [y, z]] = [[wp + 1, x], [y, zp + 1]]
together with the vector [a, b] = [ap + 1, bp + 1].
ctor PNat.XgcdType.mk E0 #
ℕ → ℕ → ℕ → ℕ → ℕ → ℕ → PNat.XgcdType
abbrev PNat.XgcdType.wp E3 #
PNat.XgcdType → ℕ
wp is a variable which changes through the algorithm.
abbrev PNat.XgcdType.x E1 #
PNat.XgcdType → ℕ
x satisfies a / d = w + x at the final step.
abbrev PNat.XgcdType.y E1 #
PNat.XgcdType → ℕ
y satisfies b / d = z + y at the final step.
abbrev PNat.XgcdType.zp E3 #
PNat.XgcdType → ℕ
zp is a variable which changes through the algorithm.
abbrev PNat.XgcdType.ap E3 #
PNat.XgcdType → ℕ
ap is a variable which changes through the algorithm.
abbrev PNat.XgcdType.bp E3 #
PNat.XgcdType → ℕ
bp is a variable which changes through the algorithm.
instance PNat.instInhabitedXgcdType E0 #
Inhabited PNat.XgcdType
instance PNat.XgcdType.instSizeOf E0 #
SizeOf PNat.XgcdType
instance PNat.XgcdType.instRepr E0 #
Repr PNat.XgcdType
The Repr instance converts terms to strings in a way that
reflects the matrix/vector interpretation as above.
def PNat.XgcdType.mk' E1 #
ℕ+ → ℕ → ℕ → ℕ+ → ℕ+ → ℕ+ → PNat.XgcdType
Another mk using ℕ and ℕ+
def PNat.XgcdType.w E1 #
PNat.XgcdType → ℕ+
w = wp + 1
def PNat.XgcdType.z E1 #
PNat.XgcdType → ℕ+
z = zp + 1
def PNat.XgcdType.a E1 #
PNat.XgcdType → ℕ+
a = ap + 1
def PNat.XgcdType.b E1 #
PNat.XgcdType → ℕ+
b = bp + 1
def PNat.XgcdType.r E1 #
PNat.XgcdType → ℕ
r = a % b: remainder
def PNat.XgcdType.q E1 #
PNat.XgcdType → ℕ
q = ap / bp: quotient
def PNat.XgcdType.qp E1 #
PNat.XgcdType → ℕ
qp = q - 1
def PNat.XgcdType.vp E1 #
PNat.XgcdType → ℕ × ℕ
The map v gives the product of the matrix
[[w, x], [y, z]] = [[wp + 1, x], [y, zp + 1]]
and the vector [a, b] = [ap + 1, bp + 1]. The map
vp gives [sp, tp] such that v = [sp + 1, tp + 1].
def PNat.XgcdType.v E1 #
PNat.XgcdType → ℕ × ℕ
v = [sp + 1, tp + 1], check vp
def PNat.XgcdType.succ₂ E1 #
ℕ × ℕ → ℕ × ℕ
succ₂ [t.1, t.2] = [t.1.succ, t.2.succ]
theorem PNat.XgcdType.v_eq_succ_vp E1 #
∀ (u : PNat.XgcdType), u.v = PNat.XgcdType.succ₂ u.vp
def PNat.XgcdType.IsSpecial E3 #
PNat.XgcdType → Prop
IsSpecial holds if the matrix has determinant one.
def PNat.XgcdType.IsSpecial' E1 #
PNat.XgcdType → Prop
IsSpecial' is an alternative of IsSpecial.
theorem PNat.XgcdType.isSpecial_iff E0 #
∀ (u : PNat.XgcdType), u.IsSpecial ↔ u.IsSpecial'
def PNat.XgcdType.IsReduced E3 #
PNat.XgcdType → Prop
IsReduced holds if the two entries in the vector are the
same. The reduction algorithm will produce a system with this
property, whose product vector is the same as for the original
system.
def PNat.XgcdType.IsReduced' E1 #
PNat.XgcdType → Prop
IsReduced' is an alternative of IsReduced.
theorem PNat.XgcdType.isReduced_iff E0 #
∀ (u : PNat.XgcdType), u.IsReduced ↔ u.IsReduced'
def PNat.XgcdType.flip E1 #
PNat.XgcdType → PNat.XgcdType
flip flips the placement of variables during the algorithm.
theorem PNat.XgcdType.flip_w E0 #
∀ (u : PNat.XgcdType), u.flip.w = u.z
theorem PNat.XgcdType.flip_x E0 #
∀ (u : PNat.XgcdType), u.flip.x = u.y
theorem PNat.XgcdType.flip_y E0 #
∀ (u : PNat.XgcdType), u.flip.y = u.x
theorem PNat.XgcdType.flip_z E0 #
∀ (u : PNat.XgcdType), u.flip.z = u.w
theorem PNat.XgcdType.flip_a E0 #
∀ (u : PNat.XgcdType), u.flip.a = u.b
theorem PNat.XgcdType.flip_b E0 #
∀ (u : PNat.XgcdType), u.flip.b = u.a
theorem PNat.XgcdType.flip_isReduced E1 #
∀ (u : PNat.XgcdType), u.flip.IsReduced ↔ u.IsReduced
theorem PNat.XgcdType.flip_isSpecial E1 #
∀ (u : PNat.XgcdType), u.flip.IsSpecial ↔ u.IsSpecial
theorem PNat.XgcdType.flip_v E1 #
∀ (u : PNat.XgcdType), u.flip.v = u.v.swap
theorem PNat.XgcdType.rq_eq E0 #
∀ (u : PNat.XgcdType), u.r + (u.bp + 1) * u.q = u.ap + 1
Properties of division with remainder for a / b.
theorem PNat.XgcdType.qp_eq E0 #
∀ (u : PNat.XgcdType), u.r = 0 → u.q = u.qp + 1
def PNat.XgcdType.start E1 #
ℕ+ → ℕ+ → PNat.XgcdType
The following function provides the starting point for our algorithm. We will apply an iterative reduction process to it, which will produce a system satisfying IsReduced. The gcd can be read off from this final system.
theorem PNat.XgcdType.start_isSpecial E1 #
∀ (a b : ℕ+), (PNat.XgcdType.start a b).IsSpecial
theorem PNat.XgcdType.start_v E1 #
∀ (a b : ℕ+), (PNat.XgcdType.start a b).v = (↑a, ↑b)
def PNat.XgcdType.finish E1 #
PNat.XgcdType → PNat.XgcdType
finish happens when the reducing process ends.
theorem PNat.XgcdType.finish_isReduced E1 #
∀ (u : PNat.XgcdType), u.finish.IsReduced
theorem PNat.XgcdType.finish_isSpecial E1 #
∀ (u : PNat.XgcdType), u.IsSpecial → u.finish.IsSpecial
theorem PNat.XgcdType.finish_v E1 #
∀ (u : PNat.XgcdType), u.r = 0 → u.finish.v = u.v
def PNat.XgcdType.step E1 #
PNat.XgcdType → PNat.XgcdType
This is the main reduction step, which is used when u.r ≠ 0, or equivalently b does not divide a.
theorem PNat.XgcdType.step_wf E1 #
∀ (u : PNat.XgcdType), u.r ≠ 0 → sizeOf u.step < sizeOf u
We will apply the above step recursively. The following result is used to ensure that the process terminates.
theorem PNat.XgcdType.step_isSpecial E1 #
∀ (u : PNat.XgcdType), u.IsSpecial → u.step.IsSpecial
theorem PNat.XgcdType.step_v E1 #
∀ (u : PNat.XgcdType), u.r ≠ 0 → u.step.v = u.v.swap
The reduction step does not change the product vector.
def PNat.XgcdType.reduce E1 #
PNat.XgcdType → PNat.XgcdType
We can now define the full reduction function, which applies step as long as possible, and then applies finish. Note that the "have" statement puts a fact in the local context, and the equation compiler uses this fact to help construct the full definition in terms of well-founded recursion. The same fact needs to be introduced in all the inductive proofs of properties given below.
theorem PNat.XgcdType.reduce_a E1 #
∀ {u : PNat.XgcdType}, u.r = 0 → u.reduce = u.finish
theorem PNat.XgcdType.reduce_b E1 #
∀ {u : PNat.XgcdType}, u.r ≠ 0 → u.reduce = u.step.reduce.flip
theorem PNat.XgcdType.reduce_isReduced E1 #
∀ (u : PNat.XgcdType), u.reduce.IsReduced
theorem PNat.XgcdType.reduce_isReduced' E1 #
∀ (u : PNat.XgcdType), u.reduce.IsReduced'
theorem PNat.XgcdType.reduce_isSpecial E1 #
∀ (u : PNat.XgcdType), u.IsSpecial → u.reduce.IsSpecial
theorem PNat.XgcdType.reduce_isSpecial' E1 #
∀ (u : PNat.XgcdType), u.IsSpecial → u.reduce.IsSpecial'
theorem PNat.XgcdType.reduce_v E1 #
∀ (u : PNat.XgcdType), u.reduce.v = u.v
def PNat.xgcd E1 #
ℕ+ → ℕ+ → PNat.XgcdType
Extended Euclidean algorithm
def PNat.gcdD E1 #
ℕ+ → ℕ+ → ℕ+
gcdD a b = gcd a b
def PNat.gcdW E1 #
ℕ+ → ℕ+ → ℕ+
Final value of w
def PNat.gcdX E1 #
ℕ+ → ℕ+ → ℕ
Final value of x
def PNat.gcdY E1 #
ℕ+ → ℕ+ → ℕ
Final value of y
def PNat.gcdZ E1 #
ℕ+ → ℕ+ → ℕ+
Final value of z
def PNat.gcdA' E1 #
ℕ+ → ℕ+ → ℕ+
Final value of a / d
def PNat.gcdB' E1 #
ℕ+ → ℕ+ → ℕ+
Final value of b / d
theorem PNat.gcdA'_coe E0 #
∀ (a b : ℕ+), ↑(a.gcdA' b) = ↑(a.gcdW b) + a.gcdX b
theorem PNat.gcdB'_coe E0 #
∀ (a b : ℕ+), ↑(a.gcdB' b) = a.gcdY b + ↑(a.gcdZ b)
theorem PNat.gcd_props E1 #
∀ (a b : ℕ+),
have d := a.gcdD b;
have w := a.gcdW b;
have x := a.gcdX b;
have y := a.gcdY b;
have z := a.gcdZ b;
have a' := a.gcdA' b;
have b' := a.gcdB' b;
w * z = (x * y).succPNat ∧
a = a' * d ∧
b = b' * d ∧
z * a' = (x * ↑b').succPNat ∧ w * b' = (y * ↑a').succPNat ∧ ↑z * ↑a = x * ↑b + ↑d ∧ ↑w * ↑b = y * ↑a + ↑d
theorem PNat.gcd_eq E0 #
∀ (a b : ℕ+), a.gcdD b = a.gcd b
theorem PNat.gcd_det_eq E0 #
∀ (a b : ℕ+), a.gcdW b * a.gcdZ b = (a.gcdX b * a.gcdY b).succPNat
theorem PNat.gcd_a_eq E0 #
∀ (a b : ℕ+), a = a.gcdA' b * a.gcd b
theorem PNat.gcd_b_eq E0 #
∀ (a b : ℕ+), b = a.gcdB' b * a.gcd b
theorem PNat.gcd_rel_left' E1 #
∀ (a b : ℕ+), a.gcdZ b * a.gcdA' b = (a.gcdX b * ↑(a.gcdB' b)).succPNat
theorem PNat.gcd_rel_right' E1 #
∀ (a b : ℕ+), a.gcdW b * a.gcdB' b = (a.gcdY b * ↑(a.gcdA' b)).succPNat
theorem PNat.gcd_rel_left E1 #
∀ (a b : ℕ+), ↑(a.gcdZ b) * ↑a = a.gcdX b * ↑b + ↑(a.gcd b)
theorem PNat.gcd_rel_right E1 #
∀ (a b : ℕ+), ↑(a.gcdW b) * ↑b = a.gcdY b * ↑a + ↑(a.gcd b)