| [967] | 1 | // $Id: tvector.cc,v 1.7 2000-04-21 16:31:26 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 | Class of vector (line or column) | 
|---|
|  | 12 | \sa TMatrix TArray | 
|---|
|  | 13 | */ | 
|---|
|  | 14 |  | 
|---|
| [804] | 15 | //////////////////////////////////////////////////////////////// | 
|---|
|  | 16 | //**** Createur, Destructeur | 
|---|
|  | 17 |  | 
|---|
| [894] | 18 | //! Default constructor | 
|---|
| [762] | 19 | template <class T> | 
|---|
| [804] | 20 | TVector<T>::TVector() | 
|---|
|  | 21 | : TMatrix<T>() | 
|---|
| [762] | 22 | { | 
|---|
|  | 23 | } | 
|---|
|  | 24 |  | 
|---|
| [894] | 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 | */ | 
|---|
| [762] | 32 | template <class T> | 
|---|
| [804] | 33 | TVector<T>::TVector(uint_4 n, short lcv, short mm) | 
|---|
|  | 34 | // Constructeur | 
|---|
| [813] | 35 | : TMatrix<T>(1,1,mm) | 
|---|
| [762] | 36 | { | 
|---|
| [813] | 37 | lcv = SelectVectorType(lcv); | 
|---|
|  | 38 | ReSize(n,lcv); | 
|---|
| [762] | 39 | } | 
|---|
|  | 40 |  | 
|---|
| [967] | 41 | //! Constructor by copy | 
|---|
|  | 42 | /*! \sa NDataBlock::NDataBlock(const NDataBlock<T>&) */ | 
|---|
| [762] | 43 | template <class T> | 
|---|
| [804] | 44 | TVector<T>::TVector(const TVector<T>& a) | 
|---|
| [967] | 45 | // Constructeur par copie | 
|---|
| [804] | 46 | : TMatrix<T>(a) | 
|---|
| [762] | 47 | { | 
|---|
|  | 48 | } | 
|---|
|  | 49 |  | 
|---|
| [894] | 50 | //! Constructor by copy | 
|---|
|  | 51 | /*! | 
|---|
|  | 52 | \param share : if true, share data. If false copy data | 
|---|
|  | 53 | */ | 
|---|
| [762] | 54 | template <class T> | 
|---|
| [804] | 55 | TVector<T>::TVector(const TVector<T>& a, bool share) | 
|---|
| [762] | 56 | // Constructeur par copie avec possibilite de forcer le partage ou non. | 
|---|
| [804] | 57 | : TMatrix<T>(a, share) | 
|---|
| [762] | 58 | { | 
|---|
|  | 59 | } | 
|---|
|  | 60 |  | 
|---|
| [894] | 61 | //! Constructor from a TArray | 
|---|
| [762] | 62 | template <class T> | 
|---|
| [804] | 63 | TVector<T>::TVector(const TArray<T>& a) | 
|---|
| [762] | 64 | : TMatrix<T>(a) | 
|---|
|  | 65 | { | 
|---|
| [813] | 66 | if ( (size_[0] != 1) && (size_[1] != 1) ) | 
|---|
|  | 67 | throw SzMismatchError("TVector<T>::TVector(const TArray<T>& a) NRows()!=1 && NCols()!=1 "); | 
|---|
| [762] | 68 | } | 
|---|
|  | 69 |  | 
|---|
|  | 70 |  | 
|---|
| [894] | 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 | */ | 
|---|
| [762] | 79 | template <class T> | 
|---|
| [914] | 80 | TVector<T>::TVector(const TArray<T>& a, bool share, short lcv, short mm) | 
|---|
| [804] | 81 | : TMatrix<T>(a, share, mm) | 
|---|
| [762] | 82 | { | 
|---|
| [813] | 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_; | 
|---|
| [804] | 89 | } | 
|---|
| [762] | 90 | } | 
|---|
|  | 91 |  | 
|---|
| [894] | 92 | //! Destructor | 
|---|
| [762] | 93 | template <class T> | 
|---|
| [804] | 94 | TVector<T>::~TVector() | 
|---|
| [762] | 95 | { | 
|---|
|  | 96 | } | 
|---|
|  | 97 |  | 
|---|
| [894] | 98 | //! Resize the vector | 
|---|
|  | 99 | /*! | 
|---|
|  | 100 | \param n : number of elements | 
|---|
|  | 101 | \param lcv : line or column vector ? | 
|---|
|  | 102 | \sa SelectVectorType | 
|---|
|  | 103 | */ | 
|---|
| [762] | 104 | template <class T> | 
|---|
| [804] | 105 | void TVector<T>::ReSize(uint_4 n, short lcv) | 
|---|
| [762] | 106 | { | 
|---|
| [804] | 107 | if( n == 0 ) | 
|---|
|  | 108 | throw(SzMismatchError("TVector::ReSize() n = 0 ")); | 
|---|
|  | 109 | uint_4 r,c; | 
|---|
| [813] | 110 | if (lcv == SameVectorType)  lcv = GetVectorType(); | 
|---|
|  | 111 | else if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType(); | 
|---|
| [804] | 112 | if (lcv == ColumnVector) { r = n;  c = 1; } | 
|---|
|  | 113 | else { c = n; r = 1; } | 
|---|
|  | 114 | TMatrix<T>::ReSize(r,c); | 
|---|
| [813] | 115 | veceli_ = (lcv ==  ColumnVector ) ?  marowi_ : macoli_; | 
|---|
| [762] | 116 | } | 
|---|
|  | 117 |  | 
|---|
| [894] | 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 | */ | 
|---|
| [762] | 126 | template <class T> | 
|---|
| [804] | 127 | void TVector<T>::Realloc(uint_4 n, short lcv, bool force) | 
|---|
| [762] | 128 | { | 
|---|
| [804] | 129 | if( n == 0 ) | 
|---|
|  | 130 | throw(SzMismatchError("TVector::Realloc() n = 0 ")); | 
|---|
|  | 131 | uint_4 r,c; | 
|---|
| [813] | 132 | if (lcv == SameVectorType)  lcv = GetVectorType(); | 
|---|
|  | 133 | else if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType(); | 
|---|
| [804] | 134 | if (lcv == ColumnVector) { r = n;  c = 1; } | 
|---|
|  | 135 | else { c = n; r = 1; } | 
|---|
|  | 136 | TMatrix<T>::Realloc(r,c,SameMemoryMapping,force); | 
|---|
| [813] | 137 | veceli_ = (lcv ==  ColumnVector ) ?  marowi_ : macoli_; | 
|---|
| [762] | 138 | } | 
|---|
|  | 139 |  | 
|---|
| [804] | 140 | // $CHECK$ Reza 03/2000  Doit-on declarer cette methode const ? | 
|---|
| [894] | 141 | //! Return a subvector define by \b Range \b relt | 
|---|
| [762] | 142 | template <class T> | 
|---|
| [813] | 143 | TVector<T> TVector<T>::SubVector(Range relt) const | 
|---|
| [762] | 144 | { | 
|---|
| [804] | 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); | 
|---|
| [762] | 152 | } | 
|---|
|  | 153 |  | 
|---|
| [894] | 154 | //! Return the norm \f$ \sum{V(i)^2} \f$ | 
|---|
| [762] | 155 | template <class T> | 
|---|
| [804] | 156 | T TVector<T>::Norm2() const | 
|---|
| [762] | 157 | { | 
|---|
| [804] | 158 | T ret = 0; | 
|---|
|  | 159 | for(uint_8 k=0; k<Size(); k++)   ret += (*this)(k)*(*this)(k); | 
|---|
|  | 160 | return ret; | 
|---|
| [762] | 161 | } | 
|---|
|  | 162 |  | 
|---|
| [894] | 163 | //! Return info on number of rows, column and type \b T | 
|---|
| [813] | 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); | 
|---|
| [804] | 173 |  | 
|---|
| [813] | 174 | } | 
|---|
|  | 175 |  | 
|---|
| [762] | 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 | 
|---|
| [804] | 196 |  | 
|---|