Changeset 288 in Sophya for trunk/SophyaLib/NTools/tmatrix.h
- Timestamp:
- May 3, 1999, 6:55:25 PM (26 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaLib/NTools/tmatrix.h
r286 r288 16 16 class TMatrix : public AnyDataObj { 17 17 public: 18 18 19 // Creation / destruction 19 20 TMatrix(); … … 26 27 // Temporaire? 27 28 inline bool IsTemp(void) const {return mNDBlock.IsTemp();} 28 inline void SetTemp(bool temp=false) const 29 {mNDBlock.SetTemp(temp);} 29 inline void SetTemp(bool temp=false) const {mNDBlock.SetTemp(temp);} 30 30 31 31 // Gestion taille/Remplissage 32 void Clone(const TMatrix<T>& a); 33 void Reset(T v=0); 34 void Realloc(uint_4 r,uint_4 c,bool force_alloc=true); 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);} 35 38 36 39 // Informations pointeur/data 37 40 inline int NRows() const {return mNr;} 38 inline int NCol() const {return mNc;} 39 T const& operator()(uint_4 r,uint_4 c) const 40 {return *(mNDBlock.Begin()+r*mNc+c);} 41 T& operator()(uint_4 r,uint_4 c) 42 {return *(mNDBlock.Begin()+r*mNc+c);} 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);} 43 50 inline T* Data() {return mNDBlock.Begin();} 44 51 inline const T* Data() const {return mNDBlock.Begin();} … … 46 53 inline const NDataBlock<T>& DataBlock() const {return mNDBlock;} 47 54 55 // Operations matricielles 56 TMatrix<T> Transpose(void) const; 57 48 58 // Operateur d'affectation 49 TMatrix<T>& operator = (T x); 50 TMatrix<T>& operator = (const TMatrix<T>& a); 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;} 51 67 52 68 // Impression … … 55 71 {Print(cout,lp,i0,ni,j0,nj);} 56 72 57 // Surcharge d'operateurs INPLACE 58 TMatrix<T>& operator += (T b);59 TMatrix<T>& operator -= (T b);60 TMatrix<T>& operator *= (T b);61 TMatrix<T>& operator /= (T b);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;} 62 78 63 TMatrix<T>& operator += (const TMatrix<T>& a); 64 TMatrix<T>& operator -= (const TMatrix<T>& a); 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;} 65 88 TMatrix<T>& operator *= (const TMatrix<T>& a); 66 89 67 // Pour surcharge d'operateurs 90 // Pour surcharge d'operateurs C = A (+,-,*) B 68 91 TMatrix<T> Add(const TMatrix<T>& b) const; 69 92 TMatrix<T> Sub(const TMatrix<T>& b) const; … … 71 94 72 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 73 103 uint_4 mNr,mNc; 74 104 NDataBlock<T> mNDBlock; 75 105 }; 76 106 77 ///////////////////////////////////////////////////////////////////////// 78 template<class T> 107 //////////////////////////////////////////////////////////////// 108 // Impression 109 110 template <class T> 79 111 inline ostream& operator << (ostream& os, const TMatrix<T>& a) 80 {a.Print(os); return(os);}112 {a.Print(os); return(os);} 81 113 82 ///////////////////////////////////////////////////////////////////////// 83 template<class T> 84 inline TMatrix<T> operator + (const TMatrix<T>& a, double b) { 85 TMatrix<T> result(a,false); result.DataBlock().SetTemp(true); 86 result += b; 87 return result; 88 } 114 //////////////////////////////////////////////////////////////// 115 // Surcharge d'operateurs A (+=,-=,*=,/=) (T) x 89 116 90 template<class T> 91 inline TMatrix<T> operator + (double b,const TMatrix<T>& a) { 92 TMatrix<T> result(a,false); result.DataBlock().SetTemp(true); 93 result += b; 94 return result; 95 } 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;} 96 119 97 template<class T> 98 inline TMatrix<T> operator - (const TMatrix<T>& a, double b) { 99 TMatrix<T> result(a,false); result.DataBlock().SetTemp(true); 100 result -= b; 101 return result; 102 } 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;} 103 122 104 template<class T> 105 inline TMatrix<T> operator - (double b,const TMatrix<T>& a) { 106 TMatrix<T> result(a,false); result.DataBlock().SetTemp(true); 107 result.DataBlock() = b - result.DataBlock(); 108 return result; 109 } 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;} 110 125 111 template<class T> 112 inline TMatrix<T> operator * (const TMatrix<T>& a, double b) { 113 TMatrix<T> result(a,false); result.DataBlock().SetTemp(true); 114 result *= b; 115 return result; 116 } 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;} 117 129 118 template<class T> 119 inline TMatrix<T> operator * (double b,const TMatrix<T>& a) { 120 TMatrix<T> result(a,false); result.DataBlock().SetTemp(true); 121 result *= b; 122 return result; 123 } 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;} 124 132 125 template<class T> 126 inline TMatrix<T> operator / (const TMatrix<T>& a, double b) { 127 TMatrix<T> result(a,false); result.DataBlock().SetTemp(true); 128 result /= b; 129 return result; 130 } 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;} 131 135 132 ///////////////////////////////////////////////////////////////////////// 133 template<class T> 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> 134 143 inline TMatrix<T> operator + (const TMatrix<T>& a,const TMatrix<T>& b) 135 144 {return a.Add(b);} 136 145 137 template <class T>146 template <class T> 138 147 inline TMatrix<T> operator - (const TMatrix<T>& a,const TMatrix<T>& b) 139 148 {return a.Sub(b);} 140 149 141 template <class T>150 template <class T> 142 151 inline TMatrix<T> operator * (const TMatrix<T>& a,const TMatrix<T>& b) 143 {return a.Mul(b);} 152 {return a.Mul(b);} 153 144 154 145 155 ///////////////////////////////////////////////////////////////////////// … … 147 157 template <class T> 148 158 class FIO_TMatrix : public PPersist { 149 150 159 public: 151 FIO_TMatrix(); 152 FIO_TMatrix(string const & filename); 153 FIO_TMatrix(const TMatrix<T> & obj); 154 FIO_TMatrix(TMatrix<T> * obj); 155 virtual ~FIO_TMatrix(); 156 157 virtual AnyDataObj* DataObj(); 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(); 158 166 inline operator TMatrix<T>() { return(*dobj); } 159 160 167 protected : 161 virtual void 162 virtual void 168 virtual void ReadSelf(PInPersist&); 169 virtual void WriteSelf(POutPersist&) const; 163 170 TMatrix<T> * dobj; 164 171 bool ownobj;
Note:
See TracChangeset
for help on using the changeset viewer.