From 013c80d37a7d325c00abe4311a368ce3f9b033a3 Mon Sep 17 00:00:00 2001 From: aayush0325 Date: Thu, 6 Feb 2025 21:50:52 +0000 Subject: [PATCH 1/2] refactor: refactor implementation --- 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/minabs/README.md | 47 ++++------- .../stats/base/minabs/benchmark/benchmark.js | 19 ++--- .../minabs/benchmark/benchmark.ndarray.js | 17 ++-- .../@stdlib/stats/base/minabs/docs/repl.txt | 32 ++++---- .../stats/base/minabs/docs/types/index.d.ts | 12 +-- .../stats/base/minabs/examples/index.js | 14 +--- .../@stdlib/stats/base/minabs/lib/index.js | 20 +++-- .../@stdlib/stats/base/minabs/lib/main.js | 21 ++++- .../@stdlib/stats/base/minabs/lib/minabs.js | 78 ------------------- .../@stdlib/stats/base/minabs/lib/ndarray.js | 19 ++--- .../stats/base/minabs/test/test.minabs.js | 15 +--- .../stats/base/minabs/test/test.ndarray.js | 13 +--- 12 files changed, 103 insertions(+), 204 deletions(-) delete mode 100644 lib/node_modules/@stdlib/stats/base/minabs/lib/minabs.js diff --git a/lib/node_modules/@stdlib/stats/base/minabs/README.md b/lib/node_modules/@stdlib/stats/base/minabs/README.md index a9a79fd743eb..1abdeeecad9b 100644 --- a/lib/node_modules/@stdlib/stats/base/minabs/README.md +++ b/lib/node_modules/@stdlib/stats/base/minabs/README.md @@ -36,15 +36,14 @@ limitations under the License. var minabs = require( '@stdlib/stats/base/minabs' ); ``` -#### minabs( N, x, stride ) +#### minabs( N, x, strideX ) Computes the minimum absolute value of a strided array `x`. ```javascript var x = [ 1.0, -2.0, 2.0 ]; -var N = x.length; -var v = minabs( N, x, 1 ); +var v = minabs( x.length, x, 1 ); // returns 1.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 absolute 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 absolute 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 = minabs( N, x, 2 ); +var v = minabs( 4, x, 2 ); // returns 1.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 = minabs( N, x1, 2 ); +var v = minabs( 4, x1, 2 ); // returns 1.0 ``` -#### minabs.ndarray( N, x, stride, offset ) +#### minabs.ndarray( N, x, strideX, offsetX ) Computes the minimum absolute value of a strided array using alternative indexing semantics. ```javascript var x = [ 1.0, -2.0, 2.0 ]; -var N = x.length; -var v = minabs.ndarray( N, x, 1, 0 ); +var v = minabs.ndarray( x.length, x, 1, 0 ); // returns 1.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 absolute 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 absolute 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 = minabs.ndarray( N, x, 2, 1 ); +var v = minabs.ndarray( 4, x, 2, 1 ); // returns 1.0 ``` @@ -133,18 +122,12 @@ var v = minabs.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 minabs = require( '@stdlib/stats/base/minabs' ); -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 = minabs( x.length, x, 1 ); diff --git a/lib/node_modules/@stdlib/stats/base/minabs/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/minabs/benchmark/benchmark.js index 09163f7da1e5..d80e79e82f05 100644 --- a/lib/node_modules/@stdlib/stats/base/minabs/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/base/minabs/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 minabs = require( './../lib/minabs.js' ); +var minabs = require( './../lib/main.js' ); + + +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; // FUNCTIONS // @@ -38,13 +45,7 @@ var minabs = require( './../lib/minabs.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/minabs/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/stats/base/minabs/benchmark/benchmark.ndarray.js index c1b1202a0bec..4caac42748ea 100644 --- a/lib/node_modules/@stdlib/stats/base/minabs/benchmark/benchmark.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/minabs/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 minabs = require( './../lib/ndarray.js' ); +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + // FUNCTIONS // /** @@ -38,13 +45,7 @@ var minabs = 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/minabs/docs/repl.txt b/lib/node_modules/@stdlib/stats/base/minabs/docs/repl.txt index e70e40a715bd..b7e38f25ddc0 100644 --- a/lib/node_modules/@stdlib/stats/base/minabs/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/base/minabs/docs/repl.txt @@ -1,9 +1,9 @@ -{{alias}}( N, x, stride ) +{{alias}}( N, x, strideX ) Computes the minimum absolute 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 ) 1.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 ) 1.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 ) 1.0 -{{alias}}.ndarray( N, x, stride, offset ) + +{{alias}}.ndarray( N, x, strideX, offsetX ) Computes the minimum absolute 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 ) 1.0 See Also diff --git a/lib/node_modules/@stdlib/stats/base/minabs/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/minabs/docs/types/index.d.ts index d70f390f55ca..d952d125ab78 100644 --- a/lib/node_modules/@stdlib/stats/base/minabs/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/base/minabs/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 absolute value * * @example @@ -40,15 +40,15 @@ interface Routine { * var v = minabs( x.length, x, 1 ); * // returns 1.0 */ - ( N: number, x: NumericArray, stride: number ): number; + ( N: number, x: NumericArray, strideX: number ): number; /** * Computes the minimum absolute 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 absolute value * * @example @@ -57,7 +57,7 @@ interface Routine { * var v = minabs.ndarray( x.length, x, 1, 0 ); * // returns 1.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 absolute value * * @example diff --git a/lib/node_modules/@stdlib/stats/base/minabs/examples/index.js b/lib/node_modules/@stdlib/stats/base/minabs/examples/index.js index 9ba4c26d52b9..bfad39f9dc19 100644 --- a/lib/node_modules/@stdlib/stats/base/minabs/examples/index.js +++ b/lib/node_modules/@stdlib/stats/base/minabs/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 minabs = 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 = minabs( x.length, x, 1 ); diff --git a/lib/node_modules/@stdlib/stats/base/minabs/lib/index.js b/lib/node_modules/@stdlib/stats/base/minabs/lib/index.js index 18de3319a629..c62e0afd782a 100644 --- a/lib/node_modules/@stdlib/stats/base/minabs/lib/index.js +++ b/lib/node_modules/@stdlib/stats/base/minabs/lib/index.js @@ -27,29 +27,33 @@ * var minabs = require( '@stdlib/stats/base/minabs' ); * * var x = [ 1.0, -2.0, 2.0 ]; -* var N = x.length; * -* var v = minabs( N, x, 1 ); +* var v = minabs( x.length, x, 1 ); * // returns 1.0 * * @example -* var floor = require( '@stdlib/math/base/special/floor' ); * var minabs = require( '@stdlib/stats/base/minabs' ); * * 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 = minabs.ndarray( N, x, 2, 1 ); +* var v = minabs.ndarray( 4, x, 2, 1 ); * // returns 1.0 */ // MODULES // -var minabs = 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 = minabs; +module.exports = main; -// exports: { "ndarray": "minabs.ndarray" } +// exports: { "ndarray": "main.ndarray" } diff --git a/lib/node_modules/@stdlib/stats/base/minabs/lib/main.js b/lib/node_modules/@stdlib/stats/base/minabs/lib/main.js index bbb8ac55c5e0..91a9b79c5e10 100644 --- a/lib/node_modules/@stdlib/stats/base/minabs/lib/main.js +++ b/lib/node_modules/@stdlib/stats/base/minabs/lib/main.js @@ -20,14 +20,29 @@ // MODULES // -var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); -var minabs = require( './minabs.js' ); +var stride2offset = require( '@stdlib/strided/base/stride2offset' ); var ndarray = require( './ndarray.js' ); // MAIN // -setReadOnly( minabs, 'ndarray', ndarray ); +/** +* Computes the minimum absolute 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 absolute value +* +* @example +* var x = [ 1.0, -2.0, 2.0 ]; +* +* var v = minabs( x.length, x, 1 ); +* // returns 1.0 +*/ +function minabs( N, x, strideX ) { + return ndarray( N, x, strideX, stride2offset( N, strideX ) ); +} // EXPORTS // diff --git a/lib/node_modules/@stdlib/stats/base/minabs/lib/minabs.js b/lib/node_modules/@stdlib/stats/base/minabs/lib/minabs.js deleted file mode 100644 index a9264217a5ec..000000000000 --- a/lib/node_modules/@stdlib/stats/base/minabs/lib/minabs.js +++ /dev/null @@ -1,78 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2020 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 isnan = require( '@stdlib/math/base/assert/is-nan' ); -var abs = require( '@stdlib/math/base/special/abs' ); - - -// MAIN // - -/** -* Computes the minimum absolute value of a strided array. -* -* @param {PositiveInteger} N - number of indexed elements -* @param {NumericArray} x - input array -* @param {integer} stride - stride length -* @returns {number} minimum absolute value -* -* @example -* var x = [ 1.0, -2.0, 2.0 ]; -* var N = x.length; -* -* var v = minabs( N, x, 1 ); -* // returns 1.0 -*/ -function minabs( N, x, stride ) { - var min; - var ix; - var v; - var i; - - if ( N <= 0 ) { - return NaN; - } - if ( N === 1 || stride === 0 ) { - return abs( x[ 0 ] ); - } - if ( stride < 0 ) { - ix = (1-N) * stride; - } else { - ix = 0; - } - min = abs( x[ ix ] ); - for ( i = 1; i < N; i++ ) { - ix += stride; - v = abs( x[ ix ] ); - if ( isnan( v ) ) { - return v; - } - if ( v < min ) { - min = v; - } - } - return min; -} - - -// EXPORTS // - -module.exports = minabs; diff --git a/lib/node_modules/@stdlib/stats/base/minabs/lib/ndarray.js b/lib/node_modules/@stdlib/stats/base/minabs/lib/ndarray.js index 527e130cd8ac..1ceea0e3c43d 100644 --- a/lib/node_modules/@stdlib/stats/base/minabs/lib/ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/minabs/lib/ndarray.js @@ -31,20 +31,17 @@ var abs = require( '@stdlib/math/base/special/abs' ); * * @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 absolute 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 = minabs( N, x, 2, 1 ); +* var v = minabs( 4, x, 2, 1 ); * // returns 1.0 */ -function minabs( N, x, stride, offset ) { +function minabs( N, x, strideX, offsetX ) { var min; var ix; var v; @@ -53,13 +50,13 @@ function minabs( N, x, stride, offset ) { if ( N <= 0 ) { return NaN; } - if ( N === 1 || stride === 0 ) { - return abs( x[ offset ] ); + if ( N === 1 || strideX === 0 ) { + return abs( x[ offsetX ] ); } - ix = offset; + ix = offsetX; min = abs( x[ ix ] ); for ( i = 1; i < N; i++ ) { - ix += stride; + ix += strideX; v = abs( x[ ix ] ); if ( isnan( v ) ) { return v; diff --git a/lib/node_modules/@stdlib/stats/base/minabs/test/test.minabs.js b/lib/node_modules/@stdlib/stats/base/minabs/test/test.minabs.js index 09816ce5e4cc..803515bc4179 100644 --- a/lib/node_modules/@stdlib/stats/base/minabs/test/test.minabs.js +++ b/lib/node_modules/@stdlib/stats/base/minabs/test/test.minabs.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 isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); var Float64Array = require( '@stdlib/array/float64' ); -var minabs = require( './../lib/minabs.js' ); +var minabs = 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 = minabs( N, x, 2 ); + v = minabs( 4, x, 2 ); t.strictEqual( v, 1.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 = minabs( N, x, -2 ); + v = minabs( 4, x, -2 ); t.strictEqual( v, 1.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 = minabs( N, x1, 2 ); + v = minabs( 4, x1, 2 ); t.strictEqual( v, 1.0, 'returns expected value' ); t.end(); diff --git a/lib/node_modules/@stdlib/stats/base/minabs/test/test.ndarray.js b/lib/node_modules/@stdlib/stats/base/minabs/test/test.ndarray.js index 411f9d1fd596..765367be7bcf 100644 --- a/lib/node_modules/@stdlib/stats/base/minabs/test/test.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/minabs/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 isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); var minabs = 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 = minabs( N, x, 2, 0 ); + v = minabs( 4, x, 2, 0 ); t.strictEqual( v, 1.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 = minabs( N, x, -2, 6 ); + v = minabs( 4, x, -2, 6 ); t.strictEqual( v, 1.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 = minabs( N, x, 2, 1 ); + v = minabs( 4, x, 2, 1 ); t.strictEqual( v, 1.0, 'returns expected value' ); t.end(); From a94b5c060e01367dd4a3a5d776bcd88de91b174f Mon Sep 17 00:00:00 2001 From: aayush0325 Date: Fri, 7 Feb 2025 08:53:06 +0000 Subject: [PATCH 2/2] feat: add support 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: 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: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../@stdlib/stats/base/minabs/README.md | 3 + .../stats/base/minabs/docs/types/index.d.ts | 11 +- .../stats/base/minabs/docs/types/test.ts | 3 + .../stats/base/minabs/lib/accessors.js | 85 +++++++++++++ .../@stdlib/stats/base/minabs/lib/ndarray.js | 7 ++ .../test/{test.minabs.js => test.main.js} | 94 ++++++++++++++ .../stats/base/minabs/test/test.ndarray.js | 115 ++++++++++++++++++ 7 files changed, 315 insertions(+), 3 deletions(-) create mode 100644 lib/node_modules/@stdlib/stats/base/minabs/lib/accessors.js rename lib/node_modules/@stdlib/stats/base/minabs/test/{test.minabs.js => test.main.js} (64%) diff --git a/lib/node_modules/@stdlib/stats/base/minabs/README.md b/lib/node_modules/@stdlib/stats/base/minabs/README.md index 1abdeeecad9b..12934bdc06b3 100644 --- a/lib/node_modules/@stdlib/stats/base/minabs/README.md +++ b/lib/node_modules/@stdlib/stats/base/minabs/README.md @@ -109,6 +109,7 @@ var v = minabs.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 ([`dminabs`][@stdlib/stats/base/dminabs], [`sminabs`][@stdlib/stats/base/sminabs], etc.) are likely to be significantly more performant. @@ -164,6 +165,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/dminabs]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/dminabs diff --git a/lib/node_modules/@stdlib/stats/base/minabs/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/minabs/docs/types/index.d.ts index d952d125ab78..0a3466f36cda 100644 --- a/lib/node_modules/@stdlib/stats/base/minabs/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/base/minabs/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 `minabs`. @@ -40,7 +45,7 @@ interface Routine { * var v = minabs( x.length, x, 1 ); * // returns 1.0 */ - ( N: number, x: NumericArray, strideX: number ): number; + ( N: number, x: InputArray, strideX: number ): number; /** * Computes the minimum absolute value of a strided array using alternative indexing semantics. @@ -57,7 +62,7 @@ interface Routine { * var v = minabs.ndarray( x.length, x, 1, 0 ); * // returns 1.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/minabs/docs/types/test.ts b/lib/node_modules/@stdlib/stats/base/minabs/docs/types/test.ts index 36118eecfa3b..35678a9dc0c3 100644 --- a/lib/node_modules/@stdlib/stats/base/minabs/docs/types/test.ts +++ b/lib/node_modules/@stdlib/stats/base/minabs/docs/types/test.ts @@ -16,6 +16,7 @@ * limitations under the License. */ +import AccessorArray = require( '@stdlib/array/base/accessor' ); import minabs = require( './index' ); @@ -26,6 +27,7 @@ import minabs = require( './index' ); const x = new Float64Array( 10 ); minabs( x.length, x, 1 ); // $ExpectType number + minabs( 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 minabs = require( './index' ); const x = new Float64Array( 10 ); minabs.ndarray( x.length, x, 1, 0 ); // $ExpectType number + minabs.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... diff --git a/lib/node_modules/@stdlib/stats/base/minabs/lib/accessors.js b/lib/node_modules/@stdlib/stats/base/minabs/lib/accessors.js new file mode 100644 index 000000000000..23bea66ca6af --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/minabs/lib/accessors.js @@ -0,0 +1,85 @@ +/** +* @license Apache-2.0 +* +* 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. +* 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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var abs = require( '@stdlib/math/base/special/abs' ); + + +// MAIN // + +/** +* Computes the minimum absolute value of a strided array. +* +* @private +* @param {PositiveInteger} N - number of indexed elements +* @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 absolute value +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +* +* var x = toAccessorArray( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); +* +* var v = minabs( 4, arraylike2object( x ), 2, 1 ); +* // returns 1.0 +*/ +function minabs( N, x, strideX, offsetX ) { + var xbuf; + var get; + var min; + var ix; + var v; + var i; + + // 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 abs( get( xbuf, offsetX ) ); + } + ix = offsetX; + min = abs( get( xbuf, ix ) ); + for ( i = 1; i < N; i++ ) { + ix += strideX; + v = abs( get( xbuf, ix ) ); + if ( isnan( v ) ) { + return v; + } + if ( v < min ) { + min = v; + } + } + return min; +} + + +// EXPORTS // + +module.exports = minabs; diff --git a/lib/node_modules/@stdlib/stats/base/minabs/lib/ndarray.js b/lib/node_modules/@stdlib/stats/base/minabs/lib/ndarray.js index 1ceea0e3c43d..5ab4abbfa55d 100644 --- a/lib/node_modules/@stdlib/stats/base/minabs/lib/ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/minabs/lib/ndarray.js @@ -22,6 +22,8 @@ var isnan = require( '@stdlib/math/base/assert/is-nan' ); var abs = require( '@stdlib/math/base/special/abs' ); +var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +var accessors = require( './accessors.js' ); // MAIN // @@ -44,12 +46,17 @@ var abs = require( '@stdlib/math/base/special/abs' ); function minabs( 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 abs( x[ offsetX ] ); } diff --git a/lib/node_modules/@stdlib/stats/base/minabs/test/test.minabs.js b/lib/node_modules/@stdlib/stats/base/minabs/test/test.main.js similarity index 64% rename from lib/node_modules/@stdlib/stats/base/minabs/test/test.minabs.js rename to lib/node_modules/@stdlib/stats/base/minabs/test/test.main.js index 803515bc4179..b9097de9fe81 100644 --- a/lib/node_modules/@stdlib/stats/base/minabs/test/test.minabs.js +++ b/lib/node_modules/@stdlib/stats/base/minabs/test/test.main.js @@ -24,6 +24,7 @@ var tape = require( 'tape' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); var Float64Array = require( '@stdlib/array/float64' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); var minabs = require( './../lib/main.js' ); @@ -67,6 +68,33 @@ tape( 'the function calculates the minimum absolute value of a strided array', f t.end(); }); +tape( 'the function calculates the minimum absolute 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 = minabs( x.length, toAccessorArray( x ), 1 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ -4.0, -5.0 ]; + v = minabs( x.length, toAccessorArray( x ), 1 ); + t.strictEqual( v, 4.0, 'returns expected value' ); + + x = [ -0.0, 0.0, -0.0 ]; + v = minabs( x.length, toAccessorArray( x ), 1 ); + t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); + + x = [ NaN ]; + v = minabs( x.length, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ NaN, NaN ]; + v = minabs( 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; @@ -94,6 +122,18 @@ tape( 'if provided an `N` parameter equal to `1`, the function returns the first t.end(); }); +tape( 'if provided an `N` parameter equal to `1`, 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 = minabs( 1, toAccessorArray( x ), 1 ); + t.strictEqual( v, 1.0, 'returns expected value' ); + + t.end(); +}); + tape( 'the function supports a `stride` parameter', function test( t ) { var x; var v; @@ -115,6 +155,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 = minabs( 4, toAccessorArray( x ), 2 ); + + t.strictEqual( v, 1.0, 'returns expected value' ); + t.end(); +}); + tape( 'the function supports a negative `stride` parameter', function test( t ) { var x; var v; @@ -136,6 +197,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 = minabs( 4, toAccessorArray( x ), -2 ); + + t.strictEqual( v, 1.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; @@ -148,6 +230,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 element (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = minabs( 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; diff --git a/lib/node_modules/@stdlib/stats/base/minabs/test/test.ndarray.js b/lib/node_modules/@stdlib/stats/base/minabs/test/test.ndarray.js index 765367be7bcf..f775f1f62296 100644 --- a/lib/node_modules/@stdlib/stats/base/minabs/test/test.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/minabs/test/test.ndarray.js @@ -23,6 +23,7 @@ var tape = require( 'tape' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); var minabs = require( './../lib/ndarray.js' ); @@ -66,6 +67,33 @@ tape( 'the function calculates the minimum absolute value of a strided array', f t.end(); }); +tape( 'the function calculates the minimum absolute 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 = minabs( x.length, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ -4.0, -5.0 ]; + v = minabs( x.length, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 4.0, 'returns expected value' ); + + x = [ -0.0, 0.0, -0.0 ]; + v = minabs( x.length, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); + + x = [ NaN ]; + v = minabs( x.length, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ NaN, NaN ]; + v = minabs( 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; @@ -93,6 +121,18 @@ tape( 'if provided an `N` parameter equal to `1`, the function returns the first t.end(); }); +tape( 'if provided an `N` parameter equal to `1`, 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 = minabs( 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 1.0, 'returns expected value' ); + + t.end(); +}); + tape( 'the function supports a `stride` parameter', function test( t ) { var x; var v; @@ -114,6 +154,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 = minabs( 4, toAccessorArray( x ), 2, 0 ); + + t.strictEqual( v, 1.0, 'returns expected value' ); + t.end(); +}); + tape( 'the function supports a negative `stride` parameter', function test( t ) { var x; var v; @@ -135,6 +196,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 = minabs( 4, toAccessorArray( x ), -2, 6 ); + + t.strictEqual( v, 1.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; @@ -147,6 +229,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 = minabs( 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; @@ -167,3 +261,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 = minabs( 4, toAccessorArray( x ), 2, 1 ); + t.strictEqual( v, 1.0, 'returns expected value' ); + + t.end(); +});