diff --git a/lib/node_modules/@stdlib/stats/strided/maxk/README.md b/lib/node_modules/@stdlib/stats/strided/maxk/README.md new file mode 100644 index 000000000000..4f6640515ae9 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/maxk/README.md @@ -0,0 +1,178 @@ + + +# maxk + +> Compute the `k` maximum values of a strided array. + +
+ +
+ + + +
+ +## Usage + +```javascript +var maxk = require( '@stdlib/stats/strided/maxk' ); +``` + +#### maxk( N, k, x, strideX, out, strideOut ) + +Computes the `k` maximum values of a strided array. + +```javascript +var x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; +var out = [ 0.0, 0.0, 0.0 ]; + +maxk( x.length, 3, x, 1, out, 1 ); +// out => [ 3.0, 4.0, 5.0 ] +``` + +The function has the following parameters: + +- **N**: number of indexed elements. +- **k**: number of maximum values to return. +- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array]. +- **strideX**: stride length for `x`. +- **out**: output [`Array`][mdn-array] or [`typed array`][mdn-typed-array]. +- **strideOut**: stride length for `out`. + +The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the `k` maximum values for every other element: + +```javascript +var x = [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ]; +var out = [ 0.0, 0.0 ]; + +maxk( 4, 2, x, 2, out, 1 ); +// out => [ 2.0, 4.0 ] +``` + +Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. + + + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); + +var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); +var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element + +var out = new Float64Array( 2 ); + +maxk( 4, 2, x1, 2, out, 1 ); +// out => [ 2.0, 4.0 ] +``` + +#### maxk.ndarray( N, k, x, strideX, offsetX, out, strideOut, offsetOut ) + +Computes the `k` maximum values of a strided array using alternative indexing semantics. + +```javascript +var x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; +var out = [ 0.0, 0.0, 0.0 ]; + +maxk.ndarray( x.length, 3, x, 1, 0, out, 1, 0 ); +// out => [ 3.0, 4.0, 5.0 ] +``` + +The function has the following additional parameters: + +- **offsetX**: starting index for `x`. +- **offsetOut**: starting index for `out`. + +While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to calculate the `k` maximum values starting from the second element: + +```javascript +var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ]; +var out = [ 0.0, 0.0 ]; + +maxk.ndarray( 4, 2, x, 2, 1, out, 1, 0 ); +// out => [ 2.0, 4.0 ] +``` + +
+ + + +
+ +## Notes + +- If `N <= 0`, both functions fill the output array with `NaN`. +- If `k <= 0`, both functions return the output array unchanged. +- The min-heap algorithm is sensitive to the presence of `NaN` values. Since `NaN` comparisons always return `false`, if `NaN` values are present in the input array, the results will be unpredictable. +- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]). + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var maxk = require( '@stdlib/stats/strided/maxk' ); + +var x = discreteUniform( 10, -50, 50, { + 'dtype': 'generic' +}); +console.log( x ); + +var out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; +maxk( x.length, 5, x, 1, out, 1 ); +console.log( out ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/strided/maxk/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/strided/maxk/benchmark/benchmark.js new file mode 100644 index 000000000000..2f4672acdcca --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/maxk/benchmark/benchmark.js @@ -0,0 +1,109 @@ +/** +* @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 uniform = require( '@stdlib/random/array/uniform' ); +var zeros = require( '@stdlib/array/base/zeros' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var maxk = require( './../lib/main.js' ); + + +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var out; + var x; + var k; + + k = len - 1; + x = uniform( len, -10, 10, options ); + out = zeros( k ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + maxk( x.length, k, x, 1, out, 1 ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( format( '%s:len=%d,k=%d', pkg, len, len - 1 ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/stats/strided/maxk/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/stats/strided/maxk/benchmark/benchmark.ndarray.js new file mode 100644 index 000000000000..ebbe54884c46 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/maxk/benchmark/benchmark.ndarray.js @@ -0,0 +1,109 @@ +/** +* @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 uniform = require( '@stdlib/random/array/uniform' ); +var zeros = require( '@stdlib/array/base/zeros' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var maxk = require( './../lib/ndarray.js' ); + + +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var out; + var x; + var k; + + k = len - 1; + x = uniform( len, -10, 10, options ); + out = zeros( k ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + maxk( x.length, k, x, 1, 0, out, 1, 0 ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( format( '%s:ndarray:len=%d,k=%d', pkg, len, len - 1 ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/stats/strided/maxk/docs/repl.txt b/lib/node_modules/@stdlib/stats/strided/maxk/docs/repl.txt new file mode 100644 index 000000000000..61b9b52ec48c --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/maxk/docs/repl.txt @@ -0,0 +1,112 @@ + +{{alias}}( N, k, x, strideX, out, strideOut ) + Computes the `k` maximum values of a strided array. + + The `N` and stride parameters determine which elements in the strided array + are accessed at runtime. + + Indexing is relative to the first index. To introduce an offset, use a typed + array view. + + Parameters + ---------- + N: integer + Number of indexed elements. + + k: integer + Number of maximum values to return. + + x: Array|TypedArray + Input array. + + strideX: integer + Stride length for `x`. + + out: Array|TypedArray + Output array. + + strideOut: integer + Stride length for `out`. + + Returns + ------- + out: Array|TypedArray + Output array. + + Examples + -------- + // Standard Usage: + > var x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + > var out = [ 0.0, 0.0, 0.0 ]; + > {{alias}}( x.length, 3, x, 1, out, 1 ) + [ 3.0, 4.0, 5.0 ] + + // Using `N` and stride parameters: + > x = [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ]; + > out = [ 0.0, 0.0 ]; + > {{alias}}( 4, 2, x, 2, out, 1 ) + [ 2.0, 4.0 ] + + // Using view offsets: + > var x0 = new {{alias:@stdlib/array/float64}}( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + > var x1 = new {{alias:@stdlib/array/float64}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); + > var out = [ 0.0, 0.0 ]; + > {{alias}}( 2, 2, x1, 2, out, 1 ) + [ 2.0, 4.0 ] + + +{{alias}}.ndarray( N, k, x, strideX, offsetX, out, strideOut, offsetOut ) + Computes the `k` maximum values of a strided array using alternative + indexing semantics. + + While typed array views mandate a view offset based on the underlying + buffer, the `offset` parameter supports indexing semantics based on a + starting index. + + Parameters + ---------- + N: integer + Number of indexed elements. + + k: integer + Number of maximum values to return. + + x: Array|TypedArray + Input array. + + strideX: integer + Stride length for `x`. + + offsetX: integer + Starting index for `x`. + + out: Array|TypedArray + Output array. + + strideOut: integer + Stride length for `out`. + + offsetOut: integer + Starting index for `out`. + + Returns + ------- + out: Array|TypedArray + Output array. + + Examples + -------- + // Standard Usage: + > var x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + > var out = [ 0.0, 0.0, 0.0 ]; + > {{alias}}.ndarray( x.length, 3, x, 1, 0, out, 1, 0 ) + [ 3.0, 4.0, 5.0 ] + + // Using offset parameter: + > var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ]; + > var out = [ 0.0, 0.0 ]; + > {{alias}}.ndarray( 4, 2, x, 2, 1, out, 1, 0 ) + [ 2.0, 4.0 ] + + See Also + -------- diff --git a/lib/node_modules/@stdlib/stats/strided/maxk/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/strided/maxk/docs/types/index.d.ts new file mode 100644 index 000000000000..5d678595d240 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/maxk/docs/types/index.d.ts @@ -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. +*/ + +// TypeScript Version: 4.1 + +/// + +import { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array'; + +/** +* Input array. +*/ +type InputArray = NumericArray | Collection | AccessorArrayLike; + +/** +* Output array. +*/ +type OutputArray = NumericArray | Collection | AccessorArrayLike; + +/** +* Interface describing `maxk`. +*/ +interface Routine { + /** + * Computes the `k` maximum values of a strided array. + * + * @param N - number of indexed elements + * @param k - number of maximum values to return + * @param x - input array + * @param strideX - stride length for `x` + * @param out - output array + * @param strideOut - stride length for `out` + * @returns output array + * + * @example + * var x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + * var out = [ 0.0, 0.0, 0.0 ]; + * + * maxk( x.length, 3, x, 1, out, 1 ); + * // out => [ 3.0, 4.0, 5.0 ] + */ + ( N: number, k: number, x: InputArray, strideX: number, out: T, strideOut: number ): T; + + /** + * Computes the `k` maximum values of a strided array using alternative indexing semantics. + * + * @param N - number of indexed elements + * @param k - number of maximum values to return + * @param x - input array + * @param strideX - stride length for `x` + * @param offsetX - starting index for `x` + * @param out - output array + * @param strideOut - stride length for `out` + * @param offsetOut - starting index for `out` + * @returns output array + * + * @example + * var x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + * var out = [ 0.0, 0.0, 0.0 ]; + * + * maxk.ndarray( x.length, 3, x, 1, 0, out, 1, 0 ); + * // out => [ 3.0, 4.0, 5.0 ] + */ + ndarray( N: number, k: number, x: InputArray, strideX: number, offsetX: number, out: T, strideOut: number, offsetOut: number ): T; +} + +/** +* Computes the `k` maximum values of a strided array. +* +* @param N - number of indexed elements +* @param k - number of maximum values to return +* @param x - input array +* @param strideX - stride length for `x` +* @param out - output array +* @param strideOut - stride length for `out` +* @returns output array +* +* @example +* var x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; +* var out = [ 0.0, 0.0, 0.0 ]; +* +* maxk( x.length, 3, x, 1, out, 1 ); +* // out => [ 3.0, 4.0, 5.0 ] +* +* @example +* var x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; +* var out = [ 0.0, 0.0, 0.0 ]; +* +* maxk.ndarray( x.length, 3, x, 1, 0, out, 1, 0 ); +* // out => [ 3.0, 4.0, 5.0 ] +*/ +declare var maxk: Routine; + + +// EXPORTS // + +export = maxk; diff --git a/lib/node_modules/@stdlib/stats/strided/maxk/docs/types/test.ts b/lib/node_modules/@stdlib/stats/strided/maxk/docs/types/test.ts new file mode 100644 index 000000000000..88c49229d957 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/maxk/docs/types/test.ts @@ -0,0 +1,268 @@ +/* +* @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 space-in-parens */ + +import maxk = require( './index' ); + + +// TESTS // + +// The function returns a collection... +{ + const x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + const out = new Float64Array( 3 ); + + maxk( x.length, 3, x, 1, out, 1 ); // $ExpectType Float64Array +} + +// The compiler throws an error if the function is provided a first argument which is not a number... +{ + const x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + const out = [ 0.0, 0.0, 0.0 ]; + + maxk( '10', 3, x, 1, out, 1 ); // $ExpectError + maxk( true, 3, x, 1, out, 1 ); // $ExpectError + maxk( false, 3, x, 1, out, 1 ); // $ExpectError + maxk( null, 3, x, 1, out, 1 ); // $ExpectError + maxk( undefined, 3, x, 1, out, 1 ); // $ExpectError + maxk( [], 3, x, 1, out, 1 ); // $ExpectError + maxk( {}, 3, x, 1, out, 1 ); // $ExpectError + maxk( ( x: number ): number => x, 3, x, 1, out, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a number... +{ + const x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + const out = [ 0.0, 0.0, 0.0 ]; + + maxk( x.length, '10', x, 1, out, 1 ); // $ExpectError + maxk( x.length, true, x, 1, out, 1 ); // $ExpectError + maxk( x.length, false, x, 1, out, 1 ); // $ExpectError + maxk( x.length, null, x, 1, out, 1 ); // $ExpectError + maxk( x.length, undefined, x, 1, out, 1 ); // $ExpectError + maxk( x.length, [], x, 1, out, 1 ); // $ExpectError + maxk( x.length, {}, x, 1, out, 1 ); // $ExpectError + maxk( x.length, ( x: number ): number => x, x, 1, out, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a third argument which is not a collection... +{ + const out = [ 0.0, 0.0, 0.0 ]; + + maxk( 5, 3, 10, 1, out, 1 ); // $ExpectError + maxk( 5, 3, true, 1, out, 1 ); // $ExpectError + maxk( 5, 3, false, 1, out, 1 ); // $ExpectError + maxk( 5, 3, null, 1, out, 1 ); // $ExpectError + maxk( 5, 3, undefined, 1, out, 1 ); // $ExpectError + maxk( 5, 3, {}, 1, out, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a fourth argument which is not a number... +{ + const x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + const out = [ 0.0, 0.0, 0.0 ]; + + maxk( x.length, 3, x, '10', out, 1 ); // $ExpectError + maxk( x.length, 3, x, true, out, 1 ); // $ExpectError + maxk( x.length, 3, x, false, out, 1 ); // $ExpectError + maxk( x.length, 3, x, null, out, 1 ); // $ExpectError + maxk( x.length, 3, x, undefined, out, 1 ); // $ExpectError + maxk( x.length, 3, x, [], out, 1 ); // $ExpectError + maxk( x.length, 3, x, {}, out, 1 ); // $ExpectError + maxk( x.length, 3, x, ( x: number ): number => x, out, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a fifth argument which is not a collection... +{ + const x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + + maxk( x.length, 3, x, 1, 10, 1 ); // $ExpectError + maxk( x.length, 3, x, 1, true, 1 ); // $ExpectError + maxk( x.length, 3, x, 1, false, 1 ); // $ExpectError + maxk( x.length, 3, x, 1, null, 1 ); // $ExpectError + maxk( x.length, 3, x, 1, undefined, 1 ); // $ExpectError + maxk( x.length, 3, x, 1, {}, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a sixth argument which is not a number... +{ + const x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + const out = [ 0.0, 0.0, 0.0 ]; + + maxk( x.length, 3, x, 1, out, '10' ); // $ExpectError + maxk( x.length, 3, x, 1, out, true ); // $ExpectError + maxk( x.length, 3, x, 1, out, false ); // $ExpectError + maxk( x.length, 3, x, 1, out, null ); // $ExpectError + maxk( x.length, 3, x, 1, out, undefined ); // $ExpectError + maxk( x.length, 3, x, 1, out, [] ); // $ExpectError + maxk( x.length, 3, x, 1, out, {} ); // $ExpectError + maxk( x.length, 3, x, 1, out, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + const out = [ 0.0, 0.0, 0.0 ]; + + maxk(); // $ExpectError + maxk( x.length ); // $ExpectError + maxk( x.length, 3 ); // $ExpectError + maxk( x.length, 3, x ); // $ExpectError + maxk( x.length, 3, x, 1 ); // $ExpectError + maxk( x.length, 3, x, 1, out ); // $ExpectError + maxk( x.length, 3, x, 1, out, 1, 10 ); // $ExpectError +} + +// Attached to main export is an `ndarray` method which returns a collection... +{ + const x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + const out = new Float64Array( 3 ); + + maxk.ndarray( x.length, 3, x, 1, 0, out, 1, 0 ); // $ExpectType Float64Array +} + +// The compiler throws an error if the `ndarray` method is provided a first argument which is not a number... +{ + const x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + const out = [ 0.0, 0.0, 0.0 ]; + + maxk.ndarray( '10', 3, x, 1, 0, out, 1, 0 ); // $ExpectError + maxk.ndarray( true, 3, x, 1, 0, out, 1, 0 ); // $ExpectError + maxk.ndarray( false, 3, x, 1, 0, out, 1, 0 ); // $ExpectError + maxk.ndarray( null, 3, x, 1, 0, out, 1, 0 ); // $ExpectError + maxk.ndarray( undefined, 3, x, 1, 0, out, 1, 0 ); // $ExpectError + maxk.ndarray( [], 3, x, 1, 0, out, 1, 0 ); // $ExpectError + maxk.ndarray( {}, 3, x, 1, 0, out, 1, 0 ); // $ExpectError + maxk.ndarray( ( x: number ): number => x, 3, x, 1, 0, out, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a second argument which is not a number... +{ + const x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + const out = [ 0.0, 0.0, 0.0 ]; + + maxk.ndarray( x.length, '10', x, 1, 0, out, 1, 0 ); // $ExpectError + maxk.ndarray( x.length, true, x, 1, 0, out, 1, 0 ); // $ExpectError + maxk.ndarray( x.length, false, x, 1, 0, out, 1, 0 ); // $ExpectError + maxk.ndarray( x.length, null, x, 1, 0, out, 1, 0 ); // $ExpectError + maxk.ndarray( x.length, undefined, x, 1, 0, out, 1, 0 ); // $ExpectError + maxk.ndarray( x.length, [], x, 1, 0, out, 1, 0 ); // $ExpectError + maxk.ndarray( x.length, {}, x, 1, 0, out, 1, 0 ); // $ExpectError + maxk.ndarray( x.length, ( x: number ): number => x, x, 1, 0, out, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a third argument which is not a collection... +{ + const out = [ 0.0, 0.0, 0.0 ]; + + maxk.ndarray( 5, 3, 10, 1, 0, out, 1, 0 ); // $ExpectError + maxk.ndarray( 5, 3, true, 1, 0, out, 1, 0 ); // $ExpectError + maxk.ndarray( 5, 3, false, 1, 0, out, 1, 0 ); // $ExpectError + maxk.ndarray( 5, 3, null, 1, 0, out, 1, 0 ); // $ExpectError + maxk.ndarray( 5, 3, undefined, 1, 0, out, 1, 0 ); // $ExpectError + maxk.ndarray( 5, 3, {}, 1, 0, out, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a fourth argument which is not a number... +{ + const x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + const out = [ 0.0, 0.0, 0.0 ]; + + maxk.ndarray( x.length, 3, x, '10', 0, out, 1, 0 ); // $ExpectError + maxk.ndarray( x.length, 3, x, true, 0, out, 1, 0 ); // $ExpectError + maxk.ndarray( x.length, 3, x, false, 0, out, 1, 0 ); // $ExpectError + maxk.ndarray( x.length, 3, x, null, 0, out, 1, 0 ); // $ExpectError + maxk.ndarray( x.length, 3, x, undefined, 0, out, 1, 0 ); // $ExpectError + maxk.ndarray( x.length, 3, x, [], 0, out, 1, 0 ); // $ExpectError + maxk.ndarray( x.length, 3, x, {}, 0, out, 1, 0 ); // $ExpectError + maxk.ndarray( x.length, 3, x, ( x: number ): number => x, 0, out, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a fifth argument which is not a number... +{ + const x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + const out = [ 0.0, 0.0, 0.0 ]; + + maxk.ndarray( x.length, 3, x, 1, '10', out, 1, 0 ); // $ExpectError + maxk.ndarray( x.length, 3, x, 1, true, out, 1, 0 ); // $ExpectError + maxk.ndarray( x.length, 3, x, 1, false, out, 1, 0 ); // $ExpectError + maxk.ndarray( x.length, 3, x, 1, null, out, 1, 0 ); // $ExpectError + maxk.ndarray( x.length, 3, x, 1, undefined, out, 1, 0 ); // $ExpectError + maxk.ndarray( x.length, 3, x, 1, [], out, 1, 0 ); // $ExpectError + maxk.ndarray( x.length, 3, x, 1, {}, out, 1, 0 ); // $ExpectError + maxk.ndarray( x.length, 3, x, 1, ( x: number ): number => x, out, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a sixth argument which is not a collection... +{ + const x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + + maxk.ndarray( x.length, 3, x, 1, 0, 10, 1, 0 ); // $ExpectError + maxk.ndarray( x.length, 3, x, 1, 0, true, 1, 0 ); // $ExpectError + maxk.ndarray( x.length, 3, x, 1, 0, false, 1, 0 ); // $ExpectError + maxk.ndarray( x.length, 3, x, 1, 0, null, 1, 0 ); // $ExpectError + maxk.ndarray( x.length, 3, x, 1, 0, undefined, 1, 0 ); // $ExpectError + maxk.ndarray( x.length, 3, x, 1, 0, {}, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a seventh argument which is not a number... +{ + const x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + const out = [ 0.0, 0.0, 0.0 ]; + + maxk.ndarray( x.length, 3, x, 1, 0, out, '10', 0 ); // $ExpectError + maxk.ndarray( x.length, 3, x, 1, 0, out, true, 0 ); // $ExpectError + maxk.ndarray( x.length, 3, x, 1, 0, out, false, 0 ); // $ExpectError + maxk.ndarray( x.length, 3, x, 1, 0, out, null, 0 ); // $ExpectError + maxk.ndarray( x.length, 3, x, 1, 0, out, undefined, 0 ); // $ExpectError + maxk.ndarray( x.length, 3, x, 1, 0, out, [], 0 ); // $ExpectError + maxk.ndarray( x.length, 3, x, 1, 0, out, {}, 0 ); // $ExpectError + maxk.ndarray( x.length, 3, x, 1, 0, out, ( x: number ): number => x, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided an eighth argument which is not a number... +{ + const x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + const out = [ 0.0, 0.0, 0.0 ]; + + maxk.ndarray( x.length, 3, x, 1, 0, out, 1, '10' ); // $ExpectError + maxk.ndarray( x.length, 3, x, 1, 0, out, 1, true ); // $ExpectError + maxk.ndarray( x.length, 3, x, 1, 0, out, 1, false ); // $ExpectError + maxk.ndarray( x.length, 3, x, 1, 0, out, 1, null ); // $ExpectError + maxk.ndarray( x.length, 3, x, 1, 0, out, 1, undefined ); // $ExpectError + maxk.ndarray( x.length, 3, x, 1, 0, out, 1, [] ); // $ExpectError + maxk.ndarray( x.length, 3, x, 1, 0, out, 1, {} ); // $ExpectError + maxk.ndarray( x.length, 3, x, 1, 0, out, 1, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided an unsupported number of arguments... +{ + const x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + const out = [ 0.0, 0.0, 0.0 ]; + + maxk.ndarray(); // $ExpectError + maxk.ndarray( x.length ); // $ExpectError + maxk.ndarray( x.length, 3 ); // $ExpectError + maxk.ndarray( x.length, 3, x ); // $ExpectError + maxk.ndarray( x.length, 3, x, 1 ); // $ExpectError + maxk.ndarray( x.length, 3, x, 1, 0 ); // $ExpectError + maxk.ndarray( x.length, 3, x, 1, 0, out ); // $ExpectError + maxk.ndarray( x.length, 3, x, 1, 0, out, 1 ); // $ExpectError + maxk.ndarray( x.length, 3, x, 1, 0, out, 1, 0, 10 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/strided/maxk/examples/index.js b/lib/node_modules/@stdlib/stats/strided/maxk/examples/index.js new file mode 100644 index 000000000000..ae154ebc2b3e --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/maxk/examples/index.js @@ -0,0 +1,31 @@ +/** +* @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 discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var maxk = require( './../lib' ); + +var x = discreteUniform( 10, -50, 50, { + 'dtype': 'generic' +}); +console.log( x ); + +var out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; +maxk( x.length, 5, x, 1, out, 1 ); +console.log( out ); diff --git a/lib/node_modules/@stdlib/stats/strided/maxk/lib/accessors.js b/lib/node_modules/@stdlib/stats/strided/maxk/lib/accessors.js new file mode 100644 index 000000000000..afbcfc7f0f25 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/maxk/lib/accessors.js @@ -0,0 +1,160 @@ +/** +* @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 isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); +var gcopy = require( '@stdlib/blas/base/gcopy' ).ndarray; +var floor = require( '@stdlib/math/base/special/floor' ); + + +// FUNCTIONS // + +/** +* Sifts a value down the heap to restore the min-heap. +* +* @private +* @param {NonNegativeInteger} root - logical root index to sift down from +* @param {PositiveInteger} size - heap size +* @param {number} value - value to sift down +* @param {Object} arr - heap storage array +* @param {Collection} arr.data - heap storage array data +* @param {Array} arr.accessors - heap storage array element accessors +* @param {integer} stride - stride length +* @param {NonNegativeInteger} offset - starting index +*/ +function siftDown( root, size, value, arr, stride, offset ) { + var ridx; + var cidx; + var abuf; + var aget; + var aset; + var j; + + abuf = arr.data; + aget = arr.accessors[ 0 ]; + aset = arr.accessors[ 1 ]; + + ridx = offset + (root*stride); + j = ( root * 2 ) + 1; + + while ( j < size ) { + cidx = offset + (j*stride); + + // Find smaller child. + if ( j + 1 < size && aget( abuf, offset + ((j+1)*stride) ) < aget( abuf, cidx ) ) { // eslint-disable-line max-len + j += 1; + cidx = offset + (j*stride); + } + + // Stop if element is already smaller than child. + if ( aget( abuf, cidx ) >= value ) { + break; + } + + // Move child up. + aset( abuf, ridx, aget( abuf, cidx ) ); + root = j; + ridx = cidx; + j = ( root * 2 ) + 1; + } + aset( abuf, ridx, value ); +} + + +// MAIN // + +/** +* Computes the `k` maximum values of a strided array. +* +* @private +* @param {PositiveInteger} N - number of indexed elements +* @param {PositiveInteger} k - number of maximum values to return +* @param {Object} x - input array object +* @param {Collection} x.data - input array data +* @param {Array} x.accessors - array element accessors +* @param {integer} strideX - stride length for `x` +* @param {NonNegativeInteger} offsetX - starting index for `x` +* @param {Object} out - output array object +* @param {Collection} out.data - output array data +* @param {Array} out.accessors - array element accessors +* @param {integer} strideOut - stride length for `out` +* @param {NonNegativeInteger} offsetOut - starting index for `out` +* @returns {Object} output array object +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +* +* var x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; +* var out = [ 0.0, 0.0, 0.0 ]; +* +* maxk( x.length, 3, arraylike2object( toAccessorArray( x ) ), 1, 0, arraylike2object( toAccessorArray( out ) ), 1, 0 ); +* // out => [ 3.0, 4.0, 5.0 ] +*/ +function maxk( N, k, x, strideX, offsetX, out, strideOut, offsetOut ) { + var xbuf; + var obuf; + var xget; + var oget; + var oset; + var min; + var ix; + var i; + var v; + + xbuf = x.data; + obuf = out.data; + xget = x.accessors[ 0 ]; + oget = out.accessors[ 0 ]; + oset = out.accessors[ 1 ]; + + // Copy first k elements from input to output. + gcopy( k, xbuf, strideX, offsetX, obuf, strideOut, offsetOut ); + + // Build min-heap from first k elements in output. + for ( i = floor(k/2)-1; i >= 0; i-- ) { + siftDown( i, k, oget( obuf, offsetOut + (i*strideOut) ), out, strideOut, offsetOut ); // eslint-disable-line max-len + } + + // Cache minimum value. + min = oget( obuf, offsetOut ); + + // Scan remaining elements and update heap if larger than current minimum. + ix = offsetX + ( k * strideX ); + for ( i = k; i < N; i++ ) { + v = xget( xbuf, ix ); + + // If element is larger than min, replace root and sift down + if ( v > min || (v === min && isPositiveZero( v )) ) { + oset( obuf, offsetOut, v ); + siftDown( 0, k, v, out, strideOut, offsetOut ); + min = oget( obuf, offsetOut ); + } + ix += strideX; + } + + return out; +} + + +// EXPORTS // + +module.exports = maxk; diff --git a/lib/node_modules/@stdlib/stats/strided/maxk/lib/index.js b/lib/node_modules/@stdlib/stats/strided/maxk/lib/index.js new file mode 100644 index 000000000000..7ad89fc9d0ac --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/maxk/lib/index.js @@ -0,0 +1,61 @@ +/** +* @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'; + +/** +* Compute the `k` maximum values of a strided array. +* +* @module @stdlib/stats/strided/maxk +* +* @example +* var maxk = require( '@stdlib/stats/strided/maxk' ); +* +* var x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; +* var out = [ 0.0, 0.0, 0.0 ]; +* +* maxk( x.length, 3, x, 1, out, 1 ); +* // out => [ 3.0, 4.0, 5.0 ] +* +* @example +* var maxk = require( '@stdlib/stats/strided/maxk' ); +* +* var x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; +* var out = [ 0.0, 0.0, 0.0 ]; +* +* maxk.ndarray( x.length, 3, x, 1, 0, out, 1, 0 ); +* // out => [ 3.0, 4.0, 5.0 ] +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var main = require( './main.js' ); +var ndarray = require( './ndarray.js' ); + + +// MAIN // + +setReadOnly( main, 'ndarray', ndarray ); + + +// EXPORTS // + +module.exports = main; + +// exports: { "ndarray": "main.ndarray" } diff --git a/lib/node_modules/@stdlib/stats/strided/maxk/lib/main.js b/lib/node_modules/@stdlib/stats/strided/maxk/lib/main.js new file mode 100644 index 000000000000..d64158b8d5bf --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/maxk/lib/main.js @@ -0,0 +1,54 @@ +/** +* @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 stride2offset = require( '@stdlib/strided/base/stride2offset' ); +var ndarray = require( './ndarray.js' ); + + +// MAIN // + +/** +* Computes the `k` maximum values of a strided array. +* +* @param {PositiveInteger} N - number of indexed elements +* @param {PositiveInteger} k - number of maximum values to return +* @param {NumericArray} x - input array +* @param {integer} strideX - stride length for `x` +* @param {NumericArray} out - output array +* @param {integer} strideOut - stride length for `out` +* @returns {NumericArray} output array +* +* @example +* var x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; +* var out = [ 0.0, 0.0, 0.0 ]; +* +* maxk( x.length, 3, x, 1, out, 1 ); +* // out => [ 3.0, 4.0, 5.0 ] +*/ +function maxk( N, k, x, strideX, out, strideOut ) { + return ndarray( N, k, x, strideX, stride2offset( N, strideX ), out, strideOut, stride2offset( k, strideOut ) ); // eslint-disable-line max-len +} + + +// EXPORTS // + +module.exports = maxk; diff --git a/lib/node_modules/@stdlib/stats/strided/maxk/lib/ndarray.js b/lib/node_modules/@stdlib/stats/strided/maxk/lib/ndarray.js new file mode 100644 index 000000000000..62c53599ba47 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/maxk/lib/ndarray.js @@ -0,0 +1,158 @@ +/** +* @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 isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); +var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +var gfill = require( '@stdlib/blas/ext/base/gfill' ).ndarray; +var gcopy = require( '@stdlib/blas/base/gcopy' ).ndarray; +var floor = require( '@stdlib/math/base/special/floor' ); +var accessors = require( './accessors.js' ); + + +// FUNCTIONS // + +/** +* Sifts a value down the heap to restore the min-heap. +* +* @private +* @param {NonNegativeInteger} root - logical root index to sift down from +* @param {PositiveInteger} size - heap size +* @param {number} value - value to sift down +* @param {NumericArray} arr - heap storage array +* @param {integer} stride - stride length +* @param {NonNegativeInteger} offset - starting index +*/ +function siftDown( root, size, value, arr, stride, offset ) { + var ridx; + var cidx; + var j; + + ridx = offset + (root*stride); + j = ( root * 2 ) + 1; + + while ( j < size ) { + cidx = offset + (j*stride); + + // Find smaller child. + if ( j + 1 < size && arr[ offset + ((j+1)*stride) ] < arr[ cidx ] ) { + j += 1; + cidx = offset + (j*stride); + } + + // Stop if element is already smaller than child. + if ( arr[ cidx ] >= value ) { + break; + } + + // Move child up. + arr[ ridx ] = arr[ cidx ]; + root = j; + ridx = cidx; + j = ( root * 2 ) + 1; + } + arr[ ridx ] = value; +} + + +// MAIN // + +/** +* Computes the `k` maximum values of a strided array. +* +* @param {PositiveInteger} N - number of indexed elements +* @param {PositiveInteger} k - number of maximum values to return +* @param {NumericArray} x - input array +* @param {integer} strideX - stride length for `x` +* @param {NonNegativeInteger} offsetX - starting index for `x` +* @param {NumericArray} out - output array +* @param {integer} strideOut - stride length for `out` +* @param {NonNegativeInteger} offsetOut - starting index for `out` +* @returns {NumericArray} output array +* +* @example +* var x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; +* var out = [ 0.0, 0.0, 0.0 ]; +* +* maxk( x.length, 3, x, 1, 0, out, 1, 0 ); +* // out => [ 3.0, 4.0, 5.0 ] +*/ +function maxk( N, k, x, strideX, offsetX, out, strideOut, offsetOut ) { + var min; + var xo; + var yo; + var ix; + var i; + var v; + + if ( N <= 0 || k <= 0 ) { + if ( k > 0 ) { + gfill( k, NaN, out, strideOut, offsetOut ); + } + return out; + } + if ( N < k ) { + gcopy( N, x, strideX, offsetX, out, strideOut, offsetOut ); + gfill( k - N, NaN, out, strideOut, offsetOut + (N * strideOut) ); + return out; + } + if ( N === k ) { + gcopy( N, x, strideX, offsetX, out, strideOut, offsetOut ); + return out; + } + xo = arraylike2object( x ); + yo = arraylike2object( out ); + if ( xo.accessorProtocol || yo.accessorProtocol ) { + accessors( N, k, xo, strideX, offsetX, yo, strideOut, offsetOut ); + return out; + } + + // Copy first k elements. + gcopy( k, x, strideX, offsetX, out, strideOut, offsetOut ); + + // Build min-heap from first k elements. + for ( i = floor(k/2)-1; i >= 0; i-- ) { + siftDown( i, k, out[ offsetOut + (i*strideOut) ], out, strideOut, offsetOut ); // eslint-disable-line max-len + } + + // Cache minimum value. + min = out[ offsetOut ]; + + // Scan remaining elements and update heap if larger than current minimum. + ix = offsetX + ( k * strideX ); + for ( i = k; i < N; i++ ) { + v = x[ ix ]; + + // If element is larger than min, replace root and sift down + if ( v > min || (v === min && isPositiveZero( v )) ) { + out[ offsetOut ] = v; + siftDown( 0, k, v, out, strideOut, offsetOut ); + min = out[ offsetOut ]; + } + ix += strideX; + } + return out; +} + + +// EXPORTS // + +module.exports = maxk; diff --git a/lib/node_modules/@stdlib/stats/strided/maxk/package.json b/lib/node_modules/@stdlib/stats/strided/maxk/package.json new file mode 100644 index 000000000000..73d2c2fb2444 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/maxk/package.json @@ -0,0 +1,71 @@ +{ + "name": "@stdlib/stats/strided/maxk", + "version": "0.0.0", + "description": "Compute the k maximum values of a strided array.", + "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", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "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", + "stdmath", + "statistics", + "stats", + "mathematics", + "math", + "maximum", + "max", + "top-k", + "k-max", + "extremes", + "domain", + "extent", + "heap", + "strided", + "strided array", + "array" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/stats/strided/maxk/test/test.js b/lib/node_modules/@stdlib/stats/strided/maxk/test/test.js new file mode 100644 index 000000000000..47fdac344a51 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/maxk/test/test.js @@ -0,0 +1,38 @@ +/** +* @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 maxk = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof maxk, '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 maxk.ndarray, 'function', 'method is a function' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/strided/maxk/test/test.main.js b/lib/node_modules/@stdlib/stats/strided/maxk/test/test.main.js new file mode 100644 index 000000000000..a2b2e9c841c6 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/maxk/test/test.main.js @@ -0,0 +1,326 @@ +/** +* @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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var Float64Array = require( '@stdlib/array/float64' ); +var maxk = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof maxk, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 6', function test( t ) { + t.strictEqual( maxk.length, 6, 'has expected arity' ); + t.end(); +}); + +tape( 'the function computes the k maximum values of a strided array', function test( t ) { + var expected; + var out; + var x; + + x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + out = [ 0.0, 0.0, 0.0 ]; + expected = [ 3.0, 4.0, 5.0 ]; + + maxk( x.length, 3, x, 1, out, 1 ); + + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function computes the k maximum values of a strided array (accessors)', function test( t ) { + var expected; + var obuf; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + x = toAccessorArray( xbuf ); + obuf = [ 0.0, 0.0, 0.0 ]; + out = toAccessorArray( obuf ); + expected = [ 3.0, 4.0, 5.0 ]; + + maxk( x.length, 3, x, 1, out, 1 ); + + t.deepEqual( obuf, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function computes the k maximum values of a strided array (k == N)', function test( t ) { + var expected; + var out; + var x; + + x = [ 1.0, 2.0, 3.0 ]; + out = [ 0.0, 0.0, 0.0 ]; + expected = [ 1.0, 2.0, 3.0 ]; + + maxk( x.length, 3, x, 1, out, 1 ); + + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function computes the k maximum values of a strided array (k == N, accessors)', function test( t ) { + var expected; + var obuf; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, 3.0 ]; + x = toAccessorArray( xbuf ); + obuf = [ 0.0, 0.0, 0.0 ]; + out = toAccessorArray( obuf ); + expected = [ 1.0, 2.0, 3.0 ]; + + maxk( x.length, 3, x, 1, out, 1 ); + + t.deepEqual( obuf, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided k > N, the function fills remaining elements in output array with NaN', function test( t ) { + var out; + var x; + + x = [ 1.0, 2.0 ]; + out = [ 0.0, 0.0, 0.0 ]; + + maxk( x.length, 3, x, 1, out, 1 ); + + t.strictEqual( out[ 0 ], 1.0, 'returns expected value' ); + t.strictEqual( out[ 1 ], 2.0, 'returns expected value' ); + t.strictEqual( isnan( out[ 2 ] ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided k > N, the function fills remaining elements in output array with NaN (accessors)', function test( t ) { + var obuf; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0 ]; + x = toAccessorArray( xbuf ); + obuf = [ 0.0, 0.0, 0.0 ]; + out = toAccessorArray( obuf ); + + maxk( x.length, 3, x, 1, out, 1 ); + + t.strictEqual( obuf[ 0 ], 1.0, 'returns expected value' ); + t.strictEqual( obuf[ 1 ], 2.0, 'returns expected value' ); + t.strictEqual( isnan( obuf[ 2 ] ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided N <= 0, the function fills output array with NaN', function test( t ) { + var out; + var i; + + out = [ 1.0, 2.0, 3.0 ]; + + maxk( 0, 3, [], 1, out, 1 ); + + for ( i = 0; i < out.length; i++ ) { + t.strictEqual( isnan( out[ i ] ), true, 'returns expected value' ); + } + t.end(); +}); + +tape( 'if provided N <= 0, the function fills output array with NaN (accessors)', function test( t ) { + var obuf; + var out; + var i; + + obuf = [ 1.0, 2.0, 3.0 ]; + out = toAccessorArray( obuf ); + + maxk( 0, 3, toAccessorArray( [] ), 1, out, 1 ); + + for ( i = 0; i < obuf.length; i++ ) { + t.strictEqual( isnan( obuf[ i ] ), true, 'returns expected value' ); + } + t.end(); +}); + +tape( 'if provided k <= 0, the function returns output unchanged', function test( t ) { + var expected; + var out; + var x; + + x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + out = [ 1.0, 2.0, 3.0 ]; + expected = [ 1.0, 2.0, 3.0 ]; + + maxk( x.length, 0, x, 1, out, 1 ); + + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided k <= 0, the function returns output unchanged (accessors)', function test( t ) { + var expected; + var obuf; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + x = toAccessorArray( xbuf ); + obuf = [ 1.0, 2.0, 3.0 ]; + out = toAccessorArray( obuf ); + expected = [ 1.0, 2.0, 3.0 ]; + + maxk( x.length, 0, x, 1, out, 1 ); + + t.deepEqual( obuf, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports providing a stride parameter for `x`', function test( t ) { + var expected; + var out; + var x; + + x = [ + 1.0, // 0 + 2.0, + 3.0, // 1 + 4.0, + 5.0 // 2 + ]; + out = [ 0.0, 0.0, 0.0 ]; + expected = [ 1.0, 3.0, 5.0 ]; + + maxk( 3, 3, x, 2, out, 1 ); + + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports providing a stride parameter for `x` (accessors)', function test( t ) { + var expected; + var obuf; + var xbuf; + var out; + var x; + + xbuf = [ + 1.0, // 0 + 2.0, + 3.0, // 1 + 4.0, + 5.0 // 2 + ]; + x = toAccessorArray( xbuf ); + obuf = [ 0.0, 0.0, 0.0 ]; + out = toAccessorArray( obuf ); + expected = [ 1.0, 3.0, 5.0 ]; + + maxk( 3, 3, x, 2, out, 1 ); + + t.deepEqual( obuf, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports providing a stride parameter for `out`', function test( t ) { + var expected; + var out; + var x; + + x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + out = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ]; + expected = [ 3.0, 0.0, 4.0, 0.0, 5.0, 0.0 ]; + + maxk( x.length, 3, x, 1, out, 2 ); + + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports providing a stride parameter for `out` (accessors)', function test( t ) { + var expected; + var obuf; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + x = toAccessorArray( xbuf ); + obuf = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ]; + out = toAccessorArray( obuf ); + expected = [ 3.0, 0.0, 4.0, 0.0, 5.0, 0.0 ]; + + maxk( x.length, 3, x, 1, out, 2 ); + + t.deepEqual( obuf, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports view offsets', function test( t ) { + var expected; + var out0; + var out1; + var x0; + var x1; + + x0 = new Float64Array([ + 1.0, + 2.0, // 0 + 3.0, + 4.0, // 1 + 5.0, + 6.0 // 2 + ]); + out0 = new Float64Array([ + 7.0, + 0.0, // 0 + 0.0, // 1 + 0.0, // 2 + 8.0 + ]); + expected = new Float64Array([ + 7.0, + 2.0, // 0 + 4.0, // 1 + 6.0, // 2 + 8.0 + ]); + + x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT * 1 ); + out1 = new Float64Array( out0.buffer, out0.BYTES_PER_ELEMENT * 1 ); + + maxk( 3, 3, x1, 2, out1, 1 ); + + t.deepEqual( out0, expected, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/strided/maxk/test/test.ndarray.js b/lib/node_modules/@stdlib/stats/strided/maxk/test/test.ndarray.js new file mode 100644 index 000000000000..8bd97f8c7862 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/maxk/test/test.ndarray.js @@ -0,0 +1,356 @@ +/** +* @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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var maxk = require( './../lib/ndarray.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof maxk, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 8', function test( t ) { + t.strictEqual( maxk.length, 8, 'has expected arity' ); + t.end(); +}); + +tape( 'the function computes the k maximum values of a strided array', function test( t ) { + var expected; + var out; + var x; + + x = [ -5.0, 3.0, -2.0, 8.0, -1.0 ]; + out = [ 0.0, 0.0, 0.0 ]; + expected = [ -1.0, 3.0, 8.0 ]; + + maxk( x.length, 3, x, 1, 0, out, 1, 0 ); + + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function computes the k maximum values of a strided array (accessors)', function test( t ) { + var expected; + var obuf; + var xbuf; + var out; + var x; + + xbuf = [ -5.0, 3.0, -2.0, 8.0, -1.0 ]; + x = toAccessorArray( xbuf ); + obuf = [ 0.0, 0.0, 0.0 ]; + out = toAccessorArray( obuf ); + expected = [ -1.0, 3.0, 8.0 ]; + + maxk( x.length, 3, x, 1, 0, out, 1, 0 ); + + t.deepEqual( obuf, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided k > N, the function fills remaining elements in output array with NaN', function test( t ) { + var out; + var x; + + x = [ 1.0, 2.0 ]; + out = [ 0.0, 0.0, 0.0 ]; + + maxk( x.length, 3, x, 1, 0, out, 1, 0 ); + + t.strictEqual( out[ 0 ], 1.0, 'returns expected value' ); + t.strictEqual( out[ 1 ], 2.0, 'returns expected value' ); + t.strictEqual( isnan( out[ 2 ] ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided k > N, the function fills remaining elements in output array with NaN (accessors)', function test( t ) { + var obuf; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0 ]; + x = toAccessorArray( xbuf ); + obuf = [ 0.0, 0.0, 0.0 ]; + out = toAccessorArray( obuf ); + + maxk( x.length, 3, x, 1, 0, out, 1, 0 ); + + t.strictEqual( obuf[ 0 ], 1.0, 'returns expected value' ); + t.strictEqual( obuf[ 1 ], 2.0, 'returns expected value' ); + t.strictEqual( isnan( obuf[ 2 ] ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided N <= 0, the function fills output array with NaN', function test( t ) { + var out; + var i; + + out = [ 1.0, 2.0, 3.0 ]; + + maxk( 0, 3, [], 1, 0, out, 1, 0 ); + + for ( i = 0; i < out.length; i++ ) { + t.strictEqual( isnan( out[ i ] ), true, 'returns expected value' ); + } + t.end(); +}); + +tape( 'if provided N <= 0, the function fills output array with NaN (accessors)', function test( t ) { + var obuf; + var out; + var i; + + obuf = [ 1.0, 2.0, 3.0 ]; + out = toAccessorArray( obuf ); + + maxk( 0, 3, toAccessorArray( [] ), 1, 0, out, 1, 0 ); + + for ( i = 0; i < obuf.length; i++ ) { + t.strictEqual( isnan( obuf[ i ] ), true, 'returns expected value' ); + } + t.end(); +}); + +tape( 'if provided k <= 0, the function returns output unchanged', function test( t ) { + var expected; + var out; + var x; + + x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + out = [ 1.0, 2.0, 3.0 ]; + expected = [ 1.0, 2.0, 3.0 ]; + + maxk( x.length, 0, x, 1, 0, out, 1, 0 ); + + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided k <= 0, the function returns output unchanged (accessors)', function test( t ) { + var expected; + var obuf; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + x = toAccessorArray( xbuf ); + obuf = [ 1.0, 2.0, 3.0 ]; + out = toAccessorArray( obuf ); + expected = [ 1.0, 2.0, 3.0 ]; + + maxk( x.length, 0, x, 1, 0, out, 1, 0 ); + + t.deepEqual( obuf, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function computes the k maximum values of a strided array (k == N)', function test( t ) { + var expected; + var out; + var x; + + x = [ 1.0, 2.0, 3.0 ]; + out = [ 0.0, 0.0, 0.0 ]; + expected = [ 1.0, 2.0, 3.0 ]; + + maxk( x.length, 3, x, 1, 0, out, 1, 0 ); + + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function computes the k maximum values of a strided array (k == N, accessors)', function test( t ) { + var expected; + var obuf; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, 3.0 ]; + x = toAccessorArray( xbuf ); + obuf = [ 0.0, 0.0, 0.0 ]; + out = toAccessorArray( obuf ); + expected = [ 1.0, 2.0, 3.0 ]; + + maxk( x.length, 3, x, 1, 0, out, 1, 0 ); + + t.deepEqual( obuf, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports providing a stride parameter for `x`', function test( t ) { + var expected; + var out; + var x; + + x = [ + -5.0, // 0 + 3.0, + -2.0, // 1 + 8.0, + -1.0, // 2 + 6.0 + ]; + out = [ 0.0, 0.0, 0.0 ]; + expected = [ -5.0, -2.0, -1.0 ]; + + maxk( 3, 3, x, 2, 0, out, 1, 0 ); + + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports providing a stride parameter for `x` (accessors)', function test( t ) { + var expected; + var obuf; + var xbuf; + var out; + var x; + + xbuf = [ + -5.0, // 0 + 3.0, + -2.0, // 1 + 8.0, + -1.0, // 2 + 6.0 + ]; + x = toAccessorArray( xbuf ); + obuf = [ 0.0, 0.0, 0.0 ]; + out = toAccessorArray( obuf ); + expected = [ -5.0, -2.0, -1.0 ]; + + maxk( 3, 3, x, 2, 0, out, 1, 0 ); + + t.deepEqual( obuf, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports providing a stride parameter for `out`', function test( t ) { + var expected; + var out; + var x; + + x = [ 10.0, 20.0, 30.0, 40.0, 50.0 ]; + out = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ]; + expected = [ 30.0, 0.0, 40.0, 0.0, 50.0, 0.0, 0.0 ]; + + maxk( x.length, 3, x, 1, 0, out, 2, 0 ); + + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports providing a stride parameter for `out` (accessors)', function test( t ) { + var expected; + var xbuf; + var obuf; + var out; + var x; + + xbuf = [ 10.0, 20.0, 30.0, 40.0, 50.0 ]; + x = toAccessorArray( xbuf ); + obuf = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ]; + out = toAccessorArray( obuf ); + expected = [ 30.0, 0.0, 40.0, 0.0, 50.0, 0.0, 0.0 ]; + + maxk( x.length, 3, x, 1, 0, out, 2, 0 ); + + t.deepEqual( obuf, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports providing an offset parameter for `x`', function test( t ) { + var expected; + var out; + var x; + + x = [ -1.0, 1.0, 2.0, 3.0, 4.0, 5.0 ]; + out = [ 0.0, 0.0, 0.0 ]; + expected = [ 3.0, 4.0, 5.0 ]; + + maxk( 5, 3, x, 1, 1, out, 1, 0 ); + + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports providing an offset parameter for `x` (accessors)', function test( t ) { + var expected; + var obuf; + var xbuf; + var out; + var x; + + xbuf = [ -1.0, 1.0, 2.0, 3.0, 4.0, 5.0 ]; + x = toAccessorArray( xbuf ); + obuf = [ 0.0, 0.0, 0.0 ]; + out = toAccessorArray( obuf ); + expected = [ 3.0, 4.0, 5.0 ]; + + maxk( 5, 3, x, 1, 1, out, 1, 0 ); + + t.deepEqual( obuf, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports providing an offset parameter for `out`', function test( t ) { + var expected; + var out; + var x; + + x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + expected = [ 0.0, 3.0, 4.0, 5.0, 0.0 ]; + + maxk( x.length, 3, x, 1, 0, out, 1, 1 ); + + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports providing an offset parameter for `out` (accessors)', function test( t ) { + var expected; + var obuf; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + x = toAccessorArray( xbuf ); + obuf = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + out = toAccessorArray( obuf ); + expected = [ 0.0, 3.0, 4.0, 5.0, 0.0 ]; + + maxk( x.length, 3, x, 1, 0, out, 1, 1 ); + + t.deepEqual( obuf, expected, 'returns expected value' ); + t.end(); +});