11 #include <shogun/lib/config.h> 20 #define NAN (strtod("NAN",NULL)) 28 #define MAX_LOG_TABLE_SIZE 10*1024*1024 29 #define LOG_TABLE_PRECISION 1e-6 31 #define MAX_LOG_TABLE_SIZE 123*1024*1024 32 #define LOG_TABLE_PRECISION 1e-15 34 int32_t CMath::LOGACCURACY = 0;
35 #endif // USE_LOGCACHE 63 LOGRANGE=CMath::determine_logrange();
64 LOGACCURACY=CMath::determine_logaccuracy(
LOGRANGE);
79 SG_FREE(CMath::logtable);
103 int32_t CMath::determine_logrange()
114 SG_SINFO(
"determined range for x in table log(1+exp(-x)) is:%d (error:%G)\n",i,acc)
118 int32_t CMath::determine_logaccuracy(int32_t range)
120 range=MAX_LOG_TABLE_SIZE/range/((int)
sizeof(
float64_t));
121 SG_SINFO(
"determined accuracy for x in table log(1+exp(-x)) is:%d (error:%G)\n",range,1.0/(
double) range)
126 void CMath::init_log_table()
128 for (int32_t i=0; i< LOGACCURACY*
LOGRANGE; i++)
136 if (a[0]==-1)
return;
139 changed=0; int32_t i=0;
140 while ((a[(i+1)*cols]!=-1) && (a[(i+1)*cols+1]!=-1))
142 if (a[i*cols+sort_col]>a[(i+1)*cols+sort_col])
144 for (int32_t j=0; j<cols; j++)
159 for (int32_t i=0; i<N-1; i++)
164 swap(idx[i],idx[i+1]) ;
173 char* seq1,
char* seq2, int32_t l1, int32_t l2,
float64_t gapCost)
183 for( i1 = 0; i1 < l1; ++i1 ) {
184 gapCosts1[ i1 ] = gapCost * i1;
187 for( i2 = 0; i2 < l2; ++i2 ) {
188 gapCosts2[ i2 ] = gapCost * i2;
189 costs2_1[ i2+1 ] = costs2_1[ i2 ] + gapCosts2[ i2 ];
192 for( i1 = 0; i1 < l1; ++i1 ) {
193 swap( costs2_0, costs2_1 );
194 actCost = costs2_0[ 0 ] + gapCosts1[ i1 ];
195 costs2_1[ 0 ] = actCost;
196 for( i2 = 0; i2 < l2; ++i2 ) {
197 const float64_t actMatch = costs2_0[ i2 ] + ( seq1[i1] == seq2[i2] );
198 const float64_t actGap1 = costs2_0[ i2+1 ] + gapCosts1[ i1 ];
199 const float64_t actGap2 = actCost + gapCosts2[ i2 ];
201 actCost =
min( actMatch, actGap );
202 costs2_1[ i2+1 ] = actCost;
230 #ifndef HAVE_STD_ISNAN 231 #if (HAVE_DECL_ISNAN == 1) || defined(HAVE_ISNAN) 234 return ((f != f) ? 1 : 0);
235 #endif // #if (HAVE_DECL_ISNAN == 1) || defined(HAVE_ISNAN) 237 return std::isnan(f);
238 #endif // #ifndef HAVE_STD_ISNAN 243 #ifndef HAVE_STD_ISINF 244 #if (HAVE_DECL_ISINF == 1) || defined(HAVE_ISINF) 246 #elif defined(FPCLASS) 247 if (::fpclass(f) == FP_NINF)
return -1;
248 else if (::fpclass(f) == FP_PINF)
return 1;
251 if ((f == f) && ((f - f) != 0.0))
return (f < 0.0 ? -1 : 1);
253 #endif // #if (HAVE_DECL_ISINF == 1) || defined(HAVE_ISINF) 255 return std::isinf(f);
256 #endif // #ifndef HAVE_STD_ISINF 261 #ifndef HAVE_STD_ISFINITE 262 #if (HAVE_DECL_ISFINITE == 1) || defined(HAVE_ISFINITE) 263 return ::isfinite(f);
264 #elif defined(HAVE_FINITE) 268 #endif // #if (HAVE_DECL_ISFINITE == 1) || defined(HAVE_ISFINITE) 270 return std::isfinite(f);
271 #endif // #ifndef HAVE_STD_ISFINITE 282 buf[i]=tolower(str[i]);
283 buf[buf.
vlen-1]=
'\0';
285 if (strstr(buf,
"inf") != NULL)
289 if (strchr(buf,
'-') != NULL)
294 if (strstr(buf,
"nan") != NULL)
300 char* endptr = buf.
vector;
301 *float_result=
::strtof(str, &endptr);
302 return endptr != buf.
vector;
313 buf[i]=tolower(str[i]);
314 buf[buf.
vlen-1]=
'\0';
316 if (strstr(buf,
"inf") != NULL)
320 if (strchr(buf,
'-') != NULL)
321 *double_result *= -1;
325 if (strstr(buf,
"nan") != NULL)
331 char* endptr = buf.
vector;
332 *double_result=
::strtod(str, &endptr);
333 return endptr != buf.
vector;
339 ASSERT(long_double_result);
344 buf[i]=tolower(str[i]);
345 buf[buf.
vlen-1]=
'\0';
347 if (strstr(buf,
"inf") != NULL)
351 if (strchr(buf,
'-') != NULL)
352 *long_double_result *= -1;
356 if (strstr(buf,
"nan") != NULL)
362 char* endptr = buf.
vector;
365 #if defined(WIN32) || defined(__CYGWIN__) 366 *long_double_result=
::strtod(str, &endptr);
368 *long_double_result=
::strtold(str, &endptr);
371 return endptr != buf.
vector;
376 REQUIRE(rel_tolerance > 0 && rel_tolerance < 1.0,
377 "Relative tolerance (%f) should be less than 1.0 and positive\n", rel_tolerance);
379 "The true_value should be finite\n");
381 if (
abs(true_value)>0.0)
386 abs_tolerance =
abs(true_value * rel_tolerance);
388 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
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