26 #define NAN (strtod("NAN",NULL))
30 using namespace shogun;
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
63 LOGRANGE=CMath::determine_logrange();
64 LOGACCURACY=CMath::determine_logaccuracy(
LOGRANGE);
79 SG_FREE(CMath::logtable);
85 int32_t CMath::determine_logrange()
96 SG_SINFO(
"determined range for x in table log(1+exp(-x)) is:%d (error:%G)\n",i,acc)
100 int32_t
CMath::determine_logaccuracy(int32_t range)
102 range=MAX_LOG_TABLE_SIZE/range/((int)
sizeof(
float64_t));
103 SG_SINFO(
"determined accuracy for x in table log(1+exp(-x)) is:%d (error:%G)\n",range,1.0/(
double) range)
108 void CMath::init_log_table()
110 for (int32_t i=0; i< LOGACCURACY*
LOGRANGE; i++)
118 if (a[0]==-1) return ;
121 changed=0; int32_t i=0 ;
122 while ((a[(i+1)*cols]!=-1) && (a[(i+1)*cols+1]!=-1))
124 if (a[i*cols+sort_col]>a[(i+1)*cols+sort_col])
126 for (int32_t j=0; j<cols; j++)
141 for (int32_t i=0; i<N-1; i++)
146 swap(idx[i],idx[i+1]) ;
155 char* seq1,
char* seq2, int32_t l1, int32_t l2,
float64_t gapCost)
165 for( i1 = 0; i1 < l1; ++i1 ) {
166 gapCosts1[ i1 ] = gapCost * i1;
169 for( i2 = 0; i2 < l2; ++i2 ) {
170 gapCosts2[ i2 ] = gapCost * i2;
171 costs2_1[ i2+1 ] = costs2_1[ i2 ] + gapCosts2[ i2 ];
174 for( i1 = 0; i1 < l1; ++i1 ) {
175 swap( costs2_0, costs2_1 );
176 actCost = costs2_0[ 0 ] + gapCosts1[ i1 ];
177 costs2_1[ 0 ] = actCost;
178 for( i2 = 0; i2 < l2; ++i2 ) {
179 const float64_t actMatch = costs2_0[ i2 ] + ( seq1[i1] == seq2[i2] );
180 const float64_t actGap1 = costs2_0[ i2+1 ] + gapCosts1[ i1 ];
181 const float64_t actGap2 = actCost + gapCosts2[ i2 ];
183 actCost =
min( actMatch, actGap );
184 costs2_1[ i2+1 ] = actCost;
213 #ifndef HAVE_STD_ISNAN
214 #if (HAVE_DECL_ISNAN == 1) || defined(HAVE_ISNAN)
217 return ((f != f) ? 1 : 0);
218 #endif // #if (HAVE_DECL_ISNAN == 1) || defined(HAVE_ISNAN)
219 #endif // #ifndef HAVE_STD_ISNAN
221 return std::isnan(f);
226 #ifndef HAVE_STD_ISINF
227 #if (HAVE_DECL_ISINF == 1) || defined(HAVE_ISINF)
229 #elif defined(FPCLASS)
230 if (::fpclass(f) == FP_NINF)
return -1;
231 else if (::fpclass(f) == FP_PINF)
return 1;
234 if ((f == f) && ((f - f) != 0.0))
return (f < 0.0 ? -1 : 1);
237 #endif // #if (HAVE_DECL_ISINF == 1) || defined(HAVE_ISINF)
238 #endif // #ifndef HAVE_STD_ISINF
240 return std::isinf(f);
245 #ifndef HAVE_STD_ISFINITE
246 #if (HAVE_DECL_ISFINITE == 1) || defined(HAVE_ISFINITE)
247 return ::isfinite(f);
248 #elif defined(HAVE_FINITE)
251 return ((!std::isnan(f) && !std::isinf(f)) ? 1 : 0);
252 #endif // #if (HAVE_DECL_ISFINITE == 1) || defined(HAVE_ISFINITE)
253 #endif // #ifndef HAVE_STD_ISFINITE
255 return std::isfinite(f);
266 buf[i]=tolower(str[i]);
267 buf[buf.
vlen-1]=
'\0';
269 if (strstr(buf,
"inf") != NULL)
273 if (strchr(buf,
'-') != NULL)
278 if (strstr(buf,
"nan") != NULL)
284 char* endptr = buf.
vector;
285 *float_result=
::strtof(str, &endptr);
286 return endptr != buf.
vector;
297 buf[i]=tolower(str[i]);
298 buf[buf.
vlen-1]=
'\0';
300 if (strstr(buf,
"inf") != NULL)
304 if (strchr(buf,
'-') != NULL)
305 *double_result *= -1;
309 if (strstr(buf,
"nan") != NULL)
315 char* endptr = buf.
vector;
316 *double_result=
::strtod(str, &endptr);
317 return endptr != buf.
vector;
323 ASSERT(long_double_result);
328 buf[i]=tolower(str[i]);
329 buf[buf.
vlen-1]=
'\0';
331 if (strstr(buf,
"inf") != NULL)
335 if (strchr(buf,
'-') != NULL)
336 *long_double_result *= -1;
340 if (strstr(buf,
"nan") != NULL)
346 char* endptr = buf.
vector;
349 #if defined(WIN32) || defined(__CYGWIN__)
350 *long_double_result=
::strtod(str, &endptr);
352 *long_double_result=
::strtold(str, &endptr);
355 return endptr != buf.
vector;
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 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 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 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 const float64_t PI