001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.numbers.gamma;
018
019/**
020 * Ratio of <a href="https://mathworld.wolfram.com/GammaFunction.html">Gamma
021 * functions</a>.
022 *
023 * <p>\[ \frac{\Gamma(a)}{\Gamma(b)} \]
024 *
025 * <p>This code has been adapted from:
026 * <ul>
027 *  <li>The <a href="https://www.boost.org/">Boost</a>
028 *      {@code c++} implementation {@code <boost/math/special_functions/gamma.hpp>}.
029 * </ul>
030 *
031 * @see
032 * <a href="https://www.boost.org/doc/libs/1_77_0/libs/math/doc/html/math_toolkit/sf_gamma/gamma_ratios.html">
033 * Boost C++ Ratios of Gamma Functions</a>
034 * @since 1.1
035 */
036public final class GammaRatio {
037    /** Private constructor. */
038    private GammaRatio() {
039        // intentionally empty.
040    }
041
042    /**
043     * Computes the ratio of gamma functions of two values.
044     *
045     * <p>\[ \frac{\Gamma(a)}{\Gamma(b)} \]
046     *
047     * <p>If either argument is {@code <= 0} or infinite then the result is NaN.
048     *
049     * @param a Argument a (must be positive finite).
050     * @param b Argument b (must be positive finite).
051     * @return \( \Gamma(a) / \Gamma(b) \)
052     */
053    public static double value(double a, double b) {
054        return BoostGamma.tgammaRatio(a, b);
055    }
056
057    /**
058     * Computes the ratio of gamma functions of a value and an offset value.
059     *
060     * <p>\[ \frac{\Gamma(a)}{\Gamma(a + delta)} \]
061     *
062     * <p>Note that the result is calculated accurately even when {@code delta} is
063     * small compared to {@code a}: indeed even if {@code a+delta ~ a}. The function
064     * is typically used when {@code a} is large and {@code delta} is very small.
065     *
066     * @param a Argument.
067     * @param delta Argument.
068     * @return \( \Gamma(a) / \Gamma(a + delta) \)
069     */
070    public static double delta(double a, double delta) {
071        return BoostGamma.tgammaDeltaRatio(a, delta);
072    }
073}