Min.java

  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.math4.legacy.stat.descriptive.rank;

  18. import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException;
  19. import org.apache.commons.math4.legacy.exception.NullArgumentException;
  20. import org.apache.commons.math4.legacy.stat.descriptive.AbstractStorelessUnivariateStatistic;
  21. import org.apache.commons.math4.legacy.core.MathArrays;

  22. /**
  23.  * Returns the minimum of the available values.
  24.  * <ul>
  25.  * <li>The result is <code>NaN</code> iff all values are <code>NaN</code>
  26.  * (i.e. <code>NaN</code> values have no impact on the value of the statistic).</li>
  27.  * <li>If any of the values equals <code>Double.NEGATIVE_INFINITY</code>,
  28.  * the result is <code>Double.NEGATIVE_INFINITY.</code></li>
  29.  * </ul>
  30.  * <p>
  31.  * <strong>Note that this implementation is not synchronized.</strong> If
  32.  * multiple threads access an instance of this class concurrently, and at least
  33.  * one of the threads invokes the <code>increment()</code> or
  34.  * <code>clear()</code> method, it must be synchronized externally.</p>
  35.  */
  36. public class Min extends AbstractStorelessUnivariateStatistic {
  37.     /**Number of values that have been added. */
  38.     private long n;

  39.     /**Current value of the statistic. */
  40.     private double value;

  41.     /**
  42.      * Create a Min instance.
  43.      */
  44.     public Min() {
  45.         n = 0;
  46.         value = Double.NaN;
  47.     }

  48.     /**
  49.      * Copy constructor, creates a new {@code Min} identical
  50.      * to the {@code original}.
  51.      *
  52.      * @param original the {@code Min} instance to copy
  53.      * @throws NullArgumentException if original is null
  54.      */
  55.     public Min(Min original) throws NullArgumentException {
  56.         copy(original, this);
  57.     }

  58.     /**
  59.      * {@inheritDoc}
  60.      */
  61.     @Override
  62.     public void increment(final double d) {
  63.         if (d < value || Double.isNaN(value)) {
  64.             value = d;
  65.         }
  66.         n++;
  67.     }

  68.     /**
  69.      * {@inheritDoc}
  70.      */
  71.     @Override
  72.     public void clear() {
  73.         value = Double.NaN;
  74.         n = 0;
  75.     }

  76.     /**
  77.      * {@inheritDoc}
  78.      */
  79.     @Override
  80.     public double getResult() {
  81.         return value;
  82.     }

  83.     /**
  84.      * {@inheritDoc}
  85.      */
  86.     @Override
  87.     public long getN() {
  88.         return n;
  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>MathIllegalArgumentException</code> if the array is null or
  96.      * the array index parameters are not valid.</p>
  97.      * <ul>
  98.      * <li>The result is <code>NaN</code> iff all values are <code>NaN</code>
  99.      * (i.e. <code>NaN</code> values have no impact on the value of the statistic).</li>
  100.      * <li>If any of the values equals <code>Double.NEGATIVE_INFINITY</code>,
  101.      * the result is <code>Double.NEGATIVE_INFINITY.</code></li>
  102.      * </ul>
  103.      *
  104.      * @param values the input array
  105.      * @param begin index of the first array element to include
  106.      * @param length the number of elements to include
  107.      * @return the minimum of the values or Double.NaN if length = 0
  108.      * @throws MathIllegalArgumentException if the array is null or the array index
  109.      *  parameters are not valid
  110.      */
  111.     @Override
  112.     public double evaluate(final double[] values,final int begin, final int length)
  113.         throws MathIllegalArgumentException {

  114.         double min = Double.NaN;
  115.         if (MathArrays.verifyValues(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.     /**
  126.      * {@inheritDoc}
  127.      */
  128.     @Override
  129.     public Min copy() {
  130.         Min result = new Min();
  131.         // No try-catch or advertised exception - args are non-null
  132.         copy(this, result);
  133.         return result;
  134.     }

  135.     /**
  136.      * Copies source to dest.
  137.      * <p>Neither source nor dest can be null.</p>
  138.      *
  139.      * @param source Min to copy
  140.      * @param dest Min to copy to
  141.      * @throws NullArgumentException if either source or dest is null
  142.      */
  143.     public static void copy(Min source, Min dest)
  144.         throws NullArgumentException {
  145.         NullArgumentException.check(source);
  146.         NullArgumentException.check(dest);
  147.         dest.n = source.n;
  148.         dest.value = source.value;
  149.     }
  150. }