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.analysis.differentiation.DSFactory;
25  import org.hipparchus.analysis.differentiation.DerivativeStructure;
26  import org.hipparchus.exception.MathIllegalArgumentException;
27  import org.hipparchus.util.Binary64;
28  import org.junit.Assert;
29  import org.junit.Test;
30  
31  
32  /**
33   * Test case for Newton form of polynomial function.
34   * <p>
35   * The small tolerance number is used only to account for round-off errors.
36   *
37   */
38  public final class PolynomialFunctionNewtonFormTest {
39  
40      /**
41       * Test of polynomial for the linear function.
42       */
43      @Test
44      public void testLinearFunction() {
45          PolynomialFunctionNewtonForm p;
46          double coefficients[], z, expected, result, tolerance = 1E-12;
47  
48          // p(x) = 1.5x - 4 = 2 + 1.5(x-4)
49          double a[] = { 2.0, 1.5 };
50          double c[] = { 4.0 };
51          p = new PolynomialFunctionNewtonForm(a, c);
52  
53          z = 2.0; expected = -1.0; result = p.value(z);
54          Assert.assertEquals(expected, result, tolerance);
55  
56          z = 4.5; expected = 2.75; result = p.value(z);
57          Assert.assertEquals(expected, result, tolerance);
58  
59          z = 6.0; expected = 5.0; result = p.value(new Binary64(z)).getReal();
60          Assert.assertEquals(expected, result, tolerance);
61  
62          Assert.assertEquals(1, p.degree());
63  
64          coefficients = p.getCoefficients();
65          Assert.assertEquals(2, coefficients.length);
66          Assert.assertEquals(-4.0, coefficients[0], tolerance);
67          Assert.assertEquals(1.5, coefficients[1], tolerance);
68      }
69  
70      /**
71       * Test of polynomial for the quadratic function.
72       */
73      @Test
74      public void testQuadraticFunction() {
75          PolynomialFunctionNewtonForm p;
76          double coefficients[], z, expected, result, tolerance = 1E-12;
77  
78          // p(x) = 2x^2 + 5x - 3 = 4 + 3(x-1) + 2(x-1)(x+2)
79          double a[] = { 4.0, 3.0, 2.0 };
80          double c[] = { 1.0, -2.0 };
81          p = new PolynomialFunctionNewtonForm(a, c);
82  
83          z = 1.0; expected = 4.0; result = p.value(z);
84          Assert.assertEquals(expected, result, tolerance);
85  
86          z = 2.5; expected = 22.0; result = p.value(z);
87          Assert.assertEquals(expected, result, tolerance);
88  
89          z = -2.0; expected = -5.0; result = p.value(z);
90          Assert.assertEquals(expected, result, tolerance);
91  
92          Assert.assertEquals(2, p.degree());
93  
94          coefficients = p.getCoefficients();
95          Assert.assertEquals(3, coefficients.length);
96          Assert.assertEquals(-3.0, coefficients[0], tolerance);
97          Assert.assertEquals(5.0, coefficients[1], tolerance);
98          Assert.assertEquals(2.0, coefficients[2], tolerance);
99      }
100 
101     /**
102      * Test of polynomial for the quintic function.
103      */
104     @Test
105     public void testQuinticFunction() {
106         PolynomialFunctionNewtonForm p;
107         double coefficients[], z, expected, result, tolerance = 1E-12;
108 
109         // p(x) = x^5 - x^4 - 7x^3 + x^2 + 6x
110         //      = 6x - 6x^2 -6x^2(x-1) + x^2(x-1)(x+1) + x^2(x-1)(x+1)(x-2)
111         double a[] = { 0.0, 6.0, -6.0, -6.0, 1.0, 1.0 };
112         double c[] = { 0.0, 0.0, 1.0, -1.0, 2.0 };
113         p = new PolynomialFunctionNewtonForm(a, c);
114 
115         z = 0.0; expected = 0.0; result = p.value(z);
116         Assert.assertEquals(expected, result, tolerance);
117 
118         z = -2.0; expected = 0.0; result = p.value(z);
119         Assert.assertEquals(expected, result, tolerance);
120 
121         z = 4.0; expected = 360.0; result = p.value(z);
122         Assert.assertEquals(expected, result, tolerance);
123 
124         Assert.assertEquals(5, p.degree());
125 
126         coefficients = p.getCoefficients();
127         Assert.assertEquals(6, coefficients.length);
128         Assert.assertEquals(0.0, coefficients[0], tolerance);
129         Assert.assertEquals(6.0, coefficients[1], tolerance);
130         Assert.assertEquals(1.0, coefficients[2], tolerance);
131         Assert.assertEquals(-7.0, coefficients[3], tolerance);
132         Assert.assertEquals(-1.0, coefficients[4], tolerance);
133         Assert.assertEquals(1.0, coefficients[5], tolerance);
134     }
135 
136     /**
137      * Test for derivatives.
138      */
139     @Test
140     public void testDerivative() {
141 
142         // x^3 = 0 * [1] + 1 * [x] + 3 * [x(x-1)] + 1 * [x(x-1)(x-2)]
143         PolynomialFunctionNewtonForm p =
144                 new PolynomialFunctionNewtonForm(new double[] { 0, 1, 3, 1 },
145                                                  new double[] { 0, 1, 2 });
146 
147         double eps = 2.0e-14;
148         DSFactory factory = new DSFactory(1, 4);
149         for (double t = 0.0; t < 10.0; t += 0.1) {
150             DerivativeStructure x = factory.variable(0, t);
151             DerivativeStructure y = p.value(x);
152             Assert.assertEquals(t * t * t,   y.getValue(),              eps * t * t * t);
153             Assert.assertEquals(3.0 * t * t, y.getPartialDerivative(1), eps * 3.0 * t * t);
154             Assert.assertEquals(6.0 * t,     y.getPartialDerivative(2), eps * 6.0 * t);
155             Assert.assertEquals(6.0,         y.getPartialDerivative(3), eps * 6.0);
156             Assert.assertEquals(0.0,         y.getPartialDerivative(4), eps);
157         }
158 
159     }
160 
161     /**
162      * Test of parameters for the polynomial.
163      */
164     @Test
165     public void testParameters() {
166 
167         try {
168             // bad input array length
169             double a[] = { 1.0 };
170             double c[] = { 2.0 };
171             new PolynomialFunctionNewtonForm(a, c);
172             Assert.fail("Expecting MathIllegalArgumentException - bad input array length");
173         } catch (MathIllegalArgumentException ex) {
174             // expected
175         }
176         try {
177             // mismatch input arrays
178             double a[] = { 1.0, 2.0, 3.0, 4.0 };
179             double c[] = { 4.0, 3.0, 2.0, 1.0 };
180             new PolynomialFunctionNewtonForm(a, c);
181             Assert.fail("Expecting MathIllegalArgumentException - mismatch input arrays");
182         } catch (MathIllegalArgumentException ex) {
183             // expected
184         }
185     }
186 }