Skip to content

Commit 3ea906b

Browse files
headlessNodekgryte
andauthored
feat: add ndarray/map
PR-URL: #3314 Ref: #2656 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Muhammad Haris <101793258+headlessNode@users.noreply.github.com>
1 parent fcaf452 commit 3ea906b

File tree

11 files changed

+2763
-0
lines changed

11 files changed

+2763
-0
lines changed
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
# map
22+
23+
> Apply a callback function to elements in an input [ndarray][@stdlib/ndarray/ctor] and assign results to elements in a new output [ndarray][@stdlib/ndarray/ctor].
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var map = require( '@stdlib/ndarray/map' );
37+
```
38+
39+
#### map( x\[, options], fcn\[, thisArg] )
40+
41+
Applies a callback function to elements in an input [ndarray][@stdlib/ndarray/ctor] and assigns results to elements in a new output [ndarray][@stdlib/ndarray/ctor].
42+
43+
<!-- eslint-disable max-len -->
44+
45+
```javascript
46+
var Float64Array = require( '@stdlib/array/float64' );
47+
var ndarray = require( '@stdlib/ndarray/ctor' );
48+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
49+
50+
function scale( z ) {
51+
return z * 10.0;
52+
}
53+
54+
var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
55+
var shape = [ 2, 3 ];
56+
var strides = [ 6, 1 ];
57+
var offset = 1;
58+
59+
var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' );
60+
// returns <ndarray>
61+
62+
var y = map( x, scale );
63+
// returns <ndarray>
64+
65+
var arr = ndarray2array( y );
66+
// returns [ [ 20.0, 30.0, 40.0 ], [ 80.0, 90.0, 100.0 ] ]
67+
```
68+
69+
The function accepts the following arguments:
70+
71+
- **x**: input [ndarray][@stdlib/ndarray/ctor].
72+
- **options**: function options.
73+
- **fcn**: callback to apply.
74+
- **thisArg**: callback execution context.
75+
76+
The function accepts the following options:
77+
78+
- **dtype**: output ndarray [data type][@stdlib/ndarray/dtypes]. If not specified, the output ndarray [data type][@stdlib/ndarray/dtypes] is inferred from the input [ndarray][@stdlib/ndarray/ctor].
79+
80+
By default, the output ndarray [data type][@stdlib/ndarray/dtypes] is inferred from the input [ndarray][@stdlib/ndarray/ctor]. To return an ndarray with a different [data type][@stdlib/ndarray/dtypes], specify the `dtype` option.
81+
82+
<!-- eslint-disable max-len -->
83+
84+
```javascript
85+
var Float64Array = require( '@stdlib/array/float64' );
86+
var ndarray = require( '@stdlib/ndarray/ctor' );
87+
var dtype = require( '@stdlib/ndarray/dtype' );
88+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
89+
90+
function scale( z ) {
91+
return z * 10.0;
92+
}
93+
94+
var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
95+
var shape = [ 2, 3 ];
96+
var strides = [ 6, 1 ];
97+
var offset = 1;
98+
99+
var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' );
100+
// returns <ndarray>
101+
102+
var opts = {
103+
'dtype': 'float32'
104+
};
105+
var y = map( x, opts, scale );
106+
// returns <ndarray>
107+
108+
var dt = dtype( y );
109+
// returns 'float32'
110+
111+
var arr = ndarray2array( y );
112+
// returns [ [ 20.0, 30.0, 40.0 ], [ 80.0, 90.0, 100.0 ] ]
113+
```
114+
115+
The callback function is provided the following arguments:
116+
117+
- **values**: current array element.
118+
- **indices**: current array element indices.
119+
- **arr**: the input [ndarray][@stdlib/ndarray/ctor].
120+
121+
</section>
122+
123+
<!-- /.usage -->
124+
125+
<section class="notes">
126+
127+
## Notes
128+
129+
- The function does **not** perform explicit casting (e.g., from a real-valued floating-point number to a complex floating-point number). Any such casting should be performed by a provided callback function.
130+
131+
<!-- eslint-disable max-len -->
132+
133+
```javascript
134+
var Float64Array = require( '@stdlib/array/float64' );
135+
var ndarray = require( '@stdlib/ndarray/ctor' );
136+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
137+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
138+
139+
function toComplex( z ) {
140+
return new Complex128( z, 0.0 );
141+
}
142+
143+
var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
144+
var shape = [ 2, 3 ];
145+
var strides = [ 6, 1 ];
146+
var offset = 1;
147+
148+
var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' );
149+
// returns <ndarray>
150+
151+
var opts = {
152+
'dtype': 'complex128'
153+
};
154+
var y = map( x, opts, toComplex );
155+
// returns <ndarray>
156+
```
157+
158+
- For very high-dimensional ndarrays which are non-contiguous, one should consider copying the underlying data to contiguous memory before applying a callback function in order to achieve better performance.
159+
160+
</section>
161+
162+
<!-- /.notes -->
163+
164+
<section class="examples">
165+
166+
## Examples
167+
168+
<!-- eslint no-undef: "error" -->
169+
170+
```javascript
171+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
172+
var abs = require( '@stdlib/math/base/special/abs' );
173+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
174+
var naryFunction = require( '@stdlib/utils/nary-function' );
175+
var ndarray = require( '@stdlib/ndarray/ctor' );
176+
var map = require( '@stdlib/ndarray/map' );
177+
178+
var buffer = discreteUniform( 10, -100, 100, {
179+
'dtype': 'generic'
180+
});
181+
var shape = [ 5, 2 ];
182+
var strides = [ 2, 1 ];
183+
var offset = 0;
184+
var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' );
185+
console.log( ndarray2array( x ) );
186+
187+
var y = map( x, naryFunction( abs, 1 ) );
188+
console.log( ndarray2array( y ) );
189+
```
190+
191+
</section>
192+
193+
<!-- /.examples -->
194+
195+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
196+
197+
<section class="related">
198+
199+
</section>
200+
201+
<!-- /.related -->
202+
203+
<section class="links">
204+
205+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
206+
207+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
208+
209+
<!-- <related-links> -->
210+
211+
<!-- </related-links> -->
212+
213+
</section>
214+
215+
<!-- /.links -->
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 bench = require( '@stdlib/bench' );
24+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
27+
var identity = require( '@stdlib/math/base/special/identity' );
28+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
29+
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
30+
var ndarray = require( '@stdlib/ndarray/ctor' );
31+
var pkg = require( './../package.json' ).name;
32+
var map = require( './../lib' );
33+
34+
35+
// VARIABLES //
36+
37+
var xtypes = [ 'generic' ];
38+
var ytypes = [ 'float64' ];
39+
var orders = [ 'row-major', 'column-major' ];
40+
41+
42+
// FUNCTIONS //
43+
44+
/**
45+
* Creates a benchmark function.
46+
*
47+
* @private
48+
* @param {PositiveInteger} len - array length
49+
* @param {NonNegativeIntegerArray} shape - ndarray shape
50+
* @param {string} xtype - input ndarray data type
51+
* @param {string} ytype - output ndarray data type
52+
* @param {string} order - ndarray memory layout
53+
* @returns {Function} benchmark function
54+
*/
55+
function createBenchmark( len, shape, xtype, ytype, order ) {
56+
var strides;
57+
var opts;
58+
var xbuf;
59+
var x;
60+
61+
xbuf = discreteUniform( len, -100, 100, {
62+
'dtype': xtype
63+
});
64+
strides = shape2strides( shape, order );
65+
x = ndarray( xtype, xbuf, shape, strides, 0, order );
66+
opts = {
67+
'dtype': ytype
68+
};
69+
70+
return benchmark;
71+
72+
/**
73+
* Benchmark function.
74+
*
75+
* @private
76+
* @param {Benchmark} b - benchmark instance
77+
*/
78+
function benchmark( b ) {
79+
var y;
80+
var i;
81+
82+
b.tic();
83+
for ( i = 0; i < b.iterations; i++ ) {
84+
y = map( x, opts, identity );
85+
if ( isnan( y.data[ i%len ] ) ) {
86+
b.fail( 'should not return NaN' );
87+
}
88+
}
89+
b.toc();
90+
if ( !isndarrayLike( y ) ) {
91+
b.fail( 'should return an ndarray' );
92+
}
93+
b.pass( 'benchmark finished' );
94+
b.end();
95+
}
96+
}
97+
98+
99+
// MAIN //
100+
101+
/**
102+
* Main execution sequence.
103+
*
104+
* @private
105+
*/
106+
function main() {
107+
var len;
108+
var min;
109+
var max;
110+
var ord;
111+
var sh;
112+
var t1;
113+
var t2;
114+
var f;
115+
var i;
116+
var j;
117+
var k;
118+
119+
min = 1; // 10^min
120+
max = 6; // 10^max
121+
122+
for ( k = 0; k < orders.length; k++ ) {
123+
ord = orders[ k ];
124+
for ( j = 0; j < xtypes.length; j++ ) {
125+
t1 = xtypes[ j ];
126+
t2 = ytypes[ j ];
127+
for ( i = min; i <= max; i++ ) {
128+
len = pow( 10, i );
129+
130+
sh = [ len ];
131+
f = createBenchmark( len, sh, t1, t2, ord );
132+
bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',yorder='+ord+',xtype='+t1+',ytype='+t2, f );
133+
}
134+
}
135+
}
136+
}
137+
138+
main();

0 commit comments

Comments
 (0)