|
| 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 --> |
0 commit comments