Search code examples
pythonsympyexponent

Sympy not recognizing expression as real


I've specified two variables, gamma and t, as real and positive, and they are used in an expression as follows:

gamma, t = symbols('gamma t', real=True, positive=True)
expr = sqrt((exp(gamma*t))-1)

result = expr*conjugate(expr)

The problem I'm having is that when I multiply the expression by its complex conjugate (using sp.conjugate), I don't get the answer I'm expecting.

Expected Result

Actual Result

In case those don't show up or something, what I'm expecting looks like exp(gamma*t))-1 while the result I get is sqrt((exp(gamma*t))-1)*sqrt((exp(gamma*t))-1) where the second sqrt term has a bar over it, indicating it is complex.

I'm expecting the simplified term since gamma and t are both specified to be positive and real, meaning the expression under the root can't be negative, so the whole expression should remain real no matter what values of gamma and t I use.

This is my first question, so apologies if the format is wrong.

I've tried using simplify, which doesn't work. I've tried specifying the whole expression as real, but I'm new to Sympy and don't think/know if that is possible.

There's another question posted that is similar, but the solution there did not work for this situation.


Solution

  • SymPy is not (yet) insightful enough to know that exp(positive) - 1).is_nonegative -> None should be True. So let's help it by replacing the exponential with 1 + nonnegative and then undoing the replacement:

    >>> from sympy import Dummy
    >>> nn= Dummy(nonnegative=True)
    >>> x = exp(gamma*t)
    >>> result.subs(x, nn + 1).subs(nn, x- 1)
    exp(gamma*t) - 1