View Javadoc
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    *      https://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  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.analysis.interpolation;
23  
24  import org.hipparchus.UnitTestUtils;
25  import org.hipparchus.analysis.UnivariateFunction;
26  import org.hipparchus.analysis.polynomials.PolynomialFunction;
27  import org.hipparchus.analysis.polynomials.PolynomialSplineFunction;
28  import org.hipparchus.util.FastMath;
29  import org.junit.Assert;
30  import org.junit.Test;
31  
32  /**
33   * Test the SplineInterpolator.
34   *
35   */
36  public class SplineInterpolatorTest extends UnivariateInterpolatorAbstractTest {
37  
38      protected UnivariateInterpolator buildDoubleInterpolator() {
39          return new SplineInterpolator();
40      }
41  
42      protected FieldUnivariateInterpolator buildFieldInterpolator() {
43          return new SplineInterpolator();
44      }
45  
46      @Test
47      public void testInterpolateSin() {
48          double sineCoefficientTolerance = 1e-6;
49          double sineInterpolationTolerance = 0.0043;
50          double x[] =
51              {
52                  0.0,
53                  FastMath.PI / 6d,
54                  FastMath.PI / 2d,
55                  5d * FastMath.PI / 6d,
56                  FastMath.PI,
57                  7d * FastMath.PI / 6d,
58                  3d * FastMath.PI / 2d,
59                  11d * FastMath.PI / 6d,
60                  2.d * FastMath.PI };
61          double y[] = { 0d, 0.5d, 1d, 0.5d, 0d, -0.5d, -1d, -0.5d, 0d };
62          UnivariateInterpolator i = buildDoubleInterpolator();
63          UnivariateFunction f = i.interpolate(x, y);
64          verifyInterpolation(f, x, y);
65          verifyConsistency((PolynomialSplineFunction) f, x);
66  
67          /* Check coefficients against values computed using R (version 1.8.1, Red Hat Linux 9)
68           *
69           * To replicate in R:
70           *     x[1] <- 0
71           *     x[2] <- pi / 6, etc, same for y[] (could use y <- scan() for y values)
72           *     g <- splinefun(x, y, "natural")
73           *     splinecoef <- eval(expression(z), envir = environment(g))
74           *     print(splinecoef)
75           */
76          PolynomialFunction polynomials[] = ((PolynomialSplineFunction) f).getPolynomials();
77          double target[] = {y[0], 1.002676d, 0d, -0.17415829d};
78          UnitTestUtils.assertEquals(polynomials[0].getCoefficients(), target, sineCoefficientTolerance);
79          target = new double[]{y[1], 8.594367e-01, -2.735672e-01, -0.08707914};
80          UnitTestUtils.assertEquals(polynomials[1].getCoefficients(), target, sineCoefficientTolerance);
81          target = new double[]{y[2], 1.471804e-17,-5.471344e-01, 0.08707914};
82          UnitTestUtils.assertEquals(polynomials[2].getCoefficients(), target, sineCoefficientTolerance);
83          target = new double[]{y[3], -8.594367e-01, -2.735672e-01, 0.17415829};
84          UnitTestUtils.assertEquals(polynomials[3].getCoefficients(), target, sineCoefficientTolerance);
85          target = new double[]{y[4], -1.002676, 6.548562e-17, 0.17415829};
86          UnitTestUtils.assertEquals(polynomials[4].getCoefficients(), target, sineCoefficientTolerance);
87          target = new double[]{y[5], -8.594367e-01, 2.735672e-01, 0.08707914};
88          UnitTestUtils.assertEquals(polynomials[5].getCoefficients(), target, sineCoefficientTolerance);
89          target = new double[]{y[6], 3.466465e-16, 5.471344e-01, -0.08707914};
90          UnitTestUtils.assertEquals(polynomials[6].getCoefficients(), target, sineCoefficientTolerance);
91          target = new double[]{y[7], 8.594367e-01, 2.735672e-01, -0.17415829};
92          UnitTestUtils.assertEquals(polynomials[7].getCoefficients(), target, sineCoefficientTolerance);
93  
94          //Check interpolation
95          Assert.assertEquals(FastMath.sqrt(2d) / 2d,f.value(FastMath.PI/4d),sineInterpolationTolerance);
96          Assert.assertEquals(FastMath.sqrt(2d) / 2d,f.value(3d*FastMath.PI/4d),sineInterpolationTolerance);
97      }
98  
99      /**
100      * Verifies that interpolating polynomials satisfy consistency requirement:
101      *    adjacent polynomials must agree through two derivatives at knot points
102      */
103     protected void verifyConsistency(PolynomialSplineFunction f, double x[])
104         {
105         PolynomialFunction polynomials[] = f.getPolynomials();
106         for (int i = 1; i < x.length - 2; i++) {
107             // evaluate polynomials and derivatives at x[i + 1]
108             Assert.assertEquals(polynomials[i].value(x[i +1] - x[i]), polynomials[i + 1].value(0), 0.1);
109             Assert.assertEquals(polynomials[i].polynomialDerivative().value(x[i +1] - x[i]),
110                                 polynomials[i + 1].polynomialDerivative().value(0), 0.5);
111             Assert.assertEquals(polynomials[i].polynomialDerivative().polynomialDerivative().value(x[i +1] - x[i]),
112                                 polynomials[i + 1].polynomialDerivative().polynomialDerivative().value(0), 0.5);
113         }
114     }
115 
116 }