Changeset 970 in Sophya for trunk/SophyaLib
- Timestamp:
- Apr 26, 2000, 7:55:11 PM (25 years ago)
- Location:
- trunk/SophyaLib/TArray
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaLib/TArray/tarray.cc
r967 r970 140 140 TArray<T>& TArray<T>::Set(const TArray<T>& a) 141 141 { 142 if (this != &a) {143 CloneOrShare(a);144 if (mInfo) {delete mInfo; mInfo = NULL;}145 if (a.mInfo) mInfo = new DVList(*(a.mInfo));146 }142 if (this == &a) return(*this); 143 if (a.NbDimensions() < 1) 144 throw RangeCheckError("TArray<T>::Set(a ) - Array a not allocated ! "); 145 if (NbDimensions() < 1) CloneOrShare(a); 146 else CopyElt(a); 147 147 return(*this); 148 148 } … … 158 158 if (a.mInfo) mInfo = new DVList(*(a.mInfo)); 159 159 } 160 161 //! Clone if \b a is not temporary, share if temporary 162 template <class T> 163 void TArray<T>::CloneOrShare(const TArray<T>& a) 164 { 165 string exmsg = "TArray<T>::CloneOrShare()"; 166 if (!UpdateSizes(a.ndim_, a.size_, a.step_, a.offset_, exmsg)) throw( ParmError(exmsg) ); 167 mNDBlock.CloneOrShare(a.mNDBlock); 168 } 169 170 //! Share data with a 171 template <class T> 172 void TArray<T>::Share(const TArray<T>& a) 173 { 174 string exmsg = "TArray<T>::Share()"; 175 if (!UpdateSizes(a.ndim_, a.size_, a.step_, a.offset_, exmsg)) throw( ParmError(exmsg) ); 176 mNDBlock.Share(a.mNDBlock); 177 } 178 160 179 161 180 //! Resize array … … 261 280 throw RangeCheckError("TArray<T>::PackElements() - Not Allocated Array ! "); 262 281 if ( !force && (AvgStep() == 1) ) { 263 TArray<T> ra(*this, true); 282 TArray<T> ra; 283 ra.Share(*this); 264 284 ra.SetTemp(true); 265 285 return(ra); … … 408 428 409 429 //! Substract a constant value \b x to an array 410 template <class T> 411 TArray<T>& TArray<T>::Sub(T x) 430 /*! 431 Substract a constant from the *this = *this-x 432 \param fginv == true : Perfoms the inverse subtraction (*this = x-(*this)) 433 */ 434 template <class T> 435 TArray<T>& TArray<T>::Sub(T x, bool fginv) 412 436 { 413 437 if (NbDimensions() < 1) … … 419 443 uint_8 maxx = totsize_*step; 420 444 pe = Data(); 421 for(k=0; k<maxx; k+=step ) pe[k] -= x; 445 if (fginv) 446 for(k=0; k<maxx; k+=step ) pe[k] = x-pe[k]; 447 else 448 for(k=0; k<maxx; k+=step ) pe[k] -= x; 422 449 } 423 450 else { // Non regular data spacing ... … … 428 455 for(j=0; j<naxa; j++) { 429 456 pe = mNDBlock.Begin()+Offset(ka,j); 430 for(k=0; k<gpas; k+=step) pe[k] -= x; 457 if (fginv) 458 for(k=0; k<gpas; k+=step) pe[k] = x-pe[k]; 459 else 460 for(k=0; k<gpas; k+=step) pe[k] -= x; 431 461 } 432 462 } … … 462 492 463 493 //! Divide an array by a constant value \b x 464 template <class T> 465 TArray<T>& TArray<T>::Div(T x) 494 /*! 495 Divide the array by a constant *this = *this/x 496 \param fginv == true : Perfoms the inverse division (*this = x/(*this)) 497 */ 498 template <class T> 499 TArray<T>& TArray<T>::Div(T x, bool fginv) 466 500 { 467 501 if (NbDimensions() < 1) 468 502 throw RangeCheckError("TArray<T>::Div(T ) - Not Allocated Array ! "); 469 if ( x == (T) 0)503 if (!fginv && (x == (T) 0) ) 470 504 throw MathExc("TArray<T>::Div(T ) - Divide by zero ! "); 471 505 T * pe; … … 475 509 uint_8 maxx = totsize_*step; 476 510 pe = Data(); 477 for(k=0; k<maxx; k+=step ) pe[k] /= x; 511 if (fginv) 512 for(k=0; k<maxx; k+=step ) pe[k] = x/pe[k]; 513 else 514 for(k=0; k<maxx; k+=step ) pe[k] /= x; 478 515 } 479 516 else { // Non regular data spacing ... … … 484 521 for(j=0; j<naxa; j++) { 485 522 pe = mNDBlock.Begin()+Offset(ka,j); 486 for(k=0; k<gpas; k+=step) pe[k] /= x; 487 } 488 } 489 return(*this); 490 } 491 492 493 //! Inverse substract : A = \b x - A 494 template <class T> 495 TArray<T>& TArray<T>::SubInv(T x) 496 { 497 if (NbDimensions() < 1) 498 throw RangeCheckError("TArray<T>::SubInv(T ) - Not Allocated Array ! "); 499 T * pe; 500 uint_8 j,k; 501 if (AvgStep() > 0) { // regularly spaced elements 502 uint_8 step = AvgStep(); 503 uint_8 maxx = totsize_*step; 504 pe = Data(); 505 for(k=0; k<maxx; k+=step ) pe[k] = x-pe[k]; 506 } 507 else { // Non regular data spacing ... 508 uint_4 ka = MaxSizeKA(); 509 uint_8 step = Step(ka); 510 uint_8 gpas = Size(ka)*step; 511 uint_8 naxa = Size()/Size(ka); 512 for(j=0; j<naxa; j++) { 513 pe = mNDBlock.Begin()+Offset(ka,j); 514 for(k=0; k<gpas; k+=step) pe[k] = x-pe[k]; 515 } 516 } 517 return(*this); 518 } 519 520 //! Inverse Divide : A(i,j,...) = x / A(i,j,...) 521 template <class T> 522 TArray<T>& TArray<T>::DivInv(T x) 523 { 524 if (NbDimensions() < 1) 525 throw RangeCheckError("TArray<T>::DivInv(T ) - Not Allocated Array ! "); 526 T * pe; 527 uint_8 j,k; 528 if (AvgStep() > 0) { // regularly spaced elements 529 uint_8 step = AvgStep(); 530 uint_8 maxx = totsize_*step; 531 pe = Data(); 532 for(k=0; k<maxx; k+=step ) pe[k] = x/pe[k]; 533 } 534 else { // Non regular data spacing ... 535 uint_4 ka = MaxSizeKA(); 536 uint_8 step = Step(ka); 537 uint_8 gpas = Size(ka)*step; 538 uint_8 naxa = Size()/Size(ka); 539 for(j=0; j<naxa; j++) { 540 pe = mNDBlock.Begin()+Offset(ka,j); 541 for(k=0; k<gpas; k+=step) pe[k] = x/pe[k]; 542 } 543 } 544 return(*this); 545 } 523 if (fginv) 524 for(k=0; k<gpas; k+=step) pe[k] = x/pe[k]; 525 else 526 for(k=0; k<gpas; k+=step) pe[k] /= x; 527 } 528 } 529 return(*this); 530 } 531 532 546 533 547 534 … … 583 570 584 571 //! Substract two TArrays 585 template <class T> 586 TArray<T>& TArray<T>::SubElt(const TArray<T>& a) 572 /*! 573 Substract two TArrays *this = *this-a 574 \param fginv == true : Perfoms the inverse subtraction (*this = a-(*this)) 575 */ 576 template <class T> 577 TArray<T>& TArray<T>::SubElt(const TArray<T>& a, bool fginv) 587 578 { 588 579 if (NbDimensions() < 1) … … 600 591 pe = Data(); 601 592 pea = a.Data(); 602 for(k=0, ka=0; k<maxx; k+=step, ka+=stepa ) pe[k] -= pea[ka] ; 593 if (fginv) 594 for(k=0, ka=0; k<maxx; k+=step, ka+=stepa ) pe[k] = pea[ka]-pe[k] ; 595 else 596 for(k=0, ka=0; k<maxx; k+=step, ka+=stepa ) pe[k] -= pea[ka] ; 603 597 } 604 598 else { // Non regular data spacing ... … … 611 605 pe = mNDBlock.Begin()+Offset(ax,j); 612 606 pea = a.DataBlock().Begin()+a.Offset(ax,j); 613 for(k=0, ka=0; k<gpas; k+=step, ka+=stepa) pe[k] -= pea[ka]; 614 } 615 } 616 return(*this); 617 } 607 if (fginv) 608 for(k=0, ka=0; k<gpas; k+=step, ka+=stepa) pe[k] = pea[ka]-pe[k] ; 609 else 610 for(k=0, ka=0; k<gpas; k+=step, ka+=stepa) pe[k] -= pea[ka]; 611 } 612 } 613 return(*this); 614 } 615 618 616 619 617 //! Multiply two TArrays (elements by elements) … … 654 652 655 653 //! Divide two TArrays (elements by elements) 656 template <class T> 657 TArray<T>& TArray<T>::DivElt(const TArray<T>& a) 654 /*! 655 Divide two TArrays *this = (*this)/a 656 \param fginv == true : Perfoms the inverse division (*this = a/(*this)) 657 */ 658 template <class T> 659 TArray<T>& TArray<T>::DivElt(const TArray<T>& a, bool fginv) 658 660 { 659 661 if (NbDimensions() < 1) … … 671 673 pe = Data(); 672 674 pea = a.Data(); 673 for(k=0, ka=0; k<maxx; k+=step, ka+=stepa ) pe[k] /= pea[ka] ; 675 if (fginv) 676 for(k=0, ka=0; k<maxx; k+=step, ka+=stepa ) pe[k] = pea[ka]/pe[k]; 677 else 678 for(k=0, ka=0; k<maxx; k+=step, ka+=stepa ) pe[k] /= pea[ka] ; 674 679 } 675 680 else { // Non regular data spacing ... … … 682 687 pe = mNDBlock.Begin()+Offset(ax,j); 683 688 pea = a.DataBlock().Begin()+a.Offset(ax,j); 684 for(k=0, ka=0; k<gpas; k+=step, ka+=stepa) pe[k] /= pea[ka]; 689 if (fginv) 690 for(k=0, ka=0; k<gpas; k+=step, ka+=stepa) pe[k] = pea[ka]/pe[k]; 691 else 692 for(k=0, ka=0; k<gpas; k+=step, ka+=stepa) pe[k] /= pea[ka]; 685 693 } 686 694 } … … 835 843 } 836 844 837 //! Clone if \b a is not temporary, share if temporary838 template <class T>839 void TArray<T>::CloneOrShare(const TArray<T>& a)840 {841 string exmsg = "TArray<T>::CloneOrShare()";842 if (!UpdateSizes(a.ndim_, a.size_, a.step_, a.offset_, exmsg)) throw( ParmError(exmsg) );843 mNDBlock.CloneOrShare(a.mNDBlock);844 }845 846 //! Share data with a847 template <class T>848 void TArray<T>::Share(const TArray<T>& a)849 {850 string exmsg = "TArray<T>::Share()";851 if (!UpdateSizes(a.ndim_, a.size_, a.step_, a.offset_, exmsg)) throw( ParmError(exmsg) );852 mNDBlock.Share(a.mNDBlock);853 }854 855 845 856 846 -
trunk/SophyaLib/TArray/tarray.h
r967 r970 46 46 // Gestion taille/Remplissage 47 47 virtual void Clone(const TArray<T>& a); 48 // partage les donnees si "a" temporaire, clone sinon. 49 void CloneOrShare(const TArray<T>& a); 50 // Share: partage les donnees de "a" 51 52 void Share(const TArray<T>& a); 48 53 void ReSize(uint_4 ndim, uint_4 * siz, uint_4 step=1); 49 54 void Realloc(uint_4 ndim, uint_4 * siz, uint_4 step=1, bool force=false); … … 122 127 //! Add \b x to all elements 123 128 inline TArray<T>& operator += (T x) { return Add(x); } 124 virtual TArray<T>& Sub(T x );129 virtual TArray<T>& Sub(T x, bool fginv=false); 125 130 //! Substract \b x to all elements 126 131 inline TArray<T>& operator -= (T x) { return Sub(x); } … … 128 133 //! Multiply all elements by \b x 129 134 inline TArray<T>& operator *= (T x) { return Mul(x); } 130 virtual TArray<T>& Div(T x );135 virtual TArray<T>& Div(T x, bool fginv=false); 131 136 //! Divide all elements by \b x 132 137 inline TArray<T>& operator /= (T x) { return Div(x); } 133 virtual TArray<T>& SubInv(T x); // A ---> x-A134 virtual TArray<T>& DivInv(T x); // A ---> x/A135 138 136 139 // A += -= (ajoute, soustrait element par element les deux tableaux ) … … 138 141 //! Operator TArray += TArray 139 142 inline TArray<T>& operator += (const TArray<T>& a) { return AddElt(a); } 140 virtual TArray<T>& SubElt(const TArray<T>& a );143 virtual TArray<T>& SubElt(const TArray<T>& a, bool fginv=false); 141 144 //! Operator TArray -= TArray 142 145 inline TArray<T>& operator -= (const TArray<T>& a) { return SubElt(a); } 143 146 // Multiplication, division element par element les deux tableaux 144 147 virtual TArray<T>& MulElt(const TArray<T>& a); 145 virtual TArray<T>& DivElt(const TArray<T>& a );148 virtual TArray<T>& DivElt(const TArray<T>& a, bool fginv=false); 146 149 // Recopie des valeurs, element par element 147 150 virtual TArray<T>& CopyElt(const TArray<T>& a); … … 159 162 160 163 protected: 161 // partage les donnees si "a" temporaire, clone sinon.162 void CloneOrShare(const TArray<T>& a);163 // Share: partage les donnees de "a"164 void Share(const TArray<T>& a);165 164 166 165 NDataBlock<T> mNDBlock; //!< Block for datas … … 180 179 \brief Operator TArray = TArray + constant */ 181 180 template <class T> inline TArray<T> operator + (const TArray<T>& a, T b) 182 {TArray<T> result(a); result.SetTemp(true); result.Add(b); return result;} 181 {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true); 182 result.Add(b); return result;} 183 183 184 184 /*! \ingroup TArray \fn operator+(T,const TArray<T>&) 185 185 \brief Operator TArray = constant + TArray */ 186 186 template <class T> inline TArray<T> operator + (T b,const TArray<T>& a) 187 {TArray<T> result(a); result.SetTemp(true); result.Add(b); return result;} 187 {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true); 188 result.Add(b); return result;} 188 189 189 190 /*! \ingroup TArray \fn operator-(const TArray<T>&,T) 190 191 \brief Operator TArray = TArray - constant */ 191 192 template <class T> inline TArray<T> operator - (const TArray<T>& a, T b) 192 {TArray<T> result(a); result.SetTemp(true); result.Sub(b); return result;} 193 {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true); 194 result.Sub(b); return result;} 193 195 194 196 /*! \ingroup TArray \fn operator-(T,const TArray<T>&) 195 197 \brief Operator TArray = constant - TArray */ 196 198 template <class T> inline TArray<T> operator - (T b,const TArray<T>& a) 197 {TArray<T> result(a); result.SetTemp(true); result.SubInv(b); return result;} 199 {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true); 200 result.Sub(b,true); return result;} 198 201 199 202 /*! \ingroup TArray \fn operator*(const TArray<T>&,T) 200 203 \brief Operator TArray = TArray * constant */ 201 204 template <class T> inline TArray<T> operator * (const TArray<T>& a, T b) 202 {TArray<T> result(a); result.SetTemp(true); result.Mul(b); return result;} 205 {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true); 206 result.Mul(b); return result;} 203 207 204 208 /*! \ingroup TArray \fn operator*(T,const TArray<T>&) 205 209 \brief Operator TArray = constant * TArray */ 206 210 template <class T> inline TArray<T> operator * (T b,const TArray<T>& a) 207 {TArray<T> result(a); result.SetTemp(true); result.Mul(b); return result;} 211 {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true); 212 result.Mul(b); return result;} 208 213 209 214 /*! \ingroup TArray \fn operator/(const TArray<T>&,T) 210 215 \brief Operator TArray = TArray / constant */ 211 216 template <class T> inline TArray<T> operator / (const TArray<T>& a, T b) 212 {TArray<T> result(a); result.SetTemp(true); result.DivInv(b); return result;} 217 {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true); 218 result.Div(b); return result;} 219 220 /*! \ingroup TArray \fn operator/(T,const TArray<T>&) 221 \brief Operator TArray = constant / TArray */ 222 template <class T> inline TArray<T> operator / (T b, const TArray<T>& a) 223 {TArray<T> result; result.CloneOrShare(a); result.SetTemp(true); 224 result.Div(b, true); return result;} 213 225 214 226 //////////////////////////////////////////////////////////////// … … 219 231 template <class T> 220 232 inline TArray<T> operator + (const TArray<T>& a,const TArray<T>& b) 221 {TArray<T> result(a); result.SetTemp(true); result.AddElt(b); return result;} 233 { TArray<T> result; result.SetTemp(true); 234 if (b.IsTemp()) { result.Share(b); result.AddElt(a); } 235 else { result.CloneOrShare(a); result.AddElt(b); } 236 return result; } 222 237 223 238 /*! \ingroup TArray \fn operator-(const TArray<T>&,const TArray<T>&) … … 225 240 template <class T> 226 241 inline TArray<T> operator - (const TArray<T>& a,const TArray<T>& b) 227 {TArray<T> result(a); result.SetTemp(true); result.SubElt(b); return result;} 242 { TArray<T> result; result.SetTemp(true); 243 if (b.IsTemp()) { result.Share(b); result.SubElt(a, true); } 244 else { result.CloneOrShare(a); result.SubElt(b); } 245 return result; } 228 246 229 247 -
trunk/SophyaLib/TArray/tmatrix.cc
r967 r970 1 // $Id: tmatrix.cc,v 1. 9 2000-04-21 16:31:25ansari Exp $1 // $Id: tmatrix.cc,v 1.10 2000-04-26 17:55:10 ansari Exp $ 2 2 // C.Magneville 04/99 3 3 #include "machdefs.h" … … 110 110 throw SzMismatchError("TMatrix<T>::Set(const TArray<T>& a) a.NbDimensions() > 2"); 111 111 TArray<T>::Set(a); 112 if ( a.NbDimensions() == 1) {112 if (NbDimensions() == 1) { 113 113 size_[1] = 1; 114 114 step_[1] = size_[0]*step_[0]; 115 115 ndim_ = 2; 116 116 } 117 UpdateMemoryMapping( a, SameMemoryMapping);117 UpdateMemoryMapping(*this, SameMemoryMapping); 118 118 return(*this); 119 119 } -
trunk/SophyaLib/TArray/tmatrix.h
r967 r970 136 136 template <class T> 137 137 inline TMatrix<T> operator + (const TMatrix<T>& a,const TMatrix<T>& b) 138 {TMatrix<T> result(a); result.SetTemp(true); result.AddElt(b); return result;} 138 {TMatrix<T> result; result.SetTemp(true); 139 if (b.IsTemp()) { result.Share(b); result.AddElt(a); } 140 else { result.CloneOrShare(a); result.AddElt(b); } 141 return result; } 142 139 143 140 144 /*! \ingroup TArray \fn operator-(const TMatrix<T>&,const TMatrix<T>&) … … 142 146 template <class T> 143 147 inline TMatrix<T> operator - (const TMatrix<T>& a,const TMatrix<T>& b) 144 {TMatrix<T> result(a); result.SetTemp(true); result.SubElt(b); return result;} 148 {TMatrix<T> result; result.SetTemp(true); 149 if (b.IsTemp()) { result.Share(b); result.SubElt(a, true); } 150 else { result.CloneOrShare(a); result.SubElt(b); } 151 return result; } 145 152 146 153 // Surcharge d'operateurs C = A * B … … 148 155 \brief * : multiply matrixes \b a and \b b */ 149 156 template <class T> inline TMatrix<T> operator * (const TMatrix<T>& a, const TMatrix<T>& b) 150 { TMatrix<T> result(a); result.SetTemp(true); return(result.Multiply(b)); }157 { return(a.Multiply(b)); } 151 158 152 159 // Typedef pour simplifier et compatibilite Peida
Note:
See TracChangeset
for help on using the changeset viewer.