Full factorization of polynomials over complexes with SymPy

亡梦爱人 提交于 2020-06-17 09:33:12

问题


I want to fully factorize a polynom, thus factorize it over complexes.

SymPy provide factor to do it, but I’m very surprised that factorization is done only over integer roots, e.g. :

>>> from sympy import *
>>> z = symbols('z')
>>> factor(z**2 - 1, z)
(z - 1)*(z + 1)
>>> factor(z**2 + 1, z)
z**2 + 1

or

>>> factor(2*z - 1, z)
2*z - 1
>>> factor(2*z - 1, z, extension=[Integer(1)/2])
2*(z - 1/2)

An answered question already exists : Factor to complex roots using sympy, and the solution given by asmeurer works :

>>> factor(z**2 + 1, z, extension=[I])
(z - I)*(z + I)

but you need to specify every divisor of non-integer roots, e.g. :

>>> factor(z**2 + 2, z, extension=[I])
z**2 + 2
>>> factor(z**2 + 2, z, extension=[I, sqrt(2)])
(z - sqrt(2)*I)*(z + sqrt(2)*I)

My question is : how to fully factorize a polynom (thus over complexes), without needing to give every divisor to extension ?

asmeurer gives a solution to do this :

>>> poly = z**2 + 2
>>> r = roots(poly, z)
>>> LC(poly, z)*Mul(*[(z - a)**r[a] for a in r])
/      ___  \ /      ___  \
\z - \/ 2 *I/*\z + \/ 2 *I/

But it should exists a native way to do it, no ?
Someting like factor(poly, z, complex=True).

I looked for in the documentation of factor, but I did not find anything. Futhermore, factor can take domain as optional argument that I believed allows to specified the set on which the factorization is made, but not

>>> factor(z**2 + 2, z, domain='Z')
 2
z  + 2
>>> factor(z**2 + 2, z, domain='R')
    /     2      \
2.0*\0.5*z  + 1.0/
>>> factor(z**2 + 2, z, domain='C')
     2
1.0*z  + 2.0

回答1:


The domain argument should work and in the case of Gaussian rationals you can also use gaussian=True which is equivalent to extension=I:

In [24]: factor(z**2 + 1, gaussian=True)
Out[24]: (z - ⅈ)⋅(z + ⅈ)

That doesn't work in your case though because the factorisation needs to be over QQ(I, sqrt(2)) rather than QQ(I). The reason that domains 'R' and 'C' don't work as expected is because they are inexact floating point domains rather than domains representing the real or complex numbers in the pure mathematical sense and factorisation is

The approaches above can be combined though with

In [28]: e = z**2 + 2

In [29]: factor(e, extension=roots(e))
Out[29]: (z - √2⋅ⅈ)⋅(z + √2⋅ⅈ)


来源:https://stackoverflow.com/questions/62357494/full-factorization-of-polynomials-over-complexes-with-sympy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!