Java Code Examples for org.apache.commons.math3.exception.util.LocalizedFormats#FACTORIAL_NEGATIVE_PARAMETER

The following examples show how to use org.apache.commons.math3.exception.util.LocalizedFormats#FACTORIAL_NEGATIVE_PARAMETER . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CombinatoricsUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Compute the natural logarithm of the factorial of {@code n}.
 *
 * @param n Argument.
 * @return {@code n!}
 * @throws NotPositiveException if {@code n < 0}.
 */
public static double factorialLog(final int n) throws NotPositiveException {
    if (n < 0) {
        throw new NotPositiveException(LocalizedFormats.FACTORIAL_NEGATIVE_PARAMETER,
                                       n);
    }
    if (n < 21) {
        return FastMath.log(FACTORIALS[n]);
    }
    double logSum = 0;
    for (int i = 2; i <= n; i++) {
        logSum += FastMath.log(i);
    }
    return logSum;
}
 
Example 2
Source File: CombinatoricsUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Compute the natural logarithm of the factorial of {@code n}.
 *
 * @param n Argument.
 * @return {@code n!}
 * @throws NotPositiveException if {@code n < 0}.
 */
public static double factorialLog(final int n) throws NotPositiveException {
    if (n < 0) {
        throw new NotPositiveException(LocalizedFormats.FACTORIAL_NEGATIVE_PARAMETER,
                                       n);
    }
    if (n < 21) {
        return FastMath.log(FACTORIALS[n]);
    }
    double logSum = 0;
    for (int i = 2; i <= n; i++) {
        logSum += FastMath.log(i);
    }
    return logSum;
}
 
Example 3
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Compute the natural logarithm of the factorial of {@code n}.
 *
 * @param n Argument.
 * @return {@code n!}
 * @throws NotPositiveException if {@code n < 0}.
 */
public static double factorialLog(final int n) throws NotPositiveException {
    if (n < 0) {
        throw new NotPositiveException(LocalizedFormats.FACTORIAL_NEGATIVE_PARAMETER,
                                       n);
    }
    if (n < 21) {
        return FastMath.log(FACTORIALS[n]);
    }
    double logSum = 0;
    for (int i = 2; i <= n; i++) {
        logSum += FastMath.log(i);
    }
    return logSum;
}
 
Example 4
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Compute the natural logarithm of the factorial of {@code n}.
 *
 * @param n Argument.
 * @return {@code n!}
 * @throws NotPositiveException if {@code n < 0}.
 */
public static double factorialLog(final int n) throws NotPositiveException {
    if (n < 0) {
        throw new NotPositiveException(LocalizedFormats.FACTORIAL_NEGATIVE_PARAMETER,
                                       n);
    }
    if (n < 21) {
        return FastMath.log(FACTORIALS[n]);
    }
    double logSum = 0;
    for (int i = 2; i <= n; i++) {
        logSum += FastMath.log(i);
    }
    return logSum;
}
 
Example 5
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Compute the natural logarithm of the factorial of {@code n}.
 *
 * @param n Argument.
 * @return {@code n!}
 * @throws NotPositiveException if {@code n < 0}.
 */
public static double factorialLog(final int n) {
    if (n < 0) {
        throw new NotPositiveException(LocalizedFormats.FACTORIAL_NEGATIVE_PARAMETER,
                                       n);
    }
    if (n < 21) {
        return FastMath.log(factorial(n));
    }
    double logSum = 0;
    for (int i = 2; i <= n; i++) {
        logSum += FastMath.log(i);
    }
    return logSum;
}
 
Example 6
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Compute the natural logarithm of the factorial of {@code n}.
 *
 * @param n Argument.
 * @return {@code n!}
 * @throws NotPositiveException if {@code n < 0}.
 */
public static double factorialLog(final int n) {
    if (n < 0) {
        throw new NotPositiveException(LocalizedFormats.FACTORIAL_NEGATIVE_PARAMETER,
                                       n);
    }
    if (n < 21) {
        return FastMath.log(factorial(n));
    }
    double logSum = 0;
    for (int i = 2; i <= n; i++) {
        logSum += FastMath.log(i);
    }
    return logSum;
}
 
