LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
moment_central_from_raw.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_CENTRAL_FROM_RAW_H
6#define LINE_API_MOMENT_MOMENT_CENTRAL_FROM_RAW_H
7
8/**
9 * @file
10 * @ingroup api_moment
11 * Central moments from raw moments.
12 *
13 * Templated port of matlab/src/api/moment/moment_central_from_raw.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/** mc_i = sum_k (-1)^(i-k) C(i,k) m_k m1^(i-k), with m1 = m[1]. */
31template <class T>
32std::vector<T> moment_central_from_raw(const std::vector<T>& m) {
33 if (m.size() < 2)
34 throw InputError(
35 "moment_central_from_raw: the mean m1 is required, so m must have at least 2 entries");
36 const std::size_t n = m.size();
37 const T m1 = m[1];
38 std::vector<T> mc(n, num_traits<T>::from_int(0));
39 for (std::size_t i = 0; i < n; ++i)
40 for (std::size_t k = 0; k <= i; ++k) {
41 const T term = num_nck<T>(static_cast<int>(i), static_cast<int>(k)) * m[k] *
42 num_pow_int(m1, static_cast<unsigned>(i - k));
43 mc[i] += ((i - k) % 2 == 0) ? term : -term;
44 }
45 return mc;
46}
47
48} // namespace moment
49} // namespace line
50
51#endif
InputError(const std::string &what)
Definition error.h:39
The exception types the port throws.
Dense matrix and non-owning view.
std::vector< T > moment_central_from_raw(const std::vector< T > &m)
mc_i = sum_k (-1)^(i-k) C(i,k) m_k m1^(i-k), with m1 = m[1].
T num_pow_int(const T &base, unsigned e)
Integer power, valid in any field (no transcendental requirement).
Definition number.h:192
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.