|
| 1 | +/** |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2025 The Stdlib Authors. |
| 5 | +* |
| 6 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +* you may not use this file except in compliance with the License. |
| 8 | +* You may obtain a copy of the License at |
| 9 | +* |
| 10 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +* |
| 12 | +* Unless required by applicable law or agreed to in writing, software |
| 13 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +* See the License for the specific language governing permissions and |
| 16 | +* limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +/* eslint-disable max-depth, max-len, max-statements, max-lines-per-function */ |
| 20 | + |
| 21 | +'use strict'; |
| 22 | + |
| 23 | +// MODULES // |
| 24 | + |
| 25 | +var isRowMajor = require( '@stdlib/ndarray/base/assert/is-row-major-string' ); |
| 26 | + |
| 27 | + |
| 28 | +// MAIN // |
| 29 | + |
| 30 | +/** |
| 31 | +* Applies a binary callback to elements in ten-dimensional input ndarrays and assigns results to elements in an equivalently shaped output ndarray. |
| 32 | +* |
| 33 | +* @private |
| 34 | +* @param {Object} x - object containing input ndarray meta data |
| 35 | +* @param {string} x.dtype - data type |
| 36 | +* @param {Collection} x.data - data buffer |
| 37 | +* @param {NonNegativeIntegerArray} x.shape - dimensions |
| 38 | +* @param {IntegerArray} x.strides - stride lengths |
| 39 | +* @param {NonNegativeInteger} x.offset - index offset |
| 40 | +* @param {string} x.order - specifies whether `x` is row-major (C-style) or column-major (Fortran-style) |
| 41 | +* @param {Array<Function>} x.accessors - data buffer accessors |
| 42 | +* @param {Object} y - object containing input ndarray meta data |
| 43 | +* @param {string} y.dtype - data type |
| 44 | +* @param {Collection} y.data - data buffer |
| 45 | +* @param {NonNegativeIntegerArray} y.shape - dimensions |
| 46 | +* @param {IntegerArray} y.strides - stride lengths |
| 47 | +* @param {NonNegativeInteger} y.offset - index offset |
| 48 | +* @param {string} y.order - specifies whether `y` is row-major (C-style) or column-major (Fortran-style) |
| 49 | +* @param {Array<Function>} y.accessors - data buffer accessors |
| 50 | +* @param {Object} z - object containing output ndarray meta data |
| 51 | +* @param {string} z.dtype - data type |
| 52 | +* @param {Collection} z.data - data buffer |
| 53 | +* @param {NonNegativeIntegerArray} z.shape - dimensions |
| 54 | +* @param {IntegerArray} z.strides - stride lengths |
| 55 | +* @param {NonNegativeInteger} z.offset - index offset |
| 56 | +* @param {string} z.order - specifies whether `z` is row-major (C-style) or column-major (Fortran-style) |
| 57 | +* @param {Array<Function>} z.accessors - data buffer accessors |
| 58 | +* @param {Callback} fcn - binary callback |
| 59 | +* @returns {void} |
| 60 | +* |
| 61 | +* @example |
| 62 | +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); |
| 63 | +* var accessors = require( '@stdlib/array/base/accessors' ); |
| 64 | +* var copy = require( '@stdlib/array/base/copy' ); |
| 65 | +* |
| 66 | +* function fcn( x, y ) { |
| 67 | +* return x + y; |
| 68 | +* } |
| 69 | +* |
| 70 | +* // Create data buffers: |
| 71 | +* var xbuf = toAccessorArray( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); |
| 72 | +* var ybuf = toAccessorArray( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] ); |
| 73 | +* var zbuf = toAccessorArray( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] ); |
| 74 | +* |
| 75 | +* // Define the shape of the input and output arrays: |
| 76 | +* var shape = [ 1, 1, 1, 1, 1, 1, 1, 3, 2, 2 ]; |
| 77 | +* |
| 78 | +* // Define the array strides: |
| 79 | +* var sx = [ 12, 12, 12, 12, 12, 12, 12, 4, 2, 1 ]; |
| 80 | +* var sy = [ 12, 12, 12, 12, 12, 12, 12, 4, 2, 1 ]; |
| 81 | +* var sz = [ 12, 12, 12, 12, 12, 12, 12, 4, 2, 1 ]; |
| 82 | +* |
| 83 | +* // Define the index offsets: |
| 84 | +* var ox = 0; |
| 85 | +* var oy = 0; |
| 86 | +* var oz = 0; |
| 87 | +* |
| 88 | +* // Create the input and output ndarray-like objects: |
| 89 | +* var x = { |
| 90 | +* 'dtype': 'generic', |
| 91 | +* 'data': xbuf, |
| 92 | +* 'shape': shape, |
| 93 | +* 'strides': sx, |
| 94 | +* 'offset': ox, |
| 95 | +* 'order': 'row-major', |
| 96 | +* 'accessors': accessors( xbuf ).accessors |
| 97 | +* }; |
| 98 | +* var y = { |
| 99 | +* 'dtype': 'generic', |
| 100 | +* 'data': ybuf, |
| 101 | +* 'shape': shape, |
| 102 | +* 'strides': sy, |
| 103 | +* 'offset': oy, |
| 104 | +* 'order': 'row-major', |
| 105 | +* 'accessors': accessors( ybuf ).accessors |
| 106 | +* }; |
| 107 | +* var z = { |
| 108 | +* 'dtype': 'generic', |
| 109 | +* 'data': zbuf, |
| 110 | +* 'shape': shape, |
| 111 | +* 'strides': sz, |
| 112 | +* 'offset': oz, |
| 113 | +* 'order': 'row-major', |
| 114 | +* 'accessors': accessors( zbuf ).accessors |
| 115 | +* }; |
| 116 | +* |
| 117 | +* // Apply the binary function: |
| 118 | +* binary10d( x, y, z, fcn ); |
| 119 | +* |
| 120 | +* console.log( copy( z.data ) ); |
| 121 | +* // => [ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0 ] |
| 122 | +*/ |
| 123 | +function binary10d( x, y, z, fcn ) { |
| 124 | + var xbuf; |
| 125 | + var ybuf; |
| 126 | + var zbuf; |
| 127 | + var xget; |
| 128 | + var yget; |
| 129 | + var zset; |
| 130 | + var dx0; |
| 131 | + var dx1; |
| 132 | + var dx2; |
| 133 | + var dx3; |
| 134 | + var dx4; |
| 135 | + var dx5; |
| 136 | + var dx6; |
| 137 | + var dx7; |
| 138 | + var dx8; |
| 139 | + var dx9; |
| 140 | + var dy0; |
| 141 | + var dy1; |
| 142 | + var dy2; |
| 143 | + var dy3; |
| 144 | + var dy4; |
| 145 | + var dy5; |
| 146 | + var dy6; |
| 147 | + var dy7; |
| 148 | + var dy8; |
| 149 | + var dy9; |
| 150 | + var dz0; |
| 151 | + var dz1; |
| 152 | + var dz2; |
| 153 | + var dz3; |
| 154 | + var dz4; |
| 155 | + var dz5; |
| 156 | + var dz6; |
| 157 | + var dz7; |
| 158 | + var dz8; |
| 159 | + var dz9; |
| 160 | + var sh; |
| 161 | + var S0; |
| 162 | + var S1; |
| 163 | + var S2; |
| 164 | + var S3; |
| 165 | + var S4; |
| 166 | + var S5; |
| 167 | + var S6; |
| 168 | + var S7; |
| 169 | + var S8; |
| 170 | + var S9; |
| 171 | + var sx; |
| 172 | + var sy; |
| 173 | + var sz; |
| 174 | + var ix; |
| 175 | + var iy; |
| 176 | + var iz; |
| 177 | + var i0; |
| 178 | + var i1; |
| 179 | + var i2; |
| 180 | + var i3; |
| 181 | + var i4; |
| 182 | + var i5; |
| 183 | + var i6; |
| 184 | + var i7; |
| 185 | + var i8; |
| 186 | + var i9; |
| 187 | + |
| 188 | + // Note on variable naming convention: S#, dx#, dy#, dz#, i# where # corresponds to the loop number, with `0` being the innermost loop... |
| 189 | + |
| 190 | + // Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments... |
| 191 | + sh = x.shape; |
| 192 | + sx = x.strides; |
| 193 | + sy = y.strides; |
| 194 | + sz = z.strides; |
| 195 | + if ( isRowMajor( x.order ) ) { |
| 196 | + // For row-major ndarrays, the last dimensions have the fastest changing indices... |
| 197 | + S0 = sh[ 9 ]; |
| 198 | + S1 = sh[ 8 ]; |
| 199 | + S2 = sh[ 7 ]; |
| 200 | + S3 = sh[ 6 ]; |
| 201 | + S4 = sh[ 5 ]; |
| 202 | + S5 = sh[ 4 ]; |
| 203 | + S6 = sh[ 3 ]; |
| 204 | + S7 = sh[ 2 ]; |
| 205 | + S8 = sh[ 1 ]; |
| 206 | + S9 = sh[ 0 ]; |
| 207 | + dx0 = sx[ 9 ]; // offset increment for innermost loop |
| 208 | + dx1 = sx[ 8 ] - ( S0*sx[9] ); |
| 209 | + dx2 = sx[ 7 ] - ( S1*sx[8] ); |
| 210 | + dx3 = sx[ 6 ] - ( S2*sx[7] ); |
| 211 | + dx4 = sx[ 5 ] - ( S3*sx[6] ); |
| 212 | + dx5 = sx[ 4 ] - ( S4*sx[5] ); |
| 213 | + dx6 = sx[ 3 ] - ( S5*sx[4] ); |
| 214 | + dx7 = sx[ 2 ] - ( S6*sx[3] ); |
| 215 | + dx8 = sx[ 1 ] - ( S7*sx[2] ); |
| 216 | + dx9 = sx[ 0 ] - ( S8*sx[1] ); // offset increment for outermost loop |
| 217 | + dy0 = sy[ 9 ]; |
| 218 | + dy1 = sy[ 8 ] - ( S0*sy[9] ); |
| 219 | + dy2 = sy[ 7 ] - ( S1*sy[8] ); |
| 220 | + dy3 = sy[ 6 ] - ( S2*sy[7] ); |
| 221 | + dy4 = sy[ 5 ] - ( S3*sy[6] ); |
| 222 | + dy5 = sy[ 4 ] - ( S4*sy[5] ); |
| 223 | + dy6 = sy[ 3 ] - ( S5*sy[4] ); |
| 224 | + dy7 = sy[ 2 ] - ( S6*sy[3] ); |
| 225 | + dy8 = sy[ 1 ] - ( S7*sy[2] ); |
| 226 | + dy9 = sy[ 0 ] - ( S8*sy[1] ); |
| 227 | + dz0 = sz[ 9 ]; |
| 228 | + dz1 = sz[ 8 ] - ( S0*sz[9] ); |
| 229 | + dz2 = sz[ 7 ] - ( S1*sz[8] ); |
| 230 | + dz3 = sz[ 6 ] - ( S2*sz[7] ); |
| 231 | + dz4 = sz[ 5 ] - ( S3*sz[6] ); |
| 232 | + dz5 = sz[ 4 ] - ( S4*sz[5] ); |
| 233 | + dz6 = sz[ 3 ] - ( S5*sz[4] ); |
| 234 | + dz7 = sz[ 2 ] - ( S6*sz[3] ); |
| 235 | + dz8 = sz[ 1 ] - ( S7*sz[2] ); |
| 236 | + dz9 = sz[ 0 ] - ( S8*sz[1] ); |
| 237 | + } else { // order === 'column-major' |
| 238 | + // For column-major ndarrays, the first dimensions have the fastest changing indices... |
| 239 | + S0 = sh[ 0 ]; |
| 240 | + S1 = sh[ 1 ]; |
| 241 | + S2 = sh[ 2 ]; |
| 242 | + S3 = sh[ 3 ]; |
| 243 | + S4 = sh[ 4 ]; |
| 244 | + S5 = sh[ 5 ]; |
| 245 | + S6 = sh[ 6 ]; |
| 246 | + S7 = sh[ 7 ]; |
| 247 | + S8 = sh[ 8 ]; |
| 248 | + S9 = sh[ 9 ]; |
| 249 | + dx0 = sx[ 0 ]; // offset increment for innermost loop |
| 250 | + dx1 = sx[ 1 ] - ( S0*sx[0] ); |
| 251 | + dx2 = sx[ 2 ] - ( S1*sx[1] ); |
| 252 | + dx3 = sx[ 3 ] - ( S2*sx[2] ); |
| 253 | + dx4 = sx[ 4 ] - ( S3*sx[3] ); |
| 254 | + dx5 = sx[ 5 ] - ( S4*sx[4] ); |
| 255 | + dx6 = sx[ 6 ] - ( S5*sx[5] ); |
| 256 | + dx7 = sx[ 7 ] - ( S6*sx[6] ); |
| 257 | + dx8 = sx[ 8 ] - ( S7*sx[7] ); |
| 258 | + dx9 = sx[ 9 ] - ( S8*sx[8] ); // offset increment for outermost loop |
| 259 | + dy0 = sy[ 0 ]; |
| 260 | + dy1 = sy[ 1 ] - ( S0*sy[0] ); |
| 261 | + dy2 = sy[ 2 ] - ( S1*sy[1] ); |
| 262 | + dy3 = sy[ 3 ] - ( S2*sy[2] ); |
| 263 | + dy4 = sy[ 4 ] - ( S3*sy[3] ); |
| 264 | + dy5 = sy[ 5 ] - ( S4*sy[4] ); |
| 265 | + dy6 = sy[ 6 ] - ( S5*sy[5] ); |
| 266 | + dy7 = sy[ 7 ] - ( S6*sy[6] ); |
| 267 | + dy8 = sy[ 8 ] - ( S7*sy[7] ); |
| 268 | + dy9 = sy[ 9 ] - ( S8*sy[8] ); |
| 269 | + dz0 = sz[ 0 ]; |
| 270 | + dz1 = sz[ 1 ] - ( S0*sz[0] ); |
| 271 | + dz2 = sz[ 2 ] - ( S1*sz[1] ); |
| 272 | + dz3 = sz[ 3 ] - ( S2*sz[2] ); |
| 273 | + dz4 = sz[ 4 ] - ( S3*sz[3] ); |
| 274 | + dz5 = sz[ 5 ] - ( S4*sz[4] ); |
| 275 | + dz6 = sz[ 6 ] - ( S5*sz[5] ); |
| 276 | + dz7 = sz[ 7 ] - ( S6*sz[6] ); |
| 277 | + dz8 = sz[ 8 ] - ( S7*sz[7] ); |
| 278 | + dz9 = sz[ 9 ] - ( S8*sz[8] ); |
| 279 | + } |
| 280 | + // Set the pointers to the first indexed elements in the respective ndarrays... |
| 281 | + ix = x.offset; |
| 282 | + iy = y.offset; |
| 283 | + iz = z.offset; |
| 284 | + |
| 285 | + // Cache references to the input and output ndarray buffers... |
| 286 | + xbuf = x.data; |
| 287 | + ybuf = y.data; |
| 288 | + zbuf = z.data; |
| 289 | + |
| 290 | + // Cache accessors: |
| 291 | + xget = x.accessors[ 0 ]; |
| 292 | + yget = y.accessors[ 0 ]; |
| 293 | + zset = z.accessors[ 1 ]; |
| 294 | + |
| 295 | + // Iterate over the ndarray dimensions... |
| 296 | + for ( i9 = 0; i9 < S9; i9++ ) { |
| 297 | + for ( i8 = 0; i8 < S8; i8++ ) { |
| 298 | + for ( i7 = 0; i7 < S7; i7++ ) { |
| 299 | + for ( i6 = 0; i6 < S6; i6++ ) { |
| 300 | + for ( i5 = 0; i5 < S5; i5++ ) { |
| 301 | + for ( i4 = 0; i4 < S4; i4++ ) { |
| 302 | + for ( i3 = 0; i3 < S3; i3++ ) { |
| 303 | + for ( i2 = 0; i2 < S2; i2++ ) { |
| 304 | + for ( i1 = 0; i1 < S1; i1++ ) { |
| 305 | + for ( i0 = 0; i0 < S0; i0++ ) { |
| 306 | + zset( zbuf, iz, fcn( xget( xbuf, ix ), yget( ybuf, iy ) ) ); |
| 307 | + ix += dx0; |
| 308 | + iy += dy0; |
| 309 | + iz += dz0; |
| 310 | + } |
| 311 | + ix += dx1; |
| 312 | + iy += dy1; |
| 313 | + iz += dz1; |
| 314 | + } |
| 315 | + ix += dx2; |
| 316 | + iy += dy2; |
| 317 | + iz += dz2; |
| 318 | + } |
| 319 | + ix += dx3; |
| 320 | + iy += dy3; |
| 321 | + iz += dz3; |
| 322 | + } |
| 323 | + ix += dx4; |
| 324 | + iy += dy4; |
| 325 | + iz += dz4; |
| 326 | + } |
| 327 | + ix += dx5; |
| 328 | + iy += dy5; |
| 329 | + iz += dz5; |
| 330 | + } |
| 331 | + ix += dx6; |
| 332 | + iy += dy6; |
| 333 | + iz += dz6; |
| 334 | + } |
| 335 | + ix += dx7; |
| 336 | + iy += dy7; |
| 337 | + iz += dz7; |
| 338 | + } |
| 339 | + ix += dx8; |
| 340 | + iy += dy8; |
| 341 | + iz += dz8; |
| 342 | + } |
| 343 | + ix += dx9; |
| 344 | + iy += dy9; |
| 345 | + iz += dz9; |
| 346 | + } |
| 347 | +} |
| 348 | + |
| 349 | + |
| 350 | +// EXPORTS // |
| 351 | + |
| 352 | +module.exports = binary10d; |
0 commit comments