21 #ifndef KALDI_BASE_KALDI_MATH_H_ 22 #define KALDI_BASE_KALDI_MATH_H_ 1 37 #define DBL_EPSILON 2.2204460492503131e-16 40 #define FLT_EPSILON 1.19209290e-7f 44 #define M_PI 3.1415926535897932384626433832795 48 #define M_SQRT2 1.4142135623730950488016887 52 #define M_2PI 6.283185307179586476925286766559005 56 #define M_SQRT1_2 0.7071067811865475244008443621048490 60 #define M_LOG_2PI 1.8378770664093454835606594728112 64 #define M_LN2 0.693147180559945309417232121458 68 #define M_LN10 2.302585092994045684017991454684 72 #define KALDI_ISNAN std::isnan 73 #define KALDI_ISINF std::isinf 74 #define KALDI_ISFINITE(x) std::isfinite(x) 76 #if !defined(KALDI_SQR) 77 # define KALDI_SQR(x) ((x) * (x)) 82 #if !defined(_MSC_VER) || (_MSC_VER >= 1900) 83 inline double Exp(
double x) {
return exp(x); }
85 inline float Exp(
float x) {
return expf(x); }
87 inline float Exp(
float x) {
return exp(static_cast<double>(x)); }
88 #endif // KALDI_NO_EXPF 90 inline double Exp(
double x) {
return exp(x); }
91 #if !defined(__INTEL_COMPILER) && _MSC_VER == 1800 && defined(_M_X64) 94 inline float Exp(
float x) {
return exp(static_cast<double>(x)); }
96 inline float Exp(
float x) {
return expf(x); }
97 #endif // !defined(__INTEL_COMPILER) && _MSC_VER == 1800 && defined(_M_X64) 98 #endif // !defined(_MSC_VER) || (_MSC_VER >= 1900) 100 inline double Log(
double x) {
return log(x); }
101 inline float Log(
float x) {
return logf(x); }
103 #if !defined(_MSC_VER) || (_MSC_VER >= 1700) 104 inline double Log1p(
double x) {
return log1p(x); }
105 inline float Log1p(
float x) {
return log1pf(x); }
107 inline double Log1p(
double x) {
108 const double cutoff = 1.0e-08;
110 return x - 0.5 * x * x;
115 inline float Log1p(
float x) {
116 const float cutoff = 1.0e-07;
118 return x - 0.5 * x * x;
152 return static_cast<float>((
Rand(state) + 1.0) / (RAND_MAX+2.0));
173 template<
class Float>
177 if (post == 0.0 || std::abs(post) >= prune_thresh)
179 return (post >= 0 ? 1.0 : -1.0) *
180 (
RandUniform(state) <= fabs(post)/prune_thresh ? prune_thresh : 0.0);
184 inline double LogAdd(
double x,
double y) {
195 if (diff >= kMinLogDiffDouble) {
217 if (diff >= kMinLogDiffFloat) {
228 inline double LogSub(
double x,
double y) {
233 KALDI_ERR <<
"Cannot subtract a larger from a smaller number.";
237 double res = x +
Log(1.0 -
Exp(diff));
252 KALDI_ERR <<
"Cannot subtract a larger from a smaller number.";
256 float res = x +
Log(1.0f -
Exp(diff));
266 float relative_tolerance = 0.001) {
268 if (a == b)
return true;
269 float diff = std::abs(a-b);
270 if (diff == std::numeric_limits<float>::infinity()
271 || diff != diff)
return false;
272 return (diff <= relative_tolerance*(std::abs(a)+std::abs(b)));
277 float relative_tolerance = 0.001) {
292 return (a - b + 1) / b;
294 return (a - b - 1) / b;
297 template<
class I> I
Gcd(I m, I n) {
298 if (m == 0 || n == 0) {
299 if (m == 0 && n == 0) {
300 KALDI_ERR <<
"Undefined GCD since m = 0, n = 0.";
302 return (m == 0 ? (n > 0 ? n : -n) : ( m > 0 ? m : -m));
310 if (m == 0)
return (n > 0 ? n : -n);
312 if (n == 0)
return (m > 0 ? m : -m);
318 template<
class I> I
Lcm(I m, I n) {
321 return gcd * (m/gcd) * (n/gcd);
325 template<
class I>
void Factorize(I m, std::vector<I> *factors) {
334 I small_factors[10] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 };
337 for (I
i = 0;
i < 10;
i++) {
339 while (m % small_factors[
i] == 0) {
340 m /= small_factors[
i];
341 factors->push_back(small_factors[
i]);
345 for (I
j = 31;;
j += 2) {
349 factors->push_back(
j);
354 inline double Hypot(
double x,
double y) {
return hypot(x, y); }
355 inline float Hypot(
float x,
float y) {
return hypotf(x, y); }
363 #endif // KALDI_BASE_KALDI_MATH_H_ This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
static int32 DivideRoundingDown(int32 a, int32 b)
Returns a / b, rounding towards negative infinity in all cases.
float RandUniform(struct RandomState *state=NULL)
Returns a random number strictly between 0 and 1.
bool WithProb(BaseFloat prob, struct RandomState *state)
Float RandPrune(Float post, BaseFloat prune_thresh, struct RandomState *state=NULL)
float RandGauss(struct RandomState *state=NULL)
int32 RoundUpToNearestPowerOfTwo(int32 n)
I Lcm(I m, I n)
Returns the least common multiple of two integers.
int32 RandPoisson(float lambda, struct RandomState *state)
double LogSub(double x, double y)
static const double kMinLogDiffDouble
void RandGauss2(float *a, float *b, RandomState *state)
int Rand(struct RandomState *state)
static const float kMinLogDiffFloat
double LogAdd(double x, double y)
#define KALDI_ASSERT(cond)
static void AssertEqual(float a, float b, float relative_tolerance=0.001)
assert abs(a - b) <= relative_tolerance * (abs(a)+abs(b))
const float kLogZeroFloat
const BaseFloat kLogZeroBaseFloat
const double kLogZeroDouble
static bool ApproxEqual(float a, float b, float relative_tolerance=0.001)
return abs(a - b) <= relative_tolerance * (abs(a)+abs(b)).
int32 RandInt(int32 min_val, int32 max_val, struct RandomState *state)
double Hypot(double x, double y)
void Factorize(I m, std::vector< I > *factors)