Skip to content

Commit 7b91254

Browse files
aayush0325Vinit-Pandit
authored andcommitted
feat: add C ndarray interface and refactor implementation for stats/base/dsemch
PR-URL: stdlib-js#4653 Reviewed-by: Athan Reines <[email protected]>
1 parent d3c2ef6 commit 7b91254

24 files changed

+394
-239
lines changed

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

Lines changed: 134 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,16 @@ where `s` is the sample [standard deviation][standard-deviation].
7070
var dsemch = require( '@stdlib/stats/base/dsemch' );
7171
```
7272

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

7575
Computes the [standard error of the mean][standard-error] of a double-precision floating-point strided array `x` using a one-pass trial mean algorithm.
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 = dsemch( N, 1, x, 1 );
82+
var v = dsemch( 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 = dsemch( N, 1, x, 2 );
100+
var v = dsemch( 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 = dsemch( N, 1, x1, 2 );
114+
var v = dsemch( 4, 1, x1, 2 );
121115
// returns 1.25
122116
```
123117

124-
#### dsemch.ndarray( N, correction, x, stride, offset )
118+
#### dsemch.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 a one-pass trial mean algorithm and 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 = dsemch.ndarray( N, 1, x, 1, 0 );
127+
var v = dsemch.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 = dsemch.ndarray( N, 1, x, 2, 1 );
142+
var v = dsemch.ndarray( 4, 1, x, 2, 1 );
152143
// returns 1.25
153144
```
154145

@@ -175,18 +166,12 @@ var v = dsemch.ndarray( N, 1, x, 2, 1 );
175166
<!-- eslint no-undef: "error" -->
176167

177168
```javascript
178-
var randu = require( '@stdlib/random/base/randu' );
179-
var round = require( '@stdlib/math/base/special/round' );
180-
var Float64Array = require( '@stdlib/array/float64' );
169+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
181170
var dsemch = require( '@stdlib/stats/base/dsemch' );
182171

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

192177
var v = dsemch( x.length, 1, x, 1 );
@@ -197,6 +182,125 @@ console.log( v );
197182

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

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

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

3130

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

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