public static double dnbinom_mu(double x, double size, double mu, boolean give_log) { /* originally, just set prob := size / (size + mu) and called dbinom_raw(), * but that suffers from cancellation when mu << size */ double ans, p; if (DoubleVector.isNaN(x) || DoubleVector.isNaN(size) || DoubleVector.isNaN(mu)) { return x + size + mu; } if (mu < 0 || size < 0) { return DoubleVector.NaN; } // R_D_nonint_check(x); if (SignRank.R_D_nonint(x, true, give_log)) { // MATHLIB_WARNING("non-integer x = %f", x); // How to warn?? return SignRank.R_D__0(true, give_log); } if (x < 0 || !DoubleVector.isFinite(x)) { return SignRank.R_D__0(true, give_log); } x = SignRank.R_D_forceint(x); if (x == 0) /* be accerate, both for n << mu, and n >> mu :*/ { return SignRank.R_D_exp( size * (size < mu ? Math.log(size / (size + mu)) : Math.log1p(-mu / (size + mu))), true, give_log); } if (x < 1e-10 * size) { /* don't use dbinom_raw() but MM's formula: */ /* FIXME --- 1e-8 shows problem; rather use algdiv() from ./toms708.c */ return SignRank.R_D_exp( x * Math.log(size * mu / (size + mu)) - mu - org.apache.commons.math.special.Gamma.logGamma(x + 1) + Math.log1p(x * (x - 1) / (2 * size)), true, give_log); } /* else: no unnecessary cancellation inside dbinom_raw, when * x_ = size and n_ = x+size are so close that n_ - x_ loses accuracy */ ans = dbinom_raw(size, x + size, size / (size + mu), mu / (size + mu), give_log); p = ((double) size) / (size + x); return ((give_log) ? Math.log(p) + ans : p * ans); }
/** Computes the probability <SPAN CLASS="MATH"><I>p</I>(<I>x</I>)</SPAN>. */ public static double prob(double n, double p, int x) { final int SLIM = 15; // To avoid overflow final double MAXEXP = (Num.DBL_MAX_EXP - 1) * Num.LN2; // To avoid overflow final double MINEXP = (Num.DBL_MIN_EXP - 1) * Num.LN2; // To avoid underflow double y; if (p < 0.0 || p > 1.0) throw new IllegalArgumentException("p not in [0, 1]"); if (n <= 0.0) throw new IllegalArgumentException("n <= 0.0"); if (x < 0) return 0.0; if (p >= 1.0) { // In fact, p == 1 if (x == 0) return 1.0; else return 0.0; } if (p <= 0.0) // In fact, p == 0 return 0.0; y = Num.lnGamma(n + x) - (Num.lnFactorial(x) + Num.lnGamma(n)) + n * Math.log(p) + x * Math.log1p(-p); if (y >= MAXEXP) throw new IllegalArgumentException("term overflow"); return Math.exp(y); }
/** {@inheritDoc} */ public OpenMapRealVector mapLog1pToSelf() { Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); entries.put(iter.key(), Math.log1p(iter.value())); } return this; }
private static float[] ComputeGamma(int[] lo, int[] md, int[] hi) { float[] array = new float[3]; for (int i = 0; i < 3; i++) { if (lo[i] < md[i] && md[i] < hi[i]) { double log = Math.log1p(/*0.5, */ (double) (((float) (md[i] - lo[i])) / ((float) (hi[i] - lo[i])))); array[i] = (log > 10.0) ? ((float) 10.0) : ((log < 0.1) ? ((float) 0.1) : ((float) log)); } else { array[i] = 1f; } } return array; }
public static double pnbeta2( double x, double o_x, double a, double b, double ncp, boolean lower_tail, boolean log_p) { double ans = pnbeta_raw(x, o_x, a, b, ncp); /* return R_DT_val(ans), but we want to warn about cancellation here */ if (lower_tail) { return log_p ? Math.log(ans) : ans; } else { if (ans > 1 - 1e-10) { return (DoubleVector.NaN); } ans = Math.min(ans, 1.0); /* Precaution */ return log_p ? Math.log1p(-ans) : (1 - ans); } }
/** * Computes the inverse of the distribution function. * * @param xi * @param beta * @param tau * @param y * @return */ public static double inverseF(double xi, double beta, double tau, double y) { if (tau <= 0.0) throw new InvalidParameterException("tau <= 0"); if (y < 0.0 || y > 1.0) throw new InvalidParameterException("y not in [0,1]"); if (y <= 0.0) return beta; if (y >= 1.0 && xi >= 0) { return Double.POSITIVE_INFINITY; } if (y >= 1.0 && xi < 0) { return beta - tau / xi; } if (xi == 0) { return beta - tau * Math.log1p(-y); } return beta + tau / xi * (-1 + Math.pow(1 - y, -xi)); }
private double calculateWeight(JobInProgress job, TaskType taskType) { if (!isRunnable(job)) { return 0; } else { double weight = 1.0; if (sizeBasedWeight) { // Set weight based on runnable tasks weight = Math.log1p(runnableTasks(job, taskType)) / Math.log(2); } weight *= getPriorityFactor(job.getPriority()); if (weightAdjuster != null) { // Run weight through the user-supplied weightAdjuster weight = weightAdjuster.adjustWeight(job, taskType, weight); } return weight; } }
public static void main(String[] args) { for (int i = 0; i <= 10; i++) { System.out.println((int) Math.pow(2, i)); // üs alma } System.out.println("\n1024 karekökü : " + (int) Math.sqrt(1204)); // karekö alma System.out.println(" - 45 in mutlak deðerini alalým : " + Math.abs(-45)); System.out.println("3.55 üst deðere yuvarla : " + Math.ceil(3.55)); System.out.println("3.55 alt degerini bulalým : " + Math.floor(3.55)); System.out.println("35 ve 155 arasýndaki büyük sayi : " + Math.max(35, 155)); System.out.println("25 ve 244 arasýndaki küþük sayi : " + Math.min(25, 244)); System.out.println( "rastgele sayi : " + Math.random()); // dönecek deðer 0 ile 1 arasýnda double deðer döner. System.out.println( "rastgele çýkan sayýyý 10 ile çarpýmsonucu : " + (int) (Math.random() * 100)); Random r = new Random(); int a = r.nextInt(100); float f = r.nextFloat(); System.out.println("Sonucumuz : " + a); System.out.println( "randon sýnýfýnda rastgele float deðer: " + f); // 0 ile 1 arasý bir float deðer verir. System.out.println("1.5 radyan : " + Math.toDegrees(1.5) + " derecedir"); System.out.println("45 derece : " + Math.toRadians(45) + " radyandýr."); System.out.println("sin 90 = " + Math.sin(Math.toRadians(90))); System.out.println("cos 55 = " + Math.cos(Math.toRadians(55))); System.out.println("Argsin 0.47 = " + Math.toDegrees(Math.asin(0.47))); System.out.println("ArgCos 0.57 = " + Math.toDegrees(Math.acos(0.57))); System.out.println("ln e = " + Math.log(Math.E)); System.out.println("log10(5) = " + Math.log10(5)); System.out.println("ln(7+1) = " + Math.log1p(7)); }
/** * Return the probability density for a particular point. * * @param x The point at which the density should be computed. * @return The pdf at point x. * @since 2.1 */ @Override public double density(double x) { recomputeZ(); if (x < 0 || x > 1) { return 0; } else if (x == 0) { if (alpha < 1) { throw MathRuntimeException.createIllegalArgumentException( "Cannot compute beta density at 0 when alpha = {0,number}", alpha); } return 0; } else if (x == 1) { if (beta < 1) { throw MathRuntimeException.createIllegalArgumentException( "Cannot compute beta density at 1 when beta = %.3g", beta); } return 0; } else { double logX = Math.log(x); double log1mX = Math.log1p(-x); return Math.exp((alpha - 1) * logX + (beta - 1) * log1mX - z); } }
public static double dbinom_raw(double x, double n, double p, double q, boolean give_log) { double lf, lc; if (p == 0) { return ((x == 0) ? SignRank.R_D__1(true, give_log) : SignRank.R_D__0(true, give_log)); } if (q == 0) { return ((x == n) ? SignRank.R_D__1(true, give_log) : SignRank.R_D__0(true, give_log)); } if (x == 0) { if (n == 0) { return SignRank.R_D__1(true, give_log); } lc = (p < 0.1) ? -bd0(n, n * q) - n * p : n * Math.log(q); return (SignRank.R_D_exp(lc, true, give_log)); } if (x == n) { lc = (q < 0.1) ? -bd0(n, n * p) - n * q : n * Math.log(p); return (SignRank.R_D_exp(lc, true, give_log)); } if (x < 0 || x > n) { return (SignRank.R_D__0(true, give_log)); } /* n*p or n*q can underflow to zero if n and p or q are small. This used to occur in dbeta, and gives NaN as from R 2.3.0. */ lc = stirlerr(n) - stirlerr(x) - stirlerr(n - x) - bd0(x, n * p) - bd0(n - x, n * q); /* f = (M_2PI*x*(n-x))/n; could overflow or underflow */ /* Upto R 2.7.1: * lf = log(M_2PI) + log(x) + log(n-x) - log(n); * -- following is much better for x << n : */ lf = Math.log(2.0 * Math.PI) + Math.log(x) + Math.log1p(-x / n); return SignRank.R_D_exp(lc - 0.5 * lf, true, give_log); }
static { for (int i = 0; i < 0x10000; i++) logTable[i] = (float) Math.log1p(i); }
// To add/remove functions change evaluateOperator() and registration public double evaluateFunction(String fncnam, ArgParser fncargs) throws ArithmeticException { switch (Character.toLowerCase(fncnam.charAt(0))) { case 'a': { if (fncnam.equalsIgnoreCase("abs")) { return Math.abs(fncargs.next()); } if (fncnam.equalsIgnoreCase("acos")) { return Math.acos(fncargs.next()); } if (fncnam.equalsIgnoreCase("asin")) { return Math.asin(fncargs.next()); } if (fncnam.equalsIgnoreCase("atan")) { return Math.atan(fncargs.next()); } } break; case 'c': { if (fncnam.equalsIgnoreCase("cbrt")) { return Math.cbrt(fncargs.next()); } if (fncnam.equalsIgnoreCase("ceil")) { return Math.ceil(fncargs.next()); } if (fncnam.equalsIgnoreCase("cos")) { return Math.cos(fncargs.next()); } if (fncnam.equalsIgnoreCase("cosh")) { return Math.cosh(fncargs.next()); } } break; case 'e': { if (fncnam.equalsIgnoreCase("exp")) { return Math.exp(fncargs.next()); } if (fncnam.equalsIgnoreCase("expm1")) { return Math.expm1(fncargs.next()); } } break; case 'f': { if (fncnam.equalsIgnoreCase("floor")) { return Math.floor(fncargs.next()); } } break; case 'g': { // if(fncnam.equalsIgnoreCase("getExponent" )) { return // Math.getExponent(fncargs.next()); } needs Java 6 } break; case 'l': { if (fncnam.equalsIgnoreCase("log")) { return Math.log(fncargs.next()); } if (fncnam.equalsIgnoreCase("log10")) { return Math.log10(fncargs.next()); } if (fncnam.equalsIgnoreCase("log1p")) { return Math.log1p(fncargs.next()); } } break; case 'm': { if (fncnam.equalsIgnoreCase("max")) { return Math.max(fncargs.next(), fncargs.next()); } if (fncnam.equalsIgnoreCase("min")) { return Math.min(fncargs.next(), fncargs.next()); } } break; case 'n': { // if(fncnam.equalsIgnoreCase("nextUp" )) { return Math.nextUp // (fncargs.next()); } needs Java 6 } break; case 'r': { if (fncnam.equalsIgnoreCase("random")) { return Math.random(); } // impure if (fncnam.equalsIgnoreCase("round")) { return Math.round(fncargs.next()); } if (fncnam.equalsIgnoreCase("roundHE")) { return Math.rint(fncargs.next()); } // round half-even } break; case 's': { if (fncnam.equalsIgnoreCase("signum")) { return Math.signum(fncargs.next()); } if (fncnam.equalsIgnoreCase("sin")) { return Math.sin(fncargs.next()); } if (fncnam.equalsIgnoreCase("sinh")) { return Math.sinh(fncargs.next()); } if (fncnam.equalsIgnoreCase("sqrt")) { return Math.sqrt(fncargs.next()); } } break; case 't': { if (fncnam.equalsIgnoreCase("tan")) { return Math.tan(fncargs.next()); } if (fncnam.equalsIgnoreCase("tanh")) { return Math.tanh(fncargs.next()); } if (fncnam.equalsIgnoreCase("toDegrees")) { return Math.toDegrees(fncargs.next()); } if (fncnam.equalsIgnoreCase("toRadians")) { return Math.toRadians(fncargs.next()); } } break; case 'u': { if (fncnam.equalsIgnoreCase("ulp")) { return Math.ulp(fncargs.next()); } } break; // no default } throw new UnsupportedOperationException( "MathEval internal function setup is incorrect - internal function \"" + fncnam + "\" not handled"); }
@Override public Object visit(RealUnaryExpression n, Void arg) { Double doubleObject = (Double) n.getOperand().accept(this, null); double doubleVal = doubleObject.doubleValue(); Operator op = n.getOperator(); switch (op) { case ABS: return Math.abs(doubleVal); case ACOS: return Math.acos(doubleVal); case ASIN: return Math.asin(doubleVal); case ATAN: return Math.atan(doubleVal); case CBRT: return Math.cbrt(doubleVal); case CEIL: return Math.ceil(doubleVal); case COS: return Math.cos(doubleVal); case COSH: return Math.cosh(doubleVal); case EXP: return Math.exp(doubleVal); case EXPM1: return Math.expm1(doubleVal); case FLOOR: return Math.floor(doubleVal); case LOG: return Math.log(doubleVal); case LOG10: return Math.log10(doubleVal); case LOG1P: return Math.log1p(doubleVal); case NEG: return -doubleVal; case NEXTUP: return Math.nextUp(doubleVal); case RINT: return Math.rint(doubleVal); case SIGNUM: return Math.signum(doubleVal); case SIN: return Math.sin(doubleVal); case SINH: return Math.sinh(doubleVal); case SQRT: return Math.sqrt(doubleVal); case TAN: return Math.tan(doubleVal); case TANH: return Math.tanh(doubleVal); case TODEGREES: return Math.toDegrees(doubleVal); case TORADIANS: return Math.toRadians(doubleVal); case ULP: return Math.ulp(doubleVal); default: log.warn("RealUnaryExpression: unimplemented operator: " + op); return null; } }
public static double R_Log1_Exp(double x, boolean lower_tail, boolean log_p) { return ((x) > -Math.log(2.0) ? Math.log(-Math.expm1(x)) : Math.log1p(-Math.exp(x))); }
public static double R_D_LExp(double x, boolean lower_tail, boolean log_p) { return (log_p ? R_Log1_Exp(x, lower_tail, log_p) : Math.log1p(-x)); }
public static double R_D_Clog(double p, boolean lower_tail, boolean log_p) { return (log_p ? Math.log1p(-(p)) : (0.5 - (p) + 0.5)); }
public static double pnbeta_raw(double x, double o_x, double a, double b, double ncp) { /* o_x == 1 - x but maybe more accurate */ /* change errmax and itrmax if desired; * original (AS 226, R84) had (errmax; itrmax) = (1e-6; 100) */ final double errmax = 1.0e-9; final int itrmax = 10000; /* 100 is not enough for pf(ncp=200) see PR#11277 */ double[] temp = new double[1]; double[] tmp_c = new double[1]; int[] ierr = new int[1]; double a0, ax, lbeta, c, errbd, x0; int j; double ans, gx, q, sumq; if (ncp < 0. || a <= 0. || b <= 0.) { return DoubleVector.NaN; } if (x < 0. || o_x > 1. || (x == 0. && o_x == 1.)) { return 0.; } if (x > 1. || o_x < 0. || (x == 1. && o_x == 0.)) { return 1.; } c = ncp / 2.; /* initialize the series */ x0 = Math.floor(Math.max(c - 7. * Math.sqrt(c), 0.)); a0 = a + x0; lbeta = org.apache.commons.math.special.Gamma.logGamma(a0) + org.apache.commons.math.special.Gamma.logGamma(b) - org.apache.commons.math.special.Gamma.logGamma(a0 + b); /* temp = pbeta_raw(x, a0, b, TRUE, FALSE), but using (x, o_x): */ Utils.bratio(a0, b, x, o_x, temp, tmp_c, ierr, false); gx = Math.exp( a0 * Math.log(x) + b * (x < .5 ? Math.log1p(-x) : Math.log(o_x)) - lbeta - Math.log(a0)); if (a0 > a) { q = Math.exp(-c + x0 * Math.log(c) - org.apache.commons.math.special.Gamma.logGamma(x0 + 1.)); } else { q = Math.exp(-c); } sumq = 1. - q; ans = ax = q * temp[0]; /* recurse over subsequent terms until convergence is achieved */ j = (int) x0; do { j++; temp[0] -= gx; gx *= x * (a + b + j - 1.) / (a + j); q *= c / j; sumq -= q; ax = temp[0] * q; ans += ax; errbd = (temp[0] - gx) * sumq; } while (errbd > errmax && j < itrmax + x0); if (errbd > errmax) { // ML_ERROR(ME_PRECISION, "pnbeta"); } if (j >= itrmax + x0) { // ML_ERROR(ME_NOCONV, "pnbeta"); } return ans; }
/** * Calculates base e logarithm for the given value. * * @param d value * @return base e logarithm for d */ private double ln(final double d) { return d == 0 ? 0 : Math.log1p(Math.abs(d)); }