source: Sophya/trunk/SophyaLib/TArray/sopemtx.cc@ 2927

Last change on this file since 2927 was 2883, checked in by ansari, 20 years ago

fonction _ZZ_TestTMatrixRC_YY_ commente par erreur ds sopemtx.cc - Reza 3/01/2006

File size: 25.7 KB
Line 
1// C.Magneville, R.Ansari 03/2000
2
3#include "sopnamsp.h"
4#include "machdefs.h"
5#include <stdio.h>
6#include <iostream>
7#include <complex>
8#include <math.h>
9#include "sopemtx.h"
10#include "smathconst.h"
11
12////////////////////////////////////////////////////////////////
13// ---------------------------------------------------------- //
14// La classe de gestion des lignes et colonnes d'une matrice //
15// ---------------------------------------------------------- //
16////////////////////////////////////////////////////////////////
17
18/*!
19 \class SOPHYA::TMatrixRC
20 \ingroup TArray
21 Class for representing rows, columns and diagonal of a matrix.
22 \sa TMatrix TArray
23 */
24
25//! Class of line, column or diagonal of a TMatrix
26/*!
27 A TMatrixRC represents a line, a column or the diagonal of a TMatrix
28 */
29namespace SOPHYA {
30template <class T>
31class TMatrixRC {
32public:
33 //! Define type of TMatrixRC
34 enum TRCKind {
35 TmatrixRow=0, //!< TMatrixRC ligne
36 TmatrixCol=1, //!< TMatrixRC column
37 TmatrixDiag=2 //!< TMatrixRC diagonal
38 };
39 TMatrixRC();
40 TMatrixRC(TMatrix<T>& m, TRCKind kind, uint_4 index=0);
41 virtual ~TMatrixRC() {}
42
43 // Acces aux rangees et colonnes de matrices
44 static TMatrixRC<T> Row(TMatrix<T> & m, uint_4 r);
45 static TMatrixRC<T> Col(TMatrix<T> & m, uint_4 c);
46 static TMatrixRC<T> Diag(TMatrix<T> & m);
47
48 // ---- A virer $CHECK$ Reza 03/2000
49 // int_4 Next();
50 // int_4 Prev();
51 int_4 SetCol(int_4 c);
52 int_4 SetRow(int_4 r);
53 int_4 SetDiag();
54
55 static uint_4 Step(const TMatrix<T>& m, TRCKind rckind);
56 static T* Org(const TMatrix<T>&, TRCKind rckind, uint_4 ind=0);
57
58 //! Return the kind of TMatrix (line,column,diagonal)
59 TRCKind Kind() const { return kind; }
60 uint_4 NElts() const;
61 T& operator()(uint_4 i);
62 T operator()(uint_4 i) const;
63
64
65 TMatrixRC<T>& operator = (const TMatrixRC<T>& rc);
66
67// ---- A virer $CHECK$ Reza 03/2000
68// TVector<T> GetVect() const;
69// TMatrixRC<T>& operator += (const TMatrixRC<T>& rc);
70// TMatrixRC<T>& operator -= (const TMatrixRC<T>& rc);
71
72 TMatrixRC<T>& operator *= (T x);
73 TMatrixRC<T>& operator /= (T x);
74
75// TMatrixRC<T>& operator -= (T x);
76// TMatrixRC<T>& operator += (T x);
77
78
79 TMatrixRC<T>& LinComb(T a, T b, const TMatrixRC& rc, uint_4 first=0);
80 TMatrixRC<T>& LinComb(T b, const TMatrixRC<T>& rc, uint_4 first=0);
81
82 uint_4 IMaxAbs(uint_4 first=0);
83 void Print(ostream & os) const ;
84
85 static void Swap(TMatrixRC<T>& rc1, TMatrixRC<T>& rc2);
86
87 //! Define Absolute value for uint_1
88 inline static double Abs_Value(uint_1 v) {return (double) v;}
89 //! Define Absolute value for uint_2
90 inline static double Abs_Value(uint_2 v) {return (double) v;}
91 //! Define Absolute value for int_2
92 inline static double Abs_Value(int_2 v) {return (v>0)? (double) v: (double) -v;}
93 //! Define Absolute value for int_4
94 inline static double Abs_Value(int_4 v) {return (v>0)? (double) v: (double) -v;}
95 //! Define Absolute value for int_8
96 inline static double Abs_Value(int_8 v) {return (v>0)? (double) v: (double) -v;}
97 //! Define Absolute value for uint_4
98 inline static double Abs_Value(uint_4 v) {return (double) v;}
99 //! Define Absolute value for uint_8
100 inline static double Abs_Value(uint_8 v) {return (double) v;}
101 //! Define Absolute value for r_4
102 inline static double Abs_Value(r_4 v) {return (double) fabs((double)v);}
103 //! Define Absolute value for r_8
104 inline static double Abs_Value(r_8 v) {return fabs(v);}
105 //! Define Absolute value for complex r_4
106 inline static double Abs_Value(complex<r_4> v)
107 {return sqrt(v.real()*v.real()+v.imag()*v.imag());}
108 //! Define Absolute value for complex r_8
109 inline static double Abs_Value(complex<r_8> v)
110 {return sqrt(v.real()*v.real()+v.imag()*v.imag());}
111
112protected:
113 TMatrix<T>* matrix; //!< pointer to the TMatrix
114 T* data; //!< pointer to the beginnig of interesting datas
115 int_4 index; //!< index of the line/column
116 uint_4 step; //!< step of the line/column
117 TRCKind kind; //!< type: line, column or diagonal
118};
119
120//! Scalar product of two TMatrixRC
121/*!
122 \return sum[ a(i) * b(i) ]
123 */
124template <class T>
125inline T operator * (const TMatrixRC<T>& a, const TMatrixRC<T>& b)
126 {
127 if ( a.NElts() != b.NElts() )
128 throw(SzMismatchError("TMatrixRC::operator * size mismatch\n"));
129 if ( a.Kind() != b.Kind() )
130 throw(SzMismatchError("TMatrixRC::operator * type mismatch\n"));
131 T sum = 0;
132 for(uint_4 i=0; i<a.NElts(); i++) sum += a(i)*b(i);
133 return sum;
134 }
135
136//! Get the step in datas for a TMatrix for type rckind
137/*!
138 \param rckind : line, column or diagonal
139 \return step in TMatrix along TMatrixRC
140 */
141template <class T>
142inline uint_4 TMatrixRC<T>::Step(const TMatrix<T>& m, TRCKind rckind)
143 { switch (rckind) { case TmatrixRow : return m.Step(m.ColsKA());
144 case TmatrixCol : return m.Step(m.RowsKA());
145 case TmatrixDiag : return (m.Step(m.RowsKA())+m.Step(m.ColsKA())); }
146 return 0; }
147
148//! Get the origin of datas.
149/*!
150 Get the origin of datas in the TMatrix for a TMatrixRC for type
151 \b rckind and index \b index .
152 \param rckind : line, column or diagonal
153 \param index : index of the line or column.
154 \return adress of the first element in datas.
155 */
156template <class T>
157inline T* TMatrixRC<T>::Org(const TMatrix<T>& m, TRCKind rckind, uint_4 index)
158 { switch (rckind) { case TmatrixRow : return const_cast<T *>(&(m(index,0)));
159 case TmatrixCol : return const_cast<T *>(&(m(0,index)));
160 case TmatrixDiag : return const_cast<T *>(m.Data()); }
161 return NULL; }
162
163//! return number of elements for a TMatrixRC
164template <class T> inline uint_4 TMatrixRC<T>::NElts() const
165 { if (!matrix) return 0;
166 switch (kind) { case TmatrixRow : return matrix->NCols();
167 case TmatrixCol : return matrix->NRows();
168 case TmatrixDiag : return matrix->NCols(); }
169 return 0; }
170
171//! access of element \b i
172template <class T>
173inline T& TMatrixRC<T>::operator()(uint_4 i) {return data[i*step];}
174//! access of element \b i
175template <class T>
176inline T TMatrixRC<T>::operator()(uint_4 i) const {return data[i*step];}
177
178////////////////////////////////////////////////////////////////
179//! Typedef to simplify TMatrixRC<r_8> writing
180typedef TMatrixRC<r_8> MatrixRC;
181
182
183//! Default constructor
184template <class T> TMatrixRC<T>::TMatrixRC()
185: matrix(NULL), data(NULL), index(0), step(0)
186{}
187
188//! Constructor
189/*!
190 \param m : matrix
191 \param rckind : select line, column or diagonal
192 \param ind : number of the line or column
193*/
194template <class T> TMatrixRC<T>::TMatrixRC(TMatrix<T>& m,TRCKind rckind,uint_4 ind)
195: matrix(&m), data(Org(m,rckind,ind)),
196 index(ind), step(Step(m,rckind)), kind(rckind)
197{
198if (kind == TmatrixDiag && m.NCols() != m.NRows())
199 throw(SzMismatchError("TMatrixRC::TMatrixRC(...,TmatrixDiag,...) size mismatch\n"));
200}
201
202////////////////////////////////////////////////////////////////
203// Acces aux rangees et colonnes de matrices
204
205//! Return TMatrixRC for line \b r of matrix \b m
206template <class T>
207TMatrixRC<T> TMatrixRC<T>::Row(TMatrix<T> & m, uint_4 r)
208{
209TMatrixRC<T> rc(m, TmatrixRow, r);
210return rc;
211}
212
213//! Return TMatrixRC for column \b r of matrix \b m
214template <class T>
215TMatrixRC<T> TMatrixRC<T>::Col(TMatrix<T> & m, uint_4 c)
216{
217TMatrixRC<T> rc(m, TmatrixCol, c);
218return rc;
219}
220
221//! Return TMatrixRC for diagonal of matrix \b m
222template <class T>
223TMatrixRC<T> TMatrixRC<T>::Diag(TMatrix<T> & m)
224{
225TMatrixRC<T> rc(m, TmatrixDiag);
226return rc;
227}
228
229
230// ---- A virer $CHECK$ Reza 03/2000
231// template <class T> int_4 TMatrixRC<T>::Next()
232// {
233// if (!matrix || kind==TmatrixDiag) return -1;
234// index++;
235// if(kind == TmatrixRow) {
236// if(index > (int_4)matrix->NRows()) {index = (int_4)matrix->NRows(); return -1;}
237// data += matrix->NCols();
238// } else {
239// if (index > (int_4)matrix->NCols()) {index = (int_4)matrix->NCols(); return -1;}
240// data++;
241// }
242// return index;
243// }
244
245// template <class T> int_4 TMatrixRC<T>::Prev()
246// {
247// if (!matrix || kind == TmatrixDiag) return -1;
248// index--;
249// if(index < 0) {index = 0; return -1;}
250// if(kind == TmatrixRow) data -= matrix->NCols();
251// else data--;
252// return index;
253// }
254
255//! Set column \b c for this TMatrixRC
256template <class T> int_4 TMatrixRC<T>::SetCol(int_4 c)
257{
258if(!matrix) return -1;
259if(c<0 || c>(int_4)matrix->NCols()) return -1;
260kind = TmatrixCol;
261index = c;
262step = Step(*matrix, TmatrixCol);
263data = Org(*matrix, TmatrixCol, c);
264return c;
265}
266
267//! Set line \b r for this TMatrixRC
268template <class T> int_4 TMatrixRC<T>::SetRow(int_4 r)
269{
270if(!matrix) return -1;
271if(r<0 && r>(int_4)matrix->NRows()) return -1;
272kind = TmatrixRow;
273index = r;
274step = Step(*matrix, TmatrixRow);
275data = Org(*matrix, TmatrixRow, r);
276return r;
277}
278
279//! Set line diaginal for this TMatrixRC
280template <class T> int_4 TMatrixRC<T>::SetDiag()
281{
282if (!matrix) return -1;
283if (matrix->NCols() != matrix->NRows())
284 throw(SzMismatchError("TMatrixRC::SetDiag size mismatch\n"));
285kind = TmatrixDiag;
286index = 0;
287step = Step(*matrix, TmatrixDiag);
288data = Org(*matrix, TmatrixDiag);
289return 0;
290}
291
292//! Operator =
293template <class T> TMatrixRC<T>& TMatrixRC<T>::operator = (const TMatrixRC<T>& rc)
294{
295matrix = rc.matrix;
296data = rc.data;
297index = rc.index;
298step = rc.step;
299kind = rc.kind;
300return *this;
301}
302
303// ---- A virer $CHECK$ Reza 03/2000
304// template <class T> TVector<T> TMatrixRC<T>::GetVect() const
305// {
306// TVector<T> v(NElts());
307// for (uint_4 i=0; i<NElts(); i++) v(i) = (*this)(i);
308// return v;
309// }
310
311// template <class T> TMatrixRC<T>& TMatrixRC<T>::operator += (const TMatrixRC<T>& rc)
312// {
313// if ( NElts() != rc.NElts() )
314// throw(SzMismatchError("TMatrixRC::operator+= size mismatch\n"));
315// if ( kind != rc.kind )
316// throw(SzMismatchError("TMatrixRC::operator+= type mismatch\n"));
317// for (uint_4 i=0; i<NElts(); i++) (*this)(i) += rc(i);
318// return *this;
319// }
320
321// template <class T> TMatrixRC<T>& TMatrixRC<T>::operator -= (const TMatrixRC<T>& rc)
322// {
323// if( NElts() != rc.NElts() )
324// throw(SzMismatchError("TMatrixRC::operator-= size mismatch\n"));
325// if( kind != rc.kind )
326// throw(SzMismatchError("TMatrixRC::operator-= type mismatch\n"));
327// for(uint_4 i=0; i<NElts(); i++) (*this)(i) -= rc(i);
328// return *this;
329// }
330
331//! Operator to multiply by constant \b x
332template <class T> TMatrixRC<T>& TMatrixRC<T>::operator *= (T x)
333{
334for(uint_4 i=0; i<NElts(); i++) (*this)(i) *= x;
335return *this;
336}
337
338//! Operator to divide by constant \b x
339template <class T> TMatrixRC<T>& TMatrixRC<T>::operator /= (T x)
340{
341for(uint_4 i=0; i<NElts(); i++) (*this)(i) /= x;
342return *this;
343}
344
345
346// ---- A virer $CHECK$ Reza 03/2000
347// template <class T> TMatrixRC<T>& TMatrixRC<T>::operator -= (T x)
348// {
349// for(uint_4 i=0; i<NElts(); i++) (*this)(i) -= x;
350// return *this;
351// }
352
353// template <class T> TMatrixRC<T>& TMatrixRC<T>::operator += (T x)
354// {
355// for(uint_4 i=0; i<NElts(); i++) (*this)(i) += x;
356// return *this;
357// }
358
359//! Linear combination
360/*!
361 Do : \f$ MRC(i) = MRC(i)*a + rc(i)*b \f$
362 \return *this
363 */
364template <class T>
365TMatrixRC<T>& TMatrixRC<T>::LinComb(T a, T b, const TMatrixRC<T>& rc, uint_4 first)
366{
367if ( NElts() != rc.NElts() )
368 throw(SzMismatchError("TMatrixRC::LinComb size mismatch\n"));
369if ( kind != rc.kind )
370 throw(SzMismatchError("TMatrixRC::LinComb type mismatch\n"));
371for(uint_4 i=first; i<NElts(); i++) (*this)(i) = (*this)(i)*a + rc(i)*b;
372return *this;
373}
374
375//! Linear combination
376/*!
377 Do : \f$ MRC(i) = MRC(i) + rc(i)*b \f$
378 */
379template <class T>
380TMatrixRC<T>& TMatrixRC<T>::LinComb(T b, const TMatrixRC<T>& rc, uint_4 first)
381{
382if ( NElts() != rc.NElts() )
383 throw(SzMismatchError("TMatrixRC::LinComb size mismatch\n"));
384if ( kind != rc.kind )
385 throw(SzMismatchError("TMatrixRC::LinComb type mismatch\n"));
386for(uint_4 i=first; i<NElts(); i++) (*this)(i) += rc(i)*b;
387return *this;
388}
389
390//! Find maximum absolute value in TMatrixRC, search begin at \b first
391template <class T> uint_4 TMatrixRC<T>::IMaxAbs(uint_4 first)
392{
393if (first>NElts())
394 throw(SzMismatchError("TMatrixRC::IMaxAbs size mismatch\n"));
395uint_4 imax = first;
396double vmax = Abs_Value((*this)(first));
397for(uint_4 i=first+1; i<NElts(); i++) {
398 double v = Abs_Value((*this)(i));
399 if(v > vmax) {vmax = v; imax = i;}
400}
401return imax;
402}
403
404//! Print on stream \b os
405template <class T>
406void TMatrixRC<T>::Print(ostream & os) const
407{
408 os << " TMatrixRC<T>::Print(ostream & os) " << NElts() << " Kind="
409 << kind << " Index=" << index << " Step= " << step << endl;
410 for(uint_4 i=0; i<NElts(); i++) {
411 os << (*this)(i);
412 if (kind == TmatrixCol) os << endl;
413 else os << ", ";
414 }
415 os << endl;
416}
417
418//! Swap two TMatrixRC of the same kind
419template <class T>
420void TMatrixRC<T>::Swap(TMatrixRC<T>& rc1, TMatrixRC<T>& rc2)
421{
422if(rc1.NElts() != rc2.NElts())
423 throw(SzMismatchError("TMatrixRC::Swap size mismatch\n"));
424if(rc1.kind != rc2.kind)
425 throw(SzMismatchError("TMatrixRC::Swap type mismatch\n"));
426if(rc1.data == rc2.data) return;
427for(uint_4 i=0; i<rc1.NElts(); i++)
428 {T tmp = rc1(i); rc1(i) = rc2(i); rc2(i) = tmp;}
429}
430
431
432////////////////////////////////////////////////////////////////
433// ---------------------------------------------------------- //
434// La classe de calcul simple sur les TMatrix //
435// ---------------------------------------------------------- //
436////////////////////////////////////////////////////////////////
437
438//**** Pour inversion
439#ifndef M_LN2
440#define M_LN2 0.69314718055994530942
441#endif
442//// CMV BUG BUG : sur OSF 5.0 DMINEXP est deconnant (~1.e+19 !!!)
443#ifndef LN_MINDOUBLE
444#define LN_MINDOUBLE (M_LN2 * (DMINEXP - 1))
445#endif
446#ifndef LN_MAXDOUBLE
447#define LN_MAXDOUBLE (M_LN2 * DMAXEXP)
448#endif
449
450template <class T>
451int SimpleMatrixOperation<T>::GausPivScaling = PIV_GLOB_SCALE;
452
453//! Gaussian pivoting
454/*!
455 Do Gauss pivoting of \b a, doing the same operations on matrix \b b
456 \param computedet = true : return determimant of \b a (beware of over/underfloat)
457 \param computedet = false : return 1 if OK, 0 if not.
458 \verbatim
459 Solve linear system A(n,n) * X(n,m) = B(n,m)
460 and put solution X in B for return.
461 \endverbatim
462 \warning If \b b is identity matrix, return inverse of \b a
463 \warning matrix \b a and \b b are modified.
464 */
465template <class T>
466T SimpleMatrixOperation<T>::GausPiv(TMatrix<T>& a, TMatrix<T>& b,bool computedet)
467// Pivot de Gauss
468// * Attention: egcs impose que cette fonction soit mise dans le .cc
469// avant ::Inverse() (car Inverse() l'utilise)
470// {TMatrix A(a); TMatrix B(b); return (T) TMatrix::GausPiv(A,B);}
471{
472uint_4 n = a.NRows();
473if(n!=b.NRows())
474 throw(SzMismatchError("TMatrix::GausPiv size mismatch\n"));
475
476T det = 1;
477
478//////////////////
479// Data scaling //
480//////////////////
481
482// Pas de normalisation des donnees
483if(GausPivScaling==PIV_NO_SCALE) {
484 if(computedet) det = (T) 1;
485// normalisation des donnees ligne par ligne
486} else if(GausPivScaling==PIV_ROW_SCALE) {
487 double nrm = 0.;
488 for(uint_4 iii=0; iii<a.NRows(); iii++) {
489 uint_4 jjj; double vmax = -1.;
490 for(jjj=0; jjj<a.NCols(); jjj++) {
491 double v = TMatrixRC<T>::Abs_Value(a(iii,jjj));
492 if(v>vmax) vmax = v;
493 }
494 if(vmax>0.) {
495 for(jjj=0; jjj<a.NCols(); jjj++) a(iii,jjj) /= (T) vmax;
496 for(jjj=0; jjj<b.NCols(); jjj++) b(iii,jjj) /= (T) vmax;
497 nrm += log(vmax);
498 } else return (T) 0;
499 }
500 if(computedet) {
501 if(nrm <= LN_MINDOUBLE || nrm >= LN_MAXDOUBLE) {
502 cerr<<"GausPiv_Row: normalisation failure, "
503 <<"determinant has to be multiplied by exp("<<nrm<<")"<<endl;
504 } else det = (T) exp(nrm);
505 }
506// On fait une normalisation un peu brutale globale
507} else {
508 double vmin=MAXDOUBLE, vmax=0;
509 for(uint_4 iii=0; iii<a.NRows(); iii++)
510 for(uint_4 jjj=0; jjj<a.NCols(); jjj++) {
511 double v = TMatrixRC<T>::Abs_Value(a(iii,jjj));
512 if(v>vmax) vmax = v;
513 if(v<vmin && v>0.) vmin = v;
514 }
515 double nrm = sqrt(vmin*vmax);
516 if(nrm>0.) { a /= (T) nrm; b /= (T) nrm;} else return (T) 0;
517 if(computedet) {
518 nrm = a.NRows() * log(nrm);
519 if (nrm <= LN_MINDOUBLE || nrm >= LN_MAXDOUBLE) {
520 cerr<<"GausPiv_Glob: normalisation failure, "
521 <<"determinant has to be multiplied by exp("<<nrm<<")"<<endl;
522 } else det = (T) exp(nrm);
523 }
524}
525
526////////////////////////////////////////
527// Gaussian elimination with pivoting //
528////////////////////////////////////////
529
530TMatrixRC<T> pivRowa(a,TMatrixRC<T>::TmatrixRow);
531TMatrixRC<T> pivRowb(b,TMatrixRC<T>::TmatrixRow);
532
533for(uint_4 k=0; k<n-1; k++) {
534 uint_4 iPiv = TMatrixRC<T>::Col(a, k).IMaxAbs(k);
535 if(iPiv != k) {
536 TMatrixRC<T> aIPiv(TMatrixRC<T>::Row(a,iPiv));
537 TMatrixRC<T> aK(TMatrixRC<T>::Row(a, k));
538 TMatrixRC<T>::Swap(aIPiv,aK);
539 TMatrixRC<T> bIPiv(TMatrixRC<T>::Row(b, iPiv));
540 TMatrixRC<T> bK(TMatrixRC<T>::Row(b, k));
541 TMatrixRC<T>::Swap(bIPiv,bK);
542 }
543 T pivot = a(k,k);
544 if( TMatrixRC<T>::Abs_Value(pivot) < 1.e-50 ) return (T) 0;
545 if(computedet) det *= pivot;
546 pivRowa.SetRow(k); // to avoid constructors
547 pivRowb.SetRow(k);
548 for (uint_4 i=k+1; i<n; i++) {
549 T r = -a(i,k)/pivot;
550 TMatrixRC<T>::Row(a, i).LinComb(r, pivRowa); // + rapide que -= r * pivRowa
551 TMatrixRC<T>::Row(b, i).LinComb(r, pivRowb);
552 }
553}
554if(computedet) det *= a(n-1, n-1);
555
556// on remonte
557for(uint_4 kk=n-1; kk>0; kk--) {
558 T pivot = a(kk,kk);
559 if( TMatrixRC<T>::Abs_Value(pivot) <= 1.e-50 ) return (T) 0;
560 pivRowa.SetRow(kk); // to avoid constructors
561 pivRowb.SetRow(kk);
562 for(uint_4 jj=0; jj<kk; jj++) {
563 T r = -a(jj,kk)/pivot;
564 TMatrixRC<T>::Row(a, jj).LinComb(r, pivRowa);
565 TMatrixRC<T>::Row(b, jj).LinComb(r, pivRowb);
566 }
567}
568
569for(uint_4 l=0; l<n; l++) {
570 if( TMatrixRC<T>::Abs_Value(a(l,l)) <= 1.e-50 ) return (T) 0;
571 TMatrixRC<T>::Row(b, l) /= a(l,l);
572}
573
574return det;
575}
576
577//! Return the inverse matrix of \b A
578template <class T>
579TMatrix<T> SimpleMatrixOperation<T>::Inverse(TMatrix<T> const & A)
580{
581TMatrix<T> a(A,false);
582TMatrix<T> b(a.NCols(),a.NRows()); b = IdentityMatrix(1.);
583if(GausPiv(a,b)==(T) 0)
584 throw(MathExc("TMatrix Inverse() Singular Matrix"));
585return b;
586}
587
588
589////////////////////////////////////////////////////////////////
590// ---------------------------------------------------------- //
591// La classe fit lineaire //
592// ---------------------------------------------------------- //
593////////////////////////////////////////////////////////////////
594
595//! Creator
596template <class T>
597LinFitter<T>::LinFitter()
598{
599}
600
601//! Destructor
602template <class T>
603LinFitter<T>::~LinFitter()
604{
605}
606
607// fit lineaire des y(k) en tant que somme de c(i)f(i,x(k)), i=0..nf-1;
608//! Linear fitting
609/*!
610 Linear fit of y(k) as the sum of \f$ c(i)f(i,x(k)), i=0..nf-1 \f$
611 \param x : vector of X values
612 \param y : vector of datas
613 \param nf: number of functions
614 \param f : f(i,x(k)), i=0..nf-1
615 \return c : vector of solutions
616 \return return chisquare
617 */
618template <class T>
619r_8 LinFitter<T>::LinFit(const TVector<T>& x, const TVector<T>& y,
620 uint_4 nf, T (*f)(uint_4,T), TVector<T>& c)
621{
622uint_4 n = x.NElts();
623if (n != y.NElts())
624 throw SzMismatchError("LinFitter<T>::LinFit(x,y,nf,f,c)/Error x.NElts() <> y.Nelts() ");
625
626TMatrix<T> fx(nf,n);
627
628for(uint_4 i=0; i<nf; i++)
629 for(uint_4 j=0; j<n; j++) fx(i,j) = f(i,x(j));
630
631return LinFit(fx,y,c);
632}
633
634// fit lineaire des y(k) en tant que somme de c(i)f(i,x(k)), i=0..nf-1,
635// la matrice fx contient les valeurs des f: fx(i,j) = f(i, x(j)).
636//! Linear fitting
637/*!
638 Linear fit of y(k) as the sum of \f$ c(i)f(i,x(k)), i=0..nf-1 \f$.
639 \param fx : matrix which contains \f$ fx(i,j) = f(i, x(j)) \f$.
640 \param y : vector of datas
641 \return c : vector of solutions
642 \return return chisquare
643 */
644template <class T>
645r_8 LinFitter<T>::LinFit(const TMatrix<T>& fx, const TVector<T>& y, TVector<T>& c)
646{
647uint_4 n = y.NElts();
648if (n != fx.NCol())
649 throw SzMismatchError("LinFitter<T>::LinFit(fx, y, c)/Error y.NElts() <> fx.Nelts() ");
650
651uint_4 nf = fx.NRows();
652
653TMatrix<T> a(nf,nf);
654
655for(uint_4 j=0; j<nf; j++)
656 for(uint_4 k=j; k<nf; k++)
657 a(j,k) = a(k,j) = TMatrixRC<T>::Row(const_cast<TMatrix<T> &>(fx), j)
658 * TMatrixRC<T>::Row(const_cast<TMatrix<T> &>(fx), k);
659
660c = fx * y;
661
662if(SimpleMatrixOperation<T>::GausPiv(a,c)==(T) 0)
663 throw SingMatrixExc("LinFitter<T>::LinFit(fx, y, c) - Non invertible matrix (by GausPiv())");
664
665r_8 xi2 = 0., ax;
666T x;
667for(uint_4 k=0; k<n; k++) {
668 x = (T) 0;
669 for(uint_4 i=0; i<nf; i++) x += c(i) * fx(i,k);
670 x -= y(k);
671 ax = TMatrixRC<T>::Abs_Value(x);
672 xi2 += ax*ax;
673}
674return xi2;
675}
676
677// fit lineaire des y(k) en tant que somme de c(i)f(i,x(k)), i=0..nf-1,
678// errY2 contient les carres des erreurs sur les Y.
679// au retour, errC contient les erreurs sur les coefs.
680//! Linear fitting with errors
681/*!
682 Linear fit with errors of y(k) as the sum of \f$ c(i)f(i,x(k)), i=0..nf-1 \f$.
683 \param x : vector of X values
684 \param y : vector of datas
685 \param errY2 : vector of errors square on Y
686 \param nf: number of functions
687 \param f : f(i,x(k)), i=0..nf-1
688 \return c : vector of solutions
689 \return errC : vector of errors on solutions C
690 \return return chisquare
691 */
692template <class T>
693r_8 LinFitter<T>::LinFit(const TVector<T>& x, const TVector<T>& y,
694 const TVector<T>& errY2, uint_4 nf, T (*f)(uint_4,T),
695 TVector<T>& c, TVector<T>& errC)
696{
697uint_4 n = x.NElts();
698if (n != y.NElts())
699 throw SzMismatchError("LinFitter<T>::LinFit(x,y,errY2,nf,f,c,errC)/Error x.NElts() <> y.Nelts() ");
700
701TMatrix<T> fx(nf, n);
702for(uint_4 i=0; i<nf; i++)
703 for(uint_4 j=0; j<n; j++)
704 fx(i,j) = f(i,x(j));
705
706return LinFit(fx,y,errY2,c,errC);
707}
708
709// fit lineaire des y(k) en tant que somme de c(i)f(i,x(k)), i=0..nf-1,
710// la matrice fx contient les valeurs des f:
711// fx(i,j) = f(i, x(j)).
712// errY2 contient les carres des erreurs sur les Y.
713// au retour, errC contient les erreurs sur les coefs.
714//! Linear fitting with errors
715/*!
716 \param fx : matrix which contains \f$ fx(i,j) = f(i, x(j)) \f$.
717 \param y : vector of datas
718 \param errY2 : vector of errors square on Y
719 \return c : vector of solutions
720 \return errC : vector of errors on solutions on C
721 \return return chisquare
722 */
723template <class T>
724r_8 LinFitter<T>::LinFit(const TMatrix<T>& fx, const TVector<T>& y,
725 const TVector<T>& errY2,TVector<T>& c, TVector<T>& errC)
726{
727uint_4 n = y.NElts();
728if( n != errY2.NElts())
729 throw SzMismatchError("LinFitter<T>::LinFit(fx,y,errY2,c,errC)/Error y.NElts() <> errY2.Nelts() ");
730if( n != fx.NCol())
731 throw SzMismatchError("LinFitter<T>::LinFit(fx,y,errY2,c,errC)/Error y.NElts() <> fx.NCols() ");
732uint_4 nf = fx.NRows();
733
734TMatrix<T> a(nf,nf);
735
736c.Realloc(nf);
737errC.Realloc(nf);
738
739for(uint_4 j=0; j<nf; j++)
740 for(uint_4 k=j; k<nf; k++) {
741 T x=0;
742 // Matrice a inverser
743 for(uint_4 l=0; l<n; l++) x += fx(j,l)*fx(k,l)/errY2(l);
744 a(j,k) = a(k,j) = x;
745 }
746
747TMatrix<T> d(nf,nf+1);
748for(uint_4 k=0; k<nf; k++) {
749 T x = (T) 0;
750 // Second membre 1ere colonne
751 for(uint_4 l=0; l<n; l++) x += fx(k,l)*y(l)/errY2(l);
752 d(k,0) = x;
753 // Reste second membre = Id.
754 for(uint_4 m=1; m<=nf; m++) d(k,m) = (k==m)?1:0;
755}
756
757if(SimpleMatrixOperation<T>::GausPiv(a,d)==(T) 0)
758 throw SingMatrixExc("LinFitter<T>::LinFit(...ErrY2...) - Non invertible matrix (by GausPiv())");
759
760
761for(uint_4 l=0; l<nf; l++) {
762 c(l) = d(l,0); // Parametres = 1ere colonne
763 errC(l) = d(l,l+1); // Erreurs = diag inverse.
764}
765
766r_8 xi2 = 0., ax;
767T x;
768for(uint_4 jj=0; jj<n; jj++) {
769 x = (T) 0;
770 for(uint_4 ii=0; ii<nf; ii++) x += c(ii) * fx(ii,jj);
771 x -= y(jj);
772 ax = TMatrixRC<T>::Abs_Value(x);
773 xi2 += ax*ax/TMatrixRC<T>::Abs_Value(errY2(jj));
774 }
775 return xi2;
776}
777
778} // Fin namespace SOPHYA
779///////////////////////////////////////////////////////////////////////////
780///////////////////////////////////////////////////////////////////////////
781///////////////////////////////////////////////////////////////////////////
782///////////////////////////////////////////////////////////////////////////
783
784void _ZZ_TestTMatrixRC_YY_(TMatrix<r_8> & m)
785{
786 cout << " ::::: _ZZ_TestTMatrixRC_YY_ :::: M= \n" << m << endl;
787 TMatrixRC<r_8> l0 = TMatrixRC<r_8>::Row(m,0);
788 cout << "TMatrixRC<r_8>::Row(m,0) = \n" ;
789 l0.Print(cout);
790 TMatrixRC<r_8> l1 = TMatrixRC<r_8>::Row(m,1);
791 cout << "TMatrixRC<r_8>::Row(m,1) = \n" ;
792 l1.Print(cout);
793 l0.SetRow(2);
794 cout << "TMatrixRC<r_8>::l0.SetRow(2 = \n" ;
795 l0.Print(cout);
796
797 TMatrixRC<r_8> c0 = TMatrixRC<r_8>::Col(m,0);
798 cout << "TMatrixRC<r_8>::Col(m,0) = \n" ;
799 c0.Print(cout);
800 TMatrixRC<r_8> c1 = TMatrixRC<r_8>::Col(m,1);
801 cout << "TMatrixRC<r_8>::Col(m,1) = \n" ;
802 c1.Print(cout);
803 c0.SetCol(2);
804 cout << "TMatrixRC<r_8>::c0.SetCol(2) = \n" ;
805 c0.Print(cout);
806 TMatrixRC<r_8> c00 = TMatrixRC<r_8>::Col(m,0);
807 TMatrixRC<r_8>::Swap(c0, c00);
808 cout << " ::::: M Apres Swap = \n" << m << endl;
809
810}
811
812///////////////////////////////////////////////////////////////
813#ifdef __CXX_PRAGMA_TEMPLATES__
814// Instances gestion lignes/colonnes
815#pragma define_template TMatrixRC<int_4>
816#pragma define_template TMatrixRC<r_4>
817#pragma define_template TMatrixRC<r_8>
818#pragma define_template TMatrixRC< complex<r_4> >
819#pragma define_template TMatrixRC< complex<r_8> >
820#pragma define_template SimpleMatrixOperation<int_4>
821#pragma define_template SimpleMatrixOperation<r_4>
822#pragma define_template SimpleMatrixOperation<r_8>
823#pragma define_template SimpleMatrixOperation< complex<r_4> >
824#pragma define_template SimpleMatrixOperation< complex<r_8> >
825#pragma define_template LinFitter<r_4>
826#pragma define_template LinFitter<r_8>
827#pragma define_template LinFitter< complex<r_4> >
828#pragma define_template LinFitter< complex<r_8> >
829#endif
830
831#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
832namespace SOPHYA {
833// Instances gestion lignes/colonnes
834template class TMatrixRC<int_4>;
835template class TMatrixRC<r_4>;
836template class TMatrixRC<r_8>;
837template class TMatrixRC< complex<r_4> >;
838template class TMatrixRC< complex<r_8> >;
839template class SimpleMatrixOperation<int_4>;
840template class SimpleMatrixOperation<r_4>;
841template class SimpleMatrixOperation<r_8>;
842template class SimpleMatrixOperation< complex<r_4> >;
843template class SimpleMatrixOperation< complex<r_8> >;
844template class LinFitter<r_4>;
845template class LinFitter<r_8>;
846template class LinFitter< complex<r_4> >;
847template class LinFitter< complex<r_8> >;
848}
849#endif
850
Note: See TracBrowser for help on using the repository browser.