문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top