Cute polynomial polynomial factor

I think that for this there should be a simple answer, but I searched for a long time and could not find it.

Example: a simple Laurent polynomial, so

>> p = 2*y*x**2+4*y/x

Factorization gives

>> factor(p) 2*y*(x**3 + 2)/x How to extract the coefficient 2*y/x? Is there an easy way to get the overall coefficient in an expression when the expression is not a polynomial? I tried a lot, but did not find anything distant satisfaction. Factorization must exist somewhere in the steps factor(), right?

+4
source share
1 answer

As @unutbu points out, from this question you can see what factor_listgives polynomial factors in an easier-to-use fashon.

, SymPy (. https://github.com/sympy/sympy/issues/5131). , . SymPy, , p , .

, -

n, d = fraction(cancel(p))
factor_list(n)
factor_list(d)

.

cancel , factor(p), , fraction . p.as_numer_denom(), ( ).

x 1/x . ()

def aspoly1t(p, t, z=Symbol('z')):
    """
    Rewrite p, a polynomial in t and 1/t, as a polynomial in t and z=1/t
    """
    pa, pd = cancel(p).as_numer_denom()
    pa, pd = Poly(pa, t), Poly(pd, t)
    assert pd.is_monomial
    d = pd.degree(t)
    one_t_part = pa.slice(0, d + 1)
    t_part = pa - one_t_part
    t_part = t_part.to_field().quo(pd)
    one_t_part = Poly.from_list(reversed(one_t_part.rep.rep), *one_t_part.gens, domain=one_t_part.domain)
    one_t_part = one_t_part.replace(t, z) # z will be 1/t
    ans = t_part.as_poly(t, z) + one_t_part.as_poly(t, z)
    return ans

( - Poly Poly(p, x, 1/x)). factor_list:

>>> aspoly1t(p, x)
Poly(2*y*x**2 + 4*y*z, x, z, domain='ZZ[y]')
>>> factor_list(aspoly1t(p, x))
(2, [(Poly(y, x, y, z, domain='ZZ'), 1), (Poly(x**2 + 2*z, x, y, z, domain='ZZ'), 1)])

, , , .

+3

Source: https://habr.com/ru/post/1690644/


All Articles