Ticket #3956: matrix_modn_dense_hash.patch

File matrix_modn_dense_hash.patch, 2.4 kB (added by malb, 4 months ago)
  • a/sage/matrix/matrix_modn_dense.pyx

    old new  
    263263        return self._richcmp(right, op) 
    264264 
    265265    def __hash__(self): 
    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 
    267305     
    268306    cdef set_unsafe(self, Py_ssize_t i, Py_ssize_t j, value): 
    269307        self._matrix[i][j] = (<IntegerMod_int> value).ivalue 
     
    14531491        Return the requested matrix window. 
    14541492 
    14551493        EXAMPLES: 
    1456             sage: a = matrix(QQ,3,range(9)) 
    1457             sage: a.matrix_window() 
    1458             Matrix window of size 3 x 3 at (0,0): 
    1459             [0 1 2] 
    1460             [3 4 5] 
    1461             [6 7 8] 
    1462             sage: type(a) 
    1463             <type 'sage.matrix.matrix_rational_dense.Matrix_rational_dense'> 
    14641494            sage: a = matrix(GF(7),3,range(9)); a 
    14651495            [0 1 2] 
    14661496            [3 4 5]