Math.Net入門筆記


統計量分析 

         若需要計算樣本參數的統計量特征分析,可以直接調用Statistics類的擴展方法,也可以將樣本傳遞給DescriptiveStatistics的構造函數一次性計算出全部特性。

代碼1是使用DescriptiveStatistics的例程:

using MathNet.Numerics.Statistics;
//ChiSquare創建X~2檢驗樣本,這里的樣本值可以換成自己的樣本集
var samples = new ChiSquare(5).Samples().Take(1000);
var statistics = new DescriptiveStatistics(samples);
 
// 最大值,最小值,中位數
var largestElement = statistics.Maximum;
var smallestElement = statistics.Minimum;
var median = statistics.Median;
 
// 平均數
var mean = statistics.Mean;
 
// 方差,標准差
var variance = statistics.Variance;
var stdDev = statistics.StandardDeviation;
 
// 峭度,不對稱度
var kurtosis = statistics.Kurtosis;
var skewness = statistics.Skewness;
using MathNet.Numerics.Statistics;
 
// 擴展方法是基於 IEnumerable<double>定義的
// 所以需要調用ToArray做一下轉化
var samples = new ChiSquare(5).Samples().Take(1000).ToArray()
;
 
// Order Statistics
var largestElement = samples.Maximum();
var smallestElement = samples.Minimum();
var median = samples.Median();
var 250thOrderStatistic = samples.OrderStatistic(250);
 
// Central Tendency
var mean = samples.Mean();
 
// Dispersion
var variance = samples.Variance();
var biasedPopulationVariance = samples.PopulationVariance();
var stdDev = samples.StandardDeviation();
var biasedPopulationStdDev = samples.
PopulationStandardDeviation();

線性代數

關於線性代數的計算位於命名空間MathNet.Numerics.LinearAlgebra下,該命名空間下有四個子命名空間。

l  MathNet.Numerics.LinearAlgebra.Double:雙精度浮點數計算

l  MathNet.Numerics.LinearAlgebra.Single:單精度浮點數計算

l  MathNet.Numerics.LinearAlgebra.Complex:進行復雜的雙精度浮點數計算

l  MathNet.Numerics.LinearAlgebra.Complex32:進行復雜的單精度浮點數計算

之所以分為四個子命名空間的原因是考慮了內存需求,雙精度浮點矩陣至少需要4倍同樣單精度浮點矩陣的內存。此外,好考慮了矩陣的存儲情況,現在Math.net支持三種矩陣的存儲:

l  DenseMatrix: 任意矩陣

l  SparseMatrix: 稀疏矩陣

l  DiagonalMatrix:對角陣

將矩陣分為這三種也也考慮了對應存儲的優化,比如稀疏矩陣在存儲時就使用了3數組的稀疏壓縮行(Compressed-sparse-row, CSR)格式。

代碼3是矩陣乘以向量的例程:

using MathNet.Numerics.LinearAlgebra.Double;
// Create a vector of dimension 5 with 4.0 in every entry.
var x = new DenseVector(5, 4.0);
// Create a 3 by 5 matrix with 2.0 in every entry.
var A = new DenseMatrix(3, 5, 2.0);
// Multiply the matrix and the vector.
var y = A * x;

特殊函數

l  階乘:Factorial

l  對數階乘:FactorialLn

l  伯努利系數:Binomial

l  對數伯努利系數:BinomialLn

l  多項式系數:Multinomial

using MathNet.Numerics;
double x = SpecialFunctions.Factorial(2);
// x will now have the value 2
 
double y = SpecialFunctions.Factorial(4);
// y will now have the value 24

函數插值

函數插值位於命名空間MathNet.Numerics.Interpolation。

Math.net的插值有兩種:

1.      為選定算法和樣本點創建一個插值結構,你將獲得一個實現了IInterpolation接口的類。

2.      使用該插值結構計算任意位置的點。一些插值算法還可以計算偏差和不定積分。

靜態類Interpolate提供了工廠方法來創建插值操作:

l  RationalWithoutPoles:創建 Floater-Hormann重心插值

l  RationalWithPoles:創建Bulirsch& Stoer有理插值

l  LinearBetweenPoints:創建樣條曲線插值

如果不確定使用哪種插值方法,我們推薦使用Floater-Hormann重心插值。或者,你也可以直接使用別的插值算法,它們都位於子命名空間Algorithms ,下面是一些插值算法:

等間距樣本點插值:

l  Polynomial: 重心算法,多項式插值

任意樣本點插值:

·        Rational pole-free:Floater-Hormann重心算法

·        Rational with poles: Bulirsch & Stoer算法

·        Neville Polynomial: Neville算法。注意 Neville算法在處理等間距樣本點時非常低效。.如果需要在等間距樣本點上進行多項式插值,推薦使用barycentric算法。

·        Linear Spline:樣條曲線

·        Cubic Spline 帶邊界條件的三次樣條曲線

·        Natural Cubic Spline:普通三次樣條曲線

·        Akima Cubic Spline:akima三次樣條曲線

其他數據插值:

·        Generic Barycentric Interpolation,需要barycentric權重

·        Generic Spline,需要樣條系數

·        Generic Cubic Hermite Spline,需要偏差

static void Main(string[] args)
    {
        // 1. 利用函數1/(1+x*x) 在區間 [-5, 5]產生10個樣本點
//points是x坐標,values是y坐標值
        Console.WriteLine(@"1. Generate 10 samples of the 
        function 1/(1+x*x) on interval [-5, 5]");
        double[] points;
        var values = SignalGenerator.EquidistantInterval(
        TargetFunction, -5, 5, 10, out points);
        Console.WriteLine();
 
        // 2. Create a floater hormann rational pole-free 
        interpolation based on arbitrary points
        // This method is used by default when create an 
        interpolation using Interpolate.Common method
        var method = Interpolate.RationalWithoutPoles(points,
        values);
        Console.WriteLine(@"2. Create a floater hormann 
        rational pole-free interpolation based on arbitrary 
        points");
        Console.WriteLine();
 
        // 3. 是否支持積分
        Console.WriteLine(@"3. Support integration = {0}", 
        method.SupportsIntegration);
        Console.WriteLine();
 
        // 4. 是否支持微分
        Console.WriteLine(@"4. Support differentiation = {0}
        ", method.SupportsDifferentiation);
        Console.WriteLine();
 
        // 5. 將插值結果和函數計算結果做比較
        Console.WriteLine(@"5. Interpolate ten random points 
        and compare to function results");
        var rng = new MersenneTwister(1);
        for (var i = 0; i < 10; i++)
        {
            // Generate random value from [0, 5]
            var point = rng.NextDouble() * 5;
            Console.WriteLine(@"Interpolate at {0} = {1}. 
            Function({0}) = {2}", point.ToString("N05"), 
            method.Interpolate(point).ToString("N05"), 
            TargetFunction(point).ToString("N05"));
        }
        Console.ReadKey();
    }
    public static double TargetFunction(double x)
    {
        return 1 / (1 + (x * x));
    }

線性積分變換

math.net目前僅支持兩種線性積分變換:離線傅立葉變換和離散哈特萊變換。它們都僅能在頻域下進行變換,但傅立葉變換支持復雜數值,而哈特萊變換僅支持實數值。它們都支持正變換和反變換,這是靠方法中的options參數來區分的。

傅立葉空間:離散傅立葉變換DFT和快速傅立葉變換FFT

目前支持的算法有:

·        Naive Discrete Fourier Transform (DFT): Out-placetransform for arbitrary vector lengths. Mainly intended for verifying fasteralgorithms: NaiveForward, NaiveInverse

·        Radix-2 Fast Fourier Transform (FFT): In-placefast fourier transform for vectors with a power-of-two length (Radix-2): Radix2Forward, Radix2Inverse

·        Bluestein Fast Fourier Transform (FFT): In-placefast fourier transform for arbitrary vector lengths:BluesteinForward, BluesteinInverse

另外,Transform類提供了靜態方法使我們能更容易地使用傅立葉正變換FourierForward和傅立葉反變換FourierInverse。

// create a complex sample vector of length 96
Complex[] samples = SignalGenerator.EquidistantInterval(
                        t => new Complex(1.0 / (t * t + 1.0),
                        t / (t * t + 1.0)),
-16, 16, 96);
 
// inplace bluestein FFT with default options
Transform.FourierForward(samples);

·        Default: Uses a negative exponent sign inforward transformations, and symmetric scaling (that is, sqrt(1/N) for bothforward and inverse transformation). This is the convention used in Maple andis widely accepted in the educational sector (due to the symmetry).

·        AsymmetricScaling: Set this flag to suppress scalingon the forward transformation but scale the inverse transform with 1/N.

·        NoScaling: Set this flag to suppress scaling forboth forward and inverse transformation. Note that in this case if you applyfirst the forward and then inverse transformation you won't get back theoriginal signal (by factor N/2).

·        InverseExponent: Uses the positive instead of thenegative sign in the forward exponent, and the negative (instead of positive)exponent in the inverse transformation.

·        Matlab: Use this flag if you need Matlab compatibility.Equals to setting the AsymmetricScaling flag.This matches the definition used in the wikipedia article.

·        NumericalRecipes: Use this flag if you needNumerical Recipes compatibility. Equal to setting both theInverseExponent and the NoScaling flags.

默認值:使用負指數符號進行正向轉換和對稱縮放(即正向和反向轉換均使用sqrt(1 / N))。這是Maple中使用的約定,並且由於對稱性而在教育界被廣泛接受。

·AsymmetricScaling:設置此標志可抑制正向變換的縮放,但以1 / N縮放逆向變換。

·NoScaling:設置此標志可抑制正向和反向變換的縮放。請注意,在這種情況下,如果先應用正向變換然后進行逆變換,則不會得到原始信號(因數N / 2)。

·InverseExponent:在正向指數中使用正號而不是負號,在逆變換中使用負號(而不是正號)。

·Matlab:如果需要Matlab兼容性,請使用此標志。等同於設置AsymmetricScaling標志。這與Wikipedia文章中使用的定義匹配。

·數值配方:如果需要數值配方兼容性,請使用此標志。等於同時設置了InverseExponent和NoScaling標志。

 

·        h(t) is real valued <=> real part of H(f) is even,imgainary part of H(f) is odd

·        h(t) is imaginary valued <=> real part of H(f) isodd, imaginary part of H(f) is even

·        h(t) is even <=> H(f) is even

·        h(t) is odd <=> H(f) is odd

·        h(t) is real-valued even <=> H(f) is real-valuedeven

·        h(t) is real-valued odd <=> H(f) isimaginary-valued odd

·        h(t) is imaginary-valued even <=> H(f) isimaginary-valued even

·        h(t) is imaginary-valued odd <=> H(f) isreal-valued odd

·h(t)是實值<=> H(f)的實部是偶數,H(f)的虛部是奇數

·h(t)是虛值<=> H(f)的實部是奇數,H(f)的虛部是偶數

·h(t)是偶數<=> H(f)是偶數

·h(t)是奇數<=> H(f)是奇數

·h(t)是實值,即使<=> H(f)是實值偶

·h(t)是實值奇數<=> H(f)是虛值奇數

·h(t)是虛值偶數== H(f)是虛數偶數

·h(t)是虛數值奇數<=> H(f)是虛數值奇數


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM