$treeview $search $mathjax
Palabos  Version 1.1
$projectbrief
$projectbrief
$searchbox

plbComplex.hh

Go to the documentation of this file.
00001 /* This file is part of the Palabos library.
00002  *
00003  * Copyright (C) 2011 FlowKit Sarl
00004  * Avenue de Chailly 23
00005  * 1012 Lausanne, Switzerland
00006  * E-mail contact: contact@flowkit.com
00007  *
00008  * The most recent release of Palabos can be downloaded at 
00009  * <http://www.palabos.org/>
00010  *
00011  * The library Palabos is free software: you can redistribute it and/or
00012  * modify it under the terms of the GNU Affero General Public License as
00013  * published by the Free Software Foundation, either version 3 of the
00014  * License, or (at your option) any later version.
00015  *
00016  * The library is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU Affero General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU Affero General Public License
00022  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00023 */
00024 
00025 #ifndef PLB_COMPLEX_HH
00026 #define PLB_COMPLEX_HH
00027 
00028 #include "core/plbComplex.h"
00029 #include <cmath>
00030 
00031 namespace plb {
00032 
00033 template<typename T>
00034 T Complex<T>::pi = (T)4*atan((T)1);
00035 
00036 template<typename T>
00037 Complex<T>::Complex()
00038     : Re(), Imag()
00039 { }
00040 
00041 template<typename T>
00042 Complex<T>::Complex(T Re_)
00043     : Re(Re_), Imag(T())
00044 { }
00045 
00046 template<typename T>
00047 Complex<T>::Complex(T Re_, T Imag_)
00048     : Re(Re_), Imag(Imag_)
00049 { }
00050 
00051 template<typename T>
00052 template<typename U>
00053 Complex<T>::operator U() const {
00054     return (U)Re;
00055 }
00056 
00057 template<typename T>
00058 T Complex<T>::real() const {
00059     return Re;
00060 }
00061 
00062 template<typename T>
00063 T Complex<T>::imaginary() const {
00064     return Imag;
00065 }
00066 
00067 template<typename T>
00068 T Complex<T>::modulus() const {
00069     return sqrt(sqrModulus);
00070 }
00071 
00072 template<typename T>
00073 T Complex<T>::sqrModulus() const {
00074     return Re*Re + Imag*Imag;
00075 }
00076 
00077 template<typename T>
00078 Complex<T> Complex<T>::conjugate() const {
00079     return Complex<T>(Re, -Imag);
00080 }
00081 
00082 template<typename T>
00083 T Complex<T>::argument() const {
00084     if (Re>T()) {
00085         return atan(Imag/Re);
00086     }
00087     else if (Re<T()) {
00088         return pi + atan(Imag/Re);
00089     }
00090     else {
00091         return pi/(T)2;
00092     }
00093 }
00094 
00095 template<typename T>
00096 Complex<T> Complex<T>::intpow(int n) const {
00097     T r_pow_n = pow(modulus(),n);
00098     T phi = argument();
00099     return Complex<T> (
00100              r_pow_n*(cos(n*phi)),
00101              r_pow_n*(sin(n*phi)) );
00102 }
00103 
00104 template<typename T>
00105 Complex<T>& Complex<T>::operator+=(Complex<T> const& rhs) {
00106     Re += rhs.Re;
00107     Imag += rhs.Imag;
00108     return *this;
00109 }
00110 
00111 template<typename T>
00112 template<typename U>
00113 Complex<T>& Complex<T>::operator+=(U rhs) {
00114     Re += (T)rhs;
00115     return *this;
00116 }
00117 
00118 template<typename T>
00119 Complex<T>& Complex<T>::operator-=(Complex<T> const& rhs) {
00120     Re -= rhs.Re;
00121     Imag -= rhs.Imag;
00122     return *this;
00123 }
00124 
00125 template<typename T>
00126 template<typename U>
00127 Complex<T>& Complex<T>::operator-=(U rhs) {
00128     Re -= (T)rhs;
00129     return *this;
00130 }
00131 template<typename T>
00132 Complex<T> Complex<T>::operator-() const
00133 {
00134     return Complex<T>(-Re, -Imag);
00135 }
00136 
00137 template<typename T>
00138 Complex<T>& Complex<T>::operator*=(Complex<T> const& rhs) {
00139     T tmpRe = Re*rhs.Re - Imag*rhs.Imag;
00140     Imag = Imag*rhs.Re + Re*rhs.Imag;
00141     Re = tmpRe;
00142     return *this;
00143 }
00144 
00145 template<typename T>
00146 template<typename U>
00147 Complex<T>& Complex<T>::operator*=(U rhs) {
00148     Re *= (T)rhs;
00149     Imag *= (T)rhs;
00150     return *this;
00151 }
00152 
00153 template<typename T>
00154 Complex<T>& Complex<T>::operator/=(Complex<T> const& rhs) {
00155     T rhsNormSqr = rhs.sqrModulus();
00156     T tmpRe = (Re*rhs.Re + Imag*rhs.Imag)/rhsNormSqr;
00157     Imag = (Imag*rhs.Re - Re*rhs.Imag)/rhsNormSqr;
00158     Re = tmpRe;
00159     return *this;
00160 }
00161 
00162 
00163 template<typename T>
00164 template<typename U>
00165 Complex<T>& Complex<T>::operator/=(U rhs) {
00166     Re /= (T)rhs;
00167     Imag /= (T)rhs;
00168     return *this;
00169 }
00170 
00171 
00172 template<typename T>
00173 Complex<T> operator+(Complex<T> const& arg1, Complex<T> const& arg2) {
00174     return Complex<T>(arg1) += arg2;
00175 }
00176 
00177 template<typename T, typename U>
00178 Complex<T> operator+(Complex<T> const& arg1, U arg2) {
00179     return Complex<T>(arg1) += (T)arg2;
00180 }
00181 
00182 template<typename T, typename U>
00183 Complex<U> operator+(T arg1, Complex<U> const& arg2) {
00184     return Complex<U>((U)arg1+arg2.real(), (U)arg2.imaginary());
00185 }
00186 
00187 
00188 
00189 template<typename T>
00190 Complex<T> operator-(Complex<T> const& arg1, Complex<T> const& arg2) {
00191     return Complex<T>(arg1) -= arg2;
00192 }
00193 
00194 template<typename T, typename U>
00195 Complex<T> operator-(Complex<T> const& arg1, U arg2) {
00196     return Complex<T>(arg1) -= (T)arg2;
00197 }
00198 
00199 template<typename T, typename U>
00200 Complex<U> operator-(T arg1, Complex<U> const& arg2) {
00201     return Complex<T>((U)arg1-arg2.real(), arg2.imaginary());
00202 }
00203 
00204 
00205 
00206 template<typename T>
00207 Complex<T> operator*(Complex<T> const& arg1, Complex<T> const& arg2) {
00208     return Complex<T>(arg1) *= arg2;
00209 }
00210 
00211 template<typename T, typename U>
00212 Complex<T> operator*(Complex<T> const& arg1, U arg2) {
00213     return Complex<T>(arg1) *= (T)arg2;
00214 }
00215 
00216 template<typename T, typename U>
00217 Complex<U> operator*(T arg1, Complex<U> const& arg2) {
00218     return Complex<U>((U)arg1*arg2.real(), (U)arg1*arg2.imaginary());
00219 }
00220 
00221 
00222 
00223 template<typename T>
00224 Complex<T> operator/(Complex<T> const& arg1, Complex<T> const& arg2) {
00225     return Complex<T>(arg1) /= arg2;
00226 }
00227 
00228 template<typename T, typename U>
00229 Complex<T> operator/(Complex<T> const& arg1, U arg2) {
00230     return Complex<T>(arg1) /= (T)arg2;
00231 }
00232 
00233 template<typename T, typename U>
00234 Complex<U> operator/(T arg1, Complex<U> const& arg2) {
00235     return Complex<U>((U)arg1/arg2.real(), (U)arg1/arg2.imaginary());
00236 }
00237 
00238 }  // namespace plb
00239 
00240 #endif  // PLB_COMPLEX_HH