1d.js (3451B)
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 one-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 = [ 4 ]; 57 * 58 * // Define the array strides: 59 * var sx = [ 2 ]; 60 * var sy = [ 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 * unary1d( x, y, scale ); 86 * 87 * console.log( y.data ); 88 * // => <Float64Array>[ 20.0, 40.0, 60.0, 80.0 ] 89 */ 90 function unary1d( x, y, fcn ) { 91 var xbuf; 92 var ybuf; 93 var dx0; 94 var dy0; 95 var S0; 96 var ix; 97 var iy; 98 var i0; 99 100 // Note on variable naming convention: S#, dx#, dy#, i# where # corresponds to the loop number, with `0` being the innermost loop... 101 102 // Extract loop variables: dimensions and loop offset (pointer) increments... 103 S0 = x.shape[ 0 ]; 104 dx0 = x.strides[ 0 ]; 105 dy0 = y.strides[ 0 ]; 106 107 // Set the pointers to the first indexed elements in the respective ndarrays... 108 ix = x.offset; 109 iy = y.offset; 110 111 // Cache references to the input and output ndarray buffers... 112 xbuf = x.data; 113 ybuf = y.data; 114 115 // Iterate over the ndarray dimensions... 116 for ( i0 = 0; i0 < S0; i0++ ) { 117 ybuf[ iy ] = fcn( xbuf[ ix ] ); 118 ix += dx0; 119 iy += dy0; 120 } 121 } 122 123 124 // EXPORTS // 125 126 module.exports = unary1d;