27 #define NAN (strtod("NAN",NULL))
35 #define MAX_LOG_TABLE_SIZE 10*1024*1024
36 #define LOG_TABLE_PRECISION 1e-6
38 #define MAX_LOG_TABLE_SIZE 123*1024*1024
39 #define LOG_TABLE_PRECISION 1e-15
41 int32_t CMath::LOGACCURACY = 0;
42 #endif // USE_LOGCACHE
70 LOGRANGE=CMath::determine_logrange();
71 LOGACCURACY=CMath::determine_logaccuracy(
LOGRANGE);
86 SG_FREE(CMath::logtable);
92 int32_t CMath::determine_logrange()
103 SG_SINFO(
"determined range for x in table log(1+exp(-x)) is:%d (error:%G)\n",i,acc)
107 int32_t
CMath::determine_logaccuracy(int32_t range)
109 range=MAX_LOG_TABLE_SIZE/range/((int)
sizeof(
float64_t));
110 SG_SINFO(
"determined accuracy for x in table log(1+exp(-x)) is:%d (error:%G)\n",range,1.0/(
double) range)
115 void CMath::init_log_table()
117 for (int32_t i=0; i< LOGACCURACY*
LOGRANGE; i++)
125 if (a[0]==-1) return ;
128 changed=0; int32_t i=0 ;
129 while ((a[(i+1)*cols]!=-1) && (a[(i+1)*cols+1]!=-1))
131 if (a[i*cols+sort_col]>a[(i+1)*cols+sort_col])
133 for (int32_t j=0; j<cols; j++)
148 for (int32_t i=0; i<N-1; i++)
153 swap(idx[i],idx[i+1]) ;
162 char* seq1,
char* seq2, int32_t l1, int32_t l2,
float64_t gapCost)
172 for( i1 = 0; i1 < l1; ++i1 ) {
173 gapCosts1[ i1 ] = gapCost * i1;
176 for( i2 = 0; i2 < l2; ++i2 ) {
177 gapCosts2[ i2 ] = gapCost * i2;
178 costs2_1[ i2+1 ] = costs2_1[ i2 ] + gapCosts2[ i2 ];
181 for( i1 = 0; i1 < l1; ++i1 ) {
182 swap( costs2_0, costs2_1 );
183 actCost = costs2_0[ 0 ] + gapCosts1[ i1 ];
184 costs2_1[ 0 ] = actCost;
185 for( i2 = 0; i2 < l2; ++i2 ) {
186 const float64_t actMatch = costs2_0[ i2 ] + ( seq1[i1] == seq2[i2] );
187 const float64_t actGap1 = costs2_0[ i2+1 ] + gapCosts1[ i1 ];
188 const float64_t actGap2 = actCost + gapCosts2[ i2 ];
190 actCost =
min( actMatch, actGap );
191 costs2_1[ i2+1 ] = actCost;
219 #ifndef HAVE_STD_ISNAN
220 #if (HAVE_DECL_ISNAN == 1) || defined(HAVE_ISNAN)
223 return ((f != f) ? 1 : 0);
224 #endif // #if (HAVE_DECL_ISNAN == 1) || defined(HAVE_ISNAN)
225 #endif // #ifndef HAVE_STD_ISNAN
227 return std::isnan(f);
232 #ifndef HAVE_STD_ISINF
233 #if (HAVE_DECL_ISINF == 1) || defined(HAVE_ISINF)
235 #elif defined(FPCLASS)
236 if (::fpclass(f) == FP_NINF)
return -1;
237 else if (::fpclass(f) == FP_PINF)
return 1;
240 if ((f == f) && ((f - f) != 0.0))
return (f < 0.0 ? -1 : 1);
243 #endif // #if (HAVE_DECL_ISINF == 1) || defined(HAVE_ISINF)
244 #endif // #ifndef HAVE_STD_ISINF
246 return std::isinf(f);
251 #ifndef HAVE_STD_ISFINITE
252 #if (HAVE_DECL_ISFINITE == 1) || defined(HAVE_ISFINITE)
253 return ::isfinite(f);
254 #elif defined(HAVE_FINITE)
257 return ((!std::isnan(f) && !std::isinf(f)) ? 1 : 0);
258 #endif // #if (HAVE_DECL_ISFINITE == 1) || defined(HAVE_ISFINITE)
259 #endif // #ifndef HAVE_STD_ISFINITE
261 return std::isfinite(f);
272 buf[i]=tolower(str[i]);
273 buf[buf.
vlen-1]=
'\0';
275 if (strstr(buf,
"inf") != NULL)
279 if (strchr(buf,
'-') != NULL)
284 if (strstr(buf,
"nan") != NULL)
290 char* endptr = buf.
vector;
291 *float_result=
::strtof(str, &endptr);
292 return endptr != buf.
vector;
303 buf[i]=tolower(str[i]);
304 buf[buf.
vlen-1]=
'\0';
306 if (strstr(buf,
"inf") != NULL)
310 if (strchr(buf,
'-') != NULL)
311 *double_result *= -1;
315 if (strstr(buf,
"nan") != NULL)
321 char* endptr = buf.
vector;
322 *double_result=
::strtod(str, &endptr);
323 return endptr != buf.
vector;
329 ASSERT(long_double_result);
334 buf[i]=tolower(str[i]);
335 buf[buf.
vlen-1]=
'\0';
337 if (strstr(buf,
"inf") != NULL)
341 if (strchr(buf,
'-') != NULL)
342 *long_double_result *= -1;
346 if (strstr(buf,
"nan") != NULL)
352 char* endptr = buf.
vector;
355 #if defined(WIN32) || defined(__CYGWIN__)
356 *long_double_result=
::strtod(str, &endptr);
358 *long_double_result=
::strtold(str, &endptr);
361 return endptr != buf.
vector;
366 REQUIRE(rel_tolorance > 0 && rel_tolorance < 1.0,
367 "Relative tolorance (%f) should be less than 1.0 and positive\n", rel_tolorance);
369 "The true_value should be finite\n");
371 if (
abs(true_value)>0.0)
376 abs_tolorance =
abs(true_value * rel_tolorance);
378 return abs_tolorance;
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 float64_t get_abs_tolorance(float64_t true_value, float64_t rel_tolorance)
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
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 T min(T a, T b)
return the minimum of two integers
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)
swap e.g. floats a and 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 T abs(T a)
return the absolute value of a number
static const float64_t PI