| 266 | | return self._hash() |
|---|
| | 266 | """ |
|---|
| | 267 | EXAMPLE: |
|---|
| | 268 | sage: B = random_matrix(GF(127),3,3) |
|---|
| | 269 | sage: B.set_immutable() |
|---|
| | 270 | sage: {B:0} # indirect doctest |
|---|
| | 271 | {[ 9 75 94] |
|---|
| | 272 | [ 4 57 112] |
|---|
| | 273 | [ 59 85 45]: 0} |
|---|
| | 274 | sage: A = matrix(GF(7),2,2) |
|---|
| | 275 | sage: A.set_immutable() |
|---|
| | 276 | sage: hex(hash(A)) |
|---|
| | 277 | '0xdeadbeec' |
|---|
| | 278 | |
|---|
| | 279 | TEST: |
|---|
| | 280 | sage: A = matrix(GF(2),2,0) |
|---|
| | 281 | sage: hash(A) |
|---|
| | 282 | Traceback (most recent call last): |
|---|
| | 283 | ... |
|---|
| | 284 | TypeError: mutable matrices are unhashable |
|---|
| | 285 | sage: A.set_immutable() |
|---|
| | 286 | sage: hash(A) |
|---|
| | 287 | 0 |
|---|
| | 288 | """ |
|---|
| | 289 | if self.is_mutable(): |
|---|
| | 290 | raise TypeError("mutable matrices are unhashable") |
|---|
| | 291 | cdef unsigned long _hash = 0xDEADBEEF ^ self._base_ring.characteristic() |
|---|
| | 292 | cdef unsigned long counter = 0 |
|---|
| | 293 | cdef unsigned long i |
|---|
| | 294 | |
|---|
| | 295 | if self._nrows == 0 or self._ncols == 0: |
|---|
| | 296 | return 0 |
|---|
| | 297 | |
|---|
| | 298 | for i from 0 <= i < self._nrows * self._ncols: |
|---|
| | 299 | _hash ^= self._entries[i] |
|---|
| | 300 | _hash = _hash ^ (self._nrows * self._ncols) |
|---|
| | 301 | |
|---|
| | 302 | if _hash == -1: |
|---|
| | 303 | return -2 |
|---|
| | 304 | return _hash |
|---|