Skip to content

Commit 87a38be

Browse files
feat: added ndarray implementation for @stats/base/dvarmpn
--- 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: na - 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 ece93c1 commit 87a38be

File tree

8 files changed

+120
-80
lines changed

8 files changed

+120
-80
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#ifndef STDLIB_STATS_BASE_DVARMPN_H
2020
#define STDLIB_STATS_BASE_DVARMPN_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 variance of a double-precision floating-point strided array provided a known mean and using Neely's correction algorithm.
3333
*/
34-
double stdlib_strided_dvarmpn( const int64_t N, const double mean, const double correction, const double *X, const int64_t stride );
34+
double API_SUFFIX( stdlib_strided_dvarmpn )( const CBLAS_INT N, const double mean, const double correction, const double *X, const CBLAS_INT strideX );
35+
/**
36+
* Computes the variance of a double-precision floating-point strided array provided a known mean and using Neely's correction algorithm using alternative indexing semantics.
37+
*/
38+
double API_SUFFIX( stdlib_strided_dvarmpn_ndarray )( const CBLAS_INT N, const double mean, const double correction, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
3539

3640
#ifdef __cplusplus
3741
}

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

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818

1919
'use strict';
2020

21+
// MODULES //
22+
23+
var stride2offset = require( '@stdlib/strided/base/stride2offset' );
24+
var ndarray = require( './ndarray.js' );
25+
26+
2127
// MAIN //
2228

