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