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