Class LogFactorial


  • public final class LogFactorial
    extends java.lang.Object
    Class for computing the natural logarithm of the factorial of a number. It allows to allocate a cache of precomputed values. In case of cache miss, computation is performed by a call to LogGamma.value(double).
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static int FACTORIALS_CACHE_SIZE
      Size of precomputed factorials.
      private double[] logFactorials
      Precomputed values of the function: logFactorials[i] = Math.log(i!).
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private LogFactorial​(int numValues, double[] cache)
      Creates an instance, reusing the already computed values if available.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static LogFactorial create()
      Creates an instance with no precomputed values.
      double value​(int n)
      Computes \( log_e(n!) \).
      LogFactorial withCache​(int cacheSize)
      Creates an instance with the specified cache size.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • logFactorials

        private final double[] logFactorials
        Precomputed values of the function: logFactorials[i] = Math.log(i!).
    • Constructor Detail

      • LogFactorial

        private LogFactorial​(int numValues,
                             double[] cache)
        Creates an instance, reusing the already computed values if available.
        Parameters:
        numValues - Number of values of the function to compute.
        cache - Cached values.
        Throws:
        java.lang.IllegalArgumentException - if n < 0.
    • Method Detail

      • create

        public static LogFactorial create()
        Creates an instance with no precomputed values.
        Returns:
        instance with no precomputed values
      • withCache

        public LogFactorial withCache​(int cacheSize)
        Creates an instance with the specified cache size.
        Parameters:
        cacheSize - Number of precomputed values of the function.
        Returns:
        a new instance where cacheSize values have been precomputed.
        Throws:
        java.lang.IllegalArgumentException - if cacheSize < 0.
      • value

        public double value​(int n)
        Computes \( log_e(n!) \).
        Parameters:
        n - Argument.
        Returns:
        log(n!).
        Throws:
        java.lang.IllegalArgumentException - if n < 0.