Skip to content

Commit 30fa067

Browse files
aayush0325kgryteheadlessNode
authored andcommitted
feat: add C ndarray interface and refactor implementation for stats/base/dnanmeanpw
PR-URL: stdlib-js#4763 Co-authored-by: Athan Reines <[email protected]> Co-authored-by: Muhammad Haris <[email protected]> Reviewed-by: Athan Reines <[email protected]> Reviewed-by: Muhammad Haris <[email protected]>
1 parent db63e0f commit 30fa067

26 files changed

+514
-765
lines changed

lib/node_modules/@stdlib/stats/base/dnanmeanpw/README.md

Lines changed: 126 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -51,84 +51,77 @@ The [arithmetic mean][arithmetic-mean] is defined as
5151
var dnanmeanpw = require( '@stdlib/stats/base/dnanmeanpw' );
5252
```
5353

54-
#### dnanmeanpw( N, x, stride )
54+
#### dnanmeanpw( N, x, strideX )
5555

5656
Computes the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array `x`, ignoring `NaN` values and using pairwise summation.
5757

5858
```javascript
5959
var Float64Array = require( '@stdlib/array/float64' );
6060

6161
var x = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] );
62-
var N = x.length;
6362

64-
var v = dnanmeanpw( N, x, 1 );
63+
var v = dnanmeanpw( x.length, x, 1 );
6564
// returns ~0.3333
6665
```
6766

6867
The function has the following parameters:
6968

7069
- **N**: number of indexed elements.
7170
- **x**: input [`Float64Array`][@stdlib/array/float64].
72-
- **stride**: index increment for `x`.
71+
- **strideX**: stride length.
7372

74-
The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to compute the [arithmetic mean][arithmetic-mean] of every other element in `x`,
73+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the [arithmetic mean][arithmetic-mean] of every other element:
7574

7675
```javascript
7776
var Float64Array = require( '@stdlib/array/float64' );
78-
var floor = require( '@stdlib/math/base/special/floor' );
7977

8078
var x = new Float64Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0, NaN ] );
81-
var N = floor( x.length / 2 );
8279

83-
var v = dnanmeanpw( N, x, 2 );
80+
var v = dnanmeanpw( 5, x, 2 );
8481
// returns 1.25
8582
```
8683

8784
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
8885

89-
<!-- eslint-disable stdlib/capitalized-comments -->
86+
<!-- eslint-disable stdlib/capitalized-comments, max-len -->
9087

9188
```javascript
9289
var Float64Array = require( '@stdlib/array/float64' );
93-
var floor = require( '@stdlib/math/base/special/floor' );
9490

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

98-
var N = floor( x0.length / 2 );
99-
100-
var v = dnanmeanpw( N, x1, 2 );
94+
var v = dnanmeanpw( 5, x1, 2 );
10195
// returns 1.25
10296
```
10397

104-
#### dnanmeanpw.ndarray( N, x, stride, offset )
98+
#### dnanmeanpw.ndarray( N, x, strideX, offsetX )
10599

106100
Computes the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array, ignoring `NaN` values and using pairwise summation and alternative indexing semantics.
107101

108102
```javascript
109103
var Float64Array = require( '@stdlib/array/float64' );
110104

111105
var x = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] );
112-
var N = x.length;
113106

114-
var v = dnanmeanpw.ndarray( N, x, 1, 0 );
107+
var v = dnanmeanpw.ndarray( x.length, x, 1, 0 );
115108
// returns ~0.33333
116109
```
117110

118111
The function has the following additional parameters:
119112

120-
- **offset**: starting index for `x`.
113+
- **offsetX**: starting index.
121114

122-
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 [arithmetic mean][arithmetic-mean] for every other value in `x` starting from the second value
115+
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 [arithmetic mean][arithmetic-mean] for every other element starting from the second element:
116+
117+
<!-- eslint-disable max-len -->
123118

124119
```javascript
125120
var Float64Array = require( '@stdlib/array/float64' );
126-
var floor = require( '@stdlib/math/base/special/floor' );
127121

128-
var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN ] );
129-
var N = floor( x.length / 2 );
122+
var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] );
130123

131-
var v = dnanmeanpw.ndarray( N, x, 2, 1 );
124+
var v = dnanmeanpw.ndarray( 5, x, 2, 1 );
132125
// returns 1.25
133126
```
134127

@@ -155,22 +148,19 @@ var v = dnanmeanpw.ndarray( N, x, 2, 1 );
155148
<!-- eslint no-undef: "error" -->
156149

157150
```javascript
158-
var randu = require( '@stdlib/random/base/randu' );
159-
var round = require( '@stdlib/math/base/special/round' );
160-
var Float64Array = require( '@stdlib/array/float64' );
151+
var uniform = require( '@stdlib/random/base/uniform' );
152+
var filledarrayBy = require( '@stdlib/array/filled-by' );
153+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
161154
var dnanmeanpw = require( '@stdlib/stats/base/dnanmeanpw' );
162155

