Skip to content

Commit 9ab7601

Browse files
committed
[Python] Add docs for new matrix/vector classes methods with examples
1 parent 64e8893 commit 9ab7601

File tree

3 files changed

+435
-8
lines changed

3 files changed

+435
-8
lines changed

python/features.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,24 @@ def gen_matrix(size, seed):
111111
print(vals)
112112
print(a.equals(a))
113113

114+
a = pycubool.Matrix.empty(shape=(4, 4))
115+
print(a.marker)
116+
a.set_marker("meow")
117+
print(a.marker)
118+
119+
matrix = pycubool.Matrix.from_lists((5, 4), [0, 1, 2, 4], [0, 1, 1, 3])
120+
vector = pycubool.Vector.from_list(4, [0, 1, 2])
121+
print(matrix.mxv(vector))
122+
123+
matrix = pycubool.Matrix.from_lists((5, 4), [0, 1, 2, 4], [0, 1, 1, 3])
124+
print(matrix.reduce_vector(), matrix.reduce_vector(transpose=True), sep="")
125+
126+
matrix = pycubool.Matrix.from_lists((5, 4), [0, 1, 2, 4], [0, 1, 1, 3])
127+
print(matrix.extract_row(1))
128+
129+
matrix = pycubool.Matrix.from_lists((5, 4), [0, 1, 2, 4], [0, 1, 1, 3])
130+
print(matrix.extract_col(1))
131+
114132
matrix = pycubool.Matrix.generate(shape=(5, 4), density=0.5)
115133
print(matrix, matrix.extract_row(1), matrix.extract_col(2), sep="\n")
116134

@@ -125,6 +143,58 @@ def gen_matrix(size, seed):
125143
matrix = pycubool.Matrix.generate(shape=(10, 6), density=0.2)
126144
print(matrix, matrix.reduce_vector(transpose=False), matrix.reduce_vector(transpose=True), sep="\n")
127145

146+
vector = pycubool.Vector.from_list(4, [0, 1, 3], is_sorted=True, no_duplicates=True)
147+
print(vector)
148+
149+
vector = pycubool.Vector.generate(nrows=4, density=0.5)
150+
print(vector)
151+
152+
vector = pycubool.Vector.empty(4)
153+
vector.build([0, 1, 3], is_sorted=True, no_duplicates=True)
154+
print(vector)
155+
156+
a = pycubool.Vector.from_list(4, [0, 1, 3], is_sorted=True, no_duplicates=True)
157+
b = a.dup()
158+
b[2] = True
159+
print(a, b, sep="")
160+
161+
a = pycubool.Vector.empty(nrows=4)
162+
print(a.marker)
163+
a.set_marker("meow")
164+
print(a.marker)
165+
166+
a = pycubool.Vector.empty(nrows=4)
167+
a[0] = True
168+
a[2] = True
169+
a[3] = True
170+
rows = a.to_list()
171+
print(list(rows))
172+
173+
vector = pycubool.Vector.from_list(4, [0, 1, 3], is_sorted=True, no_duplicates=True)
174+
print(vector)
175+
176+
vector = pycubool.Vector.from_list(5, [0, 1, 3])
177+
print(vector.extract_vector(1, nrows=3))
178+
179+
matrix = pycubool.Matrix.from_lists((5, 4), [0, 1, 2, 4], [0, 1, 1, 3])
180+
vector = pycubool.Vector.from_list(5, [2, 3, 4])
181+
print(vector.vxm(matrix))
182+
183+
a = pycubool.Vector.from_list(4, [0, 1, 3])
184+
b = pycubool.Vector.from_list(4, [1, 2, 3])
185+
print(a.ewiseadd(b))
186+
187+
vector = pycubool.Vector.from_list(5, [2, 3, 4])
188+
print(vector.reduce())
128189

190+
vector = pycubool.Vector.from_list(5, [1, 3, 4])
191+
print(list(iter(vector)))
129192

193+
vector = pycubool.Vector.from_list(5, [1, 3, 4])
194+
print(vector[0:3], vector[2:], sep="")
130195

196+
vector = pycubool.Vector.empty(4)
197+
vector[0] = True
198+
vector[2] = True
199+
vector[3] = True
200+
print(vector)

python/pycubool/matrix.py

Lines changed: 92 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def empty(cls, shape):
7777
@classmethod
7878
def from_lists(cls, shape, rows, cols, is_sorted=False, no_duplicates=False):
7979
"""
80-
Build matrix from provided `shape` and non-zero values data.
80+
Create matrix from provided `shape` and non-zero values data.
8181
8282
>>> matrix = Matrix.from_lists((4, 4), [0, 1, 2, 3], [0, 1, 2, 0], is_sorted=True, no_duplicates=True)
8383
>>> print(matrix)
@@ -260,7 +260,7 @@ def set_marker(self, marker: str):
260260
'meow (0x1a767b0)'
261261
262262
:param marker: String marker to set
263-
:return: None
263+
:return:
264264
"""
265265

266266
assert marker is not None
@@ -492,6 +492,23 @@ def extract_matrix(self, i, j, shape, out=None, time_check=False):
492492
return out
493493

