Skip to content

Commit

Permalink
Refs #9531. Mapping fit weights in CostFuncFitting.
Browse files Browse the repository at this point in the history
Instead of calling the virtual function getWeights each time fit weights are accessed in LeastSquaresCostFunc, they are mapped once before the calculations.
  • Loading branch information
Michael Wedel committed Jun 2, 2014
1 parent 46747ba commit eb67fc5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
Expand Up @@ -75,7 +75,6 @@ class DLLExport CostFuncLeastSquares : public CostFuncFitting

void setParameters(const GSLVector& params);
void getParameters(GSLVector& params) const;

protected:

virtual void calActiveCovarianceMatrix(GSLMatrix& covar, double epsrel = 1e-8);
Expand All @@ -90,6 +89,9 @@ class DLLExport CostFuncLeastSquares : public CostFuncFitting
API::FunctionValues_sptr values,
bool evalFunction = true, bool evalDeriv = true, bool evalHessian = true) const;

/// Get mapped weights from FunctionValues
std::vector<double> getFitWeights(API::FunctionValues_sptr values, double sqrtW) const;

/// Get weight (1/sigma)
virtual double getWeight(API::FunctionValues_sptr values, size_t i, double sqrtW=1.0) const;

Expand Down
18 changes: 15 additions & 3 deletions Code/Mantid/Framework/CurveFitting/src/CostFuncLeastSquares.cpp
Expand Up @@ -296,6 +296,9 @@ void CostFuncLeastSquares::addValDerivHessian(
}
}
double sqrtw = calSqrtW(values);

std::vector<double> weights = getFitWeights(values, sqrtw);

for(size_t ip = 0; ip < np; ++ip)
{
if ( !function->isActive(ip) ) continue;
Expand All @@ -304,8 +307,7 @@ void CostFuncLeastSquares::addValDerivHessian(
{
double calc = values->getCalculated(i);
double obs = values->getFitData(i);
// double w = values->getFitWeight(i);
double w = getWeight(values, i, sqrtw);
double w = weights[i];
double y = ( calc - obs ) * w;
d += y * jacobian.get(i,ip) * w;
if (iActiveP == 0 && evalFunction)
Expand Down Expand Up @@ -344,7 +346,7 @@ void CostFuncLeastSquares::addValDerivHessian(
for(size_t k = 0; k < ny; ++k) // over fitting data
{
// double w = values->getFitWeight(k);
double w = getWeight(values, k, sqrtw);
double w = weights[k];//getWeight(values, k, sqrtw);
d += jacobian.get(k,i) * jacobian.get(k,j) * w * w;
}
PARALLEL_CRITICAL(hessian_set)
Expand All @@ -363,6 +365,16 @@ void CostFuncLeastSquares::addValDerivHessian(
}
}

std::vector<double> CostFuncLeastSquares::getFitWeights(API::FunctionValues_sptr values, double sqrtW) const
{
std::vector<double> weights(values->size());
for(size_t i = 0; i < weights.size(); ++i) {
weights[i] = getWeight(values, i, sqrtW);
}

return weights;
}

/**
* Return cached or calculate the drivatives.
*/
Expand Down

0 comments on commit eb67fc5

Please sign in to comment.