49 typedef vector<vector<double> >
Matrix;
52 static double PolyFit2 (
const vector<double> &x,
53 const vector<double> &y,
54 vector<double> &coef);
64 const vector<double> &y,
67 const int nrow,
const int ncol);
71 const vector<double> &y,
72 vector<double> &coef);
76 const vector<double> &y,
78 vector<vector<int> > &index);
85 static void swap(
double &a,
double &b) {
double t = a; a = b; b = t;}
87 static void zeroise(vector<double> &array,
int n) {
89 for(
int j = 0; j < n; ++j) array.push_back(0);
92 static void zeroise(vector<int> &array,
int n) {
94 for(
int j = 0; j < n; ++j) array.push_back(0);
97 static void zeroise(vector<vector<double> > &matrix,
int m,
int n) {
101 for(
int j = 0; j < m; ++j) matrix.push_back(zero);
104 static void zeroise(vector<vector<int> > &matrix,
int m,
int n) {
108 for(
int j = 0; j < m; ++j) matrix.push_back(zero);
110 static double sqr(
const double &x) {
return x * x;}
117 const vector<double> &y,
118 vector<double> &coefs)
123 double xi, yi, yc, srs, sum_y, sum_y2;
127 const int npoints(x.size());
128 const int nterms(coefs.size());
134 std::cerr <<
"ERROR: PolyFit called with less than one term" << std::endl;
138 std::cerr <<
"ERROR: PolyFit called with less than two points" << std::endl;
141 if(npoints != (
int)y.size()) {
142 std::cerr <<
"ERROR: PolyFit called with x and y of unequal size" << std::endl;
145 for(i = 0; i < npoints; ++i)
150 for(j = 1; j < nterms; ++j)
151 xmatr[i][j] = xmatr [i][j - 1] * xi;
153 Square (xmatr, y, a, g, npoints, nterms);
159 for(i = 0; i < npoints; ++i)
163 for(j = 0; j < nterms; ++j)
164 yc += coefs [j] * xmatr [i][j];
173 if (correl_coef != 0)
174 correl_coef = srs / correl_coef;
175 if (correl_coef >= 1)
178 correl_coef = sqrt (1.0 - correl_coef);
192 const vector<double> &y,
199 for(k = 0; k < ncol; ++k)
201 for(l = 0; l < k + 1; ++l)
204 for(i = 0; i < nrow; ++i)
206 a[k][l] += x[i][l] * x [i][k];
212 for(i = 0; i < nrow; ++i)
213 g[k] += y[i] * x[i][k];
220 const vector<double> &y,
221 vector<double> &coef)
243 vector<vector<int> >index;
254 for (
int i = 0; i < ncol; ++i)
257 if(index [m][0] != index [m][1])
261 for(
int k = 0; k < ncol; ++k)
262 swap (b[k][irow], b[k][icol]);
266 for(
int k = 0; k < ncol; ++k)
268 if(index [k][2] != 0)
270 std::cerr <<
"ERROR: Error in PolyFit::GaussJordan: matrix is singular" << std::endl;
275 for(
int i = 0; i < ncol; ++i)
285 const vector<double> &y,
287 vector<vector<int> > &index)
295 int irow = 0, icol = 0;
298 for(
int i = 0; i < ncol; ++i)
304 for(
int i = 0; i < ncol; ++i)
308 for(
int j = 0; j < ncol; ++j)
312 for(
int k = 0; k < ncol; ++k)
314 if(index[k][2] > 0) {
315 std::cerr <<
"ERROR: Error in PolyFit::GaussJordan2: matrix is singular" << std::endl;
319 if(index[k][2] < 0 && fabs(b[j][k]) > big)
328 index [icol][2] = index [icol][2] + 1;
337 for(
int m = 0; m < ncol; ++m)
338 swap (b [irow][m], b[icol][m]);
340 for (
int m = 0; m < nv; ++m)
341 swap (w[irow][m], w[icol][m]);
345 pivot = b[icol][icol];
349 for(
int m = 0; m < ncol; ++m)
352 for(
int m = 0; m < nv; ++m)
356 for(
int n = 0; n < ncol; ++n)
362 for(
int m = 0; m < ncol; ++m)
363 b[n][m] -= b[icol][m] * t;
365 for(
int m = 0; m < nv; ++m)
366 w[n][m] -= w[icol][m] * t;
TPolyFit(const TPolyFit &)
vector< vector< double > > Matrix
static bool GaussJordan(Matrix &b, const vector< double > &y, vector< double > &coef)
static void Square(const Matrix &x, const vector< double > &y, Matrix &a, vector< double > &g, const int nrow, const int ncol)
static double PolyFit2(const vector< double > &x, const vector< double > &y, vector< double > &coef)
static bool GaussJordan2(Matrix &b, const vector< double > &y, Matrix &w, vector< vector< int > > &index)
TPolyFit & operator=(const TPolyFit &)
static void zeroise(vector< double > &array, int n)
static void swap(double &a, double &b)
static void zeroise(vector< int > &array, int n)
static void zeroise(vector< vector< double > > &matrix, int m, int n)
static void zeroise(vector< vector< int > > &matrix, int m, int n)
static double sqr(const double &x)