def crs_toccs(n_row, n_col, n_tube, Ap, Aj, Ak, Ax): """ Compute B = A for CRS matrix A, CSC matrix B Also, with the appropriate arguments can also be used to: - compute B = A^t for CRS matrix A, CRS matrix B - compute B = A^t for CSC matrix A, CSC matrix B - convert CSC->CRS Input Arguments: I n_row - number of rows in A I n_col - number of columns in A I Ap[n_row+1] - row pointer I Aj[nnz(A)] - column indices T Ax[nnz(A)] - nonzeros Output Arguments: I Bp[n_col+1] - column pointer I Bj[nnz(A)] - row indices T Bx[nnz(A)] - nonzeros Note: Output arrays Bp, Bj, Bx must be preallocated Note: Input: column indices *are not* assumed to be in sorted order Output: row indices *will be* in sorted order Complexity: Linear. Specifically O(nnz(A) + max(n_row,n_col)) Note: Adapted from scipy.sparse. """ nnz = Ap[n_row] Bp = cumsumdups(Aj, n_col+1) Bi = [0] * nnz Bk = [0] * nnz Bx = [0] * nnz for row in range(n_row): for jj in range(Ap[row], Ap[row+1]): col = Aj[jj] dest = Bp[col] Bi[dest] = row Bk[dest] = Ak[jj] Bx[dest] = Ax[jj] Bp[col] += 1 Bp = [0] + [Bp[i] for i in range(n_col-1)] return Bp, Bi, Bk, Bx
def coo_tocrs(n_row, n_col, n_tube, Ai, Aj, Ak, Ax): """ Compute B = A for COO matrix A, CRS matrix B Input Arguments: I n_row - number of rows in A I n_col - number of columns in A I nnz - number of nonzeros in A I Ai[nnz(A)] - row indices I Aj[nnz(A)] - column indices T Ax[nnz(A)] - nonzeros Output Arguments: I Bp - row pointer I Bj - column indices T Bx - nonzeros Note: Output arrays Bp, Bj, and Bx must be preallocated Note: Input: row and column indices *are not* assumed to be ordered Note: Duplicate entries are carried over to the CRS represention Complexity: Linear. Specifically O(nnz(A) + max(n_row,n_col)) Note: Adapted from scipy.sparse. """ nnz = len(Ax) Bp = cumsumdups(Ai, n_row) Bj = [0] * nnz Bk = [0] * nnz Bx = [0] * nnz for n in range(nnz): row = Ai[n] dest = Bp[row] Bj[dest] = Aj[n] Bk[dest] = Ak[n] Bx[dest] = Ax[n] Bp[row] += 1 Bp = [0] + [Bp[i] for i in range(n_row-1)] return Bp, Bj, Bk, Bx