From 96241d94074655c2baaba2b1eab753d56d2a1dc8 Mon Sep 17 00:00:00 2001 From: gururaj1512 Date: Mon, 30 Dec 2024 19:47:31 +0000 Subject: [PATCH] feat: add ndarray/find --- 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 --- --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: na - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: na --- --- .../@stdlib/ndarray/find/README.md | 225 ++++++++ .../ndarray/find/benchmark/benchmark.1d.js | 142 +++++ .../ndarray/find/benchmark/benchmark.2d.js | 153 ++++++ .../@stdlib/ndarray/find/docs/repl.txt | 46 ++ .../ndarray/find/docs/types/index.d.ts | 416 ++++++++++++++ .../@stdlib/ndarray/find/docs/types/test.ts | 151 +++++ .../@stdlib/ndarray/find/examples/index.js | 38 ++ .../@stdlib/ndarray/find/lib/index.js | 55 ++ .../@stdlib/ndarray/find/lib/main.js | 156 ++++++ .../@stdlib/ndarray/find/package.json | 62 +++ .../@stdlib/ndarray/find/test/test.js | 516 ++++++++++++++++++ 11 files changed, 1960 insertions(+) create mode 100644 lib/node_modules/@stdlib/ndarray/find/README.md create mode 100644 lib/node_modules/@stdlib/ndarray/find/benchmark/benchmark.1d.js create mode 100644 lib/node_modules/@stdlib/ndarray/find/benchmark/benchmark.2d.js create mode 100644 lib/node_modules/@stdlib/ndarray/find/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/ndarray/find/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/ndarray/find/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/ndarray/find/examples/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/find/lib/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/find/lib/main.js create mode 100644 lib/node_modules/@stdlib/ndarray/find/package.json create mode 100644 lib/node_modules/@stdlib/ndarray/find/test/test.js diff --git a/lib/node_modules/@stdlib/ndarray/find/README.md b/lib/node_modules/@stdlib/ndarray/find/README.md new file mode 100644 index 000000000000..19844bc44739 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/find/README.md @@ -0,0 +1,225 @@ + + +# findElement + +> Return the first element in the [ndarray][@stdlib/ndarray/ctor] that passes a test implemented by a predicate function. + +
+ +
+ + + +
+ +## Usage + +```javascript +var findElement = require( '@stdlib/ndarray/find' ); +``` + +#### findElement( x\[, options], predicate\[, thisArg] ) + +Return the first element in the [ndarray][@stdlib/ndarray/ctor] that passes a test implemented by a predicate function. + + + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); + +function predicate( z ) { + return z > 6.0; +} + +var buffer = 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 ] ); +var shape = [ 2, 3 ]; +var strides = [ 6, 1 ]; +var offset = 1; + +var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +// returns + +var arr = ndarray2array( x ); +// returns [ [ 2.0, 3.0, 4.0 ], [ 8.0, 9.0, 10.0 ] ] + +var y = findElement( x, predicate ); +// returns 8.0 +``` + +The function accepts the following arguments: + +- **x**: input [ndarray][@stdlib/ndarray/ctor]. +- **options**: function options _(optional)_. +- **predicate**: predicate function. +- **thisArg**: predicate function execution context _(optional)_. + +The function accepts the following options: + +- **order**: index iteration order. By default, the function iterates over elements according to the [layout order][@stdlib/ndarray/orders] of the provided [ndarray][@stdlib/ndarray/ctor]. Accordingly, for row-major input [ndarrays][@stdlib/ndarray/ctor], the last dimension indices increment fastest. For column-major input [ndarrays][@stdlib/ndarray/ctor], the first dimension indices increment fastest. To override the inferred order and ensure that indices increment in a specific manner, regardless of the input [ndarray][@stdlib/ndarray/ctor]'s layout order, explicitly set the iteration order. Note, however, that iterating according to an order which does not match that of the input [ndarray][@stdlib/ndarray/ctor] may, in some circumstances, result in performance degradation due to cache misses. Must be either `'row-major'` or `'column-major'`. + +By default, the output element's [data type][@stdlib/ndarray/dtypes] is inferred from the input [ndarray][@stdlib/ndarray/ctor]. + + + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); + +function predicate( z ) { + return z > 3.0; +} + +var buffer = 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 ] ); +var shape = [ 2, 3 ]; +var strides = [ 6, 1 ]; +var offset = 1; + +var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +// returns + +var arr = ndarray2array( x ); +// returns [ [ 2.0, 3.0, 4.0 ], [ 8.0, 9.0, 10.0 ] ] + +var opts = { + 'order': 'column-major' +}; +var y = findElement( x, opts, predicate ); +// returns 8.0 + +opts = { + 'order': 'row-major' +}; +y = findElement( x, opts, predicate ); +// returns 4.0 +``` + +To set the `predicate` function execution context, provide a `thisArg`. + + + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); + +function predicate( z ) { + this.count += 1; + return z > 6.0; +} + +var buffer = 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 ] ); +var shape = [ 2, 3 ]; +var strides = [ 6, 1 ]; +var offset = 1; + +var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +// returns + +var arr = ndarray2array( x ); +// returns [ [ 2.0, 3.0, 4.0 ], [ 8.0, 9.0, 10.0 ] ] + +var ctx = { + 'count': 0 +}; +var y = findElement( x, predicate, ctx ); +// returns 8.0 + +var count = ctx.count; +// returns 4 +``` + +The `predicate` function is provided the following arguments: + +- **value**: current array element. +- **indices**: current array element indices. +- **arr**: the input [ndarray][@stdlib/ndarray/ctor]. + +
+ + + +
+ +## Notes + +- The function returns **NaN** if no element in ndarray passes test implemented by the predicate function. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var naryFunction = require( '@stdlib/utils/nary-function' ); +var array = require( '@stdlib/ndarray/array' ); +var isPositive = require( '@stdlib/assert/is-positive-number' ).isPrimitive; +var findElement = require( '@stdlib/ndarray/find' ); + +var buffer = discreteUniform( 10, -100, 100, { + 'dtype': 'generic' +}); +var x = array( buffer, { + 'shape': [ 5, 2 ], + 'dtype': 'generic' +}); +console.log( ndarray2array( x ) ); + +var y = findElement( x, naryFunction( isPositive, 1 ) ); +console.log( y ); +``` + +
+ + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/find/benchmark/benchmark.1d.js b/lib/node_modules/@stdlib/ndarray/find/benchmark/benchmark.1d.js new file mode 100644 index 000000000000..1a6fa1fed757 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/find/benchmark/benchmark.1d.js @@ -0,0 +1,142 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var isInteger = require( '@stdlib/assert/is-integer' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var pkg = require( './../package.json' ).name; +var findElement = require( './../lib' ); + + +// VARIABLES // + +var xtypes = [ 'generic' ]; +var orders = [ 'row-major', 'column-major' ]; + + +// FUNCTIONS // + +/** +* Predicate function. +* +* @private +* @param {number} value - array element +* @param {NonNegativeIntegerArray} indices - element indices +* @param {ndarray} arr - input array +* @returns {boolean} result +*/ +function predicate( value ) { + return value > 0.0; +} + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @param {NonNegativeIntegerArray} shape - ndarray shape +* @param {string} xtype - input ndarray data type +* @param {string} order - ndarray memory layout +* @returns {Function} benchmark function +*/ +function createBenchmark( len, shape, xtype, order ) { + var strides; + var xbuf; + var x; + + xbuf = discreteUniform( len, -100, 100, { + 'dtype': xtype + }); + strides = shape2strides( shape, order ); + x = ndarray( xtype, xbuf, shape, strides, 0, order ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var y; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = findElement( x, predicate ); + if ( isnan( y ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( !isInteger( y ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var ord; + var sh; + var t1; + var f; + var i; + var j; + var k; + + min = 1; // 10^min + max = 6; // 10^max + + for ( k = 0; k < orders.length; k++ ) { + ord = orders[ k ]; + for ( j = 0; j < xtypes.length; j++ ) { + t1 = xtypes[ j ]; + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + + sh = [ len ]; + f = createBenchmark( len, sh, t1, ord ); + bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',yorder='+ord+',xtype='+t1, f ); + } + } + } +} + +main(); diff --git a/lib/node_modules/@stdlib/ndarray/find/benchmark/benchmark.2d.js b/lib/node_modules/@stdlib/ndarray/find/benchmark/benchmark.2d.js new file mode 100644 index 000000000000..a3cf1d91b304 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/find/benchmark/benchmark.2d.js @@ -0,0 +1,153 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var isInteger = require( '@stdlib/assert/is-integer' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var pkg = require( './../package.json' ).name; +var findElement = require( './../lib' ); + + +// VARIABLES // + +var xtypes = [ 'generic' ]; +var orders = [ 'row-major', 'column-major' ]; + + +// FUNCTIONS // + +/** +* Predicate function. +* +* @private +* @param {number} value - array element +* @param {NonNegativeIntegerArray} indices - element indices +* @param {ndarray} arr - input array +* @returns {boolean} result +*/ +function predicate( value ) { + return value > 0.0; +} + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - ndarray length +* @param {NonNegativeIntegerArray} shape - ndarray shape +* @param {string} xtype - input ndarray data type +* @param {string} order - ndarray memory layout +* @returns {Function} benchmark function +*/ +function createBenchmark( len, shape, xtype, order ) { + var strides; + var xbuf; + var x; + var y; + + xbuf = discreteUniform( len, -100, 100, { + 'dtype': xtype + }); + strides = shape2strides( shape, order ); + x = ndarray( xtype, xbuf, shape, strides, 0, order ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = findElement( x, predicate ); + if ( isnan( y ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( !isInteger( y ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var ord; + var sh; + var t1; + var f; + var i; + var j; + var k; + + min = 1; // 10^min + max = 6; // 10^max + + for ( k = 0; k < orders.length; k++ ) { + ord = orders[ k ]; + for ( j = 0; j < xtypes.length; j++ ) { + t1 = xtypes[ j ]; + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + + sh = [ len/2, 2 ]; + f = createBenchmark( len, sh, t1, ord ); + bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',yorder='+ord+',xtype='+t1, f ); + + sh = [ 2, len/2 ]; + f = createBenchmark( len, sh, t1, ord ); + bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',yorder='+ord+',xtype='+t1, f ); + + len = floor( sqrt( len ) ); + sh = [ len, len ]; + len *= len; + f = createBenchmark( len, sh, t1, ord ); + bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',yorder='+ord+',xtype='+t1, f ); + } + } + } +} + +main(); diff --git a/lib/node_modules/@stdlib/ndarray/find/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/find/docs/repl.txt new file mode 100644 index 000000000000..9e5115e1bf20 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/find/docs/repl.txt @@ -0,0 +1,46 @@ + +{{alias}}( x[, options], predicate[, thisArg] ) + Return the first element in the ndarray that passes a test implemented by + a predicate function. + + The predicate function is provided the following arguments: + + - value: current array element. + - indices: current array element indices. + - arr: the input ndarray. + + Parameters + ---------- + x: ndarray + Input ndarray. + + options: Object (optional) + Function options. + + options.order: string (optional) + Index iteration order. By default, the function iterates over elements + according to the layout order of the provided array. Accordingly, for + row-major input arrays, the last dimension indices increment fastest. + For column-major input arrays, the first dimension indices increment + fastest. To override the inferred order and ensure that indices + increment in a specific manor, regardless of the input array's layout + order, explicitly set the iteration order. Note, however, that iterating + according to an order which does not match that of the input array may, + in some circumstances, result in performance degradation due to cache + misses. Must be either 'row-major' or 'column-major'. + + predicate: Function + Predicate function. + + thisArg: any (optional) + Predicate function execution context. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ); + > function f( v ) { return v > 0.0; }; + > var y = {{alias}}( x, f ) + 1.0 + + See Also + -------- diff --git a/lib/node_modules/@stdlib/ndarray/find/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/find/docs/types/index.d.ts new file mode 100644 index 000000000000..83da1dcb641a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/find/docs/types/index.d.ts @@ -0,0 +1,416 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 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 + +/// + +/* eslint-disable max-lines */ + +import { typedndarray, Order, float64ndarray, float32ndarray, complex128ndarray, complex64ndarray, int32ndarray, int16ndarray, int8ndarray, uint32ndarray, uint16ndarray, uint8ndarray, uint8cndarray, boolndarray, genericndarray } from '@stdlib/types/ndarray'; +import { Complex64, Complex128 } from '@stdlib/types/complex'; + +/** +* Returns a boolean indicating whether an element passes a test. +* +* @returns boolean indicating whether an element passes a test +*/ +type Nullary = ( this: V ) => boolean; + +/** +* Returns a boolean indicating whether an element passes a test. +* +* @param value - current array element +* @returns boolean indicating whether an element passes a test +*/ +type Unary = ( this: V, 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 element passes a test +*/ +type Binary = ( this: V, 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 element passes a test +*/ +type Ternary = ( this: V, 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 element passes a test +*/ +type Predicate = Nullary | Unary | Binary | Ternary; + +/** +* Interface describing function options. +*/ +interface OrderOptions { + /** + * Index iteration order. + * + * ## Notes + * + * - By default, the function iterates over elements according to the layout order of the provided ndarray. Accordingly, for row-major input ndarrays, the last dimension indices increment fastest. For column-major input ndarrays, the first dimension indices increment fastest. To override the inferred order and ensure that indices increment in a specific manor, regardless of the input ndarray's layout order, explicitly set the iteration order. Note, however, that iterating according to an order which does not match that of the input ndarray may, in some circumstances, result in performance degradation due to cache misses. + */ + order: Order; +} + +/** +* Return the first element in the ndarray that passes a test implemented by a predicate function. +* +* @param x - input ndarray +* @param predicate - predicate function +* @param thisArg - predicate function execution context +* @returns output number +* +* @example +* var isEven = require( '@stdlib/assert/is-even' ).isPrimitive; +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var buffer = 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 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 5, 2 ]; +* var offset = 0; +* +* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ 1.0, 3.0, 5.0 ], [ 6.0, 8.0, 10.0 ] ] +* +* var y = findElement( x, isEven ); +* // returns 6.0 +*/ +declare function findElement( x: float64ndarray | float32ndarray | int32ndarray | int16ndarray | int8ndarray | uint32ndarray | uint16ndarray | uint8ndarray | uint8cndarray, predicate: Predicate, thisArg?: ThisParameterType> ): number; + +/** +* Return the first element in the ndarray that passes a test implemented by a predicate function. +* +* @param x - input ndarray +* @param predicate - predicate function +* @param thisArg - predicate function execution context +* @returns output number +* +* @example +* var Complex64Array = require( '@stdlib/array/complex64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* function predicate( z, idx ) { +* return idx[ 0 ] > 0; +* } +* +* var buffer = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 3, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'complex64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var y = findElement( x, predicate ); +* // Complex64 { re: 7, im: 8 } +*/ +declare function findElement( x: complex64ndarray, predicate: Predicate, thisArg?: ThisParameterType> ): Complex64; + +/** +* Return the first element in the ndarray that passes a test implemented by a predicate function. +* +* @param x - input ndarray +* @param predicate - predicate function +* @param thisArg - predicate function execution context +* @returns output number +* +* @example +* var Complex128Array = require( '@stdlib/array/complex128' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* function predicate( z, idx ) { +* return idx[ 0 ] > 0; +* } +* +* var buffer = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 3, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'complex128', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var y = findElement( x, predicate ); +* // Complex128 { re: 7, im: 8 } +*/ +declare function findElement( x: complex128ndarray, predicate: Predicate, thisArg?: ThisParameterType> ): Complex128; + +/** +* Return the first element in the ndarray that passes a test implemented by a predicate function. +* +* @param x - input ndarray +* @param predicate - predicate function +* @param thisArg - predicate function execution context +* @returns output number +* +* @example +* var isEven = require( '@stdlib/assert/is-even' ).isPrimitive; +* var BooleanArray = require( '@stdlib/array/bool' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function predicate( z ) { +* return z; +* } +* +* var buffer = new BooleanArray( [ false, true, false, true, false, true, false, true, false, true, false, true ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 5, 2 ]; +* var offset = 0; +* +* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ false, false, false ], [ true, true, true ] ] +* +* var y = findElement( x, predicate ); +* // returns true +*/ +declare function findElement( x: boolndarray, predicate: Predicate, thisArg?: ThisParameterType> ): boolean; + +/** +* Return the first element in the ndarray that passes a test implemented by a predicate function. +* +* @param x - input ndarray +* @param predicate - predicate function +* @param thisArg - predicate function execution context +* @returns output number +* +* @example +* var isEven = require( '@stdlib/assert/is-even' ).isPrimitive; +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ]; +* var shape = [ 2, 3 ]; +* var strides = [ 5, 2 ]; +* var offset = 0; +* +* var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ 1.0, 3.0, 5.0 ], [ 6.0, 8.0, 10.0 ] ] +* +* var y = findElement( x, isEven ); +* // returns 6.0 +*/ +declare function findElement( x: genericndarray, predicate: Predicate, thisArg?: ThisParameterType> ): number; + +/** +* Return the first element in the ndarray that passes a test implemented by a predicate function. +* +* @param x - input ndarray +* @param options - function options +* @param options.order - iteration order +* @param predicate - predicate function +* @param thisArg - predicate function execution context +* @returns output number +* +* @example +* var isEven = require( '@stdlib/assert/is-even' ).isPrimitive; +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var buffer = 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 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 5, 2 ]; +* var offset = 0; +* +* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ 1.0, 3.0, 5.0 ], [ 6.0, 8.0, 10.0 ] ] +* +* var opts = { +* 'order': 'row-major' +* }; +* var y = findElement( x, opts, isEven ); +* // returns 6.0 +*/ +declare function findElement( x: float64ndarray | float32ndarray | int32ndarray | int16ndarray | int8ndarray | uint32ndarray | uint16ndarray | uint8ndarray | uint8cndarray, options: OrderOptions, predicate: Predicate, thisArg?: ThisParameterType> ): number; + +/** +* Return the first element in the ndarray that passes a test implemented by a predicate function. +* +* @param x - input ndarray +* @param options - function options +* @param options.order - iteration order +* @param predicate - predicate function +* @param thisArg - predicate function execution context +* @returns output number +* +* @example +* var Complex64Array = require( '@stdlib/array/complex64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* function predicate( z, idx ) { +* return idx[ 0 ] > 0; +* } +* +* var buffer = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 3, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'complex64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var opts = { +* 'order': 'row-major' +* }; +* var y = findElement( x, opts, predicate ); +* // Complex64 { re: 7, im: 8 } +*/ +declare function findElement( x: complex64ndarray, options: OrderOptions, predicate: Predicate, thisArg?: ThisParameterType> ): Complex64; + +/** +* Return the first element in the ndarray that passes a test implemented by a predicate function. +* +* @param x - input ndarray +* @param options - function options +* @param options.order - iteration order +* @param predicate - predicate function +* @param thisArg - predicate function execution context +* @returns output number +* +* @example +* var Complex128Array = require( '@stdlib/array/complex128' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* function predicate( z, idx ) { +* return idx[ 0 ] > 0; +* } +* +* var buffer = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 3, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'complex128', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var opts = { +* 'order': 'row-major' +* }; +* var y = findElement( x, opts, predicate ); +* // Complex128 { re: 7, im: 8 } +*/ +declare function findElement( x: complex128ndarray, options: OrderOptions, predicate: Predicate, thisArg?: ThisParameterType> ): Complex128; + +/** +* Return the first element in the ndarray that passes a test implemented by a predicate function. +* +* @param x - input ndarray +* @param options - function options +* @param options.order - iteration order +* @param predicate - predicate function +* @param thisArg - predicate function execution context +* @returns output number +* +* @example +* var isEven = require( '@stdlib/assert/is-even' ).isPrimitive; +* var BooleanArray = require( '@stdlib/array/bool' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function predicate( z ) { +* return z; +* } +* +* var buffer = new BooleanArray( [ false, true, false, true, false, true, false, true, false, true, false, true ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 5, 2 ]; +* var offset = 0; +* +* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ false, false, false ], [ true, true, true ] ] +* +* var opts = { +* 'order': 'row-major' +* }; +* var y = findElement( x, opts, predicate ); +* // returns true +*/ +declare function findElement( x: boolndarray, options: OrderOptions, predicate: Predicate, thisArg?: ThisParameterType> ): boolean; + +/** +* Return the first element in the ndarray that passes a test implemented by a predicate function. +* +* @param x - input ndarray +* @param options - function options +* @param options.order - iteration order +* @param predicate - predicate function +* @param thisArg - predicate function execution context +* @returns output number +* +* @example +* var isEven = require( '@stdlib/assert/is-even' ).isPrimitive; +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ]; +* var shape = [ 2, 3 ]; +* var strides = [ 5, 2 ]; +* var offset = 0; +* +* var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ 1.0, 3.0, 5.0 ], [ 6.0, 8.0, 10.0 ] ] +* +* var opts = { +* 'order': 'row-major' +* }; +* var y = find( x, opts, isEven ); +* // returns 6.0 +*/ +declare function findElement( x: genericndarray, options: OrderOptions, predicate: Predicate, thisArg?: ThisParameterType> ): number; + + +// EXPORTS // + +export = findElement; diff --git a/lib/node_modules/@stdlib/ndarray/find/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/find/docs/types/test.ts new file mode 100644 index 000000000000..22e9bb3f883f --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/find/docs/types/test.ts @@ -0,0 +1,151 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 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 empty = require( '@stdlib/ndarray/base/empty' ); +import zeros = require( '@stdlib/ndarray/base/zeros' ); +import findElement = require( './index' ); + +/** +* Predicate function. +* +* @param x - input value +* @returns result +*/ +function predicate( x: any ): boolean { + return Boolean( x ); +} + +// The function returns an number... +{ + const sh = [ 2, 2 ]; + const ord = 'row-major'; + + findElement( zeros( 'float64', sh, ord ), predicate ); // $ExpectType number + findElement( zeros( 'float64', sh, ord ), predicate, {} ); // $ExpectType number + findElement( zeros( 'float32', sh, ord ), predicate ); // $ExpectType number + findElement( zeros( 'float32', sh, ord ), predicate, {} ); // $ExpectType number + findElement( zeros( 'complex64', sh, ord ), predicate ); // $ExpectType Complex64 + findElement( zeros( 'complex64', sh, ord ), predicate, {} ); // $ExpectType Complex64 + findElement( zeros( 'complex128', sh, ord ), predicate ); // $ExpectType Complex128 + findElement( zeros( 'complex128', sh, ord ), predicate, {} ); // $ExpectType Complex128 + findElement( zeros( 'int32', sh, ord ), predicate ); // $ExpectType number + findElement( zeros( 'int32', sh, ord ), predicate, {} ); // $ExpectType number + findElement( zeros( 'int16', sh, ord ), predicate ); // $ExpectType number + findElement( zeros( 'int16', sh, ord ), predicate, {} ); // $ExpectType number + findElement( zeros( 'int8', sh, ord ), predicate ); // $ExpectType number + findElement( zeros( 'int8', sh, ord ), predicate, {} ); // $ExpectType number + findElement( zeros( 'uint32', sh, ord ), predicate ); // $ExpectType number + findElement( zeros( 'uint32', sh, ord ), predicate, {} ); // $ExpectType number + findElement( zeros( 'uint16', sh, ord ), predicate ); // $ExpectType number + findElement( zeros( 'uint16', sh, ord ), predicate, {} ); // $ExpectType number + findElement( zeros( 'uint8', sh, ord ), predicate ); // $ExpectType number + findElement( zeros( 'uint8', sh, ord ), predicate, {} ); // $ExpectType number + findElement( zeros( 'uint8c', sh, ord ), predicate ); // $ExpectType number + findElement( zeros( 'uint8c', sh, ord ), predicate, {} ); // $ExpectType number + findElement( empty( 'bool', sh, ord ), predicate ); // $ExpectType boolean + findElement( empty( 'bool', sh, ord ), predicate, {} ); // $ExpectType boolean + findElement( zeros( 'generic', sh, ord ), predicate ); // $ExpectType number + findElement( zeros( 'generic', sh, ord ), predicate, {} ); // $ExpectType number + + findElement( zeros( 'float64', sh, ord ), { 'order': ord }, predicate ); // $ExpectType number + findElement( zeros( 'float64', sh, ord ), { 'order': ord }, predicate, {} ); // $ExpectType number + findElement( zeros( 'float32', sh, ord ), { 'order': ord }, predicate ); // $ExpectType number + findElement( zeros( 'float32', sh, ord ), { 'order': ord }, predicate, {} ); // $ExpectType number + findElement( zeros( 'complex64', sh, ord ), { 'order': ord }, predicate ); // $ExpectType Complex64 + findElement( zeros( 'complex64', sh, ord ), { 'order': ord }, predicate, {} ); // $ExpectType Complex64 + findElement( zeros( 'complex128', sh, ord ), { 'order': ord }, predicate ); // $ExpectType Complex128 + findElement( zeros( 'complex128', sh, ord ), { 'order': ord }, predicate, {} ); // $ExpectType Complex128 + findElement( zeros( 'int32', sh, ord ), { 'order': ord }, predicate ); // $ExpectType number + findElement( zeros( 'int32', sh, ord ), { 'order': ord }, predicate, {} ); // $ExpectType number + findElement( zeros( 'int16', sh, ord ), { 'order': ord }, predicate ); // $ExpectType number + findElement( zeros( 'int16', sh, ord ), { 'order': ord }, predicate, {} ); // $ExpectType number + findElement( zeros( 'int8', sh, ord ), { 'order': ord }, predicate ); // $ExpectType number + findElement( zeros( 'int8', sh, ord ), { 'order': ord }, predicate, {} ); // $ExpectType number + findElement( zeros( 'uint32', sh, ord ), { 'order': ord }, predicate ); // $ExpectType number + findElement( zeros( 'uint32', sh, ord ), { 'order': ord }, predicate, {} ); // $ExpectType number + findElement( zeros( 'uint16', sh, ord ), { 'order': ord }, predicate ); // $ExpectType number + findElement( zeros( 'uint16', sh, ord ), { 'order': ord }, predicate, {} ); // $ExpectType number + findElement( zeros( 'uint8', sh, ord ), { 'order': ord }, predicate ); // $ExpectType number + findElement( zeros( 'uint8', sh, ord ), { 'order': ord }, predicate, {} ); // $ExpectType number + findElement( zeros( 'uint8c', sh, ord ), { 'order': ord }, predicate ); // $ExpectType number + findElement( zeros( 'uint8c', sh, ord ), { 'order': ord }, predicate, {} ); // $ExpectType number + findElement( empty( 'bool', sh, ord ), { 'order': ord }, predicate ); // $ExpectType boolean + findElement( empty( 'bool', sh, ord ), { 'order': ord }, predicate, {} ); // $ExpectType boolean + findElement( zeros( 'generic', sh, ord ), { 'order': ord }, predicate ); // $ExpectType number + findElement( zeros( 'generic', sh, ord ), { 'order': ord }, predicate, {} ); // $ExpectType number +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + findElement( 5, predicate ); // $ExpectError + findElement( true, predicate ); // $ExpectError + findElement( false, predicate ); // $ExpectError + findElement( null, predicate ); // $ExpectError + findElement( undefined, predicate ); // $ExpectError + findElement( {}, predicate ); // $ExpectError + findElement( [ 1 ], predicate ); // $ExpectError + findElement( ( x: number ): number => x, predicate ); // $ExpectError +} + +// The compiler throws an error if the function is provided a callback which is not a function... +{ + const x = zeros( 'generic', [ 2, 2 ], 'row-major' ); + + findElement( x, '5' ); // $ExpectError + findElement( x, true ); // $ExpectError + findElement( x, false ); // $ExpectError + findElement( x, null ); // $ExpectError + findElement( x, undefined ); // $ExpectError + findElement( x, {} ); // $ExpectError + findElement( x, [ 1 ] ); // $ExpectError +} + +// The compiler throws an error if the function is provided an options argument which is not an object... +{ + const x = zeros( 'generic', [ 2, 2 ], 'row-major' ); + + findElement( x, '10', predicate, {} ); // $ExpectError + findElement( x, 10, predicate, {} ); // $ExpectError + findElement( x, false, predicate, {} ); // $ExpectError + findElement( x, true, predicate, {} ); // $ExpectError + findElement( x, [], predicate, {} ); // $ExpectError + findElement( x, ( x: number ): number => x, predicate, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided an `order` option which is not a valid order... +{ + const x = zeros( 'generic', [ 2, 2 ], 'row-major' ); + + findElement( x, { 'order': '10' }, predicate ); // $ExpectError + findElement( x, { 'order': 10 }, predicate ); // $ExpectError + findElement( x, { 'order': null }, predicate ); // $ExpectError + findElement( x, { 'order': false }, predicate ); // $ExpectError + findElement( x, { 'order': true }, predicate ); // $ExpectError + findElement( x, { 'order': [] }, predicate ); // $ExpectError + findElement( x, { 'order': {} }, predicate ); // $ExpectError + findElement( x, { 'order': ( x: number ): number => x }, predicate ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + findElement(); // $ExpectError + findElement( zeros( 'float64', [ 2, 2 ], 'row-major' ) ); // $ExpectError + findElement( zeros( 'float64', [ 2, 2 ], 'row-major' ), {}, ( x: number ): number => x, {}, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/find/examples/index.js b/lib/node_modules/@stdlib/ndarray/find/examples/index.js new file mode 100644 index 000000000000..878d4bad6a1a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/find/examples/index.js @@ -0,0 +1,38 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var naryFunction = require( '@stdlib/utils/nary-function' ); +var array = require( '@stdlib/ndarray/array' ); +var isPositive = require( '@stdlib/assert/is-positive-number' ).isPrimitive; +var findElement = require( '@stdlib/ndarray/find' ); + +var buffer = discreteUniform( 10, -100, 100, { + 'dtype': 'generic' +}); +var x = array( buffer, { + 'shape': [ 5, 2 ], + 'dtype': 'generic' +}); +console.log( ndarray2array( x ) ); + +var y = findElement( x, naryFunction( isPositive, 1 ) ); +console.log( y ); diff --git a/lib/node_modules/@stdlib/ndarray/find/lib/index.js b/lib/node_modules/@stdlib/ndarray/find/lib/index.js new file mode 100644 index 000000000000..d06daee6610a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/find/lib/index.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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'; + +/** +* Return the first element in the ndarray that passes a test implemented by a predicate function. +* +* @module @stdlib/ndarray/find +* +* @example +* var isEven = require( '@stdlib/assert/is-even' ).isPrimitive; +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* var findElement = require( '@stdlib/ndarray/find' ); +* +* var buffer = 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 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 5, 2 ]; +* var offset = 0; +* +* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ 1.0, 3.0, 5.0 ], [ 6.0, 8.0, 10.0 ] ] +* +* var y = findElement( x, isEven ); +* // returns 6.0 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/find/lib/main.js b/lib/node_modules/@stdlib/ndarray/find/lib/main.js new file mode 100644 index 000000000000..29a8367296c9 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/find/lib/main.js @@ -0,0 +1,156 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 isPlainObject = require( '@stdlib/assert/is-plain-object' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var isOrder = require( '@stdlib/ndarray/base/assert/is-order' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var zeros = require( '@stdlib/array/base/zeros' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var numel = require( '@stdlib/ndarray/base/numel' ); +var nextCartesianIndex = require( '@stdlib/ndarray/base/next-cartesian-index' ).assign; +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Return the first element in the ndarray that passes a test implemented by a predicate function. +* +* @param {ndarray} x - input ndarray +* @param {Options} [options] - function options +* @param {boolean} [options.order] - index iteration order +* @param {Callback} predicate - predicate function +* @param {*} [thisArg] - predicate execution context +* @throws {TypeError} first argument must be an ndarray-like object +* @throws {TypeError} options argument must be an object +* @throws {TypeError} callback argument must be a function +* @returns {number} result +* +* @example +* var isEven = require( '@stdlib/assert/is-even' ).isPrimitive; +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var buffer = 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]); +* var shape = [2, 3]; +* var strides = [ 5, 2 ]; +* var offset = 0; +* +* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ 1.0, 3.0, 5.0 ], [ 6.0, 8.0, 10.0 ] ] +* +* var y = findElement( x, isEven ); +* // returns 6.0 +*/ +function findElement( x, options, predicate, thisArg ) { + var hasOpts; + var ndims; + var clbk; + var opts; + var ctx; + var ord; + var dim; + var idx; + var sh; + var N; + var v; + var i; + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object. Value: `%s`.', x ) ); + } + if ( arguments.length < 3 ) { + clbk = options; + } else if ( arguments.length === 3 ) { + if ( isFunction( options ) ) { + clbk = options; + ctx = predicate; + } else { + hasOpts = true; + opts = options; + clbk = predicate; + } + } else { + hasOpts = true; + opts = options; + clbk = predicate; + ctx = thisArg; + } + if ( !isFunction( clbk ) ) { + throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', clbk ) ); + } + if ( hasOpts ) { + if ( !isPlainObject( opts ) ) { + throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', opts ) ); + } + if ( hasOwnProp( opts, 'order' ) ) { + if ( !isOrder( opts.order ) ) { + throw new TypeError( format( 'invalid option. `%s` option must be a recognized order. Option: `%s`.', 'order', opts.order ) ); + } + ord = opts.order; + } + } + // Resolve the iteration order: + if ( ord === void 0 ) { + ord = getOrder( x ); + } + // Resolve the input array shape: + sh = getShape( x ); + + // Compute the number of array elements: + N = numel( sh ); + + // Retrieve the number of dimensions: + ndims = sh.length; + + // Resolve the dimension in which indices should iterate fastest: + if ( ord === 'row-major' ) { + dim = ndims - 1; + } else { // ord === 'column-major' + dim = 0; + } + // Initialize an index array workspace: + idx = zeros( ndims ); + + // Check elements according to a predicate function... + for ( i = 0; i < N; i++ ) { + if ( i > 0 ) { + idx = nextCartesianIndex( sh, ord, idx, dim, idx ); + } + v = x.get.apply( x, idx ); + if ( clbk.call( ctx, v, idx.slice(), x ) ) { + return v; + } + } + return NaN; +} + + +// EXPORTS // + +module.exports = findElement; diff --git a/lib/node_modules/@stdlib/ndarray/find/package.json b/lib/node_modules/@stdlib/ndarray/find/package.json new file mode 100644 index 000000000000..a9cc7a94dee6 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/find/package.json @@ -0,0 +1,62 @@ +{ + "name": "@stdlib/ndarray/find", + "version": "0.0.0", + "description": "Return the first element in the ndarray that passes a test implemented by a predicate function.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "strided", + "array", + "ndarray", + "find", + "reject", + "select", + "take" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/ndarray/find/test/test.js b/lib/node_modules/@stdlib/ndarray/find/test/test.js new file mode 100644 index 000000000000..f386a59505f4 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/find/test/test.js @@ -0,0 +1,516 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isnan = require( '@stdlib/assert/is-nan' ); +var ones = require( '@stdlib/array/ones' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); +var strides2offset = require( '@stdlib/ndarray/base/strides2offset' ); +var Float64Array = require( '@stdlib/array/float64' ); +var findElement = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof findElement, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + true, + false, + null, + void 0, + [], + {}, + function noop() {}, + { + 'data': true + } + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided ' + values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + findElement( value, predicate ); + }; + } + + function predicate( z ) { + return ( z > 0.0 ); + } +}); + +tape( 'the function throws an error if a callback argument which is not a function', function test( t ) { + var values; + var x; + var i; + + values = [ + '5', + 5, + true, + false, + null, + void 0, + {}, + [] + ]; + x = ndarray( 'generic', ones( 4, 'generic' ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided ' + values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + findElement( x, value ); + }; + } +}); + +tape( 'the function throws an error if callback argument which is not a function (options)', function test( t ) { + var values; + var opts; + var x; + var i; + + values = [ + '5', + 5, + true, + false, + null, + void 0, + {}, + [] + ]; + x = ndarray( 'generic', ones( 4, 'generic' ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + opts = { + 'order': 'row-major' + }; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided ' + values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + findElement( x, opts, value ); + }; + } +}); + +tape( 'the function throws an error if provided an options argument which is not an object', function test( t ) { + var values; + var x; + var i; + + values = [ + '5', + 5, + true, + false, + null, + void 0, + [] + ]; + x = ndarray( 'generic', ones( 4, 'generic' ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+ values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + findElement( x, value, predicate ); + }; + } + + function predicate( z ) { + return ( z > 0.0 ); + } +}); + +tape( 'the function throws an error if provided an invalid `order` option', function test( t ) { + var values; + var x; + var i; + + values = [ + 'foo', + 'bar', + 1, + NaN, + true, + false, + void 0, + null, + [], + {}, + function noop() {} + ]; + x = ndarray( 'generic', ones( 4, 'generic' ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+ values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var opts = { + 'order': value + }; + findElement( x, opts, predicate ); + }; + } + + function predicate( z ) { + return ( z > 0.0 ); + } +}); + +tape( 'the function finds element in array according to a predicate function (row-major)', function test( t ) { + var expected; + var ord; + var buf; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 1, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + buf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + x = ndarray( dt, buf, sh, st, o, ord ); + y = findElement( x, predicate ); + + expected = 1.0; + t.strictEqual( y, expected, 'returns expected value' ); + + buf = new Float64Array( [ -1.0, 2.0, -3.0, 4.0 ] ); + x = ndarray( dt, buf, sh, st, o, ord ); + y = findElement( x, predicate ); + + expected = 2.0; + t.strictEqual( y, expected, 'returns expected value' ); + + t.end(); + + function predicate( z ) { + return ( z > 0.0 ); + } +}); + +tape( 'the function finds element in array according to a predicate function (column-major)', function test( t ) { + var expected; + var ord; + var buf; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'column-major'; + sh = [ 2, 1, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + buf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + x = ndarray( dt, buf, sh, st, o, ord ); + y = findElement( x, predicate ); + + expected = 1.0; + t.strictEqual( y, expected, 'returns expected value' ); + + buf = new Float64Array( [ -1.0, 2.0, -3.0, 4.0 ] ); + x = ndarray( dt, buf, sh, st, o, ord ); + y = findElement( x, predicate ); + + expected = 2.0; + t.strictEqual( y, expected, 'returns expected value' ); + + t.end(); + + function predicate( z ) { + return ( z > 0.0 ); + } +}); + +tape( 'the function finds element in array according to a predicate function (row-major, contiguous, options)', function test( t ) { + var expected; + var opts; + var ord; + var buf; + var sh; + var st; + var dt; + var o; + var x; + var y; + + buf = new Float64Array( [ -1.0, 2.0, 3.0, 4.0 ] ); + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 1, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + x = ndarray( dt, buf, sh, st, o, ord ); + + opts = {}; + y = findElement( x, opts, predicate ); + + expected = 2.0; + t.strictEqual( y, expected, 'returns expected value' ); + + opts = { + 'order': 'column-major' + }; + y = findElement( x, opts, predicate ); + + expected = 3.0; + t.strictEqual( y, expected, 'returns expected value' ); + + t.end(); + + function predicate( z ) { + return ( z > 0.0 ); + } +}); + +tape( 'the function finds element in array according to a predicate function (column-major, contiguous, options)', function test( t ) { + var expected; + var opts; + var ord; + var buf; + var sh; + var st; + var dt; + var o; + var x; + var y; + + buf = new Float64Array( [ -1.0, 2.0, 3.0, 4.0 ] ); + dt = 'float64'; + ord = 'column-major'; + sh = [ 2, 1, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + x = ndarray( dt, buf, sh, st, o, ord ); + + opts = {}; + y = findElement( x, opts, predicate ); + + expected = 2.0; + t.strictEqual( y, expected, 'returns expected value' ); + + opts = { + 'order': 'row-major' + }; + y = findElement( x, opts, predicate ); + + expected = 3.0; + t.strictEqual( y, expected, 'returns expected value' ); + + t.end(); + + function predicate( z ) { + return ( z > 0.0 ); + } +}); + +tape( 'the function supports providing a callback execution context', function test( t ) { + var expected; + var ctx; + var ord; + var buf; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 1, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + buf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + x = ndarray( dt, buf, sh, st, o, ord ); + + ctx = { + 'count': 0 + }; + y = findElement( x, predicate, ctx ); + + expected = 1.0; + t.strictEqual( y, expected, 'returns expected value' ); + t.strictEqual( ctx.count, 1, 'returns expected value' ); + + buf = new Float64Array( [ -1.0, -2.0, -3.0, 4.0 ] ); + x = ndarray( dt, buf, sh, st, o, ord ); + + ctx = { + 'count': 0 + }; + y = findElement( x, predicate, ctx ); + + expected = 4.0; + t.strictEqual( y, expected, 'returns expected value' ); + t.strictEqual( ctx.count, 4, 'returns expected value' ); + + t.end(); + + function predicate( z ) { + this.count += 1; // eslint-disable-line no-invalid-this + return ( z > 0.0 ); + } +}); + +tape( 'the function invokes a provided callback with three arguments', function test( t ) { + var expected; + var indices; + var values; + var arrays; + var ord; + var buf; + var sh; + var st; + var dt; + var o; + var x; + var y; + var i; + + buf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 1, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + x = ndarray( dt, buf, sh, st, o, ord ); + + values = []; + indices = []; + arrays = []; + y = findElement( x, predicate ); + + expected = 4.0; + t.strictEqual( y, expected, 'returns expected value' ); + + expected = [ + [ 0, 0, 0 ], + [ 0, 0, 1 ], + [ 1, 0, 0 ], + [ 1, 0, 1 ] + ]; + t.deepEqual( indices, expected, 'returns expected value' ); + + expected = [ + x, + x, + x, + x + ]; + for ( i = 0; i < expected.length; i++ ) { + t.strictEqual( arrays[ i ], expected[ i ], 'returns expected value' ); + } + + t.end(); + + function predicate( z, idx, arr ) { + values.push( z ); + indices.push( idx ); + arrays.push( arr ); + return ( z > 3.0 ); + } +}); + +tape( 'the function returns NaN if no element in ndarray passes test implemented by the predicate function', function test( t ) { + var ord; + var buf; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 1, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + buf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + x = ndarray( dt, buf, sh, st, o, ord ); + y = findElement( x, predicate ); + + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + dt = 'float64'; + ord = 'column-major'; + sh = [ 2, 1, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + buf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + x = ndarray( dt, buf, sh, st, o, ord ); + y = findElement( x, predicate ); + + t.strictEqual( isnan( y ), true, 'returns expected value' ); + + t.end(); + + function predicate( z ) { + return ( z > 4.0 ); + } +});