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.CalculusFieldUnivariateFunction;
26  import org.hipparchus.analysis.UnivariateFunction;
27  import org.hipparchus.analysis.polynomials.FieldPolynomialFunction;
28  import org.hipparchus.analysis.polynomials.FieldPolynomialSplineFunction;
29  import org.hipparchus.analysis.polynomials.PolynomialFunction;
30  import org.hipparchus.analysis.polynomials.PolynomialSplineFunction;
31  import org.hipparchus.util.Binary64;
32  import org.junit.Test;
33  
34  /**
35   * Test the LinearInterpolator.
36   */
37  public class LinearInterpolatorTest extends UnivariateInterpolatorAbstractTest {
38  
39      protected UnivariateInterpolator buildDoubleInterpolator() {
40          return new LinearInterpolator();
41      }
42  
43      protected FieldUnivariateInterpolator buildFieldInterpolator() {
44          return new LinearInterpolator();
45      }
46  
47      @Test
48      public void testInterpolateLinear() {
49          double[] x = { 0.0, 0.5, 1.0 };
50          double[] y = { 0.0, 0.5, 0.0 };
51          UnivariateInterpolator i = buildDoubleInterpolator();
52          UnivariateFunction f = i.interpolate(x, y);
53          verifyInterpolation(f, x, y);
54  
55          // Verify coefficients using analytical values
56          PolynomialFunction[] polynomials = ((PolynomialSplineFunction) f).getPolynomials();
57          double[] target = {y[0], 1d};
58          UnitTestUtils.assertEquals(polynomials[0].getCoefficients(), target, coefficientTolerance);
59          target = new double[]{y[1], -1d};
60          UnitTestUtils.assertEquals(polynomials[1].getCoefficients(), target, coefficientTolerance);
61      }
62  
63      @Test
64      public void testInterpolateLinearD64() {
65          Binary64[] x = buildD64(0.0, 0.5, 1.0);
66          Binary64[] y = buildD64(0.0, 0.5, 0.0);
67          FieldUnivariateInterpolator i = buildFieldInterpolator();
68          CalculusFieldUnivariateFunction<Binary64> f = i.interpolate(x, y);
69          verifyInterpolation(f, x, y);
70  
71          // Verify coefficients using analytical values
72          FieldPolynomialFunction<Binary64>[] polynomials = ((FieldPolynomialSplineFunction<Binary64>) f).getPolynomials();
73          checkCoeffs(coefficientTolerance, polynomials[0], y[0].getReal(), +1.0);
74          checkCoeffs(coefficientTolerance, polynomials[1], y[1].getReal(), -1.0);
75      }
76  
77  }