Sum.java

  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.math4.legacy.stat.descriptive.summary;

  18. import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException;
  19. import org.apache.commons.math4.legacy.exception.NullArgumentException;
  20. import org.apache.commons.math4.legacy.stat.descriptive.AbstractStorelessUnivariateStatistic;
  21. import org.apache.commons.math4.legacy.core.MathArrays;


  22. /**
  23.   * Returns the sum of the available values.
  24.  * <p>
  25.  * If there are no values in the dataset, then 0 is returned.
  26.  * If any of the values are
  27.  * <code>NaN</code>, then <code>NaN</code> is returned.</p>
  28.  * <p>
  29.  * <strong>Note that this implementation is not synchronized.</strong> If
  30.  * multiple threads access an instance of this class concurrently, and at least
  31.  * one of the threads invokes the <code>increment()</code> or
  32.  * <code>clear()</code> method, it must be synchronized externally.</p>
  33.  */
  34. public class Sum extends AbstractStorelessUnivariateStatistic {
  35.     /** */
  36.     private long n;

  37.     /**
  38.      * The currently running sum.
  39.      */
  40.     private double value;

  41.     /**
  42.      * Create a Sum instance.
  43.      */
  44.     public Sum() {
  45.         n = 0;
  46.         value = 0;
  47.     }

  48.     /**
  49.      * Copy constructor, creates a new {@code Sum} identical
  50.      * to the {@code original}.
  51.      *
  52.      * @param original the {@code Sum} instance to copy
  53.      * @throws NullArgumentException if original is null
  54.      */
  55.     public Sum(Sum original) throws NullArgumentException {
  56.         copy(original, this);
  57.     }

  58.     /**
  59.      * {@inheritDoc}
  60.      */
  61.     @Override
  62.     public void increment(final double d) {
  63.         value += d;
  64.         n++;
  65.     }

  66.     /**
  67.      * {@inheritDoc}
  68.      */
  69.     @Override
  70.     public double getResult() {
  71.         return value;
  72.     }

  73.     /**
  74.      * {@inheritDoc}
  75.      */
  76.     @Override
  77.     public long getN() {
  78.         return n;
  79.     }

  80.     /**
  81.      * {@inheritDoc}
  82.      */
  83.     @Override
  84.     public void clear() {
  85.         value = 0;
  86.         n = 0;
  87.     }

  88.     /**
  89.      * The sum of the entries in the specified portion of the input array,
  90.      * or 0 if the designated subarray is empty.
  91.      * <p>
  92.      * Throws <code>MathIllegalArgumentException</code> if the array is null.</p>
  93.      *
  94.      * @param values the input array
  95.      * @param begin index of the first array element to include
  96.      * @param length the number of elements to include
  97.      * @return the sum of the values or 0 if length = 0
  98.      * @throws MathIllegalArgumentException if the array is null or the array index
  99.      *  parameters are not valid
  100.      */
  101.     @Override
  102.     public double evaluate(final double[] values, final int begin, final int length)
  103.         throws MathIllegalArgumentException {

  104.         double sum = Double.NaN;
  105.         if (MathArrays.verifyValues(values, begin, length, true)) {
  106.             sum = 0.0;
  107.             for (int i = begin; i < begin + length; i++) {
  108.                 sum += values[i];
  109.             }
  110.         }
  111.         return sum;
  112.     }

  113.     /**
  114.      * The weighted sum of the entries in the specified portion of
  115.      * the input array, or 0 if the designated subarray
  116.      * is empty.
  117.      * <p>
  118.      * Throws <code>MathIllegalArgumentException</code> if any of the following are true:
  119.      * <ul><li>the values array is null</li>
  120.      *     <li>the weights array is null</li>
  121.      *     <li>the weights array does not have the same length as the values array</li>
  122.      *     <li>the weights array contains one or more infinite values</li>
  123.      *     <li>the weights array contains one or more NaN values</li>
  124.      *     <li>the weights array contains negative values</li>
  125.      *     <li>the start and length arguments do not determine a valid array</li>
  126.      * </ul>
  127.      * <p>
  128.      * Uses the formula, <pre>
  129.      *    weighted sum = &Sigma;(values[i] * weights[i])
  130.      * </pre>
  131.      *
  132.      * @param values the input array
  133.      * @param weights the weights array
  134.      * @param begin index of the first array element to include
  135.      * @param length the number of elements to include
  136.      * @return the sum of the values or 0 if length = 0
  137.      * @throws MathIllegalArgumentException if the parameters are not valid
  138.      * @since 2.1
  139.      */
  140.     public double evaluate(final double[] values, final double[] weights,
  141.                            final int begin, final int length) throws MathIllegalArgumentException {
  142.         double sum = Double.NaN;
  143.         if (MathArrays.verifyValues(values, weights, begin, length, true)) {
  144.             sum = 0.0;
  145.             for (int i = begin; i < begin + length; i++) {
  146.                 sum += values[i] * weights[i];
  147.             }
  148.         }
  149.         return sum;
  150.     }

  151.     /**
  152.      * The weighted sum of the entries in the input array.
  153.      * <p>
  154.      * Throws <code>MathIllegalArgumentException</code> if any of the following are true:
  155.      * <ul><li>the values array is null</li>
  156.      *     <li>the weights array is null</li>
  157.      *     <li>the weights array does not have the same length as the values array</li>
  158.      *     <li>the weights array contains one or more infinite values</li>
  159.      *     <li>the weights array contains one or more NaN values</li>
  160.      *     <li>the weights array contains negative values</li>
  161.      * </ul>
  162.      * <p>
  163.      * Uses the formula, <pre>
  164.      *    weighted sum = &Sigma;(values[i] * weights[i])
  165.      * </pre>
  166.      *
  167.      * @param values the input array
  168.      * @param weights the weights array
  169.      * @return the sum of the values or Double.NaN if length = 0
  170.      * @throws MathIllegalArgumentException if the parameters are not valid
  171.      * @since 2.1
  172.      */
  173.     public double evaluate(final double[] values, final double[] weights) throws MathIllegalArgumentException {
  174.         return evaluate(values, weights, 0, values.length);
  175.     }

  176.     /**
  177.      * {@inheritDoc}
  178.      */
  179.     @Override
  180.     public Sum copy() {
  181.         Sum result = new Sum();
  182.         // No try-catch or advertised exception because args are valid
  183.         copy(this, result);
  184.         return result;
  185.     }

  186.     /**
  187.      * Copies source to dest.
  188.      * <p>Neither source nor dest can be null.</p>
  189.      *
  190.      * @param source Sum to copy
  191.      * @param dest Sum to copy to
  192.      * @throws NullArgumentException if either source or dest is null
  193.      */
  194.     public static void copy(Sum source, Sum dest)
  195.         throws NullArgumentException {
  196.         NullArgumentException.check(source);
  197.         NullArgumentException.check(dest);
  198.         dest.n = source.n;
  199.         dest.value = source.value;
  200.     }
  201. }