main.js (2275B)
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 binary callback to strided input array elements and assigns results to elements in a strided output array. 25 * 26 * @param {ArrayLikeObject<Collection>} arrays - array-like object containing two input arrays 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 {Callback} fcn - binary callback 30 * @returns {void} 31 * 32 * @example 33 * var Float64Array = require( '@stdlib/array/float64' ); 34 * 35 * function add( x, y ) { 36 * return x + y; 37 * } 38 * 39 * var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); 40 * var y = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); 41 * var z = new Float64Array( x.length ); 42 * 43 * var shape = [ x.length ]; 44 * var strides = [ 1, 1, 1 ]; 45 * 46 * binary( [ x, y, z ], shape, strides, add ); 47 * 48 * console.log( z ); 49 * // => <Float64Array>[ 2.0, 4.0, 6.0, 8.0, 10.0 ] 50 */ 51 function binary( arrays, shape, strides, fcn ) { 52 var sx; 53 var sy; 54 var sz; 55 var ix; 56 var iy; 57 var iz; 58 var x; 59 var y; 60 var z; 61 var N; 62 var i; 63 64 N = shape[ 0 ]; 65 if ( N <= 0 ) { 66 return; 67 } 68 sx = strides[ 0 ]; 69 sy = strides[ 1 ]; 70 sz = strides[ 2 ]; 71 if ( sx < 0 ) { 72 ix = (1-N) * sx; 73 } else { 74 ix = 0; 75 } 76 if ( sy < 0 ) { 77 iy = (1-N) * sy; 78 } else { 79 iy = 0; 80 } 81 if ( sz < 0 ) { 82 iz = (1-N) * sz; 83 } else { 84 iz = 0; 85 } 86 x = arrays[ 0 ]; 87 y = arrays[ 1 ]; 88 z = arrays[ 2 ]; 89 for ( i = 0; i < N; i++ ) { 90 z[ iz ] = fcn( x[ ix ], y[ iy ] ); 91 ix += sx; 92 iy += sy; 93 iz += sz; 94 } 95 } 96 97 98 // EXPORTS // 99 100 module.exports = binary;