假设#
A module to implement logical predicates and assumption system.
Predicate#
- 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>
- classmethod register(*types, **kwargs)[源代码]
Register the signature to the handler.
- classmethod register_many(*types, **kwargs)[源代码]
Register multiple signatures to same handler.
- 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.
查询#
Queries are used to ask information about expressions. Main method for this
is ask()
:
- sympy.assumptions.ask.ask(proposition, assumptions=True, context={})[源代码]
Function to evaluate the proposition with assumptions.
- 参数:
proposition : Boolean
Proposition which will be evaluated to boolean value. If this is not
AppliedPredicate
, it will be wrapped byQ.is_true
.assumptions : Boolean, optional
Local assumptions to evaluate the proposition.
context : AssumptionsContext, optional
Default assumptions to evaluate the proposition. By default, this is
sympy.assumptions.global_assumptions
variable.- 返回:
True
,False
, orNone
- 加薪:
TypeError : proposition or assumptions is not valid logical expression.
ValueError : assumptions are inconsistent.
解释
This function evaluates the proposition to
True
orFalse
if the truth value can be determined. If not, it returnsNone
.It should be discerned from
refine()
which, when applied to a proposition, simplifies the argument to symbolicBoolean
instead of Python built-inTrue
,False
orNone
.Syntax
- 提问(命题)
Evaluate the proposition in global assumption context.
- 提问(命题、假设)
Evaluate the proposition with respect to assumptions in global assumption context.
实例
>>> from sympy import ask, Q, pi >>> from sympy.abc import x, y >>> ask(Q.rational(pi)) False >>> ask(Q.even(x*y), Q.even(x) & Q.integer(y)) True >>> ask(Q.prime(4*x), Q.integer(x)) False
If the truth value cannot be determined,
None
will be returned.>>> print(ask(Q.odd(3*x))) # cannot determine unless we know x None
ValueError
is raised if assumptions are inconsistent.>>> ask(Q.integer(x), Q.even(x) & Q.odd(x)) Traceback (most recent call last): ... ValueError: inconsistent assumptions Q.even(x) & Q.odd(x)
笔记
假设中的关系还没有实现,所以下面的结果不会有意义。
>>> ask(Q.positive(x), x > 0)
然而,这是一项正在进行的工作。
参见
sympy.assumptions.refine.refine
Simplification using assumptions. Proposition is not reduced to
None
if the truth value cannot be determined.
ask
's optional second argument should be a boolean expression involving
assumptions about objects in expr. Valid values include:
Q.integer(x)
Q.positive(x)
Q.integer(x) & Q.positive(x)
等。
Q
is an object holding known predicates.
有关有效布尔表达式的完整列表,请参阅逻辑模块的文档。
You can also define a context so you don't have to pass that argument
each time to function ask()
. This is done by using the assuming context manager
from module sympy.assumptions.
>>> from sympy import *
>>> x = Symbol('x')
>>> y = Symbol('y')
>>> facts = Q.positive(x), Q.positive(y)
>>> with assuming(*facts):
... print(ask(Q.positive(2*x + y)))
True
目录#
性能改进#
在涉及符号系数的查询中,使用逻辑推理。提高可满足性功能的工作(同感逻辑推理.可满足)应导致显著的速度改进。
在一个ask中使用的逻辑推理可以用来加速进一步的查询,而当前的系统并没有利用这一点。例如,真相维护系统(https://en.wikipedia.org/wiki/Truth_维护_系统)可以实现。
误码率#
You can find more examples in the form of tests in the directory
sympy/assumptions/tests/