View Javadoc

1   /*
2    * Copyright 2003-2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.math.stat.descriptive.rank;
17  
18  import java.io.Serializable;
19  
20  import org.apache.commons.math.stat.descriptive.AbstractStorelessUnivariateStatistic;
21  
22  /**
23   * Returns the minimum of the available values.
24   * <p>
25   * <ul>
26   * <li>The result is <code>NaN</code> iff all values are <code>NaN</code> 
27   * (i.e. <code>NaN</code> values have no impact on the value of the statistic).</li>
28   * <li>If any of the values equals <code>Double.NEGATIVE_INFINITY</code>, 
29   * the result is <code>Double.NEGATIVE_INFINITY.</code></li>
30   * </ul> 
31   * <p>
32   * <strong>Note that this implementation is not synchronized.</strong> If 
33   * multiple threads access an instance of this class concurrently, and at least
34   * one of the threads invokes the <code>increment()</code> or 
35   * <code>clear()</code> method, it must be synchronized externally.
36   * 
37   * @version $Revision: 348519 $ $Date: 2005-11-23 12:12:18 -0700 (Wed, 23 Nov 2005) $
38   */
39  public class Min extends AbstractStorelessUnivariateStatistic implements Serializable {
40  
41      /** Serializable version identifier */
42      private static final long serialVersionUID = -2941995784909003131L;  
43        
44      /**Number of values that have been added */
45      private long n;
46      
47      /**Current value of the statistic */
48      private double value;
49  
50      /**
51       * Create a Min instance
52       */
53      public Min() {
54          n = 0;
55          value = Double.NaN;
56      }
57      
58      /**
59       * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#increment(double)
60       */
61      public void increment(final double d) {
62          if (d < value || Double.isNaN(value)) {
63              value = d;
64          }
65          n++;
66      }
67  
68      /**
69       * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#clear()
70       */
71      public void clear() {
72          value = Double.NaN;
73          n = 0;
74      }
75  
76      /**
77       * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#getResult()
78       */
79      public double getResult() {
80          return value;
81      }
82  
83      /**
84       * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#getN()
85       */
86      public long getN() {
87          return n;
88      }
89      
90      /**
91       * Returns the minimum of the entries in the specified portion of
92       * the input array, or <code>Double.NaN</code> if the designated subarray
93       * is empty.
94       * <p>
95       * Throws <code>IllegalArgumentException</code> if the array is null or
96       * the array index parameters are not valid.
97       * <p>
98       * <ul>
99       * <li>The result is <code>NaN</code> iff all values are <code>NaN</code> 
100      * (i.e. <code>NaN</code> values have no impact on the value of the statistic).</li>
101      * <li>If any of the values equals <code>Double.NEGATIVE_INFINITY</code>, 
102      * the result is <code>Double.NEGATIVE_INFINITY.</code></li>
103      * </ul> 
104      * 
105      * @param values the input array
106      * @param begin index of the first array element to include
107      * @param length the number of elements to include
108      * @return the minimum of the values or Double.NaN if length = 0
109      * @throws IllegalArgumentException if the array is null or the array index
110      *  parameters are not valid
111      */
112     public double evaluate(final double[] values,final int begin, final int length) {
113         double min = Double.NaN;
114         if (test(values, begin, length)) {
115             min = values[begin];
116             for (int i = begin; i < begin + length; i++) {
117                 if (!Double.isNaN(values[i])) {
118                     min = (min < values[i]) ? min : values[i];
119                 }
120             }
121         }
122         return min;
123     }
124 }