Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在辛方程中计算矩阵的幂?_Python_Sympy_Factorization - Fatal编程技术网

Python 如何在辛方程中计算矩阵的幂?

Python 如何在辛方程中计算矩阵的幂?,python,sympy,factorization,Python,Sympy,Factorization,我有一个来自于 from sympy.physics.quantum import Commutator as cmm x, t, A, V, W, D = sp.symbols('x t A V W D', commutative = False) Q = sp.Function('Q', commutative = False) F = (sp.diff(Q(x,t),x)+ cmm(W,Q(x,t)).doit() - sp.I*cmm(A,Q(x,t)+ cmm

我有一个来自于

from sympy.physics.quantum import Commutator as cmm

    x, t, A, V, W, D = sp.symbols('x t A V W D', commutative = False)
    Q = sp.Function('Q', commutative = False)
    F = (sp.diff(Q(x,t),x)+ cmm(W,Q(x,t)).doit() - sp.I*cmm(A,Q(x,t)+ cmm(W,Q(x,t)).doit()).doit())*(sp.diff(Q(x,t),x)+ cmm(W,Q(x,t)).doit() - sp.I*cmm(A,Q(x,t)+ cmm(W,Q(x,t)).doit()).doit())
    F.expand()
这给了我一个表达式,其中的元素在W中为零级,在W中为一级,在W中为二级。我只想要第一份W的。我尝试了因子分解过程,但由于没有交换的事实,它似乎不承认W的幂,它总是给我0。有什么简单的方法可以做到这一点吗?当然我可以用手做,但这不是我的目标


谢谢

您可以在遍历F的参数时收集W中一阶的所有术语:

>>> from sympy import Add
>>> first_order_terms = []
>>> for i in Add.make_args(F.expand()):
...   if i == W or i.is_Mul and i.has(W) and i.subs(W,y).as_independent(y)[1] == y:
...     first_order_terms.append(i)
... 
>>> Add(*first_order_terms)
-A*W*Q(x, t)*A*Q(x, t) - I*A*W*Q(x, t)*Derivative(Q(x, t), x) + A*W*Q(x, t)**2*A - 
A*Q(x, t)*A*W*Q(x, t) + A*Q(x, t)*A*Q(x, t)*W + A*Q(x, t)*W*A*Q(x, t) - I*A*Q(x, 
t)*W*Q(x, t) + I*A*Q(x, t)*W*Derivative(Q(x, t), x) + I*A*Q(x, t)**2*W - A*Q(x, 
t)**2*W*A - I*W*Q(x, t)*A*Q(x, t) - W*Q(x, t)*A*Q(x, t)*A + I*W*Q(x, 
t)*A*Derivative(Q(x, t), x) + W*Q(x, t)*A**2*Q(x, t) + W*Q(x, t)*Derivative(Q(x, t), 
x) + I*W*Q(x, t)**2*A + I*Q(x, t)*A*W*Q(x, t) - Q(x, t)*A*W*Q(x, t)*A - I*Q(x, 
t)*A*Q(x, t)*W + Q(x, t)*A*Q(x, t)*W*A + Q(x, t)*A**2*W*Q(x, t) - Q(x, t)*A**2*Q(x, 
t)*W + I*Q(x, t)*W*A*Q(x, t) + Q(x, t)*W*A*Q(x, t)*A - I*Q(x, t)*W*A*Derivative(Q(x, 
t), x) - Q(x, t)*W*A**2*Q(x, t) - I*Q(x, t)*W*Q(x, t)*A - Q(x, t)*W*Derivative(Q(x, 
t), x) - I*Derivative(Q(x, t), x)*A*W*Q(x, t) + I*Derivative(Q(x, t), x)*A*Q(x, t)*W + 
Derivative(Q(x, t), x)*W*Q(x, t) + I*Derivative(Q(x, t), x)*W*Q(x, t)*A - 
Derivative(Q(x, t), x)*Q(x, t)*W - I*Derivative(Q(x, t), x)*Q(x, t)*W*A