cmplx.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /* cmplx.c
  2. *
  3. * Complex number arithmetic
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * typedef struct {
  10. * double r; real part
  11. * double i; imaginary part
  12. * }cmplx;
  13. *
  14. * cmplx *a, *b, *c;
  15. *
  16. * cadd( a, b, c ); c = b + a
  17. * csub( a, b, c ); c = b - a
  18. * cmul( a, b, c ); c = b * a
  19. * cdiv( a, b, c ); c = b / a
  20. * cneg( c ); c = -c
  21. * cmov( b, c ); c = b
  22. *
  23. *
  24. *
  25. * DESCRIPTION:
  26. *
  27. * Addition:
  28. * c.r = b.r + a.r
  29. * c.i = b.i + a.i
  30. *
  31. * Subtraction:
  32. * c.r = b.r - a.r
  33. * c.i = b.i - a.i
  34. *
  35. * Multiplication:
  36. * c.r = b.r * a.r - b.i * a.i
  37. * c.i = b.r * a.i + b.i * a.r
  38. *
  39. * Division:
  40. * d = a.r * a.r + a.i * a.i
  41. * c.r = (b.r * a.r + b.i * a.i)/d
  42. * c.i = (b.i * a.r - b.r * a.i)/d
  43. * ACCURACY:
  44. *
  45. * In DEC arithmetic, the test (1/z) * z = 1 had peak relative
  46. * error 3.1e-17, rms 1.2e-17. The test (y/z) * (z/y) = 1 had
  47. * peak relative error 8.3e-17, rms 2.1e-17.
  48. *
  49. * Tests in the rectangle {-10,+10}:
  50. * Relative error:
  51. * arithmetic function # trials peak rms
  52. * DEC cadd 10000 1.4e-17 3.4e-18
  53. * IEEE cadd 100000 1.1e-16 2.7e-17
  54. * DEC csub 10000 1.4e-17 4.5e-18
  55. * IEEE csub 100000 1.1e-16 3.4e-17
  56. * DEC cmul 3000 2.3e-17 8.7e-18
  57. * IEEE cmul 100000 2.1e-16 6.9e-17
  58. * DEC cdiv 18000 4.9e-17 1.3e-17
  59. * IEEE cdiv 100000 3.7e-16 1.1e-16
  60. */
  61. /* cmplx.c
  62. * complex number arithmetic
  63. */
  64. /*
  65. Cephes Math Library Release 2.8: June, 2000
  66. Copyright 1984, 1995, 2000 by Stephen L. Moshier
  67. */
  68. #include <math.h>
  69. #ifdef ANSIPROT
  70. extern double fabs ( double );
  71. extern double cabs ( cmplx * );
  72. extern double sqrt ( double );
  73. extern double atan2 ( double, double );
  74. extern double cos ( double );
  75. extern double sin ( double );
  76. extern double sqrt ( double );
  77. extern double frexp ( double, int * );
  78. extern double ldexp ( double, int );
  79. int isnan ( double );
  80. void cdiv ( cmplx *, cmplx *, cmplx * );
  81. void cadd ( cmplx *, cmplx *, cmplx * );
  82. #else
  83. double fabs(), cabs(), sqrt(), atan2(), cos(), sin();
  84. double sqrt(), frexp(), ldexp();
  85. int isnan();
  86. void cdiv(), cadd();
  87. #endif
  88. extern double MAXNUM, MACHEP, PI, PIO2, INFINITY, NAN;
  89. /*
  90. typedef struct
  91. {
  92. double r;
  93. double i;
  94. }cmplx;
  95. */
  96. cmplx czero = {0.0, 0.0};
  97. extern cmplx czero;
  98. cmplx cone = {1.0, 0.0};
  99. extern cmplx cone;
  100. /* c = b + a */
  101. void cadd( a, b, c )
  102. register cmplx *a, *b;
  103. cmplx *c;
  104. {
  105. c->r = b->r + a->r;
  106. c->i = b->i + a->i;
  107. }
  108. /* c = b - a */
  109. void csub( a, b, c )
  110. register cmplx *a, *b;
  111. cmplx *c;
  112. {
  113. c->r = b->r - a->r;
  114. c->i = b->i - a->i;
  115. }
  116. /* c = b * a */
  117. void cmul( a, b, c )
  118. register cmplx *a, *b;
  119. cmplx *c;
  120. {
  121. double y;
  122. y = b->r * a->r - b->i * a->i;
  123. c->i = b->r * a->i + b->i * a->r;
  124. c->r = y;
  125. }
  126. /* c = b / a */
  127. void cdiv( a, b, c )
  128. register cmplx *a, *b;
  129. cmplx *c;
  130. {
  131. double y, p, q, w;
  132. y = a->r * a->r + a->i * a->i;
  133. p = b->r * a->r + b->i * a->i;
  134. q = b->i * a->r - b->r * a->i;
  135. if( y < 1.0 )
  136. {
  137. w = MAXNUM * y;
  138. if( (fabs(p) > w) || (fabs(q) > w) || (y == 0.0) )
  139. {
  140. c->r = MAXNUM;
  141. c->i = MAXNUM;
  142. mtherr( "cdiv", OVERFLOW );
  143. return;
  144. }
  145. }
  146. c->r = p/y;
  147. c->i = q/y;
  148. }
  149. /* b = a
  150. Caution, a `short' is assumed to be 16 bits wide. */
  151. void cmov( a, b )
  152. void *a, *b;
  153. {
  154. register short *pa, *pb;
  155. int i;
  156. pa = (short *) a;
  157. pb = (short *) b;
  158. i = 8;
  159. do
  160. *pb++ = *pa++;
  161. while( --i );
  162. }
  163. void cneg( a )
  164. register cmplx *a;
  165. {
  166. a->r = -a->r;
  167. a->i = -a->i;
  168. }
  169. /* cabs()
  170. *
  171. * Complex absolute value
  172. *
  173. *
  174. *
  175. * SYNOPSIS:
  176. *
  177. * double cabs();
  178. * cmplx z;
  179. * double a;
  180. *
  181. * a = cabs( &z );
  182. *
  183. *
  184. *
  185. * DESCRIPTION:
  186. *
  187. *
  188. * If z = x + iy
  189. *
  190. * then
  191. *
  192. * a = sqrt( x**2 + y**2 ).
  193. *
  194. * Overflow and underflow are avoided by testing the magnitudes
  195. * of x and y before squaring. If either is outside half of
  196. * the floating point full scale range, both are rescaled.
  197. *
  198. *
  199. * ACCURACY:
  200. *
  201. * Relative error:
  202. * arithmetic domain # trials peak rms
  203. * DEC -30,+30 30000 3.2e-17 9.2e-18
  204. * IEEE -10,+10 100000 2.7e-16 6.9e-17
  205. */
  206. /*
  207. Cephes Math Library Release 2.1: January, 1989
  208. Copyright 1984, 1987, 1989 by Stephen L. Moshier
  209. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  210. */
  211. /*
  212. typedef struct
  213. {
  214. double r;
  215. double i;
  216. }cmplx;
  217. */
  218. #ifdef UNK
  219. #define PREC 27
  220. #define MAXEXP 1024
  221. #define MINEXP -1077
  222. #endif
  223. #ifdef DEC
  224. #define PREC 29
  225. #define MAXEXP 128
  226. #define MINEXP -128
  227. #endif
  228. #ifdef IBMPC
  229. #define PREC 27
  230. #define MAXEXP 1024
  231. #define MINEXP -1077
  232. #endif
  233. #ifdef MIEEE
  234. #define PREC 27
  235. #define MAXEXP 1024
  236. #define MINEXP -1077
  237. #endif
  238. double cabs( z )
  239. register cmplx *z;
  240. {
  241. double x, y, b, re, im;
  242. int ex, ey, e;
  243. #ifdef INFINITIES
  244. /* Note, cabs(INFINITY,NAN) = INFINITY. */
  245. if( z->r == INFINITY || z->i == INFINITY
  246. || z->r == -INFINITY || z->i == -INFINITY )
  247. return( INFINITY );
  248. #endif
  249. #ifdef NANS
  250. if( isnan(z->r) )
  251. return(z->r);
  252. if( isnan(z->i) )
  253. return(z->i);
  254. #endif
  255. re = fabs( z->r );
  256. im = fabs( z->i );
  257. if( re == 0.0 )
  258. return( im );
  259. if( im == 0.0 )
  260. return( re );
  261. /* Get the exponents of the numbers */
  262. x = frexp( re, &ex );
  263. y = frexp( im, &ey );
  264. /* Check if one number is tiny compared to the other */
  265. e = ex - ey;
  266. if( e > PREC )
  267. return( re );
  268. if( e < -PREC )
  269. return( im );
  270. /* Find approximate exponent e of the geometric mean. */
  271. e = (ex + ey) >> 1;
  272. /* Rescale so mean is about 1 */
  273. x = ldexp( re, -e );
  274. y = ldexp( im, -e );
  275. /* Hypotenuse of the right triangle */
  276. b = sqrt( x * x + y * y );
  277. /* Compute the exponent of the answer. */
  278. y = frexp( b, &ey );
  279. ey = e + ey;
  280. /* Check it for overflow and underflow. */
  281. if( ey > MAXEXP )
  282. {
  283. mtherr( "cabs", OVERFLOW );
  284. return( INFINITY );
  285. }
  286. if( ey < MINEXP )
  287. return(0.0);
  288. /* Undo the scaling */
  289. b = ldexp( b, e );
  290. return( b );
  291. }
  292. /* csqrt()
  293. *
  294. * Complex square root
  295. *
  296. *
  297. *
  298. * SYNOPSIS:
  299. *
  300. * void csqrt();
  301. * cmplx z, w;
  302. *
  303. * csqrt( &z, &w );
  304. *
  305. *
  306. *
  307. * DESCRIPTION:
  308. *
  309. *
  310. * If z = x + iy, r = |z|, then
  311. *
  312. * 1/2
  313. * Im w = [ (r - x)/2 ] ,
  314. *
  315. * Re w = y / 2 Im w.
  316. *
  317. *
  318. * Note that -w is also a square root of z. The root chosen
  319. * is always in the upper half plane.
  320. *
  321. * Because of the potential for cancellation error in r - x,
  322. * the result is sharpened by doing a Heron iteration
  323. * (see sqrt.c) in complex arithmetic.
  324. *
  325. *
  326. *
  327. * ACCURACY:
  328. *
  329. * Relative error:
  330. * arithmetic domain # trials peak rms
  331. * DEC -10,+10 25000 3.2e-17 9.6e-18
  332. * IEEE -10,+10 100000 3.2e-16 7.7e-17
  333. *
  334. * 2
  335. * Also tested by csqrt( z ) = z, and tested by arguments
  336. * close to the real axis.
  337. */
  338. void csqrt( z, w )
  339. cmplx *z, *w;
  340. {
  341. cmplx q, s;
  342. double x, y, r, t;
  343. x = z->r;
  344. y = z->i;
  345. if( y == 0.0 )
  346. {
  347. if( x < 0.0 )
  348. {
  349. w->r = 0.0;
  350. w->i = sqrt(-x);
  351. return;
  352. }
  353. else
  354. {
  355. w->r = sqrt(x);
  356. w->i = 0.0;
  357. return;
  358. }
  359. }
  360. if( x == 0.0 )
  361. {
  362. r = fabs(y);
  363. r = sqrt(0.5*r);
  364. if( y > 0 )
  365. w->r = r;
  366. else
  367. w->r = -r;
  368. w->i = r;
  369. return;
  370. }
  371. /* Approximate sqrt(x^2+y^2) - x = y^2/2x - y^4/24x^3 + ... .
  372. * The relative error in the first term is approximately y^2/12x^2 .
  373. */
  374. if( (fabs(y) < 2.e-4 * fabs(x))
  375. && (x > 0) )
  376. {
  377. t = 0.25*y*(y/x);
  378. }
  379. else
  380. {
  381. r = cabs(z);
  382. t = 0.5*(r - x);
  383. }
  384. r = sqrt(t);
  385. q.i = r;
  386. q.r = y/(2.0*r);
  387. /* Heron iteration in complex arithmetic */
  388. cdiv( &q, z, &s );
  389. cadd( &q, &s, w );
  390. w->r *= 0.5;
  391. w->i *= 0.5;
  392. }
  393. double hypot( x, y )
  394. double x, y;
  395. {
  396. cmplx z;
  397. z.r = x;
  398. z.i = y;
  399. return( cabs(&z) );
  400. }