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

Last change on this file since 3718 was 3332, checked in by ansari, 18 years ago

1- Methode TArray::SumX2() renommee en ::SumSq()
2- Methode TArray::Norm2() appelle maintenant SumSq() - sauf pour les
tableaux complexes, ou on calcule Sum[el x conj(el)]=Sum[module2]
3- Nettoyage fichier sopemtx.cc (utilisation sa_size_t pour supprimer
les warnings g++

Reza , 02/10/2007

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