def dot(self, o) -> "CscMat": """ Dot product :param o: CscMat instance :return: CscMat instance """ # 2-pass matrix multiplication Cp = np.empty(self.n + 1, dtype=np.int32) sptools.csc_matmat_pass1(self.n, o.m, self.indptr, self.indices, o.indptr, o.indices, Cp) nnz = Cp[-1] Ci = np.empty(nnz, dtype=np.int32) Cx = np.empty(nnz, dtype=np.float64) sptools.csc_matmat_pass2(self.n, o.m, self.indptr, self.indices, self.data, o.indptr, o.indices, o.data, Cp, Ci, Cx) return CscMat((Cx, Ci, Cp), shape=self.shape)
def csc_dot2(A: csc_matrix, B: csc_matrix): """ :param A: :param B: :return: """ Cp = np.empty(A.shape[0] + 1, dtype=np.int32) sptools.csc_matmat_pass1(A.shape[0], B.shape[1], A.indptr, A.indices, B.indptr, B.indices, Cp) nnz = Cp[-1] Ci = np.empty(nnz, dtype=np.int32) Cx = np.empty(nnz, dtype=np.float64) sptools.csc_matmat_pass2(A.shape[0], B.shape[1], A.indptr, A.indices, A.data, B.indptr, B.indices, B.data, Cp, Ci, Cx) N, M = A.shape[0], B.shape[1] C = csc_matrix((Cx, Ci, Cp), shape=(N, M)) return C
def __mul__(self, other): """ Matrix multiplication :param other: CscMat instance :return: CscMat instance """ if isinstance(other, CscMat): # mat-mat multiplication # 2-pass matrix multiplication Cp = np.empty(self.n + 1, dtype=np.int32) sptools.csc_matmat_pass1(self.n, other.m, self.indptr, self.indices, other.indptr, other.indices, Cp) nnz = Cp[-1] Ci = np.empty(nnz, dtype=np.int32) Cx = np.empty(nnz, dtype=np.float64) sptools.csc_matmat_pass2(self.n, other.m, self.indptr, self.indices, self.data, other.indptr, other.indices, other.data, Cp, Ci, Cx) return CscMat(n=self.m, m=other.m, indptr=Cp, indices=Ci, data=Cx) elif isinstance( other, np.ndarray): # multiply by a vector or array of vectors if len(other.shape) == 1: y = np.zeros(self.m, dtype=np.float64) sptools.csc_matvec(self.m, self.n, self.indptr, self.indices, self.data, other, y) return y elif len(other.shape) == 2: ''' * Input Arguments: * I n_row - number of rows in A * I n_col - number of columns in A * I n_vecs - number of column vectors in X and Y * I Ap[n_row+1] - row pointer * I Aj[nnz(A)] - column indices * T Ax[nnz(A)] - nonzeros * T Xx[n_col,n_vecs] - input vector * * Output Arguments: * T Yx[n_row,n_vecs] - output vector * * Note: * Output array Yx must be preallocated * void csc_matvecs(const I n_row, const I n_col, const I n_vecs, const I Ap[], const I Ai[], const T Ax[], const T Xx[], T Yx[]) ''' n_col, n_vecs = other.shape y = np.zeros((self.m, n_vecs), dtype=np.float64) sptools.csc_matvecs(self.m, self.n, n_vecs, self.indptr, self.indices, self.data, other, y) return y elif isinstance(other, float) or isinstance( other, int): # multiply by a scalar value C = self.copy() C.data *= other return C else: raise Exception('Type not supported')