Skip to content

Commit 92ef3fd

Browse files
gururaj1512kgrytestdlib-bot
authored
feat: add C ndarray interface and refactor implementation for stats/base/dvarm
PR-URL: #7269 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent 99c73a5 commit 92ef3fd

File tree

23 files changed

+331
-223
lines changed

23 files changed

+331
-223
lines changed

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

Lines changed: 135 additions & 27 deletions
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 dvarm = require( '@stdlib/stats/base/dvarm' );
9999
```
100100

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

103-
Computes the [variance][variance] of a double-precision floating-point strided array `x` provided a known `mean`.
103+
Computes the [variance][variance] of a double-precision floating-point strided array provided a known `mean`.
104104

105105
```javascript
106106
var Float64Array = require( '@stdlib/array/float64' );
@@ -117,18 +117,16 @@ The function has the following parameters:
117117
- **mean**: mean.
118118
- **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).
119119
- **x**: input [`Float64Array`][@stdlib/array/float64].
120-
- **stride**: index increment for `x`.
120+
- **stride**: stride length for `x`.
121121

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`,
122+
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`,
123123

124124
```javascript
125125
var Float64Array = require( '@stdlib/array/float64' );
126-
var floor = require( '@stdlib/math/base/special/floor' );
127126

128127
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 );
130128

131-
var v = dvarm( N, 1.25, 1, x, 2 );
129+
var v = dvarm( 4, 1.25, 1, x, 2 );
132130
// returns 6.25
133131
```
134132

@@ -138,18 +136,15 @@ Note that indexing is relative to the first index. To introduce an offset, use [
138136

139137
```javascript
140138
var Float64Array = require( '@stdlib/array/float64' );
141-
var floor = require( '@stdlib/math/base/special/floor' );
142139

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

146-
var N = floor( x0.length / 2 );
147-
148-
var v = dvarm( N, 1.25, 1, x1, 2 );
143+
var v = dvarm( 4, 1.25, 1, x1, 2 );
149144
// returns 6.25
150145
```
151146

152-
#### dvarm.ndarray( N, mean, correction, x, stride, offset )
147+
#### dvarm.ndarray( N, mean, correction, x, strideX, offsetX )
153148

154149
Computes the [variance][variance] of a double-precision floating-point strided array provided a known `mean` and using alternative indexing semantics.
155150

@@ -164,18 +159,16 @@ var v = dvarm.ndarray( x.length, 1.0/3.0, 1, x, 1, 0 );
164159

165160
The function has the following additional parameters:
166161

167-
- **offset**: starting index for `x`.
162+
- **offsetX**: starting index for `x`.
168163

169-
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
164+
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
170165

171166
```javascript
172167
var Float64Array = require( '@stdlib/array/float64' );
173-
var floor = require( '@stdlib/math/base/special/floor' );
174168

175169
var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
176-
var N = floor( x.length / 2 );
177170

178-
var v = dvarm.ndarray( N, 1.25, 1, x, 2, 1 );
171+
var v = dvarm.ndarray( 4, 1.25, 1, x, 2, 1 );
179172
// returns 6.25
180173
```
181174

@@ -201,18 +194,12 @@ var v = dvarm.ndarray( N, 1.25, 1, x, 2, 1 );
201194
<!-- eslint no-undef: "error" -->
202195

203196
```javascript
204-
var randu = require( '@stdlib/random/base/randu' );
205-
var round = require( '@stdlib/math/base/special/round' );
206-
var Float64Array = require( '@stdlib/array/float64' );
197+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
207198
var dvarm = require( '@stdlib/stats/base/dvarm' );
208199

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

218205
var v = dvarm( x.length, 0.0, 1, x, 1 );
@@ -223,6 +210,127 @@ console.log( v );
223210

224211
<!-- /.examples -->
225212

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

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

Lines changed: 9 additions & 9 deletions
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' );
2524
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2625
var pow = require( '@stdlib/math/base/special/pow' );
27-
var Float64Array = require( '@stdlib/array/float64' );
26+
var uniform = require( '@stdlib/random/array/uniform' );
2827
var pkg = require( './../package.json' ).name;
2928
var dvarm = require( './../lib/dvarm.js' );
3029

3130

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

3440
/**
@@ -39,13 +45,7 @@ var dvarm = require( './../lib/dvarm.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 ) {

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

Lines changed: 5 additions & 9 deletions
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' );
2625
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2726
var pow = require( '@stdlib/math/base/special/pow' );
28-
var Float64Array = require( '@stdlib/array/float64' );
27+
var uniform = require( '@stdlib/random/array/uniform' );
2928
var tryRequire = require( '@stdlib/utils/try-require' );
3029
var pkg = require( './../package.json' ).name;
3130

@@ -36,6 +35,9 @@ var dvarm = tryRequire( resolve( __dirname, './../lib/dvarm.native.js' ) );
3635
var opts = {
3736
'skip': ( dvarm 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 ) {

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

Lines changed: 9 additions & 9 deletions
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' );
2524
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2625
var pow = require( '@stdlib/math/base/special/pow' );
27-
var Float64Array = require( '@stdlib/array/float64' );
26+
var uniform = require( '@stdlib/random/array/uniform' );
2827
var pkg = require( './../package.json' ).name;
2928
var dvarm = 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 dvarm = 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 ) {

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

Lines changed: 5 additions & 9 deletions
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' );
2625
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2726
var pow = require( '@stdlib/math/base/special/pow' );
28-
var Float64Array = require( '@stdlib/array/float64' );
27+
var uniform = require( '@stdlib/random/array/uniform' );
2928
var tryRequire = require( '@stdlib/utils/try-require' );
3029
var pkg = require( './../package.json' ).name;
3130

@@ -36,6 +35,9 @@ var dvarm = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) );
3635
var opts = {
3736
'skip': ( dvarm 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)