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