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