2016-01-16 06:37:17 +00:00
|
|
|
# frac.py (C) 2015-present SheetJS -- http://sheetjs.com
|
2015-04-22 04:14:03 +00:00
|
|
|
# vim: set fileencoding=utf-8 :
|
|
|
|
"""
|
|
|
|
Rational approximations to numbers
|
|
|
|
|
|
|
|
This module can generate fraction representations using:
|
|
|
|
- Mediant method (akin to fractions.Fraction#limit_denominator)
|
|
|
|
- Aberth method (as used by spreadsheet software)
|
|
|
|
|
|
|
|
All functions take 3 arguments:
|
|
|
|
|
|
|
|
- x: the number to be approximated
|
|
|
|
- D: the max denominator
|
|
|
|
- mixed: if True, generate a mixed representation
|
|
|
|
|
|
|
|
The return value is a list of 3 elements: [quotient, numerator, denominator]
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
def med(x, D, mixed=False):
|
|
|
|
"""Generate fraction representation using Mediant method"""
|
2015-05-05 06:19:23 +00:00
|
|
|
n1, d1 = int(x), 1
|
2015-04-22 04:14:03 +00:00
|
|
|
n2, d2 = n1+1, 1
|
|
|
|
m = 0.
|
|
|
|
if x != n1:
|
|
|
|
while d1 <= D and d2 <= D:
|
|
|
|
m = float(n1 + n2) / (d1 + d2)
|
2015-05-05 06:19:23 +00:00
|
|
|
if x == m:
|
2015-04-22 04:14:03 +00:00
|
|
|
if d1 + d2 <= D:
|
|
|
|
n1, d1 = n1 + n2, d1 + d2
|
|
|
|
d2 = D + 1
|
|
|
|
elif d1 > d2:
|
|
|
|
d2 = D+1
|
|
|
|
else:
|
|
|
|
d1 = D+1
|
|
|
|
break
|
|
|
|
elif x < m:
|
|
|
|
n2, d2 = n1+n2, d1+d2
|
|
|
|
else:
|
|
|
|
n1, d1 = n1+n2, d1+d2
|
|
|
|
if d1 > D:
|
|
|
|
n1, d1 = n2, d2
|
|
|
|
if not mixed:
|
|
|
|
return [0, n1, d1]
|
|
|
|
q = divmod(n1, d1)
|
|
|
|
return [q[0], q[1], d1]
|
|
|
|
|
|
|
|
|
|
|
|
def cont(x, D, mixed=False):
|
|
|
|
"""Generate fraction representation using Aberth method"""
|
|
|
|
B = abs(x)
|
2015-05-05 06:19:23 +00:00
|
|
|
I = int
|
|
|
|
P_2, P_1, P, Q_2, Q_1, Q = 0, 1, 0, 1, 0, 0
|
2015-04-22 04:14:03 +00:00
|
|
|
while Q_1 < D:
|
2015-05-05 06:19:23 +00:00
|
|
|
A = I(B)
|
2015-04-22 04:14:03 +00:00
|
|
|
P = A * P_1 + P_2
|
|
|
|
Q = A * Q_1 + Q_2
|
2016-09-23 05:43:23 +00:00
|
|
|
if (B - A) < 0.00000005:
|
2015-04-22 04:14:03 +00:00
|
|
|
break
|
|
|
|
B = 1. / (B-A)
|
|
|
|
P_2, P_1 = P_1, P
|
|
|
|
Q_2, Q_1 = Q_1, Q
|
2015-05-05 06:19:23 +00:00
|
|
|
if Q > D:
|
|
|
|
if Q_1 <= D:
|
|
|
|
P, Q = P_1, Q_1
|
|
|
|
else:
|
|
|
|
P, Q = P_2, Q_2
|
|
|
|
sgn = -1 if x < 0 else 1
|
2015-04-22 04:14:03 +00:00
|
|
|
if not mixed:
|
|
|
|
return [0, sgn * P, Q]
|
|
|
|
q = divmod(sgn * P, Q)
|
|
|
|
return [q[0], q[1], Q]
|