Julius 4.2
libsent/src/phmm/addlog.c
説明を見る。
00001 
00018 /*
00019  * Copyright (c) 1991-2011 Kawahara Lab., Kyoto University
00020  * Copyright (c) 2000-2005 Shikano Lab., Nara Institute of Science and Technology
00021  * Copyright (c) 2005-2011 Julius project team, Nagoya Institute of Technology
00022  * All rights reserved
00023  */
00024 
00025 #include <sent/stddefs.h>
00026 #include <sent/hmm.h>
00027 
00028 #define TBLSIZE 500000          ///< Table size (precision depends on this)
00029 #define VRANGE 15               ///< Must be larger than -LOG_ADDMIN
00030 #define TMAG 33333.3333         ///< TBLSIZE / VRANGE
00031 
00032 static LOGPROB tbl[TBLSIZE];    
00033 static boolean built_tbl = FALSE;
00034 
00041 void
00042 make_log_tbl()
00043 {
00044   LOGPROB f;
00045   int i;
00046 
00047   if (built_tbl == FALSE) {
00048     jlog("Stat: addlog: generating addlog table (size = %d kB)\n", (TBLSIZE * sizeof(LOGPROB)) / 1024);
00049     for (i=0;i<TBLSIZE;i++){
00050       f = - ((float)VRANGE * (float)i / (float)TBLSIZE);
00051       tbl[i] = log(1 + exp(f));
00052       /*if (i < 10 || i > TBLSIZE - 10) j_printf("%f: %d(%f)\n", f, i, tbl[i]);*/
00053     }
00054     jlog("Stat: addlog: addlog table generated\n");
00055     built_tbl = TRUE;
00056   }
00057 }
00058 
00070 LOGPROB
00071 addlog(LOGPROB x, LOGPROB y)
00072 {
00073   /* return(log(exp(x)+exp(y))) */
00074   LOGPROB tmp;
00075   unsigned int idx;
00076   
00077   if (x < y) {
00078     if ((tmp = x - y) < LOG_ADDMIN) return y;
00079     else {
00080       idx = (unsigned int)((- tmp) * TMAG + 0.5);
00081       /* jlog("%f == %f\n",tbl[idx],log(1 + exp(tmp))); */
00082       return (y + tbl[idx]);
00083     }
00084   } else {
00085     if ((tmp = y - x) < LOG_ADDMIN) return x;
00086     else {
00087       idx =(unsigned int)((- tmp) * TMAG + 0.5);
00088       /* jlog("%f == %f\n",tbl[idx],log(1 + exp(tmp))); */
00089       return (x + tbl[idx]);
00090     }
00091   }
00092 }
00093 
00102 LOGPROB
00103 addlog_array(LOGPROB *a, int n)
00104 {
00105   LOGPROB tmp;
00106   LOGPROB x,y;
00107   unsigned int idx;
00108 
00109   y = LOG_ZERO;
00110   for(n--; n >= 0; n--) {
00111     x = a[n];
00112     if (x > y) {
00113       tmp = x; x = y; y = tmp;
00114     }
00115     /* always y >= x */
00116     if ((tmp = x - y) < LOG_ADDMIN) continue;
00117     else {
00118       idx = (unsigned int)((- tmp) * TMAG + 0.5);
00119       y += tbl[idx];
00120     }
00121   }
00122   return(y);
00123 }