time-to-botec

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

gcusumors.js (1621B)


      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 * Computes the cumulative sum of strided array elements using ordinary recursive summation.
     25 *
     26 * @param {PositiveInteger} N - number of indexed elements
     27 * @param {number} sum - initial sum
     28 * @param {NumericArray} x - input array
     29 * @param {integer} strideX - `x` stride length
     30 * @param {NumericArray} y - output array
     31 * @param {integer} strideY - `y` stride length
     32 * @returns {NumericArray} output array
     33 *
     34 * @example
     35 * var x = [ 1.0, -2.0, 2.0 ];
     36 * var y = [ 0.0, 0.0, 0.0 ];
     37 *
     38 * var v = gcusumors( x.length, 0.0, x, 1, y, 1 );
     39 * // returns [ 1.0, -1.0, 1.0 ]
     40 */
     41 function gcusumors( N, sum, x, strideX, y, strideY ) {
     42 	var ix;
     43 	var iy;
     44 	var i;
     45 
     46 	if ( N <= 0 ) {
     47 		return y;
     48 	}
     49 	if ( strideX < 0 ) {
     50 		ix = (1-N) * strideX;
     51 	} else {
     52 		ix = 0;
     53 	}
     54 	if ( strideY < 0 ) {
     55 		iy = (1-N) * strideY;
     56 	} else {
     57 		iy = 0;
     58 	}
     59 	for ( i = 0; i < N; i++ ) {
     60 		sum += x[ ix ];
     61 		y[ iy ] = sum;
     62 		ix += strideX;
     63 		iy += strideY;
     64 	}
     65 	return y;
     66 }
     67 
     68 
     69 // EXPORTS //
     70 
     71 module.exports = gcusumors;