Skip to content

Commit 2887967

Browse files
feat: add C ndarray implementation for stats/base/dmeanpn
--- 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 2887967

File tree

10 files changed

+245
-68
lines changed

10 files changed

+245
-68
lines changed

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

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

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

5656
Computes the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array `x` using a two-pass error correction algorithm.
5757

@@ -69,9 +69,9 @@ The function has the following parameters:
6969

7070
- **N**: number of indexed elements.
7171
- **x**: input [`Float64Array`][@stdlib/array/float64].
72-
- **stride**: index increment for `x`.
72+
- **strideX**: index increment for `x`.
7373

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`,
74+
The `N` and `strideX` 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`,
7575

7676
```javascript
7777
var Float64Array = require( '@stdlib/array/float64' );
@@ -101,7 +101,7 @@ var v = dmeanpn( N, x1, 2 );
101101
// returns 1.25
102102
```
103103

104-
#### dmeanpn.ndarray( N, x, stride, offset )
104+
#### dmeanpn.ndarray( N, x, stridXe, offset )
105105

106106
Computes the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array using a two-pass error correction algorithm and alternative indexing semantics.
107107

@@ -175,6 +175,107 @@ console.log( v );
175175

176176
<!-- /.examples -->
177177

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

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

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ static double rand_double( 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
double x[ len ];
100100
double v;
@@ -107,6 +107,7 @@ static double benchmark( int iterations, int len ) {
107107
v = 0.0;
108108
t = tic();
109109
for ( i = 0; i < iterations; i++ ) {
110+
// cppcheck-suppress uninitvar
110111
v = stdlib_strided_dmeanpn( 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+
double x[ len ];
134+
double v;
135+
double t;
136+
int i;
137+
138+
for ( i = 0; i < len; i++ ) {
139+
x[ i ] = ( rand_double() * 20000.0 ) - 10000.0;
140+
}
141+
v = 0.0;
142+
t = tic();
143+
for ( i = 0; i < iterations; i++ ) {
144+
// cppcheck-suppress uninitvar
145+
v = stdlib_strided_dmeanpn_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/dmeanpn/include/stdlib/stats/base/dmeanpn.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#ifndef STDLIB_STATS_BASE_DMEANPN_H
2020
#define STDLIB_STATS_BASE_DMEANPN_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,13 @@ extern "C" {
3131
/**
3232
* Computes the arithmetic mean of a double-precision floating-point strided array using a two-pass error correction algorithm.
3333
*/
34-
double stdlib_strided_dmeanpn( const int64_t N, const double *X, const int64_t stride );
34+
double API_SUFFIX( stdlib_strided_dmeanpn )( const CBLAS_INT N, const double *X, const CBLAS_INT strideX );
35+
36+
/**
37+
* Computes the arithmetic mean of a double-precision floating-point strided array using a two-pass error correction algorithm.
38+
*/
39+
double API_SUFFIX( stdlib_strided_dmeanpn_ndarray )( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
40+
3541

3642
#ifdef __cplusplus
3743
}

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

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

2121
// MODULES //
2222

23-
var dsumpw = require( '@stdlib/blas/ext/base/dsumpw' );
24-
var dapxsumpw = require( '@stdlib/blas/ext/base/dapxsumpw' );
23+
var stride2offset = require( '@stdlib/strided/base/stride2offset' );
24+
var ndarray = require( './ndarray.js' );
2525

2626

2727
// MAIN //
@@ -40,7 +40,7 @@ var dapxsumpw = require( '@stdlib/blas/ext/base/dapxsumpw' );
4040
*
4141
* @param {PositiveInteger} N - number of indexed elements
4242
* @param {Float64Array} x - input array
43-
* @param {integer} stride - stride length
43+
* @param {integer} strideX - strideX length
4444
* @returns {number} arithmetic mean
4545
*
4646
* @example
@@ -52,23 +52,9 @@ var dapxsumpw = require( '@stdlib/blas/ext/base/dapxsumpw' );
5252
* var v = dmeanpn( N, x, 1 );
5353
* // returns ~0.3333
5454
*/
55-
function dmeanpn( N, x, stride ) {
56-
var mu;
57-
var c;
58-
59-
if ( N <= 0 ) {
60-
return NaN;
61-
}
62-
if ( N === 1 || stride === 0 ) {
63-
return x[ 0 ];
64-
}
65-
// Compute an estimate for the mean:
66-
mu = dsumpw( N, x, stride ) / N;
67-
68-
// Compute an error term:
69-
c = dapxsumpw( N, -mu, x, stride ) / N;
70-
71-
return mu + c;
55+
function dmeanpn( N, x, strideX ) {
56+
var ox = stride2offset( N, strideX );
57+
return ndarray( N, x, strideX, ox );
7258
}
7359

7460

lib/node_modules/@stdlib/stats/base/dmeanpn/lib/dmeanpn.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 {Float64Array} x - input array
33-
* @param {integer} stride - stride length
33+
* @param {integer} strideX - strideX 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 = dmeanpn( N, x, 1 );
43-
* // returns ~0.3333
43+
* //returns ~0.3333
4444
*/
45-
function dmeanpn( N, x, stride ) {
46-
return addon( N, x, stride );
45+
function dmeanpn( N, x, strideX ) {
46+
return addon( N, x, strideX );
4747
}
4848

4949

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ var dapxsumpw = require( '@stdlib/blas/ext/base/dapxsumpw' ).ndarray;
4040
*
4141
* @param {PositiveInteger} N - number of indexed elements
4242
* @param {Float64Array} x - input array
43-
* @param {integer} stride - stride length
43+
* @param {integer} strideX - strideX length
4444
* @param {NonNegativeInteger} offset - starting index
4545
* @returns {number} arithmetic mean
4646
*
@@ -54,21 +54,21 @@ var dapxsumpw = require( '@stdlib/blas/ext/base/dapxsumpw' ).ndarray;
5454
* var v = dmeanpn( N, x, 2, 1 );
5555
* // returns 1.25
5656
*/
57-
function dmeanpn( N, x, stride, offset ) {
57+
function dmeanpn( N, x, strideX, offset ) {
5858
var mu;
5959
var c;
6060

6161
if ( N <= 0 ) {
6262
return NaN;
6363
}
64-
if ( N === 1 || stride === 0 ) {
64+
if ( N === 1 || strideX === 0 ) {
6565
return x[ offset ];
6666
}
6767
// Compute an estimate for the mean:
68-
mu = dsumpw( N, x, stride, offset ) / N;
68+
mu = dsumpw( N, x, strideX, offset ) / N;
6969

7070
// Compute an error term:
71-
c = dapxsumpw( N, -mu, x, stride, offset ) / N;
71+
c = dapxsumpw( N, -mu, x, strideX, offset ) / N;
7272

7373
return mu + c;
7474
}

lib/node_modules/@stdlib/stats/base/dmeanpn/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 Float64Array = require( '@stdlib/array/float64' );
24-
var addon = require( './dmeanpn.native.js' );
23+
var addon = require( './../src/addon.node' );
2524

2625

2726
// MAIN //
@@ -31,8 +30,8 @@ var addon = require( './dmeanpn.native.js' );
3130
*
3231
* @param {PositiveInteger} N - number of indexed elements
3332
* @param {Float64Array} 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( './dmeanpn.native.js' );
4342
* var N = floor( x.length / 2 );
4443
*
4544
* var v = dmeanpn( N, x, 2, 1 );
46-
* // returns 1.25
45+
* //returns 1.25
4746
*/
48-
function dmeanpn( N, x, stride, offset ) {
49-
var view;
50-
if ( stride < 0 ) {
51-
offset += (N-1) * stride;
52-
}
53-
view = new Float64Array( 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 dmeanpn( N, x, strideX, offsetX ) {
48+
return addon.ndarray( N, x, strideX, offsetX );
5549
}
5650

5751

0 commit comments

Comments
 (0)