(f, g)
return p, q
Cancel common factors in a rational function ``f/g``.

        Examples
        --------
        >>> from sympy.polys import ring, ZZ
        >>> R, x,y = ring("x,y", ZZ)

        >>> (2*x**2 - 2).cancel(x**2 - 2*x + 1)
        (2*x + 2, x - 1)
Cancel common factors in a rational function ``f/g``.
Cancel common factors in a rational function f / g .
def cancel(f, g):
        """
        Cancel common factors in a rational function ``f/g``.

        Examples
        --------
        >>> from sympy.polys import ring, ZZ
        >>> R, x,y = ring("x,y", ZZ)

        >>> (2*x**2 - 2).cancel(x**2 - 2*x + 1)
        (2*x + 2, x - 1)

        """
        ring = f.ring

        if not f:
            return f, ring.one

        domain = ring.domain

        if not (domain.has_Field and domain.has_assoc_Ring):
            _, p, q = f.cofactors(g)

            if q.is_negative:
                p, q = -p, -q
        else:
            new_ring = ring.clone(domain=domain.get_ring())

            cq, f = f.clear_denoms()
            cp, g = g.clear_denoms()

            f = f.set_ring(new_ring)
            g = g.set_ring(new_ring)

            _, p, q = f.cofactors(g)
            _, cp, cq = new_ring.domain.cofactors(cp, cq)

            p = p.set_ring(ring)
            q = q.set_ring(ring)

            p_neg = p.is_negative
            q_neg = q.is_negative

            if p_neg and q_neg:
                p, q = -p, -q
            elif p_neg:
                cp, p = -cp, -p
            elif q_neg:
                cp, q = -cp, -q

            p = p.mul_ground(cp)
            q = q.mul_ground(cq)

        return p, q
def cancel ( f , g ) : ring = f . ring if not f : return f , ring . one domain = ring . domain if not ( domain . has_Field and domain . has_assoc_Ring ) : _ , p , q = f . cofactors ( g ) if q . is_negative : p , q = - p , - q else : new_ring = ring . clone ( domain = domain . get_ring ( ) ) cq , f = f . clear_denoms ( ) cp , g = g . clear_denoms ( ) f = f . set_ring ( new_ring ) g = g . set_ring ( new_ring ) _ , p , q = f . cofactors ( g ) _ , cp , cq = new_ring . domain . cofactors ( cp , cq ) p = p . set_ring ( ring ) q = q . set_ring ( ring ) p_neg = p . is_negative q_neg = q . is_negative if p_neg and q_neg : p , q = - p , - q elif p_neg : cp , p = - cp , - p elif q_neg : cp , q = - cp , - q p = p . mul_ground ( cp ) q = q . mul_ground ( cq ) return p , q