From cbed19ac9ddcefd45465bee5a9a2fa87dabe44f6 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Sun, 15 Dec 2024 14:30:10 +0500 Subject: [PATCH 1/7] feat: add readme --- .../@stdlib/ndarray/for-each/README.md | 177 ++++++++++++++++++ .../@stdlib/ndarray/for-each/package.json | 65 +++++++ 2 files changed, 242 insertions(+) create mode 100644 lib/node_modules/@stdlib/ndarray/for-each/README.md create mode 100644 lib/node_modules/@stdlib/ndarray/for-each/package.json diff --git a/lib/node_modules/@stdlib/ndarray/for-each/README.md b/lib/node_modules/@stdlib/ndarray/for-each/README.md new file mode 100644 index 000000000000..e91eaab3f854 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/for-each/README.md @@ -0,0 +1,177 @@ + + +# forEach + +> Invoke a callback function once for each [ndarray][@stdlib/ndarray/ctor] element. + +
+ +
+ + + +
+ +## Usage + +```javascript +var forEach = require( '@stdlib/ndarray/for-each' ); +``` + +#### forEach( x, fcn\[, thisArg] ) + +Invokes a callback function once for each [ndarray][@stdlib/ndarray/ctor] element. + + + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); + +function scale( z ) { + return z * 10.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 + +forEach( x, scale ); + +var arr = ndarray2array( x ); +// returns [ [ 20.0, 30.0, 40.0 ], [ 80.0, 90.0, 100.0 ] ] +``` + +The function accepts the following arguments: + +- **x**: input [ndarray][@stdlib/ndarray/ctor]. +- **fcn**: callback to apply. +- **thisArg**: callback execution context _(optional)_. + +To set the callback 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 scale( z ) { + this.count += 1; + return z * 10.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 ctx = { + 'count': 0 +}; +forEach( x, scale, ctx ); + +var arr = ndarray2array( x ); +// returns [ [ 20.0, 30.0, 40.0 ], [ 80.0, 90.0, 100.0 ] ] + +var count = ctx.count; +// returns 6 +``` + +The callback function is provided the following arguments: + +- **value**: current array element. +- **indices**: current array element indices. +- **arr**: the input [ndarray][@stdlib/ndarray/ctor]. + +
+ + + +
+ +## Notes + +- For very high-dimensional ndarrays which are non-contiguous, one should consider copying the underlying data to contiguous memory before applying a callback function in order to achieve better performance. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var naryFunction = require( '@stdlib/utils/nary-function' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var forEach = require( '@stdlib/ndarray/for-each' ); + +var buffer = discreteUniform( 10, -100, 100, { + 'dtype': 'generic' +}); +var shape = [ 5, 2 ]; +var strides = [ 2, 1 ]; +var offset = 0; +var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); +console.log( ndarray2array( x ) ); + +forEach( x, naryFunction( abs, 1 ) ); +console.log( ndarray2array( x ) ); +``` + +
+ + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/for-each/package.json b/lib/node_modules/@stdlib/ndarray/for-each/package.json new file mode 100644 index 000000000000..a9455c3bd04a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/for-each/package.json @@ -0,0 +1,65 @@ +{ + "name": "@stdlib/ndarray/for-each", + "version": "0.0.0", + "description": "Invoke a callback function once for each ndarray element.", + "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", + "base", + "strided", + "array", + "ndarray", + "unary", + "apply", + "foreach", + "for-each", + "map", + "transform" + ], + "__stdlib__": {} +} From d10605efc7eaa5a51ad9443148fa01ffede00ddd Mon Sep 17 00:00:00 2001 From: headlessNode Date: Sun, 15 Dec 2024 19:25:47 +0500 Subject: [PATCH 2/7] feat: add ndarray/for-each --- .../@stdlib/ndarray/for-each/README.md | 39 ++--- .../for-each/benchmark/benchmark.1d.js | 140 ++++++++++++++++ .../for-each/benchmark/benchmark.2d.js | 152 ++++++++++++++++++ .../@stdlib/ndarray/for-each/docs/repl.txt | 29 ++++ .../ndarray/for-each/docs/types/index.d.ts | 91 +++++++++++ .../ndarray/for-each/docs/types/test.ts | 100 ++++++++++++ .../ndarray/for-each/examples/index.js | 37 +++++ .../@stdlib/ndarray/for-each/lib/index.js | 52 ++++++ .../@stdlib/ndarray/for-each/lib/main.js | 71 ++++++++ .../@stdlib/ndarray/for-each/test/test.js | 138 ++++++++++++++++ 10 files changed, 823 insertions(+), 26 deletions(-) create mode 100644 lib/node_modules/@stdlib/ndarray/for-each/benchmark/benchmark.1d.js create mode 100644 lib/node_modules/@stdlib/ndarray/for-each/benchmark/benchmark.2d.js create mode 100644 lib/node_modules/@stdlib/ndarray/for-each/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/ndarray/for-each/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/ndarray/for-each/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/ndarray/for-each/examples/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/for-each/lib/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/for-each/lib/main.js create mode 100644 lib/node_modules/@stdlib/ndarray/for-each/test/test.js diff --git a/lib/node_modules/@stdlib/ndarray/for-each/README.md b/lib/node_modules/@stdlib/ndarray/for-each/README.md index e91eaab3f854..6959ed300624 100644 --- a/lib/node_modules/@stdlib/ndarray/for-each/README.md +++ b/lib/node_modules/@stdlib/ndarray/for-each/README.md @@ -45,11 +45,8 @@ Invokes a callback function once for each [ndarray][@stdlib/ndarray/ctor] elemen ```javascript var Float64Array = require( '@stdlib/array/float64' ); var ndarray = require( '@stdlib/ndarray/ctor' ); -var ndarray2array = require( '@stdlib/ndarray/to-array' ); - -function scale( z ) { - return z * 10.0; -} +var naryFunction = require( '@stdlib/utils/nary-function' ); +var log = require( '@stdlib/console/log' ); 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 ]; @@ -59,10 +56,7 @@ var offset = 1; var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); // returns -forEach( x, scale ); - -var arr = ndarray2array( x ); -// returns [ [ 20.0, 30.0, 40.0 ], [ 80.0, 90.0, 100.0 ] ] +forEach( x, naryFunction( log, 1 ) ); ``` The function accepts the following arguments: @@ -78,31 +72,25 @@ To set the callback 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 scale( z ) { - this.count += 1; - return z * 10.0; +function accumulate( z ) { + this.sum += z; } 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 ctx = { - 'count': 0 + 'sum': 0 }; -forEach( x, scale, ctx ); - -var arr = ndarray2array( x ); -// returns [ [ 20.0, 30.0, 40.0 ], [ 80.0, 90.0, 100.0 ] ] -var count = ctx.count; -// returns 6 +forEach( x, accumulate, ctx ); +var sum = ctx.sum; +// returns 36 ``` The callback function is provided the following arguments: @@ -133,10 +121,10 @@ The callback function is provided the following arguments: ```javascript var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); -var abs = require( '@stdlib/math/base/special/abs' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); var ndarray2array = require( '@stdlib/ndarray/to-array' ); var naryFunction = require( '@stdlib/utils/nary-function' ); -var ndarray = require( '@stdlib/ndarray/ctor' ); +var log = require( '@stdlib/console/log' ); var forEach = require( '@stdlib/ndarray/for-each' ); var buffer = discreteUniform( 10, -100, 100, { @@ -146,10 +134,9 @@ var shape = [ 5, 2 ]; var strides = [ 2, 1 ]; var offset = 0; var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); -console.log( ndarray2array( x ) ); -forEach( x, naryFunction( abs, 1 ) ); -console.log( ndarray2array( x ) ); +log( ndarray2array( x ) ); +forEach( x, naryFunction( log, 2 ) ); ``` diff --git a/lib/node_modules/@stdlib/ndarray/for-each/benchmark/benchmark.1d.js b/lib/node_modules/@stdlib/ndarray/for-each/benchmark/benchmark.1d.js new file mode 100644 index 000000000000..38ad9ee75c15 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/for-each/benchmark/benchmark.1d.js @@ -0,0 +1,140 @@ +/** +* @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 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 forEach = require( './../lib' ); + + +// VARIABLES // + +var xtypes = [ 'generic' ]; +var orders = [ 'row-major', 'column-major' ]; + + +// FUNCTIONS // + +/** +* Callback invoked for each ndarray element. +* +* @private +* @param {number} value - array element +* @throws {Error} unexpected error +*/ +function fcn( value ) { + if ( isnan( value ) ) { + throw new Error( 'unexpected error' ); + } +} + +/** +* 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 i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + forEach( x, fcn ); + if ( isnan( x.data[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( x.data[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var 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+',xtype='+t1, f ); + } + } + } +} + +main(); diff --git a/lib/node_modules/@stdlib/ndarray/for-each/benchmark/benchmark.2d.js b/lib/node_modules/@stdlib/ndarray/for-each/benchmark/benchmark.2d.js new file mode 100644 index 000000000000..e4a93e8869f5 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/for-each/benchmark/benchmark.2d.js @@ -0,0 +1,152 @@ +/** +* @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 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 forEach = require( './../lib' ); + + +// VARIABLES // + +var xtypes = [ 'generic' ]; +var orders = [ 'row-major', 'column-major' ]; + + +// FUNCTIONS // + +/** +* Callback invoked for each ndarray element. +* +* @private +* @param {number} value - array element +* @throws {Error} unexpected error +*/ +function fcn( value ) { + if ( isnan( value ) ) { + throw new Error( 'unexpected error' ); + } +} + +/** +* 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 i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + forEach( x, fcn ); + if ( isnan( x.data[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( x.data[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var 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+',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+',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+',xtype='+t1, f ); + } + } + } +} + +main(); diff --git a/lib/node_modules/@stdlib/ndarray/for-each/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/for-each/docs/repl.txt new file mode 100644 index 000000000000..73cfcf3ebd23 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/for-each/docs/repl.txt @@ -0,0 +1,29 @@ + +{{alias}}( x, fcn[, thisArg] ) + Invokes a callback function once for each ndarray element. + + The callback function is provided the following arguments: + + - value: current array element. + - indices: current array element indices. + - arr: the input ndarray. + + Parameters + ---------- + x: ndarray + Input ndarray. + + fcn: Function + Callback function. + + thisArg: any (optional) + Callback function execution context. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ); + > function f( v ) { if ( v !== v ) { throw new Error( '...' ); } }; + > {{alias}}( x, f ); + + See Also + -------- diff --git a/lib/node_modules/@stdlib/ndarray/for-each/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/for-each/docs/types/index.d.ts new file mode 100644 index 000000000000..80143b3abb4b --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/for-each/docs/types/index.d.ts @@ -0,0 +1,91 @@ +/* +* @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 + +/// +import { typedndarray, float64ndarray, float32ndarray, complex128ndarray, complex64ndarray, int32ndarray, int16ndarray, int8ndarray, uint32ndarray, uint16ndarray, uint8ndarray, uint8cndarray, boolndarray, genericndarray } from '@stdlib/types/ndarray'; + +/** +* Callback invoked for each ndarray element. +*/ +type Nullary = ( this: U ) => void; + +/** +* Callback invoked for each ndarray element. +* +* @param value - current array element +*/ +type Unary = ( this: U, value: T ) => void; + +/** +* Callback invoked for each ndarray element. +* +* @param value - current array element +* @param indices - current array element indices +*/ +type Binary = ( this: U, value: T, indices: Array ) => void; + +/** +* Callback invoked for each ndarray element. +* +* @param value - current array element +* @param indices - current array element indices +* @param arr - input array +*/ +type Ternary = ( this: U, value: T, indices: Array, arr: typedndarray ) => void; + +/** +* Callback invoked for each ndarray element. +* +* @param value - current array element +* @param indices - current array element indices +* @param arr - input array +*/ +type Callback = Nullary | Unary | Binary | Ternary; + +/** +* Invokes a callback function once for each ndarray element. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback function execution context +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var naryFunction = require( '@stdlib/utils/nary-function' ); +* var log = require( '@stdlib/console/log' ); +* +* 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 = [ 3, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* // Apply the callback function: +* forEach( x, naryFunction( log, 1 ) ); +*/ +declare function forEach( x: float64ndarray | float32ndarray | complex128ndarray | complex64ndarray | int32ndarray | int16ndarray | int8ndarray | uint32ndarray | uint16ndarray | uint8ndarray | uint8cndarray | boolndarray | genericndarray, fcn: Callback, thisArg?: ThisParameterType> ): void; + + +// EXPORTS // + +export = forEach; diff --git a/lib/node_modules/@stdlib/ndarray/for-each/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/for-each/docs/types/test.ts new file mode 100644 index 000000000000..5dbe274fed91 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/for-each/docs/types/test.ts @@ -0,0 +1,100 @@ +/* +* @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 forEach = require( './index' ); + +/** +* Callback function. +* +* @param v - ndarray element +* @throws unexpected error +*/ +function clbk( v: any ): void { + if ( v !== v ) { + throw new Error( 'unexpected error' ); + } +} + +// The function returns `undefined`... +{ + const sh = [ 2, 2 ]; + const ord = 'row-major'; + + forEach( zeros( 'float64', sh, ord ), clbk ); // $ExpectType void + forEach( zeros( 'float64', sh, ord ), clbk, {} ); // $ExpectType void + forEach( zeros( 'float32', sh, ord ), clbk ); // $ExpectType void + forEach( zeros( 'float32', sh, ord ), clbk, {} ); // $ExpectType void + forEach( zeros( 'complex64', sh, ord ), clbk ); // $ExpectType void + forEach( zeros( 'complex64', sh, ord ), clbk, {} ); // $ExpectType void + forEach( zeros( 'complex128', sh, ord ), clbk ); // $ExpectType void + forEach( zeros( 'complex128', sh, ord ), clbk, {} ); // $ExpectType void + forEach( zeros( 'int32', sh, ord ), clbk ); // $ExpectType void + forEach( zeros( 'int32', sh, ord ), clbk, {} ); // $ExpectType void + forEach( zeros( 'int16', sh, ord ), clbk ); // $ExpectType void + forEach( zeros( 'int16', sh, ord ), clbk, {} ); // $ExpectType void + forEach( zeros( 'int8', sh, ord ), clbk ); // $ExpectType void + forEach( zeros( 'int8', sh, ord ), clbk, {} ); // $ExpectType void + forEach( zeros( 'uint32', sh, ord ), clbk ); // $ExpectType void + forEach( zeros( 'uint32', sh, ord ), clbk, {} ); // $ExpectType void + forEach( zeros( 'uint16', sh, ord ), clbk ); // $ExpectType void + forEach( zeros( 'uint16', sh, ord ), clbk, {} ); // $ExpectType void + forEach( zeros( 'uint8', sh, ord ), clbk ); // $ExpectType void + forEach( zeros( 'uint8', sh, ord ), clbk, {} ); // $ExpectType void + forEach( zeros( 'uint8c', sh, ord ), clbk ); // $ExpectType void + forEach( zeros( 'uint8c', sh, ord ), clbk, {} ); // $ExpectType void + forEach( empty( 'bool', sh, ord ), clbk ); // $ExpectType void + forEach( empty( 'bool', sh, ord ), clbk, {} ); // $ExpectType void + forEach( zeros( 'generic', sh, ord ), clbk ); // $ExpectType void + forEach( zeros( 'generic', sh, ord ), clbk, {} ); // $ExpectType void +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + forEach( 5, clbk ); // $ExpectError + forEach( true, clbk ); // $ExpectError + forEach( false, clbk ); // $ExpectError + forEach( null, clbk ); // $ExpectError + forEach( undefined, clbk ); // $ExpectError + forEach( {}, clbk ); // $ExpectError + forEach( [ 1 ], clbk ); // $ExpectError + forEach( ( x: number ): number => x, clbk ); // $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' ); + + forEach( x, '5' ); // $ExpectError + forEach( x, true ); // $ExpectError + forEach( x, false ); // $ExpectError + forEach( x, null ); // $ExpectError + forEach( x, undefined ); // $ExpectError + forEach( x, {} ); // $ExpectError + forEach( x, [ 1 ] ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + forEach(); // $ExpectError + forEach( zeros( 'float64', [ 2, 2 ], 'row-major' ) ); // $ExpectError + forEach( zeros( 'float64', [ 2, 2 ], 'row-major' ), {}, ( x: number ): number => x, {}, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/for-each/examples/index.js b/lib/node_modules/@stdlib/ndarray/for-each/examples/index.js new file mode 100644 index 000000000000..eaa889bc55cc --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/for-each/examples/index.js @@ -0,0 +1,37 @@ +/** +* @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 ndarray = require( '@stdlib/ndarray/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var naryFunction = require( '@stdlib/utils/nary-function' ); +var log = require( '@stdlib/console/log' ); +var forEach = require( '@stdlib/ndarray/for-each' ); + +var buffer = discreteUniform( 10, -100, 100, { + 'dtype': 'generic' +}); +var shape = [ 5, 2 ]; +var strides = [ 2, 1 ]; +var offset = 0; +var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); + +log( ndarray2array( x ) ); +forEach( x, naryFunction( log, 2 ) ); diff --git a/lib/node_modules/@stdlib/ndarray/for-each/lib/index.js b/lib/node_modules/@stdlib/ndarray/for-each/lib/index.js new file mode 100644 index 000000000000..9176632264f5 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/for-each/lib/index.js @@ -0,0 +1,52 @@ +/** +* @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'; + +/** +* Invoke a callback function once for each ndarray element. +* +* @module @stdlib/ndarray/for-each +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var naryFunction = require( '@stdlib/utils/nary-function' ); +* var log = require( '@stdlib/console/log' ); +* var forEach = require( '@stdlib/ndarray/for-each' ); +* +* 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 +* +* // Apply the callback function: +* forEach( x, naryFunction( log, 1 ) ); +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/for-each/lib/main.js b/lib/node_modules/@stdlib/ndarray/for-each/lib/main.js new file mode 100644 index 000000000000..af57b9d7e35a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/for-each/lib/main.js @@ -0,0 +1,71 @@ +/** +* @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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var base = require( '@stdlib/ndarray/base/for-each' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Invokes a callback function once for each ndarray element. +* +* @param {ndarray} x - input ndarray +* @param {Callback} fcn - callback function +* @param {*} [thisArg] - callback execution context +* @throws {TypeError} first argument must be an ndarray-like object +* @throws {TypeError} callback argument must be a function +* @returns {void} +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var naryFunction = require( '@stdlib/utils/nary-function' ); +* var log = require( '@stdlib/console/log' ); +* +* 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 +* +* // Apply the callback function: +* forEach( x, naryFunction( log, 1 ) ); +*/ +function forEach( x, fcn, thisArg ) { + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object. Value: `%s`.', x ) ); + } + if ( !isFunction( fcn ) ) { + throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', fcn ) ); + } + base( [ x ], fcn, thisArg ); +} + + +// EXPORTS // + +module.exports = forEach; diff --git a/lib/node_modules/@stdlib/ndarray/for-each/test/test.js b/lib/node_modules/@stdlib/ndarray/for-each/test/test.js new file mode 100644 index 000000000000..d94ba1297a68 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/for-each/test/test.js @@ -0,0 +1,138 @@ +/** +* @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 ones = require( '@stdlib/array/ones' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var naryFunction = require( '@stdlib/utils/nary-function' ); +var log = require( '@stdlib/console/log' ); +var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); +var strides2offset = require( '@stdlib/ndarray/base/strides2offset' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isSameFloat64Array = require( '@stdlib/assert/is-same-float64array' ); +var numel = require( '@stdlib/ndarray/base/numel' ); +var forEach = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof forEach, '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() { + forEach( value, naryFunction( log, 1 ) ); + }; + } +}); + +tape( 'the function throws an error if 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() { + forEach( x, value ); + }; + } +}); + +tape( 'the function applies a callback to each indexed element in the input ndarray (row-major, contiguous)', function test( t ) { + var expected; + var ord; + var sh; + var st; + var dt; + var o; + var x; + + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 1, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + x = ndarray( dt, ones( numel( sh ), dt ), sh, st, o, ord ); + + forEach( x, scale ); + + expected = new Float64Array([ + 10.0, + 10.0, + 10.0, + 10.0 + ]); + t.strictEqual( isSameFloat64Array( x.data, expected ), true, 'returns expected value' ); + + t.end(); + + function scale( v, i ) { + x.set( i[0], i[1], i[2], v*10.0 ); + } +}); + +// TODO: add more tests From 982be20dd317b0c47b93aa342599e4c0aee002e7 Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Sun, 15 Dec 2024 20:24:27 +0500 Subject: [PATCH 3/7] refactor: apply review suggestion Signed-off-by: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> --- lib/node_modules/@stdlib/ndarray/for-each/test/test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/ndarray/for-each/test/test.js b/lib/node_modules/@stdlib/ndarray/for-each/test/test.js index d94ba1297a68..7942dbed4b70 100644 --- a/lib/node_modules/@stdlib/ndarray/for-each/test/test.js +++ b/lib/node_modules/@stdlib/ndarray/for-each/test/test.js @@ -72,7 +72,7 @@ tape( 'the function throws an error if provided a first argument which is not an } }); -tape( 'the function throws an error if callback argument which is not a function', function test( t ) { +tape( 'the function throws an error if provided callback argument which is not a function', function test( t ) { var values; var x; var i; From 7884b2e255ae654a4670dbd3ea9352a9935146e6 Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Mon, 16 Dec 2024 10:29:07 +0000 Subject: [PATCH 4/7] test: add more tests --- .../@stdlib/ndarray/for-each/test/test.js | 266 +++++++++++++++++- 1 file changed, 265 insertions(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/ndarray/for-each/test/test.js b/lib/node_modules/@stdlib/ndarray/for-each/test/test.js index d94ba1297a68..87c3358aef13 100644 --- a/lib/node_modules/@stdlib/ndarray/for-each/test/test.js +++ b/lib/node_modules/@stdlib/ndarray/for-each/test/test.js @@ -30,6 +30,8 @@ var strides2offset = require( '@stdlib/ndarray/base/strides2offset' ); var Float64Array = require( '@stdlib/array/float64' ); var isSameFloat64Array = require( '@stdlib/assert/is-same-float64array' ); var numel = require( '@stdlib/ndarray/base/numel' ); +var dfill = require( '@stdlib/blas/ext/base/dfill' ); +var blockSize = require( '@stdlib/ndarray/base/unary-tiling-block-size' ); var forEach = require( './../lib' ); @@ -135,4 +137,266 @@ tape( 'the function applies a callback to each indexed element in the input ndar } }); -// TODO: add more tests +tape( 'the function applies a callback to each indexed element in the input ndarray (column-major, contiguous)', function test( t ) { + var expected; + var ord; + var sh; + var st; + var dt; + var o; + var x; + + dt = 'float64'; + ord = 'column-major'; + sh = [ 2, 1, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + x = ndarray( dt, ones( numel( sh ), dt ), sh, st, o, ord ); + + forEach( x, scale ); + + expected = new Float64Array([ + 10.0, + 10.0, + 10.0, + 10.0 + ]); + t.strictEqual( isSameFloat64Array( x.data, expected ), true, 'returns expected value' ); + + t.end(); + + function scale( v, i ) { + x.set( i[0], i[1], i[2], v*10.0 ); + } +}); + +tape( 'the function applies a callback to each indexed element in the input ndarray (row-major, non-contiguous, large arrays)', function test( t ) { + var expected; + var bsize; + var ord; + var sh; + var st; + var dt; + var o; + var x; + + dt = 'float64'; + ord = 'row-major'; + + bsize = blockSize( dt ); + sh = [ bsize*2, 1, 2 ]; + st = [ -4, -4, 2 ]; + o = strides2offset( sh, st ); + + x = ndarray( dt, ones( numel( sh )*2, dt ), sh, st, o, ord ); + + forEach( x, scale ); + expected = ones( x.length*2, dt ); + dfill.ndarray( x.length, 100.0, expected, st[2], expected.length/2 ); + + t.strictEqual( isSameFloat64Array( x.data, expected ), true, 'returns expected value' ); + + t.end(); + + function scale( v, i ) { + x.set( i[0], i[1], i[2], v*10.0 ); + } +}); + +tape( 'the function applies a callback to each indexed element in the input ndarray (column-major, non-contiguous, large arrays)', function test( t ) { + var expected; + var bsize; + var ord; + var sh; + var st; + var dt; + var o; + var x; + + dt = 'float64'; + ord = 'column-major'; + + bsize = blockSize( dt ); + sh = [ 2, 1, bsize*2 ]; + st = [ 2, -4, -4 ]; + o = strides2offset( sh, st ); + + x = ndarray( dt, ones( numel( sh )*2, dt ), sh, st, o, ord ); + + forEach( x, scale ); + expected = ones( x.length*2, dt ); + dfill.ndarray( x.length, 100.0, expected, st[0], expected.length/2 ); + + t.strictEqual( isSameFloat64Array( x.data, expected ), true, 'returns expected value' ); + + t.end(); + + function scale( v, i ) { + x.set( i[0], i[1], i[2], v*10.0 ); + } +}); + +tape( 'the function supports providing a callback execution context', function test( t ) { + var expected; + var ctx; + var ord; + var sh; + var st; + var dt; + var o; + var x; + + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 1, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + x = ndarray( dt, ones( numel( sh ), dt ), sh, st, o, ord ); + + ctx = { + 'count': 0 + }; + forEach( x, scale, ctx ); + + expected = new Float64Array([ + 10.0, + 10.0, + 10.0, + 10.0 + ]); + t.strictEqual( isSameFloat64Array( x.data, expected ), true, 'returns expected value' ); + t.strictEqual( ctx.count, 4, 'returns expected value' ); + + t.end(); + + function scale( v, i ) { + this.count += 1; // eslint-disable-line no-invalid-this + x.set( i[0], i[1], i[2], v*10.0 ); + } +}); + +tape( 'the function invokes a provided callback with three arguments (row-major)', function test( t ) { + var expected; + var indices; + var values; + var arrays; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var i; + + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 1, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + x = ndarray( dt, ones( numel( sh ), dt ), sh, st, o, ord ); + + values = []; + indices = []; + arrays = []; + forEach( x, scale ); + + expected = new Float64Array([ + 10.0, + 10.0, + 10.0, + 10.0 + ]); + t.strictEqual( isSameFloat64Array( x.data, expected ), true, '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 scale( v, i, arr ) { + values.push( v ); + indices.push( i ); + arrays.push( arr ); + x.set( i[0], i[1], i[2], v*10.0 ); + } +}); + +tape( 'the function invokes a provided callback with three arguments (column-major)', function test( t ) { + var expected; + var indices; + var values; + var arrays; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var i; + + dt = 'float64'; + ord = 'column-major'; + sh = [ 2, 1, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + x = ndarray( dt, ones( numel( sh ), dt ), sh, st, o, ord ); + + values = []; + indices = []; + arrays = []; + forEach( x, scale ); + + expected = new Float64Array([ + 10.0, + 10.0, + 10.0, + 10.0 + ]); + t.strictEqual( isSameFloat64Array( x.data, expected ), true, 'returns expected value' ); + + expected = [ + [ 0, 0, 0 ], + [ 1, 0, 0 ], + [ 0, 0, 1 ], + [ 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 scale( v, i, arr ) { + values.push( v ); + indices.push( i ); + arrays.push( arr ); + x.set( i[0], i[1], i[2], v*10.0 ); + } +}); From b9b1156612d99271e396865cd18ce51f9079c633 Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Mon, 16 Dec 2024 15:43:56 +0500 Subject: [PATCH 5/7] docs: apply review suggestion Signed-off-by: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> --- lib/node_modules/@stdlib/ndarray/for-each/test/test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/ndarray/for-each/test/test.js b/lib/node_modules/@stdlib/ndarray/for-each/test/test.js index 19980dbe90d7..0f9922199f03 100644 --- a/lib/node_modules/@stdlib/ndarray/for-each/test/test.js +++ b/lib/node_modules/@stdlib/ndarray/for-each/test/test.js @@ -74,7 +74,7 @@ tape( 'the function throws an error if provided a first argument which is not an } }); -tape( 'the function throws an error if provided callback argument which is not a function', function test( t ) { +tape( 'the function throws an error if provided a callback argument which is not a function', function test( t ) { var values; var x; var i; From 09534dbc105b17946d18f02751cd2601d1dd7344 Mon Sep 17 00:00:00 2001 From: Athan Reines Date: Sat, 21 Dec 2024 17:35:37 -0800 Subject: [PATCH 6/7] chore: improve type specificity and fix definition placement --- .../@stdlib/ndarray/for-each/README.md | 4 +- .../ndarray/for-each/docs/types/index.d.ts | 350 +++++++++++++++++- 2 files changed, 346 insertions(+), 8 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/for-each/README.md b/lib/node_modules/@stdlib/ndarray/for-each/README.md index 6959ed300624..07fbb840f010 100644 --- a/lib/node_modules/@stdlib/ndarray/for-each/README.md +++ b/lib/node_modules/@stdlib/ndarray/for-each/README.md @@ -153,10 +153,10 @@ forEach( x, naryFunction( log, 2 ) ); diff --git a/lib/node_modules/@stdlib/ndarray/for-each/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/for-each/docs/types/index.d.ts index 80143b3abb4b..ae46f1d59acb 100644 --- a/lib/node_modules/@stdlib/ndarray/for-each/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/ndarray/for-each/docs/types/index.d.ts @@ -19,19 +19,21 @@ // TypeScript Version: 4.1 /// + import { typedndarray, float64ndarray, float32ndarray, complex128ndarray, complex64ndarray, int32ndarray, int16ndarray, int8ndarray, uint32ndarray, uint16ndarray, uint8ndarray, uint8cndarray, boolndarray, genericndarray } from '@stdlib/types/ndarray'; +import { Complex64, Complex128 } from '@stdlib/types/complex'; /** * Callback invoked for each ndarray element. */ -type Nullary = ( this: U ) => void; +type Nullary = ( this: V ) => void; /** * Callback invoked for each ndarray element. * * @param value - current array element */ -type Unary = ( this: U, value: T ) => void; +type Unary = ( this: V, value: T ) => void; /** * Callback invoked for each ndarray element. @@ -39,7 +41,7 @@ type Unary = ( this: U, value: T ) => void; * @param value - current array element * @param indices - current array element indices */ -type Binary = ( this: U, value: T, indices: Array ) => void; +type Binary = ( this: V, value: T, indices: Array ) => void; /** * Callback invoked for each ndarray element. @@ -48,7 +50,7 @@ type Binary = ( this: U, value: T, indices: Array ) => void; * @param indices - current array element indices * @param arr - input array */ -type Ternary = ( this: U, value: T, indices: Array, arr: typedndarray ) => void; +type Ternary = ( this: V, value: T, indices: Array, arr: U ) => void; /** * Callback invoked for each ndarray element. @@ -57,7 +59,7 @@ type Ternary = ( this: U, value: T, indices: Array, arr: typedndar * @param indices - current array element indices * @param arr - input array */ -type Callback = Nullary | Unary | Binary | Ternary; +type Callback = Nullary | Unary | Binary | Ternary; /** * Invokes a callback function once for each ndarray element. @@ -83,7 +85,343 @@ type Callback = Nullary | Unary | Binary | Ternary; * // Apply the callback function: * forEach( x, naryFunction( log, 1 ) ); */ -declare function forEach( x: float64ndarray | float32ndarray | complex128ndarray | complex64ndarray | int32ndarray | int16ndarray | int8ndarray | uint32ndarray | uint16ndarray | uint8ndarray | uint8cndarray | boolndarray | genericndarray, fcn: Callback, thisArg?: ThisParameterType> ): void; +declare function forEach( x: float64ndarray, fcn: Callback, thisArg?: ThisParameterType> ): void; + +/** +* Invokes a callback function once for each ndarray element. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback function execution context +* +* @example +* var Float32Array = require( '@stdlib/array/float32' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var naryFunction = require( '@stdlib/utils/nary-function' ); +* var log = require( '@stdlib/console/log' ); +* +* var buffer = new Float32Array( [ 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( 'float32', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* // Apply the callback function: +* forEach( x, naryFunction( log, 1 ) ); +*/ +declare function forEach( x: float32ndarray, fcn: Callback, thisArg?: ThisParameterType> ): void; + +/** +* Invokes a callback function once for each ndarray element. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback function execution context +* +* @example +* var Int32Array = require( '@stdlib/array/int32' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var naryFunction = require( '@stdlib/utils/nary-function' ); +* var log = require( '@stdlib/console/log' ); +* +* var buffer = new Int32Array( [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 3, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'int32', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* // Apply the callback function: +* forEach( x, naryFunction( log, 1 ) ); +*/ +declare function forEach( x: int32ndarray, fcn: Callback, thisArg?: ThisParameterType> ): void; + +/** +* Invokes a callback function once for each ndarray element. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback function execution context +* +* @example +* var Int16Array = require( '@stdlib/array/int16' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var naryFunction = require( '@stdlib/utils/nary-function' ); +* var log = require( '@stdlib/console/log' ); +* +* var buffer = new Int16Array( [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 3, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'int16', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* // Apply the callback function: +* forEach( x, naryFunction( log, 1 ) ); +*/ +declare function forEach( x: int16ndarray, fcn: Callback, thisArg?: ThisParameterType> ): void; + +/** +* Invokes a callback function once for each ndarray element. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback function execution context +* +* @example +* var Int8Array = require( '@stdlib/array/int8' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var naryFunction = require( '@stdlib/utils/nary-function' ); +* var log = require( '@stdlib/console/log' ); +* +* var buffer = new Int8Array( [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 3, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'int8', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* // Apply the callback function: +* forEach( x, naryFunction( log, 1 ) ); +*/ +declare function forEach( x: int8ndarray, fcn: Callback, thisArg?: ThisParameterType> ): void; + +/** +* Invokes a callback function once for each ndarray element. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback function execution context +* +* @example +* var Uint32Array = require( '@stdlib/array/uint32' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var naryFunction = require( '@stdlib/utils/nary-function' ); +* var log = require( '@stdlib/console/log' ); +* +* var buffer = new Uint32Array( [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 3, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'uint32', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* // Apply the callback function: +* forEach( x, naryFunction( log, 1 ) ); +*/ +declare function forEach( x: uint32ndarray, fcn: Callback, thisArg?: ThisParameterType> ): void; + +/** +* Invokes a callback function once for each ndarray element. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback function execution context +* +* @example +* var Uint16Array = require( '@stdlib/array/uint16' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var naryFunction = require( '@stdlib/utils/nary-function' ); +* var log = require( '@stdlib/console/log' ); +* +* var buffer = new Uint16Array( [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 3, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'uint16', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* // Apply the callback function: +* forEach( x, naryFunction( log, 1 ) ); +*/ +declare function forEach( x: uint16ndarray, fcn: Callback, thisArg?: ThisParameterType> ): void; + +/** +* Invokes a callback function once for each ndarray element. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback function execution context +* +* @example +* var Uint8Array = require( '@stdlib/array/uint8' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var naryFunction = require( '@stdlib/utils/nary-function' ); +* var log = require( '@stdlib/console/log' ); +* +* var buffer = new Uint8Array( [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 3, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'uint8', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* // Apply the callback function: +* forEach( x, naryFunction( log, 1 ) ); +*/ +declare function forEach( x: uint8ndarray, fcn: Callback, thisArg?: ThisParameterType> ): void; + +/** +* Invokes a callback function once for each ndarray element. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback function execution context +* +* @example +* var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var naryFunction = require( '@stdlib/utils/nary-function' ); +* var log = require( '@stdlib/console/log' ); +* +* var buffer = new Uint8ClampedArray( [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 3, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'uint8c', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* // Apply the callback function: +* forEach( x, naryFunction( log, 1 ) ); +*/ +declare function forEach( x: uint8cndarray, fcn: Callback, thisArg?: ThisParameterType> ): void; + +/** +* Invokes a callback function once for each ndarray element. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback function execution context +* +* @example +* var Complex128Array = require( '@stdlib/array/complex128' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var naryFunction = require( '@stdlib/utils/nary-function' ); +* var log = require( '@stdlib/console/log' ); +* +* 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 +* +* // Apply the callback function: +* forEach( x, naryFunction( log, 1 ) ); +*/ +declare function forEach( x: complex128ndarray, fcn: Callback, thisArg?: ThisParameterType> ): void; + +/** +* Invokes a callback function once for each ndarray element. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback function execution context +* +* @example +* var Complex64Array = require( '@stdlib/array/complex64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var naryFunction = require( '@stdlib/utils/nary-function' ); +* var log = require( '@stdlib/console/log' ); +* +* 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 +* +* // Apply the callback function: +* forEach( x, naryFunction( log, 1 ) ); +*/ +declare function forEach( x: complex64ndarray, fcn: Callback, thisArg?: ThisParameterType> ): void; + +/** +* Invokes a callback function once for each ndarray element. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback function execution context +* +* @example +* var BooleanArray = require( '@stdlib/array/bool' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var naryFunction = require( '@stdlib/utils/nary-function' ); +* var log = require( '@stdlib/console/log' ); +* +* var buffer = new BooleanArray( [ true, false, true, false, true, false, true, false, true, false, true, false ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 3, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'bool', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* // Apply the callback function: +* forEach( x, naryFunction( log, 1 ) ); +*/ +declare function forEach( x: boolndarray, fcn: Callback, thisArg?: ThisParameterType> ): void; + +/** +* Invokes a callback function once for each ndarray element. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback function execution context +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var naryFunction = require( '@stdlib/utils/nary-function' ); +* var log = require( '@stdlib/console/log' ); +* +* 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 = [ 3, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* // Apply the callback function: +* forEach( x, naryFunction( log, 1 ) ); +*/ +declare function forEach( x: genericndarray, fcn: Callback, V>, thisArg?: ThisParameterType, V>> ): void; + +/** +* Invokes a callback function once for each ndarray element. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback function execution context +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var naryFunction = require( '@stdlib/utils/nary-function' ); +* var log = require( '@stdlib/console/log' ); +* +* 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 = [ 3, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* // Apply the callback function: +* forEach( x, naryFunction( log, 1 ) ); +*/ +declare function forEach( x: typedndarray, fcn: Callback, V>, thisArg?: ThisParameterType, V>> ): void; // EXPORTS // From 0df5094e63ba62c65e2990324221e4bd4415c726 Mon Sep 17 00:00:00 2001 From: Athan Reines Date: Sat, 21 Dec 2024 17:40:01 -0800 Subject: [PATCH 7/7] fix: correctly map types --- lib/node_modules/@stdlib/ndarray/for-each/docs/types/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/ndarray/for-each/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/for-each/docs/types/index.d.ts index ae46f1d59acb..1280c2cbf2d3 100644 --- a/lib/node_modules/@stdlib/ndarray/for-each/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/ndarray/for-each/docs/types/index.d.ts @@ -59,7 +59,7 @@ type Ternary = ( this: V, value: T, indices: Array, arr: U ) => * @param indices - current array element indices * @param arr - input array */ -type Callback = Nullary | Unary | Binary | Ternary; +type Callback = Nullary | Unary | Binary | Ternary; /** * Invokes a callback function once for each ndarray element.