当 Mathematica 轻松成功时,SymPy 的零空间陷入困境
SymPy's nullspace struggling when Mathematica succeeds with ease
我一直在尝试使用命令 A.nullspace
在 SymPy 中找到符号矩阵 A
的零空间。现在,我输入的矩阵的计算没有完成(或者比我等待的时间更长)。
奇怪的是 Mathematica 在几分之一秒内完成了这个计算。我将给出我用作 Mathematica 输入代码的具体示例(应该 运行)
L = ({{0, -I a Sqrt[b] Conjugate[g] Conjugate[w], 0, I a Sqrt[b] g w,
2 g2 + 2 b g z Conjugate[g], 0, 0, 0,
2 g2}, {-I a Sqrt[b] g w, -g2 - b g z Conjugate[g] -
I (-dD + b d g Conjugate[g]), -I DD, 0, I a Sqrt[b] g w, 0, 0,
0, 0}, {0, -I DD, I dD - g2, 0, 0, I a Sqrt[b] g w, 0, 0,
0}, {I a Sqrt[b] Conjugate[g] Conjugate[w], 0,
0, -g2 - b g z Conjugate[g] -
I (dD - b d g Conjugate[g]), -I a Sqrt[b] Conjugate[
g] Conjugate[w], 0, I DD, 0, 0}, {0,
I a Sqrt[b] Conjugate[g] Conjugate[w],
0, -I a Sqrt[b] g w, -2 g2 - 2 b g z Conjugate[g], -I DD, 0,
I DD, 0}, {0, 0, I a Sqrt[b] Conjugate[g] Conjugate[w],
0, -I DD, -2 g2 + I b d g Conjugate[g] - b g z Conjugate[g], 0,
0, I DD}, {0, 0, 0, I DD, 0,
0, -I dD - g2, -I a Sqrt[b] Conjugate[g] Conjugate[w], 0}, {0, 0,
0, 0, I DD,
0, -I a Sqrt[b] g w, -2 g2 - I b d g Conjugate[g] -
b g z Conjugate[g], -I DD}, {0, 0, 0, 0, 0, I DD,
0, -I DD, -2 g2}});
MatrixForm[L]
NullSpace[L]
如前所述,对于同一个矩阵,SymPy 正在苦苦挣扎。这有什么理由吗?在python中有没有其他方法可以解决这个问题?
这是我的 SymPy 代码:
import numpy as np
import scipy
import sympy as sp
a = sp.symbols("a", real=True, positive=True)
b = sp.symbols("b", real=True, positive=True)
g = sp.symbols("g", real=True, positive=True)
g2 = sp.symbols("g2", real=True, positive=True)
DD = sp.symbols("DD", real=True, positive=True)
dD = sp.symbols("dD", real=True)
w = sp.symbols("w")
z = sp.symbols("z")
d = sp.symbols("d")
L = sp.Matrix([[0, -1j*a*sp.sqrt(b)*sp.conjugate(g)*sp.conjugate(w), 0, 1j*a*sp.sqrt(b)*g*w,
2*g2 + 2*b*g*z*sp.conjugate(g), 0, 0, 0,
2*g2], [-1j*a*sp.sqrt(b)*g*w, -g2 - b*g*z*sp.conjugate(g) -
1j*(-dD + b*d*g*sp.conjugate(g)), -1j*DD, 0, 1j*a*sp.sqrt(b)*g*w, 0, 0,
0, 0], [0, -1j*DD, 1j*dD - g2, 0, 0, 1j*a*sp.sqrt(b)*g*w, 0, 0,
0], [1j*a*sp.sqrt(b)*sp.conjugate(g)*sp.conjugate(w), 0,
0, -g2 - b*g*z*sp.conjugate(g) -
1j*(dD - b*d*g*sp.conjugate(g)), -1j*a*sp.sqrt(b)*sp.conjugate(
g)*sp.conjugate(w), 0, 1j*DD, 0, 0], [0,
1j*a*sp.sqrt(b)*sp.conjugate(g)*sp.conjugate(w),
0, -1j*a*sp.sqrt(b)*g*w, -2*g2 - 2*b*g*z*sp.conjugate(g), -1j*DD, 0,
1j*DD, 0], [0, 0, 1j*a*sp.sqrt(b)*sp.conjugate(g)*sp.conjugate(w),
0, -1j*DD, -2*g2 + 1j*b*d*g*sp.conjugate(g) - b*g*z*sp.conjugate(g), 0,
0, 1j*DD], [0, 0, 0, 1j*DD, 0,
0, -1j*dD - g2, -1j*a*sp.sqrt(b)*sp.conjugate(g)*sp.conjugate(w), 0], [0, 0,
0, 0, 1j*DD,
0, -1j*a*sp.sqrt(b)*g*w, -2*g2 - 1j*b*d*g*sp.conjugate(g) -
b*g*z*sp.conjugate(g), -1j*DD], [0, 0, 0, 0, 0, 1j*DD,
0, -1j*DD, -2*g2]]);
sp.pprint(L)
sp.pprint(L.nullspace())
根据评论,以下信息也可能相关:
- 真实的正参数:
a, b, g, g2, DD
- 实际参数:
dD
- 复杂参数:
w, z, d
sympy 中的虚数单位是sympy.I
(不是1j
)。显然,1j
是可以被 sympy 接受的,但是,它被解释为一个数值,在符号计算中应该避免这种情况。
以下代码相当快速地给出了零空间。请注意,我还提供了符号属性。这些通常有助于 sympy 提供更快的答案
a = sp.symbols("a", positive = True)
b = sp.symbols("b", positive = True)
g = sp.symbols("g", positive = True)
w = sp.symbols("w")
z = sp.symbols("z")
g2 = sp.symbols("g2", positive = True)
DD = sp.symbols("DD", positive = True)
dD = sp.symbols("dD", real = True)
d = sp.symbols("d")
L = sp.Matrix([[0, -sp.I*a*sp.sqrt(b)*sp.conjugate(g)*sp.conjugate(w), 0, sp.I*a*sp.sqrt(b)*g*w,
2*g2 + 2*b*g*z*sp.conjugate(g), 0, 0, 0,
2*g2], [-sp.I*a*sp.sqrt(b)*g*w, -g2 - b*g*z*sp.conjugate(g) -
sp.I*(-dD + b*d*g*sp.conjugate(g)), -sp.I*DD, 0, sp.I*a*sp.sqrt(b)*g*w, 0, 0,
0, 0], [0, -sp.I*DD, sp.I*dD - g2, 0, 0, sp.I*a*sp.sqrt(b)*g*w, 0, 0,
0], [sp.I*a*sp.sqrt(b)*sp.conjugate(g)*sp.conjugate(w), 0,
0, -g2 - b*g*z*sp.conjugate(g) -
sp.I*(dD - b*d*g*sp.conjugate(g)), -sp.I*a*sp.sqrt(b)*sp.conjugate(
g)*sp.conjugate(w), 0, sp.I*DD, 0, 0], [0,
sp.I*a*sp.sqrt(b)*sp.conjugate(g)*sp.conjugate(w),
0, -sp.I*a*sp.sqrt(b)*g*w, -2*g2 - 2*b*g*z*sp.conjugate(g), -sp.I*DD, 0,
sp.I*DD, 0], [0, 0, sp.I*a*sp.sqrt(b)*sp.conjugate(g)*sp.conjugate(w),
0, -sp.I*DD, -2*g2 + sp.I*b*d*g*sp.conjugate(g) - b*g*z*sp.conjugate(g), 0,
0, sp.I*DD], [0, 0, 0, sp.I*DD, 0,
0, -sp.I*dD - g2, -sp.I*a*sp.sqrt(b)*sp.conjugate(g)*sp.conjugate(w), 0], [0, 0,
0, 0, sp.I*DD,
0, -sp.I*a*sp.sqrt(b)*g*w, -2*g2 - sp.I*b*d*g*sp.conjugate(g) -
b*g*z*sp.conjugate(g), -sp.I*DD], [0, 0, 0, 0, 0, sp.I*DD,
0, -sp.I*DD, -2*g2]]);
Ln = L.nullspace()
我一直在尝试使用命令 A.nullspace
在 SymPy 中找到符号矩阵 A
的零空间。现在,我输入的矩阵的计算没有完成(或者比我等待的时间更长)。
奇怪的是 Mathematica 在几分之一秒内完成了这个计算。我将给出我用作 Mathematica 输入代码的具体示例(应该 运行)
L = ({{0, -I a Sqrt[b] Conjugate[g] Conjugate[w], 0, I a Sqrt[b] g w,
2 g2 + 2 b g z Conjugate[g], 0, 0, 0,
2 g2}, {-I a Sqrt[b] g w, -g2 - b g z Conjugate[g] -
I (-dD + b d g Conjugate[g]), -I DD, 0, I a Sqrt[b] g w, 0, 0,
0, 0}, {0, -I DD, I dD - g2, 0, 0, I a Sqrt[b] g w, 0, 0,
0}, {I a Sqrt[b] Conjugate[g] Conjugate[w], 0,
0, -g2 - b g z Conjugate[g] -
I (dD - b d g Conjugate[g]), -I a Sqrt[b] Conjugate[
g] Conjugate[w], 0, I DD, 0, 0}, {0,
I a Sqrt[b] Conjugate[g] Conjugate[w],
0, -I a Sqrt[b] g w, -2 g2 - 2 b g z Conjugate[g], -I DD, 0,
I DD, 0}, {0, 0, I a Sqrt[b] Conjugate[g] Conjugate[w],
0, -I DD, -2 g2 + I b d g Conjugate[g] - b g z Conjugate[g], 0,
0, I DD}, {0, 0, 0, I DD, 0,
0, -I dD - g2, -I a Sqrt[b] Conjugate[g] Conjugate[w], 0}, {0, 0,
0, 0, I DD,
0, -I a Sqrt[b] g w, -2 g2 - I b d g Conjugate[g] -
b g z Conjugate[g], -I DD}, {0, 0, 0, 0, 0, I DD,
0, -I DD, -2 g2}});
MatrixForm[L]
NullSpace[L]
如前所述,对于同一个矩阵,SymPy 正在苦苦挣扎。这有什么理由吗?在python中有没有其他方法可以解决这个问题?
这是我的 SymPy 代码:
import numpy as np
import scipy
import sympy as sp
a = sp.symbols("a", real=True, positive=True)
b = sp.symbols("b", real=True, positive=True)
g = sp.symbols("g", real=True, positive=True)
g2 = sp.symbols("g2", real=True, positive=True)
DD = sp.symbols("DD", real=True, positive=True)
dD = sp.symbols("dD", real=True)
w = sp.symbols("w")
z = sp.symbols("z")
d = sp.symbols("d")
L = sp.Matrix([[0, -1j*a*sp.sqrt(b)*sp.conjugate(g)*sp.conjugate(w), 0, 1j*a*sp.sqrt(b)*g*w,
2*g2 + 2*b*g*z*sp.conjugate(g), 0, 0, 0,
2*g2], [-1j*a*sp.sqrt(b)*g*w, -g2 - b*g*z*sp.conjugate(g) -
1j*(-dD + b*d*g*sp.conjugate(g)), -1j*DD, 0, 1j*a*sp.sqrt(b)*g*w, 0, 0,
0, 0], [0, -1j*DD, 1j*dD - g2, 0, 0, 1j*a*sp.sqrt(b)*g*w, 0, 0,
0], [1j*a*sp.sqrt(b)*sp.conjugate(g)*sp.conjugate(w), 0,
0, -g2 - b*g*z*sp.conjugate(g) -
1j*(dD - b*d*g*sp.conjugate(g)), -1j*a*sp.sqrt(b)*sp.conjugate(
g)*sp.conjugate(w), 0, 1j*DD, 0, 0], [0,
1j*a*sp.sqrt(b)*sp.conjugate(g)*sp.conjugate(w),
0, -1j*a*sp.sqrt(b)*g*w, -2*g2 - 2*b*g*z*sp.conjugate(g), -1j*DD, 0,
1j*DD, 0], [0, 0, 1j*a*sp.sqrt(b)*sp.conjugate(g)*sp.conjugate(w),
0, -1j*DD, -2*g2 + 1j*b*d*g*sp.conjugate(g) - b*g*z*sp.conjugate(g), 0,
0, 1j*DD], [0, 0, 0, 1j*DD, 0,
0, -1j*dD - g2, -1j*a*sp.sqrt(b)*sp.conjugate(g)*sp.conjugate(w), 0], [0, 0,
0, 0, 1j*DD,
0, -1j*a*sp.sqrt(b)*g*w, -2*g2 - 1j*b*d*g*sp.conjugate(g) -
b*g*z*sp.conjugate(g), -1j*DD], [0, 0, 0, 0, 0, 1j*DD,
0, -1j*DD, -2*g2]]);
sp.pprint(L)
sp.pprint(L.nullspace())
根据评论,以下信息也可能相关:
- 真实的正参数:
a, b, g, g2, DD
- 实际参数:
dD
- 复杂参数:
w, z, d
sympy 中的虚数单位是sympy.I
(不是1j
)。显然,1j
是可以被 sympy 接受的,但是,它被解释为一个数值,在符号计算中应该避免这种情况。
以下代码相当快速地给出了零空间。请注意,我还提供了符号属性。这些通常有助于 sympy 提供更快的答案
a = sp.symbols("a", positive = True)
b = sp.symbols("b", positive = True)
g = sp.symbols("g", positive = True)
w = sp.symbols("w")
z = sp.symbols("z")
g2 = sp.symbols("g2", positive = True)
DD = sp.symbols("DD", positive = True)
dD = sp.symbols("dD", real = True)
d = sp.symbols("d")
L = sp.Matrix([[0, -sp.I*a*sp.sqrt(b)*sp.conjugate(g)*sp.conjugate(w), 0, sp.I*a*sp.sqrt(b)*g*w,
2*g2 + 2*b*g*z*sp.conjugate(g), 0, 0, 0,
2*g2], [-sp.I*a*sp.sqrt(b)*g*w, -g2 - b*g*z*sp.conjugate(g) -
sp.I*(-dD + b*d*g*sp.conjugate(g)), -sp.I*DD, 0, sp.I*a*sp.sqrt(b)*g*w, 0, 0,
0, 0], [0, -sp.I*DD, sp.I*dD - g2, 0, 0, sp.I*a*sp.sqrt(b)*g*w, 0, 0,
0], [sp.I*a*sp.sqrt(b)*sp.conjugate(g)*sp.conjugate(w), 0,
0, -g2 - b*g*z*sp.conjugate(g) -
sp.I*(dD - b*d*g*sp.conjugate(g)), -sp.I*a*sp.sqrt(b)*sp.conjugate(
g)*sp.conjugate(w), 0, sp.I*DD, 0, 0], [0,
sp.I*a*sp.sqrt(b)*sp.conjugate(g)*sp.conjugate(w),
0, -sp.I*a*sp.sqrt(b)*g*w, -2*g2 - 2*b*g*z*sp.conjugate(g), -sp.I*DD, 0,
sp.I*DD, 0], [0, 0, sp.I*a*sp.sqrt(b)*sp.conjugate(g)*sp.conjugate(w),
0, -sp.I*DD, -2*g2 + sp.I*b*d*g*sp.conjugate(g) - b*g*z*sp.conjugate(g), 0,
0, sp.I*DD], [0, 0, 0, sp.I*DD, 0,
0, -sp.I*dD - g2, -sp.I*a*sp.sqrt(b)*sp.conjugate(g)*sp.conjugate(w), 0], [0, 0,
0, 0, sp.I*DD,
0, -sp.I*a*sp.sqrt(b)*g*w, -2*g2 - sp.I*b*d*g*sp.conjugate(g) -
b*g*z*sp.conjugate(g), -sp.I*DD], [0, 0, 0, 0, 0, sp.I*DD,
0, -sp.I*DD, -2*g2]]);
Ln = L.nullspace()