Continued fraction/Arithmetic/G(matrix ng, continued fraction n1, continued fraction n2): Difference between revisions

Line 4,609:
 
=={{header|Python}}==
{{trans|ATS}}
 
Here I have implemented heuristics to try to get correct answers for problems such as squaring the square root of two.
 
The Scheme from which this Python was derived is at https://archive.is/Vb1it
 
Both can be considered as based on the ATS.
 
<syntaxhighlight lang="python">
Line 4,766 ⟶ 4,759:
while not done:
(a12, a1, a2, a, b12, b1, b2, b) = env[ng]
if b == 0 and b1 == 0 and b2 == 0 and b12 == 0:
if# b1There ==are 0no andmore b12 == 0:terms.
retval = # There are no more terms.None
retvaldone = NoneTrue
elif b == 0 and b2 done == True0:
else:absorb_x_term ()
absorb_x_term ()
elif b == 0 or b2 == 0:
absorb_y_term ()
Line 4,781 ⟶ 4,773:
q1 = a1 // b1
q2 = a2 // b2
q1_diffif b12 != abs (q1 - q)0:
q2_diff = abs (q2 -q12 = a12 // q)b12
if q1_diffb12 >!= q2_diff0 and q == q1 and q == q2 and q == q12:
absorb_x_term ()
elif q1_diff < q2_diff:
absorb_y_term ()
elif b12 == 0 or q != a12 // b12:
#
# Because q = q1 = q2, the following also are
# true:
#
# a1/b1 - a/b = r1/b1 - r/b
# a2/b2 - a/b = r2/b2 - r/b
#
# So we can work with the remainders, which are
# smaller and so less likely to require "big
# integers".
#
# Rather than compare fractions, we will put the
# numerators over a common denominator of b*b1*b2,
# and then compare the new numerators.
#
n = (a - (b * q)) * b1 * b2
n1 = (a1 - (b1 * q)) * b * b2
n2 = (a2 - (b2 * q)) * b * b1
if abs (n1 - n) > abs (n2 - n):
absorb_x_term ()
else:
absorb_y_term ()
else:
# Output a term. Notice the resemblance to r2cf.
env[0] = (b12, b1, b2, b, # Divisors.
Line 4,819 ⟶ 4,784:
retval = (None if treat_as_infinite (q) else q)
done = True
#else:
# Rather than compare fractions, we will put the
# numerators over a common denominator of b*b1*b2,
# and then a2/b2compare -the a/bnew = r2/b2 - r/bnumerators.
elif b12 == 0 orn q= !=a * a12b1 //* b12:b2
#n1 true:= a1 * b * b2
#n2 = a2 * b * b1
#if Becauseabs q(n1 =- q1n) => q2,abs the(n2 following- also aren):
absorb_x_term ()
elif q1_diff < q2_diff else:
absorb_x_term absorb_y_term ()
return retval
 
Line 4,850 ⟶ 4,826:
print (" 4 => ", cf2string (four))
print (" (1 + 1/sqrt(2))/2 => ",
cf2string (cf_div (cf_add (one, cf_div (one, sqrt2)), two))),
" method 1")
print (" (1 + 1/sqrt(2))/2 => ",
cf2string (NG8 (1, 0, 0, 1, 0, 0, 0, 8) (silver_ratio,
silver_ratio)),
" method 2")
print (" (1 + 1/sqrt(2))/2 => ",
cf2string (cf_div (cf_div (cf_div (silver_ratio, sqrt2),
# a1/b1 - a/b = r1/b1 - r/b sqrt2),
absorb_y_term ( sqrt2)),
" method 3")
print (" sqrt(2) + sqrt(2) => ", cf2string (cf_add (sqrt2, sqrt2)))
print (" sqrt(2) - sqrt(2) => ", cf2string (cf_sub (sqrt2, sqrt2)))
Line 4,870 ⟶ 4,856:
3 => [3]
4 => [4]
(1 + 1/sqrt(2))/2 => [0;1,5,1,4,1,4,1,4,1,4,1,4,1,4,1,4,1,4,1,...] method 1
(1 + 1/sqrt(2))/2 => [0;1,5,1,4,1,4,1,4,1,4,1,4,1,4,1,4,1,4,1,...] method 2
(1 + 1/sqrt(2))/2 => [0;1,5,1,4,1,4,1,4,1,4,1,4,1,4,1,4,1,4,1,...] method 3
sqrt(2) + sqrt(2) => [2;1,4,1,4,1,4,1,4,1,4,1,4,1,4,1,4,1,4,1,...]
sqrt(2) - sqrt(2) => [0]
1,448

edits