| 1 | // $Id: tvector.cc,v 1.7 2000-04-21 16:31:26 ansari Exp $ | 
|---|
| 2 | //                         C.Magneville          04/99 | 
|---|
| 3 | #include "machdefs.h" | 
|---|
| 4 | #include <stdlib.h> | 
|---|
| 5 | #include "pexceptions.h" | 
|---|
| 6 | #include "tvector.h" | 
|---|
| 7 |  | 
|---|
| 8 | /*! | 
|---|
| 9 | \class SOPHYA::TVector | 
|---|
| 10 | \ingroup TArray | 
|---|
| 11 | Class of vector (line or column) | 
|---|
| 12 | \sa TMatrix TArray | 
|---|
| 13 | */ | 
|---|
| 14 |  | 
|---|
| 15 | //////////////////////////////////////////////////////////////// | 
|---|
| 16 | //**** Createur, Destructeur | 
|---|
| 17 |  | 
|---|
| 18 | //! Default constructor | 
|---|
| 19 | template <class T> | 
|---|
| 20 | TVector<T>::TVector() | 
|---|
| 21 | : TMatrix<T>() | 
|---|
| 22 | { | 
|---|
| 23 | } | 
|---|
| 24 |  | 
|---|
| 25 | //! construct a vector | 
|---|
| 26 | /*! | 
|---|
| 27 | \param n : number of elements | 
|---|
| 28 | \param lcv : line or column vector ? | 
|---|
| 29 | \param mm : memory mapping type | 
|---|
| 30 | \sa SelectVectorType | 
|---|
| 31 | */ | 
|---|
| 32 | template <class T> | 
|---|
| 33 | TVector<T>::TVector(uint_4 n, short lcv, short mm) | 
|---|
| 34 | // Constructeur | 
|---|
| 35 | : TMatrix<T>(1,1,mm) | 
|---|
| 36 | { | 
|---|
| 37 | lcv = SelectVectorType(lcv); | 
|---|
| 38 | ReSize(n,lcv); | 
|---|
| 39 | } | 
|---|
| 40 |  | 
|---|
| 41 | //! Constructor by copy | 
|---|
| 42 | /*! \sa NDataBlock::NDataBlock(const NDataBlock<T>&) */ | 
|---|
| 43 | template <class T> | 
|---|
| 44 | TVector<T>::TVector(const TVector<T>& a) | 
|---|
| 45 | // Constructeur par copie | 
|---|
| 46 | : TMatrix<T>(a) | 
|---|
| 47 | { | 
|---|
| 48 | } | 
|---|
| 49 |  | 
|---|
| 50 | //! Constructor by copy | 
|---|
| 51 | /*! | 
|---|
| 52 | \param share : if true, share data. If false copy data | 
|---|
| 53 | */ | 
|---|
| 54 | template <class T> | 
|---|
| 55 | TVector<T>::TVector(const TVector<T>& a, bool share) | 
|---|
| 56 | // Constructeur par copie avec possibilite de forcer le partage ou non. | 
|---|
| 57 | : TMatrix<T>(a, share) | 
|---|
| 58 | { | 
|---|
| 59 | } | 
|---|
| 60 |  | 
|---|
| 61 | //! Constructor from a TArray | 
|---|
| 62 | template <class T> | 
|---|
| 63 | TVector<T>::TVector(const TArray<T>& a) | 
|---|
| 64 | : TMatrix<T>(a) | 
|---|
| 65 | { | 
|---|
| 66 | if ( (size_[0] != 1) && (size_[1] != 1) ) | 
|---|
| 67 | throw SzMismatchError("TVector<T>::TVector(const TArray<T>& a) NRows()!=1 && NCols()!=1 "); | 
|---|
| 68 | } | 
|---|
| 69 |  | 
|---|
| 70 |  | 
|---|
| 71 | //! Constructor of a vector from a TArray \b a | 
|---|
| 72 | /*! | 
|---|
| 73 | \param a : TArray to be copied or shared | 
|---|
| 74 | \param share : if true, share data. If false copy data | 
|---|
| 75 | \param mm : define the memory mapping type | 
|---|
| 76 | \param lcv : line or column vector ? | 
|---|
| 77 | \sa SelectVectorType | 
|---|
| 78 | */ | 
|---|
| 79 | template <class T> | 
|---|
| 80 | TVector<T>::TVector(const TArray<T>& a, bool share, short lcv, short mm) | 
|---|
| 81 | : TMatrix<T>(a, share, mm) | 
|---|
| 82 | { | 
|---|
| 83 | if ( (size_[0] != 1) && (size_[1] != 1) ) | 
|---|
| 84 | throw SzMismatchError("TVector<T>::TVector(const TArray<T>& a) NRows()!=1 && NCols()!=1 "); | 
|---|
| 85 | if ( (size_[0] == 1) && (size_[1] == 1) ) { | 
|---|
| 86 | if (lcv == SameVectorType) lcv = a.GetVectorType(); | 
|---|
| 87 | if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType(); | 
|---|
| 88 | veceli_ = (lcv ==  ColumnVector ) ?  marowi_ : macoli_; | 
|---|
| 89 | } | 
|---|
| 90 | } | 
|---|
| 91 |  | 
|---|
| 92 | //! Destructor | 
|---|
| 93 | template <class T> | 
|---|
| 94 | TVector<T>::~TVector() | 
|---|
| 95 | { | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | //! Resize the vector | 
|---|
| 99 | /*! | 
|---|
| 100 | \param n : number of elements | 
|---|
| 101 | \param lcv : line or column vector ? | 
|---|
| 102 | \sa SelectVectorType | 
|---|
| 103 | */ | 
|---|
| 104 | template <class T> | 
|---|
| 105 | void TVector<T>::ReSize(uint_4 n, short lcv) | 
|---|
| 106 | { | 
|---|
| 107 | if( n == 0 ) | 
|---|
| 108 | throw(SzMismatchError("TVector::ReSize() n = 0 ")); | 
|---|
| 109 | uint_4 r,c; | 
|---|
| 110 | if (lcv == SameVectorType)  lcv = GetVectorType(); | 
|---|
| 111 | else if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType(); | 
|---|
| 112 | if (lcv == ColumnVector) { r = n;  c = 1; } | 
|---|
| 113 | else { c = n; r = 1; } | 
|---|
| 114 | TMatrix<T>::ReSize(r,c); | 
|---|
| 115 | veceli_ = (lcv ==  ColumnVector ) ?  marowi_ : macoli_; | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 | //! Re-allocate space for the vector | 
|---|
| 119 | /*! | 
|---|
| 120 | \param n : number of elements | 
|---|
| 121 | \param lcv : line or column vector ? | 
|---|
| 122 | \param force : if true re-allocation is forced, if not it occurs | 
|---|
| 123 | only if the required space is greater than the old one. | 
|---|
| 124 | \sa ReSize SelectVectorType | 
|---|
| 125 | */ | 
|---|
| 126 | template <class T> | 
|---|
| 127 | void TVector<T>::Realloc(uint_4 n, short lcv, bool force) | 
|---|
| 128 | { | 
|---|
| 129 | if( n == 0 ) | 
|---|
| 130 | throw(SzMismatchError("TVector::Realloc() n = 0 ")); | 
|---|
| 131 | uint_4 r,c; | 
|---|
| 132 | if (lcv == SameVectorType)  lcv = GetVectorType(); | 
|---|
| 133 | else if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType(); | 
|---|
| 134 | if (lcv == ColumnVector) { r = n;  c = 1; } | 
|---|
| 135 | else { c = n; r = 1; } | 
|---|
| 136 | TMatrix<T>::Realloc(r,c,SameMemoryMapping,force); | 
|---|
| 137 | veceli_ = (lcv ==  ColumnVector ) ?  marowi_ : macoli_; | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | // $CHECK$ Reza 03/2000  Doit-on declarer cette methode const ? | 
|---|
| 141 | //! Return a subvector define by \b Range \b relt | 
|---|
| 142 | template <class T> | 
|---|
| 143 | TVector<T> TVector<T>::SubVector(Range relt) const | 
|---|
| 144 | { | 
|---|
| 145 | Range rr, cr; | 
|---|
| 146 | if (GetVectorType() == ColumnVector )  rr = relt; | 
|---|
| 147 | else cr = relt; | 
|---|
| 148 | TMatrix<T> const & mtx = (*this); | 
|---|
| 149 | TVector sv( mtx(rr, cr) , true, GetVectorType(), GetMemoryMapping() ); | 
|---|
| 150 | sv.SetTemp(true); | 
|---|
| 151 | return(sv); | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 | //! Return the norm \f$ \sum{V(i)^2} \f$ | 
|---|
| 155 | template <class T> | 
|---|
| 156 | T TVector<T>::Norm2() const | 
|---|
| 157 | { | 
|---|
| 158 | T ret = 0; | 
|---|
| 159 | for(uint_8 k=0; k<Size(); k++)   ret += (*this)(k)*(*this)(k); | 
|---|
| 160 | return ret; | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | //! Return info on number of rows, column and type \b T | 
|---|
| 164 | template <class T> | 
|---|
| 165 | string TVector<T>::InfoString() const | 
|---|
| 166 | { | 
|---|
| 167 | string rs = "TVector<"; | 
|---|
| 168 | rs += typeid(T).name(); | 
|---|
| 169 | char buff[64]; | 
|---|
| 170 | sprintf(buff, ">(%ld) (nr=%ld, nc=%ld)", (long)NElts(), (long)NRows(), (long)NCols()); | 
|---|
| 171 | rs += buff; | 
|---|
| 172 | return(rs); | 
|---|
| 173 |  | 
|---|
| 174 | } | 
|---|
| 175 |  | 
|---|
| 176 | /////////////////////////////////////////////////////////////// | 
|---|
| 177 | #ifdef __CXX_PRAGMA_TEMPLATES__ | 
|---|
| 178 | #pragma define_template TVector<uint_2> | 
|---|
| 179 | #pragma define_template TVector<int_4> | 
|---|
| 180 | #pragma define_template TVector<int_8> | 
|---|
| 181 | #pragma define_template TVector<r_4> | 
|---|
| 182 | #pragma define_template TVector<r_8> | 
|---|
| 183 | #pragma define_template TVector< complex<r_4> > | 
|---|
| 184 | #pragma define_template TVector< complex<r_8> > | 
|---|
| 185 | #endif | 
|---|
| 186 |  | 
|---|
| 187 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES) | 
|---|
| 188 | template class TVector<uint_2>; | 
|---|
| 189 | template class TVector<int_4>; | 
|---|
| 190 | template class TVector<int_8>; | 
|---|
| 191 | template class TVector<r_4>; | 
|---|
| 192 | template class TVector<r_8>; | 
|---|
| 193 | template class TVector< complex<r_4> >; | 
|---|
| 194 | template class TVector< complex<r_8> >; | 
|---|
| 195 | #endif | 
|---|
| 196 |  | 
|---|