SumOfSquares.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 squares 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 SumOfSquares extends AbstractStorelessUnivariateStatistic {
  35.     /** Number of values that have been added. */
  36.     private long n;

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

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

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

  58.     /**
  59.      * {@inheritDoc}
  60.      */
  61.     @Override
  62.     public void increment(final double d) {
  63.         value += d * 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.      * Returns the sum of the squares of the entries in the specified portion of
  90.      * the input array, or <code>Double.NaN</code> if the designated subarray
  91.      * is empty.
  92.      * <p>
  93.      * Throws <code>MathIllegalArgumentException</code> if the array is null.</p>
  94.      *
  95.      * @param values the input array
  96.      * @param begin index of the first array element to include
  97.      * @param length the number of elements to include
  98.      * @return the sum of the squares of the values or 0 if length = 0
  99.      * @throws MathIllegalArgumentException if the array is null or the array index
  100.      *  parameters are not valid
  101.      */
  102.     @Override
  103.     public double evaluate(final double[] values,final int begin, final int length)
  104.         throws MathIllegalArgumentException {

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

  114.     /**
  115.      * {@inheritDoc}
  116.      */
  117.     @Override
  118.     public SumOfSquares copy() {
  119.         SumOfSquares result = new SumOfSquares();
  120.         // no try-catch or advertised exception here because args are valid
  121.         copy(this, result);
  122.         return result;
  123.     }

  124.     /**
  125.      * Copies source to dest.
  126.      * <p>Neither source nor dest can be null.</p>
  127.      *
  128.      * @param source SumOfSquares to copy
  129.      * @param dest SumOfSquares to copy to
  130.      * @throws NullArgumentException if either source or dest is null
  131.      */
  132.     public static void copy(SumOfSquares source, SumOfSquares dest)
  133.         throws NullArgumentException {
  134.         NullArgumentException.check(source);
  135.         NullArgumentException.check(dest);
  136.         dest.n = source.n;
  137.         dest.value = source.value;
  138.     }
  139. }