Exemplo n.º 1
0
def poly_factors(f, *symbols, **flags):
    """Factor polynomials over rationals.

       >>> from sympy.polys.factortools import poly_factors
       >>> from sympy.abc import x, y

       >>> poly_factors(x**2 - y**2, x, y)
       (1, [(Poly(x - y, x, y), 1), (Poly(x + y, x, y), 1)])

    """
    if not isinstance(f, Poly):
        f = Poly(f, *symbols)
    elif symbols:
        raise SymbolsError("Redundant symbols were given")

    symbols = list(f.symbols)

    try:
        denom, F = f.as_integer()
    except CoefficientError:
        other = set([])

        for coeff in f.iter_coeffs():
            other |= coeff.atoms(Symbol)

        symbols += sorted(other)

        F = Poly(f, *symbols)
        denom, F = F.as_integer()

    cont, factors = zzX_factor(zzX_from_poly(F))

    for i, (h, k) in enumerate(factors):
        h = zzX_to_poly(h, *symbols)

        if f.symbols != symbols:
            h = h.as_poly(*f.symbols)

        factors[i] = (h, k)

    return Rational(cont, denom), factors
Exemplo n.º 2
0
def poly_factors(f, *symbols, **flags):
    """Factor polynomials over rationals.

       >>> from sympy import *
       >>> x, y = symbols("x y")

       >>> poly_factors(x**2 - y**2, x, y)
       (1, [(Poly(x - y, x, y), 1), (Poly(x + y, x, y), 1)])

    """
    if not isinstance(f, Poly):
        f = Poly(f, *symbols)
    elif symbols:
        raise SymbolsError("Redundant symbols were given")

    symbols = list(f.symbols)

    try:
        denom, F = f.as_integer()
    except CoefficientError:
        other = set([])

        for coeff in f.iter_coeffs():
            other |= coeff.atoms(Symbol)

        symbols += sorted(other)

        F = Poly(f, *symbols)
        denom, F = F.as_integer()

    cont, factors = zzX_factor(zzX_from_poly(F))

    for i, (h, k) in enumerate(factors):
        h = zzX_to_poly(h, *symbols)

        if f.symbols != symbols:
            h = h.as_poly(*f.symbols)

        factors[i] = (h, k)

    return Rational(cont, denom), factors
Exemplo n.º 3
0
def poly_factors(f, *symbols, **flags):
    """Factor polynomials over rationals.

       >>> from sympy import *
       >>> x, y = symbols("x y")

       >>> poly_factors(x**2 - y**2, x, y)
       (1, [(Poly(x - y, x, y), 1), (Poly(x + y, x, y), 1)])

    """
    if not isinstance(f, Poly):
        f = Poly(f, *symbols)
    elif symbols:
        raise SymbolsError("Redundant symbols were given")

    denom, f = f.as_integer()

    if f.is_univariate:
        coeffs = map(int, f.iter_all_coeffs())
        content, factors = zzx_factor(coeffs)

        for i in xrange(len(factors)):
            factor, k = factors[i]
            n = zzx_degree(factor)

            terms = {}

            for j, coeff in enumerate(factor):
                if coeff != 0:
                    terms[(n - j, )] = Integer(coeff)

            factors[i] = Poly(terms, *f.symbols), k
    else:
        content, factors = kronecker_mv(f, **flags)

    return Rational(content, denom), factors
Exemplo n.º 4
0
def poly_factors(f, *symbols, **flags):
    """Factor polynomials over rationals.

       >>> from sympy import *
       >>> x, y = symbols("x y")

       >>> poly_factors(x**2 - y**2, x, y)
       (1, [(Poly(x - y, x, y), 1), (Poly(x + y, x, y), 1)])

    """
    if not isinstance(f, Poly):
        f = Poly(f, *symbols)
    elif symbols:
        raise SymbolsError("Redundant symbols were given")

    denom, f = f.as_integer()

    if f.is_univariate:
        coeffs = map(int, f.iter_all_coeffs())
        content, factors = zzx_factor(coeffs)

        for i in xrange(len(factors)):
            factor, k = factors[i]
            n = zzx_degree(factor)

            terms = {}

            for j, coeff in enumerate(factor):
                if coeff != 0:
                    terms[(n-j,)] = Integer(coeff)

            factors[i] = Poly(terms, *f.symbols), k
    else:
        content, factors = kronecker_mv(f, **flags)

    return Rational(content, denom), factors