SHOGUN  6.1.3
Math.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 1999-2009 Soeren Sonnenburg
8  * Written (W) 1999-2008 Gunnar Raetsch
9  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
10  */
11 #include <shogun/lib/config.h>
12 
15 
16 #include <stdlib.h>
17 
18 #ifndef NAN
19 #include <stdlib.h>
20 #define NAN (strtod("NAN",NULL))
21 #endif
22 
23 
24 using namespace shogun;
25 
26 #ifdef USE_LOGCACHE
27 #ifdef USE_HMMDEBUG
28 #define MAX_LOG_TABLE_SIZE 10*1024*1024
29 #define LOG_TABLE_PRECISION 1e-6
30 #else //USE_HMMDEBUG
31 #define MAX_LOG_TABLE_SIZE 123*1024*1024
32 #define LOG_TABLE_PRECISION 1e-15
33 #endif //USE_HMMDEBUG
34 int32_t CMath::LOGACCURACY = 0; // 100000 steps per integer
35 #endif // USE_LOGCACHE
36 
37 int32_t CMath::LOGRANGE = 0; // range for logtable: log(1+exp(x)) -25 <= x <= 0
38 
40 const float64_t CMath::INFTY = INFINITY; // infinity
41 const float64_t CMath::ALMOST_INFTY = +1e+300; //a large number
42 const float64_t CMath::ALMOST_NEG_INFTY = -1e+300;
44 const float64_t CMath::MACHINE_EPSILON=DBL_EPSILON;
45 const float64_t CMath::MAX_REAL_NUMBER=DBL_MAX;
46 const float64_t CMath::MIN_REAL_NUMBER=DBL_MIN;
47 const float32_t CMath::F_MAX_VAL32=FLT_MAX;
49 const float64_t CMath::F_MAX_VAL64=DBL_MAX;
51 const float32_t CMath::F_MIN_VAL32=(FLT_MIN * FLT_EPSILON);
52 const float64_t CMath::F_MIN_VAL64=(DBL_MIN * DBL_EPSILON);
53 
54 #ifdef USE_LOGCACHE
55 float64_t* CMath::logtable = NULL;
56 #endif
57 uint32_t CMath::seed = 0;
58 
60 : CSGObject()
61 {
62 #ifdef USE_LOGCACHE
63  LOGRANGE=CMath::determine_logrange();
64  LOGACCURACY=CMath::determine_logaccuracy(LOGRANGE);
65  CMath::logtable=SG_MALLOC(float64_t, LOGRANGE*LOGACCURACY);
66  init_log_table();
67 #else
68  int32_t i=0;
69  while ((float64_t)log(1+((float64_t)exp(-float64_t(i)))))
70  i++;
71 
72  LOGRANGE=i;
73 #endif
74 }
75 
77 {
78 #ifdef USE_LOGCACHE
79  SG_FREE(CMath::logtable);
80  CMath::logtable=NULL;
81 #endif
82 }
83 
84 #ifdef USE_LOGCACHE
85 int32_t CMath::determine_logrange()
86 {
87  int32_t i;
88  float64_t acc=0;
89  for (i=0; i<50; i++)
90  {
91  acc=((float64_t)log(1+((float64_t)exp(-float64_t(i)))));
92  if (acc<=(float64_t)LOG_TABLE_PRECISION)
93  break;
94  }
95 
96  SG_SINFO("determined range for x in table log(1+exp(-x)) is:%d (error:%G)\n",i,acc)
97  return i;
98 }
99 
100 int32_t CMath::determine_logaccuracy(int32_t range)
101 {
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)
104  return range;
105 }
106 
107 //init log table of form log(1+exp(x))
108 void CMath::init_log_table()
109 {
110  for (int32_t i=0; i< LOGACCURACY*LOGRANGE; i++)
111  logtable[i]=log(1+exp(float64_t(-i)/float64_t(LOGACCURACY)));
112 }
113 #endif
114 
115 void CMath::sort(int32_t *a, int32_t cols, int32_t sort_col)
116 {
117  int32_t changed=1;
118  if (a[0]==-1) return;
119  while (changed)
120  {
121  changed=0; int32_t i=0;
122  while ((a[(i+1)*cols]!=-1) && (a[(i+1)*cols+1]!=-1)) // to be sure
123  {
124  if (a[i*cols+sort_col]>a[(i+1)*cols+sort_col])
125  {
126  for (int32_t j=0; j<cols; j++)
127  CMath::swap(a[i*cols+j],a[(i+1)*cols+j]);
128  changed=1;
129  };
130  i++;
131  };
132  };
133 }
134 
135 void CMath::sort(float64_t *a, int32_t* idx, int32_t N)
136 {
137  int32_t changed=1;
138  while (changed)
139  {
140  changed=0;
141  for (int32_t i=0; i<N-1; i++)
142  {
143  if (a[i]>a[i+1])
144  {
145  swap(a[i],a[i+1]) ;
146  swap(idx[i],idx[i+1]) ;
147  changed=1 ;
148  } ;
149  } ;
150  } ;
151 
152 }
153 
155  char* seq1, char* seq2, int32_t l1, int32_t l2, float64_t gapCost)
156 {
157  float64_t actCost=0 ;
158  int32_t i1, i2 ;
159  float64_t* const gapCosts1 = SG_MALLOC(float64_t, l1 );
160  float64_t* const gapCosts2 = SG_MALLOC(float64_t, l2 );
161  float64_t* costs2_0 = SG_MALLOC(float64_t, l2 + 1 );
162  float64_t* costs2_1 = SG_MALLOC(float64_t, l2 + 1 );
163 
164  // initialize borders
165  for( i1 = 0; i1 < l1; ++i1 ) {
166  gapCosts1[ i1 ] = gapCost * i1;
167  }
168  costs2_1[ 0 ] = 0;
169  for( i2 = 0; i2 < l2; ++i2 ) {
170  gapCosts2[ i2 ] = gapCost * i2;
171  costs2_1[ i2+1 ] = costs2_1[ i2 ] + gapCosts2[ i2 ];
172  }
173  // compute alignment
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 ];
182  const float64_t actGap = min( actGap1, actGap2 );
183  actCost = min( actMatch, actGap );
184  costs2_1[ i2+1 ] = actCost;
185  }
186  }
187 
188  SG_FREE(gapCosts1);
189  SG_FREE(gapCosts2);
190  SG_FREE(costs2_0);
191  SG_FREE(costs2_1);
192 
193  // return the final cost
194  return actCost;
195 }
196 
197 void CMath::linspace(float64_t* output, float64_t start, float64_t end, int32_t n)
198 {
199  float64_t delta = (end-start) / (n-1);
200  float64_t v = start;
201  index_t i = 0;
202  while ( v <= end )
203  {
204  output[i++] = v;
205  v += delta;
206  }
207  output[n-1] = end;
208 }
209 
210 int CMath::is_nan(double f)
211 {
212  return std::isnan(f);
213 }
214 
215 int CMath::is_infinity(double f)
216 {
217  return std::isinf(f);
218 }
219 
220 int CMath::is_finite(double f)
221 {
222  return std::isfinite(f);
223 }
224 
225 bool CMath::strtof(const char* str, float32_t* float_result)
226 {
227  ASSERT(str);
228  ASSERT(float_result);
229 
230  SGVector<char> buf(strlen(str)+1);
231 
232  for (index_t i=0; i<buf.vlen-1; i++)
233  buf[i]=tolower(str[i]);
234  buf[buf.vlen-1]='\0';
235 
236  if (strstr(buf, "inf") != NULL)
237  {
238  *float_result = CMath::INFTY;
239 
240  if (strchr(buf,'-') != NULL)
241  *float_result *= -1;
242  return true;
243  }
244 
245  if (strstr(buf, "nan") != NULL)
246  {
247  *float_result = CMath::NOT_A_NUMBER;
248  return true;
249  }
250 
251  char* endptr = buf.vector;
252  *float_result=::strtof(str, &endptr);
253  return endptr != buf.vector;
254 }
255 
256 bool CMath::strtod(const char* str, float64_t* double_result)
257 {
258  ASSERT(str);
259  ASSERT(double_result);
260 
261  SGVector<char> buf(strlen(str)+1);
262 
263  for (index_t i=0; i<buf.vlen-1; i++)
264  buf[i]=tolower(str[i]);
265  buf[buf.vlen-1]='\0';
266 
267  if (strstr(buf, "inf") != NULL)
268  {
269  *double_result = CMath::INFTY;
270 
271  if (strchr(buf,'-') != NULL)
272  *double_result *= -1;
273  return true;
274  }
275 
276  if (strstr(buf, "nan") != NULL)
277  {
278  *double_result = CMath::NOT_A_NUMBER;
279  return true;
280  }
281 
282  char* endptr = buf.vector;
283  *double_result=::strtod(str, &endptr);
284  return endptr != buf.vector;
285 }
286 
287 bool CMath::strtold(const char* str, floatmax_t* long_double_result)
288 {
289  ASSERT(str);
290  ASSERT(long_double_result);
291 
292  SGVector<char> buf(strlen(str)+1);
293 
294  for (index_t i=0; i<buf.vlen-1; i++)
295  buf[i]=tolower(str[i]);
296  buf[buf.vlen-1]='\0';
297 
298  if (strstr(buf, "inf") != NULL)
299  {
300  *long_double_result = CMath::INFTY;
301 
302  if (strchr(buf,'-') != NULL)
303  *long_double_result *= -1;
304  return true;
305  }
306 
307  if (strstr(buf, "nan") != NULL)
308  {
309  *long_double_result = CMath::NOT_A_NUMBER;
310  return true;
311  }
312 
313  char* endptr = buf.vector;
314 
315 // fall back to double on win32 / cygwin since strtold is undefined there
316 #if defined(WIN32) || defined(__CYGWIN__)
317  *long_double_result=::strtod(str, &endptr);
318 #else
319  *long_double_result=::strtold(str, &endptr);
320 #endif
321 
322  return endptr != buf.vector;
323 }
324 
326 {
327  REQUIRE(rel_tolerance > 0 && rel_tolerance < 1.0,
328  "Relative tolerance (%f) should be less than 1.0 and positive\n", rel_tolerance);
329  REQUIRE(is_finite(true_value),
330  "The true_value should be finite\n");
331  float64_t abs_tolerance = rel_tolerance;
332  if (abs(true_value)>0.0)
333  {
334  if (log(abs(true_value)) + log(rel_tolerance) < log(F_MIN_VAL64))
335  abs_tolerance = F_MIN_VAL64;
336  else
337  abs_tolerance = abs(true_value * rel_tolerance);
338  }
339  return abs_tolerance;
340 }
static const float32_t F_MAX_VAL32
Definition: Math.h:1885
static const float64_t MACHINE_EPSILON
Definition: Math.h:1878
static bool strtof(const char *str, float32_t *float_result)
Definition: Math.cpp:225
static uint32_t seed
random generator seed
Definition: Math.h:1899
static int is_finite(double f)
checks whether a float is finite
Definition: Math.cpp:220
static float64_t Align(char *seq1, char *seq2, int32_t l1, int32_t l2, float64_t gapCost)
Definition: Math.cpp:154
static void linspace(float64_t *output, float64_t start, float64_t end, int32_t n=100)
Definition: Math.cpp:197
int32_t index_t
Definition: common.h:72
static bool strtod(const char *str, float64_t *double_result)
Definition: Math.cpp:256
virtual ~CMath()
Destructor - frees logtable.
Definition: Math.cpp:76
static const float64_t INFTY
infinity
Definition: Math.h:1868
static const float64_t MIN_REAL_NUMBER
Definition: Math.h:1882
#define REQUIRE(x,...)
Definition: SGIO.h:181
static const float64_t F_MAX_VAL64
Definition: Math.h:1887
static const float32_t F_MIN_VAL32
Definition: Math.h:1891
Range< T > range(T rend)
Definition: range.h:136
static const float32_t F_MIN_NORM_VAL32
Definition: Math.h:1886
static int32_t LOGRANGE
range for logtable: log(1+exp(x)) -LOGRANGE <= x <= 0
Definition: Math.h:1896
static const float64_t ALMOST_NEG_INFTY
almost neg (log) infinity
Definition: Math.h:1872
CMath()
Constructor - initializes log-table.
Definition: Math.cpp:59
#define ASSERT(x)
Definition: SGIO.h:176
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:124
double float64_t
Definition: common.h:60
long double floatmax_t
Definition: common.h:61
#define M_PI
Definition: Math.h:50
float float32_t
Definition: common.h:59
static float64_t get_abs_tolerance(float64_t true_value, float64_t rel_tolerance)
Definition: Math.cpp:325
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
static int is_infinity(double f)
checks whether a float is infinity
Definition: Math.cpp:215
static int is_nan(double f)
checks whether a float is nan
Definition: Math.cpp:210
static float64_t exp(float64_t x)
Definition: Math.h:551
#define SG_SINFO(...)
Definition: SGIO.h:158
static const float64_t F_MIN_VAL64
Definition: Math.h:1892
static const float64_t F_MIN_NORM_VAL64
Definition: Math.h:1888
static float64_t log(float64_t v)
Definition: Math.h:714
static const float64_t ALMOST_INFTY
Definition: Math.h:1869
static void swap(T &a, T &b)
Definition: Math.h:406
static void sort(int32_t *a, int32_t cols, int32_t sort_col=0)
Definition: Math.cpp:115
#define NAN
Definition: Math.cpp:20
static T min(T a, T b)
Definition: Math.h:138
static bool strtold(const char *str, floatmax_t *long_double_result)
Definition: Math.cpp:287
static const float64_t NOT_A_NUMBER
not a number
Definition: Math.h:1866
static const float64_t MAX_REAL_NUMBER
Definition: Math.h:1881
index_t vlen
Definition: SGVector.h:571
static T abs(T a)
Definition: Math.h:161
static const float64_t PI
Definition: Math.h:1875

SHOGUN Machine Learning Toolbox - Documentation