Java Code Examples for org.apache.commons.math3.exception.util.LocalizedFormats#INFINITE_ARRAY_ELEMENT

The following examples show how to use org.apache.commons.math3.exception.util.LocalizedFormats#INFINITE_ARRAY_ELEMENT . 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: AbstractUnivariateStatistic.java    From astor with GNU General Public License v2.0 5 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 {

    if (weights == null || values == null) {
        throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
    }

    if (weights.length != values.length) {
        throw new DimensionMismatchException(weights.length, values.length);
    }

    boolean containsPositiveWeight = false;
    for (int i = begin; i < begin + length; i++) {
        if (Double.isNaN(weights[i])) {
            throw new MathIllegalArgumentException(LocalizedFormats.NAN_ELEMENT_AT_INDEX, i);
        }
        if (Double.isInfinite(weights[i])) {
            throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_ARRAY_ELEMENT, weights[i], i);
        }
        if (weights[i] < 0) {
            throw new MathIllegalArgumentException(LocalizedFormats.NEGATIVE_ELEMENT_AT_INDEX, i, weights[i]);
        }
        if (!containsPositiveWeight && weights[i] > 0.0) {
            containsPositiveWeight = true;
        }
    }

    if (!containsPositiveWeight) {
        throw new MathIllegalArgumentException(LocalizedFormats.WEIGHT_AT_LEAST_ONE_NON_ZERO);
    }

    return test(values, begin, length, allowEmpty);
}
 
Example 2
Source File: MathArrays.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This method is used
 * 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.3
 */
public static boolean verifyValues(final double[] values, final double[] weights,
        final int begin, final int length, final boolean allowEmpty) throws MathIllegalArgumentException {

    if (weights == null || values == null) {
        throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
    }

    if (weights.length != values.length) {
        throw new DimensionMismatchException(weights.length, values.length);
    }

    boolean containsPositiveWeight = false;
    for (int i = begin; i < begin + length; i++) {
        final double weight = weights[i];
        if (Double.isNaN(weight)) {
            throw new MathIllegalArgumentException(LocalizedFormats.NAN_ELEMENT_AT_INDEX, Integer.valueOf(i));
        }
        if (Double.isInfinite(weight)) {
            throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_ARRAY_ELEMENT, Double.valueOf(weight), Integer.valueOf(i));
        }
        if (weight < 0) {
            throw new MathIllegalArgumentException(LocalizedFormats.NEGATIVE_ELEMENT_AT_INDEX, Integer.valueOf(i), Double.valueOf(weight));
        }
        if (!containsPositiveWeight && weight > 0.0) {
            containsPositiveWeight = true;
        }
    }

    if (!containsPositiveWeight) {
        throw new MathIllegalArgumentException(LocalizedFormats.WEIGHT_AT_LEAST_ONE_NON_ZERO);
    }

    return verifyValues(values, begin, length, allowEmpty);
}
 
Example 3
Source File: MathArrays.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Normalizes an array to make it sum to a specified value.
 * Returns the result of the transformation
 * <pre>
 *    x |-> x * normalizedSum / sum
 * </pre>
 * applied to each non-NaN element x of the input array, where sum is the
 * sum of the non-NaN entries in the input array.
 * <p>
 * Throws IllegalArgumentException if {@code normalizedSum} is infinite
 * or NaN and ArithmeticException if the input array contains any infinite elements
 * or sums to 0.
 * <p>
 * Ignores (i.e., copies unchanged to the output array) NaNs in the input array.
 *
 * @param values Input array to be normalized
 * @param normalizedSum Target sum for the normalized array
 * @return the normalized array.
 * @throws MathArithmeticException if the input array contains infinite
 * elements or sums to zero.
 * @throws MathIllegalArgumentException if the target sum is infinite or {@code NaN}.
 * @since 2.1
 */
