6d.js (5512B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2021 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 */ 20 21 'use strict'; 22 23 // MAIN // 24 25 /** 26 * Applies a unary callback to elements in a six-dimensional input ndarray and assigns results to elements in an equivalently shaped output ndarray. 27 * 28 * @private 29 * @param {Object} x - object containing input ndarray meta data 30 * @param {string} x.dtype - data type 31 * @param {Collection} x.data - data buffer 32 * @param {NonNegativeIntegerArray} x.shape - dimensions 33 * @param {IntegerArray} x.strides - stride lengths 34 * @param {NonNegativeInteger} x.offset - index offset 35 * @param {string} x.order - specifies whether `x` is row-major (C-style) or column-major (Fortran-style) 36 * @param {Object} y - object containing output ndarray meta data 37 * @param {string} y.dtype - data type 38 * @param {Collection} y.data - data buffer 39 * @param {NonNegativeIntegerArray} y.shape - dimensions 40 * @param {IntegerArray} y.strides - stride lengths 41 * @param {NonNegativeInteger} y.offset - index offset 42 * @param {string} y.order - specifies whether `y` is row-major (C-style) or column-major (Fortran-style) 43 * @param {Callback} fcn - unary callback 44 * @returns {void} 45 * 46 * @example 47 * var Float64Array = require( '@stdlib/array/float64' ); 48 * 49 * function scale( x ) { 50 * return x * 10.0; 51 * } 52 * 53 * // Create data buffers: 54 * 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 ] ); 55 * var ybuf = new Float64Array( 6 ); 56 * 57 * // Define the shape of the input and output arrays: 58 * var shape = [ 1, 1, 1, 3, 1, 2 ]; 59 * 60 * // Define the array strides: 61 * var sx = [ 12, 12, 12, 4, 4, 1 ]; 62 * var sy = [ 6, 6, 6, 2, 2, 1 ]; 63 * 64 * // Define the index offsets: 65 * var ox = 1; 66 * var oy = 0; 67 * 68 * // Create the input and output ndarray-like objects: 69 * var x = { 70 * 'dtype': 'float64', 71 * 'data': xbuf, 72 * 'shape': shape, 73 * 'strides': sx, 74 * 'offset': ox, 75 * 'order': 'row-major' 76 * }; 77 * var y = { 78 * 'dtype': 'float64', 79 * 'data': ybuf, 80 * 'shape': shape, 81 * 'strides': sy, 82 * 'offset': oy, 83 * 'order': 'row-major' 84 * }; 85 * 86 * // Apply the unary function: 87 * unary6d( x, y, scale ); 88 * 89 * console.log( y.data ); 90 * // => <Float64Array>[ 20.0, 30.0, 60.0, 70.0, 100.0, 110.0 ] 91 */ 92 function unary6d( x, y, fcn ) { 93 var xbuf; 94 var ybuf; 95 var dx0; 96 var dx1; 97 var dx2; 98 var dx3; 99 var dx4; 100 var dx5; 101 var dy0; 102 var dy1; 103 var dy2; 104 var dy3; 105 var dy4; 106 var dy5; 107 var sh; 108 var S0; 109 var S1; 110 var S2; 111 var S3; 112 var S4; 113 var S5; 114 var sx; 115 var sy; 116 var ix; 117 var iy; 118 var i0; 119 var i1; 120 var i2; 121 var i3; 122 var i4; 123 var i5; 124 125 // Note on variable naming convention: S#, dx#, dy#, i# where # corresponds to the loop number, with `0` being the innermost loop... 126 127 // Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments... 128 sh = x.shape; 129 sx = x.strides; 130 sy = y.strides; 131 if ( x.order === 'row-major' ) { 132 // For row-major ndarrays, the last dimensions have the fastest changing indices... 133 S0 = sh[ 5 ]; 134 S1 = sh[ 4 ]; 135 S2 = sh[ 3 ]; 136 S3 = sh[ 2 ]; 137 S4 = sh[ 1 ]; 138 S5 = sh[ 0 ]; 139 dx0 = sx[ 5 ]; // offset increment for innermost loop 140 dx1 = sx[ 4 ] - ( S0*sx[5] ); 141 dx2 = sx[ 3 ] - ( S1*sx[4] ); 142 dx3 = sx[ 2 ] - ( S2*sx[3] ); 143 dx4 = sx[ 1 ] - ( S3*sx[2] ); 144 dx5 = sx[ 0 ] - ( S4*sx[1] ); // offset increment for outermost loop 145 dy0 = sy[ 5 ]; 146 dy1 = sy[ 4 ] - ( S0*sy[5] ); 147 dy2 = sy[ 3 ] - ( S1*sy[4] ); 148 dy3 = sy[ 2 ] - ( S2*sy[3] ); 149 dy4 = sy[ 1 ] - ( S3*sy[2] ); 150 dy5 = sy[ 0 ] - ( S4*sy[1] ); 151 } else { // order === 'column-major' 152 // For column-major ndarrays, the first dimensions have the fastest changing indices... 153 S0 = sh[ 0 ]; 154 S1 = sh[ 1 ]; 155 S2 = sh[ 2 ]; 156 S3 = sh[ 3 ]; 157 S4 = sh[ 4 ]; 158 S5 = sh[ 5 ]; 159 dx0 = sx[ 0 ]; // offset increment for innermost loop 160 dx1 = sx[ 1 ] - ( S0*sx[0] ); 161 dx2 = sx[ 2 ] - ( S1*sx[1] ); 162 dx3 = sx[ 3 ] - ( S2*sx[2] ); 163 dx4 = sx[ 4 ] - ( S3*sx[3] ); 164 dx5 = sx[ 5 ] - ( S4*sx[4] ); // offset increment for outermost loop 165 dy0 = sy[ 0 ]; 166 dy1 = sy[ 1 ] - ( S0*sy[0] ); 167 dy2 = sy[ 2 ] - ( S1*sy[1] ); 168 dy3 = sy[ 3 ] - ( S2*sy[2] ); 169 dy4 = sy[ 4 ] - ( S3*sy[3] ); 170 dy5 = sy[ 5 ] - ( S4*sy[4] ); 171 } 172 // Set the pointers to the first indexed elements in the respective ndarrays... 173 ix = x.offset; 174 iy = y.offset; 175 176 // Cache references to the input and output ndarray buffers... 177 xbuf = x.data; 178 ybuf = y.data; 179 180 // Iterate over the ndarray dimensions... 181 for ( i5 = 0; i5 < S5; i5++ ) { 182 for ( i4 = 0; i4 < S4; i4++ ) { 183 for ( i3 = 0; i3 < S3; i3++ ) { 184 for ( i2 = 0; i2 < S2; i2++ ) { 185 for ( i1 = 0; i1 < S1; i1++ ) { 186 for ( i0 = 0; i0 < S0; i0++ ) { 187 ybuf[ iy ] = fcn( xbuf[ ix ] ); 188 ix += dx0; 189 iy += dy0; 190 } 191 ix += dx1; 192 iy += dy1; 193 } 194 ix += dx2; 195 iy += dy2; 196 } 197 ix += dx3; 198 iy += dy3; 199 } 200 ix += dx4; 201 iy += dy4; 202 } 203 ix += dx5; 204 iy += dy5; 205 } 206 } 207 208 209 // EXPORTS // 210 211 module.exports = unary6d;