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