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
6 /*`This example demonstrates the usage of the MPC backend for multiprecision complex numbers.
7 In the following, we will show how using MPC backend allows for the same operations as the C++ standard library complex numbers.
8 */
9
10 //[complex128_eg
11 #include <iostream>
12 #include <complex>
13 #include <boost/multiprecision/complex128.hpp>
14
15 template<class Complex>
complex_number_examples()16 void complex_number_examples()
17 {
18 Complex z1{0, 1};
19 std::cout << std::setprecision(std::numeric_limits<typename Complex::value_type>::digits10);
20 std::cout << std::scientific << std::fixed;
21 std::cout << "Print a complex number: " << z1 << std::endl;
22 std::cout << "Square it : " << z1*z1 << std::endl;
23 std::cout << "Real part : " << z1.real() << " = " << real(z1) << std::endl;
24 std::cout << "Imaginary part : " << z1.imag() << " = " << imag(z1) << std::endl;
25 using std::abs;
26 std::cout << "Absolute value : " << abs(z1) << std::endl;
27 std::cout << "Argument : " << arg(z1) << std::endl;
28 std::cout << "Norm : " << norm(z1) << std::endl;
29 std::cout << "Complex conjugate : " << conj(z1) << std::endl;
30 std::cout << "Projection onto Riemann sphere: " << proj(z1) << std::endl;
31 typename Complex::value_type r = 1;
32 typename Complex::value_type theta = 0.8;
33 using std::polar;
34 std::cout << "Polar coordinates (phase = 0) : " << polar(r) << std::endl;
35 std::cout << "Polar coordinates (phase !=0) : " << polar(r, theta) << std::endl;
36
37 std::cout << "\nElementary special functions:\n";
38 using std::exp;
39 std::cout << "exp(z1) = " << exp(z1) << std::endl;
40 using std::log;
41 std::cout << "log(z1) = " << log(z1) << std::endl;
42 using std::log10;
43 std::cout << "log10(z1) = " << log10(z1) << std::endl;
44 using std::pow;
45 std::cout << "pow(z1, z1) = " << pow(z1, z1) << std::endl;
46 using std::sqrt;
47 std::cout << "Take its square root : " << sqrt(z1) << std::endl;
48 using std::sin;
49 std::cout << "sin(z1) = " << sin(z1) << std::endl;
50 using std::cos;
51 std::cout << "cos(z1) = " << cos(z1) << std::endl;
52 using std::tan;
53 std::cout << "tan(z1) = " << tan(z1) << std::endl;
54 using std::asin;
55 std::cout << "asin(z1) = " << asin(z1) << std::endl;
56 using std::acos;
57 std::cout << "acos(z1) = " << acos(z1) << std::endl;
58 using std::atan;
59 std::cout << "atan(z1) = " << atan(z1) << std::endl;
60 using std::sinh;
61 std::cout << "sinh(z1) = " << sinh(z1) << std::endl;
62 using std::cosh;
63 std::cout << "cosh(z1) = " << cosh(z1) << std::endl;
64 using std::tanh;
65 std::cout << "tanh(z1) = " << tanh(z1) << std::endl;
66 using std::asinh;
67 std::cout << "asinh(z1) = " << asinh(z1) << std::endl;
68 using std::acosh;
69 std::cout << "acosh(z1) = " << acosh(z1) << std::endl;
70 using std::atanh;
71 std::cout << "atanh(z1) = " << atanh(z1) << std::endl;
72 }
73
main()74 int main()
75 {
76 std::cout << "First, some operations we usually perform with std::complex:\n";
77 complex_number_examples<std::complex<double>>();
78 std::cout << "\nNow the same operations performed using quad precision complex numbers:\n";
79 complex_number_examples<boost::multiprecision::complex128>();
80
81 return 0;
82 }
83 //]
84
85 /*
86
87 //[complex128_out
88
89 Print a complex number: (0.000000000000000000000000000000000,1.000000000000000000000000000000000)
90 Square it : -1.000000000000000000000000000000000
91 Real part : 0.000000000000000000000000000000000 = 0.000000000000000000000000000000000
92 Imaginary part : 1.000000000000000000000000000000000 = 1.000000000000000000000000000000000
93 Absolute value : 1.000000000000000000000000000000000
94 Argument : 1.570796326794896619231321691639751
95 Norm : 1.000000000000000000000000000000000
96 Complex conjugate : (0.000000000000000000000000000000000,-1.000000000000000000000000000000000)
97 Projection onto Riemann sphere: (0.000000000000000000000000000000000,1.000000000000000000000000000000000)
98 Polar coordinates (phase = 0) : 1.000000000000000000000000000000000
99 Polar coordinates (phase !=0) : (0.696706709347165389063740022772449,0.717356090899522792567167815703377)
100
101 Elementary special functions:
102 exp(z1) = (0.540302305868139717400936607442977,0.841470984807896506652502321630299)
103 log(z1) = (0.000000000000000000000000000000000,1.570796326794896619231321691639751)
104 log10(z1) = (0.000000000000000000000000000000000,0.682188176920920673742891812715678)
105 pow(z1, z1) = 0.207879576350761908546955619834979
106 Take its square root : (0.707106781186547524400844362104849,0.707106781186547524400844362104849)
107 sin(z1) = (0.000000000000000000000000000000000,1.175201193643801456882381850595601)
108 cos(z1) = 1.543080634815243778477905620757061
109 tan(z1) = (0.000000000000000000000000000000000,0.761594155955764888119458282604794)
110 asin(z1) = (0.000000000000000000000000000000000,0.881373587019543025232609324979792)
111 acos(z1) = (1.570796326794896619231321691639751,-0.881373587019543025232609324979792)
112 atan(z1) = (0.000000000000000000000000000000000,inf)
113 sinh(z1) = (0.000000000000000000000000000000000,0.841470984807896506652502321630299)
114 cosh(z1) = 0.540302305868139717400936607442977
115 tanh(z1) = (0.000000000000000000000000000000000,1.557407724654902230506974807458360)
116 asinh(z1) = (0.000000000000000000000000000000000,1.570796326794896619231321691639751)
117 acosh(z1) = (0.881373587019543025232609324979792,1.570796326794896619231321691639751)
118 atanh(z1) = (0.000000000000000000000000000000000,0.785398163397448309615660845819876)
119 //]
120 */
121