source: Sophya/trunk/SophyaLib/TArray/tvector.h@ 762

Last change on this file since 762 was 762, checked in by ansari, 26 years ago

Reorganisation - Creation du module TArray - Reza 2/3/2000

File size: 3.4 KB
RevLine 
[762]1// This may look like C code, but it is really -*- C++ -*-
2// C.Magneville 05/99
3#ifndef TVector_SEEN
4#define TVector_SEEN
5
6#include "tmatrix.h"
7
8namespace SOPHYA {
9
10template <class T>
11class TVector : public TMatrix<T> {
12public:
13
14 // Creation / destruction
15 TVector(uint_4 n=1);
16 TVector(uint_4 n, T* values,Bridge* br=NULL);
17 TVector(const TVector<T>& v);
18 TVector(const TVector<T>& v,bool share);
19 TVector(const TMatrix<T>& a);
20
21 // Gestion taille/Remplissage
22 inline void ReSize(uint_4 n) {TMatrix<T>::ReSize(n,1);} // Reallocation de place
23 inline void Realloc(uint_4 n,bool force=false) {TMatrix<T>::Realloc(n,1,force);}
24
25 // Informations pointeur/data
26 inline uint_4 NElts() const {return NRows();}
27
28 // Acces aux elements
29 inline T& operator()(uint_4 n) {return (*this)[n];}
30 inline T const& operator()(uint_4 n) const {return (*this)[n];}
31 inline T& Element(uint_4 n) {return (*this)[n];}
32 inline T const& Element(uint_4 n) const {return (*this)[n];}
33
34 // Operateur d'affectation
35 inline TVector& operator = (const TVector& v)
36 {TMatrix<T>::operator =(v); return *this;}
37 inline TVector& operator = (T x)
38 {for(uint_4 i=0;i<NRows();i++) (*this)(i)=x; return *this;}
39
40
41};
42
43// produit scalaire, matrice*vecteur
44template <class T>
45inline T operator* (const TVector<T>& v1, const TVector<T>& v2)
46 {if(v1.NRows() != v2.NRows())
47 throw(SzMismatchError("TVector::operator*(TVector& v1,TVector v2) size mismatch"));
48 T *p = const_cast<T *>(v1.Data()), *pEnd = p+v1.NElts(),
49 *q = const_cast<T *>(v2.Data()), r = 0;
50 while (p<pEnd) r += *p++ * *q++;
51 return r;}
52
53template <class T>
54inline TVector<T> operator* (const TMatrix<T>& a, const TVector<T>& b)
55 {return TVector<T>(a * ((TMatrix<T> const&)(b)));}
56
57// Resolution du systeme A*C = B
58inline r_8 LinSolveInPlace(TMatrix<r_8>& a, TVector<r_8>& b)
59{
60if(a.NCols() != b.NRows() || a.NCols() != a.NRows())
61 throw(SzMismatchError("LinSolveInPlace(TMatrix<r_8>,TVector<r_8>) size mismatch"));
62return TMatrix<r_8>::GausPiv(a,b);
63}
64
65// Resolution du systeme A*C = B, avec C retourne dans B
66inline r_8 LinSolve(const TMatrix<r_8>& a, const TVector<r_8>& b, TVector<r_8>& c)
67{
68if(a.NCols() != b.NRows() || a.NCols() != a.NRows())
69 throw(SzMismatchError("LinSolve(TMatrix<r_8>,TVector<r_8>) size mismatch"));
70c = b;
71TMatrix<r_8> a1(a);
72return TMatrix<r_8>::GausPiv(a1,c);
73}
74
75// Typedef pour simplifier et compatibilite Peida
76typedef TVector<r_8> Vector;
77
78/////////////////////////////////////////////////////////////////////////
79// Classe pour la gestion de persistance
80template <class T>
81class FIO_TVector : public PPersist {
82public:
83 FIO_TVector();
84 FIO_TVector(string const & filename);
85 FIO_TVector(const TVector<T> & obj);
86 FIO_TVector(TVector<T> * obj);
87 virtual ~FIO_TVector();
88 virtual AnyDataObj* DataObj();
89 virtual void SetDataObj(AnyDataObj & o);
90 inline operator TVector<T>() { return(*dobj); }
91protected :
92 virtual void ReadSelf(PInPersist&);
93 virtual void WriteSelf(POutPersist&) const;
94 TVector<T> * dobj;
95 bool ownobj;
96};
97
98template <class T>
99inline POutPersist& operator << (POutPersist& os, TVector<T> & obj)
100{ FIO_TVector<T> fio(&obj); fio.Write(os); return(os); }
101template <class T>
102inline PInPersist& operator >> (PInPersist& is, TVector<T> & obj)
103{ FIO_TVector<T> fio(&obj); fio.Read(is); return(is); }
104
105} // Fin du namespace
106
107#endif
Note: See TracBrowser for help on using the repository browser.