trigamma.js (2874B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 * 18 * 19 * ## Notice 20 * 21 * The original C++ code and copyright notice are from the [Boost library]{@link http://www.boost.org/doc/libs/1_65_0/boost/math/special_functions/trigamma.hpp}. The implementation follows the original but has been reformatted and modified for JavaScript. 22 * 23 * ```text 24 * (C) Copyright John Maddock 2006. 25 * 26 * Use, modification and distribution are subject to the 27 * Boost Software License, Version 1.0. (See accompanying file 28 * LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) 29 * ``` 30 */ 31 32 'use strict'; 33 34 // MODULES // 35 36 var floor = require( './../../../../base/special/floor' ); 37 var sinpi = require( './../../../../base/special/sinpi' ); 38 var PI_SQUARED = require( '@stdlib/constants/float64/pi-squared' ); 39 var rateval12 = require( './rational_p12q12.js' ); 40 var rateval24 = require( './rational_p24q24.js' ); 41 var rateval48 = require( './rational_p48q48.js' ); 42 var rateval816 = require( './rational_p816q816.js' ); 43 var rateval16INF = require( './rational_p16infq16inf.js' ); 44 45 46 // VARIABLES // 47 48 var YOFFSET24 = 3.558437347412109375; 49 50 51 // MAIN // 52 53 /** 54 * Evaluates the trigamma function. 55 * 56 * @param {number} x - input value 57 * @returns {number} function value 58 * 59 * @example 60 * var v = trigamma( -2.5 ); 61 * // returns ~9.539 62 * 63 * @example 64 * var v = trigamma( 1.0 ); 65 * // returns ~1.645 66 * 67 * @example 68 * var v = trigamma( 10.0 ); 69 * // returns ~0.105 70 * 71 * @example 72 * var v = trigamma( NaN ); 73 * // returns NaN 74 * 75 * @example 76 * var v = trigamma( -1.0 ); 77 * // returns NaN 78 */ 79 function trigamma( x ) { 80 var result; 81 var s; 82 var y; 83 var z; 84 85 result = 0.0; 86 87 // Check for negative arguments and use reflection: 88 if ( x <= 0 ) { 89 if ( floor( x ) === x ) { 90 return NaN; 91 } 92 s = sinpi( x ); 93 z = 1.0 - x; 94 return -trigamma( z ) + ( PI_SQUARED / ( s*s ) ); 95 } 96 if ( x < 1.0 ) { 97 result = 1.0 / ( x*x ); 98 x += 1.0; 99 } 100 if ( x <= 2.0 ) { 101 result += ( 2.0+rateval12( x ) ) / ( x*x ); 102 } 103 else if ( x <= 4.0 ) { 104 result += ( YOFFSET24+rateval24( x ) ) / ( x*x ); 105 } 106 else if ( x <= 8.0 ) { 107 y = 1.0 / x; 108 result += ( 1.0+rateval48( y ) ) / x; 109 } 110 else if ( x <= 16.0 ) { 111 y = 1.0 / x; 112 result += ( 1.0+rateval816( y ) ) / x; 113 } 114 else { 115 y = 1.0 / x; 116 result += ( 1.0+rateval16INF( y ) ) / x; 117 } 118 return result; 119 } 120 121 122 // EXPORTS // 123 124 module.exports = trigamma;