Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
from __future__ import print_function, division
from sympy.core.numbers import nan from .function import Function
class Mod(Function): """Represents a modulo operation on symbolic expressions.
Receives two arguments, dividend p and divisor q.
The convention used is the same as Python's: the remainder always has the same sign as the divisor.
Examples ========
>>> from sympy.abc import x, y >>> x**2 % y Mod(x**2, y) >>> _.subs({x: 5, y: 6}) 1
"""
@classmethod def eval(cls, p, q):
"""Try to return p % q if both are numbers or +/-p is known to be less than or equal q. """
return nan p.is_Pow and p.exp.is_Integer and p.base == q or p.is_integer and q == 1): return S.Zero
return (p % q) return S.Zero return S.One
# by ratio else:
# by difference if q.is_negative: return d elif q.is_positive: return p
# denest # easy # XXX other possibilities?
# extract gcd; any further simplification should be done by the user p, q = [ gcd_terms(i/G, clear=False, fraction=False) for i in (p, q)]
# simplify terms # (x + y + 2) % x -> Mod(y + 2, x) args = [] for i in p.args: a = cls(i, q) if a.count(cls) > i.count(cls): args.append(i) else: args.append(a) if args != list(p.args): p = Add(*args)
else: # handle coefficients if they are not Rational # since those are not handled by factor_terms # e.g. Mod(.6*x, .3*y) -> 0.3*Mod(2*x, y) r = cp % cq if r == 0: G *= cq p *= int(cp/cq) ok = True
# simple -1 extraction G, p, q = [-i for i in (G, p, q)]
# check again to see if p and q can now be handled as numbers return rv*G
# put 1.0 from G on inside p *= G return cls(p, q, evaluate=False) p = G.args[0]*p G = Mul._from_args(G.args[1:])
def _eval_is_integer(self): return True
def _eval_is_nonnegative(self):
def _eval_is_nonpositive(self): return True |