Java Code Examples for org.apache.commons.math3.util.MathArrays#verifyValues()

The following examples show how to use org.apache.commons.math3.util.MathArrays#verifyValues() . 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: Percentile.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Remove the occurrence of a given value in a copied slice of array
 * defined by the array part from [begin, begin+length).
 * @param values the input array
 * @param begin start index of the array to include
 * @param length number of elements to include from begin
 * @param removedValue the value to be removed from the sliced array
 * @return the copy of the sliced array after removing the removedValue
 */
private static double[] removeAndSlice(final double[] values,
                                       final int begin, final int length,
                                       final double removedValue) {
    MathArrays.verifyValues(values, begin, length);
    final double[] temp;
    //BitSet(length) to indicate where the removedValue is located
    final BitSet bits = new BitSet(length);
    for (int i = begin; i < begin+length; i++) {
        if (Precision.equalsIncludingNaN(removedValue, values[i])) {
            bits.set(i - begin);
        }
    }
    //Check if empty then create a new copy
    if (bits.isEmpty()) {
        temp = copyOf(values, begin, length); // Nothing removed, just copy
    } else if(bits.cardinality() == length){
        temp = new double[0];                 // All removed, just empty
    }else {                                   // Some removable, so new
        temp = new double[length - bits.cardinality()];
        int start = begin;  //start index from source array (i.e values)
        int dest = 0;       //dest index in destination array(i.e temp)
        int nextOne = -1;   //nextOne is the index of bit set of next one
        int bitSetPtr = 0;  //bitSetPtr is start index pointer of bitset
        while ((nextOne = bits.nextSetBit(bitSetPtr)) != -1) {
            final int lengthToCopy = nextOne - bitSetPtr;
            System.arraycopy(values, start, temp, dest, lengthToCopy);
            dest += lengthToCopy;
            start = begin + (bitSetPtr = bits.nextClearBit(nextOne));
        }
        //Copy any residue past start index till begin+length
        if (start < begin + length) {
            System.arraycopy(values,start,temp,dest,begin + length - start);
        }
    }
    return temp;
}
 
Example 2
Source File: Percentile.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Remove the occurrence of a given value in a copied slice of array
 * defined by the array part from [begin, begin+length).
 * @param values the input array
 * @param begin start index of the array to include
 * @param length number of elements to include from begin
 * @param removedValue the value to be removed from the sliced array
 * @return the copy of the sliced array after removing the removedValue
 */
private static double[] removeAndSlice(final double[] values,
                                       final int begin, final int length,
                                       final double removedValue) {
    MathArrays.verifyValues(values, begin, length);
    final double[] temp;
    //BitSet(length) to indicate where the removedValue is located
    final BitSet bits = new BitSet(length);
    for (int i = begin; i < begin+length; i++) {
        if (Precision.equalsIncludingNaN(removedValue, values[i])) {
            bits.set(i - begin);
        }
    }
    //Check if empty then create a new copy
    if (bits.isEmpty()) {
        temp = copyOf(values, begin, length); // Nothing removed, just copy
    } else if(bits.cardinality() == length){
        temp = new double[0];                 // All removed, just empty
    }else {                                   // Some removable, so new
        temp = new double[length - bits.cardinality()];
        int start = begin;  //start index from source array (i.e values)
        int dest = 0;       //dest index in destination array(i.e temp)
        int nextOne = -1;   //nextOne is the index of bit set of next one
        int bitSetPtr = 0;  //bitSetPtr is start index pointer of bitset
        while ((nextOne = bits.nextSetBit(bitSetPtr)) != -1) {
            final int lengthToCopy = nextOne - bitSetPtr;
            System.arraycopy(values, start, temp, dest, lengthToCopy);
            dest += lengthToCopy;
            start = begin + (bitSetPtr = bits.nextClearBit(nextOne));
        }
        //Copy any residue past start index till begin+length
        if (start < begin + length) {
            System.arraycopy(values,start,temp,dest,begin + length - start);
        }
    }
    return temp;
}
 
