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