Math.NET Numerics

repository·master·Indexed 26 days ago

https://github.com/mathnet/mathnet-numerics

An open-source numerical library for .NET and Mono providing essential algorithms for science and engineering, including linear algebra, statistics, and special functions. It supports .NET 5.0+, .NET Framework 4.6.1+, and .NET Standard 2.0+. The library includes a Constants class for mathematical, scientific, and physical constants, and offers optional native providers for Intel MKL to optimize linear algebra performance.

Tokens
23K
Snippets
57
Records
151
Agent score
87%

What's inside Math.NET Numerics

  1. Choose the appropriate Statistics class for your data

    master

    Math.NET Numerics provides several classes for statistical analysis, optimized for different data structures and memory requirements:

    • Statistics: Provides common descriptive statistics as static extension methods to IEnumerable<double> sequences.
    • ArrayStatistics: Optimized for single-dimensional arrays. Includes Inplace methods that reorder the input array (partially sorting it) to improve performance.
    • SortedArrayStatistics: Optimized for arrays that are already sorted ascendingly. Provides highly efficient (sometimes constant time) order-statistics.
    • StreamingStatistics: Designed for processing large datasets that do not fit in memory by streaming data directly from disk or network.
    • DescriptiveStatistics: Used to gather a complete set of statistical characteristics (mean, median, variance, etc.) in a single pass over the data.
  2. Install Math.NET Numerics via NuGet

    master

    The primary package for most scenarios is MathNet.Numerics. It supports .NET 4.0, .NET Standard 1.3, and higher.

    If you are using F#, it is recommended to also install MathNet.Numerics.FSharp for a more idiomatic experience.

    If your project requires strong-named assemblies, use the .Signed variants, but note that these do not contain portable builds.

  3. Install IF# Notebook for interactive Math.NET Numerics use

    master

    IF# is an F# profile for iPython that supports IntelliSense and embedded FSharp.Charting. You can use it to interactively work with Math.NET Numerics in a browser-based notebook environment.

    To install IF# Notebook:

    1. Install Anaconda.
    2. Update conda and ipython via your shell:
      conda update conda
      conda update ipython
    3. Install IfSharp.
    conda update conda
    conda update ipython
  4. Use probability distributions in Math.NET Numerics

    master

    Math.NET Numerics provides a wide range of continuous, discrete, and multivariate probability distributions. You can use these distributions to investigate statistical properties (like mean, variance, and entropy) or to sample non-uniform random numbers.

    Distributions can be used in two ways:

    1. Instance-based: Create a parametrized instance of a distribution class. This allows for caching and accessing properties easily.
    2. Static-based: Call static methods directly on the distribution class, passing parameters as arguments. This is simpler for one-off calculations but does not allow for intermediate value caching.

    Common operations include:

    • Density (PDF): Density(x) or PDF(params, x)
    • Log-Density (ln PDF): DensityLn(x) or PDFLn(params, x)
    • Cumulative Distribution (CDF): CumulativeDistribution(x) or CDF(params, x)
    • Sampling: Sample() or Sample(params)
  5. Use MKL in F# Interactive

    master

    To use native providers in F# Interactive, you can explicitly set the NativeProviderPath to a shared directory.

    If using F# Power Tools in Visual Studio, you can extend the generated load-references.fsx script to automate loading.

    // Direct approach
    Control.NativeProviderPath <- @"C:\MKL"
    Control.UseNativeMKL()
    
    // Using a relative path in a script
    open System.IO
    open MathNet.Numerics
    
    Control.NativeProviderPath <- Path.Combine(__SOURCE_DIRECTORY__,"../")
    Control.UseNativeMKL()
  6. Configure distribution parameters and random sources

    master

    When instantiating distributions, you can specify parameters using different methods or provide a custom random number generator.

    • Alternative Parameterization: Some distributions offer static factory methods for different parameter sets (e.g., Normal.WithMeanPrecision vs the standard mean/standard deviation).
    • Custom Random Sources: All constructors optionally accept a random generator as the last argument. You can also replace the RandomSource on an existing instance.
    • Maximum-Likelihood Estimation: Some distributions (like LogNormal) provide an Estimate method to derive parameters from a set of samples.
  7. Setup MKL Native Binaries on Windows

    master

    On Windows, ensure the native libraries are accessible to the runtime. The simplest method is to place libMathNetNumericsMKL.dll and libiomp5md.dll in the same folder as your application's executable.

    If using NuGet, set the 'Copy to Output Directory' property for both libMathNetNumericsMKL.dll and libiomp5md.dll to Copy always.

  8. Install Intel MKL Native Provider

    master

    To use the Intel MKL native provider, install the package corresponding to your platform. These packages are MsBuild integrated and automatically select the correct 32-bit or 64-bit binary at runtime.

    Note: This provider requires Math.NET Numerics v3.6.0 or higher.

  9. Generate and preview documentation

    master

    Documentation is generated from CommonMark files in doc/content/.

    • Full Build: Run ./build.sh docs to generate the final documentation.
    • Local Development/Preview: To edit and preview docs locally with incremental updates, run ./build.sh DocsWatch in a separate console. This monitors content files and regenerates HTML automatically using local/relative URIs.
    • One-time Dev Build: Run ./build.sh DocsDev for a full build optimized for development.