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