diff --git a/CHANGELOG.md b/CHANGELOG.md index 617c8ff7..474afdee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@
-## Unreleased (2024-12-12) +## Unreleased (2024-12-13)
@@ -146,6 +146,28 @@ +
+ +#### [@stdlib/ndarray/map](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/map) + +
+ +
+ +##### Features + +- [`3ea906b`](https://github.com/stdlib-js/stdlib/commit/3ea906bb64f93b4d323bc91f99a176d2729a2cc9) - add `ndarray/map` [(#3314)](https://github.com/stdlib-js/stdlib/pull/3314) + +
+ + + +
+ +
+ + +
@@ -166,8 +188,10 @@ ### Contributors -A total of 1 person contributed to this release. Thank you to this contributor: +A total of 3 people contributed to this release. Thank you to the following contributors: +- Athan Reines +- Muhammad Haris - Philipp Burckhardt
@@ -180,6 +204,7 @@ A total of 1 person contributed to this release. Thank you to this contributor:
+- [`3ea906b`](https://github.com/stdlib-js/stdlib/commit/3ea906bb64f93b4d323bc91f99a176d2729a2cc9) - **feat:** add `ndarray/map` [(#3314)](https://github.com/stdlib-js/stdlib/pull/3314) _(by Muhammad Haris, Athan Reines)_ - [`cf7d38a`](https://github.com/stdlib-js/stdlib/commit/cf7d38ae3e7bce92cf47778f7b1c3da731121d77) - **docs:** update related packages sections [(#3527)](https://github.com/stdlib-js/stdlib/pull/3527) _(by stdlib-bot)_ - [`bf5643f`](https://github.com/stdlib-js/stdlib/commit/bf5643fb1a3f32a60903d8e210f71571e609119f) - **docs:** update related packages sections [(#3404)](https://github.com/stdlib-js/stdlib/pull/3404) _(by stdlib-bot)_ - [`a80835b`](https://github.com/stdlib-js/stdlib/commit/a80835b8f9959a15751adfce5572bb2b29cfeeed) - **refactor:** declare parameters and pointer as const _(by Philipp Burckhardt)_ diff --git a/CONTRIBUTORS b/CONTRIBUTORS index ba851aae..1ba9b590 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -112,6 +112,7 @@ UtkershBasnet <119008923+UtkershBasnet@users.noreply.github.com> Vaibhav Patel <98279986+noobCoderVP@users.noreply.github.com> Varad Gupta Vinit Pandit <106718914+MeastroZI@users.noreply.github.com> +Vivek maurya <155618190+vivekmaurya001@users.noreply.github.com> Xiaochuan Ye Yaswanth Kosuru <116426380+yaswanthkosuru@users.noreply.github.com> Yernar Yergaziyev diff --git a/map/README.md b/map/README.md new file mode 100644 index 00000000..aeeb692a --- /dev/null +++ b/map/README.md @@ -0,0 +1,215 @@ + + +# map + +> Apply a callback function to elements in an input [ndarray][@stdlib/ndarray/ctor] and assign results to elements in a new output [ndarray][@stdlib/ndarray/ctor]. + +
+ +
+ + + +
+ +## Usage + +```javascript +var map = require( '@stdlib/ndarray/map' ); +``` + +#### map( x\[, options], fcn\[, thisArg] ) + +Applies a callback function to elements in an input [ndarray][@stdlib/ndarray/ctor] and assigns results to elements in a new output [ndarray][@stdlib/ndarray/ctor]. + + + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); + +function 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 + +var y = map( x, scale ); +// returns + +var arr = ndarray2array( y ); +// 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]. +- **options**: function options. +- **fcn**: callback to apply. +- **thisArg**: callback execution context. + +The function accepts the following options: + +- **dtype**: output ndarray [data type][@stdlib/ndarray/dtypes]. If not specified, the output ndarray [data type][@stdlib/ndarray/dtypes] is inferred from the input [ndarray][@stdlib/ndarray/ctor]. + +By default, the output ndarray [data type][@stdlib/ndarray/dtypes] is inferred from the input [ndarray][@stdlib/ndarray/ctor]. To return an ndarray with a different [data type][@stdlib/ndarray/dtypes], specify the `dtype` option. + + + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var dtype = require( '@stdlib/ndarray/dtype' ); +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 + +var opts = { + 'dtype': 'float32' +}; +var y = map( x, opts, scale ); +// returns + +var dt = dtype( y ); +// returns 'float32' + +var arr = ndarray2array( y ); +// returns [ [ 20.0, 30.0, 40.0 ], [ 80.0, 90.0, 100.0 ] ] +``` + +The callback function is provided the following arguments: + +- **values**: current array element. +- **indices**: current array element indices. +- **arr**: the input [ndarray][@stdlib/ndarray/ctor]. + +
+ + + +
+ +## Notes + +- The function does **not** perform explicit casting (e.g., from a real-valued floating-point number to a complex floating-point number). Any such casting should be performed by a provided callback function. + + + + ```javascript + var Float64Array = require( '@stdlib/array/float64' ); + var ndarray = require( '@stdlib/ndarray/ctor' ); + var Complex128 = require( '@stdlib/complex/float64/ctor' ); + var ndarray2array = require( '@stdlib/ndarray/to-array' ); + + function toComplex( z ) { + return new Complex128( z, 0.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 opts = { + 'dtype': 'complex128' + }; + var y = map( x, opts, toComplex ); + // returns + ``` + +- 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 map = require( '@stdlib/ndarray/map' ); + +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 ) ); + +var y = map( x, naryFunction( abs, 1 ) ); +console.log( ndarray2array( y ) ); +``` + +
+ + + + + + + + + + + + diff --git a/map/benchmark/benchmark.1d.js b/map/benchmark/benchmark.1d.js new file mode 100644 index 00000000..c8688ea5 --- /dev/null +++ b/map/benchmark/benchmark.1d.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 bench = require( '@stdlib/bench' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var identity = require( '@stdlib/math/base/special/identity' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var shape2strides = require( './../../base/shape2strides' ); +var ndarray = require( './../../ctor' ); +var pkg = require( './../package.json' ).name; +var map = require( './../lib' ); + + +// VARIABLES // + +var xtypes = [ 'generic' ]; +var ytypes = [ 'float64' ]; +var orders = [ 'row-major', 'column-major' ]; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @param {NonNegativeIntegerArray} shape - ndarray shape +* @param {string} xtype - input ndarray data type +* @param {string} ytype - output ndarray data type +* @param {string} order - ndarray memory layout +* @returns {Function} benchmark function +*/ +function createBenchmark( len, shape, xtype, ytype, order ) { + var strides; + var opts; + var xbuf; + var x; + + xbuf = discreteUniform( len, -100, 100, { + 'dtype': xtype + }); + strides = shape2strides( shape, order ); + x = ndarray( xtype, xbuf, shape, strides, 0, order ); + opts = { + 'dtype': ytype + }; + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var y; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = map( x, opts, identity ); + if ( isnan( y.data[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( !isndarrayLike( y ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var ord; + var sh; + var t1; + var t2; + 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 ]; + t2 = ytypes[ j ]; + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + + sh = [ len ]; + f = createBenchmark( len, sh, t1, t2, ord ); + bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',yorder='+ord+',xtype='+t1+',ytype='+t2, f ); + } + } + } +} + +main(); diff --git a/map/benchmark/benchmark.2d.js b/map/benchmark/benchmark.2d.js new file mode 100644 index 00000000..c529933b --- /dev/null +++ b/map/benchmark/benchmark.2d.js @@ -0,0 +1,148 @@ +/** +* @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 identity = require( '@stdlib/math/base/special/identity' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var shape2strides = require( './../../base/shape2strides' ); +var ndarray = require( './../../ctor' ); +var pkg = require( './../package.json' ).name; +var map = require( './../lib' ); + + +// VARIABLES // + +var xtypes = [ 'generic' ]; +var ytypes = [ 'float64' ]; +var orders = [ 'row-major', 'column-major' ]; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - ndarray length +* @param {NonNegativeIntegerArray} shape - ndarray shape +* @param {string} xtype - input ndarray data type +* @param {string} ytype - output ndarray data type +* @param {string} order - ndarray memory layout +* @returns {Function} benchmark function +*/ +function createBenchmark( len, shape, xtype, ytype, order ) { + var strides; + var opts; + var xbuf; + var x; + var y; + + xbuf = discreteUniform( len, -100, 100, { + 'dtype': xtype + }); + strides = shape2strides( shape, order ); + x = ndarray( xtype, xbuf, shape, strides, 0, order ); + opts = { + 'dtype': ytype + }; + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = map( x, opts, identity ); + if ( isnan( y.data[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( y.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 t2; + 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 ]; + t2 = ytypes[ j ]; + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + + sh = [ len/2, 2 ]; + f = createBenchmark( len, sh, t1, t2, ord ); + bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',yorder='+ord+',xtype='+t1+',ytype='+t2, f ); + + sh = [ 2, len/2 ]; + f = createBenchmark( len, sh, t1, t2, ord ); + bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',yorder='+ord+',xtype='+t1+',ytype='+t2, f ); + + len = floor( sqrt( len ) ); + sh = [ len, len ]; + len *= len; + f = createBenchmark( len, sh, t1, t2, ord ); + bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',yorder='+ord+',xtype='+t1+',ytype='+t2, f ); + } + } + } +} + +main(); diff --git a/map/docs/repl.txt b/map/docs/repl.txt new file mode 100644 index 00000000..bba74415 --- /dev/null +++ b/map/docs/repl.txt @@ -0,0 +1,39 @@ + +{{alias}}( x[, options], fcn[, thisArg] ) + Applies a callback function to elements in an input ndarray and assigns + results to elements in a new output ndarray. + + 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. + + options: Object (optional) + Function options. + + options.dtype: string (optional) + Output ndarray data type. Overrides using the input array's inferred + data type. + + 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 ) { return v*10.0; }; + > var y = {{alias}}( x, f ); + > {{alias:@stdlib/ndarray/to-array}}( y ) + [ [ 10.0, 20.0 ], [ 30.0, 40.0 ] ] + + See Also + -------- diff --git a/map/docs/types/index.d.ts b/map/docs/types/index.d.ts new file mode 100644 index 00000000..7000bbd3 --- /dev/null +++ b/map/docs/types/index.d.ts @@ -0,0 +1,1179 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/// + +/* eslint-disable max-lines */ + +import { typedndarray, DataType, float64ndarray, float32ndarray, complex128ndarray, complex64ndarray, int32ndarray, int16ndarray, int8ndarray, uint32ndarray, uint16ndarray, uint8ndarray, uint8cndarray, boolndarray, genericndarray } from '@stdlib/types/ndarray'; +import { Complex64, Complex128, ComplexLike } from '@stdlib/types/complex'; + +/** +* Callback invoked for each ndarray element. +* +* @returns output value +*/ +type Nullary = ( this: V ) => U; + +/** +* Callback invoked for each ndarray element. +* +* @param value - current array element +* @returns output value +*/ +type Unary = ( this: V, value: T ) => U; + +/** +* Callback invoked for each ndarray element. +* +* @param value - current array element +* @param indices - current array element indices +* @returns output value +*/ +type Binary = ( this: V, value: T, indices: Array ) => U; + +/** +* Callback invoked for each ndarray element. +* +* @param value - current array element +* @param indices - current array element indices +* @param arr - input array +* @returns output value +*/ +type Ternary = ( this: V, value: T, indices: Array, arr: typedndarray ) => U; + +/** +* Callback invoked for each ndarray element. +* +* @param value - current array element +* @param indices - current array element indices +* @param arr - input array +* @returns output value +*/ +type Callback = Nullary | Unary | Binary | Ternary; + +/** +* Interface describing function options. +*/ +interface Options { + /** + * Output ndarray data type. + * + * ## Notes + * + * - This option overrides using the input ndarray's inferred data type. + */ + dtype?: DataType; +} + +/** +* Interface describing function options. +*/ +interface Float64Options extends Options { + /** + * Output ndarray data type. + * + * ## Notes + * + * - This option overrides using the input ndarray's inferred data type. + */ + dtype?: 'float64'; +} + +/** +* Interface describing function options. +*/ +interface Float32Options extends Options { + /** + * Output ndarray data type. + * + * ## Notes + * + * - This option overrides using the input ndarray's inferred data type. + */ + dtype?: 'float32'; +} + +/** +* Interface describing function options. +*/ +interface Complex128Options extends Options { + /** + * Output ndarray data type. + * + * ## Notes + * + * - This option overrides using the input ndarray's inferred data type. + */ + dtype?: 'complex128'; +} + +/** +* Interface describing function options. +*/ +interface Complex64Options extends Options { + /** + * Output ndarray data type. + * + * ## Notes + * + * - This option overrides using the input ndarray's inferred data type. + */ + dtype?: 'complex64'; +} + +/** +* Interface describing function options. +*/ +interface Int32Options extends Options { + /** + * Output ndarray data type. + * + * ## Notes + * + * - This option overrides using the input ndarray's inferred data type. + */ + dtype?: 'int32'; +} + +/** +* Interface describing function options. +*/ +interface Int16Options extends Options { + /** + * Output ndarray data type. + * + * ## Notes + * + * - This option overrides using the input ndarray's inferred data type. + */ + dtype?: 'int16'; +} + +/** +* Interface describing function options. +*/ +interface Int8Options extends Options { + /** + * Output ndarray data type. + * + * ## Notes + * + * - This option overrides using the input ndarray's inferred data type. + */ + dtype?: 'int8'; +} + +/** +* Interface describing function options. +*/ +interface Uint32Options extends Options { + /** + * Output ndarray data type. + * + * ## Notes + * + * - This option overrides using the input ndarray's inferred data type. + */ + dtype?: 'uint32'; +} + +/** +* Interface describing function options. +*/ +interface Uint16Options extends Options { + /** + * Output ndarray data type. + * + * ## Notes + * + * - This option overrides using the input ndarray's inferred data type. + */ + dtype?: 'uint16'; +} + +/** +* Interface describing function options. +*/ +interface Uint8Options extends Options { + /** + * Output ndarray data type. + * + * ## Notes + * + * - This option overrides using the input ndarray's inferred data type. + */ + dtype?: 'uint8'; +} + +/** +* Interface describing function options. +*/ +interface Uint8COptions extends Options { + /** + * Output ndarray data type. + * + * ## Notes + * + * - This option overrides using the input ndarray's inferred data type. + */ + dtype?: 'uint8c'; +} + +/** +* Interface describing function options. +*/ +interface BoolOptions extends Options { + /** + * Output ndarray data type. + * + * ## Notes + * + * - This option overrides using the input ndarray's inferred data type. + */ + dtype?: 'bool'; +} + +/** +* Interface describing function options. +*/ +interface GenericOptions extends Options { + /** + * Output ndarray data type. + * + * ## Notes + * + * - This option overrides using the input ndarray's inferred data type. + */ + dtype?: 'generic'; +} + +/** +* Applies a callback function to elements in an input ndarray and assigns results to elements in a new output ndarray. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* 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 +* +* var y = map( x, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ 20.0, 30.0, 40.0 ], [ 80.0, 90.0, 100.0 ] ] +*/ +declare function map( x: float64ndarray, fcn: Callback, thisArg?: ThisParameterType> ): float64ndarray; + +/** +* Applies a callback function to elements in an input ndarray and assigns results to elements in a new output ndarray. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Float32Array = require( '@stdlib/array/float32' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( z ) { +* return z * 10.0; +* } +* +* 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 = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'float32', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var y = map( x, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ 20.0, 30.0, 40.0 ], [ 80.0, 90.0, 100.0 ] ] +*/ +declare function map( x: float32ndarray, fcn: Callback, thisArg?: ThisParameterType> ): float32ndarray; + +/** +* Applies a callback function to elements in an input ndarray and assigns results to elements in a new output ndarray. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Complex64Array = require( '@stdlib/array/complex64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* function identity( z ) { +* return z; +* } +* +* var buffer = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 3, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'complex64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var y = map( x, identity ); +* // returns +*/ +declare function map( x: complex64ndarray, fcn: Callback, thisArg?: ThisParameterType> ): complex64ndarray; + +/** +* Applies a callback function to elements in an input ndarray and assigns results to elements in a new output ndarray. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Complex128Array = require( '@stdlib/array/complex128' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* function identity( z ) { +* return z; +* } +* +* var buffer = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 3, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'complex128', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var y = map( x, identity ); +* // returns +*/ +declare function map( x: complex128ndarray, fcn: Callback, thisArg?: ThisParameterType> ): complex128ndarray; + +/** +* Applies a callback function to elements in an input ndarray and assigns results to elements in a new output ndarray. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Int32Array = require( '@stdlib/array/int32' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( z ) { +* return z * 10; +* } +* +* var buffer = new Int32Array( [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'int32', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var y = map( x, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ 20, 30, 40 ], [ 80, 90, 100 ] ] +*/ +declare function map( x: int32ndarray, fcn: Callback, thisArg?: ThisParameterType> ): int32ndarray; + +/** +* Applies a callback function to elements in an input ndarray and assigns results to elements in a new output ndarray. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Int16Array = require( '@stdlib/array/int16' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( z ) { +* return z * 10; +* } +* +* var buffer = new Int16Array( [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'int16', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var y = map( x, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ 20, 30, 40 ], [ 80, 90, 100 ] ] +*/ +declare function map( x: int16ndarray, fcn: Callback, thisArg?: ThisParameterType> ): int16ndarray; + +/** +* Applies a callback function to elements in an input ndarray and assigns results to elements in a new output ndarray. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Int8Array = require( '@stdlib/array/int8' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( z ) { +* return z * 10; +* } +* +* var buffer = new Int8Array( [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'int8', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var y = map( x, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ 20, 30, 40 ], [ 80, 90, 100 ] ] +*/ +declare function map( x: int8ndarray, fcn: Callback, thisArg?: ThisParameterType> ): int8ndarray; + +/** +* Applies a callback function to elements in an input ndarray and assigns results to elements in a new output ndarray. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Uint32Array = require( '@stdlib/array/uint32' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( z ) { +* return z * 10; +* } +* +* var buffer = new Uint32Array( [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'uint32', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var y = map( x, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ 20, 30, 40 ], [ 80, 90, 100 ] ] +*/ +declare function map( x: uint32ndarray, fcn: Callback, thisArg?: ThisParameterType> ): uint32ndarray; + +/** +* Applies a callback function to elements in an input ndarray and assigns results to elements in a new output ndarray. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Uint16Array = require( '@stdlib/array/uint16' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( z ) { +* return z * 10; +* } +* +* var buffer = new Uint16Array( [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'uint16', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var y = map( x, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ 20, 30, 40 ], [ 80, 90, 100 ] ] +*/ +declare function map( x: uint16ndarray, fcn: Callback, thisArg?: ThisParameterType> ): uint16ndarray; + +/** +* Applies a callback function to elements in an input ndarray and assigns results to elements in a new output ndarray. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Uint8Array = require( '@stdlib/array/uint8' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( z ) { +* return z * 10; +* } +* +* var buffer = new Uint8Array( [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'uint8', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var y = map( x, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ 20, 30, 40 ], [ 80, 90, 100 ] ] +*/ +declare function map( x: uint8ndarray, fcn: Callback, thisArg?: ThisParameterType> ): uint8ndarray; + +/** +* Applies a callback function to elements in an input ndarray and assigns results to elements in a new output ndarray. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( z ) { +* return z * 10; +* } +* +* var buffer = new Uint8ClampedArray( [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'uint8c', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var y = map( x, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ 20, 30, 40 ], [ 80, 90, 100 ] ] +*/ +declare function map( x: uint8cndarray, fcn: Callback, thisArg?: ThisParameterType> ): uint8cndarray; + +/** +* Applies a callback function to elements in an input ndarray and assigns results to elements in a new output ndarray. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var BooleanArray = require( '@stdlib/array/bool' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function invert( z ) { +* return !z; +* } +* +* var buffer = new BooleanArray( [ false, false, false, false, false, false, false, false, false, false, false, false ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'bool', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var y = map( x, invert ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ true, true, true ], [ true, true, true ] ] +*/ +declare function map( x: boolndarray, fcn: Callback, thisArg?: ThisParameterType> ): boolndarray; + +/** +* Applies a callback function to elements in an input ndarray and assigns results to elements in a new output ndarray. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( z ) { +* return z * 10.0; +* } +* +* 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 = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var y = map( x, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ 20.0, 30.0, 40.0 ], [ 80.0, 90.0, 100.0 ] ] +*/ +declare function map( x: genericndarray, fcn: Callback, thisArg?: ThisParameterType> ): genericndarray; + +/** +* Applies a callback function to elements in an input ndarray and assigns results to elements in a new output ndarray. +* +* @param x - input ndarray +* @param options - options +* @param options.dtype - output ndarray data type +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* 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 +* +* var opts = { +* 'dtype': 'float64' +* }; +* var y = map( x, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ 20.0, 30.0, 40.0 ], [ 80.0, 90.0, 100.0 ] ] +*/ +declare function map( x: typedndarray, options: Float64Options, fcn: Callback, thisArg?: ThisParameterType> ): float64ndarray; + +/** +* Applies a callback function to elements in an input ndarray and assigns results to elements in a new output ndarray. +* +* @param x - input ndarray +* @param options - options +* @param options.dtype - output ndarray data type +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* 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 +* +* var opts = { +* 'dtype': 'float32' +* }; +* var y = map( x, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ 20.0, 30.0, 40.0 ], [ 80.0, 90.0, 100.0 ] ] +*/ +declare function map( x: typedndarray, options: Float32Options, fcn: Callback, thisArg?: ThisParameterType> ): float32ndarray; + +/** +* Applies a callback function to elements in an input ndarray and assigns results to elements in a new output ndarray. +* +* @param x - input ndarray +* @param options - options +* @param options.dtype - output ndarray data type +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var Complex128 = require( '@stdlib/complex/float64/ctor' ); +* +* function toComplex( z ) { +* return new Complex128( z, 0.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 opts = { +* 'dtype': 'complex128' +* }; +* var y = map( x, opts, toComplex ); +* // returns +*/ +declare function map( x: typedndarray, options: Complex128Options, fcn: Callback, thisArg?: ThisParameterType> ): complex128ndarray; + +/** +* Applies a callback function to elements in an input ndarray and assigns results to elements in a new output ndarray. +* +* @param x - input ndarray +* @param options - options +* @param options.dtype - output ndarray data type +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var Complex64 = require( '@stdlib/complex/float32/ctor' ); +* +* function toComplex( z ) { +* return new Complex64( z, 0.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 opts = { +* 'dtype': 'complex64' +* }; +* var y = map( x, opts, toComplex ); +* // returns +*/ +declare function map( x: typedndarray, options: Complex64Options, fcn: Callback, thisArg?: ThisParameterType> ): complex64ndarray; + +/** +* Applies a callback function to elements in an input ndarray and assigns results to elements in a new output ndarray. +* +* @param x - input ndarray +* @param options - options +* @param options.dtype - output ndarray data type +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* 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 +* +* var opts = { +* 'dtype': 'int32' +* }; +* var y = map( x, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ 20, 30, 40 ], [ 80, 90, 100 ] ] +*/ +declare function map( x: typedndarray, options: Int32Options, fcn: Callback, thisArg?: ThisParameterType> ): int32ndarray; + +/** +* Applies a callback function to elements in an input ndarray and assigns results to elements in a new output ndarray. +* +* @param x - input ndarray +* @param options - options +* @param options.dtype - output ndarray data type +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* 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 +* +* var opts = { +* 'dtype': 'int16' +* }; +* var y = map( x, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ 20, 30, 40 ], [ 80, 90, 100 ] ] +*/ +declare function map( x: typedndarray, options: Int16Options, fcn: Callback, thisArg?: ThisParameterType> ): int16ndarray; + +/** +* Applies a callback function to elements in an input ndarray and assigns results to elements in a new output ndarray. +* +* @param x - input ndarray +* @param options - options +* @param options.dtype - output ndarray data type +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* 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 +* +* var opts = { +* 'dtype': 'int8' +* }; +* var y = map( x, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ 20, 30, 40 ], [ 80, 90, 100 ] ] +*/ +declare function map( x: typedndarray, options: Int8Options, fcn: Callback, thisArg?: ThisParameterType> ): int8ndarray; + +/** +* Applies a callback function to elements in an input ndarray and assigns results to elements in a new output ndarray. +* +* @param x - input ndarray +* @param options - options +* @param options.dtype - output ndarray data type +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* 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 +* +* var opts = { +* 'dtype': 'uint32' +* }; +* var y = map( x, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ 20, 30, 40 ], [ 80, 90, 100 ] ] +*/ +declare function map( x: typedndarray, options: Uint32Options, fcn: Callback, thisArg?: ThisParameterType> ): uint32ndarray; + +/** +* Applies a callback function to elements in an input ndarray and assigns results to elements in a new output ndarray. +* +* @param x - input ndarray +* @param options - options +* @param options.dtype - output ndarray data type +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* 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 +* +* var opts = { +* 'dtype': 'uint16' +* }; +* var y = map( x, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ 20, 30, 40 ], [ 80, 90, 100 ] ] +*/ +declare function map( x: typedndarray, options: Uint16Options, fcn: Callback, thisArg?: ThisParameterType> ): uint16ndarray; + +/** +* Applies a callback function to elements in an input ndarray and assigns results to elements in a new output ndarray. +* +* @param x - input ndarray +* @param options - options +* @param options.dtype - output ndarray data type +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* 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 +* +* var opts = { +* 'dtype': 'uint8' +* }; +* var y = map( x, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ 20, 30, 40 ], [ 80, 90, 100 ] ] +*/ +declare function map( x: typedndarray, options: Uint8Options, fcn: Callback, thisArg?: ThisParameterType> ): uint8ndarray; + +/** +* Applies a callback function to elements in an input ndarray and assigns results to elements in a new output ndarray. +* +* @param x - input ndarray +* @param options - options +* @param options.dtype - output ndarray data type +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* 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 +* +* var opts = { +* 'dtype': 'uint8c' +* }; +* var y = map( x, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ 20, 30, 40 ], [ 80, 90, 100 ] ] +*/ +declare function map( x: typedndarray, options: Uint8COptions, fcn: Callback, thisArg?: ThisParameterType> ): uint8cndarray; + +/** +* Applies a callback function to elements in an input ndarray and assigns results to elements in a new output ndarray. +* +* @param x - input ndarray +* @param options - options +* @param options.dtype - output ndarray data type +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* var Boolean = require( '@stdlib/boolean/ctor' ); +* +* function toBoolean( z ) { +* return Boolean( 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 opts = { +* 'dtype': 'bool' +* }; +* var y = map( x, opts, toBoolean ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ true, true, true ], [ true, true, true ] ] +*/ +declare function map( x: typedndarray, options: BoolOptions, fcn: Callback, thisArg?: ThisParameterType> ): boolndarray; + +/** +* Applies a callback function to elements in an input ndarray and assigns results to elements in a new output ndarray. +* +* @param x - input ndarray +* @param options - options +* @param options.dtype - output ndarray data type +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* 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 +* +* var opts = { +* 'dtype': 'generic' +* }; +* var y = map( x, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ 20, 30, 40 ], [ 80, 90, 100 ] ] +*/ +declare function map( x: typedndarray, options: GenericOptions, fcn: Callback, thisArg?: ThisParameterType> ): genericndarray; + +// EXPORTS // + +export = map; diff --git a/map/docs/types/test.ts b/map/docs/types/test.ts new file mode 100644 index 00000000..7c436d27 --- /dev/null +++ b/map/docs/types/test.ts @@ -0,0 +1,153 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/// + +import empty = require( './../../../base/empty' ); +import zeros = require( './../../../base/zeros' ); +import map = require( './index' ); + +/** +* Evaluates the identity function. +* +* @param x - input value +* @returns input value +*/ +function identity( x: any ): any { + return x; +} + +// The function returns an ndarray... +{ + const sh = [ 2, 2 ]; + const ord = 'row-major'; + + map( zeros( 'float64', sh, ord ), identity ); // $ExpectType float64ndarray + map( zeros( 'float64', sh, ord ), identity, {} ); // $ExpectType float64ndarray + map( zeros( 'float32', sh, ord ), identity ); // $ExpectType float32ndarray + map( zeros( 'float32', sh, ord ), identity, {} ); // $ExpectType float32ndarray + map( zeros( 'complex64', sh, ord ), identity ); // $ExpectType complex64ndarray + map( zeros( 'complex64', sh, ord ), identity, {} ); // $ExpectType complex64ndarray + map( zeros( 'complex128', sh, ord ), identity ); // $ExpectType complex128ndarray + map( zeros( 'complex128', sh, ord ), identity, {} ); // $ExpectType complex128ndarray + map( zeros( 'int32', sh, ord ), identity ); // $ExpectType int32ndarray + map( zeros( 'int32', sh, ord ), identity, {} ); // $ExpectType int32ndarray + map( zeros( 'int16', sh, ord ), identity ); // $ExpectType int16ndarray + map( zeros( 'int16', sh, ord ), identity, {} ); // $ExpectType int16ndarray + map( zeros( 'int8', sh, ord ), identity ); // $ExpectType int8ndarray + map( zeros( 'int8', sh, ord ), identity, {} ); // $ExpectType int8ndarray + map( zeros( 'uint32', sh, ord ), identity ); // $ExpectType uint32ndarray + map( zeros( 'uint32', sh, ord ), identity, {} ); // $ExpectType uint32ndarray + map( zeros( 'uint16', sh, ord ), identity ); // $ExpectType uint16ndarray + map( zeros( 'uint16', sh, ord ), identity, {} ); // $ExpectType uint16ndarray + map( zeros( 'uint8', sh, ord ), identity ); // $ExpectType uint8ndarray + map( zeros( 'uint8', sh, ord ), identity, {} ); // $ExpectType uint8ndarray + map( zeros( 'uint8c', sh, ord ), identity ); // $ExpectType uint8cndarray + map( zeros( 'uint8c', sh, ord ), identity, {} ); // $ExpectType uint8cndarray + map( empty( 'bool', sh, ord ), identity ); // $ExpectType boolndarray + map( empty( 'bool', sh, ord ), identity, {} ); // $ExpectType boolndarray + map( zeros( 'generic', sh, ord ), identity ); // $ExpectType genericndarray + map( zeros( 'generic', sh, ord ), identity, {} ); // $ExpectType genericndarray + + + map( zeros( 'generic', sh, ord ), { 'dtype': 'float64' }, identity ); // $ExpectType float64ndarray + map( zeros( 'generic', sh, ord ), { 'dtype': 'float64' }, identity, {} ); // $ExpectType float64ndarray + map( zeros( 'generic', sh, ord ), { 'dtype': 'float32' }, identity ); // $ExpectType float32ndarray + map( zeros( 'generic', sh, ord ), { 'dtype': 'float32' }, identity, {} ); // $ExpectType float32ndarray + map( zeros( 'generic', sh, ord ), { 'dtype': 'complex64' }, identity ); // $ExpectType complex64ndarray + map( zeros( 'generic', sh, ord ), { 'dtype': 'complex64' }, identity, {} ); // $ExpectType complex64ndarray + map( zeros( 'generic', sh, ord ), { 'dtype': 'complex128' }, identity ); // $ExpectType complex128ndarray + map( zeros( 'generic', sh, ord ), { 'dtype': 'complex128' }, identity, {} ); // $ExpectType complex128ndarray + map( zeros( 'generic', sh, ord ), { 'dtype': 'int32' }, identity ); // $ExpectType int32ndarray + map( zeros( 'generic', sh, ord ), { 'dtype': 'int32' }, identity, {} ); // $ExpectType int32ndarray + map( zeros( 'generic', sh, ord ), { 'dtype': 'int16' }, identity ); // $ExpectType int16ndarray + map( zeros( 'generic', sh, ord ), { 'dtype': 'int16' }, identity, {} ); // $ExpectType int16ndarray + map( zeros( 'generic', sh, ord ), { 'dtype': 'int8' }, identity ); // $ExpectType int8ndarray + map( zeros( 'generic', sh, ord ), { 'dtype': 'int8' }, identity, {} ); // $ExpectType int8ndarray + map( zeros( 'generic', sh, ord ), { 'dtype': 'uint32' }, identity ); // $ExpectType uint32ndarray + map( zeros( 'generic', sh, ord ), { 'dtype': 'uint32' }, identity, {} ); // $ExpectType uint32ndarray + map( zeros( 'generic', sh, ord ), { 'dtype': 'uint16' }, identity ); // $ExpectType uint16ndarray + map( zeros( 'generic', sh, ord ), { 'dtype': 'uint16' }, identity, {} ); // $ExpectType uint16ndarray + map( zeros( 'generic', sh, ord ), { 'dtype': 'uint8' }, identity ); // $ExpectType uint8ndarray + map( zeros( 'generic', sh, ord ), { 'dtype': 'uint8' }, identity, {} ); // $ExpectType uint8ndarray + map( zeros( 'generic', sh, ord ), { 'dtype': 'uint8c' }, identity ); // $ExpectType uint8cndarray + map( zeros( 'generic', sh, ord ), { 'dtype': 'uint8c' }, identity, {} ); // $ExpectType uint8cndarray + map( zeros( 'generic', sh, ord ), { 'dtype': 'bool' }, identity ); // $ExpectType boolndarray + map( zeros( 'generic', sh, ord ), { 'dtype': 'bool' }, identity, {} ); // $ExpectType boolndarray + map( zeros( 'generic', sh, ord ), { 'dtype': 'generic' }, identity ); // $ExpectType genericndarray + map( zeros( 'generic', sh, ord ), { 'dtype': 'generic' }, identity, {} ); // $ExpectType genericndarray + +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + map( 5, identity ); // $ExpectError + map( true, identity ); // $ExpectError + map( false, identity ); // $ExpectError + map( null, identity ); // $ExpectError + map( undefined, identity ); // $ExpectError + map( {}, identity ); // $ExpectError + map( [ 1 ], identity ); // $ExpectError + map( ( x: number ): number => x, identity ); // $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' ); + + map( x, '5' ); // $ExpectError + map( x, true ); // $ExpectError + map( x, false ); // $ExpectError + map( x, null ); // $ExpectError + map( x, undefined ); // $ExpectError + map( x, {} ); // $ExpectError + map( x, [ 1 ] ); // $ExpectError +} + +// The compiler throws an error if the function is provided an options argument which is not an object... +{ + const x = zeros( 'generic', [ 2, 2 ], 'row-major' ); + + map( x, '10', identity, {} ); // $ExpectError + map( x, 10, identity, {} ); // $ExpectError + map( x, false, identity, {} ); // $ExpectError + map( x, true, identity, {} ); // $ExpectError + map( x, [], identity, {} ); // $ExpectError + map( x, ( x: number ): number => x, identity, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a `dtype` option which is not a valid data type... +{ + const x = zeros( 'generic', [ 2, 2 ], 'row-major' ); + + map( x, { 'dtype': '10' }, identity ); // $ExpectError + map( x, { 'dtype': 10 }, identity ); // $ExpectError + map( x, { 'dtype': null }, identity ); // $ExpectError + map( x, { 'dtype': false }, identity ); // $ExpectError + map( x, { 'dtype': true }, identity ); // $ExpectError + map( x, { 'dtype': [] }, identity ); // $ExpectError + map( x, { 'dtype': {} }, identity ); // $ExpectError + map( x, { 'dtype': ( x: number ): number => x }, identity ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + map(); // $ExpectError + map( zeros( 'float64', [ 2, 2 ], 'row-major' ) ); // $ExpectError + map( zeros( 'float64', [ 2, 2 ], 'row-major' ), {}, ( x: number ): number => x, {}, {} ); // $ExpectError +} diff --git a/map/examples/index.js b/map/examples/index.js new file mode 100644 index 00000000..5c46b6b6 --- /dev/null +++ b/map/examples/index.js @@ -0,0 +1,38 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var ndarray2array = require( './../../to-array' ); +var naryFunction = require( '@stdlib/utils/nary-function' ); +var ndarray = require( './../../ctor' ); +var map = require( './../lib' ); + +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 ) ); + +var y = map( x, naryFunction( abs, 1 ) ); +console.log( ndarray2array( y ) ); diff --git a/map/lib/index.js b/map/lib/index.js new file mode 100644 index 00000000..42914b34 --- /dev/null +++ b/map/lib/index.js @@ -0,0 +1,58 @@ +/** +* @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'; + +/** +* Apply a callback function to elements in an input ndarray and assign results to elements in an output ndarray. +* +* @module @stdlib/ndarray/map +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* var map = require( '@stdlib/ndarray/map' ); +* +* 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 +* +* var y = map( x, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ 20.0, 30.0, 40.0 ], [ 80.0, 90.0, 100.0 ] ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/map/lib/main.js b/map/lib/main.js new file mode 100644 index 00000000..c30965fc --- /dev/null +++ b/map/lib/main.js @@ -0,0 +1,122 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isPlainObject = require( '@stdlib/assert/is-plain-object' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var emptyLike = require( './../../empty-like' ); +var base = require( './../../base/map' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Applies a callback function to elements in an input ndarray and assigns results to elements in a new output ndarray. +* +* @param {ndarray} x - input ndarray +* @param {Options} [options] - function options +* @param {string} [options.dtype] - output array data type +* @param {Callback} fcn - callback function +* @param {*} [thisArg] - callback execution context +* @throws {TypeError} first argument must have a recognized data type +* @throws {TypeError} callback argument must be a function +* @throws {TypeError} options argument must be an object +* @returns {ndarray} output ndarray +* +* @example +* 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 +* +* var y = map( x, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ [ 20.0, 30.0, 40.0 ], [ 80.0, 90.0, 100.0 ] ] +*/ +function map( x, options, fcn, thisArg ) { + var hasOpts; + var clbk; + var opts; + var ctx; + var y; + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object. Value: `%s`.', x ) ); + } + if ( arguments.length < 3 ) { + clbk = options; + } else if ( arguments.length === 3 ) { + if ( isFunction( options ) ) { + clbk = options; + ctx = fcn; + } else { + hasOpts = true; + opts = options; + clbk = fcn; + } + } else { + hasOpts = true; + opts = options; + clbk = fcn; + ctx = thisArg; + } + if ( !isFunction( clbk ) ) { + throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', clbk ) ); + } + if ( hasOpts ) { + if ( !isPlainObject( opts ) ) { + throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', opts ) ); + } + if ( hasOwnProp( opts, 'dtype' ) ) { + // Delegate `dtype` validation to `emptyLike` during output array creation: + y = emptyLike( x, { + 'dtype': opts.dtype + }); + } else { + // We only support a `dtype` option, so avoid passing along any other options: + y = emptyLike( x ); + } + } else { + y = emptyLike( x ); + } + base( [ x, y ], clbk, ctx ); + return y; +} + + +// EXPORTS // + +module.exports = map; diff --git a/map/package.json b/map/package.json new file mode 100644 index 00000000..1679a2cb --- /dev/null +++ b/map/package.json @@ -0,0 +1,64 @@ +{ + "name": "@stdlib/ndarray/map", + "version": "0.0.0", + "description": "Apply a callback to elements in an input ndarray and assign results to elements in a new output ndarray.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "strided", + "array", + "ndarray", + "map", + "apply", + "unary", + "foreach", + "transform", + "for-each" + ], + "__stdlib__": {} +} diff --git a/map/test/test.js b/map/test/test.js new file mode 100644 index 00000000..89529dfc --- /dev/null +++ b/map/test/test.js @@ -0,0 +1,609 @@ +/** +* @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( './../../ctor' ); +var shape2strides = require( './../../base/shape2strides' ); +var strides2offset = require( './../../base/strides2offset' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Float32Array = require( '@stdlib/array/float32' ); +var isSameFloat64Array = require( '@stdlib/assert/is-same-float64array' ); +var isSameFloat32Array = require( '@stdlib/assert/is-same-float32array' ); +var numel = require( './../../base/numel' ); +var dfill = require( '@stdlib/blas/ext/base/dfill' ); +var sfill = require( '@stdlib/blas/ext/base/sfill' ); +var blockSize = require( './../../base/unary-tiling-block-size' ); +var map = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof map, '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() { + map( value, scale ); + }; + } + + function scale( z ) { + return z * 10.0; + } +}); + +tape( 'the function throws an error if a callback argument which is not a function', function test( t ) { + var values; + var x; + var i; + + values = [ + '5', + 5, + true, + false, + null, + void 0, + {}, + [] + ]; + x = ndarray( 'generic', ones( 4, 'generic' ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided ' + values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + map( x, value ); + }; + } +}); + +tape( 'the function throws an error if callback argument which is not a function (options)', function test( t ) { + var values; + var opts; + var x; + var i; + + values = [ + '5', + 5, + true, + false, + null, + void 0, + {}, + [] + ]; + x = ndarray( 'generic', ones( 4, 'generic' ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + opts = { + 'dtype': 'float64' + }; + + 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() { + map( x, opts, value ); + }; + } +}); + +tape( 'the function throws an error if provided an options argument which is not an object', function test( t ) { + var values; + var x; + var i; + + values = [ + '5', + 5, + true, + false, + null, + void 0, + [] + ]; + x = ndarray( 'generic', ones( 4, 'generic' ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+ values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + map( x, value, scale ); + }; + } + + function scale( z ) { + return z * 10.0; + } +}); + +tape( 'the function throws an error if provided an invalid `dtype` option', function test( t ) { + var values; + var x; + var i; + + values = [ + 'foo', + 'bar', + 1, + NaN, + true, + false, + void 0, + null, + [], + {}, + function noop() {} + ]; + x = ndarray( 'generic', ones( 4, 'generic' ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+ values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var opts = { + 'dtype': value + }; + map( x, opts, scale ); + }; + } + + function scale( z ) { + return z * 10.0; + } +}); + +tape( 'the function applies a callback to each indexed element in an input ndarray and returns an output ndarray with mapped values (row-major, contiguous)', function test( t ) { + var expected; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 1, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + x = ndarray( dt, ones( numel( sh ), dt ), sh, st, o, ord ); + + y = map( x, scale ); + + expected = new Float64Array([ + 10.0, + 10.0, + 10.0, + 10.0 + ]); + t.strictEqual( isSameFloat64Array( y.data, expected ), true, 'returns expected value' ); + + t.end(); + + function scale( z ) { + return z * 10.0; + } +}); + +tape( 'the function applies a callback to each indexed element in an input ndarray and returns an output ndarray with mapped values (column-major, contiguous)', function test( t ) { + var expected; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'column-major'; + sh = [ 2, 1, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + x = ndarray( dt, ones( numel( sh ), dt ), sh, st, o, ord ); + + y = map( x, scale ); + + expected = new Float64Array([ + 10.0, + 10.0, + 10.0, + 10.0 + ]); + t.strictEqual( isSameFloat64Array( y.data, expected ), true, 'returns expected value' ); + + t.end(); + + function scale( z ) { + return z * 10.0; + } +}); + +tape( 'the function applies a callback to each indexed element in an input ndarray and returns an output ndarray with mapped values (row-major, contiguous, options)', function test( t ) { + var expected; + var opts; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 1, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + x = ndarray( dt, ones( numel( sh ), dt ), sh, st, o, ord ); + + opts = {}; + y = map( x, opts, scale ); + + expected = new Float64Array([ + 10.0, + 10.0, + 10.0, + 10.0 + ]); + t.strictEqual( isSameFloat64Array( y.data, expected ), true, 'returns expected value' ); + + opts = { + 'dtype': 'float32' + }; + y = map( x, opts, scale ); + + expected = new Float32Array([ + 10.0, + 10.0, + 10.0, + 10.0 + ]); + t.strictEqual( isSameFloat32Array( y.data, expected ), true, 'returns expected value' ); + + t.end(); + + function scale( z ) { + return z * 10.0; + } +}); + +tape( 'the function applies a callback to each indexed element in an input ndarray and returns an output ndarray with mapped values (column-major, contiguous, options)', function test( t ) { + var expected; + var opts; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'column-major'; + sh = [ 2, 1, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + x = ndarray( dt, ones( numel( sh ), dt ), sh, st, o, ord ); + + opts = {}; + y = map( x, opts, scale ); + + expected = new Float64Array([ + 10.0, + 10.0, + 10.0, + 10.0 + ]); + t.strictEqual( isSameFloat64Array( y.data, expected ), true, 'returns expected value' ); + + opts = { + 'dtype': 'float32' + }; + y = map( x, opts, scale ); + + expected = new Float32Array([ + 10.0, + 10.0, + 10.0, + 10.0 + ]); + t.strictEqual( isSameFloat32Array( y.data, expected ), true, 'returns expected value' ); + + t.end(); + + function scale( z ) { + return z * 10.0; + } +}); + +tape( 'the function applies a callback to each indexed element in an input ndarray and returns an output ndarray with mapped values (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; + var y; + + 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 ); + + y = map( x, scale ); + expected = new Float64Array( y.length ); + dfill( y.length, 10.0, expected, 1 ); + + t.strictEqual( isSameFloat64Array( y.data, expected ), true, 'returns expected value' ); + + t.end(); + + function scale( z ) { + return z * 10.0; + } +}); + +tape( 'the function applies a callback to each indexed element in an input ndarray and returns an output ndarray with mapped values (row-major, non-contiguous, large arrays, options)', function test( t ) { + var expected; + var bsize; + var opts; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + 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 ); + + opts = { + 'dtype': 'float32' + }; + y = map( x, opts, scale ); + expected = new Float32Array( y.length ); + sfill( y.length, 10.0, expected, 1 ); + + t.strictEqual( isSameFloat32Array( y.data, expected ), true, 'returns expected value' ); + + t.end(); + + function scale( z ) { + return z * 10.0; + } +}); + +tape( 'the function applies a callback to each indexed element in an input ndarray and returns an output ndarray with mapped values (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; + var y; + + 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 ); + + y = map( x, scale ); + + expected = new Float64Array( y.length ); + dfill( y.length, 10.0, expected, 1 ); + + t.strictEqual( isSameFloat64Array( y.data, expected ), true, 'returns expected value' ); + + t.end(); + + function scale( z ) { + return z * 10.0; + } +}); + +tape( 'the function applies a callback to each indexed element in an input ndarray and returns an output ndarray with mapped values (column-major, non-contiguous, large arrays, options)', function test( t ) { + var expected; + var bsize; + var opts; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + 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 ); + + opts = { + 'dtype': 'float32' + }; + y = map( x, opts, scale ); + + expected = new Float32Array( y.length ); + sfill( y.length, 10.0, expected, 1 ); + + t.strictEqual( isSameFloat32Array( y.data, expected ), true, 'returns expected value' ); + + t.end(); + + function scale( z ) { + return z * 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; + var y; + + 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 + }; + y = map( x, scale, ctx ); + + expected = new Float64Array([ + 10.0, + 10.0, + 10.0, + 10.0 + ]); + t.strictEqual( isSameFloat64Array( y.data, expected ), true, 'returns expected value' ); + t.strictEqual( ctx.count, 4, 'returns expected value' ); + + t.end(); + + function scale( z ) { + this.count += 1; // eslint-disable-line no-invalid-this + return z * 10.0; + } +}); + +tape( 'the function supports providing a callback execution context (options)', function test( t ) { + var expected; + var ctx; + var ord; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 1, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + x = ndarray( dt, ones( numel( sh ), dt ), sh, st, o, ord ); + + ctx = { + 'count': 0 + }; + y = map( x, {}, scale, ctx ); + + expected = new Float64Array([ + 10.0, + 10.0, + 10.0, + 10.0 + ]); + t.strictEqual( isSameFloat64Array( y.data, expected ), true, 'returns expected value' ); + t.strictEqual( ctx.count, 4, 'returns expected value' ); + + t.end(); + + function scale( z ) { + this.count += 1; // eslint-disable-line no-invalid-this + return z * 10.0; + } +});