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

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

documentation cmv 13/4/00

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