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