@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/lib/ndarray.js ADDED
@@ -0,0 +1,236 @@
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 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 elements in two input arrays while iterating from right to left and assigns the results to an output array.
35
+ *
36
+ * @private
37
+ * @param {Object} x - object containing meta data for the first input ndarray
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 meta data for the second input ndarray
48
+ * @param {string} y.ref - reference to original input ndarray-like object
49
+ * @param {string} y.dtype - data type
50
+ * @param {Collection} y.data - data buffer
51
+ * @param {NonNegativeInteger} y.length - number of elements
52
+ * @param {NonNegativeIntegerArray} y.shape - dimensions
53
+ * @param {IntegerArray} y.strides - stride lengths
54
+ * @param {NonNegativeInteger} y.offset - index offset
55
+ * @param {string} y.order - specifies whether `y` is row-major (C-style) or column-major (Fortran-style)
56
+ * @param {Function} y.getter - callback for accessing `y` data buffer elements
57
+ * @param {Object} z - object containing output ndarray meta data
58
+ * @param {string} z.dtype - data type
59
+ * @param {Collection} z.data - data buffer
60
+ * @param {NonNegativeInteger} z.length - number of elements
61
+ * @param {NonNegativeIntegerArray} z.shape - dimensions
62
+ * @param {IntegerArray} z.strides - stride lengths
63
+ * @param {NonNegativeInteger} z.offset - index offset
64
+ * @param {string} z.order - specifies whether `z` is row-major (C-style) or column-major (Fortran-style)
65
+ * @param {Function} z.setter - callback for setting `z` data buffer elements
66
+ * @param {Function} fcn - function to apply
67
+ * @param {*} thisArg - function execution context
68
+ * @returns {void}
69
+ *
70
+ * @example
71
+ * var Complex64Array = require( '@stdlib/array-complex64' );
72
+ * var Complex64 = require( '@stdlib/complex-float32' );
73
+ * var realf = require( '@stdlib/complex-realf' );
74
+ * var imagf = require( '@stdlib/complex-imagf' );
75
+ * var naryFunction = require( '@stdlib/utils-nary-function' );
76
+ * var add = require( '@stdlib/math-base-ops-caddf' );
77
+ *
78
+ * // Create data buffers:
79
+ * var xbuf = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
80
+ * var ybuf = new Complex64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
81
+ * var zbuf = new Complex64Array( 4 );
82
+ *
83
+ * // Define the shape of the input and output arrays:
84
+ * var shape = [ 2, 2 ];
85
+ *
86
+ * // Define the array strides:
87
+ * var sx = [ 2, 1 ];
88
+ * var sy = [ 2, 1 ];
89
+ * var sz = [ 2, 1 ];
90
+ *
91
+ * // Define the index offsets:
92
+ * var ox = 0;
93
+ * var oy = 0;
94
+ * var oz = 0;
95
+ *
96
+ * // Define getters and setters:
97
+ * function getter( buf, idx ) {
98
+ * return buf.get( idx );
99
+ * }
100
+ *
101
+ * function setter( buf, idx, value ) {
102
+ * buf.set( value, idx );
103
+ * }
104
+ *
105
+ * // Create the input and output ndarray-like objects:
106
+ * var x = {
107
+ * 'ref': null,
108
+ * 'dtype': 'complex64',
109
+ * 'data': xbuf,
110
+ * 'length': 4,
111
+ * 'shape': shape,
112
+ * 'strides': sx,
113
+ * 'offset': ox,
114
+ * 'order': 'row-major',
115
+ * 'getter': getter
116
+ * };
117
+ * x.ref = x;
118
+ *
119
+ * var y = {
120
+ * 'ref': null,
121
+ * 'dtype': 'complex64',
122
+ * 'data': ybuf,
123
+ * 'length': 4,
124
+ * 'shape': shape,
125
+ * 'strides': sy,
126
+ * 'offset': ox,
127
+ * 'order': 'row-major',
128
+ * 'getter': getter
129
+ * };
130
+ * y.ref = y;
131
+ *
132
+ * var z = {
133
+ * 'ref': null,
134
+ * 'dtype': 'complex64',
135
+ * 'data': zbuf,
136
+ * 'length': 4,
137
+ * 'shape': shape,
138
+ * 'strides': sz,
139
+ * 'offset': oz,
140
+ * 'order': 'row-major',
141
+ * 'setter': setter
142
+ * };
143
+ *
144
+ * // Apply the function:
145
+ * map2Right( x, y, z, naryFunction( add, 2 ) );
146
+ *
147
+ * var v = z.data.get( 0 );
148
+ *
149
+ * var re = realf( v );
150
+ * // returns 2.0
151
+ *
152
+ * var im = imagf( v );
153
+ * // returns 3.0
154
+ */
155
+ function map2Right( x, y, z, fcn, thisArg ) {
156
+ var xbuf;
157
+ var ybuf;
158
+ var zbuf;
159
+ var ordx;
160
+ var ordy;
161
+ var ordz;
162
+ var xget;
163
+ var yget;
164
+ var zset;
165
+ var xref;
166
+ var yref;
167
+ var shx;
168
+ var shy;
169
+ var shz;
170
+ var len;
171
+ var sx;
172
+ var sy;
173
+ var sz;
174
+ var ox;
175
+ var oy;
176
+ var oz;
177
+ var ix;
178
+ var iy;
179
+ var iz;
180
+ var i;
181
+
182
+ // Cache the total number of elements over which to iterate:
183
+ len = x.length;
184
+
185
+ // Cache the array shapes:
186
+ shx = x.shape;
187
+ shy = y.shape;
188
+ shz = z.shape;
189
+
190
+ // Cache references to the input and output ndarray data buffers:
191
+ xbuf = x.data;
192
+ ybuf = y.data;
193
+ zbuf = z.data;
194
+
195
+ // Cache references to the respective stride arrays:
196
+ sx = x.strides;
197
+ sy = y.strides;
198
+ sz = z.strides;
199
+
200
+ // Cache the indices of the first indexed elements in the respective ndarrays:
201
+ ox = x.offset;
202
+ oy = y.offset;
203
+ oz = z.offset;
204
+
205
+ // Cache the respective array orders:
206
+ ordx = x.order;
207
+ ordy = y.order;
208
+ ordz = z.order;
209
+
210
+ // Cache accessors:
211
+ xget = x.getter;
212
+ yget = y.getter;
213
+ zset = z.setter;
214
+
215
+ // Cache references to the original input arrays:
216
+ xref = x.ref;
217
+ yref = y.ref;
218
+
219
+ // Check for a zero-dimensional array...
220
+ if ( shx.length === 0 && shy.length === 0 ) {
221
+ zset( zbuf, oz, fcn.call( thisArg, xget( xbuf, ox ), yget( ybuf, oy ), 0, xref, yref ) ); // eslint-disable-line max-len
222
+ return;
223
+ }
224
+ // Iterate over the arrays 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)...
225
+ for ( i = len-1; i >= 0; i-- ) {
226
+ ix = vind2bind( shx, sx, ox, ordx, i, MODE );
227
+ iy = vind2bind( shy, sy, oy, ordy, i, MODE );
228
+ iz = vind2bind( shz, sz, oz, ordz, i, MODE );
229
+ zset( zbuf, iz, fcn.call( thisArg, xget( xbuf, ix ), yget( ybuf, iy ), i, xref, yref ) ); // eslint-disable-line max-len
230
+ }
231
+ }
232
+
233
+
234
+ // EXPORTS //
235
+
236
+ module.exports = map2Right;
package/package.json ADDED
@@ -0,0 +1,119 @@
1
+ {
2
+ "name": "@stdlib/utils-map2-right",
3
+ "version": "0.0.1",
4
+ "description": "Apply a function to elements in two input arrays while iterating from right to left and assign the results to 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-map2-right.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-broadcast-shapes": "^0.0.x",
47
+ "@stdlib/ndarray-base-maybe-broadcast-array": "^0.0.x",
48
+ "@stdlib/ndarray-base-ndarraylike2object": "^0.0.x",
49
+ "@stdlib/ndarray-base-vind2bind": "^0.0.x",
50
+ "@stdlib/ndarray-zeros": "^0.0.x",
51
+ "@stdlib/types": "^0.0.x",
52
+ "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.x"
53
+ },
54
+ "devDependencies": {
55
+ "@stdlib/array-base-filled": "^0.0.x",
56
+ "@stdlib/array-complex64": "^0.0.x",
57
+ "@stdlib/array-filled-by": "^0.0.x",
58
+ "@stdlib/array-float64": "^0.0.x",
59
+ "@stdlib/assert-is-array": "^0.0.x",
60
+ "@stdlib/bench": "^0.0.x",
61
+ "@stdlib/complex-imagf": "^0.0.x",
62
+ "@stdlib/complex-realf": "^0.0.x",
63
+ "@stdlib/math-base-ops-add": "^0.0.x",
64
+ "@stdlib/math-base-ops-caddf": "^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
+ "binary"
114
+ ],
115
+ "funding": {
116
+ "type": "patreon",
117
+ "url": "https://www.patreon.com/athan"
118
+ }
119
+ }