Making a dictionary of matrices
I am trying to make a dictionary of matrices. Ideally
MatDic={
matrix(GF(2),2,[1,0,0,1]):'a',
matrix(GF(2),2,[1,1,0,1]):'b'
# etc
}
But Sage refuses to hash (mutable) matrices. I then tried
MatDic={
matrix(GF(2),2,[1,0,0,1]).set_immutable():'a',
matrix(GF(2),2,[1,1,0,1]).set_immutable():'b'
}
MatDic
which gives
{None: 'b'}
I also tried making a dictionary of tuples
MatDic={
(1,0,0,1):'a',
(1,1,0,1):'b'
}
which raises no errors; However
MatDic[tuple(matrix(GF(2),2,[1,0,0,1]))]
gives
TypeError: mutable vectors are unhashable
What is the best, if possible, way to make a dictionary and lookup matrices?
Why do we need to force the situation in a place where python insists to have hashable objects (...the keys)? A good possibility is to use the information from the entries as a tuple instead, e.g.
and the keys are easily transformed to matrices, for instance:
Here, for the last matrix:
@dan_fulea Well I expect the dictionary to record not only entries but also the width and height. If I come up with a method that turns a matrix into a hashable matrix then I am reinventing
.set_immutable()
. So maybe the real question is why don't sage/python automatically make a hashable copy and then hash it.