📄 mathx.java
字号:
* <li>n is negative and s is positive</li> * <li>n is positive and s is negative</li> * </ul> * In all such cases, Double.NaN is returned. * @param n * @param s * @return */ public static double ceiling(double n, double s) { double c; if ((n<0 && s>0) || (n>0 && s<0)) { c = Double.NaN; } else { c = (n == 0 || s == 0) ? 0 : Math.ceil(n/s) * s; } return c; } /** * <br/> for all n >= 1; factorial n = n * (n-1) * (n-2) * ... * 1 * <br/> else if n == 0; factorial n = 1 * <br/> else if n < 0; factorial n = Double.NaN * <br/> Loss of precision can occur if n is large enough. * If n is large so that the resulting value would be greater * than Double.MAX_VALUE; Double.POSITIVE_INFINITY is returned. * If n < 0, Double.NaN is returned. * @param n * @return */ public static double factorial(int n) { double d = 1; if (n >= 0) { if (n <= 170) { for (int i=1; i<=n; i++) { d *= i; } } else { d = Double.POSITIVE_INFINITY; } } else { d = Double.NaN; } return d; } /** * returns the remainder resulting from operation: * n / d. * <br/> The result has the sign of the divisor. * <br/> Examples: * <ul> * <li>mod(3.4, 2) = 1.4</li> * <li>mod(-3.4, 2) = 0.6</li> * <li>mod(-3.4, -2) = -1.4</li> * <li>mod(3.4, -2) = -0.6</li> * </ul> * If d == 0, result is NaN * @param n * @param d * @return */ public static double mod(double n, double d) { double result = 0; if (d == 0) { result = Double.NaN; } else if (sign(n) == sign(d)) { double t = Math.abs(n / d); t = t - (long) t; result = sign(d) * Math.abs(t * d); } else { double t = Math.abs(n / d); t = t - (long) t; t = Math.ceil(t) - t; result = sign(d) * Math.abs(t * d); } return result; } /** * inverse hyperbolic cosine * @param d * @return */ public static double acosh(double d) { return Math.log(Math.sqrt(Math.pow(d, 2) - 1) + d); } /** * inverse hyperbolic sine * @param d * @return */ public static double asinh(double d) { double d2 = d*d; return Math.log(Math.sqrt(d*d + 1) + d); } /** * inverse hyperbolic tangent * @param d * @return */ public static double atanh(double d) { return Math.log((1 + d)/(1 - d)) / 2; } /** * hyperbolic cosine * @param d * @return */ public static double cosh(double d) { double ePowX = Math.pow(Math.E, d); double ePowNegX = Math.pow(Math.E, -d); d = (ePowX + ePowNegX) / 2; return d; } /** * hyperbolic sine * @param d * @return */ public static double sinh(double d) { double ePowX = Math.pow(Math.E, d); double ePowNegX = Math.pow(Math.E, -d); d = (ePowX - ePowNegX) / 2; return d; } /** * hyperbolic tangent * @param d * @return */ public static double tanh(double d) { double ePowX = Math.pow(Math.E, d); double ePowNegX = Math.pow(Math.E, -d); d = (ePowX - ePowNegX) / (ePowX + ePowNegX); return d; } /** * returns the sum of product of corresponding double value in each * subarray. It is the responsibility of the caller to ensure that * all the subarrays are of equal length. If the subarrays are * not of equal length, the return value can be unpredictable. * @param arrays * @return */ public static double sumproduct(double[][] arrays) { double d = 0; try { int narr = arrays.length; int arrlen = arrays[0].length; for (int j=0; j<arrlen; j++) { double t = 1; for (int i=0; i<narr; i++) { t *= arrays[i][j]; } d += t; } } catch (ArrayIndexOutOfBoundsException ae) { d = Double.NaN; } return d; } /** * returns the sum of difference of squares of corresponding double * value in each subarray: ie. sigma (xarr[i]^2-yarr[i]^2) * <br/> * It is the responsibility of the caller * to ensure that the two subarrays are of equal length. If the * subarrays are not of equal length, the return value can be * unpredictable. * @param arrays * @return */ public static double sumx2my2(double[] xarr, double[] yarr) { double d = 0; try { for (int i=0, iSize=xarr.length; i<iSize; i++) { d += (xarr[i] + yarr[i]) * (xarr[i] - yarr[i]); } } catch (ArrayIndexOutOfBoundsException ae) { d = Double.NaN; } return d; } /** * returns the sum of sum of squares of corresponding double * value in each subarray: ie. sigma (xarr[i]^2 + yarr[i]^2) * <br/> * It is the responsibility of the caller * to ensure that the two subarrays are of equal length. If the * subarrays are not of equal length, the return value can be * unpredictable. * @param arrays * @return */ public static double sumx2py2(double[] xarr, double[] yarr) { double d = 0; try { for (int i=0, iSize=xarr.length; i<iSize; i++) { d += (xarr[i] * xarr[i]) + (yarr[i] * yarr[i]); } } catch (ArrayIndexOutOfBoundsException ae) { d = Double.NaN; } return d; } /** * returns the sum of squares of difference of corresponding double * value in each subarray: ie. sigma ( (xarr[i]-yarr[i])^2 ) * <br/> * It is the responsibility of the caller * to ensure that the two subarrays are of equal length. If the * subarrays are not of equal length, the return value can be * unpredictable. * @param arrays * @return */ public static double sumxmy2(double[] xarr, double[] yarr) { double d = 0; try { for (int i=0, iSize=xarr.length; i<iSize; i++) { double t = (xarr[i] - yarr[i]); d += t * t; } } catch (ArrayIndexOutOfBoundsException ae) { d = Double.NaN; } return d; } /** * returns the total number of combinations possible when * k items are chosen out of total of n items. If the number * is too large, loss of precision may occur (since returned * value is double). If the returned value is larger than * Double.MAX_VALUE, Double.POSITIVE_INFINITY is returned. * If either of the parameters is negative, Double.NaN is returned. * @param n * @param k * @return */ public static double nChooseK(int n, int k) { double d = 1; if (n<0 || k<0 || n<k) { d= Double.NaN; } else { int minnk = Math.min(n-k, k); int maxnk = Math.max(n-k, k); for (int i=maxnk; i<n; i++) { d *= i+1; } d /= factorial(minnk); } return d; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -