Mercurial > hg > qm-dsp
changeset 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 | 914ad0e15491 |
children | 51bf24c0a57b |
files | maths/Polyfit.h |
diffstat | 1 files changed, 34 insertions(+), 67 deletions(-) [+] |
line wrap: on
line diff
--- a/maths/Polyfit.h Wed Oct 07 11:15:13 2015 +0100 +++ b/maths/Polyfit.h Wed Oct 07 12:05:44 2015 +0100 @@ -80,22 +80,36 @@ // some utility functions -namespace NSUtility +struct NSUtility { - inline void swap(double &a, double &b) {double t = a; a = b; b = t;} - void zeroise(vector<double> &array, int n); - void zeroise(vector<int> &array, int n); - void zeroise(vector<vector<double> > &matrix, int m, int n); - void zeroise(vector<vector<int> > &matrix, int m, int n); - inline double sqr(const double &x) {return x * x;} + static void swap(double &a, double &b) {double t = a; a = b; b = t;} + // fills a vector with zeros. + static void zeroise(vector<double> &array, int n) { + array.clear(); + for(int j = 0; j < n; ++j) array.push_back(0); + } + // fills a vector with zeros. + static void zeroise(vector<int> &array, int n) { + array.clear(); + for(int j = 0; j < n; ++j) array.push_back(0); + } + // fills a (m by n) matrix with zeros. + static void zeroise(vector<vector<double> > &matrix, int m, int n) { + vector<double> zero; + zeroise(zero, n); + matrix.clear(); + for(int j = 0; j < m; ++j) matrix.push_back(zero); + } + // fills a (m by n) matrix with zeros. + static void zeroise(vector<vector<int> > &matrix, int m, int n) { + vector<int> zero; + zeroise(zero, n); + matrix.clear(); + for(int j = 0; j < m; ++j) matrix.push_back(zero); + } + static double sqr(const double &x) {return x * x;} }; -//--------------------------------------------------------------------------- -// Implementation -//--------------------------------------------------------------------------- -using namespace NSUtility; -//------------------------------------------------------------------------------------------ - // main PolyFit routine @@ -113,9 +127,9 @@ const int npoints(x.size()); const int nterms(coefs.size()); double correl_coef; - zeroise(g, nterms); - zeroise(a, nterms, nterms); - zeroise(xmatr, npoints, nterms); + NSUtility::zeroise(g, nterms); + NSUtility::zeroise(a, nterms, nterms); + NSUtility::zeroise(xmatr, npoints, nterms); if (nterms < 1) { std::cerr << "ERROR: PolyFit called with less than one term" << std::endl; return 0; @@ -148,13 +162,13 @@ yc = 0.0; for(j = 0; j < nterms; ++j) yc += coefs [j] * xmatr [i][j]; - srs += sqr (yc - yi); + srs += NSUtility::sqr (yc - yi); sum_y += yi; sum_y2 += yi * yi; } // If all Y values are the same, avoid dividing by zero - correl_coef = sum_y2 - sqr (sum_y) / npoints; + correl_coef = sum_y2 - NSUtility::sqr (sum_y) / npoints; // Either return 0 or the correct value of correlation coefficient if (correl_coef != 0) correl_coef = srs / correl_coef; @@ -229,8 +243,8 @@ vector<vector<int> >index; Matrix w; - zeroise(w, ncol, ncol); - zeroise(index, ncol, 3); + NSUtility::zeroise(w, ncol, ncol); + NSUtility::zeroise(index, ncol, 3); if(!GaussJordan2(b, y, w, index)) return false; @@ -355,53 +369,6 @@ } // { i-loop } return true; } -//---------------------------------------------------------------------------------------------- - -//------------------------------------------------------------------------------------ - -// Utility functions -//-------------------------------------------------------------------------- - -// fills a vector with zeros. -void NSUtility::zeroise(vector<double> &array, int n) -{ - array.clear(); - for(int j = 0; j < n; ++j) - array.push_back(0); -} -//-------------------------------------------------------------------------- - -// fills a vector with zeros. -void NSUtility::zeroise(vector<int> &array, int n) -{ - array.clear(); - for(int j = 0; j < n; ++j) - array.push_back(0); -} -//-------------------------------------------------------------------------- - -// fills a (m by n) matrix with zeros. -void NSUtility::zeroise(vector<vector<double> > &matrix, int m, int n) -{ - vector<double> zero; - zeroise(zero, n); - matrix.clear(); - for(int j = 0; j < m; ++j) - matrix.push_back(zero); -} -//-------------------------------------------------------------------------- - -// fills a (m by n) matrix with zeros. -void NSUtility::zeroise(vector<vector<int> > &matrix, int m, int n) -{ - vector<int> zero; - zeroise(zero, n); - matrix.clear(); - for(int j = 0; j < m; ++j) - matrix.push_back(zero); -} -//-------------------------------------------------------------------------- - #endif