psteitz     2004/06/17 23:32:07

  Modified:    math/src/java/org/apache/commons/math/stat/univariate/summary
                        SumOfLogs.java
  Log:
  Improved efficiency and javadoc.
  
  Revision  Changes    Path
  1.18      +27 -28    
jakarta-commons/math/src/java/org/apache/commons/math/stat/univariate/summary/SumOfLogs.java
  
  Index: SumOfLogs.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/math/src/java/org/apache/commons/math/stat/univariate/summary/SumOfLogs.java,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- SumOfLogs.java    27 Apr 2004 16:42:32 -0000      1.17
  +++ SumOfLogs.java    18 Jun 2004 06:32:07 -0000      1.18
  @@ -17,16 +17,20 @@
   
   import java.io.Serializable;
   
  -import org
  -    .apache
  -    .commons
  -    .math
  -    .stat
  -    .univariate
  -    .AbstractStorelessUnivariateStatistic;
  +import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic;
   
   /**
  - * Returns the sum of the natural logs for this collection of values.
  + * Returns the sum of the natural logs for this collection of values.  
  + * <p>
  + * Uses [EMAIL PROTECTED] java.lang.Math#log(double)} to compute the logs.  
Therefore,
  + * <ul>
  + * <li>If any of values are < 0, the result is <code>NaN.</code></li>
  + * <li>If all values are non-negative and less than 
<code>Double.POSITIVE_INFINITY</code>, 
  + * but at least one value is 0, the result is 
<code>Double.NEGATIVE_INFINITY.</code></li>
  + * <li>If both <code>Double.POSITIVE_INFINITY</code> and 
  + * <code>Double.NEGATIVE_INFINITY</code> are among the values, the result is
  + * <code>NaN.</code></li>
  + * </ul>
    * 
    * @version $Revision$ $Date$
    */
  @@ -35,27 +39,19 @@
       /** Serializable version identifier */
       static final long serialVersionUID = -370076995648386763L;    
   
  -    /** */
  +    /**Number of values that have been added */
       private int n = 0;
       
       /**
        * The currently running value
        */
  -    private double value = Double.NaN;
  -
  -    /** */
  -    private boolean init = true;
  +    private double value = 0d;
   
       /**
        * @see 
org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
        */
       public void increment(final double d) {
  -        if (init) {
  -            value = Math.log(d);
  -            init = false;
  -        } else {
  -            value += Math.log(d);
  -        }
  +        value += Math.log(d);
           n++;
       }
   
  @@ -63,7 +59,11 @@
        * @see 
org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
        */
       public double getResult() {
  -        return value;
  +        if (n > 0) {
  +            return value;
  +        } else {
  +            return Double.NaN;
  +        }
       }
   
       /**
  @@ -77,23 +77,22 @@
        * @see 
org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
        */
       public void clear() {
  -        value = Double.NaN;
  -        init = true;
  +        value = 0d;
           n = 0;
       }
   
       /**
  -     * Returns the sum of the natural logs for this collection of values
  +     * Returns the sum of the natural logs for this collection of values.
  +     * <p>
  +     * See [EMAIL PROTECTED] SumOfLogs}.
  +     * 
        * @param values Is a double[] containing the values
        * @param begin processing at this point in the array
        * @param length the number of elements to include
        * @return the sumLog value or Double.NaN if the array is empty
        * @see 
org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, 
int)
        */
  -    public double evaluate(
  -        final double[] values,
  -        final int begin,
  -        final int length) {
  +    public double evaluate(final double[] values, final int begin, final int 
length) {
           double sumLog = Double.NaN;
           if (test(values, begin, length)) {
               sumLog = 0.0;
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to