diff --git a/lib/node_modules/@stdlib/array/base/take-map/README.md b/lib/node_modules/@stdlib/array/base/take-map/README.md new file mode 100644 index 000000000000..5d426e425772 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/take-map/README.md @@ -0,0 +1,147 @@ + + +# takeMap + +> Take elements from an array and return a new array after applying a mapping function. + +
+ +## Usage + +```javascript +var takeMap = require( '@stdlib/array/base/take-map' ); +``` + +### takeMap( x, indices, mode, clbk ) + +Takes elements from an array and returns a new array after applying a mapping function. + +```javascript +var x = [ 1, 2, 3, 4 ]; + +function customMapping( value ) { + return value * 2; +} + +var y = takeMap( x, [ 1, 3 ], 'throw', customMapping ); +// returns [ 4, 8 ] +``` + +The function supports the following parameters: + +- **x**: input array. +- **indices**: list of indices. +- **mode**: index [mode][@stdlib/ndarray/base/ind]. +- **clbk**: function to apply. + +### takeMap.assign( x, indices, mode, out, stride, offset, clbk ) + +> Takes elements from an array and assigns the values to elements in a provided output array. + +```javascript +var x = [ 1, 2, 3, 4 ]; + +var out = [ 0, 0, 0, 0, 0, 0 ]; +var indices = [ 0, 0, 1, 1, 3, 3 ]; + +function clbk( val ) { + return val * 2; +} + +var arr = takeMap.assign( x, indices, 'throw', out, -1, out.length-1, clbk ); +// returns [ 8, 8, 4, 4, 2, 2 ] + +var bool = ( arr === out ); +// returns true +``` + +The function supports the following parameters: + +- **x**: input array. +- **indices**: list of indices. +- **mode**: index [mode][@stdlib/ndarray/base/ind]. +- **out**: output array. +- **stride**: output array stride. +- **offset**: output array offset. +- **clbk**: callback function. + +
+ + + +
+ +
+ + + +
+ +## Examples + + + +```javascript +var filledBy = require( '@stdlib/array/base/filled-by' ); +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); +var linspace = require( '@stdlib/array/base/linspace' ); +var takeMap = require( '@stdlib/array/base/take-map' ); + +// Generate a linearly spaced array: +var x = linspace( 0, 100, 11 ); +console.log( x ); + +// Generate an array of random indices: +var N = discreteUniform( 5, 15 ); +var indices = filledBy( N, discreteUniform.factory( 0, x.length-1 ) ); +console.log( indices ); + +// Define a mapping function (e.g., square the value): +function square( val ) { + return val * val; +} + +// Take a random sample of elements from `x` and apply the mapping function: +var y = takeMap( x, indices, 'throw', square ); +console.log( y ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/array/base/take-map/benchmark/benchmark.assign.length.js b/lib/node_modules/@stdlib/array/base/take-map/benchmark/benchmark.assign.length.js new file mode 100644 index 000000000000..5f150f679fd5 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/take-map/benchmark/benchmark.assign.length.js @@ -0,0 +1,109 @@ +/** +* @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 pow = require( '@stdlib/math/base/special/pow' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var zeros = require( '@stdlib/array/zeros' ); +var isArray = require( '@stdlib/assert/is-array' ); +var pkg = require( './../package.json' ).name; +var takeMap = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var out; + var idx; + + idx = discreteUniform( len, 0, 3, { + 'dtype': 'generic' + }); + out = zeros( len, 'generic' ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var x; + var v; + var i; + + x = [ 1, 2, 3, 4 ]; + function clbk( val ) { + return val; + } + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = takeMap.assign( x, idx, 'throw', out, 1, 0, clbk ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an array' ); + } + } + b.toc(); + if ( !isArray( v ) ) { + b.fail( 'should return an array' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':assign:len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/base/take-map/benchmark/benchmark.js b/lib/node_modules/@stdlib/array/base/take-map/benchmark/benchmark.js new file mode 100644 index 000000000000..e1fef30bd2e1 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/take-map/benchmark/benchmark.js @@ -0,0 +1,56 @@ +/** +* @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 isArray = require( '@stdlib/assert/is-array' ); +var zeroTo = require( '@stdlib/array/base/zero-to' ); +var pkg = require( './../package.json' ).name; +var takeMap = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::copy:len=100', function benchmark( b ) { + var x; + var i; + var v; + + x = zeroTo( 100 ); + + function clbk( val ) { + return val; + } + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = takeMap( x, x, 'throw', clbk ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an array' ); + } + } + b.toc(); + if ( !isArray( v ) ) { + b.fail( 'should return an array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/array/base/take-map/benchmark/benchmark.length.js b/lib/node_modules/@stdlib/array/base/take-map/benchmark/benchmark.length.js new file mode 100644 index 000000000000..df46e8bd47f5 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/take-map/benchmark/benchmark.length.js @@ -0,0 +1,102 @@ +/** +* @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 pow = require( '@stdlib/math/base/special/pow' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var isArray = require( '@stdlib/assert/is-array' ); +var pkg = require( './../package.json' ).name; +var takeMap = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var idx = discreteUniform( len, 0, 3 ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var x; + var v; + var i; + + x = [ 1, 2, 3, 4 ]; + + function clbk( val ) { + return val; + } + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = takeMap( x, idx, 'throw', clbk ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an array' ); + } + } + b.toc(); + if ( !isArray( v ) ) { + b.fail( 'should return an array' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/base/take-map/docs/repl.txt b/lib/node_modules/@stdlib/array/base/take-map/docs/repl.txt new file mode 100644 index 000000000000..50ca76703374 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/take-map/docs/repl.txt @@ -0,0 +1,89 @@ + +{{alias}}( x, indices, mode, callback ) + Takes elements from an array and returns a new array after applying a + mapping function. + + If `indices` is an empty array, the function returns an empty array. + + Parameters + ---------- + x: ArrayLikeObject + Input array. + + indices: ArrayLikeObject + List of element indices. + + mode: string + Specifies how to handle an index outside the interval [0, max], where + `max` is the maximum possible array index. If equal to 'throw', the + function throws an error. If equal to 'normalize', the function throws + an error if provided an out-of-bounds normalized index. If equal to + 'wrap', the function wraps around an index using modulo arithmetic. If + equal to 'clamp', the function sets an index to either 0 (minimum index) + or the maximum index. + + callback : function + Map function to apply to selected elements of `x`. + + Returns + ------- + out: Array + Output array. + + Examples + -------- + > var x = [ -1, -2, -3, -4 ]; + > var y = {{alias}}( x, [ 1, 3 ], 'throw', {{alias:@stdlib/math/base/special/abs}} ) + [ 2, 4 ] + + +{{alias}}.assign( x, indices, mode, out, stride, offset, callback ) + Takes elements from an array, applies a mapping function, and assigns the + values to elements in a provided output array. + + Parameters + ---------- + x: ArrayLikeObject + Input array. + + indices: ArrayLikeObject + List of element indices. + + mode: string + Specifies how to handle an index outside the interval [0, max], where + `max` is the maximum possible array index. If equal to 'throw', the + function throws an error. If equal to 'normalize', the function throws + an error if provided an out-of-bounds normalized index. If equal to + 'wrap', the function wraps around an index using modulo arithmetic. If + equal to 'clamp', the function sets an index to either 0 (minimum index) + or the maximum index. + + out: ArrayLikeObject + Output array. + + stride: integer + Output array stride. + + offset: integer + Output array offset. + + callback : function + Map function to apply to selected elements of `x`. + + Returns + ------- + out: ArrayLikeObject + Output array. + + Examples + -------- + > var x = [ -1, -2, -3, -4 ]; + > var out = [ 0, 0, 0, 0 ]; + > var arr = {{alias}}.assign( x, [ 1, 3 ], 'throw', out, 2, 0 ,{{alias:@stdlib/math/base/special/abs}} ) + [ 2, 0, 4, 0 ] + > var bool = ( arr === out ) + true + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/array/base/take-map/docs/types/index.d.ts b/lib/node_modules/@stdlib/array/base/take-map/docs/types/index.d.ts new file mode 100644 index 000000000000..cbb0a7ec7cb3 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/take-map/docs/types/index.d.ts @@ -0,0 +1,206 @@ +/** +* @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 { Collection, AccessorArrayLike } from '@stdlib/types/array'; +import { Mode } from '@stdlib/types/ndarray'; + +/** +* Index array. +*/ +type IndexArray = Collection | AccessorArrayLike; + +/** +* Callback function invoked for each indexed element. +*/ +type Callback = ( value: T, index: number ) => U; + +/** +* Interface describing `takeMap`. +*/ +interface TakeMap { + /** + * Takes elements from an array based on an index array and applies a callback function to each element. + * + * @param x - input array + * @param indices - list of element indices + * @param mode - index mode + * @param clbk - callback function + * @returns output array + * + * @example + * var x = [ 1, 2, 3, 4 ]; + * + * function transform( v ) { + * return v * 2; + * } + * + * var y = takeMap( x, [ 1, 3 ], 'throw', transform ); + * // returns [ 4, 8 ] + */ + ( x: Collection, indices: IndexArray, mode: Mode, clbk: Callback ): Array; + + /** + * Takes elements from an array based on an index array and applies a callback function to each element. The function assigns the transformed values to elements in a provided output array. + * + * @param x - input array + * @param indices - list of element indices + * @param mode - index mode + * @param clbk - callback function + * @param out - output array + * @param stride - output array stride + * @param offset - output array offset + * @returns output array + * + * @example + * var Float64Array = require( '@stdlib/array/float64' ); + * + * var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + * var out = new Float64Array( [ 0.0, 0.0, 0.0, 0.0 ] ); + * + * function transform( v ) { + * return v * 2; + * } + * + * var arr = takeMap.assign( x, [ 1, 3 ], 'throw', transform, out, 2, 0 ); + * // returns [ 4.0, 0.0, 8.0, 0.0 ] + * + * var bool = ( arr === out ); + * // returns true + */ + assign( x: Collection | AccessorArrayLike, indices: IndexArray, mode: Mode, clbk: Callback, out: Collection, stride: number, offset: number ): Collection; + + /** + * Takes elements from an array based on an index array and applies a callback function to each element. The function assigns the transformed values to elements in a provided output array. + * + * @param x - input array + * @param indices - list of element indices + * @param mode - index mode + * @param clbk - callback function + * @param out - output array + * @param stride - output array stride + * @param offset - output array offset + * @returns output array + * + * @example + * var Float32Array = require( '@stdlib/array/float32' ); + * + * var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + * var out = new Float32Array( [ 0.0, 0.0, 0.0, 0.0 ] ); + * + * function transform( v ) { + * return v * 2; + * } + * + * var arr = takeMap.assign( x, [ 1, 3 ], 'throw', transform, out, 2, 0 ); + * // returns [ 4.0, 0.0, 8.0, 0.0 ] + * + * var bool = ( arr === out ); + * // returns true + */ + assign( x: Collection | AccessorArrayLike, indices: IndexArray, mode: Mode, clbk: Callback, out: AccessorArrayLike, stride: number, offset: number ): AccessorArrayLike; + + /** + * Takes elements from an array based on an index array and applies a callback function to each element. The function assigns the transformed values to elements in a provided output array. + * + * @param x - input array + * @param indices - list of element indices + * @param mode - index mode + * @param clbk - callback function + * @param out - output array + * @param stride - output array stride + * @param offset - output array offset + * @returns output array + * + * @example + * var Complex128Array = require( '@stdlib/array/float64' ); + * + * var x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + * var out = new Complex128Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] ); + * + * function transform( v ) { + * return v * 2; + * } + * + * var arr = takeMap.assign( x, [ 1, 3 ], 'throw', transform, out, 2, 0 ); + * // returns [ 4.0, 0.0, 8.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] + * + * var bool = ( arr === out ); + * // returns true + */ + assign( x: Collection | AccessorArrayLike, indices: IndexArray, mode: Mode, clbk: Callback, out: Collection, stride: number, offset: number ): Collection; +} + +/** +* Takes elements from an array based on an index array and applies a callback function to each element. +* +* @param x - input array +* @param indices - list of element indices +* @param mode - index mode +* @param clbk - callback function +* @returns output array +* +* @example +* var x = [ 1, 2, 3, 4 ]; +* +* function transform( v ) { +* return v * 2; +* } +* +* var y = takeMap( x, [ 1, 3 ], 'throw', transform ); +* // returns [ 4, 8 ] +*/ +declare function takeMap( x: Collection | AccessorArrayLike, indices: IndexArray, mode: Mode, clbk: Callback ): Collection; + +/** +* Takes elements from an array based on an index array and applies a callback function to each element. The function assigns the transformed values to elements in a provided output array. +* +* @param x - input array +* @param indices - list of element indices +* @param mode - index mode +* @param clbk - callback function +* @param out - output array +* @param stride - output array stride +* @param offset - output array offset +* @returns output array +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* +* var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); +* var out = new Float64Array( [ 0.0, 0.0, 0.0, 0.0 ] ); +* +* function transform( v ) { +* return v * 2; +* } +* +* var arr = takeMap.assign( x, [ 1, 3 ], 'throw', transform, out, 2, 0 ); +* // returns [ 4.0, 0.0, 8.0, 0.0 ] +* +* var bool = ( arr === out ); +* // returns true +*/ +declare function takeMapAssign( x: Collection | AccessorArrayLike, indices: IndexArray, mode: Mode, clbk: Callback, out: Collection, stride: number, offset: number ): Collection; + + +// EXPORTS // + +export = takeMap; diff --git a/lib/node_modules/@stdlib/array/base/take-map/docs/types/test.ts b/lib/node_modules/@stdlib/array/base/take-map/docs/types/test.ts new file mode 100644 index 000000000000..2eaef25ff043 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/take-map/docs/types/test.ts @@ -0,0 +1,85 @@ +/* +* @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 takeMap = require( './index' ); + +// TESTS // + +// The function returns an array... +{ + // eslint-disable-next-line @typescript-eslint/explicit-function-return-type + const transform = ( v: number ) => v * 2; + + takeMap( [ 1, 2, 3, 4 ], [ 1, 3 ], 'throw', transform ); // $ExpectType number[] + takeMap( [ 1, 2, 3, 4 ], [ 1, 3 ], 'normalize', transform ); // $ExpectType any[] + takeMap( [ 1, 2, 3, 4 ], [ 1, 3 ], 'clamp', transform ); // $ExpectType number[] + takeMap( [ '1', '2', '3', '4' ], [ 1, 3 ], 'wrap', transform ); // $ExpectType string[] +} + +// The compiler throws an error if the function is provided a first argument which is not an array-like object... +{ + // eslint-disable-next-line @typescript-eslint/explicit-function-return-type + const transform = ( v: number ) => v * 2; + + takeMap( 1, [ 1, 3 ], 'throw', transform ); // $ExpectError + takeMap( true, [ 1, 3 ], 'throw', transform ); // $ExpectError + takeMap( false, [ 1, 3 ], 'throw', transform ); // $ExpectError + takeMap( null, [ 1, 3 ], 'throw', transform ); // $ExpectError + takeMap( void 0, [ 1, 3 ], 'throw', transform ); // $ExpectError + takeMap( {}, [ 1, 3 ], 'throw', transform ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... +{ + // eslint-disable-next-line @typescript-eslint/explicit-function-return-type + const transform = ( v: number ) => v * 2; + + takeMap( [], 1, 'throw', transform ); // $ExpectError + takeMap( [], true, 'throw', transform ); // $ExpectError + takeMap( [], false, 'throw', transform ); // $ExpectError + takeMap( [], null, 'throw', transform ); // $ExpectError + takeMap( [], void 0, 'throw', transform ); // $ExpectError + takeMap( [], {}, 'throw', transform ); // $ExpectError +} + +// The compiler throws an error if the function is provided a third argument which is not a valid index mode... +{ + // eslint-disable-next-line @typescript-eslint/explicit-function-return-type + const transform = ( v: number ) => v * 2; + + takeMap( [], [ 1, 3 ], '1', transform ); // $ExpectError + takeMap( [], [ 1, 3 ], 1, transform ); // $ExpectError + takeMap( [], [ 1, 3 ], true, transform ); // $ExpectError + takeMap( [], [ 1, 3 ], false, transform ); // $ExpectError + takeMap( [], [ 1, 3 ], null, transform ); // $ExpectError + takeMap( [], [ 1, 3 ], void 0, transform ); // $ExpectError + takeMap( [], [ 1, 3 ], {}, transform ); // $ExpectError + takeMap( [], [ 1, 3 ], [], transform ); // $ExpectError + takeMap( [], [ 1, 3 ], ( x: number ): number => x, transform ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + // eslint-disable-next-line @typescript-eslint/explicit-function-return-type + const transform = ( v: number ) => v * 2; + + takeMap(); // $ExpectError + takeMap( [], [] ); // $ExpectError + takeMap( [], [], 'throw' ); // $ExpectError + takeMap( [], [], 'throw', transform, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/array/base/take-map/examples/index.js b/lib/node_modules/@stdlib/array/base/take-map/examples/index.js new file mode 100644 index 000000000000..0b513736c544 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/take-map/examples/index.js @@ -0,0 +1,42 @@ +/** +* @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 filledBy = require( '@stdlib/array/base/filled-by' ); +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); +var linspace = require( '@stdlib/array/base/linspace' ); +var takeMap = require( './../lib' ); + +// Generate a linearly spaced array: +var x = linspace( 0, 100, 11 ); +console.log( x ); + +// Generate an array of random indices: +var N = discreteUniform( 5, 15 ); +var indices = filledBy( N, discreteUniform.factory( 0, x.length-1 ) ); +console.log( indices ); + +// Define a mapping function (e.g., square the value): +function square( val ) { + return val * val; +} + +// Take a random sample of elements from `x` and apply the mapping function: +var y = takeMap( x, indices, 'throw', square ); +console.log( y ); diff --git a/lib/node_modules/@stdlib/array/base/take-map/lib/assign.js b/lib/node_modules/@stdlib/array/base/take-map/lib/assign.js new file mode 100644 index 000000000000..5c868c946569 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/take-map/lib/assign.js @@ -0,0 +1,277 @@ +/** +* @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 isComplexDataType = require( '@stdlib/array/base/assert/is-complex-floating-point-data-type' ); +var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +var reinterpret = require( '@stdlib/strided/base/reinterpret-complex' ); +var ind = require( '@stdlib/ndarray/base/ind' ).factory; + + +// FUNCTIONS // + +/** +* Takes elements from an indexed array and assigns the values to elements in an indexed output array. +* +* @private +* @param {Collection} x - input array. +* @param {IntegerArray} indices - list of indices. +* @param {string} mode - index mode. +* @param {Collection} out - output array. +* @param {integer} stride - output array stride. +* @param {NonNegativeInteger} offset - output array offset. +* @param {Function} clbk - callback function applied to each selected element. +* @returns {Collection} output array. +* +* @example +* var x = [ 1, 2, 3, 4 ]; +* var indices = [ 3, 1, 2, 0 ]; +* var out = [ 0, 0, 0, 0 ]; +* +* function clbk( val ){ + return val; +} +* +* var arr = takeMapIndexed( x, indices, 'throw', out, 1, 0, clbk ); +* // returns [ 4, 2, 3, 1 ] +*/ +function takeMapIndexed( x, indices, mode, out, stride, offset, clbk ) { + var getIndex; + var max; + var io; + var i; + var j; + + // Resolve a function for returning an index according to the specified index mode: + getIndex = ind( mode ); + + // Resolve the maximum index: + max = x.length - 1; + + // Extract each desired element from the provided array... + io = offset; + for ( i = 0; i < indices.length; i++ ) { + j = getIndex( indices[ i ], max ); + out[ io ] = x[ j ]; + io += stride; + } + for (i = 0; i[ 1.0, 2.0, 1.0, 2.0, 3.0, 4.0, 3.0, 4.0 ] +*/ +function complexMap( x, indices, mode, out, stride, offset, clbk ) { + var getIndex; + var idata; + var iget; + var max; + var io; + var so; + var i; + var j; + var k; + + idata = indices.data; + iget = indices.accessors[ 0 ]; + + // Resolve a function for returning an index according to the specified index mode: + getIndex = ind( mode ); + + // Resolve the maximum index: + max = ( x.length/2 ) - 1; // resolve the length of the original complex array + + // Extract each desired element from the provided array... + so = stride * 2; // note: multiply by 2, as real-valued array consists of interleaved real and imaginary components + io = offset * 2; + for ( i = 0; i < idata.length; i++ ) { + j = getIndex( iget( idata, i ), max ); + k = j * 2; + + // eslint-disable-next-line no-useless-call + out[ io ] = clbk.call( null, x[ k ], k ); + + // eslint-disable-next-line no-useless-call + out[ io+1 ] = clbk.call( null, x[ k+1 ], k+1 ); + io += so; + } + return out; +} + + +// MAIN // + +/** +* Takes elements from an array and assigns the values to elements in a provided output array. +* +* @param {Collection} x - input array. +* @param {IntegerArray} indices - list of indices. +* @param {string} mode - index mode. +* @param {Collection} out - output array. +* @param {integer} stride - output array stride. +* @param {NonNegativeInteger} offset - output array offset. +* @param {Function} clbk - callback function applied to each selected element. +* @returns {Collection} output array. +* +* @example +* var x = [ 1, 2, 3, 4 ]; +* var indices = [ 3, 1, 2, 0 ]; +* +* var out = [ 0, 0, 0, 0 ]; +* +* function clbk( val ){ + return val; +} +* var arr = assignMap( x, indices, 'throw', out, 1, 0, clbk); +* // arr is [ 4, 2, 3, 1 ] +* +* var bool = ( arr === out ); +* // bool is true +*/ +function assignMap( x, indices, mode, out, stride, offset, clbk ) { + var xo; + var io; + var oo; + + xo = arraylike2object( x ); + io = arraylike2object( indices ); + oo = arraylike2object( out ); + if ( + xo.accessorProtocol || + io.accessorProtocol || + oo.accessorProtocol + ) { + // Note: we only explicitly support complex-to-complex, as this function should not be concerned with casting rules, etc. That is left to userland... + if ( + isComplexDataType( xo.dtype ) && + isComplexDataType( oo.dtype ) + ) { + complexMap( reinterpret( x, 0 ), io, mode, reinterpret( out, 0 ), stride, offset, clbk ); // eslint-disable-line max-len + return out; + } + accessorsMap( xo, io, mode, oo, stride, offset, clbk ); + return out; + } + takeMapIndexed( x, indices, mode, out, stride, offset, clbk ); + return out; +} + + +// EXPORTS // + +module.exports = assignMap; diff --git a/lib/node_modules/@stdlib/array/base/take-map/lib/index.js b/lib/node_modules/@stdlib/array/base/take-map/lib/index.js new file mode 100644 index 000000000000..b3918c07e909 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/take-map/lib/index.js @@ -0,0 +1,72 @@ +/** +* @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'; + +/** +* Take elements from an array and apply a provided function to map values. +* +* @module @stdlib/array/base/take-map +* +* @example +* var takeMap = require( '@stdlib/array/base/take-map' ); +* +* var x = [ 1, 2, 3, 4 ]; +* +* function mapFunction( val ) { + return val; +} +* +* var indices = [ 0, 0, 1, 1, 3, 3 ]; +* var y = takeMap( x, indices, 'throw', mapFunction ); +* // returns [ 1, 1, 2, 2, 4, 4 ] +* +* @example +* var takeMap = require( '@stdlib/array/base/take-map' ); +* +* var x = [ 1, 2, 3, 4 ]; +* +* var out = [ 0, 0, 0, 0, 0, 0 ]; +* var indices = [ 0, 0, 1, 1, 3, 3 ]; +* +* function clbk( val ) { + return val; +} +* +* var arr = takeMap.assign( x, indices, 'throw', out, 1, 0, mapFunction ); +* // returns [ 1, 1, 2, 2, 4, 4 ] +* +* var bool = ( arr === out ); +* // returns true +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var main = require( './main.js' ); +var assign = require( './assign.js' ); + + +// MAIN // + +setReadOnly( main, 'assign', assign ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/array/base/take-map/lib/main.js b/lib/node_modules/@stdlib/array/base/take-map/lib/main.js new file mode 100644 index 000000000000..7e9d637e075c --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/take-map/lib/main.js @@ -0,0 +1,82 @@ +/** +* @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 resolveGetter = require( '@stdlib/array/base/resolve-getter' ); +var ind = require( '@stdlib/ndarray/base/ind' ).factory; + + +// MAIN // + +/** +* Takes elements from an array, applies a mapping function using a callback, and returns a new array. +* +* @param {Collection} x - input array +* @param {IntegerArray} indices - list of indices +* @param {string} mode - index mode +* @param {Function} clbk - callback to invoke +* @returns {Array} output array +* +* @example +* var x = [ 1, 2, 3, 4 ]; +* var indices = [ 3, 1, 2, 0 ]; +* +* function clbk(val){ + return 2 * val; +} +* +* var y = takeMap( x, indices, 'throw' ,clbk ); +* // returns [ 8, 4, 6, 2 ] +*/ +function takeMap( x, indices, mode, clbk ) { + var getIndex; + var xget; + var iget; + var out; + var max; + var i; + var j; + + // Resolve an accessor for retrieving array elements: + xget = resolveGetter( x ); + iget = resolveGetter( indices ); + + // Resolve a function for returning an index according to the specified index mode: + getIndex = ind( mode ); + + // Resolve the maximum index: + max = x.length - 1; + + // Extract each desired element from the provided array... + out = []; + for ( i = 0; i < indices.length; i++ ) { + j = getIndex( iget( indices, i ), max ); + + // eslint-disable-next-line no-useless-call + out.push( clbk.call( null, xget( x, j ), j ) ); // use `Array#push` to ensure "fast" elements + } + return out; +} + + +// EXPORTS // + +module.exports = takeMap; diff --git a/lib/node_modules/@stdlib/array/base/take-map/package.json b/lib/node_modules/@stdlib/array/base/take-map/package.json new file mode 100644 index 000000000000..b64bac7386f7 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/take-map/package.json @@ -0,0 +1,62 @@ +{ + "name": "@stdlib/array/base/take-map", + "version": "0.0.0", + "description": "Take elements from an array and return a new array after applying a mapping function.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "utilities", + "utils", + "generic", + "array", + "take", + "extract", + "copy", + "index" + ] +} diff --git a/lib/node_modules/@stdlib/array/base/take-map/test/test.assign.js b/lib/node_modules/@stdlib/array/base/take-map/test/test.assign.js new file mode 100644 index 000000000000..7e8156daef4d --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/take-map/test/test.assign.js @@ -0,0 +1,707 @@ +/** +* @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 Complex128Array = require( '@stdlib/array/complex128' ); +var Complex64Array = require( '@stdlib/array/complex64' ); +var Float64Array = require( '@stdlib/array/float64' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var isSameComplex128Array = require( '@stdlib/assert/is-same-complex128array' ); +var isSameComplex64Array = require( '@stdlib/assert/is-same-complex64array' ); +var zeros = require( '@stdlib/array/zeros' ); +var takeMap = require( './../lib/assign.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof takeMap, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function takes elements from an array (generic)', function test( t ) { + var expected; + var indices; + var actual; + var out; + var x; + + function clbk( val ) { + return val * val; + } + + x = [ 1, 2, 3, 4 ]; + + indices = [ 1, 3 ]; + out = zeros( indices.length, 'generic' ); + actual = takeMap( x, indices, 'throw', out, 1, 0, clbk ); + expected = [ 4, 16 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + indices = [ 1, 1, 3, 3 ]; + out = zeros( indices.length*2, 'generic' ); + actual = takeMap( x, indices, 'throw', out, 2, 0, clbk ); + expected = [ 4, 0, 4, 0, 16, 0, 16, 0 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + indices = [ 3, 2, 1, 0 ]; + out = zeros( indices.length, 'generic' ); + actual = takeMap( x, indices, 'throw', out, -1, out.length-1, clbk ); + expected = [ 1, 4, 9, 16 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + indices = [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ]; + out = zeros( indices.length+1, 'generic' ); + actual = takeMap( x, indices, 'throw', out, 1, 1, clbk ); + expected = [ 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function takes and maps elements from an array (real typed array)', function test( t ) { + var expected; + var indices; + var actual; + var out; + var x; + + function clbk( val ) { + return val * 2; + } + + x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + + indices = [ 1, 3 ]; + out = zeros( indices.length, 'float64' ); + actual = takeMap( x, indices, 'throw', out, 1, 0, clbk ); + expected = new Float64Array( [ 4.0, 8.0 ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + indices = [ 1, 1, 3, 3 ]; + out = zeros( indices.length*2, 'float64' ); + actual = takeMap( x, indices, 'throw', out, 2, 0, clbk ); + expected = new Float64Array( [ 4.0, 0.0, 4.0, 0.0, 8.0, 0.0, 8.0, 0.0 ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + indices = [ 3, 2, 1, 0 ]; + out = zeros( indices.length, 'float64' ); + actual = takeMap( x, indices, 'throw', out, -1, out.length-1, clbk ); + expected = new Float64Array( [ 2.0, 4.0, 6.0, 8.0 ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + indices = [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ]; + out = zeros( indices.length+1, 'float64' ); + actual = takeMap( x, indices, 'throw', out, 1, 1, clbk ); + expected = new Float64Array( [ 0.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0 ] ); // eslint-disable-line max-len + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function takes and maps elements from an array (complex typed array)', function test( t ) { + var expected; + var indices; + var actual; + var out; + var x; + + function clbk( val ) { + return val; + } + + x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + indices = [ 1, 3 ]; + out = zeros( indices.length, 'complex128' ); + actual = takeMap( x, indices, 'throw', out, 1, 0, clbk ); + expected = new Complex128Array( [ 3.0, 4.0, 7.0, 8.0 ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameComplex128Array( actual, expected ), true, 'returns expected value' ); + + indices = [ 1, 1, 3, 3 ]; + out = zeros( indices.length*2, 'complex64' ); + actual = takeMap( x, indices, 'throw', out, 2, 0, clbk ); + expected = new Complex64Array( [ 3.0, 4.0, 0.0, 0.0, 3.0, 4.0, 0.0, 0.0, 7.0, 8.0, 0.0, 0.0, 7.0, 8.0, 0.0, 0.0 ] ); // eslint-disable-line max-len + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameComplex64Array( actual, expected ), true, 'returns expected value' ); + + x = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + indices = [ 3, 2, 1, 0 ]; + out = zeros( indices.length, 'complex128' ); + actual = takeMap( x, indices, 'throw', out, -1, out.length-1, clbk ); + expected = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); // eslint-disable-line max-len + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameComplex128Array( actual, expected ), true, 'returns expected value' ); + + indices = [ 1, 1, 1, 1 ]; + out = zeros( indices.length+1, 'complex64' ); + actual = takeMap( x, indices, 'throw', out, 1, 1, clbk ); + expected = new Complex64Array( [ 0.0, 0.0, 3.0, 4.0, 3.0, 4.0, 3.0, 4.0, 3.0, 4.0 ] ); // eslint-disable-line max-len + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameComplex64Array( actual, expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function takes and maps elements from an array (accessors)', function test( t ) { + var expected; + var indices; + var actual; + var out; + var x; + + function clbk( val ) { + return val + 1; + } + + x = toAccessorArray( [ 1, 2, 3, 4 ] ); + + indices = toAccessorArray( [ 1, 3 ] ); + out = toAccessorArray( zeros( indices.length, 'generic' ) ); + actual = takeMap( x, indices, 'throw', out, 1, 0, clbk ); + expected = [ 3, 5 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + isEqual( actual, expected ); + + indices = toAccessorArray( [ 1, 1, 3, 3 ] ); + out = toAccessorArray( zeros( indices.length*2, 'generic' ) ); + actual = takeMap( x, indices, 'throw', out, 2, 0, clbk ); + expected = [ 3, 0, 3, 0, 5, 0, 5, 0 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + isEqual( actual, expected ); + + indices = toAccessorArray( [ 3, 2, 1, 0 ] ); + out = toAccessorArray( zeros( indices.length, 'generic' ) ); + actual = takeMap( x, indices, 'throw', out, -1, out.length-1, clbk ); + expected = [ 2, 3, 4, 5 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + isEqual( actual, expected ); + + indices = toAccessorArray( [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ] ); + out = toAccessorArray( zeros( indices.length+1, 'generic' ) ); + actual = takeMap( x, indices, 'throw', out, 1, 1, clbk ); + expected = [ 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + isEqual( actual, expected ); + + t.end(); + + function isEqual( actual, expected ) { + var i; + for ( i = 0; i < expected.length; i++ ) { + t.strictEqual( actual.get( i ), expected[ i ], 'returns expected value' ); + } + } +}); + +tape( 'the function returns an output array unchanged if provided a second argument which is empty', function test( t ) { + var expected; + var actual; + var out; + var x; + + function clbk( val ) { + return val; + } + + x = [ 1, 2, 3, 4 ]; + out = [ 0, 0, 0, 0 ]; + expected = [ 0, 0, 0, 0 ]; + actual = takeMap( x, [], 'throw', out, 1, 0, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + x = toAccessorArray( [ 1, 2, 3, 4 ] ); + out = [ 0, 0, 0, 0 ]; + expected = [ 0, 0, 0, 0 ]; + actual = takeMap( x, [], 'throw', out, 1, 0, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + out = [ 0, 0, 0, 0 ]; + expected = [ 0, 0, 0, 0 ]; + actual = takeMap( x, [], 'throw', out, 1, 0, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + out = new Complex128Array( [ 0.0, 0.0, 0.0, 0.0 ] ); + expected = new Complex128Array( [ 0.0, 0.0, 0.0, 0.0 ] ); + actual = takeMap( x, [], 'throw', out, 1, 0, clbk ); + t.strictEqual( isSameComplex128Array( actual, expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'when the "mode" is "throw", the function throws an error if provided an out-of-bounds index (generic)', function test( t ) { + var indices; + var out; + var x; + + function clbk( val ) { + return val; + } + + x = [ 1, 2, 3, 4 ]; + indices = [ 4, 5, 1, 2 ]; + out = zeros( x.length, 'generic' ); + + t.throws( badValue, RangeError, 'throws an error' ); + t.end(); + + function badValue() { + takeMap( x, indices, 'throw', out, 1, 0, clbk ); + } +}); + +tape( 'when the "mode" is "throw", the function throws an error if provided an out-of-bounds index (accessors)', function test( t ) { + var indices; + var out; + var x; + + function clbk( val ) { + return val; + } + + x = toAccessorArray( [ 1, 2, 3, 4 ] ); + indices = toAccessorArray( [ 4, 5, 1, 2 ] ); + out = toAccessorArray( zeros( x.length, 'generic' ) ); + + t.throws( badValue, RangeError, 'throws an error' ); + t.end(); + + function badValue() { + takeMap( x, indices, 'throw', out, 1, 0, clbk ); + } +}); + +tape( 'when the "mode" is "throw", the function throws an error if provided an out-of-bounds index (real typed)', function test( t ) { + var indices; + var out; + var x; + + function clbk( val ) { + return val; + } + + x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + indices = [ 4, 5, 1, 2 ]; + out = zeros( x.length, 'float64' ); + + t.throws( badValue, RangeError, 'throws an error' ); + t.end(); + + function badValue() { + takeMap( x, indices, 'throw', out, 1, 0, clbk ); + } +}); + +tape( 'when the "mode" is "throw", the function throws an error if provided an out-of-bounds index (complex typed)', function test( t ) { + var indices; + var out; + var x; + + function clbk( val ) { + return val; + } + + x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + indices = [ 4, 5, 1, 2 ]; + out = zeros( x.length, 'complex128' ); + + t.throws( badValue, RangeError, 'throws an error' ); + t.end(); + + function badValue() { + takeMap( x, indices, 'throw', out, 1, 0, clbk ); + } +}); + +tape( 'when the "mode" is "normalize", the function normalizes negative indices (generic)', function test( t ) { + var expected; + var indices; + var actual; + var out; + var x; + + function clbk( val ) { + return val * 2; + } + + x = [ 1, 2, 3, 4 ]; + indices = [ -1, -2, -3, -4 ]; + out = zeros( x.length, 'generic' ); + + actual = takeMap( x, indices, 'normalize', out, 1, 0, clbk ); + expected = [ 8, 6, 4, 2 ]; + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'when the "mode" is "normalize", the function normalizes negative indices (accessors)', function test( t ) { + var expected; + var indices; + var out; + var x; + + function clbk( val ) { + return val; + } + + x = toAccessorArray( [ 1, 2, 3, 4 ] ); + indices = toAccessorArray( [ -1, -2, -3, -4 ] ); + out = zeros( x.length, 'generic' ); + + takeMap( x, indices, 'normalize', toAccessorArray( out ), 1, 0, clbk ); + expected = [ 4, 3, 2, 1 ]; + t.deepEqual( out, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'when the "mode" is "normalize", the function normalizes negative indices (real typed)', function test( t ) { + var expected; + var indices; + var actual; + var out; + var x; + + function clbk( val ) { + return val; + } + + x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + indices = [ -1, -2, -3, -4 ]; + out = zeros( x.length, 'float64' ); + + actual = takeMap( x, indices, 'normalize', out, 1, 0, clbk ); + expected = new Float64Array( [ 4.0, 3.0, 2.0, 1.0 ] ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'when the "mode" is "normalize", the function normalizes negative indices (complex typed)', function test( t ) { + var expected; + var indices; + var actual; + var out; + var x; + + function clbk( val ) { + return val; + } + + x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + indices = [ -1, -2 ]; + out = zeros( x.length, 'complex128' ); + + actual = takeMap( x, indices, 'normalize', out, 1, 0, clbk ); + expected = new Complex128Array( [ 3.0, 4.0, 1.0, 2.0 ] ); + t.strictEqual( isSameComplex128Array( actual, expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'when the "mode" is "normalize", the function throws an error if provided an out-of-bounds index (generic)', function test( t ) { + var indices; + var out; + var x; + + function clbk( val ) { + return val; + } + + x = [ 1, 2, 3, 4 ]; + indices = [ 4, 5, 1, 2 ]; + out = zeros( x.length, 'generic' ); + + t.throws( badValue, RangeError, 'throws an error' ); + t.end(); + + function badValue() { + takeMap( x, indices, 'normalize', out, 1, 0, clbk ); + } +}); + +tape( 'when the "mode" is "normalize", the function throws an error if provided an out-of-bounds index (accessors)', function test( t ) { + var indices; + var out; + var x; + + function clbk( val ) { + return val; + } + + x = toAccessorArray( [ 1, 2, 3, 4 ] ); + indices = toAccessorArray( [ 4, 5, 1, 2 ] ); + out = toAccessorArray( zeros( x.length, 'generic' ) ); + + t.throws( badValue, RangeError, 'throws an error' ); + t.end(); + + function badValue() { + takeMap( x, indices, 'normalize', out, 1, 0, clbk ); + } +}); + +tape( 'when the "mode" is "normalize", the function throws an error if provided an out-of-bounds index (real typed)', function test( t ) { + var indices; + var out; + var x; + + function clbk( val ) { + return val; + } + + x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + indices = [ 4, 5, 1, 2 ]; + out = zeros( x.length, 'float64' ); + + t.throws( badValue, RangeError, 'throws an error' ); + t.end(); + + function badValue() { + takeMap( x, indices, 'normalize', out, 1, 0, clbk ); + } +}); + +tape( 'when the "mode" is "normalize", the function throws an error if provided an out-of-bounds index (complex typed)', function test( t ) { + var indices; + var out; + var x; + + function clbk( val ) { + return val; + } + + x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + indices = [ 4, 5, 1, 2 ]; + out = zeros( x.length, 'complex128' ); + + t.throws( badValue, RangeError, 'throws an error' ); + t.end(); + + function badValue() { + takeMap( x, indices, 'normalize', out, 1, 0, clbk ); + } +}); + +tape( 'when the "mode" is "clamp", the function clamps out-of-bounds indices (generic)', function test( t ) { + var expected; + var indices; + var actual; + var out; + var x; + + function clbk( val ) { + return val; + } + + x = [ 1, 2, 3, 4 ]; + indices = [ -10, 10, -5, 5 ]; + out = zeros( x.length, 'generic' ); + + actual = takeMap( x, indices, 'clamp', out, 1, 0, clbk ); + expected = [ 1, 4, 1, 4 ]; + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'when the "mode" is "clamp", the function clamps out-of-bounds indices (accessors)', function test( t ) { + var expected; + var indices; + var out; + var x; + + function clbk( val ) { + return val; + } + + x = toAccessorArray( [ 1, 2, 3, 4 ] ); + indices = toAccessorArray( [ -10, 10, -5, 5 ] ); + out = zeros( x.length, 'generic' ); + + takeMap( x, indices, 'clamp', toAccessorArray( out ), 1, 0, clbk ); + expected = [ 1, 4, 1, 4 ]; + t.deepEqual( out, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'when the "mode" is "clamp", the function clamps out-of-bounds indices (real typed)', function test( t ) { + var expected; + var indices; + var actual; + var out; + var x; + + function clbk( val ) { + return val; + } + + x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + indices = [ -10, 10, -5, 5 ]; + out = zeros( x.length, 'float64' ); + + actual = takeMap( x, indices, 'clamp', out, 1, 0, clbk ); + expected = new Float64Array( [ 1.0, 4.0, 1.0, 4.0 ] ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'when the "mode" is "clamp", the function clamps out-of-bounds indices (complex typed)', function test( t ) { + var expected; + var indices; + var actual; + var out; + var x; + + function clbk( val ) { + return val; + } + + x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + indices = [ -10, 10, -5, 5 ]; + out = zeros( x.length, 'complex128' ); + + actual = takeMap( x, indices, 'clamp', out, 1, 0, clbk ); + expected = new Complex128Array( [ 1.0, 2.0, 7.0, 8.0, 1.0, 2.0, 7.0, 8.0 ] ); // eslint-disable-line max-len + t.strictEqual( isSameComplex128Array( actual, expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'when the "mode" is "wrap", the function wraps out-of-bounds indices (generic)', function test( t ) { + var expected; + var indices; + var actual; + var out; + var x; + + function clbk( val ) { + return val; + } + + x = [ 1, 2, 3, 4 ]; + indices = [ -10, 10, -5, 5 ]; + out = zeros( x.length, 'generic' ); + + actual = takeMap( x, indices, 'wrap', out, 1, 0, clbk ); + expected = [ 3, 3, 4, 2 ]; + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'when the "mode" is "wrap", the function wraps out-of-bounds indices (accessors)', function test( t ) { + var expected; + var indices; + var out; + var x; + + function clbk( val ) { + return val; + } + + x = toAccessorArray( [ 1, 2, 3, 4 ] ); + indices = toAccessorArray( [ -10, 10, -5, 5 ] ); + out = zeros( x.length, 'generic' ); + + takeMap( x, indices, 'wrap', toAccessorArray( out ), 1, 0, clbk ); + expected = [ 3, 3, 4, 2 ]; + t.deepEqual( out, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'when the "mode" is "wrap", the function wraps out-of-bounds indices (real typed)', function test( t ) { + var expected; + var indices; + var actual; + var out; + var x; + + function clbk( val ) { + return val; + } + + x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + indices = [ -10, 10, -5, 5 ]; + out = zeros( x.length, 'float64' ); + + actual = takeMap( x, indices, 'wrap', out, 1, 0, clbk ); + expected = new Float64Array( [ 3.0, 3.0, 4.0, 2.0 ] ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'when the "mode" is "wrap", the function wraps out-of-bounds indices (complex typed)', function test( t ) { + var expected; + var indices; + var actual; + var out; + var x; + + function clbk( val ) { + return val; + } + + x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + indices = [ -10, 10, -5, 5 ]; + out = zeros( x.length, 'complex128' ); + + actual = takeMap( x, indices, 'wrap', out, 1, 0, clbk ); + expected = new Complex128Array( [ 5.0, 6.0, 5.0, 6.0, 7.0, 8.0, 3.0, 4.0 ] ); // eslint-disable-line max-len + t.strictEqual( isSameComplex128Array( actual, expected ), true, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/array/base/take-map/test/test.js b/lib/node_modules/@stdlib/array/base/take-map/test/test.js new file mode 100644 index 000000000000..a03afc0f668d --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/take-map/test/test.js @@ -0,0 +1,41 @@ +/** +* @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 hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var hasMethod = require( '@stdlib/assert/is-method' ); +var takeMap = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof takeMap, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is an `assign` method', function test( t ) { + t.strictEqual( hasOwnProp( takeMap, 'assign' ), true, 'returns expected value' ); + t.strictEqual( hasMethod( takeMap, 'assign' ), true, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/array/base/take-map/test/test.main.js b/lib/node_modules/@stdlib/array/base/take-map/test/test.main.js new file mode 100644 index 000000000000..e783cef71464 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/take-map/test/test.main.js @@ -0,0 +1,212 @@ +/** +* @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 Complex64Array = require( '@stdlib/array/complex64' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var realf = require( '@stdlib/complex/realf' ); +var imagf = require( '@stdlib/complex/imagf' ); +var isComplex64 = require( '@stdlib/assert/is-complex64' ); +var takeMap = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof takeMap, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is an `assign` method', function test( t ) { + t.strictEqual( typeof takeMap.assign, 'function', 'assign method is a function' ); + t.end(); +}); + +tape( 'the function takes elements from an array', function test( t ) { + var expected; + var indices; + var actual; + var x; + + function clbk( v ) { + return v*v; + } + + x = [ 1, 2, 3, 4 ]; + + indices = [ 1, 3 ]; + actual = takeMap( x, indices, 'throw', clbk ); + expected = [ 4, 16 ]; + t.deepEqual( actual, expected, 'returns expected value' ); + + indices = [ 1, 1, 3, 3 ]; + actual = takeMap( x, indices, 'throw', clbk ); + expected = [ 4, 4, 16, 16 ]; + t.deepEqual( actual, expected, 'returns expected value' ); + + indices = [ 3, 2, 1, 0 ]; + actual = takeMap( x, indices, 'throw', clbk ); + expected = [ 16, 9, 4, 1 ]; + t.deepEqual( actual, expected, 'returns expected value' ); + + indices = [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ]; + actual = takeMap( x, indices, 'throw', clbk ); + expected = [ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 ]; + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function takes elements from an array (accessors)', function test( t ) { + var expected; + var indices; + var actual; + var x; + var v; + var i; + + function clbk( v ) { + return v; + } + + x = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + indices = toAccessorArray( [ 1, 1, 3, 3 ] ); + actual = takeMap( x, indices, 'throw', clbk ); + + t.notEqual( actual, x, 'returns different reference' ); + for ( i = 0; i < indices.length; i++ ) { + v = actual[ i ]; + expected = x.get( indices.get( i ) ); + t.strictEqual( isComplex64( v ), true, 'returns expected value' ); + t.strictEqual( realf( v ), realf( expected ), 'returns expected value' ); + t.strictEqual( imagf( v ), imagf( expected ), 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function returns an empty array if provided a second argument which is empty', function test( t ) { + var x = [ 1, 2, 3, 4 ]; + function clbk( v ) { + return v; + } + t.deepEqual( takeMap( x, [], 'throw' ), [], 'returns expected value', clbk ); + t.end(); +}); + +tape( 'when the "mode" is "throw", the function throws an error if provided an out-of-bounds index', function test( t ) { + var indices; + var x; + + function clbk( v ) { + return v; + } + + x = [ 1, 2, 3, 4 ]; + indices = [ 4, 5, 1, 2 ]; + + t.throws( badValue, RangeError, 'throws an error' ); + t.end(); + + function badValue() { + takeMap( x, indices, 'throw', clbk ); + } +}); + +tape( 'when the "mode" is "normalize", the function normalizes negative indices', function test( t ) { + var expected; + var indices; + var actual; + var x; + + function clbk( v ) { + return v*2; + } + + x = [ 1, 2, 3, 4 ]; + + indices = [ -1, -2, -3, -4 ]; + actual = takeMap( x, indices, 'normalize', clbk ); + expected = [ 8, 6, 4, 2 ]; + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'when the "mode" is "normalize", the function throws an error if provided an out-of-bounds index', function test( t ) { + var indices; + var x; + + function clbk( v ) { + return v; + } + + x = [ 1, 2, 3, 4 ]; + indices = [ 2, 50, 1, 2 ]; + + t.throws( badValue, RangeError, 'throws an error' ); + t.end(); + + function badValue() { + takeMap( x, indices, 'normalize', clbk ); + } +}); + +tape( 'when the "mode" is "clamp", the function clamps out-of-bounds indices', function test( t ) { + var expected; + var indices; + var actual; + var x; + + function clbk( v ) { + return v; + } + + x = [ 1, 2, 3, 4 ]; + + indices = [ -10, 10, -5, 5 ]; + actual = takeMap( x, indices, 'clamp', clbk ); + expected = [ 1, 4, 1, 4 ]; + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'when the "mode" is "wrap", the function wraps out-of-bounds indices', function test( t ) { + var expected; + var indices; + var actual; + var x; + + function clbk( v ) { + return v; + } + + x = [ 1, 2, 3, 4 ]; + + indices = [ -10, 10, -5, 5 ]; + actual = takeMap( x, indices, 'wrap', clbk ); + expected = [ 3, 3, 4, 2 ]; + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +});