gsumors.js (1758B)
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 // VARIABLES // 22 23 var M = 6; 24 25 26 // MAIN // 27 28 /** 29 * Computes the sum of strided array elements using ordinary recursive summation. 30 * 31 * @param {PositiveInteger} N - number of indexed elements 32 * @param {NumericArray} x - input array 33 * @param {integer} stride - stride length 34 * @returns {number} sum 35 * 36 * @example 37 * var x = [ 1.0, -2.0, 2.0 ]; 38 * var N = x.length; 39 * 40 * var v = gsumors( N, x, 1 ); 41 * // returns 1.0 42 */ 43 function gsumors( N, x, stride ) { 44 var ix; 45 var m; 46 var s; 47 var i; 48 49 s = 0.0; 50 if ( N <= 0 ) { 51 return s; 52 } 53 if ( N === 1 || stride === 0 ) { 54 return x[ 0 ]; 55 } 56 // If the stride is equal to `1`, use unrolled loops... 57 if ( stride === 1 ) { 58 m = N % M; 59 60 // If we have a remainder, run a clean-up loop... 61 if ( m > 0 ) { 62 for ( i = 0; i < m; i++ ) { 63 s += x[ i ]; 64 } 65 } 66 if ( N < M ) { 67 return s; 68 } 69 for ( i = m; i < N; i += M ) { 70 s += x[i] + x[i+1] + x[i+2] + x[i+3] + x[i+4] + x[i+5]; 71 } 72 return s; 73 } 74 if ( stride < 0 ) { 75 ix = (1-N) * stride; 76 } else { 77 ix = 0; 78 } 79 for ( i = 0; i < N; i++ ) { 80 s += x[ ix ]; 81 ix += stride; 82 } 83 return s; 84 } 85 86 87 // EXPORTS // 88 89 module.exports = gsumors;