假定#
A module which implements predicates and assumption context.
- class sympy.assumptions.assume.AppliedPredicate(predicate, *args)[源代码]#
The class of expressions resulting from applying
Predicate
to the arguments.AppliedPredicate
merely wraps its argument and remain unevaluated. To evaluate it, use theask()
function.实例
>>> from sympy import Q, ask >>> Q.integer(1) Q.integer(1)
The
function
attribute returns the predicate, and thearguments
attribute returns the tuple of arguments.>>> type(Q.integer(1)) <class 'sympy.assumptions.assume.AppliedPredicate'> >>> Q.integer(1).function Q.integer >>> Q.integer(1).arguments (1,)
Applied predicates can be evaluated to a boolean value with
ask
:>>> ask(Q.integer(1)) True
- property arg#
返回此假设使用的表达式。
实例
>>> from sympy import Q, Symbol >>> x = Symbol('x') >>> a = Q.integer(x + 1) >>> a.arg x + 1
- property arguments#
Return the arguments which are applied to the predicate.
- property function#
Return the predicate.
- class sympy.assumptions.assume.AssumptionsContext[源代码]#
Set containing default assumptions which are applied to the
ask()
function.解释
它用于表示全局假设,但也可以使用此类创建自己的本地假设上下文。它基本上是Python集合的一个瘦包装器,因此请参阅其文档以了解高级用法。
实例
The default assumption context is
global_assumptions
, which is initially empty:>>> from sympy import ask, Q >>> from sympy.assumptions import global_assumptions >>> global_assumptions AssumptionsContext()
You can add default assumptions:
>>> from sympy.abc import x >>> global_assumptions.add(Q.real(x)) >>> global_assumptions AssumptionsContext({Q.real(x)}) >>> ask(Q.real(x)) True
And remove them:
>>> global_assumptions.remove(Q.real(x)) >>> print(ask(Q.real(x))) None
The
clear()
method removes every assumption:>>> global_assumptions.add(Q.positive(x)) >>> global_assumptions AssumptionsContext({Q.positive(x)}) >>> global_assumptions.clear() >>> global_assumptions AssumptionsContext()
参见
- class sympy.assumptions.assume.Predicate(*args, **kwargs)[源代码]#
Base class for mathematical predicates. It also serves as a constructor for undefined predicate objects.
解释
Predicate is a function that returns a boolean value [1].
Predicate function is object, and it is instance of predicate class. When a predicate is applied to arguments,
AppliedPredicate
instance is returned. This merely wraps the argument and remain unevaluated. To obtain the truth value of applied predicate, use the functionask
.Evaluation of predicate is done by multiple dispatching. You can register new handler to the predicate to support new types.
Every predicate in SymPy can be accessed via the property of
Q
. For example,Q.even
returns the predicate which checks if the argument is even number.To define a predicate which can be evaluated, you must subclass this class, make an instance of it, and register it to
Q
. After then, dispatch the handler by argument types.If you directly construct predicate using this class, you will get
UndefinedPredicate
which cannot be dispatched. This is useful when you are building boolean expressions which do not need to be evaluated.实例
Applying and evaluating to boolean value:
>>> from sympy import Q, ask >>> ask(Q.prime(7)) True
You can define a new predicate by subclassing and dispatching. Here, we define a predicate for sexy primes [2] as an example.
>>> from sympy import Predicate, Integer >>> class SexyPrimePredicate(Predicate): ... name = "sexyprime" >>> Q.sexyprime = SexyPrimePredicate() >>> @Q.sexyprime.register(Integer, Integer) ... def _(int1, int2, assumptions): ... args = sorted([int1, int2]) ... if not all(ask(Q.prime(a), assumptions) for a in args): ... return False ... return args[1] - args[0] == 6 >>> ask(Q.sexyprime(5, 11)) True
Direct constructing returns
UndefinedPredicate
, which can be applied but cannot be dispatched.>>> from sympy import Predicate, Integer >>> Q.P = Predicate("P") >>> type(Q.P) <class 'sympy.assumptions.assume.UndefinedPredicate'> >>> Q.P(1) Q.P(1) >>> Q.P.register(Integer)(lambda expr, assump: True) Traceback (most recent call last): ... TypeError: <class 'sympy.assumptions.assume.UndefinedPredicate'> cannot be dispatched.
工具书类
- eval(args, assumptions=True)[源代码]#
Evaluate
self(*args)
under the given assumptions.这只使用直接解析方法,不使用逻辑推理。
- handler = <dispatched AskPredicateHandler>#
- class sympy.assumptions.assume.UndefinedPredicate(name, handlers=None)[源代码]#
Predicate without handler.
解释
This predicate is generated by using
Predicate
directly for construction. It does not have a handler, and evaluating this with arguments is done by SAT solver.实例
>>> from sympy import Predicate, Q >>> Q.P = Predicate('P') >>> Q.P.func <class 'sympy.assumptions.assume.UndefinedPredicate'> >>> Q.P.name Str('P')