Skip to content

Commit 52b42b2

Browse files
committed
Move internal docs for fractions module to InternalDocs/fractions.md
This covers algorithms for addition/subtraction/multiplication.
1 parent 22691a3 commit 52b42b2

2 files changed

Lines changed: 72 additions & 66 deletions

File tree

‎InternalDocs/fractions.md‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,3 +363,75 @@ require a potentially expensive `gcd` computation. We can use the
363363
In addition to the above proof, a formal, computer-verified proof
364364
of correctness (using Lean) of the algorithm can be found here:
365365
https://github.com/mdickinson/snippets/tree/1f47f8cc33185c646b679647e88cb24f29fd6a4e/proofs/limit_denominator
366+
367+
# Algorithms for rational arithmetics
368+
369+
In following we assume, that rational numbers $\frac{p}{q}$ are
370+
represented by fractions in lowest terms. I.e. $p$ and $q$
371+
are coprime, $\gcd(p, q) = 1$. Also, denominator $q$ is positive.
372+
373+
Consider fractions $a/b$ and $c/d$.
374+
375+
## Addition/subtraction
376+
377+
Let $g = \gcd(b, d)$. Then\
378+
$$\frac{a}{b} \pm \frac{c}{d} = \frac{a d \pm b c}{b d}
379+
=\frac{a \left(d/g\right) \pm c \left(b/g\right)}{\left(b d\right)/g}
380+
=\frac{p}{q}$$
381+
382+
Now, if $g > 1$, we're working with smaller integers. Also,
383+
$p$, $b/g$, $d/g$ are pairwise coprime.
384+
385+
Indeed, $b/g$ and $d/g$ share no common factors (they were
386+
removed) and $b$ is coprime with $a$ (since input fractions are
387+
in lowest terms), hence $b/g$ and $a$ are coprime. By symmetry,
388+
$d/g$ and $c$ are coprime too. Then\
389+
$$\gcd\left(p, b/g\right)
390+
=\gcd\left(a \left(d/g\right) \pm c \left(b/g\right), b/g\right)
391+
=\gcd\left(a \left(d/g\right), b/g\right) = 1$$
392+
393+
and similary\
394+
$$\gcd\left(p, d/g\right) = \gcd\left(c\left(b/g\right), d/g\right) = 1$$
395+
396+
Above allows us optimize reduction of the result to lowest
397+
terms. Indeed,\
398+
$$h = \gcd\left(p, q\right)
399+
=\gcd\left(p, \left(b/g\right)\left(d/g\right) g\right)
400+
=\gcd\left(p, g\right)$$
401+
402+
and\
403+
$$\frac{a}{b}\pm\frac{c}{d}
404+
=\frac{p/h}{\left(b/g\right)\left(d/g\right)\left(g/h\right)}
405+
=\frac{\left(a \left(d/g\right) \pm c \left(b/g\right) \right)/h}
406+
{\left(b/g\right)\left(d/h\right)}$$
407+
408+
is a normalized fraction. This is useful because the unnormalized
409+
denominator $q$ could be much larger than $g$.
410+
411+
We should special-case $g = 1$ and $h = 1$, since 61% of randomly-chosen
412+
integers [are
413+
coprime](https://en.wikipedia.org/wiki/Coprime_integers#Probability_of_coprimality).
414+
Note, that second case always holds for fractions, obtained from floats: here
415+
$g$ is a power of 2 and the unnormalized numerator $p$ is an odd integer.
416+
417+
## Multiplication
418+
419+
Let $g = \gcd(a, d)$ and $h = \gcd\left(b, c\right)$, then\
420+
$$\frac{a}{b}\times\frac{c}{d}
421+
=\frac{\left(a/g\right)\left(c/h\right)}
422+
{\left(d/g\right)\left(b/h\right)}$$
423+
424+
Note, that after divisions we're multiplying smaller integers.
425+
426+
Also, the resulting fraction is normalized, because each of
427+
two factors in the numerator is coprime to each of the two factors
428+
in the denominator.
429+
430+
Indeed, pick $a/g$. It's coprime with $b/h$, because input
431+
fractions are in lowest terms. It's also coprime with $d/g$,
432+
because common factors are removed by $g = \gcd\left(a, d\right)$.
433+
By symmetry, $c/h$ also coprime with both factors in the denominator.
434+
435+
As for addition/subtraction, we should special-case $g = 1$
436+
and $h = 1$ for same reason. That happens also for multiplying
437+
rationals, obtained from floats.

‎Lib/fractions.py‎

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -725,72 +725,6 @@ def reverse(b, a):
725725
return forward, reverse
726726

727727
# Rational arithmetic algorithms: Knuth, TAOCP, Volume 2, 4.5.1.
728-
#
729-
# Assume input fractions a and b are normalized.
730-
#
731-
# 1) Consider addition/subtraction.
732-
#
733-
# Let g = gcd(da, db). Then
734-
#
735-
# na nb na*db ± nb*da
736-
# a ± b == -- ± -- == ------------- ==
737-
# da db da*db
738-
#
739-
# na*(db//g) ± nb*(da//g) t
740-
# == ----------------------- == -
741-
# (da*db)//g d
742-
#
743-
# Now, if g > 1, we're working with smaller integers.
744-
#
745-
# Note, that t, (da//g) and (db//g) are pairwise coprime.
746-
#
747-
# Indeed, (da//g) and (db//g) share no common factors (they were
748-
# removed) and da is coprime with na (since input fractions are
749-
# normalized), hence (da//g) and na are coprime. By symmetry,
750-
# (db//g) and nb are coprime too. Then,
751-
#
752-
# gcd(t, da//g) == gcd(na*(db//g), da//g) == 1
753-
# gcd(t, db//g) == gcd(nb*(da//g), db//g) == 1
754-
#
755-
# Above allows us optimize reduction of the result to lowest
756-
# terms. Indeed,
757-
#
758-
# g2 = gcd(t, d) == gcd(t, (da//g)*(db//g)*g) == gcd(t, g)
759-
#
760-
# t//g2 t//g2
761-
# a ± b == ----------------------- == ----------------
762-
# (da//g)*(db//g)*(g//g2) (da//g)*(db//g2)
763-
#
764-
# is a normalized fraction. This is useful because the unnormalized
765-
# denominator d could be much larger than g.
766-
#
767-
# We should special-case g == 1 (and g2 == 1), since 60.8% of
768-
# randomly-chosen integers are coprime:
769-
# https://en.wikipedia.org/wiki/Coprime_integers#Probability_of_coprimality
770-
# Note, that g2 == 1 always for fractions, obtained from floats: here
771-
# g is a power of 2 and the unnormalized numerator t is an odd integer.
772-
#
773-
# 2) Consider multiplication
774-
#
775-
# Let g1 = gcd(na, db) and g2 = gcd(nb, da), then
776-
#
777-
# na*nb na*nb (na//g1)*(nb//g2)
778-
# a*b == ----- == ----- == -----------------
779-
# da*db db*da (db//g1)*(da//g2)
780-
#
781-
# Note, that after divisions we're multiplying smaller integers.
782-
#
783-
# Also, the resulting fraction is normalized, because each of
784-
# two factors in the numerator is coprime to each of the two factors
785-
# in the denominator.
786-
#
787-
# Indeed, pick (na//g1). It's coprime with (da//g2), because input
788-
# fractions are normalized. It's also coprime with (db//g1), because
789-
# common factors are removed by g1 == gcd(na, db).
790-
#
791-
# As for addition/subtraction, we should special-case g1 == 1
792-
# and g2 == 1 for same reason. That happens also for multiplying
793-
# rationals, obtained from floats.
794728

795729
def _add(a, b):
796730
"""a + b"""

0 commit comments

Comments
 (0)