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