Skip to content

Commit

Permalink
refactor: update blas/ext/base/gapxsumkbn2 to follow current projec…
Browse files Browse the repository at this point in the history
…t conventions

PR-URL: #4378
Co-authored-by: Athan Reines <kgryte@gmail.com>
Reviewed-by: Athan Reines <kgryte@gmail.com>
  • Loading branch information
headlessNode and kgryte authored Jan 18, 2025
1 parent 04c416e commit 64846e4
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 174 deletions.
53 changes: 18 additions & 35 deletions lib/node_modules/@stdlib/blas/ext/base/gapxsumkbn2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ limitations under the License.

# gapxsumkbn2

> Add a constant to each strided array element and compute the sum using a second-order iterative Kahan–Babuška algorithm.
> Add a scalar constant to each strided array element and compute the sum using a second-order iterative Kahan–Babuška algorithm.
<section class="intro">

Expand All @@ -36,33 +36,29 @@ limitations under the License.
var gapxsumkbn2 = require( '@stdlib/blas/ext/base/gapxsumkbn2' );
```

#### gapxsumkbn2( N, alpha, x, stride )
#### gapxsumkbn2( N, alpha, x, strideX )

Adds a constant to each strided array element and computes the sum using a second-order iterative Kahan–Babuška algorithm.
Adds a scalar constant to each strided array element and computes the sum using a second-order iterative Kahan–Babuška algorithm.

```javascript
var x = [ 1.0, -2.0, 2.0 ];
var N = x.length;

var v = gapxsumkbn2( N, 5.0, x, 1 );
var v = gapxsumkbn2( x.length, 5.0, x, 1 );
// returns 16.0
```

The function has the following parameters:

- **N**: number of indexed elements.
- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
- **stride**: index increment for `x`.
- **strideX**: stride length.

The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to access every other element in `x`,
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to access every other element:

```javascript
var floor = require( '@stdlib/math/base/special/floor' );

var x = [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ];
var N = floor( x.length / 2 );

var v = gapxsumkbn2( N, 5.0, x, 2 );
var v = gapxsumkbn2( 4, 5.0, x, 2 );
// returns 25.0
```

Expand All @@ -72,42 +68,35 @@ Note that indexing is relative to the first index. To introduce an offset, use [

```javascript
var Float64Array = require( '@stdlib/array/float64' );
var floor = require( '@stdlib/math/base/special/floor' );

var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element

var N = floor( x0.length / 2 );

var v = gapxsumkbn2( N, 5.0, x1, 2 );
var v = gapxsumkbn2( 4, 5.0, x1, 2 );
// returns 25.0
```

#### gapxsumkbn2.ndarray( N, alpha, x, stride, offset )
#### gapxsumkbn2.ndarray( N, alpha, x, strideX, offsetX )

Adds a constant to each strided array element and computes the sum using a second-order iterative Kahan–Babuška algorithm and alternative indexing semantics.
Adds a scalar constant to each strided array element and computes the sum using a second-order iterative Kahan–Babuška algorithm and alternative indexing semantics.

```javascript
var x = [ 1.0, -2.0, 2.0 ];
var N = x.length;

var v = gapxsumkbn2.ndarray( N, 5.0, x, 1, 0 );
var v = gapxsumkbn2.ndarray( x.length, 5.0, x, 1, 0 );
// returns 16.0
```

The function has the following additional parameters:

- **offset**: starting index for `x`.
- **offsetX**: starting index.

While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying `buffer`, the `offset` parameter supports indexing semantics based on a starting index. For example, to access every other value in `x` starting from the second value
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to access every other element starting from the second element:

```javascript
var floor = require( '@stdlib/math/base/special/floor' );

var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ];
var N = floor( x.length / 2 );

