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.math.stat.descriptive.rank;
18  
19  import java.io.Serializable;
20  
21  import org.apache.commons.math.stat.descriptive.AbstractStorelessUnivariateStatistic;
22  
23  /**
24   * Returns the minimum of the available values.
25   * <p>
26   * <ul>
27   * <li>The result is <code>NaN</code> iff all values are <code>NaN</code> 
28   * (i.e. <code>NaN</code> values have no impact on the value of the statistic).</li>
29   * <li>If any of the values equals <code>Double.NEGATIVE_INFINITY</code>, 
30   * the result is <code>Double.NEGATIVE_INFINITY.</code></li>
31   * </ul></p>
32   * <p>
33   * <strong>Note that this implementation is not synchronized.</strong> If 
34   * multiple threads access an instance of this class concurrently, and at least
35   * one of the threads invokes the <code>increment()</code> or 
36   * <code>clear()</code> method, it must be synchronized externally.</p>
37   * 
38   * @version $Revision: 617953 $ $Date: 2008-02-02 22:54:00 -0700 (Sat, 02 Feb 2008) $
39   */
40  public class Min extends AbstractStorelessUnivariateStatistic implements Serializable {
41  
42      /** Serializable version identifier */
43      private static final long serialVersionUID = -2941995784909003131L;  
44        
45      /**Number of values that have been added */
46      private long n;
47      
48      /**Current value of the statistic */
49      private double value;
50  
51      /**
52       * Create a Min instance
53       */
54      public Min() {
55          n = 0;
56          value = Double.NaN;
57      }
58      
59      /**
60       * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#increment(double)
61       */
62      public void increment(final double d) {
63          if (d < value || Double.isNaN(value)) {
64              value = d;
65          }
66          n++;
67      }
68  
69      /**
70       * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#clear()
71       */
72      public void clear() {
73          value = Double.NaN;
74          n = 0;
75      }
76  
77      /**
78       * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#getResult()
79       */
80      public double getResult() {
81          return value;
82      }
83  
84      /**
85       * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#getN()
86       */
87      public long getN() {
88          return n;
89      }
90      
91      /**
92       * Returns the minimum of the entries in the specified portion of
93       * the input array, or <code>Double.NaN</code> if the designated subarray
94       * is empty.
95       * <p>
96       * Throws <code>IllegalArgumentException</code> if the array is null or
97       * the array index parameters are not valid.</p>
98       * <p>
99       * <ul>
100      * <li>The result is <code>NaN</code> iff all values are <code>NaN</code> 
101      * (i.e. <code>NaN</code> values have no impact on the value of the statistic).</li>
102      * <li>If any of the values equals <code>Double.NEGATIVE_INFINITY</code>, 
103      * the result is <code>Double.NEGATIVE_INFINITY.</code></li>
104      * </ul> </p>
105      * 
106      * @param values the input array
107      * @param begin index of the first array element to include
108      * @param length the number of elements to include
109      * @return the minimum of the values or Double.NaN if length = 0
110      * @throws IllegalArgumentException if the array is null or the array index
111      *  parameters are not valid
112      */
113     public double evaluate(final double[] values,final int begin, final int length) {
114         double min = Double.NaN;
115         if (test(values, begin, length)) {
116             min = values[begin];
117             for (int i = begin; i < begin + length; i++) {
118                 if (!Double.isNaN(values[i])) {
119                     min = (min < values[i]) ? min : values[i];
120                 }
121             }
122         }
123         return min;
124     }
125 }