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