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

The following examples show how to use org.apache.commons.math3.exception.util.LocalizedFormats#LOWER_BOUND_NOT_BELOW_UPPER_BOUND . 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: Math_22_UniformRealDistribution_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Creates a uniform distribution.
 *
 * @param rng Random number generator.
 * @param lower Lower bound of this distribution (inclusive).
 * @param upper Upper bound of this distribution (exclusive).
 * @param inverseCumAccuracy Inverse cumulative probability accuracy.
 * @throws NumberIsTooLargeException if {@code lower >= upper}.
 * @since 3.1
 */
public UniformRealDistribution(RandomGenerator rng,
                               double lower,
                               double upper,
                               double inverseCumAccuracy)
    throws NumberIsTooLargeException {
    super(rng);
    if (lower >= upper) {
        throw new NumberIsTooLargeException(
                        LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND,
                        lower, upper, false);
    }

    this.lower = lower;
    this.upper = upper;
    solverAbsoluteAccuracy = inverseCumAccuracy;
}
 
Example 2
Source File: RandomDataGenerator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc} */
public long nextSecureLong(final long lower, final long upper) throws NumberIsTooLargeException {
    if (lower >= upper) {
        throw new NumberIsTooLargeException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND,
                                            lower, upper, false);
    }
    final RandomGenerator rng = getSecRan();
    final long max = (upper - lower) + 1;
    if (max <= 0) {
        // the range is too wide to fit in a positive long (larger than 2^63); as it covers
        // more than half the long range, we use directly a simple rejection method
        while (true) {
            final long r = rng.nextLong();
            if (r >= lower && r <= upper) {
                return r;
            }
        }
    } else if (max < Integer.MAX_VALUE){
        // we can shift the range and generate directly a positive int
        return lower + rng.nextInt((int) max);
    } else {
        // we can shift the range and generate directly a positive long
        return lower + nextLong(rng, max);
    }
}
 
Example 3
Source File: UniformRealDistribution.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a uniform distribution.
 *
 * @param rng Random number generator.
 * @param lower Lower bound of this distribution (inclusive).
 * @param upper Upper bound of this distribution (exclusive).
 * @param inverseCumAccuracy Inverse cumulative probability accuracy.
 * @throws NumberIsTooLargeException if {@code lower >= upper}.
 * @since 3.1
 */
public UniformRealDistribution(RandomGenerator rng,
                               double lower,
                               double upper,
                               double inverseCumAccuracy)
    throws NumberIsTooLargeException {
    super(rng);
    if (lower >= upper) {
        throw new NumberIsTooLargeException(
                        LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND,
                        lower, upper, false);
    }

    this.lower = lower;
    this.upper = upper;
    solverAbsoluteAccuracy = inverseCumAccuracy;
}
 
Example 4
Source File: RandomDataGenerator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc} */
public long nextLong(final long lower, final long upper) throws NumberIsTooLargeException {
    if (lower >= upper) {
        throw new NumberIsTooLargeException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND,
                                            lower, upper, false);
    }
    final long max = (upper - lower) + 1;
    if (max <= 0) {
        // the range is too wide to fit in a positive long (larger than 2^63); as it covers
        // more than half the long range, we use directly a simple rejection method
        final RandomGenerator rng = getRandomGenerator();
        while (true) {
            final long r = rng.nextLong();
            if (r >= lower && r <= upper) {
                return r;
            }
        }
    } else if (max < Integer.MAX_VALUE){
        // we can shift the range and generate directly a positive int
        return lower + getRandomGenerator().nextInt((int) max);
    } else {
        // we can shift the range and generate directly a positive long
        return lower + nextLong(getRandomGenerator(), max);
    }
}
 
Example 5
Source File: RandomDataGenerator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc} */
public long nextSecureLong(final long lower, final long upper) throws NumberIsTooLargeException {
    if (lower >= upper) {
        throw new NumberIsTooLargeException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND,
                                            lower, upper, false);
    }
    final RandomGenerator rng = getSecRan();
    final long max = (upper - lower) + 1;
    if (max <= 0) {
        // the range is too wide to fit in a positive long (larger than 2^63); as it covers
        // more than half the long range, we use directly a simple rejection method
        while (true) {
            final long r = rng.nextLong();
            if (r >= lower && r <= upper) {
                return r;
            }
        }
    } else if (max < Integer.MAX_VALUE){
        // we can shift the range and generate directly a positive int
        return lower + rng.nextInt((int) max);
    } else {
        // we can shift the range and generate directly a positive long
        return lower + nextLong(rng, max);
    }
}
 
