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

Last change on this file since 2575 was 2575, checked in by ansari, 21 years ago

1/ Remplacement des methodes Add/Sub/Mul/DivElt(a) par

Add/Sub/Mul/DivElt(TArray a, TArray res)

2/ Operateurs += -= A+B A-B TArray et TMatrix/TVecteur modifies en consequence
3/ Ajout methode TArray::ScalarProduct()
4/ Methode TArray::SetT renomme en SetCst() SetT garde en alias
5/ Ajout parametre bool fzero (mise a zero) ajoute ds constructeur et

ReSize() de TMatrix et TVecteur.

Reza 29/07/2004

File size: 6.5 KB
Line 
1// $Id: tvector.cc,v 1.16 2004-07-29 12:31:16 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 \param fzero : if \b true , set vector elements to zero
55 \sa SelectVectorType
56 */
57template <class T>
58TVector<T>::TVector(sa_size_t n, short lcv, short mm, bool fzero)
59// Constructeur
60 : TMatrix<T>(1,1,mm,false)
61{
62 arrtype_ = 2; // Type = Vector
63 lcv = SelectVectorType(lcv);
64 ReSize(n,lcv,fzero);
65}
66
67//! Constructor by copy
68/*!
69 \warning datas are \b SHARED with \b a.
70 \sa NDataBlock::NDataBlock(const NDataBlock<T>&)
71*/
72template <class T>
73TVector<T>::TVector(const TVector<T>& a)
74// Constructeur par copie
75 : TMatrix<T>(a)
76{
77 arrtype_ = 2; // Type = Vector
78}
79
80//! Constructor by copy
81/*!
82 \param share : if true, share data. If false copy data
83 */
84template <class T>
85TVector<T>::TVector(const TVector<T>& a, bool share)
86// Constructeur par copie avec possibilite de forcer le partage ou non.
87: TMatrix<T>(a, share)
88{
89 arrtype_ = 2; // Type = Vector
90}
91
92//! Constructor from a TArray
93template <class T>
94TVector<T>::TVector(const TArray<T>& a)
95: TMatrix<T>(a)
96{
97 if ( (size_[0] != 1) && (size_[1] != 1) )
98 throw SzMismatchError("TVector<T>::TVector(const TArray<T>& a) NRows()!=1 && NCols()!=1 ");
99 arrtype_ = 2; // Type = Vector
100}
101
102
103//! Constructor of a vector from a TArray \b a
104/*!
105 \param a : TArray to be copied or shared
106 \param share : if true, share data. If false copy data
107 \param mm : define the memory mapping type
108 \param lcv : line or column vector ?
109 \sa SelectVectorType
110 */
111template <class T>
112TVector<T>::TVector(const TArray<T>& a, bool share, short lcv, short mm)
113: TMatrix<T>(a, share, mm)
114{
115 if ( (size_[0] != 1) && (size_[1] != 1) )
116 throw SzMismatchError("TVector<T>::TVector(const TArray<T>& a) NRows()!=1 && NCols()!=1 ");
117 arrtype_ = 2; // Type = Vector
118 if ( (size_[0] == 1) && (size_[1] == 1) ) {
119 if (lcv == SameVectorType) lcv = a.GetVectorType();
120 if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType();
121 veceli_ = (lcv == ColumnVector ) ? marowi_ : macoli_;
122 }
123}
124
125//! Constructor of a vector from a TArray \b a , with a different data type
126template <class T>
127TVector<T>::TVector(const BaseArray& a)
128: TMatrix<T>(a)
129{
130 if ( (size_[0] != 1) && (size_[1] != 1) )
131 throw SzMismatchError("TVector<T>::TVector(const BaseArray& a) NRows()!=1 && NCols()!=1 ");
132 arrtype_ = 2; // Type = Vector
133}
134
135//! Destructor
136template <class T>
137TVector<T>::~TVector()
138{
139}
140
141//! Resize the vector
142/*!
143 \param n : number of elements
144 \param lcv : line or column vector ?
145 \sa SelectVectorType
146 */
147template <class T>
148void TVector<T>::ReSize(sa_size_t n, short lcv, bool fzero)
149{
150 if( n == 0 )
151 throw(SzMismatchError("TVector::ReSize() n = 0 "));
152 sa_size_t r,c;
153 if (lcv == SameVectorType) lcv = GetVectorType();
154 else if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType();
155 if (lcv == ColumnVector) { r = n; c = 1; }
156 else { c = n; r = 1; }
157 TMatrix<T>::ReSize(r,c,BaseArray::SameMemoryMapping,fzero);
158 veceli_ = (lcv == ColumnVector ) ? marowi_ : macoli_;
159}
160
161//! Re-allocate space for the vector
162/*!
163 \param n : number of elements
164 \param lcv : line or column vector ?
165 \param force : if true re-allocation is forced, if not it occurs
166 only if the required space is greater than the old one.
167 \sa ReSize SelectVectorType
168 */
169template <class T>
170void TVector<T>::Realloc(sa_size_t n, short lcv, bool force)
171{
172 if( n == 0 )
173 throw(SzMismatchError("TVector::Realloc() n = 0 "));
174 sa_size_t r,c;
175 if (lcv == SameVectorType) lcv = GetVectorType();
176 else if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType();
177 if (lcv == ColumnVector) { r = n; c = 1; }
178 else { c = n; r = 1; }
179 TMatrix<T>::Realloc(r,c,SameMemoryMapping,force);
180 veceli_ = (lcv == ColumnVector ) ? marowi_ : macoli_;
181}
182
183// $CHECK$ Reza 03/2000 Doit-on declarer cette methode const ?
184//! Return a subvector define by \b Range \b relt
185template <class T>
186TVector<T> TVector<T>::SubVector(Range relt) const
187{
188 Range rr, cr;
189 if (GetVectorType() == ColumnVector ) rr = relt;
190 else cr = relt;
191 TMatrix<T> const & mtx = (*this);
192 TVector sv( mtx(rr, cr) , true, GetVectorType(), GetMemoryMapping() );
193 return(sv);
194}
195
196//! Return info on number of rows, column and type \b T
197template <class T>
198string TVector<T>::InfoString() const
199{
200 string rs = "TVector<";
201 rs += typeid(T).name();
202 char buff[64];
203 sprintf(buff, ">(%ld) (nr=%ld, nc=%ld)", (long)NElts(), (long)NRows(), (long)NCols());
204 rs += buff;
205 return(rs);
206
207}
208
209///////////////////////////////////////////////////////////////
210#ifdef __CXX_PRAGMA_TEMPLATES__
211#pragma define_template TVector<uint_2>
212#pragma define_template TVector<uint_8>
213#pragma define_template TVector<int_4>
214#pragma define_template TVector<int_8>
215#pragma define_template TVector<r_4>
216#pragma define_template TVector<r_8>
217#pragma define_template TVector< complex<r_4> >
218#pragma define_template TVector< complex<r_8> >
219#endif
220
221#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
222template class TVector<uint_2>;
223template class TVector<uint_8>;
224template class TVector<int_4>;
225template class TVector<int_8>;
226template class TVector<r_4>;
227template class TVector<r_8>;
228template class TVector< complex<r_4> >;
229template class TVector< complex<r_8> >;
230#endif
231
Note: See TracBrowser for help on using the repository browser.