163-
var x;
164-
var i;
165-
166-
x = new Float64Array( 10 );
167-
for ( i = 0; i < x.length; i++ ) {
168-
if ( randu() < 0.2 ) {
169-
x[ i ] = NaN;
170-
} else {
171-
x[ i ] = round( (randu()*100.0) - 50.0 );
156+
function rand() {
157+
if ( bernoulli( 0.8 ) < 1 ) {
158+
return NaN;
172159
}
160+
return uniform( -50.0, 50.0 );
173161
}
162+
163+
var x = filledarrayBy( 10, 'float64', rand );
174164
console.log( x );
175165

176166
var v = dnanmeanpw( x.length, x, 1 );
@@ -181,6 +171,107 @@ console.log( v );
181171

182172
<!-- /.examples -->
183173

174+
<!-- C usage documentation. -->
175+
176+
<section class="usage">
177+
178+
### Usage
179+
180+
```c
181+
#include "stdlib/stats/base/dnanmeanpw.h"
182+
```
183+
184+
#### stdlib_strided_dnanmeanpw( N, \*X, strideX )
185+
186+
Computes the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array, ignoring `NaN` values and using pairwise summation.
187+
188+
```c
189+
const double x[] = { 1.0, -2.0, 0.0/0.0, 2.0 };
190+
191+
double v = stdlib_strided_dnanmeanpw( 4, x, 1 );
192+
// returns ~0.3333
193+
```
194+
195+
The function accepts the following arguments:
196+
197+
- **N**: `[in] CBLAS_INT` number of indexed elements.
198+
- **X**: `[in] double*` input array.
199+
- **strideX**: `[in] CBLAS_INT` stride length.
200+
201+
```c
202+
double stdlib_strided_dnanmeanpw( const CBLAS_INT N, const double *X, const CBLAS_INT strideX );
203+
```
204+
205+
#### stdlib_strided_dnanmeanpw_ndarray( N, \*X, strideX, offsetX )
206+
207+
Computes the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array, ignoring `NaN` values and using pairwise summation and alternative indexing semantics.
208+
209+
```c
210+
const double x[] = { 1.0, -2.0, 0.0/0.0, 2.0 };
211+
212+
double v = stdlib_strided_dnanmeanpw_ndarray( 4, x, 1, 0 );
213+
// returns ~0.3333
214+
```
215+
216+
The function accepts the following arguments:
217+
218+
- **N**: `[in] CBLAS_INT` number of indexed elements.
219+
- **X**: `[in] double*` input array.
220+
- **strideX**: `[in] CBLAS_INT` stride length.
221+
- **offsetX**: `[in] CBLAS_INT` starting index.
222+
223+
```c
224+
double stdlib_strided_dnanmeanpw_ndarray( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
225+
```
226+
227+
</section>
228+
229+
<!-- /.usage -->
230+
231+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
232+
233+
<section class="notes">
234+
235+
</section>
236+
237+
<!-- /.notes -->
238+
239+
<!-- C API usage examples. -->
240+
241+
<section class="examples">
242+
243+
### Examples
244+
245+
```c
246+
#include "stdlib/stats/base/dnanmeanpw.h"
247+
#include <stdio.h>
248+
249+
int main( void ) {
250+
// Create a strided array:
251+
const double x[] = { 1.0, 2.0, 0.0/0.0, 3.0, 0.0/0.0, 4.0, 5.0, 6.0, 0.0/0.0, 7.0, 8.0, 0.0/0.0 };
252+
253+
// Specify the number of elements:
254+
const int N = 6;
255+
256+
// Specify the stride length:
257+
const int strideX = 2;
258+
259+
// Compute the arithmetic mean:
260+
double v = stdlib_strided_dnanmeanpw( N, x, strideX );
261+
262+
// Print the result:
263+
printf( "mean: %lf\n", v );
264+
}
265+
```
266+
267+
</section>
268+
269+
<!-- /.examples -->
270+
271+
</section>
272+
273+
<!-- /.c -->
274+
184275
* * *
185276
186277
<section class="references">

lib/node_modules/@stdlib/stats/base/dnanmeanpw/benchmark/benchmark.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,30 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
2524
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2625
var pow = require( '@stdlib/math/base/special/pow' );
27-
var Float64Array = require( '@stdlib/array/float64' );
26+
var uniform = require( '@stdlib/random/base/uniform' );
27+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
28+
var filledarrayBy = require( '@stdlib/array/filled-by' );
2829
var pkg = require( './../package.json' ).name;
2930
var dnanmeanpw = require( './../lib/dnanmeanpw.js' );
3031

3132

3233
// FUNCTIONS //
3334

35+
/**
36+
* Returns a random value or `NaN`.
37+
*
38+
* @private
39+
* @returns {number} random number or `NaN`
40+
*/
41+
function rand() {
42+
if ( bernoulli( 0.2 ) ) {
43+
return NaN;
44+
}
45+
return uniform( -10.0, 10.0 );
46+
}
47+
3448
/**
3549
* Creates a benchmark function.
3650
*
@@ -39,17 +53,7 @@ var dnanmeanpw = require( './../lib/dnanmeanpw.js' );
3953
* @returns {Function} benchmark function
4054
*/
4155
function createBenchmark( len ) {
42-
var x;
43-
var i;
44-
45-
x = new Float64Array( len );
46-
for ( i = 0; i < x.length; i++ ) {
47-
if ( randu() < 0.2 ) {
48-
x[ i ] = NaN;
49-
} else {
50-
x[ i ] = ( randu()*20.0 ) - 10.0;
51-
}
52-
}
56+
var x = filledarrayBy( len, 'float64', rand );
5357
return benchmark;
5458

5559
function benchmark( b ) {

lib/node_modules/@stdlib/stats/base/dnanmeanpw/benchmark/benchmark.native.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@
2222

2323
var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
25-
var randu = require( '@stdlib/random/base/randu' );
2625
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2726
var pow = require( '@stdlib/math/base/special/pow' );
28-
var Float64Array = require( '@stdlib/array/float64' );
27+
var uniform = require( '@stdlib/random/base/uniform' );
28+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
29+
var filledarrayBy = require( '@stdlib/array/filled-by' );
2930
var tryRequire = require( '@stdlib/utils/try-require' );
3031
var pkg = require( './../package.json' ).name;
3132

@@ -40,6 +41,19 @@ var opts = {
4041

4142
// FUNCTIONS //
4243

44+
/**
45+
* Returns a random value or `NaN`.
46+
*
47+
* @private
48+
* @returns {number} random number or `NaN`
49+
*/
50+
function rand() {
51+
if ( bernoulli( 0.2 ) ) {
52+
return NaN;
53+
}
54+
return uniform( -10.0, 10.0 );
55+
}
56+
4357
/**
4458
* Creates a benchmark function.
4559
*
@@ -48,17 +62,7 @@ var opts = {
4862
* @returns {Function} benchmark function
4963
*/
5064
function createBenchmark( len ) {
51-
var x;
52-
var i;
53-
54-
x = new Float64Array( len );
55-
for ( i = 0; i < x.length; i++ ) {
56-
if ( randu() < 0.2 ) {
57-
x[ i ] = NaN;
58-
} else {
59-
x[ i ] = ( randu()*20.0 ) - 10.0;
60-
}
61-
}
65+
var x = filledarrayBy( len, 'float64', rand );
6266
return benchmark;
6367

6468
function benchmark( b ) {

lib/node_modules/@stdlib/stats/base/dnanmeanpw/benchmark/benchmark.ndarray.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,30 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
2524
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2625
var pow = require( '@stdlib/math/base/special/pow' );
27-
var Float64Array = require( '@stdlib/array/float64' );
26+
var uniform = require( '@stdlib/random/base/uniform' );
27+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
28+
var filledarrayBy = require( '@stdlib/array/filled-by' );
2829
var pkg = require( './../package.json' ).name;
2930
var dnanmeanpw = require( './../lib/ndarray.js' );
3031

3132

3233
// FUNCTIONS //
3334

35+
/**
36+
* Returns a random value or `NaN`.
37+
*
38+
* @private
39+
* @returns {number} random number or `NaN`
40+
*/
41+
function rand() {
42+
if ( bernoulli( 0.2 ) ) {
43+
return NaN;
44+
}
45+
return uniform( -10.0, 10.0 );
46+
}
47+
3448
/**
3549
* Creates a benchmark function.
3650
*
@@ -39,17 +53,7 @@ var dnanmeanpw = require( './../lib/ndarray.js' );
3953
* @returns {Function} benchmark function
4054
*/
4155
function createBenchmark( len ) {
42-
var x;
43-
var i;
44-
45-
x = new Float64Array( len );
46-
for ( i = 0; i < x.length; i++ ) {
47-
if ( randu() < 0.2 ) {
48-
x[ i ] = NaN;
49-
} else {
50-
x[ i ] = ( randu()*20.0 ) - 10.0;
51-
}
52-
}
56+
var x = filledarrayBy( len, 'float64', rand );
5357
return benchmark;
5458

5559
function benchmark( b ) {

0 commit comments

Comments
 (0)