Example 6
Source File: TriangularDistribution.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a triangular distribution.
 *
 * @param rng Random number generator.
 * @param a Lower limit of this distribution (inclusive).
 * @param b Upper limit of this distribution (inclusive).
 * @param c Mode of this distribution.
 * @throws NumberIsTooLargeException if {@code a >= b} or if {@code c > b}.
 * @throws NumberIsTooSmallException if {@code c < a}.
 * @since 3.1
 */
public TriangularDistribution(RandomGenerator rng,
                              double a,
                              double c,
                              double b)
    throws NumberIsTooLargeException, NumberIsTooSmallException {
    super(rng);

    if (a >= b) {
        throw new NumberIsTooLargeException(
                        LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND,
                        a, b, false);
    }
    if (c < a) {
        throw new NumberIsTooSmallException(
                LocalizedFormats.NUMBER_TOO_SMALL, c, a, true);
    }
    if (c > b) {
        throw new NumberIsTooLargeException(
                LocalizedFormats.NUMBER_TOO_LARGE, c, b, true);
    }

    this.a = a;
    this.c = c;
    this.b = b;
    solverAbsoluteAccuracy = FastMath.max(FastMath.ulp(a), FastMath.ulp(b));
}
 
Example 7
Source File: TriangularDistribution.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a triangular distribution.
 *
 * @param rng Random number generator.
 * @param a Lower limit of this distribution (inclusive).
 * @param b Upper limit of this distribution (inclusive).
 * @param c Mode of this distribution.
 * @throws NumberIsTooLargeException if {@code a >= b} or if {@code c > b}.
 * @throws NumberIsTooSmallException if {@code c < a}.
 * @since 3.1
 */
public TriangularDistribution(RandomGenerator rng,
                              double a,
                              double c,
                              double b)
    throws NumberIsTooLargeException, NumberIsTooSmallException {
    super(rng);

    if (a >= b) {
        throw new NumberIsTooLargeException(
                        LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND,
                        a, b, false);
    }
    if (c < a) {
        throw new NumberIsTooSmallException(
                LocalizedFormats.NUMBER_TOO_SMALL, c, a, true);
    }
    if (c > b) {
        throw new NumberIsTooLargeException(
                LocalizedFormats.NUMBER_TOO_LARGE, c, b, true);
    }

    this.a = a;
    this.c = c;
    this.b = b;
    solverAbsoluteAccuracy = FastMath.max(FastMath.ulp(a), FastMath.ulp(b));
}
 
Example 8
Source File: ConfidenceInterval.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Verifies that (lower, upper) is a valid non-empty interval and confidence
 * is strictly between 0 and 1.
 *
 * @param lower lower endpoint
 * @param upper upper endpoint
 * @param confidence confidence level
 */
private void checkParameters(double lower, double upper, double confidence) {
    if (lower >= upper) {
        throw new MathIllegalArgumentException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND, lower, upper);
    }
    if (confidence <= 0 || confidence >= 1) {
        throw new MathIllegalArgumentException(LocalizedFormats.OUT_OF_BOUNDS_CONFIDENCE_LEVEL, confidence, 0, 1);
    }
}
 
Example 9
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
public int nextInt(int lower, int upper) {
    if (lower >= upper) {
        throw new NumberIsTooLargeException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND,
                                            lower, upper, false);
    }
    double r = getRan().nextDouble();
    double scaled = r * upper + (1.0 - r) * lower + r;
    return (int) FastMath.floor(scaled);
}
 
Example 10
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**  {@inheritDoc} */
public int nextSecureInt(int lower, int upper) {
    if (lower >= upper) {
        throw new NumberIsTooLargeException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND,
                                            lower, upper, false);
    }
    SecureRandom sec = getSecRan();
    double r = sec.nextDouble();
    double scaled = r * upper + (1.0 - r) * lower + r;
    return (int)FastMath.floor(scaled);
}
 
Example 11
Source File: RandomDataGenerator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>
 * <strong>Algorithm Description</strong>: if the lower bound is excluded,
 * scales the output of Random.nextDouble(), but rejects 0 values (i.e.,
 * will generate another random double if Random.nextDouble() returns 0).
 * This is necessary to provide a symmetric output interval (both
 * endpoints excluded).
 * </p>
 *
 * @throws NumberIsTooLargeException if {@code lower >= upper}
 * @throws NotFiniteNumberException if one of the bounds is infinite
 * @throws NotANumberException if one of the bounds is NaN
 */
