UNPKG

@stdlib/math-base-special-gamma1pm1

Version:
152 lines (138 loc) 4.42 kB
/** * @license Apache-2.0 * * Copyright (c) 2018 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * * ## Notice * * The original C++ code and copyright notice are from the [Boost library]{@link http://www.boost.org/doc/libs/1_64_0/boost/math/special_functions/detail/lgamma_small.hpp}. The implementation has been modified for JavaScript. * * ```text * (C) Copyright John Maddock 2006-7, 2013-14. * (C) Copyright Paul A. Bristow 2007, 2013-14. * (C) Copyright Nikhar Agrawal 2013-14. * (C) Copyright Christopher Kormanyos 2013-14. * * Use, modification and distribution are subject to the * Boost Software License, Version 1.0. (See accompanying file * LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) * ``` */ 'use strict'; // MODULES // var ln = require( '@stdlib/math-base-special-ln' ); var EPS = require( '@stdlib/constants-float64-eps' ); var rateval1 = require( './rational_p1q1.js' ); var rateval2 = require( './rational_p2q2.js' ); var rateval3 = require( './rational_p3q3.js' ); // VARIABLES // var Y1 = 0.158963680267333984375; var Y2 = 0.52815341949462890625; var Y3 = 0.452017307281494140625; // MAIN // /** * Evaluates the natural logarithm of the gamma function for small arguments. * * ## Method * * 1. For \\( z > 2 \\), begin by performing argument reduction until \\( z \\) is in \\(\[2,3)\\). Use the following form: * * ```tex * \operatorname{gammaln}(z) = (z-2)(z+1)(Y + R(z-2)) * ``` * * where \\( R(z-2) \\) is a rational approximation optimized for low absolute error. As long as the absolute error is small compared to the constant \\( Y \\), then any rounding error in the computation will get wiped out. * * 2. If \\( z < 1 \\), use recurrence to shift to \\( z \\) in the interval \\(\[1,2\]\\). Then, use one of two approximations: one for \\( z \\) in \\(\[1,1.5\]\\) and one for \\( z \\) in \\(\[1.5,2\]\\): * * - For \(( z \\) in \\(\[1,1.5\]\\), use * * ```tex * \operatorname{gammaln}(z) = (z-1)(z-2)(Y + R(z-1)) * ``` * * where \\( R(z-1) \\) is a rational approximation optimized for low absolute error. As long as the absolute error is small compared to the constant \\( Y \\), then any rounding error in the computation will get wiped out. * * - For \\( z \\) in \\(\[1.5,2\]\\), use * * ```tex * \operatorname{gammaln}(z) = (2-z)(1-z)(Y + R(2-z)) * ``` * * where \\( R(2-z) \\) is a rational approximation optimized for low absolute error. As long as the absolute error is small compared to the constant \\( Y \\), then any rounding error in the computation will get wiped out. * * ## Notes * * - Relative error: * * | function | peak | maximum deviation | * |:--------:|:------------:|:-----------------:| * | R(Z-2) | 4.231e-18 | 5.900e-24 | * | R(Z-1) | 1.230011e-17 | 3.139e-021 | * | R(2-Z) | 1.797565e-17 | 2.151e-021 | * * @private * @param {number} z - input value * @param {number} zm1 - `z` minus one * @param {number} zm2 - `z` minus two * @returns {number} function value */ function lgammaSmallImp( z, zm1, zm2 ) { var prefix; var result; var r; var R; if ( z < EPS ) { return -ln( z ); } if ( zm1 === 0.0 || zm2 === 0.0 ) { return 0.0; } result = 0.0; if ( z > 2.0 ) { if ( z >= 3.0 ) { do { z -= 1.0; zm2 -= 1.0; result += ln(z); } while ( z >= 3.0 ); zm2 = z - 2.0; } r = zm2 * ( z+1.0 ); R = rateval1( zm2 ); result += ( r*Y1 ) + ( r*R ); return result; } if ( z < 1.0 ) { result += -ln(z); zm2 = zm1; zm1 = z; z += 1.0; } if ( z <= 1.5 ) { r = rateval2( zm1 ); prefix = zm1 * zm2; result += ( prefix*Y2 ) + ( prefix*r ); return result; } // Case: 1.5 < z <= 2 r = zm2 * zm1; R = rateval3( -zm2 ); result += ( r*Y3 ) + ( r*R ); return result; } // EXPORTS // module.exports = lgammaSmallImp;