Skip to content

Commit fcda856

Browse files
aayush0325kgrytestdlib-bot
authored
feat: add C ndarray interface and refactor implementation for stats/base/dmeanvarpn
PR-URL: #5030 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent bde1562 commit fcda856

21 files changed

+363
-259
lines changed

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

+142-26
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ var dmeanvarpn = require( '@stdlib/stats/base/dmeanvarpn' );
102102

103103
#### dmeanvarpn( N, correction, x, strideX, out, strideOut )
104104

105-
Computes the [mean][arithmetic-mean] and [variance][variance] of a double-precision floating-point strided array `x` using a two-pass algorithm.
105+
Computes the [mean][arithmetic-mean] and [variance][variance] of a double-precision floating-point strided array using a two-pass algorithm.
106106

107107
```javascript
108108
var Float64Array = require( '@stdlib/array/float64' );
@@ -122,21 +122,19 @@ The function has the following parameters:
122122
- **N**: number of indexed elements.
123123
- **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).
124124
- **x**: input [`Float64Array`][@stdlib/array/float64].
125-
- **strideX**: index increment for `x`.
125+
- **strideX**: stride length for `x`.
126126
- **out**: output [`Float64Array`][@stdlib/array/float64] for storing results.
127-
- **strideOut**: index increment for `out`.
127+
- **strideOut**: stride length for `out`.
128128

129-
The `N` and `stride` parameters determine which elements are accessed at runtime. For example, to compute the [variance][variance] of every other element in `x`,
129+
The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to compute the [variance][variance] of every other element in `x`,
130130

131131
```javascript
132132
var Float64Array = require( '@stdlib/array/float64' );
133-
var floor = require( '@stdlib/math/base/special/floor' );
134133

135134
var x = new Float64Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ] );
136135
var out = new Float64Array( 2 );
137-
var N = floor( x.length / 2 );
138136

139-
var v = dmeanvarpn( N, 1, x, 2, out, 1 );
137+
var v = dmeanvarpn( 4, 1, x, 2, out, 1 );
140138
// returns <Float64Array>[ 1.25, 6.25 ]
141139
```
142140

@@ -146,17 +144,14 @@ Note that indexing is relative to the first index. To introduce an offset, use [
146144

147145
```javascript
148146
var Float64Array = require( '@stdlib/array/float64' );
149-
var floor = require( '@stdlib/math/base/special/floor' );
150147

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

154151
var out0 = new Float64Array( 4 );
155152
var out1 = new Float64Array( out0.buffer, out0.BYTES_PER_ELEMENT*2 ); // start at 3rd element
156153

157-
var N = floor( x0.length / 2 );
158-
159-
var v = dmeanvarpn( N, 1, x1, 2, out1, 1 );
154+
var v = dmeanvarpn( 4, 1, x1, 2, out1, 1 );
160155
// returns <Float64Array>[ 1.25, 6.25 ]
161156
```
162157

@@ -179,17 +174,15 @@ The function has the following additional parameters:
179174
- **offsetX**: starting index for `x`.
180175
- **offsetOut**: starting index for `out`.
181176

182-
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying `buffer`, the `offset` parameters support indexing semantics based on a starting index. For example, to calculate the [mean][arithmetic-mean] and [variance][variance] for every other value in `x` starting from the second value
177+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameters support indexing semantics based on a starting index. For example, to calculate the [mean][arithmetic-mean] and [variance][variance] for every other element in `x` starting from the second element
183178

184179
```javascript
185180
var Float64Array = require( '@stdlib/array/float64' );
186-
var floor = require( '@stdlib/math/base/special/floor' );
187181

188182
var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
189183
var out = new Float64Array( 4 );
190-
var N = floor( x.length / 2 );
191184

192-
var v = dmeanvarpn.ndarray( N, 1, x, 2, 1, out, 2, 1 );
185+
var v = dmeanvarpn.ndarray( 4, 1, x, 2, 1, out, 2, 1 );
193186
// returns <Float64Array>[ 0.0, 1.25, 0.0, 6.25 ]
194187
```
195188

@@ -215,22 +208,16 @@ var v = dmeanvarpn.ndarray( N, 1, x, 2, 1, out, 2, 1 );
215208
<!-- eslint no-undef: "error" -->
216209

217210
```javascript
218-
var randu = require( '@stdlib/random/base/randu' );
219-
var round = require( '@stdlib/math/base/special/round' );
211+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
220212
var Float64Array = require( '@stdlib/array/float64' );
221213
var dmeanvarpn = require( '@stdlib/stats/base/dmeanvarpn' );
222214

