main.js (1971B)
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 {Float64Array} y - destination array 30 * @param {integer} strideY - `y` stride length 31 * @param {Function} fcn - unary function to apply 32 * @returns {Float64Array} `y` 33 * 34 * @example 35 * var Float64Array = require( '@stdlib/array/float64' ); 36 * 37 * function scale( x ) { 38 * return x * 10.0; 39 * } 40 * 41 * var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); 42 * var y = new Float64Array( x.length ); 43 * 44 * dmap( x.length, x, 1, y, 1, scale ); 45 * 46 * console.log( y ); 47 * // => <Float64Array>[ 10.0, 20.0, 30.0, 40.0, 50.0 ] 48 */ 49 function dmap( N, x, strideX, y, strideY, fcn ) { 50 var ix; 51 var iy; 52 var i; 53 if ( N <= 0 ) { 54 return y; 55 } 56 if ( strideX < 0 ) { 57 ix = (1-N) * strideX; 58 } else { 59 ix = 0; 60 } 61 if ( strideY < 0 ) { 62 iy = (1-N) * strideY; 63 } else { 64 iy = 0; 65 } 66 for ( i = 0; i < N; i++ ) { 67 y[ iy ] = fcn( x[ ix ] ); 68 ix += strideX; 69 iy += strideY; 70 } 71 return y; 72 } 73 74 75 // EXPORTS // 76 77 module.exports = dmap;