| 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 |
package dr.math.functionEval; |
| 27 |
|
| 28 |
import java.io.PrintStream; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public final class DrMath |
| 36 |
{ |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
static private double defaultNumericalPrecision = 0; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
static private double smallNumber = 0; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
static private int radix = 0; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
static private double machinePrecision = 0; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
static private double negativeMachinePrecision = 0; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
static private double smallestNumber = 0; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
static private double largestNumber = 0; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
static private double largestExponentialArgument = 0; |
| 69 |
|
| 70 |
|
| 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 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public static boolean equal( double a, double b) |
| 151 |
{ |
| 152 |
return equal( a, b, defaultNumericalPrecision()); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 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 |
|
| 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 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
public static double roundTo( double value, double scale) |
| 238 |
{ |
| 239 |
return Math.round( value / scale) * scale; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 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; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
public static double smallNumber() |
| 277 |
{ |
| 278 |
if ( smallNumber == 0 ) |
| 279 |
smallNumber = Math.sqrt( getSmallestNumber()); |
| 280 |
return smallNumber; |
| 281 |
} |
| 282 |
} |