public static void normalize ( double [ ] a ) { double total = sum ( a ) ; if ( total == 0.0 || Double . isNaN ( total ) ) { throw new RuntimeException ( "Can't normalize an array with sum 0.0 or NaN: " + Arrays . toString ( a ) ) ; } multiplyInPlace ( a , 1.0 / total ) ; // divide each value by total }
Makes the values in this array sum to 1 . 0 . Does it in place . If the total is 0 . 0 or NaN throws an RuntimeException .