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