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.  *      https://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. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.stat.descriptive.summary;

  22. import java.io.Serializable;

  23. import org.hipparchus.exception.MathIllegalArgumentException;
  24. import org.hipparchus.exception.NullArgumentException;
  25. import org.hipparchus.stat.descriptive.AbstractStorelessUnivariateStatistic;
  26. import org.hipparchus.stat.descriptive.AggregatableStatistic;
  27. import org.hipparchus.util.MathArrays;
  28. import org.hipparchus.util.MathUtils;

  29. /**
  30.  * Returns the sum of the squares of the available values.
  31.  * <p>
  32.  * If there are no values in the dataset, then 0 is returned.
  33.  * If any of the values are
  34.  * <code>NaN</code>, then <code>NaN</code> is returned.
  35.  * <p>
  36.  * <strong>Note that this implementation is not synchronized.</strong> If
  37.  * multiple threads access an instance of this class concurrently, and at least
  38.  * one of the threads invokes the <code>increment()</code> or
  39.  * <code>clear()</code> method, it must be synchronized externally.
  40.  */
  41. public class SumOfSquares extends AbstractStorelessUnivariateStatistic
  42.     implements AggregatableStatistic<SumOfSquares>, Serializable {

  43.     /** Serializable version identifier */
  44.     private static final long serialVersionUID = 20150412L;

  45.     /** Number of values that have been added */
  46.     private long n;

  47.     /** The currently running sumSq */
  48.     private double value;

  49.     /**
  50.      * Create a SumOfSquares instance.
  51.      */
  52.     public SumOfSquares() {
  53.         n = 0;
  54.         value = 0;
  55.     }

  56.     /**
  57.      * Copy constructor, creates a new {@code SumOfSquares} identical
  58.      * to the {@code original}.
  59.      *
  60.      * @param original the {@code SumOfSquares} instance to copy
  61.      * @throws NullArgumentException if original is null
  62.      */
  63.     public SumOfSquares(SumOfSquares original) throws NullArgumentException {
  64.         MathUtils.checkNotNull(original);
  65.         this.n     = original.n;
  66.         this.value = original.value;
  67.     }

  68.     /** {@inheritDoc} */
  69.     @Override
  70.     public void increment(final double d) {
  71.         value += d * d;
  72.         n++;
  73.     }

  74.     /** {@inheritDoc} */
  75.     @Override
  76.     public double getResult() {
  77.         return value;
  78.     }

  79.     /** {@inheritDoc} */
  80.     @Override
  81.     public long getN() {
  82.         return n;
  83.     }

  84.     /** {@inheritDoc} */
  85.     @Override
  86.     public void clear() {
  87.         value = 0;
  88.         n = 0;
  89.     }

  90.     /** {@inheritDoc} */
  91.     @Override
  92.     public void aggregate(SumOfSquares other) {
  93.         MathUtils.checkNotNull(other);
  94.         if (other.n > 0) {
  95.             this.n     += other.n;
  96.             this.value += other.value;
  97.         }
  98.     }

  99.     /**
  100.      * Returns the sum of the squares of the entries in the specified portion of
  101.      * the input array, or <code>Double.NaN</code> if the designated subarray
  102.      * is empty.
  103.      *
  104.      * @param values the input array
  105.      * @param begin index of the first array element to include
  106.      * @param length the number of elements to include
  107.      * @return the sum of the squares of the values or 0 if length = 0
  108.      * @throws MathIllegalArgumentException if the array is null or the array index
  109.      *  parameters are not valid
  110.      */
  111.     @Override
  112.     public double evaluate(final double[] values,final int begin, final int length)
  113.         throws MathIllegalArgumentException {

  114.         double sumSq = Double.NaN;
  115.         if (MathArrays.verifyValues(values, begin, length, true)) {
  116.             sumSq = 0.0;
  117.             for (int i = begin; i < begin + length; i++) {
  118.                 sumSq += values[i] * values[i];
  119.             }
  120.         }
  121.         return sumSq;
  122.     }

  123.     /** {@inheritDoc} */
  124.     @Override
  125.     public SumOfSquares copy() {
  126.         return new SumOfSquares(this);
  127.     }

  128. }