| Bsoft 2.0.5 Bernard's software package

matrix_linear.cpp
Go to the documentation of this file.
1
9 #include "Matrix.h"
10 #include "utilities.h"
11
12 // Declaration of global variables
13 extern int verbose; // Level of output to the screen
14
28 double linear_least_squares(int n1, int n2, double *x, double *y, double& a, double& b)
29 {
30 int i, n;
31 double sx, sx2, sy, sxy, sd, dy, d, denom;
32
33 // Initial values in case the function returns prematurely
34 a = 0;
35 b = 1;
36
37 if ( verbose & VERB_DEBUG )
38 cout << "DEBUG linear_least_squares: Starting fit" << endl;
39
40 if ( n2 < n1 ) {
41 n = n1;
42 n1 = n2;
43 n2 = n;
44 }
45 if ( n1 < 0 ) n1 = 0;
46 n = n2 - n1 + 1;
47
48 sx = 0;
49 sx2 = 0;
50 sy = 0;
51 sxy = 0;
52 for ( i=n1; i<=n2; i++ ) {
53 sx += x[i];
54 sx2 += x[i]*x[i];
55 sy += y[i];
56 sxy += x[i]*y[i];
57 }
58
59 denom = n*sx2 - sx*sx;
60 if ( fabs(denom) < 1e-30 ) return 0;
61
62 a = (sx2*sy-sx*sxy)/denom;
63 b = (n*sxy-sx*sy)/denom;
64
65 sy = sy/n;
66 sd = 0;
67 dy = 0;
68 for ( i=n1; i<=n2; i++ ) {
69 d = y[i] - sy;
70 sd += d*d;
71 d = a + b*x[i] - y[i];
72 dy += d*d;
73 }
74
75 if ( verbose & VERB_DEBUG )
76 cout << "DEBUG linear_least_squares: a = " << a << " b = " << b << endl;
77
78 return 1-dy/sd;
79 }
80
99 double fit_polynomial(int n, vector<double>& x, vector<double>& y, int order, vector<double>& coeff)
100 {
101 int i, j, k;
102 int nt(order + 1);
103 Matrix a(nt,nt);
104 vector<double> b(nt);
105 vector<double> v(nt);
106 double f, df, R(0);
107
108 if ( verbose & VERB_DEBUG )
109 cout << "DEBUG fit_polynomial: n=" << n << " order=" << order << endl;
110
111 for ( i=0; i<nt; i++ ) b[i] = v[i] = 0;
112
113 v[0] = 1;
114 for ( i=0; i<n; i++ ) {
115 for ( j=1; j<nt; j++ ) v[j] = v[j-1]*x[i];
116 for ( j=0; j<nt; j++ ) b[j] += v[j]*y[i];
117 for ( j=0; j<nt; j++ )
118 for ( k=0; k<=j; k++ ) a[j][k] += v[j]*v[k];
119 }
120 for ( j=0; j<nt-1; j++ )
121 for ( k=j+1; k<nt; k++ ) a[j][k] = a[k][j];
122
123 a.LU_decomposition(b);
124 // a.singular_value_decomposition(b);
125
126 for ( i=0, R=0; i<n; i++ ) {
127 for ( j=1; j<nt; j++ ) v[j] = v[j-1]*x[i];
128 for ( j=0, f=0; j<nt; j++ ) f += v[j]*b[j];
129 df = f - y[i];
130 R += df*df;
131 if ( verbose & VERB_DEBUG )
132 cout << x[i] << " " << y[i] << " " << f << " " << df << endl;
133 }
134
135 R = sqrt(R/n);
136
137 if ( coeff.size() >= nt ) for ( i=0; i<nt; i++ ) coeff[i] = b[i];
138
139 if ( verbose & VERB_FULL ) {
140 cout << "Polynomial: f(x) = " << b[0];
141 for ( i=1; i<nt; i++ ) cout << " + " << b[i] << " x^" << i;
142 cout << endl << "R = " << R << endl << endl;
143 }
144
145 return R;
146 }
147
166 double fit_polynomial(int n, double* x, double* y, int order, double* coeff)
167 {
168 int i, j, k;
169 int nt = order + 1;
170 Matrix a(nt,nt);
171 vector<double> b(nt);
172 vector<double> v(nt);
173 double f, df, R=0;
174
175 if ( verbose & VERB_DEBUG )
176 cout << "DEBUG fit_polynomial: n=" << n << " order=" << order << endl;
177
178 for ( i=0; i<nt; i++ ) b[i] = v[i] = 0;
179
180 v[0] = 1;
181 for ( i=0; i<n; i++ ) {
182 for ( j=1; j<nt; j++ ) v[j] = v[j-1]*x[i];
183 for ( j=0; j<nt; j++ ) b[j] += v[j]*y[i];
184 for ( j=0; j<nt; j++ )
185 for ( k=0; k<=j; k++ ) a[j][k] += v[j]*v[k];
186 }
187 for ( j=0; j<nt-1; j++ )
188 for ( k=j+1; k<nt; k++ ) a[j][k] = a[k][j];
189
190 a.LU_decomposition(b);
191 // a.singular_value_decomposition(b);
192
193 for ( i=0, R=0; i<n; i++ ) {
194 for ( j=1; j<nt; j++ ) v[j] = v[j-1]*x[i];
195 for ( j=0, f=0; j<nt; j++ ) f += v[j]*b[j];
196 df = f - y[i];
197 R += df*df;
198 if ( verbose & VERB_DEBUG )
199 cout << x[i] << " " << y[i] << " " << f << " " << df << endl;
200 }
201
202 R = sqrt(R/n);
203
204 if ( coeff ) for ( i=0; i<nt; i++ ) coeff[i] = b[i];
205
206 if ( verbose & VERB_FULL ) {
207 cout << "Polynomial: f(x) = " << b[0];
208 for ( i=1; i<nt; i++ ) cout << " + " << b[i] << " x^" << i;
209 cout << endl << "R = " << R << endl << endl;
210 }
211
212 return R;
213 }
214
215
227 Vector3<double> fit_plane(Matrix a, vector<double> b)
228 {
229 Vector3<double> normal;
230
231 if ( a[0][0] == 0 ) {
232 normal = Vector3<double>(1, 0, 0);
233 } else if ( a[1][1] == 0 ) {
234 normal = Vector3<double>(0, 1, 0);
235 } else if ( a[2][2] == 0 ) {
236 normal = Vector3<double>(0, 0, 1);
237 } else {
238 a.LU_decomposition(b);
239 normal = Vector3<double>(b[0], b[1], b[2]);
240 normal.normalize();
241 }
242
243 return normal;
244 }
Vector3::normalize
double normalize()
Definition: Vector3.h:236
verbose
int verbose
utilities.h
Header file for general utilities.
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:112
Matrix.h
Generalized matrix class.
Matrix::LU_decomposition
double LU_decomposition()
Matrix inversion by LU decomposition.
Definition: matrix.cpp:78
linear_least_squares
double linear_least_squares(int n1, int n2, double *x, double *y, double &a, double &b)
Does a linear least squares fit between two vectors.
Definition: matrix_linear.cpp:28
fit_plane
Vector3< double > fit_plane(Matrix a, vector< double > b)
Solves for fitting a plane through a model.
Definition: matrix_linear.cpp:227
Matrix
Definition: Matrix.h:21
Vector3< double >
fit_polynomial
double fit_polynomial(int n, vector< double > &x, vector< double > &y, int order, vector< double > &coeff)
Fits a data set to a polynomial function.
Definition: matrix_linear.cpp:99
VERB_FULL
#define VERB_FULL
Definition: utilities.h:109