ndarray.js (2188B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2020 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 strided input array and assigns results to elements in a strided output array. 25 * 26 * @param {ArrayLikeObject<Collection>} arrays - array-like object containing one input array and one output array 27 * @param {NonNegativeIntegerArray} shape - array-like object containing a single element, the number of indexed elements 28 * @param {IntegerArray} strides - array-like object containing the stride lengths for the input and output arrays 29 * @param {NonNegativeIntegerArray} offsets - array-like object containing the starting indices (i.e., index offsets) for the input and output arrays 30 * @param {Callback} fcn - unary callback 31 * @returns {void} 32 * 33 * @example 34 * var Float64Array = require( '@stdlib/array/float64' ); 35 * 36 * function scale( x ) { 37 * return x * 10.0; 38 * } 39 * 40 * var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); 41 * var y = new Float64Array( x.length ); 42 * 43 * var shape = [ x.length ]; 44 * var strides = [ 1, 1 ]; 45 * var offsets = [ 0, 0 ]; 46 * 47 * unary( [ x, y ], shape, strides, offsets, scale ); 48 * 49 * console.log( y ); 50 * // => <Float64Array>[ 10.0, 20.0, 30.0, 40.0, 50.0 ] 51 */ 52 function unary( arrays, shape, strides, offsets, fcn ) { 53 var sx; 54 var sy; 55 var ix; 56 var iy; 57 var x; 58 var y; 59 var N; 60 var i; 61 62 N = shape[ 0 ]; 63 if ( N <= 0 ) { 64 return; 65 } 66 ix = offsets[ 0 ]; 67 iy = offsets[ 1 ]; 68 sx = strides[ 0 ]; 69 sy = strides[ 1 ]; 70 x = arrays[ 0 ]; 71 y = arrays[ 1 ]; 72 for ( i = 0; i < N; i++ ) { 73 y[ iy ] = fcn( x[ ix ] ); 74 ix += sx; 75 iy += sy; 76 } 77 } 78 79 80 // EXPORTS // 81 82 module.exports = unary;