Here is a simple Python solution for Dense Matrix X CSR Matrix . It should be clear.
def main(): # 4 x 4 csr matrix # [1, 0, 0, 0], # [2, 0, 3, 0], # [0, 0, 0, 0], # [0, 4, 0, 0], csr_values = [1, 2, 3, 4] col_idx = [0, 0, 2, 1] row_ptr = [0, 1, 3, 3, 4] csr_matrix = [ csr_values, col_idx, row_ptr ] dense_matrix = [ [1, 3, 3, 4], [1, 2, 3, 4], [1, 4, 3, 4], [1, 2, 3, 5], ] res = [ [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], ] # matrix order, assumes both matrices are square n = len(dense_matrix) # res = dense X csr csr_row = 0 # Current row in CSR matrix for i in range(n): start, end = row_ptr[i], row_ptr[i + 1] for j in range(start, end): col, csr_value = col_idx[j], csr_values[j] for k in range(n): dense_value = dense_matrix[k][csr_row] res[k][col] += csr_value * dense_value csr_row += 1 print res if __name__ == '__main__': main()
Is CSR Matrix X Dense Matrix really just a CSR Matrix X Vector product sequence for each row of a dense matrix on the right? Therefore, for this it is enough to simply extend the code shown above to do this.
Moving forward, I suggest you not to code these routines yourself. If you use C ++ (tag based), you can look at Boost ublas , or Eigen . At first, the APIs may seem a little cryptic, but it really stands in the long run. First, you get access to much more functionality that you are likely to require in the future. Secondly, these implementations will be better optimized.