Debian Codegraph @

src/dr/math/functionEval/DrMath.java

raw symbols SCIP
1 /*
2 * DrMath.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.math.functionEval;
27
28 import java.io.PrintStream;
29 /**
30 * This class implements additional mathematical functions
31 * and determines the parameters of the floating point representation.
32 *
33 * @author Didier H. Besset
34 */
35 public final class DrMath
36 {
37 /**
38 * Typical meaningful precision for numerical calculations.
39 */
40 static private double defaultNumericalPrecision = 0;
41 /**
42 * Typical meaningful small number for numerical calculations.
43 */
44 static private double smallNumber = 0;
45 /**
46 * Radix used by floating-point numbers.
47 */
48 static private int radix = 0;
49 /**
50 * Largest positive value which, when added to 1.0, yields 0.
51 */
52 static private double machinePrecision = 0;
53 /**
54 * Largest positive value which, when subtracted to 1.0, yields 0.
55 */
56 static private double negativeMachinePrecision = 0;
57 /**
58 * Smallest number different from zero.
59 */
60 static private double smallestNumber = 0;
61 /**
62 * Largest possible number
63 */
64 static private double largestNumber = 0;
65 /**
66 * Largest argument for the exponential
67 */
68 static private double largestExponentialArgument = 0;
69 /**
70 * Values used to compute human readable scales.
71 */
72 private static final double scales[] = {1.25, 2, 2.5, 4, 5, 7.5, 8, 10};
73 private static final double semiIntegerScales[] = {2, 2.5, 4, 5, 7.5, 8, 10};
74 private static final double integerScales[] = {2, 4, 5, 8, 10};
75
76 private static void computeLargestNumber()
77 {
78 double floatingRadix = getRadix();
79 double fullMantissaNumber = 1.0d -
80 floatingRadix * getNegativeMachinePrecision();
81 while ( !Double.isInfinite( fullMantissaNumber) )
82 {
83 largestNumber = fullMantissaNumber;
84 fullMantissaNumber *= floatingRadix;
85 }
86 }
87 private static void computeMachinePrecision()
88 {
89 double floatingRadix = getRadix();
90 double inverseRadix = 1.0d / floatingRadix;
91 machinePrecision = 1.0d;
92 double tmp = 1.0d + machinePrecision;
93 while ( tmp - 1.0d != 0.0d)
94 {
95 machinePrecision *= inverseRadix;
96 tmp = 1.0d + machinePrecision;
97 }
98 }
99 private static void computeNegativeMachinePrecision()
100 {
101 double floatingRadix = getRadix();
102 double inverseRadix = 1.0d / floatingRadix;
103 negativeMachinePrecision = 1.0d;
104 double tmp = 1.0d - negativeMachinePrecision;
105 while ( tmp - 1.0d != 0.0d)
106 {
107 negativeMachinePrecision *= inverseRadix;
108 tmp = 1.0d - negativeMachinePrecision;
109 }
110 }
111 private static void computeRadix()
112 {
113 double a = 1.0d;
114 double tmp1, tmp2;
115 do { a += a;
116 tmp1 = a + 1.0d;
117 tmp2 = tmp1 - a;
118 } while ( tmp2 - 1.0d != 0.0d);
119 double b = 1.0d;
120 while ( radix == 0)
121 {
122 b += b;
123 tmp1 = a + b;
124 radix = (int) ( tmp1 - a);
125 }
126 }
127 private static void computeSmallestNumber()
128 {
129 double floatingRadix = getRadix();
130 double inverseRadix = 1.0d / floatingRadix;
131 double fullMantissaNumber = 1.0d - floatingRadix * getNegativeMachinePrecision();
132 while ( fullMantissaNumber != 0.0d )
133 {
134 smallestNumber = fullMantissaNumber;
135 fullMantissaNumber *= inverseRadix;
136 }
137 }
138 public static double defaultNumericalPrecision()
139 {
140 if ( defaultNumericalPrecision == 0 )
141 defaultNumericalPrecision = Math.sqrt( getMachinePrecision());
142 return defaultNumericalPrecision;
143 }
144 /**
145 * @return boolean true if the difference between a and b is
146 * less than the default numerical precision
147 * @param a double
148 * @param b double
149 */
150 public static boolean equal( double a, double b)
151 {
152 return equal( a, b, defaultNumericalPrecision());
153 }
154 /**
155 * @return boolean true if the relative difference between a and b
156 * is less than precision
157 * @param a double
158 * @param b double
159 * @param precision double
160 */
161 public static boolean equal( double a, double b, double precision)
162 {
163 double norm = Math.max( Math.abs(a), Math.abs( b));
164 return norm < precision || Math.abs( a - b) < precision * norm;
165 }
166 public static double getLargestExponentialArgument()
167 {
168 if ( largestExponentialArgument == 0 )
169 largestExponentialArgument = Math.log(getLargestNumber());
170 return largestExponentialArgument;
171 }
172 /**
173 * (c) Copyrights Didier BESSET, 1999, all rights reserved.
174 */
175 public static double getLargestNumber()
176 {
177 if ( largestNumber == 0 )
178 computeLargestNumber();
179 return largestNumber;
180 }
181 public static double getMachinePrecision()
182 {
183 if ( machinePrecision == 0 )
184 computeMachinePrecision();
185 return machinePrecision;
186 }
187 public static double getNegativeMachinePrecision()
188 {
189 if ( negativeMachinePrecision == 0 )
190 computeNegativeMachinePrecision();
191 return negativeMachinePrecision;
192 }
193 public static int getRadix()
194 {
195 if ( radix == 0 )
196 computeRadix();
197 return radix;
198 }
199 public static double getSmallestNumber()
200 {
201 if ( smallestNumber == 0 )
202 computeSmallestNumber();
203 return smallestNumber;
204 }
205 public static void printParameters( PrintStream printStream)
206 {
207 printStream.println( "Floating-point machine parameters");
208 printStream.println( "---------------------------------");
209 printStream.println( " ");
210 printStream.println( "radix = "+ getRadix());
211 printStream.println( "Machine precision = "
212 + getMachinePrecision());
213 printStream.println( "Negative machine precision = "
214 + getNegativeMachinePrecision());
215 printStream.println( "Smallest number = "+ getSmallestNumber());
216 printStream.println( "Largest number = "+ getLargestNumber());
217 return;
218 }
219 public static void reset()
220 {
221 defaultNumericalPrecision = 0;
222 smallNumber = 0;
223 radix = 0;
224 machinePrecision = 0;
225 negativeMachinePrecision = 0;
226 smallestNumber = 0;
227 largestNumber = 0;
228 }
229 /**
230 * This method returns the specified value rounded to
231 * the nearest integer multiple of the specified scale.
232 *
233 * @param value number to be rounded
234 * @param scale defining the rounding scale
235 * @return rounded value
236 */
237 public static double roundTo( double value, double scale)
238 {
239 return Math.round( value / scale) * scale;
240 }
241 /**
242 * Round the specified value upward to the next scale value.
243 * @param the value to be rounded.
244 * @param a fag specified whether integer scale are used, otherwise double scale is used.
245 * @return a number rounded upward to the next scale value.
246 */
247 public static double roundToScale( double value, boolean integerValued)
248 {
249 double[] scaleValues;
250 int orderOfMagnitude = (int) Math.floor( Math.log( value) / Math.log( 10.0));
251 if ( integerValued )
252 {
253 orderOfMagnitude = Math.max( 1, orderOfMagnitude);
254 if ( orderOfMagnitude == 1)
255 scaleValues = integerScales;
256 else if ( orderOfMagnitude == 2)
257 scaleValues = semiIntegerScales;
258 else
259 scaleValues = scales;
260 }
261 else
262 scaleValues = scales;
263 double exponent = Math.pow( 10.0, orderOfMagnitude);
264 double rValue = value / exponent;
265 for ( int n = 0; n < scaleValues.length; n++)
266 {
267 if ( rValue <= scaleValues[n])
268 return scaleValues[n] * exponent;
269 }
270 return exponent; // Should never reach here
271 }
272 /**
273 * (c) Copyrights Didier BESSET, 1999, all rights reserved.
274 * @return double
275 */
276 public static double smallNumber()
277 {
278 if ( smallNumber == 0 )
279 smallNumber = Math.sqrt( getSmallestNumber());
280 return smallNumber;
281 }
282 }