source: Sophya/trunk/SophyaLib/NTools/tvector.h@ 502

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

intro du Realloc cmv 23/10/99

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