Skip to content

Commit ea4fd1d

Browse files
Neerajpathak07kgrytestdlib-bot
authored
feat: add C ndarray interface and refactor implementation for stats/base/dnanmeanors
PR-URL: #4248 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Reviewed-by: Aayush Khanna <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent d66db3f commit ea4fd1d

23 files changed

+457
-304
lines changed

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

+120-24
Original file line numberDiff line numberDiff line change
@@ -51,84 +51,79 @@ The [arithmetic mean][arithmetic-mean] is defined as
5151
var dnanmeanors = require( '@stdlib/stats/base/dnanmeanors' );
5252
```
5353

54-
#### dnanmeanors( N, x, stride )
54+
#### dnanmeanors( 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 ordinary recursive 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 = dnanmeanors( N, x, 1 );
63+
var v = dnanmeanors( 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 for `x`.
72+
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 in `x`,
7374

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`,
75+
<!-- eslint-disable max-len -->
7576

7677
```javascript
7778
var Float64Array = require( '@stdlib/array/float64' );
78-
var floor = require( '@stdlib/math/base/special/floor' );
7979

80-
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 );
80+
var x = new Float64Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0, NaN, NaN ] );
8281

83-
var v = dnanmeanors( N, x, 2 );
82+
var v = dnanmeanors( 5, x, 2 );
8483
// returns 1.25
8584
```
8685

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

89-
<!-- eslint-disable stdlib/capitalized-comments -->
88+
<!-- eslint-disable stdlib/capitalized-comments, max-len -->
9089

9190
```javascript
9291
var Float64Array = require( '@stdlib/array/float64' );
93-
var floor = require( '@stdlib/math/base/special/floor' );
9492

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

98-
var N = floor( x0.length / 2 );
99-
100-
var v = dnanmeanors( N, x1, 2 );
96+
var v = dnanmeanors( 5, x1, 2 );
10197
// returns 1.25
10298
```
10399

104-
#### dnanmeanors.ndarray( N, x, stride, offset )
100+
#### dnanmeanors.ndarray( N, x, strideX, offsetX )
105101

106102
Computes the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array, ignoring `NaN` values and using ordinary recursive summation and alternative indexing semantics.
107103

108104
```javascript
109105
var Float64Array = require( '@stdlib/array/float64' );
110106

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

114-
var v = dnanmeanors.ndarray( N, x, 1, 0 );
109+
var v = dnanmeanors.ndarray( x.length, x, 1, 0 );
115110
// returns ~0.33333
116111
```
117112

118113
The function has the following additional parameters:
119114

120-
- **offset**: starting index for `x`.
115+
- **offsetX**: starting index for `x`.
116+
117+
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 in `x` starting from the second element
121118

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
119+
<!-- eslint-disable max-len -->
123120

124121
```javascript
125122
var Float64Array = require( '@stdlib/array/float64' );
126-
var floor = require( '@stdlib/math/base/special/floor' );
127123

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 );
124+
var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] );
130125

131-
var v = dnanmeanors.ndarray( N, x, 2, 1 );
126+
var v = dnanmeanors.ndarray( 5, x, 2, 1 );
132127
// returns 1.25
133128
```
134129

@@ -181,6 +176,107 @@ console.log( v );
181176

182177
<!-- /.examples -->
183178

