Skip to content

Commit 72ed2e1

Browse files
headlessNodekgryte
andauthored
feat: add ndarray/base/map
PR-URL: #2715 Ref: #2656 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent 12a87d5 commit 72ed2e1

File tree

93 files changed

+18090
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+18090
-0
lines changed
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
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/base/map' );
37+
```
38+
39+
#### map( arrays, 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+
46+
function scale( x ) {
47+
return x * 10.0;
48+
}
49+
50+
// Create data buffers:
51+
var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
52+
var ybuf = new Float64Array( 6 );
53+
54+
// Define the shape of the input and output arrays:
55+
var shape = [ 3, 2 ];
56+
57+
// Define the array strides:
58+
var sx = [ 2, 1 ];
59+
var sy = [ 2, 1 ];
60+
61+
// Define the index offsets:
62+
var ox = 0;
63+
var oy = 0;
64+
65+
// Create the input and output ndarray-like objects:
66+
var x = {
67+
'ref': null,
68+
'dtype': 'float64',
69+
'data': xbuf,
70+
'shape': shape,
71+
'strides': sx,
72+
'offset': ox,
73+
'order': 'row-major'
74+
};
75+
var y = {
76+
'dtype': 'float64',
77+
'data': ybuf,
78+
'shape': shape,
79+
'strides': sy,
80+
'offset': oy,
81+
'order': 'row-major'
82+
};
83+
84+
// Apply the map function:
85+
map( [ x, y ], scale );
86+
87+
console.log( y.data );
88+
// => <Float64Array>[ 10.0, 20.0, 30.0, 40.0, 50.0, 60.0 ]
89+
```
90+
91+
The function accepts the following arguments:
92+
93+
- **arrays**: array-like object containing one input ndarray and one output ndarray.
94+
- **fcn**: callback to apply.
95+
- **thisArg**: callback execution context.
96+
97+
The callback function is provided the following arguments:
98+
99+
- **values**: current array element.
100+
- **indices**: current array element indices.
101+
- **arr**: the input ndarray.
102+
103+
</section>
104+
105+
<!-- /.usage -->
106+
107+
<section class="notes">
108+
109+
## Notes
110+
111+
- Each provided ndarray should be an object with the following properties:
112+
113+
- **dtype**: data type.
114+
- **data**: data buffer.
115+
- **shape**: dimensions.
116+
- **strides**: stride lengths.
117+
- **offset**: index offset.
118+
- **order**: specifies whether an ndarray is row-major (C-style) or column major (Fortran-style).
119+
120+
- 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.
121+
122+
</section>
123+
124+
<!-- /.notes -->
125+
126+
<section class="examples">
127+
128+
## Examples
129+
130+
<!-- eslint no-undef: "error" -->
131+
132+
```javascript
133+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
134+
var filledarray = require( '@stdlib/array/filled' );
135+
var filledarrayBy = require( '@stdlib/array/filled-by' );
136+
var abs = require( '@stdlib/math/base/special/abs' );
137+
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
138+
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
139+
var naryFunction = require( '@stdlib/utils/nary-function' );
140+
var map = require( '@stdlib/ndarray/base/map' );
141+
142+
var N = 10;
143+
var x = {
144+
'dtype': 'generic',
145+
'data': filledarrayBy( N, 'generic', discreteUniform( -100, 100 ) ),
146+
'shape': [ 5, 2 ],
147+
'strides': [ 2, 1 ],
148+
'offset': 0,
149+
'order': 'row-major'
150+
};
151+
var y = {
152+
'dtype': 'generic',
153+
'data': filledarray( 0, N, 'generic' ),
154+
'shape': x.shape.slice(),
155+
'strides': shape2strides( x.shape, 'column-major' ),
156+
'offset': 0,
157+
'order': 'column-major'
158+
};
159+
160+
map( [ x, y ], naryFunction( abs, 1 ) );
161+
console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) );
162+
console.log( ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ) );
163+
```
164+
165+
</section>
166+
167+
<!-- /.examples -->
168+
169+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
170+
171+
<section class="related">
172+
173+
</section>
174+
175+
<!-- /.related -->
176+
177+
<section class="links">
178+
179+
<!-- <related-links> -->
180+
181+
<!-- </related-links> -->
182+
183+
</section>
184+
185+
<!-- /.links -->
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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 randu = require( '@stdlib/random/base/randu' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var floor = require( '@stdlib/math/base/special/floor' );
28+
var round = require( '@stdlib/math/base/special/round' );
29+
var identity = require( '@stdlib/math/base/special/identity' );
30+
var filledarray = require( '@stdlib/array/filled' );
31+
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
32+
var pkg = require( './../package.json' ).name;
33+
var map = require( './../lib/10d_blocked.js' );
34+
35+
36+
// VARIABLES //
37+
38+
var types = [ 'float64' ];
39+
var order = 'column-major';
40+
41+
42+
// FUNCTIONS //
43+
44+
/**
45+
* Creates a benchmark function.
46+
*
47+
* @private
48+
* @param {PositiveInteger} len - ndarray 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 x;
56+
var y;
57+
var i;
58+
59+
x = filledarray( 0.0, len, xtype );
60+
y = filledarray( 0.0, len, ytype );
61+
for ( i = 0; i < len; i++ ) {
62+
x[ i ] = round( ( randu()*200.0 ) - 100.0 );
63+
}
64+
x = {
65+
'dtype': xtype,
66+
'data': x,
67+
'shape': shape,
68+
'strides': shape2strides( shape, order ),
69+
'offset': 0,
70+
'order': order
71+
};
72+
y = {
73+
'dtype': ytype,
74+
'data': y,
75+
'shape': shape,
76+
'strides': shape2strides( shape, order ),
77+
'offset': 0,
78+
'order': order
79+
};
80+
return benchmark;
81+
82+
/**
83+
* Benchmark function.
84+
*
85+
* @private
86+
* @param {Benchmark} b - benchmark instance
87+
*/
88+
function benchmark( b ) {
89+
var i;
90+
91+
b.tic();
92+
for ( i = 0; i < b.iterations; i++ ) {
93+
map( x, y, identity );
94+
if ( isnan( y.data[ i%len ] ) ) {
95+
b.fail( 'should not return NaN' );
96+
}
97+
}
98+
b.toc();
99+
if ( isnan( y.data[ i%len ] ) ) {
100+
b.fail( 'should not return NaN' );
101+
}
102+
b.pass( 'benchmark finished' );
103+
b.end();
104+
}
105+
}
106+
107+
108+
// MAIN //
109+
110+
/**
111+
* Main execution sequence.
112+
*
113+
* @private
114+
*/
115+
function main() {
116+
var len;
117+
var min;
118+
var max;
119+
var sh;
120+
var t1;
121+
var t2;
122+
var f;
123+
var i;
124+
var j;
125+
126+
min = 1; // 10^min
127+
max = 6; // 10^max
128+
129+
for ( j = 0; j < types.length; j++ ) {
130+
t1 = types[ j ];
131+
t2 = types[ j ];
132+
for ( i = min; i <= max; i++ ) {
133+
len = pow( 10, i );
134+
135+
sh = [ len/2, 2, 1, 1, 1, 1, 1, 1, 1, 1 ];
136+
f = createBenchmark( len, sh, t1, t2 );
137+
bench( pkg+'::blocked:ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+order+',yorder='+order+',xtype='+t1+',ytype='+t2, f );
138+
139+
sh = [ 1, 1, 1, 1, 1, 1, 1, 1, 2, len/2 ];
140+
f = createBenchmark( len, sh, t1, t2 );
141+
bench( pkg+'::blocked:ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+order+',yorder='+order+',xtype='+t1+',ytype='+t2, f );
142+
143+
len = floor( pow( len, 1.0/10.0 ) );
144+
sh = [ len, len, len, len, len, len, len, len, len, len ];
145+
len *= pow( len, 9 );
146+
f = createBenchmark( len, sh, t1, t2 );
147+
bench( pkg+'::blocked:ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+order+',yorder='+order+',xtype='+t1+',ytype='+t2, f );
148+
}
149+
}
150+
}
151+
152+
main();

0 commit comments

Comments
 (0)