|
| 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 | +# unaryStrided1dDispatchFactory |
| 22 | + |
| 23 | +> Create a function for performing a reduction on an input ndarray. |
| 24 | +
|
| 25 | +<section class="usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +<!-- eslint-disable id-length --> |
| 30 | + |
| 31 | +```javascript |
| 32 | +var unaryStrided1dDispatchFactory = require( '@stdlib/ndarray/base/unary-reduce-strided1d-dispatch-factory' ); |
| 33 | +``` |
| 34 | + |
| 35 | +#### unaryStrided1dDispatchFactory( table, idtypes, odtypes, policy ) |
| 36 | + |
| 37 | +Returns a function for performing a reduction on an input ndarray. |
| 38 | + |
| 39 | +<!-- eslint-disable id-length --> |
| 40 | + |
| 41 | +```javascript |
| 42 | +var base = require( '@stdlib/stats/base/ndarray/max' ); |
| 43 | + |
| 44 | +var table = { |
| 45 | + 'default': base |
| 46 | +}; |
| 47 | + |
| 48 | +var dtypes = [ 'float64', 'float32', 'generic' ]; |
| 49 | +var policy = 'same'; |
| 50 | + |
| 51 | +var unary = unaryStrided1dDispatchFactory( table, [ dtypes ], dtypes, policy ); |
| 52 | +``` |
| 53 | + |
| 54 | +- **table**: strided reduction function dispatch table. Must have a `'default'` property and a corresponding strided reduction function. May have additional properties corresponding to specific data types and associated specialized strided reduction functions. |
| 55 | +- **idtypes**: list containing lists of supported input data types for each input ndarray argument. |
| 56 | +- **odtypes**: list of supported input data types. |
| 57 | +- **policy**: output data type policy. |
| 58 | + |
| 59 | +#### unary( x\[, ...args]\[, options] ) |
| 60 | + |
| 61 | +Performs a reduction on a provided input ndarray. |
| 62 | + |
| 63 | +<!-- eslint-disable id-length --> |
| 64 | + |
| 65 | +```javascript |
| 66 | +var ndarray = require( '@stdlib/ndarray/base/ctor' ); |
| 67 | +var base = require( '@stdlib/stats/base/ndarray/max' ); |
| 68 | + |
| 69 | +var table = { |
| 70 | + 'default': base |
| 71 | +}; |
| 72 | + |
| 73 | +var dtypes = [ 'float64', 'float32', 'generic' ]; |
| 74 | +var policy = 'same'; |
| 75 | + |
| 76 | +var unary = unaryStrided1dDispatchFactory( table, [ dtypes ], dtypes, policy ); |
| 77 | + |
| 78 | +var xbuf = [ -1.0, 2.0, -3.0 ]; |
| 79 | +var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); |
| 80 | + |
| 81 | +var y = unary( x ); |
| 82 | +// returns <ndarray> |
| 83 | + |
| 84 | +var v = y.get(); |
| 85 | +// returns 2.0 |
| 86 | +``` |
| 87 | + |
| 88 | +The function has the following parameters: |
| 89 | + |
| 90 | +- **x**: input ndarray. |
| 91 | +- **...args**: additional input ndarray arguments (_optional_). |
| 92 | +- **options**: function options (_optional_). |
| 93 | + |
| 94 | +The function accepts the following options: |
| 95 | + |
| 96 | +- **dims**: list of dimensions over which to perform a reduction. |
| 97 | +- **dtype**: output ndarray data type. Setting this option, overrides the output data type policy. |
| 98 | +- **keepdims**: boolean indicating whether the reduced dimensions should be included in the returned ndarray as singleton dimensions. Default: `false`. |
| 99 | + |
| 100 | +By default, the function returns an ndarray having a data type determined by the output data type policy. To override the default behavior, set the `dtype` option. |
| 101 | + |
| 102 | +<!-- eslint-disable id-length --> |
| 103 | + |
| 104 | +```javascript |
| 105 | +var ndarray = require( '@stdlib/ndarray/base/ctor' ); |
| 106 | +var base = require( '@stdlib/stats/base/ndarray/max' ); |
| 107 | +var getDType = require( '@stdlib/ndarray/dtype' ); |
| 108 | + |
| 109 | +var table = { |
| 110 | + 'default': base |
| 111 | +}; |
| 112 | + |
| 113 | +var dtypes = [ 'float64', 'float32', 'generic' ]; |
| 114 | +var policy = 'same'; |
| 115 | + |
| 116 | +var unary = unaryStrided1dDispatchFactory( table, [ dtypes ], dtypes, policy ); |
| 117 | + |
| 118 | +var xbuf = [ -1.0, 2.0, -3.0 ]; |
| 119 | +var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); |
| 120 | + |
| 121 | +var y = unary( x, { |
| 122 | + 'dtype': 'float64' |
| 123 | +}); |
| 124 | +// returns <ndarray> |
| 125 | + |
| 126 | +var dt = getDType( y ); |
| 127 | +// returns 'float64' |
| 128 | +``` |
| 129 | + |
| 130 | +#### unary.assign( x\[, ...args], out\[, options] ) |
| 131 | + |
| 132 | +Performs a reduction on a provided input ndarray and assigns results to a provided output ndarray. |
| 133 | + |
| 134 | +<!-- eslint-disable id-length --> |
| 135 | + |
| 136 | +```javascript |
| 137 | +var base = require( '@stdlib/stats/base/ndarray/max' ); |
| 138 | +var dtypes = require( '@stdlib/ndarray/dtypes' ); |
| 139 | +var ndarray = require( '@stdlib/ndarray/base/ctor' ); |
| 140 | + |
| 141 | +var idt = dtypes( 'real_and_generic' ); |
| 142 | +var odt = idt; |
| 143 | +var policy = 'same'; |
| 144 | + |
| 145 | +var table = { |
| 146 | + 'default': base |
| 147 | +}; |
| 148 | +var unary = unaryStrided1dDispatchFactory( table, [ idt ], odt, policy ); |
| 149 | + |
| 150 | +var xbuf = [ -1.0, 2.0, -3.0 ]; |
| 151 | +var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); |
| 152 | + |
| 153 | +var ybuf = [ 0.0 ]; |
| 154 | +var y = new ndarray( 'generic', ybuf, [], [ 0 ], 0, 'row-major' ); |
| 155 | + |
| 156 | +var out = unary.assign( x, y ); |
| 157 | +// returns <ndarray> |
| 158 | + |
| 159 | +var v = out.get(); |
| 160 | +// returns 2.0 |
| 161 | + |
| 162 | +var bool = ( out === y ); |
| 163 | +// returns true |
| 164 | +``` |
| 165 | + |
| 166 | +The method has the following parameters: |
| 167 | + |
| 168 | +- **x**: input ndarray. |
| 169 | +- **args**: additional input ndarray arguments (_optional_). |
| 170 | +- **out**: output ndarray. |
| 171 | +- **options**: function options (_optional_). |
| 172 | + |
| 173 | +The function accepts the following options: |
| 174 | + |
| 175 | +- **dims**: list of dimensions over which to perform a reduction. |
| 176 | + |
| 177 | +</section> |
| 178 | + |
| 179 | +<!-- /.usage --> |
| 180 | + |
| 181 | +<section class="notes"> |
| 182 | + |
| 183 | +## Notes |
| 184 | + |
| 185 | +- The output data type policy only applies to the function returned by `factory`. For the `assign` method, the output ndarray is allowed to have any data type. |
| 186 | + |
| 187 | +</section> |
| 188 | + |
| 189 | +<!-- /.notes --> |
| 190 | + |
| 191 | +<section class="examples"> |
| 192 | + |
| 193 | +## Examples |
| 194 | + |
| 195 | +<!-- eslint-disable id-length, max-len --> |
| 196 | + |
| 197 | +<!-- eslint no-undef: "error" --> |
| 198 | + |
| 199 | +```javascript |
| 200 | +var base = require( '@stdlib/stats/base/ndarray/max' ); |
| 201 | +var uniform = require( '@stdlib/random/array/uniform' ); |
| 202 | +var dtypes = require( '@stdlib/ndarray/dtypes' ); |
| 203 | +var dtype = require( '@stdlib/ndarray/dtype' ); |
| 204 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 205 | +var ndarray = require( '@stdlib/ndarray/ctor' ); |
| 206 | +var unaryStrided1dDispatchFactory = require( '@stdlib/ndarray/base/unary-reduce-strided1d-dispatch-factory' ); |
| 207 | + |
| 208 | +// Define the supported input and output data types: |
| 209 | +var idt = dtypes( 'real_and_generic' ); |
| 210 | +var odt = dtypes( 'real_and_generic' ); |
| 211 | + |
| 212 | +// Define the policy mapping an input data type to an output data type: |
| 213 | +var policy = 'same'; |
| 214 | + |
| 215 | +// Define a dispatch table: |
| 216 | +var table = { |
| 217 | + 'default': base |
| 218 | +}; |
| 219 | + |
| 220 | +// Create an interface for performing a reduction: |
| 221 | +var max = unaryStrided1dDispatchFactory( table, [ idt ], odt, policy ); |
| 222 | + |
| 223 | +// Generate an array of random numbers: |
| 224 | +var xbuf = uniform( 100, -1.0, 1.0, { |
| 225 | + 'dtype': 'generic' |
| 226 | +}); |
| 227 | + |
| 228 | +// Wrap in an ndarray: |
| 229 | +var x = new ndarray( 'generic', xbuf, [ 10, 10 ], [ 10, 1 ], 0, 'row-major' ); |
| 230 | + |
| 231 | +// Perform a reduction: |
| 232 | +var y = max( x, { |
| 233 | + 'dims': [ 0 ] |
| 234 | +}); |
| 235 | + |
| 236 | +// Resolve the output array data type: |
| 237 | +var dt = dtype( y ); |
| 238 | +console.log( dt ); |
| 239 | + |
| 240 | +// Print the results: |
| 241 | +console.log( ndarray2array( y ) ); |
| 242 | +``` |
| 243 | + |
| 244 | +</section> |
| 245 | + |
| 246 | +<!-- /.examples --> |
| 247 | + |
| 248 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 249 | + |
| 250 | +<section class="related"> |
| 251 | + |
| 252 | +</section> |
| 253 | + |
| 254 | +<!-- /.related --> |
| 255 | + |
| 256 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 257 | + |
| 258 | +<section class="links"> |
| 259 | + |
| 260 | +</section> |
| 261 | + |
| 262 | +<!-- /.links --> |
0 commit comments