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