mpc.hpp 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright 2018 John Maddock. Distributed under the Boost
  3. // Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_MP_MPC_HPP
  6. #define BOOST_MP_MPC_HPP
  7. #include <cstdint>
  8. #include <boost/multiprecision/detail/standalone_config.hpp>
  9. #include <boost/multiprecision/detail/fpclassify.hpp>
  10. #include <boost/multiprecision/number.hpp>
  11. #include <boost/multiprecision/detail/digits.hpp>
  12. #include <boost/multiprecision/detail/atomic.hpp>
  13. #include <boost/multiprecision/traits/is_variable_precision.hpp>
  14. #include <boost/multiprecision/mpfr.hpp>
  15. #include <boost/multiprecision/detail/hash.hpp>
  16. #include <boost/multiprecision/detail/no_exceptions_support.hpp>
  17. #include <boost/multiprecision/detail/assert.hpp>
  18. #include <mpc.h>
  19. #include <cmath>
  20. #include <algorithm>
  21. #include <complex>
  22. #ifndef BOOST_MULTIPRECISION_MPFI_DEFAULT_PRECISION
  23. #define BOOST_MULTIPRECISION_MPFI_DEFAULT_PRECISION 20
  24. #endif
  25. namespace boost {
  26. namespace multiprecision {
  27. template <unsigned digits10>
  28. struct number_category<backends::mpc_complex_backend<digits10> > : public std::integral_constant<int, number_kind_complex>
  29. {};
  30. namespace backends {
  31. namespace detail {
  32. inline void mpc_copy_precision(mpc_t dest, const mpc_t src)
  33. {
  34. mpfr_prec_t p_dest = mpc_get_prec(dest);
  35. mpfr_prec_t p_src = mpc_get_prec(src);
  36. if (p_dest != p_src)
  37. mpc_set_prec(dest, p_src);
  38. }
  39. inline void mpc_copy_precision(mpc_t dest, const mpc_t src1, const mpc_t src2)
  40. {
  41. mpfr_prec_t p_dest = mpc_get_prec(dest);
  42. mpfr_prec_t p_src1 = mpc_get_prec(src1);
  43. mpfr_prec_t p_src2 = mpc_get_prec(src2);
  44. if (p_src2 > p_src1)
  45. p_src1 = p_src2;
  46. if (p_dest != p_src1)
  47. mpc_set_prec(dest, p_src1);
  48. }
  49. template <unsigned digits10>
  50. struct mpc_complex_imp
  51. {
  52. #ifdef BOOST_HAS_LONG_LONG
  53. using signed_types = std::tuple<long, long long> ;
  54. using unsigned_types = std::tuple<unsigned long, unsigned long long>;
  55. #else
  56. using signed_types = std::tuple<long> ;
  57. using unsigned_types = std::tuple<unsigned long>;
  58. #endif
  59. using float_types = std::tuple<double, long double>;
  60. using exponent_type = long ;
  61. mpc_complex_imp()
  62. {
  63. mpc_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  64. mpc_set_ui(m_data, 0u, GMP_RNDN);
  65. }
  66. mpc_complex_imp(unsigned digits2)
  67. {
  68. mpc_init2(m_data, digits2);
  69. mpc_set_ui(m_data, 0u, GMP_RNDN);
  70. }
  71. mpc_complex_imp(const mpc_complex_imp& o)
  72. {
  73. mpc_init2(m_data, preserve_source_precision() ? mpc_get_prec(o.data()) : boost::multiprecision::detail::digits10_2_2(get_default_precision()));
  74. if (o.m_data[0].re[0]._mpfr_d)
  75. mpc_set(m_data, o.m_data, GMP_RNDN);
  76. }
  77. // rvalue copy
  78. mpc_complex_imp(mpc_complex_imp&& o) noexcept
  79. {
  80. mpfr_prec_t binary_default_precision = boost::multiprecision::detail::digits10_2_2(get_default_precision());
  81. if ((this->get_default_options() != variable_precision_options::preserve_target_precision) || (mpc_get_prec(o.data()) == binary_default_precision))
  82. {
  83. m_data[0] = o.m_data[0];
  84. o.m_data[0].re[0]._mpfr_d = nullptr;
  85. }
  86. else
  87. {
  88. // NOTE: C allocation interface must not throw:
  89. mpc_init2(m_data, binary_default_precision);
  90. if (o.m_data[0].re[0]._mpfr_d)
  91. mpc_set(m_data, o.m_data, GMP_RNDN);
  92. }
  93. }
  94. mpc_complex_imp& operator=(const mpc_complex_imp& o)
  95. {
  96. if ((o.m_data[0].re[0]._mpfr_d) && (this != &o))
  97. {
  98. if (m_data[0].re[0]._mpfr_d == nullptr)
  99. mpc_init2(m_data, preserve_source_precision() ? mpc_get_prec(o.m_data) : boost::multiprecision::detail::digits10_2_2(get_default_precision()));
  100. else if (preserve_source_precision() && (mpc_get_prec(o.data()) != mpc_get_prec(data())))
  101. {
  102. mpc_set_prec(m_data, mpc_get_prec(o.m_data));
  103. }
  104. mpc_set(m_data, o.m_data, GMP_RNDN);
  105. }
  106. return *this;
  107. }
  108. // rvalue assign
  109. mpc_complex_imp& operator=(mpc_complex_imp&& o) noexcept
  110. {
  111. if ((this->get_default_options() != variable_precision_options::preserve_target_precision) || (mpc_get_prec(o.data()) == mpc_get_prec(data())))
  112. mpc_swap(m_data, o.m_data);
  113. else
  114. *this = static_cast<const mpc_complex_imp&>(o);
  115. return *this;
  116. }
  117. #ifdef BOOST_HAS_LONG_LONG
  118. #ifdef _MPFR_H_HAVE_INTMAX_T
  119. mpc_complex_imp& operator=(unsigned long long i)
  120. {
  121. if (m_data[0].re[0]._mpfr_d == nullptr)
  122. mpc_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  123. mpc_set_uj(data(), i, GMP_RNDN);
  124. return *this;
  125. }
  126. mpc_complex_imp& operator=(long long i)
  127. {
  128. if (m_data[0].re[0]._mpfr_d == nullptr)
  129. mpc_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  130. mpc_set_sj(data(), i, GMP_RNDN);
  131. return *this;
  132. }
  133. #else
  134. mpc_complex_imp& operator=(unsigned long long i)
  135. {
  136. mpfr_float_backend<digits10> f(0uL, mpc_get_prec(m_data));
  137. f = i;
  138. mpc_set_fr(this->data(), f.data(), GMP_RNDN);
  139. return *this;
  140. }
  141. mpc_complex_imp& operator=(long long i)
  142. {
  143. mpfr_float_backend<digits10> f(0uL, mpc_get_prec(m_data));
  144. f = i;
  145. mpc_set_fr(this->data(), f.data(), GMP_RNDN);
  146. return *this;
  147. }
  148. #endif
  149. #endif
  150. mpc_complex_imp& operator=(unsigned long i)
  151. {
  152. if (m_data[0].re[0]._mpfr_d == nullptr)
  153. mpc_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  154. mpc_set_ui(m_data, i, GMP_RNDN);
  155. return *this;
  156. }
  157. mpc_complex_imp& operator=(long i)
  158. {
  159. if (m_data[0].re[0]._mpfr_d == nullptr)
  160. mpc_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  161. mpc_set_si(m_data, i, GMP_RNDN);
  162. return *this;
  163. }
  164. mpc_complex_imp& operator=(double d)
  165. {
  166. if (m_data[0].re[0]._mpfr_d == nullptr)
  167. mpc_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  168. mpc_set_d(m_data, d, GMP_RNDN);
  169. return *this;
  170. }
  171. mpc_complex_imp& operator=(long double d)
  172. {
  173. if (m_data[0].re[0]._mpfr_d == nullptr)
  174. mpc_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  175. mpc_set_ld(m_data, d, GMP_RNDN);
  176. return *this;
  177. }
  178. mpc_complex_imp& operator=(mpz_t i)
  179. {
  180. if (m_data[0].re[0]._mpfr_d == nullptr)
  181. mpc_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  182. mpc_set_z(m_data, i, GMP_RNDN);
  183. return *this;
  184. }
  185. mpc_complex_imp& operator=(gmp_int i)
  186. {
  187. if (m_data[0].re[0]._mpfr_d == nullptr)
  188. mpc_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  189. mpc_set_z(m_data, i.data(), GMP_RNDN);
  190. return *this;
  191. }
  192. #ifdef BOOST_HAS_INT128
  193. mpc_complex_imp& operator=(int128_type val)
  194. {
  195. gmp_int i;
  196. i = val;
  197. return *this = i.data();
  198. }
  199. mpc_complex_imp& operator=(uint128_type val)
  200. {
  201. gmp_int i;
  202. i = val;
  203. return *this = i.data();
  204. }
  205. #endif
  206. #ifdef BOOST_HAS_FLOAT128
  207. mpc_complex_imp& operator=(float128_type val)
  208. {
  209. mpfr_float_backend<digits10> f;
  210. f = val;
  211. mpc_set_fr(this->m_data, f.data(), GMP_RNDN);
  212. return *this;
  213. }
  214. #endif
  215. mpc_complex_imp& operator=(const char* s)
  216. {
  217. using default_ops::eval_fpclassify;
  218. if (m_data[0].re[0]._mpfr_d == nullptr)
  219. mpc_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  220. mpfr_float_backend<digits10> a(0uL, mpc_get_prec(m_data)), b(0uL, mpc_get_prec(m_data));
  221. if (s && (*s == '('))
  222. {
  223. std::string part;
  224. const char* p = ++s;
  225. while (*p && (*p != ',') && (*p != ')'))
  226. ++p;
  227. part.assign(s, p);
  228. if (part.size())
  229. a = part.c_str();
  230. else
  231. a = 0uL;
  232. s = p;
  233. if (*p && (*p != ')'))
  234. {
  235. ++p;
  236. while (*p && (*p != ')'))
  237. ++p;
  238. part.assign(s + 1, p);
  239. }
  240. else
  241. part.erase();
  242. if (part.size())
  243. b = part.c_str();
  244. else
  245. b = 0uL;
  246. }
  247. else
  248. {
  249. a = s;
  250. b = 0uL;
  251. }
  252. if (eval_fpclassify(a) == static_cast<int>(FP_NAN))
  253. {
  254. mpc_set_fr(this->data(), a.data(), GMP_RNDN);
  255. }
  256. else if (eval_fpclassify(b) == static_cast<int>(FP_NAN))
  257. {
  258. mpc_set_fr(this->data(), b.data(), GMP_RNDN);
  259. }
  260. else
  261. {
  262. mpc_set_fr_fr(m_data, a.data(), b.data(), GMP_RNDN);
  263. }
  264. return *this;
  265. }
  266. void swap(mpc_complex_imp& o) noexcept
  267. {
  268. mpc_swap(m_data, o.m_data);
  269. }
  270. std::string str(std::streamsize digits, std::ios_base::fmtflags f) const
  271. {
  272. BOOST_MP_ASSERT(m_data[0].re[0]._mpfr_d);
  273. mpfr_float_backend<digits10> a(0uL, mpc_get_prec(m_data)), b(0uL, mpc_get_prec(m_data));
  274. mpc_real(a.data(), m_data, GMP_RNDN);
  275. mpc_imag(b.data(), m_data, GMP_RNDN);
  276. if (eval_is_zero(b))
  277. return a.str(digits, f);
  278. return "(" + a.str(digits, f) + "," + b.str(digits, f) + ")";
  279. }
  280. ~mpc_complex_imp() noexcept
  281. {
  282. if (m_data[0].re[0]._mpfr_d)
  283. mpc_clear(m_data);
  284. }
  285. void negate() noexcept
  286. {
  287. BOOST_MP_ASSERT(m_data[0].re[0]._mpfr_d);
  288. mpc_neg(m_data, m_data, GMP_RNDN);
  289. }
  290. int compare(const mpc_complex_imp& o) const noexcept
  291. {
  292. BOOST_MP_ASSERT(m_data[0].re[0]._mpfr_d && o.m_data[0].re[0]._mpfr_d);
  293. return mpc_cmp(m_data, o.m_data);
  294. }
  295. int compare(const mpc_complex_backend<digits10>& o) const noexcept
  296. {
  297. BOOST_MP_ASSERT(m_data[0].re[0]._mpfr_d && o.m_data[0].re[0]._mpfr_d);
  298. return mpc_cmp(m_data, o.data());
  299. }
  300. int compare(long int i) const noexcept
  301. {
  302. BOOST_MP_ASSERT(m_data[0].re[0]._mpfr_d);
  303. return mpc_cmp_si(m_data, i);
  304. }
  305. int compare(unsigned long int i) const noexcept
  306. {
  307. BOOST_MP_ASSERT(m_data[0].re[0]._mpfr_d);
  308. constexpr const unsigned long int max_val = (std::numeric_limits<long>::max)();
  309. if (i > max_val)
  310. {
  311. mpc_complex_imp d(mpc_get_prec(m_data));
  312. d = i;
  313. return compare(d);
  314. }
  315. return mpc_cmp_si(m_data, static_cast<long>(i));
  316. }
  317. template <class V>
  318. int compare(const V& v) const noexcept
  319. {
  320. mpc_complex_imp d(mpc_get_prec(m_data));
  321. d = v;
  322. return compare(d);
  323. }
  324. mpc_t& data() noexcept
  325. {
  326. BOOST_MP_ASSERT(m_data[0].re[0]._mpfr_d);
  327. return m_data;
  328. }
  329. const mpc_t& data() const noexcept
  330. {
  331. BOOST_MP_ASSERT(m_data[0].re[0]._mpfr_d);
  332. return m_data;
  333. }
  334. protected:
  335. mpc_t m_data;
  336. static boost::multiprecision::detail::precision_type& get_global_default_precision() noexcept
  337. {
  338. static boost::multiprecision::detail::precision_type val(BOOST_MULTIPRECISION_MPFI_DEFAULT_PRECISION);
  339. return val;
  340. }
  341. static unsigned& get_default_precision() noexcept
  342. {
  343. static BOOST_MP_THREAD_LOCAL unsigned val(get_global_default_precision());
  344. return val;
  345. }
  346. #ifndef BOOST_MT_NO_ATOMIC_INT
  347. static std::atomic<variable_precision_options>& get_global_default_options() noexcept
  348. #else
  349. static variable_precision_options& get_global_default_options() noexcept
  350. #endif
  351. {
  352. #ifndef BOOST_MT_NO_ATOMIC_INT
  353. static std::atomic<variable_precision_options> val{variable_precision_options::preserve_related_precision};
  354. #else
  355. static variable_precision_options val{variable_precision_options::preserve_related_precision};
  356. #endif
  357. return val;
  358. }
  359. static variable_precision_options& get_default_options() noexcept
  360. {
  361. static BOOST_MP_THREAD_LOCAL variable_precision_options val(get_global_default_options());
  362. return val;
  363. }
  364. static bool preserve_source_precision() noexcept
  365. {
  366. return get_default_options() >= variable_precision_options::preserve_source_precision;
  367. }
  368. static bool preserve_component_precision() noexcept
  369. {
  370. return get_default_options() >= variable_precision_options::preserve_component_precision;
  371. }
  372. static bool preserve_related_precision() noexcept
  373. {
  374. return get_default_options() >= variable_precision_options::preserve_related_precision;
  375. }
  376. static bool preserve_all_precision() noexcept
  377. {
  378. return get_default_options() >= variable_precision_options::preserve_all_precision;
  379. }
  380. };
  381. } // namespace detail
  382. template <unsigned digits10>
  383. struct mpc_complex_backend : public detail::mpc_complex_imp<digits10>
  384. {
  385. mpc_complex_backend() : detail::mpc_complex_imp<digits10>() {}
  386. mpc_complex_backend(const mpc_complex_backend& o) : detail::mpc_complex_imp<digits10>(o) {}
  387. // rvalue copy
  388. mpc_complex_backend(mpc_complex_backend&& o) : detail::mpc_complex_imp<digits10>(static_cast<detail::mpc_complex_imp<digits10>&&>(o))
  389. {}
  390. template <unsigned D>
  391. mpc_complex_backend(const mpc_complex_backend<D>& val, typename std::enable_if<D <= digits10>::type* = nullptr)
  392. : detail::mpc_complex_imp<digits10>()
  393. {
  394. mpc_set(this->m_data, val.data(), GMP_RNDN);
  395. }
  396. template <unsigned D>
  397. explicit mpc_complex_backend(const mpc_complex_backend<D>& val, typename std::enable_if<!(D <= digits10)>::type* = nullptr)
  398. : detail::mpc_complex_imp<digits10>()
  399. {
  400. mpc_set(this->m_data, val.data(), GMP_RNDN);
  401. }
  402. mpc_complex_backend(const mpc_t val)
  403. : detail::mpc_complex_imp<digits10>()
  404. {
  405. mpc_set(this->m_data, val, GMP_RNDN);
  406. }
  407. mpc_complex_backend(const std::complex<float>& val)
  408. : detail::mpc_complex_imp<digits10>()
  409. {
  410. mpc_set_d_d(this->m_data, val.real(), val.imag(), GMP_RNDN);
  411. }
  412. mpc_complex_backend(const std::complex<double>& val)
  413. : detail::mpc_complex_imp<digits10>()
  414. {
  415. mpc_set_d_d(this->m_data, val.real(), val.imag(), GMP_RNDN);
  416. }
  417. mpc_complex_backend(const std::complex<long double>& val)
  418. : detail::mpc_complex_imp<digits10>()
  419. {
  420. mpc_set_ld_ld(this->m_data, val.real(), val.imag(), GMP_RNDN);
  421. }
  422. mpc_complex_backend(mpz_srcptr val) : detail::mpc_complex_imp<digits10>()
  423. {
  424. mpc_set_z(this->m_data, val, GMP_RNDN);
  425. }
  426. mpc_complex_backend& operator=(mpz_srcptr val)
  427. {
  428. mpc_set_z(this->m_data, val, GMP_RNDN);
  429. return *this;
  430. }
  431. mpc_complex_backend(gmp_int const& val) : detail::mpc_complex_imp<digits10>()
  432. {
  433. mpc_set_z(this->m_data, val.data(), GMP_RNDN);
  434. }
  435. mpc_complex_backend& operator=(gmp_int const& val)
  436. {
  437. mpc_set_z(this->m_data, val.data(), GMP_RNDN);
  438. return *this;
  439. }
  440. mpc_complex_backend(mpf_srcptr val) : detail::mpc_complex_imp<digits10>()
  441. {
  442. mpc_set_f(this->m_data, val, GMP_RNDN);
  443. }
  444. mpc_complex_backend& operator=(mpf_srcptr val)
  445. {
  446. mpc_set_f(this->m_data, val, GMP_RNDN);
  447. return *this;
  448. }
  449. template <unsigned D10>
  450. mpc_complex_backend(gmp_float<D10> const& val) : detail::mpc_complex_imp<digits10>()
  451. {
  452. mpc_set_f(this->m_data, val.data(), GMP_RNDN);
  453. }
  454. template <unsigned D10>
  455. mpc_complex_backend& operator=(gmp_float<D10> const& val)
  456. {
  457. mpc_set_f(this->m_data, val.data(), GMP_RNDN);
  458. return *this;
  459. }
  460. mpc_complex_backend(mpq_srcptr val) : detail::mpc_complex_imp<digits10>()
  461. {
  462. mpc_set_q(this->m_data, val, GMP_RNDN);
  463. }
  464. mpc_complex_backend& operator=(mpq_srcptr val)
  465. {
  466. mpc_set_q(this->m_data, val, GMP_RNDN);
  467. return *this;
  468. }
  469. mpc_complex_backend(gmp_rational const& val) : detail::mpc_complex_imp<digits10>()
  470. {
  471. mpc_set_q(this->m_data, val.data(), GMP_RNDN);
  472. }
  473. mpc_complex_backend& operator=(gmp_rational const& val)
  474. {
  475. mpc_set_q(this->m_data, val.data(), GMP_RNDN);
  476. return *this;
  477. }
  478. mpc_complex_backend(mpfr_srcptr val) : detail::mpc_complex_imp<digits10>()
  479. {
  480. mpc_set_fr(this->m_data, val, GMP_RNDN);
  481. }
  482. mpc_complex_backend& operator=(mpfr_srcptr val)
  483. {
  484. mpc_set_fr(this->m_data, val, GMP_RNDN);
  485. return *this;
  486. }
  487. template <unsigned D10, mpfr_allocation_type AllocationType>
  488. mpc_complex_backend(mpfr_float_backend<D10, AllocationType> const& val, typename std::enable_if<D10 <= digits10>::type* = nullptr) : detail::mpc_complex_imp<digits10>()
  489. {
  490. mpc_set_fr(this->m_data, val.data(), GMP_RNDN);
  491. }
  492. template <unsigned D10, mpfr_allocation_type AllocationType>
  493. explicit mpc_complex_backend(mpfr_float_backend<D10, AllocationType> const& val, typename std::enable_if<!(D10 <= digits10)>::type* = nullptr) : detail::mpc_complex_imp<digits10>()
  494. {
  495. mpc_set_fr(this->m_data, val.data(), GMP_RNDN);
  496. }
  497. template <unsigned D10, mpfr_allocation_type AllocationType>
  498. mpc_complex_backend& operator=(mpfr_float_backend<D10, AllocationType> const& val)
  499. {
  500. mpc_set_fr(this->m_data, val.data(), GMP_RNDN);
  501. return *this;
  502. }
  503. mpc_complex_backend& operator=(const mpc_complex_backend& o)
  504. {
  505. *static_cast<detail::mpc_complex_imp<digits10>*>(this) = static_cast<detail::mpc_complex_imp<digits10> const&>(o);
  506. return *this;
  507. }
  508. // rvalue assign
  509. mpc_complex_backend& operator=(mpc_complex_backend&& o) noexcept
  510. {
  511. *static_cast<detail::mpc_complex_imp<digits10>*>(this) = static_cast<detail::mpc_complex_imp<digits10>&&>(o);
  512. return *this;
  513. }
  514. template <class V>
  515. typename std::enable_if<std::is_assignable<detail::mpc_complex_imp<digits10>, V>::value, mpc_complex_backend&>::type operator=(const V& v)
  516. {
  517. *static_cast<detail::mpc_complex_imp<digits10>*>(this) = v;
  518. return *this;
  519. }
  520. mpc_complex_backend& operator=(const mpc_t val)
  521. {
  522. mpc_set(this->m_data, val, GMP_RNDN);
  523. return *this;
  524. }
  525. mpc_complex_backend& operator=(const std::complex<float>& val)
  526. {
  527. mpc_set_d_d(this->m_data, val.real(), val.imag(), GMP_RNDN);
  528. return *this;
  529. }
  530. mpc_complex_backend& operator=(const std::complex<double>& val)
  531. {
  532. mpc_set_d_d(this->m_data, val.real(), val.imag(), GMP_RNDN);
  533. return *this;
  534. }
  535. mpc_complex_backend& operator=(const std::complex<long double>& val)
  536. {
  537. mpc_set_ld_ld(this->m_data, val.real(), val.imag(), GMP_RNDN);
  538. return *this;
  539. }
  540. // We don't change our precision here, this is a fixed precision type:
  541. template <unsigned D>
  542. mpc_complex_backend& operator=(const mpc_complex_backend<D>& val)
  543. {
  544. mpc_set(this->m_data, val.data(), GMP_RNDN);
  545. return *this;
  546. }
  547. };
  548. template <>
  549. struct mpc_complex_backend<0> : public detail::mpc_complex_imp<0>
  550. {
  551. mpc_complex_backend() : detail::mpc_complex_imp<0>() {}
  552. mpc_complex_backend(const mpc_t val)
  553. : detail::mpc_complex_imp<0>(mpc_get_prec(val))
  554. {
  555. mpc_set(this->m_data, val, GMP_RNDN);
  556. }
  557. mpc_complex_backend(const mpc_complex_backend& o) : detail::mpc_complex_imp<0>(o) {}
  558. // rvalue copy
  559. mpc_complex_backend(mpc_complex_backend&& o) noexcept : detail::mpc_complex_imp<0>(static_cast<detail::mpc_complex_imp<0>&&>(o))
  560. {}
  561. mpc_complex_backend(const mpc_complex_backend& o, unsigned digits10)
  562. : detail::mpc_complex_imp<0>(multiprecision::detail::digits10_2_2(digits10))
  563. {
  564. mpc_set(this->m_data, o.data(), GMP_RNDN);
  565. }
  566. template <unsigned D>
  567. mpc_complex_backend(const mpc_complex_backend<D>& val)
  568. : detail::mpc_complex_imp<0>(preserve_related_precision() ? mpc_get_prec(val.data()) : multiprecision::detail::digits10_2_2(get_default_precision()))
  569. {
  570. mpc_set(this->m_data, val.data(), GMP_RNDN);
  571. }
  572. template <unsigned D>
  573. mpc_complex_backend(const mpfr_float_backend<D>& val)
  574. : detail::mpc_complex_imp<0>((D == 0 ? this->preserve_component_precision() : this->preserve_related_precision()) ? mpfr_get_prec(val.data()) : multiprecision::detail::digits10_2_2(this->get_default_precision()))
  575. {
  576. mpc_set_fr(this->m_data, val.data(), GMP_RNDN);
  577. }
  578. mpc_complex_backend(mpz_srcptr val) : detail::mpc_complex_imp<0>()
  579. {
  580. mpc_set_z(this->m_data, val, GMP_RNDN);
  581. }
  582. mpc_complex_backend& operator=(mpz_srcptr val)
  583. {
  584. mpc_set_z(this->m_data, val, GMP_RNDN);
  585. return *this;
  586. }
  587. mpc_complex_backend(gmp_int const& val) : detail::mpc_complex_imp<0>(preserve_all_precision() ? used_gmp_int_bits(val) : boost::multiprecision::detail::digits10_2_2(thread_default_precision()))
  588. {
  589. mpc_set_z(this->m_data, val.data(), GMP_RNDN);
  590. }
  591. mpc_complex_backend& operator=(gmp_int const& val)
  592. {
  593. if (this->m_data[0].im->_mpfr_d == nullptr)
  594. {
  595. unsigned requested_precision = this->thread_default_precision();
  596. if (thread_default_variable_precision_options() >= variable_precision_options::preserve_all_precision)
  597. {
  598. unsigned d2 = used_gmp_int_bits(val);
  599. unsigned d10 = 1 + multiprecision::detail::digits2_2_10(d2);
  600. if (d10 > requested_precision)
  601. requested_precision = d10;
  602. }
  603. mpc_init2(this->m_data, multiprecision::detail::digits10_2_2(requested_precision));
  604. }
  605. else if (thread_default_variable_precision_options() >= variable_precision_options::preserve_all_precision)
  606. {
  607. unsigned requested_precision = this->thread_default_precision();
  608. unsigned d2 = used_gmp_int_bits(val);
  609. unsigned d10 = 1 + multiprecision::detail::digits2_2_10(d2);
  610. if (d10 > requested_precision)
  611. this->precision(d10);
  612. }
  613. mpc_set_z(this->m_data, val.data(), GMP_RNDN);
  614. return *this;
  615. }
  616. mpc_complex_backend(mpf_srcptr val) : detail::mpc_complex_imp<0>((unsigned)mpf_get_prec(val))
  617. {
  618. mpc_set_f(this->m_data, val, GMP_RNDN);
  619. }
  620. mpc_complex_backend& operator=(mpf_srcptr val)
  621. {
  622. if ((mp_bitcnt_t)mpc_get_prec(data()) != mpf_get_prec(val))
  623. {
  624. mpc_complex_backend t(val);
  625. t.swap(*this);
  626. }
  627. else
  628. mpc_set_f(this->m_data, val, GMP_RNDN);
  629. return *this;
  630. }
  631. template <unsigned digits10>
  632. mpc_complex_backend(gmp_float<digits10> const& val) : detail::mpc_complex_imp<0>(preserve_all_precision() ? (unsigned)mpf_get_prec(val.data()) : multiprecision::detail::digits10_2_2(get_default_precision()))
  633. {
  634. mpc_set_f(this->m_data, val.data(), GMP_RNDN);
  635. }
  636. template <unsigned digits10>
  637. mpc_complex_backend& operator=(gmp_float<digits10> const& val)
  638. {
  639. if (preserve_all_precision() && (mpc_get_prec(data()) != (mpfr_prec_t)mpf_get_prec(val.data())))
  640. {
  641. mpc_complex_backend t(val);
  642. t.swap(*this);
  643. }
  644. else
  645. mpc_set_f(this->m_data, val.data(), GMP_RNDN);
  646. return *this;
  647. }
  648. mpc_complex_backend(mpq_srcptr val) : detail::mpc_complex_imp<0>()
  649. {
  650. mpc_set_q(this->m_data, val, GMP_RNDN);
  651. }
  652. mpc_complex_backend& operator=(mpq_srcptr val)
  653. {
  654. mpc_set_q(this->m_data, val, GMP_RNDN);
  655. return *this;
  656. }
  657. mpc_complex_backend(gmp_rational const& val) : detail::mpc_complex_imp<0>(preserve_all_precision() ? used_gmp_rational_bits(val) : boost::multiprecision::detail::digits10_2_2(thread_default_precision()))
  658. {
  659. mpc_set_q(this->m_data, val.data(), GMP_RNDN);
  660. }
  661. mpc_complex_backend& operator=(gmp_rational const& val)
  662. {
  663. if (this->m_data[0].im->_mpfr_d == nullptr)
  664. {
  665. unsigned requested_precision = this->get_default_precision();
  666. if (thread_default_variable_precision_options() >= variable_precision_options::preserve_all_precision)
  667. {
  668. unsigned d10 = 1 + multiprecision::detail::digits2_2_10(used_gmp_rational_bits(val));
  669. if (d10 > requested_precision)
  670. requested_precision = d10;
  671. }
  672. mpc_init2(this->m_data, multiprecision::detail::digits10_2_2(requested_precision));
  673. }
  674. else if (thread_default_variable_precision_options() >= variable_precision_options::preserve_all_precision)
  675. {
  676. unsigned requested_precision = this->get_default_precision();
  677. unsigned d10 = 1 + multiprecision::detail::digits2_2_10(used_gmp_rational_bits(val));
  678. if (d10 > requested_precision)
  679. this->precision(d10);
  680. }
  681. mpc_set_q(this->m_data, val.data(), GMP_RNDN);
  682. return *this;
  683. }
  684. mpc_complex_backend(mpfr_srcptr val) : detail::mpc_complex_imp<0>(mpfr_get_prec(val))
  685. {
  686. mpc_set_fr(this->m_data, val, GMP_RNDN);
  687. }
  688. mpc_complex_backend& operator=(mpfr_srcptr val)
  689. {
  690. if (mpc_get_prec(data()) != mpfr_get_prec(val))
  691. {
  692. mpc_complex_backend t(val);
  693. t.swap(*this);
  694. }
  695. else
  696. mpc_set_fr(this->m_data, val, GMP_RNDN);
  697. return *this;
  698. }
  699. mpc_complex_backend(const std::complex<float>& val)
  700. : detail::mpc_complex_imp<0>()
  701. {
  702. mpc_set_d_d(this->m_data, val.real(), val.imag(), GMP_RNDN);
  703. }
  704. mpc_complex_backend(const std::complex<double>& val)
  705. : detail::mpc_complex_imp<0>()
  706. {
  707. mpc_set_d_d(this->m_data, val.real(), val.imag(), GMP_RNDN);
  708. }
  709. mpc_complex_backend(const std::complex<long double>& val)
  710. : detail::mpc_complex_imp<0>()
  711. {
  712. mpc_set_ld_ld(this->m_data, val.real(), val.imag(), GMP_RNDN);
  713. }
  714. // Construction with precision:
  715. template <class T, class U>
  716. mpc_complex_backend(const T& a, const U& b, unsigned digits10)
  717. : detail::mpc_complex_imp<0>(multiprecision::detail::digits10_2_2(digits10))
  718. {
  719. // We can't use assign_components here because it copies the precision of
  720. // a and b, not digits10....
  721. boost::multiprecision::detail::scoped_precision_options<mpfr_float> scoped(*this);
  722. (void)scoped;
  723. mpfr_float ca(a), cb(b);
  724. mpc_set_fr_fr(this->data(), ca.backend().data(), cb.backend().data(), GMP_RNDN);
  725. }
  726. template <unsigned N>
  727. mpc_complex_backend(const mpfr_float_backend<N>& a, const mpfr_float_backend<N>& b, unsigned digits10)
  728. : detail::mpc_complex_imp<0>(multiprecision::detail::digits10_2_2(digits10))
  729. {
  730. mpc_set_fr_fr(this->data(), a.data(), b.data(), GMP_RNDN);
  731. }
  732. mpc_complex_backend& operator=(const mpc_complex_backend& o) = default;
  733. // rvalue assign
  734. mpc_complex_backend& operator=(mpc_complex_backend&& o) noexcept = default;
  735. template <class V>
  736. mpc_complex_backend& operator=(const V& v)
  737. {
  738. constexpr unsigned d10 = std::is_floating_point<V>::value ?
  739. std::numeric_limits<V>::digits10 :
  740. std::numeric_limits<V>::digits10 ? 1 + std::numeric_limits<V>::digits10 :
  741. 1 + boost::multiprecision::detail::digits2_2_10(std::numeric_limits<V>::digits);
  742. if (thread_default_variable_precision_options() >= variable_precision_options::preserve_all_precision)
  743. {
  744. BOOST_IF_CONSTEXPR(std::is_floating_point<V>::value)
  745. {
  746. if (std::numeric_limits<V>::digits > mpc_get_prec(this->data()))
  747. mpc_set_prec(this->data(), std::numeric_limits<V>::digits);
  748. }
  749. else
  750. {
  751. if (precision() < d10)
  752. this->precision(d10);
  753. }
  754. }
  755. *static_cast<detail::mpc_complex_imp<0>*>(this) = v;
  756. return *this;
  757. }
  758. mpc_complex_backend& operator=(const mpc_t val)
  759. {
  760. mpc_set_prec(this->m_data, mpc_get_prec(val));
  761. mpc_set(this->m_data, val, GMP_RNDN);
  762. return *this;
  763. }
  764. template <unsigned D>
  765. mpc_complex_backend& operator=(const mpc_complex_backend<D>& val)
  766. {
  767. mpc_set_prec(this->m_data, mpc_get_prec(val.data()));
  768. mpc_set(this->m_data, val.data(), GMP_RNDN);
  769. return *this;
  770. }
  771. template <unsigned D>
  772. mpc_complex_backend& operator=(const mpfr_float_backend<D>& val)
  773. {
  774. if (D == 0 ? this->preserve_component_precision() : this->preserve_related_precision())
  775. mpc_set_prec(this->m_data, mpfr_get_prec(val.data()));
  776. mpc_set_fr(this->m_data, val.data(), GMP_RNDN);
  777. return *this;
  778. }
  779. mpc_complex_backend& operator=(const std::complex<float>& val)
  780. {
  781. mpc_set_d_d(this->m_data, val.real(), val.imag(), GMP_RNDN);
  782. return *this;
  783. }
  784. mpc_complex_backend& operator=(const std::complex<double>& val)
  785. {
  786. mpc_set_d_d(this->m_data, val.real(), val.imag(), GMP_RNDN);
  787. return *this;
  788. }
  789. mpc_complex_backend& operator=(const std::complex<long double>& val)
  790. {
  791. mpc_set_ld_ld(this->m_data, val.real(), val.imag(), GMP_RNDN);
  792. return *this;
  793. }
  794. static unsigned default_precision() noexcept
  795. {
  796. return get_global_default_precision();
  797. }
  798. static void default_precision(unsigned v) noexcept
  799. {
  800. get_global_default_precision() = v;
  801. }
  802. static unsigned thread_default_precision() noexcept
  803. {
  804. return get_default_precision();
  805. }
  806. static void thread_default_precision(unsigned v) noexcept
  807. {
  808. get_default_precision() = v;
  809. }
  810. unsigned precision() const noexcept
  811. {
  812. return multiprecision::detail::digits2_2_10(mpc_get_prec(this->m_data));
  813. }
  814. void precision(unsigned digits10) noexcept
  815. {
  816. mpfr_prec_round(mpc_realref(this->m_data), multiprecision::detail::digits10_2_2((digits10)), GMP_RNDN);
  817. mpfr_prec_round(mpc_imagref(this->m_data), multiprecision::detail::digits10_2_2((digits10)), GMP_RNDN);
  818. }
  819. //
  820. // Variable precision options:
  821. //
  822. static variable_precision_options default_variable_precision_options() noexcept
  823. {
  824. return get_global_default_options();
  825. }
  826. static variable_precision_options thread_default_variable_precision_options() noexcept
  827. {
  828. return get_default_options();
  829. }
  830. static void default_variable_precision_options(variable_precision_options opts)
  831. {
  832. get_global_default_options() = opts;
  833. }
  834. static void thread_default_variable_precision_options(variable_precision_options opts)
  835. {
  836. get_default_options() = opts;
  837. }
  838. };
  839. template <unsigned digits10, class T>
  840. inline typename std::enable_if<boost::multiprecision::detail::is_arithmetic<T>::value, bool>::type eval_eq(const mpc_complex_backend<digits10>& a, const T& b) noexcept
  841. {
  842. return a.compare(b) == 0;
  843. }
  844. template <unsigned digits10, class T>
  845. inline typename std::enable_if<boost::multiprecision::detail::is_arithmetic<T>::value, bool>::type eval_lt(const mpc_complex_backend<digits10>& a, const T& b) noexcept
  846. {
  847. return a.compare(b) < 0;
  848. }
  849. template <unsigned digits10, class T>
  850. inline typename std::enable_if<boost::multiprecision::detail::is_arithmetic<T>::value, bool>::type eval_gt(const mpc_complex_backend<digits10>& a, const T& b) noexcept
  851. {
  852. return a.compare(b) > 0;
  853. }
  854. template <unsigned D1, unsigned D2>
  855. inline void eval_add(mpc_complex_backend<D1>& result, const mpc_complex_backend<D2>& o)
  856. {
  857. mpc_add(result.data(), result.data(), o.data(), GMP_RNDN);
  858. }
  859. template <unsigned D1, unsigned D2>
  860. inline void eval_add(mpc_complex_backend<D1>& result, const mpfr_float_backend<D2>& o)
  861. {
  862. mpc_add_fr(result.data(), result.data(), o.data(), GMP_RNDN);
  863. }
  864. template <unsigned D1, unsigned D2>
  865. inline void eval_subtract(mpc_complex_backend<D1>& result, const mpc_complex_backend<D2>& o)
  866. {
  867. mpc_sub(result.data(), result.data(), o.data(), GMP_RNDN);
  868. }
  869. template <unsigned D1, unsigned D2>
  870. inline void eval_subtract(mpc_complex_backend<D1>& result, const mpfr_float_backend<D2>& o)
  871. {
  872. mpc_sub_fr(result.data(), result.data(), o.data(), GMP_RNDN);
  873. }
  874. template <unsigned D1, unsigned D2>
  875. inline void eval_multiply(mpc_complex_backend<D1>& result, const mpc_complex_backend<D2>& o)
  876. {
  877. if ((void*)&result == (void*)&o)
  878. mpc_sqr(result.data(), o.data(), GMP_RNDN);
  879. else
  880. mpc_mul(result.data(), result.data(), o.data(), GMP_RNDN);
  881. }
  882. template <unsigned D1, unsigned D2>
  883. inline void eval_multiply(mpc_complex_backend<D1>& result, const mpfr_float_backend<D2>& o)
  884. {
  885. mpc_mul_fr(result.data(), result.data(), o.data(), GMP_RNDN);
  886. }
  887. template <unsigned D1, unsigned D2>
  888. inline void eval_divide(mpc_complex_backend<D1>& result, const mpc_complex_backend<D2>& o)
  889. {
  890. mpc_div(result.data(), result.data(), o.data(), GMP_RNDN);
  891. }
  892. template <unsigned D1, unsigned D2>
  893. inline void eval_divide(mpc_complex_backend<D1>& result, const mpfr_float_backend<D2>& o)
  894. {
  895. mpc_div_fr(result.data(), result.data(), o.data(), GMP_RNDN);
  896. }
  897. template <unsigned digits10>
  898. inline void eval_add(mpc_complex_backend<digits10>& result, unsigned long i)
  899. {
  900. mpc_add_ui(result.data(), result.data(), i, GMP_RNDN);
  901. }
  902. template <unsigned digits10>
  903. inline void eval_subtract(mpc_complex_backend<digits10>& result, unsigned long i)
  904. {
  905. mpc_sub_ui(result.data(), result.data(), i, GMP_RNDN);
  906. }
  907. template <unsigned digits10>
  908. inline void eval_multiply(mpc_complex_backend<digits10>& result, unsigned long i)
  909. {
  910. mpc_mul_ui(result.data(), result.data(), i, GMP_RNDN);
  911. }
  912. template <unsigned digits10>
  913. inline void eval_divide(mpc_complex_backend<digits10>& result, unsigned long i)
  914. {
  915. mpc_div_ui(result.data(), result.data(), i, GMP_RNDN);
  916. }
  917. template <unsigned digits10>
  918. inline void eval_add(mpc_complex_backend<digits10>& result, long i)
  919. {
  920. if (i > 0)
  921. mpc_add_ui(result.data(), result.data(), i, GMP_RNDN);
  922. else
  923. mpc_sub_ui(result.data(), result.data(), boost::multiprecision::detail::unsigned_abs(i), GMP_RNDN);
  924. }
  925. template <unsigned digits10>
  926. inline void eval_subtract(mpc_complex_backend<digits10>& result, long i)
  927. {
  928. if (i > 0)
  929. mpc_sub_ui(result.data(), result.data(), i, GMP_RNDN);
  930. else
  931. mpc_add_ui(result.data(), result.data(), boost::multiprecision::detail::unsigned_abs(i), GMP_RNDN);
  932. }
  933. template <unsigned digits10>
  934. inline void eval_multiply(mpc_complex_backend<digits10>& result, long i)
  935. {
  936. mpc_mul_ui(result.data(), result.data(), boost::multiprecision::detail::unsigned_abs(i), GMP_RNDN);
  937. if (i < 0)
  938. mpc_neg(result.data(), result.data(), GMP_RNDN);
  939. }
  940. template <unsigned digits10>
  941. inline void eval_divide(mpc_complex_backend<digits10>& result, long i)
  942. {
  943. mpc_div_ui(result.data(), result.data(), boost::multiprecision::detail::unsigned_abs(i), GMP_RNDN);
  944. if (i < 0)
  945. mpc_neg(result.data(), result.data(), GMP_RNDN);
  946. }
  947. //
  948. // Specialised 3 arg versions of the basic operators:
  949. //
  950. template <unsigned D1, unsigned D2, unsigned D3>
  951. inline void eval_add(mpc_complex_backend<D1>& a, const mpc_complex_backend<D2>& x, const mpc_complex_backend<D3>& y)
  952. {
  953. mpc_add(a.data(), x.data(), y.data(), GMP_RNDN);
  954. }
  955. template <unsigned D1, unsigned D2, unsigned D3>
  956. inline void eval_add(mpc_complex_backend<D1>& a, const mpc_complex_backend<D2>& x, const mpfr_float_backend<D3>& y)
  957. {
  958. mpc_add_fr(a.data(), x.data(), y.data(), GMP_RNDN);
  959. }
  960. template <unsigned D1, unsigned D2, unsigned D3>
  961. inline void eval_add(mpc_complex_backend<D1>& a, const mpfr_float_backend<D2>& x, const mpc_complex_backend<D3>& y)
  962. {
  963. mpc_add_fr(a.data(), y.data(), x.data(), GMP_RNDN);
  964. }
  965. template <unsigned D1, unsigned D2>
  966. inline void eval_add(mpc_complex_backend<D1>& a, const mpc_complex_backend<D2>& x, unsigned long y)
  967. {
  968. mpc_add_ui(a.data(), x.data(), y, GMP_RNDN);
  969. }
  970. template <unsigned D1, unsigned D2>
  971. inline void eval_add(mpc_complex_backend<D1>& a, const mpc_complex_backend<D2>& x, long y)
  972. {
  973. if (y < 0)
  974. mpc_sub_ui(a.data(), x.data(), boost::multiprecision::detail::unsigned_abs(y), GMP_RNDN);
  975. else
  976. mpc_add_ui(a.data(), x.data(), y, GMP_RNDN);
  977. }
  978. template <unsigned D1, unsigned D2>
  979. inline void eval_add(mpc_complex_backend<D1>& a, unsigned long x, const mpc_complex_backend<D2>& y)
  980. {
  981. mpc_add_ui(a.data(), y.data(), x, GMP_RNDN);
  982. }
  983. template <unsigned D1, unsigned D2>
  984. inline void eval_add(mpc_complex_backend<D1>& a, long x, const mpc_complex_backend<D2>& y)
  985. {
  986. if (x < 0)
  987. {
  988. mpc_ui_sub(a.data(), boost::multiprecision::detail::unsigned_abs(x), y.data(), GMP_RNDN);
  989. mpc_neg(a.data(), a.data(), GMP_RNDN);
  990. }
  991. else
  992. mpc_add_ui(a.data(), y.data(), x, GMP_RNDN);
  993. }
  994. template <unsigned D1, unsigned D2, unsigned D3>
  995. inline void eval_subtract(mpc_complex_backend<D1>& a, const mpc_complex_backend<D2>& x, const mpc_complex_backend<D3>& y)
  996. {
  997. mpc_sub(a.data(), x.data(), y.data(), GMP_RNDN);
  998. }
  999. template <unsigned D1, unsigned D2, unsigned D3>
  1000. inline void eval_subtract(mpc_complex_backend<D1>& a, const mpc_complex_backend<D2>& x, const mpfr_float_backend<D3>& y)
  1001. {
  1002. mpc_sub_fr(a.data(), x.data(), y.data(), GMP_RNDN);
  1003. }
  1004. template <unsigned D1, unsigned D2, unsigned D3>
  1005. inline void eval_subtract(mpc_complex_backend<D1>& a, const mpfr_float_backend<D2>& x, const mpc_complex_backend<D3>& y)
  1006. {
  1007. mpc_fr_sub(a.data(), x.data(), y.data(), GMP_RNDN);
  1008. }
  1009. template <unsigned D1, unsigned D2>
  1010. inline void eval_subtract(mpc_complex_backend<D1>& a, const mpc_complex_backend<D2>& x, unsigned long y)
  1011. {
  1012. mpc_sub_ui(a.data(), x.data(), y, GMP_RNDN);
  1013. }
  1014. template <unsigned D1, unsigned D2>
  1015. inline void eval_subtract(mpc_complex_backend<D1>& a, const mpc_complex_backend<D2>& x, long y)
  1016. {
  1017. if (y < 0)
  1018. mpc_add_ui(a.data(), x.data(), boost::multiprecision::detail::unsigned_abs(y), GMP_RNDN);
  1019. else
  1020. mpc_sub_ui(a.data(), x.data(), y, GMP_RNDN);
  1021. }
  1022. template <unsigned D1, unsigned D2>
  1023. inline void eval_subtract(mpc_complex_backend<D1>& a, unsigned long x, const mpc_complex_backend<D2>& y)
  1024. {
  1025. mpc_ui_sub(a.data(), x, y.data(), GMP_RNDN);
  1026. }
  1027. template <unsigned D1, unsigned D2>
  1028. inline void eval_subtract(mpc_complex_backend<D1>& a, long x, const mpc_complex_backend<D2>& y)
  1029. {
  1030. if (x < 0)
  1031. {
  1032. mpc_add_ui(a.data(), y.data(), boost::multiprecision::detail::unsigned_abs(x), GMP_RNDN);
  1033. mpc_neg(a.data(), a.data(), GMP_RNDN);
  1034. }
  1035. else
  1036. mpc_ui_sub(a.data(), x, y.data(), GMP_RNDN);
  1037. }
  1038. template <unsigned D1, unsigned D2, unsigned D3>
  1039. inline void eval_multiply(mpc_complex_backend<D1>& a, const mpc_complex_backend<D2>& x, const mpc_complex_backend<D3>& y)
  1040. {
  1041. if ((void*)&x == (void*)&y)
  1042. mpc_sqr(a.data(), x.data(), GMP_RNDN);
  1043. else
  1044. mpc_mul(a.data(), x.data(), y.data(), GMP_RNDN);
  1045. }
  1046. template <unsigned D1, unsigned D2, unsigned D3>
  1047. inline void eval_multiply(mpc_complex_backend<D1>& a, const mpc_complex_backend<D2>& x, const mpfr_float_backend<D3>& y)
  1048. {
  1049. mpc_mul_fr(a.data(), x.data(), y.data(), GMP_RNDN);
  1050. }
  1051. template <unsigned D1, unsigned D2, unsigned D3>
  1052. inline void eval_multiply(mpc_complex_backend<D1>& a, const mpfr_float_backend<D2>& x, const mpc_complex_backend<D3>& y)
  1053. {
  1054. mpc_mul_fr(a.data(), y.data(), x.data(), GMP_RNDN);
  1055. }
  1056. template <unsigned D1, unsigned D2>
  1057. inline void eval_multiply(mpc_complex_backend<D1>& a, const mpc_complex_backend<D2>& x, unsigned long y)
  1058. {
  1059. mpc_mul_ui(a.data(), x.data(), y, GMP_RNDN);
  1060. }
  1061. template <unsigned D1, unsigned D2>
  1062. inline void eval_multiply(mpc_complex_backend<D1>& a, const mpc_complex_backend<D2>& x, long y)
  1063. {
  1064. if (y < 0)
  1065. {
  1066. mpc_mul_ui(a.data(), x.data(), boost::multiprecision::detail::unsigned_abs(y), GMP_RNDN);
  1067. a.negate();
  1068. }
  1069. else
  1070. mpc_mul_ui(a.data(), x.data(), y, GMP_RNDN);
  1071. }
  1072. template <unsigned D1, unsigned D2>
  1073. inline void eval_multiply(mpc_complex_backend<D1>& a, unsigned long x, const mpc_complex_backend<D2>& y)
  1074. {
  1075. mpc_mul_ui(a.data(), y.data(), x, GMP_RNDN);
  1076. }
  1077. template <unsigned D1, unsigned D2>
  1078. inline void eval_multiply(mpc_complex_backend<D1>& a, long x, const mpc_complex_backend<D2>& y)
  1079. {
  1080. if (x < 0)
  1081. {
  1082. mpc_mul_ui(a.data(), y.data(), boost::multiprecision::detail::unsigned_abs(x), GMP_RNDN);
  1083. mpc_neg(a.data(), a.data(), GMP_RNDN);
  1084. }
  1085. else
  1086. mpc_mul_ui(a.data(), y.data(), x, GMP_RNDN);
  1087. }
  1088. template <unsigned D1, unsigned D2, unsigned D3>
  1089. inline void eval_divide(mpc_complex_backend<D1>& a, const mpc_complex_backend<D2>& x, const mpc_complex_backend<D3>& y)
  1090. {
  1091. mpc_div(a.data(), x.data(), y.data(), GMP_RNDN);
  1092. }
  1093. template <unsigned D1, unsigned D2, unsigned D3>
  1094. inline void eval_divide(mpc_complex_backend<D1>& a, const mpc_complex_backend<D2>& x, const mpfr_float_backend<D3>& y)
  1095. {
  1096. mpc_div_fr(a.data(), x.data(), y.data(), GMP_RNDN);
  1097. }
  1098. template <unsigned D1, unsigned D2, unsigned D3>
  1099. inline void eval_divide(mpc_complex_backend<D1>& a, const mpfr_float_backend<D2>& x, const mpc_complex_backend<D3>& y)
  1100. {
  1101. mpc_fr_div(a.data(), x.data(), y.data(), GMP_RNDN);
  1102. }
  1103. template <unsigned D1, unsigned D2>
  1104. inline void eval_divide(mpc_complex_backend<D1>& a, const mpc_complex_backend<D2>& x, unsigned long y)
  1105. {
  1106. mpc_div_ui(a.data(), x.data(), y, GMP_RNDN);
  1107. }
  1108. template <unsigned D1, unsigned D2>
  1109. inline void eval_divide(mpc_complex_backend<D1>& a, const mpc_complex_backend<D2>& x, long y)
  1110. {
  1111. if (y < 0)
  1112. {
  1113. mpc_div_ui(a.data(), x.data(), boost::multiprecision::detail::unsigned_abs(y), GMP_RNDN);
  1114. a.negate();
  1115. }
  1116. else
  1117. mpc_div_ui(a.data(), x.data(), y, GMP_RNDN);
  1118. }
  1119. template <unsigned D1, unsigned D2>
  1120. inline void eval_divide(mpc_complex_backend<D1>& a, unsigned long x, const mpc_complex_backend<D2>& y)
  1121. {
  1122. mpc_ui_div(a.data(), x, y.data(), GMP_RNDN);
  1123. }
  1124. template <unsigned D1, unsigned D2>
  1125. inline void eval_divide(mpc_complex_backend<D1>& a, long x, const mpc_complex_backend<D2>& y)
  1126. {
  1127. if (x < 0)
  1128. {
  1129. mpc_ui_div(a.data(), boost::multiprecision::detail::unsigned_abs(x), y.data(), GMP_RNDN);
  1130. mpc_neg(a.data(), a.data(), GMP_RNDN);
  1131. }
  1132. else
  1133. mpc_ui_div(a.data(), x, y.data(), GMP_RNDN);
  1134. }
  1135. template <unsigned digits10>
  1136. inline bool eval_is_zero(const mpc_complex_backend<digits10>& val) noexcept
  1137. {
  1138. return (0 != mpfr_zero_p(mpc_realref(val.data()))) && (0 != mpfr_zero_p(mpc_imagref(val.data())));
  1139. }
  1140. template <unsigned digits10>
  1141. inline int eval_get_sign(const mpc_complex_backend<digits10>&)
  1142. {
  1143. static_assert(digits10 == UINT_MAX, "Complex numbers have no sign bit."); // designed to always fail
  1144. return 0;
  1145. }
  1146. template <unsigned digits10>
  1147. inline void eval_convert_to(unsigned long* result, const mpc_complex_backend<digits10>& val)
  1148. {
  1149. if (0 == mpfr_zero_p(mpc_imagref(val.data())))
  1150. {
  1151. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Could not convert imaginary number to scalar."));
  1152. }
  1153. mpfr_float_backend<digits10> t;
  1154. mpc_real(t.data(), val.data(), GMP_RNDN);
  1155. eval_convert_to(result, t);
  1156. }
  1157. template <unsigned digits10>
  1158. inline void eval_convert_to(long* result, const mpc_complex_backend<digits10>& val)
  1159. {
  1160. if (0 == mpfr_zero_p(mpc_imagref(val.data())))
  1161. {
  1162. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Could not convert imaginary number to scalar."));
  1163. }
  1164. mpfr_float_backend<digits10> t;
  1165. mpc_real(t.data(), val.data(), GMP_RNDN);
  1166. eval_convert_to(result, t);
  1167. }
  1168. #ifdef _MPFR_H_HAVE_INTMAX_T
  1169. template <unsigned digits10>
  1170. inline void eval_convert_to(unsigned long long* result, const mpc_complex_backend<digits10>& val)
  1171. {
  1172. if (0 == mpfr_zero_p(mpc_imagref(val.data())))
  1173. {
  1174. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Could not convert imaginary number to scalar."));
  1175. }
  1176. mpfr_float_backend<digits10> t;
  1177. mpc_real(t.data(), val.data(), GMP_RNDN);
  1178. eval_convert_to(result, t);
  1179. }
  1180. template <unsigned digits10>
  1181. inline void eval_convert_to(long long* result, const mpc_complex_backend<digits10>& val)
  1182. {
  1183. if (0 == mpfr_zero_p(mpc_imagref(val.data())))
  1184. {
  1185. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Could not convert imaginary number to scalar."));
  1186. }
  1187. mpfr_float_backend<digits10> t;
  1188. mpc_real(t.data(), val.data(), GMP_RNDN);
  1189. eval_convert_to(result, t);
  1190. }
  1191. #endif
  1192. template <unsigned digits10>
  1193. inline void eval_convert_to(double* result, const mpc_complex_backend<digits10>& val) noexcept
  1194. {
  1195. if (0 == mpfr_zero_p(mpc_imagref(val.data())))
  1196. {
  1197. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Could not convert imaginary number to scalar."));
  1198. }
  1199. mpfr_float_backend<digits10> t;
  1200. mpc_real(t.data(), val.data(), GMP_RNDN);
  1201. eval_convert_to(result, t);
  1202. }
  1203. template <unsigned digits10>
  1204. inline void eval_convert_to(long double* result, const mpc_complex_backend<digits10>& val) noexcept
  1205. {
  1206. if (0 == mpfr_zero_p(mpc_imagref(val.data())))
  1207. {
  1208. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Could not convert imaginary number to scalar."));
  1209. }
  1210. mpfr_float_backend<digits10> t;
  1211. mpc_real(t.data(), val.data(), GMP_RNDN);
  1212. eval_convert_to(result, t);
  1213. }
  1214. #ifdef BOOST_HAS_INT128
  1215. template <unsigned digits10>
  1216. inline void eval_convert_to(uint128_type* result, const mpc_complex_backend<digits10>& val)
  1217. {
  1218. using default_ops::eval_convert_to;
  1219. if (0 == mpfr_zero_p(mpc_imagref(val.data())))
  1220. {
  1221. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Could not convert imaginary number to scalar."));
  1222. }
  1223. mpfr_float_backend<digits10> t;
  1224. mpc_real(t.data(), val.data(), GMP_RNDN);
  1225. eval_convert_to(result, t);
  1226. }
  1227. template <unsigned digits10>
  1228. inline void eval_convert_to(int128_type* result, const mpc_complex_backend<digits10>& val)
  1229. {
  1230. using default_ops::eval_convert_to;
  1231. if (0 == mpfr_zero_p(mpc_imagref(val.data())))
  1232. {
  1233. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Could not convert imaginary number to scalar."));
  1234. }
  1235. mpfr_float_backend<digits10> t;
  1236. mpc_real(t.data(), val.data(), GMP_RNDN);
  1237. eval_convert_to(result, t);
  1238. }
  1239. #endif
  1240. #ifdef BOOST_HAS_FLOAT128
  1241. template <unsigned digits10>
  1242. inline void eval_convert_to(float128_type* result, const mpc_complex_backend<digits10>& val)
  1243. {
  1244. using default_ops::eval_convert_to;
  1245. if (0 == mpfr_zero_p(mpc_imagref(val.data())))
  1246. {
  1247. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Could not convert imaginary number to scalar."));
  1248. }
  1249. mpfr_float_backend<digits10> t;
  1250. mpc_real(t.data(), val.data(), GMP_RNDN);
  1251. eval_convert_to(result, t);
  1252. }
  1253. #endif
  1254. template <mpfr_allocation_type AllocationType>
  1255. inline void assign_components_set_precision(mpc_complex_backend<0>& result, const mpfr_float_backend<0, AllocationType>& a, const mpfr_float_backend<0, AllocationType>& b)
  1256. {
  1257. if (result.thread_default_variable_precision_options() >= variable_precision_options::preserve_component_precision)
  1258. {
  1259. unsigned long prec = (std::max)(mpfr_get_prec(a.data()), mpfr_get_prec(b.data()));
  1260. mpc_set_prec(result.data(), prec);
  1261. }
  1262. }
  1263. template <unsigned D2, mpfr_allocation_type AllocationType>
  1264. inline void assign_components_set_precision(mpc_complex_backend<0>& result, const mpfr_float_backend<D2, AllocationType>& a, const mpfr_float_backend<D2, AllocationType>& b)
  1265. {
  1266. if (result.thread_default_variable_precision_options() >= variable_precision_options::preserve_related_precision)
  1267. {
  1268. unsigned long prec = (std::max)(mpfr_get_prec(a.data()), mpfr_get_prec(b.data()));
  1269. mpc_set_prec(result.data(), prec);
  1270. }
  1271. }
  1272. template <unsigned D1, unsigned D2, mpfr_allocation_type AllocationType>
  1273. inline void assign_components_set_precision(mpc_complex_backend<D1>&, const mpfr_float_backend<D2, AllocationType>&, const mpfr_float_backend<D2, AllocationType>&)
  1274. {
  1275. }
  1276. template <unsigned D1, unsigned D2, mpfr_allocation_type AllocationType>
  1277. inline void assign_components(mpc_complex_backend<D1>& result, const mpfr_float_backend<D2, AllocationType>& a, const mpfr_float_backend<D2, AllocationType>& b)
  1278. {
  1279. //
  1280. // This is called from class number's constructors, so if we have variable
  1281. // precision, then copy the precision of the source variables.
  1282. //
  1283. assign_components_set_precision(result, a, b);
  1284. using default_ops::eval_fpclassify;
  1285. if (eval_fpclassify(a) == static_cast<int>(FP_NAN))
  1286. {
  1287. mpc_set_fr(result.data(), a.data(), GMP_RNDN);
  1288. }
  1289. else if (eval_fpclassify(b) == static_cast<int>(FP_NAN))
  1290. {
  1291. mpc_set_fr(result.data(), b.data(), GMP_RNDN);
  1292. }
  1293. else
  1294. {
  1295. mpc_set_fr_fr(result.data(), a.data(), b.data(), GMP_RNDN);
  1296. }
  1297. }
  1298. template <unsigned D1, unsigned D2, mpfr_allocation_type AllocationType>
  1299. inline void assign_components(mpc_complex_backend<D1>& result, unsigned long a, unsigned long b)
  1300. {
  1301. mpc_set_ui_ui(result.data(), a, b, GMP_RNDN);
  1302. }
  1303. template <unsigned D1, unsigned D2, mpfr_allocation_type AllocationType>
  1304. inline void assign_components(mpc_complex_backend<D1>& result, long a, long b)
  1305. {
  1306. mpc_set_si_si(result.data(), a, b, GMP_RNDN);
  1307. }
  1308. #if defined(BOOST_HAS_LONG_LONG) && defined(_MPFR_H_HAVE_INTMAX_T)
  1309. template <unsigned D1, unsigned D2, mpfr_allocation_type AllocationType>
  1310. inline void assign_components(mpc_complex_backend<D1>& result, unsigned long long a, unsigned long long b)
  1311. {
  1312. mpc_set_uj_uj(result.data(), a, b, GMP_RNDN);
  1313. }
  1314. template <unsigned D1, unsigned D2, mpfr_allocation_type AllocationType>
  1315. inline void assign_components(mpc_complex_backend<D1>& result, long long a, long long b)
  1316. {
  1317. mpc_set_sj_sj(result.data(), a, b, GMP_RNDN);
  1318. }
  1319. #endif
  1320. template <unsigned D1, unsigned D2, mpfr_allocation_type AllocationType>
  1321. inline void assign_components(mpc_complex_backend<D1>& result, double a, double b)
  1322. {
  1323. if (BOOST_MP_ISNAN(a))
  1324. {
  1325. mpc_set_d(result.data(), a, GMP_RNDN);
  1326. }
  1327. else if (BOOST_MP_ISNAN(b))
  1328. {
  1329. mpc_set_d(result.data(), b, GMP_RNDN);
  1330. }
  1331. else
  1332. {
  1333. mpc_set_d_d(result.data(), a, b, GMP_RNDN);
  1334. }
  1335. }
  1336. template <unsigned D1, unsigned D2, mpfr_allocation_type AllocationType>
  1337. inline void assign_components(mpc_complex_backend<D1>& result, long double a, long double b)
  1338. {
  1339. if (BOOST_MP_ISNAN(a))
  1340. {
  1341. mpc_set_d(result.data(), a, GMP_RNDN);
  1342. }
  1343. else if (BOOST_MP_ISNAN(b))
  1344. {
  1345. mpc_set_d(result.data(), b, GMP_RNDN);
  1346. }
  1347. else
  1348. {
  1349. mpc_set_ld_ld(result.data(), a, b, GMP_RNDN);
  1350. }
  1351. }
  1352. //
  1353. // Native non-member operations:
  1354. //
  1355. template <unsigned Digits10>
  1356. inline void eval_sqrt(mpc_complex_backend<Digits10>& result, const mpc_complex_backend<Digits10>& val)
  1357. {
  1358. mpc_sqrt(result.data(), val.data(), GMP_RNDN);
  1359. }
  1360. template <unsigned Digits10>
  1361. inline void eval_pow(mpc_complex_backend<Digits10>& result, const mpc_complex_backend<Digits10>& b, const mpc_complex_backend<Digits10>& e)
  1362. {
  1363. mpc_pow(result.data(), b.data(), e.data(), GMP_RNDN);
  1364. }
  1365. template <unsigned Digits10>
  1366. inline void eval_exp(mpc_complex_backend<Digits10>& result, const mpc_complex_backend<Digits10>& arg)
  1367. {
  1368. mpc_exp(result.data(), arg.data(), GMP_RNDN);
  1369. }
  1370. template <unsigned Digits10>
  1371. inline void eval_log(mpc_complex_backend<Digits10>& result, const mpc_complex_backend<Digits10>& arg)
  1372. {
  1373. mpc_log(result.data(), arg.data(), GMP_RNDN);
  1374. }
  1375. template <unsigned Digits10>
  1376. inline void eval_log10(mpc_complex_backend<Digits10>& result, const mpc_complex_backend<Digits10>& arg)
  1377. {
  1378. mpc_log10(result.data(), arg.data(), GMP_RNDN);
  1379. }
  1380. template <unsigned Digits10>
  1381. inline void eval_sin(mpc_complex_backend<Digits10>& result, const mpc_complex_backend<Digits10>& arg)
  1382. {
  1383. mpc_sin(result.data(), arg.data(), GMP_RNDN);
  1384. }
  1385. template <unsigned Digits10>
  1386. inline void eval_cos(mpc_complex_backend<Digits10>& result, const mpc_complex_backend<Digits10>& arg)
  1387. {
  1388. mpc_cos(result.data(), arg.data(), GMP_RNDN);
  1389. }
  1390. template <unsigned Digits10>
  1391. inline void eval_tan(mpc_complex_backend<Digits10>& result, const mpc_complex_backend<Digits10>& arg)
  1392. {
  1393. mpc_tan(result.data(), arg.data(), GMP_RNDN);
  1394. }
  1395. template <unsigned Digits10>
  1396. inline void eval_asin(mpc_complex_backend<Digits10>& result, const mpc_complex_backend<Digits10>& arg)
  1397. {
  1398. mpc_asin(result.data(), arg.data(), GMP_RNDN);
  1399. }
  1400. template <unsigned Digits10>
  1401. inline void eval_acos(mpc_complex_backend<Digits10>& result, const mpc_complex_backend<Digits10>& arg)
  1402. {
  1403. mpc_acos(result.data(), arg.data(), GMP_RNDN);
  1404. }
  1405. template <unsigned Digits10>
  1406. inline void eval_atan(mpc_complex_backend<Digits10>& result, const mpc_complex_backend<Digits10>& arg)
  1407. {
  1408. mpc_atan(result.data(), arg.data(), GMP_RNDN);
  1409. }
  1410. template <unsigned Digits10>
  1411. inline void eval_sinh(mpc_complex_backend<Digits10>& result, const mpc_complex_backend<Digits10>& arg)
  1412. {
  1413. mpc_sinh(result.data(), arg.data(), GMP_RNDN);
  1414. }
  1415. template <unsigned Digits10>
  1416. inline void eval_cosh(mpc_complex_backend<Digits10>& result, const mpc_complex_backend<Digits10>& arg)
  1417. {
  1418. mpc_cosh(result.data(), arg.data(), GMP_RNDN);
  1419. }
  1420. template <unsigned Digits10>
  1421. inline void eval_tanh(mpc_complex_backend<Digits10>& result, const mpc_complex_backend<Digits10>& arg)
  1422. {
  1423. mpc_tanh(result.data(), arg.data(), GMP_RNDN);
  1424. }
  1425. template <unsigned Digits10>
  1426. inline void eval_asinh(mpc_complex_backend<Digits10>& result, const mpc_complex_backend<Digits10>& arg)
  1427. {
  1428. mpc_asinh(result.data(), arg.data(), GMP_RNDN);
  1429. }
  1430. template <unsigned Digits10>
  1431. inline void eval_acosh(mpc_complex_backend<Digits10>& result, const mpc_complex_backend<Digits10>& arg)
  1432. {
  1433. mpc_acosh(result.data(), arg.data(), GMP_RNDN);
  1434. }
  1435. template <unsigned Digits10>
  1436. inline void eval_atanh(mpc_complex_backend<Digits10>& result, const mpc_complex_backend<Digits10>& arg)
  1437. {
  1438. mpc_atanh(result.data(), arg.data(), GMP_RNDN);
  1439. }
  1440. template <unsigned Digits10>
  1441. inline void eval_conj(mpc_complex_backend<Digits10>& result, const mpc_complex_backend<Digits10>& arg)
  1442. {
  1443. mpc_conj(result.data(), arg.data(), GMP_RNDN);
  1444. }
  1445. template <unsigned Digits10>
  1446. inline void eval_proj(mpc_complex_backend<Digits10>& result, const mpc_complex_backend<Digits10>& arg)
  1447. {
  1448. mpc_proj(result.data(), arg.data(), GMP_RNDN);
  1449. }
  1450. template <unsigned Digits10>
  1451. inline void eval_real(mpfr_float_backend<Digits10>& result, const mpc_complex_backend<Digits10>& arg)
  1452. {
  1453. mpfr_set_prec(result.data(), mpfr_get_prec(mpc_realref(arg.data())));
  1454. mpfr_set(result.data(), mpc_realref(arg.data()), GMP_RNDN);
  1455. }
  1456. template <unsigned Digits10>
  1457. inline void eval_imag(mpfr_float_backend<Digits10>& result, const mpc_complex_backend<Digits10>& arg)
  1458. {
  1459. mpfr_set_prec(result.data(), mpfr_get_prec(mpc_imagref(arg.data())));
  1460. mpfr_set(result.data(), mpc_imagref(arg.data()), GMP_RNDN);
  1461. }
  1462. template <unsigned Digits10>
  1463. inline void eval_set_imag(mpc_complex_backend<Digits10>& result, const mpfr_float_backend<Digits10>& arg)
  1464. {
  1465. mpfr_set(mpc_imagref(result.data()), arg.data(), GMP_RNDN);
  1466. }
  1467. template <unsigned Digits10>
  1468. inline void eval_set_real(mpc_complex_backend<Digits10>& result, const mpfr_float_backend<Digits10>& arg)
  1469. {
  1470. mpfr_set(mpc_realref(result.data()), arg.data(), GMP_RNDN);
  1471. }
  1472. template <unsigned Digits10>
  1473. inline void eval_set_real(mpc_complex_backend<Digits10>& result, const gmp_int& arg)
  1474. {
  1475. mpfr_set_z(mpc_realref(result.data()), arg.data(), GMP_RNDN);
  1476. }
  1477. template <unsigned Digits10>
  1478. inline void eval_set_real(mpc_complex_backend<Digits10>& result, const gmp_rational& arg)
  1479. {
  1480. mpfr_set_q(mpc_realref(result.data()), arg.data(), GMP_RNDN);
  1481. }
  1482. template <unsigned Digits10>
  1483. inline void eval_set_real(mpc_complex_backend<Digits10>& result, const unsigned& arg)
  1484. {
  1485. mpfr_set_ui(mpc_realref(result.data()), arg, GMP_RNDN);
  1486. }
  1487. template <unsigned Digits10>
  1488. inline void eval_set_real(mpc_complex_backend<Digits10>& result, const unsigned long& arg)
  1489. {
  1490. mpfr_set_ui(mpc_realref(result.data()), arg, GMP_RNDN);
  1491. }
  1492. template <unsigned Digits10>
  1493. inline void eval_set_real(mpc_complex_backend<Digits10>& result, const int& arg)
  1494. {
  1495. mpfr_set_si(mpc_realref(result.data()), arg, GMP_RNDN);
  1496. }
  1497. template <unsigned Digits10>
  1498. inline void eval_set_real(mpc_complex_backend<Digits10>& result, const long& arg)
  1499. {
  1500. mpfr_set_si(mpc_realref(result.data()), arg, GMP_RNDN);
  1501. }
  1502. template <unsigned Digits10>
  1503. inline void eval_set_real(mpc_complex_backend<Digits10>& result, const float& arg)
  1504. {
  1505. mpfr_set_flt(mpc_realref(result.data()), arg, GMP_RNDN);
  1506. }
  1507. template <unsigned Digits10>
  1508. inline void eval_set_real(mpc_complex_backend<Digits10>& result, const double& arg)
  1509. {
  1510. mpfr_set_d(mpc_realref(result.data()), arg, GMP_RNDN);
  1511. }
  1512. template <unsigned Digits10>
  1513. inline void eval_set_real(mpc_complex_backend<Digits10>& result, const long double& arg)
  1514. {
  1515. mpfr_set_ld(mpc_realref(result.data()), arg, GMP_RNDN);
  1516. }
  1517. #if defined(BOOST_HAS_LONG_LONG) && defined(_MPFR_H_HAVE_INTMAX_T)
  1518. template <unsigned Digits10>
  1519. inline void eval_set_real(mpc_complex_backend<Digits10>& result, const unsigned long long& arg)
  1520. {
  1521. mpfr_set_uj(mpc_realref(result.data()), arg, GMP_RNDN);
  1522. }
  1523. template <unsigned Digits10>
  1524. inline void eval_set_real(mpc_complex_backend<Digits10>& result, const long long& arg)
  1525. {
  1526. mpfr_set_sj(mpc_realref(result.data()), arg, GMP_RNDN);
  1527. }
  1528. #endif
  1529. template <unsigned Digits10>
  1530. inline void eval_set_imag(mpc_complex_backend<Digits10>& result, const gmp_int& arg)
  1531. {
  1532. mpfr_set_z(mpc_imagref(result.data()), arg.data(), GMP_RNDN);
  1533. }
  1534. template <unsigned Digits10>
  1535. inline void eval_set_imag(mpc_complex_backend<Digits10>& result, const gmp_rational& arg)
  1536. {
  1537. mpfr_set_q(mpc_imagref(result.data()), arg.data(), GMP_RNDN);
  1538. }
  1539. template <unsigned Digits10>
  1540. inline void eval_set_imag(mpc_complex_backend<Digits10>& result, const unsigned& arg)
  1541. {
  1542. mpfr_set_ui(mpc_imagref(result.data()), arg, GMP_RNDN);
  1543. }
  1544. template <unsigned Digits10>
  1545. inline void eval_set_imag(mpc_complex_backend<Digits10>& result, const unsigned long& arg)
  1546. {
  1547. mpfr_set_ui(mpc_imagref(result.data()), arg, GMP_RNDN);
  1548. }
  1549. template <unsigned Digits10>
  1550. inline void eval_set_imag(mpc_complex_backend<Digits10>& result, const int& arg)
  1551. {
  1552. mpfr_set_si(mpc_imagref(result.data()), arg, GMP_RNDN);
  1553. }
  1554. template <unsigned Digits10>
  1555. inline void eval_set_imag(mpc_complex_backend<Digits10>& result, const long& arg)
  1556. {
  1557. mpfr_set_si(mpc_imagref(result.data()), arg, GMP_RNDN);
  1558. }
  1559. template <unsigned Digits10>
  1560. inline void eval_set_imag(mpc_complex_backend<Digits10>& result, const float& arg)
  1561. {
  1562. mpfr_set_flt(mpc_imagref(result.data()), arg, GMP_RNDN);
  1563. }
  1564. template <unsigned Digits10>
  1565. inline void eval_set_imag(mpc_complex_backend<Digits10>& result, const double& arg)
  1566. {
  1567. mpfr_set_d(mpc_imagref(result.data()), arg, GMP_RNDN);
  1568. }
  1569. template <unsigned Digits10>
  1570. inline void eval_set_imag(mpc_complex_backend<Digits10>& result, const long double& arg)
  1571. {
  1572. mpfr_set_ld(mpc_imagref(result.data()), arg, GMP_RNDN);
  1573. }
  1574. #if defined(BOOST_HAS_LONG_LONG) && defined(_MPFR_H_HAVE_INTMAX_T)
  1575. template <unsigned Digits10>
  1576. inline void eval_set_imag(mpc_complex_backend<Digits10>& result, const unsigned long long& arg)
  1577. {
  1578. mpfr_set_uj(mpc_imagref(result.data()), arg, GMP_RNDN);
  1579. }
  1580. template <unsigned Digits10>
  1581. inline void eval_set_imag(mpc_complex_backend<Digits10>& result, const long long& arg)
  1582. {
  1583. mpfr_set_sj(mpc_imagref(result.data()), arg, GMP_RNDN);
  1584. }
  1585. #endif
  1586. template <unsigned Digits10>
  1587. inline std::size_t hash_value(const mpc_complex_backend<Digits10>& val)
  1588. {
  1589. std::size_t result = 0;
  1590. std::size_t len = val.data()[0].re[0]._mpfr_prec / mp_bits_per_limb;
  1591. if (val.data()[0].re[0]._mpfr_prec % mp_bits_per_limb)
  1592. ++len;
  1593. for (std::size_t i = 0; i < len; ++i)
  1594. boost::multiprecision::detail::hash_combine(result, val.data()[0].re[0]._mpfr_d[i]);
  1595. boost::multiprecision::detail::hash_combine(result, val.data()[0].re[0]._mpfr_exp, val.data()[0].re[0]._mpfr_sign);
  1596. len = val.data()[0].im[0]._mpfr_prec / mp_bits_per_limb;
  1597. if (val.data()[0].im[0]._mpfr_prec % mp_bits_per_limb)
  1598. ++len;
  1599. for (std::size_t i = 0; i < len; ++i)
  1600. boost::multiprecision::detail::hash_combine(result, val.data()[0].im[0]._mpfr_d[i]);
  1601. boost::multiprecision::detail::hash_combine(result, val.data()[0].im[0]._mpfr_exp, val.data()[0].im[0]._mpfr_sign);
  1602. return result;
  1603. }
  1604. } // namespace backends
  1605. namespace detail {
  1606. template <>
  1607. struct is_variable_precision<backends::mpc_complex_backend<0> > : public std::integral_constant<bool, true>
  1608. {};
  1609. } // namespace detail
  1610. template <>
  1611. struct number_category<detail::canonical<mpc_t, backends::mpc_complex_backend<0> >::type> : public std::integral_constant<int, number_kind_floating_point>
  1612. {};
  1613. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  1614. struct component_type<number<mpc_complex_backend<Digits10>, ExpressionTemplates> >
  1615. {
  1616. using type = number<mpfr_float_backend<Digits10>, ExpressionTemplates>;
  1617. };
  1618. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  1619. struct component_type<number<backends::logged_adaptor<mpc_complex_backend<Digits10> >, ExpressionTemplates> >
  1620. {
  1621. using type = number<mpfr_float_backend<Digits10>, ExpressionTemplates>;
  1622. };
  1623. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  1624. struct component_type<number<backends::debug_adaptor<mpc_complex_backend<Digits10> >, ExpressionTemplates> >
  1625. {
  1626. using type = number<backends::debug_adaptor<mpfr_float_backend<Digits10> >, ExpressionTemplates>;
  1627. };
  1628. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  1629. struct complex_result_from_scalar<number<mpfr_float_backend<Digits10>, ExpressionTemplates> >
  1630. {
  1631. using type = number<mpc_complex_backend<Digits10>, ExpressionTemplates>;
  1632. };
  1633. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  1634. struct complex_result_from_scalar<number<backends::logged_adaptor<mpfr_float_backend<Digits10>>, ExpressionTemplates> >
  1635. {
  1636. using type = number<mpc_complex_backend<Digits10>, ExpressionTemplates>;
  1637. };
  1638. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  1639. struct complex_result_from_scalar<number<backends::debug_adaptor<mpfr_float_backend<Digits10>>, ExpressionTemplates> >
  1640. {
  1641. using type = number<backends::debug_adaptor<mpc_complex_backend<Digits10> >, ExpressionTemplates>;
  1642. };
  1643. }
  1644. } // namespace boost::multiprecision
  1645. #endif