| 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:
|
|---|
| 18 |
|
|---|
| 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 |
|
|---|
| 27 | // Temporaire?
|
|---|
| 28 | inline bool IsTemp(void) const {return mNDBlock.IsTemp();}
|
|---|
| 29 | inline void SetTemp(bool temp=false) const {mNDBlock.SetTemp(temp);}
|
|---|
| 30 |
|
|---|
| 31 | // Gestion taille/Remplissage
|
|---|
| 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);}
|
|---|
| 38 |
|
|---|
| 39 | // Informations pointeur/data
|
|---|
| 40 | inline int NRows() const {return mNr;}
|
|---|
| 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);}
|
|---|
| 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;}
|
|---|
| 54 |
|
|---|
| 55 | // Operations matricielles
|
|---|
| 56 | TMatrix<T> Transpose(void) const;
|
|---|
| 57 |
|
|---|
| 58 | // Operateur d'affectation
|
|---|
| 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;}
|
|---|
| 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 |
|
|---|
| 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;}
|
|---|
| 78 |
|
|---|
| 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;}
|
|---|
| 88 | TMatrix<T>& operator *= (const TMatrix<T>& a);
|
|---|
| 89 |
|
|---|
| 90 | // Pour surcharge d'operateurs C = A (+,-,*) B
|
|---|
| 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 |
|
|---|
| 95 | protected:
|
|---|
| 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 |
|
|---|
| 103 | uint_4 mNr,mNc;
|
|---|
| 104 | NDataBlock<T> mNDBlock;
|
|---|
| 105 | };
|
|---|
| 106 |
|
|---|
| 107 | ////////////////////////////////////////////////////////////////
|
|---|
| 108 | // Impression
|
|---|
| 109 |
|
|---|
| 110 | template <class T>
|
|---|
| 111 | inline ostream& operator << (ostream& os, const TMatrix<T>& a)
|
|---|
| 112 | {a.Print(os); return(os);}
|
|---|
| 113 |
|
|---|
| 114 | ////////////////////////////////////////////////////////////////
|
|---|
| 115 | // Surcharge d'operateurs A (+=,-=,*=,/=) (T) x
|
|---|
| 116 |
|
|---|
| 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;}
|
|---|
| 119 |
|
|---|
| 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;}
|
|---|
| 122 |
|
|---|
| 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;}
|
|---|
| 125 |
|
|---|
| 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;}
|
|---|
| 129 |
|
|---|
| 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;}
|
|---|
| 132 |
|
|---|
| 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;}
|
|---|
| 135 |
|
|---|
| 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>
|
|---|
| 143 | inline TMatrix<T> operator + (const TMatrix<T>& a,const TMatrix<T>& b)
|
|---|
| 144 | {return a.Add(b);}
|
|---|
| 145 |
|
|---|
| 146 | template <class T>
|
|---|
| 147 | inline TMatrix<T> operator - (const TMatrix<T>& a,const TMatrix<T>& b)
|
|---|
| 148 | {return a.Sub(b);}
|
|---|
| 149 |
|
|---|
| 150 | template <class T>
|
|---|
| 151 | inline TMatrix<T> operator * (const TMatrix<T>& a,const TMatrix<T>& b)
|
|---|
| 152 | {return a.Mul(b);}
|
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 | /////////////////////////////////////////////////////////////////////////
|
|---|
| 156 | // Classe pour la gestion de persistance
|
|---|
| 157 | template <class T>
|
|---|
| 158 | class FIO_TMatrix : public PPersist {
|
|---|
| 159 | public:
|
|---|
| 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();
|
|---|
| 166 | inline operator TMatrix<T>() { return(*dobj); }
|
|---|
| 167 | protected :
|
|---|
| 168 | virtual void ReadSelf(PInPersist&);
|
|---|
| 169 | virtual void WriteSelf(POutPersist&) const;
|
|---|
| 170 | TMatrix<T> * dobj;
|
|---|
| 171 | bool ownobj;
|
|---|
| 172 | };
|
|---|
| 173 |
|
|---|
| 174 | } // Fin du namespace
|
|---|
| 175 |
|
|---|
| 176 | #endif
|
|---|