GremlinEq
complex_ops.h
1 //---------------------------------------------------------------------------
2 //
3 // $Id: complex_ops.h,v 1.2 2014/08/01 17:17:18 sahughes Exp $
4 //
5 //---------------------------------------------------------------------------
6 
7 /* complex_ops.h
8  * add some more operations on complex numbers
9  */
10 
11 #ifndef COMPLEX_OPS_H_SEEN
12 #define COMPLEX_OPS_H_SEEN
13 
14 #include <complex>
15 namespace std {
16 
17  /* math ops with real types (or anything convertable to a float) */
18 
19  /* general cases */
20 
21  template<typename _Tp, typename _S>
22  inline complex<_Tp>
23  operator+(const complex<_Tp>& __x, const _S& __y) {
24  complex<_Tp> __r = __x;
25  __r += __y;
26  return __r;
27  }
28 
29  template<typename _Tp, typename _S>
30  inline complex<_Tp>
31  operator+(const _S& __x, const complex<_Tp>& __y) {
32  complex<_Tp> __r = __y;
33  __r += __x;
34  return __r;
35  }
36 
37 
38  template<typename _Tp, typename _S>
39  inline complex<_Tp>
40  operator*(const complex<_Tp>& __x, const _S& __y) {
41  complex<_Tp> __r = __x;
42  __r *= __y;
43  return __r;
44  }
45 
46  template<typename _Tp, typename _S>
47  inline complex<_Tp>
48  operator*(const _S& __x, const complex<_Tp>& __y) {
49  complex<_Tp> __r = __y;
50  __r *= __x;
51  return __r;
52  }
53 
54 
55  template<typename _Tp, typename _S>
56  inline complex<_Tp>
57  operator-(const complex<_Tp>& __x, const _S& __y) {
58  complex<_Tp> __r = __x;
59  __r -= __y;
60  return __r;
61  }
62 
63  /* Edited by SAH 1 Aug 2014, new compilers barf on old code */
64  template<typename _Tp, typename _S>
65  inline complex<_Tp>
66  operator-(const _S& __x, const complex<_Tp>& __y) {
67  complex<_Tp> __r(__x - __y.real(), - __y.imag());
68  /* complex<_Tp> __r(__x, - __y.imag()); */
69  /* __r.real() -= __y.real(); */
70  return __r;
71  }
72 
73 
74  template<typename _Tp, typename _S>
75  inline complex<_Tp>
76  operator/(const complex<_Tp>& __x, const _S& __y) {
77  complex<_Tp> __r = __x;
78  __r /= __y;
79  return __r;
80  }
81 
82  template<typename _Tp, typename _S>
83  inline complex<_Tp>
84  operator/(const _S& __x, const complex<_Tp>& __y) {
85  complex<_Tp> __r = __x;
86  __r /= __y;
87  return __r;
88  }
89 
90  /* special cases for operations between two complexes of different widths
91  * (only double + long double implemented) */
92 
93  inline complex<long double>
94  operator+(const complex<long double>& __x, const complex<double>& __y) {
95  complex<long double> __r = __x;
96  __r += __y;
97  return __r;
98  }
99  inline complex<long double>
100  operator+(const complex<double>& __x, const complex<long double>& __y) {
101  complex<long double> __r = __y;
102  __r += __x;
103  return __r;
104  }
105 
106  inline complex<long double>
107  operator-(const complex<long double>& __x, const complex<double>& __y) {
108  complex<long double> __r = __x;
109  __r -= __y;
110  return __r;
111  }
112  inline complex<long double>
113  operator-(const complex<double>& __x, const complex<long double>& __y) {
114  complex<long double> __r = - __y;
115  __r += __x;
116  return __r;
117  }
118 
119  inline complex<long double>
120  operator*(const complex<long double>& __x, const complex<double>& __y) {
121  complex<long double> __r = __x;
122  __r *= __y;
123  return __r;
124  }
125  inline complex<long double>
126  operator*(const complex<double>& __x, const complex<long double>& __y) {
127  complex<long double> __r = __y;
128  __r *= __x;
129  return __r;
130  }
131 
132  inline complex<long double>
133  operator/(const complex<long double>& __x, const complex<double>& __y) {
134  complex<long double> __r = __x;
135  __r /= __y;
136  return __r;
137  }
138  inline complex<long double>
139  operator/(const complex<double>& __x, const complex<long double>& __y) {
140  complex<long double> __r = 1/__y;
141  __r *= __x;
142  return __r;
143  }
144 }
145 
146 #endif /* !COMPLEX_OPS_H_SEEN */
Definition: complex_ops.h:15