223-
var out;
224-
var x;
225-
var i;
226-
227-
x = new Float64Array( 10 );
228-
for ( i = 0; i < x.length; i++ ) {
229-
x[ i ] = round( (randu()*100.0) - 50.0 );
230-
}
215+
var x = discreteUniform( 10, -50, 50, {
216+
'dtype': 'float64'
217+
});
231218
console.log( x );
232219

233-
out = new Float64Array( 2 );
220+
var out = new Float64Array( 2 );
234221
dmeanvarpn( x.length, 1, x, 1, out, 1 );
235222
console.log( out );
236223
```
@@ -239,6 +226,135 @@ console.log( out );
239226

240227
<!-- /.examples -->
241228

229+
<!-- C interface documentation. -->
230+
231+
* * *
232+
233+
<section class="c">
234+
235+
## C APIs
236+
237+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
238+
239+
<section class="intro">
240+
241+
</section>
242+
243+
<!-- /.intro -->
244+
245+
<!-- C usage documentation. -->
246+
247+
<section class="usage">
248+
249+
### Usage
250+
251+
```c
252+
#include "stdlib/stats/base/dmeanvarpn.h"
253+
```
254+
255+
#### stdlib_strided_dmeanvarpn( N, correction, \*X, strideX, \*Out, strideOut )
256+
257+
Computes the [mean][arithmetic-mean] and [variance][variance] of a double-precision floating-point strided array using a two-pass algorithm.
258+
259+
```c
260+
const double x[] = { 1.0, -2.0, 2.0 };
261+
double out[] = { 0.0, 0.0 }
262+
263+
stdlib_strided_dmeanvarpn( 3, 1.0, x, 1, out, 1 );
264+
```
265+
266+
The function accepts the following arguments:
267+
268+
- **N**: `[in] CBLAS_INT` number of indexed elements.
269+
- **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).
270+
- **X**: `[in] double*` input array.
271+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
272+
- **Out**: `[out] double*` output array.
273+
- **strideOut**: `[in] CBLAS_INT` stride length for `Out`.
274+
275+
```c
276+
double stdlib_strided_dmeanvarpn( const CBLAS_INT N, const double correction, const double *X, const CBLAS_INT strideX, double *Out, const CBLAS_INT strideOut );
277+
```
278+
279+
#### stdlib_strided_dmeanvarpn( N, correction, \*X, strideX, offsetX, \*Out, strideOut, offsetOut )
280+
281+
Computes the [mean][arithmetic-mean] and [variance][variance] of a double-precision floating-point strided array using a two-pass algorithm and alternative indexing semantics.
282+
283+
```c
284+
const double x[] = { 1.0, -2.0, 2.0 };
285+
double out[] = { 0.0, 0.0 }
286+
287+
stdlib_strided_dmeanvarpn_ndarray( 3, 1.0, x, 1, 0, out, 1, 0 );
288+
```
289+
290+
The function accepts the following arguments:
291+
292+
- **N**: `[in] CBLAS_INT` number of indexed elements.
293+
- **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).
294+
- **X**: `[in] double*` input array.
295+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
296+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
297+
- **Out**: `[out] double*` output array.
298+
- **strideOut**: `[in] CBLAS_INT` stride length for `Out`.
299+
- **offsetOut**: `[in] CBLAS_INT` starting index for `Out`.
300+
301+
```c
302+
double stdlib_strided_dmeanvarpn_ndarray( const CBLAS_INT N, const double correction, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, double *Out, const CBLAS_INT strideOut, const CBLAS_INT offsetOut );
303+
```
304+
305+
</section>
306+
307+
<!-- /.usage -->
308+
309+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
310+
311+
<section class="notes">
312+
313+
</section>
314+
315+
<!-- /.notes -->
316+
317+
<!-- C API usage examples. -->
318+
319+
<section class="examples">
320+
321+
### Examples
322+
323+
```c
324+
#include "stdlib/stats/base/dmeanvarpn.h"
325+
#include <stdio.h>
326+
327+
int main( void ) {
328+
// Create a strided array:
329+
const double x[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 };
330+
331+
// Create an output array:
332+
double out[] = { 0.0, 0.0 };
333+
334+
// Specify the number of elements:
335+
const int N = 4;
336+
337+
// Specify the stride lengths:
338+
const int strideX = 2;
339+
const int strideOut = 1;
340+
341+
// Compute the mean and variance:
342+
stdlib_strided_dmeanvarpn( N, 1.0, x, strideX, out, strideOut );
343+
344+
// Print the result:
345+
printf( "sample mean: %lf\n", out[ 0 ] );
346+
printf( "sample variance: %lf\n", out[ 1 ] );
347+
}
348+
```
349+
350+
</section>
351+
352+
<!-- /.examples -->
353+
354+
</section>
355+
356+
<!-- /.c -->
357+
242358
* * *
243359
244360
<section class="references">

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

+10-10
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,21 @@
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' );
2727
var Float64Array = require( '@stdlib/array/float64' );
2828
var pkg = require( './../package.json' ).name;
2929
var dmeanvarpn = require( './../lib/dmeanvarpn.js' );
3030

3131

32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'float64'
36+
};
37+
38+
3239
// FUNCTIONS //
3340

3441
/**
@@ -39,15 +46,8 @@ var dmeanvarpn = require( './../lib/dmeanvarpn.js' );
3946
* @returns {Function} benchmark function
4047
*/
4148
function createBenchmark( len ) {
42-
var out;
43-
var x;
44-
var i;
45-
46-
x = new Float64Array( len );
47-
for ( i = 0; i < x.length; i++ ) {
48-
x[ i ] = ( randu()*20.0 ) - 10.0;
49-
}
50-
out = new Float64Array( 2 );
49+
var out = new Float64Array( 2 );
50+
var x = uniform( len, -10.0, 10.0, options );
5151
return benchmark;
5252

5353
function benchmark( b ) {

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

+6-10
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
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' );
2828
var Float64Array = require( '@stdlib/array/float64' );
@@ -36,6 +36,9 @@ var dmeanvarpn = tryRequire( resolve( __dirname, './../lib/dmeanvarpn.native.js'
3636
var opts = {
3737
'skip': ( dmeanvarpn instanceof Error )
3838
};
39+
var options = {
40+
'dtype': 'float64'
41+
};
3942

4043

4144
// FUNCTIONS //
@@ -48,15 +51,8 @@ var opts = {
4851
* @returns {Function} benchmark function
4952
*/
5053
function createBenchmark( len ) {
51-
var out;
52-
var x;
53-
var i;
54-
55-
x = new Float64Array( len );
56-
for ( i = 0; i < x.length; i++ ) {
57-
x[ i ] = ( randu()*20.0 ) - 10.0;
58-
}
59-
out = new Float64Array( 2 );
54+
var out = new Float64Array( 2 );
55+
var x = uniform( len, -10.0, 10.0, options );
6056
return benchmark;
6157

6258
function benchmark( b ) {

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

+10-10
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,21 @@
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' );
2727
var Float64Array = require( '@stdlib/array/float64' );
2828
var pkg = require( './../package.json' ).name;
2929
var dmeanvarpn = require( './../lib/ndarray.js' );
3030

3131

32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'float64'
36+
};
37+
38+
3239
// FUNCTIONS //
3340

3441
/**
@@ -39,15 +46,8 @@ var dmeanvarpn = require( './../lib/ndarray.js' );
3946
* @returns {Function} benchmark function
4047
*/
4148
function createBenchmark( len ) {
42-
var out;
43-
var x;
44-
var i;
45-
46-
x = new Float64Array( len );
47-
for ( i = 0; i < x.length; i++ ) {
48-
x[ i ] = ( randu()*20.0 ) - 10.0;
49-
}
50-
out = new Float64Array( 2 );
49+
var out = new Float64Array( 2 );
50+
var x = uniform( len, -10.0, 10.0, options );
5151
return benchmark;
5252

5353
function benchmark( b ) {

lib/node_modules/@stdlib/stats/base/dmeanvarpn/benchmark/benchmark.ndarray.native.js

+6-10
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
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' );
2828
var Float64Array = require( '@stdlib/array/float64' );
@@ -36,6 +36,9 @@ var dmeanvarpn = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' )
3636
var opts = {
3737
'skip': ( dmeanvarpn instanceof Error )
3838
};
39+
var options = {
40+
'dtype': 'float64'
41+
};
3942

4043

4144
// FUNCTIONS //
@@ -48,15 +51,8 @@ var opts = {
4851
* @returns {Function} benchmark function
4952
*/
5053
function createBenchmark( len ) {
51-
var out;
52-
var x;
53-
var i;
54-
55-
x = new Float64Array( len );
56-
for ( i = 0; i < x.length; i++ ) {
57-
x[ i ] = ( randu()*20.0 ) - 10.0;
58-
}
59-
out = new Float64Array( 2 );
54+
var out = new Float64Array( 2 );
55+
var x = uniform( len, -10.0, 10.0, options );
6056
return benchmark;
6157

6258
function benchmark( b ) {

0 commit comments

Comments
 (0)