Skip to content

Commit ce86a5d

Browse files
gururaj1512kgryte
andauthored
feat: add C ndarray interface and refactor implementation for stats/base/dmeanpn
PR-URL: #6510 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Reviewed-by: Aayush Khanna <[email protected]>
1 parent 0283c27 commit ce86a5d

21 files changed

+306
-210
lines changed

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

+129-21
Original file line numberDiff line numberDiff line change
@@ -51,36 +51,33 @@ The [arithmetic mean][arithmetic-mean] is defined as
5151
var dmeanpn = require( '@stdlib/stats/base/dmeanpn' );
5252
```
5353

54-
#### dmeanpn( N, x, stride )
54+
#### dmeanpn( N, x, strideX )
5555

56-
Computes the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array `x` using a two-pass error correction algorithm.
56+
Computes the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array using a two-pass error correction algorithm.
5757

5858
```javascript
5959
var Float64Array = require( '@stdlib/array/float64' );
6060

6161
var x = new Float64Array( [ 1.0, -2.0, 2.0 ] );
62-
var N = x.length;
6362

64-
var v = dmeanpn( N, x, 1 );
63+
var v = dmeanpn( x.length, x, 1 );
6564
// returns ~0.3333
6665
```
6766

6867
The function has the following parameters:
6968

7069
- **N**: number of indexed elements.
7170
- **x**: input [`Float64Array`][@stdlib/array/float64].
72-
- **stride**: index increment for `x`.
71+
- **strideX**: stride length for `x`.
7372

74-
The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to compute the [arithmetic mean][arithmetic-mean] of every other element in `x`,
73+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the [arithmetic mean][arithmetic-mean] of every other element in `x`,
7574

7675
```javascript
7776
var Float64Array = require( '@stdlib/array/float64' );
78-
var floor = require( '@stdlib/math/base/special/floor' );
7977

8078
var x = new Float64Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ] );
81-
var N = floor( x.length / 2 );
8279

83-
var v = dmeanpn( N, x, 2 );
80+
var v = dmeanpn( 4, x, 2 );
8481
// returns 1.25
8582
```
8683

@@ -90,45 +87,39 @@ Note that indexing is relative to the first index. To introduce an offset, use [
9087

9188
```javascript
9289
var Float64Array = require( '@stdlib/array/float64' );
93-
var floor = require( '@stdlib/math/base/special/floor' );
9490

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

98-
var N = floor( x0.length / 2 );
99-
100-
var v = dmeanpn( N, x1, 2 );
94+
var v = dmeanpn( 4, x1, 2 );
10195
// returns 1.25
10296
```
10397

104-
#### dmeanpn.ndarray( N, x, stride, offset )
98+
#### dmeanpn.ndarray( N, x, strideX, offsetX )
10599

106100
Computes the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array using a two-pass error correction algorithm and alternative indexing semantics.
107101

108102
```javascript
109103
var Float64Array = require( '@stdlib/array/float64' );
110104

111105
var x = new Float64Array( [ 1.0, -2.0, 2.0 ] );
112-
var N = x.length;
113106

114-
var v = dmeanpn.ndarray( N, x, 1, 0 );
107+
var v = dmeanpn.ndarray( x.length, x, 1, 0 );
115108
// returns ~0.33333
116109
```
117110

118111
The function has the following additional parameters:
119112

120-
- **offset**: starting index for `x`.
113+
- **offsetX**: starting index for `x`.
121114

122-
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 [arithmetic mean][arithmetic-mean] for every other value in `x` starting from the second value
115+
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 [arithmetic mean][arithmetic-mean] for every other element in `x` starting from the second element
123116

124117
```javascript
125118
var Float64Array = require( '@stdlib/array/float64' );
126-
var floor = require( '@stdlib/math/base/special/floor' );
127119

128120
var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
129-
var N = floor( x.length / 2 );
130121

131-
var v = dmeanpn.ndarray( N, x, 2, 1 );
122+
var v = dmeanpn.ndarray( 4, x, 2, 1 );
132123
// returns 1.25
133124
```
134125

@@ -175,6 +166,123 @@ console.log( v );
175166

176167
<!-- /.examples -->
177168

169+
<!-- C interface documentation. -->
170+
171+
* * *
172+
173+
<section class="c">
174+
175+
## C APIs
176+
177+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
178+
179+
<section class="intro">
180+
181+
</section>
182+
183+
<!-- /.intro -->
184+
185+
<!-- C usage documentation. -->
186+
187+
<section class="usage">
188+
189+
### Usage
190+
191+
```c
192+
#include "stdlib/stats/base/dmeanpn.h"
193+
```
194+
195+
#### stdlib_strided_dmeanpn( N, \*X, strideX )
196+
197+
Computes the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array using a two-pass error correction algorithm.
198+
199+
```c
200+
const double x[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 };
201+
202+
double v = stdlib_strided_dmeanpn( 4, x, 2 );
203+
// returns 4.0
204+
```
205+
206+
The function accepts the following arguments:
207+
208+
- **N**: `[in] CBLAS_INT` number of indexed elements.
209+
- **X**: `[in] double*` input array.
210+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
211+
212+
```c
213+
double stdlib_strided_dmeanpn( const CBLAS_INT N, const double *X, const CBLAS_INT strideX );
214+
```
215+
216+
#### stdlib_strided_dmeanpn_ndarray( N, \*X, strideX, offsetX )
217+
218+
Computes the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array using a two-pass error correction algorithm and alternative indexing semantics.
219+
220+
```c
221+
const double x[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 };
222+
223+
double v = stdlib_strided_dmeanpn_ndarray( 4, x, 2, 0 );
224+
// returns 4.0
225+
```
226+
227+
The function accepts the following arguments:
228+
229+
- **N**: `[in] CBLAS_INT` number of indexed elements.
230+
- **X**: `[in] double*` input array.
231+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
232+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
233+
234+
```c
235+
double stdlib_strided_dmeanpn_ndarray( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
236+
```
237+
238+
</section>
239+
240+
<!-- /.usage -->
241+
242+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
243+
244+
<section class="notes">
245+
246+
</section>
247+
248+
<!-- /.notes -->
249+
250+
<!-- C API usage examples. -->
251+
252+
<section class="examples">
253+
254+
### Examples
255+
256+
```c
257+
#include "stdlib/stats/base/dmeanpn.h"
258+
#include <stdio.h>
259+
260+
int main( void ) {
261+
// Create a strided array:
262+
const double x[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 };
263+
264+
// Specify the number of elements:
265+
const int N = 4;
266+
267+
// Specify the stride length:
268+
const int strideX = 2;
269+
270+
// Compute the arithmetic mean:
271+
double v = stdlib_strided_dmeanpn( N, x, strideX );
272+
273+
// Print the result:
274+
printf( "mean: %lf\n", v );
275+
}
276+
```
277+
278+
</section>
279+
280+
<!-- /.examples -->
281+
282+
</section>
283+
284+
<!-- /.c -->
285+
178286
* * *
179287
180288
<section class="references">

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

3130

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

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