class KMeansUtil implements Serializable {

    public static int updateSumMatrix(FastDistanceVectorData sample,
                                      long sampleWeight,
                                      FastDistanceMatrixData centroids,
                                      int vectorSize,
                                      double[] sumMatrix,
                                      int k,
                                      FastDistance fastDistance,
                                      DenseMatrix distanceMatrix) {
        Preconditions.checkNotNull(sumMatrix);
        Preconditions.checkNotNull(distanceMatrix);
        Preconditions.checkArgument(distanceMatrix.numRows() == centroids.getVectors().numCols() &&
            distanceMatrix.numCols() == 1, "Memory not preallocated!");

        fastDistance.calc(sample, centroids, distanceMatrix);
        int clusterIndex = getClosestClusterIndex(sample, centroids, k, fastDistance, distanceMatrix).f0;
        int startIndex = clusterIndex * (vectorSize + 1);
        Vector vec = sample.getVector();
        if (vec instanceof DenseVector) {
            BLAS.axpy(vectorSize, sampleWeight, ((DenseVector)vec).getData(), 0, sumMatrix, startIndex);
        } else {
            SparseVector sparseVector = (SparseVector)vec;
            sparseVector.forEach((index, value) -> sumMatrix[startIndex + index] += sampleWeight * value);
        }
        sumMatrix[startIndex + vectorSize] += sampleWeight;
        return clusterIndex;
    }

}