|
| 1 | +// <copyright file="MakimaSplineTest.cs" company="Math.NET"> |
| 2 | +// Math.NET Numerics, part of the Math.NET Project |
| 3 | +// https://numerics.mathdotnet.com |
| 4 | +// https://github.com/mathnet/mathnet-numerics |
| 5 | +// |
| 6 | +// Copyright (c) 2009-$CURRENT_YEAR$ Math.NET |
| 7 | +// |
| 8 | +// Permission is hereby granted, free of charge, to any person |
| 9 | +// obtaining a copy of this software and associated documentation |
| 10 | +// files (the "Software"), to deal in the Software without |
| 11 | +// restriction, including without limitation the rights to use, |
| 12 | +// copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | +// copies of the Software, and to permit persons to whom the |
| 14 | +// Software is furnished to do so, subject to the following |
| 15 | +// conditions: |
| 16 | +// |
| 17 | +// The above copyright notice and this permission notice shall be |
| 18 | +// included in all copies or substantial portions of the Software. |
| 19 | +// |
| 20 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 21 | +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 22 | +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 23 | +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 24 | +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 25 | +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 26 | +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 27 | +// OTHER DEALINGS IN THE SOFTWARE. |
| 28 | +// </copyright> |
| 29 | + |
| 30 | +using MathNet.Numerics.Interpolation; |
| 31 | +using NUnit.Framework; |
| 32 | + |
| 33 | +namespace MathNet.Numerics.Tests.InterpolationTests |
| 34 | +{ |
| 35 | + [TestFixture, Category("Interpolation")] |
| 36 | + public class MakimaSplineTest |
| 37 | + { |
| 38 | + // Test sample data |
| 39 | + readonly double[] _t = { -2.0, -1.0, 0.0, 1.0, 2.0 }; |
| 40 | + readonly double[] _y = { 1.0, 2.0, -1.0, 0.0, 1.0 }; |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Verifies that the Makima interpolation exactly fits the sample points. |
| 44 | + /// </summary> |
| 45 | + [Test] |
| 46 | + public void FitsAtSamplePoints() |
| 47 | + { |
| 48 | + IInterpolation it = CubicSpline.InterpolateMakima(_t, _y); |
| 49 | + for (var i = 0; i < _y.Length; i++) |
| 50 | + { |
| 51 | + Assert.AreEqual(_y[i], it.Interpolate(_t[i]), 1e-15, "Exact point " + i); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Verifies that at arbitrary points, the interpolation matches the reference values. |
| 57 | + /// (Note: Replace the expected values with verified reference values.) |
| 58 | + /// </summary> |
| 59 | + /// <param name="t">The x-value to interpolate.</param> |
| 60 | + /// <param name="expected">The expected interpolated value.</param> |
| 61 | + /// <param name="maxAbsoluteError">Maximum allowed absolute error.</param> |
| 62 | + [TestCase(-2.4, 0.14266666666666683, 1e-10)] |
| 63 | + [TestCase(-0.9, 1.805, 1e-10)] |
| 64 | + [TestCase(-0.5, 0.29166666666666674, 1e-10)] |
| 65 | + [TestCase(-0.1, -0.955, 1e-10)] |
| 66 | + [TestCase(0.1, -0.954, 1e-10)] |
| 67 | + [TestCase(0.4, -0.696, 1e-10)] |
| 68 | + [TestCase(1.2, 0.2, 1e-10)] |
| 69 | + [TestCase(10.0, 9.0, 1e-10)] |
| 70 | + [TestCase(-10.0, 527.0, 1e-10)] |
| 71 | + public void FitsAtArbitraryPoints(double t, double expected, double maxAbsoluteError) |
| 72 | + { |
| 73 | + IInterpolation it = CubicSpline.InterpolateMakima(_t, _y); |
| 74 | + Assert.AreEqual(expected, it.Interpolate(t), maxAbsoluteError, "Interpolation at {0}", t); |
| 75 | + } |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// Verifies that Makima interpolation handles linear data correctly. |
| 79 | + /// </summary> |
| 80 | + /// <param name="samples">The number of sample points.</param> |
| 81 | + [TestCase(5)] |
| 82 | + [TestCase(7)] |
| 83 | + [TestCase(15)] |
| 84 | + public void SupportsLinearCase(int samples) |
| 85 | + { |
| 86 | + double[] x, y, xtest, ytest; |
| 87 | + // LinearInterpolationCase.Build is a utility to generate linear test data. |
| 88 | + LinearInterpolationCase.Build(out x, out y, out xtest, out ytest, samples); |
| 89 | + IInterpolation it = CubicSpline.InterpolateMakima(x, y); |
| 90 | + for (var i = 0; i < xtest.Length; i++) |
| 91 | + { |
| 92 | + Assert.AreEqual(ytest[i], it.Interpolate(xtest[i]), 1e-15, "Linear case with {0} samples, sample {1}", samples, i); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// Verifies that the interpolation throws an exception when provided with too few samples. |
| 98 | + /// </summary> |
| 99 | + [Test] |
| 100 | + public void FewSamples() |
| 101 | + { |
| 102 | + Assert.That(() => CubicSpline.InterpolateMakima(new double[0], new double[0]), Throws.ArgumentException); |
| 103 | + Assert.That(() => CubicSpline.InterpolateMakima(new double[4], new double[4]), Throws.ArgumentException); |
| 104 | + Assert.That(CubicSpline.InterpolateMakima(new[] { 1.0, 2.0, 3.0, 4.0, 5.0 }, |
| 105 | + new[] { 2.0, 2.0, 2.0, 2.0, 2.0 }) |
| 106 | + .Interpolate(1.0), Is.EqualTo(2.0)); |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments