001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.math4.legacy.stat.descriptive.rank; 018 019import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException; 020import org.apache.commons.math4.legacy.exception.NullArgumentException; 021import org.apache.commons.math4.legacy.stat.descriptive.AbstractStorelessUnivariateStatistic; 022import org.apache.commons.math4.legacy.core.MathArrays; 023 024/** 025 * Returns the maximum of the available values. 026 * <ul> 027 * <li>The result is <code>NaN</code> iff all values are <code>NaN</code> 028 * (i.e. <code>NaN</code> values have no impact on the value of the statistic).</li> 029 * <li>If any of the values equals <code>Double.POSITIVE_INFINITY</code>, 030 * the result is <code>Double.POSITIVE_INFINITY.</code></li> 031 * </ul> 032* <p> 033 * <strong>Note that this implementation is not synchronized.</strong> If 034 * multiple threads access an instance of this class concurrently, and at least 035 * one of the threads invokes the <code>increment()</code> or 036 * <code>clear()</code> method, it must be synchronized externally.</p> 037 */ 038public class Max extends AbstractStorelessUnivariateStatistic { 039 /** Number of values that have been added. */ 040 private long n; 041 042 /** Current value of the statistic. */ 043 private double value; 044 045 /** 046 * Create a Max instance. 047 */ 048 public Max() { 049 n = 0; 050 value = Double.NaN; 051 } 052 053 /** 054 * Copy constructor, creates a new {@code Max} identical 055 * to the {@code original}. 056 * 057 * @param original the {@code Max} instance to copy 058 * @throws NullArgumentException if original is null 059 */ 060 public Max(Max original) throws NullArgumentException { 061 copy(original, this); 062 } 063 064 /** 065 * {@inheritDoc} 066 */ 067 @Override 068 public void increment(final double d) { 069 if (d > value || Double.isNaN(value)) { 070 value = d; 071 } 072 n++; 073 } 074 075 /** 076 * {@inheritDoc} 077 */ 078 @Override 079 public void clear() { 080 value = Double.NaN; 081 n = 0; 082 } 083 084 /** 085 * {@inheritDoc} 086 */ 087 @Override 088 public double getResult() { 089 return value; 090 } 091 092 /** 093 * {@inheritDoc} 094 */ 095 @Override 096 public long getN() { 097 return n; 098 } 099 100 /** 101 * Returns the maximum of the entries in the specified portion of 102 * the input array, or <code>Double.NaN</code> if the designated subarray 103 * is empty. 104 * <p> 105 * Throws <code>MathIllegalArgumentException</code> if the array is null or 106 * the array index parameters are not valid.</p> 107 * <ul> 108 * <li>The result is <code>NaN</code> iff all values are <code>NaN</code> 109 * (i.e. <code>NaN</code> values have no impact on the value of the statistic).</li> 110 * <li>If any of the values equals <code>Double.POSITIVE_INFINITY</code>, 111 * the result is <code>Double.POSITIVE_INFINITY.</code></li> 112 * </ul> 113 * 114 * @param values the input array 115 * @param begin index of the first array element to include 116 * @param length the number of elements to include 117 * @return the maximum of the values or Double.NaN if length = 0 118 * @throws MathIllegalArgumentException if the array is null or the array index 119 * parameters are not valid 120 */ 121 @Override 122 public double evaluate(final double[] values, final int begin, final int length) 123 throws MathIllegalArgumentException { 124 125 double max = Double.NaN; 126 if (MathArrays.verifyValues(values, begin, length)) { 127 max = values[begin]; 128 for (int i = begin; i < begin + length; i++) { 129 if (!Double.isNaN(values[i])) { 130 max = (max > values[i]) ? max : values[i]; 131 } 132 } 133 } 134 return max; 135 } 136 137 /** 138 * {@inheritDoc} 139 */ 140 @Override 141 public Max copy() { 142 Max result = new Max(); 143 // No try-catch or advertised exception because args are non-null 144 copy(this, result); 145 return result; 146 } 147 148 /** 149 * Copies source to dest. 150 * <p>Neither source nor dest can be null.</p> 151 * 152 * @param source Max to copy 153 * @param dest Max to copy to 154 * @throws NullArgumentException if either source or dest is null 155 */ 156 public static void copy(Max source, Max dest) 157 throws NullArgumentException { 158 NullArgumentException.check(source); 159 NullArgumentException.check(dest); 160 dest.n = source.n; 161 dest.value = source.value; 162 } 163}