1dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/*
2dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Licensed to the Apache Software Foundation (ASF) under one or more
3dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * contributor license agreements.  See the NOTICE file distributed with
4dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * this work for additional information regarding copyright ownership.
5dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * The ASF licenses this file to You under the Apache License, Version 2.0
6dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * (the "License"); you may not use this file except in compliance with
7dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * the License.  You may obtain a copy of the License at
8dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
9dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *      http://www.apache.org/licenses/LICENSE-2.0
10dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
11dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Unless required by applicable law or agreed to in writing, software
12dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * distributed under the License is distributed on an "AS IS" BASIS,
13dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * See the License for the specific language governing permissions and
15dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * limitations under the License.
16dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */
17dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpackage org.apache.commons.math.stat.descriptive.rank;
18dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
19dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport java.io.Serializable;
20dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
21dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.stat.descriptive.AbstractStorelessUnivariateStatistic;
22dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
23dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/**
24dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Returns the minimum of the available values.
25dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>
26dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <ul>
27dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>The result is <code>NaN</code> iff all values are <code>NaN</code>
28dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * (i.e. <code>NaN</code> values have no impact on the value of the statistic).</li>
29dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>If any of the values equals <code>Double.NEGATIVE_INFINITY</code>,
30dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * the result is <code>Double.NEGATIVE_INFINITY.</code></li>
31dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </ul></p>
32dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>
33dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <strong>Note that this implementation is not synchronized.</strong> If
34dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * multiple threads access an instance of this class concurrently, and at least
35dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * one of the threads invokes the <code>increment()</code> or
36dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <code>clear()</code> method, it must be synchronized externally.</p>
37dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
38dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @version $Revision: 1006299 $ $Date: 2010-10-10 16:47:17 +0200 (dim. 10 oct. 2010) $
39dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */
40dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpublic class Min extends AbstractStorelessUnivariateStatistic implements Serializable {
41dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
42dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** Serializable version identifier */
43dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private static final long serialVersionUID = -2941995784909003131L;
44dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
45dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**Number of values that have been added */
46dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private long n;
47dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
48dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**Current value of the statistic */
49dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private double value;
50dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
51dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
52dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Create a Min instance
53dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
54dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public Min() {
55dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        n = 0;
56dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        value = Double.NaN;
57dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
58dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
59dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
60dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Copy constructor, creates a new {@code Min} identical
61dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * to the {@code original}
62dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
63dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param original the {@code Min} instance to copy
64dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
65dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public Min(Min original) {
66dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        copy(original, this);
67dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
68dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
69dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
70dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * {@inheritDoc}
71dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
72dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    @Override
73dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public void increment(final double d) {
74dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (d < value || Double.isNaN(value)) {
75dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            value = d;
76dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
77dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        n++;
78dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
79dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
80dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
81dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * {@inheritDoc}
82dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
83dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    @Override
84dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public void clear() {
85dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        value = Double.NaN;
86dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        n = 0;
87dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
88dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
89dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
90dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * {@inheritDoc}
91dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
92dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    @Override
93dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public double getResult() {
94dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return value;
95dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
96dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
97dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
98dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * {@inheritDoc}
99dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
100dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public long getN() {
101dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return n;
102dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
103dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
104dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
105dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns the minimum of the entries in the specified portion of
106dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * the input array, or <code>Double.NaN</code> if the designated subarray
107dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * is empty.
108dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
109dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Throws <code>IllegalArgumentException</code> if the array is null or
110dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * the array index parameters are not valid.</p>
111dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
112dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <ul>
113dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <li>The result is <code>NaN</code> iff all values are <code>NaN</code>
114dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * (i.e. <code>NaN</code> values have no impact on the value of the statistic).</li>
115dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <li>If any of the values equals <code>Double.NEGATIVE_INFINITY</code>,
116dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * the result is <code>Double.NEGATIVE_INFINITY.</code></li>
117dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * </ul> </p>
118dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
119dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param values the input array
120dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param begin index of the first array element to include
121dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param length the number of elements to include
122dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the minimum of the values or Double.NaN if length = 0
123dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws IllegalArgumentException if the array is null or the array index
124dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *  parameters are not valid
125dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
126dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    @Override
127dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public double evaluate(final double[] values,final int begin, final int length) {
128dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        double min = Double.NaN;
129dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (test(values, begin, length)) {
130dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            min = values[begin];
131dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            for (int i = begin; i < begin + length; i++) {
132dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                if (!Double.isNaN(values[i])) {
133dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    min = (min < values[i]) ? min : values[i];
134dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                }
135dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
136dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
137dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return min;
138dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
139dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
140dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
141dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * {@inheritDoc}
142dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
143dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    @Override
144dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public Min copy() {
145dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        Min result = new Min();
146dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        copy(this, result);
147dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return result;
148dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
149dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
150dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
151dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Copies source to dest.
152dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>Neither source nor dest can be null.</p>
153dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
154dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param source Min to copy
155dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param dest Min to copy to
156dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws NullPointerException if either source or dest is null
157dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
158dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static void copy(Min source, Min dest) {
159dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        dest.setData(source.getDataRef());
160dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        dest.n = source.n;
161dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        dest.value = source.value;
162dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
163dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond}
164