Skip to content

Commit d5fe50e

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

File tree

8 files changed

+147
-113
lines changed

8 files changed

+147
-113
lines changed

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

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,12 @@ 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 out[ 2 ];
100+
if(len <= 0){
101+
return 0.0;
102+
}
100103
double x[ len ];
101104
double t;
102105
int i;
@@ -122,6 +125,44 @@ static double benchmark( int iterations, int len ) {
122125
return elapsed;
123126
}
124127

128+
/**
129+
* Runs a benchmark.
130+
*
131+
* @param iterations number of iterations
132+
* @param len array length
133+
* @return elapsed time in seconds
134+
*/
135+
static double benchmark2( int iterations, int len ) {
136+
double elapsed;
137+
double out[ 2 ];
138+
if(len <= 0){
139+
return 0.0;
140+
}
141+
double x[ len ];
142+
double t;
143+
int i;
144+
145+
for ( i = 0; i < len; i++ ) {
146+
x[ i ] = ( rand_double() * 20000.0 ) - 10000.0;
147+
}
148+
out[ 0 ] = 0.0;
149+
out[ 1 ] = 0.0;
150+
151+
t = tic();
152+
for ( i = 0; i < iterations; i++ ) {
153+
stdlib_strided_dmeanvarpn_ndarray( len, 1, x, 1, 0, out, 1, 0 );
154+
if ( out[ i%2 ] != out[ i%2 ] ) {
155+
printf( "should not return NaN\n" );
156+
break;
157+
}
158+
}
159+
elapsed = tic() - t;
160+
if ( out[ i%2 ] != out[ i%2 ] ) {
161+
printf( "should not return NaN\n" );
162+
}
163+
return elapsed;
164+
}
165+
125166
/**
126167
* Main execution sequence.
127168
*/
@@ -144,7 +185,18 @@ int main( void ) {
144185
for ( j = 0; j < REPEATS; j++ ) {
145186
count += 1;
146187
printf( "# c::%s:len=%d\n", NAME, len );
147-
elapsed = benchmark( iter, len );
188+
elapsed = benchmark1( iter, len );
189+
print_results( iter, elapsed );
190+
printf( "ok %d benchmark finished\n", count );
191+
}
192+
}
193+
for ( i = MIN; i <= MAX; i++ ) {
194+
len = pow( 10, i );
195+
iter = ITERATIONS / pow( 10, i-1 );
196+
for ( j = 0; j < REPEATS; j++ ) {
197+
count += 1;
198+
printf( "# c::%s:ndarray:len=%d\n", NAME, len );
199+
elapsed = benchmark2( iter, len );
148200
print_results( iter, elapsed );
149201
printf( "ok %d benchmark finished\n", count );
150202
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#ifndef STDLIB_STATS_BASE_DMEANVARPN_H
2020
#define STDLIB_STATS_BASE_DMEANVARPN_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,11 @@ extern "C" {
3131
/**
3232
* Computes the mean and variance of a double-precision floating-point strided array using a two-pass algorithm.
3333
*/
34-
void stdlib_strided_dmeanvarpn( const int64_t N, const double correction, const double *X, const int64_t strideX, double *Out, const int64_t strideOut );
34+
void API_SUFFIX( stdlib_strided_dmeanvarpn )( const CBLAS_INT N, const double correction, const double *X, const CBLAS_INT strideX, double *Out, const CBLAS_INT strideOut );
35+
/**
36+
* Computes the mean and variance of a double-precision floating-point strided array using a two-pass algorithm.
37+
*/
38+
void API_SUFFIX( stdlib_strided_dmeanvarpn_ndarray )( const CBLAS_INT N, const double correction, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, double *Out, const CBLAS_INT strideOut, const CBLAS_INT offsetOut );
3539

3640
#ifdef __cplusplus
3741
}

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

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

2121
// MODULES //
2222

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

2626

2727
// MAIN //
@@ -56,66 +56,9 @@ var dsumpw = require( '@stdlib/blas/ext/base/dsumpw' );
5656
* // returns <Float64Array>[ ~0.3333, ~4.3333 ]
5757
*/
5858
function dmeanvarpn( N, correction, x, strideX, out, strideOut ) {
59-
var mu;
60-
var ix;
61-
var io;
62-
var M2;
63-
var M;
64-
var d;
65-
var c;
66-
var n;
67-
var i;
68-
69-
if ( strideX < 0 ) {
70-
ix = (1-N) * strideX;
71-
} else {
72-
ix = 0;
73-
}
74-
if ( strideOut < 0 ) {
75-
io = -strideOut;
76-
} else {
77-
io = 0;
78-
}
79-
if ( N <= 0 ) {
80-
out[ io ] = NaN;
81-
out[ io+strideOut ] = NaN;
82-
return out;
83-
}
84-
n = N - correction;
85-
if ( N === 1 || strideX === 0 ) {
86-
out[ io ] = x[ ix ];
87-
if ( n <= 0.0 ) {
88-
out[ io+strideOut ] = NaN;
89-
} else {
90-
out[ io+strideOut ] = 0.0;
91-
}
92-
return out;
93-
}
94-
// Compute an estimate for the mean:
95-
mu = dsumpw( N, x, strideX ) / N;
96-
if ( isnan( mu ) ) {
97-
out[ io ] = NaN;
98-
out[ io+strideOut ] = NaN;
99-
return out;
100-
}
101-
// Compute the sum of squared differences from the mean...
102-
M2 = 0.0;
103-
M = 0.0;
104-
for ( i = 0; i < N; i++ ) {
105-
d = x[ ix ] - mu;
106-
M2 += d * d;
107-
M += d;
108-
ix += strideX;
109-
}
110-
// Compute an error term for the mean:
111-
c = M / N;
112-
113-
out[ io ] = mu + c;
114-
if ( n <= 0.0 ) {
115-
out[ io+strideOut ] = NaN;
116-
} else {
117-
out[ io+strideOut ] = (M2/n) - (c*(M/n));
118-
}
59+
var offsetOut = stride2offset( 2, strideX );
60+
var offsetX = stride2offset( N, strideX );
61+
ndarray( N, correction, x, strideX, offsetX, out, strideOut, offsetOut );
11962
return out;
12063
}
12164

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ var addon = require( './../src/addon.node' );
4343
* var out = new Float64Array( 2 );
4444
*
4545
* var v = dmeanvarpn( x.length, 1, x, 1, out, 1 );
46-
* // returns <Float64Array>[ ~0.3333, ~4.3333 ]
46+
* //returns <Float64Array>[ ~0.3333, ~4.3333 ]
4747
*/
4848
function dmeanvarpn( N, correction, x, strideX, out, strideOut ) {
4949
addon( N, correction, x, strideX, out, strideOut );

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

Lines changed: 3 additions & 14 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( './dmeanvarpn.native.js' );
23+
var addon = require( './../src/addon.node' );
2524

2625

2726
// MAIN //
@@ -49,20 +48,10 @@ var addon = require( './dmeanvarpn.native.js' );
4948
* var N = floor( x.length / 2 );
5049
*
5150
* var v = dmeanvarpn( N, 1, x, 2, 1, out, 1, 0 );
52-
* // returns <Float64Array>[ 1.25, 6.25 ]
51+
* //returns <Float64Array>[ 1.25, 6.25 ]
5352
*/
5453
function dmeanvarpn( N, correction, x, strideX, offsetX, out, strideOut, offsetOut ) { // eslint-disable-line max-len
55-
var viewOut;
56-
var viewX;
57-
if ( strideX < 0 ) {
58-
offsetX += (N-1) * strideX;
59-
}
60-
if ( strideOut < 0 ) {
61-
offsetOut += strideOut;
62-
}
63-
viewX = new Float64Array( x.buffer, x.byteOffset+(x.BYTES_PER_ELEMENT*offsetX), x.length-offsetX ); // eslint-disable-line max-len
64-
viewOut = new Float64Array( out.buffer, out.byteOffset+(out.BYTES_PER_ELEMENT*offsetOut), out.length-offsetOut ); // eslint-disable-line max-len
65-
addon( N, correction, viewX, strideX, viewOut, strideOut );
54+
addon.ndarray( N, correction, x, strideX, offsetX, out, strideOut, offsetOut );
6655
return out;
6756
}
6857

lib/node_modules/@stdlib/stats/base/dmeanvarpn/manifest.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
"@stdlib/napi/argv-double",
4545
"@stdlib/napi/argv-strided-float64array",
4646
"@stdlib/blas/base/shared",
47+
"@stdlib/math/base/assert/is-nan",
48+
"@stdlib/strided/base/stride2offset",
4749
"@stdlib/blas/ext/base/dsumpw"
4850
]
4951
},
@@ -60,6 +62,7 @@
6062
],
6163
"libpath": [],
6264
"dependencies": [
65+
"@stdlib/math/base/assert/is-nan",
6366
"@stdlib/blas/ext/base/dsumpw"
6467
]
6568
},
@@ -76,6 +79,7 @@
7679
],
7780
"libpath": [],
7881
"dependencies": [
82+
"@stdlib/math/base/assert/is-nan",
7983
"@stdlib/blas/ext/base/dsumpw"
8084
]
8185
}

