In scientific computing, large matrices often contain mostly zero values. Storing and multiplying these as dense arrays wastes both memory and processing time. SciPy’s scipy.sparse module efficiently handles sparse matrices(2D arrays with mostly zero values) designed specifically for fast storage and computation.
Two commonly used classes in this module are:
- csr_matrix: compressed Sparse Row matrix
- csc_matrix: compressed Sparse Column matrix
To multiply two sparse matrices, .multiply() method is used both in csr_matrix and in csc_matrix. This method supports multiplication between matrices of same format (e.g., CSR × CSR) or even different formats (e.g., CSR × CSC).
Let's understand it better through Example.
Example 1: Multiply Two csc_matrix Matrices
In this example two sparse matrices are created using csc_matrix() class. These matrices are then multiplied element-wise using multiply() method which performs efficient operations on only the non-zero elements.
import numpy as np
from scipy.sparse import csc_matrix
# Create first csc matrix A
row_A = np.array([0, 0, 1, 2])
col_A = np.array([0, 1, 0, 1])
data_A = np.array([4, 3, 8, 9])
csc_A = csc_matrix((data_A, (row_A, col_A)), shape=(3, 3))
print("First CSC matrix:\n", csc_A.toarray())
# Create second csc matrix B
row_B = np.array([0, 1, 1, 2])
col_B = np.array([0, 0, 1, 0])
data_B = np.array([7, 2, 5, 1])
csc_B = csc_matrix((data_B, (row_B, col_B)), shape=(3, 3))
print("Second CSC matrix:\n", csc_B.toarray())
# Element-wise multiplication
result = csc_A.multiply(csc_B)
print("Element-wise Product (CSC x CSC):\n", result.toarray())
Output

Example 2: Multiply Two csr_matrix Matrices
Here, two sparse matrices are created using csr_matrix() class. These matrices are then multiplied element-wise using the multiply() method focusing only on non-zero elements.
import numpy as np
from scipy.sparse import csr_matrix
# Create first csr matrix A
row_A = np.array([0, 0, 1, 2])
col_A = np.array([0, 1, 0, 1])
data_A = np.array([4, 3, 8, 9])
csr_A = csr_matrix((data_A, (row_A, col_A)), shape=(3, 3))
print("First CSR matrix:\n", csr_A.toarray())
# Create second csr matrix B
row_B = np.array([0, 1, 1, 2])
col_B = np.array([0, 0, 1, 0])
data_B = np.array([7, 2, 5, 1])
csr_B = csr_matrix((data_B, (row_B, col_B)), shape=(3, 3))
print("Second CSR matrix:\n", csr_B.toarray())
# Element-wise multiplication
result = csr_A.multiply(csr_B)
print("Element-wise Product (CSR x CSR):\n", result.toarray())
Output

Example 3: Multiply csc_matrix and csr_matrix
In this example two sparse matrices are created, one in CSC format and other in CSR format. Then element-wise multiplication is performed using multiply() method to demonstrate compatibility between different sparse matrix.
import numpy as np
from scipy.sparse import csc_matrix, csr_matrix
# Create CSC matrix
row_A = np.array([0, 0, 1, 2])
col_A = np.array([0, 1, 0, 1])
data_A = np.array([4, 3, 8, 9])
csc_A = csc_matrix((data_A, (row_A, col_A)), shape=(3, 3))
print("CSC matrix:\n", csc_A.toarray())
# Create CSR matrix
row_B = np.array([0, 1, 1, 2])
col_B = np.array([0, 0, 1, 0])
data_B = np.array([7, 2, 5, 1])
csr_B = csr_matrix((data_B, (row_B, col_B)), shape=(3, 3))
print("CSR matrix:\n", csr_B.toarray())
# Multiply CSC with CSR
result1 = csc_A.multiply(csr_B)
print("Element-wise Product (CSC x CSR):\n", result1.toarray())
# Multiply CSR with CSC
result2 = csr_B.multiply(csc_A)
print("Element-wise Product (CSR x CSC):\n", result2.toarray())
Output

Related Articles: