1dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/* 2dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Licensed to the Apache Software Foundation (ASF) under one or more 3dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * contributor license agreements. See the NOTICE file distributed with 4dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * this work for additional information regarding copyright ownership. 5dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * The ASF licenses this file to You under the Apache License, Version 2.0 6dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * (the "License"); you may not use this file except in compliance with 7dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * the License. You may obtain a copy of the License at 8dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 9dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * http://www.apache.org/licenses/LICENSE-2.0 10dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 11dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Unless required by applicable law or agreed to in writing, software 12dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * distributed under the License is distributed on an "AS IS" BASIS, 13dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * See the License for the specific language governing permissions and 15dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * limitations under the License. 16dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 17dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpackage org.apache.commons.math.stat.descriptive; 18dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 19dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.exception.util.LocalizedFormats; 20dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.exception.NullArgumentException; 21dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.util.MathUtils; 22dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 23dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/** 24dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 25dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Abstract implementation of the {@link StorelessUnivariateStatistic} interface. 26dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p> 27dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Provides default <code>evaluate()</code> and <code>incrementAll(double[])<code> 28dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * implementations.</p> 29dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p> 30dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <strong>Note that these implementations are not synchronized.</strong></p> 31dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 32dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @version $Revision: 983921 $ $Date: 2010-08-10 12:46:06 +0200 (mar. 10 août 2010) $ 33dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 34dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpublic abstract class AbstractStorelessUnivariateStatistic 35dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond extends AbstractUnivariateStatistic 36dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond implements StorelessUnivariateStatistic { 37dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 38dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 39dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * This default implementation calls {@link #clear}, then invokes 40dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * {@link #increment} in a loop over the the input array, and then uses 41dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * {@link #getResult} to compute the return value. 42dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p> 43dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Note that this implementation changes the internal state of the 44dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * statistic. Its side effects are the same as invoking {@link #clear} and 45dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * then {@link #incrementAll(double[])}.</p> 46dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p> 47dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Implementations may override this method with a more efficient and 48dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * possibly more accurate implementation that works directly with the 49dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * input array.</p> 50dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p> 51dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * If the array is null, an IllegalArgumentException is thrown.</p> 52dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param values input array 53dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return the value of the statistic applied to the input array 54dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @see org.apache.commons.math.stat.descriptive.UnivariateStatistic#evaluate(double[]) 55dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 56dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond @Override 57dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public double evaluate(final double[] values) { 58dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if (values == null) { 59dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY); 60dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 61dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return evaluate(values, 0, values.length); 62dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 63dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 64dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 65dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * This default implementation calls {@link #clear}, then invokes 66dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * {@link #increment} in a loop over the specified portion of the input 67dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * array, and then uses {@link #getResult} to compute the return value. 68dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p> 69dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Note that this implementation changes the internal state of the 70dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * statistic. Its side effects are the same as invoking {@link #clear} and 71dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * then {@link #incrementAll(double[], int, int)}.</p> 72dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p> 73dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Implementations may override this method with a more efficient and 74dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * possibly more accurate implementation that works directly with the 75dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * input array.</p> 76dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p> 77dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * If the array is null or the index parameters are not valid, an 78dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * IllegalArgumentException is thrown.</p> 79dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param values the input array 80dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param begin the index of the first element to include 81dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param length the number of elements to include 82dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return the value of the statistic applied to the included array entries 83dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @see org.apache.commons.math.stat.descriptive.UnivariateStatistic#evaluate(double[], int, int) 84dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 85dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond @Override 86dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public double evaluate(final double[] values, final int begin, final int length) { 87dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if (test(values, begin, length)) { 88dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond clear(); 89dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond incrementAll(values, begin, length); 90dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 91dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return getResult(); 92dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 93dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 94dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 95dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * {@inheritDoc} 96dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 97dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond @Override 98dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public abstract StorelessUnivariateStatistic copy(); 99dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 100dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 101dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * {@inheritDoc} 102dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 103dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public abstract void clear(); 104dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 105dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 106dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * {@inheritDoc} 107dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 108dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public abstract double getResult(); 109dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 110dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 111dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * {@inheritDoc} 112dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 113dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public abstract void increment(final double d); 114dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 115dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 116dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * This default implementation just calls {@link #increment} in a loop over 117dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * the input array. 118dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p> 119dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Throws IllegalArgumentException if the input values array is null.</p> 120dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 121dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param values values to add 122dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws IllegalArgumentException if values is null 123dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#incrementAll(double[]) 124dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 125dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public void incrementAll(double[] values) { 126dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if (values == null) { 127dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY); 128dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 129dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond incrementAll(values, 0, values.length); 130dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 131dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 132dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 133dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * This default implementation just calls {@link #increment} in a loop over 134dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * the specified portion of the input array. 135dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p> 136dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Throws IllegalArgumentException if the input values array is null.</p> 137dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 138dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param values array holding values to add 139dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param begin index of the first array element to add 140dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param length number of array elements to add 141dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws IllegalArgumentException if values is null 142dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#incrementAll(double[], int, int) 143dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 144dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public void incrementAll(double[] values, int begin, int length) { 145dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if (test(values, begin, length)) { 146dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond int k = begin + length; 147dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int i = begin; i < k; i++) { 148dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond increment(values[i]); 149dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 150dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 151dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 152dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 153dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 154dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Returns true iff <code>object</code> is an 155dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <code>AbstractStorelessUnivariateStatistic</code> returning the same 156dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * values as this for <code>getResult()</code> and <code>getN()</code> 157dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param object object to test equality against. 158dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return true if object returns the same value as this 159dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 160dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond @Override 161dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public boolean equals(Object object) { 162dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if (object == this ) { 163dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return true; 164dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 165dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if (object instanceof AbstractStorelessUnivariateStatistic == false) { 166dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return false; 167dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 168dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond AbstractStorelessUnivariateStatistic stat = (AbstractStorelessUnivariateStatistic) object; 169dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return MathUtils.equalsIncludingNaN(stat.getResult(), this.getResult()) && 170dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond MathUtils.equalsIncludingNaN(stat.getN(), this.getN()); 171dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 172dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 173dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 174dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Returns hash code based on getResult() and getN() 175dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 176dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return hash code 177dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 178dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond @Override 179dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public int hashCode() { 180dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return 31* (31 + MathUtils.hash(getResult())) + MathUtils.hash(getN()); 181dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 182dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 183dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond} 184