lib/node_modules/@stdlib/stats/base/dmeanvarpn/src/addon.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,25 @@ static napi_value addon( napi_env env, napi_callback_info info ) {
4343
API_SUFFIX(stdlib_strided_dmeanvarpn)( N, correction, X, strideX, Out, strideOut );
4444
return NULL;
4545
}
46+
/**
47+
* Receives JavaScript callback invocation data.
48+
*
49+
* @param env environment under which the function is invoked
50+
* @param info callback data
51+
* @return Node-API value
52+
*/
53+
static napi_value addon_method( napi_env env, napi_callback_info info ) {
54+
STDLIB_NAPI_ARGV( env, info, argv, argc, 8 );
55+
STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 );
56+
STDLIB_NAPI_ARGV_INT64( env, strideX, argv, 3 );
57+
STDLIB_NAPI_ARGV_INT64( env, offsetX, argv, 4 );
58+
STDLIB_NAPI_ARGV_INT64( env, offsetOut, argv, 7 );
59+
STDLIB_NAPI_ARGV_INT64( env, strideOut, argv, 6 );
60+
STDLIB_NAPI_ARGV_DOUBLE( env, correction, argv, 1 );
61+
STDLIB_NAPI_ARGV_STRIDED_FLOAT64ARRAY( env, X, N, strideX, argv, 2 );
62+
STDLIB_NAPI_ARGV_STRIDED_FLOAT64ARRAY( env, Out, 2, strideOut, argv, 5 );
63+
API_SUFFIX( stdlib_strided_dmeanvarpn_ndarray )( N, correction, X, strideX, offsetX, Out, strideOut, offsetOut );
64+
return NULL;
65+
}
4666

47-
STDLIB_NAPI_MODULE_EXPORT_FCN( addon )
67+
STDLIB_NAPI_MODULE_EXPORT_FCN_WITH_METHOD( addon, "ndarray", addon_method )

0 commit comments

Comments
 (0)