SumOfLogs.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 {@link Math#log(double) natural logarithm} of available values.
  20.  *
  21.  * <ul>
  22.  *   <li>The result is zero if no values are added.
  23.  *   <li>The result is {@code NaN} if any of the values is {@code NaN}.
  24.  *   <li>The result is {@code NaN} if any of the values is negative.
  25.  * </ul>
  26.  *
  27.  * <p>The sum follows the IEEE754 result for summing infinite values:
  28.  *
  29.  * <ul>
  30.  *   <li>The result is {@code +infinity} if all values are in the range {@code (0, +infinity]}
  31.  *       and at least one value is {@code +infinity}.
  32.  *   <li>The result is {@code -infinity} if all values are in the range {@code [0, +infinity)}
  33.  *       and at least one value is zero.
  34.  *   <li>The result is {@code NaN} if all values are in the range {@code [0, +infinity]}
  35.  *       and at least one value is zero, and one value is {@code +infinity}.
  36.  * </ul>
  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 instance 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.DoubleConsumer#accept(double) 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.DoubleConsumer#accept(double) 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 instance 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.  * @see org.apache.commons.numbers.core.Sum
  55.  * @see Math#log(double)
  56.  * @since 1.1
  57.  */
  58. public final class SumOfLogs implements DoubleStatistic, StatisticAccumulator<SumOfLogs> {

  59.     /** {@link org.apache.commons.numbers.core.Sum Sum} used to compute the sum. */
  60.     private final org.apache.commons.numbers.core.Sum delegate =
  61.             org.apache.commons.numbers.core.Sum.create();

  62.     /**
  63.      * Create an instance.
  64.      */
  65.     private SumOfLogs() {
  66.         // No-op
  67.     }

  68.     /**
  69.      * Creates an instance.
  70.      *
  71.      * <p>The initial result is zero.
  72.      *
  73.      * @return {@code SumOfLogs} instance.
  74.      */
  75.     public static SumOfLogs create() {
  76.         return new SumOfLogs();
  77.     }

  78.     /**
  79.      * Returns an instance populated using the input {@code values}.
  80.      *
  81.      * <p>The result is {@code NaN} if any of the values is {@code NaN}
  82.      * or negative; or the sum at any point is a {@code NaN}.
  83.      *
  84.      * <p>When the input is an empty array, the result is zero.
  85.      *
  86.      * @param values Values.
  87.      * @return {@code SumOfLogs} instance.
  88.      */
  89.     public static SumOfLogs of(double... values) {
  90.         return Statistics.add(new SumOfLogs(), values);
  91.     }

  92.     /**
  93.      * Returns an instance populated using the specified range of {@code values}.
  94.      *
  95.      * <p>The result is {@code NaN} if any of the values is {@code NaN}
  96.      * or negative; or the sum at any point is a {@code NaN}.
  97.      *
  98.      * <p>When the range is empty, the result is zero.
  99.      *
  100.      * @param values Values.
  101.      * @param from Inclusive start of the range.
  102.      * @param to Exclusive end of the range.
  103.      * @return {@code SumOfLogs} instance.
  104.      * @throws IndexOutOfBoundsException if the sub-range is out of bounds
  105.      * @since 1.2
  106.      */
  107.     public static SumOfLogs ofRange(double[] values, int from, int to) {
  108.         Statistics.checkFromToIndex(from, to, values.length);
  109.         return createFromRange(values, from, to);
  110.     }

  111.     /**
  112.      * Create an instance using the specified range of {@code values}.
  113.      *
  114.      * <p>Warning: No range checks are performed.
  115.      *
  116.      * @param values Values.
  117.      * @param from Inclusive start of the range.
  118.      * @param to Exclusive end of the range.
  119.      * @return {@code SumOfLogs} instance.
  120.      */
  121.     static SumOfLogs createFromRange(double[] values, int from, int to) {
  122.         return Statistics.add(new SumOfLogs(), values, from, to);
  123.     }

  124.     /**
  125.      * Returns an instance populated using the input {@code values}.
  126.      *
  127.      * <p>The result is {@code NaN} if any of the values is negative.
  128.      *
  129.      * <p>When the input is an empty array, the result is zero.
  130.      *
  131.      * @param values Values.
  132.      * @return {@code SumOfLogs} instance.
  133.      */
  134.     public static SumOfLogs of(int... values) {
  135.         return Statistics.add(new SumOfLogs(), values);
  136.     }

  137.     /**
  138.      * Returns an instance populated using the specified range of {@code values}.
  139.      *
  140.      * <p>The result is {@code NaN} if any of the values is negative.
  141.      *
  142.      * <p>When the range is empty, the result is zero.
  143.      *
  144.      * @param values Values.
  145.      * @param from Inclusive start of the range.
  146.      * @param to Exclusive end of the range.
  147.      * @return {@code SumOfLogs} instance.
  148.      * @throws IndexOutOfBoundsException if the sub-range is out of bounds
  149.      * @since 1.2
  150.      */
  151.     public static SumOfLogs ofRange(int[] values, int from, int to) {
  152.         Statistics.checkFromToIndex(from, to, values.length);
  153.         return createFromRange(values, from, to);
  154.     }

  155.     /**
  156.      * Create an instance using the specified range of {@code values}.
  157.      *
  158.      * <p>Warning: No range checks are performed.
  159.      *
  160.      * @param values Values.
  161.      * @param from Inclusive start of the range.
  162.      * @param to Exclusive end of the range.
  163.      * @return {@code SumOfLogs} instance.
  164.      */
  165.     static SumOfLogs createFromRange(int[] values, int from, int to) {
  166.         return Statistics.add(new SumOfLogs(), values, from, to);
  167.     }

  168.     /**
  169.      * Returns an instance populated using the input {@code values}.
  170.      *
  171.      * <p>The result is {@code NaN} if any of the values is negative.
  172.      *
  173.      * <p>When the input is an empty array, the result is zero.
  174.      *
  175.      * @param values Values.
  176.      * @return {@code SumOfLogs} instance.
  177.      */
  178.     public static SumOfLogs of(long... values) {
  179.         return Statistics.add(new SumOfLogs(), values);
  180.     }

  181.     /**
  182.      * Returns an instance populated using the specified range of {@code values}.
  183.      *
  184.      * <p>The result is {@code NaN} if any of the values is negative.
  185.      *
  186.      * <p>When the range is empty, the result is zero.
  187.      *
  188.      * @param values Values.
  189.      * @param from Inclusive start of the range.
  190.      * @param to Exclusive end of the range.
  191.      * @return {@code SumOfLogs} instance.
  192.      * @throws IndexOutOfBoundsException if the sub-range is out of bounds
  193.      * @since 1.2
  194.      */
  195.     public static SumOfLogs ofRange(long[] values, int from, int to) {
  196.         Statistics.checkFromToIndex(from, to, values.length);
  197.         return createFromRange(values, from, to);
  198.     }

  199.     /**
  200.      * Create an instance using the specified range of {@code values}.
  201.      *
  202.      * <p>Warning: No range checks are performed.
  203.      *
  204.      * @param values Values.
  205.      * @param from Inclusive start of the range.
  206.      * @param to Exclusive end of the range.
  207.      * @return {@code SumOfLogs} instance.
  208.      */
  209.     static SumOfLogs createFromRange(long[] values, int from, int to) {
  210.         return Statistics.add(new SumOfLogs(), values, from, to);
  211.     }

  212.     /**
  213.      * Updates the state of the statistic to reflect the addition of {@code value}.
  214.      *
  215.      * @param value Value.
  216.      */
  217.     @Override
  218.     public void accept(double value) {
  219.         delegate.accept(Math.log(value));
  220.     }

  221.     /**
  222.      * Gets the sum of all input values.
  223.      *
  224.      * <p>When no values have been added, the result is zero.
  225.      *
  226.      * @return sum of all values.
  227.      */
  228.     @Override
  229.     public double getAsDouble() {
  230.         return delegate.getAsDouble();
  231.     }

  232.     @Override
  233.     public SumOfLogs combine(SumOfLogs other) {
  234.         delegate.add(other.delegate);
  235.         return this;
  236.     }
  237. }