Skip to content

Commit 7b3f845

Browse files
feat: ndarray-dsnanmean
1 parent 8785e54 commit 7b3f845

25 files changed

+403
-305
lines changed

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

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,107 @@ console.log( v );
181181

182182
<!-- /.examples -->
183183

184+
<!-- C usage documentation. -->
185+
186+
<section class="usage">
187+
188+
### Usage
189+
190+
```c
191+
#include "stdlib/stats/base/dsnanmean.h"
192+
```
193+
194+
#### stdlib_strided_dsnanmean( N, \*X, strideX )
195+
196+
Calculate the Arithmetic Mean value of a double-precision floating-point strided array, ignoring `NaN` values.
197+
198+
```c
199+
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 };
200+
201+
double v = stdlib_strided_dsnanmean( 5, x, 2 );
202+
// returns 1.25
203+
```
204+
205+
The function accepts the following arguments:
206+
207+
- **N**: `[in] CBLAS_INT` number of indexed elements.
208+
- **X**: `[in] float*` input array.
209+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
210+
211+
```c
212+
double stdlib_strided_dsnanmean( const CBLAS_INT N, const float *X, const CBLAS_INT strideX );
213+
```
214+
215+
#### stdlib_strided_dsnanmean_ndarray( N, \*X, strideX, offsetX )
216+
217+
Computes the Arithmetic Mean value of a double-precision floating-point strided array, ignoring `NaN` values and using alternative indexing semantics.
218+
219+
```c
220+
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 };
221+
222+
double v = stdlib_strided_dsnanmean_ndarray( 5, x, 2, 0 );
223+
// returns 1.25
224+
```
225+
226+
The function accepts the following arguments:
227+
228+
- **N**: `[in] CBLAS_INT` number of indexed elements.
229+
- **X**: `[in] float*` input array.
230+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
231+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
232+
233+
```c
234+
double stdlib_strided_dsnanmean_ndarray( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
235+
```
236+
237+
</section>
238+
239+
<!-- /.usage -->
240+
241+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
242+
243+
<section class="notes">
244+
245+
</section>
246+
247+
<!-- /.notes -->
248+
249+
<!-- C API usage examples. -->
250+
251+
<section class="examples">
252+
253+
### Examples
254+
255+
```c
256+
#include "stdlib/stats/base/dsnanmean.h"
257+
#include <stdio.h>
258+
259+
int main( void ) {
260+
// Create a strided array:
261+
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 };
262+
263+
// Specify the number of elements:
264+
const int N = 6;
265+
266+
// Specify the stride length:
267+
const int strideX = 2;
268+
269+
// Compute the arithmetic mean:
270+
double v = stdlib_strided_dsnanmean( N, x, strideX );
271+
272+
// Print the result:
273+
printf( "mean: %f\n", v );
274+
}
275+
```
276+
277+
</section>
278+
279+
<!-- /.examples -->
280+
281+
</section>
282+
283+
<!-- /.c -->
284+
184285
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
185286
186287
<section class="related">

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

Lines changed: 9 additions & 13 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 Float32Array = require( '@stdlib/array/float32' );
2827
var pkg = require( './../package.json' ).name;
2928
var dsnanmean = require( './../lib/dsnanmean.js' );
3029

3130

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

3440
/**
@@ -39,17 +45,7 @@ var dsnanmean = require( './../lib/dsnanmean.js' );
3945
* @returns {Function} benchmark function
4046
*/
4147
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-
}
48+
var x = uniform( len, -10.0, 10.0, options );
5349
return benchmark;
5450

