Search code examples
mathsympynumerical-methodssymbolic-math

Find roots of a polynomial with a symbolic constant SymPy


I have this polynomial for which I need to find the roots. The problem I'm running into is that my polynomial has an exogenously chosen parameter for which I need to find the roots in order to interpret. My approach has been to use the real_roots() method in SymPy:

from sympy.abc import x, c
from sympy import Poly, real_roots
Poly(4-2*c*c*x+c*c*x**3,x,domain='RR[c]').real_roots()

but I get the error NotImplementedError: sorted roots not supported over RR[c] which I'm having a hard time interpreting. Is this even the right way to go about approximating the roots of this polynomial? Ideally, the roots will be functions of c.


Solution

  • Unless you have need of Poly methods, it is not necessary to work with an explicit Poly expression. In this case, the real_roots method does not work unless the roots can be sorted (and they can't in this case). But if you just solve the expression without the Poly wrapper for x you will get the 3 roots of the cubic expression and can substitute in values of c afterwards. You may not be able to tell which is real without pugging in a value of c, however. (But if you know the value of c then real_roots will give you the answer.)

    >>> sol = solve(c**2*x**3 - 2*c**2*x + 4, x)  # sol is a list with 3 solution exprs