[math-fun] How can Mathematica not have Mediant?
To get all "best" rational approximations to a real, instead of continued fractions, use Mediant: Mediant[a_] := a; Mediant[a__] := Divide @@ (Total /@ {Numerator[{a}], Denominator[{a}]}) (Just sum of numerators over sum of denominators.) Then we can, e.g., In[62]:= mediate[π, 10] Out[62]= {0, 1, 2, 3} | {22/7, 19/6, 16/5, 13/4, 10/3, 7/2, 4, 1/0} where the fractions are in numerical order and improve toward the "|". In[60]:= mediate[√2, 6] Out[60]= {0, 1, 4/3, 7/5} | {10/7, 3/2, 2, 1/0} In[41]:= mediate[√2, 12] Out[41]= {0, 1, 4/3, 7/5, 24/17, 41/29, 140/99} | {99/70, 58/41, 17/12, 10/7, 3/2, 2, 1/0} (I confess to being unable to convince Mathematica to say 1/0 vs ComplexInfinity.) Here's mediate: Unprotect[Numerator, Denominator]; Numerator[ComplexInfinity] = 1; Denominator[ComplexInfinity] = 0; Off[Power::infy] (* 1/0 is an important rational number! *) mediate[x_?NumericQ, ct_?NumericQ /; ct > 0, lo_ : {0}, hi_ : {ComplexInfinity}] := Piecewise[{{mediate [x, ct - 1, Append[lo, #], hi], # <= x}}, mediate[x, ct - 1, lo, Prepend[hi, #]]] &@ Mediant[lo[[-1]], hi[[1]]] /; ct >= 1 mediate[_, _, lo_, hi_] := lo | hi (Julian, is there a cool way to avoid repeating "mediate [x, ct - 1" with Sequence?) Pushing harder on π, In[39]:= mediate[π, 26] Out[39]= {0, 1, 2, 3, 25/8, 47/15, 69/22, 91/29, 113/36, 135/43, 157/50, 179/57, 201/64, 223/71, 245/78, 267/85, 289/92, 311/99, 333/106} | {355/113, 22/7, 19/6, 16/5, 13/4, 10/3, 7/2, 4, 1/0} so we see that 22/7 and 355/113 are both overestimates. And we see the drawback of not using continued fractions. The "15" in π = {3,7,15,1,292,...} caused 15 consecutive slowly converging underestimates. Soon, we shall face 292 more! To see how easy it is to perform mediate by hand, arithmetic with boxes <http://gosper.org/rectarith12.pdf> . —rwg
participants (1)
-
Bill Gosper