Ticket #10734: trac_10734.dense_matrix_and_submatrix

File trac_10734.dense_matrix_and_submatrix, 4.5 KB (added by Gonzalo Tornaría, 11 years ago)

For matrix_modn_sparse: implement optimized versions of submatrix(), dense_matrix(), add methods dense_submatrix() and set_block_unsafe()

Line 
1# HG changeset patch
2# Date 1296612000 7200
3# User Gonzalo Tornaría <tornaria@cmat.edu.uy>
4# Parent ce324e28c3334398d3552640e2cb1520d22465a3
5For matrix_modn_sparse: implement optimized versions of submatrix(), dense_matrix(), add methods dense_submatrix() and set_block_unsafe()
6
7diff -r ce324e28c333 sage/matrix/matrix_modn_dense.pxd
8--- a/sage/matrix/matrix_modn_dense.pxd Mon May 23 13:36:51 2011 +0000
9+++ b/sage/matrix/matrix_modn_dense.pxd Fri Jun 10 11:51:07 2011 -0300
10@@ -14,6 +14,7 @@
11     #cdef mod_int **get_matrix(Matrix_modn_dense self)
12     #cdef mod_int entry(self, mod_int i, mod_int j)
13     cdef set_unsafe_int(self, Py_ssize_t i, Py_ssize_t j, int value)
14+    cdef get_unsafe_int(self, Py_ssize_t i, Py_ssize_t j)
15     cdef _rescale_row_c(self, Py_ssize_t row, mod_int multiple, Py_ssize_t start_col)
16     cdef _rescale_col_c(self, Py_ssize_t col, mod_int multiple, Py_ssize_t start_row)   
17     cdef _add_multiple_of_row_c(self,  Py_ssize_t row_to, Py_ssize_t row_from,
18diff -r ce324e28c333 sage/matrix/matrix_modn_dense.pyx
19--- a/sage/matrix/matrix_modn_dense.pyx Mon May 23 13:36:51 2011 +0000
20+++ b/sage/matrix/matrix_modn_dense.pyx Fri Jun 10 11:51:07 2011 -0300
21@@ -355,6 +355,9 @@
22         """       
23         self._matrix[i][j] = (<IntegerMod_int> value).ivalue
24 
25+    cdef get_unsafe_int(self, Py_ssize_t i, Py_ssize_t j):
26+        return self._matrix[i][j]
27+
28     cdef get_unsafe(self, Py_ssize_t i, Py_ssize_t j):
29         cdef IntegerMod_int n
30         n =  IntegerMod_int.__new__(IntegerMod_int)
31diff -r ce324e28c333 sage/matrix/matrix_modn_sparse.pxd
32--- a/sage/matrix/matrix_modn_sparse.pxd        Mon May 23 13:36:51 2011 +0000
33+++ b/sage/matrix/matrix_modn_sparse.pxd        Fri Jun 10 11:51:07 2011 -0300
34@@ -1,4 +1,4 @@
35-cimport matrix_sparse
36+from matrix_modn_dense cimport Matrix_modn_dense
37 
38 include '../modules/vector_modn_sparse_h.pxi'
39 
40@@ -6,5 +6,6 @@
41     cdef c_vector_modint* rows
42     cdef public int p
43     cdef swap_rows_c(self, Py_ssize_t n1, Py_ssize_t n2)
44+    cdef set_block_unsafe(self, Py_ssize_t row, Py_ssize_t col, Matrix_modn_dense block)
45 
46     cdef _init_linbox(self)
47diff -r ce324e28c333 sage/matrix/matrix_modn_sparse.pyx
48--- a/sage/matrix/matrix_modn_sparse.pyx        Mon May 23 13:36:51 2011 +0000
49+++ b/sage/matrix/matrix_modn_sparse.pyx        Fri Jun 10 11:51:07 2011 -0300
50@@ -525,6 +525,59 @@
51         self.cache('pivots',tuple(pivots))
52         self.cache('in_echelon_form',True)
53 
54+    def submatrix(self, Py_ssize_t row=0, Py_ssize_t col=0,
55+                        Py_ssize_t nrows=-1, Py_ssize_t ncols=-1):
56+        cdef Py_ssize_t i, j, pos
57+        cdef Matrix_modn_sparse M
58+
59+        if nrows == -1:
60+           nrows = self._nrows - row
61+        if ncols == -1:
62+           ncols = self._ncols - col
63+
64+        M = self.new_matrix(nrows, ncols)
65+        for i from 0 <= i < nrows:
66+          for j from 0 <= j < self.rows[row+i].num_nonzero:
67+            pos = self.rows[row+i].positions[j] - col
68+            if 0 <= pos < ncols:
69+              set_entry(&M.rows[i], pos, self.rows[row+i].entries[j])
70+        return M
71+
72+    def dense_matrix(self):
73+        cdef Py_ssize_t i, j
74+        cdef Matrix_modn_dense M
75+
76+        M = self.new_matrix(self._nrows, self._ncols, sparse=False)
77+        for i from 0 <= i < self._nrows:
78+            for j from 0 <= j < self.rows[i].num_nonzero:
79+                M.set_unsafe_int(i, self.rows[i].positions[j], self.rows[i].entries[j])
80+        M.subdivide(self.get_subdivisions())
81+        return M
82+
83+    def dense_submatrix(self, Py_ssize_t row=0, Py_ssize_t col=0,
84+                              Py_ssize_t nrows=-1, Py_ssize_t ncols=-1):
85+        cdef Py_ssize_t i, j, pos
86+        cdef Matrix_modn_dense M
87+
88+        if nrows == -1:
89+          nrows = self._nrows - row
90+        if ncols == -1:
91+          ncols = self._ncols - col
92+
93+        M = self.new_matrix(nrows, ncols, sparse=False)
94+        for i from 0 <= i < nrows:
95+          for j from 0 <= j < self.rows[row+i].num_nonzero:
96+            pos = self.rows[row+i].positions[j] - col
97+            if 0 <= pos < ncols:
98+              M.set_unsafe_int(i, pos, self.rows[row+i].entries[j])
99+        return M
100+
101+    cdef set_block_unsafe(self, Py_ssize_t row, Py_ssize_t col, Matrix_modn_dense block):
102+        cdef Py_ssize_t i, j
103+        for i from 0 <= i < block.nrows():
104+            for j from 0 <= j < block.ncols():
105+                set_entry(&self.rows[row+i], col+j, block.get_unsafe_int(i,j))
106+
107     def _nonzero_positions_by_row(self, copy=True):
108         """
109         Returns the list of pairs (i,j) such that self[i,j] != 0.