同情中真正有价值的假设

时间:2018-01-14 19:40:19

标签: python 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 

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

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

然后是一堆追溯信息。显然,这些假设会以某种方式发生冲突,我必须误解它们的含义。

如果我尝试确保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

这清楚地表明,它将此视为一个复杂的价值。

我做错了吗?我真的可以相信real=True假设变量没有想象的部分吗?

2 个答案:

答案 0 :(得分:4)

sympy手册中明确说明了这一点:

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

特别是

  

每个实数都很复杂。

从数学的角度来看,这是有道理的。这些&#34;假设&#34;用于表示符号的属性,在数学意义上。因此,如果一个数字是真实的,那意味着它属于所有Reals的集合,它是Complex平面的一个子集。因此,每一个真实都是一个复杂的,Real=True就是这个。你可以使用 renderOptionFieldSOF( props : Object, fieldName : String, optionItems : Array, disabled : Boolean, placeHolderText : String) { const {width} = Dimensions.get("window"); const buttonStyle = {backgroundColor: disabled ? "#rgb(200,200,200)" : "transparent" , width: width - 20}; return (<Picker mode="dialog" enabled={!disabled} /*This works only on Android*/ renderButton={ ({onPress, text, picker, selectedItem }) => { return ( <Button style={buttonStyle} dark picker transparent onPress={onPress} disabled={disabled} /*This will disable user interaction on IOS*/ > {selectedItem ? ( <Text style={this.props.textStyle} note={this.props.note}> {text} </Text> ) : ( <Text style={[this.props.textStyle, this.props.placeholderStyle]} note={this.props.note === false ? false : true}> {placeHolderText} </Text> )} </Button> )} } > { optionItems && optionItems.map( (item) => (<Item label={item.Name} value={item.Key} key={item.Key}/>)) } </Picker>);}

答案 1 :(得分:2)

要回答第二个问题,conjugate函数没有对其输入进行任何类型的检查,因为它的非复数输入无论如何都没有意义,它假设没有人会传递一个不是一个复数的值。

非复杂符号的一个例子是非交换变量

>>> A, B = symbols("A B", commutative=False)
>>> A.is_complex
False
>>> A*B
A*B

(顺便说一句,检查对象假设的正确方法是使用is_方法,如is_complex)。