|
| 1 | +/** |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2025 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 | +// MODULES // |
| 22 | + |
| 23 | +var abs = require( '@stdlib/math/base/special/abs' ); |
| 24 | + |
| 25 | + |
| 26 | +// VARIABLES // |
| 27 | + |
| 28 | +var M = 6; |
| 29 | + |
| 30 | + |
| 31 | +// MAIN // |
| 32 | + |
| 33 | +/** |
| 34 | +* Computes the sum of absolute values. |
| 35 | +* |
| 36 | +* @param {PositiveInteger} N - number of indexed elements |
| 37 | +* @param {Object} x - input array object |
| 38 | +* @param {Collection} x.data - input array data |
| 39 | +* @param {Array<Function>} x.accessors - array element accessors |
| 40 | +* @param {integer} stride - index increment |
| 41 | +* @param {NonNegativeInteger} offset - starting index |
| 42 | +* @returns {number} sum |
| 43 | +* |
| 44 | +* @example |
| 45 | +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); |
| 46 | +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); |
| 47 | +* |
| 48 | +* var x = [ 1.0, -2.0, 3.0, -4.0, 5.0 ]; |
| 49 | +* |
| 50 | +* var s = gasum( x.length, arraylike2object( toAccessorArray( x ) ), 1, 0 ); |
| 51 | +* // returns 15.0 |
| 52 | +*/ |
| 53 | +function gasum( N, x, stride, offset ) { |
| 54 | + var buf; |
| 55 | + var get; |
| 56 | + var sum; |
| 57 | + var ix; |
| 58 | + var m; |
| 59 | + var i; |
| 60 | + |
| 61 | + buf = x.data; |
| 62 | + get = x.accessors[ 0 ]; |
| 63 | + |
| 64 | + sum = 0.0; |
| 65 | + ix = offset; |
| 66 | + if ( stride === 0 ) { |
| 67 | + sum = abs( get( buf, ix ) * N ); |
| 68 | + return sum; |
| 69 | + } |
| 70 | + |
| 71 | + // Use unrolled loops if the stride is equal to `1`... |
| 72 | + if ( stride === 1 ) { |
| 73 | + m = N % M; |
| 74 | + |
| 75 | + // If we have a remainder, run a clean-up loop... |
| 76 | + if ( m > 0 ) { |
| 77 | + for ( i = 0; i < m; i++ ) { |
| 78 | + sum += abs( get( buf, ix ) ); |
| 79 | + ix += stride; |
| 80 | + } |
| 81 | + } |
| 82 | + if ( N < M ) { |
| 83 | + return sum; |
| 84 | + } |
| 85 | + for ( i = m; i < N; i += M ) { |
| 86 | + sum += abs( get( buf, ix ) ) + abs( get( buf, ix+1 ) ) + abs( get( buf, ix+2 ) ) + abs( get( buf, ix+3 ) ) + abs( get( buf, ix+4 ) ) + abs( get( buf, ix+5 ) ); // eslint-disable-line max-len |
| 87 | + ix += M; |
| 88 | + } |
| 89 | + return sum; |
| 90 | + } |
| 91 | + for ( i = 0; i < N; i++ ) { |
| 92 | + sum += abs( get( buf, ix ) ); |
| 93 | + ix += stride; |
| 94 | + } |
| 95 | + return sum; |
| 96 | +} |
| 97 | + |
| 98 | + |
| 99 | +// EXPORTS // |
| 100 | + |
| 101 | +module.exports = gasum; |
0 commit comments