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.integration.gauss;
23
24 import org.hipparchus.analysis.CalculusFieldUnivariateFunction;
25 import org.hipparchus.util.Binary64;
26 import org.hipparchus.util.Binary64Field;
27 import org.hipparchus.util.FastMath;
28 import org.junit.Assert;
29 import org.junit.Test;
30
31
32
33
34
35 public class FieldHermiteTest {
36 private static final FieldGaussIntegratorFactory<Binary64> factory = new FieldGaussIntegratorFactory<>(Binary64Field.getInstance());
37
38 @Test
39 public void testNormalDistribution() {
40 final Binary64 oneOverSqrtPi = new Binary64(1 / FastMath.sqrt(Math.PI));
41
42
43
44 final int numPoints = 1;
45
46
47
48
49
50
51
52 final CalculusFieldUnivariateFunction<Binary64> f = y -> oneOverSqrtPi;
53
54 final FieldGaussIntegrator<Binary64> integrator = factory.hermite(numPoints);
55 final double result = integrator.integrate(f).getReal();
56 final double expected = 1;
57 Assert.assertEquals(expected, result, FastMath.ulp(expected));
58 }
59
60 @Test
61 public void testNormalMean() {
62 final Binary64 sqrtTwo = new Binary64(FastMath.sqrt(2));
63 final Binary64 oneOverSqrtPi = new Binary64(1 / FastMath.sqrt(Math.PI));
64
65 final Binary64 mu = new Binary64(12345.6789);
66 final Binary64 sigma = new Binary64(987.654321);
67 final int numPoints = 6;
68
69
70
71
72
73
74
75 final CalculusFieldUnivariateFunction<Binary64> f =
76 y -> oneOverSqrtPi.multiply(sqrtTwo.multiply(sigma).multiply(y).add(mu));
77
78 final FieldGaussIntegrator<Binary64> integrator = factory.hermite(numPoints);
79 final double result = integrator.integrate(f).getReal();
80 final double expected = mu.getReal();
81 Assert.assertEquals(expected, result, 5 * FastMath.ulp(expected));
82 }
83
84 @Test
85 public void testNormalVariance() {
86 final Binary64 twoOverSqrtPi = new Binary64(2 / FastMath.sqrt(Math.PI));
87
88 final Binary64 sigma = new Binary64(987.654321);
89 final Binary64 sigma2 = sigma.multiply(sigma);
90 final int numPoints = 5;
91
92
93
94
95
96
97
98 final CalculusFieldUnivariateFunction<Binary64> f =
99 y -> twoOverSqrtPi.multiply(sigma2).multiply(y).multiply(y);
100
101 final FieldGaussIntegrator<Binary64> integrator = factory.hermite(numPoints);
102 final double result = integrator.integrate(f).getReal();
103 final double expected = sigma2.getReal();
104 Assert.assertEquals(expected, result, 10 * FastMath.ulp(expected));
105 }
106 }