test-complex-lib.cc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #include <complex>
  2. #include <string>
  3. #include <stdio.h>
  4. #include "ucomplex.h"
  5. void output(std::string operation, complex c1, std::complex<double> c2) {
  6. double diff_r = c1.r - c2.real();
  7. double diff_i = c1.i - c2.imag();
  8. printf("%s: \tdiff=(%g,%g), \tc1 = (%g,%g), \tc2 = (%g,%g)\n",
  9. operation.c_str(),
  10. diff_r, diff_i,
  11. c1.r, c1.i,
  12. c2.real(), c2.imag());
  13. // Epsilon for double is 1e-16, check it 1.0 + 1e-16 = 1.0
  14. if (diff_r > 1e-16||diff_i > 1e-16)
  15. //if (diff_r > 1e-15||diff_i > 1e-15)
  16. printf("\n********\n\t\tWARNING!! Non-zero diff!!!\n********\n");
  17. }
  18. /********************************************************************/
  19. /********************************************************************/
  20. /********************************************************************/
  21. void output(std::string operation, complex c1, complex c2) {
  22. double diff_r = c1.r - c2.r;
  23. double diff_i = c1.i - c2.i;
  24. printf("%s: \tdiff=(%g,%g), \tc1 = (%g,%g), \tc2 = (%g,%g)\n",
  25. operation.c_str(),
  26. diff_r, diff_i,
  27. c1.r, c1.i,
  28. c2.r, c2.i);
  29. // Epsilon for double is 1e-16, check it 1.0 + 1e-16 = 1.0
  30. if (diff_r > 1e-16||diff_i > 1e-16)
  31. //if (diff_r > 1e-15||diff_i > 1e-15)
  32. printf("\n********\n\t\tWARNING!! Non-zero diff!!!\n********\n");
  33. }
  34. /********************************************************************/
  35. /********************************************************************/
  36. /********************************************************************/
  37. void output(std::string operation, std::complex<double> c1, std::complex<double> c2) {
  38. double diff_r = c1.real() - c2.real();
  39. double diff_i = c1.imag() - c2.imag();
  40. printf("%s: \tdiff=(%g,%g), \tc1 = (%g,%g), \tc2 = (%g,%g)\n",
  41. operation.c_str(),
  42. diff_r, diff_i,
  43. c1.real(), c1.imag(),
  44. c2.real(), c2.imag());
  45. // Epsilon for double is 1e-16, check it 1.0 + 1e-16 = 1.0
  46. if (diff_r > 1e-16||diff_i > 1e-16)
  47. //if (diff_r > 1e-15||diff_i > 1e-15)
  48. printf("\n********\n\t\tWARNING!! Non-zero diff!!!\n********\n");
  49. }
  50. /********************************************************************/
  51. /********************************************************************/
  52. /********************************************************************/
  53. void output_double(std::string operation, double c1, double c2) {
  54. double diff = c1 - c2;
  55. printf("%s: \tdiff=(%g), \tc1 = (%g), \tc2 = (%g)\n",
  56. operation.c_str(),
  57. diff, c1, c2);
  58. // Epsilon for double is 1e-16, check it 1.0 + 1e-16 = 1.0
  59. if (diff > 1e-16)
  60. printf("\n********\n\t\tWARNING!! Non-zero diff!!!\n********\n");
  61. }
  62. /********************************************************************/
  63. /********************************************************************/
  64. /********************************************************************/
  65. int main() {
  66. double r = 1.412039487560983471903;
  67. complex a1 = {1.0012001301318988478, -1.0123131231241412987237},
  68. b1 = {0.42312341412412398237, 1.32312314124141232987987}, c1;
  69. std::complex<double> a2(a1.r, a1.i), b2(b1.r, b1.i), c2;
  70. printf("Re(a1) = %g, Im(a1) = %g\n", a1.r, a1.i);
  71. printf("Re(a2) = %g, Im(a2) = %g\n", a2.real(), a2.imag());
  72. printf("Re(b1) = %g, Im(b1) = %g\n", b1.r, b1.i);
  73. printf("Re(b2) = %g, Im(b2) = %g\n", b2.real(), b2.imag());
  74. printf("r = %g\n", r);
  75. c1 = Cadd(a1, b1);
  76. c2 = a2 + b2;
  77. output("Add", c1, c2);
  78. c1 = Cadd(b1, a1);
  79. c2 = b2 + a2;
  80. output("sAdd", c1, c2);
  81. c1 = Csub(a1, b1);
  82. c2 = a2 - b2;
  83. output("Sub", c1, c2);
  84. c1 = Csub(b1, a1);
  85. c2 = b2 - a2;
  86. output("sSub", c1, c2);
  87. c1 = Cmul(a1, b1);
  88. c2 = a2 * b2;
  89. output("Mul", c1, c2);
  90. c1 = Cmul(b1, a1);
  91. c2 = b2 * a2;
  92. output("sMul", c1, c2);
  93. c1 = RCmul(r, b1);
  94. c2 = r * b2;
  95. output("RCMul", c1, c2);
  96. c1 = Cdiv(a1, b1);
  97. c2 = a2 / b2;
  98. output("Div", c1, c2);
  99. c1 = Cdiv(b1, a1);
  100. c2 = b2 / a2;
  101. output("sDiv", c1, c2);
  102. c1 = Conjg(a1);
  103. c2 = std::conj(a2);
  104. output("Conj", c1, c2);
  105. c1 = Conjg(b1);
  106. c2 = std::conj(b2);
  107. output("sConj", c1, c2);
  108. c1 = Cinv(a1);
  109. c2 = 1.0/a2;
  110. output("Inv", c1, c2);
  111. c1 = Cinv(b1);
  112. c2 = 1.0/b2;
  113. output("sInv", c1, c2);
  114. output_double("Abs", Cabs(a1), std::abs(a2));
  115. output_double("sAbs", Cabs(b1), std::abs(b2));
  116. output_double("Arg", Carc(a1), std::arg(a2));
  117. output_double("sArg", Carc(b1), std::arg(b2));
  118. c1 = Cexp(a1);
  119. c2 = std::exp(a2);
  120. output("Exp", c1, c2);
  121. c1 = Cexp(b1);
  122. c2 = std::exp(b2);
  123. output("sExp", c1, c2);
  124. c1 = Clog(a1);
  125. c2 = std::log(a2);
  126. output("Log", c1, c2);
  127. c1 = Clog(b1);
  128. c2 = std::log(b2);
  129. output("sLog", c1, c2);
  130. c1 = Csqrt(a1);
  131. c2 = std::sqrt(a2);
  132. output("Sqrt", c1, c2);
  133. c1 = Csqrt(b1);
  134. c2 = std::sqrt(b2);
  135. output("sSqrt", c1, c2);
  136. c1 = Ccos(a1);
  137. c2 = std::cos(a2);
  138. output("Cos", c1, c2);
  139. c1 = Ccos(b1);
  140. c2 = std::cos(b2);
  141. output("sCos", c1, c2);
  142. c1 = Csin(a1);
  143. c2 = std::sin(a2);
  144. output("Sin", c1, c2);
  145. c1 = Csin(b1);
  146. c2 = std::sin(b2);
  147. output("sSin", c1, c2);
  148. c1 = Ctan(a1);
  149. c2 = std::tan(a2);
  150. output("Tan", c1, c2);
  151. c1 = Ctan(b1);
  152. c2 = std::tan(b2);
  153. output("sTan", c1, c2);
  154. c1 = Carc_cos(a1);
  155. c2 = std::acos(a2);
  156. output("aCos", c1, c2);
  157. c1 = Carc_cos(b1);
  158. c2 = std::acos(b2);
  159. output("saCos", c1, c2);
  160. c1 = Ccos(Carc_cos(b1));
  161. output("saCos_cos_b1", c1, b1);
  162. c2 = std::cos(std::acos(b2));
  163. output("saCos_cos_b2", c2, b2);
  164. c1 = Carc_sin(a1);
  165. c2 = std::asin(a2);
  166. output("aSin", c1, c2);
  167. c1 = Carc_sin(b1);
  168. c2 = std::asin(b2);
  169. output("saSin", c1, c2);
  170. c1 = Carc_tan(a1);
  171. c2 = std::atan(a2);
  172. output("aTan", c1, c2);
  173. c1 = Carc_tan(b1);
  174. c2 = std::atan(b2);
  175. output("saTan", c1, c2);
  176. // Hyperbolic
  177. c1 = Cch(a1);
  178. c2 = std::cosh(a2);
  179. output("Cosh", c1, c2);
  180. c1 = Cch(b1);
  181. c2 = std::cosh(b2);
  182. output("sCosh", c1, c2);
  183. c1 = Csh(a1);
  184. c2 = std::sinh(a2);
  185. output("Sinh", c1, c2);
  186. c1 = Csh(b1);
  187. c2 = std::sinh(b2);
  188. output("sSinh", c1, c2);
  189. c1 = Cth(a1);
  190. c2 = std::tanh(a2);
  191. output("Tanh", c1, c2);
  192. c1 = Cth(b1);
  193. c2 = std::tanh(b2);
  194. output("sTanh", c1, c2);
  195. c1 = Carc_ch(a1);
  196. c2 = std::acosh(a2);
  197. output("aCosh", c1, c2);
  198. c1 = Carc_ch(b1);
  199. c2 = std::acosh(b2);
  200. output("saCosh", c1, c2);
  201. c1 = Cch(Carc_ch(b1));
  202. output("saCosh_cosh_b1", c1, b1);
  203. c2 = std::cosh(std::acosh(b2));
  204. output("saCosh_cosh_b2", c2, b2);
  205. c1 = Carc_sh(a1);
  206. c2 = std::asinh(a2);
  207. output("aSinh", c1, c2);
  208. c1 = Carc_sh(b1);
  209. c2 = std::asinh(b2);
  210. output("saSinh", c1, c2);
  211. c1 = Carc_th(a1);
  212. c2 = std::atanh(a2);
  213. output("aTanh", c1, c2);
  214. c1 = Carc_th(b1);
  215. c2 = std::atanh(b2);
  216. output("saTanh", c1, c2);
  217. return 0;
  218. }