| 1 | /*
|
|---|
| 2 | * SPDX-FileCopyrightText: 2019 Jaroslav Jindrak
|
|---|
| 3 | *
|
|---|
| 4 | * SPDX-License-Identifier: BSD-3-Clause
|
|---|
| 5 | */
|
|---|
| 6 |
|
|---|
| 7 | #ifndef LIBCPP_BITS_COMPLEX
|
|---|
| 8 | #define LIBCPP_BITS_COMPLEX
|
|---|
| 9 |
|
|---|
| 10 | #include <cassert>
|
|---|
| 11 | #include <iosfwd>
|
|---|
| 12 | #include <sstream>
|
|---|
| 13 |
|
|---|
| 14 | namespace std
|
|---|
| 15 | {
|
|---|
| 16 | template<class T>
|
|---|
| 17 | class complex
|
|---|
| 18 | {
|
|---|
| 19 | public:
|
|---|
| 20 | using value_type = T;
|
|---|
| 21 |
|
|---|
| 22 | constexpr complex(const value_type& re = value_type{},
|
|---|
| 23 | const value_type& im = value_type{})
|
|---|
| 24 | : real_{re}, imag_{im}
|
|---|
| 25 | { /* DUMMY BODY */ }
|
|---|
| 26 |
|
|---|
| 27 | constexpr complex(const complex& other)
|
|---|
| 28 | : real_{other.real_}, imag_{other.imag_}
|
|---|
| 29 | { /* DUMMY BODY */ }
|
|---|
| 30 |
|
|---|
| 31 | template<class U>
|
|---|
| 32 | constexpr complex(const complex<U>& other)
|
|---|
| 33 | : real_(other.real()), imag_(other.imag())
|
|---|
| 34 | { /* DUMMY BODY */ }
|
|---|
| 35 |
|
|---|
| 36 | constexpr value_type real() const
|
|---|
| 37 | {
|
|---|
| 38 | return real_;
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | void real(value_type val)
|
|---|
| 42 | {
|
|---|
| 43 | real_ = val;
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | constexpr value_type imag() const
|
|---|
| 47 | {
|
|---|
| 48 | return imag_;
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | void imag(value_type val)
|
|---|
| 52 | {
|
|---|
| 53 | imag_ = val;
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | complex& operator=(const value_type& val)
|
|---|
| 57 | {
|
|---|
| 58 | real_ = val;
|
|---|
| 59 | imag_ = value_type{};
|
|---|
| 60 |
|
|---|
| 61 | return *this;
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | complex& operator+=(const value_type& val)
|
|---|
| 65 | {
|
|---|
| 66 | real_ += val;
|
|---|
| 67 |
|
|---|
| 68 | return *this;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | complex& operator-=(const value_type& val)
|
|---|
| 72 | {
|
|---|
| 73 | real_ -= val;
|
|---|
| 74 |
|
|---|
| 75 | return *this;
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | complex& operator*=(const value_type& val)
|
|---|
| 79 | {
|
|---|
| 80 | real_ *= val;
|
|---|
| 81 | imag_ *= val;
|
|---|
| 82 |
|
|---|
| 83 | return *this;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | complex& operator/=(const value_type& val)
|
|---|
| 87 | {
|
|---|
| 88 | real_ /= val;
|
|---|
| 89 | imag_ /= val;
|
|---|
| 90 |
|
|---|
| 91 | return *this;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | complex& operator=(const complex& other)
|
|---|
| 95 | {
|
|---|
| 96 | real_ = other.real_;
|
|---|
| 97 | imag_ = other.imag_;
|
|---|
| 98 |
|
|---|
| 99 | return *this;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | template<class U>
|
|---|
| 103 | complex& operator=(const complex<U>& rhs)
|
|---|
| 104 | {
|
|---|
| 105 | real_ = rhs.real_;
|
|---|
| 106 | imag_ = rhs.imag_;
|
|---|
| 107 |
|
|---|
| 108 | return *this;
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | template<class U>
|
|---|
| 112 | complex& operator+=(const complex<U>& rhs)
|
|---|
| 113 | {
|
|---|
| 114 | real_ += rhs.real_;
|
|---|
| 115 | imag_ += rhs.imag_;
|
|---|
| 116 |
|
|---|
| 117 | return *this;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | template<class U>
|
|---|
| 121 | complex& operator-=(const complex<U>& rhs)
|
|---|
| 122 | {
|
|---|
| 123 | real_ -= rhs.real_;
|
|---|
| 124 | imag_ -= rhs.imag_;
|
|---|
| 125 |
|
|---|
| 126 | return *this;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | template<class U>
|
|---|
| 130 | complex& operator*=(const complex<U>& rhs)
|
|---|
| 131 | {
|
|---|
| 132 | auto old_real = real_;
|
|---|
| 133 | real_ = real_ * rhs.real_ - imag_ * rhs.imag_;
|
|---|
| 134 | imag_ = old_real * rhs.imag_ + imag_ * rhs.real_;
|
|---|
| 135 |
|
|---|
| 136 | return *this;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | template<class U>
|
|---|
| 140 | complex& operator/=(const complex<U>& rhs)
|
|---|
| 141 | {
|
|---|
| 142 | auto old_real = real_;
|
|---|
| 143 | real_ = (real_ * rhs.real_ + imag_ * rhs.imag_)
|
|---|
| 144 | / (rhs.real_ * rhs.real_ + rhs.imag_ * rhs.imag_);
|
|---|
| 145 | imag_ = (imag_ * rhs.real_ - old_real * rhs.imag_)
|
|---|
| 146 | / (rhs.real_ * rhs.real_ + rhs.imag_ * rhs.imag_);
|
|---|
| 147 |
|
|---|
| 148 | return *this;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | private:
|
|---|
| 152 | value_type real_;
|
|---|
| 153 | value_type imag_;
|
|---|
| 154 | };
|
|---|
| 155 |
|
|---|
| 156 | template<>
|
|---|
| 157 | class complex<float>
|
|---|
| 158 | {
|
|---|
| 159 | public:
|
|---|
| 160 | using value_type = float;
|
|---|
| 161 |
|
|---|
| 162 | constexpr complex(const value_type& re = value_type{},
|
|---|
| 163 | const value_type& im = value_type{})
|
|---|
| 164 | : real_{re}, imag_{im}
|
|---|
| 165 | { /* DUMMY BODY */ }
|
|---|
| 166 |
|
|---|
| 167 | constexpr complex(const complex& other)
|
|---|
| 168 | : real_{other.real_}, imag_{other.imag_}
|
|---|
| 169 | { /* DUMMY BODY */ }
|
|---|
| 170 |
|
|---|
| 171 | template<class U>
|
|---|
| 172 | constexpr complex(const complex<U>& other)
|
|---|
| 173 | : real_(other.real()), imag_(other.imag())
|
|---|
| 174 | { /* DUMMY BODY */ }
|
|---|
| 175 |
|
|---|
| 176 | constexpr value_type real() const
|
|---|
| 177 | {
|
|---|
| 178 | return real_;
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | void real(value_type val)
|
|---|
| 182 | {
|
|---|
| 183 | real_ = val;
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | constexpr value_type imag() const
|
|---|
| 187 | {
|
|---|
| 188 | return imag_;
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | void imag(value_type val)
|
|---|
| 192 | {
|
|---|
| 193 | imag_ = val;
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | complex& operator=(const value_type& val)
|
|---|
| 197 | {
|
|---|
| 198 | real_ = val;
|
|---|
| 199 | imag_ = value_type{};
|
|---|
| 200 |
|
|---|
| 201 | return *this;
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 | complex& operator+=(const value_type& val)
|
|---|
| 205 | {
|
|---|
| 206 | real_ += val;
|
|---|
| 207 |
|
|---|
| 208 | return *this;
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | complex& operator-=(const value_type& val)
|
|---|
| 212 | {
|
|---|
| 213 | real_ -= val;
|
|---|
| 214 |
|
|---|
| 215 | return *this;
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | complex& operator*=(const value_type& val)
|
|---|
| 219 | {
|
|---|
| 220 | real_ *= val;
|
|---|
| 221 | imag_ *= val;
|
|---|
| 222 |
|
|---|
| 223 | return *this;
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | complex& operator/=(const value_type& val)
|
|---|
| 227 | {
|
|---|
| 228 | real_ /= val;
|
|---|
| 229 | imag_ /= val;
|
|---|
| 230 |
|
|---|
| 231 | return *this;
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | complex& operator=(const complex& other)
|
|---|
| 235 | {
|
|---|
| 236 | real_ = other.real_;
|
|---|
| 237 | imag_ = other.imag_;
|
|---|
| 238 |
|
|---|
| 239 | return *this;
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | template<class U>
|
|---|
| 243 | complex& operator=(const complex<U>& rhs)
|
|---|
| 244 | {
|
|---|
| 245 | real_ = rhs.real_;
|
|---|
| 246 | imag_ = rhs.imag_;
|
|---|
| 247 |
|
|---|
| 248 | return *this;
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | template<class U>
|
|---|
| 252 | complex& operator+=(const complex<U>& rhs)
|
|---|
| 253 | {
|
|---|
| 254 | real_ += rhs.real_;
|
|---|
| 255 | imag_ += rhs.imag_;
|
|---|
| 256 |
|
|---|
| 257 | return *this;
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | template<class U>
|
|---|
| 261 | complex& operator-=(const complex<U>& rhs)
|
|---|
| 262 | {
|
|---|
| 263 | real_ -= rhs.real_;
|
|---|
| 264 | imag_ -= rhs.imag_;
|
|---|
| 265 |
|
|---|
| 266 | return *this;
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | template<class U>
|
|---|
| 270 | complex& operator*=(const complex<U>& rhs)
|
|---|
| 271 | {
|
|---|
| 272 | auto old_real = real_;
|
|---|
| 273 | real_ = real_ * rhs.real_ - imag_ * rhs.imag_;
|
|---|
| 274 | imag_ = old_real * rhs.imag_ + imag_ * rhs.real_;
|
|---|
| 275 |
|
|---|
| 276 | return *this;
|
|---|
| 277 | }
|
|---|
| 278 |
|
|---|
| 279 | template<class U>
|
|---|
| 280 | complex& operator/=(const complex<U>& rhs)
|
|---|
| 281 | {
|
|---|
| 282 | auto old_real = real_;
|
|---|
| 283 | real_ = (real_ * rhs.real_ + imag_ * rhs.imag_)
|
|---|
| 284 | / (rhs.real_ * rhs.real_ + rhs.imag_ * rhs.imag_);
|
|---|
| 285 | imag_ = (imag_ * rhs.real_ - old_real * rhs.imag_)
|
|---|
| 286 | / (rhs.real_ * rhs.real_ + rhs.imag_ * rhs.imag_);
|
|---|
| 287 |
|
|---|
| 288 | return *this;
|
|---|
| 289 | }
|
|---|
| 290 |
|
|---|
| 291 | private:
|
|---|
| 292 | value_type real_;
|
|---|
| 293 | value_type imag_;
|
|---|
| 294 | };
|
|---|
| 295 |
|
|---|
| 296 | template<>
|
|---|
| 297 | class complex<double>
|
|---|
| 298 | {
|
|---|
| 299 | public:
|
|---|
| 300 | using value_type = double;
|
|---|
| 301 |
|
|---|
| 302 | constexpr complex(const value_type& re = value_type{},
|
|---|
| 303 | const value_type& im = value_type{})
|
|---|
| 304 | : real_{re}, imag_{im}
|
|---|
| 305 | { /* DUMMY BODY */ }
|
|---|
| 306 |
|
|---|
| 307 | constexpr complex(const complex& other)
|
|---|
| 308 | : real_{other.real_}, imag_{other.imag_}
|
|---|
| 309 | { /* DUMMY BODY */ }
|
|---|
| 310 |
|
|---|
| 311 | template<class U>
|
|---|
| 312 | constexpr complex(const complex<U>& other)
|
|---|
| 313 | : real_(other.real()), imag_(other.imag())
|
|---|
| 314 | { /* DUMMY BODY */ }
|
|---|
| 315 |
|
|---|
| 316 | constexpr value_type real() const
|
|---|
| 317 | {
|
|---|
| 318 | return real_;
|
|---|
| 319 | }
|
|---|
| 320 |
|
|---|
| 321 | void real(value_type val)
|
|---|
| 322 | {
|
|---|
| 323 | real_ = val;
|
|---|
| 324 | }
|
|---|
| 325 |
|
|---|
| 326 | constexpr value_type imag() const
|
|---|
| 327 | {
|
|---|
| 328 | return imag_;
|
|---|
| 329 | }
|
|---|
| 330 |
|
|---|
| 331 | void imag(value_type val)
|
|---|
| 332 | {
|
|---|
| 333 | imag_ = val;
|
|---|
| 334 | }
|
|---|
| 335 |
|
|---|
| 336 | complex& operator=(const value_type& val)
|
|---|
| 337 | {
|
|---|
| 338 | real_ = val;
|
|---|
| 339 | imag_ = value_type{};
|
|---|
| 340 |
|
|---|
| 341 | return *this;
|
|---|
| 342 | }
|
|---|
| 343 |
|
|---|
| 344 | complex& operator+=(const value_type& val)
|
|---|
| 345 | {
|
|---|
| 346 | real_ += val;
|
|---|
| 347 |
|
|---|
| 348 | return *this;
|
|---|
| 349 | }
|
|---|
| 350 |
|
|---|
| 351 | complex& operator-=(const value_type& val)
|
|---|
| 352 | {
|
|---|
| 353 | real_ -= val;
|
|---|
| 354 |
|
|---|
| 355 | return *this;
|
|---|
| 356 | }
|
|---|
| 357 |
|
|---|
| 358 | complex& operator*=(const value_type& val)
|
|---|
| 359 | {
|
|---|
| 360 | real_ *= val;
|
|---|
| 361 | imag_ *= val;
|
|---|
| 362 |
|
|---|
| 363 | return *this;
|
|---|
| 364 | }
|
|---|
| 365 |
|
|---|
| 366 | complex& operator/=(const value_type& val)
|
|---|
| 367 | {
|
|---|
| 368 | real_ /= val;
|
|---|
| 369 | imag_ /= val;
|
|---|
| 370 |
|
|---|
| 371 | return *this;
|
|---|
| 372 | }
|
|---|
| 373 |
|
|---|
| 374 | complex& operator=(const complex& other)
|
|---|
| 375 | {
|
|---|
| 376 | real_ = other.real_;
|
|---|
| 377 | imag_ = other.imag_;
|
|---|
| 378 |
|
|---|
| 379 | return *this;
|
|---|
| 380 | }
|
|---|
| 381 |
|
|---|
| 382 | template<class U>
|
|---|
| 383 | complex& operator=(const complex<U>& rhs)
|
|---|
| 384 | {
|
|---|
| 385 | real_ = rhs.real_;
|
|---|
| 386 | imag_ = rhs.imag_;
|
|---|
| 387 |
|
|---|
| 388 | return *this;
|
|---|
| 389 | }
|
|---|
| 390 |
|
|---|
| 391 | template<class U>
|
|---|
| 392 | complex& operator+=(const complex<U>& rhs)
|
|---|
| 393 | {
|
|---|
| 394 | real_ += rhs.real_;
|
|---|
| 395 | imag_ += rhs.imag_;
|
|---|
| 396 |
|
|---|
| 397 | return *this;
|
|---|
| 398 | }
|
|---|
| 399 |
|
|---|
| 400 | template<class U>
|
|---|
| 401 | complex& operator-=(const complex<U>& rhs)
|
|---|
| 402 | {
|
|---|
| 403 | real_ -= rhs.real_;
|
|---|
| 404 | imag_ -= rhs.imag_;
|
|---|
| 405 |
|
|---|
| 406 | return *this;
|
|---|
| 407 | }
|
|---|
| 408 |
|
|---|
| 409 | template<class U>
|
|---|
| 410 | complex& operator*=(const complex<U>& rhs)
|
|---|
| 411 | {
|
|---|
| 412 | auto old_real = real_;
|
|---|
| 413 | real_ = real_ * rhs.real_ - imag_ * rhs.imag_;
|
|---|
| 414 | imag_ = old_real * rhs.imag_ + imag_ * rhs.real_;
|
|---|
| 415 |
|
|---|
| 416 | return *this;
|
|---|
| 417 | }
|
|---|
| 418 |
|
|---|
| 419 | template<class U>
|
|---|
| 420 | complex& operator/=(const complex<U>& rhs)
|
|---|
| 421 | {
|
|---|
| 422 | auto old_real = real_;
|
|---|
| 423 | real_ = (real_ * rhs.real_ + imag_ * rhs.imag_)
|
|---|
| 424 | / (rhs.real_ * rhs.real_ + rhs.imag_ * rhs.imag_);
|
|---|
| 425 | imag_ = (imag_ * rhs.real_ - old_real * rhs.imag_)
|
|---|
| 426 | / (rhs.real_ * rhs.real_ + rhs.imag_ * rhs.imag_);
|
|---|
| 427 |
|
|---|
| 428 | return *this;
|
|---|
| 429 | }
|
|---|
| 430 |
|
|---|
| 431 | private:
|
|---|
| 432 | value_type real_;
|
|---|
| 433 | value_type imag_;
|
|---|
| 434 | };
|
|---|
| 435 |
|
|---|
| 436 | template<>
|
|---|
| 437 | class complex<long double>
|
|---|
| 438 | {
|
|---|
| 439 | public:
|
|---|
| 440 | using value_type = long double;
|
|---|
| 441 |
|
|---|
| 442 | constexpr complex(const value_type& re = value_type{},
|
|---|
| 443 | const value_type& im = value_type{})
|
|---|
| 444 | : real_{re}, imag_{im}
|
|---|
| 445 | { /* DUMMY BODY */ }
|
|---|
| 446 |
|
|---|
| 447 | constexpr complex(const complex& other)
|
|---|
| 448 | : real_{other.real_}, imag_{other.imag_}
|
|---|
| 449 | { /* DUMMY BODY */ }
|
|---|
| 450 |
|
|---|
| 451 | template<class U>
|
|---|
| 452 | constexpr complex(const complex<U>& other)
|
|---|
| 453 | : real_(other.real()), imag_(other.imag())
|
|---|
| 454 | { /* DUMMY BODY */ }
|
|---|
| 455 |
|
|---|
| 456 | constexpr value_type real() const
|
|---|
| 457 | {
|
|---|
| 458 | return real_;
|
|---|
| 459 | }
|
|---|
| 460 |
|
|---|
| 461 | void real(value_type val)
|
|---|
| 462 | {
|
|---|
| 463 | real_ = val;
|
|---|
| 464 | }
|
|---|
| 465 |
|
|---|
| 466 | constexpr value_type imag() const
|
|---|
| 467 | {
|
|---|
| 468 | return imag_;
|
|---|
| 469 | }
|
|---|
| 470 |
|
|---|
| 471 | void imag(value_type val)
|
|---|
| 472 | {
|
|---|
| 473 | imag_ = val;
|
|---|
| 474 | }
|
|---|
| 475 |
|
|---|
| 476 | complex& operator=(const value_type& val)
|
|---|
| 477 | {
|
|---|
| 478 | real_ = val;
|
|---|
| 479 | imag_ = value_type{};
|
|---|
| 480 |
|
|---|
| 481 | return *this;
|
|---|
| 482 | }
|
|---|
| 483 |
|
|---|
| 484 | complex& operator+=(const value_type& val)
|
|---|
| 485 | {
|
|---|
| 486 | real_ += val;
|
|---|
| 487 |
|
|---|
| 488 | return *this;
|
|---|
| 489 | }
|
|---|
| 490 |
|
|---|
| 491 | complex& operator-=(const value_type& val)
|
|---|
| 492 | {
|
|---|
| 493 | real_ -= val;
|
|---|
| 494 |
|
|---|
| 495 | return *this;
|
|---|
| 496 | }
|
|---|
| 497 |
|
|---|
| 498 | complex& operator*=(const value_type& val)
|
|---|
| 499 | {
|
|---|
| 500 | real_ *= val;
|
|---|
| 501 | imag_ *= val;
|
|---|
| 502 |
|
|---|
| 503 | return *this;
|
|---|
| 504 | }
|
|---|
| 505 |
|
|---|
| 506 | complex& operator/=(const value_type& val)
|
|---|
| 507 | {
|
|---|
| 508 | real_ /= val;
|
|---|
| 509 | imag_ /= val;
|
|---|
| 510 |
|
|---|
| 511 | return *this;
|
|---|
| 512 | }
|
|---|
| 513 |
|
|---|
| 514 | complex& operator=(const complex& other)
|
|---|
| 515 | {
|
|---|
| 516 | real_ = other.real_;
|
|---|
| 517 | imag_ = other.imag_;
|
|---|
| 518 |
|
|---|
| 519 | return *this;
|
|---|
| 520 | }
|
|---|
| 521 |
|
|---|
| 522 | template<class U>
|
|---|
| 523 | complex& operator=(const complex<U>& rhs)
|
|---|
| 524 | {
|
|---|
| 525 | real_ = rhs.real_;
|
|---|
| 526 | imag_ = rhs.imag_;
|
|---|
| 527 |
|
|---|
| 528 | return *this;
|
|---|
| 529 | }
|
|---|
| 530 |
|
|---|
| 531 | template<class U>
|
|---|
| 532 | complex& operator+=(const complex<U>& rhs)
|
|---|
| 533 | {
|
|---|
| 534 | real_ += rhs.real_;
|
|---|
| 535 | imag_ += rhs.imag_;
|
|---|
| 536 |
|
|---|
| 537 | return *this;
|
|---|
| 538 | }
|
|---|
| 539 |
|
|---|
| 540 | template<class U>
|
|---|
| 541 | complex& operator-=(const complex<U>& rhs)
|
|---|
| 542 | {
|
|---|
| 543 | real_ -= rhs.real_;
|
|---|
| 544 | imag_ -= rhs.imag_;
|
|---|
| 545 |
|
|---|
| 546 | return *this;
|
|---|
| 547 | }
|
|---|
| 548 |
|
|---|
| 549 | template<class U>
|
|---|
| 550 | complex& operator*=(const complex<U>& rhs)
|
|---|
| 551 | {
|
|---|
| 552 | auto old_real = real_;
|
|---|
| 553 | real_ = real_ * rhs.real_ - imag_ * rhs.imag_;
|
|---|
| 554 | imag_ = old_real * rhs.imag_ + imag_ * rhs.real_;
|
|---|
| 555 |
|
|---|
| 556 | return *this;
|
|---|
| 557 | }
|
|---|
| 558 |
|
|---|
| 559 | template<class U>
|
|---|
| 560 | complex& operator/=(const complex<U>& rhs)
|
|---|
| 561 | {
|
|---|
| 562 | auto old_real = real_;
|
|---|
| 563 | real_ = (real_ * rhs.real_ + imag_ * rhs.imag_)
|
|---|
| 564 | / (rhs.real_ * rhs.real_ + rhs.imag_ * rhs.imag_);
|
|---|
| 565 | imag_ = (imag_ * rhs.real_ - old_real* rhs.imag_)
|
|---|
| 566 | / (rhs.real_ * rhs.real_ + rhs.imag_ * rhs.imag_);
|
|---|
| 567 |
|
|---|
| 568 | return *this;
|
|---|
| 569 | }
|
|---|
| 570 |
|
|---|
| 571 | private:
|
|---|
| 572 | value_type real_;
|
|---|
| 573 | value_type imag_;
|
|---|
| 574 | };
|
|---|
| 575 |
|
|---|
| 576 | /**
|
|---|
| 577 | * 26.4.6, operators:
|
|---|
| 578 | */
|
|---|
| 579 |
|
|---|
| 580 | template<class T>
|
|---|
| 581 | complex<T> operator+(const complex<T>& lhs, const complex<T>& rhs)
|
|---|
| 582 | {
|
|---|
| 583 | return complex<T>{lhs} += rhs;
|
|---|
| 584 | }
|
|---|
| 585 |
|
|---|
| 586 | template<class T>
|
|---|
| 587 | complex<T> operator+(const complex<T>& lhs, const T& rhs)
|
|---|
| 588 | {
|
|---|
| 589 | return complex<T>{lhs} += rhs;
|
|---|
| 590 | }
|
|---|
| 591 |
|
|---|
| 592 | template<class T>
|
|---|
| 593 | complex<T> operator+(const T& lhs, const complex<T>& rhs)
|
|---|
| 594 | {
|
|---|
| 595 | return complex<T>{lhs} += rhs;
|
|---|
| 596 | }
|
|---|
| 597 |
|
|---|
| 598 | template<class T>
|
|---|
| 599 | complex<T> operator-(const complex<T>& lhs, const complex<T>& rhs)
|
|---|
| 600 | {
|
|---|
| 601 | return complex<T>{lhs} -= rhs;
|
|---|
| 602 | }
|
|---|
| 603 |
|
|---|
| 604 | template<class T>
|
|---|
| 605 | complex<T> operator-(const complex<T>& lhs, const T& rhs)
|
|---|
| 606 | {
|
|---|
| 607 | return complex<T>{lhs} -= rhs;
|
|---|
| 608 | }
|
|---|
| 609 |
|
|---|
| 610 | template<class T>
|
|---|
| 611 | complex<T> operator-(const T& lhs, const complex<T>& rhs)
|
|---|
| 612 | {
|
|---|
| 613 | return complex<T>{lhs} -= rhs;
|
|---|
| 614 | }
|
|---|
| 615 |
|
|---|
| 616 | template<class T>
|
|---|
| 617 | complex<T> operator*(const complex<T>& lhs, const complex<T>& rhs)
|
|---|
| 618 | {
|
|---|
| 619 | return complex<T>{lhs} *= rhs;
|
|---|
| 620 | }
|
|---|
| 621 |
|
|---|
| 622 | template<class T>
|
|---|
| 623 | complex<T> operator*(const complex<T>& lhs, const T& rhs)
|
|---|
| 624 | {
|
|---|
| 625 | return complex<T>{lhs} *= rhs;
|
|---|
| 626 | }
|
|---|
| 627 |
|
|---|
| 628 | template<class T>
|
|---|
| 629 | complex<T> operator*(const T& lhs, const complex<T>& rhs)
|
|---|
| 630 | {
|
|---|
| 631 | return complex<T>{lhs} *= rhs;
|
|---|
| 632 | }
|
|---|
| 633 |
|
|---|
| 634 | template<class T>
|
|---|
| 635 | complex<T> operator/(const complex<T>& lhs, const complex<T>& rhs)
|
|---|
| 636 | {
|
|---|
| 637 | return complex<T>{lhs} /= rhs;
|
|---|
| 638 | }
|
|---|
| 639 |
|
|---|
| 640 | template<class T>
|
|---|
| 641 | complex<T> operator/(const complex<T>& lhs, const T& rhs)
|
|---|
| 642 | {
|
|---|
| 643 | return complex<T>{lhs} /= rhs;
|
|---|
| 644 | }
|
|---|
| 645 |
|
|---|
| 646 | template<class T>
|
|---|
| 647 | complex<T> operator/(const T& lhs, const complex<T>& rhs)
|
|---|
| 648 | {
|
|---|
| 649 | return complex<T>{lhs} /= rhs;
|
|---|
| 650 | }
|
|---|
| 651 |
|
|---|
| 652 | template<class T>
|
|---|
| 653 | complex<T> operator+(const complex<T>& c)
|
|---|
| 654 | {
|
|---|
| 655 | return complex<T>{c};
|
|---|
| 656 | }
|
|---|
| 657 |
|
|---|
| 658 | template<class T>
|
|---|
| 659 | complex<T> operator-(const complex<T>& c)
|
|---|
| 660 | {
|
|---|
| 661 | return complex<T>{-c.real(), -c.imag()};
|
|---|
| 662 | }
|
|---|
| 663 |
|
|---|
| 664 | template<class T>
|
|---|
| 665 | constexpr bool operator==(const complex<T>& lhs, const complex<T>& rhs)
|
|---|
| 666 | {
|
|---|
| 667 | return lhs.real() == rhs.real() && lhs.imag() == rhs.imag();
|
|---|
| 668 | }
|
|---|
| 669 |
|
|---|
| 670 | template<class T>
|
|---|
| 671 | constexpr bool operator==(const complex<T>& lhs, const T& rhs)
|
|---|
| 672 | {
|
|---|
| 673 | return lhs.real() == rhs && lhs.imag() == T{};
|
|---|
| 674 | }
|
|---|
| 675 |
|
|---|
| 676 | template<class T>
|
|---|
| 677 | constexpr bool operator==(const T& lhs, const complex<T>& rhs)
|
|---|
| 678 | {
|
|---|
| 679 | return lhs == rhs.real() && T{} == rhs.imag();
|
|---|
| 680 | }
|
|---|
| 681 |
|
|---|
| 682 | template<class T>
|
|---|
| 683 | constexpr bool operator!=(const complex<T>& lhs, const complex<T>& rhs)
|
|---|
| 684 | {
|
|---|
| 685 | return lhs.real() != rhs.real() || lhs.imag() != rhs.imag();
|
|---|
| 686 | }
|
|---|
| 687 |
|
|---|
| 688 | template<class T>
|
|---|
| 689 | constexpr bool operator!=(const complex<T>& lhs, const T& rhs)
|
|---|
| 690 | {
|
|---|
| 691 | return lhs.real() != rhs || lhs.imag() != T{};
|
|---|
| 692 | }
|
|---|
| 693 |
|
|---|
| 694 | template<class T>
|
|---|
| 695 | constexpr bool operator!=(const T& lhs, const complex<T>& rhs)
|
|---|
| 696 | {
|
|---|
| 697 | return lhs != rhs.real() || T{} != rhs.imag();
|
|---|
| 698 | }
|
|---|
| 699 |
|
|---|
| 700 | template<class T, class Char, class Traits>
|
|---|
| 701 | basic_istream<Char, Traits>& operator>>(basic_istream<Char, Traits>& is,
|
|---|
| 702 | const complex<T>& c)
|
|---|
| 703 | {
|
|---|
| 704 | // TODO: implement
|
|---|
| 705 | __unimplemented();
|
|---|
| 706 | return is;
|
|---|
| 707 | }
|
|---|
| 708 |
|
|---|
| 709 | template<class T, class Char, class Traits>
|
|---|
| 710 | basic_ostream<Char, Traits>& operator<<(basic_ostream<Char, Traits>& os,
|
|---|
| 711 | const complex<T>& c)
|
|---|
| 712 | {
|
|---|
| 713 | basic_ostringstream<Char, Traits> oss{};
|
|---|
| 714 | oss.flags(os.flags());
|
|---|
| 715 | oss.imbue(os.getloc());
|
|---|
| 716 | oss.precision(os.precision());
|
|---|
| 717 |
|
|---|
| 718 | oss << "(" << c.real() << "," << c.imag() << ")";
|
|---|
| 719 |
|
|---|
| 720 | return os << oss.str();
|
|---|
| 721 | }
|
|---|
| 722 |
|
|---|
| 723 | /**
|
|---|
| 724 | * 26.4.7, values:
|
|---|
| 725 | */
|
|---|
| 726 |
|
|---|
| 727 | template<class T>
|
|---|
| 728 | constexpr T real(const complex<T>& c)
|
|---|
| 729 | {
|
|---|
| 730 | return c.real();
|
|---|
| 731 | }
|
|---|
| 732 |
|
|---|
| 733 | template<class T>
|
|---|
| 734 | constexpr T imag(const complex<T>& c)
|
|---|
| 735 | {
|
|---|
| 736 | return c.imag();
|
|---|
| 737 | }
|
|---|
| 738 |
|
|---|
| 739 | template<class T>
|
|---|
| 740 | T abs(const complex<T>& c)
|
|---|
| 741 | {
|
|---|
| 742 | return c.real() * c.real() + c.imag() * c.imag();
|
|---|
| 743 | }
|
|---|
| 744 |
|
|---|
| 745 | template<class T>
|
|---|
| 746 | T arg(const complex<T>& c)
|
|---|
| 747 | {
|
|---|
| 748 | // TODO: implement
|
|---|
| 749 | __unimplemented();
|
|---|
| 750 | return c;
|
|---|
| 751 | }
|
|---|
| 752 |
|
|---|
| 753 | template<class T>
|
|---|
| 754 | T norm(const complex<T>& c)
|
|---|
| 755 | {
|
|---|
| 756 | return abs(c) * abs(c);
|
|---|
| 757 | }
|
|---|
| 758 |
|
|---|
| 759 | template<class T>
|
|---|
| 760 | complex<T> conj(const complex<T>& c)
|
|---|
| 761 | {
|
|---|
| 762 | // TODO: implement
|
|---|
| 763 | __unimplemented();
|
|---|
| 764 | return c;
|
|---|
| 765 | }
|
|---|
| 766 |
|
|---|
| 767 | template<class T>
|
|---|
| 768 | complex<T> proj(const complex<T>& c)
|
|---|
| 769 | {
|
|---|
| 770 | // TODO: implement
|
|---|
| 771 | __unimplemented();
|
|---|
| 772 | return c;
|
|---|
| 773 | }
|
|---|
| 774 |
|
|---|
| 775 | template<class T>
|
|---|
| 776 | complex<T> polar(const T&, const T& = T{})
|
|---|
| 777 | {
|
|---|
| 778 | // TODO: implement
|
|---|
| 779 | __unimplemented();
|
|---|
| 780 | return complex<T>{};
|
|---|
| 781 | }
|
|---|
| 782 |
|
|---|
| 783 | /**
|
|---|
| 784 | * 26.4.8, transcendentals:
|
|---|
| 785 | */
|
|---|
| 786 |
|
|---|
| 787 | template<class T>
|
|---|
| 788 | complex<T> acos(const complex<T>& c)
|
|---|
| 789 | {
|
|---|
| 790 | // TODO: implement
|
|---|
| 791 | __unimplemented();
|
|---|
| 792 | return c;
|
|---|
| 793 | }
|
|---|
| 794 |
|
|---|
| 795 | template<class T>
|
|---|
| 796 | complex<T> asin(const complex<T>& c)
|
|---|
| 797 | {
|
|---|
| 798 | // TODO: implement
|
|---|
| 799 | __unimplemented();
|
|---|
| 800 | return c;
|
|---|
| 801 | }
|
|---|
| 802 |
|
|---|
| 803 | template<class T>
|
|---|
| 804 | complex<T> atan(const complex<T>& c)
|
|---|
| 805 | {
|
|---|
| 806 | // TODO: implement
|
|---|
| 807 | __unimplemented();
|
|---|
| 808 | return c;
|
|---|
| 809 | }
|
|---|
| 810 |
|
|---|
| 811 | template<class T>
|
|---|
| 812 | complex<T> acosh(const complex<T>& c)
|
|---|
| 813 | {
|
|---|
| 814 | // TODO: implement
|
|---|
| 815 | __unimplemented();
|
|---|
| 816 | return c;
|
|---|
| 817 | }
|
|---|
| 818 |
|
|---|
| 819 | template<class T>
|
|---|
| 820 | complex<T> asinh(const complex<T>& c)
|
|---|
| 821 | {
|
|---|
| 822 | // TODO: implement
|
|---|
| 823 | __unimplemented();
|
|---|
| 824 | return c;
|
|---|
| 825 | }
|
|---|
| 826 |
|
|---|
| 827 | template<class T>
|
|---|
| 828 | complex<T> atanh(const complex<T>& c)
|
|---|
| 829 | {
|
|---|
| 830 | // TODO: implement
|
|---|
| 831 | __unimplemented();
|
|---|
| 832 | return c;
|
|---|
| 833 | }
|
|---|
| 834 |
|
|---|
| 835 | template<class T>
|
|---|
| 836 | complex<T> cos(const complex<T>& c)
|
|---|
| 837 | {
|
|---|
| 838 | // TODO: implement
|
|---|
| 839 | __unimplemented();
|
|---|
| 840 | return c;
|
|---|
| 841 | }
|
|---|
| 842 |
|
|---|
| 843 | template<class T>
|
|---|
| 844 | complex<T> cosh(const complex<T>& c)
|
|---|
| 845 | {
|
|---|
| 846 | // TODO: implement
|
|---|
| 847 | __unimplemented();
|
|---|
| 848 | return c;
|
|---|
| 849 | }
|
|---|
| 850 |
|
|---|
| 851 | template<class T>
|
|---|
| 852 | complex<T> exp(const complex<T>& c)
|
|---|
| 853 | {
|
|---|
| 854 | // TODO: implement
|
|---|
| 855 | __unimplemented();
|
|---|
| 856 | return c;
|
|---|
| 857 | }
|
|---|
| 858 |
|
|---|
| 859 | template<class T>
|
|---|
| 860 | complex<T> log(const complex<T>& c)
|
|---|
| 861 | {
|
|---|
| 862 | // TODO: implement
|
|---|
| 863 | __unimplemented();
|
|---|
| 864 | return c;
|
|---|
| 865 | }
|
|---|
| 866 |
|
|---|
| 867 | template<class T>
|
|---|
| 868 | complex<T> log10(const complex<T>& c)
|
|---|
| 869 | {
|
|---|
| 870 | // TODO: implement
|
|---|
| 871 | __unimplemented();
|
|---|
| 872 | return c;
|
|---|
| 873 | }
|
|---|
| 874 |
|
|---|
| 875 | template<class T>
|
|---|
| 876 | complex<T> pow(const complex<T>& base, const T& exp)
|
|---|
| 877 | {
|
|---|
| 878 | // TODO: implement
|
|---|
| 879 | __unimplemented();
|
|---|
| 880 | return base;
|
|---|
| 881 | }
|
|---|
| 882 |
|
|---|
| 883 | template<class T>
|
|---|
| 884 | complex<T> pow(const complex<T>& base, const complex<T>& exp)
|
|---|
| 885 | {
|
|---|
| 886 | // TODO: implement
|
|---|
| 887 | __unimplemented();
|
|---|
| 888 | return base;
|
|---|
| 889 | }
|
|---|
| 890 |
|
|---|
| 891 | template<class T>
|
|---|
| 892 | complex<T> pow(const T& base, const complex<T>& exp)
|
|---|
| 893 | {
|
|---|
| 894 | // TODO: implement
|
|---|
| 895 | __unimplemented();
|
|---|
| 896 | return complex<T>{base};
|
|---|
| 897 | }
|
|---|
| 898 |
|
|---|
| 899 | template<class T>
|
|---|
| 900 | complex<T> sin(const complex<T>& c)
|
|---|
| 901 | {
|
|---|
| 902 | // TODO: implement
|
|---|
| 903 | __unimplemented();
|
|---|
| 904 | return c;
|
|---|
| 905 | }
|
|---|
| 906 |
|
|---|
| 907 | template<class T>
|
|---|
| 908 | complex<T> sinh(const complex<T>& c)
|
|---|
| 909 | {
|
|---|
| 910 | // TODO: implement
|
|---|
| 911 | __unimplemented();
|
|---|
| 912 | return c;
|
|---|
| 913 | }
|
|---|
| 914 |
|
|---|
| 915 | template<class T>
|
|---|
| 916 | complex<T> sqrt(const complex<T>& c)
|
|---|
| 917 | {
|
|---|
| 918 | // TODO: implement
|
|---|
| 919 | __unimplemented();
|
|---|
| 920 | return c;
|
|---|
| 921 | }
|
|---|
| 922 |
|
|---|
| 923 | template<class T>
|
|---|
| 924 | complex<T> tan(const complex<T>& c)
|
|---|
| 925 | {
|
|---|
| 926 | // TODO: implement
|
|---|
| 927 | __unimplemented();
|
|---|
| 928 | return c;
|
|---|
| 929 | }
|
|---|
| 930 |
|
|---|
| 931 | template<class T>
|
|---|
| 932 | complex<T> tanh(const complex<T>& c)
|
|---|
| 933 | {
|
|---|
| 934 | // TODO: implement
|
|---|
| 935 | __unimplemented();
|
|---|
| 936 | return c;
|
|---|
| 937 | }
|
|---|
| 938 |
|
|---|
| 939 | /**
|
|---|
| 940 | * 26.4.10, complex literals:
|
|---|
| 941 | */
|
|---|
| 942 |
|
|---|
| 943 | #pragma GCC diagnostic push
|
|---|
| 944 | #pragma GCC diagnostic ignored "-Wliteral-suffix"
|
|---|
| 945 | inline namespace literals {
|
|---|
| 946 | inline namespace complex_literals
|
|---|
| 947 | {
|
|---|
| 948 | constexpr complex<long double> operator ""il(long double val)
|
|---|
| 949 | {
|
|---|
| 950 | return complex<long double>{0.0L, val};
|
|---|
| 951 | }
|
|---|
| 952 |
|
|---|
| 953 | constexpr complex<long double> operator ""il(unsigned long long val)
|
|---|
| 954 | {
|
|---|
| 955 | return complex<long double>{0.0L, static_cast<long double>(val)};
|
|---|
| 956 | }
|
|---|
| 957 |
|
|---|
| 958 | constexpr complex<double> operator ""i(long double val)
|
|---|
| 959 | {
|
|---|
| 960 | return complex<double>{0.0, static_cast<double>(val)};
|
|---|
| 961 | }
|
|---|
| 962 |
|
|---|
| 963 | constexpr complex<double> operator ""i(unsigned long long val)
|
|---|
| 964 | {
|
|---|
| 965 | return complex<double>{0.0, static_cast<double>(val)};
|
|---|
| 966 | }
|
|---|
| 967 |
|
|---|
| 968 | constexpr complex<float> operator ""if(long double val)
|
|---|
| 969 | {
|
|---|
| 970 | return complex<float>{0.0f, static_cast<float>(val)};
|
|---|
| 971 | }
|
|---|
| 972 |
|
|---|
| 973 | constexpr complex<float> operator ""if(unsigned long long val)
|
|---|
| 974 | {
|
|---|
| 975 | return complex<float>{0.0f, static_cast<float>(val)};
|
|---|
| 976 | }
|
|---|
| 977 | }}
|
|---|
| 978 | #pragma GCC diagnostic pop
|
|---|
| 979 | }
|
|---|
| 980 |
|
|---|
| 981 | #endif
|
|---|