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