CLAM-Development  1.4.0
PolarTmplDef.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 
23 #ifndef _PolarTmplDef_
24 #define _PolarTmplDef_
25 
26 #include <iostream>
27 #include <sstream>
28 
29 namespace CLAM
30 {
31  // complex '+=' operator
32  template <class T>
34  {
35  T r1,i1,r2,i2,r3,i3;
36 
37  r1 = fabs(a.mMag) * CLAM_cos(a.mAng);
38  i1 = fabs(a.mMag) * CLAM_sin(a.mAng);
39  r2 = fabs(mMag) * CLAM_cos(mAng);
40  i2 = fabs(mMag) * CLAM_sin(mAng);
41 
42  r3 = r1+r2;
43  i3 = i1+i2;
44 
45  mMag = sqrt (r3*r3 + i3*i3);
46  mAng = atan2 (i3,r3);
47 
48  return *this;
49  }
50 
51  // complex '-=' operator
52  template <class T>
54  {
55  T r1,i1,r2,i2,r3,i3;
56 
57  r1 = fabs(a.mMag) * CLAM_cos(a.mAng);
58  i1 = fabs(a.mMag) * CLAM_sin(a.mAng);
59  r2 = fabs(mMag) * CLAM_cos(mAng);
60  i2 = fabs(mMag) * CLAM_sin(mAng);
61 
62  r3 = r2-r1;
63  i3 = i2-i1;
64 
65  mMag = CLAM_sqrt (r3*r3 + i3*i3);
66  mAng = CLAM_atan2 (i3,r3);
67 
68  return *this;
69  }
70 
71  // polar '+' operator
72  template <class T>
74  {
75  T r1,i1,r2,i2,r3,i3;
76 
77  r1 = fabs(mMag) * CLAM_cos(mAng);
78  i1 = fabs(mMag) * CLAM_sin(mAng);
79  r2 = fabs(b.mMag) * CLAM_cos(b.mAng);
80  i2 = fabs(b.mMag) * CLAM_sin(b.mAng);
81 
82  r3 = r1+r2;
83  i3 = i1+i2;
84 
85  PolarTmpl<T> ret(CLAM_sqrt (r3*r3 + i3*i3),CLAM_atan2 (i3,r3));
86  return ret;
87  }
88 
89  // polar '-' operator
90  template <class T>
92  {
93  T r1,i1,r2,i2,r3,i3;
94 
95  r1 = fabs(mMag) * CLAM_cos(mAng);
96  i1 = fabs(mMag) * CLAM_sin(mAng);
97  r2 = fabs(b.mMag) * CLAM_cos(b.mAng);
98  i2 = fabs(b.mMag) * CLAM_sin(b.mAng);
99 
100  r3 = r1-r2;
101  i3 = i1-i2;
102 
103  PolarTmpl<T> ret(CLAM_sqrt (r3*r3 + i3*i3),CLAM_atan2 (i3,r3));
104  return ret;
105  }
106 
107  template <class T>
108  inline std::istream& operator >> (std::istream & is,
109  PolarTmpl<T> & a)
110  {
111  if (is.flags() & std::ios::skipws) {
112  char c = '\0';
113  do
114  is.get(c);
115  while (is && isspace(c));
116  if (is) is.putback(c);
117  }
118  char c = '\0';
119  is >> c;
120  if (c!='{') {
121  if (is) is.putback(c);
122 // std::cerr << "A polar starting with '" << c << "'" << std::endl;
123  return is;
124  }
125  T m;
126  T p;
127  if (!(is >> m)) return is;
128  if (!(is >> p)) return is;
129  if (is.flags() & std::ios::skipws) {
130  char c = '\0';
131  do
132  is.get(c);
133  while (is && isspace(c));
134  if (is) is.putback(c);
135  }
136  if (!is.get(c) || c!='}') return is;
137 
138  a.SetMag(m);
139  a.SetAng(p);
140  return is;
141  }
142 
143  template <class T>
144  std::ostream& operator << (std::ostream& myStream, const PolarTmpl<T>& a)
145  {
146  return myStream
147  << "{"
148  << a.Mag()
149  << " "
150  << a.Ang()
151  << "}";
152  }
153 
154 } // namespace CLAM
155 
156 
157 #endif // _PolarTmplDef_
158