You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用sympy.physics.quantum Operator验证位置动量算符对易关系

SymPy位置与动量算符对易子简化方案

你遇到的问题是qapply执行后没有自动完成求导计算和公因子提取,只需要在qapply返回结果后追加doit()执行求导运算,再调用simplify()提取公共因子即可得到预期结果。

方案1:基于现有代码的修改版

from sympy import *
from sympy.physics.quantum import *

x, hbar, L = symbols("x hbar L")
psi = symbols("psi", cls=Function)

xhat = DifferentialOperator(x * psi(x), psi(x))
phat = DifferentialOperator(-I * hbar * Derivative(psi(x), x), psi(x))

w = Wavefunction(sqrt(2/L) * sin(pi*x/L), x)

# 追加doit执行求导,simplify整理表达式
res = qapply((xhat * phat - phat * xhat) * w).doit().simplify()
print(res)

运行后输出为I*hbar*sqrt(2/L)*sin(pi*x/L),即iℏ * w,符合对易子预期结果。

方案2:通用对易子证明(无需指定测试波函数)

如果不需要用特定波函数测试,直接对任意波函数作用对易子即可得到更简洁的证明结果:

from sympy import *
from sympy.physics.quantum import *

x, hbar = symbols("x hbar")
psi = Function("psi")(x)

x_op = DifferentialOperator(x * psi, psi)
p_op = DifferentialOperator(-I * hbar * diff(psi, x), psi)

commutator = x_op * p_op - p_op * x_op
res = commutator(psi).doit().simplify()
print(res)

运行后直接输出I*hbar*psi(x),直接验证[x, p] = iℏ的结论。

内容的提问来源于stack exchange,提问作者Leo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 07:45:03