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