RegularizedGamma.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.  * <a href="https://mathworld.wolfram.com/RegularizedGammaFunction.html">
  20.  * Regularized Gamma functions</a>.
  21.  *
  22.  * <p>By definition, the lower and upper regularized gamma functions satisfy:
  23.  *
  24.  * <p>\[ 1 = P(a, x) + Q(a, x) \]
  25.  *
  26.  * <p>This code has been adapted from the <a href="https://www.boost.org/">Boost</a>
  27.  * {@code c++} implementation {@code <boost/math/special_functions/gamma.hpp>}.
  28.  *
  29.  * @see
  30.  * <a href="https://www.boost.org/doc/libs/1_77_0/libs/math/doc/html/math_toolkit/sf_gamma/igamma.html">
  31.  * Boost C++ Incomplete Gamma functions</a>
  32.  */
  33. public final class RegularizedGamma {
  34.     /** Private constructor. */
  35.     private RegularizedGamma() {
  36.         // intentionally empty.
  37.     }

  38.     /**
  39.      * <a href="http://mathworld.wolfram.com/RegularizedGammaFunction.html">
  40.      * Lower regularized Gamma function</a> \( P(a, x) \).
  41.      *
  42.      * <p>\[ P(a,x) = 1 - Q(a,x) = \frac{\gamma(a,x)}{\Gamma(a)} = \frac{1}{\Gamma(a)} \int_0^x t^{a-1}\,e^{-t}\,dt \]
  43.      */
  44.     public static final class P {
  45.         /** Prevent instantiation. */
  46.         private P() {}

  47.         /**
  48.          * Computes the lower regularized gamma function \( P(a, x) \).
  49.          *
  50.          * @param a Argument.
  51.          * @param x Argument.
  52.          * @return \( P(a, x) \).
  53.          * @throws ArithmeticException if the continued fraction fails to converge.
  54.          */
  55.         public static double value(double a,
  56.                                    double x) {
  57.             return BoostGamma.gammaP(a, x);
  58.         }

  59.         /**
  60.          * Computes the lower regularized gamma function \( P(a, x) \).
  61.          *
  62.          * @param a Argument.
  63.          * @param x Argument.
  64.          * @param epsilon Tolerance in series evaluation.
  65.          * @param maxIterations Maximum number of iterations in series evaluation.
  66.          * @return \( P(a, x) \).
  67.          * @throws ArithmeticException if the series evaluation fails to converge.
  68.          */
  69.         public static double value(double a,
  70.                                    double x,
  71.                                    double epsilon,
  72.                                    int maxIterations) {
  73.             return BoostGamma.gammaP(a, x, new Policy(epsilon, maxIterations));
  74.         }

  75.         /**
  76.          * Computes the derivative of the lower regularized gamma function \( P(a, x) \).
  77.          *
  78.          * <p>\[ \frac{\delta}{\delta x} P(a,x) = \frac{e^{-x} x^{a-1}}{\Gamma(a)} \]
  79.          *
  80.          * <p>This function has uses in some statistical distributions.
  81.          *
  82.          * @param a Argument.
  83.          * @param x Argument.
  84.          * @return derivative of \( P(a,x) \) with respect to x.
  85.          * @since 1.1
  86.          */
  87.         public static double derivative(double a,
  88.                                         double x) {
  89.             return BoostGamma.gammaPDerivative(a, x);
  90.         }
  91.     }

  92.     /**
  93.      * <a href="http://mathworld.wolfram.com/RegularizedGammaFunction.html">
  94.      * Upper regularized Gamma function</a> \( Q(a, x) \).
  95.      *
  96.      * <p>\[ Q(a,x) = 1 - P(a,x) = \frac{\Gamma(a,x)}{\Gamma(a)} = \frac{1}{\Gamma(a)} \int_x^{\infty} t^{a-1}\,e^{-t}\,dt \]
  97.      */
  98.     public static final class Q {
  99.         /** Prevent instantiation. */
  100.         private Q() {}

  101.         /**
  102.          * Computes the upper regularized gamma function \( Q(a, x) \).
  103.          *
  104.          * @param a Argument.
  105.          * @param x Argument.
  106.          * @return \( Q(a, x) \).
  107.          * @throws ArithmeticException if the series evaluation fails to converge.
  108.          */
  109.         public static double value(double a,
  110.                                    double x) {
  111.             return BoostGamma.gammaQ(a, x);
  112.         }

  113.         /**
  114.          * Computes the upper regularized gamma function \( Q(a, x) \).
  115.          *
  116.          * @param a Argument.
  117.          * @param x Argument.
  118.          * @param epsilon Tolerance in series evaluation.
  119.          * @param maxIterations Maximum number of iterations in series evaluation.
  120.          * @return \( Q(a, x) \).
  121.          * @throws ArithmeticException if the series evaluation fails to converge.
  122.          */
  123.         public static double value(final double a,
  124.                                    double x,
  125.                                    double epsilon,
  126.                                    int maxIterations) {
  127.             return BoostGamma.gammaQ(a, x, new Policy(epsilon, maxIterations));
  128.         }

  129.         /**
  130.          * Computes the derivative of the upper regularized gamma function \( Q(a, x) \).
  131.          *
  132.          * <p>\[ \frac{\delta}{\delta x} Q(a,x) = -\frac{e^{-x} x^{a-1}}{\Gamma(a)} \]
  133.          *
  134.          * <p>This function has uses in some statistical distributions.
  135.          *
  136.          * @param a Argument.
  137.          * @param x Argument.
  138.          * @return derivative of \( Q(a,x) \) with respect to x.
  139.          * @since 1.1
  140.          */
  141.         public static double derivative(double a,
  142.                                         double x) {
  143.             return -BoostGamma.gammaPDerivative(a, x);
  144.         }
  145.     }
  146. }