1 | // This may look like C code, but it is really -*- C++ -*-
|
---|
2 | // C.Magneville 04/99
|
---|
3 | #ifndef TMATRIX_SEEN
|
---|
4 | #define TMATRIX_SEEN
|
---|
5 |
|
---|
6 | #include "machdefs.h"
|
---|
7 | #include <stdio.h>
|
---|
8 | #include <iostream.h>
|
---|
9 | #include "ppersist.h"
|
---|
10 | #include "anydataobj.h"
|
---|
11 | #include "ndatablock.h"
|
---|
12 | class GeneralFit;
|
---|
13 |
|
---|
14 | namespace PlanckDPC {
|
---|
15 |
|
---|
16 | template <class T>
|
---|
17 | class TMatrix : public AnyDataObj {
|
---|
18 | public:
|
---|
19 |
|
---|
20 | // Creation / destruction
|
---|
21 | TMatrix();
|
---|
22 | TMatrix(uint_4 r,uint_4 c);
|
---|
23 | TMatrix(uint_4 r,uint_4 c,T* values,Bridge* br=NULL);
|
---|
24 | TMatrix(const TMatrix<T>& a);
|
---|
25 | TMatrix(const TMatrix<T>& a,bool share);
|
---|
26 | virtual ~TMatrix();
|
---|
27 |
|
---|
28 | // Temporaire?
|
---|
29 | inline bool IsTemp(void) const {return mNDBlock.IsTemp();}
|
---|
30 | inline void SetTemp(bool temp=false) const {mNDBlock.SetTemp(temp);}
|
---|
31 |
|
---|
32 | // Gestion taille/Remplissage
|
---|
33 | inline void Clone(const TMatrix<T>& a) // Clone: copie des donnees de "a"
|
---|
34 | {mNDBlock.Clone(a.mNDBlock); mNr = a.mNr; mNc = a.mNc;}
|
---|
35 | inline void Reset(T v=0) {mNDBlock.Reset(v);}
|
---|
36 | inline void ReSize(uint_4 r,uint_4 c) // Reallocation de place
|
---|
37 | {if(r==0||c==0) throw(SzMismatchError("TMatrix::ReSize r ou c==0\n"));
|
---|
38 | mNr = r; mNc = c; mNDBlock.ReSize(r*c);}
|
---|
39 |
|
---|
40 | // Informations pointeur/data
|
---|
41 | inline int NRows() const {return mNr;}
|
---|
42 | inline int NCols() const {return mNc;}
|
---|
43 | inline T const& operator()(uint_4 r,uint_4 c) const
|
---|
44 | {return *(mNDBlock.Begin()+r*mNc+c);}
|
---|
45 | inline T& operator()(uint_4 r,uint_4 c)
|
---|
46 | {return *(mNDBlock.Begin()+r*mNc+c);}
|
---|
47 | inline T const& operator[](uint_4 ip) const
|
---|
48 | {return *(mNDBlock.Begin()+ip);}
|
---|
49 | inline T& operator[](uint_4 ip)
|
---|
50 | {return *(mNDBlock.Begin()+ip);}
|
---|
51 | inline T* Data() {return mNDBlock.Begin();}
|
---|
52 | inline const T* Data() const {return mNDBlock.Begin();}
|
---|
53 | inline NDataBlock<T>& DataBlock() {return mNDBlock;}
|
---|
54 | inline const NDataBlock<T>& DataBlock() const {return mNDBlock;}
|
---|
55 |
|
---|
56 | // Operations matricielles
|
---|
57 | TMatrix<T> Transpose(void) const;
|
---|
58 |
|
---|
59 | // Operateur d'affectation
|
---|
60 | // A = x (matrice diagonale x*Identite)
|
---|
61 | inline TMatrix<T>& operator = (T x)
|
---|
62 | {if(mNr!=mNc || mNr==0) throw(SzMismatchError("TMatrix::operator= mNc!=mNr ou ==0\n"));
|
---|
63 | for(uint_4 r=0;r<mNr;r++) for(uint_4 c=0;c<mNc;c++) (*this)(r,c)=(r==c)?x:0;
|
---|
64 | return *this;}
|
---|
65 | // A = B : partage les donnees si "a" est temporaire, clone sinon.
|
---|
66 | inline TMatrix<T>& operator = (const TMatrix<T>& a)
|
---|
67 | {if(this == &a) return *this; CloneOrShare(a); return *this;}
|
---|
68 |
|
---|
69 | // Impression
|
---|
70 | void Print(ostream& os,int lp=0,uint_4 i0=0,uint_4 ni=10,uint_4 j0=0,uint_4 nj=10) const;
|
---|
71 | inline void Print(int lp=0,uint_4 i0=0,uint_4 ni=10,uint_4 j0=0,uint_4 nj=10) const
|
---|
72 | {Print(cout,lp,i0,ni,j0,nj);}
|
---|
73 |
|
---|
74 | // Surcharge d'operateurs INPLACE: A (+=,-=,*=,/=) (T) x
|
---|
75 | inline TMatrix<T>& operator += (T b) {mNDBlock += b; return *this;}
|
---|
76 | inline TMatrix<T>& operator -= (T b) {mNDBlock -= b; return *this;}
|
---|
77 | inline TMatrix<T>& operator *= (T b) {mNDBlock *= b; return *this;}
|
---|
78 | inline TMatrix<T>& operator /= (T b) {mNDBlock /= b; return *this;}
|
---|
79 |
|
---|
80 | // Surcharge d'operateurs INPLACE: A (+=,-=,*=,/=) B
|
---|
81 | inline TMatrix<T>& operator += (const TMatrix<T>& a)
|
---|
82 | {if(mNr==0 || mNc==0 || mNr!=a.mNr || mNc!=a.mNc)
|
---|
83 | throw(SzMismatchError("TMatrix::operator+=A size mismatch"));
|
---|
84 | mNDBlock += a.mNDBlock; return *this;}
|
---|
85 | inline TMatrix<T>& operator -= (const TMatrix<T>& a)
|
---|
86 | {if(mNr==0 || mNc==0 || mNr!=a.mNr || mNc!=a.mNc)
|
---|
87 | throw(SzMismatchError("TMatrix::operator-=A size mismatch"));
|
---|
88 | mNDBlock -= a.mNDBlock; return *this;}
|
---|
89 | TMatrix<T>& operator *= (const TMatrix<T>& a);
|
---|
90 |
|
---|
91 | // Pour surcharge d'operateurs C = A (+,-,*) B
|
---|
92 | TMatrix<T> Add(const TMatrix<T>& b) const;
|
---|
93 | TMatrix<T> Sub(const TMatrix<T>& b) const;
|
---|
94 | TMatrix<T> Mul(const TMatrix<T>& b) const;
|
---|
95 |
|
---|
96 | // Pivot de Gauss : diagonalise la matrice A, en effectuant les memes
|
---|
97 | // operations sur la matrice B
|
---|
98 | TMatrix<T> Inverse() const;
|
---|
99 | static T GausPiv(TMatrix<T>& A, TMatrix<T>& B);
|
---|
100 |
|
---|
101 | // Residus et fonction fittees.
|
---|
102 | TMatrix<T> FitResidus(GeneralFit& gfit);
|
---|
103 | TMatrix<T> FitFunction(GeneralFit& gfit);
|
---|
104 |
|
---|
105 | protected:
|
---|
106 | // partage les donnees si "a" temporaire, clone sinon.
|
---|
107 | inline void CloneOrShare(const TMatrix<T>& a)
|
---|
108 | {mNDBlock.CloneOrShare(a.mNDBlock); mNr=a.mNr; mNc=a.mNc;}
|
---|
109 | // Share: partage les donnees de "a"
|
---|
110 | inline void Share(const TMatrix<T>& a)
|
---|
111 | {mNDBlock.Share(a.mNDBlock); mNr=a.mNr; mNc=a.mNc;}
|
---|
112 |
|
---|
113 | uint_4 mNr,mNc;
|
---|
114 | NDataBlock<T> mNDBlock;
|
---|
115 | };
|
---|
116 |
|
---|
117 | ////////////////////////////////////////////////////////////////
|
---|
118 | // Impression
|
---|
119 |
|
---|
120 | template <class T>
|
---|
121 | inline ostream& operator << (ostream& os, const TMatrix<T>& a)
|
---|
122 | {a.Print(os); return(os);}
|
---|
123 |
|
---|
124 | ////////////////////////////////////////////////////////////////
|
---|
125 | // Surcharge d'operateurs A (+=,-=,*=,/=) (T) x
|
---|
126 |
|
---|
127 | template <class T> inline TMatrix<T> operator + (const TMatrix<T>& a, T b)
|
---|
128 | {TMatrix<T> result(a); result.SetTemp(true); result += b; return result;}
|
---|
129 |
|
---|
130 | template <class T> inline TMatrix<T> operator + (T b,const TMatrix<T>& a)
|
---|
131 | {TMatrix<T> result(a); result.SetTemp(true); result += b; return result;}
|
---|
132 |
|
---|
133 | template <class T> inline TMatrix<T> operator - (const TMatrix<T>& a, T b)
|
---|
134 | {TMatrix<T> result(a); result.SetTemp(true); result -= b; return result;}
|
---|
135 |
|
---|
136 | template <class T> inline TMatrix<T> operator - (T b,const TMatrix<T>& a)
|
---|
137 | {TMatrix<T> result(a); result.SetTemp(true);
|
---|
138 | result.DataBlock() = b-result.DataBlock(); return result;}
|
---|
139 |
|
---|
140 | template <class T> inline TMatrix<T> operator * (const TMatrix<T>& a, T b)
|
---|
141 | {TMatrix<T> result(a); result.SetTemp(true); result *= b; return result;}
|
---|
142 |
|
---|
143 | template <class T> inline TMatrix<T> operator * (T b,const TMatrix<T>& a)
|
---|
144 | {TMatrix<T> result(a); result.SetTemp(true); result *= b; return result;}
|
---|
145 |
|
---|
146 | template <class T> inline TMatrix<T> operator / (const TMatrix<T>& a, T b)
|
---|
147 | {TMatrix<T> result(a); result.SetTemp(true); result /= b; return result;}
|
---|
148 |
|
---|
149 | ////////////////////////////////////////////////////////////////
|
---|
150 | // Surcharge d'operateurs C = A (+,-,*,/) B
|
---|
151 |
|
---|
152 | template <class T>
|
---|
153 | inline TMatrix<T> operator + (const TMatrix<T>& a,const TMatrix<T>& b)
|
---|
154 | {return a.Add(b);}
|
---|
155 |
|
---|
156 | template <class T>
|
---|
157 | inline TMatrix<T> operator - (const TMatrix<T>& a,const TMatrix<T>& b)
|
---|
158 | {return a.Sub(b);}
|
---|
159 |
|
---|
160 | template <class T>
|
---|
161 | inline TMatrix<T> operator * (const TMatrix<T>& a,const TMatrix<T>& b)
|
---|
162 | {return a.Mul(b);}
|
---|
163 |
|
---|
164 |
|
---|
165 | /////////////////////////////////////////////////////////////////////////
|
---|
166 | // Classe pour la gestion de persistance
|
---|
167 | template <class T>
|
---|
168 | class FIO_TMatrix : public PPersist {
|
---|
169 | public:
|
---|
170 | FIO_TMatrix();
|
---|
171 | FIO_TMatrix(string const & filename);
|
---|
172 | FIO_TMatrix(const TMatrix<T> & obj);
|
---|
173 | FIO_TMatrix(TMatrix<T> * obj);
|
---|
174 | virtual ~FIO_TMatrix();
|
---|
175 | virtual AnyDataObj* DataObj();
|
---|
176 | inline operator TMatrix<T>() { return(*dobj); }
|
---|
177 | protected :
|
---|
178 | virtual void ReadSelf(PInPersist&);
|
---|
179 | virtual void WriteSelf(POutPersist&) const;
|
---|
180 | TMatrix<T> * dobj;
|
---|
181 | bool ownobj;
|
---|
182 | };
|
---|
183 |
|
---|
184 | } // Fin du namespace
|
---|
185 |
|
---|
186 | #endif
|
---|