Java Code Examples for org.apache.commons.math.MathRuntimeException#createIllegalArgumentException()

The following examples show how to use org.apache.commons.math.MathRuntimeException#createIllegalArgumentException() . 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: AbstractIntegerDistribution.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * For a random variable X whose values are distributed according
 * to this distribution, this method returns P(x0 ≤ X ≤ x1).
 *
 * @param x0 the (inclusive) lower bound
 * @param x1 the (inclusive) upper bound
 * @return the probability that a random variable with this distribution
 * will take a value between <code>x0</code> and <code>x1</code>,
 * including the endpoints.
 * @throws MathException if the cumulative probability can not be
 * computed due to convergence or other numerical errors.
 * @throws IllegalArgumentException if <code>x0 > x1</code>
 */
@Override
public double cumulativeProbability(double x0, double x1)
    throws MathException {
    if (x0 > x1) {
        throw MathRuntimeException.createIllegalArgumentException(
              WRONG_ORDER_ENDPOINTS_MESSAGE, x0, x1);
    }
    if (Math.floor(x0) < x0) {
        return cumulativeProbability(((int) Math.floor(x0)) + 1,
           (int) Math.floor(x1)); // don't want to count mass below x0
    } else { // x0 is mathematical integer, so use as is
        return cumulativeProbability((int) Math.floor(x0),
            (int) Math.floor(x1));
    }
}
 
Example 2
Source File: FractionFormat.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Formats an object and appends the result to a StringBuffer. <code>obj</code> must be either a
 * {@link Fraction} object or a {@link Number} object.  Any other type of
 * object will result in an {@link IllegalArgumentException} being thrown.
 *
 * @param obj the object to format.
 * @param toAppendTo where the text is to be appended
 * @param pos On input: an alignment field, if desired. On output: the
 *            offsets of the alignment field
 * @return the value passed in as toAppendTo.
 * @see java.text.Format#format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition)
 * @throws IllegalArgumentException is <code>obj</code> is not a valid type.
 */
@Override
public StringBuffer format(final Object obj,
                           final StringBuffer toAppendTo, final FieldPosition pos) {
    StringBuffer ret = null;

    if (obj instanceof Fraction) {
        ret = format((Fraction) obj, toAppendTo, pos);
    } else if (obj instanceof Number) {
        try {
            ret = format(new Fraction(((Number) obj).doubleValue()),
                         toAppendTo, pos);
        } catch (ConvergenceException ex) {
            throw MathRuntimeException.createIllegalArgumentException(
                "cannot convert given object to a fraction number: {0}",
                ex.getLocalizedMessage());
        }
    } else {
        throw MathRuntimeException.createIllegalArgumentException(
            "cannot format given object as a fraction number");
    }

    return ret;
}
 
Example 3
Source File: FastFourierTransformer.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Set a matrix element.
 * @param magnitude magnitude of the element
 * @param vector indices of the element
 * @return the previous value
 * @exception IllegalArgumentException if dimensions do not match
 */
public Complex set(Complex magnitude, int... vector)
    throws IllegalArgumentException {
    if (vector == null) {
        if (dimensionSize.length > 0) {
            throw MathRuntimeException.createIllegalArgumentException(
                    DIMENSION_MISMATCH_MESSAGE, 0, dimensionSize.length);
        }
        return null;
    }
    if (vector.length != dimensionSize.length) {
        throw MathRuntimeException.createIllegalArgumentException(
                DIMENSION_MISMATCH_MESSAGE, vector.length,dimensionSize.length);
    }

    Object[] lastDimension = (Object[]) multiDimensionalComplexArray;
    for (int i = 0; i < dimensionSize.length - 1; i++) {
        lastDimension = (Object[]) lastDimension[vector[i]];
    }

    Complex lastValue = (Complex) lastDimension[vector[dimensionSize.length - 1]];
    lastDimension[vector[dimensionSize.length - 1]] = magnitude;

    return lastValue;
}
 
Example 4
Source File: FastFourierTransformer.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Get the imaginary part of the k<sup>th</sup> n<sup>th</sup> root of unity
 * @param k index of the n<sup>th</sup> root of unity
 * @return imaginary part of the k<sup>th</sup> n<sup>th</sup> root of unity
 * @throws IllegalStateException if no roots of unity have been computed yet
 * @throws IllegalArgumentException if k is out of range
 */
