public DoubleVector centerOfMass ( ) { // Handle lazy initialization if ( centroid == null ) { if ( indices . size ( ) == 1 ) centroid = sumVector ; else { // Update the centroid by normalizing by the number of elements. // We expect that the centroid vector might be compared with // other vectors multiple times. If we used a ScaledVector here, // we would be re-doing this multiplication each time, which is // wasted. The centerOfMass is already lazily instantiated, so // we know that if we do the computation here we'll be using the // results at least once. Therefore do the normalization here // once to save cost. int length = sumVector . length ( ) ; double d = 1d / indices . size ( ) ; if ( sumVector instanceof SparseVector ) { centroid = new SparseHashDoubleVector ( length ) ; SparseVector sv = ( SparseVector ) sumVector ; for ( int nz : sv . getNonZeroIndices ( ) ) centroid . set ( nz , sumVector . get ( nz ) * ) ; } else { centroid = new DenseVector ( length ) ; for ( int i = 0 ; i < length ; ++ i ) centroid . set ( i , sumVector . get ( i ) * d ) ; } } } return centroid ; }
Returns the average data point assigned to this candidate cluster