Example 3
Source File: AbstractUnivariateStatistic.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * This method is used by <code>evaluate(double[], int, int)</code> methods
 * to verify that the input parameters designate a subarray of positive length.
 * <p>
 * <ul>
 * <li>returns <code>true</code> iff the parameters designate a subarray of
 * positive length</li>
 * <li>throws <code>MathIllegalArgumentException</code> if the array is null or
 * or the indices are invalid</li>
 * <li>returns <code>false</li> if the array is non-null, but
 * <code>length</code> is 0.
 * </ul></p>
 *
 * @param values the input array
 * @param begin index of the first array element to include
 * @param length the number of elements to include
 * @return true if the parameters are valid and designate a subarray of positive length
 * @throws MathIllegalArgumentException if the indices are invalid or the array is null
 */
protected boolean test(
    final double[] values,
    final int begin,
    final int length) throws MathIllegalArgumentException {
    return MathArrays.verifyValues(values, begin, length, false);
}
 
Example 4
Source File: AbstractUnivariateStatistic.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * This method is used by <code>evaluate(double[], double[], int, int)</code> methods
 * to verify that the begin and length parameters designate a subarray of positive length
 * and the weights are all non-negative, non-NaN, finite, and not all zero.
 * <p>
 * <ul>
 * <li>returns <code>true</code> iff the parameters designate a subarray of
 * positive length and the weights array contains legitimate values.</li>
 * <li>throws <code>IllegalArgumentException</code> if any of the following are true:
 * <ul><li>the values array is null</li>
 *     <li>the weights array is null</li>
 *     <li>the weights array does not have the same length as the values array</li>
 *     <li>the weights array contains one or more infinite values</li>
 *     <li>the weights array contains one or more NaN values</li>
 *     <li>the weights array contains negative values</li>
 *     <li>the start and length arguments do not determine a valid array</li></ul>
 * </li>
 * <li>returns <code>false</li> if the array is non-null, but
 * <code>length</code> is 0.
 * </ul></p>
 *
 * @param values the input array
 * @param weights the weights array
 * @param begin index of the first array element to include
 * @param length the number of elements to include
 * @return true if the parameters are valid and designate a subarray of positive length
 * @throws MathIllegalArgumentException if the indices are invalid or the array is null
 * @since 2.1
 */
protected boolean test(
    final double[] values,
    final double[] weights,
    final int begin,
    final int length) throws MathIllegalArgumentException {
    return MathArrays.verifyValues(values, weights, begin, length, false);
}
 
Example 5
Source File: AbstractUnivariateStatistic.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * This method is used by <code>evaluate(double[], int, int)</code> methods
 * to verify that the input parameters designate a subarray of positive length.
 * <p>
 * <ul>
 * <li>returns <code>true</code> iff the parameters designate a subarray of
 * positive length</li>
 * <li>throws <code>MathIllegalArgumentException</code> if the array is null or
 * or the indices are invalid</li>
 * <li>returns <code>false</li> if the array is non-null, but
 * <code>length</code> is 0.
 * </ul></p>
 *
 * @param values the input array
 * @param begin index of the first array element to include
 * @param length the number of elements to include
 * @return true if the parameters are valid and designate a subarray of positive length
 * @throws MathIllegalArgumentException if the indices are invalid or the array is null
 */
protected boolean test(
    final double[] values,
    final int begin,
    final int length) throws MathIllegalArgumentException {
    return MathArrays.verifyValues(values, begin, length, false);
}
 
