Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

vector.h

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*
00002  *                                   IT++                                    *
00003  *---------------------------------------------------------------------------*
00004  * Copyright (c) 1995-2002 by Tony Ottosson, Thomas Eriksson, Pål Frenger,   *
00005  * Tobias Ringström, and Jonas Samuelsson.                                   *
00006  *                                                                           *
00007  * Permission to use, copy, modify, and distribute this software and its     *
00008  * documentation under the terms of the GNU General Public License is hereby *
00009  * granted. No representations are made about the suitability of this        *
00010  * software for any purpose. It is provided "as is" without expressed or     *
00011  * implied warranty. See the GNU General Public License for more details.    *
00012  *---------------------------------------------------------------------------*/
00016 //  SPUC - Signal processing using C++ - A DSP library
00017 
00028 #ifndef _vectorh
00029 #define _vectorh
00030 
00031 #include <cmath>
00032 #include <sstream>
00033 #include <iostream>
00034 
00035 #ifdef HAVE_CBLAS
00036 #include "cblas.h"
00037 #endif
00038 
00039 // This needs to be included before algorithm included in scalfunc for g++-2.7.2
00040 #include <binary.h>
00041 #include <spucconfig.h>
00042 #include <algorithm>
00043 //#include "spucassert.h"
00044 #include <complex.h>
00045 #include <cstdlib>
00046 #include <cstring>
00047 #define Vector Vec
00048 namespace SPUC {
00049 
00051 template<class T> class Vec;
00053 template<class T> class Mat;
00054 class bin;
00055 
00056 //----------------- Vec Friends -------------------------
00058 template<class T> Vec<T> operator+(const Vec<T> &v1, const Vec<T> &v2);
00060 template<class T> Vec<T> operator+(const Vec<T> &v, T t);
00062 template<class T> Vec<T> operator+(T t, const Vec<T> &v);
00064 template<class T> Vec<T> operator-(const Vec<T> &v1, const Vec<T> &v2);
00066 template<class T> Vec<T> operator-(const Vec<T> &v, T t);
00068 template<class T> Vec<T> operator-(T t, const Vec<T> &v);
00070 template<class T> Vec<T> operator-(const Vec<T> &v);
00072 template<class T> T dot(const Vec<T> &v1, const Vec<T> &v2);
00073 template<class T> T dot_prod(const Vec<T> &v1, const Vec<T> &v2);
00075 template<class T> T operator*(const Vec<T> &v1, const Vec<T> &v2)
00076   { return dot(v1, v2); }
00078 template<class T> Mat<T> outer_product(const Vec<T> &v1, const Vec<T> &v2);
00080 template<class T> Vec<T> operator*(const Vec<T> &v, T t);
00082 template<class T> Vec<T> operator*(T t, const Vec<T> &v);
00084 template<class T> Vec<T> elem_mult(const Vec<T> &v1, const Vec<T> &v2);
00086 template<class T> Vec<T> elem_mult(const Vec<T> &v1, const Vec<T> &v2, const Vec<T> &v3);
00088 template<class T> Vec<T> elem_mult(const Vec<T> &v1, const Vec<T> &v2, const Vec<T> &v3, const Vec<T> &v4);
00089 
00091 template<class T> Vec<T> operator/(const Vec<T> &v, T t);
00093 template<class T> Vec<T> operator/(const T t, const Vec<T> &v);
00095 template<class T> Vec<T> elem_div(const Vec<T> &v1, const Vec<T> &v2);
00097 template<class T> Vec<T> elem_div(const T t, const Vec<T> &v);
00098 
00100 template<class T> Vec<T> concat(const Vec<T> &v, const T a);
00102 template<class T> Vec<T> concat(const T a, const Vec<T> &v);
00104 template<class T> Vec<T> concat(const Vec<T> &v1,const Vec<T> &v2);
00106 template<class T> Vec<T> concat(const Vec<T> &v1, const Vec<T> &v2, const Vec<T> &v3);
00108 template<class T> Vec<T> concat(const Vec<T> &v1, const Vec<T> &v2, const Vec<T> &v3, const Vec<T> &v4);
00110 template<class T> Vec<T> concat(const Vec<T> &v1, const Vec<T> &v2, const Vec<T> &v3, 
00111                                 const Vec<T> &v4, const Vec<T> &v5);
00112 
00171 template<class T>
00172 class Vec {
00173  public:
00175   Vec() { init(); }
00177   explicit Vec(int size) { 
00178 //        it_assert1(size>=0,"Negative size in Vec::Vec(int)"); 
00179           init(); alloc(size); }
00181   Vec(const Vec<T> &v);
00183   Vec(const char *values) { init(); set(values); }
00185   Vec(const string &values) { init(); set(values); }
00187   Vec(T *c_array, int size) { init(); alloc(size); memcpy(data, c_array, size*sizeof(T)); }
00189   ~Vec() { free(); }
00190 
00192   int length() const { return datasize; }
00194   int size() const { return datasize; }
00195 
00197   void set_length(int size, bool copy=false) { set_size(size,copy); }
00199   void set_size(int size, bool copy=false);
00201   void zeros() { for (int i=0; i<datasize; i++) {data[i]=T(0);} }
00203   void clear() { zeros(); }
00205   void ones() { for (int i=0; i<datasize; i++) {data[i]=T(1);} }
00207   bool set(const char *str);
00209   bool set(const string &str);
00210 
00212   T operator[](int i) const { 
00213           //it_assert0(i>=0&&i<datasize, "operator[]");
00214           return data[i]; }
00216   T operator()(int i) const { 
00217 //        it_assert0(i>=0&&i<datasize, "operator()"); 
00218           return data[i]; }
00220   T &operator[](int i) { 
00221 //              it_assert0(i>=0&&i<datasize, "operator[]"); 
00222                 return data[i]; }
00224   T &operator()(int i) { 
00225           //it_assert0(i>=0&&i<datasize, "operator()"); 
00226         return data[i]; }
00228   const Vec<T> operator()(int i1, int i2) const;
00230   const Vec<T> operator()(const Vec<int> &indexlist) const;
00231 
00233   void operator+=(const Vec<T> &v);
00235   void operator+=(T t) { for (int i=0;i<datasize;i++) data[i]+=t; }
00237   friend Vec<T> operator+TEMPLATE_FUN(const Vec<T> &v1, const Vec<T> &v2);
00239   friend Vec<T> operator+TEMPLATE_FUN(const Vec<T> &v, T t);
00241   friend Vec<T> operator+TEMPLATE_FUN(T t, const Vec<T> &v);
00242   
00244   void operator-=(const Vec<T> &v);
00246   void operator-=(T t) { for (int i=0;i<datasize;i++) data[i]-=t; }
00248   friend Vec<T> operator-TEMPLATE_FUN(const Vec<T> &v1, const Vec<T> &v2);
00250   friend Vec<T> operator-TEMPLATE_FUN(const Vec<T> &v, T t);
00252   friend Vec<T> operator-TEMPLATE_FUN(T t, const Vec<T> &v);
00254   friend Vec<T> operator-TEMPLATE_FUN(const Vec<T> &v);
00255   
00257   void operator*=(T t) { for (int i=0;i<datasize;i++) data[i] *= t; }
00259   friend T operator*TEMPLATE_FUN(const Vec<T> &v1, const Vec<T> &v2);
00261   friend T dot TEMPLATE_FUN(const Vec<T> &v1, const Vec<T> &v2);
00262   friend T dot_prod TEMPLATE_FUN(const Vec<T> &v1, const Vec<T> &v2);
00264   friend Mat<T> outer_product TEMPLATE_FUN(const Vec<T> &v1, const Vec<T> &v2);
00266   friend Vec<T> operator*TEMPLATE_FUN(const Vec<T> &v, T t);
00268   friend Vec<T> operator*TEMPLATE_FUN(T t, const Vec<T> &v);
00270   friend Vec<T> elem_mult TEMPLATE_FUN(const Vec<T> &v1, const Vec<T> &v2);
00272   friend Vec<T> elem_mult TEMPLATE_FUN(const Vec<T> &v1, const Vec<T> &v2, const Vec<T> &v3);  
00274   friend Vec<T> elem_mult TEMPLATE_FUN(const Vec<T> &v1, const Vec<T> &v2, const Vec<T> &v3, const Vec<T> &v4);
00275 
00277   void operator/=(T t) { for (int i=0;i<datasize;i++) data[i]/=t; }
00279   friend Vec<T> operator/TEMPLATE_FUN(const Vec<T> &v, T t);
00281   friend Vec<T> operator/TEMPLATE_FUN(const T t, const Vec<T> &v);
00283   void operator/=(const Vec<T> &v);
00285   friend Vec<T> elem_div TEMPLATE_FUN(const Vec<T> &v1, const Vec<T> &v2);
00287   friend Vec<T> elem_div TEMPLATE_FUN(const T t, const Vec<T> &v);
00288   
00290   Vec<T> get(const Vec<bin> &binlist) const;
00292   Vec<T> right(int nr) const;
00294   Vec<T> left(int nr) const;
00296   Vec<T> mid(int start, int nr) const;
00298   Vec<T> split(int pos);
00300   void shift_right(T In, int n=1);
00302   void shift_right(const Vec<T> &In);
00304   void shift_left(T In, int n=1);
00306   void shift_left(const Vec<T> &In);
00307 
00309   friend Vec<T> concat TEMPLATE_FUN(const Vec<T> &v, const T a);
00311   friend Vec<T> concat TEMPLATE_FUN(const T a, const Vec<T> &v);
00313   friend Vec<T> concat TEMPLATE_FUN(const Vec<T> &v1,const Vec<T> &v2);
00315   friend Vec<T> concat TEMPLATE_FUN(const Vec<T> &v1, const Vec<T> &v2, const Vec<T> &v3);
00316 
00318   void set_subvector(int i1, int i2, const Vec<T> &v);
00320   void set_subvector(int i1, int i2, const T t);
00322   void replace_mid(int pos, const Vec<T> &v);
00324   void del(int index);
00326   void ins(int index, T in);
00328   void ins(int index, const Vec<T> &in);
00329 
00331   void operator=(T t) { for (int i=0;i<datasize;i++) data[i] = t; }
00333   void operator=(const Vec<T> &v);
00335   void operator=(const Mat<T> &m);
00337   void operator=(const char *values) { set(values); }
00338 
00340   Vec<bin> operator==(const T value);
00342   Vec<bin> operator!=(const T value);
00344   Vec<bin> operator<(const T value);
00346   Vec<bin> operator<=(const T value);
00348   Vec<bin> operator>(const T value);
00350   Vec<bin> operator>=(const T value);
00351 
00353   bool operator==(const Vec<T> &v) const;
00355   bool operator!=(const Vec<T> &v) const;
00356 
00358   T &_elem(int i) { return data[i]; }
00360   T _elem(int i) const { return data[i]; }
00361 
00363   T *_data() { return data; }
00364 
00366   const T *_data() const { return data; }
00367     
00368  protected:
00370   void alloc(int size)
00371     {
00372       if ( datasize == size ) return;
00373 
00374       free();  // Free memory (if any allocated)
00375       if (size == 0) return;
00376     
00377       data = new T[size];
00378       datasize=size;
00379 //      it_assert1(data, "Out of memory in Vec::alloc()");
00380     }
00381 
00383   void free() { delete [] data;  init(); }
00384    
00386   int datasize;
00388   T *data;
00389 
00390  private:
00391   void init() { data = 0; datasize = 0; }
00392 };
00393 
00398 #ifdef XXX
00399 template <class T>
00400 std::ostream &operator<<(std::ostream& os, const Vec<T> &v);
00401 
00406 template <class T>
00407 std::istream &operator>>(std::istream& is, Vec<T> &v);
00408 #endif
00409 
00414 typedef Vec<double> vec;
00415 
00420 typedef Vec<complex<double> > cvec;
00421 
00426 typedef Vec<int> ivec;
00427 
00428 
00433 typedef Vec<long_long> llvec;
00434 
00439 typedef Vec<short int> svec;
00440 
00445 typedef Vec<bin> bvec;
00446 
00447 /*------------------ Inline definitions ----------------------------*/
00448 
00449 template<class T> inline
00450 const Vec<T> Vec<T>::operator()(int i1, int i2) const
00451 {
00452   if (i1 == -1) i1 = datasize-1;
00453   if (i2 == -1) i2 = datasize-1;
00454 
00455 //  it_assert1(i1>=0 && i2>=0 && i1<datasize && i2<datasize, "Vec<T>::operator()(i1,i2): indicies out of range");
00456 //  it_assert1(i2>=i1, "Vec<T>::op(i1,i2): i2 >= i1 necessary");
00457 
00458    Vec<T> s(i2-i1+1);
00459    memcpy(s.data, data+i1, s.datasize*sizeof(T));
00460 
00461    return s;
00462 }
00463 
00464 
00465 template<class T> inline
00466 Vec<T>::Vec(const Vec<T> &v)
00467 {
00468   init();
00469   alloc(v.datasize);
00470   copy_vector(datasize, v.data, data);
00471 }
00472 
00473 template<class T>
00474 void Vec<T>::set_size(int size, bool copy)
00475 {
00476         //  it_assert1(size >= 0, "New size must not be negative in Vec::set_size()");
00477   if (size!=datasize) {
00478     if (copy) {
00479       Vec<T> temp(*this);
00480     
00481       alloc(size);
00482       for (int i=0; i<size; i++)
00483         data[i] = i < temp.datasize ? temp.data[i] : T(0);
00484     } else
00485       alloc(size);
00486   }
00487 }
00488 
00489 template<class T>
00490 bool Vec<T>::set(const char *values)
00491 {
00492     istringstream buffer(values);       
00493     T b,c;
00494     int pos=0,maxpos=10;
00495   
00496     alloc(maxpos); 
00497         
00498     while(buffer.peek()!=EOF) {
00499 
00500         switch (buffer.peek()) {
00501         case ':': // reads format a:b:c or a:b
00502             buffer.get();
00503             if (!buffer.eof()) {
00504                 buffer >> b;
00505             }                    
00506             if (!buffer.eof() && buffer.peek() == ':') {
00507                 buffer.get();
00508                 if (!buffer.eof()) {
00509                     buffer >> c;
00510                                                                 
00511                     while((T)(data[pos-1]+b-c) <= (T)0) {
00512                         pos++;
00513                         if (pos > maxpos) {
00514                             maxpos=maxpos*2;
00515                             set_size(maxpos, true);
00516                         }
00517                         data[pos-1]=data[pos-2]+b;
00518                     }
00519                 }                       
00520             } else {
00521                 while(data[pos-1]<b) {
00522                     pos++;
00523                     if (pos > maxpos) {
00524                         maxpos=maxpos*2;
00525                         set_size(maxpos, true);
00526                     }
00527                     data[pos-1]= (T)data[pos-2]+(T)1;
00528                 }
00529             }
00530             break;
00531       
00532         case ',':
00533             buffer.get();
00534             break;
00535             
00536         default:
00537             pos++;
00538             if (pos > maxpos) {
00539                 maxpos *= 2;
00540                 set_size(maxpos, true);
00541             }                   
00542             buffer >> data[pos-1];
00543             break;
00544         }
00545         
00546     }
00547     set_size(pos, true);
00548 
00549     return true;
00550 }
00551 //#ifdef XXX
00552 template<class T>
00553 bool Vec<T>::set(const string &str)
00554 {
00555     return set(str.c_str());
00556 }
00557 //#endif
00558 template<class T>
00559 const Vec<T> Vec<T>::operator()(const Vec<int> &indexlist) const
00560 {
00561     Vec<T> temp(indexlist.length());
00562     for (int i=0;i<indexlist.length();i++) {
00563         // it_assert((indexlist(i)>=0) && (indexlist(i) < datasize), "Vec<T>::operator()(ivec &): index outside range");
00564         temp(i)=data[indexlist(i)];
00565     }
00566     return temp;
00567 }
00568 
00569 template<class T> inline
00570 void Vec<T>::operator+=(const Vec<T> &v)
00571 {
00572     int i;
00573 
00574     if (datasize == 0) { // if not assigned a size.
00575         alloc(v.datasize);
00576         for (i=0; i<v.datasize; i++)
00577             data[i] = v.data[i];        
00578     } else {
00579         // it_assert1(datasize==v.datasize, "Vec<T>::operator+=: wrong sizes");
00580         for (i=0; i<datasize; i++)
00581             data[i] += v.data[i];
00582     }
00583 }
00584 
00585 template<class T> inline
00586 Vec<T> operator+(const Vec<T> &v1, const Vec<T> &v2)
00587 {
00588     int i;
00589     Vec<T> r(v1.datasize);
00590 
00591     // it_assert1(v1.datasize==v2.datasize, "Vec<T>::operator+: wrong sizes");
00592     for (i=0; i<v1.datasize; i++)
00593         r.data[i] = v1.data[i] + v2.data[i];
00594 
00595     return r;
00596 }
00597 
00598 template<class T> inline
00599 Vec<T> operator+(const Vec<T> &v, T t)
00600 {
00601     int i;
00602     Vec<T> r(v.datasize);
00603 
00604     for (i=0; i<v.datasize; i++)
00605         r.data[i] = v.data[i] + t;
00606 
00607     return r;
00608 }
00609 
00610 template<class T> inline
00611 Vec<T> operator+(T t, const Vec<T> &v)
00612 {
00613     int i;
00614     Vec<T> r(v.datasize);
00615 
00616     for (i=0; i<v.datasize; i++)
00617         r.data[i] = t + v.data[i];
00618 
00619     return r;
00620 }
00621 
00622 template<class T> inline
00623 void Vec<T>::operator-=(const Vec<T> &v)
00624 {
00625     int i;
00626 
00627     if (datasize == 0) { // if not assigned a size.
00628         alloc(v.datasize);
00629         for (i=0; i<v.datasize; i++)
00630             data[i] = -v.data[i];      
00631     } else {
00632         // it_assert1(datasize==v.datasize, "Vec<T>::operator-=: wrong sizes");
00633         for (i=0; i<datasize; i++)
00634             data[i] -= v.data[i];
00635     }
00636 }
00637 
00638 template<class T> inline
00639 Vec<T> operator-(const Vec<T> &v1, const Vec<T> &v2)
00640 {
00641     int i;
00642     Vec<T> r(v1.datasize);
00643 
00644     // it_assert1(v1.datasize==v2.datasize, "Vec<T>::operator-: wrong sizes");
00645     for (i=0; i<v1.datasize; i++)
00646         r.data[i] = v1.data[i] - v2.data[i];
00647 
00648     return r;
00649 }
00650 
00651 template<class T> inline
00652 Vec<T> operator-(const Vec<T> &v, T t)
00653 {
00654     int i;
00655     Vec<T> r(v.datasize);
00656 
00657     for (i=0; i<v.datasize; i++)
00658         r.data[i] = v.data[i] - t;
00659 
00660     return r;
00661 }
00662 
00663 template<class T> inline
00664 Vec<T> operator-(T t, const Vec<T> &v)
00665 {
00666     int i;
00667     Vec<T> r(v.datasize);
00668 
00669     for (i=0; i<v.datasize; i++)
00670         r.data[i] = t - v.data[i];
00671 
00672     return r;
00673 }
00674 
00675 template<class T> inline
00676 Vec<T> operator-(const Vec<T> &v)
00677 {
00678     int i;
00679     Vec<T> r(v.datasize);
00680 
00681     for (i=0; i<v.datasize; i++)
00682         r.data[i] = -v.data[i];
00683 
00684     return r;
00685 }
00686  
00687 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00688 
00689 template<class T> inline
00690 T dot(const Vec<T> &v1, const Vec<T> &v2)
00691 {
00692     int i;
00693     T r=T(0);
00694 
00695     // it_assert1(v1.datasize==v2.datasize, "Vec<T>::dot: wrong sizes");
00696     for (i=0; i<v1.datasize; i++)
00697         r += v1.data[i] * v2.data[i];
00698 
00699     return r;
00700 }
00701 
00702 #endif //DOXYGEN_SHOULD_SKIP_THIS
00703 
00704 template<class T> inline
00705 Mat<T> outer_product(const Vec<T> &v1, const Vec<T> &v2)
00706 {
00707     int i, j;
00708 
00709     // it_assert1(v1.datasize>0 && v2.datasize>0, "outer_product:: Vector of zero size");
00710 
00711     Mat<T> r(v1.datasize, v2.datasize);
00712 
00713     for (i=0; i<v1.datasize; i++) {
00714       for (j=0; j<v2.datasize; j++) {
00715         r(i,j) = v1.data[i] * v2.data[j];
00716       }
00717     }
00718 
00719     return r;
00720 }
00721 
00722 /* This is currently not fast enough !!! 
00723 #ifdef HAVE_CBLAS  // double and complex specializations using CBLAS
00724 template<>
00725 vec operator*(const vec &v, const double t)
00726 {
00727   vec r(v.size());
00728   cblas_dcopy(v.size(), v._data(), 1, r._data(), 1);
00729 
00730   cblas_dscal(r.size(), t, r._data(), 1);
00731 
00732   return r;
00733 }
00734 
00735 template<>
00736 cvec operator*(const cvec &v, const double_complex t)
00737 {
00738   cvec r(v);
00739 
00740   cblas_zscal(r.size(), (const void *)&t, r._data(), 1);
00741 
00742   return r;
00743 }
00744 #endif
00745 */
00746 
00747 template<class T> inline
00748 Vec<T> operator*(const Vec<T> &v, T t)
00749 {
00750     int i;
00751     Vec<T> r(v.datasize);
00752 
00753     for (i=0; i<v.datasize; i++)
00754         r.data[i] = v.data[i] * t;
00755 
00756     return r;
00757 }
00758 
00759 template<class T> inline
00760 Vec<T> operator*(T t, const Vec<T> &v)
00761 {
00762     int i;
00763     Vec<T> r(v.datasize);
00764 
00765     for (i=0; i<v.datasize; i++)
00766         r.data[i] = t * v.data[i];
00767 
00768     return r;
00769 }
00770 
00771 template<class T> inline
00772 Vec<T> elem_mult(const Vec<T> &v1, const Vec<T> &v2)
00773 {
00774     int i;
00775     Vec<T> r(v1.datasize);
00776 
00777     // it_assert1(v1.datasize==v2.datasize, "Vec<T>::elem_mult: wrong sizes");
00778     for (i=0; i<v1.datasize; i++)
00779         r.data[i] = v1.data[i] * v2.data[i];
00780 
00781     return r;
00782 }
00783 
00784 template<class T> inline
00785 Vec<T> elem_mult(const Vec<T> &v1, const Vec<T> &v2, const Vec<T> &v3)
00786 {
00787     int i;
00788     Vec<T> r(v1.datasize);
00789 
00790     // it_assert1(v1.datasize==v2.datasize, "Vec<T>::elem_mult: wrong sizes");
00791     // it_assert1(v2.datasize==v3.datasize, "Vec<T>::elem_mult: wrong sizes");
00792     for (i=0; i<v1.datasize; i++)
00793         r.data[i] = v1.data[i] * v2.data[i] * v3.data[i];
00794 
00795     return r;
00796 }
00797 
00798 template<class T> inline
00799 Vec<T> elem_mult(const Vec<T> &v1, const Vec<T> &v2, const Vec<T> &v3, const Vec<T> &v4)
00800 {
00801     int i;
00802     Vec<T> r(v1.datasize);
00803 
00804     // it_assert1(v1.datasize==v2.datasize, "Vec<T>::elem_mult: wrong sizes");
00805     // it_assert1(v2.datasize==v3.datasize, "Vec<T>::elem_mult: wrong sizes");
00806     // it_assert1(v3.datasize==v4.datasize, "Vec<T>::elem_mult: wrong sizes");
00807     for (i=0; i<v1.datasize; i++)
00808         r.data[i] = v1.data[i] * v2.data[i] * v3.data[i] * v4.data[i];
00809 
00810     return r;
00811 }
00812 
00813 template<class T> inline
00814 Vec<T> operator/(const Vec<T> &v, T t)
00815 {
00816     int i;
00817     Vec<T> r(v.datasize);
00818 
00819     for (i=0; i<v.datasize; i++)
00820         r.data[i] = v.data[i] / t;
00821 
00822     return r;
00823 }
00824 
00825 template<class T> inline
00826 Vec<T> operator/(const T t, const Vec<T> &v)
00827 {
00828     int i;
00829     Vec<T> r(v.datasize);
00830 
00831     for (i=0; i<v.datasize; i++)
00832         r.data[i] = t / v.data[i];
00833 
00834     return r;
00835 }
00836 
00837 template<class T> inline
00838 void Vec<T>::operator/=(const Vec<T> &v)
00839 {
00840     int i;
00841 
00842     // it_assert1(datasize==v.datasize, "Vec<T>::operator/=: wrong sizes");
00843     for (i=0; i<datasize; i++)
00844         data[i] /= v.data[i];
00845 }
00846 
00847 template<class T> inline
00848 Vec<T> elem_div(const Vec<T> &v1, const Vec<T> &v2)
00849 {
00850     int i;
00851     Vec<T> r(v1.datasize);
00852 
00853     // it_assert1(v1.datasize==v2.datasize, "elem_div: wrong sizes");
00854     for (i=0; i<v1.datasize; i++)
00855         r.data[i] = v1.data[i] / v2.data[i];
00856 
00857     return r;
00858 }
00859 
00860 template<class T> inline
00861 Vec<T> elem_div(const T t, const Vec<T> &v)
00862 {
00863     int i;
00864     Vec<T> r(v.datasize);
00865 
00866     for (i=0; i<v.datasize; i++)
00867         r.data[i] = t / v.data[i];
00868 
00869     return r;
00870 }
00871 
00872 template<class T>
00873 Vec<T> Vec<T>::get(const Vec<bin> &binlist) const
00874 {
00875     // it_assert1(datasize == binlist.size(), "Vec<T>::get(bvec &): wrong sizes");
00876     Vec<T> temp(binlist.length());
00877     int j=0;
00878   
00879     for (int i=0;i<binlist.length();i++) {
00880         if (binlist(i) == bin(1)) {
00881             temp(j)=data[i];
00882             j++;
00883         }
00884     }
00885     temp.set_size(j, true);
00886     return temp;
00887 }
00888 
00889 template<class T> inline
00890 Vec<T> Vec<T>::right(int nr) const
00891 {
00892     // it_assert1(nr<=datasize, "Vec<T>::right: index out of range");
00893     Vec<T> temp(nr);
00894     if (nr!=0) {
00895       copy_vector(nr, &data[datasize-nr], &temp[0]);
00896     }
00897     return temp;
00898 }
00899 
00900 template<class T> inline
00901 Vec<T> Vec<T>::left(int nr) const
00902 {
00903     // it_assert1(nr<=datasize, "Vec<T>::left: index out of range");
00904     Vec<T> temp(nr);
00905     if (nr!=0) {
00906       copy_vector(nr, &data[0], &temp[0]);
00907     }
00908     return temp;
00909 }
00910 
00911 template<class T> inline
00912 Vec<T> Vec<T>::mid(int start, int nr) const
00913 {
00914     // it_assert1((start>=0)&& ((start+nr)<=datasize), "Vec<T>::mid: indexing out of range");
00915     Vec<T> temp(nr);
00916 
00917     if (nr!=0) {
00918       copy_vector(nr, &data[start], &temp[0]);
00919     }
00920     return temp;
00921 }
00922 
00923 template<class T>
00924 Vec<T> Vec<T>::split(int Position)
00925 {
00926     // it_assert1((Position>=0) && (Position<=datasize), "Vec<T>::split: index out of range");
00927     Vec<T> Temp1(Position);
00928     Vec<T> Temp2(datasize-Position);
00929     int  i;
00930 
00931     for (i=0;i<Position;i++) {
00932         Temp1[i]=data[i];
00933     }
00934     for (i=Position;i<datasize;i++) {
00935         Temp2[i-Position]=data[i];
00936     }
00937     (*this)=Temp2;
00938     return Temp1;
00939 }
00940 
00941 template<class T>
00942 void Vec<T>::shift_right(T In, int n)
00943 {
00944     int i=datasize;
00945 
00946     // it_assert1(n>=0, "Vec<T>::shift_right: index out of range");
00947     while (--i >= n)
00948         data[i] = data[i-n];
00949     while (i >= 0)
00950         data[i--] = In;
00951 }
00952 
00953 template<class T>
00954 void Vec<T>::shift_right(const Vec<T> &In)
00955 {
00956     int i;
00957 
00958     for (i=datasize-1; i>=In.datasize; i--)
00959         data[i]=data[i-In.datasize];
00960     for (i=0; i<In.datasize; i++)
00961         data[i]=In[i];
00962 }
00963 
00964 template<class T>
00965 void Vec<T>::shift_left(T In, int n)
00966 {
00967     int i;
00968 
00969     // it_assert1(n>=0, "Vec<T>::shift_left: index out of range");
00970     for (i=0; i<datasize-n; i++) 
00971         data[i] = data[i+n];
00972     while (i < datasize)
00973         data[i++] = In;
00974 }
00975 
00976 template<class T>
00977 void Vec<T>::shift_left(const Vec<T> &In)
00978 {
00979     int i;
00980 
00981     for (i=0; i<datasize-In.datasize; i++)
00982         data[i]=data[i+In.datasize];
00983     for (i=datasize-In.datasize; i<datasize; i++)
00984         data[i]=In[i-datasize+In.datasize];
00985 }
00986 
00987 template<class T>
00988 Vec<T> concat(const Vec<T> &v, const T a)
00989 {
00990     Vec<T> temp(v.size()+1);
00991 
00992     for (int i=0; i<v.size(); i++)
00993         temp(i) = v(i);
00994     temp(v.size()) = a;
00995 
00996     return temp;
00997 }
00998 
00999 template<class T>
01000 Vec<T> concat(const T a, const Vec<T> &v)
01001 {
01002     Vec<T> temp(v.size()+1);
01003 
01004     temp(0) = a;
01005 
01006     for (int i=0; i<v.size(); i++)
01007         temp(i+1) = v(i);
01008 
01009     return temp;
01010 }
01011 
01012 template<class T>
01013 Vec<T> concat(const Vec<T> &v1, const Vec<T> &v2)
01014 {
01015     int i;
01016     Vec<T> temp(v1.size()+v2.size());
01017 
01018     for (i=0;i<v1.size();i++) {
01019         temp[i] = v1[i];
01020     }
01021     for (i=0;i<v2.size();i++) {
01022         temp[v1.size()+i] = v2[i];
01023     }
01024     return temp;
01025 }
01026 
01027 template<class T>
01028 Vec<T> concat(const Vec<T> &v1, const Vec<T> &v2, const Vec<T> &v3)
01029 {
01030   // There should be some error control?
01031     int i;
01032     Vec<T> temp(v1.size()+v2.size()+v3.size());
01033 
01034     for (i=0;i<v1.size();i++) {
01035         temp[i] = v1[i];
01036     }
01037     for (i=0;i<v2.size();i++) {
01038         temp[v1.size()+i] = v2[i];
01039     }
01040     for (i=0;i<v3.size();i++) {
01041         temp[v1.size()+v2.size()+i] = v3[i];
01042     }
01043     return temp;
01044 }
01045 
01046 template<class T>
01047 Vec<T> concat(const Vec<T> &v1, const Vec<T> &v2, const Vec<T> &v3, const Vec<T> &v4)
01048 {
01049   // There should be some error control?
01050     int i;
01051     Vec<T> temp(v1.size()+v2.size()+v3.size()+v4.size());
01052 
01053     for (i=0;i<v1.size();i++) {
01054         temp[i] = v1[i];
01055     }
01056     for (i=0;i<v2.size();i++) {
01057         temp[v1.size()+i] = v2[i];
01058     }
01059     for (i=0;i<v3.size();i++) {
01060         temp[v1.size()+v2.size()+i] = v3[i];
01061     }
01062     for (i=0;i<v4.size();i++) {
01063         temp[v1.size()+v2.size()+v3.size()+i] = v4[i];
01064     }
01065     return temp;
01066 }
01067 
01068 template<class T>
01069 Vec<T> concat(const Vec<T> &v1, const Vec<T> &v2, const Vec<T> &v3, const Vec<T> &v4, const Vec<T> &v5)
01070 {
01071   // There should be some error control?
01072     int i;
01073     Vec<T> temp(v1.size()+v2.size()+v3.size()+v4.size()+v5.size());
01074 
01075     for (i=0;i<v1.size();i++) {
01076         temp[i] = v1[i];
01077     }
01078     for (i=0;i<v2.size();i++) {
01079         temp[v1.size()+i] = v2[i];
01080     }
01081     for (i=0;i<v3.size();i++) {
01082         temp[v1.size()+v2.size()+i] = v3[i];
01083     }
01084     for (i=0;i<v4.size();i++) {
01085         temp[v1.size()+v2.size()+v3.size()+i] = v4[i];
01086     }
01087     for (i=0;i<v5.size();i++) {
01088         temp[v1.size()+v2.size()+v3.size()+v4.size()+i] = v5[i];
01089     }
01090     return temp;
01091 }
01092 
01093 template<class T> inline
01094 void Vec<T>::set_subvector(int i1, int i2, const Vec<T> &v)
01095 {
01096   if (i1 == -1) i1 = datasize-1;
01097   if (i2 == -1) i2 = datasize-1;
01098   
01099   // it_assert1(i1>=0 && i2>=0 && i1<datasize && i2<datasize, "Vec<T>::set_subvector(): indicies out of range");
01100   // it_assert1(i2>=i1, "Vec<T>::set_subvector(): i2 >= i1 necessary");
01101   // it_assert1(i2-i1+1 == v.datasize, "Vec<T>::set_subvector(): wrong sizes");
01102   
01103   copy_vector(v.datasize, v.data, data+i1);
01104 }
01105 
01106 template<class T>
01107 void Vec<T>::set_subvector(int i1, int i2, const T t)
01108 {
01109   if (i1 == -1) i1 = datasize-1;
01110   if (i2 == -1) i2 = datasize-1;
01111   
01112   // it_assert1(i1>=0 && i2>=0 && i1<datasize && i2<datasize, "Vec<T>::set_subvector(): indicies out of range");
01113   // it_assert1(i2>=i1, "Vec<T>::set_subvector(): i2 >= i1 necessary");
01114 
01115   for (int i=i1;i<=i2;i++)
01116     data[i] = t;
01117 }
01118 
01119 template<class T>
01120 void Vec<T>::replace_mid(int pos, const Vec<T> &v)
01121 {
01122     // it_assert1((pos>=0) && ((pos+v.length())<=datasize), "Vec<T>::replace_mid: indexing out of range");
01123     copy_vector(v.datasize, v.data, &data[pos]);
01124 }
01125 
01126 template<class T>
01127 void Vec<T>::del(int index)
01128 {
01129     // it_assert1((index>=0) && (index<datasize), "Vec<T>::del: index out of range");
01130     Vec<T> Temp(*this);
01131     int i;
01132 
01133     set_size(datasize-1, false);
01134     for (i=0;i<index;i++) {
01135         data[i]=Temp[i];
01136     }
01137     for (i=index;i<datasize;i++) {
01138         data[i]=Temp[i+1];
01139     }
01140 }
01141 
01142 template<class T>
01143 void Vec<T>::ins(int index, T in)
01144 {
01145     // it_assert1((index>=0) && (index<datasize), "Vec<T>::ins: index out of range");
01146     Vec<T> Temp(*this);
01147 
01148     set_size(datasize+1, false);
01149 
01150     copy_vector(index, Temp.data, data);
01151     data[index]=in;
01152     copy_vector(Temp.datasize-index, &Temp[index], &data[index+1]);
01153 }
01154 
01155 template<class T>
01156 void Vec<T>::ins(int index, const Vec<T> &in)
01157 {
01158     // it_assert1((index>=0) && (index<datasize), "Vec<T>::ins: index out of range");
01159     Vec<T> Temp(*this);
01160 
01161     set_size(datasize+in.length(), false);
01162     copy_vector(index, Temp.data, data);
01163     copy_vector(in.size(), in.data, &data[index]);
01164     copy_vector(Temp.datasize-index, &Temp[index], &data[index+in.size()]);
01165 }
01166 
01167 template<class T> inline
01168 void Vec<T>::operator=(const Vec<T> &v)
01169 {
01170   set_size(v.datasize, false);
01171   copy_vector(datasize, v.data, data);
01172 }
01173 
01174 template<class T> inline
01175 void Vec<T>::operator=(const Mat<T> &m)
01176 {
01177     // it_assert1( (m.cols() == 1 && datasize == m.rows()) ||
01178         //              (m.rows() == 1 && datasize == m.cols()), "vec::op=(mat); wrong size");
01179 
01180     if (m.cols() == 1) {
01181       set_size(m.rows(), false);
01182       copy_vector(m.rows(), m._data(), data);
01183     } else if (m.rows() == 1) {
01184       set_size(m.cols(), false);
01185       copy_vector(m.cols(), m._data(), m.rows(), data, 1);
01186     }
01187         //      else it_error("vec::op=(mat); wrong size");
01188 }
01189 
01190 // Elementwise comparisons with a scalar T
01191 
01192 template<class T>
01193 bvec Vec<T>::operator==(const T value)
01194 {
01195     // it_assert(datasize > 0, "Vec<T>::operator==: vector must have size > 0");
01196     Vec<T> invector(*this);
01197     bvec temp(invector.length());
01198 
01199     for (int i=0;i<invector.length();i++)
01200         temp(i)=(invector(i)==value);
01201 
01202     return temp;
01203 }
01204 
01205 template<class T>
01206 bvec Vec<T>::operator!=(const T value)
01207 {
01208     // it_assert(datasize > 0, "Vec<T>::operator!=: vector must have size > 0");
01209     Vec<T> invector(*this);
01210     bvec temp(invector.length());
01211 
01212     for (int i=0;i<invector.length();i++)
01213         temp(i)=(invector(i)!=value);
01214 
01215     return temp;
01216 }
01217 
01218 template<class T>
01219 bvec Vec<T>::operator<(const T value)
01220 {
01221     // it_assert(datasize > 0, "Vec<T>::operator<: vector must have size > 0");
01222     Vec<T> invector(*this);
01223     bvec temp(invector.length());
01224 
01225     for (int i=0;i<invector.length();i++)
01226         temp(i)=(invector(i)<value);
01227 
01228     return temp;
01229 }
01230 
01231 template<class T>
01232 bvec Vec<T>::operator<=(const T value)
01233 {
01234     // it_assert(datasize > 0, "Vec<T>::operator<=: vector must have size > 0");
01235     Vec<T> invector(*this);
01236     bvec temp(invector.length());
01237 
01238     for (int i=0;i<invector.length();i++)
01239         temp(i)=(invector(i)<=value);
01240 
01241     return temp;
01242 }
01243 
01244 template<class T>
01245 bvec Vec<T>::operator>(const T value)
01246 {
01247     // it_assert(datasize > 0, "Vec<T>::operator>: vector must have size > 0");
01248     Vec<T> invector(*this);
01249     bvec temp(invector.length());
01250 
01251     for (int i=0;i<invector.length();i++)
01252         temp(i)=(invector(i)>value);
01253 
01254     return temp;
01255 }
01256 
01257 template<class T>
01258 bvec Vec<T>::operator>=(const T value)
01259 {
01260     // it_assert(datasize > 0, "Vec<T>::operator>=: vector must have size > 0");
01261     Vec<T> invector(*this);
01262     bvec temp(invector.length());
01263 
01264     for (int i=0;i<invector.length();i++)
01265         temp(i)=(invector(i)>=value);
01266 
01267     return temp;
01268 }
01269 
01270 template<class T>
01271 bool Vec<T>::operator==(const Vec<T> &invector) const
01272 {
01273 // OBS ! if wrong size, return false
01274     if (datasize!=invector.datasize) return false;
01275     for (int i=0;i<datasize;i++) {
01276         if (data[i]!=invector.data[i]) return false;
01277     }
01278     return true;
01279 }
01280 
01281 template<class T>
01282 bool Vec<T>::operator!=(const Vec<T> &invector) const
01283 {
01284     if (datasize!=invector.datasize) return true;
01285     for (int i=0;i<datasize;i++) {
01286         if (data[i]!=invector.data[i]) return true;
01287     }
01288     return false;
01289 }
01290 
01291 #ifndef DOXYGEN_SHOULD_SKIP_THIS
01292 
01293 template <class T>
01294 ostream &operator<<(ostream& os, const Vec<T> &v)
01295 {
01296     int i, sz=v.length();
01297   
01298     os << "[" ;
01299     for (i=0; i<sz; i++) {
01300         os << v(i);
01301         if (i < sz-1)
01302             os << " ";
01303     }
01304     os << "]" ;
01305 
01306     return os;
01307 }
01308 
01309 template <class T>
01310 istream &operator>>(istream& is, Vec<T> &v)
01311 {
01312     string str;
01313 
01314     getline(is, str);
01315     if (is.eof())
01316         v.set_size(0, false);
01317     else
01318         v.set(str);
01319 
01320     return is;
01321 }
01322 
01323 #endif //DOXYGEN_SHOULD_SKIP_THIS
01324 
01325 
01326 #ifndef DOXYGEN_SHOULD_SKIP_THIS
01327 
01328 template<class T> void copy_vector(const int n, const T *x, T *y);
01329 template<class T> void copy_vector(const int n, const T *x, const int incx, T *y, const int incy);
01330 template<class T> void swap_vector(const int n, T *x, T *y);
01331 template<class T> void swap_vector(const int n, T *x, const int incx, T *y, const int incy);
01332 
01333 /*
01334   Copy vector x to vector y. Both vectors are of size n
01335 */
01336 #ifdef HAVE_CBLAS
01337 template<> inline
01338 void copy_vector(const int n, const double *x, double *y)
01339 {
01340   cblas_dcopy(n, x, 1, y, 1);   
01341 }
01342 
01343 template<> inline
01344 void copy_vector(const int n, const double_complex *x, double_complex *y)
01345 {
01346   cblas_zcopy(n, x, 1, y, 1);           
01347 }
01348 #endif
01349 
01350 template<class T> inline
01351 void copy_vector(const int n, const T *x, T *y)
01352 {
01353   memcpy(y, x, (unsigned int)n*sizeof(T));
01354 }
01355 
01356 /*
01357   Copy vector x to vector y. Both vectors are of size n
01358   vector x elements are stored linearly with element increament incx
01359   vector y elements are stored linearly with element increament incx
01360 */
01361 #ifdef HAVE_CBLAS
01362 template<> inline
01363 void copy_vector(const int n, const double *x, const int incx, double *y, const int incy)
01364 {
01365   cblas_dcopy(n, x, incx, y, incy);     
01366 }
01367 
01368 template<> inline
01369 void copy_vector(const int n, const double_complex *x, const int incx, double_complex *y, const int incy)
01370 {
01371   cblas_zcopy(n, x, incx, y, incy);             
01372 }
01373 #endif
01374 
01375 template<class T> inline
01376 void copy_vector(const int n, const T *x, const int incx, T *y, const int incy)
01377 {
01378   for (int i=0;i<n; i++)
01379     y[i*incy] = x[i*incx];
01380 }
01381 
01382 /*
01383   Swap vector x to vector y. Both vectors are of size n
01384 */
01385 
01386 #ifdef HAVE_CBLAS
01387 template<> inline
01388 void swap_vector(const int n, double *x, double *y)
01389 {
01390   cblas_dswap(n, x, 1, y, 1);   
01391 }
01392 
01393 template<> inline
01394 void swap_vector(const int n, double_complex *x, double_complex *y)
01395 {
01396   cblas_zswap(n, x, 1, y, 1);           
01397 }
01398 #endif
01399 
01400 template<class T> inline
01401 void swap_vector(const int n, T *x, T *y)
01402 {
01403   for (int i=0; i<n; i++)
01404      swap(x[i], y[i]);
01405 }
01406 
01407 /*
01408   Swap vector x to vector y. Both vectors are of size n
01409   vector x elements are stored linearly with element increament incx
01410   vector y elements are stored linearly with element increament incx
01411 */
01412 #ifdef HAVE_CBLAS
01413 template<> inline
01414 void swap_vector(const int n, double *x, const int incx, double *y, const int incy)
01415 {
01416   cblas_dswap(n, x, incx, y, incy);     
01417 }
01418 
01419 template<> inline
01420 void swap_vector(const int n, double_complex *x, const int incx, double_complex *y, const int incy)
01421 {
01422   cblas_zswap(n, x, incx, y, incy);             
01423 }
01424 #endif
01425 
01426 template<class T> inline
01427 void swap_vector(const int n, T *x, const int incx, T *y, const int incy)
01428 {
01429         //  void swap(int *a, int *b);
01430   for (int i=0; i<n; i++)
01431           swap(x[i*incx], y[i*incy]);
01432 }
01433 
01434 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
01435 } //
01436 #endif

Generated on Fri Sep 16 11:02:30 2005 for spuc by  doxygen 1.4.4