001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.statistics.descriptive;
018
019/**
020 * Returns the sum of the {@link Math#log(double) natural logarithm} of available values.
021 *
022 * <ul>
023 *   <li>The result is zero if no values are added.
024 *   <li>The result is {@code NaN} if any of the values is {@code NaN}.
025 *   <li>The result is {@code NaN} if any of the values is negative.
026 * </ul>
027 *
028 * <p>The sum follows the IEEE754 result for summing infinite values:
029 *
030 * <ul>
031 *   <li>The result is {@code +infinity} if all values are in the range {@code (0, +infinity]}
032 *       and at least one value is {@code +infinity}.
033 *   <li>The result is {@code -infinity} if all values are in the range {@code [0, +infinity)}
034 *       and at least one value is zero.
035 *   <li>The result is {@code NaN} if all values are in the range {@code [0, +infinity]}
036 *       and at least one value is zero, and one value is {@code +infinity}.
037 * </ul>
038 *
039 * <p>This class is designed to work with (though does not require)
040 * {@linkplain java.util.stream streams}.
041 *
042 * <p><strong>This instance is not thread safe.</strong>
043 * If multiple threads access an instance of this class concurrently,
044 * and at least one of the threads invokes the {@link java.util.function.DoubleConsumer#accept(double) accept} or
045 * {@link StatisticAccumulator#combine(StatisticResult) combine} method, it must be synchronized externally.
046 *
047 * <p>However, it is safe to use {@link java.util.function.DoubleConsumer#accept(double) accept}
048 * and {@link StatisticAccumulator#combine(StatisticResult) combine}
049 * as {@code accumulator} and {@code combiner} functions of
050 * {@link java.util.stream.Collector Collector} on a parallel stream,
051 * because the parallel instance of {@link java.util.stream.Stream#collect Stream.collect()}
052 * provides the necessary partitioning, isolation, and merging of results for
053 * safe and efficient parallel execution.
054 *
055 * @see org.apache.commons.numbers.core.Sum
056 * @see Math#log(double)
057 * @since 1.1
058 */
059public final class SumOfLogs implements DoubleStatistic, StatisticAccumulator<SumOfLogs> {
060
061    /** {@link org.apache.commons.numbers.core.Sum Sum} used to compute the sum. */
062    private final org.apache.commons.numbers.core.Sum delegate =
063            org.apache.commons.numbers.core.Sum.create();
064
065    /**
066     * Create an instance.
067     */
068    private SumOfLogs() {
069        // No-op
070    }
071
072    /**
073     * Creates an instance.
074     *
075     * <p>The initial result is zero.
076     *
077     * @return {@code SumOfLogs} instance.
078     */
079    public static SumOfLogs create() {
080        return new SumOfLogs();
081    }
082
083    /**
084     * Returns an instance populated using the input {@code values}.
085     *
086     * <p>The result is {@code NaN} if any of the values is {@code NaN}
087     * or negative; or the sum at any point is a {@code NaN}.
088     *
089     * <p>When the input is an empty array, the result is zero.
090     *
091     * @param values Values.
092     * @return {@code SumOfLogs} instance.
093     */
094    public static SumOfLogs of(double... values) {
095        return Statistics.add(new SumOfLogs(), values);
096    }
097
098    /**
099     * 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}