|
| 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 | +# unaryReduceSubarray |
| 22 | + |
| 23 | +> Perform a reduction over a list of specified dimensions in an input ndarray and assign results to a provided 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 unaryReduceSubarray = require( '@stdlib/ndarray/base/unary-reduce-subarray' ); |
| 37 | +``` |
| 38 | + |
| 39 | +#### unaryReduceSubarray( fcn, arrays, dims\[, options] ) |
| 40 | + |
| 41 | +Performs a reduction over a list of specified dimensions in an input ndarray and assigns results to a provided output ndarray. |
| 42 | + |
| 43 | +<!-- eslint-disable max-len --> |
| 44 | + |
| 45 | +```javascript |
| 46 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 47 | +var filled = require( '@stdlib/array/base/filled' ); |
| 48 | +var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); |
| 49 | +var every = require( '@stdlib/ndarray/base/every' ); |
| 50 | + |
| 51 | +// Create data buffers: |
| 52 | +var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 0.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); |
| 53 | +var ybuf = filled( false, 3 ); |
| 54 | + |
| 55 | +// Define the array shapes: |
| 56 | +var xsh = [ 1, 3, 2, 2 ]; |
| 57 | +var ysh = [ 1, 3 ]; |
| 58 | + |
| 59 | +// Define the array strides: |
| 60 | +var sx = [ 12, 4, 2, 1 ]; |
| 61 | +var sy = [ 3, 1 ]; |
| 62 | + |
| 63 | +// Define the index offsets: |
| 64 | +var ox = 0; |
| 65 | +var oy = 0; |
| 66 | + |
| 67 | +// Create an input ndarray-like object: |
| 68 | +var x = { |
| 69 | + 'dtype': 'float64', |
| 70 | + 'data': xbuf, |
| 71 | + 'shape': xsh, |
| 72 | + 'strides': sx, |
| 73 | + 'offset': ox, |
| 74 | + 'order': 'row-major' |
| 75 | +}; |
| 76 | + |
| 77 | +// Create an output ndarray-like object: |
| 78 | +var y = { |
| 79 | + 'dtype': 'generic', |
| 80 | + 'data': ybuf, |
| 81 | + 'shape': ysh, |
| 82 | + 'strides': sy, |
| 83 | + 'offset': oy, |
| 84 | + 'order': 'row-major' |
| 85 | +}; |
| 86 | + |
| 87 | +// Perform a reduction: |
| 88 | +unaryReduceSubarray( every, [ x, y ], [ 2, 3 ] ); |
| 89 | + |
| 90 | +var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); |
| 91 | +// returns [ [ true, false, true ] ] |
| 92 | +``` |
| 93 | + |
| 94 | +The function accepts the following arguments: |
| 95 | + |
| 96 | +- **fcn**: function which will be applied to a subarray and should reduce the subarray to a single scalar value. |
| 97 | +- **arrays**: array-like object containing one input ndarray and one output ndarray, followed by any additional ndarray arguments. |
| 98 | +- **dims**: list of dimensions over which to perform a reduction. |
| 99 | +- **options**: function options which are passed through to `fcn` (_optional_). |
| 100 | + |
| 101 | +Each provided ndarray should be an object with the following properties: |
| 102 | + |
| 103 | +- **dtype**: data type. |
| 104 | +- **data**: data buffer. |
| 105 | +- **shape**: dimensions. |
| 106 | +- **strides**: stride lengths. |
| 107 | +- **offset**: index offset. |
| 108 | +- **order**: specifies whether an ndarray is row-major (C-style) or column major (Fortran-style). |
| 109 | + |
| 110 | +</section> |
| 111 | + |
| 112 | +<!-- /.usage --> |
| 113 | + |
| 114 | +<section class="notes"> |
| 115 | + |
| 116 | +## Notes |
| 117 | + |
| 118 | +- The output ndarray and any additional ndarray arguments are expected to have the same dimensions as the non-reduced dimensions of the input ndarray. When calling the reduction function, any additional ndarray arguments are provided as zero-dimensional ndarray-like objects. |
| 119 | + |
| 120 | +- The reduction function is expected to have the following signature: |
| 121 | + |
| 122 | + ```text |
| 123 | + fcn( arrays[, options] ) |
| 124 | + ``` |
| 125 | +
|
| 126 | + where |
| 127 | +
|
| 128 | + - **arrays**: array containing a subarray from the input ndarray and any additional ndarray arguments as zero-dimensional ndarrays. |
| 129 | + - **options**: function options (_optional_). |
| 130 | +
|
| 131 | +- For very high-dimensional ndarrays which are non-contiguous, one should consider copying the underlying data to contiguous memory before performing a reduction in order to achieve better performance. |
| 132 | +
|
| 133 | +</section> |
| 134 | +
|
| 135 | +<!-- /.notes --> |
| 136 | +
|
| 137 | +<section class="examples"> |
| 138 | +
|
| 139 | +## Examples |
| 140 | +
|
| 141 | +<!-- eslint no-undef: "error" --> |
| 142 | +
|
| 143 | +```javascript |
| 144 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 145 | +var filled = require( '@stdlib/array/base/filled' ); |
| 146 | +var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); |
| 147 | +var every = require( '@stdlib/ndarray/base/every' ); |
| 148 | +var unaryReduceSubarray = require( '@stdlib/ndarray/base/unary-reduce-subarray' ); |
| 149 | +
|
| 150 | +var N = 10; |
| 151 | +var x = { |
| 152 | + 'dtype': 'generic', |
| 153 | + 'data': discreteUniform( N, -5, 5, { |
| 154 | + 'dtype': 'generic' |
| 155 | + }), |
| 156 | + 'shape': [ 1, 5, 2 ], |
| 157 | + 'strides': [ 10, 2, 1 ], |
| 158 | + 'offset': 0, |
| 159 | + 'order': 'row-major' |
| 160 | +}; |
| 161 | +var y = { |
| 162 | + 'dtype': 'generic', |
| 163 | + 'data': filled( false, 2 ), |
| 164 | + 'shape': [ 1, 2 ], |
| 165 | + 'strides': [ 2, 1 ], |
| 166 | + 'offset': 0, |
| 167 | + 'order': 'row-major' |
| 168 | +}; |
| 169 | +
|
| 170 | +unaryReduceSubarray( every, [ x, y ], [ 1 ] ); |
| 171 | +
|
| 172 | +console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) ); |
| 173 | +console.log( ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ) ); |
| 174 | +``` |
| 175 | + |
| 176 | +</section> |
| 177 | + |
| 178 | +<!-- /.examples --> |
| 179 | + |
| 180 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 181 | + |
| 182 | +<section class="related"> |
| 183 | + |
| 184 | +</section> |
| 185 | + |
| 186 | +<!-- /.related --> |
| 187 | + |
| 188 | +<section class="links"> |
| 189 | + |
| 190 | +</section> |
| 191 | + |
| 192 | +<!-- /.links --> |
0 commit comments