ndarray.js (2542B)
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 ternary 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 three 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 - ternary callback 31 * @returns {void} 32 * 33 * @example 34 * var Float64Array = require( '@stdlib/array/float64' ); 35 * 36 * function add( x, y, z ) { 37 * return x + y + z; 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( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); 43 * var w = new Float64Array( x.length ); 44 * 45 * var shape = [ x.length ]; 46 * var strides = [ 1, 1, 1, 1 ]; 47 * var offsets = [ 0, 0, 0, 0 ]; 48 * 49 * ternary( [ x, y, z, w ], shape, strides, offsets, add ); 50 * 51 * console.log( w ); 52 * // => <Float64Array>[ 3.0, 6.0, 9.0, 12.0, 15.0 ] 53 */ 54 function ternary( arrays, shape, strides, offsets, fcn ) { 55 var sx; 56 var sy; 57 var sz; 58 var sw; 59 var ix; 60 var iy; 61 var iz; 62 var iw; 63 var x; 64 var y; 65 var z; 66 var w; 67 var N; 68 var i; 69 70 N = shape[ 0 ]; 71 if ( N <= 0 ) { 72 return; 73 } 74 ix = offsets[ 0 ]; 75 iy = offsets[ 1 ]; 76 iz = offsets[ 2 ]; 77 iw = offsets[ 3 ]; 78 sx = strides[ 0 ]; 79 sy = strides[ 1 ]; 80 sz = strides[ 2 ]; 81 sw = strides[ 3 ]; 82 x = arrays[ 0 ]; 83 y = arrays[ 1 ]; 84 z = arrays[ 2 ]; 85 w = arrays[ 3 ]; 86 for ( i = 0; i < N; i++ ) { 87 w[ iw ] = fcn( x[ ix ], y[ iy ], z[ iz ] ); 88 ix += sx; 89 iy += sy; 90 iz += sz; 91 iw += sw; 92 } 93 } 94 95 96 // EXPORTS // 97 98 module.exports = ternary;