Skip to content

Commit 02f3600

Browse files
feat: ndarray-dsnanmeanors
1 parent 8785e54 commit 02f3600

25 files changed

+398
-369
lines changed

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

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

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

185+
<!-- C usage documentation. -->
186+
187+
<section class="usage">
188+
189+
### Usage
190+
191+
```c
192+
#include "stdlib/stats/base/dsnanmeanors.h"
193+
```
194+
195+
#### stdlib_strided_dsnanmeanors( N, \*X, strideX )
196+
197+
Calculate the Arithmetic Mean value of a single-precision floating-point strided array, ignoring `NaN` values.
198+
199+
```c
200+
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 };
201+
202+
double v = stdlib_strided_dsnanmeanors( 5, x, 2 );
203+
// returns 1.25
204+
```
205+
206+
The function accepts the following arguments:
207+
208+
- **N**: `[in] CBLAS_INT` number of indexed elements.
209+
- **X**: `[in] float*` input array.
210+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
211+
212+
```c
213+
double stdlib_strided_dsnanmeanors( const CBLAS_INT N, const float *X, const CBLAS_INT strideX );
214+
```
215+
216+
#### stdlib_strided_dsnanmeanors_ndarray( N, \*X, strideX, offsetX )
217+
218+
Computes the Arithmetic Mean value of a single-precision floating-point strided array, ignoring `NaN` values and using alternative indexing semantics.
219+
220+
```c
221+
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 };
222+
223+
double v = stdlib_strided_dsnanmeanors_ndarray( 5, x, 2, 0 );
224+
// returns 1.25
225+
```
226+
227+
The function accepts the following arguments:
228+
229+
- **N**: `[in] CBLAS_INT` number of indexed elements.
230+
- **X**: `[in] float*` 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_dsnanmeanors_ndarray( const CBLAS_INT N, const float *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/dsnanmeanors.h"
258+
#include <stdio.h>
259+
260+
int main( void ) {
261+
// Create a strided array:
262+
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 };
263+
264+
// Specify the number of elements:
265+
const int N = 6;
266+
267+
// Specify the stride length:
268+
const int strideX = 2;
269+
270+
// Compute the arithmetic mean:
271+
double v = stdlib_strided_dsnanmeanors( N, x, strideX );
272+
273+
// Print the result:
274+
printf( "mean: %f\n", v );
275+
}
276+
```
277+
278+
</section>
279+
280+
<!-- /.examples -->
281+
282+
</section>
283+
284+
<!-- /.c -->
285+
185286
<section class="references">
186287
187288
</section>

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

3130

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

3440
/**
@@ -39,17 +45,7 @@ var dsnanmeanors = require( './../lib/dsnanmeanors.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/dsnanmeanors/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 dsnanmeanors = tryRequire( resolve( __dirname, './../lib/dsnanmeanors.native
3635
var opts = {
3736
'skip': ( dsnanmeanors 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/dsnanmeanors/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 dsnanmeanors = 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 dsnanmeanors = 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/dsnanmeanors/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 dsnanmeanors = tryRequire( resolve( __dirname, './../lib/ndarray.native.js'
3635
var opts = {
3736
'skip': ( dsnanmeanors 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/dsnanmeanors/benchmark/c/benchmark.length.c

Lines changed: 49 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_dsnanmeanors( len, x, 1 );
111112
if ( v != v ) {
112113
printf( "should not return NaN\n" );
@@ -120,6 +121,39 @@ 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_dsnanmeanors_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+
}
123157
/**
124158
* Main execution sequence.
125159
*/
@@ -142,7 +176,18 @@ int main( void ) {
142176
for ( j = 0; j < REPEATS; j++ ) {
143177
count += 1;
144178
printf( "# c::%s:len=%d\n", NAME, len );
145-
elapsed = benchmark( iter, len );
179+
elapsed = benchmark1( iter, len );
180+
print_results( iter, elapsed );
181+
printf( "ok %d benchmark finished\n", count );
182+
}
183+
}
184+
for ( i = MIN; i <= MAX; i++ ) {
185+
len = pow( 10, i );
186+
iter = ITERATIONS / pow( 10, i-1 );
187+
for ( j = 0; j < REPEATS; j++ ) {
188+
count += 1;
189+
printf( "# c::%s:ndarray:len=%d\n", NAME, len );
190+
elapsed = benchmark2( iter, len );
146191
print_results( iter, elapsed );
147192
printf( "ok %d benchmark finished\n", count );
148193
}

0 commit comments

Comments
 (0)