org.apache.commons.math.exception.NotPositiveException Java Examples

The following examples show how to use org.apache.commons.math.exception.NotPositiveException. 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: MathUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Raise a BigInteger to a long power.
 *
 * @param k Number to raise.
 * @param e Exponent (must be positive or zero).
 * @return k<sup>e</sup>
 * @throws NotPositiveException if {@code e < 0}.
 */
public static BigInteger pow(final BigInteger k, long e) {
    if (e < 0) {
        throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
    }

    BigInteger result = BigInteger.ONE;
    BigInteger k2p    = k;
    while (e != 0) {
        if ((e & 0x1) != 0) {
            result = result.multiply(k2p);
        }
        k2p = k2p.multiply(k2p);
        e = e >> 1;
    }

    return result;

}
 
Example #2
Source File: MathUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Raise a BigInteger to a BigInteger power.
 *
 * @param k Number to raise.
 * @param e Exponent (must be positive or zero).
 * @return k<sup>e</sup>
 * @throws NotPositiveException if {@code e < 0}.
 */
public static BigInteger pow(final BigInteger k, BigInteger e) {
    if (e.compareTo(BigInteger.ZERO) < 0) {
        throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
    }

    BigInteger result = BigInteger.ONE;
    BigInteger k2p    = k;
    while (!BigInteger.ZERO.equals(e)) {
        if (e.testBit(0)) {
            result = result.multiply(k2p);
        }
        k2p = k2p.multiply(k2p);
        e = e.shiftRight(1);
    }

    return result;
}
 
Example #3
Source File: MathUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Raise a BigInteger to a BigInteger power.
 *
 * @param k Number to raise.
 * @param e Exponent (must be positive or zero).
 * @return k<sup>e</sup>
 * @throws NotPositiveException if {@code e < 0}.
 */
public static BigInteger pow(final BigInteger k, BigInteger e) {
    if (e.compareTo(BigInteger.ZERO) < 0) {
        throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
    }

    BigInteger result = BigInteger.ONE;
    BigInteger k2p    = k;
    while (!BigInteger.ZERO.equals(e)) {
        if (e.testBit(0)) {
            result = result.multiply(k2p);
        }
        k2p = k2p.multiply(k2p);
        e = e.shiftRight(1);
    }

    return result;
}
 
Example #4
Source File: MathUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Raise a BigInteger to a long power.
 *
 * @param k Number to raise.
 * @param e Exponent (must be positive or zero).
 * @return k<sup>e</sup>
 * @throws NotPositiveException if {@code e < 0}.
 */
public static BigInteger pow(final BigInteger k, long e) {
    if (e < 0) {
        throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
    }

    BigInteger result = BigInteger.ONE;
    BigInteger k2p    = k;
    while (e != 0) {
        if ((e & 0x1) != 0) {
            result = result.multiply(k2p);
        }
        k2p = k2p.multiply(k2p);
        e = e >> 1;
    }

    return result;

}
 
Example #5
Source File: MathUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Raise a long to a long power.
 *
 * @param k Number to raise.
 * @param e Exponent (must be positive or zero).
 * @return k<sup>e</sup>
 * @throws NotPositiveException if {@code e < 0}.
 */
public static long pow(final long k, long e) {
    if (e < 0) {
        throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
    }

    long result = 1l;
    long k2p    = k;
    while (e != 0) {
        if ((e & 0x1) != 0) {
            result *= k2p;
        }
        k2p *= k2p;
        e = e >> 1;
    }

    return result;
}
 
Example #6
Source File: MathUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Raise a long to an int power.
 *
 * @param k Number to raise.
 * @param e Exponent (must be positive or zero).
 * @return k<sup>e</sup>
 * @throws NotPositiveException if {@code e < 0}.
 */
public static long pow(final long k, int e) {
    if (e < 0) {
        throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
    }

    long result = 1l;
    long k2p    = k;
    while (e != 0) {
        if ((e & 0x1) != 0) {
            result *= k2p;
        }
        k2p *= k2p;
        e = e >> 1;
    }

    return result;
}
 
Example #7
Source File: MathUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Raise an int to a long power.
 *
 * @param k Number to raise.
 * @param e Exponent (must be positive or zero).
 * @return k<sup>e</sup>
 * @throws NotPositiveException if {@code e < 0}.
 */
