time-to-botec

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

ndarray.js (2903B)


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