MathUtils#
Currently, this API is unsupported in Python
-
class MathUtils#
Public Static Functions
-
static int mod(int a, int modulus)#
Computes (a % modulus) and returns a positive number in [0,modulus-1].
- Parameters:
a – the dividend
modulus – the modulus
-
static double mod(double a, double modulus)#
Computes (a % modulus) and returns a positive number in [0,modulus)
- Parameters:
a – the dividend
modulus – the modulus
-
static int integerDivisionCeil(int numerator, int denominator)#
Returns ceil(numerator / denominator)
- Parameters:
numerator – The numerator.
denominator – The denominator.
-
static uint64_t roundDownToPowerOf2(uint64_t x)#
Rounds down x to the biggest power of 2 that is at most x.
- Parameters:
x – number to round down
-
static uint64_t roundUpToPowerOf2(uint64_t x)#
Rounds up x to the smallest power of 2 that is at least x.
- Parameters:
x – number to round up
-
static bool isPowerOf2(uint64_t x)#
Returns true if
x
is power of 2 and false otherwise.- Parameters:
x – Number to check whether it is a power of 2
-
static bool isInt(double val)#
Returns true if this double value can be safely converted to an int.
-
static int getConvolutionOutputSize(int inputSize, int filterSize, int stride, int startPadding = 0, int endPadding = 0)#
Returns the size of the output of the convolution operator for a specific dimension.
- Parameters:
inputSize – the size of the input
filterSize – the size of the filter
stride – the size of the stride
startPadding – padding added at the start of the input
endPadding – padding added at the end of the input
-
static int randInt(int min, int max)#
Returns a uniform random integer in [min, max] (inclusive).
This version of randInt can be used when the user does not care about ensuring reproducibility or controlling the randomness of the chosen integer.
- Parameters:
min – The left boundary of the range to choose from.
max – The right boundary of the range to choose from.
- Throws:
std::exception – If random devices are not available to produce random numbers in the used system
-
static int randInt(int min, int max, std::mt19937 &randomNumberGenerator)#
Returns a uniform random integer in [min, max] (inclusive), using
randomNumberGenerator
random engine.This version of randInt can be used to control the randomness of the generated number (by using a a random engine initialized with a fixed seed, for example).
- Parameters:
min – The left boundary of the range to choose from.
max – The right boundary of the range to choose from.
randomNumberGenerator – The random engine used to generate a random number.
-
static bool isEqual(double a, double b, double eps = 1e-10)#
Returns indication to whether two values should be considered equal.
Taking into account the relative difference, unless at least one of the inputs equals zero, in this case the absolute difference is considered.
- Parameters:
a – The first value.
b – The second value.
eps – The maximal allowed relative difference: (abs(a-b) / max(abs(a),abs(b))) or the maximal allowed absolute difference if at least one of the inputs equals zero.
-
static bool isLess(double a, double b, double eps = 1e-10)#
Returns indication to whether one value should be considered smaller than another.
Based on “MathUtils::isEqual” method, see its description for more details.
- Parameters:
a – The first value.
b – The second value.
eps – See description of “MathUtils::isEqual” method.
-
static bool isLessOrEqual(double a, double b, double eps = 1e-10)#
Returns indication to whether one value should be considered smaller than another or equal to it.
Based on “MathUtils::isEqual” method, see its description for more details.
- Parameters:
a – The first value.
b – The second value.
eps – See description of “MathUtils::isEqual” method.
-
static double getRelativeError(double a, double b)#
Returns the relative error between a and b, which is computed as |a-b|/max(|a|,|b|).
- Parameters:
a – The first value.
b – The second value.
-
static std::vector<double> getInverse(const std::vector<double> &src)#
Returns element-wise inverse vector R given a vector of values V, where R[i] = 1 / V[i] for every index i.
- Parameters:
src – The given input vector.
-
static std::string toString(double d, int precision = 6)#
Returns a string representing the given double, using the given number of precision digits.
- Parameters:
d – The double to convert to a string.
precision – The number of precision digits to use in the string representation of “d”.
-
template<typename T>
static T safeCast(double v, double eps = 1e-6)# Casts “v” to be of type T.
Asserts the result of the casting has the same value (up to “eps”) as the original value of “v”.
- Parameters:
v – The double to cast.
eps – The maximal allowed difference between the casting result and “v”.
-
template<typename T>
static int findUnique(const std::vector<T> &vals, const T &val)# Returns the index of a given unique value in a given vector of values, or -1 if the value does not exist in the vector.
Throws an exception if more than one element in the vector has the given value.
- Parameters:
vals – The vector of values.
val – The value to find.
-
template<typename T>
static void remove(std::vector<T> &vals, const T &val, size_t startDim = 0)# Removes all the elements with the given value from the given vector of values, or from a sub-vector starting at the given index.
- Parameters:
vals – The vector of values.
val – The value to remove.
startDim – An optional index, only remove elements at index greater or equal the given index.
-
template<typename T>
static std::vector<T> getBatch(const std::vector<T> &vec, size_t batchNum, size_t batchSize)# Returns a batch that is a subset of the given vector, according to batch number and the size of each batch, similar to pagination mechanism.
- Parameters:
vec – The vector of values.
batchNum – The batch number.
batchSize – The size of each batch.
-
static void fillPerm(std::vector<int> &perm)#
Fills values of dimensions in a permutation that contains -1 values in some of its elements.
The -1 values will be replaced by values in the range [0,size_of_perm) that currently do not appear in the permutation. The permutation will be filled such that a minimal number of dimensions will be permuted.
- Parameters:
perm – The given permutation.
-
static int mod(int a, int modulus)#