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.  *      https://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. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.stat.descriptive.rank;

  22. import java.io.Serializable;

  23. import org.hipparchus.exception.MathIllegalArgumentException;
  24. import org.hipparchus.exception.NullArgumentException;
  25. import org.hipparchus.stat.descriptive.AbstractStorelessUnivariateStatistic;
  26. import org.hipparchus.stat.descriptive.AggregatableStatistic;
  27. import org.hipparchus.util.MathArrays;
  28. import org.hipparchus.util.MathUtils;

  29. /**
  30.  * Returns the minimum of the available values.
  31.  * <ul>
  32.  * <li>The result is <code>NaN</code> iff all values are <code>NaN</code>
  33.  * (i.e. <code>NaN</code> values have no impact on the value of the statistic).</li>
  34.  * <li>If any of the values equals <code>Double.NEGATIVE_INFINITY</code>,
  35.  * the result is <code>Double.NEGATIVE_INFINITY.</code></li>
  36.  * </ul>
  37.  * <p>
  38.  * <strong>Note that this implementation is not synchronized.</strong> If
  39.  * multiple threads access an instance of this class concurrently, and at least
  40.  * one of the threads invokes the <code>increment()</code> or
  41.  * <code>clear()</code> method, it must be synchronized externally.
  42.  */
  43. public class Min extends AbstractStorelessUnivariateStatistic
  44.     implements AggregatableStatistic<Min>, Serializable {

  45.     /** Serializable version identifier */
  46.     private static final long serialVersionUID = 20150412L;

  47.     /** Number of values that have been added */
  48.     private long n;

  49.     /** Current value of the statistic */
  50.     private double value;

  51.     /**
  52.      * Create a Min instance.
  53.      */
  54.     public Min() {
  55.         n = 0;
  56.         value = Double.NaN;
  57.     }

  58.     /**
  59.      * Copy constructor, creates a new {@code Min} identical
  60.      * to the {@code original}.
  61.      *
  62.      * @param original the {@code Min} instance to copy
  63.      * @throws NullArgumentException if original is null
  64.      */
  65.     public Min(Min original) throws NullArgumentException {
  66.         MathUtils.checkNotNull(original);
  67.         this.n     = original.n;
  68.         this.value = original.value;
  69.     }

  70.     /** {@inheritDoc} */
  71.     @Override
  72.     public void increment(final double d) {
  73.         if (d < value || Double.isNaN(value)) {
  74.             value = d;
  75.         }
  76.         n++;
  77.     }

  78.     /** {@inheritDoc} */
  79.     @Override
  80.     public void clear() {
  81.         value = Double.NaN;
  82.         n = 0;
  83.     }

  84.     /** {@inheritDoc} */
  85.     @Override
  86.     public double getResult() {
  87.         return value;
  88.     }

  89.     /** {@inheritDoc} */
  90.     @Override
  91.     public long getN() {
  92.         return n;
  93.     }

  94.     /** {@inheritDoc} */
  95.     @Override
  96.     public void aggregate(Min other) {
  97.         MathUtils.checkNotNull(other);
  98.         if (other.n > 0) {
  99.             if (other.value < this.value || Double.isNaN(this.value)) {
  100.                 this.value = other.value;
  101.             }
  102.             this.n += other.n;
  103.         }
  104.     }

  105.     /**
  106.      * Returns the minimum of the entries in the specified portion of
  107.      * the input array, or <code>Double.NaN</code> if the designated subarray
  108.      * is empty.
  109.      * <p>
  110.      * Throws <code>MathIllegalArgumentException</code> if the array is null or
  111.      * the array index parameters are not valid.
  112.      * </p>
  113.      * <ul>
  114.      * <li>The result is <code>NaN</code> iff all values are <code>NaN</code>
  115.      * (i.e. <code>NaN</code> values have no impact on the value of the statistic).</li>
  116.      * <li>If any of the values equals <code>Double.NEGATIVE_INFINITY</code>,
  117.      * the result is <code>Double.NEGATIVE_INFINITY.</code></li>
  118.      * </ul>
  119.      *
  120.      * @param values the input array
  121.      * @param begin index of the first array element to include
  122.      * @param length the number of elements to include
  123.      * @return the minimum of the values or Double.NaN if length = 0
  124.      * @throws MathIllegalArgumentException if the array is null or the array index
  125.      *  parameters are not valid
  126.      */
  127.     @Override
  128.     public double evaluate(final double[] values,final int begin, final int length)
  129.         throws MathIllegalArgumentException {

  130.         double min = Double.NaN;
  131.         if (MathArrays.verifyValues(values, begin, length)) {
  132.             min = values[begin];
  133.             for (int i = begin; i < begin + length; i++) {
  134.                 if (!Double.isNaN(values[i])) {
  135.                     min = (min < values[i]) ? min : values[i];
  136.                 }
  137.             }
  138.         }
  139.         return min;
  140.     }

  141.     /** {@inheritDoc} */
  142.     @Override
  143.     public Min copy() {
  144.         return new Min(this);
  145.     }

  146. }