1. #ifndef __MYCOMPLEX__
    2. #define __MYCOMPLEX__
    3. class complex;
    4. complex&
    5. __doapl (complex* ths, const complex& r);
    6. complex&
    7. __doami (complex* ths, const complex& r);
    8. complex&
    9. __doaml (complex* ths, const complex& r);
    10. class complex
    11. {
    12. public:
    13. complex (double r = 0, double i = 0): re (r), im (i) { }
    14. complex& operator += (const complex&);
    15. complex& operator -= (const complex&);
    16. complex& operator *= (const complex&);
    17. complex& operator /= (const complex&);
    18. double real () const { return re; }
    19. double imag () const { return im; }
    20. private:
    21. double re, im;
    22. friend complex& __doapl (complex *, const complex&);
    23. friend complex& __doami (complex *, const complex&);
    24. friend complex& __doaml (complex *, const complex&);
    25. };
    26. inline complex&
    27. __doapl (complex* ths, const complex& r)
    28. {
    29. ths->re += r.re;
    30. ths->im += r.im;
    31. return *ths;
    32. }
    33. inline complex&
    34. complex::operator += (const complex& r)
    35. {
    36. return __doapl (this, r);
    37. }
    38. inline complex&
    39. __doami (complex* ths, const complex& r)
    40. {
    41. ths->re -= r.re;
    42. ths->im -= r.im;
    43. return *ths;
    44. }
    45. inline complex&
    46. complex::operator -= (const complex& r)
    47. {
    48. return __doami (this, r);
    49. }
    50. inline complex&
    51. __doaml (complex* ths, const complex& r)
    52. {
    53. double f = ths->re * r.re - ths->im * r.im;
    54. ths->im = ths->re * r.im + ths->im * r.re;
    55. ths->re = f;
    56. return *ths;
    57. }
    58. inline complex&
    59. complex::operator *= (const complex& r)
    60. {
    61. return __doaml (this, r);
    62. }
    63. inline double
    64. imag (const complex& x)
    65. {
    66. return x.imag ();
    67. }
    68. inline double
    69. real (const complex& x)
    70. {
    71. return x.real ();
    72. }
    73. inline complex
    74. operator + (const complex& x, const complex& y)
    75. {
    76. return complex (real (x) + real (y), imag (x) + imag (y));
    77. }
    78. inline complex
    79. operator + (const complex& x, double y)
    80. {
    81. return complex (real (x) + y, imag (x));
    82. }
    83. inline complex
    84. operator + (double x, const complex& y)
    85. {
    86. return complex (x + real (y), imag (y));
    87. }
    88. inline complex
    89. operator - (const complex& x, const complex& y)
    90. {
    91. return complex (real (x) - real (y), imag (x) - imag (y));
    92. }
    93. inline complex
    94. operator - (const complex& x, double y)
    95. {
    96. return complex (real (x) - y, imag (x));
    97. }
    98. inline complex
    99. operator - (double x, const complex& y)
    100. {
    101. return complex (x - real (y), - imag (y));
    102. }
    103. inline complex
    104. operator * (const complex& x, const complex& y)
    105. {
    106. return complex (real (x) * real (y) - imag (x) * imag (y),
    107. real (x) * imag (y) + imag (x) * real (y));
    108. }
    109. inline complex
    110. operator * (const complex& x, double y)
    111. {
    112. return complex (real (x) * y, imag (x) * y);
    113. }
    114. inline complex
    115. operator * (double x, const complex& y)
    116. {
    117. return complex (x * real (y), x * imag (y));
    118. }
    119. complex
    120. operator / (const complex& x, double y)
    121. {
    122. return complex (real (x) / y, imag (x) / y);
    123. }
    124. inline complex
    125. operator + (const complex& x)
    126. {
    127. return x;
    128. }
    129. inline complex
    130. operator - (const complex& x)
    131. {
    132. return complex (-real (x), -imag (x));
    133. }
    134. inline bool
    135. operator == (const complex& x, const complex& y)
    136. {
    137. return real (x) == real (y) && imag (x) == imag (y);
    138. }
    139. inline bool
    140. operator == (const complex& x, double y)
    141. {
    142. return real (x) == y && imag (x) == 0;
    143. }
    144. inline bool
    145. operator == (double x, const complex& y)
    146. {
    147. return x == real (y) && imag (y) == 0;
    148. }
    149. inline bool
    150. operator != (const complex& x, const complex& y)
    151. {
    152. return real (x) != real (y) || imag (x) != imag (y);
    153. }
    154. inline bool
    155. operator != (const complex& x, double y)
    156. {
    157. return real (x) != y || imag (x) != 0;
    158. }
    159. inline bool
    160. operator != (double x, const complex& y)
    161. {
    162. return x != real (y) || imag (y) != 0;
    163. }
    164. #include <cmath>
    165. inline complex
    166. polar (double r, double t)
    167. {
    168. return complex (r * cos (t), r * sin (t));
    169. }
    170. inline complex
    171. conj (const complex& x)
    172. {
    173. return complex (real (x), -imag (x));
    174. }
    175. inline double
    176. norm (const complex& x)
    177. {
    178. return real (x) * real (x) + imag (x) * imag (x);
    179. }
    180. #endif //__MYCOMPLEX__