From 56deca4264c9a94416c1d9419b2e6a3f93dea1b2 Mon Sep 17 00:00:00 2001 From: AbhijitRaut04 Date: Mon, 30 Dec 2024 01:58:06 +0530 Subject: [PATCH 1/4] feat: add c 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: 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: missing_dependencies - task: lint_c_examples status: missing_dependencies - task: lint_c_benchmarks status: missing_dependencies - 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/binomial/pmf/README.md | 97 ++++++ .../pmf/benchmark/benchmark.native.js | 78 +++++ .../dists/binomial/pmf/benchmark/c/Makefile | 70 ++++ .../binomial/pmf/benchmark/c/benchmark.c | 142 +++++++++ .../stats/base/dists/binomial/pmf/binding.gyp | 170 ++++++++++ .../dists/binomial/pmf/examples/c/Makefile | 70 ++++ .../dists/binomial/pmf/examples/c/example.c | 42 +++ .../base/dists/binomial/pmf/include.gypi | 53 ++++ .../stdlib/stats/base/dists/binomial/pmf.h | 38 +++ .../base/dists/binomial/pmf/lib/native.js | 88 +++++ .../base/dists/binomial/pmf/manifest.json | 76 +++++ .../base/dists/binomial/pmf/package.json | 3 + .../base/dists/binomial/pmf/src/Makefile | 70 ++++ .../stats/base/dists/binomial/pmf/src/addon.c | 109 +++++++ .../stats/base/dists/binomial/pmf/src/main.c | 66 ++++ .../dists/binomial/pmf/test/test.native.js | 300 ++++++++++++++++++ 16 files changed, 1472 insertions(+) create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/benchmark/benchmark.native.js create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/benchmark/c/Makefile create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/benchmark/c/benchmark.c create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/binding.gyp create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/examples/c/Makefile create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/examples/c/example.c create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/include.gypi create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/include/stdlib/stats/base/dists/binomial/pmf.h create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/lib/native.js create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/manifest.json create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/src/Makefile create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/src/addon.c create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/src/main.c create mode 100644 lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/test/test.native.js diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/README.md b/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/README.md index b4725a5d5719..5cab50e00aa1 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/README.md +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/README.md @@ -152,6 +152,103 @@ for ( i = 0; i < 10; i++ ) { + + +* * * + +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### Usage + +```c +#include "stdlib/stats/base/dists/binomial/pmf.h" +``` + +#### stdlib_base_dists_binomial_pmf( x, n, p ) + +Evaluates the probability mass function (PMF) for a binomial distribution with number of trials `n` and success probability `p` at a value `x`. + +```c +double out = stdlib_base_dists_binomial_pmf( 3.0, 20, 0.2 ); +// returns ~0.205 +``` + +The function accepts the following arguments: + +- **x**: `[in] double` input value. +- **n**: `[in] double` number of trials. +- **p**: `[in] double` success probability. + +```c +double stdlib_base_dists_binomial_pmf( const double x, const double n, const double p ); +``` + +
+ + + + + +
+ +
+ + + + + +
+ +### Examples + +```c +#include "stdlib/stats/base/dists/invgamma/variance.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 x; + double n; + double p; + double y; + int i; + for ( i = 0; i < 25; i++ ) { + x = random_uniform( 0.0, 20.0 ); + n = random_uniform( 0.0, 20.0 ); + n = random_uniform( 0.0, 1.0 ); + y = stdlib_base_dists_binomial_pmf( x, n, p ); + printf( "x: %lf, n: %lf, p: %lf pmf(x, n, p): %lf\n", x, n, p, y ); + } +} +``` + +
+ + + +
+ + + @@ -212,27 +212,33 @@ double stdlib_base_dists_binomial_pmf( const double x, const double n, const dou ### Examples ```c -#include "stdlib/stats/base/dists/invgamma/variance.h" +#include "stdlib/stats/base/dists/binomial/pmf.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) ); } +static int32_t random_int( const int32_t min, const int32_t max ) { + return min + (int32_t)( random_uniform( 0.0, 1.0 ) * ( max - min + 1 ) ); +} + int main( void ) { - double x; - double n; + int32_t n; double p; + double x; double y; int i; + for ( i = 0; i < 25; i++ ) { - x = random_uniform( 0.0, 20.0 ); - n = random_uniform( 0.0, 20.0 ); - n = random_uniform( 0.0, 1.0 ); + n = random_int( 1, 20 ); + x = random_uniform( 0.0, (double)n ); + p = random_uniform( 0.0, 1.0 ); y = stdlib_base_dists_binomial_pmf( x, n, p ); - printf( "x: %lf, n: %lf, p: %lf pmf(x, n, p): %lf\n", x, n, p, y ); + printf( "x: %.4f, n: %d, p: %.4f, P(X = x;n,p): %.4f\n", x, n, p, y ); } } ``` diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/benchmark/benchmark.native.js b/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/benchmark/benchmark.native.js index b6ac2af6931e..59dd85c0c0c2 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/benchmark/benchmark.native.js +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/benchmark/benchmark.native.js @@ -22,10 +22,9 @@ var resolve = require( 'path' ).resolve; var bench = require( '@stdlib/bench' ); -var Float64Array = require( '@stdlib/array/float64' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); var tryRequire = require( '@stdlib/utils/try-require' ); -var ceil = require( '@stdlib/math/base/special/ceil' ); -var randu = require( '@stdlib/random/base/randu' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pkg = require( './../package.json' ).name; @@ -49,14 +48,9 @@ bench( pkg+'::native', opts, function benchmark( b ) { var i; len = 100; - x = new Float64Array( len ); - n = new Float64Array( len ); - p = new Float64Array( len ); - for ( i = 0; i < len; i++ ) { - x[ i ] = ceil( randu()*50.0 ); - y[ i ] = ceil( randu()*50.0 ); - p[ i ] = randu(); - } + x = discreteUniform( len, 0, 50 ); + n = discreteUniform( len, 1, 100 ); + p = uniform( len, 0.0, 1.0 ); b.tic(); for ( i = 0; i < b.iterations; i++ ) { diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/benchmark/c/Makefile b/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/benchmark/c/Makefile index 7733b6180cb4..85a01e54fdaf 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/benchmark/c/Makefile +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/benchmark/c/Makefile @@ -45,19 +45,74 @@ endif endif endif +# Define the program used for compiling C source files: +ifdef C_COMPILER + CC := $(C_COMPILER) +else + CC := gcc +endif + +# Define the command-line options when compiling C files: +CFLAGS ?= \ + -std=c99 \ + -O3 \ + -Wall \ + -pedantic + +# Determine whether to generate position independent code ([1][1], [2][2]). +# +# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options +# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option +ifeq ($(OS), WINNT) + fPIC ?= +else + fPIC ?= -fPIC +endif + +# List of C targets: +c_targets := benchmark.out + # RULES # #/ -# Removes generated files for building an add-on. +# Compiles C source files. +# +# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`) +# @param {string} [CFLAGS] - C compiler options +# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code (e.g., `-fPIC`) +# +# @example +# make +# +# @example +# make all +#/ +all: $(c_targets) + +.PHONY: all + +#/ +# Compiles C source files. +# +# @private +# @param {string} CC - C compiler +# @param {string} CFLAGS - C compiler flags +# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code +#/ +$(c_targets): %.out: %.c + $(QUIET) $(CC) $(CFLAGS) $(fPIC) -o $@ $< -lm + +#/ +# Runs compiled benchmarks. # # @example -# make clean-addon +# make run #/ -clean-addon: - $(QUIET) -rm -f *.o *.node +run: $(c_targets) + $(QUIET) ./$< -.PHONY: clean-addon +.PHONY: run #/ # Removes generated files. @@ -65,6 +120,7 @@ clean-addon: # @example # make clean #/ -clean: clean-addon +clean: + $(QUIET) -rm -f *.o *.out .PHONY: clean diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/benchmark/c/benchmark.c b/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/benchmark/c/benchmark.c index 0dc1d6277eb8..08389f09392b 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/benchmark/c/benchmark.c +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/benchmark/c/benchmark.c @@ -16,14 +16,15 @@ * limitations under the License. */ -#include "stdlib/stats/base/dists/invgamma/pmf.h" +#include "stdlib/stats/base/dists/binomial/pmf.h" #include #include +#include #include #include #include -#define NAME "pmf" +#define NAME "binomial-pmf" #define ITERATIONS 1000000 #define REPEATS 3 @@ -86,6 +87,17 @@ static double random_uniform( const double min, const double max ) { return min + ( v*(max-min) ); } +/** +* Generates a random integer on the interval [min,max]. +* +* @param min minimum value (inclusive) +* @param max maximum value (inclusive) +* @return random integer +*/ +static int32_t random_int( const int32_t min, const int32_t max ) { + return min + (int32_t)( random_uniform( 0.0, 1.0 ) * ( max - min + 1 ) ); +} + /** * Runs a benchmark. * @@ -93,17 +105,17 @@ static double random_uniform( const double min, const double max ) { */ static double benchmark( void ) { double elapsed; - double n[ 100 ]; - double p[ 100 ]; + double p[ 100 ]; double x[ 100 ]; + int32_t n[ 100 ]; double y; double t; int i; for ( i = 0; i < 100; i++ ) { - n[ i ] = random_uniform( 0.0, 20.0 ); + n[ i ] = random_int( 1, 50 ); p[ i ] = random_uniform( 0.0, 1.0 ); - x[ i ] = random_uniform( 0.0, 20.0 ); + x[ i ] = random_uniform( 0.0, (double)n[ i ] ); } t = tic(); diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/examples/c/Makefile b/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/examples/c/Makefile index 7733b6180cb4..25ced822f96a 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/examples/c/Makefile +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/examples/c/Makefile @@ -45,19 +45,94 @@ endif endif endif +# Define the program used for compiling C source files: +ifdef C_COMPILER + CC := $(C_COMPILER) +else + CC := gcc +endif + +# Define the command-line options when compiling C files: +CFLAGS ?= \ + -std=c99 \ + -O3 \ + -Wall \ + -pedantic + +# Determine whether to generate position independent code ([1][1], [2][2]). +# +# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options +# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option +ifeq ($(OS), WINNT) + fPIC ?= +else + fPIC ?= -fPIC +endif + +# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): +INCLUDE ?= + +# List of source files: +SOURCE_FILES ?= + +# List of libraries (e.g., `-lopenblas -lpthread`): +LIBRARIES ?= + +# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): +LIBPATH ?= + +# List of C targets: +c_targets := example.out + # RULES # #/ -# Removes generated files for building an add-on. +# Compiles source files. +# +# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`) +# @param {string} [CFLAGS] - C compiler options +# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`) +# @param {string} [SOURCE_FILES] - list of source files +# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) +# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas -lpthread`) +# +# @example +# make +# +# @example +# make all +#/ +all: $(c_targets) + +.PHONY: all + +#/ +# Compiles C source files. +# +# @private +# @param {string} CC - C compiler (e.g., `gcc`) +# @param {string} CFLAGS - C compiler options +# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar`) +# @param {string} SOURCE_FILES - list of source files +# @param {string} LIBPATH - list of library paths (e.g., `-L /foo/bar`) +# @param {string} LIBRARIES - list of libraries (e.g., `-lopenblas`) +#/ +$(c_targets): %.out: %.c + $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) + +#/ +# Runs compiled examples. # # @example -# make clean-addon +# make run #/ -clean-addon: - $(QUIET) -rm -f *.o *.node +run: $(c_targets) + $(QUIET) ./$< -.PHONY: clean-addon +.PHONY: run #/ # Removes generated files. @@ -65,6 +140,7 @@ clean-addon: # @example # make clean #/ -clean: clean-addon +clean: + $(QUIET) -rm -f *.o *.out .PHONY: clean diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/examples/c/example.c b/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/examples/c/example.c index 1c0064664f04..bc608fbe43a4 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/examples/c/example.c +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/examples/c/example.c @@ -16,27 +16,32 @@ * limitations under the License. */ -#include "stdlib/stats/base/dists/invgamma/pmf.h" +#include "stdlib/stats/base/dists/binomial/pmf.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) ); } +static int32_t random_int( const int32_t min, const int32_t max ) { + return min + (int32_t)( random_uniform( 0.0, 1.0 ) * ( max - min + 1 ) ); +} + int main( void ) { - double n; + int32_t n; double p; - double x; + double x; double y; int i; for ( i = 0; i < 25; i++ ) { - n = random_uniform( 0.0, 20.0 ); - x = random_uniform( 0.0, 20.0 ); - p = random_uniform( 0.0, 1.0 ); + n = random_int( 1, 20 ); + x = random_uniform( 0.0, (double)n ); + p = random_uniform( 0.0, 1.0 ); y = stdlib_base_dists_binomial_pmf( x, n, p ); - printf( "x: %lf, n: %lf, p: %lf pmf( x, n, p ): %lf\n", x, n, p, y ); + printf( "x: %.4f, n: %d, p: %.4f, P(X = x;n,p): %.4f\n", x, n, p, y ); } } diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/include/stdlib/stats/base/dists/binomial/pmf.h b/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/include/stdlib/stats/base/dists/binomial/pmf.h index 1ded56538c23..4180f730b468 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/include/stdlib/stats/base/dists/binomial/pmf.h +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/include/stdlib/stats/base/dists/binomial/pmf.h @@ -19,6 +19,8 @@ #ifndef STDLIB_STATS_BASE_DISTS_BINOMIAL_PMF_H #define STDLIB_STATS_BASE_DISTS_BINOMIAL_PMF_H +#include + /* * If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. */ @@ -27,9 +29,9 @@ extern "C" { #endif /** -* Returns the variance of an inverse gamma distribution. +* Evaluates the probability mass function (PMF) for a binomial distribution. */ -double stdlib_base_dists_binominal_pmf( const double x, const double n, const double p ); +double stdlib_base_dists_binomial_pmf( const double x, const int32_t n, const double p ); #ifdef __cplusplus } diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/lib/index.js b/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/lib/index.js index bf3af57b7778..e8cd568b9327 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/lib/index.js +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/lib/index.js @@ -36,7 +36,7 @@ * // returns ~0.201 * * y = pmf( 0.0, 10, 0.4 ); -* // returns ~0.06 +* // returns ~0.006 * * @example * var factory = require( '@stdlib/stats/base/dists/binomial/pmf' ).factory; diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/lib/native.js b/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/lib/native.js index a6c51c7a2bd7..4d535cf08cb9 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/lib/native.js +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/lib/native.js @@ -26,7 +26,7 @@ var addon = require( './../src/addon.node' ); // MAIN // /** -* Returns the variance of an inverse gamma distribution. +* Evaluates the probability mass function (PMF) for a binomial distribution with number of trials `n` and success probability `p` at a value `x`. * * @private * @param {number} x - input value @@ -55,28 +55,8 @@ var addon = require( './../src/addon.node' ); * // returns NaN * * @example -* var y = pmf( 0.0, NaN, 0.5 ); -* // returns NaN -* -* @example * var y = pmf( 0.0, 20, NaN ); * // returns NaN -* -* @example -* var y = pmf( 2.0, 1.5, 0.5 ); -* // returns NaN -* -* @example -* var y = pmf( 2.0, -2.0, 0.5 ); -* // returns NaN -* -* @example -* var y = pmf( 2.0, 20, -1.0 ); -* // returns NaN -* -* @example -* var y = pmf( 2.0, 20, 1.5 ); -* // returns NaN */ function pmf( x, n, p ) { return addon( x, n, p ); diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/manifest.json b/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/manifest.json index c4c7aaad6528..84eb39532c6c 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/manifest.json +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/manifest.json @@ -1,76 +1,91 @@ { - "options": { - "task": "build", - "wasm": false + "options": { + "task": "build", + "wasm": false + }, + "fields": [ + { + "field": "src", + "resolve": true, + "relative": true }, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "task": "build", - "wasm": false, - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/math/base/special/binomcoefln", - "@stdlib/math/base/assert/is-nan", - "@stdlib/math/base/special/exp", - "@stdlib/math/base/special/log1p", - "@stdlib/math/base/special/ln", - "@stdlib/math/base/assert/is-nonnegative-integer" - ] - }, - { - "task": "benchmark", - "wasm": false, - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [] - }, - { - "task": "examples", - "wasm": false, - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [] - } - ] + { + "field": "include", + "resolve": true, + "relative": true + }, + { + "field": "libraries", + "resolve": false, + "relative": false + }, + { + "field": "libpath", + "resolve": true, + "relative": false + } + ], + "confs": [ + { + "task": "build", + "wasm": false, + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/math/base/napi/ternary", + "@stdlib/math/base/special/binomcoefln", + "@stdlib/math/base/assert/is-nan", + "@stdlib/math/base/special/exp", + "@stdlib/math/base/special/log1p", + "@stdlib/math/base/special/ln", + "@stdlib/math/base/assert/is-nonnegative-integer" + ] + }, + { + "task": "benchmark", + "wasm": false, + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/math/base/special/binomcoefln", + "@stdlib/math/base/assert/is-nan", + "@stdlib/math/base/special/exp", + "@stdlib/math/base/special/log1p", + "@stdlib/math/base/special/ln", + "@stdlib/math/base/assert/is-nonnegative-integer" + ] + }, + { + "task": "examples", + "wasm": false, + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/math/base/special/binomcoefln", + "@stdlib/math/base/assert/is-nan", + "@stdlib/math/base/special/exp", + "@stdlib/math/base/special/log1p", + "@stdlib/math/base/special/ln", + "@stdlib/math/base/assert/is-nonnegative-integer" + ] + } + ] } diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/src/addon.c b/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/src/addon.c index 79bc8359d091..da631d202ca5 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/src/addon.c +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/src/addon.c @@ -17,93 +17,6 @@ */ #include "stdlib/stats/base/dists/binomial/pmf.h" -#include -#include -#include +#include "stdlib/math/base/napi/ternary.h" -/** -* -* @private -* @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 ) { - napi_status status; - - // Get callback arguments: - size_t argc = 3; - napi_value argv[ 3 ]; - status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL ); - assert( status == napi_ok ); - - // Check whether we were provided the correct number of arguments: - if ( argc < 3 ) { - status = napi_throw_error( env, NULL, "invalid invocation. Insufficient arguments." ); - assert( status == napi_ok ); - return NULL; - } - if ( argc > 3 ) { - status = napi_throw_error( env, NULL, "invalid invocation. Too many arguments." ); - assert( status == napi_ok ); - return NULL; - } - - napi_valuetype vtype0; - status = napi_typeof( env, argv[ 0 ], &vtype0 ); - assert( status == napi_ok ); - if ( vtype1 != 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; - } - - double value0; - status = napi_get_value_double( env, argv[ 0 ], &value0); - assert( status == napi_ok ); - - double value1; - status = napi_get_value_double( env, argv[ 1 ], &value1 ); - assert( status == napi_ok ); - - double value2; - status = napi_get_value_double( env, argv[ 2 ], &value2 ); - assert( status == napi_ok ); - - stdlib_base_minmax( value0, value1, value2 ); - - return NULL; -} - -/** -* Initializes a Node-API module. -* -* @private -* @param env environment under which the function is invoked -* @param exports exports object -* @return main export -*/ -static napi_value init( napi_env env, napi_value exports ) { - napi_value fcn; - napi_status status = napi_create_function( env, "exports", NAPI_AUTO_LENGTH, addon, NULL, &fcn ); - assert( status == napi_ok ); - return fcn; -} - -NAPI_MODULE( NODE_GYP_MODULE_NAME, init ) +STDLIB_MATH_BASE_NAPI_MODULE_DID_D( stdlib_base_dists_binomial_pmf ) diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/src/main.c b/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/src/main.c index 05abdd6562ae..b4830447f55c 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/src/main.c +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/src/main.c @@ -16,13 +16,14 @@ * limitations under the License. */ -#include "stdlib/stats/base/dists/invgamma/pmf.h" +#include "stdlib/stats/base/dists/binomial/pmf.h" #include "stdlib/math/base/assert/is_nan.h" #include "stdlib/math/base/assert/is_nonnegative_integer.h" #include "stdlib/math/base/special/binomcoefln.h" #include "stdlib/math/base/special/ln.h" #include "stdlib/math/base/special/log1p.h" #include "stdlib/math/base/special/exp.h" +#include /** * Evaluates the probability mass function (PMF) for a binomial distribution with number of trials `n` and success probability `p` at a value `x`. @@ -33,34 +34,34 @@ * @return evaluated PMF * * @example -* double y = stdlib_base_binomial_pmf( 3.0, 20, 0.2 ); +* double y = stdlib_base_dists_binomial_pmf( 3.0, 20, 0.2 ); * // returns ~0.205 */ -double stdlib_base_dists_binomial_pmf( const double x, const double n, const double p ) { - double lnl; +double stdlib_base_dists_binomial_pmf( const double x, const int32_t n, const double p ) { + double lnl; + if ( - stdlib_base_is_nan( x ) || - stdlib_base_is_nan( n ) || - stdlib_base_is_nan( p ) || - p < 0.0 || - p > 1.0 || - stdlib_base_is_nonnegative_integer( n ) - ) { + stdlib_base_is_nan( x ) || + stdlib_base_is_nan( p ) || + n < 0 || + p < 0.0 || + p > 1.0 + ) { return 0.0 / 0.0; // NaN } - if ( stdlib_base_is_nonnegative_integer( x ) ) { - if ( x > n ) { - return 0.0; - } - if ( p === 0.0 ) { - return ( x === 0 ) ? 1.0 : 0.0; - } - if ( p === 0.0 ) { - return ( x === n ) ? 1.0 : 0.0; - } - lnl = stdlib_base_binomcoefln( n, x ); - lnl += ( x * stdlib_base_ln( p ) ) + ( ( n - x ) * stdlib_base_log1p( -p ) ); - return stdlib_base_exp( lnl ); - } - return 0.0; + if ( !stdlib_base_is_nonnegative_integer( x ) ) { + return 0.0; + } + if ( x > n ) { + return 0.0; + } + if ( p == 0.0 ) { + return ( x == 0.0 ) ? 1.0 : 0.0; + } + if ( p == 1.0 ) { + return ( x == n ) ? 1.0 : 0.0; + } + lnl = stdlib_base_binomcoefln( n, x ); + lnl += ( x * stdlib_base_ln( p ) ) + ( ( n - x ) * stdlib_base_log1p( -p ) ); + return stdlib_base_exp( lnl ); } diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/test/test.native.js b/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/test/test.native.js index 4b22a748d33b..c82df8b8914a 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/test/test.native.js +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/test/test.native.js @@ -54,11 +54,9 @@ tape( 'main export is a function', opts, function test( t ) { t.end(); }); -tape( 'if provided `NaN` for any parameter, the function returns `NaN`', opts, function test( t ) { +tape( 'if provided `NaN` for `x` or `p`, the function returns `NaN`', opts, function test( t ) { var y = pmf( NaN, 20, 0.5 ); t.equal( isnan( y ), true, 'returns NaN' ); - y = pmf( 0.0, NaN, 0.5 ); - t.equal( isnan( y ), true, 'returns NaN' ); y = pmf( 0.0, 20, NaN ); t.equal( isnan( y ), true, 'returns NaN' ); t.end(); @@ -96,24 +94,6 @@ tape( 'if provided a non-integer for `x` and a valid `n` and `p`, the function r t.end(); }); -tape( 'if provided `n` which is not a nonnegative integer, the function returns `NaN`', opts, function test( t ) { - var y; - - y = pmf( 2.0, 1.5, 0.5 ); - t.equal( isnan( y ), true, 'returns NaN' ); - - y = pmf( 0.0, -1.0, 0.5 ); - t.equal( isnan( y ), true, 'returns NaN' ); - - y = pmf( 2.0, NINF, 0.5 ); - t.equal( isnan( y ), true, 'returns NaN' ); - - y = pmf( 2.0, PINF, 0.5 ); - t.equal( isnan( y ), true, 'returns NaN' ); - - t.end(); -}); - tape( 'if provided a success probability `p` outside of `[0,1]`, the function returns `NaN`', opts, function test( t ) { var y; @@ -292,7 +272,7 @@ tape( 'the function evaluates the pmf for `x` given small `n` and `p`', opts, fu t.equal( y, expected[ i ], 'x: ' + x[ i ] + ', n: ' + n[ i ] + ', p: ' + p[ i ] + ', y: ' + y + ', expected: ' + expected[ i ] ); } else { delta = abs( y - expected[ i ] ); - tol = 100.0 * EPS * abs( expected[ i ] ); + tol = 120.0 * EPS * abs( expected[ i ] ); t.strictEqual( delta <= tol, true, 'within tolerance. x: ' + x[ i ] + '. n: ' + n[ i ] + '. p: ' + p[ i ] + '. y: ' + y + '. Expected: ' + expected[ i ] + '. tol: ' + tol + '. delta: ' + delta + '.' ); } } From bc2b096d56782893b0733f6f252c43dc618fec6b Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Thu, 24 Jul 2025 22:00:46 -0500 Subject: [PATCH 4/4] chore: fix Makefile --- 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: na - 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 --- --- .../dists/binomial/pmf/benchmark/c/Makefile | 32 +++++++++++++++---- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/benchmark/c/Makefile b/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/benchmark/c/Makefile index 85a01e54fdaf..a4bd7b38fd74 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/benchmark/c/Makefile +++ b/lib/node_modules/@stdlib/stats/base/dists/binomial/pmf/benchmark/c/Makefile @@ -69,6 +69,18 @@ else fPIC ?= -fPIC endif +# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): +INCLUDE ?= + +# List of source files: +SOURCE_FILES ?= + +# List of libraries (e.g., `-lopenblas -lpthread`): +LIBRARIES ?= + +# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): +LIBPATH ?= + # List of C targets: c_targets := benchmark.out @@ -76,11 +88,15 @@ c_targets := benchmark.out # RULES # #/ -# Compiles C source files. +# Compiles source files. # # @param {string} [C_COMPILER] - C compiler (e.g., `gcc`) # @param {string} [CFLAGS] - C compiler options -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code (e.g., `-fPIC`) +# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`) +# @param {string} [SOURCE_FILES] - list of source files +# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) +# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas -lpthread`) # # @example # make @@ -96,12 +112,16 @@ all: $(c_targets) # Compiles C source files. # # @private -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code +# @param {string} CC - C compiler (e.g., `gcc`) +# @param {string} CFLAGS - C compiler options +# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar`) +# @param {string} SOURCE_FILES - list of source files +# @param {string} LIBPATH - list of library paths (e.g., `-L /foo/bar`) +# @param {string} LIBRARIES - list of libraries (e.g., `-lopenblas`) #/ $(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) -o $@ $< -lm + $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) #/ # Runs compiled benchmarks.