Skip to content

Commit 2b334ad

Browse files
feat: add C ndarray interface and refactor implementation for stats/base/snanmeanpn
PR-URL: #4777 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Reviewed-by: Aayush Khanna <[email protected]> Signed-off-by: Athan Reines <[email protected]>
1 parent 42252d8 commit 2b334ad

23 files changed

+442
-304
lines changed

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

+146-37
Original file line numberDiff line numberDiff line change
@@ -51,84 +51,79 @@ The [arithmetic mean][arithmetic-mean] is defined as
5151
var snanmeanpn = require( '@stdlib/stats/base/snanmeanpn' );
5252
```
5353

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

56-
Computes the [arithmetic mean][arithmetic-mean] of a single-precision floating-point strided array `x`, ignoring `NaN` values and using a two-pass error correction algorithm.
56+
Computes the [arithmetic mean][arithmetic-mean] of a single-precision floating-point strided array, ignoring `NaN` values and using a two-pass error correction algorithm.
5757

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

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

64-
var v = snanmeanpn( N, x, 1 );
63+
var v = snanmeanpn( 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 [`Float32Array`][@stdlib/array/float32].
72-
- **stride**: index increment for `x`.
71+
- **strideX**: stride length for `x`.
72+
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`,
7374

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`,
75+
<!-- eslint-disable max-len -->
7576

7677
```javascript
7778
var Float32Array = require( '@stdlib/array/float32' );
78-
var floor = require( '@stdlib/math/base/special/floor' );
7979

80-
var x = new Float32Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0, NaN ] );
81-
var N = floor( x.length / 2 );
80+
var x = new Float32Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0, NaN, NaN ] );
8281

83-
var v = snanmeanpn( N, x, 2 );
82+
var v = snanmeanpn( 5, x, 2 );
8483
// returns 1.25
8584
```
8685

8786
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
8887

89-
<!-- eslint-disable stdlib/capitalized-comments -->
88+
<!-- eslint-disable stdlib/capitalized-comments, max-len -->
9089

9190
```javascript
9291
var Float32Array = require( '@stdlib/array/float32' );
93-
var floor = require( '@stdlib/math/base/special/floor' );
9492

95-
var x0 = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN ] );
93+
var x0 = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] );
9694
var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
9795

98-
var N = floor( x0.length / 2 );
99-
100-
var v = snanmeanpn( N, x1, 2 );
96+
var v = snanmeanpn( 5, x1, 2 );
10197
// returns 1.25
10298
```
10399

104-
#### snanmeanpn.ndarray( N, x, stride, offset )
100+
#### snanmeanpn.ndarray( N, x, strideX, offsetX )
105101

106102
Computes the [arithmetic mean][arithmetic-mean] of a single-precision floating-point strided array, ignoring `NaN` values and using a two-pass error correction algorithm and alternative indexing semantics.
107103

108104
```javascript
109105
var Float32Array = require( '@stdlib/array/float32' );
110106

111107
var x = new Float32Array( [ 1.0, -2.0, NaN, 2.0 ] );
112-
var N = x.length;
113108

114-
var v = snanmeanpn.ndarray( N, x, 1, 0 );
109+
var v = snanmeanpn.ndarray( x.length, x, 1, 0 );
115110
// returns ~0.33333
116111
```
117112

118113
The function has the following additional parameters:
119114

120-
- **offset**: starting index for `x`.
115+
- **offsetX**: starting index for `x`.
121116

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
117+
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
118+
119+
<!-- eslint-disable max-len -->
123120

124121
```javascript
125122
var Float32Array = require( '@stdlib/array/float32' );
126-
var floor = require( '@stdlib/math/base/special/floor' );
127123

128-
var x = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN ] );
129-
var N = floor( x.length / 2 );
124+
var x = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] );
130125

131-
var v = snanmeanpn.ndarray( N, x, 2, 1 );
126+
var v = snanmeanpn.ndarray( 5, x, 2, 1 );
132127
// returns 1.25
133128
```
134129

@@ -154,22 +149,19 @@ var v = snanmeanpn.ndarray( N, x, 2, 1 );
154149
<!-- eslint no-undef: "error" -->
155150

156151
```javascript
157-
var randu = require( '@stdlib/random/base/randu' );
158-
var round = require( '@stdlib/math/base/special/round' );
159-
var Float32Array = require( '@stdlib/array/float32' );
152+
var uniform = require( '@stdlib/random/base/uniform' );
153+
var filledarrayBy = require( '@stdlib/array/filled-by' );
154+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
160155
var snanmeanpn = require( '@stdlib/stats/base/snanmeanpn' );
161156

