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