1 // ******************************************************************************
2 //
3 // Title: Force Field X.
4 // Description: Force Field X - Software for Molecular Biophysics.
5 // Copyright: Copyright (c) Michael J. Schnieders 2001-2025.
6 //
7 // This file is part of Force Field X.
8 //
9 // Force Field X is free software; you can redistribute it and/or modify it
10 // under the terms of the GNU General Public License version 3 as published by
11 // the Free Software Foundation.
12 //
13 // Force Field X is distributed in the hope that it will be useful, but WITHOUT
14 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 // details.
17 //
18 // You should have received a copy of the GNU General Public License along with
19 // Force Field X; if not, write to the Free Software Foundation, Inc., 59 Temple
20 // Place, Suite 330, Boston, MA 02111-1307 USA
21 //
22 // Linking this library statically or dynamically with other modules is making a
23 // combined work based on this library. Thus, the terms and conditions of the
24 // GNU General Public License cover the whole combination.
25 //
26 // As a special exception, the copyright holders of this library give you
27 // permission to link this library with independent modules to produce an
28 // executable, regardless of the license terms of these independent modules, and
29 // to copy and distribute the resulting executable under terms of your choice,
30 // provided that you also meet, for each linked independent module, the terms
31 // and conditions of the license of that module. An independent module is a
32 // module which is not derived from or based on this library. If you modify this
33 // library, you may extend this exception to your version of the library, but
34 // you are not obligated to do so. If you do not wish to do so, delete this
35 // exception statement from your version.
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 * @author Michael J. Schnieders
52 */
53 @RunWith(Parameterized.class)
54 public class ErfTest extends FFXTest {
55
56 /**
57 * Java double precision follows the IEEE 754 Binary Floating-Point Arithmetic standard. Each
58 * double consumes 8 bytes of storage and offers 52 binary digits of precision (14-15 decimal
59 * digits). This implementation of Erf passes for a tolerance of 1.0e-15 and (as one might expect)
60 * fails using 1.0e-16.
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 * The expected values were found to 20 decimal points of precision using Mathematica:
76 * Erf[SetPrecision[x, 20]] Erfc[SetPrecision[x, 20]]
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 // Very small value tests
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 // Very large value tests
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 // Negative value tests (erf is an odd function: erf(-x) = -erf(x))
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 * Test of erf method, of class Erf.
111 */
112 @Test
113 public void testErf() {
114 double actual = Erf.erf(x);
115 Assert.assertEquals(info, expected, actual, tolerance);
116 }
117
118 /**
119 * Test of erfc method, of class Erf.
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 * Test of erf method with special values.
129 */
130 @Test
131 public void testErfSpecialCases() {
132 // Test NaN - not using parameterized test to avoid issues with NaN comparison
133 double nanResult = Erf.erf(Double.NaN);
134 Assert.assertTrue("erf(NaN) should be NaN", Double.isNaN(nanResult));
135
136 // Test positive infinity
137 double posInfResult = Erf.erf(Double.POSITIVE_INFINITY);
138 Assert.assertEquals("erf(+Infinity) should be 1.0", 1.0, posInfResult, 0.0);
139
140 // Test negative infinity
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 * Test of erfc method with special values.
147 */
148 @Test
149 public void testErfcSpecialCases() {
150 // Test NaN - not using parameterized test to avoid issues with NaN comparison
151 double nanResult = Erf.erfc(Double.NaN);
152 Assert.assertTrue("erfc(NaN) should be NaN", Double.isNaN(nanResult));
153
154 // Test positive infinity
155 double posInfResult = Erf.erfc(Double.POSITIVE_INFINITY);
156 Assert.assertEquals("erfc(+Infinity) should be 0.0", 0.0, posInfResult, 0.0);
157
158 // Test negative infinity
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 }