sympy 中的实值假设

Eri*_*sen 5 python sympy

我正在尝试在 sympy 中创建实值变量,因为只有实部并且不复杂,没有虚部。我使用的是 sympy 1.0、IPython 5.3.0 和 Python 2.7.13。

In [48]: x = Symbol('x', real=True)
In [49]: x.assumptions0
Out[49]: 
{'commutative': True, 
  'complex': True, 
  'hermitian': True, 
  'imaginary': False, 
  'real': True}
In [50]: x * conjugate(x)
Out[50]: 
 2
x 
Run Code Online (Sandbox Code Playgroud)

结果是正确的,但它同时表示complexrealare True,这让我担心未来的结果。如果我试图让它真实但不复杂,我会得到:

In [51]: x = Symbol('x', real=True, complex=False)
---------------------------------------------------------------------------
InconsistentAssumptions                   Traceback (most recent call last)
Run Code Online (Sandbox Code Playgroud)

然后是一堆回溯信息。显然,这些假设在某种程度上相互冲突,我一定是误解了它们的含义。

如果我试图确保complex=False我得到:

In [52]: x = Symbol('x', complex=False)
In [53]: x.assumptions0
Out[53]: 
{'algebraic': False,
 'commutative': True,
 'complex': False,
 'composite': False,
 'even': False,
 'imaginary': False,
 'integer': False,
 'irrational': False,
 'negative': False,
 'noninteger': False,
 'nonnegative': False,
 'nonpositive': False,
 'nonzero': False,
 'odd': False,
 'positive': False,
 'prime': False,
 'rational': False,
 'real': False,
 'transcendental': False,
 'zero': False}
In [54]: x * conjugate(x)
Out[54]: 
  _
x?x
Run Code Online (Sandbox Code Playgroud)

这清楚地表明它把它当作一个复杂的值来对待。

难道我做错了什么?我真的可以相信real=True假设变量没有虚部吗?

kab*_*nus 7

这在sympy手册中有明确说明,这里:

http://docs.sympy.org/latest/modules/assumptions/ask.html?highlight=real#sympy.assumptions.ask.AssumptionKeys.real

特别是,

每个实数也是复数。

从数学的角度来看,这是有道理的。在数学意义上,这些“假设”旨在表示符号的属性。所以如果一个数是实数,那意味着它属于所有实数的集合,它是复平面的一个子集。因此,每一个实数都是一个复合体,并sympy依附于此。你应该没问题Real=True