179+
<!-- C usage documentation. -->
180+
181+
<section class="usage">
182+
183+
### Usage
184+
185+
```c
186+
#include "stdlib/stats/base/dnanmeanors.h"
187+
```
188+
189+
#### stdlib_strided_dnanmeanors( N, \*X, strideX )
190+
191+
Computes the arithmetic mean of a double-precision floating-point strided array, ignoring `NaN` values and using ordinary recursive summation.
192+
193+
```c
194+
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 };
195+
196+
double v = stdlib_strided_dnanmeanors( 6, x, 2 );
197+
// returns ~4.6667
198+
```
199+
200+
The function accepts the following arguments:
201+
202+
- **N**: `[in] CBLAS_INT` number of indexed elements.
203+
- **X**: `[in] double*` input array.
204+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
205+
206+
```c
207+
double stdlib_strided_dnanmeanors( const CBLAS_INT N, const double *X, const CBLAS_INT strideX );
208+
```
209+
210+
#### stdlib_strided_dnanmeanors_ndarray( N, \*X, strideX, offsetX )
211+
212+
Computes the arithmetic mean of a double-precision floating-point strided array, ignoring `NaN` values and using ordinary recursive summation and alternative indexing semantics.
213+
214+
```c
215+
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 };
216+
217+
double v = stdlib_strided_dnanmeanors_ndarray( 6, x, 2, 0 );
218+
// returns ~4.6667
219+
```
220+
221+
The function accepts the following arguments:
222+
223+
- **N**: `[in] CBLAS_INT` number of indexed elements.
224+
- **X**: `[in] double*` input array.
225+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
226+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
227+
228+
```c
229+
double stdlib_strided_dnanmeanors_ndarray( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
230+
```
231+
232+
</section>
233+
234+
<!-- /.usage -->
235+
236+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
237+
238+
<section class="notes">
239+
240+
</section>
241+
242+
<!-- /.notes -->
243+
244+
<!-- C API usage examples. -->
245+
246+
<section class="examples">
247+
248+
### Examples
249+
250+
```c
251+
#include "stdlib/stats/base/dnanmeanors.h"
252+
#include <stdio.h>
253+
254+
int main( void ) {
255+
// Create a strided array:
256+
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 };
257+
258+
// Specify the number of elements:
259+
const int N = 6;
260+
261+
// Specify the stride length:
262+
const int strideX = 2;
263+
264+
// Compute the arithmetic mean:
265+
double v = stdlib_strided_dnanmeanors( N, x, strideX );
266+
267+
// Print the result:
268+
printf( "mean: %lf\n", v );
269+
}
270+
```
271+
272+
</section>
273+
274+
<!-- /.examples -->
275+
276+
</section>
277+
278+
<!-- /.c -->
279+
184280
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
185281
186282
<section class="related">

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

+17-13
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' );
24+
var uniform = require( '@stdlib/random/base/uniform' );
25+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
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' );
27-
var Float64Array = require( '@stdlib/array/float64' );
2829
var pkg = require( './../package.json' ).name;
2930
var dnanmeanors = require( './../lib/dnanmeanors.js' );
3031

3132

3233
// FUNCTIONS //
3334

35+
/**
36+
* Returns a random number.
37+
*
38+
* @private
39+
* @returns {number} random number
40+
*/
41+
function rand() {
42+
if ( bernoulli( 0.8 ) < 1 ) {
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 dnanmeanors = require( './../lib/dnanmeanors.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/dnanmeanors/benchmark/benchmark.native.js

+17-13
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' );
25+
var uniform = require( '@stdlib/random/base/uniform' );
26+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
27+
var filledarrayBy = require( '@stdlib/array/filled-by' );
2628
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2729
var pow = require( '@stdlib/math/base/special/pow' );
28-
var Float64Array = require( '@stdlib/array/float64' );
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 number.
46+
*
47+
* @private
48+
* @returns {number} random number
49+
*/
50+
function rand() {
51+
if ( bernoulli( 0.8 ) < 1 ) {
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/dnanmeanors/benchmark/benchmark.ndarray.js

+17-13
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' );
24+
var uniform = require( '@stdlib/random/base/uniform' );
25+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
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' );
27-
var Float64Array = require( '@stdlib/array/float64' );
2829
var pkg = require( './../package.json' ).name;
2930
var dnanmeanors = require( './../lib/ndarray.js' );
3031

3132

3233
// FUNCTIONS //
3334

35+
/**
36+
* Returns a random number.
37+
*
38+
* @private
39+
* @returns {number} random number
40+
*/
41+
function rand() {
42+
if ( bernoulli( 0.8 ) < 1 ) {
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 dnanmeanors = 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)