From 098cc5b0bdf880c9ea6faeffc89eb843dfc4597b Mon Sep 17 00:00:00 2001 From: aayush0325 Date: Mon, 3 Feb 2025 06:25:54 +0000 Subject: [PATCH 1/8] feat: add support for accessor arrays and refactor stats/base/min --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../@stdlib/stats/base/min/README.md | 47 ++++++------------ .../stats/base/min/benchmark/benchmark.js | 19 +++---- .../base/min/benchmark/benchmark.ndarray.js | 17 ++++--- .../@stdlib/stats/base/min/docs/repl.txt | 32 ++++++------ .../stats/base/min/docs/types/index.d.ts | 12 ++--- .../@stdlib/stats/base/min/examples/index.js | 14 ++---- .../base/min/lib/{min.js => accessors.js} | 49 +++++++++++-------- .../@stdlib/stats/base/min/lib/index.js | 20 +++++--- .../@stdlib/stats/base/min/lib/main.js | 22 +++++++-- .../@stdlib/stats/base/min/lib/ndarray.js | 19 +++---- .../min/test/{test.min.js => test.main.js} | 15 ++---- .../stats/base/min/test/test.ndarray.js | 13 ++--- 12 files changed, 132 insertions(+), 147 deletions(-) rename lib/node_modules/@stdlib/stats/base/min/lib/{min.js => accessors.js} (54%) rename lib/node_modules/@stdlib/stats/base/min/test/{test.min.js => test.main.js} (93%) diff --git a/lib/node_modules/@stdlib/stats/base/min/README.md b/lib/node_modules/@stdlib/stats/base/min/README.md index c2ec19b4bbb5..ef35b4e0bd4e 100644 --- a/lib/node_modules/@stdlib/stats/base/min/README.md +++ b/lib/node_modules/@stdlib/stats/base/min/README.md @@ -36,15 +36,14 @@ limitations under the License. var min = require( '@stdlib/stats/base/min' ); ``` -#### min( N, x, stride ) +#### min( N, x, strideX ) Computes the minimum value of a strided array `x`. ```javascript var x = [ 1.0, -2.0, 2.0 ]; -var N = x.length; -var v = min( N, x, 1 ); +var v = min( x.length, x, 1 ); // returns -2.0 ``` @@ -52,17 +51,14 @@ The function has the following parameters: - **N**: number of indexed elements. - **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array]. -- **stride**: index increment for `x`. +- **strideX**: stride length for `x`. -The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to compute the minimum value of every other element in `x`, +The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the minimum value of every other element in `x`, ```javascript -var floor = require( '@stdlib/math/base/special/floor' ); - var x = [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ]; -var N = floor( x.length / 2 ); -var v = min( N, x, 2 ); +var v = min( 4, x, 2 ); // returns -2.0 ``` @@ -72,42 +68,35 @@ Note that indexing is relative to the first index. To introduce an offset, use [ ```javascript var Float64Array = require( '@stdlib/array/float64' ); -var floor = require( '@stdlib/math/base/special/floor' ); 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 N = floor( x0.length / 2 ); - -var v = min( N, x1, 2 ); +var v = min( 4, x1, 2 ); // returns -2.0 ``` -#### min.ndarray( N, x, stride, offset ) +#### min.ndarray( N, x, strideX, offsetX ) Computes the minimum value of a strided array using alternative indexing semantics. ```javascript var x = [ 1.0, -2.0, 2.0 ]; -var N = x.length; -var v = min.ndarray( N, x, 1, 0 ); +var v = min.ndarray( x.length, x, 1, 0 ); // returns -2.0 ``` The function has the following additional parameters: -- **offset**: starting index for `x`. +- **offsetX**: starting index for `x`. -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 minimum value for every other value in `x` starting from the second value +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 minimum value for every other element in `x` starting from the second element ```javascript -var floor = require( '@stdlib/math/base/special/floor' ); - var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ]; -var N = floor( x.length / 2 ); -var v = min.ndarray( N, x, 2, 1 ); +var v = min.ndarray( 4, x, 2, 1 ); // returns -2.0 ``` @@ -133,18 +122,12 @@ var v = min.ndarray( N, x, 2, 1 ); ```javascript -var randu = require( '@stdlib/random/base/randu' ); -var round = require( '@stdlib/math/base/special/round' ); -var Float64Array = require( '@stdlib/array/float64' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); var min = require( '@stdlib/stats/base/min' ); -var x; -var i; - -x = new Float64Array( 10 ); -for ( i = 0; i < x.length; i++ ) { - x[ i ] = round( (randu()*100.0) - 50.0 ); -} +var x = discreteUniform( 10, -50, 50, { + 'dtype': 'float64' +}); console.log( x ); var v = min( x.length, x, 1 ); diff --git a/lib/node_modules/@stdlib/stats/base/min/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/min/benchmark/benchmark.js index c0cff5646a3f..4befe9ba8087 100644 --- a/lib/node_modules/@stdlib/stats/base/min/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/base/min/benchmark/benchmark.js @@ -21,11 +21,18 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); +var uniform = require( '@stdlib/random/array/uniform' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pow = require( '@stdlib/math/base/special/pow' ); var pkg = require( './../package.json' ).name; -var min = require( './../lib/min.js' ); +var min = require( './../lib/main.js' ); + + +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; // FUNCTIONS // @@ -38,13 +45,7 @@ var min = require( './../lib/min.js' ); * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x; - var i; - - x = []; - for ( i = 0; i < len; i++ ) { - x.push( ( randu()*20.0 ) - 10.0 ); - } + var x = uniform( len, -10, 10, options ); return benchmark; function benchmark( b ) { diff --git a/lib/node_modules/@stdlib/stats/base/min/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/stats/base/min/benchmark/benchmark.ndarray.js index ca60d8f0a87f..e7c5ef0a934c 100644 --- a/lib/node_modules/@stdlib/stats/base/min/benchmark/benchmark.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/min/benchmark/benchmark.ndarray.js @@ -21,13 +21,20 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); +var uniform = require( '@stdlib/random/array/uniform' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pow = require( '@stdlib/math/base/special/pow' ); var pkg = require( './../package.json' ).name; var min = require( './../lib/ndarray.js' ); +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + // FUNCTIONS // /** @@ -38,13 +45,7 @@ var min = require( './../lib/ndarray.js' ); * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x; - var i; - - x = []; - for ( i = 0; i < len; i++ ) { - x.push( ( randu()*20.0 ) - 10.0 ); - } + var x = uniform( len, -10, 10, options ); return benchmark; function benchmark( b ) { diff --git a/lib/node_modules/@stdlib/stats/base/min/docs/repl.txt b/lib/node_modules/@stdlib/stats/base/min/docs/repl.txt index 2eec9e632083..17988021741f 100644 --- a/lib/node_modules/@stdlib/stats/base/min/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/base/min/docs/repl.txt @@ -1,9 +1,9 @@ -{{alias}}( N, x, stride ) +{{alias}}( N, x, strideX ) Computes the minimum value of a strided array. - The `N` and `stride` parameters determine which elements in `x` are accessed - at runtime. + 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. @@ -18,8 +18,8 @@ x: Array|TypedArray Input array. - stride: integer - Index increment. + strideX: integer + Stride length. Returns ------- @@ -33,22 +33,19 @@ > {{alias}}( x.length, x, 1 ) -2.0 - // Using `N` and `stride` parameters: + // Using `N` and stride parameters: > x = [ -2.0, 1.0, 1.0, -5.0, 2.0, -1.0 ]; - > var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 ); - > var stride = 2; - > {{alias}}( N, x, stride ) + > {{alias}}( 3, x, 2 ) -2.0 // Using view offsets: > var x0 = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ] ); > var x1 = new {{alias:@stdlib/array/float64}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); - > N = {{alias:@stdlib/math/base/special/floor}}( x0.length / 2 ); - > stride = 2; - > {{alias}}( N, x1, stride ) + > {{alias}}( 3, x1, 2 ) -2.0 -{{alias}}.ndarray( N, x, stride, offset ) + +{{alias}}.ndarray( N, x, strideX, offsetX ) Computes the minimum value of a strided array using alternative indexing semantics. @@ -64,10 +61,10 @@ x: Array|TypedArray Input array. - stride: integer - Index increment. + strideX: integer + Stride length. - offset: integer + offsetX: integer Starting index. Returns @@ -84,8 +81,7 @@ // Using offset parameter: > var x = [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ]; - > var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 ); - > {{alias}}.ndarray( N, x, 2, 1 ) + > {{alias}}.ndarray( 3, x, 2, 1 ) -2.0 See Also diff --git a/lib/node_modules/@stdlib/stats/base/min/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/min/docs/types/index.d.ts index 4445f3a90dba..0cb29db6f595 100644 --- a/lib/node_modules/@stdlib/stats/base/min/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/base/min/docs/types/index.d.ts @@ -31,7 +31,7 @@ interface Routine { * * @param N - number of indexed elements * @param x - input array - * @param stride - stride length + * @param strideX - stride length * @returns minimum value * * @example @@ -40,15 +40,15 @@ interface Routine { * var v = min( x.length, x, 1 ); * // returns -2.0 */ - ( N: number, x: NumericArray, stride: number ): number; + ( N: number, x: NumericArray, strideX: number ): number; /** * Computes the minimum value of a strided array using alternative indexing semantics. * * @param N - number of indexed elements * @param x - input array - * @param stride - stride length - * @param offset - starting index + * @param strideX - stride length + * @param offsetX - starting index * @returns minimum value * * @example @@ -57,7 +57,7 @@ interface Routine { * var v = min.ndarray( x.length, x, 1, 0 ); * // returns -2.0 */ - ndarray( N: number, x: NumericArray, stride: number, offset: number ): number; + ndarray( N: number, x: NumericArray, strideX: number, offsetX: number ): number; } /** @@ -65,7 +65,7 @@ interface Routine { * * @param N - number of indexed elements * @param x - input array -* @param stride - stride length +* @param strideX - stride length * @returns minimum value * * @example diff --git a/lib/node_modules/@stdlib/stats/base/min/examples/index.js b/lib/node_modules/@stdlib/stats/base/min/examples/index.js index 4276ada2661b..352b078d7289 100644 --- a/lib/node_modules/@stdlib/stats/base/min/examples/index.js +++ b/lib/node_modules/@stdlib/stats/base/min/examples/index.js @@ -18,18 +18,12 @@ 'use strict'; -var randu = require( '@stdlib/random/base/randu' ); -var round = require( '@stdlib/math/base/special/round' ); -var Float64Array = require( '@stdlib/array/float64' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); var min = require( './../lib' ); -var x; -var i; - -x = new Float64Array( 10 ); -for ( i = 0; i < x.length; i++ ) { - x[ i ] = round( (randu()*100.0) - 50.0 ); -} +var x = discreteUniform( 10, -50, 50, { + 'dtype': 'float64' +}); console.log( x ); var v = min( x.length, x, 1 ); diff --git a/lib/node_modules/@stdlib/stats/base/min/lib/min.js b/lib/node_modules/@stdlib/stats/base/min/lib/accessors.js similarity index 54% rename from lib/node_modules/@stdlib/stats/base/min/lib/min.js rename to lib/node_modules/@stdlib/stats/base/min/lib/accessors.js index 35df296cf0b4..f6a1782425c7 100644 --- a/lib/node_modules/@stdlib/stats/base/min/lib/min.js +++ b/lib/node_modules/@stdlib/stats/base/min/lib/accessors.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2020 The Stdlib Authors. +* Copyright (c) 2025 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. @@ -27,41 +27,48 @@ var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' ); // MAIN // /** -* Computes the minimum value of a strided array. +* Computes the maximum value of a strided array. * +* @private * @param {PositiveInteger} N - number of indexed elements -* @param {NumericArray} x - input array -* @param {integer} stride - stride length +* @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 +* @param {NonNegativeInteger} offsetX - starting index * @returns {number} minimum value * * @example -* var x = [ 1.0, -2.0, 2.0 ]; -* var N = x.length; +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); * -* var v = min( N, x, 1 ); +* var x = toAccessorArray( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); +* +* var v = min( 4, arraylike2object( x ), 2, 1 ); * // returns -2.0 */ -function min( N, x, stride ) { +function min( N, x, strideX, offsetX ) { + var xbuf; + var get; var min; var ix; var v; var i; - if ( N <= 0 ) { - return NaN; - } - if ( N === 1 || stride === 0 ) { - return x[ 0 ]; - } - if ( stride < 0 ) { - ix = (1-N) * stride; - } else { - ix = 0; + // Cache reference to array data: + xbuf = x.data; + + // Cache a reference to the element accessor: + get = x.accessors[ 0 ]; + + if ( N === 1 || strideX === 0 ) { + return get( xbuf, offsetX ); } - min = x[ ix ]; + ix = offsetX; + min = get( xbuf, ix ); for ( i = 1; i < N; i++ ) { - ix += stride; - v = x[ ix ]; + ix += strideX; + v = get( xbuf, ix ); if ( isnan( v ) ) { return v; } diff --git a/lib/node_modules/@stdlib/stats/base/min/lib/index.js b/lib/node_modules/@stdlib/stats/base/min/lib/index.js index e91e3cca83f2..0457766f0283 100644 --- a/lib/node_modules/@stdlib/stats/base/min/lib/index.js +++ b/lib/node_modules/@stdlib/stats/base/min/lib/index.js @@ -27,29 +27,33 @@ * var min = require( '@stdlib/stats/base/min' ); * * var x = [ 1.0, -2.0, 2.0 ]; -* var N = x.length; * -* var v = min( N, x, 1 ); +* var v = min( x.length, x, 1 ); * // returns -2.0 * * @example -* var floor = require( '@stdlib/math/base/special/floor' ); * var min = require( '@stdlib/stats/base/min' ); * * var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ]; -* var N = floor( x.length / 2 ); * -* var v = min.ndarray( N, x, 2, 1 ); +* var v = min.ndarray( 4, x, 2, 1 ); * // returns -2.0 */ // MODULES // -var min = require( './main.js' ); +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 = min; +module.exports = main; -// exports: { "ndarray": "min.ndarray" } +// exports: { "ndarray": "main.ndarray" } diff --git a/lib/node_modules/@stdlib/stats/base/min/lib/main.js b/lib/node_modules/@stdlib/stats/base/min/lib/main.js index 71a9f1778466..64770703bd91 100644 --- a/lib/node_modules/@stdlib/stats/base/min/lib/main.js +++ b/lib/node_modules/@stdlib/stats/base/min/lib/main.js @@ -20,14 +20,30 @@ // MODULES // -var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); -var min = require( './min.js' ); +var stride2offset = require( '@stdlib/strided/base/stride2offset' ); var ndarray = require( './ndarray.js' ); // MAIN // -setReadOnly( min, 'ndarray', ndarray ); +/** +* Computes the minimum value of a strided array. +* +* @param {PositiveInteger} N - number of indexed elements +* @param {NumericArray} x - input array +* @param {integer} strideX - stride length +* @returns {number} minimum value +* +* @example +* var x = [ 1.0, -2.0, 2.0 ]; +* var N = x.length; +* +* var v = min( N, x, 1 ); +* // returns -2.0 +*/ +function min( N, x, strideX ) { + return ndarray( N, x, strideX, stride2offset( N, strideX ) ); +} // EXPORTS // diff --git a/lib/node_modules/@stdlib/stats/base/min/lib/ndarray.js b/lib/node_modules/@stdlib/stats/base/min/lib/ndarray.js index 7cfa3e15773f..1fb50f570f1a 100644 --- a/lib/node_modules/@stdlib/stats/base/min/lib/ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/min/lib/ndarray.js @@ -31,20 +31,17 @@ var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' ); * * @param {PositiveInteger} N - number of indexed elements * @param {NumericArray} x - input array -* @param {integer} stride - stride length -* @param {NonNegativeInteger} offset - starting index +* @param {integer} strideX - stride length +* @param {NonNegativeInteger} offsetX - starting index * @returns {number} minimum value * * @example -* var floor = require( '@stdlib/math/base/special/floor' ); -* * var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ]; -* var N = floor( x.length / 2 ); * -* var v = min( N, x, 2, 1 ); +* var v = min( 4, x, 2, 1 ); * // returns -2.0 */ -function min( N, x, stride, offset ) { +function min( N, x, strideX, offsetX ) { var min; var ix; var v; @@ -53,13 +50,13 @@ function min( N, x, stride, offset ) { if ( N <= 0 ) { return NaN; } - if ( N === 1 || stride === 0 ) { - return x[ offset ]; + if ( N === 1 || strideX === 0 ) { + return x[ offsetX ]; } - ix = offset; + ix = offsetX; min = x[ ix ]; for ( i = 1; i < N; i++ ) { - ix += stride; + ix += strideX; v = x[ ix ]; if ( isnan( v ) ) { return v; diff --git a/lib/node_modules/@stdlib/stats/base/min/test/test.min.js b/lib/node_modules/@stdlib/stats/base/min/test/test.main.js similarity index 93% rename from lib/node_modules/@stdlib/stats/base/min/test/test.min.js rename to lib/node_modules/@stdlib/stats/base/min/test/test.main.js index c913b26878fc..65a664bd2e32 100644 --- a/lib/node_modules/@stdlib/stats/base/min/test/test.min.js +++ b/lib/node_modules/@stdlib/stats/base/min/test/test.main.js @@ -21,11 +21,10 @@ // MODULES // var tape = require( 'tape' ); -var floor = require( '@stdlib/math/base/special/floor' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' ); var Float64Array = require( '@stdlib/array/float64' ); -var min = require( './../lib/min.js' ); +var min = require( './../lib/main.js' ); // TESTS // @@ -96,7 +95,6 @@ tape( 'if provided an `N` parameter equal to `1`, the function returns the first }); tape( 'the function supports a `stride` parameter', function test( t ) { - var N; var x; var v; @@ -111,15 +109,13 @@ tape( 'the function supports a `stride` parameter', function test( t ) { 2.0 ]; - N = floor( x.length / 2 ); - v = min( N, x, 2 ); + v = min( 4, x, 2 ); t.strictEqual( v, -2.0, 'returns expected value' ); t.end(); }); tape( 'the function supports a negative `stride` parameter', function test( t ) { - var N; var x; var v; @@ -134,8 +130,7 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) 2.0 ]; - N = floor( x.length / 2 ); - v = min( N, x, -2 ); + v = min( 4, x, -2 ); t.strictEqual( v, -2.0, 'returns expected value' ); t.end(); @@ -156,7 +151,6 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns the f tape( 'the function supports view offsets', function test( t ) { var x0; var x1; - var N; var v; x0 = new Float64Array([ @@ -172,9 +166,8 @@ tape( 'the function supports view offsets', function test( t ) { ]); x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element - N = floor(x1.length / 2); - v = min( N, x1, 2 ); + v = min( 4, x1, 2 ); t.strictEqual( v, -2.0, 'returns expected value' ); t.end(); diff --git a/lib/node_modules/@stdlib/stats/base/min/test/test.ndarray.js b/lib/node_modules/@stdlib/stats/base/min/test/test.ndarray.js index 68047fd3d67e..4bb85d52c9ef 100644 --- a/lib/node_modules/@stdlib/stats/base/min/test/test.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/min/test/test.ndarray.js @@ -21,7 +21,6 @@ // MODULES // var tape = require( 'tape' ); -var floor = require( '@stdlib/math/base/special/floor' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' ); var min = require( './../lib/ndarray.js' ); @@ -95,7 +94,6 @@ tape( 'if provided an `N` parameter equal to `1`, the function returns the first }); tape( 'the function supports a `stride` parameter', function test( t ) { - var N; var x; var v; @@ -110,15 +108,13 @@ tape( 'the function supports a `stride` parameter', function test( t ) { 2.0 ]; - N = floor( x.length / 2 ); - v = min( N, x, 2, 0 ); + v = min( 4, x, 2, 0 ); t.strictEqual( v, -2.0, 'returns expected value' ); t.end(); }); tape( 'the function supports a negative `stride` parameter', function test( t ) { - var N; var x; var v; @@ -133,8 +129,7 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) 2.0 ]; - N = floor( x.length / 2 ); - v = min( N, x, -2, 6 ); + v = min( 4, x, -2, 6 ); t.strictEqual( v, -2.0, 'returns expected value' ); t.end(); @@ -153,7 +148,6 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns the f }); tape( 'the function supports an `offset` parameter', function test( t ) { - var N; var x; var v; @@ -167,9 +161,8 @@ tape( 'the function supports an `offset` parameter', function test( t ) { 3.0, 4.0 // 3 ]; - N = floor( x.length / 2 ); - v = min( N, x, 2, 1 ); + v = min( 4, x, 2, 1 ); t.strictEqual( v, -2.0, 'returns expected value' ); t.end(); From 022fc9b0e782f0a551ebcc0b3a3b6ee998d35a49 Mon Sep 17 00:00:00 2001 From: aayush0325 Date: Mon, 3 Feb 2025 11:03:58 +0000 Subject: [PATCH 2/8] feat: accessor arrays tests --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../@stdlib/stats/base/min/lib/ndarray.js | 7 ++ .../@stdlib/stats/base/min/test/test.main.js | 70 ++++++++++++++ .../stats/base/min/test/test.ndarray.js | 91 +++++++++++++++++++ 3 files changed, 168 insertions(+) diff --git a/lib/node_modules/@stdlib/stats/base/min/lib/ndarray.js b/lib/node_modules/@stdlib/stats/base/min/lib/ndarray.js index 1fb50f570f1a..126a0351bb7d 100644 --- a/lib/node_modules/@stdlib/stats/base/min/lib/ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/min/lib/ndarray.js @@ -22,6 +22,8 @@ var isnan = require( '@stdlib/math/base/assert/is-nan' ); var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' ); +var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +var accessors = require( './accessors.js' ); // MAIN // @@ -44,12 +46,17 @@ var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' ); function min( N, x, strideX, offsetX ) { var min; var ix; + var o; var v; var i; if ( N <= 0 ) { return NaN; } + o = arraylike2object( x ); + if ( o.accessorProtocol ) { + return accessors( N, o, strideX, offsetX ); + } if ( N === 1 || strideX === 0 ) { return x[ offsetX ]; } diff --git a/lib/node_modules/@stdlib/stats/base/min/test/test.main.js b/lib/node_modules/@stdlib/stats/base/min/test/test.main.js index 65a664bd2e32..828502fcf507 100644 --- a/lib/node_modules/@stdlib/stats/base/min/test/test.main.js +++ b/lib/node_modules/@stdlib/stats/base/min/test/test.main.js @@ -23,6 +23,7 @@ var tape = require( 'tape' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); var Float64Array = require( '@stdlib/array/float64' ); var min = require( './../lib/main.js' ); @@ -67,6 +68,33 @@ tape( 'the function calculates the minimum value of a strided array', function t t.end(); }); +tape( 'the function calculates the minimum value of a strided array (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + v = min( x.length, toAccessorArray( x ), 1 ); + t.strictEqual( v, -4.0, 'returns expected value' ); + + x = [ -4.0, -5.0 ]; + v = min( x.length, toAccessorArray( x ), 1 ); + t.strictEqual( v, -5.0, 'returns expected value' ); + + x = [ 0.0, -0.0, 0.0 ]; + v = min( x.length, toAccessorArray( x ), 1 ); + t.strictEqual( isNegativeZero( v ), true, 'returns expected value' ); + + x = [ NaN ]; + v = min( x.length, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ NaN, NaN ]; + v = min( x.length, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `NaN`', function test( t ) { var x; var v; @@ -115,6 +143,27 @@ tape( 'the function supports a `stride` parameter', function test( t ) { t.end(); }); +tape( 'the function supports a `stride` parameter (accessors)', function test( t ) { + var x; + var v; + + x = [ + 1.0, // 0 + 2.0, + 2.0, // 1 + -7.0, + -2.0, // 2 + 3.0, + 4.0, // 3 + 2.0 + ]; + + v = min( 4, toAccessorArray( x ), 2 ); + + t.strictEqual( v, -2.0, 'returns expected value' ); + t.end(); +}); + tape( 'the function supports a negative `stride` parameter', function test( t ) { var x; var v; @@ -136,6 +185,27 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) t.end(); }); +tape( 'the function supports a negative `stride` parameter (accessors)', function test( t ) { + var x; + var v; + + x = [ + 1.0, // 3 + 2.0, + 2.0, // 2 + -7.0, + -2.0, // 1 + 3.0, + 4.0, // 0 + 2.0 + ]; + + v = min( 4, toAccessorArray( x ), -2 ); + + t.strictEqual( v, -2.0, 'returns expected value' ); + t.end(); +}); + tape( 'if provided a `stride` parameter equal to `0`, the function returns the first element', function test( t ) { var x; var v; diff --git a/lib/node_modules/@stdlib/stats/base/min/test/test.ndarray.js b/lib/node_modules/@stdlib/stats/base/min/test/test.ndarray.js index 4bb85d52c9ef..81e6c500d182 100644 --- a/lib/node_modules/@stdlib/stats/base/min/test/test.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/min/test/test.ndarray.js @@ -23,6 +23,7 @@ var tape = require( 'tape' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); var min = require( './../lib/ndarray.js' ); @@ -66,6 +67,33 @@ tape( 'the function calculates the minimum value of a strided array', function t t.end(); }); +tape( 'the function calculates the minimum value of a strided array (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + v = min( x.length, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, -4.0, 'returns expected value' ); + + x = [ -4.0, -5.0 ]; + v = min( x.length, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, -5.0, 'returns expected value' ); + + x = [ 0.0, -0.0, 0.0 ]; + v = min( x.length, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isNegativeZero( v ), true, 'returns expected value' ); + + x = [ NaN ]; + v = min( x.length, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ NaN, NaN ]; + v = min( x.length, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `NaN`', function test( t ) { var x; var v; @@ -114,6 +142,27 @@ tape( 'the function supports a `stride` parameter', function test( t ) { t.end(); }); +tape( 'the function supports a `stride` parameter (accessors)', function test( t ) { + var x; + var v; + + x = [ + 1.0, // 0 + 2.0, + 2.0, // 1 + -7.0, + -2.0, // 2 + 3.0, + 4.0, // 3 + 2.0 + ]; + + v = min( 4, toAccessorArray( x ), 2, 0 ); + + t.strictEqual( v, -2.0, 'returns expected value' ); + t.end(); +}); + tape( 'the function supports a negative `stride` parameter', function test( t ) { var x; var v; @@ -135,6 +184,27 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) t.end(); }); +tape( 'the function supports a negative `stride` parameter (accessors)', function test( t ) { + var x; + var v; + + x = [ + 1.0, // 3 + 2.0, + 2.0, // 2 + -7.0, + -2.0, // 1 + 3.0, + 4.0, // 0 + 2.0 + ]; + + v = min( 4, toAccessorArray( x ), -2, 6 ); + + t.strictEqual( v, -2.0, 'returns expected value' ); + t.end(); +}); + tape( 'if provided a `stride` parameter equal to `0`, the function returns the first indexed element', function test( t ) { var x; var v; @@ -167,3 +237,24 @@ tape( 'the function supports an `offset` parameter', function test( t ) { t.end(); }); + +tape( 'the function supports an `offset` parameter (accessors)', function test( t ) { + var x; + var v; + + x = [ + 2.0, + 1.0, // 0 + 2.0, + -2.0, // 1 + -2.0, + 2.0, // 2 + 3.0, + 4.0 // 3 + ]; + + v = min( 4, toAccessorArray( x ), 2, 1 ); + t.strictEqual( v, -2.0, 'returns expected value' ); + + t.end(); +}); From 949c89a0cab275e2225c5a18ec8c5f4855c237e0 Mon Sep 17 00:00:00 2001 From: aayush0325 Date: Tue, 4 Feb 2025 12:29:48 +0000 Subject: [PATCH 3/8] docs: add docs for accessor arrays --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- lib/node_modules/@stdlib/stats/base/min/README.md | 3 +++ .../@stdlib/stats/base/min/docs/types/index.d.ts | 11 ++++++++--- .../@stdlib/stats/base/min/docs/types/test.ts | 3 +++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/min/README.md b/lib/node_modules/@stdlib/stats/base/min/README.md index ef35b4e0bd4e..47fa8fc7a6c6 100644 --- a/lib/node_modules/@stdlib/stats/base/min/README.md +++ b/lib/node_modules/@stdlib/stats/base/min/README.md @@ -109,6 +109,7 @@ var v = min.ndarray( 4, x, 2, 1 ); ## Notes - If `N <= 0`, both functions return `NaN`. +- 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]). - Depending on the environment, the typed versions ([`dmin`][@stdlib/stats/base/dmin], [`smin`][@stdlib/stats/base/smin], etc.) are likely to be significantly more performant. @@ -163,6 +164,8 @@ console.log( v ); [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray +[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor + [@stdlib/stats/base/dmin]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/dmin [@stdlib/stats/base/smin]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/smin diff --git a/lib/node_modules/@stdlib/stats/base/min/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/min/docs/types/index.d.ts index 0cb29db6f595..c3471761df54 100644 --- a/lib/node_modules/@stdlib/stats/base/min/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/base/min/docs/types/index.d.ts @@ -20,7 +20,12 @@ /// -import { NumericArray } from '@stdlib/types/array'; +import { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array'; + +/** +* Input array. +*/ +type InputArray = NumericArray | Collection | AccessorArrayLike; /** * Interface describing `min`. @@ -40,7 +45,7 @@ interface Routine { * var v = min( x.length, x, 1 ); * // returns -2.0 */ - ( N: number, x: NumericArray, strideX: number ): number; + ( N: number, x: InputArray, strideX: number ): number; /** * Computes the minimum value of a strided array using alternative indexing semantics. @@ -57,7 +62,7 @@ interface Routine { * var v = min.ndarray( x.length, x, 1, 0 ); * // returns -2.0 */ - ndarray( N: number, x: NumericArray, strideX: number, offsetX: number ): number; + ndarray( N: number, x: InputArray, strideX: number, offsetX: number ): number; } /** diff --git a/lib/node_modules/@stdlib/stats/base/min/docs/types/test.ts b/lib/node_modules/@stdlib/stats/base/min/docs/types/test.ts index 235e7a466c23..1e0ee79ff23b 100644 --- a/lib/node_modules/@stdlib/stats/base/min/docs/types/test.ts +++ b/lib/node_modules/@stdlib/stats/base/min/docs/types/test.ts @@ -16,6 +16,7 @@ * limitations under the License. */ +import AccessorArray = require( '@stdlib/array/base/accessor' ); import min = require( './index' ); @@ -26,6 +27,7 @@ import min = require( './index' ); const x = new Float64Array( 10 ); min( x.length, x, 1 ); // $ExpectType number + min( x.length, new AccessorArray( x ), 1 ); // $ExpectType number } // The compiler throws an error if the function is provided a first argument which is not a number... @@ -85,6 +87,7 @@ import min = require( './index' ); const x = new Float64Array( 10 ); min.ndarray( x.length, x, 1, 0 ); // $ExpectType number + min.ndarray( x.length, new AccessorArray( x ), 1, 0 ); // $ExpectType number } // The compiler throws an error if the `ndarray` method is provided a first argument which is not a number... From 7ec0df69c40ff26f963d7d53d7eb576232bb4b97 Mon Sep 17 00:00:00 2001 From: Aayush Khanna <96649223+aayush0325@users.noreply.github.com> Date: Tue, 4 Feb 2025 18:09:25 +0530 Subject: [PATCH 4/8] chore: code review --- lib/node_modules/@stdlib/stats/base/min/lib/accessors.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/stats/base/min/lib/accessors.js b/lib/node_modules/@stdlib/stats/base/min/lib/accessors.js index f6a1782425c7..8b339a2783e9 100644 --- a/lib/node_modules/@stdlib/stats/base/min/lib/accessors.js +++ b/lib/node_modules/@stdlib/stats/base/min/lib/accessors.js @@ -27,7 +27,7 @@ var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' ); // MAIN // /** -* Computes the maximum value of a strided array. +* Computes the minimum value of a strided array. * * @private * @param {PositiveInteger} N - number of indexed elements From 839743a226db3a27a0e06757f1b9683767eadde3 Mon Sep 17 00:00:00 2001 From: Athan Date: Tue, 4 Feb 2025 19:35:30 -0800 Subject: [PATCH 5/8] docs: update copy Signed-off-by: Athan --- lib/node_modules/@stdlib/stats/base/min/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/stats/base/min/README.md b/lib/node_modules/@stdlib/stats/base/min/README.md index 47fa8fc7a6c6..254742a88e46 100644 --- a/lib/node_modules/@stdlib/stats/base/min/README.md +++ b/lib/node_modules/@stdlib/stats/base/min/README.md @@ -91,7 +91,7 @@ The function has the following additional parameters: - **offsetX**: starting index for `x`. -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 minimum value for every other element in `x` starting from the second element +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 minimum value for every other element in the input strided array starting from the second element ```javascript var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ]; From d3979db6829063c15c518db69dca5eca1f739b3a Mon Sep 17 00:00:00 2001 From: Athan Date: Tue, 4 Feb 2025 20:05:21 -0800 Subject: [PATCH 6/8] docs: update copy Signed-off-by: Athan --- lib/node_modules/@stdlib/stats/base/min/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/stats/base/min/README.md b/lib/node_modules/@stdlib/stats/base/min/README.md index 254742a88e46..2d7f1e0c2e96 100644 --- a/lib/node_modules/@stdlib/stats/base/min/README.md +++ b/lib/node_modules/@stdlib/stats/base/min/README.md @@ -91,7 +91,7 @@ The function has the following additional parameters: - **offsetX**: starting index for `x`. -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 minimum value for every other element in the input strided array starting from the second element +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 minimum value for every other element in the strided array starting from the second element ```javascript var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ]; From a754c57b77b009e4a6c8bb5657a88212de4012d6 Mon Sep 17 00:00:00 2001 From: Athan Date: Tue, 4 Feb 2025 20:10:11 -0800 Subject: [PATCH 7/8] test: add test case Signed-off-by: Athan --- .../@stdlib/stats/base/min/test/test.main.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/node_modules/@stdlib/stats/base/min/test/test.main.js b/lib/node_modules/@stdlib/stats/base/min/test/test.main.js index 828502fcf507..2c6aac7fc3b8 100644 --- a/lib/node_modules/@stdlib/stats/base/min/test/test.main.js +++ b/lib/node_modules/@stdlib/stats/base/min/test/test.main.js @@ -218,6 +218,19 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns the f t.end(); }); +tape( 'if provided a `stride` parameter equal to `0`, the function returns the first element (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = min( x.length, toAccessorArray( x ), 0 ); + t.strictEqual( v, 1.0, 'returns expected value' ); + + t.end(); +}); + + tape( 'the function supports view offsets', function test( t ) { var x0; var x1; From e0c24d1cf4c299d14abab5243bd95d718946276e Mon Sep 17 00:00:00 2001 From: Athan Date: Tue, 4 Feb 2025 20:10:54 -0800 Subject: [PATCH 8/8] test: add test case Signed-off-by: Athan --- .../@stdlib/stats/base/min/test/test.ndarray.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/node_modules/@stdlib/stats/base/min/test/test.ndarray.js b/lib/node_modules/@stdlib/stats/base/min/test/test.ndarray.js index 81e6c500d182..668871a8037d 100644 --- a/lib/node_modules/@stdlib/stats/base/min/test/test.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/min/test/test.ndarray.js @@ -217,6 +217,18 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns the f t.end(); }); +tape( 'if provided a `stride` parameter equal to `0`, the function returns the first indexed element (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = min( x.length, toAccessorArray( x ), 0, 0 ); + t.strictEqual( v, 1.0, 'returns expected value' ); + + t.end(); +}); + tape( 'the function supports an `offset` parameter', function test( t ) { var x; var v;