VOOZH about

URL: https://www.geeksforgeeks.org/python/scipy-sparse-matrix-multiplication/

⇱ SciPy - Sparse Matrix Multiplication - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

SciPy - Sparse Matrix Multiplication

Last Updated : 5 Jul, 2025

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.

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.

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.

Comment
Article Tags:
Article Tags: