Skip to content

Commit bb638df

Browse files
authored
refactor: update blas/ext/base/gnannsumkbn to follow current project conventions
PR-URL: #4631 Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com> Signed-off-by: Muhammad Haris <101793258+headlessNode@users.noreply.github.com>
1 parent 3901a45 commit bb638df

File tree

11 files changed

+151
-198
lines changed

11 files changed

+151
-198
lines changed

lib/node_modules/@stdlib/blas/ext/base/gnannsumkbn/README.md

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,17 @@ The function has the following parameters:
5252

5353
- **N**: number of indexed elements.
5454
- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
55-
- **strideX**: index increment for `x`.
55+
- **strideX**: stride length for `x`.
5656
- **out**: output [`Array`][mdn-array] or [`typed array`][mdn-typed-array] whose first element is the sum and whose second element is the number of non-NaN elements.
57-
- **strideOut**: index increment for `out`.
57+
- **strideOut**: stride length for `out`.
5858

59-
The `N` and `stride` parameters determine which elements are accessed at runtime. For example, to compute the sum of every other element in `x`,
59+
The `N` and stride parameters determine which elements are accessed at runtime. For example, to compute the sum of every other element:
6060

6161
```javascript
62-
var floor = require( '@stdlib/math/base/special/floor' );
63-
6462
var x = [ 1.0, 2.0, NaN, -7.0, NaN, 3.0, 4.0, 2.0 ];
6563
var out = [ 0.0, 0 ];
66-
var N = floor( x.length / 2 );
6764

68-
var v = gnannsumkbn( N, x, 2, out, 1 );
65+
var v = gnannsumkbn( 4, x, 2, out, 1 );
6966
// returns [ 5.0, 2 ]
7067
```
7168

