public void SolveLinearSystem( [Values(10, 20, 50, 100, 200, 500)] int n) { double[][] a = Matrix.CreateMatrixData(n, n); double[][] x = Matrix.CreateMatrixData(n, 1); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (j == 0) { x[i][0] = Rnd.NextDouble(); } a[i][j] = Rnd.NextDouble(); } } Matrix ma = new Matrix(a); Matrix mx = new Matrix(x); Matrix mb = ma * mx; Assert.That(ma.Solve(mb), NumericIs.AlmostEqualTo(mx, 1e-10)); }
public void TestAccumulatorNumericStability() { /* NOTE: Statistically it is possible that this test fails even * if everything works as expected. However, it's very unlikely to happen, * and even more unlikely to happen in a series. */ Accumulator accumulator = new Accumulator(); NormalDistribution gaussian = new NormalDistribution(); // Test around 0, no stability issues expected gaussian.SetDistributionParameters(0, 1); for (int i = 0; i < 10000; i++) { accumulator.Add(gaussian.NextDouble()); } Assert.That(accumulator.Mean, NumericIs.AlmostEqualTo((double)0, 0.2), "Mean of (0,1)"); Assert.That(accumulator.Variance, NumericIs.AlmostEqualTo((double)1, 0.5), "Variance of (0,1)"); // Test around 10^9, potential stability issues accumulator.Clear(); gaussian.SetDistributionParameters(1e+9, 1); for (int i = 0; i < 10000; i++) { accumulator.Add(gaussian.NextDouble()); } Assert.That(accumulator.Mean, NumericIs.AlmostEqualTo(1e+9, 0.2), "Mean of (1e+9,1)"); Assert.That(accumulator.Variance, NumericIs.AlmostEqualTo((double)1, 0.5), "Variance of (1e+9,1)"); }
public void MatrixEigenvalueDecomposition() { double[][] pvals = { new double[] { 25, -5, 10 }, new double[] { -5, 17, 10 }, new double[] { 10, 10, 62 } }; double[][] evals = { new double[] { 0.0, 1.0, 0.0, 0.0 }, new double[] { 1.0, 0.0, 2e-7, 0.0 }, new double[] { 0.0, -2e-7, 0.0, 1.0 }, new double[] { 0.0, 0.0, 1.0, 0.0 } }; Matrix e = new Matrix(pvals); EigenvalueDecomposition eig = e.EigenvalueDecomposition; Matrix eigd = eig.BlockDiagonal; Matrix eigv = eig.EigenVectors; Assert.That(eigv * eigd, NumericIs.AlmostEqualTo(e * eigv), "EigenvalueDecomposition (symmetric)"); Matrix h = new Matrix(evals); eig = h.EigenvalueDecomposition; eigd = eig.BlockDiagonal; eigv = eig.EigenVectors; Assert.That(eigv * eigd, NumericIs.AlmostEqualTo(h * eigv, 1e-14), "EigenvalueDecomposition (nonsymmetric)"); }
static void ComplexTestImagZero(double[] samples) { for (int i = 1; i < samples.Length; i += 2) { Assert.That(samples[i], NumericIs.AlmostEqualTo((double)0), "Complex: Zero Imaginary Part"); } }
public void MatrixSingularValueDecomposition() { double[] columnwise = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 }; double[][] condmat = { new double[] { 1.0, 3.0 }, new double[] { 7.0, 9.0 } }; Matrix b = new Matrix(columnwise, 4); SingularValueDecomposition svd = b.SingularValueDecomposition; Assert.That(svd.LeftSingularVectors * (svd.S * Matrix.Transpose(svd.RightSingularVectors)), NumericIs.AlmostEqualTo(b), "SingularValueDecomposition"); // Matrix Rank (of rank deficient matrix) Matrix def = new Matrix(columnwise, 3); Assert.That(def.Rank(), Is.EqualTo(Math.Min(def.RowCount, def.ColumnCount) - 1), "Rank"); // Matrix Condition Matrix c = new Matrix(condmat); svd = c.SingularValueDecomposition; double[] singularvalues = svd.SingularValues; Assert.That(c.Condition(), NumericIs.AlmostEqualTo(singularvalues[0] / singularvalues[Math.Min(c.RowCount, c.ColumnCount) - 1]), "Condition"); }
public void FftSingleReal( [Values(1024, 4096, 8192, 16384, 65536, 262144, 1048576)] int numSamples) { int half = numSamples >> 1; double[] dataEven = new double[numSamples]; for (int i = 0; i < numSamples; i++) { double z = (double)(i - half) / half; dataEven[i] = 1.0 / ((z * z) + 1.0); } double[] evenReal, evenImag; _fft.TransformForward(dataEven, out evenReal, out evenImag); // round trip check double[] dataEven2; _fft.TransformBackward(evenReal, evenImag, out dataEven2); // Compare with original samples for (int i = 0; i < numSamples; i += 2) { Assert.That(dataEven2[i], NumericIs.AlmostEqualTo(dataEven[i], 1e-5), i.ToString()); } }
public void TestSpecialFunctions_Gamma() { // ensure poles return NaN Assert.That(Fn.Gamma(0.0), Is.NaN, "A1"); Assert.That(Fn.Gamma(-1.0), Is.NaN, "A2"); Assert.That(Fn.Gamma(-2.0), Is.NaN, "A3"); Assert.That(Fn.Gamma(-20.0), Is.NaN, "A4"); Assert.That(Fn.Gamma(-20.0000000001), Is.Not.NaN, "A4b"); // Compare Gamma with Maple: "evalf(GAMMA(x),20);" Assert.That(Fn.Gamma(0.001), NumericIs.AlmostEqualTo(999.42377248459546611), "B1"); Assert.That(Fn.Gamma(0.01), NumericIs.AlmostEqualTo(99.432585119150603714), "B2"); Assert.That(Fn.Gamma(0.1), NumericIs.AlmostEqualTo(9.5135076986687318363), "B3"); Assert.That(Fn.Gamma(0.2), NumericIs.AlmostEqualTo(4.5908437119988030532), "B4"); Assert.That(Fn.Gamma(0.4), NumericIs.AlmostEqualTo(2.2181595437576882231), "B5"); Assert.That(Fn.Gamma(0.6), NumericIs.AlmostEqualTo(1.4891922488128171024), "B6"); Assert.That(Fn.Gamma(0.9), NumericIs.AlmostEqualTo(1.0686287021193193549), "B7"); Assert.That(Fn.Gamma(0.999), NumericIs.AlmostEqualTo(1.0005782056293586480), "B8"); Assert.That(Fn.Gamma(1.0), NumericIs.AlmostEqualTo(1.0), "B9"); Assert.That(Fn.Gamma(1.001), NumericIs.AlmostEqualTo(.99942377248459546611), "B10"); Assert.That(Fn.Gamma(1.5), NumericIs.AlmostEqualTo(.88622692545275801365), "B11"); Assert.That(Fn.Gamma(1.9), NumericIs.AlmostEqualTo(.96176583190738741941), "B12"); Assert.That(Fn.Gamma(2.0), NumericIs.AlmostEqualTo(1.0), "B13"); Assert.That(Fn.Gamma(10.0), NumericIs.AlmostEqualTo(362880.0), "B14"); Assert.That(Fn.Gamma(10.51), NumericIs.AlmostEqualTo(1159686.4489708177739), "B15"); Assert.That(Fn.Gamma(100), NumericIs.AlmostEqualTo(.93326215443944152682e156), "B16"); Assert.That(Fn.Gamma(-0.01), NumericIs.AlmostEqualTo(-100.58719796441077919, 1e-14), "B17"); Assert.That(Fn.Gamma(-0.1), NumericIs.AlmostEqualTo(-10.686287021193193549), "B18"); Assert.That(Fn.Gamma(-0.5), NumericIs.AlmostEqualTo(-3.5449077018110320546), "B19"); Assert.That(Fn.Gamma(-1.2), NumericIs.AlmostEqualTo(4.8509571405220973902), "B20"); Assert.That(Fn.Gamma(-2.01), NumericIs.AlmostEqualTo(-49.547903041431840399, 1e-13), "B21"); Assert.That(Fn.Gamma(-100.01), NumericIs.AlmostEqualTo(-.10234011287149294961e-155, 1e-12), "B22"); }
public void TestSpecialFunctions_Digamma() { // ensure poles return NaN Assert.That(Fn.Digamma(0.0), Is.NaN, "A1"); Assert.That(Fn.Digamma(-1.0), Is.NaN, "A2"); Assert.That(Fn.Digamma(-2.0), Is.NaN, "A3"); Assert.That(Fn.Digamma(-20.0), Is.NaN, "A4"); Assert.That(Fn.Digamma(-20.0000000001), Is.Not.NaN, "A4b"); // Compare Gamma with Maple: "evalf(Psi(x),20);" Assert.That(Fn.Digamma(0.001), NumericIs.AlmostEqualTo(-1000.5755719318103005), "B1"); Assert.That(Fn.Digamma(0.01), NumericIs.AlmostEqualTo(-100.56088545786867450), "B2"); Assert.That(Fn.Digamma(0.1), NumericIs.AlmostEqualTo(-10.423754940411076795), "B3"); Assert.That(Fn.Digamma(0.2), NumericIs.AlmostEqualTo(-5.2890398965921882955), "B4"); Assert.That(Fn.Digamma(0.4), NumericIs.AlmostEqualTo(-2.5613845445851161457), "B5"); Assert.That(Fn.Digamma(0.6), NumericIs.AlmostEqualTo(-1.5406192138931904148), "B6"); Assert.That(Fn.Digamma(0.9), NumericIs.AlmostEqualTo(-.75492694994705139189), "B7"); Assert.That(Fn.Digamma(0.999), NumericIs.AlmostEqualTo(-.57886180210864542646), "B8"); Assert.That(Fn.Digamma(1.0), NumericIs.AlmostEqualTo(-.57721566490153286061), "B9"); Assert.That(Fn.Digamma(1.001), NumericIs.AlmostEqualTo(-.57557193181030047147, 1e-14), "B10"); Assert.That(Fn.Digamma(1.5), NumericIs.AlmostEqualTo(.36489973978576520559e-1, 1e-14), "B11"); Assert.That(Fn.Digamma(1.9), NumericIs.AlmostEqualTo(.35618416116405971922), "B12"); Assert.That(Fn.Digamma(2.0), NumericIs.AlmostEqualTo(.42278433509846713939), "B13"); Assert.That(Fn.Digamma(10.0), NumericIs.AlmostEqualTo(2.2517525890667211076), "B14"); Assert.That(Fn.Digamma(10.51), NumericIs.AlmostEqualTo(2.3039997054324985520), "B15"); Assert.That(Fn.Digamma(100), NumericIs.AlmostEqualTo(4.6001618527380874002), "B16"); Assert.That(Fn.Digamma(-0.01), NumericIs.AlmostEqualTo(99.406213695944404856), "B17"); Assert.That(Fn.Digamma(-0.1), NumericIs.AlmostEqualTo(9.2450730500529486081), "B18"); Assert.That(Fn.Digamma(-0.5), NumericIs.AlmostEqualTo(.36489973978576520559e-1, 1e-14), "B19"); Assert.That(Fn.Digamma(-1.2), NumericIs.AlmostEqualTo(4.8683247666271948739), "B20"); Assert.That(Fn.Digamma(-2.01), NumericIs.AlmostEqualTo(100.89382514365634023, 1e-13), "B21"); Assert.That(Fn.Digamma(-100.01), NumericIs.AlmostEqualTo(104.57736050326787844, 1e-12), "B22"); }
public void TestSpecialFunctions_BetaRegularized() { // Maple: Ix := (x,a,b) -> int(t^(a-1)*(1-t)^(b-1),t=0..x)/Beta(a,b); // Compare with Maple: "evalf(Ix(x,0.2,0.2),20);", with relative accuracy Assert.That(Fn.BetaRegularized(0.2, 0.2, 0.0), NumericIs.AlmostEqualTo(0.0), "A1"); Assert.That(Fn.BetaRegularized(0.2, 0.2, 0.2), NumericIs.AlmostEqualTo(.39272216435257082965), "A2"); Assert.That(Fn.BetaRegularized(0.2, 0.2, 0.5), NumericIs.AlmostEqualTo(.50000000000000000000), "A3"); Assert.That(Fn.BetaRegularized(0.2, 0.2, 0.8), NumericIs.AlmostEqualTo(.60727783564742917036), "A4"); Assert.That(Fn.BetaRegularized(0.2, 0.2, 1.0), NumericIs.AlmostEqualTo(1.0000000000000000000), "A5"); // Compare with Maple: "evalf(Ix(x,0.6,1.2),20);", with relative accuracy Assert.That(Fn.BetaRegularized(0.6, 1.2, 0.0), NumericIs.AlmostEqualTo(0.0), "B1"); Assert.That(Fn.BetaRegularized(0.6, 1.2, 0.2), NumericIs.AlmostEqualTo(.42540331997033591754), "B2"); Assert.That(Fn.BetaRegularized(0.6, 1.2, 0.5), NumericIs.AlmostEqualTo(.71641011564425207256), "B3"); Assert.That(Fn.BetaRegularized(0.6, 1.2, 0.8), NumericIs.AlmostEqualTo(.91373194998181983314), "B4"); Assert.That(Fn.BetaRegularized(0.6, 1.2, 1.0), NumericIs.AlmostEqualTo(1.0000000000000000000), "B5"); // Compare with Maple: "evalf(Ix(x,7.0,1.2),20);", with relative accuracy Assert.That(Fn.BetaRegularized(7.0, 1.2, 0.0), NumericIs.AlmostEqualTo(0.0), "C1"); Assert.That(Fn.BetaRegularized(7.0, 1.2, 0.2), NumericIs.AlmostEqualTo(.20126888449347947608e-4), "C2"); Assert.That(Fn.BetaRegularized(7.0, 1.2, 0.5), NumericIs.AlmostEqualTo(.11371092280417448678e-1), "C3"); Assert.That(Fn.BetaRegularized(7.0, 1.2, 0.7), NumericIs.AlmostEqualTo(.11102090346884848038, 1e-14), "C4"); Assert.That(Fn.BetaRegularized(7.0, 1.2, 0.8), NumericIs.AlmostEqualTo(.26774648551269072265, 1e-14), "C5"); Assert.That(Fn.BetaRegularized(7.0, 1.2, 0.9), NumericIs.AlmostEqualTo(.56477467605979107895), "C6"); Assert.That(Fn.BetaRegularized(7.0, 1.2, 0.95), NumericIs.AlmostEqualTo(.77753405618146275868), "C7"); Assert.That(Fn.BetaRegularized(7.0, 1.2, 1.0), NumericIs.AlmostEqualTo(1.0000000000000000000), "C8"); }
public void TestSpecialFunctions_Beta() { // Symmetry: Assert.That(Fn.Beta(0.1, 1.0), NumericIs.AlmostEqualTo(Fn.Beta(1.0, 0.1)), "A1"); Assert.That(Fn.Beta(0.1, 10.0), NumericIs.AlmostEqualTo(Fn.Beta(10.0, 0.1)), "A2"); Assert.That(Fn.Beta(0.5, 1.0), NumericIs.AlmostEqualTo(Fn.Beta(1.0, 0.5)), "A3"); Assert.That(Fn.Beta(0.5, 10.0), NumericIs.AlmostEqualTo(Fn.Beta(10.0, 0.5)), "A4"); Assert.That(Fn.Beta(10.0, 100.0), NumericIs.AlmostEqualTo(Fn.Beta(100.0, 10.0)), "A1"); // Compare with Maple: "evalf(Beta(0.1,x),20);", with relative accuracy Assert.That(Fn.Beta(0.1, 0.1), NumericIs.AlmostEqualTo(19.714639489050161663), "B1"); Assert.That(Fn.Beta(0.1, 0.2), NumericIs.AlmostEqualTo(14.599371492764829943), "B2"); Assert.That(Fn.Beta(0.1, 0.3), NumericIs.AlmostEqualTo(12.830598536321300437), "B3"); Assert.That(Fn.Beta(0.1, 1.0), NumericIs.AlmostEqualTo(10.0), "B4"); Assert.That(Fn.Beta(0.1, 2.0), NumericIs.AlmostEqualTo(9.0909090909090909091), "B5"); Assert.That(Fn.Beta(0.1, 5.0), NumericIs.AlmostEqualTo(8.1743590791584497328), "B6"); Assert.That(Fn.Beta(0.1, 10.0), NumericIs.AlmostEqualTo(7.5913800009109903433), "B7"); Assert.That(Fn.Beta(0.1, 100.0), NumericIs.AlmostEqualTo(6.0053229390929389725, 1e-12), "B8"); // Compare with Maple: "evalf(Beta(25.0,x),20);", with relative accuracy Assert.That(Fn.Beta(25.0, 0.1), NumericIs.AlmostEqualTo(6.9076854432998202098, 1e-13), "C1"); Assert.That(Fn.Beta(25.0, 0.2), NumericIs.AlmostEqualTo(2.4193558279880311532, 1e-14), "C2"); Assert.That(Fn.Beta(25.0, 0.3), NumericIs.AlmostEqualTo(1.1437887414566949564, 1e-14), "C3"); Assert.That(Fn.Beta(25.0, 1.0), NumericIs.AlmostEqualTo(.40000000000000000000e-1, 1e-14), "C4"); Assert.That(Fn.Beta(25.0, 2.0), NumericIs.AlmostEqualTo(.15384615384615384615e-2, 1e-14), "C5"); Assert.That(Fn.Beta(25.0, 5.0), NumericIs.AlmostEqualTo(.16841396151740979327e-5, 1e-13), "C6"); Assert.That(Fn.Beta(25.0, 10.0), NumericIs.AlmostEqualTo(.76261281522028757519e-9, 1e-13), "C7"); Assert.That(Fn.Beta(25.0, 100.0), NumericIs.AlmostEqualTo(.38445319996184968535e-27, 1e-13), "C8"); }
public void MatrixMultiplication() { /* * MATLAB: * prod = ma3x2 * md2x4 * prod_s = ma3x2 * 2 */ Matrix product = new Matrix(new double[][] { new double[] { -5, -4.2, -11, 8 }, new double[] { 11, 10.4, 19, -4 }, new double[] { 26, 31.7, 13, 74 } }); Assert.That(_ma3X2 * _md2X4, NumericIs.AlmostEqualTo(product), "prod 1"); Assert.That(_ma3X2.Multiply(_md2X4), NumericIs.AlmostEqualTo(product), "prod 2"); Matrix scaled = new Matrix(new double[][] { new double[] { 2, -4 }, new double[] { -2, 8 }, new double[] { 10, 14 } }); Assert.That(_ma3X2 * 2, NumericIs.AlmostEqualTo(scaled), "prod s 1"); Matrix scaledInplace = _ma3X2.Clone(); scaledInplace.MultiplyInplace(2); Assert.That(scaledInplace, NumericIs.AlmostEqualTo(scaled), "prod s 2"); }
public void MatrixMultiplicationByMatrix() { Matrix a = Matrix.Create( new double[3, 4] { { 10, -61, -8, -29 }, { 95, 11, -49, -47 }, { 40, -81, 91, 68 } }); Matrix b = Matrix.Create( new double[4, 2] { { 72, 37 }, { -23, 87 }, { 44, 29 }, { 98, -23 } }); Matrix c = Matrix.Create( new double[3, 2] { { -1071, -4502 }, { -175, 4132 }, { 15411, -4492 } }); Matrix p = a.Multiply(b); Assert.That(p.ColumnCount, Is.EqualTo(c.ColumnCount), "#A00 Invalid column count in linear product."); Assert.That(p.RowCount, Is.EqualTo(c.RowCount), "#A01 Invalid row count in linear product."); Assert.That(p, NumericIs.AlmostEqualTo(c), "#A02 Unexpected product value."); }
public void TestInterpolationMethod_Chebyshev1BarycentricPolynomial() { double[] x = new double[] { 0.0, 3.0, 2.5, 1.0, 3.0 }; IInterpolationMethod method = Interpolation.CreateOnChebyshevFirstKindPoints(0.0, 4.0, x); Assert.That(method, Is.TypeOf(typeof(ChebyshevFirstKindPolynomialInterpolation)), "Type"); double[] t = Interpolation.GenerateChebyshevFirstKindSamplePoints(0.0, 4.0, 5); for (int i = 0; i < 4; i++) { // verify the generated chebyshev1 points double tt = 2.0 + (2.0 * Math.Cos(Math.PI * 0.1 * ((2 * i) + 1))); Assert.That(tt, NumericIs.AlmostEqualTo(t[i]), "Point " + i.ToString()); // verify the interpolated values exactly at the sample points. Assert.That(method.Interpolate(tt), NumericIs.AlmostEqualTo(x[i]), "A Exact Point " + i.ToString()); } // Maple: "with(CurveFitting);" // Maple: "evalf(subs({x=0.1},PolynomialInterpolation(evalf([[2*cos(Pi/10)+2,0],[2*cos(3*Pi/10)+2,3],[2*cos(5*Pi/10)+2,2.5],[2*cos(7*Pi/10)+2,1],[2*cos(9*Pi/10)+2,3]]), x)),20);" Assert.That(method.Interpolate(0.1), NumericIs.AlmostEqualTo(2.9882560375702001608, 1e-15), "A 0.1"); Assert.That(method.Interpolate(0.4), NumericIs.AlmostEqualTo(1.7097090371118968872, 1e-15), "A 0.4"); Assert.That(method.Interpolate(1.1), NumericIs.AlmostEqualTo(1.0462830804302586508, 1e-15), "A 1.1"); Assert.That(method.Interpolate(3.2), NumericIs.AlmostEqualTo(2.951922899377369724, 1e-15), "A 3.2"); Assert.That(method.Interpolate(4.5), NumericIs.AlmostEqualTo(-5.394317844683536750, 1e-15), "A 4.5"); Assert.That(method.Interpolate(10.0), NumericIs.AlmostEqualTo(-228.01438153088988107, 1e-13), "A 10.0"); Assert.That(method.Interpolate(-10.0), NumericIs.AlmostEqualTo(1979.2646653044133954, 1e-12), "A -10.0"); }
public void MatrixTranspose2() { /* 2x3 rectangular case */ // MATLAB: trans_m = ma3x2' Matrix u = new Matrix(new double[][] { new double[] { 1, -1, 5 }, new double[] { -2, 4, 7 } }); Assert.That(Matrix.Transpose(_ma3X2), NumericIs.AlmostEqualTo(u), "trans m 1"); Assert.That(Matrix.Transpose(_ma3X2), Is.Not.SameAs(_ma3X2)); Assert.That(Matrix.Transpose(_ma3X2).GetArray(), Is.Not.SameAs(_ma3X2.GetArray())); Matrix uInplace = _ma3X2.Clone(); Assert.That(uInplace, Is.Not.SameAs(_ma3X2)); Assert.That(uInplace.GetArray(), Is.Not.SameAs(_ma3X2.GetArray())); double[][] internalArray = uInplace.GetArray(); uInplace.TransposeInplace(); Assert.That(uInplace, NumericIs.AlmostEqualTo(u), "trans m 2"); // 2009-05-23: Note, this internal behavior might change in a future release: Assert.That(uInplace.GetArray(), Is.Not.SameAs(internalArray)); }
public void TestSimpsonRule() { SimpsonRule algorithm = new SimpsonRule(); Assert.That( algorithm.IntegrateThreePoint(TargetFunctionA, StartA, StopA), NumericIs.AlmostEqualTo(TargetAreaA, 1.7e-1), "Direct (2 Partitions)"); Assert.That( algorithm.IntegrateComposite(TargetFunctionA, StartA, StopA, 2), NumericIs.AlmostEqualTo(TargetAreaA, 1.7e-1), "Composite 2 Partitions"); Assert.That( algorithm.IntegrateComposite(TargetFunctionA, StartA, StopA, 6), NumericIs.AlmostEqualTo(TargetAreaA, 1.2e-1), "Composite 6 Partitions"); Assert.That( algorithm.IntegrateComposite(TargetFunctionA, StartA, StopA, 10), NumericIs.AlmostEqualTo(TargetAreaA, 8e-3), "Composite 10 Partitions"); Assert.That( algorithm.IntegrateComposite(TargetFunctionA, StartA, StopA, 50), NumericIs.AlmostEqualTo(TargetAreaA, 8e-6), "Composite 50 Partitions"); Assert.That( algorithm.IntegrateComposite(TargetFunctionA, StartA, StopA, 1000), NumericIs.AlmostEqualTo(TargetAreaA, 5e-11), "Composite 1000 Partitions"); }
public void TestAccumulatorAddRemove() { Accumulator accumulator = new Accumulator(); for (int i = 0; i <= 10; i++) { accumulator.Add(i); } Assert.That(accumulator.Mean, NumericIs.AlmostEqualTo((double)5), "A Mean"); Assert.That(accumulator.Variance, NumericIs.AlmostEqualTo((double)11), "A Variance"); Assert.That(accumulator.Sum, NumericIs.AlmostEqualTo((double)55), "A Sum"); accumulator.Remove(9); accumulator.Remove(4); Assert.That(accumulator.Mean, NumericIs.AlmostEqualTo(14d / 3), "B Mean"); Assert.That(accumulator.Variance, NumericIs.AlmostEqualTo(23d / 2), "B Variance"); Assert.That(accumulator.Sum, NumericIs.AlmostEqualTo((double)42), "B Sum"); accumulator.Add(9); accumulator.Add(4); Assert.That(accumulator.Mean, NumericIs.AlmostEqualTo((double)5), "C Mean"); Assert.That(accumulator.Variance, NumericIs.AlmostEqualTo((double)11), "C Variance"); Assert.That(accumulator.Sum, NumericIs.AlmostEqualTo((double)55), "C Sum"); }
public void TestInterpolationMethod_CubicSpline_BoundarySecondDerivativeFixed() { double[] t = new double[] { -2.0, -1.0, 0.0, 1.0, 2.0 }; double[] x = new double[] { 1.0, 2.0, -1.0, 0.0, 1.0 }; IInterpolationMethod method = Interpolation.CreateCubicSpline(t, x, SplineBoundaryCondition.SecondDerivative, -5.0, SplineBoundaryCondition.SecondDerivative, -1.0); Assert.That(method, Is.TypeOf(typeof(CubicSplineInterpolation)), "Type"); for (int i = 0; i < t.Length; i++) { // verify the interpolated values exactly at the sample points. Assert.That(method.Interpolate(t[i]), Is.EqualTo(x[i]), "A Exact Point " + i.ToString()); } // Maple: "with(CurveFitting);" // Maple: "evalf(subs({x=-2.4},Spline([[-2,1],[-1,2],[0,-1],[1,0],[2,1]], x, degree=3, endpoints=Matrix(2,13,{(1,3)=1,(1,13)=-5,(2,10)=1,(2,13)=-1}))),20);" Assert.That(method.Interpolate(-2.4), NumericIs.AlmostEqualTo(-.8999999999999999993, 1e-15), "A -2.4"); Assert.That(method.Interpolate(-0.9), NumericIs.AlmostEqualTo(1.7590357142857142857, 1e-15), "A -0.9"); Assert.That(method.Interpolate(-0.5), NumericIs.AlmostEqualTo(.41517857142857142854, 1e-15), "A -0.5"); Assert.That(method.Interpolate(-0.1), NumericIs.AlmostEqualTo(-.82010714285714285714, 1e-15), "A -0.1"); Assert.That(method.Interpolate(0.1), NumericIs.AlmostEqualTo(-1.1026071428571428572, 1e-15), "A 0.1"); Assert.That(method.Interpolate(0.4), NumericIs.AlmostEqualTo(-1.0211428571428571429, 1e-15), "A 0.4"); Assert.That(method.Interpolate(1.2), NumericIs.AlmostEqualTo(.31771428571428571421, 1e-15), "A 1.2"); Assert.That(method.Interpolate(10.0), NumericIs.AlmostEqualTo((double)39, 1e-14), "A 10.0"); Assert.That(method.Interpolate(-10.0), NumericIs.AlmostEqualTo((double)(-37), 1e-14), "A -10.0"); }
public void TestSpecialFunctions_GammaRegularized() { /* * Maple: P := (a,x) -> 1 - GAMMA(a,x)/GAMMA(a) * Mathematica: GammaRegularized[a,0,x] */ // Special Points Assert.That(Fn.GammaRegularized(0, 0), Is.NaN, "(0,0) -> NaN"); // x axis (a=0) Assert.That(Fn.GammaRegularized(0, 1), NumericIs.AlmostEqualTo((double)1), "(0,1) -> 1"); Assert.That(Fn.GammaRegularized(0, 0.5), NumericIs.AlmostEqualTo((double)1), "(0,1/2) -> 1"); Assert.That(Fn.GammaRegularized(0, 0.001), NumericIs.AlmostEqualTo((double)1), "(0,1/1000) -> 1"); // a axis (x=0) Assert.That(Fn.GammaRegularized(1, 0), NumericIs.AlmostEqualTo((double)0), "(1,0) -> 0"); Assert.That(Fn.GammaRegularized(0.5, 0), NumericIs.AlmostEqualTo((double)0), "(1/2,0) -> 0"); Assert.That(Fn.GammaRegularized(0.001, 0), NumericIs.AlmostEqualTo((double)0), "(1/1000,0) -> 0"); // various points (some with known other representation) Assert.That(Fn.GammaRegularized(1, 1), NumericIs.AlmostEqualTo(0.63212055882855767840), "(1,1) -> 1-exp(-1)"); Assert.That(Fn.GammaRegularized(1, Math.PI), NumericIs.AlmostEqualTo(0.95678608173622775023), "(1,pi) -> 1-exp(-pi)"); Assert.That(Fn.GammaRegularized(0.5, 1), NumericIs.AlmostEqualTo(0.84270079294971486934), "(1/2,1) -> erf(1)"); Assert.That(Fn.GammaRegularized(0.5, 0.2), NumericIs.AlmostEqualTo(0.47291074313446191487), "(1/2,1/5) -> erf(sqrt(5)/5)"); Assert.That(Fn.GammaRegularized(0.5, 0.4), NumericIs.AlmostEqualTo(0.62890663047730242621), "(1/2,2/5) -> erf(sqrt(10)/5)"); Assert.That(Fn.GammaRegularized(0.5, 0.8), NumericIs.AlmostEqualTo(0.79409678926793169113), "(1/2,4/5) -> erf(sqrt(20)/5)"); Assert.That(Fn.GammaRegularized(0.25, 0.2), NumericIs.AlmostEqualTo(0.70985103173698245837), "(1/4,1/5)"); Assert.That(Fn.GammaRegularized(0.5, 20d), NumericIs.AlmostEqualTo(0.99999999974603714105), "(1/2,20) -> erf(2*5^(1/2))"); }
public void TestInterpolationMethod_CubicSpline_BoundaryFirstDerivativeFixed() { double[] t = new double[] { -2.0, -1.0, 0.0, 1.0, 2.0 }; double[] x = new double[] { 1.0, 2.0, -1.0, 0.0, 1.0 }; IInterpolationMethod method = Interpolation.CreateCubicSpline(t, x, SplineBoundaryCondition.FirstDerivative, 1.0, SplineBoundaryCondition.FirstDerivative, -1.0); Assert.That(method, Is.TypeOf(typeof(CubicSplineInterpolation)), "Type"); for (int i = 0; i < t.Length; i++) { // verify the interpolated values exactly at the sample points. Assert.That(method.Interpolate(t[i]), Is.EqualTo(x[i]), "A Exact Point " + i.ToString()); } // Maple: "with(CurveFitting);" // Maple: "evalf(subs({x=-2.4},Spline([[-2,1],[-1,2],[0,-1],[1,0],[2,1]], x, degree=3, endpoints=[1,-1])),20);" Assert.That(method.Interpolate(-2.4), NumericIs.AlmostEqualTo(1.120000000000000001, 1e-15), "A -2.4"); Assert.That(method.Interpolate(-0.9), NumericIs.AlmostEqualTo(1.8243928571428571428, 1e-15), "A -0.9"); Assert.That(method.Interpolate(-0.5), NumericIs.AlmostEqualTo(.54910714285714285715, 1e-15), "A -0.5"); Assert.That(method.Interpolate(-0.1), NumericIs.AlmostEqualTo(-.78903571428571428572, 1e-15), "A -0.1"); Assert.That(method.Interpolate(0.1), NumericIs.AlmostEqualTo(-1.1304642857142857143, 1e-15), "A 0.1"); Assert.That(method.Interpolate(0.4), NumericIs.AlmostEqualTo(-1.1040000000000000000, 1e-15), "A 0.4"); Assert.That(method.Interpolate(1.2), NumericIs.AlmostEqualTo(.4148571428571428571, 1e-15), "A 1.2"); Assert.That(method.Interpolate(10.0), NumericIs.AlmostEqualTo(-608.14285714285714286, 1e-15), "A 10.0"); Assert.That(method.Interpolate(-10.0), NumericIs.AlmostEqualTo(1330.1428571428571429, 1e-15), "A -10.0"); }
public void TestInterpolationMethod_LimitedOrderRationalWithPoles() { double[] t = new double[] { 0, 1, 3, 4, 5 }; double[] x = new double[] { 0, 3, 1000, -1000, 3 }; LimitedOrderRationalInterpolation method = new LimitedOrderRationalInterpolation(); method.Init(t, x); for (int i = 0; i < t.Length; i++) { Assert.That(method.Interpolate(t[i]), Is.EqualTo(x[i]), "Exact Point " + i.ToString()); } // Maple: "with(CurveFitting);" // Maple: "evalf(subs({x=0.1},RationalInterpolation([[0,0],[1,3],[3,1000],[4,-1000], [5,3]], x)),20);" Assert.That(method.Interpolate(0.1), NumericIs.AlmostEqualTo(.19389203383553566255, 1e-15), "A 0.1"); Assert.That(method.Interpolate(0.4), NumericIs.AlmostEqualTo(.88132900698869875369, 1e-14), "A 0.4"); Assert.That(method.Interpolate(1.1), NumericIs.AlmostEqualTo(3.5057665681580626913, 1e-15), "A 1.1"); Assert.That(method.Interpolate(3.01), NumericIs.AlmostEqualTo(1548.7666642693586902, 1e-13), "A 3.01"); Assert.That(method.Interpolate(3.02), NumericIs.AlmostEqualTo(3362.2564334253633516, 1e-13), "A 3.02"); Assert.That(method.Interpolate(3.03), NumericIs.AlmostEqualTo(-22332.603641443806014, 1e-12), "A 3.03"); Assert.That(method.Interpolate(3.1), NumericIs.AlmostEqualTo(-440.30323769822443789, 1e-14), "A 3.1"); Assert.That(method.Interpolate(3.2), NumericIs.AlmostEqualTo(-202.42421196280566349, 1e-14), "A 3.2"); Assert.That(method.Interpolate(4.5), NumericIs.AlmostEqualTo(21.208249625210155439, 1e-14), "A 4.5"); Assert.That(method.Interpolate(10.0), NumericIs.AlmostEqualTo(-4.8936986959784751517, 1e-13), "A 10.0"); Assert.That(method.Interpolate(-10.0), NumericIs.AlmostEqualTo(-3.6017584308603731307, 1e-13), "A -10.0"); }
public void TestInterpolationMethod_Chebyshev2BarycentricPolynomial() { double[] x = new double[] { 0.0, 3.0, 2.5, 1.0, 3.0 }; IInterpolationMethod method = Interpolation.CreateOnChebyshevSecondKindPoints(0.0, 4.0, x); Assert.That(method, Is.TypeOf(typeof(ChebyshevSecondKindPolynomialInterpolation)), "Type"); double[] t = Interpolation.GenerateChebyshevSecondKindSamplePoints(0.0, 4.0, 5); for (int i = 0; i < 4; i++) { // verify the generated chebyshev2 points double tt = 2.0 + (2.0 * Math.Cos(Math.PI * i * 0.25)); Assert.That(tt, NumericIs.AlmostEqualTo(t[i]), "Point " + i.ToString()); // verify the interpolated values exactly at the sample points. Assert.That(method.Interpolate(tt), NumericIs.AlmostEqualTo(x[i]), "A Exact Point " + i.ToString()); } // Maple: "with(CurveFitting);" // Maple: "evalf(subs({x=0.1},PolynomialInterpolation(evalf([[2*cos(0*Pi/4)+2,0],[2*cos(1*Pi/4)+2,3],[2*cos(2*Pi/4)+2,2.5],[2*cos(3*Pi/4)+2,1],[2*cos(4*Pi/4)+2,3]]), x)),20);" Assert.That(method.Interpolate(0.1), NumericIs.AlmostEqualTo(2.4826419375703841423, 1e-14), "A 0.1"); Assert.That(method.Interpolate(0.4), NumericIs.AlmostEqualTo(1.3814129880730972522, 1e-14), "A 0.4"); Assert.That(method.Interpolate(1.1), NumericIs.AlmostEqualTo(.8808232156067110292, 1e-15), "A 1.1"); Assert.That(method.Interpolate(3.2), NumericIs.AlmostEqualTo(3.478116015902536997, 1e-15), "A 3.2"); Assert.That(method.Interpolate(4.5), NumericIs.AlmostEqualTo(-5.035612822087164912, 1e-15), "A 4.5"); Assert.That(method.Interpolate(10.0), NumericIs.AlmostEqualTo(-369.20562748477140583, 1e-13), "A 10.0"); Assert.That(method.Interpolate(-10.0), NumericIs.AlmostEqualTo(1199.4696961966999204, 1e-12), "A -10.0"); }
public void TestSpecialFunctions_GammaLn_SpecialPoints() { // becomes negative in 1..2 Assert.That(Fn.GammaLn(1), NumericIs.AlmostEqualTo(0.0), "A1"); Assert.That(Fn.GammaLn(2), NumericIs.AlmostEqualTo(0.0), "A2"); Assert.That(Fn.GammaLn(1.461632145), NumericIs.AlmostEqualTo(-.1214862905, 1e-9), "A3"); Assert.That(Fn.GammaLn(1.2), NumericIs.AlmostEqualTo(-0.08537409000331584971970284), "A4"); Assert.That(Fn.GammaLn(1.8), NumericIs.AlmostEqualTo(-0.07108387291437216698800249), "A5"); // positive infinity at non-positive integers Assert.That(Fn.GammaLn(0.0), Is.EqualTo(double.PositiveInfinity), "A6"); Assert.That(Fn.GammaLn(-1.0), Is.EqualTo(double.PositiveInfinity), "A7"); Assert.That(Fn.GammaLn(-2.0), Is.EqualTo(double.PositiveInfinity), "A8"); Assert.That(Fn.GammaLn(-10.0), Is.EqualTo(double.PositiveInfinity), "A9"); Assert.That(Fn.GammaLn(-100.0), Is.EqualTo(double.PositiveInfinity), "A10"); Assert.That(Fn.GammaLn(-100.1), Is.Not.EqualTo(double.PositiveInfinity), "A11"); Assert.That(Fn.GammaLn(-99.9), Is.Not.EqualTo(double.PositiveInfinity), "A12"); Assert.That(Fn.GammaLn(-100000), Is.EqualTo(double.PositiveInfinity), "A13"); // continuous at branch points Assert.That(Fn.GammaLn(Number.Increment(13)), NumericIs.AlmostEqualTo(Fn.GammaLn(13)), "13+"); Assert.That(Fn.GammaLn(Number.Decrement(13)), NumericIs.AlmostEqualTo(Fn.GammaLn(13)), "13-"); Assert.That(Fn.GammaLn(Number.Increment(34)), NumericIs.AlmostEqualTo(Fn.GammaLn(34)), "34+"); Assert.That(Fn.GammaLn(Number.Decrement(34)), NumericIs.AlmostEqualTo(Fn.GammaLn(34)), "34-"); }
public void TestContinuousDistributions_StudensT() { TestContinuousDistributionShapeMatchesCumulativeDensity( new StudentsTDistribution(2), -2.0, 5.0, 10, 100000, 0.01, "StudentsTDistribution(2)"); StudentsTDistribution d = new StudentsTDistribution(2); // PDF - Evaluated in Maple with "stats[statevalf,pdf,studentst[2]](x);" Assert.That(d.ProbabilityDensity(0.0), NumericIs.AlmostEqualTo(0.3535533906, 1e-9), "pdf(0)"); Assert.That(d.ProbabilityDensity(1.0), NumericIs.AlmostEqualTo(0.1924500897, 1e-9), "pdf(1)"); Assert.That(d.ProbabilityDensity(2.0), NumericIs.AlmostEqualTo(0.06804138174, 1e-9), "pdf(2)"); Assert.That(d.ProbabilityDensity(3.0), NumericIs.AlmostEqualTo(0.02741012223, 1e-9), "pdf(3)"); Assert.That(d.ProbabilityDensity(4.0), NumericIs.AlmostEqualTo(0.01309457002, 1e-9), "pdf(4)"); Assert.That(d.ProbabilityDensity(-1.0), NumericIs.AlmostEqualTo(0.1924500897, 1e-9), "pdf(-1)"); Assert.That(d.ProbabilityDensity(-2.0), NumericIs.AlmostEqualTo(0.06804138174, 1e-9), "pdf(-2)"); Assert.That(d.ProbabilityDensity(-3.0), NumericIs.AlmostEqualTo(0.02741012223, 1e-9), "pdf(-3)"); Assert.That(d.ProbabilityDensity(-4.0), NumericIs.AlmostEqualTo(0.01309457002, 1e-9), "pdf(-4)"); // CDF - Evaluated in Maple with "stats[statevalf,cdf,studentst[2]](x);" Assert.That(d.CumulativeDistribution(0.0), NumericIs.AlmostEqualTo(0.5000000000), "cdf(0)"); Assert.That(d.CumulativeDistribution(1.0), NumericIs.AlmostEqualTo(0.7886751346, 1e-9), "cdf(1)"); Assert.That(d.CumulativeDistribution(2.0), NumericIs.AlmostEqualTo(0.9082482905, 1e-9), "cdf(2)"); Assert.That(d.CumulativeDistribution(3.0), NumericIs.AlmostEqualTo(0.9522670169, 1e-9), "cdf(3)"); Assert.That(d.CumulativeDistribution(4.0), NumericIs.AlmostEqualTo(0.9714045208, 1e-9), "cdf(4)"); Assert.That(d.CumulativeDistribution(-1.0), NumericIs.AlmostEqualTo(0.2113248654, 1e-9), "cdf(-1)"); Assert.That(d.CumulativeDistribution(-2.0), NumericIs.AlmostEqualTo(0.09175170954, 1e-9), "cdf(-2)"); Assert.That(d.CumulativeDistribution(-3.0), NumericIs.AlmostEqualTo(0.04773298313, 1e-9), "cdf(-3)"); Assert.That(d.CumulativeDistribution(-4.0), NumericIs.AlmostEqualTo(0.02859547921, 1e-9), "cdf(-4)"); }
public void Complex_MultiDim_1D_Inverse_Mix() { const int numSamples = 32; const int length = 2 * numSamples; int[] dims = new int[] { numSamples }; double[] data = new double[length]; for (int i = 0; i < length; i += 2) { double z = (double)(i - numSamples) / numSamples; data[i] = 1.0 / ((z * z) + 1.0); data[i + 1] = z / ((z * z) + 1.0); } data[1] = 0.0; // periodic continuation; force odd _cft.Convention = TransformationConvention.Matlab; _cft.TransformForward(data, dims); ComplexTestImagZero(data); /* Compare With MATLAB: * samples_t = 1.0i * ([-16:1:15] ./ 16) ./ (([-16:1:15] ./ 16) .^ 2 + 1.0) + 1.0 ./ (([-16:1:15] ./ 16) .^ 2 + 1.0) + samples_t(1) = real(samples_t(1)) + samples_f = fftn(samples_t) */ Assert.That(data[0 * 2], NumericIs.AlmostEqualTo(25.128, 0.001), "MATLAB 1"); Assert.That(data[1 * 2], NumericIs.AlmostEqualTo(-11.118, 0.001), "MATLAB 2"); Assert.That(data[2 * 2], NumericIs.AlmostEqualTo(-2.7838, 0.0001), "MATLAB 3"); Assert.That(data[6 * 2], NumericIs.AlmostEqualTo(-0.80124, 0.00001), "MATLAB 7"); Assert.That(data[7 * 2], NumericIs.AlmostEqualTo(-0.64953, 0.00001), "MATLAB 8"); Assert.That(data[8 * 2], NumericIs.AlmostEqualTo(-0.53221, 0.00001), "MATLAB 9"); Assert.That(data[13 * 2], NumericIs.AlmostEqualTo(-0.1689, 0.0001), "MATLAB 14"); Assert.That(data[14 * 2], NumericIs.AlmostEqualTo(-0.1158, 0.0001), "MATLAB 15"); Assert.That(data[15 * 2], NumericIs.AlmostEqualTo(-0.065071, 0.00001), "MATLAB 16"); Assert.That(data[20 * 2], NumericIs.AlmostEqualTo(0.18904, 0.0001), "MATLAB 21"); Assert.That(data[21 * 2], NumericIs.AlmostEqualTo(0.2475, 0.0001), "MATLAB 22"); Assert.That(data[22 * 2], NumericIs.AlmostEqualTo(0.31196, 0.00001), "MATLAB 23"); Assert.That(data[29 * 2], NumericIs.AlmostEqualTo(1.4812, 0.0001), "MATLAB 30"); Assert.That(data[30 * 2], NumericIs.AlmostEqualTo(2.1627, 0.0001), "MATLAB 31"); Assert.That(data[31 * 2], NumericIs.AlmostEqualTo(3.8723, 0.0001), "MATLAB 32"); _cft.TransformBackward(data, dims); // Compare with original samples for (int i = 0; i < length; i += 2) { double z = (double)(i - numSamples) / numSamples; Assert.That(data[i], Is.EqualTo(1.0 / ((z * z) + 1.0)).Within(0.00001), "Inv: Real: " + i); Assert.That(data[i + 1], Is.EqualTo(i == 0 ? 0.0 : z / ((z * z) + 1.0)).Within(0.00001), "Inv: Imag: " + i); } }
public void MatrixSolve4() { Matrix ma = new Matrix(new double[][] { new double[] { 1, 2, 3 }, new double[] { 5, 7, 11 }, new double[] { 13, 17, 19 } }); Matrix mx = new Matrix(new double[][] { new double[] { 23 }, new double[] { 29 }, new double[] { 31 } }); Matrix mb = ma * mx; Assert.That(ma.Solve(mb), NumericIs.AlmostEqualTo(mx)); }
public void TestSpecialFunctions_Sinc() { // Test at integers: for (int i = -10; i < 10; i++) { Assert.That(Fn.Sinc(i), NumericIs.AlmostEqualTo((i == 0) ? 1.0 : 0.0), "sinc(" + i.ToString() + ")"); } }
public void MatrixSolve3() { Matrix ma = new Matrix(new double[][] { new double[] { 1, 2 }, new double[] { 3, 5 } }); Matrix mx = new Matrix(new double[][] { new double[] { 7 }, new double[] { 11.0 } }); Matrix mb = ma * mx; Assert.That(ma.Solve(mb), NumericIs.AlmostEqualTo(mx)); }
public void TestSerializeComplex() { Complex before = Complex.Random(_random); Complex after = SerializeDeserialize(before); Assert.That(after, Is.Not.EqualTo(default(Complex)), "Not Null"); Assert.That(after, Is.EqualTo(before), "Equal"); Assert.That(after, NumericIs.AlmostEqualTo(before), "Almost Equal"); }
public void TestSerializePolynomial() { Polynomial before = new Polynomial(Vector.Random(10, _random)); Polynomial after = SerializeDeserialize(before); Assert.That(after, Is.Not.Null, "Not Null"); Assert.That(after, Is.EqualTo(before), "Equal"); Assert.That(after, NumericIs.AlmostEqualTo(before), "Almost Equal"); }
public void TestSerializeComplexVector() { ComplexVector before = ComplexVector.Random(10, _random); ComplexVector after = SerializeDeserialize(before); Assert.That(after, Is.Not.Null, "Not Null"); Assert.That(after, Is.EqualTo(before), "Equal"); Assert.That(after, NumericIs.AlmostEqualTo(before), "Almost Equal"); }