ks905383
ks905383

Reputation: 3

solve sympy equation results in an error on numpy array dimensions

I'm currently trying to solve a system of equations using sympy (following this lecture on scientific python) and I'm getting the following error:

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

My code is:

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])

I know this seems to be an issue with how I set up the solve function, but I'm a bit confused as how to get around that.

Upvotes: 0

Views: 127

Answers (1)

Oliver W.
Oliver W.

Reputation: 13459

The problem comes from using the

from pylab import *

line right after

from sympy import *

You've created a namespace collision, because solve exists both under pylab (where it is in fact an alias for numpy.linalg.solve) as well as sympy.

You should try to avoid such generic imports.

For example, this will work:

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])

Your system of equations might not have a solution though.

Upvotes: 1

Related Questions