<s> from __future__ import print_function , division <EOL> from sympy . core . numbers import nan <EOL> from . function import Function <EOL> class Mod ( Function ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def eval ( cls , p , q ) : <EOL> from sympy . core . add import Add <EOL> from sympy . core . mul import Mul <EOL> from sympy . core . singleton import S <EOL> from sympy . core . exprtools import gcd_terms <EOL> from sympy . polys . polytools import gcd <EOL> def doit ( p , q ) : <EOL> """<STR_LIT>""" <EOL> if p . is_infinite or q . is_infinite or p is nan or q is nan : <EOL> return nan <EOL> if ( p == q or p == - q or <EOL> p . is_Pow and p . exp . is_Integer and p . base == q or <EOL> p . is_integer and q == <NUM_LIT:1> ) : <EOL> return S . Zero <EOL> if q . is_Number : <EOL> if p . is_Number : <EOL> return ( p % q ) <EOL> if q == <NUM_LIT:2> : <EOL> if p . is_even : <EOL> return S . Zero <EOL> elif p . is_odd : <EOL> return S . One <EOL> r = p / q <EOL> try : <EOL> d = int ( r ) <EOL> except TypeError : <EOL> pass <EOL> else : <EOL> if type ( d ) is int : <EOL> rv = p - d * q <EOL> if ( rv * q < <NUM_LIT:0> ) == True : <EOL> rv += q <EOL> return rv <EOL> d = p - q <EOL> if d . is_negative : <EOL> if q . is_negative : <EOL> return d <EOL> elif q . is_positive : <EOL> return p <EOL> rv = doit ( p , q ) <EOL> if rv is not None : <EOL> return rv <EOL> if p . func is cls : <EOL> qinner = p . args [ <NUM_LIT:1> ] <EOL> if qinner == q : <EOL> return p <EOL> G = gcd ( p , q ) <EOL> if G != <NUM_LIT:1> : <EOL> p , q = [ <EOL> gcd_terms ( i / G , clear = False , fraction = False ) for i in ( p , q ) ] <EOL> pwas , qwas = p , q <EOL> if p . is_Add : <EOL> args = [ ] <EOL> for i in p . args : <EOL> a = cls ( i , q ) <EOL> if a . count ( cls ) > i . count ( cls ) : <EOL> args . append ( i ) <EOL> else : <EOL> args . append ( a ) <EOL> if args != list ( p . args ) : <EOL> p = Add ( * args ) <EOL> else : <EOL> cp , p = p . as_coeff_Mul ( ) <EOL> cq , q = q . as_coeff_Mul ( ) <EOL> ok = False <EOL> if not cp . is_Rational or not cq . is_Rational : <EOL> r = cp % cq <EOL> if r == <NUM_LIT:0> : <EOL> G *= cq <EOL> p *= int ( cp / cq ) <EOL> ok = True <EOL> if not ok : <EOL> p = cp * p <EOL> q = cq * q <EOL> if p . could_extract_minus_sign ( ) and q . could_extract_minus_sign ( ) : <EOL> G , p , q = [ - i for i in ( G , p , q ) ] <EOL> rv = doit ( p , q ) <EOL> if rv is not None : <EOL> return rv * G <EOL> if G . is_Float and G == <NUM_LIT:1> : <EOL> p *= G <EOL> return cls ( p , q , evaluate = False ) <EOL> elif G . is_Mul and G . args [ <NUM_LIT:0> ] . is_Float and G . args [ <NUM_LIT:0> ] == <NUM_LIT:1> : <EOL> p = G . args [ <NUM_LIT:0> ] * p <EOL> G = Mul . _from_args ( G . args [ <NUM_LIT:1> : ] ) <EOL> return G * cls ( p , q , evaluate = ( p , q ) != ( pwas , qwas ) ) <EOL> def _eval_is_integer ( self ) : <EOL> from sympy . core . logic import fuzzy_and , fuzzy_not <EOL> p , q = self . args <EOL> if fuzzy_and ( [ p . is_integer , q . is_integer , fuzzy_not ( q . is_zero ) ] ) : <EOL> return True <EOL> def _eval_is_nonnegative ( self ) : <EOL> if self . args [ <NUM_LIT:1> ] . is_positive : <EOL> return True <EOL> def _eval_is_nonpositive ( self ) : <EOL> if self . args [ <NUM_LIT:1> ] . is_negative : <EOL> return True </s>
