/** * Check that all entries of the input array are >= 0. * * @param in Array to be tested * @throws NotPositiveException if any array entries are less than 0. * @since 3.1 */ public static void checkNonNegative(final long[] in) throws NotPositiveException { for (int i = 0; i < in.length; i++) { if (in[i] < 0) { throw new NotPositiveException(in[i]); } } }
/** * Creates a new instance of a DBSCANClusterer. * * @param eps maximum radius of the neighborhood to be considered * @param minPts minimum number of points needed for a cluster * @throws NotPositiveException if {@code eps < 0.0} or {@code minPts < 0} */ public DBSCANClusterer(final double eps, final int minPts) throws NotPositiveException { if (eps < 0.0d) { throw new NotPositiveException(eps); } if (minPts < 0) { throw new NotPositiveException(minPts); } this.eps = eps; this.minPts = minPts; }
/** * Check all entries of the input array are >= 0. * * @param in Array to be tested * @throws NotPositiveException if any array entries are less than 0. * @since 3.1 */ public static void checkNonNegative(final long[][] in) throws NotPositiveException { for (int i = 0; i < in.length; i ++) { for (int j = 0; j < in[i].length; j++) { if (in[i][j] < 0) { throw new NotPositiveException(in[i][j]); } } } }
/** * Creates a new instance of a DBSCANClusterer. * * @param eps maximum radius of the neighborhood to be considered * @param minPts minimum number of points needed for a cluster * @param measure the distance measure to use * @throws NotPositiveException if {@code eps < 0.0} or {@code minPts < 0} */ public DBSCANClusterer(final double eps, final int minPts, final DistanceMeasure measure) throws NotPositiveException { super(measure); if (eps < 0.0d) { throw new NotPositiveException(eps); } if (minPts < 0) { throw new NotPositiveException(minPts); } this.eps = eps; this.minPts = minPts; }
/** * Raise a BigInteger 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 BigInteger pow(final BigInteger k, int e) throws NotPositiveException { if (e < 0) { throw new NotPositiveException(LocalizedFormats.EXPONENT, e); } return k.pow(e); }
/** * 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) throws NotPositiveException { 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 >>= 1; } return result; }
/** * @param s Sigma values. * @throws NotPositiveException if any of the array entries is smaller * than zero. */ public Sigma(double[] s) throws NotPositiveException { for (int i = 0; i < s.length; i++) { if (s[i] < 0) { throw new NotPositiveException(s[i]); } } sigma = s.clone(); }
/** * @param s Sigma values. * @throws NotPositiveException if any of the array entries is smaller * than zero. */ public Sigma(double[] s) throws NotPositiveException { for (int i = 0; i < s.length; i++) { if (s[i] < 0) { throw new NotPositiveException(s[i]); } } sigma = s.clone(); }
/** * Sets the maximal population size. * @param populationLimit maximal population size. * @throws NotPositiveException if the population limit is not a positive number (< 1) * @throws NumberIsTooSmallException if the new population size is smaller than the current number * of chromosomes in the population */ public void setPopulationLimit(final int populationLimit) throws NotPositiveException, NumberIsTooSmallException { if (populationLimit <= 0) { throw new NotPositiveException(LocalizedFormats.POPULATION_LIMIT_NOT_POSITIVE, populationLimit); } if (populationLimit < chromosomes.size()) { throw new NumberIsTooSmallException(populationLimit, chromosomes.size(), true); } this.populationLimit = populationLimit; }
/** * 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); } } }
/** * Creates a binomial distribution. * * @param rng Random number generator. * @param trials Number of trials. * @param p Probability of success. * @throws org.apache.commons.math3.exception.NotPositiveException if {@code trials < 0}. * @throws org.apache.commons.math3.exception.OutOfRangeException if {@code p < 0} or {@code p > 1}. * @since 3.1 */ public BinomialDistribution(Random rng, int trials, double p) { super(rng); 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; }
/** 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) throws NotPositiveException, NotStrictlyPositiveException { if (exponent < 0) { throw new NotPositiveException(exponent); } if (elements <= 0) { throw new NotStrictlyPositiveException(elements); } microsphereElements = elements; brightnessExponent = exponent; }
/** * Creates a binomial distribution. * * @param rng Random number generator. * @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}. * @since 3.1 */ public BinomialDistribution(RandomGenerator rng, int trials, double p) { super(rng); 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; }
/** {@inheritDoc} */ public FieldVector<T> getSubVector(int index, int n) throws OutOfRangeException, NotPositiveException { if (n < 0) { throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n); } ArrayFieldVector<T> out = new ArrayFieldVector<T>(field, n); try { System.arraycopy(data, index, out.data, 0, n); } catch (IndexOutOfBoundsException e) { checkIndex(index); checkIndex(index + n - 1); } return out; }
/** {@inheritDoc} */ @Override public RealVector getSubVector(int index, int n) throws OutOfRangeException, NotPositiveException { if (n < 0) { throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n); } ArrayRealVector out = new ArrayRealVector(n); try { System.arraycopy(data, index, out.data, 0, n); } catch (IndexOutOfBoundsException e) { checkIndex(index); checkIndex(index + n - 1); } return out; }
/** * 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; }
/** * 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!} does not exceed * {@code 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); }
/** * Checks dimensions and values of boundaries and inputSigma if defined. */ private void checkParameters() { final double[] init = getStartPoint(); final double[] lB = getLowerBound(); final double[] uB = getUpperBound(); if (inputSigma != null) { if (inputSigma.length != init.length) { throw new DimensionMismatchException(inputSigma.length, init.length); } for (int i = 0; i < init.length; i++) { if (inputSigma[i] < 0) { // XXX Remove this block in 4.0 (check performed in "Sigma" class). throw new NotPositiveException(inputSigma[i]); } if (inputSigma[i] > uB[i] - lB[i]) { throw new OutOfRangeException(inputSigma[i], 0, uB[i] - lB[i]); } } } }
/** {@inheritDoc} */ public FieldVector<T> getSubVector(int index, int n) throws OutOfRangeException, NotPositiveException { if (n < 0) { throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n); } checkIndex(index); checkIndex(index + n - 1); SparseFieldVector<T> res = new SparseFieldVector<T>(field,n); int end = index + n; OpenIntToFieldHashMap<T>.Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); int key = iter.key(); if (key >= index && key < end) { res.setEntry(key - index, iter.value()); } } return res; }
/** {@inheritDoc} */ @Override public OpenMapRealVector getSubVector(int index, int n) throws NotPositiveException, OutOfRangeException { checkIndex(index); if (n < 0) { throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n); } checkIndex(index + n - 1); OpenMapRealVector res = new OpenMapRealVector(n); int end = index + n; Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); int key = iter.key(); if (key >= index && key < end) { res.setEntry(key - index, iter.value()); } } return res; }