public double nextUniform(double lower, double upper, boolean lowerInclusive)
    throws NumberIsTooLargeException, NotFiniteNumberException, NotANumberException {

    if (lower >= upper) {
        throw new NumberIsTooLargeException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND,
                                            lower, upper, false);
    }

    if (Double.isInfinite(lower)) {
        throw new NotFiniteNumberException(LocalizedFormats.INFINITE_BOUND, lower);
    }
    if (Double.isInfinite(upper)) {
        throw new NotFiniteNumberException(LocalizedFormats.INFINITE_BOUND, upper);
    }

    if (Double.isNaN(lower) || Double.isNaN(upper)) {
        throw new NotANumberException();
    }

    final RandomGenerator generator = getRandomGenerator();

    // ensure nextDouble() isn't 0.0
    double u = generator.nextDouble();
    while (!lowerInclusive && u <= 0.0) {
        u = generator.nextDouble();
    }

    return u * upper + (1.0 - u) * lower;
}
 
Example 12
Source File: TriangularDistribution.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a triangular distribution.
 *
 * @param rng Random number generator.
 * @param a Lower limit of this distribution (inclusive).
 * @param b Upper limit of this distribution (inclusive).
 * @param c Mode of this distribution.
 * @throws NumberIsTooLargeException if {@code a >= b} or if {@code c > b}.
 * @throws NumberIsTooSmallException if {@code c < a}.
 * @since 3.1
 */
public TriangularDistribution(RandomGenerator rng,
                              double a,
                              double c,
                              double b)
    throws NumberIsTooLargeException, NumberIsTooSmallException {
    super(rng);

    if (a >= b) {
        throw new NumberIsTooLargeException(
                        LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND,
                        a, b, false);
    }
    if (c < a) {
        throw new NumberIsTooSmallException(
                LocalizedFormats.NUMBER_TOO_SMALL, c, a, true);
    }
    if (c > b) {
        throw new NumberIsTooLargeException(
                LocalizedFormats.NUMBER_TOO_LARGE, c, b, true);
    }

    this.a = a;
    this.c = c;
    this.b = b;
    solverAbsoluteAccuracy = FastMath.max(FastMath.ulp(a), FastMath.ulp(b));
}
 
Example 13
Source File: UniformIntegerDistribution.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a new uniform integer distribution using the given lower and
 * upper bounds (both inclusive).
 *
 * @param rng Random number generator.
 * @param lower Lower bound (inclusive) of this distribution.
 * @param upper Upper bound (inclusive) of this distribution.
 * @throws NumberIsTooLargeException if {@code lower >= upper}.
 * @since 3.1
 */
public UniformIntegerDistribution(RandomGenerator rng,
                                  int lower,
                                  int upper)
    throws NumberIsTooLargeException {
    super(rng);

    if (lower >= upper) {
        throw new NumberIsTooLargeException(
                        LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND,
                        lower, upper, false);
    }
    this.lower = lower;
    this.upper = upper;
}
 
Example 14
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
public int nextInt(int lower, int upper) {
    if (lower >= upper) {
        throw new NumberIsTooLargeException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND,
                                            lower, upper, false);
    }
    double r = getRan().nextDouble();
    double scaled = r * upper + (1.0 - r) * lower + r;
    return (int) FastMath.floor(scaled);
}
 
Example 15
Source File: UniformDistribution.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a uniform distribution.
 *
 * @param rng   Random number generator.
 * @param lower Lower bound of this distribution (inclusive).
 * @param upper Upper bound of this distribution (exclusive).
 * @throws NumberIsTooLargeException if {@code lower >= upper}.
 * @since 3.1
 */
public UniformDistribution(org.nd4j.linalg.api.rng.Random rng, double lower, double upper)
                throws NumberIsTooLargeException {
    super(rng);
    if (lower >= upper) {
        throw new NumberIsTooLargeException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND, lower, upper,
                        false);
    }

    this.lower = lower;
    this.upper = upper;
}
 
Example 16
Source File: RandomDataGenerator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>
 * <strong>Algorithm Description</strong>: if the lower bound is excluded,
 * scales the output of Random.nextDouble(), but rejects 0 values (i.e.,
 * will generate another random double if Random.nextDouble() returns 0).
 * This is necessary to provide a symmetric output interval (both
 * endpoints excluded).
 * </p>
 *
 * @throws NumberIsTooLargeException if {@code lower >= upper}
 * @throws NotFiniteNumberException if one of the bounds is infinite
 * @throws NotANumberException if one of the bounds is NaN
 */
