LogGamma.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.gamma;

  18. /**
  19.  * Natural logarithm of the absolute value of \( \Gamma(x) \).
  20.  *
  21.  * <p>\[ \operatorname{lgamma}(z) = \ln \lvert \Gamma(x) \rvert \]
  22.  *
  23.  * <p>This code has been adapted from the <a href="https://www.boost.org/">Boost</a>
  24.  * {@code c++} implementation {@code <boost/math/special_functions/gamma.hpp>}.
  25.  *
  26.  * @see
  27.  * <a href="https://www.boost.org/doc/libs/1_77_0/libs/math/doc/html/math_toolkit/sf_gamma/lgamma.html">
  28.  * Boost C++ Log Gamma functions</a>
  29.  */
  30. public final class LogGamma {
  31.     /** Private constructor. */
  32.     private LogGamma() {
  33.         // intentionally empty.
  34.     }

  35.     /**
  36.      * Computes the function \( \ln \lvert \Gamma(x) \rvert \), the natural
  37.      * logarithm of the absolute value of \( \Gamma(x) \).
  38.      *
  39.      * @param x Argument.
  40.      * @return \( \ln \lvert \Gamma(x) \rvert \), or {@code NaN} if {@code x <= 0}
  41.      * and is an integer.
  42.      */
  43.     public static double value(double x) {
  44.         return BoostGamma.lgamma(x);
  45.     }

  46.     /**
  47.      * Computes the function \( \ln \lvert \Gamma(x) \rvert \), the natural
  48.      * logarithm of the absolute value of \( \Gamma(x) \).
  49.      *
  50.      * <p>The sign output is set to 1 if the sign of gamma(x) is positive or zero;
  51.      * otherwise it is set to -1.
  52.      *
  53.      * @param x Argument.
  54.      * @param sign Sign output. If a non-zero length the first index {@code sign[0]} is
  55.      * set on output to the sign of gamma(z).
  56.      * @return \( \ln \lvert \Gamma(x) \rvert \), or {@code NaN} if {@code x <= 0}
  57.      * and is an integer.
  58.      * @since 1.1
  59.      */
  60.     public static double value(double x, int[] sign) {
  61.         return BoostGamma.lgamma(x, sign);
  62.     }
  63. }