csharpfftfsharpintegrationinterpolationlinear-algebramathdifferentiationmatrixnumericsrandomregressionstatisticsmathnet
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.
280 lines
21 KiB
280 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, shrink-to-fit=no">
|
|
<meta name="author" content="Christoph Ruegg, Marcus Cuda, Jurgen Van Gael">
|
|
|
|
<link rel="stylesheet" id="theme_link" href="https://cdnjs.cloudflare.com/ajax/libs/bootswatch/4.6.0/materia/bootstrap.min.css">
|
|
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-Piv4xVNRyMGpqkS2by6br4gNJ7DXjqk09RmUpJ8jgGtD7zP9yug3goQfGII0yAns" crossorigin="anonymous"></script>
|
|
|
|
<script type="text/javascript" async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML"></script>
|
|
|
|
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico">
|
|
<link type="text/css" rel="stylesheet" href="https://numerics.mathdotnet.com/content/navbar-fixed-left.css" />
|
|
<link type="text/css" rel="stylesheet" href="https://numerics.mathdotnet.com/content/fsdocs-default.css" />
|
|
<link type="text/css" rel="stylesheet" href="https://numerics.mathdotnet.com/content/fsdocs-custom.css" />
|
|
<script type="text/javascript" src="https://numerics.mathdotnet.com/content/fsdocs-tips.js"></script>
|
|
<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
|
|
<!--[if lt IE 9]>
|
|
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
|
|
<![endif]-->
|
|
<!-- BEGIN SEARCH BOX: this adds support for the search box -->
|
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/JavaScript-autoComplete/1.0.4/auto-complete.css" />
|
|
<!-- END SEARCH BOX: this adds support for the search box -->
|
|
|
|
</head>
|
|
|
|
<body>
|
|
<nav class="navbar navbar-expand-md navbar-light bg-secondary fixed-left" id="fsdocs-nav">
|
|
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
|
|
<span class="navbar-toggler-icon"></span>
|
|
</button>
|
|
<div class="collapse navbar-collapse navbar-nav-scroll" id="navbarsExampleDefault">
|
|
<a href="https://numerics.mathdotnet.com/"><img id="fsdocs-logo" src="/logo.png" /></a>
|
|
<!-- BEGIN SEARCH BOX: this adds support for the search box -->
|
|
<div id="header">
|
|
<div class="searchbox" id="fsdocs-searchbox">
|
|
<label for="search-by">
|
|
<i class="fas fa-search"></i>
|
|
</label>
|
|
<input data-search-input="" id="search-by" type="search" placeholder="Search..." />
|
|
<span data-search-clear="">
|
|
<i class="fas fa-times"></i>
|
|
</span>
|
|
</div>
|
|
</div>
|
|
<!-- END SEARCH BOX: this adds support for the search box -->
|
|
<ul class="navbar-nav">
|
|
<li class="nav-header">Math.NET Numerics</li>
|
|
<li class="nav-item"><a class="nav-link" href="Packages.html">NuGet & Binaries</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="ReleaseNotes.html">Release Notes</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="https://github.com/mathnet/mathnet-numerics/blob/master/LICENSE.md">MIT License</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="Compatibility.html">Platform Support</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="https://numerics.mathdotnet.com/api/">Class Reference</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="https://github.com/mathnet/mathnet-numerics/issues">Issues & Bugs</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="Users.html">Who is using Math.NET?</a></li>
|
|
|
|
<li class="nav-header">Contributing</li>
|
|
<li class="nav-item"><a class="nav-link" href="Contributors.html">Contributors</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="Contributing.html">Contributing</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="Build.html">Build & Tools</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="https://github.com/mathnet/mathnet-numerics/discussions/categories/ideas">Your Ideas</a></li>
|
|
|
|
<li class="nav-header">Getting Help</li>
|
|
<li class="nav-item"><a class="nav-link" href="https://discuss.mathdotnet.com/c/numerics">Discuss</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="https://stackoverflow.com/questions/tagged/mathdotnet">Stack Overflow</a></li>
|
|
|
|
<li class="nav-header">Getting Started</li>
|
|
<l class="nav-item"i><a class="nav-link" href="/">Getting started</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="Constants.html">Constants</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="Matrix.html">Matrices and Vectors</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="Euclid.html">Euclid & Number Theory</a></li>
|
|
<li class="nav-item">Combinatorics</li>
|
|
|
|
<li class="nav-header">Evaluation</li>
|
|
<li class="nav-item"><a class="nav-link" href="Functions.html">Special Functions</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="Integration.html">Integration</a></li>
|
|
|
|
<li class="nav-header">Statistics/Probability</li>
|
|
<li class="nav-item"><a class="nav-link" href="DescriptiveStatistics.html">Descriptive Statistics</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="Probability.html">Probability Distributions</a></li>
|
|
|
|
<li class="nav-header">Generation</li>
|
|
<li class="nav-item"><a class="nav-link" href="Generate.html">Generating Data</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="Random.html">Random Numbers</a></li>
|
|
|
|
<li class="nav-header">Solving Equations</li>
|
|
<li class="nav-item"><a class="nav-link" href="LinearEquations.html">Linear Equation Systems</a></li>
|
|
|
|
<li class="nav-header">Optimization</li>
|
|
<li class="nav-item"><a class="nav-link" href="Distance.html">Distance Metrics</a></li>
|
|
|
|
<li class="nav-header">Curve Fitting</li>
|
|
<li class="nav-item"><a class="nav-link" href="Regression.html">Regression</a></li>
|
|
|
|
<li class="nav-header">Native Providers</li>
|
|
<li class="nav-item"><a class="nav-link" href="MKL.html">Intel MKL</a></li>
|
|
|
|
<li class="nav-header">Working Together</li>
|
|
<li class="nav-item"><a class="nav-link" href="CSV.html">Delimited Text Files (CSV)</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="MatrixMarket.html">NIST MatrixMarket</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="MatlabFiles.html">MATLAB</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="IFSharpNotebook.html">IF# Notebook</a></li>
|
|
</ul>
|
|
</div>
|
|
</nav>
|
|
<div class="container">
|
|
<div class="masthead">
|
|
<h3 class="muted">
|
|
<a href="https://numerics.mathdotnet.com">Math.NET Numerics</a> |
|
|
<a href="https://www.mathdotnet.com">Math.NET Project</a> |
|
|
<a href="https://github.com/mathnet/mathnet-numerics">GitHub</a>
|
|
</h3>
|
|
</div>
|
|
<hr />
|
|
<div class="container" id="fsdocs-content">
|
|
<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 License</a>.
|
|
It targets Microsoft .NET 5.0, .NET 4.6.1 and higher, and .NET Standard 2.0
|
|
and higher. 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 & 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="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<<span class="k">double</span>> 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<<span class="k">double</span>>[] 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>
|
|
<pre class="fssnip highlighted"><code lang="fsharp"><span class="k">open</span> <span class="id">MathNet</span><span class="pn">.</span><span class="id">Numerics</span><span class="pn">.</span><span class="id">LinearAlgebra</span>
|
|
<span class="k">let</span> <span onmouseout="hideTip(event, 'fs1', 1)" onmouseover="showTip(event, 'fs1', 1)" class="id">m</span> <span class="o">=</span> <span class="id">matrix</span> <span class="pn">[</span><span class="pn">[</span> <span class="n">1.0</span><span class="pn">;</span> <span class="n">2.0</span> <span class="pn">]</span>
|
|
<span class="pn">[</span> <span class="n">3.0</span><span class="pn">;</span> <span class="n">4.0</span> <span class="pn">]</span><span class="pn">]</span>
|
|
<span class="k">let</span> <span onmouseout="hideTip(event, 'fs2', 2)" onmouseover="showTip(event, 'fs2', 2)" class="id">m'</span> <span class="o">=</span> <span onmouseout="hideTip(event, 'fs1', 3)" onmouseover="showTip(event, 'fs1', 3)" class="id">m</span><span class="pn">.</span><span class="id">Inverse</span><span class="pn">(</span><span class="pn">)</span>
|
|
</code></pre>
|
|
<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>
|
|
<pre class="fssnip highlighted"><code lang="fsharp"><span class="pp">#load</span> <span class="s">"../packages/MathNet.Numerics.FSharp/MathNet.Numerics.fsx"</span>
|
|
|
|
<span class="k">open</span> <span class="id">MathNet</span><span class="pn">.</span><span class="id">Numerics</span>
|
|
<span class="id">SpecialFunctions</span><span class="pn">.</span><span class="id">Gamma</span><span class="pn">(</span><span class="n">0.5</span><span class="pn">)</span>
|
|
|
|
<span class="k">open</span> <span class="id">MathNet</span><span class="pn">.</span><span class="id">Numerics</span><span class="pn">.</span><span class="id">LinearAlgebra</span>
|
|
<span class="k">let</span> <span onmouseout="hideTip(event, 'fs1', 4)" onmouseover="showTip(event, 'fs1', 4)" class="id">m</span> <span class="pn">:</span> <span class="id">Matrix</span><span class="pn"><</span><span onmouseout="hideTip(event, 'fs3', 5)" onmouseover="showTip(event, 'fs3', 5)" class="id">float</span><span class="pn">></span> <span class="o">=</span> <span class="id">DenseMatrix</span><span class="pn">.</span><span class="id">randomStandard</span> <span class="n">50</span> <span class="n">50</span>
|
|
<span class="pn">(</span><span onmouseout="hideTip(event, 'fs1', 6)" onmouseover="showTip(event, 'fs1', 6)" class="id">m</span> <span class="o">*</span> <span onmouseout="hideTip(event, 'fs1', 7)" onmouseover="showTip(event, 'fs1', 7)" class="id">m</span><span class="pn">.</span><span class="id">Transpose</span><span class="pn">(</span><span class="pn">)</span><span class="pn">)</span><span class="pn">.</span><span class="id">Determinant</span><span class="pn">(</span><span class="pn">)</span>
|
|
</code></pre>
|
|
<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="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="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="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="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="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<<span class="k">double</span>>.Build.Random(<span class="n">500</span>, <span class="n">500</span>);
|
|
<span class="k">var</span> v <span class="o">=</span> Vector<<span class="k">double</span>>.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="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="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 class="fsdocs-tip" id="fs1">val m : obj</div>
|
|
<div class="fsdocs-tip" id="fs2">val m' : obj</div>
|
|
<div class="fsdocs-tip" id="fs3">Multiple items<br />val float : value:'T -> float (requires member op_Explicit)<br /><em><summary>Converts the argument to 64-bit float. This is a direct conversion for all
|
|
primitive numeric types. For strings, the input is converted using <c>Double.Parse()</c>
|
|
with InvariantCulture settings. Otherwise the operation requires an appropriate
|
|
static conversion method on the input type.</summary><br /><param name="value">The input value.</param><br /><returns>The converted float</returns></em><br /><br />--------------------<br />[<Struct>]
|
|
type float = System.Double<br /><em><summary>An abbreviation for the CLI type <see cref="T:System.Double" />.</summary><br /><category>Basic Types</category></em><br /><br />--------------------<br />type float<'Measure> =
|
|
float<br /><em><summary>The type of double-precision floating point numbers, annotated with a unit of measure.
|
|
The unit of measure is erased in compiled code and when values of this type
|
|
are analyzed using reflection. The type is representationally equivalent to
|
|
<see cref="T:System.Double" />.</summary><br /><category index="6">Basic Types with Units of Measure</category></em></div>
|
|
|
|
</div>
|
|
<!-- BEGIN SEARCH BOX: this adds support for the search box -->
|
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/JavaScript-autoComplete/1.0.4/auto-complete.css" />
|
|
<script type="text/javascript">var fsdocs_search_baseurl = 'https://numerics.mathdotnet.com/';</script>
|
|
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lunr.js/2.3.8/lunr.min.js"></script>
|
|
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/JavaScript-autoComplete/1.0.4/auto-complete.min.js"></script>
|
|
<script type="text/javascript" src="https://numerics.mathdotnet.com/content/fsdocs-search.js"></script>
|
|
<!-- END SEARCH BOX: this adds support for the search box -->
|
|
</div>
|
|
</body>
|
|
|
|
</html>
|
|
|