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

Last change on this file since 1099 was 1099, checked in by ansari, 25 years ago

Protection integrite TMatrix,TVector - operateur = (BaseArray & pour TVector et operations entre matrices avec <> MemMapping (Pas termine) Reza 27/6/2000

File size: 5.6 KB
Line 
1// $Id: tvector.cc,v 1.9 2000-07-26 16:29:46 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(uint_4 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>()
104{
105 arrtype_ = 2; // Type = Vector
106 SetBA(a);
107}
108
109//! Destructor
110template <class T>
111TVector<T>::~TVector()
112{
113}
114
115//! Resize the vector
116/*!
117 \param n : number of elements
118 \param lcv : line or column vector ?
119 \sa SelectVectorType
120 */
121template <class T>
122void TVector<T>::ReSize(uint_4 n, short lcv)
123{
124 if( n == 0 )
125 throw(SzMismatchError("TVector::ReSize() n = 0 "));
126 uint_4 r,c;
127 if (lcv == SameVectorType) lcv = GetVectorType();
128 else if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType();
129 if (lcv == ColumnVector) { r = n; c = 1; }
130 else { c = n; r = 1; }
131 TMatrix<T>::ReSize(r,c);
132 veceli_ = (lcv == ColumnVector ) ? marowi_ : macoli_;
133}
134
135//! Re-allocate space for the vector
136/*!
137 \param n : number of elements
138 \param lcv : line or column vector ?
139 \param force : if true re-allocation is forced, if not it occurs
140 only if the required space is greater than the old one.
141 \sa ReSize SelectVectorType
142 */
143template <class T>
144void TVector<T>::Realloc(uint_4 n, short lcv, bool force)
145{
146 if( n == 0 )
147 throw(SzMismatchError("TVector::Realloc() n = 0 "));
148 uint_4 r,c;
149 if (lcv == SameVectorType) lcv = GetVectorType();
150 else if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType();
151 if (lcv == ColumnVector) { r = n; c = 1; }
152 else { c = n; r = 1; }
153 TMatrix<T>::Realloc(r,c,SameMemoryMapping,force);
154 veceli_ = (lcv == ColumnVector ) ? marowi_ : macoli_;
155}
156
157// $CHECK$ Reza 03/2000 Doit-on declarer cette methode const ?
158//! Return a subvector define by \b Range \b relt
159template <class T>
160TVector<T> TVector<T>::SubVector(Range relt) const
161{
162 Range rr, cr;
163 if (GetVectorType() == ColumnVector ) rr = relt;
164 else cr = relt;
165 TMatrix<T> const & mtx = (*this);
166 TVector sv( mtx(rr, cr) , true, GetVectorType(), GetMemoryMapping() );
167 sv.SetTemp(true);
168 return(sv);
169}
170
171//! Return the norm \f$ \sum{V(i)^2} \f$
172template <class T>
173T TVector<T>::Norm2() const
174{
175 T ret = 0;
176 for(uint_8 k=0; k<Size(); k++) ret += (*this)(k)*(*this)(k);
177 return ret;
178}
179
180//! Return info on number of rows, column and type \b T
181template <class T>
182string TVector<T>::InfoString() const
183{
184 string rs = "TVector<";
185 rs += typeid(T).name();
186 char buff[64];
187 sprintf(buff, ">(%ld) (nr=%ld, nc=%ld)", (long)NElts(), (long)NRows(), (long)NCols());
188 rs += buff;
189 return(rs);
190
191}
192
193///////////////////////////////////////////////////////////////
194#ifdef __CXX_PRAGMA_TEMPLATES__
195#pragma define_template TVector<uint_2>
196#pragma define_template TVector<int_4>
197#pragma define_template TVector<int_8>
198#pragma define_template TVector<r_4>
199#pragma define_template TVector<r_8>
200#pragma define_template TVector< complex<r_4> >
201#pragma define_template TVector< complex<r_8> >
202#endif
203
204#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
205template class TVector<uint_2>;
206template class TVector<int_4>;
207template class TVector<int_8>;
208template class TVector<r_4>;
209template class TVector<r_8>;
210template class TVector< complex<r_4> >;
211template class TVector< complex<r_8> >;
212#endif
213
Note: See TracBrowser for help on using the repository browser.