@@ -110,6 +110,31 @@ def build(self, rows, cols, is_sorted=False, no_duplicates=False):
110
110
bridge .check (status )
111
111
112
112
def dup (self ):
113
+ """
114
+ Creates new matrix instance, the exact copy of the `self`
115
+
116
+ >>> a = Matrix.from_lists((4, 4), [0, 1, 2, 3], [0, 1, 2, 0], is_sorted=True, no_duplicates=True)
117
+ >>> b = a.dup()
118
+ >>> b[3, 3] = True
119
+ >>> print(a, b, sep="")
120
+ '
121
+ 0 1 2 3
122
+ 0 | 1 . . . | 0
123
+ 1 | . 1 . . | 1
124
+ 2 | . . 1 . | 2
125
+ 3 | 1 . . . | 3
126
+ 0 1 2 3
127
+ 0 1 2 3
128
+ 0 | 1 . . . | 0
129
+ 1 | . 1 . . | 1
130
+ 2 | . . 1 . | 2
131
+ 3 | 1 . . 1 | 3
132
+ 0 1 2 3
133
+ '
134
+
135
+ :return: New matrix instance with `self` copied data
136
+ """
137
+
113
138
hnd = ctypes .c_void_p (0 )
114
139
115
140
status = wrapper .loaded_dll .cuBool_Matrix_Duplicate (
@@ -120,6 +145,30 @@ def dup(self):
120
145
return Matrix (hnd )
121
146
122
147
def transpose (self ):
148
+ """
149
+ Creates new transposed `self` matrix.
150
+
151
+ >>> a = Matrix.from_lists((4, 4), [0, 1, 2, 3], [0, 1, 2, 0], is_sorted=True, no_duplicates=True)
152
+ >>> b = a.transpose()
153
+ >>> print(a, b, sep="")
154
+ '
155
+ 0 1 2 3
156
+ 0 | 1 . . . | 0
157
+ 1 | . 1 . . | 1
158
+ 2 | . . 1 . | 2
159
+ 3 | 1 . . . | 3
160
+ 0 1 2 3
161
+ 0 1 2 3
162
+ 0 | 1 . . 1 | 0
163
+ 1 | . 1 . . | 1
164
+ 2 | . . 1 . | 2
165
+ 3 | . . . . | 3
166
+ 0 1 2 3
167
+ '
168
+
169
+ :return: New matrix instance with `self` transposed data
170
+ """
171
+
123
172
shape = (self .ncols , self .nrows )
124
173
out = Matrix .empty (shape )
125
174
@@ -132,6 +181,11 @@ def transpose(self):
132
181
133
182
@property
134
183
def nrows (self ) -> int :
184
+ """
185
+ Query number of rows of the `self` matrix.
186
+ :return: Number of rows
187
+ """
188
+
135
189
result = ctypes .c_uint (0 )
136
190
137
191
status = wrapper .loaded_dll .cuBool_Matrix_Nrows (
@@ -143,6 +197,11 @@ def nrows(self) -> int:
143
197
144
198
@property
145
199
def ncols (self ) -> int :
200
+ """
201
+ Query number of columns of the `self` matrix.
202
+ :return: Number of columns
203
+ """
204
+
146
205
result = ctypes .c_uint (0 )
147
206
148
207
status = wrapper .loaded_dll .cuBool_Matrix_Ncols (
@@ -154,6 +213,11 @@ def ncols(self) -> int:
154
213
155
214
@property
156
215
def nvals (self ) -> int :
216
+ """
217
+ Query number of non-zero values of the `self` matrix.
218
+ :return: Number of non-zero values
219
+ """
220
+
157
221
result = ctypes .c_uint (0 )
158
222
159
223
status = wrapper .loaded_dll .cuBool_Matrix_Nvals (
@@ -165,12 +229,26 @@ def nvals(self) -> int:
165
229
166
230
@property
167
231
def shape (self ) -> (int , int ):
232
+ """
233
+ Query shape of `self` matrix as (nrows, ncols) tuple.
234
+ :return: Return tuple of (nrows, ncols)
235
+ """
236
+
168
237
return self .nrows , self .ncols
169
238
170
239
def to_lists (self ):
171
240
"""
172
241
Read matrix data as lists of `rows` and `clos` indices.
173
242
243
+ >>> a = Matrix.empty(shape=(4, 4))
244
+ >>> a[0, 0] = True
245
+ >>> a[1, 3] = True
246
+ >>> a[1, 0] = True
247
+ >>> a[2, 2] = True
248
+ >>> rows, cols = a.to_lists()
249
+ >>> print(list(rows), list(cols))
250
+ '[0, 1, 1, 2] [0, 0, 3, 2]'
251
+
174
252
:return: Pair with `rows` and `cols` lists
175
253
"""
176
254
@@ -188,6 +266,25 @@ def to_lists(self):
188
266
189
267
return rows , cols
190
268
269
+ def to_list (self ):
270
+ """
271
+ Read matrix values as list of (i,j) pairs.
272
+
273
+ >>> a = Matrix.empty(shape=(4, 4))
274
+ >>> a[0, 0] = True
275
+ >>> a[1, 3] = True
276
+ >>> a[1, 0] = True
277
+ >>> a[2, 2] = True
278
+ >>> vals = a.to_list()
279
+ >>> print(vals)
280
+ '[(0, 0), (1, 0), (1, 3), (2, 2)]'
281
+
282
+ :return: List of (i, j) pairs
283
+ """
284
+
285
+ I , J = self .to_lists ()
286
+ return list (zip (I , J ))
287
+
191
288
def to_string (self , width = 3 ):
192
289
"""
193
290
Return a string representation of the matrix.
@@ -232,7 +329,7 @@ def to_string(self, width=3):
232
329
line += "| " + format_str .format (i ) + "\n "
233
330
result += line
234
331
235
- result += header
332
+ result += header + " \n "
236
333
return result
237
334
238
335
def extract_matrix (self , i , j , shape , out = None ):
0 commit comments