Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: update blas/ext/base/gcusumkbn to follow current project conventions #4412

Merged
merged 7 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
40 changes: 12 additions & 28 deletions lib/node_modules/@stdlib/blas/ext/base/gcusumkbn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,17 @@ The function has the following parameters:
- **N**: number of indexed elements.
- **sum**: initial sum.
- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
- **strideX**: index increment for `x`.
- **strideX**: stride length for `x`.
- **y**: output [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
- **strideY**: index increment for `y`.
- **strideY**: stride length for `y`.

The `N` and `stride` parameters determine which elements in `x` and `y` are accessed at runtime. For example, to compute the cumulative sum of every other element in `x`,
The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to compute the cumulative sum of every other element in the strided input array:
headlessNode marked this conversation as resolved.
Show resolved Hide resolved

```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 y = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ];

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

var v = gcusumkbn( N, 0.0, x, 2, y, 1 );
var v = gcusumkbn( 4, 0.0, x, 2, y, 1 );
// y => [ 1.0, 3.0, 1.0, 5.0, 0.0, 0.0, 0.0, 0.0 ]
```

Expand All @@ -83,7 +79,6 @@ 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' );

// Initial arrays...
var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
Expand All @@ -93,9 +88,7 @@ var y0 = new Float64Array( x0.length );
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
var y1 = new Float64Array( y0.buffer, y0.BYTES_PER_ELEMENT*3 ); // start at 4th element

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

gcusumkbn( N, 0.0, x1, -2, y1, 1 );
gcusumkbn( 4, 0.0, x1, -2, y1, 1 );
// y0 => <Float64Array>[ 0.0, 0.0, 0.0, 4.0, 6.0, 4.0, 5.0, 0.0 ]
```

Expand All @@ -116,17 +109,13 @@ The function has the following additional parameters:
- **offsetX**: starting index for `x`.
- **offsetY**: starting index for `y`.

While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying `buffer`, `offsetX` and `offsetY` parameters support indexing semantics based on a starting indices. For example, to calculate the cumulative sum of every other value in `x` starting from the second value and to store in the last `N` elements of `y` starting from the last element
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, offsetX and offsetY parameters support indexing semantics based on starting indices. For example, to calculate the cumulative sum of every other element in the strided input array starting from the second element and to store in the last `N` elements of the strided output array starting from the last element:
kgryte marked this conversation as resolved.
Show resolved Hide resolved

```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 y = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ];

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

