Sparse matrices are required fore image processing and AI applications. You can use them to scale, rotate, or translate other matrices or even do a combination of all those operations. You can also precompute sparse matrices in order to pre-define fix operations and execute them very efficiently.
Sparse matrices are matrices, which contain only a small number of allocated elements, while the majority of elements has the value zero. An easy example is the diagonal matrix used for scale operations, where only the elements in the diagonal cells (index of row and column are equal) contain the scaling value for a single dimension.
There is no intrinsic data type or class for sparse matrices in .NET. Standard matrices are no real alternative, as they become non-neglectable memory intensive. If you donβt want to implement your own class for sparse matrices, you can either use free or commercial mathematical NuGet packages. I will show how to use the MathNet package for this use case!
Using this package a sparse matrix class SparseMatrix is contained in MathNet.Numerics.LinearAlgebra.Double for floating-point values, MathNet.Numerics.LinearAlgebra.Complex for complex values, etc. You can instantiate this class using the number of rows and number of columns. The following example creates two diagonal matrices with 1000 x 1000 double elements (8 bytes) with values 1 and 2 on the diagonals, and multiplies those matrices.
using MathNet.Numerics.LinearAlgebra.Double;
var matrixA = SparseMatrix.CreateDiagonal(1000, 1000, 1);
var matrixB = SparseMatrix.CreateDiagonal(1000, 1000, 2);
var matrixC = matrixA.Multiply(matrixB);
Console.WriteLine(matrixC);
You would expect the matrix objects to allocate at least 8 MB heap space, when its elements are represented by floating-point values with double precision (1000 x 1000 x 8 Byte). However the class SparseMatrix uses internally the class SparseCompressedRowMatrixStorage to store only the occupied elements, and the Visual Studio profiler will show you the objects allocate only 24 kB on the .NET Small Object Heap.
So instead of just defining a matrix, think first how it will be populated, and if itβs a sparse matrix use a suitable data representation for memory and computation efficiency!
Happy coding π! If you like my post follow me on LinkedIn π!