All Classes Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Math module

Provides utilities to help with mathematics. More...

Modules

 Mathematical Constants
 Defines mathematical constants commonly in use.
 

Classes

struct  gears::math::uintx< Bits, Digit, Digits >
 Multi-precision unsigned integer. More...
 

Functions

template<typename T >
gears::math::fibonacci (T number)
 Calculates the nth fibonacci number. More...
 
template<typename T >
constexpr T gears::math::factorial (const T &number)
 Calculates the Nth factorial. More...
 
template<typename T >
constexpr T gears::math::gcd (const T &x, const T &y)
 Calculates the greatest common divisor. More...
 
template<typename T >
gears::math::mod_pow (T base, T exponent, const T &modulus)
 Calculates the modular exponentiation. More...
 
template<typename T >
gears::math::sum_of_divisors (const T &number) noexcept
 Calculates the sum of divisors function. More...
 
template<typename T >
bool gears::math::is_prime (const T &number) noexcept
 Checks if a number is prime. More...
 
template<typename T >
constexpr T gears::math::abs (T t)
 Calculates the absolute value of a number. More...
 
template<typename Container , typename Value = typename Container::value_type>
void gears::math::primes (Value limit, Container &cont)
 Generates primes using a modified Sieve of Eratosthenes. More...
 
template<typename Container , typename Integral >
void gears::math::pythagorean_triples (Integral limit, Container &cont)
 Generates Pythagorean triples. More...
 
template<typename T , typename U , typename... Args>
constexpr std::common_type< T,
U, Args...>::type 
gears::math::min (T &&t, U &&u, Args &&...args)
 Calculates the minimum value of a list of numbers. More...
 
template<typename T , typename U , typename... Args>
constexpr std::common_type< T,
U, Args...>::type 
gears::math::max (T &&t, U &&u, Args &&...args)
 Calculates the maximum value of a list of numbers. More...
 

Detailed Description

This module provides an implementation of common mathematical algorithms and an infinite precision integer.

Example usage:

#include <gears/math.hpp>
#include <iostream>
namespace math = gears::math;
// needed to use _x user defined literal
using namespace gears::math::operators;
int main() {
auto lhs = 9384721983748274812387232191827321_x;
auto rhs = 2937189237182973865769137697362736_x;
std::cout << (lhs * rhs) << '\n';
std::cout << math::factorial(10) << '\n';
}

Output

27564704404619880556797005156529364384954680388538087951954412110256
3628800

Function Documentation

template<typename T >
constexpr T gears::math::abs ( t)

Calculates the absolute value of a number. This uses the incredibly naive implementation and should only be used for its constexpr status, which the function in <cmath> lacks.

Parameters
tThe number to make positive.
Returns
The absolute value of a number.

Definition at line 191 of file algorithm.hpp.

template<typename T >
constexpr T gears::math::factorial ( const T &  number)

Calculates the Nth factorial. The internal implementation uses the naive recursion way of doing it in order to preserve C++11 constexpr status.

Parameters
numberWhich factorial to calculate.
Returns
Nth factorial number.

Definition at line 69 of file algorithm.hpp.

template<typename T >
T gears::math::fibonacci ( number)
inline

Calculates the nth fibonacci number. The internal implementation does not use the naive recursion.

Parameters
numberWhich fibonacci number to calculate.
Returns
Nth fibonacci number.

Definition at line 40 of file algorithm.hpp.

template<typename T >
constexpr T gears::math::gcd ( const T &  x,
const T &  y 
)

Calculates the greatest common divisor between two numbers. Uses Euclid's algorithm to compute it.

Parameters
xLeft hand side.
yRight hand side.
Returns
The greatest common divisor between the numbers.

Definition at line 84 of file algorithm.hpp.

template<typename T >
bool gears::math::is_prime ( const T &  number)
inlinenoexcept

Checks if a number is prime using the AKS primality test. This function is pretty slow for numbers that are relatively large.

Parameters
numberNumber to test for primality.
Returns
true is number is prime, false otherwise.

Definition at line 147 of file algorithm.hpp.

template<typename T , typename U , typename... Args>
constexpr std::common_type<T, U, Args...>::type gears::math::max ( T &&  t,
U &&  u,
Args &&...  args 
)

Calculates the maximum value of a list of numbers. It is recommended to not mix different types when using this function as it will promote types and make it unsafe.

Parameters
tFirst type to compare against.
uSecond type to compare against.
argsOther types to compare against.
Returns
The maximum value of the list of numbers.

Definition at line 248 of file algorithm.hpp.

template<typename T , typename U , typename... Args>
constexpr std::common_type<T, U, Args...>::type gears::math::min ( T &&  t,
U &&  u,
Args &&...  args 
)

Calculates the minimum value of a list of numbers. It is recommended to not mix different types when using this function as it will promote types and make it unsafe.

Parameters
tFirst type to compare against.
uSecond type to compare against.
argsOther types to compare against.
Returns
The minimum value of the list of numbers.

Definition at line 219 of file algorithm.hpp.

template<typename T >
T gears::math::mod_pow ( base,
exponent,
const T &  modulus 
)
inline

Calculates the modular exponentiation. That is, it computes essentially (base ** exponent) % modulus where ** denotes exponentiation. All numbers provided must be positive.

Parameters
baseThe base number of the formula.
exponentThe exponent number of the formula.
modulusThe modulo number of the formula.
Returns
(base ** exponent) % modulus.

Definition at line 102 of file algorithm.hpp.

template<typename Container , typename Value = typename Container::value_type>
void gears::math::primes ( Value  limit,
Container &  cont 
)
inline

Generates primes using a modified Sieve of Eratosthenes. The function will generate primes up to the limit provided. e.g. if limit is 100, then the last prime generated would be 97. The container must have an integral value_type for best results. Currently this function only works on containers that provide the push_back function. This might be changed in the future. The first two primes (2 and 3) are always provided regardless of the limit given.

Parameters
limitThe upper limit of primes to generate.
contThe container to insert the primes to.

Definition at line 47 of file generator.hpp.

template<typename Container , typename Integral >
void gears::math::pythagorean_triples ( Integral  limit,
Container &  cont 
)
inline

Generates Pythagorean triples and puts the results into a container provided. The results are added to the container as if calling:

cont.emplace_back(x, y, z)
Parameters
limitThe upper limit of Pythagorean triples to generate.
contThe container to emplace the Pythagorean triples.

Definition at line 92 of file generator.hpp.

template<typename T >
T gears::math::sum_of_divisors ( const T &  number)
inlinenoexcept

Calculates the sum of divisors of a given integer. For example, the divisors for the number 12 are 1, 2, 3, 4, 6, and 12. The sum of these numbers is 28. So sum_of_divisors(12) would return 28.

Parameters
numberThe number to take the sum of divisors for.
Returns
The sum of the number's divisors.

Definition at line 128 of file algorithm.hpp.