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

Last change on this file since 1554 was 1543, checked in by ansari, 24 years ago

instanciation de TArray,TMatrix,TVector en uint_8 - Reza 18/6/2001

File size: 5.8 KB
Line 
1// $Id: tvector.cc,v 1.12 2001-06-18 14:47:10 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 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}
24
25//! construct a vector
26/*!
27 \param n : number of elements
28 \param lcv : line or column vector ?
29 \param mm : memory mapping type
30 \sa SelectVectorType
31 */
32template <class T>
33TVector<T>::TVector(sa_size_t n, short lcv, short mm)
34// Constructeur
35 : TMatrix<T>(1,1,mm)
36{
37 arrtype_ = 2; // Type = Vector
38 lcv = SelectVectorType(lcv);
39 ReSize(n,lcv);
40}
41
42//! Constructor by copy
43/*!
44 \warning datas are \b SHARED with \b a.
45 \sa NDataBlock::NDataBlock(const NDataBlock<T>&)
46*/
47template <class T>
48TVector<T>::TVector(const TVector<T>& a)
49// Constructeur par copie
50 : TMatrix<T>(a)
51{
52 arrtype_ = 2; // Type = Vector
53}
54
55//! Constructor by copy
56/*!
57 \param share : if true, share data. If false copy data
58 */
59template <class T>
60TVector<T>::TVector(const TVector<T>& a, bool share)
61// Constructeur par copie avec possibilite de forcer le partage ou non.
62: TMatrix<T>(a, share)
63{
64 arrtype_ = 2; // Type = Vector
65}
66
67//! Constructor from a TArray
68template <class T>
69TVector<T>::TVector(const TArray<T>& a)
70: TMatrix<T>(a)
71{
72 if ( (size_[0] != 1) && (size_[1] != 1) )
73 throw SzMismatchError("TVector<T>::TVector(const TArray<T>& a) NRows()!=1 && NCols()!=1 ");
74 arrtype_ = 2; // Type = Vector
75}
76
77
78//! Constructor of a vector from a TArray \b a
79/*!
80 \param a : TArray to be copied or shared
81 \param share : if true, share data. If false copy data
82 \param mm : define the memory mapping type
83 \param lcv : line or column vector ?
84 \sa SelectVectorType
85 */
86template <class T>
87TVector<T>::TVector(const TArray<T>& a, bool share, short lcv, short mm)
88: TMatrix<T>(a, share, mm)
89{
90 if ( (size_[0] != 1) && (size_[1] != 1) )
91 throw SzMismatchError("TVector<T>::TVector(const TArray<T>& a) NRows()!=1 && NCols()!=1 ");
92 arrtype_ = 2; // Type = Vector
93 if ( (size_[0] == 1) && (size_[1] == 1) ) {
94 if (lcv == SameVectorType) lcv = a.GetVectorType();
95 if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType();
96 veceli_ = (lcv == ColumnVector ) ? marowi_ : macoli_;
97 }
98}
99
100//! Constructor of a vector from a TArray \b a , with a different data type
101template <class T>
102TVector<T>::TVector(const BaseArray& a)
103: TMatrix<T>(a)
104{
105 if ( (size_[0] != 1) && (size_[1] != 1) )
106 throw SzMismatchError("TVector<T>::TVector(const BaseArray& a) NRows()!=1 && NCols()!=1 ");
107 arrtype_ = 2; // Type = Vector
108}
109
110//! Destructor
111template <class T>
112TVector<T>::~TVector()
113{
114}
115
116//! Resize the vector
117/*!
118 \param n : number of elements
119 \param lcv : line or column vector ?
120 \sa SelectVectorType
121 */
122template <class T>
123void TVector<T>::ReSize(sa_size_t n, short lcv)
124{
125 if( n == 0 )
126 throw(SzMismatchError("TVector::ReSize() n = 0 "));
127 sa_size_t r,c;
128 if (lcv == SameVectorType) lcv = GetVectorType();
129 else if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType();
130 if (lcv == ColumnVector) { r = n; c = 1; }
131 else { c = n; r = 1; }
132 TMatrix<T>::ReSize(r,c);
133 veceli_ = (lcv == ColumnVector ) ? marowi_ : macoli_;
134}
135
136//! Re-allocate space for the vector
137/*!
138 \param n : number of elements
139 \param lcv : line or column vector ?
140 \param force : if true re-allocation is forced, if not it occurs
141 only if the required space is greater than the old one.
142 \sa ReSize SelectVectorType
143 */
144template <class T>
145void TVector<T>::Realloc(sa_size_t n, short lcv, bool force)
146{
147 if( n == 0 )
148 throw(SzMismatchError("TVector::Realloc() n = 0 "));
149 sa_size_t r,c;
150 if (lcv == SameVectorType) lcv = GetVectorType();
151 else if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType();
152 if (lcv == ColumnVector) { r = n; c = 1; }
153 else { c = n; r = 1; }
154 TMatrix<T>::Realloc(r,c,SameMemoryMapping,force);
155 veceli_ = (lcv == ColumnVector ) ? marowi_ : macoli_;
156}
157
158// $CHECK$ Reza 03/2000 Doit-on declarer cette methode const ?
159//! Return a subvector define by \b Range \b relt
160template <class T>
161TVector<T> TVector<T>::SubVector(Range relt) const
162{
163 Range rr, cr;
164 if (GetVectorType() == ColumnVector ) rr = relt;
165 else cr = relt;
166 TMatrix<T> const & mtx = (*this);
167 TVector sv( mtx(rr, cr) , true, GetVectorType(), GetMemoryMapping() );
168 sv.SetTemp(true);
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.