Arithmetic

Header: fiction/utils/math/math_utils.hpp

constexpr double fiction::utils::math::ERROR_MARGIN = 1E-6

The tolerance for comparing floating-point numbers.

inline double fiction::utils::math::cost_function_chi(const std::vector<double> &chis, const std::vector<double> &weights)

Calculates the cost function \( \chi = \sum_{i=1} w_{i} \cdot \chi_{i} \) by summing the product of normalized chi values \( \chi_{i} \) and weights \( w_{i} \).

Parameters:
  • chis – The vector containing the chi values.

  • weights – The vector containing the weights.

Throws:

std::invalid_argument – If the sizes of chis and weights vectors are different.

Returns:

The calculated cost function \( \chi(L) \).

template<typename T>
inline T fiction::utils::math::round_to_n_decimal_places(const T number, const uint64_t n) noexcept

Rounds a number to a specified number of decimal places.

Template Parameters:

T – The type of the number to round.

Parameters:
  • number – The number to round.

  • n – The number of decimal places to round to.

Returns:

The number rounded to n decimal places.

template<std::integral T>
inline T fiction::utils::math::integral_abs(const T n) noexcept

Takes the absolute value of an integral number if it is signed, and otherwise computes the identity. This avoids a compiler warning when taking the absolute value of an unsigned number.

Template Parameters:

T – The type of the number to take the absolute value of. Must be integral.

Parameters:

n – The number to take the absolute value of.

Returns:

|n|.

inline uint64_t fiction::utils::math::binomial_coefficient(const uint64_t n, uint64_t k) noexcept

Calculates the binomial coefficient \(\binom{n}{k}\).

Parameters:
  • n – The total number of items.

  • k – The number of items to choose from n.

Returns:

The binomial coefficient \(\binom{n}{k}\), saturated at uint64_t’s maximum.