Linear Algebra

Your Progress in this Chapter

0%

0 / 38 units completed

Chapter Overview

Sparse Matrices

Section 5.9 of 1130 min13 code examples

A sparse matrix is a type of matrix in which most of the elements are zero. This contrasts with a dense matrix, where most elements are non-zero. Sparse matrices are common in scientific computing, data science, and machine learning because many real-world problems naturally produce matrices with lots of zeros.

Key Characteristics

  • Definition: A matrix with significantly more zero elements than non-zero ones.
  • Sparsity: The proportion of zero elements in the matrix.
  • Density: The proportion of non-zero elements. For example, a matrix with 74% zeros has 26% density.

Why Sparse Matrices Matter

  • Memory Efficiency: Storing only non-zero elements saves space.
  • Computational Speed: Operations can skip zeros, reducing processing time.

Applications

  • Graph algorithms (adjacency matrices often sparse).
  • Machine learning (e.g., text data represented as word-frequency matrices).
  • Finite element analysis in engineering.

Common Representations

Instead of storing all elements, sparse matrices are represented using specialized data structures:

RepresentationDescriptionExample Use Case
Coordinate List (COO)Stores row, column, and value of non-zero entries.Quick construction of sparse matrices
Compressed Sparse Row (CSR)Stores non-zero values with row pointers.Efficient row slicing and matrix-vector multiplication.
Compressed Sparse Column (CSC)Similar to CSR but column-based.Useful for column operations.
Linked List RepresentationEach non-zero element stored as a node with row/column indices.Flexible but less efficient.

SepalSolver represents sparse matrices using a Dictionary:

Example 1C#

1
Code is ready to run

-Keys: A tuple (i, j) representing the row and column indices.

-Values: The non-zero entry at that position.

This dictionary-based approach makes construction and updates simple,while providing fast element lookups.

Dynamic Conversion

Although the dictionary form is flexible, SepalSolver transforms the matrix into specialized formats during computation for efficiency:

  • CSR (Compressed Sparse Row):
  • Used when traversing rows.
  • Ideal for matrix-vector multiplication and row slicing.
  • Example: In multiplication A * B, matrix A is converted to CSR.
  • CSC (Compressed Sparse Column):
  • Used when traversing columns.
  • Ideal for column slicing and dot products.
  • Example: In multiplication A * B, matrix B is converted to CSC.

Hybrid Approach

This design combines the strengths of both representations: Flexibility: Dictionary form is intuitive for construction and updates. Performance: CSR and CSC conversions ensure efficient heavy operations. * Consistency: Results are returned in dictionary form, keeping the API uniform.

Example Workflow

Matrix multiplication C = A * B proceeds as follows: 1. A stored as dictionary → converted to CSR. 2. B stored as dictionary → converted to CSC. 3. Multiplication performed by traversing rows of A (CSR) and columns of B (CSC). 4. Result C stored back as dictionary Dictionary<(int, int), double>.

SepalSolver’s sparse matrix implementation achieves a balance betwee ease of use and computational efficiency. By starting with a dictionary and dynamically converting to CSR or CSC when needed, it provides both developer-friendly construction and high-performance operations.

Making a ``SparseMatrix``

A SparseMatrix can be made in the following ways: 1. Converting an existing dense matrix into sparse matrix. 2. From Arrays of row indices, column indices and values. 3. For a small matrix, you can declare and empty sparse matrix using the number of rows and columns, and then asigne each element into the matrix.

Matrix to SpraseMatrix

Example 2C#

1
2
3
4
5
6
7
8
9
10
11
12
Code is ready to run
OutputFrom the book
Total elements = 16
Non-zero elements  = 3
Sparsity = 0.1875

Rows, Columns and Values

Example 3C#

1
2
3
4
5
Code is ready to run
OutputFrom the book
Total elements = 16
Non-zero elements  = 3
Sparsity = 0.1875

Assigning Values

Example 4C#

1
2
3
4
5
6
7
Code is ready to run
OutputFrom the book
Total elements = 16
Non-zero elements  = 3
Sparsity = 0.1875

SepalSolver also has inbuilt Sparsematrices that can be loaded without manually creating them as started above. examples of these are : Squid and Bucky

Visualisation

SparseMatrices sparsity partterns can be visualized using Spy in the Plotlibrary.

Example 5C#

1
2
3
Code is ready to run
OutputFrom the book
Squid-Pattern.png

Arithmetic Operation

