求解 sympy 方程会导致 numpy 数组维度出错

solve sympy equation results in an error on numpy array dimensions

我目前正在尝试使用 sympy 求解方程组(遵循 this lecture on scientific python),但出现以下错误:

Traceback (most recent call last):
  File "VMT.py", line 13, in <module>
    [Vmc, Vgp, tmc, tgp])
  File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/numpy/linalg/linalg.py", line 311, in solve
    _assertRank2(a, b)
  File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/numpy/linalg/linalg.py", line 155, in _assertRank2
    two-dimensional' % len(a.shape)
numpy.linalg.linalg.LinAlgError: 1-dimensional array given. Array must be two-dimensional

我的代码是:

from sympy import *
from pylab import *

Vmc=Symbol('Vmc')
Vgp=Symbol('Vgp')
tmc=Symbol('tmc')
tgp=Symbol('tgp')

solve([-Vmc + (((2300**10)*(tmc - 85))/(0.02*85))**(1/10), 
  -Vgp + (((6900**10)*(tgp - 85))/(0.02*0.85))**(1/10), 
  -Vmc + 12000*(((2.76/(tgp - tmc)) - 7)/(16 - 7))**(1/10), 
  -Vgp - Vmc + 12000], 
  [Vmc, Vgp, tmc, tgp])

我知道这似乎是我如何设置求解函数的问题,但我对如何解决这个问题有点困惑。

问题来自使用

from pylab import *

之后的行
from sympy import *

您创建了命名空间冲突,因为 solve 同时存在于 pylab 下(它实际上是 numpy.linalg.solve 的别名)和 sympy .

您应该尽量避免此类通用导入。

例如,这将起作用:

from sympy import *  # still not a big fan of this
import pylab

Vmc=Symbol('Vmc')
Vgp=Symbol('Vgp')
tmc=Symbol('tmc')
tgp=Symbol('tgp')

solve([-Vmc + (((2300**10)*(tmc - 85))/(0.02*85))**(1/10), 
  -Vgp + (((6900**10)*(tgp - 85))/(0.02*0.85))**(1/10), 
  -Vmc + 12000*(((2.76/(tgp - tmc)) - 7)/(16 - 7))**(1/10), 
  -Vgp - Vmc + 12000], 
  [Vmc, Vgp, tmc, tgp])

虽然您的方程组可能没有解。