Java Double Number Clip clip(double[] values, double min, double max)

Here you can find the source of clip(double[] values, double min, double max)

Description

Ensures that each entry in the specified array has a min value equal to or greater than the specified min and a maximum value less than or equal to the specified max.

License

Open Source License

Parameter

Parameter Description
values the values to clip
min the minimum value
max the maximum value

Declaration

public static double[] clip(double[] values, double min, double max) 

Method Source Code

//package com.java2s;
/* ---------------------------------------------------------------------
 * Numenta Platform for Intelligent Computing (NuPIC)
 * Copyright (C) 2014, Numenta, Inc.  Unless you have an agreement
 * with Numenta, Inc., for a separate license for this software code, the
 * following terms and conditions apply:
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Affero Public License for more details.
 *
 * You should have received a copy of the GNU Affero Public License
 * along with this program.  If not, see http://www.gnu.org/licenses.
 *
 * http://numenta.org/licenses//*  w w  w  . j a  va 2s.c  om*/
 * ---------------------------------------------------------------------
 */

public class Main {
    /**
     * Ensures that each entry in the specified array has a min value
     * equal to or greater than the specified min and a maximum value less
     * than or equal to the specified max.
     *
     * @param values the values to clip
     * @param min    the minimum value
     * @param max    the maximum value
     */
    public static double[] clip(double[] values, double min, double max) {
        for (int i = 0; i < values.length; i++) {
            values[i] = Math.min(1, Math.max(0, values[i]));
        }
        return values;
    }

    /**
     * Ensures that each entry in the specified array has a min value
     * equal to or greater than the min at the specified index and a maximum value less
     * than or equal to the max at the specified index.
     *
     * @param values the values to clip
     * @param min    the minimum value
     * @param max    the maximum value
     */
    public static int[] clip(int[] values, int[] min, int[] max) {
        for (int i = 0; i < values.length; i++) {
            values[i] = Math.max(min[i], Math.min(max[i], values[i]));
        }
        return values;
    }

    /**
     * Ensures that each entry in the specified array has a min value
     * equal to or greater than the min at the specified index and a maximum value less
     * than or equal to the max at the specified index.
     *
     * @param values the values to clip
     * @param max    the minimum value
     * @param adj    the adjustment amount
     */
    public static int[] clip(int[] values, int[] max, int adj) {
        for (int i = 0; i < values.length; i++) {
            values[i] = Math.max(0, Math.min(max[i] + adj, values[i]));
        }
        return values;
    }

    /**
     * Returns the minimum value in the specified array
     * @param array
     * @return
     */
    public static int min(int[] array) {
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < array.length; i++) {
            if (array[i] < min) {
                min = array[i];
            }
        }
        return min;
    }

    /**
     * Returns the minimum value in the specified array
     * @param array
     * @return
     */
    public static double min(double[] array) {
        double min = Double.MAX_VALUE;
        for (int i = 0; i < array.length; i++) {
            if (array[i] < min) {
                min = array[i];
            }
        }
        return min;
    }

    /**
     * Returns the maximum value in the specified array
     * @param array
     * @return
     */
    public static int max(int[] array) {
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < array.length; i++) {
            if (array[i] > max) {
                max = array[i];
            }
        }
        return max;
    }

    /**
     * Returns the maximum value in the specified array
     * @param array
     * @return
     */
    public static double max(double[] array) {
        double max = Double.MIN_VALUE;
        for (int i = 0; i < array.length; i++) {
            if (array[i] > max) {
                max = array[i];
            }
        }
        return max;
    }
}

Related

  1. clip(double v, double min, double max)
  2. clip(double value, double min, double max)
  3. clip(double value, double min, double max)
  4. clip(final double n, final double minValue, final double maxValue)
  5. clip(final double value, final double min, final double max)
  6. clip2(double n, double d, double[] t)
  7. clipDecimals(double num, int n)