IntSumOfSquares.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. import java.math.BigInteger;

  19. /**
  20.  * Returns the sum of the squares of the available values. Uses the following definition:
  21.  *
  22.  * <p>\[ \sum_{i=1}^n x_i^2 \]
  23.  *
  24.  * <p>where \( n \) is the number of samples.
  25.  *
  26.  * <ul>
  27.  *   <li>The result is zero if no values are observed.
  28.  * </ul>
  29.  *
  30.  * <p>The implementation uses an exact integer sum to compute the sum of squared values.
  31.  * The exact sum is returned using {@link #getAsBigInteger()}. Methods that return {@code int} or
  32.  * {@code long} primitives will raise an exception if the result overflows.
  33.  *
  34.  * <p>Note that the implementation does not use {@code BigInteger} arithmetic; for
  35.  * performance the sum is computed using primitives to create an unsigned 128-bit integer.
  36.  * Support is provided for at least 2<sup>63</sup> observations.
  37.  *
  38.  * <p>This class is designed to work with (though does not require)
  39.  * {@linkplain java.util.stream streams}.
  40.  *
  41.  * <p><strong>This implementation is not thread safe.</strong>
  42.  * If multiple threads access an instance of this class concurrently,
  43.  * and at least one of the threads invokes the {@link java.util.function.IntConsumer#accept(int) accept} or
  44.  * {@link StatisticAccumulator#combine(StatisticResult) combine} method, it must be synchronized externally.
  45.  *
  46.  * <p>However, it is safe to use {@link java.util.function.IntConsumer#accept(int) accept}
  47.  * and {@link StatisticAccumulator#combine(StatisticResult) combine}
  48.  * as {@code accumulator} and {@code combiner} functions of
  49.  * {@link java.util.stream.Collector Collector} on a parallel stream,
  50.  * because the parallel implementation of {@link java.util.stream.Stream#collect Stream.collect()}
  51.  * provides the necessary partitioning, isolation, and merging of results for
  52.  * safe and efficient parallel execution.
  53.  *
  54.  * @since 1.1
  55.  */
  56. public final class IntSumOfSquares implements IntStatistic, StatisticAccumulator<IntSumOfSquares> {
  57.     /** Small array sample size.
  58.      * Used to avoid computing with UInt96 then converting to UInt128. */
  59.     private static final int SMALL_SAMPLE = 10;

  60.     /** Sum of the squared values. */
  61.     private final UInt128 sumSq;

  62.     /**
  63.      * Create an instance.
  64.      */
  65.     private IntSumOfSquares() {
  66.         this(UInt128.create());
  67.     }

  68.     /**
  69.      * Create an instance.
  70.      *
  71.      * @param sumSq Sum of the squared values.
  72.      */
  73.     private IntSumOfSquares(UInt128 sumSq) {
  74.         this.sumSq = sumSq;
  75.     }

  76.     /**
  77.      * Creates an instance.
  78.      *
  79.      * <p>The initial result is zero.
  80.      *
  81.      * @return {@code IntSumOfSquares} instance.
  82.      */
  83.     public static IntSumOfSquares create() {
  84.         return new IntSumOfSquares();
  85.     }

  86.     /**
  87.      * Returns an instance populated using the input {@code values}.
  88.      *
  89.      * <p>When the input is an empty array, the result is zero.
  90.      *
  91.      * @param values Values.
  92.      * @return {@code IntSumOfSquares} instance.
  93.      */
  94.     public static IntSumOfSquares of(int... values) {
  95.         return createFromRange(values, 0, values.length);
  96.     }

  97.     /**
  98.      * Returns an instance populated using the specified range of {@code values}.
  99.      *
  100.      * <p>When the range is empty, the result is zero.
  101.      *
  102.      * @param values Values.
  103.      * @param from Inclusive start of the range.
  104.      * @param to Exclusive end of the range.
  105.      * @return {@code IntSumOfSquares} instance.
  106.      * @throws IndexOutOfBoundsException if the sub-range is out of bounds
  107.      * @since 1.2
  108.      */
  109.     public static IntSumOfSquares ofRange(int[] values, int from, int to) {
  110.         Statistics.checkFromToIndex(from, to, values.length);
  111.         return createFromRange(values, from, to);
  112.     }

  113.     /**
  114.      * Create an instance using the specified range of {@code values}.
  115.      *
  116.      * <p>Warning: No range checks are performed.
  117.      *
  118.      * @param values Values.
  119.      * @param from Inclusive start of the range.
  120.      * @param to Exclusive end of the range.
  121.      * @return {@code IntSumOfSquares} instance.
  122.      */
  123.     static IntSumOfSquares createFromRange(int[] values, int from, int to) {
  124.         // Small arrays can be processed using the object
  125.         final int length = to - from;
  126.         if (length < SMALL_SAMPLE) {
  127.             final IntSumOfSquares stat = new IntSumOfSquares();
  128.             for (int i = from; i < to; i++) {
  129.                 stat.accept(values[i]);
  130.             }
  131.             return stat;
  132.         }

  133.         // Arrays can be processed using specialised counts knowing the maximum limit
  134.         // for an array is 2^31 values.
  135.         final UInt96 ss = UInt96.create();
  136.         // Process pairs as we know two maximum value int^2 will not overflow
  137.         // an unsigned long.
  138.         final int end = from + (length & ~0x1);
  139.         for (int i = from; i < end; i += 2) {
  140.             final long x = values[i];
  141.             final long y = values[i + 1];
  142.             ss.addPositive(x * x + y * y);
  143.         }
  144.         if (end < to) {
  145.             final long x = values[end];
  146.             ss.addPositive(x * x);
  147.         }

  148.         // Convert
  149.         return new IntSumOfSquares(UInt128.of(ss));
  150.     }

  151.     /**
  152.      * Gets the sum of squares.
  153.      *
  154.      * <p>This is package private for use in {@link IntStatistics}.
  155.      *
  156.      * @return the sum of squares
  157.      */
  158.     UInt128 getSumOfSquares() {
  159.         return sumSq;
  160.     }

  161.     /**
  162.      * Updates the state of the statistic to reflect the addition of {@code value}.
  163.      *
  164.      * @param value Value.
  165.      */
  166.     @Override
  167.     public void accept(int value) {
  168.         sumSq.addPositive((long) value * value);
  169.     }

  170.     /**
  171.      * Gets the sum of squares of all input values.
  172.      *
  173.      * <p>When no values have been added, the result is zero.
  174.      *
  175.      * <p>Warning: This will raise an {@link ArithmeticException}
  176.      * if the result is not within the range {@code [0, 2^31)}.
  177.      *
  178.      * @return sum of all values.
  179.      * @throws ArithmeticException if the {@code result} overflows an {@code int}
  180.      * @see #getAsBigInteger()
  181.      */
  182.     @Override
  183.     public int getAsInt() {
  184.         return sumSq.toIntExact();
  185.     }

  186.     /**
  187.      * Gets the sum of squares of all input values.
  188.      *
  189.      * <p>When no values have been added, the result is zero.
  190.      *
  191.      * <p>Warning: This will raise an {@link ArithmeticException}
  192.      * if the result is not within the range {@code [0, 2^63)}.
  193.      *
  194.      * @return sum of all values.
  195.      * @throws ArithmeticException if the {@code result} overflows a {@code long}
  196.      * @see #getAsBigInteger()
  197.      */
  198.     @Override
  199.     public long getAsLong() {
  200.         return sumSq.toLongExact();
  201.     }

  202.     /**
  203.      * Gets the sum of squares of all input values.
  204.      *
  205.      * <p>When no values have been added, the result is zero.
  206.      *
  207.      * <p>Note that this conversion can lose information about the precision of the
  208.      * {@code BigInteger} value.
  209.      *
  210.      * @return sum of squares of all values.
  211.      * @see #getAsBigInteger()
  212.      */
  213.     @Override
  214.     public double getAsDouble() {
  215.         return sumSq.toDouble();
  216.     }

  217.     /**
  218.      * Gets the sum of squares of all input values.
  219.      *
  220.      * <p>When no values have been added, the result is zero.
  221.      *
  222.      * @return sum of squares of all values.
  223.      */
  224.     @Override
  225.     public BigInteger getAsBigInteger() {
  226.         return sumSq.toBigInteger();
  227.     }

  228.     @Override
  229.     public IntSumOfSquares combine(IntSumOfSquares other) {
  230.         sumSq.add(other.sumSq);
  231.         return this;
  232.     }
  233. }