ellint_1.hpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. // Copyright (c) 2006 Xiaogang Zhang
  2. // Copyright (c) 2006 John Maddock
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // History:
  8. // XZ wrote the original of this file as part of the Google
  9. // Summer of Code 2006. JM modified it to fit into the
  10. // Boost.Math conceptual framework better, and to ensure
  11. // that the code continues to work no matter how many digits
  12. // type T has.
  13. #ifndef BOOST_MATH_ELLINT_1_HPP
  14. #define BOOST_MATH_ELLINT_1_HPP
  15. #ifdef _MSC_VER
  16. #pragma once
  17. #endif
  18. #include <boost/math/special_functions/math_fwd.hpp>
  19. #include <boost/math/special_functions/ellint_rf.hpp>
  20. #include <boost/math/constants/constants.hpp>
  21. #include <boost/math/policies/error_handling.hpp>
  22. #include <boost/math/tools/workaround.hpp>
  23. #include <boost/math/special_functions/round.hpp>
  24. // Elliptic integrals (complete and incomplete) of the first kind
  25. // Carlson, Numerische Mathematik, vol 33, 1 (1979)
  26. namespace boost { namespace math {
  27. template <class T1, class T2, class Policy>
  28. typename tools::promote_args<T1, T2>::type ellint_1(T1 k, T2 phi, const Policy& pol);
  29. namespace detail{
  30. template <typename T, typename Policy>
  31. T ellint_k_imp(T k, const Policy& pol, std::integral_constant<int, 0> const&);
  32. template <typename T, typename Policy>
  33. T ellint_k_imp(T k, const Policy& pol, std::integral_constant<int, 1> const&);
  34. template <typename T, typename Policy>
  35. T ellint_k_imp(T k, const Policy& pol, std::integral_constant<int, 2> const&);
  36. // Elliptic integral (Legendre form) of the first kind
  37. template <typename T, typename Policy>
  38. T ellint_f_imp(T phi, T k, const Policy& pol)
  39. {
  40. BOOST_MATH_STD_USING
  41. using namespace boost::math::tools;
  42. using namespace boost::math::constants;
  43. static const char* function = "boost::math::ellint_f<%1%>(%1%,%1%)";
  44. BOOST_MATH_INSTRUMENT_VARIABLE(phi);
  45. BOOST_MATH_INSTRUMENT_VARIABLE(k);
  46. BOOST_MATH_INSTRUMENT_VARIABLE(function);
  47. bool invert = false;
  48. if(phi < 0)
  49. {
  50. BOOST_MATH_INSTRUMENT_VARIABLE(phi);
  51. phi = fabs(phi);
  52. invert = true;
  53. }
  54. T result;
  55. if(phi >= tools::max_value<T>())
  56. {
  57. // Need to handle infinity as a special case:
  58. result = policies::raise_overflow_error<T>(function, nullptr, pol);
  59. BOOST_MATH_INSTRUMENT_VARIABLE(result);
  60. }
  61. else if(phi > 1 / tools::epsilon<T>())
  62. {
  63. // Phi is so large that phi%pi is necessarily zero (or garbage),
  64. // just return the second part of the duplication formula:
  65. typedef std::integral_constant<int,
  66. std::is_floating_point<T>::value&& std::numeric_limits<T>::digits && (std::numeric_limits<T>::digits <= 54) ? 0 :
  67. std::is_floating_point<T>::value && std::numeric_limits<T>::digits && (std::numeric_limits<T>::digits <= 64) ? 1 : 2
  68. > precision_tag_type;
  69. result = 2 * phi * ellint_k_imp(k, pol, precision_tag_type()) / constants::pi<T>();
  70. BOOST_MATH_INSTRUMENT_VARIABLE(result);
  71. }
  72. else
  73. {
  74. // Carlson's algorithm works only for |phi| <= pi/2,
  75. // use the integrand's periodicity to normalize phi
  76. //
  77. // Xiaogang's original code used a cast to long long here
  78. // but that fails if T has more digits than a long long,
  79. // so rewritten to use fmod instead:
  80. //
  81. BOOST_MATH_INSTRUMENT_CODE("pi/2 = " << constants::pi<T>() / 2);
  82. T rphi = boost::math::tools::fmod_workaround(phi, T(constants::half_pi<T>()));
  83. BOOST_MATH_INSTRUMENT_VARIABLE(rphi);
  84. T m = boost::math::round((phi - rphi) / constants::half_pi<T>());
  85. BOOST_MATH_INSTRUMENT_VARIABLE(m);
  86. int s = 1;
  87. if(boost::math::tools::fmod_workaround(m, T(2)) > T(0.5))
  88. {
  89. m += 1;
  90. s = -1;
  91. rphi = constants::half_pi<T>() - rphi;
  92. BOOST_MATH_INSTRUMENT_VARIABLE(rphi);
  93. }
  94. T sinp = sin(rphi);
  95. sinp *= sinp;
  96. if (sinp * k * k >= 1)
  97. {
  98. return policies::raise_domain_error<T>(function,
  99. "Got k^2 * sin^2(phi) = %1%, but the function requires this < 1", sinp * k * k, pol);
  100. }
  101. T cosp = cos(rphi);
  102. cosp *= cosp;
  103. BOOST_MATH_INSTRUMENT_VARIABLE(sinp);
  104. BOOST_MATH_INSTRUMENT_VARIABLE(cosp);
  105. if(sinp > tools::min_value<T>())
  106. {
  107. BOOST_MATH_ASSERT(rphi != 0); // precondition, can't be true if sin(rphi) != 0.
  108. //
  109. // Use http://dlmf.nist.gov/19.25#E5, note that
  110. // c-1 simplifies to cot^2(rphi) which avoid cancellation:
  111. //
  112. T c = 1 / sinp;
  113. result = static_cast<T>(s * ellint_rf_imp(T(cosp / sinp), T(c - k * k), c, pol));
  114. }
  115. else
  116. result = s * sin(rphi);
  117. BOOST_MATH_INSTRUMENT_VARIABLE(result);
  118. if(m != 0)
  119. {
  120. typedef std::integral_constant<int,
  121. std::is_floating_point<T>::value&& std::numeric_limits<T>::digits && (std::numeric_limits<T>::digits <= 54) ? 0 :
  122. std::is_floating_point<T>::value && std::numeric_limits<T>::digits && (std::numeric_limits<T>::digits <= 64) ? 1 : 2
  123. > precision_tag_type;
  124. result += m * ellint_k_imp(k, pol, precision_tag_type());
  125. BOOST_MATH_INSTRUMENT_VARIABLE(result);
  126. }
  127. }
  128. return invert ? T(-result) : result;
  129. }
  130. // Complete elliptic integral (Legendre form) of the first kind
  131. template <typename T, typename Policy>
  132. T ellint_k_imp(T k, const Policy& pol, std::integral_constant<int, 2> const&)
  133. {
  134. BOOST_MATH_STD_USING
  135. using namespace boost::math::tools;
  136. static const char* function = "boost::math::ellint_k<%1%>(%1%)";
  137. if (abs(k) > 1)
  138. {
  139. return policies::raise_domain_error<T>(function, "Got k = %1%, function requires |k| <= 1", k, pol);
  140. }
  141. if (abs(k) == 1)
  142. {
  143. return policies::raise_overflow_error<T>(function, nullptr, pol);
  144. }
  145. T x = 0;
  146. T y = 1 - k * k;
  147. T z = 1;
  148. T value = ellint_rf_imp(x, y, z, pol);
  149. return value;
  150. }
  151. //
  152. // Special versions for double and 80-bit long double precision,
  153. // double precision versions use the coefficients from:
  154. // "Fast computation of complete elliptic integrals and Jacobian elliptic functions",
  155. // Celestial Mechanics and Dynamical Astronomy, April 2012.
  156. //
  157. // Higher precision coefficients for 80-bit long doubles can be calculated
  158. // using for example:
  159. // Table[N[SeriesCoefficient[ EllipticK [ m ], { m, 875/1000, i} ], 20], {i, 0, 24}]
  160. // and checking the value of the first neglected term with:
  161. // N[SeriesCoefficient[ EllipticK [ m ], { m, 875/1000, 24} ], 20] * (2.5/100)^24
  162. //
  163. // For m > 0.9 we don't use the method of the paper above, but simply call our
  164. // existing routines. The routine used in the above paper was tried (and is
  165. // archived in the code below), but was found to have slightly higher error rates.
  166. //
  167. template <typename T, typename Policy>
  168. BOOST_MATH_FORCEINLINE T ellint_k_imp(T k, const Policy& pol, std::integral_constant<int, 0> const&)
  169. {
  170. using std::abs;
  171. using namespace boost::math::tools;
  172. T m = k * k;
  173. switch (static_cast<int>(m * 20))
  174. {
  175. case 0:
  176. case 1:
  177. //if (m < 0.1)
  178. {
  179. constexpr T coef[] =
  180. {
  181. static_cast<T>(1.591003453790792180),
  182. static_cast<T>(0.416000743991786912),
  183. static_cast<T>(0.245791514264103415),
  184. static_cast<T>(0.179481482914906162),
  185. static_cast<T>(0.144556057087555150),
  186. static_cast<T>(0.123200993312427711),
  187. static_cast<T>(0.108938811574293531),
  188. static_cast<T>(0.098853409871592910),
  189. static_cast<T>(0.091439629201749751),
  190. static_cast<T>(0.085842591595413900),
  191. static_cast<T>(0.081541118718303215),
  192. static_cast<T>(0.078199656811256481910)
  193. };
  194. return boost::math::tools::evaluate_polynomial(coef, m - static_cast<T>(0.05));
  195. }
  196. case 2:
  197. case 3:
  198. //else if (m < 0.2)
  199. {
  200. constexpr T coef[] =
  201. {
  202. static_cast<T>(1.635256732264579992),
  203. static_cast<T>(0.471190626148732291),
  204. static_cast<T>(0.309728410831499587),
  205. static_cast<T>(0.252208311773135699),
  206. static_cast<T>(0.226725623219684650),
  207. static_cast<T>(0.215774446729585976),
  208. static_cast<T>(0.213108771877348910),
  209. static_cast<T>(0.216029124605188282),
  210. static_cast<T>(0.223255831633057896),
  211. static_cast<T>(0.234180501294209925),
  212. static_cast<T>(0.248557682972264071),
  213. static_cast<T>(0.266363809892617521)
  214. };
  215. return boost::math::tools::evaluate_polynomial(coef, m - static_cast<T>(0.15));
  216. }
  217. case 4:
  218. case 5:
  219. //else if (m < 0.3)
  220. {
  221. constexpr T coef[] =
  222. {
  223. static_cast<T>(1.685750354812596043),
  224. static_cast<T>(0.541731848613280329),
  225. static_cast<T>(0.401524438390690257),
  226. static_cast<T>(0.369642473420889090),
  227. static_cast<T>(0.376060715354583645),
  228. static_cast<T>(0.405235887085125919),
  229. static_cast<T>(0.453294381753999079),
  230. static_cast<T>(0.520518947651184205),
  231. static_cast<T>(0.609426039204995055),
  232. static_cast<T>(0.724263522282908870),
  233. static_cast<T>(0.871013847709812357),
  234. static_cast<T>(1.057652872753547036)
  235. };
  236. return boost::math::tools::evaluate_polynomial(coef, m - static_cast<T>(0.25));
  237. }
  238. case 6:
  239. case 7:
  240. //else if (m < 0.4)
  241. {
  242. constexpr T coef[] =
  243. {
  244. static_cast<T>(1.744350597225613243),
  245. static_cast<T>(0.634864275371935304),
  246. static_cast<T>(0.539842564164445538),
  247. static_cast<T>(0.571892705193787391),
  248. static_cast<T>(0.670295136265406100),
  249. static_cast<T>(0.832586590010977199),
  250. static_cast<T>(1.073857448247933265),
  251. static_cast<T>(1.422091460675497751),
  252. static_cast<T>(1.920387183402304829),
  253. static_cast<T>(2.632552548331654201),
  254. static_cast<T>(3.652109747319039160),
  255. static_cast<T>(5.115867135558865806),
  256. static_cast<T>(7.224080007363877411)
  257. };
  258. return boost::math::tools::evaluate_polynomial(coef, m - static_cast<T>(0.35));
  259. }
  260. case 8:
  261. case 9:
  262. //else if (m < 0.5)
  263. {
  264. constexpr T coef[] =
  265. {
  266. static_cast<T>(1.813883936816982644),
  267. static_cast<T>(0.763163245700557246),
  268. static_cast<T>(0.761928605321595831),
  269. static_cast<T>(0.951074653668427927),
  270. static_cast<T>(1.315180671703161215),
  271. static_cast<T>(1.928560693477410941),
  272. static_cast<T>(2.937509342531378755),
  273. static_cast<T>(4.594894405442878062),
  274. static_cast<T>(7.330071221881720772),
  275. static_cast<T>(11.87151259742530180),
  276. static_cast<T>(19.45851374822937738),
  277. static_cast<T>(32.20638657246426863),
  278. static_cast<T>(53.73749198700554656),
  279. static_cast<T>(90.27388602940998849)
  280. };
  281. return boost::math::tools::evaluate_polynomial(coef, m - static_cast<T>(0.45));
  282. }
  283. case 10:
  284. case 11:
  285. //else if (m < 0.6)
  286. {
  287. constexpr T coef[] =
  288. {
  289. static_cast<T>(1.898924910271553526),
  290. static_cast<T>(0.950521794618244435),
  291. static_cast<T>(1.151077589959015808),
  292. static_cast<T>(1.750239106986300540),
  293. static_cast<T>(2.952676812636875180),
  294. static_cast<T>(5.285800396121450889),
  295. static_cast<T>(9.832485716659979747),
  296. static_cast<T>(18.78714868327559562),
  297. static_cast<T>(36.61468615273698145),
  298. static_cast<T>(72.45292395127771801),
  299. static_cast<T>(145.1079577347069102),
  300. static_cast<T>(293.4786396308497026),
  301. static_cast<T>(598.3851815055010179),
  302. static_cast<T>(1228.420013075863451),
  303. static_cast<T>(2536.529755382764488)
  304. };
  305. return boost::math::tools::evaluate_polynomial(coef, m - static_cast<T>(0.55));
  306. }
  307. case 12:
  308. case 13:
  309. //else if (m < 0.7)
  310. {
  311. constexpr T coef[] =
  312. {
  313. static_cast<T>(2.007598398424376302),
  314. static_cast<T>(1.248457231212347337),
  315. static_cast<T>(1.926234657076479729),
  316. static_cast<T>(3.751289640087587680),
  317. static_cast<T>(8.119944554932045802),
  318. static_cast<T>(18.66572130873555361),
  319. static_cast<T>(44.60392484291437063),
  320. static_cast<T>(109.5092054309498377),
  321. static_cast<T>(274.2779548232413480),
  322. static_cast<T>(697.5598008606326163),
  323. static_cast<T>(1795.716014500247129),
  324. static_cast<T>(4668.381716790389910),
  325. static_cast<T>(12235.76246813664335),
  326. static_cast<T>(32290.17809718320818),
  327. static_cast<T>(85713.07608195964685),
  328. static_cast<T>(228672.1890493117096),
  329. static_cast<T>(612757.2711915852774)
  330. };
  331. return boost::math::tools::evaluate_polynomial(coef, m - static_cast<T>(0.65));
  332. }
  333. case 14:
  334. case 15:
  335. //else if (m < static_cast<T>(0.8))
  336. {
  337. constexpr T coef[] =
  338. {
  339. static_cast<T>(2.156515647499643235),
  340. static_cast<T>(1.791805641849463243),
  341. static_cast<T>(3.826751287465713147),
  342. static_cast<T>(10.38672468363797208),
  343. static_cast<T>(31.40331405468070290),
  344. static_cast<T>(100.9237039498695416),
  345. static_cast<T>(337.3268282632272897),
  346. static_cast<T>(1158.707930567827917),
  347. static_cast<T>(4060.990742193632092),
  348. static_cast<T>(14454.00184034344795),
  349. static_cast<T>(52076.66107599404803),
  350. static_cast<T>(189493.6591462156887),
  351. static_cast<T>(695184.5762413896145),
  352. static_cast<T>(2567994.048255284686),
  353. static_cast<T>(9541921.966748386322),
  354. static_cast<T>(35634927.44218076174),
  355. static_cast<T>(133669298.4612040871),
  356. static_cast<T>(503352186.6866284541),
  357. static_cast<T>(1901975729.538660119),
  358. static_cast<T>(7208915015.330103756)
  359. };
  360. return boost::math::tools::evaluate_polynomial(coef, m - static_cast<T>(0.75));
  361. }
  362. case 16:
  363. //else if (m < static_cast<T>(0.85))
  364. {
  365. constexpr T coef[] =
  366. {
  367. static_cast<T>(2.318122621712510589),
  368. static_cast<T>(2.616920150291232841),
  369. static_cast<T>(7.897935075731355823),
  370. static_cast<T>(30.50239715446672327),
  371. static_cast<T>(131.4869365523528456),
  372. static_cast<T>(602.9847637356491617),
  373. static_cast<T>(2877.024617809972641),
  374. static_cast<T>(14110.51991915180325),
  375. static_cast<T>(70621.44088156540229),
  376. static_cast<T>(358977.2665825309926),
  377. static_cast<T>(1847238.263723971684),
  378. static_cast<T>(9600515.416049214109),
  379. static_cast<T>(50307677.08502366879),
  380. static_cast<T>(265444188.6527127967),
  381. static_cast<T>(1408862325.028702687),
  382. static_cast<T>(7515687935.373774627)
  383. };
  384. return boost::math::tools::evaluate_polynomial(coef, m - static_cast<T>(0.825));
  385. }
  386. case 17:
  387. //else if (m < static_cast<T>(0.90))
  388. {
  389. constexpr T coef[] =
  390. {
  391. static_cast<T>(2.473596173751343912),
  392. static_cast<T>(3.727624244118099310),
  393. static_cast<T>(15.60739303554930496),
  394. static_cast<T>(84.12850842805887747),
  395. static_cast<T>(506.9818197040613935),
  396. static_cast<T>(3252.277058145123644),
  397. static_cast<T>(21713.24241957434256),
  398. static_cast<T>(149037.0451890932766),
  399. static_cast<T>(1043999.331089990839),
  400. static_cast<T>(7427974.817042038995),
  401. static_cast<T>(53503839.67558661151),
  402. static_cast<T>(389249886.9948708474),
  403. static_cast<T>(2855288351.100810619),
  404. static_cast<T>(21090077038.76684053),
  405. static_cast<T>(156699833947.7902014),
  406. static_cast<T>(1170222242422.439893),
  407. static_cast<T>(8777948323668.937971),
  408. static_cast<T>(66101242752484.95041),
  409. static_cast<T>(499488053713388.7989),
  410. static_cast<T>(37859743397240299.20)
  411. };
  412. return boost::math::tools::evaluate_polynomial(coef, m - static_cast<T>(0.875));
  413. }
  414. default:
  415. //
  416. // This handles all cases where m > 0.9,
  417. // including all error handling:
  418. //
  419. return ellint_k_imp(k, pol, std::integral_constant<int, 2>());
  420. #if 0
  421. else
  422. {
  423. T lambda_prime = (1 - sqrt(k)) / (2 * (1 + sqrt(k)));
  424. T k_prime = ellint_k(sqrt((1 - k) * (1 + k))); // K(m')
  425. T lambda_prime_4th = boost::math::pow<4>(lambda_prime);
  426. T q_prime = ((((((20910 * lambda_prime_4th) + 1707) * lambda_prime_4th + 150) * lambda_prime_4th + 15) * lambda_prime_4th + 2) * lambda_prime_4th + 1) * lambda_prime;
  427. /*T q_prime_2 = lambda_prime
  428. + 2 * boost::math::pow<5>(lambda_prime)
  429. + 15 * boost::math::pow<9>(lambda_prime)
  430. + 150 * boost::math::pow<13>(lambda_prime)
  431. + 1707 * boost::math::pow<17>(lambda_prime)
  432. + 20910 * boost::math::pow<21>(lambda_prime);*/
  433. return -log(q_prime) * k_prime / boost::math::constants::pi<T>();
  434. }
  435. #endif
  436. }
  437. }
  438. template <typename T, typename Policy>
  439. BOOST_MATH_FORCEINLINE T ellint_k_imp(T k, const Policy& pol, std::integral_constant<int, 1> const&)
  440. {
  441. using std::abs;
  442. using namespace boost::math::tools;
  443. T m = k * k;
  444. switch (static_cast<int>(m * 20))
  445. {
  446. case 0:
  447. case 1:
  448. {
  449. constexpr T coef[] =
  450. {
  451. 1.5910034537907921801L,
  452. 0.41600074399178691174L,
  453. 0.24579151426410341536L,
  454. 0.17948148291490616181L,
  455. 0.14455605708755514976L,
  456. 0.12320099331242771115L,
  457. 0.10893881157429353105L,
  458. 0.098853409871592910399L,
  459. 0.091439629201749751268L,
  460. 0.085842591595413899672L,
  461. 0.081541118718303214749L,
  462. 0.078199656811256481910L,
  463. 0.075592617535422415648L,
  464. 0.073562939365441925050L
  465. };
  466. return boost::math::tools::evaluate_polynomial(coef, m - 0.05L);
  467. }
  468. case 2:
  469. case 3:
  470. {
  471. constexpr T coef[] =
  472. {
  473. 1.6352567322645799924L,
  474. 0.47119062614873229055L,
  475. 0.30972841083149958708L,
  476. 0.25220831177313569923L,
  477. 0.22672562321968464974L,
  478. 0.21577444672958597588L,
  479. 0.21310877187734890963L,
  480. 0.21602912460518828154L,
  481. 0.22325583163305789567L,
  482. 0.23418050129420992492L,
  483. 0.24855768297226407136L,
  484. 0.26636380989261752077L,
  485. 0.28772845215611466775L,
  486. 0.31290024539780334906L,
  487. 0.34223105446381299902L
  488. };
  489. return boost::math::tools::evaluate_polynomial(coef, m - 0.15L);
  490. }
  491. case 4:
  492. case 5:
  493. {
  494. constexpr T coef[] =
  495. {
  496. 1.6857503548125960429L,
  497. 0.54173184861328032882L,
  498. 0.40152443839069025682L,
  499. 0.36964247342088908995L,
  500. 0.37606071535458364462L,
  501. 0.40523588708512591863L,
  502. 0.45329438175399907924L,
  503. 0.52051894765118420473L,
  504. 0.60942603920499505544L,
  505. 0.72426352228290886975L,
  506. 0.87101384770981235737L,
  507. 1.0576528727535470365L,
  508. 1.2945970872087764321L,
  509. 1.5953368253888783747L,
  510. 1.9772844873556364793L,
  511. 2.4628890581910021287L
  512. };
  513. return boost::math::tools::evaluate_polynomial(coef, m - 0.25L);
  514. }
  515. case 6:
  516. case 7:
  517. {
  518. constexpr T coef[] =
  519. {
  520. 1.7443505972256132429L,
  521. 0.63486427537193530383L,
  522. 0.53984256416444553751L,
  523. 0.57189270519378739093L,
  524. 0.67029513626540610034L,
  525. 0.83258659001097719939L,
  526. 1.0738574482479332654L,
  527. 1.4220914606754977514L,
  528. 1.9203871834023048288L,
  529. 2.6325525483316542006L,
  530. 3.6521097473190391602L,
  531. 5.1158671355588658061L,
  532. 7.2240800073638774108L,
  533. 10.270306349944787227L,
  534. 14.685616935355757348L,
  535. 21.104114212004582734L,
  536. 30.460132808575799413L,
  537. };
  538. return boost::math::tools::evaluate_polynomial(coef, m - 0.35L);
  539. }
  540. case 8:
  541. case 9:
  542. {
  543. constexpr T coef[] =
  544. {
  545. 1.8138839368169826437L,
  546. 0.76316324570055724607L,
  547. 0.76192860532159583095L,
  548. 0.95107465366842792679L,
  549. 1.3151806717031612153L,
  550. 1.9285606934774109412L,
  551. 2.9375093425313787550L,
  552. 4.5948944054428780618L,
  553. 7.3300712218817207718L,
  554. 11.871512597425301798L,
  555. 19.458513748229377383L,
  556. 32.206386572464268628L,
  557. 53.737491987005546559L,
  558. 90.273886029409988491L,
  559. 152.53312130253275268L,
  560. 259.02388747148299086L,
  561. 441.78537518096201946L,
  562. 756.39903981567380952L
  563. };
  564. return boost::math::tools::evaluate_polynomial(coef, m - 0.45L);
  565. }
  566. case 10:
  567. case 11:
  568. {
  569. constexpr T coef[] =
  570. {
  571. 1.8989249102715535257L,
  572. 0.95052179461824443490L,
  573. 1.1510775899590158079L,
  574. 1.7502391069863005399L,
  575. 2.9526768126368751802L,
  576. 5.2858003961214508892L,
  577. 9.8324857166599797471L,
  578. 18.787148683275595622L,
  579. 36.614686152736981447L,
  580. 72.452923951277718013L,
  581. 145.10795773470691023L,
  582. 293.47863963084970259L,
  583. 598.38518150550101790L,
  584. 1228.4200130758634505L,
  585. 2536.5297553827644880L,
  586. 5263.9832725075189576L,
  587. 10972.138126273491753L,
  588. 22958.388550988306870L,
  589. 48203.103373625406989L
  590. };
  591. return boost::math::tools::evaluate_polynomial(coef, m - 0.55L);
  592. }
  593. case 12:
  594. case 13:
  595. {
  596. constexpr T coef[] =
  597. {
  598. 2.0075983984243763017L,
  599. 1.2484572312123473371L,
  600. 1.9262346570764797287L,
  601. 3.7512896400875876798L,
  602. 8.1199445549320458022L,
  603. 18.665721308735553611L,
  604. 44.603924842914370633L,
  605. 109.50920543094983774L,
  606. 274.27795482324134804L,
  607. 697.55980086063261629L,
  608. 1795.7160145002471293L,
  609. 4668.3817167903899100L,
  610. 12235.762468136643348L,
  611. 32290.178097183208178L,
  612. 85713.076081959646847L,
  613. 228672.18904931170958L,
  614. 612757.27119158527740L,
  615. 1.6483233976504668314e6L,
  616. 4.4492251046211960936e6L,
  617. 1.2046317340783185238e7L,
  618. 3.2705187507963254185e7L
  619. };
  620. return boost::math::tools::evaluate_polynomial(coef, m - 0.65L);
  621. }
  622. case 14:
  623. case 15:
  624. {
  625. constexpr T coef[] =
  626. {
  627. 2.1565156474996432354L,
  628. 1.7918056418494632425L,
  629. 3.8267512874657131470L,
  630. 10.386724683637972080L,
  631. 31.403314054680702901L,
  632. 100.92370394986954165L,
  633. 337.32682826322728966L,
  634. 1158.7079305678279173L,
  635. 4060.9907421936320917L,
  636. 14454.001840343447947L,
  637. 52076.661075994048028L,
  638. 189493.65914621568866L,
  639. 695184.57624138961450L,
  640. 2.5679940482552846861e6L,
  641. 9.5419219667483863221e6L,
  642. 3.5634927442180761743e7L,
  643. 1.3366929846120408712e8L,
  644. 5.0335218668662845411e8L,
  645. 1.9019757295386601192e9L,
  646. 7.2089150153301037563e9L,
  647. 2.7398741806339510931e10L,
  648. 1.0439286724885300495e11L,
  649. 3.9864875581513728207e11L,
  650. 1.5254661585564745591e12L,
  651. 5.8483259088850315936e12
  652. };
  653. return boost::math::tools::evaluate_polynomial(coef, m - 0.75L);
  654. }
  655. case 16:
  656. {
  657. constexpr T coef[] =
  658. {
  659. 2.3181226217125105894L,
  660. 2.6169201502912328409L,
  661. 7.8979350757313558232L,
  662. 30.502397154466723270L,
  663. 131.48693655235284561L,
  664. 602.98476373564916170L,
  665. 2877.0246178099726410L,
  666. 14110.519919151803247L,
  667. 70621.440881565402289L,
  668. 358977.26658253099258L,
  669. 1.8472382637239716844e6L,
  670. 9.6005154160492141090e6L,
  671. 5.0307677085023668786e7L,
  672. 2.6544418865271279673e8L,
  673. 1.4088623250287026866e9L,
  674. 7.5156879353737746270e9L,
  675. 4.0270783964955246149e10L,
  676. 2.1662089325801126339e11L,
  677. 1.1692489201929996116e12L,
  678. 6.3306543358985679881e12
  679. };
  680. return boost::math::tools::evaluate_polynomial(coef, m - 0.825L);
  681. }
  682. case 17:
  683. {
  684. constexpr T coef[] =
  685. {
  686. 2.4735961737513439120L,
  687. 3.7276242441180993105L,
  688. 15.607393035549304964L,
  689. 84.128508428058877470L,
  690. 506.98181970406139349L,
  691. 3252.2770581451236438L,
  692. 21713.242419574342564L,
  693. 149037.04518909327662L,
  694. 1.0439993310899908390e6L,
  695. 7.4279748170420389947e6L,
  696. 5.3503839675586611510e7L,
  697. 3.8924988699487084738e8L,
  698. 2.8552883511008106195e9L,
  699. 2.1090077038766840525e10L,
  700. 1.5669983394779020136e11L,
  701. 1.1702222424224398927e12L,
  702. 8.7779483236689379709e12L,
  703. 6.6101242752484950408e13L,
  704. 4.9948805371338879891e14L,
  705. 3.7859743397240299201e15L,
  706. 2.8775996123036112296e16L,
  707. 2.1926346839925760143e17L,
  708. 1.6744985438468349361e18L,
  709. 1.2814410112866546052e19L,
  710. 9.8249807041031260167e19
  711. };
  712. return boost::math::tools::evaluate_polynomial(coef, m - 0.875L);
  713. }
  714. default:
  715. //
  716. // All cases where m > 0.9
  717. // including all error handling:
  718. //
  719. return ellint_k_imp(k, pol, std::integral_constant<int, 2>());
  720. }
  721. }
  722. template <typename T, typename Policy>
  723. BOOST_MATH_FORCEINLINE typename tools::promote_args<T>::type ellint_1(T k, const Policy& pol, const std::true_type&)
  724. {
  725. typedef typename tools::promote_args<T>::type result_type;
  726. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  727. typedef std::integral_constant<int,
  728. #if defined(__clang_major__) && (__clang_major__ == 7)
  729. 2
  730. #else
  731. std::is_floating_point<T>::value && std::numeric_limits<T>::digits && (std::numeric_limits<T>::digits <= 54) ? 0 :
  732. std::is_floating_point<T>::value && std::numeric_limits<T>::digits && (std::numeric_limits<T>::digits <= 64) ? 1 : 2
  733. #endif
  734. > precision_tag_type;
  735. return policies::checked_narrowing_cast<result_type, Policy>(detail::ellint_k_imp(static_cast<value_type>(k), pol, precision_tag_type()), "boost::math::ellint_1<%1%>(%1%)");
  736. }
  737. template <class T1, class T2>
  738. BOOST_MATH_FORCEINLINE typename tools::promote_args<T1, T2>::type ellint_1(T1 k, T2 phi, const std::false_type&)
  739. {
  740. return boost::math::ellint_1(k, phi, policies::policy<>());
  741. }
  742. }
  743. // Complete elliptic integral (Legendre form) of the first kind
  744. template <typename T>
  745. BOOST_MATH_FORCEINLINE typename tools::promote_args<T>::type ellint_1(T k)
  746. {
  747. return ellint_1(k, policies::policy<>());
  748. }
  749. // Elliptic integral (Legendre form) of the first kind
  750. template <class T1, class T2, class Policy>
  751. BOOST_MATH_FORCEINLINE typename tools::promote_args<T1, T2>::type ellint_1(T1 k, T2 phi, const Policy& pol) // LCOV_EXCL_LINE gcc misses this but sees the function body, strange!
  752. {
  753. typedef typename tools::promote_args<T1, T2>::type result_type;
  754. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  755. return policies::checked_narrowing_cast<result_type, Policy>(detail::ellint_f_imp(static_cast<value_type>(phi), static_cast<value_type>(k), pol), "boost::math::ellint_1<%1%>(%1%,%1%)");
  756. }
  757. template <class T1, class T2>
  758. BOOST_MATH_FORCEINLINE typename tools::promote_args<T1, T2>::type ellint_1(T1 k, T2 phi)
  759. {
  760. typedef typename policies::is_policy<T2>::type tag_type;
  761. return detail::ellint_1(k, phi, tag_type());
  762. }
  763. }} // namespaces
  764. #endif // BOOST_MATH_ELLINT_1_HPP