From 54367d57a8601304bd01e366f6aceffba3876f06 Mon Sep 17 00:00:00 2001 From: aayush0325 Date: Mon, 13 Jan 2025 17:43:29 +0000 Subject: [PATCH] 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: 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 --- --- .../include/stdlib/stats/base/dmeanors.h | 9 +++- .../stats/base/dmeanors/lib/dmeanors.js | 18 +++---- .../base/dmeanors/lib/dmeanors.native.js | 9 ++-- .../@stdlib/stats/base/dmeanors/lib/index.js | 7 +-- .../stats/base/dmeanors/lib/ndarray.js | 16 +++--- .../stats/base/dmeanors/lib/ndarray.native.js | 20 +++---- .../@stdlib/stats/base/dmeanors/manifest.json | 45 +++++++++++----- .../@stdlib/stats/base/dmeanors/src/addon.c | 26 +++++++-- .../stats/base/dmeanors/src/dmeanors.c | 39 -------------- .../@stdlib/stats/base/dmeanors/src/main.c | 54 +++++++++++++++++++ 10 files changed, 141 insertions(+), 102 deletions(-) delete mode 100644 lib/node_modules/@stdlib/stats/base/dmeanors/src/dmeanors.c create mode 100644 lib/node_modules/@stdlib/stats/base/dmeanors/src/main.c diff --git a/lib/node_modules/@stdlib/stats/base/dmeanors/include/stdlib/stats/base/dmeanors.h b/lib/node_modules/@stdlib/stats/base/dmeanors/include/stdlib/stats/base/dmeanors.h index 909d7215c7d5..c508c4e9495f 100644 --- a/lib/node_modules/@stdlib/stats/base/dmeanors/include/stdlib/stats/base/dmeanors.h +++ b/lib/node_modules/@stdlib/stats/base/dmeanors/include/stdlib/stats/base/dmeanors.h @@ -19,7 +19,7 @@ #ifndef STDLIB_STATS_BASE_DMEANORS_H #define STDLIB_STATS_BASE_DMEANORS_H -#include +#include "stdlib/blas/base/shared.h" /* * 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" { /** * Computes the arithmetic mean of a double-precision floating-point strided array using ordinary recursive summation. */ -double stdlib_strided_dmeanors( const int64_t N, const double *X, const int64_t stride ); +double API_SUFFIX(stdlib_strided_dmeanors)( const CBLAS_INT N, const double *X, const CBLAS_INT strideX ); + +/** +* Computes the arithmetic mean of a double-precision floating-point strided array using ordinary recursive summation and alternative indexing semantics. +*/ +double API_SUFFIX(stdlib_strided_dmeanors_ndarray)( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ); #ifdef __cplusplus } diff --git a/lib/node_modules/@stdlib/stats/base/dmeanors/lib/dmeanors.js b/lib/node_modules/@stdlib/stats/base/dmeanors/lib/dmeanors.js index 9b96ecff81e7..c02463849521 100644 --- a/lib/node_modules/@stdlib/stats/base/dmeanors/lib/dmeanors.js +++ b/lib/node_modules/@stdlib/stats/base/dmeanors/lib/dmeanors.js @@ -20,7 +20,8 @@ // MODULES // -var dsumors = require( '@stdlib/blas/ext/base/dsumors' ); +var stride2offset = require( '@stdlib/strided/base/stride2offset' ); +var ndarray = require( './ndarray.js' ); // MAIN // @@ -30,26 +31,19 @@ var dsumors = require( '@stdlib/blas/ext/base/dsumors' ); * * @param {PositiveInteger} N - number of indexed elements * @param {Float64Array} x - input array -* @param {integer} stride - stride length +* @param {integer} strideX - stride length * @returns {number} arithmetic mean * * @example * var Float64Array = require( '@stdlib/array/float64' ); * * var x = new Float64Array( [ 1.0, -2.0, 2.0 ] ); -* var N = x.length; * -* var v = dmeanors( N, x, 1 ); +* var v = dmeanors( x.length, x, 1 ); * // returns ~0.3333 */ -function dmeanors( N, x, stride ) { - if ( N <= 0 ) { - return NaN; - } - if ( N === 1 || stride === 0 ) { - return x[ 0 ]; - } - return dsumors( N, x, stride ) / N; +function dmeanors( N, x, strideX ) { + return ndarray( N, x, strideX, stride2offset( N, strideX ) ); } diff --git a/lib/node_modules/@stdlib/stats/base/dmeanors/lib/dmeanors.native.js b/lib/node_modules/@stdlib/stats/base/dmeanors/lib/dmeanors.native.js index 37421aea14a2..63a193e1f77b 100644 --- a/lib/node_modules/@stdlib/stats/base/dmeanors/lib/dmeanors.native.js +++ b/lib/node_modules/@stdlib/stats/base/dmeanors/lib/dmeanors.native.js @@ -30,20 +30,19 @@ var addon = require( './../src/addon.node' ); * * @param {PositiveInteger} N - number of indexed elements * @param {Float64Array} x - input array -* @param {integer} stride - stride length +* @param {integer} strideX - stride length * @returns {number} arithmetic mean * * @example * var Float64Array = require( '@stdlib/array/float64' ); * * var x = new Float64Array( [ 1.0, -2.0, 2.0 ] ); -* var N = x.length; * -* var v = dmeanors( N, x, 1 ); +* var v = dmeanors( x.length, x, 1 ); * // returns ~0.3333 */ -function dmeanors( N, x, stride ) { - return addon( N, x, stride ); +function dmeanors( N, x, strideX ) { + return addon( N, x, strideX ); } diff --git a/lib/node_modules/@stdlib/stats/base/dmeanors/lib/index.js b/lib/node_modules/@stdlib/stats/base/dmeanors/lib/index.js index 3172955e48cc..420515201cf6 100644 --- a/lib/node_modules/@stdlib/stats/base/dmeanors/lib/index.js +++ b/lib/node_modules/@stdlib/stats/base/dmeanors/lib/index.js @@ -28,20 +28,17 @@ * var dmeanors = require( '@stdlib/stats/base/dmeanors' ); * * var x = new Float64Array( [ 1.0, -2.0, 2.0 ] ); -* var N = x.length; * -* var v = dmeanors( N, x, 1 ); +* var v = dmeanors( x.length, x, 1 ); * // returns ~0.3333 * * @example * var Float64Array = require( '@stdlib/array/float64' ); -* var floor = require( '@stdlib/math/base/special/floor' ); * var dmeanors = require( '@stdlib/stats/base/dmeanors' ); * * var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); -* var N = floor( x.length / 2 ); * -* var v = dmeanors.ndarray( N, x, 2, 1 ); +* var v = dmeanors.ndarray( 4, x, 2, 1 ); * // returns 1.25 */ diff --git a/lib/node_modules/@stdlib/stats/base/dmeanors/lib/ndarray.js b/lib/node_modules/@stdlib/stats/base/dmeanors/lib/ndarray.js index cca34655640f..47763f2a7941 100644 --- a/lib/node_modules/@stdlib/stats/base/dmeanors/lib/ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/dmeanors/lib/ndarray.js @@ -30,28 +30,26 @@ var dsumors = require( '@stdlib/blas/ext/base/dsumors' ).ndarray; * * @param {PositiveInteger} N - number of indexed elements * @param {Float64Array} x - input array -* @param {integer} stride - stride length -* @param {NonNegativeInteger} offset - starting index +* @param {integer} strideX - stride length +* @param {NonNegativeInteger} offsetX - starting index * @returns {number} arithmetic mean * * @example * var Float64Array = require( '@stdlib/array/float64' ); -* var floor = require( '@stdlib/math/base/special/floor' ); * * var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); -* var N = floor( x.length / 2 ); * -* var v = dmeanors( N, x, 2, 1 ); +* var v = dmeanors( 4, x, 2, 1 ); * // returns 1.25 */ -function dmeanors( N, x, stride, offset ) { +function dmeanors( N, x, strideX, offsetX ) { if ( N <= 0 ) { return NaN; } - if ( N === 1 || stride === 0 ) { - return x[ offset ]; + if ( N === 1 || strideX === 0 ) { + return x[ offsetX ]; } - return dsumors( N, x, stride, offset ) / N; + return dsumors( N, x, strideX, offsetX ) / N; } diff --git a/lib/node_modules/@stdlib/stats/base/dmeanors/lib/ndarray.native.js b/lib/node_modules/@stdlib/stats/base/dmeanors/lib/ndarray.native.js index 84e10b7f9e00..c78167f66496 100644 --- a/lib/node_modules/@stdlib/stats/base/dmeanors/lib/ndarray.native.js +++ b/lib/node_modules/@stdlib/stats/base/dmeanors/lib/ndarray.native.js @@ -20,8 +20,7 @@ // MODULES // -var Float64Array = require( '@stdlib/array/float64' ); -var addon = require( './dmeanors.native.js' ); +var addon = require( './../src/addon.node' ); // MAIN // @@ -31,27 +30,20 @@ var addon = require( './dmeanors.native.js' ); * * @param {PositiveInteger} N - number of indexed elements * @param {Float64Array} x - input array -* @param {integer} stride - stride length -* @param {NonNegativeInteger} offset - starting index +* @param {integer} strideX - stride length +* @param {NonNegativeInteger} offsetX - starting index * @returns {number} arithmetic mean * * @example * var Float64Array = require( '@stdlib/array/float64' ); -* var floor = require( '@stdlib/math/base/special/floor' ); * * var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); -* var N = floor( x.length / 2 ); * -* var v = dmeanors( N, x, 2, 1 ); +* var v = dmeanors( 4, x, 2, 1 ); * // returns 1.25 */ -function dmeanors( N, x, stride, offset ) { - var view; - if ( stride < 0 ) { - offset += (N-1) * stride; - } - view = new Float64Array( x.buffer, x.byteOffset+(x.BYTES_PER_ELEMENT*offset), x.length-offset ); // eslint-disable-line max-len - return addon( N, view, stride ); +function dmeanors( N, x, strideX, offsetX ) { + return addon.ndarray( N, x, strideX, offsetX ); } diff --git a/lib/node_modules/@stdlib/stats/base/dmeanors/manifest.json b/lib/node_modules/@stdlib/stats/base/dmeanors/manifest.json index c4d125525906..58b25e8b64fc 100644 --- a/lib/node_modules/@stdlib/stats/base/dmeanors/manifest.json +++ b/lib/node_modules/@stdlib/stats/base/dmeanors/manifest.json @@ -1,6 +1,7 @@ { "options": { - "task": "build" + "task": "build", + "wasm": false }, "fields": [ { @@ -27,17 +28,18 @@ "confs": [ { "task": "build", + "wasm": false, "src": [ - "./src/dmeanors.c" + "./src/main.c" ], "include": [ "./include" ], - "libraries": [ - "-lm" - ], + "libraries": [], "libpath": [], "dependencies": [ + "@stdlib/blas/base/shared", + "@stdlib/strided/base/stride2offset", "@stdlib/blas/ext/base/dsumors", "@stdlib/napi/export", "@stdlib/napi/argv", @@ -48,33 +50,52 @@ }, { "task": "benchmark", + "wasm": false, "src": [ - "./src/dmeanors.c" + "./src/main.c" ], "include": [ "./include" ], - "libraries": [ - "-lm" - ], + "libraries": [], "libpath": [], "dependencies": [ + "@stdlib/blas/base/shared", + "@stdlib/strided/base/stride2offset", "@stdlib/blas/ext/base/dsumors" ] }, { "task": "examples", + "wasm": false, "src": [ - "./src/dmeanors.c" + "./src/main.c" ], "include": [ "./include" ], - "libraries": [ - "-lm" + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/blas/base/shared", + "@stdlib/strided/base/stride2offset", + "@stdlib/blas/ext/base/dsumors" + ] + }, + { + "task": "", + "wasm": true, + "src": [ + "./src/main.c" + ], + "include": [ + "./include" ], + "libraries": [], "libpath": [], "dependencies": [ + "@stdlib/blas/base/shared", + "@stdlib/strided/base/stride2offset", "@stdlib/blas/ext/base/dsumors" ] } diff --git a/lib/node_modules/@stdlib/stats/base/dmeanors/src/addon.c b/lib/node_modules/@stdlib/stats/base/dmeanors/src/addon.c index 4d4bffc1a971..1985367b0455 100644 --- a/lib/node_modules/@stdlib/stats/base/dmeanors/src/addon.c +++ b/lib/node_modules/@stdlib/stats/base/dmeanors/src/addon.c @@ -17,6 +17,7 @@ */ #include "stdlib/stats/base/dmeanors.h" +#include "stdlib/blas/base/shared.h" #include "stdlib/napi/export.h" #include "stdlib/napi/argv.h" #include "stdlib/napi/argv_int64.h" @@ -34,10 +35,27 @@ static napi_value addon( napi_env env, napi_callback_info info ) { STDLIB_NAPI_ARGV( env, info, argv, argc, 3 ); STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 ); - STDLIB_NAPI_ARGV_INT64( env, stride, argv, 2 ); - STDLIB_NAPI_ARGV_STRIDED_FLOAT64ARRAY( env, X, N, stride, argv, 1 ); - STDLIB_NAPI_CREATE_DOUBLE( env, stdlib_strided_dmeanors( N, X, stride ), v ); + STDLIB_NAPI_ARGV_INT64( env, strideX, argv, 2 ); + STDLIB_NAPI_ARGV_STRIDED_FLOAT64ARRAY( env, X, N, strideX, argv, 1 ); + STDLIB_NAPI_CREATE_DOUBLE( env, API_SUFFIX(stdlib_strided_dmeanors)( N, X, strideX ), v ); return v; } -STDLIB_NAPI_MODULE_EXPORT_FCN( addon ) +/** +* Receives JavaScript callback invocation data. +* +* @param env environment under which the function is invoked +* @param info callback data +* @return Node-API value +*/ +static napi_value addon_method( napi_env env, napi_callback_info info ) { + STDLIB_NAPI_ARGV( env, info, argv, argc, 4 ); + STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 ); + STDLIB_NAPI_ARGV_INT64( env, strideX, argv, 2 ); + STDLIB_NAPI_ARGV_INT64( env, offsetX, argv, 3 ); + STDLIB_NAPI_ARGV_STRIDED_FLOAT64ARRAY( env, X, N, strideX, argv, 1 ); + STDLIB_NAPI_CREATE_DOUBLE( env, API_SUFFIX(stdlib_strided_dmeanors_ndarray)( N, X, strideX, offsetX ), v ); + return v; +} + +STDLIB_NAPI_MODULE_EXPORT_FCN_WITH_METHOD( addon, "ndarray", addon_method ) diff --git a/lib/node_modules/@stdlib/stats/base/dmeanors/src/dmeanors.c b/lib/node_modules/@stdlib/stats/base/dmeanors/src/dmeanors.c deleted file mode 100644 index d2b73a48343b..000000000000 --- a/lib/node_modules/@stdlib/stats/base/dmeanors/src/dmeanors.c +++ /dev/null @@ -1,39 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2020 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -#include "stdlib/stats/base/dmeanors.h" -#include "stdlib/blas/ext/base/dsumors.h" -#include - -/** -* Computes the arithmetic mean of a double-precision floating-point strided array using ordinary recursive summation. -* -* @param N number of indexed elements -* @param X input array -* @param stride stride length -* @return output value -*/ -double stdlib_strided_dmeanors( const int64_t N, const double *X, const int64_t stride ) { - if ( N <= 0 ) { - return 0.0 / 0.0; // NaN - } - if ( N == 1 || stride == 0 ) { - return X[ 0 ]; - } - return stdlib_strided_dsumors( N, X, stride ) / (double)N; -} diff --git a/lib/node_modules/@stdlib/stats/base/dmeanors/src/main.c b/lib/node_modules/@stdlib/stats/base/dmeanors/src/main.c new file mode 100644 index 000000000000..acf24890cf0d --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dmeanors/src/main.c @@ -0,0 +1,54 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "stdlib/stats/base/dmeanors.h" +#include "stdlib/blas/ext/base/dsumors.h" +#include "stdlib/blas/base/shared.h" +#include "stdlib/strided/base/stride2offset.h" + +/** +* Computes the arithmetic mean of a double-precision floating-point strided array using ordinary recursive summation. +* +* @param N number of indexed elements +* @param X input array +* @param strideX stride length +* @return output value +*/ +double API_SUFFIX(stdlib_strided_dmeanors)( const CBLAS_INT N, const double *X, const CBLAS_INT strideX ) { + const CBLAS_INT ox = stdlib_strided_stride2offset( N, strideX ); + return API_SUFFIX(stdlib_strided_dmeanors_ndarray)( N, X, strideX, ox ); +} + +/** +* Computes the arithmetic mean of a double-precision floating-point strided array using ordinary recursive summation and alternative indexing semantics. +* +* @param N number of indexed elements +* @param X input array +* @param strideX stride length +* @param offsetX starting index for X +* @return output value +*/ +double API_SUFFIX(stdlib_strided_dmeanors_ndarray)( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ) { + if ( N <= 0 ) { + return 0.0 / 0.0; // NaN + } + if ( N == 1 || strideX == 0 ) { + return X[ offsetX ]; + } + return API_SUFFIX(stdlib_strided_dsumors_ndarray)( N, X, strideX, offsetX ) / (double)N; +}