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 org.apache.commons.math.MathException;
20  import junit.framework.TestCase;
21  
22  /**
23   * Testcase for Newton form of polynomial function.
24   * <p>
25   * The small tolerance number is used only to account for round-off errors.
26   *
27   * @version $Revision: 799857 $ $Date: 2009-08-01 09:07:12 -0400 (Sat, 01 Aug 2009) $ 
28   */
29  public final class PolynomialFunctionNewtonFormTest extends TestCase {
30  
31      /**
32       * Test of polynomial for the linear function.
33       */
34      public void testLinearFunction() throws MathException {
35          PolynomialFunctionNewtonForm p;
36          double coefficients[], z, expected, result, tolerance = 1E-12;
37  
38          // p(x) = 1.5x - 4 = 2 + 1.5(x-4)
39          double a[] = { 2.0, 1.5 };
40          double c[] = { 4.0 };
41          p = new PolynomialFunctionNewtonForm(a, c);
42  
43          z = 2.0; expected = -1.0; result = p.value(z);
44          assertEquals(expected, result, tolerance);
45  
46          z = 4.5; expected = 2.75; result = p.value(z);
47          assertEquals(expected, result, tolerance);
48  
49          z = 6.0; expected = 5.0; result = p.value(z);
50          assertEquals(expected, result, tolerance);
51  
52          assertEquals(1, p.degree());
53  
54          coefficients = p.getCoefficients();
55          assertEquals(2, coefficients.length);
56          assertEquals(-4.0, coefficients[0], tolerance);
57          assertEquals(1.5, coefficients[1], tolerance);
58      }
59  
60      /**
61       * Test of polynomial for the quadratic function.
62       */
63      public void testQuadraticFunction() throws MathException {
64          PolynomialFunctionNewtonForm p;
65          double coefficients[], z, expected, result, tolerance = 1E-12;
66  
67          // p(x) = 2x^2 + 5x - 3 = 4 + 3(x-1) + 2(x-1)(x+2)
68          double a[] = { 4.0, 3.0, 2.0 };
69          double c[] = { 1.0, -2.0 };
70          p = new PolynomialFunctionNewtonForm(a, c);
71  
72          z = 1.0; expected = 4.0; result = p.value(z);
73          assertEquals(expected, result, tolerance);
74  
75          z = 2.5; expected = 22.0; result = p.value(z);
76          assertEquals(expected, result, tolerance);
77  
78          z = -2.0; expected = -5.0; result = p.value(z);
79          assertEquals(expected, result, tolerance);
80  
81          assertEquals(2, p.degree());
82  
83          coefficients = p.getCoefficients();
84          assertEquals(3, coefficients.length);
85          assertEquals(-3.0, coefficients[0], tolerance);
86          assertEquals(5.0, coefficients[1], tolerance);
87          assertEquals(2.0, coefficients[2], tolerance);
88      }
89  
90      /**
91       * Test of polynomial for the quintic function.
92       */
93      public void testQuinticFunction() throws MathException {
94          PolynomialFunctionNewtonForm p;
95          double coefficients[], z, expected, result, tolerance = 1E-12;
96  
97          // p(x) = x^5 - x^4 - 7x^3 + x^2 + 6x
98          //      = 6x - 6x^2 -6x^2(x-1) + x^2(x-1)(x+1) + x^2(x-1)(x+1)(x-2)
99          double a[] = { 0.0, 6.0, -6.0, -6.0, 1.0, 1.0 };
100         double c[] = { 0.0, 0.0, 1.0, -1.0, 2.0 };
101         p = new PolynomialFunctionNewtonForm(a, c);
102 
103         z = 0.0; expected = 0.0; result = p.value(z);
104         assertEquals(expected, result, tolerance);
105 
106         z = -2.0; expected = 0.0; result = p.value(z);
107         assertEquals(expected, result, tolerance);
108 
109         z = 4.0; expected = 360.0; result = p.value(z);
110         assertEquals(expected, result, tolerance);
111 
112         assertEquals(5, p.degree());
113 
114         coefficients = p.getCoefficients();
115         assertEquals(6, coefficients.length);
116         assertEquals(0.0, coefficients[0], tolerance);
117         assertEquals(6.0, coefficients[1], tolerance);
118         assertEquals(1.0, coefficients[2], tolerance);
119         assertEquals(-7.0, coefficients[3], tolerance);
120         assertEquals(-1.0, coefficients[4], tolerance);
121         assertEquals(1.0, coefficients[5], tolerance);
122     }
123 
124     /**
125      * Test of parameters for the polynomial.
126      */
127     public void testParameters() throws Exception {
128 
129         try {
130             // bad input array length
131             double a[] = { 1.0 };
132             double c[] = { 2.0 };
133             new PolynomialFunctionNewtonForm(a, c);
134             fail("Expecting IllegalArgumentException - bad input array length");
135         } catch (IllegalArgumentException ex) {
136             // expected
137         }
138         try {
139             // mismatch input arrays
140             double a[] = { 1.0, 2.0, 3.0, 4.0 };
141             double c[] = { 4.0, 3.0, 2.0, 1.0 };
142             new PolynomialFunctionNewtonForm(a, c);
143             fail("Expecting IllegalArgumentException - mismatch input arrays");
144         } catch (IllegalArgumentException ex) {
145             // expected
146         }
147     }
148 }