View Javadoc
1   /*
2    * Licensed to the Hipparchus project 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 Hipparchus project 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  package org.hipparchus.analysis.polynomials;
18  
19  import org.hipparchus.exception.MathIllegalArgumentException;
20  import org.hipparchus.util.Binary64;
21  import org.hipparchus.util.Binary64Field;
22  import org.hipparchus.util.MathArrays;
23  import org.junit.jupiter.api.Test;
24  import org.junit.jupiter.params.ParameterizedTest;
25  import org.junit.jupiter.params.provider.ValueSource;
26  
27  import static org.junit.jupiter.api.Assertions.*;
28  
29  class FieldPolynomialFunctionLagrangeFormTest {
30  
31      @Test
32      void testExceptionSize() {
33          // GIVEN
34          final Binary64Field field = Binary64Field.getInstance();
35          final Binary64[] x = MathArrays.buildArray(field, 1);
36          final Binary64[] y = x.clone();
37          // WHEN & THEN
38          assertThrows(MathIllegalArgumentException.class, () -> new FieldPolynomialFunctionLagrangeForm<>(x, y));
39      }
40  
41      @Test
42      void testExceptionOrder() {
43          // GIVEN
44          final Binary64Field field = Binary64Field.getInstance();
45          final Binary64[] x = MathArrays.buildArray(field, 2);
46          x[0] = field.getOne();
47          x[1] = field.getZero();
48          final Binary64[] y = x.clone();
49          // WHEN & THEN
50          assertThrows(MathIllegalArgumentException.class, () -> new FieldPolynomialFunctionLagrangeForm<>(x, y));
51      }
52  
53      @Test
54      void testGetter() {
55          // GIVEN
56          final Binary64Field field = Binary64Field.getInstance();
57          Binary64[] x = { field.getOne(), field.getOne().multiply(2) };
58          Binary64[] y = { field.getOne().negate(), field.getOne().multiply(4.) };
59          final FieldPolynomialFunctionLagrangeForm<Binary64> lagrangeForm = new FieldPolynomialFunctionLagrangeForm<>(x, y);
60          // WHEN & THEN
61          assertArrayEquals(x, lagrangeForm.getInterpolatingPoints());
62          assertArrayEquals(y, lagrangeForm.getInterpolatingValues());
63          final Binary64[] expected = MathArrays.buildArray(field, 2);
64          expected[0] = field.getOne().multiply(-6.);
65          expected[1] = field.getOne().multiply(5.);
66          assertArrayEquals(expected, lagrangeForm.getCoefficients());
67      }
68  
69      @Test
70      void testGetCoefficients() {
71          // GIVEN
72          final Binary64Field field = Binary64Field.getInstance();
73          Binary64[] x = { field.getOne(), field.getOne().multiply(2) };
74          Binary64[] y = { field.getOne().negate(), field.getOne().multiply(4.) };
75          final FieldPolynomialFunctionLagrangeForm<Binary64> lagrangeForm = new FieldPolynomialFunctionLagrangeForm<>(x, y);
76          // WHEN
77          final Binary64[] coefficients = lagrangeForm.getCoefficients();
78          // WHEN & THEN
79          assertArrayEquals(lagrangeForm.getCoefficients(), coefficients);
80      }
81  
82      @ParameterizedTest
83      @ValueSource(ints = {2, 3, 4, 5, 6, 7, 8, 9, 10})
84      void testValueDegree(final int n) {
85          // GIVEN
86          final Binary64Field field = Binary64Field.getInstance();
87          final Binary64[] x = MathArrays.buildArray(field, n);
88          final Binary64[] y = MathArrays.buildArray(field, n);
89          for (int i = 0; i < n; i++) {
90              x[i] = field.getOne().multiply(i);
91              y[i] = field.getOne().multiply(2 * i + 1);
92          }
93          final FieldPolynomialFunctionLagrangeForm<Binary64> lagrangeForm = new FieldPolynomialFunctionLagrangeForm<>(x, y);
94          final Binary64 five = field.getOne().multiply(5);
95          // WHEN
96          final Binary64 fieldValue = lagrangeForm.value(five);
97          // WHEN & THEN
98          final double[] xDouble = new double[n];
99          final double[] yDouble = new double[n];
100         for (int i = 0; i < n; i++) {
101             xDouble[i] = x[i].getReal();
102             yDouble[i] = y[i].getReal();
103         }
104         assertEquals(PolynomialFunctionLagrangeForm.evaluate(xDouble, yDouble, five.getReal()),
105                 fieldValue.getReal());
106     }
107 
108     @ParameterizedTest
109     @ValueSource(doubles = {2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.6, 11})
110     void testValuePoint(final double z) {
111         // GIVEN
112         final Binary64Field field = Binary64Field.getInstance();
113         final Binary64[] x = MathArrays.buildArray(field, 10);
114         final Binary64[] y = MathArrays.buildArray(field, 10);
115         for (int i = 0; i < x.length; i++) {
116             x[i] = field.getOne().multiply(i);
117             y[i] = field.getOne().multiply(2 * i + 1);
118         }
119         final FieldPolynomialFunctionLagrangeForm<Binary64> lagrangeForm = new FieldPolynomialFunctionLagrangeForm<>(x, y);
120         // WHEN
121         final Binary64 fieldValue = lagrangeForm.value(new Binary64(z));
122         // WHEN & THEN
123         final double[] xDouble = new double[x.length];
124         final double[] yDouble = xDouble.clone();
125         for (int i = 0; i < xDouble.length; i++) {
126             xDouble[i] = x[i].getReal();
127             yDouble[i] = y[i].getReal();
128         }
129         assertEquals(PolynomialFunctionLagrangeForm.evaluate(xDouble, yDouble, z), fieldValue.getReal());
130     }
131 }