Ticket #10568: 10568-speedup-QQmatrix-lmul.2

File 10568-speedup-QQmatrix-lmul.2, 1.4 KB (added by mderickx, 10 years ago)
Line
1#10568 speeds up scalar multiplication of sparse matrices a lot
2
3diff -r 2d2ae890de4a sage/matrix/matrix_sparse.pyx
4--- a/sage/matrix/matrix_sparse.pyx     Tue Jan 04 12:33:38 2011 +0100
5+++ b/sage/matrix/matrix_sparse.pyx     Fri Jan 07 18:08:22 2011 +0100
6@@ -6,6 +6,7 @@
7 cimport matrix0
8 from sage.structure.element cimport Element, Vector
9 from sage.rings.ring import is_Ring
10+from sage.misc.misc import verbose
11
12 include '../ext/cdefs.pxi'
13 include '../ext/stdsage.pxi'
14@@ -287,6 +288,29 @@
15
16         return left.new_matrix(left._nrows, right._ncols, entries=e, coerce=False, copy=False)
17
18+    def _lmul_(self,s):
19+        """
20+        Left scalar multiplication
21+
22+        EXAMPLES::
23+
24+        sage: M=Matrix(QQ,3,6,xrange(18),sparse=true); M
25+        sage: (2/3)*M
26+        sage: 7*M
27+        sage: (1/4)*M
28+        sage: M=MatrixSpace(QQ,530,720,sparse=true)
29+        sage: m=M.random_element(density=4/720)
30+        sage: m==(97/42)*(42/97*m)
31+        """
32+        t=verbose("scalar multiplying",level=2,caller_name="_lmul_")
33+        M=self
34+        if self._mutability._is_immutable:
35+            M=self.__copy__()
36+        for key in M._dict():
37+            M._dict()[key]=s*M._dict()[key]
38+        verbose("scalar multiplying finished",t,level=2,caller_name="_lmul_")
39+        return M
40+
41     cdef bint _will_use_strassen(self, matrix0.Matrix right) except -2:
42         # never use Strassen for sparse matrix multiply
43         return 0