Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/node_modules/@stdlib/blas/ext/base/gnannsumkbn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ var v = gnannsumkbn.ndarray( 4, x, 2, 1, out, 2, 1 );
## Notes

- If `N <= 0`, both functions return a sum equal to `0.0`.
- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor])

</section>

Expand Down Expand Up @@ -190,6 +191,8 @@ console.log( out );

[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray

[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor

[@neumaier:1974a]: https://doi.org/10.1002/zamm.19740540106

<!-- <related-links> -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,17 @@

/// <reference types="@stdlib/types"/>

import { NumericArray } from '@stdlib/types/array';
import { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array';

/**
* Input array.
*/
type InputArray = NumericArray | Collection<number> | AccessorArrayLike<number>;

/**
* Output array.
*/
type OutputArray = NumericArray | Collection<number> | AccessorArrayLike<number>;

/**
* Interface describing `gnannsumkbn`.
Expand All @@ -43,7 +53,7 @@ interface Routine {
* var v = gnannsumkbn( x.length, x, 1, out, 1 );
* // returns [ 1.0, 3 ]
*/
( N: number, x: NumericArray, strideX: number, out: NumericArray, strideOut: number ): NumericArray;
<T extends OutputArray>( N: number, x: InputArray, strideX: number, out: T, strideOut: number ): T;

/**
* Computes the sum of strided array elements, ignoring `NaN` values and using an improved Kahan–Babuška algorithm and alternative indexing semantics.
Expand All @@ -64,7 +74,7 @@ interface Routine {
* var v = gnannsumkbn( x.length, x, 1, 0, out, 1, 0 );
* // returns [ 1.0, 3 ]
*/
ndarray( N: number, x: NumericArray, strideX: number, offsetX: number, out: NumericArray, strideOut: number, offsetOut: number ): NumericArray;
ndarray<T extends OutputArray>( N: number, x: InputArray, strideX: number, offsetX: number, out: T, strideOut: number, offsetOut: number ): T;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* limitations under the License.
*/

import AccessorArray = require( '@stdlib/array/base/accessor' );
import gnannsumkbn = require( './index' );


Expand All @@ -26,7 +27,8 @@ import gnannsumkbn = require( './index' );
const x = new Float64Array( 10 );
const out = new Float64Array( 2 );

gnannsumkbn( x.length, x, 1, out, 1 ); // $ExpectType NumericArray
gnannsumkbn( x.length, x, 1, out, 1 ); // $ExpectType Float64Array
gnannsumkbn( x.length, new AccessorArray( x ), 1, new AccessorArray( out ), 1 ); // $ExpectType AccessorArray<number>
}

// The compiler throws an error if the function is provided a first argument which is not a number...
Expand Down Expand Up @@ -123,7 +125,8 @@ import gnannsumkbn = require( './index' );
const x = new Float64Array( 10 );
const out = new Float64Array( 2 );

gnannsumkbn.ndarray( x.length, x, 1, 0, out, 1, 0 ); // $ExpectType NumericArray
gnannsumkbn.ndarray( x.length, x, 1, 0, out, 1, 0 ); // $ExpectType Float64Array
gnannsumkbn.ndarray( x.length, new AccessorArray( x ), 1, 0, new AccessorArray( out ), 1, 0 ); // $ExpectType AccessorArray<number>
}

// The compiler throws an error if the `ndarray` method is provided a first argument which is not a number...
Expand Down
122 changes: 122 additions & 0 deletions lib/node_modules/@stdlib/blas/ext/base/gnannsumkbn/lib/accessors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var isnan = require( '@stdlib/math/base/assert/is-nan' );
var abs = require( '@stdlib/math/base/special/abs' );


// MAIN //

/**
* Computes the sum of strided array elements, ignoring `NaN` values and using an improved Kahan–Babuška algorithm.
*
* ## Method
*
* - This implementation uses an "improved Kahan–Babuška algorithm", as described by Neumaier (1974).
*
* ## References
*
* - Neumaier, Arnold. 1974. "Rounding Error Analysis of Some Methods for Summing Finite Sums." _Zeitschrift Für Angewandte Mathematik Und Mechanik_ 54 (1): 39–51. doi:[10.1002/zamm.19740540106](https://doi.org/10.1002/zamm.19740540106).
*
* @private
* @param {PositiveInteger} N - number of indexed elements
* @param {Object} x - input array object
* @param {Collection} x.data - input array data
* @param {Array<Function>} x.accessors - array element accessors
* @param {integer} strideX - stride length for `x`
* @param {NonNegativeInteger} offsetX - starting index for `x`
* @param {Object} out - output array object
* @param {Collection} out.data - output array data
* @param {Array<Function>} out.accessors - array element accessors
* @param {integer} strideOut - stride length for `out`
* @param {NonNegativeInteger} offsetOut - starting index for `out`
* @returns {Object} output array object
*
* @example
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
* var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
*
* var x = toAccessorArray( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] );
* var out = toAccessorArray( [ 0.0, 0 ] );
*
* var v = gnannsumkbn( 5, arraylike2object( x ), 2, 1, arraylike2object( out ), 1, 0 );
* // returns {...}
*/
function gnannsumkbn( N, x, strideX, offsetX, out, strideOut, offsetOut ) {
var obuf;
var xbuf;
var xget;
var oset;
var sum;
var ix;
var v;
var t;
var c;
var n;
var i;

// Cache reference to array data:
xbuf = x.data;
obuf = out.data;

// Cache reference to the element accessors:
xget = x.accessors[ 0 ];
oset = out.accessors[ 1 ];

sum = 0.0;
ix = offsetX;
if ( strideX === 0 ) {
v = xget( xbuf, ix );
if ( isnan( v ) ) {
oset( obuf, offsetOut, sum );
oset( obuf, offsetOut+strideOut, 0 );
return out;
}
oset( obuf, offsetOut, v * N );
oset( obuf, offsetOut+strideOut, N );
return out;
}
c = 0.0;
n = 0;
for ( i = 0; i < N; i++ ) {
v = xget( xbuf, ix );
if ( isnan( v ) === false ) {
t = sum + v;
if ( abs( sum ) >= abs( v ) ) {
c += (sum-t) + v;
} else {
c += (v-t) + sum;
}
sum = t;
n += 1;
}
ix += strideX;
}
oset( obuf, offsetOut, sum + c );
oset( obuf, offsetOut+strideOut, n );
return out;
}


// EXPORTS //

module.exports = gnannsumkbn;
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@

// MODULES //

var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var abs = require( '@stdlib/math/base/special/abs' );
var accessors = require( './accessors.js' );


// MAIN //
Expand Down Expand Up @@ -56,6 +58,8 @@ var abs = require( '@stdlib/math/base/special/abs' );
function gnannsumkbn( N, x, strideX, offsetX, out, strideOut, offsetOut ) {
var sum;
var ix;
var ox;
var oo;
var v;
var t;
var c;
Expand All @@ -68,6 +72,11 @@ function gnannsumkbn( N, x, strideX, offsetX, out, strideOut, offsetOut ) {
out[ offsetOut+strideOut ] = 0;
return out;
}
ox = arraylike2object( x );
oo = arraylike2object( out );
if ( ox.accessorProtocol || oo.accessorProtocol ) {
return accessors( N, ox, strideX, offsetX, oo, strideOut, offsetOut );
}
ix = offsetX;
if ( strideX === 0 ) {
if ( isnan( x[ ix ] ) ) {
Expand Down
Loading