Why does factor() sometimes just return the input?

28 views
Skip to first unread message

Adam Leeper

unread,
Nov 30, 2015, 1:01:43 AM11/30/15
to sympy
Hi all-

I'm trying to understand why factor() and Poly.all_roots() behave differently when trying to find simple complex roots. I suspect there is something to do with exact values vs. numerical approximations, but I want something that "just works". I'm doing work with transfer functions, and I'd really like to be able to manipulate the numerator and denominator forms and get back an expression of the form (x + a)*(x + b) rather than a list of the roots [-a, -b].

Here's an example:

from sympy import *
x = Symbol('x')
factor(x**2 + 4, x, extension=[I])
Poly(x**2 + 4).all_roots()
factor(x**2 + 3, x, extension=[I])
Poly(x**2 + 3).all_roots()

With sympy 0.7.4.1 the output is:

In [3]: factor(x**2 + 4, x, extension=[I])
Out[3]: (x - 2*I)*(x + 2*I)

In [4]: Poly(x**2 + 4).all_roots()
Out[4]: [-2*I, 2*I]

In [5]: factor(x**2 + 3, x, extension=[I])
Out[5]: x**2 + 3

In [6]: Poly(x**2 + 3).all_roots()
Out[6]: [-sqrt(3)*I, sqrt(3)*I]

As you can see, Out[3] and Out[4] both "work".
But, Out[5] doesn't work (it's just the same as the input), while Out[6] is correct (but not in the form I want it).
Any help is much appreciated :)


Mateusz Paprocki

unread,
Nov 30, 2015, 3:59:51 AM11/30/15
to sympy
Hi,
output _5 is correct, x**2 + 3 doesn't have linear factors over Q(I).
However, it has over Q(I, sqrt(3)):

In [1]: factor(x**2 + 3, x, extension=[I, sqrt(3)])
Out[1]: (x - sqrt(3)*I)*(x + sqrt(3)*I)

With all_roots() method (or roots() function), you can get the
expected form like this:

In [2]: Mul(*[ (x - r)**k for r, k in roots(x**2 + 3).items() ])
Out[2]: (x - sqrt(3)*I)*(x + sqrt(3)*I)

Mateusz

>
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/2ef035c5-e6cf-4573-99ee-c30d72f86e1a%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages