public static double normalizeProps ( double [ ] props ) { double propSum = DoubleArrays . sum ( props ) ; if ( propSum == 0 ) { for ( int d = 0 ; d < props . length ; d ++ ) { props [ d ] = 1.0 / ( double ) props . length ; } } else if ( propSum == Double . POSITIVE_INFINITY ) { int count = DoubleArrays . count ( props , Double . POSITIVE_INFINITY ) ; if ( count == 0 ) { throw new RuntimeException ( "Unable to normalize since sum is infinite but contains no infinities: " + Arrays . toString ( props ) ) ; } double constant = 1.0 / ( double ) count ; for ( int d = 0 ; d < props . length ; d ++ ) { if ( props [ d ] == Double . POSITIVE_INFINITY ) { props [ d ] = constant ; } else { props [ d ] = 0.0 ; } } } else { for ( int d = 0 ; d < props . length ; d ++ ) { props [ d ] /= propSum ; assert ( ! Double . isNaN ( props [ d ] ) ) ; } } return propSum ; }
Normalize the proportions and return their sum .