mpc_examples.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. ///////////////////////////////////////////////////////////////
  2. // Copyright 2018 Nick Thompson. Distributed under the Boost
  3. // Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
  5. /*`This example demonstrates the usage of the MPC backend for multiprecision complex numbers.
  6. In the following, we will show how using MPC backend allows for the same operations as the C++ standard library complex numbers.
  7. */
  8. //[mpc_eg
  9. #include <iostream>
  10. #include <complex>
  11. #include <boost/multiprecision/mpc.hpp>
  12. template<class Complex>
  13. void complex_number_examples()
  14. {
  15. Complex z1{0, 1};
  16. std::cout << std::setprecision(std::numeric_limits<typename Complex::value_type>::digits10);
  17. std::cout << std::scientific << std::fixed;
  18. std::cout << "Print a complex number: " << z1 << std::endl;
  19. std::cout << "Square it : " << z1*z1 << std::endl;
  20. std::cout << "Real part : " << z1.real() << " = " << real(z1) << std::endl;
  21. std::cout << "Imaginary part : " << z1.imag() << " = " << imag(z1) << std::endl;
  22. using std::abs;
  23. std::cout << "Absolute value : " << abs(z1) << std::endl;
  24. std::cout << "Argument : " << arg(z1) << std::endl;
  25. std::cout << "Norm : " << norm(z1) << std::endl;
  26. std::cout << "Complex conjugate : " << conj(z1) << std::endl;
  27. std::cout << "Projection onto Riemann sphere: " << proj(z1) << std::endl;
  28. typename Complex::value_type r = 1;
  29. typename Complex::value_type theta = 0.8;
  30. using std::polar;
  31. std::cout << "Polar coordinates (phase = 0) : " << polar(r) << std::endl;
  32. std::cout << "Polar coordinates (phase !=0) : " << polar(r, theta) << std::endl;
  33. std::cout << "\nElementary special functions:\n";
  34. using std::exp;
  35. std::cout << "exp(z1) = " << exp(z1) << std::endl;
  36. using std::log;
  37. std::cout << "log(z1) = " << log(z1) << std::endl;
  38. using std::log10;
  39. std::cout << "log10(z1) = " << log10(z1) << std::endl;
  40. using std::pow;
  41. std::cout << "pow(z1, z1) = " << pow(z1, z1) << std::endl;
  42. using std::sqrt;
  43. std::cout << "Take its square root : " << sqrt(z1) << std::endl;
  44. using std::sin;
  45. std::cout << "sin(z1) = " << sin(z1) << std::endl;
  46. using std::cos;
  47. std::cout << "cos(z1) = " << cos(z1) << std::endl;
  48. using std::tan;
  49. std::cout << "tan(z1) = " << tan(z1) << std::endl;
  50. using std::asin;
  51. std::cout << "asin(z1) = " << asin(z1) << std::endl;
  52. using std::acos;
  53. std::cout << "acos(z1) = " << acos(z1) << std::endl;
  54. using std::atan;
  55. std::cout << "atan(z1) = " << atan(z1) << std::endl;
  56. using std::sinh;
  57. std::cout << "sinh(z1) = " << sinh(z1) << std::endl;
  58. using std::cosh;
  59. std::cout << "cosh(z1) = " << cosh(z1) << std::endl;
  60. using std::tanh;
  61. std::cout << "tanh(z1) = " << tanh(z1) << std::endl;
  62. using std::asinh;
  63. std::cout << "asinh(z1) = " << asinh(z1) << std::endl;
  64. using std::acosh;
  65. std::cout << "acosh(z1) = " << acosh(z1) << std::endl;
  66. using std::atanh;
  67. std::cout << "atanh(z1) = " << atanh(z1) << std::endl;
  68. }
  69. int main()
  70. {
  71. std::cout << "First, some operations we usually perform with std::complex:\n";
  72. complex_number_examples<std::complex<double>>();
  73. std::cout << "\nNow the same operations performed using the MPC backend:\n";
  74. complex_number_examples<boost::multiprecision::mpc_complex_50>();
  75. return 0;
  76. }
  77. //]
  78. /*
  79. //[mpc_out
  80. Print a complex number: (0.00000000000000000000000000000000000000000000000000,1.00000000000000000000000000000000000000000000000000)
  81. Square it : -1.00000000000000000000000000000000000000000000000000
  82. Real part : 0.00000000000000000000000000000000000000000000000000 = 0.00000000000000000000000000000000000000000000000000
  83. Imaginary part : 1.00000000000000000000000000000000000000000000000000 = 1.00000000000000000000000000000000000000000000000000
  84. Absolute value : 1.00000000000000000000000000000000000000000000000000
  85. Argument : 1.57079632679489661923132169163975144209858469968755
  86. Norm : 1.00000000000000000000000000000000000000000000000000
  87. Complex conjugate : (0.00000000000000000000000000000000000000000000000000,-1.00000000000000000000000000000000000000000000000000)
  88. Projection onto Riemann sphere: (0.00000000000000000000000000000000000000000000000000,1.00000000000000000000000000000000000000000000000000)
  89. Polar coordinates (phase = 0) : 1.00000000000000000000000000000000000000000000000000
  90. Polar coordinates (phase !=0) : (0.69670670934716538906374002277244853473117519431538,0.71735609089952279256716781570337728075604730751255)
  91. Elementary special functions:
  92. exp(z1) = (0.54030230586813971740093660744297660373231042061792,0.84147098480789650665250232163029899962256306079837)
  93. log(z1) = (0.00000000000000000000000000000000000000000000000000,1.57079632679489661923132169163975144209858469968755)
  94. log10(z1) = (0.00000000000000000000000000000000000000000000000000,0.68218817692092067374289181271567788510506374186196)
  95. pow(z1, z1) = 0.20787957635076190854695561983497877003387784163177
  96. Take its square root : (0.70710678118654752440084436210484903928483593768847,0.70710678118654752440084436210484903928483593768847)
  97. sin(z1) = (0.00000000000000000000000000000000000000000000000000,1.17520119364380145688238185059560081515571798133410)
  98. cos(z1) = 1.54308063481524377847790562075706168260152911236587
  99. tan(z1) = (0.00000000000000000000000000000000000000000000000000,0.76159415595576488811945828260479359041276859725794)
  100. asin(z1) = (0.00000000000000000000000000000000000000000000000000,0.88137358701954302523260932497979230902816032826163)
  101. acos(z1) = (1.57079632679489661923132169163975144209858469968755,-0.88137358701954302523260932497979230902816032826163)
  102. atan(z1) = (0.00000000000000000000000000000000000000000000000000,inf)
  103. sinh(z1) = (0.00000000000000000000000000000000000000000000000000,0.84147098480789650665250232163029899962256306079837)
  104. cosh(z1) = 0.54030230586813971740093660744297660373231042061792
  105. tanh(z1) = (0.00000000000000000000000000000000000000000000000000,1.55740772465490223050697480745836017308725077238152)
  106. asinh(z1) = (0.00000000000000000000000000000000000000000000000000,1.57079632679489661923132169163975144209858469968755)
  107. acosh(z1) = (0.88137358701954302523260932497979230902816032826163,1.57079632679489661923132169163975144209858469968755)
  108. atanh(z1) = (0.00000000000000000000000000000000000000000000000000,0.78539816339744830961566084581987572104929234984378)
  109. //]
  110. */