ndarray.js (2005B)
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 function accepting and returning double-precision floating-point numbers to each element in a double-precision floating-point strided input array and assigns each result to an element in a double-precision floating-point strided output array. 25 * 26 * @param {NonNegativeInteger} N - number of indexed elements 27 * @param {Float64Array} x - input array 28 * @param {integer} strideX - `x` stride length 29 * @param {NonNegativeInteger} offsetX - starting `x` index 30 * @param {Float64Array} y - destination array 31 * @param {integer} strideY - `y` stride length 32 * @param {NonNegativeInteger} offsetY - starting `y` index 33 * @param {Function} fcn - unary function to apply 34 * @returns {Float64Array} `y` 35 * 36 * @example 37 * var Float64Array = require( '@stdlib/array/float64' ); 38 * 39 * function scale( x ) { 40 * return x * 10.0; 41 * } 42 * 43 * var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); 44 * var y = new Float64Array( x.length ); 45 * 46 * dmap( x.length, x, 1, 0, y, 1, 0, scale ); 47 * 48 * console.log( y ); 49 * // => <Float64Array>[ 10.0, 20.0, 30.0, 40.0, 50.0 ] 50 */ 51 function dmap( N, x, strideX, offsetX, y, strideY, offsetY, fcn ) { 52 var ix; 53 var iy; 54 var i; 55 if ( N <= 0 ) { 56 return y; 57 } 58 ix = offsetX; 59 iy = offsetY; 60 for ( i = 0; i < N; i++ ) { 61 y[ iy ] = fcn( x[ ix ] ); 62 ix += strideX; 63 iy += strideY; 64 } 65 return y; 66 } 67 68 69 // EXPORTS // 70 71 module.exports = dmap;