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