PolarTmplDef.hxx

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
00003  *                         UNIVERSITAT POMPEU FABRA
00004  *
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  */
00021 
00022 
00023 #ifndef _PolarTmplDef_
00024 #define _PolarTmplDef_
00025 
00026 #include <iostream>
00027 #include <sstream>
00028 
00029 namespace CLAM
00030 {
00031         // complex '+=' operator
00032         template <class T>
00033         const PolarTmpl<T>& PolarTmpl<T>::operator += (const PolarTmpl<T>& a)
00034         {
00035                 T r1,i1,r2,i2,r3,i3;
00036                 
00037                 r1 = fabs(a.mMag) * CLAM_cos(a.mAng); 
00038                 i1 = fabs(a.mMag) * CLAM_sin(a.mAng); 
00039                 r2 = fabs(mMag) * CLAM_cos(mAng);
00040                 i2 = fabs(mMag) * CLAM_sin(mAng);
00041                 
00042                 r3 = r1+r2;
00043                 i3 = i1+i2;
00044                 
00045                 mMag = sqrt (r3*r3 + i3*i3);
00046                 mAng = atan2 (i3,r3); 
00047         
00048                 return *this;
00049         }
00050 
00051         //  complex '-=' operator 
00052         template <class T>
00053         const PolarTmpl<T>& PolarTmpl<T>::operator -= (const PolarTmpl<T>& a)
00054         {
00055                 T r1,i1,r2,i2,r3,i3;
00056 
00057                 r1 = fabs(a.mMag) * CLAM_cos(a.mAng); 
00058                 i1 = fabs(a.mMag) * CLAM_sin(a.mAng); 
00059                 r2 = fabs(mMag) * CLAM_cos(mAng);
00060                 i2 = fabs(mMag) * CLAM_sin(mAng);
00061                 
00062                 r3 = r2-r1;
00063                 i3 = i2-i1;
00064                 
00065                 mMag = CLAM_sqrt (r3*r3 + i3*i3);
00066                 mAng = CLAM_atan2 (i3,r3); 
00067         
00068                 return *this;
00069         }
00070 
00071         // polar '+' operator 
00072         template <class T>
00073         PolarTmpl<T> PolarTmpl<T>::operator + (const PolarTmpl<T>& b) const
00074         {
00075                 T r1,i1,r2,i2,r3,i3;
00076                 
00077                 r1 = fabs(mMag) * CLAM_cos(mAng); 
00078                 i1 = fabs(mMag) * CLAM_sin(mAng); 
00079                 r2 = fabs(b.mMag) * CLAM_cos(b.mAng);
00080                 i2 = fabs(b.mMag) * CLAM_sin(b.mAng);
00081                 
00082                 r3 = r1+r2;
00083                 i3 = i1+i2;
00084                 
00085                 PolarTmpl<T> ret(CLAM_sqrt (r3*r3 + i3*i3),CLAM_atan2 (i3,r3)); 
00086                 return ret;
00087         }
00088 
00089          //  polar '-' operator 
00090         template <class T>
00091         PolarTmpl<T> PolarTmpl<T>::operator - (const PolarTmpl<T>& b) const
00092         {
00093                 T r1,i1,r2,i2,r3,i3;
00094                 
00095                 r1 = fabs(mMag) * CLAM_cos(mAng); 
00096                 i1 = fabs(mMag) * CLAM_sin(mAng); 
00097                 r2 = fabs(b.mMag) * CLAM_cos(b.mAng);
00098                 i2 = fabs(b.mMag) * CLAM_sin(b.mAng);
00099                 
00100                 r3 = r1-r2;
00101                 i3 = i1-i2;
00102                 
00103                 PolarTmpl<T> ret(CLAM_sqrt (r3*r3 + i3*i3),CLAM_atan2 (i3,r3)); 
00104                 return ret;
00105         }
00106 
00107         template <class T>
00108         inline std::istream& operator >> (std::istream & is,
00109                         PolarTmpl<T> & a)
00110         {
00111                 if (is.flags() & std::ios::skipws) {
00112                         char c = '\0';
00113                         do
00114                                 is.get(c);
00115                         while (is && isspace(c));
00116                         if (is) is.putback(c);
00117                 }
00118                 char c = '\0';
00119                 is >> c;
00120                 if (c!='{') {
00121                         if (is) is.putback(c);
00122 //                      std::cerr << "A polar starting with '" << c << "'" << std::endl;
00123                         return is;
00124                 }
00125                 T m;
00126                 T p; 
00127                 if (!(is >> m)) return is;
00128                 if (!(is >> p)) return is;
00129                 if (is.flags() & std::ios::skipws) {
00130                         char c = '\0';
00131                         do
00132                                 is.get(c);
00133                         while (is && isspace(c));
00134                         if (is) is.putback(c);
00135                 }
00136                 if (!is.get(c) || c!='}') return is;
00137 
00138                 a.SetMag(m);
00139                 a.SetAng(p);
00140                 return is;
00141         }
00142 
00143         template <class T> 
00144         std::ostream& operator << (std::ostream& myStream, const PolarTmpl<T>& a)
00145         {
00146                 return myStream 
00147                         << "{"
00148                         << a.Mag()
00149                         << " "
00150                         << a.Ang()
00151                         << "}";
00152         }
00153 
00154 } // namespace CLAM
00155 
00156 
00157 #endif // _PolarTmplDef_
00158 
Generated by  doxygen 1.6.3