26 #define NAN (strtod("NAN",NULL))
34 #define MAX_LOG_TABLE_SIZE 10*1024*1024
35 #define LOG_TABLE_PRECISION 1e-6
37 #define MAX_LOG_TABLE_SIZE 123*1024*1024
38 #define LOG_TABLE_PRECISION 1e-15
40 int32_t CMath::LOGACCURACY = 0;
41 #endif // USE_LOGCACHE
69 LOGRANGE=CMath::determine_logrange();
70 LOGACCURACY=CMath::determine_logaccuracy(
LOGRANGE);
85 SG_FREE(CMath::logtable);
109 int32_t CMath::determine_logrange()
120 SG_SINFO(
"determined range for x in table log(1+exp(-x)) is:%d (error:%G)\n",i,acc)
124 int32_t
CMath::determine_logaccuracy(int32_t range)
126 range=MAX_LOG_TABLE_SIZE/range/((int)
sizeof(
float64_t));
127 SG_SINFO(
"determined accuracy for x in table log(1+exp(-x)) is:%d (error:%G)\n",range,1.0/(
double) range)
132 void CMath::init_log_table()
134 for (int32_t i=0; i< LOGACCURACY*
LOGRANGE; i++)
142 if (a[0]==-1)
return;
145 changed=0; int32_t i=0;
146 while ((a[(i+1)*cols]!=-1) && (a[(i+1)*cols+1]!=-1))
148 if (a[i*cols+sort_col]>a[(i+1)*cols+sort_col])
150 for (int32_t j=0; j<cols; j++)
165 for (int32_t i=0; i<N-1; i++)
170 swap(idx[i],idx[i+1]) ;
179 char* seq1,
char* seq2, int32_t l1, int32_t l2,
float64_t gapCost)
189 for( i1 = 0; i1 < l1; ++i1 ) {
190 gapCosts1[ i1 ] = gapCost * i1;
193 for( i2 = 0; i2 < l2; ++i2 ) {
194 gapCosts2[ i2 ] = gapCost * i2;
195 costs2_1[ i2+1 ] = costs2_1[ i2 ] + gapCosts2[ i2 ];
198 for( i1 = 0; i1 < l1; ++i1 ) {
199 swap( costs2_0, costs2_1 );
200 actCost = costs2_0[ 0 ] + gapCosts1[ i1 ];
201 costs2_1[ 0 ] = actCost;
202 for( i2 = 0; i2 < l2; ++i2 ) {
203 const float64_t actMatch = costs2_0[ i2 ] + ( seq1[i1] == seq2[i2] );
204 const float64_t actGap1 = costs2_0[ i2+1 ] + gapCosts1[ i1 ];
205 const float64_t actGap2 = actCost + gapCosts2[ i2 ];
207 actCost =
min( actMatch, actGap );
208 costs2_1[ i2+1 ] = actCost;
236 #ifndef HAVE_STD_ISNAN
237 #if (HAVE_DECL_ISNAN == 1) || defined(HAVE_ISNAN)
240 return ((f != f) ? 1 : 0);
241 #endif // #if (HAVE_DECL_ISNAN == 1) || defined(HAVE_ISNAN)
242 #endif // #ifndef HAVE_STD_ISNAN
244 return std::isnan(f);
249 #ifndef HAVE_STD_ISINF
250 #if (HAVE_DECL_ISINF == 1) || defined(HAVE_ISINF)
252 #elif defined(FPCLASS)
253 if (::fpclass(f) == FP_NINF)
return -1;
254 else if (::fpclass(f) == FP_PINF)
return 1;
257 if ((f == f) && ((f - f) != 0.0))
return (f < 0.0 ? -1 : 1);
260 #endif // #if (HAVE_DECL_ISINF == 1) || defined(HAVE_ISINF)
261 #endif // #ifndef HAVE_STD_ISINF
263 return std::isinf(f);
268 #ifndef HAVE_STD_ISFINITE
269 #if (HAVE_DECL_ISFINITE == 1) || defined(HAVE_ISFINITE)
270 return ::isfinite(f);
271 #elif defined(HAVE_FINITE)
274 return ((!std::isnan(f) && !std::isinf(f)) ? 1 : 0);
275 #endif // #if (HAVE_DECL_ISFINITE == 1) || defined(HAVE_ISFINITE)
276 #endif // #ifndef HAVE_STD_ISFINITE
278 return std::isfinite(f);
289 buf[i]=tolower(str[i]);
290 buf[buf.
vlen-1]=
'\0';
292 if (strstr(buf,
"inf") != NULL)
296 if (strchr(buf,
'-') != NULL)
301 if (strstr(buf,
"nan") != NULL)
307 char* endptr = buf.
vector;
308 *float_result=
::strtof(str, &endptr);
309 return endptr != buf.
vector;
320 buf[i]=tolower(str[i]);
321 buf[buf.
vlen-1]=
'\0';
323 if (strstr(buf,
"inf") != NULL)
327 if (strchr(buf,
'-') != NULL)
328 *double_result *= -1;
332 if (strstr(buf,
"nan") != NULL)
338 char* endptr = buf.
vector;
339 *double_result=
::strtod(str, &endptr);
340 return endptr != buf.
vector;
346 ASSERT(long_double_result);
351 buf[i]=tolower(str[i]);
352 buf[buf.
vlen-1]=
'\0';
354 if (strstr(buf,
"inf") != NULL)
358 if (strchr(buf,
'-') != NULL)
359 *long_double_result *= -1;
363 if (strstr(buf,
"nan") != NULL)
369 char* endptr = buf.
vector;
372 #if defined(WIN32) || defined(__CYGWIN__)
373 *long_double_result=
::strtod(str, &endptr);
375 *long_double_result=
::strtold(str, &endptr);
378 return endptr != buf.
vector;
383 REQUIRE(rel_tolerance > 0 && rel_tolerance < 1.0,
384 "Relative tolerance (%f) should be less than 1.0 and positive\n", rel_tolerance);
386 "The true_value should be finite\n");
388 if (
abs(true_value)>0.0)
393 abs_tolerance =
abs(true_value * rel_tolerance);
395 return abs_tolerance;
static const float32_t F_MAX_VAL32
static const float64_t MACHINE_EPSILON
static bool strtof(const char *str, float32_t *float_result)
static uint32_t seed
random generator seed
static int is_finite(double f)
checks whether a float is finite
static float64_t Align(char *seq1, char *seq2, int32_t l1, int32_t l2, float64_t gapCost)
static void linspace(float64_t *output, float64_t start, float64_t end, int32_t n=100)
static bool strtod(const char *str, float64_t *double_result)
virtual ~CMath()
Destructor - frees logtable.
static const float64_t INFTY
infinity
static const float64_t MIN_REAL_NUMBER
static const float64_t F_MAX_VAL64
static const float32_t F_MIN_VAL32
static const float32_t F_MIN_NORM_VAL32
static int32_t LOGRANGE
range for logtable: log(1+exp(x)) -LOGRANGE <= x <= 0
static const float64_t ALMOST_NEG_INFTY
almost neg (log) infinity
CMath()
Constructor - initializes log-table.
Class SGObject is the base class of all shogun objects.
#define M_PI
workaround for log2 being a define on cygwin
static float64_t dot(const bool *v1, const bool *v2, int32_t n)
Compute dot product between v1 and v2 (blas optimized)
static float64_t get_abs_tolerance(float64_t true_value, float64_t rel_tolerance)
all of classes and functions are contained in the shogun namespace
static int is_infinity(double f)
checks whether a float is infinity
static int is_nan(double f)
checks whether a float is nan
static float64_t exp(float64_t x)
static const float64_t F_MIN_VAL64
static const float64_t F_MIN_NORM_VAL64
static float64_t log(float64_t v)
static const float64_t ALMOST_INFTY
Class which collects generic mathematical functions.
static void swap(T &a, T &b)
static void sort(int32_t *a, int32_t cols, int32_t sort_col=0)
static bool strtold(const char *str, floatmax_t *long_double_result)
static const float64_t NOT_A_NUMBER
not a number
static const float64_t MAX_REAL_NUMBER
static const float64_t PI