1

I'm getting an error message about dictionaries despite having never used a dictionary anywhere in my code

Here is my code:

mm=[]
soln=[]
for i in range(len(momvec)):
    string = str(momvec[i])
    num = string[2:]
    mm.append(Symbol('x'+num))
    print num
    print mm
    soln.append(solve(mom[i]-momvec[i],mm))
print type(MFK[0])
for m in range(0,len(MFK)):
    for i in range(0,len(mm)):
        print MFK[m]
        MFK[m]= Subs(MFK[m],mm[i], soln[i]).doit()

What I am trying to do is replace some items in the list MFK with what is indicated by mm with the contents of soln iteratively.

The traceback:

Traceback (most recent call last):
  File "MEA.py", line 313, in <module>
    MFK_final(numMoments)
  File "MEA.py", line 242, in MFK_final
    MFK[m]= Subs(MFK[m],mm[i], soln[i]).doit()
  File "/cluster/soft/linux64/epd/lib/python2.7/site-packages/sympy/core/function.py", line 1270, in __new__
    pts = sorted(set(point), key=default_sort_key)
TypeError: unhashable type: 'dict'

values for mom, momvec and MFK respectively:

[x2 - y_0**2]

[ym2]

[-2*c_0*y_0*(y_0 - 1) - 2*c_0*(-y_0**2 + yx1*(y_0**2/yx1 + 1)) + 2*c_1*(0.5*c_2 - 0.5*y_0), 4.0*c_0*y_0**2 - 4.0*c_0*y_0 + 2.0*c_1*c_2 - 2.0*c_1*y_0 - (-y_0**2 + yx1*(y_0**2/yx1 + 1))*(8.0*c_0*y_0 - 8.0*c_0 + 2.0*c_1)]

Printing output to screen it appears as though the first loop goes fine, then it throws the error in the title. Does anyone know how I could solve this?

Many thanks

user124123
  • 1,642
  • 7
  • 30
  • 50

2 Answers2

1

From your description, I can't reproduce your error. That is, I made a Python file:

from sympy import *

x2 = Symbol("x2")
y_0 = Symbol("y_0")
ym2 = Symbol("ym2")
c_0 = Symbol("c_0")
c_1 = Symbol("c_1")
c_2 = Symbol("c_2")
yx1 = Symbol("yx1")

mom = [x2 - y_0**2]
momvec = [ym2]
MFK = [-2*c_0*y_0*(y_0 - 1) - 2*c_0*(-y_0**2 + yx1*(y_0**2/yx1 + 1)) + 2*c_1*(0.5*c_2 - 0.5*y_0), 4.0*c_0*y_0**2 - 4.0*c_0*y_0 + 2.0*c_1*c_2 - 2.0*c_1*y_0 - (-y_0**2 + yx1*(y_0**2/yx1 + 1))*(8.0*c_0*y_0 - 8.0*c_0 + 2.0*c_1)]

mm=[]
soln=[]
for i in range(len(momvec)):
    string = str(momvec[i])
    num = string[2:]
    mm.append(Symbol('x'+num))
    print num
    print mm
    soln.append(solve(mom[i]-momvec[i],mm))
print type(MFK[0])
for m in range(0,len(MFK)):
    for i in range(0,len(mm)):
        print MFK[m]
        MFK[m]= Subs(MFK[m],mm[i], soln[i]).doit()

and it simply outputs

2
[x2]
<class 'sympy.core.add.Add'>
-2*c_0*y_0*(y_0 - 1) - 2*c_0*(-y_0**2 + yx1*(y_0**2/yx1 + 1)) + 2*c_1*(0.5*c_2 - 0.5*y_0)
4.0*c_0*y_0**2 - 4.0*c_0*y_0 + 2.0*c_1*c_2 - 2.0*c_1*y_0 - (-y_0**2 + yx1*(y_0**2/yx1 + 1))*(8.0*c_0*y_0 - 8.0*c_0 + 2.0*c_1)

without any errors or tracebacks.

However, if you do find the point in the code where the hidden dictionary appears, the reason dictionaries shouldn't be hashable (and therefore usable as keys in other dictionaries) is because they're mutable. If you use one as a key and then later change a value somewhere deep in its structure, you could get weird x != x style errors. You could fix such an error with a hashable frozen dictionary...

Community
  • 1
  • 1
Jim Pivarski
  • 5,568
  • 2
  • 35
  • 47
1

Your soln[i] is a dictionary, but it needs to be a SymPy expression (the one you plan to substitute into MFK[m] for mm[i]).

asmeurer
  • 86,894
  • 26
  • 169
  • 240