public synchronized double getOmegaImaginary(int k)
  throws IllegalStateException, IllegalArgumentException {

  if (omegaCount == 0) {
      throw MathRuntimeException.createIllegalStateException(
              "roots of unity have not been computed yet");
  }
  if ((k < 0) || (k >= omegaCount)) {
    throw MathRuntimeException.createIllegalArgumentException(
            "out of range root of unity index {0} (must be in [{1};{2}])",
            k, 0, omegaCount - 1);
  }

  return (isForward) ?
      omegaImaginaryForward[k] : omegaImaginaryInverse[k];
  
}
 
Example 5
Source File: FastFourierTransformer.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Set a matrix element.
 * @param magnitude magnitude of the element
 * @param vector indices of the element
 * @return the previous value
 * @exception IllegalArgumentException if dimensions do not match
 */
public Complex set(Complex magnitude, int... vector)
    throws IllegalArgumentException {
    if (vector == null) {
        if (dimensionSize.length > 0) {
            throw MathRuntimeException.createIllegalArgumentException(
                    LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE, 0, dimensionSize.length);
        }
        return null;
    }
    if (vector.length != dimensionSize.length) {
        throw MathRuntimeException.createIllegalArgumentException(
                LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE, vector.length,dimensionSize.length);
    }

    Object[] lastDimension = (Object[]) multiDimensionalComplexArray;
    for (int i = 0; i < dimensionSize.length - 1; i++) {
        lastDimension = (Object[]) lastDimension[vector[i]];
    }

    Complex lastValue = (Complex) lastDimension[vector[dimensionSize.length - 1]];
    lastDimension[vector[dimensionSize.length - 1]] = magnitude;

    return lastValue;
}
 
Example 6
Source File: BigFractionFormat.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Formats an object and appends the result to a StringBuffer.
 * <code>obj</code> must be either a  {@link BigFraction} object or a
 * {@link BigInteger} object or a {@link Number} object. Any other type of
 * object will result in an {@link IllegalArgumentException} being thrown.
 *
 * @param obj the object to format.
 * @param toAppendTo where the text is to be appended
 * @param pos On input: an alignment field, if desired. On output: the
 *            offsets of the alignment field
 * @return the value passed in as toAppendTo.
 * @see java.text.Format#format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition)
 * @throws IllegalArgumentException is <code>obj</code> is not a valid type.
 */
@Override
public StringBuffer format(final Object obj,
                           final StringBuffer toAppendTo, final FieldPosition pos) {

    final StringBuffer ret;
    if (obj instanceof BigFraction) {
        ret = format((BigFraction) obj, toAppendTo, pos);
    } else if (obj instanceof BigInteger) {
        ret = format(new BigFraction((BigInteger) obj), toAppendTo, pos);
    } else if (obj instanceof Number) {
        ret = format(new BigFraction(((Number) obj).doubleValue()),
                     toAppendTo, pos);
    } else { 
        throw MathRuntimeException.createIllegalArgumentException(
            "cannot format given object as a fraction number");
    }
    
    return ret;
}
 
Example 7
Source File: BitsStreamGenerator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc} */
public int nextInt(int n) throws IllegalArgumentException {

    if (n < 1) {
        throw MathRuntimeException.createIllegalArgumentException(
              "upper bound must be positive ({0})", n);
    }

    // find bit mask for n
    int mask = n;
    mask |= mask >> 1;
    mask |= mask >> 2;
    mask |= mask >> 4;
    mask |= mask >> 8;
    mask |= mask >> 16;

    while (true) {
        final int random = next(32) & mask;
        if (random < n) {
            return random;
        }
    }

}
 
Example 8
Source File: FastFourierTransformer.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Sample the given univariate real function on the given interval.
 * <p>
 * The interval is divided equally into N sections and sample points
 * are taken from min to max-(max-min)/N. Usually f(x) is periodic
 * such that f(min) = f(max) (note max is not sampled), but we don't
 * require that.</p>
 *
 * @param f the function to be sampled
 * @param min the lower bound for the interval
 * @param max the upper bound for the interval
 * @param n the number of sample points
 * @return the samples array
 * @throws MathUserException if function cannot be evaluated at some point
 * @throws IllegalArgumentException if any parameters are invalid
 */