All Operations supported by the Matrix class is also supported by the SparseMatrix Class. In addition to the standard matrix operation, sparse matrices can be reordered. Reordering is done to reduce fillin during matrix factorization.

LU, iLU, Cholesky and iCholesky Factorization

Just like Matrix class, LU, iLU, Cholesky and iCholesky factorization can be performed using MakeLU(), MakeiLU(), MakeChol(), MakeiChol() rspectively.

Here we look at the incomplete LU and Cholesky, since the complete form as been dealt with in dense matrices and that model carries over into sparse matrices.

Example 6C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Code is ready to run
OutputFrom the book
L = 
   1.0000    0.0000    0.0000    0.0000    0.0000
  -0.4000    1.0000    0.0000    0.0000    0.0000
   0.0000   -0.4000    1.0000    0.0000    0.0000
  -0.4000    0.0000   -0.4000    1.0000    0.0000
  -0.4000    0.0000    0.0000   -0.4000    1.0000

U = 
 5   0   0   0   0 
 0   5   0   0   0 
 0   0   5   0   0 
 0   0   0   5   0 
 0   0   0   0   5 

L * U = 
 5   0   0   0   0 
-2   5   0   0   0 
 0  -2   5   0   0 
-2   0  -2   5   0 
-2   0   0  -2   5 
L_from_Incomplete_LU_Factorization_of_B.png
U_from_Incomplete_LU_Factorization_of _B.png

Example 7C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Code is ready to run
OutputFrom the book
L = 
 (0,0)            2.2361
 (1,0)           -0.8944
 (3,0)           -0.8944
 (4,0)           -0.8944
 (1,1)            2.0494
 (2,1)           -0.9759
 (2,2)            2.0119
 (3,2)           -0.9941
 (3,3)            1.7921
 (4,3)           -1.5624
 (4,4)            1.3263

L*LT = 
 (0,0)            5.0000
 (1,0)           -2.0000
 (3,0)           -2.0000
 (4,0)           -2.0000
 (0,1)           -2.0000
 (1,1)            5.0000
 (2,1)           -2.0000
 (3,1)            0.8000
 (4,1)            0.8000
 (1,2)           -2.0000
 (2,2)            5.0000
 (3,2)           -2.0000
 (0,3)           -2.0000
 (1,3)            0.8000
 (2,3)           -2.0000
 (3,3)            5.0000
 (4,3)           -2.0000
 (0,4)           -2.0000
 (1,4)            0.8000
 (3,4)           -2.0000
 (4,4)            5.0000
L_from_Incomplete_Cholesky_Factorization_of_B.png

Example 8C#

1
2
3
4
5
6
7
8
9
10
11
12
Code is ready to run
OutputFrom the book
(0,0)            4.7681
(1,0)            0.3955
(4,0)            0.2726
(1,1)            4.6987
(2,1)            0.4355
(4,1)           -0.0230
(2,2)            4.7507
(3,2)            0.5179
(4,2)            0.0021
(3,3)            4.7240
(4,3)            0.4817
(4,4)            4.7094

Reodering

Matrix rearrangement (or reordering) aims to find a permutation matrix P such that the factorization of PAP^T minimizes fill-in.

Reverse Cuthill-McKee(RCM) Reduces the bandwidthof the matrix by clustering non-zeros near the diagonal.Ideal for simpler, structured systems.

Minimum Degree(MD) A greedy approach that eliminates the vertex with the lowest degree first.This is a local optimization strategy.

Nested Dissection(ND) A "divide and conquer" approach using graph separators.

..image::https://upload.wikimedia.org/wikipedia/commons/thumb/e/e5/Sparse_matrix_fill-in.svg/400px-Sparse_matrix_fill-in.svg.png :alt: Diagram showing fill-in during factorization :align: center

StrategyLogicProsCons
RCMBandwidth ReductionFast; simple memory accessHigh total fill-in risk
Minimum DegreeLocal GreedyGreat for general matricesSlow on massive systems
Nested Diss.Divide & ConquerBest for 3D grids/parallelismComplex implementation

..note::

The fill-in is governed by the elimination tree of the matrix.A "bushy" tree allows for more parallel factorization.

Example 9C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Code is ready to run
OutputFrom the book
RCM_reordering_of_Squid.png

Example 10C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Code is ready to run
OutputFrom the book
AMD_reordering_of_Squid.png

Example 11C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Code is ready to run

Example 12C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Code is ready to run
OutputFrom the book
AMD_reordering_of_Bucky.png

Example 13C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Code is ready to run
OutputFrom the book
RCM_reordering_of_Bucky.png