[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>
|
---|
| 9 | #include "ppersist.h"
|
---|
| 10 | #include "anydataobj.h"
|
---|
| 11 | #include "ndatablock.h"
|
---|
[299] | 12 | class GeneralFit;
|
---|
[279] | 13 |
|
---|
| 14 | namespace PlanckDPC {
|
---|
| 15 |
|
---|
| 16 | template <class T>
|
---|
| 17 | class TMatrix : public AnyDataObj {
|
---|
| 18 | public:
|
---|
[288] | 19 |
|
---|
[279] | 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 |
|
---|
[286] | 28 | // Temporaire?
|
---|
| 29 | inline bool IsTemp(void) const {return mNDBlock.IsTemp();}
|
---|
[288] | 30 | inline void SetTemp(bool temp=false) const {mNDBlock.SetTemp(temp);}
|
---|
[286] | 31 |
|
---|
[279] | 32 | // Gestion taille/Remplissage
|
---|
[288] | 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);}
|
---|
[279] | 39 |
|
---|
| 40 | // Informations pointeur/data
|
---|
| 41 | inline int NRows() const {return mNr;}
|
---|
[288] | 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);}
|
---|
[286] | 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;}
|
---|
[279] | 55 |
|
---|
[288] | 56 | // Operations matricielles
|
---|
| 57 | TMatrix<T> Transpose(void) const;
|
---|
| 58 |
|
---|
[279] | 59 | // Operateur d'affectation
|
---|
[288] | 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;}
|
---|
[279] | 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 |
|
---|
[288] | 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;}
|
---|
[279] | 79 |
|
---|
[288] | 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;}
|
---|
[279] | 89 | TMatrix<T>& operator *= (const TMatrix<T>& a);
|
---|
| 90 |
|
---|
[288] | 91 | // Pour surcharge d'operateurs C = A (+,-,*) B
|
---|
[286] | 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 |
|
---|
[299] | 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 |
|
---|
[279] | 105 | protected:
|
---|
[288] | 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 |
|
---|
[279] | 113 | uint_4 mNr,mNc;
|
---|
[286] | 114 | NDataBlock<T> mNDBlock;
|
---|
[279] | 115 | };
|
---|
| 116 |
|
---|
[288] | 117 | ////////////////////////////////////////////////////////////////
|
---|
| 118 | // Impression
|
---|
| 119 |
|
---|
| 120 | template <class T>
|
---|
[279] | 121 | inline ostream& operator << (ostream& os, const TMatrix<T>& a)
|
---|
[288] | 122 | {a.Print(os); return(os);}
|
---|
[279] | 123 |
|
---|
[288] | 124 | ////////////////////////////////////////////////////////////////
|
---|
| 125 | // Surcharge d'operateurs A (+=,-=,*=,/=) (T) x
|
---|
[286] | 126 |
|
---|
[288] | 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;}
|
---|
[286] | 129 |
|
---|
[288] | 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;}
|
---|
[286] | 132 |
|
---|
[288] | 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;}
|
---|
[286] | 135 |
|
---|
[288] | 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;}
|
---|
[286] | 139 |
|
---|
[288] | 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;}
|
---|
[286] | 142 |
|
---|
[288] | 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;}
|
---|
[286] | 145 |
|
---|
[288] | 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>
|
---|
[286] | 153 | inline TMatrix<T> operator + (const TMatrix<T>& a,const TMatrix<T>& b)
|
---|
[288] | 154 | {return a.Add(b);}
|
---|
[286] | 155 |
|
---|
[288] | 156 | template <class T>
|
---|
[286] | 157 | inline TMatrix<T> operator - (const TMatrix<T>& a,const TMatrix<T>& b)
|
---|
[288] | 158 | {return a.Sub(b);}
|
---|
[286] | 159 |
|
---|
[288] | 160 | template <class T>
|
---|
[286] | 161 | inline TMatrix<T> operator * (const TMatrix<T>& a,const TMatrix<T>& b)
|
---|
[288] | 162 | {return a.Mul(b);}
|
---|
[286] | 163 |
|
---|
[288] | 164 |
|
---|
[286] | 165 | /////////////////////////////////////////////////////////////////////////
|
---|
| 166 | // Classe pour la gestion de persistance
|
---|
| 167 | template <class T>
|
---|
| 168 | class FIO_TMatrix : public PPersist {
|
---|
| 169 | public:
|
---|
[288] | 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();
|
---|
[286] | 176 | inline operator TMatrix<T>() { return(*dobj); }
|
---|
| 177 | protected :
|
---|
[288] | 178 | virtual void ReadSelf(PInPersist&);
|
---|
| 179 | virtual void WriteSelf(POutPersist&) const;
|
---|
[286] | 180 | TMatrix<T> * dobj;
|
---|
| 181 | bool ownobj;
|
---|
| 182 | };
|
---|
| 183 |
|
---|
[279] | 184 | } // Fin du namespace
|
---|
| 185 |
|
---|
| 186 | #endif
|
---|