001: /*
002: * Copyright 2003-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.math.stat.descriptive.rank;
017:
018: import org.apache.commons.math.stat.descriptive.AbstractStorelessUnivariateStatistic;
019:
020: /**
021: * Returns the maximum of the available values.
022: * <p>
023: * <ul>
024: * <li>The result is <code>NaN</code> iff all values are <code>NaN</code>
025: * (i.e. <code>NaN</code> values have no impact on the value of the statistic).</li>
026: * <li>If any of the values equals <code>Double.POSITIVE_INFINITY</code>,
027: * the result is <code>Double.POSITIVE_INFINITY.</code></li>
028: * </ul>
029: * <p>
030: * <strong>Note that this implementation is not synchronized.</strong> If
031: * multiple threads access an instance of this class concurrently, and at least
032: * one of the threads invokes the <code>increment()</code> or
033: * <code>clear()</code> method, it must be synchronized externally.
034: *
035: * @version $Revision: 348519 $ $Date: 2005-11-23 12:12:18 -0700 (Wed, 23 Nov 2005) $
036: */
037: public class Max extends AbstractStorelessUnivariateStatistic {
038:
039: /** Serializable version identifier */
040: private static final long serialVersionUID = -5593383832225844641L;
041:
042: /** Number of values that have been added */
043: private long n;
044:
045: /** Current value of the statistic */
046: private double value;
047:
048: /**
049: * Create a Max instance
050: */
051: public Max() {
052: n = 0;
053: value = Double.NaN;
054: }
055:
056: /**
057: * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#increment(double)
058: */
059: public void increment(final double d) {
060: if (d > value || Double.isNaN(value)) {
061: value = d;
062: }
063: n++;
064: }
065:
066: /**
067: * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#clear()
068: */
069: public void clear() {
070: value = Double.NaN;
071: n = 0;
072: }
073:
074: /**
075: * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#getResult()
076: */
077: public double getResult() {
078: return value;
079: }
080:
081: /**
082: * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#getN()
083: */
084: public long getN() {
085: return n;
086: }
087:
088: /**
089: * Returns the maximum of the entries in the specified portion of
090: * the input array, or <code>Double.NaN</code> if the designated subarray
091: * is empty.
092: * <p>
093: * Throws <code>IllegalArgumentException</code> if the array is null or
094: * the array index parameters are not valid.
095: * <p>
096: * <ul>
097: * <li>The result is <code>NaN</code> iff all values are <code>NaN</code>
098: * (i.e. <code>NaN</code> values have no impact on the value of the statistic).</li>
099: * <li>If any of the values equals <code>Double.POSITIVE_INFINITY</code>,
100: * the result is <code>Double.POSITIVE_INFINITY.</code></li>
101: * </ul>
102: *
103: * @param values the input array
104: * @param begin index of the first array element to include
105: * @param length the number of elements to include
106: * @return the maximum of the values or Double.NaN if length = 0
107: * @throws IllegalArgumentException if the array is null or the array index
108: * parameters are not valid
109: */
110: public double evaluate(final double[] values, final int begin,
111: final int length) {
112: double max = Double.NaN;
113: if (test(values, begin, length)) {
114: max = values[begin];
115: for (int i = begin; i < begin + length; i++) {
116: if (!Double.isNaN(values[i])) {
117: max = (max > values[i]) ? max : values[i];
118: }
119: }
120: }
121: return max;
122: }
123: }
|