|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.commons.math4.legacy.analysis.interpolation; |
| 18 | + |
| 19 | +import org.apache.commons.math4.legacy.TestUtils; |
| 20 | +import org.apache.commons.math4.legacy.analysis.UnivariateFunction; |
| 21 | +import org.apache.commons.math4.legacy.analysis.integration.SimpsonIntegrator; |
| 22 | +import org.apache.commons.math4.legacy.analysis.polynomials.PolynomialFunction; |
| 23 | +import org.apache.commons.math4.legacy.analysis.polynomials.PolynomialSplineFunction; |
| 24 | +import org.apache.commons.math4.legacy.exception.DimensionMismatchException; |
| 25 | +import org.apache.commons.math4.legacy.exception.NonMonotonicSequenceException; |
| 26 | +import org.apache.commons.math4.legacy.exception.NumberIsTooSmallException; |
| 27 | +import org.junit.Assert; |
| 28 | +import org.junit.Test; |
| 29 | + |
| 30 | +/** |
| 31 | + * Test the ClampedSplineInterpolator. |
| 32 | + */ |
| 33 | +public class ClampedSplineInterpolatorTest { |
| 34 | + /** Error tolerance for spline interpolator value at knot points. */ |
| 35 | + private static final double KNOT_TOL = 1e-14; |
| 36 | + /** Error tolerance for interpolating polynomial coefficients. */ |
| 37 | + private static final double COEF_TOL = 1e-14; |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testInterpolateLinearDegenerateTwoSegment() { |
| 41 | + final double[] x = {0, 0.5, 1}; |
| 42 | + final double[] y = {1, Math.exp(0.5), Math.exp(1)}; |
| 43 | + final double fpo = 1; |
| 44 | + final double fpn = Math.exp(1); |
| 45 | + final ClampedSplineInterpolator i = new ClampedSplineInterpolator(); |
| 46 | + final PolynomialSplineFunction f = i.interpolate(x, y, fpo, fpn); |
| 47 | + verifyInterpolation(f, x, y); |
| 48 | + verifyConsistency(f, x); |
| 49 | + |
| 50 | + // Verify coefficients using analytical values |
| 51 | + final PolynomialFunction[] polynomials = f.getPolynomials(); |
| 52 | + |
| 53 | + final double[] target0 = {1, 1, 0.4889506772539256, 0.21186881109317435}; |
| 54 | + final double[] target1 = {1.6487212707001282, 1.6478522855738063, 0.8067538938936871, 0.35156753198873575}; |
| 55 | + TestUtils.assertEquals(polynomials[0].getCoefficients(), target0, COEF_TOL); |
| 56 | + TestUtils.assertEquals(polynomials[1].getCoefficients(), target1, COEF_TOL); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testInterpolateLinearDegenerateThreeSegment() { |
| 61 | + final double[] x = {0, 1, 2, 3}; |
| 62 | + final double[] y = {1, Math.exp(1), Math.exp(2), Math.exp(3)}; |
| 63 | + final double fpo = 1; |
| 64 | + final double fpn = Math.exp(3); |
| 65 | + final ClampedSplineInterpolator i = new ClampedSplineInterpolator(); |
| 66 | + final PolynomialSplineFunction f = i.interpolate(x, y, fpo, fpn); |
| 67 | + verifyInterpolation(f, x, y); |
| 68 | + verifyConsistency(f, x); |
| 69 | + |
| 70 | + // Verify coefficients using analytical values |
| 71 | + final PolynomialFunction[] polynomials = f.getPolynomials(); |
| 72 | + |
| 73 | + final double[] target0 = {1, 0.9999999999999999, 0.4446824969658283, 0.27359933149321697}; |
| 74 | + final double[] target1 = {2.718281828459045, 2.710162988411307, 1.2654804914454791, 0.6951307906148195}; |
| 75 | + final double[] target2 = {7.38905609893065, 7.326516343146723, 3.3508728632899376, 2.019091617820356}; |
| 76 | + TestUtils.assertEquals(polynomials[0].getCoefficients(), target0, COEF_TOL); |
| 77 | + TestUtils.assertEquals(polynomials[1].getCoefficients(), target1, COEF_TOL); |
| 78 | + TestUtils.assertEquals(polynomials[2].getCoefficients(), target2, COEF_TOL); |
| 79 | + } |
| 80 | + |
| 81 | + @Test(expected=DimensionMismatchException.class) |
| 82 | + public void testArrayLengthMismatch() { |
| 83 | + // Data set arrays of different size. |
| 84 | + new ClampedSplineInterpolator().interpolate(new double[] {1, 2, 3, 4}, |
| 85 | + new double[] {2, 3, 5}, |
| 86 | + 2, 1); |
| 87 | + } |
| 88 | + @Test(expected=NonMonotonicSequenceException.class) |
| 89 | + public void testUnsortedArray() { |
| 90 | + // Knot values not sorted. |
| 91 | + new ClampedSplineInterpolator().interpolate(new double[] {1, 3, 2 }, |
| 92 | + new double[] {2, 3, 5}, |
| 93 | + 2, 1); |
| 94 | + } |
| 95 | + @Test(expected=NumberIsTooSmallException.class) |
| 96 | + public void testInsufficientData() { |
| 97 | + // Not enough data to interpolate. |
| 98 | + new ClampedSplineInterpolator().interpolate(new double[] {1, 2 }, |
| 99 | + new double[] {2, 3}, |
| 100 | + 2, 1); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Verifies that a clamped spline <i>without</i> specified boundary conditions behaves similar to a natural |
| 105 | + * ("unclamped") spline. |
| 106 | + * |
| 107 | + * <p> |
| 108 | + * Using the exponential function <code>e<sup>x</sup></code> over the interval <code>[0, 3]</code>, the test |
| 109 | + * evaluates: |
| 110 | + * <ol> |
| 111 | + * <li>The integral of a clamped spline with specified boundary conditions (endpoint slopes/derivatives).</li> |
| 112 | + * <li>The integral of a clamped spline without specified boundary conditions.</li> |
| 113 | + * <li>The integral of a natural spline (without boundary conditions by default).</li> |
| 114 | + * </ol> |
| 115 | + * |
| 116 | + * These integrals are compared against the direct integral of the function <code>e<sup>x</sup></code> over the same |
| 117 | + * interval.</p> |
| 118 | + * |
| 119 | + * <p> |
| 120 | + * This test is based on "Example 4" in R.L. Burden, J.D. Faires, |
| 121 | + * <u>Numerical Analysis</u>, 9th Ed., 2010, Cengage Learning, ISBN 0-538-73351-9, pp 156-157. |
| 122 | + * </p> |
| 123 | + */ |
| 124 | + @Test |
| 125 | + public void testIntegral() { |
| 126 | + final double[] x = {0, 1, 2, 3}; |
| 127 | + final double[] y = {1, Math.exp(1), Math.exp(2), Math.exp(3)}; |
| 128 | + final double fpo = 1; |
| 129 | + final double fpn = Math.exp(3); |
| 130 | + |
| 131 | + final ClampedSplineInterpolator clampedSplineInterpolator = new ClampedSplineInterpolator(); |
| 132 | + final PolynomialSplineFunction clampedSpline = clampedSplineInterpolator.interpolate(x, y, fpo, fpn); |
| 133 | + final PolynomialSplineFunction clampedSplineAsNaturalSpline = clampedSplineInterpolator.interpolate(x, y); |
| 134 | + |
| 135 | + final SplineInterpolator naturalSplineInterpolator = new SplineInterpolator(); |
| 136 | + final PolynomialSplineFunction naturalSpline = naturalSplineInterpolator.interpolate(x, y); |
| 137 | + |
| 138 | + final SimpsonIntegrator integrator = new SimpsonIntegrator(); |
| 139 | + |
| 140 | + final double clampedSplineIntegral = integrator.integrate(1000, clampedSpline, 0, 3); |
| 141 | + final double clampedSplineAsNaturalSplineIntegral = integrator.integrate(1000, clampedSplineAsNaturalSpline, 0, 3); |
| 142 | + final double naturalSplineIntegral = integrator.integrate(1000, naturalSpline, 0, 3); |
| 143 | + final double exponentialFunctionIntegral = integrator.integrate(1000, (arg) -> Math.exp(arg), 0, 3); |
| 144 | + |
| 145 | + Assert.assertEquals(Math.abs(clampedSplineAsNaturalSplineIntegral - naturalSplineIntegral), 0, 0); |
| 146 | + Assert.assertEquals(Math.abs(exponentialFunctionIntegral - clampedSplineIntegral), 0.02589, 0.1); |
| 147 | + Assert.assertEquals(Math.abs(exponentialFunctionIntegral - naturalSplineIntegral), 0.46675, 0.1); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Verifies that f(x[i]) = y[i] for i = 0, ..., n-1 (where n is common length). |
| 152 | + */ |
| 153 | + private void verifyInterpolation(PolynomialSplineFunction f, |
| 154 | + double[] x, double[] y) { |
| 155 | + for (int i = 0; i < x.length; i++) { |
| 156 | + Assert.assertEquals(f.value(x[i]), y[i], KNOT_TOL); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Verifies that interpolating polynomials satisfy consistency requirement: adjacent polynomials must agree through |
| 162 | + * two derivatives at knot points. |
| 163 | + */ |
| 164 | + private void verifyConsistency(PolynomialSplineFunction f, |
| 165 | + double[] x) { |
| 166 | + PolynomialFunction polynomials[] = f.getPolynomials(); |
| 167 | + for (int i = 1; i < x.length - 2; i++) { |
| 168 | + // evaluate polynomials and derivatives at x[i + 1] |
| 169 | + Assert.assertEquals(polynomials[i].value(x[i + 1] - x[i]), polynomials[i + 1].value(0), 0.1); |
| 170 | + Assert.assertEquals(polynomials[i].polynomialDerivative().value(x[i + 1] - x[i]), |
| 171 | + polynomials[i + 1].polynomialDerivative().value(0), 0.5); |
| 172 | + Assert.assertEquals(polynomials[i].polynomialDerivative().polynomialDerivative().value(x[i + 1] - x[i]), |
| 173 | + polynomials[i + 1].polynomialDerivative().polynomialDerivative().value(0), 0.5); |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments