Skip to content

Commit 9c85c1e

Browse files
feat:ndarray-dsnanmeanwd
1 parent 321e287 commit 9c85c1e

24 files changed

+455
-355
lines changed

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

Lines changed: 112 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,36 +51,33 @@ The [arithmetic mean][arithmetic-mean] is defined as
5151
var dsnanmeanwd = require( '@stdlib/stats/base/dsnanmeanwd' );
5252
```
5353

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

5656
Computes the [arithmetic mean][arithmetic-mean] of a single-precision floating-point strided array `x`, ignoring `NaN` values, using Welford's algorithm with extended accumulation, and returning an extended precision result.
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 = dsnanmeanwd( N, x, 1 );
63+
var v = dsnanmeanwd( 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`.
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 `x` are accessed at runtime. For example, to compute the [arithmetic mean][arithmetic-mean] of every other element in `x`,
7574

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

8078
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 );
8279

83-
var v = dsnanmeanwd( N, x, 2 );
80+
var v = dsnanmeanwd( 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 Float32Array = require( '@stdlib/array/float32' );
93-
var floor = require( '@stdlib/math/base/special/floor' );
9490

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

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

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

106100
Computes the [arithmetic mean][arithmetic-mean] of a single-precision floating-point strided array, ignoring `NaN` values and using Welford's algorithm with extended accumulation and alternative indexing semantics.
107101

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

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

114-
var v = dsnanmeanwd.ndarray( N, x, 1, 0 );
107+
var v = dsnanmeanwd.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 Float32Array = require( '@stdlib/array/float32' );
126-
var floor = require( '@stdlib/math/base/special/floor' );
127119

128120
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 );
130121

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

@@ -181,6 +172,107 @@ console.log( v );
181172

182173
<!-- /.examples -->
183174

175+
<!-- C usage documentation. -->
176+
177+
<section class="usage">
178+
179+
### Usage
180+
181+
```c
182+
#include "stdlib/stats/base/dsnanmeanwd.h"
183+
```
184+
185+
#### stdlib_strided_dsnanmeanwd( N, \*X, strideX )
186+
187+
Computes the arithmetic mean of a single-precision floating-point strided array `x`, ignoring `NaN` values, using Welford's algorithm with extended accumulation, and returning an extended precision result.
188+
189+
```c
190+
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 };
191+
192+
double v = stdlib_strided_dsnanmeanwd( 6, x, 2 );
193+
// returns 1.25
194+
```
195+
196+
The function accepts the following arguments:
197+
198+
- **N**: `[in] CBLAS_INT` number of indexed elements.
199+
- **X**: `[in] float*` input array.
200+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
201+
202+
```c
203+
double stdlib_strided_dsnanmeanwd( const CBLAS_INT N, const float *X, const CBLAS_INT strideX );
204+
```
205+
206+
#### stdlib_strided_dsnanmeanwd_ndarray( N, \*X, strideX, offsetX )
207+
208+
Computes the arithmetic mean of a single-precision floating-point strided array, ignoring `NaN` values and using Welford's algorithm with extended accumulation and alternative indexing semantics.
209+
210+
```c
211+
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 };
212+
213+
double v = stdlib_strided_dsnanmeanwd_ndarray( 6, x, 2, 0 );
214+
// returns 1.25
215+
```
216+
217+
The function accepts the following arguments:
218+
219+
- **N**: `[in] CBLAS_INT` number of indexed elements.
220+
- **X**: `[in] float*` input array.
221+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
222+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
223+
224+
```c
225+
double stdlib_strided_dsnanmeanwd_ndarray( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
226+
```
227+
228+
</section>
229+
230+
<!-- /.usage -->
231+
232+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
233+
234+
<section class="notes">
235+
236+
</section>
237+
238+
<!-- /.notes -->
239+
240+
<!-- C API usage examples. -->
241+
242+
<section class="examples">
243+
244+
### Examples
245+
246+
```c
247+
#include "stdlib/stats/base/dsnanmeanwd.h"
248+
#include <stdio.h>
249+
250+
int main( void ) {
251+
// Create a strided array:
252+
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 };
253+
254+
// Specify the number of elements:
255+
const int N = 6;
256+
257+
// Specify the stride length:
258+
const int strideX = 2;
259+
260+
// Compute the arithmetic mean:
261+
double v = stdlib_strided_dsnanmeanwd( N, x, strideX );
262+
263+
// Print the result:
264+
printf( "mean: %f\n", v );
265+
}
266+
```
267+
268+
</section>
269+
270+
<!-- /.examples -->
271+
272+
</section>
273+
274+
<!-- /.c -->
275+
184276
* * *
185277
186278
<section class="references">

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

Lines changed: 17 additions & 13 deletions
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' );
24+
var uniform = require( '@stdlib/random/base/uniform' );
25+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
26+
var filledarrayBy = require( '@stdlib/array/filled-by' );
2527
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2628
var pow = require( '@stdlib/math/base/special/pow' );
27-
var Float32Array = require( '@stdlib/array/float32' );
2829
var pkg = require( './../package.json' ).name;
2930
var dsnanmeanwd = require( './../lib/dsnanmeanwd.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.2 ) ) {
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 dsnanmeanwd = require( './../lib/dsnanmeanwd.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/dsnanmeanwd/benchmark/benchmark.native.js

Lines changed: 17 additions & 13 deletions
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' );
25+
var uniform = require( '@stdlib/random/base/uniform' );
26+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
27+
var filledarrayBy = require( '@stdlib/array/filled-by' );
2628
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2729
var pow = require( '@stdlib/math/base/special/pow' );
28-
var Float32Array = require( '@stdlib/array/float32' );
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.2 ) ) {
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 ) {

lib/node_modules/@stdlib/stats/base/dsnanmeanwd/benchmark/benchmark.ndarray.js

Lines changed: 17 additions & 13 deletions
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' );
24+
var uniform = require( '@stdlib/random/base/uniform' );
25+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
26+
var filledarrayBy = require( '@stdlib/array/filled-by' );
2527
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2628
var pow = require( '@stdlib/math/base/special/pow' );
27-
var Float32Array = require( '@stdlib/array/float32' );
2829
var pkg = require( './../package.json' ).name;
2930
var dsnanmeanwd = require( './../lib/ndarray.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.2 ) ) {
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 dsnanmeanwd = require( './../lib/ndarray.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 ) {

0 commit comments

Comments
 (0)