From 9a289440b28ecd136324fe88e2aa3d2975a0c40c Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Sat, 31 May 2025 13:10:04 +0000 Subject: [PATCH 1/7] feat: add ndarray/some-by --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../@stdlib/ndarray/some-by/README.md | 388 ++++++++++++++++++ .../ndarray/some-by/benchmark/benchmark.1d.js | 141 +++++++ .../ndarray/some-by/benchmark/benchmark.2d.js | 155 +++++++ .../@stdlib/ndarray/some-by/docs/repl.txt | 121 ++++++ .../ndarray/some-by/docs/types/index.d.ts | 344 ++++++++++++++++ .../ndarray/some-by/docs/types/test.ts | 387 +++++++++++++++++ .../@stdlib/ndarray/some-by/examples/index.js | 39 ++ .../@stdlib/ndarray/some-by/lib/assign.js | 205 +++++++++ .../@stdlib/ndarray/some-by/lib/defaults.json | 4 + .../@stdlib/ndarray/some-by/lib/index.js | 103 +++++ .../@stdlib/ndarray/some-by/lib/main.js | 231 +++++++++++ .../@stdlib/ndarray/some-by/lib/validate.js | 90 ++++ .../@stdlib/ndarray/some-by/package.json | 66 +++ .../@stdlib/ndarray/some-by/test/test.js | 39 ++ 14 files changed, 2313 insertions(+) create mode 100644 lib/node_modules/@stdlib/ndarray/some-by/README.md create mode 100644 lib/node_modules/@stdlib/ndarray/some-by/benchmark/benchmark.1d.js create mode 100644 lib/node_modules/@stdlib/ndarray/some-by/benchmark/benchmark.2d.js create mode 100644 lib/node_modules/@stdlib/ndarray/some-by/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/ndarray/some-by/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/ndarray/some-by/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/ndarray/some-by/examples/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/some-by/lib/assign.js create mode 100644 lib/node_modules/@stdlib/ndarray/some-by/lib/defaults.json create mode 100644 lib/node_modules/@stdlib/ndarray/some-by/lib/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/some-by/lib/main.js create mode 100644 lib/node_modules/@stdlib/ndarray/some-by/lib/validate.js create mode 100644 lib/node_modules/@stdlib/ndarray/some-by/package.json create mode 100644 lib/node_modules/@stdlib/ndarray/some-by/test/test.js diff --git a/lib/node_modules/@stdlib/ndarray/some-by/README.md b/lib/node_modules/@stdlib/ndarray/some-by/README.md new file mode 100644 index 000000000000..bd4dd09a6e76 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/some-by/README.md @@ -0,0 +1,388 @@ + + +# someBy + +> Test whether at least `n` elements in an [`ndarray`][@stdlib/ndarray/ctor] pass a test implemented by a predicate function along one or more dimensions. + +
+ +
+ + + +
+ +## Usage + +```javascript +var someBy = require( '@stdlib/ndarray/someBy' ); +``` + +#### someBy( x, n\[, options], predicate\[, thisArg] ) + +Tests whether at least `n` elements in an [`ndarray`][@stdlib/ndarray/ctor] pass a test implemented by a predicate function along one or more dimensions. + + + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); + +function predicate( 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 = someBy( x, 2, predicate ); +// returns + +console.log( out.get() ); +// => true +``` + +The function accepts the following arguments: + +- **x**: input [`ndarray`][@stdlib/ndarray/ctor]. +- **n**: number of elements which must pass the test implemented by a predicate function. May be either a scalar or an [`ndarray`][@stdlib/ndarray/ctor]. +- **options**: function options (_optional_). +- **predicate**: predicate function. +- **thisArg**: predicate 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 Float64Array = require( '@stdlib/array/float64' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); + +function predicate( 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' ); + +var opts = { + 'dims': [ 0, 1 ] +}; + +// Perform reduction: +var out = someBy( x, 2, opts, predicate ); +// returns + +var v = ndarray2array( out ); +// returns [ true, true ] +``` + +By default, the function returns an [`ndarray`][@stdlib/ndarray/ctor] having a shape matching only the non-reduced dimensions of the input [`ndarray`][@stdlib/ndarray/ctor] (i.e., the reduced dimensions are dropped). To include the reduced dimensions as singleton dimensions in the output [`ndarray`][@stdlib/ndarray/ctor], set the `keepdims` option to `true`. + + + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); + +function predicate( 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' ); + +var opts = { + 'dims': [ 0, 1 ], + 'keepdims': true +}; + +// Perform reduction: +var out = someBy( x, 2, opts, predicate ); +// returns + +var v = ndarray2array( out ); +// returns [[[ true, true ]]] +``` + +To set the predicate function execution context, provide a `thisArg`. + + + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); + +function predicate( value ) { + this.count += 1; + 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' ); + +// Create a context object: +var ctx = { + 'count': 0 +}; + +// Perform operation: +var out = someBy( x, 2, predicate, ctx ); +// returns + +var v = out.get(); +// returns true + +var count = ctx.count; +// returns 6 +``` + +#### someBy.assign( x, n, out\[, options], predicate\[, thisArg] ) + +Test whether at least `n` elements in an [`ndarray`][@stdlib/ndarray/ctor] pass a test implemented by a predicate function along one or more dimensions and assigns results to a provided output [`ndarray`][@stdlib/ndarray/ctor]. + + + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var empty = require( '@stdlib/ndarray/empty' ); + +function predicate( 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' ); + +// Create an output ndarray: +var y = empty( [], { + 'dtype': 'bool' +}); + +// Perform reduction: +var out = someBy.assign( x, 2, y, predicate ); +// returns + +var bool = ( out === y ); +// returns true + +var v = y.get(); +// returns true +``` + +The function accepts the following arguments: + +- **x**: input [`ndarray`][@stdlib/ndarray/ctor]. +- **n**: number of elements which must pass the test implemented by a predicate function. May be either a scalar or an [`ndarray`][@stdlib/ndarray/ctor]. +- **out**: output [`ndarray`][@stdlib/ndarray/ctor]. The output [`ndarray`][@stdlib/ndarray/ctor] must have a shape matching the non-reduced dimensions of the input [`ndarray`][@stdlib/ndarray/ctor]. +- **options**: function options (_optional_). +- **predicate**: predicate function. +- **thisArg**: predicate 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 Float64Array = require( '@stdlib/array/float64' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var empty = require( '@stdlib/ndarray/empty' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); + +function predicate( 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' ); + +// Create an output ndarray: +var y = empty( [ 3 ], { + 'dtype': 'bool' +}); + +var opts = { + 'dims': [ 0, 1 ] +}; + +// Perform reduction: +var out = someBy.assign( x, 2, y, opts, predicate ); + +var bool = ( out === y ); +// returns true + +var v = ndarray2array( y ); +// returns [ true, true ] +``` + +
+ + + +
+ +## 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 scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var fillBy = require( '@stdlib/ndarray/fill-by' ); +var zeros = require( '@stdlib/ndarray/zeros' ); + +var x = zeros( [ 2, 4, 5 ], { + 'dtype': 'float64' +}); +x = fillBy( x, discreteUniform( 0, 10 ) ); +console.log( ndarray2array( x ) ); + +var n = scalar2ndarray( 4, { + 'dtype': 'int8' +}); +var y = someBy( x, n, isEven ); +console.log( y.get() ); +``` + +
+ + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/some-by/benchmark/benchmark.1d.js b/lib/node_modules/@stdlib/ndarray/some-by/benchmark/benchmark.1d.js new file mode 100644 index 000000000000..7595e65130a5 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/some-by/benchmark/benchmark.1d.js @@ -0,0 +1,141 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var 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 isEven = require( '@stdlib/assert/is-even' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var someBy = require( './../lib' ); + + +// VARIABLES // + +var types = [ 'float64' ]; +var orders = [ 'row-major', 'column-major' ]; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - ndarray length +* @param {NonNegativeIntegerArray} shape - ndarray shape +* @param {string} xtype - ndarray data type +* @param {string} order - memory layout +* @param {NonNegativeIntegerArray} dims - list of dimensions to reduce +* @returns {Function} benchmark function +*/ +function createBenchmark( len, shape, xtype, order, dims ) { + var x; + + x = discreteUniform( len, 1, 100 ); + x = new ndarray( xtype, x, shape, shape2strides( shape, order ), 0, order ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var opts; + var out; + var i; + + opts = { + 'dims': dims + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = someBy( x, len, opts, isEven ); + 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/some-by/benchmark/benchmark.2d.js b/lib/node_modules/@stdlib/ndarray/some-by/benchmark/benchmark.2d.js new file mode 100644 index 000000000000..4fe00d34c721 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/some-by/benchmark/benchmark.2d.js @@ -0,0 +1,155 @@ +/** +* @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 isEven = require( '@stdlib/assert/is-even' ).isPrimitive; +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 // + +/** +* 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, len, opts, isEven ); + 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/some-by/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/some-by/docs/repl.txt new file mode 100644 index 000000000000..5253e1418115 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/some-by/docs/repl.txt @@ -0,0 +1,121 @@ + +{{alias}}( x, n[, options], predicate[, thisArg] ) + Tests whether at least `n` elements in an ndarray pass a test implemented by + a predicate function along one or more dimensions. + + Parameters + ---------- + x: ndarray + Input ndarray. + + n: ndarray|any + Number of elements which must pass the test implemented by a predicate + function. + + 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 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 xbuf = new {{alias:@stdlib/array/float64}}( [ 1.0, 2.0, 3.0, 4.0 ] ); + > var dt = 'float64'; + > var sh = [ 2, 2 ]; + > var sx = [ 2, 1 ]; + > var ox = 0; + > var ord = 'row-major'; + > var x = {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, sx, ox, ord ); + > function f ( v ) { return v > 0.0; }; + > var y = {{alias}}( x, 3, f ) + + > y.get() + true + > y = {{alias}}( x, 3, { 'keepdims': true }, f ) + + > {{alias:@stdlib/ndarray/to-array}}( y ) + [ [ true ] ] + > y.get( 0, 0 ) + true + + +{{alias}}.assign( x, n, y[, options], predicate[, thisArg] ) + Tests whether at least `n` elements in an ndarray pass a test implemented by + a predicate function along one or more dimensions. + + Parameters + ---------- + x: ndarray + Input ndarray. + + n: ndarray|any + Number of elements which must pass the test implemented by a predicate + function. + + 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. + + 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 execution context. + + Returns + ------- + y: ndarray + Output ndarray. + + Examples + -------- + > var xbuf = new {{alias:@stdlib/array/float64}}( [ 1.0, 2.0, 3.0, 4.0 ] ); + > var dt = 'float64'; + > var sh = [ 2, 2 ]; + > var sx = [ 2, 1 ]; + > var ox = 0; + > var ord = 'row-major'; + > var x = {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, sx, ox, ord ); + > var y = {{alias:@stdlib/ndarray/from-scalar}}( false ); + > function f ( v ) { return v > 0.0 }; + > var out = {{alias}}.assign( x, 3, y, f ) + + > var bool = ( out === y ) + true + > y.get() + true + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/ndarray/some-by/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/some-by/docs/types/index.d.ts new file mode 100644 index 000000000000..8aef14c2f8ac --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/some-by/docs/types/index.d.ts @@ -0,0 +1,344 @@ +/* +* @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'; + +/** +* 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: typedndarray ) => 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 `someBy`. +*/ +interface SomeBy { + /** + * Tests whether at least `n` elements in an ndarray pass a test implemented by a predicate function along one or more dimensions. + * + * @param x - input ndarray + * @param n - number of elements which must pass a test + * @param predicate - predicate function + * @param thisArg - predicate execution context + * @returns output ndarray + * + * @example + * var Float64Array = require( '@stdlib/array/float64' ); + * var isEven = require( '@stdlib/assert/is-even' ).isPrimitive; + * var ndarray = require( '@stdlib/ndarray/ctor' ); + * + * // Create a data buffer: + * var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); + * + * // Define the shape of the input array: + * var sh = [ 3, 1, 2 ]; + * + * // Define the array strides: + * var sx = [ 4, 4, 1 ]; + * + * // Define the index offset: + * var ox = 1; + * + * // Create an input ndarray: + * var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); + * + * // Perform reduction: + * var out = someBy( x, 3, isEven ); + * // returns + * + * var v = out.get(); + * // returns true + */ + ( x: ndarray, n: ndarray | unknown, predicate: Predicate, thisArg?: ThisParameterType> ): boolndarray; + + /** + * Tests whether at least `n` elements in an ndarray pass a test implemented by a predicate function along one or more dimensions. + * + * @param x - input ndarray + * @param n - number of elements which must pass a test + * @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 isEven = require( '@stdlib/assert/is-even' ).isPrimitive; + * var ndarray = require( '@stdlib/ndarray/ctor' ); + * + * // Create a data buffer: + * var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); + * + * // Define the shape of the input array: + * var sh = [ 3, 1, 2 ]; + * + * // Define the array strides: + * var sx = [ 4, 4, 1 ]; + * + * // Define the index offset: + * var ox = 1; + * + * // Create an input ndarray: + * var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); + * + * // Perform reduction: + * var out = someBy( x, 3, {}, isEven ); + * // returns + * + * var v = out.get(); + * // returns true + */ + ( x: ndarray, n: ndarray | unknown, options: Options, predicate: Predicate, thisArg?: ThisParameterType> ): boolndarray; + + /** + * Tests whether at least `n` elements in an ndarray pass a test implemented by a predicate function along one or more dimensions. + * + * @param x - input ndarray + * @param n - number of elements which must pass a test + * @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 isEven = require( '@stdlib/assert/is-even' ).isPrimitive; + * var empty = require( '@stdlib/ndarray/empty' ); + * + * // Create a data buffer: + * var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); + * + * // Define the shape of the input array: + * var shape = [ 3, 1, 2 ]; + * + * // Define the array strides: + * var sx = [ 4, 4, 1 ]; + * + * // Define the index offset: + * var ox = 1; + * + * // Create an input ndarray: + * var x = new ndarray( 'float64', xbuf, shape, sx, ox, 'row-major' ); + * + * // Create an output ndarray: + * var y = empty( [], { + * 'dtype': 'bool' + * }); + * + * // Perform reduction: + * var out = someBy.assign( x, 3, y, isEven ); + * // returns + * + * var v = out.get(); + * // returns true + */ + assign( x: ndarray, n: ndarray | unknown, y: ndarray, predicate: Predicate, thisArg?: ThisParameterType> ): boolndarray; + + /** + * Tests whether at least `n` elements in an ndarray pass a test implemented by a predicate function along one or more dimensions. + * + * @param x - input ndarray + * @param n - number of elements which must pass a test + * @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 isEven = require( '@stdlib/assert/is-even' ).isPrimitive; + * var empty = require( '@stdlib/ndarray/empty' ); + * + * // Create a data buffer: + * var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); + * + * // Define the shape of the input array: + * var shape = [ 3, 1, 2 ]; + * + * // Define the array strides: + * var sx = [ 4, 4, 1 ]; + * + * // Define the index offset: + * var ox = 1; + * + * // Create an input ndarray: + * var x = new ndarray( 'float64', xbuf, shape, sx, ox, 'row-major' ); + * + * // Create an output ndarray: + * var y = empty( [], { + * 'dtype': 'bool' + * }); + * + * // Perform reduction: + * var out = someBy.assign( x, 3, y, {}, isEven ); + * // returns + * + * var v = out.get(); + * // returns true + */ + assign( x: ndarray, n: ndarray | unknown, y: ndarray, options: BaseOptions, predicate: Predicate, thisArg?: ThisParameterType> ): boolndarray; +} + +/** +* Tests whether at least `n` elements in an ndarray pass a test implemented by a predicate function along one or more dimensions. +* +* @param x - input ndarray +* @param n - number of elements which must pass a test +* @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 isEven = require( '@stdlib/assert/is-even' ).isPrimitive; +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* // Create a data buffer: +* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* +* // Define the shape of the input array: +* var sh = [ 3, 1, 2 ]; +* +* // Define the array strides: +* var sx = [ 4, 4, 1 ]; +* +* // Define the index offset: +* var ox = 1; +* +* // Create an input ndarray: +* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); +* +* // Perform reduction: +* var out = someBy( x, 3, isEven ); +* // returns +* +* var v = out.get(); +* // returns true +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var isEven = require( '@stdlib/assert/is-even' ).isPrimitive; +* var empty = require( '@stdlib/ndarray/empty' ); +* +* // Create a data buffer: +* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* +* // Define the shape of the input array: +* var shape = [ 3, 1, 2 ]; +* +* // Define the array strides: +* var sx = [ 4, 4, 1 ]; +* +* // Define the index offset: +* var ox = 1; +* +* // Create an input ndarray: +* var x = new ndarray( 'float64', xbuf, shape, sx, ox, 'row-major' ); +* +* // Create an output ndarray: +* var y = empty( [], { +* 'dtype': 'bool' +* }); +* +* // Perform reduction: +* var out = someBy.assign( x, 3, y, isEven ); +* // returns +* +* var v = out.get(); +* // returns true +*/ +declare var someBy: SomeBy; + + +// EXPORTS // + +export = someBy; + diff --git a/lib/node_modules/@stdlib/ndarray/some-by/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/some-by/docs/types/test.ts new file mode 100644 index 000000000000..2561c88371ec --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/some-by/docs/types/test.ts @@ -0,0 +1,387 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/// + +import zeros = require( '@stdlib/ndarray/zeros' ); +import empty = require( '@stdlib/ndarray/empty' ); +import someBy = require( './index' ); + + +/** +* Predicate function. +* +* @param v - ndarray element +* @returns result +*/ +function clbk( v: any ): boolean { + return v > 0.0; +} + + +// TESTS // + +// The function returns an ndarray... +{ + const x = zeros( [ 2, 2 ] ); + + someBy( x, 2, clbk ); // $ExpectType boolndarray + someBy( x, 2, { 'keepdims': true }, clbk ); // $ExpectType boolndarray + someBy( x, 2, { 'dims': [ 0 ] }, clbk ); // $ExpectType boolndarray + + someBy( x, 2, clbk, {} ); // $ExpectType boolndarray + someBy( x, 2, {}, clbk, {} ); // $ExpectType boolndarray + someBy( x, 2, { 'keepdims': true }, clbk, {} ); // $ExpectType boolndarray + someBy( x, 2, { 'dims': [ 0 ] }, clbk, {} ); // $ExpectType boolndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + someBy( 5, 2, clbk ); // $ExpectError + someBy( true, 2, clbk ); // $ExpectError + someBy( false, 2, clbk ); // $ExpectError + someBy( null, 2, clbk ); // $ExpectError + someBy( undefined, 2, clbk ); // $ExpectError + someBy( {}, 2, clbk ); // $ExpectError + someBy( [ 1 ], 2, clbk ); // $ExpectError + someBy( ( x: number ): number => x, 2, clbk ); // $ExpectError + + someBy( 5, 2, {}, clbk ); // $ExpectError + someBy( true, 2, {}, clbk ); // $ExpectError + someBy( false, 2, {}, clbk ); // $ExpectError + someBy( null, 2, {}, clbk ); // $ExpectError + someBy( undefined, 2, {}, clbk ); // $ExpectError + someBy( {}, 2, {}, clbk ); // $ExpectError + someBy( [ 1 ], 2, {}, clbk ); // $ExpectError + someBy( ( x: number ): number => x, 2, {}, clbk ); // $ExpectError + + someBy( 5, 2, clbk, {} ); // $ExpectError + someBy( true, 2, clbk, {} ); // $ExpectError + someBy( false, 2, clbk, {} ); // $ExpectError + someBy( null, 2, clbk, {} ); // $ExpectError + someBy( undefined, 2, clbk, {} ); // $ExpectError + someBy( {}, 2, clbk, {} ); // $ExpectError + someBy( [ 1 ], 2, clbk, {} ); // $ExpectError + someBy( ( x: number ): number => x, 2, clbk, {} ); // $ExpectError + + someBy( 5, 2, {}, clbk, {} ); // $ExpectError + someBy( true, 2, {}, clbk, {} ); // $ExpectError + someBy( false, 2, {}, clbk, {} ); // $ExpectError + someBy( null, 2, {}, clbk, {} ); // $ExpectError + someBy( undefined, 2, {}, clbk, {} ); // $ExpectError + someBy( {}, 2, {}, clbk, {} ); // $ExpectError + someBy( [ 1 ], 2, {}, clbk, {} ); // $ExpectError + someBy( ( x: number ): number => x, 2, {}, 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 ] ); + + someBy( x, 2, '5', clbk ); // $ExpectError + someBy( x, 2, 5, clbk ); // $ExpectError + someBy( x, 2, true, clbk ); // $ExpectError + someBy( x, 2, false, clbk ); // $ExpectError + someBy( x, 2, null, clbk ); // $ExpectError + someBy( x, 2, [ 1 ], clbk ); // $ExpectError + someBy( x, 2, ( x: number ): number => x, clbk ); // $ExpectError + + someBy( x, 2, '5', clbk, {} ); // $ExpectError + someBy( x, 2, 5, clbk, {} ); // $ExpectError + someBy( x, 2, true, clbk, {} ); // $ExpectError + someBy( x, 2, false, clbk, {} ); // $ExpectError + someBy( x, 2, null, clbk, {} ); // $ExpectError + someBy( x, 2, [ 1 ], clbk, {} ); // $ExpectError + someBy( x, 2, ( 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 ] ); + + someBy( x, 2, '5' ); // $ExpectError + someBy( x, 2, 5 ); // $ExpectError + someBy( x, 2, true ); // $ExpectError + someBy( x, 2, false ); // $ExpectError + someBy( x, 2, null ); // $ExpectError + someBy( x, 2, [ 1 ] ); // $ExpectError + someBy( x, 2, {} ); // $ExpectError + + someBy( x, 2, '5', {} ); // $ExpectError + someBy( x, 2, 5, {} ); // $ExpectError + someBy( x, 2, true, {} ); // $ExpectError + someBy( x, 2, false, {} ); // $ExpectError + someBy( x, 2, null, {} ); // $ExpectError + someBy( x, 2, [ 1 ], {} ); // $ExpectError + someBy( x, 2, {}, {} ); // $ExpectError + + someBy( x, 2, {}, '5' ); // $ExpectError + someBy( x, 2, {}, 5 ); // $ExpectError + someBy( x, 2, {}, true ); // $ExpectError + someBy( x, 2, {}, false ); // $ExpectError + someBy( x, 2, {}, null ); // $ExpectError + someBy( x, 2, {}, [ 1 ] ); // $ExpectError + someBy( x, 2, {}, {} ); // $ExpectError + + someBy( x, 2, {}, '5', {} ); // $ExpectError + someBy( x, 2, {}, 5, {} ); // $ExpectError + someBy( x, 2, {}, true, {} ); // $ExpectError + someBy( x, 2, {}, false, {} ); // $ExpectError + someBy( x, 2, {}, null, {} ); // $ExpectError + someBy( x, 2, {}, [ 1 ], {} ); // $ExpectError + someBy( x, 2, {}, {}, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a `keepdims` option which is not a boolean... +{ + const x = zeros( [ 2, 2 ] ); + + someBy( x, 2, { 'keepdims': '5' }, clbk ); // $ExpectError + someBy( x, 2, { 'keepdims': 5 }, clbk ); // $ExpectError + someBy( x, 2, { 'keepdims': null }, clbk ); // $ExpectError + someBy( x, 2, { 'keepdims': [ 1 ] }, clbk ); // $ExpectError + someBy( x, 2, { 'keepdims': {} }, clbk ); // $ExpectError + someBy( x, 2, { 'keepdims': ( x: number ): number => x }, clbk ); // $ExpectError + + someBy( x, 2, { 'keepdims': '5' }, clbk, {} ); // $ExpectError + someBy( x, 2, { 'keepdims': 5 }, clbk, {} ); // $ExpectError + someBy( x, 2, { 'keepdims': null }, clbk, {} ); // $ExpectError + someBy( x, 2, { 'keepdims': [ 1 ] }, clbk, {} ); // $ExpectError + someBy( x, 2, { 'keepdims': {} }, clbk, {} ); // $ExpectError + someBy( x, 2, { '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 ] ); + + someBy( x, 2, { 'dims': '5' }, clbk ); // $ExpectError + someBy( x, 2, { 'dims': 5 }, clbk ); // $ExpectError + someBy( x, 2, { 'dims': null }, clbk ); // $ExpectError + someBy( x, 2, { 'dims': true }, clbk ); // $ExpectError + someBy( x, 2, { 'dims': false }, clbk ); // $ExpectError + someBy( x, 2, { 'dims': {} }, clbk ); // $ExpectError + someBy( x, 2, { 'dims': ( x: number ): number => x }, clbk ); // $ExpectError + + someBy( x, 2, { 'dims': '5' }, clbk, {} ); // $ExpectError + someBy( x, 2, { 'dims': 5 }, clbk, {} ); // $ExpectError + someBy( x, 2, { 'dims': null }, clbk, {} ); // $ExpectError + someBy( x, 2, { 'dims': true }, clbk, {} ); // $ExpectError + someBy( x, 2, { 'dims': false }, clbk, {} ); // $ExpectError + someBy( x, 2, { 'dims': {} }, clbk, {} ); // $ExpectError + someBy( x, 2, { '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 ] ); + + someBy(); // $ExpectError + someBy( x ); // $ExpectError + someBy( x, 2 ); // $ExpectError + someBy( x, 2, {}, clbk, {}, {} ); // $ExpectError +} + +// Attached to the function is an `assign` method which returns an ndarray... +{ + const x = zeros( [ 2, 2 ] ); + const y = empty( [], { 'dtype': 'int32' } ); + + someBy.assign( x, 2, y, clbk ); // $ExpectType boolndarray + someBy.assign( x, 2, y, {}, clbk ); // $ExpectType boolndarray + someBy.assign( x, 2, y, { 'dims': [ 0 ] }, clbk ); // $ExpectType boolndarray + + someBy.assign( x, 2, y, clbk, {} ); // $ExpectType boolndarray + someBy.assign( x, 2, y, {}, clbk, {} ); // $ExpectType boolndarray + someBy.assign( x, 2, 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': 'int32' } ); + + someBy.assign( 5, 2, y, clbk ); // $ExpectError + someBy.assign( true, 2, y, clbk ); // $ExpectError + someBy.assign( false, 2, y, clbk ); // $ExpectError + someBy.assign( null, 2, y, clbk ); // $ExpectError + someBy.assign( undefined, 2, y, clbk ); // $ExpectError + someBy.assign( {}, 2, y, clbk ); // $ExpectError + someBy.assign( [ 1 ], 2, y, clbk ); // $ExpectError + someBy.assign( ( x: number ): number => x, 2, y, clbk ); // $ExpectError + + someBy.assign( 5, 2, y, {}, clbk ); // $ExpectError + someBy.assign( true, 2, y, {}, clbk ); // $ExpectError + someBy.assign( false, 2, y, {}, clbk ); // $ExpectError + someBy.assign( null, 2, y, {}, clbk ); // $ExpectError + someBy.assign( undefined, 2, y, {}, clbk ); // $ExpectError + someBy.assign( {}, 2, y, {}, clbk ); // $ExpectError + someBy.assign( [ 1 ], 2, y, {}, clbk ); // $ExpectError + someBy.assign( ( x: number ): number => x, 2, y, {}, clbk ); // $ExpectError + + someBy.assign( 5, 2, y, clbk, {} ); // $ExpectError + someBy.assign( true, 2, y, clbk, {} ); // $ExpectError + someBy.assign( false, 2, y, clbk, {} ); // $ExpectError + someBy.assign( null, 2, y, clbk, {} ); // $ExpectError + someBy.assign( undefined, 2, y, clbk, {} ); // $ExpectError + someBy.assign( {}, 2, y, clbk, {} ); // $ExpectError + someBy.assign( [ 1 ], 2, y, clbk, {} ); // $ExpectError + someBy.assign( ( x: number ): number => x, 2, y, clbk, {} ); // $ExpectError + + someBy.assign( 5, 2, y, {}, clbk, {} ); // $ExpectError + someBy.assign( true, 2, y, {}, clbk, {} ); // $ExpectError + someBy.assign( false, 2, y, {}, clbk, {} ); // $ExpectError + someBy.assign( null, 2, y, {}, clbk, {} ); // $ExpectError + someBy.assign( undefined, 2, y, {}, clbk, {} ); // $ExpectError + someBy.assign( {}, 2, y, {}, clbk, {} ); // $ExpectError + someBy.assign( [ 1 ], 2, y, {}, clbk, {} ); // $ExpectError + someBy.assign( ( x: number ): number => x, 2, y, {}, clbk, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided an output argument which is not an ndarray... +{ + const x = zeros( [ 2, 2 ] ); + + someBy.assign( x, 2, 5, clbk ); // $ExpectError + someBy.assign( x, 2, true, clbk ); // $ExpectError + someBy.assign( x, 2, false, clbk ); // $ExpectError + someBy.assign( x, 2, null, clbk ); // $ExpectError + someBy.assign( x, 2, undefined, clbk ); // $ExpectError + someBy.assign( x, 2, {}, clbk ); // $ExpectError + someBy.assign( x, 2, [ 1 ], clbk ); // $ExpectError + someBy.assign( x, 2, ( x: number ): number => x, clbk ); // $ExpectError + + someBy.assign( x, 2, 5, {}, clbk ); // $ExpectError + someBy.assign( x, 2, true, {}, clbk ); // $ExpectError + someBy.assign( x, 2, false, {}, clbk ); // $ExpectError + someBy.assign( x, 2, null, {}, clbk ); // $ExpectError + someBy.assign( x, 2, undefined, {}, clbk ); // $ExpectError + someBy.assign( x, 2, {}, {}, clbk ); // $ExpectError + someBy.assign( x, 2, [ 1 ], {}, clbk ); // $ExpectError + someBy.assign( x, 2, ( x: number ): number => x, {}, clbk ); // $ExpectError + + someBy.assign( x, 2, 5, clbk, {} ); // $ExpectError + someBy.assign( x, 2, true, clbk, {} ); // $ExpectError + someBy.assign( x, 2, false, clbk, {} ); // $ExpectError + someBy.assign( x, 2, null, clbk, {} ); // $ExpectError + someBy.assign( x, 2, undefined, clbk, {} ); // $ExpectError + someBy.assign( x, 2, {}, clbk, {} ); // $ExpectError + someBy.assign( x, 2, [ 1 ], clbk, {} ); // $ExpectError + someBy.assign( x, 2, ( x: number ): number => x, clbk, {} ); // $ExpectError + + someBy.assign( x, 2, 5, {}, clbk, {} ); // $ExpectError + someBy.assign( x, 2, true, {}, clbk, {} ); // $ExpectError + someBy.assign( x, 2, false, {}, clbk, {} ); // $ExpectError + someBy.assign( x, 2, null, {}, clbk, {} ); // $ExpectError + someBy.assign( x, 2, undefined, {}, clbk, {} ); // $ExpectError + someBy.assign( x, 2, {}, {}, clbk, {} ); // $ExpectError + someBy.assign( x, 2, [ 1 ], {}, clbk, {} ); // $ExpectError + someBy.assign( x, 2, ( 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': 'int32' } ); + + someBy.assign( x, 2, y, '5', clbk ); // $ExpectError + someBy.assign( x, 2, y, 5, clbk ); // $ExpectError + someBy.assign( x, 2, y, true, clbk ); // $ExpectError + someBy.assign( x, 2, y, false, clbk ); // $ExpectError + someBy.assign( x, 2, y, null, clbk ); // $ExpectError + someBy.assign( x, 2, y, [ 1 ], clbk ); // $ExpectError + someBy.assign( x, 2, y, ( x: number ): number => x, clbk ); // $ExpectError + + someBy.assign( x, 2, y, '5', clbk, {} ); // $ExpectError + someBy.assign( x, 2, y, 5, clbk, {} ); // $ExpectError + someBy.assign( x, 2, y, true, clbk, {} ); // $ExpectError + someBy.assign( x, 2, y, false, clbk, {} ); // $ExpectError + someBy.assign( x, 2, y, null, clbk, {} ); // $ExpectError + someBy.assign( x, 2, y, [ 1 ], clbk, {} ); // $ExpectError + someBy.assign( x, 2, 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' } ); + + someBy.assign( x, 2, y, '5' ); // $ExpectError + someBy.assign( x, 2, y, 5 ); // $ExpectError + someBy.assign( x, 2, y, true ); // $ExpectError + someBy.assign( x, 2, y, false ); // $ExpectError + someBy.assign( x, 2, y, null ); // $ExpectError + someBy.assign( x, 2, y, [ 1 ] ); // $ExpectError + someBy.assign( x, 2, y, {} ); // $ExpectError + + someBy.assign( x, 2, y, '5', {} ); // $ExpectError + someBy.assign( x, 2, y, 5, {} ); // $ExpectError + someBy.assign( x, 2, y, true, {} ); // $ExpectError + someBy.assign( x, 2, y, false, {} ); // $ExpectError + someBy.assign( x, 2, y, null, {} ); // $ExpectError + someBy.assign( x, 2, y, [ 1 ], {} ); // $ExpectError + someBy.assign( x, 2, y, {}, {} ); // $ExpectError + + someBy.assign( x, 2, y, {}, '5' ); // $ExpectError + someBy.assign( x, 2, y, {}, 5 ); // $ExpectError + someBy.assign( x, 2, y, {}, true ); // $ExpectError + someBy.assign( x, 2, y, {}, false ); // $ExpectError + someBy.assign( x, 2, y, {}, null ); // $ExpectError + someBy.assign( x, 2, y, {}, [ 1 ] ); // $ExpectError + someBy.assign( x, 2, y, {}, {} ); // $ExpectError + + someBy.assign( x, 2, y, {}, '5', {} ); // $ExpectError + someBy.assign( x, 2, y, {}, 5, {} ); // $ExpectError + someBy.assign( x, 2, y, {}, true, {} ); // $ExpectError + someBy.assign( x, 2, y, {}, false, {} ); // $ExpectError + someBy.assign( x, 2, y, {}, null, {} ); // $ExpectError + someBy.assign( x, 2, y, {}, [ 1 ], {} ); // $ExpectError + someBy.assign( x, 2, 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': 'int32' } ); + + someBy.assign( x, 2, y, { 'dims': '5' }, clbk ); // $ExpectError + someBy.assign( x, 2, y, { 'dims': 5 }, clbk ); // $ExpectError + someBy.assign( x, 2, y, { 'dims': null }, clbk ); // $ExpectError + someBy.assign( x, 2, y, { 'dims': true }, clbk ); // $ExpectError + someBy.assign( x, 2, y, { 'dims': false }, clbk ); // $ExpectError + someBy.assign( x, 2, y, { 'dims': {} }, clbk ); // $ExpectError + someBy.assign( x, 2, y, { 'dims': ( x: number ): number => x }, clbk ); // $ExpectError + + someBy.assign( x, 2, y, { 'dims': '5' }, clbk, {} ); // $ExpectError + someBy.assign( x, 2, y, { 'dims': 5 }, clbk, {} ); // $ExpectError + someBy.assign( x, 2, y, { 'dims': null }, clbk, {} ); // $ExpectError + someBy.assign( x, 2, y, { 'dims': true }, clbk, {} ); // $ExpectError + someBy.assign( x, 2, y, { 'dims': false }, clbk, {} ); // $ExpectError + someBy.assign( x, 2, y, { 'dims': {} }, clbk, {} ); // $ExpectError + someBy.assign( x, 2, 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': 'int32' } ); + + someBy.assign(); // $ExpectError + someBy.assign( x ); // $ExpectError + someBy.assign( x, 2, y ); // $ExpectError + someBy.assign( x, 2, y, {}, clbk, {}, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/some-by/examples/index.js b/lib/node_modules/@stdlib/ndarray/some-by/examples/index.js new file mode 100644 index 000000000000..dc63278b8c26 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/some-by/examples/index.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'; + +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 scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var fillBy = require( '@stdlib/ndarray/fill-by' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var someBy = require( './../lib' ); + +var x = zeros( [ 2, 4, 5 ], { + 'dtype': 'float64' +}); +x = fillBy( x, discreteUniform( 0, 10 ) ); +console.log( ndarray2array( x ) ); + +var n = scalar2ndarray( 4, { + 'dtype': 'int8' +}); +var y = someBy( x, n, isEven ); +console.log( y.get() ); diff --git a/lib/node_modules/@stdlib/ndarray/some-by/lib/assign.js b/lib/node_modules/@stdlib/ndarray/some-by/lib/assign.js new file mode 100644 index 000000000000..4483b715ec1d --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/some-by/lib/assign.js @@ -0,0 +1,205 @@ +/** +* @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 isObject = require( '@stdlib/assert/is-object' ); +var isInteger = require( '@stdlib/assert/is-integer' ); +var contains = require( '@stdlib/assert/contains' ); +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/some-by' ); +var getDtype = require( '@stdlib/ndarray/dtype' ); +var getShape = require( '@stdlib/ndarray/shape' ); // note: non-base accessor is intentional due to the input arrays originating in userland +var getOrder = require( '@stdlib/ndarray/base/order' ); +var maybeBroadcastArray = require( '@stdlib/ndarray/base/maybe-broadcast-array' ); +var broadcastScalar = require( '@stdlib/ndarray/base/broadcast-scalar' ); +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' ); + + +// VARIABLES // + +var TYPES = [ + 'int8', + 'int16', + 'int32', + 'uint8', + 'uint8c', + 'uint16', + 'uint32' +]; + + +// MAIN // + +/** +* Tests whether at least `n` elements in an ndarray pass a test implemented by a predicate function along one or more dimensions. +* +* @param {ndarray} x - input ndarray +* @param {(ndarray|*)} n - number of elements which must pass the test +* @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 {Error} second argument must be broadcast-compatible with the non-reduced dimensions of the input ndarray +* @throws {TypeError} third argument must be an ndarray-like object +* @throws {TypeError} options argument must be an object +* @throws {Error} must provide valid options +* @throws {TypeError} predicate must be a function +* @returns {ndarray} output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var isEven = require( '@stdlib/assert/is-even' ).isPrimitive; +* var empty = require( '@stdlib/ndarray/empty' ); +* +* // Create a data buffer: +* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* +* // Define the shape of the input array: +* var shape = [ 3, 1, 2 ]; +* +* // Define the array strides: +* var sx = [ 4, 4, 1 ]; +* +* // Define the index offset: +* var ox = 1; +* +* // Create an input ndarray: +* var x = new ndarray( 'float64', xbuf, shape, sx, ox, 'row-major' ); +* +* // Create an output ndarray: +* var y = empty( [], { +* 'dtype': 'bool' +* }); +* +* // Perform reduction: +* var out = assign( x, 3, y, isEven ); +* // returns +* +* var v = out.get(); +* // returns true +*/ +function assign( x, n, y, options, predicate, thisArg ) { + var nargs; + var opts; + var err; + var ord; + var ctx; + var cb; + var N; + var v; + + 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. Third argument must be an ndarray-like object. Value: `%s`.', y ) ); + } + + N = ndims( x ); + nargs = arguments.length; + + if ( nargs === 4 ) { + if ( !isFunction( options ) ) { + throw new TypeError( format( 'invalid argument. Predicate must be a function. Value: `%s`.', options ) ); + } + cb = options; + ctx = thisArg; + opts = objectAssign( {}, defaults ); + opts.dims = zeroTo( N ); + } else if ( nargs === 5 ) { + if ( isFunction( options ) ) { + cb = options; + ctx = thisArg; + opts = objectAssign( {}, defaults ); + opts.dims = zeroTo( N ); + } + if ( isObject( options ) ) { + if ( !isFunction( predicate ) ) { + throw new TypeError( format( 'invalid argument. Predicate must be a function. Value: `%s`.', predicate ) ); + } + cb = predicate; + ctx = thisArg; + opts = objectAssign( {}, defaults, options ); + err = validate( opts, N, options ); + if ( err ) { + throw err; + } + if ( opts.dims === null ) { + opts.dims = zeroTo( N ); + } + } + } else if ( nargs === 6 ) { + if ( !isObject( options ) ) { + throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + } + if ( !isFunction( predicate ) ) { + throw new TypeError( format( 'invalid argument. Predicate must be a function. Value: `%s`.', predicate ) ); + } + cb = predicate; + ctx = thisArg; + opts = objectAssign( {}, defaults, options ); + err = validate( opts, N, options ); + if ( err ) { + throw err; + } + if ( opts.dims === null ) { + opts.dims = zeroTo( N ); + } + } + + // Resolve input array meta data: + ord = getOrder( x ); + + if ( isndarrayLike( n ) ) { + if ( !contains( TYPES, getDtype( n ) ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be an ndarray-like object having an integer data type. Value: `%s`.', n ) ); + } + try { + v = maybeBroadcastArray( n, getShape( y ) ); + } catch ( err ) { // eslint-disable-line no-unused-vars + throw new Error( 'invalid argument. Second argument must be broadcast-compatible with the non-reduced dimensions of the input array.' ); + } + } else { + if ( !isInteger( n ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be an integer or an ndarray-like object. Value: `%s`.', n ) ); + } + v = broadcastScalar( n, 'generic', getShape( y ), ord ); + } + + // Perform the reduction: + unaryReduceSubarrayBy( base, [ x, y, v ], 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/some-by/lib/defaults.json b/lib/node_modules/@stdlib/ndarray/some-by/lib/defaults.json new file mode 100644 index 000000000000..08433b373a0e --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/some-by/lib/defaults.json @@ -0,0 +1,4 @@ +{ + "dims": null, + "keepdims": false +} diff --git a/lib/node_modules/@stdlib/ndarray/some-by/lib/index.js b/lib/node_modules/@stdlib/ndarray/some-by/lib/index.js new file mode 100644 index 000000000000..05e42299596f --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/some-by/lib/index.js @@ -0,0 +1,103 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test whether at least `n` elements in an ndarray pass a test implemented by a predicate function along one or more dimensions. +* +* @module @stdlib/ndarray/some-by +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var someBy = require( '@stdlib/ndarray/some-by' ); +* +* // 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 = someBy( x, 6 ); +* // 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 someBy = require( '@stdlib/ndarray/some-by' ); +* +* // 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 = someBy.assign( x, 6.0, y ); +* // returns +* +* var v = out.get(); +* // returns true +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var main = require( './main.js' ); +var assign = require( './assign.js' ); + + +// MAIN // + +setReadOnly( main, 'assign', assign ); + + +// EXPORTS // + +module.exports = main; + +// exports: { "assign": "main.assign" } diff --git a/lib/node_modules/@stdlib/ndarray/some-by/lib/main.js b/lib/node_modules/@stdlib/ndarray/some-by/lib/main.js new file mode 100644 index 000000000000..c0c8d426ad66 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/some-by/lib/main.js @@ -0,0 +1,231 @@ +/** +* @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 isObject = require( '@stdlib/assert/is-object' ); +var isInteger = require( '@stdlib/assert/is-integer' ); +var contains = require( '@stdlib/assert/contains' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var unaryReduceSubarrayBy = require( '@stdlib/ndarray/base/unary-reduce-subarray-by' ); +var base = require( '@stdlib/ndarray/base/some-by' ); +var spreadDimensions = require( '@stdlib/ndarray/base/spread-dimensions' ); +var indicesComplement = require( '@stdlib/array/base/indices-complement' ); +var getDtype = require( '@stdlib/ndarray/dtype' ); +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 maybeBroadcastArray = require( '@stdlib/ndarray/base/maybe-broadcast-array' ); +var broadcastScalar = require( '@stdlib/ndarray/base/broadcast-scalar' ); +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' ); + + +// VARIABLES // + +var TYPES = [ + 'int8', + 'int16', + 'int32', + 'uint8', + 'uint8c', + 'uint16', + 'uint32' +]; + + +// MAIN // + +/** +* Tests whether at least `n` elements in an ndarray pass a test implemented by a predicate function along one or more dimensions. +* +* @param {ndarray} x - input ndarray +* @param {(ndarray|*)} n - number of elements which must pass a test +* @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} second argument must be an ndarray-like object or a scalar value +* @throws {TypeError} options argument must be an object +* @throws {TypeError} predicate must be a function +* @throws {RangeError} dimension indices must not exceed input ndarray bounds +* @throws {RangeError} number of dimension indices must not exceed the number of input ndarray dimensions +* @throws {Error} must provide valid options +* @returns {ndarray} output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var isEven = require( '@stdlib/assert/is-even' ).isPrimitive; +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* // Create a data buffer: +* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* +* // Define the shape of the input array: +* var sh = [ 3, 1, 2 ]; +* +* // Define the array strides: +* var sx = [ 4, 4, 1 ]; +* +* // Define the index offset: +* var ox = 1; +* +* // Create an input ndarray: +* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); +* +* // Perform reduction: +* var out = someBy( x, 3, isEven ); +* // returns +* +* var v = out.get(); +* // returns true +*/ +function someBy( x, n, options, predicate, thisArg ) { + var nargs; + var opts; + var view; + var ctx; + var err; + var idx; + var shx; + var shy; + var ord; + var cb; + var N; + var v; + var y; + + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object. Value: `%s`.', x ) ); + } + + shx = getShape( x ); + N = shx.length; + nargs = arguments.length; + + if ( nargs === 3 ) { + if ( !isFunction( options ) ) { + throw new TypeError( format( 'invalid argument. Predicate must be a function. Value: `%s`.', options ) ); + } + cb = options; + ctx = thisArg; + opts = objectAssign( {}, defaults ); + opts.dims = zeroTo( N ); + } else if ( nargs === 4 ) { + if ( isFunction( options ) ) { + cb = options; + ctx = thisArg; + opts = objectAssign( {}, defaults ); + opts.dims = zeroTo( N ); + } + if ( isObject( options ) ) { + if ( !isFunction( predicate ) ) { + throw new TypeError( format( 'invalid argument. Predicate must be a function. Value: `%s`.', predicate ) ); + } + cb = predicate; + ctx = thisArg; + opts = objectAssign( {}, defaults, options ); + err = validate( opts, N, options ); + if ( err ) { + throw err; + } + if ( opts.dims === null ) { + opts.dims = zeroTo( N ); + } + } + } else if ( nargs === 5 ) { + if ( !isObject( options ) ) { + throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + } + if ( !isFunction( predicate ) ) { + throw new TypeError( format( 'invalid argument. Predicate must be a function. Value: `%s`.', predicate ) ); + } + cb = predicate; + ctx = thisArg; + opts = objectAssign( {}, defaults, options ); + err = validate( opts, N, options ); + if ( err ) { + throw err; + } + 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 ); + + if ( isndarrayLike( n ) ) { + if ( !contains( TYPES, getDtype( n ) ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be an ndarray-like object having an integer data type. Value: `%s`.', n ) ); + } + try { + v = maybeBroadcastArray( n, shy ); + } catch ( err ) { // eslint-disable-line no-unused-vars + throw new Error( 'invalid argument. Second argument must be broadcast-compatible with the non-reduced dimensions of the input array.' ); + } + } else { + if ( !isInteger( n ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be an integer or an ndarray-like object. Value: `%s`.', n ) ); + } + v = broadcastScalar( n, 'generic', shy, ord ); + } + + // 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, v ], 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 = someBy; diff --git a/lib/node_modules/@stdlib/ndarray/some-by/lib/validate.js b/lib/node_modules/@stdlib/ndarray/some-by/lib/validate.js new file mode 100644 index 000000000000..3a1f3837df22 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/some-by/lib/validate.js @@ -0,0 +1,90 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isObject = require( '@stdlib/assert/is-plain-object' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var isIntegerArray = require( '@stdlib/assert/is-integer-array' ).primitives; +var isEmptyCollection = require( '@stdlib/assert/is-empty-collection' ); +var normalizeIndices = require( '@stdlib/ndarray/base/to-unique-normalized-indices' ); +var join = require( '@stdlib/array/base/join' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Validates function options. +* +* @private +* @param {Object} opts - destination object +* @param {NonNegativeInteger} ndims - number of input ndarray dimensions +* @param {Options} options - function options +* @param {boolean} [options.keepdims] - boolean indicating whether the reduced dimensions should be included in the returned ndarray as singleton dimensions +* @param {IntegerArray} [options.dims] - list of dimensions over which to perform a reduction +* @returns {(Error|null)} null or an error object +* +* @example +* var opts = {}; +* var options = { +* 'keepdims': true +* }; +* var err = validate( opts, 3, options ); +* if ( err ) { +* throw err; +* } +*/ +function validate( opts, ndims, options ) { + var tmp; + if ( !isObject( options ) ) { + return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + } + if ( hasOwnProp( options, 'keepdims' ) ) { + opts.keepdims = options.keepdims; + if ( !isBoolean( opts.keepdims ) ) { + return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'keepdims', opts.keepdims ) ); + } + } + if ( hasOwnProp( options, 'dims' ) ) { + opts.dims = options.dims; + if ( !isIntegerArray( opts.dims ) && !isEmptyCollection( opts.dims ) ) { + return new TypeError( format( 'invalid option. `%s` option must be an array of integers. Option: `%s`.', 'dims', opts.dims ) ); + } + tmp = normalizeIndices( opts.dims, ndims-1 ); + if ( tmp === null ) { + return new RangeError( format( 'invalid option. `%s` option contains an out-of-bounds dimension index. Option: [%s].', 'dims', join( opts.dims, ',' ) ) ); + } + if ( tmp.length !== opts.dims.length ) { + return new Error( format( 'invalid option. `%s` option contains duplicate indices. Option: [%s].', 'dims', join( opts.dims, ',' ) ) ); + } + if ( tmp.length > ndims ) { + return new RangeError( format( 'invalid option. `%s` option specifies more dimensions than exists in the input array. Number of dimensions: %d. Option: [%s].', ndims, join( opts.dims, ',' ) ) ); + } + opts.dims = tmp; + } + return null; +} + + +// EXPORTS // + +module.exports = validate; diff --git a/lib/node_modules/@stdlib/ndarray/some-by/package.json b/lib/node_modules/@stdlib/ndarray/some-by/package.json new file mode 100644 index 000000000000..cbadba1ecd1c --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/some-by/package.json @@ -0,0 +1,66 @@ +{ + "name": "@stdlib/ndarray/some-by", + "version": "0.0.0", + "description": "Test whether at least `n` elements in an ndarray pass a test implemented by a predicate function along one or more dimensions.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "strided", + "array", + "ndarray", + "every", + "some", + "all", + "utility", + "utils", + "truthy", + "reduce", + "reduction" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/ndarray/some-by/test/test.js b/lib/node_modules/@stdlib/ndarray/some-by/test/test.js new file mode 100644 index 000000000000..e7d7dbd59961 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/some-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 someBy = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof someBy, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is an `assign` method', function test( t ) { + t.strictEqual( isMethod( someBy, 'assign' ), true, 'returns expected value' ); + t.end(); +}); From f19d14555352def9b1d926903401cfa7c20e3294 Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Sat, 31 May 2025 13:32:45 +0000 Subject: [PATCH 2/7] fix: bugs & docs --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- lib/node_modules/@stdlib/ndarray/some-by/README.md | 7 ++++--- lib/node_modules/@stdlib/ndarray/some-by/lib/assign.js | 2 +- lib/node_modules/@stdlib/ndarray/some-by/lib/main.js | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/some-by/README.md b/lib/node_modules/@stdlib/ndarray/some-by/README.md index bd4dd09a6e76..22feae72b8e9 100644 --- a/lib/node_modules/@stdlib/ndarray/some-by/README.md +++ b/lib/node_modules/@stdlib/ndarray/some-by/README.md @@ -33,7 +33,7 @@ limitations under the License. ## Usage ```javascript -var someBy = require( '@stdlib/ndarray/someBy' ); +var someBy = require( '@stdlib/ndarray/some-by' ); ``` #### someBy( x, n\[, options], predicate\[, thisArg] ) @@ -208,7 +208,7 @@ var v = out.get(); // returns true var count = ctx.count; -// returns 6 +// returns 2 ``` #### someBy.assign( x, n, out\[, options], predicate\[, thisArg] ) @@ -300,7 +300,7 @@ var ox = 1; var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); // Create an output ndarray: -var y = empty( [ 3 ], { +var y = empty( [ 2 ], { 'dtype': 'bool' }); @@ -349,6 +349,7 @@ var ndarray2array = require( '@stdlib/ndarray/to-array' ); var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); var fillBy = require( '@stdlib/ndarray/fill-by' ); var zeros = require( '@stdlib/ndarray/zeros' ); +var someBy = require( '@stdlib/ndarray/some-by' ); var x = zeros( [ 2, 4, 5 ], { 'dtype': 'float64' diff --git a/lib/node_modules/@stdlib/ndarray/some-by/lib/assign.js b/lib/node_modules/@stdlib/ndarray/some-by/lib/assign.js index 4483b715ec1d..2638128a4ccf 100644 --- a/lib/node_modules/@stdlib/ndarray/some-by/lib/assign.js +++ b/lib/node_modules/@stdlib/ndarray/some-by/lib/assign.js @@ -137,7 +137,7 @@ function assign( x, n, y, options, predicate, thisArg ) { } else if ( nargs === 5 ) { if ( isFunction( options ) ) { cb = options; - ctx = thisArg; + ctx = predicate; opts = objectAssign( {}, defaults ); opts.dims = zeroTo( N ); } diff --git a/lib/node_modules/@stdlib/ndarray/some-by/lib/main.js b/lib/node_modules/@stdlib/ndarray/some-by/lib/main.js index c0c8d426ad66..32ff3fbe6ba7 100644 --- a/lib/node_modules/@stdlib/ndarray/some-by/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/some-by/lib/main.js @@ -143,7 +143,7 @@ function someBy( x, n, options, predicate, thisArg ) { } else if ( nargs === 4 ) { if ( isFunction( options ) ) { cb = options; - ctx = thisArg; + ctx = predicate; opts = objectAssign( {}, defaults ); opts.dims = zeroTo( N ); } From 7ab876a1e011140ec22c55ad909bf92dcbba3d72 Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Mon, 2 Jun 2025 08:24:26 +0000 Subject: [PATCH 3/7] fix: apply suggestions from code review --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../@stdlib/ndarray/some-by/README.md | 132 ++++-------------- .../@stdlib/ndarray/some-by/docs/repl.txt | 38 ++--- .../ndarray/some-by/docs/types/index.d.ts | 20 +-- .../ndarray/some-by/docs/types/test.ts | 83 +++++++++++ .../@stdlib/ndarray/some-by/lib/assign.js | 100 +++++++------ .../@stdlib/ndarray/some-by/lib/index.js | 2 +- .../@stdlib/ndarray/some-by/lib/main.js | 100 +++++++------ .../@stdlib/ndarray/some-by/package.json | 2 +- 8 files changed, 237 insertions(+), 240 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/some-by/README.md b/lib/node_modules/@stdlib/ndarray/some-by/README.md index 22feae72b8e9..3980b3629119 100644 --- a/lib/node_modules/@stdlib/ndarray/some-by/README.md +++ b/lib/node_modules/@stdlib/ndarray/some-by/README.md @@ -20,7 +20,7 @@ limitations under the License. # someBy -> Test whether at least `n` elements in an [`ndarray`][@stdlib/ndarray/ctor] pass a test implemented by a predicate function along one or more dimensions. +> Test whether at least `n` elements along one or more [`ndarray`][@stdlib/ndarray/ctor] dimensions pass a test implemented by a predicate function.
@@ -38,32 +38,20 @@ var someBy = require( '@stdlib/ndarray/some-by' ); #### someBy( x, n\[, options], predicate\[, thisArg] ) -Tests whether at least `n` elements in an [`ndarray`][@stdlib/ndarray/ctor] pass a test implemented by a predicate function along one or more dimensions. +Tests whether at least `n` elements along one or more [`ndarray`][@stdlib/ndarray/ctor] dimensions pass a test implemented by a predicate function. ```javascript -var Float64Array = require( '@stdlib/array/float64' ); -var ndarray = require( '@stdlib/ndarray/ctor' ); +var array = require( '@stdlib/ndarray/array' ); function predicate( 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' ); +var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 0.0, 6.0 ] ] ] ); +// returns // Perform reduction: var out = someBy( x, 2, predicate ); @@ -76,7 +64,7 @@ console.log( out.get() ); The function accepts the following arguments: - **x**: input [`ndarray`][@stdlib/ndarray/ctor]. -- **n**: number of elements which must pass the test implemented by a predicate function. May be either a scalar or an [`ndarray`][@stdlib/ndarray/ctor]. +- **n**: number of elements which must pass the test implemented by a predicate function. May be either a scalar or an [`ndarray`][@stdlib/ndarray/ctor]. Must be [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the non-reduced dimensions of input [`ndarray`][@stdlib/ndarray/ctor]. Must have an integer [data type][@stdlib/ndarray/dtypes]. - **options**: function options (_optional_). - **predicate**: predicate function. - **thisArg**: predicate execution context (_optional_). @@ -88,31 +76,17 @@ The function accepts the following `options`: 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 Float64Array = require( '@stdlib/array/float64' ); -var ndarray = require( '@stdlib/ndarray/ctor' ); +var array = require( '@stdlib/ndarray/array' ); var ndarray2array = require( '@stdlib/ndarray/to-array' ); function predicate( 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' ); +var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 0.0, 6.0 ] ] ] ); +// returns var opts = { 'dims': [ 0, 1 ] @@ -128,31 +102,17 @@ var v = ndarray2array( out ); 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 Float64Array = require( '@stdlib/array/float64' ); -var ndarray = require( '@stdlib/ndarray/ctor' ); +var array = require( '@stdlib/ndarray/array' ); var ndarray2array = require( '@stdlib/ndarray/to-array' ); function predicate( 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' ); +var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 0.0, 6.0 ] ] ] ); +// returns var opts = { 'dims': [ 0, 1 ], @@ -164,36 +124,24 @@ var out = someBy( x, 2, opts, predicate ); // returns var v = ndarray2array( out ); -// returns [[[ true, true ]]] +// returns [ [ [ true, true ] ] ] ``` To set the predicate function execution context, provide a `thisArg`. - + ```javascript -var Float64Array = require( '@stdlib/array/float64' ); -var ndarray = require( '@stdlib/ndarray/ctor' ); +var array = require( '@stdlib/ndarray/array' ); function predicate( value ) { this.count += 1; 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' ); +var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 0.0, 6.0 ] ] ] ); +// returns // Create a context object: var ctx = { @@ -213,33 +161,21 @@ var count = ctx.count; #### someBy.assign( x, n, out\[, options], predicate\[, thisArg] ) -Test whether at least `n` elements in an [`ndarray`][@stdlib/ndarray/ctor] pass a test implemented by a predicate function along one or more dimensions and assigns results to a provided output [`ndarray`][@stdlib/ndarray/ctor]. +Tests whether at least `n` elements along one or more [`ndarray`][@stdlib/ndarray/ctor] dimensions pass a test implemented by a predicate function and assigns results to a provided output [`ndarray`][@stdlib/ndarray/ctor]. ```javascript -var Float64Array = require( '@stdlib/array/float64' ); -var ndarray = require( '@stdlib/ndarray/ctor' ); +var array = require( '@stdlib/ndarray/array' ); var empty = require( '@stdlib/ndarray/empty' ); function predicate( 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' ); +var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 0.0, 6.0 ] ] ] ); +// returns // Create an output ndarray: var y = empty( [], { @@ -260,7 +196,7 @@ var v = y.get(); The function accepts the following arguments: - **x**: input [`ndarray`][@stdlib/ndarray/ctor]. -- **n**: number of elements which must pass the test implemented by a predicate function. May be either a scalar or an [`ndarray`][@stdlib/ndarray/ctor]. +- **n**: number of elements which must pass the test implemented by a predicate function. May be either a scalar or an [`ndarray`][@stdlib/ndarray/ctor]. Must be [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the non-reduced dimensions of input [`ndarray`][@stdlib/ndarray/ctor]. Must have an integer [data type][@stdlib/ndarray/dtypes]. - **out**: output [`ndarray`][@stdlib/ndarray/ctor]. The output [`ndarray`][@stdlib/ndarray/ctor] must have a shape matching the non-reduced dimensions of the input [`ndarray`][@stdlib/ndarray/ctor]. - **options**: function options (_optional_). - **predicate**: predicate function. @@ -272,11 +208,8 @@ The function accepts the following `options`: 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 Float64Array = require( '@stdlib/array/float64' ); -var ndarray = require( '@stdlib/ndarray/ctor' ); +var array = require( '@stdlib/ndarray/array' ); var empty = require( '@stdlib/ndarray/empty' ); var ndarray2array = require( '@stdlib/ndarray/to-array' ); @@ -284,20 +217,9 @@ function predicate( 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' ); +var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 0.0, 6.0 ] ] ] ); +// returns // Create an output ndarray: var y = empty( [ 2 ], { @@ -380,6 +302,10 @@ console.log( y.get() ); [@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor +[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes + +[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes + diff --git a/lib/node_modules/@stdlib/ndarray/some-by/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/some-by/docs/repl.txt index 5253e1418115..f802b7377e0e 100644 --- a/lib/node_modules/@stdlib/ndarray/some-by/docs/repl.txt +++ b/lib/node_modules/@stdlib/ndarray/some-by/docs/repl.txt @@ -1,16 +1,17 @@ {{alias}}( x, n[, options], predicate[, thisArg] ) - Tests whether at least `n` elements in an ndarray pass a test implemented by - a predicate function along one or more dimensions. + Tests whether at least `n` elements along one or more ndarray dimensions + pass a test implemented by a predicate function. Parameters ---------- x: ndarray Input ndarray. - n: ndarray|any + n: ndarray|integer Number of elements which must pass the test implemented by a predicate - function. + function. Must be brodcast compatible with the non-reduced dimensions of + input ndarray. options: Object (optional) Function options. @@ -38,14 +39,9 @@ Examples -------- - > var xbuf = new {{alias:@stdlib/array/float64}}( [ 1.0, 2.0, 3.0, 4.0 ] ); - > var dt = 'float64'; - > var sh = [ 2, 2 ]; - > var sx = [ 2, 1 ]; - > var ox = 0; - > var ord = 'row-major'; - > var x = {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, sx, ox, ord ); + > function f ( v ) { return v > 0.0; }; + > var x = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2], [ 3, 4 ] ] ); > var y = {{alias}}( x, 3, f ) > y.get() @@ -59,17 +55,18 @@ {{alias}}.assign( x, n, y[, options], predicate[, thisArg] ) - Tests whether at least `n` elements in an ndarray pass a test implemented by - a predicate function along one or more dimensions. + Tests whether at least `n` elements along one or more ndarray dimensions + pass a test implemented by a predicate function. Parameters ---------- x: ndarray Input ndarray. - n: ndarray|any + n: ndarray|integer Number of elements which must pass the test implemented by a predicate - function. + function. Must be brodcast compatible with the non-reduced dimensions of + input ndarray. y: ndarray Output ndarray. The output shape must match the shape of the non-reduced @@ -100,15 +97,10 @@ Examples -------- - > var xbuf = new {{alias:@stdlib/array/float64}}( [ 1.0, 2.0, 3.0, 4.0 ] ); - > var dt = 'float64'; - > var sh = [ 2, 2 ]; - > var sx = [ 2, 1 ]; - > var ox = 0; - > var ord = 'row-major'; - > var x = {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, sx, ox, ord ); - > var y = {{alias:@stdlib/ndarray/from-scalar}}( false ); + > function f ( v ) { return v > 0.0 }; + > var x = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2], [ 3, 4 ] ] ); + > var y = {{alias:@stdlib/ndarray/from-scalar}}( false ); > var out = {{alias}}.assign( x, 3, y, f ) > var bool = ( out === y ) diff --git a/lib/node_modules/@stdlib/ndarray/some-by/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/some-by/docs/types/index.d.ts index 8aef14c2f8ac..b9a59704a5eb 100644 --- a/lib/node_modules/@stdlib/ndarray/some-by/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/ndarray/some-by/docs/types/index.d.ts @@ -21,7 +21,7 @@ /// import { ArrayLike } from '@stdlib/types/array'; -import { ndarray, boolndarray, typedndarray } from '@stdlib/types/ndarray'; +import { ndarray, boolndarray, integerndarray, typedndarray } from '@stdlib/types/ndarray'; /** * Returns a boolean indicating whether an element passes a test. @@ -92,7 +92,7 @@ interface Options extends BaseOptions { */ interface SomeBy { /** - * Tests whether at least `n` elements in an ndarray pass a test implemented by a predicate function along one or more dimensions. + * Tests whether at least `n` elements along one or more ndarray dimensions pass a test implemented by a predicate function. * * @param x - input ndarray * @param n - number of elements which must pass a test @@ -127,10 +127,10 @@ interface SomeBy { * var v = out.get(); * // returns true */ - ( x: ndarray, n: ndarray | unknown, predicate: Predicate, thisArg?: ThisParameterType> ): boolndarray; + ( x: ndarray, n: integerndarray | number, predicate: Predicate, thisArg?: ThisParameterType> ): boolndarray; /** - * Tests whether at least `n` elements in an ndarray pass a test implemented by a predicate function along one or more dimensions. + * Tests whether at least `n` elements along one or more ndarray dimensions pass a test implemented by a predicate function. * * @param x - input ndarray * @param n - number of elements which must pass a test @@ -168,10 +168,10 @@ interface SomeBy { * var v = out.get(); * // returns true */ - ( x: ndarray, n: ndarray | unknown, options: Options, predicate: Predicate, thisArg?: ThisParameterType> ): boolndarray; + ( x: ndarray, n: integerndarray | number, options: Options, predicate: Predicate, thisArg?: ThisParameterType> ): boolndarray; /** - * Tests whether at least `n` elements in an ndarray pass a test implemented by a predicate function along one or more dimensions. + * Tests whether at least `n` elements along one or more ndarray dimensions pass a test implemented by a predicate function. * * @param x - input ndarray * @param n - number of elements which must pass a test @@ -213,10 +213,10 @@ interface SomeBy { * var v = out.get(); * // returns true */ - assign( x: ndarray, n: ndarray | unknown, y: ndarray, predicate: Predicate, thisArg?: ThisParameterType> ): boolndarray; + assign( x: ndarray, n: integerndarray | number, y: ndarray, predicate: Predicate, thisArg?: ThisParameterType> ): boolndarray; /** - * Tests whether at least `n` elements in an ndarray pass a test implemented by a predicate function along one or more dimensions. + * Tests whether at least `n` elements along one or more ndarray dimensions pass a test implemented by a predicate function. * * @param x - input ndarray * @param n - number of elements which must pass a test @@ -260,11 +260,11 @@ interface SomeBy { * var v = out.get(); * // returns true */ - assign( x: ndarray, n: ndarray | unknown, y: ndarray, options: BaseOptions, predicate: Predicate, thisArg?: ThisParameterType> ): boolndarray; + assign( x: ndarray, n: integerndarray | number, y: ndarray, options: BaseOptions, predicate: Predicate, thisArg?: ThisParameterType> ): boolndarray; } /** -* Tests whether at least `n` elements in an ndarray pass a test implemented by a predicate function along one or more dimensions. +* Tests whether at least `n` elements along one or more ndarray dimensions pass a test implemented by a predicate function. * * @param x - input ndarray * @param n - number of elements which must pass a test diff --git a/lib/node_modules/@stdlib/ndarray/some-by/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/some-by/docs/types/test.ts index 2561c88371ec..fb64f3017a35 100644 --- a/lib/node_modules/@stdlib/ndarray/some-by/docs/types/test.ts +++ b/lib/node_modules/@stdlib/ndarray/some-by/docs/types/test.ts @@ -89,6 +89,47 @@ function clbk( v: any ): boolean { someBy( ( x: number ): number => x, 2, {}, clbk, {} ); // $ExpectError } +// The compiler throws an error if the function is provided a second argument which is not an integer ndarray or a number... +{ + const x = zeros( [ 2, 2 ] ); + + someBy( x, '5', clbk ); // $ExpectError + someBy( x, true, clbk ); // $ExpectError + someBy( x, false, clbk ); // $ExpectError + someBy( x, null, clbk ); // $ExpectError + someBy( x, undefined, clbk ); // $ExpectError + someBy( x, {}, clbk ); // $ExpectError + someBy( x, [ 1 ], clbk ); // $ExpectError + someBy( x, ( x: number ): number => x, clbk ); // $ExpectError + + someBy( x, '5', {}, clbk ); // $ExpectError + someBy( x, true, {}, clbk ); // $ExpectError + someBy( x, false, {}, clbk ); // $ExpectError + someBy( x, null, {}, clbk ); // $ExpectError + someBy( x, undefined, {}, clbk ); // $ExpectError + someBy( x, {}, {}, clbk ); // $ExpectError + someBy( x, [ 1 ], {}, clbk ); // $ExpectError + someBy( x, ( x: number ): number => x, {}, clbk ); // $ExpectError + + someBy( x, '5', clbk, {} ); // $ExpectError + someBy( x, true, clbk, {} ); // $ExpectError + someBy( x, false, clbk, {} ); // $ExpectError + someBy( x, null, clbk, {} ); // $ExpectError + someBy( x, undefined, clbk, {} ); // $ExpectError + someBy( x, {}, clbk, {} ); // $ExpectError + someBy( x, [ 1 ], clbk, {} ); // $ExpectError + someBy( x, ( x: number ): number => x, clbk, {} ); // $ExpectError + + someBy( x, '5', {}, clbk, {} ); // $ExpectError + someBy( x, true, {}, clbk, {} ); // $ExpectError + someBy( x, false, {}, clbk, {} ); // $ExpectError + someBy( x, null, {}, clbk, {} ); // $ExpectError + someBy( x, undefined, {}, clbk, {} ); // $ExpectError + someBy( x, {}, {}, clbk, {} ); // $ExpectError + someBy( x, [ 1 ], {}, clbk, {} ); // $ExpectError + someBy( x, ( 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 ] ); @@ -252,6 +293,48 @@ function clbk( v: any ): boolean { someBy.assign( ( x: number ): number => x, 2, y, {}, clbk, {} ); // $ExpectError } +// The compiler throws an error if the `assign` method is provided a second argument which is not an integer ndarray or a number... +{ + const x = zeros( [ 2, 2 ] ); + const y = empty( [], { 'dtype': 'int32' } ); + + someBy.assign( x, '5', y, clbk ); // $ExpectError + someBy.assign( x, true, y, clbk ); // $ExpectError + someBy.assign( x, false, y, clbk ); // $ExpectError + someBy.assign( x, null, y, clbk ); // $ExpectError + someBy.assign( x, undefined, y, clbk ); // $ExpectError + someBy.assign( x, {}, y, clbk ); // $ExpectError + someBy.assign( x, [ 1 ], y, clbk ); // $ExpectError + someBy.assign( x, ( x: number ): number => x, y, clbk ); // $ExpectError + + someBy.assign( x, '5', y, {}, clbk ); // $ExpectError + someBy.assign( x, true, y, {}, clbk ); // $ExpectError + someBy.assign( x, false, y, {}, clbk ); // $ExpectError + someBy.assign( x, null, y, {}, clbk ); // $ExpectError + someBy.assign( x, undefined, y, {}, clbk ); // $ExpectError + someBy.assign( x, {}, y, {}, clbk ); // $ExpectError + someBy.assign( x, [ 1 ], y, {}, clbk ); // $ExpectError + someBy.assign( x, ( x: number ): number => x, y, {}, clbk ); // $ExpectError + + someBy.assign( x, '5', y, clbk, {} ); // $ExpectError + someBy.assign( x, true, y, clbk, {} ); // $ExpectError + someBy.assign( x, false, y, clbk, {} ); // $ExpectError + someBy.assign( x, null, y, clbk, {} ); // $ExpectError + someBy.assign( x, undefined, y, clbk, {} ); // $ExpectError + someBy.assign( x, {}, y, clbk, {} ); // $ExpectError + someBy.assign( x, [ 1 ], y, clbk, {} ); // $ExpectError + someBy.assign( x, ( x: number ): number => x, y, clbk, {} ); // $ExpectError + + someBy.assign( x, '5', y, {}, clbk, {} ); // $ExpectError + someBy.assign( x, true, y, {}, clbk, {} ); // $ExpectError + someBy.assign( x, false, y, {}, clbk, {} ); // $ExpectError + someBy.assign( x, null, y, {}, clbk, {} ); // $ExpectError + someBy.assign( x, undefined, y, {}, clbk, {} ); // $ExpectError + someBy.assign( x, {}, y, {}, clbk, {} ); // $ExpectError + someBy.assign( x, [ 1 ], y, {}, clbk, {} ); // $ExpectError + someBy.assign( x, ( x: number ): number => x, y, {}, clbk, {} ); // $ExpectError +} + // The compiler throws an error if the `assign` method is provided an output argument which is not an ndarray... { const x = zeros( [ 2, 2 ] ); diff --git a/lib/node_modules/@stdlib/ndarray/some-by/lib/assign.js b/lib/node_modules/@stdlib/ndarray/some-by/lib/assign.js index 2638128a4ccf..1ded9104e5e8 100644 --- a/lib/node_modules/@stdlib/ndarray/some-by/lib/assign.js +++ b/lib/node_modules/@stdlib/ndarray/some-by/lib/assign.js @@ -23,7 +23,7 @@ var isFunction = require( '@stdlib/assert/is-function' ); var isObject = require( '@stdlib/assert/is-object' ); var isInteger = require( '@stdlib/assert/is-integer' ); -var contains = require( '@stdlib/assert/contains' ); +var isIntegerDataType = require( '@stdlib/ndarray/base/assert/is-integer-data-type' ); var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); var unaryReduceSubarrayBy = require( '@stdlib/ndarray/base/unary-reduce-subarray-by' ); var ndims = require( '@stdlib/ndarray/ndims' ); @@ -31,35 +31,28 @@ var base = require( '@stdlib/ndarray/base/some-by' ); var getDtype = require( '@stdlib/ndarray/dtype' ); var getShape = require( '@stdlib/ndarray/shape' ); // note: non-base accessor is intentional due to the input arrays originating in userland var getOrder = require( '@stdlib/ndarray/base/order' ); +var defaults = require( '@stdlib/ndarray/defaults' ); var maybeBroadcastArray = require( '@stdlib/ndarray/base/maybe-broadcast-array' ); var broadcastScalar = require( '@stdlib/ndarray/base/broadcast-scalar' ); 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 DEFAULTS = require( './defaults.json' ); var validate = require( './validate.js' ); // VARIABLES // -var TYPES = [ - 'int8', - 'int16', - 'int32', - 'uint8', - 'uint8c', - 'uint16', - 'uint32' -]; +var DEFAULT_DTYPE = defaults.get( 'dtypes.integer_index' ); // MAIN // /** -* Tests whether at least `n` elements in an ndarray pass a test implemented by a predicate function along one or more dimensions. +* Tests whether at least `n` elements along one or more ndarray dimensions pass a test implemented by a predicate function. * * @param {ndarray} x - input ndarray -* @param {(ndarray|*)} n - number of elements which must pass the test +* @param {(ndarray|integer)} n - number of elements which must pass the test * @param {ndarray} y - output ndarray * @param {Options} [options] - function options * @param {IntegerArray} [options.dims] - list of dimensions over which to perform a reduction @@ -70,7 +63,7 @@ var TYPES = [ * @throws {TypeError} third argument must be an ndarray-like object * @throws {TypeError} options argument must be an object * @throws {Error} must provide valid options -* @throws {TypeError} predicate must be a function +* @throws {TypeError} callback argument must be a function * @returns {ndarray} output ndarray * * @example @@ -111,10 +104,12 @@ function assign( x, n, y, options, predicate, thisArg ) { var opts; var err; var ord; + var flg; var ctx; var cb; var N; var v; + var o; if ( !isndarrayLike( x ) ) { throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object. Value: `%s`.', x ) ); @@ -126,61 +121,64 @@ function assign( x, n, y, options, predicate, thisArg ) { N = ndims( x ); nargs = arguments.length; - if ( nargs === 4 ) { - if ( !isFunction( options ) ) { - throw new TypeError( format( 'invalid argument. Predicate must be a function. Value: `%s`.', options ) ); + // Case: assign( x, n, y, predicate ) + if ( nargs < 5 ) { + if ( !isObject( options ) ) { + if ( !isFunction( options ) ) { + throw new TypeError( format( 'invalid argument. Fourth argument must be a function. Value: `%s`.', options ) ); + } + cb = options; } - cb = options; + } + // Case: assign( x, n, y, options, predicate, thisArg ) + else if ( nargs > 5 ) { + if ( !isObject( options ) ) { + throw new TypeError( format( 'invalid argument. Fourth argument must be an object. Value: `%s`.', options ) ); + } + if ( !isFunction( predicate ) ) { + throw new TypeError( format( 'invalid argument. Fifth argument must be a function. Value: `%s`.', predicate ) ); + } + cb = predicate; ctx = thisArg; - opts = objectAssign( {}, defaults ); - opts.dims = zeroTo( N ); - } else if ( nargs === 5 ) { - if ( isFunction( options ) ) { + o = options; + flg = true; + } else { + // Case: assign( x, n, y, predicate, thisArg ) + if ( !isObject( options ) ) { + if ( !isFunction( options ) ) { + throw new TypeError( format( 'invalid argument. Fourth argument must be a function. Value: `%s`.', options ) ); + } cb = options; ctx = predicate; - opts = objectAssign( {}, defaults ); - opts.dims = zeroTo( N ); } + // Case: assign( x, n, y, options, predicate ) if ( isObject( options ) ) { if ( !isFunction( predicate ) ) { - throw new TypeError( format( 'invalid argument. Predicate must be a function. Value: `%s`.', predicate ) ); + throw new TypeError( format( 'invalid argument. Fifth argument must be a function. Value: `%s`.', predicate ) ); } + o = options; cb = predicate; - ctx = thisArg; - opts = objectAssign( {}, defaults, options ); - err = validate( opts, N, options ); - if ( err ) { - throw err; - } - if ( opts.dims === null ) { - opts.dims = zeroTo( N ); - } - } - } else if ( nargs === 6 ) { - if ( !isObject( options ) ) { - throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + flg = true; } - if ( !isFunction( predicate ) ) { - throw new TypeError( format( 'invalid argument. Predicate must be a function. Value: `%s`.', predicate ) ); - } - cb = predicate; - ctx = thisArg; - opts = objectAssign( {}, defaults, options ); - err = validate( opts, N, options ); + } + + opts = objectAssign( {}, DEFAULTS ); + if ( flg ) { + err = validate( opts, N, o ); if ( err ) { throw err; } - if ( opts.dims === null ) { - opts.dims = zeroTo( N ); - } + } + if ( opts.dims === null ) { + opts.dims = zeroTo( N ); } // Resolve input array meta data: ord = getOrder( x ); if ( isndarrayLike( n ) ) { - if ( !contains( TYPES, getDtype( n ) ) ) { - throw new TypeError( format( 'invalid argument. Second argument must be an ndarray-like object having an integer data type. Value: `%s`.', n ) ); + if ( isIntegerDataType( getDtype( n ) ) ) { + throw new TypeError( format( 'invalid argument. Second argument must have an integer data type. Value: `%s`.', n ) ); } try { v = maybeBroadcastArray( n, getShape( y ) ); @@ -191,7 +189,7 @@ function assign( x, n, y, options, predicate, thisArg ) { if ( !isInteger( n ) ) { throw new TypeError( format( 'invalid argument. Second argument must be an integer or an ndarray-like object. Value: `%s`.', n ) ); } - v = broadcastScalar( n, 'generic', getShape( y ), ord ); + v = broadcastScalar( n, DEFAULT_DTYPE, getShape( y ), ord ); } // Perform the reduction: diff --git a/lib/node_modules/@stdlib/ndarray/some-by/lib/index.js b/lib/node_modules/@stdlib/ndarray/some-by/lib/index.js index 05e42299596f..f386757770a5 100644 --- a/lib/node_modules/@stdlib/ndarray/some-by/lib/index.js +++ b/lib/node_modules/@stdlib/ndarray/some-by/lib/index.js @@ -19,7 +19,7 @@ 'use strict'; /** -* Test whether at least `n` elements in an ndarray pass a test implemented by a predicate function along one or more dimensions. +* Test whether at least `n` elements along one or more ndarray dimensions pass a test implemented by a predicate function. * * @module @stdlib/ndarray/some-by * diff --git a/lib/node_modules/@stdlib/ndarray/some-by/lib/main.js b/lib/node_modules/@stdlib/ndarray/some-by/lib/main.js index 32ff3fbe6ba7..a8a5892b9951 100644 --- a/lib/node_modules/@stdlib/ndarray/some-by/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/some-by/lib/main.js @@ -23,8 +23,8 @@ var isFunction = require( '@stdlib/assert/is-function' ); var isObject = require( '@stdlib/assert/is-object' ); var isInteger = require( '@stdlib/assert/is-integer' ); -var contains = require( '@stdlib/assert/contains' ); var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var isIntegerDataType = require( '@stdlib/ndarray/base/assert/is-integer-data-type' ); var unaryReduceSubarrayBy = require( '@stdlib/ndarray/base/unary-reduce-subarray-by' ); var base = require( '@stdlib/ndarray/base/some-by' ); var spreadDimensions = require( '@stdlib/ndarray/base/spread-dimensions' ); @@ -35,6 +35,7 @@ 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 defaults = require( '@stdlib/ndarray/defaults' ); var empty = require( '@stdlib/ndarray/empty' ); var ndarrayCtor = require( '@stdlib/ndarray/base/ctor' ); var maybeBroadcastArray = require( '@stdlib/ndarray/base/maybe-broadcast-array' ); @@ -44,30 +45,22 @@ 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 DEFAULTS = require( './defaults.json' ); var validate = require( './validate.js' ); // VARIABLES // -var TYPES = [ - 'int8', - 'int16', - 'int32', - 'uint8', - 'uint8c', - 'uint16', - 'uint32' -]; +var DEFAULT_DTYPE = defaults.get( 'dtypes.integer_index' ); // MAIN // /** -* Tests whether at least `n` elements in an ndarray pass a test implemented by a predicate function along one or more dimensions. +* Tests whether at least `n` elements along one or more ndarray dimensions pass a test implemented by a predicate function. * * @param {ndarray} x - input ndarray -* @param {(ndarray|*)} n - number of elements which must pass a test +* @param {(ndarray|integer)} n - number of elements which must pass a test * @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 @@ -76,7 +69,7 @@ var TYPES = [ * @throws {TypeError} first argument must be an ndarray-like object * @throws {TypeError} second argument must be an ndarray-like object or a scalar value * @throws {TypeError} options argument must be an object -* @throws {TypeError} predicate must be a function +* @throws {TypeError} callback argument must be a function * @throws {RangeError} dimension indices must not exceed input ndarray bounds * @throws {RangeError} number of dimension indices must not exceed the number of input ndarray dimensions * @throws {Error} must provide valid options @@ -119,10 +112,12 @@ function someBy( x, n, options, predicate, thisArg ) { var shx; var shy; var ord; + var flg; var cb; var N; var v; var y; + var o; if ( !isndarrayLike( x ) ) { throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object. Value: `%s`.', x ) ); @@ -132,53 +127,56 @@ function someBy( x, n, options, predicate, thisArg ) { N = shx.length; nargs = arguments.length; - if ( nargs === 3 ) { - if ( !isFunction( options ) ) { - throw new TypeError( format( 'invalid argument. Predicate must be a function. Value: `%s`.', options ) ); + // Case: assign( x, n, predicate ) + if ( nargs < 4 ) { + if ( !isObject( options ) ) { + if ( !isFunction( options ) ) { + throw new TypeError( format( 'invalid argument. Third argument must be a function. Value: `%s`.', options ) ); + } + cb = options; } - cb = options; + } + // Case: assign( x, n, options, predicate, thisArg ) + else if ( nargs > 4 ) { + if ( !isObject( options ) ) { + throw new TypeError( format( 'invalid argument. Third argument must be an object. Value: `%s`.', options ) ); + } + if ( !isFunction( predicate ) ) { + throw new TypeError( format( 'invalid argument. Fourth argument must be a function. Value: `%s`.', predicate ) ); + } + cb = predicate; ctx = thisArg; - opts = objectAssign( {}, defaults ); - opts.dims = zeroTo( N ); - } else if ( nargs === 4 ) { - if ( isFunction( options ) ) { + o = options; + flg = true; + } else { + // Case: assign( x, n, predicate, thisArg ) + if ( !isObject( options ) ) { + if ( !isFunction( options ) ) { + throw new TypeError( format( 'invalid argument. Third argument must be a function. Value: `%s`.', options ) ); + } cb = options; ctx = predicate; - opts = objectAssign( {}, defaults ); - opts.dims = zeroTo( N ); } + // Case: assign( x, n, options, predicate ) if ( isObject( options ) ) { if ( !isFunction( predicate ) ) { - throw new TypeError( format( 'invalid argument. Predicate must be a function. Value: `%s`.', predicate ) ); + throw new TypeError( format( 'invalid argument. Fourth argument must be a function. Value: `%s`.', predicate ) ); } + o = options; cb = predicate; - ctx = thisArg; - opts = objectAssign( {}, defaults, options ); - err = validate( opts, N, options ); - if ( err ) { - throw err; - } - if ( opts.dims === null ) { - opts.dims = zeroTo( N ); - } - } - } else if ( nargs === 5 ) { - if ( !isObject( options ) ) { - throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + flg = true; } - if ( !isFunction( predicate ) ) { - throw new TypeError( format( 'invalid argument. Predicate must be a function. Value: `%s`.', predicate ) ); - } - cb = predicate; - ctx = thisArg; - opts = objectAssign( {}, defaults, options ); - err = validate( opts, N, options ); + } + + opts = objectAssign( {}, DEFAULTS ); + if ( flg ) { + err = validate( opts, N, o ); if ( err ) { throw err; } - if ( opts.dims === null ) { - opts.dims = zeroTo( N ); - } + } + if ( opts.dims === null ) { + opts.dims = zeroTo( N ); } // Resolve the list of non-reduced dimensions: @@ -191,8 +189,8 @@ function someBy( x, n, options, predicate, thisArg ) { ord = getOrder( x ); if ( isndarrayLike( n ) ) { - if ( !contains( TYPES, getDtype( n ) ) ) { - throw new TypeError( format( 'invalid argument. Second argument must be an ndarray-like object having an integer data type. Value: `%s`.', n ) ); + if ( !isIntegerDataType( getDtype( n ) ) ) { + throw new TypeError( format( 'invalid argument. Second argument must have an integer data type. Value: `%s`.', n ) ); } try { v = maybeBroadcastArray( n, shy ); @@ -203,7 +201,7 @@ function someBy( x, n, options, predicate, thisArg ) { if ( !isInteger( n ) ) { throw new TypeError( format( 'invalid argument. Second argument must be an integer or an ndarray-like object. Value: `%s`.', n ) ); } - v = broadcastScalar( n, 'generic', shy, ord ); + v = broadcastScalar( n, DEFAULT_DTYPE, shy, ord ); } // Initialize an output array whose shape matches that of the non-reduced dimensions and which has the same memory layout as the input array: diff --git a/lib/node_modules/@stdlib/ndarray/some-by/package.json b/lib/node_modules/@stdlib/ndarray/some-by/package.json index cbadba1ecd1c..8528f01efbae 100644 --- a/lib/node_modules/@stdlib/ndarray/some-by/package.json +++ b/lib/node_modules/@stdlib/ndarray/some-by/package.json @@ -1,7 +1,7 @@ { "name": "@stdlib/ndarray/some-by", "version": "0.0.0", - "description": "Test whether at least `n` elements in an ndarray pass a test implemented by a predicate function along one or more dimensions.", + "description": "Test whether at least `n` elements along one or more ndarray dimensions pass a test implemented by a predicate function.", "license": "Apache-2.0", "author": { "name": "The Stdlib Authors", From c8cab9ce9e851069df4a907a3aee8b00fea6ce8b Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Mon, 2 Jun 2025 21:54:57 +0500 Subject: [PATCH 4/7] Apply suggestions from code review Signed-off-by: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> --- lib/node_modules/@stdlib/ndarray/some-by/README.md | 2 -- lib/node_modules/@stdlib/ndarray/some-by/docs/repl.txt | 4 ++-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/some-by/README.md b/lib/node_modules/@stdlib/ndarray/some-by/README.md index 3980b3629119..8d0c0893df2c 100644 --- a/lib/node_modules/@stdlib/ndarray/some-by/README.md +++ b/lib/node_modules/@stdlib/ndarray/some-by/README.md @@ -40,7 +40,6 @@ var someBy = require( '@stdlib/ndarray/some-by' ); Tests whether at least `n` elements along one or more [`ndarray`][@stdlib/ndarray/ctor] dimensions pass a test implemented by a predicate function. - ```javascript var array = require( '@stdlib/ndarray/array' ); @@ -163,7 +162,6 @@ var count = ctx.count; Tests whether at least `n` elements along one or more [`ndarray`][@stdlib/ndarray/ctor] dimensions pass a test implemented by a predicate function and assigns results to a provided output [`ndarray`][@stdlib/ndarray/ctor]. - ```javascript var array = require( '@stdlib/ndarray/array' ); diff --git a/lib/node_modules/@stdlib/ndarray/some-by/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/some-by/docs/repl.txt index f802b7377e0e..33fae9c6482c 100644 --- a/lib/node_modules/@stdlib/ndarray/some-by/docs/repl.txt +++ b/lib/node_modules/@stdlib/ndarray/some-by/docs/repl.txt @@ -10,7 +10,7 @@ n: ndarray|integer Number of elements which must pass the test implemented by a predicate - function. Must be brodcast compatible with the non-reduced dimensions of + function. Must be broadcast compatible with the non-reduced dimensions of input ndarray. options: Object (optional) @@ -65,7 +65,7 @@ n: ndarray|integer Number of elements which must pass the test implemented by a predicate - function. Must be brodcast compatible with the non-reduced dimensions of + function. Must be broadcast compatible with the non-reduced dimensions of input ndarray. y: ndarray From da7b1e5d0dfb9ee62a6c731e1afce06fc4e3a1aa Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Mon, 2 Jun 2025 17:01:28 +0000 Subject: [PATCH 5/7] docs: fix line length --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: passed - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- lib/node_modules/@stdlib/ndarray/some-by/docs/repl.txt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/some-by/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/some-by/docs/repl.txt index 33fae9c6482c..ba58c1a589f5 100644 --- a/lib/node_modules/@stdlib/ndarray/some-by/docs/repl.txt +++ b/lib/node_modules/@stdlib/ndarray/some-by/docs/repl.txt @@ -10,8 +10,8 @@ n: ndarray|integer Number of elements which must pass the test implemented by a predicate - function. Must be broadcast compatible with the non-reduced dimensions of - input ndarray. + function. Must be broadcast compatible with the non-reduced dimensions + of input ndarray. options: Object (optional) Function options. @@ -65,8 +65,8 @@ n: ndarray|integer Number of elements which must pass the test implemented by a predicate - function. Must be broadcast compatible with the non-reduced dimensions of - input ndarray. + function. Must be broadcast compatible with the non-reduced dimensions + of input ndarray. y: ndarray Output ndarray. The output shape must match the shape of the non-reduced From 171c5803d09abc53ab36977c9bad67fe6f4611e6 Mon Sep 17 00:00:00 2001 From: Athan Date: Mon, 2 Jun 2025 16:55:07 -0700 Subject: [PATCH 6/7] chore: clean-up --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../@stdlib/ndarray/some-by/docs/repl.txt | 6 +- .../ndarray/some-by/docs/types/index.d.ts | 8 +-- .../ndarray/some-by/docs/types/test.ts | 70 ++++++++++++++----- .../@stdlib/ndarray/some-by/lib/assign.js | 67 +++++++----------- .../@stdlib/ndarray/some-by/lib/main.js | 70 ++++++++----------- .../@stdlib/ndarray/some-by/test/test.js | 2 + 6 files changed, 118 insertions(+), 105 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/some-by/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/some-by/docs/repl.txt index ba58c1a589f5..b8d128e5eb97 100644 --- a/lib/node_modules/@stdlib/ndarray/some-by/docs/repl.txt +++ b/lib/node_modules/@stdlib/ndarray/some-by/docs/repl.txt @@ -11,7 +11,7 @@ n: ndarray|integer Number of elements which must pass the test implemented by a predicate function. Must be broadcast compatible with the non-reduced dimensions - of input ndarray. + of input ndarray. Must have an integer data type. options: Object (optional) Function options. @@ -39,7 +39,6 @@ Examples -------- - > function f ( v ) { return v > 0.0; }; > var x = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2], [ 3, 4 ] ] ); > var y = {{alias}}( x, 3, f ) @@ -66,7 +65,7 @@ n: ndarray|integer Number of elements which must pass the test implemented by a predicate function. Must be broadcast compatible with the non-reduced dimensions - of input ndarray. + of input ndarray. Must have an integer data type. y: ndarray Output ndarray. The output shape must match the shape of the non-reduced @@ -97,7 +96,6 @@ Examples -------- - > function f ( v ) { return v > 0.0 }; > var x = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2], [ 3, 4 ] ] ); > var y = {{alias:@stdlib/ndarray/from-scalar}}( false ); diff --git a/lib/node_modules/@stdlib/ndarray/some-by/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/some-by/docs/types/index.d.ts index b9a59704a5eb..3cc6bf04112f 100644 --- a/lib/node_modules/@stdlib/ndarray/some-by/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/ndarray/some-by/docs/types/index.d.ts @@ -127,7 +127,7 @@ interface SomeBy { * var v = out.get(); * // returns true */ - ( x: ndarray, n: integerndarray | number, predicate: Predicate, thisArg?: ThisParameterType> ): boolndarray; + ( x: ndarray, n: integerndarray | number, predicate: Predicate, thisArg?: ThisParameterType> ): boolndarray; /** * Tests whether at least `n` elements along one or more ndarray dimensions pass a test implemented by a predicate function. @@ -168,7 +168,7 @@ interface SomeBy { * var v = out.get(); * // returns true */ - ( x: ndarray, n: integerndarray | number, options: Options, predicate: Predicate, thisArg?: ThisParameterType> ): boolndarray; + ( x: ndarray, n: integerndarray | number, options: Options, predicate: Predicate, thisArg?: ThisParameterType> ): boolndarray; /** * Tests whether at least `n` elements along one or more ndarray dimensions pass a test implemented by a predicate function. @@ -213,7 +213,7 @@ interface SomeBy { * var v = out.get(); * // returns true */ - assign( x: ndarray, n: integerndarray | number, y: ndarray, predicate: Predicate, thisArg?: ThisParameterType> ): boolndarray; + assign( x: ndarray, n: integerndarray | number, y: U, predicate: Predicate, thisArg?: ThisParameterType> ): U; /** * Tests whether at least `n` elements along one or more ndarray dimensions pass a test implemented by a predicate function. @@ -260,7 +260,7 @@ interface SomeBy { * var v = out.get(); * // returns true */ - assign( x: ndarray, n: integerndarray | number, y: ndarray, options: BaseOptions, predicate: Predicate, thisArg?: ThisParameterType> ): boolndarray; + assign( x: ndarray, n: integerndarray | number, y: U, options: BaseOptions, predicate: Predicate, thisArg?: ThisParameterType> ): U; } /** diff --git a/lib/node_modules/@stdlib/ndarray/some-by/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/some-by/docs/types/test.ts index fb64f3017a35..58d74ba4f0e0 100644 --- a/lib/node_modules/@stdlib/ndarray/some-by/docs/types/test.ts +++ b/lib/node_modules/@stdlib/ndarray/some-by/docs/types/test.ts @@ -16,13 +16,14 @@ * limitations under the License. */ +/* eslint-disable space-in-parens */ + /// import zeros = require( '@stdlib/ndarray/zeros' ); import empty = require( '@stdlib/ndarray/empty' ); import someBy = require( './index' ); - /** * Predicate function. * @@ -39,6 +40,9 @@ function clbk( v: any ): boolean { // The function returns an ndarray... { const x = zeros( [ 2, 2 ] ); + const n = zeros( [], { + 'dtype': 'int32' + }); someBy( x, 2, clbk ); // $ExpectType boolndarray someBy( x, 2, { 'keepdims': true }, clbk ); // $ExpectType boolndarray @@ -48,6 +52,15 @@ function clbk( v: any ): boolean { someBy( x, 2, {}, clbk, {} ); // $ExpectType boolndarray someBy( x, 2, { 'keepdims': true }, clbk, {} ); // $ExpectType boolndarray someBy( x, 2, { 'dims': [ 0 ] }, clbk, {} ); // $ExpectType boolndarray + + someBy( x, n, clbk ); // $ExpectType boolndarray + someBy( x, n, { 'keepdims': true }, clbk ); // $ExpectType boolndarray + someBy( x, n, { 'dims': [ 0 ] }, clbk ); // $ExpectType boolndarray + + someBy( x, n, clbk, {} ); // $ExpectType boolndarray + someBy( x, n, {}, clbk, {} ); // $ExpectType boolndarray + someBy( x, n, { 'keepdims': true }, clbk, {} ); // $ExpectType boolndarray + someBy( x, n, { 'dims': [ 0 ] }, clbk, {} ); // $ExpectType boolndarray } // The compiler throws an error if the function is provided a first argument which is not an ndarray... @@ -241,20 +254,35 @@ function clbk( v: any ): boolean { // Attached to the function is an `assign` method which returns an ndarray... { const x = zeros( [ 2, 2 ] ); - const y = empty( [], { 'dtype': 'int32' } ); - - someBy.assign( x, 2, y, clbk ); // $ExpectType boolndarray - someBy.assign( x, 2, y, {}, clbk ); // $ExpectType boolndarray - someBy.assign( x, 2, y, { 'dims': [ 0 ] }, clbk ); // $ExpectType boolndarray - - someBy.assign( x, 2, y, clbk, {} ); // $ExpectType boolndarray - someBy.assign( x, 2, y, {}, clbk, {} ); // $ExpectType boolndarray - someBy.assign( x, 2, y, { 'dims': [ 0 ] }, clbk, {} ); // $ExpectType boolndarray + const n = zeros( [], { + 'dtype': 'int32' + }); + const y = empty( [], { + 'dtype': 'int32' + }); + + someBy.assign( x, 2, y, clbk ); // $ExpectType int32ndarray + someBy.assign( x, 2, y, {}, clbk ); // $ExpectType int32ndarray + someBy.assign( x, 2, y, { 'dims': [ 0 ] }, clbk ); // $ExpectType int32ndarray + + someBy.assign( x, 2, y, clbk, {} ); // $ExpectType int32ndarray + someBy.assign( x, 2, y, {}, clbk, {} ); // $ExpectType int32ndarray + someBy.assign( x, 2, y, { 'dims': [ 0 ] }, clbk, {} ); // $ExpectType int32ndarray + + someBy.assign( x, n, y, clbk ); // $ExpectType int32ndarray + someBy.assign( x, n, y, {}, clbk ); // $ExpectType int32ndarray + someBy.assign( x, n, y, { 'dims': [ 0 ] }, clbk ); // $ExpectType int32ndarray + + someBy.assign( x, n, y, clbk, {} ); // $ExpectType int32ndarray + someBy.assign( x, n, y, {}, clbk, {} ); // $ExpectType int32ndarray + someBy.assign( x, n, y, { 'dims': [ 0 ] }, clbk, {} ); // $ExpectType int32ndarray } // The compiler throws an error if the `assign` method is provided a first argument which is not an ndarray... { - const y = empty( [], { 'dtype': 'int32' } ); + const y = empty( [], { + 'dtype': 'int32' + }); someBy.assign( 5, 2, y, clbk ); // $ExpectError someBy.assign( true, 2, y, clbk ); // $ExpectError @@ -296,7 +324,9 @@ function clbk( v: any ): boolean { // The compiler throws an error if the `assign` method is provided a second argument which is not an integer ndarray or a number... { const x = zeros( [ 2, 2 ] ); - const y = empty( [], { 'dtype': 'int32' } ); + const y = empty( [], { + 'dtype': 'int32' + }); someBy.assign( x, '5', y, clbk ); // $ExpectError someBy.assign( x, true, y, clbk ); // $ExpectError @@ -379,7 +409,9 @@ function clbk( v: any ): boolean { // 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': 'int32' } ); + const y = empty( [], { + 'dtype': 'int32' + }); someBy.assign( x, 2, y, '5', clbk ); // $ExpectError someBy.assign( x, 2, y, 5, clbk ); // $ExpectError @@ -401,7 +433,9 @@ function clbk( v: any ): boolean { // 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' } ); + const y = empty( [], { + 'dtype': 'int32' + }); someBy.assign( x, 2, y, '5' ); // $ExpectError someBy.assign( x, 2, y, 5 ); // $ExpectError @@ -439,7 +473,9 @@ function clbk( v: any ): boolean { // 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': 'int32' } ); + const y = empty( [], { + 'dtype': 'int32' + }); someBy.assign( x, 2, y, { 'dims': '5' }, clbk ); // $ExpectError someBy.assign( x, 2, y, { 'dims': 5 }, clbk ); // $ExpectError @@ -461,7 +497,9 @@ function clbk( v: any ): boolean { // The compiler throws an error if the function is provided an unsupported number of arguments... { const x = zeros( [ 2, 2 ] ); - const y = empty( [], { 'dtype': 'int32' } ); + const y = empty( [], { + 'dtype': 'int32' + }); someBy.assign(); // $ExpectError someBy.assign( x ); // $ExpectError diff --git a/lib/node_modules/@stdlib/ndarray/some-by/lib/assign.js b/lib/node_modules/@stdlib/ndarray/some-by/lib/assign.js index 1ded9104e5e8..22f7a2d57afa 100644 --- a/lib/node_modules/@stdlib/ndarray/some-by/lib/assign.js +++ b/lib/node_modules/@stdlib/ndarray/some-by/lib/assign.js @@ -21,8 +21,7 @@ // MODULES // var isFunction = require( '@stdlib/assert/is-function' ); -var isObject = require( '@stdlib/assert/is-object' ); -var isInteger = require( '@stdlib/assert/is-integer' ); +var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; var isIntegerDataType = require( '@stdlib/ndarray/base/assert/is-integer-data-type' ); var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); var unaryReduceSubarrayBy = require( '@stdlib/ndarray/base/unary-reduce-subarray-by' ); @@ -62,8 +61,8 @@ var DEFAULT_DTYPE = defaults.get( 'dtypes.integer_index' ); * @throws {Error} second argument must be broadcast-compatible with the non-reduced dimensions of the input ndarray * @throws {TypeError} third argument must be an ndarray-like object * @throws {TypeError} options argument must be an object -* @throws {Error} must provide valid options * @throws {TypeError} callback argument must be a function +* @throws {Error} must provide valid options * @returns {ndarray} output ndarray * * @example @@ -111,57 +110,46 @@ function assign( x, n, y, options, predicate, thisArg ) { var v; 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. Third argument must be an ndarray-like object. Value: `%s`.', y ) ); } - - N = ndims( x ); - nargs = arguments.length; - // Case: assign( x, n, y, predicate ) if ( nargs < 5 ) { - if ( !isObject( options ) ) { - if ( !isFunction( options ) ) { - throw new TypeError( format( 'invalid argument. Fourth argument must be a function. Value: `%s`.', options ) ); - } - cb = options; + cb = options; + if ( !isFunction( cb ) ) { + throw new TypeError( format( 'invalid argument. Fourth argument must be a function. Value: `%s`.', cb ) ); } } // Case: assign( x, n, y, options, predicate, thisArg ) else if ( nargs > 5 ) { - if ( !isObject( options ) ) { - throw new TypeError( format( 'invalid argument. Fourth argument must be an object. Value: `%s`.', options ) ); - } - if ( !isFunction( predicate ) ) { - throw new TypeError( format( 'invalid argument. Fifth argument must be a function. Value: `%s`.', predicate ) ); - } + flg = true; + o = options; cb = predicate; + if ( !isFunction( cb ) ) { + throw new TypeError( format( 'invalid argument. Fifth argument must be a function. Value: `%s`.', cb ) ); + } ctx = thisArg; - o = options; + } + // Case: assign( x, n, y, predicate, thisArg ) + else if ( isFunction( options ) ) { + cb = options; + ctx = predicate; + } + // Case: assign( x, n, y, options, predicate ) + else if ( isFunction( predicate ) ) { flg = true; - } else { - // Case: assign( x, n, y, predicate, thisArg ) - if ( !isObject( options ) ) { - if ( !isFunction( options ) ) { - throw new TypeError( format( 'invalid argument. Fourth argument must be a function. Value: `%s`.', options ) ); - } - cb = options; - ctx = predicate; - } - // Case: assign( x, n, y, options, predicate ) - if ( isObject( options ) ) { - if ( !isFunction( predicate ) ) { - throw new TypeError( format( 'invalid argument. Fifth argument must be a function. Value: `%s`.', predicate ) ); - } - o = options; - cb = predicate; - flg = true; - } + o = options; + cb = predicate; } - + // Case: assign( x, n, y, ???, ??? ) + else { + throw new TypeError( format( 'invalid argument. Fifth argument must be a function. Value: `%s`.', predicate ) ); + } + N = ndims( x ); opts = objectAssign( {}, DEFAULTS ); if ( flg ) { err = validate( opts, N, o ); @@ -169,10 +157,10 @@ function assign( x, n, y, options, predicate, thisArg ) { 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 input array meta data: ord = getOrder( x ); @@ -191,7 +179,6 @@ function assign( x, n, y, options, predicate, thisArg ) { } v = broadcastScalar( n, DEFAULT_DTYPE, getShape( y ), ord ); } - // Perform the reduction: unaryReduceSubarrayBy( base, [ x, y, v ], 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; diff --git a/lib/node_modules/@stdlib/ndarray/some-by/lib/main.js b/lib/node_modules/@stdlib/ndarray/some-by/lib/main.js index a8a5892b9951..8c9dd90276e4 100644 --- a/lib/node_modules/@stdlib/ndarray/some-by/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/some-by/lib/main.js @@ -21,8 +21,7 @@ // MODULES // var isFunction = require( '@stdlib/assert/is-function' ); -var isObject = require( '@stdlib/assert/is-object' ); -var isInteger = require( '@stdlib/assert/is-integer' ); +var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); var isIntegerDataType = require( '@stdlib/ndarray/base/assert/is-integer-data-type' ); var unaryReduceSubarrayBy = require( '@stdlib/ndarray/base/unary-reduce-subarray-by' ); @@ -119,54 +118,44 @@ function someBy( x, n, options, predicate, thisArg ) { 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 ) ); } - - shx = getShape( x ); - N = shx.length; - nargs = arguments.length; - - // Case: assign( x, n, predicate ) + // Case: someBy( x, n, predicate ) if ( nargs < 4 ) { - if ( !isObject( options ) ) { - if ( !isFunction( options ) ) { - throw new TypeError( format( 'invalid argument. Third argument must be a function. Value: `%s`.', options ) ); - } - cb = options; + if ( !isFunction( options ) ) { + throw new TypeError( format( 'invalid argument. Third argument must be a function. Value: `%s`.', options ) ); } + cb = options; } - // Case: assign( x, n, options, predicate, thisArg ) + // Case: someBy( x, n, options, predicate, thisArg ) else if ( nargs > 4 ) { - if ( !isObject( options ) ) { - throw new TypeError( format( 'invalid argument. Third argument must be an object. Value: `%s`.', options ) ); - } - if ( !isFunction( predicate ) ) { - throw new TypeError( format( 'invalid argument. Fourth argument must be a function. Value: `%s`.', predicate ) ); - } + 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; - o = options; + } + // Case: someBy( x, n, predicate, thisArg ) + else if ( isFunction( options ) ) { + cb = options; + ctx = predicate; + } + // Case: someBy( x, n, options, predicate ) + else if ( isFunction( predicate ) ) { flg = true; - } else { - // Case: assign( x, n, predicate, thisArg ) - if ( !isObject( options ) ) { - if ( !isFunction( options ) ) { - throw new TypeError( format( 'invalid argument. Third argument must be a function. Value: `%s`.', options ) ); - } - cb = options; - ctx = predicate; - } - // Case: assign( x, n, options, predicate ) - if ( isObject( options ) ) { - if ( !isFunction( predicate ) ) { - throw new TypeError( format( 'invalid argument. Fourth argument must be a function. Value: `%s`.', predicate ) ); - } - o = options; - cb = predicate; - flg = true; - } + o = options; + cb = predicate; + } + // Case: someBy( x, n, ???, ??? ) + else { + throw new TypeError( format( 'invalid argument. Fourth argument must be a function. Value: `%s`.', predicate ) ); } + shx = getShape( x ); + N = shx.length; opts = objectAssign( {}, DEFAULTS ); if ( flg ) { @@ -175,10 +164,10 @@ function someBy( x, n, options, predicate, thisArg ) { 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 ); @@ -203,7 +192,6 @@ function someBy( x, n, options, predicate, thisArg ) { } v = broadcastScalar( n, DEFAULT_DTYPE, shy, ord ); } - // 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', diff --git a/lib/node_modules/@stdlib/ndarray/some-by/test/test.js b/lib/node_modules/@stdlib/ndarray/some-by/test/test.js index e7d7dbd59961..2d2917d3a951 100644 --- a/lib/node_modules/@stdlib/ndarray/some-by/test/test.js +++ b/lib/node_modules/@stdlib/ndarray/some-by/test/test.js @@ -37,3 +37,5 @@ tape( 'attached to the main export is an `assign` method', function test( t ) { t.strictEqual( isMethod( someBy, 'assign' ), true, 'returns expected value' ); t.end(); }); + +// TODO: add tests From a135dd632ae2016abb822e25c8fe57ebd08f0a1c Mon Sep 17 00:00:00 2001 From: Athan Date: Mon, 2 Jun 2025 16:56:46 -0700 Subject: [PATCH 7/7] test: update dtype --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../ndarray/some-by/docs/types/test.ts | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/some-by/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/some-by/docs/types/test.ts index 58d74ba4f0e0..347da93b12a1 100644 --- a/lib/node_modules/@stdlib/ndarray/some-by/docs/types/test.ts +++ b/lib/node_modules/@stdlib/ndarray/some-by/docs/types/test.ts @@ -258,30 +258,30 @@ function clbk( v: any ): boolean { 'dtype': 'int32' }); const y = empty( [], { - 'dtype': 'int32' + 'dtype': 'bool' }); - someBy.assign( x, 2, y, clbk ); // $ExpectType int32ndarray - someBy.assign( x, 2, y, {}, clbk ); // $ExpectType int32ndarray - someBy.assign( x, 2, y, { 'dims': [ 0 ] }, clbk ); // $ExpectType int32ndarray + someBy.assign( x, 2, y, clbk ); // $ExpectType boolndarray + someBy.assign( x, 2, y, {}, clbk ); // $ExpectType boolndarray + someBy.assign( x, 2, y, { 'dims': [ 0 ] }, clbk ); // $ExpectType boolndarray - someBy.assign( x, 2, y, clbk, {} ); // $ExpectType int32ndarray - someBy.assign( x, 2, y, {}, clbk, {} ); // $ExpectType int32ndarray - someBy.assign( x, 2, y, { 'dims': [ 0 ] }, clbk, {} ); // $ExpectType int32ndarray + someBy.assign( x, 2, y, clbk, {} ); // $ExpectType boolndarray + someBy.assign( x, 2, y, {}, clbk, {} ); // $ExpectType boolndarray + someBy.assign( x, 2, y, { 'dims': [ 0 ] }, clbk, {} ); // $ExpectType boolndarray - someBy.assign( x, n, y, clbk ); // $ExpectType int32ndarray - someBy.assign( x, n, y, {}, clbk ); // $ExpectType int32ndarray - someBy.assign( x, n, y, { 'dims': [ 0 ] }, clbk ); // $ExpectType int32ndarray + someBy.assign( x, n, y, clbk ); // $ExpectType boolndarray + someBy.assign( x, n, y, {}, clbk ); // $ExpectType boolndarray + someBy.assign( x, n, y, { 'dims': [ 0 ] }, clbk ); // $ExpectType boolndarray - someBy.assign( x, n, y, clbk, {} ); // $ExpectType int32ndarray - someBy.assign( x, n, y, {}, clbk, {} ); // $ExpectType int32ndarray - someBy.assign( x, n, y, { 'dims': [ 0 ] }, clbk, {} ); // $ExpectType int32ndarray + someBy.assign( x, n, y, clbk, {} ); // $ExpectType boolndarray + someBy.assign( x, n, y, {}, clbk, {} ); // $ExpectType boolndarray + someBy.assign( x, n, 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': 'int32' + 'dtype': 'bool' }); someBy.assign( 5, 2, y, clbk ); // $ExpectError @@ -325,7 +325,7 @@ function clbk( v: any ): boolean { { const x = zeros( [ 2, 2 ] ); const y = empty( [], { - 'dtype': 'int32' + 'dtype': 'bool' }); someBy.assign( x, '5', y, clbk ); // $ExpectError @@ -410,7 +410,7 @@ function clbk( v: any ): boolean { { const x = zeros( [ 2, 2 ] ); const y = empty( [], { - 'dtype': 'int32' + 'dtype': 'bool' }); someBy.assign( x, 2, y, '5', clbk ); // $ExpectError @@ -474,7 +474,7 @@ function clbk( v: any ): boolean { { const x = zeros( [ 2, 2 ] ); const y = empty( [], { - 'dtype': 'int32' + 'dtype': 'bool' }); someBy.assign( x, 2, y, { 'dims': '5' }, clbk ); // $ExpectError @@ -498,7 +498,7 @@ function clbk( v: any ): boolean { { const x = zeros( [ 2, 2 ] ); const y = empty( [], { - 'dtype': 'int32' + 'dtype': 'bool' }); someBy.assign(); // $ExpectError