alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Commons Math example source code file (AbstractUnivariateStatistic.java)

This example Commons Math source code file (AbstractUnivariateStatistic.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Commons Math tags/keywords

abstractunivariatestatistic, abstractunivariatestatistic, different, different, infinite, nan, nan, univariatestatistic, univariatestatistic

The Commons Math AbstractUnivariateStatistic.java source code

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.commons.math.stat.descriptive;

import org.apache.commons.math.MathRuntimeException;

/**
 * Abstract base class for all implementations of the
 * {@link UnivariateStatistic} interface.
 * <p>
 * Provides a default implementation of <code>evaluate(double[]),
 * delegating to <code>evaluate(double[], int, int) in the natural way.
 * </p>
 * <p>
 * Also includes a <code>test method that performs generic parameter
 * validation for the <code>evaluate methods.

* * @version $Revision: 894705 $ $Date: 2009-12-30 15:24:54 -0500 (Wed, 30 Dec 2009) $ */ public abstract class AbstractUnivariateStatistic implements UnivariateStatistic { /** * {@inheritDoc} */ public double evaluate(final double[] values) { test(values, 0, 0); return evaluate(values, 0, values.length); } /** * {@inheritDoc} */ public abstract double evaluate(final double[] values, final int begin, final int length); /** * {@inheritDoc} */ public abstract UnivariateStatistic copy(); /** * This method is used by <code>evaluate(double[], int, int) methods * to verify that the input parameters designate a subarray of positive length. * <p> * <ul> * <li>returns true iff the parameters designate a subarray of * positive length</li> * <li>throws IllegalArgumentException if the array is null or * or the indices are invalid</li> * <li>returns false if the array is non-null, but * <code>length is 0. * </ul>

* * @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 IllegalArgumentException if the indices are invalid or the array is null */ protected boolean test( final double[] values, final int begin, final int length) { if (values == null) { throw MathRuntimeException.createIllegalArgumentException("input values array is null"); } if (begin < 0) { throw MathRuntimeException.createIllegalArgumentException( "start position cannot be negative ({0})", begin); } if (length < 0) { throw MathRuntimeException.createIllegalArgumentException( "length cannot be negative ({0})", length); } if (begin + length > values.length) { throw MathRuntimeException.createIllegalArgumentException( "subarray ends after array end"); } if (length == 0) { return false; } return true; } /** * This method is used by <code>evaluate(double[], double[], int, int) 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 true iff the parameters designate a subarray of * positive length and the weights array contains legitimate values.</li> * <li>throws IllegalArgumentException if any of the following are true: * <ul>
  • the values array is null
  • * <li>the weights array is null * <li>the weights array does not have the same length as the values array * <li>the weights array contains one or more infinite values * <li>the weights array contains one or more NaN values * <li>the weights array contains negative values * <li>the start and length arguments do not determine a valid array * </li> * <li>returns false if the array is non-null, but * <code>length is 0. * </ul>

    * * @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 IllegalArgumentException 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) { if (weights == null) { throw MathRuntimeException.createIllegalArgumentException("input weights array is null"); } if (weights.length != values.length) { throw MathRuntimeException.createIllegalArgumentException( "Different number of weights and values"); } boolean containsPositiveWeight = false; for (int i = begin; i < begin + length; i++) { if (Double.isNaN(weights[i])) { throw MathRuntimeException.createIllegalArgumentException( "NaN weight at index {0}", i); } if (Double.isInfinite(weights[i])) { throw MathRuntimeException.createIllegalArgumentException( "Infinite weight at index {0}", i); } if (weights[i] < 0) { throw MathRuntimeException.createIllegalArgumentException( "negative weight {0} at index {1} ", weights[i], i); } if (!containsPositiveWeight && weights[i] > 0.0) { containsPositiveWeight = true; } } if (!containsPositiveWeight) { throw MathRuntimeException.createIllegalArgumentException( "weight array must contain at least one non-zero value"); } return test(values, begin, length); } }

    Other Commons Math examples (source code examples)

    Here is a short list of links related to this Commons Math AbstractUnivariateStatistic.java source code file:

    ... this post is sponsored by my books ...

    #1 New Release!

    FP Best Seller

     

    new blog posts

     

    Copyright 1998-2021 Alvin Alexander, alvinalexander.com
    All Rights Reserved.

    A percentage of advertising revenue from
    pages under the /java/jwarehouse URI on this website is
    paid back to open source projects.