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.

perror
  • 7,071
  • 16
  • 58
  • 85
K4stan
  • 61
  • 4

2 Answers2

1

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

smichr
  • 16,948
  • 2
  • 27
  • 34
  • 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) – K4stan Feb 06 '15 at 19:17
  • Looks like this is a bug, which has been fixed in the git version of SymPy. – asmeurer Feb 06 '15 at 21:40
  • 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? – K4stan Feb 07 '15 at 12:19
  • 1
    Install git (on OS X and Windows the easiest way is to install the GitHub desktop client), clone https://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. – asmeurer Feb 08 '15 at 04:14
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)
Mark
  • 85
  • 1
  • 11