Skip to content

Commit 3c04537

Browse files
gururaj1512kgrytestdlib-bot
authored
feat: add add C ndarray interface and refactor implementation for stats/base/sstdev
PR-URL: #6930 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent e9b6d47 commit 3c04537

File tree

23 files changed

+332
-234
lines changed

23 files changed

+332
-234
lines changed

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

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

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

103-
Computes the [standard deviation][standard-deviation] of a single-precision floating-point strided array `x`.
103+
Computes the [standard deviation][standard-deviation] of a single-precision floating-point strided array.
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 = sstdev( N, 1, x, 1 );
110+
var v = sstdev( x.length, 1, 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 [`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 [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 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 = sstdev( N, 1, x, 2 );
128+
var v = sstdev( 4, 1, 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 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 = sstdev( N, 1, x1, 2 );
142+
var v = sstdev( 4, 1, x1, 2 );
149143
// returns 2.5
150144
```
151145

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

154148
Computes the [standard deviation][standard-deviation] of a single-precision floating-point strided array using alternative indexing semantics.
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 = sstdev.ndarray( N, 1, x, 1, 0 );
155+
var v = sstdev.ndarray( x.length, 1, 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 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 = sstdev.ndarray( N, 1, x, 2, 1 );
170+
var v = sstdev.ndarray( 4, 1, x, 2, 1 );
180171
// returns 2.5
181172
```
182173

@@ -202,18 +193,12 @@ var v = sstdev.ndarray( N, 1, x, 2, 1 );
202193
<!-- eslint no-undef: "error" -->
203194

204195
```javascript
205-
var randu = require( '@stdlib/random/base/randu' );
206-
var round = require( '@stdlib/math/base/special/round' );
207-
var Float32Array = require( '@stdlib/array/float32' );
196+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
208197
var sstdev = require( '@stdlib/stats/base/sstdev' );
209198

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

219204
var v = sstdev( x.length, 1, x, 1 );
@@ -224,10 +209,129 @@ console.log( v );
224209

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

212+
<!-- C interface documentation. -->
213+
227214
* * *
228215

216+
<section class="c">
217+
218+
## C APIs
219+
220+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
221+
222+
<section class="intro">
223+
224+
</section>
225+
226+
<!-- /.intro -->
227+
228+
<!-- C usage documentation. -->
229+
230+
<section class="usage">
231+
232+
### Usage
233+
234+
```c
235+
#include "stdlib/stats/base/sstdev.h"
236+
```
237+
238+
#### stdlib_strided_sstdev( N, correction, \*X, strideX )
239+
240+
Computes the [standard deviation][standard-deviation] of a single-precision floating-point strided array.
241+
242+
```c
243+
const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f };
244+
245+
float v = stdlib_strided_sstdev( 4, 1.0f, x, 2 );
246+
// returns 2.581989f
247+
```
248+
249+
The function accepts the following arguments:
250+
251+
- **N**: `[in] CBLAS_INT` number of indexed elements.
252+
- **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 [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).
253+
- **X**: `[in] float*` input array.
254+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
255+
256+
```c
257+
float stdlib_strided_sstdev( const CBLAS_INT N, const float correction, const float *X, const CBLAS_INT strideX );
258+
```
259+
260+
#### stdlib_strided_sstdev_ndarray( N, correction, \*X, strideX, offsetX )
261+
262+
Computes the [standard deviation][standard-deviation] of a single-precision floating-point strided array using alternative indexing semantics.
263+
264+
```c
265+
const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f };
266+
267+
float v = stdlib_strided_sstdev_ndarray( 4, 1.0f, x, 2, 0 );
268+
// returns 2.581989f
269+
```
270+
271+
The function accepts the following arguments:
272+
273+
- **N**: `[in] CBLAS_INT` number of indexed elements.
274+
- **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 [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).
275+
- **X**: `[in] float*` input array.
276+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
277+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
278+
279+
```c
280+
float stdlib_strided_sstdev_ndarray( const CBLAS_INT N, const float correction, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
281+
```
282+
283+
</section>
284+
285+
<!-- /.usage -->
286+
287+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
288+
289+
<section class="notes">
290+
291+
</section>
292+
293+
<!-- /.notes -->
294+
295+
<!-- C API usage examples. -->
296+
297+
<section class="examples">
298+
299+
### Examples
300+
301+
```c
302+
#include "stdlib/stats/base/sstdev.h"
303+
#include <stdio.h>
304+
305+
int main( void ) {
306+
// Create a strided array:
307+
const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f };
308+
309+
// Specify the number of elements:
310+
const int N = 4;
311+
312+
// Specify the stride length:
313+
const int strideX = 2;
314+
315+
// Compute the standard deviation:
316+
float v = stdlib_strided_sstdev( N, 1.0f, x, strideX );
317+
318+
// Print the result:
319+
printf( "sample standard deviation: %f\n", v );
320+
}
321+
```
322+
323+
</section>
324+
325+
<!-- /.examples -->
326+
327+
</section>
328+
329+
<!-- /.c -->
330+
229331
<section class="references">
230332
333+
* * *
334+
231335
## References
232336
233337
- Neely, Peter M. 1966. "Comparison of Several Algorithms for Computation of Means, Standard Deviations and Correlation Coefficients." _Communications of the ACM_ 9 (7). Association for Computing Machinery: 496–99. doi:[10.1145/365719.365958][@neely:1966a].

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

3130

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

3440
/**
@@ -39,13 +45,7 @@ var sstdev = require( './../lib/sstdev.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/sstdev/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 Float32Array = require( '@stdlib/array/float32' );
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 sstdev = tryRequire( resolve( __dirname, './../lib/sstdev.native.js' ) );
3635
var opts = {
3736
'skip': ( sstdev 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/sstdev/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 Float32Array = require( '@stdlib/array/float32' );
26+
var uniform = require( '@stdlib/random/array/uniform' );
2827
var pkg = require( './../package.json' ).name;
2928
var sstdev = 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 sstdev = 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 ) {

lib/node_modules/@stdlib/stats/base/sstdev/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 Float32Array = require( '@stdlib/array/float32' );
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 sstdev = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) );
3635
var opts = {
3736
'skip': ( sstdev 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 ) {

0 commit comments

Comments
 (0)