* DenseDynamicMagnitudeVector} can be used to represent the difference.
* This vector type is optimized for when many calls to magnitude are
* interleaved with updates to a few dimensions in the vector.
*/
protected static DoubleVector subtract(DoubleVector c, DoubleVector v) {
DoubleVector newCentroid = new DenseDynamicMagnitudeVector(c.length());
// Special case sparse double vectors so that we don't incure a possibly
// log n get operation for each zero value, as that's the common case
// for CompactSparseVector.
if (v instanceof SparseDoubleVector) {
SparseDoubleVector sv = (SparseDoubleVector) v;
int[] nonZeros = sv.getNonZeroIndices();
int sparseIndex = 0;
for (int i = 0; i < c.length(); ++i) {
double value = c.get(i);
if (sparseIndex < nonZeros.length &&
i == nonZeros[sparseIndex])
value -= sv.get(nonZeros[sparseIndex++]);
newCentroid.set(i, value);
}
} else
for (int i = 0; i < c.length(); ++i)
newCentroid.set(i, c.get(i) - v.get(i));
return newCentroid;
}