zfunc.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /**************************************************************************
  2. **
  3. ** Copyright (C) 1993 David E. Steward & Zbigniew Leyk, all rights reserved.
  4. **
  5. ** Meschach Library
  6. **
  7. ** This Meschach Library is provided "as is" without any express
  8. ** or implied warranty of any kind with respect to this software.
  9. ** In particular the authors shall not be liable for any direct,
  10. ** indirect, special, incidental or consequential damages arising
  11. ** in any way from use of the software.
  12. **
  13. ** Everyone is granted permission to copy, modify and redistribute this
  14. ** Meschach Library, provided:
  15. ** 1. All copies contain this copyright notice.
  16. ** 2. All modified copies shall carry a notice stating who
  17. ** made the last modification and the date of such modification.
  18. ** 3. No charge is made for this software or works derived from it.
  19. ** This clause shall not be construed as constraining other software
  20. ** distributed on the same medium as this software, nor is a
  21. ** distribution fee considered a charge.
  22. **
  23. ***************************************************************************/
  24. /*
  25. Elementary functions for complex numbers
  26. -- if not already defined
  27. */
  28. #include <math.h>
  29. #include "zmatrix.h"
  30. static char rcsid[] = "$Id: zfunc.c,v 1.3 1995/04/07 16:27:25 des Exp $";
  31. #ifndef COMPLEX_H
  32. #ifndef zmake
  33. /* zmake -- create complex number real + i*imag */
  34. complex zmake(real,imag)
  35. double real, imag;
  36. {
  37. complex w; /* == real + i*imag */
  38. w.re = real; w.im = imag;
  39. return w;
  40. }
  41. #endif
  42. #ifndef zneg
  43. /* zneg -- returns negative of z */
  44. complex zneg(z)
  45. complex z;
  46. {
  47. z.re = - z.re;
  48. z.im = - z.im;
  49. return z;
  50. }
  51. #endif
  52. #ifndef zabs
  53. /* zabs -- returns |z| */
  54. double zabs(z)
  55. complex z;
  56. {
  57. Real x, y, tmp;
  58. int x_expt, y_expt;
  59. /* Note: we must ensure that overflow does not occur! */
  60. x = ( z.re >= 0.0 ) ? z.re : -z.re;
  61. y = ( z.im >= 0.0 ) ? z.im : -z.im;
  62. if ( x < y )
  63. {
  64. tmp = x;
  65. x = y;
  66. y = tmp;
  67. }
  68. if ( x == 0.0 ) /* then y == 0.0 as well */
  69. return 0.0;
  70. x = frexp(x,&x_expt);
  71. y = frexp(y,&y_expt);
  72. y = ldexp(y,y_expt-x_expt);
  73. tmp = sqrt(x*x+y*y);
  74. return ldexp(tmp,x_expt);
  75. }
  76. #endif
  77. #ifndef zadd
  78. /* zadd -- returns z1+z2 */
  79. complex zadd(z1,z2)
  80. complex z1, z2;
  81. {
  82. complex z;
  83. z.re = z1.re + z2.re;
  84. z.im = z1.im + z2.im;
  85. return z;
  86. }
  87. #endif
  88. #ifndef zsub
  89. /* zsub -- returns z1-z2 */
  90. complex zsub(z1,z2)
  91. complex z1, z2;
  92. {
  93. complex z;
  94. z.re = z1.re - z2.re;
  95. z.im = z1.im - z2.im;
  96. return z;
  97. }
  98. #endif
  99. #ifndef zmlt
  100. /* zmlt -- returns z1*z2 */
  101. complex zmlt(z1,z2)
  102. complex z1, z2;
  103. {
  104. complex z;
  105. z.re = z1.re * z2.re - z1.im * z2.im;
  106. z.im = z1.re * z2.im + z1.im * z2.re;
  107. return z;
  108. }
  109. #endif
  110. #ifndef zinv
  111. /* zmlt -- returns 1/z */
  112. complex zinv(z)
  113. complex z;
  114. {
  115. Real x, y, tmp;
  116. int x_expt, y_expt;
  117. if ( z.re == 0.0 && z.im == 0.0 )
  118. error(E_SING,"zinv");
  119. /* Note: we must ensure that overflow does not occur! */
  120. x = ( z.re >= 0.0 ) ? z.re : -z.re;
  121. y = ( z.im >= 0.0 ) ? z.im : -z.im;
  122. if ( x < y )
  123. {
  124. tmp = x;
  125. x = y;
  126. y = tmp;
  127. }
  128. x = frexp(x,&x_expt);
  129. y = frexp(y,&y_expt);
  130. y = ldexp(y,y_expt-x_expt);
  131. tmp = 1.0/(x*x + y*y);
  132. z.re = z.re*tmp*ldexp(1.0,-2*x_expt);
  133. z.im = -z.im*tmp*ldexp(1.0,-2*x_expt);
  134. return z;
  135. }
  136. #endif
  137. #ifndef zdiv
  138. /* zdiv -- returns z1/z2 */
  139. complex zdiv(z1,z2)
  140. complex z1, z2;
  141. {
  142. return zmlt(z1,zinv(z2));
  143. }
  144. #endif
  145. #ifndef zsqrt
  146. /* zsqrt -- returns sqrt(z); uses branch with Re sqrt(z) >= 0 */
  147. complex zsqrt(z)
  148. complex z;
  149. {
  150. complex w; /* == sqrt(z) at end */
  151. Real alpha;
  152. alpha = sqrt(0.5*(fabs(z.re) + zabs(z)));
  153. if (alpha!=0)
  154. {
  155. if (z.re>=0.0)
  156. {
  157. w.re = alpha;
  158. w.im = z.im / (2.0*alpha);
  159. }
  160. else
  161. {
  162. w.re = fabs(z.im)/(2.0*alpha);
  163. w.im = ( z.im >= 0 ) ? alpha : - alpha;
  164. }
  165. }
  166. else
  167. w.re = w.im = 0.0;
  168. return w;
  169. }
  170. #endif
  171. #ifndef zexp
  172. /* zexp -- returns exp(z) */
  173. complex zexp(z)
  174. complex z;
  175. {
  176. complex w; /* == exp(z) at end */
  177. Real r;
  178. r = exp(z.re);
  179. w.re = r*cos(z.im);
  180. w.im = r*sin(z.im);
  181. return w;
  182. }
  183. #endif
  184. #ifndef zlog
  185. /* zlog -- returns log(z); uses principal branch with -pi <= Im log(z) <= pi */
  186. complex zlog(z)
  187. complex z;
  188. {
  189. complex w; /* == log(z) at end */
  190. w.re = log(zabs(z));
  191. w.im = atan2(z.im,z.re);
  192. return w;
  193. }
  194. #endif
  195. #ifndef zconj
  196. complex zconj(z)
  197. complex z;
  198. {
  199. complex w; /* == conj(z) */
  200. w.re = z.re;
  201. w.im = - z.im;
  202. return w;
  203. }
  204. #endif
  205. #endif