How to specify a non-negative real number in sympy

Viewed 974

I'm doing some large-ish bunch of operations in symbolic math, which is taking too long (for my taste), and I wanted to test if baking some assumptions into the symbols might speed things up a bit. The independent variable is t.

However, I find this:

>>> t = sympy.Symbol('t', positive=True)
>>> t.is_complex
True

Does this mean that Sympy assumes complex values? At the same time:

>>> t.is_real
True

What? Is this now a real or a complex number? Let's try and make it explicit:

>>> t = sympy.Symbol('t', negative=False, real=True, complex=False)
*** InconsistentAssumptions: {'real': True, 'imaginary': False, 'hermitian': True, 'complex': True, 'commutative': True}, complex=False

So ... I am clearly not understanding how the symbol definition works. I would like a positive real number, i.e. not a complex number, but for some reason Sympy seems to insist that it must be complex, and also a Hermitian matrix? Or are real scalars special cases of (1-element) Hermitian matrices? I don't think I had enough Math lessons to know that

I'm also not sure if positive=True includes the possibility for t to be zero. The documentation doesn't seem to explain what exactly the arguments mean or how they are interpreted, either.

In case this is relevant: I am using SYmpy 0.7.6. I know that this is fairly old, but upgrading is not an option because the program needs to run on machines whose update schedule is not my decision...

1 Answers

All real numbers are complex but not all real numbers have an imaginary component; the imaginary part of a real number is 0 (but this doesn't make the number not complex):

>>> Symbol('x', real=True).as_real_imag()
(x, 0)

Since all reals are complex you can't say that it is both real and not complex. It might help to think about reals, rationals and integers: all integers are rational (with denominator of 1) but not all rationals are integers. All rationals are real, but not all reals are rational.

The positive, nonnegative, zero, nonpositive and negative designations have their usual meanings. You can always ask SymPy to tell you how it views a number to get a better feel for the types:

>>>> from sympy import Tuple
>>> [i.is_positive for i in Tuple(-1, 0, 1)]
[False, True, True]
>>> [i.is_nonnegative for i in Tuple(-1, 0, 1)]
[False, True, True]
Related