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