Ticket #12290: trac12290_matrixspace_hash.patch

File trac12290_matrixspace_hash.patch, 1.7 KB (added by Simon King, 11 years ago)

Make the hashes of two equal matrix spaces equal. Improve the performance

  • sage/matrix/matrix_space.py

    # HG changeset patch
    # User Simon King <simon.king@uni-jena.de>
    # Date 1326200560 -3600
    # Node ID 335a0ee3527410666d2a1468aee4827c18653123
    # Parent  8883449f439a6a078ec685b24f0ddd5b9323fb31
    #12290: Make the hashes of equal matrix spaces equal, and return the hash faster.
    
    diff --git a/sage/matrix/matrix_space.py b/sage/matrix/matrix_space.py
    a b  
    291291        #sage.structure.parent.Parent.__init__(self, category=category)
    292292        sage.structure.category_object.CategoryObject._init_category_(self, category)
    293293
     294    def __hash__(self):
     295        """
     296        EXAMPLES:
     297
     298        The following tests against a bug fixed in trac ticket #12290::
     299
     300            sage: M = MatrixSpace(ZZ, 10)
     301            sage: N = MatrixSpace(ZZ, 10,sparse=True)
     302            sage: M == N
     303            True
     304            sage: hash(M)==hash(N)     # indirect doctest
     305            True
     306
     307        """
     308        return self._hash_val
     309   
     310    @lazy_attribute
     311    def _hash_val(self):
     312        """
     313        This lazy attribute computes and stores the hash. This is in order to
     314        have maximal speed for the hash of matrix spaces.
     315
     316        EXAMPLES:
     317
     318        The following tests against a bug fixed in trac ticket #12290::
     319
     320            sage: M = MatrixSpace(ZZ, 10)
     321            sage: N = MatrixSpace(ZZ, 10,sparse=True)
     322            sage: M == N
     323            True
     324            sage: hash(M)==hash(N)     # indirect doctest
     325            True
     326
     327        """
     328        return hash((self.base(),self.__nrows, self.__ncols))
     329
    294330    def full_category_initialisation(self):
    295331        """
    296332        Make full use of the category framework.