main.js (2730B)
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 quaternary 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 four 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 - quaternary callback 30 * @returns {void} 31 * 32 * @example 33 * var Float64Array = require( '@stdlib/array/float64' ); 34 * 35 * function add( x, y, z, w ) { 36 * return x + y + z + w; 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( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); 42 * var w = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); 43 * var u = new Float64Array( x.length ); 44 * 45 * var shape = [ x.length ]; 46 * var strides = [ 1, 1, 1, 1, 1 ]; 47 * 48 * quaternary( [ x, y, z, w, u ], shape, strides, add ); 49 * 50 * console.log( u ); 51 * // => <Float64Array>[ 4.0, 8.0, 12.0, 16.0, 20.0 ] 52 */ 53 function quaternary( arrays, shape, strides, fcn ) { 54 var sx; 55 var sy; 56 var sz; 57 var sw; 58 var su; 59 var ix; 60 var iy; 61 var iz; 62 var iw; 63 var iu; 64 var x; 65 var y; 66 var z; 67 var w; 68 var u; 69 var N; 70 var i; 71 72 N = shape[ 0 ]; 73 if ( N <= 0 ) { 74 return; 75 } 76 sx = strides[ 0 ]; 77 sy = strides[ 1 ]; 78 sz = strides[ 2 ]; 79 sw = strides[ 3 ]; 80 su = strides[ 4 ]; 81 if ( sx < 0 ) { 82 ix = (1-N) * sx; 83 } else { 84 ix = 0; 85 } 86 if ( sy < 0 ) { 87 iy = (1-N) * sy; 88 } else { 89 iy = 0; 90 } 91 if ( sz < 0 ) { 92 iz = (1-N) * sz; 93 } else { 94 iz = 0; 95 } 96 if ( sw < 0 ) { 97 iw = (1-N) * sw; 98 } else { 99 iw = 0; 100 } 101 if ( su < 0 ) { 102 iu = (1-N) * su; 103 } else { 104 iu = 0; 105 } 106 x = arrays[ 0 ]; 107 y = arrays[ 1 ]; 108 z = arrays[ 2 ]; 109 w = arrays[ 3 ]; 110 u = arrays[ 4 ]; 111 for ( i = 0; i < N; i++ ) { 112 u[ iu ] = fcn( x[ ix ], y[ iy ], z[ iz ], w[ iw ] ); 113 ix += sx; 114 iy += sy; 115 iz += sz; 116 iw += sw; 117 iu += su; 118 } 119 } 120 121 122 // EXPORTS // 123 124 module.exports = quaternary;