SymPy cannot solve polynomial equation of 4th order
Asked Answered
C

2

6

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.

Cork answered 6/2, 2015 at 13:14 Comment(0)
R
1

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

Revamp answered 6/2, 2015 at 15:14 Comment(4)
After i update to sympy 0.7.6 (using python 3.4) there is different error massage: line 103 in nonzero : raise TypeError("cannot determine truth value of\n%s" % self)Cork
Looks like this is a bug, which has been fixed in the git version of SymPy.Preraphaelite
I'm using anaconda distribution of python. Is it possible to update somehow to this git version there, or i have to rewrite some scripts?Cork
Install git (on OS X and Windows the easiest way is to install the GitHub desktop client), clone github.com/sympy/sympy, and use python setup.py install or python setup.py develop (the latter will automatically update if you do a git pull, otherwise you have to install it again each time). Or if you only use isympy, you can just cd to the sympy directory and run bin/isympy from there.Preraphaelite
R
0

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)
Rothman answered 23/2, 2016 at 8:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.