Math.NET Numerics
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

368 lines
21 KiB

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>Math.NET Numerics
</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta name="description" content="Math.NET Numerics, providing methods and algorithms for numerical computations in science, engineering and every day use. .Net 4, .Net 3.5, SL5, Win8, WP8, PCL 47 and 136, Mono, Xamarin Android/iOS."/>
<meta name="author" content="Christoph Ruegg, Marcus Cuda, Jurgen Van Gael"/>
<script src="https://code.jquery.com/jquery-1.8.0.js"></script>
<script src="https://code.jquery.com/ui/1.8.23/jquery-ui.js"></script>
<script src="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.1/js/bootstrap.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.1/css/bootstrap-combined.min.css" rel="stylesheet"/>
<link type="text/css" rel="stylesheet" href="https://numerics.mathdotnet.com/content/style.css" />
<style>
#main table:not(.pre) {
border: 1px solid #dddddd;
max-width: 100%;
border-style: solid;
border-width: 1px;
border-color: gray;
border-collapse: collapse;
border-right-width: 1px;
border-bottom-width: 1px;
margin-top: 15px;
margin-bottom: 25px;
}
#main table:not(.pre) th, #main table:not(.pre) td {
border: 1px solid #dddddd;
padding: 6px;
}
#main table:not(.pre) th p, #main table:not(.pre) td p {
margin-bottom: 5px;
}
</style>
<script type="text/javascript" src="https://numerics.mathdotnet.com/content/tips.js"></script>
<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<![endif]-->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
</head>
<body>
<div class="container">
<div class="masthead">
<ul class="nav nav-pills pull-right">
<li><a href="https://www.mathdotnet.com">Math.NET Project</a></li>
<li><a href="https://numerics.mathdotnet.com">Math.NET Numerics</a></li>
<li><a href="https://github.com/mathnet/mathnet-numerics">GitHub</a></li>
</ul>
<h3 class="muted">Math.NET Numerics</h3>
</div>
<hr />
<div class="row">
<div class="span9" id="main">
<h1><a name="Math-NET-Numerics" class="anchor" href="#Math-NET-Numerics">Math.NET Numerics</a></h1>
<p>Math.NET Numerics aims to provide methods and algorithms for numerical computations
in science, engineering and every day use. Covered topics include special functions,
linear algebra, probability models, random numbers, interpolation, integration,
regression, optimization problems and more.</p>
<p>Math.NET Numerics is part of the <a href="https://www.mathdotnet.com/">Math.NET initiative</a>
and is the result of merging dnAnalytics with Math.NET Iridium, replacing both.
Available for free under the <a href="License.html">MIT/X11 License</a>.
It targets Microsoft .Net 4.0 and higher, including Mono, and .Net Standard 1.3
and higher (with builds for .Net Standard 2.0). In addition to a purely
managed implementation it also supports native hardware optimization.
See <a href="Compatibility.html">Platform Support</a> for full details.</p>
<h2><a name="NuGet-Packages" class="anchor" href="#NuGet-Packages">NuGet Packages</a></h2>
<ul>
<li><a href="https://www.nuget.org/packages/MathNet.Numerics/"><strong>MathNet.Numerics</strong></a> - core package</li>
<li><a href="https://www.nuget.org/packages/MathNet.Numerics.FSharp/"><strong>MathNet.Numerics.FSharp</strong></a> - optional extensions for a better experience when using F#.</li>
</ul>
<p>See <a href="Packages.html">NuGet &amp; Binaries</a> for a complete list of our NuGet packages,
Zip files and the release archive.</p>
<h2><a name="Using-Math-NET-Numerics-with-C" class="anchor" href="#Using-Math-NET-Numerics-with-C">Using Math.NET Numerics with C#</a></h2>
<p>Being written in it, Math.NET Numerics works very well with C# and related .Net languages.
When using Visual Studio or another IDE with built-in NuGet support, you can get started
quickly by adding a reference to the <code>MathNet.Numerics</code> NuGet package. Alternatively you can grab
that package with the command line tool with <code>nuget.exe install MathNet.Numerics -Pre</code>
or simply download the Zip package.</p>
<p>let's say we have a matrix <span class="math">\(\mathrm{A}\)</span> and want to find an orthonormal basis of the kernel or null-space
of that matrix, such that <span class="math">\(\mathrm{A}x = 0\)</span> for all <span class="math">\(x\)</span> in that subspace.</p>
<table class="pre"><tr><td class="lines"><pre class="fssnip"><span class="l"> 1: </span>
<span class="l"> 2: </span>
<span class="l"> 3: </span>
<span class="l"> 4: </span>
<span class="l"> 5: </span>
<span class="l"> 6: </span>
<span class="l"> 7: </span>
<span class="l"> 8: </span>
<span class="l"> 9: </span>
<span class="l">10: </span>
<span class="l">11: </span>
</pre></td>
<td class="snippet"><pre class="fssnip highlighted"><code lang="csharp"><span class="k">using</span> MathNet.Numerics.LinearAlgebra;
<span class="k">using</span> MathNet.Numerics.LinearAlgebra.Double;
Matrix&lt;<span class="k">double</span>&gt; A <span class="o">=</span> DenseMatrix.OfArray(<span class="k">new</span> <span class="k">double</span>[,] {
{<span class="n">1</span>,<span class="n">1</span>,<span class="n">1</span>,<span class="n">1</span>},
{<span class="n">1</span>,<span class="n">2</span>,<span class="n">3</span>,<span class="n">4</span>},
{<span class="n">4</span>,<span class="n">3</span>,<span class="n">2</span>,<span class="n">1</span>}});
Vector&lt;<span class="k">double</span>&gt;[] nullspace <span class="o">=</span> A.Kernel();
<span class="c">// verify: the following should be approximately (0,0,0)</span>
(A <span class="o">*</span> (<span class="n">2</span>*nullspace[<span class="n">0</span>] <span class="o">-</span> <span class="n">3</span>*nullspace[<span class="n">1</span>]))
</code></pre></td></tr></table>
<h2><a name="F-and-F-Interactive" class="anchor" href="#F-and-F-Interactive">F# and F# Interactive</a></h2>
<p>Even though the core of Math.NET Numerics is written in C#, it aims to support F#
just as well. In order to achieve this we recommend to reference the <code>MathNet.Numerics.FSharp</code>
package in addition to <code>MathNet.Numerics</code>, which adds a few modules to make it more
idiomatic and includes arbitrary precision types (BigInteger, BigRational).</p>
<table class="pre"><tr><td class="lines"><pre class="fssnip"><span class="l">1: </span>
<span class="l">2: </span>
<span class="l">3: </span>
<span class="l">4: </span>
</pre></td>
<td class="snippet"><pre class="fssnip highlighted"><code lang="fsharp"><span class="k">open</span> <span class="i">MathNet</span><span class="o">.</span><span class="i">Numerics</span><span class="o">.</span><span class="i">LinearAlgebra</span>
<span class="k">let</span> <span class="i">m</span> <span class="o">=</span> <span class="i">matrix</span> [[ <span class="n">1.0</span>; <span class="n">2.0</span> ]
[ <span class="n">3.0</span>; <span class="n">4.0</span> ]]
<span class="k">let</span> <span class="i">m&#39;</span> <span class="o">=</span> <span class="i">m</span><span class="o">.</span><span class="i">Inverse</span>()
</code></pre></td>
</tr>
</table>
<p>It also works well in the interactive F# environment (REPL) which can be launched with
<code>fsharpi</code> on all platforms (including Linux). As a start let's enter the following lines
into F# interactive. Append <code>;;</code> to the end of a line to run all code up to there
immediately and print the result to the output. Use the tab key for auto-completion or <code>#help;;</code> for help.
For convenience our F# packages include a small script that sets everything up properly:</p>
<table class="pre"><tr><td class="lines"><pre class="fssnip"><span class="l">1: </span>
<span class="l">2: </span>
<span class="l">3: </span>
<span class="l">4: </span>
<span class="l">5: </span>
<span class="l">6: </span>
<span class="l">7: </span>
<span class="l">8: </span>
</pre></td>
<td class="snippet"><pre class="fssnip highlighted"><code lang="fsharp"><span class="prep">#load</span> <span class="s">&quot;../packages/MathNet.Numerics.FSharp/MathNet.Numerics.fsx&quot;</span>
<span class="k">open</span> <span class="i">MathNet</span><span class="o">.</span><span class="i">Numerics</span>
<span class="i">SpecialFunctions</span><span class="o">.</span><span class="i">Gamma</span>(<span class="n">0.5</span>)
<span class="k">open</span> <span class="i">MathNet</span><span class="o">.</span><span class="i">Numerics</span><span class="o">.</span><span class="i">LinearAlgebra</span>
<span class="k">let</span> <span class="i">m</span> <span class="o">:</span> <span class="i">Matrix</span><span class="o">&lt;</span><span class="i">float</span><span class="o">&gt;</span> <span class="o">=</span> <span class="i">DenseMatrix</span><span class="o">.</span><span class="i">randomStandard</span> <span class="n">50</span> <span class="n">50</span>
(<span class="i">m</span> <span class="o">*</span> <span class="i">m</span><span class="o">.</span><span class="i">Transpose</span>())<span class="o">.</span><span class="i">Determinant</span>()
</code></pre></td>
</tr>
</table>
<h2><a name="Visual-Basic" class="anchor" href="#Visual-Basic">Visual Basic</a></h2>
<p>Let's use Visual Basic to find the polynomial roots <span class="math">\(x\)</span> such that <span class="math">\(2x^2 - 2x - 2 = 0\)</span>
numerically. We already know there are two roots, one between -2 and 0, the other between 0 and 2:</p>
<table class="pre"><tr><td class="lines"><pre class="fssnip"><span class="l">1: </span>
<span class="l">2: </span>
<span class="l">3: </span>
<span class="l">4: </span>
<span class="l">5: </span>
<span class="l">6: </span>
<span class="l">7: </span>
<span class="l">8: </span>
<span class="l">9: </span>
</pre></td>
<td class="snippet"><pre class="fssnip"><code lang="visualbasic">Imports MathNet.Numerics.RootFinding
Dim f As Func(Of Double, Double) = Function(x) 2*x^2 - 2*x - 2
Bisection.FindRoot(f, 0, 2) ' returns 1.61803398874989
Bisection.FindRoot(f, -2, 0) ' returns -0.618033988749895
' Alternative to directly compute the roots for this special case:
FindRoots.Quadratic(-2, -2, 2)
</code></pre></td></tr></table>
<h2><a name="Linux-with-Mono" class="anchor" href="#Linux-with-Mono">Linux with Mono</a></h2>
<p>You need a recent version of Mono in order to use Math.NET Numerics on anything other than Windows.
Luckily there has been great progress lately to make both Mono and F# available as proper Debian packages.
In Debian <em>testing</em> and Ubuntu <em>14.04 (trusty/universe)</em> you can install both of them with APT:</p>
<table class="pre"><tr><td class="lines"><pre class="fssnip"><span class="l">1: </span>
<span class="l">2: </span>
<span class="l">3: </span>
</pre></td>
<td class="snippet"><pre class="fssnip"><code lang="sh">sudo apt-get update
sudo apt-get install mono-complete
sudo apt-get install fsharp
</code></pre></td></tr></table>
<p>If you don't have NuGet yet:</p>
<table class="pre"><tr><td class="lines"><pre class="fssnip"><span class="l">1: </span>
<span class="l">2: </span>
</pre></td>
<td class="snippet"><pre class="fssnip"><code lang="sh">sudo mozroots --import --sync
curl -L https://nuget.org/nuget.exe -o nuget.exe
</code></pre></td></tr></table>
<p>Then you can use NuGet to fetch the latest binaries in your working directory.
The <code>-Pre</code> argument causes it to include pre-releases, omit it if you want stable releases only.</p>
<table class="pre"><tr><td class="lines"><pre class="fssnip"><span class="l">1: </span>
<span class="l">2: </span>
<span class="l">3: </span>
</pre></td>
<td class="snippet"><pre class="fssnip"><code lang="sh">mono nuget.exe install MathNet.Numerics -Pre -OutputDirectory packages
# or if you intend to use F#:
mono nuget.exe install MathNet.Numerics.FSharp -Pre -OutputDirectory packages
</code></pre></td></tr></table>
<p>In practice you'd probably use the Monodevelop IDE instead which can take care of fetching and updating
NuGet packages and maintain assembly references. But for completeness let's use the compiler directly this time.
Let's create a C# file <code>Start.cs</code>:</p>
<table class="pre"><tr><td class="lines"><pre class="fssnip"><span class="l"> 1: </span>
<span class="l"> 2: </span>
<span class="l"> 3: </span>
<span class="l"> 4: </span>
<span class="l"> 5: </span>
<span class="l"> 6: </span>
<span class="l"> 7: </span>
<span class="l"> 8: </span>
<span class="l"> 9: </span>
<span class="l">10: </span>
<span class="l">11: </span>
<span class="l">12: </span>
<span class="l">13: </span>
<span class="l">14: </span>
<span class="l">15: </span>
<span class="l">16: </span>
<span class="l">17: </span>
<span class="l">18: </span>
</pre></td>
<td class="snippet"><pre class="fssnip highlighted"><code lang="csharp"><span class="k">using</span> System;
<span class="k">using</span> MathNet.Numerics;
<span class="k">using</span> MathNet.Numerics.LinearAlgebra;
<span class="k">class</span> Program
{
<span class="k">static</span> <span class="k">void</span> Main(<span class="k">string</span>[] args)
{
<span class="c">// Evaluate a special function</span>
Console.WriteLine(SpecialFunctions.Erf(<span class="n">0.5</span>));
<span class="c">// Solve a random linear equation system with 500 unknowns</span>
<span class="k">var</span> m <span class="o">=</span> Matrix&lt;<span class="k">double</span>&gt;.Build.Random(<span class="n">500</span>, <span class="n">500</span>);
<span class="k">var</span> v <span class="o">=</span> Vector&lt;<span class="k">double</span>&gt;.Build.Random(<span class="n">500</span>);
<span class="k">var</span> y <span class="o">=</span> m.Solve(v);
Console.WriteLine(y);
}
}
</code></pre></td></tr></table>
<p>Compile and run:</p>
<table class="pre"><tr><td class="lines"><pre class="fssnip"><span class="l">1: </span>
<span class="l">2: </span>
<span class="l">3: </span>
<span class="l">4: </span>
<span class="l">5: </span>
</pre></td>
<td class="snippet"><pre class="fssnip"><code lang="sh"># single line:
mcs -optimize -lib:packages/MathNet.Numerics.3.0.0-alpha8/lib/net40/
-r:MathNet.Numerics.dll Start.cs -out:Start
# launch:
mono Start
</code></pre></td></tr></table>
<p>Which will print something like the following to the output:</p>
<table class="pre"><tr><td class="lines"><pre class="fssnip"><span class="l"> 1: </span>
<span class="l"> 2: </span>
<span class="l"> 3: </span>
<span class="l"> 4: </span>
<span class="l"> 5: </span>
<span class="l"> 6: </span>
<span class="l"> 7: </span>
<span class="l"> 8: </span>
<span class="l"> 9: </span>
<span class="l">10: </span>
</pre></td>
<td class="snippet"><pre class="fssnip"><code lang="text">0.520499877813047
DenseVector 500-Double
-0.181414 -1.25024 -0.607136 1.12975 -3.31201 0.344146
0.934095 -2.96364 1.84499 1.20752 0.753055 1.56942
0.472414 6.10418 -0.359401 0.613927 -0.140105 2.6079
0.163564 -3.04402 -0.350791 2.37228 -1.65218 -0.84056
1.51311 -2.17326 -0.220243 -0.0368934 -0.970052 0.580543
0.755483 -1.01755 -0.904162 -1.21824 -2.24888 1.42923
-0.971345 -3.16723 -0.822723 1.85148 -1.12235 -0.547885
-2.01044 4.06481 -0.128382 0.51167 -1.70276 ...
</code></pre></td></tr></table>
<p>See <a href="MKL.html">Intel MKL</a> for details how to use native providers on Linux.</p>
</div>
<div class="span3">
<ul class="nav nav-list" id="menu">
<li class="nav-header">Math.NET Numerics</li>
<li><a href="https://numerics.mathdotnet.com/Packages.html">NuGet & Binaries</a></li>
<li><a href="https://numerics.mathdotnet.com/ReleaseNotes.html">Release Notes</a></li>
<li><a href="https://numerics.mathdotnet.com/License.html">MIT/X11 License</a></li>
<li><a href="https://numerics.mathdotnet.com/Compatibility.html">Platform Support</a></li>
<li><a href="https://numerics.mathdotnet.com/api/">Class Reference</a></li>
<li><a href="https://github.com/mathnet/mathnet-numerics/issues">Issues & Bugs</a></li>
<li><a href="https://numerics.mathdotnet.com/Users.html">Who is using Math.NET?</a></li>
<li class="nav-header">Contributing</li>
<li><a href="https://numerics.mathdotnet.com/Contributors.html">Contributors</a></li>
<li><a href="https://numerics.mathdotnet.com/Contributing.html">Contributing</a></li>
<li><a href="https://numerics.mathdotnet.com/Build.html">Build & Tools</a></li>
<li><a href="http://feedback.mathdotnet.com/forums/2060-math-net-numerics">Your Ideas</a></li>
<li class="nav-header">Getting Help</li>
<li><a href="https://discuss.mathdotnet.com/c/numerics">Discuss</a></li>
<li><a href="https://stackoverflow.com/questions/tagged/mathdotnet">Stack Overflow</a></li>
<li class="nav-header">Getting Started</li>
<li><a href="https://numerics.mathdotnet.com/">Getting started</a></li>
<li><a href="https://numerics.mathdotnet.com/Constants.html">Constants</a></li>
<li>Floating-Point Numbers</li>
<li>Arbitrary Precision Numbers</li>
<li>Complex Numbers</li>
<li><a href="https://numerics.mathdotnet.com/Matrix.html">Matrices and Vectors</a></li>
<li><a href="https://numerics.mathdotnet.com/Euclid.html">Euclid & Number Theory</a></li>
<li>Combinatorics</li>
<li class="nav-header">Evaluation</li>
<li><a href="https://numerics.mathdotnet.com/Functions.html">Special Functions</a></li>
<li>Differentiation</li>
<li><a href="https://numerics.mathdotnet.com/Integration.html">Integration</a></li>
<li class="nav-header">Statistics/Probability</li>
<li><a href="https://numerics.mathdotnet.com/DescriptiveStatistics.html">Descriptive Statistics</a></li>
<li><a href="https://numerics.mathdotnet.com/Probability.html">Probability Distributions</a></li>
<li class="nav-header">Generation</li>
<li><a href="https://numerics.mathdotnet.com/Generate.html">Generating Data</a></li>
<li><a href="https://numerics.mathdotnet.com/Random.html">Random Numbers</a></li>
<li class="nav-header">Transformation</li>
<li>Fourier Transform (FFT)</li>
<li>Filtering & DSP</li>
<li>Window Functions</li>
<li class="nav-header">Solving Equations</li>
<li><a href="https://numerics.mathdotnet.com/LinearEquations.html">Linear Equation Systems</a></li>
<li>Nonlinear Root Finding</li>
<li class="nav-header">Optimization</li>
<li>Linear Least Squares</li>
<li>Nonlinear Optimization</li>
<li><a href="https://numerics.mathdotnet.com/Distance.html">Distance Metrics</a></li>
<li class="nav-header">Curve Fitting</li>
<li><a href="https://numerics.mathdotnet.com/Regression.html">Regression</a></li>
<li>Interpolation</li>
<li>Fourier Approximation</li>
<li class="nav-header">Native Providers</li>
<li><a href="https://numerics.mathdotnet.com/MKL.html">Intel MKL</a></li>
<li class="nav-header">Working Together</li>
<li><a href="https://numerics.mathdotnet.com/CSV.html">Delimited Text Files (CSV)</a></li>
<li><a href="https://numerics.mathdotnet.com/MatrixMarket.html">NIST MatrixMarket</a></li>
<li><a href="https://numerics.mathdotnet.com/MatlabFiles.html">MATLAB</a></li>
<li><a href="https://numerics.mathdotnet.com/IFSharpNotebook.html">IF# Notebook</a></li>
<li>FsLab & Deedle</li>
<li>Microsoft Excel</li>
<li>numl.net machine learning</li>
<li>R-project</li>
</ul>
</div>
</div>
</div>
</body>
</html>