Real valued assumption in sympy

Question:

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?

Asked By: Eric C.

||

Answers:

This is clearly stated in the sympy manual, here:

https://docs.sympy.org/latest/guides/assumptions.html#id28

In particular, the table lists "complex" as an implication of "real"

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.

Answered By: 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).

Answered By: asmeurer
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.