gcusumkbn.ndarray( N, 0.0, x, 2, 1, y, -1, y.length-1 );
gcusumkbn.ndarray( 4, 0.0, x, 2, 1, y, -1, y.length-1 );
// y => [ 0.0, 0.0, 0.0, 0.0, 5.0, 1.0, -1.0, 1.0 ]
```

Expand All @@ -152,20 +141,15 @@ gcusumkbn.ndarray( N, 0.0, x, 2, 1, y, -1, y.length-1 );
<!-- eslint no-undef: "error" -->

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

var y;
var x;
var i;
var x = discreteUniform( 10.0, -100, 100, {
kgryte marked this conversation as resolved.
Show resolved Hide resolved
'dtype': 'float64'
});
var y = new Float64Array( x.length );

x = new Float64Array( 10 );
y = new Float64Array( x.length );
for ( i = 0; i < x.length; i++ ) {
x[ i ] = round( randu()*100.0 );
}
console.log( x );
console.log( y );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,22 @@
// 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 gfill = require( '@stdlib/blas/ext/base/gfill' );
var Float64Array = require( '@stdlib/array/float64' );
var pkg = require( './../package.json' ).name;
var gcusumkbn = require( './../lib/main.js' );


// VARIABLES //

var options = {
'dtype': 'float64'
kgryte marked this conversation as resolved.
Show resolved Hide resolved
};


// FUNCTIONS //

/**
Expand All @@ -39,16 +47,8 @@ var gcusumkbn = require( './../lib/main.js' );
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var y;
var x;
var i;

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

function benchmark( b ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,22 @@
// 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 gfill = require( '@stdlib/blas/ext/base/gfill' );
var Float64Array = require( '@stdlib/array/float64' );
var pkg = require( './../package.json' ).name;
var gcusumkbn = require( './../lib/ndarray.js' );


// VARIABLES //

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


// FUNCTIONS //

/**
Expand All @@ -39,16 +47,8 @@ var gcusumkbn = require( './../lib/ndarray.js' );
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var x;
var y;
var i;

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

function benchmark( b ) {
Expand Down
28 changes: 13 additions & 15 deletions lib/node_modules/@stdlib/blas/ext/base/gcusumkbn/docs/repl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
Computes the cumulative sum of strided array elements using an improved
Kahan–Babuška algorithm.

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

Indexing is relative to the first index. To introduce an offset, use a typed
array view.
Expand All @@ -23,13 +23,13 @@
Input array.

strideX: integer
Index increment for `x`.
Stride length for `x`.

y: Array<number>|TypedArray
Output array.

strideY: integer
Index increment for `y`.
Stride length for `y`.

Returns
-------
Expand All @@ -44,31 +44,30 @@
> {{alias}}( x.length, 0.0, x, 1, y, 1 )
[ 1.0, -1.0, 1.0 ]

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

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


{{alias}}.ndarray( N, sum, x, strideX, offsetX, y, strideY, offsetY )
Computes the cumulative sum of strided array elements using an improved
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
starting index.
buffer, the offset parameter support indexing semantics based on starting
kgryte marked this conversation as resolved.
Show resolved Hide resolved
indices.

Parameters
----------
Expand All @@ -82,7 +81,7 @@
Input array.

strideX: integer
Index increment for `x`.
Stride length for `x`.

offsetX: integer
Starting index for `x`.
Expand All @@ -91,7 +90,7 @@
Output array.

strideY: integer
Index increment for `y`.
Stride length for `y`.

offsetY: integer
Starting index for `y`.
Expand All @@ -112,8 +111,7 @@
// Advanced indexing:
> x = [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ];
> y = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ];
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
> {{alias}}.ndarray( N, 0.0, x, 2, 1, y, -1, y.length-1 )
> {{alias}}.ndarray( 3, 0.0, x, 2, 1, y, -1, y.length-1 )
<Float64Array>[ 0.0, 0.0, 0.0, -1.0, 0.0, -2.0 ]

See Also
Expand Down
17 changes: 6 additions & 11 deletions lib/node_modules/@stdlib/blas/ext/base/gcusumkbn/examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,15 @@

'use strict';

var randu = require( '@stdlib/random/base/randu' );
var round = require( '@stdlib/math/base/special/round' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var Float64Array = require( '@stdlib/array/float64' );
var gcusumkbn = require( './../lib' );
var gcusumkbn = require( '@stdlib/blas/ext/base/gcusumkbn' );
headlessNode marked this conversation as resolved.
Show resolved Hide resolved

var y;
var x;
var i;
var x = discreteUniform( 10.0, -100, 100, {
kgryte marked this conversation as resolved.
Show resolved Hide resolved
'dtype': 'float64'
});
var y = new Float64Array( x.length );

x = new Float64Array( 10 );
y = new Float64Array( x.length );
for ( i = 0; i < x.length; i++ ) {
x[ i ] = round( randu()*100.0 );
}
console.log( x );
console.log( y );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@
* // y => [ 1.0, -1.0, 1.0 ]
*
* @example
* var floor = require( '@stdlib/math/base/special/floor' );
* var gcusumkbn = require( '@stdlib/blas/ext/base/gcusumkbn' );
*
* var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ];
* var y = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ];
* var N = floor( x.length / 2 );
*
* gcusumkbn.ndarray( N, 0.0, x, 2, 1, y, 1, 0 );
* gcusumkbn.ndarray( 4, 0.0, x, 2, 1, y, 1, 0 );
* // y => [ 1.0, -1.0, 1.0, 5.0, 0.0, 0.0, 0.0, 0.0 ]
*/

Expand Down
43 changes: 5 additions & 38 deletions lib/node_modules/@stdlib/blas/ext/base/gcusumkbn/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@

// MODULES //

var abs = require( '@stdlib/math/base/special/abs' );
var stride2offset = require( '@stdlib/strided/base/stride2offset' );
var ndarray = require( './ndarray.js' );


// MAIN //
Expand Down Expand Up @@ -52,43 +53,9 @@ var abs = require( '@stdlib/math/base/special/abs' );
* // returns [ 1.0, -1.0, 1.0 ]
*/
function gcusumkbn( N, sum, x, strideX, y, strideY ) {
var ix;
var iy;
var s;
var v;
var t;
var c;
var i;

if ( N <= 0 ) {
return y;
}
if ( strideX < 0 ) {
ix = (1-N) * strideX;
} else {
ix = 0;
}
if ( strideY < 0 ) {
iy = (1-N) * strideY;
} else {
iy = 0;
}
s = sum;
c = 0.0;
for ( i = 0; i < N; i++ ) {
v = x[ ix ];
t = s + v;
if ( abs( s ) >= abs( v ) ) {
c += (s-t) + v;
} else {
c += (v-t) + s;
}
s = t;
y[ iy ] = s + c;
ix += strideX;
iy += strideY;
}
return y;
var ox = stride2offset( N, strideX );
var oy = stride2offset( N, strideY );
return ndarray( N, sum, x, strideX, ox, y, strideY, oy );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,10 @@ var abs = require( '@stdlib/math/base/special/abs' );
* @returns {NumericArray} output array
*
* @example
* 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 y = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ];
* var N = floor( x.length / 2 );
*
* gcusumkbn( N, 0.0, x, 2, 1, y, 1, 0 );
* gcusumkbn( 4, 0.0, x, 2, 1, y, 1, 0 );
* // y => [ 1.0, -1.0, 1.0, 5.0, 0.0, 0.0, 0.0, 0.0 ]
*/
function gcusumkbn( N, sum, x, strideX, offsetX, y, strideY, offsetY ) {
Expand Down
Loading