@@ -75,17 +72,14 @@ Note that indexing is relative to the first index. To introduce an offset, use [
7572

7673
```javascript
7774
var Float64Array = require( '@stdlib/array/float64' );
78-
var floor = require( '@stdlib/math/base/special/floor' );
7975

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

8379
var out0 = new Float64Array( 4 );
8480
var out1 = new Float64Array( out0.buffer, out0.BYTES_PER_ELEMENT*2 ); // start at 3rd element
8581

86-
var N = floor( x0.length / 2 );
87-
88-
var v = gnannsumkbn( N, x1, 2, out1, 1 );
82+
var v = gnannsumkbn( 4, x1, 2, out1, 1 );
8983
// returns <Float64Array>[ 5.0, 4 ]
9084
```
9185

@@ -106,16 +100,13 @@ The function has the following additional parameters:
106100
- **offsetX**: starting index for `x`.
107101
- **offsetOut**: starting index for `out`.
108102

109-
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 calculate the sum of every other value in `x` starting from the second value
103+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameters support indexing semantics based on starting indices. For example, to calculate the sum of every other element starting from the second element:
110104

111105
```javascript
112-
var floor = require( '@stdlib/math/base/special/floor' );
113-
114106
var x = [ 2.0, 1.0, NaN, -2.0, -2.0, 2.0, 3.0, 4.0 ];
115107
var out = [ 0.0, 0.0, 0.0, 0 ];
116-
var N = floor( x.length / 2 );
117108

118-
var v = gnannsumkbn.ndarray( N, x, 2, 1, out, 2, 1 );
109+
var v = gnannsumkbn.ndarray( 4, x, 2, 1, out, 2, 1 );
119110
// returns <Float64Array>[ 0.0, 5.0, 0.0, 4 ]
120111
```
121112

@@ -140,22 +131,20 @@ var v = gnannsumkbn.ndarray( N, x, 2, 1, out, 2, 1 );
140131
<!-- eslint no-undef: "error" -->
141132

142133
```javascript
143-
var randu = require( '@stdlib/random/base/randu' );
144-
var round = require( '@stdlib/math/base/special/round' );
134+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
135+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
136+
var filledarrayBy = require( '@stdlib/array/filled-by' );
145137
var Float64Array = require( '@stdlib/array/float64' );
146138
var gnannsumkbn = require( '@stdlib/blas/ext/base/gnannsumkbn' );
147139

148-
var x;
149-
var i;
150-
151-
x = new Float64Array( 10 );
152-
for ( i = 0; i < x.length; i++ ) {
153-
if ( randu() < 0.2 ) {
154-
x[ i ] = NaN;
155-
} else {
156-
x[ i ] = round( randu()*100.0 );
140+
function rand() {
141+
if ( bernoulli( 0.8 ) > 0 ) {
142+
return discreteUniform( 0, 100 );
157143
}
144+
return NaN;
158145
}
146+
147+
var x = filledarrayBy( 10, 'float64', rand );
159148
console.log( x );
160149

161150
var out = new Float64Array( 2 );

lib/node_modules/@stdlib/blas/ext/base/gnannsumkbn/benchmark/benchmark.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,31 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
24+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
25+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
26+
var filledarrayBy = require( '@stdlib/array/filled-by' );
2527
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2628
var pow = require( '@stdlib/math/base/special/pow' );
29+
var zeros = require( '@stdlib/array/base/zeros' );
2730
var pkg = require( './../package.json' ).name;
2831
var gnannsumkbn = require( './../lib/main.js' );
2932

3033

3134
// FUNCTIONS //
3235

36+
/**
37+
* Returns a random number.
38+
*
39+
* @private
40+
* @returns {number} random number
41+
*/
42+
function rand() {
43+
if ( bernoulli( 0.7 ) > 0 ) {
44+
return discreteUniform( -10.0, 10.0 );
45+
}
46+
return NaN;
47+
}
48+
3349
/**
3450
* Creates a benchmark function.
3551
*
@@ -38,19 +54,8 @@ var gnannsumkbn = require( './../lib/main.js' );
3854
* @returns {Function} benchmark function
3955
*/
4056
function createBenchmark( len ) {
41-
var out;
42-
var x;
43-
var i;
44-
45-
x = [];
46-
for ( i = 0; i < len; i++ ) {
47-
if ( randu() < 0.2 ) {
48-
x.push( NaN );
49-
} else {
50-
x.push( ( randu()*10.0 ) - 20.0 );
51-
}
52-
}
53-
out = [ 0.0, 0 ];
57+
var out = zeros( 2 );
58+
var x = filledarrayBy( len, 'generic', rand );
5459
return benchmark;
5560

5661
function benchmark( b ) {

lib/node_modules/@stdlib/blas/ext/base/gnannsumkbn/benchmark/benchmark.ndarray.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,31 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
24+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
25+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
26+
var filledarrayBy = require( '@stdlib/array/filled-by' );
2527
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2628
var pow = require( '@stdlib/math/base/special/pow' );
29+
var zeros = require( '@stdlib/array/base/zeros' );
2730
var pkg = require( './../package.json' ).name;
2831
var gnannsumkbn = require( './../lib/ndarray.js' );
2932

3033

3134
// FUNCTIONS //
3235

36+
/**
37+
* Returns a random number.
38+
*
39+
* @private
40+
* @returns {number} random number
41+
*/
42+
function rand() {
43+
if ( bernoulli( 0.7 ) > 0 ) {
44+
return discreteUniform( -10.0, 10.0 );
45+
}
46+
return NaN;
47+
}
48+
3349
/**
3450
* Creates a benchmark function.
3551
*
@@ -38,19 +54,8 @@ var gnannsumkbn = require( './../lib/ndarray.js' );
3854
* @returns {Function} benchmark function
3955
*/
4056
function createBenchmark( len ) {
41-
var out;
42-
var x;
43-
var i;
44-
45-
x = [];
46-
for ( i = 0; i < len; i++ ) {
47-
if ( randu() < 0.2 ) {
48-
x.push( NaN );
49-
} else {
50-
x.push( ( randu()*10.0 ) - 20.0 );
51-
}
52-
}
53-
out = [ 0.0, 0 ];
57+
var out = zeros( 2 );
58+
var x = filledarrayBy( len, 'generic', rand );
5459
return benchmark;
5560

5661
function benchmark( b ) {

lib/node_modules/@stdlib/blas/ext/base/gnannsumkbn/docs/repl.txt

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Computes the sum of strided array elements, ignoring `NaN` values and using
44
an improved Kahan–Babuška algorithm.
55

6-
The `N` and `stride` parameters determine which elements are accessed at
6+
The `N` and stride parameters determine which elements are accessed at
77
runtime.
88

99
Indexing is relative to the first index. To introduce an offset, use a typed
@@ -20,13 +20,13 @@
2020
Input array.
2121

2222
strideX: integer
23-
Index increment for `x`.
23+
Stride length for `x`.
2424

2525
out: Array<number>|TypedArray
2626
Output array.
2727

2828
strideOut: integer
29-
Index increment for `out`.
29+
Stride length for `out`.
3030

3131
Returns
3232
-------
@@ -42,28 +42,27 @@
4242
> {{alias}}( x.length, x, 1, out, 1 )
4343
[ 1.0, 3 ]
4444

45-
// Using `N` and `stride` parameters:
45+
// Using `N` and stride parameters:
4646
> x = [ -2.0, 1.0, 1.0, -5.0, 2.0, -1.0, NaN, NaN ];
4747
> out = [ 0.0, 0 ];
48-
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
49-
> {{alias}}( N, x, 2, out, 1 )
48+
> {{alias}}( 4, x, 2, out, 1 )
5049
[ 1.0, 3 ]
5150

5251
// Using view offsets:
5352
> var x0 = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, 1.0, NaN, NaN ] );
5453
> var x1 = new {{alias:@stdlib/array/float64}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 );
55-
> N = {{alias:@stdlib/math/base/special/floor}}( x0.length / 2 );
5654
> out = [ 0.0, 0 ];
57-
> {{alias}}( N, x1, 2, out, 1 )
55+
> {{alias}}( 4, x1, 2, out, 1 )
5856
<Float64Array>[ 1.0, 3 ]
5957

58+
6059
{{alias}}.ndarray( N, x, strideX, offsetX, out, strideOut, offsetOut )
6160
Computes the sum of strided array elements, ignoring `NaN` values and using
6261
an improved Kahan–Babuška algorithm and alternative indexing semantics.
6362

6463
While typed array views mandate a view offset based on the underlying
65-
buffer, the `offset` parameter supports indexing semantics based on a
66-
starting index.
64+
buffer, the offset parameters support indexing semantics based on a starting
65+
indices.
6766

6867
Parameters
6968
----------
@@ -74,7 +73,7 @@
7473
Input array.
7574

7675
strideX: integer
77-
Index increment for `x`.
76+
Stride length for `x`.
7877

7978
offsetX: integer
8079
Starting index for `x`.
@@ -83,7 +82,7 @@
8382
Output array.
8483

8584
strideOut: integer
86-
Index increment for `out`.
85+
Stride length for `out`.
8786

8887
offsetOut: integer
8988
Starting index for `out`.
@@ -104,9 +103,8 @@
104103

105104
// Using offset parameter:
106105
> var x = [ 1.0, -2.0, 3.0, 2.0, 5.0, 1.0, NaN, NaN ];
107-
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
108106
> out = [ 0.0, 0 ];
109-
> {{alias}}.ndarray( N, x, 2, 1, out, 1, 0 )
107+
> {{alias}}.ndarray( 4, x, 2, 1, out, 1, 0 )
110108
[ 1.0, 3 ]
111109

112110
See Also

lib/node_modules/@stdlib/blas/ext/base/gnannsumkbn/docs/types/index.d.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ interface Routine {
3131
*
3232
* @param N - number of indexed elements
3333
* @param x - input array
34-
* @param strideX - `x` stride length
34+
* @param strideX - stride length for `x`
3535
* @param out - output array whose first element is the sum and whose second element is the number of non-NaN elements
36-
* @param strideOut - `out` stride length
36+
* @param strideOut - stride length for `out`
3737
* @returns output array
3838
*
3939
* @example
@@ -50,11 +50,11 @@ interface Routine {
5050
*
5151
* @param N - number of indexed elements
5252
* @param x - input array
53-
* @param strideX - `x` stride length
54-
* @param offsetX - `x` starting index
53+
* @param strideX - stride length for `x`
54+
* @param offsetX - starting index for `x`
5555
* @param out - output array whose first element is the sum and whose second element is the number of non-NaN elements
56-
* @param strideOut - `out` stride length
57-
* @param offsetOut - `out` starting index
56+
* @param strideOut - stride length for `out`
57+
* @param offsetOut - starting index for `out`
5858
* @returns output array
5959
*
6060
* @example
@@ -72,9 +72,9 @@ interface Routine {
7272
*
7373
* @param N - number of indexed elements
7474
* @param x - input array
75-
* @param strideX - `x` stride length
75+
* @param strideX - stride length for `x`
7676
* @param out - output array whose first element is the sum and whose second element is the number of non-NaN elements
77-
* @param strideOut - `out` stride length
77+
* @param strideOut - stride length for `out`
7878
* @returns output array
7979
*
8080
* @example

lib/node_modules/@stdlib/blas/ext/base/gnannsumkbn/examples/index.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,22 @@
1818

1919
'use strict';
2020

21-
var randu = require( '@stdlib/random/base/randu' );
22-
var round = require( '@stdlib/math/base/special/round' );
21+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
22+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
23+
var filledarrayBy = require( '@stdlib/array/filled-by' );
2324
var Float64Array = require( '@stdlib/array/float64' );
2425
var gnannsumkbn = require( './../lib' );
2526

26-
var out;
27-
var x;
28-
var i;
29-
30-
x = new Float64Array( 10 );
31-
for ( i = 0; i < x.length; i++ ) {
32-
if ( randu() < 0.2 ) {
33-
x[ i ] = NaN;
34-
} else {
35-
x[ i ] = round( randu()*100.0 );
27+
function rand() {
28+
if ( bernoulli( 0.8 ) > 0 ) {
29+
return discreteUniform( 0, 100 );
3630
}
31+
return NaN;
3732
}
33+
34+
var x = filledarrayBy( 10, 'float64', rand );
3835
console.log( x );
3936

40-
out = new Float64Array( 2 );
37+
var out = new Float64Array( 2 );
4138
gnannsumkbn( x.length, x, 1, out, 1 );
4239
console.log( out );

lib/node_modules/@stdlib/blas/ext/base/gnannsumkbn/lib/index.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,12 @@
3333
* // returns [ 1.0, 3 ]
3434
*
3535
* @example
36-
* var floor = require( '@stdlib/math/base/special/floor' );
3736
* var gnannsumkbn = require( '@stdlib/blas/ext/base/gnannsumkbn' );
3837
*
3938
* var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ];
4039
* var out = [ 0.0, 0 ];
4140
*
42-
* var N = floor( x.length / 2 );
43-
*
44-
* var v = gnannsumkbn.ndarray( N, x, 2, 1, out, 1, 0 );
41+
* var v = gnannsumkbn.ndarray( 5, x, 2, 1, out, 1, 0 );
4542
* // returns [ 5.0, 4 ]
4643
*/
4744

0 commit comments

Comments
 (0)