Skip to content

Commit 2572caf

Browse files
feat: add C ndarray interface and refactor implementation for stats/base/dvariancetk
PR-URL: #4534 Reviewed-by: Athan Reines <kgryte@gmail.com> Co-authored-by: stdlib-bot <82920195+stdlib-bot@users.noreply.github.com>
1 parent d5d5a5a commit 2572caf

24 files changed

+420
-296
lines changed

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

Lines changed: 134 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,16 @@ The use of the term `n-1` is commonly referred to as Bessel's correction. Note,
128128
var dvariancetk = require( '@stdlib/stats/base/dvariancetk' );
129129
```
130130

131-
#### dvariancetk( N, correction, x, stride )
131+
#### dvariancetk( N, correction, x, strideX )
132132

133133
Computes the [variance][variance] of a double-precision floating-point strided array `x` using a one-pass textbook algorithm.
134134

135135
```javascript
136136
var Float64Array = require( '@stdlib/array/float64' );
137137

138138
var x = new Float64Array( [ 1.0, -2.0, 2.0 ] );
139-
var N = x.length;
140139

141-
var v = dvariancetk( N, 1, x, 1 );
140+
var v = dvariancetk( x.length, 1, x, 1 );
142141
// returns ~4.3333
143142
```
144143

@@ -147,18 +146,16 @@ The function has the following parameters:
147146
- **N**: number of indexed elements.
148147
- **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).
149148
- **x**: input [`Float64Array`][@stdlib/array/float64].
150-
- **stride**: index increment for `x`.
149+
- **strideX**: stride length for `x`.
151150

152-
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`,
151+
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`,
153152

154153
```javascript
155154
var Float64Array = require( '@stdlib/array/float64' );
156-
var floor = require( '@stdlib/math/base/special/floor' );
157155

158156
var x = new Float64Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ] );
159-
var N = floor( x.length / 2 );
160157

161-
var v = dvariancetk( N, 1, x, 2 );
158+
var v = dvariancetk( 4, 1, x, 2 );
162159
// returns 6.25
163160
```
164161

@@ -168,45 +165,39 @@ Note that indexing is relative to the first index. To introduce an offset, use [
168165

169166
```javascript
170167
var Float64Array = require( '@stdlib/array/float64' );
171-
var floor = require( '@stdlib/math/base/special/floor' );
172168

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

176-
var N = floor( x0.length / 2 );
177-
178-
var v = dvariancetk( N, 1, x1, 2 );
172+
var v = dvariancetk( 4, 1, x1, 2 );
179173
// returns 6.25
180174
```
181175

182-
#### dvariancetk.ndarray( N, correction, x, stride, offset )
176+
#### dvariancetk.ndarray( N, correction, x, strideX, offsetX )
183177

184178
Computes the [variance][variance] of a double-precision floating-point strided array using a one-pass textbook algorithm and alternative indexing semantics.
185179

186180
```javascript
187181
var Float64Array = require( '@stdlib/array/float64' );
188182

189183
var x = new Float64Array( [ 1.0, -2.0, 2.0 ] );
190-
var N = x.length;
191184

192-
var v = dvariancetk.ndarray( N, 1, x, 1, 0 );
185+
var v = dvariancetk.ndarray( x.length, 1, x, 1, 0 );
193186
// returns ~4.33333
194187
```
195188

196189
The function has the following additional parameters:
197190

198-
- **offset**: starting index for `x`.
191+
- **offsetX**: starting index for `x`.
199192

200-
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
193+
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 the strided array starting from the second value
201194

202195
```javascript
203196
var Float64Array = require( '@stdlib/array/float64' );
204-
var floor = require( '@stdlib/math/base/special/floor' );
205197

206198
var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
207-
var N = floor( x.length / 2 );
208199

209-
var v = dvariancetk.ndarray( N, 1, x, 2, 1 );
200+
var v = dvariancetk.ndarray( 4, 1, x, 2, 1 );
210201
// returns 6.25
211202
```
212203

@@ -233,18 +224,12 @@ var v = dvariancetk.ndarray( N, 1, x, 2, 1 );
233224
<!-- eslint no-undef: "error" -->
234225

235226
```javascript
236-
var randu = require( '@stdlib/random/base/randu' );
237-
var round = require( '@stdlib/math/base/special/round' );
238-
var Float64Array = require( '@stdlib/array/float64' );
227+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
239228
var dvariancetk = require( '@stdlib/stats/base/dvariancetk' );
240229

241-
var x;
242-
var i;
243-
244-
x = new Float64Array( 10 );
245-
for ( i = 0; i < x.length; i++ ) {
246-
x[ i ] = round( (randu()*100.0) - 50.0 );
247-
}
230+
var x = discreteUniform( 10, -50, 50, {
231+
'dtype': 'float64'
232+
});
248233
console.log( x );
249234

250235
var v = dvariancetk( x.length, 1, x, 1 );
@@ -255,6 +240,125 @@ console.log( v );
255240

256241
<!-- /.examples -->
257242

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

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

3130

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

3440
/**
@@ -39,13 +45,7 @@ var dvariancetk = require( './../lib/dvariancetk.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/dvariancetk/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' );
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 dvariancetk = tryRequire( resolve( __dirname, './../lib/dvariancetk.native.j
3635
var opts = {
3736
'skip': ( dvariancetk 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/dvariancetk/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' );
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 dvariancetk = 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 dvariancetk = 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/dvariancetk/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' );
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 dvariancetk = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' )
3635
var opts = {
3736
'skip': ( dvariancetk 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)