Skip to content

Commit

Permalink
Auto-generated commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stdlib-bot committed Feb 5, 2025
1 parent 361af0e commit 8c5e915
Show file tree
Hide file tree
Showing 8 changed files with 597 additions and 5 deletions.
23 changes: 23 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -1514,6 +1514,28 @@ This release closes the following issue:

<!-- /.package -->

<section class="package" id="blas-ext-base-gnannsumkbn-unreleased">

#### [@stdlib/blas/ext/base/gnannsumkbn](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/ext/base/gnannsumkbn)

<details>

<section class="features">

##### Features

- [`9da9e46`](https://github.com/stdlib-js/stdlib/commit/9da9e464d924bae935b701593e8c7a8cc4377f5c) - add accessor arrays support to `blas/ext/base/gnannsumkbn` [(#5023)](https://github.com/stdlib-js/stdlib/pull/5023)

</section>

<!-- /.features -->

</details>

</section>

<!-- /.package -->

<section class="package" id="blas-ext-base-gnansumkbn-unreleased">

#### [@stdlib/blas/ext/base/gnansumkbn](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/ext/base/gnansumkbn)
Expand Down Expand Up @@ -2497,6 +2519,7 @@ A total of 11 people contributed to this release. Thank you to the following con

<details>

- [`9da9e46`](https://github.com/stdlib-js/stdlib/commit/9da9e464d924bae935b701593e8c7a8cc4377f5c) - **feat:** add accessor arrays support to `blas/ext/base/gnannsumkbn` [(#5023)](https://github.com/stdlib-js/stdlib/pull/5023) _(by Muhammad Haris, Athan Reines)_
- [`6c90e8f`](https://github.com/stdlib-js/stdlib/commit/6c90e8fa6ad8f6bf254e6479d1a3ece17b5e92da) - **feat:** add accessor arrays support to `blas/ext/base/gcusumpw` [(#5012)](https://github.com/stdlib-js/stdlib/pull/5012) _(by Muhammad Haris)_
- [`1dd8adf`](https://github.com/stdlib-js/stdlib/commit/1dd8adfd104b90f0b87cd76ce0addbbd6f723aba) - **feat:** add accessor arrays support to `blas/ext/base/gcusumkbn2` [(#5009)](https://github.com/stdlib-js/stdlib/pull/5009) _(by Muhammad Haris, Athan Reines)_
- [`8310c1f`](https://github.com/stdlib-js/stdlib/commit/8310c1f4054d2ff0ee8b05f1c77b1d3c846a1012) - **feat:** add accessor arrays support to `blas/ext/base/gcusumors` [(#5010)](https://github.com/stdlib-js/stdlib/pull/5010) _(by Muhammad Haris)_
Expand Down
3 changes: 3 additions & 0 deletions 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/array-base-accessor

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

<!-- <related-links> -->
Expand Down
16 changes: 13 additions & 3 deletions ext/base/gnannsumkbn/docs/types/index.d.ts
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
7 changes: 5 additions & 2 deletions ext/base/gnannsumkbn/docs/types/test.ts
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 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;
10 changes: 10 additions & 0 deletions ext/base/gnannsumkbn/lib/ndarray.js
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,12 @@ 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 ) {
accessors( N, ox, strideX, offsetX, oo, strideOut, offsetOut );
return out;
}
ix = offsetX;
if ( strideX === 0 ) {
if ( isnan( x[ ix ] ) ) {
Expand Down
Loading

0 comments on commit 8c5e915

Please sign in to comment.