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