diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/README.md b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/README.md new file mode 100644 index 000000000000..4021add40240 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/README.md @@ -0,0 +1,465 @@ + + +# dclosestCentroids + +> Assign each data point in a double-precision floating-point input matrix to its closest centroid. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var dclosestCentroids = require( '@stdlib/ml/strided/dkmeans-closest-centroids' ); +``` + + + +#### dclosestCentroids( order, M, N, k, metric, X, LDX, C, LDC, out, so, counts, sco ) + + + +Assigns each data point in a double-precision floating-point input matrix to its closest centroid. + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var Int32Array = require( '@stdlib/array/int32' ); + +/* + X = [ + [ 1.0, 1.0 ], + [ 5.0, 5.0 ], + [ 1.5, 1.5 ] + ] +*/ +var X = new Float64Array( [ 1.0, 1.0, 5.0, 5.0, 1.5, 1.5 ] ); + +/* + C = [ + [ 1.0, 1.0 ], + [ 5.0, 5.0 ] + ] +*/ +var C = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + +var out = new Int32Array( 3 ); +var counts = new Int32Array( 2 ); + +dclosestCentroids( 'row-major', 3, 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1, counts, 1 ); +// out => [ 0, 1, 0 ] +``` + +The function has the following parameters: + +- **order**: storage layout. +- **M**: number of data points. +- **N**: number of features. +- **k**: number of centroids. +- **metric**: distance [metric][@stdlib/ml/base/kmeans/metrics] used to compute the distance between a data point and a centroid. Must be one of `'sqeuclidean'`, `'cosine'`, `'cityblock'`, or `'correlation'`. +- **X**: input data matrix stored as a [`Float64Array`][mdn-float64array]. +- **LDX**: stride of the first dimension of `X` (a.k.a., leading dimension of the matrix `X`). +- **C**: centroid matrix stored as a [`Float64Array`][mdn-float64array]. +- **LDC**: stride of the first dimension of `C` (a.k.a., leading dimension of the matrix `C`). +- **out**: output array stored as an [`Int32Array`][mdn-int32array] for storing the index of the closest centroid for each data point. +- **so**: stride length of `out`. +- **counts**: output array stored as an [`Int32Array`][mdn-int32array] for storing the number of data points assigned to each centroid. Should be initialized to zeros prior to invocation. +- **sco**: stride length of `counts`. + +The function **mutates** `out` and `counts` in-place and returns `out`. + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var Int32Array = require( '@stdlib/array/int32' ); + +/* + X = [ + [ 1.0, 1.0 ], + [ 5.0, 5.0 ], + [ 1.5, 1.5 ] + ] +*/ +var X = new Float64Array( [ 1.0, 5.0, 1.5, 1.0, 5.0, 1.5 ] ); +var C = new Float64Array( [ 1.0, 5.0, 1.0, 5.0 ] ); + +var out = new Int32Array( 3 ); +var counts = new Int32Array( 2 ); + +dclosestCentroids( 'column-major', 3, 2, 2, 'sqeuclidean', X, 3, C, 2, out, 1, counts, 1 ); +// out => [ 0, 1, 0 ] +``` + + + +#### dclosestCentroids.ndarray( M, N, k, metric, X, sx1, sx2, ox, C, sc1, sc2, oc, out, so, oo, counts, sco, ocounts ) + + + +Assigns each data point in a double-precision floating-point input matrix to its closest centroid using alternative indexing semantics. + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var Int32Array = require( '@stdlib/array/int32' ); + +var X = new Float64Array( [ 1.0, 1.0, 5.0, 5.0, 1.5, 1.5 ] ); +var C = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + +var out = new Int32Array( 3 ); +var counts = new Int32Array( 2 ); + +dclosestCentroids.ndarray( 3, 2, 2, 'sqeuclidean', X, 2, 1, 0, C, 2, 1, 0, out, 1, 0, counts, 1, 0 ); +// out => [ 0, 1, 0 ] +``` + +The function has the following parameters: + +- **M**: number of data points. +- **N**: number of features. +- **k**: number of centroids. +- **metric**: distance [metric][@stdlib/ml/base/kmeans/metrics]. +- **X**: input data matrix stored as a [`Float64Array`][mdn-float64array]. +- **sx1**: stride of the first dimension of `X`. +- **sx2**: stride of the second dimension of `X`. +- **ox**: starting index for `X`. +- **C**: centroid matrix stored as a [`Float64Array`][mdn-float64array]. +- **sc1**: stride of the first dimension of `C`. +- **sc2**: stride of the second dimension of `C`. +- **oc**: starting index for `C`. +- **out**: output array stored as an [`Int32Array`][mdn-int32array]. +- **so**: stride length of `out`. +- **oo**: starting index for `out`. +- **counts**: output array stored as an [`Int32Array`][mdn-int32array]. Should be initialized to zeros prior to invocation. +- **sco**: stride length of `counts`. +- **ocounts**: starting index for `counts`. + +While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameters support indexing semantics based on starting indices. + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var Int32Array = require( '@stdlib/array/int32' ); + +var X = new Float64Array( [ 1.0, 1.0, 5.0, 5.0, 1.5, 1.5 ] ); +var C = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + +var out = new Int32Array( 3 ); +var counts = new Int32Array( 2 ); + +// Traverse the data points in reverse order: +dclosestCentroids.ndarray( 3, 2, 2, 'sqeuclidean', X, -2, 1, 4, C, 2, 1, 0, out, -1, 2, counts, 1, 0 ); +// out => [ 0, 1, 0 ] +``` + +
+ + + + + +
+ +## Notes + +- For each data point, the function writes the index of the closest centroid to the output array `out` and increments the corresponding element of the `counts` array. Accordingly, the `counts` array should be initialized to zeros prior to invocation. +- If `M`, `N`, or `k` is less than `1`, both functions return `out` unchanged. + +
+ + + + + +
+ +## Examples + + + + + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var Int32Array = require( '@stdlib/array/int32' ); +var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); +var dclosestCentroids = require( '@stdlib/ml/strided/dkmeans-closest-centroids' ); + +var order = 'row-major'; + +var shapeX = [ 4, 2 ]; +var stridesX = shape2strides( shapeX, order ); +var X = new Float64Array( [ 1.0, 1.0, 5.0, 5.0, 1.5, 1.5, 4.5, 5.5 ] ); +console.log( ndarray2array( X, shapeX, stridesX, 0, order ) ); + +var shapeC = [ 2, 2 ]; +var stridesC = shape2strides( shapeC, order ); +var C = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); +console.log( ndarray2array( C, shapeC, stridesC, 0, order ) ); + +var out = new Int32Array( shapeX[ 0 ] ); +var counts = new Int32Array( shapeC[ 0 ] ); + +dclosestCentroids( order, shapeX[ 0 ], shapeX[ 1 ], shapeC[ 0 ], 'sqeuclidean', X, stridesX[ 0 ], C, stridesC[ 0 ], out, 1, counts, 1 ); + +console.log( 'out = %s', out.toString() ); +console.log( 'counts = %s', counts.toString() ); +``` + +
+ + + + + +* * * + +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### Usage + +```c +#include "stdlib/ml/strided/dkmeans_closest_centroids.h" +``` + + + +#### stdlib_strided_dclosest_centroids( order, M, N, k, metric, \*X, LDX, \*C, LDC, \*out, so, \*counts, sco ) + + + +Assigns each data point in a double-precision floating-point input matrix to its closest centroid. + +```c +#include "stdlib/blas/base/shared.h" +#include "stdlib/ml/base/kmeans/metrics.h" +#include + +const double X[] = { 1.0, 1.0, 5.0, 5.0, 1.5, 1.5 }; +const double C[] = { 1.0, 1.0, 5.0, 5.0 }; + +int32_t out[ 3 ]; +int32_t counts[] = { 0, 0 }; + +stdlib_strided_dclosest_centroids( CblasRowMajor, 3, 2, 2, STDLIB_ML_KMEANS_SQEUCLIDEAN, X, 2, C, 2, out, 1, counts, 1 ); +// out => { 0, 1, 0 } +``` + +The function accepts the following arguments: + +- **order**: `[in] CBLAS_LAYOUT` storage layout. +- **M**: `[in] CBLAS_INT` number of data points. +- **N**: `[in] CBLAS_INT` number of features. +- **k**: `[in] CBLAS_INT` number of centroids. +- **metric**: `[in] enum STDLIB_ML_KMEANS_METRIC` distance metric. +- **X**: `[in] double*` input data matrix. +- **LDX**: `[in] CBLAS_INT` stride of the first dimension of `X` (a.k.a., leading dimension of the matrix `X`). +- **C**: `[in] double*` centroid matrix. +- **LDC**: `[in] CBLAS_INT` stride of the first dimension of `C` (a.k.a., leading dimension of the matrix `C`). +- **out**: `[out] int32_t*` output array for closest centroid indices. +- **so**: `[in] CBLAS_INT` stride length for `out`. +- **counts**: `[inout] int32_t*` output array for per-centroid assignment counts. +- **sco**: `[in] CBLAS_INT` stride length for `counts`. + +```c +int32_t * stdlib_strided_dclosest_centroids( const CBLAS_LAYOUT order, const CBLAS_INT M, const CBLAS_INT N, const CBLAS_INT k, const enum STDLIB_ML_KMEANS_METRIC metric, const double *X, const CBLAS_INT LDX, const double *C, const CBLAS_INT LDC, int32_t *out, const CBLAS_INT so, int32_t *counts, const CBLAS_INT sco ); +``` + + + +#### stdlib_strided_dclosest_centroids_ndarray( M, N, k, metric, \*X, sx1, sx2, ox, \*C, sc1, sc2, oc, \*out, so, oo, \*counts, sco, oco ) + + + +Assigns each data point in a double-precision floating-point input matrix to its closest centroid using alternative indexing semantics. + +```c +#include "stdlib/ml/base/kmeans/metrics.h" +#include + +const double X[] = { 1.0, 1.0, 5.0, 5.0, 1.5, 1.5 }; +const double C[] = { 1.0, 1.0, 5.0, 5.0 }; + +int32_t out[ 3 ]; +int32_t counts[] = { 0, 0 }; + +stdlib_strided_dclosest_centroids_ndarray( 3, 2, 2, STDLIB_ML_KMEANS_SQEUCLIDEAN, X, 2, 1, 0, C, 2, 1, 0, out, 1, 0, counts, 1, 0 ); +// out => { 0, 1, 0 } +``` + +The function accepts the following arguments: + +- **M**: `[in] CBLAS_INT` number of data points. +- **N**: `[in] CBLAS_INT` number of features. +- **k**: `[in] CBLAS_INT` number of centroids. +- **metric**: `[in] enum STDLIB_ML_KMEANS_METRIC` distance metric. +- **X**: `[in] double*` input data matrix. +- **sx1**: `[in] CBLAS_INT` stride of the first dimension of `X`. +- **sx2**: `[in] CBLAS_INT` stride of the second dimension of `X`. +- **ox**: `[in] CBLAS_INT` index offset for `X`. +- **C**: `[in] double*` centroid matrix. +- **sc1**: `[in] CBLAS_INT` stride of the first dimension of `C`. +- **sc2**: `[in] CBLAS_INT` stride of the second dimension of `C`. +- **oc**: `[in] CBLAS_INT` index offset for `C`. +- **out**: `[out] int32_t*` output array for closest centroid indices. +- **so**: `[in] CBLAS_INT` stride length for `out`. +- **oo**: `[in] CBLAS_INT` index offset for `out`. +- **counts**: `[inout] int32_t*` output array for per-centroid assignment counts. +- **sco**: `[in] CBLAS_INT` stride length for `counts`. +- **oco**: `[in] CBLAS_INT` index offset for `counts`. + +```c +int32_t * stdlib_strided_dclosest_centroids_ndarray( const CBLAS_INT M, const CBLAS_INT N, const CBLAS_INT k, const enum STDLIB_ML_KMEANS_METRIC metric, const double *X, const CBLAS_INT sx1, const CBLAS_INT sx2, const CBLAS_INT ox, const double *C, const CBLAS_INT sc1, const CBLAS_INT sc2, const CBLAS_INT oc, int32_t *out, const CBLAS_INT so, const CBLAS_INT oo, int32_t *counts, const CBLAS_INT sco, const CBLAS_INT oco ); +``` + +
+ + + + + +
+ +
+ + + + + +
+ +### Examples + +```c +#include "stdlib/ml/strided/dkmeans_closest_centroids.h" +#include "stdlib/ml/base/kmeans/metrics.h" +#include "stdlib/blas/base/shared.h" +#include +#include + +int main( void ) { + // Define a collection of data points (row-major): + const double X[ 3*3 ] = { + 1.0, 2.0, 3.0, + 4.0, 5.0, 6.0, + 7.0, 8.0, 9.0 + }; + + // Define a collection of centroids (row-major): + const double C[] = { 1.0, 1.0, 5.0, 5.0, 2.0, 2.0 }; + + // Specify the number of data points, features, and centroids: + const int M = 3; + const int N = 3; + const int k = 2; + + // Allocate output arrays: + int32_t out[ 3 ]; + int32_t counts[] = { 0, 0 }; + + // Assign each data point to its closest centroid: + stdlib_strided_dclosest_centroids( CblasRowMajor, M, N, k, STDLIB_ML_KMEANS_SQEUCLIDEAN, X, N, C, N, out, 1, counts, 1 ); + + // Print the results: + int i; + for ( i = 0; i < M; i++ ) { + printf( "out[ %i ] = %i\n", i, out[ i ] ); + } + for ( i = 0; i < k; i++ ) { + printf( "counts[ %i ] = %i\n", i, counts[ i ] ); + } + + for ( i = 0; i < k; i++ ) { + counts[ i ] = 0; + } + + // Assign each data point to its closest centroid using alternative indexing semantics: + stdlib_strided_dclosest_centroids_ndarray( M, N, k, STDLIB_ML_KMEANS_SQEUCLIDEAN, X, N, 1, 0, C, N, 1, 0, out, 1, 0, counts, 1, 0 ); + + // Print the results: + for ( i = 0; i < M; i++ ) { + printf( "out[ %i ] = %i\n", i, out[ i ] ); + } + for ( i = 0; i < k; i++ ) { + printf( "counts[ %i ] = %i\n", i, counts[ i ] ); + } +} +``` + +
+ + + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/benchmark/benchmark.js b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/benchmark/benchmark.js new file mode 100644 index 000000000000..c96744d120d6 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/benchmark/benchmark.js @@ -0,0 +1,134 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var zeros = require( '@stdlib/array/zeros' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var dclosestCentroids = require( './../lib/dkmeans_closest_centroids.js' ); + + +// VARIABLES // + +var LAYOUTS = [ + 'row-major', + 'column-major' +]; +var K = 4; // number of centroids +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {string} order - storage layout +* @param {PositiveInteger} N - number of data points and features along each dimension +* @returns {Function} benchmark function +*/ +function createBenchmark( order, N ) { + var counts; + var LDX; + var LDC; + var out; + var X; + var C; + + X = uniform( N*N, -10000.0, 10000.0, options ); + C = uniform( K*N, -10000.0, 10000.0, options ); + out = zeros( N, 'int32' ); + counts = zeros( K, 'int32' ); + + if ( order === 'column-major' ) { + LDX = N; + LDC = K; + } else { + LDX = N; + LDC = N; + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var z; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + z = dclosestCentroids( order, N, N, K, 'sqeuclidean', X, LDX, C, LDC, out, 1, counts, 1 ); + if ( isnan( z[ i%z.length ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( z[ i%z.length ] ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var min; + var max; + var ord; + var N; + var f; + var i; + var k; + + min = 1; // 10^min + max = 6; // 10^max + + for ( k = 0; k < LAYOUTS.length; k++ ) { + ord = LAYOUTS[ k ]; + for ( i = min; i <= max; i++ ) { + N = floor( pow( pow( 10, i ), 1.0/2.0 ) ); + f = createBenchmark( ord, N ); + bench( format( '%s:order=%s,size=%d', pkg, ord, N*N ), f ); + } + } +} + +main(); diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/benchmark/benchmark.native.js b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/benchmark/benchmark.native.js new file mode 100644 index 000000000000..aa77bc5c6a43 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/benchmark/benchmark.native.js @@ -0,0 +1,139 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var bench = require( '@stdlib/bench' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var zeros = require( '@stdlib/array/zeros' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var tryRequire = require( '@stdlib/utils/try-require' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; + + +// VARIABLES // + +var dclosestCentroids = tryRequire( resolve( __dirname, './../lib/dkmeans_closest_centroids.native.js' ) ); +var opts = { + 'skip': ( dclosestCentroids instanceof Error ) +}; +var LAYOUTS = [ + 'row-major', + 'column-major' +]; +var K = 4; // number of centroids +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {string} order - storage layout +* @param {PositiveInteger} N - number of data points and features along each dimension +* @returns {Function} benchmark function +*/ +function createBenchmark( order, N ) { + var counts; + var LDX; + var LDC; + var out; + var X; + var C; + + X = uniform( N*N, -10000.0, 10000.0, options ); + C = uniform( K*N, -10000.0, 10000.0, options ); + out = zeros( N, 'int32' ); + counts = zeros( K, 'int32' ); + + if ( order === 'column-major' ) { + LDX = N; + LDC = K; + } else { + LDX = N; + LDC = N; + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var z; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + z = dclosestCentroids( order, N, N, K, 'sqeuclidean', X, LDX, C, LDC, out, 1, counts, 1 ); + if ( isnan( z[ i%z.length ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( z[ i%z.length ] ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var min; + var max; + var ord; + var N; + var f; + var i; + var k; + + min = 1; // 10^min + max = 6; // 10^max + + for ( k = 0; k < LAYOUTS.length; k++ ) { + ord = LAYOUTS[ k ]; + for ( i = min; i <= max; i++ ) { + N = floor( pow( pow( 10, i ), 1.0/2.0 ) ); + f = createBenchmark( ord, N ); + bench( format( '%s::native:order=%s,size=%d', pkg, ord, N*N ), opts, f ); + } + } +} + +main(); diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/benchmark/benchmark.ndarray.js new file mode 100644 index 000000000000..7e51c34bdb5f --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/benchmark/benchmark.ndarray.js @@ -0,0 +1,141 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var zeros = require( '@stdlib/array/zeros' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var isColumnMajor = require( '@stdlib/ndarray/base/assert/is-column-major-string' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var dclosestCentroids = require( './../lib/ndarray.js' ); + + +// VARIABLES // + +var LAYOUTS = [ + 'row-major', + 'column-major' +]; +var K = 4; // number of centroids +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {string} order - storage layout +* @param {PositiveInteger} N - number of data points and features along each dimension +* @returns {Function} benchmark function +*/ +function createBenchmark( order, N ) { + var counts; + var out; + var sx1; + var sx2; + var sc1; + var sc2; + var X; + var C; + + X = uniform( N*N, -10000.0, 10000.0, options ); + C = uniform( K*N, -10000.0, 10000.0, options ); + out = zeros( N, 'int32' ); + counts = zeros( K, 'int32' ); + + if ( isColumnMajor( order ) ) { + sx1 = 1; + sx2 = N; + sc1 = 1; + sc2 = K; + } else { // order === 'row-major' + sx1 = N; + sx2 = 1; + sc1 = N; + sc2 = 1; + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var z; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + z = dclosestCentroids( N, N, K, 'sqeuclidean', X, sx1, sx2, 0, C, sc1, sc2, 0, out, 1, 0, counts, 1, 0 ); + if ( isnan( z[ i%z.length ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( z[ i%z.length ] ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var min; + var max; + var ord; + var N; + var f; + var i; + var k; + + min = 1; // 10^min + max = 6; // 10^max + + for ( k = 0; k < LAYOUTS.length; k++ ) { + ord = LAYOUTS[ k ]; + for ( i = min; i <= max; i++ ) { + N = floor( pow( pow( 10, i ), 1.0/2.0 ) ); + f = createBenchmark( ord, N ); + bench( format( '%s:ndarray:order=%s,size=%d', pkg, ord, N*N ), f ); + } + } +} + +main(); diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/benchmark/benchmark.ndarray.native.js b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/benchmark/benchmark.ndarray.native.js new file mode 100644 index 000000000000..7fded2a5318f --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/benchmark/benchmark.ndarray.native.js @@ -0,0 +1,146 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var bench = require( '@stdlib/bench' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var zeros = require( '@stdlib/array/zeros' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var isColumnMajor = require( '@stdlib/ndarray/base/assert/is-column-major-string' ); +var tryRequire = require( '@stdlib/utils/try-require' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; + + +// VARIABLES // + +var dclosestCentroids = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) ); +var opts = { + 'skip': ( dclosestCentroids instanceof Error ) +}; +var LAYOUTS = [ + 'row-major', + 'column-major' +]; +var K = 4; // number of centroids +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {string} order - storage layout +* @param {PositiveInteger} N - number of data points and features along each dimension +* @returns {Function} benchmark function +*/ +function createBenchmark( order, N ) { + var counts; + var out; + var sx1; + var sx2; + var sc1; + var sc2; + var X; + var C; + + X = uniform( N*N, -10000.0, 10000.0, options ); + C = uniform( K*N, -10000.0, 10000.0, options ); + out = zeros( N, 'int32' ); + counts = zeros( K, 'int32' ); + + if ( isColumnMajor( order ) ) { + sx1 = 1; + sx2 = N; + sc1 = 1; + sc2 = K; + } else { // order === 'row-major' + sx1 = N; + sx2 = 1; + sc1 = N; + sc2 = 1; + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var z; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + z = dclosestCentroids( N, N, K, 'sqeuclidean', X, sx1, sx2, 0, C, sc1, sc2, 0, out, 1, 0, counts, 1, 0 ); + if ( isnan( z[ i%z.length ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( z[ i%z.length ] ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var min; + var max; + var ord; + var N; + var f; + var i; + var k; + + min = 1; // 10^min + max = 6; // 10^max + + for ( k = 0; k < LAYOUTS.length; k++ ) { + ord = LAYOUTS[ k ]; + for ( i = min; i <= max; i++ ) { + N = floor( pow( pow( 10, i ), 1.0/2.0 ) ); + f = createBenchmark( ord, N ); + bench( format( '%s::native:ndarray:order=%s,size=%d', pkg, ord, N*N ), opts, f ); + } + } +} + +main(); diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/benchmark/c/Makefile b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/benchmark/c/Makefile new file mode 100644 index 000000000000..0756dc7da20a --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/benchmark/c/Makefile @@ -0,0 +1,146 @@ +#/ +# @license Apache-2.0 +# +# Copyright (c) 2026 The Stdlib Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +#/ + +# VARIABLES # + +ifndef VERBOSE + QUIET := @ +else + QUIET := +endif + +# Determine the OS ([1][1], [2][2]). +# +# [1]: https://en.wikipedia.org/wiki/Uname#Examples +# [2]: http://stackoverflow.com/a/27776822/2225624 +OS ?= $(shell uname) +ifneq (, $(findstring MINGW,$(OS))) + OS := WINNT +else +ifneq (, $(findstring MSYS,$(OS))) + OS := WINNT +else +ifneq (, $(findstring CYGWIN,$(OS))) + OS := WINNT +else +ifneq (, $(findstring Windows_NT,$(OS))) + OS := WINNT +endif +endif +endif +endif + +# Define the program used for compiling C source files: +ifdef C_COMPILER + CC := $(C_COMPILER) +else + CC := gcc +endif + +# Define the command-line options when compiling C files: +CFLAGS ?= \ + -std=c99 \ + -O3 \ + -Wall \ + -pedantic + +# Determine whether to generate position independent code ([1][1], [2][2]). +# +# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options +# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option +ifeq ($(OS), WINNT) + fPIC ?= +else + fPIC ?= -fPIC +endif + +# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): +INCLUDE ?= + +# List of source files: +SOURCE_FILES ?= + +# List of libraries (e.g., `-lopenblas -lpthread`): +LIBRARIES ?= + +# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): +LIBPATH ?= + +# List of C targets: +c_targets := benchmark.length.out + + +# RULES # + +#/ +# Compiles source files. +# +# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`) +# @param {string} [CFLAGS] - C compiler options +# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`) +# @param {string} [SOURCE_FILES] - list of source files +# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) +# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas -lpthread`) +# +# @example +# make +# +# @example +# make all +#/ +all: $(c_targets) + +.PHONY: all + +#/ +# Compiles C source files. +# +# @private +# @param {string} CC - C compiler (e.g., `gcc`) +# @param {string} CFLAGS - C compiler options +# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar`) +# @param {string} SOURCE_FILES - list of source files +# @param {string} LIBPATH - list of library paths (e.g., `-L /foo/bar`) +# @param {string} LIBRARIES - list of libraries (e.g., `-lopenblas`) +#/ +$(c_targets): %.out: %.c + $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) + +#/ +# Runs compiled benchmarks. +# +# @example +# make run +#/ +run: $(c_targets) + $(QUIET) ./$< + +.PHONY: run + +#/ +# Removes generated files. +# +# @example +# make clean +#/ +clean: + $(QUIET) -rm -f *.o *.out + +.PHONY: clean diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/benchmark/c/benchmark.length.c b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/benchmark/c/benchmark.length.c new file mode 100644 index 000000000000..4db30fccecba --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/benchmark/c/benchmark.length.c @@ -0,0 +1,225 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "stdlib/ml/strided/dkmeans_closest_centroids.h" +#include "stdlib/ml/base/kmeans/metrics.h" +#include "stdlib/blas/base/shared.h" +#include +#include +#include +#include +#include +#include + +#define NAME "dclosest_centroids" +#define ITERATIONS 10000000 +#define REPEATS 3 +#define MIN 1 +#define MAX 6 +#define K 4 + +/** +* Prints the TAP version. +*/ +static void print_version( void ) { + printf( "TAP version 13\n" ); +} + +/** +* Prints the TAP summary. +* +* @param total total number of tests +* @param passing total number of passing tests +*/ +static void print_summary( int total, int passing ) { + printf( "#\n" ); + printf( "1..%d\n", total ); // TAP plan + printf( "# total %d\n", total ); + printf( "# pass %d\n", passing ); + printf( "#\n" ); + printf( "# ok\n" ); +} + +/** +* Prints benchmarks results. +* +* @param iterations number of iterations +* @param elapsed elapsed time in seconds +*/ +static void print_results( int iterations, double elapsed ) { + double rate = (double)iterations / elapsed; + printf( " ---\n" ); + printf( " iterations: %d\n", iterations ); + printf( " elapsed: %0.9f\n", elapsed ); + printf( " rate: %0.9f\n", rate ); + printf( " ...\n" ); +} + +/** +* Returns a clock time. +* +* @return clock time +*/ +static double tic( void ) { + struct timeval now; + gettimeofday( &now, NULL ); + return (double)now.tv_sec + (double)now.tv_usec/1.0e6; +} + +/** +* Generates a random number on the interval [min,max). +* +* @param min minimum value (inclusive) +* @param max maximum value (exclusive) +* @return random number +*/ +static double random_uniform( const double min, const double max ) { + double v = (double)rand() / ( (double)RAND_MAX + 1.0 ); + return min + ( v*(max-min) ); +} + +/** +* Runs a benchmark. +* +* @param iterations number of iterations +* @param N number of data points and features along each dimension +* @return elapsed time in seconds +*/ +static double benchmark1( int iterations, int N ) { + int32_t *counts; + double elapsed; + int32_t *out; + double *X; + double *C; + double t; + int i; + + X = (double *)malloc( ( N*N ) * sizeof( double ) ); + C = (double *)malloc( ( K*N ) * sizeof( double ) ); + out = (int32_t *)malloc( N * sizeof( int32_t ) ); + counts = (int32_t *)calloc( K, sizeof( int32_t ) ); + for ( i = 0; i < N*N; i++ ) { + X[ i ] = random_uniform( -10000.0, 10000.0 ); + } + for ( i = 0; i < K*N; i++ ) { + C[ i ] = random_uniform( -10000.0, 10000.0 ); + } + t = tic(); + for ( i = 0; i < iterations; i++ ) { + // cppcheck-suppress uninitvar + stdlib_strided_dclosest_centroids( CblasRowMajor, N, N, K, STDLIB_ML_KMEANS_SQEUCLIDEAN, X, N, C, N, out, 1, counts, 1 ); + if ( out[ i%N ] < 0 ) { + printf( "unexpected result\n" ); + break; + } + } + elapsed = tic() - t; + if ( out[ i%N ] < 0 ) { + printf( "unexpected result\n" ); + } + free( X ); + free( C ); + free( out ); + free( counts ); + return elapsed; +} + +/** +* Runs a benchmark. +* +* @param iterations number of iterations +* @param N number of data points and features along each dimension +* @return elapsed time in seconds +*/ +static double benchmark2( int iterations, int N ) { + int32_t *counts; + double elapsed; + int32_t *out; + double *X; + double *C; + double t; + int i; + + X = (double *)malloc( ( N*N ) * sizeof( double ) ); + C = (double *)malloc( ( K*N ) * sizeof( double ) ); + out = (int32_t *)malloc( N * sizeof( int32_t ) ); + counts = (int32_t *)calloc( K, sizeof( int32_t ) ); + for ( i = 0; i < N*N; i++ ) { + X[ i ] = random_uniform( -10000.0, 10000.0 ); + } + for ( i = 0; i < K*N; i++ ) { + C[ i ] = random_uniform( -10000.0, 10000.0 ); + } + t = tic(); + for ( i = 0; i < iterations; i++ ) { + // cppcheck-suppress uninitvar + stdlib_strided_dclosest_centroids_ndarray( N, N, K, STDLIB_ML_KMEANS_SQEUCLIDEAN, X, N, 1, 0, C, N, 1, 0, out, 1, 0, counts, 1, 0 ); + if ( out[ i%N ] < 0 ) { + printf( "unexpected result\n" ); + break; + } + } + elapsed = tic() - t; + if ( out[ i%N ] < 0 ) { + printf( "unexpected result\n" ); + } + free( X ); + free( C ); + free( out ); + free( counts ); + return elapsed; +} + +/** +* Main execution sequence. +*/ +int main( void ) { + double elapsed; + int count; + int iter; + int len; + int N; + int i; + int j; + + // Use the current time to seed the random number generator: + srand( time( NULL ) ); + + print_version(); + count = 0; + for ( i = MIN; i <= MAX; i++ ) { + len = pow( 10, i ); + N = (int)sqrt( (double)len ); + iter = ITERATIONS / pow( 10, i-1 ); + for ( j = 0; j < REPEATS; j++ ) { + count += 1; + printf( "# c::native::%s:order=row-major,size=%d\n", NAME, N*N ); + elapsed = benchmark1( iter, N ); + print_results( iter, elapsed ); + printf( "ok %d benchmark finished\n", count ); + } + for ( j = 0; j < REPEATS; j++ ) { + count += 1; + printf( "# c::native::%s:ndarray:order=row-major,size=%d\n", NAME, N*N ); + elapsed = benchmark2( iter, N ); + print_results( iter, elapsed ); + printf( "ok %d benchmark finished\n", count ); + } + } + print_summary( count, count ); +} diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/binding.gyp b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/binding.gyp new file mode 100644 index 000000000000..60dce9d0b31a --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/binding.gyp @@ -0,0 +1,265 @@ +# @license Apache-2.0 +# +# Copyright (c) 2026 The Stdlib Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# A `.gyp` file for building a Node.js native add-on. +# +# [1]: https://gyp.gsrc.io/docs/InputFormatReference.md +# [2]: https://gyp.gsrc.io/docs/UserDocumentation.md +{ + # List of files to include in this file: + 'includes': [ + './include.gypi', + ], + + # Define variables to be used throughout the configuration for all targets: + 'variables': { + # Target name should match the add-on export name: + 'addon_target_name%': 'addon', + + # Fortran compiler (to override -Dfortran_compiler=): + 'fortran_compiler%': 'gfortran', + + # Fortran compiler flags: + 'fflags': [ + # Specify the Fortran standard to which a program is expected to conform: + '-std=f95', + + # Indicate that the layout is free-form source code: + '-ffree-form', + + # Aggressive optimization: + '-O3', + + # Enable commonly used warning options: + '-Wall', + + # Warn if source code contains problematic language features: + '-Wextra', + + # Warn if a procedure is called without an explicit interface: + '-Wimplicit-interface', + + # Do not transform names of entities specified in Fortran source files by appending underscores (i.e., don't mangle names, thus allowing easier usage in C wrappers): + '-fno-underscoring', + + # Warn if source code contains Fortran 95 extensions and C-language constructs: + '-pedantic', + + # Compile but do not link (output is an object file): + '-c', + ], + + # Set variables based on the host OS: + 'conditions': [ + [ + 'OS=="win"', + { + # Define the object file suffix: + 'obj': 'obj', + }, + { + # Define the object file suffix: + 'obj': 'o', + } + ], # end condition (OS=="win") + ], # end conditions + }, # end variables + + # Define compile targets: + 'targets': [ + + # Target to generate an add-on: + { + # The target name should match the add-on export name: + 'target_name': '<(addon_target_name)', + + # Define dependencies: + 'dependencies': [], + + # Define directories which contain relevant include headers: + 'include_dirs': [ + # Local include directory: + '<@(include_dirs)', + ], + + # List of source files: + 'sources': [ + '<@(src_files)', + ], + + # Settings which should be applied when a target's object files are used as linker input: + 'link_settings': { + # Define libraries: + 'libraries': [ + '<@(libraries)', + ], + + # Define library directories: + 'library_dirs': [ + '<@(library_dirs)', + ], + }, + + # C/C++ compiler flags: + 'cflags': [ + # Enable commonly used warning options: + '-Wall', + + # Aggressive optimization: + '-O3', + ], + + # C specific compiler flags: + 'cflags_c': [ + # Specify the C standard to which a program is expected to conform: + '-std=c99', + ], + + # C++ specific compiler flags: + 'cflags_cpp': [ + # Specify the C++ standard to which a program is expected to conform: + '-std=c++11', + ], + + # Linker flags: + 'ldflags': [], + + # Apply conditions based on the host OS: + 'conditions': [ + [ + 'OS=="mac"', + { + # Linker flags: + 'ldflags': [ + '-undefined dynamic_lookup', + '-Wl,-no-pie', + '-Wl,-search_paths_first', + ], + }, + ], # end condition (OS=="mac") + [ + 'OS!="win"', + { + # C/C++ flags: + 'cflags': [ + # Generate platform-independent code: + '-fPIC', + ], + }, + ], # end condition (OS!="win") + ], # end conditions + + # Define custom build actions for particular inputs: + 'rules': [ + { + # Define a rule for processing Fortran files: + 'extension': 'f', + + # Define the pathnames to be used as inputs when performing processing: + 'inputs': [ + # Full path of the current input: + '<(RULE_INPUT_PATH)' + ], + + # Define the outputs produced during processing: + 'outputs': [ + # Store an output object file in a directory for placing intermediate results (only accessible within a single target): + '<(INTERMEDIATE_DIR)/<(RULE_INPUT_ROOT).<(obj)' + ], + + # Define the rule for compiling Fortran based on the host OS: + 'conditions': [ + [ + 'OS=="win"', + + # Rule to compile Fortran on Windows: + { + 'rule_name': 'compile_fortran_windows', + 'message': 'Compiling Fortran file <(RULE_INPUT_PATH) on Windows...', + + 'process_outputs_as_sources': 0, + + # Define the command-line invocation: + 'action': [ + '<(fortran_compiler)', + '<@(fflags)', + '<@(_inputs)', + '-o', + '<@(_outputs)', + ], + }, + + # Rule to compile Fortran on non-Windows: + { + 'rule_name': 'compile_fortran_linux', + 'message': 'Compiling Fortran file <(RULE_INPUT_PATH) on Linux...', + + 'process_outputs_as_sources': 1, + + # Define the command-line invocation: + 'action': [ + '<(fortran_compiler)', + '<@(fflags)', + '-fPIC', # generate platform-independent code + '<@(_inputs)', + '-o', + '<@(_outputs)', + ], + } + ], # end condition (OS=="win") + ], # end conditions + }, # end rule (extension=="f") + ], # end rules + }, # end target <(addon_target_name) + + # Target to copy a generated add-on to a standard location: + { + 'target_name': 'copy_addon', + + # Declare that the output of this target is not linked: + 'type': 'none', + + # Define dependencies: + 'dependencies': [ + # Require that the add-on be generated before building this target: + '<(addon_target_name)', + ], + + # Define a list of actions: + 'actions': [ + { + 'action_name': 'copy_addon', + 'message': 'Copying addon...', + + # Explicitly list the inputs in the command-line invocation below: + 'inputs': [], + + # Declare the expected outputs: + 'outputs': [ + '<(addon_output_dir)/<(addon_target_name).node', + ], + + # Define the command-line invocation: + 'action': [ + 'cp', + '<(PRODUCT_DIR)/<(addon_target_name).node', + '<(addon_output_dir)/<(addon_target_name).node', + ], + }, + ], # end actions + }, # end target copy_addon + ], # end targets +} diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/docs/repl.txt b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/docs/repl.txt new file mode 100644 index 000000000000..012ce0193687 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/docs/repl.txt @@ -0,0 +1,154 @@ + +{{alias}}( order, M, N, k, metric, X, LDX, C, LDC, out, so, counts, sco ) + Assigns each data point in a double-precision floating-point input matrix to + its closest centroid. + + Indexing is relative to the first index. To introduce an offset, use typed + array views. + + If `M`, `N`, or `k` is less than one, the function returns `out` unchanged. + + Parameters + ---------- + order: string + Row-major (C-style) or column-major (Fortran-style) order. Must be + either 'row-major' or 'column-major'. + + M: integer + Number of data points. + + N: integer + Number of features. + + k: integer + Number of centroids. + + metric: string + Distance metric. + + X: Float64Array + Input data matrix. + + LDX: integer + Stride of the first dimension of `X` (a.k.a., leading dimension of the + matrix `X`). + + C: Float64Array + Centroid matrix. + + LDC: integer + Stride of the first dimension of `C` (a.k.a., leading dimension of the + matrix `C`). + + out: Int32Array + Output array for closest centroid indices. + + so: integer + Stride length for `out`. + + counts: Int32Array + Output array for per-centroid assignment counts. + + sco: integer + Stride length for `counts`. + + Returns + ------- + out: Int32Array + Output array. + + Examples + -------- + > var X = new {{alias:@stdlib/array/float64}}( [ 1.0, 1.0, 5.0, 5.0, 1.5, + ... 1.5 ] ); + > var C = new {{alias:@stdlib/array/float64}}( [ 1.0, 1.0, 5.0, 5.0 ] ); + > var out = new {{alias:@stdlib/array/int32}}( 3 ); + > var counts = new {{alias:@stdlib/array/int32}}( 2 ); + > {{alias}}( 'row-major', 3, 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1, + ... counts, 1 ); + > out + [ 0, 1, 0 ] + + +{{alias}}.ndarray( M,N,k,metric,X,sx1,sx2,ox,C,sc1,sc2,oc,out,so,oo,co,sco,oco ) + Assigns each data point in a double-precision floating-point input matrix to + its closest centroid using alternative indexing semantics. + + While typed array views mandate a view offset based on the underlying + buffer, offset parameters support indexing semantics based on starting + indices. + + Parameters + ---------- + M: integer + Number of data points. + + N: integer + Number of features. + + k: integer + Number of centroids. + + metric: string + Distance metric. + + X: Float64Array + Input data matrix. + + sx1: integer + Stride of the first dimension of `X`. + + sx2: integer + Stride of the second dimension of `X`. + + ox: integer + Starting index for `X`. + + C: Float64Array + Centroid matrix. + + sc1: integer + Stride of the first dimension of `C`. + + sc2: integer + Stride of the second dimension of `C`. + + oc: integer + Starting index for `C`. + + out: Int32Array + Output array for closest centroid indices. + + so: integer + Stride length for `out`. + + oo: integer + Starting index for `out`. + + co: Int32Array + Output array for per-centroid assignment counts. + + sco: integer + Stride length for `co`. + + oco: integer + Starting index for `co`. + + Returns + ------- + out: Int32Array + Output array. + + Examples + -------- + > var X = new {{alias:@stdlib/array/float64}}( [ 1.0, 1.0, 5.0, 5.0, 1.5, 1.5 ] ); + > var C = new {{alias:@stdlib/array/float64}}( [ 1.0, 1.0, 5.0, 5.0 ] ); + > var out = new {{alias:@stdlib/array/int32}}( 3 ); + > var co = new {{alias:@stdlib/array/int32}}( 2 ); + > {{alias}}.ndarray( 3, 2, 2, 'sqeuclidean', X, 2, 1, 0, C, 2, 1, 0, out, + ... 1, 0, co, 1, 0 ); + > out + [ 0, 1, 0 ] + + See Also + -------- diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/docs/types/index.d.ts b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/docs/types/index.d.ts new file mode 100644 index 000000000000..908e6d9cc89e --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/docs/types/index.d.ts @@ -0,0 +1,150 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/// + +import { Layout } from '@stdlib/types/blas'; + +/** +* Interface describing `dclosestCentroids`. +*/ +interface Routine { + /** + * Assigns each data point in a double-precision floating-point input matrix to its closest centroid. + * + * @param order - storage layout + * @param M - number of data points + * @param N - number of features + * @param k - number of centroids + * @param metric - distance metric + * @param X - input data matrix + * @param LDX - stride of the first dimension of `X` (a.k.a., leading dimension of the matrix `X`) + * @param C - centroid matrix + * @param LDC - stride of the first dimension of `C` (a.k.a., leading dimension of the matrix `C`) + * @param out - output array for closest centroid indices + * @param so - stride length for `out` + * @param counts - output array for per-centroid assignment counts + * @param sco - stride length for `counts` + * @returns output array + * + * @example + * var Float64Array = require( '@stdlib/array/float64' ); + * var Int32Array = require( '@stdlib/array/int32' ); + * + * var X = new Float64Array( [ 1.0, 1.0, 5.0, 5.0, 1.5, 1.5 ] ); + * var C = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + * + * var out = new Int32Array( 3 ); + * var counts = new Int32Array( 2 ); + * + * dclosestCentroids( 'row-major', 3, 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1, counts, 1 ); + * // out => [ 0, 1, 0 ] + */ + ( order: Layout, M: number, N: number, k: number, metric: string, X: Float64Array, LDX: number, C: Float64Array, LDC: number, out: Int32Array, so: number, counts: Int32Array, sco: number ): Int32Array; + + /** + * Assigns each data point in a double-precision floating-point input matrix to its closest centroid using alternative indexing semantics. + * + * @param M - number of data points + * @param N - number of features + * @param k - number of centroids + * @param metric - distance metric + * @param X - input data matrix + * @param sx1 - stride of the first dimension of `X` + * @param sx2 - stride of the second dimension of `X` + * @param ox - starting index for `X` + * @param C - centroid matrix + * @param sc1 - stride of the first dimension of `C` + * @param sc2 - stride of the second dimension of `C` + * @param oc - starting index for `C` + * @param out - output array for closest centroid indices + * @param so - stride length for `out` + * @param oo - starting index for `out` + * @param counts - output array for per-centroid assignment counts + * @param sco - stride length for `counts` + * @param oco - starting index for `counts` + * @returns output array + * + * @example + * var Float64Array = require( '@stdlib/array/float64' ); + * var Int32Array = require( '@stdlib/array/int32' ); + * + * var X = new Float64Array( [ 1.0, 1.0, 5.0, 5.0, 1.5, 1.5 ] ); + * var C = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + * + * var out = new Int32Array( 3 ); + * var counts = new Int32Array( 2 ); + * + * dclosestCentroids.ndarray( 3, 2, 2, 'sqeuclidean', X, 2, 1, 0, C, 2, 1, 0, out, 1, 0, counts, 1, 0 ); + * // out => [ 0, 1, 0 ] + */ + ndarray( M: number, N: number, k: number, metric: string, X: Float64Array, sx1: number, sx2: number, ox: number, C: Float64Array, sc1: number, sc2: number, oc: number, out: Int32Array, so: number, oo: number, counts: Int32Array, sco: number, oco: number ): Int32Array; +} + +/** +* Assigns each data point in a double-precision floating-point input matrix to its closest centroid. +* +* @param order - storage layout +* @param M - number of data points +* @param N - number of features +* @param k - number of centroids +* @param metric - distance metric +* @param X - input data matrix +* @param LDX - stride of the first dimension of `X` (a.k.a., leading dimension of the matrix `X`) +* @param C - centroid matrix +* @param LDC - stride of the first dimension of `C` (a.k.a., leading dimension of the matrix `C`) +* @param out - output array for closest centroid indices +* @param so - stride length for `out` +* @param counts - output array for per-centroid assignment counts +* @param sco - stride length for `counts` +* @returns output array +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var Int32Array = require( '@stdlib/array/int32' ); +* +* var X = new Float64Array( [ 1.0, 1.0, 5.0, 5.0, 1.5, 1.5 ] ); +* var C = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); +* +* var out = new Int32Array( 3 ); +* var counts = new Int32Array( 2 ); +* +* dclosestCentroids( 'row-major', 3, 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1, counts, 1 ); +* // out => [ 0, 1, 0 ] +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var Int32Array = require( '@stdlib/array/int32' ); +* +* var X = new Float64Array( [ 1.0, 1.0, 5.0, 5.0, 1.5, 1.5 ] ); +* var C = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); +* +* var out = new Int32Array( 3 ); +* var counts = new Int32Array( 2 ); +* +* dclosestCentroids.ndarray( 3, 2, 2, 'sqeuclidean', X, 2, 1, 0, C, 2, 1, 0, out, 1, 0, counts, 1, 0 ); +* // out => [ 0, 1, 0 ] +*/ +declare var dclosestCentroids: Routine; + + +// EXPORTS // + +export = dclosestCentroids; diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/docs/types/test.ts b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/docs/types/test.ts new file mode 100644 index 000000000000..baaa08fad91c --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/docs/types/test.ts @@ -0,0 +1,360 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import dclosestCentroids = require( './index' ); + + +// TESTS // + +// The function returns an Int32Array... +{ + const X = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + const C = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + const out = new Int32Array( 2 ); + const counts = new Int32Array( 2 ); + + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1, counts, 1 ); // $ExpectType Int32Array +} + +// The compiler throws an error if the function is provided a first argument which is not a valid order... +{ + const X = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + const C = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + const out = new Int32Array( 2 ); + const counts = new Int32Array( 2 ); + + dclosestCentroids( 5, 2, 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( true, 2, 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( false, 2, 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( null, 2, 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( void 0, 2, 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( [], 2, 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( {}, 2, 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( ( x: number ): number => x, 2, 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1, counts, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a number... +{ + const X = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + const C = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + const out = new Int32Array( 2 ); + const counts = new Int32Array( 2 ); + + dclosestCentroids( 'row-major', '5', 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', true, 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', false, 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', null, 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', void 0, 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', [], 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', {}, 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', ( x: number ): number => x, 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1, counts, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a third argument which is not a number... +{ + const X = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + const C = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + const out = new Int32Array( 2 ); + const counts = new Int32Array( 2 ); + + dclosestCentroids( 'row-major', 2, '5', 2, 'sqeuclidean', X, 2, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, true, 2, 'sqeuclidean', X, 2, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, false, 2, 'sqeuclidean', X, 2, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, null, 2, 'sqeuclidean', X, 2, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, void 0, 2, 'sqeuclidean', X, 2, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, [], 2, 'sqeuclidean', X, 2, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, {}, 2, 'sqeuclidean', X, 2, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, ( x: number ): number => x, 2, 'sqeuclidean', X, 2, C, 2, out, 1, counts, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a fourth argument which is not a number... +{ + const X = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + const C = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + const out = new Int32Array( 2 ); + const counts = new Int32Array( 2 ); + + dclosestCentroids( 'row-major', 2, 2, '5', 'sqeuclidean', X, 2, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, true, 'sqeuclidean', X, 2, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, false, 'sqeuclidean', X, 2, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, null, 'sqeuclidean', X, 2, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, void 0, 'sqeuclidean', X, 2, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, [], 'sqeuclidean', X, 2, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, {}, 'sqeuclidean', X, 2, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, ( x: number ): number => x, 'sqeuclidean', X, 2, C, 2, out, 1, counts, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a fifth argument which is not a string... +{ + const X = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + const C = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + const out = new Int32Array( 2 ); + const counts = new Int32Array( 2 ); + + dclosestCentroids( 'row-major', 2, 2, 2, 5, X, 2, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, true, X, 2, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, false, X, 2, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, null, X, 2, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, void 0, X, 2, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, [], X, 2, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, {}, X, 2, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, ( x: number ): number => x, X, 2, C, 2, out, 1, counts, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a sixth argument which is not a Float64Array... +{ + const C = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + const out = new Int32Array( 2 ); + const counts = new Int32Array( 2 ); + + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', 5, 2, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', true, 2, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', false, 2, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', null, 2, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', void 0, 2, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', {}, 2, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', ( x: number ): number => x, 2, C, 2, out, 1, counts, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a seventh argument which is not a number... +{ + const X = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + const C = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + const out = new Int32Array( 2 ); + const counts = new Int32Array( 2 ); + + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, '5', C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, true, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, false, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, null, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, void 0, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, [], C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, {}, C, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, ( x: number ): number => x, C, 2, out, 1, counts, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided an eighth argument which is not a Float64Array... +{ + const X = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + const out = new Int32Array( 2 ); + const counts = new Int32Array( 2 ); + + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, 5, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, true, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, false, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, null, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, void 0, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, {}, 2, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, ( x: number ): number => x, 2, out, 1, counts, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a ninth argument which is not a number... +{ + const X = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + const C = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + const out = new Int32Array( 2 ); + const counts = new Int32Array( 2 ); + + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, '5', out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, true, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, false, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, null, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, void 0, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, [], out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, {}, out, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, ( x: number ): number => x, out, 1, counts, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a tenth argument which is not an Int32Array... +{ + const X = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + const C = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + const counts = new Int32Array( 2 ); + + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, 2, 5, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, 2, true, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, 2, false, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, 2, null, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, 2, void 0, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, 2, {}, 1, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, 2, ( x: number ): number => x, 1, counts, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided an eleventh argument which is not a number... +{ + const X = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + const C = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + const out = new Int32Array( 2 ); + const counts = new Int32Array( 2 ); + + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, 2, out, '5', counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, 2, out, true, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, 2, out, false, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, 2, out, null, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, 2, out, void 0, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, 2, out, [], counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, 2, out, {}, counts, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, 2, out, ( x: number ): number => x, counts, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a twelfth argument which is not an Int32Array... +{ + const X = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + const C = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + const out = new Int32Array( 2 ); + + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1, 5, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1, true, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1, false, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1, null, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1, void 0, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1, {}, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1, ( x: number ): number => x, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a thirteenth argument which is not a number... +{ + const X = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + const C = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + const out = new Int32Array( 2 ); + const counts = new Int32Array( 2 ); + + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1, counts, '5' ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1, counts, true ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1, counts, false ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1, counts, null ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1, counts, void 0 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1, counts, [] ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1, counts, {} ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1, counts, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const X = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + const C = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + const out = new Int32Array( 2 ); + const counts = new Int32Array( 2 ); + + dclosestCentroids(); // $ExpectError + dclosestCentroids( 'row-major' ); // $ExpectError + dclosestCentroids( 'row-major', 2 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean' ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, 2 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, 2, out ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1 ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1, counts ); // $ExpectError + dclosestCentroids( 'row-major', 2, 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1, counts, 1, 0 ); // $ExpectError +} + +// Attached to main export is an `ndarray` method which returns an Int32Array... +{ + const X = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + const C = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + const out = new Int32Array( 2 ); + const counts = new Int32Array( 2 ); + + dclosestCentroids.ndarray( 2, 2, 2, 'sqeuclidean', X, 2, 1, 0, C, 2, 1, 0, out, 1, 0, counts, 1, 0 ); // $ExpectType Int32Array +} + +// The compiler throws an error if the `ndarray` method is provided a fourth argument which is not a string... +{ + const X = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + const C = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + const out = new Int32Array( 2 ); + const counts = new Int32Array( 2 ); + + dclosestCentroids.ndarray( 2, 2, 2, 5, X, 2, 1, 0, C, 2, 1, 0, out, 1, 0, counts, 1, 0 ); // $ExpectError + dclosestCentroids.ndarray( 2, 2, 2, true, X, 2, 1, 0, C, 2, 1, 0, out, 1, 0, counts, 1, 0 ); // $ExpectError + dclosestCentroids.ndarray( 2, 2, 2, null, X, 2, 1, 0, C, 2, 1, 0, out, 1, 0, counts, 1, 0 ); // $ExpectError + dclosestCentroids.ndarray( 2, 2, 2, {}, X, 2, 1, 0, C, 2, 1, 0, out, 1, 0, counts, 1, 0 ); // $ExpectError + dclosestCentroids.ndarray( 2, 2, 2, ( x: number ): number => x, X, 2, 1, 0, C, 2, 1, 0, out, 1, 0, counts, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a fifth argument which is not a Float64Array... +{ + const C = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + const out = new Int32Array( 2 ); + const counts = new Int32Array( 2 ); + + dclosestCentroids.ndarray( 2, 2, 2, 'sqeuclidean', 5, 2, 1, 0, C, 2, 1, 0, out, 1, 0, counts, 1, 0 ); // $ExpectError + dclosestCentroids.ndarray( 2, 2, 2, 'sqeuclidean', true, 2, 1, 0, C, 2, 1, 0, out, 1, 0, counts, 1, 0 ); // $ExpectError + dclosestCentroids.ndarray( 2, 2, 2, 'sqeuclidean', null, 2, 1, 0, C, 2, 1, 0, out, 1, 0, counts, 1, 0 ); // $ExpectError + dclosestCentroids.ndarray( 2, 2, 2, 'sqeuclidean', {}, 2, 1, 0, C, 2, 1, 0, out, 1, 0, counts, 1, 0 ); // $ExpectError + dclosestCentroids.ndarray( 2, 2, 2, 'sqeuclidean', ( x: number ): number => x, 2, 1, 0, C, 2, 1, 0, out, 1, 0, counts, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a ninth argument which is not a Float64Array... +{ + const X = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + const out = new Int32Array( 2 ); + const counts = new Int32Array( 2 ); + + dclosestCentroids.ndarray( 2, 2, 2, 'sqeuclidean', X, 2, 1, 0, 5, 2, 1, 0, out, 1, 0, counts, 1, 0 ); // $ExpectError + dclosestCentroids.ndarray( 2, 2, 2, 'sqeuclidean', X, 2, 1, 0, true, 2, 1, 0, out, 1, 0, counts, 1, 0 ); // $ExpectError + dclosestCentroids.ndarray( 2, 2, 2, 'sqeuclidean', X, 2, 1, 0, null, 2, 1, 0, out, 1, 0, counts, 1, 0 ); // $ExpectError + dclosestCentroids.ndarray( 2, 2, 2, 'sqeuclidean', X, 2, 1, 0, {}, 2, 1, 0, out, 1, 0, counts, 1, 0 ); // $ExpectError + dclosestCentroids.ndarray( 2, 2, 2, 'sqeuclidean', X, 2, 1, 0, ( x: number ): number => x, 2, 1, 0, out, 1, 0, counts, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a thirteenth argument which is not an Int32Array... +{ + const X = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + const C = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + const counts = new Int32Array( 2 ); + + dclosestCentroids.ndarray( 2, 2, 2, 'sqeuclidean', X, 2, 1, 0, C, 2, 1, 0, 5, 1, 0, counts, 1, 0 ); // $ExpectError + dclosestCentroids.ndarray( 2, 2, 2, 'sqeuclidean', X, 2, 1, 0, C, 2, 1, 0, true, 1, 0, counts, 1, 0 ); // $ExpectError + dclosestCentroids.ndarray( 2, 2, 2, 'sqeuclidean', X, 2, 1, 0, C, 2, 1, 0, null, 1, 0, counts, 1, 0 ); // $ExpectError + dclosestCentroids.ndarray( 2, 2, 2, 'sqeuclidean', X, 2, 1, 0, C, 2, 1, 0, {}, 1, 0, counts, 1, 0 ); // $ExpectError + dclosestCentroids.ndarray( 2, 2, 2, 'sqeuclidean', X, 2, 1, 0, C, 2, 1, 0, ( x: number ): number => x, 1, 0, counts, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a sixteenth argument which is not an Int32Array... +{ + const X = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + const C = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + const out = new Int32Array( 2 ); + + dclosestCentroids.ndarray( 2, 2, 2, 'sqeuclidean', X, 2, 1, 0, C, 2, 1, 0, out, 1, 0, 5, 1, 0 ); // $ExpectError + dclosestCentroids.ndarray( 2, 2, 2, 'sqeuclidean', X, 2, 1, 0, C, 2, 1, 0, out, 1, 0, true, 1, 0 ); // $ExpectError + dclosestCentroids.ndarray( 2, 2, 2, 'sqeuclidean', X, 2, 1, 0, C, 2, 1, 0, out, 1, 0, null, 1, 0 ); // $ExpectError + dclosestCentroids.ndarray( 2, 2, 2, 'sqeuclidean', X, 2, 1, 0, C, 2, 1, 0, out, 1, 0, {}, 1, 0 ); // $ExpectError + dclosestCentroids.ndarray( 2, 2, 2, 'sqeuclidean', X, 2, 1, 0, C, 2, 1, 0, out, 1, 0, ( x: number ): number => x, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided an unsupported number of arguments... +{ + const X = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + const C = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); + const out = new Int32Array( 2 ); + const counts = new Int32Array( 2 ); + + dclosestCentroids.ndarray(); // $ExpectError + dclosestCentroids.ndarray( 2 ); // $ExpectError + dclosestCentroids.ndarray( 2, 2 ); // $ExpectError + dclosestCentroids.ndarray( 2, 2, 2 ); // $ExpectError + dclosestCentroids.ndarray( 2, 2, 2, 'sqeuclidean' ); // $ExpectError + dclosestCentroids.ndarray( 2, 2, 2, 'sqeuclidean', X ); // $ExpectError + dclosestCentroids.ndarray( 2, 2, 2, 'sqeuclidean', X, 2, 1, 0, C, 2, 1, 0, out, 1, 0, counts, 1, 0, 0 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/examples/c/Makefile b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/examples/c/Makefile new file mode 100644 index 000000000000..c8f8e9a1517b --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/examples/c/Makefile @@ -0,0 +1,146 @@ +#/ +# @license Apache-2.0 +# +# Copyright (c) 2026 The Stdlib Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +#/ + +# VARIABLES # + +ifndef VERBOSE + QUIET := @ +else + QUIET := +endif + +# Determine the OS ([1][1], [2][2]). +# +# [1]: https://en.wikipedia.org/wiki/Uname#Examples +# [2]: http://stackoverflow.com/a/27776822/2225624 +OS ?= $(shell uname) +ifneq (, $(findstring MINGW,$(OS))) + OS := WINNT +else +ifneq (, $(findstring MSYS,$(OS))) + OS := WINNT +else +ifneq (, $(findstring CYGWIN,$(OS))) + OS := WINNT +else +ifneq (, $(findstring Windows_NT,$(OS))) + OS := WINNT +endif +endif +endif +endif + +# Define the program used for compiling C source files: +ifdef C_COMPILER + CC := $(C_COMPILER) +else + CC := gcc +endif + +# Define the command-line options when compiling C files: +CFLAGS ?= \ + -std=c99 \ + -O3 \ + -Wall \ + -pedantic + +# Determine whether to generate position independent code ([1][1], [2][2]). +# +# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options +# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option +ifeq ($(OS), WINNT) + fPIC ?= +else + fPIC ?= -fPIC +endif + +# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): +INCLUDE ?= + +# List of source files: +SOURCE_FILES ?= + +# List of libraries (e.g., `-lopenblas -lpthread`): +LIBRARIES ?= + +# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): +LIBPATH ?= + +# List of C targets: +c_targets := example.out + + +# RULES # + +#/ +# Compiles source files. +# +# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`) +# @param {string} [CFLAGS] - C compiler options +# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`) +# @param {string} [SOURCE_FILES] - list of source files +# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) +# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas -lpthread`) +# +# @example +# make +# +# @example +# make all +#/ +all: $(c_targets) + +.PHONY: all + +#/ +# Compiles C source files. +# +# @private +# @param {string} CC - C compiler (e.g., `gcc`) +# @param {string} CFLAGS - C compiler options +# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar`) +# @param {string} SOURCE_FILES - list of source files +# @param {string} LIBPATH - list of library paths (e.g., `-L /foo/bar`) +# @param {string} LIBRARIES - list of libraries (e.g., `-lopenblas`) +#/ +$(c_targets): %.out: %.c + $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) + +#/ +# Runs compiled examples. +# +# @example +# make run +#/ +run: $(c_targets) + $(QUIET) ./$< + +.PHONY: run + +#/ +# Removes generated files. +# +# @example +# make clean +#/ +clean: + $(QUIET) -rm -f *.o *.out + +.PHONY: clean diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/examples/c/example.c b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/examples/c/example.c new file mode 100644 index 000000000000..9ac3045365ce --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/examples/c/example.c @@ -0,0 +1,71 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "stdlib/ml/strided/dkmeans_closest_centroids.h" +#include "stdlib/ml/base/kmeans/metrics.h" +#include "stdlib/blas/base/shared.h" +#include +#include + +int main( void ) { + // Define a collection of data points (row-major): + const double X[ 3*3 ] = { + 1.0, 2.0, 3.0, + 4.0, 5.0, 6.0, + 7.0, 8.0, 9.0 + }; + + // Define a collection of centroids (row-major): + const double C[] = { 1.0, 1.0, 5.0, 5.0, 2.0, 2.0 }; + + // Specify the number of data points, features, and centroids: + const int M = 3; + const int N = 3; + const int k = 2; + + // Allocate output arrays: + int32_t out[ 3 ]; + int32_t counts[] = { 0, 0 }; + + // Assign each data point to its closest centroid: + stdlib_strided_dclosest_centroids( CblasRowMajor, M, N, k, STDLIB_ML_KMEANS_SQEUCLIDEAN, X, N, C, N, out, 1, counts, 1 ); + + // Print the results: + int i; + for ( i = 0; i < M; i++ ) { + printf( "out[ %i ] = %i\n", i, out[ i ] ); + } + for ( i = 0; i < k; i++ ) { + printf( "counts[ %i ] = %i\n", i, counts[ i ] ); + } + + for ( i = 0; i < k; i++ ) { + counts[ i ] = 0; + } + + // Assign each data point to its closest centroid using alternative indexing semantics: + stdlib_strided_dclosest_centroids_ndarray( M, N, k, STDLIB_ML_KMEANS_SQEUCLIDEAN, X, N, 1, 0, C, N, 1, 0, out, 1, 0, counts, 1, 0 ); + + // Print the results: + for ( i = 0; i < M; i++ ) { + printf( "out[ %i ] = %i\n", i, out[ i ] ); + } + for ( i = 0; i < k; i++ ) { + printf( "counts[ %i ] = %i\n", i, counts[ i ] ); + } +} diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/examples/index.js b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/examples/index.js new file mode 100644 index 000000000000..9de9ba7e30ee --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/examples/index.js @@ -0,0 +1,45 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var Float64Array = require( '@stdlib/array/float64' ); +var Int32Array = require( '@stdlib/array/int32' ); +var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); +var dclosestCentroids = require( './../lib' ); + +var order = 'row-major'; + +var shapeX = [ 4, 2 ]; +var stridesX = shape2strides( shapeX, order ); +var X = new Float64Array( [ 1.0, 1.0, 5.0, 5.0, 1.5, 1.5, 4.5, 5.5 ] ); +console.log( ndarray2array( X, shapeX, stridesX, 0, order ) ); + +var shapeC = [ 2, 2 ]; +var stridesC = shape2strides( shapeC, order ); +var C = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); +console.log( ndarray2array( C, shapeC, stridesC, 0, order ) ); + +var out = new Int32Array( shapeX[ 0 ] ); +var counts = new Int32Array( shapeC[ 0 ] ); + +dclosestCentroids( order, shapeX[ 0 ], shapeX[ 1 ], shapeC[ 0 ], 'sqeuclidean', X, stridesX[ 0 ], C, stridesC[ 0 ], out, 1, counts, 1 ); + +console.log( 'out = %s', out.toString() ); +console.log( 'counts = %s', counts.toString() ); diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/include.gypi b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/include.gypi new file mode 100644 index 000000000000..dcb556d250e8 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/include.gypi @@ -0,0 +1,70 @@ +# @license Apache-2.0 +# +# Copyright (c) 2026 The Stdlib Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# A GYP include file for building a Node.js native add-on. +# +# Note that nesting variables is required due to how GYP processes a configuration. Any variables defined within a nested 'variables' section is defined in the outer scope. Thus, conditions in the outer variable scope are free to use these variables without running into "variable undefined" errors. +# +# Main documentation: +# +# [1]: https://gyp.gsrc.io/docs/InputFormatReference.md +# [2]: https://gyp.gsrc.io/docs/UserDocumentation.md +# +# Variable nesting hacks: +# +# [3]: https://chromium.googlesource.com/external/skia/gyp/+/master/common_variables.gypi +# [4]: https://src.chromium.org/viewvc/chrome/trunk/src/build/common.gypi?revision=127004 +{ + # Define variables to be used throughout the configuration for all targets: + 'variables': { + 'variables': { + # Host BLAS library (to override -Dblas=): + 'blas%': '', + + # Path to BLAS library (to override -Dblas_dir=): + 'blas_dir%': '', + }, # end variables + + # Source directory: + 'src_dir': './src', + + # Include directories: + 'include_dirs': [ + '<@(blas_dir)', + ' + +/* +* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. +*/ +#ifdef __cplusplus +extern "C" { +#endif + +/** +* Assigns each data point in a double-precision floating-point input matrix to its closest centroid. +*/ +void API_SUFFIX(stdlib_strided_dclosest_centroids)( const CBLAS_LAYOUT order, const CBLAS_INT M, const CBLAS_INT N, const CBLAS_INT k, const enum STDLIB_ML_KMEANS_METRIC metric, const double *X, const CBLAS_INT LDX, const double *C, const CBLAS_INT LDC, CBLAS_INT *out, const CBLAS_INT so, CBLAS_INT *counts, const CBLAS_INT sco ); + +/** +* Assigns each data point in a double-precision floating-point input matrix to its closest centroid using alternative indexing semantics. +*/ +void API_SUFFIX(stdlib_strided_dclosest_centroids_ndarray)( const CBLAS_INT M, const CBLAS_INT N, const CBLAS_INT k, const enum STDLIB_ML_KMEANS_METRIC metric, const double *X, const CBLAS_INT sx1, const CBLAS_INT sx2, const CBLAS_INT ox, const double *C, const CBLAS_INT sc1, const CBLAS_INT sc2, const CBLAS_INT oc, CBLAS_INT *out, const CBLAS_INT so, const CBLAS_INT oo, CBLAS_INT *counts, const CBLAS_INT sco, const CBLAS_INT oco ); + +#ifdef __cplusplus +} +#endif + +#endif // !STDLIB_ML_STRIDED_DKMEANS_CLOSEST_CENTROIDS_H diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/lib/base.js b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/lib/base.js new file mode 100644 index 000000000000..fc747e689ec1 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/lib/base.js @@ -0,0 +1,111 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var dsquaredEuclidean = require( '@stdlib/stats/strided/distances/dsquared-euclidean' ).ndarray; +var dcorrelation = require( '@stdlib/stats/strided/distances/dcorrelation' ).ndarray; +var dcityblock = require( '@stdlib/stats/strided/distances/dcityblock' ).ndarray; +var dcosine = require( '@stdlib/stats/strided/distances/dcosine-distance' ).ndarray; + + +// MAIN // + +/** +* Assigns each data point in a double-precision floating-point input matrix to its closest centroid. +* +* @private +* @param {NonNegativeInteger} M - number of data points +* @param {NonNegativeInteger} N - number of features +* @param {NonNegativeInteger} k - number of centroids +* @param {string} metric - distance metric +* @param {Float64Array} X - input data matrix +* @param {integer} sx1 - stride of the first dimension of `X` +* @param {integer} sx2 - stride of the second dimension of `X` +* @param {NonNegativeInteger} ox - index offset for `X` +* @param {Float64Array} C - centroid matrix +* @param {integer} sc1 - stride of the first dimension of `C` +* @param {integer} sc2 - stride of the second dimension of `C` +* @param {NonNegativeInteger} oc - index offset for `C` +* @param {Int32Array} out - output array for closest centroid indices +* @param {integer} so - stride length for `out` +* @param {NonNegativeInteger} oo - index offset for `out` +* @param {Int32Array} counts - output array for per-centroid assignment counts +* @param {integer} sco - stride length for `counts` +* @param {NonNegativeInteger} oco - index offset for `counts` +* @returns {Int32Array} output array +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var Int32Array = require( '@stdlib/array/int32' ); +* +* var X = new Float64Array( [ 1.0, 1.0, 5.0, 5.0, 1.5, 1.5 ] ); +* var C = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); +* +* var out = new Int32Array( 3 ); +* var counts = new Int32Array( 2 ); +* +* closestCentroids( 3, 2, 2, 'sqeuclidean', X, 2, 1, 0, C, 2, 1, 0, out, 1, 0, counts, 1, 0 ); +* // out => [ 0, 1, 0 ] +*/ +function closestCentroids( M, N, k, metric, X, sx1, sx2, ox, C, sc1, sc2, oc, out, so, oo, counts, sco, oco ) { // eslint-disable-line max-len, max-params + var bestDist; + var best; + var xidx; + var oidx; + var dist; + var d; + var i; + var j; + + if ( metric === 'sqeuclidean' ) { + dist = dsquaredEuclidean; + } else if ( metric === 'correlation' ) { + dist = dcorrelation; + } else if ( metric === 'cityblock' ) { + dist = dcityblock; + } else { + dist = dcosine; + } + + xidx = ox; + for ( i = 0; i < M; i++ ) { + oidx = oc; + best = 0; + bestDist = dist( N, X, sx2, xidx, C, sc2, oidx ); + + oidx += sc1; // move to the next centroid + for ( j = 1; j < k; j++ ) { + d = dist( N, X, sx2, xidx, C, sc2, oidx ); + if ( d < bestDist ) { + bestDist = d; + best = j; + } + oidx += sc1; + } + + out[ oo + ( i*so ) ] = best; + counts[ oco + ( sco*best ) ] += 1; + xidx += sx1; + } + return out; +} + +module.exports = closestCentroids; diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/lib/dkmeans_closest_centroids.js b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/lib/dkmeans_closest_centroids.js new file mode 100644 index 000000000000..8ab083aa1da0 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/lib/dkmeans_closest_centroids.js @@ -0,0 +1,119 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isLayout = require( '@stdlib/blas/base/assert/is-layout' ); +var isRowMajor = require( '@stdlib/ndarray/base/assert/is-row-major-string' ); +var isColumnMajor = require( '@stdlib/ndarray/base/assert/is-column-major-string' ); +var resolveMetricStr = require( '@stdlib/ml/base/kmeans/metric-resolve-str' ); +var stride2offset = require( '@stdlib/strided/base/stride2offset' ); +var max = require( '@stdlib/math/base/special/fast/max' ); +var format = require( '@stdlib/string/format' ); +var ndarray = require( './ndarray.js' ); + + +// MAIN // + +/** +* Assigns each data point in a double-precision floating-point input matrix to its closest centroid. +* +* @param {string} order - storage layout +* @param {NonNegativeInteger} M - number of data points +* @param {NonNegativeInteger} N - number of features +* @param {NonNegativeInteger} k - number of centroids +* @param {string} metric - distance metric +* @param {Float64Array} X - input data matrix +* @param {integer} LDX - stride of the first dimension of `X` (a.k.a., leading dimension of the matrix `X`) +* @param {Float64Array} C - centroid matrix +* @param {integer} LDC - stride of the first dimension of `C` (a.k.a., leading dimension of the matrix `C`) +* @param {Int32Array} out - output array for closest centroid indices +* @param {integer} so - stride length for `out` +* @param {Int32Array} counts - output array for per-centroid assignment counts +* @param {integer} sco - stride length for `counts` +* @throws {TypeError} first argument must be a valid order +* @throws {TypeError} fifth argument must be a supported distance metric +* @throws {RangeError} seventh argument must be greater than or equal to max(1,N) (row-major) or max(1,M) (column-major) +* @throws {RangeError} ninth argument must be greater than or equal to max(1,N) (row-major) or max(1,k) (column-major) +* @returns {Int32Array} output array +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var Int32Array = require( '@stdlib/array/int32' ); +* +* var X = new Float64Array( [ 1.0, 1.0, 5.0, 5.0, 1.5, 1.5 ] ); +* var C = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); +* +* var out = new Int32Array( 3 ); +* var counts = new Int32Array( 2 ); +* +* dclosestCentroids( 'row-major', 3, 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1, counts, 1 ); +* // out => [ 0, 1, 0 ] +*/ +function dclosestCentroids( order, M, N, k, metric, X, LDX, C, LDC, out, so, counts, sco ) { // eslint-disable-line max-len, max-params + var sx1; + var sx2; + var sc1; + var sc2; + var sx; + var sc; + + if ( !isLayout( order ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a valid order. Value: `%s`.', order ) ); + } + if ( isRowMajor( order ) ) { + sx = N; + sc = N; + } else { + sx = M; + sc = k; + } + if ( resolveMetricStr( metric ) === null ) { + throw new TypeError( format( 'invalid argument. Fifth argument must be a supported distance metric. Value: `%s`.', metric ) ); + } + if ( LDX < max( 1, sx ) ) { + throw new RangeError( format( 'invalid argument. Seventh argument must be greater than or equal to max(1,%d). Value: `%d`.', sx, LDX ) ); + } + if ( LDC < max( 1, sc ) ) { + throw new RangeError( format( 'invalid argument. Ninth argument must be greater than or equal to max(1,%d). Value: `%d`.', sc, LDC ) ); + } + if ( k < 1 || M < 1 || N < 1 ) { + return out; + } + if ( isColumnMajor( order ) ) { + sx1 = 1; + sx2 = LDX; + + sc1 = 1; + sc2 = LDC; + } else { // order === 'row-major' + sx1 = LDX; + sx2 = 1; + + sc1 = LDC; + sc2 = 1; + } + return ndarray( M, N, k, metric, X, sx1, sx2, 0, C, sc1, sc2, 0, out, so, stride2offset( M, so ), counts, sco, stride2offset( k, sco ) ); // eslint-disable-line max-len +} + + +// EXPORTS // + +module.exports = dclosestCentroids; diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/lib/dkmeans_closest_centroids.native.js b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/lib/dkmeans_closest_centroids.native.js new file mode 100644 index 000000000000..0a913832493c --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/lib/dkmeans_closest_centroids.native.js @@ -0,0 +1,103 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isLayout = require( '@stdlib/blas/base/assert/is-layout' ); +var isRowMajor = require( '@stdlib/ndarray/base/assert/is-row-major-string' ); +var resolveMetricStr = require( '@stdlib/ml/base/kmeans/metric-resolve-str' ); +var resolveMetricEnum = require( '@stdlib/ml/base/kmeans/metric-resolve-enum' ); +var resolveOrder = require( '@stdlib/blas/base/layout-resolve-enum' ); +var max = require( '@stdlib/math/base/special/fast/max' ); +var format = require( '@stdlib/string/format' ); +var addon = require( './../src/addon.node' ); + + +// MAIN // + +/** +* Assigns each data point in a double-precision floating-point input matrix to its closest centroid. +* +* @param {string} order - storage layout +* @param {NonNegativeInteger} M - number of data points +* @param {NonNegativeInteger} N - number of features +* @param {NonNegativeInteger} k - number of centroids +* @param {string} metric - distance metric +* @param {Float64Array} X - input data matrix +* @param {integer} LDX - stride of the first dimension of `X` (a.k.a., leading dimension of the matrix `X`) +* @param {Float64Array} C - centroid matrix +* @param {integer} LDC - stride of the first dimension of `C` (a.k.a., leading dimension of the matrix `C`) +* @param {Int32Array} out - output array for closest centroid indices +* @param {integer} so - stride length for `out` +* @param {Int32Array} counts - output array for per-centroid assignment counts +* @param {integer} sco - stride length for `counts` +* @throws {TypeError} first argument must be a valid order +* @throws {TypeError} fifth argument must be a supported distance metric +* @throws {RangeError} seventh argument must be greater than or equal to max(1,N) (row-major) or max(1,M) (column-major) +* @throws {RangeError} ninth argument must be greater than or equal to max(1,N) (row-major) or max(1,k) (column-major) +* @returns {Int32Array} output array +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var Int32Array = require( '@stdlib/array/int32' ); +* +* var X = new Float64Array( [ 1.0, 1.0, 5.0, 5.0, 1.5, 1.5 ] ); +* var C = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); +* +* var out = new Int32Array( 3 ); +* var counts = new Int32Array( 2 ); +* +* dclosestCentroids( 'row-major', 3, 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1, counts, 1 ); +* // out => [ 0, 1, 0 ] +*/ +function dclosestCentroids( order, M, N, k, metric, X, LDX, C, LDC, out, so, counts, sco ) { // eslint-disable-line max-len, max-params + var sx; + var sc; + + if ( !isLayout( order ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a valid order. Value: `%s`.', order ) ); + } + if ( isRowMajor( order ) ) { + sx = N; + sc = N; + } else { + sx = M; + sc = k; + } + if ( resolveMetricStr( metric ) === null ) { + throw new TypeError( format( 'invalid argument. Fifth argument must be a supported distance metric. Value: `%s`.', metric ) ); + } + if ( LDX < max( 1, sx ) ) { + throw new RangeError( format( 'invalid argument. Seventh argument must be greater than or equal to max(1,%d). Value: `%d`.', sx, LDX ) ); + } + if ( LDC < max( 1, sc ) ) { + throw new RangeError( format( 'invalid argument. Ninth argument must be greater than or equal to max(1,%d). Value: `%d`.', sc, LDC ) ); + } + if ( k < 1 || M < 1 || N < 1 ) { + return out; + } + addon( resolveOrder( order ), M, N, k, resolveMetricEnum( metric ), X, LDX, C, LDC, out, so, counts, sco ); // eslint-disable-line max-len + return out; +} + + +// EXPORTS // + +module.exports = dclosestCentroids; diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/lib/index.js b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/lib/index.js new file mode 100644 index 000000000000..87643f4f6f85 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/lib/index.js @@ -0,0 +1,78 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Assign each data point in a double-precision floating-point input matrix to its closest centroid. +* +* @module @stdlib/ml/strided/dkmeans-closest-centroids +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var Int32Array = require( '@stdlib/array/int32' ); +* var dclosestCentroids = require( '@stdlib/ml/strided/dkmeans-closest-centroids' ); +* +* var X = new Float64Array( [ 1.0, 1.0, 5.0, 5.0, 1.5, 1.5 ] ); +* var C = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); +* +* var out = new Int32Array( 3 ); +* var counts = new Int32Array( 2 ); +* +* dclosestCentroids( 'row-major', 3, 2, 2, 'sqeuclidean', X, 2, C, 2, out, 1, counts, 1 ); +* // out => [ 0, 1, 0 ] +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var Int32Array = require( '@stdlib/array/int32' ); +* var dclosestCentroids = require( '@stdlib/ml/strided/dkmeans-closest-centroids' ); +* +* var X = new Float64Array( [ 1.0, 1.0, 5.0, 5.0, 1.5, 1.5 ] ); +* var C = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); +* +* var out = new Int32Array( 3 ); +* var counts = new Int32Array( 2 ); +* +* dclosestCentroids.ndarray( 3, 2, 2, 'sqeuclidean', X, 2, 1, 0, C, 2, 1, 0, out, 1, 0, counts, 1, 0 ); +* // out => [ 0, 1, 0 ] +*/ + +// MODULES // + +var join = require( 'path' ).join; +var tryRequire = require( '@stdlib/utils/try-require' ); +var isError = require( '@stdlib/assert/is-error' ); +var main = require( './main.js' ); + + +// MAIN // + +var dclosestCentroids; +var tmp = tryRequire( join( __dirname, './native.js' ) ); +if ( isError( tmp ) ) { + dclosestCentroids = main; +} else { + dclosestCentroids = tmp; +} + + +// EXPORTS // + +module.exports = dclosestCentroids; + +// exports: { "ndarray": "dclosestCentroids.ndarray" } diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/lib/main.js b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/lib/main.js new file mode 100644 index 000000000000..2728bf7a2f13 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/lib/main.js @@ -0,0 +1,35 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var dclosestCentroids = require( './dkmeans_closest_centroids.js' ); +var ndarray = require( './ndarray.js' ); + + +// MAIN // + +setReadOnly( dclosestCentroids, 'ndarray', ndarray ); + + +// EXPORTS // + +module.exports = dclosestCentroids; diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/lib/native.js b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/lib/native.js new file mode 100644 index 000000000000..ce44a736d7e2 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/lib/native.js @@ -0,0 +1,35 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var dclosestCentroids = require( './dkmeans_closest_centroids.native.js' ); +var ndarray = require( './ndarray.native.js' ); + + +// MAIN // + +setReadOnly( dclosestCentroids, 'ndarray', ndarray ); + + +// EXPORTS // + +module.exports = dclosestCentroids; diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/lib/ndarray.js b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/lib/ndarray.js new file mode 100644 index 000000000000..0431496ef459 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/lib/ndarray.js @@ -0,0 +1,81 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolveMetricStr = require( '@stdlib/ml/base/kmeans/metric-resolve-str' ); +var format = require( '@stdlib/string/format' ); +var base = require( './base.js' ); + + +// MAIN // + +/** +* Assigns each data point in a double-precision floating-point input matrix to its closest centroid using alternative indexing semantics. +* +* @param {NonNegativeInteger} M - number of data points +* @param {NonNegativeInteger} N - number of features +* @param {NonNegativeInteger} k - number of centroids +* @param {string} metric - distance metric +* @param {Float64Array} X - input data matrix +* @param {integer} sx1 - stride of the first dimension of `X` +* @param {integer} sx2 - stride of the second dimension of `X` +* @param {NonNegativeInteger} ox - index offset for `X` +* @param {Float64Array} C - centroid matrix +* @param {integer} sc1 - stride of the first dimension of `C` +* @param {integer} sc2 - stride of the second dimension of `C` +* @param {NonNegativeInteger} oc - index offset for `C` +* @param {Int32Array} out - output array for closest centroid indices +* @param {integer} so - stride length for `out` +* @param {NonNegativeInteger} oo - index offset for `out` +* @param {Int32Array} counts - output array for per-centroid assignment counts +* @param {integer} sco - stride length for `counts` +* @param {NonNegativeInteger} oco - index offset for `counts` +* @throws {TypeError} fourth argument must be a supported distance metric +* @returns {Int32Array} output array +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var Int32Array = require( '@stdlib/array/int32' ); +* +* var X = new Float64Array( [ 1.0, 1.0, 5.0, 5.0, 1.5, 1.5 ] ); +* var C = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); +* +* var out = new Int32Array( 3 ); +* var counts = new Int32Array( 2 ); +* +* dclosestCentroids( 3, 2, 2, 'sqeuclidean', X, 2, 1, 0, C, 2, 1, 0, out, 1, 0, counts, 1, 0 ); +* // out => [ 0, 1, 0 ] +*/ +function dclosestCentroids( M, N, k, metric, X, sx1, sx2, ox, C, sc1, sc2, oc, out, so, oo, counts, sco, oco ) { // eslint-disable-line max-len, max-params + var m = resolveMetricStr( metric ); + if ( m === null ) { + throw new TypeError( format( 'invalid argument. Fourth argument must be a supported distance metric. Value: `%s`.', metric ) ); + } + if ( k < 1 || M < 1 || N < 1 ) { + return out; + } + return base( M, N, k, m, X, sx1, sx2, ox, C, sc1, sc2, oc, out, so, oo, counts, sco, oco ); // eslint-disable-line max-len +} + + +// EXPORTS // + +module.exports = dclosestCentroids; diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/lib/ndarray.native.js b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/lib/ndarray.native.js new file mode 100644 index 000000000000..553cc86d6fc2 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/lib/ndarray.native.js @@ -0,0 +1,82 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolveMetricStr = require( '@stdlib/ml/base/kmeans/metric-resolve-str' ); +var resolveMetricEnum = require( '@stdlib/ml/base/kmeans/metric-resolve-enum' ); +var format = require( '@stdlib/string/format' ); +var addon = require( './../src/addon.node' ); + + +// MAIN // + +/** +* Assigns each data point in a double-precision floating-point input matrix to its closest centroid using alternative indexing semantics. +* +* @param {NonNegativeInteger} M - number of data points +* @param {NonNegativeInteger} N - number of features +* @param {NonNegativeInteger} k - number of centroids +* @param {string} metric - distance metric +* @param {Float64Array} X - input data matrix +* @param {integer} sx1 - stride of the first dimension of `X` +* @param {integer} sx2 - stride of the second dimension of `X` +* @param {NonNegativeInteger} ox - index offset for `X` +* @param {Float64Array} C - centroid matrix +* @param {integer} sc1 - stride of the first dimension of `C` +* @param {integer} sc2 - stride of the second dimension of `C` +* @param {NonNegativeInteger} oc - index offset for `C` +* @param {Int32Array} out - output array for closest centroid indices +* @param {integer} so - stride length for `out` +* @param {NonNegativeInteger} oo - index offset for `out` +* @param {Int32Array} counts - output array for per-centroid assignment counts +* @param {integer} sco - stride length for `counts` +* @param {NonNegativeInteger} oco - index offset for `counts` +* @throws {TypeError} fourth argument must be a supported distance metric +* @returns {Int32Array} output array +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var Int32Array = require( '@stdlib/array/int32' ); +* +* var X = new Float64Array( [ 1.0, 1.0, 5.0, 5.0, 1.5, 1.5 ] ); +* var C = new Float64Array( [ 1.0, 1.0, 5.0, 5.0 ] ); +* +* var out = new Int32Array( 3 ); +* var counts = new Int32Array( 2 ); +* +* dclosestCentroids( 3, 2, 2, 'sqeuclidean', X, 2, 1, 0, C, 2, 1, 0, out, 1, 0, counts, 1, 0 ); +* // out => [ 0, 1, 0 ] +*/ +function dclosestCentroids( M, N, k, metric, X, sx1, sx2, ox, C, sc1, sc2, oc, out, so, oo, counts, sco, oco ) { // eslint-disable-line max-len, max-params + if ( resolveMetricStr( metric ) === null ) { + throw new TypeError( format( 'invalid argument. Fourth argument must be a supported distance metric. Value: `%s`.', metric ) ); + } + if ( k < 1 || M < 1 || N < 1 ) { + return out; + } + addon.ndarray( M, N, k, resolveMetricEnum( metric ), X, sx1, sx2, ox, C, sc1, sc2, oc, out, so, oo, counts, sco, oco ); // eslint-disable-line max-len + return out; +} + + +// EXPORTS // + +module.exports = dclosestCentroids; diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/manifest.json b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/manifest.json new file mode 100644 index 000000000000..ed7c833db019 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/manifest.json @@ -0,0 +1,95 @@ +{ + "options": { + "task": "build" + }, + "fields": [ + { + "field": "src", + "resolve": true, + "relative": true + }, + { + "field": "include", + "resolve": true, + "relative": true + }, + { + "field": "libraries", + "resolve": false, + "relative": false + }, + { + "field": "libpath", + "resolve": true, + "relative": false + } + ], + "confs": [ + { + "task": "build", + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/blas/base/shared", + "@stdlib/strided/base/stride2offset", + "@stdlib/ml/base/kmeans/metrics", + "@stdlib/stats/strided/distances/dsquared-euclidean", + "@stdlib/stats/strided/distances/dcorrelation", + "@stdlib/stats/strided/distances/dcosine-distance", + "@stdlib/stats/strided/distances/dcityblock", + "@stdlib/napi/export", + "@stdlib/napi/argv", + "@stdlib/napi/argv-int32", + "@stdlib/napi/argv-int64", + "@stdlib/napi/argv-strided-int32array", + "@stdlib/napi/argv-strided-float64array2d" + ] + }, + { + "task": "benchmark", + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/blas/base/shared", + "@stdlib/strided/base/stride2offset", + "@stdlib/ml/base/kmeans/metrics", + "@stdlib/stats/strided/distances/dsquared-euclidean", + "@stdlib/stats/strided/distances/dcorrelation", + "@stdlib/stats/strided/distances/dcosine-distance", + "@stdlib/stats/strided/distances/dcityblock" + ] + }, + { + "task": "examples", + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/blas/base/shared", + "@stdlib/strided/base/stride2offset", + "@stdlib/ml/base/kmeans/metrics", + "@stdlib/stats/strided/distances/dsquared-euclidean", + "@stdlib/stats/strided/distances/dcorrelation", + "@stdlib/stats/strided/distances/dcosine-distance", + "@stdlib/stats/strided/distances/dcityblock" + ] + } + ] +} diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/package.json b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/package.json new file mode 100644 index 000000000000..17599c728bb7 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/package.json @@ -0,0 +1,78 @@ +{ + "name": "@stdlib/ml/strided/dkmeans-closest-centroids", + "version": "0.0.0", + "description": "Assign each data point in a double-precision floating-point input matrix to its closest centroid.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "browser": "./lib/main.js", + "gypfile": true, + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "include": "./include", + "lib": "./lib", + "src": "./src", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "ml", + "machine learning", + "kmeans", + "k-means", + "clustering", + "cluster", + "centroid", + "closest", + "nearest", + "assign", + "distance", + "metric", + "matrix", + "strided", + "array", + "ndarray", + "double", + "float", + "float64", + "float64array" + ] +} diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/src/Makefile b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/src/Makefile new file mode 100644 index 000000000000..2caf905cedbe --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/src/Makefile @@ -0,0 +1,70 @@ +#/ +# @license Apache-2.0 +# +# Copyright (c) 2026 The Stdlib Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +#/ + +# VARIABLES # + +ifndef VERBOSE + QUIET := @ +else + QUIET := +endif + +# Determine the OS ([1][1], [2][2]). +# +# [1]: https://en.wikipedia.org/wiki/Uname#Examples +# [2]: http://stackoverflow.com/a/27776822/2225624 +OS ?= $(shell uname) +ifneq (, $(findstring MINGW,$(OS))) + OS := WINNT +else +ifneq (, $(findstring MSYS,$(OS))) + OS := WINNT +else +ifneq (, $(findstring CYGWIN,$(OS))) + OS := WINNT +else +ifneq (, $(findstring Windows_NT,$(OS))) + OS := WINNT +endif +endif +endif +endif + + +# RULES # + +#/ +# Removes generated files for building an add-on. +# +# @example +# make clean-addon +#/ +clean-addon: + $(QUIET) -rm -f *.o *.node + +.PHONY: clean-addon + +#/ +# Removes generated files. +# +# @example +# make clean +#/ +clean: clean-addon + +.PHONY: clean diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/src/addon.c b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/src/addon.c new file mode 100644 index 000000000000..7f15d669b3c5 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/src/addon.c @@ -0,0 +1,112 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "stdlib/ml/strided/dkmeans_closest_centroids.h" +#include "stdlib/blas/base/shared.h" +#include "stdlib/napi/export.h" +#include "stdlib/napi/argv.h" +#include "stdlib/napi/argv_int64.h" +#include "stdlib/napi/argv_int32.h" +#include "stdlib/napi/argv_strided_int32array.h" +#include "stdlib/napi/argv_strided_float64array2d.h" +#include + +/** +* Receives JavaScript callback invocation data. +* +* @param env environment under which the function is invoked +* @param info callback data +* @return Node-API value +*/ +static napi_value addon( napi_env env, napi_callback_info info ) { + CBLAS_INT sx1; + CBLAS_INT sx2; + CBLAS_INT sc1; + CBLAS_INT sc2; + + STDLIB_NAPI_ARGV( env, info, argv, argc, 13 ); + + STDLIB_NAPI_ARGV_INT32( env, order, argv, 0 ); + STDLIB_NAPI_ARGV_INT32( env, metric, argv, 4 ); + + STDLIB_NAPI_ARGV_INT64( env, M, argv, 1 ); + STDLIB_NAPI_ARGV_INT64( env, N, argv, 2 ); + STDLIB_NAPI_ARGV_INT64( env, k, argv, 3 ); + STDLIB_NAPI_ARGV_INT64( env, so, argv, 10 ); + STDLIB_NAPI_ARGV_INT64( env, sco, argv, 12 ); + STDLIB_NAPI_ARGV_INT64( env, LDX, argv, 6 ); + STDLIB_NAPI_ARGV_INT64( env, LDC, argv, 8 ); + + if ( order == CblasColMajor ) { + sx1 = 1; + sc1 = 1; + sx2 = LDX; + sc2 = LDC; + } else { // order == CblasRowMajor + sx1 = LDX; + sc1 = LDC; + sx2 = 1; + sc2 = 1; + } + STDLIB_NAPI_ARGV_STRIDED_INT32ARRAY( env, out, M, so, argv, 9 ); + STDLIB_NAPI_ARGV_STRIDED_INT32ARRAY( env, counts, k, sco, argv, 11 ); + STDLIB_NAPI_ARGV_STRIDED_FLOAT64ARRAY2D( env, X, M, N, sx1, sx2, argv, 5 ); + STDLIB_NAPI_ARGV_STRIDED_FLOAT64ARRAY2D( env, C, k, N, sc1, sc2, argv, 7 ); + + API_SUFFIX(stdlib_strided_dclosest_centroids)( order, M, N, k, metric, X, LDX, C, LDC, out, so, counts, sco ); + + return NULL; +} + +/** +* Receives JavaScript callback invocation data. +* +* @param env environment under which the function is invoked +* @param info callback data +* @return Node-API value +*/ +static napi_value addon_method( napi_env env, napi_callback_info info ) { + STDLIB_NAPI_ARGV( env, info, argv, argc, 18 ); + + STDLIB_NAPI_ARGV_INT32( env, metric, argv, 3 ); + + STDLIB_NAPI_ARGV_INT64( env, M, argv, 0 ); + STDLIB_NAPI_ARGV_INT64( env, N, argv, 1 ); + STDLIB_NAPI_ARGV_INT64( env, k, argv, 2 ); + STDLIB_NAPI_ARGV_INT64( env, sx1, argv, 5 ); + STDLIB_NAPI_ARGV_INT64( env, sx2, argv, 6 ); + STDLIB_NAPI_ARGV_INT64( env, ox, argv, 7 ); + STDLIB_NAPI_ARGV_INT64( env, sc1, argv, 9 ); + STDLIB_NAPI_ARGV_INT64( env, sc2, argv, 10 ); + STDLIB_NAPI_ARGV_INT64( env, oc, argv, 11 ); + STDLIB_NAPI_ARGV_INT64( env, so, argv, 13 ); + STDLIB_NAPI_ARGV_INT64( env, oo, argv, 14 ); + STDLIB_NAPI_ARGV_INT64( env, sco, argv, 16 ); + STDLIB_NAPI_ARGV_INT64( env, oco, argv, 17 ); + + STDLIB_NAPI_ARGV_STRIDED_INT32ARRAY( env, out, M, so, argv, 12 ); + STDLIB_NAPI_ARGV_STRIDED_INT32ARRAY( env, counts, k, sco, argv, 15 ); + STDLIB_NAPI_ARGV_STRIDED_FLOAT64ARRAY2D( env, X, M, N, sx1, sx2, argv, 4 ); + STDLIB_NAPI_ARGV_STRIDED_FLOAT64ARRAY2D( env, C, k, N, sc1, sc2, argv, 8 ); + + API_SUFFIX(stdlib_strided_dclosest_centroids_ndarray)( M, N, k, metric, X, sx1, sx2, ox, C, sc1, sc2, oc, out, so, oo, counts, sco, oco ); + + return NULL; +} + +STDLIB_NAPI_MODULE_EXPORT_FCN_WITH_METHOD( addon, "ndarray", addon_method ) diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/src/main.c b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/src/main.c new file mode 100644 index 000000000000..9741730266b4 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/src/main.c @@ -0,0 +1,137 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "stdlib/ml/strided/dkmeans_closest_centroids.h" +#include "stdlib/strided/base/stride2offset.h" +#include "stdlib/blas/base/shared.h" +#include "stdlib/ml/base/kmeans/metrics.h" +#include "stdlib/stats/strided/distances/dsquared_euclidean.h" +#include "stdlib/stats/strided/distances/dcorrelation.h" +#include "stdlib/stats/strided/distances/dcosine_distance.h" +#include "stdlib/stats/strided/distances/dcityblock.h" +#include + +/** +* Data type to store distance metric function. +*/ +typedef double func_type( const CBLAS_INT N, const double *X, const CBLAS_INT sx, const CBLAS_INT ox, const double *Y, const CBLAS_INT strideY, const CBLAS_INT offsetY ); + +/** +* Assigns each data point in a double-precision floating-point input matrix to its closest centroid. +* +* @param order storage layout +* @param M number of data points +* @param N number of features +* @param k number of centroids +* @param metric distance metric +* @param X input data matrix +* @param LDX stride of the first dimension of `X` (a.k.a., leading dimension of the matrix `X`) +* @param C centroid matrix +* @param LDC stride of the first dimension of `C` (a.k.a., leading dimension of the matrix `C`) +* @param out output array for closest centroid indices +* @param so stride length for `out` +* @param counts output array for per-centroid assignment counts +* @param sco stride length for `counts` +*/ +void API_SUFFIX(stdlib_strided_dclosest_centroids)( const CBLAS_LAYOUT order, const CBLAS_INT M, const CBLAS_INT N, const CBLAS_INT k, const enum STDLIB_ML_KMEANS_METRIC metric, const double *X, const CBLAS_INT LDX, const double *C, const CBLAS_INT LDC, CBLAS_INT *out, const CBLAS_INT so, CBLAS_INT *counts, const CBLAS_INT sco ) { + CBLAS_INT sx1; + CBLAS_INT sx2; + CBLAS_INT sc1; + CBLAS_INT sc2; + CBLAS_INT oco; + CBLAS_INT oo; + + if ( order == CblasColMajor ) { + sx1 = 1; + sc1 = 1; + sx2 = LDX; + sc2 = LDC; + } else { // order == CblasRowMajor + sx1 = LDX; + sc1 = LDC; + sx2 = 1; + sc2 = 1; + } + oo = stdlib_strided_stride2offset( M, so ); + oco = stdlib_strided_stride2offset( k, sco ); + API_SUFFIX(stdlib_strided_dclosest_centroids_ndarray)( M, N, k, metric, X, sx1, sx2, 0, C, sc1, sc2, 0, out, so, oo, counts, sco, oco ); +} + +/** +* Assigns each data point in a double-precision floating-point input matrix to its closest centroid using alternative indexing semantics. +* +* @param M number of data points +* @param N number of features +* @param k number of centroids +* @param metric distance metric +* @param X input data matrix +* @param sx1 stride of the first dimension of `X` +* @param sx2 stride of the second dimension of `X` +* @param ox index offset for `X` +* @param C centroid matrix +* @param sc1 stride of the first dimension of `C` +* @param sc2 stride of the second dimension of `C` +* @param oc index offset for `C` +* @param out output array for closest centroid indices +* @param so stride length for `out` +* @param oo index offset for `out` +* @param counts output array for per-centroid assignment counts +* @param sco stride length for `counts` +* @param oco index offset for `counts` +*/ +void API_SUFFIX(stdlib_strided_dclosest_centroids_ndarray)( const CBLAS_INT M, const CBLAS_INT N, const CBLAS_INT k, const enum STDLIB_ML_KMEANS_METRIC metric, const double *X, const CBLAS_INT sx1, const CBLAS_INT sx2, const CBLAS_INT ox, const double *C, const CBLAS_INT sc1, const CBLAS_INT sc2, const CBLAS_INT oc, CBLAS_INT *out, const CBLAS_INT so, const CBLAS_INT oo, CBLAS_INT *counts, const CBLAS_INT sco, const CBLAS_INT oco ) { + double bestDist; + CBLAS_INT best; + CBLAS_INT xidx; + CBLAS_INT oidx; + func_type *f; + CBLAS_INT i; + CBLAS_INT j; + double d; + + if ( metric == STDLIB_ML_KMEANS_SQEUCLIDEAN ) { + f = (func_type *) stdlib_strided_dsquared_euclidean_ndarray; + } else if ( metric == STDLIB_ML_KMEANS_CORRELATION ) { + f = (func_type *) stdlib_strided_dcorrelation_ndarray; + } else if ( metric == STDLIB_ML_KMEANS_CITYBLOCK ) { + f = (func_type *) stdlib_strided_dcityblock_ndarray; + } else { + f = (func_type *) stdlib_strided_dcosine_distance_ndarray; + } + + xidx = ox; + for ( i = 0; i < M; i++ ) { + oidx = oc; + best = 0; + bestDist = f( N, X, sx2, xidx, C, sc2, oidx ); + + oidx += sc1; // move to the next centroid + for ( j = 1; j < k; j++ ) { + d = f( N, X, sx2, xidx, C, sc2, oidx ); + if ( d < bestDist ) { + bestDist = d; + best = j; + } + oidx += sc1; + } + + out[ oo + ( i*so ) ] = best; + counts[ oco + ( sco*best ) ] += 1; + xidx += sx1; + } +} diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/fixtures/column_major.json b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/fixtures/column_major.json new file mode 100644 index 000000000000..8b14ec02faef --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/fixtures/column_major.json @@ -0,0 +1,33 @@ +{ + "order": "column-major", + "metric": "sqeuclidean", + "M": 4, + "N": 2, + "k": 2, + "X": [ 1.0, 8.0, 2.0, 0.0, 2.0, 9.0, 3.0, 1.0 ], + "strideX1": 1, + "strideX2": 4, + "offsetX": 0, + "LDX": 4, + "X_mat": [ + [ 1.0, 2.0 ], + [ 8.0, 9.0 ], + [ 2.0, 3.0 ], + [ 0.0, 1.0 ] + ], + "C": [ 1.0, 8.0, 2.0, 9.0 ], + "strideC1": 1, + "strideC2": 2, + "offsetC": 0, + "LDC": 2, + "C_mat": [ + [ 1.0, 2.0 ], + [ 8.0, 9.0 ] + ], + "strideO": 1, + "offsetO": 0, + "strideCounts": 1, + "offsetCounts": 0, + "expected_out": [ 0, 1, 0, 0 ], + "expected_counts": [ 3, 1 ] +} diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/fixtures/large-strides/column_major.json b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/fixtures/large-strides/column_major.json new file mode 100644 index 000000000000..1d3b83ce994a --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/fixtures/large-strides/column_major.json @@ -0,0 +1,31 @@ +{ + "order": "column-major", + "metric": "sqeuclidean", + "M": 4, + "N": 2, + "k": 2, + "X": [ 1.0, 99.0, 8.0, 99.0, 2.0, 99.0, 0.0, 99.0, 2.0, 99.0, 9.0, 99.0, 3.0, 99.0, 1.0, 99.0 ], + "strideX1": 2, + "strideX2": 8, + "offsetX": 0, + "X_mat": [ + [ 1.0, 2.0 ], + [ 8.0, 9.0 ], + [ 2.0, 3.0 ], + [ 0.0, 1.0 ] + ], + "C": [ 1.0, 99.0, 8.0, 99.0, 2.0, 99.0, 9.0, 99.0 ], + "strideC1": 2, + "strideC2": 4, + "offsetC": 0, + "C_mat": [ + [ 1.0, 2.0 ], + [ 8.0, 9.0 ] + ], + "strideO": 1, + "offsetO": 0, + "strideCounts": 1, + "offsetCounts": 0, + "expected_out": [ 0, 1, 0, 0 ], + "expected_counts": [ 3, 1 ] +} diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/fixtures/large-strides/row_major.json b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/fixtures/large-strides/row_major.json new file mode 100644 index 000000000000..98930240b080 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/fixtures/large-strides/row_major.json @@ -0,0 +1,31 @@ +{ + "order": "row-major", + "metric": "sqeuclidean", + "M": 4, + "N": 2, + "k": 2, + "X": [ 1.0, 99.0, 2.0, 99.0, 8.0, 99.0, 9.0, 99.0, 2.0, 99.0, 3.0, 99.0, 0.0, 99.0, 1.0, 99.0 ], + "strideX1": 4, + "strideX2": 2, + "offsetX": 0, + "X_mat": [ + [ 1.0, 2.0 ], + [ 8.0, 9.0 ], + [ 2.0, 3.0 ], + [ 0.0, 1.0 ] + ], + "C": [ 1.0, 99.0, 2.0, 99.0, 8.0, 99.0, 9.0, 99.0 ], + "strideC1": 4, + "strideC2": 2, + "offsetC": 0, + "C_mat": [ + [ 1.0, 2.0 ], + [ 8.0, 9.0 ] + ], + "strideO": 1, + "offsetO": 0, + "strideCounts": 1, + "offsetCounts": 0, + "expected_out": [ 0, 1, 0, 0 ], + "expected_counts": [ 3, 1 ] +} diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/fixtures/mixed-strides/column_major.json b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/fixtures/mixed-strides/column_major.json new file mode 100644 index 000000000000..4a904bcc1bfc --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/fixtures/mixed-strides/column_major.json @@ -0,0 +1,31 @@ +{ + "order": "column-major", + "metric": "sqeuclidean", + "M": 4, + "N": 2, + "k": 2, + "X": [ 1.0, 8.0, 2.0, 0.0, 2.0, 9.0, 3.0, 1.0 ], + "strideX1": 1, + "strideX2": -4, + "offsetX": 4, + "X_mat": [ + [ 1.0, 2.0 ], + [ 8.0, 9.0 ], + [ 2.0, 3.0 ], + [ 0.0, 1.0 ] + ], + "C": [ 1.0, 8.0, 2.0, 9.0 ], + "strideC1": 1, + "strideC2": -2, + "offsetC": 2, + "C_mat": [ + [ 1.0, 2.0 ], + [ 8.0, 9.0 ] + ], + "strideO": 1, + "offsetO": 0, + "strideCounts": 1, + "offsetCounts": 0, + "expected_out": [ 0, 1, 0, 0 ], + "expected_counts": [ 3, 1 ] +} diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/fixtures/mixed-strides/row_major.json b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/fixtures/mixed-strides/row_major.json new file mode 100644 index 000000000000..7fd093f2f6d8 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/fixtures/mixed-strides/row_major.json @@ -0,0 +1,31 @@ +{ + "order": "row-major", + "metric": "sqeuclidean", + "M": 4, + "N": 2, + "k": 2, + "X": [ 1.0, 2.0, 8.0, 9.0, 2.0, 3.0, 0.0, 1.0 ], + "strideX1": -2, + "strideX2": 1, + "offsetX": 6, + "X_mat": [ + [ 1.0, 2.0 ], + [ 8.0, 9.0 ], + [ 2.0, 3.0 ], + [ 0.0, 1.0 ] + ], + "C": [ 1.0, 2.0, 8.0, 9.0 ], + "strideC1": 2, + "strideC2": 1, + "offsetC": 0, + "C_mat": [ + [ 1.0, 2.0 ], + [ 8.0, 9.0 ] + ], + "strideO": 1, + "offsetO": 0, + "strideCounts": 1, + "offsetCounts": 0, + "expected_out": [ 0, 0, 1, 0 ], + "expected_counts": [ 3, 1 ] +} diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/fixtures/negative-strides/column_major.json b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/fixtures/negative-strides/column_major.json new file mode 100644 index 000000000000..39551a2510d5 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/fixtures/negative-strides/column_major.json @@ -0,0 +1,31 @@ +{ + "order": "column-major", + "metric": "sqeuclidean", + "M": 4, + "N": 2, + "k": 2, + "X": [ 1.0, 8.0, 2.0, 0.0, 2.0, 9.0, 3.0, 1.0 ], + "strideX1": -1, + "strideX2": 4, + "offsetX": 3, + "X_mat": [ + [ 1.0, 2.0 ], + [ 8.0, 9.0 ], + [ 2.0, 3.0 ], + [ 0.0, 1.0 ] + ], + "C": [ 1.0, 8.0, 2.0, 9.0 ], + "strideC1": 1, + "strideC2": 2, + "offsetC": 0, + "C_mat": [ + [ 1.0, 2.0 ], + [ 8.0, 9.0 ] + ], + "strideO": -1, + "offsetO": 3, + "strideCounts": 1, + "offsetCounts": 0, + "expected_out": [ 0, 1, 0, 0 ], + "expected_counts": [ 3, 1 ] +} diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/fixtures/negative-strides/row_major.json b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/fixtures/negative-strides/row_major.json new file mode 100644 index 000000000000..1d10ac41404c --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/fixtures/negative-strides/row_major.json @@ -0,0 +1,31 @@ +{ + "order": "row-major", + "metric": "sqeuclidean", + "M": 4, + "N": 2, + "k": 2, + "X": [ 1.0, 2.0, 8.0, 9.0, 2.0, 3.0, 0.0, 1.0 ], + "strideX1": -2, + "strideX2": 1, + "offsetX": 6, + "X_mat": [ + [ 1.0, 2.0 ], + [ 8.0, 9.0 ], + [ 2.0, 3.0 ], + [ 0.0, 1.0 ] + ], + "C": [ 1.0, 2.0, 8.0, 9.0 ], + "strideC1": 2, + "strideC2": 1, + "offsetC": 0, + "C_mat": [ + [ 1.0, 2.0 ], + [ 8.0, 9.0 ] + ], + "strideO": -1, + "offsetO": 3, + "strideCounts": 1, + "offsetCounts": 0, + "expected_out": [ 0, 1, 0, 0 ], + "expected_counts": [ 3, 1 ] +} diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/fixtures/offsets/column_major.json b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/fixtures/offsets/column_major.json new file mode 100644 index 000000000000..33f798fe7360 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/fixtures/offsets/column_major.json @@ -0,0 +1,31 @@ +{ + "order": "column-major", + "metric": "sqeuclidean", + "M": 4, + "N": 2, + "k": 2, + "X": [ 99.0, 1.0, 8.0, 2.0, 0.0, 2.0, 9.0, 3.0, 1.0 ], + "strideX1": 1, + "strideX2": 4, + "offsetX": 1, + "X_mat": [ + [ 1.0, 2.0 ], + [ 8.0, 9.0 ], + [ 2.0, 3.0 ], + [ 0.0, 1.0 ] + ], + "C": [ 99.0, 1.0, 8.0, 2.0, 9.0 ], + "strideC1": 1, + "strideC2": 2, + "offsetC": 1, + "C_mat": [ + [ 1.0, 2.0 ], + [ 8.0, 9.0 ] + ], + "strideO": 1, + "offsetO": 0, + "strideCounts": 1, + "offsetCounts": 0, + "expected_out": [ 0, 1, 0, 0 ], + "expected_counts": [ 3, 1 ] +} diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/fixtures/offsets/row_major.json b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/fixtures/offsets/row_major.json new file mode 100644 index 000000000000..7d56bf1d74fb --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/fixtures/offsets/row_major.json @@ -0,0 +1,31 @@ +{ + "order": "row-major", + "metric": "sqeuclidean", + "M": 4, + "N": 2, + "k": 2, + "X": [ 99.0, 99.0, 1.0, 2.0, 8.0, 9.0, 2.0, 3.0, 0.0, 1.0 ], + "strideX1": 2, + "strideX2": 1, + "offsetX": 2, + "X_mat": [ + [ 1.0, 2.0 ], + [ 8.0, 9.0 ], + [ 2.0, 3.0 ], + [ 0.0, 1.0 ] + ], + "C": [ 99.0, 99.0, 1.0, 2.0, 8.0, 9.0 ], + "strideC1": 2, + "strideC2": 1, + "offsetC": 2, + "C_mat": [ + [ 1.0, 2.0 ], + [ 8.0, 9.0 ] + ], + "strideO": 1, + "offsetO": 0, + "strideCounts": 1, + "offsetCounts": 0, + "expected_out": [ 0, 1, 0, 0 ], + "expected_counts": [ 3, 1 ] +} diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/fixtures/row_major.json b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/fixtures/row_major.json new file mode 100644 index 000000000000..b6d1f0503245 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/fixtures/row_major.json @@ -0,0 +1,33 @@ +{ + "order": "row-major", + "metric": "sqeuclidean", + "M": 4, + "N": 2, + "k": 2, + "X": [ 1.0, 2.0, 8.0, 9.0, 2.0, 3.0, 0.0, 1.0 ], + "strideX1": 2, + "strideX2": 1, + "offsetX": 0, + "LDX": 2, + "X_mat": [ + [ 1.0, 2.0 ], + [ 8.0, 9.0 ], + [ 2.0, 3.0 ], + [ 0.0, 1.0 ] + ], + "C": [ 1.0, 2.0, 8.0, 9.0 ], + "strideC1": 2, + "strideC2": 1, + "offsetC": 0, + "LDC": 2, + "C_mat": [ + [ 1.0, 2.0 ], + [ 8.0, 9.0 ] + ], + "strideO": 1, + "offsetO": 0, + "strideCounts": 1, + "offsetCounts": 0, + "expected_out": [ 0, 1, 0, 0 ], + "expected_counts": [ 3, 1 ] +} diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/test.dkmeans_closest_centroids.js b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/test.dkmeans_closest_centroids.js new file mode 100644 index 000000000000..d40d20f9df14 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/test.dkmeans_closest_centroids.js @@ -0,0 +1,264 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Int32Array = require( '@stdlib/array/int32' ); +var dclosestCentroids = require( './../lib/dkmeans_closest_centroids.js' ); + + +// FIXTURES // + +var ROW_MAJOR_DATA = require( './fixtures/row_major.json' ); +var COLUMN_MAJOR_DATA = require( './fixtures/column_major.json' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof dclosestCentroids, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 13', function test( t ) { + t.strictEqual( dclosestCentroids.length, 13, 'returns expected value' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not a valid order', function test( t ) { + var values; + var data; + var i; + + data = ROW_MAJOR_DATA; + + values = [ + 'foo', + 'bar', + 'beep', + 'boop', + -5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + dclosestCentroids( value, data.M, data.N, data.k, data.metric, new Float64Array( data.X ), data.LDX, new Float64Array( data.C ), data.LDC, new Int32Array( data.M ), 1, new Int32Array( data.k ), 1 ); + }; + } +}); + +tape( 'the function throws an error if provided a fifth argument which is not a supported distance metric', function test( t ) { + var values; + var data; + var i; + + data = ROW_MAJOR_DATA; + + values = [ + 'foo', + 'bar', + 'beep', + 'boop', + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + dclosestCentroids( data.order, data.M, data.N, data.k, value, new Float64Array( data.X ), data.LDX, new Float64Array( data.C ), data.LDC, new Int32Array( data.M ), 1, new Int32Array( data.k ), 1 ); + }; + } +}); + +tape( 'the function throws an error if provided a seventh argument which is not a valid `LDX` value (row-major)', function test( t ) { + var values; + var data; + var i; + + data = ROW_MAJOR_DATA; + + values = [ + 0, + 1 + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + dclosestCentroids( data.order, data.M, data.N, data.k, data.metric, new Float64Array( data.X ), value, new Float64Array( data.C ), data.LDC, new Int32Array( data.M ), 1, new Int32Array( data.k ), 1 ); + }; + } +}); + +tape( 'the function throws an error if provided a seventh argument which is not a valid `LDX` value (column-major)', function test( t ) { + var values; + var data; + var i; + + data = COLUMN_MAJOR_DATA; + + values = [ + 0, + 1 + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + dclosestCentroids( data.order, data.M, data.N, data.k, data.metric, new Float64Array( data.X ), value, new Float64Array( data.C ), data.LDC, new Int32Array( data.M ), 1, new Int32Array( data.k ), 1 ); + }; + } +}); + +tape( 'the function throws an error if provided a ninth argument which is not a valid `LDC` value (row-major)', function test( t ) { + var values; + var data; + var i; + + data = ROW_MAJOR_DATA; + + values = [ + 0, + 1 + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + dclosestCentroids( data.order, data.M, data.N, data.k, data.metric, new Float64Array( data.X ), data.LDX, new Float64Array( data.C ), value, new Int32Array( data.M ), 1, new Int32Array( data.k ), 1 ); + }; + } +}); + +tape( 'the function throws an error if provided a ninth argument which is not a valid `LDC` value (column-major)', function test( t ) { + var values; + var data; + var i; + + data = COLUMN_MAJOR_DATA; + + values = [ + 0, + 1 + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + dclosestCentroids( data.order, data.M, data.N, data.k, data.metric, new Float64Array( data.X ), data.LDX, new Float64Array( data.C ), value, new Int32Array( data.M ), 1, new Int32Array( data.k ), 1 ); + }; + } +}); + +tape( 'the function assigns each data point to its closest centroid (row-major)', function test( t ) { + var counts; + var data; + var out; + + data = ROW_MAJOR_DATA; + out = new Int32Array( data.expected_out.length ); + counts = new Int32Array( data.expected_counts.length ); + + dclosestCentroids( data.order, data.M, data.N, data.k, data.metric, new Float64Array( data.X ), data.LDX, new Float64Array( data.C ), data.LDC, out, data.strideO, counts, data.strideCounts ); + + t.deepEqual( out, new Int32Array( data.expected_out ), 'returns expected assignments' ); + t.deepEqual( counts, new Int32Array( data.expected_counts ), 'returns expected counts' ); + t.end(); +}); + +tape( 'the function assigns each data point to its closest centroid (column-major)', function test( t ) { + var counts; + var data; + var out; + + data = COLUMN_MAJOR_DATA; + out = new Int32Array( data.expected_out.length ); + counts = new Int32Array( data.expected_counts.length ); + + dclosestCentroids( data.order, data.M, data.N, data.k, data.metric, new Float64Array( data.X ), data.LDX, new Float64Array( data.C ), data.LDC, out, data.strideO, counts, data.strideCounts ); + + t.deepEqual( out, new Int32Array( data.expected_out ), 'returns expected assignments' ); + t.deepEqual( counts, new Int32Array( data.expected_counts ), 'returns expected counts' ); + t.end(); +}); + +tape( 'the function supports all documented distance metrics', function test( t ) { + var metrics; + var counts; + var out; + var X; + var C; + var i; + + metrics = [ 'sqeuclidean', 'cosine', 'cityblock', 'correlation' ]; + + X = new Float64Array( [ 1.0, 3.0, 5.0, 5.0, 1.5, 4.0 ] ); + C = new Float64Array( [ 1.0, 3.0, 5.0, 5.0 ] ); + + for ( i = 0; i < metrics.length; i++ ) { + out = new Int32Array( 3 ); + counts = new Int32Array( 2 ); + dclosestCentroids( 'row-major', 3, 2, 2, metrics[ i ], X, 2, C, 2, out, 1, counts, 1 ); + t.strictEqual( out.length, 3, 'returns an output array of expected length for metric: ' + metrics[ i ] ); + } + t.end(); +}); diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/test.dkmeans_closest_centroids.native.js b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/test.dkmeans_closest_centroids.native.js new file mode 100644 index 000000000000..b2a0efac2515 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/test.dkmeans_closest_centroids.native.js @@ -0,0 +1,273 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var tape = require( 'tape' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Int32Array = require( '@stdlib/array/int32' ); +var tryRequire = require( '@stdlib/utils/try-require' ); + + +// FIXTURES // + +var ROW_MAJOR_DATA = require( './fixtures/row_major.json' ); +var COLUMN_MAJOR_DATA = require( './fixtures/column_major.json' ); + + +// VARIABLES // + +var dclosestCentroids = tryRequire( resolve( __dirname, './../lib/dkmeans_closest_centroids.native.js' ) ); +var opts = { + 'skip': ( dclosestCentroids instanceof Error ) +}; + + +// TESTS // + +tape( 'main export is a function', opts, function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof dclosestCentroids, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 13', opts, function test( t ) { + t.strictEqual( dclosestCentroids.length, 13, 'returns expected value' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not a valid order', opts, function test( t ) { + var values; + var data; + var i; + + data = ROW_MAJOR_DATA; + + values = [ + 'foo', + 'bar', + 'beep', + 'boop', + -5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + dclosestCentroids( value, data.M, data.N, data.k, data.metric, new Float64Array( data.X ), data.LDX, new Float64Array( data.C ), data.LDC, new Int32Array( data.M ), 1, new Int32Array( data.k ), 1 ); + }; + } +}); + +tape( 'the function throws an error if provided a fifth argument which is not a supported distance metric', opts, function test( t ) { + var values; + var data; + var i; + + data = ROW_MAJOR_DATA; + + values = [ + 'foo', + 'bar', + 'beep', + 'boop', + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + dclosestCentroids( data.order, data.M, data.N, data.k, value, new Float64Array( data.X ), data.LDX, new Float64Array( data.C ), data.LDC, new Int32Array( data.M ), 1, new Int32Array( data.k ), 1 ); + }; + } +}); + +tape( 'the function throws an error if provided a seventh argument which is not a valid `LDX` value (row-major)', opts, function test( t ) { + var values; + var data; + var i; + + data = ROW_MAJOR_DATA; + + values = [ + 0, + 1 + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + dclosestCentroids( data.order, data.M, data.N, data.k, data.metric, new Float64Array( data.X ), value, new Float64Array( data.C ), data.LDC, new Int32Array( data.M ), 1, new Int32Array( data.k ), 1 ); + }; + } +}); + +tape( 'the function throws an error if provided a seventh argument which is not a valid `LDX` value (column-major)', opts, function test( t ) { + var values; + var data; + var i; + + data = COLUMN_MAJOR_DATA; + + values = [ + 0, + 1 + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + dclosestCentroids( data.order, data.M, data.N, data.k, data.metric, new Float64Array( data.X ), value, new Float64Array( data.C ), data.LDC, new Int32Array( data.M ), 1, new Int32Array( data.k ), 1 ); + }; + } +}); + +tape( 'the function throws an error if provided a ninth argument which is not a valid `LDC` value (row-major)', opts, function test( t ) { + var values; + var data; + var i; + + data = ROW_MAJOR_DATA; + + values = [ + 0, + 1 + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + dclosestCentroids( data.order, data.M, data.N, data.k, data.metric, new Float64Array( data.X ), data.LDX, new Float64Array( data.C ), value, new Int32Array( data.M ), 1, new Int32Array( data.k ), 1 ); + }; + } +}); + +tape( 'the function throws an error if provided a ninth argument which is not a valid `LDC` value (column-major)', opts, function test( t ) { + var values; + var data; + var i; + + data = COLUMN_MAJOR_DATA; + + values = [ + 0, + 1 + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + dclosestCentroids( data.order, data.M, data.N, data.k, data.metric, new Float64Array( data.X ), data.LDX, new Float64Array( data.C ), value, new Int32Array( data.M ), 1, new Int32Array( data.k ), 1 ); + }; + } +}); + +tape( 'the function assigns each data point to its closest centroid (row-major)', opts, function test( t ) { + var counts; + var data; + var out; + + data = ROW_MAJOR_DATA; + out = new Int32Array( data.expected_out.length ); + counts = new Int32Array( data.expected_counts.length ); + + dclosestCentroids( data.order, data.M, data.N, data.k, data.metric, new Float64Array( data.X ), data.LDX, new Float64Array( data.C ), data.LDC, out, data.strideO, counts, data.strideCounts ); + + t.deepEqual( out, new Int32Array( data.expected_out ), 'returns expected assignments' ); + t.deepEqual( counts, new Int32Array( data.expected_counts ), 'returns expected counts' ); + t.end(); +}); + +tape( 'the function assigns each data point to its closest centroid (column-major)', opts, function test( t ) { + var counts; + var data; + var out; + + data = COLUMN_MAJOR_DATA; + out = new Int32Array( data.expected_out.length ); + counts = new Int32Array( data.expected_counts.length ); + + dclosestCentroids( data.order, data.M, data.N, data.k, data.metric, new Float64Array( data.X ), data.LDX, new Float64Array( data.C ), data.LDC, out, data.strideO, counts, data.strideCounts ); + + t.deepEqual( out, new Int32Array( data.expected_out ), 'returns expected assignments' ); + t.deepEqual( counts, new Int32Array( data.expected_counts ), 'returns expected counts' ); + t.end(); +}); + +tape( 'the function supports all documented distance metrics', opts, function test( t ) { + var metrics; + var counts; + var out; + var X; + var C; + var i; + + metrics = [ 'sqeuclidean', 'cosine', 'cityblock', 'correlation' ]; + + X = new Float64Array( [ 1.0, 3.0, 5.0, 5.0, 1.5, 4.0 ] ); + C = new Float64Array( [ 1.0, 3.0, 5.0, 5.0 ] ); + + for ( i = 0; i < metrics.length; i++ ) { + out = new Int32Array( 3 ); + counts = new Int32Array( 2 ); + dclosestCentroids( 'row-major', 3, 2, 2, metrics[ i ], X, 2, C, 2, out, 1, counts, 1 ); + t.strictEqual( out.length, 3, 'returns an output array of expected length for metric: ' + metrics[ i ] ); + } + t.end(); +}); diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/test.js b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/test.js new file mode 100644 index 000000000000..98511059e1b3 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/test.js @@ -0,0 +1,82 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var dclosestCentroids = require( './../lib' ); + + +// VARIABLES // + +var opts = { + 'skip': IS_BROWSER +}; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof dclosestCentroids, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method providing an ndarray interface', function test( t ) { + t.strictEqual( typeof dclosestCentroids.ndarray, 'function', 'method is a function' ); + t.end(); +}); + +tape( 'if a native implementation is available, the main export is the native implementation', opts, function test( t ) { + var dclosestCentroids = proxyquire( './../lib', { + '@stdlib/utils/try-require': tryRequire + }); + + t.strictEqual( dclosestCentroids, mock, 'returns expected value' ); + t.end(); + + function tryRequire() { + return mock; + } + + function mock() { + // Mock... + } +}); + +tape( 'if a native implementation is not available, the main export is a JavaScript implementation', opts, function test( t ) { + var dclosestCentroids; + var main; + + main = require( './../lib/dkmeans_closest_centroids.js' ); + + dclosestCentroids = proxyquire( './../lib', { + '@stdlib/utils/try-require': tryRequire + }); + + t.strictEqual( dclosestCentroids, main, 'returns expected value' ); + t.end(); + + function tryRequire() { + return new Error( 'Cannot find module' ); + } +}); diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/test.ndarray.js b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/test.ndarray.js new file mode 100644 index 000000000000..31c33382b778 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/test.ndarray.js @@ -0,0 +1,277 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Int32Array = require( '@stdlib/array/int32' ); +var dclosestCentroids = require( './../lib/ndarray.js' ); + + +// FIXTURES // + +var ROW_MAJOR_DATA = require( './fixtures/row_major.json' ); +var COLUMN_MAJOR_DATA = require( './fixtures/column_major.json' ); +var OFFSET_ROW_MAJOR_DATA = require( './fixtures/offsets/row_major.json' ); +var OFFSET_COLUMN_MAJOR_DATA = require( './fixtures/offsets/column_major.json' ); +var NEGATIVE_STRIDES_ROW_MAJOR_DATA = require( './fixtures/negative-strides/row_major.json' ); +var NEGATIVE_STRIDES_COLUMN_MAJOR_DATA = require( './fixtures/negative-strides/column_major.json' ); +var MIXED_STRIDES_ROW_MAJOR_DATA = require( './fixtures/mixed-strides/row_major.json' ); +var MIXED_STRIDES_COLUMN_MAJOR_DATA = require( './fixtures/mixed-strides/column_major.json' ); +var LARGE_STRIDES_ROW_MAJOR_DATA = require( './fixtures/large-strides/row_major.json' ); +var LARGE_STRIDES_COLUMN_MAJOR_DATA = require( './fixtures/large-strides/column_major.json' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof dclosestCentroids, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 18', function test( t ) { + t.strictEqual( dclosestCentroids.length, 18, 'returns expected value' ); + t.end(); +}); + +tape( 'the function throws an error if provided a fourth argument which is not a supported distance metric', function test( t ) { + var values; + var data; + var i; + + data = ROW_MAJOR_DATA; + + values = [ + 'foo', + 'bar', + 'beep', + 'boop', + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + dclosestCentroids( data.M, data.N, data.k, value, new Float64Array( data.X ), data.strideX1, data.strideX2, data.offsetX, new Float64Array( data.C ), data.strideC1, data.strideC2, data.offsetC, new Int32Array( data.M ), 1, 0, new Int32Array( data.k ), 1, 0 ); + }; + } +}); + +tape( 'the function returns the output array unchanged if `M`, `N`, or `k` is less than 1', function test( t ) { + var counts; + var data; + var out; + + data = ROW_MAJOR_DATA; + + out = new Int32Array( data.expected_out.length ); + counts = new Int32Array( data.expected_counts.length ); + + t.strictEqual( dclosestCentroids( 0, data.N, data.k, data.metric, new Float64Array( data.X ), data.strideX1, data.strideX2, data.offsetX, new Float64Array( data.C ), data.strideC1, data.strideC2, data.offsetC, out, data.strideO, data.offsetO, counts, data.strideCounts, data.offsetCounts ), out, 'returns expected value' ); + t.strictEqual( dclosestCentroids( data.M, 0, data.k, data.metric, new Float64Array( data.X ), data.strideX1, data.strideX2, data.offsetX, new Float64Array( data.C ), data.strideC1, data.strideC2, data.offsetC, out, data.strideO, data.offsetO, counts, data.strideCounts, data.offsetCounts ), out, 'returns expected value' ); + t.strictEqual( dclosestCentroids( data.M, data.N, 0, data.metric, new Float64Array( data.X ), data.strideX1, data.strideX2, data.offsetX, new Float64Array( data.C ), data.strideC1, data.strideC2, data.offsetC, out, data.strideO, data.offsetO, counts, data.strideCounts, data.offsetCounts ), out, 'returns expected value' ); + + t.deepEqual( out, new Int32Array( data.expected_out.length ), 'leaves output unchanged' ); + t.deepEqual( counts, new Int32Array( data.expected_counts.length ), 'leaves counts unchanged' ); + + t.end(); +}); + +tape( 'the function assigns each data point to its closest centroid (row-major)', function test( t ) { + var counts; + var data; + var out; + + data = ROW_MAJOR_DATA; + + out = new Int32Array( data.expected_out.length ); + counts = new Int32Array( data.expected_counts.length ); + + dclosestCentroids( data.M, data.N, data.k, data.metric, new Float64Array( data.X ), data.strideX1, data.strideX2, data.offsetX, new Float64Array( data.C ), data.strideC1, data.strideC2, data.offsetC, out, data.strideO, data.offsetO, counts, data.strideCounts, data.offsetCounts ); + + t.deepEqual( out, new Int32Array( data.expected_out ), 'returns expected assignments' ); + t.deepEqual( counts, new Int32Array( data.expected_counts ), 'returns expected counts' ); + t.end(); +}); + +tape( 'the function assigns each data point to its closest centroid (column-major)', function test( t ) { + var counts; + var data; + var out; + + data = COLUMN_MAJOR_DATA; + + out = new Int32Array( data.expected_out.length ); + counts = new Int32Array( data.expected_counts.length ); + + dclosestCentroids( data.M, data.N, data.k, data.metric, new Float64Array( data.X ), data.strideX1, data.strideX2, data.offsetX, new Float64Array( data.C ), data.strideC1, data.strideC2, data.offsetC, out, data.strideO, data.offsetO, counts, data.strideCounts, data.offsetCounts ); + + t.deepEqual( out, new Int32Array( data.expected_out ), 'returns expected assignments' ); + t.deepEqual( counts, new Int32Array( data.expected_counts ), 'returns expected counts' ); + t.end(); +}); + +tape( 'the function assigns each data point to its closest centroid (row-major, offsets)', function test( t ) { + var counts; + var data; + var out; + + data = OFFSET_ROW_MAJOR_DATA; + + out = new Int32Array( data.expected_out.length ); + counts = new Int32Array( data.expected_counts.length ); + + dclosestCentroids( data.M, data.N, data.k, data.metric, new Float64Array( data.X ), data.strideX1, data.strideX2, data.offsetX, new Float64Array( data.C ), data.strideC1, data.strideC2, data.offsetC, out, data.strideO, data.offsetO, counts, data.strideCounts, data.offsetCounts ); + + t.deepEqual( out, new Int32Array( data.expected_out ), 'returns expected assignments' ); + t.deepEqual( counts, new Int32Array( data.expected_counts ), 'returns expected counts' ); + t.end(); +}); + +tape( 'the function assigns each data point to its closest centroid (column-major, offsets)', function test( t ) { + var counts; + var data; + var out; + + data = OFFSET_COLUMN_MAJOR_DATA; + + out = new Int32Array( data.expected_out.length ); + counts = new Int32Array( data.expected_counts.length ); + + dclosestCentroids( data.M, data.N, data.k, data.metric, new Float64Array( data.X ), data.strideX1, data.strideX2, data.offsetX, new Float64Array( data.C ), data.strideC1, data.strideC2, data.offsetC, out, data.strideO, data.offsetO, counts, data.strideCounts, data.offsetCounts ); + + t.deepEqual( out, new Int32Array( data.expected_out ), 'returns expected assignments' ); + t.deepEqual( counts, new Int32Array( data.expected_counts ), 'returns expected counts' ); + t.end(); +}); + +tape( 'the function assigns each data point to its closest centroid (row-major, negative strides)', function test( t ) { + var counts; + var data; + var out; + + data = NEGATIVE_STRIDES_ROW_MAJOR_DATA; + + out = new Int32Array( data.expected_out.length ); + counts = new Int32Array( data.expected_counts.length ); + + dclosestCentroids( data.M, data.N, data.k, data.metric, new Float64Array( data.X ), data.strideX1, data.strideX2, data.offsetX, new Float64Array( data.C ), data.strideC1, data.strideC2, data.offsetC, out, data.strideO, data.offsetO, counts, data.strideCounts, data.offsetCounts ); + + t.deepEqual( out, new Int32Array( data.expected_out ), 'returns expected assignments' ); + t.deepEqual( counts, new Int32Array( data.expected_counts ), 'returns expected counts' ); + t.end(); +}); + +tape( 'the function assigns each data point to its closest centroid (column-major, negative strides)', function test( t ) { + var counts; + var data; + var out; + + data = NEGATIVE_STRIDES_COLUMN_MAJOR_DATA; + + out = new Int32Array( data.expected_out.length ); + counts = new Int32Array( data.expected_counts.length ); + + dclosestCentroids( data.M, data.N, data.k, data.metric, new Float64Array( data.X ), data.strideX1, data.strideX2, data.offsetX, new Float64Array( data.C ), data.strideC1, data.strideC2, data.offsetC, out, data.strideO, data.offsetO, counts, data.strideCounts, data.offsetCounts ); + + t.deepEqual( out, new Int32Array( data.expected_out ), 'returns expected assignments' ); + t.deepEqual( counts, new Int32Array( data.expected_counts ), 'returns expected counts' ); + t.end(); +}); + +tape( 'the function assigns each data point to its closest centroid (row-major, mixed strides)', function test( t ) { + var counts; + var data; + var out; + + data = MIXED_STRIDES_ROW_MAJOR_DATA; + + out = new Int32Array( data.expected_out.length ); + counts = new Int32Array( data.expected_counts.length ); + + dclosestCentroids( data.M, data.N, data.k, data.metric, new Float64Array( data.X ), data.strideX1, data.strideX2, data.offsetX, new Float64Array( data.C ), data.strideC1, data.strideC2, data.offsetC, out, data.strideO, data.offsetO, counts, data.strideCounts, data.offsetCounts ); + + t.deepEqual( out, new Int32Array( data.expected_out ), 'returns expected assignments' ); + t.deepEqual( counts, new Int32Array( data.expected_counts ), 'returns expected counts' ); + t.end(); +}); + +tape( 'the function assigns each data point to its closest centroid (column-major, mixed strides)', function test( t ) { + var counts; + var data; + var out; + + data = MIXED_STRIDES_COLUMN_MAJOR_DATA; + + out = new Int32Array( data.expected_out.length ); + counts = new Int32Array( data.expected_counts.length ); + + dclosestCentroids( data.M, data.N, data.k, data.metric, new Float64Array( data.X ), data.strideX1, data.strideX2, data.offsetX, new Float64Array( data.C ), data.strideC1, data.strideC2, data.offsetC, out, data.strideO, data.offsetO, counts, data.strideCounts, data.offsetCounts ); + + t.deepEqual( out, new Int32Array( data.expected_out ), 'returns expected assignments' ); + t.deepEqual( counts, new Int32Array( data.expected_counts ), 'returns expected counts' ); + t.end(); +}); + +tape( 'the function assigns each data point to its closest centroid (row-major, large strides)', function test( t ) { + var counts; + var data; + var out; + + data = LARGE_STRIDES_ROW_MAJOR_DATA; + + out = new Int32Array( data.expected_out.length ); + counts = new Int32Array( data.expected_counts.length ); + + dclosestCentroids( data.M, data.N, data.k, data.metric, new Float64Array( data.X ), data.strideX1, data.strideX2, data.offsetX, new Float64Array( data.C ), data.strideC1, data.strideC2, data.offsetC, out, data.strideO, data.offsetO, counts, data.strideCounts, data.offsetCounts ); + + t.deepEqual( out, new Int32Array( data.expected_out ), 'returns expected assignments' ); + t.deepEqual( counts, new Int32Array( data.expected_counts ), 'returns expected counts' ); + t.end(); +}); + +tape( 'the function assigns each data point to its closest centroid (column-major, large strides)', function test( t ) { + var counts; + var data; + var out; + + data = LARGE_STRIDES_COLUMN_MAJOR_DATA; + + out = new Int32Array( data.expected_out.length ); + counts = new Int32Array( data.expected_counts.length ); + + dclosestCentroids( data.M, data.N, data.k, data.metric, new Float64Array( data.X ), data.strideX1, data.strideX2, data.offsetX, new Float64Array( data.C ), data.strideC1, data.strideC2, data.offsetC, out, data.strideO, data.offsetO, counts, data.strideCounts, data.offsetCounts ); + + t.deepEqual( out, new Int32Array( data.expected_out ), 'returns expected assignments' ); + t.deepEqual( counts, new Int32Array( data.expected_counts ), 'returns expected counts' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/test.ndarray.native.js b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/test.ndarray.native.js new file mode 100644 index 000000000000..0db5005cb6c9 --- /dev/null +++ b/lib/node_modules/@stdlib/ml/strided/dkmeans-closest-centroids/test/test.ndarray.native.js @@ -0,0 +1,288 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, id-length */ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var tape = require( 'tape' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Int32Array = require( '@stdlib/array/int32' ); +var tryRequire = require( '@stdlib/utils/try-require' ); + + +// FIXTURES // + +var ROW_MAJOR_DATA = require( './fixtures/row_major.json' ); +var COLUMN_MAJOR_DATA = require( './fixtures/column_major.json' ); +var OFFSET_ROW_MAJOR_DATA = require( './fixtures/offsets/row_major.json' ); +var OFFSET_COLUMN_MAJOR_DATA = require( './fixtures/offsets/column_major.json' ); +var NEGATIVE_STRIDES_ROW_MAJOR_DATA = require( './fixtures/negative-strides/row_major.json' ); +var NEGATIVE_STRIDES_COLUMN_MAJOR_DATA = require( './fixtures/negative-strides/column_major.json' ); +var MIXED_STRIDES_ROW_MAJOR_DATA = require( './fixtures/mixed-strides/row_major.json' ); +var MIXED_STRIDES_COLUMN_MAJOR_DATA = require( './fixtures/mixed-strides/column_major.json' ); +var LARGE_STRIDES_ROW_MAJOR_DATA = require( './fixtures/large-strides/row_major.json' ); +var LARGE_STRIDES_COLUMN_MAJOR_DATA = require( './fixtures/large-strides/column_major.json' ); + + +// VARIABLES // + +var dclosestCentroids = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) ); +var opts = { + 'skip': ( dclosestCentroids instanceof Error ) +}; + + +// TESTS // + +tape( 'main export is a function', opts, function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof dclosestCentroids, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 18', opts, function test( t ) { + t.strictEqual( dclosestCentroids.length, 18, 'returns expected value' ); + t.end(); +}); + +tape( 'the function throws an error if provided a fourth argument which is not a supported distance metric', opts, function test( t ) { + var values; + var data; + var i; + + data = ROW_MAJOR_DATA; + + values = [ + 'foo', + 'bar', + 'beep', + 'boop', + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + dclosestCentroids( data.M, data.N, data.k, value, new Float64Array( data.X ), data.strideX1, data.strideX2, data.offsetX, new Float64Array( data.C ), data.strideC1, data.strideC2, data.offsetC, new Int32Array( data.M ), 1, 0, new Int32Array( data.k ), 1, 0 ); + }; + } +}); + +tape( 'the function returns the output array unchanged if `M`, `N`, or `k` is less than 1', opts, function test( t ) { + var counts; + var data; + var out; + + data = ROW_MAJOR_DATA; + + out = new Int32Array( data.expected_out.length ); + counts = new Int32Array( data.expected_counts.length ); + + t.strictEqual( dclosestCentroids( 0, data.N, data.k, data.metric, new Float64Array( data.X ), data.strideX1, data.strideX2, data.offsetX, new Float64Array( data.C ), data.strideC1, data.strideC2, data.offsetC, out, data.strideO, data.offsetO, counts, data.strideCounts, data.offsetCounts ), out, 'returns expected value' ); + t.strictEqual( dclosestCentroids( data.M, 0, data.k, data.metric, new Float64Array( data.X ), data.strideX1, data.strideX2, data.offsetX, new Float64Array( data.C ), data.strideC1, data.strideC2, data.offsetC, out, data.strideO, data.offsetO, counts, data.strideCounts, data.offsetCounts ), out, 'returns expected value' ); + t.strictEqual( dclosestCentroids( data.M, data.N, 0, data.metric, new Float64Array( data.X ), data.strideX1, data.strideX2, data.offsetX, new Float64Array( data.C ), data.strideC1, data.strideC2, data.offsetC, out, data.strideO, data.offsetO, counts, data.strideCounts, data.offsetCounts ), out, 'returns expected value' ); + + t.deepEqual( out, new Int32Array( data.expected_out.length ), 'leaves output unchanged' ); + t.deepEqual( counts, new Int32Array( data.expected_counts.length ), 'leaves counts unchanged' ); + + t.end(); +}); + +tape( 'the function assigns each data point to its closest centroid (row-major)', opts, function test( t ) { + var counts; + var data; + var out; + + data = ROW_MAJOR_DATA; + + out = new Int32Array( data.expected_out.length ); + counts = new Int32Array( data.expected_counts.length ); + + dclosestCentroids( data.M, data.N, data.k, data.metric, new Float64Array( data.X ), data.strideX1, data.strideX2, data.offsetX, new Float64Array( data.C ), data.strideC1, data.strideC2, data.offsetC, out, data.strideO, data.offsetO, counts, data.strideCounts, data.offsetCounts ); + + t.deepEqual( out, new Int32Array( data.expected_out ), 'returns expected assignments' ); + t.deepEqual( counts, new Int32Array( data.expected_counts ), 'returns expected counts' ); + t.end(); +}); + +tape( 'the function assigns each data point to its closest centroid (column-major)', opts, function test( t ) { + var counts; + var data; + var out; + + data = COLUMN_MAJOR_DATA; + + out = new Int32Array( data.expected_out.length ); + counts = new Int32Array( data.expected_counts.length ); + + dclosestCentroids( data.M, data.N, data.k, data.metric, new Float64Array( data.X ), data.strideX1, data.strideX2, data.offsetX, new Float64Array( data.C ), data.strideC1, data.strideC2, data.offsetC, out, data.strideO, data.offsetO, counts, data.strideCounts, data.offsetCounts ); + + t.deepEqual( out, new Int32Array( data.expected_out ), 'returns expected assignments' ); + t.deepEqual( counts, new Int32Array( data.expected_counts ), 'returns expected counts' ); + t.end(); +}); + +tape( 'the function assigns each data point to its closest centroid (row-major, offsets)', opts, function test( t ) { + var counts; + var data; + var out; + + data = OFFSET_ROW_MAJOR_DATA; + + out = new Int32Array( data.expected_out.length ); + counts = new Int32Array( data.expected_counts.length ); + + dclosestCentroids( data.M, data.N, data.k, data.metric, new Float64Array( data.X ), data.strideX1, data.strideX2, data.offsetX, new Float64Array( data.C ), data.strideC1, data.strideC2, data.offsetC, out, data.strideO, data.offsetO, counts, data.strideCounts, data.offsetCounts ); + + t.deepEqual( out, new Int32Array( data.expected_out ), 'returns expected assignments' ); + t.deepEqual( counts, new Int32Array( data.expected_counts ), 'returns expected counts' ); + t.end(); +}); + +tape( 'the function assigns each data point to its closest centroid (column-major, offsets)', opts, function test( t ) { + var counts; + var data; + var out; + + data = OFFSET_COLUMN_MAJOR_DATA; + + out = new Int32Array( data.expected_out.length ); + counts = new Int32Array( data.expected_counts.length ); + + dclosestCentroids( data.M, data.N, data.k, data.metric, new Float64Array( data.X ), data.strideX1, data.strideX2, data.offsetX, new Float64Array( data.C ), data.strideC1, data.strideC2, data.offsetC, out, data.strideO, data.offsetO, counts, data.strideCounts, data.offsetCounts ); + + t.deepEqual( out, new Int32Array( data.expected_out ), 'returns expected assignments' ); + t.deepEqual( counts, new Int32Array( data.expected_counts ), 'returns expected counts' ); + t.end(); +}); + +tape( 'the function assigns each data point to its closest centroid (row-major, negative strides)', opts, function test( t ) { + var counts; + var data; + var out; + + data = NEGATIVE_STRIDES_ROW_MAJOR_DATA; + + out = new Int32Array( data.expected_out.length ); + counts = new Int32Array( data.expected_counts.length ); + + dclosestCentroids( data.M, data.N, data.k, data.metric, new Float64Array( data.X ), data.strideX1, data.strideX2, data.offsetX, new Float64Array( data.C ), data.strideC1, data.strideC2, data.offsetC, out, data.strideO, data.offsetO, counts, data.strideCounts, data.offsetCounts ); + + t.deepEqual( out, new Int32Array( data.expected_out ), 'returns expected assignments' ); + t.deepEqual( counts, new Int32Array( data.expected_counts ), 'returns expected counts' ); + t.end(); +}); + +tape( 'the function assigns each data point to its closest centroid (column-major, negative strides)', opts, function test( t ) { + var counts; + var data; + var out; + + data = NEGATIVE_STRIDES_COLUMN_MAJOR_DATA; + + out = new Int32Array( data.expected_out.length ); + counts = new Int32Array( data.expected_counts.length ); + + dclosestCentroids( data.M, data.N, data.k, data.metric, new Float64Array( data.X ), data.strideX1, data.strideX2, data.offsetX, new Float64Array( data.C ), data.strideC1, data.strideC2, data.offsetC, out, data.strideO, data.offsetO, counts, data.strideCounts, data.offsetCounts ); + + t.deepEqual( out, new Int32Array( data.expected_out ), 'returns expected assignments' ); + t.deepEqual( counts, new Int32Array( data.expected_counts ), 'returns expected counts' ); + t.end(); +}); + +tape( 'the function assigns each data point to its closest centroid (row-major, mixed strides)', opts, function test( t ) { + var counts; + var data; + var out; + + data = MIXED_STRIDES_ROW_MAJOR_DATA; + + out = new Int32Array( data.expected_out.length ); + counts = new Int32Array( data.expected_counts.length ); + + dclosestCentroids( data.M, data.N, data.k, data.metric, new Float64Array( data.X ), data.strideX1, data.strideX2, data.offsetX, new Float64Array( data.C ), data.strideC1, data.strideC2, data.offsetC, out, data.strideO, data.offsetO, counts, data.strideCounts, data.offsetCounts ); + + t.deepEqual( out, new Int32Array( data.expected_out ), 'returns expected assignments' ); + t.deepEqual( counts, new Int32Array( data.expected_counts ), 'returns expected counts' ); + t.end(); +}); + +tape( 'the function assigns each data point to its closest centroid (column-major, mixed strides)', opts, function test( t ) { + var counts; + var data; + var out; + + data = MIXED_STRIDES_COLUMN_MAJOR_DATA; + + out = new Int32Array( data.expected_out.length ); + counts = new Int32Array( data.expected_counts.length ); + + dclosestCentroids( data.M, data.N, data.k, data.metric, new Float64Array( data.X ), data.strideX1, data.strideX2, data.offsetX, new Float64Array( data.C ), data.strideC1, data.strideC2, data.offsetC, out, data.strideO, data.offsetO, counts, data.strideCounts, data.offsetCounts ); + + t.deepEqual( out, new Int32Array( data.expected_out ), 'returns expected assignments' ); + t.deepEqual( counts, new Int32Array( data.expected_counts ), 'returns expected counts' ); + t.end(); +}); + +tape( 'the function assigns each data point to its closest centroid (row-major, large strides)', opts, function test( t ) { + var counts; + var data; + var out; + + data = LARGE_STRIDES_ROW_MAJOR_DATA; + + out = new Int32Array( data.expected_out.length ); + counts = new Int32Array( data.expected_counts.length ); + + dclosestCentroids( data.M, data.N, data.k, data.metric, new Float64Array( data.X ), data.strideX1, data.strideX2, data.offsetX, new Float64Array( data.C ), data.strideC1, data.strideC2, data.offsetC, out, data.strideO, data.offsetO, counts, data.strideCounts, data.offsetCounts ); + + t.deepEqual( out, new Int32Array( data.expected_out ), 'returns expected assignments' ); + t.deepEqual( counts, new Int32Array( data.expected_counts ), 'returns expected counts' ); + t.end(); +}); + +tape( 'the function assigns each data point to its closest centroid (column-major, large strides)', opts, function test( t ) { + var counts; + var data; + var out; + + data = LARGE_STRIDES_COLUMN_MAJOR_DATA; + + out = new Int32Array( data.expected_out.length ); + counts = new Int32Array( data.expected_counts.length ); + + dclosestCentroids( data.M, data.N, data.k, data.metric, new Float64Array( data.X ), data.strideX1, data.strideX2, data.offsetX, new Float64Array( data.C ), data.strideC1, data.strideC2, data.offsetC, out, data.strideO, data.offsetO, counts, data.strideCounts, data.offsetCounts ); + + t.deepEqual( out, new Int32Array( data.expected_out ), 'returns expected assignments' ); + t.deepEqual( counts, new Int32Array( data.expected_counts ), 'returns expected counts' ); + t.end(); +});