Skip to content

Commit 8211fd0

Browse files
aayush0325kgryte
authored andcommitted
feat: add C ndarray interface and refactor implementation for stats/base/dstdevtk
PR-URL: stdlib-js#4621 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent 3f1c9c2 commit 8211fd0

24 files changed

+392
-239
lines changed

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

+135-31
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 dstdevtk = require( '@stdlib/stats/base/dstdevtk' );
9999
```
100100

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

103103
Computes the [standard deviation][standard-deviation] of a double-precision floating-point strided array `x` using a one-pass textbook 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 = dstdevtk( N, 1, x, 1 );
110+
var v = dstdevtk( x.length, 1.0, x, 1 );
112111
// returns ~2.0817
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 [standard deviation][standard-deviation] according to `N-c` where `c` corresponds to the provided degrees of freedom adjustment. When computing the [standard deviation][standard-deviation] 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 corrected sample [standard deviation][standard-deviation], 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 [standard deviation][standard-deviation] 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 [standard deviation][standard-deviation] 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 = dstdevtk( N, 1, x, 2 );
128+
var v = dstdevtk( 4, 1.0, x, 2 );
132129
// returns 2.5
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 = dstdevtk( N, 1, x1, 2 );
142+
var v = dstdevtk( 4, 1.0, x1, 2 );
149143
// returns 2.5
150144
```
151145

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

154148
Computes the [standard deviation][standard-deviation] of a double-precision floating-point strided array using a one-pass textbook 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 = dstdevtk.ndarray( N, 1, x, 1, 0 );
155+
var v = dstdevtk.ndarray( x.length, 1.0, x, 1, 0 );
163156
// returns ~2.0817
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 [standard deviation][standard-deviation] 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 [standard deviation][standard-deviation] 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 = dstdevtk.ndarray( N, 1, x, 2, 1 );
170+
var v = dstdevtk.ndarray( 4, 1.0, x, 2, 1 );
180171
// returns 2.5
181172
```
182173

@@ -203,28 +194,141 @@ var v = dstdevtk.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 Float64Array = require( '@stdlib/array/float64' );
197+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
209198
var dstdevtk = require( '@stdlib/stats/base/dstdevtk' );
210199

211-
var x;
212-
var i;
213-
214-
x = new Float64Array( 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': 'float64'
202+
});
218203
console.log( x );
219204

220-
var v = dstdevtk( x.length, 1, x, 1 );
205+
var v = dstdevtk( x.length, 1.0, x, 1 );
221206
console.log( v );
222207
```
223208

224209
</section>
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/dstdevtk.h"
237+
```
238+
239+
#### stdlib_strided_dstdevtk( N, correction, \*X, strideX )
240+
241+
Computes the [standard deviation][standard-deviation] of a double-precision floating-point strided array using a one-pass textbook algorithm.
242+
243+
```c
244+
const double x[] = { 1.0, -2.0, 2.0 };
245+
246+
double v = stdlib_strided_dstdevtk( 3, 1.0, x, 1 );
247+
// returns ~2.0817
248+
```
249+
250+
The function accepts the following arguments:
251+
252+
- **N**: `[in] CBLAS_INT` number of indexed elements.
253+
- **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 [standard deviation][standard-deviation] according to `N-c` where `c` corresponds to the provided degrees of freedom adjustment. When computing the [standard deviation][standard-deviation] 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 corrected sample [standard deviation][standard-deviation], 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] double*` input array.
255+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
256+
257+
```c
258+
double stdlib_strided_dstdevtk( const CBLAS_INT N, const double correction, const double *X, const CBLAS_INT strideX );
259+
```
260+
261+
#### stdlib_strided_dstdevtk_ndarray( N, correction, \*X, strideX, offsetX )
262+
263+
Computes the [standard deviation][standard-deviation] of a double-precision floating-point strided array using a one-pass textbook algorithm and alternative indexing semantics.
264+
265+
```c
266+
const double x[] = { 1.0, -2.0, 2.0 };
267+
268+
double v = stdlib_strided_dstdevtk_ndarray( 3, 1.0, x, 1, 0 );
269+
// returns ~2.0817
270+
```
271+
272+
The function accepts the following arguments:
273+
274+
- **N**: `[in] CBLAS_INT` number of indexed elements.
275+
- **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 [standard deviation][standard-deviation] according to `N-c` where `c` corresponds to the provided degrees of freedom adjustment. When computing the [standard deviation][standard-deviation] 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 corrected sample [standard deviation][standard-deviation], 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] double*` 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_dstdevtk_ndarray( const CBLAS_INT N, const double correction, const double *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/dstdevtk.h"
304+
#include <stdio.h>
305+
306+
int main( void ) {
307+
// Create a strided array:
308+
const double x[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 };
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_dstdevtk( N, 1.0, x, strideX );
318+
319+
// Print the result:
320+
printf( "sample standard deviation: %lf\n", v );
321+
}
322+
```
323+
324+
</section>
325+
326+
<!-- /.examples -->
327+
328+
</section>
329+
330+
<!-- /.c -->
331+
228332
* * *
229333
230334
<section class="references">

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

3130

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

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