00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifndef _COMPLEX_VECTOR_H_
00032 #define _COMPLEX_VECTOR_H_
00033
00034 #include <math.h>
00035 #include <stdlib.h>
00036 #include "Complex.h"
00037
00038 namespace FDDlib {
00039
00044 template<class T>
00045 class ComplexVector
00046 {
00047 protected:
00049 int len_;
00051 Complex<T>* valVec_;
00052
00053 public:
00057 ComplexVector(int len);
00058
00060 ComplexVector();
00061
00063 void init(int len);
00064
00066 ComplexVector(const ComplexVector<T>& cv);
00067
00069 ~ComplexVector();
00070
00072 void deallocate();
00073
00075 Complex<T> val(int i) const;
00076
00080 Complex<T>& val(int i);
00081
00083 Complex<T> operator()(int i) const;
00084
00086 Complex<T>& operator()(int i);
00087
00092 void set(int ind, Complex<T>& c);
00093
00098 void setReal(int ind, T real);
00099
00104 void setImag(int ind, T imag);
00105
00107 T getReal(int ind) const;
00108
00110 T getImag(int ind) const;
00111
00113 int size() const;
00114
00118 ComplexVector<T>& operator=(const ComplexVector<T>& cv);
00119
00123 ComplexVector<T>& operator*=(const Complex<T>& c);
00124
00128 ComplexVector<T>& operator+=(const ComplexVector<T>& cv);
00129
00133 ComplexVector<T>& operator-=(const ComplexVector<T>& cv);
00134
00138 ComplexVector<T> operator+(const ComplexVector<T>& cv) const;
00139
00143 ComplexVector<T> operator-(const ComplexVector<T>& cv) const;
00144
00148 ComplexVector<T> operator*(const Complex<T>& c) const;
00149
00151 ComplexVector<T> conjugate() const;
00152
00154 Complex<T> dot(const ComplexVector<T> cv) const;
00155
00157 double norm() const;
00158
00159 };
00160
00161 template<class T>
00162 ComplexVector<T>::ComplexVector(int len)
00163 {
00164 int i;
00165
00166 len_ = len;
00167 valVec_ = new Complex<T>[len_];
00168 for (i = 0; i < len_; i++)
00169 {
00170 setReal(i, (T) 0);
00171 setImag(i, (T) 0);
00172 }
00173 }
00174
00175 template<class T>
00176 ComplexVector<T>::ComplexVector()
00177 {
00178 len_ = 0;
00179 valVec_ = (Complex<T>*) NULL;
00180 }
00181
00182 template<class T>
00183 void ComplexVector<T>::init(int len)
00184 {
00185 int i;
00186
00187 len_ = len;
00188
00189 if (valVec_ != (Complex<T>*) NULL)
00190 delete[] valVec_;
00191
00192 valVec_ = new Complex<T>[len_];
00193 for (i = 0; i < len_; i++)
00194 {
00195 setReal(i, (T) 0);
00196 setImag(i, (T) 0);
00197 }
00198 }
00199
00200 template<class T>
00201 ComplexVector<T>::ComplexVector(const ComplexVector<T>& cv)
00202 {
00203 int i;
00204
00205 len_ = cv.size();
00206 valVec_ = new Complex<T>[len_];
00207 for (i = 0; i < len_; i++)
00208 {
00209 setReal(i, cv.getReal(i));
00210 setImag(i, cv.getImag(i));
00211 }
00212 }
00213
00214 template<class T>
00215 ComplexVector<T>::~ComplexVector()
00216 {
00217 if (valVec_ != (Complex<T>*) NULL)
00218 delete[] valVec_;
00219 }
00220
00221 template<class T>
00222 void ComplexVector<T>::deallocate()
00223 {
00224 if (valVec_ != (Complex<T>*) NULL)
00225 delete[] valVec_;
00226 len_ = 0;
00227
00228 valVec_ = (Complex<T>*) NULL;
00229 }
00230
00231 template<class T>
00232 inline Complex<T> ComplexVector<T>::val(int i) const
00233 {
00234 return valVec_[i];
00235 }
00236
00237 template<class T>
00238 inline Complex<T>& ComplexVector<T>::val(int i)
00239 {
00240 return valVec_[i];
00241 }
00242
00243 template<class T>
00244 inline Complex<T> ComplexVector<T>::operator()(int i) const
00245 {
00246 return valVec_[i];
00247 }
00248
00249 template<class T>
00250 inline Complex<T>& ComplexVector<T>::operator()(int i)
00251 {
00252 return valVec_[i];
00253 }
00254
00255
00256 template<class T>
00257 inline void ComplexVector<T>::set(int ind, Complex<T>& c)
00258 {
00259 setReal(ind, c.getReal());
00260 setImag(ind, c.getImag());
00261 }
00262
00263 template<class T>
00264 inline void ComplexVector<T>::setReal(int ind, T real)
00265 {
00266 valVec_[ind].setReal(real);
00267 }
00268
00269 template<class T>
00270 inline void ComplexVector<T>::setImag(int ind, T imag)
00271 {
00272 valVec_[ind].setImag(imag);
00273 }
00274
00275 template<class T>
00276 inline T ComplexVector<T>::getReal(int ind) const
00277 {
00278 return valVec_[ind].getReal();
00279 }
00280
00281 template<class T>
00282 inline T ComplexVector<T>::getImag(int ind) const
00283 {
00284 return valVec_[ind].getImag();
00285 }
00286
00287 template<class T>
00288 inline int ComplexVector<T>::size() const
00289 {
00290 return len_;
00291 }
00292
00293 template<class T>
00294 ComplexVector<T>& ComplexVector<T>::operator=(const ComplexVector<T>& cv)
00295 {
00296 int i;
00297
00298 if (cv.size() != size())
00299 {
00300 if (valVec_ != (Complex<T>*) NULL)
00301 delete[] valVec_;
00302 valVec_ = new Complex<T>[cv.size()];
00303 len_ = cv.size();
00304 }
00305 for (i = 0; i < len_; i++)
00306 {
00307 setReal(i, cv.getReal(i));
00308 setImag(i, cv.getImag(i));
00309 }
00310
00311 return *this;
00312 }
00313
00314 template<class T>
00315 ComplexVector<T>& ComplexVector<T>::operator*=(const Complex<T>& c)
00316 {
00317 int i;
00318 Complex<T> tmp;
00319
00320 for (i = 0; i < size(); i++)
00321 {
00322 tmp = val(i) * c;
00323 setReal(i, tmp.getReal());
00324 setImag(i, tmp.getImag());
00325 }
00326
00327 return *this;
00328 }
00329
00330 template<class T>
00331 ComplexVector<T>& ComplexVector<T>::operator+=(const ComplexVector<T>& cv)
00332 {
00333 int i;
00334 Complex<T> tmp;
00335
00336 for (i = 0; i < size(); i++)
00337 {
00338 tmp = val(i) + cv.val(i);
00339 setReal(i, tmp.getReal());
00340 setImag(i, tmp.getImag());
00341 }
00342
00343 return *this;
00344 }
00345
00346 template<class T>
00347 ComplexVector<T>& ComplexVector<T>::operator-=(const ComplexVector<T>& cv)
00348 {
00349 int i;
00350 Complex<T> tmp;
00351
00352 for (i = 0; i < size(); i++)
00353 {
00354 tmp = val(i) - cv.val(i);
00355 setReal(i, tmp.getReal());
00356 setImag(i, tmp.getImag());
00357 }
00358
00359 return *this;
00360 }
00361
00362 template<class T>
00363 ComplexVector<T> ComplexVector<T>::operator+(const ComplexVector<T>& cv) const
00364 {
00365 ComplexVector<T> ret(size());
00366 int i;
00367
00368 for (i = 0; i < size(); i++)
00369 {
00370 ret.set(i, val(i)+cv.val(i));
00371 }
00372
00373 return ret;
00374 }
00375
00376 template<class T>
00377 ComplexVector<T> ComplexVector<T>::operator-(const ComplexVector<T>& cv) const
00378 {
00379 ComplexVector<T> ret(size());
00380 int i;
00381
00382 for (i = 0; i < size(); i++)
00383 {
00384 ret.set(i, val(i)-cv.val(i));
00385 }
00386
00387 return ret;
00388 }
00389
00390 template<class T>
00391 ComplexVector<T> ComplexVector<T>::operator*(const Complex<T>& c) const
00392 {
00393 ComplexVector<T> ret(size());
00394 int i;
00395
00396 for (i = 0; i < size(); i++)
00397 {
00398 ret.set(i, val(i)*c);
00399 }
00400
00401 return ret;
00402 }
00403
00404 template<class T>
00405 ComplexVector<T> ComplexVector<T>::conjugate() const
00406 {
00407 ComplexVector<T> ret(size());
00408 int i;
00409
00410 for (i = 0; i < size(); i++)
00411 {
00412 ret.set(i, val(i).conjugate());
00413 }
00414
00415 return ret;
00416 }
00417
00418 template<class T>
00419 Complex<T> ComplexVector<T>::dot(const ComplexVector<T> cv) const
00420 {
00421 Complex<T> ret;
00422 int i;
00423
00424 ret.setReal(0);
00425 ret.setImag(0);
00426
00427 for (i = 0; i < size(); i++)
00428 {
00429 ret += val(i)*cv.val(i);
00430 }
00431
00432 return ret;
00433 }
00434
00435 template<class T>
00436 double ComplexVector<T>::norm() const
00437 {
00438 int i;
00439 double ret;
00440
00441 ret = 0;
00442 for (i = 0; i < size(); i++)
00443 {
00444 v = (double) getReal(i);
00445 ret += v*v;
00446 v = (double) getImag(i);
00447 ret += v*v;
00448 }
00449
00450 ret = sqrt(ret);
00451 return ret;
00452 }
00453
00454 }
00455 #endif