/** Check equation index. * @param index index of the equation, must be between 0 included and * {@link #getNumberOfEquations()} (excluded) * @exception MathIllegalArgumentException if index is out of range */ private void checkIndex(final int index) throws MathIllegalArgumentException { if (index < 0 || index > start.length - 2) { throw new MathIllegalArgumentException(LocalizedFormats.ARGUMENT_OUTSIDE_DOMAIN, index, 0, start.length - 2); } }
/** * 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); } } }
/** * Returns the base-2 logarithm of the specified {@code int}. Throws an * exception if {@code n} is not a power of two. * * @param n the {@code int} whose base-2 logarithm is to be evaluated * @return the base-2 logarithm of {@code n} * @throws MathIllegalArgumentException if {@code n} is not a power of two */ public static int exactLog2(final int n) throws MathIllegalArgumentException { int index = Arrays.binarySearch(TransformUtils.POWERS_OF_TWO, n); if (index < 0) { throw new MathIllegalArgumentException( LocalizedFormats.NOT_POWER_OF_TWO_CONSIDER_PADDING, Integer.valueOf(n)); } return index; } }
/** * {@inheritDoc} * * @throws MathIllegalArgumentException if the chromosomes are not an instance of {@link AbstractListChromosome} * @throws DimensionMismatchException if the length of the two chromosomes is different */ @SuppressWarnings("unchecked") public ChromosomePair crossover(final Chromosome first, final Chromosome second) throws DimensionMismatchException, MathIllegalArgumentException { if (!(first instanceof AbstractListChromosome<?> && second instanceof AbstractListChromosome<?>)) { throw new MathIllegalArgumentException(LocalizedFormats.INVALID_FIXED_LENGTH_CHROMOSOME); } return mate((AbstractListChromosome<T>) first, (AbstractListChromosome<T>) second); }
/** * {@inheritDoc} * * @throws MathIllegalArgumentException iff one of the chromosomes is * not an instance of {@link AbstractListChromosome} * @throws DimensionMismatchException if the length of the two chromosomes is different */ @SuppressWarnings("unchecked") public ChromosomePair crossover(final Chromosome first, final Chromosome second) throws DimensionMismatchException, MathIllegalArgumentException { if (!(first instanceof AbstractListChromosome<?> && second instanceof AbstractListChromosome<?>)) { throw new MathIllegalArgumentException(LocalizedFormats.INVALID_FIXED_LENGTH_CHROMOSOME); } return mate((AbstractListChromosome<T>) first, (AbstractListChromosome<T>) second); }
/** * {@inheritDoc} * * @throws MathIllegalArgumentException iff one of the chromosomes is * not an instance of {@link AbstractListChromosome} * @throws DimensionMismatchException if the length of the two chromosomes is different */ @SuppressWarnings("unchecked") public ChromosomePair crossover(final Chromosome first, final Chromosome second) throws DimensionMismatchException, MathIllegalArgumentException { if (!(first instanceof AbstractListChromosome<?> && second instanceof AbstractListChromosome<?>)) { throw new MathIllegalArgumentException(LocalizedFormats.INVALID_FIXED_LENGTH_CHROMOSOME); } return mate((AbstractListChromosome<T>) first, (AbstractListChromosome<T>) second); }
/** * Prime factors decomposition * * @param n number to factorize: must be ≥ 2 * @return list of prime factors of n * @throws MathIllegalArgumentException if n < 2. */ public static List<Integer> primeFactors(int n) { if (n < 2) { throw new MathIllegalArgumentException(LocalizedFormats.NUMBER_TOO_SMALL, n, 2); } // slower than trial div unless we do an awful lot of computation // (then it finally gets JIT-compiled efficiently // List<Integer> out = PollardRho.primeFactors(n); return SmallPrimes.trialDivision(n); }
/** * @param o the object that gets transformed. * @return a double primitive representation of the Object o. * @throws NullArgumentException if Object <code>o</code> is {@code null}. * @throws MathIllegalArgumentException if Object <code>o</code> * cannot successfully be transformed * @see <a href="http://commons.apache.org/collections/api-release/org/apache/commons/collections/Transformer.html">Commons Collections Transformer</a> */ public double transform(Object o) throws NullArgumentException, MathIllegalArgumentException { if (o == null) { throw new NullArgumentException(LocalizedFormats.OBJECT_TRANSFORMATION); } if (o instanceof Number) { return ((Number)o).doubleValue(); } try { return Double.parseDouble(o.toString()); } catch (NumberFormatException e) { throw new MathIllegalArgumentException(LocalizedFormats.CANNOT_TRANSFORM_TO_DOUBLE, o.toString()); } }
/** * This function allows you to control the number of elements contained * in this array, and can be used to "throw out" the last n values in an * array. This function will also expand the internal array as needed. * * @param i a new number of elements * @throws MathIllegalArgumentException if <code>i</code> is negative. */ public synchronized void setNumElements(int i) throws MathIllegalArgumentException { // If index is negative thrown an error. if (i < 0) { throw new MathIllegalArgumentException( LocalizedFormats.INDEX_NOT_POSITIVE, i); } // Test the new num elements, check to see if the array needs to be // expanded to accommodate this new number of elements. final int newSize = startIndex + i; if (newSize > internalArray.length) { expandTo(newSize); } // Set the new number of elements to new value. numElements = i; }
/** * Simple constructor. * @param vertices the vertices of the convex hull, must be ordered * @param tolerance tolerance below which points are considered identical * @throws MathIllegalArgumentException if the vertices do not form a convex hull */ public ConvexHull2D(final Vector2D[] vertices, final double tolerance) throws MathIllegalArgumentException { // assign tolerance as it will be used by the isConvex method this.tolerance = tolerance; if (!isConvex(vertices)) { throw new MathIllegalArgumentException(LocalizedFormats.NOT_CONVEX); } this.vertices = vertices.clone(); }
/** * Throws MathIllegalArgumentException if the matrix does not have at least * two columns and two rows. * * @param matrix matrix to check for sufficiency * @throws MathIllegalArgumentException if there is insufficient data */ private void checkSufficientData(final RealMatrix matrix) { int nRows = matrix.getRowDimension(); int nCols = matrix.getColumnDimension(); if (nRows < 2 || nCols < 2) { throw new MathIllegalArgumentException(LocalizedFormats.INSUFFICIENT_ROWS_AND_COLUMNS, nRows, nCols); } } }
/** * Throws MathIllegalArgumentException if the matrix does not have at least * one column and two rows. * @param matrix matrix to check * @throws MathIllegalArgumentException if the matrix does not contain sufficient data * to compute covariance */ private void checkSufficientData(final RealMatrix matrix) throws MathIllegalArgumentException { int nRows = matrix.getRowDimension(); int nCols = matrix.getColumnDimension(); if (nRows < 2 || nCols < 1) { throw new MathIllegalArgumentException( LocalizedFormats.INSUFFICIENT_ROWS_AND_COLUMNS, nRows, nCols); } } }
/** * Sets the <code>expansionMode</code>. The specified value must be one of * ADDITIVE_MODE, MULTIPLICATIVE_MODE. * * @param expansionMode The expansionMode to set. * @throws MathIllegalArgumentException if the specified mode value is not valid. * @deprecated As of 3.1. Please use {@link #setExpansionMode(ExpansionMode)} instead. */ @Deprecated public void setExpansionMode(int expansionMode) throws MathIllegalArgumentException { if (expansionMode != MULTIPLICATIVE_MODE && expansionMode != ADDITIVE_MODE) { throw new MathIllegalArgumentException(LocalizedFormats.UNSUPPORTED_EXPANSION_MODE, expansionMode, MULTIPLICATIVE_MODE, "MULTIPLICATIVE_MODE", ADDITIVE_MODE, "ADDITIVE_MODE"); } synchronized(this) { if (expansionMode == MULTIPLICATIVE_MODE) { setExpansionMode(ExpansionMode.MULTIPLICATIVE); } else if (expansionMode == ADDITIVE_MODE) { setExpansionMode(ExpansionMode.ADDITIVE); } } }
/** * {@inheritDoc} * * @throws MathIllegalArgumentException if <code>original</code> is not a {@link RandomKey} instance */ public Chromosome mutate(final Chromosome original) throws MathIllegalArgumentException { if (!(original instanceof RandomKey<?>)) { throw new MathIllegalArgumentException(LocalizedFormats.RANDOMKEY_MUTATION_WRONG_CLASS, original.getClass().getSimpleName()); } RandomKey<?> originalRk = (RandomKey<?>) original; List<Double> repr = originalRk.getRepresentation(); int rInd = GeneticAlgorithm.getRandomGenerator().nextInt(repr.size()); List<Double> newRepr = new ArrayList<Double> (repr); newRepr.set(rInd, GeneticAlgorithm.getRandomGenerator().nextDouble()); return originalRk.newFixedLengthChromosome(newRepr); }
/** * Mutate the given chromosome. Randomly changes one gene. * * @param original the original chromosome. * @return the mutated chromosome. * @throws MathIllegalArgumentException if <code>original</code> is not an instance of {@link BinaryChromosome}. */ public Chromosome mutate(Chromosome original) throws MathIllegalArgumentException { if (!(original instanceof BinaryChromosome)) { throw new MathIllegalArgumentException(LocalizedFormats.INVALID_BINARY_CHROMOSOME); } BinaryChromosome origChrom = (BinaryChromosome) original; List<Integer> newRepr = new ArrayList<Integer>(origChrom.getRepresentation()); // randomly select a gene int geneIndex = GeneticAlgorithm.getRandomGenerator().nextInt(origChrom.getLength()); // and change it newRepr.set(geneIndex, origChrom.getRepresentation().get(geneIndex) == 0 ? 1 : 0); Chromosome newChrom = origChrom.newFixedLengthChromosome(newRepr); return newChrom; }
/** * Mutate the given chromosome. Randomly changes one gene. * * @param original the original chromosome. * @return the mutated chromosome. * @throws IllegalArgumentException if <code>original</code> is not an instance of {@link BitsChromosome}. */ public Chromosome mutate(Chromosome original) throws IllegalArgumentException { if (!(original instanceof BitsChromosome)) { throw new MathIllegalArgumentException(new DummyLocalizable("bits mutation only works on BitsChromosome")); } BitsChromosome origChrom = (BitsChromosome) original; BitSet newNey = (BitSet) origChrom.getRepresentation().clone(); // randomly select a gene int geneIndex = GeneticAlgorithm.getRandomGenerator().nextInt(origChrom.getLength()); // change it newNey.set(geneIndex, !newNey.get(geneIndex)); Chromosome newChrom = origChrom.newBitsChromosome(newNey); return newChrom; } }
/** * Returns the first Bessel function, \(J_{order}(x)\). * * @param order Order of the Bessel function * @param x Argument * @return Value of the Bessel function of the first kind, \(J_{order}(x)\) * @throws MathIllegalArgumentException if {@code x} is too large relative to {@code order} * @throws ConvergenceException if the algorithm fails to converge */ public static double value(double order, double x) throws MathIllegalArgumentException, ConvergenceException { final int n = (int) order; final double alpha = order - n; final int nb = n + 1; final BesselJResult res = rjBesl(x, alpha, nb); if (res.nVals >= nb) { return res.vals[n]; } else if (res.nVals < 0) { throw new MathIllegalArgumentException(LocalizedFormats.BESSEL_FUNCTION_BAD_ARGUMENT,order, x); } else if (FastMath.abs(res.vals[res.nVals - 1]) < 1e-100) { return res.vals[n]; // underflow; return value (will be zero) } throw new ConvergenceException(LocalizedFormats.BESSEL_FUNCTION_FAILED_CONVERGENCE, order, x); }
/** * Computes the Pearson's product-moment correlation coefficient between two arrays. * * <p>Throws MathIllegalArgumentException if the arrays do not have the same length * or their common length is less than 2. Returns {@code NaN} if either of the arrays * has zero variance (i.e., if one of the arrays does not contain at least two distinct * values).</p> * * @param xArray first data array * @param yArray second data array * @return Returns Pearson's correlation coefficient for the two arrays * @throws DimensionMismatchException if the arrays lengths do not match * @throws MathIllegalArgumentException if there is insufficient data */ public double correlation(final double[] xArray, final double[] yArray) { SimpleRegression regression = new SimpleRegression(); if (xArray.length != yArray.length) { throw new DimensionMismatchException(xArray.length, yArray.length); } else if (xArray.length < 2) { throw new MathIllegalArgumentException(LocalizedFormats.INSUFFICIENT_DIMENSION, xArray.length, 2); } else { for(int i=0; i<xArray.length; i++) { regression.addData(xArray[i], yArray[i]); } return regression.getR(); } }
/** Reset the instance as if built from two points. * @param p1 first point belonging to the line (this can be any point) * @param p2 second point belonging to the line (this can be any point, different from p1) * @exception MathIllegalArgumentException if the points are equal */ public void reset(final Vector3D p1, final Vector3D p2) throws MathIllegalArgumentException { final Vector3D delta = p2.subtract(p1); final double norm2 = delta.getNormSq(); if (norm2 == 0.0) { throw new MathIllegalArgumentException(LocalizedFormats.ZERO_NORM); } this.direction = new Vector3D(1.0 / FastMath.sqrt(norm2), delta); zero = new Vector3D(1.0, p1, -p1.dotProduct(delta) / norm2, delta); }
/** Build a rotation from an axis and an angle. * @param axis axis around which to rotate * @param angle rotation angle * @param convention convention to use for the semantics of the angle * @exception MathIllegalArgumentException if the axis norm is zero * @since 3.6 */ public Rotation(final Vector3D axis, final double angle, final RotationConvention convention) throws MathIllegalArgumentException { double norm = axis.getNorm(); if (norm == 0) { throw new MathIllegalArgumentException(LocalizedFormats.ZERO_NORM_FOR_ROTATION_AXIS); } double halfAngle = convention == RotationConvention.VECTOR_OPERATOR ? -0.5 * angle : +0.5 * angle; double coeff = FastMath.sin(halfAngle) / norm; q0 = FastMath.cos (halfAngle); q1 = coeff * axis.getX(); q2 = coeff * axis.getY(); q3 = coeff * axis.getZ(); }