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