OpenFOAM logo
Open Source CFD Toolkit

complexI.H

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2005 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Description
00026     Complex number.
00027 
00028 \*---------------------------------------------------------------------------*/
00029 
00030 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00031 
00032 namespace Foam
00033 {
00034 
00035 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00036 
00037 inline complex::complex()
00038 {}
00039 
00040 
00041 inline complex::complex(const scalar Re, const scalar Im)
00042 :
00043     re(Re),
00044     im(Im)
00045 {}
00046 
00047 
00048 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00049 
00050 inline scalar complex::Re() const
00051 {
00052     return re;
00053 }
00054 
00055 
00056 inline scalar complex::Im() const
00057 {
00058     return im;
00059 }
00060 
00061 
00062 inline scalar& complex::Re()
00063 {
00064     return re;
00065 }
00066 
00067 
00068 inline scalar& complex::Im()
00069 {
00070     return im;
00071 }
00072 
00073 
00074 inline complex complex::conjugate() const
00075 {
00076     return complex(re, -im);
00077 }
00078 
00079 
00080 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00081 
00082 inline void complex::operator=(const complex& c)
00083 {
00084     re = c.re;
00085     im = c.im;
00086 }
00087 
00088 
00089 inline void complex::operator+=(const complex& c)
00090 {
00091     re += c.re;
00092     im += c.im;
00093 }
00094 
00095 
00096 inline void complex::operator-=(const complex& c)
00097 {
00098     re -= c.re;
00099     im -= c.im;
00100 }
00101 
00102 
00103 inline void complex::operator*=(const complex& c)
00104 {
00105     *this = (*this)*c;
00106 }
00107 
00108 
00109 inline void complex::operator/=(const complex& c)
00110 {
00111     *this = *this/c;
00112 }
00113 
00114 
00115 inline void complex::operator=(const scalar s)
00116 {
00117     re = s;
00118     im = 0.0;
00119 }
00120 
00121 
00122 inline void complex::operator+=(const scalar s)
00123 {
00124     re += s;
00125 }
00126 
00127 
00128 inline void complex::operator-=(const scalar s)
00129 {
00130     re -= s;
00131 }
00132 
00133 
00134 inline void complex::operator*=(const scalar s)
00135 {
00136     re *= s;
00137     im *= s;
00138 }
00139 
00140 
00141 inline void complex::operator/=(const scalar s)
00142 {
00143     re /= s;
00144     im /= s;
00145 }
00146 
00147 
00148 inline complex complex::operator!() const
00149 {
00150     return conjugate();
00151 }
00152 
00153 
00154 inline bool complex::operator==(const complex& c) const
00155 {
00156     return (equal(re, c.re) && equal(im, c.im));
00157 }
00158 
00159 
00160 inline bool complex::operator!=(const complex& c) const
00161 {
00162     return !operator==(c);
00163 }
00164 
00165 
00166 // * * * * * * * * * * * * * * * Friend Functions  * * * * * * * * * * * * * //
00167 
00168 
00169 inline scalar magSqr(const complex& c)
00170 {
00171     return (c.re*c.re + c.im*c.im);
00172 }
00173 
00174 
00175 inline complex sqr(const complex& c)
00176 {
00177     return c * c;
00178 }
00179 
00180 
00181 inline scalar mag(const complex& c)
00182 {
00183     return sqrt(magSqr(c));
00184 }
00185 
00186 
00187 inline const complex& max(const complex& c1, const complex& c2)
00188 {
00189     if (mag(c1) > mag(c2))
00190     {
00191         return c1;
00192     }
00193     else
00194     {
00195         return c2;
00196     }
00197 }
00198 
00199 
00200 inline const complex& min(const complex& c1, const complex& c2)
00201 {
00202     if (mag(c1) < mag(c2))
00203     {
00204         return c1;
00205     }
00206     else
00207     {
00208         return c2;
00209     }
00210 }
00211 
00212 
00213 inline complex limit(const complex& c1, const complex& c2)
00214 {
00215     return complex(limit(c1.re, c2.re), limit(c1.im, c2.im));
00216 }
00217 
00218 
00219 inline const complex& sum(const complex& c)
00220 {
00221     return c;
00222 }
00223 
00224 
00225 template<class Cmpt>
00226 class Tensor;
00227 
00228 inline complex transform(const Tensor<scalar>&, const complex c)
00229 {
00230     return c;
00231 }
00232 
00233 
00234 // * * * * * * * * * * * * * * * Friend Operators  * * * * * * * * * * * * * //
00235 
00236 inline complex operator+(const complex& c1, const complex& c2)
00237 {
00238     return complex
00239     (
00240         c1.re+c2.re,
00241         c1.im+c2.im
00242     );
00243 }
00244 
00245 
00246 inline complex operator-(const complex& c)
00247 {
00248     return complex
00249     (
00250         -c.re,
00251         -c.im
00252     );
00253 }
00254 
00255 
00256 inline complex operator-(const complex& c1, const complex& c2)
00257 {
00258     return complex
00259     (
00260         c1.re-c2.re,
00261         c1.im-c2.im
00262     );
00263 }
00264 
00265 
00266 inline complex operator*(const complex& c1, const complex& c2)
00267 {
00268     return complex
00269     (
00270         c1.re*c2.re - c1.im*c2.im,
00271         c1.im*c2.re + c1.re*c2.im
00272     );
00273 }
00274 
00275 
00276 inline complex operator/(const complex& c1, const complex& c2)
00277 {
00278     scalar sqrC2 = magSqr(c2);
00279 
00280     return complex
00281     (
00282         (c1.re*c2.re + c1.im*c2.im)/sqrC2,
00283         (c1.im*c2.re - c1.re*c2.im)/sqrC2
00284     );
00285 }
00286 
00287 
00288 inline complex operator*(const scalar s, const complex& c)
00289 {
00290     return complex(s*c.re, s*c.im);
00291 }
00292 
00293 
00294 inline complex operator*(const complex& c, const scalar s)
00295 {
00296     return complex(s*c.re, s*c.im);
00297 }
00298 
00299 
00300 inline complex operator/(const complex& c, const scalar s)
00301 {
00302     return complex(c.re/s, c.im/s);
00303 }
00304 
00305 
00306 inline complex operator/(const scalar s, const complex& c)
00307 {
00308     return complex(s/c.re, s/c.im);
00309 }
00310 
00311 
00312 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00313 
00314 } // End namespace Foam
00315 
00316 // ************************************************************************* //
For further information go to www.openfoam.org