494494
def extract_row(self, i, out=None):
495+
"""
496+
Extract specified `self` matrix row as sparse vector.
497+
498+
>>> matrix = Matrix.from_lists((5, 4), [0, 1, 2, 4], [0, 1, 1, 3])
499+
>>> print(matrix.extract_row(1))
500+
'
501+
0 | . | 0
502+
1 | 1 | 1
503+
2 | . | 2
504+
3 | . | 3
505+
'
506+
507+
:param i: Row index to extract
508+
:param out: Optional out vector to store result
509+
:return: Return extracted row
510+
"""
511+
495512
if out is None:
496513
out = vector.Vector.empty(self.ncols)
497514

@@ -506,6 +523,24 @@ def extract_row(self, i, out=None):
506523
return out
507524

508525
def extract_col(self, j, out=None):
526+
"""
527+
Extract specified `self` matrix column as sparse vector.
528+
529+
>>> matrix = Matrix.from_lists((5, 4), [0, 1, 2, 4], [0, 1, 1, 3])
530+
>>> print(matrix.extract_col(1))
531+
'
532+
0 | . | 0
533+
1 | 1 | 1
534+
2 | 1 | 2
535+
3 | . | 3
536+
4 | . | 4
537+
'
538+
539+
:param j: Column index to extract
540+
:param out: Optional out vector to store result
541+
:return: Return extracted column
542+
"""
543+
509544
if out is None:
510545
out = vector.Vector.empty(self.nrows)
511546

@@ -562,6 +597,29 @@ def mxm(self, other, out=None, accumulate=False, time_check=False):
562597
return out
563598

564599
def mxv(self, other, out=None, time_check=False):
600+
"""
601+
Matrix-vector multiply.
602+
603+
Multiply `this` matrix by column `other` vector `on the right`.
604+
For row vector-matrix multiplication "on the left" see `Vector.vxm`.
605+
606+
>>> matrix = Matrix.from_lists((5, 4), [0, 1, 2, 4], [0, 1, 1, 3])
607+
>>> vector = Vector.from_list(4, [0, 1, 2])
608+
>>> print(matrix.mxv(vector))
609+
'
610+
0 | 1 | 0
611+
1 | 1 | 1
612+
2 | 1 | 2
613+
3 | . | 3
614+
4 | . | 4
615+
'
616+
617+
:param other: Input matrix for multiplication
618+
:param out: Optional out vector to store result
619+
:param time_check: Pass True to measure and log elapsed time of the operation
620+
:return: Vector-matrix multiplication result
621+
"""
622+
565623
if out is None:
566624
out = vector.Vector.empty(self.nrows)
567625

@@ -654,9 +712,9 @@ def ewiseadd(self, other, out=None, time_check=False):
654712
bridge.check(status)
655713
return out
656714

657-
def reduce(self, time_check=False):
715+
def reduce(self, out=None, time_check=False):
658716
"""
659-
Reduce matrix to vector with boolean "+ = or" operation.
717+
Reduce matrix to column matrix with boolean "+ = or" operation.
660718
Return `self` reduced matrix.
661719
662720
>>> matrix = Matrix.from_lists((4, 4), [0, 1, 2, 2], [0, 1, 0, 2])
@@ -670,12 +728,14 @@ def reduce(self, time_check=False):
670728
0
671729
'
672730
731+
:param out: Optional out matrix to store result
673732
:param time_check: Pass True to measure and log elapsed time of the operation
674733
:return: Reduced matrix (matrix with M x 1 shape)
675734
"""
676735

677-
shape = (self.nrows, 1)
678-
out = Matrix.empty(shape)
736+
if out is None:
737+
shape = (self.nrows, 1)
738+
out = Matrix.empty(shape)
679739

680740
status = wrapper.loaded_dll.cuBool_Matrix_Reduce2(
681741
out.hnd,
@@ -687,6 +747,31 @@ def reduce(self, time_check=False):
687747
return out
688748

689749
def reduce_vector(self, out=None, transpose=False, time_check=False):
750+
"""
751+
Reduce matrix to column vector with boolean "+ = or" operation.
752+
Return `self` reduced matrix.
753+
754+
>>> matrix = Matrix.from_lists((5, 4), [0, 1, 2, 4], [0, 1, 1, 3])
755+
>>> print(matrix.reduce_vector(), matrix.reduce_vector(transpose=True), sep="")
756+
'
757+
0 | 1 | 0
758+
1 | 1 | 1
759+
2 | 1 | 2
760+
3 | . | 3
761+
4 | 1 | 4
762+
763+
0 | 1 | 0
764+
1 | 1 | 1
765+
2 | . | 2
766+
3 | 1 | 3
767+
'
768+
769+
:param out: Optional out matrix to store result
770+
:param transpose: Pass True to reduce matrix to row vector
771+
:param time_check: Pass True to measure and log elapsed time of the operation
772+
:return: Reduced matrix (matrix with M x 1 shape)
773+
"""
774+
690775
if out is None:
691776
nrows = self.ncols if transpose else self.nrows
692777
out = vector.Vector.empty(nrows)
@@ -795,7 +880,7 @@ def __getitem__(self, item):
795880

796881
def __setitem__(self, key, value):
797882
"""
798-
Sets Sets specified `key` = (i, j) value of the matrix to True.
883+
Sets specified `key` = (i, j) value of the matrix to True.
799884
800885
>>> matrix = Matrix.empty(shape=(4, 4))
801886
>>> matrix[0, 0] = True

0 commit comments

Comments
 (0)