Skip to content

Commit 7feea68

Browse files
feat: add C ndarray interface and refactor implementation for stats/base/dvariancepn
PR-URL: #4688 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent 66a14d0 commit 7feea68

23 files changed

+371
-256
lines changed

Diff for: lib/node_modules/@stdlib/stats/base/dvariancepn/README.md

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

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

103103
Computes the [variance][variance] of a double-precision floating-point strided array `x` using a two-pass algorithm.
104104

105105
```javascript
106106
var Float64Array = require( '@stdlib/array/float64' );
107107

108108
var x = new Float64Array( [ 1.0, -2.0, 2.0 ] );
109-
var N = x.length;
110109

111-
var v = dvariancepn( N, 1, x, 1 );
110+
var v = dvariancepn( x.length, 1, x, 1 );
112111
// returns ~4.3333
113112
```
114113

@@ -117,18 +116,16 @@ The function has the following parameters:
117116
- **N**: number of indexed elements.
118117
- **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. 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).
119118
- **x**: input [`Float64Array`][@stdlib/array/float64].
120-
- **stride**: index increment for `x`.
119+
- **strideX**: stride length for `x`.
121120

122-
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`,
123122

124123
```javascript
125124
var Float64Array = require( '@stdlib/array/float64' );
126-
var floor = require( '@stdlib/math/base/special/floor' );
127125

128126
var x = new Float64Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ] );
129-
var N = floor( x.length / 2 );
130127

131-
var v = dvariancepn( N, 1, x, 2 );
128+
var v = dvariancepn( 4, 1, x, 2 );
132129
// returns 6.25
133130
```
134131

@@ -138,45 +135,39 @@ Note that indexing is relative to the first index. To introduce an offset, use [
138135

139136
```javascript
140137
var Float64Array = require( '@stdlib/array/float64' );
141-
var floor = require( '@stdlib/math/base/special/floor' );
142138

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

146-
var N = floor( x0.length / 2 );
147-
148-
var v = dvariancepn( N, 1, x1, 2 );
142+
var v = dvariancepn( 4, 1, x1, 2 );
149143
// returns 6.25
150144
```
151145

152-
#### dvariancepn.ndarray( N, correction, x, stride, offset )
146+
#### dvariancepn.ndarray( N, correction, x, strideX, offsetX )
153147

154148
Computes the [variance][variance] of a double-precision floating-point strided array using a two-pass algorithm and alternative indexing semantics.
155149

156150
```javascript
157151
var Float64Array = require( '@stdlib/array/float64' );
158152

159153
var x = new Float64Array( [ 1.0, -2.0, 2.0 ] );
160-
var N = x.length;
161154

162-
var v = dvariancepn.ndarray( N, 1, x, 1, 0 );
155+
var v = dvariancepn.ndarray( x.length, 1, x, 1, 0 );
163156
// returns ~4.33333
164157
```
165158

166159
The function has the following additional parameters:
167160

168-
- **offset**: starting index for `x`.
161+
- **offsetX**: starting index for `x`.
169162

170-
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
163+
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
171164

172165
```javascript
173166
var Float64Array = require( '@stdlib/array/float64' );
174-
var floor = require( '@stdlib/math/base/special/floor' );
175167

176168
var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
177-
var N = floor( x.length / 2 );
178169

179-
var v = dvariancepn.ndarray( N, 1, x, 2, 1 );
170+
var v = dvariancepn.ndarray( 4, 1, x, 2, 1 );
180171
// returns 6.25
181172
```
182173

@@ -202,18 +193,12 @@ var v = dvariancepn.ndarray( N, 1, x, 2, 1 );
202193
<!-- eslint no-undef: "error" -->
203194

204195
```javascript
205-
var randu = require( '@stdlib/random/base/randu' );
206-
var round = require( '@stdlib/math/base/special/round' );
207-
var Float64Array = require( '@stdlib/array/float64' );
196+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
208197
var dvariancepn = require( '@stdlib/stats/base/dvariancepn' );
209198

210-
var x;
211-
var i;
212-
213-
x = new Float64Array( 10 );
214-
for ( i = 0; i < x.length; i++ ) {
215-
x[ i ] = round( (randu()*100.0) - 50.0 );
216-
}
199+
var x = discreteUniform( 10, -50, 50, {
200+
'dtype': 'float64'
201+
});
217202
console.log( x );
218203

