From de9014565bfa56f543e6181328ebfa3c7e62ed9e Mon Sep 17 00:00:00 2001 From: Aadish Jain Date: Fri, 13 Dec 2024 23:01:06 +0000 Subject: [PATCH 1/4] feat(binomial-cdf): adding all required binomial cdf functionality --- .../include/stdlib/math/base/napi/ternary.h | 47 ++++ .../@stdlib/math/base/napi/ternary/src/main.c | 76 +++++++ .../stats/base/dists/binomial/cdf/README.md | 97 +++++++++ .../dists/binomial/cdf/benchmark/benchmark.js | 62 +++--- .../cdf/benchmark/benchmark.native.js | 74 +++++++ .../dists/binomial/cdf/benchmark/c/Makefile | 146 +++++++++++++ .../binomial/cdf/benchmark/c/benchmark.c | 142 ++++++++++++ .../stats/base/dists/binomial/cdf/binding.gyp | 170 +++++++++++++++ .../dists/binomial/cdf/examples/c/Makefile | 146 +++++++++++++ .../dists/binomial/cdf/examples/c/example.c | 42 ++++ .../base/dists/binomial/cdf/include.gypi | 53 +++++ .../stdlib/stats/base/dists/binomial/cdf.h | 45 ++++ .../base/dists/binomial/cdf/lib/native.js | 61 ++++++ .../base/dists/binomial/cdf/manifest.json | 82 +++++++ .../base/dists/binomial/cdf/package.json | 3 + .../base/dists/binomial/cdf/src/Makefile | 70 ++++++ .../stats/base/dists/binomial/cdf/src/addon.c | 22 ++ .../stats/base/dists/binomial/cdf/src/main.c | 55 +++++ .../dists/binomial/cdf/test/test.native.js | 203 ++++++++++++++++++ 19 files changed, 1570 insertions(+), 26 deletions(-) create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/cdf/benchmark/benchmark.native.js create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/cdf/benchmark/c/Makefile create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/cdf/benchmark/c/benchmark.c create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/cdf/binding.gyp create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/cdf/examples/c/Makefile create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/cdf/examples/c/example.c create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/cdf/include.gypi create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/cdf/include/stdlib/stats/base/dists/binomial/cdf.h create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/cdf/lib/native.js create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/cdf/manifest.json create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/cdf/src/Makefile create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/cdf/src/addon.c create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/cdf/src/main.c create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/cdf/test/test.native.js diff --git a/lib/node_modules/@stdlib/math/base/napi/ternary/include/stdlib/math/base/napi/ternary.h b/lib/node_modules/@stdlib/math/base/napi/ternary/include/stdlib/math/base/napi/ternary.h index 514b72add029..c918e2410b2d 100644 --- a/lib/node_modules/@stdlib/math/base/napi/ternary/include/stdlib/math/base/napi/ternary.h +++ b/lib/node_modules/@stdlib/math/base/napi/ternary/include/stdlib/math/base/napi/ternary.h @@ -144,6 +144,48 @@ }; \ NAPI_MODULE( NODE_GYP_MODULE_NAME, stdlib_math_base_napi_dii_d_init ) +/** +* Macro for registering a Node-API module exporting an interface invoking a ternary function accepting a double-precision floating-point number and two signed 64-bit integers and returning a double-precision floating-point number. +* +* @param fcn ternary function +* +* @example +* #include +* +* static double fcn( const int_64 x, const int_64 y, const double z ) { +* // ... +* } +* +* // ... +* +* // Register a Node-API module: +* STDLIB_MATH_BASE_NAPI_MODULE_LLD_D( fcn ); +*/ +#define STDLIB_MATH_BASE_NAPI_MODULE_LLD_D( fcn ) \ + static napi_value stdlib_math_base_napi_lld_d_wrapper( \ + napi_env env, \ + napi_callback_info info \ + ) { \ + return stdlib_math_base_napi_lld_d( env, info, fcn ); \ + }; \ + static napi_value stdlib_math_base_napi_lld_d_init( \ + napi_env env, \ + napi_value exports \ + ) { \ + napi_value fcn; \ + napi_status status = napi_create_function( \ + env, \ + "exports", \ + NAPI_AUTO_LENGTH, \ + stdlib_math_base_napi_lld_d_wrapper, \ + NULL, \ + &fcn \ + ); \ + assert( status == napi_ok ); \ + return fcn; \ + }; \ + NAPI_MODULE( NODE_GYP_MODULE_NAME, stdlib_math_base_napi_lld_d_init ) + /* * If C++, prevent name mangling so that the compiler emits a ternary file having undecorated names, thus mirroring the behavior of a C compiler. */ @@ -166,6 +208,11 @@ napi_value stdlib_math_base_napi_fff_f( napi_env env, napi_callback_info info, f */ napi_value stdlib_math_base_napi_dii_d( napi_env env, napi_callback_info info, double (*fcn)( double, int32_t, int32_t ) ); +/** +* Invokes a ternary function accepting a double-precision floating-point number and two signed 64-bit integers and returning a double-precision floating-point number. +*/ +napi_value stdlib_math_base_napi_lld_d( napi_env env, napi_callback_info info, double ( *fcn )( int64_t, int64_t, double ) ); + #ifdef __cplusplus } #endif diff --git a/lib/node_modules/@stdlib/math/base/napi/ternary/src/main.c b/lib/node_modules/@stdlib/math/base/napi/ternary/src/main.c index 92b89ca0999e..77a4f4831145 100644 --- a/lib/node_modules/@stdlib/math/base/napi/ternary/src/main.c +++ b/lib/node_modules/@stdlib/math/base/napi/ternary/src/main.c @@ -248,3 +248,79 @@ napi_value stdlib_math_base_napi_dii_d( napi_env env, napi_callback_info info, d return v; } + +/** +* Invokes a ternary function accepting a double-precision floating-point number and two signed 64-bit integers and returning a double-precision floating-point number. +* +* ## Notes +* +* - This function expects that the callback `info` argument provides access to the following JavaScript arguments: +* +* - `x`: input value. +* - `y`: input value. +* - `z`: input value. +* +* @param env environment under which the function is invoked +* @param info callback data +* @param fcn ternary function +* @return function return value as a Node-API double-precision floating-point number +*/ +napi_value stdlib_math_base_napi_lld_d( napi_env env, napi_callback_info info, double ( *fcn )( int64_t, int64_t, double ) ) { + napi_status status; + + size_t argc = 3; + napi_value argv[ 3 ]; + status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL ); + assert( status == napi_ok ); + + if ( argc < 3 ) { + status = napi_throw_error( env, NULL, "invalid invocation. Must provide three numbers." ); + assert( status == napi_ok ); + return NULL; + } + + napi_valuetype vtype0; + status = napi_typeof( env, argv[ 0 ], &vtype0 ); + assert( status == napi_ok ); + if ( vtype0 != napi_number ) { + status = napi_throw_type_error( env, NULL, "invalid argument. First argument must be a number." ); + assert( status == napi_ok ); + return NULL; + } + + napi_valuetype vtype1; + status = napi_typeof( env, argv[ 1 ], &vtype1 ); + assert( status == napi_ok ); + if ( vtype1 != napi_number ) { + status = napi_throw_type_error( env, NULL, "invalid argument. Second argument must be a number." ); + assert( status == napi_ok ); + return NULL; + } + + napi_valuetype vtype2; + status = napi_typeof( env, argv[ 2 ], &vtype2 ); + assert( status == napi_ok ); + if ( vtype2 != napi_number ) { + status = napi_throw_type_error( env, NULL, "invalid argument. Third argument must be a number." ); + assert( status == napi_ok ); + return NULL; + } + + int64_t x; + status = napi_get_value_int64( env, argv[ 0 ], &x ); + assert( status == napi_ok ); + + int64_t y; + status = napi_get_value_int64( env, argv[ 1 ], &y ); + assert( status == napi_ok ); + + double z; + status = napi_get_value_double( env, argv[ 2 ], &z ); + assert( status == napi_ok ); + + napi_value v; + status = napi_create_double( env, fcn( x, y, z ), &v ); + assert( status == napi_ok ); + + return v; +} diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/cdf/README.md b/lib/node_modules/@stdlib/stats/base/dists/binomial/cdf/README.md index 44a044f55476..bedb5f0dac06 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/cdf/README.md +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/cdf/README.md @@ -167,6 +167,103 @@ for ( i = 0; i < 10; i++ ) { + + +* * * + +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### Usage + +```c +#include "stdlib/stats/base/dists/binomial/cdf.h" +``` + +#### stdlib_base_dists_binomial_cdf( k, n, p ) + +Evaluates the cumulative distribution function (CDF) for a binomial distribution. + +```c +double out = stdlib_base_dists_binomial_cdf( 5, 10, 0.5 ); +// returns ~0.623 +``` + +The function accepts the following arguments: + +- **k**: `[in] int` input value. +- **n**: `[in] int` number of trials. +- **p**: `[in] double` success probability. + +```c +double stdlib_base_dists_binomial_cdf( const int64_t k, const int64_t n, const double p ); +``` + +
+ + + + +
+ +
+ + + + + +
+### Examples + +```c +#include "stdlib/stats/base/dists/binomial/cdf.h" +#include +#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 ) { + int64_t k; + int64_t n; + double p; + double y; + int i; + + for ( i = 0; i < 25; i++ ) { + k = (int64_t)random_uniform(0.0,10.0); // Random integer between 0 and 10 + n = k+(int64_t)random_uniform(0.0,10.0); // Random integer between k and 20 + p = random_uniform(0.0,1.0); // Random double between 0.0 and 1.0 + y = stdlib_base_dists_binomial_cdf( k, n, p ); + printf( "k: %d, n: %d, p: %lf, F(k;n,p): %lf\n", k, n, p, y ); + } +} +``` + +
+ + + +
+ + +