C-XSC - A C++ Class Library for Extended Scientific Computing  2.5.4
lx_complex.inl
00001 /*
00002 **  CXSC is a C++ library for eXtended Scientific Computing (V 2.5.4)
00003 **
00004 **  Copyright (C) 1990-2000 Institut fuer Angewandte Mathematik,
00005 **                          Universitaet Karlsruhe, Germany
00006 **            (C) 2000-2014 Wiss. Rechnen/Softwaretechnologie
00007 **                          Universitaet Wuppertal, Germany   
00008 **
00009 **  This library is free software; you can redistribute it and/or
00010 **  modify it under the terms of the GNU Library General Public
00011 **  License as published by the Free Software Foundation; either
00012 **  version 2 of the License, or (at your option) any later version.
00013 **
00014 **  This library is distributed in the hope that it will be useful,
00015 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 **  Library General Public License for more details.
00018 **
00019 **  You should have received a copy of the GNU Library General Public
00020 **  License along with this library; if not, write to the Free
00021 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 */
00023 
00024 /* CVS $Id: lx_complex.inl,v 1.8 2014/01/30 17:23:47 cxsc Exp $ */
00025 
00026 /*
00027 **  F. Blomquist, University of Wuppertal, 19.09.2007;
00028 */
00029 
00030 namespace cxsc {
00031 // --------------------------------------------------------------------------
00032 // ------- Inline functions and operators related to type lx_complex --------
00033 // --------------------------------------------------------------------------
00034 
00035 inline lx_real Re(const lx_complex &a)
00036 { return a.re; }
00037 
00038 inline lx_real Im(const lx_complex &a)
00039 { return a.im; }
00040 
00041 inline int StagPrec(const lx_complex &a) throw() 
00042 { return StagPrec(a.re); }
00043 
00044 inline real expoRe(const lx_complex &a) throw()
00045 { return expo(a.re); }
00046 
00047 inline real expoIm(const lx_complex &a) throw()
00048 { return expo(a.im); }
00049 
00050 inline l_real lr_partRe(const lx_complex &a) throw()
00051 { return lr_part(a.re); }
00052 
00053 inline l_real lr_partIm(const lx_complex &a) throw()
00054 { return lr_part(a.im); }
00055 
00056 inline lx_complex & SetRe(lx_complex &a, const lx_real &b)
00057 { a.re = b; return a; } // The real part of a is substituted by b.
00058 inline lx_complex & SetRe(lx_complex &a, const l_real &b)
00059 { a.re = b; return a; } // The real part of a is substituted by b.
00060 inline lx_complex & SetRe(lx_complex &a, const real &b)
00061 { a.re = b; return a; } // The real part of a is substituted by b.
00062 
00063 inline lx_complex & SetIm(lx_complex &a, const lx_real &b)
00064 { a.im = b; return a; } // The imaginary part of a is substituted by b.
00065 inline lx_complex & SetIm(lx_complex &a, const l_real &b)
00066 { a.im = b; return a; } // The imaginary part of a is substituted by b.
00067 inline lx_complex & SetIm(lx_complex &a, const real &b)
00068 { a.im = b; return a; } // The imaginary part of a is substituted by b.
00069 
00070 inline lx_complex conj(const lx_complex& a) throw()
00071 { return lx_complex(a.re, -a.im); }
00072 
00073 inline bool operator ! (const lx_complex& a) throw()
00074 { return !a.re && !a.im; }
00075 
00076 inline bool operator == (const lx_complex &a, const lx_complex &b) 
00077                 throw() { return (a.re == b.re && a.im == b.im); }
00078 
00079 inline bool operator == (const lx_complex &a, const l_complex &b) 
00080                 throw() { return (a.re == Re(b) && a.im == Im(b)); }
00081 inline bool operator == (const lx_complex &a, const complex &b) 
00082                 throw() { return (a.re == Re(b) && a.im == Im(b)); }
00083 inline bool operator == (const l_complex &a, const lx_complex &b) 
00084                 throw() { return b == a; }
00085 inline bool operator == (const complex &a, const lx_complex &b) 
00086                 throw() { return (b == a); }
00087 
00088 inline bool operator == (const lx_complex &a, const lx_real &b) throw()
00089 { return a.re == b && a.im == 0.0; }
00090 inline bool operator == (const lx_complex &a, const l_real &b) throw()
00091 { return a.re == b && a.im == 0.0; }
00092 inline bool operator == (const lx_complex &a, const real &b) throw()
00093 { return a.re == b && a.im == 0.0; }
00094 
00095 inline bool operator == (const lx_real &a, const lx_complex &b) throw()
00096 { return a == b.re && b.im == 0.0; }
00097 inline bool operator == (const l_real &a, const lx_complex &b) throw()
00098 { return a == b.re && b.im == 0.0; }
00099 inline bool operator == (const real &a,   const lx_complex &b) throw()
00100 { return a == b.re && b.im == 0.0; }
00101 
00102 inline bool operator != (const lx_complex &a, const lx_complex &b) throw()
00103 { return !(a == b); }
00104 
00105 inline bool operator != (const lx_complex &a, const l_complex &b) throw()
00106 { return !(a == b); }
00107 inline bool operator != (const lx_complex &a, const complex   &b) throw()
00108 { return !(a == b); }
00109 inline bool operator != (const l_complex &a, const lx_complex &b) throw()
00110 { return !(a == b); }
00111 inline bool operator != (const complex   &a, const lx_complex &b) throw()
00112 { return !(a == b); }
00113 
00114 inline bool operator != (const lx_complex &a, const lx_real &b) throw()
00115 { return !(a == b); }
00116 inline bool operator != (const lx_complex &a, const l_real &b) throw()
00117 { return !(a == b); }
00118 inline bool operator != (const lx_complex &a, const real &b)   throw()
00119 { return !(a == b); }
00120 inline bool operator != (const lx_real &a, const lx_complex &b) throw()
00121 { return !(a == b); }
00122 inline bool operator != (const l_real &a, const lx_complex &b) throw()
00123 { return !(a == b); }
00124 inline bool operator != (const real &a,   const lx_complex &b) throw()
00125 { return !(a == b); }
00126 
00127 inline lx_complex operator + (const lx_complex &a) throw()
00128 { return a; }
00129 inline lx_complex operator - (const lx_complex &a) throw()
00130 { return lx_complex(-a.re,-a.im); }
00131 
00132 inline lx_complex operator + (const lx_complex& a, const lx_complex& b) throw()
00133 { return lx_complex(a.re+b.re,a.im+b.im); }
00134 
00135 inline lx_complex operator + (const lx_complex& a, const l_complex& b) throw()
00136 { return a + lx_complex(b); }
00137 inline lx_complex operator + (const lx_complex& a, const complex& b) throw()
00138 { return a + lx_complex(b); }
00139 inline lx_complex operator + (const l_complex& a, const lx_complex& b) throw()
00140 { return lx_complex(a) + b; }
00141 inline lx_complex operator + (const complex& a, const lx_complex& b) throw()
00142 { return lx_complex(a) + b; }
00143 inline lx_complex operator + (const lx_complex& a, const lx_real& b) throw()
00144 { return lx_complex(a.re + b, Im(a)); }
00145 inline lx_complex operator + (const lx_real& a, const lx_complex& b) throw()
00146 { return lx_complex(b.re + a, Im(b)); }
00147 inline lx_complex operator + (const lx_complex& a, const l_real& b) throw()
00148 { return lx_complex(a.re + b, Im(a)); }
00149 inline lx_complex operator + (const l_real& a, const lx_complex& b) throw()
00150 { return lx_complex(b.re + a, Im(b)); }
00151 inline lx_complex operator + (const lx_complex& a, const real& b) throw()
00152 { return lx_complex(a.re + b, Im(a)); }
00153 inline lx_complex operator + (const real& a, const lx_complex& b) throw()
00154 { return lx_complex(b.re + a, Im(b)); }
00155 
00156 inline lx_complex & operator +=(lx_complex& a, const lx_complex& b) throw()
00157 { return a = a+b; }
00158 inline lx_complex & operator +=(lx_complex& a, const l_complex& b) throw()
00159 { return a = a+b; }
00160 inline lx_complex & operator +=(lx_complex& a, const complex& b) throw()
00161 { return a = a+b; }
00162 inline lx_complex & operator +=(lx_complex& a, const lx_real& b) throw()
00163 { return a = a+b; }
00164 inline lx_complex & operator +=(lx_complex& a, const l_real& b) throw()
00165 { return a = a+b; }
00166 inline lx_complex & operator +=(lx_complex& a, const real& b) throw()
00167 { return a = a+b; }
00168 
00169 inline lx_complex operator - (const lx_complex& a, const lx_complex& b) throw()
00170 { return a + (-b); }
00171 inline lx_complex operator - (const lx_complex& a, const l_complex& b) throw()
00172 { return a + (-b); }
00173 inline lx_complex operator - (const lx_complex& a, const complex& b) throw()
00174 { return a + (-b); }
00175 inline lx_complex operator - (const l_complex& a, const lx_complex& b) throw()
00176 { return a + (-b); }
00177 inline lx_complex operator - (const complex& a, const lx_complex& b) throw()
00178 { return a + (-b); }
00179 inline lx_complex operator - (const lx_complex& a, const lx_real& b) throw()
00180 { return a + (-b); }
00181 inline lx_complex operator - (const lx_complex& a, const l_real& b) throw()
00182 { return a + (-b); }
00183 inline lx_complex operator - (const lx_complex& a, const real& b) throw()
00184 { return a + (-b); }
00185 inline lx_complex operator - (const lx_real& a, const lx_complex& b) throw()
00186 { return a + (-b); }
00187 inline lx_complex operator - (const l_real& a, const lx_complex& b) throw()
00188 { return a + (-b); }
00189 inline lx_complex operator - (const real& a, const lx_complex& b) throw()
00190 { return a + (-b); }
00191 
00192 inline lx_complex & operator -=(lx_complex& a, const lx_complex& b) throw()
00193 { return a = a-b; }
00194 inline lx_complex & operator -=(lx_complex& a, const l_complex& b) throw()
00195 { return a = a-b; }
00196 inline lx_complex & operator -=(lx_complex& a, const complex& b) throw()
00197 { return a = a-b; }
00198 inline lx_complex & operator -=(lx_complex& a, const lx_real& b) throw()
00199 { return a = a-b; }
00200 inline lx_complex & operator -=(lx_complex& a, const l_real& b) throw()
00201 { return a = a-b; }
00202 inline lx_complex & operator -=(lx_complex& a, const real& b) throw()
00203 { return a = a-b; }
00204 
00205 
00206 inline lx_complex operator * (const lx_complex& a, const lx_complex& b) throw()
00207 {
00208         lx_real x,y;
00209         
00210         x = a.re*b.re - a.im*b.im;
00211         y = a.im*b.re + a.re*b.im;
00212         
00213         return lx_complex(x,y);
00214 }
00215 inline lx_complex operator * (const lx_complex& a, const l_complex& b) throw()
00216 { return a*lx_complex(b); }
00217 inline lx_complex operator * (const lx_complex& a, const complex& b) throw()
00218 { return a*lx_complex(b); }
00219 inline lx_complex operator * (const l_complex& a, const lx_complex& b) throw()
00220 { return lx_complex(a)*b; }
00221 inline lx_complex operator * (const complex& a, const lx_complex& b) throw()
00222 { return lx_complex(a)*b; }
00223 inline lx_complex operator * (const lx_complex& a, const lx_real& b) throw()
00224 { return a*lx_complex(b); }
00225 inline lx_complex operator * (const lx_complex& a, const l_real& b) throw()
00226 { return a*lx_complex(b); }
00227 inline lx_complex operator * (const lx_complex& a, const real& b) throw()
00228 { return a*lx_complex(b); }
00229 inline lx_complex operator * (const lx_real& a, const lx_complex& b) throw()
00230 { return lx_complex(a)*b; }
00231 inline lx_complex operator * (const l_real& a, const lx_complex& b) throw()
00232 { return lx_complex(a)*b; }
00233 inline lx_complex operator * (const real& a, const lx_complex& b) throw()
00234 { return lx_complex(a)*b; }
00235 
00236 inline lx_complex & operator *=(lx_complex& a, const lx_complex& b) throw()
00237 { return a = a*b; }
00238 inline lx_complex & operator *=(lx_complex& a, const l_complex& b) throw()
00239 { return a = a*b; }
00240 inline lx_complex & operator *=(lx_complex& a, const complex& b) throw()
00241 { return a = a*b; }
00242 inline lx_complex & operator *=(lx_complex& a, const lx_real& b) throw()
00243 { return a = a*b; }
00244 inline lx_complex & operator *=(lx_complex& a, const l_real& b) throw()
00245 { return a = a*b; }
00246 inline lx_complex & operator *=(lx_complex& a, const real& b) throw()
00247 { return a = a*b; }
00248 
00249 
00250 inline lx_complex operator / (const lx_complex& a, const lx_complex& b) throw()
00251 {
00252         lx_real x,y,Ne;
00253                 
00254         Ne = b.re*b.re + b.im*b.im;
00255         x = (a.re*b.re + a.im*b.im) / Ne;
00256         y = (a.im*b.re - a.re*b.im) / Ne;
00257         return lx_complex(x,y);
00258 }
00259 inline lx_complex operator / (const lx_complex& a, const l_complex& b) throw()
00260 { return a/lx_complex(b); }
00261 inline lx_complex operator / (const lx_complex& a, const complex& b) throw()
00262 { return a/lx_complex(b); }
00263 inline lx_complex operator / (const l_complex& a, const lx_complex& b) throw()
00264 { return lx_complex(a)/b; }
00265 inline lx_complex operator / (const complex& a, const lx_complex& b) throw()
00266 { return lx_complex(a)/b; }
00267 inline lx_complex operator / (const lx_complex& a, const lx_real& b) throw()
00268 { return a/lx_complex(b); }
00269 inline lx_complex operator / (const lx_complex& a, const l_real& b) throw()
00270 { return a/lx_complex(b); }
00271 inline lx_complex operator / (const lx_complex& a, const real& b) throw()
00272 { return a/lx_complex(b); }
00273 inline lx_complex operator / (const lx_real& a, const lx_complex& b) throw()
00274 { return lx_complex(a)/b; }
00275 inline lx_complex operator / (const l_real& a, const lx_complex& b) throw()
00276 { return lx_complex(a)/b; }
00277 inline lx_complex operator / (const real& a, const lx_complex& b) throw()
00278 { return lx_complex(a)/b; }
00279 
00280 inline lx_complex & operator /=(lx_complex& a, const lx_complex& b) throw()
00281 { return a = a/b; }
00282 inline lx_complex & operator /=(lx_complex& a, const l_complex& b) throw()
00283 { return a = a/b; }
00284 inline lx_complex & operator /=(lx_complex& a, const complex& b) throw()
00285 { return a = a/b; }
00286 inline lx_complex & operator /=(lx_complex& a, const lx_real& b) throw()
00287 { return a = a/b; }
00288 inline lx_complex & operator /=(lx_complex& a, const l_real& b) throw()
00289 { return a = a/b; }
00290 inline lx_complex & operator /=(lx_complex& a, const real& b) throw()
00291 { return a = a/b; }
00292 
00293 // --------------------------- Output ---------------------------------
00294 
00295 inline std::ostream& operator << (std::ostream& s, const lx_complex& a) 
00296                 throw()
00297 // A value a of type lx_complex is written to the output channel.
00298 // The output has the form:  { ? , ? }
00299 {     
00300         s << '('
00301                         << a.re
00302                         << " , "
00303                         << a.im
00304                         << ')';
00305         return s;
00306 }
00307 
00308 inline std::string & operator << (std::string &s, const lx_complex& a) throw()
00309 // The value of a variable a of type lx_complex is copied to a string s.
00310 // s has the form:  ({2**(...)*...} , {2**(...)*...})
00311 {
00312         string str;
00313         s += "(";
00314         str = "";
00315         str << a.re;
00316         s += str;
00317         s += " , ";
00318         str = "";
00319         str << a.im;
00320         s += str;
00321         s += ")";
00322         return s;
00323 }
00324 
00325 } // end namespace cxsc