public static int pow(final int k, long e) {
    if (e < 0) {
        throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
    }

    int result = 1;
    int k2p    = k;
    while (e != 0) {
        if ((e & 0x1) != 0) {
            result *= k2p;
        }
        k2p *= k2p;
        e = e >> 1;
    }

    return result;
}
 
Example #8
Source File: MathUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Raise an int to an int power.
 *
 * @param k Number to raise.
 * @param e Exponent (must be positive or zero).
 * @return k<sup>e</sup>
 * @throws NotPositiveException if {@code e < 0}.
 */
public static int pow(final int k, int e) {
    if (e < 0) {
        throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
    }

    int result = 1;
    int k2p    = k;
    while (e != 0) {
        if ((e & 0x1) != 0) {
            result *= k2p;
        }
        k2p *= k2p;
        e = e >> 1;
    }

    return result;
}
 
Example #9
Source File: MathUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Raise a long to a long power.
 *
 * @param k Number to raise.
 * @param e Exponent (must be positive or zero).
 * @return k<sup>e</sup>
 * @throws NotPositiveException if {@code e < 0}.
 */
public static long pow(final long k, long e) {
    if (e < 0) {
        throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
    }

    long result = 1l;
    long k2p    = k;
    while (e != 0) {
        if ((e & 0x1) != 0) {
            result *= k2p;
        }
        k2p *= k2p;
        e = e >> 1;
    }

    return result;
}
 
Example #10
Source File: MathUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Raise an int to an int power.
 *
 * @param k Number to raise.
 * @param e Exponent (must be positive or zero).
 * @return k<sup>e</sup>
 * @throws NotPositiveException if {@code e < 0}.
 */
public static int pow(final int k, int e) {
    if (e < 0) {
        throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
    }

    int result = 1;
    int k2p    = k;
    while (e != 0) {
        if ((e & 0x1) != 0) {
            result *= k2p;
        }
        k2p *= k2p;
        e = e >> 1;
    }

    return result;
}
 
Example #11
Source File: MathUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Raise an int to an int power.
 *
 * @param k Number to raise.
 * @param e Exponent (must be positive or zero).
 * @return k<sup>e</sup>
 * @throws NotPositiveException if {@code e < 0}.
 */
public static int pow(final int k, int e) {
    if (e < 0) {
        throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
    }

    int result = 1;
    int k2p    = k;
    while (e != 0) {
        if ((e & 0x1) != 0) {
            result *= k2p;
        }
        k2p *= k2p;
        e = e >> 1;
    }

    return result;
}
 
Example #12
Source File: MathUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Raise a long to an int power.
 *
 * @param k Number to raise.
 * @param e Exponent (must be positive or zero).
 * @return k<sup>e</sup>
 * @throws NotPositiveException if {@code e < 0}.
 */
public static long pow(final long k, int e) {
    if (e < 0) {
        throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
    }

    long result = 1l;
    long k2p    = k;
    while (e != 0) {
        if ((e & 0x1) != 0) {
            result *= k2p;
        }
        k2p *= k2p;
        e = e >> 1;
    }

    return result;
}
 
Example #13
Source File: MathUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Raise a long to an int power.
 *
 * @param k Number to raise.
 * @param e Exponent (must be positive or zero).
 * @return k<sup>e</sup>
 * @throws NotPositiveException if {@code e < 0}.
 */
public static long pow(final long k, int e) {
    if (e < 0) {
        throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
    }

    long result = 1l;
    long k2p    = k;
    while (e != 0) {
        if ((e & 0x1) != 0) {
            result *= k2p;
        }
        k2p *= k2p;
        e = e >> 1;
    }

    return result;
}
 
Example #14
Source File: MathUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Raise a long to a long power.
 *
 * @param k Number to raise.
 * @param e Exponent (must be positive or zero).
 * @return k<sup>e</sup>
 * @throws NotPositiveException if {@code e < 0}.
 */
public static long pow(final long k, long e) {
    if (e < 0) {
        throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
    }

    long result = 1l;
    long k2p    = k;
    while (e != 0) {
        if ((e & 0x1) != 0) {
            result *= k2p;
        }
        k2p *= k2p;
        e = e >> 1;
    }

    return result;
}
 
Example #15
Source File: MathUtils.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 #16
Source File: PascalDistributionImpl.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a Pascal distribution with the given number of trials and
 * probability of success.
 *
 * @param r Number of successes.
 * @param p Probability of success.
 */
