@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/README.md ADDED
@@ -0,0 +1,396 @@
1
+ <!--
2
+
3
+ @license Apache-2.0
4
+
5
+ Copyright (c) 2022 The Stdlib Authors.
6
+
7
+ Licensed under the Apache License, Version 2.0 (the "License");
8
+ you may not use this file except in compliance with the License.
9
+ You may obtain a copy of the License at
10
+
11
+ http://www.apache.org/licenses/LICENSE-2.0
12
+
13
+ Unless required by applicable law or agreed to in writing, software
14
+ distributed under the License is distributed on an "AS IS" BASIS,
15
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ See the License for the specific language governing permissions and
17
+ limitations under the License.
18
+
19
+ -->
20
+
21
+ # map2Right
22
+
23
+ [![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url] <!-- [![dependencies][dependencies-image]][dependencies-url] -->
24
+
25
+ > Apply a function to elements in two input arrays while iterating from right to left and assign the results to an output array.
26
+
27
+ <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
28
+
29
+ <section class="intro">
30
+
31
+ </section>
32
+
33
+ <!-- /.intro -->
34
+
35
+ <!-- Package usage documentation. -->
36
+
37
+ <section class="installation">
38
+
39
+ ## Installation
40
+
41
+ ```bash
42
+ npm install @stdlib/utils-map2-right
43
+ ```
44
+
45
+ </section>
46
+
47
+ <section class="usage">
48
+
49
+ ## Usage
50
+
51
+ ```javascript
52
+ var map2Right = require( '@stdlib/utils-map2-right' );
53
+ ```
54
+
55
+ <a name="fcn-map2-right"></a>
56
+
57
+ #### map2Right( x, y, fcn\[, thisArg] )
58
+
59
+ Applies a function to elements in two input arrays while iterating from right to left and assigns the results to a new array.
60
+
61
+ ```javascript
62
+ var naryFunction = require( '@stdlib/utils-nary-function' );
63
+ var add = require( '@stdlib/math-base-ops-add' );
64
+
65
+ var x = [ 1, 2, 3, 4, 5, 6 ];
66
+ var y = [ 1, 1, 1, 1, 1, 1 ];
67
+
68
+ var out = map2Right( x, y, naryFunction( add, 2 ) );
69
+ // returns [ 2, 3, 4, 5, 6, 7 ]
70
+ ```
71
+
72
+ The function accepts both array-like objects and [`ndarray`][@stdlib/ndarray/ctor]-like objects.
73
+
74
+ ```javascript
75
+ var naryFunction = require( '@stdlib/utils-nary-function' );
76
+ var add = require( '@stdlib/math-base-ops-add' );
77
+ var array = require( '@stdlib/ndarray-array' );
78
+
79
+ var opts = {
80
+ 'dtype': 'generic'
81
+ };
82
+ var x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ], opts );
83
+ var y = array( [ [ 1, 1, 1 ], [ 1, 1, 1 ] ], opts );
84
+
85
+ var out = map2Right( x, y, naryFunction( add, 2 ) );
86
+ // returns <ndarray>
87
+
88
+ var v = out.get( 1, 1 );
89
+ // returns 6
90
+ ```
91
+
92
+ The applied function is provided the following arguments:
93
+
94
+ - **v1**: element from first input array.
95
+ - **v2**: element from second input array.
96
+ - **idx**: element index.
97
+ - **x**: first input array.
98
+ - **y**: second input array.
99
+
100
+ To set the `this` context when invoking the input function, provide a `thisArg`.
101
+
102
+ <!-- eslint-disable no-invalid-this -->
103
+
104
+ ```javascript
105
+ var add = require( '@stdlib/math-base-ops-add' );
106
+
107
+ function fcn( v1, v2 ) {
108
+ this.count += 1;
109
+ return add( v1, v2 );
110
+ }
111
+
112
+ var x = [ 1, 2, 3, 4, 5, 6 ];
113
+ var y = [ 1, 1, 1, 1, 1, 1 ];
114
+
115
+ var ctx = {
116
+ 'count': 0
117
+ };
118
+
119
+ var out = map2Right( x, y, fcn, ctx );
120
+ // returns [ 2, 3, 4, 5, 6, 7 ]
121
+
122
+ var cnt = ctx.count;
123
+ // returns 6
124
+ ```
125
+
126
+ <a name="method-map2-right-assign"></a>
127
+
128
+ #### map2Right.assign( x, y, out, fcn\[, thisArg] )
129
+
130
+ Applies a function to elements in two input arrays while iterating from right to left and assigns the results to an output array `out`.
131
+
132
+ ```javascript
133
+ var naryFunction = require( '@stdlib/utils-nary-function' );
134
+ var add = require( '@stdlib/math-base-ops-add' );
135
+
136
+ var x = [ 1, 2, 3, 4, 5, 6 ];
137
+ var y = [ 1, 1, 1, 1, 1, 1 ];
138
+ var out = [ 0, 0, 0, 0, 0, 0 ];
139
+
140
+ map2Right.assign( x, y, out, naryFunction( add, 2 ) );
141
+
142
+ console.log( out );
143
+ // => [ 2, 3, 4, 5, 6, 7 ]
144
+ ```
145
+
146
+ The method accepts both array-like objects and [`ndarray`][@stdlib/ndarray/ctor]-like objects.
147
+
148
+ ```javascript
149
+ var naryFunction = require( '@stdlib/utils-nary-function' );
150
+ var add = require( '@stdlib/math-base-ops-add' );
151
+ var array = require( '@stdlib/ndarray-array' );
152
+
153
+ var opts = {
154
+ 'dtype': 'generic',
155
+ 'shape': [ 2, 3 ]
156
+ };
157
+ var x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ], opts );
158
+ var y = array( [ [ 1, 1, 1 ], [ 1, 1, 1 ] ], opts );
159
+ var out = array( opts );
160
+
161
+ map2Right.assign( x, y, out, naryFunction( add, 2 ) );
162
+
163
+ var v = out.get( 1, 1 );
164
+ // returns 6
165
+ ```
166
+
167
+ Input and output arrays must be either all array-like objects or all [`ndarray`][@stdlib/ndarray/ctor]-like objects. If input and output arrays are array-like objects, all arrays **must** have the same number of elements.
168
+
169
+ If input and output arrays are [`ndarray`][@stdlib/ndarray/ctor]-like objects, the arrays **must** be [broadcast compatible][@stdlib/ndarray/base/broadcast-shapes]. To map from one or more input [`ndarrays`][@stdlib/ndarray/ctor] to an output [`ndarray`][@stdlib/ndarray/ctor] which has the same rank (i.e., dimensionality) and the same number of elements, but which is not [broadcast compatible][@stdlib/ndarray/base/broadcast-shapes], reshape the arrays prior to invocation.
170
+
171
+ ```javascript
172
+ var naryFunction = require( '@stdlib/utils-nary-function' );
173
+ var add = require( '@stdlib/math-base-ops-add' );
174
+ var array = require( '@stdlib/ndarray-array' );
175
+
176
+ var opts = {
177
+ 'dtype': 'generic',
178
+ 'shape': [ 2, 3 ]
179
+ };
180
+ var x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ], opts );
181
+ var y = array( [ [ 1, 1, 1 ], [ 1, 1, 1 ] ], opts );
182
+
183
+ opts = {
184
+ 'dtype': 'generic',
185
+ 'shape': [ 2, 2, 3 ] // broadcast compatible shape
186
+ };
187
+ var out = array( opts );
188
+
189
+ map2Right.assign( x, y, out, naryFunction( add, 2 ) );
190
+
191
+ var v = out.get( 0, 1, 1 );
192
+ // returns 6
193
+
194
+ v = out.get( 1, 1, 1 );
195
+ // returns 6
196
+ ```
197
+
198
+ In general, avoid providing output [`ndarray`][@stdlib/ndarray/ctor]-like objects which are [non-contiguous][@stdlib/ndarray/base/assert/is-contiguous] views containing one or more elements referring to the **same** memory location. Writing to an overlapping [non-contiguous][@stdlib/ndarray/base/assert/is-contiguous] view is likely to simultaneously affect multiple elements and yield unexpected results.
199
+
200
+ The applied function is provided the same arguments as with [`map2Right`](#fcn-map2-right).
201
+
202
+ </section>
203
+
204
+ <!-- /.usage -->
205
+
206
+ <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
207
+
208
+ <section class="notes">
209
+
210
+ ## Notes
211
+
212
+ - The [`map2Right`](#fcn-map2-right) function **always** returns an output value having a "generic" data type. For example, if provided array-like objects, the function returns a generic `array`. If provided [`ndarrays`][@stdlib/ndarray/ctor], the function returns an [`ndarray`][@stdlib/ndarray/ctor] having a "generic" data type.
213
+
214
+ Accordingly, when provided a typed array, the [`map2Right`](#fcn-map2-right) function does **not** return a typed array of the same type. To assign results to a typed array, use the [`map2Right.assign`](#method-map2-right-assign) method.
215
+
216
+ - Both [`map2Right`](#fcn-map2-right) and [`map2Right.assign`](#method-map2-right-assign) accept array-like objects exposing getters and setters for array element access (e.g., [`Complex64Array`][@stdlib/array/complex64], [`Complex128Array`][@stdlib/array/complex128], etc).
217
+
218
+ ```javascript
219
+ var naryFunction = require( '@stdlib/utils-nary-function' );
220
+ var Complex64Array = require( '@stdlib/array-complex64' );
221
+ var Complex64 = require( '@stdlib/complex-float32' );
222
+ var realf = require( '@stdlib/complex-realf' );
223
+ var imagf = require( '@stdlib/complex-imagf' );
224
+ var add = require( '@stdlib/math-base-ops-caddf' );
225
+
226
+ var x = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
227
+ var y = new Complex64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
228
+ var z = new Complex64Array( 4 );
229
+
230
+ map2Right.assign( x, y, z, naryFunction( add, 2 ) );
231
+
232
+ var v = z.get( 0 );
233
+
234
+ var re = realf( v );
235
+ // returns 2.0
236
+
237
+ var im = imagf( v );
238
+ // returns 3.0
239
+ ```
240
+
241
+ - When applying a function to [`ndarray`][@stdlib/ndarray/ctor]-like objects, performance will be best for [`ndarray`][@stdlib/ndarray/ctor]-like objects which are single-segment contiguous. For non-contiguous arrays, see [`@stdlib/ndarray/base/binary`][@stdlib/ndarray/base/binary].
242
+
243
+ - Both [`map2Right`](#fcn-map2-right) and [`map2Right.assign`](#method-map2-right-assign) do **not** skip `undefined` elements.
244
+
245
+ </section>
246
+
247
+ <!-- /.notes -->
248
+
249
+ <!-- Package usage examples. -->
250
+
251
+ <section class="examples">
252
+
253
+ ## Examples
254
+
255
+ <!-- eslint no-undef: "error" -->
256
+
257
+ ```javascript
258
+ var filledarrayBy = require( '@stdlib/array-filled-by' );
259
+ var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ).factory;
260
+ var naryFunction = require( '@stdlib/utils-nary-function' );
261
+ var add = require( '@stdlib/math-base-ops-add' );
262
+ var array = require( '@stdlib/ndarray-array' );
263
+ var map2Right = require( '@stdlib/utils-map2-right' );
264
+
265
+ function fill( i ) {
266
+ var rand = discreteUniform( -10*(i+1), 10*(i+1) );
267
+ return filledarrayBy( 10, 'generic', rand );
268
+ }
269
+
270
+ // Create two-dimensional ndarrays (i.e., matrices):
271
+ var opts = {
272
+ 'dtype': 'generic',
273
+ 'flatten': true
274
+ };
275
+ var x = array( filledarrayBy( 10, opts.dtype, fill ), opts );
276
+ var y = array( filledarrayBy( 10, opts.dtype, fill ), opts );
277
+
278
+ // Create an explicit binary function:
279
+ var f = naryFunction( add, 2 );
280
+
281
+ // Compute element-wise sums...
282
+ var z = map2Right( x, y, f );
283
+
284
+ console.log( 'x:' );
285
+ console.log( x.data );
286
+
287
+ console.log( 'y:' );
288
+ console.log( y.data );
289
+
290
+ console.log( 'z:' );
291
+ console.log( z.data );
292
+ ```
293
+
294
+ </section>
295
+
296
+ <!-- /.examples -->
297
+
298
+ <!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
299
+
300
+ <section class="references">
301
+
302
+ </section>
303
+
304
+ <!-- /.references -->
305
+
306
+ <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
307
+
308
+ <section class="related">
309
+
310
+ </section>
311
+
312
+ <!-- /.related -->
313
+
314
+ <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
315
+
316
+
317
+ <section class="main-repo" >
318
+
319
+ * * *
320
+
321
+ ## Notice
322
+
323
+ This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
324
+
325
+ For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib].
326
+
327
+ #### Community
328
+
329
+ [![Chat][chat-image]][chat-url]
330
+
331
+ ---
332
+
333
+ ## License
334
+
335
+ See [LICENSE][stdlib-license].
336
+
337
+
338
+ ## Copyright
339
+
340
+ Copyright &copy; 2016-2022. The Stdlib [Authors][stdlib-authors].
341
+
342
+ </section>
343
+
344
+ <!-- /.stdlib -->
345
+
346
+ <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
347
+
348
+ <section class="links">
349
+
350
+ [npm-image]: http://img.shields.io/npm/v/@stdlib/utils-map2-right.svg
351
+ [npm-url]: https://npmjs.org/package/@stdlib/utils-map2-right
352
+
353
+ [test-image]: https://github.com/stdlib-js/utils-map2-right/actions/workflows/test.yml/badge.svg
354
+ [test-url]: https://github.com/stdlib-js/utils-map2-right/actions/workflows/test.yml
355
+
356
+ [coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/utils-map2-right/main.svg
357
+ [coverage-url]: https://codecov.io/github/stdlib-js/utils-map2-right?branch=main
358
+
359
+ <!--
360
+
361
+ [dependencies-image]: https://img.shields.io/david/stdlib-js/utils-map2-right.svg
362
+ [dependencies-url]: https://david-dm.org/stdlib-js/utils-map2-right/main
363
+
364
+ -->
365
+
366
+ [umd]: https://github.com/umdjs/umd
367
+ [es-module]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules
368
+
369
+ [deno-url]: https://github.com/stdlib-js/utils-map2-right/tree/deno
370
+ [umd-url]: https://github.com/stdlib-js/utils-map2-right/tree/umd
371
+ [esm-url]: https://github.com/stdlib-js/utils-map2-right/tree/esm
372
+
373
+ [chat-image]: https://img.shields.io/gitter/room/stdlib-js/stdlib.svg
374
+ [chat-url]: https://gitter.im/stdlib-js/stdlib/
375
+
376
+ [stdlib]: https://github.com/stdlib-js/stdlib
377
+
378
+ [stdlib-authors]: https://github.com/stdlib-js/stdlib/graphs/contributors
379
+
380
+ [stdlib-license]: https://raw.githubusercontent.com/stdlib-js/utils-map2-right/main/LICENSE
381
+
382
+ [@stdlib/ndarray/ctor]: https://www.npmjs.com/package/@stdlib/stdlib
383
+
384
+ [@stdlib/ndarray/base/binary]: https://www.npmjs.com/package/@stdlib/stdlib
385
+
386
+ [@stdlib/ndarray/base/broadcast-shapes]: https://www.npmjs.com/package/@stdlib/stdlib
387
+
388
+ [@stdlib/ndarray/base/assert/is-contiguous]: https://www.npmjs.com/package/@stdlib/stdlib
389
+
390
+ [@stdlib/array/complex64]: https://www.npmjs.com/package/@stdlib/stdlib
391
+
392
+ [@stdlib/array/complex128]: https://www.npmjs.com/package/@stdlib/stdlib
393
+
394
+ </section>
395
+
396
+ <!-- /.links -->
package/docs/repl.txt ADDED
@@ -0,0 +1,129 @@
1
+
2
+ {{alias}}( x, y, fcn[, thisArg] )
3
+ Applies a function to elements in two input arrays while iterating from
4
+ right to left and assigns the results to a new array.
5
+
6
+ The applied function is provided the following arguments:
7
+
8
+ - v1: element from first input array.
9
+ - v2: element from second input array.
10
+ - idx: element index.
11
+ - x: first input array.
12
+ - y: second input array.
13
+
14
+ The returned output array always has a "generic" data type. For example, if
15
+ provided an array-like object, the function returns a generic array. If
16
+ provided an ndarray, the function returns an ndarray having a "generic" data
17
+ type.
18
+
19
+ Input arrays must be either both array-like objects or both ndarray-like
20
+ objects.
21
+
22
+ If input arrays are array-like objects, the arrays must have the same number
23
+ of elements.
24
+
25
+ If input arrays are ndarray-like objects, the arrays must be broadcast
26
+ compatible.
27
+
28
+ Parameters
29
+ ----------
30
+ x: ArrayLikeObject|ndarray
31
+ First input array.
32
+
33
+ y: ArrayLikeObject|ndarray
34
+ Second input array.
35
+
36
+ fcn: Function
37
+ Function to apply.
38
+
39
+ thisArg: any (optional)
40
+ Input function context.
41
+
42
+ Returns
43
+ -------
44
+ out: Array|ndarray
45
+ Output array.
46
+
47
+ Examples
48
+ --------
49
+ // array-like object:
50
+ > var f = {{alias:@stdlib/utils/nary-function}}( {{alias:@stdlib/math/base/ops/add}}, 2 );
51
+ > var x = [ 1, 2, 3, 4, 5, 6 ];
52
+ > var y = [ 1, 1, 1, 1, 1, 1 ];
53
+ > var out = {{alias}}( x, y, f )
54
+ [ 2, 3, 4, 5, 6, 7 ]
55
+
56
+ // ndarray:
57
+ > x = {{alias:@stdlib/ndarray/array}}( x, { 'shape': [ 2, 3 ] } );
58
+ > y = {{alias:@stdlib/ndarray/array}}( y, { 'shape': [ 2, 3 ] } );
59
+ > out = {{alias}}( x, y, f );
60
+ > var v = out.get( 1, 1 )
61
+ 6
62
+
63
+
64
+ {{alias}}.assign( x, y, out, fcn[, thisArg] )
65
+ Applies a function to elements in two input arrays while iterating from
66
+ right to left and assigns the results to an output array.
67
+
68
+ The applied function is provided the following arguments:
69
+
70
+ - v1: element from first input array.
71
+ - v2: element from second input array.
72
+ - idx: element index.
73
+ - x: first input array.
74
+ - y: second input array.
75
+
76
+ Input and output arrays must be either all array-like objects or all
77
+ ndarray-like objects.
78
+
79
+ If input and output arrays are array-like objects, the arrays must have the
80
+ same number of elements.
81
+
82
+ If input and output arrays are ndarray-like objects, the arrays must be
83
+ broadcast compatible.
84
+
85
+ Parameters
86
+ ----------
87
+ x: ArrayLikeObject|ndarray
88
+ First input array.
89
+
90
+ y: ArrayLikeObject|ndarray
91
+ Second input array.
92
+
93
+ out: ArrayLikeObject|ndarray
94
+ Output array.
95
+
96
+ fcn: Function
97
+ Function to apply.
98
+
99
+ thisArg: any (optional)
100
+ Input function context.
101
+
102
+ Returns
103
+ -------
104
+ out: Array|ndarray
105
+ Output array.
106
+
107
+ Examples
108
+ --------
109
+ // array-like object:
110
+ > var f = {{alias:@stdlib/utils/nary-function}}( {{alias:@stdlib/math/base/ops/add}}, 2 );
111
+ > var x = [ 1, 2, 3, 4, 5, 6 ];
112
+ > var y = [ 1, 1, 1, 1, 1, 1 ];
113
+ > var out = [ 0, 0, 0, 0, 0, 0 ];
114
+ > {{alias}}.assign( x, y, out, f );
115
+ > out
116
+ [ 2, 3, 4, 5, 6, 7 ]
117
+
118
+ // ndarray:
119
+ > var opts = { 'shape': [ 2, 3 ] };
120
+ > x = {{alias:@stdlib/ndarray/array}}( x, opts );
121
+ > y = {{alias:@stdlib/ndarray/array}}( y, opts );
122
+ > out = {{alias:@stdlib/ndarray/array}}( [ 0, 0, 0, 0, 0, 0 ], opts );
123
+ > {{alias}}.assign( x, y, out, f );
124
+ > var v = out.get( 1, 1 )
125
+ 6
126
+
127
+ See Also
128
+ --------
129
+