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