Sympy:用式发言替代表达式
我正在使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
下
在这些情况 。使用命令
U.Subs(bra(pi)*ket(mu),theta)
您要求搜索一个类型innerproduct
的对象u
/code>,但没有,因此没有替代。
在这种情况下,您必须执行:
编辑:或正如@oscar Benjamin指出的那样,您要这样做:
现在,您可以看到
innerproduct
作为mul
的参数。最后:In these occasions
srepr
can shed some light:Note that the first output is a multiplication, object of type
Mul
, whereas the second output is an object of typeInnerProduct
. With the commandU.subs(Bra(pi)*Ket(mu),theta)
you are asking to search for an object of typeInnerProduct
intoU
, but there is none, hence no substitution has been done.In this case, you have to do:
Edit: or as @Oscar Benjamin pointed out, you do:
Now you can see an
InnerProduct
as an argument ofMul
. FInally: