1 | // $Id: tvector.cc,v 1.26 2010-08-11 22:59:10 ansari Exp $
|
---|
2 | // C.Magneville 04/99
|
---|
3 | #include "sopnamsp.h"
|
---|
4 | #include "machdefs.h"
|
---|
5 | #include <stdlib.h>
|
---|
6 | #include "pexceptions.h"
|
---|
7 |
|
---|
8 | #define TVECTOR_CC_BFILE // avoid extern template declarations
|
---|
9 | #include "tvector.h"
|
---|
10 |
|
---|
11 | namespace SOPHYA {
|
---|
12 |
|
---|
13 | /*!
|
---|
14 | \class TVector
|
---|
15 | \ingroup TArray
|
---|
16 |
|
---|
17 | The TVector class specializes the TMatrix class for representing
|
---|
18 | row or column vectors.
|
---|
19 |
|
---|
20 | \b Vector is a typedef for double precision floating point vectors ( TVector<r_8> ).
|
---|
21 |
|
---|
22 | \sa SOPHYA::TArray SOPHYA::TMatrix
|
---|
23 | \sa SOPHYA::Range SOPHYA::Sequence
|
---|
24 | \sa SOPHYA::MathArray
|
---|
25 |
|
---|
26 | The following sample code illustrates sub-vector manipulation:
|
---|
27 | \code
|
---|
28 | #include "array.h"
|
---|
29 | // ...
|
---|
30 | // Input Vector containing a noisy periodic signal
|
---|
31 | Vector in(1024), out(1024);
|
---|
32 | in = RandomSequence(RandomSequence::Gaussian, 0., 1.);
|
---|
33 | for(int kk=0; kk<in.Size(); kk++)
|
---|
34 | in(kk) += 2*sin(kk*0.05);
|
---|
35 | // Compute the output vector by a simple low pass filter
|
---|
36 | Vector out(1024);
|
---|
37 | int w = 2;
|
---|
38 | for(int k=w; k<in.Size()-w; k++)
|
---|
39 | out(k) = in(Range(k-w, k+w).Sum()/(2.*w+1.);
|
---|
40 | \endcode
|
---|
41 | */
|
---|
42 |
|
---|
43 | ////////////////////////////////////////////////////////////////
|
---|
44 | //**** Createur, Destructeur
|
---|
45 |
|
---|
46 | //! Default constructor
|
---|
47 | template <class T>
|
---|
48 | TVector<T>::TVector()
|
---|
49 | : TMatrix<T>()
|
---|
50 | {
|
---|
51 | arrtype_ = 2; // Type = Vector
|
---|
52 | }
|
---|
53 |
|
---|
54 | //! construct a vector
|
---|
55 | /*!
|
---|
56 | \param n : number of elements
|
---|
57 | \param lcv : line or column vector ?
|
---|
58 | \param mm : memory mapping type
|
---|
59 | \param fzero : if \b true , set vector elements to zero
|
---|
60 | \sa SelectVectorType
|
---|
61 | */
|
---|
62 | template <class T>
|
---|
63 | TVector<T>::TVector(sa_size_t n, short lcv, short mm, bool fzero)
|
---|
64 | // Constructeur
|
---|
65 | : TMatrix<T>(1,1,mm,false)
|
---|
66 | {
|
---|
67 | arrtype_ = 2; // Type = Vector
|
---|
68 | lcv = SelectVectorType(lcv);
|
---|
69 | ReSize(n,lcv,fzero);
|
---|
70 | }
|
---|
71 |
|
---|
72 | //! Constructor by copy
|
---|
73 | /*!
|
---|
74 | \warning datas are \b SHARED with \b a.
|
---|
75 | \sa NDataBlock::NDataBlock(const NDataBlock<T>&)
|
---|
76 | */
|
---|
77 | template <class T>
|
---|
78 | TVector<T>::TVector(const TVector<T>& a)
|
---|
79 | // Constructeur par copie
|
---|
80 | : TMatrix<T>(a)
|
---|
81 | {
|
---|
82 | arrtype_ = 2; // Type = Vector
|
---|
83 | }
|
---|
84 |
|
---|
85 | //! Constructor by copy
|
---|
86 | /*!
|
---|
87 | \param share : if true, share data. If false copy data
|
---|
88 | */
|
---|
89 | template <class T>
|
---|
90 | TVector<T>::TVector(const TVector<T>& a, bool share)
|
---|
91 | // Constructeur par copie avec possibilite de forcer le partage ou non.
|
---|
92 | : TMatrix<T>(a, share)
|
---|
93 | {
|
---|
94 | arrtype_ = 2; // Type = Vector
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | //! Constructor of a vector from a TArray \b a
|
---|
99 | /*!
|
---|
100 | \param a : TArray to be copied or shared
|
---|
101 | \param share : if true, share data. If false copy data
|
---|
102 | \param lcv : line or column vector ?
|
---|
103 | \sa SelectVectorType
|
---|
104 | */
|
---|
105 | template <class T>
|
---|
106 | TVector<T>::TVector(const TArray<T>& a, bool share, short lcv)
|
---|
107 | : TMatrix<T>(a, share)
|
---|
108 | {
|
---|
109 | if ( (size_[0] != 1) && (size_[1] != 1) )
|
---|
110 | throw SzMismatchError("TVector<T>::TVector(const TArray<T>& a) NRows()!=1 && NCols()!=1 ");
|
---|
111 | arrtype_ = 2; // Type = Vector
|
---|
112 | if ( (size_[0] == 1) && (size_[1] == 1) ) {
|
---|
113 | if (lcv == SameVectorType) lcv = a.GetVectorType();
|
---|
114 | if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType();
|
---|
115 | veceli_ = (lcv == ColumnVector ) ? marowi_ : macoli_;
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | //! Constructor of a vector from a TArray \b a , with a different data type
|
---|
120 | template <class T>
|
---|
121 | TVector<T>::TVector(const BaseArray& a)
|
---|
122 | : TMatrix<T>(a)
|
---|
123 | {
|
---|
124 | if ( (size_[0] != 1) && (size_[1] != 1) )
|
---|
125 | throw SzMismatchError("TVector<T>::TVector(const BaseArray& a) NRows()!=1 && NCols()!=1 ");
|
---|
126 | arrtype_ = 2; // Type = Vector
|
---|
127 | }
|
---|
128 |
|
---|
129 | //! Constructor from an STL vector
|
---|
130 | template <class T>
|
---|
131 | TVector<T>::TVector(const vector<T>& v, short lcv)
|
---|
132 | {
|
---|
133 | sa_size_t n = v.size();
|
---|
134 | if(n==0)
|
---|
135 | throw SzMismatchError("TVector<T>::TVector(const vector<T>& v) size()==0 ");
|
---|
136 | arrtype_ = 2; // Type = Vector
|
---|
137 | ReSize(n,lcv,false);
|
---|
138 | for(sa_size_t i=0;i<n;i++) (*this)(i) = v[i];
|
---|
139 | }
|
---|
140 |
|
---|
141 | //! Destructor
|
---|
142 | template <class T>
|
---|
143 | TVector<T>::~TVector()
|
---|
144 | {
|
---|
145 | }
|
---|
146 |
|
---|
147 | //! Resize the vector
|
---|
148 | /*!
|
---|
149 | \param n : number of elements
|
---|
150 | \param lcv : line or column vector ?
|
---|
151 | \sa SelectVectorType
|
---|
152 | */
|
---|
153 | template <class T>
|
---|
154 | void TVector<T>::ReSize(sa_size_t n, short lcv, bool fzero)
|
---|
155 | {
|
---|
156 | if( n == 0 )
|
---|
157 | throw(SzMismatchError("TVector::ReSize() n = 0 "));
|
---|
158 | sa_size_t r,c;
|
---|
159 | if (lcv == SameVectorType) lcv = GetVectorType();
|
---|
160 | else if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType();
|
---|
161 | if (lcv == ColumnVector) { r = n; c = 1; }
|
---|
162 | else { c = n; r = 1; }
|
---|
163 | TMatrix<T>::ReSize(r,c,BaseArray::SameMemoryMapping,fzero);
|
---|
164 | veceli_ = (lcv == ColumnVector ) ? marowi_ : macoli_;
|
---|
165 | }
|
---|
166 |
|
---|
167 | //! Re-allocate space for the vector
|
---|
168 | /*!
|
---|
169 | \param n : number of elements
|
---|
170 | \param lcv : line or column vector ?
|
---|
171 | \param force : if true re-allocation is forced, if not it occurs
|
---|
172 | only if the required space is greater than the old one.
|
---|
173 | \sa ReSize SelectVectorType
|
---|
174 | */
|
---|
175 | template <class T>
|
---|
176 | void TVector<T>::Realloc(sa_size_t n, short lcv, bool force)
|
---|
177 | {
|
---|
178 | if( n == 0 )
|
---|
179 | throw(SzMismatchError("TVector::Realloc() n = 0 "));
|
---|
180 | sa_size_t r,c;
|
---|
181 | if (lcv == SameVectorType) lcv = GetVectorType();
|
---|
182 | else if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType();
|
---|
183 | if (lcv == ColumnVector) { r = n; c = 1; }
|
---|
184 | else { c = n; r = 1; }
|
---|
185 | TMatrix<T>::Realloc(r,c,SameMemoryMapping,force);
|
---|
186 | veceli_ = (lcv == ColumnVector ) ? marowi_ : macoli_;
|
---|
187 | }
|
---|
188 |
|
---|
189 | // $CHECK$ Reza 03/2000 Doit-on declarer cette methode const ?
|
---|
190 | //! Return a subvector define by \b Range \b relt
|
---|
191 | template <class T>
|
---|
192 | TVector<T> TVector<T>::SubVector(Range relt) const
|
---|
193 | {
|
---|
194 | Range rr=Range::first();
|
---|
195 | Range cr=Range::first();
|
---|
196 | if (GetVectorType() == ColumnVector ) rr = relt;
|
---|
197 | else cr = relt;
|
---|
198 | TMatrix<T> const & mtx = (*this);
|
---|
199 | TVector sv( mtx(rr, cr) , true, GetVectorType() );
|
---|
200 | return(sv);
|
---|
201 | }
|
---|
202 |
|
---|
203 | //! Return info on number of rows, column and type \b T
|
---|
204 | template <class T>
|
---|
205 | string TVector<T>::InfoString() const
|
---|
206 | {
|
---|
207 | string rs = "TVector<";
|
---|
208 | rs += typeid(T).name();
|
---|
209 | char buff[64];
|
---|
210 | sprintf(buff, ">(%ld) (nr=%ld, nc=%ld)", (long)NElts(), (long)NRows(), (long)NCols());
|
---|
211 | rs += buff;
|
---|
212 | return(rs);
|
---|
213 |
|
---|
214 | }
|
---|
215 |
|
---|
216 | //! Fill from an STL vector
|
---|
217 | /*!
|
---|
218 | \param v : STL vector to copy
|
---|
219 | \param noresize : "true" means TVector keeps its size, if "false", ReSize(, ,false) is called
|
---|
220 | \warning Filling is always done starting at TVector(0)
|
---|
221 | */
|
---|
222 | template <class T>
|
---|
223 | sa_size_t TVector<T>::FillFr(const vector<T>& v,bool noresize)
|
---|
224 | {
|
---|
225 | sa_size_t n = v.size();
|
---|
226 | if(n==0) return 0; //STL vector de taille nulle!
|
---|
227 | if(!noresize) ReSize(n,SameVectorType,false);
|
---|
228 | if(Size()<n) n = Size();
|
---|
229 | if(n==0) return 0; //TVector de taille nulle sans resize!
|
---|
230 | for(sa_size_t i=0;i<n;i++) (*this)(i) = v[i];
|
---|
231 | return n;
|
---|
232 | }
|
---|
233 |
|
---|
234 | //! Fill an STL vector
|
---|
235 | /*!
|
---|
236 | \param v : STL vector to fill
|
---|
237 | \param addtoend : "true" means TVector elements will be appended at the end of "v"
|
---|
238 | */
|
---|
239 | template <class T>
|
---|
240 | sa_size_t TVector<T>::FillTo(vector<T>& v,bool addtoend)
|
---|
241 | {
|
---|
242 | sa_size_t n = Size();
|
---|
243 | if(n==0) return 0; //TVector de taille nulle!
|
---|
244 | if(!addtoend) v.resize(0);
|
---|
245 | for(sa_size_t i=0;i<n;i++) v.push_back((*this)(i));
|
---|
246 | return n;
|
---|
247 | }
|
---|
248 |
|
---|
249 | ///////////////////////////////////////////////////////////////
|
---|
250 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
251 | #pragma define_template TVector<uint_1>
|
---|
252 | #pragma define_template TVector<uint_2>
|
---|
253 | #pragma define_template TVector<uint_4>
|
---|
254 | #pragma define_template TVector<uint_8>
|
---|
255 | #pragma define_template TVector<int_1>
|
---|
256 | #pragma define_template TVector<int_2>
|
---|
257 | #pragma define_template TVector<int_4>
|
---|
258 | #pragma define_template TVector<int_8>
|
---|
259 | #pragma define_template TVector<r_4>
|
---|
260 | #pragma define_template TVector<r_8>
|
---|
261 | #pragma define_template TVector< complex<r_4> >
|
---|
262 | #pragma define_template TVector< complex<r_8> >
|
---|
263 | #ifdef SO_LDBLE128
|
---|
264 | #pragma define_template TVector<r_16>
|
---|
265 | #pragma define_template TVector< complex<r_16> >
|
---|
266 | #endif
|
---|
267 | #endif
|
---|
268 |
|
---|
269 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
270 | template class TVector<uint_1>;
|
---|
271 | template class TVector<uint_2>;
|
---|
272 | template class TVector<uint_4>;
|
---|
273 | template class TVector<uint_8>;
|
---|
274 | template class TVector<int_1>;
|
---|
275 | template class TVector<int_2>;
|
---|
276 | template class TVector<int_4>;
|
---|
277 | template class TVector<int_8>;
|
---|
278 | template class TVector<r_4>;
|
---|
279 | template class TVector<r_8>;
|
---|
280 | template class TVector< complex<r_4> >;
|
---|
281 | template class TVector< complex<r_8> >;
|
---|
282 | #ifdef SO_LDBLE128
|
---|
283 | template class TVector<r_16>;
|
---|
284 | template class TVector< complex<r_16> >;
|
---|
285 | #endif
|
---|
286 | #endif
|
---|
287 |
|
---|
288 | } // FIN namespace SOPHYA
|
---|
289 |
|
---|