Question

I am currently using Python 2.7.3, sympy 0.7.1.rc1 I am constructing two matrices like this:

import sympy as sp

A = sp.Matrix([[0,0,1],[0,1,0],[1,0,0]])
B = sp.Matrix([[0,0,1],[0,1,0],[1,0,0]])

print A
print B
print A==B
print hash(A)
print hash(B)

and the result is...

[0, 0, 1]
[0, 1, 0]
[1, 0, 0]
[0, 0, 1]
[0, 1, 0]
[1, 0, 0]
True
3144597
3144601

The hash value of A,B are different. I need to put these two matrix into a set(), but the hash value are different and then I am unable to do what I intended for. Is it a bug of sympy or I should do it another way?

Was it helpful?

Solution

As the commenters noted, you need to update to a newer version of SymPy. In older versions, mutable matrices were hashable, which was incorrect. Now, hash(Matrix([[0,0,1],[0,1,0],[1,0,0]])) raises TypeError as it should. If you want a hashable matrix, use ImmutableMatrix.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top