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.math3.stat.descriptive.summary;
018
019import java.io.Serializable;
020
021import org.apache.commons.math3.exception.MathIllegalArgumentException;
022import org.apache.commons.math3.exception.NullArgumentException;
023import org.apache.commons.math3.stat.descriptive.AbstractStorelessUnivariateStatistic;
024import org.apache.commons.math3.util.FastMath;
025import org.apache.commons.math3.util.MathUtils;
026
027/**
028 * Returns the sum of the natural logs for this collection of values.
029 * <p>
030 * Uses {@link org.apache.commons.math3.util.FastMath#log(double)} to compute the logs.
031 * Therefore,
032 * <ul>
033 * <li>If any of values are &lt; 0, the result is <code>NaN.</code></li>
034 * <li>If all values are non-negative and less than
035 * <code>Double.POSITIVE_INFINITY</code>,  but at least one value is 0, the
036 * result is <code>Double.NEGATIVE_INFINITY.</code></li>
037 * <li>If both <code>Double.POSITIVE_INFINITY</code> and
038 * <code>Double.NEGATIVE_INFINITY</code> are among the values, the result is
039 * <code>NaN.</code></li>
040 * </ul></p>
041 * <p>
042 * <strong>Note that this implementation is not synchronized.</strong> If
043 * multiple threads access an instance of this class concurrently, and at least
044 * one of the threads invokes the <code>increment()</code> or
045 * <code>clear()</code> method, it must be synchronized externally.</p>
046 *
047 */
048public class SumOfLogs extends AbstractStorelessUnivariateStatistic implements Serializable {
049
050    /** Serializable version identifier */
051    private static final long serialVersionUID = -370076995648386763L;
052
053    /**Number of values that have been added */
054    private int n;
055
056    /**
057     * The currently running value
058     */
059    private double value;
060
061    /**
062     * Create a SumOfLogs instance
063     */
064    public SumOfLogs() {
065       value = 0d;
066       n = 0;
067    }
068
069    /**
070     * Copy constructor, creates a new {@code SumOfLogs} identical
071     * to the {@code original}
072     *
073     * @param original the {@code SumOfLogs} instance to copy
074     * @throws NullArgumentException if original is null
075     */
076    public SumOfLogs(SumOfLogs original) throws NullArgumentException {
077        copy(original, this);
078    }
079
080    /**
081     * {@inheritDoc}
082     */
083    @Override
084    public void increment(final double d) {
085        value += FastMath.log(d);
086        n++;
087    }
088
089    /**
090     * {@inheritDoc}
091     */
092    @Override
093    public double getResult() {
094        return value;
095    }
096
097    /**
098     * {@inheritDoc}
099     */
100    public long getN() {
101        return n;
102    }
103
104    /**
105     * {@inheritDoc}
106     */
107    @Override
108    public void clear() {
109        value = 0d;
110        n = 0;
111    }
112
113    /**
114     * Returns the sum of the natural logs of the entries in the specified portion of
115     * the input array, or <code>Double.NaN</code> if the designated subarray
116     * is empty.
117     * <p>
118     * Throws <code>MathIllegalArgumentException</code> if the array is null.</p>
119     * <p>
120     * See {@link SumOfLogs}.</p>
121     *
122     * @param values the input array
123     * @param begin index of the first array element to include
124     * @param length the number of elements to include
125     * @return the sum of the natural logs of the values or 0 if
126     * length = 0
127     * @throws MathIllegalArgumentException if the array is null or the array index
128     *  parameters are not valid
129     */
130    @Override
131    public double evaluate(final double[] values, final int begin, final int length)
132    throws MathIllegalArgumentException {
133        double sumLog = Double.NaN;
134        if (test(values, begin, length, true)) {
135            sumLog = 0.0;
136            for (int i = begin; i < begin + length; i++) {
137                sumLog += FastMath.log(values[i]);
138            }
139        }
140        return sumLog;
141    }
142
143    /**
144     * {@inheritDoc}
145     */
146    @Override
147    public SumOfLogs copy() {
148        SumOfLogs result = new SumOfLogs();
149        // No try-catch or advertised exception here because args are valid
150        copy(this, result);
151        return result;
152    }
153
154    /**
155     * Copies source to dest.
156     * <p>Neither source nor dest can be null.</p>
157     *
158     * @param source SumOfLogs to copy
159     * @param dest SumOfLogs to copy to
160     * @throws NullArgumentException if either source or dest is null
161     */
162    public static void copy(SumOfLogs source, SumOfLogs dest)
163        throws NullArgumentException {
164        MathUtils.checkNotNull(source);
165        MathUtils.checkNotNull(dest);
166        dest.setData(source.getDataRef());
167        dest.n = source.n;
168        dest.value = source.value;
169    }
170}