var v = gapxsumkbn2.ndarray( N, 5.0, x, 2, 1 );
var v = gapxsumkbn2.ndarray( 4, 5.0, x, 2, 1 );
// returns 25.0
```

Expand All @@ -133,18 +122,12 @@ var v = gapxsumkbn2.ndarray( N, 5.0, x, 2, 1 );
<!-- eslint no-undef: "error" -->

```javascript
var randu = require( '@stdlib/random/base/randu' );
var round = require( '@stdlib/math/base/special/round' );
var Float64Array = require( '@stdlib/array/float64' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var gapxsumkbn2 = require( '@stdlib/blas/ext/base/gapxsumkbn2' );

var x;
var i;

x = new Float64Array( 10 );
for ( i = 0; i < x.length; i++ ) {
x[ i ] = round( randu()*100.0 );
}
var x = discreteUniform( 10, -100, 100, {
'dtype': 'float64'
});
console.log( x );

var v = gapxsumkbn2( x.length, 5.0, x, 1 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,20 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var uniform = require( '@stdlib/random/array/uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pow = require( '@stdlib/math/base/special/pow' );
var pkg = require( './../package.json' ).name;
var gapxsumkbn2 = require( './../lib/main.js' );


// VARIABLES //

var options = {
'dtype': 'generic'
};


// FUNCTIONS //

/**
Expand All @@ -38,13 +45,7 @@ var gapxsumkbn2 = require( './../lib/main.js' );
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var x;
var i;

x = [];
for ( i = 0; i < len; i++ ) {
x.push( ( randu()*20.0 ) - 10.0 );
}
var x = uniform( len, -100, 100, options );
return benchmark;

function benchmark( b ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,20 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var uniform = require( '@stdlib/random/array/uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pow = require( '@stdlib/math/base/special/pow' );
var pkg = require( './../package.json' ).name;
var gapxsumkbn2 = require( './../lib/ndarray.js' );


// VARIABLES //

var options = {
'dtype': 'generic'
};


// FUNCTIONS //

/**
Expand All @@ -38,13 +45,7 @@ var gapxsumkbn2 = require( './../lib/ndarray.js' );
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var x;
var i;

x = [];
for ( i = 0; i < len; i++ ) {
x.push( ( randu()*20.0 ) - 10.0 );
}
var x = uniform( len, -100, 100, options );
return benchmark;

function benchmark( b ) {
Expand Down
48 changes: 22 additions & 26 deletions lib/node_modules/@stdlib/blas/ext/base/gapxsumkbn2/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

{{alias}}( N, alpha, x, stride )
Adds a constant to each strided array element and computes the sum using a
second-order iterative Kahan–Babuška algorithm.
{{alias}}( N, alpha, x, strideX )
Adds a scalar constant to each strided array element and computes the sum
using a second-order iterative Kahan–Babuška algorithm.

The `N` and `stride` parameters determine which elements in `x` are accessed
at runtime.
The `N` and stride parameters determine which elements in the strided array
are accessed at runtime.

Indexing is relative to the first index. To introduce an offset, use a typed
array view.
Expand All @@ -17,13 +17,13 @@
Number of indexed elements.

alpha: number
Constant.
Scalar constant.

x: Array<number>|TypedArray
Input array.

stride: integer
Index increment.
strideX: integer
Stride length.

Returns
-------
Expand All @@ -37,28 +37,25 @@
> {{alias}}( x.length, 5.0, x, 1 )
16.0

// Using `N` and `stride` parameters:
// Using `N` and stride parameters:
> x = [ -2.0, 1.0, 1.0, -5.0, 2.0, -1.0 ];
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
> var stride = 2;
> {{alias}}( N, 5.0, x, stride )
> {{alias}}( 3, 5.0, x, 2 )
16.0

// Using view offsets:
> var x0 = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ] );
> var x1 = new {{alias:@stdlib/array/float64}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 );
> N = {{alias:@stdlib/math/base/special/floor}}( x0.length / 2 );
> stride = 2;
> {{alias}}( N, 5.0, x1, stride )
> {{alias}}( 3, 5.0, x1, 2 )
14.0

{{alias}}.ndarray( N, alpha, x, stride, offset )
Adds a constant to each strided array element and computes the sum using a
second-order iterative Kahan–Babuška algorithm and alternative indexing
semantics.

{{alias}}.ndarray( N, alpha, x, strideX, offsetX )
Adds a scalar constant to each strided array element and computes the sum
using a second-order iterative Kahan–Babuška algorithm and alternative
indexing semantics.

While typed array views mandate a view offset based on the underlying
buffer, the `offset` parameter supports indexing semantics based on a
buffer, the offset parameter supports indexing semantics based on a
starting index.

Parameters
Expand All @@ -67,15 +64,15 @@
Number of indexed elements.

alpha: number
Constant.
Scalar constant.

x: Array<number>|TypedArray
Input array.

stride: integer
Index increment.
strideX: integer
Stride length.

offset: integer
offsetX: integer
Starting index.

Returns
Expand All @@ -92,8 +89,7 @@

// Using offset parameter:
> var x = [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ];
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
> {{alias}}.ndarray( N, 5.0, x, 2, 1 )
> {{alias}}.ndarray( 3, 5.0, x, 2, 1 )
14.0

See Also
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ import { NumericArray } from '@stdlib/types/array';
*/
interface Routine {
/**
* Adds a constant to each strided array element and computes the sum using a second-order iterative Kahan–Babuška algorithm.
* Adds a scalar constant to each strided array element and computes the sum using a second-order iterative Kahan–Babuška algorithm.
*
* @param N - number of indexed elements
* @param alpha - constant
* @param alpha - scalar constant
* @param x - input array
* @param stride - stride length
* @param strideX - stride length
* @returns sum
*
* @example
Expand All @@ -41,16 +41,16 @@ interface Routine {
* var v = gapxsumkbn2( x.length, 5.0, x, 1 );
* // returns 16.0
*/
( N: number, alpha: number, x: NumericArray, stride: number ): number;
( N: number, alpha: number, x: NumericArray, strideX: number ): number;

/**
* Adds a constant to each strided array element and computes the sum using a second-order iterative Kahan–Babuška algorithm and alternative indexing semantics.
* Adds a scalar constant to each strided array element and computes the sum using a second-order iterative Kahan–Babuška algorithm and alternative indexing semantics.
*
* @param N - number of indexed elements
* @param alpha - constant
* @param alpha - scalar constant
* @param x - input array
* @param stride - stride length
* @param offset - starting index
* @param strideX - stride length
* @param offsetX - starting index
* @returns sum
*
* @example
Expand All @@ -59,16 +59,16 @@ interface Routine {
* var v = gapxsumkbn2.ndarray( x.length, 5.0, x, 1, 0 );
* // returns 16.0
*/
ndarray( N: number, alpha: number, x: NumericArray, stride: number, offset: number ): number;
ndarray( N: number, alpha: number, x: NumericArray, strideX: number, offsetX: number ): number;
}

/**
* Adds a constant to each strided array element and computes the sum using a second-order iterative Kahan–Babuška algorithm.
* Adds a scalar constant to each strided array element and computes the sum using a second-order iterative Kahan–Babuška algorithm.
*
* @param N - number of indexed elements
* @param alpha - constant
* @param alpha - scalar constant
* @param x - input array
* @param stride - stride length
* @param strideX - stride length
* @returns sum
*
* @example
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,12 @@

'use strict';

var randu = require( '@stdlib/random/base/randu' );
var round = require( '@stdlib/math/base/special/round' );
var Float64Array = require( '@stdlib/array/float64' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var gapxsumkbn2 = require( './../lib' );

var x;
var i;

x = new Float64Array( 10 );
for ( i = 0; i < x.length; i++ ) {
x[ i ] = round( randu()*100.0 );
}
var x = discreteUniform( 10, -100, 100, {
'dtype': 'float64'
});
console.log( x );

var v = gapxsumkbn2( x.length, 5.0, x, 1 );
Expand Down
Loading

1 comment on commit 64846e4

@stdlib-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage Report

Package Statements Branches Functions Lines
blas/ext/base/gapxsumkbn2 $\color{green}214/214$
$\color{green}+100.00\%$
$\color{green}15/15$
$\color{green}+100.00\%$
$\color{green}2/2$
$\color{green}+100.00\%$
$\color{green}214/214$
$\color{green}+100.00\%$

The above coverage report was generated for the changes in this push.

Please sign in to comment.