2329
/**
@@ -32,7 +38,7 @@
3238
* @param {number} mean - mean
3339
* @param {number} correction - degrees of freedom adjustment
3440
* @param {Float64Array} x - input array
35-
* @param {integer} stride - stride length
41+
* @param {integer} strideX - strideX length
3642
* @returns {number} variance
3743
*
3844
* @example
@@ -43,35 +49,9 @@
4349
* var v = dvarmpn( x.length, 1.0/3.0, 1, x, 1 );
4450
* // returns ~4.3333
4551
*/
46-
function dvarmpn( N, mean, correction, x, stride ) {
47-
var ix;
48-
var M2;
49-
var M;
50-
var d;
51-
var n;
52-
var i;
53-
54-
n = N - correction;
55-
if ( N <= 0 || n <= 0.0 ) {
56-
return NaN;
57-
}
58-
if ( N === 1 || stride === 0 ) {
59-
return 0.0;
60-
}
61-
if ( stride < 0 ) {
62-
ix = (1-N) * stride;
63-
} else {
64-
ix = 0;
65-
}
66-
M2 = 0.0;
67-
M = 0.0;
68-
for ( i = 0; i < N; i++ ) {
69-
d = x[ ix ] - mean;
70-
M2 += d * d;
71-
M += d;
72-
ix += stride;
73-
}
74-
return (M2/n) - ((M/N)*(M/n));
52+
function dvarmpn( N, mean, correction, x, strideX ) {
53+
var ox = stride2offset( N, strideX);
54+
return ndarray( N, mean, correction, x, strideX, ox );
7555
}
7656

7757

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ var addon = require( './../src/addon.node' );
3232
* @param {number} mean - mean
3333
* @param {number} correction - degrees of freedom adjustment
3434
* @param {Float64Array} x - input array
35-
* @param {integer} stride - stride length
35+
* @param {integer} strideX - strideX length
3636
* @returns {number} variance
3737
*
3838
* @example
@@ -43,8 +43,8 @@ var addon = require( './../src/addon.node' );
4343
* var v = dvarmpn( x.length, 1.0/3.0, 1, x, 1 );
4444
* // returns ~4.3333
4545
*/
46-
function dvarmpn( N, mean, correction, x, stride ) {
47-
return addon( N, mean, correction, x, stride );
46+
function dvarmpn( N, mean, correction, x, strideX ) {
47+
return addon( N, mean, correction, x, strideX );
4848
}
4949

5050

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,19 @@
3232
* @param {number} mean - mean
3333
* @param {number} correction - degrees of freedom adjustment
3434
* @param {Float64Array} x - input array
35-
* @param {integer} stride - stride length
36-
* @param {NonNegativeInteger} offset - starting index
35+
* @param {integer} strideX - strideX length
36+
* @param {NonNegativeInteger} offsetX - starting index
3737
* @returns {number} variance
3838
*
3939
* @example
4040
* var Float64Array = require( '@stdlib/array/float64' );
41-
* var floor = require( '@stdlib/math/base/special/floor' );
4241
*
4342
* var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
44-
* var N = floor( x.length / 2 );
4543
*
46-
* var v = dvarmpn( N, 1.25, 1, x, 2, 1 );
44+
* var v = dvarmpn( 4, 1.25, 1, x, 2, 1 );
4745
* // returns 6.25
4846
*/
49-
function dvarmpn( N, mean, correction, x, stride, offset ) {
47+
function dvarmpn( N, mean, correction, x, strideX, offsetX ) {
5048
var ix;
5149
var M2;
5250
var M;
@@ -58,17 +56,17 @@ function dvarmpn( N, mean, correction, x, stride, offset ) {
5856
if ( N <= 0 || n <= 0.0 ) {
5957
return NaN;
6058
}
61-
if ( N === 1 || stride === 0 ) {
59+
if ( N === 1 || strideX === 0 ) {
6260
return 0.0;
6361
}
64-
ix = offset;
62+
ix = offsetX;
6563
M2 = 0.0;
6664
M = 0.0;
6765
for ( i = 0; i < N; i++ ) {
6866
d = x[ ix ] - mean;
6967
M2 += d * d;
7068
M += d;
71-
ix += stride;
69+
ix += strideX;
7270
}
7371
return (M2/n) - ((M/N)*(M/n));
7472
}

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

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

2625

2726
// MAIN //
@@ -33,27 +32,20 @@ var addon = require( './dvarmpn.native.js' );
3332
* @param {number} mean - mean
3433
* @param {number} correction - degrees of freedom adjustment
3534
* @param {Float64Array} x - input array
36-
* @param {integer} stride - stride length
37-
* @param {NonNegativeInteger} offset - starting index
35+
* @param {integer} strideX - strideX length
36+
* @param {NonNegativeInteger} offsetX - starting index
3837
* @returns {number} variance
3938
*
4039
* @example
4140
* var Float64Array = require( '@stdlib/array/float64' );
42-
* var floor = require( '@stdlib/math/base/special/floor' );
4341
*
4442
* var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
45-
* var N = floor( x.length / 2 );
4643
*
47-
* var v = dvarmpn( N, 1.25, 1, x, 2, 1 );
44+
* var v = dvarmpn( 4, 1.25, 1, x, 2, 1 );
4845
* // returns 6.25
4946
*/
50-
function dvarmpn( N, mean, correction, x, stride, offset ) {
51-
var view;
52-
if ( stride < 0 ) {
53-
offset += (N-1) * stride;
54-
}
55-
view = new Float64Array( x.buffer, x.byteOffset+(x.BYTES_PER_ELEMENT*offset), x.length-offset ); // eslint-disable-line max-len
56-
return addon( N, mean, correction, view, stride );
47+
function dvarmpn( N, mean, correction, x, strideX, offsetX ) {
48+
return addon.ndarray( N, mean, correction, x, strideX, offsetX );
5749
}
5850

5951

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

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"options": {
3-
"task": "build"
3+
"task": "build",
4+
"wasm": false
45
},
56
"fields": [
67
{
@@ -27,8 +28,9 @@
2728
"confs": [
2829
{
2930
"task": "build",
31+
"wasm": false,
3032
"src": [
31-
"./src/dvarmpn.c"
33+
"./src/main.c"
3234
],
3335
"include": [
3436
"./include"
@@ -38,6 +40,8 @@
3840
],
3941
"libpath": [],
4042
"dependencies": [
43+
"@stdlib/blas/base/shared",
44+
"@stdlib/strided/base/stride2offset",
4145
"@stdlib/napi/export",
4246
"@stdlib/napi/argv",
4347
"@stdlib/napi/argv-int64",
@@ -48,8 +52,9 @@
4852
},
4953
{
5054
"task": "benchmark",
55+
"wasm": false,
5156
"src": [
52-
"./src/dvarmpn.c"
57+
"./src/main.c"
5358
],
5459
"include": [
5560
"./include"
@@ -58,12 +63,16 @@
5863
"-lm"
5964
],
6065
"libpath": [],
61-
"dependencies": []
66+
"dependencies": [
67+
"@stdlib/blas/base/shared",
68+
"@stdlib/strided/base/stride2offset"
69+
]
6270
},
6371
{
6472
"task": "examples",
73+
"wasm": false,
6574
"src": [
66-
"./src/dvarmpn.c"
75+
"./src/main.c"
6776
],
6877
"include": [
6978
"./include"
@@ -72,7 +81,26 @@
7281
"-lm"
7382
],
7483
"libpath": [],
75-
"dependencies": []
84+
"dependencies": [
85+
"@stdlib/blas/base/shared",
86+
"@stdlib/strided/base/stride2offset"
87+
]
88+
},
89+
{
90+
"task": "",
91+
"wasm": true,
92+
"src": [
93+
"./src/main.c"
94+
],
95+
"include": [
96+
"./include"
97+
],
98+
"libraries": [],
99+
"libpath": [],
100+
"dependencies": [
101+
"@stdlib/blas/base/shared",
102+
"@stdlib/strided/base/stride2offset"
103+
]
76104
}
77105
]
78106
}

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,31 @@
3535
static napi_value addon( napi_env env, napi_callback_info info ) {
3636
STDLIB_NAPI_ARGV( env, info, argv, argc, 5 );
3737
STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 );
38-
STDLIB_NAPI_ARGV_INT64( env, stride, argv, 4 );
38+
STDLIB_NAPI_ARGV_INT64( env, strideX, argv, 4 );
3939
STDLIB_NAPI_ARGV_DOUBLE( env, mean, argv, 1 );
4040
STDLIB_NAPI_ARGV_DOUBLE( env, correction, argv, 2 );
41-
STDLIB_NAPI_ARGV_STRIDED_FLOAT64ARRAY( env, X, N, stride, argv, 3 );
42-
STDLIB_NAPI_CREATE_DOUBLE( env, stdlib_strided_dvarmpn( N, mean, correction, X, stride ), v );
41+
STDLIB_NAPI_ARGV_STRIDED_FLOAT64ARRAY( env, X, N, strideX, argv, 3 );
42+
STDLIB_NAPI_CREATE_DOUBLE( env, stdlib_strided_dvarmpn( N, mean, correction, X, strideX ), v );
4343
return v;
4444
}
4545

46-
STDLIB_NAPI_MODULE_EXPORT_FCN( addon )
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, 6 );
55+
STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 );
56+
STDLIB_NAPI_ARGV_INT64( env, strideX, argv, 4 );
57+
STDLIB_NAPI_ARGV_INT64( env, offsetX, argv, 5 );
58+
STDLIB_NAPI_ARGV_DOUBLE( env, mean, argv, 1 );
59+
STDLIB_NAPI_ARGV_DOUBLE( env, correction, argv, 2 );
60+
STDLIB_NAPI_ARGV_STRIDED_FLOAT64ARRAY( env, X, N, strideX, argv, 3 );
61+
STDLIB_NAPI_CREATE_DOUBLE( env, stdlib_strided_dvarmpn_ndarray( N, mean, correction, X, strideX, offsetX ), v );
62+
return v;
63+
}
64+
65+
STDLIB_NAPI_MODULE_EXPORT_FCN_WITH_METHOD( addon, "ndarray", addon_method )

lib/node_modules/@stdlib/stats/base/dvarmpn/src/dvarmpn.c renamed to lib/node_modules/@stdlib/stats/base/dvarmpn/src/main.c

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
*/
1818

1919
#include "stdlib/stats/base/dvarmpn.h"
20-
#include <stdint.h>
20+
#include "stdlib/strided/base/stride2offset.h"
21+
#include "stdlib/blas/base/shared.h"
2122

2223
/**
2324
* Computes the variance of a double-precision floating-point strided array provided a known mean and using Neely's correction algorithm.
@@ -31,12 +32,34 @@
3132
* @param mean mean
3233
* @param correction degrees of freedom adjustment
3334
* @param X input array
34-
* @param stride stride length
35+
* @param strideX strideX length
3536
* @return output value
3637
*/
37-
double stdlib_strided_dvarmpn( const int64_t N, const double mean, const double correction, const double *X, const int64_t stride ) {
38-
int64_t ix;
39-
int64_t i;
38+
double API_SUFFIX( stdlib_strided_dvarmpn )( const CBLAS_INT N, const double mean, const double correction, const double *X, const CBLAS_INT strideX ) {
39+
const CBLAS_INT ox = stdlib_strided_stride2offset( N, strideX );
40+
return API_SUFFIX(stdlib_strided_dvarmpn_ndarray)( N, mean, correction, X, strideX, ox );
41+
}
42+
43+
/**
44+
* Computes the variance of a double-precision floating-point strided array provided a known mean and using Neely's correction algorithm.
45+
*
46+
* ## References
47+
*
48+
* - Neely, Peter M. 1966. "Comparison of Several Algorithms for Computation of Means, Standard Deviations and Correlation Coefficients." _Communications of the ACM_ 9 (7). Association for Computing Machinery: 496–99. doi:[10.1145/365719.365958](https://doi.org/10.1145/365719.365958).
49+
* - Schubert, Erich, and Michael Gertz. 2018. "Numerically Stable Parallel Computation of (Co-)Variance." In _Proceedings of the 30th International Conference on Scientific and Statistical Database Management_. New York, NY, USA: Association for Computing Machinery. doi:[10.1145/3221269.3223036](https://doi.org/10.1145/3221269.3223036).
50+
*
51+
* @param N number of indexed elements
52+
* @param mean mean
53+
* @param correction degrees of freedom adjustment
54+
* @param X input array
55+
* @param strideX strideX length
56+
* @param offsetX starting index
57+
* @return output value
58+
*/
59+
60+
double API_SUFFIX( stdlib_strided_dvarmpn_ndarray )( const CBLAS_INT N, const double mean, const double correction, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ) {
61+
CBLAS_INT ix;
62+
CBLAS_INT i;
4063
double dN;
4164
double M2;
4265
double M;
@@ -48,21 +71,17 @@ double stdlib_strided_dvarmpn( const int64_t N, const double mean, const double
4871
if ( N <= 0 || n <= 0.0 ) {
4972
return 0.0 / 0.0; // NaN
5073
}
51-
if ( N == 1 || stride == 0 ) {
74+
if ( N == 1 || strideX == 0 ) {
5275
return 0.0;
5376
}
54-
if ( stride < 0 ) {
55-
ix = (1-N) * stride;
56-
} else {
57-
ix = 0;
58-
}
77+
ix = offsetX;
5978
M2 = 0.0;
6079
M = 0.0;
6180
for ( i = 0; i < N; i++ ) {
6281
d = X[ ix ] - mean;
6382
M2 += d * d;
6483
M += d;
65-
ix += stride;
84+
ix += strideX;
6685
}
6786
return (M2/n) - ((M/dN)*(M/n));
6887
}

0 commit comments

Comments
 (0)