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.statistics.descriptive;

  18. /**
  19.  * Returns the sum of the squares of the available values. Uses the following definition:
  20.  *
  21.  * <p>\[ \sum_{i=1}^n x_i^2 \]
  22.  *
  23.  * <p>where \( n \) is the number of samples.
  24.  *
  25.  * <ul>
  26.  *   <li>The result is zero if no values are observed.
  27.  *   <li>The result is {@code NaN} if any of the values is {@code NaN}.
  28.  *   <li>The result is {@code +infinity} if any of the values is {@code infinity},
  29.  *       or the sum overflows.
  30.  * </ul>
  31.  *
  32.  * <p>This class is designed to work with (though does not require)
  33.  * {@linkplain java.util.stream streams}.
  34.  *
  35.  * <p><strong>This instance is not thread safe.</strong>
  36.  * If multiple threads access an instance of this class concurrently,
  37.  * and at least one of the threads invokes the {@link java.util.function.DoubleConsumer#accept(double) accept} or
  38.  * {@link StatisticAccumulator#combine(StatisticResult) combine} method, it must be synchronized externally.
  39.  *
  40.  * <p>However, it is safe to use {@link java.util.function.DoubleConsumer#accept(double) accept}
  41.  * and {@link StatisticAccumulator#combine(StatisticResult) combine}
  42.  * as {@code accumulator} and {@code combiner} functions of
  43.  * {@link java.util.stream.Collector Collector} on a parallel stream,
  44.  * because the parallel instance of {@link java.util.stream.Stream#collect Stream.collect()}
  45.  * provides the necessary partitioning, isolation, and merging of results for
  46.  * safe and efficient parallel execution.
  47.  *
  48.  * @since 1.1
  49.  */
  50. public final class SumOfSquares implements DoubleStatistic, StatisticAccumulator<SumOfSquares> {

  51.     /** Sum of squares of all values. */
  52.     private double ss;

  53.     /**
  54.      * Create an instance.
  55.      */
  56.     private SumOfSquares() {
  57.         // No-op
  58.     }

  59.     /**
  60.      * Creates an instance.
  61.      *
  62.      * <p>The initial result is zero.
  63.      *
  64.      * @return {@code SumOfSquares} instance.
  65.      */
  66.     public static SumOfSquares create() {
  67.         return new SumOfSquares();
  68.     }

  69.     /**
  70.      * Returns an instance populated using the input {@code values}.
  71.      *
  72.      * <p>The result is {@code NaN} if any of the values is {@code NaN}
  73.      * or the product at any point is a {@code NaN}.
  74.      *
  75.      * <p>When the input is an empty array, the result is zero.
  76.      *
  77.      * @param values Values.
  78.      * @return {@code SumOfSquares} instance.
  79.      */
  80.     public static SumOfSquares of(double... values) {
  81.         return Statistics.add(new SumOfSquares(), values);
  82.     }

  83.     /**
  84.      * Updates the state of the statistic to reflect the addition of {@code value}.
  85.      *
  86.      * @param value Value.
  87.      */
  88.     @Override
  89.     public void accept(double value) {
  90.         ss += value * value;
  91.     }

  92.     /**
  93.      * Gets the sum of squares of all input values.
  94.      *
  95.      * <p>When no values have been added, the result is zero.
  96.      *
  97.      * @return sum of squares of all values.
  98.      */
  99.     @Override
  100.     public double getAsDouble() {
  101.         return ss;
  102.     }

  103.     @Override
  104.     public SumOfSquares combine(SumOfSquares other) {
  105.         ss += other.ss;
  106.         return this;
  107.     }
  108. }