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..07fbb840f010 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/for-each/README.md @@ -0,0 +1,164 @@ + + +# 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 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 + +forEach( x, naryFunction( log, 1 ) ); +``` + +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' ); + +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 = { + 'sum': 0 +}; + +forEach( x, accumulate, ctx ); +var sum = ctx.sum; +// returns 36 +``` + +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 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/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..1280c2cbf2d3 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/for-each/docs/types/index.d.ts @@ -0,0 +1,429 @@ +/* +* @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'; +import { Complex64, Complex128 } from '@stdlib/types/complex'; + +/** +* Callback invoked for each ndarray element. +*/ +type Nullary = ( this: V ) => void; + +/** +* Callback invoked for each ndarray element. +* +* @param value - current array element +*/ +type Unary = ( this: V, value: T ) => void; + +/** +* Callback invoked for each ndarray element. +* +* @param value - current array element +* @param indices - current array element indices +*/ +type Binary = ( this: V, 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: V, value: T, indices: Array, arr: U ) => 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, 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 // + +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/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__": {} +} 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..0f9922199f03 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/for-each/test/test.js @@ -0,0 +1,402 @@ +/** +* @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 dfill = require( '@stdlib/blas/ext/base/dfill' ); +var blockSize = require( '@stdlib/ndarray/base/unary-tiling-block-size' ); +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 provided 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() { + 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 ); + } +}); + +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 ); + } +});