Here you can find the source of normalize(double[] array)
Parameter | Description |
---|---|
array | a parameter |
public static void normalize(double[] array)
//package com.java2s; //License from project: Open Source License public class Main { /**/*from w w w .jav a 2 s. co m*/ * Normalizes the given array such that the sum of its elements equals 1. * All elements must be non-negative. If all elemnts are zero, then * the given array is assigned a uniform distribution. * @param array */ public static void normalize(double[] array) { double sum = 0; for (double d : array) { if (d < 0) throw new IllegalArgumentException( "Elements cannot be negative."); sum += d; } if (sum == 0) { double value = 1.0 / array.length; for (int i = 0; i < array.length; i++) { array[i] = value; } return; } if (Double.isNaN(sum)) { throw new IllegalArgumentException( "Sum is NaN. Did the array have unknown values?"); } for (int i = 0; i < array.length; i++) { array[i] /= sum; } } }