From 82c9eb430fef7d9548685ee59990b1d1ad744ae5 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Sat, 20 Sep 2025 22:32:45 +0500 Subject: [PATCH 1/3] feat: add ndarray/any --- 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/ndarray/any/README.md | 280 +++++++++++++++ .../ndarray/any/benchmark/benchmark.1d.js | 137 +++++++ .../ndarray/any/benchmark/benchmark.2d.js | 151 ++++++++ .../@stdlib/ndarray/any/docs/repl.txt | 82 +++++ .../@stdlib/ndarray/any/docs/types/index.d.ts | 203 +++++++++++ .../@stdlib/ndarray/any/docs/types/test.ts | 197 ++++++++++ .../@stdlib/ndarray/any/examples/index.js | 84 +++++ .../@stdlib/ndarray/any/lib/assign.js | 111 ++++++ .../@stdlib/ndarray/any/lib/defaults.json | 4 + .../@stdlib/ndarray/any/lib/index.js | 103 ++++++ .../@stdlib/ndarray/any/lib/main.js | 141 ++++++++ .../@stdlib/ndarray/any/lib/validate.js | 90 +++++ .../@stdlib/ndarray/any/package.json | 65 ++++ .../@stdlib/ndarray/any/test/test.assign.js | 262 ++++++++++++++ .../@stdlib/ndarray/any/test/test.js | 39 ++ .../@stdlib/ndarray/any/test/test.main.js | 337 ++++++++++++++++++ 16 files changed, 2286 insertions(+) create mode 100644 lib/node_modules/@stdlib/ndarray/any/README.md create mode 100644 lib/node_modules/@stdlib/ndarray/any/benchmark/benchmark.1d.js create mode 100644 lib/node_modules/@stdlib/ndarray/any/benchmark/benchmark.2d.js create mode 100644 lib/node_modules/@stdlib/ndarray/any/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/ndarray/any/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/ndarray/any/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/ndarray/any/examples/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/any/lib/assign.js create mode 100644 lib/node_modules/@stdlib/ndarray/any/lib/defaults.json create mode 100644 lib/node_modules/@stdlib/ndarray/any/lib/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/any/lib/main.js create mode 100644 lib/node_modules/@stdlib/ndarray/any/lib/validate.js create mode 100644 lib/node_modules/@stdlib/ndarray/any/package.json create mode 100644 lib/node_modules/@stdlib/ndarray/any/test/test.assign.js create mode 100644 lib/node_modules/@stdlib/ndarray/any/test/test.js create mode 100644 lib/node_modules/@stdlib/ndarray/any/test/test.main.js diff --git a/lib/node_modules/@stdlib/ndarray/any/README.md b/lib/node_modules/@stdlib/ndarray/any/README.md new file mode 100644 index 000000000000..d17744ac203d --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/any/README.md @@ -0,0 +1,280 @@ + + +# any + +> Test whether at least one element along one or more [`ndarray`][@stdlib/ndarray/ctor] dimensions is truthy. + +
+ +
+ + + +
+ +## Usage + +```javascript +var any = require( '@stdlib/ndarray/any' ); +``` + +#### any( x\[, options] ) + +Tests whether at least one element along one or more [`ndarray`][@stdlib/ndarray/ctor] dimensions is truthy. + +```javascript +var array = require( '@stdlib/ndarray/array' ); + +// Create an input ndarray: +var x = array( [ [ [ -1.0, 0.0 ] ], [ [ -3.0, -4.0 ] ], [ [ 5.0, -6.0 ] ] ] ); +// returns + +// Perform reduction: +var out = any( x ); +// returns + +var v = out.get(); +// returns true +``` + +The function accepts the following arguments: + +- **x**: input [`ndarray`][@stdlib/ndarray/ctor]. +- **options**: function options (_optional_). + +The function accepts the following `options`: + +- **dims**: list of dimensions over which to perform a reduction. +- **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 [`ndarray`][@stdlib/ndarray/ctor]. To reduce specific dimensions, set the `dims` option. + +```javascript +var array = require( '@stdlib/ndarray/array' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); + +// Create an input ndarray: +var x = array( [ [ [ -1.0, 0.0 ] ], [ [ -3.0, 0.0 ] ], [ [ 5.0, 0.0 ] ] ] ); +// returns + +// Perform reduction: +var out = any( x, { + 'dims': [ 1, 2 ] +}); +// returns + +var v = ndarray2array( out ); +// returns [ true, true, true ] +``` + +By default, the function returns an [`ndarray`][@stdlib/ndarray/ctor] having a shape matching only the non-reduced dimensions of the input [`ndarray`][@stdlib/ndarray/ctor] (i.e., the reduced dimensions are dropped). To include the reduced dimensions as singleton dimensions in the output [`ndarray`][@stdlib/ndarray/ctor], set the `keepdims` option to `true`. + +```javascript +var array = require( '@stdlib/ndarray/array' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); + +// Create an input ndarray: +var x = array( [ [ [ -1.0, 0.0 ] ], [ [ -3.0, 0.0 ] ], [ [ 5.0, 0.0 ] ] ] ); +// returns + +// Perform reduction: +var out = any( x, { + 'dims': [ 1, 2 ], + 'keepdims': true +}); +// returns + +var v = ndarray2array( out ); +// returns [ [ [ true ] ], [ [ true ] ], [ [ true ] ] ] +``` + +#### any.assign( x, out\[, options] ) + +Tests whether at least one element along one or more [`ndarray`][@stdlib/ndarray/ctor] dimensions is truthy and assigns results to a provided output [`ndarray`][@stdlib/ndarray/ctor]. + +```javascript +var array = require( '@stdlib/ndarray/array' ); +var empty = require( '@stdlib/ndarray/empty' ); + +// Create an input ndarray: +var x = array( [ [ [ -1.0, 0.0 ] ], [ [ -3.0, 0.0 ] ], [ [ 5.0, 0.0 ] ] ] ); +// returns + +// Create an output ndarray: +var y = empty( [], { + 'dtype': 'bool' +}); + +// Perform reduction: +var out = any.assign( x, y ); +// returns + +var bool = ( out === y ); +// returns true + +var v = y.get(); +// returns true +``` + +The function accepts the following arguments: + +- **x**: input [`ndarray`][@stdlib/ndarray/ctor]. +- **out**: output [`ndarray`][@stdlib/ndarray/ctor]. The output [`ndarray`][@stdlib/ndarray/ctor] must have a shape matching the non-reduced dimensions of the input [`ndarray`][@stdlib/ndarray/ctor]. +- **options**: function options (_optional_). + +The function accepts the following `options`: + +- **dims**: list of dimensions over which to perform a reduction. + +By default, the function performs a reduction over all elements in a provided [`ndarray`][@stdlib/ndarray/ctor]. To reduce specific dimensions, set the `dims` option. + +```javascript +var array = require( '@stdlib/ndarray/array' ); +var empty = require( '@stdlib/ndarray/empty' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); + +// Create an input ndarray: +var x = array( [ [ [ -1.0, 0.0 ] ], [ [ -3.0, 0.0 ] ], [ [ 5.0, 0.0 ] ] ] ); +// returns + +// Create an output ndarray: +var y = empty( [ 3 ], { + 'dtype': 'bool' +}); + +// Perform reduction: +var out = any.assign( x, y, { + 'dims': [ 1, 2 ] +}); + +var bool = ( out === y ); +// returns true + +var v = ndarray2array( y ); +// returns [ true, true, true ] +``` + +
+ + + +
+ +
+ + + +
+ +## Examples + + + +```javascript +var bernoulli = require( '@stdlib/random/base/bernoulli' ).factory; +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var fillBy = require( '@stdlib/ndarray/fill-by' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var any = require( '@stdlib/ndarray/any' ); + +var x = zeros( [ 2, 4, 5 ], { + 'dtype': 'float64' +}); +x = fillBy( x, bernoulli( 0.50 ) ); +console.log( ndarray2array( x ) ); + +var y = any( x ); +console.log( 'any(x[:,:,:]) =' ); +console.log( y.get() ); + +y = any( x, { + 'dims': [ 0 ], + 'keepdims': true +}); +console.log( 'any(x[:,j,k]) =' ); +console.log( ndarray2array( y ) ); + +y = any( x, { + 'dims': [ 1 ], + 'keepdims': true +}); +console.log( 'any(x[i,:,k]) =' ); +console.log( ndarray2array( y ) ); + +y = any( x, { + 'dims': [ 2 ], + 'keepdims': true +}); +console.log( 'any(x[i,j,:]) =' ); +console.log( ndarray2array( y ) ); + +y = any( x, { + 'dims': [ 0, 1 ], + 'keepdims': true +}); +console.log( 'any(x[:,:,k]) =' ); +console.log( ndarray2array( y ) ); + +y = any( x, { + 'dims': [ 0, 2 ], + 'keepdims': true +}); +console.log( 'any(x[:,j,:]) =' ); +console.log( ndarray2array( y ) ); + +y = any( x, { + 'dims': [ 1, 2 ], + 'keepdims': true +}); +console.log( 'any(x[i,:,:]) =' ); +console.log( ndarray2array( y ) ); + +y = any( x, { + 'dims': [ 0, 1, 2 ], + 'keepdims': true +}); +console.log( 'any(x[:,:,:]) =' ); +console.log( ndarray2array( y ) ); +``` + +
+ + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/any/benchmark/benchmark.1d.js b/lib/node_modules/@stdlib/ndarray/any/benchmark/benchmark.1d.js new file mode 100644 index 000000000000..2880678513f2 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/any/benchmark/benchmark.1d.js @@ -0,0 +1,137 @@ +/** +* @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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var pkg = require( './../package.json' ).name; +var any = require( './../lib' ); + + +// VARIABLES // + +var types = [ 'float64' ]; +var orders = [ 'row-major', 'column-major' ]; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - ndarray length +* @param {NonNegativeIntegerArray} shape - ndarray shape +* @param {string} xtype - ndarray data type +* @param {string} order - memory layout +* @param {NonNegativeIntegerArray} dims - list of dimensions to reduce +* @returns {Function} benchmark function +*/ +function createBenchmark( len, shape, xtype, order, dims ) { + var x; + + x = discreteUniform( len, -1, -100 ); + x = new ndarray( xtype, x, shape, shape2strides( shape, order ), 0, order ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var out; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = any( x, { + 'dims': dims + }); + if ( typeof out !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var dims; + var len; + var min; + var max; + var ord; + var sh; + var t1; + var f; + var i; + var j; + var k; + var n; + var d; + + min = 1; // 10^min + max = 6; // 10^max + + d = [ + [ 0 ], + [] + ]; + + for ( n = 0; n < d.length; n++ ) { + dims = d[ n ]; + for ( k = 0; k < orders.length; k++ ) { + ord = orders[ k ]; + for ( j = 0; j < types.length; j++ ) { + t1 = types[ j ]; + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + + sh = [ len ]; + f = createBenchmark( len, sh, t1, ord, dims ); + bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',xtype='+t1+',dims=['+dims.join(',' )+']', f ); + } + } + } + } +} + +main(); diff --git a/lib/node_modules/@stdlib/ndarray/any/benchmark/benchmark.2d.js b/lib/node_modules/@stdlib/ndarray/any/benchmark/benchmark.2d.js new file mode 100644 index 000000000000..1debc01b4396 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/any/benchmark/benchmark.2d.js @@ -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. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var pkg = require( './../package.json' ).name; +var any = require( './../lib' ); + + +// VARIABLES // + +var types = [ 'float64' ]; +var orders = [ 'row-major', 'column-major' ]; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - ndarray length +* @param {NonNegativeIntegerArray} shape - ndarray shape +* @param {string} xtype - ndarray data type +* @param {string} order - memory layout +* @param {NonNegativeIntegerArray} dims - list of dimensions to reduce +* @returns {Function} benchmark function +*/ +function createBenchmark( len, shape, xtype, order, dims ) { + var x; + + x = discreteUniform( len, -1, -100 ); + x = new ndarray( xtype, x, shape, shape2strides( shape, order ), 0, order ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var out; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = any( x, { + 'dims': dims + }); + if ( typeof out !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var dims; + var len; + var min; + var max; + var ord; + var sh; + var t1; + var f; + var i; + var j; + var k; + var n; + var d; + + min = 1; // 10^min + max = 6; // 10^max + + d = [ + [ 0, 1 ], + [ 0 ], + [ 1 ], + [] + ]; + + for ( n = 0; n < d.length; n++ ) { + dims = d[ n ]; + for ( k = 0; k < orders.length; k++ ) { + ord = orders[ k ]; + for ( j = 0; j < types.length; j++ ) { + t1 = types[ j ]; + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + + sh = [ len/2, 2 ]; + f = createBenchmark( len, sh, t1, ord, dims ); + bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',xtype='+t1+',dims=['+dims.join(',' )+']', f ); + + sh = [ 2, len/2 ]; + f = createBenchmark( len, sh, t1, ord, dims ); + bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',xtype='+t1+',dims=['+dims.join(',' )+']', f ); + + len = floor( sqrt( len ) ); + sh = [ len, len ]; + len *= len; + f = createBenchmark( len, sh, t1, ord, dims ); + bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',xtype='+t1+',dims=['+dims.join(',' )+']', f ); + } + } + } + } +} + +main(); diff --git a/lib/node_modules/@stdlib/ndarray/any/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/any/docs/repl.txt new file mode 100644 index 000000000000..9623b5a6bc92 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/any/docs/repl.txt @@ -0,0 +1,82 @@ + +{{alias}}( x[, options] ) + Tests whether at least one element along one or more ndarray dimensions is + truthy. + + Parameters + ---------- + x: ndarray + Input ndarray. + + 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. + + 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 ndarray. When performing a reduction over all elements, the + function returns a zero-dimensional ndarray containing the result. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ [ 1.0, 0.0 ], [ 0.0, 1.0 ] ] ); + > var y = {{alias}}( x ) + + > y.get() + true + > y = {{alias}}( x, { 'keepdims': true } ) + + > {{alias:@stdlib/ndarray/to-array}}( y ) + [ [ true ] ] + > y.get( 0, 0 ) + true + + +{{alias}}.assign( x, y[, options] ) + Tests whether at least one element along one or more ndarray dimensions is + truthy and assigns the results to a provided output ndarray. + + Parameters + ---------- + x: ndarray + Input ndarray. + + y: ndarray + Output ndarray. The output shape must match the shape of the non-reduced + dimensions of the input ndarray. + + 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 + ------- + y: ndarray + Output ndarray. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ [ 1.0, 0.0 ], [ 0.0, 1.0 ] ] ); + > var y = {{alias:@stdlib/ndarray/from-scalar}}( false ); + > var out = {{alias}}.assign( x, y ) + + > var bool = ( out === y ) + true + > y.get() + true + + See Also + -------- diff --git a/lib/node_modules/@stdlib/ndarray/any/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/any/docs/types/index.d.ts new file mode 100644 index 000000000000..029716680464 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/any/docs/types/index.d.ts @@ -0,0 +1,203 @@ +/* +* @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 { ndarray, boolndarray } from '@stdlib/types/ndarray'; + +/** +* Base options. +*/ +interface BaseOptions { + /** + * List of dimensions over which to perform the reduction. + */ + dims?: ArrayLike; +} + +/** +* Options. +*/ +interface Options extends BaseOptions { + /** + * Boolean indicating whether the reduced dimensions should be included in the returned ndarray as singleton dimensions. Default: `false`. + */ + keepdims?: boolean; +} + +/** +* Interface describing `any`. +*/ +interface Any { + /** + * Tests whether at least one element along one or more ndarray dimensions is truthy. + * + * @param x - input ndarray + * @param options - function options + * @param options.dims - list of dimensions over which to perform a reduction + * @param options.keepdims - boolean indicating whether the reduced dimensions should be included in the returned ndarray as singleton dimensions (default: false) + * @returns output ndarray + * + * @example + * var Float64Array = require( '@stdlib/array/float64' ); + * var ndarray = require( '@stdlib/ndarray/ctor' ); + * + * // Create a data buffer: + * var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.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 = any( x ); + * // returns + * + * var v = out.get(); + * // returns true + */ + ( x: ndarray, options?: Options ): boolndarray; + + /** + * Tests whether at least one element along one or more ndarray dimensions is truthy and assigns the results to a provided output ndarray. + * + * @param x - input ndarray + * @param y - output ndarray + * @param options - function options + * @param options.dims - list of dimensions over which to perform a reduction + * @returns output ndarray + * + * @example + * var Float64Array = require( '@stdlib/array/float64' ); + * var ndarray = require( '@stdlib/ndarray/ctor' ); + * var empty = require( '@stdlib/ndarray/empty' ); + * + * // Create a data buffer: + * var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); + * + * // Define the shape of the input array: + * var shape = [ 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, shape, sx, ox, 'row-major' ); + * + * // Create an output ndarray: + * var y = empty( [], { + * 'dtype': 'bool' + * }); + * + * // Perform reduction: + * var out = any.assign( x, y ); + * // returns + * + * var v = out.get(); + * // returns true + */ + assign( x: ndarray, y: T, options?: BaseOptions ): T; +} + +/** +* Tests whether at least one element along one or more ndarray dimensions is truthy. +* +* @param x - input ndarray +* @param options - function options +* @param options.dims - list of dimensions over which to perform a reduction +* @param options.keepdims - boolean indicating whether the reduced dimensions should be included in the returned ndarray as singleton dimensions (default: false) +* @returns output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* // Create a data buffer: +* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.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 = any( x ); +* // returns +* +* var v = out.get(); +* // returns true +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var empty = require( '@stdlib/ndarray/empty' ); +* +* // Create a data buffer: +* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* +* // Define the shape of the input array: +* var shape = [ 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, shape, sx, ox, 'row-major' ); +* +* // Create an output ndarray: +* var y = empty( [], { +* 'dtype': 'bool' +* }); +* +* // Perform reduction: +* var out = any.assign( x, y ); +* // returns +* +* var v = out.get(); +* // returns true +*/ +declare var any: Any; + + +// EXPORTS // + +export = any; diff --git a/lib/node_modules/@stdlib/ndarray/any/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/any/docs/types/test.ts new file mode 100644 index 000000000000..28d9e43d4f33 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/any/docs/types/test.ts @@ -0,0 +1,197 @@ +/* +* @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. +*/ + +/// + +import zeros = require( '@stdlib/ndarray/zeros' ); +import empty = require( '@stdlib/ndarray/empty' ); +import any = require( './index' ); + + +// TESTS // + +// The function returns an boolean ndarray... +{ + const x = zeros( [ 2, 2 ] ); + + any( x ); // $ExpectType boolndarray + any( x, {} ); // $ExpectType boolndarray + any( x, { 'keepdims': true } ); // $ExpectType boolndarray + any( x, { 'dims': [ 0 ] } ); // $ExpectType boolndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + any( 5 ); // $ExpectError + any( true ); // $ExpectError + any( false ); // $ExpectError + any( null ); // $ExpectError + any( undefined ); // $ExpectError + any( {} ); // $ExpectError + any( [ 1 ] ); // $ExpectError + any( ( x: number ): number => x ); // $ExpectError + + any( 5, {} ); // $ExpectError + any( true, {} ); // $ExpectError + any( false, {} ); // $ExpectError + any( null, {} ); // $ExpectError + any( undefined, {} ); // $ExpectError + any( {}, {} ); // $ExpectError + any( [ 1 ], {} ); // $ExpectError + any( ( 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 ] ); + + any( x, '5' ); // $ExpectError + any( x, 5 ); // $ExpectError + any( x, true ); // $ExpectError + any( x, false ); // $ExpectError + any( x, null ); // $ExpectError + any( x, [ 1 ] ); // $ExpectError + any( x, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided a `keepdims` option which is not a boolean... +{ + const x = zeros( [ 2, 2 ] ); + + any( x, { 'keepdims': '5' } ); // $ExpectError + any( x, { 'keepdims': 5 } ); // $ExpectError + any( x, { 'keepdims': null } ); // $ExpectError + any( x, { 'keepdims': [ 1 ] } ); // $ExpectError + any( x, { 'keepdims': {} } ); // $ExpectError + any( x, { 'keepdims': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided a `dims` option which is not an array of numbers... +{ + const x = zeros( [ 2, 2 ] ); + + any( x, { 'dims': '5' } ); // $ExpectError + any( x, { 'dims': 5 } ); // $ExpectError + any( x, { 'dims': null } ); // $ExpectError + any( x, { 'dims': true } ); // $ExpectError + any( x, { 'dims': false } ); // $ExpectError + any( x, { 'dims': {} } ); // $ExpectError + any( 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 ] ); + + any(); // $ExpectError + any( x, {}, {} ); // $ExpectError +} + +// Attached to the function is an `assign` method which returns an ndarray... +{ + const x = zeros( [ 2, 2 ] ); + const y = empty( [], { 'dtype': 'bool' } ); + + any.assign( x, y ); // $ExpectType boolndarray + any.assign( x, y, {} ); // $ExpectType boolndarray + any.assign( x, y, { 'dims': [ 0 ] } ); // $ExpectType boolndarray +} + +// The compiler throws an error if the `assign` method is provided a first argument which is not an ndarray... +{ + const y = empty( [], { 'dtype': 'bool' } ); + + any.assign( 5, y ); // $ExpectError + any.assign( true, y ); // $ExpectError + any.assign( false, y ); // $ExpectError + any.assign( null, y ); // $ExpectError + any.assign( undefined, y ); // $ExpectError + any.assign( {}, y ); // $ExpectError + any.assign( [ 1 ], y ); // $ExpectError + any.assign( ( x: number ): number => x, y ); // $ExpectError + + any.assign( 5, y, {} ); // $ExpectError + any.assign( true, y, {} ); // $ExpectError + any.assign( false, y, {} ); // $ExpectError + any.assign( null, y, {} ); // $ExpectError + any.assign( undefined, y, {} ); // $ExpectError + any.assign( {}, y, {} ); // $ExpectError + any.assign( [ 1 ], y, {} ); // $ExpectError + any.assign( ( x: number ): number => x, y, {} ); // $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 ] ); + + any.assign( x, 5 ); // $ExpectError + any.assign( x, true ); // $ExpectError + any.assign( x, false ); // $ExpectError + any.assign( x, null ); // $ExpectError + any.assign( x, undefined ); // $ExpectError + any.assign( x, {} ); // $ExpectError + any.assign( x, [ 1 ] ); // $ExpectError + any.assign( x, ( x: number ): number => x ); // $ExpectError + + any.assign( x, 5, {} ); // $ExpectError + any.assign( x, true, {} ); // $ExpectError + any.assign( x, false, {} ); // $ExpectError + any.assign( x, null, {} ); // $ExpectError + any.assign( x, undefined, {} ); // $ExpectError + any.assign( x, {}, {} ); // $ExpectError + any.assign( x, [ 1 ], {} ); // $ExpectError + any.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 ] ); + const y = empty( [], { 'dtype': 'bool' } ); + + any.assign( x, y, '5' ); // $ExpectError + any.assign( x, y, 5 ); // $ExpectError + any.assign( x, y, true ); // $ExpectError + any.assign( x, y, false ); // $ExpectError + any.assign( x, y, null ); // $ExpectError + any.assign( x, y, [ 1 ] ); // $ExpectError + any.assign( x, y, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided a `dims` option which is not an array of numbers... +{ + const x = zeros( [ 2, 2 ] ); + const y = empty( [], { 'dtype': 'bool' } ); + + any.assign( x, y, { 'dims': '5' } ); // $ExpectError + any.assign( x, y, { 'dims': 5 } ); // $ExpectError + any.assign( x, y, { 'dims': null } ); // $ExpectError + any.assign( x, y, { 'dims': true } ); // $ExpectError + any.assign( x, y, { 'dims': false } ); // $ExpectError + any.assign( x, y, { 'dims': {} } ); // $ExpectError + any.assign( x, y, { '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 ] ); + const y = empty( [], { 'dtype': 'bool' } ); + + any.assign(); // $ExpectError + any.assign( x ); // $ExpectError + any.assign( x, y, {}, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/any/examples/index.js b/lib/node_modules/@stdlib/ndarray/any/examples/index.js new file mode 100644 index 000000000000..ae1073a941c6 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/any/examples/index.js @@ -0,0 +1,84 @@ +/** +* @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 bernoulli = require( '@stdlib/random/base/bernoulli' ).factory; +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var fillBy = require( '@stdlib/ndarray/fill-by' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var any = require( './../lib' ); + +var x = zeros( [ 2, 4, 5 ], { + 'dtype': 'float64' +}); +x = fillBy( x, bernoulli( 0.50 ) ); +console.log( ndarray2array( x ) ); + +var y = any( x ); +console.log( 'any(x[:,:,:]) =' ); +console.log( y.get() ); + +y = any( x, { + 'dims': [ 0 ], + 'keepdims': true +}); +console.log( 'any(x[:,j,k]) =' ); +console.log( ndarray2array( y ) ); + +y = any( x, { + 'dims': [ 1 ], + 'keepdims': true +}); +console.log( 'any(x[i,:,k]) =' ); +console.log( ndarray2array( y ) ); + +y = any( x, { + 'dims': [ 2 ], + 'keepdims': true +}); +console.log( 'any(x[i,j,:]) =' ); +console.log( ndarray2array( y ) ); + +y = any( x, { + 'dims': [ 0, 1 ], + 'keepdims': true +}); +console.log( 'any(x[:,:,k]) =' ); +console.log( ndarray2array( y ) ); + +y = any( x, { + 'dims': [ 0, 2 ], + 'keepdims': true +}); +console.log( 'any(x[:,j,:]) =' ); +console.log( ndarray2array( y ) ); + +y = any( x, { + 'dims': [ 1, 2 ], + 'keepdims': true +}); +console.log( 'any(x[i,:,:]) =' ); +console.log( ndarray2array( y ) ); + +y = any( x, { + 'dims': [ 0, 1, 2 ], + 'keepdims': true +}); +console.log( 'any(x[:,:,:]) =' ); +console.log( ndarray2array( y ) ); diff --git a/lib/node_modules/@stdlib/ndarray/any/lib/assign.js b/lib/node_modules/@stdlib/ndarray/any/lib/assign.js new file mode 100644 index 000000000000..6eca91f8f471 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/any/lib/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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var unaryReduceSubarray = require( '@stdlib/ndarray/base/unary-reduce-subarray' ); +var ndims = require( '@stdlib/ndarray/ndims' ); +var base = require( '@stdlib/ndarray/base/any' ); +var objectAssign = require( '@stdlib/object/assign' ); +var zeroTo = require( '@stdlib/array/base/zero-to' ); +var format = require( '@stdlib/string/format' ); +var defaults = require( './defaults.json' ); +var validate = require( './validate.js' ); + + +// MAIN // + +/** +* Tests whether at least one element along one or more ndarray dimensions is truthy and assigns the results to a provided output ndarray. +* +* @param {ndarray} x - input ndarray +* @param {ndarray} y - output ndarray +* @param {Options} [options] - function options +* @param {IntegerArray} [options.dims] - list of dimensions over which to perform a reduction +* @throws {TypeError} first argument must be an ndarray-like object +* @throws {TypeError} second argument must be an ndarray-like object +* @throws {TypeError} options argument must be an object +* @throws {Error} must provide valid options +* @returns {ndarray} output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var empty = require( '@stdlib/ndarray/empty' ); +* +* // Create a data buffer: +* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* +* // Define the shape of the input array: +* var shape = [ 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, shape, sx, ox, 'row-major' ); +* +* // Create an output ndarray: +* var y = empty( [], { +* 'dtype': 'bool' +* }); +* +* // Perform reduction: +* var out = assign( x, y ); +* // returns +* +* var v = out.get(); +* // returns true +*/ +function assign( x, y, options ) { + var opts; + var err; + var N; + + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object. Value: `%s`.', x ) ); + } + if ( !isndarrayLike( y ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be an ndarray-like object. Value: `%s`.', y ) ); + } + N = ndims( x ); + + opts = objectAssign( {}, defaults ); + if ( arguments.length > 2 ) { + err = validate( opts, N, options ); + if ( err ) { + throw err; + } + } + if ( opts.dims === null ) { + opts.dims = zeroTo( N ); + } + unaryReduceSubarray( base, [ x, y ], opts.dims ); // note: we assume that this lower-level function handles further validation of the output ndarray (e.g., expected shape, etc) + return y; +} + + +// EXPORTS // + +module.exports = assign; diff --git a/lib/node_modules/@stdlib/ndarray/any/lib/defaults.json b/lib/node_modules/@stdlib/ndarray/any/lib/defaults.json new file mode 100644 index 000000000000..08433b373a0e --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/any/lib/defaults.json @@ -0,0 +1,4 @@ +{ + "dims": null, + "keepdims": false +} diff --git a/lib/node_modules/@stdlib/ndarray/any/lib/index.js b/lib/node_modules/@stdlib/ndarray/any/lib/index.js new file mode 100644 index 000000000000..564ba13fe0e1 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/any/lib/index.js @@ -0,0 +1,103 @@ +/** +* @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'; + +/** +* Test whether at least one element along one or more ndarray dimensions is truthy. +* +* @module @stdlib/ndarray/any +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var any = require( '@stdlib/ndarray/any' ); +* +* // Create a data buffer: +* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.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 = any( x ); +* // returns +* +* var v = out.get(); +* // returns true +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var empty = require( '@stdlib/ndarray/empty' ); +* var any = require( '@stdlib/ndarray/any' ); +* +* // Create a data buffer: +* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* +* // Define the shape of the input array: +* var shape = [ 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, shape, sx, ox, 'row-major' ); +* +* // Create an output ndarray: +* var y = empty( [], { +* 'dtype': 'bool' +* }); +* +* // Perform reduction: +* var out = any.assign( x, y ); +* // returns +* +* var v = out.get(); +* // returns true +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var main = require( './main.js' ); +var assign = require( './assign.js' ); + + +// MAIN // + +setReadOnly( main, 'assign', assign ); + + +// EXPORTS // + +module.exports = main; + +// exports: { "assign": "main.assign" } diff --git a/lib/node_modules/@stdlib/ndarray/any/lib/main.js b/lib/node_modules/@stdlib/ndarray/any/lib/main.js new file mode 100644 index 000000000000..3db9fcac0866 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/any/lib/main.js @@ -0,0 +1,141 @@ +/** +* @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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var unaryReduceSubarray = require( '@stdlib/ndarray/base/unary-reduce-subarray' ); +var base = require( '@stdlib/ndarray/base/any' ); +var spreadDimensions = require( '@stdlib/ndarray/base/spread-dimensions' ); +var indicesComplement = require( '@stdlib/array/base/indices-complement' ); +var getShape = require( '@stdlib/ndarray/shape' ); // note: non-base accessor is intentional due to the input array originating in userland +var getOrder = require( '@stdlib/ndarray/base/order' ); +var getData = require( '@stdlib/ndarray/base/data-buffer' ); +var getStrides = require( '@stdlib/ndarray/base/strides' ); +var getOffset = require( '@stdlib/ndarray/base/offset' ); +var empty = require( '@stdlib/ndarray/empty' ); +var ndarrayCtor = require( '@stdlib/ndarray/base/ctor' ); +var reinterpretBoolean = require( '@stdlib/strided/base/reinterpret-boolean' ); +var takeIndexed = require( '@stdlib/array/base/take-indexed' ); +var zeroTo = require( '@stdlib/array/base/zero-to' ); +var objectAssign = require( '@stdlib/object/assign' ); +var format = require( '@stdlib/string/format' ); +var defaults = require( './defaults.json' ); +var validate = require( './validate.js' ); + + +// MAIN // + +/** +* Tests whether at least one element along one or more ndarray dimensions is truthy. +* +* @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 +* @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( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.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 = any( x ); +* // returns +* +* var v = out.get(); +* // returns true +*/ +function any( x, options ) { + var opts; + var view; + var err; + var idx; + var shx; + var shy; + var N; + var y; + + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object. Value: `%s`.', x ) ); + } + shx = getShape( x ); + N = shx.length; + + opts = objectAssign( {}, defaults ); + if ( arguments.length > 1 ) { + err = validate( opts, N, options ); + if ( err ) { + throw err; + } + } + // When a list of dimensions is not provided, reduce the entire input array across all dimensions... + if ( opts.dims === null ) { + opts.dims = zeroTo( N ); + } + // Resolve the list of non-reduced dimensions: + idx = indicesComplement( N, opts.dims ); + + // Resolve the output array shape: + shy = takeIndexed( shx, idx ); + + // Initialize an output array whose shape matches that of the non-reduced dimensions and which has the same memory layout as the input array: + y = empty( shy, { + 'dtype': 'bool', + 'order': getOrder( x ) + }); + + // Reinterpret the output array as an "indexed" array to ensure faster element access: + view = new ndarrayCtor( 'uint8', reinterpretBoolean( getData( y ), 0 ), shy, getStrides( y, false ), getOffset( y ), getOrder( y ) ); + + // Perform the reduction: + unaryReduceSubarray( base, [ x, view ], opts.dims ); + + // Check whether we need to reinsert singleton dimensions which can be useful for broadcasting the returned output array to the shape of the original input array... + if ( opts.keepdims ) { + y = spreadDimensions( N, y, idx ); + } + return y; +} + + +// EXPORTS // + +module.exports = any; diff --git a/lib/node_modules/@stdlib/ndarray/any/lib/validate.js b/lib/node_modules/@stdlib/ndarray/any/lib/validate.js new file mode 100644 index 000000000000..fe5c7b00edc0 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/any/lib/validate.js @@ -0,0 +1,90 @@ +/** +* @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 isObject = require( '@stdlib/assert/is-plain-object' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var isIntegerArray = require( '@stdlib/assert/is-integer-array' ).primitives; +var isEmptyCollection = require( '@stdlib/assert/is-empty-collection' ); +var normalizeIndices = require( '@stdlib/ndarray/base/to-unique-normalized-indices' ); +var join = require( '@stdlib/array/base/join' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Validates function options. +* +* @private +* @param {Object} opts - destination object +* @param {NonNegativeInteger} ndims - number of input ndarray dimensions +* @param {Options} options - function options +* @param {boolean} [options.keepdims] - boolean indicating whether the reduced dimensions should be included in the returned ndarray as singleton dimensions +* @param {IntegerArray} [options.dims] - list of dimensions over which to perform a reduction +* @returns {(Error|null)} null or an error object +* +* @example +* var opts = {}; +* var options = { +* 'keepdims': true +* }; +* var err = validate( opts, 3, options ); +* if ( err ) { +* throw err; +* } +*/ +function validate( opts, ndims, options ) { + var tmp; + if ( !isObject( options ) ) { + return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + } + if ( hasOwnProp( options, 'keepdims' ) ) { + opts.keepdims = options.keepdims; + if ( !isBoolean( opts.keepdims ) ) { + return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'keepdims', opts.keepdims ) ); + } + } + if ( hasOwnProp( options, 'dims' ) ) { + opts.dims = options.dims; + if ( !isIntegerArray( opts.dims ) && !isEmptyCollection( opts.dims ) ) { + return new TypeError( format( 'invalid option. `%s` option must be an array of integers. Option: `%s`.', 'dims', opts.dims ) ); + } + tmp = normalizeIndices( opts.dims, ndims-1 ); + if ( tmp === null ) { + return new RangeError( format( 'invalid option. `%s` option contains an out-of-bounds dimension index. Option: [%s].', 'dims', join( opts.dims, ',' ) ) ); + } + if ( tmp.length !== opts.dims.length ) { + return new Error( format( 'invalid option. `%s` option contains duplicate indices. Option: [%s].', 'dims', join( opts.dims, ',' ) ) ); + } + if ( tmp.length > ndims ) { + return new RangeError( format( 'invalid option. `%s` option specifies more dimensions than exists in the input array. Number of dimensions: %d. Option: [%s].', 'dims', ndims, join( opts.dims, ',' ) ) ); + } + opts.dims = tmp; + } + return null; +} + + +// EXPORTS // + +module.exports = validate; diff --git a/lib/node_modules/@stdlib/ndarray/any/package.json b/lib/node_modules/@stdlib/ndarray/any/package.json new file mode 100644 index 000000000000..949ddb5868d3 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/any/package.json @@ -0,0 +1,65 @@ +{ + "name": "@stdlib/ndarray/any", + "version": "0.0.0", + "description": "Test whether at least one element along one or more ndarray dimensions is truthy.", + "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", + "strided", + "array", + "ndarray", + "any", + "one", + "utility", + "utils", + "truthy", + "reduce", + "reduction" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/ndarray/any/test/test.assign.js b/lib/node_modules/@stdlib/ndarray/any/test/test.assign.js new file mode 100644 index 000000000000..4466c0d8f732 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/any/test/test.assign.js @@ -0,0 +1,262 @@ +/** +* @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 Float64Array = require( '@stdlib/array/float64' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var empty = require( '@stdlib/ndarray/empty' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var assign = require( './../lib/assign.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof assign, '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 y; + var i; + + y = empty( [], { + 'dtype': 'bool' + }); + + 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() { + assign( value, y ); + }; + } +}); + +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 = empty( [ 2, 2 ], { + 'dtype': 'float64' + }); + + 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() { + assign( x, value ); + }; + } +}); + +tape( 'the function throws an error if provided an options argument which is not an object', function test( t ) { + var values; + var x; + var y; + var i; + + x = empty( [ 2, 2 ], { + 'dtype': 'float64' + }); + y = empty( [ 2 ], { + 'dtype': 'bool' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [] + ]; + + 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() { + assign( x, y, value ); + }; + } +}); + +tape( 'the function throws an error if provided an options with invalid `dims` property', function test( t ) { + var values; + var x; + var y; + var i; + + x = empty( [ 2, 2 ], { + 'dtype': 'float64' + }); + y = empty( [ 2 ], { + 'dtype': 'bool' + }); + + values = [ + '5', + NaN, + true, + false, + null, + void 0, + {} + ]; + + 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() { + var opts = { + 'dims': value + }; + assign( x, y, opts ); + }; + } +}); + +tape( 'the function tests whether at least one element along one or more ndarray dimensions is truthy (row-major, contiguous)', function test( t ) { + var expected; + var actual; + var x; + var y; + + x = new ndarray( 'float64', new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] ), [ 4 ], [ 1 ], 0, 'row-major' ); + y = empty( [], { + 'dtype': 'bool' + }); + + actual = assign( x, y ); + expected = true; + + t.strictEqual( actual, y, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function tests whether at least one element along one or more ndarray dimensions is truthy (row-major, contiguous, options)', function test( t ) { + var expected; + var actual; + var opts; + var x; + var y; + + x = new ndarray( 'float64', new Float64Array( [ 1.0, 0.0, 3.0, 0.0, -5.0, 0.0, -7.0, 0.0 ] ), [ 2, 4 ], [ 4, 1 ], 0, 'row-major' ); + + opts = { + 'dims': [ 0 ] + }; + y = empty( [ 4 ], { + 'dtype': 'bool' + }); + actual = assign( x, y, opts ); + expected = [ true, false, true, false ]; + t.strictEqual( actual, y, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function tests whether at least one element along one or more ndarray dimensions is truthy (column-major, contiguous)', function test( t ) { + var expected; + var actual; + var x; + var y; + + x = new ndarray( 'float64', new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] ), [ 4 ], [ 1 ], 0, 'column-major' ); + y = empty( [], { + 'dtype': 'bool' + }); + + actual = assign( x, y ); + expected = true; + + t.strictEqual( actual, y, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function tests whether at least one element along one or more ndarray dimensions is truthy (column-major, contiguous, options)', function test( t ) { + var expected; + var actual; + var opts; + var x; + var y; + + x = new ndarray( 'float64', new Float64Array( [ 1.0, 0.0, 3.0, 0.0, 5.0, 0.0, 7.0, 0.0 ] ), [ 2, 4 ], [ 1, 2 ], 0, 'column-major' ); + + opts = { + 'dims': [ 1 ] + }; + y = empty( [ 2 ], { + 'dtype': 'bool' + }); + actual = assign( x, y, opts ); + expected = [ true, false ]; + + t.strictEqual( actual, y, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/any/test/test.js b/lib/node_modules/@stdlib/ndarray/any/test/test.js new file mode 100644 index 000000000000..db4771678f46 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/any/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 any = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof any, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is an `assign` method', function test( t ) { + t.strictEqual( isMethod( any, 'assign' ), true, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/any/test/test.main.js b/lib/node_modules/@stdlib/ndarray/any/test/test.main.js new file mode 100644 index 000000000000..524fb6b0cd5c --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/any/test/test.main.js @@ -0,0 +1,337 @@ +/** +* @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 Float64Array = require( '@stdlib/array/float64' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var empty = require( '@stdlib/ndarray/empty' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var any = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof any, '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() { + any( value ); + }; + } +}); + +tape( 'the function throws an error if provided an options argument which is not an object', function test( t ) { + var values; + var x; + var i; + + x = empty( [ 2, 2 ], { + 'dtype': 'float64' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [] + ]; + + 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() { + any( x, value ); + }; + } +}); + +tape( 'the function throws an error if provided an options with invalid `dims` property', function test( t ) { + var values; + var x; + var i; + + x = empty( [ 2, 2 ], { + 'dtype': 'float64' + }); + + values = [ + '5', + NaN, + true, + false, + null, + void 0, + {} + ]; + + 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() { + var opts = { + 'dims': value + }; + any( x, opts ); + }; + } +}); + +tape( 'the function throws an error if provided an options with an out-of-bounds `dims` property', function test( t ) { + var values; + var x; + var i; + + x = empty( [ 2, 2 ], { + 'dtype': 'float64' + }); + + values = [ + [ 1, 3 ], + [ 3, 0 ], + [ 0, 2 ] + ]; + + 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() { + var opts = { + 'dims': value + }; + any( x, opts ); + }; + } +}); + +tape( 'the function throws an error if provided an options with `dims` property which contains duplicate dimensions', function test( t ) { + var values; + var x; + var i; + + x = empty( [ 2, 2 ], { + 'dtype': 'float64' + }); + + values = [ + [ 0, 0 ], + [ 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 opts = { + 'dims': value + }; + any( x, opts ); + }; + } +}); + +tape( 'the function throws an error if provided an options with `dims` property which contains dimensions more than input ndarray', function test( t ) { + var values; + var x; + var i; + + x = empty( [ 2, 2 ], { + 'dtype': 'float64' + }); + + values = [ + [ 0, 1, 2 ], + [ 0, 1, 2, 3 ], + [ 0, 1, 2, 3, 4 ] + ]; + + 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() { + var opts = { + 'dims': value + }; + any( x, opts ); + }; + } +}); + +tape( 'the function throws an error if provided an options with invalid `keepdims` property', function test( t ) { + var values; + var x; + var i; + + x = empty( [ 2, 2 ], { + 'dtype': 'float64' + }); + + values = [ + '5', + 5, + NaN, + null, + void 0, + {} + ]; + + 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() { + var opts = { + 'keepdims': value + }; + any( x, opts ); + }; + } +}); + +tape( 'the function tests whether at least one element along one or more ndarray dimensions is truthy (row-major, contiguous)', function test( t ) { + var expected; + var actual; + var x; + + x = new ndarray( 'float64', new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] ), [ 4 ], [ 1 ], 0, 'row-major' ); + + actual = any( x ); + expected = true; + + t.strictEqual( actual.get(), expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function tests whether at least one element along one or more ndarray dimensions is truthy (row-major, contiguous, options)', function test( t ) { + var expected; + var actual; + var opts; + var x; + + x = new ndarray( 'float64', new Float64Array( [ 1.0, 0.0, 3.0, 0.0, -5.0, 0.0, -7.0, 0.0 ] ), [ 2, 4 ], [ 4, 1 ], 0, 'row-major' ); + + opts = { + 'dims': [ 0 ] + }; + actual = any( x, opts ); + expected = [ true, false, true, false ]; + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + opts = { + 'dims': [ 0 ], + 'keepdims': true + }; + actual = any( x, opts ); + expected = [ [ true, false, true, false ] ]; + + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function tests whether at least one element along one or more ndarray dimensions is truthy (column-major, contiguous)', function test( t ) { + var expected; + var actual; + var x; + + x = new ndarray( 'float64', new Float64Array( [ 1.0, 0.0, 3.0, 0.0 ] ), [ 4 ], [ 1 ], 0, 'column-major' ); + + actual = any( x ); + expected = true; + + t.strictEqual( actual.get(), expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function tests whether at least one element along one or more ndarray dimensions is truthy (column-major, contiguous, options)', function test( t ) { + var expected; + var actual; + var opts; + var x; + + x = new ndarray( 'float64', new Float64Array( [ 1.0, 0.0, 3.0, 0.0, 5.0, 0.0, 7.0, 0.0 ] ), [ 2, 4 ], [ 1, 2 ], 0, 'column-major' ); + + opts = { + 'dims': [ 1 ] + }; + actual = any( x, opts ); + expected = [ true, false ]; + + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + opts = { + 'dims': [ 1 ], + 'keepdims': true + }; + actual = any( x, opts ); + expected = [ [ true ], [ false ] ]; + + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); From 6d4ff69f4aeb35880295262f5b6b212286afdb51 Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 21 Sep 2025 17:11:39 -0700 Subject: [PATCH 2/3] chore: clean-up --- 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: na - task: lint_javascript_src status: na - 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: na - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../@stdlib/ndarray/any/README.md | 2 +- .../ndarray/any/benchmark/benchmark.1d.js | 13 +- .../ndarray/any/benchmark/benchmark.2d.js | 13 +- .../@stdlib/ndarray/any/docs/types/test.ts | 2 +- .../@stdlib/ndarray/any/examples/index.js | 2 +- .../@stdlib/ndarray/any/package.json | 2 +- .../@stdlib/ndarray/any/test/test.assign.js | 113 ++++++++++++++---- .../@stdlib/ndarray/any/test/test.main.js | 83 +++++++++---- 8 files changed, 163 insertions(+), 67 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/any/README.md b/lib/node_modules/@stdlib/ndarray/any/README.md index d17744ac203d..26170d3f2d30 100644 --- a/lib/node_modules/@stdlib/ndarray/any/README.md +++ b/lib/node_modules/@stdlib/ndarray/any/README.md @@ -198,7 +198,7 @@ var any = require( '@stdlib/ndarray/any' ); var x = zeros( [ 2, 4, 5 ], { 'dtype': 'float64' }); -x = fillBy( x, bernoulli( 0.50 ) ); +x = fillBy( x, bernoulli( 0.10 ) ); console.log( ndarray2array( x ) ); var y = any( x ); diff --git a/lib/node_modules/@stdlib/ndarray/any/benchmark/benchmark.1d.js b/lib/node_modules/@stdlib/ndarray/any/benchmark/benchmark.1d.js index 2880678513f2..38ec63f95d1c 100644 --- a/lib/node_modules/@stdlib/ndarray/any/benchmark/benchmark.1d.js +++ b/lib/node_modules/@stdlib/ndarray/any/benchmark/benchmark.1d.js @@ -23,9 +23,7 @@ var bench = require( '@stdlib/bench' ); var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); var pow = require( '@stdlib/math/base/special/pow' ); -var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); -var ndarray = require( '@stdlib/ndarray/ctor' ); +var zeros = require( '@stdlib/ndarray/zeros' ); var pkg = require( './../package.json' ).name; var any = require( './../lib' ); @@ -50,11 +48,10 @@ var orders = [ 'row-major', 'column-major' ]; * @returns {Function} benchmark function */ function createBenchmark( len, shape, xtype, order, dims ) { - var x; - - x = discreteUniform( len, -1, -100 ); - x = new ndarray( xtype, x, shape, shape2strides( shape, order ), 0, order ); - + var x = zeros( shape, { + 'dtype': xtype, + 'order': order + }); return benchmark; /** diff --git a/lib/node_modules/@stdlib/ndarray/any/benchmark/benchmark.2d.js b/lib/node_modules/@stdlib/ndarray/any/benchmark/benchmark.2d.js index 1debc01b4396..0a1407a01c9e 100644 --- a/lib/node_modules/@stdlib/ndarray/any/benchmark/benchmark.2d.js +++ b/lib/node_modules/@stdlib/ndarray/any/benchmark/benchmark.2d.js @@ -25,9 +25,7 @@ var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); var pow = require( '@stdlib/math/base/special/pow' ); var sqrt = require( '@stdlib/math/base/special/sqrt' ); var floor = require( '@stdlib/math/base/special/floor' ); -var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); -var ndarray = require( '@stdlib/ndarray/ctor' ); +var zeros = require( '@stdlib/ndarray/zeros' ); var pkg = require( './../package.json' ).name; var any = require( './../lib' ); @@ -52,11 +50,10 @@ var orders = [ 'row-major', 'column-major' ]; * @returns {Function} benchmark function */ function createBenchmark( len, shape, xtype, order, dims ) { - var x; - - x = discreteUniform( len, -1, -100 ); - x = new ndarray( xtype, x, shape, shape2strides( shape, order ), 0, order ); - + var x = zeros( shape, { + 'dtype': xtype, + 'order': order + }); return benchmark; /** diff --git a/lib/node_modules/@stdlib/ndarray/any/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/any/docs/types/test.ts index 28d9e43d4f33..f291873ee778 100644 --- a/lib/node_modules/@stdlib/ndarray/any/docs/types/test.ts +++ b/lib/node_modules/@stdlib/ndarray/any/docs/types/test.ts @@ -25,7 +25,7 @@ import any = require( './index' ); // TESTS // -// The function returns an boolean ndarray... +// The function returns a boolean ndarray... { const x = zeros( [ 2, 2 ] ); diff --git a/lib/node_modules/@stdlib/ndarray/any/examples/index.js b/lib/node_modules/@stdlib/ndarray/any/examples/index.js index ae1073a941c6..b1d55a42ed1d 100644 --- a/lib/node_modules/@stdlib/ndarray/any/examples/index.js +++ b/lib/node_modules/@stdlib/ndarray/any/examples/index.js @@ -27,7 +27,7 @@ var any = require( './../lib' ); var x = zeros( [ 2, 4, 5 ], { 'dtype': 'float64' }); -x = fillBy( x, bernoulli( 0.50 ) ); +x = fillBy( x, bernoulli( 0.10 ) ); console.log( ndarray2array( x ) ); var y = any( x ); diff --git a/lib/node_modules/@stdlib/ndarray/any/package.json b/lib/node_modules/@stdlib/ndarray/any/package.json index 949ddb5868d3..1742c3eaf0b6 100644 --- a/lib/node_modules/@stdlib/ndarray/any/package.json +++ b/lib/node_modules/@stdlib/ndarray/any/package.json @@ -54,7 +54,7 @@ "array", "ndarray", "any", - "one", + "some", "utility", "utils", "truthy", diff --git a/lib/node_modules/@stdlib/ndarray/any/test/test.assign.js b/lib/node_modules/@stdlib/ndarray/any/test/test.assign.js index 4466c0d8f732..e1410253a457 100644 --- a/lib/node_modules/@stdlib/ndarray/any/test/test.assign.js +++ b/lib/node_modules/@stdlib/ndarray/any/test/test.assign.js @@ -70,6 +70,40 @@ tape( 'the function throws an error if provided a first argument which is not an } }); +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 y; + var i; + + y = empty( [], { + 'dtype': 'bool' + }); + + 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() { + assign( value, y, {} ); + }; + } +}); + 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; @@ -104,6 +138,40 @@ tape( 'the function throws an error if provided a second argument which is not a } }); +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 = empty( [ 2, 2 ], { + 'dtype': 'float64' + }); + + 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() { + assign( x, value, {} ); + }; + } +}); + tape( 'the function throws an error if provided an options argument which is not an object', function test( t ) { var values; var x; @@ -125,7 +193,8 @@ tape( 'the function throws an error if provided an options argument which is not false, null, void 0, - [] + [], + function noop() {} ]; for ( i = 0; i < values.length; i++ ) { @@ -178,7 +247,7 @@ tape( 'the function throws an error if provided an options with invalid `dims` p } }); -tape( 'the function tests whether at least one element along one or more ndarray dimensions is truthy (row-major, contiguous)', function test( t ) { +tape( 'the function tests whether at least one element along one or more ndarray dimensions is truthy (row-major)', function test( t ) { var expected; var actual; var x; @@ -197,48 +266,48 @@ tape( 'the function tests whether at least one element along one or more ndarray t.end(); }); -tape( 'the function tests whether at least one element along one or more ndarray dimensions is truthy (row-major, contiguous, options)', function test( t ) { +tape( 'the function tests whether at least one element along one or more ndarray dimensions is truthy (column-major)', function test( t ) { var expected; var actual; - var opts; var x; var y; - x = new ndarray( 'float64', new Float64Array( [ 1.0, 0.0, 3.0, 0.0, -5.0, 0.0, -7.0, 0.0 ] ), [ 2, 4 ], [ 4, 1 ], 0, 'row-major' ); - - opts = { - 'dims': [ 0 ] - }; - y = empty( [ 4 ], { + x = new ndarray( 'float64', new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] ), [ 4 ], [ 1 ], 0, 'column-major' ); + y = empty( [], { 'dtype': 'bool' }); - actual = assign( x, y, opts ); - expected = [ true, false, true, false ]; + + actual = assign( x, y ); + expected = true; + t.strictEqual( actual, y, 'returns expected value' ); - t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); t.end(); }); -tape( 'the function tests whether at least one element along one or more ndarray dimensions is truthy (column-major, contiguous)', function test( t ) { +tape( 'the function supports specifying reduction dimensions (row-major)', function test( t ) { var expected; var actual; + var opts; var x; var y; - x = new ndarray( 'float64', new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] ), [ 4 ], [ 1 ], 0, 'column-major' ); - y = empty( [], { + x = new ndarray( 'float64', new Float64Array( [ 1.0, 0.0, 3.0, 0.0, -5.0, 0.0, -7.0, 0.0 ] ), [ 2, 4 ], [ 4, 1 ], 0, 'row-major' ); + + opts = { + 'dims': [ 0 ] + }; + y = empty( [ 4 ], { 'dtype': 'bool' }); - - actual = assign( x, y ); - expected = true; - + actual = assign( x, y, opts ); + expected = [ true, false, true, false ]; t.strictEqual( actual, y, 'returns expected value' ); - t.strictEqual( actual.get(), expected, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); t.end(); }); -tape( 'the function tests whether at least one element along one or more ndarray dimensions is truthy (column-major, contiguous, options)', function test( t ) { +tape( 'the function supports specifying reduction dimensions (column-major)', function test( t ) { var expected; var actual; var opts; diff --git a/lib/node_modules/@stdlib/ndarray/any/test/test.main.js b/lib/node_modules/@stdlib/ndarray/any/test/test.main.js index 524fb6b0cd5c..72d5471c5982 100644 --- a/lib/node_modules/@stdlib/ndarray/any/test/test.main.js +++ b/lib/node_modules/@stdlib/ndarray/any/test/test.main.js @@ -65,6 +65,35 @@ tape( 'the function throws an error if provided a first argument which is not an } }); +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() { + any( value, {} ); + }; + } +}); + tape( 'the function throws an error if provided an options argument which is not an object', function test( t ) { var values; var x; @@ -82,7 +111,8 @@ tape( 'the function throws an error if provided an options argument which is not false, null, void 0, - [] + [], + function noop() {} ]; for ( i = 0; i < values.length; i++ ) { @@ -97,7 +127,7 @@ tape( 'the function throws an error if provided an options argument which is not } }); -tape( 'the function throws an error if provided an options with invalid `dims` property', function test( t ) { +tape( 'the function throws an error if provided an options argument with invalid `dims` property', function test( t ) { var values; var x; var i; @@ -113,7 +143,8 @@ tape( 'the function throws an error if provided an options with invalid `dims` p false, null, void 0, - {} + {}, + function noop() {} ]; for ( i = 0; i < values.length; i++ ) { @@ -131,7 +162,7 @@ tape( 'the function throws an error if provided an options with invalid `dims` p } }); -tape( 'the function throws an error if provided an options with an out-of-bounds `dims` property', function test( t ) { +tape( 'the function throws an error if provided an options argument with a `dims` property which contains out-of-bounds dimensions', function test( t ) { var values; var x; var i; @@ -161,7 +192,7 @@ tape( 'the function throws an error if provided an options with an out-of-bounds } }); -tape( 'the function throws an error if provided an options with `dims` property which contains duplicate dimensions', function test( t ) { +tape( 'the function throws an error if provided an options argument with `dims` property which contains duplicate dimensions', function test( t ) { var values; var x; var i; @@ -190,7 +221,7 @@ tape( 'the function throws an error if provided an options with `dims` property } }); -tape( 'the function throws an error if provided an options with `dims` property which contains dimensions more than input ndarray', function test( t ) { +tape( 'the function throws an error if provided an options argument with `dims` property which contains more dimensions than are present in the input ndarray', function test( t ) { var values; var x; var i; @@ -220,7 +251,7 @@ tape( 'the function throws an error if provided an options with `dims` property } }); -tape( 'the function throws an error if provided an options with invalid `keepdims` property', function test( t ) { +tape( 'the function throws an error if provided an options argument with invalid `keepdims` property', function test( t ) { var values; var x; var i; @@ -235,7 +266,9 @@ tape( 'the function throws an error if provided an options with invalid `keepdim NaN, null, void 0, - {} + {}, + [], + function noop() {} ]; for ( i = 0; i < values.length; i++ ) { @@ -253,7 +286,7 @@ tape( 'the function throws an error if provided an options with invalid `keepdim } }); -tape( 'the function tests whether at least one element along one or more ndarray dimensions is truthy (row-major, contiguous)', function test( t ) { +tape( 'the function tests whether at least one element along one or more ndarray dimensions is truthy (row-major)', function test( t ) { var expected; var actual; var x; @@ -267,7 +300,21 @@ tape( 'the function tests whether at least one element along one or more ndarray t.end(); }); -tape( 'the function tests whether at least one element along one or more ndarray dimensions is truthy (row-major, contiguous, options)', function test( t ) { +tape( 'the function tests whether at least one element along one or more ndarray dimensions is truthy (column-major)', function test( t ) { + var expected; + var actual; + var x; + + x = new ndarray( 'float64', new Float64Array( [ 1.0, 0.0, 3.0, 0.0 ] ), [ 4 ], [ 1 ], 0, 'column-major' ); + + actual = any( x ); + expected = true; + + t.strictEqual( actual.get(), expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports specifying reduction dimensions (row-major)', function test( t ) { var expected; var actual; var opts; @@ -294,21 +341,7 @@ tape( 'the function tests whether at least one element along one or more ndarray t.end(); }); -tape( 'the function tests whether at least one element along one or more ndarray dimensions is truthy (column-major, contiguous)', function test( t ) { - var expected; - var actual; - var x; - - x = new ndarray( 'float64', new Float64Array( [ 1.0, 0.0, 3.0, 0.0 ] ), [ 4 ], [ 1 ], 0, 'column-major' ); - - actual = any( x ); - expected = true; - - t.strictEqual( actual.get(), expected, 'returns expected value' ); - t.end(); -}); - -tape( 'the function tests whether at least one element along one or more ndarray dimensions is truthy (column-major, contiguous, options)', function test( t ) { +tape( 'the function supports specifying reduction dimensions (column-major)', function test( t ) { var expected; var actual; var opts; From 59832c765f54a715d636129716e27213800ee1ce Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 21 Sep 2025 17:17:03 -0700 Subject: [PATCH 3/3] refactor: remove unreachable path --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- lib/node_modules/@stdlib/ndarray/any/lib/main.js | 2 +- lib/node_modules/@stdlib/ndarray/any/lib/validate.js | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/any/lib/main.js b/lib/node_modules/@stdlib/ndarray/any/lib/main.js index 3db9fcac0866..8fbe5a681dc4 100644 --- a/lib/node_modules/@stdlib/ndarray/any/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/any/lib/main.js @@ -53,7 +53,7 @@ var validate = require( './validate.js' ); * @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} dimension indices must be unique * @throws {Error} must provide valid options * @returns {ndarray} output ndarray * diff --git a/lib/node_modules/@stdlib/ndarray/any/lib/validate.js b/lib/node_modules/@stdlib/ndarray/any/lib/validate.js index fe5c7b00edc0..b7985b72461f 100644 --- a/lib/node_modules/@stdlib/ndarray/any/lib/validate.js +++ b/lib/node_modules/@stdlib/ndarray/any/lib/validate.js @@ -76,9 +76,6 @@ function validate( opts, ndims, options ) { if ( tmp.length !== opts.dims.length ) { return new Error( format( 'invalid option. `%s` option contains duplicate indices. Option: [%s].', 'dims', join( opts.dims, ',' ) ) ); } - if ( tmp.length > ndims ) { - return new RangeError( format( 'invalid option. `%s` option specifies more dimensions than exists in the input array. Number of dimensions: %d. Option: [%s].', 'dims', ndims, join( opts.dims, ',' ) ) ); - } opts.dims = tmp; } return null;