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