Mauro Gentile
Mauro Gentile

Reputation: 1511

simply solve not showing doble solutions

I need SymPy to show all the solutions of a function, duplicated if it is the case. In other words I would need that:

x=sym.symbols('x')
sym.solve(x**2, x)

returned

[0,0]

while it actually returns only:

[0]

How do I achieve this?

Upvotes: 0

Views: 44

Answers (1)

Oscar Benjamin
Oscar Benjamin

Reputation: 14480

What you are looking for is the roots of a polynomial along with their multiplicity. That is given by the roots function if you specify multiple=True:

In [6]: roots(x**2, multiple=True)                                                                                                
Out[6]: [0, 0]

In [7]: roots(x**2 + 1, multiple=True)                                                                                            
Out[7]: [ⅈ, -ⅈ]

https://docs.sympy.org/latest/modules/polys/reference.html#sympy.polys.polyroots.roots

There are other functions such as real_roots to get the real roots and nroots to get numerical approximations of the roots depending on exactly what you want to do.

Upvotes: 1

Related Questions