K4stan
K4stan

Reputation: 61

SymPy cannot solve polynomial equation of 4th order

I have an polynomial equation of 4th order and I need to find all roots. Simple example:

from sympy import (Symbol,solve,I)

a=4+5*I; b=3+7*I; c=12-56*I; d=33+56*I; e=345-67*I; x=Symbol('x')
eq=a*x**4 + b*x**3 + c*x**2 + d*x +e
solve(eq,x)

If a,b,c,d,e are pure real, then it work just fine. But in my case all of them are complex numbers. Then i did get call:

PolynomialError: 'cannot return general quartic solution'

I find kind of similar issue, and implement the fix: Description of the issue. Fix of the issue

but it doesn't really help. There is some kind of strange problem, as now the call is (as changed in the fix):

PolynomialError: Cannot determine if `-((12 - 56*I)/(4 + 5*I) - 3*(3 + 7*I)**2/(8*(4 + 5*I)**2))**2/12 + (3 + 7*I)*((33 + 56*I)/(4*(4 + 5*I)) + (3 + 7*I)*(3*(3 + 7*I)**2/(256*(4 + 5*I)**2) - (12 - 56*I)/(16*(4 + 5*I)))/(4 + 5*I))/(4 + 5*I) - (345 - 67*I)/(4 + 5*I)` is nonzero.

But to determine if expression above is nonzero is the most simplest thing, so don't know where the problem could be.

Upvotes: 6

Views: 1820

Answers (2)

Mark
Mark

Reputation: 85

If you want a more flexible solution, you could solve for x using binary search with something like the following:

def maybeRightX(maybeX, polys):
    sum = 0
    for i in range(len(polys)):
        sum += polys[i]*(maybeX ** i)
    return sum

def solve(y, polys):
    lo = 0
    hi = y
    while lo <= hi:
        mid = (lo + hi)//2
        if (maybeRightX(mid, polys)) < y:
            lo = mid + 1
        else:
            hi = mid - 1
    return (hi + 1)

Upvotes: 0

smichr
smichr

Reputation: 19047

Upgrade to the most recent version of SymPy which supports arbitrary quartic solutions.

Upvotes: 1

Related Questions