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