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.analysis.UnivariateFunction;
25  import org.hipparchus.analysis.integration.IterativeLegendreGaussIntegrator;
26  import org.hipparchus.util.CombinatoricsUtils;
27  import org.hipparchus.util.FastMath;
28  import org.hipparchus.util.Precision;
29  import org.junit.jupiter.api.Assertions;
30  import org.junit.jupiter.api.Test;
31  
32  import static org.junit.jupiter.api.Assertions.assertEquals;
33  import static org.junit.jupiter.api.Assertions.assertNotEquals;
34  import static org.junit.jupiter.api.Assertions.assertTrue;
35  import static org.junit.jupiter.api.Assertions.fail;
36  
37  /**
38   * Tests the PolynomialsUtils class.
39   *
40   */
41  class PolynomialsUtilsTest {
42  
43      @Test
44      void testFirstChebyshevPolynomials() {
45          checkPolynomial(PolynomialsUtils.createChebyshevPolynomial(3), "-3 x + 4 x^3");
46          checkPolynomial(PolynomialsUtils.createChebyshevPolynomial(2), "-1 + 2 x^2");
47          checkPolynomial(PolynomialsUtils.createChebyshevPolynomial(1), "x");
48          checkPolynomial(PolynomialsUtils.createChebyshevPolynomial(0), "1");
49  
50          checkPolynomial(PolynomialsUtils.createChebyshevPolynomial(7), "-7 x + 56 x^3 - 112 x^5 + 64 x^7");
51          checkPolynomial(PolynomialsUtils.createChebyshevPolynomial(6), "-1 + 18 x^2 - 48 x^4 + 32 x^6");
52          checkPolynomial(PolynomialsUtils.createChebyshevPolynomial(5), "5 x - 20 x^3 + 16 x^5");
53          checkPolynomial(PolynomialsUtils.createChebyshevPolynomial(4), "1 - 8 x^2 + 8 x^4");
54  
55      }
56  
57      @Test
58      void testChebyshevBounds() {
59          for (int k = 0; k < 12; ++k) {
60              PolynomialFunction Tk = PolynomialsUtils.createChebyshevPolynomial(k);
61              for (double x = -1; x <= 1; x += 0.02) {
62                  assertTrue(FastMath.abs(Tk.value(x)) < (1 + 1e-12), k + " " + Tk.value(x));
63              }
64          }
65      }
66  
67      @Test
68      void testChebyshevDifferentials() {
69          for (int k = 0; k < 12; ++k) {
70  
71              PolynomialFunction Tk0 = PolynomialsUtils.createChebyshevPolynomial(k);
72              PolynomialFunction Tk1 = Tk0.polynomialDerivative();
73              PolynomialFunction Tk2 = Tk1.polynomialDerivative();
74  
75              PolynomialFunction g0 = new PolynomialFunction(new double[] { k * k });
76              PolynomialFunction g1 = new PolynomialFunction(new double[] { 0, -1});
77              PolynomialFunction g2 = new PolynomialFunction(new double[] { 1, 0, -1 });
78  
79              PolynomialFunction Tk0g0 = Tk0.multiply(g0);
80              PolynomialFunction Tk1g1 = Tk1.multiply(g1);
81              PolynomialFunction Tk2g2 = Tk2.multiply(g2);
82  
83              checkNullPolynomial(Tk0g0.add(Tk1g1.add(Tk2g2)));
84  
85          }
86      }
87  
88      @Test
89      void testChebyshevOrthogonality() {
90          UnivariateFunction weight = x -> 1 / FastMath.sqrt(1 - x * x);
91          for (int i = 0; i < 10; ++i) {
92              PolynomialFunction pi = PolynomialsUtils.createChebyshevPolynomial(i);
93              for (int j = 0; j <= i; ++j) {
94                  PolynomialFunction pj = PolynomialsUtils.createChebyshevPolynomial(j);
95                  checkOrthogonality(pi, pj, weight, -0.9999, 0.9999, 1.5, 0.03);
96              }
97          }
98      }
99  
100     @Test
101     void testFirstHermitePolynomials() {
102         checkPolynomial(PolynomialsUtils.createHermitePolynomial(3), "-12 x + 8 x^3");
103         checkPolynomial(PolynomialsUtils.createHermitePolynomial(2), "-2 + 4 x^2");
104         checkPolynomial(PolynomialsUtils.createHermitePolynomial(1), "2 x");
105         checkPolynomial(PolynomialsUtils.createHermitePolynomial(0), "1");
106 
107         checkPolynomial(PolynomialsUtils.createHermitePolynomial(7), "-1680 x + 3360 x^3 - 1344 x^5 + 128 x^7");
108         checkPolynomial(PolynomialsUtils.createHermitePolynomial(6), "-120 + 720 x^2 - 480 x^4 + 64 x^6");
109         checkPolynomial(PolynomialsUtils.createHermitePolynomial(5), "120 x - 160 x^3 + 32 x^5");
110         checkPolynomial(PolynomialsUtils.createHermitePolynomial(4), "12 - 48 x^2 + 16 x^4");
111 
112     }
113 
114     @Test
115     void testHermiteDifferentials() {
116         for (int k = 0; k < 12; ++k) {
117 
118             PolynomialFunction Hk0 = PolynomialsUtils.createHermitePolynomial(k);
119             PolynomialFunction Hk1 = Hk0.polynomialDerivative();
120             PolynomialFunction Hk2 = Hk1.polynomialDerivative();
121 
122             PolynomialFunction g0 = new PolynomialFunction(new double[] { 2 * k });
123             PolynomialFunction g1 = new PolynomialFunction(new double[] { 0, -2 });
124             PolynomialFunction g2 = new PolynomialFunction(new double[] { 1 });
125 
126             PolynomialFunction Hk0g0 = Hk0.multiply(g0);
127             PolynomialFunction Hk1g1 = Hk1.multiply(g1);
128             PolynomialFunction Hk2g2 = Hk2.multiply(g2);
129 
130             checkNullPolynomial(Hk0g0.add(Hk1g1.add(Hk2g2)));
131 
132         }
133     }
134 
135     @Test
136     void testHermiteOrthogonality() {
137         UnivariateFunction weight = x -> FastMath.exp(-x * x);
138         for (int i = 0; i < 10; ++i) {
139             PolynomialFunction pi = PolynomialsUtils.createHermitePolynomial(i);
140             for (int j = 0; j <= i; ++j) {
141                 PolynomialFunction pj = PolynomialsUtils.createHermitePolynomial(j);
142                 checkOrthogonality(pi, pj, weight, -50, 50, 1.5, 1.0e-8);
143             }
144         }
145     }
146 
147     @Test
148     void testFirstLaguerrePolynomials() {
149         checkPolynomial(PolynomialsUtils.createLaguerrePolynomial(3), 6l, "6 - 18 x + 9 x^2 - x^3");
150         checkPolynomial(PolynomialsUtils.createLaguerrePolynomial(2), 2l, "2 - 4 x + x^2");
151         checkPolynomial(PolynomialsUtils.createLaguerrePolynomial(1), 1l, "1 - x");
152         checkPolynomial(PolynomialsUtils.createLaguerrePolynomial(0), 1l, "1");
153 
154         checkPolynomial(PolynomialsUtils.createLaguerrePolynomial(7), 5040l,
155                 "5040 - 35280 x + 52920 x^2 - 29400 x^3"
156                 + " + 7350 x^4 - 882 x^5 + 49 x^6 - x^7");
157         checkPolynomial(PolynomialsUtils.createLaguerrePolynomial(6),  720l,
158                 "720 - 4320 x + 5400 x^2 - 2400 x^3 + 450 x^4"
159                 + " - 36 x^5 + x^6");
160         checkPolynomial(PolynomialsUtils.createLaguerrePolynomial(5),  120l,
161         "120 - 600 x + 600 x^2 - 200 x^3 + 25 x^4 - x^5");
162         checkPolynomial(PolynomialsUtils.createLaguerrePolynomial(4),   24l,
163         "24 - 96 x + 72 x^2 - 16 x^3 + x^4");
164 
165     }
166 
167     @Test
168     void testLaguerreDifferentials() {
169         for (int k = 0; k < 12; ++k) {
170 
171             PolynomialFunction Lk0 = PolynomialsUtils.createLaguerrePolynomial(k);
172             PolynomialFunction Lk1 = Lk0.polynomialDerivative();
173             PolynomialFunction Lk2 = Lk1.polynomialDerivative();
174 
175             PolynomialFunction g0 = new PolynomialFunction(new double[] { k });
176             PolynomialFunction g1 = new PolynomialFunction(new double[] { 1, -1 });
177             PolynomialFunction g2 = new PolynomialFunction(new double[] { 0, 1 });
178 
179             PolynomialFunction Lk0g0 = Lk0.multiply(g0);
180             PolynomialFunction Lk1g1 = Lk1.multiply(g1);
181             PolynomialFunction Lk2g2 = Lk2.multiply(g2);
182 
183             checkNullPolynomial(Lk0g0.add(Lk1g1.add(Lk2g2)));
184 
185         }
186     }
187 
188     @Test
189     void testLaguerreOrthogonality() {
190         UnivariateFunction weight = x -> FastMath.exp(-x);
191         for (int i = 0; i < 10; ++i) {
192             PolynomialFunction pi = PolynomialsUtils.createLaguerrePolynomial(i);
193             for (int j = 0; j <= i; ++j) {
194                 PolynomialFunction pj = PolynomialsUtils.createLaguerrePolynomial(j);
195                 checkOrthogonality(pi, pj, weight, 0.0, 100.0, 0.99999, 1.0e-13);
196             }
197         }
198     }
199 
200     @Test
201     void testFirstLegendrePolynomials() {
202         checkPolynomial(PolynomialsUtils.createLegendrePolynomial(3),  2l, "-3 x + 5 x^3");
203         checkPolynomial(PolynomialsUtils.createLegendrePolynomial(2),  2l, "-1 + 3 x^2");
204         checkPolynomial(PolynomialsUtils.createLegendrePolynomial(1),  1l, "x");
205         checkPolynomial(PolynomialsUtils.createLegendrePolynomial(0),  1l, "1");
206 
207         checkPolynomial(PolynomialsUtils.createLegendrePolynomial(7), 16l, "-35 x + 315 x^3 - 693 x^5 + 429 x^7");
208         checkPolynomial(PolynomialsUtils.createLegendrePolynomial(6), 16l, "-5 + 105 x^2 - 315 x^4 + 231 x^6");
209         checkPolynomial(PolynomialsUtils.createLegendrePolynomial(5),  8l, "15 x - 70 x^3 + 63 x^5");
210         checkPolynomial(PolynomialsUtils.createLegendrePolynomial(4),  8l, "3 - 30 x^2 + 35 x^4");
211 
212     }
213 
214     @Test
215     void testLegendreDifferentials() {
216         for (int k = 0; k < 12; ++k) {
217 
218             PolynomialFunction Pk0 = PolynomialsUtils.createLegendrePolynomial(k);
219             PolynomialFunction Pk1 = Pk0.polynomialDerivative();
220             PolynomialFunction Pk2 = Pk1.polynomialDerivative();
221 
222             PolynomialFunction g0 = new PolynomialFunction(new double[] { k * (k + 1) });
223             PolynomialFunction g1 = new PolynomialFunction(new double[] { 0, -2 });
224             PolynomialFunction g2 = new PolynomialFunction(new double[] { 1, 0, -1 });
225 
226             PolynomialFunction Pk0g0 = Pk0.multiply(g0);
227             PolynomialFunction Pk1g1 = Pk1.multiply(g1);
228             PolynomialFunction Pk2g2 = Pk2.multiply(g2);
229 
230             checkNullPolynomial(Pk0g0.add(Pk1g1.add(Pk2g2)));
231 
232         }
233     }
234 
235     @Test
236     void testLegendreOrthogonality() {
237         UnivariateFunction weight = x -> 1;
238         for (int i = 0; i < 10; ++i) {
239             PolynomialFunction pi = PolynomialsUtils.createLegendrePolynomial(i);
240             for (int j = 0; j <= i; ++j) {
241                 PolynomialFunction pj = PolynomialsUtils.createLegendrePolynomial(j);
242                 checkOrthogonality(pi, pj, weight, -1, 1, 0.1, 1.0e-13);
243             }
244         }
245     }
246 
247     @Test
248     void testHighDegreeLegendre() {
249         PolynomialsUtils.createLegendrePolynomial(40);
250         double[] l40 = PolynomialsUtils.createLegendrePolynomial(40).getCoefficients();
251         double denominator = 274877906944d;
252         double[] numerators = new double[] {
253                           +34461632205d,            -28258538408100d,          +3847870979902950d,        -207785032914759300d,
254                   +5929294332103310025d,     -103301483474866556880d,    +1197358103913226000200d,    -9763073770369381232400d,
255               +58171647881784229843050d,  -260061484647976556945400d,  +888315281771246239250340d, -2345767627188139419665400d,
256             +4819022625419112503443050d, -7710436200670580005508880d, +9566652323054238154983240d, -9104813935044723209570256d,
257             +6516550296251767619752905d, -3391858621221953912598660d, +1211378079007840683070950d,  -265365894974690562152100d,
258               +26876802183334044115405d
259         };
260         for (int i = 0; i < l40.length; ++i) {
261             if (i % 2 == 0) {
262                 double ci = numerators[i / 2] / denominator;
263                 assertEquals(ci, l40[i], FastMath.abs(ci) * 1e-15);
264             } else {
265                 assertEquals(0, l40[i], 0);
266             }
267         }
268     }
269 
270     @Test
271     void testNegativeDegree() {
272         try {
273             PolynomialsUtils.createChebyshevPolynomial(-1);
274             fail("Expected MathIllegalArgumentException");
275         } catch (org.hipparchus.exception.MathIllegalArgumentException e) {
276             // expected
277         }
278     }
279 
280     @Test
281     void testJacobiLegendre() {
282         for (int i = 0; i < 10; ++i) {
283             PolynomialFunction legendre = PolynomialsUtils.createLegendrePolynomial(i);
284             PolynomialFunction jacobi   = PolynomialsUtils.createJacobiPolynomial(i, 0, 0);
285             checkNullPolynomial(legendre.subtract(jacobi));
286         }
287     }
288 
289     @Test
290     void testJacobiEvaluationAt1() {
291         for (int v = 0; v < 10; ++v) {
292             for (int w = 0; w < 10; ++w) {
293                 for (int i = 0; i < 10; ++i) {
294                     PolynomialFunction jacobi = PolynomialsUtils.createJacobiPolynomial(i, v, w);
295                     double binomial = CombinatoricsUtils.binomialCoefficient(v + i, i);
296                     assertTrue(Precision.equals(binomial, jacobi.value(1.0), 1));
297                 }
298             }
299         }
300     }
301 
302     @Test
303     void testJacobiOrthogonality() {
304         for (int v = 0; v < 5; ++v) {
305             for (int w = v; w < 5; ++w) {
306                 final int vv = v;
307                 final int ww = w;
308                 UnivariateFunction weight = x -> FastMath.pow(1 - x, vv) * FastMath.pow(1 + x, ww);
309                 for (int i = 0; i < 10; ++i) {
310                     PolynomialFunction pi = PolynomialsUtils.createJacobiPolynomial(i, v, w);
311                     for (int j = 0; j <= i; ++j) {
312                         PolynomialFunction pj = PolynomialsUtils.createJacobiPolynomial(j, v, w);
313                         checkOrthogonality(pi, pj, weight, -1, 1, 0.1, 1.0e-12);
314                     }
315                 }
316             }
317         }
318     }
319 
320     /** {@link JacobiKey} coverage tests.
321      * @since 3.1
322      */
323     @Test
324     void testJacobiKeyCoverage() {
325         
326         final JacobiKey key = new JacobiKey(3, 4);
327 
328         assertNotEquals(null, key);
329         assertNotEquals(key, new Object());
330         assertNotEquals(key, new JacobiKey(3, 5));
331         assertNotEquals(key, new JacobiKey(5, 4));
332         assertNotEquals(key, new JacobiKey(5, 5));
333         assertEquals(key, new JacobiKey(3, 4));
334     }
335 
336     @Test
337     void testShift() {
338         // f1(x) = 1 + x + 2 x^2
339         PolynomialFunction f1x = new PolynomialFunction(new double[] { 1, 1, 2 });
340 
341         PolynomialFunction f1x1
342             = new PolynomialFunction(PolynomialsUtils.shift(f1x.getCoefficients(), 1));
343         checkPolynomial(f1x1, "4 + 5 x + 2 x^2");
344 
345         PolynomialFunction f1xM1
346             = new PolynomialFunction(PolynomialsUtils.shift(f1x.getCoefficients(), -1));
347         checkPolynomial(f1xM1, "2 - 3 x + 2 x^2");
348 
349         PolynomialFunction f1x3
350             = new PolynomialFunction(PolynomialsUtils.shift(f1x.getCoefficients(), 3));
351         checkPolynomial(f1x3, "22 + 13 x + 2 x^2");
352 
353         // f2(x) = 2 + 3 x^2 + 8 x^3 + 121 x^5
354         PolynomialFunction f2x = new PolynomialFunction(new double[]{2, 0, 3, 8, 0, 121});
355 
356         PolynomialFunction f2x1
357             = new PolynomialFunction(PolynomialsUtils.shift(f2x.getCoefficients(), 1));
358         checkPolynomial(f2x1, "134 + 635 x + 1237 x^2 + 1218 x^3 + 605 x^4 + 121 x^5");
359 
360         PolynomialFunction f2x3
361             = new PolynomialFunction(PolynomialsUtils.shift(f2x.getCoefficients(), 3));
362         checkPolynomial(f2x3, "29648 + 49239 x + 32745 x^2 + 10898 x^3 + 1815 x^4 + 121 x^5");
363 
364         // Zero shift
365         double[] coeffs = { 1.0, 2.0, 3.0 };
366         Assertions.assertArrayEquals(coeffs, PolynomialsUtils.shift(coeffs, 0), 1e-15);
367 
368         // Empty coeffs
369         double[] empty = new double[0];
370         Assertions.assertArrayEquals(empty, PolynomialsUtils.shift(empty, 1.0), 1e-15);
371 
372         // Single coeff
373         double[] single = { 5.0 };
374         Assertions.assertArrayEquals(single, PolynomialsUtils.shift(single, 10.0), 1e-15);
375 
376         // Null coeffs
377         try {
378             PolynomialsUtils.shift(null, 1.0);
379             fail("Expected NullPointerException");
380         } catch (NullPointerException e) {
381             // expected
382         }
383     }
384 
385 
386     private void checkPolynomial(PolynomialFunction p, long denominator, String reference) {
387         PolynomialFunction q = new PolynomialFunction(new double[] { denominator});
388         assertEquals(reference, p.multiply(q).toString());
389     }
390 
391     private void checkPolynomial(PolynomialFunction p, String reference) {
392         assertEquals(reference, p.toString());
393     }
394 
395     private void checkNullPolynomial(PolynomialFunction p) {
396         for (double coefficient : p.getCoefficients()) {
397             assertEquals(0, coefficient, 1e-13);
398         }
399     }
400 
401     private void checkOrthogonality(final PolynomialFunction p1,
402                                     final PolynomialFunction p2,
403                                     final UnivariateFunction weight,
404                                     final double a, final double b,
405                                     final double nonZeroThreshold,
406                                     final double zeroThreshold) {
407         UnivariateFunction f = x -> weight.value(x) * p1.value(x) * p2.value(x);
408         double dotProduct =
409                 new IterativeLegendreGaussIntegrator(5, 1.0e-9, 1.0e-8, 2, 15).integrate(1000000, f, a, b);
410         if (p1.degree() == p2.degree()) {
411             // integral should be non-zero
412             assertTrue(FastMath.abs(dotProduct) > nonZeroThreshold,
413                               "I(" + p1.degree() + ", " + p2.degree() + ") = "+ dotProduct);
414         } else {
415             // integral should be zero
416             assertEquals(0.0, FastMath.abs(dotProduct), zeroThreshold, "I(" + p1.degree() + ", " + p2.degree() + ") = "+ dotProduct);
417         }
418     }
419 }