Skip to content

Commit 4871b02

Browse files
feat: add C ndarray interface and refactor implementation
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: passed - task: lint_c_benchmarks status: passed - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: na - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: na ---
1 parent 26c06be commit 4871b02

23 files changed

+414
-271
lines changed

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

Lines changed: 146 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ The use of the term `n-1` is commonly referred to as Bessel's correction. Note,
9898
var dnanvariancewd = require( '@stdlib/stats/base/dnanvariancewd' );
9999
```
100100

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

103103
Computes the [variance][variance] of a double-precision floating-point strided array `x` ignoring `NaN` values and using Welford's algorithm.
104104

@@ -116,39 +116,38 @@ 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 [variance][variance] according to `n-c` where `c` corresponds to the provided degrees of freedom adjustment and `n` corresponds to the number of non-`NaN` indexed elements. When computing the [variance][variance] 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 unbiased sample [variance][variance], 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**: index increment for `X`.
120120

121-
The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to compute the [variance][variance] 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 [variance][variance] of every other element in `X`,
122+
123+
<!-- eslint-disable max-len -->
122124

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

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

130-
var v = dnanvariancewd( N, 1, x, 2 );
131+
var v = dnanvariancewd( 5, 1, x, 2 );
131132
// returns 6.25
132133
```
133134

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

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

138139
```javascript
139140
var Float64Array = require( '@stdlib/array/float64' );
140141
var floor = require( '@stdlib/math/base/special/floor' );
141142

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

145-
var N = floor( x0.length / 2 );
146-
147-
var v = dnanvariancewd( N, 1, x1, 2 );
146+
var v = dnanvariancewd( 5, 1, x1, 2 );
148147
// returns 6.25
149148
```
150149

151-
#### dnanvariancewd.ndarray( N, correction, x, stride, offset )
150+
#### dnanvariancewd.ndarray( N, correction, x, strideX, offsetX )
152151

153152
Computes the [variance][variance] of a double-precision floating-point strided array ignoring `NaN` values and using Welford's algorithm and alternative indexing semantics.
154153

@@ -163,18 +162,19 @@ var v = dnanvariancewd.ndarray( x.length, 1, x, 1, 0 );
163162

164163
The function has the following additional parameters:
165164

166-
- **offset**: starting index for `x`.
165+
- **offset**: starting index for `X`.
166+
167+
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 [variance][variance] for every other element in `X` starting from the second element
167168

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 [variance][variance] for every other value in `x` starting from the second value
169+
<!-- eslint-disable max-len -->
169170

170171
```javascript
171172
var Float64Array = require( '@stdlib/array/float64' );
172173
var floor = require( '@stdlib/math/base/special/floor' );
173174

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

177-
var v = dnanvariancewd.ndarray( N, 1, x, 2, 1 );
177+
var v = dnanvariancewd.ndarray( 5, 1, x, 2, 1 );
178178
// returns 6.25
179179
```
180180

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

202202
```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' );
203+
var uniform = require( '@stdlib/random/base/uniform' );
204+
var filledarrayBy = require( '@stdlib/array/filled-by' );
205+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
206206
var dnanvariancewd = require( '@stdlib/stats/base/dnanvariancewd' );
207207

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 );
208+
function rand() {
209+
if ( bernoulli( 0.8 ) < 1 ) {
210+
return NaN;
211+
}
212+
return uniform( -50.0, 50.0 );
214213
}
214+
215+
var x = filledarrayBy( 10, 'float64', rand );
215216
console.log( x );
216217

217218
var v = dnanvariancewd( x.length, 1, x, 1 );
@@ -222,6 +223,125 @@ console.log( v );
222223

223224
<!-- /.examples -->
224225

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

lib/node_modules/@stdlib/stats/base/dnanvariancewd/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' );
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 dnanvariancewd = require( './../lib/dnanvariancewd.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 dnanvariancewd = require( './../lib/dnanvariancewd.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/dnanvariancewd/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' );
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/dnanvariancewd/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' );
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 dnanvariancewd = 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 dnanvariancewd = 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)