public PascalDistributionImpl(int r, double p) {
    if (r < 0) {
        throw new NotPositiveException(LocalizedFormats.NUMBER_OF_SUCCESSES,
                                       r);
    }
    if (p < 0 || p > 1) {
        throw new OutOfRangeException(p, 0, 1);
    }

    numberOfSuccesses = r;
    probabilityOfSuccess = p;
}
 
Example #17
Source File: BinomialDistributionImpl.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a binomial distribution with the given number of trials and
 * probability of success.
 *
 * @param trials Number of trials.
 * @param p Probability of success.
 * @throws NotPositiveException if {@code trials < 0}.
 * @throws OutOfRangeException if {@code p < 0} or {@code p > 1}.
 */
public BinomialDistributionImpl(int trials, double p) {
    if (trials < 0) {
        throw new NotPositiveException(LocalizedFormats.NUMBER_OF_TRIALS,
                                       trials);
    }
    if (p < 0 || p > 1) {
        throw new OutOfRangeException(p, 0, 1);
    }

    probabilityOfSuccess = p;
    numberOfTrials = trials;
}
 
Example #18
Source File: AbstractUnivariateStatistic.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This method is used by <code>evaluate(double[], int, int)</code> methods
 * to verify that the input parameters designate a subarray of positive length.
 * <p>
 * <ul>
 * <li>returns <code>true</code> iff the parameters designate a subarray of
 * non-negative length</li>
 * <li>throws <code>IllegalArgumentException</code> if the array is null or
 * or the indices are invalid</li>
 * <li>returns <code>false</li> if the array is non-null, but
 * <code>length</code> is 0 unless <code>allowEmpty</code> is <code>true</code>
 * </ul></p>
 *
 * @param values the input array
 * @param begin index of the first array element to include
 * @param length the number of elements to include
 * @param allowEmpty if <code>true</code> then zero length arrays are allowed
 * @return true if the parameters are valid
 * @throws IllegalArgumentException if the indices are invalid or the array is null
 * @since 3.0
 */
protected boolean test(final double[] values, final int begin, final int length, final boolean allowEmpty){

    if (values == null) {
        throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
    }

    if (begin < 0) {
        throw new NotPositiveException(LocalizedFormats.START_POSITION, begin);
    }

    if (length < 0) {
        throw new NotPositiveException(LocalizedFormats.LENGTH, length);
    }

    if (begin + length > values.length) {
        throw new NumberIsTooLargeException(LocalizedFormats.SUBARRAY_ENDS_AFTER_ARRAY_END,
                                            begin + length, values.length, true);
    }

    if (length == 0 && !allowEmpty) {
        return false;
    }

    return true;

}
 
Example #19
Source File: ListPopulation.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a new ListPopulation instance and initializes its inner
 * chromosome list.
 *
 * @param populationLimit maximal size of the population
 */
public ListPopulation (int populationLimit) {
    if (populationLimit < 0) {
        throw new NotPositiveException(LocalizedFormats.POPULATION_LIMIT_NOT_POSITIVE, populationLimit);
    }
    this.populationLimit = populationLimit;
    this.chromosomes = new ArrayList<Chromosome>(populationLimit);
}
 
Example #20
Source File: ListPopulation.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a new ListPopulation instance.
 *
 * @param chromosomes list of chromosomes in the population
 * @param populationLimit maximal size of the population
 */
public ListPopulation (List<Chromosome> chromosomes, int populationLimit) {
    if (chromosomes.size() > populationLimit) {
        throw new NumberIsTooLargeException(LocalizedFormats.LIST_OF_CHROMOSOMES_BIGGER_THAN_POPULATION_SIZE,
                                            chromosomes.size(), populationLimit, false);
    }
    if (populationLimit < 0) {
        throw new NotPositiveException(LocalizedFormats.POPULATION_LIMIT_NOT_POSITIVE, populationLimit);
    }

    this.chromosomes = chromosomes;
    this.populationLimit = populationLimit;
}
 
Example #21
Source File: MicrosphereInterpolator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Set the brightness exponent.
 * @param exponent Exponent for computing the distance dimming
 * factor.
 * @throws NotPositiveException if {@code exponent < 0}.
 */
public void setBrightnessExponent(final int exponent) {
    if (exponent < 0) {
        throw new NotPositiveException(exponent);
    }
    brightnessExponent = exponent;
}
 
