View Javadoc
1   // ******************************************************************************
2   //
3   // Title:       Force Field X.
4   // Description: Force Field X - Software for Molecular Biophysics.
5   // Copyright:   Copyright (c) Michael J. Schnieders 2001-2024.
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 java.util.Arrays;
41  import java.util.Collection;
42  
43  import ffx.utilities.FFXTest;
44  import org.junit.Assert;
45  import org.junit.Test;
46  import org.junit.runner.RunWith;
47  import org.junit.runners.Parameterized;
48  import org.junit.runners.Parameterized.Parameters;
49  
50  /** @author Michael J. Schnieders */
51  @RunWith(Parameterized.class)
52  public class ErfTest extends FFXTest {
53  
54    /**
55     * Java double precision follows the IEEE 754 Binary Floating-Point Arithmetic standard. Each
56     * double consumes 8 bytes of storage and offers 52 binary digits of precision (14-15 decimal
57     * digits). This implementation of Erf passes for a tolerance of 1.0e-15 and (as one might expect)
58     * fails using 1.0e-16.
59     */
60    private static final double tolerance = 1.0e-15;
61  
62    private final String info;
63    private final double x;
64    private final double expected;
65  
66    public ErfTest(String info, double x, double expected) {
67      this.info = info;
68      this.x = x;
69      this.expected = expected;
70    }
71  
72    /**
73     * The expected values were found to 20 decimal points of precision using Mathematica:
74     * Erf[SetPrecision[x, 20]] Erfc[SetPrecision[x, 20]]
75     */
76    @Parameters
77    public static Collection<Object[]> data() {
78      return Arrays.asList(
79          new Object[][] {
80            {"Test 0.0", 0.0e0, 0.0e0},
81            {"Test 0.1; below the first branch point.", 0.1e0, 0.1124629160182848984e0},
82            {"Test 0.46875; at the first branch point.", 0.46875e0, 0.4926134732179379916e0},
83            {"Test 1.0; between the branch points.", 1.0e0, 0.842700792949714869e0},
84            {"Test 4.0; at the second branch point.", 4.0e0, 1.0e0 - 1.5417257900280018852e-8},
85            {"Test 5.0; above the second branch point.", 5.0e0, 1.0e0 - 1.5374597944280348502e-12}
86          });
87    }
88  
89    /** Test of erf method, of class Erf. */
90    @Test
91    public void testErf() {
92      double actual = Erf.erf(x);
93      Assert.assertEquals(info, expected, actual, tolerance);
94    }
95  
96    /** Test of erfc method, of class Erf. */
97    @Test
98    public void testErfc() {
99      double actual = Erf.erfc(x);
100     Assert.assertEquals(info, 1.0 - expected, actual, tolerance);
101   }
102 }