Example 6
Source File: AbstractUnivariateStatistic.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * This method is used by <code>evaluate(double[], double[], int, int)</code> methods
 * to verify that the begin and length parameters designate a subarray of positive length
 * and the weights are all non-negative, non-NaN, finite, and not all zero.
 * <p>
 * <ul>
 * <li>returns <code>true</code> iff the parameters designate a subarray of
 * positive length and the weights array contains legitimate values.</li>
 * <li>throws <code>IllegalArgumentException</code> if any of the following are true:
 * <ul><li>the values array is null</li>
 *     <li>the weights array is null</li>
 *     <li>the weights array does not have the same length as the values array</li>
 *     <li>the weights array contains one or more infinite values</li>
 *     <li>the weights array contains one or more NaN values</li>
 *     <li>the weights array contains negative values</li>
 *     <li>the start and length arguments do not determine a valid array</li></ul>
 * </li>
 * <li>returns <code>false</li> if the array is non-null, but
 * <code>length</code> is 0.
 * </ul></p>
 *
 * @param values the input array
 * @param weights the weights array
 * @param begin index of the first array element to include
 * @param length the number of elements to include
 * @return true if the parameters are valid and designate a subarray of positive length
 * @throws MathIllegalArgumentException if the indices are invalid or the array is null
 * @since 2.1
 */
protected boolean test(
    final double[] values,
    final double[] weights,
    final int begin,
    final int length) throws MathIllegalArgumentException {
    return MathArrays.verifyValues(values, weights, begin, length, false);
}
 
Example 7
Source File: Percentile.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Make a copy of the array for the slice defined by array part from
 * [begin, begin+length)
 * @param values the input array
 * @param begin start index of the array to include
 * @param length number of elements to include from begin
 * @return copy of a slice of the original array
 */
private static double[] copyOf(final double[] values, final int begin, final int length) {
    MathArrays.verifyValues(values, begin, length);
    return MathArrays.copyOfRange(values, begin, begin + length);
}
 
Example 8
Source File: AbstractUnivariateStatistic.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * This method is used by <code>evaluate(double[], int, int)</code> methods
 * to verify that the input parameters designate a subarray of positive length.
 * <p>
 * <ul>
 * <li>returns <code>true</code> iff the parameters designate a subarray of
 * non-negative length</li>
 * <li>throws <code>IllegalArgumentException</code> if the array is null or
 * or the indices are invalid</li>
 * <li>returns <code>false</li> if the array is non-null, but
 * <code>length</code> is 0 unless <code>allowEmpty</code> is <code>true</code>
 * </ul></p>
 *
 * @param values the input array
 * @param begin index of the first array element to include
 * @param length the number of elements to include
 * @param allowEmpty if <code>true</code> then zero length arrays are allowed
 * @return true if the parameters are valid
 * @throws MathIllegalArgumentException if the indices are invalid or the array is null
 * @since 3.0
 */
protected boolean test(final double[] values, final int begin,
        final int length, final boolean allowEmpty) throws MathIllegalArgumentException {
    return MathArrays.verifyValues(values, begin, length, allowEmpty);
}
 
Example 9
Source File: AbstractUnivariateStatistic.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * This method is used by <code>evaluate(double[], double[], int, int)</code> methods
 * to verify that the begin and length parameters designate a subarray of positive length
 * and the weights are all non-negative, non-NaN, finite, and not all zero.
 * <p>
 * <ul>
 * <li>returns <code>true</code> iff the parameters designate a subarray of
 * non-negative length and the weights array contains legitimate values.</li>
 * <li>throws <code>MathIllegalArgumentException</code> if any of the following are true:
 * <ul><li>the values array is null</li>
 *     <li>the weights array is null</li>
 *     <li>the weights array does not have the same length as the values array</li>
 *     <li>the weights array contains one or more infinite values</li>
 *     <li>the weights array contains one or more NaN values</li>
 *     <li>the weights array contains negative values</li>
 *     <li>the start and length arguments do not determine a valid array</li></ul>
 * </li>
 * <li>returns <code>false</li> if the array is non-null, but
 * <code>length</code> is 0 unless <code>allowEmpty</code> is <code>true</code>.
 * </ul></p>
 *
 * @param values the input array.
 * @param weights the weights array.
 * @param begin index of the first array element to include.
 * @param length the number of elements to include.
 * @param allowEmpty if {@code true} than allow zero length arrays to pass.
 * @return {@code true} if the parameters are valid.
 * @throws NullArgumentException if either of the arrays are null
 * @throws MathIllegalArgumentException if the array indices are not valid,
 * the weights array contains NaN, infinite or negative elements, or there
 * are no positive weights.
 * @since 3.0
 */
