Since Math.NET is all about programming libraries and toolkits, screenshots wouldn't help you much to get an idea on how it works. But some small code samples can, so we provide some on this page.
In order to run any of these samples you need to add a reference to the MathNet.Iridium.dll library to your project, or the compiler/linker won't find our classes and namespaces.
This page is a Draft, its content is not complete and might contain errors.
Special Functions
The .Net Framework already provides some special functions like
Exp,
Log and
Sin in the
System.Math class. However, if you're doning something more involved, say some statistics work, sooner or later you'll need to evaluate some more sopisticated functions, like the
error-function. Luckily, Iridium provides this function:
using MathNet.Numerics;
class Test
{
static void Main(string[] args)
{
double x = 0.9;
double res = Fn.Erf(x);
// res will now have the value 0.7969082124
}
}
This
Fn class also provides several other useful special functions, like the Gamma/Beta functions, the binomial function or simply a factorial:
using MathNet.Numerics;
class Test
{
static void Main(string[] args)
{
double x = Fn.Factorial(14);
// x will now have the value 87178291200.0
double y = Fn.Factorial(31);
// y will now have the value 8.2228386541779224E+33
}
}
Combinatorics
Say you have a list of numbers and would like to shuffle them so that the items are in random order. How many such orderings (permutations) are possible? The
Combinatorics class provides methods for both counting combinations and variations and to generate them randomly:
using MathNet.Numerics;
class Test
{
static void Main(string[] args)
{
int[] numbers = new int[] { 1, 2, 3, 4, 5 };
int count = numbers.Length; // = 5
double numberOfPermutations = Combinatorics.Permutations(count);
// Will be 120 since there are 120 possible orderings for 5 numbers
int[] permutation = new int[count];
Combinatorics.RandomShuffle(numbers, permutation);
// 'permutations' now contains the same numbers as 'numbers',
// but in a random order.
}
}
See
Combinatorics for more samples and details on Combinatorics.
Linear Algebra
Everything around linear algebra (matrices, vectors, linear equation systems etc.) is in the
MathNet.Numerics.LinearAlgebra namespace.
For example, you want to compute the eigenvalues and -vectors for the following matrix:
First you have to create the matrix. The sample code below shows one way to do that. Then you ask for the eigenvalue decomposition which provides properties for both eigenvalues and eigenvectors:
using MathNet.Numerics;
using MathNet.Numerics.LinearAlgebra;
class Test
{
static void Main(string[] args)
{
Matrix m = new Matrix(new double[][] {
new double[] { 10.0, -18.0 },
new double[] { 6.0, -11.0 }});
// alternative way to create the matrix:
// double[][] data = Matrix.CreateMatrixData(2, 2);
// data[0][0] = 10.0;
// data[1][0] = 6.0;
// data[0][1] = -18.0;
// data[1][1] = -11.0;
// Matrix m = new Matrix(data);
EigenvalueDecomposition eigen = m.EigenvalueDecomposition;
Complex[] eigenValues = eigen.EigenValues;
// eigenvalues: 1, -2
Matrix eigenVectors = eigen.EigenVectors;
// eigenvectors: [0.894...,0.447...] and [6.708...,4.473...]
// alternative way to access the eigenvalues witout the Complex type:
// double[] eigenValuesReal = eigen.RealEigenvalues; // real part
// double[] eigenValuesImag = eigen.ImagEigenvalues; // imaginary part
}
}
Matrices of course also support all the basic operations. For arithmectis operations like the addition or multiplication of two matrices you can even use the standard operations "+" and "*". To solve linear equation systems, have a look at the
Solve method.