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.summary;
18dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
19dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport java.io.Serializable;
20dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
21dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.stat.descriptive.AbstractStorelessUnivariateStatistic;
22dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
23dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
24dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/**
25dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  * Returns the sum of the available values.
26dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>
27dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * If there are no values in the dataset, or any of the values are
28dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <code>NaN</code>, then <code>NaN</code> is returned.</p>
29dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>
30dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <strong>Note that this implementation is not synchronized.</strong> If
31dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * multiple threads access an instance of this class concurrently, and at least
32dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * one of the threads invokes the <code>increment()</code> or
33dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <code>clear()</code> method, it must be synchronized externally.</p>
34dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
35dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @version $Revision: 1006299 $ $Date: 2010-10-10 16:47:17 +0200 (dim. 10 oct. 2010) $
36dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */
37dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpublic class Sum extends AbstractStorelessUnivariateStatistic implements Serializable {
38dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
39dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** Serializable version identifier */
40dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private static final long serialVersionUID = -8231831954703408316L;
41dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
42dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** */
43dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private long n;
44dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
45dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
46dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * The currently running sum.
47dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
48dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private double value;
49dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
50dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
51dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Create a Sum instance
52dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
53dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public Sum() {
54dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        n = 0;
55dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        value = Double.NaN;
56dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
57dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
58dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
59dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Copy constructor, creates a new {@code Sum} identical
60dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * to the {@code original}
61dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
62dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param original the {@code Sum} instance to copy
63dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
64dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public Sum(Sum original) {
65dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        copy(original, this);
66dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
67dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
68dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
69dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * {@inheritDoc}
70dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
71dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    @Override
72dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public void increment(final double d) {
73dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (n == 0) {
74dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            value = d;
75dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        } else {
76dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            value += d;
77dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
78dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        n++;
79dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
80dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
81dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
82dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * {@inheritDoc}
83dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
84dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    @Override
85dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public double getResult() {
86dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return value;
87dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
88dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
89dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
90dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * {@inheritDoc}
91dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
92dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public long getN() {
93dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return n;
94dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
95dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
96dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
97dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * {@inheritDoc}
98dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
99dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    @Override
100dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public void clear() {
101dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        value = Double.NaN;
102dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        n = 0;
103dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
104dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
105dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
106dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * The sum of the entries in the specified portion of
107dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * the input array, or <code>Double.NaN</code> if the designated subarray
108dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * is empty.
109dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
110dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Throws <code>IllegalArgumentException</code> if the array is null.</p>
111dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
112dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param values the input array
113dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param begin index of the first array element to include
114dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param length the number of elements to include
115dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the sum of the values or Double.NaN if length = 0
116dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws IllegalArgumentException if the array is null or the array index
117dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *  parameters are not valid
118dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
119dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    @Override
120dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public double evaluate(final double[] values, final int begin, final int length) {
121dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        double sum = Double.NaN;
122dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (test(values, begin, length)) {
123dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            sum = 0.0;
124dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            for (int i = begin; i < begin + length; i++) {
125dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                sum += values[i];
126dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
127dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
128dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return sum;
129dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
130dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
131dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
132dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * The weighted sum of the entries in the specified portion of
133dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * the input array, or <code>Double.NaN</code> if the designated subarray
134dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * is empty.
135dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
136dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Throws <code>IllegalArgumentException</code> if any of the following are true:
137dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <ul><li>the values array is null</li>
138dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *     <li>the weights array is null</li>
139dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *     <li>the weights array does not have the same length as the values array</li>
140dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *     <li>the weights array contains one or more infinite values</li>
141dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *     <li>the weights array contains one or more NaN values</li>
142dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *     <li>the weights array contains negative values</li>
143dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *     <li>the start and length arguments do not determine a valid array</li>
144dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * </ul></p>
145dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
146dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Uses the formula, <pre>
147dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *    weighted sum = &Sigma;(values[i] * weights[i])
148dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * </pre></p>
149dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
150dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param values the input array
151dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param weights the weights array
152dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param begin index of the first array element to include
153dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param length the number of elements to include
154dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the sum of the values or Double.NaN if length = 0
155dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws IllegalArgumentException if the parameters are not valid
156dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @since 2.1
157dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
158dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public double evaluate(final double[] values, final double[] weights,
159dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                           final int begin, final int length) {
160dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        double sum = Double.NaN;
161dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (test(values, weights, begin, length)) {
162dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            sum = 0.0;
163dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            for (int i = begin; i < begin + length; i++) {
164dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                sum += values[i] * weights[i];
165dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
166dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
167dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return sum;
168dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
169dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
170dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
171dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * The weighted sum of the entries in the the input array.
172dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
173dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Throws <code>IllegalArgumentException</code> if any of the following are true:
174dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <ul><li>the values array is null</li>
175dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *     <li>the weights array is null</li>
176dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *     <li>the weights array does not have the same length as the values array</li>
177dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *     <li>the weights array contains one or more infinite values</li>
178dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *     <li>the weights array contains one or more NaN values</li>
179dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *     <li>the weights array contains negative values</li>
180dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * </ul></p>
181dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
182dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Uses the formula, <pre>
183dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *    weighted sum = &Sigma;(values[i] * weights[i])
184dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * </pre></p>
185dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
186dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param values the input array
187dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param weights the weights array
188dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the sum of the values or Double.NaN if length = 0
189dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws IllegalArgumentException if the parameters are not valid
190dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @since 2.1
191dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
192dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public double evaluate(final double[] values, final double[] weights) {
193dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return evaluate(values, weights, 0, values.length);
194dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
195dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
196dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
197dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * {@inheritDoc}
198dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
199dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    @Override
200dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public Sum copy() {
201dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        Sum result = new Sum();
202dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        copy(this, result);
203dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return result;
204dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
205dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
206dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
207dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Copies source to dest.
208dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>Neither source nor dest can be null.</p>
209dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
210dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param source Sum to copy
211dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param dest Sum to copy to
212dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws NullPointerException if either source or dest is null
213dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
214dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static void copy(Sum source, Sum dest) {
215dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        dest.setData(source.getDataRef());
216dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        dest.n = source.n;
217dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        dest.value = source.value;
218dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
219dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
220dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond}
221