Skip to content

Commit a7c3c3b

Browse files
refactor: updated implementation according to code review
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 6273473 commit a7c3c3b

3 files changed

Lines changed: 271 additions & 71 deletions

File tree

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
24+
// MAIN //
25+
26+
/**
27+
* Find closest centroid.
28+
* @private
29+
* @param {Function} dist - distance method.
30+
* @param {PositiveInteger} N - number of features.
31+
* @param {PositiveInteger} k - number of clusters.
32+
* @param {Float64Array} X - input strided matrix.
33+
* @param {integer} sx - stride length of X.
34+
* @param {integer} ox - starting index of X.
35+
* @param {Float64Array} c - strided array centroid locations.
36+
* @param {integer} sc1 - stride of the third dimension.
37+
* @param {integer} sc2 - stride of second dimension.
38+
* @param {integer} oc - initial index of centroids.
39+
* @returns {NonNegativeInteger} closest centroid.
40+
*/
41+
function closestCentroid( dist, N, k, X, sx, ox, c, sc1, sc2, oc ) {
42+
var bestDist;
43+
var best;
44+
var d;
45+
var i;
46+
47+
best = 0;
48+
bestDist = dist( N, X, sx, ox, c, sc2, oc );
49+
50+
oc += sc1; // move to the next centroid
51+
for ( i = 1; i < k; i++ ) {
52+
d = dist( N, X, sx, ox, c, sc2, oc );
53+
if ( d < bestDist ) {
54+
bestDist = d;
55+
best = i;
56+
}
57+
oc += sc1;
58+
}
59+
return best;
60+
}
61+
62+
module.exports = closestCentroid;
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var drss = require( '@stdlib/blas/ext/base/drss' ).ndarray;
24+
25+
26+
// MAIN //
27+
28+
/**
29+
* Compute inertia.
30+
* @private
31+
* @param {PositiveInteger} M - number of samples.
32+
* @param {PositiveInteger} N - number of features.
33+
* @param {Float64Array} X - input strided matrix.
34+
* @param {integer} strideX1 - stride length of first dimension of X.
35+
* @param {integer} strideX2 - stride length of second dimension of X.
36+
* @param {integer} offsetX - starting index of X.
37+
* @param {Float64Array} centroids - strided array centroid locations.
38+
* @param {integer} strideC1 - stride length of first dimension of c.
39+
* @param {integer} strideC2 - stride length of second dimension of c.
40+
* @param {integer} offsetC - initial index of centroids.
41+
* @param {Int32Array} labels - labels array.
42+
* @returns {number} inertia.
43+
*/
44+
function computeInertia( M, N, X, strideX1, strideX2, offsetX, centroids, strideC1, strideC2, offsetC, labels ) { // eslint-disable-line max-len, max-params
45+
var inertia;
46+
var ox;
47+
var oc;
48+
var d;
49+
var c;
50+
var i;
51+
52+
inertia = 0.0;
53+
ox = offsetX;
54+
for ( i = 0; i < M; i++ ) {
55+
c = labels[ i ];
56+
oc = offsetC + ( c * strideC1 );
57+
d = drss( N, X, strideX2, ox, centroids, strideC2, oc );
58+
inertia += d;
59+
ox += strideX1;
60+
}
61+
return inertia;
62+
}
63+
64+
module.exports = computeInertia;

0 commit comments

Comments
 (0)