|
| 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 | +# maybeBroadcastArrays |
| 22 | + |
| 23 | +> Broadcast [ndarrays][@stdlib/ndarray/base/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 maybeBroadcastArrays = require( '@stdlib/ndarray/base/maybe-broadcast-arrays' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### maybeBroadcastArrays( arrays ) |
| 44 | + |
| 45 | +Broadcasts [ndarrays][@stdlib/ndarray/base/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 = maybeBroadcastArrays( [ x, y ] ); |
| 61 | +// returns [ <ndarray>, <ndarray> ] |
| 62 | +``` |
| 63 | + |
| 64 | +</section> |
| 65 | + |
| 66 | +<!-- /.usage --> |
| 67 | + |
| 68 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 69 | + |
| 70 | +<section class="notes"> |
| 71 | + |
| 72 | +## Notes |
| 73 | + |
| 74 | +- The function throws an error if a provided [broadcast-incompatible][@stdlib/ndarray/base/broadcast-shapes] ndarrays. |
| 75 | +- If a provided [ndarray][@stdlib/ndarray/base/ctor] has a shape matching the common shape, the function returns the provided [ndarray][@stdlib/ndarray/base/ctor]. |
| 76 | +- If a provided [ndarray][@stdlib/ndarray/base/ctor] has a different (broadcast compatible) shape than the common shape, the function returns a new (base) [ndarray][@stdlib/ndarray/base/ctor] view of the provided [ndarray][@stdlib/ndarray/base/ctor]'s data. The view is 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/base/ctor], copy the [ndarray][@stdlib/ndarray/base/ctor] **before** performing operations which may mutate elements. |
| 77 | +- A returned [ndarray][@stdlib/ndarray/base/ctor] is a "base" [ndarray][@stdlib/ndarray/base/ctor], and, thus, a returned [ndarray][@stdlib/ndarray/base/ctor] view does not perform bounds checking or afford any of the guarantees of the non-base [ndarray][@stdlib/ndarray/ctor] constructor. The primary intent of this function is to broadcast ndarray-like objects within internal implementations and to do so with minimal overhead. |
| 78 | + |
| 79 | +</section> |
| 80 | + |
| 81 | +<!-- /.notes --> |
| 82 | + |
| 83 | +<!-- Package usage examples. --> |
| 84 | + |
| 85 | +<section class="examples"> |
| 86 | + |
| 87 | +## Examples |
| 88 | + |
| 89 | +<!-- eslint no-undef: "error" --> |
| 90 | + |
| 91 | +```javascript |
| 92 | +var array = require( '@stdlib/ndarray/array' ); |
| 93 | +var zeros = require( '@stdlib/ndarray/zeros' ); |
| 94 | +var numel = require( '@stdlib/ndarray/base/numel' ); |
| 95 | +var ind2sub = require( '@stdlib/ndarray/ind2sub' ); |
| 96 | +var maybeBroadcastArrays = require( '@stdlib/ndarray/base/maybe-broadcast-arrays' ); |
| 97 | + |
| 98 | +// Create a 2x2 array: |
| 99 | +var x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); |
| 100 | +// returns <ndarray> |
| 101 | + |
| 102 | +// Create a 3x2x2 array: |
| 103 | +var y = zeros( [ 3, 2, 2 ] ); |
| 104 | +// returns <ndarray> |
| 105 | + |
| 106 | +// Broadcast arrays to a common shape: |
| 107 | +var out = maybeBroadcastArrays( [ x, y ] ); |
| 108 | +// returns <ndarray> |
| 109 | + |
| 110 | +// Retrieve the common shape: |
| 111 | +var sh = out[ 0 ].shape; |
| 112 | +// returns [ 3, 2, 2 ] |
| 113 | + |
| 114 | +// Retrieve the number of elements: |
| 115 | +var N = numel( sh ); |
| 116 | + |
| 117 | +// Loop through the array elements... |
| 118 | +var i; |
| 119 | +for ( i = 0; i < N; i++ ) { |
| 120 | + console.log( 'X[%s] = %d', ind2sub( sh, i ).join( ', ' ), out[ 0 ].iget( i ) ); |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +</section> |
| 125 | + |
| 126 | +<!-- /.examples --> |
| 127 | + |
| 128 | +<!-- 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. --> |
| 129 | + |
| 130 | +<section class="references"> |
| 131 | + |
| 132 | +</section> |
| 133 | + |
| 134 | +<!-- /.references --> |
| 135 | + |
| 136 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 137 | + |
| 138 | +<section class="related"> |
| 139 | + |
| 140 | +</section> |
| 141 | + |
| 142 | +<!-- /.related --> |
| 143 | + |
| 144 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 145 | + |
| 146 | +<section class="links"> |
| 147 | + |
| 148 | +[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor |
| 149 | + |
| 150 | +[@stdlib/ndarray/base/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/ctor |
| 151 | + |
| 152 | +[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes |
| 153 | + |
| 154 | +</section> |
| 155 | + |
| 156 | +<!-- /.links --> |
0 commit comments