Skip to content

Commit d3b0c0c

Browse files
feat: add C ndarray interface and refactor implementation
--- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: na - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: na ---
1 parent bb638df commit d3b0c0c

File tree

24 files changed

+394
-246
lines changed

24 files changed

+394
-246
lines changed

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

Lines changed: 137 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,15 @@ The use of the term `n-1` is commonly referred to as Bessel's correction. Note,
9898
var dstdev = require( '@stdlib/stats/base/dstdev' );
9999
```
100100

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

103103
Computes the [standard deviation][standard-deviation] of a double-precision floating-point strided array `x`.
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;
110-
111-
var v = dstdev( N, 1, x, 1 );
109+
var v = dstdev( x.length, 1, x, 1 );
112110
// returns ~2.0817
113111
```
114112

@@ -117,19 +115,17 @@ The function has the following parameters:
117115
- **N**: number of indexed elements.
118116
- **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).
119117
- **x**: input [`Float64Array`][@stdlib/array/float64].
120-
- **stride**: index increment for `x`.
118+
- **strideX**: stride length for `x`.
121119

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`,
120+
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`,
123121

124122
```javascript
125123
var Float64Array = require( '@stdlib/array/float64' );
126-
var floor = require( '@stdlib/math/base/special/floor' );
127124

128125
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 );
130126

131-
var v = dstdev( N, 1, x, 2 );
132-
// returns 2.5
127+
var v = dstdev( 4, 1, x, 2 );
128+
// returns ~2.0817
133129
```
134130

135131
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
@@ -138,46 +134,40 @@ Note that indexing is relative to the first index. To introduce an offset, use [
138134

139135
```javascript
140136
var Float64Array = require( '@stdlib/array/float64' );
141-
var floor = require( '@stdlib/math/base/special/floor' );
142137

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

146-
var N = floor( x0.length / 2 );
147-
148-
var v = dstdev( N, 1, x1, 2 );
149-
// returns 2.5
141+
var v = dstdev( 4, 1, x1, 2 );
142+
// returns ~2.0817
150143
```
151144

152-
#### dstdev.ndarray( N, correction, x, stride, offset )
145+
#### dstdev.ndarray( N, correction, x, strideX, offsetX )
153146

154147
Computes the [standard deviation][standard-deviation] of a double-precision floating-point strided array using alternative indexing semantics.
155148

156149
```javascript
157150
var Float64Array = require( '@stdlib/array/float64' );
158151

159152
var x = new Float64Array( [ 1.0, -2.0, 2.0 ] );
160-
var N = x.length;
161153

162-
var v = dstdev.ndarray( N, 1, x, 1, 0 );
154+
var v = dstdev.ndarray( x.length, 1, x, 1, 0 );
163155
// returns ~2.0817
164156
```
165157

166158
The function has the following additional parameters:
167159

168-
- **offset**: starting index for `x`.
160+
- **offsetX**: starting index for `x`.
169161

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
162+
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
171163

172164
```javascript
173165
var Float64Array = require( '@stdlib/array/float64' );
174-
var floor = require( '@stdlib/math/base/special/floor' );
175166

176167
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 );
178168

179-
var v = dstdev.ndarray( N, 1, x, 2, 1 );
180-
// returns 2.5
169+
var v = dstdev.ndarray( 4, 1, x, 2, 1 );
170+
// returns ~2.0817
181171
```
182172

183173
</section>
@@ -202,18 +192,12 @@ var v = dstdev.ndarray( N, 1, x, 2, 1 );
202192
<!-- eslint no-undef: "error" -->
203193

204194
```javascript
205-
var randu = require( '@stdlib/random/base/randu' );
206-
var round = require( '@stdlib/math/base/special/round' );
207-
var Float64Array = require( '@stdlib/array/float64' );
195+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
208196
var dstdev = require( '@stdlib/stats/base/dstdev' );
209197

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

219203
var v = dstdev( x.length, 1, x, 1 );
@@ -224,6 +208,125 @@ console.log( v );
224208

225209
<!-- /.examples -->
226210

211+
<!-- C interface documentation. -->
212+
213+
* * *
214+
215+
<section class="c">
216+
217+
## C APIs
218+
219+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
220+
221+
<section class="intro">
222+
223+
</section>
224+
225+
<!-- /.intro -->
226+
227+
<!-- C usage documentation. -->
228+
229+
<section class="usage">
230+
231+
### Usage
232+
233+
```c
234+
#include "stdlib/stats/base/dstdev.h"
235+
```
236+
237+
#### stdlib_strided_dstdev( N, correction, \*X, strideX )
238+
239+
Computes the [standard deviation][standard-deviation] of a double-precision floating-point strided array `x`.
240+
241+
```c
242+
const double x[] = { 1.0, -2.0, 2.0 };
243+
244+
double v = stdlib_strided_dstdev( 3, 1.0, x, 1 );
245+
// returns ~2.0817
246+
```
247+
248+
The function accepts the following arguments:
249+
250+
- **N**: `[in] CBLAS_INT` number of indexed elements.
251+
- **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).
252+
- **x**: `[in] double*` input array.
253+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
254+
255+
```c
256+
double stdlib_strided_dstdev( const CBLAS_INT N, const double correction, const double *X, const CBLAS_INT strideX );
257+
```
258+
259+
#### stdlib_strided_dstdev_ndarray( N, correction, \*X, strideX, offsetX )
260+
261+
Computes the [standard deviation][standard-deviation] of a double-precision floating-point strided array using alternative indexing semantics.
262+
263+
```c
264+
const double x[] = { 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 };
265+
266+
double v = stdlib_strided_dstdev_ndarray( 4, 1.0, x, 2, 1 );
267+
// returns ~2.0817
268+
```
269+
270+
The function accepts the following arguments:
271+
272+
- **N**: `[in] CBLAS_INT` number of indexed elements.
273+
- **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).
274+
- **X**: `[in] double*` input array.
275+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
276+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
277+
278+
```c
279+
double stdlib_strided_dstdev_ndarray( const CBLAS_INT N, const double correction, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
280+
```
281+
282+
</section>
283+
284+
<!-- /.usage -->
285+
286+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
287+
288+
<section class="notes">
289+
290+
</section>
291+
292+
<!-- /.notes -->
293+
294+
<!-- C API usage examples. -->
295+
296+
<section class="examples">
297+
298+
### Examples
299+
300+
```c
301+
#include "stdlib/stats/base/dstdev.h"
302+
#include <stdio.h>
303+
304+
int main( void ) {
305+
// Create a strided array:
306+
const double x[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 };
307+
308+
// Specify the number of elements:
309+
const int N = 4;
310+
311+
// Specify the stride length:
312+
const int strideX = 2;
313+
314+
// Compute the standard error of the mean:
315+
double v = stdlib_strided_dstdev( N, 1.0, x, strideX );
316+
317+
// Print the result:
318+
printf( "sample standard deviation: %lf\n", v );
319+
}
320+
```
321+
322+
</section>
323+
324+
<!-- /.examples -->
325+
326+
</section>
327+
328+
<!-- /.c -->
329+
227330
<section class="references">
228331
229332
</section>

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

3130

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

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