Skip to content

Commit d8322de

Browse files
0PrashantYadav0Aayush Khanna
and
Aayush Khanna
authored
feat: add C ndarray interface and refactor implementation for stats/base/dnanstdevwd
PR-URL: #5406 Co-authored-by: Aayush Khanna <[email protected]> Reviewed-by: Aayush Khanna <[email protected]>
1 parent 6c08f89 commit d8322de

26 files changed

+535
-378
lines changed

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

+147-30
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ The use of the term `n-1` is commonly referred to as Bessel's correction. Note,
9898
var dnanstdevwd = require( '@stdlib/stats/base/dnanstdevwd' );
9999
```
100100

101-
#### dnanstdevwd( N, correction, x, stride )
101+
#### dnanstdevwd( N, correction, x, strideX )
102102

103-
Computes the [standard deviation][standard-deviation] of a double-precision floating-point strided array `x` ignoring `NaN` values and using Welford's algorithm.
103+
Computes the [standard deviation][standard-deviation] of a double-precision floating-point strided array, ignoring `NaN` values and using Welford's algorithm.
104104

105105
```javascript
106106
var Float64Array = require( '@stdlib/array/float64' );
@@ -116,39 +116,36 @@ The function has the following parameters:
116116
- **N**: number of indexed elements.
117117
- **correction**: degrees of freedom adjustment. Setting this parameter to a value other than `0` has the effect of adjusting the divisor during the calculation of the [standard deviation][standard-deviation] according to `N-c` where `c` corresponds to the provided degrees of freedom adjustment. When computing the [standard deviation][standard-deviation] of a population, setting this parameter to `0` is the standard choice (i.e., the provided array contains data constituting an entire population). When computing the corrected sample [standard deviation][standard-deviation], setting this parameter to `1` is the standard choice (i.e., the provided array contains data sampled from a larger population; this is commonly referred to as Bessel's correction).
118118
- **x**: input [`Float64Array`][@stdlib/array/float64].
119-
- **stride**: index increment for `x`.
119+
- **strideX**: stride length for `x`.
120120

121-
The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to compute the [standard deviation][standard-deviation] of every other element in `x`,
121+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the [standard deviation][standard-deviation] of every other element in `x`,
122+
123+
<!-- eslint-disable max-len -->
122124

123125
```javascript
124126
var Float64Array = require( '@stdlib/array/float64' );
125-
var floor = require( '@stdlib/math/base/special/floor' );
126127

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

130-
var v = dnanstdevwd( N, 1, x, 2 );
130+
var v = dnanstdevwd( 5, 1, x, 2 );
131131
// returns 2.5
132132
```
133133

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

136-
<!-- eslint-disable stdlib/capitalized-comments -->
136+
<!-- eslint-disable stdlib/capitalized-comments, max-len -->
137137

138138
```javascript
139139
var Float64Array = require( '@stdlib/array/float64' );
140-
var floor = require( '@stdlib/math/base/special/floor' );
141140

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

145-
var N = floor( x0.length / 2 );
146-
147-
var v = dnanstdevwd( N, 1, x1, 2 );
144+
var v = dnanstdevwd( 5, 1, x1, 2 );
148145
// returns 2.5
149146
```
150147

151-
#### dnanstdevwd.ndarray( N, correction, x, stride, offset )
148+
#### dnanstdevwd.ndarray( N, correction, x, strideX, offsetX )
152149

153150
Computes the [standard deviation][standard-deviation] of a double-precision floating-point strided array ignoring `NaN` values and using Welford's algorithm and alternative indexing semantics.
154151

@@ -163,18 +160,18 @@ var v = dnanstdevwd.ndarray( x.length, 1, x, 1, 0 );
163160

164161
The function has the following additional parameters:
165162

166-
- **offset**: starting index for `x`.
163+
- **offsetX**: starting index for `x`.
164+
165+
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 [standard deviation][standard-deviation] for every other value in `x` starting from the second value
167166

168-
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 [standard deviation][standard-deviation] for every other value in `x` starting from the second value
167+
<!-- eslint-disable max-len -->
169168

170169
```javascript
171170
var Float64Array = require( '@stdlib/array/float64' );
172-
var floor = require( '@stdlib/math/base/special/floor' );
173171

174-
var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
175-
var N = floor( x.length / 2 );
172+
var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] );
176173

177-
var v = dnanstdevwd.ndarray( N, 1, x, 2, 1 );
174+
var v = dnanstdevwd.ndarray( 5, 1, x, 2, 1 );
178175
// returns 2.5
179176
```
180177

@@ -200,18 +197,19 @@ var v = dnanstdevwd.ndarray( N, 1, x, 2, 1 );
200197
<!-- eslint no-undef: "error" -->
201198

202199
```javascript
203-
var randu = require( '@stdlib/random/base/randu' );
204-
var round = require( '@stdlib/math/base/special/round' );
205-
var Float64Array = require( '@stdlib/array/float64' );
200+
var uniform = require( '@stdlib/random/base/uniform' );
201+
var filledarrayBy = require( '@stdlib/array/filled-by' );
202+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
206203
var dnanstdevwd = require( '@stdlib/stats/base/dnanstdevwd' );
207204

208-
var x;
209-
var i;
210-
211-
x = new Float64Array( 10 );
212-
for ( i = 0; i < x.length; i++ ) {
213-
x[ i ] = round( (randu()*100.0) - 50.0 );
205+
function rand() {
206+
if ( bernoulli( 0.8 ) < 1 ) {
207+
return NaN;
208+
}
209+
return uniform( -50.0, 50.0 );
214210
}
211+
212+
var x = filledarrayBy( 10, 'float64', rand );
215213
console.log( x );
216214

217215
var v = dnanstdevwd( x.length, 1, x, 1 );
@@ -222,6 +220,125 @@ console.log( v );
222220

223221
<!-- /.examples -->
224222

223+
<!-- C interface documentation. -->
224+
225+
* * *
226+
227+
<section class="c">
228+
229+
## C APIs
230+
231+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
232+
233+
<section class="intro">
234+
235+
</section>
236+
237+
<!-- /.intro -->
238+
239+
<!-- C usage documentation. -->
240+
241+
<section class="usage">
242+
243+
### Usage
244+
245+
```c
246+
#include "stdlib/stats/base/dnanstdevwd.h"
247+
```
248+
249+
#### stdlib_strided_dnanstdevwd( N, correction, \*X, strideX )
250+
251+
Computes the [standard deviation][standard-deviation] of a double-precision floating-point strided array ignoring `NaN` values and using Welford's algorithm.
252+
253+
```c
254+
const double x[] = { 1.0, -2.0, 0.0/0.0, 2.0 };
255+
256+
double v = stdlib_strided_dnanstdevwd( 4, 1.0, x, 1 );
257+
// returns ~4.3333
258+
```
259+
260+
The function accepts the following arguments:
261+
262+
- **N**: `[in] CBLAS_INT` number of indexed elements.
263+
- **correction**: `[in] double` degrees of freedom adjustment. Setting this parameter to a value other than `0` has the effect of adjusting the divisor during the calculation of the [standard deviation][standard-deviation] according to `N-c` where `c` corresponds to the provided degrees of freedom adjustment. When computing the [standard deviation][standard-deviation] of a population, setting this parameter to `0` is the standard choice (i.e., the provided array contains data constituting an entire population). When computing the corrected sample [standard deviation][standard-deviation], setting this parameter to `1` is the standard choice (i.e., the provided array contains data sampled from a larger population; this is commonly referred to as Bessel's correction).
264+
- **X**: `[in] double*` input array.
265+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
266+
267+
```c
268+
double stdlib_strided_dnanstdevwd( const CBLAS_INT N, const double correction, const double *X, const CBLAS_INT strideX );
269+
```
270+
271+
#### stdlib_strided_dnanstdevwd_ndarray( N, correction, \*X, strideX, offsetX )
272+
273+
Computes the [standard deviation][standard-deviation] of a double-precision floating-point strided array ignoring `NaN` values and using Welford's algorithm and alternative indexing semantics.
274+
275+
```c
276+
const double x[] = { 1.0, -2.0, 0.0/0.0, 2.0 };
277+
278+
double v = stdlib_strided_dnanstdevwd_ndarray( 4, 1.0, x, 1, 0 );
279+
// returns ~4.3333
280+
```
281+
282+
The function accepts the following arguments:
283+
284+
- **N**: `[in] CBLAS_INT` number of indexed elements.
285+
- **correction**: `[in] double` degrees of freedom adjustment. Setting this parameter to a value other than `0` has the effect of adjusting the divisor during the calculation of the [standard deviation][standard-deviation] according to `N-c` where `c` corresponds to the provided degrees of freedom adjustment. When computing the [standard deviation][standard-deviation] of a population, setting this parameter to `0` is the standard choice (i.e., the provided array contains data constituting an entire population). When computing the corrected sample [standard deviation][standard-deviation], setting this parameter to `1` is the standard choice (i.e., the provided array contains data sampled from a larger population; this is commonly referred to as Bessel's correction).
286+
- **X**: `[in] double*` input array.
287+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
288+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
289+
290+
```c
291+
double stdlib_strided_dnanstdevwd_ndarray( const CBLAS_INT N, const double correction, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
292+
```
293+
294+
</section>
295+
296+
<!-- /.usage -->
297+
298+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
299+
300+
<section class="notes">
301+
302+
</section>
303+
304+
<!-- /.notes -->
305+
306+
<!-- C API usage examples. -->
307+
308+
<section class="examples">
309+
310+
### Examples
311+
312+
```c
313+
#include "stdlib/stats/base/dnanstdevwd.h"
314+
#include <stdio.h>
315+
316+
int main( void ) {
317+
// Create a strided array:
318+
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 };
319+
320+
// Specify the number of elements:
321+
const int N = 6;
322+
323+
// Specify the stride length:
324+
const int strideX = 2;
325+
326+
// Compute the variance:
327+
double v = stdlib_strided_dnanstdevwd( N, 1.0, x, strideX );
328+
329+
// Print the result:
330+
printf( "sample standard deviation: %lf\n", v );
331+
}
332+
```
333+
334+
</section>
335+
336+
<!-- /.examples -->
337+
338+
</section>
339+
340+
<!-- /.c -->
341+
225342
* * *
226343
227344
<section class="references">

lib/node_modules/@stdlib/stats/base/dnanstdevwd/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 dnanstdevwd = require( './../lib/dnanstdevwd.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.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 dnanstdevwd = require( './../lib/dnanstdevwd.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/dnanstdevwd/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 value or `NaN`.
46+
*
47+
* @private
48+
* @returns {number} random number or `NaN`
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/dnanstdevwd/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 dnanstdevwd = 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.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 dnanstdevwd = 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)