Skip to content

Commit f3b79c9

Browse files
committed
feat: add ndarray/broadcast-arrays
1 parent 7de0830 commit f3b79c9

File tree

12 files changed

+1884
-0
lines changed

12 files changed

+1884
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2023 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+
# broadcastArrays
22+
23+
> Broadcast [ndarrays][@stdlib/ndarray/ctor] to a common shape.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var broadcastArrays = require( '@stdlib/ndarray/broadcast-arrays' );
41+
```
42+
43+
#### broadcastArrays( ...arrays )
44+
45+
Broadcasts a list of [ndarrays][@stdlib/ndarray/ctor] to a common shape.
46+
47+
```javascript
48+
var array = require( '@stdlib/ndarray/array' );
49+
var zeros = require( '@stdlib/ndarray/zeros' );
50+
51+
// Create a 2x2 ndarray:
52+
var x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
53+
// returns <ndarray>
54+
55+
// Create a 2x2x2 ndarray:
56+
var y = zeros( [ 2, 2, 2 ] );
57+
// returns <ndarray>
58+
59+
// Broadcast to a common shape:
60+
var out = broadcastArrays( [ x, y ] );
61+
// returns [ <ndarray>, <ndarray> ]
62+
```
63+
64+
The function supports two (mutually exclusive) means for providing [ndarray][@stdlib/ndarray/ctor] arguments:
65+
66+
1. providing a single array of [ndarray][@stdlib/ndarray/ctor] arguments.
67+
2. providing [ndarray][@stdlib/ndarray/ctor] arguments as separate arguments.
68+
69+
```javascript
70+
var array = require( '@stdlib/ndarray/array' );
71+
var zeros = require( '@stdlib/ndarray/zeros' );
72+
73+
// Create a 2x2 ndarray:
74+
var x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
75+
// returns <ndarray>
76+
77+
// Create a 2x2x2 ndarray:
78+
var y = zeros( [ 2, 2, 2 ] );
79+
// returns <ndarray>
80+
81+
// Broadcast to a common shape:
82+
var out = broadcastArrays( x, y );
83+
// returns [ <ndarray>, <ndarray> ]
84+
```
85+
86+
</section>
87+
88+
<!-- /.usage -->
89+
90+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
91+
92+
<section class="notes">
93+
94+
## Notes
95+
96+
- The function throws an error if provided [broadcast-incompatible][@stdlib/ndarray/base/broadcast-shapes] ndarrays.
97+
- Returned [ndarrays][@stdlib/ndarray/ctor] are **read-only** views on their respective underlying data buffers. The views are typically **not** contiguous. As more than one element of a returned view may refer to the same memory location, writing to a view may affect multiple elements. If you need to write to a returned [ndarray][@stdlib/ndarray/ctor], copy the [ndarray][@stdlib/ndarray/ctor] **before** broadcasting.
98+
- The function always returns new [ndarray][@stdlib/ndarray/ctor] instances even if an input [ndarray][@stdlib/ndarray/ctor] shape and the broadcasted shape are the same.
99+
100+
</section>
101+
102+
<!-- /.notes -->
103+
104+
<!-- Package usage examples. -->
105+
106+
<section class="examples">
107+
108+
## Examples
109+
110+
<!-- eslint no-undef: "error" -->
111+
112+
```javascript
113+
var array = require( '@stdlib/ndarray/array' );
114+
var zeros = require( '@stdlib/ndarray/zeros' );
115+
var numel = require( '@stdlib/ndarray/base/numel' );
116+
var ind2sub = require( '@stdlib/ndarray/ind2sub' );
117+
var broadcastArrays = require( '@stdlib/ndarray/broadcast-arrays' );
118+
119+
// Create a 2x2 array:
120+
var x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
121+
// returns <ndarray>
122+
123+
// Create a 3x2x2 array:
124+
var y = zeros( [ 3, 2, 2 ] );
125+
// returns <ndarray>
126+
127+
// Broadcast the arrays to a common shape:
128+
var out = broadcastArrays( [ x, y ] );
129+
// returns [ <ndarray>, <ndarray> ]
130+
131+
// Retrieve the common shape:
132+
var sh = out[ 0 ].shape;
133+
// returns [ 3, 2, 2 ]
134+
135+
// Retrieve the number of elements:
136+
var N = numel( sh );
137+
138+
// Loop through the array elements...
139+
var i;
140+
for ( i = 0; i < N; i++ ) {
141+
console.log( 'X[%s] = %d', ind2sub( sh, i ).join( ', ' ), out[ 0 ].iget( i ) );
142+
}
143+
```
144+
145+
</section>
146+
147+
<!-- /.examples -->
148+
149+
<!-- 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. -->
150+
151+
<section class="references">
152+
153+
</section>
154+
155+
<!-- /.references -->
156+
157+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
158+
159+
<section class="related">
160+
161+
</section>
162+
163+
<!-- /.related -->
164+
165+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
166+
167+
<section class="links">
168+
169+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
170+
171+
[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes
172+
173+
</section>
174+
175+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 Float64Array = require( '@stdlib/array/float64' );
25+
var ndarrayBase = require( '@stdlib/ndarray/base/ctor' );
26+
var ndarray = require( '@stdlib/ndarray/ctor' );
27+
var zeros = require( '@stdlib/ndarray/base/zeros' );
28+
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
29+
var pkg = require( './../package.json' ).name;
30+
var broadcastArrays = require( './../lib' );
31+
32+
33+
// MAIN //
34+
35+
bench( pkg+'::base_ndarray,2d:num_arrays=2', function benchmark( b ) {
36+
var strides;
37+
var values;
38+
var buffer;
39+
var offset;
40+
var dtype;
41+
var shape;
42+
var order;
43+
var out;
44+
var y;
45+
var i;
46+
47+
dtype = 'float64';
48+
buffer = new Float64Array( 4 );
49+
shape = [ 2, 2 ];
50+
strides = [ 2, 1 ];
51+
offset = 0;
52+
order = 'row-major';
53+
54+
values = [
55+
ndarrayBase( dtype, buffer, shape, strides, offset, order ),
56+
ndarrayBase( dtype, buffer, shape, strides, offset, order ),
57+
ndarrayBase( dtype, buffer, shape, strides, offset, order ),
58+
ndarrayBase( dtype, buffer, shape, strides, offset, order ),
59+
ndarrayBase( dtype, buffer, shape, strides, offset, order )
60+
];
61+
y = zeros( dtype, [ 2, 2, 2 ], order );
62+
63+
b.tic();
64+
for ( i = 0; i < b.iterations; i++ ) {
65+
out = broadcastArrays( [ values[ i%values.length ], y ] );
66+
if ( typeof out !== 'object' ) {
67+
b.fail( 'should return an object' );
68+
}
69+
}
70+
b.toc();
71+
if ( !isndarrayLike( out[ 0 ] ) || !isndarrayLike( out[ 1 ] ) ) {
72+
b.fail( 'should return an array of ndarrays' );
73+
}
74+
b.pass( 'benchmark finished' );
75+
b.end();
76+
});
77+
78+
bench( pkg+'::ndarray,2d:num_arrays=2', function benchmark( b ) {
79+
var strides;
80+
var values;
81+
var buffer;
82+
var offset;
83+
var dtype;
84+
var shape;
85+
var order;
86+
var out;
87+
var y;
88+
var i;
89+
90+
dtype = 'float64';
91+
buffer = new Float64Array( 4 );
92+
shape = [ 2, 2 ];
93+
strides = [ 2, 1 ];
94+
offset = 0;
95+
order = 'row-major';
96+
97+
values = [
98+
ndarray( dtype, buffer, shape, strides, offset, order ),
99+
ndarray( dtype, buffer, shape, strides, offset, order ),
100+
ndarray( dtype, buffer, shape, strides, offset, order ),
101+
ndarray( dtype, buffer, shape, strides, offset, order ),
102+
ndarray( dtype, buffer, shape, strides, offset, order )
103+
];
104+
y = zeros( dtype, [ 2, 2, 2 ], order );
105+
106+
b.tic();
107+
for ( i = 0; i < b.iterations; i++ ) {
108+
out = broadcastArrays( [ values[ i%values.length ], y ] );
109+
if ( typeof out !== 'object' ) {
110+
b.fail( 'should return an object' );
111+
}
112+
}
113+
b.toc();
114+
if ( !isndarrayLike( out[ 0 ] ) || !isndarrayLike( out[ 1 ] ) ) {
115+
b.fail( 'should return an array of ndarrays' );
116+
}
117+
b.pass( 'benchmark finished' );
118+
b.end();
119+
});

0 commit comments

Comments
 (0)