public double nextUniform(double lower, double upper, boolean lowerInclusive)
    throws NumberIsTooLargeException, NotFiniteNumberException, NotANumberException {

    if (lower >= upper) {
        throw new NumberIsTooLargeException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND,
                                            lower, upper, false);
    }

    if (Double.isInfinite(lower)) {
        throw new NotFiniteNumberException(LocalizedFormats.INFINITE_BOUND, lower);
    }
    if (Double.isInfinite(upper)) {
        throw new NotFiniteNumberException(LocalizedFormats.INFINITE_BOUND, upper);
    }

    if (Double.isNaN(lower) || Double.isNaN(upper)) {
        throw new NotANumberException();
    }

    final RandomGenerator generator = getRandomGenerator();

    // ensure nextDouble() isn't 0.0
    double u = generator.nextDouble();
    while (!lowerInclusive && u <= 0.0) {
        u = generator.nextDouble();
    }

    return u * upper + (1.0 - u) * lower;
}
 
Example 17
Source File: ConfidenceInterval.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Verifies that (lower, upper) is a valid non-empty interval and confidence
 * is strictly between 0 and 1.
 *
 * @param lower lower endpoint
 * @param upper upper endpoint
 * @param confidence confidence level
 */
private void checkParameters(double lower, double upper, double confidence) {
    if (lower >= upper) {
        throw new MathIllegalArgumentException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND, lower, upper);
    }
    if (confidence <= 0 || confidence >= 1) {
        throw new MathIllegalArgumentException(LocalizedFormats.OUT_OF_BOUNDS_CONFIDENCE_LEVEL, confidence, 0, 1);
    }
}
 
Example 18
Source File: UniformIntegerDistribution.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a new uniform integer distribution using the given lower and
 * upper bounds (both inclusive).
 *
 * @param rng Random number generator.
 * @param lower Lower bound (inclusive) of this distribution.
 * @param upper Upper bound (inclusive) of this distribution.
 * @throws NumberIsTooLargeException if {@code lower > upper}.
 * @since 3.1
 */
public UniformIntegerDistribution(RandomGenerator rng,
                                  int lower,
                                  int upper)
    throws NumberIsTooLargeException {
    super(rng);

    if (lower > upper) {
        throw new NumberIsTooLargeException(
                        LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND,
                        lower, upper, true);
    }
    this.lower = lower;
    this.upper = upper;
}
 
Example 19
Source File: TriangularDistribution.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a triangular distribution.
 *
 * @param rng Random number generator.
 * @param a Lower limit of this distribution (inclusive).
 * @param b Upper limit of this distribution (inclusive).
 * @param c Mode of this distribution.
 * @throws NumberIsTooLargeException if {@code a >= b} or if {@code c > b}.
 * @throws NumberIsTooSmallException if {@code c < a}.
 * @since 3.1
 */
public TriangularDistribution(RandomGenerator rng,
                              double a,
                              double c,
                              double b)
    throws NumberIsTooLargeException, NumberIsTooSmallException {
    super(rng);

    if (a >= b) {
        throw new NumberIsTooLargeException(
                        LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND,
                        a, b, false);
    }
    if (c < a) {
        throw new NumberIsTooSmallException(
                LocalizedFormats.NUMBER_TOO_SMALL, c, a, true);
    }
    if (c > b) {
        throw new NumberIsTooLargeException(
                LocalizedFormats.NUMBER_TOO_LARGE, c, b, true);
    }

    this.a = a;
    this.c = c;
    this.b = b;
    solverAbsoluteAccuracy = FastMath.max(FastMath.ulp(a), FastMath.ulp(b));
}
 
Example 20
Source File: UniformRealDistribution.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a uniform distribution.
 *
 * @param rng Random number generator.
 * @param lower Lower bound of this distribution (inclusive).
 * @param upper Upper bound of this distribution (exclusive).
 * @throws NumberIsTooLargeException if {@code lower >= upper}.
 * @since 3.1
 */
public UniformRealDistribution(RandomGenerator rng,
                               double lower,
                               double upper)
    throws NumberIsTooLargeException {
    super(rng);
    if (lower >= upper) {
        throw new NumberIsTooLargeException(
                        LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND,
                        lower, upper, false);
    }

    this.lower = lower;
    this.upper = upper;
}