5551
function benchmark( b ) {

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

Lines changed: 5 additions & 13 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 Float32Array = require( '@stdlib/array/float32' );
2928
var tryRequire = require( '@stdlib/utils/try-require' );
3029
var pkg = require( './../package.json' ).name;
3130

@@ -36,6 +35,9 @@ var dsnanmean = tryRequire( resolve( __dirname, './../lib/dsnanmean.native.js' )
3635
var opts = {
3736
'skip': ( dsnanmean instanceof Error )
3837
};
38+
var options = {
39+
'dtype': 'float32'
40+
};
3941

4042

4143
// FUNCTIONS //
@@ -48,17 +50,7 @@ var opts = {
4850
* @returns {Function} benchmark function
4951
*/
5052
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-
}
53+
var x = uniform( len, -10.0, 10.0, options );
6254
return benchmark;
6355

6456
function benchmark( b ) {

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

Lines changed: 9 additions & 13 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 Float32Array = require( '@stdlib/array/float32' );
2827
var pkg = require( './../package.json' ).name;
2928
var dsnanmean = require( './../lib/ndarray.js' );
3029

3130

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

3440
/**
@@ -39,17 +45,7 @@ var dsnanmean = require( './../lib/ndarray.js' );
3945
* @returns {Function} benchmark function
4046
*/
4147
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-
}
48+
var x = uniform( len, -10.0, 10.0, options );
5349
return benchmark;
5450

5551
function benchmark( b ) {

lib/node_modules/@stdlib/stats/base/dsnanmean/benchmark/benchmark.ndarray.native.js

Lines changed: 5 additions & 13 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 Float32Array = require( '@stdlib/array/float32' );
2928
var tryRequire = require( '@stdlib/utils/try-require' );
3029
var pkg = require( './../package.json' ).name;
3130

@@ -36,6 +35,9 @@ var dsnanmean = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) )
3635
var opts = {
3736
'skip': ( dsnanmean instanceof Error )
3837
};
38+
var options = {
39+
'dtype': 'float32'
40+
};
3941

4042

4143
// FUNCTIONS //
@@ -48,17 +50,7 @@ var opts = {
4850
* @returns {Function} benchmark function
4951
*/
5052
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-
}
53+
var x = uniform( len, -10.0, 10.0, options );
6254
return benchmark;
6355

6456
function benchmark( b ) {

lib/node_modules/@stdlib/stats/base/dsnanmean/benchmark/c/benchmark.length.c

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,19 +94,20 @@ static float rand_float( void ) {
9494
* @param len array length
9595
* @return elapsed time in seconds
9696
*/
97-
static double benchmark( int iterations, int len ) {
97+
static double benchmark1( int iterations, int len ) {
9898
double elapsed;
9999
float x[ len ];
100-
double v;
100+
float v;
101101
double t;
102102
int i;
103103

104104
for ( i = 0; i < len; i++ ) {
105105
x[ i ] = ( rand_float()*20000.0f ) - 10000.0f;
106106
}
107-
v = 0.0;
107+
v = 0.0f;
108108
t = tic();
109109
for ( i = 0; i < iterations; i++ ) {
110+
// cppcheck-suppress uninitvar
110111
v = stdlib_strided_dsnanmean( len, x, 1 );
111112
if ( v != v ) {
112113
printf( "should not return NaN\n" );
@@ -120,6 +121,40 @@ static double benchmark( int iterations, int len ) {
120121
return elapsed;
121122
}
122123

124+
/**
125+
* Runs a benchmark.
126+
*
127+
* @param iterations number of iterations
128+
* @param len array length
129+
* @return elapsed time in seconds
130+
*/
131+
static double benchmark2( int iterations, int len ) {
132+
double elapsed;
133+
float x[ len ];
134+
float v;
135+
double t;
136+
int i;
137+
138+
for ( i = 0; i < len; i++ ) {
139+
x[ i ] = ( rand_float()*20000.0f ) - 10000.0f;
140+
}
141+
v = 0.0f;
142+
t = tic();
143+
for ( i = 0; i < iterations; i++ ) {
144+
// cppcheck-suppress uninitvar
145+
v = stdlib_strided_dsnanmean_ndarray( len, x, 1, 0 );
146+
if ( v != v ) {
147+
printf( "should not return NaN\n" );
148+
break;
149+
}
150+
}
151+
elapsed = tic() - t;
152+
if ( v != v ) {
153+
printf( "should not return NaN\n" );
154+
}
155+
return elapsed;
156+
}
157+
123158
/**
124159
* Main execution sequence.
125160
*/
@@ -142,7 +177,18 @@ int main( void ) {
142177
for ( j = 0; j < REPEATS; j++ ) {
143178
count += 1;
144179
printf( "# c::%s:len=%d\n", NAME, len );
145-
elapsed = benchmark( iter, len );
180+
elapsed = benchmark1( iter, len );
181+
print_results( iter, elapsed );
182+
printf( "ok %d benchmark finished\n", count );
183+
}
184+
}
185+
for ( i = MIN; i <= MAX; i++ ) {
186+
len = pow( 10, i );
187+
iter = ITERATIONS / pow( 10, i-1 );
188+
for ( j = 0; j < REPEATS; j++ ) {
189+
count += 1;
190+
printf( "# c::%s:ndarray:len=%d\n", NAME, len );
191+
elapsed = benchmark2( iter, len );
146192
print_results( iter, elapsed );
147193
printf( "ok %d benchmark finished\n", count );
148194
}

0 commit comments

Comments
 (0)