diff options
| author | Ethan Werner <mail@ethanwerner.com> | 2026-07-01 09:42:39 +0400 |
|---|---|---|
| committer | Ethan Werner <mail@ethanwerner.com> | 2026-07-01 09:42:39 +0400 |
| commit | 07e18043145f3d91c8ea7ec3ad41b3c905585277 (patch) | |
| tree | 2bdeae46973f4bdfce263679a4008ac768011f28 /lib/binomial.c | |
initial commit: ts time series analysis library v0.1.0
- 354 financial and mathematical time-series indicators
- Uniform C11 API with consistent function signatures
- 349 passing tests with -Werror
- Static and shared library builds
- pkg-config integration
- Python bindings
- Fuzzing and sanitizer support
- Security hardening flags
Diffstat (limited to 'lib/binomial.c')
| -rw-r--r-- | lib/binomial.c | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/lib/binomial.c b/lib/binomial.c new file mode 100644 index 0000000..9b50698 --- /dev/null +++ b/lib/binomial.c @@ -0,0 +1,58 @@ +// Binomial Coefficient +// +// binomial - C(n, k) +// +// Number of combinations: C(n, k) = n! / (k! * (n-k)!) +// +// Inputs: 2 (n, k) +// Outputs: 1 (coefficient) +// Constants: 0 + +#include <math.h> +#include "ts.h" + + +ts_parameter_t const ts_binomial_p = { .n_inputs = 2, .n_outputs = 1, .n_constants = 0, .params_size = 0 }; + +uint_t ts_binomial_offset( ts_constant_t const *c ) +{ + ( void )c; + return ( 0 ); +} + +int ts_binomial( + fp_t * const *y, uint_t y_n, + fp_t const * const *x, uint_t x_n, + uint_t n, ts_constant_t const *c +) +{ + TS_PROLOGUE( ts_binomial_p ); + (void)c; + /* C(n, k) = n! / (k! * (n-k)!) */ + for (uint_t i = 0; i < n; ++i) + { + fp_t n_val = floor(x[0][i]); + fp_t k_val = floor(x[1][i]); + + if (k_val > n_val || k_val < 0 || n_val < 0) + { + y[0][i] = 0; + continue; + } + + /* Use symmetry: C(n,k) = C(n, n-k) */ + if (k_val > n_val - k_val) + k_val = n_val - k_val; + + fp_t result = 1; + for (uint_t j = 0; j < (uint_t)k_val; ++j) + { + result = result * (n_val - j) / (j + 1); + } + y[0][i] = result; + } + return 0; +} + + + |