Skip to content

Commit 3609273

Browse files
committed
Replace multiplication with addition
1 parent 2fbf296 commit 3609273

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

lib/node_modules/@stdlib/ml/incr/kmeans/lib/init_kmeansplusplus.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,11 @@ function kmeansplusplus( out, buffer, metric, normalize, trials, seed ) {
195195

196196
// Create a strided array for storing closest centroid results:
197197
dhash = new Array( npts*2 );
198+
ind = 0;
198199
for ( i = 0; i < npts; i++ ) {
199-
dhash[ 2*i ] = PINF; // squared distance
200-
dhash[ (2*i)+1 ] = 0; // index of the closest centroid
200+
dhash[ ind ] = PINF; // squared distance
201+
dhash[ ind+1 ] = 0; // index of the closest centroid
202+
ind += 2; // +stride
201203
}
202204
// Create a scratch array for storing cumulative probabilities:
203205
probs = new Array( npts );
@@ -207,20 +209,23 @@ function kmeansplusplus( out, buffer, metric, normalize, trials, seed ) {
207209
// Note: instead of repeatedly computing centroid distances for each data point, we only need to compute the distances for the most recent centroid and to maintain a hash of closest distance results...
208210
dapply( d2, dist, npts, ndims, buffer, centroids[ j-1 ] );
209211
csum = 0.0; // total cumulative distance
212+
ind = 0;
210213
for ( i = 0; i < npts; i++ ) {
211-
ind = 2 * i;
212214
if ( d2[ i ] < dhash[ ind ] ) {
213215
dhash[ ind ] = d2[ i ];
214216
dhash[ ind+1 ] = j - 1;
215217
csum += d2[ i ];
216218
} else {
217219
csum += dhash[ ind ];
218220
}
221+
ind += 2; // +stride
219222
}
220223
// Compute the cumulative probabilities...
221224
probs[ 0 ] = dhash[ 0 ] / csum;
225+
ind = 2;
222226
for ( i = 1; i < npts; i++ ) {
223-
probs[ i ] = probs[ i-1 ] + ( dhash[ 2*i ] / csum );
227+
probs[ i ] = probs[ i-1 ] + ( dhash[ ind ] / csum );
228+
ind += 2; // +stride
224229
}
225230
// Based Arthur's and Vassilvitskii's paper "kmeans++: The Advantages of Careful Seeding" (see conclusion), randomly select candidate centroids and pick the candidate which minimizes the total squared distance...
226231
bsum = PINF; // best sum
@@ -242,14 +247,15 @@ function kmeansplusplus( out, buffer, metric, normalize, trials, seed ) {
242247
// Compute the sum of squared distances were we to include the candidate centroid...
243248
csum = 0.0;
244249
offsetC = sb1 * c;
250+
ind = 0;
245251
for ( i = 0; i < npts; i++ ) {
246252
d = dist( ndims, buf, 1, sb1*i, buf, 1, offsetC ); // Magic number `1` for stride as matrix is row-major single-segment contiguous
247-
ind = 2 * i;
248253
if ( d < dhash[ ind ] ) {
249254
csum += d;
250255
} else {
251256
csum += dhash[ ind ];
252257
}
258+
ind += 2; // +stride
253259
}
254260
// Determine if the candidate is the best candidate we have seen thus far...
255261
if ( csum < bsum ) {

0 commit comments

Comments
 (0)