001: /*
002: * Copyright 2003-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.math.stat.descriptive.summary;
017:
018: import java.io.Serializable;
019:
020: import org.apache.commons.math.stat.descriptive.AbstractStorelessUnivariateStatistic;
021:
022: /**
023: * Returns the sum of the available values.
024: * <p>
025: * If there are no values in the dataset, or any of the values are
026: * <code>NaN</code>, then <code>NaN</code> is returned.
027: * <p>
028: * <strong>Note that this implementation is not synchronized.</strong> If
029: * multiple threads access an instance of this class concurrently, and at least
030: * one of the threads invokes the <code>increment()</code> or
031: * <code>clear()</code> method, it must be synchronized externally.
032: *
033: * @version $Revision: 348519 $ $Date: 2005-11-23 12:12:18 -0700 (Wed, 23 Nov 2005) $
034: */
035: public class Sum extends AbstractStorelessUnivariateStatistic implements
036: Serializable {
037:
038: /** Serializable version identifier */
039: private static final long serialVersionUID = -8231831954703408316L;
040:
041: /** */
042: private long n;
043:
044: /**
045: * The currently running sum.
046: */
047: private double value;
048:
049: /**
050: * Create a Sum instance
051: */
052: public Sum() {
053: n = 0;
054: value = Double.NaN;
055: }
056:
057: /**
058: * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#increment(double)
059: */
060: public void increment(final double d) {
061: if (n == 0) {
062: value = d;
063: } else {
064: value += d;
065: }
066: n++;
067: }
068:
069: /**
070: * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#getResult()
071: */
072: public double getResult() {
073: return value;
074: }
075:
076: /**
077: * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#getN()
078: */
079: public long getN() {
080: return n;
081: }
082:
083: /**
084: * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#clear()
085: */
086: public void clear() {
087: value = Double.NaN;
088: n = 0;
089: }
090:
091: /**
092: * The sum of the entries in the specified portion of
093: * the input array, or <code>Double.NaN</code> if the designated subarray
094: * is empty.
095: * <p>
096: * Throws <code>IllegalArgumentException</code> if the array is null.
097: *
098: * @param values the input array
099: * @param begin index of the first array element to include
100: * @param length the number of elements to include
101: * @return the sum of the values or Double.NaN if length = 0
102: * @throws IllegalArgumentException if the array is null or the array index
103: * parameters are not valid
104: */
105: public double evaluate(final double[] values, final int begin,
106: final int length) {
107: double sum = Double.NaN;
108: if (test(values, begin, length)) {
109: sum = 0.0;
110: for (int i = begin; i < begin + length; i++) {
111: sum += values[i];
112: }
113: }
114: return sum;
115: }
116:
117: }
|