Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solving polynomials with complex coefficients using sympy

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.

like image 963
ben_afobe Avatar asked Oct 29 '22 21:10

ben_afobe


1 Answers

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]
like image 163
Cimbali Avatar answered Nov 15 '22 07:11

Cimbali