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[], z, expected, result, tolerance = 1E-12;
45  
46          // p(x) = 1.5x - 4
47          double x[] = { 0.0, 3.0 };
48          double y[] = { -4.0, 0.5 };
49          p = new PolynomialFunctionLagrangeForm(x, y);
50  
51          z = 2.0; expected = -1.0; result = p.value(z);
52          Assert.assertEquals(expected, result, tolerance);
53  
54          z = 4.5; expected = 2.75; result = p.value(z);
55          Assert.assertEquals(expected, result, tolerance);
56  
57          z = 6.0; expected = 5.0; result = p.value(z);
58          Assert.assertEquals(expected, result, tolerance);
59  
60          Assert.assertEquals(1, p.degree());
61  
62          c = p.getCoefficients();
63          Assert.assertEquals(2, c.length);
64          Assert.assertEquals(-4.0, c[0], tolerance);
65          Assert.assertEquals(1.5, c[1], tolerance);
66      }
67  
68      /**
69       * Test of polynomial for the quadratic function.
70       */
71      @Test
72      public void testQuadraticFunction() {
73          PolynomialFunctionLagrangeForm p;
74          double c[], z, expected, result, tolerance = 1E-12;
75  
76          // p(x) = 2x^2 + 5x - 3 = (2x - 1)(x + 3)
77          double x[] = { 0.0, -1.0, 0.5 };
78          double y[] = { -3.0, -6.0, 0.0 };
79          p = new PolynomialFunctionLagrangeForm(x, y);
80  
81          z = 1.0; expected = 4.0; result = p.value(z);
82          Assert.assertEquals(expected, result, tolerance);
83  
84          z = 2.5; expected = 22.0; result = p.value(z);
85          Assert.assertEquals(expected, result, tolerance);
86  
87          z = -2.0; expected = -5.0; result = p.value(z);
88          Assert.assertEquals(expected, result, tolerance);
89  
90          Assert.assertEquals(2, p.degree());
91  
92          c = p.getCoefficients();
93          Assert.assertEquals(3, c.length);
94          Assert.assertEquals(-3.0, c[0], tolerance);
95          Assert.assertEquals(5.0, c[1], tolerance);
96          Assert.assertEquals(2.0, c[2], tolerance);
97      }
98  
99      /**
100      * Test of polynomial for the quintic function.
101      */
102     @Test
103     public void testQuinticFunction() {
104         PolynomialFunctionLagrangeForm p;
105         double c[], z, expected, result, tolerance = 1E-12;
106 
107         // p(x) = x^5 - x^4 - 7x^3 + x^2 + 6x = x(x^2 - 1)(x + 2)(x - 3)
108         double x[] = { 1.0, -1.0, 2.0, 3.0, -3.0, 0.5 };
109         double y[] = { 0.0, 0.0, -24.0, 0.0, -144.0, 2.34375 };
110         p = new PolynomialFunctionLagrangeForm(x, y);
111 
112         z = 0.0; expected = 0.0; result = p.value(z);
113         Assert.assertEquals(expected, result, tolerance);
114 
115         z = -2.0; expected = 0.0; result = p.value(z);
116         Assert.assertEquals(expected, result, tolerance);
117 
118         z = 4.0; expected = 360.0; result = p.value(z);
119         Assert.assertEquals(expected, result, tolerance);
120 
121         Assert.assertEquals(5, p.degree());
122 
123         c = p.getCoefficients();
124         Assert.assertEquals(6, c.length);
125         Assert.assertEquals(0.0, c[0], tolerance);
126         Assert.assertEquals(6.0, c[1], tolerance);
127         Assert.assertEquals(1.0, c[2], tolerance);
128         Assert.assertEquals(-7.0, c[3], tolerance);
129         Assert.assertEquals(-1.0, c[4], tolerance);
130         Assert.assertEquals(1.0, c[5], tolerance);
131     }
132 
133     /**
134      * Test of parameters for the polynomial.
135      */
136     @Test
137     public void testParameters() {
138 
139         try {
140             // bad input array length
141             double x[] = { 1.0 };
142             double y[] = { 2.0 };
143             new PolynomialFunctionLagrangeForm(x, y);
144             Assert.fail("Expecting MathIllegalArgumentException - bad input array length");
145         } catch (MathIllegalArgumentException ex) {
146             // expected
147         }
148         try {
149             // mismatch input arrays
150             double x[] = { 1.0, 2.0, 3.0, 4.0 };
151             double y[] = { 0.0, -4.0, -24.0 };
152             new PolynomialFunctionLagrangeForm(x, y);
153             Assert.fail("Expecting MathIllegalArgumentException - mismatch input arrays");
154         } catch (MathIllegalArgumentException ex) {
155             // expected
156         }
157     }
158 }