LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
moment_binotrans.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_BINOTRANS_H
6#define LINE_API_MOMENT_MOMENT_BINOTRANS_H
7
8/**
9 * @file
10 * @ingroup api_moment
11 * Binomial transform with alternating signs.
12 *
13 * Templated port of matlab/src/api/moment/moment_binotrans.m.
14 *
15 * Applied to a moment sequence m_i = E[X^i] it returns the moments of the unit
16 * DOWNSHIFT, y_i = E[(X-1)^i]. It is NOT an involution -- an easy thing to
17 * assume from the alternating signs, and wrong: its inverse is
18 * moment_binotransinv, the unsigned transform.
19 *
20 * Every operation is integer or rational, so the exact instantiation returns
21 * the transform with no rounding at all. That matters more here than almost anywhere else in the API:
22 * the alternating binomial sums of the moment conversions cancel
23 * catastrophically in double arithmetic once the order grows.
24 */
25
26#include <vector>
27
28#include "line/num/number.h"
29#include "line/util/error.h"
30#include "line/util/matrix.h"
32
33namespace line {
34namespace moment {
35
36/** y_i = sum_k (-1)^(i-k) C(i,k) x_k. */
37template <class T>
38std::vector<T> moment_binotrans(const std::vector<T>& x) {
39 const std::size_t n = x.size();
40 std::vector<T> y(n, num_traits<T>::from_int(0));
41 for (std::size_t i = 0; i < n; ++i)
42 for (std::size_t k = 0; k <= i; ++k) {
43 const T term = num_nck<T>(static_cast<int>(i), static_cast<int>(k)) * x[k];
44 y[i] += ((i - k) % 2 == 0) ? term : -term;
45 }
46 return y;
47}
48
49} // namespace moment
50} // namespace line
51
52#endif
The exception types the port throws.
Dense matrix and non-owning view.
std::vector< T > moment_binotrans(const std::vector< T > &x)
y_i = sum_k (-1)^(i-k) C(i,k) x_k.
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.