Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Real valued assumption in sympy

Tags:

python

sympy

I'm trying to create variables in sympy that are real-valued, as in only have a real part and are not complex, no imaginary part. I'm on sympy version 1.0, IPython 5.3.0 and 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 

The result is correct, but it says both complex and real are True, which makes me worry about future results. If I try to make it real but not complex I get:

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

Then a bunch of traceback information. Obviously these assumptions conflict somehow, and I must be misunderstanding the meaning of them.

If I try to make sure that complex=False I get:

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

Which clearly shows that it's treating this as if it can be a complex value.

Am I doing something wrong? Can I actually trust real=True to assume that there is no imaginary part to variables?

like image 745
Eric C. Avatar asked Jan 14 '18 19:01

Eric C.


2 Answers

This is clearly stated in the sympy manual, here:

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

In particular,

Every real number is also complex.

This makes sense from a Mathematical point of view. These "assumptions" are meant to represent attributes of the symbol, in the mathematical sense. So if a number is a real, that means it belongs in the set of all Reals, which is a subset of the Complex plane. Thus, every real is a complex, and sympy adhers to this. You should be fine with Real=True.

like image 100
kabanus Avatar answered Sep 20 '22 23:09

kabanus


To answer the second question, the conjugate function is not doing any kind of type checking on its input, as a non-complex input to it doesn't really make sense anyway, it's assumed that no one will pass it a value that isn't a complex number.

An example of a non-complex Symbol would be a noncommutative variable

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

(and by the way, the correct way to check the assumptions on an object is to use the is_ methods, like is_complex).

like image 22
asmeurer Avatar answered Sep 19 '22 23:09

asmeurer