-
Notifications
You must be signed in to change notification settings - Fork 10
/
mycomplex.h
1151 lines (970 loc) · 25.5 KB
/
mycomplex.h
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// complex standard header
#pragma once
#ifndef _COMPLEX_
#define _COMPLEX_
#ifndef RC_INVOKED
#include <ymath.h>
#include <ccomplex> /* dummy if not C99 library */
#include <cmath>
#include <sstream>
#pragma pack(push,_CRT_PACKING)
#pragma warning(push,3)
#pragma warning(disable: 4244)
_C_STD_BEGIN
#ifndef _C_COMPLEX_T
#define _C_COMPLEX_T
typedef value struct _C_double_complex
{ /* double complex */
double _real;
double _img;
// double _Val[2];
} _C_double_complex;
typedef value struct _C_float_complex
{ /* float complex */
float _real;
float _img;
// float _Val[2];
} _C_float_complex;
typedef value struct _C_ldouble_complex
{ /* long double complex */
long double _real;
long double _img;
// long double _Val[2];
} _C_ldouble_complex;
#endif /* _C_COMPLEX_T */
_C_STD_END
// COMPLEX _Val OFFSETS
#define _RE 0
#define _IM 1
_STD_BEGIN
typedef _CSTD _C_double_complex _Dcomplex_value;
typedef _CSTD _C_float_complex _Fcomplex_value;
typedef _CSTD _C_ldouble_complex _Lcomplex_value;
template<class _Ty>
class complex;
template<> class complex<float>;
template<> class complex<double>;
template<> class complex<long double>;
// TEMPLATE CLASS _Ctraits
template<class _Ty>
class _Ctraits
{ // complex traits for _Ty
public:
static _Ty _Cosh(_Ty _Left, _Ty _Right)
{ // return cosh(_Left) * _Right
return (_CSTD _Cosh((double)_Left, (double)_Right));
}
static short _Exp(_Ty *_Pleft, _Ty _Right, short _Exponent)
{ // compute exp(*_Pleft) * _Right * 2 ^ _Exponent
double _Tmp = (double)*_Pleft;
short _Ans = _CSTD _Exp(&_Tmp, (double)_Right, _Exponent);
*_Pleft = (_Ty)_Tmp;
return (_Ans);
}
static _Ty _Infv(_Ty)
{ // return infinity
return (_CSTD _Inf._Double);
}
static bool _Isinf(_Ty _Left)
{ // test for infinity
double _Tmp = (double)_Left;
return (_CSTD _Dtest(&_Tmp) == _INFCODE);
}
static bool _Isnan(_Ty _Left)
{ // test for NaN
double _Tmp = (double)_Left;
return (_CSTD _Dtest(&_Tmp) == _NANCODE);
}
static _Ty _Nanv(_Ty)
{ // return NaN
return (_CSTD _Nan._Double);
}
static _Ty _Sinh(_Ty _Left, _Ty _Right)
{ // return sinh(_Left) * _Right
return (_CSTD _Sinh((double)_Left, (double)_Right));
}
static _Ty atan2(_Ty _Yval, _Ty _Xval)
{ // return atan(_Yval / _Xval)
return (_CSTD atan2((double)_Yval, (double)_Xval));
}
static _Ty cos(_Ty _Left)
{ // return cos(_Left)
return (_CSTD cos((double)_Left));
}
static _Ty exp(_Ty _Left)
{ // return exp(_Left)
return (_CSTD exp((double)_Left));
}
static _Ty ldexp(_Ty _Left, int _Exponent)
{ // return _Left * 2 ^ _Exponent
return (_CSTD ldexp((double)_Left, _Exponent));
}
static _Ty log(_Ty _Left)
{ // return log(_Left)
return (_CSTD log((double)_Left));
}
static _Ty pow(_Ty _Left, _Ty _Right)
{ // return _Left ^ _Right
return (_CSTD pow((double)_Left, (double)_Right));
}
static _Ty sin(_Ty _Left)
{ // return sin(_Left)
return (_CSTD sin((double)_Left));
}
static _Ty sqrt(_Ty _Left)
{ // return sqrt(_Left)
return (_CSTD sqrt((double)_Left));
}
static _Ty tan(_Ty _Left)
{ // return tan(_Left)
return (_CSTD tan((double)_Left));
}
};
// CLASS _Ctraits<long double>
template<> class _Ctraits<long double>
{ // complex traits for long double
public:
typedef long double _Ty;
static _Ty _Cosh(_Ty _Left, _Ty _Right)
{ // return cosh(_Left) * _Right
return (_CSTD _LCosh(_Left, _Right));
}
static short _Exp(_Ty *_Pleft, _Ty _Right, short _Exponent)
{ // compute exp(*_Pleft) * _Right * 2 ^ _Exponent
return (_CSTD _LExp(_Pleft, _Right, _Exponent));
}
static _Ty _Infv(_Ty)
{ // return infinity
return (_CSTD _LInf._Long_double);
}
static bool _Isinf(_Ty _Left)
{ // test for infinity
return (_CSTD _LDtest(&_Left) == _INFCODE);
}
static bool _Isnan(_Ty _Left)
{ // test for NaN
return (_CSTD _LDtest(&_Left) == _NANCODE);
}
static _Ty _Nanv(_Ty)
{ // return NaN
return (_CSTD _LNan._Long_double);
}
static _Ty _Sinh(_Ty _Left, _Ty _Right)
{ // return sinh(_Left) * _Right
return (_CSTD _LSinh(_Left, _Right));
}
static _Ty atan2(_Ty _Yval, _Ty _Xval)
{ // return atan(_Yval / _Xval)
return (_CSTD atan2l(_Yval, _Xval));
}
static _Ty cos(_Ty _Left)
{ // return cos(_Left)
return (_CSTD cosl(_Left));
}
static _Ty exp(_Ty _Left)
{ // return exp(_Left)
return (_CSTD expl(_Left));
}
static _Ty ldexp(_Ty _Left, int _Exponent)
{ // return _Left * 2 ^ _Exponent
return (_CSTD ldexpl(_Left, _Exponent));
}
static _Ty log(_Ty _Left)
{ // return log(_Left)
return (_CSTD logl(_Left));
}
static _Ty pow(_Ty _Left, _Ty _Right)
{ // return _Left ^ _Right
return (_CSTD powl(_Left, _Right));
}
static _Ty sin(_Ty _Left)
{ // return sin(_Left)
return (_CSTD sinl(_Left));
}
static _Ty sqrt(_Ty _Left)
{ // return sqrt(_Left)
return (_CSTD sqrtl(_Left));
}
static _Ty tan(_Ty _Left)
{ // return tan(_Left)
return (_CSTD tanl(_Left));
}
};
// CLASS _Ctraits<double>
template<> class _Ctraits<double>
{ // complex traits for double
public:
typedef double _Ty;
static _Ty _Cosh(_Ty _Left, _Ty _Right)
{ // return cosh(_Left) * _Right
return (_CSTD _Cosh(_Left, _Right));
}
static short _Exp(_Ty *_Pleft, _Ty _Right, short _Exponent)
{ // compute exp(*_Pleft) * _Right * 2 ^ _Exponent
return (_CSTD _Exp(_Pleft, _Right, _Exponent));
}
static _Ty _Infv(_Ty)
{ // return infinity
return (_CSTD _Inf._Double);
}
static bool _Isinf(_Ty _Left)
{ // test for infinity
return (_CSTD _Dtest(&_Left) == _INFCODE);
}
static bool _Isnan(_Ty _Left)
{ // test for NaN
return (_CSTD _Dtest(&_Left) == _NANCODE);
}
static _Ty _Nanv(_Ty)
{ // return NaN
return (_CSTD _Nan._Double);
}
static _Ty _Sinh(_Ty _Left, _Ty _Right)
{ // return sinh(_Left) * _Right
return (_CSTD _Sinh(_Left, _Right));
}
static _Ty atan2(_Ty _Yval, _Ty _Xval)
{ // return atan(_Yval / _Xval)
return (_CSTD atan2(_Yval, _Xval));
}
static _Ty cos(_Ty _Left)
{ // return cos(_Left)
return (_CSTD cos(_Left));
}
static _Ty exp(_Ty _Left)
{ // return exp(_Left)
return (_CSTD exp(_Left));
}
static _Ty ldexp(_Ty _Left, int _Exponent)
{ // return _Left * 2 ^ _Exponent
return (_CSTD ldexp(_Left, _Exponent));
}
static _Ty log(_Ty _Left)
{ // return log(_Left)
return (_CSTD log(_Left));
}
static _Ty pow(_Ty _Left, _Ty _Right)
{ // return _Left ^ _Right
return (_CSTD pow(_Left, _Right));
}
static _Ty sin(_Ty _Left)
{ // return sin(_Left)
return (_CSTD sin(_Left));
}
static _Ty sqrt(_Ty _Left)
{ // return sqrt(_Left)
return (_CSTD sqrt(_Left));
}
static _Ty tan(_Ty _Left)
{ // return tan(_Left)
return (_CSTD tan(_Left));
}
};
// CLASS _Ctraits<float>
template<> class _Ctraits<float>
{ // complex traits for float
public:
typedef float _Ty;
static _Ty _Cosh(_Ty _Left, _Ty _Right)
{ // return cosh(_Left) * _Right
return (_CSTD _FCosh(_Left, _Right));
}
static short _Exp(_Ty *_Pleft, _Ty _Right, short _Exponent)
{ // compute exp(*_Pleft) * _Right * 2 ^ _Exponent
return (_CSTD _FExp(_Pleft, _Right, _Exponent));
}
static _Ty _Infv(_Ty)
{ // return infinity
return (_CSTD _FInf._Float);
}
static bool _Isinf(_Ty _Left)
{ // test for infinity
return (_CSTD _FDtest(&_Left) == _INFCODE);
}
static bool _Isnan(_Ty _Left)
{ // test for NaN
return (_CSTD _FDtest(&_Left) == _NANCODE);
}
static _Ty _Nanv(_Ty)
{ // return NaN
return (_CSTD _FNan._Float);
}
static _Ty _Sinh(_Ty _Left, _Ty _Right)
{ // return sinh(_Left) * _Right
return (_CSTD _FSinh(_Left, _Right));
}
static _Ty atan2(_Ty _Yval, _Ty _Xval)
{ // return atan(_Yval / _Xval)
return (_CSTD atan2f(_Yval, _Xval));
}
static _Ty cos(_Ty _Left)
{ // return cos(_Left)
return (_CSTD cosf(_Left));
}
static _Ty exp(_Ty _Left)
{ // return exp(_Left)
return (_CSTD expf(_Left));
}
static _Ty ldexp(_Ty _Left, int _Exponent)
{ // return _Left * 2 ^ _Exponent
return (_CSTD ldexpf(_Left, _Exponent));
}
static _Ty log(_Ty _Left)
{ // return log(_Left)
return (_CSTD logf(_Left));
}
static _Ty pow(_Ty _Left, _Ty _Right)
{ // return _Left ^ _Right
return (_CSTD powf(_Left, _Right));
}
static _Ty sin(_Ty _Left)
{ // return sin(_Left)
return (_CSTD sinf(_Left));
}
static _Ty sqrt(_Ty _Left)
{ // return sqrt(_Left)
return (_CSTD sqrtf(_Left));
}
static _Ty tan(_Ty _Left)
{ // return tan(_Left)
return (_CSTD tanf(_Left));
}
};
// TEMPLATE CLASS _Complex_value
template<class _Ty>
value struct _Complex_value
{ /* templatized complex value */
// enum {_Re = 0, _Im = 1};
_Ty _real;
_Ty _imag;
};
// TEMPLATE CLASS _Complex_base
template<class _Ty,
class _Valbase>
class _Complex_base
: public _Valbase
{ // base for all complex types
public:
typedef _Ctraits<_Ty> _Myctraits;
typedef _Complex_base<_Ty, _Valbase> _Myt;
typedef _Ty value_type;
_Complex_base(const _Ty& _Realval, const _Ty& _Imagval)
{ // construct from components of same type
this->_real = _Realval;
this->_imag = _Imagval;
}
_Ty real(const _Ty& _Right)
{ // set real component
return (this->_real = _Right);
}
_Ty imag(const _Ty& _Right)
{ // set imaginary component
return (this->_imag = _Right);
}
_Ty real() const
{ // return real component
return (this->_real);
}
_Ty imag() const
{ // return imaginary component
return (this->_imag);
}
protected:
template<class _Other> inline
void _Add(const complex<_Other>& _Right)
{ // add other complex
this->_real = this->_real + (_Ty)_Right.real();
this->_imag = this->_imag + (_Ty)_Right.imag();
}
template<class _Other> inline
void _Sub(const complex<_Other>& _Right)
{ // subtract other complex
this->_real = this->_real - (_Ty)_Right.real();
this->_imag = this->_imag - (_Ty)_Right.imag();
}
template<class _Other> inline
void _Mul(const complex<_Other>& _Right)
{ // multiply by other complex
_Ty _Rightreal = (_Ty)_Right.real();
_Ty _Rightimag = (_Ty)_Right.imag();
_Ty _Tmp = this->_real * _Rightreal
- this->_imag * _Rightimag;
this->_imag = this->_real * _Rightimag
+ this->_imag * _Rightreal;
this->_real = _Tmp;
}
template<class _Other> inline
void _Div(const complex<_Other>& _Right)
{ // divide by other complex
typedef _Ctraits<_Ty> _Myctraits;
_Ty _Rightreal = (_Ty)_Right.real();
_Ty _Rightimag = (_Ty)_Right.imag();
if (_Myctraits::_Isnan(_Rightreal) || _Myctraits::_Isnan(_Rightimag))
{ // set NaN result
this->_real = _Myctraits::_Nanv(_Rightreal);
this->_imag = this->_real;
}
else if ((_Rightimag < 0 ? -_Rightimag : +_Rightimag)
< (_Rightreal < 0 ? -_Rightreal : +_Rightreal))
{ // |_Right.imag()| < |_Right.real()|
_Ty _Wr = _Rightimag / _Rightreal;
_Ty _Wd = _Rightreal + _Wr * _Rightimag;
if (_Myctraits::_Isnan(_Wd) || _Wd == 0)
{ // set NaN result
this->_real = _Myctraits::_Nanv(_Rightreal);
this->_imag = this->_real;
}
else
{ // compute representable result
_Ty _Tmp = (this->_real
+ this->_imag * _Wr) / _Wd;
this->_imag = (this->_imag
- this->_real * _Wr) / _Wd;
this->_real = _Tmp;
}
}
else if (_Rightimag == 0)
{ // set NaN result
this->_real = _Myctraits::_Nanv(_Rightreal);
this->_imag = this->_real;
}
else
{ // 0 < |_Right.real()| <= |_Right.imag()|
_Ty _Wr = _Rightreal / _Rightimag;
_Ty _Wd = _Rightimag + _Wr * _Rightreal;
if (_Myctraits::_Isnan(_Wd) || _Wd == 0)
{ // set NaN result
this->_real = _Myctraits::_Nanv(_Rightreal);
this->_imag = this->_real;
}
else
{ // compute representable result
_Ty _Tmp = (this->_real * _Wr + this->_imag) / _Wd;
this->_imag = (this->_imag * _Wr
- this->_real) / _Wd;
this->_real = _Tmp;
}
}
}
};
// CLASS complex<float>
template<> class complex<float>
: public _Complex_base<float, _Fcomplex_value>
{ // complex with float components
public:
typedef float _Ty;
typedef complex<_Ty> _Myt;
explicit complex(
const complex<double>&); // defined below
explicit complex(
const complex<long double>&); // defined below
complex(const _Ty& _Realval = 0,
const _Ty& _Imagval = 0)
: _Complex_base<float, _Fcomplex_value>(_Realval, _Imagval)
{ // construct from float components
}
complex(const _Fcomplex_value& _Right)
: _Complex_base<float, _Fcomplex_value>(_Right._real,
_Right._imag)
{ // construct from float complex value
}
complex(const _Dcomplex_value& _Right)
: _Complex_base<float, _Fcomplex_value>(_Right._real,
_Right._imag)
{ // construct from double complex value
}
complex(const _Lcomplex_value& _Right)
: _Complex_base<float, _Fcomplex_value>(_Right._real,
_Right._imag)
{ // construct from long double complex value
}
complex<_Ty>& operator=(const _Ty& _Right)
{ // assign real
_real = _Right;
_imag = 0;
return (*this);
}
_Myt& operator+=(const _Ty& _Right)
{ // add real
_real = _real + _Right;
return (*this);
}
_Myt& operator-=(const _Ty& _Right)
{ // subtract real
_real = _real - _Right;
return (*this);
}
_Myt& operator*=(const _Ty& _Right)
{ // multiply by real
_real = _real * _Right;
_imag = _imag * _Right;
return (*this);
}
_Myt& operator/=(const _Ty& _Right)
{ // divide by real
_real = _real / _Right;
_imag = _imag / _Right;
return (*this);
}
_Myt& operator+=(const _Myt& _Right)
{ // add other complex
this->_Add(_Right);
return (*this);
}
_Myt& operator-=(const _Myt& _Right)
{ // subtract other complex
this->_Sub(_Right);
return (*this);
}
_Myt& operator*=(const _Myt& _Right)
{ // multiply by other complex
this->_Mul(_Right);
return (*this);
}
_Myt& operator/=(const _Myt& _Right)
{ // divide by other complex
this->_Div(_Right);
return (*this);
}
template<class _Other> inline
_Myt& operator=(const complex<_Other>& _Right)
{ // assign another complex
_real = (_Ty)_Right._real;
_imag = (_Ty)_Right._imag;
return (*this);
}
template<class _Other> inline
_Myt& operator+=(const complex<_Other>& _Right)
{ // add other complex
this->_Add(_Right);
return (*this);
}
template<class _Other> inline
_Myt& operator-=(const complex<_Other>& _Right)
{ // subtract other complex
this->_Sub(_Right);
return (*this);
}
template<class _Other> inline
_Myt& operator*=(const complex<_Other>& _Right)
{ // multiply by other complex
this->_Mul(_Right);
return (*this);
}
template<class _Other> inline
_Myt& operator/=(const complex<_Other>& _Right)
{ // divide by other complex
this->_Div(_Right);
return (*this);
}
};
// CLASS complex<double>
template<> class complex<double>
: public _Complex_base<double, _Dcomplex_value>
{ // complex with double components
public:
typedef double _Ty;
typedef complex<_Ty> _Myt;
complex(
const complex<float>&); // defined below
explicit complex(
const complex<long double>&); // defined below
complex(const _Ty& _Realval = 0,
const _Ty& _Imagval = 0)
: _Complex_base<double, _Dcomplex_value>(_Realval, _Imagval)
{ // construct from double components
}
complex(const _Dcomplex_value& _Right)
: _Complex_base<double, _Dcomplex_value>(_Right._real,
_Right._imag)
{ // construct from double complex value
}
complex(const _Lcomplex_value& _Right)
: _Complex_base<double, _Dcomplex_value>(_Right._real,
_Right._imag)
{ // construct from long double complex value
}
complex<_Ty>& operator=(const _Ty& _Right)
{ // assign real
_real = _Right;
_imag = 0;
return (*this);
}
_Myt& operator+=(const _Ty& _Right)
{ // add real
_real = _real + _Right;
return (*this);
}
_Myt& operator-=(const _Ty& _Right)
{ // subtract real
_real = _real - _Right;
return (*this);
}
_Myt& operator*=(const _Ty& _Right)
{ // multiply by real
_real = _real * _Right;
_imag = _imag * _Right;
return (*this);
}
_Myt& operator/=(const _Ty& _Right)
{ // divide by real
_real = _real / _Right;
_imag = _imag / _Right;
return (*this);
}
_Myt& operator+=(const _Myt& _Right)
{ // add other complex
this->_Add(_Right);
return (*this);
}
_Myt& operator-=(const _Myt& _Right)
{ // subtract other complex
this->_Sub(_Right);
return (*this);
}
_Myt& operator*=(const _Myt& _Right)
{ // multiply by other complex
this->_Mul(_Right);
return (*this);
}
_Myt& operator/=(const _Myt& _Right)
{ // divide by other complex
this->_Div(_Right);
return (*this);
}
template<class _Other> inline
_Myt& operator=(const complex<_Other>& _Right)
{ // assign another complex
_real = (_Ty)_Right._real;
_imag = (_Ty)_Right._imag;
return (*this);
}
template<class _Other> inline
_Myt& operator+=(const complex<_Other>& _Right)
{ // add other complex
this->_Add(_Right);
return (*this);
}
template<class _Other> inline
_Myt& operator-=(const complex<_Other>& _Right)
{ // subtract other complex
this->_Sub(_Right);
return (*this);
}
template<class _Other> inline
_Myt& operator*=(const complex<_Other>& _Right)
{ // multiply by other complex
this->_Mul(_Right);
return (*this);
}
template<class _Other> inline
_Myt& operator/=(const complex<_Other>& _Right)
{ // divide by other complex
this->_Div(_Right);
return (*this);
}
};
// CLASS complex<long double>
template<> class complex<long double>
: public _Complex_base<long double, _Lcomplex_value>
{ // complex with long double components
public:
typedef long double _Ty;
typedef complex<_Ty> _Myt;
complex(
const complex<float>&); // defined below
complex(
const complex<double>&); // defined below
complex(const _Ty& _Realval = 0,
const _Ty& _Imagval = 0)
: _Complex_base<long double, _Lcomplex_value>(_Realval, _Imagval)
{ // construct from long double components
}
complex(const _Lcomplex_value& _Right)
: _Complex_base<long double, _Lcomplex_value>(_Right._real,
_Right._imag)
{ // construct from long double complex value
}
complex<_Ty>& operator=(const _Ty& _Right)
{ // assign real
_real = _Right;
_imag = 0;
return (*this);
}
_Myt& operator+=(const _Ty& _Right)
{ // add real
_real = _real + _Right;
return (*this);
}
_Myt& operator-=(const _Ty& _Right)
{ // subtract real
_real = _real - _Right;
return (*this);
}
_Myt& operator*=(const _Ty& _Right)
{ // multiply by real
_real = _real * _Right;
_imag = _imag * _Right;
return (*this);
}
_Myt& operator/=(const _Ty& _Right)
{ // divide by real
_real = _real / _Right;
_imag = _imag / _Right;
return (*this);
}
_Myt& operator+=(const _Myt& _Right)
{ // add other complex
this->_Add(_Right);
return (*this);
}
_Myt& operator-=(const _Myt& _Right)
{ // subtract other complex
this->_Sub(_Right);
return (*this);
}
_Myt& operator*=(const _Myt& _Right)
{ // multiply by other complex
this->_Mul(_Right);
return (*this);
}
_Myt& operator/=(const _Myt& _Right)
{ // divide by other complex
this->_Div(_Right);
return (*this);
}
template<class _Other> inline
_Myt& operator=(const complex<_Other>& _Right)
{ // assign another complex
_real = (_Ty)_Right._real;
_imag = (_Ty)_Right._imag;
return (*this);
}
template<class _Other> inline
_Myt& operator+=(const complex<_Other>& _Right)
{ // add other complex
this->_Add(_Right);
return (*this);
}
template<class _Other> inline
_Myt& operator-=(const complex<_Other>& _Right)
{ // subtract other complex
this->_Sub(_Right);
return (*this);
}
template<class _Other> inline
_Myt& operator*=(const complex<_Other>& _Right)
{ // multiply by other complex
this->_Mul(_Right);
return (*this);
}
template<class _Other> inline
_Myt& operator/=(const complex<_Other>& _Right)
{ // divide by other complex
this->_Div(_Right);
return (*this);
}
};
// CONSTRUCTORS FOR complex SPECIALIZATIONS
inline complex<float>::complex(
const complex<double>& _Right)
: _Complex_base<float, _Fcomplex_value>(
(_Ty)_Right.real(), (_Ty)_Right.imag())
{ // construct complex<float> from complex<double>
}
inline complex<float>::complex(
const complex<long double>& _Right)
: _Complex_base<float, _Fcomplex_value>(
(_Ty)_Right.real(), (_Ty)_Right.imag())
{ // construct complex<float> from complex<long double>
}
inline complex<double>::complex(
const complex<float>& _Right)
: _Complex_base<double, _Dcomplex_value>(
(_Ty)_Right.real(), (_Ty)_Right.imag())
{ // construct complex<double> from complex<float>
}
inline complex<double>::complex(
const complex<long double>& _Right)
: _Complex_base<double, _Dcomplex_value>(
(_Ty)_Right.real(), (_Ty)_Right.imag())
{ // construct complex<double> from complex<long double>
}
inline complex<long double>::complex(
const complex<float>& _Right)
: _Complex_base<long double, _Lcomplex_value>(
(_Ty)_Right.real(), (_Ty)_Right.imag())
{ // construct complex<long double> from complex<float>
}
inline complex<long double>::complex(
const complex<double>& _Right)
: _Complex_base<long double, _Lcomplex_value>(
(_Ty)_Right.real(), (_Ty)_Right.imag())
{ // construct complex<long double> from complex<double>
}
// TEMPLATE CLASS complex
template<class _Ty>
class complex
: public _Complex_base<_Ty, _Complex_value<_Ty> >
{ // complex with _Ty components
public:
typedef complex<_Ty> _Myt;
typedef _Complex_base<_Ty, _Complex_value<_Ty> > _Mybase;
complex(const _Ty& _Realval = _Ty(), const _Ty& _Imagval = _Ty())
: _Mybase(_Realval, _Imagval)
{ // construct from components of same type
}
_Myt& operator=(const _Ty& _Right)
{ // assign real
this->_real = _Right;
this->_imag = _Ty();
return (*this);
}
template<class _Other>
complex(const complex<_Other>& _Right)
: _Mybase((_Ty)_Right.real(), (_Ty)_Right.imag())
{ // construct from other complex type
}
template<class _Other>
_Myt& operator=(const complex<_Other>& _Right)
{ // assign other complex type
this->_real = (_Ty)_Right.real();
this->_imag = (_Ty)_Right.imag();
return (*this);
}
_Myt& operator+=(const _Ty& _Right)
{ // add real
this->_real = this->_real + _Right;
return (*this);
}