logmath.h File Reference

Fast integer logarithmic addition operations. More...

`#include <sphinxbase/sphinxbase_export.h>`
`#include <sphinxbase/prim_type.h>`
`#include <sphinxbase/cmd_ln.h>`

Go to the source code of this file.

## Data Structures

struct | logadd_s

## Macros

#define | LOGMATH_TABLE(lm)
| Obtain the log-add table from a logmath_t *.

## Typedefs

typedef struct logadd_s | logadd_t
| Integer log math computation table.
typedef struct logmath_s | logmath_t
| Integer log math computation class.

## Functions

SPHINXBASE_EXPORT logmath_t * | logmath_init (float64 base, int shift, int use_table)
| Initialize a log math computation table.
SPHINXBASE_EXPORT logmath_t * | logmath_read (const char *filename)
| Memory-map (or read) a log table from a file.
SPHINXBASE_EXPORT int32 | logmath_write (logmath_t *lmath, const char *filename)
| Write a log table to a file.
SPHINXBASE_EXPORT int32 | logmath_get_table_shape (logmath_t *lmath, uint32 *out_size, uint32 *out_width, uint32 *out_shift)
| Get the log table size and dimensions.
SPHINXBASE_EXPORT float64 | logmath_get_base (logmath_t *lmath)
| Get the log base.
SPHINXBASE_EXPORT int | logmath_get_zero (logmath_t *lmath)
| Get the smallest possible value represented in this base.
SPHINXBASE_EXPORT int | logmath_get_width (logmath_t *lmath)
| Get the width of the values in a log table.
SPHINXBASE_EXPORT int | logmath_get_shift (logmath_t *lmath)
| Get the shift of the values in a log table.
SPHINXBASE_EXPORT logmath_t * | logmath_retain (logmath_t *lmath)
| Retain ownership of a log table.
SPHINXBASE_EXPORT int | logmath_free (logmath_t *lmath)
| Free a log table.
SPHINXBASE_EXPORT int | logmath_add_exact (logmath_t *lmath, int logb_p, int logb_q)
| Add two values in log space exactly and slowly (without using add table).
SPHINXBASE_EXPORT int | logmath_add (logmath_t *lmath, int logb_p, int logb_q)
| Add two values in log space (i.e.
SPHINXBASE_EXPORT int | logmath_log (logmath_t *lmath, float64 p)
| Convert linear floating point number to integer log in base B.
SPHINXBASE_EXPORT float64 | logmath_exp (logmath_t *lmath, int logb_p)
| Convert integer log in base B to linear floating point.
SPHINXBASE_EXPORT int | logmath_ln_to_log (logmath_t *lmath, float64 log_p)
| Convert natural log (in floating point) to integer log in base B.
SPHINXBASE_EXPORT float64 | logmath_log_to_ln (logmath_t *lmath, int logb_p)
| Convert integer log in base B to natural log (in floating point).
SPHINXBASE_EXPORT int | logmath_log10_to_log (logmath_t *lmath, float64 log_p)
| Convert base 10 log (in floating point) to integer log in base B.
SPHINXBASE_EXPORT float | logmath_log10_to_log_float (logmath_t *lmath, float64 log_p)
| Convert base 10 log (in floating point) to float log in base B.
SPHINXBASE_EXPORT float64 | logmath_log_to_log10 (logmath_t *lmath, int logb_p)
| Convert integer log in base B to base 10 log (in floating point).
SPHINXBASE_EXPORT float64 | logmath_log_float_to_log10 (logmath_t *lmath, float log_p)
| Convert float log in base B to base 10 log.

## Detailed Description

Fast integer logarithmic addition operations.

In evaluating HMM 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:

So:

- when
- when
- where

The first entry in logadd_table is simply , for the case where and thus . The last entry is zero, where 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.

Definition in file logmath.h.

## Macro Definition Documentation

## LOGMATH_TABLE

#define LOGMATH_TABLE | ( |  | lm | ) |

Value:
((logadd_t *)lm)
logadd_t
struct logadd_s logadd_t
Integer log math computation table.
Definition logmath.h:93

Obtain the log-add table from a logmath_t *.

Definition at line 113 of file logmath.h.

Referenced by logmath_add().

## Typedef Documentation

## logadd_t

typedef struct logadd_s 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.

## logmath_t

typedef struct logmath_s logmath_t

Integer log math computation class.

Definition at line 108 of file logmath.h.

## Function Documentation

## logmath_add()

SPHINXBASE_EXPORT int logmath_add | ( | logmath_t * | lmath,
|  | int | logb_p,
|  | int | logb_q )

Add two values in log space (i.e.

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

Definition at line 392 of file logmath.c.

References logmath_add(), logmath_add_exact(), LOGMATH_TABLE, logadd_s::table, logadd_s::table_size, and logadd_s::width.

Referenced by logmath_add().

## logmath_add_exact()

SPHINXBASE_EXPORT int logmath_add_exact | ( | logmath_t * | lmath,
|  | int | logb_p,
|  | int | logb_q )

Add two values in log space exactly and slowly (without using add table).

Definition at line 439 of file logmath.c.

References logmath_add_exact(), logmath_exp(), and logmath_log().

Referenced by logmath_add(), and logmath_add_exact().

## logmath_exp()

SPHINXBASE_EXPORT float64 logmath_exp | ( | logmath_t * | lmath,
|  | int | logb_p )

Convert integer log in base B to linear floating point.

Definition at line 456 of file logmath.c.

References logmath_exp(), and logadd_s::shift.

Referenced by logmath_add_exact(), logmath_exp(), and ngram_model_set_remove().

## logmath_free()

SPHINXBASE_EXPORT int logmath_free | ( | logmath_t * | lmath | ) |

Free a log table.

Returns
  new reference count (0 if freed completely)

Definition at line 342 of file logmath.c.

References ckd_free(), logmath_free(), mmio_file_unmap(), and logadd_s::table.

Referenced by jsgf_write_fsg(), logmath_free(), and logmath_read().

## logmath_get_base()

SPHINXBASE_EXPORT float64 logmath_get_base | ( | logmath_t * | lmath | ) |

Get the log base.

Definition at line 368 of file logmath.c.

References logmath_get_base().

Referenced by logmath_get_base(), and ngram_model_set_init().

## logmath_get_shift()

SPHINXBASE_EXPORT int logmath_get_shift | ( | logmath_t * | lmath | ) |

Get the shift of the values in a log table.

Definition at line 386 of file logmath.c.

References logmath_get_shift(), and logadd_s::shift.

Referenced by logmath_get_shift(), and ngram_model_set_init().

## logmath_get_table_shape()

SPHINXBASE_EXPORT int32 logmath_get_table_shape | ( | logmath_t * | lmath,
|  | uint32 * | out_size,
|  | uint32 * | out_width,
|  | uint32 * | out_shift )

Get the log table size and dimensions.

Definition at line 357 of file logmath.c.

References logmath_get_table_shape(), logadd_s::shift, logadd_s::table_size, and logadd_s::width.

Referenced by logmath_get_table_shape().

## logmath_get_width()

SPHINXBASE_EXPORT int logmath_get_width | ( | logmath_t * | lmath | ) |

Get the width of the values in a log table.

Definition at line 380 of file logmath.c.

References logmath_get_width(), and logadd_s::width.

Referenced by logmath_get_width().

## logmath_get_zero()

SPHINXBASE_EXPORT int logmath_get_zero | ( | logmath_t * | lmath | ) |

Get the smallest possible value represented in this base.

Definition at line 374 of file logmath.c.

References logmath_get_zero().

Referenced by logmath_get_zero().

## logmath_init()

SPHINXBASE_EXPORT logmath_t * logmath_init | ( | float64 | base,
|  | int | shift,
|  | int | use_table )

Initialize a log math computation table.

Parameters

  base | The base B in which computation is to be done.
  shift | Log values are shifted right by this many bits.
  use_table | Whether to use an add table or not

Returns
  The newly created log math table.

Definition at line 62 of file logmath.c.

References ckd_calloc, E_ERROR, logmath_init(), logadd_s::shift, logadd_s::table, logadd_s::table_size, and logadd_s::width.

Referenced by jsgf_write_fsg(), and logmath_init().

## logmath_ln_to_log()

SPHINXBASE_EXPORT int logmath_ln_to_log | ( | logmath_t * | lmath,
|  | float64 | log_p )

Convert natural log (in floating point) to integer log in base B.

Definition at line 462 of file logmath.c.

References logmath_ln_to_log(), and logadd_s::shift.

Referenced by logmath_ln_to_log().

## logmath_log()

SPHINXBASE_EXPORT int logmath_log | ( | logmath_t * | lmath,
|  | float64 | p )

Convert linear floating point number to integer log in base B.

Definition at line 447 of file logmath.c.

References logmath_log(), and logadd_s::shift.

Referenced by logmath_add_exact(), logmath_log(), ngram_model_add_class_word(), ngram_model_add_word(), ngram_model_set_add(), ngram_model_set_init(), ngram_model_set_interp(), and ngram_model_set_remove().

## logmath_log10_to_log()

SPHINXBASE_EXPORT int logmath_log10_to_log | ( | logmath_t * | lmath,
|  | float64 | log_p )

Convert base 10 log (in floating point) to integer log in base B.

Definition at line 474 of file logmath.c.

References logmath_log10_to_log(), and logadd_s::shift.

Referenced by logmath_log10_to_log().

## logmath_log10_to_log_float()

SPHINXBASE_EXPORT float logmath_log10_to_log_float | ( | logmath_t * | lmath,
|  | float64 | log_p )

Convert base 10 log (in floating point) to float log in base B.

Definition at line 480 of file logmath.c.

References logmath_log10_to_log_float(), and logadd_s::shift.

Referenced by logmath_log10_to_log_float().

## logmath_log_float_to_log10()

SPHINXBASE_EXPORT float64 logmath_log_float_to_log10 | ( | logmath_t * | lmath,
|  | float | log_p )

Convert float log in base B to base 10 log.

Definition at line 496 of file logmath.c.

References logmath_log_float_to_log10(), and logadd_s::shift.

Referenced by logmath_log_float_to_log10().

## logmath_log_to_ln()

SPHINXBASE_EXPORT float64 logmath_log_to_ln | ( | logmath_t * | lmath,
|  | int | logb_p )

Convert integer log in base B to natural log (in floating point).

Definition at line 468 of file logmath.c.

References logmath_log_to_ln(), and logadd_s::shift.

Referenced by logmath_log_to_ln().

## logmath_log_to_log10()

SPHINXBASE_EXPORT float64 logmath_log_to_log10 | ( | logmath_t * | lmath,
|  | int | logb_p )

Convert integer log in base B to base 10 log (in floating point).

Definition at line 490 of file logmath.c.

References logmath_log_to_log10(), and logadd_s::shift.

Referenced by logmath_log_to_log10().

## logmath_read()

SPHINXBASE_EXPORT logmath_t * logmath_read | ( | const char * | filename | ) |

Memory-map (or read) a log table from a file.

Definition at line 164 of file logmath.c.

References atof_c(), bio_fread(), bio_hdrarg_free(), bio_readhdr(), bio_verify_chksum(), ckd_calloc, E_ERROR, E_ERROR_SYSTEM, E_INFO, E_WARN, logmath_free(), logmath_read(), mmio_file_ptr(), mmio_file_read(), logadd_s::shift, logadd_s::table, logadd_s::table_size, and logadd_s::width.

Referenced by logmath_read().

## logmath_retain()

SPHINXBASE_EXPORT logmath_t * logmath_retain | ( | logmath_t * | lmath | ) |

Retain ownership of a log table.

Returns
  pointer to retained log table.

Definition at line 335 of file logmath.c.

References logmath_retain().

Referenced by logmath_retain().

## logmath_write()

SPHINXBASE_EXPORT int32 logmath_write | ( | logmath_t * | lmath,
|  | const char * | filename )

Write a log table to a file.

Definition at line 272 of file logmath.c.

References bio_fwrite(), E_ERROR, E_ERROR_SYSTEM, E_INFO, logmath_write(), logadd_s::shift, logadd_s::table, logadd_s::table_size, and logadd_s::width.

Referenced by logmath_write().