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.polynomials;
23  
24  import org.hipparchus.exception.LocalizedCoreFormats;
25  import org.hipparchus.exception.MathIllegalArgumentException;
26  import org.hipparchus.exception.MathIllegalStateException;
27  import org.hipparchus.util.Binary64;
28  import org.junit.jupiter.api.Test;
29  
30  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
31  import static org.junit.jupiter.api.Assertions.assertEquals;
32  import static org.junit.jupiter.api.Assertions.assertFalse;
33  import static org.junit.jupiter.api.Assertions.assertTrue;
34  import static org.junit.jupiter.api.Assertions.fail;
35  
36  /**
37   * Tests the PolynomialSplineFunction implementation.
38   *
39   */
40  class PolynomialSplineFunctionTest {
41  
42      /** Error tolerance for tests */
43      protected double tolerance = 1.0e-12;
44  
45      /**
46       * Quadratic polynomials used in tests:
47       *
48       * x^2 + x            [-1, 0)
49       * x^2 + x + 2        [0, 1)
50       * x^2 + x + 4        [1, 2)
51       *
52       * Defined so that evaluation using PolynomialSplineFunction evaluation
53       * algorithm agrees at knot point boundaries.
54       */
55      protected PolynomialFunction[] polynomials = {
56          new PolynomialFunction(new double[] {0d, 1d, 1d}),
57          new PolynomialFunction(new double[] {2d, 1d, 1d}),
58          new PolynomialFunction(new double[] {4d, 1d, 1d})
59      };
60  
61      /** Knot points  */
62      protected double[] knots = {-1, 0, 1, 2};
63  
64      /** Derivative of test polynomials -- 2x + 1  */
65      protected PolynomialFunction dp =
66          new PolynomialFunction(new double[] {1d, 2d});
67  
68  
69      @Test
70      void testConstructor() {
71          PolynomialSplineFunction spline =
72              new PolynomialSplineFunction(knots, polynomials);
73          assertArrayEquals(knots, spline.getKnots());
74          assertEquals(1d, spline.getPolynomials()[0].getCoefficients()[2], 0);
75          assertEquals(3, spline.getN());
76  
77          try { // too few knots
78              new PolynomialSplineFunction(new double[] {0}, polynomials);
79              fail("Expecting MathIllegalArgumentException");
80          } catch (MathIllegalArgumentException ex) {
81              // expected
82          }
83  
84          try { // too many knots
85              new PolynomialSplineFunction(new double[] {0,1,2,3,4}, polynomials);
86              fail("Expecting MathIllegalArgumentException");
87          } catch (MathIllegalArgumentException ex) {
88              // expected
89          }
90  
91          try { // knots not increasing
92              new PolynomialSplineFunction(new double[] {0,1, 3, 2}, polynomials);
93              fail("Expecting MathIllegalArgumentException");
94          } catch (MathIllegalArgumentException ex) {
95              // expected
96          }
97      }
98  
99      @Test
100     void testValues() {
101         PolynomialSplineFunction spline =
102             new PolynomialSplineFunction(knots, polynomials);
103         PolynomialSplineFunction dSpline = spline.polynomialSplineDerivative();
104 
105         /**
106          * interior points -- spline value at x should equal p(x - knot)
107          * where knot is the largest knot point less than or equal to x and p
108          * is the polynomial defined over the knot segment to which x belongs.
109          */
110         double x = -1;
111         int index = 0;
112         for (int i = 0; i < 10; i++) {
113            x+=0.25;
114            index = findKnot(knots, x);
115            assertEquals(polynomials[index].value(x - knots[index]), spline.value(x), tolerance, "spline function evaluation failed for x=" + x);
116            assertEquals(dp.value(x - knots[index]), dSpline.value(x), tolerance, "spline derivative evaluation failed for x=" + x);
117         }
118 
119         // knot points -- centering should zero arguments
120         for (int i = 0; i < 3; i++) {
121             assertEquals(polynomials[i].value(0), spline.value(knots[i]), tolerance, "spline function evaluation failed for knot=" + knots[i]);
122             assertEquals(dp.value(0), dSpline.value(new Binary64(knots[i])).getReal(), tolerance, "spline function evaluation failed for knot=" + knots[i]);
123         }
124 
125         try { //outside of domain -- under min
126             x = spline.value(-1.5);
127             fail("Expecting MathIllegalArgumentException");
128         } catch (MathIllegalArgumentException ex) {
129             // expected
130         }
131 
132         try { //outside of domain -- over max
133             x = spline.value(2.5);
134             fail("Expecting MathIllegalArgumentException");
135         } catch (MathIllegalArgumentException ex) {
136             // expected
137         }
138     }
139 
140     @Test
141     void testIsValidPoint() {
142         final PolynomialSplineFunction spline =
143             new PolynomialSplineFunction(knots, polynomials);
144         final double xMin = knots[0];
145         final double xMax = knots[knots.length - 1];
146 
147         double x;
148 
149         x = xMin;
150         assertTrue(spline.isValidPoint(x));
151         // Ensure that no exception is thrown.
152         spline.value(x);
153 
154         x = xMax;
155         assertTrue(spline.isValidPoint(x));
156         // Ensure that no exception is thrown.
157         spline.value(x);
158 
159         final double xRange = xMax - xMin;
160         x = xMin + xRange / 3.4;
161         assertTrue(spline.isValidPoint(x));
162         // Ensure that no exception is thrown.
163         spline.value(x);
164 
165         final double small = 1e-8;
166         x = xMin - small;
167         assertFalse(spline.isValidPoint(x));
168         // Ensure that an exception would have been thrown.
169         try {
170             spline.value(x);
171             fail("MathIllegalArgumentException expected");
172         } catch (MathIllegalArgumentException expected) {}
173     }
174 
175     /**
176      *  Do linear search to find largest knot point less than or equal to x.
177      *  Implementation does binary search.
178      */
179      protected int findKnot(double[] knots, double x) {
180          if (x < knots[0] || x >= knots[knots.length -1]) {
181              throw new MathIllegalArgumentException(LocalizedCoreFormats.OUT_OF_RANGE_SIMPLE,
182                                                     x, knots[0], knots[knots.length -1]);
183          }
184          for (int i = 0; i < knots.length; i++) {
185              if (knots[i] > x) {
186                  return i - 1;
187              }
188          }
189          throw new MathIllegalStateException(LocalizedCoreFormats.ILLEGAL_STATE);
190      }
191 }
192