[2719] | 1 | // $Id: tvector.cc,v 1.18 2005-05-13 16:44:30 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 |
|
---|
[894] | 93 | //! Constructor from a TArray
|
---|
[762] | 94 | template <class T>
|
---|
[804] | 95 | TVector<T>::TVector(const TArray<T>& a)
|
---|
[762] | 96 | : TMatrix<T>(a)
|
---|
| 97 | {
|
---|
[813] | 98 | if ( (size_[0] != 1) && (size_[1] != 1) )
|
---|
| 99 | throw SzMismatchError("TVector<T>::TVector(const TArray<T>& a) NRows()!=1 && NCols()!=1 ");
|
---|
[1099] | 100 | arrtype_ = 2; // Type = Vector
|
---|
[762] | 101 | }
|
---|
| 102 |
|
---|
[894] | 103 | //! Constructor of a vector from a TArray \b a
|
---|
| 104 | /*!
|
---|
| 105 | \param a : TArray to be copied or shared
|
---|
| 106 | \param share : if true, share data. If false copy data
|
---|
| 107 | \param mm : define the memory mapping type
|
---|
| 108 | \param lcv : line or column vector ?
|
---|
| 109 | \sa SelectVectorType
|
---|
| 110 | */
|
---|
[762] | 111 | template <class T>
|
---|
[914] | 112 | TVector<T>::TVector(const TArray<T>& a, bool share, short lcv, short mm)
|
---|
[804] | 113 | : TMatrix<T>(a, share, mm)
|
---|
[762] | 114 | {
|
---|
[813] | 115 | if ( (size_[0] != 1) && (size_[1] != 1) )
|
---|
| 116 | throw SzMismatchError("TVector<T>::TVector(const TArray<T>& a) NRows()!=1 && NCols()!=1 ");
|
---|
[1099] | 117 | arrtype_ = 2; // Type = Vector
|
---|
[813] | 118 | if ( (size_[0] == 1) && (size_[1] == 1) ) {
|
---|
| 119 | if (lcv == SameVectorType) lcv = a.GetVectorType();
|
---|
| 120 | if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType();
|
---|
| 121 | veceli_ = (lcv == ColumnVector ) ? marowi_ : macoli_;
|
---|
[804] | 122 | }
|
---|
[762] | 123 | }
|
---|
| 124 |
|
---|
[1099] | 125 | //! Constructor of a vector from a TArray \b a , with a different data type
|
---|
| 126 | template <class T>
|
---|
| 127 | TVector<T>::TVector(const BaseArray& a)
|
---|
[1103] | 128 | : TMatrix<T>(a)
|
---|
[1099] | 129 | {
|
---|
[1103] | 130 | if ( (size_[0] != 1) && (size_[1] != 1) )
|
---|
| 131 | throw SzMismatchError("TVector<T>::TVector(const BaseArray& a) NRows()!=1 && NCols()!=1 ");
|
---|
[1099] | 132 | arrtype_ = 2; // Type = Vector
|
---|
| 133 | }
|
---|
| 134 |
|
---|
[2719] | 135 | //! Constructor from a STL vector
|
---|
| 136 | template <class T>
|
---|
| 137 | TVector<T>::TVector(const vector<T>& v, short lcv)
|
---|
| 138 | {
|
---|
| 139 | sa_size_t n = v.size();
|
---|
| 140 | if(n==0)
|
---|
| 141 | throw SzMismatchError("TVector<T>::TVector(const vector<T>& v) size()==0 ");
|
---|
| 142 | arrtype_ = 2; // Type = Vector
|
---|
| 143 | ReSize(n,lcv,false);
|
---|
| 144 | for(sa_size_t i=0;i<n;i++) (*this)(i) = v[i];
|
---|
| 145 | }
|
---|
| 146 |
|
---|
[894] | 147 | //! Destructor
|
---|
[762] | 148 | template <class T>
|
---|
[804] | 149 | TVector<T>::~TVector()
|
---|
[762] | 150 | {
|
---|
| 151 | }
|
---|
| 152 |
|
---|
[894] | 153 | //! Resize the vector
|
---|
| 154 | /*!
|
---|
| 155 | \param n : number of elements
|
---|
| 156 | \param lcv : line or column vector ?
|
---|
| 157 | \sa SelectVectorType
|
---|
| 158 | */
|
---|
[762] | 159 | template <class T>
|
---|
[2575] | 160 | void TVector<T>::ReSize(sa_size_t n, short lcv, bool fzero)
|
---|
[762] | 161 | {
|
---|
[804] | 162 | if( n == 0 )
|
---|
| 163 | throw(SzMismatchError("TVector::ReSize() n = 0 "));
|
---|
[1156] | 164 | sa_size_t r,c;
|
---|
[813] | 165 | if (lcv == SameVectorType) lcv = GetVectorType();
|
---|
| 166 | else if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType();
|
---|
[804] | 167 | if (lcv == ColumnVector) { r = n; c = 1; }
|
---|
| 168 | else { c = n; r = 1; }
|
---|
[2575] | 169 | TMatrix<T>::ReSize(r,c,BaseArray::SameMemoryMapping,fzero);
|
---|
[813] | 170 | veceli_ = (lcv == ColumnVector ) ? marowi_ : macoli_;
|
---|
[762] | 171 | }
|
---|
| 172 |
|
---|
[894] | 173 | //! Re-allocate space for the vector
|
---|
| 174 | /*!
|
---|
| 175 | \param n : number of elements
|
---|
| 176 | \param lcv : line or column vector ?
|
---|
| 177 | \param force : if true re-allocation is forced, if not it occurs
|
---|
| 178 | only if the required space is greater than the old one.
|
---|
| 179 | \sa ReSize SelectVectorType
|
---|
| 180 | */
|
---|
[762] | 181 | template <class T>
|
---|
[1156] | 182 | void TVector<T>::Realloc(sa_size_t n, short lcv, bool force)
|
---|
[762] | 183 | {
|
---|
[804] | 184 | if( n == 0 )
|
---|
| 185 | throw(SzMismatchError("TVector::Realloc() n = 0 "));
|
---|
[1156] | 186 | sa_size_t r,c;
|
---|
[813] | 187 | if (lcv == SameVectorType) lcv = GetVectorType();
|
---|
| 188 | else if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType();
|
---|
[804] | 189 | if (lcv == ColumnVector) { r = n; c = 1; }
|
---|
| 190 | else { c = n; r = 1; }
|
---|
| 191 | TMatrix<T>::Realloc(r,c,SameMemoryMapping,force);
|
---|
[813] | 192 | veceli_ = (lcv == ColumnVector ) ? marowi_ : macoli_;
|
---|
[762] | 193 | }
|
---|
| 194 |
|
---|
[804] | 195 | // $CHECK$ Reza 03/2000 Doit-on declarer cette methode const ?
|
---|
[894] | 196 | //! Return a subvector define by \b Range \b relt
|
---|
[762] | 197 | template <class T>
|
---|
[813] | 198 | TVector<T> TVector<T>::SubVector(Range relt) const
|
---|
[762] | 199 | {
|
---|
[804] | 200 | Range rr, cr;
|
---|
| 201 | if (GetVectorType() == ColumnVector ) rr = relt;
|
---|
| 202 | else cr = relt;
|
---|
| 203 | TMatrix<T> const & mtx = (*this);
|
---|
| 204 | TVector sv( mtx(rr, cr) , true, GetVectorType(), GetMemoryMapping() );
|
---|
| 205 | return(sv);
|
---|
[762] | 206 | }
|
---|
| 207 |
|
---|
[894] | 208 | //! Return info on number of rows, column and type \b T
|
---|
[813] | 209 | template <class T>
|
---|
| 210 | string TVector<T>::InfoString() const
|
---|
| 211 | {
|
---|
| 212 | string rs = "TVector<";
|
---|
| 213 | rs += typeid(T).name();
|
---|
| 214 | char buff[64];
|
---|
| 215 | sprintf(buff, ">(%ld) (nr=%ld, nc=%ld)", (long)NElts(), (long)NRows(), (long)NCols());
|
---|
| 216 | rs += buff;
|
---|
| 217 | return(rs);
|
---|
[804] | 218 |
|
---|
[813] | 219 | }
|
---|
| 220 |
|
---|
[2719] | 221 | //! Fill from a STL vector
|
---|
| 222 | /*!
|
---|
| 223 | \param v : STL vector to copy
|
---|
| 224 | \param noresize : "true" means TVector keeps its size
|
---|
| 225 | \warning Filling is always done starting at TVector(0)
|
---|
| 226 | */
|
---|
| 227 | template <class T>
|
---|
| 228 | sa_size_t TVector<T>::FillFr(const vector<T>& v,bool noresize)
|
---|
| 229 | {
|
---|
| 230 | sa_size_t n = v.size();
|
---|
| 231 | if(n==0) return 0; //STL vector de taille nulle!
|
---|
| 232 | if(!noresize) ReSize(n,SameVectorType,false);
|
---|
| 233 | if(Size()<n) n = Size();
|
---|
| 234 | if(n==0) return 0; //TVector de taille nulle sans resize!
|
---|
| 235 | for(sa_size_t i=0;i<n;i++) (*this)(i) = v[i];
|
---|
| 236 | return n;
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | //! Fill a STL vector
|
---|
| 240 | /*!
|
---|
| 241 | \param v : STL vector to fill
|
---|
| 242 | \param addtoend : "true" means TVector will be added at the end of "v"
|
---|
| 243 | */
|
---|
| 244 | template <class T>
|
---|
| 245 | sa_size_t TVector<T>::FillTo(vector<T>& v,bool addtoend)
|
---|
| 246 | {
|
---|
| 247 | sa_size_t n = Size();
|
---|
| 248 | if(n==0) return 0; //TVector de taille nulle!
|
---|
| 249 | if(!addtoend) v.resize(0);
|
---|
| 250 | for(sa_size_t i=0;i<n;i++) v.push_back((*this)(i));
|
---|
| 251 | return n;
|
---|
| 252 | }
|
---|
| 253 |
|
---|
[762] | 254 | ///////////////////////////////////////////////////////////////
|
---|
| 255 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
| 256 | #pragma define_template TVector<uint_2>
|
---|
[1543] | 257 | #pragma define_template TVector<uint_8>
|
---|
[762] | 258 | #pragma define_template TVector<int_4>
|
---|
| 259 | #pragma define_template TVector<int_8>
|
---|
| 260 | #pragma define_template TVector<r_4>
|
---|
| 261 | #pragma define_template TVector<r_8>
|
---|
| 262 | #pragma define_template TVector< complex<r_4> >
|
---|
| 263 | #pragma define_template TVector< complex<r_8> >
|
---|
| 264 | #endif
|
---|
| 265 |
|
---|
| 266 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
| 267 | template class TVector<uint_2>;
|
---|
[1543] | 268 | template class TVector<uint_8>;
|
---|
[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> >;
|
---|
| 275 | #endif
|
---|
[804] | 276 |
|
---|