|
| 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 | +# unaryReduceStrided1d |
| 22 | + |
| 23 | +> Perform a reduction over a list of specified dimensions in an input ndarray via a one-dimensional strided array reduction function 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 unaryReduceStrided1d = require( '@stdlib/ndarray/base/unary-reduce-strided1d-to-struct' ); |
| 37 | +``` |
| 38 | + |
| 39 | +#### unaryReduceStrided1d( fcn, arrays, dims\[, options] ) |
| 40 | + |
| 41 | +Performs a reduction over a list of specified dimensions in an input ndarray via a one-dimensional strided array reduction function 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 ndarray2array = require( '@stdlib/ndarray/base/to-array' ); |
| 48 | +var Float64Results = require( '@stdlib/stats/base/ztest/one-sample/results/float64' ); |
| 49 | +var structFactory = require( '@stdlib/array/struct-factory' ); |
| 50 | +var ztest = require( '@stdlib/stats/base/ndarray/ztest' ); |
| 51 | + |
| 52 | +var ResultsArray = structFactory( Float64Results ); |
| 53 | + |
| 54 | +// Create data buffers: |
| 55 | +var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); |
| 56 | +var ybuf = new ResultsArray( 3 ); |
| 57 | + |
| 58 | +// Define the array shapes: |
| 59 | +var xsh = [ 1, 3, 2, 2 ]; |
| 60 | +var ysh = [ 1, 3 ]; |
| 61 | + |
| 62 | +// Define the array strides: |
| 63 | +var sx = [ 12, 4, 2, 1 ]; |
| 64 | +var sy = [ 3, 1 ]; |
| 65 | + |
| 66 | +// Define the index offsets: |
| 67 | +var ox = 0; |
| 68 | +var oy = 0; |
| 69 | + |
| 70 | +// Create an input ndarray-like object: |
| 71 | +var x = { |
| 72 | + 'dtype': 'float64', |
| 73 | + 'data': xbuf, |
| 74 | + 'shape': xsh, |
| 75 | + 'strides': sx, |
| 76 | + 'offset': ox, |
| 77 | + 'order': 'row-major' |
| 78 | +}; |
| 79 | + |
| 80 | +// Create an output ndarray-like object: |
| 81 | +var y = { |
| 82 | + 'dtype': Float64Results, |
| 83 | + 'data': ybuf, |
| 84 | + 'shape': ysh, |
| 85 | + 'strides': sy, |
| 86 | + 'offset': oy, |
| 87 | + 'order': 'row-major' |
| 88 | +}; |
| 89 | + |
| 90 | +// Create additional parameter ndarray-like objects: |
| 91 | +var alternative = { |
| 92 | + 'dtype': 'generic', |
| 93 | + 'data': [ 'two-sided' ], |
| 94 | + 'shape': ysh, |
| 95 | + 'strides': [ 0, 0 ], |
| 96 | + 'offset': 0, |
| 97 | + 'order': 'row-major' |
| 98 | +}; |
| 99 | +var alpha = { |
| 100 | + 'dtype': 'float64', |
| 101 | + 'data': [ 0.05 ], |
| 102 | + 'shape': ysh, |
| 103 | + 'strides': [ 0, 0 ], |
| 104 | + 'offset': 0, |
| 105 | + 'order': 'row-major' |
| 106 | +}; |
| 107 | +var mu = { |
| 108 | + 'dtype': 'float64', |
| 109 | + 'data': [ 0.0 ], |
| 110 | + 'shape': ysh, |
| 111 | + 'strides': [ 0, 0 ], |
| 112 | + 'offset': 0, |
| 113 | + 'order': 'row-major' |
| 114 | +}; |
| 115 | +var sigma = { |
| 116 | + 'dtype': 'float64', |
| 117 | + 'data': [ 1.0 ], |
| 118 | + 'shape': ysh, |
| 119 | + 'strides': [ 0, 0 ], |
| 120 | + 'offset': 0, |
| 121 | + 'order': 'row-major' |
| 122 | +}; |
| 123 | + |
| 124 | +// Perform a reduction: |
| 125 | +unaryReduceStrided1d( ztest, [ x, y, alternative, alpha, mu, sigma ], [ 2, 3 ] ); |
| 126 | + |
| 127 | +var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); |
| 128 | +// returns [ [ <Float64Results>, <Float64Results>, <Float64Results> ] ] |
| 129 | +``` |
| 130 | + |
| 131 | +The function accepts the following arguments: |
| 132 | + |
| 133 | +- **fcn**: function which will be applied to a one-dimensional subarray and should store reduction results in an output [`struct`][@stdlib/dstructs/struct] object. |
| 134 | +- **arrays**: array-like object containing one input ndarray and one output ndarray, followed by any additional ndarray arguments. |
| 135 | +- **dims**: list of dimensions over which to perform a reduction. |
| 136 | +- **options**: function options which are passed through to `fcn` (_optional_). |
| 137 | + |
| 138 | +Each provided ndarray should be an object with the following properties: |
| 139 | + |
| 140 | +- **dtype**: data type. |
| 141 | +- **data**: data buffer. |
| 142 | +- **shape**: dimensions. |
| 143 | +- **strides**: stride lengths. |
| 144 | +- **offset**: index offset. |
| 145 | +- **order**: specifies whether an ndarray is row-major (C-style) or column major (Fortran-style). |
| 146 | + |
| 147 | +#### TODO: document factory method |
| 148 | + |
| 149 | +</section> |
| 150 | + |
| 151 | +<!-- /.usage --> |
| 152 | + |
| 153 | +<section class="notes"> |
| 154 | + |
| 155 | +## Notes |
| 156 | + |
| 157 | +- 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. |
| 158 | + |
| 159 | +- The reduction function is expected to have the following signature: |
| 160 | + |
| 161 | + ```text |
| 162 | + fcn( arrays[, options] ) |
| 163 | + ``` |
| 164 | +
|
| 165 | + where |
| 166 | +
|
| 167 | + - **arrays**: array containing a one-dimensional subarray of the input ndarray, a zero-dimensional subarray of the output ndarray containing the output [`struct`][@stdlib/dstructs/struct] object, and any additional ndarray arguments as zero-dimensional ndarrays. |
| 168 | + - **options**: function options (_optional_). |
| 169 | +
|
| 170 | +- 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. |
| 171 | +
|
| 172 | +</section> |
| 173 | +
|
| 174 | +<!-- /.notes --> |
| 175 | +
|
| 176 | +<section class="examples"> |
| 177 | +
|
| 178 | +## Examples |
| 179 | +
|
| 180 | +<!-- eslint no-undef: "error" --> |
| 181 | +
|
| 182 | +```javascript |
| 183 | +var normal = require( '@stdlib/random/array/normal' ); |
| 184 | +var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); |
| 185 | +var Float64Results = require( '@stdlib/stats/base/ztest/one-sample/results/float64' ); |
| 186 | +var structFactory = require( '@stdlib/array/struct-factory' ); |
| 187 | +var ztest = require( '@stdlib/stats/base/ndarray/ztest' ); |
| 188 | +var unaryReduceStrided1d = require( '@stdlib/ndarray/base/unary-reduce-strided1d-to-struct' ); |
| 189 | +
|
| 190 | +var ResultsArray = structFactory( Float64Results ); |
| 191 | +
|
| 192 | +var N = 10; |
| 193 | +var x = { |
| 194 | + 'dtype': 'generic', |
| 195 | + 'data': normal( N, 0.0, 1.0, { |
| 196 | + 'dtype': 'generic' |
| 197 | + }), |
| 198 | + 'shape': [ 1, 5, 2 ], |
| 199 | + 'strides': [ 10, 2, 1 ], |
| 200 | + 'offset': 0, |
| 201 | + 'order': 'row-major' |
| 202 | +}; |
| 203 | +var y = { |
| 204 | + 'dtype': Float64Results, |
| 205 | + 'data': new ResultsArray( 2 ), |
| 206 | + 'shape': [ 1, 2 ], |
| 207 | + 'strides': [ 2, 1 ], |
| 208 | + 'offset': 0, |
| 209 | + 'order': 'row-major' |
| 210 | +}; |
| 211 | +var alternative = { |
| 212 | + 'dtype': 'generic', |
| 213 | + 'data': [ 'two-sided' ], |
| 214 | + 'shape': [ 1, 2 ], |
| 215 | + 'strides': [ 0, 0 ], |
| 216 | + 'offset': 0, |
| 217 | + 'order': 'row-major' |
| 218 | +}; |
| 219 | +var alpha = { |
| 220 | + 'dtype': 'generic', |
| 221 | + 'data': [ 0.05 ], |
| 222 | + 'shape': [ 1, 2 ], |
| 223 | + 'strides': [ 0, 0 ], |
| 224 | + 'offset': 0, |
| 225 | + 'order': 'row-major' |
| 226 | +}; |
| 227 | +var mu = { |
| 228 | + 'dtype': 'generic', |
| 229 | + 'data': [ 0.0 ], |
| 230 | + 'shape': [ 1, 2 ], |
| 231 | + 'strides': [ 0, 0 ], |
| 232 | + 'offset': 0, |
| 233 | + 'order': 'row-major' |
| 234 | +}; |
| 235 | +var sigma = { |
| 236 | + 'dtype': 'generic', |
| 237 | + 'data': [ 1.0 ], |
| 238 | + 'shape': [ 1, 2 ], |
| 239 | + 'strides': [ 0, 0 ], |
| 240 | + 'offset': 0, |
| 241 | + 'order': 'row-major' |
| 242 | +}; |
| 243 | +
|
| 244 | +unaryReduceStrided1d( ztest, [ x, y, alternative, alpha, mu, sigma ], [ 1 ] ); |
| 245 | +
|
| 246 | +console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) ); |
| 247 | +console.log( ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ) ); |
| 248 | +``` |
| 249 | + |
| 250 | +</section> |
| 251 | + |
| 252 | +<!-- /.examples --> |
| 253 | + |
| 254 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 255 | + |
| 256 | +<section class="related"> |
| 257 | + |
| 258 | +</section> |
| 259 | + |
| 260 | +<!-- /.related --> |
| 261 | + |
| 262 | +<section class="links"> |
| 263 | + |
| 264 | +[@stdlib/dstructs/struct]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/dstructs/struct |
| 265 | + |
| 266 | +</section> |
| 267 | + |
| 268 | +<!-- /.links --> |
0 commit comments