LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
moment_binomial_from_negbinomial.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012-2026, QORE Lab, Imperial College London
3 * All rights reserved.
4 */
5#ifndef LINE_API_MOMENT_MOMENT_BINOMIAL_FROM_NEGBINOMIAL_H
6#define LINE_API_MOMENT_MOMENT_BINOMIAL_FROM_NEGBINOMIAL_H
7
8/**
9 * @file
10 * @ingroup api_moment
11 * Binomial moments from negative-binomial moments.
12 *
13 * Templated port of matlab/src/api/moment/moment_binomial_from_negbinomial.m. Every operation is integer
14 * or rational, so the exact instantiation returns the transform with no
15 * rounding at all. That matters more here than almost anywhere else in the API:
16 * the alternating binomial sums of the moment conversions cancel
17 * catastrophically in double arithmetic once the order grows.
18 */
19
20#include <vector>
21
22#include "line/num/number.h"
23#include "line/util/error.h"
24#include "line/util/matrix.h"
26
27namespace line {
28namespace moment {
29
30/** b_0 = 1; b_i = sum_k (-1)^(i-k) C(i-1,k-1) bm_k for i >= 1. */
31template <class T>
32std::vector<T> moment_binomial_from_negbinomial(const std::vector<T>& bm) {
33 const int n = static_cast<int>(bm.size()) - 1;
34 std::vector<T> b(bm.size(), num_traits<T>::from_int(0));
36 for (int i = 1; i <= n; ++i)
37 for (int k = 1; k <= i; ++k) {
38 const T term = num_nck<T>(i - 1, k - 1) * bm[static_cast<std::size_t>(k)];
39 b[static_cast<std::size_t>(i)] += ((i - k) % 2 == 0) ? term : -term;
40 }
41 return b;
42}
43
44} // namespace moment
45} // namespace line
46
47#endif
The exception types the port throws.
Dense matrix and non-owning view.
std::vector< T > moment_binomial_from_negbinomial(const std::vector< T > &bm)
b_0 = 1; b_i = sum_k (-1)^(i-k) C(i-1,k-1) bm_k for i >= 1.
T num_nck(int n, int k)
Binomial coefficient as a value of T, by the Pascal recurrence.
Definition population.h:87
Number-type abstraction for the templated API port.
Population-vector enumeration and combinatorics.