Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:40:23

0001 //  (C) Copyright John Maddock 2006.
0002 //  Use, modification and distribution are subject to the
0003 //  Boost Software License, Version 1.0. (See accompanying file
0004 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0005 
0006 #ifndef BOOST_MATH_SF_TRIGAMMA_HPP
0007 #define BOOST_MATH_SF_TRIGAMMA_HPP
0008 
0009 #ifdef _MSC_VER
0010 #pragma once
0011 #endif
0012 
0013 #include <boost/math/special_functions/math_fwd.hpp>
0014 #include <boost/math/tools/rational.hpp>
0015 #include <boost/math/tools/series.hpp>
0016 #include <boost/math/tools/promotion.hpp>
0017 #include <boost/math/policies/error_handling.hpp>
0018 #include <boost/math/constants/constants.hpp>
0019 #include <boost/math/tools/big_constant.hpp>
0020 #include <boost/math/special_functions/polygamma.hpp>
0021 
0022 #if defined(__GNUC__) && defined(BOOST_MATH_USE_FLOAT128)
0023 //
0024 // This is the only way we can avoid
0025 // warning: non-standard suffix on floating constant [-Wpedantic]
0026 // when building with -Wall -pedantic.  Neither __extension__
0027 // nor #pragma diagnostic ignored work :(
0028 //
0029 #pragma GCC system_header
0030 #endif
0031 
0032 namespace boost{
0033 namespace math{
0034 namespace detail{
0035 
0036 template<class T, class Policy>
0037 T polygamma_imp(const int n, T x, const Policy &pol);
0038 
0039 template <class T, class Policy>
0040 T trigamma_prec(T x, const std::integral_constant<int, 53>*, const Policy&)
0041 {
0042    // Max error in interpolated form: 3.736e-017
0043    static const T offset = BOOST_MATH_BIG_CONSTANT(T, 53, 2.1093254089355469);
0044    static const T P_1_2[] = {
0045       BOOST_MATH_BIG_CONSTANT(T, 53, -1.1093280605946045),
0046       BOOST_MATH_BIG_CONSTANT(T, 53, -3.8310674472619321),
0047       BOOST_MATH_BIG_CONSTANT(T, 53, -3.3703848401898283),
0048       BOOST_MATH_BIG_CONSTANT(T, 53, 0.28080574467981213),
0049       BOOST_MATH_BIG_CONSTANT(T, 53, 1.6638069578676164),
0050       BOOST_MATH_BIG_CONSTANT(T, 53, 0.64468386819102836),
0051    };
0052    static const T Q_1_2[] = {
0053       BOOST_MATH_BIG_CONSTANT(T, 53, 1.0),
0054       BOOST_MATH_BIG_CONSTANT(T, 53, 3.4535389668541151),
0055       BOOST_MATH_BIG_CONSTANT(T, 53, 4.5208926987851437),
0056       BOOST_MATH_BIG_CONSTANT(T, 53, 2.7012734178351534),
0057       BOOST_MATH_BIG_CONSTANT(T, 53, 0.64468798399785611),
0058       BOOST_MATH_BIG_CONSTANT(T, 53, -0.20314516859987728e-6),
0059    };
0060    // Max error in interpolated form: 1.159e-017
0061    static const T P_2_4[] = {
0062       BOOST_MATH_BIG_CONSTANT(T, 53, -0.13803835004508849e-7),
0063       BOOST_MATH_BIG_CONSTANT(T, 53, 0.50000049158540261),
0064       BOOST_MATH_BIG_CONSTANT(T, 53, 1.6077979838469348),
0065       BOOST_MATH_BIG_CONSTANT(T, 53, 2.5645435828098254),
0066       BOOST_MATH_BIG_CONSTANT(T, 53, 2.0534873203680393),
0067       BOOST_MATH_BIG_CONSTANT(T, 53, 0.74566981111565923),
0068    };
0069    static const T Q_2_4[] = {
0070       BOOST_MATH_BIG_CONSTANT(T, 53, 1.0),
0071       BOOST_MATH_BIG_CONSTANT(T, 53, 2.8822787662376169),
0072       BOOST_MATH_BIG_CONSTANT(T, 53, 4.1681660554090917),
0073       BOOST_MATH_BIG_CONSTANT(T, 53, 2.7853527819234466),
0074       BOOST_MATH_BIG_CONSTANT(T, 53, 0.74967671848044792),
0075       BOOST_MATH_BIG_CONSTANT(T, 53, -0.00057069112416246805),
0076    };
0077    // Maximum Deviation Found:                     6.896e-018
0078    // Expected Error Term :                       -6.895e-018
0079    // Maximum Relative Change in Control Points :  8.497e-004
0080    static const T P_4_inf[] = {
0081       static_cast<T>(0.68947581948701249e-17L),
0082       static_cast<T>(0.49999999999998975L),
0083       static_cast<T>(1.0177274392923795L),
0084       static_cast<T>(2.498208511343429L),
0085       static_cast<T>(2.1921221359427595L),
0086       static_cast<T>(1.5897035272532764L),
0087       static_cast<T>(0.40154388356961734L),
0088    };
0089    static const T Q_4_inf[] = {
0090       static_cast<T>(1.0L),
0091       static_cast<T>(1.7021215452463932L),
0092       static_cast<T>(4.4290431747556469L),
0093       static_cast<T>(2.9745631894384922L),
0094       static_cast<T>(2.3013614809773616L),
0095       static_cast<T>(0.28360399799075752L),
0096       static_cast<T>(0.022892987908906897L),
0097    };
0098 
0099    if(x <= 2)
0100    {
0101       return (offset + boost::math::tools::evaluate_polynomial(P_1_2, x) / tools::evaluate_polynomial(Q_1_2, x)) / (x * x);
0102    }
0103    else if(x <= 4)
0104    {
0105       T y = 1 / x;
0106       return (1 + tools::evaluate_polynomial(P_2_4, y) / tools::evaluate_polynomial(Q_2_4, y)) / x;
0107    }
0108    T y = 1 / x;
0109    return (1 + tools::evaluate_polynomial(P_4_inf, y) / tools::evaluate_polynomial(Q_4_inf, y)) / x;
0110 }
0111 
0112 template <class T, class Policy>
0113 T trigamma_prec(T x, const std::integral_constant<int, 64>*, const Policy&)
0114 {
0115    // Max error in interpolated form: 1.178e-020
0116    static const T offset_1_2 = BOOST_MATH_BIG_CONSTANT(T, 64, 2.109325408935546875);
0117    static const T P_1_2[] = {
0118       BOOST_MATH_BIG_CONSTANT(T, 64, -1.10932535608960258341),
0119       BOOST_MATH_BIG_CONSTANT(T, 64, -4.18793841543017129052),
0120       BOOST_MATH_BIG_CONSTANT(T, 64, -4.63865531898487734531),
0121       BOOST_MATH_BIG_CONSTANT(T, 64, -0.919832884430500908047),
0122       BOOST_MATH_BIG_CONSTANT(T, 64, 1.68074038333180423012),
0123       BOOST_MATH_BIG_CONSTANT(T, 64, 1.21172611429185622377),
0124       BOOST_MATH_BIG_CONSTANT(T, 64, 0.259635673503366427284),
0125    };
0126    static const T Q_1_2[] = {
0127       BOOST_MATH_BIG_CONSTANT(T, 64, 1.0),
0128       BOOST_MATH_BIG_CONSTANT(T, 64, 3.77521119359546982995),
0129       BOOST_MATH_BIG_CONSTANT(T, 64, 5.664338024578956321),
0130       BOOST_MATH_BIG_CONSTANT(T, 64, 4.25995134879278028361),
0131       BOOST_MATH_BIG_CONSTANT(T, 64, 1.62956638448940402182),
0132       BOOST_MATH_BIG_CONSTANT(T, 64, 0.259635512844691089868),
0133       BOOST_MATH_BIG_CONSTANT(T, 64, 0.629642219810618032207e-8),
0134    };
0135    // Max error in interpolated form: 3.912e-020
0136    static const T P_2_8[] = {
0137       BOOST_MATH_BIG_CONSTANT(T, 64, -0.387540035162952880976e-11),
0138       BOOST_MATH_BIG_CONSTANT(T, 64, 0.500000000276430504),
0139       BOOST_MATH_BIG_CONSTANT(T, 64, 3.21926880986360957306),
0140       BOOST_MATH_BIG_CONSTANT(T, 64, 10.2550347708483445775),
0141       BOOST_MATH_BIG_CONSTANT(T, 64, 18.9002075150709144043),
0142       BOOST_MATH_BIG_CONSTANT(T, 64, 21.0357215832399705625),
0143       BOOST_MATH_BIG_CONSTANT(T, 64, 13.4346512182925923978),
0144       BOOST_MATH_BIG_CONSTANT(T, 64, 3.98656291026448279118),
0145    };
0146    static const T Q_2_8[] = {
0147       BOOST_MATH_BIG_CONSTANT(T, 64, 1.0),
0148       BOOST_MATH_BIG_CONSTANT(T, 64, 6.10520430478613667724),
0149       BOOST_MATH_BIG_CONSTANT(T, 64, 18.475001060603645512),
0150       BOOST_MATH_BIG_CONSTANT(T, 64, 31.7087534567758405638),
0151       BOOST_MATH_BIG_CONSTANT(T, 64, 31.908814523890465398),
0152       BOOST_MATH_BIG_CONSTANT(T, 64, 17.4175479039227084798),
0153       BOOST_MATH_BIG_CONSTANT(T, 64, 3.98749106958394941276),
0154       BOOST_MATH_BIG_CONSTANT(T, 64, -0.000115917322224411128566),
0155    };
0156    // Maximum Deviation Found:                     2.635e-020
0157    // Expected Error Term :                        2.635e-020
0158    // Maximum Relative Change in Control Points :  1.791e-003
0159    static const T P_8_inf[] = {
0160       BOOST_MATH_BIG_CONSTANT(T, 64, -0.263527875092466899848e-19),
0161       BOOST_MATH_BIG_CONSTANT(T, 64, 0.500000000000000058145),
0162       BOOST_MATH_BIG_CONSTANT(T, 64, 0.0730121433777364138677),
0163       BOOST_MATH_BIG_CONSTANT(T, 64, 1.94505878379957149534),
0164       BOOST_MATH_BIG_CONSTANT(T, 64, 0.0517092358874932620529),
0165       BOOST_MATH_BIG_CONSTANT(T, 64, 1.07995383547483921121),
0166    };
0167    static const T Q_8_inf[] = {
0168       BOOST_MATH_BIG_CONSTANT(T, 64, 1.0),
0169       BOOST_MATH_BIG_CONSTANT(T, 64, -0.187309046577818095504),
0170       BOOST_MATH_BIG_CONSTANT(T, 64, 3.95255391645238842975),
0171       BOOST_MATH_BIG_CONSTANT(T, 64, -1.14743283327078949087),
0172       BOOST_MATH_BIG_CONSTANT(T, 64, 2.52989799376344914499),
0173       BOOST_MATH_BIG_CONSTANT(T, 64, -0.627414303172402506396),
0174       BOOST_MATH_BIG_CONSTANT(T, 64, 0.141554248216425512536),
0175    };
0176 
0177    if(x <= 2)
0178    {
0179       return (offset_1_2 + boost::math::tools::evaluate_polynomial(P_1_2, x) / tools::evaluate_polynomial(Q_1_2, x)) / (x * x);
0180    }
0181    else if(x <= 8)
0182    {
0183       T y = 1 / x;
0184       return (1 + tools::evaluate_polynomial(P_2_8, y) / tools::evaluate_polynomial(Q_2_8, y)) / x;
0185    }
0186    T y = 1 / x;
0187    return (1 + tools::evaluate_polynomial(P_8_inf, y) / tools::evaluate_polynomial(Q_8_inf, y)) / x;
0188 }
0189 
0190 template <class T, class Policy>
0191 T trigamma_prec(T x, const std::integral_constant<int, 113>*, const Policy&)
0192 {
0193    // Max error in interpolated form: 1.916e-035
0194 
0195    static const T P_1_2[] = {
0196       BOOST_MATH_BIG_CONSTANT(T, 113, -0.999999999999999082554457936871832533),
0197       BOOST_MATH_BIG_CONSTANT(T, 113, -4.71237311120865266379041700054847734),
0198       BOOST_MATH_BIG_CONSTANT(T, 113, -7.94125711970499027763789342500817316),
0199       BOOST_MATH_BIG_CONSTANT(T, 113, -5.74657746697664735258222071695644535),
0200       BOOST_MATH_BIG_CONSTANT(T, 113, -0.404213349456398905981223965160595687),
0201       BOOST_MATH_BIG_CONSTANT(T, 113, 2.47877781178642876561595890095758896),
0202       BOOST_MATH_BIG_CONSTANT(T, 113, 2.07714151702455125992166949812126433),
0203       BOOST_MATH_BIG_CONSTANT(T, 113, 0.858877899162360138844032265418028567),
0204       BOOST_MATH_BIG_CONSTANT(T, 113, 0.20499222604410032375789018837922397),
0205       BOOST_MATH_BIG_CONSTANT(T, 113, 0.0272103140348194747360175268778415049),
0206       BOOST_MATH_BIG_CONSTANT(T, 113, 0.0015764849020876949848954081173520686),
0207    };
0208    static const T Q_1_2[] = {
0209       BOOST_MATH_BIG_CONSTANT(T, 113, 1.0),
0210       BOOST_MATH_BIG_CONSTANT(T, 113, 4.71237311120863419878375031457715223),
0211       BOOST_MATH_BIG_CONSTANT(T, 113, 9.58619118655339853449127952145877467),
0212       BOOST_MATH_BIG_CONSTANT(T, 113, 11.0940067269829372437561421279054968),
0213       BOOST_MATH_BIG_CONSTANT(T, 113, 8.09075424749327792073276309969037885),
0214       BOOST_MATH_BIG_CONSTANT(T, 113, 3.87705890159891405185343806884451286),
0215       BOOST_MATH_BIG_CONSTANT(T, 113, 1.22758678701914477836330837816976782),
0216       BOOST_MATH_BIG_CONSTANT(T, 113, 0.249092040606385004109672077814668716),
0217       BOOST_MATH_BIG_CONSTANT(T, 113, 0.0295750413900655597027079600025569048),
0218       BOOST_MATH_BIG_CONSTANT(T, 113, 0.00157648490200498142247694709728858139),
0219       BOOST_MATH_BIG_CONSTANT(T, 113, 0.161264050344059471721062360645432809e-14),
0220    };
0221 
0222    // Max error in interpolated form: 8.958e-035
0223    static const T P_2_4[] = {
0224       BOOST_MATH_BIG_CONSTANT(T, 113, -2.55843734739907925764326773972215085),
0225       BOOST_MATH_BIG_CONSTANT(T, 113, -12.2830208240542011967952466273455887),
0226       BOOST_MATH_BIG_CONSTANT(T, 113, -23.9195022162767993526575786066414403),
0227       BOOST_MATH_BIG_CONSTANT(T, 113, -24.9256431504823483094158828285470862),
0228       BOOST_MATH_BIG_CONSTANT(T, 113, -14.7979122765478779075108064826412285),
0229       BOOST_MATH_BIG_CONSTANT(T, 113, -4.46654453928610666393276765059122272),
0230       BOOST_MATH_BIG_CONSTANT(T, 113, -0.0191439033405649675717082465687845002),
0231       BOOST_MATH_BIG_CONSTANT(T, 113, 0.515412052554351265708917209749037352),
0232       BOOST_MATH_BIG_CONSTANT(T, 113, 0.195378348786064304378247325360320038),
0233       BOOST_MATH_BIG_CONSTANT(T, 113, 0.0334761282624174313035014426794245393),
0234       BOOST_MATH_BIG_CONSTANT(T, 113, 0.002373665205942206348500250056602687),
0235    };
0236    static const T Q_2_4[] = {
0237       BOOST_MATH_BIG_CONSTANT(T, 113, 1.0),
0238       BOOST_MATH_BIG_CONSTANT(T, 113, 4.80098558454419907830670928248659245),
0239       BOOST_MATH_BIG_CONSTANT(T, 113, 9.99220727843170133895059300223445265),
0240       BOOST_MATH_BIG_CONSTANT(T, 113, 11.8896146167631330735386697123464976),
0241       BOOST_MATH_BIG_CONSTANT(T, 113, 8.96613256683809091593793565879092581),
0242       BOOST_MATH_BIG_CONSTANT(T, 113, 4.47254136149624110878909334574485751),
0243       BOOST_MATH_BIG_CONSTANT(T, 113, 1.48600982028196527372434773913633152),
0244       BOOST_MATH_BIG_CONSTANT(T, 113, 0.319570735766764237068541501137990078),
0245       BOOST_MATH_BIG_CONSTANT(T, 113, 0.0407358345787680953107374215319322066),
0246       BOOST_MATH_BIG_CONSTANT(T, 113, 0.00237366520593271641375755486420859837),
0247       BOOST_MATH_BIG_CONSTANT(T, 113, 0.239554887903526152679337256236302116e-15),
0248       BOOST_MATH_BIG_CONSTANT(T, 113, -0.294749244740618656265237072002026314e-17),
0249    };
0250 
0251    static const T y_offset_2_4 = BOOST_MATH_BIG_CONSTANT(T, 113, 3.558437347412109375);
0252 
0253    // Max error in interpolated form: 4.319e-035
0254    static const T P_4_8[] = {
0255       BOOST_MATH_BIG_CONSTANT(T, 113, 0.166626112697021464248967707021688845e-16),
0256       BOOST_MATH_BIG_CONSTANT(T, 113, 0.499999999999997739552090249208808197),
0257       BOOST_MATH_BIG_CONSTANT(T, 113, 6.40270945019053817915772473771553187),
0258       BOOST_MATH_BIG_CONSTANT(T, 113, 41.3833374155000608013677627389343329),
0259       BOOST_MATH_BIG_CONSTANT(T, 113, 166.803341854562809335667241074035245),
0260       BOOST_MATH_BIG_CONSTANT(T, 113, 453.39964786925369319960722793414521),
0261       BOOST_MATH_BIG_CONSTANT(T, 113, 851.153712317697055375935433362983944),
0262       BOOST_MATH_BIG_CONSTANT(T, 113, 1097.70657567285059133109286478004458),
0263       BOOST_MATH_BIG_CONSTANT(T, 113, 938.431232478455316020076349367632922),
0264       BOOST_MATH_BIG_CONSTANT(T, 113, 487.268001604651932322080970189930074),
0265       BOOST_MATH_BIG_CONSTANT(T, 113, 119.953445242335730062471193124820659),
0266    };
0267    static const T Q_4_8[] = {
0268       BOOST_MATH_BIG_CONSTANT(T, 113, 1.0),
0269       BOOST_MATH_BIG_CONSTANT(T, 113, 12.4720855670474488978638945855932398),
0270       BOOST_MATH_BIG_CONSTANT(T, 113, 78.6093129753298570701376952709727391),
0271       BOOST_MATH_BIG_CONSTANT(T, 113, 307.470246050318322489781182863190127),
0272       BOOST_MATH_BIG_CONSTANT(T, 113, 805.140686101151538537565264188630079),
0273       BOOST_MATH_BIG_CONSTANT(T, 113, 1439.12019760292146454787601409644413),
0274       BOOST_MATH_BIG_CONSTANT(T, 113, 1735.6105285756048831268586001383127),
0275       BOOST_MATH_BIG_CONSTANT(T, 113, 1348.32500712856328019355198611280536),
0276       BOOST_MATH_BIG_CONSTANT(T, 113, 607.225985860570846699704222144650563),
0277       BOOST_MATH_BIG_CONSTANT(T, 113, 119.952317857277045332558673164517227),
0278       BOOST_MATH_BIG_CONSTANT(T, 113, 0.000140165918355036060868680809129436084),
0279    };
0280 
0281    // Maximum Deviation Found:                     2.867e-035
0282    // Expected Error Term :                        2.866e-035
0283    // Maximum Relative Change in Control Points :  2.662e-004
0284    static const T P_8_16[] = {
0285       BOOST_MATH_BIG_CONSTANT(T, 113, -0.184828315274146610610872315609837439e-19),
0286       BOOST_MATH_BIG_CONSTANT(T, 113, 0.500000000000000004122475157735807738),
0287       BOOST_MATH_BIG_CONSTANT(T, 113, 3.02533865247313349284875558880415875),
0288       BOOST_MATH_BIG_CONSTANT(T, 113, 13.5995927517457371243039532492642734),
0289       BOOST_MATH_BIG_CONSTANT(T, 113, 35.3132224283087906757037999452941588),
0290       BOOST_MATH_BIG_CONSTANT(T, 113, 67.1639424550714159157603179911505619),
0291       BOOST_MATH_BIG_CONSTANT(T, 113, 83.5767733658513967581959839367419891),
0292       BOOST_MATH_BIG_CONSTANT(T, 113, 71.073491212235705900866411319363501),
0293       BOOST_MATH_BIG_CONSTANT(T, 113, 35.8621515614725564575893663483998663),
0294       BOOST_MATH_BIG_CONSTANT(T, 113, 8.72152231639983491987779743154333318),
0295    };
0296    static const T Q_8_16[] = {
0297       BOOST_MATH_BIG_CONSTANT(T, 113, 1.0),
0298       BOOST_MATH_BIG_CONSTANT(T, 113, 5.71734397161293452310624822415866372),
0299       BOOST_MATH_BIG_CONSTANT(T, 113, 25.293404179620438179337103263274815),
0300       BOOST_MATH_BIG_CONSTANT(T, 113, 62.2619767967468199111077640625328469),
0301       BOOST_MATH_BIG_CONSTANT(T, 113, 113.955048909238993473389714972250235),
0302       BOOST_MATH_BIG_CONSTANT(T, 113, 130.807138328938966981862203944329408),
0303       BOOST_MATH_BIG_CONSTANT(T, 113, 102.423146902337654110717764213057753),
0304       BOOST_MATH_BIG_CONSTANT(T, 113, 44.0424772805245202514468199602123565),
0305       BOOST_MATH_BIG_CONSTANT(T, 113, 8.89898032477904072082994913461386099),
0306       BOOST_MATH_BIG_CONSTANT(T, 113, -0.0296627336872039988632793863671456398),
0307    };
0308    // Maximum Deviation Found:                     1.079e-035
0309    // Expected Error Term :                       -1.079e-035
0310    // Maximum Relative Change in Control Points :  7.884e-003
0311    static const T P_16_inf[] = {
0312       BOOST_MATH_BIG_CONSTANT(T, 113, 0.0),
0313       BOOST_MATH_BIG_CONSTANT(T, 113, 0.500000000000000000000000000000087317),
0314       BOOST_MATH_BIG_CONSTANT(T, 113, 0.345625669885456215194494735902663968),
0315       BOOST_MATH_BIG_CONSTANT(T, 113, 9.62895499360842232127552650044647769),
0316       BOOST_MATH_BIG_CONSTANT(T, 113, 3.5936085382439026269301003761320812),
0317       BOOST_MATH_BIG_CONSTANT(T, 113, 49.459599118438883265036646019410669),
0318       BOOST_MATH_BIG_CONSTANT(T, 113, 7.77519237321893917784735690560496607),
0319       BOOST_MATH_BIG_CONSTANT(T, 113, 74.4536074488178075948642351179304121),
0320       BOOST_MATH_BIG_CONSTANT(T, 113, 2.75209340397069050436806159297952699),
0321       BOOST_MATH_BIG_CONSTANT(T, 113, 23.9292359711471667884504840186561598),
0322    };
0323    static const T Q_16_inf[] = {
0324       BOOST_MATH_BIG_CONSTANT(T, 113, 1.0),
0325       BOOST_MATH_BIG_CONSTANT(T, 113, 0.357918006437579097055656138920742037),
0326       BOOST_MATH_BIG_CONSTANT(T, 113, 19.1386039850709849435325005484512944),
0327       BOOST_MATH_BIG_CONSTANT(T, 113, 0.874349081464143606016221431763364517),
0328       BOOST_MATH_BIG_CONSTANT(T, 113, 98.6516097434855572678195488061432509),
0329       BOOST_MATH_BIG_CONSTANT(T, 113, -16.1051972833382893468655223662534306),
0330       BOOST_MATH_BIG_CONSTANT(T, 113, 154.316860216253720989145047141653727),
0331       BOOST_MATH_BIG_CONSTANT(T, 113, -40.2026880424378986053105969312264534),
0332       BOOST_MATH_BIG_CONSTANT(T, 113, 60.1679136674264778074736441126810223),
0333       BOOST_MATH_BIG_CONSTANT(T, 113, -13.3414844622256422644504472438320114),
0334       BOOST_MATH_BIG_CONSTANT(T, 113, 2.53795636200649908779512969030363442),
0335    };
0336 
0337    if(x <= 2)
0338    {
0339       return (2 + boost::math::tools::evaluate_polynomial(P_1_2, x) / tools::evaluate_polynomial(Q_1_2, x)) / (x * x);
0340    }
0341    else if(x <= 4)
0342    {
0343       return (y_offset_2_4 + boost::math::tools::evaluate_polynomial(P_2_4, x) / tools::evaluate_polynomial(Q_2_4, x)) / (x * x);
0344    }
0345    else if(x <= 8)
0346    {
0347       T y = 1 / x;
0348       return (1 + tools::evaluate_polynomial(P_4_8, y) / tools::evaluate_polynomial(Q_4_8, y)) / x;
0349    }
0350    else if(x <= 16)
0351    {
0352       T y = 1 / x;
0353       return (1 + tools::evaluate_polynomial(P_8_16, y) / tools::evaluate_polynomial(Q_8_16, y)) / x;
0354    }
0355    T y = 1 / x;
0356    return (1 + tools::evaluate_polynomial(P_16_inf, y) / tools::evaluate_polynomial(Q_16_inf, y)) / x;
0357 }
0358 
0359 template <class T, class Tag, class Policy>
0360 T trigamma_imp(T x, const Tag* t, const Policy& pol)
0361 {
0362    //
0363    // This handles reflection of negative arguments, and all our
0364    // error handling, then forwards to the T-specific approximation.
0365    //
0366    BOOST_MATH_STD_USING // ADL of std functions.
0367 
0368    T result = 0;
0369    //
0370    // Check for negative arguments and use reflection:
0371    //
0372    if(x <= 0)
0373    {
0374       // Reflect:
0375       T z = 1 - x;
0376       // Argument reduction for tan:
0377       if(floor(x) == x)
0378       {
0379          return policies::raise_pole_error<T>("boost::math::trigamma<%1%>(%1%)", nullptr, (1-x), pol);
0380       }
0381       T s = fabs(x) < fabs(z) ? boost::math::sin_pi(x, pol) : boost::math::sin_pi(z, pol);
0382       return -trigamma_imp(z, t, pol) + boost::math::pow<2>(constants::pi<T>()) / (s * s);
0383    }
0384    if(x < 1)
0385    {
0386       result = 1 / (x * x);
0387       x += 1;
0388    }
0389    return result + trigamma_prec(x, t, pol);
0390 }
0391 
0392 template <class T, class Policy>
0393 T trigamma_imp(T x, const std::integral_constant<int, 0>*, const Policy& pol)
0394 {
0395    return polygamma_imp(1, x, pol);
0396 }
0397 //
0398 // Initializer: ensure all our constants are initialized prior to the first call of main:
0399 //
0400 template <class T, class Policy>
0401 struct trigamma_initializer
0402 {
0403    struct init
0404    {
0405       init()
0406       {
0407          typedef typename policies::precision<T, Policy>::type precision_type;
0408          do_init(std::integral_constant<bool, precision_type::value && (precision_type::value <= 113)>());
0409       }
0410       void do_init(const std::true_type&)
0411       {
0412          boost::math::trigamma(T(2.5), Policy());
0413       }
0414       void do_init(const std::false_type&){}
0415       void force_instantiate()const{}
0416    };
0417    static const init initializer;
0418    static void force_instantiate()
0419    {
0420       initializer.force_instantiate();
0421    }
0422 };
0423 
0424 template <class T, class Policy>
0425 const typename trigamma_initializer<T, Policy>::init trigamma_initializer<T, Policy>::initializer;
0426 
0427 } // namespace detail
0428 
0429 template <class T, class Policy>
0430 inline typename tools::promote_args<T>::type
0431    trigamma(T x, const Policy&)
0432 {
0433    typedef typename tools::promote_args<T>::type result_type;
0434    typedef typename policies::evaluation<result_type, Policy>::type value_type;
0435    typedef typename policies::precision<T, Policy>::type precision_type;
0436    typedef std::integral_constant<int,
0437       precision_type::value <= 0 ? 0 :
0438       precision_type::value <= 53 ? 53 :
0439       precision_type::value <= 64 ? 64 :
0440       precision_type::value <= 113 ? 113 : 0
0441    > tag_type;
0442    typedef typename policies::normalise<
0443       Policy,
0444       policies::promote_float<false>,
0445       policies::promote_double<false>,
0446       policies::discrete_quantile<>,
0447       policies::assert_undefined<> >::type forwarding_policy;
0448 
0449    // Force initialization of constants:
0450    detail::trigamma_initializer<value_type, forwarding_policy>::force_instantiate();
0451 
0452    return policies::checked_narrowing_cast<result_type, Policy>(detail::trigamma_imp(
0453       static_cast<value_type>(x),
0454       static_cast<const tag_type*>(nullptr), forwarding_policy()), "boost::math::trigamma<%1%>(%1%)");
0455 }
0456 
0457 template <class T>
0458 inline typename tools::promote_args<T>::type
0459    trigamma(T x)
0460 {
0461    return trigamma(x, policies::policy<>());
0462 }
0463 
0464 } // namespace math
0465 } // namespace boost
0466 #endif
0467