Re: How to do Continued fraction of polynomials

*To*: mathgroup at smc.vnet.net*Subject*: [mg53111] Re: [mg53052] How to do Continued fraction of polynomials*From*: Andrzej Kozlowski <akoz at mimuw.edu.pl>*Date*: Thu, 23 Dec 2004 08:00:01 -0500 (EST)*Sender*: owner-wri-mathgroup at wolfram.com

On 23 Dec 2004, at 04:33, Milind Gupta wrote: >>> >>> How can we do a continued fraction expansion in Mathematica. >>> >>> Suppose we have >>> x^3 + 2x >>> ------------ >>> x^2 + 1 >>> >>> and we want: >>> >>> x + 1 >>> ------------- >>> x + 1 >>> ---- >>> x >>> >>> Can we get this by using some function or some technique in >>> Mathematica?? > >> >> I am not quite sure whether the program below gives quite what you wan >> tbut I am pretty sure that if it doesn't it can be adapted. >> >> F[f_, g_] := >> If[PolynomialReduce[f, g][[1, 1]] =!= 0, PolynomialReduce[f, >> g][[ >> 1, 1]] + F[PolynomialReduce[f, >> g][[2]], g], Module[{u = g, v = f, p, ls}, ls = Flatten[Last[ >> Reap[While[u =!= 0, p = PolynomialReduce[u, v]; u = v; v = >> p[[2]]; Sow[ >> p[[1]]]]]]]; 1/Fold[Function[{x, y}, y + 1/x], Infinity, >> Reverse[ls]]]] >> >> In your case it gives the answer you wanted: >> >> F[x^3 + 2*x, x^2 + 1] >> >> x + 1/(1/x + x) >> >> Together[%] >> >> (2*x + x^3)/(1 + x^2) >> >> here is another case >> >> F[x^3 + 2*x^2 + x + 2, >> x^4 + x^3 + 3*x - 1] >> >> 1/(-1 + x + >> 1/(x + 2/(1 + 2*x + x^2))) >> >> Together[%] >> >> (2 + x + 2*x^2 + x^3)/ >> (-1 + 3*x + x^3 + x^4) >> >> It's best to look at it in "TraditionalForm". Unless you use Hold or >> HoldForm in the program it is difficult to insure that Matheamtica >> will >> not sometimes convert it to a slightly different form. One can change >> the program just to give a list of terms of the continued fraction but >> I shall leave it at this. >> >> P.S. You need at least Matheamtica 5.0 to use Sow and Reap. If you >> have >> an earlier version of Mathematica you need to change the program >> slightly. >> >> > > When I input this: > > expr = 1. + 2. x + 2. x^2 + 2. x^3 > ----------------------------------------- > 1. + 2. x + 2. x^2 > > and then I do F[Numerator[expr], Denominator[expr]] I get error > like Recursion Limit exceeded and stuff. But when I make expr as: > > > expr = 1 + 2 x + 2 x^2 + 2 x^3 > ----------------------------------------- > 1 + 2 x + 2 x^2 > > This works now perfectly. expr is the result from my previous steps. > Is there any way I can make the coefficients of expr as integers > automatically to use with F ??? > > Milind > Yes, use Rationalize. Rationalize[(1. + 2.*x + 2.*x^2 + 2.*x^3)/ (1. + 2.*x + 2.*x^2)] (1 + 2*x + 2*x^2 + 2*x^3)/(1 + 2*x + 2*x^2) You should remeber that in Mathematica numbers like 0.1 are not the same as 1/10. They are not decimals but "approximate numbers" and they have their own arithmetic that sometimes gives strange results. You should avoid them in symbolic algebra because they will prevent various simplifications, like cancellation of common factors. Andrzej KOzlowski