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