gpt4 book ai didi

python - 在 sympy 中,使用规范交换关系简化表达式

转载 作者:行者123 更新时间:2023-12-05 02:57:55 26 4
gpt4 key购买 nike

我有一个 ladder operator â,满足 commutator relation有自己的伴随词:

[â, â⁺] = 1

在 sympy 中我写了这段代码:

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

a = Operator('a')
ad = Dagger(a)

ccr = Eq( Commutator(a, ad), 1 )

现在我需要像这样扩展和简化表达式:

(â⁺ + â)⁴

如果我只使用 ((ad + a)**4).expand(),则 sympy 不使用换向器关系。如何在使用正则换向器关系时简化表达式

最佳答案

我找不到任何内置的方法来做到这一点,所以我为它编写了一个非常基本的算法。它是这样使用的:

((ad + a)**4).expand().apply_ccr(ccr)

结果

3 + 12 a⁺ a + 4 a⁺ a³ + 6 a⁺² + 6 a⁺² a² + 4 a⁺³ a + a⁺⁴ + 6a² + a⁴

.

有一个名为 reverse 的可选参数,它会将表达式重新排列为首先是 a,然后是 a⁺。这是克服 sympy 的限制所必需的,它不允许您以不同的顺序指定 Commutator [source] .


这是apply_ccr的实现:

from sympy.core.operations import AssocOp

def apply_ccr(expr, ccr, reverse=False):
if not isinstance(expr, Basic):
raise TypeError("The expression to simplify is not a sympy expression.")

if not isinstance(ccr, Eq):
if isinstance(ccr, Basic):
ccr = Eq(ccr, 0)
else:
raise TypeError("The canonical commutation relation is not a sympy expression.")

comm = None

for node in preorder_traversal(ccr):
if isinstance(node, Commutator):
comm = node
break

if comm is None:
raise ValueError("The cannonical commutation relation doesn not include a commutator.")

solutions = solve(ccr, comm)

if len(solutions) != 1:
raise ValueError("There are more solutions to the cannonical commutation relation.")

value = solutions[0]

A = comm.args[0]
B = comm.args[1]

if reverse:
(A, B) = (B, A)
value = -value

def is_expandable_pow_of(base, expr):
return isinstance(expr, Pow) \
and base == expr.args[0] \
and isinstance(expr.args[1], Number) \
and expr.args[1] >= 1


def walk_tree(expr):
if isinstance(expr, Number):
return expr

if not isinstance(expr, AssocOp) and not isinstance(expr, Function):
return expr.copy()

elif not isinstance(expr, Mul):
return expr.func(*(walk_tree(node) for node in expr.args))

else:
args = [arg for arg in expr.args]

for i in range(len(args)-1):
x = args[i]
y = args[i+1]

if B == x and A == y:
args = args[0:i] + [A*B - value] + args[i+2:]
return walk_tree( Mul(*args).expand() )

if B == x and is_expandable_pow_of(A, y):
ypow = Pow(A, y.args[1] - 1)
args = args[0:i] + [A*B - value, ypow] + args[i+2:]
return walk_tree( Mul(*args).expand() )

if is_expandable_pow_of(B, x) and A == y:
xpow = Pow(B, x.args[1] - 1)
args = args[0:i] + [xpow, A*B - value] + args[i+2:]
return walk_tree( Mul(*args).expand() )

if is_expandable_pow_of(B, x) and is_expandable_pow_of(A, y):
xpow = Pow(B, x.args[1] - 1)
ypow = Pow(A, y.args[1] - 1)
args = args[0:i] + [xpow, A*B - value, ypow] + args[i+2:]
return walk_tree( Mul(*args).expand() )

return expr.copy()


return walk_tree(expr)


Basic.apply_ccr = lambda self, ccr, reverse=False: apply_ccr(self, ccr, reverse)

(No rights reserved.)

关于python - 在 sympy 中,使用规范交换关系简化表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59523322/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com