public static double[] normalizeArray(double[] values, double normalizedSum)
    throws MathIllegalArgumentException, MathArithmeticException {
    if (Double.isInfinite(normalizedSum)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NORMALIZE_INFINITE);
    }
    if (Double.isNaN(normalizedSum)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NORMALIZE_NAN);
    }
    double sum = 0d;
    final int len = values.length;
    double[] out = new double[len];
    for (int i = 0; i < len; i++) {
        if (Double.isInfinite(values[i])) {
            throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_ARRAY_ELEMENT, values[i], i);
        }
        if (!Double.isNaN(values[i])) {
            sum += values[i];
        }
    }
    if (sum == 0) {
        throw new MathArithmeticException(LocalizedFormats.ARRAY_SUMS_TO_ZERO);
    }
    for (int i = 0; i < len; i++) {
        if (Double.isNaN(values[i])) {
            out[i] = Double.NaN;
        } else {
            out[i] = values[i] * normalizedSum / sum;
        }
    }
    return out;
}
 
Example 4
Source File: MathArrays.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Normalizes an array to make it sum to a specified value.
 * Returns the result of the transformation <pre>
 *    x |-> x * normalizedSum / sum
 * </pre>
 * applied to each non-NaN element x of the input array, where sum is the
 * sum of the non-NaN entries in the input array.</p>
 *
 * <p>Throws IllegalArgumentException if {@code normalizedSum} is infinite
 * or NaN and ArithmeticException if the input array contains any infinite elements
 * or sums to 0.</p>
 *
 * <p>Ignores (i.e., copies unchanged to the output array) NaNs in the input array.</p>
 *
 * @param values Input array to be normalized
 * @param normalizedSum Target sum for the normalized array
 * @return the normalized array.
 * @throws MathArithmeticException if the input array contains infinite
 * elements or sums to zero.
 * @throws MathIllegalArgumentException if the target sum is infinite or {@code NaN}.
 * @since 2.1
 */
public static double[] normalizeArray(double[] values, double normalizedSum)
    throws MathIllegalArgumentException, MathArithmeticException {
    if (Double.isInfinite(normalizedSum)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NORMALIZE_INFINITE);
    }
    if (Double.isNaN(normalizedSum)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NORMALIZE_NAN);
    }
    double sum = 0d;
    final int len = values.length;
    double[] out = new double[len];
    for (int i = 0; i < len; i++) {
        if (Double.isInfinite(values[i])) {
            throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_ARRAY_ELEMENT, values[i], i);
        }
        if (!Double.isNaN(values[i])) {
            sum += values[i];
        }
    }
    if (sum == 0) {
        throw new MathArithmeticException(LocalizedFormats.ARRAY_SUMS_TO_ZERO);
    }
    for (int i = 0; i < len; i++) {
        if (Double.isNaN(values[i])) {
            out[i] = Double.NaN;
        } else {
            out[i] = values[i] * normalizedSum / sum;
        }
    }
    return out;
}
 
Example 5
Source File: AbstractUnivariateStatistic.java    From astor with GNU General Public License v2.0 5 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 {

    if (weights == null || values == null) {
        throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
    }

    if (weights.length != values.length) {
        throw new DimensionMismatchException(weights.length, values.length);
    }

    boolean containsPositiveWeight = false;
    for (int i = begin; i < begin + length; i++) {
        if (Double.isNaN(weights[i])) {
            throw new MathIllegalArgumentException(LocalizedFormats.NAN_ELEMENT_AT_INDEX, i);
        }
        if (Double.isInfinite(weights[i])) {
            throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_ARRAY_ELEMENT, weights[i], i);
        }
        if (weights[i] < 0) {
            throw new MathIllegalArgumentException(LocalizedFormats.NEGATIVE_ELEMENT_AT_INDEX, i, weights[i]);
        }
        if (!containsPositiveWeight && weights[i] > 0.0) {
            containsPositiveWeight = true;
        }
    }

    if (!containsPositiveWeight) {
        throw new MathIllegalArgumentException(LocalizedFormats.WEIGHT_AT_LEAST_ONE_NON_ZERO);
    }

    return test(values, begin, length, allowEmpty);
}
 
Example 6
Source File: MathArrays.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Normalizes an array to make it sum to a specified value.
 * Returns the result of the transformation <pre>
 *    x |-> x * normalizedSum / sum
 * </pre>
 * applied to each non-NaN element x of the input array, where sum is the
 * sum of the non-NaN entries in the input array.</p>
 *
 * <p>Throws IllegalArgumentException if {@code normalizedSum} is infinite
 * or NaN and ArithmeticException if the input array contains any infinite elements
 * or sums to 0.</p>
 *
 * <p>Ignores (i.e., copies unchanged to the output array) NaNs in the input array.</p>
 *
 * @param values Input array to be normalized
 * @param normalizedSum Target sum for the normalized array
 * @return the normalized array.
 * @throws MathArithmeticException if the input array contains infinite
 * elements or sums to zero.
 * @throws MathIllegalArgumentException if the target sum is infinite or {@code NaN}.
 * @since 2.1
 */
public static double[] normalizeArray(double[] values, double normalizedSum)
    throws MathIllegalArgumentException, MathArithmeticException {
    if (Double.isInfinite(normalizedSum)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NORMALIZE_INFINITE);
    }
    if (Double.isNaN(normalizedSum)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NORMALIZE_NAN);
    }
    double sum = 0d;
    final int len = values.length;
    double[] out = new double[len];
    for (int i = 0; i < len; i++) {
        if (Double.isInfinite(values[i])) {
            throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_ARRAY_ELEMENT, values[i], i);
        }
        if (!Double.isNaN(values[i])) {
            sum += values[i];
        }
    }
    if (sum == 0) {
        throw new MathArithmeticException(LocalizedFormats.ARRAY_SUMS_TO_ZERO);
    }
    for (int i = 0; i < len; i++) {
        if (Double.isNaN(values[i])) {
            out[i] = Double.NaN;
        } else {
            out[i] = values[i] * normalizedSum / sum;
        }
    }
    return out;
}
 
Example 7
Source File: AbstractUnivariateStatistic.java    From astor with GNU General Public License v2.0 5 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 {

    if (weights == null || values == null) {
        throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
    }

    if (weights.length != values.length) {
        throw new DimensionMismatchException(weights.length, values.length);
    }

    boolean containsPositiveWeight = false;
    for (int i = begin; i < begin + length; i++) {
        if (Double.isNaN(weights[i])) {
            throw new MathIllegalArgumentException(LocalizedFormats.NAN_ELEMENT_AT_INDEX, i);
        }
        if (Double.isInfinite(weights[i])) {
            throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_ARRAY_ELEMENT, weights[i], i);
        }
        if (weights[i] < 0) {
            throw new MathIllegalArgumentException(LocalizedFormats.NEGATIVE_ELEMENT_AT_INDEX, i, weights[i]);
        }
        if (!containsPositiveWeight && weights[i] > 0.0) {
            containsPositiveWeight = true;
        }
    }

    if (!containsPositiveWeight) {
        throw new MathIllegalArgumentException(LocalizedFormats.WEIGHT_AT_LEAST_ONE_NON_ZERO);
    }

    return test(values, begin, length, allowEmpty);
}
 
Example 8
Source File: MathArrays.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Normalizes an array to make it sum to a specified value.
 * Returns the result of the transformation <pre>
 *    x |-> x * normalizedSum / sum
 * </pre>
 * applied to each non-NaN element x of the input array, where sum is the
 * sum of the non-NaN entries in the input array.</p>
 *
 * <p>Throws IllegalArgumentException if {@code normalizedSum} is infinite
 * or NaN and ArithmeticException if the input array contains any infinite elements
 * or sums to 0.</p>
 *
 * <p>Ignores (i.e., copies unchanged to the output array) NaNs in the input array.</p>
 *
 * @param values Input array to be normalized
 * @param normalizedSum Target sum for the normalized array
 * @return the normalized array.
 * @throws MathArithmeticException if the input array contains infinite
 * elements or sums to zero.
 * @throws MathIllegalArgumentException if the target sum is infinite or {@code NaN}.
 * @since 2.1
 */
public static double[] normalizeArray(double[] values, double normalizedSum) {
    if (Double.isInfinite(normalizedSum)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NORMALIZE_INFINITE);
    }
    if (Double.isNaN(normalizedSum)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NORMALIZE_NAN);
    }
    double sum = 0d;
    final int len = values.length;
    double[] out = new double[len];
    for (int i = 0; i < len; i++) {
        if (Double.isInfinite(values[i])) {
            throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_ARRAY_ELEMENT, values[i], i);
        }
        if (!Double.isNaN(values[i])) {
            sum += values[i];
        }
    }
    if (sum == 0) {
        throw new MathArithmeticException(LocalizedFormats.ARRAY_SUMS_TO_ZERO);
    }
    for (int i = 0; i < len; i++) {
        if (Double.isNaN(values[i])) {
            out[i] = Double.NaN;
        } else {
            out[i] = values[i] * normalizedSum / sum;
        }
    }
    return out;
}
 
