[2267] | 1 | // $Id: tvector.cc,v 1.15 2002-11-15 09:35:44 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
|
---|
| 54 | \sa SelectVectorType
|
---|
| 55 | */
|
---|
[762] | 56 | template <class T>
|
---|
[1156] | 57 | TVector<T>::TVector(sa_size_t n, short lcv, short mm)
|
---|
[804] | 58 | // Constructeur
|
---|
[813] | 59 | : TMatrix<T>(1,1,mm)
|
---|
[762] | 60 | {
|
---|
[1099] | 61 | arrtype_ = 2; // Type = Vector
|
---|
[813] | 62 | lcv = SelectVectorType(lcv);
|
---|
| 63 | ReSize(n,lcv);
|
---|
[762] | 64 | }
|
---|
| 65 |
|
---|
[967] | 66 | //! Constructor by copy
|
---|
[976] | 67 | /*!
|
---|
| 68 | \warning datas are \b SHARED with \b a.
|
---|
| 69 | \sa NDataBlock::NDataBlock(const NDataBlock<T>&)
|
---|
| 70 | */
|
---|
[762] | 71 | template <class T>
|
---|
[804] | 72 | TVector<T>::TVector(const TVector<T>& a)
|
---|
[967] | 73 | // Constructeur par copie
|
---|
[804] | 74 | : TMatrix<T>(a)
|
---|
[762] | 75 | {
|
---|
[1099] | 76 | arrtype_ = 2; // Type = Vector
|
---|
[762] | 77 | }
|
---|
| 78 |
|
---|
[894] | 79 | //! Constructor by copy
|
---|
| 80 | /*!
|
---|
| 81 | \param share : if true, share data. If false copy data
|
---|
| 82 | */
|
---|
[762] | 83 | template <class T>
|
---|
[804] | 84 | TVector<T>::TVector(const TVector<T>& a, bool share)
|
---|
[762] | 85 | // Constructeur par copie avec possibilite de forcer le partage ou non.
|
---|
[804] | 86 | : TMatrix<T>(a, share)
|
---|
[762] | 87 | {
|
---|
[1099] | 88 | arrtype_ = 2; // Type = Vector
|
---|
[762] | 89 | }
|
---|
| 90 |
|
---|
[894] | 91 | //! Constructor from a TArray
|
---|
[762] | 92 | template <class T>
|
---|
[804] | 93 | TVector<T>::TVector(const TArray<T>& a)
|
---|
[762] | 94 | : TMatrix<T>(a)
|
---|
| 95 | {
|
---|
[813] | 96 | if ( (size_[0] != 1) && (size_[1] != 1) )
|
---|
| 97 | throw SzMismatchError("TVector<T>::TVector(const TArray<T>& a) NRows()!=1 && NCols()!=1 ");
|
---|
[1099] | 98 | arrtype_ = 2; // Type = Vector
|
---|
[762] | 99 | }
|
---|
| 100 |
|
---|
| 101 |
|
---|
[894] | 102 | //! Constructor of a vector from a TArray \b a
|
---|
| 103 | /*!
|
---|
| 104 | \param a : TArray to be copied or shared
|
---|
| 105 | \param share : if true, share data. If false copy data
|
---|
| 106 | \param mm : define the memory mapping type
|
---|
| 107 | \param lcv : line or column vector ?
|
---|
| 108 | \sa SelectVectorType
|
---|
| 109 | */
|
---|
[762] | 110 | template <class T>
|
---|
[914] | 111 | TVector<T>::TVector(const TArray<T>& a, bool share, short lcv, short mm)
|
---|
[804] | 112 | : TMatrix<T>(a, share, mm)
|
---|
[762] | 113 | {
|
---|
[813] | 114 | if ( (size_[0] != 1) && (size_[1] != 1) )
|
---|
| 115 | throw SzMismatchError("TVector<T>::TVector(const TArray<T>& a) NRows()!=1 && NCols()!=1 ");
|
---|
[1099] | 116 | arrtype_ = 2; // Type = Vector
|
---|
[813] | 117 | if ( (size_[0] == 1) && (size_[1] == 1) ) {
|
---|
| 118 | if (lcv == SameVectorType) lcv = a.GetVectorType();
|
---|
| 119 | if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType();
|
---|
| 120 | veceli_ = (lcv == ColumnVector ) ? marowi_ : macoli_;
|
---|
[804] | 121 | }
|
---|
[762] | 122 | }
|
---|
| 123 |
|
---|
[1099] | 124 | //! Constructor of a vector from a TArray \b a , with a different data type
|
---|
| 125 | template <class T>
|
---|
| 126 | TVector<T>::TVector(const BaseArray& a)
|
---|
[1103] | 127 | : TMatrix<T>(a)
|
---|
[1099] | 128 | {
|
---|
[1103] | 129 | if ( (size_[0] != 1) && (size_[1] != 1) )
|
---|
| 130 | throw SzMismatchError("TVector<T>::TVector(const BaseArray& a) NRows()!=1 && NCols()!=1 ");
|
---|
[1099] | 131 | arrtype_ = 2; // Type = Vector
|
---|
| 132 | }
|
---|
| 133 |
|
---|
[894] | 134 | //! Destructor
|
---|
[762] | 135 | template <class T>
|
---|
[804] | 136 | TVector<T>::~TVector()
|
---|
[762] | 137 | {
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[894] | 140 | //! Resize the vector
|
---|
| 141 | /*!
|
---|
| 142 | \param n : number of elements
|
---|
| 143 | \param lcv : line or column vector ?
|
---|
| 144 | \sa SelectVectorType
|
---|
| 145 | */
|
---|
[762] | 146 | template <class T>
|
---|
[1156] | 147 | void TVector<T>::ReSize(sa_size_t n, short lcv)
|
---|
[762] | 148 | {
|
---|
[804] | 149 | if( n == 0 )
|
---|
| 150 | throw(SzMismatchError("TVector::ReSize() n = 0 "));
|
---|
[1156] | 151 | sa_size_t r,c;
|
---|
[813] | 152 | if (lcv == SameVectorType) lcv = GetVectorType();
|
---|
| 153 | else if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType();
|
---|
[804] | 154 | if (lcv == ColumnVector) { r = n; c = 1; }
|
---|
| 155 | else { c = n; r = 1; }
|
---|
| 156 | TMatrix<T>::ReSize(r,c);
|
---|
[813] | 157 | veceli_ = (lcv == ColumnVector ) ? marowi_ : macoli_;
|
---|
[762] | 158 | }
|
---|
| 159 |
|
---|
[894] | 160 | //! Re-allocate space for the vector
|
---|
| 161 | /*!
|
---|
| 162 | \param n : number of elements
|
---|
| 163 | \param lcv : line or column vector ?
|
---|
| 164 | \param force : if true re-allocation is forced, if not it occurs
|
---|
| 165 | only if the required space is greater than the old one.
|
---|
| 166 | \sa ReSize SelectVectorType
|
---|
| 167 | */
|
---|
[762] | 168 | template <class T>
|
---|
[1156] | 169 | void TVector<T>::Realloc(sa_size_t n, short lcv, bool force)
|
---|
[762] | 170 | {
|
---|
[804] | 171 | if( n == 0 )
|
---|
| 172 | throw(SzMismatchError("TVector::Realloc() n = 0 "));
|
---|
[1156] | 173 | sa_size_t r,c;
|
---|
[813] | 174 | if (lcv == SameVectorType) lcv = GetVectorType();
|
---|
| 175 | else if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType();
|
---|
[804] | 176 | if (lcv == ColumnVector) { r = n; c = 1; }
|
---|
| 177 | else { c = n; r = 1; }
|
---|
| 178 | TMatrix<T>::Realloc(r,c,SameMemoryMapping,force);
|
---|
[813] | 179 | veceli_ = (lcv == ColumnVector ) ? marowi_ : macoli_;
|
---|
[762] | 180 | }
|
---|
| 181 |
|
---|
[804] | 182 | // $CHECK$ Reza 03/2000 Doit-on declarer cette methode const ?
|
---|
[894] | 183 | //! Return a subvector define by \b Range \b relt
|
---|
[762] | 184 | template <class T>
|
---|
[813] | 185 | TVector<T> TVector<T>::SubVector(Range relt) const
|
---|
[762] | 186 | {
|
---|
[804] | 187 | Range rr, cr;
|
---|
| 188 | if (GetVectorType() == ColumnVector ) rr = relt;
|
---|
| 189 | else cr = relt;
|
---|
| 190 | TMatrix<T> const & mtx = (*this);
|
---|
| 191 | TVector sv( mtx(rr, cr) , true, GetVectorType(), GetMemoryMapping() );
|
---|
| 192 | return(sv);
|
---|
[762] | 193 | }
|
---|
| 194 |
|
---|
[894] | 195 | //! Return the norm \f$ \sum{V(i)^2} \f$
|
---|
[762] | 196 | template <class T>
|
---|
[804] | 197 | T TVector<T>::Norm2() const
|
---|
[762] | 198 | {
|
---|
[804] | 199 | T ret = 0;
|
---|
[1156] | 200 | for(sa_size_t k=0; k<Size(); k++) ret += (*this)(k)*(*this)(k);
|
---|
[804] | 201 | return ret;
|
---|
[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 |
|
---|
[762] | 217 | ///////////////////////////////////////////////////////////////
|
---|
| 218 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
| 219 | #pragma define_template TVector<uint_2>
|
---|
[1543] | 220 | #pragma define_template TVector<uint_8>
|
---|
[762] | 221 | #pragma define_template TVector<int_4>
|
---|
| 222 | #pragma define_template TVector<int_8>
|
---|
| 223 | #pragma define_template TVector<r_4>
|
---|
| 224 | #pragma define_template TVector<r_8>
|
---|
| 225 | #pragma define_template TVector< complex<r_4> >
|
---|
| 226 | #pragma define_template TVector< complex<r_8> >
|
---|
| 227 | #endif
|
---|
| 228 |
|
---|
| 229 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
| 230 | template class TVector<uint_2>;
|
---|
[1543] | 231 | template class TVector<uint_8>;
|
---|
[762] | 232 | template class TVector<int_4>;
|
---|
| 233 | template class TVector<int_8>;
|
---|
| 234 | template class TVector<r_4>;
|
---|
| 235 | template class TVector<r_8>;
|
---|
| 236 | template class TVector< complex<r_4> >;
|
---|
| 237 | template class TVector< complex<r_8> >;
|
---|
| 238 | #endif
|
---|
[804] | 239 |
|
---|