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