On Mon, Mar 8, 2010 at 8:43 AM, smichr <smi...@gmail.com> wrote: > Here's a draft of a Horner routine...any ideas where this might go? > rewrite? > > def _horner(p, x, n=0): > """recursive helper for horner.""" > > pow, co = p.TM[0], p.TC > if pow == co == 0: > return 0 > else: > t = co * Pow(x, pow) > return Pow(x, pow - n)*(co + _horner(p - t, x, pow)) > > def horner(p, x): > """Return the Horner rewrite of univariate polynomial, p. > > >>> from sympy import horner > >>> from sympy.abc import x, a, b, c > >>> horner(x**5 + a*x**2 + b*x + c, x) > c + x*(b + x*(a + x**3)) > > """ > if not p.func is Poly: > p = Poly(p, x) > rv = _horner(p, x) > assert not expand(rv - p.as_basic()) > return rv > > >>>> horner(2*x**3-6*x**2+2*x-1,x) > -1 + x*(2 - x*(6 - 2*x))
I needed this too --- please put it into polys I guess, together with tests and doctests. Ondrej -- You received this message because you are subscribed to the Google Groups "sympy" group. To post to this group, send email to sy...@googlegroups.com. To unsubscribe from this group, send email to sympy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sympy?hl=en.