source: Sophya/trunk/SophyaLib/TArray/tvector.cc@ 3753

Last change on this file since 3753 was 3751, checked in by ansari, 16 years ago

Prise en charge de float 128 bits (r_16, complex<r_16>) par TArray<T>,TMatrix<T>,TVector<T>. activation par le flag de compilation SO_LDBLE128

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