Example 9
Source File: AbstractUnivariateStatistic.java    From astor with GNU General Public License v2.0 5 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>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 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 IllegalArgumentException if the indices are invalid or the array
 * is {@code null}.
 * @since 3.0
 */
protected boolean test(final double[] values, final double[] weights, final int begin, final int length, final boolean allowEmpty){

    if (weights == null) {
        throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
    }

    if (weights.length != values.length) {
        throw new DimensionMismatchException(weights.length, values.length);
    }

    boolean containsPositiveWeight = false;
    for (int i = begin; i < begin + length; i++) {
        if (Double.isNaN(weights[i])) {
            throw new MathIllegalArgumentException(LocalizedFormats.NAN_ELEMENT_AT_INDEX, i);
        }
        if (Double.isInfinite(weights[i])) {
            throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_ARRAY_ELEMENT, weights[i], i);
        }
        if (weights[i] < 0) {
            throw new MathIllegalArgumentException(LocalizedFormats.NEGATIVE_ELEMENT_AT_INDEX, i, weights[i]);
        }
        if (!containsPositiveWeight && weights[i] > 0.0) {
            containsPositiveWeight = true;
        }
    }

    if (!containsPositiveWeight) {
        throw new MathIllegalArgumentException(LocalizedFormats.WEIGHT_AT_LEAST_ONE_NON_ZERO);
    }

    return test(values, begin, length, allowEmpty);
}
 
Example 10
Source File: MathArrays.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Normalizes an array to make it sum to a specified value.
 * Returns the result of the transformation <pre>
 *    x |-> x * normalizedSum / sum
 * </pre>
 * applied to each non-NaN element x of the input array, where sum is the
 * sum of the non-NaN entries in the input array.</p>
 *
 * <p>Throws IllegalArgumentException if {@code normalizedSum} is infinite
 * or NaN and ArithmeticException if the input array contains any infinite elements
 * or sums to 0.</p>
 *
 * <p>Ignores (i.e., copies unchanged to the output array) NaNs in the input array.</p>
 *
 * @param values Input array to be normalized
 * @param normalizedSum Target sum for the normalized array
 * @return the normalized array.
 * @throws MathArithmeticException if the input array contains infinite
 * elements or sums to zero.
 * @throws MathIllegalArgumentException if the target sum is infinite or {@code NaN}.
 * @since 2.1
 */
public static double[] normalizeArray(double[] values, double normalizedSum)
    throws MathIllegalArgumentException, MathArithmeticException {
    if (Double.isInfinite(normalizedSum)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NORMALIZE_INFINITE);
    }
    if (Double.isNaN(normalizedSum)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NORMALIZE_NAN);
    }
    double sum = 0d;
    final int len = values.length;
    double[] out = new double[len];
    for (int i = 0; i < len; i++) {
        if (Double.isInfinite(values[i])) {
            throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_ARRAY_ELEMENT, values[i], i);
        }
        if (!Double.isNaN(values[i])) {
            sum += values[i];
        }
    }
    if (sum == 0) {
        throw new MathArithmeticException(LocalizedFormats.ARRAY_SUMS_TO_ZERO);
    }
    for (int i = 0; i < len; i++) {
        if (Double.isNaN(values[i])) {
            out[i] = Double.NaN;
        } else {
            out[i] = values[i] * normalizedSum / sum;
        }
    }
    return out;
}
 
Example 11
Source File: Math_3_MathArrays_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Normalizes an array to make it sum to a specified value.
 * Returns the result of the transformation <pre>
 *    x |-> x * normalizedSum / sum
 * </pre>
 * applied to each non-NaN element x of the input array, where sum is the
 * sum of the non-NaN entries in the input array.</p>
 *
 * <p>Throws IllegalArgumentException if {@code normalizedSum} is infinite
 * or NaN and ArithmeticException if the input array contains any infinite elements
 * or sums to 0.</p>
 *
 * <p>Ignores (i.e., copies unchanged to the output array) NaNs in the input array.</p>
 *
 * @param values Input array to be normalized
 * @param normalizedSum Target sum for the normalized array
 * @return the normalized array.
 * @throws MathArithmeticException if the input array contains infinite
 * elements or sums to zero.
 * @throws MathIllegalArgumentException if the target sum is infinite or {@code NaN}.
 * @since 2.1
 */
