View Javadoc
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  /**
20   * Returns the sum of the {@link Math#log(double) natural logarithm} of available values.
21   *
22   * <ul>
23   *   <li>The result is zero if no values are added.
24   *   <li>The result is {@code NaN} if any of the values is {@code NaN}.
25   *   <li>The result is {@code NaN} if any of the values is negative.
26   * </ul>
27   *
28   * <p>The sum follows the IEEE754 result for summing infinite values:
29   *
30   * <ul>
31   *   <li>The result is {@code +infinity} if all values are in the range {@code (0, +infinity]}
32   *       and at least one value is {@code +infinity}.
33   *   <li>The result is {@code -infinity} if all values are in the range {@code [0, +infinity)}
34   *       and at least one value is zero.
35   *   <li>The result is {@code NaN} if all values are in the range {@code [0, +infinity]}
36   *       and at least one value is zero, and one value is {@code +infinity}.
37   * </ul>
38   *
39   * <p>This class is designed to work with (though does not require)
40   * {@linkplain java.util.stream streams}.
41   *
42   * <p><strong>This instance is not thread safe.</strong>
43   * If multiple threads access an instance of this class concurrently,
44   * and at least one of the threads invokes the {@link java.util.function.DoubleConsumer#accept(double) accept} or
45   * {@link StatisticAccumulator#combine(StatisticResult) combine} method, it must be synchronized externally.
46   *
47   * <p>However, it is safe to use {@link java.util.function.DoubleConsumer#accept(double) accept}
48   * and {@link StatisticAccumulator#combine(StatisticResult) combine}
49   * as {@code accumulator} and {@code combiner} functions of
50   * {@link java.util.stream.Collector Collector} on a parallel stream,
51   * because the parallel instance of {@link java.util.stream.Stream#collect Stream.collect()}
52   * provides the necessary partitioning, isolation, and merging of results for
53   * safe and efficient parallel execution.
54   *
55   * @see org.apache.commons.numbers.core.Sum
56   * @see Math#log(double)
57   * @since 1.1
58   */
59  public final class SumOfLogs implements DoubleStatistic, StatisticAccumulator<SumOfLogs> {
60  
61      /** {@link org.apache.commons.numbers.core.Sum Sum} used to compute the sum. */
62      private final org.apache.commons.numbers.core.Sum delegate =
63              org.apache.commons.numbers.core.Sum.create();
64  
65      /**
66       * Create an instance.
67       */
68      private SumOfLogs() {
69          // No-op
70      }
71  
72      /**
73       * Creates an instance.
74       *
75       * <p>The initial result is zero.
76       *
77       * @return {@code SumOfLogs} instance.
78       */
79      public static SumOfLogs create() {
80          return new SumOfLogs();
81      }
82  
83      /**
84       * Returns an instance populated using the input {@code values}.
85       *
86       * <p>The result is {@code NaN} if any of the values is {@code NaN}
87       * or negative; or the sum at any point is a {@code NaN}.
88       *
89       * <p>When the input is an empty array, the result is zero.
90       *
91       * @param values Values.
92       * @return {@code SumOfLogs} instance.
93       */
94      public static SumOfLogs of(double... values) {
95          return Statistics.add(new SumOfLogs(), values);
96      }
97  
98      /**
99       * Returns an instance populated using the input {@code values}.
100      *
101      * <p>The result is {@code NaN} if any of the values is negative.
102      *
103      * <p>When the input is an empty array, the result is zero.
104      *
105      * @param values Values.
106      * @return {@code SumOfLogs} instance.
107      */
108     public static SumOfLogs of(int... values) {
109         return Statistics.add(new SumOfLogs(), values);
110     }
111 
112     /**
113      * Returns an instance populated using the input {@code values}.
114      *
115      * <p>The result is {@code NaN} if any of the values is negative.
116      *
117      * <p>When the input is an empty array, the result is zero.
118      *
119      * @param values Values.
120      * @return {@code SumOfLogs} instance.
121      */
122     public static SumOfLogs of(long... values) {
123         return Statistics.add(new SumOfLogs(), values);
124     }
125 
126     /**
127      * Updates the state of the statistic to reflect the addition of {@code value}.
128      *
129      * @param value Value.
130      */
131     @Override
132     public void accept(double value) {
133         delegate.accept(Math.log(value));
134     }
135 
136     /**
137      * Gets the sum of all input values.
138      *
139      * <p>When no values have been added, the result is zero.
140      *
141      * @return sum of all values.
142      */
143     @Override
144     public double getAsDouble() {
145         return delegate.getAsDouble();
146     }
147 
148     @Override
149     public SumOfLogs combine(SumOfLogs other) {
150         delegate.add(other.delegate);
151         return this;
152     }
153 }