/elec/propeller-clock

To get this branch, use:
bzr branch http://bzr.ed.am/elec/propeller-clock

« back to all changes in this revision

Viewing changes to src/utility/complex

  • Committer: edam
  • Date: 2012-02-25 01:31:43 UTC
  • Revision ID: tim@ed.am-20120225013143-9fet2y2d3fjlrwez
added ulibc

Show diffs side-by-side

added added

removed removed

 
1
/*      Copyright (C) 2004 Garrett A. Kajmowicz
 
2
 
 
3
        This file is part of the uClibc++ Library.
 
4
 
 
5
        This library is free software; you can redistribute it and/or
 
6
        modify it under the terms of the GNU Lesser General Public
 
7
        License as published by the Free Software Foundation; either
 
8
        version 2.1 of the License, or (at your option) any later version.
 
9
 
 
10
        This library is distributed in the hope that it will be useful,
 
11
        but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
        Lesser General Public License for more details.
 
14
 
 
15
        You should have received a copy of the GNU Lesser General Public
 
16
        License along with this library; if not, write to the Free Software
 
17
        Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
*/
 
19
 
 
20
#include <istream>
 
21
#include <ostream>
 
22
 
 
23
#ifndef __STD_HEADER_COMPLEX
 
24
#define __STD_HEADER_COMPLEX 1
 
