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

Last change on this file since 2786 was 2752, checked in by ansari, 20 years ago

1/ Suppression du constructeur de copie avec specification d'organisation memoire (MemoryMapping) pour TMatrix<T> et TVector<T>
2/ Amelioration de l'impression des TArray/TMatrix avec la specification setw(largeur)
3/ Correction petit bug dans la lecture fichiers ASCII argument non transmis ds utilarr.cc

Reza 23 Mai 2005

File size: 7.3 KB
Line 
1// $Id: tvector.cc,v 1.19 2005-05-23 16:18:17 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
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, cr;
191 if (GetVectorType() == ColumnVector ) rr = relt;
192 else cr = relt;
193 TMatrix<T> const & mtx = (*this);
194 TVector sv( mtx(rr, cr) , true, GetVectorType() );
195 return(sv);
196}
197
198//! Return info on number of rows, column and type \b T
199template <class T>
200string TVector<T>::InfoString() const
201{
202 string rs = "TVector<";
203 rs += typeid(T).name();
204 char buff[64];
205 sprintf(buff, ">(%ld) (nr=%ld, nc=%ld)", (long)NElts(), (long)NRows(), (long)NCols());
206 rs += buff;
207 return(rs);
208
209}
210
211//! Fill from a STL vector
212/*!
213 \param v : STL vector to copy
214 \param noresize : "true" means TVector keeps its size
215 \warning Filling is always done starting at TVector(0)
216 */
217template <class T>
218sa_size_t TVector<T>::FillFr(const vector<T>& v,bool noresize)
219{
220 sa_size_t n = v.size();
221 if(n==0) return 0; //STL vector de taille nulle!
222 if(!noresize) ReSize(n,SameVectorType,false);
223 if(Size()<n) n = Size();
224 if(n==0) return 0; //TVector de taille nulle sans resize!
225 for(sa_size_t i=0;i<n;i++) (*this)(i) = v[i];
226 return n;
227}
228
229//! Fill a STL vector
230/*!
231 \param v : STL vector to fill
232 \param addtoend : "true" means TVector will be added at the end of "v"
233 */
234template <class T>
235sa_size_t TVector<T>::FillTo(vector<T>& v,bool addtoend)
236{
237 sa_size_t n = Size();
238 if(n==0) return 0; //TVector de taille nulle!
239 if(!addtoend) v.resize(0);
240 for(sa_size_t i=0;i<n;i++) v.push_back((*this)(i));
241 return n;
242}
243
244///////////////////////////////////////////////////////////////
245#ifdef __CXX_PRAGMA_TEMPLATES__
246#pragma define_template TVector<uint_2>
247#pragma define_template TVector<uint_8>
248#pragma define_template TVector<int_4>
249#pragma define_template TVector<int_8>
250#pragma define_template TVector<r_4>
251#pragma define_template TVector<r_8>
252#pragma define_template TVector< complex<r_4> >
253#pragma define_template TVector< complex<r_8> >
254#endif
255
256#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
257template class TVector<uint_2>;
258template class TVector<uint_8>;
259template class TVector<int_4>;
260template class TVector<int_8>;
261template class TVector<r_4>;
262template class TVector<r_8>;
263template class TVector< complex<r_4> >;
264template class TVector< complex<r_8> >;
265#endif
266
Note: See TracBrowser for help on using the repository browser.