(poly)
return coeff, poly
Try to get rid of symbolic coefficients from ``poly``.
Try to get rid of symbolic coefficients from ``poly``.
Try to get rid of symbolic coefficients from poly .
def preprocess_roots(poly):
    """Try to get rid of symbolic coefficients from ``poly``. """
    coeff = S.One

    try:
        _, poly = poly.clear_denoms(convert=True)
    except DomainError:
        return coeff, poly

    poly = poly.primitive()[1]
    poly = poly.retract()

    # TODO: This is fragile. Figure out how to make this independent of construct_domain().
    if poly.get_domain().is_Poly and all(c.is_term for c in poly.rep.coeffs()):
        poly = poly.inject()

        strips = list(zip(*poly.monoms()))
        gens = list(poly.gens[1:])

        base, strips = strips[0], strips[1:]

        for gen, strip in zip(list(gens), strips):
            reverse = False

            if strip[0] < strip[-1]:
                strip = reversed(strip)
                reverse = True

            ratio = None

            for a, b in zip(base, strip):
                if not a and not b:
                    continue
                elif not a or not b:
                    break
                elif b % a != 0:
                    break
                else:
                    _ratio = b // a

                    if ratio is None:
                        ratio = _ratio
                    elif ratio != _ratio:
                        break
            else:
                if reverse:
                    ratio = -ratio

                poly = poly.eval(gen, 1)
                coeff *= gen**(-ratio)
                gens.remove(gen)

        if gens:
            poly = poly.eject(*gens)

    if poly.is_univariate and poly.get_domain().is_ZZ:
        basis = _integer_basis(poly)

        if basis is not None:
            n = poly.degree()

            def func(k, coeff):
                return coeff//basis**(n - k[0])

            poly = poly.termwise(func)
            coeff *= basis

    return coeff, poly
def preprocess_roots ( poly ) : coeff = S . One try : _ , poly = poly . clear_denoms ( convert = True ) except DomainError : return coeff , poly poly = poly . primitive ( ) [ 1 ] poly = poly . retract ( ) # TODO: This is fragile. Figure out how to make this independent of construct_domain(). if poly . get_domain ( ) . is_Poly and all ( c . is_term for c in poly . rep . coeffs ( ) ) : poly = poly . inject ( ) strips = list ( zip ( * poly . monoms ( ) ) ) gens = list ( poly . gens [ 1 : ] ) base , strips = strips [ 0 ] , strips [ 1 : ] for gen , strip in zip ( list ( gens ) , strips ) : reverse = False if strip [ 0 ] < strip [ - 1 ] : strip = reversed ( strip ) reverse = True ratio = None for a , b in zip ( base , strip ) : if not a and not b : continue elif not a or not b : break elif b % a != 0 : break else : _ratio = b // a if ratio is None : ratio = _ratio elif ratio != _ratio : break else : if reverse : ratio = - ratio poly = poly . eval ( gen , 1 ) coeff *= gen ** ( - ratio ) gens . remove ( gen ) if gens : poly = poly . eject ( * gens ) if poly . is_univariate and poly . get_domain ( ) . is_ZZ : basis = _integer_basis ( poly ) if basis is not None : n = poly . degree ( ) def func ( k , coeff ) : return coeff // basis ** ( n - k [ 0 ] ) poly = poly . termwise ( func ) coeff *= basis return coeff , poly