From aebb93a04afd4916deddce2e6b7e878ea77347ea Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Fri, 20 Dec 2013 21:04:31 +0100 Subject: [PATCH] Interpolation: Differentiate2 instead of DifferentiateAll; definite integral; cleanup --- .../Interpolation/AkimaSplineInterpolation.cs | 41 +++++------ .../Interpolation/BarycentricInterpolation.cs | 31 ++++---- .../BulirschStoerRationalInterpolation.cs | 31 ++++---- .../CubicHermiteSplineInterpolation.cs | 33 ++++----- .../Interpolation/CubicSplineInterpolation.cs | 33 ++++----- .../EquidistantPolynomialInterpolation.cs | 31 ++++---- .../FloaterHormannRationalInterpolation.cs | 31 ++++---- src/Numerics/Interpolation/IInterpolation.cs | 30 ++++---- src/Numerics/Interpolation/LinearSpline.cs | 32 ++++++--- .../NevillePolynomialInterpolation.cs | 39 ++++++----- .../Interpolation/SplineInterpolation.cs | 70 +++++++++---------- .../InterpolationTests/AkimaSplineTest.cs | 16 ++--- .../BulirschStoerRationalTest.cs | 12 ++-- .../InterpolationTests/CubicSplineTest.cs | 41 ++--------- .../EquidistantPolynomialTest.cs | 14 ++-- .../FloaterHormannRationalTest.cs | 18 ++--- .../LinearInterpolationCase.cs | 8 +-- .../InterpolationTests/LinearSplineTest.cs | 70 ++++++++++++------- .../NevillePolynomialTest.cs | 28 +++----- 19 files changed, 297 insertions(+), 312 deletions(-) diff --git a/src/Numerics/Interpolation/AkimaSplineInterpolation.cs b/src/Numerics/Interpolation/AkimaSplineInterpolation.cs index 91a452d7..40ebae5d 100644 --- a/src/Numerics/Interpolation/AkimaSplineInterpolation.cs +++ b/src/Numerics/Interpolation/AkimaSplineInterpolation.cs @@ -4,7 +4,7 @@ // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com // -// Copyright (c) 2009-2010 Math.NET +// Copyright (c) 2009-2013 Math.NET // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation @@ -69,8 +69,6 @@ namespace MathNet.Numerics.Interpolation /// /// Gets a value indicating whether the algorithm supports differentiation (interpolated derivative). /// - /// - /// bool IInterpolation.SupportsDifferentiation { get { return true; } @@ -79,7 +77,6 @@ namespace MathNet.Numerics.Interpolation /// /// Gets a value indicating whether the algorithm supports integration (interpolated quadrature). /// - /// bool IInterpolation.SupportsIntegration { get { return true; } @@ -155,11 +152,11 @@ namespace MathNet.Numerics.Interpolation derivatives[i] = weights[i - 1].AlmostEqual(0.0) && weights[i + 1].AlmostEqual(0.0) ? (((samplePoints[i + 1] - samplePoints[i])*differences[i - 1]) - + ((samplePoints[i] - samplePoints[i - 1])*differences[i])) - /(samplePoints[i + 1] - samplePoints[i - 1]) + + ((samplePoints[i] - samplePoints[i - 1])*differences[i])) + /(samplePoints[i + 1] - samplePoints[i - 1]) : ((weights[i + 1]*differences[i - 1]) - + (weights[i - 1]*differences[i])) - /(weights[i + 1] + weights[i - 1]); + + (weights[i - 1]*differences[i])) + /(weights[i + 1] + weights[i - 1]); } derivatives[0] = DifferentiateThreePoint(samplePoints, sampleValues, 0, 0, 1, 2); @@ -227,34 +224,38 @@ namespace MathNet.Numerics.Interpolation /// /// Point t to interpolate at. /// Interpolated first derivative at point t. - /// - /// public double Differentiate(double t) { return _spline.Differentiate(t); } /// - /// Interpolate, differentiate and 2nd differentiate at point t. + /// Differentiate twice at point t. /// /// Point t to interpolate at. - /// Interpolated first derivative at point t. - /// - /// - public Tuple DifferentiateAll(double t) + /// Interpolated second derivative at point t. + public double Differentiate2(double t) { - return _spline.DifferentiateAll(t); + return _spline.Differentiate2(t); } /// - /// Integrate up to point t. + /// Indefinite integral at point t. /// - /// Right bound of the integration interval [a,t]. - /// Interpolated definite integral over the interval [a,t]. - /// + /// Point t to integrate at. public double Integrate(double t) { return _spline.Integrate(t); } + + /// + /// Definite integral between points a and b. + /// + /// Left bound of the integration interval [a,b]. + /// Right bound of the integration interval [a,b]. + public double Integrate(double a, double b) + { + return _spline.Integrate(a, b); + } } } diff --git a/src/Numerics/Interpolation/BarycentricInterpolation.cs b/src/Numerics/Interpolation/BarycentricInterpolation.cs index 89cc0ccb..88e01e9e 100644 --- a/src/Numerics/Interpolation/BarycentricInterpolation.cs +++ b/src/Numerics/Interpolation/BarycentricInterpolation.cs @@ -4,7 +4,7 @@ // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com // -// Copyright (c) 2009-2010 Math.NET +// Copyright (c) 2009-2013 Math.NET // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation @@ -77,8 +77,6 @@ namespace MathNet.Numerics.Interpolation /// /// Gets a value indicating whether the algorithm supports differentiation (interpolated derivative). /// - /// - /// bool IInterpolation.SupportsDifferentiation { get { return false; } @@ -87,7 +85,6 @@ namespace MathNet.Numerics.Interpolation /// /// Gets a value indicating whether the algorithm supports integration (interpolated quadrature). /// - /// bool IInterpolation.SupportsIntegration { get { return false; } @@ -201,34 +198,38 @@ namespace MathNet.Numerics.Interpolation /// /// Point t to interpolate at. /// Interpolated first derivative at point t. - /// - /// double IInterpolation.Differentiate(double t) { throw new NotSupportedException(); } /// - /// Interpolate, differentiate and 2nd differentiate at point t. NOT SUPPORTED. + /// Differentiate twice at point t. NOT SUPPORTED. /// /// Point t to interpolate at. - /// Interpolated first derivative at point t. - /// - /// - Tuple IInterpolation.DifferentiateAll(double t) + /// Interpolated second derivative at point t. + double IInterpolation.Differentiate2(double t) { throw new NotSupportedException(); } /// - /// Integrate up to point t. NOT SUPPORTED. + /// Indefinite integral at point t. NOT SUPPORTED. /// - /// Right bound of the integration interval [a,t]. - /// Interpolated definite integral over the interval [a,t]. - /// + /// Point t to integrate at. double IInterpolation.Integrate(double t) { throw new NotSupportedException(); } + + /// + /// Definite integral between points a and b. NOT SUPPORTED. + /// + /// Left bound of the integration interval [a,b]. + /// Right bound of the integration interval [a,b]. + double IInterpolation.Integrate(double a, double b) + { + throw new NotSupportedException(); + } } } diff --git a/src/Numerics/Interpolation/BulirschStoerRationalInterpolation.cs b/src/Numerics/Interpolation/BulirschStoerRationalInterpolation.cs index efce88a4..b97f97e0 100644 --- a/src/Numerics/Interpolation/BulirschStoerRationalInterpolation.cs +++ b/src/Numerics/Interpolation/BulirschStoerRationalInterpolation.cs @@ -4,7 +4,7 @@ // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com // -// Copyright (c) 2009-2010 Math.NET +// Copyright (c) 2009-2013 Math.NET // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation @@ -73,8 +73,6 @@ namespace MathNet.Numerics.Interpolation /// /// Gets a value indicating whether the algorithm supports differentiation (interpolated derivative). /// - /// - /// bool IInterpolation.SupportsDifferentiation { get { return false; } @@ -83,7 +81,6 @@ namespace MathNet.Numerics.Interpolation /// /// Gets a value indicating whether the algorithm supports integration (interpolated quadrature). /// - /// bool IInterpolation.SupportsIntegration { get { return false; } @@ -187,34 +184,38 @@ namespace MathNet.Numerics.Interpolation /// /// Point t to interpolate at. /// Interpolated first derivative at point t. - /// - /// double IInterpolation.Differentiate(double t) { throw new NotSupportedException(); } /// - /// Interpolate, differentiate and 2nd differentiate at point t. NOT SUPPORTED. + /// Differentiate twice at point t. NOT SUPPORTED. /// /// Point t to interpolate at. - /// Interpolated first derivative at point t. - /// - /// - Tuple IInterpolation.DifferentiateAll(double t) + /// Interpolated second derivative at point t. + double IInterpolation.Differentiate2(double t) { throw new NotSupportedException(); } /// - /// Integrate up to point t. NOT SUPPORTED. + /// Indefinite integral at point t. NOT SUPPORTED. /// - /// Right bound of the integration interval [a,t]. - /// Interpolated definite integral over the interval [a,t]. - /// + /// Point t to integrate at. double IInterpolation.Integrate(double t) { throw new NotSupportedException(); } + + /// + /// Definite integral between points a and b. NOT SUPPORTED. + /// + /// Left bound of the integration interval [a,b]. + /// Right bound of the integration interval [a,b]. + double IInterpolation.Integrate(double a, double b) + { + throw new NotSupportedException(); + } } } diff --git a/src/Numerics/Interpolation/CubicHermiteSplineInterpolation.cs b/src/Numerics/Interpolation/CubicHermiteSplineInterpolation.cs index b4b88682..ee54263f 100644 --- a/src/Numerics/Interpolation/CubicHermiteSplineInterpolation.cs +++ b/src/Numerics/Interpolation/CubicHermiteSplineInterpolation.cs @@ -4,7 +4,7 @@ // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com // -// Copyright (c) 2009-2010 Math.NET +// Copyright (c) 2009-2013 Math.NET // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation @@ -70,8 +70,6 @@ namespace MathNet.Numerics.Interpolation /// /// Gets a value indicating whether the algorithm supports differentiation (interpolated derivative). /// - /// - /// bool IInterpolation.SupportsDifferentiation { get { return true; } @@ -80,7 +78,6 @@ namespace MathNet.Numerics.Interpolation /// /// Gets a value indicating whether the algorithm supports integration (interpolated quadrature). /// - /// bool IInterpolation.SupportsIntegration { get { return true; } @@ -169,34 +166,38 @@ namespace MathNet.Numerics.Interpolation /// /// Point t to interpolate at. /// Interpolated first derivative at point t. - /// - /// public double Differentiate(double t) { return _spline.Differentiate(t); } /// - /// Interpolate, differentiate and 2nd differentiate at point t. + /// Differentiate twice at point t. /// /// Point t to interpolate at. - /// Interpolated first derivative at point t. - /// - /// - public Tuple DifferentiateAll(double t) + /// Interpolated second derivative at point t. + public double Differentiate2(double t) { - return _spline.DifferentiateAll(t); + return _spline.Differentiate2(t); } /// - /// Integrate up to point t. + /// Indefinite integral at point t. /// - /// Right bound of the integration interval [a,t]. - /// Interpolated definite integral over the interval [a,t]. - /// + /// Point t to integrate at. public double Integrate(double t) { return _spline.Integrate(t); } + + /// + /// Definite integral between points a and b. + /// + /// Left bound of the integration interval [a,b]. + /// Right bound of the integration interval [a,b]. + public double Integrate(double a, double b) + { + return _spline.Integrate(a, b); + } } } diff --git a/src/Numerics/Interpolation/CubicSplineInterpolation.cs b/src/Numerics/Interpolation/CubicSplineInterpolation.cs index 725073ed..220d748f 100644 --- a/src/Numerics/Interpolation/CubicSplineInterpolation.cs +++ b/src/Numerics/Interpolation/CubicSplineInterpolation.cs @@ -4,7 +4,7 @@ // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com // -// Copyright (c) 2009-2010 Math.NET +// Copyright (c) 2009-2013 Math.NET // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation @@ -91,8 +91,6 @@ namespace MathNet.Numerics.Interpolation /// /// Gets a value indicating whether the algorithm supports differentiation (interpolated derivative). /// - /// - /// bool IInterpolation.SupportsDifferentiation { get { return true; } @@ -101,7 +99,6 @@ namespace MathNet.Numerics.Interpolation /// /// Gets a value indicating whether the algorithm supports integration (interpolated quadrature). /// - /// bool IInterpolation.SupportsIntegration { get { return true; } @@ -339,34 +336,38 @@ namespace MathNet.Numerics.Interpolation /// /// Point t to interpolate at. /// Interpolated first derivative at point t. - /// - /// public double Differentiate(double t) { return _spline.Differentiate(t); } /// - /// Interpolate, differentiate and 2nd differentiate at point t. + /// Differentiate twice at point t. /// /// Point t to interpolate at. - /// Interpolated first derivative at point t. - /// - /// - public Tuple DifferentiateAll(double t) + /// Interpolated second derivative at point t. + public double Differentiate2(double t) { - return _spline.DifferentiateAll(t); + return _spline.Differentiate2(t); } /// - /// Integrate up to point t. + /// Indefinite integral at point t. /// - /// Right bound of the integration interval [a,t]. - /// Interpolated definite integral over the interval [a,t]. - /// + /// Point t to integrate at. public double Integrate(double t) { return _spline.Integrate(t); } + + /// + /// Definite integral between points a and b. + /// + /// Left bound of the integration interval [a,b]. + /// Right bound of the integration interval [a,b]. + public double Integrate(double a, double b) + { + return _spline.Integrate(a, b); + } } } diff --git a/src/Numerics/Interpolation/EquidistantPolynomialInterpolation.cs b/src/Numerics/Interpolation/EquidistantPolynomialInterpolation.cs index bf3d155f..72ddb638 100644 --- a/src/Numerics/Interpolation/EquidistantPolynomialInterpolation.cs +++ b/src/Numerics/Interpolation/EquidistantPolynomialInterpolation.cs @@ -4,7 +4,7 @@ // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com // -// Copyright (c) 2009-2010 Math.NET +// Copyright (c) 2009-2013 Math.NET // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation @@ -80,8 +80,6 @@ namespace MathNet.Numerics.Interpolation /// /// Gets a value indicating whether the algorithm supports differentiation (interpolated derivative). /// - /// - /// bool IInterpolation.SupportsDifferentiation { get { return false; } @@ -90,7 +88,6 @@ namespace MathNet.Numerics.Interpolation /// /// Gets a value indicating whether the algorithm supports integration (interpolated quadrature). /// - /// bool IInterpolation.SupportsIntegration { get { return false; } @@ -181,34 +178,38 @@ namespace MathNet.Numerics.Interpolation /// /// Point t to interpolate at. /// Interpolated first derivative at point t. - /// - /// double IInterpolation.Differentiate(double t) { throw new NotSupportedException(); } /// - /// Interpolate, differentiate and 2nd differentiate at point t. NOT SUPPORTED. + /// Differentiate twice at point t. NOT SUPPORTED. /// /// Point t to interpolate at. - /// Interpolated first derivative at point t. - /// - /// - Tuple IInterpolation.DifferentiateAll(double t) + /// Interpolated second derivative at point t. + double IInterpolation.Differentiate2(double t) { throw new NotSupportedException(); } /// - /// Integrate up to point t. NOT SUPPORTED. + /// Indefinite integral at point t. NOT SUPPORTED. /// - /// Right bound of the integration interval [a,t]. - /// Interpolated definite integral over the interval [a,t]. - /// + /// Point t to integrate at. double IInterpolation.Integrate(double t) { throw new NotSupportedException(); } + + /// + /// Definite integral between points a and b. NOT SUPPORTED. + /// + /// Left bound of the integration interval [a,b]. + /// Right bound of the integration interval [a,b]. + double IInterpolation.Integrate(double a, double b) + { + throw new NotSupportedException(); + } } } diff --git a/src/Numerics/Interpolation/FloaterHormannRationalInterpolation.cs b/src/Numerics/Interpolation/FloaterHormannRationalInterpolation.cs index 6ae1573e..586e5852 100644 --- a/src/Numerics/Interpolation/FloaterHormannRationalInterpolation.cs +++ b/src/Numerics/Interpolation/FloaterHormannRationalInterpolation.cs @@ -4,7 +4,7 @@ // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com // -// Copyright (c) 2009-2010 Math.NET +// Copyright (c) 2009-2013 Math.NET // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation @@ -83,8 +83,6 @@ namespace MathNet.Numerics.Interpolation /// /// Gets a value indicating whether the algorithm supports differentiation (interpolated derivative). /// - /// - /// bool IInterpolation.SupportsDifferentiation { get { return false; } @@ -93,7 +91,6 @@ namespace MathNet.Numerics.Interpolation /// /// Gets a value indicating whether the algorithm supports integration (interpolated quadrature). /// - /// bool IInterpolation.SupportsIntegration { get { return false; } @@ -250,34 +247,38 @@ namespace MathNet.Numerics.Interpolation /// /// Point t to interpolate at. /// Interpolated first derivative at point t. - /// - /// double IInterpolation.Differentiate(double t) { throw new NotSupportedException(); } /// - /// Interpolate, differentiate and 2nd differentiate at point t. NOT SUPPORTED. + /// Differentiate twice at point t. NOT SUPPORTED. /// /// Point t to interpolate at. - /// Interpolated first derivative at point t. - /// - /// - Tuple IInterpolation.DifferentiateAll(double t) + /// Interpolated second derivative at point t. + double IInterpolation.Differentiate2(double t) { throw new NotSupportedException(); } /// - /// Integrate up to point t. NOT SUPPORTED. + /// Indefinite integral at point t. NOT SUPPORTED. /// - /// Right bound of the integration interval [a,t]. - /// Interpolated definite integral over the interval [a,t]. - /// + /// Point t to integrate at. double IInterpolation.Integrate(double t) { throw new NotSupportedException(); } + + /// + /// Definite integral between points a and b. NOT SUPPORTED. + /// + /// Left bound of the integration interval [a,b]. + /// Right bound of the integration interval [a,b]. + double IInterpolation.Integrate(double a, double b) + { + throw new NotSupportedException(); + } } } diff --git a/src/Numerics/Interpolation/IInterpolation.cs b/src/Numerics/Interpolation/IInterpolation.cs index d36fb344..7c91e6ff 100644 --- a/src/Numerics/Interpolation/IInterpolation.cs +++ b/src/Numerics/Interpolation/IInterpolation.cs @@ -4,7 +4,7 @@ // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com // -// Copyright (c) 2009-2010 Math.NET +// Copyright (c) 2009-2013 Math.NET // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation @@ -28,8 +28,6 @@ // OTHER DEALINGS IN THE SOFTWARE. // -using System; - namespace MathNet.Numerics.Interpolation { /// @@ -40,14 +38,11 @@ namespace MathNet.Numerics.Interpolation /// /// Gets a value indicating whether the algorithm supports differentiation (interpolated derivative). /// - /// - /// bool SupportsDifferentiation { get; } /// /// Gets a value indicating whether the algorithm supports integration (interpolated quadrature). /// - /// bool SupportsIntegration { get; } /// @@ -62,25 +57,26 @@ namespace MathNet.Numerics.Interpolation /// /// Point t to interpolate at. /// Interpolated first derivative at point t. - /// - /// double Differentiate(double t); /// - /// Interpolate, differentiate and 2nd differentiate at point t. + /// Differentiate twice at point t. /// /// Point t to interpolate at. - /// Interpolated first derivative at point t. - /// - /// - Tuple DifferentiateAll(double t); + /// Interpolated second derivative at point t. + double Differentiate2(double t); /// - /// Integrate up to point t. + /// Indefinite integral at point t. /// - /// Right bound of the integration interval [a,t]. - /// Interpolated definite integral over the interval [a,t]. - /// + /// Point t to integrate at. double Integrate(double t); + + /// + /// Definite integral between points a and b. + /// + /// Left bound of the integration interval [a,b]. + /// Right bound of the integration interval [a,b]. + double Integrate(double a, double b); } } diff --git a/src/Numerics/Interpolation/LinearSpline.cs b/src/Numerics/Interpolation/LinearSpline.cs index 8996429f..307252ba 100644 --- a/src/Numerics/Interpolation/LinearSpline.cs +++ b/src/Numerics/Interpolation/LinearSpline.cs @@ -44,6 +44,7 @@ namespace MathNet.Numerics.Interpolation readonly double[] _x; readonly double[] _c0; readonly double[] _c1; + readonly Lazy _indefiniteIntegral; /// Sample points (N+1), sorted ascending /// Sample values (N or N+1) at the corresponding points; intercept, zero order coefficients @@ -53,6 +54,7 @@ namespace MathNet.Numerics.Interpolation _x = x; _c0 = c0; _c1 = c1; + _indefiniteIntegral = new Lazy(ComputeIndefiniteIntegral); } /// @@ -132,27 +134,35 @@ namespace MathNet.Numerics.Interpolation } /// - /// Integrate up to point t. + /// Indefinite integral at point t. /// - /// Right bound of the integration interval [a,t]. - /// Interpolated definite integral over the interval [a,t]. + /// Point t to integrate at. public double Integrate(double t) { int k = LeftBracketIndex(t); var x = (t - _x[k]); - return x*(_c0[k] + x*0.5*_c1[k]); + return _indefiniteIntegral.Value[k] + x*(_c0[k] + x*_c1[k]/2); } /// - /// Interpolate, differentiate and 2nd differentiate at point t. + /// Definite integral between points a and b. /// - /// Point t to interpolate at. - /// Interpolated first derivative at point t. - public Tuple DifferentiateAll(double t) + /// Left bound of the integration interval [a,b]. + /// Right bound of the integration interval [a,b]. + public double Integrate(double a, double b) { - int k = LeftBracketIndex(t); - var x = (t - _x[k]); - return new Tuple(_c0[k] + x*_c1[k], _c1[k], 0d); + return Integrate(b) - Integrate(a); + } + + double[] ComputeIndefiniteIntegral() + { + var integral = new double[_c1.Length]; + for (int i = 0; i < integral.Length - 1; i++) + { + double w = _x[i + 1] - _x[i]; + integral[i + 1] = integral[i] + w*(_c0[i] + w*_c1[i]/2); + } + return integral; } /// diff --git a/src/Numerics/Interpolation/NevillePolynomialInterpolation.cs b/src/Numerics/Interpolation/NevillePolynomialInterpolation.cs index c63ed4ca..d2ee878e 100644 --- a/src/Numerics/Interpolation/NevillePolynomialInterpolation.cs +++ b/src/Numerics/Interpolation/NevillePolynomialInterpolation.cs @@ -4,7 +4,7 @@ // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com // -// Copyright (c) 2009-2010 Math.NET +// Copyright (c) 2009-2013 Math.NET // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation @@ -79,8 +79,6 @@ namespace MathNet.Numerics.Interpolation /// /// Gets a value indicating whether the algorithm supports differentiation (interpolated derivative). /// - /// - /// bool IInterpolation.SupportsDifferentiation { get { return true; } @@ -89,7 +87,6 @@ namespace MathNet.Numerics.Interpolation /// /// Gets a value indicating whether the algorithm supports integration (interpolated quadrature). /// - /// bool IInterpolation.SupportsIntegration { get { return false; } @@ -159,8 +156,6 @@ namespace MathNet.Numerics.Interpolation /// /// Point t to interpolate at. /// Interpolated first derivative at point t. - /// - /// public double Differentiate(double t) { var x = new double[_values.Count]; @@ -183,13 +178,11 @@ namespace MathNet.Numerics.Interpolation } /// - /// Interpolate, differentiate and 2nd differentiate at point t. + /// Differentiate twice at point t. /// /// Point t to interpolate at. - /// Interpolated first derivative at point t. - /// - /// - public Tuple DifferentiateAll(double t) + /// Interpolated second derivative at point t. + public double Differentiate2(double t) { var x = new double[_values.Count]; var dx = new double[_values.Count]; @@ -203,24 +196,32 @@ namespace MathNet.Numerics.Interpolation double hp = t - _points[i + level]; double ho = _points[i] - t; double den = _points[i] - _points[i + level]; - ddx[i] = ((hp * ddx[i]) + (ho * ddx[i + 1]) + (2 * dx[i]) - (2 * dx[i + 1])) / den; - dx[i] = ((hp * dx[i]) + x[i] + (ho * dx[i + 1]) - x[i + 1]) / den; - x[i] = ((hp * x[i]) + (ho * x[i + 1])) / den; + ddx[i] = ((hp*ddx[i]) + (ho*ddx[i + 1]) + (2*dx[i]) - (2*dx[i + 1]))/den; + dx[i] = ((hp*dx[i]) + x[i] + (ho*dx[i + 1]) - x[i + 1])/den; + x[i] = ((hp*x[i]) + (ho*x[i + 1]))/den; } } - return new Tuple(x[0], dx[0], ddx[0]); + return ddx[0]; } /// - /// Integrate up to point t. NOT SUPPORTED. + /// Indefinite integral at point t. NOT SUPPORTED. /// - /// Right bound of the integration interval [a,t]. - /// Interpolated definite integral over the interval [a,t]. - /// + /// Point t to integrate at. double IInterpolation.Integrate(double t) { throw new NotSupportedException(); } + + /// + /// Definite integral between points a and b. NOT SUPPORTED. + /// + /// Left bound of the integration interval [a,b]. + /// Right bound of the integration interval [a,b]. + double IInterpolation.Integrate(double a, double b) + { + throw new NotSupportedException(); + } } } diff --git a/src/Numerics/Interpolation/SplineInterpolation.cs b/src/Numerics/Interpolation/SplineInterpolation.cs index cca27bbd..537d1117 100644 --- a/src/Numerics/Interpolation/SplineInterpolation.cs +++ b/src/Numerics/Interpolation/SplineInterpolation.cs @@ -4,7 +4,7 @@ // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com // -// Copyright (c) 2009-2010 Math.NET +// Copyright (c) 2009-2013 Math.NET // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation @@ -77,8 +77,6 @@ namespace MathNet.Numerics.Interpolation /// /// Gets a value indicating whether the algorithm supports differentiation (interpolated derivative). /// - /// - /// bool IInterpolation.SupportsDifferentiation { get { return true; } @@ -87,7 +85,6 @@ namespace MathNet.Numerics.Interpolation /// /// Gets a value indicating whether the algorithm supports integration (interpolated quadrature). /// - /// bool IInterpolation.SupportsIntegration { get { return true; } @@ -136,16 +133,16 @@ namespace MathNet.Numerics.Interpolation /// Interpolated value x(t). public double Interpolate(double t) { - int closestLeftIndex = IndexOfClosestPointLeftOf(t); + int closestLeftIndex = LeftBracketIndex(t); // Interpolation double offset = t - _points[closestLeftIndex]; int k = closestLeftIndex << 2; return _coefficients[k] - + (offset*(_coefficients[k + 1] - + (offset*(_coefficients[k + 2] - + (offset*_coefficients[k + 3]))))); + + (offset*(_coefficients[k + 1] + + (offset*(_coefficients[k + 2] + + (offset*_coefficients[k + 3]))))); } /// @@ -153,49 +150,38 @@ namespace MathNet.Numerics.Interpolation /// /// Point t to interpolate at. /// Interpolated first derivative at point t. - /// - /// public double Differentiate(double t) { - int closestLeftIndex = IndexOfClosestPointLeftOf(t); - - // Differentiation + int closestLeftIndex = LeftBracketIndex(t); double offset = t - _points[closestLeftIndex]; int k = closestLeftIndex << 2; return _coefficients[k + 1] - + (2*offset*_coefficients[k + 2]) - + (3*offset*offset*_coefficients[k + 3]); + + (2*offset*_coefficients[k + 2]) + + (3*offset*offset*_coefficients[k + 3]); } /// - /// Interpolate, differentiate and 2nd differentiate at point t. + /// Differentiate twice at point t. /// /// Point t to interpolate at. - /// Interpolated first derivative at point t. - /// - /// - public Tuple DifferentiateAll(double t) + /// Interpolated second derivative at point t. + public double Differentiate2(double t) { - int closestLeftIndex = IndexOfClosestPointLeftOf(t); + int closestLeftIndex = LeftBracketIndex(t); double offset = t - _points[closestLeftIndex]; int k = closestLeftIndex << 2; - return new Tuple( - _coefficients[k] + (offset*(_coefficients[k + 1] + (offset*(_coefficients[k + 2] + (offset*_coefficients[k + 3]))))), - _coefficients[k + 1] + (2*offset*_coefficients[k + 2]) + (3*offset*offset*_coefficients[k + 3]), - (2*_coefficients[k + 2]) + (6*offset*_coefficients[k + 3])); + return (2*_coefficients[k + 2]) + (6*offset*_coefficients[k + 3]); } /// - /// Integrate up to point t. + /// Indefinite integral at point t. /// - /// Right bound of the integration interval [a,t]. - /// Interpolated definite integral over the interval [a,t]. - /// + /// Point t to integrate at. public double Integrate(double t) { - int closestLeftIndex = IndexOfClosestPointLeftOf(t); + int closestLeftIndex = LeftBracketIndex(t); // Integration double result = 0; @@ -203,18 +189,28 @@ namespace MathNet.Numerics.Interpolation { double w = _points[i + 1] - _points[i]; result += w*(_coefficients[j] - + ((w*_coefficients[j + 1]*0.5) - + (w*((_coefficients[j + 2]/3) - + (w*_coefficients[j + 3]*0.25))))); + + ((w*_coefficients[j + 1]*0.5) + + (w*((_coefficients[j + 2]/3) + + (w*_coefficients[j + 3]*0.25))))); } double offset = t - _points[closestLeftIndex]; int k = closestLeftIndex << 2; return result + (offset*(_coefficients[k] - + (offset*_coefficients[k + 1]*0.5) - + (offset*_coefficients[k + 2]/3) - + (offset*_coefficients[k + 3]*0.25))); + + (offset*_coefficients[k + 1]*0.5) + + (offset*_coefficients[k + 2]/3) + + (offset*_coefficients[k + 3]*0.25))); + } + + /// + /// Definite integral between points a and b. + /// + /// Left bound of the integration interval [a,b]. + /// Right bound of the integration interval [a,b]. + public double Integrate(double a, double b) + { + return Integrate(b) - Integrate(a); } /// @@ -222,7 +218,7 @@ namespace MathNet.Numerics.Interpolation /// /// The value to look for. /// The sample point index. - int IndexOfClosestPointLeftOf(double t) + int LeftBracketIndex(double t) { // Binary search in the [ t[0], ..., t[n-2] ] (t[n-1] is not included) int low = 0; diff --git a/src/UnitTests/InterpolationTests/AkimaSplineTest.cs b/src/UnitTests/InterpolationTests/AkimaSplineTest.cs index 9f8c4d1e..acc97113 100644 --- a/src/UnitTests/InterpolationTests/AkimaSplineTest.cs +++ b/src/UnitTests/InterpolationTests/AkimaSplineTest.cs @@ -28,11 +28,11 @@ // OTHER DEALINGS IN THE SOFTWARE. // +using MathNet.Numerics.Interpolation; +using NUnit.Framework; + namespace MathNet.Numerics.UnitTests.InterpolationTests { - using Interpolation; - using NUnit.Framework; - /// /// AkimaSpline test case. /// @@ -42,12 +42,12 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests /// /// Sample points. /// - private readonly double[] _t = new[] { -2.0, -1.0, 0.0, 1.0, 2.0 }; + readonly double[] _t = { -2.0, -1.0, 0.0, 1.0, 2.0 }; /// /// Sample values. /// - private readonly double[] _x = new[] { 1.0, 2.0, -1.0, 0.0, 1.0 }; + readonly double[] _x = { 1.0, 2.0, -1.0, 0.0, 1.0 }; /// /// Verifies that the interpolation matches the given value at all the provided sample points. @@ -60,9 +60,6 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests for (int i = 0; i < _x.Length; i++) { Assert.AreEqual(_x[i], interpolation.Interpolate(_t[i]), "A Exact Point " + i); - - var actual = interpolation.DifferentiateAll(_t[i]); - Assert.AreEqual(_x[i], actual.Item1, "B Exact Point " + i); } } @@ -87,9 +84,6 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests // TODO: Verify the expected values (that they are really the expected ones) Assert.AreEqual(x, interpolation.Interpolate(t), maxAbsoluteError, "Interpolation at {0}", t); - - var actual = interpolation.DifferentiateAll(t); - Assert.AreEqual(x, actual.Item1, maxAbsoluteError, "Interpolation as by-product of differentiation at {0}", t); } /// diff --git a/src/UnitTests/InterpolationTests/BulirschStoerRationalTest.cs b/src/UnitTests/InterpolationTests/BulirschStoerRationalTest.cs index 05ae54e4..3db86cd6 100644 --- a/src/UnitTests/InterpolationTests/BulirschStoerRationalTest.cs +++ b/src/UnitTests/InterpolationTests/BulirschStoerRationalTest.cs @@ -4,7 +4,7 @@ // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com // -// Copyright (c) 2002-2011 Math.NET +// Copyright (c) 2002-2013 Math.NET // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation @@ -28,11 +28,11 @@ // OTHER DEALINGS IN THE SOFTWARE. // +using MathNet.Numerics.Interpolation; +using NUnit.Framework; + namespace MathNet.Numerics.UnitTests.InterpolationTests { - using Interpolation; - using NUnit.Framework; - /// /// BulirschStoerRational test case. /// @@ -42,12 +42,12 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests /// /// Sample points. /// - private readonly double[] _t = new[] { 0d, 1, 3, 4, 5 }; + readonly double[] _t = { 0d, 1, 3, 4, 5 }; /// /// Sample values. /// - private readonly double[] _x = new[] { 0d, 3, 1000, -1000, 3 }; + readonly double[] _x = { 0d, 3, 1000, -1000, 3 }; /// /// Verifies that the interpolation matches the given value at all the provided sample points. diff --git a/src/UnitTests/InterpolationTests/CubicSplineTest.cs b/src/UnitTests/InterpolationTests/CubicSplineTest.cs index 64dd0199..4e1c27af 100644 --- a/src/UnitTests/InterpolationTests/CubicSplineTest.cs +++ b/src/UnitTests/InterpolationTests/CubicSplineTest.cs @@ -28,12 +28,11 @@ // OTHER DEALINGS IN THE SOFTWARE. // +using MathNet.Numerics.Interpolation; +using NUnit.Framework; + namespace MathNet.Numerics.UnitTests.InterpolationTests { - using System; - using Interpolation; - using NUnit.Framework; - /// /// CubicSpline Test case. /// @@ -43,12 +42,12 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests /// /// Sample points. /// - private readonly double[] _t = new[] { -2.0, -1.0, 0.0, 1.0, 2.0 }; + readonly double[] _t = { -2.0, -1.0, 0.0, 1.0, 2.0 }; /// /// Sample values. /// - private readonly double[] _x = new[] { 1.0, 2.0, -1.0, 0.0, 1.0 }; + readonly double[] _x = { 1.0, 2.0, -1.0, 0.0, 1.0 }; /// /// Verifies that the interpolation matches the given value at all the provided sample points. @@ -61,9 +60,6 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests for (int i = 0; i < _x.Length; i++) { Assert.AreEqual(_x[i], interpolation.Interpolate(_t[i]), "A Exact Point " + i); - - var actual = interpolation.DifferentiateAll(_t[i]); - Assert.AreEqual(_x[i], actual.Item1, "B Exact Point " + i); } } @@ -92,9 +88,6 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests IInterpolation interpolation = new CubicSplineInterpolation(_t, _x); Assert.AreEqual(x, interpolation.Interpolate(t), maxAbsoluteError, "Interpolation at {0}", t); - - var actual = interpolation.DifferentiateAll(t); - Assert.AreEqual(x, actual.Item1, maxAbsoluteError, "Interpolation as by-product of differentiation at {0}", t); } /// @@ -108,9 +101,6 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests for (int i = 0; i < _x.Length; i++) { Assert.AreEqual(_x[i], interpolation.Interpolate(_t[i]), "A Exact Point " + i); - - var actual = interpolation.DifferentiateAll(_t[i]); - Assert.AreEqual(_x[i], actual.Item1, "B Exact Point " + i); } } @@ -139,9 +129,6 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests IInterpolation interpolation = new CubicSplineInterpolation(_t, _x, SplineBoundaryCondition.FirstDerivative, 1.0, SplineBoundaryCondition.FirstDerivative, -1.0); Assert.AreEqual(x, interpolation.Interpolate(t), maxAbsoluteError, "Interpolation at {0}", t); - - var actual = interpolation.DifferentiateAll(t); - Assert.AreEqual(x, actual.Item1, maxAbsoluteError, "Interpolation as by-product of differentiation at {0}", t); } /// @@ -155,9 +142,6 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests for (int i = 0; i < _x.Length; i++) { Assert.AreEqual(_x[i], interpolation.Interpolate(_t[i]), "A Exact Point " + i); - - var actual = interpolation.DifferentiateAll(_t[i]); - Assert.AreEqual(_x[i], actual.Item1, "B Exact Point " + i); } } @@ -186,9 +170,6 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests IInterpolation interpolation = new CubicSplineInterpolation(_t, _x, SplineBoundaryCondition.SecondDerivative, -5.0, SplineBoundaryCondition.SecondDerivative, -1.0); Assert.AreEqual(x, interpolation.Interpolate(t), maxAbsoluteError, "Interpolation at {0}", t); - - var actual = interpolation.DifferentiateAll(t); - Assert.AreEqual(x, actual.Item1, maxAbsoluteError, "Interpolation as by-product of differentiation at {0}", t); } /// @@ -208,17 +189,5 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests Assert.AreEqual(ytest[i], interpolation.Interpolate(xtest[i]), 1e-15, "Linear with {0} samples, sample {1}", samples, i); } } - - /// - /// Verifies that sample points are required to be sorted in strictly monotonically ascending order. - /// - [Test] - [ExpectedException(typeof(ArgumentException))] - public void Constructor_SamplePointsNotStrictlyAscending_Throws() - { - var x = new[] { -1.0, 0.0, 1.5, 1.5, 2.5, 4.0 }; - var y = new[] { 1.0, 0.3, -0.7, -0.6, -0.1, 0.4 }; - var interpolation = new CubicSplineInterpolation(x, y); - } } } diff --git a/src/UnitTests/InterpolationTests/EquidistantPolynomialTest.cs b/src/UnitTests/InterpolationTests/EquidistantPolynomialTest.cs index 1a1c470f..e7c4d618 100644 --- a/src/UnitTests/InterpolationTests/EquidistantPolynomialTest.cs +++ b/src/UnitTests/InterpolationTests/EquidistantPolynomialTest.cs @@ -4,7 +4,7 @@ // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com // -// Copyright (c) 2002-2011 Math.NET +// Copyright (c) 2002-2013 Math.NET // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation @@ -28,11 +28,11 @@ // OTHER DEALINGS IN THE SOFTWARE. // +using MathNet.Numerics.Interpolation; +using NUnit.Framework; + namespace MathNet.Numerics.UnitTests.InterpolationTests { - using Interpolation; - using NUnit.Framework; - /// /// EquidistantPolynomial Test case. /// @@ -42,17 +42,17 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests /// /// Left bound; /// - private const double Tmin = 0.0; + const double Tmin = 0.0; /// /// Right bound. /// - private const double Tmax = 4.0; + const double Tmax = 4.0; /// /// Sample values. /// - private readonly double[] _x = new[] { 0.0, 3.0, 2.5, 1.0, 3.0 }; + readonly double[] _x = { 0.0, 3.0, 2.5, 1.0, 3.0 }; /// /// Verifies that the interpolation matches the given value at all the provided sample points. diff --git a/src/UnitTests/InterpolationTests/FloaterHormannRationalTest.cs b/src/UnitTests/InterpolationTests/FloaterHormannRationalTest.cs index 987b7ce6..37f7be20 100644 --- a/src/UnitTests/InterpolationTests/FloaterHormannRationalTest.cs +++ b/src/UnitTests/InterpolationTests/FloaterHormannRationalTest.cs @@ -4,7 +4,7 @@ // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com // -// Copyright (c) 2002-2011 Math.NET +// Copyright (c) 2002-2013 Math.NET // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation @@ -28,11 +28,11 @@ // OTHER DEALINGS IN THE SOFTWARE. // +using MathNet.Numerics.Interpolation; +using NUnit.Framework; + namespace MathNet.Numerics.UnitTests.InterpolationTests { - using Interpolation; - using NUnit.Framework; - /// /// FloaterHormannRational test case. /// @@ -42,12 +42,12 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests /// /// Sample points. /// - private readonly double[] _t = new[] { -2.0, -1.0, 0.0, 1.0, 2.0 }; + readonly double[] _t = { -2.0, -1.0, 0.0, 1.0, 2.0 }; /// /// Sample values. /// - private readonly double[] _x = new[] { 1.0, 2.0, -1.0, 0.0, 1.0 }; + readonly double[] _x = { 1.0, 2.0, -1.0, 0.0, 1.0 }; /// /// Verifies that the interpolation matches the given value at all the provided polynomial sample points. @@ -99,12 +99,12 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests var t = new double[40]; var x = new double[40]; - const double Step = 10.0 / 39.0; + const double Step = 10.0/39.0; for (int i = 0; i < t.Length; i++) { - double tt = -5 + (i * Step); + double tt = -5 + (i*Step); t[i] = tt; - x[i] = 1.0 / (1.0 + (tt * tt)); + x[i] = 1.0/(1.0 + (tt*tt)); } IInterpolation interpolation = new FloaterHormannRationalInterpolation(t, x); diff --git a/src/UnitTests/InterpolationTests/LinearInterpolationCase.cs b/src/UnitTests/InterpolationTests/LinearInterpolationCase.cs index c6301b10..4e989c90 100644 --- a/src/UnitTests/InterpolationTests/LinearInterpolationCase.cs +++ b/src/UnitTests/InterpolationTests/LinearInterpolationCase.cs @@ -4,7 +4,7 @@ // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com // -// Copyright (c) 2002-2011 Math.NET +// Copyright (c) 2002-2013 Math.NET // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation @@ -60,7 +60,7 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests for (int i = 0; i < x.Length; i++) { x[i] = i + sampleOffset; - y[i] = (x[i] * slope) + intercept; + y[i] = (x[i]*slope) + intercept; } // build linear test vectors randomly between the sample points @@ -71,14 +71,14 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests // y = const xtest[0] = sampleOffset - uniform.Sample(); xtest[1] = sampleOffset + uniform.Sample(); - ytest[0] = ytest[1] = (sampleOffset * slope) + intercept; + ytest[0] = ytest[1] = (sampleOffset*slope) + intercept; } else { for (int i = 0; i < xtest.Length; i++) { xtest[i] = (i - 1) + sampleOffset + uniform.Sample(); - ytest[i] = (xtest[i] * slope) + intercept; + ytest[i] = (xtest[i]*slope) + intercept; } } } diff --git a/src/UnitTests/InterpolationTests/LinearSplineTest.cs b/src/UnitTests/InterpolationTests/LinearSplineTest.cs index fea52514..44e07b20 100644 --- a/src/UnitTests/InterpolationTests/LinearSplineTest.cs +++ b/src/UnitTests/InterpolationTests/LinearSplineTest.cs @@ -33,21 +33,46 @@ using NUnit.Framework; namespace MathNet.Numerics.UnitTests.InterpolationTests { - /// - /// LinearSpline test case - /// [TestFixture, Category("Interpolation")] public class LinearSplineTest { - /// - /// Sample points. - /// readonly double[] _t = { -2.0, -1.0, 0.0, 1.0, 2.0 }; + readonly double[] _y = { 1.0, 2.0, -1.0, 0.0, 1.0 }; - /// - /// Sample values. - /// - readonly double[] _x = { 1.0, 2.0, -1.0, 0.0, 1.0 }; + [Test] + public void FirstDerivative() + { + IInterpolation ip = LinearSpline.Interpolate(_t, _y); + Assert.That(ip.Differentiate(-3.0), Is.EqualTo(1.0)); + Assert.That(ip.Differentiate(-2.0), Is.EqualTo(1.0)); + Assert.That(ip.Differentiate(-1.5), Is.EqualTo(1.0)); + Assert.That(ip.Differentiate(-1.0), Is.EqualTo(-3.0)); + Assert.That(ip.Differentiate(-0.5), Is.EqualTo(-3.0)); + Assert.That(ip.Differentiate(0.0), Is.EqualTo(1.0)); + Assert.That(ip.Differentiate(0.5), Is.EqualTo(1.0)); + Assert.That(ip.Differentiate(1.0), Is.EqualTo(1.0)); + Assert.That(ip.Differentiate(2.0), Is.EqualTo(1.0)); + Assert.That(ip.Differentiate(3.0), Is.EqualTo(1.0)); + } + + [Test] + public void DefiniteIntegral() + { + IInterpolation ip = LinearSpline.Interpolate(_t, _y); + Assert.That(ip.Integrate(-4.0, -3.0), Is.EqualTo(-0.5)); + Assert.That(ip.Integrate(-3.0, -2.0), Is.EqualTo(0.5)); + Assert.That(ip.Integrate(-2.0, -1.0), Is.EqualTo(1.5)); + Assert.That(ip.Integrate(-1.0, 0.0), Is.EqualTo(0.5)); + Assert.That(ip.Integrate(0.0, 1.0), Is.EqualTo(-0.5)); + Assert.That(ip.Integrate(1.0, 2.0), Is.EqualTo(0.5)); + Assert.That(ip.Integrate(2.0, 3.0), Is.EqualTo(1.5)); + Assert.That(ip.Integrate(3.0, 4.0), Is.EqualTo(2.5)); + Assert.That(ip.Integrate(0.0, 4.0), Is.EqualTo(4.0)); + Assert.That(ip.Integrate(-3.0, -1.0), Is.EqualTo(2.0)); + Assert.That(ip.Integrate(-3.0, 4.0), Is.EqualTo(6.5)); + Assert.That(ip.Integrate(0.5, 1.5), Is.EqualTo(0.0)); + Assert.That(ip.Integrate(-2.5, -1.0), Is.EqualTo(1.875)); + } /// /// Verifies that the interpolation matches the given value at all the provided sample points. @@ -55,14 +80,10 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests [Test] public void FitsAtSamplePoints() { - IInterpolation interpolation = LinearSpline.Interpolate(_t, _x); - - for (int i = 0; i < _x.Length; i++) + IInterpolation ip = LinearSpline.Interpolate(_t, _y); + for (int i = 0; i < _y.Length; i++) { - Assert.AreEqual(_x[i], interpolation.Interpolate(_t[i]), "A Exact Point " + i); - - var actual = interpolation.DifferentiateAll(_t[i]); - Assert.AreEqual(_x[i], actual.Item1, "B Exact Point " + i); + Assert.AreEqual(_y[i], ip.Interpolate(_t[i]), "A Exact Point " + i); } } @@ -86,14 +107,10 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests [TestCase(1.2, .2, 1e-15)] [TestCase(10.0, 9.0, 1e-15)] [TestCase(-10.0, -7.0, 1e-15)] - public void FitsAtArbitraryPointsWithMaple(double t, double x, double maxAbsoluteError) + public void FitsAtArbitraryPoints(double t, double x, double maxAbsoluteError) { - IInterpolation interpolation = LinearSpline.Interpolate(_t, _x); - - Assert.AreEqual(x, interpolation.Interpolate(t), maxAbsoluteError, "Interpolation at {0}", t); - - var actual = interpolation.DifferentiateAll(t); - Assert.AreEqual(x, actual.Item1, maxAbsoluteError, "Interpolation as by-product of differentiation at {0}", t); + IInterpolation ip = LinearSpline.Interpolate(_t, _y); + Assert.AreEqual(x, ip.Interpolate(t), maxAbsoluteError, "Interpolation at {0}", t); } /// @@ -107,10 +124,11 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests { double[] x, y, xtest, ytest; LinearInterpolationCase.Build(out x, out y, out xtest, out ytest, samples); - IInterpolation interpolation = LinearSpline.Interpolate(x, y); + + IInterpolation ip = LinearSpline.Interpolate(x, y); for (int i = 0; i < xtest.Length; i++) { - Assert.AreEqual(ytest[i], interpolation.Interpolate(xtest[i]), 1e-15, "Linear with {0} samples, sample {1}", samples, i); + Assert.AreEqual(ytest[i], ip.Interpolate(xtest[i]), 1e-15, "Linear with {0} samples, sample {1}", samples, i); } } } diff --git a/src/UnitTests/InterpolationTests/NevillePolynomialTest.cs b/src/UnitTests/InterpolationTests/NevillePolynomialTest.cs index b6cfa7e9..2f7d19e3 100644 --- a/src/UnitTests/InterpolationTests/NevillePolynomialTest.cs +++ b/src/UnitTests/InterpolationTests/NevillePolynomialTest.cs @@ -4,7 +4,7 @@ // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com // -// Copyright (c) 2002-2011 Math.NET +// Copyright (c) 2002-2013 Math.NET // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation @@ -28,15 +28,15 @@ // OTHER DEALINGS IN THE SOFTWARE. // +using System; +using System.Globalization; +using System.IO; +using System.Linq; +using MathNet.Numerics.Interpolation; +using NUnit.Framework; + namespace MathNet.Numerics.UnitTests.InterpolationTests { - using System; - using System.Globalization; - using System.IO; - using System.Linq; - using Interpolation; - using NUnit.Framework; - /// /// NevillePolynomial test case. /// @@ -46,12 +46,12 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests /// /// Sample points. /// - private readonly double[] _t = new[] { 0.0, 1.0, 3.0, 4.0 }; + readonly double[] _t = { 0.0, 1.0, 3.0, 4.0 }; /// /// Sample values. /// - private readonly double[] _x = new[] { 0.0, 3.0, 1.0, 3.0 }; + readonly double[] _x = { 0.0, 3.0, 1.0, 3.0 }; /// /// Verifies that the interpolation matches the given value at all the provided sample points. @@ -64,9 +64,6 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests for (int i = 0; i < _x.Length; i++) { Assert.AreEqual(_x[i], interpolation.Interpolate(_t[i]), "A Exact Point " + i); - - var actual = interpolation.DifferentiateAll(_t[i]); - Assert.AreEqual(_x[i], actual.Item1, "B Exact Point " + i); } } @@ -93,9 +90,6 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests IInterpolation interpolation = new NevillePolynomialInterpolation(_t, _x); Assert.AreEqual(x, interpolation.Interpolate(t), maxAbsoluteError, "Interpolation at {0}", t); - - var actual = interpolation.DifferentiateAll(t); - Assert.AreEqual(x, actual.Item1, maxAbsoluteError, "Interpolation as by-product of differentiation at {0}", t); } /// @@ -120,7 +114,7 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests /// Verifies that all sample points must be unique. /// [Test] - [ExpectedException(typeof(ArgumentException))] + [ExpectedException(typeof (ArgumentException))] public void Constructor_SamplePointsNotUnique_Throws() { var x = new[] { -1.0, 0.0, 1.5, 1.5, 2.5, 4.0 };