Ticket #3956: matrix_modn_dense_hash.2.patch

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

    old new  
    7171        for i from 0 <= i < len(v): 
    7272            h = h ^ (i * hash(v[i])) 
    7373             
     74        if h == -1: 
     75            h = -2 
     76 
    7477        self.cache('hash', h) 
    7578        return h 
    7679 
  • 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 
     275            sage: M = random_matrix(GF(7), 10, 10) 
     276            sage: M.set_immutable() 
     277            sage: hash(M) 
     278            143 
     279            sage: MZ = M.change_ring(ZZ) 
     280            sage: MZ.set_immutable() 
     281            sage: hash(MZ) 
     282            143 
     283            sage: MS = M.sparse_matrix() 
     284            sage: MS.set_immutable() 
     285            sage: hash(MS) 
     286            143 
     287 
     288        TEST: 
     289            sage: A = matrix(GF(2),2,0) 
     290            sage: hash(A) 
     291            Traceback (most recent call last): 
     292            ... 
     293            TypeError: mutable matrices are unhashable 
     294            sage: A.set_immutable() 
     295            sage: hash(A) 
     296            0 
     297        """ 
     298        if self.is_mutable(): 
     299            raise TypeError("mutable matrices are unhashable") 
     300        x = self.fetch('hash') 
     301        if not x is None:  
     302            return x 
     303 
     304        cdef unsigned long _hash = 0 
     305        cdef Py_ssize_t i 
     306 
     307        if self._nrows == 0 or self._ncols == 0: 
     308            return 0 
     309 
     310        for i from 0 <= i < self._nrows * self._ncols: 
     311            _hash ^= (i * self._entries[i]) 
     312 
     313        if _hash == -1: 
     314            return -2 
     315 
     316        self.cache('hash', _hash) 
     317 
     318        return _hash 
    267319     
    268320    cdef set_unsafe(self, Py_ssize_t i, Py_ssize_t j, value): 
    269321        self._matrix[i][j] = (<IntegerMod_int> value).ivalue 
     
    14531505        Return the requested matrix window. 
    14541506 
    14551507        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'> 
    14641508            sage: a = matrix(GF(7),3,range(9)); a 
    14651509            [0 1 2] 
    14661510            [3 4 5]