View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.math3.stat.descriptive.summary;
18  
19  import java.io.Serializable;
20  
21  import org.apache.commons.math3.exception.MathIllegalArgumentException;
22  import org.apache.commons.math3.exception.NullArgumentException;
23  import org.apache.commons.math3.stat.descriptive.AbstractStorelessUnivariateStatistic;
24  import org.apache.commons.math3.util.MathUtils;
25  
26  
27  /**
28    * Returns the sum of the available values.
29   * <p>
30   * If there are no values in the dataset, then 0 is returned.
31   * If any of the values are
32   * <code>NaN</code>, then <code>NaN</code> is returned.</p>
33   * <p>
34   * <strong>Note that this implementation is not synchronized.</strong> If
35   * multiple threads access an instance of this class concurrently, and at least
36   * one of the threads invokes the <code>increment()</code> or
37   * <code>clear()</code> method, it must be synchronized externally.</p>
38   *
39   */
40  public class Sum extends AbstractStorelessUnivariateStatistic implements Serializable {
41  
42      /** Serializable version identifier */
43      private static final long serialVersionUID = -8231831954703408316L;
44  
45      /** */
46      private long n;
47  
48      /**
49       * The currently running sum.
50       */
51      private double value;
52  
53      /**
54       * Create a Sum instance
55       */
56      public Sum() {
57          n = 0;
58          value = 0;
59      }
60  
61      /**
62       * Copy constructor, creates a new {@code Sum} identical
63       * to the {@code original}
64       *
65       * @param original the {@code Sum} instance to copy
66       * @throws NullArgumentException if original is null
67       */
68      public Sum(Sum original) throws NullArgumentException {
69          copy(original, this);
70      }
71  
72      /**
73       * {@inheritDoc}
74       */
75      @Override
76      public void increment(final double d) {
77          value += d;
78          n++;
79      }
80  
81      /**
82       * {@inheritDoc}
83       */
84      @Override
85      public double getResult() {
86          return value;
87      }
88  
89      /**
90       * {@inheritDoc}
91       */
92      public long getN() {
93          return n;
94      }
95  
96      /**
97       * {@inheritDoc}
98       */
99      @Override
100     public void clear() {
101         value = 0;
102         n = 0;
103     }
104 
105     /**
106      * The sum of the entries in the specified portion of
107      * the input array, or 0 if the designated subarray
108      * is empty.
109      * <p>
110      * Throws <code>MathIllegalArgumentException</code> if the array is null.</p>
111      *
112      * @param values the input array
113      * @param begin index of the first array element to include
114      * @param length the number of elements to include
115      * @return the sum of the values or 0 if length = 0
116      * @throws MathIllegalArgumentException if the array is null or the array index
117      *  parameters are not valid
118      */
119     @Override
120     public double evaluate(final double[] values, final int begin, final int length)
121     throws MathIllegalArgumentException {
122         double sum = Double.NaN;
123         if (test(values, begin, length, true)) {
124             sum = 0.0;
125             for (int i = begin; i < begin + length; i++) {
126                 sum += values[i];
127             }
128         }
129         return sum;
130     }
131 
132     /**
133      * The weighted sum of the entries in the specified portion of
134      * the input array, or 0 if the designated subarray
135      * is empty.
136      * <p>
137      * Throws <code>MathIllegalArgumentException</code> if any of the following are true:
138      * <ul><li>the values array is null</li>
139      *     <li>the weights array is null</li>
140      *     <li>the weights array does not have the same length as the values array</li>
141      *     <li>the weights array contains one or more infinite values</li>
142      *     <li>the weights array contains one or more NaN values</li>
143      *     <li>the weights array contains negative values</li>
144      *     <li>the start and length arguments do not determine a valid array</li>
145      * </ul></p>
146      * <p>
147      * Uses the formula, <pre>
148      *    weighted sum = &Sigma;(values[i] * weights[i])
149      * </pre></p>
150      *
151      * @param values the input array
152      * @param weights the weights array
153      * @param begin index of the first array element to include
154      * @param length the number of elements to include
155      * @return the sum of the values or 0 if length = 0
156      * @throws MathIllegalArgumentException if the parameters are not valid
157      * @since 2.1
158      */
159     public double evaluate(final double[] values, final double[] weights,
160         final int begin, final int length) throws MathIllegalArgumentException {
161         double sum = Double.NaN;
162         if (test(values, weights, begin, length, true)) {
163             sum = 0.0;
164             for (int i = begin; i < begin + length; i++) {
165                 sum += values[i] * weights[i];
166             }
167         }
168         return sum;
169     }
170 
171     /**
172      * The weighted sum of the entries in the the input array.
173      * <p>
174      * Throws <code>MathIllegalArgumentException</code> if any of the following are true:
175      * <ul><li>the values array is null</li>
176      *     <li>the weights array is null</li>
177      *     <li>the weights array does not have the same length as the values array</li>
178      *     <li>the weights array contains one or more infinite values</li>
179      *     <li>the weights array contains one or more NaN values</li>
180      *     <li>the weights array contains negative values</li>
181      * </ul></p>
182      * <p>
183      * Uses the formula, <pre>
184      *    weighted sum = &Sigma;(values[i] * weights[i])
185      * </pre></p>
186      *
187      * @param values the input array
188      * @param weights the weights array
189      * @return the sum of the values or Double.NaN if length = 0
190      * @throws MathIllegalArgumentException if the parameters are not valid
191      * @since 2.1
192      */
193     public double evaluate(final double[] values, final double[] weights)
194     throws MathIllegalArgumentException {
195         return evaluate(values, weights, 0, values.length);
196     }
197 
198     /**
199      * {@inheritDoc}
200      */
201     @Override
202     public Sum copy() {
203         Sum result = new Sum();
204         // No try-catch or advertised exception because args are valid
205         copy(this, result);
206         return result;
207     }
208 
209     /**
210      * Copies source to dest.
211      * <p>Neither source nor dest can be null.</p>
212      *
213      * @param source Sum to copy
214      * @param dest Sum to copy to
215      * @throws NullArgumentException if either source or dest is null
216      */
217     public static void copy(Sum source, Sum dest)
218         throws NullArgumentException {
219         MathUtils.checkNotNull(source);
220         MathUtils.checkNotNull(dest);
221         dest.setData(source.getDataRef());
222         dest.n = source.n;
223         dest.value = source.value;
224     }
225 
226 }