Ticket #3956: matrix_modn_dense_hash.3.patch

File matrix_modn_dense_hash.3.patch, 3.1 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 long _hash = 0 
     305        cdef mod_int *_matrix 
     306        cdef long n = 0 
     307        cdef Py_ssize_t i, j 
     308 
     309        if self._nrows == 0 or self._ncols == 0: 
     310            return 0 
     311 
     312        _sig_on 
     313        for i from 0 <= i < self._nrows: 
     314            _matrix = self._matrix[i] 
     315            for j from 0 <= j < self._ncols: 
     316                _hash ^= (n * _matrix[j]) 
     317                n+=1 
     318        _sig_off 
     319 
     320        if _hash == -1: 
     321            return -2 
     322 
     323        self.cache('hash', _hash) 
     324 
     325        return _hash 
    267326     
    268327    cdef set_unsafe(self, Py_ssize_t i, Py_ssize_t j, value): 
    269328        self._matrix[i][j] = (<IntegerMod_int> value).ivalue 
     
    14531512        Return the requested matrix window. 
    14541513 
    14551514        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'> 
    14641515            sage: a = matrix(GF(7),3,range(9)); a 
    14651516            [0 1 2] 
    14661517            [3 4 5]