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