219204
var v = dvariancepn( x.length, 1, x, 1 );
@@ -224,6 +209,125 @@ console.log( v );
224209

225210
<!-- /.examples -->
226211

212+
<!-- C interface documentation. -->
213+
214+
* * *
215+
216+
<section class="c">
217+
218+
## C APIs
219+
220+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
221+
222+
<section class="intro">
223+
224+
</section>
225+
226+
<!-- /.intro -->
227+
228+
<!-- C usage documentation. -->
229+
230+
<section class="usage">
231+
232+
### Usage
233+
234+
```c
235+
#include "stdlib/stats/base/dvariancepn.h"
236+
```
237+
238+
#### stdlib_strided_dvariancepn( N, correction, \*X, strideX )
239+
240+
Computes the [variance][variance] of a double-precision floating-point strided array using a two-pass algorithm.
241+
242+
```c
243+
const double x[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 }
244+
245+
double v = stdlib_strided_dvariancepn( 8, 1.0, x, 1 );
246+
// returns 6.0
247+
```
248+
249+
The function accepts the following arguments:
250+
251+
- **N**: `[in] CBLAS_INT` number of indexed elements.
252+
- **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. 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).
253+
- **X**: `[in] double*` input array.
254+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
255+
256+
```c
257+
double stdlib_strided_dvariancepn( const CBLAS_INT N, const double correction, const double *X, const CBLAS_INT strideX );
258+
```
259+
260+
#### stdlib_strided_dvariancepn_ndarray( N, correction, \*X, strideX, offsetX )
261+
262+
Computes the [variance][variance] of a double-precision floating-point strided array using a two-pass algorithm and alternative indexing semantics.
263+
264+
```c
265+
const double x[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 }
266+
267+
double v = stdlib_strided_dvariancepn_ndarray( 4, 1.0, x, 2, 0 );
268+
// returns ~6.666667
269+
```
270+
271+
The function accepts the following arguments:
272+
273+
- **N**: `[in] CBLAS_INT` number of indexed elements.
274+
- **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. 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).
275+
- **X**: `[in] double*` input array.
276+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
277+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
278+
279+
```c
280+
double stdlib_strided_dvariancepn_ndarray( const CBLAS_INT N, const double correction, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
281+
```
282+
283+
</section>
284+
285+
<!-- /.usage -->
286+
287+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
288+
289+
<section class="notes">
290+
291+
</section>
292+
293+
<!-- /.notes -->
294+
295+
<!-- C API usage examples. -->
296+
297+
<section class="examples">
298+
299+
### Examples
300+
301+
```c
302+
#include "stdlib/stats/base/dvariancepn.h"
303+
#include <stdio.h>
304+
305+
int main( void ) {
306+
// Create a strided array:
307+
const double x[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 };
308+
309+
// Specify the number of elements:
310+
const int N = 4;
311+
312+
// Specify the stride length:
313+
const int strideX = 2;
314+
315+
// Compute the variance:
316+
double v = stdlib_strided_dvariancepn( N, 1.0, x, strideX );
317+
318+
// Print the result:
319+
printf( "sample variance: %lf\n", v );
320+
}
321+
```
322+
323+
</section>
324+
325+
<!-- /.examples -->
326+
327+
</section>
328+
329+
<!-- /.c -->
330+
227331
* * *
228332
229333
<section class="references">

Diff for: lib/node_modules/@stdlib/stats/base/dvariancepn/benchmark/benchmark.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,20 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
2525
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2626
var pow = require( '@stdlib/math/base/special/pow' );
27-
var Float64Array = require( '@stdlib/array/float64' );
2827
var pkg = require( './../package.json' ).name;
2928
var dvariancepn = require( './../lib/dvariancepn.js' );
3029

3130

31+
// VARIABLES //
32+
33+
var options = {
34+
'dtype': 'float64'
35+
};
36+
37+
3238
// FUNCTIONS //
3339

3440
/**
@@ -39,13 +45,7 @@ var dvariancepn = require( './../lib/dvariancepn.js' );
3945
* @returns {Function} benchmark function
4046
*/
4147
function createBenchmark( len ) {
42-
var x;
43-
var i;
44-
45-
x = new Float64Array( len );
46-
for ( i = 0; i < x.length; i++ ) {
47-
x[ i ] = ( randu()*20.0 ) - 10.0;
48-
}
48+
var x = uniform( len, -10.0, 10.0, options );
4949
return benchmark;
5050

5151
function benchmark( b ) {

Diff for: lib/node_modules/@stdlib/stats/base/dvariancepn/benchmark/benchmark.native.js

+5-9
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@
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/array/uniform' );
2626
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2727
var pow = require( '@stdlib/math/base/special/pow' );
28-
var Float64Array = require( '@stdlib/array/float64' );
2928
var tryRequire = require( '@stdlib/utils/try-require' );
3029
var pkg = require( './../package.json' ).name;
3130

@@ -36,6 +35,9 @@ var dvariancepn = tryRequire( resolve( __dirname, './../lib/dvariancepn.native.j
3635
var opts = {
3736
'skip': ( dvariancepn instanceof Error )
3837
};
38+
var options = {
39+
'dtype': 'float64'
40+
};
3941

4042

4143
// FUNCTIONS //
@@ -48,13 +50,7 @@ var opts = {
4850
* @returns {Function} benchmark function
4951
*/
5052
function createBenchmark( len ) {
51-
var x;
52-
var i;
53-
54-
x = new Float64Array( len );
55-
for ( i = 0; i < x.length; i++ ) {
56-
x[ i ] = ( randu()*20.0 ) - 10.0;
57-
}
53+
var x = uniform( len, -10.0, 10.0, options );
5854
return benchmark;
5955

6056
function benchmark( b ) {

Diff for: lib/node_modules/@stdlib/stats/base/dvariancepn/benchmark/benchmark.ndarray.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,20 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
2525
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2626
var pow = require( '@stdlib/math/base/special/pow' );
27-
var Float64Array = require( '@stdlib/array/float64' );
2827
var pkg = require( './../package.json' ).name;
2928
var dvariancepn = require( './../lib/ndarray.js' );
3029

3130

31+
// VARIABLES //
32+
33+
var options = {
34+
'dtype': 'float64'
35+
};
36+
37+
3238
// FUNCTIONS //
3339

3440
/**
@@ -39,13 +45,7 @@ var dvariancepn = require( './../lib/ndarray.js' );
3945
* @returns {Function} benchmark function
4046
*/
4147
function createBenchmark( len ) {
42-
var x;
43-
var i;
44-
45-
x = new Float64Array( len );
46-
for ( i = 0; i < x.length; i++ ) {
47-
x[ i ] = ( randu()*20.0 ) - 10.0;
48-
}
48+
var x = uniform( len, -10.0, 10.0, options );
4949
return benchmark;
5050

5151
function benchmark( b ) {

Diff for: lib/node_modules/@stdlib/stats/base/dvariancepn/benchmark/benchmark.ndarray.native.js

+5-9
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@
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/array/uniform' );
2626
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2727
var pow = require( '@stdlib/math/base/special/pow' );
28-
var Float64Array = require( '@stdlib/array/float64' );
2928
var tryRequire = require( '@stdlib/utils/try-require' );
3029
var pkg = require( './../package.json' ).name;
3130

@@ -36,6 +35,9 @@ var dvariancepn = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' )
3635
var opts = {
3736
'skip': ( dvariancepn instanceof Error )
3837
};
38+
var options = {
39+
'dtype': 'float64'
40+
};
3941

4042

4143
// FUNCTIONS //
@@ -48,13 +50,7 @@ var opts = {
4850
* @returns {Function} benchmark function
4951
*/
5052
function createBenchmark( len ) {
51-
var x;
52-
var i;
53-
54-
x = new Float64Array( len );
55-
for ( i = 0; i < x.length; i++ ) {
56-
x[ i ] = ( randu()*20.0 ) - 10.0;
57-
}
53+
var x = uniform( len, -10.0, 10.0, options );
5854
return benchmark;
5955

6056
function benchmark( b ) {

0 commit comments

Comments
 (0)