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