/** * Check that the endpoints specify an interval. * * @param lower Lower endpoint. * @param upper Upper endpoint. * @throws NumberIsTooLargeException if {@code lower >= upper}. */ public static void verifyInterval(final double lower, final double upper) throws NumberIsTooLargeException { if (lower >= upper) { throw new NumberIsTooLargeException(LocalizedFormats.ENDPOINTS_NOT_AN_INTERVAL, lower, upper, false); } }
/** * 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; }
/** * Build a Romberg integrator with given iteration counts. * @param minimalIterationCount minimum number of iterations * @param maximalIterationCount maximum number of iterations * (must be less than or equal to {@link #ROMBERG_MAX_ITERATIONS_COUNT}) * @exception NotStrictlyPositiveException if minimal number of iterations * is not strictly positive * @exception NumberIsTooSmallException if maximal number of iterations * is lesser than or equal to the minimal number of iterations * @exception NumberIsTooLargeException if maximal number of iterations * is greater than {@link #ROMBERG_MAX_ITERATIONS_COUNT} */ public RombergIntegrator(final int minimalIterationCount, final int maximalIterationCount) throws NotStrictlyPositiveException, NumberIsTooSmallException, NumberIsTooLargeException { super(minimalIterationCount, maximalIterationCount); if (maximalIterationCount > ROMBERG_MAX_ITERATIONS_COUNT) { throw new NumberIsTooLargeException(maximalIterationCount, ROMBERG_MAX_ITERATIONS_COUNT, false); } }
/** * Build a Simpson integrator with given iteration counts. * @param minimalIterationCount minimum number of iterations * @param maximalIterationCount maximum number of iterations * (must be less than or equal to {@link #SIMPSON_MAX_ITERATIONS_COUNT}) * @exception NotStrictlyPositiveException if minimal number of iterations * is not strictly positive * @exception NumberIsTooSmallException if maximal number of iterations * is lesser than or equal to the minimal number of iterations * @exception NumberIsTooLargeException if maximal number of iterations * is greater than {@link #SIMPSON_MAX_ITERATIONS_COUNT} */ public SimpsonIntegrator(final int minimalIterationCount, final int maximalIterationCount) throws NotStrictlyPositiveException, NumberIsTooSmallException, NumberIsTooLargeException { super(minimalIterationCount, maximalIterationCount); if (maximalIterationCount > SIMPSON_MAX_ITERATIONS_COUNT) { throw new NumberIsTooLargeException(maximalIterationCount, SIMPSON_MAX_ITERATIONS_COUNT, false); } }
/** * Build a midpoint integrator with given iteration counts. * @param minimalIterationCount minimum number of iterations * @param maximalIterationCount maximum number of iterations * (must be less than or equal to {@link #MIDPOINT_MAX_ITERATIONS_COUNT} * @exception NotStrictlyPositiveException if minimal number of iterations * is not strictly positive * @exception NumberIsTooSmallException if maximal number of iterations * is lesser than or equal to the minimal number of iterations * @exception NumberIsTooLargeException if maximal number of iterations * is greater than {@link #MIDPOINT_MAX_ITERATIONS_COUNT} */ public MidPointIntegrator(final int minimalIterationCount, final int maximalIterationCount) throws NotStrictlyPositiveException, NumberIsTooSmallException, NumberIsTooLargeException { super(minimalIterationCount, maximalIterationCount); if (maximalIterationCount > MIDPOINT_MAX_ITERATIONS_COUNT) { throw new NumberIsTooLargeException(maximalIterationCount, MIDPOINT_MAX_ITERATIONS_COUNT, false); } }
/** * 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; }
/** * Add the given chromosome to the population. * * @param chromosome the chromosome to add. * @throws NumberIsTooLargeException if the population would exceed the {@code populationLimit} after * adding this chromosome */ public void addChromosome(final Chromosome chromosome) throws NumberIsTooLargeException { if (chromosomes.size() >= populationLimit) { throw new NumberIsTooLargeException(LocalizedFormats.LIST_OF_CHROMOSOMES_BIGGER_THAN_POPULATION_SIZE, chromosomes.size(), populationLimit, false); } this.chromosomes.add(chromosome); }
/** * Add a {@link Collection} of chromosomes to this {@link Population}. * @param chromosomeColl a {@link Collection} of chromosomes * @throws NumberIsTooLargeException if the population would exceed the population limit when * adding this chromosome * @since 3.1 */ public void addChromosomes(final Collection<Chromosome> chromosomeColl) throws NumberIsTooLargeException { if (chromosomes.size() + chromosomeColl.size() > populationLimit) { throw new NumberIsTooLargeException(LocalizedFormats.LIST_OF_CHROMOSOMES_BIGGER_THAN_POPULATION_SIZE, chromosomes.size(), populationLimit, false); } this.chromosomes.addAll(chromosomeColl); }
/** * {@inheritDoc} */ @Override public double probability(double x0, double x1) throws NumberIsTooLargeException { if (x0 > x1) { throw new NumberIsTooLargeException(LocalizedFormats.LOWER_ENDPOINT_ABOVE_UPPER_ENDPOINT, x0, x1, true); } final double denom = standardDeviation * SQRT2; final double v0 = (x0 - mean) / denom; final double v1 = (x1 - mean) / denom; return 0.5 * Erf.erf(v0, v1); }
/** * {@inheritDoc} */ @Override public double probability(double x0, double x1) throws NumberIsTooLargeException { if (x0 > x1) { throw new NumberIsTooLargeException(LocalizedFormats.LOWER_ENDPOINT_ABOVE_UPPER_ENDPOINT, x0, x1, true); } final double denom = standardDeviation * SQRT2; final double v0 = (x0 - mean) / denom; final double v1 = (x1 - mean) / denom; return 0.5 * Erf.erf(v0, v1); }
/** * {@inheritDoc} */ @Override public double probability(double x0, double x1) throws NumberIsTooLargeException { if (x0 > x1) { throw new NumberIsTooLargeException(LocalizedFormats.LOWER_ENDPOINT_ABOVE_UPPER_ENDPOINT, x0, x1, true); } final double denom = standardDeviation * SQRT2; final double v0 = (x0 - mean) / denom; final double v1 = (x1 - mean) / denom; return 0.5 * Erf.erf(v0, v1); }
/** {@inheritDoc} */ @Override public double probability(double x0, double x1) throws NumberIsTooLargeException { if (x0 > x1) { throw new NumberIsTooLargeException(LocalizedFormats.LOWER_ENDPOINT_ABOVE_UPPER_ENDPOINT, x0, x1, true); } final double denom = standardDeviation * SQRT2; final double v0 = (x0 - mean) / denom; final double v1 = (x1 - mean) / denom; return 0.5 * Erf.erf(v0, v1); }
/** * 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}. */ public static void checkBinomial(final int n, final int k) throws NumberIsTooLargeException, NotPositiveException { 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); } } }
/** Ensure a value is zero. * @param value value to check * @exception NumberIsTooLargeException if value is not zero */ private void ensureZero(final double value) throws NumberIsTooLargeException { if (!Precision.equals(0.0, value, 1)) { throw new NumberIsTooLargeException(FastMath.abs(value), 0, true); } }
/** * {@inheritDoc} * * The default implementation uses the identity * <p>{@code P(x0 < X <= x1) = P(X <= x1) - P(X <= x0)}</p> */ public double cumulativeProbability(int x0, int x1) throws NumberIsTooLargeException { if (x1 < x0) { throw new NumberIsTooLargeException(LocalizedFormats.LOWER_ENDPOINT_ABOVE_UPPER_ENDPOINT, x0, x1, true); } return cumulativeProbability(x1) - cumulativeProbability(x0); }
/** * For a random variable {@code X} whose values are distributed according * to this distribution, this method returns {@code P(x0 < X <= x1)}. * * @param x0 Lower bound (excluded). * @param x1 Upper bound (included). * @return the probability that a random variable with this distribution * takes a value between {@code x0} and {@code x1}, excluding the lower * and including the upper endpoint. * @throws org.apache.commons.math3.exception.NumberIsTooLargeException if {@code x0 > x1}. * <p/> * The default implementation uses the identity * {@code P(x0 < X <= x1) = P(X <= x1) - P(X <= x0)} * @since 3.1 */ public double probability(double x0, double x1) { if (x0 > x1) { throw new NumberIsTooLargeException(LocalizedFormats.LOWER_ENDPOINT_ABOVE_UPPER_ENDPOINT, x0, x1, true); } return cumulativeProbability(x1) - cumulativeProbability(x0); }
/** * Construct a vector from part of a array. * * @param d Array. * @param pos Position of the first entry. * @param size Number of entries to copy. * @throws NullArgumentException if {@code d} is {@code null}. * @throws NumberIsTooLargeException if the size of {@code d} is less * than {@code pos + size}. */ public ArrayFieldVector(T[] d, int pos, int size) throws NullArgumentException, NumberIsTooLargeException { MathUtils.checkNotNull(d); if (d.length < pos + size) { throw new NumberIsTooLargeException(pos + size, d.length, true); } field = d[0].getField(); data = MathArrays.buildArray(field, size); System.arraycopy(d, pos, data, 0, size); }
/** * Returns the value of log Γ(1 + x) for -0.5 ≤ x ≤ 1.5. * This implementation is based on the double precision implementation in * the <em>NSWC Library of Mathematics Subroutines</em>, {@code DGMLN1}. * * @param x Argument. * @return The value of {@code log(Gamma(1 + x))}. * @throws NumberIsTooSmallException if {@code x < -0.5}. * @throws NumberIsTooLargeException if {@code x > 1.5}. * @since 3.1 */ public static double logGamma1p(final double x) throws NumberIsTooSmallException, NumberIsTooLargeException { if (x < -0.5) { throw new NumberIsTooSmallException(x, -0.5, true); } if (x > 1.5) { throw new NumberIsTooLargeException(x, 1.5, true); } return -FastMath.log1p(invGamma1pm1(x)); }
/** {@inheritDoc} */ @Override public double probability(double x0, double x1) throws NumberIsTooLargeException { if (x0 > x1) { throw new NumberIsTooLargeException(LocalizedFormats.LOWER_ENDPOINT_ABOVE_UPPER_ENDPOINT, x0, x1, true); } if (x0 <= 0 || x1 <= 0) { return super.probability(x0, x1); } final double denom = shape * SQRT2; final double v0 = (FastMath.log(x0) - scale) / denom; final double v1 = (FastMath.log(x1) - scale) / denom; return 0.5 * Erf.erf(v0, v1); }
/** {@inheritDoc} */ public DerivativeStructure value(final DerivativeStructure t) throws MathIllegalArgumentException { // check we can achieve the requested derivation order with the sample if (t.getOrder() >= nbPoints) { throw new NumberIsTooLargeException(t.getOrder(), nbPoints, false); } // compute sample position, trying to be centered if possible final double t0 = FastMath.max(FastMath.min(t.getValue(), tMax), tMin) - halfSampleSpan; // compute sample points final double[] y = new double[nbPoints]; for (int i = 0; i < nbPoints; ++i) { y[i] = function.value(t0 + i * stepSize); } // evaluate derivatives return evaluate(t, t0, y); }