Example #22
Source File: AbstractUnivariateStatistic.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This method is used by <code>evaluate(double[], int, int)</code> methods
 * to verify that the input parameters designate a subarray of positive length.
 * <p>
 * <ul>
 * <li>returns <code>true</code> iff the parameters designate a subarray of
 * positive length</li>
 * <li>throws <code>IllegalArgumentException</code> if the array is null or
 * or the indices are invalid</li>
 * <li>returns <code>false</li> if the array is non-null, but
 * <code>length</code> is 0.
 * </ul></p>
 *
 * @param values the input array
 * @param begin index of the first array element to include
 * @param length the number of elements to include
 * @return true if the parameters are valid and designate a subarray of positive length
 * @throws IllegalArgumentException if the indices are invalid or the array is null
 */
protected boolean test(
    final double[] values,
    final int begin,
    final int length) {

    if (values == null) {
        throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
    }

    if (begin < 0) {
        throw new NotPositiveException(LocalizedFormats.START_POSITION, begin);
    }

    if (length < 0) {
        throw new NotPositiveException(LocalizedFormats.LENGTH, length);
    }

    if (begin + length > values.length) {
        throw MathRuntimeException.createIllegalArgumentException(
              LocalizedFormats.SUBARRAY_ENDS_AFTER_ARRAY_END);
    }

    if (length == 0) {
        return false;
    }

    return true;

}
 
Example #23
Source File: ListPopulation.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a new ListPopulation instance and initializes its inner
 * chromosome list.
 *
 * @param populationLimit maximal size of the population
 */
public ListPopulation (int populationLimit) {
    if (populationLimit < 0) {
        throw new NotPositiveException(LocalizedFormats.POPULATION_LIMIT_NOT_POSITIVE, populationLimit);
    }
    this.populationLimit = populationLimit;
    this.chromosomes = new ArrayList<Chromosome>(populationLimit);
}
 
Example #24
Source File: ListPopulation.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a new ListPopulation instance.
 *
 * @param chromosomes list of chromosomes in the population
 * @param populationLimit maximal size of the population
 */
public ListPopulation (List<Chromosome> chromosomes, int populationLimit) {
    if (chromosomes.size() > populationLimit) {
        throw new NumberIsTooLargeException(LocalizedFormats.LIST_OF_CHROMOSOMES_BIGGER_THAN_POPULATION_SIZE,
                                            chromosomes.size(), populationLimit, false);
    }
    if (populationLimit < 0) {
        throw new NotPositiveException(LocalizedFormats.POPULATION_LIMIT_NOT_POSITIVE, populationLimit);
    }

    this.chromosomes = chromosomes;
    this.populationLimit = populationLimit;
}
 
Example #25
Source File: MicrosphereInterpolator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** Create a microsphere interpolator.
 * @param elements Number of surface elements of the microsphere.
 * @param exponent Exponent used in the power law that computes the
 * weights (distance dimming factor) of the sample data.
 * @throws NotPositiveException if {@code exponent < 0}.
 * @throws NotStrictlyPositiveException if {@code elements <= 0}.
 */
public MicrosphereInterpolator(final int elements,
                               final int exponent) {
    if (exponent < 0) {
        throw new NotPositiveException(exponent);
    }
    if (elements <= 0) {
        throw new NotStrictlyPositiveException(elements);
    }

    microsphereElements = elements;
    brightnessExponent = exponent;
}
 
