\File{complex\-gcc.c},{00:02},{Dec 28 1997}
\L{\LB{\C{}\/* \$Source: \/mit\/bert\/Cvsroot\/Thesis\/src\/exact\/complex\-gcc.c,v \$}}
\L{\LB{ *}}
\L{\LB{ * Various helpful operations on complex numbers}}
\L{\LB{ *\/\CE{}}}
\L{\LB{}}
\L{\LB{\K{static} \K{char} rcsid\_complex\_c[] = \S{}\"\$Id: complex\-gcc.c,v 1.8 1997\/12\/28 05:02:54 bert Exp \$\"\SE{};}}
\L{\LB{}}
\L{\LB{\K{\#include} \S{}\"complex\-gcc.h\"\SE{}}}
\L{\LB{\K{\#include} \<math.h\>}}
\L{\LB{}}
\L{\LB{\C{}\/*}}
\L{\LB{ *   exponential of a complex number.}}
\L{\LB{ *\/\CE{}}}
\L{\LB{\K{complex} \_Cexp(\K{complex} z)}}
\L{\LB{\{}}
\L{\LB{  \K{return} exp(Re(z)) * (cos(Im(z)) + 1i*sin(Im(z)));}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{\C{}\/*}}
\L{\LB{ *   return (real) absolute value of a complex number.}}
\L{\LB{ *\/\CE{}}}
\L{\LB{\K{real} \_Cabs(\K{complex} z)}}
\L{\LB{\{}}
\L{\LB{  \C{}\/* C has no power operator, and it\'s silly to call pow() for squaring... *\/\CE{}}}
\L{\LB{  \K{return} sqrt((Re(z)*Re(z)) + (Im(z)*Im(z)));}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{\C{}\/*}}
\L{\LB{ *   return square of (real) absolute value of a complex number.}}
\L{\LB{ *\/\CE{}}}
\L{\LB{\K{real} \_Csqabs(\K{complex} z)}}
\L{\LB{\{}}
\L{\LB{  \C{}\/* C has no power operator, and it\'s silly to call pow() for squaring... *\/\CE{}}}
\L{\LB{  \K{return} ((Re(z)*Re(z)) + (Im(z)*Im(z)));}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{\C{}\/*}}
\L{\LB{ *   return one of two square roots of a complex number.}}
\L{\LB{ *   we choose to return the value with positive real part.}}
\L{\LB{ *\/\CE{}}}
\L{\LB{\K{complex} \_Csqrt(\K{complex} z)}}
\L{\LB{\{}}
\L{\LB{  \K{real} abq = sqrt((Re(z)*Re(z)) + (Im(z)*Im(z)));   \C{}\/* see above *\/\CE{}}}
\L{\LB{}}
\L{\LB{  \K{if} (Im(z)\>=0) \{}}
\L{\LB{    \K{return} sqrt((abq+Re(z))\/2) + 1i * sqrt((abq\-Re(z))\/2);}}
\L{\LB{  \} \K{else} \{}}
\L{\LB{    \K{return} sqrt((abq+Re(z))\/2) \- 1i * sqrt((abq\-Re(z))\/2);}}
\L{\LB{  \}}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{\C{}\/*}}
\L{\LB{ *   return the argument (ie, \"angle\") of a complex number.}}
\L{\LB{ *\/\CE{}}}
\L{\LB{\K{real} \_Carg(\K{complex} z)}}
\L{\LB{\{}}
\L{\LB{  \K{return} atan2(Im(z), Re(z));}}
\L{\LB{\}}}
