Sympy:用式发言替代表达式

发布于 2025-01-24 21:21:38 字数 501 浏览 0 评论 0原文

我正在使用Sympy(在Sagemath中)。我想用刹车通用(用于量子机械问题)进行替代。下面有一个简约的代码,以重现问题。

from sympy.physics.quantum import Bra, Ket
from sympy import *
theta=symbols('theta',commutative=True)
pi, mu= symbols("pi mu",commutative=False)
W=2*pi*mu
print(W.subs(pi*mu,theta))
V=Bra(pi)*Ket(mu)
print(V.subs(Bra(pi)*Ket(mu),theta))
U=2*Bra(pi)*Ket(mu)
print(U.subs(Bra(pi)*Ket(mu),theta))

输出是:

2*theta
theta
2*<pi|*|mu>

如果没有领先的标量乘数,则替换可以很好地工作。我被更复杂的表达困住了。

I am using sympy (in sagemath). I would like to do a substitution, with Braket-notation (for quantum mechanical problem). Below there is a minimalistic code, in order to reproduce the problem.

from sympy.physics.quantum import Bra, Ket
from sympy import *
theta=symbols('theta',commutative=True)
pi, mu= symbols("pi mu",commutative=False)
W=2*pi*mu
print(W.subs(pi*mu,theta))
V=Bra(pi)*Ket(mu)
print(V.subs(Bra(pi)*Ket(mu),theta))
U=2*Bra(pi)*Ket(mu)
print(U.subs(Bra(pi)*Ket(mu),theta))

The output is:

2*theta
theta
2*<pi|*|mu>

If there is no leading scalar multiplier, the substitution works finely. I am stuck with a more complicated expression.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

于我来说 2025-01-31 21:21:38

srepr(U)
# out: "Mul(Integer(2), Bra(Symbol('pi', commutative=False)), Ket(Symbol('mu', commutative=False)))"

srepr(Bra(pi)*Ket(mu))
# out: "InnerProduct(Bra(Symbol('pi', commutative=False)),Ket(Symbol('mu', commutative=False)))"

在这些情况 。使用命令U.Subs(bra(pi)*ket(mu),theta)您要求搜索一个类型innerproduct的对象u /code>,但没有,因此没有替代。

在这种情况下,您必须执行:

U.subs(Mul(Bra(pi), Ket(mu)),theta)
# out: 2*theta

编辑:或正如@oscar Benjamin指出的那样,您要这样做:

from sympy.physics.quantum import qapply
U = qapply(U)
srepr(U)
# out: "Mul(Integer(2), InnerProduct(Bra(Symbol('pi', commutative=False)),Ket(Symbol('mu', commutative=False))))"

现在,您可以看到innerproduct作为mul的参数。最后:

U.subs(Bra(pi)*Ket(mu), theta)
# out: 2*theta

In these occasions srepr can shed some light:

srepr(U)
# out: "Mul(Integer(2), Bra(Symbol('pi', commutative=False)), Ket(Symbol('mu', commutative=False)))"

srepr(Bra(pi)*Ket(mu))
# out: "InnerProduct(Bra(Symbol('pi', commutative=False)),Ket(Symbol('mu', commutative=False)))"

Note that the first output is a multiplication, object of type Mul, whereas the second output is an object of type InnerProduct. With the command U.subs(Bra(pi)*Ket(mu),theta) you are asking to search for an object of type InnerProduct into U, but there is none, hence no substitution has been done.

In this case, you have to do:

U.subs(Mul(Bra(pi), Ket(mu)),theta)
# out: 2*theta

Edit: or as @Oscar Benjamin pointed out, you do:

from sympy.physics.quantum import qapply
U = qapply(U)
srepr(U)
# out: "Mul(Integer(2), InnerProduct(Bra(Symbol('pi', commutative=False)),Ket(Symbol('mu', commutative=False))))"

Now you can see an InnerProduct as an argument of Mul. FInally:

U.subs(Bra(pi)*Ket(mu), theta)
# out: 2*theta
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文