com.sun.labs.minion.util
Class LogMath

java.lang.Object
  extended by com.sun.labs.minion.util.LogMath
All Implemented Interfaces:
com.sun.labs.util.props.Component, com.sun.labs.util.props.Configurable

public final class LogMath
extends java.lang.Object
implements com.sun.labs.util.props.Configurable

Provides a set of methods for performing simple math in the log domain. The logarithmic base can be set by the SphinxProperty:
edu.cmu.sphinx.util.LogMath.logBase


Field Summary
static java.lang.String PROP_LOG_BASE
          Sphinx property to get the Log base
static java.lang.String PROP_USE_ADD_TABLE
          Sphinx property that controls whether we use the old, slow (but correct) method of performing the LogMath.add by doing the actual computation.
 
Constructor Summary
LogMath()
           
LogMath(float logBase, boolean useAddTable)
           
 
Method Summary
 float addAsLinear(float logVal1, float logVal2)
          Returns the summation of two numbers when the arguments and the result are in log.
 float divideAsLinear(float lv1, float lv2)
           
 float getLogBase()
          Returns the actual log base.
static float getLogOne()
          Returns the one value in the log domain
static float getLogZero()
          Returns the zero value in the log domain
 java.lang.String getName()
           
 float linearToLog(double linearValue)
          Converts the value from linear scale to log scale.
 float lnToLog(float logSource)
          Converts the source, which is a number in base Math.E, to a log value which base is the LogBase of this LogMath.
static float log10(float value)
          Returns the log (base 10) of value
 float log10ToLog(float logSource)
          Converts the source, which is a number in base 10, to a log value which base is the LogBase of this LogMath.
 double logToLinear(float logValue)
          Converts the value from log scale to linear scale.
 float logToLn(float logSource)
          Converts the source, whose base is the LogBase of this LogMath, to a log value which is a number in base Math.E.
static float logToLog(float logSource, float sourceBase, float resultBase)
          Converts the source, which is assumed to be a log value whose base is sourceBase, to a log value whose base is resultBase.
 float multiplyAsLinear(float lv1, float lv2)
           
 void newProperties(com.sun.labs.util.props.PropertySheet ps)
           
 float powerAsLinear(float lv, int x)
           
 float subtractAsLinear(float logMinuend, float logSubtrahend)
          Returns the difference between two numbers when the arguments and the result are in log.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PROP_LOG_BASE

@ConfigDouble(defaultValue=2.718281828459045)
public static final java.lang.String PROP_LOG_BASE
Sphinx property to get the Log base

See Also:
Constant Field Values

PROP_USE_ADD_TABLE

@ConfigBoolean(defaultValue=true)
public static final java.lang.String PROP_USE_ADD_TABLE
Sphinx property that controls whether we use the old, slow (but correct) method of performing the LogMath.add by doing the actual computation.

See Also:
Constant Field Values
Constructor Detail

LogMath

public LogMath()

LogMath

public LogMath(float logBase,
               boolean useAddTable)
Method Detail

newProperties

public void newProperties(com.sun.labs.util.props.PropertySheet ps)
                   throws com.sun.labs.util.props.PropertyException
Specified by:
newProperties in interface com.sun.labs.util.props.Configurable
Throws:
com.sun.labs.util.props.PropertyException

getName

public java.lang.String getName()

addAsLinear

public final float addAsLinear(float logVal1,
                               float logVal2)
Returns the summation of two numbers when the arguments and the result are in log.

That is, it returns log(a + b) given log(a) and log(b)

This method makes use of the equality:

log(a + b) = log(a) + log (1 + exp(log(b) - log(a)))

which is derived from:

a + b = a * (1 + (b / a))

which in turns makes use of:

b / a = exp (log(b) - log(a))

Important to notice that subtractAsLinear(a, b) is *not* the same as addAsLinear(a, -b), since we're in the log domain, and -b is in fact the inverse.

No underflow/overflow check is performed.

Parameters:
logVal1 - value in log domain (i.e. log(val1)) to add
logVal2 - value in log domain (i.e. log(val2)) to add
Returns:
sum of val1 and val2 in the log domain

subtractAsLinear

public final float subtractAsLinear(float logMinuend,
                                    float logSubtrahend)
                             throws java.lang.IllegalArgumentException
Returns the difference between two numbers when the arguments and the result are in log.

That is, it returns log(a - b) given log(a) and log(b)

Implementation is less efficient than add(), since we're less likely to use this function, provided for completeness. Notice however that the result only makes sense if the minuend is higher than the subtrahend. Otherwise, we should return the log of a negative number.

It implements the subtraction as:

log(a - b) = log(a) + log(1 - exp(log(b) - log(a)))

No need to check for underflow/overflow.

Parameters:
logMinuend - value in log domain (i.e. log(minuend)) to be subtracted from
logSubtrahend - value in log domain (i.e. log(subtrahend)) that is being subtracted
Returns:
difference between minuend and the subtrahend in the log domain
Throws:
java.lang.IllegalArgumentException -

This is a very slow way to do this, but this method should rarely be used.


multiplyAsLinear

public float multiplyAsLinear(float lv1,
                              float lv2)

divideAsLinear

public float divideAsLinear(float lv1,
                            float lv2)

powerAsLinear

public float powerAsLinear(float lv,
                           int x)

logToLog

public static float logToLog(float logSource,
                             float sourceBase,
                             float resultBase)
                      throws java.lang.IllegalArgumentException
Converts the source, which is assumed to be a log value whose base is sourceBase, to a log value whose base is resultBase. Possible values for both the source and result bases include Math.E, 10.0, LogMath.getLogBase(). If a source or result base is not supported, an IllegalArgumentException will be thrown.

It takes advantage of the relation:

log_a(b) = log_c(b) / lob_c(a)

or:

log_a(b) = log_c(b) * lob_a(c)

where log_a(b) is logarithm of b base a etc.

Parameters:
logSource - log value whose base is sourceBase
sourceBase - the base of the log the source
resultBase - the base to convert the source log to
Throws:
java.lang.IllegalArgumentException

lnToLog

public final float lnToLog(float logSource)
Converts the source, which is a number in base Math.E, to a log value which base is the LogBase of this LogMath.

Parameters:
logSource - the number in base Math.E to convert

log10ToLog

public final float log10ToLog(float logSource)
Converts the source, which is a number in base 10, to a log value which base is the LogBase of this LogMath.

Parameters:
logSource - the number in base Math.E to convert

logToLn

public final float logToLn(float logSource)
Converts the source, whose base is the LogBase of this LogMath, to a log value which is a number in base Math.E.

Parameters:
logSource - the number to convert to base Math.E

linearToLog

public final float linearToLog(double linearValue)
                        throws java.lang.IllegalArgumentException
Converts the value from linear scale to log scale. The log scale numbers are limited by the range of the type float. The linear scale numbers can be any double value.

Parameters:
linearValue - the value to be converted to log scale
Returns:
the value in log scale
Throws:
java.lang.IllegalArgumentException

logToLinear

public final double logToLinear(float logValue)
Converts the value from log scale to linear scale.

Parameters:
logValue - the value to be converted to the linear scale
Returns:
the value in the linear scale

getLogZero

public static final float getLogZero()
Returns the zero value in the log domain

Returns:
zero value in the log domain

getLogOne

public static final float getLogOne()
Returns the one value in the log domain

Returns:
one value in the log domain

getLogBase

public final float getLogBase()
Returns the actual log base.


log10

public static float log10(float value)
Returns the log (base 10) of value

Parameters:
value - the value to take the log of
Returns:
the log (base 10) of value