public static double[] normalizeArray(double[] values, double normalizedSum)
    throws MathIllegalArgumentException, MathArithmeticException {
    if (Double.isInfinite(normalizedSum)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NORMALIZE_INFINITE);
    }
    if (Double.isNaN(normalizedSum)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NORMALIZE_NAN);
    }
    double sum = 0d;
    final int len = values.length;
    double[] out = new double[len];
    for (int i = 0; i < len; i++) {
        if (Double.isInfinite(values[i])) {
            throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_ARRAY_ELEMENT, values[i], i);
        }
        if (!Double.isNaN(values[i])) {
            sum += values[i];
        }
    }
    if (sum == 0) {
        throw new MathArithmeticException(LocalizedFormats.ARRAY_SUMS_TO_ZERO);
    }
    for (int i = 0; i < len; i++) {
        if (Double.isNaN(values[i])) {
            out[i] = Double.NaN;
        } else {
            out[i] = values[i] * normalizedSum / sum;
        }
    }
    return out;
}
 
Example 12
Source File: MathArrays.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Normalizes an array to make it sum to a specified value.
 * Returns the result of the transformation <pre>
 *    x |-> x * normalizedSum / sum
 * </pre>
 * applied to each non-NaN element x of the input array, where sum is the
 * sum of the non-NaN entries in the input array.</p>
 *
 * <p>Throws IllegalArgumentException if {@code normalizedSum} is infinite
 * or NaN and ArithmeticException if the input array contains any infinite elements
 * or sums to 0.</p>
 *
 * <p>Ignores (i.e., copies unchanged to the output array) NaNs in the input array.</p>
 *
 * @param values Input array to be normalized
 * @param normalizedSum Target sum for the normalized array
 * @return the normalized array.
 * @throws MathArithmeticException if the input array contains infinite
 * elements or sums to zero.
 * @throws MathIllegalArgumentException if the target sum is infinite or {@code NaN}.
 * @since 2.1
 */
public static double[] normalizeArray(double[] values, double normalizedSum) {
    if (Double.isInfinite(normalizedSum)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NORMALIZE_INFINITE);
    }
    if (Double.isNaN(normalizedSum)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NORMALIZE_NAN);
    }
    double sum = 0d;
    final int len = values.length;
    double[] out = new double[len];
    for (int i = 0; i < len; i++) {
        if (Double.isInfinite(values[i])) {
            throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_ARRAY_ELEMENT, values[i], i);
        }
        if (!Double.isNaN(values[i])) {
            sum += values[i];
        }
    }
    if (sum == 0) {
        throw new MathArithmeticException(LocalizedFormats.ARRAY_SUMS_TO_ZERO);
    }
    for (int i = 0; i < len; i++) {
        if (Double.isNaN(values[i])) {
            out[i] = Double.NaN;
        } else {
            out[i] = values[i] * normalizedSum / sum;
        }
    }
    return out;
}
 
Example 13
Source File: AbstractUnivariateStatistic.java    From astor with GNU General Public License v2.0 5 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>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 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 IllegalArgumentException if the indices are invalid or the array
 * is {@code null}.
 * @since 3.0
 */
protected boolean test(final double[] values, final double[] weights, final int begin, final int length, final boolean allowEmpty){

    if (weights == null) {
        throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
    }

    if (weights.length != values.length) {
        throw new DimensionMismatchException(weights.length, values.length);
    }

    boolean containsPositiveWeight = false;
    for (int i = begin; i < begin + length; i++) {
        if (Double.isNaN(weights[i])) {
            throw new MathIllegalArgumentException(LocalizedFormats.NAN_ELEMENT_AT_INDEX, i);
        }
        if (Double.isInfinite(weights[i])) {
            throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_ARRAY_ELEMENT, weights[i], i);
        }
        if (weights[i] < 0) {
            throw new MathIllegalArgumentException(LocalizedFormats.NEGATIVE_ELEMENT_AT_INDEX, i, weights[i]);
        }
        if (!containsPositiveWeight && weights[i] > 0.0) {
            containsPositiveWeight = true;
        }
    }

    if (!containsPositiveWeight) {
        throw new MathIllegalArgumentException(LocalizedFormats.WEIGHT_AT_LEAST_ONE_NON_ZERO);
    }

    return test(values, begin, length, allowEmpty);
}
 
