Source code for sympy.assumptions.assume

[docs]classAssumptionsContext(set):"""Set representing assumptions. This is used to represent global assumptions, but you can also use this class to create your own local assumptions contexts. It is basically a thin wrapper to Python's set, so see its documentation for advanced usage. Examples ======== >>> from sympy import global_assumptions, AppliedPredicate, Q >>> global_assumptions AssumptionsContext() >>> from sympy.abc import x >>> global_assumptions.add(Q.real(x)) >>> global_assumptions AssumptionsContext([Q.real(x)]) >>> global_assumptions.remove(Q.real(x)) >>> global_assumptions AssumptionsContext() >>> global_assumptions.clear() """

[docs]defadd(self,*assumptions):"""Add an assumption."""forainassumptions:super(AssumptionsContext,self).add(a)

[docs]defeval(self,expr,assumptions=True):""" Evaluate self(expr) under the given assumptions. This uses only direct resolution methods, not logical inference. """res,_res=None,Nonemro=inspect.getmro(type(expr))forhandlerinself.handlers:cls=get_class(handler)forsubclassinmro:try:eval=getattr(cls,subclass.__name__)exceptAttributeError:continueres=eval(expr,assumptions)if_resisNone:_res=reselifresisNone:# since first resolutor was conclusive, we keep that valueres=_reselse:# only check consistency if both resolutors have concludedif_res!=res:raiseValueError('incompatible resolutors')breakreturnres