Skip to content

Commit c155eb0

Browse files
feat: add C ndarray interface and refactor implementation for stats/base/dsvariance
PR-URL: #5641 Reviewed-by: Athan Reines <kgryte@gmail.com> Co-authored-by: stdlib-bot <noreply@stdlib.io>
1 parent 8f9dfb3 commit c155eb0

24 files changed

+394
-240
lines changed

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

+138-32
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 dsvariance = require( '@stdlib/stats/base/dsvariance' );
9999
```
100100

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

103-
Computes the [variance][variance] of a single-precision floating-point strided array `x` using extended accumulation and returning an extended precision result.
103+
Computes the [variance][variance] of a single-precision floating-point strided array using extended accumulation and returning an extended precision result.
104104

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

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

111-
var v = dsvariance( N, 1, x, 1 );
110+
var v = dsvariance( 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 [`Float32Array`][@stdlib/array/float32].
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 Float32Array = require( '@stdlib/array/float32' );
126-
var floor = require( '@stdlib/math/base/special/floor' );
127125

128126
var x = new Float32Array( [ 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 = dsvariance( N, 1, x, 2 );
128+
var v = dsvariance( 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 Float32Array = require( '@stdlib/array/float32' );
141-
var floor = require( '@stdlib/math/base/special/floor' );
142138

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

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

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

154148
Computes the [variance][variance] of a single-precision floating-point strided array using extended accumulation and alternative indexing semantics and returning an extended precision result.
155149

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

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

162-
var v = dsvariance.ndarray( N, 1, x, 1, 0 );
155+
var v = dsvariance.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 Float32Array = require( '@stdlib/array/float32' );
174-
var floor = require( '@stdlib/math/base/special/floor' );
175167

176168
var x = new Float32Array( [ 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 = dsvariance.ndarray( N, 1, x, 2, 1 );
170+
var v = dsvariance.ndarray( 4, 1, x, 2, 1 );
180171
// returns 6.25
181172
```
182173

@@ -190,7 +181,7 @@ var v = dsvariance.ndarray( N, 1, x, 2, 1 );
190181

191182
- If `N <= 0`, both functions return `NaN`.
192183
- If `N - c` is less than or equal to `0` (where `c` corresponds to the provided degrees of freedom adjustment), both functions return `NaN`.
193-
- Accumulated intermediate values are stored as double-precision floating-point numbers.
184+
- Accumulated intermediate values are stored as double-precision floating-point numbers.
194185

195186
</section>
196187

@@ -203,18 +194,12 @@ var v = dsvariance.ndarray( N, 1, x, 2, 1 );
203194
<!-- eslint no-undef: "error" -->
204195

205196
```javascript
206-
var randu = require( '@stdlib/random/base/randu' );
207-
var round = require( '@stdlib/math/base/special/round' );
208-
var Float32Array = require( '@stdlib/array/float32' );
197+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
209198
var dsvariance = require( '@stdlib/stats/base/dsvariance' );
210199

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

220205
var v = dsvariance( x.length, 1, x, 1 );
@@ -225,6 +210,127 @@ console.log( v );
225210

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

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/dsvariance.h"
237+
```
238+
239+
#### stdlib_strided_dsvariance( N, correction, \*X, strideX )
240+
241+
Computes the [variance][variance] of a single-precision floating-point strided array using extended accumulation and returning an extended precision result.
242+
243+
```c
244+
const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f }
245+
246+
double v = stdlib_strided_dsvariance( 8, 1.0f, x, 1 );
247+
// returns 6.0
248+
```
249+
250+
The function accepts the following arguments:
251+
252+
- **N**: `[in] CBLAS_INT` number of indexed elements.
253+
- **correction**: `[in] float` 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).
254+
- **X**: `[in] float*` input array.
255+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
256+
257+
```c
258+
double stdlib_strided_dsvariance( const CBLAS_INT N, const float correction, const float *X, const CBLAS_INT strideX );
259+
```
260+
261+
#### stdlib_strided_dsvariance_ndarray( N, correction, \*X, strideX, offsetX )
262+
263+
Computes the [variance][variance] of a single-precision floating-point strided array using extended accumulation and alternative indexing semantics and returning an extended precision result.
264+
265+
```c
266+
const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f }
267+
268+
double v = stdlib_strided_dsvariance_ndarray( 4, 1.0f, x, 2, 0 );
269+
// returns ~6.666667
270+
```
271+
272+
The function accepts the following arguments:
273+
274+
- **N**: `[in] CBLAS_INT` number of indexed elements.
275+
- **correction**: `[in] float` 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).
276+
- **X**: `[in] float*` input array.
277+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
278+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
279+
280+
```c
281+
double stdlib_strided_dsvariance_ndarray( const CBLAS_INT N, const float correction, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
282+
```
283+
284+
</section>
285+
286+
<!-- /.usage -->
287+
288+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
289+
290+
<section class="notes">
291+
292+
</section>
293+
294+
<!-- /.notes -->
295+
296+
<!-- C API usage examples. -->
297+
298+
<section class="examples">
299+
300+
### Examples
301+
302+
```c
303+
#include "stdlib/stats/base/dsvariance.h"
304+
#include <stdio.h>
305+
306+
int main( void ) {
307+
// Create a strided array:
308+
const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f };
309+
310+
// Specify the number of elements:
311+
const int N = 4;
312+
313+
// Specify the stride length:
314+
const int strideX = 2;
315+
316+
// Compute the variance:
317+
double v = stdlib_strided_dsvariance( N, 1.0f, x, strideX );
318+
319+
// Print the result:
320+
printf( "sample variance: %lf\n", v );
321+
}
322+
```
323+
324+
</section>
325+
326+
<!-- /.examples -->
327+
328+
</section>
329+
330+
<!-- /.c -->
331+
332+
* * *
333+
228334
<section class="references">
229335
230336
</section>

lib/node_modules/@stdlib/stats/base/dsvariance/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 Float32Array = require( '@stdlib/array/float32' );
2827
var pkg = require( './../package.json' ).name;
2928
var dsvariance = require( './../lib/dsvariance.js' );
3029

3130

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

3440
/**
@@ -39,13 +45,7 @@ var dsvariance = require( './../lib/dsvariance.js' );
3945
* @returns {Function} benchmark function
4046
*/
4147
function createBenchmark( len ) {
42-
var x;
43-
var i;
44-
45-
x = new Float32Array( 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/dsvariance/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 Float32Array = require( '@stdlib/array/float32' );
2928
var tryRequire = require( '@stdlib/utils/try-require' );
3029
var pkg = require( './../package.json' ).name;
3130

@@ -36,6 +35,9 @@ var dsvariance = tryRequire( resolve( __dirname, './../lib/dsvariance.native.js'
3635
var opts = {
3736
'skip': ( dsvariance instanceof Error )
3837
};
38+
var options = {
39+
'dtype': 'float32'
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 Float32Array( 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/dsvariance/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 Float32Array = require( '@stdlib/array/float32' );
2827
var pkg = require( './../package.json' ).name;
2928
var dsvariance = require( './../lib/ndarray.js' );
3029

3130

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

3440
/**
@@ -39,13 +45,7 @@ var dsvariance = require( './../lib/ndarray.js' );
3945
* @returns {Function} benchmark function
4046
*/
4147
function createBenchmark( len ) {
42-
var x;
43-
var i;
44-
45-
x = new Float32Array( 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 ) {

0 commit comments

Comments
 (0)