|
| 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 | +# nullaryStrided1dDispatchFactory |
| 22 | + |
| 23 | +> Create a function for applying a strided function to an output ndarray. |
| 24 | +
|
| 25 | +<section class="usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +<!-- eslint-disable id-length --> |
| 30 | + |
| 31 | +```javascript |
| 32 | +var nullaryStrided1dDispatchFactory = require( '@stdlib/ndarray/base/nullary-strided1d-dispatch-factory' ); |
| 33 | +``` |
| 34 | + |
| 35 | +#### nullaryStrided1dDispatchFactory( table, odtypes, policies\[, options] ) |
| 36 | + |
| 37 | +Returns a function for applying a strided function to an output ndarray. |
| 38 | + |
| 39 | +<!-- eslint-disable id-length --> |
| 40 | + |
| 41 | +```javascript |
| 42 | +var base = require( '@stdlib/blas/ext/base/ndarray/gsorthp' ); |
| 43 | + |
| 44 | +var table = { |
| 45 | + 'default': base |
| 46 | +}; |
| 47 | + |
| 48 | +var dtypes = [ 'float64', 'float32', 'generic' ]; |
| 49 | +var policies = { |
| 50 | + 'output': 'same' |
| 51 | +}; |
| 52 | + |
| 53 | +var nullary = nullaryStrided1dDispatchFactory( table, [ dtypes ], policies ); |
| 54 | +``` |
| 55 | + |
| 56 | +The function has the following parameters: |
| 57 | + |
| 58 | +- **table**: strided function dispatch table. Must have the following properties: |
| 59 | + |
| 60 | + - **default**: default strided function which should be invoked when provided ndarrays have data types which do not have a corresponding specialized implementation. |
| 61 | + |
| 62 | + A dispatch table may have the following additional properties: |
| 63 | + |
| 64 | + - **types**: one-dimensional list of ndarray data types describing specialized output ndarray argument signatures. Only the output ndarray argument data types should be specified. Additional ndarray argument data types should be omitted and are not considered during dispatch. The length of `types` must equal the number of strided functions specified by `fcns`. |
| 65 | + - **fcns**: list of strided functions which are specific to specialized output ndarray argument signatures. |
| 66 | + |
| 67 | +- **odtypes**: list containing lists of supported output data types for each input ndarray argument. |
| 68 | + |
| 69 | +- **policies**: dispatch policies. Must have the following properties: |
| 70 | + |
| 71 | + - **output**: output data type [policy][@stdlib/ndarray/output-dtype-policies]. |
| 72 | + |
| 73 | +- **options**: function options (_optional_). |
| 74 | + |
| 75 | +The function supports the following options: |
| 76 | + |
| 77 | +- **strictTraversalOrder**: boolean specifying whether the order of element traversal must match the memory layout order of an input ndarray. Default: `false`. |
| 78 | + |
| 79 | +#### unary.assign( x\[, ...args]\[, options] ) |
| 80 | + |
| 81 | +Applies a strided function and assigns results to a provided output ndarray. |
| 82 | + |
| 83 | +<!-- eslint-disable id-length --> |
| 84 | + |
| 85 | +```javascript |
| 86 | +var base = require( '@stdlib/blas/ext/base/ndarray/gsorthp' ); |
| 87 | +var dtypes = require( '@stdlib/ndarray/dtypes' ); |
| 88 | +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); |
| 89 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 90 | +var ndarray = require( '@stdlib/ndarray/base/ctor' ); |
| 91 | + |
| 92 | +var odt = dtypes( 'real_and_generic' ); |
| 93 | +var policies = { |
| 94 | + 'output': 'same' |
| 95 | +}; |
| 96 | + |
| 97 | +var table = { |
| 98 | + 'default': base |
| 99 | +}; |
| 100 | +var nullary = nullaryStrided1dDispatchFactory( table, [ odt ], policies ); |
| 101 | + |
| 102 | +var xbuf = [ -1.0, 2.0, -3.0 ]; |
| 103 | +var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); |
| 104 | + |
| 105 | +var o = scalar2ndarray( 1.0, { |
| 106 | + 'dtype': 'generic' |
| 107 | +}); |
| 108 | + |
| 109 | +var out = nullary.assign( x, o ); |
| 110 | +// returns <ndarray> |
| 111 | + |
| 112 | +var arr = ndarray2array( out ); |
| 113 | +// returns [ -3.0, -1.0, 2.0 ] |
| 114 | + |
| 115 | +var bool = ( out === x ); |
| 116 | +// returns true |
| 117 | +``` |
| 118 | + |
| 119 | +The method has the following parameters: |
| 120 | + |
| 121 | +- **x**: output ndarray. |
| 122 | +- **args**: additional output ndarray arguments (_optional_). |
| 123 | +- **options**: function options (_optional_). |
| 124 | + |
| 125 | +The method accepts the following options: |
| 126 | + |
| 127 | +- **dims**: list of dimensions over which to perform operation. |
| 128 | + |
| 129 | +</section> |
| 130 | + |
| 131 | +<!-- /.usage --> |
| 132 | + |
| 133 | +<section class="notes"> |
| 134 | + |
| 135 | +## Notes |
| 136 | + |
| 137 | +- A strided function should have the following signature: |
| 138 | + |
| 139 | + ```text |
| 140 | + f( arrays ) |
| 141 | + ``` |
| 142 | +
|
| 143 | + where |
| 144 | +
|
| 145 | + - **arrays**: array containing an output ndarray, followed by any additional ndarray arguments. |
| 146 | +
|
| 147 | +- The output data type policy only applies to the function returned by the main function. For the `assign` method, the output ndarray is allowed to have any supported output data type. |
| 148 | +
|
| 149 | +</section> |
| 150 | +
|
| 151 | +<!-- /.notes --> |
| 152 | +
|
| 153 | +<section class="examples"> |
| 154 | +
|
| 155 | +## Examples |
| 156 | +
|
| 157 | +<!-- eslint-disable id-length, max-len, array-element-newline --> |
| 158 | +
|
| 159 | +<!-- eslint no-undef: "error" --> |
| 160 | +
|
| 161 | +```javascript |
| 162 | +var dsorthp = require( '@stdlib/blas/ext/base/ndarray/dsorthp' ); |
| 163 | +var ssorthp = require( '@stdlib/blas/ext/base/ndarray/ssorthp' ); |
| 164 | +var base = require( '@stdlib/blas/ext/base/ndarray/gsorthp' ); |
| 165 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 166 | +var dtypes = require( '@stdlib/ndarray/dtypes' ); |
| 167 | +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); |
| 168 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 169 | +var ndarray = require( '@stdlib/ndarray/ctor' ); |
| 170 | +var nullaryStrided1dDispatchFactory = require( '@stdlib/ndarray/base/nullary-strided1d-dispatch-factory' ); |
| 171 | +
|
| 172 | +// Define the supported output data types: |
| 173 | +var odt = dtypes( 'all' ); |
| 174 | +
|
| 175 | +// Define dispatch policies: |
| 176 | +var policies = { |
| 177 | + 'output': 'same' |
| 178 | +}; |
| 179 | +
|
| 180 | +// Define a dispatch table: |
| 181 | +var table = { |
| 182 | + 'types': [ |
| 183 | + 'float64', |
| 184 | + 'float32' |
| 185 | + ], |
| 186 | + 'fcns': [ |
| 187 | + dsorthp, |
| 188 | + ssorthp |
| 189 | + ], |
| 190 | + 'default': base |
| 191 | +}; |
| 192 | +
|
| 193 | +// Create an interface for performing an operation: |
| 194 | +var sorthp = nullaryStrided1dDispatchFactory( table, [ odt ], policies ); |
| 195 | +
|
| 196 | +// Generate an array of random numbers: |
| 197 | +var xbuf = discreteUniform( 25, -10, 10, { |
| 198 | + 'dtype': 'generic' |
| 199 | +}); |
| 200 | +
|
| 201 | +// Wrap in an ndarray: |
| 202 | +var x = new ndarray( 'generic', xbuf, [ 5, 5 ], [ 5, 1 ], 0, 'row-major' ); |
| 203 | +console.log( ndarray2array( x ) ); |
| 204 | +
|
| 205 | +var o = scalar2ndarray( 1.0, { |
| 206 | + 'dtype': 'generic' |
| 207 | +}); |
| 208 | +
|
| 209 | +// Perform operation: |
| 210 | +sorthp.assign( x, o, { |
| 211 | + 'dims': [ 0 ] |
| 212 | +}); |
| 213 | +
|
| 214 | +// Print the results: |
| 215 | +console.log( ndarray2array( x ) ); |
| 216 | +``` |
| 217 | + |
| 218 | +</section> |
| 219 | + |
| 220 | +<!-- /.examples --> |
| 221 | + |
| 222 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 223 | + |
| 224 | +<section class="related"> |
| 225 | + |
| 226 | +</section> |
| 227 | + |
| 228 | +<!-- /.related --> |
| 229 | + |
| 230 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 231 | + |
| 232 | +<section class="links"> |
| 233 | + |
| 234 | +[@stdlib/ndarray/output-dtype-policies]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/output-dtype-policies |
| 235 | + |
| 236 | +</section> |
| 237 | + |
| 238 | +<!-- /.links --> |
0 commit comments