Java Array Normalize normalize(double[] doubles)

Here you can find the source of normalize(double[] doubles)

Description

Normalizes the doubles in the array by their sum.

License

Open Source License

Parameter

Parameter Description
doubles the array of double

Declaration

public static void normalize(double[] doubles) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//from   www. j  a va2s  .  com
     * Normalizes the doubles in the array by their sum.
     *
     * @param doubles the array of double
     * @exception IllegalArgumentException if sum is Zero or NaN
     */
    public static void normalize(double[] doubles) {

        double sum = 0;
        for (int i = 0; i < doubles.length; i++) {
            sum += doubles[i];
        }
        normalize(doubles, sum);
    }

    /**
     * Normalizes the doubles in the array using the given value.
     *
     * @param doubles the array of double
     * @param sum the value by which the doubles are to be normalized
     * @exception IllegalArgumentException if sum is zero or NaN
     */
    public static void normalize(double[] doubles, double sum) {

        if (Double.isNaN(sum)) {
            throw new IllegalArgumentException("Can't normalize array. Sum is NaN.");
        }
        if (sum == 0) {
            // Maybe this should just be a return.
            throw new IllegalArgumentException("Can't normalize array. Sum is zero.");
        }
        for (int i = 0; i < doubles.length; i++) {
            doubles[i] /= sum;
        }
    }
}

Related

  1. normalize(double[] d)
  2. normalize(double[] data)
  3. normalize(double[] data, double floor)
  4. normalize(double[] descriptor)
  5. normalize(double[] doubleArray)
  6. normalize(double[] doubles)
  7. normalize(double[] doubles)
  8. normalize(double[] doubles, double sum)
  9. normalize(double[] doubles, double sum)