[1099] | 1 | // $Id: tvector.cc,v 1.9 2000-07-26 16:29:46 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)
|
---|
| 103 | : TMatrix<T>()
|
---|
| 104 | {
|
---|
| 105 | arrtype_ = 2; // Type = Vector
|
---|
| 106 | SetBA(a);
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[894] | 109 | //! Destructor
|
---|
[762] | 110 | template <class T>
|
---|
[804] | 111 | TVector<T>::~TVector()
|
---|
[762] | 112 | {
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[894] | 115 | //! Resize the vector
|
---|
| 116 | /*!
|
---|
| 117 | \param n : number of elements
|
---|
| 118 | \param lcv : line or column vector ?
|
---|
| 119 | \sa SelectVectorType
|
---|
| 120 | */
|
---|
[762] | 121 | template <class T>
|
---|
[804] | 122 | void TVector<T>::ReSize(uint_4 n, short lcv)
|
---|
[762] | 123 | {
|
---|
[804] | 124 | if( n == 0 )
|
---|
| 125 | throw(SzMismatchError("TVector::ReSize() n = 0 "));
|
---|
| 126 | uint_4 r,c;
|
---|
[813] | 127 | if (lcv == SameVectorType) lcv = GetVectorType();
|
---|
| 128 | else if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType();
|
---|
[804] | 129 | if (lcv == ColumnVector) { r = n; c = 1; }
|
---|
| 130 | else { c = n; r = 1; }
|
---|
| 131 | TMatrix<T>::ReSize(r,c);
|
---|
[813] | 132 | veceli_ = (lcv == ColumnVector ) ? marowi_ : macoli_;
|
---|
[762] | 133 | }
|
---|
| 134 |
|
---|
[894] | 135 | //! Re-allocate space for the vector
|
---|
| 136 | /*!
|
---|
| 137 | \param n : number of elements
|
---|
| 138 | \param lcv : line or column vector ?
|
---|
| 139 | \param force : if true re-allocation is forced, if not it occurs
|
---|
| 140 | only if the required space is greater than the old one.
|
---|
| 141 | \sa ReSize SelectVectorType
|
---|
| 142 | */
|
---|
[762] | 143 | template <class T>
|
---|
[804] | 144 | void TVector<T>::Realloc(uint_4 n, short lcv, bool force)
|
---|
[762] | 145 | {
|
---|
[804] | 146 | if( n == 0 )
|
---|
| 147 | throw(SzMismatchError("TVector::Realloc() n = 0 "));
|
---|
| 148 | uint_4 r,c;
|
---|
[813] | 149 | if (lcv == SameVectorType) lcv = GetVectorType();
|
---|
| 150 | else if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType();
|
---|
[804] | 151 | if (lcv == ColumnVector) { r = n; c = 1; }
|
---|
| 152 | else { c = n; r = 1; }
|
---|
| 153 | TMatrix<T>::Realloc(r,c,SameMemoryMapping,force);
|
---|
[813] | 154 | veceli_ = (lcv == ColumnVector ) ? marowi_ : macoli_;
|
---|
[762] | 155 | }
|
---|
| 156 |
|
---|
[804] | 157 | // $CHECK$ Reza 03/2000 Doit-on declarer cette methode const ?
|
---|
[894] | 158 | //! Return a subvector define by \b Range \b relt
|
---|
[762] | 159 | template <class T>
|
---|
[813] | 160 | TVector<T> TVector<T>::SubVector(Range relt) const
|
---|
[762] | 161 | {
|
---|
[804] | 162 | Range rr, cr;
|
---|
| 163 | if (GetVectorType() == ColumnVector ) rr = relt;
|
---|
| 164 | else cr = relt;
|
---|
| 165 | TMatrix<T> const & mtx = (*this);
|
---|
| 166 | TVector sv( mtx(rr, cr) , true, GetVectorType(), GetMemoryMapping() );
|
---|
| 167 | sv.SetTemp(true);
|
---|
| 168 | return(sv);
|
---|
[762] | 169 | }
|
---|
| 170 |
|
---|
[894] | 171 | //! Return the norm \f$ \sum{V(i)^2} \f$
|
---|
[762] | 172 | template <class T>
|
---|
[804] | 173 | T TVector<T>::Norm2() const
|
---|
[762] | 174 | {
|
---|
[804] | 175 | T ret = 0;
|
---|
| 176 | for(uint_8 k=0; k<Size(); k++) ret += (*this)(k)*(*this)(k);
|
---|
| 177 | return ret;
|
---|
[762] | 178 | }
|
---|
| 179 |
|
---|
[894] | 180 | //! Return info on number of rows, column and type \b T
|
---|
[813] | 181 | template <class T>
|
---|
| 182 | string TVector<T>::InfoString() const
|
---|
| 183 | {
|
---|
| 184 | string rs = "TVector<";
|
---|
| 185 | rs += typeid(T).name();
|
---|
| 186 | char buff[64];
|
---|
| 187 | sprintf(buff, ">(%ld) (nr=%ld, nc=%ld)", (long)NElts(), (long)NRows(), (long)NCols());
|
---|
| 188 | rs += buff;
|
---|
| 189 | return(rs);
|
---|
[804] | 190 |
|
---|
[813] | 191 | }
|
---|
| 192 |
|
---|
[762] | 193 | ///////////////////////////////////////////////////////////////
|
---|
| 194 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
| 195 | #pragma define_template TVector<uint_2>
|
---|
| 196 | #pragma define_template TVector<int_4>
|
---|
| 197 | #pragma define_template TVector<int_8>
|
---|
| 198 | #pragma define_template TVector<r_4>
|
---|
| 199 | #pragma define_template TVector<r_8>
|
---|
| 200 | #pragma define_template TVector< complex<r_4> >
|
---|
| 201 | #pragma define_template TVector< complex<r_8> >
|
---|
| 202 | #endif
|
---|
| 203 |
|
---|
| 204 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
| 205 | template class TVector<uint_2>;
|
---|
| 206 | template class TVector<int_4>;
|
---|
| 207 | template class TVector<int_8>;
|
---|
| 208 | template class TVector<r_4>;
|
---|
| 209 | template class TVector<r_8>;
|
---|
| 210 | template class TVector< complex<r_4> >;
|
---|
| 211 | template class TVector< complex<r_8> >;
|
---|
| 212 | #endif
|
---|
[804] | 213 |
|
---|