LogFactorial.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.numbers.combinatorics;

  18. import org.apache.commons.numbers.gamma.LogGamma;

  19. /**
  20.  * Class for computing the natural logarithm of the factorial of a number.
  21.  * It allows to allocate a cache of precomputed values.
  22.  * In case of cache miss, computation is performed by a call to
  23.  * {@link LogGamma#value(double)}.
  24.  */
  25. public final class LogFactorial {
  26.     /**
  27.      * Size of precomputed factorials.
  28.      * @see Factorial
  29.      */
  30.     private static final int FACTORIALS_CACHE_SIZE = 21;
  31.     /**
  32.      * Precomputed values of the function: {@code logFactorials[i] = Math.log(i!)}.
  33.      */
  34.     private final double[] logFactorials;

  35.     /**
  36.      * Creates an instance, reusing the already computed values if available.
  37.      *
  38.      * @param numValues Number of values of the function to compute.
  39.      * @param cache Cached values.
  40.      * @throws IllegalArgumentException if {@code n < 0}.
  41.      */
  42.     private LogFactorial(int numValues,
  43.                          double[] cache) {
  44.         if (numValues < 0) {
  45.             throw new CombinatoricsException(CombinatoricsException.NEGATIVE, numValues);
  46.         }

  47.         logFactorials = new double[numValues];

  48.         final int beginCopy = 2;
  49.         final int endCopy;
  50.         if (cache == null || cache.length <= beginCopy) {
  51.             endCopy = beginCopy;
  52.         } else {
  53.             endCopy = Math.min(cache.length, numValues);
  54.         }


  55.         // Copy available values.
  56.         if (endCopy - beginCopy > 0) {
  57.             System.arraycopy(cache, beginCopy, logFactorials, beginCopy, endCopy - beginCopy);
  58.         }

  59.         // Precompute.
  60.         for (int i = endCopy; i < numValues; i++) {
  61.             logFactorials[i] = logFactorials[i - 1] + Math.log(i);
  62.         }
  63.     }

  64.     /**
  65.      * Creates an instance with no precomputed values.
  66.      * @return instance with no precomputed values
  67.      */
  68.     public static LogFactorial create() {
  69.         return new LogFactorial(0, null);
  70.     }

  71.     /**
  72.      * Creates an instance with the specified cache size.
  73.      *
  74.      * @param cacheSize Number of precomputed values of the function.
  75.      * @return a new instance where {@code cacheSize} values have been
  76.      * precomputed.
  77.      * @throws IllegalArgumentException if {@code cacheSize < 0}.
  78.      */
  79.     public LogFactorial withCache(final int cacheSize) {
  80.         return new LogFactorial(cacheSize, logFactorials);
  81.     }

  82.     /**
  83.      * Computes \( log_e(n!) \).
  84.      *
  85.      * @param n Argument.
  86.      * @return {@code log(n!)}.
  87.      * @throws IllegalArgumentException if {@code n < 0}.
  88.      */
  89.     public double value(int n) {
  90.         if (n < 0) {
  91.             throw new CombinatoricsException(CombinatoricsException.NEGATIVE, n);
  92.         }

  93.         // Use cache of precomputed values.
  94.         if (n < logFactorials.length) {
  95.             return logFactorials[n];
  96.         }

  97.         // Use cache of precomputed factorial values.
  98.         if (n < FACTORIALS_CACHE_SIZE) {
  99.             return Math.log(Factorial.value(n));
  100.         }

  101.         // Delegate.
  102.         return LogGamma.value(n + 1.0);
  103.     }
  104. }