(f)
return None
try to transform a polynomial to have rational coefficients

    try to find a transformation ``x = alpha*y``

    ``f(x) = lc*alpha**n * g(y)`` where ``g`` is a polynomial with
    rational coefficients, ``lc`` the leading coefficient.

    If this fails, try ``x = y + beta``
    ``f(x) = g(y)``

    Returns ``None`` if ``g`` not found;
    ``(lc, alpha, None, g)`` in case of rescaling
    ``(None, None, beta, g)`` in case of translation

    Notes
    =====

    Currently it transforms only polynomials without roots larger than 2.

    Examples
    ========

    >>> from sympy import sqrt, Poly, simplify
    >>> from sympy.polys.polytools import to_rational_coeffs
    >>> from sympy.abc import x
    >>> p = Poly(((x**2-1)*(x-2)).subs({x:x*(1 + sqrt(2))}), x, domain='EX')
    >>> lc, r, _, g = to_rational_coeffs(p)
    >>> lc, r
    (7 + 5*sqrt(2), -2*sqrt(2) + 2)
    >>> g
    Poly(x**3 + x**2 - 1/4*x - 1/4, x, domain='QQ')
    >>> r1 = simplify(1/r)
    >>> Poly(lc*r**3*(g.as_expr()).subs({x:x*r1}), x, domain='EX') == p
    True
try to transform a polynomial to have rational coefficients
try to transform a polynomial to have rational coefficients
def to_rational_coeffs(f):
    """
    try to transform a polynomial to have rational coefficients

    try to find a transformation ``x = alpha*y``

    ``f(x) = lc*alpha**n * g(y)`` where ``g`` is a polynomial with
    rational coefficients, ``lc`` the leading coefficient.

    If this fails, try ``x = y + beta``
    ``f(x) = g(y)``

    Returns ``None`` if ``g`` not found;
    ``(lc, alpha, None, g)`` in case of rescaling
    ``(None, None, beta, g)`` in case of translation

    Notes
    =====

    Currently it transforms only polynomials without roots larger than 2.

    Examples
    ========

    >>> from sympy import sqrt, Poly, simplify
    >>> from sympy.polys.polytools import to_rational_coeffs
    >>> from sympy.abc import x
    >>> p = Poly(((x**2-1)*(x-2)).subs({x:x*(1 + sqrt(2))}), x, domain='EX')
    >>> lc, r, _, g = to_rational_coeffs(p)
    >>> lc, r
    (7 + 5*sqrt(2), -2*sqrt(2) + 2)
    >>> g
    Poly(x**3 + x**2 - 1/4*x - 1/4, x, domain='QQ')
    >>> r1 = simplify(1/r)
    >>> Poly(lc*r**3*(g.as_expr()).subs({x:x*r1}), x, domain='EX') == p
    True

    """
    from sympy.simplify.simplify import simplify

    def _try_rescale(f):
        """
        try rescaling ``x -> alpha*x`` to convert f to a polynomial
        with rational coefficients.
        Returns ``alpha, f``; if the rescaling is successful,
        ``alpha`` is the rescaling factor, and ``f`` is the rescaled
        polynomial; else ``alpha`` is ``None``.
        """
        from sympy.core.add import Add
        if not len(f.gens) == 1 or not (f.gens[0]).is_Atom:
            return None, f
        n = f.degree()
        lc = f.LC()
        coeffs = f.monic().all_coeffs()[1:]
        coeffs = [simplify(coeffx) for coeffx in coeffs]
        if coeffs[-2] and not all(coeffx.is_rational for coeffx in coeffs):
            rescale1_x = simplify(coeffs[-2]/coeffs[-1])
            coeffs1 = []
            for i in range(len(coeffs)):
                coeffx = simplify(coeffs[i]*rescale1_x**(i + 1))
                if not coeffx.is_rational:
                    break
                coeffs1.append(coeffx)
            else:
                rescale_x = simplify(1/rescale1_x)
                x = f.gens[0]
                v = [x**n]
                for i in range(1, n + 1):
                    v.append(coeffs1[i - 1]*x**(n - i))
                f = Add(*v)
                f = Poly(f)
                return lc, rescale_x, f
        return None

    def _try_translate(f):
        """
        try translating ``x -> x + alpha`` to convert f to a polynomial
        with rational coefficients.
        Returns ``alpha, f``; if the translating is successful,
        ``alpha`` is the translating factor, and ``f`` is the shifted
        polynomial; else ``alpha`` is ``None``.
        """
        from sympy.core.add import Add
        if not len(f.gens) == 1 or not (f.gens[0]).is_Atom:
            return None, f
        n = f.degree()
        f1 = f.monic()
        coeffs = f1.all_coeffs()[1:]
        c = simplify(coeffs[0])
        if c and not c.is_rational:
            func = Add
            if c.is_Add:
                args = c.args
                func = c.func
            else:
                args = [c]
            sifted = sift(args, lambda z: z.is_rational)
            c1, c2 = sifted[True], sifted[False]
            alpha = -func(*c2)/n
            f2 = f1.shift(alpha)
            return alpha, f2
        return None

    def _has_square_roots(p):
        """
        Return True if ``f`` is a sum with square roots but no other root
        """
        from sympy.core.exprtools import Factors
        coeffs = p.coeffs()
        has_sq = False
        for y in coeffs:
            for x in Add.make_args(y):
                f = Factors(x).factors
                r = [wx.q for wx in f.values() if wx.is_Rational and wx.q >= 2]
                if not r:
                    continue
                if min(r) == 2:
                    has_sq = True
                if max(r) > 2:
                    return False
        return has_sq

    if f.get_domain().is_EX and _has_square_roots(f):
        r = _try_rescale(f)
        if r:
            return r[0], r[1], None, r[2]
        else:
            r = _try_translate(f)
            if r:
                return None, None, r[0], r[1]
    return None
