英文:
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.
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.
英文:
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.
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.
答案1
得分: 1
SymPy尚不足够聪明,无法理解exp(positive) - 1).is_nonegative -> None
应该为True。因此,让我们通过将指数替换为1 + nonnegative
来帮助它,然后撤消替换:
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
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论