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