162-
var x;
163-
var i;
164-
165-
x = new Float32Array( 10 );
166-
for ( i = 0; i < x.length; i++ ) {
167-
if ( randu() < 0.2 ) {
168-
x[ i ] = NaN;
169-
} else {
170-
x[ i ] = round( (randu()*100.0) - 50.0 );
157+
function rand() {
158+
if ( bernoulli( 0.8 ) < 1 ) {
159+
return NaN;
171160
}
161+
return uniform( -50.0, 50.0 );
172162
}
163+
164+
var x = filledarrayBy( 10, 'float32', rand );
173165
console.log( x );
174166

175167
var v = snanmeanpn( x.length, x, 1 );
@@ -180,6 +172,123 @@ console.log( v );
180172

181173
<!-- /.examples -->
182174

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

lib/node_modules/@stdlib/stats/base/snanmeanpn/benchmark/benchmark.js

+17-13
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,30 @@
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/base/uniform' );
27+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
28+
var filledarrayBy = require( '@stdlib/array/filled-by' );
2829
var pkg = require( './../package.json' ).name;
2930
var snanmeanpn = require( './../lib/snanmeanpn.js' );
3031

3132

3233
// FUNCTIONS //
3334

35+
/**
36+
* Returns a random value or `NaN`.
37+
*
38+
* @private
39+
* @returns {number} random number or `NaN`
40+
*/
41+
function rand() {
42+
if ( bernoulli( 0.8 ) < 1 ) {
43+
return NaN;
44+
}
45+
return uniform( -10.0, 10.0 );
46+
}
47+
3448
/**
3549
* Creates a benchmark function.
3650
*
@@ -39,17 +53,7 @@ var snanmeanpn = require( './../lib/snanmeanpn.js' );
3953
* @returns {Function} benchmark function
4054
*/
4155
function createBenchmark( len ) {
42-
var x;
43-
var i;
44-
45-
x = new Float32Array( len );
46-
for ( i = 0; i < x.length; i++ ) {
47-
if ( randu() < 0.2 ) {
48-
x[ i ] = NaN;
49-
} else {
50-
x[ i ] = ( randu()*20.0 ) - 10.0;
51-
}
52-
}
56+
var x = filledarrayBy( len, 'float32', rand );
5357
return benchmark;
5458

5559
function benchmark( b ) {

lib/node_modules/@stdlib/stats/base/snanmeanpn/benchmark/benchmark.native.js

+17-13
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@
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/base/uniform' );
28+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
29+
var filledarrayBy = require( '@stdlib/array/filled-by' );
2930
var tryRequire = require( '@stdlib/utils/try-require' );
3031
var pkg = require( './../package.json' ).name;
3132

@@ -40,6 +41,19 @@ var opts = {
4041

4142
// FUNCTIONS //
4243

44+
/**
45+
* Returns a random value or `NaN`.
46+
*
47+
* @private
48+
* @returns {number} random number or `NaN`
49+
*/
50+
function rand() {
51+
if ( bernoulli( 0.8 ) < 1 ) {
52+
return NaN;
53+
}
54+
return uniform( -10.0, 10.0 );
55+
}
56+
4357
/**
4458
* Creates a benchmark function.
4559
*
@@ -48,17 +62,7 @@ var opts = {
4862
* @returns {Function} benchmark function
4963
*/
5064
function createBenchmark( len ) {
51-
var x;
52-
var i;
53-
54-
x = new Float32Array( len );
55-
for ( i = 0; i < x.length; i++ ) {
56-
if ( randu() < 0.2 ) {
57-
x[ i ] = NaN;
58-
} else {
59-
x[ i ] = ( randu()*20.0 ) - 10.0;
60-
}
61-
}
65+
var x = filledarrayBy( len, 'float32', rand );
6266
return benchmark;
6367

6468
function benchmark( b ) {

0 commit comments

Comments
 (0)