1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 package ffx.numerics.special;
39
40 import ffx.utilities.FFXTest;
41 import org.junit.Assert;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.junit.runners.Parameterized;
45 import org.junit.runners.Parameterized.Parameters;
46
47 import java.util.Arrays;
48 import java.util.Collection;
49
50
51
52
53 @RunWith(Parameterized.class)
54 public class ErfTest extends FFXTest {
55
56
57
58
59
60
61
62 private static final double tolerance = 1.0e-15;
63
64 private final String info;
65 private final double x;
66 private final double expected;
67
68 public ErfTest(String info, double x, double expected) {
69 this.info = info;
70 this.x = x;
71 this.expected = expected;
72 }
73
74
75
76
77
78 @Parameters
79 public static Collection<Object[]> data() {
80 return Arrays.asList(
81 new Object[][]{
82 {"Test 0.0", 0.0e0, 0.0e0},
83
84
85 {"Test 1.0e-16; near xSmall threshold.", 1.0e-16, 1.128379167095513e-16},
86 {"Test 1.0e-10; small value.", 1.0e-10, 1.128379167095513e-10},
87
88 {"Test 0.1; below the first branch point.", 0.1e0, 0.1124629160182849},
89 {"Test 0.46; just below first branch point.", 0.46, 0.4846553900016797},
90 {"Test 0.46875; at the first branch point.", 0.46875, 0.492613473217938},
91 {"Test 0.47; just above first branch point.", 0.47, 0.4937450508860821},
92 {"Test 1.0; between the branch points.", 1.0e0, 0.842700792949715},
93 {"Test 3.9; just below second branch point.", 3.9, 1.0 - 3.479224859723177e-8},
94 {"Test 4.0; at the second branch point.", 4.0, 1.0 - 1.5417257900280018852e-8},
95 {"Test 4.1; just above second branch point.", 4.1, 1.0 - 6.700027654084919e-9},
96 {"Test 5.0; above the second branch point.", 5.0e0, 1.0e0 - 1.5374597944280348502e-12},
97
98
99 {"Test 10.0; large value.", 10.0e0, 1.0 - 2.088487583762545e-45},
100 {"Test 26.0; near xBig threshold.", 26.0, 1.0e0},
101
102
103 {"Test -0.1; negative value below first branch point.", -0.1e0, -0.1124629160182849},
104 {"Test -1.0; negative value between branch points.", -1.0e0, -0.842700792949715},
105 {"Test -5.0; negative value above second branch point.", -5.0e0, -1.0e0 + 1.5374597944280348502e-12}
106 });
107 }
108
109
110
111
112 @Test
113 public void testErf() {
114 double actual = Erf.erf(x);
115 Assert.assertEquals(info, expected, actual, tolerance);
116 }
117
118
119
120
121 @Test
122 public void testErfc() {
123 double actual = Erf.erfc(x);
124 Assert.assertEquals(info, 1.0 - expected, actual, tolerance);
125 }
126
127
128
129
130 @Test
131 public void testErfSpecialCases() {
132
133 double nanResult = Erf.erf(Double.NaN);
134 Assert.assertTrue("erf(NaN) should be NaN", Double.isNaN(nanResult));
135
136
137 double posInfResult = Erf.erf(Double.POSITIVE_INFINITY);
138 Assert.assertEquals("erf(+Infinity) should be 1.0", 1.0, posInfResult, 0.0);
139
140
141 double negInfResult = Erf.erf(Double.NEGATIVE_INFINITY);
142 Assert.assertEquals("erf(-Infinity) should be -1.0", -1.0, negInfResult, 0.0);
143 }
144
145
146
147
148 @Test
149 public void testErfcSpecialCases() {
150
151 double nanResult = Erf.erfc(Double.NaN);
152 Assert.assertTrue("erfc(NaN) should be NaN", Double.isNaN(nanResult));
153
154
155 double posInfResult = Erf.erfc(Double.POSITIVE_INFINITY);
156 Assert.assertEquals("erfc(+Infinity) should be 0.0", 0.0, posInfResult, 0.0);
157
158
159 double negInfResult = Erf.erfc(Double.NEGATIVE_INFINITY);
160 Assert.assertEquals("erfc(-Infinity) should be 2.0", 2.0, negInfResult, 0.0);
161 }
162
163 }