1 package JSci.maths; 2 3 10 public final class ExtraMath extends AbstractMath { 11 private ExtraMath() {} 12 13 18 public static double round(final double x, final int significant) { 19 if(x == 0.0) 20 return x; 21 else if(significant == 0) 22 return 0.0; 23 final double signedExp = log10(Math.abs(x)) - significant; 24 if(signedExp < 0.0) { 25 final double factor = Math.pow(10.0, Math.floor(-signedExp)); 27 return Math.round(x*factor)/factor; 28 } else { 29 final double factor = Math.pow(10.0, Math.ceil(signedExp)); 30 return Math.round(x/factor)*factor; 31 } 32 } 33 36 public static double random(double min, double max) { 37 return (max-min)*Math.random()+min; 38 } 39 43 public static int sign(double x) { 44 if(x > 0.0) 45 return 1; 46 else if(x < 0.0) 47 return -1; 48 else 49 return 0; 50 } 51 54 public static double hypot(final double x,final double y) { 55 final double xAbs=Math.abs(x); 56 final double yAbs=Math.abs(y); 57 if(xAbs==0.0 && yAbs==0.0) 58 return 0.0; 59 else if(xAbs<yAbs) 60 return yAbs*Math.sqrt(1.0+(x/y)*(x/y)); 61 else 62 return xAbs*Math.sqrt(1.0+(y/x)*(y/x)); 63 } 64 69 public static int pow(int a, int b) { 70 if(b < 0) { 71 throw new IllegalArgumentException (b+" must be a positive integer."); 72 } else if(b == 0) { 73 return 1; 74 } else { 75 if(a == 0) { 76 return 0; 77 } else if(a == 1) { 78 return 1; 79 } else if(a == 2) { 80 return 1<<b; 81 } else { 82 for(int i=1; i<b; i++) 83 a *= a; 84 return a; 85 } 86 } 87 } 88 92 public static int pow2(int a) { 93 return 1<<a; 94 } 95 101 public static double factorial(double x) { 102 return SpecialMath.gamma(x+1.0); 103 } 104 110 public static double logFactorial(double x) { 111 return SpecialMath.logGamma(x+1.0); 112 } 113 120 public static int binomial(int n,int k) { 121 if(k == n || k ==0) 122 return 1; 123 else if(n == 0) 124 return 1; 125 else 126 return binomial(n-1, k-1)+binomial(n-1, k); 127 } 128 135 public static double binomial(double n,double k) { 136 return Math.exp(SpecialMath.logGamma(n+1.0)-SpecialMath.logGamma(k+1.0)-SpecialMath.logGamma(n-k+1.0)); 137 } 138 142 public static double log10(double x) { 143 return Math.log(x)/NumericalConstants.LOG10; 144 } 145 149 public static double sinh(double x) { 150 return (Math.exp(x)-Math.exp(-x))/2.0; 151 } 152 156 public static double cosh(double x) { 157 return (Math.exp(x)+Math.exp(-x))/2.0; 158 } 159 163 public static double tanh(double x) { 164 return sinh(x)/cosh(x); 165 } 166 167 183 public static double 184 coth(double x) { 185 return 1.0D/tanh(x); 186 } 188 203 public static double 204 csch(double x) { 205 return 1.0D/sinh(x); 206 } 208 221 public static double 222 sech(double x) { 223 return 1.0D/cosh(x); 224 } 226 239 public static double 240 asinh(double x) { 241 return Double.isInfinite(x) ? x : (x == 0.0) ? x : Math.log(x+Math.sqrt(x*x+1.0)); 244 } 246 260 public static double 261 acosh(double x) { 262 return Math.log(x + Math.sqrt(x*x - 1.0D)); 263 } 265 278 public static double 279 atanh(double x) { 280 return (x != 0.0) ? (Math.log(1.0D + x)-Math.log(1.0D - x))/2.0D : x; 282 } 284 297 public static double 298 acoth(double x) { 299 if (Double.isInfinite(x)) return (x < 0.0) ? -0.0D : +0.0D; 301 return (x == -1.0D) ? Double.NEGATIVE_INFINITY : (Math.log(x+1.0) - Math.log(x - 1.0D))/2.0D; 303 } 305 319 public static double 320 acsch(double x) { 321 323 if (Double.isInfinite(x)) return (x < 0.0) ? -0.0D : +0.0D; 324 return (x == 0.0D) ? 1.0/x : Math.log((1.0D + sign(x)*Math.sqrt(x*x+1.0))/x); 326 } 328 341 public static double 342 asech(double x) { 343 return (x == 0.0D) ? 1.0/x : Math.log((1.0D + Math.sqrt(1.0D - x*x))/x); 345 } } 347 | Popular Tags |