protected boolean test(final double[] values, final double[] weights,
        final int begin, final int length, final boolean allowEmpty) throws MathIllegalArgumentException {

    return MathArrays.verifyValues(values, weights, begin, length, allowEmpty);
}
 
Example 10
Source File: Percentile.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Make a copy of the array for the slice defined by array part from
 * [begin, begin+length)
 * @param values the input array
 * @param begin start index of the array to include
 * @param length number of elements to include from begin
 * @return copy of a slice of the original array
 */
private static double[] copyOf(final double[] values, final int begin, final int length) {
    MathArrays.verifyValues(values, begin, length);
    return MathArrays.copyOfRange(values, begin, begin + length);
}
 
Example 11
Source File: AbstractUnivariateStatistic.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * This method is used by <code>evaluate(double[], int, int)</code> methods
 * to verify that the input parameters designate a subarray of positive length.
 * <p>
 * <ul>
 * <li>returns <code>true</code> iff the parameters designate a subarray of
 * non-negative length</li>
 * <li>throws <code>IllegalArgumentException</code> if the array is null or
 * or the indices are invalid</li>
 * <li>returns <code>false</li> if the array is non-null, but
 * <code>length</code> is 0 unless <code>allowEmpty</code> is <code>true</code>
 * </ul></p>
 *
 * @param values the input array
 * @param begin index of the first array element to include
 * @param length the number of elements to include
 * @param allowEmpty if <code>true</code> then zero length arrays are allowed
 * @return true if the parameters are valid
 * @throws MathIllegalArgumentException if the indices are invalid or the array is null
 * @since 3.0
 */
protected boolean test(final double[] values, final int begin,
        final int length, final boolean allowEmpty) throws MathIllegalArgumentException {
    return MathArrays.verifyValues(values, begin, length, allowEmpty);
}
 
Example 12
Source File: AbstractUnivariateStatistic.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * This method is used by <code>evaluate(double[], double[], int, int)</code> methods
 * to verify that the begin and length parameters designate a subarray of positive length
 * and the weights are all non-negative, non-NaN, finite, and not all zero.
 * <p>
 * <ul>
 * <li>returns <code>true</code> iff the parameters designate a subarray of
 * non-negative length and the weights array contains legitimate values.</li>
 * <li>throws <code>MathIllegalArgumentException</code> if any of the following are true:
 * <ul><li>the values array is null</li>
 *     <li>the weights array is null</li>
 *     <li>the weights array does not have the same length as the values array</li>
 *     <li>the weights array contains one or more infinite values</li>
 *     <li>the weights array contains one or more NaN values</li>
 *     <li>the weights array contains negative values</li>
 *     <li>the start and length arguments do not determine a valid array</li></ul>
 * </li>
 * <li>returns <code>false</li> if the array is non-null, but
 * <code>length</code> is 0 unless <code>allowEmpty</code> is <code>true</code>.
 * </ul></p>
 *
 * @param values the input array.
 * @param weights the weights array.
 * @param begin index of the first array element to include.
 * @param length the number of elements to include.
 * @param allowEmpty if {@code true} than allow zero length arrays to pass.
 * @return {@code true} if the parameters are valid.
 * @throws NullArgumentException if either of the arrays are null
 * @throws MathIllegalArgumentException if the array indices are not valid,
 * the weights array contains NaN, infinite or negative elements, or there
 * are no positive weights.
 * @since 3.0
 */
protected boolean test(final double[] values, final double[] weights,
        final int begin, final int length, final boolean allowEmpty) throws MathIllegalArgumentException {

    return MathArrays.verifyValues(values, weights, begin, length, allowEmpty);
}