public static double[] sample(UnivariateRealFunction f, double min, double max, int n)
    throws MathUserException, IllegalArgumentException {

    if (n <= 0) {
        throw MathRuntimeException.createIllegalArgumentException(
                LocalizedFormats.NOT_POSITIVE_NUMBER_OF_SAMPLES,
                n);
    }
    verifyInterval(min, max);

    double s[] = new double[n];
    double h = (max - min) / n;
    for (int i = 0; i < n; i++) {
        s[i] = f.value(min + i * h);
    }
    return s;
}
 
Example 9
Source File: Arja_0046_s.java    From coming with MIT License 5 votes vote down vote up
/** Solve the linear equation A &times; X = B for symmetric matrices A.
 * <p>This method only find exact linear solutions, i.e. solutions for
 * which ||A &times; X - B|| is exactly 0.</p>
 * @param b right-hand side of the equation A &times; X = B
 * @return a matrix X that minimizes the two norm of A &times; X - B
 * @exception IllegalArgumentException if matrices dimensions don't match
 * @exception InvalidMatrixException if decomposed matrix is singular
 */
public RealMatrix solve(final RealMatrix b)
    throws IllegalArgumentException, InvalidMatrixException {

    if (!isNonSingular()) {
        throw new SingularMatrixException();
    }

    final int m = realEigenvalues.length;
    if (b.getRowDimension() != m) {
        throw MathRuntimeException.createIllegalArgumentException(
                "dimensions mismatch: got {0}x{1} but expected {2}x{3}",
                b.getRowDimension(), b.getColumnDimension(), m, "n");
    }

    final int nColB = b.getColumnDimension();
    final double[][] bp = new double[m][nColB];
    for (int k = 0; k < nColB; ++k) {
        for (int i = 0; i < m; ++i) {
            final ArrayRealVector v = eigenvectors[i];
            final double[] vData = v.getDataRef();
            double s = 0;
            for (int j = 0; j < m; ++j) {
                s += v.getEntry(j) * b.getEntry(j, k);
            }
            s /= realEigenvalues[i];
            for (int j = 0; j < m; ++j) {
                bp[j][k] += s * vData[j];
            }
        }
    }

    return MatrixUtils.createRealMatrix(bp);

}
 
Example 10
Source File: Cardumen_00273_s.java    From coming with MIT License 5 votes vote down vote up
/** Solve the linear equation A &times; X = B for symmetric matrices A.
 * <p>This method only find exact linear solutions, i.e. solutions for
 * which ||A &times; X - B|| is exactly 0.</p>
 * @param b right-hand side of the equation A &times; X = B
 * @return a vector X that minimizes the two norm of A &times; X - B
 * @exception IllegalArgumentException if matrices dimensions don't match
 * @exception InvalidMatrixException if decomposed matrix is singular
 */
public RealVector solve(final RealVector b)
    throws IllegalArgumentException, InvalidMatrixException {

    if (!isNonSingular()) {
        throw new SingularMatrixException();
    }

    final int m = realEigenvalues.length;
    if (b.getDimension() != m) {
        throw MathRuntimeException.createIllegalArgumentException(
                "vector length mismatch: got {0} but expected {1}",
                b.getDimension(), m);
    }

    final double[] bp = new double[m];
    for (int i = 0; i < m; ++i) {
        final ArrayRealVector v = eigenvectors[i];
        final double[] vData = v.getDataRef();
        final double s = v.dotProduct(b) / realEigenvalues[i];
        for (int j = 0; j < m; ++j) {
            bp[j] += s * vData[j];
        }
    }

    return new ArrayRealVector(bp, false);

}
 
Example 11
Source File: jKali_0050_s.java    From coming with MIT License 5 votes vote down vote up
/** Solve the linear equation A &times; X = B for symmetric matrices A.
 * <p>This method only find exact linear solutions, i.e. solutions for
 * which ||A &times; X - B|| is exactly 0.</p>
 * @param b right-hand side of the equation A &times; X = B
 * @return a matrix X that minimizes the two norm of A &times; X - B
 * @exception IllegalArgumentException if matrices dimensions don't match
 * @exception InvalidMatrixException if decomposed matrix is singular
 */
