Skip to content

Commit 27b7e04

Browse files
feat: add C ndarray interface and refactor implementation for stats/base/dsem
PR-URL: #6839 Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent 76f65a5 commit 27b7e04

23 files changed

+341
-236
lines changed

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

+137-31
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,16 @@ where `s` is the sample [standard deviation][standard-deviation].
7070
var dsem = require( '@stdlib/stats/base/dsem' );
7171
```
7272

73-
#### dsem( N, correction, x, stride )
73+
#### dsem( N, correction, x, strideX )
7474

75-
Computes the [standard error of the mean][standard-error] of a double-precision floating-point strided array `x`.
75+
Computes the [standard error of the mean][standard-error] of a double-precision floating-point strided array.
7676

7777
```javascript
7878
var Float64Array = require( '@stdlib/array/float64' );
7979

8080
var x = new Float64Array( [ 1.0, -2.0, 2.0 ] );
81-
var N = x.length;
8281

83-
var v = dsem( N, 1, x, 1 );
82+
var v = dsem( x.length, 1, x, 1 );
8483
// returns ~1.20185
8584
```
8685

@@ -89,18 +88,16 @@ The function has the following parameters:
8988
- **N**: number of indexed elements.
9089
- **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).
9190
- **x**: input [`Float64Array`][@stdlib/array/float64].
92-
- **stride**: index increment for `x`.
91+
- **strideX**: stride length for `x`.
9392

94-
The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to compute the [standard error of the mean][standard-error] of every other element in `x`,
93+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the [standard error of the mean][standard-error] of every other element in `x`,
9594

9695
```javascript
9796
var Float64Array = require( '@stdlib/array/float64' );
98-
var floor = require( '@stdlib/math/base/special/floor' );
9997

10098
var x = new Float64Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ] );
101-
var N = floor( x.length / 2 );
10299

103-
var v = dsem( N, 1, x, 2 );
100+
var v = dsem( 4, 1, x, 2 );
104101
// returns 1.25
105102
```
106103

@@ -110,45 +107,39 @@ Note that indexing is relative to the first index. To introduce an offset, use [
110107

111108
```javascript
112109
var Float64Array = require( '@stdlib/array/float64' );
113-
var floor = require( '@stdlib/math/base/special/floor' );
114110

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

118-
var N = floor( x0.length / 2 );
119-
120-
var v = dsem( N, 1, x1, 2 );
114+
var v = dsem( 4, 1, x1, 2 );
121115
// returns 1.25
122116
```
123117

124-
#### dsem.ndarray( N, correction, x, stride, offset )
118+
#### dsem.ndarray( N, correction, x, strideX, offsetX )
125119

126120
Computes the [standard error of the mean][standard-error] of a double-precision floating-point strided array using alternative indexing semantics.
127121

128122
```javascript
129123
var Float64Array = require( '@stdlib/array/float64' );
130124

131125
var x = new Float64Array( [ 1.0, -2.0, 2.0 ] );
132-
var N = x.length;
133126

134-
var v = dsem.ndarray( N, 1, x, 1, 0 );
127+
var v = dsem.ndarray( x.length, 1, x, 1, 0 );
135128
// returns ~1.20185
136129
```
137130

138131
The function has the following additional parameters:
139132

140-
- **offset**: starting index for `x`.
133+
- **offsetX**: starting index for `x`.
141134

142-
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 error of the mean][standard-error] for every other value in `x` starting from the second value
135+
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 error of the mean][standard-error] for every other element in `x` starting from the second element
143136

144137
```javascript
145138
var Float64Array = require( '@stdlib/array/float64' );
146-
var floor = require( '@stdlib/math/base/special/floor' );
147139

148140
var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
149-
var N = floor( x.length / 2 );
150141

