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.math.analysis.polynomials;
18  
19  import java.util.Arrays;
20  import junit.framework.TestCase;
21  
22  import org.apache.commons.math.FunctionEvaluationException;
23  import org.apache.commons.math.analysis.UnivariateRealFunction;
24  
25  /**
26   * Tests the PolynomialSplineFunction implementation.
27   *
28   * @version $Revision: 799857 $
29   */
30  public class PolynomialSplineFunctionTest extends TestCase {
31  
32      /** Error tolerance for tests */
33      protected double tolerance = 1.0e-12;
34      
35      /** 
36       * Quadratic polynomials used in tests: 
37       * 
38       * x^2 + x            [-1, 0)
39       * x^2 + x + 2        [0, 1)
40       * x^2 + x + 4        [1, 2)
41       * 
42       * Defined so that evaluation using PolynomialSplineFunction evaluation
43       * algorithm agrees at knot point boundaries.
44       */
45      protected PolynomialFunction[] polynomials = {
46          new PolynomialFunction(new double[] {0d, 1d, 1d}), 
47          new PolynomialFunction(new double[] {2d, 1d, 1d}),
48          new PolynomialFunction(new double[] {4d, 1d, 1d})
49      };
50      
51      /** Knot points  */
52      protected double[] knots = {-1, 0, 1, 2};
53      
54      /** Derivative of test polynomials -- 2x + 1  */
55      protected PolynomialFunction dp = 
56          new PolynomialFunction(new double[] {1d, 2d});
57      
58      
59      public void testConstructor() {
60          PolynomialSplineFunction spline = 
61              new PolynomialSplineFunction(knots, polynomials);
62          assertTrue(Arrays.equals(knots, spline.getKnots()));
63          assertEquals(1d, spline.getPolynomials()[0].getCoefficients()[2], 0);
64          assertEquals(3, spline.getN());
65          
66          try { // too few knots
67              new PolynomialSplineFunction(new double[] {0}, polynomials);
68              fail("Expecting IllegalArgumentException");
69          } catch (IllegalArgumentException ex) {
70              // expected
71          }
72          
73          try { // too many knots
74              new PolynomialSplineFunction(new double[] {0,1,2,3,4}, polynomials);
75              fail("Expecting IllegalArgumentException");
76          } catch (IllegalArgumentException ex) {
77              // expected
78          }
79          
80          try { // knots not increasing
81              new PolynomialSplineFunction(new double[] {0,1, 3, 2}, polynomials);
82              fail("Expecting IllegalArgumentException");
83          } catch (IllegalArgumentException ex) {
84              // expected
85          }
86      }
87      
88      public void testValues() throws Exception {
89          PolynomialSplineFunction spline = 
90              new PolynomialSplineFunction(knots, polynomials);
91          UnivariateRealFunction dSpline = spline.derivative();
92          
93          /**
94           * interior points -- spline value at x should equal p(x - knot)
95           * where knot is the largest knot point less than or equal to x and p 
96           * is the polynomial defined over the knot segment to which x belongs.
97           */
98          double x = -1;
99          int index = 0;
100         for (int i = 0; i < 10; i++) {
101            x+=0.25;
102            index = findKnot(knots, x);
103            assertEquals("spline function evaluation failed for x=" + x, 
104                    polynomials[index].value(x - knots[index]), spline.value(x), tolerance);
105            assertEquals("spline derivative evaluation failed for x=" + x,
106                    dp.value(x - knots[index]), dSpline.value(x), tolerance);
107         }
108         
109         // knot points -- centering should zero arguments
110         for (int i = 0; i < 3; i++) {
111             assertEquals("spline function evaluation failed for knot=" + knots[i],
112                     polynomials[i].value(0), spline.value(knots[i]), tolerance);
113             assertEquals("spline function evaluation failed for knot=" + knots[i],
114                     dp.value(0), dSpline.value(knots[i]), tolerance);
115         }
116         
117         try { //outside of domain -- under min
118             x = spline.value(-1.5);
119             fail("Expecting IllegalArgumentException");
120         } catch (FunctionEvaluationException ex) {
121             // expected
122         }
123         
124         try { //outside of domain -- over max
125             x = spline.value(2.5);
126             fail("Expecting IllegalArgumentException");
127         } catch (FunctionEvaluationException ex) {
128             // expected
129         }         
130     }  
131     
132     /**
133      *  Do linear search to find largest knot point less than or equal to x.
134      *  Implementation does binary search.
135      */
136      protected int findKnot(double[] knots, double x) {
137          if (x < knots[0] || x >= knots[knots.length -1]) {
138              throw new IllegalArgumentException("x is out of range");
139          }
140          for (int i = 0; i < knots.length; i++) {
141              if (knots[i] > x) {
142                  return i -1;
143              }
144          }
145          throw new IllegalArgumentException("x is out of range");
146      }
147 }
148