diff --git a/lib/node_modules/@stdlib/ndarray/any-by/README.md b/lib/node_modules/@stdlib/ndarray/any-by/README.md new file mode 100644 index 000000000000..3df85c58bf05 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/any-by/README.md @@ -0,0 +1,292 @@ + + +# anyBy + +> Test whether at least one element along one or more [`ndarray`][@stdlib/ndarray/ctor] dimensions passes a test implemented by a predicate function. + +
+ +
+ + + +
+ +## Usage + +```javascript +var anyBy = require( '@stdlib/ndarray/any-by' ); +``` + +#### anyBy( x\[, options], predicate\[, thisArg] ) + +Tests whether at least one element along one or more [`ndarray`][@stdlib/ndarray/ctor] dimensions passes a test implemented by a predicate function. + +```javascript +var array = require( '@stdlib/ndarray/array' ); + +function isPositive( value ) { + return value > 0.0; +} + +// Create an input ndarray: +var x = array( [ [ 1.0, -2.0 ], [ 3.0, -4.0 ] ] ); + +// Test whether at least one element is positive: +var out = anyBy( x, isPositive ); +// returns + +var v = out.get(); +// returns true +``` + +The function accepts the following arguments: + +- **x**: input [`ndarray`][@stdlib/ndarray/ctor]. +- **options**: function options _(optional)_. +- **predicate**: predicate function. +- **thisArg**: predicate function execution context _(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 ndarray2array = require( '@stdlib/ndarray/to-array' ); +var array = require( '@stdlib/ndarray/array' ); + +function isPositive( value ) { + return value > 0.0; +} + +// Create an input ndarray: +var x = array( [ [ 1.0, 2.0 ], [ -3.0, -4.0 ] ] ); + +var opts = { + 'dims': [ 1 ] +}; + +// Perform reduction: +var out = anyBy( x, opts, isPositive ); +// returns + +var v = ndarray2array( out ); +// returns [ true, false ] +``` + +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 ndarray2array = require( '@stdlib/ndarray/to-array' ); +var array = require( '@stdlib/ndarray/array' ); + +function isPositive( value ) { + return value > 0.0; +} + +// Create an input ndarray: +var x = array( [ [ 1.0, 2.0 ], [ -3.0, -4.0 ] ] ); + +var opts = { + 'keepdims': true +}; + +// Perform reduction: +var out = anyBy( x, opts, isPositive ); +// returns + +var v = ndarray2array( out ); +// returns [ [ [ true ] ] ] +``` + +To set the function execution context, provide a `thisArg`. + +```javascript +var array = require( '@stdlib/ndarray/array' ); + +function isPositive( value ) { + this.count += 1; + return value > 0.0; +} + +// Create an input ndarray: +var x = array( [ [ -1.0, -2.0 ], [ -3.0, 4.0 ] ] ); +// returns + +// Create a context object: +var ctx = { + 'count': 0 +}; + +// Perform reduction: +var out = anyBy( x, isPositive, ctx ); +// returns + +var v = out.get(); +// returns true + +var count = ctx.count; +// returns 4 +``` + +#### anyBy.assign( x, y\[, options], predicate\[, thisArg] ) + +Tests whether at least one element along one or more [`ndarray`][@stdlib/ndarray/ctor] dimensions passes a test implemented by a predicate function and assigns the result to a provided output [`ndarray`][@stdlib/ndarray/ctor]. + +```javascript +var array = require( '@stdlib/ndarray/array' ); +var empty = require( '@stdlib/ndarray/empty' ); + +function isPositive( value ) { + return value > 0.0; +} + +// Create an input ndarray: +var x = array( [ [ 1.0, -2.0 ], [ 3.0, -4.0 ] ] ); + +// Create an output ndarray: +var y = empty( [], { + 'dtype': 'bool' +}); + +// Perform reduction: +var out = anyBy.assign( x, y, isPositive ); +// returns + +var v = out.get(); +// returns true + +var bool = ( out === y ); +// returns true +``` + +The function accepts the following arguments: + +- **x**: input [`ndarray`][@stdlib/ndarray/ctor]. +- **y**: 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)_. +- **predicate**: predicate function. +- **thisArg**: predicate function execution context _(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' ); + +function predicate( value ) { + return value > 0.0; +} + +// Create an input ndarray: +var x = array( [ [ 1.0, 2.0 ], [ -3.0, -4.0 ] ] ); +// returns + +// Create an output ndarray: +var y = empty( [ 2 ], { + 'dtype': 'bool' +}); + +var opts = { + 'dims': [ 1 ] +}; + +// Perform reduction: +var out = anyBy.assign( x, y, opts, predicate ); + +var bool = ( out === y ); +// returns true + +var v = ndarray2array( y ); +// returns [ true, false ] +``` + +
+ + + +
+ +## Notes + +- The predicate function is provided the following arguments: + + - **value**: current array element. + - **indices**: current array element indices. + - **arr**: the input ndarray. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory; +var isEven = require( '@stdlib/assert/is-even' ).isPrimitive; +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var fillBy = require( '@stdlib/ndarray/fill-by' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var anyBy = require( '@stdlib/ndarray/any-by' ); + +var x = zeros( [ 2, 4, 5 ], { + 'dtype': 'float64' +}); +x = fillBy( x, discreteUniform( 0, 10 ) ); +console.log( ndarray2array( x ) ); + +var y = anyBy( x, isEven ); +console.log( y.get() ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/any-by/benchmark/benchmark.1d.js b/lib/node_modules/@stdlib/ndarray/any-by/benchmark/benchmark.1d.js new file mode 100644 index 000000000000..77abf14267a8 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/any-by/benchmark/benchmark.1d.js @@ -0,0 +1,150 @@ +/** +* @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 anyBy = require( './../lib' ); + + +// VARIABLES // + +var types = [ 'float64' ]; +var orders = [ 'row-major', 'column-major' ]; + + +// FUNCTIONS // + +/** +* Callback function. +* +* @param {number} value - current array element +* @returns {boolean} result +*/ +function clbk( value ) { + return value < 0.0; +} + +/** +* 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 opts; + var out; + var i; + + opts = { + 'dims': dims + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = anyBy( x, opts, clbk ); + 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-by/benchmark/benchmark.2d.js b/lib/node_modules/@stdlib/ndarray/any-by/benchmark/benchmark.2d.js new file mode 100644 index 000000000000..e7a3bc08f162 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/any-by/benchmark/benchmark.2d.js @@ -0,0 +1,164 @@ +/** +* @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 someBy = require( './../lib' ); + + +// VARIABLES // + +var types = [ 'float64' ]; +var orders = [ 'row-major', 'column-major' ]; + + +// FUNCTIONS // + +/** +* Callback function. +* +* @param {number} value - current array element +* @returns {boolean} result +*/ +function clbk( value ) { + return value < 0.0; +} + +/** +* 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 opts; + var out; + var i; + + opts = { + 'dims': dims + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = someBy( x, opts, clbk ); + 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-by/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/any-by/docs/repl.txt new file mode 100644 index 000000000000..aecb2c595bbe --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/any-by/docs/repl.txt @@ -0,0 +1,94 @@ + +{{alias}}( x[, options], predicate[, thisArg] ) + Tests whether at least one element along one or more ndarray dimensions + passes a test implemented by a predicate function. + + 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. + + predicate: Function + Predicate function. + + thisArg: any (optional) + Predicate function execution context. + + 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, -2.0 ], [ 3.0, -4.0 ] ] ) + + > function f( v ) { return v > 0.0; }; + > var out = {{alias}}( x, f ) + + > out.get() + true + + +{{alias}}.assign( x, y[, options], predicate[, thisArg] ) + Tests whether at least one element along one or more ndarray dimensions + passes a test implemented by a predicate function 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. + + predicate: Function + Predicate function. + + thisArg: any (optional) + Predicate function execution context. + + Returns + ------- + out: ndarray + Output ndarray. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ [ 1.0, -2.0 ], [ 3.0, -4.0 ] ] ) + + > var y = {{alias:@stdlib/ndarray/empty}}( [], { 'dtype': 'bool' } ) + + > function f( v ) { return v > 0.0; }; + > var out = {{alias}}.assign( x, y, f ) + + > var bool = ( y === out ) + true + > out.get() + true + + See Also + -------- diff --git a/lib/node_modules/@stdlib/ndarray/any-by/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/any-by/docs/types/index.d.ts new file mode 100644 index 000000000000..f4e199cc124b --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/any-by/docs/types/index.d.ts @@ -0,0 +1,361 @@ +/* +* @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, typedndarray } from '@stdlib/types/ndarray'; + +/** +* Input array. +*/ +type InputArray = typedndarray; + +/** +* Returns a boolean indicating whether an element passes a test. +* +* @returns boolean indicating whether an ndarray element passes a test +*/ +type Nullary = ( this: ThisArg ) => boolean; + +/** +* Returns a boolean indicating whether an element passes a test. +* +* @param value - current array element +* @returns boolean indicating whether an ndarray element passes a test +*/ +type Unary = ( this: ThisArg, value: T ) => boolean; + +/** +* Returns a boolean indicating whether an element passes a test. +* +* @param value - current array element +* @param indices - current array element indices +* @returns boolean indicating whether an ndarray element passes a test +*/ +type Binary = ( this: ThisArg, value: T, indices: Array ) => boolean; + +/** +* Returns a boolean indicating whether an element passes a test. +* +* @param value - current array element +* @param indices - current array element indices +* @param arr - input array +* @returns boolean indicating whether an ndarray element passes a test +*/ +type Ternary = ( this: ThisArg, value: T, indices: Array, arr: U ) => boolean; + +/** +* Returns a boolean indicating whether an element passes a test. +* +* @param value - current array element +* @param indices - current array element indices +* @param arr - input array +* @returns boolean indicating whether an ndarray element passes a test +*/ +type Predicate = Nullary | Unary | Binary | Ternary; + +/** +* 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 `anyBy`. +*/ +interface AnyBy { + /** + * Tests whether at least one element along one or more ndarray dimensions passes a test implemented by a predicate function. + * + * @param x - input ndarray + * @param predicate - predicate function + * @param thisArg - predicate execution context + * @returns output ndarray + * + * @example + * var Float64Array = require( '@stdlib/array/float64' ); + * var ndarray = require( '@stdlib/ndarray/ctor' ); + * + * function isPositive( value ) { + * return value > 0.0; + * } + * + * // 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 = anyBy( x, isPositive ); + * // returns + * + * var v = out.get(); + * // returns true + */ + = InputArray, ThisArg = unknown>( x: U, predicate: Predicate, thisArg?: ThisParameterType> ): boolndarray; + + /** + * Tests whether at least one element along one or more ndarray dimensions passes a test implemented by a predicate function. + * + * @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) + * @param predicate - predicate function + * @param thisArg - predicate execution context + * @returns output ndarray + * + * @example + * var Float64Array = require( '@stdlib/array/float64' ); + * var ndarray = require( '@stdlib/ndarray/ctor' ); + * + * function isPositive( value ) { + * return value > 0.0; + * } + * + * // 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 = anyBy( x, {}, isPositive ); + * // returns + * + * var v = out.get(); + * // returns true + */ + = InputArray, ThisArg = unknown>( x: U, options: Options, predicate: Predicate, thisArg?: ThisParameterType> ): boolndarray; + + /** + * Tests whether at least one element along one or more ndarray dimensions passes a test implemented by a predicate function. + * + * @param x - input ndarray + * @param y - output ndarray + * @param predicate - predicate function + * @param thisArg - predicate execution context + * @returns output ndarray + * + * @example + * var Float64Array = require( '@stdlib/array/float64' ); + * var ndarray = require( '@stdlib/ndarray/ctor' ); + * var empty = require( '@stdlib/ndarray/empty' ); + * + * function isPositive( value ) { + * return value > 0.0; + * } + * + * // 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 = anyBy.assign( x, y, isPositive ); + * // returns + * + * var v = out.get(); + * // returns true + */ + assign = InputArray, V extends ndarray = ndarray, ThisArg = unknown>( x: U, y: V, predicate: Predicate, thisArg?: ThisParameterType> ): V; + + /** + * Tests whether at least one element along one or more ndarray dimensions passes a test implemented by a predicate function. + * + * @param x - input ndarray + * @param y - output ndarray + * @param options - function options + * @param options.dims - list of dimensions over which to perform a reduction + * @param predicate - predicate function + * @param thisArg - predicate execution context + * @returns output ndarray + * + * @example + * var Float64Array = require( '@stdlib/array/float64' ); + * var ndarray = require( '@stdlib/ndarray/ctor' ); + * var empty = require( '@stdlib/ndarray/empty' ); + * + * function isPositive( value ) { + * return value > 0.0; + * } + * + * // 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 = anyBy.assign( x, y, {}, isPositive ); + * // returns + * + * var v = out.get(); + * // returns true + */ + assign = InputArray, V extends ndarray = ndarray, ThisArg = unknown>( x: U, y: V, options: BaseOptions, predicate: Predicate, thisArg?: ThisParameterType> ): V; +} + +/** +* Tests whether at least one element along one or more ndarray dimensions passes a test implemented by a predicate function. +* +* @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) +* @param predicate - predicate function +* @param thisArg - predicate execution context +* @returns output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* function isPositive( value ) { +* return value > 0.0; +* } +* +* // 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 = anyBy( x, isPositive ); +* // 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' ); +* +* function isPositive( value ) { +* return value > 0.0; +* } +* +* // 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 = anyBy.assign( x, y, isPositive ); +* // returns +* +* var v = out.get(); +* // returns true +*/ +declare var anyBy: AnyBy; + + +// EXPORTS // + +export = anyBy; diff --git a/lib/node_modules/@stdlib/ndarray/any-by/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/any-by/docs/types/test.ts new file mode 100644 index 000000000000..38e00d18781a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/any-by/docs/types/test.ts @@ -0,0 +1,360 @@ +/* +* @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 empty = require( '@stdlib/ndarray/empty' ); +import anyBy = require( './index' ); + + +// FUNCTIONS // + +/** +* Callback function. +* +* @param value - current array element +* @returns result +*/ +function clbk( value: number ): boolean { + return value > 0.0; +} + +// TESTS // + +// The function returns an ndarray... +{ + const x = zeros( [ 2, 2 ] ); + + anyBy( x, clbk ); // $ExpectType boolndarray + anyBy( x, {}, clbk ); // $ExpectType boolndarray + anyBy( x, { 'dims': [ 0 ] }, clbk ); // $ExpectType boolndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + anyBy( '10', clbk ); // $ExpectError + anyBy( 10, clbk ); // $ExpectError + anyBy( true, clbk ); // $ExpectError + anyBy( false, clbk ); // $ExpectError + anyBy( null, clbk ); // $ExpectError + anyBy( {}, clbk ); // $ExpectError + anyBy( [], clbk ); // $ExpectError + anyBy( ( x: number ): number => x, clbk ); // $ExpectError + + anyBy( '10', {}, clbk ); // $ExpectError + anyBy( 10, {}, clbk ); // $ExpectError + anyBy( true, {}, clbk ); // $ExpectError + anyBy( false, {}, clbk ); // $ExpectError + anyBy( null, {}, clbk ); // $ExpectError + anyBy( {}, {}, clbk ); // $ExpectError + anyBy( [], {}, clbk ); // $ExpectError + anyBy( ( x: number ): number => x, {}, clbk ); // $ExpectError +} + +// The compiler throws an error if the function is provided an options argument which is not an object... +{ + const x = zeros( [ 2, 2 ] ); + + anyBy( x, '5', clbk ); // $ExpectError + anyBy( x, 5, clbk ); // $ExpectError + anyBy( x, true, clbk ); // $ExpectError + anyBy( x, false, clbk ); // $ExpectError + anyBy( x, null, clbk ); // $ExpectError + anyBy( x, [ 1 ], clbk ); // $ExpectError + anyBy( x, ( x: number ): number => x, clbk ); // $ExpectError + + anyBy( x, '5', clbk, {} ); // $ExpectError + anyBy( x, 5, clbk, {} ); // $ExpectError + anyBy( x, true, clbk, {} ); // $ExpectError + anyBy( x, false, clbk, {} ); // $ExpectError + anyBy( x, null, clbk, {} ); // $ExpectError + anyBy( x, [ 1 ], clbk, {} ); // $ExpectError + anyBy( x, ( x: number ): number => x, clbk, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a callback argument which is not a function... +{ + const x = zeros( [ 2, 2 ] ); + + anyBy( x, '10' ); // $ExpectError + anyBy( x, 10 ); // $ExpectError + anyBy( x, true ); // $ExpectError + anyBy( x, false ); // $ExpectError + anyBy( x, null ); // $ExpectError + anyBy( x, {} ); // $ExpectError + anyBy( x, [] ); // $ExpectError + + anyBy( x, {}, '10' ); // $ExpectError + anyBy( x, {}, 10 ); // $ExpectError + anyBy( x, {}, true ); // $ExpectError + anyBy( x, {}, false ); // $ExpectError + anyBy( x, {}, null ); // $ExpectError + anyBy( x, {}, {} ); // $ExpectError + anyBy( 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 ] ); + + anyBy( x, { 'keepdims': '5' }, clbk ); // $ExpectError + anyBy( x, { 'keepdims': 5 }, clbk ); // $ExpectError + anyBy( x, { 'keepdims': null }, clbk ); // $ExpectError + anyBy( x, { 'keepdims': [ 1 ] }, clbk ); // $ExpectError + anyBy( x, { 'keepdims': {} }, clbk ); // $ExpectError + anyBy( x, { 'keepdims': ( x: number ): number => x }, clbk ); // $ExpectError + + anyBy( x, { 'keepdims': '5' }, clbk, {} ); // $ExpectError + anyBy( x, { 'keepdims': 5 }, clbk, {} ); // $ExpectError + anyBy( x, { 'keepdims': null }, clbk, {} ); // $ExpectError + anyBy( x, { 'keepdims': [ 1 ] }, clbk, {} ); // $ExpectError + anyBy( x, { 'keepdims': {} }, clbk, {} ); // $ExpectError + anyBy( x, { 'keepdims': ( x: number ): number => x }, clbk, {} ); // $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 ] ); + + anyBy( x, { 'dims': '5' }, clbk ); // $ExpectError + anyBy( x, { 'dims': 5 }, clbk ); // $ExpectError + anyBy( x, { 'dims': null }, clbk ); // $ExpectError + anyBy( x, { 'dims': true }, clbk ); // $ExpectError + anyBy( x, { 'dims': false }, clbk ); // $ExpectError + anyBy( x, { 'dims': {} }, clbk ); // $ExpectError + anyBy( x, { 'dims': ( x: number ): number => x }, clbk ); // $ExpectError + + anyBy( x, { 'dims': '5' }, clbk, {} ); // $ExpectError + anyBy( x, { 'dims': 5 }, clbk, {} ); // $ExpectError + anyBy( x, { 'dims': null }, clbk, {} ); // $ExpectError + anyBy( x, { 'dims': true }, clbk, {} ); // $ExpectError + anyBy( x, { 'dims': false }, clbk, {} ); // $ExpectError + anyBy( x, { 'dims': {} }, clbk, {} ); // $ExpectError + anyBy( x, { 'dims': ( x: number ): number => x }, clbk, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid number of arguments... +{ + const x = zeros( [ 2, 2 ] ); + + anyBy(); // $ExpectError + anyBy( x ); // $ExpectError + anyBy( x, {}, clbk, {}, {} ); // $ExpectError +} + +// Attached to the function is an `assign` method which returns an ndarray... +{ + const x = zeros( [ 2, 2 ] ); + const y = empty( [], { + 'dtype': 'bool' + }); + + anyBy.assign( x, y, clbk ); // $ExpectType boolndarray + anyBy.assign( x, y, {}, clbk ); // $ExpectType boolndarray + anyBy.assign( x, y, { 'dims': [ 0 ] }, clbk ); // $ExpectType boolndarray + + anyBy.assign( x, y, clbk, {} ); // $ExpectType boolndarray + anyBy.assign( x, y, {}, clbk, {} ); // $ExpectType boolndarray + anyBy.assign( x, y, { 'dims': [ 0 ] }, clbk, {} ); // $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' + }); + + anyBy.assign( 5, y, clbk ); // $ExpectError + anyBy.assign( true, y, clbk ); // $ExpectError + anyBy.assign( false, y, clbk ); // $ExpectError + anyBy.assign( null, y, clbk ); // $ExpectError + anyBy.assign( undefined, y, clbk ); // $ExpectError + anyBy.assign( {}, y, clbk ); // $ExpectError + anyBy.assign( [ 1 ], y, clbk ); // $ExpectError + anyBy.assign( ( x: number ): number => x, y, clbk ); // $ExpectError + + anyBy.assign( 5, y, {}, clbk ); // $ExpectError + anyBy.assign( true, y, {}, clbk ); // $ExpectError + anyBy.assign( false, y, {}, clbk ); // $ExpectError + anyBy.assign( null, y, {}, clbk ); // $ExpectError + anyBy.assign( undefined, y, {}, clbk ); // $ExpectError + anyBy.assign( {}, y, {}, clbk ); // $ExpectError + anyBy.assign( [ 1 ], y, {}, clbk ); // $ExpectError + anyBy.assign( ( x: number ): number => x, y, {}, clbk ); // $ExpectError + + anyBy.assign( 5, y, clbk, {} ); // $ExpectError + anyBy.assign( true, y, clbk, {} ); // $ExpectError + anyBy.assign( false, y, clbk, {} ); // $ExpectError + anyBy.assign( null, y, clbk, {} ); // $ExpectError + anyBy.assign( undefined, y, clbk, {} ); // $ExpectError + anyBy.assign( {}, y, clbk, {} ); // $ExpectError + anyBy.assign( [ 1 ], y, clbk, {} ); // $ExpectError + anyBy.assign( ( x: number ): number => x, y, clbk, {} ); // $ExpectError + + anyBy.assign( 5, y, {}, clbk, {} ); // $ExpectError + anyBy.assign( true, y, {}, clbk, {} ); // $ExpectError + anyBy.assign( false, y, {}, clbk, {} ); // $ExpectError + anyBy.assign( null, y, {}, clbk, {} ); // $ExpectError + anyBy.assign( undefined, y, {}, clbk, {} ); // $ExpectError + anyBy.assign( {}, y, {}, clbk, {} ); // $ExpectError + anyBy.assign( [ 1 ], y, {}, clbk, {} ); // $ExpectError + anyBy.assign( ( x: number ): number => x, y, {}, clbk, {} ); // $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 ] ); + + anyBy.assign( x, 5, clbk ); // $ExpectError + anyBy.assign( x, true, clbk ); // $ExpectError + anyBy.assign( x, false, clbk ); // $ExpectError + anyBy.assign( x, null, clbk ); // $ExpectError + anyBy.assign( x, undefined, clbk ); // $ExpectError + anyBy.assign( x, {}, clbk ); // $ExpectError + anyBy.assign( x, [ 1 ], clbk ); // $ExpectError + anyBy.assign( x, ( x: number ): number => x, clbk ); // $ExpectError + + anyBy.assign( x, 5, {}, clbk ); // $ExpectError + anyBy.assign( x, true, {}, clbk ); // $ExpectError + anyBy.assign( x, false, {}, clbk ); // $ExpectError + anyBy.assign( x, null, {}, clbk ); // $ExpectError + anyBy.assign( x, undefined, {}, clbk ); // $ExpectError + anyBy.assign( x, {}, {}, clbk ); // $ExpectError + anyBy.assign( x, [ 1 ], {}, clbk ); // $ExpectError + anyBy.assign( x, ( x: number ): number => x, {}, clbk ); // $ExpectError + + anyBy.assign( x, 5, clbk, {} ); // $ExpectError + anyBy.assign( x, true, clbk, {} ); // $ExpectError + anyBy.assign( x, false, clbk, {} ); // $ExpectError + anyBy.assign( x, null, clbk, {} ); // $ExpectError + anyBy.assign( x, undefined, clbk, {} ); // $ExpectError + anyBy.assign( x, {}, clbk, {} ); // $ExpectError + anyBy.assign( x, [ 1 ], clbk, {} ); // $ExpectError + anyBy.assign( x, ( x: number ): number => x, clbk, {} ); // $ExpectError + + anyBy.assign( x, 5, {}, clbk, {} ); // $ExpectError + anyBy.assign( x, true, {}, clbk, {} ); // $ExpectError + anyBy.assign( x, false, {}, clbk, {} ); // $ExpectError + anyBy.assign( x, null, {}, clbk, {} ); // $ExpectError + anyBy.assign( x, undefined, {}, clbk, {} ); // $ExpectError + anyBy.assign( x, {}, {}, clbk, {} ); // $ExpectError + anyBy.assign( x, [ 1 ], {}, clbk, {} ); // $ExpectError + anyBy.assign( x, ( x: number ): number => x, {}, clbk, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided an options argument which is not an object... +{ + const x = zeros( [ 2, 2 ] ); + const y = empty( [], { + 'dtype': 'bool' + }); + + anyBy.assign( x, y, '5', clbk ); // $ExpectError + anyBy.assign( x, y, 5, clbk ); // $ExpectError + anyBy.assign( x, y, true, clbk ); // $ExpectError + anyBy.assign( x, y, false, clbk ); // $ExpectError + anyBy.assign( x, y, null, clbk ); // $ExpectError + anyBy.assign( x, y, [ 1 ], clbk ); // $ExpectError + anyBy.assign( x, y, ( x: number ): number => x, clbk ); // $ExpectError + + anyBy.assign( x, y, '5', clbk, {} ); // $ExpectError + anyBy.assign( x, y, 5, clbk, {} ); // $ExpectError + anyBy.assign( x, y, true, clbk, {} ); // $ExpectError + anyBy.assign( x, y, false, clbk, {} ); // $ExpectError + anyBy.assign( x, y, null, clbk, {} ); // $ExpectError + anyBy.assign( x, y, [ 1 ], clbk, {} ); // $ExpectError + anyBy.assign( x, y, ( x: number ): number => x, clbk, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a callback argument which is not a function... +{ + const x = zeros( [ 2, 2 ] ); + const y = empty( [], { + 'dtype': 'int32' + }); + + anyBy.assign( x, y, '5' ); // $ExpectError + anyBy.assign( x, y, 5 ); // $ExpectError + anyBy.assign( x, y, true ); // $ExpectError + anyBy.assign( x, y, false ); // $ExpectError + anyBy.assign( x, y, null ); // $ExpectError + anyBy.assign( x, y, [ 1 ] ); // $ExpectError + anyBy.assign( x, y, {} ); // $ExpectError + + anyBy.assign( x, y, '5', {} ); // $ExpectError + anyBy.assign( x, y, 5, {} ); // $ExpectError + anyBy.assign( x, y, true, {} ); // $ExpectError + anyBy.assign( x, y, false, {} ); // $ExpectError + anyBy.assign( x, y, null, {} ); // $ExpectError + anyBy.assign( x, y, [ 1 ], {} ); // $ExpectError + anyBy.assign( x, y, {}, {} ); // $ExpectError + + anyBy.assign( x, y, {}, '5' ); // $ExpectError + anyBy.assign( x, y, {}, 5 ); // $ExpectError + anyBy.assign( x, y, {}, true ); // $ExpectError + anyBy.assign( x, y, {}, false ); // $ExpectError + anyBy.assign( x, y, {}, null ); // $ExpectError + anyBy.assign( x, y, {}, [ 1 ] ); // $ExpectError + anyBy.assign( x, y, {}, {} ); // $ExpectError + + anyBy.assign( x, y, {}, '5', {} ); // $ExpectError + anyBy.assign( x, y, {}, 5, {} ); // $ExpectError + anyBy.assign( x, y, {}, true, {} ); // $ExpectError + anyBy.assign( x, y, {}, false, {} ); // $ExpectError + anyBy.assign( x, y, {}, null, {} ); // $ExpectError + anyBy.assign( x, y, {}, [ 1 ], {} ); // $ExpectError + anyBy.assign( x, y, {}, {}, {} ); // $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' + }); + + anyBy.assign( x, y, { 'dims': '5' }, clbk ); // $ExpectError + anyBy.assign( x, y, { 'dims': 5 }, clbk ); // $ExpectError + anyBy.assign( x, y, { 'dims': null }, clbk ); // $ExpectError + anyBy.assign( x, y, { 'dims': true }, clbk ); // $ExpectError + anyBy.assign( x, y, { 'dims': false }, clbk ); // $ExpectError + anyBy.assign( x, y, { 'dims': {} }, clbk ); // $ExpectError + anyBy.assign( x, y, { 'dims': ( x: number ): number => x }, clbk ); // $ExpectError + + anyBy.assign( x, y, { 'dims': '5' }, clbk, {} ); // $ExpectError + anyBy.assign( x, y, { 'dims': 5 }, clbk, {} ); // $ExpectError + anyBy.assign( x, y, { 'dims': null }, clbk, {} ); // $ExpectError + anyBy.assign( x, y, { 'dims': true }, clbk, {} ); // $ExpectError + anyBy.assign( x, y, { 'dims': false }, clbk, {} ); // $ExpectError + anyBy.assign( x, y, { 'dims': {} }, clbk, {} ); // $ExpectError + anyBy.assign( x, y, { 'dims': ( x: number ): number => x }, clbk, {} ); // $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' + }); + + anyBy.assign(); // $ExpectError + anyBy.assign( x ); // $ExpectError + anyBy.assign( x, y ); // $ExpectError + anyBy.assign( x, y, {}, clbk, {}, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/any-by/examples/index.js b/lib/node_modules/@stdlib/ndarray/any-by/examples/index.js new file mode 100644 index 000000000000..67ed653bf96f --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/any-by/examples/index.js @@ -0,0 +1,35 @@ +/** +* @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/base/discrete-uniform' ).factory; +var isEven = require( '@stdlib/assert/is-even' ).isPrimitive; +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var fillBy = require( '@stdlib/ndarray/fill-by' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var anyBy = require( './../lib' ); + +var x = zeros( [ 2, 4, 5 ], { + 'dtype': 'float64' +}); +x = fillBy( x, discreteUniform( 0, 10 ) ); +console.log( ndarray2array( x ) ); + +var y = anyBy( x, isEven ); +console.log( y.get() ); diff --git a/lib/node_modules/@stdlib/ndarray/any-by/lib/assign.js b/lib/node_modules/@stdlib/ndarray/any-by/lib/assign.js new file mode 100644 index 000000000000..5aa59b057486 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/any-by/lib/assign.js @@ -0,0 +1,161 @@ +/** +* @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 isFunction = require( '@stdlib/assert/is-function' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var unaryReduceSubarrayBy = require( '@stdlib/ndarray/base/unary-reduce-subarray-by' ); +var ndims = require( '@stdlib/ndarray/ndims' ); +var base = require( '@stdlib/ndarray/base/any-by' ); +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 passes a test implemented by a predicate function. +* +* @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 +* @param {Function} predicate - predicate function +* @param {*} [thisArg] - predicate execution context +* @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 {TypeError} callback argument must be a function +* @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' ); +* +* function isPositive( value ) { +* return value > 0.0; +* } +* +* // 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, isPositive ); +* // returns +* +* var bool = ( out === y ); +* // returns true +* +* var v = out.get(); +* // returns true +*/ +function assign( x, y, options, predicate, thisArg ) { + var nargs; + var opts; + var err; + var flg; + var ctx; + var cb; + var N; + var o; + + nargs = arguments.length; + 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 ) ); + } + // Case: assign( x, y, predicate ) + if ( nargs < 4 ) { + cb = options; + if ( !isFunction( cb ) ) { + throw new TypeError( format( 'invalid argument. Third argument must be a function. Value: `%s`.', cb ) ); + } + } + // Case: assign( x, y, options, predicate, thisArg ) + else if ( nargs > 4 ) { + flg = true; + o = options; + cb = predicate; + if ( !isFunction( cb ) ) { + throw new TypeError( format( 'invalid argument. Fourth argument must be a function. Value: `%s`.', cb ) ); + } + ctx = thisArg; + } + // Case: assign( x, y, predicate, thisArg ) + else if ( isFunction( options ) ) { + cb = options; + ctx = predicate; + } + // Case: assign( x, y, options, predicate ) + else if ( isFunction( predicate ) ) { + flg = true; + o = options; + cb = predicate; + } + // Case: assign( x, y, ???, ??? ) + else { + throw new TypeError( format( 'invalid argument. Fourth argument must be a function. Value: `%s`.', predicate ) ); + } + N = ndims( x ); + opts = objectAssign( {}, DEFAULTS ); + if ( flg ) { + err = validate( opts, N, o ); + 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 ); + } + // Perform the reduction: + unaryReduceSubarrayBy( base, [ x, y ], opts.dims, cb, ctx ); // 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-by/lib/defaults.json b/lib/node_modules/@stdlib/ndarray/any-by/lib/defaults.json new file mode 100644 index 000000000000..08433b373a0e --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/any-by/lib/defaults.json @@ -0,0 +1,4 @@ +{ + "dims": null, + "keepdims": false +} diff --git a/lib/node_modules/@stdlib/ndarray/any-by/lib/index.js b/lib/node_modules/@stdlib/ndarray/any-by/lib/index.js new file mode 100644 index 000000000000..e3d78f200281 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/any-by/lib/index.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'; + +/** +* Test whether at least one element along one or more ndarray dimensions passes a test implemented by a predicate function. +* +* @module @stdlib/ndarray/any-by +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var anyBy = require( '@stdlib/ndarray/any-by' ); +* +* function isPositive( value ) { +* return value > 0.0; +* } +* +* // 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 = anyBy( x, isPositive ); +* // 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 anyBy = require( '@stdlib/ndarray/any-by' ); +* +* function isPositive( value ) { +* return value > 0.0; +* } +* +* // 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 = anyBy.assign( x, y, isPositive ); +* // 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-by/lib/main.js b/lib/node_modules/@stdlib/ndarray/any-by/lib/main.js new file mode 100644 index 000000000000..c77897def246 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/any-by/lib/main.js @@ -0,0 +1,191 @@ +/** +* @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 isFunction = require( '@stdlib/assert/is-function' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var unaryReduceSubarrayBy = require( '@stdlib/ndarray/base/unary-reduce-subarray-by' ); +var base = require( '@stdlib/ndarray/base/any-by' ); +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 passes a test implemented by a predicate 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 {Function} predicate - predicate function +* @param {*} [thisArg] - predicate function execution context +* @throws {TypeError} first argument must be an ndarray-like object +* @throws {TypeError} options argument must be an object +* @throws {TypeError} callback argument must be a function +* @throws {RangeError} dimension indices must not exceed input ndarray bounds +* @throws {Error} dimension indices must be unique +* @throws {Error} must provide valid options +* @returns {ndarray} output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* function isPositive( value ) { +* return value > 0.0; +* } +* +* // 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 = anyBy( x, isPositive ); +* // returns +* +* var v = out.get(); +* // returns true +*/ +function anyBy( x, options, predicate, thisArg ) { + var nargs; + var opts; + var view; + var ctx; + var err; + var idx; + var shx; + var shy; + var ord; + var flg; + var cb; + var N; + var y; + var o; + + nargs = arguments.length; + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object. Value: `%s`.', x ) ); + } + // Case: anyBy( x, predicate ) + if ( nargs < 3 ) { + if ( !isFunction( options ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be a function. Value: `%s`.', options ) ); + } + cb = options; + } + // Case: anyBy( x, options, predicate, thisArg ) + else if ( nargs > 3 ) { + flg = true; + o = options; + cb = predicate; + if ( !isFunction( cb ) ) { + throw new TypeError( format( 'invalid argument. Third argument must be a function. Value: `%s`.', cb ) ); + } + ctx = thisArg; + } + // Case: anyBy( x, predicate, thisArg ) + else if ( isFunction( options ) ) { + cb = options; + ctx = predicate; + } + // Case: anyBy( x, options, predicate ) + else if ( isFunction( predicate ) ) { + flg = true; + o = options; + cb = predicate; + } + // Case: anyBy( x, ???, ??? ) + else { + throw new TypeError( format( 'invalid argument. Third argument must be a function. Value: `%s`.', predicate ) ); + } + shx = getShape( x ); + N = shx.length; + + opts = objectAssign( {}, DEFAULTS ); + if ( flg ) { + err = validate( opts, N, o ); + 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 ); + + // Resolve input array meta data: + ord = getOrder( x ); + + // 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': ord + }); + + // 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: + unaryReduceSubarrayBy( base, [ x, view ], opts.dims, cb, ctx ); + + // 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 = anyBy; diff --git a/lib/node_modules/@stdlib/ndarray/any-by/lib/validate.js b/lib/node_modules/@stdlib/ndarray/any-by/lib/validate.js new file mode 100644 index 000000000000..b7985b72461f --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/any-by/lib/validate.js @@ -0,0 +1,87 @@ +/** +* @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, ',' ) ) ); + } + opts.dims = tmp; + } + return null; +} + + +// EXPORTS // + +module.exports = validate; diff --git a/lib/node_modules/@stdlib/ndarray/any-by/package.json b/lib/node_modules/@stdlib/ndarray/any-by/package.json new file mode 100644 index 000000000000..e53197b076b9 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/any-by/package.json @@ -0,0 +1,65 @@ +{ + "name": "@stdlib/ndarray/any-by", + "version": "0.0.0", + "description": "Test whether at least one element along one or more ndarray dimensions passes a test implemented by a predicate function.", + "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", + "some", + "utils", + "truthy", + "reduce", + "reduction" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/ndarray/any-by/test/test.assign.js b/lib/node_modules/@stdlib/ndarray/any-by/test/test.assign.js new file mode 100644 index 000000000000..cce075493e1d --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/any-by/test/test.assign.js @@ -0,0 +1,937 @@ +/** +* @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' ); + + +// FUNCTIONS // + +/** +* Callback function. +* +* @private +* @param {number} value - input value +* @returns {boolean} result +*/ +function clbk( value ) { + return value > 0.0; +} + + +// 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, clbk ); + }; + } +}); + +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, {}, clbk ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (thisArg)', 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, clbk, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (options, thisArg)', 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, {}, clbk, {} ); + }; + } +}); + +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, clbk ); + }; + } +}); + +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, {}, clbk ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not an ndarray-like object (thisArg)', 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, clbk, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not an ndarray-like object (options, thisArg)', 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, {}, clbk, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a callback argument which is not a function', function test( t ) { + var values; + var x; + var y; + var i; + + x = empty( [ 2, 2 ], { + 'dtype': 'float64' + }); + y = empty( [], { + '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 a callback argument which is not a function (options)', 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 a callback argument which is not a function (thisArg)', 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 a callback argument which is not a function (options, thisArg)', 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 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, clbk ); + }; + } +}); + +tape( 'the function throws an error if provided an options argument which is not an object (thisArg)', 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, + [], + 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, y, value, clbk, {} ); + }; + } +}); + +tape( 'the function throws an error if provided an options with an 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, + {}, + 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() { + var opts = { + 'dims': value + }; + assign( x, y, opts, clbk ); + }; + } +}); + +tape( 'the function throws an error if provided an options with a `dims` property containing out-of-bounds dimension indices', function test( t ) { + var values; + var x; + var y; + var i; + + x = empty( [ 2, 2 ], { + 'dtype': 'float64' + }); + y = empty( [ 2 ], { + 'dtype': 'bool' + }); + + 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 + }; + assign( x, y, opts, clbk ); + }; + } +}); + +tape( 'the function throws an error if provided an options with a `dims` property which contains duplicate dimensions', function test( t ) { + var values; + var x; + var y; + var i; + + x = empty( [ 2, 2 ], { + 'dtype': 'float64' + }); + y = empty( [ 2 ], { + 'dtype': 'bool' + }); + + 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 + }; + assign( x, y, opts, clbk ); + }; + } +}); + +tape( 'the function tests whether at least one element along one or more ndarray dimensions passes a test implemented by a predicate function (row-major)', 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, clbk ); + expected = true; + + t.strictEqual( actual, y, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + 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, clbk ); + expected = false; + + 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 passes a test implemented by a predicate function (column-major)', 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, clbk ); + expected = true; + + t.strictEqual( actual, y, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + 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, clbk ); + expected = false; + + t.strictEqual( actual, y, 'returns expected value' ); + 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; + var x; + var y; + + x = new ndarray( 'float64', new Float64Array( [ 1.0, -2.0, 3.0, -4.0, -5.0, -6.0, -7.0, -8.0 ] ), [ 2, 4 ], [ 4, 1 ], 0, 'row-major' ); + + opts = { + 'dims': [ 0 ] + }; + y = empty( [ 4 ], { + 'dtype': 'bool' + }); + actual = assign( x, y, opts, clbk ); + expected = [ true, false, true, false ]; + t.strictEqual( actual, y, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + opts = { + 'dims': [ 1 ] + }; + y = empty( [ 2 ], { + 'dtype': 'bool' + }); + actual = assign( x, y, opts, clbk ); + expected = [ true, false ]; + t.strictEqual( actual, y, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + opts = { + 'dims': [ 0, 1 ] + }; + y = empty( [], { + 'dtype': 'bool' + }); + actual = assign( x, y, opts, clbk ); + expected = true; + t.strictEqual( actual, y, 'returns expected value' ); + t.deepEqual( actual.get(), expected, 'returns expected value' ); + + opts = { + 'dims': [] + }; + y = empty( [ 2, 4 ], { + 'dtype': 'bool' + }); + actual = assign( x, y, opts, clbk ); + expected = [ [ true, false, true, false ], [ false, false, false, false ] ]; + t.strictEqual( actual, y, '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 opts; + var x; + var y; + + x = new ndarray( 'float64', new Float64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0, 7.0, -8.0 ] ), [ 2, 4 ], [ 1, 2 ], 0, 'column-major' ); + + opts = { + 'dims': [ 0 ] + }; + y = empty( [ 4 ], { + 'dtype': 'bool' + }); + actual = assign( x, y, opts, clbk ); + expected = [ true, true, true, true ]; + + t.strictEqual( actual, y, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + opts = { + 'dims': [ 1 ] + }; + y = empty( [ 2 ], { + 'dtype': 'bool' + }); + actual = assign( x, y, opts, clbk ); + expected = [ true, false ]; + + t.strictEqual( actual, y, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + opts = { + 'dims': [ 0, 1 ] + }; + y = empty( [], { + 'dtype': 'bool' + }); + actual = assign( x, y, opts, clbk ); + expected = true; + + t.strictEqual( actual, y, 'returns expected value' ); + t.deepEqual( actual.get(), expected, 'returns expected value' ); + + opts = { + 'dims': [] + }; + y = empty( [ 2, 4 ], { + 'dtype': 'bool' + }); + actual = assign( x, y, opts, clbk ); + expected = [ [ true, true, true, true ], [ false, false, false, false ] ]; + + t.strictEqual( actual, y, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing an execution context', function test( t ) { + var expected; + var indices; + var values; + var arrays; + var actual; + var ctx; + 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' + }); + + ctx = { + 'count': 0 + }; + + indices = []; + values = []; + arrays = []; + actual = assign( x, y, predicate, ctx ); + + expected = true; + t.strictEqual( actual, y, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + t.strictEqual( ctx.count, 4, 'returns expected value' ); + + expected = [ -1.0, -2.0, -3.0, 4.0 ]; + t.deepEqual( values, expected, 'returns expected value' ); + + expected = [ + [ 0 ], + [ 1 ], + [ 2 ], + [ 3 ] + ]; + t.deepEqual( indices, expected, 'returns expected value' ); + + expected = [ x, x, x, x ]; + t.deepEqual( arrays, expected, 'returns expected value' ); + + t.end(); + + function predicate( value, idx, arr ) { + this.count += 1; // eslint-disable-line no-invalid-this + values.push( value ); + indices.push( idx ); + arrays.push( arr ); + return value > 0.0; + } +}); + +tape( 'the function supports providing an execution context (options)', function test( t ) { + var expected; + var indices; + var values; + var arrays; + var actual; + var opts; + var ctx; + var x; + var y; + + x = new ndarray( 'float64', new Float64Array( [ -1.0, -2.0, -3.0, 4.0 ] ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + y = empty( [ 2 ], { + 'dtype': 'bool' + }); + + ctx = { + 'count': 0 + }; + opts = { + 'dims': [ 1 ] + }; + indices = []; + values = []; + arrays = []; + actual = assign( x, y, opts, predicate, ctx ); + + expected = [ false, true ]; + t.strictEqual( actual, y, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + t.strictEqual( ctx.count, 4, 'returns expected value' ); + + expected = [ -1.0, -2.0, -3.0, 4.0 ]; + t.deepEqual( values, expected, 'returns expected value' ); + + expected = [ + [ 0, 0 ], + [ 0, 1 ], + [ 1, 0 ], + [ 1, 1 ] + ]; + t.deepEqual( indices, expected, 'returns expected value' ); + + expected = [ x, x, x, x ]; + t.deepEqual( arrays, expected, 'returns expected value' ); + + t.end(); + + function predicate( value, idx, arr ) { + this.count += 1; // eslint-disable-line no-invalid-this + values.push( value ); + indices.push( idx ); + arrays.push( arr ); + return value > 0.0; + } +}); diff --git a/lib/node_modules/@stdlib/ndarray/any-by/test/test.js b/lib/node_modules/@stdlib/ndarray/any-by/test/test.js new file mode 100644 index 000000000000..24d357ef0102 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/any-by/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 anyBy = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof anyBy, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is an `assign` method', function test( t ) { + t.strictEqual( isMethod( anyBy, 'assign' ), true, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/any-by/test/test.main.js b/lib/node_modules/@stdlib/ndarray/any-by/test/test.main.js new file mode 100644 index 000000000000..b99c9d3b7364 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/any-by/test/test.main.js @@ -0,0 +1,831 @@ +/** +* @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 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 anyBy = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Callback function. +* +* @private +* @param {number} value - input value +* @returns {boolean} result +*/ +function clbk( value ) { + return value > 0.0; +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof anyBy, '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() { + anyBy( value, clbk ); + }; + } +}); + +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() { + anyBy( value, {}, clbk ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (thisArg)', 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() { + anyBy( value, clbk, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (options, thisArg)', 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() { + anyBy( value, {}, clbk, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a callback argument which is not a function', 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() { + anyBy( x, value ); + }; + } +}); + +tape( 'the function throws an error if provided a callback argument which is not a function (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, + {}, + [] + ]; + + 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() { + anyBy( x, {}, value ); + }; + } +}); + +tape( 'the function throws an error if provided a callback argument which is not a function (thisArg)', 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() { + anyBy( x, value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a callback argument which is not a function (options, thisArg)', 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() { + anyBy( 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 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() { + anyBy( x, value, clbk ); + }; + } +}); + +tape( 'the function throws an error if provided an options argument which is not an object (thisArg)', 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() { + anyBy( x, value, clbk, {} ); + }; + } +}); + +tape( 'the function throws an error if provided an options with an 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, + {}, + 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() { + var opts = { + 'dims': value + }; + anyBy( x, opts, clbk ); + }; + } +}); + +tape( 'the function throws an error if provided an options with a `dims` property containing out-of-bounds dimension indices', 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 + }; + anyBy( x, opts, clbk ); + }; + } +}); + +tape( 'the function throws an error if provided an options with a `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 + }; + anyBy( x, opts, clbk ); + }; + } +}); + +tape( 'the function throws an error if provided an options with a `dims` property which contains dimensions more than the 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 + }; + anyBy( x, opts, clbk ); + }; + } +}); + +tape( 'the function throws an error if provided an options with an 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, + {}, + [], + 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() { + var opts = { + 'keepdims': value + }; + anyBy( x, opts, clbk ); + }; + } +}); + +tape( 'the function tests whether at least one element along one or more ndarray dimensions passes a test implemented by a predicate function (row-major)', 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 = anyBy( x, clbk ); + expected = true; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + x = new ndarray( 'float64', new Float64Array( [ -1.0, -2.0, -3.0, -4.0 ] ), [ 4 ], [ 1 ], 0, 'row-major' ); + + actual = anyBy( x, clbk ); + expected = false; + + t.strictEqual( isndarrayLike( actual ), true, '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 passes a test implemented by a predicate function (column-major)', 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, 'column-major' ); + + actual = anyBy( x, clbk ); + expected = true; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + x = new ndarray( 'float64', new Float64Array( [ -1.0, -2.0, -3.0, -4.0 ] ), [ 4 ], [ 1 ], 0, 'column-major' ); + + actual = anyBy( x, clbk ); + expected = false; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + 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; + var x; + + x = new ndarray( 'float64', new Float64Array( [ 1.0, -2.0, 3.0, -4.0, -5.0, -6.0, -7.0, -8.0 ] ), [ 2, 4 ], [ 4, 1 ], 0, 'row-major' ); + + opts = { + 'dims': [ 0 ] + }; + actual = anyBy( x, opts, clbk ); + expected = [ true, false, true, false ]; + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + opts = { + 'dims': [ 0 ], + 'keepdims': true + }; + actual = anyBy( x, opts, clbk ); + expected = [ [ true, false, true, false ] ]; + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + opts = { + 'dims': [ 1 ] + }; + actual = anyBy( x, opts, clbk ); + expected = [ true, false ]; + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + opts = { + 'dims': [ 1 ], + 'keepdims': true + }; + actual = anyBy( x, opts, clbk ); + expected = [ [ true ], [ false ] ]; + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + opts = { + 'dims': [ 0, 1 ] + }; + actual = anyBy( x, opts, clbk ); + expected = true; + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( actual.get(), expected, 'returns expected value' ); + + opts = { + 'dims': [ 0, 1 ], + 'keepdims': true + }; + actual = anyBy( x, opts, clbk ); + expected = [ [ true ] ]; + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + opts = { + 'dims': [] + }; + actual = anyBy( x, opts, clbk ); + expected = [ [ true, false, true, false ], [ false, false, false, false ] ]; + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + opts = { + 'dims': [], + 'keepdims': true + }; + actual = anyBy( x, opts, clbk ); + expected = [ [ true, false, true, false ], [ false, false, false, false ] ]; + t.strictEqual( isndarrayLike( actual ), true, '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 opts; + var x; + + x = new ndarray( 'float64', new Float64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0, 7.0, -8.0 ] ), [ 2, 4 ], [ 1, 2 ], 0, 'column-major' ); + + opts = { + 'dims': [ 0 ] + }; + actual = anyBy( x, opts, clbk ); + expected = [ true, true, true, true ]; + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + opts = { + 'dims': [ 0 ], + 'keepdims': true + }; + actual = anyBy( x, opts, clbk ); + expected = [ [ true, true, true, true ] ]; + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + opts = { + 'dims': [ 1 ] + }; + actual = anyBy( x, opts, clbk ); + expected = [ true, false ]; + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + opts = { + 'dims': [ 1 ], + 'keepdims': true + }; + actual = anyBy( x, opts, clbk ); + expected = [ [ true ], [ false ] ]; + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + opts = { + 'dims': [ 0, 1 ] + }; + actual = anyBy( x, opts, clbk ); + expected = true; + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( actual.get(), expected, 'returns expected value' ); + + opts = { + 'dims': [ 0, 1 ], + 'keepdims': true + }; + actual = anyBy( x, opts, clbk ); + expected = [ [ true ] ]; + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + opts = { + 'dims': [] + }; + actual = anyBy( x, opts, clbk ); + expected = [ [ true, true, true, true ], [ false, false, false, false ] ]; + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + opts = { + 'dims': [], + 'keepdims': true + }; + actual = anyBy( x, opts, clbk ); + expected = [ [ true, true, true, true ], [ false, false, false, false ] ]; + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing an execution context', function test( t ) { + var expected; + var indices; + var values; + var arrays; + var actual; + var ctx; + var x; + + x = new ndarray( 'float64', new Float64Array( [ -1.0, -2.0, -3.0, 4.0 ] ), [ 4 ], [ 1 ], 0, 'row-major' ); + + ctx = { + 'count': 0 + }; + + indices = []; + values = []; + arrays = []; + actual = anyBy( x, predicate, ctx ); + expected = true; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + t.strictEqual( ctx.count, 4, 'returns expected value' ); + + expected = [ -1.0, -2.0, -3.0, 4.0 ]; + t.deepEqual( values, expected, 'returns expected value' ); + + expected = [ + [ 0 ], + [ 1 ], + [ 2 ], + [ 3 ] + ]; + t.deepEqual( indices, expected, 'returns expected value' ); + + expected = [ x, x, x, x ]; + t.deepEqual( arrays, expected, 'returns expected value' ); + + t.end(); + + function predicate( value, idx, arr ) { + this.count += 1; // eslint-disable-line no-invalid-this + values.push( value ); + indices.push( idx ); + arrays.push( arr ); + return value > 0.0; + } +}); + +tape( 'the function supports providing an execution context (options)', function test( t ) { + var expected; + var indices; + var values; + var arrays; + var actual; + var opts; + var ctx; + var x; + + x = new ndarray( 'float64', new Float64Array( [ -1.0, -2.0, -3.0, 4.0 ] ), [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + ctx = { + 'count': 0 + }; + opts = { + 'dims': [ 0 ] + }; + indices = []; + values = []; + arrays = []; + actual = anyBy( x, opts, predicate, ctx ); + expected = [ false, true ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + t.strictEqual( ctx.count, 4, 'returns expected value' ); + + expected = [ -1.0, -2.0, -3.0, 4.0 ]; + t.deepEqual( values, expected, 'returns expected value' ); + + expected = [ + [ 0, 0 ], + [ 1, 0 ], + [ 0, 1 ], + [ 1, 1 ] + ]; + t.deepEqual( indices, expected, 'returns expected value' ); + + expected = [ x, x, x, x ]; + t.deepEqual( arrays, expected, 'returns expected value' ); + + t.end(); + + function predicate( value, idx, arr ) { + this.count += 1; // eslint-disable-line no-invalid-this + values.push( value ); + indices.push( idx ); + arrays.push( arr ); + return value > 0.0; + } +});