On Wed, 16 Feb 2022 at 22:26, brombo <[email protected]> wrote:
>
> Does sympy have the capability to optimally factor (minimum number of 
> numerical operations) a polynomial or better yet a multinomial for numerical 
> evaluation.  The simple example for a polynomial would be consider the 
> polynomial -
>
> y = a*x**3+b*x**2+c*x+d
>
> then the  optimal factorization (minimizes number of numerical operations) 
> would be -
>
> y  = ((a*x+b)*x+c)*x+d
>
> What are sympy's the current capabilities?

This particular example can be handled with horner:

In [1]: a, b, c, d, x = symbols('a, b, c, d, x')

In [2]: y = a * x ** 3 + b * x ** 2 + c * x + d

In [3]: print(y)
a*x**3 + b*x**2 + c*x + d

In [4]: print(horner(y))
d + x*(c + x*(a*x + b))

In the multivariate case the result depends on the ordering of the symbols e.g.:

In [9]: x, y = symbols("x, y")

In [10]: p = x ** 2 * y + y ** 2 * x + x ** 2 + y ** 2

In [11]: print(horner(p, x, y))
x*(x*(y + 1) + y**2) + y**2

In [12]: print(horner(p, y, x))
x**2 + y*(x**2 + y*(x + 1))

I have heard of algorithms that attempt to find an optimal ordering
for hornerisation but I don't think any is implemented in sympy.

--
Oscar

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/CAHVvXxRGgtTA-bODRB8SoNO3fcX%2B5eEjYAR2WUSF%2BAr6Z5xViw%40mail.gmail.com.

Reply via email to