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 <complex>
|
---|
10 | #include "ppersist.h"
|
---|
11 | #include "anydataobj.h"
|
---|
12 | #include "ndatablock.h"
|
---|
13 |
|
---|
14 | namespace PlanckDPC {
|
---|
15 |
|
---|
16 | class GeneralFit;
|
---|
17 | template <class T> class TVector;
|
---|
18 | template <class T> class TMatrixRC;
|
---|
19 |
|
---|
20 | template <class T>
|
---|
21 | class TMatrix : public AnyDataObj {
|
---|
22 | friend class TMatrixRC<T>;
|
---|
23 | friend class TVector<T>;
|
---|
24 | public:
|
---|
25 |
|
---|
26 | // Creation / destruction
|
---|
27 | TMatrix();
|
---|
28 | TMatrix(uint_4 r,uint_4 c);
|
---|
29 | TMatrix(uint_4 r,uint_4 c,T* values,Bridge* br=NULL);
|
---|
30 | TMatrix(const TMatrix<T>& a);
|
---|
31 | TMatrix(const TMatrix<T>& a,bool share);
|
---|
32 | virtual ~TMatrix();
|
---|
33 |
|
---|
34 | // Temporaire?
|
---|
35 | inline bool IsTemp(void) const {return mNDBlock.IsTemp();}
|
---|
36 | inline void SetTemp(bool temp=false) const {mNDBlock.SetTemp(temp);}
|
---|
37 |
|
---|
38 | // Gestion taille/Remplissage
|
---|
39 | inline void Clone(const TMatrix<T>& a) // Clone: copie des donnees de "a"
|
---|
40 | {mNDBlock.Clone(a.mNDBlock); mNr = a.mNr; mNc = a.mNc;}
|
---|
41 | inline void Reset(T v=0) {mNDBlock.Reset(v);}
|
---|
42 | inline void ReSize(uint_4 r,uint_4 c) // Reallocation de place
|
---|
43 | {if(r==0||c==0) throw(SzMismatchError("TMatrix::ReSize r ou c==0\n"));
|
---|
44 | mNr = r; mNc = c; mNDBlock.ReSize(r*c);}
|
---|
45 |
|
---|
46 | // Informations pointeur/data
|
---|
47 | inline uint_4 NRows() const {return mNr;}
|
---|
48 | inline uint_4 NCols() const {return mNc;}
|
---|
49 | inline T const& operator()(uint_4 r,uint_4 c) const
|
---|
50 | {return *(mNDBlock.Begin()+r*mNc+c);}
|
---|
51 | inline T& operator()(uint_4 r,uint_4 c)
|
---|
52 | {return *(mNDBlock.Begin()+r*mNc+c);}
|
---|
53 | inline T const& operator[](uint_4 ip) const
|
---|
54 | {return *(mNDBlock.Begin()+ip);}
|
---|
55 | inline T& operator[](uint_4 ip)
|
---|
56 | {return *(mNDBlock.Begin()+ip);}
|
---|
57 | inline T* Data() {return mNDBlock.Begin();}
|
---|
58 | inline const T* Data() const {return mNDBlock.Begin();}
|
---|
59 | inline NDataBlock<T>& DataBlock() {return mNDBlock;}
|
---|
60 | inline const NDataBlock<T>& DataBlock() const {return mNDBlock;}
|
---|
61 |
|
---|
62 | // Operations matricielles
|
---|
63 | TMatrix<T> Transpose(void) const;
|
---|
64 |
|
---|
65 | // Operateur d'affectation
|
---|
66 | // A = x (matrice diagonale x*Identite)
|
---|
67 | inline TMatrix<T>& operator = (T x)
|
---|
68 | {if(mNr!=mNc || mNr==0) throw(SzMismatchError("TMatrix::operator= mNc!=mNr ou ==0\n"));
|
---|
69 | for(uint_4 r=0;r<mNr;r++) for(uint_4 c=0;c<mNc;c++) (*this)(r,c)=(r==c)?x:0;
|
---|
70 | return *this;}
|
---|
71 | // A = B : partage les donnees si "a" est temporaire, clone sinon.
|
---|
72 | inline TMatrix<T>& operator = (const TMatrix<T>& a)
|
---|
73 | {if(this == &a) return *this; CloneOrShare(a); return *this;}
|
---|
74 |
|
---|
75 | // Impression
|
---|
76 | void Print(ostream& os,int lp=0,uint_4 i0=0,uint_4 ni=10,uint_4 j0=0,uint_4 nj=10) const;
|
---|
77 | inline void Print(int lp=0,uint_4 i0=0,uint_4 ni=10,uint_4 j0=0,uint_4 nj=10) const
|
---|
78 | {Print(cout,lp,i0,ni,j0,nj);}
|
---|
79 |
|
---|
80 | // Surcharge d'operateurs INPLACE: A (+=,-=,*=,/=) (T) x
|
---|
81 | inline TMatrix<T>& operator += (T b) {mNDBlock += b; return *this;}
|
---|
82 | inline TMatrix<T>& operator -= (T b) {mNDBlock -= b; return *this;}
|
---|
83 | inline TMatrix<T>& operator *= (T b) {mNDBlock *= b; return *this;}
|
---|
84 | inline TMatrix<T>& operator /= (T b) {mNDBlock /= b; return *this;}
|
---|
85 |
|
---|
86 | // Surcharge d'operateurs INPLACE: A (+=,-=,*=,/=) B
|
---|
87 | inline TMatrix<T>& operator += (const TMatrix<T>& a)
|
---|
88 | {if(mNr==0 || mNc==0 || mNr!=a.mNr || mNc!=a.mNc)
|
---|
89 | throw(SzMismatchError("TMatrix::operator+=A size mismatch"));
|
---|
90 | mNDBlock += a.mNDBlock; return *this;}
|
---|
91 | inline TMatrix<T>& operator -= (const TMatrix<T>& a)
|
---|
92 | {if(mNr==0 || mNc==0 || mNr!=a.mNr || mNc!=a.mNc)
|
---|
93 | throw(SzMismatchError("TMatrix::operator-=A size mismatch"));
|
---|
94 | mNDBlock -= a.mNDBlock; return *this;}
|
---|
95 | TMatrix<T>& operator *= (const TMatrix<T>& a);
|
---|
96 |
|
---|
97 | // Pour surcharge d'operateurs C = A (+,-,*) B
|
---|
98 | TMatrix<T> Add(const TMatrix<T>& b) const;
|
---|
99 | TMatrix<T> Sub(const TMatrix<T>& b) const;
|
---|
100 | TMatrix<T> Mul(const TMatrix<T>& b) const;
|
---|
101 |
|
---|
102 | // Pivot de Gauss : diagonalise la matrice A, en effectuant les memes
|
---|
103 | // operations sur la matrice B
|
---|
104 | TMatrix<T> Inverse() const;
|
---|
105 | static T GausPiv(TMatrix<T>& A, TMatrix<T>& B);
|
---|
106 |
|
---|
107 | // Residus et fonction fittees.
|
---|
108 | TMatrix<T> FitResidus(GeneralFit& gfit
|
---|
109 | ,double xorg=0.,double yorg=0.,double dx=1.,double dy=1.);
|
---|
110 | TMatrix<T> FitFunction(GeneralFit& gfit
|
---|
111 | ,double xorg=0.,double yorg=0.,double dx=1.,double dy=1.);
|
---|
112 |
|
---|
113 | // Acces aux rangees et colonnes
|
---|
114 | TMatrixRC<T> Row(uint_4 r) const;
|
---|
115 | TMatrixRC<T> Col(uint_4 c) const;
|
---|
116 | TMatrixRC<T> Diag() const;
|
---|
117 |
|
---|
118 | protected:
|
---|
119 | // partage les donnees si "a" temporaire, clone sinon.
|
---|
120 | inline void CloneOrShare(const TMatrix<T>& a)
|
---|
121 | {mNDBlock.CloneOrShare(a.mNDBlock); mNr=a.mNr; mNc=a.mNc;}
|
---|
122 | // Share: partage les donnees de "a"
|
---|
123 | inline void Share(const TMatrix<T>& a)
|
---|
124 | {mNDBlock.Share(a.mNDBlock); mNr=a.mNr; mNc=a.mNc;}
|
---|
125 |
|
---|
126 | uint_4 mNr,mNc;
|
---|
127 | NDataBlock<T> mNDBlock;
|
---|
128 | };
|
---|
129 |
|
---|
130 | ////////////////////////////////////////////////////////////////
|
---|
131 | // Impression
|
---|
132 |
|
---|
133 | template <class T>
|
---|
134 | inline ostream& operator << (ostream& os, const TMatrix<T>& a)
|
---|
135 | {a.Print(os); return(os);}
|
---|
136 |
|
---|
137 | ////////////////////////////////////////////////////////////////
|
---|
138 | // Surcharge d'operateurs A (+=,-=,*=,/=) (T) x
|
---|
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 | template <class T> inline TMatrix<T> operator - (T b,const TMatrix<T>& a)
|
---|
150 | {TMatrix<T> result(a); result.SetTemp(true);
|
---|
151 | result.DataBlock() = b-result.DataBlock(); return result;}
|
---|
152 |
|
---|
153 | template <class T> inline TMatrix<T> operator * (const TMatrix<T>& a, T b)
|
---|
154 | {TMatrix<T> result(a); result.SetTemp(true); result *= b; return result;}
|
---|
155 |
|
---|
156 | template <class T> inline TMatrix<T> operator * (T b,const TMatrix<T>& a)
|
---|
157 | {TMatrix<T> result(a); result.SetTemp(true); result *= b; return result;}
|
---|
158 |
|
---|
159 | template <class T> inline TMatrix<T> operator / (const TMatrix<T>& a, T b)
|
---|
160 | {TMatrix<T> result(a); result.SetTemp(true); result /= b; return result;}
|
---|
161 |
|
---|
162 | ////////////////////////////////////////////////////////////////
|
---|
163 | // Surcharge d'operateurs C = A (+,-,*,/) B
|
---|
164 |
|
---|
165 | template <class T>
|
---|
166 | inline TMatrix<T> operator + (const TMatrix<T>& a,const TMatrix<T>& b)
|
---|
167 | {return a.Add(b);}
|
---|
168 |
|
---|
169 | template <class T>
|
---|
170 | inline TMatrix<T> operator - (const TMatrix<T>& a,const TMatrix<T>& b)
|
---|
171 | {return a.Sub(b);}
|
---|
172 |
|
---|
173 | template <class T>
|
---|
174 | inline TMatrix<T> operator * (const TMatrix<T>& a,const TMatrix<T>& b)
|
---|
175 | {return a.Mul(b);}
|
---|
176 |
|
---|
177 |
|
---|
178 | /////////////////////////////////////////////////////////////////////////
|
---|
179 | // Classe pour la gestion de persistance
|
---|
180 | template <class T>
|
---|
181 | class FIO_TMatrix : public PPersist {
|
---|
182 | public:
|
---|
183 | FIO_TMatrix();
|
---|
184 | FIO_TMatrix(string const & filename);
|
---|
185 | FIO_TMatrix(const TMatrix<T> & obj);
|
---|
186 | FIO_TMatrix(TMatrix<T> * obj);
|
---|
187 | virtual ~FIO_TMatrix();
|
---|
188 | virtual AnyDataObj* DataObj();
|
---|
189 | inline operator TMatrix<T>() { return(*dobj); }
|
---|
190 | protected :
|
---|
191 | virtual void ReadSelf(PInPersist&);
|
---|
192 | virtual void WriteSelf(POutPersist&) const;
|
---|
193 | TMatrix<T> * dobj;
|
---|
194 | bool ownobj;
|
---|
195 | };
|
---|
196 |
|
---|
197 | template <class T>
|
---|
198 | inline POutPersist& operator << (POutPersist& os, TMatrix<T> & obj)
|
---|
199 | { FIO_TMatrix<T> fio(&obj); fio.Write(os); return(os); }
|
---|
200 | template <class T>
|
---|
201 | inline PInPersist& operator >> (PInPersist& is, TMatrix<T> & obj)
|
---|
202 | { FIO_TMatrix<T> fio(&obj); fio.Read(is); return(is); }
|
---|
203 |
|
---|
204 | /////////////////////////////////////////////////////////////////////////
|
---|
205 | // Classe de lignes/colonnes de matrices
|
---|
206 | enum TRCKind {TmatrixRow=0, TmatrixCol=1, TmatrixDiag=2};
|
---|
207 | template <class T>
|
---|
208 | class TMatrixRC {
|
---|
209 | friend class TVector<T>;
|
---|
210 | friend class TMatrix<T>;
|
---|
211 | public:
|
---|
212 | TMatrixRC();
|
---|
213 |
|
---|
214 | virtual ~TMatrixRC() {}
|
---|
215 |
|
---|
216 | int_4 Next();
|
---|
217 | int_4 Prev();
|
---|
218 | int_4 SetCol(int_4 c);
|
---|
219 | int_4 SetRow(int_4 r);
|
---|
220 | int_4 SetDiag();
|
---|
221 |
|
---|
222 | static uint_4 Step(const TMatrix<T>& m, TRCKind rckind);
|
---|
223 | static T* Org(const TMatrix<T>&, TRCKind rckind, uint_4 ind=0);
|
---|
224 |
|
---|
225 | uint_4 NElts() const;
|
---|
226 | T& operator()(uint_4 i);
|
---|
227 | T operator()(uint_4 i) const;
|
---|
228 |
|
---|
229 | TMatrixRC<T>& operator = (const TMatrixRC<T>& rc);
|
---|
230 | TVector<T> GetVect() const;
|
---|
231 |
|
---|
232 | TMatrixRC<T>& operator += (const TMatrixRC<T>& rc);
|
---|
233 | TMatrixRC<T>& operator -= (const TMatrixRC<T>& rc);
|
---|
234 |
|
---|
235 | TMatrixRC<T>& operator *= (T x);
|
---|
236 | TMatrixRC<T>& operator /= (T x);
|
---|
237 | TMatrixRC<T>& operator -= (T x);
|
---|
238 | TMatrixRC<T>& operator += (T x);
|
---|
239 |
|
---|
240 | TMatrixRC<T>& LinComb(T a, T b, const TMatrixRC& rc, uint_4 first=0);
|
---|
241 | TMatrixRC<T>& LinComb(T b, const TMatrixRC<T>& rc, uint_4 first=0);
|
---|
242 |
|
---|
243 | uint_4 IMaxAbs(uint_4 first=0);
|
---|
244 |
|
---|
245 | static void Swap(TMatrixRC<T>& rc1, TMatrixRC<T>& rc2);
|
---|
246 |
|
---|
247 | protected:
|
---|
248 | TMatrixRC(TMatrix<T>& m, TRCKind kind, uint_4 index=0);
|
---|
249 | TMatrix<T>* matrix;
|
---|
250 | inline static double Abs_Value(uint_1 v) {return (double) v;}
|
---|
251 | inline static double Abs_Value(uint_2 v) {return (double) v;}
|
---|
252 | inline static double Abs_Value(int_2 v) {return (v>0)? (double) v: (double) -v;}
|
---|
253 | inline static double Abs_Value(int_4 v) {return (v>0)? (double) v: (double) -v;}
|
---|
254 | inline static double Abs_Value(int_8 v) {return (v>0)? (double) v: (double) -v;}
|
---|
255 | inline static double Abs_Value(uint_4 v) {return (double) v;}
|
---|
256 | inline static double Abs_Value(uint_8 v) {return (double) v;}
|
---|
257 | inline static double Abs_Value(r_4 v) {return (double) fabsf(v);}
|
---|
258 | inline static double Abs_Value(r_8 v) {return fabs(v);}
|
---|
259 | inline static double Abs_Value(complex<float> v)
|
---|
260 | {return sqrt(v.real()*v.real()+v.imag()*v.imag());}
|
---|
261 | inline static double Abs_Value(complex<double> v)
|
---|
262 | {return sqrt(v.real()*v.real()+v.imag()*v.imag());}
|
---|
263 |
|
---|
264 | T* data;
|
---|
265 | int_4 index;
|
---|
266 | uint_4 step;
|
---|
267 | TRCKind kind;
|
---|
268 | };
|
---|
269 |
|
---|
270 |
|
---|
271 | template <class T> inline T operator * (const TMatrixRC<T>& a, const TMatrixRC<T>& b)
|
---|
272 | {
|
---|
273 | if ( a.NElts() != b.NElts() )
|
---|
274 | throw(SzMismatchError("TMatrixRC::operator * size mismatch\n"));
|
---|
275 | if ( a.kind != b.kind )
|
---|
276 | throw(SzMismatchError("TMatrixRC::operator * type mismatch\n"));
|
---|
277 | T sum = 0;
|
---|
278 | for(uint_4 i=0; i<a.NElts(); i++) sum += a(i)*b(i);
|
---|
279 | return sum;
|
---|
280 | }
|
---|
281 |
|
---|
282 | template <class T>
|
---|
283 | inline uint_4 TMatrixRC<T>::Step(const TMatrix<T>& m, TRCKind rckind)
|
---|
284 | { switch (rckind) { case TmatrixRow : return 1;
|
---|
285 | case TmatrixCol : return m.mNc;
|
---|
286 | case TmatrixDiag : return m.mNc+1; }
|
---|
287 | return 0; }
|
---|
288 |
|
---|
289 | template <class T>
|
---|
290 | inline T* TMatrixRC<T>::Org(const TMatrix<T>& m, TRCKind rckind, uint_4 index)
|
---|
291 | { switch (rckind) { case TmatrixRow : return const_cast<T *>(m.Data()) + index * m.mNc;
|
---|
292 | case TmatrixCol : return const_cast<T *>(m.Data()) + index;
|
---|
293 | case TmatrixDiag : return const_cast<T *>(m.Data()); }
|
---|
294 | return NULL; }
|
---|
295 |
|
---|
296 | template <class T> inline uint_4 TMatrixRC<T>::NElts() const
|
---|
297 | { if (!matrix) return 0;
|
---|
298 | switch (kind) { case TmatrixRow : return matrix->mNc;
|
---|
299 | case TmatrixCol : return matrix->mNr;
|
---|
300 | case TmatrixDiag : return matrix->mNc; }
|
---|
301 | return 0; }
|
---|
302 |
|
---|
303 | template <class T>
|
---|
304 | inline T& TMatrixRC<T>::operator()(uint_4 i) {return data[i*step];}
|
---|
305 | template <class T>
|
---|
306 | inline T TMatrixRC<T>::operator()(uint_4 i) const {return data[i*step];}
|
---|
307 |
|
---|
308 | } // Fin du namespace
|
---|
309 |
|
---|
310 | #endif
|
---|