@stdlib/stats-strided-snanmeanpn 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/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,91 @@
1
+ {
2
+ "name": "@stdlib/stats-strided-snanmeanpn",
3
+ "version": "0.1.0",
4
+ "description": "Calculate the arithmetic mean of a single-precision floating-point strided array, ignoring NaN values and using a two-pass error correction 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-snanmeanpn.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
+ "strided",
79
+ "strided array",
80
+ "typed",
81
+ "array",
82
+ "float32",
83
+ "single",
84
+ "float",
85
+ "float32array"
86
+ ],
87
+ "funding": {
88
+ "type": "opencollective",
89
+ "url": "https://opencollective.com/stdlib"
90
+ }
91
+ }
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/snanmeanpn.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_snanmeanpn)( 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_snanmeanpn_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,101 @@
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/snanmeanpn.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 a two-pass error correction algorithm.
25
+ *
26
+ * ## Method
27
+ *
28
+ * - This implementation uses a two-pass approach, as suggested by Neely (1966).
29
+ *
30
+ * ## References
31
+ *
32
+ * - 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).
33
+ * - 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).
34
+ *
35
+ * @param N number of indexed elements
36
+ * @param X input array
37
+ * @param strideX stride length
38
+ * @return output value
39
+ */
40
+ float API_SUFFIX(stdlib_strided_snanmeanpn)( const CBLAS_INT N, const float *X, const CBLAS_INT strideX ) {
41
+ const CBLAS_INT ox = stdlib_strided_stride2offset( N, strideX );
42
+ return API_SUFFIX(stdlib_strided_snanmeanpn_ndarray)( N, X, strideX, ox );
43
+ }
44
+
45
+ /**
46
+ * Computes the arithmetic mean of a single-precision floating-point strided array, ignoring `NaN` values and using a two-pass error correction algorithm and alternative indexing semantics.
47
+ *
48
+ * @param N number of indexed elements
49
+ * @param X input array
50
+ * @param strideX stride length
51
+ * @param offsetX starting index for X
52
+ * @return output value
53
+ */
54
+ float API_SUFFIX(stdlib_strided_snanmeanpn_ndarray)( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ) {
55
+ CBLAS_INT ix;
56
+ CBLAS_INT i;
57
+ CBLAS_INT n;
58
+ CBLAS_INT o;
59
+ double dn;
60
+ float s;
61
+ float t;
62
+ float v;
63
+
64
+ if ( N <= 0 ) {
65
+ return 0.0f / 0.0f; // NaN
66
+ }
67
+ if ( N == 1 || strideX == 0 ) {
68
+ return X[ offsetX ];
69
+ }
70
+ ix = offsetX;
71
+ o = ix;
72
+
73
+ // Compute an estimate for the mean...
74
+ s = 0.0f;
75
+ n = 0;
76
+ for ( i = 0; i < N; i++ ) {
77
+ v = X[ ix ];
78
+ if ( v == v ) {
79
+ s += v;
80
+ n += 1;
81
+ }
82
+ ix += strideX;
83
+ }
84
+ if ( n == 0 ) {
85
+ return 0.0f / 0.0f; // NaN
86
+ }
87
+ dn = (double)n;
88
+ s = (double)s / dn;
89
+
90
+ // Compute an error term...
91
+ t = 0.0f;
92
+ ix = o;
93
+ for ( i = 0; i < N; i++ ) {
94
+ v = X[ ix ];
95
+ if ( v == v ) {
96
+ t += v - s;
97
+ }
98
+ ix += strideX;
99
+ }
100
+ return s + (float)((double)t/dn);
101
+ }