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 * Natural logarithm of the absolute value of \( \Gamma(x) \).
021 *
022 * <p>\[ \operatorname{lgamma}(z) = \ln \lvert \Gamma(x) \rvert \]
023 *
024 * <p>This code has been adapted from the <a href="https://www.boost.org/">Boost</a>
025 * {@code c++} implementation {@code <boost/math/special_functions/gamma.hpp>}.
026 *
027 * @see
028 * <a href="https://www.boost.org/doc/libs/1_77_0/libs/math/doc/html/math_toolkit/sf_gamma/lgamma.html">
029 * Boost C++ Log Gamma functions</a>
030 */
031public final class LogGamma {
032    /** Private constructor. */
033    private LogGamma() {
034        // intentionally empty.
035    }
036
037    /**
038     * Computes the function \( \ln \lvert \Gamma(x) \rvert \), the natural
039     * logarithm of the absolute value of \( \Gamma(x) \).
040     *
041     * @param x Argument.
042     * @return \( \ln \lvert \Gamma(x) \rvert \), or {@code NaN} if {@code x <= 0}
043     * and is an integer.
044     */
045    public static double value(double x) {
046        return BoostGamma.lgamma(x);
047    }
048
049    /**
050     * Computes the function \( \ln \lvert \Gamma(x) \rvert \), the natural
051     * logarithm of the absolute value of \( \Gamma(x) \).
052     *
053     * <p>The sign output is set to 1 if the sign of gamma(x) is positive or zero;
054     * otherwise it is set to -1.
055     *
056     * @param x Argument.
057     * @param sign Sign output. If a non-zero length the first index {@code sign[0]} is
058     * set on output to the sign of gamma(z).
059     * @return \( \ln \lvert \Gamma(x) \rvert \), or {@code NaN} if {@code x <= 0}
060     * and is an integer.
061     * @since 1.1
062     */
063    public static double value(double x, int[] sign) {
064        return BoostGamma.lgamma(x, sign);
065    }
066}