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

Last change on this file since 2444 was 2267, checked in by ansari, 23 years ago

MAJ documentation + methode Read/WriteASCII() en virtuelle pure ds BaseArray - Reza 15/11/2002

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