[2575] | 1 | // $Id: tvector.cc,v 1.16 2004-07-29 12:31:16 ansari Exp $
|
---|
[762] | 2 | // C.Magneville 04/99
|
---|
| 3 | #include "machdefs.h"
|
---|
| 4 | #include <stdlib.h>
|
---|
| 5 | #include "pexceptions.h"
|
---|
| 6 | #include "tvector.h"
|
---|
| 7 |
|
---|
[926] | 8 | /*!
|
---|
| 9 | \class SOPHYA::TVector
|
---|
| 10 | \ingroup TArray
|
---|
| 11 |
|
---|
[2267] | 12 | The TVector class specializes the TMatrix class for representing
|
---|
| 13 | row or column vectors.
|
---|
| 14 |
|
---|
| 15 | \b Vector is a typedef for double precision floating point vectors ( TVector<r_8> ).
|
---|
| 16 |
|
---|
| 17 | \sa SOPHYA::TArray SOPHYA::TMatrix
|
---|
| 18 | \sa SOPHYA::Range SOPHYA::Sequence
|
---|
| 19 | \sa SOPHYA::MathArray
|
---|
| 20 |
|
---|
| 21 | The following sample code illustrates sub-vector manipulation:
|
---|
| 22 | \code
|
---|
| 23 | #include "array.h"
|
---|
| 24 | // ...
|
---|
| 25 | // Input Vector containing a noisy periodic signal
|
---|
| 26 | Vector in(1024), out(1024);
|
---|
| 27 | in = RandomSequence(RandomSequence::Gaussian, 0., 1.);
|
---|
| 28 | for(int kk=0; kk<in.Size(); kk++)
|
---|
| 29 | in(kk) += 2*sin(kk*0.05);
|
---|
| 30 | // Compute the output vector by a simple low pass filter
|
---|
| 31 | Vector out(1024);
|
---|
| 32 | int w = 2;
|
---|
| 33 | for(int k=w; k<in.Size()-w; k++)
|
---|
| 34 | out(k) = in(Range(k-w, k+w).Sum()/(2.*w+1.);
|
---|
| 35 | \endcode
|
---|
| 36 | */
|
---|
| 37 |
|
---|
[804] | 38 | ////////////////////////////////////////////////////////////////
|
---|
| 39 | //**** Createur, Destructeur
|
---|
| 40 |
|
---|
[894] | 41 | //! Default constructor
|
---|
[762] | 42 | template <class T>
|
---|
[804] | 43 | TVector<T>::TVector()
|
---|
| 44 | : TMatrix<T>()
|
---|
[762] | 45 | {
|
---|
[1560] | 46 | arrtype_ = 2; // Type = Vector
|
---|
[762] | 47 | }
|
---|
| 48 |
|
---|
[894] | 49 | //! construct a vector
|
---|
| 50 | /*!
|
---|
| 51 | \param n : number of elements
|
---|
| 52 | \param lcv : line or column vector ?
|
---|
| 53 | \param mm : memory mapping type
|
---|
[2575] | 54 | \param fzero : if \b true , set vector elements to zero
|
---|
[894] | 55 | \sa SelectVectorType
|
---|
| 56 | */
|
---|
[762] | 57 | template <class T>
|
---|
[2575] | 58 | TVector<T>::TVector(sa_size_t n, short lcv, short mm, bool fzero)
|
---|
[804] | 59 | // Constructeur
|
---|
[2575] | 60 | : TMatrix<T>(1,1,mm,false)
|
---|
[762] | 61 | {
|
---|
[1099] | 62 | arrtype_ = 2; // Type = Vector
|
---|
[813] | 63 | lcv = SelectVectorType(lcv);
|
---|
[2575] | 64 | ReSize(n,lcv,fzero);
|
---|
[762] | 65 | }
|
---|
| 66 |
|
---|
[967] | 67 | //! Constructor by copy
|
---|
[976] | 68 | /*!
|
---|
| 69 | \warning datas are \b SHARED with \b a.
|
---|
| 70 | \sa NDataBlock::NDataBlock(const NDataBlock<T>&)
|
---|
| 71 | */
|
---|
[762] | 72 | template <class T>
|
---|
[804] | 73 | TVector<T>::TVector(const TVector<T>& a)
|
---|
[967] | 74 | // Constructeur par copie
|
---|
[804] | 75 | : TMatrix<T>(a)
|
---|
[762] | 76 | {
|
---|
[1099] | 77 | arrtype_ = 2; // Type = Vector
|
---|
[762] | 78 | }
|
---|
| 79 |
|
---|
[894] | 80 | //! Constructor by copy
|
---|
| 81 | /*!
|
---|
| 82 | \param share : if true, share data. If false copy data
|
---|
| 83 | */
|
---|
[762] | 84 | template <class T>
|
---|
[804] | 85 | TVector<T>::TVector(const TVector<T>& a, bool share)
|
---|
[762] | 86 | // Constructeur par copie avec possibilite de forcer le partage ou non.
|
---|
[804] | 87 | : TMatrix<T>(a, share)
|
---|
[762] | 88 | {
|
---|
[1099] | 89 | arrtype_ = 2; // Type = Vector
|
---|
[762] | 90 | }
|
---|
| 91 |
|
---|
[894] | 92 | //! Constructor from a TArray
|
---|
[762] | 93 | template <class T>
|
---|
[804] | 94 | TVector<T>::TVector(const TArray<T>& a)
|
---|
[762] | 95 | : TMatrix<T>(a)
|
---|
| 96 | {
|
---|
[813] | 97 | if ( (size_[0] != 1) && (size_[1] != 1) )
|
---|
| 98 | throw SzMismatchError("TVector<T>::TVector(const TArray<T>& a) NRows()!=1 && NCols()!=1 ");
|
---|
[1099] | 99 | arrtype_ = 2; // Type = Vector
|
---|
[762] | 100 | }
|
---|
| 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 |
|
---|
[894] | 135 | //! Destructor
|
---|
[762] | 136 | template <class T>
|
---|
[804] | 137 | TVector<T>::~TVector()
|
---|
[762] | 138 | {
|
---|
| 139 | }
|
---|
| 140 |
|
---|
[894] | 141 | //! Resize the vector
|
---|
| 142 | /*!
|
---|
| 143 | \param n : number of elements
|
---|
| 144 | \param lcv : line or column vector ?
|
---|
| 145 | \sa SelectVectorType
|
---|
| 146 | */
|
---|
[762] | 147 | template <class T>
|
---|
[2575] | 148 | void TVector<T>::ReSize(sa_size_t n, short lcv, bool fzero)
|
---|
[762] | 149 | {
|
---|
[804] | 150 | if( n == 0 )
|
---|
| 151 | throw(SzMismatchError("TVector::ReSize() n = 0 "));
|
---|
[1156] | 152 | sa_size_t r,c;
|
---|
[813] | 153 | if (lcv == SameVectorType) lcv = GetVectorType();
|
---|
| 154 | else if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType();
|
---|
[804] | 155 | if (lcv == ColumnVector) { r = n; c = 1; }
|
---|
| 156 | else { c = n; r = 1; }
|
---|
[2575] | 157 | TMatrix<T>::ReSize(r,c,BaseArray::SameMemoryMapping,fzero);
|
---|
[813] | 158 | veceli_ = (lcv == ColumnVector ) ? marowi_ : macoli_;
|
---|
[762] | 159 | }
|
---|
| 160 |
|
---|
[894] | 161 | //! Re-allocate space for the vector
|
---|
| 162 | /*!
|
---|
| 163 | \param n : number of elements
|
---|
| 164 | \param lcv : line or column vector ?
|
---|
| 165 | \param force : if true re-allocation is forced, if not it occurs
|
---|
| 166 | only if the required space is greater than the old one.
|
---|
| 167 | \sa ReSize SelectVectorType
|
---|
| 168 | */
|
---|
[762] | 169 | template <class T>
|
---|
[1156] | 170 | void TVector<T>::Realloc(sa_size_t n, short lcv, bool force)
|
---|
[762] | 171 | {
|
---|
[804] | 172 | if( n == 0 )
|
---|
| 173 | throw(SzMismatchError("TVector::Realloc() n = 0 "));
|
---|
[1156] | 174 | sa_size_t r,c;
|
---|
[813] | 175 | if (lcv == SameVectorType) lcv = GetVectorType();
|
---|
| 176 | else if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType();
|
---|
[804] | 177 | if (lcv == ColumnVector) { r = n; c = 1; }
|
---|
| 178 | else { c = n; r = 1; }
|
---|
| 179 | TMatrix<T>::Realloc(r,c,SameMemoryMapping,force);
|
---|
[813] | 180 | veceli_ = (lcv == ColumnVector ) ? marowi_ : macoli_;
|
---|
[762] | 181 | }
|
---|
| 182 |
|
---|
[804] | 183 | // $CHECK$ Reza 03/2000 Doit-on declarer cette methode const ?
|
---|
[894] | 184 | //! Return a subvector define by \b Range \b relt
|
---|
[762] | 185 | template <class T>
|
---|
[813] | 186 | TVector<T> TVector<T>::SubVector(Range relt) const
|
---|
[762] | 187 | {
|
---|
[804] | 188 | Range rr, cr;
|
---|
| 189 | if (GetVectorType() == ColumnVector ) rr = relt;
|
---|
| 190 | else cr = relt;
|
---|
| 191 | TMatrix<T> const & mtx = (*this);
|
---|
| 192 | TVector sv( mtx(rr, cr) , true, GetVectorType(), GetMemoryMapping() );
|
---|
| 193 | return(sv);
|
---|
[762] | 194 | }
|
---|
| 195 |
|
---|
[894] | 196 | //! Return info on number of rows, column and type \b T
|
---|
[813] | 197 | template <class T>
|
---|
| 198 | string TVector<T>::InfoString() const
|
---|
| 199 | {
|
---|
| 200 | string rs = "TVector<";
|
---|
| 201 | rs += typeid(T).name();
|
---|
| 202 | char buff[64];
|
---|
| 203 | sprintf(buff, ">(%ld) (nr=%ld, nc=%ld)", (long)NElts(), (long)NRows(), (long)NCols());
|
---|
| 204 | rs += buff;
|
---|
| 205 | return(rs);
|
---|
[804] | 206 |
|
---|
[813] | 207 | }
|
---|
| 208 |
|
---|
[762] | 209 | ///////////////////////////////////////////////////////////////
|
---|
| 210 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
| 211 | #pragma define_template TVector<uint_2>
|
---|
[1543] | 212 | #pragma define_template TVector<uint_8>
|
---|
[762] | 213 | #pragma define_template TVector<int_4>
|
---|
| 214 | #pragma define_template TVector<int_8>
|
---|
| 215 | #pragma define_template TVector<r_4>
|
---|
| 216 | #pragma define_template TVector<r_8>
|
---|
| 217 | #pragma define_template TVector< complex<r_4> >
|
---|
| 218 | #pragma define_template TVector< complex<r_8> >
|
---|
| 219 | #endif
|
---|
| 220 |
|
---|
| 221 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
| 222 | template class TVector<uint_2>;
|
---|
[1543] | 223 | template class TVector<uint_8>;
|
---|
[762] | 224 | template class TVector<int_4>;
|
---|
| 225 | template class TVector<int_8>;
|
---|
| 226 | template class TVector<r_4>;
|
---|
| 227 | template class TVector<r_8>;
|
---|
| 228 | template class TVector< complex<r_4> >;
|
---|
| 229 | template class TVector< complex<r_8> >;
|
---|
| 230 | #endif
|
---|
[804] | 231 |
|
---|