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