| [3661] | 1 | // $Id: tvector.cc,v 1.23 2009-10-23 19:48:27 cmv Exp $ | 
|---|
| [762] | 2 | //                         C.Magneville          04/99 | 
|---|
| [2615] | 3 | #include "sopnamsp.h" | 
|---|
| [762] | 4 | #include "machdefs.h" | 
|---|
|  | 5 | #include <stdlib.h> | 
|---|
|  | 6 | #include "pexceptions.h" | 
|---|
|  | 7 | #include "tvector.h" | 
|---|
|  | 8 |  | 
|---|
| [926] | 9 | /*! | 
|---|
|  | 10 | \class SOPHYA::TVector | 
|---|
|  | 11 | \ingroup TArray | 
|---|
|  | 12 |  | 
|---|
| [2267] | 13 | The TVector class specializes the TMatrix class for representing | 
|---|
|  | 14 | row or column vectors. | 
|---|
|  | 15 |  | 
|---|
|  | 16 | \b Vector is a typedef for double precision floating point vectors ( TVector<r_8> ). | 
|---|
|  | 17 |  | 
|---|
|  | 18 | \sa SOPHYA::TArray SOPHYA::TMatrix | 
|---|
|  | 19 | \sa SOPHYA::Range SOPHYA::Sequence | 
|---|
|  | 20 | \sa SOPHYA::MathArray | 
|---|
|  | 21 |  | 
|---|
|  | 22 | The following sample code illustrates sub-vector manipulation: | 
|---|
|  | 23 | \code | 
|---|
|  | 24 | #include "array.h" | 
|---|
|  | 25 | // ... | 
|---|
|  | 26 | // Input Vector containing a noisy periodic signal | 
|---|
|  | 27 | Vector in(1024), out(1024); | 
|---|
|  | 28 | in = RandomSequence(RandomSequence::Gaussian, 0., 1.); | 
|---|
|  | 29 | for(int kk=0; kk<in.Size(); kk++) | 
|---|
|  | 30 | in(kk) += 2*sin(kk*0.05); | 
|---|
|  | 31 | // Compute the output vector by a simple low pass filter | 
|---|
|  | 32 | Vector out(1024); | 
|---|
|  | 33 | int w = 2; | 
|---|
|  | 34 | for(int k=w; k<in.Size()-w; k++) | 
|---|
|  | 35 | out(k) = in(Range(k-w, k+w).Sum()/(2.*w+1.); | 
|---|
|  | 36 | \endcode | 
|---|
|  | 37 | */ | 
|---|
|  | 38 |  | 
|---|
| [804] | 39 | //////////////////////////////////////////////////////////////// | 
|---|
|  | 40 | //**** Createur, Destructeur | 
|---|
|  | 41 |  | 
|---|
| [894] | 42 | //! Default constructor | 
|---|
| [762] | 43 | template <class T> | 
|---|
| [804] | 44 | TVector<T>::TVector() | 
|---|
|  | 45 | : TMatrix<T>() | 
|---|
| [762] | 46 | { | 
|---|
| [1560] | 47 | arrtype_ = 2;   // Type = Vector | 
|---|
| [762] | 48 | } | 
|---|
|  | 49 |  | 
|---|
| [894] | 50 | //! construct a vector | 
|---|
|  | 51 | /*! | 
|---|
|  | 52 | \param n : number of elements | 
|---|
|  | 53 | \param lcv : line or column vector ? | 
|---|
|  | 54 | \param mm : memory mapping type | 
|---|
| [2575] | 55 | \param fzero : if \b true , set vector elements to zero | 
|---|
| [894] | 56 | \sa SelectVectorType | 
|---|
|  | 57 | */ | 
|---|
| [762] | 58 | template <class T> | 
|---|
| [2575] | 59 | TVector<T>::TVector(sa_size_t n, short lcv, short mm, bool fzero) | 
|---|
| [804] | 60 | // Constructeur | 
|---|
| [2575] | 61 | : TMatrix<T>(1,1,mm,false) | 
|---|
| [762] | 62 | { | 
|---|
| [1099] | 63 | arrtype_ = 2;   // Type = Vector | 
|---|
| [813] | 64 | lcv = SelectVectorType(lcv); | 
|---|
| [2575] | 65 | ReSize(n,lcv,fzero); | 
|---|
| [762] | 66 | } | 
|---|
|  | 67 |  | 
|---|
| [967] | 68 | //! Constructor by copy | 
|---|
| [976] | 69 | /*! | 
|---|
|  | 70 | \warning datas are \b SHARED with \b a. | 
|---|
|  | 71 | \sa NDataBlock::NDataBlock(const NDataBlock<T>&) | 
|---|
|  | 72 | */ | 
|---|
| [762] | 73 | template <class T> | 
|---|
| [804] | 74 | TVector<T>::TVector(const TVector<T>& a) | 
|---|
| [967] | 75 | // Constructeur par copie | 
|---|
| [804] | 76 | : TMatrix<T>(a) | 
|---|
| [762] | 77 | { | 
|---|
| [1099] | 78 | arrtype_ = 2;   // Type = Vector | 
|---|
| [762] | 79 | } | 
|---|
|  | 80 |  | 
|---|
| [894] | 81 | //! Constructor by copy | 
|---|
|  | 82 | /*! | 
|---|
|  | 83 | \param share : if true, share data. If false copy data | 
|---|
|  | 84 | */ | 
|---|
| [762] | 85 | template <class T> | 
|---|
| [804] | 86 | TVector<T>::TVector(const TVector<T>& a, bool share) | 
|---|
| [762] | 87 | // Constructeur par copie avec possibilite de forcer le partage ou non. | 
|---|
| [804] | 88 | : TMatrix<T>(a, share) | 
|---|
| [762] | 89 | { | 
|---|
| [1099] | 90 | arrtype_ = 2;   // Type = Vector | 
|---|
| [762] | 91 | } | 
|---|
|  | 92 |  | 
|---|
|  | 93 |  | 
|---|
| [894] | 94 | //! Constructor of a vector from a TArray \b a | 
|---|
|  | 95 | /*! | 
|---|
|  | 96 | \param a : TArray to be copied or shared | 
|---|
|  | 97 | \param share : if true, share data. If false copy data | 
|---|
|  | 98 | \param lcv : line or column vector ? | 
|---|
|  | 99 | \sa SelectVectorType | 
|---|
|  | 100 | */ | 
|---|
| [762] | 101 | template <class T> | 
|---|
| [2752] | 102 | TVector<T>::TVector(const TArray<T>& a, bool share, short lcv) | 
|---|
|  | 103 | : TMatrix<T>(a, share) | 
|---|
| [762] | 104 | { | 
|---|
| [813] | 105 | if ( (size_[0] != 1) && (size_[1] != 1) ) | 
|---|
|  | 106 | throw SzMismatchError("TVector<T>::TVector(const TArray<T>& a) NRows()!=1 && NCols()!=1 "); | 
|---|
| [1099] | 107 | arrtype_ = 2;   // Type = Vector | 
|---|
| [813] | 108 | if ( (size_[0] == 1) && (size_[1] == 1) ) { | 
|---|
|  | 109 | if (lcv == SameVectorType) lcv = a.GetVectorType(); | 
|---|
|  | 110 | if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType(); | 
|---|
|  | 111 | veceli_ = (lcv ==  ColumnVector ) ?  marowi_ : macoli_; | 
|---|
| [804] | 112 | } | 
|---|
| [762] | 113 | } | 
|---|
|  | 114 |  | 
|---|
| [1099] | 115 | //! Constructor of a vector from a TArray \b a , with a different data type | 
|---|
|  | 116 | template <class T> | 
|---|
|  | 117 | TVector<T>::TVector(const BaseArray& a) | 
|---|
| [1103] | 118 | : TMatrix<T>(a) | 
|---|
| [1099] | 119 | { | 
|---|
| [1103] | 120 | if ( (size_[0] != 1) && (size_[1] != 1) ) | 
|---|
|  | 121 | throw SzMismatchError("TVector<T>::TVector(const BaseArray& a) NRows()!=1 && NCols()!=1 "); | 
|---|
| [1099] | 122 | arrtype_ = 2;   // Type = Vector | 
|---|
|  | 123 | } | 
|---|
|  | 124 |  | 
|---|
| [2719] | 125 | //! Constructor from a STL vector | 
|---|
|  | 126 | template <class T> | 
|---|
|  | 127 | TVector<T>::TVector(const vector<T>& v, short lcv) | 
|---|
|  | 128 | { | 
|---|
|  | 129 | sa_size_t n = v.size(); | 
|---|
|  | 130 | if(n==0) | 
|---|
|  | 131 | throw SzMismatchError("TVector<T>::TVector(const vector<T>& v) size()==0 "); | 
|---|
|  | 132 | arrtype_ = 2;   // Type = Vector | 
|---|
|  | 133 | ReSize(n,lcv,false); | 
|---|
|  | 134 | for(sa_size_t i=0;i<n;i++) (*this)(i) = v[i]; | 
|---|
|  | 135 | } | 
|---|
|  | 136 |  | 
|---|
| [894] | 137 | //! Destructor | 
|---|
| [762] | 138 | template <class T> | 
|---|
| [804] | 139 | TVector<T>::~TVector() | 
|---|
| [762] | 140 | { | 
|---|
|  | 141 | } | 
|---|
|  | 142 |  | 
|---|
| [894] | 143 | //! Resize the vector | 
|---|
|  | 144 | /*! | 
|---|
|  | 145 | \param n : number of elements | 
|---|
|  | 146 | \param lcv : line or column vector ? | 
|---|
|  | 147 | \sa SelectVectorType | 
|---|
|  | 148 | */ | 
|---|
| [762] | 149 | template <class T> | 
|---|
| [2575] | 150 | void TVector<T>::ReSize(sa_size_t n, short lcv, bool fzero) | 
|---|
| [762] | 151 | { | 
|---|
| [804] | 152 | if( n == 0 ) | 
|---|
|  | 153 | throw(SzMismatchError("TVector::ReSize() n = 0 ")); | 
|---|
| [1156] | 154 | sa_size_t r,c; | 
|---|
| [813] | 155 | if (lcv == SameVectorType)  lcv = GetVectorType(); | 
|---|
|  | 156 | else if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType(); | 
|---|
| [804] | 157 | if (lcv == ColumnVector) { r = n;  c = 1; } | 
|---|
|  | 158 | else { c = n; r = 1; } | 
|---|
| [2575] | 159 | TMatrix<T>::ReSize(r,c,BaseArray::SameMemoryMapping,fzero); | 
|---|
| [813] | 160 | veceli_ = (lcv ==  ColumnVector ) ?  marowi_ : macoli_; | 
|---|
| [762] | 161 | } | 
|---|
|  | 162 |  | 
|---|
| [894] | 163 | //! Re-allocate space for the vector | 
|---|
|  | 164 | /*! | 
|---|
|  | 165 | \param n : number of elements | 
|---|
|  | 166 | \param lcv : line or column vector ? | 
|---|
|  | 167 | \param force : if true re-allocation is forced, if not it occurs | 
|---|
|  | 168 | only if the required space is greater than the old one. | 
|---|
|  | 169 | \sa ReSize SelectVectorType | 
|---|
|  | 170 | */ | 
|---|
| [762] | 171 | template <class T> | 
|---|
| [1156] | 172 | void TVector<T>::Realloc(sa_size_t n, short lcv, bool force) | 
|---|
| [762] | 173 | { | 
|---|
| [804] | 174 | if( n == 0 ) | 
|---|
|  | 175 | throw(SzMismatchError("TVector::Realloc() n = 0 ")); | 
|---|
| [1156] | 176 | sa_size_t r,c; | 
|---|
| [813] | 177 | if (lcv == SameVectorType)  lcv = GetVectorType(); | 
|---|
|  | 178 | else if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType(); | 
|---|
| [804] | 179 | if (lcv == ColumnVector) { r = n;  c = 1; } | 
|---|
|  | 180 | else { c = n; r = 1; } | 
|---|
|  | 181 | TMatrix<T>::Realloc(r,c,SameMemoryMapping,force); | 
|---|
| [813] | 182 | veceli_ = (lcv ==  ColumnVector ) ?  marowi_ : macoli_; | 
|---|
| [762] | 183 | } | 
|---|
|  | 184 |  | 
|---|
| [804] | 185 | // $CHECK$ Reza 03/2000  Doit-on declarer cette methode const ? | 
|---|
| [894] | 186 | //! Return a subvector define by \b Range \b relt | 
|---|
| [762] | 187 | template <class T> | 
|---|
| [813] | 188 | TVector<T> TVector<T>::SubVector(Range relt) const | 
|---|
| [762] | 189 | { | 
|---|
| [2915] | 190 | Range rr=Range::first(); | 
|---|
|  | 191 | Range cr=Range::first(); | 
|---|
| [804] | 192 | if (GetVectorType() == ColumnVector )  rr = relt; | 
|---|
|  | 193 | else cr = relt; | 
|---|
|  | 194 | TMatrix<T> const & mtx = (*this); | 
|---|
| [2752] | 195 | TVector sv( mtx(rr, cr) , true, GetVectorType() ); | 
|---|
| [804] | 196 | return(sv); | 
|---|
| [762] | 197 | } | 
|---|
|  | 198 |  | 
|---|
| [894] | 199 | //! Return info on number of rows, column and type \b T | 
|---|
| [813] | 200 | template <class T> | 
|---|
|  | 201 | string TVector<T>::InfoString() const | 
|---|
|  | 202 | { | 
|---|
|  | 203 | string rs = "TVector<"; | 
|---|
|  | 204 | rs += typeid(T).name(); | 
|---|
|  | 205 | char buff[64]; | 
|---|
|  | 206 | sprintf(buff, ">(%ld) (nr=%ld, nc=%ld)", (long)NElts(), (long)NRows(), (long)NCols()); | 
|---|
|  | 207 | rs += buff; | 
|---|
|  | 208 | return(rs); | 
|---|
| [804] | 209 |  | 
|---|
| [813] | 210 | } | 
|---|
|  | 211 |  | 
|---|
| [2719] | 212 | //! Fill from a STL vector | 
|---|
|  | 213 | /*! | 
|---|
|  | 214 | \param v : STL vector to copy | 
|---|
|  | 215 | \param noresize : "true" means TVector keeps its size | 
|---|
|  | 216 | \warning Filling is always done starting at TVector(0) | 
|---|
|  | 217 | */ | 
|---|
|  | 218 | template <class T> | 
|---|
|  | 219 | sa_size_t TVector<T>::FillFr(const vector<T>& v,bool noresize) | 
|---|
|  | 220 | { | 
|---|
|  | 221 | sa_size_t n = v.size(); | 
|---|
|  | 222 | if(n==0) return 0; //STL vector de taille nulle! | 
|---|
|  | 223 | if(!noresize) ReSize(n,SameVectorType,false); | 
|---|
|  | 224 | if(Size()<n) n = Size(); | 
|---|
|  | 225 | if(n==0) return 0; //TVector de taille nulle sans resize! | 
|---|
|  | 226 | for(sa_size_t i=0;i<n;i++) (*this)(i) = v[i]; | 
|---|
|  | 227 | return n; | 
|---|
|  | 228 | } | 
|---|
|  | 229 |  | 
|---|
|  | 230 | //! Fill a STL vector | 
|---|
|  | 231 | /*! | 
|---|
|  | 232 | \param v : STL vector to fill | 
|---|
|  | 233 | \param addtoend : "true" means TVector will be added at the end of "v" | 
|---|
|  | 234 | */ | 
|---|
|  | 235 | template <class T> | 
|---|
|  | 236 | sa_size_t TVector<T>::FillTo(vector<T>& v,bool addtoend) | 
|---|
|  | 237 | { | 
|---|
|  | 238 | sa_size_t n = Size(); | 
|---|
|  | 239 | if(n==0) return 0; //TVector de taille nulle! | 
|---|
|  | 240 | if(!addtoend) v.resize(0); | 
|---|
|  | 241 | for(sa_size_t i=0;i<n;i++) v.push_back((*this)(i)); | 
|---|
|  | 242 | return n; | 
|---|
|  | 243 | } | 
|---|
|  | 244 |  | 
|---|
| [762] | 245 | /////////////////////////////////////////////////////////////// | 
|---|
|  | 246 | #ifdef __CXX_PRAGMA_TEMPLATES__ | 
|---|
| [3661] | 247 | #pragma define_template TVector<uint_1> | 
|---|
| [762] | 248 | #pragma define_template TVector<uint_2> | 
|---|
| [2927] | 249 | #pragma define_template TVector<uint_4> | 
|---|
| [1543] | 250 | #pragma define_template TVector<uint_8> | 
|---|
| [3661] | 251 | #pragma define_template TVector<int_1> | 
|---|
| [2927] | 252 | #pragma define_template TVector<int_2> | 
|---|
| [762] | 253 | #pragma define_template TVector<int_4> | 
|---|
|  | 254 | #pragma define_template TVector<int_8> | 
|---|
|  | 255 | #pragma define_template TVector<r_4> | 
|---|
|  | 256 | #pragma define_template TVector<r_8> | 
|---|
|  | 257 | #pragma define_template TVector< complex<r_4> > | 
|---|
|  | 258 | #pragma define_template TVector< complex<r_8> > | 
|---|
|  | 259 | #endif | 
|---|
|  | 260 |  | 
|---|
|  | 261 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES) | 
|---|
| [2868] | 262 | namespace SOPHYA { | 
|---|
| [3661] | 263 | template class TVector<uint_1>; | 
|---|
| [762] | 264 | template class TVector<uint_2>; | 
|---|
| [2927] | 265 | template class TVector<uint_4>; | 
|---|
| [1543] | 266 | template class TVector<uint_8>; | 
|---|
| [3661] | 267 | template class TVector<int_1>; | 
|---|
| [2927] | 268 | template class TVector<int_2>; | 
|---|
| [762] | 269 | template class TVector<int_4>; | 
|---|
|  | 270 | template class TVector<int_8>; | 
|---|
|  | 271 | template class TVector<r_4>; | 
|---|
|  | 272 | template class TVector<r_8>; | 
|---|
|  | 273 | template class TVector< complex<r_4> >; | 
|---|
|  | 274 | template class TVector< complex<r_8> >; | 
|---|
| [2868] | 275 | } | 
|---|
| [762] | 276 | #endif | 
|---|
| [804] | 277 |  | 
|---|