Java Array Normalize normalize(Number[] array)

Here you can find the source of normalize(Number[] array)

Description

Normalizes the given array (returns a copy), i.e., the array will sum up to 1.

License

Open Source License

Parameter

Parameter Description
array the array to work on

Return

the normalized array

Declaration

public static Double[] normalize(Number[] array) 

Method Source Code

//package com.java2s;
/*//w  ww. ja va2 s.  c om
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   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 General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Normalizes the given array (returns a copy), i.e., the array will sum up
     * to 1. In case of a sum of 0, it returns null.
     *
     * @param array   the array to work on
     * @return      the normalized array
     */
    public static Double[] normalize(Number[] array) {
        Double[] result;
        double sum;
        int i;

        result = new Double[array.length];
        sum = 0;
        for (i = 0; i < array.length; i++)
            sum += array[i].doubleValue();
        if (sum > 0) {
            for (i = 0; i < array.length; i++)
                result[i] = array[i].doubleValue() / sum;
        } else {
            result = null;
        }

        return result;
    }

    /**
     * Normalizes the given array (returns a copy).
     *
     * @param array   the array to work on
     * @return      the std deviation
     */
    public static double[] normalize(int[] array) {
        return toDoubleArray(normalize(toNumberArray(array)));
    }

    /**
     * Normalizes the given array (returns a copy).
     *
     * @param array   the array to work on
     * @return      the std deviation
     */
    public static double[] normalize(double[] array) {
        return toDoubleArray(normalize(toNumberArray(array)));
    }

    /**
     * Turns the Number array into one consisting of primitive doubles.
     *
     * @param array   the array to convert
     * @return      the converted array
     */
    public static double[] toDoubleArray(Number[] array) {
        double[] result;
        int i;

        result = new double[array.length];
        for (i = 0; i < array.length; i++)
            result[i] = array[i].doubleValue();

        return result;
    }

    /**
     * Turns the byte array into a Byte array.
     *
     * @param array   the array to convert
     * @return      the converted array
     */
    public static Number[] toNumberArray(byte[] array) {
        Byte[] result;
        int i;

        result = new Byte[array.length];
        for (i = 0; i < array.length; i++)
            result[i] = new Byte(array[i]);

        return result;
    }

    /**
     * Turns the short array into a Short array.
     *
     * @param array   the array to convert
     * @return      the converted array
     */
    public static Number[] toNumberArray(short[] array) {
        Short[] result;
        int i;

        result = new Short[array.length];
        for (i = 0; i < array.length; i++)
            result[i] = new Short(array[i]);

        return result;
    }

    /**
     * Turns the int array into a Integer array.
     *
     * @param array   the array to convert
     * @return      the converted array
     */
    public static Number[] toNumberArray(int[] array) {
        Integer[] result;
        int i;

        result = new Integer[array.length];
        for (i = 0; i < array.length; i++)
            result[i] = new Integer(array[i]);

        return result;
    }

    /**
     * Turns the long array into a Long array.
     *
     * @param array   the array to convert
     * @return      the converted array
     */
    public static Number[] toNumberArray(long[] array) {
        Long[] result;
        int i;

        result = new Long[array.length];
        for (i = 0; i < array.length; i++)
            result[i] = new Long(array[i]);

        return result;
    }

    /**
     * Turns the float array into a Float array.
     *
     * @param array   the array to convert
     * @return      the converted array
     */
    public static Number[] toNumberArray(float[] array) {
        Float[] result;
        int i;

        result = new Float[array.length];
        for (i = 0; i < array.length; i++)
            result[i] = new Float(array[i]);

        return result;
    }

    /**
     * Turns the double array into a Double array.
     *
     * @param array   the array to convert
     * @return      the converted array
     */
    public static Number[] toNumberArray(double[] array) {
        Double[] result;
        int i;

        result = new Double[array.length];
        for (i = 0; i < array.length; i++)
            result[i] = new Double(array[i]);

        return result;
    }
}

Related

  1. normalize(float[][] vals, float min, float max)
  2. normalize(int[] a)
  3. normalize(int[] values)
  4. normalize(int[][][] data, int startX, int startY, int stopX, int stopY, double scale)
  5. normalize(long[] v, float[] target)
  6. normalize(String[] slist)
  7. normalize01(float[] array)
  8. normalize2(double[] x)
  9. normalize3(float[] result, float[] a)