public static double [ ] normalizeL1 ( double [ ] vector ) { // compute vector 1-norm double norm1 = 0 ; for ( int i = 0 ; i < vector . length ; i ++ ) { norm1 += Math . abs ( vector [ i ] ) ; } if ( norm1 == 0 ) { Arrays . fill ( vector , 1.0 / vector . length ) ; } else { for ( int i = 0 ; i < vector . length ; i ++ ) { vector [ i ] = vector [ i ] / norm1 ; } } return vector ; }
This method applies L1 normalization on a given array of doubles . The passed vector is modified by the method .