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

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

modifs doc cmv 27/4/00

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