[914] | 1 | // $Id: tvector.cc,v 1.5 2000-04-13 16:04:49 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 |
|
---|
[804] | 8 | ////////////////////////////////////////////////////////////////
|
---|
| 9 | //**** Createur, Destructeur
|
---|
| 10 |
|
---|
[894] | 11 | //! Default constructor
|
---|
[762] | 12 | template <class T>
|
---|
[804] | 13 | TVector<T>::TVector()
|
---|
| 14 | : TMatrix<T>()
|
---|
[762] | 15 | {
|
---|
| 16 | }
|
---|
| 17 |
|
---|
[894] | 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 | */
|
---|
[762] | 25 | template <class T>
|
---|
[804] | 26 | TVector<T>::TVector(uint_4 n, short lcv, short mm)
|
---|
| 27 | // Constructeur
|
---|
[813] | 28 | : TMatrix<T>(1,1,mm)
|
---|
[762] | 29 | {
|
---|
[813] | 30 | lcv = SelectVectorType(lcv);
|
---|
| 31 | ReSize(n,lcv);
|
---|
[762] | 32 | }
|
---|
| 33 |
|
---|
[894] | 34 | //! Constructor by copy (share if \b a is temporary)
|
---|
[762] | 35 | template <class T>
|
---|
[804] | 36 | TVector<T>::TVector(const TVector<T>& a)
|
---|
| 37 | // Constructeur par copie (partage si "a" temporaire).
|
---|
| 38 | : TMatrix<T>(a)
|
---|
[762] | 39 | {
|
---|
| 40 | }
|
---|
| 41 |
|
---|
[894] | 42 | //! Constructor by copy
|
---|
| 43 | /*!
|
---|
| 44 | \param share : if true, share data. If false copy data
|
---|
| 45 | */
|
---|
[762] | 46 | template <class T>
|
---|
[804] | 47 | TVector<T>::TVector(const TVector<T>& a, bool share)
|
---|
[762] | 48 | // Constructeur par copie avec possibilite de forcer le partage ou non.
|
---|
[804] | 49 | : TMatrix<T>(a, share)
|
---|
[762] | 50 | {
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[894] | 53 | //! Constructor from a TArray
|
---|
[762] | 54 | template <class T>
|
---|
[804] | 55 | TVector<T>::TVector(const TArray<T>& a)
|
---|
[762] | 56 | : TMatrix<T>(a)
|
---|
| 57 | {
|
---|
[813] | 58 | if ( (size_[0] != 1) && (size_[1] != 1) )
|
---|
| 59 | throw SzMismatchError("TVector<T>::TVector(const TArray<T>& a) NRows()!=1 && NCols()!=1 ");
|
---|
[762] | 60 | }
|
---|
| 61 |
|
---|
| 62 |
|
---|
[894] | 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 | */
|
---|
[762] | 71 | template <class T>
|
---|
[914] | 72 | TVector<T>::TVector(const TArray<T>& a, bool share, short lcv, short mm)
|
---|
[804] | 73 | : TMatrix<T>(a, share, mm)
|
---|
[762] | 74 | {
|
---|
[813] | 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_;
|
---|
[804] | 81 | }
|
---|
[762] | 82 | }
|
---|
| 83 |
|
---|
[894] | 84 | //! Destructor
|
---|
[762] | 85 | template <class T>
|
---|
[804] | 86 | TVector<T>::~TVector()
|
---|
[762] | 87 | {
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[894] | 90 | //! Resize the vector
|
---|
| 91 | /*!
|
---|
| 92 | \param n : number of elements
|
---|
| 93 | \param lcv : line or column vector ?
|
---|
| 94 | \sa SelectVectorType
|
---|
| 95 | */
|
---|
[762] | 96 | template <class T>
|
---|
[804] | 97 | void TVector<T>::ReSize(uint_4 n, short lcv)
|
---|
[762] | 98 | {
|
---|
[804] | 99 | if( n == 0 )
|
---|
| 100 | throw(SzMismatchError("TVector::ReSize() n = 0 "));
|
---|
| 101 | uint_4 r,c;
|
---|
[813] | 102 | if (lcv == SameVectorType) lcv = GetVectorType();
|
---|
| 103 | else if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType();
|
---|
[804] | 104 | if (lcv == ColumnVector) { r = n; c = 1; }
|
---|
| 105 | else { c = n; r = 1; }
|
---|
| 106 | TMatrix<T>::ReSize(r,c);
|
---|
[813] | 107 | veceli_ = (lcv == ColumnVector ) ? marowi_ : macoli_;
|
---|
[762] | 108 | }
|
---|
| 109 |
|
---|
[894] | 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 | */
|
---|
[762] | 118 | template <class T>
|
---|
[804] | 119 | void TVector<T>::Realloc(uint_4 n, short lcv, bool force)
|
---|
[762] | 120 | {
|
---|
[804] | 121 | if( n == 0 )
|
---|
| 122 | throw(SzMismatchError("TVector::Realloc() n = 0 "));
|
---|
| 123 | uint_4 r,c;
|
---|
[813] | 124 | if (lcv == SameVectorType) lcv = GetVectorType();
|
---|
| 125 | else if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType();
|
---|
[804] | 126 | if (lcv == ColumnVector) { r = n; c = 1; }
|
---|
| 127 | else { c = n; r = 1; }
|
---|
| 128 | TMatrix<T>::Realloc(r,c,SameMemoryMapping,force);
|
---|
[813] | 129 | veceli_ = (lcv == ColumnVector ) ? marowi_ : macoli_;
|
---|
[762] | 130 | }
|
---|
| 131 |
|
---|
[804] | 132 | // $CHECK$ Reza 03/2000 Doit-on declarer cette methode const ?
|
---|
[894] | 133 | //! Return a subvector define by \b Range \b relt
|
---|
[762] | 134 | template <class T>
|
---|
[813] | 135 | TVector<T> TVector<T>::SubVector(Range relt) const
|
---|
[762] | 136 | {
|
---|
[804] | 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);
|
---|
[762] | 144 | }
|
---|
| 145 |
|
---|
[894] | 146 | //! Return the norm \f$ \sum{V(i)^2} \f$
|
---|
[762] | 147 | template <class T>
|
---|
[804] | 148 | T TVector<T>::Norm2() const
|
---|
[762] | 149 | {
|
---|
[804] | 150 | T ret = 0;
|
---|
| 151 | for(uint_8 k=0; k<Size(); k++) ret += (*this)(k)*(*this)(k);
|
---|
| 152 | return ret;
|
---|
[762] | 153 | }
|
---|
| 154 |
|
---|
[894] | 155 | //! Return info on number of rows, column and type \b T
|
---|
[813] | 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);
|
---|
[804] | 165 |
|
---|
[813] | 166 | }
|
---|
| 167 |
|
---|
[762] | 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
|
---|
[804] | 188 |
|
---|