Skip to content

Commit 9ec29f1

Browse files
committed
[Python] Add docs info into matrix class
1 parent c766897 commit 9ec29f1

File tree

2 files changed

+107
-1
lines changed

2 files changed

+107
-1
lines changed

python/pycubool/matrix.py

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,31 @@ def build(self, rows, cols, is_sorted=False, no_duplicates=False):
110110
bridge.check(status)
111111

112112
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+
113138
hnd = ctypes.c_void_p(0)
114139

115140
status = wrapper.loaded_dll.cuBool_Matrix_Duplicate(
@@ -120,6 +145,30 @@ def dup(self):
120145
return Matrix(hnd)
121146

122147
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+
123172
shape = (self.ncols, self.nrows)
124173
out = Matrix.empty(shape)
125174

@@ -132,6 +181,11 @@ def transpose(self):
132181

133182
@property
134183
def nrows(self) -> int:
184+
"""
185+
Query number of rows of the `self` matrix.
186+
:return: Number of rows
187+
"""
188+
135189
result = ctypes.c_uint(0)
136190

137191
status = wrapper.loaded_dll.cuBool_Matrix_Nrows(
@@ -143,6 +197,11 @@ def nrows(self) -> int:
143197

144198
@property
145199
def ncols(self) -> int:
200+
"""
201+
Query number of columns of the `self` matrix.
202+
:return: Number of columns
203+
"""
204+
146205
result = ctypes.c_uint(0)
147206

148207
status = wrapper.loaded_dll.cuBool_Matrix_Ncols(
@@ -154,6 +213,11 @@ def ncols(self) -> int:
154213

155214
@property
156215
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+
157221
result = ctypes.c_uint(0)
158222

159223
status = wrapper.loaded_dll.cuBool_Matrix_Nvals(
@@ -165,12 +229,26 @@ def nvals(self) -> int:
165229

166230
@property
167231
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+
168237
return self.nrows, self.ncols
169238

170239
def to_lists(self):
171240
"""
172241
Read matrix data as lists of `rows` and `clos` indices.
173242
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+
174252
:return: Pair with `rows` and `cols` lists
175253
"""
176254

@@ -188,6 +266,25 @@ def to_lists(self):
188266

189267
return rows, cols
190268

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+
191288
def to_string(self, width=3):
192289
"""
193290
Return a string representation of the matrix.
@@ -232,7 +329,7 @@ def to_string(self, width=3):
232329
line += "| " + format_str.format(i) + "\n"
233330
result += line
234331

235-
result += header
332+
result += header + "\n"
236333
return result
237334

238335
def extract_matrix(self, i, j, shape, out=None):

python/test_app.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,12 @@ def gen_matrix(size, seed):
9898
print(a)
9999
a[3, 3] = True
100100
print(a)
101+
102+
a = pycubool.Matrix.empty(shape=(4, 4))
103+
a[0, 0] = True
104+
a[1, 3] = True
105+
a[1, 0] = True
106+
a[2, 2] = True
107+
vals = a.to_list()
108+
print(vals)
109+

0 commit comments

Comments
 (0)