5

I'm very new to python so forgive me if this has a simple fix. I'm trying to solve polynomials with complex coefficients using sympy. I find that I get a blank output if k is 'too complicated'... I'm not quite sure how to define what that means just yet. As a first example consider this fourth order polynomial with complex coefficients,

In [424]: solve(k**4+ 2*I,k)
Out[424]: 
[-2**(1/4)*sqrt(-sqrt(2)/4 + 1/2) - 2**(1/4)*I*sqrt(sqrt(2)/4 + 1/2),
 2**(1/4)*sqrt(-sqrt(2)/4 + 1/2) + 2**(1/4)*I*sqrt(sqrt(2)/4 + 1/2),
 -2**(1/4)*sqrt(sqrt(2)/4 + 1/2) + 2**(1/4)*I*sqrt(-sqrt(2)/4 + 1/2),
 2**(1/4)*sqrt(sqrt(2)/4 + 1/2) - 2**(1/4)*I*sqrt(-sqrt(2)/4 + 1/2)]

there are no problems obtaining an output. I'm interested, though, in solving something like,

In [427]: solve(k**6 + 3*I*k**5 - 2*k**4 + 9*k**3 - 4*k**2 + k - 1,k)
Out[427]: []

which is a lot more complicated and returns an empty list. I can, however, solve this using maple, for instance. Also, note that in removing the complex coefficients, there are no issues,

In [434]: solve(k**6 + 3*k**5 - 2*k**4 + 9*k**3 - 4*k**2 + k - 1,k)
Out[434]: 
[CRootOf(k**6 + 3*k**5 - 2*k**4 + 9*k**3 - 4*k**2 + k - 1, 0),
 CRootOf(k**6 + 3*k**5 - 2*k**4 + 9*k**3 - 4*k**2 + k - 1, 1),
 CRootOf(k**6 + 3*k**5 - 2*k**4 + 9*k**3 - 4*k**2 + k - 1, 2),
 CRootOf(k**6 + 3*k**5 - 2*k**4 + 9*k**3 - 4*k**2 + k - 1, 3),
 CRootOf(k**6 + 3*k**5 - 2*k**4 + 9*k**3 - 4*k**2 + k - 1, 4),
 CRootOf(k**6 + 3*k**5 - 2*k**4 + 9*k**3 - 4*k**2 + k - 1, 5)]

The elements of the resulting array can be evaluated numerically.

So, is this a problem to do with complex coefficients? How can I solve equations like the one on line [427]?

I have tried to solve with nsolve() and factor the roots out one by one, though I've had no luck with this method either.

ben_afobe
  • 95
  • 4
  • You need a solver that will work in the complex plane. A root finder that works on the real number line will find discrete points, one per root. A complex root finder will have to find curves in the complex plane. It's a harder problem. – duffymo Aug 31 '16 at 18:03
  • 1
    `sympy.nroots()` is able to find the (numerical) roots of the polynomial in question. – Stelios Aug 31 '16 at 19:36
  • That's worked thank you very much @Stelios. I hadn't come across `sympy.nroots` before. – ben_afobe Aug 31 '16 at 19:44
  • I guess CRootOf doesn't support complex coefficients yet. – asmeurer Sep 01 '16 at 17:44

1 Answers1

2

As per the comment of Stelios, you can use sympy.polys.polytools.nroots:

>>> from sympy import solve, nroots, I
>>> from sympy.abc import k
>>> solve(k**6 + 3*I*k**5 - 2*k**4 + 9*k**3 - 4*k**2 + k - 1,k)
[]
>>> nroots(k**6 + 3*I*k**5 - 2*k**4 + 9*k**3 - 4*k**2 + k - 1)
[-2.05972684672 - 0.930178254620881*I, -0.0901851681681614 + 0.433818575087712*I, -0.0734840785305346 - 0.434217215694685*I, 0.60726931721974 - 0.0485101438937812*I, 0.745127208196241 + 0.945593905069312*I, 0.870999568002712 - 2.96650686594768*I]
Cimbali
  • 11,012
  • 1
  • 39
  • 68