Example 14
Source File: MathArrays.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Normalizes an array to make it sum to a specified value.
 * Returns the result of the transformation <pre>
 *    x |-> x * normalizedSum / sum
 * </pre>
 * applied to each non-NaN element x of the input array, where sum is the
 * sum of the non-NaN entries in the input array.</p>
 *
 * <p>Throws IllegalArgumentException if {@code normalizedSum} is infinite
 * or NaN and ArithmeticException if the input array contains any infinite elements
 * or sums to 0.</p>
 *
 * <p>Ignores (i.e., copies unchanged to the output array) NaNs in the input array.</p>
 *
 * @param values Input array to be normalized
 * @param normalizedSum Target sum for the normalized array
 * @return the normalized array.
 * @throws MathArithmeticException if the input array contains infinite
 * elements or sums to zero.
 * @throws MathIllegalArgumentException if the target sum is infinite or {@code NaN}.
 * @since 2.1
 */
public static double[] normalizeArray(double[] values, double normalizedSum)
    throws MathIllegalArgumentException, MathArithmeticException {
    if (Double.isInfinite(normalizedSum)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NORMALIZE_INFINITE);
    }
    if (Double.isNaN(normalizedSum)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NORMALIZE_NAN);
    }
    double sum = 0d;
    final int len = values.length;
    double[] out = new double[len];
    for (int i = 0; i < len; i++) {
        if (Double.isInfinite(values[i])) {
            throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_ARRAY_ELEMENT, values[i], i);
        }
        if (!Double.isNaN(values[i])) {
            sum += values[i];
        }
    }
    if (sum == 0) {
        throw new MathArithmeticException(LocalizedFormats.ARRAY_SUMS_TO_ZERO);
    }
    for (int i = 0; i < len; i++) {
        if (Double.isNaN(values[i])) {
            out[i] = Double.NaN;
        } else {
            out[i] = values[i] * normalizedSum / sum;
        }
    }
    return out;
}
 
Example 15
Source File: AbstractUnivariateStatistic.java    From astor with GNU General Public License v2.0 5 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 {

    if (weights == null || values == null) {
        throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
    }

    if (weights.length != values.length) {
        throw new DimensionMismatchException(weights.length, values.length);
    }

    boolean containsPositiveWeight = false;
    for (int i = begin; i < begin + length; i++) {
        if (Double.isNaN(weights[i])) {
            throw new MathIllegalArgumentException(LocalizedFormats.NAN_ELEMENT_AT_INDEX, i);
        }
        if (Double.isInfinite(weights[i])) {
            throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_ARRAY_ELEMENT, weights[i], i);
        }
        if (weights[i] < 0) {
            throw new MathIllegalArgumentException(LocalizedFormats.NEGATIVE_ELEMENT_AT_INDEX, i, weights[i]);
        }
        if (!containsPositiveWeight && weights[i] > 0.0) {
            containsPositiveWeight = true;
        }
    }

    if (!containsPositiveWeight) {
        throw new MathIllegalArgumentException(LocalizedFormats.WEIGHT_AT_LEAST_ONE_NON_ZERO);
    }

    return test(values, begin, length, allowEmpty);
}
 
Example 16
Source File: MathArrays.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This method is used
 * 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.3
 */
