Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Concerning your

motivation: sagematrix,it seems, are not "hashable" and cannot be used as keys in lists to do groupby/countby

I'd like to make a comment. Matrices in sage are, by default, mutable and hence (just like lists in python) not hashable. You can a matrix to be immutable, and if its entries are hashable, the matrix itself is also hashable.

sage: A=random_matrix(ZZ,1,5)
sage: hash(A)
TypeError: mutable matrices are unhashable
sage: A.set_immutable()
sage: hash(A)
-4

If you're OK with your matrices being immutable, this can save you having to copy the entries into tuples.

Concerning your

motivation: sagematrix,it seems, are not "hashable" and cannot be used as keys in lists to do groupby/countby

I'd like to make a comment. Matrices in sage are, by default, mutable and hence (just like lists in python) not hashable. You can set a matrix to be immutable, and if its entries are hashable, the matrix itself is will also be hashable.

sage: A=random_matrix(ZZ,1,5)
sage: hash(A)
TypeError: mutable matrices are unhashable
sage: A.set_immutable()
sage: hash(A)
-4

If you're OK with your matrices being immutable, this can save you having to copy the entries into tuples.