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.exception.MathIllegalArgumentException;
20  import org.junit.Assert;
21  import org.junit.Test;
22  
23  /**
24   * Test case for Lagrange form of polynomial function.
25   * <p>
26   * We use n+1 points to interpolate a polynomial of degree n. This should
27   * give us the exact same polynomial as result. Thus we can use a very
28   * small tolerance to account only for round-off errors.
29   *
30   */
31  public final class PolynomialFunctionLagrangeFormTest {
32  
33      /**
34       * Test of polynomial for the linear function.
35       */
36      @Test
37      public void testLinearFunction() {
38          PolynomialFunctionLagrangeForm p;
39          double[] c;
40          double z;
41          double expected;
42          double result;
43          double tolerance = 1E-12;
44  
45          // p(x) = 1.5x - 4
46          double x[] = { 0.0, 3.0 };
47          double y[] = { -4.0, 0.5 };
48          p = new PolynomialFunctionLagrangeForm(x, y);
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          c = p.getCoefficients();
62          Assert.assertEquals(2, c.length);
63          Assert.assertEquals(-4.0, c[0], tolerance);
64          Assert.assertEquals(1.5, c[1], tolerance);
65      }
66  
67      /**
68       * Test of polynomial for the quadratic function.
69       */
70      @Test
71      public void testQuadraticFunction() {
72          PolynomialFunctionLagrangeForm p;
73          double[] c;
74          double z;
75          double expected;
76          double result;
77          double tolerance = 1E-12;
78  
79          // p(x) = 2x^2 + 5x - 3 = (2x - 1)(x + 3)
80          double x[] = { 0.0, -1.0, 0.5 };
81          double y[] = { -3.0, -6.0, 0.0 };
82          p = new PolynomialFunctionLagrangeForm(x, y);
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          c = p.getCoefficients();
96          Assert.assertEquals(3, c.length);
97          Assert.assertEquals(-3.0, c[0], tolerance);
98          Assert.assertEquals(5.0, c[1], tolerance);
99          Assert.assertEquals(2.0, c[2], tolerance);
100     }
101 
102     /**
103      * Test of polynomial for the quintic function.
104      */
105     @Test
106     public void testQuinticFunction() {
107         PolynomialFunctionLagrangeForm p;
108         double[] c;
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 = x(x^2 - 1)(x + 2)(x - 3)
115         double x[] = { 1.0, -1.0, 2.0, 3.0, -3.0, 0.5 };
116         double y[] = { 0.0, 0.0, -24.0, 0.0, -144.0, 2.34375 };
117         p = new PolynomialFunctionLagrangeForm(x, y);
118 
119         z = 0.0; expected = 0.0; result = p.value(z);
120         Assert.assertEquals(expected, result, tolerance);
121 
122         z = -2.0; expected = 0.0; result = p.value(z);
123         Assert.assertEquals(expected, result, tolerance);
124 
125         z = 4.0; expected = 360.0; result = p.value(z);
126         Assert.assertEquals(expected, result, tolerance);
127 
128         Assert.assertEquals(5, p.degree());
129 
130         c = p.getCoefficients();
131         Assert.assertEquals(6, c.length);
132         Assert.assertEquals(0.0, c[0], tolerance);
133         Assert.assertEquals(6.0, c[1], tolerance);
134         Assert.assertEquals(1.0, c[2], tolerance);
135         Assert.assertEquals(-7.0, c[3], tolerance);
136         Assert.assertEquals(-1.0, c[4], tolerance);
137         Assert.assertEquals(1.0, c[5], tolerance);
138     }
139 
140     /**
141      * Test of parameters for the polynomial.
142      */
143     @Test
144     public void testParameters() {
145 
146         try {
147             // bad input array length
148             double x[] = { 1.0 };
149             double y[] = { 2.0 };
150             new PolynomialFunctionLagrangeForm(x, y);
151             Assert.fail("Expecting MathIllegalArgumentException - bad input array length");
152         } catch (MathIllegalArgumentException ex) {
153             // expected
154         }
155         try {
156             // mismatch input arrays
157             double x[] = { 1.0, 2.0, 3.0, 4.0 };
158             double y[] = { 0.0, -4.0, -24.0 };
159             new PolynomialFunctionLagrangeForm(x, y);
160             Assert.fail("Expecting MathIllegalArgumentException - mismatch input arrays");
161         } catch (MathIllegalArgumentException ex) {
162             // expected
163         }
164     }
165 }