|
| 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 | +# nullaryStrided1d |
| 22 | + |
| 23 | +> Apply a one-dimensional strided array function to a list of specified dimensions in an input 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 nullaryStrided1d = require( '@stdlib/ndarray/base/nullary-strided1d' ); |
| 37 | +``` |
| 38 | + |
| 39 | +#### nullaryStrided1d( fcn, arrays, dims\[, options] ) |
| 40 | + |
| 41 | +Applies a one-dimensional strided array function to a list of specified dimensions in an input ndarray. |
| 42 | + |
| 43 | +<!-- eslint-disable max-len --> |
| 44 | + |
| 45 | +```javascript |
| 46 | +var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); |
| 47 | +var getStride = require( '@stdlib/ndarray/base/stride' ); |
| 48 | +var getOffset = require( '@stdlib/ndarray/base/offset' ); |
| 49 | +var getData = require( '@stdlib/ndarray/base/data-buffer' ); |
| 50 | +var numelDimension = require( '@stdlib/ndarray/base/numel-dimension' ); |
| 51 | +var ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' ); |
| 52 | +var gsorthp = require( '@stdlib/blas/ext/base/gsorthp' ).ndarray; |
| 53 | + |
| 54 | +function wrapper( arrays ) { |
| 55 | + var x = arrays[ 0 ]; |
| 56 | + var o = arrays[ 1 ]; |
| 57 | + return gsorthp( numelDimension( x, 0 ), ndarraylike2scalar( o ), getData( x ), getStride( x, 0 ), getOffset( x ) ); |
| 58 | +} |
| 59 | + |
| 60 | +// Create data buffers: |
| 61 | +var xbuf = [ 12.0, 11.0, 10.0, 9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0 ]; |
| 62 | + |
| 63 | +// Define the array shapes: |
| 64 | +var xsh = [ 1, 3, 2, 2 ]; |
| 65 | + |
| 66 | +// Define the array strides: |
| 67 | +var sx = [ 12, 4, 2, 1 ]; |
| 68 | + |
| 69 | +// Define the index offsets: |
| 70 | +var ox = 0; |
| 71 | + |
| 72 | +// Create an input ndarray-like object: |
| 73 | +var x = { |
| 74 | + 'dtype': 'generic', |
| 75 | + 'data': xbuf, |
| 76 | + 'shape': xsh, |
| 77 | + 'strides': sx, |
| 78 | + 'offset': ox, |
| 79 | + 'order': 'row-major' |
| 80 | +}; |
| 81 | + |
| 82 | +// Create an ndarray-like object for the sort order: |
| 83 | +var sortOrder = { |
| 84 | + 'dtype': 'generic', |
| 85 | + 'data': [ 1.0 ], |
| 86 | + 'shape': [ 1, 3 ], |
| 87 | + 'strides': [ 0, 0 ], |
| 88 | + 'offset': 0, |
| 89 | + 'order': 'row-major' |
| 90 | +}; |
| 91 | + |
| 92 | +// Apply strided function: |
| 93 | +nullaryStrided1d( wrapper, [ x, sortOrder ], [ 2, 3 ] ); |
| 94 | + |
| 95 | +var arr = ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ); |
| 96 | +// returns [ [ [ [ 9.0, 10.0 ], [ 11.0, 12.0 ] ], [ [ 5.0, 6.0 ], [ 7.0, 8.0 ] ], [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ] ] |
| 97 | +``` |
| 98 | + |
| 99 | +The function accepts the following arguments: |
| 100 | + |
| 101 | +- **fcn**: function which will be applied to a one-dimensional input subarray. |
| 102 | +- **arrays**: array-like object containing one input ndarray followed by any additional ndarray arguments. |
| 103 | +- **dims**: list of dimensions to which to apply a strided array function. |
| 104 | +- **options**: function options which are passed through to `fcn` (_optional_). |
| 105 | + |
| 106 | +Each provided ndarray should be an object with the following properties: |
| 107 | + |
| 108 | +- **dtype**: data type. |
| 109 | +- **data**: data buffer. |
| 110 | +- **shape**: dimensions. |
| 111 | +- **strides**: stride lengths. |
| 112 | +- **offset**: index offset. |
| 113 | +- **order**: specifies whether an ndarray is row-major (C-style) or column major (Fortran-style). |
| 114 | + |
| 115 | +#### TODO: document factory method |
| 116 | + |
| 117 | +</section> |
| 118 | + |
| 119 | +<!-- /.usage --> |
| 120 | + |
| 121 | +<section class="notes"> |
| 122 | + |
| 123 | +## Notes |
| 124 | + |
| 125 | +- Any additional ndarray arguments are expected to have the same dimensions as the loop dimensions of the input ndarray. When calling the strided array function, any additional ndarray arguments are provided as zero-dimensional ndarray-like objects. |
| 126 | + |
| 127 | +- The strided array function is expected to have the following signature: |
| 128 | + |
| 129 | + ```text |
| 130 | + fcn( arrays[, options] ) |
| 131 | + ``` |
| 132 | +
|
| 133 | + where |
| 134 | +
|
| 135 | + - **arrays**: array containing a one-dimensional subarray of the input ndarray and any additional ndarray arguments as zero-dimensional ndarrays. |
| 136 | + - **options**: function options (_optional_). |
| 137 | +
|
| 138 | +- The function iterates over ndarray elements according to the memory layout of the input ndarray. |
| 139 | +
|
| 140 | +- For very high-dimensional ndarrays which are non-contiguous, one should consider copying the underlying data to contiguous memory before performing an operation in order to achieve better performance. |
| 141 | +
|
| 142 | +</section> |
| 143 | +
|
| 144 | +<!-- /.notes --> |
| 145 | +
|
| 146 | +<section class="examples"> |
| 147 | +
|
| 148 | +## Examples |
| 149 | +
|
| 150 | +<!-- eslint-disable max-len --> |
| 151 | +
|
| 152 | +<!-- eslint no-undef: "error" --> |
| 153 | +
|
| 154 | +```javascript |
| 155 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 156 | +var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); |
| 157 | +var numelDimension = require( '@stdlib/ndarray/base/numel-dimension' ); |
| 158 | +var getData = require( '@stdlib/ndarray/base/data-buffer' ); |
| 159 | +var getStride = require( '@stdlib/ndarray/base/stride' ); |
| 160 | +var getOffset = require( '@stdlib/ndarray/base/offset' ); |
| 161 | +var ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' ); |
| 162 | +var gsorthp = require( '@stdlib/blas/ext/base/gsorthp' ).ndarray; |
| 163 | +var nullaryStrided1d = require( '@stdlib/ndarray/base/nullary-strided1d' ); |
| 164 | +
|
| 165 | +function wrapper( arrays ) { |
| 166 | + var x = arrays[ 0 ]; |
| 167 | + var o = arrays[ 1 ]; |
| 168 | + return gsorthp( numelDimension( x, 0 ), ndarraylike2scalar( o ), getData( x ), getStride( x, 0 ), getOffset( x ) ); |
| 169 | +} |
| 170 | +
|
| 171 | +var N = 10; |
| 172 | +var x = { |
| 173 | + 'dtype': 'generic', |
| 174 | + 'data': discreteUniform( N, -5, 5, { |
| 175 | + 'dtype': 'generic' |
| 176 | + }), |
| 177 | + 'shape': [ 1, 5, 2 ], |
| 178 | + 'strides': [ 10, 2, 1 ], |
| 179 | + 'offset': 0, |
| 180 | + 'order': 'row-major' |
| 181 | +}; |
| 182 | +var sortOrder = { |
| 183 | + 'dtype': 'generic', |
| 184 | + 'data': [ 0.0 ], |
| 185 | + 'shape': [ 1, 2 ], |
| 186 | + 'strides': [ 0, 0 ], |
| 187 | + 'offset': 0, |
| 188 | + 'order': 'row-major' |
| 189 | +}; |
| 190 | +
|
| 191 | +nullaryStrided1d( wrapper, [ x, sortOrder ], [ 1 ] ); |
| 192 | +
|
| 193 | +console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) ); |
| 194 | +``` |
| 195 | + |
| 196 | +</section> |
| 197 | + |
| 198 | +<!-- /.examples --> |
| 199 | + |
| 200 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 201 | + |
| 202 | +<section class="related"> |
| 203 | + |
| 204 | +</section> |
| 205 | + |
| 206 | +<!-- /.related --> |
| 207 | + |
| 208 | +<section class="links"> |
| 209 | + |
| 210 | +</section> |
| 211 | + |
| 212 | +<!-- /.links --> |
0 commit comments