Skip to content

Commit 10d1d28

Browse files
feat: add C ndarray and refactor for stats/base/sdsnanmeanors
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: na - task: lint_c_benchmarks status: passed - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: na - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: na ---
1 parent a3cd058 commit 10d1d28

File tree

10 files changed

+245
-85
lines changed

10 files changed

+245
-85
lines changed

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

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,109 @@ 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/sdsnanmeanors.h"
192+
```
193+
194+
#### stdlib_strided_sdsnanmeanors( N, \*X, strideX )
195+
196+
Computes the [arithmetic mean][arithmetic-mean] of a single-precision floating-point strided array, ignoring `NaN` values and using ordinary recursive summation with extended accumulation.
197+
198+
```c
199+
const float x[] = { 1.0, -2.0, 0.0/0.0, 2.0 };
200+
201+
double v = stdlib_strided_sdsnanmeanors( 4, x, 1 );
202+
// returns ~0.3333
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_sdsnanmeanors( const CBLAS_INT N, const double *X, const CBLAS_INT strideX );
213+
```
214+
215+
#### stdlib_strided_sdsnanmeanors_ndarray( N, \*X, strideX, offsetX )
216+
217+
Computes the [arithmetic mean][arithmetic-mean] of a single-precision floating-point strided array, ignoring `NaN` values and using ordinary recursive summation with extended accumulation and alternative indexing semantics.
218+
219+
```c
220+
const float x[] = { 1.0, -2.0, 0.0/0.0, 2.0 };
221+
222+
double v = stdlib_strided_sdsnanmeanors_ndarray( 4, x, 1, 0 );
223+
// returns ~0.3333
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_sdsnanmeanors_ndarray( const CBLAS_INT N, const double *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/sdsnanmeanors.h"
257+
#include <stdio.h>
258+
259+
int main( void ) {
260+
// Create a strided array:
261+
const float x[] = { 1.0, 2.0, 0.0/0.0, 3.0, 0.0/0.0, 4.0, 5.0, 6.0, 0.0/0.0, 7.0, 8.0, 0.0/0.0 };
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_sdsnanmeanors( N, x, strideX );
271+
272+
// Print the result:
273+
printf( "mean: %lf\n", v );
274+
}
275+
```
276+
277+
</section>
278+
279+
<!-- /.examples -->
280+
281+
</section>
282+
283+
<!-- /.c -->
284+
285+
* * *
286+
184287
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
185288
186289
<section class="related">

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

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ 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 ];
100100
float v;
@@ -107,6 +107,7 @@ static double benchmark( int iterations, int len ) {
107107
v = 0.0f;
108108
t = tic();
109109
for ( i = 0; i < iterations; i++ ) {
110+
// cppcheck-suppress uninitvar
110111
v = stdlib_strided_sdsnanmeanors( 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_sdsnanmeanors_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
}

lib/node_modules/@stdlib/stats/base/sdsnanmeanors/include/stdlib/stats/base/sdsnanmeanors.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#ifndef STDLIB_STATS_BASE_SDSNANMEANORS_H
2020
#define STDLIB_STATS_BASE_SDSNANMEANORS_H
2121

22-
#include <stdint.h>
22+
#include "stdlib/blas/base/shared.h"
2323

2424
/*
2525
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
@@ -31,7 +31,12 @@ extern "C" {
3131
/**
3232
* Computes the arithmetic mean of a single-precision floating-point strided array, ignoring `NaN` values and using ordinary recursive summation with extended accumulation.
3333
*/
34-
float stdlib_strided_sdsnanmeanors( const int64_t N, const float *X, const int64_t stride );
34+
float API_SUFFIX( stdlib_strided_sdsnanmeanors )( const CBLAS_INT N, const float *X, const CBLAS_INT strideX );
35+
36+
/**
37+
* Computes the arithmetic mean of a single-precision floating-point strided array, ignoring `NaN` values and using ordinary recursive summation with extended accumulation.
38+
*/
39+
float API_SUFFIX( stdlib_strided_sdsnanmeanors_ndarray )( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
3540

3641
#ifdef __cplusplus
3742
}

lib/node_modules/@stdlib/stats/base/sdsnanmeanors/lib/ndarray.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' );
3030
*
3131
* @param {PositiveInteger} N - number of indexed elements
3232
* @param {Float32Array} x - input array
33-
* @param {integer} stride - stride length
34-
* @param {NonNegativeInteger} offset - starting index
33+
* @param {integer} strideX - stride length
34+
* @param {NonNegativeInteger} offsetX - starting index
3535
* @returns {number} arithmetic mean
3636
*
3737
* @example
@@ -44,7 +44,7 @@ var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' );
4444
* var v = sdsnanmeanors( N, x, 2, 1 );
4545
* // returns 1.25
4646
*/
47-
function sdsnanmeanors( N, x, stride, offset ) {
47+
function sdsnanmeanors( N, x, strideX, offsetX ) {
4848
var sum;
4949
var ix;
5050
var v;
@@ -54,10 +54,10 @@ function sdsnanmeanors( N, x, stride, offset ) {
5454
if ( N <= 0 ) {
5555
return NaN;
5656
}
57-
if ( N === 1 || stride === 0 ) {
58-
return x[ offset ];
57+
if ( N === 1 || strideX === 0 ) {
58+
return x[ offsetX ];
5959
}
60-
ix = offset;
60+
ix = offsetX;
6161
sum = 0.0;
6262
n = 0;
6363
for ( i = 0; i < N; i++ ) {
@@ -66,7 +66,7 @@ function sdsnanmeanors( N, x, stride, offset ) {
6666
sum += v;
6767
n += 1;
6868
}
69-
ix += stride;
69+
ix += strideX;
7070
}
7171
if ( n === 0 ) {
7272
return NaN;

lib/node_modules/@stdlib/stats/base/sdsnanmeanors/lib/ndarray.native.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020

2121
// MODULES //
2222

23-
var Float32Array = require( '@stdlib/array/float32' );
24-
var addon = require( './sdsnanmeanors.native.js' );
23+
var addon = require( './../src/addon.node' );
2524

2625

2726
// MAIN //
@@ -31,8 +30,8 @@ var addon = require( './sdsnanmeanors.native.js' );
3130
*
3231
* @param {PositiveInteger} N - number of indexed elements
3332
* @param {Float32Array} x - input array
34-
* @param {integer} stride - stride length
35-
* @param {NonNegativeInteger} offset - starting index
33+
* @param {integer} strideX - stride length
34+
* @param {NonNegativeInteger} offsetX - starting index
3635
* @returns {number} arithmetic mean
3736
*
3837
* @example
@@ -43,15 +42,10 @@ var addon = require( './sdsnanmeanors.native.js' );
4342
* var N = floor( x.length / 2 );
4443
*
4544
* var v = sdsnanmeanors( N, x, 2, 1 );
46-
* // returns 1.25
45+
* //returns 1.25
4746
*/
48-
function sdsnanmeanors( N, x, stride, offset ) {
49-
var view;
50-
if ( stride < 0 ) {
51-
offset += (N-1) * stride;
52-
}
53-
view = new Float32Array( x.buffer, x.byteOffset+(x.BYTES_PER_ELEMENT*offset), x.length-offset ); // eslint-disable-line max-len
54-
return addon( N, view, stride );
47+
function sdsnanmeanors( N, x, strideX, offsetX ) {
48+
return addon.ndarray( N, x, strideX, offsetX );
5549
}
5650

5751

lib/node_modules/@stdlib/stats/base/sdsnanmeanors/lib/sdsnanmeanors.js

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020

2121
// MODULES //
2222

23-
var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' );
23+
var stride2offset = require( '@stdlib/strided/base/stride2offset' );
24+
var ndarray = require( './ndarray.js' );
2425

2526

2627
// MAIN //
@@ -30,7 +31,7 @@ var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' );
3031
*
3132
* @param {PositiveInteger} N - number of indexed elements
3233
* @param {Float32Array} x - input array
33-
* @param {integer} stride - stride length
34+
* @param {integer} strideX - stride length
3435
* @returns {number} arithmetic mean
3536
*
3637
* @example
@@ -42,38 +43,9 @@ var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' );
4243
* var v = sdsnanmeanors( N, x, 1 );
4344
* // returns ~0.3333
4445
*/
45-
function sdsnanmeanors( N, x, stride ) {
46-
var sum;
47-
var ix;
48-
var v;
49-
var n;
50-
var i;
51-
52-
if ( N <= 0 ) {
53-
return NaN;
54-
}
55-
if ( N === 1 || stride === 0 ) {
56-
return x[ 0 ];
57-
}
58-
if ( stride < 0 ) {
59-
ix = (1-N) * stride;
60-
} else {
61-
ix = 0;
62-
}
63-
sum = 0.0;
64-
n = 0;
65-
for ( i = 0; i < N; i++ ) {
66-
v = x[ ix ];
67-
if ( v === v ) {
68-
sum += v;
69-
n += 1;
70-
}
71-
ix += stride;
72-
}
73-
if ( n === 0 ) {
74-
return NaN;
75-
}
76-
return float64ToFloat32( sum / n );
46+
function sdsnanmeanors( N, x, strideX ) {
47+
var ox = stride2offset( N, strideX );
48+
return ndarray( N, x, strideX, ox );
7749
}
7850

7951

lib/node_modules/@stdlib/stats/base/sdsnanmeanors/lib/sdsnanmeanors.native.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ var addon = require( './../src/addon.node' );
3030
*
3131
* @param {PositiveInteger} N - number of indexed elements
3232
* @param {Float32Array} x - input array
33-
* @param {integer} stride - stride length
33+
* @param {integer} strideX - stride length
3434
* @returns {number} arithmetic mean
3535
*
3636
* @example
@@ -40,10 +40,10 @@ var addon = require( './../src/addon.node' );
4040
* var N = x.length;
4141
*
4242
* var v = sdsnanmeanors( N, x, 1 );
43-
* // returns ~0.3333
43+
* //returns ~0.3333
4444
*/
45-
function sdsnanmeanors( N, x, stride ) {
46-
return addon( N, x, stride );
45+
function sdsnanmeanors( N, x, strideX ) {
46+
return addon( N, x, strideX );
4747
}
4848

4949

0 commit comments

Comments
 (0)