From f40c20c4def7b56eedacb3c46bc418fc9ae198da Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Fri, 28 Aug 2026 00:14:10 -0500 Subject: [PATCH] feat: add C implementation for `stats/base/dists/negative-binomial/logpmf` PR-URL: https://github.com/stdlib-js/stdlib/pull/10132 Co-authored-by: nirmaljb Co-Authored-By: Claude Fable 5 --- .../dists/negative-binomial/logpmf/README.md | 111 +++++++++- .../logpmf/benchmark/benchmark.js | 3 +- .../logpmf/benchmark/benchmark.native.js | 72 ++++++ .../logpmf/benchmark/c/Makefile | 146 +++++++++++++ .../logpmf/benchmark/c/benchmark.c | 145 +++++++++++++ .../negative-binomial/logpmf/binding.gyp | 170 +++++++++++++++ .../logpmf/examples/c/Makefile | 146 +++++++++++++ .../logpmf/examples/c/example.c | 44 ++++ .../negative-binomial/logpmf/include.gypi | 53 +++++ .../base/dists/negative-binomial/logpmf.h | 38 ++++ .../negative-binomial/logpmf/lib/native.js | 96 ++++++++ .../negative-binomial/logpmf/manifest.json | 90 ++++++++ .../negative-binomial/logpmf/package.json | 3 + .../negative-binomial/logpmf/src/Makefile | 70 ++++++ .../negative-binomial/logpmf/src/addon.c | 22 ++ .../dists/negative-binomial/logpmf/src/main.c | 58 +++++ .../logpmf/test/test.factory.js | 10 +- .../test/{test.logpmf.js => test.main.js} | 10 +- .../logpmf/test/test.native.js | 205 ++++++++++++++++++ 19 files changed, 1479 insertions(+), 13 deletions(-) create mode 100644 lib/node_modules/@stdlib/stats/base/dists/negative-binomial/logpmf/benchmark/benchmark.native.js create mode 100644 lib/node_modules/@stdlib/stats/base/dists/negative-binomial/logpmf/benchmark/c/Makefile create mode 100644 lib/node_modules/@stdlib/stats/base/dists/negative-binomial/logpmf/benchmark/c/benchmark.c create mode 100644 lib/node_modules/@stdlib/stats/base/dists/negative-binomial/logpmf/binding.gyp create mode 100644 lib/node_modules/@stdlib/stats/base/dists/negative-binomial/logpmf/examples/c/Makefile create mode 100644 lib/node_modules/@stdlib/stats/base/dists/negative-binomial/logpmf/examples/c/example.c create mode 100644 lib/node_modules/@stdlib/stats/base/dists/negative-binomial/logpmf/include.gypi create mode 100644 lib/node_modules/@stdlib/stats/base/dists/negative-binomial/logpmf/include/stdlib/stats/base/dists/negative-binomial/logpmf.h create mode 100644 lib/node_modules/@stdlib/stats/base/dists/negative-binomial/logpmf/lib/native.js create mode 100644 lib/node_modules/@stdlib/stats/base/dists/negative-binomial/logpmf/manifest.json create mode 100644 lib/node_modules/@stdlib/stats/base/dists/negative-binomial/logpmf/src/Makefile create mode 100644 lib/node_modules/@stdlib/stats/base/dists/negative-binomial/logpmf/src/addon.c create mode 100644 lib/node_modules/@stdlib/stats/base/dists/negative-binomial/logpmf/src/main.c rename lib/node_modules/@stdlib/stats/base/dists/negative-binomial/logpmf/test/{test.logpmf.js => test.main.js} (90%) create mode 100644 lib/node_modules/@stdlib/stats/base/dists/negative-binomial/logpmf/test/test.native.js diff --git a/lib/node_modules/@stdlib/stats/base/dists/negative-binomial/logpmf/README.md b/lib/node_modules/@stdlib/stats/base/dists/negative-binomial/logpmf/README.md index 0143acf5f34a..0fdf34540615 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/negative-binomial/logpmf/README.md +++ b/lib/node_modules/@stdlib/stats/base/dists/negative-binomial/logpmf/README.md @@ -2,7 +2,7 @@ @license Apache-2.0 -Copyright (c) 2018 The Stdlib Authors. +Copyright (c) 2026 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. @@ -157,6 +157,115 @@ logEachMap( 'x: %d, r: %0.4f, p: %0.4f, ln(P(X=x;r,p)): %0.4f', x, r, p, logpmf + + +* * * + +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### Usage + +```c +#include "stdlib/stats/base/dists/negative-binomial/logpmf.h" +``` + +#### stdlib_base_dists_negative_binomial_logpmf( x, r, p ) + +Evaluates the natural logarithm of the probability mass function (PMF) for a negative binomial distribution with number of successes until experiment is stopped `r` and success probability `p`. + +```c +double out = stdlib_base_dists_negative_binomial_logpmf( 5.0, 20.0, 0.8 ); +// returns ~-1.853 +``` + +The function accepts the following arguments: + +- **x**: `[in] double` input value. +- **r**: `[in] double` number of successes until experiment is stopped. +- **p**: `[in] double` success probability. + +```c +double stdlib_base_dists_negative_binomial_logpmf( const double x, const double r, const double p ); +``` + +
+ + + + + +
+ +
+ + + + + +
+ +### Examples + +```c +#include "stdlib/stats/base/dists/negative-binomial/logpmf.h" +#include "stdlib/math/base/special/ceil.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 r; + double p; + double x; + double y; + int i; + + for ( i = 0; i < 10; i++ ) { + x = stdlib_base_ceil( random_uniform( 0.0, 30.0 ) ); + r = random_uniform( 0.0, 50.0 ); + p = random_uniform( 0.0, 1.0 ); + y = stdlib_base_dists_negative_binomial_logpmf( x, r, p ); + printf( "x: %lf, r: %lf, p: %lf, ln(P(X=x;r,p)): %lf\n", x, r, p, y ); + } + + return 0; +} +``` + +
+ + + +
+ + + + + +
+ +
+ + +