complex.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #ifndef _complex__h_
  2. #define _complex__h_
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include <iostream.h>
  6. class Complex
  7. {
  8. friend Complex operator*(const double&, const Complex&);
  9. friend Complex operator+(const double&, const Complex&);
  10. friend Complex operator-(const double&, const Complex&);
  11. friend Complex operator/(const double&, const Complex&);
  12. friend double real(const Complex&);
  13. friend double imag(const Complex&);
  14. friend double arg(const Complex&);
  15. friend double cabs(const Complex&);
  16. friend double cabsm(const Complex&);
  17. friend Complex conjg(const Complex&);
  18. friend Complex cmplx(const double,const double);
  19. friend Complex polar(const double&, const double&);
  20. friend Complex cos(const Complex&);
  21. friend Complex cosh(const Complex&);
  22. friend Complex sin(const Complex&);
  23. friend Complex sinh(const Complex&);
  24. friend Complex tan(const Complex&);
  25. friend Complex tanh(const Complex&);
  26. friend Complex sqrt(const Complex&);
  27. friend Complex sqrtm(const Complex&);
  28. friend Complex exp(const Complex&);
  29. friend Complex log(const Complex&);
  30. friend Complex log10(const Complex&);
  31. friend Complex pow(const Complex&, const Complex&);
  32. friend Complex pow(const Complex&, const double&);
  33. friend Complex pow(const Complex&, int&);
  34. friend Complex pow(const double&, const Complex&);
  35. friend ostream& operator<<(ostream&, Complex&);
  36. friend istream& operator>>(istream&, Complex&);
  37. public:
  38. Complex(){} // constructors
  39. Complex(double r, double i = 0.0);
  40. Complex(const Complex&);
  41. Complex operator-(const Complex&) const;
  42. Complex operator-(const double&) const;
  43. Complex operator+(const Complex&) const;
  44. Complex operator+(const double&) const;
  45. Complex operator*(const Complex&) const;
  46. Complex operator*(const double&) const;
  47. Complex operator/(const Complex&) const;
  48. Complex operator/(const double&) const;
  49. Complex operator-();
  50. Complex operator-() const;
  51. Complex& operator=(const Complex&);
  52. Complex& operator=(const double&);
  53. Complex& operator+=(const Complex&);
  54. Complex& operator+=(const double&);
  55. Complex& operator-=(const Complex&);
  56. Complex& operator-=(const double&);
  57. Complex& operator*=(const Complex&);
  58. Complex& operator*=(const double&);
  59. Complex& operator/=(const Complex&);
  60. Complex& operator/=(const double&);
  61. int operator==(const Complex&) const;
  62. int operator!=(const Complex&) const;
  63. private:
  64. double re, im;
  65. };
  66. inline Complex Complex::operator*(const Complex& c) const {
  67. Complex com;
  68. com.re = re * c.re - im * c.im;
  69. com.im = im * c.re + re * c.im;
  70. return(com);
  71. }
  72. inline Complex Complex::operator*(const double& r) const {
  73. Complex com;
  74. com.re = re * r;
  75. com.im = im * r;
  76. return(com);
  77. }
  78. inline Complex Complex::operator/(const Complex& c) const {
  79. Complex com;
  80. double t, d;
  81. if(fabs(c.re) <= fabs(c.im)) {
  82. t = c.re / c.im;
  83. d = c.im * (1 + t*t);
  84. com.re = (re * t + im) / d;
  85. com.im = (im * t - re) / d;
  86. } else {
  87. t = c.im / c.re;
  88. d = c.re * (1 + t*t);
  89. com.re = (re + im * t) / d;
  90. com.im = (im - re * t) / d;
  91. }
  92. return(com);
  93. }
  94. inline Complex Complex::operator/(const double& r) const {
  95. Complex com;
  96. com.re = re / r;
  97. com.im = im / r;
  98. return(com);
  99. }
  100. inline Complex Complex::operator-(const Complex& c) const {
  101. Complex com;
  102. com.re = re - c.re;
  103. com.im = im - c.im;
  104. return(com);
  105. }
  106. inline Complex Complex::operator-(const double& c) const {
  107. Complex com;
  108. com.re = re - c;
  109. com.im = im;
  110. return(com);
  111. }
  112. inline Complex Complex::operator+(const Complex& c) const {
  113. Complex com;
  114. com.re = re + c.re;
  115. com.im = im + c.im;
  116. return(com);
  117. }
  118. inline Complex Complex::operator+(const double& c) const {
  119. Complex com;
  120. com.re = re + c;
  121. com.im = im;
  122. return(com);
  123. }
  124. inline Complex& Complex::operator=(const Complex& c) {
  125. if(this == &c) return(*this);
  126. re = c.re;
  127. im = c.im;
  128. return(*this);
  129. }
  130. inline Complex& Complex::operator=(const double& c) {
  131. re = c;
  132. im = 0.0;
  133. return(*this);
  134. }
  135. inline Complex& Complex::operator+=(const Complex& c) {
  136. re = re + c.re;
  137. im = im + c.im;
  138. return(*this);
  139. }
  140. inline Complex& Complex::operator+=(const double& c) {
  141. re = re + c;
  142. im = im;
  143. return(*this);
  144. }
  145. inline Complex& Complex::operator-=(const Complex& c) {
  146. re = re - c.re;
  147. im = im - c.im;
  148. return(*this);
  149. }
  150. inline Complex& Complex::operator-=(const double& c) {
  151. re = re - c;
  152. im = im;
  153. return(*this);
  154. }
  155. inline Complex& Complex::operator*=(const Complex& c) {
  156. double rep, imp;
  157. rep = re * c.re-im * c.im;
  158. imp = im * c.re+re * c.im;
  159. re = rep; im = imp;
  160. return(*this);
  161. }
  162. inline Complex& Complex::operator*=(const double& c) {
  163. re = re * c;
  164. im = im * c;
  165. return(*this);
  166. }
  167. inline Complex& Complex::operator/=(const Complex& c) {
  168. double t, d, rep, imp;
  169. if(fabs(c.re) <= fabs(c.im)) {
  170. t = c.re / c.im;
  171. d = c.im * (1 + t*t);
  172. rep = (re * t + im) / d;
  173. imp = (im * t - re) / d;
  174. } else {
  175. t = c.im / c.re;
  176. d = c.re * (1 + t*t);
  177. rep = (re + im * t) / d;
  178. imp = (im - re * t) / d;
  179. }
  180. re = rep; im = imp;
  181. return(*this);
  182. }
  183. inline Complex& Complex::operator/=(const double& r) {
  184. double rep, imp;
  185. rep = re / r;
  186. imp = im / r;
  187. re = rep; im = imp;
  188. return(*this);
  189. }
  190. inline int Complex::operator==(const Complex& c) const {
  191. return((re==c.re) && (im==c.im));
  192. }
  193. inline int Complex::operator!=(const Complex& c) const {
  194. return((re!=c.re) || (im!=c.im));
  195. }
  196. inline Complex Complex::operator-() {
  197. Complex result;
  198. result.re = -re;
  199. result.im = -im;
  200. return(result);
  201. }
  202. inline Complex Complex::operator-() const {
  203. Complex result;
  204. result.re = -re;
  205. result.im = -im;
  206. return(result);
  207. }
  208. #endif