Math¶
Combinations¶
Tools to step through combinations of items.
Step through all combinations of size K from a set of N values. For ComboIDs just return the indecies (the specific of the container don’t matter). Other versions will directly wrapper containers.
- Note
This file is part of Empirical, https://github.com/devosoft/Empirical
- Copyright
Copyright (C) Michigan State University, MIT Software license; see doc/LICENSE.md
- Date
2017
-
namespace
emp
If we are in emscripten, make sure to include the header.
-
class
ComboIDs
Public Functions
-
ComboIDs
(size_t in_max, size_t combo_size)
-
~ComboIDs
()
-
size_t
GetComboSize
() const
-
size_t
GetNumCombos
() const
-
size_t &
operator[]
(const size_t index)
-
const size_t &
operator[]
(const size_t index) const
-
bool
NextCombo
()
-
void
ResizeCombos
(size_t new_size)
-
ComboIDs &
operator++
()
-
ComboIDs &
operator++
(int)
-
size_t
size
()
Private Members
-
size_t
max_count
-
size_t
num_combos
Private Static Functions
-
size_t
CountCombos
(size_t max_count, size_t combo_size)
-
-
class
Constants¶
Commonly used constant values.
- Note
This file is part of Empirical, https://github.com/devosoft/Empirical
- Copyright
Copyright (C) Michigan State University, MIT Software license; see doc/LICENSE.md
- Date
2015-2017
- Note
Status: RELEASE
-
namespace
emp
If we are in emscripten, make sure to include the header.
Functions
-
template<typename
T
>
constexpr TMaxValue
() Determine the maximum value for any type.
-
template<typename
T
>
constexpr doubleInterpolateTable
(T &&table, double pos, double tsize) The following function takes a table and a position [0.0, 1.0) and intepolates a value.
Variables
-
constexpr const double
E
= 2.71828 e
-
constexpr const double
PHI
= 1.61803398874 Golden ratio.
-
constexpr const double
PI
= 3.14159265358979 pi
-
constexpr const double
SQRT2
= 1.41421356237310 sqrt(2)
-
constexpr const uint32_t
MAX_BYTE
= 255 (2^8 - 1)
-
constexpr const uint32_t
MAX_2BYTE
= 65535 (2^16 - 1)
-
constexpr const uint32_t
MAX_WORD
= 65535 (2^16 - 1)
-
constexpr const uint32_t
MAX_3BYTE
= 16777215 (2^24 - 1)
-
constexpr const uint32_t
MAX_UINT
= 4294967295 (2^32 - 1)
-
constexpr const uint32_t
MAX_4BYTE
= 4294967295 (2^32 - 1)
-
constexpr const int32_t
MIN_INT
= -2147483648 (- 2^31)
-
constexpr const double
log2_chart_1_2
[] Large table to log base-2 results.
-
constexpr const double
pow2_chart_bits
[] = {1.4142135623730951, 1.1892071150027210, 1.0905077326652577, 1.0442737824274138, 1.0218971486541166, 1.0108892860517005, 1.0054299011128027, 1.0027112750502025, 1.0013547198921082, 1.0006771306930664, 1.0003385080526823, 1.0001692397053021, 1.0000846162726944, 1.0000423072413958, 1.0000211533969647, 1.0000105766425498, 1.0000052883072919, 1.0000026441501502, 1.0000013220742012, 1.0000006610368821, 1.0000003305183864, 1.0000001652591795, 1.0000000826295863, 1.0000000413147923, 1.0000000206573960, 1.0000000103286979, 1.0000000051643489, 1.0000000025821745, 1.0000000012910872, 1.0000000006455436, 1.0000000003227718, 1.0000000001613858} Table to provide results of Pow2 for values of bits (0.1, 0.01, 0.001, etc, in binary)
-
constexpr const double
pow2_chart_0_1
[] Table to provide results of Pow2 from 0 to 1.
-
template<typename
Information Theory Tools¶
Tools to calculate Information Theory metrics.
Info-theory formulas: H(X) = -SUM(X: p[x] log2 p[x]) H(X|Y) = H(XY) - H(Y) I(X:Y) = H(X) - H(X|Y) H2(p) = -p log2(p) - (1-p)log2(1-p) = H({p, 1-p})
- Note
This file is part of Empirical, https://github.com/devosoft/Empirical
- Copyright
Copyright (C) Michigan State University, MIT Software license; see doc/LICENSE.md
- Date
2021.
- Note
Status: ALPHA
Developer notes:
Input may come as WEIGHTS or as ELEMENTS (or both!). ELEMENTS need to be converted to WEIGHTS for calculations.
Want basic info theory functions, as well as tools (for channels, error-correction, compression, etc.)
-
namespace
emp
If we are in emscripten, make sure to include the header.
Functions
-
template<typename
CONTAINER
>
doubleEntropy
(const CONTAINER &weights) Convert a vector of weights to probabilities and return the entropy of the system.
-
template<typename
CONTAINER
, typenameWEIGHT_FUN
>
doubleEntropy
(const CONTAINER &objs, WEIGHT_FUN fun, double total = 0.0) Calculate the entropy in a container of arbitrary objects. Args are a container, a function to extract the weight of each member, and an (optional) total weight.
-
constexpr double
Entropy2
(const double p) Calculate the entropy when their are two possibile states based on one state’s probability.
-
template<typename
Math¶
Useful mathematical functions (that are constexpr when possible.)
- Note
This file is part of Empirical, https://github.com/devosoft/Empirical
- Copyright
Copyright (C) Michigan State University, MIT Software license; see doc/LICENSE.md
- Date
2016-2021.
- Note
Status: BETA (though new functions are added frequently)
-
namespace
emp
If we are in emscripten, make sure to include the header.
Functions
-
constexpr int
Mod
(int in_val, int mod_val) % is actually remainder; Mod is a proper modulus command that handles negative #’s correctly
-
double
Mod
(double in_val, double mod_val) Regular Mod doesn’t work on doubles. Build one that does!
-
template<typename
T
>
intSgn
(T val) Calculate the sign (i.e., +1, -1, or 0) of a value.
-
int
FloorDivide
(int dividend, int divisor) Divide one integer by another, rounding towards minus infinity.
-
int
RoundedDivide
(int dividend, int divisor) Default integer division is truncated, not rounded. Round the division result instead of truncating it. Rounding ties (i.e., result % divisor == 0.5) are rounded up.
-
size_t
RoundedDivide
(size_t dividend, size_t divisor) Default integer division is truncated, not rounded. Round the division result instead of truncating it. Rounding ties (i.e., result % divisor == 0.5) will be rounded up.
-
int
UnbiasedDivide
(int dividend, int divisor, emp::Random &r) Regular integer division is truncated, not rounded. Round the division result instead of truncating it. Rounding ties (i.e., result % divisor == 0.5) are broken by coin toss.
-
size_t
UnbiasedDivide
(size_t dividend, size_t divisor, emp::Random &r) Regular integer division is truncated, not rounded. Round the division result instead of truncating it. Rounding ties (i.e., result % divisor == 0.5) are broken by coin toss.
-
template<typename
TYPE
>
constexpr TYPEToRange
(const TYPE &value, const TYPE &in_min, const TYPE &in_max) Run both min and max on a value to put it into a desired range.
-
template<typename
T
, typename ...Ts
>
constexpr TMin
(T in1, T in2, Ts... extras) Min of multiple elements is solved recursively.
-
template<typename
T
, typename ...Ts
>
constexpr TMax
(T in1, T in2, Ts... extras) Max of multiple elements is solved recursively.
-
template<typename
T
>
constexpr const T &MinRef
(const T &in1) MinRef works like Min, but never copies any inputs; always treats as references. MinRef of only one element returns reference to that element itself!
-
template<typename
T
, typename ...Ts
>
constexpr const T &MinRef
(const T &in1, const T &in2, const Ts&... extras) MinRef of multiple elements returns reference to minimum value.
-
template<typename
T
>
constexpr const T &MaxRef
(const T &in1) MaxRef works like Max, but never copies any inputs; always treats as references. MaxRef of only one element returns reference to that element itself!
-
template<typename
T
, typename ...Ts
>
constexpr const T &MaxRef
(const T &in1, const T &in2, const Ts&... extras) MaxRef of multiple elements returns reference to maximum value.
-
constexpr double
Log2
(double x) Compile-time log base 2 calculator.
-
constexpr double
Log
(double x, double base = 10.0) Compile-time log calculator.
-
constexpr double
Ln
(double x) Compile-time natural log calculator.
-
constexpr double
Log10
(double x) Compile-time log base 10 calculator.
-
constexpr double
Pow2
(double exp) A fast 2^x command.
-
template<typename
TYPE
>
constexpr TYPEIntPow
(TYPE base, TYPE exp) A fast method for calculating exponents for int types.
-
template<typename
T
>
constexpr TPow
(T base, typename internal::identity<T>::type exp) A fast method for calculating exponents on doubles or integral types. Uses if constexpr to work around compiler bug in Emscripten (issue #296).
-
constexpr double
Exp
(double exp) A fast method of calculating e^x.
-
template<typename
TYPE
>
constexpr intIntLog2
(TYPE x) A compile-time int-log calculator (aka, significant bits)
-
template<typename
T
>
constexpr const T &Min
(const T &in1, const T &in2, const T &in3) Return the minimum of three values.
-
template<typename
T
>
const T &Min
(std::initializer_list<const T&> lst) A version of Min that allows a variable number of inputs to be compared.
-
template<typename
T
>
const T &Max
(std::initializer_list<const T&> lst) A version of Max that allows a variable number of inputs to be compared.
-
uint64_t
NextPowerOf2
(uint64_t A) Returns the next power of two (in 64-bits) that is strictly greater than A. Returns zero on overflow.
-
constexpr bool
IsPowerOf2
(const size_t x) Tests if a number is a power of two.
-
constexpr int
Factorial
(int i)
-
bool
Toggle
(bool &in_bool) Toggle an input bool.
-
constexpr bool
AllTrue
() Combine bools to AND them all together.
-
template<typename ...
Ts
>
boolAllTrue
(bool result, Ts... OTHER)
-
constexpr bool
AnyTrue
() Combine bools to OR them all together.
-
template<typename ...
Ts
>
boolAnyTrue
(bool result, Ts... OTHER)
-
constexpr int
Randomness Utilites¶
Helper functions for emp::Random for common random tasks.
- Note
This file is part of Empirical, https://github.com/devosoft/Empirical
- Copyright
Copyright (C) Michigan State University, MIT Software license; see doc/LICENSE.md
- Date
2016-2017
- Note
Status: RELEASE
-
namespace
emp
If we are in emscripten, make sure to include the header.
Functions
-
template<typename
T
>
voidShuffle
(Random &random, emp::vector<T> &v, size_t max_count) Randomly reorder all of the elements in a vector. If max_count is provided, just make sure that the first max_count entries are randomly drawn from entire vector.
-
emp::vector<size_t>
GetPermutation
(Random &random, size_t size) Return an emp::vector<int> numbered 0 through size-1 in a random order.
-
void
Choose
(Random &random, size_t N, size_t K, std::vector<size_t> &choices) Choose K positions from N possibilities.
-
BitVector
RandomBitVector
(Random &random, size_t size, double p = 0.5) Generate a random BitVector of the specified size.
-
emp::vector<double>
RandomDoubleVector
(Random &random, size_t size, double min, double max) Generate a random double vector in the specified range.
-
emp::vector<size_t>
RandomUIntVector
(Random &random, size_t size, size_t min, size_t max) Generate a random size_t vector in the specified range.
-
template<typename
T
>
emp::vector<T>RandomVector
(Random &random, size_t size, T min, T max) Generate a random vector in the specified type and range.
-
void
RandomizeBitVector
(BitVector &bits, Random &random, double p = 0.5) Generate a random BitVector of the specified size.
-
template<typename
Random Number Generator¶
A versatile and non-patterned pseudo-random-number generator.
- Note
This file is part of Empirical, https://github.com/devosoft/Empirical
- Copyright
Copyright (C) Michigan State University, MIT Software license; see doc/LICENSE.md
- Date
2015-2021.
- Note
Status: RELEASE
-
namespace
emp
If we are in emscripten, make sure to include the header.
-
class
Random
- #include <Random.hpp>
-
class
Range¶
A simple way to track value ranges.
- Note
This file is part of Empirical, https://github.com/devosoft/Empirical
- Copyright
Copyright (C) Michigan State University, MIT Software license; see doc/LICENSE.md
- Date
2016-2019
- Note
Status: BETA
-
namespace
emp
If we are in emscripten, make sure to include the header.
Functions
-
Range<int>
IntRange
(int _l, int _u) Build a new range of type int.
-
Range<double>
DRange
(double _l, double _u) Build a new range of type double.
-
template<typename
T
>
classRange
- #include <Range.hpp>
A range of values from a lower limit to and upper limit, of any provided type.
Public Functions
-
Range
() = default
-
T
GetLower
() const
-
T
GetUpper
() const
-
size_t
CalcBin
(T value, size_t num_bins) const
-
bool
operator==
(const Range &_in) const
-
bool
operator!=
(const Range &_in) const
-
void
SetLower
(T l)
-
void
SetUpper
(T u)
-
void
SetMaxLower
()
-
void
SetMaxUpper
()
-
bool
Valid
(T value) const Determine if a provided value is in the range.
-
-
Range<int>
Sequence Utilities¶
Functions for analyzing with generic sequence types.
A set of functions for analyzing sequences, including distance metrics (Hamming and Edit/Levenschtein) and alignment.
- Note
This file is part of Empirical, https://github.com/devosoft/Empirical
- Copyright
Copyright (C) Michigan State University, MIT Software license; see doc/LICENSE.md
- Date
2016-2020.
- Note
Status: BETA
-
namespace
emp
If we are in emscripten, make sure to include the header.
Functions
-
template<typename
TYPE
>
size_tcalc_hamming_distance
(const TYPE &in1, const TYPE &in2, int offset = 0) Hamming distance is a simple count of substitutions needed to convert one array to another.
- Parameters
in1
: The first sequence to compare.in2
: The second sequence to compare.offset
: (optional) Position in the first sequence to start the second sequence.
-
template<typename
Statistics Tools¶
Functions for calculating various statistics about an ensemble.
- Note
This file is part of Empirical, https://github.com/devosoft/Empirical
- Copyright
Copyright (C) Michigan State University, MIT Software license; see doc/LICENSE.md
- Date
2016-2019
- Note
Status: BETA
-
namespace
emp
If we are in emscripten, make sure to include the header.
Functions
-
template<typename
C
>
emp::remove_ptr_type<typename C::value_type>::typeSum
(C &elements) Calculate sum of the values in a container; if pointers to scalars, convert to scalar type.
-
template<typename
C
, typenameFUN
>
autoSumScalarResults
(C &elements, FUN &&fun) Sum the RESULTS of scalar values in a container; if pointers to scalars, convert to scalar type.
-
template<typename
C
>
doubleShannonEntropy
(C &elements) Calculate Shannon Entropy of the members of the container passed.
-
template<typename
C
>
emp::sfinae_decoy<double, typename C::value_type>Mean
(C &elements) Calculate the mean of the values in a container If values are pointers, they will be automatically de-referenced Values must be numeric.
-
template<typename
C
>
emp::sfinae_decoy<double, typename C::value_type>Median
(C elements)
-
template<typename
C
>
autoVariance
(C &elements) Calculate variance of the members of the container passed Only works on containers with a scalar member type
-
template<typename
C
>
emp::sfinae_decoy<double, typename C::value_type>StandardDeviation
(C &elements) Calculate the standard deviation of the values in a container If values are pointers, they will be automatically de-referenced Values must be numeric.
-
template<typename
C
>
emp::sfinae_decoy<double, typename C::value_type>StandardError
(C &elements) Calculate the standard error of the values in a container If values are pointers, they will be automatically de-referenced Values must be numeric.
-
template<typename
C
>
std::enable_if<!emp::is_ptr_type<typename C::value_type>::value, int>::typeUniqueCount
(C &elements) Count the number of unique elements in a container.
-
template<typename
C
, typenameRET_TYPE
, typenameARG_TYPE
>
RET_TYPEMaxResult
(std::function<RET_TYPE(ARG_TYPE)> &fun, C &elements, ) Run the provided function on every member of a container and return the MAXIMUM result.
-
template<typename
C
, typenameRET_TYPE
, typenameARG_TYPE
>
RET_TYPEMinResult
(std::function<RET_TYPE(ARG_TYPE)> &fun, C &elements, ) Run the provided function on every member of a container and return the MINIMUM result.
-
template<typename
C
, typenameRET_TYPE
, typenameARG_TYPE
>
std::enable_if<std::is_scalar<RET_TYPE>::value, double>::typeMeanResult
(std::function<RET_TYPE(ARG_TYPE)> &fun, C &elements, ) Run the provided function on every member of a container and return the AVERAGE result. Function must return a scalar (i.e. numeric) type.
-
template<typename