151-
var v = dsem.ndarray( N, 1, x, 2, 1 );
142+
var v = dsem.ndarray( 4, 1, x, 2, 1 );
152143
// returns 1.25
153144
```
154145

@@ -174,18 +165,12 @@ var v = dsem.ndarray( N, 1, x, 2, 1 );
174165
<!-- eslint no-undef: "error" -->
175166

176167
```javascript
177-
var randu = require( '@stdlib/random/base/randu' );
178-
var round = require( '@stdlib/math/base/special/round' );
179-
var Float64Array = require( '@stdlib/array/float64' );
168+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
180169
var dsem = require( '@stdlib/stats/base/dsem' );
181170

182-
var x;
183-
var i;
184-
185-
x = new Float64Array( 10 );
186-
for ( i = 0; i < x.length; i++ ) {
187-
x[ i ] = round( (randu()*100.0) - 50.0 );
188-
}
171+
var x = discreteUniform( 10, -50, 50, {
172+
'dtype': 'float64'
173+
});
189174
console.log( x );
190175

191176
var v = dsem( x.length, 1, x, 1 );
@@ -196,6 +181,127 @@ console.log( v );
196181

197182
<!-- /.examples -->
198183

184+
<!-- C interface documentation. -->
185+
186+
* * *
187+
188+
<section class="c">
189+
190+
## C APIs
191+
192+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
193+
194+
<section class="intro">
195+
196+
</section>
197+
198+
<!-- /.intro -->
199+
200+
<!-- C usage documentation. -->
201+
202+
<section class="usage">
203+
204+
### Usage
205+
206+
```c
207+
#include "stdlib/stats/base/dsem.h"
208+
```
209+
210+
#### stdlib_strided_dsem( N, correction, \*X, strideX )
211+
212+
Computes the [standard error of the mean][standard-error] of a double-precision floating-point strided array.
213+
214+
```c
215+
const double x[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 };
216+
217+
double v = stdlib_strided_dsem( 4, 1.0, x, 2 );
218+
// returns ~1.29099
219+
```
220+
221+
The function accepts the following arguments:
222+
223+
- **N**: `[in] CBLAS_INT` number of indexed elements.
224+
- **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).
225+
- **X**: `[in] double*` input array.
226+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
227+
228+
```c
229+
double stdlib_strided_dsem( const CBLAS_INT N, const double correction, const double *X, const CBLAS_INT strideX );
230+
```
231+
232+
#### stdlib_strided_dsem_ndarray( N, correction, \*X, strideX, offsetX )
233+
234+
Computes the [standard error of the mean][standard-error] of a double-precision floating-point strided array using alternative indexing semantics.
235+
236+
```c
237+
const double x[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 };
238+
239+
double v = stdlib_strided_dsem_ndarray( 4, 1.0, x, 2, 0 );
240+
// returns ~1.29099
241+
```
242+
243+
The function accepts the following arguments:
244+
245+
- **N**: `[in] CBLAS_INT` number of indexed elements.
246+
- **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).
247+
- **X**: `[in] double*` input array.
248+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
249+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
250+
251+
```c
252+
double stdlib_strided_dsem_ndarray( const CBLAS_INT N, const double correction, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
253+
```
254+
255+
</section>
256+
257+
<!-- /.usage -->
258+
259+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
260+
261+
<section class="notes">
262+
263+
</section>
264+
265+
<!-- /.notes -->
266+
267+
<!-- C API usage examples. -->
268+
269+
<section class="examples">
270+
271+
### Examples
272+
273+
```c
274+
#include "stdlib/stats/base/dsem.h"
275+
#include <stdio.h>
276+
277+
int main( void ) {
278+
// Create a strided array:
279+
const double x[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 };
280+
281+
// Specify the number of elements:
282+
const int N = 4;
283+
284+
// Specify the stride length:
285+
const int strideX = 2;
286+
287+
// Compute the standard error of the mean:
288+
double v = stdlib_strided_dsem( N, 1.0, x, strideX );
289+
290+
// Print the result:
291+
printf( "dsem: %lf\n", v );
292+
}
293+
```
294+
295+
</section>
296+
297+
<!-- /.examples -->
298+
299+
</section>
300+
301+
<!-- /.c -->
302+
303+
* * *
304+
199305
<section class="references">
200306
201307
</section>

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

3130

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

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