ucomplex.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. //**********************************************************************************//
  2. // Copyright (C) 2009-2015 Ovidio Pena <ovidio@bytesfall.com> //
  3. // //
  4. // This file is part of scattnlay //
  5. // //
  6. // This program is free software: you can redistribute it and/or modify //
  7. // it under the terms of the GNU General Public License as published by //
  8. // the Free Software Foundation, either version 3 of the License, or //
  9. // (at your option) any later version. //
  10. // //
  11. // This program is distributed in the hope that it will be useful, //
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of //
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
  14. // GNU General Public License for more details. //
  15. // //
  16. // The only additional remark is that we expect that all publications //
  17. // describing work using this software, or all commercial products //
  18. // using it, cite the following reference: //
  19. // [1] O. Pena and U. Pal, "Scattering of electromagnetic radiation by //
  20. // a multilayered sphere," Computer Physics Communications, //
  21. // vol. 180, Nov. 2009, pp. 2348-2354. //
  22. // //
  23. // You should have received a copy of the GNU General Public License //
  24. // along with this program. If not, see <http://www.gnu.org/licenses/>. //
  25. //**********************************************************************************//
  26. #include <math.h>
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include "ucomplex.h"
  30. complex Cadd(complex z1, complex z2) {
  31. complex zr;
  32. zr.r = z1.r + z2.r;
  33. zr.i = z1.i + z2.i;
  34. return zr;
  35. }
  36. complex Csub(complex z1, complex z2) {
  37. complex zr;
  38. zr.r = z1.r - z2.r;
  39. zr.i = z1.i - z2.i;
  40. return zr;
  41. }
  42. complex Cmul(complex z1, complex z2) {
  43. complex zr;
  44. zr.r = (z1.r*z2.r) - (z1.i*z2.i);
  45. zr.i = (z1.r*z2.i) + (z1.i*z2.r);
  46. return zr;
  47. }
  48. complex RCmul(double r, complex z) {
  49. complex zr;
  50. zr.r = z.r*r;
  51. zr.i = z.i*r;
  52. return zr;
  53. }
  54. complex Cdiv(complex z1, complex z2) {
  55. /* The following algorithm is used to properly handle
  56. denominator overflow:
  57. | a + b(d/c) b - a(d/c)
  58. | ---------- + ----------- I if |d| < |c|
  59. a + b I | c + d(d/c) c + d(d/c)
  60. ------- = |
  61. c + d I | b + a(c/d) -a + b(c/d)
  62. | ---------- + ----------- I if |d| >= |c|
  63. | d + c(c/d) d + c(c/d)
  64. */
  65. complex zr;
  66. double tmp, denom;
  67. if(fabs(z2.r) > fabs(z2.i)) {
  68. tmp = z2.i/z2.r;
  69. denom = z2.r + z2.i*tmp;
  70. zr.r = (z1.r + z1.i*tmp)/denom;
  71. zr.i = (z1.i - z1.r*tmp)/denom;
  72. } else {
  73. tmp = z2.r/z2.i;
  74. denom = z2.i + z2.r*tmp;
  75. zr.r = (z1.i + z1.r*tmp)/denom;
  76. zr.i = (-z1.r + z1.i*tmp)/denom;
  77. }
  78. return zr;
  79. }
  80. complex Complex(double re, double im) {
  81. complex zr;
  82. zr.r = re;
  83. zr.i = im;
  84. return zr;
  85. }
  86. double Cabs(complex z) {
  87. double r;
  88. r = sqrt((z.r*z.r) + (z.i*z.i));
  89. return r;
  90. }
  91. double Carc(complex z) {
  92. double r;
  93. r = atan2(z.i, z.r);
  94. return r;
  95. }
  96. complex Conjg(complex z) {
  97. complex zr;
  98. zr.r = z.r;
  99. zr.i = -z.i;
  100. return zr;
  101. }
  102. complex Cinv(complex z) {
  103. complex zr;
  104. double denom;
  105. denom = (z.r*z.r) + (z.i*z.i);
  106. zr.r = z.r/denom;
  107. zr.i = -z.i/denom;
  108. return zr;
  109. }
  110. complex Cexp(complex z) {
  111. // exp(a + ib) = exp(a)*exp(ib) = exp(a)*[cos(b) + i sin(b)]
  112. complex zr;
  113. double expz;
  114. expz = exp(z.r);
  115. zr.r = expz*cos(z.i);
  116. zr.i = expz*sin(z.i);
  117. return zr;
  118. }
  119. complex Clog(complex z) {
  120. // ln( p exp(i0)) = ln(p) + i0 + 2kpi
  121. complex zr;
  122. zr.r = log(Cabs(z));
  123. zr.i = atan2(z.i, z.r);
  124. return zr;
  125. }
  126. complex Csqrt(complex z) {
  127. complex zr;
  128. double root, q;
  129. if((z.r != 0.0) ||(z.i != 0.0)) {
  130. root = sqrt(0.5*(fabs(z.r) + Cabs(z)));
  131. q = z.i/(2.0*root);
  132. if(z.r >= 0.0) {
  133. zr.r = root;
  134. zr.i = q;
  135. } else if(z.i < 0.0) {
  136. zr.r = -q;
  137. zr.i = -root;
  138. } else {
  139. zr.r = q;
  140. zr.i = root;
  141. }
  142. } else zr = z;
  143. return zr;
  144. }
  145. // complex trigonometric functions
  146. // complex cosinus
  147. complex Ccos(complex z) {
  148. // cos(x+iy) = cos(x).cos(iy) - sin(x).sin(iy)
  149. // cos(ix) = cosh(x) et sin(ix) = i.sinh(x)
  150. complex zr;
  151. zr.r = cos(z.r)*cosh(z.i);
  152. zr.i = -sin(z.r)*sinh(z.i);
  153. return zr;
  154. }
  155. // complex sinus
  156. complex Csin(complex z) {
  157. // sin(x+iy) = sin(x).cos(iy) + cos(x).sin(iy)
  158. // cos(ix) = cosh(x) et sin(ix) = i.sinh(x)
  159. complex zr;
  160. zr.r = sin(z.r)*cosh(z.i);
  161. zr.i = cos(z.r)*sinh(z.i);
  162. return zr;
  163. }
  164. // tangent
  165. complex Ctan(complex z) {
  166. return Cdiv(Csin(z), Ccos(z));
  167. }
  168. // Inverse trigonometric functions
  169. // complex arc cosinus
  170. complex Carc_cos(complex z) {
  171. // arccos(z) = -i.arcch(z)
  172. complex temp = Carc_ch(z);
  173. return Complex(temp.i, -temp.r);
  174. }
  175. // complex arc sinus
  176. complex Carc_sin(complex z) {
  177. // arcsin(z) = -i.arcsh(i.z)
  178. complex temp = Carc_sh(Complex(-z.i, z.r));
  179. return Complex(temp.i, -temp.r);
  180. }
  181. // complex arc tangent
  182. complex Carc_tan(complex z) {
  183. // arctg(z) = -i.arcth(i.z)
  184. complex temp = Carc_th(Complex(-z.i, z.r));
  185. return Complex(temp.i, -temp.r);
  186. }
  187. // Hyberbolic complex functions
  188. // complex hyberbolic cosinus
  189. complex Cch(complex z) {
  190. // cosh(x+iy) = cosh(x).cosh(iy) + sinh(x).sinh(iy)
  191. // cosh(iy) = cos(y) et sinh(iy) = i.sin(y)
  192. complex zr;
  193. zr.r = cosh(z.r)*cos(z.i);
  194. zr.i = sinh(z.r)*sin(z.i);
  195. return zr;
  196. }
  197. // complex hyberbolic sinus
  198. complex Csh(complex z) {
  199. // sinh(x+iy) = sinh(x).cosh(iy) + cosh(x).sinh(iy)
  200. // cosh(iy) = cos(y) et sinh(iy) = i.sin(y)
  201. complex zr;
  202. zr.r = sinh(z.r)*cos(z.i);
  203. zr.i = cosh(z.r)*sin(z.i);
  204. return zr;
  205. }
  206. // complex hyberbolic tangent
  207. complex Cth(complex z) {
  208. // th(x) = sinh(x)/cosh(x)
  209. // cosh(x) > 1 qq x
  210. complex temp, zr;
  211. temp = Cch(z);
  212. zr = Csh(z);
  213. return Cdiv(zr, temp);
  214. }
  215. // Inverse complex hyperbolic functions
  216. // complex hyperbolic arc cosinus
  217. complex Carc_ch(complex z) {
  218. // _________
  219. // arcch(z) = -/+ ln(z + V z.z - 1 )
  220. complex zr;
  221. complex z2 = Cmul(z, z);
  222. zr = Clog(Cadd(z, Csqrt(Complex(z2.r - 1.0, z2.i))));
  223. zr.r = -zr.r;
  224. zr.i = -zr.i;
  225. return zr;
  226. }
  227. // complex hyperbolic arc sinus
  228. complex Carc_sh(complex z) {
  229. // ________
  230. // arcsh(z) = ln(z + V 1 + z.z)
  231. complex z2 = Cmul(z, z);
  232. return Clog(Cadd(z, Csqrt(Complex(z2.r + 1.0, z2.i))));
  233. }
  234. // complex hyperbolic arc tangent
  235. complex Carc_th(complex z) {
  236. // arcth(z) = 1/2 ln((z + 1)/(1 - z))
  237. return RCmul(0.5, Clog(Cdiv(Complex(z.r + 1.0, z.i), Complex(1.0 - z.r, -z.i))));
  238. }