Example #26
Source File: Complex.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes the n-th roots of this complex number.
 * The nth roots are defined by the formula:
 * <pre>
 *  <code>
 *   z<sub>k</sub> = abs<sup>1/n</sup> (cos(phi + 2&pi;k/n) + i (sin(phi + 2&pi;k/n))
 *  </code>
 * </pre>
 * for <i>{@code k=0, 1, ..., n-1}</i>, where {@code abs} and {@code phi}
 * are respectively the {@link #abs() modulus} and
 * {@link #getArgument() argument} of this complex number.
 * <br/>
 * If one or both parts of this complex number is NaN, a list with just
 * one element, {@link #NaN} is returned.
 * if neither part is NaN, but at least one part is infinite, the result
 * is a one-element list containing {@link #INF}.
 *
 * @param n Degree of root.
 * @return a List<Complex> of all {@code n}-th roots of {@code this}.
 * @throws NotPositiveException if {@code n <= 0}.
 * @since 2.0
 */
public List<Complex> nthRoot(int n) {

    if (n <= 0) {
        throw new NotPositiveException(LocalizedFormats.CANNOT_COMPUTE_NTH_ROOT_FOR_NEGATIVE_N,
                                       n);
    }

    final List<Complex> result = new ArrayList<Complex>();

    if (isNaN) {
        result.add(NaN);
        return result;
    }
    if (isInfinite()) {
        result.add(INF);
        return result;
    }

    // nth root of abs -- faster / more accurate to use a solver here?
    final double nthRootOfAbs = FastMath.pow(abs(), 1.0 / n);

    // Compute nth roots of complex number with k = 0, 1, ... n-1
    final double nthPhi = getArgument() / n;
    final double slice = 2 * FastMath.PI / n;
    double innerPart = nthPhi;
    for (int k = 0; k < n ; k++) {
        // inner part
        final double realPart = nthRootOfAbs *  FastMath.cos(innerPart);
        final double imaginaryPart = nthRootOfAbs *  FastMath.sin(innerPart);
        result.add(createComplex(realPart, imaginaryPart));
        innerPart += slice;
    }

    return result;
}
 
Example #27
Source File: MathUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Check binomial preconditions.
 *
 * @param n Size of the set.
 * @param k Size of the subsets to be counted.
 * @throws NotPositiveException if {@code n < 0}.
 * @throws NumberIsTooLargeException if {@code k > n}.
 */
private static void checkBinomial(final int n, final int k) {
    if (n < k) {
        throw new NumberIsTooLargeException(LocalizedFormats.BINOMIAL_INVALID_PARAMETERS_ORDER,
                                            k, n, true);
    }
    if (n < 0) {
        throw new NotPositiveException(LocalizedFormats.BINOMIAL_NEGATIVE_PARAMETER, n);
    }
}
 
Example #28
Source File: 1_Complex.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes the n-th roots of this complex number.
 * The nth roots are defined by the formula:
 * <pre>
 *  <code>
 *   z<sub>k</sub> = abs<sup>1/n</sup> (cos(phi + 2&pi;k/n) + i (sin(phi + 2&pi;k/n))
 *  </code>
 * </pre>
 * for <i>{@code k=0, 1, ..., n-1}</i>, where {@code abs} and {@code phi}
 * are respectively the {@link #abs() modulus} and
 * {@link #getArgument() argument} of this complex number.
 * <br/>
 * If one or both parts of this complex number is NaN, a list with just
 * one element, {@link #NaN} is returned.
 * if neither part is NaN, but at least one part is infinite, the result
 * is a one-element list containing {@link #INF}.
 *
 * @param n Degree of root.
 * @return a List<Complex> of all {@code n}-th roots of {@code this}.
 * @throws NotPositiveException if {@code n <= 0}.
 * @since 2.0
 */
public List<Complex> nthRoot(int n) {

    if (n <= 0) {
        throw new NotPositiveException(LocalizedFormats.CANNOT_COMPUTE_NTH_ROOT_FOR_NEGATIVE_N,
                                       n);
    }

    final List<Complex> result = new ArrayList<Complex>();

    if (isNaN) {
        result.add(NaN);
        return result;
    }
    if (isInfinite()) {
        result.add(INF);
        return result;
    }

    // nth root of abs -- faster / more accurate to use a solver here?
    final double nthRootOfAbs = FastMath.pow(abs(), 1.0 / n);

    // Compute nth roots of complex number with k = 0, 1, ... n-1
    final double nthPhi = getArgument() / n;
    final double slice = 2 * FastMath.PI / n;
    double innerPart = nthPhi;
    for (int k = 0; k < n ; k++) {
        // inner part
        final double realPart = nthRootOfAbs *  FastMath.cos(innerPart);
        final double imaginaryPart = nthRootOfAbs *  FastMath.sin(innerPart);
        result.add(createComplex(realPart, imaginaryPart));
        innerPart += slice;
    }

    return result;
}
 
Example #29
Source File: MathUtils.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 #30
Source File: PascalDistributionImpl.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a Pascal distribution with the given number of trials and
 * probability of success.
 *
 * @param r Number of successes.
 * @param p Probability of success.
 */
public PascalDistributionImpl(int r, double p) {
    if (r < 0) {
        throw new NotPositiveException(LocalizedFormats.NUMBER_OF_SUCCESSES,
                                       r);
    }
    if (p < 0 || p > 1) {
        throw new OutOfRangeException(p, 0, 1);
    }

    numberOfSuccesses = r;
    probabilityOfSuccess = p;
}