25
 
 
26
 
 
27
namespace std {
 
28
//      class complex<float>;
 
29
//      class complex<double>;
 
30
//      class complex<long double>;
 
31
 
 
32
        template<class T> class _UCXXEXPORT complex{
 
33
        public:
 
34
                typedef T value_type;
 
35
 
 
36
                complex(const T& re = T(), const T& im = T()) : r(re), i(im) {  }
 
37
                complex(const complex& c): r(c.r), i(c.i){  }
 
38
                template<class X> complex(const complex<X>& c): r(c.r), i(c.i){  }
 
39
 
 
40
                inline T real() const{
 
41
                        return r;
 
42
                }
 
43
                inline T imag() const{
 
44
                        return i;
 
45
                }
 
46
 
 
47
                complex<T>& operator= (const T& v){
 
48
                        r = v;
 
49
                        i = 0;
 
50
                        return *this;
 
51
                }
 
52
                complex<T>& operator+=(const T& v){
 
53
                        r +=v;
 
54
                        return *this;
 
55
                }
 
56
                complex<T>& operator-=(const T& v){
 
57
                        r -=v;
 
58
                        return *this;
 
59
                }
 
60
                complex<T>& operator*=(const T& v){
 
61
                        r*=v;
 
62
                        i*=v;
 
63
                        return *this;
 
64
                }
 
65
                complex<T>& operator/=(const T& v){
 
66
                        r/=v;
 
67
                        i/=v;
 
68
                        return *this;
 
69
                }
 
70
                complex& operator=(const complex& v){
 
71
                        if(&v != this){
 
72
                                r = v.r;
 
73
                                i = v.i;
 
74
                        }
 
75
                        return *this;
 
76
                }
 
77
                template<class X> complex<T>& operator= (const complex<X>& v){
 
78
                        r = v.r;
 
79
                        i = v.i;
 
80
                        return *this;
 
81
                }
 
82
                template<class X> complex<T>& operator+=(const complex<X>& v){
 
83
                        r+=v.r;
 
84
                        i+=v.i;
 
85
                        return *this;
 
86
                }
 
87
                template<class X> complex<T>& operator-=(const complex<X>& v){
 
88
                        r-=v.r;
 
89
                        i-=v.i;
 
90
                        return *this;
 
91
                }
 
92
                template<class X> complex<T>& operator*=(const complex<X>& v){
 
93
                        T tempr = r*v.r - i*v.i;
 
94
                        T tempi = r*v.i + i*v.r;
 
95
                        r = tempr;
 
96
                        i = tempi;
 
97
                        return *this;
 
98
                }
 
99
                template<class X> complex<T>& operator/=(const complex<X>& v){
 
100
                        T tempr = (r*v.r + i*v.i) / (v.r*v.r + v.i*v.i);
 
101
                        T tempi = (i*v.r - r*v.i) / (v.r*v.r + v.i*v.i);
 
102
                        r = tempr;
 
103
                        i = tempi;
 
104
                        return *this;
 
105
                }
 
106
        private:
 
107
                T r;
 
108
                T i;
 
109
        };
 
110
 
 
111
        template<class T> _UCXXEXPORT complex<T> operator+(const complex<T>& ls, const complex<T>& rs){
 
112
                complex<T> retval(ls);
 
113
                retval += rs;
 
114
                return retval;
 
115
        }
 
116
        template<class T> _UCXXEXPORT complex<T> operator+(const complex<T>& ls, const T& rs){
 
117
                complex<T> retval(ls);
 
118
                ls += rs;
 
119
                return retval;
 
120
        }
 
121
        template<class T> _UCXXEXPORT inline complex<T> operator+(const T& ls, const complex<T>& rs){
 
122
                return rs + ls;
 
123
        }
 
124
        template<class T> _UCXXEXPORT complex<T> operator-(const complex<T>& ls, const complex<T>& rs){
 
125
                complex<T> retval(ls);
 
126
                retval -= rs;
 
127
                return retval;
 
128
        }
 
129
        template<class T> _UCXXEXPORT complex<T> operator-(const complex<T>& ls, const T& rs){
 
130
                complex<T> retval(ls);
 
131
                retval -= rs;
 
132
                return retval;
 
133
        }
 
134
        template<class T> _UCXXEXPORT complex<T> operator-(const T& ls, const complex<T>& rs){
 
135
                complex<T> retval(ls);
 
136
                retval -= rs;
 
137
                return retval;
 
138
        }
 
139
        template<class T> _UCXXEXPORT complex<T> operator*(const complex<T>& ls, const complex<T>& rs){
 
140
                complex<T> retval(ls);
 
141
                retval *= rs;
 
142
                return retval;
 
143
        }
 
144
        template<class T> _UCXXEXPORT complex<T> operator*(const complex<T>& ls, const T& rs){
 
145
                complex<T> retval(ls);
 
146
                retval *= rs;
 
147
                return retval;
 
148
        }
 
149
        template<class T> _UCXXEXPORT complex<T> operator*(const T& ls, const complex<T>& rs){
 
150
                complex<T> retval(ls);
 
151
                retval *=rs;
 
152
                return retval;
 
153
        }
 
154
        template<class T> _UCXXEXPORT complex<T> operator/(const complex<T>& ls, const complex<T>& rs){
 
155
                complex<T> retval(ls);
 
156
                retval/=rs;
 
157
                return retval;
 
158
        }
 
159
        template<class T> _UCXXEXPORT complex<T> operator/(const complex<T>& ls, const T& rs){
 
160
                complex<T> retval(ls);
 
161
                retval/=rs;
 
162
                return retval;
 
163
        }
 
164
        template<class T> _UCXXEXPORT complex<T> operator/(const T& ls, const complex<T>& rs){
 
165
                complex<T> retval(ls);
 
166
                retval/=rs;
 
167
                return retval;
 
168
        }
 
169
        template<class T> _UCXXEXPORT complex<T> operator+(const complex<T>& v){
 
170
                return v;
 
171
        }
 
172
        template<class T> _UCXXEXPORT complex<T> operator-(const complex<T>& v){
 
173
                return complex<T> (-v.real(), -v.imag());
 
174
        }
 
175
        template<class T> _UCXXEXPORT bool operator==(const complex<T>& ls, const complex<T>& rs){
 
176
                if( ls.real() == rs.real() && ls.imag() == rs.image()){
 
177
                        return true;
 
178
                }
 
179
                return false;
 
180
        }
 
181
        template<class T> _UCXXEXPORT bool operator==(const complex<T>& ls, const T& rs){
 
182
                if(ls.real() == rs && ls.imag() == T()){
 
183
                        return true;
 
184
                }
 
185
                return false;
 
186
        }
 
187
        template<class T> _UCXXEXPORT bool operator==(const T& ls, const complex<T>& rs){
 
188
                if(ls == rs.real() && rs.imag() == T()){
 
189
                        return true;
 
190
                }
 
191
                return false;
 
192
        }
 
193
        template<class T> _UCXXEXPORT bool operator!=(const complex<T>& ls, const complex<T>& rs){
 
194
                if(ls == rs){
 
195
                        return false;
 
196
                }
 
197
                return true;
 
198
        }
 
199
        template<class T> _UCXXEXPORT bool operator!=(const complex<T>& ls, const T& rs){
 
200
                if(ls == rs){
 
201
                        return false;
 
202
                }
 
203
                return true;
 
204
        }
 
205
        template<class T> _UCXXEXPORT bool operator!=(const T& ls, const complex<T>& rs){
 
206
                if(ls == rs){
 
207
                        return false;
 
208
                }
 
209
                return true;
 
210
        }
 
211
        template<class T, class charT, class traits> _UCXXEXPORT basic_istream<charT, traits>&
 
212
                 operator>>(basic_istream<charT, traits>& is, complex<T>& v)
 
213
        {
 
214
                T tempr;
 
215
                T tempi;
 
216
                is >> tempr;
 
217
                is.get();
 
218
                is >> tempi;
 
219
                v = complex<T>(tempr, tempi);
 
220
                return is;
 
221
        }
 
222
 
 
223
        template<class T, class charT, class traits> _UCXXEXPORT basic_ostream<charT, traits>&
 
224
                 operator<<(basic_ostream<charT, traits>& os, const complex<T>&v)
 
225
        {
 
226
                os << v.real() << ", " << v.imag();
 
227
                return os;
 
228
        }
 
229
 
 
230
        template<class T> _UCXXEXPORT T real(const complex<T>& v){
 
231
                return v.real();
 
232
        }
 
233
 
 
234
        template<class T> _UCXXEXPORT T imag(const complex<T>& v){
 
235
                return v.imag();
 
236
        }
 
237
 
 
238
        template<class T> _UCXXEXPORT T norm(const complex<T>& v){
 
239
                return (v.real() * v.real() + v.imag() * v.imag());
 
240
        }
 
241
 
 
242
        template<class T> _UCXXEXPORT complex<T> conj(const complex<T>& v){
 
243
                return complex<T>(v.real(), -v.imag());
 
244
        }
 
245
 
 
246
#ifdef __UCLIBCXX_SUPPORT_MATH__        //Can we link with libm?
 
247
 
 
248
        template<class T> _UCXXEXPORT T abs(const complex<T>& v){
 
249
                return sqrt(v.real() * v.real() + v.imag() * v.imag());
 
250
        }
 
251
 
 
252
        template<class T> _UCXXEXPORT T arg(const complex<T>& v){
 
253
                return atan2(v.imag(), v.real());
 
254
        }
 
255
 
 
256
        template<class T> _UCXXEXPORT complex<T> polar(const T& rho, const T& theta){
 
257
                return complex<T>(rho * cos(theta), rho * sin(theta));
 
258
        }
 
259
 
 
260
        template<class T> _UCXXEXPORT complex<T> cos  (const complex<T>& v){
 
261
                return complex<T>(cos(v.real()) * cosh(v.imag()), -sin(v.real()) * sinh(v.imag()));
 
262
        }
 
263
 
 
264
        template<class T> _UCXXEXPORT complex<T> cosh (const complex<T>& v){
 
265
                return complex<T>(cosh(v.real()) * cos(v.imag()), sinh(v.real()) * sin(v.imag()));
 
266
        }
 
267
 
 
268
        template<class T> _UCXXEXPORT complex<T> exp  (const complex<T>& v){
 
269
                return polar(exp(v.real()), v.imag());
 
270
        }
 
271
 
 
272
        template<class T> _UCXXEXPORT complex<T> log  (const complex<T>& v){
 
273
                return complex<T>(log(abs(v)), arg(v));
 
274
        }
 
275
 
 
276
        template<class T> _UCXXEXPORT complex<T> log10(const complex<T>& v){
 
277
                return (log(v) / log(T(10.0)));
 
278
        }
 
279
 
 
280
        template<class T> _UCXXEXPORT complex<T> pow(const complex<T>& v, int p){
 
281
                T rho = pow(abs(v), p);
 
282
                T theta = arg(v);
 
283
                return complex<T>(rho * cos(p * theta), rho * sin(p * theta) );
 
284
        }
 
285
 
 
286
        template<class T> _UCXXEXPORT complex<T> pow(const complex<T>& v, const T& p){
 
287
                return polar( pow(abs(v),p), arg(v)*p );
 
288
        }
 
289
 
 
290
        template<class T> _UCXXEXPORT complex<T> pow(const complex<T>& v, const complex<T>& p){
 
291
                if(v == T()){
 
292
                        //We are using "0" as the value
 
293
                        return T();
 
294
                }
 
295
                return exp(p * log(v));
 
296
        }
 
297
 
 
298
        template<class T> _UCXXEXPORT complex<T> pow(const T& v, const complex<T>& p){
 
299
                if(v == T()){
 
300
                        return T();
 
301
                }
 
302
                return polar(pow(v,p.real()), y.imag() * log(x) );
 
303
        }
 
304
 
 
305
        template<class T> _UCXXEXPORT complex<T> sin  (const complex<T>& v){
 
306
                return complex<T>(sin(v.real()) * cosh(v.imag()), cosh(v.real()) * sin(v.imag()));
 
307
        }
 
308
 
 
309
        template<class T> _UCXXEXPORT complex<T> sinh (const complex<T>& v){
 
310
                return complext<T>(sinh(v.real()) * cos(v.imag()), cosh(v.real()) * sin(v.imag()) );
 
311
        }
 
312
 
 
313
        template<class T> _UCXXEXPORT complex<T> sqrt (const complex<T>&);
 
314
        template<class T> _UCXXEXPORT complex<T> tan  (const complex<T>& v){
 
315
                return sin(v) / cos(v);
 
316
        }
 
317
 
 
318
        template<class T> _UCXXEXPORT complex<T> tanh (const complex<T>& v){
 
319
                return sinh(v) / cosh(v);
 
320
        }
 
321
 
 
322
#endif
 
323
 
 
324
}
 
325
 
 
326
#endif
 
327