Ticket #10763: trac10763-speedup_matrix_multiplication.patch

File trac10763-speedup_matrix_multiplication.patch, 4.5 KB (added by SimonKing, 10 years ago)

Improve performance of matrix multiplication by using a shortpath to matrix spaces and a shortpath in multiplication algorithms for creating a zero matrix

  • sage/matrix/matrix1.pyx

    # HG changeset patch
    # User Simon King <simon.king@uni-jena.de>
    # Date 1297329302 -3600
    # Node ID a90f35609e9e62ad20bb3a91d7f9941d2c049e2d
    # Parent  361a4ad7d52c69b64ae2e658ffd0820af0d87e93
    10763: Use shortpath for the creation of matrix spaces from a matrix;
    use another shortpath for the creation of zero matrices in multiplication algorithms.
    
    diff -r 361a4ad7d52c -r a90f35609e9e sage/matrix/matrix1.pyx
    a b  
    14061406            sage: m.matrix_space(1, 2, True)
    14071407            Full MatrixSpace of 1 by 2 sparse matrices over Integer Ring
    14081408        """
     1409        from sage.matrix.matrix_space import MatrixSpace
    14091410        if nrows is None:
    14101411            nrows = self._nrows
    14111412        if ncols is None:
    14121413            ncols = self._ncols
    14131414        if sparse is None:
    14141415            sparse = self.is_sparse()
    1415         return self.parent().matrix_space(nrows, ncols, sparse=sparse)
     1416        base_ring = self._base_ring
     1417        try:
     1418            from sage.matrix.matrix_space import _cache
     1419            MS = _cache[base_ring, nrows, ncols, sparse]()
     1420        except KeyError:
     1421            return MatrixSpace(base_ring, nrows, ncols, sparse)
     1422        if MS is not None:
     1423            return MS
     1424        return MatrixSpace(base_ring, nrows, ncols, sparse)
    14161425
    14171426    def new_matrix(self, nrows=None, ncols=None, entries=0,
    14181427                   coerce=True, copy=True, sparse=None):
  • sage/matrix/matrix2.pyx

    diff -r 361a4ad7d52c -r a90f35609e9e sage/matrix/matrix2.pyx
    a b  
    48724872        if cutoff <= 0:
    48734873            raise ValueError, "cutoff must be at least 1"
    48744874
    4875         output = self.new_matrix(self._nrows, right._ncols)
     4875        #output = self.new_matrix(self._nrows, right._ncols)
     4876        # the following is a little faster:
     4877        if self.is_sparse():
     4878            output = self.matrix_space(self._nrows, right._ncols, sparse = True)(0)
     4879        else:
     4880            output = self.matrix_space(self._nrows, right._ncols, sparse = False).zero_matrix().__copy__()
    48764881       
    48774882        self_window   = self.matrix_window()
    48784883        right_window  = right.matrix_window()
  • sage/matrix/matrix_mod2_dense.pyx

    diff -r 361a4ad7d52c -r a90f35609e9e sage/matrix/matrix_mod2_dense.pyx
    a b  
    761761
    762762        cdef Matrix_mod2_dense ans
    763763       
    764         ans = self.new_matrix(nrows = self.nrows(), ncols = right.ncols())
     764        ans = self.new_matrix(nrows = self._nrows, ncols = right._ncols)
    765765        if self._nrows == 0 or self._ncols == 0 or right._ncols == 0:
    766766            return ans
    767767        sig_on()
     
    813813            []
    814814        """
    815815        cdef Matrix_mod2_dense A
    816         A = self.new_matrix(nrows = self.nrows(), ncols = right.ncols())
     816        A = self.new_matrix(nrows = self._nrows, ncols = right._ncols)
    817817        if self._nrows == 0 or self._ncols == 0 or right._ncols == 0:
    818818            return A
    819819        A._entries = mzd_mul_naive(A._entries, self._entries,(<Matrix_mod2_dense>right)._entries)
     
    898898            raise ArithmeticError("left ncols must match right nrows")
    899899
    900900        cdef Matrix_mod2_dense ans
    901         ans = self.new_matrix(nrows = self.nrows(), ncols = right.ncols())
     901        #ans = self.new_matrix(nrows = self._nrows, ncols = right._ncols)
     902        # The following is a little faster:
     903        ans = self.matrix_space(self._nrows, right._ncols, sparse=False).zero_matrix().__copy__()
    902904        if self._nrows == 0 or self._ncols == 0 or right._ncols == 0:
    903905            return ans
    904906
  • sage/matrix/matrix_space.py

    diff -r 361a4ad7d52c -r a90f35609e9e sage/matrix/matrix_space.py
    a b  
    11531153            nrows = self.__nrows
    11541154        if ncols is None:
    11551155            ncols = self.__ncols
    1156         return MatrixSpace(self.base_ring(), nrows, ncols,
    1157                         sparse=sparse)
    1158  
     1156        base = self._base
     1157        try:
     1158            MS = _cache[base,nrows,ncols,sparse]()
     1159        except KeyError:
     1160            return MatrixSpace(base, nrows, ncols, sparse=sparse)
     1161        if MS is not None:
     1162            return MS
     1163        return MatrixSpace(base, nrows, ncols, sparse=sparse)
     1164
    11591165    def ncols(self):
    11601166        """
    11611167        Return the number of columns of matrices in this space.