Changeset 288 in Sophya for trunk/SophyaLib/NTools
- Timestamp:
- May 3, 1999, 6:55:25 PM (26 years ago)
- Location:
- trunk/SophyaLib/NTools
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaLib/NTools/matrix.cc
r244 r288 1 // $Id: matrix.cc,v 1. 2 1999-04-22 16:18:41 ansari Exp $1 // $Id: matrix.cc,v 1.3 1999-05-03 16:55:21 ansari Exp $ 2 2 3 3 #include "machdefs.h" … … 10 10 #include "cvector.h" 11 11 #include "generalfit.h" 12 #include "tmatrix.h" 12 13 13 14 // Sous Linux, LN_MAXDOUBLE LN_MINDOUBLE ne semblent pas etre definies … … 41 42 // Construit une matrice 1x1 (pour ppersist). 42 43 //-- 43 : nr(1), nc(1), ndata(1), nalloc(1), data(new double[1]) 44 : nr(1), nc(1), ndata(1), nalloc(1), data(new double[1]), FromTmatrix(false) 44 45 { 45 46 // if (r<=0 || c<=0) THROW(rangeCheckErr); … … 53 54 // Construit une matrice de r lignes et c colonnes. 54 55 //-- 55 : nr(r), nc(c), ndata(r*c), nalloc(r*c), data(new double[r*c]) 56 : nr(r), nc(c), ndata(r*c), nalloc(r*c), data(new double[r*c]), FromTmatrix(false) 56 57 { 57 58 // if (r<=0 || c<=0) THROW(rangeCheckErr); … … 66 67 // le tableau des valeurs : pas d'allocation. 67 68 //-- 68 : nr(r), nc(c), ndata(r*c), nalloc(0), data(values) 69 : nr(r), nc(c), ndata(r*c), nalloc(0), data(values), FromTmatrix(false) 69 70 { 70 71 END_CONSTRUCTOR … … 77 78 //-- 78 79 : nr(a.nr), nc(a.nc), ndata(a.nr*a.nc), nalloc(a.nr*a.nc), 79 data(new double[a.nr*a.nc]) 80 data(new double[a.nr*a.nc]), FromTmatrix(false) 80 81 { 81 82 memcpy(data, a.data, nalloc * sizeof(double)); … … 83 84 } 84 85 86 //++ 87 Matrix::Matrix(const TMatrix<r_8>& a) 88 // 89 // Constructeur par copie a partir d'une TMatrix<r_8>. 90 // Attention, les donnees sont partagees. 91 //-- 92 : nr(a.NRows()), nc(a.NCols()), ndata(a.NRows()*a.NCols()) 93 , nalloc(a.NRows()*a.NCols()) 94 , data(const_cast<r_8 *>(a.Data())) 95 , FromTmatrix(true) 96 { 97 END_CONSTRUCTOR 98 } 99 85 100 86 101 Matrix::~Matrix() 87 102 { 88 103 DBASSERT(ndata == nr*nc); 89 if (nalloc ) delete[] data;104 if (nalloc && !FromTmatrix) delete[] data; 90 105 } 91 106 … … 102 117 { 103 118 DBASSERT(ndata == nr*nc); 104 for (int i=0; i<ndata; i++) 105 data[i] = 0; 119 for (int i=0; i<ndata; i++) data[i] = 0; 106 120 } 107 121 -
trunk/SophyaLib/NTools/matrix.h
r220 r288 12 12 13 13 class GeneralFit; 14 #include "tmatrix.h" 15 //cmv template <class T> class TMatrix; 14 16 15 17 // <summary> Operations matricielles </summary> … … 18 20 friend class Vector; 19 21 friend class MatrixRC; 22 friend class TMatrix<r_8>; 20 23 public: 21 24 Matrix(); … … 26 29 // Constructeur par copie 27 30 Matrix(const Matrix& a); 31 // Constructeur par copie a partir d'une TMatrix<r_8> 32 Matrix(const TMatrix<r_8>& a); 28 33 // Destructeur 29 34 virtual ~Matrix(); … … 151 156 int nalloc; 152 157 r_8* data; 158 bool FromTmatrix; 153 159 }; 154 160 -
trunk/SophyaLib/NTools/tmatrix.cc
r286 r288 1 // $Id: tmatrix.cc,v 1. 2 1999-04-30 11:02:52 ansari Exp $1 // $Id: tmatrix.cc,v 1.3 1999-05-03 16:55:22 ansari Exp $ 2 2 // C.Magneville 04/99 3 3 #include "machdefs.h" … … 13 13 14 14 //////////////////////////////////////////////////////////////// 15 //**** ******** Createur, Destructeur, gestion des donnees15 //**** Createur, Destructeur 16 16 17 17 template <class T> … … 39 39 template <class T> 40 40 TMatrix<T>::TMatrix(const TMatrix<T>& a) 41 // Constructeur par copie (partage des donnees).41 // Constructeur par copie (partage si "a" temporaire). 42 42 : mNr(a.mNr), mNc(a.mNc), mNDBlock(a.mNDBlock) 43 43 { … … 46 46 template <class T> 47 47 TMatrix<T>::TMatrix(const TMatrix<T>& a,bool share) 48 // Constructeur par copie .48 // Constructeur par copie avec possibilite de forcer le partage ou non. 49 49 : mNr(a.mNr), mNc(a.mNc), mNDBlock(a.mNDBlock,share) 50 50 { … … 58 58 59 59 //////////////////////////////////////////////////////////////// 60 template <class T> 61 void TMatrix<T>::Clone(const TMatrix<T>& a) 62 // Clone (copie de donnees) a partir de "a" 63 // Partage des donnees si "a" est temporaire. 64 { 65 mNDBlock.Clone(a.mNDBlock); mNr = a.mNr; mNc = a.mNc; 66 } 67 68 template <class T> 69 void TMatrix<T>::Reset(T v) 70 // Reset de la matrice a "v" 71 { 72 mNDBlock.Reset(v); 73 } 74 75 template <class T> 76 void TMatrix<T>::Realloc(uint_4 r,uint_4 c,bool force_alloc) 77 // Reallocation de place 78 { 79 if(r==0 || c==0) throw(SzMismatchError("TMatrix::ReSize r ou c==0\n")); 80 if(!force_alloc && mNr==r && mNc==c) return; 81 mNr = r; mNc = c; mNDBlock.ReSize(r*c,force_alloc); 82 } 83 84 //////////////////////////////////////////////////////////////// 85 //**** Surcharge de TMatrix=TMatrix; TMatrix=<T> b; 86 87 template <class T> 88 TMatrix<T>& TMatrix<T>::operator = (T x) 89 // Operateur d'affectation depuis scalaire : identite * scalaire. 90 { 91 if(mNr!=mNc || mNr==0) 92 throw(SzMismatchError("TMatrix::operator= mNc!=mNr ou ==0\n")); 93 for(uint_4 r=0;r<mNr;r++) for(uint_4 c=0;c<mNc;c++) 94 (*this)(r,c) = (r==c)? x: 0; 95 return *this; 96 } 97 98 template <class T> 99 TMatrix<T>& TMatrix<T>::operator = (const TMatrix<T>& a) 100 // Operateur d'affectation A=a 101 // (Re)allocation de la place memoire 102 // sauf si "a" est cree temporairement (partage) 103 { 104 if(this == &a) return *this; 105 Clone(a); 106 return *this; 60 // Operations matricielles 61 template <class T> 62 TMatrix<T> TMatrix<T>::Transpose(void) const 63 // Transposition 64 { 65 TMatrix<T> a; a.Clone(*this); a.SetTemp(true); 66 a.mNr = mNc; a.mNc = mNr; 67 {for(int i=0; i<mNr; i++) 68 for(int j=0; j<mNc; j++) { 69 a(j,i) = (*this)(i,j); 70 }} 71 return a; 107 72 } 108 73 … … 115 80 // Impression de la sous-matrice (i:i+ni-1,i:j+nj-1) 116 81 { 117 82 os<<"TMatrix::Print("<<mNr<<","<<mNc<<")"<<endl; 118 83 if(lp>0) 119 84 {os<<" this="<<this<<endl; mNDBlock.Print(0,0);} … … 129 94 130 95 //////////////////////////////////////////////////////////////// 131 //**** Surcharge de +=,-=,*=,/= (INPLACE): TMatrix += <T> b; 132 133 template <class T> 134 TMatrix<T>& TMatrix<T>::operator += (T b) 135 { 136 mNDBlock += b; 137 return *this; 138 } 139 140 template <class T> 141 TMatrix<T>& TMatrix<T>::operator -= (T b) 142 { 143 mNDBlock -= b; 144 return *this; 145 } 146 147 template <class T> 148 TMatrix<T>& TMatrix<T>::operator *= (T b) 149 { 150 mNDBlock *= b; 151 return *this; 152 } 153 154 template <class T> 155 TMatrix<T>& TMatrix<T>::operator /= (T b) 156 { 157 mNDBlock /= b; 158 return *this; 159 } 160 161 //////////////////////////////////////////////////////////////// 162 //**** Surcharge de +=,-=,*= (INPLACE): TMatrix += TMatrix; 163 164 template <class T> 165 TMatrix<T>& TMatrix<T>::operator += (const TMatrix<T>& a) 166 { 167 if(mNr==0 || mNc==0 || mNr!=a.mNr || mNc!=a.mNc) 168 throw(SzMismatchError("TMatrix::operator+=A size mismatch")); 169 mNDBlock += a.mNDBlock; 170 return *this; 171 } 172 173 template <class T> 174 TMatrix<T>& TMatrix<T>::operator -= (const TMatrix<T>& a) 175 { 176 if(mNr==0 || mNc==0 || mNr!=a.mNr || mNc!=a.mNc) 177 throw(SzMismatchError("TMatrix::operator-=A size mismatch")); 178 mNDBlock -= a.mNDBlock; 179 return *this; 180 } 96 //**** Surcharge de *= (INPLACE): TMatrix *= TMatrix; 181 97 182 98 template <class T> … … 209 125 210 126 //////////////////////////////////////////////////////////////// 211 //**** Surcharge de +,-,*: TMatrix = TMatrix + TMatrix; 212 213 template <class T> 214 TMatrix<T> TMatrix<T>::Add(const TMatrix<T>& b) const 215 // C = A(this)+B 127 //**** Pour surcharge d'operateurs C = A (+,-,*,/) B 128 129 template <class T> TMatrix<T> TMatrix<T>::Add(const TMatrix<T>& b) const 216 130 { 217 131 if(mNr!=b.mNr || mNc!=b.mNc) 218 throw(SzMismatchError("NDataBlock operator C=A+B size mismatch\n")); 219 TMatrix<T> result; result.SetTemp(true); 220 if(b.IsTemp()) { 221 result.Clone(b); 222 result += *this; 223 } else { 224 result.Clone(*this); 225 result += b; 226 } 132 throw(SzMismatchError("NDataBlock operator C=A+B size mismatch\n")); 133 TMatrix<T> result; result.SetTemp(true); result.mNr=mNr; result.mNc=mNc; 134 result.mNDBlock = mNDBlock+b.mNDBlock; 227 135 return result; 228 136 } 229 137 230 template <class T> 231 TMatrix<T> TMatrix<T>::Sub(const TMatrix<T>& b) const 232 // C = A(this)-B 138 template <class T> TMatrix<T> TMatrix<T>::Sub(const TMatrix<T>& b) const 233 139 { 234 140 if(mNr!=b.mNr || mNc!=b.mNc) 235 throw(SzMismatchError("NDataBlock operator C=A-B size mismatch\n")); 236 TMatrix<T> result; result.SetTemp(true); 237 if(b.IsTemp()) { 238 result.Clone(b); 239 result.mNDBlock = (*this).mNDBlock - result.mNDBlock; 240 } else { 241 result.Clone(*this); 242 result -= b; 243 } 141 throw(SzMismatchError("NDataBlock operator C=A-B size mismatch\n")); 142 TMatrix<T> result; result.SetTemp(true); result.mNr=mNr; result.mNc=mNc; 143 result.mNDBlock = mNDBlock-b.mNDBlock; 244 144 return result; 245 145 } 246 146 247 template <class T> 248 TMatrix<T> TMatrix<T>::Mul(const TMatrix<T>& b) const 147 template <class T> TMatrix<T> TMatrix<T>::Mul(const TMatrix<T>& b) const 249 148 // C = A(this)*B : Cij = Aik Bkj (allocation forcee dans tous les cas) 250 149 { 251 150 if(mNr==0 || mNc==0 || b.mNc==0 || mNc!=b.mNr) 252 151 throw(SzMismatchError("NDataBlock operator C=A*B size mismatch\n")); 253 TMatrix<T> r; r.SetTemp(true); r.Re alloc(mNr,b.mNc,true);152 TMatrix<T> r; r.SetTemp(true); r.ReSize(mNr,b.mNc); 254 153 T *ai,*aik,*bj,*bkj,*ri,*rij; 255 154 for(ri=const_cast<T *>(r.Data()),ai=const_cast<T *>(Data()); … … 317 216 is.Get(itab,2); 318 217 if (dobj == NULL) dobj = new TMatrix<T>(itab[0],itab[1]); 319 else dobj->Re alloc(itab[0],itab[1],false);218 else dobj->ReSize(itab[0],itab[1]); 320 219 // On lit le NDataBlock 321 220 //cmv encore des problemes avec les consteries … … 331 230 uint_4 itab[2]; 332 231 itab[0] = dobj->NRows(); 333 itab[1] = dobj->NCol ();232 itab[1] = dobj->NCols(); 334 233 os.Put(itab,2); 335 234 // On ecrit le NDataBlock -
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.