CodePlexProject Hosting for Open Source Software

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.

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 } }

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 IridiumCombinatorics for more samples and details on Combinatorics.

For example, you want to compute the eigenvalues and -vectors for the following matrix:

10.0 | -18.0 |

6.0 | -11.0 |

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:

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.

Last edited Jul 29, 2012 at 3:59 PM by cdrnet, version 1