Example 7
Source File: CombinatoricsUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Compute the natural logarithm of the factorial of {@code n}.
 *
 * @param n Argument.
 * @return {@code n!}
 * @throws NotPositiveException if {@code n < 0}.
 */
public static double factorialLog(final int n) throws NotPositiveException {
    if (n < 0) {
        throw new NotPositiveException(LocalizedFormats.FACTORIAL_NEGATIVE_PARAMETER,
                                       n);
    }
    if (n < 21) {
        return FastMath.log(FACTORIALS[n]);
    }
    double logSum = 0;
    for (int i = 2; i <= n; i++) {
        logSum += FastMath.log(i);
    }
    return logSum;
}
 
Example 8
Source File: CombinatoricsUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Compute n!, the<a href="http://mathworld.wolfram.com/Factorial.html">
 * factorial</a> of {@code n} (the product of the numbers 1 to n), as a
 * {@code double}.
 * The result should be small enough to fit into a {@code double}: The
 * largest {@code n} for which {@code n! < Double.MAX_VALUE} is 170.
 * If the computed value exceeds {@code Double.MAX_VALUE},
 * {@code Double.POSITIVE_INFINITY} is returned.
 *
 * @param n Argument.
 * @return {@code n!}
 * @throws NotPositiveException if {@code n < 0}.
 */
public static double factorialDouble(final int n) throws NotPositiveException {
    if (n < 0) {
        throw new NotPositiveException(LocalizedFormats.FACTORIAL_NEGATIVE_PARAMETER,
                                       n);
    }
    if (n < 21) {
        return FACTORIALS[n];
    }
    return FastMath.floor(FastMath.exp(CombinatoricsUtils.factorialLog(n)) + 0.5);
}
 
Example 9
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Compute n!, the<a href="http://mathworld.wolfram.com/Factorial.html">
 * factorial</a> of {@code n} (the product of the numbers 1 to n), as a
 * {@code double}.
 * The result should be small enough to fit into a {@code double}: The
 * largest {@code n} for which {@code n! < Double.MAX_VALUE} is 170.
 * If the computed value exceeds {@code Double.MAX_VALUE},
 * {@code Double.POSITIVE_INFINITY} is returned.
 *
 * @param n Argument.
 * @return {@code n!}
 * @throws NotPositiveException if {@code n < 0}.
 */
public static double factorialDouble(final int n) throws NotPositiveException {
    if (n < 0) {
        throw new NotPositiveException(LocalizedFormats.FACTORIAL_NEGATIVE_PARAMETER,
                                       n);
    }
    if (n < 21) {
        return FACTORIALS[n];
    }
    return FastMath.floor(FastMath.exp(ArithmeticUtils.factorialLog(n)) + 0.5);
}
 
Example 10
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Compute n!, the<a href="http://mathworld.wolfram.com/Factorial.html">
 * factorial</a> of {@code n} (the product of the numbers 1 to n), as a
 * {@code double}.
 * The result should be small enough to fit into a {@code double}: The
 * largest {@code n} for which {@code n! < Double.MAX_VALUE} is 170.
 * If the computed value exceeds {@code Double.MAX_VALUE},
 * {@code Double.POSITIVE_INFINITY} is returned.
 *
 * @param n Argument.
 * @return {@code n!}
 * @throws NotPositiveException if {@code n < 0}.
 */
public static double factorialDouble(final int n) throws NotPositiveException {
    if (n < 0) {
        throw new NotPositiveException(LocalizedFormats.FACTORIAL_NEGATIVE_PARAMETER,
                                       n);
    }
    if (n < 21) {
        return FACTORIALS[n];
    }
    return FastMath.floor(FastMath.exp(ArithmeticUtils.factorialLog(n)) + 0.5);
}
 
Example 11
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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>
 * <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];
}
 
