@stdlib/utils-map2-right 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 +396 -0
- package/docs/repl.txt +129 -0
- package/docs/types/index.d.ts +363 -0
- package/docs/types/test.ts +343 -0
- package/lib/array.js +100 -0
- package/lib/assign.js +151 -0
- package/lib/index.js +103 -0
- package/lib/main.js +147 -0
- package/lib/ndarray.js +236 -0
- package/package.json +119 -0
package/lib/array.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2022 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
|
+
// MAIN //
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Applies a function to elements in two input arrays while iterating from right to left and assigns the results to an output array.
|
|
25
|
+
*
|
|
26
|
+
* @private
|
|
27
|
+
* @param {Object} x - object containing data for the first input array
|
|
28
|
+
* @param {ArrayLikeObject} x.data - array data
|
|
29
|
+
* @param {Function} x.getter - callback for accessing array data elements
|
|
30
|
+
* @param {Object} y - object containing data for the second input array
|
|
31
|
+
* @param {ArrayLikeObject} y.data - array data
|
|
32
|
+
* @param {Function} y.getter - callback for accessing array data elements
|
|
33
|
+
* @param {Object} z - object containing output array data
|
|
34
|
+
* @param {ArrayLikeObject} z.data - array data
|
|
35
|
+
* @param {Function} z.setter - callback for setting array data elements
|
|
36
|
+
* @param {Function} fcn - function to apply
|
|
37
|
+
* @param {*} thisArg - function execution context
|
|
38
|
+
* @returns {void}
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* var naryFunction = require( '@stdlib/utils-nary-function' );
|
|
42
|
+
* var add = require( '@stdlib/math-base-ops-add' );
|
|
43
|
+
*
|
|
44
|
+
* // Define getters and setters:
|
|
45
|
+
* function getter( buf, idx ) {
|
|
46
|
+
* return buf[ idx ];
|
|
47
|
+
* }
|
|
48
|
+
*
|
|
49
|
+
* function setter( buf, idx, value ) {
|
|
50
|
+
* buf[ idx ] = value;
|
|
51
|
+
* }
|
|
52
|
+
*
|
|
53
|
+
* // Create the input and output array objects:
|
|
54
|
+
* var x = {
|
|
55
|
+
* 'data': [ 1, 2, 3, 4, 5, 6 ],
|
|
56
|
+
* 'getter': getter
|
|
57
|
+
* };
|
|
58
|
+
* var y = {
|
|
59
|
+
* 'data': [ 1, 1, 1, 1, 1, 1 ],
|
|
60
|
+
* 'getter': getter
|
|
61
|
+
* };
|
|
62
|
+
* var z = {
|
|
63
|
+
* 'data': [ 0, 0, 0, 0, 0, 0 ],
|
|
64
|
+
* 'setter': setter
|
|
65
|
+
* };
|
|
66
|
+
*
|
|
67
|
+
* map2Right( x, y, z, naryFunction( add, 2 ) );
|
|
68
|
+
*
|
|
69
|
+
* var data = z.data;
|
|
70
|
+
* // returns [ 2, 3, 4, 5, 6, 7 ]
|
|
71
|
+
*/
|
|
72
|
+
function map2Right( x, y, z, fcn, thisArg ) {
|
|
73
|
+
var xbuf;
|
|
74
|
+
var ybuf;
|
|
75
|
+
var zbuf;
|
|
76
|
+
var xget;
|
|
77
|
+
var yget;
|
|
78
|
+
var zset;
|
|
79
|
+
var i;
|
|
80
|
+
|
|
81
|
+
// Cache references to the input and output data:
|
|
82
|
+
xbuf = x.data;
|
|
83
|
+
ybuf = y.data;
|
|
84
|
+
zbuf = z.data;
|
|
85
|
+
|
|
86
|
+
// Cache accessors:
|
|
87
|
+
xget = x.getter;
|
|
88
|
+
yget = y.getter;
|
|
89
|
+
zset = z.setter;
|
|
90
|
+
|
|
91
|
+
// Iterate over the elements...
|
|
92
|
+
for ( i = xbuf.length-1; i >= 0; i-- ) {
|
|
93
|
+
zset( zbuf, i, fcn.call( thisArg, xget( xbuf, i ), yget( ybuf, i ), i, xbuf, ybuf ) ); // eslint-disable-line max-len
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
// EXPORTS //
|
|
99
|
+
|
|
100
|
+
module.exports = map2Right;
|
package/lib/assign.js
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2022 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 elements in two input arrays while iterating from right to left and assigns the results to an output array.
|
|
38
|
+
*
|
|
39
|
+
* ## Notes
|
|
40
|
+
*
|
|
41
|
+
* - The applied function is provided the following arguments:
|
|
42
|
+
*
|
|
43
|
+
* - **v1**: element from first input array.
|
|
44
|
+
* - **v2**: element from second input array.
|
|
45
|
+
* - **idx**: element index.
|
|
46
|
+
* - **x**: first input array.
|
|
47
|
+
* - **y**: second input array.
|
|
48
|
+
*
|
|
49
|
+
* @param {(ArrayLikeObject|ndarray)} x - first input array
|
|
50
|
+
* @param {(ArrayLikeObject|ndarray)} y - second input array
|
|
51
|
+
* @param {(ArrayLikeObject|ndarray)} out - output array
|
|
52
|
+
* @param {Function} fcn - function to apply
|
|
53
|
+
* @param {*} [thisArg] - function execution context
|
|
54
|
+
* @throws {TypeError} first argument must be an array-like object or an ndarray
|
|
55
|
+
* @throws {TypeError} second argument must be an array-like object or an ndarray
|
|
56
|
+
* @throws {TypeError} third argument must be an array-like object or an ndarray
|
|
57
|
+
* @throws {TypeError} fourth argument must be a function
|
|
58
|
+
* @throws {TypeError} input and output arrays must be either all array-like objects or all ndarrays
|
|
59
|
+
* @throws {RangeError} input and output arrays must have the same length
|
|
60
|
+
* @throws {Error} input and output ndarrays must be broadcast compatible
|
|
61
|
+
* @throws {Error} cannot write to a read-only ndarray
|
|
62
|
+
* @returns {(ArrayLikeObject|ndarray)} output array
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* var naryFunction = require( '@stdlib/utils-nary-function' );
|
|
66
|
+
* var add = require( '@stdlib/math-base-ops-add' );
|
|
67
|
+
*
|
|
68
|
+
* var x = [ 1, 2, 3, 4, 5, 6 ];
|
|
69
|
+
* var y = [ 1, 1, 1, 1, 1, 1 ];
|
|
70
|
+
* var out = [ 0, 0, 0, 0, 0, 0 ];
|
|
71
|
+
*
|
|
72
|
+
* map2Right( x, y, out, naryFunction( add, 2 ) );
|
|
73
|
+
*
|
|
74
|
+
* console.log( out );
|
|
75
|
+
* // => [ 2, 3, 4, 5, 6, 7 ]
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* var naryFunction = require( '@stdlib/utils-nary-function' );
|
|
79
|
+
* var add = require( '@stdlib/math-base-ops-add' );
|
|
80
|
+
* var array = require( '@stdlib/ndarray-array' );
|
|
81
|
+
*
|
|
82
|
+
* var opts = {
|
|
83
|
+
* 'dtype': 'generic',
|
|
84
|
+
* 'shape': [ 2, 3 ]
|
|
85
|
+
* };
|
|
86
|
+
* var x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ], opts );
|
|
87
|
+
* var y = array( [ [ 1, 1, 1 ], [ 1, 1, 1 ] ], opts );
|
|
88
|
+
* var out = array( opts );
|
|
89
|
+
*
|
|
90
|
+
* map2Right( x, y, out, naryFunction( add, 2 ) );
|
|
91
|
+
*
|
|
92
|
+
* var data = out.data;
|
|
93
|
+
* // returns [ 2, 3, 4, 5, 6, 7 ]
|
|
94
|
+
*/
|
|
95
|
+
function map2Right( x, y, out, fcn, thisArg ) {
|
|
96
|
+
var isxnd;
|
|
97
|
+
var isynd;
|
|
98
|
+
var isznd;
|
|
99
|
+
var tmp;
|
|
100
|
+
var sh;
|
|
101
|
+
if ( !isFunction( fcn ) ) {
|
|
102
|
+
throw new TypeError( 'invalid argument. Fourth argument must be a function. Value: `' + fcn + '`.' );
|
|
103
|
+
}
|
|
104
|
+
isxnd = isndarrayLike( x );
|
|
105
|
+
isynd = isndarrayLike( y );
|
|
106
|
+
isznd = isndarrayLike( out );
|
|
107
|
+
if ( isxnd ) { // note: assertion order matters here, as an ndarray-like object is also array-like
|
|
108
|
+
if ( !isynd ) {
|
|
109
|
+
throw new TypeError( 'invalid argument. If the first input array is an ndarray, the second input array must also be an ndarray. Value: `' + y + '`.' );
|
|
110
|
+
}
|
|
111
|
+
if ( !isznd ) {
|
|
112
|
+
throw new TypeError( 'invalid argument. If the input arrays are ndarrays, the output array must also be an ndarray. Value: `' + out + '`.' );
|
|
113
|
+
}
|
|
114
|
+
if ( isReadOnly( out ) ) {
|
|
115
|
+
throw new Error( 'invalid argument. The output ndarray must be writable. Cannot write to a read-only ndarray.' );
|
|
116
|
+
}
|
|
117
|
+
out = ndarraylike2object( out );
|
|
118
|
+
sh = out.shape;
|
|
119
|
+
|
|
120
|
+
// Broadcast and wrap the input arrays and ensure that the `ref` properties point to the original input arrays...
|
|
121
|
+
tmp = ndarraylike2object( broadcast( x, sh ) );
|
|
122
|
+
tmp.ref = x;
|
|
123
|
+
x = tmp;
|
|
124
|
+
|
|
125
|
+
tmp = ndarraylike2object( broadcast( y, sh ) );
|
|
126
|
+
tmp.ref = y;
|
|
127
|
+
y = tmp;
|
|
128
|
+
|
|
129
|
+
ndarrayFcn( x, y, out, fcn, thisArg );
|
|
130
|
+
return out.ref;
|
|
131
|
+
}
|
|
132
|
+
if ( isArrayLikeObject( x ) ) {
|
|
133
|
+
if ( isynd || !isArrayLikeObject( y ) ) {
|
|
134
|
+
throw new TypeError( 'invalid argument. If the first input array is an array-like object, the second input array must also be an array-like object. Value: `' + y + '`.' );
|
|
135
|
+
}
|
|
136
|
+
if ( isznd || !isArrayLikeObject( out ) ) {
|
|
137
|
+
throw new TypeError( 'invalid argument. If the input arrays are array-like objects, the output array must also be an array-like object. Value: `' + out + '`.' );
|
|
138
|
+
}
|
|
139
|
+
if ( x.length !== y.length || y.length !== out.length ) {
|
|
140
|
+
throw new RangeError( 'invalid arguments. Input and output arrays must have the same number of elements ( i.e., length).' );
|
|
141
|
+
}
|
|
142
|
+
arrayFcn( arraylike2object( x ), arraylike2object( y ), arraylike2object( out ), fcn, thisArg ); // eslint-disable-line max-len
|
|
143
|
+
return out;
|
|
144
|
+
}
|
|
145
|
+
throw new TypeError( 'invalid argument. First argument must be an array-like object or an ndarray. Value: `' + x + '`.' );
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
// EXPORTS //
|
|
150
|
+
|
|
151
|
+
module.exports = map2Right;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2022 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 elements in two input arrays while iterating from right to left and assign the results to an output array.
|
|
23
|
+
*
|
|
24
|
+
* @module @stdlib/utils-map2-right
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* var naryFunction = require( '@stdlib/utils-nary-function' );
|
|
28
|
+
* var add = require( '@stdlib/math-base-ops-add' );
|
|
29
|
+
* var map2Right = require( '@stdlib/utils-map2-right' );
|
|
30
|
+
*
|
|
31
|
+
* var x = [ 1, 2, 3, 4, 5, 6 ];
|
|
32
|
+
* var y = [ 1, 1, 1, 1, 1, 1 ];
|
|
33
|
+
*
|
|
34
|
+
* var out = map2Right( x, y, naryFunction( add, 2 ) );
|
|
35
|
+
* // returns [ 2, 3, 4, 5, 6, 7 ]
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* var naryFunction = require( '@stdlib/utils-nary-function' );
|
|
39
|
+
* var add = require( '@stdlib/math-base-ops-add' );
|
|
40
|
+
* var array = require( '@stdlib/ndarray-array' );
|
|
41
|
+
* var map2Right = require( '@stdlib/utils-map2-right' );
|
|
42
|
+
*
|
|
43
|
+
* var opts = {
|
|
44
|
+
* 'dtype': 'generic'
|
|
45
|
+
* };
|
|
46
|
+
* var x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ], opts );
|
|
47
|
+
* var y = array( [ [ 1, 1, 1 ], [ 1, 1, 1 ] ], opts );
|
|
48
|
+
*
|
|
49
|
+
* var out = map2Right( x, y. naryFunction( add, 2 ) );
|
|
50
|
+
* // returns <ndarray>
|
|
51
|
+
*
|
|
52
|
+
* var data = out.data;
|
|
53
|
+
* // returns [ 2, 3, 4, 5, 6, 7 ]
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* var naryFunction = require( '@stdlib/utils-nary-function' );
|
|
57
|
+
* var add = require( '@stdlib/math-base-ops-add' );
|
|
58
|
+
* var map2Right = require( '@stdlib/utils-map2-right' );
|
|
59
|
+
*
|
|
60
|
+
* var x = [ 1, 2, 3, 4, 5, 6 ];
|
|
61
|
+
* var y = [ 1, 1, 1, 1, 1, 1 ];
|
|
62
|
+
* var out = [ 0, 0, 0, 0, 0, 0 ];
|
|
63
|
+
*
|
|
64
|
+
* map2Right.assign( x, y, out, naryFunction( add, 2 ) );
|
|
65
|
+
*
|
|
66
|
+
* console.log( out );
|
|
67
|
+
* // => [ 2, 3, 4, 5, 6, 7 ]
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* var naryFunction = require( '@stdlib/utils-nary-function' );
|
|
71
|
+
* var add = require( '@stdlib/math-base-ops-add' );
|
|
72
|
+
* var array = require( '@stdlib/ndarray-array' );
|
|
73
|
+
* var map2Right = require( '@stdlib/utils-map2-right' );
|
|
74
|
+
*
|
|
75
|
+
* var opts = {
|
|
76
|
+
* 'dtype': 'generic',
|
|
77
|
+
* 'shape': [ 2, 3 ]
|
|
78
|
+
* };
|
|
79
|
+
* var x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ], opts );
|
|
80
|
+
* var y = array( [ [ 1, 1, 1 ], [ 1, 1, 1 ] ], opts );
|
|
81
|
+
* var out = array( opts );
|
|
82
|
+
*
|
|
83
|
+
* map2Right.assign( x, y, out, naryFunction( add, 2 ) );
|
|
84
|
+
*
|
|
85
|
+
* var data = out.data;
|
|
86
|
+
* // returns [ 2, 3, 4, 5, 6, 7 ]
|
|
87
|
+
*/
|
|
88
|
+
|
|
89
|
+
// MODULES //
|
|
90
|
+
|
|
91
|
+
var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );
|
|
92
|
+
var main = require( './main.js' );
|
|
93
|
+
var assign = require( './assign.js' );
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
// MAIN //
|
|
97
|
+
|
|
98
|
+
setReadOnly( main, 'assign', assign );
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
// EXPORTS //
|
|
102
|
+
|
|
103
|
+
module.exports = main;
|
package/lib/main.js
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2022 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 broadcastShapes = require( '@stdlib/ndarray-base-broadcast-shapes' );
|
|
31
|
+
var broadcast = require( '@stdlib/ndarray-base-maybe-broadcast-array' );
|
|
32
|
+
var ndarrayFcn = require( './ndarray.js' );
|
|
33
|
+
var arrayFcn = require( './array.js' );
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
// MAIN //
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Applies a function to elements in two input arrays while iterating from right to left and assigns the results to a new array.
|
|
40
|
+
*
|
|
41
|
+
* ## Notes
|
|
42
|
+
*
|
|
43
|
+
* - The applied function is provided the following arguments:
|
|
44
|
+
*
|
|
45
|
+
* - **v1**: element from first input array.
|
|
46
|
+
* - **v2**: element from second input array.
|
|
47
|
+
* - **idx**: element index.
|
|
48
|
+
* - **x**: first input array.
|
|
49
|
+
* - **y**: second input array.
|
|
50
|
+
*
|
|
51
|
+
* @param {(ArrayLikeObject|ndarray)} x - first input array
|
|
52
|
+
* @param {(ArrayLikeObject|ndarray)} y - second input array
|
|
53
|
+
* @param {Function} fcn - function to apply
|
|
54
|
+
* @param {*} [thisArg] - function execution context
|
|
55
|
+
* @throws {TypeError} first argument must be an array-like object or an ndarray
|
|
56
|
+
* @throws {TypeError} second argument must be an array-like object or an ndarray
|
|
57
|
+
* @throws {TypeError} input arrays must be either both array-like objects or both ndarrays
|
|
58
|
+
* @throws {RangeError} input arrays must have the same length
|
|
59
|
+
* @throws {Error} input ndarrays must be broadcast compatible
|
|
60
|
+
* @throws {TypeError} third argument must be a function
|
|
61
|
+
* @returns {(Array|ndarray)} output array
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* var naryFunction = require( '@stdlib/utils-nary-function' );
|
|
65
|
+
* var add = require( '@stdlib/math-base-ops-add' );
|
|
66
|
+
*
|
|
67
|
+
* var x = [ 1, 2, 3, 4, 5, 6 ];
|
|
68
|
+
* var y = [ 1, 1, 1, 1, 1, 1 ];
|
|
69
|
+
*
|
|
70
|
+
* var out = map2Right( x, y, naryFunction( add, 2 ) );
|
|
71
|
+
* // returns [ 2, 3, 4, 5, 6, 7 ]
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* var naryFunction = require( '@stdlib/utils-nary-function' );
|
|
75
|
+
* var add = require( '@stdlib/math-base-ops-add' );
|
|
76
|
+
* var array = require( '@stdlib/ndarray-array' );
|
|
77
|
+
*
|
|
78
|
+
* var opts = {
|
|
79
|
+
* 'dtype': 'generic'
|
|
80
|
+
* };
|
|
81
|
+
* var x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ], opts );
|
|
82
|
+
* var y = array( [ [ 1, 1, 1 ], [ 1, 1, 1 ] ], opts );
|
|
83
|
+
*
|
|
84
|
+
* var out = map2Right( x, y, naryFunction( add, 2 ) );
|
|
85
|
+
* // returns <ndarray>
|
|
86
|
+
*
|
|
87
|
+
* var data = out.data;
|
|
88
|
+
* // returns [ 2, 3, 4, 5, 6, 7 ]
|
|
89
|
+
*/
|
|
90
|
+
function map2Right( x, y, fcn, thisArg ) {
|
|
91
|
+
var isxnd;
|
|
92
|
+
var isynd;
|
|
93
|
+
var out;
|
|
94
|
+
var tmp;
|
|
95
|
+
var sh;
|
|
96
|
+
|
|
97
|
+
if ( !isFunction( fcn ) ) {
|
|
98
|
+
throw new TypeError( 'invalid argument. Second argument must be a function. Value: `' + fcn + '`.' );
|
|
99
|
+
}
|
|
100
|
+
isxnd = isndarrayLike( x );
|
|
101
|
+
isynd = isndarrayLike( y );
|
|
102
|
+
if ( isxnd ) { // note: assertion order matters here, as an ndarray-like object is also array-like
|
|
103
|
+
if ( !isynd ) {
|
|
104
|
+
throw new TypeError( 'invalid argument. If the first input array is an ndarray, the second input array must also be an ndarray. Value: `' + y + '`.' );
|
|
105
|
+
}
|
|
106
|
+
// Broadcast `x` and `y` to a common shape:
|
|
107
|
+
sh = broadcastShapes( [ x.shape, y.shape ] );
|
|
108
|
+
if ( sh === null ) {
|
|
109
|
+
throw new Error( 'invalid arguments. Input ndarrays must be broadcast compatible.' );
|
|
110
|
+
}
|
|
111
|
+
// Broadcast and wrap the input arrays and ensure that the `ref` properties point to the original input arrays...
|
|
112
|
+
tmp = ndarraylike2object( broadcast( x, sh ) );
|
|
113
|
+
tmp.ref = x;
|
|
114
|
+
x = tmp;
|
|
115
|
+
|
|
116
|
+
tmp = ndarraylike2object( broadcast( y, sh ) );
|
|
117
|
+
tmp.ref = y;
|
|
118
|
+
y = tmp;
|
|
119
|
+
|
|
120
|
+
// Create an output array:
|
|
121
|
+
out = ndzeros( sh, {
|
|
122
|
+
'dtype': 'generic',
|
|
123
|
+
'order': x.order
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
// Apply the function to the input arrays:
|
|
127
|
+
ndarrayFcn( x, y, ndarraylike2object( out ), fcn, thisArg );
|
|
128
|
+
return out;
|
|
129
|
+
}
|
|
130
|
+
if ( isArrayLikeObject( x ) ) {
|
|
131
|
+
if ( isynd || !isArrayLikeObject( y ) ) {
|
|
132
|
+
throw new TypeError( 'invalid argument. If the first input array is an array-like object, the second input array must also be an array-like object. Value: `' + y + '`.' );
|
|
133
|
+
}
|
|
134
|
+
if ( y.length !== x.length ) {
|
|
135
|
+
throw new RangeError( 'invalid arguments. Input arrays must have the same number of elements (i.e., length).' );
|
|
136
|
+
}
|
|
137
|
+
out = zeros( x.length );
|
|
138
|
+
arrayFcn( arraylike2object( x ), arraylike2object( y ), arraylike2object( out ), fcn, thisArg ); // eslint-disable-line max-len
|
|
139
|
+
return out;
|
|
140
|
+
}
|
|
141
|
+
throw new TypeError( 'invalid argument. First argument must be an array-like object or an ndarray. Value: `' + x + '`.' );
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
// EXPORTS //
|
|
146
|
+
|
|
147
|
+
module.exports = map2Right;
|