@stdlib/stats-strided-snanmeanwd 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +177 -0
- package/NOTICE +1 -0
- package/README.md +437 -0
- package/SECURITY.md +5 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +7 -0
- package/docs/img/equation_arithmetic_mean.svg +42 -0
- package/docs/types/index.d.ts +92 -0
- package/include/stdlib/stats/strided/snanmeanwd.h +45 -0
- package/lib/index.js +68 -0
- package/lib/main.js +35 -0
- package/lib/native.js +35 -0
- package/lib/ndarray.js +96 -0
- package/lib/ndarray.native.js +52 -0
- package/lib/snanmeanwd.js +70 -0
- package/lib/snanmeanwd.native.js +51 -0
- package/manifest.json +99 -0
- package/package.json +92 -0
- package/src/addon.c +61 -0
- package/src/main.c +91 -0
package/manifest.json
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
{
|
|
2
|
+
"options": {
|
|
3
|
+
"task": "build",
|
|
4
|
+
"wasm": false
|
|
5
|
+
},
|
|
6
|
+
"fields": [
|
|
7
|
+
{
|
|
8
|
+
"field": "src",
|
|
9
|
+
"resolve": true,
|
|
10
|
+
"relative": true
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
"field": "include",
|
|
14
|
+
"resolve": true,
|
|
15
|
+
"relative": true
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
"field": "libraries",
|
|
19
|
+
"resolve": false,
|
|
20
|
+
"relative": false
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
"field": "libpath",
|
|
24
|
+
"resolve": true,
|
|
25
|
+
"relative": false
|
|
26
|
+
}
|
|
27
|
+
],
|
|
28
|
+
"confs": [
|
|
29
|
+
{
|
|
30
|
+
"task": "build",
|
|
31
|
+
"wasm": false,
|
|
32
|
+
"src": [
|
|
33
|
+
"./src/main.c"
|
|
34
|
+
],
|
|
35
|
+
"include": [
|
|
36
|
+
"./include"
|
|
37
|
+
],
|
|
38
|
+
"libraries": [],
|
|
39
|
+
"libpath": [],
|
|
40
|
+
"dependencies": [
|
|
41
|
+
"@stdlib/blas-base-shared",
|
|
42
|
+
"@stdlib/strided-base-stride2offset",
|
|
43
|
+
"@stdlib/napi-export",
|
|
44
|
+
"@stdlib/napi-argv",
|
|
45
|
+
"@stdlib/napi-argv-int64",
|
|
46
|
+
"@stdlib/napi-argv-strided-float32array",
|
|
47
|
+
"@stdlib/napi-create-double"
|
|
48
|
+
]
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
"task": "benchmark",
|
|
52
|
+
"wasm": false,
|
|
53
|
+
"src": [
|
|
54
|
+
"./src/main.c"
|
|
55
|
+
],
|
|
56
|
+
"include": [
|
|
57
|
+
"./include"
|
|
58
|
+
],
|
|
59
|
+
"libraries": [],
|
|
60
|
+
"libpath": [],
|
|
61
|
+
"dependencies": [
|
|
62
|
+
"@stdlib/blas-base-shared",
|
|
63
|
+
"@stdlib/strided-base-stride2offset"
|
|
64
|
+
]
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
"task": "examples",
|
|
68
|
+
"wasm": false,
|
|
69
|
+
"src": [
|
|
70
|
+
"./src/main.c"
|
|
71
|
+
],
|
|
72
|
+
"include": [
|
|
73
|
+
"./include"
|
|
74
|
+
],
|
|
75
|
+
"libraries": [],
|
|
76
|
+
"libpath": [],
|
|
77
|
+
"dependencies": [
|
|
78
|
+
"@stdlib/blas-base-shared",
|
|
79
|
+
"@stdlib/strided-base-stride2offset"
|
|
80
|
+
]
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
"task": "build",
|
|
84
|
+
"wasm": true,
|
|
85
|
+
"src": [
|
|
86
|
+
"./src/main.c"
|
|
87
|
+
],
|
|
88
|
+
"include": [
|
|
89
|
+
"./include"
|
|
90
|
+
],
|
|
91
|
+
"libraries": [],
|
|
92
|
+
"libpath": [],
|
|
93
|
+
"dependencies": [
|
|
94
|
+
"@stdlib/blas-base-shared",
|
|
95
|
+
"@stdlib/strided-base-stride2offset"
|
|
96
|
+
]
|
|
97
|
+
}
|
|
98
|
+
]
|
|
99
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stdlib/stats-strided-snanmeanwd",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Calculate the arithmetic mean of a single-precision floating-point strided array, ignoring NaN values and using Welford's algorithm.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "The Stdlib Authors",
|
|
8
|
+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
|
|
9
|
+
},
|
|
10
|
+
"contributors": [
|
|
11
|
+
{
|
|
12
|
+
"name": "The Stdlib Authors",
|
|
13
|
+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
|
|
14
|
+
}
|
|
15
|
+
],
|
|
16
|
+
"main": "./lib",
|
|
17
|
+
"browser": "./lib/main.js",
|
|
18
|
+
"gypfile": false,
|
|
19
|
+
"directories": {
|
|
20
|
+
"doc": "./docs",
|
|
21
|
+
"include": "./include",
|
|
22
|
+
"lib": "./lib",
|
|
23
|
+
"src": "./src",
|
|
24
|
+
"dist": "./dist"
|
|
25
|
+
},
|
|
26
|
+
"types": "./docs/types",
|
|
27
|
+
"scripts": {},
|
|
28
|
+
"homepage": "https://stdlib.io",
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "git://github.com/stdlib-js/stats-strided-snanmeanwd.git"
|
|
32
|
+
},
|
|
33
|
+
"bugs": {
|
|
34
|
+
"url": "https://github.com/stdlib-js/stdlib/issues"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@stdlib/assert-is-error": "^0.2.2",
|
|
38
|
+
"@stdlib/blas-base-shared": "^0.2.0",
|
|
39
|
+
"@stdlib/napi-argv": "^0.2.2",
|
|
40
|
+
"@stdlib/napi-argv-int64": "^0.2.2",
|
|
41
|
+
"@stdlib/napi-argv-strided-float32array": "^0.2.2",
|
|
42
|
+
"@stdlib/napi-create-double": "^0.0.2",
|
|
43
|
+
"@stdlib/napi-export": "^0.3.0",
|
|
44
|
+
"@stdlib/number-float64-base-to-float32": "^0.2.2",
|
|
45
|
+
"@stdlib/strided-base-stride2offset": "^0.1.0",
|
|
46
|
+
"@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2",
|
|
47
|
+
"@stdlib/utils-library-manifest": "^0.2.3",
|
|
48
|
+
"@stdlib/utils-try-require": "^0.2.2"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {},
|
|
51
|
+
"engines": {
|
|
52
|
+
"node": ">=0.10.0",
|
|
53
|
+
"npm": ">2.7.0"
|
|
54
|
+
},
|
|
55
|
+
"os": [
|
|
56
|
+
"aix",
|
|
57
|
+
"darwin",
|
|
58
|
+
"freebsd",
|
|
59
|
+
"linux",
|
|
60
|
+
"macos",
|
|
61
|
+
"openbsd",
|
|
62
|
+
"sunos",
|
|
63
|
+
"win32",
|
|
64
|
+
"windows"
|
|
65
|
+
],
|
|
66
|
+
"keywords": [
|
|
67
|
+
"stdlib",
|
|
68
|
+
"stdmath",
|
|
69
|
+
"statistics",
|
|
70
|
+
"stats",
|
|
71
|
+
"mathematics",
|
|
72
|
+
"math",
|
|
73
|
+
"average",
|
|
74
|
+
"avg",
|
|
75
|
+
"mean",
|
|
76
|
+
"arithmetic mean",
|
|
77
|
+
"central tendency",
|
|
78
|
+
"welford",
|
|
79
|
+
"strided",
|
|
80
|
+
"strided array",
|
|
81
|
+
"typed",
|
|
82
|
+
"array",
|
|
83
|
+
"float32",
|
|
84
|
+
"single",
|
|
85
|
+
"float",
|
|
86
|
+
"float32array"
|
|
87
|
+
],
|
|
88
|
+
"funding": {
|
|
89
|
+
"type": "opencollective",
|
|
90
|
+
"url": "https://opencollective.com/stdlib"
|
|
91
|
+
}
|
|
92
|
+
}
|
package/src/addon.c
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
#include "stdlib/stats/strided/snanmeanwd.h"
|
|
20
|
+
#include "stdlib/blas/base/shared.h"
|
|
21
|
+
#include "stdlib/napi/export.h"
|
|
22
|
+
#include "stdlib/napi/argv.h"
|
|
23
|
+
#include "stdlib/napi/argv_int64.h"
|
|
24
|
+
#include "stdlib/napi/argv_strided_float32array.h"
|
|
25
|
+
#include "stdlib/napi/create_double.h"
|
|
26
|
+
#include <node_api.h>
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Receives JavaScript callback invocation data.
|
|
30
|
+
*
|
|
31
|
+
* @param env environment under which the function is invoked
|
|
32
|
+
* @param info callback data
|
|
33
|
+
* @return Node-API value
|
|
34
|
+
*/
|
|
35
|
+
static napi_value addon( napi_env env, napi_callback_info info ) {
|
|
36
|
+
STDLIB_NAPI_ARGV( env, info, argv, argc, 3 );
|
|
37
|
+
STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 );
|
|
38
|
+
STDLIB_NAPI_ARGV_INT64( env, strideX, argv, 2 );
|
|
39
|
+
STDLIB_NAPI_ARGV_STRIDED_FLOAT32ARRAY( env, X, N, strideX, argv, 1 );
|
|
40
|
+
STDLIB_NAPI_CREATE_DOUBLE( env, (double)API_SUFFIX(stdlib_strided_snanmeanwd)( N, X, strideX ), v );
|
|
41
|
+
return v;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Receives JavaScript callback invocation data.
|
|
46
|
+
*
|
|
47
|
+
* @param env environment under which the function is invoked
|
|
48
|
+
* @param info callback data
|
|
49
|
+
* @return Node-API value
|
|
50
|
+
*/
|
|
51
|
+
static napi_value addon_method( napi_env env, napi_callback_info info ) {
|
|
52
|
+
STDLIB_NAPI_ARGV( env, info, argv, argc, 4 );
|
|
53
|
+
STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 );
|
|
54
|
+
STDLIB_NAPI_ARGV_INT64( env, strideX, argv, 2 );
|
|
55
|
+
STDLIB_NAPI_ARGV_INT64( env, offsetX, argv, 3 );
|
|
56
|
+
STDLIB_NAPI_ARGV_STRIDED_FLOAT32ARRAY( env, X, N, strideX, argv, 1 );
|
|
57
|
+
STDLIB_NAPI_CREATE_DOUBLE( env, (double)API_SUFFIX(stdlib_strided_snanmeanwd_ndarray)( N, X, strideX, offsetX ), v );
|
|
58
|
+
return v;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
STDLIB_NAPI_MODULE_EXPORT_FCN_WITH_METHOD( addon, "ndarray", addon_method )
|
package/src/main.c
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2020 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
#include "stdlib/stats/strided/snanmeanwd.h"
|
|
20
|
+
#include "stdlib/blas/base/shared.h"
|
|
21
|
+
#include "stdlib/strided/base/stride2offset.h"
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Computes the arithmetic mean of a single-precision floating-point strided array, ignoring `NaN` values and using Welford's algorithm.
|
|
25
|
+
*
|
|
26
|
+
* ## Method
|
|
27
|
+
*
|
|
28
|
+
* - This implementation uses Welford's algorithm for efficient computation, which can be derived as follows
|
|
29
|
+
*
|
|
30
|
+
* ```tex
|
|
31
|
+
* \begin{align*}
|
|
32
|
+
* \mu_n &= \frac{1}{n} \sum_{i=0}^{n-1} x_i \\
|
|
33
|
+
* &= \frac{1}{n} \biggl(x_{n-1} + \sum_{i=0}^{n-2} x_i \biggr) \\
|
|
34
|
+
* &= \frac{1}{n} (x_{n-1} + (n-1)\mu_{n-1}) \\
|
|
35
|
+
* &= \mu_{n-1} + \frac{1}{n} (x_{n-1} - \mu_{n-1})
|
|
36
|
+
* \end{align*}
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* ## References
|
|
40
|
+
*
|
|
41
|
+
* - Welford, B. P. 1962. "Note on a Method for Calculating Corrected Sums of Squares and Products." _Technometrics_ 4 (3). Taylor & Francis: 419–20. doi:[10.1080/00401706.1962.10490022](https://doi.org/10.1080/00401706.1962.10490022).
|
|
42
|
+
* - van Reeken, A. J. 1968. "Letters to the Editor: Dealing with Neely's Algorithms." _Communications of the ACM_ 11 (3): 149–50. doi:[10.1145/362929.362961](https://doi.org/10.1145/362929.362961).
|
|
43
|
+
*
|
|
44
|
+
* @param N number of indexed elements
|
|
45
|
+
* @param X input array
|
|
46
|
+
* @param strideX stride length
|
|
47
|
+
* @return output value
|
|
48
|
+
*/
|
|
49
|
+
float API_SUFFIX(stdlib_strided_snanmeanwd)( const CBLAS_INT N, const float *X, const CBLAS_INT strideX ) {
|
|
50
|
+
const CBLAS_INT ox = stdlib_strided_stride2offset( N, strideX );
|
|
51
|
+
return API_SUFFIX(stdlib_strided_snanmeanwd_ndarray)( N, X, strideX, ox );
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Computes the arithmetic mean of a single-precision floating-point strided array, ignoring `NaN` values and using Welford's algorithm and alternative indexing semantics.
|
|
56
|
+
*
|
|
57
|
+
* @param N number of indexed elements
|
|
58
|
+
* @param X input array
|
|
59
|
+
* @param strideX stride length
|
|
60
|
+
* @param offsetX starting index for X
|
|
61
|
+
* @return output value
|
|
62
|
+
*/
|
|
63
|
+
float API_SUFFIX(stdlib_strided_snanmeanwd_ndarray)( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ) {
|
|
64
|
+
CBLAS_INT ix;
|
|
65
|
+
CBLAS_INT i;
|
|
66
|
+
double n;
|
|
67
|
+
float mu;
|
|
68
|
+
float v;
|
|
69
|
+
|
|
70
|
+
if ( N <= 0 ) {
|
|
71
|
+
return 0.0f / 0.0f; // NaN
|
|
72
|
+
}
|
|
73
|
+
if ( N == 1 || strideX == 0 ) {
|
|
74
|
+
return X[ 0 ];
|
|
75
|
+
}
|
|
76
|
+
ix = offsetX;
|
|
77
|
+
mu = 0.0f;
|
|
78
|
+
n = 0.0;
|
|
79
|
+
for ( i = 0; i < N; i++ ) {
|
|
80
|
+
v = X[ ix ];
|
|
81
|
+
if ( v == v ) {
|
|
82
|
+
n += 1.0;
|
|
83
|
+
mu += (float)((double)( v-mu ) / n);
|
|
84
|
+
}
|
|
85
|
+
ix += strideX;
|
|
86
|
+
}
|
|
87
|
+
if ( n == 0.0 ) {
|
|
88
|
+
return 0.0f / 0.0f; // NaN
|
|
89
|
+
}
|
|
90
|
+
return mu;
|
|
91
|
+
}
|