Ticket #7723: trac_7723-generic_multiply.patch

File trac_7723-generic_multiply.patch, 4.7 KB (added by dagss, 11 years ago)
  • sage/matrix/action.pyx

    # HG changeset patch
    # User Dag Sverre Seljebotn <dagss@student.matnat.uio.no>
    # Date 1261045212 -3600
    # Node ID 977f52f47b09cdf5cfbca81ff74d5fd9f5c257fe
    # Parent  964c2f4ce74db0417a771de0b0cfc951b1fab73c
    imported patch structure
    
    diff -r 964c2f4ce74d -r 977f52f47b09 sage/matrix/action.pyx
    a b  
    135135        """
    136136        cdef Matrix A = g #<Matrix>g
    137137        cdef Matrix B = s #<Matrix>s
    138         if A._parent._base is not self._codomain._base:
    139             A = A.change_ring(self._codomain._base)
    140         if B._parent._base is not self._codomain._base:
    141             B = B.change_ring(self._codomain._base)
    142         if self.fix_sparseness:
    143             if B.is_sparse_c():
    144                 B = B.dense_matrix()
    145             else:
    146                 A = A.dense_matrix()
    147         prod = A._matrix_times_matrix_(B)
     138        prod = A._generic_matrix_times_matrix_(B, self._codomain)
    148139        if A.subdivisions is not None or B.subdivisions is not None:
    149140            Asubs = A.get_subdivisions()
    150141            Bsubs = B.get_subdivisions()
  • sage/matrix/matrix0.pyx

    diff -r 964c2f4ce74d -r 977f52f47b09 sage/matrix/matrix0.pyx
    a b  
    4040from   sage.structure.element    cimport ModuleElement, Element, RingElement, Vector
    4141from   sage.structure.mutability cimport Mutability
    4242from   sage.misc.misc_c cimport normalize_index
     43from sage.structure.parent cimport Parent
    4344
    4445from sage.rings.ring cimport CommutativeRing
    4546from sage.rings.ring import is_Ring
     
    34423443                ans.set_unsafe(r, c, (<RingElement>self.get_unsafe(r, c))._mul_(x))
    34433444        return ans
    34443445
     3446    cdef sage.structure.element.Matrix _generic_matrix_times_matrix_(self,
     3447                                           sage.structure.element.Matrix right,
     3448                                           Parent codomain):
     3449        r"""
     3450        Return the product of two matrices.
     3451
     3452        The matrices may have different parents, but are guaranteed to
     3453        have a multiplication codomain (e.g. they have conforming
     3454        shape).  The output should be the product of the matrices in
     3455        the ``codomain`` given.
     3456
     3457        By default:
     3458         - Change base ring to that of codomain
     3459         - If sparsity differs, make the sparse matrix dense
     3460         - Call ``_matrix_times_matrix``
     3461
     3462        Specific matrix implementations might override this to provide faster
     3463        operations (in particular, perform sparse-dense multiplication).
     3464        """
     3465        cdef sage.structure.element.Matrix A = self, B = right
     3466        cdef bint left_sparse = self.is_sparse_c()
     3467        cdef bint right_sparse = right.is_sparse_c()
     3468        base = codomain._base
     3469       
     3470        if A._parent._base is not base:
     3471            A = A.change_ring(codomain._base)
     3472        if B._parent._base is not base:
     3473            B = B.change_ring(codomain._base)
     3474        if (left_sparse == 0) != (right_sparse == 0):
     3475            if left_sparse:
     3476                A = A.dense_matrix()
     3477            else:
     3478                B = B.dense_matrix()
     3479        return A._matrix_times_matrix_(B)
     3480
    34453481    cdef sage.structure.element.Matrix _matrix_times_matrix_(self, sage.structure.element.Matrix right):
    34463482        r"""
    34473483        Return the product of two matrices.
     3484
     3485        The matrices should both the same base ring and either both
     3486        should be dense or both sparse.
    34483487       
    34493488        EXAMPLE of matrix times matrix over same base ring: We multiply
    34503489        matrices over `\QQ[x,y]`.
  • sage/structure/element.pxd

    diff -r 964c2f4ce74d -r 977f52f47b09 sage/structure/element.pxd
    a b  
    119119    cdef Vector _matrix_times_vector_(matrix_left, Vector vector_right)    # OK to override, AND call directly   
    120120    cdef Matrix _matrix_times_matrix_(left, Matrix right)                  # OK to override, AND call directly
    121121
     122    cdef Matrix _generic_matrix_times_matrix_(left, Matrix right, Parent codomain) # OK to override, AND call directly
     123
    122124    cdef bint is_sparse_c(self)
    123125    cdef bint is_dense_c(self)
    124126       
  • sage/structure/element.pyx

    diff -r 964c2f4ce74d -r 977f52f47b09 sage/structure/element.pyx
    a b  
    20012001    cdef Matrix _matrix_times_matrix_(left, Matrix right):
    20022002        raise TypeError
    20032003   
    2004    
     2004    cdef Matrix _generic_matrix_times_matrix_(left, Matrix right, Parent codomain):
     2005        raise TypeError   
    20052006
    20062007def is_Matrix(x):
    20072008    return IS_INSTANCE(x, Matrix)