Skip to content

Commit 2fbf296

Browse files
committed
Replace multiplication with addition and refactor function signature
1 parent 8c1aae5 commit 2fbf296

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,27 @@ var squaredCorrelation = require( './squared_correlation.js' );
4141
* @param {Array} out - output array
4242
* @param {Function} dist - distance function to apply
4343
* @param {PositiveInteger} npts - number of data points
44+
* @param {PositiveInteger} ndims - number of dimensions
4445
* @param {ndarray} matrix - data point matrix
4546
* @param {NonNegativeInteger} ci - centroid row index
4647
* @returns {Array} output array
4748
*/
48-
function dapply( out, dist, npts, matrix, ci ) {
49+
function dapply( out, dist, npts, ndims, matrix, ci ) {
4950
var offsetC;
50-
var stride;
51-
var offset;
51+
var offsetD;
52+
var strideD;
5253
var buf;
53-
var N;
5454
var i;
5555

5656
buf = matrix.data;
57-
N = matrix.shape[ 1 ];
58-
stride = matrix.strides[ 0 ];
59-
offsetC = stride * ci;
57+
58+
strideD = matrix.strides[ 0 ];
59+
offsetC = strideD * ci;
60+
offsetD = 0;
61+
6062
for ( i = 0; i < npts; i++ ) {
61-
offset = stride * i;
62-
out[ i ] = dist( N, buf, 1, offset, buf, 1, offsetC ); // Magic number `1` for stride is based on knowing that the matrix is row-major single-segment contiguous
63+
out[ i ] = dist( ndims, buf, 1, offsetD, buf, 1, offsetC ); // Magic number `1` for stride is based on knowing that the matrix is row-major single-segment contiguous
64+
offsetD += strideD;
6365
}
6466
return out;
6567
}
@@ -203,7 +205,7 @@ function kmeansplusplus( out, buffer, metric, normalize, trials, seed ) {
203205
// 2-5. For each data point, compute the distances to each centroid, find the closest centroid, and, based on the distance to the closest centroid, assign a probability to the data point to be chosen as centroid `c_j`...
204206
for ( j = 1; j < k; j++ ) {
205207
// 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...
206-
dapply( d2, dist, npts, buffer, centroids[ j-1 ] );
208+
dapply( d2, dist, npts, ndims, buffer, centroids[ j-1 ] );
207209
csum = 0.0; // total cumulative distance
208210
for ( i = 0; i < npts; i++ ) {
209211
ind = 2 * i;

0 commit comments

Comments
 (0)