7.2. formula — Miscellaneous mathematical formula

This module contains miscellaneous mathematical formula is various areas of mathematics. Most of them exist because they are needed in the implementations of other POLDER modules.

7.2.1. Basic functions

int math::sign(Number x)

Signum function: \(\mathrm{sign}(x) = \begin{cases} -1 & \text{if } x < 0, \\ 0 & \text{if } x = 0, \\ 1 & \text{if } x > 0. \end{cases}\).

CommonType math::sum(T first, U second, Rest... rest)

Return the sum of two or more arguments. If the arguments have different types, the result type will be the common type of the different arguments, std::common_type_t<T, U, Rest...>.

CommonType math::mean(Numbers... args)

Return the mean of two or more arguments. If the arguments have different types, the result type will be the common type of the different arguments, std::common_type_t<Numbers...>.

Number math::sqr(Number x)

Return the square of x: \(x*x\).

Number math::clamp(Number x, Number min, Number max)

Return x, clamped between min and max.

7.2.3. Angle conversions

Float math::degrees(Float x)

Converts angle x from radians to degrees.

Float math::radians(Float x)

Converts angle x from degrees to radians.

7.2.4. Trigonometric functions

Float math::sinc(Float x)

Unnormalized cardinal since of x: \(\mathrm{sinc}(x) = \frac{\sin(x)}{x}\).

Float math::normalized_sinc(Float x)

Normalized cardinal since of x: \(\mathrm{sinc}(x) = \frac{\sin(\pi x)}{\pi x}\).

7.2.5. Miscellaneous functions

std::array<std::complex<Float, 2u>> math::quadratic(Float a, Float b, Float c)

Return the results x solutions of the quadratic equation \(ax^2 + bx +c\).

\(x = \frac{-b\pm\sqrt{b^2-4ac\ }}{2a}\).

constexpr bool is_close(T lhs, T rhs)

This function uses a mathematical formula to check whether two values are close from each other. It returns whether the following relation holds:

\(|lhs-rhs| \le \epsilon * max(|lhs|, |rhs|)\)

Where \(\epsilon\) is std::numeric_limits<T>::epsilon().

7.2.6. Compile-time functions

Some of the functions also have equivalents in the namespace math::meta that can be executed at compile time:

  • math::meta::sign
  • math::meta::sum
  • math::meta::mean
  • math::meta::sqr
  • math::meta::clamp
  • math::meta::is_even
  • math::meta::is_odd
  • math::meta::is_prime
  • math::meta::fibonacci
  • math::meta::gcd
  • math::meta::lcm
  • math::meta::modpow
  • math::meta::degrees
  • math::meta::radians
  • math::meta::is_close