#NEWLINE# def cancel( self , g ) : #NEWLINE# #INDENT# '\n        Cancel common factors in a rational function ``f/g``.\n\n        Examples\n        ========\n\n        >>> from sympy.polys import ring, ZZ\n        >>> R, x,y = ring("x,y", ZZ)\n\n        >>> (2*x**2 - 2).cancel(x**2 - 2*x + 1)\n        (2*x + 2, x - 1)\n\n        ' #NEWLINE# f = self #NEWLINE# ring = f . ring #NEWLINE# if ( not f ) : #NEWLINE# #INDENT# return ( g , ring . one ) #NEWLINE# #UNINDENT# domain = ring . domain #NEWLINE# if ( not ( domain . has_Field and domain . has_assoc_Ring ) ) : #NEWLINE# #INDENT# ( _ , p , q ) = f . cofactors ( g ) #NEWLINE# if q . is_negative : #NEWLINE# #INDENT# ( p , q ) = ( ( - p ) , ( - q ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# new_ring = ring . clone ( domain = domain . get_ring ( ) ) #NEWLINE# ( cq , f ) = f . clear_denoms ( ) #NEWLINE# ( cp , g ) = g . clear_denoms ( ) #NEWLINE# f = f . set_ring ( new_ring ) #NEWLINE# g = g . set_ring ( new_ring ) #NEWLINE# ( _ , p , q ) = f . cofactors ( g ) #NEWLINE# ( _ , cp , cq ) = new_ring . domain . cofactors ( cp , cq ) #NEWLINE# p = p . set_ring ( ring ) #NEWLINE# q = q . set_ring ( ring ) #NEWLINE# p_neg = p . is_negative #NEWLINE# q_neg = q . is_negative #NEWLINE# if ( p_neg and q_neg ) : #NEWLINE# #INDENT# ( p , q ) = ( ( - p ) , ( - q ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if p_neg : #NEWLINE# #INDENT# ( cp , p ) = ( ( - cp ) , ( - p ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if q_neg : #NEWLINE# #INDENT# ( cp , q ) = ( ( - cp ) , ( - q ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# p = p . mul_ground ( cp ) #NEWLINE# q = q . mul_ground ( cq ) #NEWLINE# #UNINDENT# return ( p , q )