[762] | 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 SOPHYA {
|
---|
| 15 |
|
---|
| 16 | template <class T>
|
---|
| 17 | class TMatrix : public AnyDataObj {
|
---|
| 18 | public:
|
---|
| 19 |
|
---|
| 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 |
|
---|
| 28 | // Temporaire?
|
---|
| 29 | inline bool IsTemp(void) const {return mNDBlock.IsTemp();}
|
---|
| 30 | inline void SetTemp(bool temp=false) const {mNDBlock.SetTemp(temp);}
|
---|
| 31 |
|
---|
| 32 | // Gestion taille/Remplissage
|
---|
| 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 | mNDBlock.ReSize(r*c); mNr = r; mNc = c;}
|
---|
| 39 | inline void Realloc(uint_4 r,uint_4 c,bool force=false)
|
---|
| 40 | {if(r==0||c==0) throw(SzMismatchError("TMatrix::Realloc r ou c==0\n"));
|
---|
| 41 | mNDBlock.Realloc(r*c,force); mNr = r; mNc = c;}
|
---|
| 42 |
|
---|
| 43 | // Informations pointeur/data
|
---|
| 44 | inline uint_4 NRows() const {return mNr;}
|
---|
| 45 | inline uint_4 NCols() const {return mNc;}
|
---|
| 46 | inline uint_4 NCol() const {return mNc;} // back-compat Peida
|
---|
| 47 | inline T const& operator()(uint_4 r,uint_4 c) const
|
---|
| 48 | {return *(mNDBlock.Begin()+r*mNc+c);}
|
---|
| 49 | inline T& operator()(uint_4 r,uint_4 c)
|
---|
| 50 | {return *(mNDBlock.Begin()+r*mNc+c);}
|
---|
| 51 | inline T const& operator[](uint_4 ip) const
|
---|
| 52 | {return *(mNDBlock.Begin()+ip);}
|
---|
| 53 | inline T& operator[](uint_4 ip)
|
---|
| 54 | {return *(mNDBlock.Begin()+ip);}
|
---|
| 55 | inline T* Data() {return mNDBlock.Begin();}
|
---|
| 56 | inline const T* Data() const {return mNDBlock.Begin();}
|
---|
| 57 | inline NDataBlock<T>& DataBlock() {return mNDBlock;}
|
---|
| 58 | inline const NDataBlock<T>& DataBlock() const {return mNDBlock;}
|
---|
| 59 |
|
---|
| 60 | // Operations matricielles
|
---|
| 61 | TMatrix<T> Transpose(void) const;
|
---|
| 62 |
|
---|
| 63 | // Operateur d'affectation
|
---|
| 64 | // A = x (matrice diagonale x*Identite)
|
---|
| 65 | inline TMatrix<T>& operator = (T x)
|
---|
| 66 | {if(mNr!=mNc || mNr==0) throw(SzMismatchError("TMatrix::operator= mNc!=mNr ou ==0\n"));
|
---|
| 67 | for(uint_4 r=0;r<mNr;r++) for(uint_4 c=0;c<mNc;c++) (*this)(r,c)=(r==c)?x:(T)0;
|
---|
| 68 | return *this;}
|
---|
| 69 | // A = B : partage les donnees si "a" est temporaire, clone sinon.
|
---|
| 70 | inline TMatrix<T>& operator = (const TMatrix<T>& a)
|
---|
| 71 | {if(this == &a) return *this; CloneOrShare(a); return *this;}
|
---|
| 72 |
|
---|
| 73 | // Impression
|
---|
| 74 | void Print(ostream& os,int lp=0,uint_4 i0=0,uint_4 ni=10,uint_4 j0=0,uint_4 nj=10) const;
|
---|
| 75 | inline void Print(int lp=0,uint_4 i0=0,uint_4 ni=10,uint_4 j0=0,uint_4 nj=10) const
|
---|
| 76 | {Print(cout,lp,i0,ni,j0,nj);}
|
---|
| 77 |
|
---|
| 78 | // Surcharge d'operateurs INPLACE: A (+=,-=,*=,/=) (T) x
|
---|
| 79 | inline TMatrix<T>& operator += (T b) {mNDBlock += b; return *this;}
|
---|
| 80 | inline TMatrix<T>& operator -= (T b) {mNDBlock -= b; return *this;}
|
---|
| 81 | inline TMatrix<T>& operator *= (T b) {mNDBlock *= b; return *this;}
|
---|
| 82 | inline TMatrix<T>& operator /= (T b) {mNDBlock /= b; return *this;}
|
---|
| 83 |
|
---|
| 84 | // Surcharge d'operateurs INPLACE: A (+=,-=,*=,/=) B
|
---|
| 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;}
|
---|
| 89 | inline TMatrix<T>& operator -= (const TMatrix<T>& a)
|
---|
| 90 | {if(mNr==0 || mNc==0 || mNr!=a.mNr || mNc!=a.mNc)
|
---|
| 91 | throw(SzMismatchError("TMatrix::operator-=A size mismatch"));
|
---|
| 92 | mNDBlock -= a.mNDBlock; return *this;}
|
---|
| 93 | TMatrix<T>& operator *= (const TMatrix<T>& a);
|
---|
| 94 |
|
---|
| 95 | // Pour surcharge d'operateurs C = A (+,-,*) B
|
---|
| 96 | TMatrix<T> Add(const TMatrix<T>& b) const;
|
---|
| 97 | TMatrix<T> Sub(const TMatrix<T>& b) const;
|
---|
| 98 | TMatrix<T> Mul(const TMatrix<T>& b) const;
|
---|
| 99 |
|
---|
| 100 |
|
---|
| 101 | protected:
|
---|
| 102 | // partage les donnees si "a" temporaire, clone sinon.
|
---|
| 103 | inline void CloneOrShare(const TMatrix<T>& a)
|
---|
| 104 | {mNDBlock.CloneOrShare(a.mNDBlock); mNr=a.mNr; mNc=a.mNc;}
|
---|
| 105 | // Share: partage les donnees de "a"
|
---|
| 106 | inline void Share(const TMatrix<T>& a)
|
---|
| 107 | {mNDBlock.Share(a.mNDBlock); mNr=a.mNr; mNc=a.mNc;}
|
---|
| 108 |
|
---|
| 109 | uint_4 mNr,mNc;
|
---|
| 110 | NDataBlock<T> mNDBlock;
|
---|
| 111 | };
|
---|
| 112 |
|
---|
| 113 | ////////////////////////////////////////////////////////////////
|
---|
| 114 | // Impression
|
---|
| 115 |
|
---|
| 116 | template <class T>
|
---|
| 117 | inline ostream& operator << (ostream& os, const TMatrix<T>& a)
|
---|
| 118 | {a.Print(os); return(os);}
|
---|
| 119 |
|
---|
| 120 | ////////////////////////////////////////////////////////////////
|
---|
| 121 | // Surcharge d'operateurs A (+,-,*,/) (T) x
|
---|
| 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); result += b; return result;}
|
---|
| 128 |
|
---|
| 129 | template <class T> inline TMatrix<T> operator - (const TMatrix<T>& a, T b)
|
---|
| 130 | {TMatrix<T> result(a); result.SetTemp(true); result -= b; return result;}
|
---|
| 131 |
|
---|
| 132 | template <class T> inline TMatrix<T> operator - (T b,const TMatrix<T>& a)
|
---|
| 133 | {TMatrix<T> result(a); result.SetTemp(true);
|
---|
| 134 | result.DataBlock() = b-result.DataBlock(); 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 | template <class T> inline TMatrix<T> operator * (T b,const TMatrix<T>& a)
|
---|
| 140 | {TMatrix<T> result(a); result.SetTemp(true); result *= b; return result;}
|
---|
| 141 |
|
---|
| 142 | template <class T> inline TMatrix<T> operator / (const TMatrix<T>& a, T b)
|
---|
| 143 | {TMatrix<T> result(a); result.SetTemp(true); result /= b; return result;}
|
---|
| 144 |
|
---|
| 145 | ////////////////////////////////////////////////////////////////
|
---|
| 146 | // Surcharge d'operateurs C = A (+,-,*,/) B
|
---|
| 147 |
|
---|
| 148 | template <class T>
|
---|
| 149 | inline TMatrix<T> operator + (const TMatrix<T>& a,const TMatrix<T>& b)
|
---|
| 150 | {return a.Add(b);}
|
---|
| 151 |
|
---|
| 152 | template <class T>
|
---|
| 153 | inline TMatrix<T> operator - (const TMatrix<T>& a,const TMatrix<T>& b)
|
---|
| 154 | {return a.Sub(b);}
|
---|
| 155 |
|
---|
| 156 | template <class T>
|
---|
| 157 | inline TMatrix<T> operator * (const TMatrix<T>& a,const TMatrix<T>& b)
|
---|
| 158 | {return a.Mul(b);}
|
---|
| 159 |
|
---|
| 160 | ////////////////////////////////////////////////////////////////
|
---|
| 161 | // Typedef pour simplifier et compatibilite Peida
|
---|
| 162 | typedef TMatrix<r_8> Matrix;
|
---|
| 163 |
|
---|
| 164 | /////////////////////////////////////////////////////////////////////////
|
---|
| 165 | // Classe pour la gestion de persistance
|
---|
| 166 | template <class T>
|
---|
| 167 | class FIO_TMatrix : public PPersist {
|
---|
| 168 | public:
|
---|
| 169 | FIO_TMatrix();
|
---|
| 170 | FIO_TMatrix(string const & filename);
|
---|
| 171 | FIO_TMatrix(const TMatrix<T> & obj);
|
---|
| 172 | FIO_TMatrix(TMatrix<T> * obj);
|
---|
| 173 | virtual ~FIO_TMatrix();
|
---|
| 174 | virtual AnyDataObj* DataObj();
|
---|
| 175 | virtual void SetDataObj(AnyDataObj & o);
|
---|
| 176 | inline operator TMatrix<T>() { return(*dobj); }
|
---|
| 177 | protected :
|
---|
| 178 | virtual void ReadSelf(PInPersist&);
|
---|
| 179 | virtual void WriteSelf(POutPersist&) const;
|
---|
| 180 | TMatrix<T> * dobj;
|
---|
| 181 | bool ownobj;
|
---|
| 182 | };
|
---|
| 183 |
|
---|
| 184 | template <class T>
|
---|
| 185 | inline POutPersist& operator << (POutPersist& os, TMatrix<T> & obj)
|
---|
| 186 | { FIO_TMatrix<T> fio(&obj); fio.Write(os); return(os); }
|
---|
| 187 | template <class T>
|
---|
| 188 | inline PInPersist& operator >> (PInPersist& is, TMatrix<T> & obj)
|
---|
| 189 | { FIO_TMatrix<T> fio(&obj); fio.Read(is); return(is); }
|
---|
| 190 |
|
---|
| 191 |
|
---|
| 192 | } // Fin du namespace
|
---|
| 193 |
|
---|
| 194 | #endif
|
---|