1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.analysis.interpolation;
23
24 import org.hipparchus.UnitTestUtils;
25 import org.hipparchus.analysis.CalculusFieldUnivariateFunction;
26 import org.hipparchus.analysis.UnivariateFunction;
27 import org.hipparchus.analysis.polynomials.FieldPolynomialFunction;
28 import org.hipparchus.analysis.polynomials.FieldPolynomialSplineFunction;
29 import org.hipparchus.analysis.polynomials.PolynomialFunction;
30 import org.hipparchus.analysis.polynomials.PolynomialSplineFunction;
31 import org.hipparchus.util.Binary64;
32 import org.junit.Test;
33
34
35
36
37 public class LinearInterpolatorTest extends UnivariateInterpolatorAbstractTest {
38
39 protected UnivariateInterpolator buildDoubleInterpolator() {
40 return new LinearInterpolator();
41 }
42
43 protected FieldUnivariateInterpolator buildFieldInterpolator() {
44 return new LinearInterpolator();
45 }
46
47 @Test
48 public void testInterpolateLinear() {
49 double[] x = { 0.0, 0.5, 1.0 };
50 double[] y = { 0.0, 0.5, 0.0 };
51 UnivariateInterpolator i = buildDoubleInterpolator();
52 UnivariateFunction f = i.interpolate(x, y);
53 verifyInterpolation(f, x, y);
54
55
56 PolynomialFunction[] polynomials = ((PolynomialSplineFunction) f).getPolynomials();
57 double[] target = {y[0], 1d};
58 UnitTestUtils.assertEquals(polynomials[0].getCoefficients(), target, coefficientTolerance);
59 target = new double[]{y[1], -1d};
60 UnitTestUtils.assertEquals(polynomials[1].getCoefficients(), target, coefficientTolerance);
61 }
62
63 @Test
64 public void testInterpolateLinearD64() {
65 Binary64[] x = buildD64(0.0, 0.5, 1.0);
66 Binary64[] y = buildD64(0.0, 0.5, 0.0);
67 FieldUnivariateInterpolator i = buildFieldInterpolator();
68 CalculusFieldUnivariateFunction<Binary64> f = i.interpolate(x, y);
69 verifyInterpolation(f, x, y);
70
71
72 FieldPolynomialFunction<Binary64>[] polynomials = ((FieldPolynomialSplineFunction<Binary64>) f).getPolynomials();
73 checkCoeffs(coefficientTolerance, polynomials[0], y[0].getReal(), +1.0);
74 checkCoeffs(coefficientTolerance, polynomials[1], y[1].getReal(), -1.0);
75 }
76
77 }