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

Last change on this file since 3739 was 3661, checked in by cmv, 16 years ago
  • ajout des TArray/TMatrix/TVector <uint_1> et <int_1>
  • cet ajout n'a pas ete porte dans Image<T>
  • correction petit bug:

inline int_4 Convert(int_2& x) const {...}
-> inline int_2 Convert(int_2& x) const {...}

cmv 23/10/2009

File size: 7.7 KB
Line 
1// $Id: tvector.cc,v 1.23 2009-10-23 19:48:27 cmv 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
43template <class T>
44TVector<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 */
58template <class T>
59TVector<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*/
73template <class T>
74TVector<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 */
85template <class T>
86TVector<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 */
101template <class T>
102TVector<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
116template <class T>
117TVector<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
126template <class T>
127TVector<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
138template <class T>
139TVector<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 */
149template <class T>
150void 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 */
171template <class T>
172void 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
187template <class T>
188TVector<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
200template <class T>
201string 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 */
218template <class T>
219sa_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 */
235template <class T>
236sa_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_1>
248#pragma define_template TVector<uint_2>
249#pragma define_template TVector<uint_4>
250#pragma define_template TVector<uint_8>
251#pragma define_template TVector<int_1>
252#pragma define_template TVector<int_2>
253#pragma define_template TVector<int_4>
254#pragma define_template TVector<int_8>
255#pragma define_template TVector<r_4>
256#pragma define_template TVector<r_8>
257#pragma define_template TVector< complex<r_4> >
258#pragma define_template TVector< complex<r_8> >
259#endif
260
261#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
262namespace SOPHYA {
263template class TVector<uint_1>;
264template class TVector<uint_2>;
265template class TVector<uint_4>;
266template class TVector<uint_8>;
267template class TVector<int_1>;
268template class TVector<int_2>;
269template class TVector<int_4>;
270template class TVector<int_8>;
271template class TVector<r_4>;
272template class TVector<r_8>;
273template class TVector< complex<r_4> >;
274template class TVector< complex<r_8> >;
275}
276#endif
277
Note: See TracBrowser for help on using the repository browser.