@stdlib/utils-map 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +481 -0
- package/NOTICE +1 -0
- package/README.md +386 -0
- package/docs/repl.txt +106 -0
- package/docs/types/index.d.ts +300 -0
- package/docs/types/test.ts +270 -0
- package/lib/array.js +89 -0
- package/lib/assign.js +128 -0
- package/lib/index.js +99 -0
- package/lib/main.js +104 -0
- package/lib/ndarray.js +197 -0
- package/package.json +118 -0
package/lib/assign.js
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2021 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
'use strict';
|
|
20
|
+
|
|
21
|
+
// MODULES //
|
|
22
|
+
|
|
23
|
+
var isArrayLikeObject = require( '@stdlib/assert-is-array-like-object' );
|
|
24
|
+
var isndarrayLike = require( '@stdlib/assert-is-ndarray-like' );
|
|
25
|
+
var isFunction = require( '@stdlib/assert-is-function' );
|
|
26
|
+
var ndarraylike2object = require( '@stdlib/ndarray-base-ndarraylike2object' );
|
|
27
|
+
var arraylike2object = require( '@stdlib/array-base-arraylike2object' );
|
|
28
|
+
var broadcast = require( '@stdlib/ndarray-base-maybe-broadcast-array' );
|
|
29
|
+
var isReadOnly = require( '@stdlib/ndarray-base-assert-is-read-only' );
|
|
30
|
+
var ndarrayFcn = require( './ndarray.js' );
|
|
31
|
+
var arrayFcn = require( './array.js' );
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
// MAIN //
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Applies a function to each element in an array and assigns the result to an element in an output array.
|
|
38
|
+
*
|
|
39
|
+
* ## Notes
|
|
40
|
+
*
|
|
41
|
+
* - The applied function is provided the following arguments:
|
|
42
|
+
*
|
|
43
|
+
* - **value**: array element.
|
|
44
|
+
* - **index**: element index.
|
|
45
|
+
* - **arr**: input array.
|
|
46
|
+
*
|
|
47
|
+
* @param {(ArrayLikeObject|ndarray)} arr - input array
|
|
48
|
+
* @param {(ArrayLikeObject|ndarray)} out - output array
|
|
49
|
+
* @param {Function} fcn - function to apply
|
|
50
|
+
* @param {*} [thisArg] - function execution context
|
|
51
|
+
* @throws {TypeError} first argument must be an array-like object or an ndarray
|
|
52
|
+
* @throws {TypeError} second argument must be an array-like object or an ndarray
|
|
53
|
+
* @throws {TypeError} third argument must be a function
|
|
54
|
+
* @throws {TypeError} input and output arrays must be either both array-like objects or both ndarrays
|
|
55
|
+
* @throws {RangeError} input and output arrays must have the same length
|
|
56
|
+
* @throws {Error} input and output ndarrays must be broadcast compatible
|
|
57
|
+
* @throws {Error} cannot write to a read-only ndarray
|
|
58
|
+
* @returns {(ArrayLikeObject|ndarray)} output array
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* var naryFunction = require( '@stdlib/utils-nary-function' );
|
|
62
|
+
* var abs = require( '@stdlib/math-base-special-abs' );
|
|
63
|
+
*
|
|
64
|
+
* var arr = [ -1, -2, -3, -4, -5, -6 ];
|
|
65
|
+
* var out = [ 0, 0, 0, 0, 0, 0 ];
|
|
66
|
+
*
|
|
67
|
+
* map( arr, out, naryFunction( abs, 1 ) );
|
|
68
|
+
*
|
|
69
|
+
* console.log( out );
|
|
70
|
+
* // => [ 1, 2, 3, 4, 5, 6 ]
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* var naryFunction = require( '@stdlib/utils-nary-function' );
|
|
74
|
+
* var abs = require( '@stdlib/math-base-special-abs' );
|
|
75
|
+
* var array = require( '@stdlib/ndarray-array' );
|
|
76
|
+
*
|
|
77
|
+
* var opts = {
|
|
78
|
+
* 'dtype': 'generic',
|
|
79
|
+
* 'shape': [ 2, 3 ]
|
|
80
|
+
* };
|
|
81
|
+
* var arr = array( [ [ -1, -2, -3 ], [ -4, -5, -6 ] ], opts );
|
|
82
|
+
* var out = array( opts );
|
|
83
|
+
*
|
|
84
|
+
* map( arr, out, naryFunction( abs, 1 ) );
|
|
85
|
+
*
|
|
86
|
+
* var data = out.data;
|
|
87
|
+
* // returns [ 1, 2, 3, 4, 5, 6 ]
|
|
88
|
+
*/
|
|
89
|
+
function map( arr, out, fcn, thisArg ) {
|
|
90
|
+
var tmp;
|
|
91
|
+
var sh;
|
|
92
|
+
if ( !isFunction( fcn ) ) {
|
|
93
|
+
throw new TypeError( 'invalid argument. Third argument must be a function. Value: `' + fcn + '`.' );
|
|
94
|
+
}
|
|
95
|
+
if ( isndarrayLike( arr ) ) { // note: assertion order matters here, as an ndarray-like object is also array-like
|
|
96
|
+
if ( !isndarrayLike( out ) ) {
|
|
97
|
+
throw new TypeError( 'invalid argument. If the input array is an ndarray, the output array must also be an ndarray. Value: `' + out + '`.' );
|
|
98
|
+
}
|
|
99
|
+
if ( isReadOnly( out ) ) {
|
|
100
|
+
throw new Error( 'invalid argument. The output ndarray must be writable. Cannot write to a read-only ndarray.' );
|
|
101
|
+
}
|
|
102
|
+
out = ndarraylike2object( out );
|
|
103
|
+
sh = out.shape;
|
|
104
|
+
|
|
105
|
+
tmp = ndarraylike2object( broadcast( arr, sh ) );
|
|
106
|
+
tmp.ref = arr;
|
|
107
|
+
arr = tmp;
|
|
108
|
+
|
|
109
|
+
ndarrayFcn( arr, out, fcn, thisArg );
|
|
110
|
+
return out.ref;
|
|
111
|
+
}
|
|
112
|
+
if ( isArrayLikeObject( arr ) ) {
|
|
113
|
+
if ( !isArrayLikeObject( out ) || isndarrayLike( out ) ) {
|
|
114
|
+
throw new TypeError( 'invalid argument. If the input array is an array-like object, the output array must also be an array-like object. Value: `' + out + '`.' );
|
|
115
|
+
}
|
|
116
|
+
if ( arr.length !== out.length ) {
|
|
117
|
+
throw new RangeError( 'invalid arguments. Input and output arrays must have the same length.' );
|
|
118
|
+
}
|
|
119
|
+
arrayFcn( arraylike2object( arr ), arraylike2object( out ), fcn, thisArg ); // eslint-disable-line max-len
|
|
120
|
+
return out;
|
|
121
|
+
}
|
|
122
|
+
throw new TypeError( 'invalid argument. First argument must be an array-like object or an ndarray. Value: `' + arr + '`.' );
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
// EXPORTS //
|
|
127
|
+
|
|
128
|
+
module.exports = map;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2021 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
'use strict';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Apply a function to each element in an array and assign the result to an element in an output array.
|
|
23
|
+
*
|
|
24
|
+
* @module @stdlib/utils-map
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* var naryFunction = require( '@stdlib/utils-nary-function' );
|
|
28
|
+
* var abs = require( '@stdlib/math-base-special-abs' );
|
|
29
|
+
* var map = require( '@stdlib/utils-map' );
|
|
30
|
+
*
|
|
31
|
+
* var arr = [ -1, -2, -3, -4, -5, -6 ];
|
|
32
|
+
*
|
|
33
|
+
* var out = map( arr, naryFunction( abs, 1 ) );
|
|
34
|
+
* // returns [ 1, 2, 3, 4, 5, 6 ]
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* var naryFunction = require( '@stdlib/utils-nary-function' );
|
|
38
|
+
* var abs = require( '@stdlib/math-base-special-abs' );
|
|
39
|
+
* var array = require( '@stdlib/ndarray-array' );
|
|
40
|
+
* var map = require( '@stdlib/utils-map' );
|
|
41
|
+
*
|
|
42
|
+
* var opts = {
|
|
43
|
+
* 'dtype': 'generic'
|
|
44
|
+
* };
|
|
45
|
+
* var arr = array( [ [ -1, -2, -3 ], [ -4, -5, -6 ] ], opts );
|
|
46
|
+
*
|
|
47
|
+
* var out = map( arr, naryFunction( abs, 1 ) );
|
|
48
|
+
* // returns <ndarray>
|
|
49
|
+
*
|
|
50
|
+
* var data = out.data;
|
|
51
|
+
* // returns [ 1, 2, 3, 4, 5, 6 ]
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* var naryFunction = require( '@stdlib/utils-nary-function' );
|
|
55
|
+
* var abs = require( '@stdlib/math-base-special-abs' );
|
|
56
|
+
* var map = require( '@stdlib/utils-map' );
|
|
57
|
+
*
|
|
58
|
+
* var arr = [ -1, -2, -3, -4, -5, -6 ];
|
|
59
|
+
* var out = [ 0, 0, 0, 0, 0, 0 ];
|
|
60
|
+
*
|
|
61
|
+
* map.assign( arr, out, naryFunction( abs, 1 ) );
|
|
62
|
+
*
|
|
63
|
+
* console.log( out );
|
|
64
|
+
* // => [ 1, 2, 3, 4, 5, 6 ]
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* var naryFunction = require( '@stdlib/utils-nary-function' );
|
|
68
|
+
* var abs = require( '@stdlib/math-base-special-abs' );
|
|
69
|
+
* var array = require( '@stdlib/ndarray-array' );
|
|
70
|
+
* var map = require( '@stdlib/utils-map' );
|
|
71
|
+
*
|
|
72
|
+
* var opts = {
|
|
73
|
+
* 'dtype': 'generic',
|
|
74
|
+
* 'shape': [ 2, 3 ]
|
|
75
|
+
* };
|
|
76
|
+
* var arr = array( [ [ -1, -2, -3 ], [ -4, -5, -6 ] ], opts );
|
|
77
|
+
* var out = array( opts );
|
|
78
|
+
*
|
|
79
|
+
* map.assign( arr, out, naryFunction( abs, 1 ) );
|
|
80
|
+
*
|
|
81
|
+
* var data = out.data;
|
|
82
|
+
* // returns [ 1, 2, 3, 4, 5, 6 ]
|
|
83
|
+
*/
|
|
84
|
+
|
|
85
|
+
// MODULES //
|
|
86
|
+
|
|
87
|
+
var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );
|
|
88
|
+
var main = require( './main.js' );
|
|
89
|
+
var assign = require( './assign.js' );
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
// MAIN //
|
|
93
|
+
|
|
94
|
+
setReadOnly( main, 'assign', assign );
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
// EXPORTS //
|
|
98
|
+
|
|
99
|
+
module.exports = main;
|
package/lib/main.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2021 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
'use strict';
|
|
20
|
+
|
|
21
|
+
// MODULES //
|
|
22
|
+
|
|
23
|
+
var isArrayLikeObject = require( '@stdlib/assert-is-array-like-object' );
|
|
24
|
+
var isndarrayLike = require( '@stdlib/assert-is-ndarray-like' );
|
|
25
|
+
var isFunction = require( '@stdlib/assert-is-function' );
|
|
26
|
+
var zeros = require( '@stdlib/array-base-zeros' );
|
|
27
|
+
var ndarraylike2object = require( '@stdlib/ndarray-base-ndarraylike2object' );
|
|
28
|
+
var arraylike2object = require( '@stdlib/array-base-arraylike2object' );
|
|
29
|
+
var ndzeros = require( '@stdlib/ndarray-zeros' );
|
|
30
|
+
var ndarrayFcn = require( './ndarray.js' );
|
|
31
|
+
var arrayFcn = require( './array.js' );
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
// MAIN //
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Applies a function to each element in an array and assigns the result to an element in a new array.
|
|
38
|
+
*
|
|
39
|
+
* ## Notes
|
|
40
|
+
*
|
|
41
|
+
* - The applied function is provided the following arguments:
|
|
42
|
+
*
|
|
43
|
+
* - **value**: array element.
|
|
44
|
+
* - **index**: element index.
|
|
45
|
+
* - **arr**: input array.
|
|
46
|
+
*
|
|
47
|
+
* @param {(ArrayLikeObject|ndarray)} arr - input array
|
|
48
|
+
* @param {Function} fcn - function to apply
|
|
49
|
+
* @param {*} [thisArg] - function execution context
|
|
50
|
+
* @throws {TypeError} first argument must be an array-like object or an ndarray
|
|
51
|
+
* @throws {TypeError} second argument must be a function
|
|
52
|
+
* @returns {(Array|ndarray)} output array
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* var naryFunction = require( '@stdlib/utils-nary-function' );
|
|
56
|
+
* var abs = require( '@stdlib/math-base-special-abs' );
|
|
57
|
+
*
|
|
58
|
+
* var arr = [ -1, -2, -3, -4, -5, -6 ];
|
|
59
|
+
*
|
|
60
|
+
* var out = map( arr, naryFunction( abs, 1 ) );
|
|
61
|
+
* // returns [ 1, 2, 3, 4, 5, 6 ]
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* var naryFunction = require( '@stdlib/utils-nary-function' );
|
|
65
|
+
* var abs = require( '@stdlib/math-base-special-abs' );
|
|
66
|
+
* var array = require( '@stdlib/ndarray-array' );
|
|
67
|
+
*
|
|
68
|
+
* var opts = {
|
|
69
|
+
* 'dtype': 'generic'
|
|
70
|
+
* };
|
|
71
|
+
* var arr = array( [ [ -1, -2, -3 ], [ -4, -5, -6 ] ], opts );
|
|
72
|
+
*
|
|
73
|
+
* var out = map( arr, naryFunction( abs, 1 ) );
|
|
74
|
+
* // returns <ndarray>
|
|
75
|
+
*
|
|
76
|
+
* var data = out.data;
|
|
77
|
+
* // returns [ 1, 2, 3, 4, 5, 6 ]
|
|
78
|
+
*/
|
|
79
|
+
function map( arr, fcn, thisArg ) {
|
|
80
|
+
var out;
|
|
81
|
+
if ( !isFunction( fcn ) ) {
|
|
82
|
+
throw new TypeError( 'invalid argument. Second argument must be a function. Value: `' + fcn + '`.' );
|
|
83
|
+
}
|
|
84
|
+
if ( isndarrayLike( arr ) ) { // note: assertion order matters here, as an ndarray-like object is also array-like
|
|
85
|
+
arr = ndarraylike2object( arr );
|
|
86
|
+
out = ndzeros( arr.shape, {
|
|
87
|
+
'dtype': 'generic',
|
|
88
|
+
'order': arr.order
|
|
89
|
+
});
|
|
90
|
+
ndarrayFcn( arr, ndarraylike2object( out ), fcn, thisArg );
|
|
91
|
+
return out;
|
|
92
|
+
}
|
|
93
|
+
if ( isArrayLikeObject( arr ) ) {
|
|
94
|
+
out = zeros( arr.length );
|
|
95
|
+
arrayFcn( arraylike2object( arr ), arraylike2object( out ), fcn, thisArg ); // eslint-disable-line max-len
|
|
96
|
+
return out;
|
|
97
|
+
}
|
|
98
|
+
throw new TypeError( 'invalid argument. First argument must be an array-like object or an ndarray. Value: `' + arr + '`.' );
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
// EXPORTS //
|
|
103
|
+
|
|
104
|
+
module.exports = map;
|
package/lib/ndarray.js
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2021 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
'use strict';
|
|
20
|
+
|
|
21
|
+
// MODULES //
|
|
22
|
+
|
|
23
|
+
var vind2bind = require( '@stdlib/ndarray-base-vind2bind' );
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
// VARIABLES //
|
|
27
|
+
|
|
28
|
+
var MODE = 'throw';
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
// MAIN //
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Applies a function to each element in an ndarray and assigns the result to an element in an output ndarray.
|
|
35
|
+
*
|
|
36
|
+
* @private
|
|
37
|
+
* @param {Object} x - object containing input ndarray meta data
|
|
38
|
+
* @param {string} x.ref - reference to original input ndarray-like object
|
|
39
|
+
* @param {string} x.dtype - data type
|
|
40
|
+
* @param {Collection} x.data - data buffer
|
|
41
|
+
* @param {NonNegativeInteger} x.length - number of elements
|
|
42
|
+
* @param {NonNegativeIntegerArray} x.shape - dimensions
|
|
43
|
+
* @param {IntegerArray} x.strides - stride lengths
|
|
44
|
+
* @param {NonNegativeInteger} x.offset - index offset
|
|
45
|
+
* @param {string} x.order - specifies whether `x` is row-major (C-style) or column-major (Fortran-style)
|
|
46
|
+
* @param {Function} x.getter - callback for accessing `x` data buffer elements
|
|
47
|
+
* @param {Object} y - object containing output ndarray meta data
|
|
48
|
+
* @param {string} y.dtype - data type
|
|
49
|
+
* @param {Collection} y.data - data buffer
|
|
50
|
+
* @param {NonNegativeInteger} y.length - number of elements
|
|
51
|
+
* @param {NonNegativeIntegerArray} y.shape - dimensions
|
|
52
|
+
* @param {IntegerArray} y.strides - stride lengths
|
|
53
|
+
* @param {NonNegativeInteger} y.offset - index offset
|
|
54
|
+
* @param {string} y.order - specifies whether `y` is row-major (C-style) or column-major (Fortran-style)
|
|
55
|
+
* @param {Function} y.setter - callback for setting `y` data buffer elements
|
|
56
|
+
* @param {Function} fcn - function to apply
|
|
57
|
+
* @param {*} thisArg - function execution context
|
|
58
|
+
* @returns {void}
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* var Complex64Array = require( '@stdlib/array-complex64' );
|
|
62
|
+
* var Complex64 = require( '@stdlib/complex-float32' );
|
|
63
|
+
* var realf = require( '@stdlib/complex-realf' );
|
|
64
|
+
* var imagf = require( '@stdlib/complex-imagf' );
|
|
65
|
+
* var naryFunction = require( '@stdlib/utils-nary-function' );
|
|
66
|
+
*
|
|
67
|
+
* function scale( z ) {
|
|
68
|
+
* return new Complex64( realf(z)*10.0, imagf(z)*10.0 );
|
|
69
|
+
* }
|
|
70
|
+
*
|
|
71
|
+
* // Create data buffers:
|
|
72
|
+
* var xbuf = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
|
|
73
|
+
* var ybuf = new Complex64Array( 4 );
|
|
74
|
+
*
|
|
75
|
+
* // Define the shape of the input and output arrays:
|
|
76
|
+
* var shape = [ 2, 2 ];
|
|
77
|
+
*
|
|
78
|
+
* // Define the array strides:
|
|
79
|
+
* var sx = [ 2, 1 ];
|
|
80
|
+
* var sy = [ 2, 1 ];
|
|
81
|
+
*
|
|
82
|
+
* // Define the index offsets:
|
|
83
|
+
* var ox = 0;
|
|
84
|
+
* var oy = 0;
|
|
85
|
+
*
|
|
86
|
+
* // Define getters and setters:
|
|
87
|
+
* function getter( buf, idx ) {
|
|
88
|
+
* return buf.get( idx );
|
|
89
|
+
* }
|
|
90
|
+
*
|
|
91
|
+
* function setter( buf, idx, value ) {
|
|
92
|
+
* buf.set( value, idx );
|
|
93
|
+
* }
|
|
94
|
+
*
|
|
95
|
+
* // Create the input and output ndarray-like objects:
|
|
96
|
+
* var x = {
|
|
97
|
+
* 'ref': null,
|
|
98
|
+
* 'dtype': 'complex64',
|
|
99
|
+
* 'data': xbuf,
|
|
100
|
+
* 'length': 4,
|
|
101
|
+
* 'shape': shape,
|
|
102
|
+
* 'strides': sx,
|
|
103
|
+
* 'offset': ox,
|
|
104
|
+
* 'order': 'row-major',
|
|
105
|
+
* 'getter': getter
|
|
106
|
+
* };
|
|
107
|
+
* x.ref = x;
|
|
108
|
+
*
|
|
109
|
+
* var y = {
|
|
110
|
+
* 'ref': null,
|
|
111
|
+
* 'dtype': 'complex64',
|
|
112
|
+
* 'data': ybuf,
|
|
113
|
+
* 'length': 4,
|
|
114
|
+
* 'shape': shape,
|
|
115
|
+
* 'strides': sy,
|
|
116
|
+
* 'offset': oy,
|
|
117
|
+
* 'order': 'row-major',
|
|
118
|
+
* 'setter': setter
|
|
119
|
+
* };
|
|
120
|
+
*
|
|
121
|
+
* // Apply the unary function:
|
|
122
|
+
* map( x, y, naryFunction( scale, 1 ) );
|
|
123
|
+
*
|
|
124
|
+
* var v = y.data.get( 0 );
|
|
125
|
+
*
|
|
126
|
+
* var re = realf( v );
|
|
127
|
+
* // returns 10.0
|
|
128
|
+
*
|
|
129
|
+
* var im = imagf( v );
|
|
130
|
+
* // returns 20.0
|
|
131
|
+
*/
|
|
132
|
+
function map( x, y, fcn, thisArg ) {
|
|
133
|
+
var xbuf;
|
|
134
|
+
var ybuf;
|
|
135
|
+
var ordx;
|
|
136
|
+
var ordy;
|
|
137
|
+
var len;
|
|
138
|
+
var get;
|
|
139
|
+
var set;
|
|
140
|
+
var ref;
|
|
141
|
+
var shx;
|
|
142
|
+
var shy;
|
|
143
|
+
var sx;
|
|
144
|
+
var sy;
|
|
145
|
+
var ox;
|
|
146
|
+
var oy;
|
|
147
|
+
var ix;
|
|
148
|
+
var iy;
|
|
149
|
+
var i;
|
|
150
|
+
|
|
151
|
+
// Cache the total number of elements over which to iterate:
|
|
152
|
+
len = x.length;
|
|
153
|
+
|
|
154
|
+
// Cache the input array shape:
|
|
155
|
+
shx = x.shape;
|
|
156
|
+
shy = y.shape;
|
|
157
|
+
|
|
158
|
+
// Cache references to the input and output ndarray data buffers:
|
|
159
|
+
xbuf = x.data;
|
|
160
|
+
ybuf = y.data;
|
|
161
|
+
|
|
162
|
+
// Cache references to the respective stride arrays:
|
|
163
|
+
sx = x.strides;
|
|
164
|
+
sy = y.strides;
|
|
165
|
+
|
|
166
|
+
// Cache the indices of the first indexed elements in the respective ndarrays:
|
|
167
|
+
ox = x.offset;
|
|
168
|
+
oy = y.offset;
|
|
169
|
+
|
|
170
|
+
// Cache the respective array orders:
|
|
171
|
+
ordx = x.order;
|
|
172
|
+
ordy = y.order;
|
|
173
|
+
|
|
174
|
+
// Cache accessors:
|
|
175
|
+
get = x.getter;
|
|
176
|
+
set = y.setter;
|
|
177
|
+
|
|
178
|
+
// Cache the reference to the original input array:
|
|
179
|
+
ref = x.ref;
|
|
180
|
+
|
|
181
|
+
// Check for a zero-dimensional array...
|
|
182
|
+
if ( shx.length === 0 ) {
|
|
183
|
+
set( ybuf, oy, fcn.call( thisArg, get( xbuf, ox ), 0, ref ) );
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
// Iterate over each element based on the linear **view** index, regardless as to how the data is stored in memory (note: this has negative performance implications for non-contiguous ndarrays due to a lack of data locality)...
|
|
187
|
+
for ( i = 0; i < len; i++ ) {
|
|
188
|
+
ix = vind2bind( shx, sx, ox, ordx, i, MODE );
|
|
189
|
+
iy = vind2bind( shy, sy, oy, ordy, i, MODE );
|
|
190
|
+
set( ybuf, iy, fcn.call( thisArg, get( xbuf, ix ), i, ref ) );
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
// EXPORTS //
|
|
196
|
+
|
|
197
|
+
module.exports = map;
|
package/package.json
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stdlib/utils-map",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Apply a function to each element in an array and assign the result to an element in an output array.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "The Stdlib Authors",
|
|
8
|
+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
|
|
9
|
+
},
|
|
10
|
+
"contributors": [
|
|
11
|
+
{
|
|
12
|
+
"name": "The Stdlib Authors",
|
|
13
|
+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
|
|
14
|
+
}
|
|
15
|
+
],
|
|
16
|
+
"main": "./lib",
|
|
17
|
+
"directories": {
|
|
18
|
+
"benchmark": "./benchmark",
|
|
19
|
+
"doc": "./docs",
|
|
20
|
+
"example": "./examples",
|
|
21
|
+
"lib": "./lib",
|
|
22
|
+
"test": "./test"
|
|
23
|
+
},
|
|
24
|
+
"types": "./docs/types",
|
|
25
|
+
"scripts": {
|
|
26
|
+
"test": "make test",
|
|
27
|
+
"test-cov": "make test-cov",
|
|
28
|
+
"examples": "make examples",
|
|
29
|
+
"benchmark": "make benchmark"
|
|
30
|
+
},
|
|
31
|
+
"homepage": "https://stdlib.io",
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git://github.com/stdlib-js/utils-map.git"
|
|
35
|
+
},
|
|
36
|
+
"bugs": {
|
|
37
|
+
"url": "https://github.com/stdlib-js/stdlib/issues"
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@stdlib/array-base-arraylike2object": "^0.0.x",
|
|
41
|
+
"@stdlib/array-base-zeros": "^0.0.x",
|
|
42
|
+
"@stdlib/assert-is-array-like-object": "^0.0.x",
|
|
43
|
+
"@stdlib/assert-is-function": "^0.0.x",
|
|
44
|
+
"@stdlib/assert-is-ndarray-like": "^0.0.x",
|
|
45
|
+
"@stdlib/ndarray-base-assert-is-read-only": "^0.0.x",
|
|
46
|
+
"@stdlib/ndarray-base-maybe-broadcast-array": "^0.0.x",
|
|
47
|
+
"@stdlib/ndarray-base-ndarraylike2object": "^0.0.x",
|
|
48
|
+
"@stdlib/ndarray-base-vind2bind": "^0.0.x",
|
|
49
|
+
"@stdlib/ndarray-zeros": "^0.0.x",
|
|
50
|
+
"@stdlib/types": "^0.0.x",
|
|
51
|
+
"@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.x"
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@stdlib/array-base-filled": "^0.0.x",
|
|
55
|
+
"@stdlib/array-complex64": "^0.0.x",
|
|
56
|
+
"@stdlib/array-filled-by": "^0.0.x",
|
|
57
|
+
"@stdlib/array-float64": "^0.0.x",
|
|
58
|
+
"@stdlib/assert-is-array": "^0.0.x",
|
|
59
|
+
"@stdlib/bench": "^0.0.x",
|
|
60
|
+
"@stdlib/complex-float32": "^0.0.x",
|
|
61
|
+
"@stdlib/complex-imagf": "^0.0.x",
|
|
62
|
+
"@stdlib/complex-realf": "^0.0.x",
|
|
63
|
+
"@stdlib/math-base-special-abs": "^0.0.x",
|
|
64
|
+
"@stdlib/math-base-special-abs2": "^0.0.x",
|
|
65
|
+
"@stdlib/math-base-special-pow": "^0.0.x",
|
|
66
|
+
"@stdlib/ndarray-array": "^0.0.x",
|
|
67
|
+
"@stdlib/ndarray-ctor": "^0.0.x",
|
|
68
|
+
"@stdlib/random-base-discrete-uniform": "^0.0.x",
|
|
69
|
+
"@stdlib/utils-nary-function": "^0.0.x",
|
|
70
|
+
"tape": "git+https://github.com/kgryte/tape.git#fix/globby",
|
|
71
|
+
"istanbul": "^0.4.1",
|
|
72
|
+
"tap-spec": "5.x.x"
|
|
73
|
+
},
|
|
74
|
+
"engines": {
|
|
75
|
+
"node": ">=0.10.0",
|
|
76
|
+
"npm": ">2.7.0"
|
|
77
|
+
},
|
|
78
|
+
"os": [
|
|
79
|
+
"aix",
|
|
80
|
+
"darwin",
|
|
81
|
+
"freebsd",
|
|
82
|
+
"linux",
|
|
83
|
+
"macos",
|
|
84
|
+
"openbsd",
|
|
85
|
+
"sunos",
|
|
86
|
+
"win32",
|
|
87
|
+
"windows"
|
|
88
|
+
],
|
|
89
|
+
"keywords": [
|
|
90
|
+
"stdlib",
|
|
91
|
+
"stdutils",
|
|
92
|
+
"stdutil",
|
|
93
|
+
"utilities",
|
|
94
|
+
"utility",
|
|
95
|
+
"utils",
|
|
96
|
+
"util",
|
|
97
|
+
"map",
|
|
98
|
+
"array",
|
|
99
|
+
"ndarray",
|
|
100
|
+
"tensor",
|
|
101
|
+
"matrix",
|
|
102
|
+
"vector",
|
|
103
|
+
"elementwise",
|
|
104
|
+
"element-wise",
|
|
105
|
+
"transform",
|
|
106
|
+
"invoke",
|
|
107
|
+
"call",
|
|
108
|
+
"apply",
|
|
109
|
+
"function",
|
|
110
|
+
"fun",
|
|
111
|
+
"func",
|
|
112
|
+
"fcn"
|
|
113
|
+
],
|
|
114
|
+
"funding": {
|
|
115
|
+
"type": "patreon",
|
|
116
|
+
"url": "https://www.patreon.com/athan"
|
|
117
|
+
}
|
|
118
|
+
}
|