public static boolean verifyValues(final double[] values, final double[] weights,
        final int begin, final int length, final boolean allowEmpty) throws MathIllegalArgumentException {

    if (weights == null || values == null) {
        throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
    }

    if (weights.length != values.length) {
        throw new DimensionMismatchException(weights.length, values.length);
    }

    boolean containsPositiveWeight = false;
    for (int i = begin; i < begin + length; i++) {
        final double weight = weights[i];
        if (Double.isNaN(weight)) {
            throw new MathIllegalArgumentException(LocalizedFormats.NAN_ELEMENT_AT_INDEX, Integer.valueOf(i));
        }
        if (Double.isInfinite(weight)) {
            throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_ARRAY_ELEMENT, Double.valueOf(weight), Integer.valueOf(i));
        }
        if (weight < 0) {
            throw new MathIllegalArgumentException(LocalizedFormats.NEGATIVE_ELEMENT_AT_INDEX, Integer.valueOf(i), Double.valueOf(weight));
        }
        if (!containsPositiveWeight && weight > 0.0) {
            containsPositiveWeight = true;
        }
    }

    if (!containsPositiveWeight) {
        throw new MathIllegalArgumentException(LocalizedFormats.WEIGHT_AT_LEAST_ONE_NON_ZERO);
    }

    return verifyValues(values, begin, length, allowEmpty);
}
 
Example 17
Source File: MathArrays.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Normalizes an array to make it sum to a specified value.
 * Returns the result of the transformation
 * <pre>
 *    x |-> x * normalizedSum / sum
 * </pre>
 * applied to each non-NaN element x of the input array, where sum is the
 * sum of the non-NaN entries in the input array.
 * <p>
 * Throws IllegalArgumentException if {@code normalizedSum} is infinite
 * or NaN and ArithmeticException if the input array contains any infinite elements
 * or sums to 0.
 * <p>
 * Ignores (i.e., copies unchanged to the output array) NaNs in the input array.
 *
 * @param values Input array to be normalized
 * @param normalizedSum Target sum for the normalized array
 * @return the normalized array.
 * @throws MathArithmeticException if the input array contains infinite
 * elements or sums to zero.
 * @throws MathIllegalArgumentException if the target sum is infinite or {@code NaN}.
 * @since 2.1
 */
public static double[] normalizeArray(double[] values, double normalizedSum)
    throws MathIllegalArgumentException, MathArithmeticException {
    if (Double.isInfinite(normalizedSum)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NORMALIZE_INFINITE);
    }
    if (Double.isNaN(normalizedSum)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NORMALIZE_NAN);
    }
    double sum = 0d;
    final int len = values.length;
    double[] out = new double[len];
    for (int i = 0; i < len; i++) {
        if (Double.isInfinite(values[i])) {
            throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_ARRAY_ELEMENT, values[i], i);
        }
        if (!Double.isNaN(values[i])) {
            sum += values[i];
        }
    }
    if (sum == 0) {
        throw new MathArithmeticException(LocalizedFormats.ARRAY_SUMS_TO_ZERO);
    }
    for (int i = 0; i < len; i++) {
        if (Double.isNaN(values[i])) {
            out[i] = Double.NaN;
        } else {
            out[i] = values[i] * normalizedSum / sum;
        }
    }
    return out;
}
 
Example 18
Source File: MathArrays_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Normalizes an array to make it sum to a specified value.
 * Returns the result of the transformation <pre>
 *    x |-> x * normalizedSum / sum
 * </pre>
 * applied to each non-NaN element x of the input array, where sum is the
 * sum of the non-NaN entries in the input array.</p>
 *
 * <p>Throws IllegalArgumentException if {@code normalizedSum} is infinite
 * or NaN and ArithmeticException if the input array contains any infinite elements
 * or sums to 0.</p>
 *
 * <p>Ignores (i.e., copies unchanged to the output array) NaNs in the input array.</p>
 *
 * @param values Input array to be normalized
 * @param normalizedSum Target sum for the normalized array
 * @return the normalized array.
 * @throws MathArithmeticException if the input array contains infinite
 * elements or sums to zero.
 * @throws MathIllegalArgumentException if the target sum is infinite or {@code NaN}.
 * @since 2.1
 */
public static double[] normalizeArray(double[] values, double normalizedSum)
    throws MathIllegalArgumentException, MathArithmeticException {
    if (Double.isInfinite(normalizedSum)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NORMALIZE_INFINITE);
    }
    if (Double.isNaN(normalizedSum)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NORMALIZE_NAN);
    }
    double sum = 0d;
    final int len = values.length;
    double[] out = new double[len];
    for (int i = 0; i < len; i++) {
        if (Double.isInfinite(values[i])) {
            throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_ARRAY_ELEMENT, values[i], i);
        }
        if (!Double.isNaN(values[i])) {
            sum += values[i];
        }
    }
    if (sum == 0) {
        throw new MathArithmeticException(LocalizedFormats.ARRAY_SUMS_TO_ZERO);
    }
    for (int i = 0; i < len; i++) {
        if (Double.isNaN(values[i])) {
            out[i] = Double.NaN;
        } else {
            out[i] = values[i] * normalizedSum / sum;
        }
    }
    return out;
}
 
Example 19
Source File: MathArrays_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Normalizes an array to make it sum to a specified value.
 * Returns the result of the transformation <pre>
 *    x |-> x * normalizedSum / sum
 * </pre>
 * applied to each non-NaN element x of the input array, where sum is the
 * sum of the non-NaN entries in the input array.</p>
 *
 * <p>Throws IllegalArgumentException if {@code normalizedSum} is infinite
 * or NaN and ArithmeticException if the input array contains any infinite elements
 * or sums to 0.</p>
 *
 * <p>Ignores (i.e., copies unchanged to the output array) NaNs in the input array.</p>
 *
 * @param values Input array to be normalized
 * @param normalizedSum Target sum for the normalized array
 * @return the normalized array.
 * @throws MathArithmeticException if the input array contains infinite
 * elements or sums to zero.
 * @throws MathIllegalArgumentException if the target sum is infinite or {@code NaN}.
 * @since 2.1
 */
public static double[] normalizeArray(double[] values, double normalizedSum)
    throws MathIllegalArgumentException, MathArithmeticException {
    if (Double.isInfinite(normalizedSum)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NORMALIZE_INFINITE);
    }
    if (Double.isNaN(normalizedSum)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NORMALIZE_NAN);
    }
    double sum = 0d;
    final int len = values.length;
    double[] out = new double[len];
    for (int i = 0; i < len; i++) {
        if (Double.isInfinite(values[i])) {
            throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_ARRAY_ELEMENT, values[i], i);
        }
        if (!Double.isNaN(values[i])) {
            sum += values[i];
        }
    }
    if (sum == 0) {
        throw new MathArithmeticException(LocalizedFormats.ARRAY_SUMS_TO_ZERO);
    }
    for (int i = 0; i < len; i++) {
        if (Double.isNaN(values[i])) {
            out[i] = Double.NaN;
        } else {
            out[i] = values[i] * normalizedSum / sum;
        }
    }
    return out;
}
 
Example 20
Source File: Math_3_MathArrays_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Normalizes an array to make it sum to a specified value.
 * Returns the result of the transformation <pre>
 *    x |-> x * normalizedSum / sum
 * </pre>
 * applied to each non-NaN element x of the input array, where sum is the
 * sum of the non-NaN entries in the input array.</p>
 *
 * <p>Throws IllegalArgumentException if {@code normalizedSum} is infinite
 * or NaN and ArithmeticException if the input array contains any infinite elements
 * or sums to 0.</p>
 *
 * <p>Ignores (i.e., copies unchanged to the output array) NaNs in the input array.</p>
 *
 * @param values Input array to be normalized
 * @param normalizedSum Target sum for the normalized array
 * @return the normalized array.
 * @throws MathArithmeticException if the input array contains infinite
 * elements or sums to zero.
 * @throws MathIllegalArgumentException if the target sum is infinite or {@code NaN}.
 * @since 2.1
 */
public static double[] normalizeArray(double[] values, double normalizedSum)
    throws MathIllegalArgumentException, MathArithmeticException {
    if (Double.isInfinite(normalizedSum)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NORMALIZE_INFINITE);
    }
    if (Double.isNaN(normalizedSum)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NORMALIZE_NAN);
    }
    double sum = 0d;
    final int len = values.length;
    double[] out = new double[len];
    for (int i = 0; i < len; i++) {
        if (Double.isInfinite(values[i])) {
            throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_ARRAY_ELEMENT, values[i], i);
        }
        if (!Double.isNaN(values[i])) {
            sum += values[i];
        }
    }
    if (sum == 0) {
        throw new MathArithmeticException(LocalizedFormats.ARRAY_SUMS_TO_ZERO);
    }
    for (int i = 0; i < len; i++) {
        if (Double.isNaN(values[i])) {
            out[i] = Double.NaN;
        } else {
            out[i] = values[i] * normalizedSum / sum;
        }
    }
    return out;
}