Skip to content

Commit

Permalink
Refs #9782. Changed implementation of Gaussians to not use pow
Browse files Browse the repository at this point in the history
I was not aware how incredibly slow pow() is for squaring a value.
  • Loading branch information
Michael Wedel committed Aug 13, 2014
1 parent e1440db commit 5e7de3e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
3 changes: 2 additions & 1 deletion Code/Mantid/Framework/CurveFitting/src/GaussianAutoDiff.cpp
Expand Up @@ -24,7 +24,8 @@ void GaussianAutoDiff::function1DAutoDiff(const FunctionDomain1D &domain, std::v
adept::adouble sigma = parameters.getParameter("Sigma");

for(size_t i = 0; i < y.size(); ++i) {
y[i] = height * exp(-0.5 * pow( (domain[i] - centre)/sigma, 2.0) );
adept::adouble term = (domain[i] - centre)/sigma;
y[i] = height * exp(-0.5 * term * term);
}
}

Expand Down
10 changes: 6 additions & 4 deletions Code/Mantid/Framework/CurveFitting/src/GaussianHandCoded.cpp
Expand Up @@ -27,7 +27,8 @@ using namespace API;
double sigma = getParameter("Sigma");

for(size_t i = 0; i < nData; ++i) {
out[i] = height * exp(-0.5 * pow( (xValues[i] - centre)/sigma, 2.0) );
double diffTerm = (xValues[i] - centre)/sigma;
out[i] = height * exp(-0.5 * diffTerm * diffTerm );
}
}

Expand All @@ -39,11 +40,12 @@ using namespace API;

for(size_t i = 0; i < nData; ++i) {
double xDiff = xValues[i] - centre;
double expTerm = exp(-0.5 * pow( (xDiff)/sigma, 2.0));
double term = (xDiff)/sigma;
double expTerm = exp(-0.5 * term * term);

out->set(i, 0, expTerm);
out->set(i, 1, (height*expTerm*(2 * xDiff))/(2*pow(sigma, 2)));
out->set(i, 2, (height*expTerm*pow(xDiff, 2))/pow(sigma, 3));
out->set(i, 1, (height*expTerm*(xDiff))/(sigma * sigma));
out->set(i, 2, (height*expTerm*term * term)/(sigma));
}
}

Expand Down
3 changes: 2 additions & 1 deletion Code/Mantid/Framework/CurveFitting/src/GaussianNumDiff.cpp
Expand Up @@ -27,7 +27,8 @@ namespace CurveFitting
double sigma = getParameter("Sigma");

for(size_t i = 0; i < nData; ++i) {
out[i] = height * exp(-0.5 * pow( (xValues[i] - centre)/sigma, 2.0) );
double term = (xValues[i] - centre)/sigma;
out[i] = height * exp(-0.5 * term * term );
}
}

Expand Down

0 comments on commit 5e7de3e

Please sign in to comment.