Open In App

SciPy - Sparse Matrix Multiplication

Last Updated : 05 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Python
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

twoCSCmultiply_output
Output of Multiplication of Two CSC matrices

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.

Python
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

twoCSRmultiply_output
Output of Multiplication of Two CSR matrices

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.

Python
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

CSRnCSCmultiply_output
Output of CSR and CSC Multiplication

Related Articles:


Article Tags :
Practice Tags :

Similar Reads