public RealMatrix solve(final RealMatrix b)
    throws IllegalArgumentException, InvalidMatrixException {

    if (!isNonSingular()) {
        throw new SingularMatrixException();
    }

    final int m = realEigenvalues.length;
    if (b.getRowDimension() != m) {
        throw MathRuntimeException.createIllegalArgumentException(
                "dimensions mismatch: got {0}x{1} but expected {2}x{3}",
                b.getRowDimension(), b.getColumnDimension(), m, "n");
    }

    final int nColB = b.getColumnDimension();
    final double[][] bp = new double[m][nColB];
    for (int k = 0; k < nColB; ++k) {
        for (int i = 0; i < m; ++i) {
            final ArrayRealVector v = eigenvectors[i];
            final double[] vData = v.getDataRef();
            double s = 0;
            for (int j = 0; j < m; ++j) {
                s += v.getEntry(j) * b.getEntry(j, k);
            }
            s /= realEigenvalues[i];
            for (int j = 0; j < m; ++j) {
                bp[j][k] += s * vData[j];
            }
        }
    }

    return MatrixUtils.createRealMatrix(bp);

}
 
Example 12
Source File: FastFourierTransformer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Verifies that the endpoints specify an interval.
 * 
 * @param lower lower endpoint
 * @param upper upper endpoint
 * @throws IllegalArgumentException if not interval
 */
public static void verifyInterval(double lower, double upper)
    throws IllegalArgumentException {

    if (lower >= upper) {
        throw MathRuntimeException.createIllegalArgumentException(
                "endpoints do not specify an interval: [{0}, {1}]",
                lower, upper);
    }       
}
 
Example 13
Source File: JGenProg2017_0037_s.java    From coming with MIT License 5 votes vote down vote up
/** Solve the linear equation A &times; X = B for symmetric matrices A.
 * <p>This method only find exact linear solutions, i.e. solutions for
 * which ||A &times; X - B|| is exactly 0.</p>
 * @param b right-hand side of the equation A &times; X = B
 * @return a vector X that minimizes the two norm of A &times; X - B
 * @exception IllegalArgumentException if matrices dimensions don't match
 * @exception InvalidMatrixException if decomposed matrix is singular
 */
public double[] solve(final double[] b)
    throws IllegalArgumentException, InvalidMatrixException {

    if (!isNonSingular()) {
        throw new SingularMatrixException();
    }

    final int m = realEigenvalues.length;
    if (b.length != m) {
        throw MathRuntimeException.createIllegalArgumentException(
                "vector length mismatch: got {0} but expected {1}",
                b.length, m);
    }

    final double[] bp = new double[m];
    for (int i = 0; i < m; ++i) {
        final ArrayRealVector v = eigenvectors[i];
        final double[] vData = v.getDataRef();
        final double s = v.dotProduct(b) / realEigenvalues[i];
        for (int j = 0; j < m; ++j) {
            bp[j] += s * vData[j];
        }
    }

    return bp;

}
 
Example 14
Source File: Nopol2017_0080_s.java    From coming with MIT License 5 votes vote down vote up
/** Solve the linear equation A &times; X = B for symmetric matrices A.
 * <p>This method only find exact linear solutions, i.e. solutions for
 * which ||A &times; X - B|| is exactly 0.</p>
 * @param b right-hand side of the equation A &times; X = B
 * @return a vector X that minimizes the two norm of A &times; X - B
 * @exception IllegalArgumentException if matrices dimensions don't match
 * @exception InvalidMatrixException if decomposed matrix is singular
 */
public RealVector solve(final RealVector b)
    throws IllegalArgumentException, InvalidMatrixException {

    if (!isNonSingular()) {
        throw new SingularMatrixException();
    }

    final int m = realEigenvalues.length;
    if (b.getDimension() != m) {
        throw MathRuntimeException.createIllegalArgumentException(
                "vector length mismatch: got {0} but expected {1}",
                b.getDimension(), m);
    }

    final double[] bp = new double[m];
    for (int i = 0; i < m; ++i) {
        final ArrayRealVector v = eigenvectors[i];
        final double[] vData = v.getDataRef();
        final double s = v.dotProduct(b) / realEigenvalues[i];
        for (int j = 0; j < m; ++j) {
            bp[j] += s * vData[j];
        }
    }

    return new ArrayRealVector(bp, false);

}
 
