Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions InternalDocs/fractions.md
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,75 @@ require a potentially expensive `gcd` computation. We can use the
In addition to the above proof, a formal, computer-verified proof
of correctness (using Lean) of the algorithm can be found here:
https://github.com/mdickinson/snippets/tree/1f47f8cc33185c646b679647e88cb24f29fd6a4e/proofs/limit_denominator

# Algorithms for rational arithmetic

In following we assume that rational numbers $\frac{p}{q}$ are
represented by fractions in lowest terms. That is, $p$ and $q$
are coprime, $\gcd(p, q) = 1$. And, the denominator $q$ is positive.

Consider fractions $a/b$ and $c/d$.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, we should cite Knuth here as well as the code.

## Addition/subtraction

Let $g = \gcd(b, d)$. Then\
$$\frac{a}{b} \pm \frac{c}{d} = \frac{a d \pm b c}{b d}
=\frac{a \left(d/g\right) \pm c \left(b/g\right)}{\left(b d\right)/g}
=\frac{p}{q}$$

Now, if $g > 1$, we're working with smaller integers. Also,
$p$, $b/g$, $d/g$ are pairwise coprime.

Indeed, $b/g$ and $d/g$ share no common factors (they were
removed) and $b$ is coprime with $a$ (since input fractions are
in lowest terms), hence $b/g$ and $a$ are coprime. By symmetry,
$d/g$ and $c$ are coprime too. Then\
$$\gcd\left(p, b/g\right)
=\gcd\left(a \left(d/g\right) \pm c \left(b/g\right), b/g\right)
=\gcd\left(a \left(d/g\right), b/g\right) = 1$$

and similarly\
$$\gcd\left(p, d/g\right) = \gcd\left(c\left(b/g\right), d/g\right) = 1$$

The above allows us optimize reduction of the result to lowest
terms. Indeed,\
$$h = \gcd\left(p, q\right)
=\gcd\left(p, \left(b/g\right)\left(d/g\right) g\right)
=\gcd\left(p, g\right)$$

and\
$$\frac{a}{b}\pm\frac{c}{d}
=\frac{p/h}{\left(b/g\right)\left(d/g\right)\left(g/h\right)}
=\frac{\left(a \left(d/g\right) \pm c \left(b/g\right) \right)/h}
{\left(b/g\right)\left(d/h\right)}$$

is a normalized fraction. This is useful because the unnormalized
denominator $q$ could be much larger than $g$.

We should special-case $g = 1$ and $h = 1$, since 61% of randomly-chosen
integers [are
integers [are coprime](https://en.wikipedia.org/wiki/Coprime_integers#Probability_of_coprimality).
Note that second case always holds for fractions, obtained from floats: here
$g$ is a power of 2 and the unnormalized numerator $p$ is an odd integer.

## Multiplication

Let $g = \gcd(a, d)$ and $h = \gcd\left(b, c\right)$, then\
$$\frac{a}{b}\times\frac{c}{d}
=\frac{\left(a/g\right)\left(c/h\right)}
{\left(d/g\right)\left(b/h\right)}$$

Note that after divisions we're multiplying smaller integers.

Also, the resulting fraction is normalized, because each of
two factors in the numerator is coprime to each of the two factors
in the denominator.

Indeed, pick $a/g$. It's coprime with $b/h$, because input
fractions are in lowest terms. It's also coprime with $d/g$,
because common factors are removed by $g = \gcd\left(a, d\right)$.
By symmetry, $c/h$ is also coprime with both factors in the denominator.

As for addition/subtraction, we should special-case $g = 1$
and $h = 1$ for the same reason. That happens also for multiplying
rationals, obtained from floats.
70 changes: 3 additions & 67 deletions Lib/fractions.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,8 @@ def limit_denominator(self, max_denominator=1000000):
# approximation to x if, and only if, it is a convergent or
# semiconvergent of the (unique shortest) continued fraction
# associated to x. See e.g. Continued Fractions by A. Ya.
# Khinchin (1964), Theorem 15.
# Khinchin (1964), Theorem 15. See also
# InternalDocs/fractions.md for details.
#
# To find a best rational approximation with denominator <= M,
# we find the best upper and lower approximations with
Expand Down Expand Up @@ -725,72 +726,7 @@ def reverse(b, a):
return forward, reverse

# Rational arithmetic algorithms: Knuth, TAOCP, Volume 2, 4.5.1.
Comment thread
StanFromIreland marked this conversation as resolved.
Resolved
#
# Assume input fractions a and b are normalized.
#
# 1) Consider addition/subtraction.
#
# Let g = gcd(da, db). Then
#
# na nb na*db ± nb*da
# a ± b == -- ± -- == ------------- ==
# da db da*db
#
# na*(db//g) ± nb*(da//g) t
# == ----------------------- == -
# (da*db)//g d
#
# Now, if g > 1, we're working with smaller integers.
#
# Note, that t, (da//g) and (db//g) are pairwise coprime.
#
# Indeed, (da//g) and (db//g) share no common factors (they were
# removed) and da is coprime with na (since input fractions are
# normalized), hence (da//g) and na are coprime. By symmetry,
# (db//g) and nb are coprime too. Then,
#
# gcd(t, da//g) == gcd(na*(db//g), da//g) == 1
# gcd(t, db//g) == gcd(nb*(da//g), db//g) == 1
#
# Above allows us optimize reduction of the result to lowest
# terms. Indeed,
#
# g2 = gcd(t, d) == gcd(t, (da//g)*(db//g)*g) == gcd(t, g)
#
# t//g2 t//g2
# a ± b == ----------------------- == ----------------
# (da//g)*(db//g)*(g//g2) (da//g)*(db//g2)
#
# is a normalized fraction. This is useful because the unnormalized
# denominator d could be much larger than g.
#
# We should special-case g == 1 (and g2 == 1), since 60.8% of
# randomly-chosen integers are coprime:
# https://en.wikipedia.org/wiki/Coprime_integers#Probability_of_coprimality
# Note, that g2 == 1 always for fractions, obtained from floats: here
# g is a power of 2 and the unnormalized numerator t is an odd integer.
#
# 2) Consider multiplication
#
# Let g1 = gcd(na, db) and g2 = gcd(nb, da), then
#
# na*nb na*nb (na//g1)*(nb//g2)
# a*b == ----- == ----- == -----------------
# da*db db*da (db//g1)*(da//g2)
#
# Note, that after divisions we're multiplying smaller integers.
#
# Also, the resulting fraction is normalized, because each of
# two factors in the numerator is coprime to each of the two factors
# in the denominator.
#
# Indeed, pick (na//g1). It's coprime with (da//g2), because input
# fractions are normalized. It's also coprime with (db//g1), because
# common factors are removed by g1 == gcd(na, db).
#
# As for addition/subtraction, we should special-case g1 == 1
# and g2 == 1 for same reason. That happens also for multiplying
# rationals, obtained from floats.
# See also InternalDocs/fractions.md for details.

def _add(a, b):
"""a + b"""
Expand Down
Loading