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

Last change on this file since 2147 was 1624, checked in by cmv, 24 years ago

On enleve les SetTemp() inutiles cmv 6/8/01

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