Ticket #7723: trac_7723-fixround1.patch

File trac_7723-fixround1.patch, 13.7 KB (added by dagss, 11 years ago)
  • sage/matrix/docs.py

    # HG changeset patch
    # User Dag Sverre Seljebotn <dagss@student.matnat.uio.no>
    # Date 1261394254 -3600
    # Node ID 7f7c7a6fe810047035fd2bb9184801068296ee31
    # Parent  2815a797fade5d3fdc25baec8130015e3f142909
    [mq]: trac_7723-fixround1
    
    diff -r 2815a797fade -r 7f7c7a6fe810 sage/matrix/docs.py
    a b  
    184184       * _generic_matrix_times_matrix_ -- multiply two matrices with compatible dimensions but
    185185                                          different base rings (for optimized code paths
    186186                                          that works between different base rings or
    187                                           sparse-dense-multiplication)
     187                                          sparse-dense-multiplication). NOTE: One must also
     188                                          implement _matrix_times_matrix_!
    188189       * cdef _cmp_c_impl -- compare two matrices with identical parents
    189190       * cdef _lmul_ -- multiply this matrix on the right by a scalar, i.e., self * scalar
    190191       * cdef _rmul_ -- multiply this matrix on the left by a scalar, i.e., scalar * self
  • sage/matrix/matrix0.pyx

    diff -r 2815a797fade -r 7f7c7a6fe810 sage/matrix/matrix0.pyx
    a b  
    34473447                                           sage.structure.element.Matrix right,
    34483448                                           Parent codomain):
    34493449        r"""
    3450         Return the product of two matrices.
     3450        Return the product of two matrices with different parents.
     3451
     3452        NOTE that even if this method is overrided, one should likely
     3453        also override ``_matrix_times_matrix`` for the case where
     3454        parents are identical.
    34513455
    34523456        The matrices may have different parents, but are guaranteed to
    34533457        have a multiplication codomain (e.g. they have conforming
     
    34613465
    34623466        Specific matrix implementations might override this to provide faster
    34633467        operations (in particular, perform sparse-dense multiplication).
     3468
    34643469        """
    34653470        cdef sage.structure.element.Matrix A = self, B = right
    34663471        cdef bint left_sparse = self.is_sparse_c()
     
    34823487        r"""
    34833488        Return the product of two matrices.
    34843489
    3485         The matrices should both the same base ring and either both
     3490        The matrices should both have the same base ring and either both
    34863491        should be dense or both sparse.
    34873492       
    34883493        EXAMPLE of matrix times matrix over same base ring: We multiply
  • sage/matrix/matrix_double_dense.pyx

    diff -r 2815a797fade -r 7f7c7a6fe810 sage/matrix/matrix_double_dense.pyx
    a b  
    437437            [ 38.0  44.0  50.0  56.0]
    438438            [ 83.0  98.0 113.0 128.0]
    439439            [128.0 152.0 176.0 200.0]
    440        """
     440
     441            sage: x=matrix(RDF, 3, 0) * matrix(RDF, 0, 4); x; parent(x)
     442            [0.0 0.0 0.0 0.0]
     443            [0.0 0.0 0.0 0.0]
     444            [0.0 0.0 0.0 0.0]
     445            Full MatrixSpace of 3 by 4 dense matrices over Real Double Field
     446            sage: x=matrix(RDF, 0, 3) * matrix(RDF, 3, 4); x; parent(x)
     447            []
     448            Full MatrixSpace of 0 by 4 dense matrices over Real Double Field
     449            sage: x=matrix(RDF, 2, 3) * matrix(RDF, 3, 0); x; parent(x)
     450            []
     451            Full MatrixSpace of 2 by 0 dense matrices over Real Double Field
     452
     453            sage: x=matrix(RDF, 3, 0) * matrix(RDF, 0, 4, sparse=True); x; parent(x)
     454            [0.0 0.0 0.0 0.0]
     455            [0.0 0.0 0.0 0.0]
     456            [0.0 0.0 0.0 0.0]
     457            Full MatrixSpace of 3 by 4 dense matrices over Real Double Field
     458            sage: x=matrix(RDF, 0, 3) * matrix(RDF, 3, 4, sparse=True); x; parent(x)
     459            []
     460            Full MatrixSpace of 0 by 4 dense matrices over Real Double Field
     461            sage: x=matrix(RDF, 2, 3) * matrix(RDF, 3, 0, sparse=True); x; parent(x)
     462            []
     463            Full MatrixSpace of 2 by 0 dense matrices over Real Double Field
     464
     465        """
    441466        if self._nrows == 0 or self._ncols == 0 or right._nrows == 0 or right._ncols == 0:
    442467            return codomain.zero_matrix()
    443468
    444469        cdef Matrix_double_dense left, M
    445470
    446471        # There's no real advantage to have a mixed complex/real code path here,
    447         # change both operands to same base ring
     472        # change both operands to same base ring (avoiding change_ring could
     473        # reduce constant overhead though)
    448474        left = self
    449475        base = codomain._base
    450476        if left._parent._base is not base:
     477            # can only result in RDF->CDF or CDF->RDF
    451478            left = left.change_ring(base)
    452479        if right._parent._base is not base:
    453480            right = right.change_ring(base)
    454481
    455         if numpy is None:
    456             import numpy
    457         M = create_uninitialized_Matrix_double_dense(codomain, left.__class__)
    458482        if right.is_dense_c():
    459             M._matrix_numpy = numpy.dot(left._matrix_numpy,
    460                                         (<Matrix_double_dense?>right)._matrix_numpy)
     483            return left._matrix_times_matrix_(right)
    461484        else:
     485            M = create_uninitialized_Matrix_double_dense(codomain, left.__class__)
    462486            # Do a transposed multiplication -- scipy.sparse only defines right-mul.
    463487            # Avoid using Sage matrices to avoid some data copying -- transposing
    464488            # a scipy.sparse CSC matrix results in a CSR matrix of the same data,
     
    469493            M._matrix_numpy = numpy.ascontiguousarray((right_T * left_T).T)
    470494        return M
    471495
     496    cdef sage.structure.element.Matrix _matrix_times_matrix_(self,
     497                                                             sage.structure.element.Matrix right):
     498        """
     499        Multiply self*right as matrices in the case where parents are identical.
     500
     501        TESTS::
     502
     503            sage: A = matrix(RDF,3,range(1,10))
     504            sage: B = matrix(RDF,3,range(1,13))
     505            sage: A*B
     506            [ 38.0  44.0  50.0  56.0]
     507            [ 83.0  98.0 113.0 128.0]
     508            [128.0 152.0 176.0 200.0]
     509       
     510        """
     511        global numpy
     512        if self._nrows == 0 or self._ncols == 0 or right._nrows == 0 or right._ncols == 0:
     513            return self.matrix_space(self._nrows, right._ncols).zero_matrix()
     514        cdef Matrix_double_dense M = self._new(self._nrows, right._ncols)
     515        if numpy is None:
     516            import numpy
     517        M._matrix_numpy = numpy.dot(self._matrix_numpy,
     518                                    (<Matrix_double_dense?>right)._matrix_numpy)
     519        return M
     520
    472521    # cdef int _cmp_c_impl(self, Matrix right) except -2:
    473522    def __invert__(self):
    474523        """
  • sage/matrix/matrix_double_sparse.pyx

    diff -r 2815a797fade -r 7f7c7a6fe810 sage/matrix/matrix_double_sparse.pyx
    a b  
    566566    # - _add_
    567567    # - __neg__
    568568    # - _generic_matrix_times_matrix_
     569    # - _matrix_times_matrix_
     570    # - _matrix_times_vector_
    569571    ########################################################################
    570572   
    571573    cpdef ModuleElement _lmul_(self, RingElement right):
     
    652654        """
    653655        Multiply ``self`` with ``right``.
    654656
    655         Overrides generic version to provide:
    656        
    657          - faster code paths for sparse times dense
    658          - complex times real uses less memory (indices not copied)
     657        Overrides generic version to provide faster code path for sparse times dense.
    659658
    660659        TESTS:
    661660
     
    697696            [ 38.0  44.0  50.0  56.0]
    698697            [ 83.0  98.0 113.0 128.0]
    699698            [128.0 152.0 176.0 200.0]
     699
     700        Test zero-size matrices::
     701
     702            sage: x=matrix(RDF, 3, 0, sparse=True) * matrix(RDF, 0, 4, sparse=False); x; parent(x)
     703            [0.0 0.0 0.0 0.0]
     704            [0.0 0.0 0.0 0.0]
     705            [0.0 0.0 0.0 0.0]
     706            Full MatrixSpace of 3 by 4 dense matrices over Real Double Field
     707            sage: x=matrix(RDF, 0, 3, sparse=True) * matrix(RDF, 3, 4, sparse=False); x; parent(x)
     708            []
     709            Full MatrixSpace of 0 by 4 dense matrices over Real Double Field
     710            sage: x=matrix(RDF, 2, 3, sparse=True) * matrix(RDF, 3, 0, sparse=False); x; parent(x)
     711            []
     712            Full MatrixSpace of 2 by 0 dense matrices over Real Double Field
    700713           
    701714        A.dense_matrix()*B is handled in ``Matrix_double_dense``.
    702715        """
    703716        cdef sage.structure.element.Matrix left
    704         cdef Matrix_double_sparse result_sparse, right_sparse
    705717        cdef Matrix_double_dense result_dense, right_dense
    706718
    707719        if self._nrows == 0 or self._ncols == 0 or right._nrows == 0 or right._ncols == 0:
     
    713725            # forward call.
    714726            left = self.change_ring(codomain._base)
    715727            return left._generic_matrix_times_matrix(right, codomain)
     728        if right._parent._base is not base:
     729            right = right.change_ring(codomain._base)
    716730
    717         # Note: Codomain base ring may be either of RDF or CDF.
    718         # Codomain is dense if and only if right.is_dense().
    719731        if right.is_dense_c():
    720732            if VERBOSE: print 'Sparse times dense'
    721733            result_dense = create_uninitialized_Matrix_double_dense(codomain)
    722734            result_dense._matrix_numpy = (self._entries_csc *
    723735                                          (<Matrix_double_dense?>right)._matrix_numpy)
    724             assert result_dense._matrix_numpy.dtype == result_dense._numpy_dtype
    725736            return result_dense
    726737        else:
    727             if VERBOSE: print 'Sparse times sparse'
    728             if right._parent._base is not base:
    729                 right = right.change_ring(codomain._base)
    730             result_sparse = create_uninitialized_Matrix_double_sparse(codomain)
    731             result_sparse._entries_csc = (self._entries_csc *
    732                                           (<Matrix_double_sparse?>right)._entries_csc).tocsc()
    733             assert result_sparse._entries_csc.dtype == result_sparse._numpy_dtype
    734             return result_sparse
     738            return self._matrix_times_matrix_(right)
     739
     740    cdef sage.structure.element.Matrix _matrix_times_matrix_(self,
     741            sage.structure.element.Matrix right):
     742        """
     743        Multiply ``self`` with ``right`` with the same parents.
     744
     745        TESTS:
     746
     747            sage: from sage.matrix.matrix_double_sparse import _verbose
     748            sage: A = matrix(RDF,3,range(1,10),sparse=True)
     749            sage: B = matrix(RDF,3,range(1,13),sparse=True)
     750            sage: with _verbose(): A*B
     751            Sparse times sparse
     752            [ 38.0  44.0  50.0  56.0]
     753            [ 83.0  98.0 113.0 128.0]
     754            [128.0 152.0 176.0 200.0]
     755
     756        Test zero-size matrices::
     757
     758            sage: x=matrix(RDF, 3, 0, sparse=True) * matrix(RDF, 0, 4, sparse=True); x; parent(x)
     759            [0.0 0.0 0.0 0.0]
     760            [0.0 0.0 0.0 0.0]
     761            [0.0 0.0 0.0 0.0]
     762            Full MatrixSpace of 3 by 4 sparse matrices over Real Double Field
     763            sage: x=matrix(RDF, 0, 3, sparse=True) * matrix(RDF, 3, 4, sparse=True); x; parent(x)
     764            []
     765            Full MatrixSpace of 0 by 4 sparse matrices over Real Double Field
     766            sage: x=matrix(RDF, 2, 3, sparse=True) * matrix(RDF, 3, 0, sparse=True); x; parent(x)
     767            []
     768            Full MatrixSpace of 2 by 0 sparse matrices over Real Double Field
     769           
     770        """
     771        cdef sage.structure.element.Matrix left
     772        cdef Matrix_double_sparse result_sparse
     773        if VERBOSE: print 'Sparse times sparse'
     774
     775        codomain = self.matrix_space(self._nrows, right._ncols)
     776        if self._nrows == 0 or self._ncols == 0 or right._nrows == 0 or right._ncols == 0:
     777            return codomain.zero_matrix()
     778        result_sparse = create_uninitialized_Matrix_double_sparse(codomain)
     779        result_sparse._entries_csc = (self._entries_csc *
     780                                      (<Matrix_double_sparse?>right)._entries_csc).tocsc()
     781        return result_sparse
    735782
    736783    cdef Vector _matrix_times_vector_(self, Vector v):
    737784        """
  • sage/matrix/matrix_integer_2x2.pyx

    diff -r 2815a797fade -r 7f7c7a6fe810 sage/matrix/matrix_integer_2x2.pyx
    a b  
    6060        """
    6161        return "Space of 2x2 integer matrices"
    6262   
    63     def _get_matrix_class(self):
     63    def _get_matrix_constructor_info(self):
    6464        """
    6565        EXAMPLES:
    6666            sage: A = sage.matrix.matrix_integer_2x2.MatrixSpace_ZZ_2x2()
    67             sage: A._get_matrix_class()
    68             <type 'sage.matrix.matrix_integer_2x2.Matrix_integer_2x2'>
     67            sage: A._get_matrix_constructor_info()
     68            (<type 'sage.matrix.matrix_integer_2x2.Matrix_integer_2x2'>, ())
    6969        """
    70         return Matrix_integer_2x2
     70        return (Matrix_integer_2x2, ())
    7171
    7272ZZ_2x2_parent = MatrixSpace_ZZ_2x2_class()
    7373def MatrixSpace_ZZ_2x2():
  • sage/matrix/matrix_space.py

    diff -r 2815a797fade -r 7f7c7a6fe810 sage/matrix/matrix_space.py
    a b  
    10591059    def matrix(self, x=0, coerce=True, copy=True, rows=True, format=None):
    10601060        """
    10611061        Create a matrix in self. The entries ``x`` can be specified
    1062         either as a single list of length ``nrows\*ncols``, or as a
     1062        either as a single list of length ``nrows*ncols``, or as a
    10631063        list of lists. If the matrix space is sparse the entries can
    10641064        also be given as a dict mapping entry coordinates to
    10651065        values. If ``format=='coo'``, ``x`` should specify the entries