// $Id: tvector.cc,v 1.27 2010-08-12 10:22:17 ansari Exp $ // C.Magneville 04/99 #include "sopnamsp.h" #include "machdefs.h" #include #include "pexceptions.h" #define TVECTOR_CC_BFILE // avoid extern template declarations #include "tvector.h" namespace SOPHYA { /*! \class TVector \ingroup TArray The TVector class specializes the TMatrix class for representing row or column vectors. TVector objects can be initialised or converted to std::vector. \b Vector is a typedef for double precision floating point vectors ( TVector ). \sa SOPHYA::TArray SOPHYA::TMatrix \sa SOPHYA::Range SOPHYA::Sequence \sa SOPHYA::MathArray The following sample code illustrates sub-vector manipulation: \code #include "array.h" // ... // Input Vector containing a noisy periodic signal Vector in(1024), out(1024); in = RandomSequence(RandomSequence::Gaussian, 0., 1.); for(int kk=0; kk TVector::TVector() : TMatrix() { arrtype_ = 2; // Type = Vector } //! construct a vector /*! \param n : number of elements \param lcv : line or column vector ? \param mm : memory mapping type \param fzero : if \b true , set vector elements to zero \sa SelectVectorType */ template TVector::TVector(sa_size_t n, short lcv, short mm, bool fzero) // Constructeur : TMatrix(1,1,mm,false) { arrtype_ = 2; // Type = Vector lcv = SelectVectorType(lcv); ReSize(n,lcv,fzero); } //! Constructor by copy /*! \warning datas are \b SHARED with \b a. \sa NDataBlock::NDataBlock(const NDataBlock&) */ template TVector::TVector(const TVector& a) // Constructeur par copie : TMatrix(a) { arrtype_ = 2; // Type = Vector } //! Constructor by copy /*! \param share : if true, share data. If false copy data */ template TVector::TVector(const TVector& a, bool share) // Constructeur par copie avec possibilite de forcer le partage ou non. : TMatrix(a, share) { arrtype_ = 2; // Type = Vector } //! Constructor of a vector from a TArray \b a /*! \param a : TArray to be copied or shared \param share : if true, share data. If false copy data \param lcv : line or column vector ? \sa SelectVectorType */ template TVector::TVector(const TArray& a, bool share, short lcv) : TMatrix(a, share) { if ( (size_[0] != 1) && (size_[1] != 1) ) throw SzMismatchError("TVector::TVector(const TArray& a) NRows()!=1 && NCols()!=1 "); arrtype_ = 2; // Type = Vector if ( (size_[0] == 1) && (size_[1] == 1) ) { if (lcv == SameVectorType) lcv = a.GetVectorType(); if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType(); veceli_ = (lcv == ColumnVector ) ? marowi_ : macoli_; } } //! Constructor of a vector from a TArray \b a , with a different data type template TVector::TVector(const BaseArray& a) : TMatrix(a) { if ( (size_[0] != 1) && (size_[1] != 1) ) throw SzMismatchError("TVector::TVector(const BaseArray& a) NRows()!=1 && NCols()!=1 "); arrtype_ = 2; // Type = Vector } //! Constructor from an STL vector template TVector::TVector(const vector& v, short lcv) { sa_size_t n = v.size(); if(n==0) throw SzMismatchError("TVector::TVector(const vector& v) size()==0 "); arrtype_ = 2; // Type = Vector ReSize(n,lcv,false); for(sa_size_t i=0;i TVector::~TVector() { } //! Resize the vector /*! \param n : number of elements \param lcv : line or column vector ? \sa SelectVectorType */ template void TVector::ReSize(sa_size_t n, short lcv, bool fzero) { if( n == 0 ) throw(SzMismatchError("TVector::ReSize() n = 0 ")); sa_size_t r,c; if (lcv == SameVectorType) lcv = GetVectorType(); else if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType(); if (lcv == ColumnVector) { r = n; c = 1; } else { c = n; r = 1; } TMatrix::ReSize(r,c,BaseArray::SameMemoryMapping,fzero); veceli_ = (lcv == ColumnVector ) ? marowi_ : macoli_; } //! Re-allocate space for the vector /*! \param n : number of elements \param lcv : line or column vector ? \param force : if true re-allocation is forced, if not it occurs only if the required space is greater than the old one. \sa ReSize SelectVectorType */ template void TVector::Realloc(sa_size_t n, short lcv, bool force) { if( n == 0 ) throw(SzMismatchError("TVector::Realloc() n = 0 ")); sa_size_t r,c; if (lcv == SameVectorType) lcv = GetVectorType(); else if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType(); if (lcv == ColumnVector) { r = n; c = 1; } else { c = n; r = 1; } TMatrix::Realloc(r,c,SameMemoryMapping,force); veceli_ = (lcv == ColumnVector ) ? marowi_ : macoli_; } // $CHECK$ Reza 03/2000 Doit-on declarer cette methode const ? //! Return a subvector define by \b Range \b relt template TVector TVector::SubVector(Range relt) const { Range rr=Range::first(); Range cr=Range::first(); if (GetVectorType() == ColumnVector ) rr = relt; else cr = relt; TMatrix const & mtx = (*this); TVector sv( mtx(rr, cr) , true, GetVectorType() ); return(sv); } //! Return info on number of rows, column and type \b T template string TVector::InfoString() const { string rs = "TVector<"; rs += typeid(T).name(); char buff[64]; sprintf(buff, ">(%ld) (nr=%ld, nc=%ld)", (long)NElts(), (long)NRows(), (long)NCols()); rs += buff; return(rs); } //! Fill from an STL vector /*! \param v : STL vector to copy \param noresize : "true" means TVector keeps its size, if "false", ReSize(, ,false) is called \warning Filling is always done starting at TVector(0) */ template sa_size_t TVector::FillFr(const vector& v,bool noresize) { sa_size_t n = v.size(); if(n==0) return 0; //STL vector de taille nulle! if(!noresize) ReSize(n,SameVectorType,false); if(Size() sa_size_t TVector::FillTo(vector& v,bool addtoend) { sa_size_t n = Size(); if(n==0) return 0; //TVector de taille nulle! if(!addtoend) v.resize(0); for(sa_size_t i=0;i conversion function Returns an std::vector with the same size as the original vector and with elements std::vec[i]=(*this)(i) */ template vector TVector::ConvertTostdvec() { sa_size_t n = Size(); std::vector rv(n); for(sa_size_t i=0;i #pragma define_template TVector #pragma define_template TVector #pragma define_template TVector #pragma define_template TVector #pragma define_template TVector #pragma define_template TVector #pragma define_template TVector #pragma define_template TVector #pragma define_template TVector #pragma define_template TVector< complex > #pragma define_template TVector< complex > #ifdef SO_LDBLE128 #pragma define_template TVector #pragma define_template TVector< complex > #endif #endif #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES) template class TVector; template class TVector; template class TVector; template class TVector; template class TVector; template class TVector; template class TVector; template class TVector; template class TVector; template class TVector; template class TVector< complex >; template class TVector< complex >; #ifdef SO_LDBLE128 template class TVector; template class TVector< complex >; #endif #endif } // FIN namespace SOPHYA