Example 15
Source File: ExponentialDistributionImpl.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Modify the mean.
 * @param mean the new mean.
 * @throws IllegalArgumentException if <code>mean</code> is not positive.
 */
public void setMean(double mean) {
    if (mean <= 0.0) {
        throw MathRuntimeException.createIllegalArgumentException(
              "mean must be positive ({0})", mean);
    }
    this.mean = mean;
}
 
Example 16
Source File: ArrayFieldVector.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Check if instance dimension is equal to some expected value.
 *
 * @param n expected dimension.
 * @exception IllegalArgumentException if the dimension is
 * inconsistent with vector size
 */
protected void checkVectorDimensions(int n)
    throws IllegalArgumentException {
    if (data.length != n) {
        throw MathRuntimeException.createIllegalArgumentException(
                "vector length mismatch: got {0} but expected {1}",
                data.length, n);
    }
}
 
Example 17
Source File: Nopol2017_0080_t.java    From coming with MIT License 5 votes vote down vote up
/** Solve the linear equation A &times; X = B for symmetric matrices A.
 * <p>This method only find exact linear solutions, i.e. solutions for
 * which ||A &times; X - B|| is exactly 0.</p>
 * @param b right-hand side of the equation A &times; X = B
 * @return a vector X that minimizes the two norm of A &times; X - B
 * @exception IllegalArgumentException if matrices dimensions don't match
 * @exception InvalidMatrixException if decomposed matrix is singular
 */
public RealVector solve(final RealVector b)
    throws IllegalArgumentException, InvalidMatrixException {

    if (!isNonSingular()) {
        throw new SingularMatrixException();
    }

    final int m = realEigenvalues.length;
    if (b.getDimension() != m) {
        throw MathRuntimeException.createIllegalArgumentException(
                "vector length mismatch: got {0} but expected {1}",
                b.getDimension(), m);
    }

    final double[] bp = new double[m];
    for (int i = 0; i < m; ++i) {
        final ArrayRealVector v = eigenvectors[i];
        final double[] vData = v.getDataRef();
        final double s = v.dotProduct(b) / realEigenvalues[i];
        for (int j = 0; j < m; ++j) {
            bp[j] += s * vData[j];
        }
    }

    return new ArrayRealVector(bp, false);

}
 
Example 18
Source File: AbstractFieldMatrix.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Check if a matrix is multiplication compatible with the instance
 * @param m matrix to check
 * @exception IllegalArgumentException if matrix is not multiplication compatible with instance
 */
protected void checkMultiplicationCompatible(final FieldMatrix<T> m) {
    if (getColumnDimension() != m.getRowDimension()) {
        throw MathRuntimeException.createIllegalArgumentException(
                "{0}x{1} and {2}x{3} matrices are not multiplication compatible",
                getRowDimension(), getColumnDimension(),
                m.getRowDimension(), m.getColumnDimension());
    }
}
 
Example 19
Source File: UnivariateRealSolverUtils.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Checks to see if f is null, throwing IllegalArgumentException if so.
 * @param f  input function
 * @throws IllegalArgumentException if f is null
 */
private static void setup(UnivariateRealFunction f) {
    if (f == null) {
        throw MathRuntimeException.createIllegalArgumentException(NULL_FUNCTION_MESSAGE);
    }
}
 
Example 20
Source File: Percentile.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Sets the value of the quantile field (determines what percentile is
 * computed when evaluate() is called with no quantile argument).
 *
 * @param p a value between 0 < p <= 100
 * @throws IllegalArgumentException  if p is not greater than 0 and less
 * than or equal to 100
 */
public void setQuantile(final double p) {
    if (p <= 0 || p > 100) {
        throw MathRuntimeException.createIllegalArgumentException(
              "out of bounds quantile value: {0}, must be in (0, 100]", p);
    }
    quantile = p;
}