Example 12
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Compute n!, the<a href="http://mathworld.wolfram.com/Factorial.html">
 * factorial</a> of {@code n} (the product of the numbers 1 to n), as a
 * {@code double}.
 * The result should be small enough to fit into a {@code double}: The
 * largest {@code n} for which {@code n! < Double.MAX_VALUE} is 170.
 * If the computed value exceeds {@code Double.MAX_VALUE},
 * {@code Double.POSITIVE_INFINITY} is returned.
 *
 * @param n Argument.
 * @return {@code n!}
 * @throws NotPositiveException if {@code n < 0}.
 */
public static double factorialDouble(final int n) {
    if (n < 0) {
        throw new NotPositiveException(LocalizedFormats.FACTORIAL_NEGATIVE_PARAMETER,
                                       n);
    }
    if (n < 21) {
        return factorial(n);
    }
    return FastMath.floor(FastMath.exp(ArithmeticUtils.factorialLog(n)) + 0.5);
}
 
Example 13
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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>
 * <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) {
    if (n < 0) {
        throw new NotPositiveException(LocalizedFormats.FACTORIAL_NEGATIVE_PARAMETER,
                                       n);
    }
    if (n > 20) {
        throw new MathArithmeticException();
    }
    return FACTORIALS[n];
}
 
Example 14
Source File: CombinatoricsUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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>
 * <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];
}
 
Example 15
Source File: CombinatoricsUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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>
 * <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];
}
 
Example 16
Source File: CombinatoricsUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Compute n!, the<a href="http://mathworld.wolfram.com/Factorial.html">
 * factorial</a> of {@code n} (the product of the numbers 1 to n), as a
 * {@code double}.
 * The result should be small enough to fit into a {@code double}: The
 * largest {@code n} for which {@code n! < Double.MAX_VALUE} is 170.
 * If the computed value exceeds {@code Double.MAX_VALUE},
 * {@code Double.POSITIVE_INFINITY} is returned.
 *
 * @param n Argument.
 * @return {@code n!}
 * @throws NotPositiveException if {@code n < 0}.
 */
public static double factorialDouble(final int n) throws NotPositiveException {
    if (n < 0) {
        throw new NotPositiveException(LocalizedFormats.FACTORIAL_NEGATIVE_PARAMETER,
                                       n);
    }
    if (n < 21) {
        return FACTORIALS[n];
    }
    return FastMath.floor(FastMath.exp(CombinatoricsUtils.factorialLog(n)) + 0.5);
}
 
Example 17
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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>
 * <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) {
    if (n < 0) {
        throw new NotPositiveException(LocalizedFormats.FACTORIAL_NEGATIVE_PARAMETER,
                                       n);
    }
    if (n > 20) {
        throw new MathArithmeticException();
    }
    return FACTORIALS[n];
}
 
Example 18
Source File: CombinatoricsUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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>
 * <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];
}
 
Example 19
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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>
 * <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];
}
 
Example 20
Source File: CombinatoricsUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Compute n!, the<a href="http://mathworld.wolfram.com/Factorial.html">
 * factorial</a> of {@code n} (the product of the numbers 1 to n), as a
 * {@code double}.
 * The result should be small enough to fit into a {@code double}: The
 * largest {@code n} for which {@code n! < Double.MAX_VALUE} is 170.
 * If the computed value exceeds {@code Double.MAX_VALUE},
 * {@code Double.POSITIVE_INFINITY} is returned.
 *
 * @param n Argument.
 * @return {@code n!}
 * @throws NotPositiveException if {@code n < 0}.
 */
public static double factorialDouble(final int n) throws NotPositiveException {
    if (n < 0) {
        throw new NotPositiveException(LocalizedFormats.FACTORIAL_NEGATIVE_PARAMETER,
                                       n);
    }
    if (n < 21) {
        return FACTORIALS[n];
    }
    return FastMath.floor(FastMath.exp(CombinatoricsUtils.factorialLog(n)) + 0.5);
}