comparison maths/Polyfit.h @ 421:f4bb0bce64d4

Avoid using a namespace (confuses docs)
author Chris Cannam <c.cannam@qmul.ac.uk>
date Wed, 07 Oct 2015 12:05:44 +0100
parents 31f22daeba64
children 7b7691b49197
comparison
equal deleted inserted replaced
420:914ad0e15491 421:f4bb0bce64d4
78 vector<vector<int> > &index); 78 vector<vector<int> > &index);
79 }; 79 };
80 80
81 // some utility functions 81 // some utility functions
82 82
83 namespace NSUtility 83 struct NSUtility
84 { 84 {
85 inline void swap(double &a, double &b) {double t = a; a = b; b = t;} 85 static void swap(double &a, double &b) {double t = a; a = b; b = t;}
86 void zeroise(vector<double> &array, int n); 86 // fills a vector with zeros.
87 void zeroise(vector<int> &array, int n); 87 static void zeroise(vector<double> &array, int n) {
88 void zeroise(vector<vector<double> > &matrix, int m, int n); 88 array.clear();
89 void zeroise(vector<vector<int> > &matrix, int m, int n); 89 for(int j = 0; j < n; ++j) array.push_back(0);
90 inline double sqr(const double &x) {return x * x;} 90 }
91 // fills a vector with zeros.
92 static void zeroise(vector<int> &array, int n) {
93 array.clear();
94 for(int j = 0; j < n; ++j) array.push_back(0);
95 }
96 // fills a (m by n) matrix with zeros.
97 static void zeroise(vector<vector<double> > &matrix, int m, int n) {
98 vector<double> zero;
99 zeroise(zero, n);
100 matrix.clear();
101 for(int j = 0; j < m; ++j) matrix.push_back(zero);
102 }
103 // fills a (m by n) matrix with zeros.
104 static void zeroise(vector<vector<int> > &matrix, int m, int n) {
105 vector<int> zero;
106 zeroise(zero, n);
107 matrix.clear();
108 for(int j = 0; j < m; ++j) matrix.push_back(zero);
109 }
110 static double sqr(const double &x) {return x * x;}
91 }; 111 };
92
93 //---------------------------------------------------------------------------
94 // Implementation
95 //---------------------------------------------------------------------------
96 using namespace NSUtility;
97 //------------------------------------------------------------------------------------------
98 112
99 113
100 // main PolyFit routine 114 // main PolyFit routine
101 115
102 double TPolyFit::PolyFit2 (const vector<double> &x, 116 double TPolyFit::PolyFit2 (const vector<double> &x,
111 Matrix a; 125 Matrix a;
112 vector<double> g; // Constant vector 126 vector<double> g; // Constant vector
113 const int npoints(x.size()); 127 const int npoints(x.size());
114 const int nterms(coefs.size()); 128 const int nterms(coefs.size());
115 double correl_coef; 129 double correl_coef;
116 zeroise(g, nterms); 130 NSUtility::zeroise(g, nterms);
117 zeroise(a, nterms, nterms); 131 NSUtility::zeroise(a, nterms, nterms);
118 zeroise(xmatr, npoints, nterms); 132 NSUtility::zeroise(xmatr, npoints, nterms);
119 if (nterms < 1) { 133 if (nterms < 1) {
120 std::cerr << "ERROR: PolyFit called with less than one term" << std::endl; 134 std::cerr << "ERROR: PolyFit called with less than one term" << std::endl;
121 return 0; 135 return 0;
122 } 136 }
123 if(npoints < 2) { 137 if(npoints < 2) {
146 { 160 {
147 yi = y[i]; 161 yi = y[i];
148 yc = 0.0; 162 yc = 0.0;
149 for(j = 0; j < nterms; ++j) 163 for(j = 0; j < nterms; ++j)
150 yc += coefs [j] * xmatr [i][j]; 164 yc += coefs [j] * xmatr [i][j];
151 srs += sqr (yc - yi); 165 srs += NSUtility::sqr (yc - yi);
152 sum_y += yi; 166 sum_y += yi;
153 sum_y2 += yi * yi; 167 sum_y2 += yi * yi;
154 } 168 }
155 169
156 // If all Y values are the same, avoid dividing by zero 170 // If all Y values are the same, avoid dividing by zero
157 correl_coef = sum_y2 - sqr (sum_y) / npoints; 171 correl_coef = sum_y2 - NSUtility::sqr (sum_y) / npoints;
158 // Either return 0 or the correct value of correlation coefficient 172 // Either return 0 or the correct value of correlation coefficient
159 if (correl_coef != 0) 173 if (correl_coef != 0)
160 correl_coef = srs / correl_coef; 174 correl_coef = srs / correl_coef;
161 if (correl_coef >= 1) 175 if (correl_coef >= 1)
162 correl_coef = 0.0; 176 correl_coef = 0.0;
227 int ncol(b.size()); 241 int ncol(b.size());
228 int irow, icol; 242 int irow, icol;
229 vector<vector<int> >index; 243 vector<vector<int> >index;
230 Matrix w; 244 Matrix w;
231 245
232 zeroise(w, ncol, ncol); 246 NSUtility::zeroise(w, ncol, ncol);
233 zeroise(index, ncol, 3); 247 NSUtility::zeroise(index, ncol, 3);
234 248
235 if(!GaussJordan2(b, y, w, index)) 249 if(!GaussJordan2(b, y, w, index))
236 return false; 250 return false;
237 251
238 // Interchange columns 252 // Interchange columns
353 } 367 }
354 } 368 }
355 } // { i-loop } 369 } // { i-loop }
356 return true; 370 return true;
357 } 371 }
358 //----------------------------------------------------------------------------------------------
359
360 //------------------------------------------------------------------------------------
361
362 // Utility functions
363 //--------------------------------------------------------------------------
364
365 // fills a vector with zeros.
366 void NSUtility::zeroise(vector<double> &array, int n)
367 {
368 array.clear();
369 for(int j = 0; j < n; ++j)
370 array.push_back(0);
371 }
372 //--------------------------------------------------------------------------
373
374 // fills a vector with zeros.
375 void NSUtility::zeroise(vector<int> &array, int n)
376 {
377 array.clear();
378 for(int j = 0; j < n; ++j)
379 array.push_back(0);
380 }
381 //--------------------------------------------------------------------------
382
383 // fills a (m by n) matrix with zeros.
384 void NSUtility::zeroise(vector<vector<double> > &matrix, int m, int n)
385 {
386 vector<double> zero;
387 zeroise(zero, n);
388 matrix.clear();
389 for(int j = 0; j < m; ++j)
390 matrix.push_back(zero);
391 }
392 //--------------------------------------------------------------------------
393
394 // fills a (m by n) matrix with zeros.
395 void NSUtility::zeroise(vector<vector<int> > &matrix, int m, int n)
396 {
397 vector<int> zero;
398 zeroise(zero, n);
399 matrix.clear();
400 for(int j = 0; j < m; ++j)
401 matrix.push_back(zero);
402 }
403 //--------------------------------------------------------------------------
404
405 372
406 #endif 373 #endif
407 374