From b615682342d667f1553f927194f82fbec612df30 Mon Sep 17 00:00:00 2001 From: gururaj1512 Date: Thu, 14 Aug 2025 23:54:46 +0530 Subject: [PATCH 1/4] feat: add `stats/base/ndarray/minabs` --- 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: passed - 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: passed - task: lint_license_headers status: passed --- --- .../stats/base/ndarray/minabs/README.md | 111 +++++++++++ .../ndarray/minabs/benchmark/benchmark.js | 102 +++++++++++ .../stats/base/ndarray/minabs/docs/repl.txt | 31 ++++ .../base/ndarray/minabs/docs/types/index.d.ts | 45 +++++ .../base/ndarray/minabs/docs/types/test.ts | 57 ++++++ .../base/ndarray/minabs/examples/index.js | 33 ++++ .../stats/base/ndarray/minabs/lib/index.js | 44 +++++ .../stats/base/ndarray/minabs/lib/main.js | 55 ++++++ .../stats/base/ndarray/minabs/package.json | 69 +++++++ .../stats/base/ndarray/minabs/test/test.js | 172 ++++++++++++++++++ 10 files changed, 719 insertions(+) create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/minabs/README.md create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/minabs/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/minabs/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/minabs/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/minabs/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/minabs/examples/index.js create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/minabs/lib/index.js create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/minabs/lib/main.js create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/minabs/package.json create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/minabs/test/test.js diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/minabs/README.md b/lib/node_modules/@stdlib/stats/base/ndarray/minabs/README.md new file mode 100644 index 000000000000..49c27d33b254 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/minabs/README.md @@ -0,0 +1,111 @@ + + +# minabs + +> Compute the minimum absolute value of a one-dimensional ndarray. + +
+ +
+ + + +
+ +## Usage + +```javascript +var minabs = require( '@stdlib/stats/base/ndarray/minabs' ); +``` + +#### minabs( arrays ) + +Computes the minimum absolute value of a one-dimensional ndarray. + +```javascript +var ndarray = require( '@stdlib/ndarray/base/ctor' ); + +var xbuf = [ -1.0, 3.0, -4.0, 2.0 ]; +var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + +var v = minabs( [ x ] ); +// returns 1.0 +``` + +The function has the following parameters: + +- **arrays**: array-like object containing a one-dimensional input ndarray. + +
+ + + +
+ +## Notes + +- If provided an empty one-dimensional ndarray, the function returns `NaN`. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var minabs = require( '@stdlib/stats/base/ndarray/minabs' ); + +var xbuf = discreteUniform( 10, -50, 50, { + 'dtype': 'generic' +}); +var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); +console.log( ndarray2array( x ) ); + +var v = minabs( [ x ] ); +console.log( v ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/minabs/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/ndarray/minabs/benchmark/benchmark.js new file mode 100644 index 000000000000..5076d7ba30ce --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/minabs/benchmark/benchmark.js @@ -0,0 +1,102 @@ +/** +* @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 bench = require( '@stdlib/bench' ); +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 ndarray = require( '@stdlib/ndarray/base/ctor' ); +var pkg = require( './../package.json' ).name; +var minabs = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var xbuf; + var x; + + xbuf = uniform( len, -10.0, 10.0, options ); + x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' ); + + return benchmark; + + function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = minabs( [ x ] ); + if ( isnan( v ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( v ) ) { + b.fail( 'should not return NaN' ); + } + 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( pkg+':len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/minabs/docs/repl.txt b/lib/node_modules/@stdlib/stats/base/ndarray/minabs/docs/repl.txt new file mode 100644 index 000000000000..5c8551bd2682 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/minabs/docs/repl.txt @@ -0,0 +1,31 @@ + +{{alias}}( arrays ) + Computes the minimum absolute value of a one-dimensional ndarray. + + If provided an empty ndarray, the function returns `NaN`. + + Parameters + ---------- + arrays: ArrayLikeObject + Array-like object containing a one-dimensional input ndarray. + + Returns + ------- + out: number + Minimum absolute value. + + Examples + -------- + > var xbuf = [ 1.0, -2.0, 2.0 ]; + > var dt = 'generic'; + > var sh = [ xbuf.length ]; + > var sx = [ 1 ]; + > var ox = 0; + > var ord = 'row-major'; + > var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, sx, ox, ord ); + > {{alias}}( [ x ] ) + 1.0 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/minabs/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/ndarray/minabs/docs/types/index.d.ts new file mode 100644 index 000000000000..f7fb60549e77 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/minabs/docs/types/index.d.ts @@ -0,0 +1,45 @@ +/* +* @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. +*/ + +// TypeScript Version: 4.1 + +/// + +import { ndarray } from '@stdlib/types/ndarray'; + +/** +* Computes the minimum absolute value of a one-dimensional ndarray. +* +* @param arrays - array-like object containing an input ndarray +* @returns minimum absolute value +* +* @example +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* +* var xbuf = [ -1.0, 3.0, -4.0, 2.0 ]; +* var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var v = minabs( [ x ] ); +* // returns 1.0 +*/ +declare function minabs( arrays: [ T ] ): number; + + +// EXPORTS // + +export = minabs; diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/minabs/docs/types/test.ts b/lib/node_modules/@stdlib/stats/base/ndarray/minabs/docs/types/test.ts new file mode 100644 index 000000000000..21964be6636d --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/minabs/docs/types/test.ts @@ -0,0 +1,57 @@ +/* +* @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. +*/ + +/* eslint-disable space-in-parens */ + +import zeros = require( '@stdlib/ndarray/zeros' ); +import minabs = require( './index' ); + + +// TESTS // + +// The function returns a number... +{ + const x = zeros( [ 10 ], { + 'dtype': 'generic' + }); + + minabs( [ x ] ); // $ExpectType number +} + +// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays... +{ + minabs( '10' ); // $ExpectError + minabs( 10 ); // $ExpectError + minabs( true ); // $ExpectError + minabs( false ); // $ExpectError + minabs( null ); // $ExpectError + minabs( undefined ); // $ExpectError + minabs( [] ); // $ExpectError + minabs( {} ); // $ExpectError + minabs( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 10 ], { + 'dtype': 'generic' + }); + + minabs(); // $ExpectError + minabs( [ x ], {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/minabs/examples/index.js b/lib/node_modules/@stdlib/stats/base/ndarray/minabs/examples/index.js new file mode 100644 index 000000000000..b39e9ebe864b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/minabs/examples/index.js @@ -0,0 +1,33 @@ +/** +* @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'; + +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var minabs = require( './../lib' ); + +var xbuf = discreteUniform( 10, -50, 50, { + 'dtype': 'generic' +}); +var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); +console.log( ndarray2array( x ) ); + +var v = minabs( [ x ] ); +console.log( v ); diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/minabs/lib/index.js b/lib/node_modules/@stdlib/stats/base/ndarray/minabs/lib/index.js new file mode 100644 index 000000000000..0fa9243ad026 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/minabs/lib/index.js @@ -0,0 +1,44 @@ +/** +* @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'; + +/** +* Compute the minimum absolute value of a one-dimensional ndarray. +* +* @module @stdlib/stats/base/ndarray/minabs +* +* @example +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* var minabs = require( '@stdlib/stats/base/ndarray/minabs' ); +* +* var xbuf = [ -1.0, 3.0, -4.0, 2.0 ]; +* var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var v = minabs( [ x ] ); +* // returns 1.0 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/minabs/lib/main.js b/lib/node_modules/@stdlib/stats/base/ndarray/minabs/lib/main.js new file mode 100644 index 000000000000..e069e5784029 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/minabs/lib/main.js @@ -0,0 +1,55 @@ +/** +* @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 numelDimension = require( '@stdlib/ndarray/base/numel-dimension' ); +var getStride = require( '@stdlib/ndarray/base/stride' ); +var getOffset = require( '@stdlib/ndarray/base/offset' ); +var getData = require( '@stdlib/ndarray/base/data-buffer' ); +var strided = require( '@stdlib/stats/strided/minabs' ).ndarray; + + +// MAIN // + +/** +* Computes the minimum absolute value of a one-dimensional ndarray. +* +* @param {ArrayLikeObject} arrays - array-like object containing an input ndarray +* @returns {number} minimum absolute value +* +* @example +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* +* var xbuf = [ -1.0, 3.0, -4.0, 2.0 ]; +* var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var v = minabs( [ x ] ); +* // returns 1.0 +*/ +function minabs( arrays ) { + var x = arrays[ 0 ]; + return strided( numelDimension( x, 0 ), getData( x ), getStride( x, 0 ), getOffset( x ) ); // eslint-disable-line max-len +} + + +// EXPORTS // + +module.exports = minabs; diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/minabs/package.json b/lib/node_modules/@stdlib/stats/base/ndarray/minabs/package.json new file mode 100644 index 000000000000..df3121aca601 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/minabs/package.json @@ -0,0 +1,69 @@ +{ + "name": "@stdlib/stats/base/ndarray/minabs", + "version": "0.0.0", + "description": "Compute the minimum absolute value of a one-dimensional ndarray.", + "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", + "minimum", + "min", + "absolute", + "abs", + "range", + "extremes", + "domain", + "extent", + "ndarray" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/minabs/test/test.js b/lib/node_modules/@stdlib/stats/base/ndarray/minabs/test/test.js new file mode 100644 index 000000000000..9592cb39dedb --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/minabs/test/test.js @@ -0,0 +1,172 @@ +/** +* @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 tape = require( 'tape' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var minabs = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Returns a one-dimensional ndarray. +* +* @private +* @param {Collection} buffer - underlying data buffer +* @param {NonNegativeInteger} length - number of indexed elements +* @param {integer} stride - stride length +* @param {NonNegativeInteger} offset - index offset +* @returns {ndarray} one-dimensional ndarray +*/ +function vector( buffer, length, stride, offset ) { + return new ndarray( 'generic', buffer, [ length ], [ stride ], offset, 'row-major' ); +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof minabs, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 1', function test( t ) { + t.strictEqual( minabs.length, 1, 'has expected arity' ); + t.end(); +}); + +tape( 'the function calculates the minimum absolute value of a one-dimensional ndarray', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + v = minabs( [ vector( x, 6, 1, 0 ) ] ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ -4.0, -5.0 ]; + v = minabs( [ vector( x, 2, 1, 0 ) ] ); + t.strictEqual( v, 4.0, 'returns expected value' ); + + x = [ -0.0, 0.0, -0.0 ]; + v = minabs( [ vector( x, 3, 1, 0 ) ] ); + t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); + + x = [ NaN ]; + v = minabs( [ vector( x, 1, 1, 0 ) ] ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ NaN, NaN ]; + v = minabs( [ vector( x, 2, 1, 0 ) ] ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an empty vector, the function returns `NaN`', function test( t ) { + var x; + var v; + + x = []; + + v = minabs( [ vector( x, 0, 1, 0 ) ] ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided a vector containing a single element, the function returns that element', function test( t ) { + var x; + var v; + + x = [ 1.0 ]; + + v = minabs( [ vector( x, 1, 1, 0 ) ] ); + t.strictEqual( v, 1.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports one-dimensional ndarrays having non-unit strides', 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( [ vector( x, 4, 2, 0 ) ] ); + + t.strictEqual( v, 1.0, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports one-dimensional ndarrays having negative strides', 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( [ vector( x, 4, -2, 6 ) ] ); + + t.strictEqual( v, 1.0, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports one-dimensional ndarrays having non-zero offsets', 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( [ vector( x, 4, 2, 1 ) ] ); + t.strictEqual( v, 1.0, 'returns expected value' ); + + t.end(); +}); From 4046966fb3cd2be3d6ca0281c373ef5eedf15580 Mon Sep 17 00:00:00 2001 From: gururaj1512 Date: Thu, 14 Aug 2025 23:56:51 +0530 Subject: [PATCH 2/4] feat: add `stats/base/ndarray/dminabs` --- 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: passed - 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: passed - task: lint_license_headers status: passed --- --- .../stats/base/ndarray/dminabs/README.md | 112 ++++++++++++ .../ndarray/dminabs/benchmark/benchmark.js | 102 +++++++++++ .../stats/base/ndarray/dminabs/docs/repl.txt | 32 ++++ .../ndarray/dminabs/docs/types/index.d.ts | 46 +++++ .../base/ndarray/dminabs/docs/types/test.ts | 57 ++++++ .../base/ndarray/dminabs/examples/index.js | 33 ++++ .../stats/base/ndarray/dminabs/lib/index.js | 45 +++++ .../stats/base/ndarray/dminabs/lib/main.js | 56 ++++++ .../stats/base/ndarray/dminabs/package.json | 69 +++++++ .../stats/base/ndarray/dminabs/test/test.js | 173 ++++++++++++++++++ 10 files changed, 725 insertions(+) create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/dminabs/README.md create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/dminabs/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/dminabs/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/dminabs/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/dminabs/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/dminabs/examples/index.js create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/dminabs/lib/index.js create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/dminabs/lib/main.js create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/dminabs/package.json create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/dminabs/test/test.js diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dminabs/README.md b/lib/node_modules/@stdlib/stats/base/ndarray/dminabs/README.md new file mode 100644 index 000000000000..96f7da6d7b80 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dminabs/README.md @@ -0,0 +1,112 @@ + + +# dminabs + +> Compute the minimum absolute value of a one-dimensional double-precision floating-point ndarray. + +
+ +
+ + + +
+ +## Usage + +```javascript +var dminabs = require( '@stdlib/stats/base/ndarray/dminabs' ); +``` + +#### dminabs( arrays ) + +Computes the minimum absolute value of a one-dimensional double-precision floating-point ndarray. + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); + +var xbuf = new Float64Array( [ -1.0, 3.0, -4.0, 2.0 ] ); +var x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + +var v = dminabs( [ x ] ); +// returns 1.0 +``` + +The function has the following parameters: + +- **arrays**: array-like object containing a one-dimensional input ndarray. + +
+ + + +
+ +## Notes + +- If provided an empty one-dimensional ndarray, the function returns `NaN`. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var dminabs = require( '@stdlib/stats/base/ndarray/dminabs' ); + +var xbuf = discreteUniform( 10, -50, 50, { + 'dtype': 'float64' +}); +var x = new ndarray( 'float64', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); +console.log( ndarray2array( x ) ); + +var v = dminabs( [ x ] ); +console.log( v ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dminabs/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/ndarray/dminabs/benchmark/benchmark.js new file mode 100644 index 000000000000..78eeb79d4c8b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dminabs/benchmark/benchmark.js @@ -0,0 +1,102 @@ +/** +* @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 bench = require( '@stdlib/bench' ); +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 ndarray = require( '@stdlib/ndarray/base/ctor' ); +var pkg = require( './../package.json' ).name; +var dminabs = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var xbuf; + var x; + + xbuf = uniform( len, -10.0, 10.0, options ); + x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' ); + + return benchmark; + + function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = dminabs( [ x ] ); + if ( isnan( v ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( v ) ) { + b.fail( 'should not return NaN' ); + } + 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( pkg+':len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dminabs/docs/repl.txt b/lib/node_modules/@stdlib/stats/base/ndarray/dminabs/docs/repl.txt new file mode 100644 index 000000000000..c364711e5005 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dminabs/docs/repl.txt @@ -0,0 +1,32 @@ + +{{alias}}( arrays ) + Computes the minimum absolute value of a one-dimensional double-precision + floating-point ndarray. + + If provided an empty ndarray, the function returns `NaN`. + + Parameters + ---------- + arrays: ArrayLikeObject + Array-like object containing a one-dimensional input ndarray. + + Returns + ------- + out: number + Minimum absolute value. + + Examples + -------- + > var xbuf = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 2.0 ] ); + > var dt = 'float64'; + > var sh = [ xbuf.length ]; + > var sx = [ 1 ]; + > var ox = 0; + > var ord = 'row-major'; + > var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, sx, ox, ord ); + > {{alias}}( [ x ] ) + 1.0 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dminabs/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/ndarray/dminabs/docs/types/index.d.ts new file mode 100644 index 000000000000..5240aad01ac7 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dminabs/docs/types/index.d.ts @@ -0,0 +1,46 @@ +/* +* @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. +*/ + +// TypeScript Version: 4.1 + +/// + +import { float64ndarray } from '@stdlib/types/ndarray'; + +/** +* Computes the minimum absolute value of a one-dimensional double-precision floating-point ndarray. +* +* @param arrays - array-like object containing an input ndarray +* @returns minimum absolute value +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* +* var xbuf = new Float64Array( [ -1.0, 3.0, -4.0, 2.0 ] ); +* var x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var v = dminabs( [ x ] ); +* // returns 1.0 +*/ +declare function dminabs( arrays: [ float64ndarray ] ): number; + + +// EXPORTS // + +export = dminabs; diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dminabs/docs/types/test.ts b/lib/node_modules/@stdlib/stats/base/ndarray/dminabs/docs/types/test.ts new file mode 100644 index 000000000000..0a9b9b2d70ca --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dminabs/docs/types/test.ts @@ -0,0 +1,57 @@ +/* +* @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. +*/ + +/* eslint-disable space-in-parens */ + +import zeros = require( '@stdlib/ndarray/zeros' ); +import dminabs = require( './index' ); + + +// TESTS // + +// The function returns a number... +{ + const x = zeros( [ 10 ], { + 'dtype': 'float64' + }); + + dminabs( [ x ] ); // $ExpectType number +} + +// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays... +{ + dminabs( '10' ); // $ExpectError + dminabs( 10 ); // $ExpectError + dminabs( true ); // $ExpectError + dminabs( false ); // $ExpectError + dminabs( null ); // $ExpectError + dminabs( undefined ); // $ExpectError + dminabs( [] ); // $ExpectError + dminabs( {} ); // $ExpectError + dminabs( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 10 ], { + 'dtype': 'float64' + }); + + dminabs(); // $ExpectError + dminabs( [ x ], {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dminabs/examples/index.js b/lib/node_modules/@stdlib/stats/base/ndarray/dminabs/examples/index.js new file mode 100644 index 000000000000..c498994ab094 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dminabs/examples/index.js @@ -0,0 +1,33 @@ +/** +* @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'; + +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var dminabs = require( './../lib' ); + +var xbuf = discreteUniform( 10, -50, 50, { + 'dtype': 'float64' +}); +var x = new ndarray( 'float64', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); +console.log( ndarray2array( x ) ); + +var v = dminabs( [ x ] ); +console.log( v ); diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dminabs/lib/index.js b/lib/node_modules/@stdlib/stats/base/ndarray/dminabs/lib/index.js new file mode 100644 index 000000000000..3e932a90002f --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dminabs/lib/index.js @@ -0,0 +1,45 @@ +/** +* @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'; + +/** +* Compute the minimum absolute value of a one-dimensional double-precision floating-point ndarray. +* +* @module @stdlib/stats/base/ndarray/dminabs +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* var dminabs = require( '@stdlib/stats/base/ndarray/dminabs' ); +* +* var xbuf = new Float64Array( [ -1.0, 3.0, -4.0, 2.0 ] ); +* var x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var v = dminabs( [ x ] ); +* // returns 1.0 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dminabs/lib/main.js b/lib/node_modules/@stdlib/stats/base/ndarray/dminabs/lib/main.js new file mode 100644 index 000000000000..6d7f6f21937c --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dminabs/lib/main.js @@ -0,0 +1,56 @@ +/** +* @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 numelDimension = require( '@stdlib/ndarray/base/numel-dimension' ); +var getStride = require( '@stdlib/ndarray/base/stride' ); +var getOffset = require( '@stdlib/ndarray/base/offset' ); +var getData = require( '@stdlib/ndarray/base/data-buffer' ); +var strided = require( '@stdlib/stats/strided/dminabs' ).ndarray; + + +// MAIN // + +/** +* Computes the minimum absolute value of a one-dimensional double-precision floating-point ndarray. +* +* @param {ArrayLikeObject} arrays - array-like object containing an input ndarray +* @returns {number} minimum absolute value +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* +* var xbuf = new Float64Array( [ -1.0, 3.0, -4.0, 2.0 ] ); +* var x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var v = dminabs( [ x ] ); +* // returns 1.0 +*/ +function dminabs( arrays ) { + var x = arrays[ 0 ]; + return strided( numelDimension( x, 0 ), getData( x ), getStride( x, 0 ), getOffset( x ) ); // eslint-disable-line max-len +} + + +// EXPORTS // + +module.exports = dminabs; diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dminabs/package.json b/lib/node_modules/@stdlib/stats/base/ndarray/dminabs/package.json new file mode 100644 index 000000000000..c802fa073e44 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dminabs/package.json @@ -0,0 +1,69 @@ +{ + "name": "@stdlib/stats/base/ndarray/dminabs", + "version": "0.0.0", + "description": "Compute the minimum absolute value of a one-dimensional double-precision floating-point ndarray.", + "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", + "minimum", + "min", + "absolute", + "abs", + "range", + "extremes", + "domain", + "extent", + "ndarray" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dminabs/test/test.js b/lib/node_modules/@stdlib/stats/base/ndarray/dminabs/test/test.js new file mode 100644 index 000000000000..1804ae456f5c --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dminabs/test/test.js @@ -0,0 +1,173 @@ +/** +* @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 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 ndarray = require( '@stdlib/ndarray/base/ctor' ); +var dminabs = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Returns a one-dimensional ndarray. +* +* @private +* @param {Collection} buffer - underlying data buffer +* @param {NonNegativeInteger} length - number of indexed elements +* @param {integer} stride - stride length +* @param {NonNegativeInteger} offset - index offset +* @returns {ndarray} one-dimensional ndarray +*/ +function vector( buffer, length, stride, offset ) { + return new ndarray( 'float64', buffer, [ length ], [ stride ], offset, 'row-major' ); +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof dminabs, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 1', function test( t ) { + t.strictEqual( dminabs.length, 1, 'has expected arity' ); + t.end(); +}); + +tape( 'the function calculates the minimum absolute value of a one-dimensional ndarray', function test( t ) { + var x; + var v; + + x = new Float64Array( [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ] ); + v = dminabs( [ vector( x, 6, 1, 0 ) ] ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = new Float64Array( [ -4.0, -5.0 ] ); + v = dminabs( [ vector( x, 2, 1, 0 ) ] ); + t.strictEqual( v, 4.0, 'returns expected value' ); + + x = new Float64Array( [ -0.0, 0.0, -0.0 ] ); + v = dminabs( [ vector( x, 3, 1, 0 ) ] ); + t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); + + x = new Float64Array( [ NaN ] ); + v = dminabs( [ vector( x, 1, 1, 0 ) ] ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = new Float64Array( [ NaN, NaN ] ); + v = dminabs( [ vector( x, 2, 1, 0 ) ] ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an empty vector, the function returns `NaN`', function test( t ) { + var x; + var v; + + x = new Float64Array( [] ); + + v = dminabs( [ vector( x, 0, 1, 0 ) ] ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided a vector containing a single element, the function returns that element', function test( t ) { + var x; + var v; + + x = new Float64Array( [ 1.0 ] ); + + v = dminabs( [ vector( x, 1, 1, 0 ) ] ); + t.strictEqual( v, 1.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports one-dimensional ndarrays having non-unit strides', function test( t ) { + var x; + var v; + + x = new Float64Array([ + 1.0, // 0 + 2.0, + 2.0, // 1 + -7.0, + -2.0, // 2 + 3.0, + 4.0, // 3 + 2.0 + ]); + + v = dminabs( [ vector( x, 4, 2, 0 ) ] ); + + t.strictEqual( v, 1.0, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports one-dimensional ndarrays having negative strides', function test( t ) { + var x; + var v; + + x = new Float64Array([ + 1.0, // 3 + 2.0, + 2.0, // 2 + -7.0, + -2.0, // 1 + 3.0, + 4.0, // 0 + 2.0 + ]); + + v = dminabs( [ vector( x, 4, -2, 6 ) ] ); + + t.strictEqual( v, 1.0, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports one-dimensional ndarrays having non-zero offsets', function test( t ) { + var x; + var v; + + x = new Float64Array([ + 2.0, + 1.0, // 0 + 2.0, + -2.0, // 1 + -2.0, + 2.0, // 2 + 3.0, + 4.0 // 3 + ]); + + v = dminabs( [ vector( x, 4, 2, 1 ) ] ); + t.strictEqual( v, 1.0, 'returns expected value' ); + + t.end(); +}); From 527cd66a6ded0dd3fbe510ebb401019a6a8807ad Mon Sep 17 00:00:00 2001 From: gururaj1512 Date: Thu, 14 Aug 2025 23:58:26 +0530 Subject: [PATCH 3/4] feat: add `stats/base/ndarray/sminabs` --- 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: passed - 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: passed - task: lint_license_headers status: passed --- --- .../stats/base/ndarray/sminabs/README.md | 112 ++++++++++++ .../ndarray/sminabs/benchmark/benchmark.js | 102 +++++++++++ .../stats/base/ndarray/sminabs/docs/repl.txt | 32 ++++ .../ndarray/sminabs/docs/types/index.d.ts | 46 +++++ .../base/ndarray/sminabs/docs/types/test.ts | 57 ++++++ .../base/ndarray/sminabs/examples/index.js | 33 ++++ .../stats/base/ndarray/sminabs/lib/index.js | 45 +++++ .../stats/base/ndarray/sminabs/lib/main.js | 56 ++++++ .../stats/base/ndarray/sminabs/package.json | 69 +++++++ .../stats/base/ndarray/sminabs/test/test.js | 173 ++++++++++++++++++ 10 files changed, 725 insertions(+) create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/sminabs/README.md create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/sminabs/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/sminabs/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/sminabs/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/sminabs/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/sminabs/examples/index.js create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/sminabs/lib/index.js create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/sminabs/lib/main.js create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/sminabs/package.json create mode 100644 lib/node_modules/@stdlib/stats/base/ndarray/sminabs/test/test.js diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/sminabs/README.md b/lib/node_modules/@stdlib/stats/base/ndarray/sminabs/README.md new file mode 100644 index 000000000000..1bc1a625aabb --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/sminabs/README.md @@ -0,0 +1,112 @@ + + +# sminabs + +> Compute the minimum absolute value of a one-dimensional single-precision floating-point ndarray. + +
+ +
+ + + +
+ +## Usage + +```javascript +var sminabs = require( '@stdlib/stats/base/ndarray/sminabs' ); +``` + +#### sminabs( arrays ) + +Computes the minimum absolute value of a one-dimensional single-precision floating-point ndarray. + +```javascript +var Float32Array = require( '@stdlib/array/float32' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); + +var xbuf = new Float32Array( [ -1.0, 3.0, -4.0, 2.0 ] ); +var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + +var v = sminabs( [ x ] ); +// returns 1.0 +``` + +The function has the following parameters: + +- **arrays**: array-like object containing a one-dimensional input ndarray. + +
+ + + +
+ +## Notes + +- If provided an empty one-dimensional ndarray, the function returns `NaN`. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var sminabs = require( '@stdlib/stats/base/ndarray/sminabs' ); + +var xbuf = discreteUniform( 10, -50, 50, { + 'dtype': 'float32' +}); +var x = new ndarray( 'float32', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); +console.log( ndarray2array( x ) ); + +var v = sminabs( [ x ] ); +console.log( v ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/sminabs/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/ndarray/sminabs/benchmark/benchmark.js new file mode 100644 index 000000000000..b0b4e4c556cb --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/sminabs/benchmark/benchmark.js @@ -0,0 +1,102 @@ +/** +* @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 bench = require( '@stdlib/bench' ); +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 ndarray = require( '@stdlib/ndarray/base/ctor' ); +var pkg = require( './../package.json' ).name; +var sminabs = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float32' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var xbuf; + var x; + + xbuf = uniform( len, -10.0, 10.0, options ); + x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' ); + + return benchmark; + + function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = sminabs( [ x ] ); + if ( isnan( v ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( v ) ) { + b.fail( 'should not return NaN' ); + } + 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( pkg+':len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/sminabs/docs/repl.txt b/lib/node_modules/@stdlib/stats/base/ndarray/sminabs/docs/repl.txt new file mode 100644 index 000000000000..0bf14d78ab2a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/sminabs/docs/repl.txt @@ -0,0 +1,32 @@ + +{{alias}}( arrays ) + Computes the minimum absolute value of a one-dimensional single-precision + floating-point ndarray. + + If provided an empty ndarray, the function returns `NaN`. + + Parameters + ---------- + arrays: ArrayLikeObject + Array-like object containing a one-dimensional input ndarray. + + Returns + ------- + out: number + Minimum absolute value. + + Examples + -------- + > var xbuf = new {{alias:@stdlib/array/float32}}( [ 1.0, -2.0, 2.0 ] ); + > var dt = 'float32'; + > var sh = [ xbuf.length ]; + > var sx = [ 1 ]; + > var ox = 0; + > var ord = 'row-major'; + > var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, sx, ox, ord ); + > {{alias}}( [ x ] ) + 1.0 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/sminabs/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/ndarray/sminabs/docs/types/index.d.ts new file mode 100644 index 000000000000..587fc1d146df --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/sminabs/docs/types/index.d.ts @@ -0,0 +1,46 @@ +/* +* @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. +*/ + +// TypeScript Version: 4.1 + +/// + +import { float32ndarray } from '@stdlib/types/ndarray'; + +/** +* Computes the minimum absolute value of a one-dimensional single-precision floating-point ndarray. +* +* @param arrays - array-like object containing an input ndarray +* @returns minimum absolute value +* +* @example +* var Float32Array = require( '@stdlib/array/float32' ); +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* +* var xbuf = new Float32Array( [ -1.0, 3.0, -4.0, 2.0 ] ); +* var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var v = sminabs( [ x ] ); +* // returns 1.0 +*/ +declare function sminabs( arrays: [ float32ndarray ] ): number; + + +// EXPORTS // + +export = sminabs; diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/sminabs/docs/types/test.ts b/lib/node_modules/@stdlib/stats/base/ndarray/sminabs/docs/types/test.ts new file mode 100644 index 000000000000..7189fc72fb07 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/sminabs/docs/types/test.ts @@ -0,0 +1,57 @@ +/* +* @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. +*/ + +/* eslint-disable space-in-parens */ + +import zeros = require( '@stdlib/ndarray/zeros' ); +import sminabs = require( './index' ); + + +// TESTS // + +// The function returns a number... +{ + const x = zeros( [ 10 ], { + 'dtype': 'float32' + }); + + sminabs( [ x ] ); // $ExpectType number +} + +// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays... +{ + sminabs( '10' ); // $ExpectError + sminabs( 10 ); // $ExpectError + sminabs( true ); // $ExpectError + sminabs( false ); // $ExpectError + sminabs( null ); // $ExpectError + sminabs( undefined ); // $ExpectError + sminabs( [] ); // $ExpectError + sminabs( {} ); // $ExpectError + sminabs( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 10 ], { + 'dtype': 'float32' + }); + + sminabs(); // $ExpectError + sminabs( [ x ], {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/sminabs/examples/index.js b/lib/node_modules/@stdlib/stats/base/ndarray/sminabs/examples/index.js new file mode 100644 index 000000000000..05d8c3e788ef --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/sminabs/examples/index.js @@ -0,0 +1,33 @@ +/** +* @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'; + +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var sminabs = require( './../lib' ); + +var xbuf = discreteUniform( 10, -50, 50, { + 'dtype': 'float32' +}); +var x = new ndarray( 'float32', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); +console.log( ndarray2array( x ) ); + +var v = sminabs( [ x ] ); +console.log( v ); diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/sminabs/lib/index.js b/lib/node_modules/@stdlib/stats/base/ndarray/sminabs/lib/index.js new file mode 100644 index 000000000000..198d9426948a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/sminabs/lib/index.js @@ -0,0 +1,45 @@ +/** +* @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'; + +/** +* Compute the minimum absolute value of a one-dimensional single-precision floating-point ndarray. +* +* @module @stdlib/stats/base/ndarray/sminabs +* +* @example +* var Float32Array = require( '@stdlib/array/float32' ); +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* var sminabs = require( '@stdlib/stats/base/ndarray/sminabs' ); +* +* var xbuf = new Float32Array( [ -1.0, 3.0, -4.0, 2.0 ] ); +* var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var v = sminabs( [ x ] ); +* // returns 1.0 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/sminabs/lib/main.js b/lib/node_modules/@stdlib/stats/base/ndarray/sminabs/lib/main.js new file mode 100644 index 000000000000..ef5ded3dc085 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/sminabs/lib/main.js @@ -0,0 +1,56 @@ +/** +* @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 numelDimension = require( '@stdlib/ndarray/base/numel-dimension' ); +var getStride = require( '@stdlib/ndarray/base/stride' ); +var getOffset = require( '@stdlib/ndarray/base/offset' ); +var getData = require( '@stdlib/ndarray/base/data-buffer' ); +var strided = require( '@stdlib/stats/strided/sminabs' ).ndarray; + + +// MAIN // + +/** +* Computes the minimum absolute value of a one-dimensional single-precision floating-point ndarray. +* +* @param {ArrayLikeObject} arrays - array-like object containing an input ndarray +* @returns {number} minimum value +* +* @example +* var Float32Array = require( '@stdlib/array/float32' ); +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* +* var xbuf = new Float32Array( [ -1.0, 3.0, -4.0, 2.0 ] ); +* var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var v = sminabs( [ x ] ); +* // returns 1.0 +*/ +function sminabs( arrays ) { + var x = arrays[ 0 ]; + return strided( numelDimension( x, 0 ), getData( x ), getStride( x, 0 ), getOffset( x ) ); // eslint-disable-line max-len +} + + +// EXPORTS // + +module.exports = sminabs; diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/sminabs/package.json b/lib/node_modules/@stdlib/stats/base/ndarray/sminabs/package.json new file mode 100644 index 000000000000..bc770d82f654 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/sminabs/package.json @@ -0,0 +1,69 @@ +{ + "name": "@stdlib/stats/base/ndarray/sminabs", + "version": "0.0.0", + "description": "Compute the minimum absolute value of a one-dimensional single-precision floating-point ndarray.", + "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", + "minimum", + "min", + "absolute", + "abs", + "range", + "extremes", + "domain", + "extent", + "ndarray" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/sminabs/test/test.js b/lib/node_modules/@stdlib/stats/base/ndarray/sminabs/test/test.js new file mode 100644 index 000000000000..e850a461d92c --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/sminabs/test/test.js @@ -0,0 +1,173 @@ +/** +* @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 tape = require( 'tape' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); +var Float32Array = require( '@stdlib/array/float32' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var sminabs = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Returns a one-dimensional ndarray. +* +* @private +* @param {Collection} buffer - underlying data buffer +* @param {NonNegativeInteger} length - number of indexed elements +* @param {integer} stride - stride length +* @param {NonNegativeInteger} offset - index offset +* @returns {ndarray} one-dimensional ndarray +*/ +function vector( buffer, length, stride, offset ) { + return new ndarray( 'float32', buffer, [ length ], [ stride ], offset, 'row-major' ); +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof sminabs, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 1', function test( t ) { + t.strictEqual( sminabs.length, 1, 'has expected arity' ); + t.end(); +}); + +tape( 'the function calculates the minimum absolute value of a one-dimensional ndarray', function test( t ) { + var x; + var v; + + x = new Float32Array( [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ] ); + v = sminabs( [ vector( x, 6, 1, 0 ) ] ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = new Float32Array( [ -4.0, -5.0 ] ); + v = sminabs( [ vector( x, 2, 1, 0 ) ] ); + t.strictEqual( v, 4.0, 'returns expected value' ); + + x = new Float32Array( [ -0.0, 0.0, -0.0 ] ); + v = sminabs( [ vector( x, 3, 1, 0 ) ] ); + t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); + + x = new Float32Array( [ NaN ] ); + v = sminabs( [ vector( x, 1, 1, 0 ) ] ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = new Float32Array( [ NaN, NaN ] ); + v = sminabs( [ vector( x, 2, 1, 0 ) ] ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an empty vector, the function returns `NaN`', function test( t ) { + var x; + var v; + + x = new Float32Array( [] ); + + v = sminabs( [ vector( x, 0, 1, 0 ) ] ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided a vector containing a single element, the function returns that element', function test( t ) { + var x; + var v; + + x = new Float32Array( [ 1.0 ] ); + + v = sminabs( [ vector( x, 1, 1, 0 ) ] ); + t.strictEqual( v, 1.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports one-dimensional ndarrays having non-unit strides', function test( t ) { + var x; + var v; + + x = new Float32Array([ + 1.0, // 0 + 2.0, + 2.0, // 1 + -7.0, + -2.0, // 2 + 3.0, + 4.0, // 3 + 2.0 + ]); + + v = sminabs( [ vector( x, 4, 2, 0 ) ] ); + + t.strictEqual( v, 1.0, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports one-dimensional ndarrays having negative strides', function test( t ) { + var x; + var v; + + x = new Float32Array([ + 1.0, // 3 + 2.0, + 2.0, // 2 + -7.0, + -2.0, // 1 + 3.0, + 4.0, // 0 + 2.0 + ]); + + v = sminabs( [ vector( x, 4, -2, 6 ) ] ); + + t.strictEqual( v, 1.0, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports one-dimensional ndarrays having non-zero offsets', function test( t ) { + var x; + var v; + + x = new Float32Array([ + 2.0, + 1.0, // 0 + 2.0, + -2.0, // 1 + -2.0, + 2.0, // 2 + 3.0, + 4.0 // 3 + ]); + + v = sminabs( [ vector( x, 4, 2, 1 ) ] ); + t.strictEqual( v, 1.0, 'returns expected value' ); + + t.end(); +}); From 1776a03cbad1817d99f114cfafb8ea93cc515929 Mon Sep 17 00:00:00 2001 From: gururaj1512 Date: Thu, 14 Aug 2025 23:59:09 +0530 Subject: [PATCH 4/4] feat: add `stats/minabs` --- 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: passed - 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: passed - task: lint_license_headers status: passed --- --- .../@stdlib/stats/minabs/README.md | 266 +++++++ .../minabs/benchmark/benchmark.assign.js | 111 +++ .../stats/minabs/benchmark/benchmark.js | 105 +++ .../@stdlib/stats/minabs/docs/repl.txt | 76 ++ .../stats/minabs/docs/types/index.d.ts | 151 ++++ .../@stdlib/stats/minabs/docs/types/test.ts | 225 ++++++ .../@stdlib/stats/minabs/examples/index.js | 46 ++ .../@stdlib/stats/minabs/lib/index.js | 63 ++ .../@stdlib/stats/minabs/lib/main.js | 101 +++ .../@stdlib/stats/minabs/package.json | 67 ++ .../@stdlib/stats/minabs/test/test.assign.js | 717 +++++++++++++++++ .../@stdlib/stats/minabs/test/test.js | 39 + .../@stdlib/stats/minabs/test/test.main.js | 753 ++++++++++++++++++ 13 files changed, 2720 insertions(+) create mode 100644 lib/node_modules/@stdlib/stats/minabs/README.md create mode 100644 lib/node_modules/@stdlib/stats/minabs/benchmark/benchmark.assign.js create mode 100644 lib/node_modules/@stdlib/stats/minabs/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/stats/minabs/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/stats/minabs/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/stats/minabs/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/stats/minabs/examples/index.js create mode 100644 lib/node_modules/@stdlib/stats/minabs/lib/index.js create mode 100644 lib/node_modules/@stdlib/stats/minabs/lib/main.js create mode 100644 lib/node_modules/@stdlib/stats/minabs/package.json create mode 100644 lib/node_modules/@stdlib/stats/minabs/test/test.assign.js create mode 100644 lib/node_modules/@stdlib/stats/minabs/test/test.js create mode 100644 lib/node_modules/@stdlib/stats/minabs/test/test.main.js diff --git a/lib/node_modules/@stdlib/stats/minabs/README.md b/lib/node_modules/@stdlib/stats/minabs/README.md new file mode 100644 index 000000000000..2f7ce02d6cf0 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/minabs/README.md @@ -0,0 +1,266 @@ + + +# minabs + +> Compute the minimum absolute value along one or more [ndarray][@stdlib/ndarray/ctor] dimensions. + +
+ +## Usage + +```javascript +var minabs = require( '@stdlib/stats/minabs' ); +``` + +#### minabs( x\[, options] ) + +Computes the minimum absolute value along one or more [ndarray][@stdlib/ndarray/ctor] dimensions. + +```javascript +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ -1.0, 2.0, -3.0 ] ); + +var y = minabs( x ); +// returns + +var v = y.get(); +// returns 1.0 +``` + +The function has the following parameters: + +- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes]. +- **options**: function options (_optional_). + +The function accepts the following options: + +- **dims**: list of dimensions over which to perform a reduction. If not provided, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. +- **dtype**: output ndarray [data type][@stdlib/ndarray/dtypes]. Must be a real-valued or "generic" [data type][@stdlib/ndarray/dtypes]. +- **keepdims**: boolean indicating whether the reduced dimensions should be included in the returned [ndarray][@stdlib/ndarray/ctor] as singleton dimensions. Default: `false`. + +By default, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. To perform a reduction over specific dimensions, provide a `dims` option. + +```javascript +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ -1.0, 2.0, -3.0, 4.0 ], { + 'shape': [ 2, 2 ], + 'order': 'row-major' +}); +var v = ndarray2array( x ); +// returns [ [ -1.0, 2.0 ], [ -3.0, 4.0 ] ] + +var y = minabs( x, { + 'dims': [ 0 ] +}); +// returns + +v = ndarray2array( y ); +// returns [ 1.0, 2.0 ] + +y = minabs( x, { + 'dims': [ 1 ] +}); +// returns + +v = ndarray2array( y ); +// returns [ 1.0, 3.0 ] + +y = minabs( x, { + 'dims': [ 0, 1 ] +}); +// returns + +v = y.get(); +// returns 1.0 +``` + +By default, the function excludes reduced dimensions from the output [ndarray][@stdlib/ndarray/ctor]. To include the reduced dimensions as singleton dimensions, set the `keepdims` option to `true`. + +```javascript +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ -1.0, 2.0, -3.0, 4.0 ], { + 'shape': [ 2, 2 ], + 'order': 'row-major' +}); + +var v = ndarray2array( x ); +// returns [ [ -1.0, 2.0 ], [ -3.0, 4.0 ] ] + +var y = minabs( x, { + 'dims': [ 0 ], + 'keepdims': true +}); +// returns + +v = ndarray2array( y ); +// returns [ [ 1.0, 2.0 ] ] + +y = minabs( x, { + 'dims': [ 1 ], + 'keepdims': true +}); +// returns + +v = ndarray2array( y ); +// returns [ [ 1.0 ], [ 3.0 ] ] + +y = minabs( x, { + 'dims': [ 0, 1 ], + 'keepdims': true +}); +// returns + +v = ndarray2array( y ); +// returns [ [ 1.0 ] ] +``` + +By default, the function returns an [ndarray][@stdlib/ndarray/ctor] having a [data type][@stdlib/ndarray/dtypes] determined by the function's output data type [policy][@stdlib/ndarray/output-dtype-policies]. To override the default behavior, set the `dtype` option. + +```javascript +var getDType = require( '@stdlib/ndarray/dtype' ); +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ -1.0, 2.0, -3.0 ], { + 'dtype': 'generic' +}); + +var y = minabs( x, { + 'dtype': 'float64' +}); +// returns + +var dt = getDType( y ); +// returns 'float64' +``` + +#### minabs.assign( x, out\[, options] ) + +Computes the minimum absolute value along one or more [ndarray][@stdlib/ndarray/ctor] dimensions and assigns results to a provided output [ndarray][@stdlib/ndarray/ctor]. + +```javascript +var array = require( '@stdlib/ndarray/array' ); +var zeros = require( '@stdlib/ndarray/zeros' ); + +var x = array( [ -1.0, 2.0, -3.0 ] ); +var y = zeros( [] ); + +var out = minabs.assign( x, y ); +// returns + +var v = out.get(); +// returns 1.0 + +var bool = ( out === y ); +// returns true +``` + +The method has the following parameters: + +- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or generic [data type][@stdlib/ndarray/dtypes]. +- **out**: output [ndarray][@stdlib/ndarray/ctor]. +- **options**: function options (_optional_). + +The method accepts the following options: + +- **dims**: list of dimensions over which to perform a reduction. If not provided, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. + +
+ + + +
+ +## Notes + +- Setting the `keepdims` option to `true` can be useful when wanting to ensure that the output [ndarray][@stdlib/ndarray/ctor] is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with ndarrays having the same shape as the input [ndarray][@stdlib/ndarray/ctor]. +- The output data type [policy][@stdlib/ndarray/output-dtype-policies] only applies to the main function and specifies that, by default, the function must return an [ndarray][@stdlib/ndarray/ctor] having the same [data type][@stdlib/ndarray/dtypes] as the input [ndarray][@stdlib/ndarray/ctor]. For the `assign` method, the output [ndarray][@stdlib/ndarray/ctor] is allowed to have any supported output [data type][@stdlib/ndarray/dtypes]. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var minabs = require( '@stdlib/stats/minabs' ); + +// Generate an array of random numbers: +var xbuf = discreteUniform( 25, -10, 10, { + 'dtype': 'generic' +}); + +// Wrap in an ndarray: +var x = new ndarray( 'generic', xbuf, [ 5, 5 ], [ 5, 1 ], 0, 'row-major' ); +console.log( ndarray2array( x ) ); + +// Perform a reduction: +var y = minabs( x, { + 'dims': [ 0 ] +}); + +// Resolve the output array data type: +var dt = getDType( y ); +console.log( dt ); + +// Print the results: +console.log( ndarray2array( y ) ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/minabs/benchmark/benchmark.assign.js b/lib/node_modules/@stdlib/stats/minabs/benchmark/benchmark.assign.js new file mode 100644 index 000000000000..8431d516a627 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/minabs/benchmark/benchmark.assign.js @@ -0,0 +1,111 @@ +/** +* @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 bench = require( '@stdlib/bench' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var zeros = require( '@stdlib/array/zeros' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var pkg = require( './../package.json' ).name; +var minabs = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var out; + var x; + + x = uniform( len, -50.0, 50.0, options ); + x = new ndarray( options.dtype, x, [ len ], [ 1 ], 0, 'row-major' ); + + out = new ndarray( options.dtype, zeros( 1, options.dtype ), [], [ 0 ], 0, 'row-major' ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var o; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = minabs.assign( x, out ); + if ( typeof o !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( isnan( o.get() ) ) { + b.fail( 'should not return NaN' ); + } + 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( pkg+':assign:dtype='+options.dtype+',len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/stats/minabs/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/minabs/benchmark/benchmark.js new file mode 100644 index 000000000000..6de9cfe2a39f --- /dev/null +++ b/lib/node_modules/@stdlib/stats/minabs/benchmark/benchmark.js @@ -0,0 +1,105 @@ +/** +* @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 bench = require( '@stdlib/bench' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var pkg = require( './../package.json' ).name; +var minabs = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = uniform( len, -50.0, 50.0, options ); + x = new ndarray( options.dtype, x, [ len ], [ 1 ], 0, 'row-major' ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var o; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = minabs( x ); + if ( typeof o !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( isnan( o.get() ) ) { + b.fail( 'should not return NaN' ); + } + 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( pkg+':dtype='+options.dtype+',len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/stats/minabs/docs/repl.txt b/lib/node_modules/@stdlib/stats/minabs/docs/repl.txt new file mode 100644 index 000000000000..f5079e8df2ab --- /dev/null +++ b/lib/node_modules/@stdlib/stats/minabs/docs/repl.txt @@ -0,0 +1,76 @@ + +{{alias}}( x[, options] ) + Computes the minimum absolute value along one or more ndarray dimensions. + + Parameters + ---------- + x: ndarray + Input array. Must have a real-valued or "generic" data type. + + options: Object (optional) + Function options. + + options.dtype: string (optional) + Output array data type. Must be a real-valued or "generic" data type. + + options.dims: Array (optional) + List of dimensions over which to perform a reduction. If not provided, + the function performs a reduction over all elements in a provided input + ndarray. + + options.keepdims: boolean (optional) + Boolean indicating whether the reduced dimensions should be included in + the returned ndarray as singleton dimensions. Default: false. + + Returns + ------- + out: ndarray + Output array. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ -1.0, 2.0, -3.0, -4.0 ] ); + > var y = {{alias}}( x ); + > var v = y.get() + 1.0 + + +{{alias}}.assign( x, out[, options] ) + Computes the minimum absolute value along one or more ndarray dimensions + and assigns results to a provided output ndarray. + + Parameters + ---------- + x: ndarray + Input array. Must have a real-valued or generic data type. + + out: ndarray + Output array. + + options: Object (optional) + Function options. + + options.dims: Array (optional) + List of dimensions over which to perform a reduction. If not provided, + the function performs a reduction over all elements in a provided input + ndarray. + + Returns + ------- + out: ndarray + Output array. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ -1.0, 2.0, -3.0, -4.0 ] ); + > var out = {{alias:@stdlib/ndarray/zeros}}( [] ); + > var y = {{alias}}.assign( x, out ) + + > var bool = ( out === y ) + true + > var v = out.get() + 1.0 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/minabs/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/minabs/docs/types/index.d.ts new file mode 100644 index 000000000000..0ac1d555a1f9 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/minabs/docs/types/index.d.ts @@ -0,0 +1,151 @@ +/* +* @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. +*/ + +// TypeScript Version: 4.1 + +/// + +import { ArrayLike } from '@stdlib/types/array'; +import { RealAndGenericDataType as DataType, typedndarray } from '@stdlib/types/ndarray'; + +/** +* Input array. +*/ +type InputArray = typedndarray; + +/** +* Output array. +*/ +type OutputArray = typedndarray; + +/** +* Interface defining "base" options. +*/ +interface BaseOptions { + /** + * List of dimensions over which to perform a reduction. + */ + dims?: ArrayLike; +} + +/** +* Interface defining options. +*/ +interface Options extends BaseOptions { + /** + * Output array data type. + */ + dtype?: DataType; + + /** + * Boolean indicating whether the reduced dimensions should be included in the returned array as singleton dimensions. Default: `false`. + */ + keepdims?: boolean; +} + +/** +* Interface for performing a reduction on an ndarray. +*/ +interface Unary { + /** + * Computes the minimum absolute value along one or more ndarray dimensions. + * + * @param x - input ndarray + * @param options - function options + * @returns output ndarray + * + * @example + * var array = require( '@stdlib/ndarray/array' ); + * + * var x = array( [ -1.0, 2.0, -3.0 ] ); + * + * var y = minabs( x ); + * // returns + * + * var v = y.get(); + * // returns 1.0 + */ + ( x: InputArray, options?: Options ): OutputArray; // NOTE: we lose type specificity here, but retaining specificity would likely be difficult and/or tedious to completely enumerate, as the output ndarray data type is dependent on how `x` interacts with output data type policy and whether that policy has been overridden by `options.dtype`. + + /** + * Computes the minimum absolute value along one or more ndarray dimensions and assigns results to a provided output ndarray. + * + * @param x - input ndarray + * @param out - output ndarray + * @param options - function options + * @returns output ndarray + * + * @example + * var array = require( '@stdlib/ndarray/array' ); + * var zeros = require( '@stdlib/ndarray/zeros' ); + * + * var x = array( [ -1.0, 2.0, -3.0 ] ); + * var y = zeros( [] ); + * + * var out = minabs.assign( x, y ); + * // returns + * + * var v = out.get(); + * // returns 1.0 + * + * var bool = ( out === y ); + * // returns true + */ + assign = OutputArray>( x: InputArray, out: U, options?: BaseOptions ): U; +} + +/** +* Computes the minimum absolute value along one or more ndarray dimensions. +* +* @param x - input ndarray +* @param options - function options +* @returns output ndarray +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ -1.0, 2.0, -3.0 ] ); +* +* var y = minabs( x ); +* // returns +* +* var v = y.get(); +* // returns 1.0 +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var zeros = require( '@stdlib/ndarray/zeros' ); +* +* var x = array( [ -1.0, 2.0, -3.0 ] ); +* var y = zeros( [] ); +* +* var out = minabs.assign( x, y ); +* // returns +* +* var v = out.get(); +* // returns 1.0 +* +* var bool = ( out === y ); +* // returns true +*/ +declare const minabs: Unary; + + +// EXPORTS // + +export = minabs; diff --git a/lib/node_modules/@stdlib/stats/minabs/docs/types/test.ts b/lib/node_modules/@stdlib/stats/minabs/docs/types/test.ts new file mode 100644 index 000000000000..49e4e6453d3c --- /dev/null +++ b/lib/node_modules/@stdlib/stats/minabs/docs/types/test.ts @@ -0,0 +1,225 @@ +/* +* @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. +*/ + +/* eslint-disable @typescript-eslint/no-unused-expressions, space-in-parens */ + +/// + +import zeros = require( '@stdlib/ndarray/zeros' ); +import minabs = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + minabs( x ); // $ExpectType OutputArray + minabs( x, {} ); // $ExpectType OutputArray +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + minabs( '5' ); // $ExpectError + minabs( 5 ); // $ExpectError + minabs( true ); // $ExpectError + minabs( false ); // $ExpectError + minabs( null ); // $ExpectError + minabs( void 0 ); // $ExpectError + minabs( {} ); // $ExpectError + minabs( ( x: number ): number => x ); // $ExpectError + + minabs( '5', {} ); // $ExpectError + minabs( 5, {} ); // $ExpectError + minabs( true, {} ); // $ExpectError + minabs( false, {} ); // $ExpectError + minabs( null, {} ); // $ExpectError + minabs( void 0, {} ); // $ExpectError + minabs( {}, {} ); // $ExpectError + minabs( ( x: number ): number => x, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not an object... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + minabs( x, '5' ); // $ExpectError + minabs( x, true ); // $ExpectError + minabs( x, false ); // $ExpectError + minabs( x, null ); // $ExpectError + minabs( x, [] ); // $ExpectError + minabs( x, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid `dtype` option... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + minabs( x, { 'dtype': '5' } ); // $ExpectError + minabs( x, { 'dtype': 5 } ); // $ExpectError + minabs( x, { 'dtype': true } ); // $ExpectError + minabs( x, { 'dtype': false } ); // $ExpectError + minabs( x, { 'dtype': null } ); // $ExpectError + minabs( x, { 'dtype': [] } ); // $ExpectError + minabs( x, { 'dtype': {} } ); // $ExpectError + minabs( x, { 'dtype': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid `keepdims` option... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + minabs( x, { 'keepdims': '5' } ); // $ExpectError + minabs( x, { 'keepdims': 5 } ); // $ExpectError + minabs( x, { 'keepdims': null } ); // $ExpectError + minabs( x, { 'keepdims': [] } ); // $ExpectError + minabs( x, { 'keepdims': {} } ); // $ExpectError + minabs( x, { 'keepdims': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid `dims` option... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + minabs( x, { 'dims': '5' } ); // $ExpectError + minabs( x, { 'dims': 5 } ); // $ExpectError + minabs( x, { 'dims': true } ); // $ExpectError + minabs( x, { 'dims': false } ); // $ExpectError + minabs( x, { 'dims': null } ); // $ExpectError + minabs( x, { 'dims': {} } ); // $ExpectError + minabs( x, { 'dims': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + minabs(); // $ExpectError + minabs( x, {}, {} ); // $ExpectError +} + +// Attached to the function is an `assign` method which returns an ndarray... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + minabs.assign( x, x ); // $ExpectType float64ndarray + minabs.assign( x, x, {} ); // $ExpectType float64ndarray +} + +// The compiler throws an error if the `assign` method is provided a first argument which is not an ndarray... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + minabs.assign( '5', x ); // $ExpectError + minabs.assign( 5, x ); // $ExpectError + minabs.assign( true, x ); // $ExpectError + minabs.assign( false, x ); // $ExpectError + minabs.assign( null, x ); // $ExpectError + minabs.assign( void 0, x ); // $ExpectError + minabs.assign( {}, x ); // $ExpectError + minabs.assign( ( x: number ): number => x, x ); // $ExpectError + + minabs.assign( '5', x, {} ); // $ExpectError + minabs.assign( 5, x, {} ); // $ExpectError + minabs.assign( true, x, {} ); // $ExpectError + minabs.assign( false, x, {} ); // $ExpectError + minabs.assign( null, x, {} ); // $ExpectError + minabs.assign( void 0, x, {} ); // $ExpectError + minabs.assign( {}, x, {} ); // $ExpectError + minabs.assign( ( x: number ): number => x, x, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a second argument which is not an ndarray... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + minabs.assign( x, '5' ); // $ExpectError + minabs.assign( x, 5 ); // $ExpectError + minabs.assign( x, true ); // $ExpectError + minabs.assign( x, false ); // $ExpectError + minabs.assign( x, null ); // $ExpectError + minabs.assign( x, void 0 ); // $ExpectError + minabs.assign( x, ( x: number ): number => x ); // $ExpectError + + minabs.assign( x, '5', {} ); // $ExpectError + minabs.assign( x, 5, {} ); // $ExpectError + minabs.assign( x, true, {} ); // $ExpectError + minabs.assign( x, false, {} ); // $ExpectError + minabs.assign( x, null, {} ); // $ExpectError + minabs.assign( x, void 0, {} ); // $ExpectError + minabs.assign( x, ( x: number ): number => x, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a third argument which is not an object... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + minabs.assign( x, x, '5' ); // $ExpectError + minabs.assign( x, x, true ); // $ExpectError + minabs.assign( x, x, false ); // $ExpectError + minabs.assign( x, x, null ); // $ExpectError + minabs.assign( x, x, [] ); // $ExpectError + minabs.assign( x, x, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided an invalid `dims` option... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + minabs.assign( x, x, { 'dims': '5' } ); // $ExpectError + minabs.assign( x, x, { 'dims': 5 } ); // $ExpectError + minabs.assign( x, x, { 'dims': true } ); // $ExpectError + minabs.assign( x, x, { 'dims': false } ); // $ExpectError + minabs.assign( x, x, { 'dims': null } ); // $ExpectError + minabs.assign( x, x, { 'dims': {} } ); // $ExpectError + minabs.assign( x, x, { 'dims': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided an unsupported number of arguments... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + minabs.assign(); // $ExpectError + minabs.assign( x ); // $ExpectError + minabs.assign( x, x, {}, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/minabs/examples/index.js b/lib/node_modules/@stdlib/stats/minabs/examples/index.js new file mode 100644 index 000000000000..8a5a77694fc9 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/minabs/examples/index.js @@ -0,0 +1,46 @@ +/** +* @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'; + +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var minabs = require( './../lib' ); + +// Generate an array of random numbers: +var xbuf = discreteUniform( 25, -10, 10, { + 'dtype': 'generic' +}); + +// Wrap in an ndarray: +var x = new ndarray( 'generic', xbuf, [ 5, 5 ], [ 5, 1 ], 0, 'row-major' ); +console.log( ndarray2array( x ) ); + +// Perform a reduction: +var y = minabs( x, { + 'dims': [ 0 ] +}); + +// Resolve the output array data type: +var dt = getDType( y ); +console.log( dt ); + +// Print the results: +console.log( ndarray2array( y ) ); diff --git a/lib/node_modules/@stdlib/stats/minabs/lib/index.js b/lib/node_modules/@stdlib/stats/minabs/lib/index.js new file mode 100644 index 000000000000..be18a83af862 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/minabs/lib/index.js @@ -0,0 +1,63 @@ +/** +* @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'; + +/** +* Compute the minimum absolute value along one or more ndarray dimensions. +* +* @module @stdlib/stats/minabs +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var minabs = require( '@stdlib/stats/minabs' ); +* +* // Create a data buffer: +* var xbuf = new Float64Array( [ 0.0, 2.0, 3.0, 0.0, 0.0, 6.0, 7.0, 0.0, 0.0, 10.0, 11.0, 0.0 ] ); +* +* // Define the shape of the input array: +* var sh = [ 3, 1, 2 ]; +* +* // Define the array strides: +* var sx = [ 4, 4, 1 ]; +* +* // Define the index offset: +* var ox = 1; +* +* // Create an input ndarray: +* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); +* +* // Perform reduction: +* var out = minabs( x ); +* // returns +* +* var v = out.get(); +* // returns 2.0 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; + +// exports: { "assign": "main.assign" } diff --git a/lib/node_modules/@stdlib/stats/minabs/lib/main.js b/lib/node_modules/@stdlib/stats/minabs/lib/main.js new file mode 100644 index 000000000000..1d5d2553a0e6 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/minabs/lib/main.js @@ -0,0 +1,101 @@ +/** +* @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 dtypes = require( '@stdlib/ndarray/dtypes' ); +var gminabs = require( '@stdlib/stats/base/ndarray/minabs' ); +var dminabs = require( '@stdlib/stats/base/ndarray/dminabs' ); +var sminabs = require( '@stdlib/stats/base/ndarray/sminabs' ); +var factory = require( '@stdlib/ndarray/base/unary-reduce-strided1d-dispatch-factory' ); + + +// VARIABLES // + +var idtypes = dtypes( 'real_and_generic' ); +var odtypes = dtypes( 'real_and_generic' ); +var policies = { + 'output': 'same', + 'casting': 'none' +}; +var table = { + 'types': [ + 'float64', // input + 'float32' // input + ], + 'fcns': [ + dminabs, + sminabs + ], + 'default': gminabs +}; + + +// MAIN // + +/** +* Computes the minimum absolute value along one or more ndarray dimensions. +* +* @name minabs +* @type {Function} +* @param {ndarray} x - input ndarray +* @param {Options} [options] - function options +* @param {IntegerArray} [options.dims] - list of dimensions over which to perform a reduction +* @param {boolean} [options.keepdims=false] - boolean indicating whether the reduced dimensions should be included in the returned ndarray as singleton dimensions +* @param {string} [options.dtype] - output ndarray data type +* @throws {TypeError} first argument must be an ndarray-like object +* @throws {TypeError} options argument must be an object +* @throws {RangeError} dimension indices must not exceed input ndarray bounds +* @throws {RangeError} number of dimension indices must not exceed the number of input ndarray dimensions +* @throws {Error} must provide valid options +* @returns {ndarray} output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* // Create a data buffer: +* var xbuf = new Float64Array( [ 0.0, 2.0, 3.0, 0.0, 0.0, 6.0, 7.0, 0.0, 0.0, 10.0, 11.0, 0.0 ] ); +* +* // Define the shape of the input array: +* var sh = [ 3, 1, 2 ]; +* +* // Define the array strides: +* var sx = [ 4, 4, 1 ]; +* +* // Define the index offset: +* var ox = 1; +* +* // Create an input ndarray: +* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); +* +* // Perform reduction: +* var out = minabs( x ); +* // returns +* +* var v = out.get(); +* // returns 2.0 +*/ +var minabs = factory( table, [ idtypes ], odtypes, policies ); + + +// EXPORTS // + +module.exports = minabs; diff --git a/lib/node_modules/@stdlib/stats/minabs/package.json b/lib/node_modules/@stdlib/stats/minabs/package.json new file mode 100644 index 000000000000..462888641bf7 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/minabs/package.json @@ -0,0 +1,67 @@ +{ + "name": "@stdlib/stats/minabs", + "version": "0.0.0", + "description": "Compute the minimum absolute value along one or more ndarray dimensions.", + "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", + "minimum", + "min", + "range", + "extremes", + "domain", + "extent", + "ndarray" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/stats/minabs/test/test.assign.js b/lib/node_modules/@stdlib/stats/minabs/test/test.assign.js new file mode 100644 index 000000000000..c6d19982d020 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/minabs/test/test.assign.js @@ -0,0 +1,717 @@ +/** +* @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 tape = require( 'tape' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var empty = require( '@stdlib/ndarray/empty' ); +var emptyLike = require( '@stdlib/ndarray/empty-like' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var minabs = require( './../lib' ).assign; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof minabs, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object', function test( t ) { + var values; + var out; + var i; + + out = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minabs( value, out ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (options)', function test( t ) { + var values; + var out; + var i; + + out = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minabs( value, out, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object having a supported data type', function test( t ) { + var values; + var out; + var i; + + out = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + empty( [ 2, 2 ], { + 'dtype': 'bool' + }) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minabs( value, out ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object having a supported data type (options)', function test( t ) { + var values; + var out; + var i; + + out = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + empty( [ 2, 2 ], { + 'dtype': 'bool' + }) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minabs( value, out, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not an ndarray-like object', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minabs( x, value ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not an ndarray-like object (options)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minabs( x, value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a third argument which is not an object', function test( t ) { + var values; + var out; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + out = zeros( [], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minabs( x, out, value ); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which is not an array-like object of integers', function test( t ) { + var values; + var out; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + out = zeros( [], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [ 'a' ], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minabs( x, out, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains out-of-bounds indices', function test( t ) { + var values; + var out; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + out = zeros( [ 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ -10 ], + [ 20 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minabs( x, out, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains too many indices', function test( t ) { + var values; + var out; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + out = zeros( [], { + 'dtype': 'generic' + }); + + values = [ + [ 0, 1, 2 ], + [ 0, 1, 2, 3 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minabs( x, out, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains duplicate indices', function test( t ) { + var values; + var out; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + out = zeros( [], { + 'dtype': 'generic' + }); + + values = [ + [ 0, 0 ], + [ 1, 1 ], + [ 0, 1, 0 ], + [ 1, 0, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minabs( x, out, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided an output array which has an invalid shape (default)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ 2, 2 ], + [ 2 ], + [ 4, 4 ], + [ 4 ], + [ 1 ], + [ 1, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var out = zeros( value, { + 'dtype': 'generic' + }); + minabs( x, out ); + }; + } +}); + +tape( 'the function throws an error if provided an output array which has an invalid shape (all dimensions)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ 2, 2 ], + [ 2 ], + [ 4, 4 ], + [ 4 ], + [ 1 ], + [ 1, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var out = zeros( value, { + 'dtype': 'generic' + }); + minabs( x, out, { + 'dims': [ 0, 1 ] + }); + }; + } +}); + +tape( 'the function throws an error if provided an output array which has an invalid shape (some dimensions)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [], + [ 4, 4 ], + [ 4 ], + [ 1 ], + [ 1, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var out = zeros( value, { + 'dtype': 'generic' + }); + minabs( x, out, { + 'dims': [ 0 ] + }); + }; + } +}); + +tape( 'the function performs a reduction on an ndarray (default, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + out = emptyLike( x, { + 'shape': [] + }); + + actual = minabs( x, out ); + expected = 1.0; + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (default, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + out = emptyLike( x, { + 'shape': [] + }); + + actual = minabs( x, out ); + expected = 1.0; + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (all dimensions, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + out = emptyLike( x, { + 'shape': [] + }); + + actual = minabs( x, out, { + 'dims': [ 0, 1 ] + }); + expected = 1.0; + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (all dimensions, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + out = emptyLike( x, { + 'shape': [] + }); + + actual = minabs( x, out, { + 'dims': [ 0, 1 ] + }); + expected = 1.0; + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (no dimensions, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + out = emptyLike( x, { + 'shape': [ 2, 2 ] + }); + + actual = minabs( x, out, { + 'dims': [] + }); + expected = [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (no dimensions, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + out = emptyLike( x, { + 'shape': [ 2, 2 ] + }); + + actual = minabs( x, out, { + 'dims': [] + }); + expected = [ [ 1.0, 3.0 ], [ 2.0, 4.0 ] ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying reduction dimensions (row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + out = emptyLike( x, { + 'shape': [ 2 ] + }); + + actual = minabs( x, out, { + 'dims': [ 0 ] + }); + expected = [ 1.0, 2.0 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + out = emptyLike( x, { + 'shape': [ 2 ] + }); + + actual = minabs( x, out, { + 'dims': [ 1 ] + }); + expected = [ 1.0, 3.0 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying reduction dimensions (column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + out = emptyLike( x, { + 'shape': [ 2 ] + }); + + actual = minabs( x, out, { + 'dims': [ 0 ] + }); + expected = [ 1.0, 3.0 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + out = emptyLike( x, { + 'shape': [ 2 ] + }); + + actual = minabs( x, out, { + 'dims': [ 1 ] + }); + expected = [ 1.0, 2.0 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/minabs/test/test.js b/lib/node_modules/@stdlib/stats/minabs/test/test.js new file mode 100644 index 000000000000..0dd9f163dbd2 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/minabs/test/test.js @@ -0,0 +1,39 @@ +/** +* @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 tape = require( 'tape' ); +var isMethod = require( '@stdlib/assert/is-method' ); +var minabs = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof minabs, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is an `assign` method', function test( t ) { + t.strictEqual( isMethod( minabs, 'assign' ), true, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/minabs/test/test.main.js b/lib/node_modules/@stdlib/stats/minabs/test/test.main.js new file mode 100644 index 000000000000..3bbcf6b9b835 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/minabs/test/test.main.js @@ -0,0 +1,753 @@ +/** +* @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 tape = require( 'tape' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var empty = require( '@stdlib/ndarray/empty' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var minabs = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof minabs, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minabs( value ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (options)', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minabs( value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object having a supported data type', function test( t ) { + var values; + var i; + + values = [ + empty( [ 2, 2 ], { + 'dtype': 'bool' + }) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minabs( value ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object having a supported data type (options)', function test( t ) { + var values; + var i; + + values = [ + empty( [ 2, 2 ], { + 'dtype': 'bool' + }) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minabs( value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not an object', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minabs( x, value ); + }; + } +}); + +tape( 'the function throws an error if provided a `dtype` option which is not a supported data type', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + 'foo', + 'bar', + 'beep', + 'boop' + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minabs( x, { + 'dtype': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `keepdims` option which is not a boolean', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minabs( x, { + 'keepdims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which is not an array-like object of integers', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [ 'a' ], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minabs( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains out-of-bounds indices', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ -10 ], + [ 0, 20 ], + [ 20 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minabs( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains too many indices', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ 0, 1, 2 ], + [ 0, 1, 2, 3 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minabs( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains duplicate indices', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ 0, 0 ], + [ 1, 1 ], + [ 0, 1, 0 ], + [ 1, 0, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minabs( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function performs a reduction on an ndarray (default, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = minabs( x ); + expected = 1.0; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (default, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = minabs( x ); + expected = 1.0; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (all dimensions, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = minabs( x, { + 'dims': [ 0, 1 ] + }); + expected = 1.0; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = minabs( x, { + 'dims': [ 0, 1 ], + 'keepdims': false + }); + expected = 1.0; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = minabs( x, { + 'dims': [ 0, 1 ], + 'keepdims': true + }); + expected = [ [ 1.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 1, 1 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (all dimensions, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = minabs( x, { + 'dims': [ 0, 1 ] + }); + expected = 1.0; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = minabs( x, { + 'dims': [ 0, 1 ], + 'keepdims': false + }); + expected = 1.0; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = minabs( x, { + 'dims': [ 0, 1 ], + 'keepdims': true + }); + expected = [ [ 1.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 1, 1 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (no dimensions, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = minabs( x, { + 'dims': [], + 'keepdims': false + }); + expected = [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = minabs( x, { + 'dims': [], + 'keepdims': true + }); + expected = [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (no dimensions, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = minabs( x, { + 'dims': [], + 'keepdims': false + }); + expected = [ [ 1.0, 3.0 ], [ 2.0, 4.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = minabs( x, { + 'dims': [], + 'keepdims': true + }); + expected = [ [ 1.0, 3.0 ], [ 2.0, 4.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying reduction dimensions (row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = minabs( x, { + 'dims': [ 0 ], + 'keepdims': false + }); + expected = [ 1.0, 2.0 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = minabs( x, { + 'dims': [ 0 ], + 'keepdims': true + }); + expected = [ [ 1.0, 2.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 1, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = minabs( x, { + 'dims': [ 1 ], + 'keepdims': false + }); + expected = [ 1.0, 3.0 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = minabs( x, { + 'dims': [ 1 ], + 'keepdims': true + }); + expected = [ [ 1.0 ], [ 3.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 1 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying reduction dimensions (column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = minabs( x, { + 'dims': [ 0 ], + 'keepdims': false + }); + expected = [ 1.0, 3.0 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = minabs( x, { + 'dims': [ 0 ], + 'keepdims': true + }); + expected = [ [ 1.0, 3.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 1, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = minabs( x, { + 'dims': [ 1 ], + 'keepdims': false + }); + expected = [ 1.0, 2.0 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = minabs( x, { + 'dims': [ 1 ], + 'keepdims': true + }); + expected = [ [ 1.0 ], [ 2.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 1 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying the output array data type', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = minabs( x, { + 'dtype': 'float64' + }); + expected = 1.0; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'float64', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = minabs( x, { + 'dtype': 'float64' + }); + expected = 1.0; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'float64', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +});