[docs]classAskPrimeHandler(CommonHandler):""" Handler for key 'prime' Test that an expression represents a prime number. When the expression is a number the result, when True, is subject to the limitations of isprime() which is used to return the result. """@staticmethoddef_number(expr,assumptions):# helper methodtry:i=int(expr.round())ifnot(expr-i).equals(0):raiseTypeErrorexceptTypeError:returnFalsereturnisprime(i)@staticmethoddefBasic(expr,assumptions):# Just use int(expr) once# http://code.google.com/p/sympy/issues/detail?id=1462# is solvedifexpr.is_number:returnAskPrimeHandler._number(expr,assumptions)@staticmethoddefMul(expr,assumptions):ifexpr.is_number:returnAskPrimeHandler._number(expr,assumptions)forarginexpr.args:ifask(Q.integer(arg),assumptions):passelse:breakelse:# a product of integers can't be a primereturnFalse@staticmethod

classAskCompositeHandler(CommonHandler):@staticmethoddefBasic(expr,assumptions):_positive=ask(Q.positive(expr),assumptions)if_positive:_integer=ask(Q.integer(expr),assumptions)if_integer:_prime=ask(Q.prime(expr),assumptions)if_primeisNone:returnreturnnot_primeelse:return_integerelse:return_positiveclassAskEvenHandler(CommonHandler):@staticmethoddef_number(expr,assumptions):# helper methodtry:i=int(expr.round())ifnot(expr-i).equals(0):raiseTypeErrorexceptTypeError:returnFalsereturni%2==0@staticmethoddefBasic(expr,assumptions):ifexpr.is_number:returnAskEvenHandler._number(expr,assumptions)@staticmethoddefMul(expr,assumptions):""" Even * Integer -> Even Even * Odd -> Even Integer * Odd -> ? Odd * Odd -> Odd Even * Even -> Even Integer * Integer -> Even if Integer + Integer = Odd -> ? otherwise """ifexpr.is_number:returnAskEvenHandler._number(expr,assumptions)even,odd,irrational,acc=False,0,False,1forarginexpr.args:# check for all integers and at least one evenifask(Q.integer(arg),assumptions):ifask(Q.even(arg),assumptions):even=Trueelifask(Q.odd(arg),assumptions):odd+=1elifnotevenandacc!=1:ifask(Q.odd(acc+arg),assumptions):even=Trueelifask(Q.irrational(arg),assumptions):# one irrational makes the result False# two makes it undefinedifirrational:breakirrational=Trueelse:breakacc=argelse:ifirrational:returnFalseifeven:returnTrueifodd==len(expr.args):returnFalse@staticmethoddefAdd(expr,assumptions):""" Even + Odd -> Odd Even + Even -> Even Odd + Odd -> Even """ifexpr.is_number:returnAskEvenHandler._number(expr,assumptions)_result=Trueforarginexpr.args:ifask(Q.even(arg),assumptions):passelifask(Q.odd(arg),assumptions):_result=not_resultelse:breakelse:return_result@staticmethoddefPow(expr,assumptions):ifexpr.is_number:returnAskEvenHandler._number(expr,assumptions)ifask(Q.integer(expr.exp),assumptions):ifask(Q.positive(expr.exp),assumptions):returnask(Q.even(expr.base),assumptions)elifask(~Q.negative(expr.exp)&Q.odd(expr.base),assumptions):returnFalseelifexpr.baseisS.NegativeOne:returnFalse@staticmethoddefInteger(expr,assumptions):returnnotbool(expr.p&1)Rational,Infinity,NegativeInfinity,ImaginaryUnit=[staticmethod(CommonHandler.AlwaysFalse)]*4@staticmethoddefFloat(expr,assumptions):returnexpr%2==0@staticmethoddefNumberSymbol(expr,assumptions):returnAskEvenHandler._number(expr,assumptions)@staticmethoddefAbs(expr,assumptions):ifask(Q.real(expr.args[0]),assumptions):returnask(Q.even(expr.args[0]),assumptions)@staticmethoddefre(expr,assumptions):ifask(Q.real(expr.args[0]),assumptions):returnask(Q.even(expr.args[0]),assumptions)@staticmethoddefim(expr,assumptions):ifask(Q.real(expr.args[0]),assumptions):returnTrue

[docs]classAskOddHandler(CommonHandler):""" Handler for key 'odd' Test that an expression represents an odd number """@staticmethoddefBasic(expr,assumptions):_integer=ask(Q.integer(expr),assumptions)if_integer:_even=ask(Q.even(expr),assumptions)if_evenisNone:returnNonereturnnot_evenreturn_integer