Skip to content

Commit 045d8d4

Browse files
committed
feat: add C ndarray interface and refactor implementation
--- 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: na - 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 ---
1 parent 861cd7f commit 045d8d4

File tree

5 files changed

+138
-79
lines changed

5 files changed

+138
-79
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#ifndef STDLIB_STATS_BASE_DVARMTK_H
2020
#define STDLIB_STATS_BASE_DVARMTK_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,12 @@ extern "C" {
3131
/**
3232
* Computes the variance of a double-precision floating-point strided array provided a known mean and using a one-pass textbook algorithm.
3333
*/
34-
double stdlib_strided_dvarmtk( const int64_t N, const double mean, const double correction, const double *X, const int64_t stride );
34+
double API_SUFFIX(stdlib_strided_dvarmtk)( const CBLAS_INT N, const double mean, const double correction, const double *X, const CBLAS_INT strideX );
35+
36+
/**
37+
* Computes the variance of a double-precision floating-point strided array provided a known mean and using a one-pass textbook algorithm and alternative indexing semantics.
38+
*/
39+
double API_SUFFIX(stdlib_strided_dvarmtk_ndarray)( const CBLAS_INT N, const double mean, const double correction, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
3540

3641
#ifdef __cplusplus
3742
}

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

Lines changed: 37 additions & 15 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
{
@@ -26,18 +27,19 @@
2627
],
2728
"confs": [
2829
{
29-
"task": "build",
30+
"task": "build",
31+
"wasm": false,
3032
"src": [
31-
"./src/dvarmtk.c"
33+
"./src/main.c"
3234
],
3335
"include": [
3436
"./include"
3537
],
36-
"libraries": [
37-
"-lm"
38-
],
38+
"libraries": [],
3939
"libpath": [],
4040
"dependencies": [
41+
"@stdlib/blas/base/shared",
42+
"@stdlib/strided/base/stride2offset",
4143
"@stdlib/napi/export",
4244
"@stdlib/napi/argv",
4345
"@stdlib/napi/argv-int64",
@@ -48,31 +50,51 @@
4850
},
4951
{
5052
"task": "benchmark",
53+
"wasm": false,
5154
"src": [
52-
"./src/dvarmtk.c"
55+
"./src/main.c"
5356
],
5457
"include": [
5558
"./include"
5659
],
57-
"libraries": [
58-
"-lm"
59-
],
60+
"libraries": [],
6061
"libpath": [],
61-
"dependencies": []
62+
"dependencies": [
63+
"@stdlib/blas/base/shared",
64+
"@stdlib/strided/base/stride2offset"
65+
]
6266
},
6367
{
6468
"task": "examples",
69+
"wasm": false,
6570
"src": [
66-
"./src/dvarmtk.c"
71+
"./src/main.c"
6772
],
6873
"include": [
6974
"./include"
7075
],
71-
"libraries": [
72-
"-lm"
76+
"libraries": [],
77+
"libpath": [],
78+
"dependencies": [
79+
"@stdlib/blas/base/shared",
80+
"@stdlib/strided/base/stride2offset"
81+
]
82+
},
83+
{
84+
"task": "",
85+
"wasm": true,
86+
"src": [
87+
"./src/main.c"
7388
],
89+
"include": [
90+
"./include"
91+
],
92+
"libraries": [],
7493
"libpath": [],
75-
"dependencies": []
94+
"dependencies": [
95+
"@stdlib/blas/base/shared",
96+
"@stdlib/strided/base/stride2offset"
97+
]
7698
}
7799
]
78100
}

lib/node_modules/@stdlib/stats/base/dvarmtk/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_dvarmtk( 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_dvarmtk( 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_dvarmtk_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/dvarmtk/src/dvarmtk.c

Lines changed: 0 additions & 58 deletions
This file was deleted.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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/base/dvarmtk.h"
20+
#include "stdlib/blas/base/shared.h"
21+
#include "stdlib/strided/base/stride2offset.h"
22+
23+
/**
24+
* Computes the variance of a double-precision floating-point strided array provided a known mean and using a one-pass textbook algorithm.
25+
*
26+
* @param N number of indexed elements
27+
* @param mean mean
28+
* @param correction degrees of freedom adjustment
29+
* @param X input array
30+
* @param strideX stride length
31+
* @return output value
32+
*/
33+
double API_SUFFIX(stdlib_strided_dvarmtk)( const CBLAS_INT N, const double mean, const double correction, const double *X, const CBLAS_INT strideX ) {
34+
const CBLAS_INT ox = stdlib_strided_stride2offset( N, strideX );
35+
return API_SUFFIX(stdlib_strided_dvarmtk_ndarray)( N, mean, correction, X, strideX, ox );
36+
}
37+
38+
/**
39+
* Computes the variance of a double-precision floating-point strided array provided a known mean and using a one-pass textbook algorithm and alternative indexing semantics.
40+
*
41+
* @param N number of indexed elements
42+
* @param mean mean
43+
* @param correction degrees of freedom adjustment
44+
* @param X input array
45+
* @param strideX stride length
46+
* @param offsetX starting index for X
47+
* @return output value
48+
*/
49+
double API_SUFFIX(stdlib_strided_dvarmtk_ndarray)( const CBLAS_INT N, const double mean, const double correction, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ) {
50+
CBLAS_INT ix;
51+
CBLAS_INT i;
52+
double M2;
53+
double n;
54+
double d;
55+
56+
n = (double)N - correction;
57+
if ( N <= 0 || n <= 0.0 ) {
58+
return 0.0 / 0.0; // NaN
59+
}
60+
if ( N == 1 || strideX == 0 ) {
61+
return 0.0;
62+
}
63+
ix = offsetX;
64+
M2 = 0.0;
65+
for ( i = 0; i < N; i++ ) {
66+
d = X[ ix ] - mean;
67+
M2 += d * d;
68+
ix += strideX;
69+
}
70+
return M2 / n;
71+
}

0 commit comments

Comments
 (0)