All linear algebra functionality for Math.NET can be found under the MathNet.Numerics.LinearAlgebra
namespace. This namespace is divided up into four different subnamespaces
- MathNet.Numerics.LinearAlgebra.Double: for doing real linear algebra using double floating point precision.
- MathNet.Numerics.LinearAlgebra.Single: for doing real linear algebra using single floating point precision.
- MathNet.Numerics.LinearAlgebra.Complex: for doing complex linear algebra using double floating point precision.
- MathNet.Numerics.LinearAlgebra.Complex32: for doing complex linear algebra using single floating point precision.
The reason we make this distinction is that each of the different formats has different memory requirements. A real matrix in single floating point precision will be the smallest. The same matrix represented as a complex double floating point matrix will require
roughly four times as much memory. The downside of real single floating point precision is less accuracy and no ability to do decompositions that involve complex quantities.
Another distinction we make in the linear algebra library is in different matrix storage representations. Currently, Math.Net supports three different matrix storage representations (for each of the four different types mentionned above):
- DenseMatrix: for representing arbitrary matrices.
- SparseMatrix: for representing sparse matrices.
- DiagonalMatrix: for representing diagonal matrices.
Again, each different matrix storage representation optimizes for a particular usage pattern. Sparse matrices are represented in 3-array
(CSR) format. Diagonal matrices are represented as vectors. Math.NET supports both dense and sparse vectors.
The final feature of the linear algebra library is which computational engine executes the calculations. The default configuration for Math.NET uses all managed code to execute matrix operations. Since managed code executes on the x87 unit, it cannot use any
of the special vectorized instructions to speed up calculations. For very large matrices this will have an impact on performance compared to native implementations. We suggest you vote on
to encourage the CLR team to implement this feature in the future. Nonetheless, Math.NET also supports native providers for executing linear algebra computations. To configure these providers, use the following code
// Use the ATLAS native linear algebra provider.
MathNet.Numerics.Control.LinearAlgebraProvider = new AtlasLinearAlgebraProvider();
Currently, Math.NET supports Atlas and Intel MKL providers. We encourage the community to contribute other providers (such as GPU backed computations, e.g. using Nvidia CUDA).
Decompositions & Least Squares Problems
A short sample for creating a matrix and a vector and multiplying them together can be found below.
// Create a vector of dimension 5 with 4.0 in every entry.
var x = new DenseVector(5, 4.0);
// Create a 3 by 5 matrix with 2.0 in every entry.
var A = new DenseMatrix(3, 5, 2.0);
// Multiply the matrix and the vector.
var y = A * x;