Skip to content

Commit 1191cc5

Browse files
committed
feat: add ndarray/map
1 parent a0ba090 commit 1191cc5

File tree

10 files changed

+2494
-0
lines changed

10 files changed

+2494
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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 and assign results to elements in an output ndarray.
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 and assigns results to elements in an output ndarray.
42+
43+
```javascript
44+
var Float64Array = require( '@stdlib/array/float64' );
45+
var ndarray = require( '@stdlib/ndarray/ctor' );
46+
47+
function scale( x ) {
48+
return x * 10.0;
49+
}
50+
51+
// Create the input data buffer:
52+
var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
53+
54+
// Define the shape of the input ndarray:
55+
var shape = [ 3, 2 ];
56+
57+
// Define the array strides:
58+
var strides = [ 2, 1 ];
59+
60+
// Define the index offset:
61+
var offset = 0;
62+
63+
// Create the input ndarray:
64+
var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' );
65+
66+
// Apply the map function:
67+
var y = map( x, scale );
68+
69+
console.log( y.data );
70+
// => <Float64Array>[ 10.0, 20.0, 30.0, 40.0, 50.0, 60.0 ]
71+
```
72+
73+
The function accepts the following arguments:
74+
75+
- **x**: input ndarray.
76+
- **options**: function options.
77+
- **fcn**: callback to apply.
78+
- **thisArg**: callback execution context.
79+
80+
The function accepts the following options:
81+
82+
- **dtype**: output ndarray data type. Defaults to match the input ndarray if not specified.
83+
84+
The callback function is provided the following arguments:
85+
86+
- **values**: current array element.
87+
- **indices**: current array element indices.
88+
- **arr**: the input ndarray.
89+
90+
</section>
91+
92+
<!-- /.usage -->
93+
94+
<section class="notes">
95+
96+
## Notes
97+
98+
- 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.
99+
100+
</section>
101+
102+
<!-- /.notes -->
103+
104+
<section class="examples">
105+
106+
## Examples
107+
108+
<!-- eslint no-undef: "error" -->
109+
110+
```javascript
111+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
112+
var abs = require( '@stdlib/math/base/special/abs' );
113+
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
114+
var naryFunction = require( '@stdlib/utils/nary-function' );
115+
var ndarray = require( '@stdlib/ndarray/ctor' );
116+
var map = require( '@stdlib/ndarray/map' );
117+
118+
var buffer = discreteUniform( 10, -100, 100, {
119+
'dtype': 'generic'
120+
});
121+
var shape = [ 5, 2 ];
122+
var strides = [ 2, 1 ];
123+
var offset = 0;
124+
var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' );
125+
126+
var y = map( x, naryFunction( abs, 1 ) );
127+
console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) );
128+
console.log( ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ) );
129+
```
130+
131+
</section>
132+
133+
<!-- /.examples -->
134+
135+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
136+
137+
<section class="related">
138+
139+
</section>
140+
141+
<!-- /.related -->
142+
143+
<section class="links">
144+
145+
<!-- <related-links> -->
146+
147+
<!-- </related-links> -->
148+
149+
</section>
150+
151+
<!-- /.links -->
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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 order = 'row-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+
* @returns {Function} benchmark function
53+
*/
54+
function createBenchmark( len, shape, xtype, ytype ) {
55+
var strides;
56+
var opts;
57+
var xbuf;
58+
var x;
59+
60+
xbuf = discreteUniform( len, -100, 100, {
61+
'dtype': xtype
62+
});
63+
strides = shape2strides( shape, order );
64+
x = ndarray( xtype, xbuf, shape, strides, 0, order );
65+
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 sh;
111+
var t1;
112+
var t2;
113+
var f;
114+
var i;
115+
var j;
116+
117+
min = 1; // 10^min
118+
max = 6; // 10^max
119+
120+
for ( j = 0; j < ytypes.length; j++ ) {
121+
t1 = xtypes[ 0 ];
122+
t2 = ytypes[ j ];
123+
for ( i = min; i <= max; i++ ) {
124+
len = pow( 10, i );
125+
126+
sh = [ len ];
127+
f = createBenchmark( len, sh, t1, t2 );
128+
bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+order+',yorder='+order+',xtype='+t1+',ytype='+t2, f );
129+
}
130+
}
131+
}
132+
133+
main();
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
2+
{{alias}}( x, [ options, ]fcn[, thisArg] )
3+
Applies a callback function to elements in an input ndarray and assigns
4+
results to elements in an output ndarray.
5+
6+
The function accepts the following options:
7+
8+
- **dtype**: output ndarray data type. Defaults to match the input ndarray
9+
if not specified.
10+
11+
The callback function is provided the following arguments:
12+
13+
- value: current array element.
14+
- indices: current array element indices.
15+
- arr: the input ndarray.
16+
17+
Parameters
18+
----------
19+
x: ndarray
20+
Input ndarray.
21+
22+
options: Object (optional)
23+
Function options.
24+
25+
fcn: Function
26+
Callback function.
27+
28+
thisArg: any (optional)
29+
Callback function execution context.
30+
31+
Examples
32+
--------
33+
// Define ndarray data and meta data...
34+
> var buffer = new {{alias:@stdlib/array/float64}}( [ 1.0, 2.0, 3.0, 4.0 ] );
35+
> var dtype = 'float64';
36+
> var shape = [ 2, 2 ];
37+
> var strides = [ 2, 1 ];
38+
> var offset = 0;
39+
> var order = 'row-major';
40+
41+
// Define a callback function:
42+
> function f( v ) { return v*10.0; };
43+
44+
// Using ndarray...
45+
> var x = {{alias:@stdlib/ndarray/ctor}}( dtype, buffer, shape, strides, offset, order );
46+
> var y = {{alias}}( x, f );
47+
> y.data
48+
<Float64Array>[ 10.0, 20.0, 30.0, 40.0 ]
49+
50+
// Using options...
51+
> var buffer = new {{alias:@stdlib/array/float64}}( [ 1.0, 2.0, 3.0, 4.0 ] );
52+
> var dtype = 'float64';
53+
> var shape = [ 2, 2 ];
54+
> var strides = [ 2, 1 ];
55+
> var offset = 0;
56+
> var order = 'row-major';
57+
58+
// Define a callback function:
59+
> function f( v ) { return v*10.0; };
60+
61+
// Define the options:
62+
> var opts = { 'dtype': 'float32' };
63+
64+
// Using ndarray...
65+
> var x = {{alias:@stdlib/ndarray/ctor}}( dtype, buffer, shape, strides, offset, order );
66+
> var y = {{alias}}( x, opts, f );
67+
> y.data
68+
<Float32Array>[ 10.0, 20.0, 30.0, 40.0 ]
69+
70+
See Also
71+
--------

0 commit comments

Comments
 (0)