1
 
/*      Copyright (C) 2004 Garrett A. Kajmowicz
 
3
 
        This file is part of the uClibc++ Library.
 
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.
 
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.
 
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
 
23
 
#ifndef __STD_HEADER_COMPLEX
 
24
 
#define __STD_HEADER_COMPLEX 1
 
28
 
//      class complex<float>;
 
29
 
//      class complex<double>;
 
30
 
//      class complex<long double>;
 
32
 
        template<class T> class _UCXXEXPORT complex{
 
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){  }
 
40
 
                inline T real() const{
 
43
 
                inline T imag() const{
 
47
 
                complex<T>& operator= (const T& v){
 
52
 
                complex<T>& operator+=(const T& v){
 
56
 
                complex<T>& operator-=(const T& v){
 
60
 
                complex<T>& operator*=(const T& v){
 
65
 
                complex<T>& operator/=(const T& v){
 
70
 
                complex& operator=(const complex& v){
 
77
 
                template<class X> complex<T>& operator= (const complex<X>& v){
 
82
 
                template<class X> complex<T>& operator+=(const complex<X>& v){
 
87
 
                template<class X> complex<T>& operator-=(const complex<X>& v){
 
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;
 
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);
 
111
 
        template<class T> _UCXXEXPORT complex<T> operator+(const complex<T>& ls, const complex<T>& rs){
 
112
 
                complex<T> retval(ls);
 
116
 
        template<class T> _UCXXEXPORT complex<T> operator+(const complex<T>& ls, const T& rs){
 
117
 
                complex<T> retval(ls);
 
121
 
        template<class T> _UCXXEXPORT inline complex<T> operator+(const T& ls, const complex<T>& rs){
 
124
 
        template<class T> _UCXXEXPORT complex<T> operator-(const complex<T>& ls, const complex<T>& rs){
 
125
 
                complex<T> retval(ls);
 
129
 
        template<class T> _UCXXEXPORT complex<T> operator-(const complex<T>& ls, const T& rs){
 
130
 
                complex<T> retval(ls);
 
134
 
        template<class T> _UCXXEXPORT complex<T> operator-(const T& ls, const complex<T>& rs){
 
135
 
                complex<T> retval(ls);
 
139
 
        template<class T> _UCXXEXPORT complex<T> operator*(const complex<T>& ls, const complex<T>& rs){
 
140
 
                complex<T> retval(ls);
 
144
 
        template<class T> _UCXXEXPORT complex<T> operator*(const complex<T>& ls, const T& rs){
 
145
 
                complex<T> retval(ls);
 
149
 
        template<class T> _UCXXEXPORT complex<T> operator*(const T& ls, const complex<T>& rs){
 
150
 
                complex<T> retval(ls);
 
154
 
        template<class T> _UCXXEXPORT complex<T> operator/(const complex<T>& ls, const complex<T>& rs){
 
155
 
                complex<T> retval(ls);
 
159
 
        template<class T> _UCXXEXPORT complex<T> operator/(const complex<T>& ls, const T& rs){
 
160
 
                complex<T> retval(ls);
 
164
 
        template<class T> _UCXXEXPORT complex<T> operator/(const T& ls, const complex<T>& rs){
 
165
 
                complex<T> retval(ls);
 
169
 
        template<class T> _UCXXEXPORT complex<T> operator+(const complex<T>& v){
 
172
 
        template<class T> _UCXXEXPORT complex<T> operator-(const complex<T>& v){
 
173
 
                return complex<T> (-v.real(), -v.imag());
 
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()){
 
181
 
        template<class T> _UCXXEXPORT bool operator==(const complex<T>& ls, const T& rs){
 
182
 
                if(ls.real() == rs && ls.imag() == T()){
 
187
 
        template<class T> _UCXXEXPORT bool operator==(const T& ls, const complex<T>& rs){
 
188
 
                if(ls == rs.real() && rs.imag() == T()){
 
193
 
        template<class T> _UCXXEXPORT bool operator!=(const complex<T>& ls, const complex<T>& rs){
 
199
 
        template<class T> _UCXXEXPORT bool operator!=(const complex<T>& ls, const T& rs){
 
205
 
        template<class T> _UCXXEXPORT bool operator!=(const T& ls, const complex<T>& rs){
 
211
 
        template<class T, class charT, class traits> _UCXXEXPORT basic_istream<charT, traits>&
 
212
 
                 operator>>(basic_istream<charT, traits>& is, complex<T>& v)
 
219
 
                v = complex<T>(tempr, tempi);
 
223
 
        template<class T, class charT, class traits> _UCXXEXPORT basic_ostream<charT, traits>&
 
224
 
                 operator<<(basic_ostream<charT, traits>& os, const complex<T>&v)
 
226
 
                os << v.real() << ", " << v.imag();
 
230
 
        template<class T> _UCXXEXPORT T real(const complex<T>& v){
 
234
 
        template<class T> _UCXXEXPORT T imag(const complex<T>& v){
 
238
 
        template<class T> _UCXXEXPORT T norm(const complex<T>& v){
 
239
 
                return (v.real() * v.real() + v.imag() * v.imag());
 
242
 
        template<class T> _UCXXEXPORT complex<T> conj(const complex<T>& v){
 
243
 
                return complex<T>(v.real(), -v.imag());
 
246
 
#ifdef __UCLIBCXX_SUPPORT_MATH__        //Can we link with libm?
 
248
 
        template<class T> _UCXXEXPORT T abs(const complex<T>& v){
 
249
 
                return sqrt(v.real() * v.real() + v.imag() * v.imag());
 
252
 
        template<class T> _UCXXEXPORT T arg(const complex<T>& v){
 
253
 
                return atan2(v.imag(), v.real());
 
256
 
        template<class T> _UCXXEXPORT complex<T> polar(const T& rho, const T& theta){
 
257
 
                return complex<T>(rho * cos(theta), rho * sin(theta));
 
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()));
 
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()));
 
268
 
        template<class T> _UCXXEXPORT complex<T> exp  (const complex<T>& v){
 
269
 
                return polar(exp(v.real()), v.imag());
 
272
 
        template<class T> _UCXXEXPORT complex<T> log  (const complex<T>& v){
 
273
 
                return complex<T>(log(abs(v)), arg(v));
 
276
 
        template<class T> _UCXXEXPORT complex<T> log10(const complex<T>& v){
 
277
 
                return (log(v) / log(T(10.0)));
 
280
 
        template<class T> _UCXXEXPORT complex<T> pow(const complex<T>& v, int p){
 
281
 
                T rho = pow(abs(v), p);
 
283
 
                return complex<T>(rho * cos(p * theta), rho * sin(p * theta) );
 
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 );
 
290
 
        template<class T> _UCXXEXPORT complex<T> pow(const complex<T>& v, const complex<T>& p){
 
292
 
                        //We are using "0" as the value
 
295
 
                return exp(p * log(v));
 
298
 
        template<class T> _UCXXEXPORT complex<T> pow(const T& v, const complex<T>& p){
 
302
 
                return polar(pow(v,p.real()), y.imag() * log(x) );
 
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()));
 
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()) );
 
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);
 
318
 
        template<class T> _UCXXEXPORT complex<T> tanh (const complex<T>& v){
 
319
 
                return sinh(v) / cosh(v);