\section{include/sphinxbase/logmath.h File Reference}
\label{logmath_8h}\index{include/sphinxbase/logmath.\+h@{include/sphinxbase/logmath.\+h}}


Fast integer logarithmic addition operations.  


{\ttfamily \#include $<$sphinxbase/sphinxbase\+\_\+export.\+h$>$}\newline
{\ttfamily \#include $<$sphinxbase/prim\+\_\+type.\+h$>$}\newline
{\ttfamily \#include $<$sphinxbase/cmd\+\_\+ln.\+h$>$}\newline
\subsection*{Data Structures}
\begin{DoxyCompactItemize}
\item 
struct \textbf{ logadd\+\_\+s}
\end{DoxyCompactItemize}
\subsection*{Macros}
\begin{DoxyCompactItemize}
\item 
\mbox{\label{logmath_8h_ae5c5ce106e9f8f1e763d419de53317a8}} 
\#define \textbf{ L\+O\+G\+M\+A\+T\+H\+\_\+\+T\+A\+B\+LE}(lm)~((\textbf{ logadd\+\_\+t} $\ast$)lm)
\begin{DoxyCompactList}\small\item\em Obtain the log-\/add table from a logmath\+\_\+t $\ast$. \end{DoxyCompactList}\end{DoxyCompactItemize}
\subsection*{Typedefs}
\begin{DoxyCompactItemize}
\item 
typedef struct \textbf{ logadd\+\_\+s} \textbf{ logadd\+\_\+t}
\begin{DoxyCompactList}\small\item\em Integer log math computation table. \end{DoxyCompactList}\item 
\mbox{\label{logmath_8h_ae613aa7db1dd40ff56a80a7dadb22cc8}} 
typedef struct \textbf{ logmath\+\_\+s} \textbf{ logmath\+\_\+t}
\begin{DoxyCompactList}\small\item\em Integer log math computation class. \end{DoxyCompactList}\end{DoxyCompactItemize}
\subsection*{Functions}
\begin{DoxyCompactItemize}
\item 
S\+P\+H\+I\+N\+X\+B\+A\+S\+E\+\_\+\+E\+X\+P\+O\+RT \textbf{ logmath\+\_\+t} $\ast$ \textbf{ logmath\+\_\+init} (float64 base, int shift, int use\+\_\+table)
\begin{DoxyCompactList}\small\item\em Initialize a log math computation table. \end{DoxyCompactList}\item 
\mbox{\label{logmath_8h_afbed298ba0bc4736415d78880fe5c7c2}} 
S\+P\+H\+I\+N\+X\+B\+A\+S\+E\+\_\+\+E\+X\+P\+O\+RT \textbf{ logmath\+\_\+t} $\ast$ \textbf{ logmath\+\_\+read} (const char $\ast$filename)
\begin{DoxyCompactList}\small\item\em Memory-\/map (or read) a log table from a file. \end{DoxyCompactList}\item 
\mbox{\label{logmath_8h_a787070f5e689878348ef219245fc7c44}} 
S\+P\+H\+I\+N\+X\+B\+A\+S\+E\+\_\+\+E\+X\+P\+O\+RT int32 \textbf{ logmath\+\_\+write} (\textbf{ logmath\+\_\+t} $\ast$lmath, const char $\ast$filename)
\begin{DoxyCompactList}\small\item\em Write a log table to a file. \end{DoxyCompactList}\item 
\mbox{\label{logmath_8h_a3280500e77bf27aa7cda23268b8eb8b3}} 
S\+P\+H\+I\+N\+X\+B\+A\+S\+E\+\_\+\+E\+X\+P\+O\+RT int32 \textbf{ logmath\+\_\+get\+\_\+table\+\_\+shape} (\textbf{ logmath\+\_\+t} $\ast$lmath, uint32 $\ast$out\+\_\+size, uint32 $\ast$out\+\_\+width, uint32 $\ast$out\+\_\+shift)
\begin{DoxyCompactList}\small\item\em Get the log table size and dimensions. \end{DoxyCompactList}\item 
\mbox{\label{logmath_8h_a6114206ec0321d7015c42fc7b81cb83e}} 
S\+P\+H\+I\+N\+X\+B\+A\+S\+E\+\_\+\+E\+X\+P\+O\+RT float64 \textbf{ logmath\+\_\+get\+\_\+base} (\textbf{ logmath\+\_\+t} $\ast$lmath)
\begin{DoxyCompactList}\small\item\em Get the log base. \end{DoxyCompactList}\item 
\mbox{\label{logmath_8h_a1c160c28a9e7d25923f391773b1028c0}} 
S\+P\+H\+I\+N\+X\+B\+A\+S\+E\+\_\+\+E\+X\+P\+O\+RT int \textbf{ logmath\+\_\+get\+\_\+zero} (\textbf{ logmath\+\_\+t} $\ast$lmath)
\begin{DoxyCompactList}\small\item\em Get the smallest possible value represented in this base. \end{DoxyCompactList}\item 
\mbox{\label{logmath_8h_a0cd690d2a414aebb5e126b8cabbfabde}} 
S\+P\+H\+I\+N\+X\+B\+A\+S\+E\+\_\+\+E\+X\+P\+O\+RT int \textbf{ logmath\+\_\+get\+\_\+width} (\textbf{ logmath\+\_\+t} $\ast$lmath)
\begin{DoxyCompactList}\small\item\em Get the width of the values in a log table. \end{DoxyCompactList}\item 
\mbox{\label{logmath_8h_aed009aca9736612bebdda57444ec63a6}} 
S\+P\+H\+I\+N\+X\+B\+A\+S\+E\+\_\+\+E\+X\+P\+O\+RT int \textbf{ logmath\+\_\+get\+\_\+shift} (\textbf{ logmath\+\_\+t} $\ast$lmath)
\begin{DoxyCompactList}\small\item\em Get the shift of the values in a log table. \end{DoxyCompactList}\item 
S\+P\+H\+I\+N\+X\+B\+A\+S\+E\+\_\+\+E\+X\+P\+O\+RT \textbf{ logmath\+\_\+t} $\ast$ \textbf{ logmath\+\_\+retain} (\textbf{ logmath\+\_\+t} $\ast$lmath)
\begin{DoxyCompactList}\small\item\em Retain ownership of a log table. \end{DoxyCompactList}\item 
S\+P\+H\+I\+N\+X\+B\+A\+S\+E\+\_\+\+E\+X\+P\+O\+RT int \textbf{ logmath\+\_\+free} (\textbf{ logmath\+\_\+t} $\ast$lmath)
\begin{DoxyCompactList}\small\item\em Free a log table. \end{DoxyCompactList}\item 
\mbox{\label{logmath_8h_a61bf79c70a38f00ca060b69b0efd7489}} 
S\+P\+H\+I\+N\+X\+B\+A\+S\+E\+\_\+\+E\+X\+P\+O\+RT int \textbf{ logmath\+\_\+add\+\_\+exact} (\textbf{ logmath\+\_\+t} $\ast$lmath, int logb\+\_\+p, int logb\+\_\+q)
\begin{DoxyCompactList}\small\item\em Add two values in log space exactly and slowly (without using add table). \end{DoxyCompactList}\item 
S\+P\+H\+I\+N\+X\+B\+A\+S\+E\+\_\+\+E\+X\+P\+O\+RT int \textbf{ logmath\+\_\+add} (\textbf{ logmath\+\_\+t} $\ast$lmath, int logb\+\_\+p, int logb\+\_\+q)
\begin{DoxyCompactList}\small\item\em Add two values in log space (i.\+e. \end{DoxyCompactList}\item 
\mbox{\label{logmath_8h_aebb4711268322fa7aec31e5798fe7e90}} 
S\+P\+H\+I\+N\+X\+B\+A\+S\+E\+\_\+\+E\+X\+P\+O\+RT int \textbf{ logmath\+\_\+log} (\textbf{ logmath\+\_\+t} $\ast$lmath, float64 p)
\begin{DoxyCompactList}\small\item\em Convert linear floating point number to integer log in base B. \end{DoxyCompactList}\item 
\mbox{\label{logmath_8h_ae8b0a168e29e448c0d6de66dc46e099e}} 
S\+P\+H\+I\+N\+X\+B\+A\+S\+E\+\_\+\+E\+X\+P\+O\+RT float64 \textbf{ logmath\+\_\+exp} (\textbf{ logmath\+\_\+t} $\ast$lmath, int logb\+\_\+p)
\begin{DoxyCompactList}\small\item\em Convert integer log in base B to linear floating point. \end{DoxyCompactList}\item 
\mbox{\label{logmath_8h_a52eff2c778ad758888b03ac5efcdccea}} 
S\+P\+H\+I\+N\+X\+B\+A\+S\+E\+\_\+\+E\+X\+P\+O\+RT int \textbf{ logmath\+\_\+ln\+\_\+to\+\_\+log} (\textbf{ logmath\+\_\+t} $\ast$lmath, float64 log\+\_\+p)
\begin{DoxyCompactList}\small\item\em Convert natural log (in floating point) to integer log in base B. \end{DoxyCompactList}\item 
\mbox{\label{logmath_8h_a8035e176636eae8b4e02fe488f25457a}} 
S\+P\+H\+I\+N\+X\+B\+A\+S\+E\+\_\+\+E\+X\+P\+O\+RT float64 \textbf{ logmath\+\_\+log\+\_\+to\+\_\+ln} (\textbf{ logmath\+\_\+t} $\ast$lmath, int logb\+\_\+p)
\begin{DoxyCompactList}\small\item\em Convert integer log in base B to natural log (in floating point). \end{DoxyCompactList}\item 
\mbox{\label{logmath_8h_aacb4dddeed63a61fb927915f7e3a642e}} 
S\+P\+H\+I\+N\+X\+B\+A\+S\+E\+\_\+\+E\+X\+P\+O\+RT int \textbf{ logmath\+\_\+log10\+\_\+to\+\_\+log} (\textbf{ logmath\+\_\+t} $\ast$lmath, float64 log\+\_\+p)
\begin{DoxyCompactList}\small\item\em Convert base 10 log (in floating point) to integer log in base B. \end{DoxyCompactList}\item 
\mbox{\label{logmath_8h_a7c17cb624003975e84fbd141ca6e2e06}} 
S\+P\+H\+I\+N\+X\+B\+A\+S\+E\+\_\+\+E\+X\+P\+O\+RT float64 \textbf{ logmath\+\_\+log\+\_\+to\+\_\+log10} (\textbf{ logmath\+\_\+t} $\ast$lmath, int logb\+\_\+p)
\begin{DoxyCompactList}\small\item\em Convert integer log in base B to base 10 log (in floating point). \end{DoxyCompactList}\end{DoxyCompactItemize}


\subsection{Detailed Description}
Fast integer logarithmic addition operations. 

In evaluating H\+MM models, probability values are often kept in log domain, to avoid overflow. To enable these logprob values to be held in int32 variables without significant loss of precision, a logbase of (1+epsilon) (where epsilon $<$ 0.\+01 or so) is used. This module maintains this logbase (B).

However, maintaining probabilities in log domain creates a problem when adding two probability values. This problem can be solved by table lookup. Note that\+:


\begin{DoxyItemize}
\item $ b^z = b^x + b^y $
\item $ b^z = b^x(1 + b^{y-x}) = b^y(1 + e^{x-y}) $
\item $ z = x + log_b(1 + b^{y-x}) = y + log_b(1 + b^{x-y}) $
\end{DoxyItemize}

So\+:


\begin{DoxyItemize}
\item when $ y > x, z = y + logadd\_table[-(x-y)] $
\item when $ x > y, z = x + logadd\_table[-(y-x)] $
\item where $ logadd\_table[n] = log_b(1 + b^{-n}) $
\end{DoxyItemize}

The first entry in {\itshape logadd\+\_\+table} is simply $ log_b(2.0) $, for the case where $ y = x $ and thus $ z = log_b(2x) = log_b(2) + x $. The last entry is zero, where $ log_b(x+y) = x = y $ due to loss of precision.

Since this table can be quite large particularly for small logbases, an option is provided to compress it by dropping the least significant bits of the table. 

\subsection{Typedef Documentation}
\mbox{\label{logmath_8h_a8c04c94e2c6364f6cf3b649eb4ce5bfd}} 
\index{logmath.\+h@{logmath.\+h}!logadd\+\_\+t@{logadd\+\_\+t}}
\index{logadd\+\_\+t@{logadd\+\_\+t}!logmath.\+h@{logmath.\+h}}
\subsubsection{logadd\+\_\+t}
{\footnotesize\ttfamily typedef struct \textbf{ logadd\+\_\+s} \textbf{ logadd\+\_\+t}}



Integer log math computation table. 

This is exposed here to allow log-\/add computations to be inlined. 

Definition at line 93 of file logmath.\+h.



\subsection{Function Documentation}
\mbox{\label{logmath_8h_a5eb70928578b0115c9c7ac2765396a06}} 
\index{logmath.\+h@{logmath.\+h}!logmath\+\_\+add@{logmath\+\_\+add}}
\index{logmath\+\_\+add@{logmath\+\_\+add}!logmath.\+h@{logmath.\+h}}
\subsubsection{logmath\+\_\+add()}
{\footnotesize\ttfamily S\+P\+H\+I\+N\+X\+B\+A\+S\+E\+\_\+\+E\+X\+P\+O\+RT int logmath\+\_\+add (\begin{DoxyParamCaption}\item[{\textbf{ logmath\+\_\+t} $\ast$}]{lmath,  }\item[{int}]{logb\+\_\+p,  }\item[{int}]{logb\+\_\+q }\end{DoxyParamCaption})}



Add two values in log space (i.\+e. 

return log(exp(p)+exp(q))) 

Definition at line 392 of file logmath.\+c.



References L\+O\+G\+M\+A\+T\+H\+\_\+\+T\+A\+B\+LE, and logadd\+\_\+s\+::table.

\mbox{\label{logmath_8h_a97865ef8bc1e8e2525a2329e0627ecfb}} 
\index{logmath.\+h@{logmath.\+h}!logmath\+\_\+free@{logmath\+\_\+free}}
\index{logmath\+\_\+free@{logmath\+\_\+free}!logmath.\+h@{logmath.\+h}}
\subsubsection{logmath\+\_\+free()}
{\footnotesize\ttfamily S\+P\+H\+I\+N\+X\+B\+A\+S\+E\+\_\+\+E\+X\+P\+O\+RT int logmath\+\_\+free (\begin{DoxyParamCaption}\item[{\textbf{ logmath\+\_\+t} $\ast$}]{lmath }\end{DoxyParamCaption})}



Free a log table. 

\begin{DoxyReturn}{Returns}
new reference count (0 if freed completely) 
\end{DoxyReturn}


Definition at line 342 of file logmath.\+c.

\mbox{\label{logmath_8h_a5835860c5f6a703c80c0214f816f0b11}} 
\index{logmath.\+h@{logmath.\+h}!logmath\+\_\+init@{logmath\+\_\+init}}
\index{logmath\+\_\+init@{logmath\+\_\+init}!logmath.\+h@{logmath.\+h}}
\subsubsection{logmath\+\_\+init()}
{\footnotesize\ttfamily S\+P\+H\+I\+N\+X\+B\+A\+S\+E\+\_\+\+E\+X\+P\+O\+RT \textbf{ logmath\+\_\+t}$\ast$ logmath\+\_\+init (\begin{DoxyParamCaption}\item[{float64}]{base,  }\item[{int}]{shift,  }\item[{int}]{use\+\_\+table }\end{DoxyParamCaption})}



Initialize a log math computation table. 


\begin{DoxyParams}{Parameters}
{\em base} & The base B in which computation is to be done. \\
\hline
{\em shift} & Log values are shifted right by this many bits. \\
\hline
{\em use\+\_\+table} & Whether to use an add table or not \\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{Returns}
The newly created log math table. 
\end{DoxyReturn}


Definition at line 62 of file logmath.\+c.



References E\+\_\+\+E\+R\+R\+OR.



Referenced by jsgf\+\_\+write\+\_\+fsg().

\mbox{\label{logmath_8h_a1c1b2ba3b137a39e9e835a8f3e27d381}} 
\index{logmath.\+h@{logmath.\+h}!logmath\+\_\+retain@{logmath\+\_\+retain}}
\index{logmath\+\_\+retain@{logmath\+\_\+retain}!logmath.\+h@{logmath.\+h}}
\subsubsection{logmath\+\_\+retain()}
{\footnotesize\ttfamily S\+P\+H\+I\+N\+X\+B\+A\+S\+E\+\_\+\+E\+X\+P\+O\+RT \textbf{ logmath\+\_\+t}$\ast$ logmath\+\_\+retain (\begin{DoxyParamCaption}\item[{\textbf{ logmath\+\_\+t} $\ast$}]{lmath }\end{DoxyParamCaption})}



Retain ownership of a log table. 

\begin{DoxyReturn}{Returns}
pointer to retained log table. 
\end{DoxyReturn}


Definition at line 335 of file logmath.\+c.

