List of usage examples for org.apache.commons.math3.exception MathArithmeticException MathArithmeticException
public MathArithmeticException()
From source file:Armadillo.Analytics.Base.Precision.java
/** * Rounds the given non-negative value to the "nearest" integer. Nearest is * determined by the rounding method specified. Rounding methods are defined * in {@link BigDecimal}.//w w w. j av a2 s . c o m * * @param unscaled Value to round. * @param sign Sign of the original, scaled value. * @param roundingMethod Rounding method, as defined in {@link BigDecimal}. * @return the rounded value. * @throws MathArithmeticException if an exact operation is required but result is not exact * @throws MathIllegalArgumentException if {@code roundingMethod} is not a valid rounding method. * @since 1.1 (previously in {@code MathUtils}, moved as of version 3.0) */ private static double roundUnscaled(double unscaled, double sign, int roundingMethod) throws MathArithmeticException, MathIllegalArgumentException { switch (roundingMethod) { case BigDecimal.ROUND_CEILING: if (sign == -1) { unscaled = FastMath.floor(FastMath.nextAfter(unscaled, Double.NEGATIVE_INFINITY)); } else { unscaled = FastMath.ceil(FastMath.nextAfter(unscaled, Double.POSITIVE_INFINITY)); } break; case BigDecimal.ROUND_DOWN: unscaled = FastMath.floor(FastMath.nextAfter(unscaled, Double.NEGATIVE_INFINITY)); break; case BigDecimal.ROUND_FLOOR: if (sign == -1) { unscaled = FastMath.ceil(FastMath.nextAfter(unscaled, Double.POSITIVE_INFINITY)); } else { unscaled = FastMath.floor(FastMath.nextAfter(unscaled, Double.NEGATIVE_INFINITY)); } break; case BigDecimal.ROUND_HALF_DOWN: { unscaled = FastMath.nextAfter(unscaled, Double.NEGATIVE_INFINITY); double fraction = unscaled - FastMath.floor(unscaled); if (fraction > 0.5) { unscaled = FastMath.ceil(unscaled); } else { unscaled = FastMath.floor(unscaled); } break; } case BigDecimal.ROUND_HALF_EVEN: { double fraction = unscaled - FastMath.floor(unscaled); if (fraction > 0.5) { unscaled = FastMath.ceil(unscaled); } else if (fraction < 0.5) { unscaled = FastMath.floor(unscaled); } else { // The following equality test is intentional and needed for rounding purposes if (FastMath.floor(unscaled) / 2.0 == FastMath.floor(Math.floor(unscaled) / 2.0)) { // even unscaled = FastMath.floor(unscaled); } else { // odd unscaled = FastMath.ceil(unscaled); } } break; } case BigDecimal.ROUND_HALF_UP: { unscaled = FastMath.nextAfter(unscaled, Double.POSITIVE_INFINITY); double fraction = unscaled - FastMath.floor(unscaled); if (fraction >= 0.5) { unscaled = FastMath.ceil(unscaled); } else { unscaled = FastMath.floor(unscaled); } break; } case BigDecimal.ROUND_UNNECESSARY: if (unscaled != FastMath.floor(unscaled)) { throw new MathArithmeticException(); } break; case BigDecimal.ROUND_UP: unscaled = FastMath.ceil(FastMath.nextAfter(unscaled, Double.POSITIVE_INFINITY)); break; default: throw new MathIllegalArgumentException(LocalizedFormats.INVALID_ROUNDING_METHOD, roundingMethod, "ROUND_CEILING", BigDecimal.ROUND_CEILING, "ROUND_DOWN", BigDecimal.ROUND_DOWN, "ROUND_FLOOR", BigDecimal.ROUND_FLOOR, "ROUND_HALF_DOWN", BigDecimal.ROUND_HALF_DOWN, "ROUND_HALF_EVEN", BigDecimal.ROUND_HALF_EVEN, "ROUND_HALF_UP", BigDecimal.ROUND_HALF_UP, "ROUND_UNNECESSARY", BigDecimal.ROUND_UNNECESSARY, "ROUND_UP", BigDecimal.ROUND_UP); } return unscaled; }
From source file:org.gitools.analysis.groupcomparison.format.math33Preview.CombinatoricsUtils.java
/** * Returns n!. Shorthand for {@code n} <a * href="http://mathworld.wolfram.com/Factorial.html"> Factorial</a>, the * product of the numbers {@code 1, ..., n}. * <p>/*from w w w.j a v a 2s .co m*/ * <Strong>Preconditions</strong>: * <ul> * <li> {@code n >= 0} (otherwise * {@code IllegalArgumentException} is thrown)</li> * <li> The result is small enough to fit into a {@code long}. The * largest value of {@code n} for which {@code n!} < * Long.MAX_VALUE} is 20. If the computed value exceeds {@code Long.MAX_VALUE} * an {@code ArithMeticException } is thrown.</li> * </ul> * </p> * * @param n argument * @return {@code n!} * @throws MathArithmeticException if the result is too large to be represented * by a {@code long}. * @throws NotPositiveException if {@code n < 0}. * @throws MathArithmeticException if {@code n > 20}: The factorial value is too * large to fit in a {@code long}. */ public static long factorial(final int n) throws NotPositiveException, MathArithmeticException { if (n < 0) { throw new NotPositiveException(LocalizedFormats.FACTORIAL_NEGATIVE_PARAMETER, n); } if (n > 20) { throw new MathArithmeticException(); } return FACTORIALS[n]; }
From source file:vn.edu.vnu.uet.nlp.smt.ibm.IBMModel3.java
/** * This function returns the probability given an alignment. The phi * variable represents the fertility according to the current alignment, * which records how many output words are generated by each input word. *//*from w w w .ja va 2s. c o m*/ double logProbability(ViterbiAlignment align, SentencePair p) { int[] a = align.getA(); int[] phi = align.getPhi(); int le = p.getE().length(); int lf = p.getF().length(); if (le - 2 * phi[0] <= 0) { return 0.0; } double p1 = 1 - p0; double total = 0.0; // Compute the NULL insertion total += Math.log(Math.pow(p1, phi[0]) * Math.pow(p0, le - 2 * phi[0])); // Compute the combination (le - fert[0]) choose fert[0] for (int i = 1; i <= phi[0]; i++) { total += Math.log((le - phi[0] - i + 1) / i); } // Compute fertilities term for (int i = 0; i <= lf; i++) { int f = p.getF().get(i); try { total += Math.log(CombinatoricsUtils.factorial(phi[i]) * n.get(getFertWord(phi[i], f))); } catch (MathArithmeticException e) { if (phi[i] > 20) { total += Math.log(n.get(getFertWord(phi[i], f))); total += Math.log(CombinatoricsUtils.factorial(20)); for (int tmp = 21; tmp <= phi[i]; tmp++) { total += Math.log(tmp); } } else { throw new MathArithmeticException(); } } } // Multiply the lexical and distortion probabilities for (int j = 1; j <= le; j++) { int i = a[j]; total += Math.log(t.get(p.getWordPair(j, i))); total += Math.log(d[j][i][le][lf]); } return Math.exp(total); }
From source file:vn.edu.vnu.uet.nlp.smt.ibm.IBMModel3.java
/** * This function returns the probability given an alignment. The phi * variable represents the fertility according to the current alignment, * which records how many output words are generated by each input word. */// w w w .j av a2s . c om private double normalProbability(ViterbiAlignment align, SentencePair p) { int[] a = align.getA(); int[] phi = align.getPhi(); int le = p.getE().length(); int lf = p.getF().length(); if (le - 2 * phi[0] <= 0) { return 0.0; } double p1 = 1 - p0; double total = 1.0; // Compute the NULL insertion total *= (Math.pow(p1, phi[0]) * Math.pow(p0, le - 2 * phi[0])); // Compute the combination (le - fert[0]) choose fert[0] for (int i = 1; i <= phi[0]; i++) { total *= ((le - phi[0] - i + 1) / i); if (total == 0.0) { return 0.0; } } // Compute fertilities term for (int i = 0; i <= lf; i++) { int f = p.getF().get(i); try { total *= (CombinatoricsUtils.factorial(phi[i]) * n.get(getFertWord(phi[i], f))); } catch (MathArithmeticException e) { if (phi[i] > 20) { total *= (n.get(getFertWord(phi[i], f))); total *= (CombinatoricsUtils.factorial(20)); for (int tmp = 21; tmp <= phi[i]; tmp++) { total *= (tmp); } } else { throw new MathArithmeticException(); } } } // Multiply the lexical and distortion probabilities for (int j = 1; j <= le; j++) { int i = a[j]; total *= (t.get(p.getWordPair(j, i))); total *= (d[j][i][le][lf]); } return total; }