def to_rational_coeffs ( f ) : from sympy . simplify . simplify import simplify def _try_rescale ( f ) : """
        try rescaling ``x -> alpha*x`` to convert f to a polynomial
        with rational coefficients.
        Returns ``alpha, f``; if the rescaling is successful,
        ``alpha`` is the rescaling factor, and ``f`` is the rescaled
        polynomial; else ``alpha`` is ``None``.
        """ from sympy . core . add import Add if not len ( f . gens ) == 1 or not ( f . gens [ 0 ] ) . is_Atom : return None , f n = f . degree ( ) lc = f . LC ( ) coeffs = f . monic ( ) . all_coeffs ( ) [ 1 : ] coeffs = [ simplify ( coeffx ) for coeffx in coeffs ] if coeffs [ - 2 ] and not all ( coeffx . is_rational for coeffx in coeffs ) : rescale1_x = simplify ( coeffs [ - 2 ] / coeffs [ - 1 ] ) coeffs1 = [ ] for i in range ( len ( coeffs ) ) : coeffx = simplify ( coeffs [ i ] * rescale1_x ** ( i + 1 ) ) if not coeffx . is_rational : break coeffs1 . append ( coeffx ) else : rescale_x = simplify ( 1 / rescale1_x ) x = f . gens [ 0 ] v = [ x ** n ] for i in range ( 1 , n + 1 ) : v . append ( coeffs1 [ i - 1 ] * x ** ( n - i ) ) f = Add ( * v ) f = Poly ( f ) return lc , rescale_x , f return None def _try_translate ( f ) : """
        try translating ``x -> x + alpha`` to convert f to a polynomial
        with rational coefficients.
        Returns ``alpha, f``; if the translating is successful,
        ``alpha`` is the translating factor, and ``f`` is the shifted
        polynomial; else ``alpha`` is ``None``.
        """ from sympy . core . add import Add if not len ( f . gens ) == 1 or not ( f . gens [ 0 ] ) . is_Atom : return None , f n = f . degree ( ) f1 = f . monic ( ) coeffs = f1 . all_coeffs ( ) [ 1 : ] c = simplify ( coeffs [ 0 ] ) if c and not c . is_rational : func = Add if c . is_Add : args = c . args func = c . func else : args = [ c ] sifted = sift ( args , lambda z : z . is_rational ) c1 , c2 = sifted [ True ] , sifted [ False ] alpha = - func ( * c2 ) / n f2 = f1 . shift ( alpha ) return alpha , f2 return None def _has_square_roots ( p ) : """
        Return True if ``f`` is a sum with square roots but no other root
        """ from sympy . core . exprtools import Factors coeffs = p . coeffs ( ) has_sq = False for y in coeffs : for x in Add . make_args ( y ) : f = Factors ( x ) . factors r = [ wx . q for wx in f . values ( ) if wx . is_Rational and wx . q >= 2 ] if not r : continue if min ( r ) == 2 : has_sq = True if max ( r ) > 2 : return False return has_sq if f . get_domain ( ) . is_EX and _has_square_roots ( f ) : r = _try_rescale ( f ) if r : return r [ 0 ] , r [ 1 ] , None , r [ 2 ] else : r = _try_translate ( f ) if r : return None , None , r [ 0 ] , r [ 1 ] return None