Factor options

55 views
Skip to first unread message

Paul Royik

unread,
Dec 7, 2014, 7:05:08 PM12/7/14
to sy...@googlegroups.com
How should I use factor to factor expression over irrational numbers?

For example,
x^2-4 produces (x-2)(x+2)
x^2-2 produces (x-sqrt(2))(x+sqrt(2))
x^4+1 produces (x^2-sqrt(2) x+1) (x^2+sqrt(2) x+1)
x^2+1 produces x^2+1 (only complex roots)
x^4-9 x^2-22 produces (x^2+2)(x-sqrt(11))(x+sqrt(11))

Mateusz Paprocki

unread,
Dec 8, 2014, 7:19:14 AM12/8/14
to sympy
Hi,
In [1]: from sympy import *

In [2]: var('x')
Out[2]: x

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

In [4]: factor(x**4-9*x**2-22, extension=[sqrt(11), sqrt(2), I])
Out[4]: (x - sqrt(11))*(x + sqrt(11))*(x - sqrt(2)*I)*(x + sqrt(2)*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/d6ad1def-31a0-4819-89fc-2a57c52f5636%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Chris Smith

unread,
Dec 8, 2014, 9:51:26 AM12/8/14
to sy...@googlegroups.com
If you don't know what extension to use you can just rebuild the expression from the roots:

>>> efactor = lambda e: Mul(*[(x - r)**m for r,m in roots(e).items()]).subs(
... x,e.free_symbols.pop())
>>> efactor(y**6 - 20*y**4 + 77*y**2 + 242)
(y - sqrt(11))**2*(y + sqrt(11))**2*(y - sqrt(2)*I)*(y + sqrt(2)*I)

Paul Royik

unread,
Dec 8, 2014, 10:09:45 AM12/8/14
to sy...@googlegroups.com
Thank you very much.

How do I discard complex roots?

Paul Royik

unread,
Dec 8, 2014, 1:03:25 PM12/8/14
to sy...@googlegroups.com
This is good method, but it doesn't work for x^4+1


On Monday, December 8, 2014 4:51:26 PM UTC+2, Chris Smith wrote:

Sergey Kirpichev

unread,
Dec 8, 2014, 3:48:27 PM12/8/14
to sy...@googlegroups.com
In [6]: factor(x**4 + 1, extension=[I, sqrt(2)])
Out[6]:
⎛      ___     ___  ⎞ ⎛      ___     ___  ⎞ ⎛      ___     ___  ⎞ ⎛      ___ 
⎜    ╲╱ 2    ╲╱ 2 ⋅ⅈ⎟ ⎜    ╲╱ 2    ╲╱ 2 ⋅ⅈ⎟ ⎜    ╲╱ 2    ╲╱ 2 ⋅ⅈ⎟ ⎜    ╲╱ 2  
⎜x - ───── - ───────⎟⋅⎜x - ───── + ───────⎟⋅⎜x + ───── - ───────⎟⋅⎜x + ───── +
⎝      2        2   ⎠ ⎝      2        2   ⎠ ⎝      2        2   ⎠ ⎝      2   

   ___  ⎞
 ╲╱ 2 ⋅ⅈ⎟
 ───────⎟
    2   ⎠

Chris Smith

unread,
Dec 9, 2014, 12:55:45 AM12/9/14
to sy...@googlegroups.com
Are you thinking of something like this?

>>> def efactor(e, I=None):
...   r = roots(e)
...   rad = set().union(*[i.atoms(Pow) for i in r])
...   if I:
...     rad.add(I)
...   return factor(e, extension=rad)
...
>>> efactor(x**4+1)
(x**2 - sqrt(2)*x + 1)*(x**2 + sqrt(2)*x + 1)

>>> print filldedent(efactor(x**4 + 1, I))

(x - sqrt(2)/2 - sqrt(2)*I/2)*(x - sqrt(2)/2 + sqrt(2)*I/2)*(x +
sqrt(2)/2 - sqrt(2)*I/2)*(x + sqrt(2)/2 + sqrt(2)*I/2)

Paul Royik

unread,
Dec 9, 2014, 6:40:14 AM12/9/14
to sy...@googlegroups.com
Exactly.
Thanks. 
Reply all
Reply to author
Forward
0 new messages