series_expansion.hpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. // Boost.Geometry
  2. // Copyright (c) 2018 Adeel Ahmad, Islamabad, Pakistan.
  3. // Contributed and/or modified by Adeel Ahmad, as part of Google Summer of Code 2018 program.
  4. // This file was modified by Oracle on 2019.
  5. // Modifications copyright (c) 2019 Oracle and/or its affiliates.
  6. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  7. // Use, modification and distribution is subject to the Boost Software License,
  8. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. // This file is converted from GeographicLib, https://geographiclib.sourceforge.io
  11. // GeographicLib is originally written by Charles Karney.
  12. // Author: Charles Karney (2008-2017)
  13. // Last updated version of GeographicLib: 1.49
  14. // Original copyright notice:
  15. // Copyright (c) Charles Karney (2008-2017) <charles@karney.com> and licensed
  16. // under the MIT/X11 License. For more information, see
  17. // https://geographiclib.sourceforge.io
  18. #ifndef BOOST_GEOMETRY_UTIL_SERIES_EXPANSION_HPP
  19. #define BOOST_GEOMETRY_UTIL_SERIES_EXPANSION_HPP
  20. #include <boost/geometry/core/assert.hpp>
  21. #include <boost/geometry/util/math.hpp>
  22. namespace boost { namespace geometry { namespace series_expansion {
  23. /*
  24. Generate and evaluate the series expansion of the following integral
  25. I1 = integrate( sqrt(1+k2*sin(sigma1)^2), sigma1, 0, sigma )
  26. which is valid for k2 small. We substitute k2 = 4 * eps / (1 - eps)^2
  27. and expand (1 - eps) * I1 retaining terms up to order eps^maxpow
  28. in A1 and C1[l].
  29. The resulting series is of the form
  30. A1 * ( sigma + sum(C1[l] * sin(2*l*sigma), l, 1, maxpow) ).
  31. The scale factor A1-1 = mean value of (d/dsigma)I1 - 1
  32. The expansion above is performed in Maxima, a Computer Algebra System.
  33. The C++ code (that yields the function evaluate_A1 below) is
  34. generated by the following Maxima script:
  35. geometry/doc/other/maxima/geod.mac
  36. To replace each number x by CT(x) the following
  37. script can be used:
  38. sed -e 's/[0-9]\+/CT(&)/g; s/\[CT/\[/g; s/)\]/\]/g;
  39. s/case\sCT(/case /g; s/):/:/g; s/epsCT(2)/eps2/g;'
  40. */
  41. template <size_t SeriesOrder, typename CT>
  42. inline CT evaluate_A1(CT eps)
  43. {
  44. CT eps2 = math::sqr(eps);
  45. CT t;
  46. switch (SeriesOrder/2) {
  47. case 0:
  48. t = CT(0);
  49. break;
  50. case 1:
  51. t = eps2/CT(4);
  52. break;
  53. case 2:
  54. t = eps2*(eps2+CT(16))/CT(64);
  55. break;
  56. case 3:
  57. t = eps2*(eps2*(eps2+CT(4))+CT(64))/CT(256);
  58. break;
  59. case 4:
  60. t = eps2*(eps2*(eps2*(CT(25)*eps2+CT(64))+CT(256))+CT(4096))/CT(16384);
  61. break;
  62. }
  63. return (t + eps) / (CT(1) - eps);
  64. }
  65. /*
  66. Generate and evaluate the series expansion of the following integral
  67. I2 = integrate( 1/sqrt(1+k2*sin(sigma1)^2), sigma1, 0, sigma )
  68. which is valid for k2 small. We substitute k2 = 4 * eps / (1 - eps)^2
  69. and expand (1 - eps) * I2 retaining terms up to order eps^maxpow
  70. in A2 and C2[l].
  71. The resulting series is of the form
  72. A2 * ( sigma + sum(C2[l] * sin(2*l*sigma), l, 1, maxpow) )
  73. The scale factor A2-1 = mean value of (d/dsigma)2 - 1
  74. The expansion above is performed in Maxima, a Computer Algebra System.
  75. The C++ code (that yields the function evaluate_A2 below) is
  76. generated by the following Maxima script:
  77. geometry/doc/other/maxima/geod.mac
  78. */
  79. template <size_t SeriesOrder, typename CT>
  80. inline CT evaluate_A2(CT const& eps)
  81. {
  82. CT const eps2 = math::sqr(eps);
  83. CT t;
  84. switch (SeriesOrder/2) {
  85. case 0:
  86. t = CT(0);
  87. break;
  88. case 1:
  89. t = -CT(3)*eps2/CT(4);
  90. break;
  91. case 2:
  92. t = (-CT(7)*eps2-CT(48))*eps2/CT(64);
  93. break;
  94. case 3:
  95. t = eps2*((-CT(11)*eps2-CT(28))*eps2-CT(192))/CT(256);
  96. break;
  97. default:
  98. t = eps2*(eps2*((-CT(375)*eps2-CT(704))*eps2-CT(1792))-CT(12288))/CT(16384);
  99. break;
  100. }
  101. return (t - eps) / (CT(1) + eps);
  102. }
  103. /*
  104. Express
  105. I3 = integrate( (2-f)/(1+(1-f)*sqrt(1+k2*sin(sigma1)^2)), sigma1, 0, sigma )
  106. as a series
  107. A3 * ( sigma + sum(C3[l] * sin(2*l*sigma), l, 1, maxpow-1) )
  108. valid for f and k2 small. It is convenient to write k2 = 4 * eps / (1 -
  109. eps)^2 and f = 2*n/(1+n) and expand in eps and n. This procedure leads
  110. to a series where the coefficients of eps^j are terminating series in n.
  111. The scale factor A3 = mean value of (d/dsigma)I3
  112. The expansion above is performed in Maxima, a Computer Algebra System.
  113. The C++ code (that yields the function evaluate_coeffs_A3 below) is
  114. generated by the following Maxima script:
  115. geometry/doc/other/maxima/geod.mac
  116. */
  117. template <typename Coeffs, typename CT>
  118. inline void evaluate_coeffs_A3(Coeffs &c, CT const& n)
  119. {
  120. switch (int(Coeffs::static_size)) {
  121. case 0:
  122. break;
  123. case 1:
  124. c[0] = CT(1);
  125. break;
  126. case 2:
  127. c[0] = CT(1);
  128. c[1] = -CT(1)/CT(2);
  129. break;
  130. case 3:
  131. c[0] = CT(1);
  132. c[1] = (n-CT(1))/CT(2);
  133. c[2] = -CT(1)/CT(4);
  134. break;
  135. case 4:
  136. c[0] = CT(1);
  137. c[1] = (n-CT(1))/CT(2);
  138. c[2] = (-n-CT(2))/CT(8);
  139. c[3] = -CT(1)/CT(16);
  140. break;
  141. case 5:
  142. c[0] = CT(1);
  143. c[1] = (n-CT(1))/CT(2);
  144. c[2] = (n*(CT(3)*n-CT(1))-CT(2))/CT(8);
  145. c[3] = (-CT(3)*n-CT(1))/CT(16);
  146. c[4] = -CT(3)/CT(64);
  147. break;
  148. case 6:
  149. c[0] = CT(1);
  150. c[1] = (n-CT(1))/CT(2);
  151. c[2] = (n*(CT(3)*n-CT(1))-CT(2))/CT(8);
  152. c[3] = ((-n-CT(3))*n-CT(1))/CT(16);
  153. c[4] = (-CT(2)*n-CT(3))/CT(64);
  154. c[5] = -CT(3)/CT(128);
  155. break;
  156. case 7:
  157. c[0] = CT(1);
  158. c[1] = (n-CT(1))/CT(2);
  159. c[2] = (n*(CT(3)*n-CT(1))-CT(2))/CT(8);
  160. c[3] = (n*(n*(CT(5)*n-CT(1))-CT(3))-CT(1))/CT(16);
  161. c[4] = ((-CT(10)*n-CT(2))*n-CT(3))/CT(64);
  162. c[5] = (-CT(5)*n-CT(3))/CT(128);
  163. c[6] = -CT(5)/CT(256);
  164. break;
  165. default:
  166. c[0] = CT(1);
  167. c[1] = (n-CT(1))/CT(2);
  168. c[2] = (n*(CT(3)*n-CT(1))-CT(2))/CT(8);
  169. c[3] = (n*(n*(CT(5)*n-CT(1))-CT(3))-CT(1))/CT(16);
  170. c[4] = (n*((-CT(5)*n-CT(20))*n-CT(4))-CT(6))/CT(128);
  171. c[5] = ((-CT(5)*n-CT(10))*n-CT(6))/CT(256);
  172. c[6] = (-CT(15)*n-CT(20))/CT(1024);
  173. c[7] = -CT(25)/CT(2048);
  174. break;
  175. }
  176. }
  177. /*
  178. The coefficients C1[l] in the Fourier expansion of B1.
  179. The expansion below is performed in Maxima, a Computer Algebra System.
  180. The C++ code (that yields the function evaluate_coeffs_C1 below) is
  181. generated by the following Maxima script:
  182. geometry/doc/other/maxima/geod.mac
  183. */
  184. template <typename Coeffs, typename CT>
  185. inline void evaluate_coeffs_C1(Coeffs &c, CT const& eps)
  186. {
  187. CT eps2 = math::sqr(eps);
  188. CT d = eps;
  189. switch (int(Coeffs::static_size) - 1) {
  190. case 0:
  191. break;
  192. case 1:
  193. c[1] = -d/CT(2);
  194. break;
  195. case 2:
  196. c[1] = -d/CT(2);
  197. d *= eps;
  198. c[2] = -d/CT(16);
  199. break;
  200. case 3:
  201. c[1] = d*(CT(3)*eps2-CT(8))/CT(16);
  202. d *= eps;
  203. c[2] = -d/CT(16);
  204. d *= eps;
  205. c[3] = -d/CT(48);
  206. break;
  207. case 4:
  208. c[1] = d*(CT(3)*eps2-CT(8))/CT(16);
  209. d *= eps;
  210. c[2] = d*(eps2-CT(2))/CT(32);
  211. d *= eps;
  212. c[3] = -d/CT(48);
  213. d *= eps;
  214. c[4] = -CT(5)*d/CT(512);
  215. break;
  216. case 5:
  217. c[1] = d*((CT(6)-eps2)*eps2-CT(16))/CT(32);
  218. d *= eps;
  219. c[2] = d*(eps2-CT(2))/CT(32);
  220. d *= eps;
  221. c[3] = d*(CT(9)*eps2-CT(16))/CT(768);
  222. d *= eps;
  223. c[4] = -CT(5)*d/CT(512);
  224. d *= eps;
  225. c[5] = -CT(7)*d/CT(1280);
  226. break;
  227. case 6:
  228. c[1] = d*((CT(6)-eps2)*eps2-CT(16))/CT(32);
  229. d *= eps;
  230. c[2] = d*((CT(64)-CT(9)*eps2)*eps2-CT(128))/CT(2048);
  231. d *= eps;
  232. c[3] = d*(CT(9)*eps2-CT(16))/CT(768);
  233. d *= eps;
  234. c[4] = d*(CT(3)*eps2-CT(5))/CT(512);
  235. d *= eps;
  236. c[5] = -CT(7)*d/CT(1280);
  237. d *= eps;
  238. c[6] = -CT(7)*d/CT(2048);
  239. break;
  240. case 7:
  241. c[1] = d*(eps2*(eps2*(CT(19)*eps2-CT(64))+CT(384))-CT(1024))/CT(2048);
  242. d *= eps;
  243. c[2] = d*((CT(64)-CT(9)*eps2)*eps2-CT(128))/CT(2048);
  244. d *= eps;
  245. c[3] = d*((CT(72)-CT(9)*eps2)*eps2-CT(128))/CT(6144);
  246. d *= eps;
  247. c[4] = d*(CT(3)*eps2-CT(5))/CT(512);
  248. d *= eps;
  249. c[5] = d*(CT(35)*eps2-CT(56))/CT(10240);
  250. d *= eps;
  251. c[6] = -CT(7)*d/CT(2048);
  252. d *= eps;
  253. c[7] = -CT(33)*d/CT(14336);
  254. break;
  255. default:
  256. c[1] = d*(eps2*(eps2*(CT(19)*eps2-CT(64))+CT(384))-CT(1024))/CT(2048);
  257. d *= eps;
  258. c[2] = d*(eps2*(eps2*(CT(7)*eps2-CT(18))+CT(128))-CT(256))/CT(4096);
  259. d *= eps;
  260. c[3] = d*((CT(72)-CT(9)*eps2)*eps2-CT(128))/CT(6144);
  261. d *= eps;
  262. c[4] = d*((CT(96)-CT(11)*eps2)*eps2-CT(160))/CT(16384);
  263. d *= eps;
  264. c[5] = d*(CT(35)*eps2-CT(56))/CT(10240);
  265. d *= eps;
  266. c[6] = d*(CT(9)*eps2-CT(14))/CT(4096);
  267. d *= eps;
  268. c[7] = -CT(33)*d/CT(14336);
  269. d *= eps;
  270. c[8] = -CT(429)*d/CT(262144);
  271. break;
  272. }
  273. }
  274. /*
  275. The coefficients C1p[l] in the Fourier expansion of B1p.
  276. The expansion below is performed in Maxima, a Computer Algebra System.
  277. The C++ code (that yields the function evaluate_coeffs_C1p below) is
  278. generated by the following Maxima script:
  279. geometry/doc/other/maxima/geod.mac
  280. */
  281. template <typename Coeffs, typename CT>
  282. inline void evaluate_coeffs_C1p(Coeffs& c, CT const& eps)
  283. {
  284. CT const eps2 = math::sqr(eps);
  285. CT d = eps;
  286. switch (int(Coeffs::static_size) - 1) {
  287. case 0:
  288. break;
  289. case 1:
  290. c[1] = d/CT(2);
  291. break;
  292. case 2:
  293. c[1] = d/CT(2);
  294. d *= eps;
  295. c[2] = CT(5)*d/CT(16);
  296. break;
  297. case 3:
  298. c[1] = d*(CT(16)-CT(9)*eps2)/CT(32);
  299. d *= eps;
  300. c[2] = CT(5)*d/CT(16);
  301. d *= eps;
  302. c[3] = CT(29)*d/CT(96);
  303. break;
  304. case 4:
  305. c[1] = d*(CT(16)-CT(9)*eps2)/CT(32);
  306. d *= eps;
  307. c[2] = d*(CT(30)-CT(37)*eps2)/CT(96);
  308. d *= eps;
  309. c[3] = CT(29)*d/CT(96);
  310. d *= eps;
  311. c[4] = CT(539)*d/CT(1536);
  312. break;
  313. case 5:
  314. c[1] = d*(eps2*(CT(205)*eps2-CT(432))+CT(768))/CT(1536);
  315. d *= eps;
  316. c[2] = d*(CT(30)-CT(37)*eps2)/CT(96);
  317. d *= eps;
  318. c[3] = d*(CT(116)-CT(225)*eps2)/CT(384);
  319. d *= eps;
  320. c[4] = CT(539)*d/CT(1536);
  321. d *= eps;
  322. c[5] = CT(3467)*d/CT(7680);
  323. break;
  324. case 6:
  325. c[1] = d*(eps2*(CT(205)*eps2-CT(432))+CT(768))/CT(1536);
  326. d *= eps;
  327. c[2] = d*(eps2*(CT(4005)*eps2-CT(4736))+CT(3840))/CT(12288);
  328. d *= eps;
  329. c[3] = d*(CT(116)-CT(225)*eps2)/CT(384);
  330. d *= eps;
  331. c[4] = d*(CT(2695)-CT(7173)*eps2)/CT(7680);
  332. d *= eps;
  333. c[5] = CT(3467)*d/CT(7680);
  334. d *= eps;
  335. c[6] = CT(38081)*d/CT(61440);
  336. break;
  337. case 7:
  338. c[1] = d*(eps2*((CT(9840)-CT(4879)*eps2)*eps2-CT(20736))+CT(36864))/CT(73728);
  339. d *= eps;
  340. c[2] = d*(eps2*(CT(4005)*eps2-CT(4736))+CT(3840))/CT(12288);
  341. d *= eps;
  342. c[3] = d*(eps2*(CT(8703)*eps2-CT(7200))+CT(3712))/CT(12288);
  343. d *= eps;
  344. c[4] = d*(CT(2695)-CT(7173)*eps2)/CT(7680);
  345. d *= eps;
  346. c[5] = d*(CT(41604)-CT(141115)*eps2)/CT(92160);
  347. d *= eps;
  348. c[6] = CT(38081)*d/CT(61440);
  349. d *= eps;
  350. c[7] = CT(459485)*d/CT(516096);
  351. break;
  352. default:
  353. c[1] = d*(eps2*((CT(9840)-CT(4879)*eps2)*eps2-CT(20736))+CT(36864))/CT(73728);
  354. d *= eps;
  355. c[2] = d*(eps2*((CT(120150)-CT(86171)*eps2)*eps2-CT(142080))+CT(115200))/CT(368640);
  356. d *= eps;
  357. c[3] = d*(eps2*(CT(8703)*eps2-CT(7200))+CT(3712))/CT(12288);
  358. d *= eps;
  359. c[4] = d*(eps2*(CT(1082857)*eps2-CT(688608))+CT(258720))/CT(737280);
  360. d *= eps;
  361. c[5] = d*(CT(41604)-CT(141115)*eps2)/CT(92160);
  362. d *= eps;
  363. c[6] = d*(CT(533134)-CT(2200311)*eps2)/CT(860160);
  364. d *= eps;
  365. c[7] = CT(459485)*d/CT(516096);
  366. d *= eps;
  367. c[8] = CT(109167851)*d/CT(82575360);
  368. break;
  369. }
  370. }
  371. /*
  372. The coefficients C2[l] in the Fourier expansion of B2.
  373. The expansion below is performed in Maxima, a Computer Algebra System.
  374. The C++ code (that yields the function evaluate_coeffs_C2 below) is
  375. generated by the following Maxima script:
  376. geometry/doc/other/maxima/geod.mac
  377. */
  378. template <typename Coeffs, typename CT>
  379. inline void evaluate_coeffs_C2(Coeffs& c, CT const& eps)
  380. {
  381. CT const eps2 = math::sqr(eps);
  382. CT d = eps;
  383. switch (int(Coeffs::static_size) - 1) {
  384. case 0:
  385. break;
  386. case 1:
  387. c[1] = d/CT(2);
  388. break;
  389. case 2:
  390. c[1] = d/CT(2);
  391. d *= eps;
  392. c[2] = CT(3)*d/CT(16);
  393. break;
  394. case 3:
  395. c[1] = d*(eps2+CT(8))/CT(16);
  396. d *= eps;
  397. c[2] = CT(3)*d/CT(16);
  398. d *= eps;
  399. c[3] = CT(5)*d/CT(48);
  400. break;
  401. case 4:
  402. c[1] = d*(eps2+CT(8))/CT(16);
  403. d *= eps;
  404. c[2] = d*(eps2+CT(6))/CT(32);
  405. d *= eps;
  406. c[3] = CT(5)*d/CT(48);
  407. d *= eps;
  408. c[4] = CT(35)*d/CT(512);
  409. break;
  410. case 5:
  411. c[1] = d*(eps2*(eps2+CT(2))+CT(16))/CT(32);
  412. d *= eps;
  413. c[2] = d*(eps2+CT(6))/CT(32);
  414. d *= eps;
  415. c[3] = d*(CT(15)*eps2+CT(80))/CT(768);
  416. d *= eps;
  417. c[4] = CT(35)*d/CT(512);
  418. d *= eps;
  419. c[5] = CT(63)*d/CT(1280);
  420. break;
  421. case 6:
  422. c[1] = d*(eps2*(eps2+CT(2))+CT(16))/CT(32);
  423. d *= eps;
  424. c[2] = d*(eps2*(CT(35)*eps2+CT(64))+CT(384))/CT(2048);
  425. d *= eps;
  426. c[3] = d*(CT(15)*eps2+CT(80))/CT(768);
  427. d *= eps;
  428. c[4] = d*(CT(7)*eps2+CT(35))/CT(512);
  429. d *= eps;
  430. c[5] = CT(63)*d/CT(1280);
  431. d *= eps;
  432. c[6] = CT(77)*d/CT(2048);
  433. break;
  434. case 7:
  435. c[1] = d*(eps2*(eps2*(CT(41)*eps2+CT(64))+CT(128))+CT(1024))/CT(2048);
  436. d *= eps;
  437. c[2] = d*(eps2*(CT(35)*eps2+CT(64))+CT(384))/CT(2048);
  438. d *= eps;
  439. c[3] = d*(eps2*(CT(69)*eps2+CT(120))+CT(640))/CT(6144);
  440. d *= eps;
  441. c[4] = d*(CT(7)*eps2+CT(35))/CT(512);
  442. d *= eps;
  443. c[5] = d*(CT(105)*eps2+CT(504))/CT(10240);
  444. d *= eps;
  445. c[6] = CT(77)*d/CT(2048);
  446. d *= eps;
  447. c[7] = CT(429)*d/CT(14336);
  448. break;
  449. default:
  450. c[1] = d*(eps2*(eps2*(CT(41)*eps2+CT(64))+CT(128))+CT(1024))/CT(2048);
  451. d *= eps;
  452. c[2] = d*(eps2*(eps2*(CT(47)*eps2+CT(70))+CT(128))+CT(768))/CT(4096);
  453. d *= eps;
  454. c[3] = d*(eps2*(CT(69)*eps2+CT(120))+CT(640))/CT(6144);
  455. d *= eps;
  456. c[4] = d*(eps2*(CT(133)*eps2+CT(224))+CT(1120))/CT(16384);
  457. d *= eps;
  458. c[5] = d*(CT(105)*eps2+CT(504))/CT(10240);
  459. d *= eps;
  460. c[6] = d*(CT(33)*eps2+CT(154))/CT(4096);
  461. d *= eps;
  462. c[7] = CT(429)*d/CT(14336);
  463. d *= eps;
  464. c[8] = CT(6435)*d/CT(262144);
  465. break;
  466. }
  467. }
  468. /*
  469. The coefficients C3[l] in the Fourier expansion of B3.
  470. The expansion below is performed in Maxima, a Computer Algebra System.
  471. The C++ code (that yields the function evaluate_coeffs_C3 below) is
  472. generated by the following Maxima script:
  473. geometry/doc/other/maxima/geod.mac
  474. */
  475. template <size_t SeriesOrder, typename Coeffs, typename CT>
  476. inline void evaluate_coeffs_C3x(Coeffs &c, CT const& n) {
  477. size_t const coeff_size = Coeffs::static_size;
  478. size_t const expected_size = (SeriesOrder * (SeriesOrder - 1)) / 2;
  479. BOOST_GEOMETRY_ASSERT((coeff_size == expected_size));
  480. const CT n2 = math::sqr(n);
  481. switch (SeriesOrder) {
  482. case 0:
  483. break;
  484. case 1:
  485. break;
  486. case 2:
  487. c[0] = (CT(1)-n)/CT(4);
  488. break;
  489. case 3:
  490. c[0] = (CT(1)-n)/CT(4);
  491. c[1] = (CT(1)-n2)/CT(8);
  492. c[2] = ((n-CT(3))*n+CT(2))/CT(32);
  493. break;
  494. case 4:
  495. c[0] = (CT(1)-n)/CT(4);
  496. c[1] = (CT(1)-n2)/CT(8);
  497. c[2] = (n*((-CT(5)*n-CT(1))*n+CT(3))+CT(3))/CT(64);
  498. c[3] = ((n-CT(3))*n+CT(2))/CT(32);
  499. c[4] = (n*(n*(CT(2)*n-CT(3))-CT(2))+CT(3))/CT(64);
  500. c[5] = (n*((CT(5)-n)*n-CT(9))+CT(5))/CT(192);
  501. break;
  502. case 5:
  503. c[0] = (CT(1)-n)/CT(4);
  504. c[1] = (CT(1)-n2)/CT(8);
  505. c[2] = (n*((-CT(5)*n-CT(1))*n+CT(3))+CT(3))/CT(64);
  506. c[3] = (n*((CT(2)-CT(2)*n)*n+CT(2))+CT(5))/CT(128);
  507. c[4] = ((n-CT(3))*n+CT(2))/CT(32);
  508. c[5] = (n*(n*(CT(2)*n-CT(3))-CT(2))+CT(3))/CT(64);
  509. c[6] = (n*((-CT(6)*n-CT(9))*n+CT(2))+CT(6))/CT(256);
  510. c[7] = (n*((CT(5)-n)*n-CT(9))+CT(5))/CT(192);
  511. c[8] = (n*(n*(CT(10)*n-CT(6))-CT(10))+CT(9))/CT(384);
  512. c[9] = (n*((CT(20)-CT(7)*n)*n-CT(28))+CT(14))/CT(1024);
  513. break;
  514. case 6:
  515. c[0] = (CT(1)-n)/CT(4);
  516. c[1] = (CT(1)-n2)/CT(8);
  517. c[2] = (n*((-CT(5)*n-CT(1))*n+CT(3))+CT(3))/CT(64);
  518. c[3] = (n*((CT(2)-CT(2)*n)*n+CT(2))+CT(5))/CT(128);
  519. c[4] = (n*(CT(3)*n+CT(11))+CT(12))/CT(512);
  520. c[5] = ((n-CT(3))*n+CT(2))/CT(32);
  521. c[6] = (n*(n*(CT(2)*n-CT(3))-CT(2))+CT(3))/CT(64);
  522. c[7] = (n*((-CT(6)*n-CT(9))*n+CT(2))+CT(6))/CT(256);
  523. c[8] = ((CT(1)-CT(2)*n)*n+CT(5))/CT(256);
  524. c[9] = (n*((CT(5)-n)*n-CT(9))+CT(5))/CT(192);
  525. c[10] = (n*(n*(CT(10)*n-CT(6))-CT(10))+CT(9))/CT(384);
  526. c[11] = ((-CT(77)*n-CT(8))*n+CT(42))/CT(3072);
  527. c[12] = (n*((CT(20)-CT(7)*n)*n-CT(28))+CT(14))/CT(1024);
  528. c[13] = ((-CT(7)*n-CT(40))*n+CT(28))/CT(2048);
  529. c[14] = (n*(CT(75)*n-CT(90))+CT(42))/CT(5120);
  530. break;
  531. case 7:
  532. c[0] = (CT(1)-n)/CT(4);
  533. c[1] = (CT(1)-n2)/CT(8);
  534. c[2] = (n*((-CT(5)*n-CT(1))*n+CT(3))+CT(3))/CT(64);
  535. c[3] = (n*((CT(2)-CT(2)*n)*n+CT(2))+CT(5))/CT(128);
  536. c[4] = (n*(CT(3)*n+CT(11))+CT(12))/CT(512);
  537. c[5] = (CT(10)*n+CT(21))/CT(1024);
  538. c[6] = ((n-CT(3))*n+CT(2))/CT(32);
  539. c[7] = (n*(n*(CT(2)*n-CT(3))-CT(2))+CT(3))/CT(64);
  540. c[8] = (n*((-CT(6)*n-CT(9))*n+CT(2))+CT(6))/CT(256);
  541. c[9] = ((CT(1)-CT(2)*n)*n+CT(5))/CT(256);
  542. c[10] = (CT(69)*n+CT(108))/CT(8192);
  543. c[11] = (n*((CT(5)-n)*n-CT(9))+CT(5))/CT(192);
  544. c[12] = (n*(n*(CT(10)*n-CT(6))-CT(10))+CT(9))/CT(384);
  545. c[13] = ((-CT(77)*n-CT(8))*n+CT(42))/CT(3072);
  546. c[14] = (CT(12)-n)/CT(1024);
  547. c[15] = (n*((CT(20)-CT(7)*n)*n-CT(28))+CT(14))/CT(1024);
  548. c[16] = ((-CT(7)*n-CT(40))*n+CT(28))/CT(2048);
  549. c[17] = (CT(72)-CT(43)*n)/CT(8192);
  550. c[18] = (n*(CT(75)*n-CT(90))+CT(42))/CT(5120);
  551. c[19] = (CT(9)-CT(15)*n)/CT(1024);
  552. c[20] = (CT(44)-CT(99)*n)/CT(8192);
  553. break;
  554. default:
  555. c[0] = (CT(1)-n)/CT(4);
  556. c[1] = (CT(1)-n2)/CT(8);
  557. c[2] = (n*((-CT(5)*n-CT(1))*n+CT(3))+CT(3))/CT(64);
  558. c[3] = (n*((CT(2)-CT(2)*n)*n+CT(2))+CT(5))/CT(128);
  559. c[4] = (n*(CT(3)*n+CT(11))+CT(12))/CT(512);
  560. c[5] = (CT(10)*n+CT(21))/CT(1024);
  561. c[6] = CT(243)/CT(16384);
  562. c[7] = ((n-CT(3))*n+CT(2))/CT(32);
  563. c[8] = (n*(n*(CT(2)*n-CT(3))-CT(2))+CT(3))/CT(64);
  564. c[9] = (n*((-CT(6)*n-CT(9))*n+CT(2))+CT(6))/CT(256);
  565. c[10] = ((CT(1)-CT(2)*n)*n+CT(5))/CT(256);
  566. c[11] = (CT(69)*n+CT(108))/CT(8192);
  567. c[12] = CT(187)/CT(16384);
  568. c[13] = (n*((CT(5)-n)*n-CT(9))+CT(5))/CT(192);
  569. c[14] = (n*(n*(CT(10)*n-CT(6))-CT(10))+CT(9))/CT(384);
  570. c[15] = ((-CT(77)*n-CT(8))*n+CT(42))/CT(3072);
  571. c[16] = (CT(12)-n)/CT(1024);
  572. c[17] = CT(139)/CT(16384);
  573. c[18] = (n*((CT(20)-CT(7)*n)*n-CT(28))+CT(14))/CT(1024);
  574. c[19] = ((-CT(7)*n-CT(40))*n+CT(28))/CT(2048);
  575. c[20] = (CT(72)-CT(43)*n)/CT(8192);
  576. c[21] = CT(127)/CT(16384);
  577. c[22] = (n*(CT(75)*n-CT(90))+CT(42))/CT(5120);
  578. c[23] = (CT(9)-CT(15)*n)/CT(1024);
  579. c[24] = CT(99)/CT(16384);
  580. c[25] = (CT(44)-CT(99)*n)/CT(8192);
  581. c[26] = CT(99)/CT(16384);
  582. c[27] = CT(429)/CT(114688);
  583. break;
  584. }
  585. }
  586. /*
  587. \brief Given the set of coefficients coeffs2[] evaluate on
  588. C3 and return the set of coefficients coeffs1[].
  589. Elements coeffs1[1] through coeffs1[SeriesOrder - 1] are set.
  590. */
  591. template <typename Coeffs1, typename Coeffs2, typename CT>
  592. inline void evaluate_coeffs_C3(Coeffs1 &coeffs1, Coeffs2 &coeffs2, CT const& eps)
  593. {
  594. CT mult = 1;
  595. size_t offset = 0;
  596. // i is the index of C3[i].
  597. for (size_t i = 1; i < Coeffs1::static_size; ++i)
  598. {
  599. // Order of polynomial in eps.
  600. size_t m = Coeffs1::static_size - i;
  601. mult *= eps;
  602. coeffs1[i] = mult * math::horner_evaluate(eps, coeffs2.begin() + offset,
  603. coeffs2.begin() + offset + m);
  604. offset += m;
  605. }
  606. // Post condition: offset == coeffs_C3_size
  607. }
  608. /*
  609. \brief Evaluate the following:
  610. y = sum(c[i] * sin(2*i * x), i, 1, n)
  611. using Clenshaw summation.
  612. */
  613. template <typename CT, typename Coeffs>
  614. inline CT sin_cos_series(CT const& sinx, CT const& cosx, Coeffs const& coeffs)
  615. {
  616. size_t n = Coeffs::static_size - 1;
  617. size_t index = 0;
  618. // Point to one beyond last element.
  619. index += (n + 1);
  620. CT ar = 2 * (cosx - sinx) * (cosx + sinx);
  621. // If n is odd, get the last element.
  622. CT k0 = n & 1 ? coeffs[--index] : 0;
  623. CT k1 = 0;
  624. // Make n even.
  625. n /= 2;
  626. while (n--) {
  627. // Unroll loop x 2, so accumulators return to their original role.
  628. k1 = ar * k0 - k1 + coeffs[--index];
  629. k0 = ar * k1 - k0 + coeffs[--index];
  630. }
  631. return 2 * sinx * cosx * k0;
  632. }
  633. /*
  634. The coefficient containers for the series expansions.
  635. These structs allow the caller to only know the series order.
  636. */
  637. template <size_t SeriesOrder, typename CT>
  638. struct coeffs_C1 : boost::array<CT, SeriesOrder + 1>
  639. {
  640. coeffs_C1(CT const& epsilon)
  641. {
  642. evaluate_coeffs_C1(*this, epsilon);
  643. }
  644. };
  645. template <size_t SeriesOrder, typename CT>
  646. struct coeffs_C1p : boost::array<CT, SeriesOrder + 1>
  647. {
  648. coeffs_C1p(CT const& epsilon)
  649. {
  650. evaluate_coeffs_C1p(*this, epsilon);
  651. }
  652. };
  653. template <size_t SeriesOrder, typename CT>
  654. struct coeffs_C2 : boost::array<CT, SeriesOrder + 1>
  655. {
  656. coeffs_C2(CT const& epsilon)
  657. {
  658. evaluate_coeffs_C2(*this, epsilon);
  659. }
  660. };
  661. template <size_t SeriesOrder, typename CT>
  662. struct coeffs_C3x : boost::array<CT, (SeriesOrder * (SeriesOrder - 1)) / 2>
  663. {
  664. coeffs_C3x(CT const& n)
  665. {
  666. evaluate_coeffs_C3x<SeriesOrder>(*this, n);
  667. }
  668. };
  669. template <size_t SeriesOrder, typename CT>
  670. struct coeffs_C3 : boost::array<CT, SeriesOrder>
  671. {
  672. coeffs_C3(CT const& n, CT const& epsilon)
  673. {
  674. coeffs_C3x<SeriesOrder, CT> coeffs_C3x(n);
  675. evaluate_coeffs_C3(*this, coeffs_C3x, epsilon);
  676. }
  677. };
  678. template <size_t SeriesOrder, typename CT>
  679. struct coeffs_A3 : boost::array<CT, SeriesOrder>
  680. {
  681. coeffs_A3(CT const& n)
  682. {
  683. evaluate_coeffs_A3(*this, n);
  684. }
  685. };
  686. }}} // namespace boost::geometry::series_expansion
  687. #endif // BOOST_GEOMETRY_UTIL_SERIES_EXPANSION_HPP