From 692afcb69e47de99d9675571503e6cc7a73dd1a7 Mon Sep 17 00:00:00 2001 From: Neeraj Pathak Date: Fri, 27 Dec 2024 14:30:14 +0530 Subject: [PATCH 1/4] refactor: update `stats/base/dnanmeanpn` native addon from C++ to C PR-URL: https://github.com/stdlib-js/stdlib/pull/4110 Reviewed-by: Athan Reines --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: --- --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: passed - task: run_c_examples status: passed - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: passed - task: run_c_benchmarks status: passed - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: passed - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: passed --- --- .../stats/base/dnanmeanpn/include.gypi | 2 +- .../stats/base/dnanmeanpn/manifest.json | 39 +++++- .../@stdlib/stats/base/dnanmeanpn/src/addon.c | 43 +++++++ .../stats/base/dnanmeanpn/src/addon.cpp | 117 ------------------ 4 files changed, 82 insertions(+), 119 deletions(-) create mode 100644 lib/node_modules/@stdlib/stats/base/dnanmeanpn/src/addon.c delete mode 100644 lib/node_modules/@stdlib/stats/base/dnanmeanpn/src/addon.cpp diff --git a/lib/node_modules/@stdlib/stats/base/dnanmeanpn/include.gypi b/lib/node_modules/@stdlib/stats/base/dnanmeanpn/include.gypi index 868c5c12e852..26476a8c2655 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanmeanpn/include.gypi +++ b/lib/node_modules/@stdlib/stats/base/dnanmeanpn/include.gypi @@ -36,7 +36,7 @@ # Source files: 'src_files': [ - '<(src_dir)/addon.cpp', + '<(src_dir)/addon.c', ' + +/** +* 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( 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_dnanmeanpn( N, X, stride ), v ); + return v; +} + +STDLIB_NAPI_MODULE_EXPORT_FCN( addon ) diff --git a/lib/node_modules/@stdlib/stats/base/dnanmeanpn/src/addon.cpp b/lib/node_modules/@stdlib/stats/base/dnanmeanpn/src/addon.cpp deleted file mode 100644 index 8c46b762906b..000000000000 --- a/lib/node_modules/@stdlib/stats/base/dnanmeanpn/src/addon.cpp +++ /dev/null @@ -1,117 +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/dnanmeanpn.h" -#include -#include -#include -#include -#include - -/** -* Add-on namespace. -*/ -namespace stdlib_stats_base_dnanmeanpn { - - /** - * Computes the arithmetic mean of a double-precision floating-point strided array, ignoring `NaN` values and using a two-pass error correction algorithm. - * - * ## Notes - * - * - When called from JavaScript, the function expects three arguments: - * - * - `N`: number of indexed elements - * - `X`: input array - * - `stride`: stride length - */ - napi_value node_dnanmeanpn( napi_env env, napi_callback_info info ) { - napi_status status; - - size_t argc = 3; - napi_value argv[ 3 ]; - status = napi_get_cb_info( env, info, &argc, argv, nullptr, nullptr ); - assert( status == napi_ok ); - - if ( argc < 3 ) { - napi_throw_error( env, nullptr, "invalid invocation. Must provide 3 arguments." ); - return nullptr; - } - - napi_valuetype vtype0; - status = napi_typeof( env, argv[ 0 ], &vtype0 ); - assert( status == napi_ok ); - if ( vtype0 != napi_number ) { - napi_throw_type_error( env, nullptr, "invalid argument. First argument must be a number." ); - return nullptr; - } - - bool res; - status = napi_is_typedarray( env, argv[ 1 ], &res ); - assert( status == napi_ok ); - if ( res == false ) { - napi_throw_type_error( env, nullptr, "invalid argument. Second argument must be a Float64Array." ); - return nullptr; - } - - napi_valuetype vtype2; - status = napi_typeof( env, argv[ 2 ], &vtype2 ); - assert( status == napi_ok ); - if ( vtype2 != napi_number ) { - napi_throw_type_error( env, nullptr, "invalid argument. Third argument must be a number." ); - return nullptr; - } - - int64_t N; - status = napi_get_value_int64( env, argv[ 0 ], &N ); - assert( status == napi_ok ); - - int64_t stride; - status = napi_get_value_int64( env, argv[ 2 ], &stride ); - assert( status == napi_ok ); - - napi_typedarray_type vtype1; - size_t xlen; - void *X; - status = napi_get_typedarray_info( env, argv[ 1 ], &vtype1, &xlen, &X, nullptr, nullptr ); - assert( status == napi_ok ); - if ( vtype1 != napi_float64_array ) { - napi_throw_type_error( env, nullptr, "invalid argument. Second argument must be a Float64Array." ); - return nullptr; - } - if ( (N-1)*llabs(stride) >= (int64_t)xlen ) { - napi_throw_range_error( env, nullptr, "invalid argument. Second argument has insufficient elements based on the associated stride and the number of indexed elements." ); - return nullptr; - } - - napi_value v; - status = napi_create_double( env, stdlib_strided_dnanmeanpn( N, (double *)X, stride ), &v ); - assert( status == napi_ok ); - - return v; - } - - napi_value Init( napi_env env, napi_value exports ) { - napi_status status; - napi_value fcn; - status = napi_create_function( env, "exports", NAPI_AUTO_LENGTH, node_dnanmeanpn, NULL, &fcn ); - assert( status == napi_ok ); - return fcn; - } - - NAPI_MODULE( NODE_GYP_MODULE_NAME, Init ) -} // end namespace stdlib_stats_base_dnanmeanpn From 665b0c2c3d8977dcd8949f59beb5225e537b3d0c Mon Sep 17 00:00:00 2001 From: JoyBoy <144602492+0PrashantYadav0@users.noreply.github.com> Date: Fri, 3 Jan 2025 21:44:02 +0530 Subject: [PATCH 2/4] feat: add C implementation for cosine logpdf --- 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: passed - task: lint_package_json status: passed - 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: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: passed - task: lint_c_benchmarks status: passed - 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 --- --- .../stats/base/dists/cosine/logpdf/README.md | 107 +++++++++ .../cosine/logpdf/benchmark/benchmark.js | 17 +- .../logpdf/benchmark/benchmark.native.js | 74 ++++++ .../dists/cosine/logpdf/benchmark/c/Makefile | 146 ++++++++++++ .../cosine/logpdf/benchmark/c/benchmark.c | 143 +++++++++++ .../base/dists/cosine/logpdf/binding.gyp | 170 ++++++++++++++ .../dists/cosine/logpdf/examples/c/Makefile | 146 ++++++++++++ .../dists/cosine/logpdf/examples/c/example.c | 43 ++++ .../base/dists/cosine/logpdf/include.gypi | 53 +++++ .../stdlib/stats/base/dists/cosine/logpdf.h | 37 +++ .../base/dists/cosine/logpdf/lib/native.js | 76 ++++++ .../base/dists/cosine/logpdf/manifest.json | 91 +++++++ .../base/dists/cosine/logpdf/package.json | 3 + .../base/dists/cosine/logpdf/src/Makefile | 70 ++++++ .../base/dists/cosine/logpdf/src/addon.c | 23 ++ .../stats/base/dists/cosine/logpdf/src/main.c | 58 +++++ .../dists/cosine/logpdf/test/test.native.js | 222 ++++++++++++++++++ 17 files changed, 1475 insertions(+), 4 deletions(-) create mode 100644 lib/node_modules/@stdlib/stats/base/dists/cosine/logpdf/benchmark/benchmark.native.js create mode 100644 lib/node_modules/@stdlib/stats/base/dists/cosine/logpdf/benchmark/c/Makefile create mode 100644 lib/node_modules/@stdlib/stats/base/dists/cosine/logpdf/benchmark/c/benchmark.c create mode 100644 lib/node_modules/@stdlib/stats/base/dists/cosine/logpdf/binding.gyp create mode 100644 lib/node_modules/@stdlib/stats/base/dists/cosine/logpdf/examples/c/Makefile create mode 100644 lib/node_modules/@stdlib/stats/base/dists/cosine/logpdf/examples/c/example.c create mode 100644 lib/node_modules/@stdlib/stats/base/dists/cosine/logpdf/include.gypi create mode 100644 lib/node_modules/@stdlib/stats/base/dists/cosine/logpdf/include/stdlib/stats/base/dists/cosine/logpdf.h create mode 100644 lib/node_modules/@stdlib/stats/base/dists/cosine/logpdf/lib/native.js create mode 100644 lib/node_modules/@stdlib/stats/base/dists/cosine/logpdf/manifest.json create mode 100644 lib/node_modules/@stdlib/stats/base/dists/cosine/logpdf/src/Makefile create mode 100644 lib/node_modules/@stdlib/stats/base/dists/cosine/logpdf/src/addon.c create mode 100644 lib/node_modules/@stdlib/stats/base/dists/cosine/logpdf/src/main.c create mode 100644 lib/node_modules/@stdlib/stats/base/dists/cosine/logpdf/test/test.native.js diff --git a/lib/node_modules/@stdlib/stats/base/dists/cosine/logpdf/README.md b/lib/node_modules/@stdlib/stats/base/dists/cosine/logpdf/README.md index 0b74b206266b..2116fc035e81 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/cosine/logpdf/README.md +++ b/lib/node_modules/@stdlib/stats/base/dists/cosine/logpdf/README.md @@ -152,6 +152,113 @@ for ( i = 0; i < 10; i++ ) { + + +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### Usage + +```c +#include "stdlib/stats/base/dists/cosine/logpdf.h" +``` + +#### stdlib_base_dists_cosine_logpdf( x, mu, s ) + +Returns the logarithm of the [probability density function][pdf] (PDF) for a [raised cosine][cosine-distribution] distribution with parameters `mu` (location parameter) and `s` (scale parameter). + +```c +double out = stdlib_base_dists_cosine_logpdf( 2.0, 0.0, 3.0 ); +// returns ~-2.485 +``` + +The function accepts the following arguments: + +- **x**: `[in] int` input value. +- **mu**: `[in] int` location parameter. +- **s**: `[in] int` scale parameter. + +```c +double stdlib_base_dists_cosine_logpdf( const double x, const double mu, const double s ); +``` + +
+ + + + + +
+ +
+ + + + + +
+ +### Examples + +```c +#include "stdlib/stats/base/dists/cosine/logpdf.h" +#include "stdlib/constants/float64/eps.h" +#include +#include + +static double random_uniform( const double min, const double max ) { + double v = (double)rand() / ( (double)RAND_MAX + 1.0 ); + return min + ( v*(max-min) ); +} + +int main( void ) { + double mu; + double s; + double x; + double y; + int i; + + for ( i = 0; i < 10; i++ ) { + x = random_uniform( -50.0, 50.0 ); + mu = random_uniform( -50.0, 50.0 ); + s = random_uniform( 0.0, 20.0 ) + STDLIB_CONSTANT_FLOAT64_EPS; + y = stdlib_base_dists_cosine_logpdf( x, mu, s ); + printf( "x: %lf, µ: %lf, s: %lf, ln(f(x;µ,s)): %lf\n", x, mu, s , y ); + } + + return 0; +} +``` + +
+ + + +
+ + + + + +
+ +
+ + +