source: Sophya/trunk/SophyaLib/NTools/linfit.cc@ 774

Last change on this file since 774 was 774, checked in by ansari, 26 years ago

Adaptation aux modifs de TMatrixRC<T> - Reza 10/3/2000

File size: 2.8 KB
Line 
1#include "machdefs.h"
2#include "peida.h"
3#include "sopemtx.h"
4#include "linfit.h"
5
6LinFitter::LinFitter()
7{
8}
9
10LinFitter::~LinFitter()
11{
12}
13
14double LinFitter::LinFit(const Vector& x, const Vector& y, int nf,
15 double (*f)(int, double), Vector& c)
16{
17 int n = x.NElts();
18 if (n != y.NElts()) THROW(sizeMismatchErr);
19
20 Matrix fx(nf, n);
21 for (int i=0; i<nf; i++)
22 for (int j=0; j<n; j++)
23 fx(i,j) = f(i,x(j));
24
25 return LinFit(fx,y,c);
26}
27
28
29
30double LinFitter::LinFit(const Matrix& fx, const Vector& y, Vector& c)
31{
32 int n = y.NElts();
33 if ( n != fx.NCol()) THROW(sizeMismatchErr);
34
35 int nf = fx.NRows();
36
37 Matrix a(nf,nf);
38
39 for (int j=0; j<nf; j++)
40 for (int k=j; k<nf; k++)
41 a(j,k) = a(k,j) = TMatrixRC<r_8>::Row(const_cast<Matrix &>(fx), j)
42 * TMatrixRC<r_8>::Row(const_cast<Matrix &>(fx), k); /* $CHECK$ Reza 10/3/2000 */
43
44 c = fx * y;
45
46 if (SimpleMatrixOperation<r_8>::GausPiv(a,c) == 0) THROW(singMatxErr); /* $CHECK$ Reza 10/3/2000 */
47
48 double xi2 = 0;
49
50 for (int k=0; k<n; k++) {
51 double x = 0;
52 for (int i=0; i<nf; i++)
53 x += c(i) * fx(i,k);
54 x -= y(k);
55 xi2 += x*x;
56 }
57 return xi2;
58}
59
60
61
62double LinFitter::LinFit(const Vector& x, const Vector& y, const Vector& errY2, int nf,
63 double (*f)(int, double), Vector& c, Vector& errC)
64{
65 int n = x.NElts();
66 if (n != y.NElts()) THROW(sizeMismatchErr);
67
68 Matrix fx(nf, n);
69 for (int i=0; i<nf; i++)
70 for (int j=0; j<n; j++)
71 fx(i,j) = f(i,x(j));
72
73 return LinFit(fx,y,errY2,c,errC);
74}
75
76
77double LinFitter::LinFit(const Matrix& fx, const Vector& y, const Vector& errY2,
78 Vector& c, Vector& errC)
79{
80 int n = y.NElts();
81 if ( n != errY2.NElts()) THROW(sizeMismatchErr);
82 if ( n != fx.NCol()) THROW(sizeMismatchErr);
83
84 int nf = fx.NRows();
85
86 Matrix a(nf,nf);
87
88 c.Realloc(nf);
89 errC.Realloc(nf);
90
91 for (int j=0; j<nf; j++)
92 for (int k=j; k<nf; k++) {
93 double x=0;
94 for (int l=0; l<n; l++)
95 x += fx(j,l) * fx(k,l) / errY2(l); // Matrice a inverser
96 a(j,k) = a(k,j) = x;
97 }
98
99 Matrix d(nf,nf+1);
100 for (int k=0; k<nf; k++) {
101 double x=0;
102 for (int l=0; l<n; l++)
103 x += fx(k,l) * y(l) / errY2(l); // Second membre 1ere colonne
104 d(k,0) = x;
105 for (int m=1; m<=nf; m++)
106 d(k,m) = (k==m) ? 1 : 0; // Reste second membre = Id.
107 }
108
109 if (SimpleMatrixOperation<r_8>::GausPiv(a,d) == 0) THROW(singMatxErr); /* $CHECK$ Reza 10/3/2000 */
110
111 for (int l=0; l<nf; l++) {
112 c(l) = d(l,0); // Parametres = 1ere colonne
113 errC(l) = d(l,l+1); // Erreurs = diag inverse.
114 }
115
116 double xi2 = 0;
117
118 for (int jj=0; jj<n; jj++) {
119 double x = 0;
120 for (int ii=0; ii<nf; ii++)
121 x += c(ii) * fx(ii,jj);
122 x -= y(jj);
123 xi2 += x*x/errY2(jj);
124 }
125 return xi2;
126}
127
128
Note: See TracBrowser for help on using the repository browser.