time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

ndarray.js (2737B)


      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 {NonNegativeIntegerArray} offsets - array-like object containing the starting indices (i.e., index offsets) for the input and output arrays
     30 * @param {Callback} fcn - quaternary callback
     31 * @returns {void}
     32 *
     33 * @example
     34 * var Float64Array = require( '@stdlib/array/float64' );
     35 *
     36 * function add( x, y, z, w ) {
     37 *     return x + y + z + w;
     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( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
     44 * var u = new Float64Array( x.length );
     45 *
     46 * var shape = [ x.length ];
     47 * var strides = [ 1, 1, 1, 1, 1 ];
     48 * var offsets = [ 0, 0, 0, 0, 0 ];
     49 *
     50 * quaternary( [ x, y, z, w, u ], shape, strides, offsets, add );
     51 *
     52 * console.log( u );
     53 * // => <Float64Array>[ 4.0, 8.0, 12.0, 16.0, 20.0 ]
     54 */
     55 function quaternary( arrays, shape, strides, offsets, fcn ) {
     56 	var sx;
     57 	var sy;
     58 	var sz;
     59 	var sw;
     60 	var su;
     61 	var ix;
     62 	var iy;
     63 	var iz;
     64 	var iw;
     65 	var iu;
     66 	var x;
     67 	var y;
     68 	var z;
     69 	var w;
     70 	var u;
     71 	var N;
     72 	var i;
     73 
     74 	N = shape[ 0 ];
     75 	if ( N <= 0 ) {
     76 		return;
     77 	}
     78 	ix = offsets[ 0 ];
     79 	iy = offsets[ 1 ];
     80 	iz = offsets[ 2 ];
     81 	iw = offsets[ 3 ];
     82 	iu = offsets[ 4 ];
     83 	sx = strides[ 0 ];
     84 	sy = strides[ 1 ];
     85 	sz = strides[ 2 ];
     86 	sw = strides[ 3 ];
     87 	su = strides[ 4 ];
     88 	x = arrays[ 0 ];
     89 	y = arrays[ 1 ];
     90 	z = arrays[ 2 ];
     91 	w = arrays[ 3 ];
     92 	u = arrays[ 4 ];
     93 	for ( i = 0; i < N; i++ ) {
     94 		u[ iu ] = fcn( x[ ix ], y[ iy ], z[ iz ], w[ iw ] );
     95 		ix += sx;
     96 		iy += sy;
     97 		iz += sz;
     98 		iw += sw;
     99 		iu += su;
    100 	}
    101 }
    102 
    103 
    104 // EXPORTS //
    105 
    106 module.exports = quaternary;