|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2025 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 | +# Binary |
| 22 | + |
| 23 | +> Apply a binary callback to elements in input ndarrays and assign results to elements in an output ndarray. |
| 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 binary = require( '@stdlib/ndarray/base/binary' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### binary( arrays, fcn ) |
| 44 | + |
| 45 | +Applies a binary callback to elements in input ndarrays and assigns results to elements in an output ndarray. |
| 46 | + |
| 47 | +```javascript |
| 48 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 49 | +var add = require( '@stdlib/number/float64/base/add' ); |
| 50 | + |
| 51 | +// Create data buffers: |
| 52 | +var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); |
| 53 | +var ybuf = new Float64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] ); |
| 54 | +var zbuf = new Float64Array( 6 ); |
| 55 | + |
| 56 | +// Define the shape of the input and output arrays: |
| 57 | +var shape = [ 3, 1, 2 ]; |
| 58 | + |
| 59 | +// Define the array strides: |
| 60 | +var sx = [ 2, 2, 1 ]; |
| 61 | +var sy = [ 2, 2, 1 ]; |
| 62 | +var sz = [ 2, 2, 1 ]; |
| 63 | + |
| 64 | +// Define the index offsets: |
| 65 | +var ox = 0; |
| 66 | +var oy = 0; |
| 67 | +var oz = 0; |
| 68 | + |
| 69 | +// Create the input and output ndarray-like objects: |
| 70 | +var x = { |
| 71 | + 'dtype': 'float64', |
| 72 | + 'data': xbuf, |
| 73 | + 'shape': shape, |
| 74 | + 'strides': sx, |
| 75 | + 'offset': ox, |
| 76 | + 'order': 'row-major' |
| 77 | +}; |
| 78 | +var y = { |
| 79 | + 'dtype': 'float64', |
| 80 | + 'data': ybuf, |
| 81 | + 'shape': shape, |
| 82 | + 'strides': sy, |
| 83 | + 'offset': oy, |
| 84 | + 'order': 'row-major' |
| 85 | +}; |
| 86 | +var z = { |
| 87 | + 'dtype': 'float64', |
| 88 | + 'data': zbuf, |
| 89 | + 'shape': shape, |
| 90 | + 'strides': sz, |
| 91 | + 'offset': oz, |
| 92 | + 'order': 'row-major' |
| 93 | +}; |
| 94 | + |
| 95 | +// Apply the binary function: |
| 96 | +binary( [ x, y, z ], add ); |
| 97 | + |
| 98 | +console.log( z.data ); |
| 99 | +// => <Float64Array>[ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 ] |
| 100 | +``` |
| 101 | + |
| 102 | +The function accepts the following arguments: |
| 103 | + |
| 104 | +- **arrays**: array-like object containing two input ndarrays and one output ndarray. |
| 105 | +- **fcn**: binary function to apply. |
| 106 | + |
| 107 | +Each provided ndarray should be an object with the following properties: |
| 108 | + |
| 109 | +- **dtype**: data type. |
| 110 | +- **data**: data buffer. |
| 111 | +- **shape**: dimensions. |
| 112 | +- **strides**: stride lengths. |
| 113 | +- **offset**: index offset. |
| 114 | +- **order**: specifies whether an ndarray is row-major (C-style) or column major (Fortran-style). |
| 115 | + |
| 116 | +</section> |
| 117 | + |
| 118 | +<!-- /.usage --> |
| 119 | + |
| 120 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 121 | + |
| 122 | +<section class="notes"> |
| 123 | + |
| 124 | +## Notes |
| 125 | + |
| 126 | +- For very high-dimensional ndarrays which are non-contiguous, one should consider copying the underlying data to contiguous memory before applying a binary function in order to achieve better performance. |
| 127 | + |
| 128 | +</section> |
| 129 | + |
| 130 | +<!-- /.notes --> |
| 131 | + |
| 132 | +<!-- Package usage examples. --> |
| 133 | + |
| 134 | +<section class="examples"> |
| 135 | + |
| 136 | +## Examples |
| 137 | + |
| 138 | +<!-- eslint no-undef: "error" --> |
| 139 | + |
| 140 | +```javascript |
| 141 | +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory; |
| 142 | +var filledarray = require( '@stdlib/array/filled' ); |
| 143 | +var filledarrayBy = require( '@stdlib/array/filled-by' ); |
| 144 | +var add = require( '@stdlib/number/float64/base/add' ); |
| 145 | +var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); |
| 146 | +var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); |
| 147 | +var binary = require( '@stdlib/ndarray/base/binary' ); |
| 148 | + |
| 149 | +var N = 10; |
| 150 | +var x = { |
| 151 | + 'dtype': 'generic', |
| 152 | + 'data': filledarrayBy( N, 'generic', discreteUniform( -100, 100 ) ), |
| 153 | + 'shape': [ 5, 2 ], |
| 154 | + 'strides': [ 2, 1 ], |
| 155 | + 'offset': 0, |
| 156 | + 'order': 'row-major' |
| 157 | +}; |
| 158 | +console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) ); |
| 159 | + |
| 160 | +var y = { |
| 161 | + 'dtype': 'generic', |
| 162 | + 'data': filledarrayBy( N, 'generic', discreteUniform( -100, 100 ) ), |
| 163 | + 'shape': x.shape.slice(), |
| 164 | + 'strides': shape2strides( x.shape, 'column-major' ), |
| 165 | + 'offset': 0, |
| 166 | + 'order': 'column-major' |
| 167 | +}; |
| 168 | +console.log( ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ) ); |
| 169 | + |
| 170 | +var z = { |
| 171 | + 'dtype': 'generic', |
| 172 | + 'data': filledarray( 0, N, 'generic' ), |
| 173 | + 'shape': x.shape.slice(), |
| 174 | + 'strides': shape2strides( x.shape, 'column-major' ), |
| 175 | + 'offset': 0, |
| 176 | + 'order': 'column-major' |
| 177 | +}; |
| 178 | + |
| 179 | +binary( [ x, y, z ], add ); |
| 180 | +console.log( ndarray2array( z.data, z.shape, z.strides, z.offset, z.order ) ); |
| 181 | +``` |
| 182 | + |
| 183 | +</section> |
| 184 | + |
| 185 | +<!-- /.examples --> |
| 186 | + |
| 187 | +<!-- 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. --> |
| 188 | + |
| 189 | +<section class="references"> |
| 190 | + |
| 191 | +</section> |
| 192 | + |
| 193 | +<!-- /.references --> |
| 194 | + |
| 195 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 196 | + |
| 197 | +<section class="related"> |
| 198 | + |
| 199 | +</section> |
| 200 | + |
| 201 | +<!-- /.related --> |
| 202 | + |
| 203 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 204 | + |
| 205 | +<section class="links"> |
| 206 | + |
| 207 | +</section> |
| 208 | + |
| 209 | +<!-- /.links --> |
0 commit comments