假设分类
- 与简化符号相关,例如
Q.positive,Q.even。
- 与代数场/环有关,例如
Q.real, Q.complex.
- 与一些事实有关,例如.
is_bounded、is_infinity、is_zero 和
很快。它们帮助我们在核心中进行计算。
显然它们是从上述其他类派生的
假设(在启动对象时)(在这种情况下,例如
is_zero is 意味着它是环的零)。或者他们可以是
源自表达式的分析:在这种情况下,我们可以创建一些
计算的假设类别(在这种情况下 is_zero 可以是
意味着但是这种计算是困难的所谓的“零
测试”问题)。无论如何,我们可以意识到我们所处理的
确切地说(现在在我们使用 is_zero 在第二个意义上的核心某处)。
假设示例:
M ... Mathematica
S0 ... SymPy, current approach
S1 .... SymPy, approach 1
S2 .... SymPy, approach 2
M: Simplify[1/Sqrt[x] - Sqrt[1/x], x > 0]
S1: x = Symbol('x', assumptions=IsPositive)
simplify(1/sqrt(x) - sqrt(1/x))
S2: simplify(1/sqrt(x) - sqrt(1/x), Assumptions(x>0))
M: FunctionExpand[Log[x y], x > 0 && y > 0]
S1: x, y = Symbol('x', assumptions=IsPositive), Symbol('y', assumptions=IsPositive)
log(x*y).expand()
S2: log(x*y).expand(Assumptions([x>0, y>0]))
M: Simplify[Sin[n Pi], n \[Element] Integers]
S1: n = Symbol('n', assumptions=IsInteger)
simplify(sin(n*pi))
S2: simplify(sin(n*pi), Assumptions(Element(n, Integer)))
# we can talk about the syntax of Element(n, Integer)
M: FunctionExpand[EulerPhi[m n], {m, n} \[Element] Integers && GCD[m, n] == 1]
S1: n = Symbol('n', assumptions=IsInteger)
m = Symbol('m', assumptions=IsInteger)
n.assumptions.add(Eq(gcd(m, n) - 1))
euler_phi(m, n)
S2: euler_phi(m, n).expand(Assumptions([Element(n, Integer), Element(m, Integer), Eq(gcd(m, n) - 1)]))
# again we can talk about the syntax of Element(n, Integer)
M: RealQ[x, x \[Element] Real]
S0: x = Symbol('x',real=True, integer=True)
assert x.is_real == True
S1:
S2: assert IsElement(x, Real, assumptions=Element(x, Real))
M: Refine[Abs[x], x>0]
Refine[Abs[x], x0))
print e.refine(Assumptions(x))
更多参考资料:
Wiki Sympi Assumptions
Assuming
Setting Assumptions on Variables in Sympy Relative to Other Variables
Using SymPy's New Assumptions