Here you can find the source of normalize(double[] a)
public static void normalize(double[] a)
//package com.java2s; //License from project: GNU General Public License import java.util.List; public class Main { public static void normalize(double[] a) { double sum = sum(a); if (sum <= 0.0) return; divide(a, sum);// w ww . jav a2s . com sum = sum(a); a[a.length - 1] = 1.0 - (sum - a[a.length - 1]); } public static double[][] normalize(double[][] a) { double sum = sum(a); if (sum <= 0.0) return null; double[][] result = copy(a); divide(result, sum); return result; } public static double sum(List<Double> list) { double sum = 0.0; for (double v : list) { sum += v; } return sum; } public static double sum(double[][] a) { double sum = 0.0; for (int i = 0; i < a.length; i++) { sum += sum(a[i]); } return sum; } public static double sum(double[] a) { double sum = 0.0; for (int i = 0; i < a.length; i++) { sum += a[i]; } return sum; } public static int sum(int[] a) { int sum = 0; for (int i = 0; i < a.length; i++) { sum += a[i]; } return sum; } public static void divide(double[][] a, double v) { for (int i = 0; i < a.length; i++) { divide(a[i], v); } } public static void divide(double[] a, double v) { for (int i = 0; i < a.length; i++) { a[i] /= v; } } private static double[][] copy(double[][] a) { double[][] result = new double[a.length][]; for (int i = 0; i < result.length; i++) { result[i] = copy(a[i]); } return result; } private static double[] copy(double[] a) { double[] result = new double[a.length]; System.arraycopy(a, 0, result, 0, a.length); return result; } }