2d.js (4304B)
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 two-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 ] ); 53 * var ybuf = new Float64Array( 4 ); 54 * 55 * // Define the shape of the input and output arrays: 56 * var shape = [ 2, 2 ]; 57 * 58 * // Define the array strides: 59 * var sx = [ 4, 1 ]; 60 * var sy = [ 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 * unary2d( x, y, scale ); 86 * 87 * console.log( y.data ); 88 * // => <Float64Array>[ 20.0, 30.0, 60.0, 70.0 ] 89 */ 90 function unary2d( x, y, fcn ) { 91 var xbuf; 92 var ybuf; 93 var dx0; 94 var dx1; 95 var dy0; 96 var dy1; 97 var sh; 98 var S0; 99 var S1; 100 var sx; 101 var sy; 102 var ix; 103 var iy; 104 var i0; 105 var i1; 106 107 // Note on variable naming convention: S#, dx#, dy#, i# where # corresponds to the loop number, with `0` being the innermost loop... 108 109 // Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments... 110 sh = x.shape; 111 sx = x.strides; 112 sy = y.strides; 113 if ( x.order === 'row-major' ) { 114 // For row-major ndarrays, the last dimensions have the fastest changing indices... 115 S0 = sh[ 1 ]; 116 S1 = sh[ 0 ]; 117 dx0 = sx[ 1 ]; // offset increment for innermost loop 118 dx1 = sx[ 0 ] - ( S0*sx[1] ); // offset increment for outermost loop 119 dy0 = sy[ 1 ]; 120 dy1 = sy[ 0 ] - ( S0*sy[1] ); 121 } else { // order === 'column-major' 122 // For column-major ndarrays, the first dimensions have the fastest changing indices... 123 S0 = sh[ 0 ]; 124 S1 = sh[ 1 ]; 125 dx0 = sx[ 0 ]; // offset increment for innermost loop 126 dx1 = sx[ 1 ] - ( S0*sx[0] ); // offset increment for outermost loop 127 dy0 = sy[ 0 ]; 128 dy1 = sy[ 1 ] - ( S0*sy[0] ); 129 } 130 // Set the pointers to the first indexed elements in the respective ndarrays... 131 ix = x.offset; 132 iy = y.offset; 133 134 // Cache references to the input and output ndarray buffers... 135 xbuf = x.data; 136 ybuf = y.data; 137 138 // Iterate over the ndarray dimensions... 139 for ( i1 = 0; i1 < S1; i1++ ) { 140 for ( i0 = 0; i0 < S0; i0++ ) { 141 ybuf[ iy ] = fcn( xbuf[ ix ] ); 142 ix += dx0; 143 iy += dy0; 144 } 145 ix += dx1; 146 iy += dy1; 147 } 148 } 149 150 151 // EXPORTS // 152 153 module.exports = unary2d;