[1103] | 1 | // $Id: tvector.cc,v 1.10 2000-07-27 00:00:10 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 | {
|
---|
[1099] | 37 | arrtype_ = 2; // Type = Vector
|
---|
[813] | 38 | lcv = SelectVectorType(lcv);
|
---|
| 39 | ReSize(n,lcv);
|
---|
[762] | 40 | }
|
---|
| 41 |
|
---|
[967] | 42 | //! Constructor by copy
|
---|
[976] | 43 | /*!
|
---|
| 44 | \warning datas are \b SHARED with \b a.
|
---|
| 45 | \sa NDataBlock::NDataBlock(const NDataBlock<T>&)
|
---|
| 46 | */
|
---|
[762] | 47 | template <class T>
|
---|
[804] | 48 | TVector<T>::TVector(const TVector<T>& a)
|
---|
[967] | 49 | // Constructeur par copie
|
---|
[804] | 50 | : TMatrix<T>(a)
|
---|
[762] | 51 | {
|
---|
[1099] | 52 | arrtype_ = 2; // Type = Vector
|
---|
[762] | 53 | }
|
---|
| 54 |
|
---|
[894] | 55 | //! Constructor by copy
|
---|
| 56 | /*!
|
---|
| 57 | \param share : if true, share data. If false copy data
|
---|
| 58 | */
|
---|
[762] | 59 | template <class T>
|
---|
[804] | 60 | TVector<T>::TVector(const TVector<T>& a, bool share)
|
---|
[762] | 61 | // Constructeur par copie avec possibilite de forcer le partage ou non.
|
---|
[804] | 62 | : TMatrix<T>(a, share)
|
---|
[762] | 63 | {
|
---|
[1099] | 64 | arrtype_ = 2; // Type = Vector
|
---|
[762] | 65 | }
|
---|
| 66 |
|
---|
[894] | 67 | //! Constructor from a TArray
|
---|
[762] | 68 | template <class T>
|
---|
[804] | 69 | TVector<T>::TVector(const TArray<T>& a)
|
---|
[762] | 70 | : TMatrix<T>(a)
|
---|
| 71 | {
|
---|
[813] | 72 | if ( (size_[0] != 1) && (size_[1] != 1) )
|
---|
| 73 | throw SzMismatchError("TVector<T>::TVector(const TArray<T>& a) NRows()!=1 && NCols()!=1 ");
|
---|
[1099] | 74 | arrtype_ = 2; // Type = Vector
|
---|
[762] | 75 | }
|
---|
| 76 |
|
---|
| 77 |
|
---|
[894] | 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 | */
|
---|
[762] | 86 | template <class T>
|
---|
[914] | 87 | TVector<T>::TVector(const TArray<T>& a, bool share, short lcv, short mm)
|
---|
[804] | 88 | : TMatrix<T>(a, share, mm)
|
---|
[762] | 89 | {
|
---|
[813] | 90 | if ( (size_[0] != 1) && (size_[1] != 1) )
|
---|
| 91 | throw SzMismatchError("TVector<T>::TVector(const TArray<T>& a) NRows()!=1 && NCols()!=1 ");
|
---|
[1099] | 92 | arrtype_ = 2; // Type = Vector
|
---|
[813] | 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_;
|
---|
[804] | 97 | }
|
---|
[762] | 98 | }
|
---|
| 99 |
|
---|
[1099] | 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)
|
---|
[1103] | 103 | : TMatrix<T>(a)
|
---|
[1099] | 104 | {
|
---|
[1103] | 105 | if ( (size_[0] != 1) && (size_[1] != 1) )
|
---|
| 106 | throw SzMismatchError("TVector<T>::TVector(const BaseArray& a) NRows()!=1 && NCols()!=1 ");
|
---|
[1099] | 107 | arrtype_ = 2; // Type = Vector
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[894] | 110 | //! Destructor
|
---|
[762] | 111 | template <class T>
|
---|
[804] | 112 | TVector<T>::~TVector()
|
---|
[762] | 113 | {
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[894] | 116 | //! Resize the vector
|
---|
| 117 | /*!
|
---|
| 118 | \param n : number of elements
|
---|
| 119 | \param lcv : line or column vector ?
|
---|
| 120 | \sa SelectVectorType
|
---|
| 121 | */
|
---|
[762] | 122 | template <class T>
|
---|
[804] | 123 | void TVector<T>::ReSize(uint_4 n, short lcv)
|
---|
[762] | 124 | {
|
---|
[804] | 125 | if( n == 0 )
|
---|
| 126 | throw(SzMismatchError("TVector::ReSize() n = 0 "));
|
---|
| 127 | uint_4 r,c;
|
---|
[813] | 128 | if (lcv == SameVectorType) lcv = GetVectorType();
|
---|
| 129 | else if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType();
|
---|
[804] | 130 | if (lcv == ColumnVector) { r = n; c = 1; }
|
---|
| 131 | else { c = n; r = 1; }
|
---|
| 132 | TMatrix<T>::ReSize(r,c);
|
---|
[813] | 133 | veceli_ = (lcv == ColumnVector ) ? marowi_ : macoli_;
|
---|
[762] | 134 | }
|
---|
| 135 |
|
---|
[894] | 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 | */
|
---|
[762] | 144 | template <class T>
|
---|
[804] | 145 | void TVector<T>::Realloc(uint_4 n, short lcv, bool force)
|
---|
[762] | 146 | {
|
---|
[804] | 147 | if( n == 0 )
|
---|
| 148 | throw(SzMismatchError("TVector::Realloc() n = 0 "));
|
---|
| 149 | uint_4 r,c;
|
---|
[813] | 150 | if (lcv == SameVectorType) lcv = GetVectorType();
|
---|
| 151 | else if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType();
|
---|
[804] | 152 | if (lcv == ColumnVector) { r = n; c = 1; }
|
---|
| 153 | else { c = n; r = 1; }
|
---|
| 154 | TMatrix<T>::Realloc(r,c,SameMemoryMapping,force);
|
---|
[813] | 155 | veceli_ = (lcv == ColumnVector ) ? marowi_ : macoli_;
|
---|
[762] | 156 | }
|
---|
| 157 |
|
---|
[804] | 158 | // $CHECK$ Reza 03/2000 Doit-on declarer cette methode const ?
|
---|
[894] | 159 | //! Return a subvector define by \b Range \b relt
|
---|
[762] | 160 | template <class T>
|
---|
[813] | 161 | TVector<T> TVector<T>::SubVector(Range relt) const
|
---|
[762] | 162 | {
|
---|
[804] | 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);
|
---|
[762] | 170 | }
|
---|
| 171 |
|
---|
[894] | 172 | //! Return the norm \f$ \sum{V(i)^2} \f$
|
---|
[762] | 173 | template <class T>
|
---|
[804] | 174 | T TVector<T>::Norm2() const
|
---|
[762] | 175 | {
|
---|
[804] | 176 | T ret = 0;
|
---|
| 177 | for(uint_8 k=0; k<Size(); k++) ret += (*this)(k)*(*this)(k);
|
---|
| 178 | return ret;
|
---|
[762] | 179 | }
|
---|
| 180 |
|
---|
[894] | 181 | //! Return info on number of rows, column and type \b T
|
---|
[813] | 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);
|
---|
[804] | 191 |
|
---|
[813] | 192 | }
|
---|
| 193 |
|
---|
[762] | 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
|
---|
[804] | 214 |
|
---|