Changeset 926 in Sophya for trunk/SophyaLib/TArray/sopemtx.cc
- Timestamp:
- Apr 13, 2000, 8:39:39 PM (25 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaLib/TArray/sopemtx.cc
r813 r926 14 14 // ------------------------------------------------------------- 15 15 //////////////////////////////////////////////////////////////// 16 ///////////////////////////////////////////////////////////////////////// 17 // Classe de lignes/colonnes de matrices 18 enum TRCKind {TmatrixRow=0, TmatrixCol=1, TmatrixDiag=2}; 16 17 //! Class of line, column or diagonal of a TMatrix 18 /*! 19 A TMatrixRC represents a line, a column or the diagonal of a TMatrix 20 */ 19 21 template <class T> 20 22 class TMatrixRC { 21 23 public: 24 //! Define type of TMatrixRC 25 enum TRCKind { 26 TmatrixRow=0, //!< TMatrixRC ligne 27 TmatrixCol=1, //!< TMatrixRC column 28 TmatrixDiag=2 //!< TMatrixRC diagonal 29 }; 22 30 TMatrixRC(); 23 31 TMatrixRC(TMatrix<T>& m, TRCKind kind, uint_4 index=0); … … 39 47 static T* Org(const TMatrix<T>&, TRCKind rckind, uint_4 ind=0); 40 48 49 //! Return the kind of TMatrix (line,column,diagonal) 41 50 TRCKind Kind() const { return kind; } 42 51 uint_4 NElts() const; … … 67 76 static void Swap(TMatrixRC<T>& rc1, TMatrixRC<T>& rc2); 68 77 78 //! Define Absolute value for uint_1 69 79 inline static double Abs_Value(uint_1 v) {return (double) v;} 80 //! Define Absolute value for uint_2 70 81 inline static double Abs_Value(uint_2 v) {return (double) v;} 82 //! Define Absolute value for int_2 71 83 inline static double Abs_Value(int_2 v) {return (v>0)? (double) v: (double) -v;} 84 //! Define Absolute value for int_4 72 85 inline static double Abs_Value(int_4 v) {return (v>0)? (double) v: (double) -v;} 86 //! Define Absolute value for int_8 73 87 inline static double Abs_Value(int_8 v) {return (v>0)? (double) v: (double) -v;} 88 //! Define Absolute value for uint_4 74 89 inline static double Abs_Value(uint_4 v) {return (double) v;} 90 //! Define Absolute value for uint_8 75 91 inline static double Abs_Value(uint_8 v) {return (double) v;} 92 //! Define Absolute value for r_4 76 93 inline static double Abs_Value(r_4 v) {return (double) fabsf(v);} 94 //! Define Absolute value for r_8 77 95 inline static double Abs_Value(r_8 v) {return fabs(v);} 78 inline static double Abs_Value(complex<float> v) 96 //! Define Absolute value for complex r_4 97 inline static double Abs_Value(complex<r_4> v) 79 98 {return sqrt(v.real()*v.real()+v.imag()*v.imag());} 80 inline static double Abs_Value(complex<double> v) 99 //! Define Absolute value for complex r_8 100 inline static double Abs_Value(complex<r_8> v) 81 101 {return sqrt(v.real()*v.real()+v.imag()*v.imag());} 82 102 83 103 protected: 84 TMatrix<T>* matrix; 85 T* data; 86 int_4 index; 87 uint_4 step; 88 TRCKind kind; 104 TMatrix<T>* matrix; //!< pointer to the TMatrix 105 T* data; //!< pointer to the beginnig of interesting datas 106 int_4 index; //!< index of the line/column 107 uint_4 step; //!< step of the line/column 108 TRCKind kind; //!< type: line, column or diagonal 89 109 }; 90 110 91 111 92 112 //! Multiply two TMatrixRC 93 113 template <class T> 94 114 inline T operator * (const TMatrixRC<T>& a, const TMatrixRC<T>& b) … … 103 123 } 104 124 105 125 //! Get the step in datas for a TMatrix for type rckind (line/col/diag) 106 126 template <class T> 107 127 inline uint_4 TMatrixRC<T>::Step(const TMatrix<T>& m, TRCKind rckind) … … 111 131 return 0; } 112 132 133 /*! Get the origin of datas for a TMatrix for type rckind and 134 number index (line/col/diag). ex: origine for line "index". */ 113 135 template <class T> 114 136 inline T* TMatrixRC<T>::Org(const TMatrix<T>& m, TRCKind rckind, uint_4 index) … … 118 140 return NULL; } 119 141 142 //! return number of elements for a TMatrixRC 120 143 template <class T> inline uint_4 TMatrixRC<T>::NElts() const 121 144 { if (!matrix) return 0; … … 125 148 return 0; } 126 149 150 //! access of element \b i 127 151 template <class T> 128 152 inline T& TMatrixRC<T>::operator()(uint_4 i) {return data[i*step];} 153 //! access of element \b i 129 154 template <class T> 130 155 inline T TMatrixRC<T>::operator()(uint_4 i) const {return data[i*step];} 131 156 132 157 //////////////////////////////////////////////////////////////// 133 // Typedef pour simplifier et compatibilite Peida158 //! Typedef to simplifier TMatrixRC<r_8> 134 159 typedef TMatrixRC<r_8> MatrixRC; 135 160 136 161 162 //! Default constructor 137 163 template <class T> TMatrixRC<T>::TMatrixRC() 138 164 : matrix(NULL), data(NULL), index(0), step(0) 139 165 {} 140 166 167 //! Constructor 168 /*! 169 \param m : matrix 170 \param rckind : select line, column or diagonal 171 \param ind : number of the line or column 172 */ 141 173 template <class T> TMatrixRC<T>::TMatrixRC(TMatrix<T>& m,TRCKind rckind,uint_4 ind) 142 174 : matrix(&m), data(Org(m,rckind,ind)), … … 150 182 // Acces aux rangees et colonnes de matrices 151 183 184 //! Return TMatrixRC for line \b r of matrix \b m 152 185 template <class T> 153 186 TMatrixRC<T> TMatrixRC<T>::Row(TMatrix<T> & m, uint_4 r) … … 157 190 } 158 191 192 //! Return TMatrixRC for column \b r of matrix \b m 159 193 template <class T> 160 194 TMatrixRC<T> TMatrixRC<T>::Col(TMatrix<T> & m, uint_4 c) … … 164 198 } 165 199 200 //! Return TMatrixRC for diagonal of matrix \b m 166 201 template <class T> 167 202 TMatrixRC<T> TMatrixRC<T>::Diag(TMatrix<T> & m) … … 197 232 // } 198 233 199 234 //! Set column \b c for this TMatrixRC 200 235 template <class T> int_4 TMatrixRC<T>::SetCol(int_4 c) 201 236 { … … 209 244 } 210 245 246 //! Set line \b r for this TMatrixRC 211 247 template <class T> int_4 TMatrixRC<T>::SetRow(int_4 r) 212 248 { … … 220 256 } 221 257 258 //! Set line diaginal for this TMatrixRC 222 259 template <class T> int_4 TMatrixRC<T>::SetDiag() 223 260 { … … 232 269 } 233 270 234 271 //! Operator = 235 272 template <class T> TMatrixRC<T>& TMatrixRC<T>::operator = (const TMatrixRC<T>& rc) 236 273 { … … 271 308 // } 272 309 273 310 //! Operator to multiply by constant \b x 274 311 template <class T> TMatrixRC<T>& TMatrixRC<T>::operator *= (T x) 275 312 { … … 278 315 } 279 316 317 //! Operator to divide by constant \b x 280 318 template <class T> TMatrixRC<T>& TMatrixRC<T>::operator /= (T x) 281 319 { … … 298 336 // } 299 337 338 //! Linear combination 339 /*! 340 Do : \f$ MRC(i) = MRC(i)*a + rc(i)*b \f$ 341 \return *this 342 */ 300 343 template <class T> 301 344 TMatrixRC<T>& TMatrixRC<T>::LinComb(T a, T b, const TMatrixRC<T>& rc, uint_4 first) … … 309 352 } 310 353 354 //! Linear combination 355 /*! 356 Do : \f$ MRC(i) = MRC(i) + rc(i)*b \f$ 357 */ 311 358 template <class T> 312 359 TMatrixRC<T>& TMatrixRC<T>::LinComb(T b, const TMatrixRC<T>& rc, uint_4 first) … … 320 367 } 321 368 369 //! Find maximum absolute value in TMatrixRC, search begin at \b first 322 370 template <class T> uint_4 TMatrixRC<T>::IMaxAbs(uint_4 first) 323 371 { … … 333 381 } 334 382 383 //! Print on stream \b os 335 384 template <class T> 336 385 void TMatrixRC<T>::Print(ostream & os) const … … 346 395 } 347 396 397 //! Swap two TMatrixRC of the same kind 348 398 template <class T> 349 399 void TMatrixRC<T>::Swap(TMatrixRC<T>& rc1, TMatrixRC<T>& rc2) … … 359 409 360 410 361 362 363 411 //////////////////////////////////////////////////////////////// 412 // ------------------------------------------------------------- 413 // La classe de calcul simple sur les TMatrix 414 // ------------------------------------------------------------- 415 //////////////////////////////////////////////////////////////// 416 364 417 //**** Pour inversion 365 418 #ifndef M_LN2 … … 373 426 #endif 374 427 428 //! Gaussian pivoting 429 /*! 430 Diagonalize matrix \b a, doing the same opreations on matrix \b b 431 \return determinat of \b a 432 */ 375 433 template <class T> 376 434 T SimpleMatrixOperation<T>::GausPiv(TMatrix<T>& a, TMatrix<T>& b) … … 394 452 double nrm = sqrt(vmin*vmax); 395 453 if(nrm > 1.e5 || nrm < 1.e-5) { 396 a /= nrm;397 b /= nrm;454 a /= (T) nrm; 455 b /= (T) nrm; 398 456 //cout << "normalisation matrice " << nrm << endl; 399 457 } else nrm=1; 400 458 401 double det = 1.0;459 T det = 1; 402 460 if(nrm != 1) { 403 461 double ld = a.NRows() * log(nrm); … … 405 463 // cerr << "TMatrix warning, overflow for det" << endl; 406 464 } else { 407 det = exp(ld);408 } 409 } 410 411 TMatrixRC<T> pivRowa(a,T matrixRow);412 TMatrixRC<T> pivRowb(b,T matrixRow);465 det = (T) exp(ld); 466 } 467 } 468 469 TMatrixRC<T> pivRowa(a,TMatrixRC<T>::TmatrixRow); 470 TMatrixRC<T> pivRowb(b,TMatrixRC<T>::TmatrixRow); 413 471 414 472 for(uint_4 k=0; k<n-1; k++) { … … 422 480 TMatrixRC<T>::Swap(bIPiv,bK); 423 481 } 424 doublepivot = a(k,k);425 if (fabs(pivot) < 1.e-50) return 0.0;482 T pivot = a(k,k); 483 if( TMatrixRC<T>::Abs_Value(pivot) < 1.e-50 ) return (T) 0; 426 484 //det *= pivot; 427 485 pivRowa.SetRow(k); // to avoid constructors 428 486 pivRowb.SetRow(k); 429 487 for (uint_4 i=k+1; i<n; i++) { 430 doubler = -a(i,k)/pivot;488 T r = -a(i,k)/pivot; 431 489 TMatrixRC<T>::Row(a, i).LinComb(r, pivRowa); // + rapide que -= r * pivRowa 432 490 TMatrixRC<T>::Row(b, i).LinComb(r, pivRowb); … … 437 495 // on remonte 438 496 for(uint_4 kk=n-1; kk>0; kk--) { 439 doublepivot = a(kk,kk);440 if (fabs(pivot) <= 1.e-50) return 0.0;497 T pivot = a(kk,kk); 498 if( TMatrixRC<T>::Abs_Value(pivot) <= 1.e-50 ) return (T) 0; 441 499 pivRowa.SetRow(kk); // to avoid constructors 442 500 pivRowb.SetRow(kk); 443 501 for(uint_4 jj=0; jj<kk; jj++) { 444 doubler = -a(jj,kk)/pivot;502 T r = -a(jj,kk)/pivot; 445 503 TMatrixRC<T>::Row(a, jj).LinComb(r, pivRowa); 446 504 TMatrixRC<T>::Row(b, jj).LinComb(r, pivRowb); … … 449 507 450 508 for(uint_4 l=0; l<n; l++) { 451 if (fabs((double)a(l,l)) <= 1.e-50) return 0.0;509 if( TMatrixRC<T>::Abs_Value(a(l,l)) <= 1.e-50 ) return (T) 0; 452 510 TMatrixRC<T>::Row(b, l) /= a(l,l); 453 511 } … … 456 514 } 457 515 516 //! Return the inverse matrix of \b A 458 517 template <class T> 459 518 TMatrix<T> SimpleMatrixOperation<T>::Inverse(TMatrix<T> const & A) 460 // Inversion461 519 { 462 520 TMatrix<T> a(A); 463 521 TMatrix<T> b(a.NCols(),a.NRows()); b = IdentityMatrix(1.); 464 if( fabs((double)GausPiv(a,b)) < 1.e-50)465 throw(MathExc("TMatrix Inverse() Singular OMatrix"));522 if( TMatrixRC<T>::Abs_Value(GausPiv(a,b)) < 1.e-50) 523 throw(MathExc("TMatrix Inverse() Singular Matrix")); 466 524 return b; 467 525 } 468 526 527 528 //////////////////////////////////////////////////////////////// 529 // ------------------------------------------------------------- 530 // La classe fit lineaire 531 // ------------------------------------------------------------- 532 //////////////////////////////////////////////////////////////// 469 533 470 534 LinFitter::LinFitter() … … 626 690 #pragma define_template TMatrixRC<r_4> 627 691 #pragma define_template TMatrixRC<r_8> 692 #pragma define_template TMatrixRC< complex<r_4> > 693 #pragma define_template TMatrixRC< complex<r_8> > 628 694 #pragma define_template SimpleMatrixOperation<int_4> 629 695 #pragma define_template SimpleMatrixOperation<r_4> 630 696 #pragma define_template SimpleMatrixOperation<r_8> 697 #pragma define_template SimpleMatrixOperation< complex<r_4> > 698 #pragma define_template SimpleMatrixOperation< complex<r_8> > 631 699 #endif 632 700 … … 636 704 template class TMatrixRC<r_4>; 637 705 template class TMatrixRC<r_8>; 706 template class TMatrixRC< complex<r_4> >; 707 template class TMatrixRC< complex<r_8> >; 638 708 template class SimpleMatrixOperation<int_4>; 639 709 template class SimpleMatrixOperation<r_4>; 640 710 template class SimpleMatrixOperation<r_8>; 711 template class SimpleMatrixOperation< complex<r_4> >; 712 template class SimpleMatrixOperation< complex<r_8> >; 641 713 #endif
Note:
See TracChangeset
for help on using the changeset viewer.