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 /** 20 * Natural logarithm of the absolute value of \( \Gamma(x) \). 21 * 22 * <p>\[ \operatorname{lgamma}(z) = \ln \lvert \Gamma(x) \rvert \] 23 * 24 * <p>This code has been adapted from the <a href="https://www.boost.org/">Boost</a> 25 * {@code c++} implementation {@code <boost/math/special_functions/gamma.hpp>}. 26 * 27 * @see 28 * <a href="https://www.boost.org/doc/libs/1_77_0/libs/math/doc/html/math_toolkit/sf_gamma/lgamma.html"> 29 * Boost C++ Log Gamma functions</a> 30 */ 31 public final class LogGamma { 32 /** Private constructor. */ 33 private LogGamma() { 34 // intentionally empty. 35 } 36 37 /** 38 * Computes the function \( \ln \lvert \Gamma(x) \rvert \), the natural 39 * logarithm of the absolute value of \( \Gamma(x) \). 40 * 41 * @param x Argument. 42 * @return \( \ln \lvert \Gamma(x) \rvert \), or {@code NaN} if {@code x <= 0} 43 * and is an integer. 44 */ 45 public static double value(double x) { 46 return BoostGamma.lgamma(x); 47 } 48 49 /** 50 * Computes the function \( \ln \lvert \Gamma(x) \rvert \), the natural 51 * logarithm of the absolute value of \( \Gamma(x) \). 52 * 53 * <p>The sign output is set to 1 if the sign of gamma(x) is positive or zero; 54 * otherwise it is set to -1. 55 * 56 * @param x Argument. 57 * @param sign Sign output. If a non-zero length the first index {@code sign[0]} is 58 * set on output to the sign of gamma(z). 59 * @return \( \ln \lvert \Gamma(x) \rvert \), or {@code NaN} if {@code x <= 0} 60 * and is an integer. 61 * @since 1.1 62 */ 63 public static double value(double x, int[] sign) { 64 return BoostGamma.lgamma(x, sign); 65 } 66 }