@@ -363,3 +363,75 @@ require a potentially expensive `gcd` computation. We can use the
363363In addition to the above proof, a formal, computer-verified proof
364364of correctness (using Lean) of the algorithm can be found here:
365365https://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.
0 commit comments