@@ -77,7 +77,7 @@ def empty(cls, shape):
77
77
@classmethod
78
78
def from_lists (cls , shape , rows , cols , is_sorted = False , no_duplicates = False ):
79
79
"""
80
- Build matrix from provided `shape` and non-zero values data.
80
+ Create matrix from provided `shape` and non-zero values data.
81
81
82
82
>>> matrix = Matrix.from_lists((4, 4), [0, 1, 2, 3], [0, 1, 2, 0], is_sorted=True, no_duplicates=True)
83
83
>>> print(matrix)
@@ -260,7 +260,7 @@ def set_marker(self, marker: str):
260
260
'meow (0x1a767b0)'
261
261
262
262
:param marker: String marker to set
263
- :return: None
263
+ :return:
264
264
"""
265
265
266
266
assert marker is not None
@@ -492,6 +492,23 @@ def extract_matrix(self, i, j, shape, out=None, time_check=False):
492
492
return out
493
493
494
494
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
+
495
512
if out is None :
496
513
out = vector .Vector .empty (self .ncols )
497
514
@@ -506,6 +523,24 @@ def extract_row(self, i, out=None):
506
523
return out
507
524
508
525
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
+
509
544
if out is None :
510
545
out = vector .Vector .empty (self .nrows )
511
546
@@ -562,6 +597,29 @@ def mxm(self, other, out=None, accumulate=False, time_check=False):
562
597
return out
563
598
564
599
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
+
565
623
if out is None :
566
624
out = vector .Vector .empty (self .nrows )
567
625
@@ -654,9 +712,9 @@ def ewiseadd(self, other, out=None, time_check=False):
654
712
bridge .check (status )
655
713
return out
656
714
657
- def reduce (self , time_check = False ):
715
+ def reduce (self , out = None , time_check = False ):
658
716
"""
659
- Reduce matrix to vector with boolean "+ = or" operation.
717
+ Reduce matrix to column matrix with boolean "+ = or" operation.
660
718
Return `self` reduced matrix.
661
719
662
720
>>> matrix = Matrix.from_lists((4, 4), [0, 1, 2, 2], [0, 1, 0, 2])
@@ -670,12 +728,14 @@ def reduce(self, time_check=False):
670
728
0
671
729
'
672
730
731
+ :param out: Optional out matrix to store result
673
732
:param time_check: Pass True to measure and log elapsed time of the operation
674
733
:return: Reduced matrix (matrix with M x 1 shape)
675
734
"""
676
735
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 )
679
739
680
740
status = wrapper .loaded_dll .cuBool_Matrix_Reduce2 (
681
741
out .hnd ,
@@ -687,6 +747,31 @@ def reduce(self, time_check=False):
687
747
return out
688
748
689
749
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
+
690
775
if out is None :
691
776
nrows = self .ncols if transpose else self .nrows
692
777
out = vector .Vector .empty (nrows )
@@ -795,7 +880,7 @@ def __getitem__(self, item):
795
880
796
881
def __setitem__ (self , key , value ):
797
882
"""
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.
799
884
800
885
>>> matrix = Matrix.empty(shape=(4, 4))
801
886
>>> matrix[0, 0] = True
0 commit comments