19double leastSquaresLoss(
const std::shared_ptr<API::FunctionValues> &values,
const size_t index) {
20 return (values->getCalculated(
index) - values->getFitData(
index)) * values->getFitWeight(
index);
23double poissonLoss(
const std::shared_ptr<API::FunctionValues> &values,
const size_t index) {
27double leastSquaresWeight(
const std::shared_ptr<API::FunctionValues> &values,
const size_t index) {
28 return values->getFitWeight(
index);
31double poissonWeight(
const std::shared_ptr<API::FunctionValues> &values,
const size_t index) {
33 values->getCalculated(
index));
43int gsl_f(
const gsl_vector *
x,
void *params, gsl_vector *f) {
45 auto *p =
reinterpret_cast<struct
GSL_FitData *
>(params);
49 for (
size_t i = 0; i < p->function->nParams(); ++i) {
50 if (p->function->isActive(i)) {
52 p->
function->setActiveParameter(i,
x->data[ia]);
61 p->function->applyTies();
63 auto values = std::dynamic_pointer_cast<API::FunctionValues>(p->costFunction->getValues());
65 throw std::invalid_argument(
"FunctionValues expected");
67 p->function->function(*p->costFunction->getDomain(), *values);
71 for (
size_t i = 0; i < p->function->nParams(); ++i) {
78 size_t n = values->size() - 1;
81 values->addToCalculated(0, penalty);
82 values->addToCalculated(
n, penalty);
84 for (
size_t i = 9; i <
n; i += 10) {
85 values->addToCalculated(i, penalty);
92 for (
size_t i = 0; i < p->n; i++) {
93 f->data[i] = p->m_loss(values, i);
105int gsl_df(
const gsl_vector *
x,
void *params, gsl_matrix *J) {
107 auto *p =
reinterpret_cast<struct
GSL_FitData *
>(params);
108 std::unique_ptr<gsl_matrix,
decltype(&gsl_matrix_free)> J_tr(gsl_matrix_calloc(J->size2, J->size1), gsl_matrix_free);
109 gsl_matrix_transpose_memcpy(J_tr.get(), J);
116 for (
size_t i = 0; i < p->function->nParams(); ++i) {
117 if (p->function->isActive(i)) {
118 p->function->setActiveParameter(i,
x->data[ia]);
123 p->function->applyTies();
126 p->function->functionDeriv(*p->costFunction->getDomain(), p->J);
130 size_t n = p->costFunction->getValues()->size() - 1;
132 for (
size_t i = 0; i < p->function->nParams(); ++i) {
133 if (!p->function->isActive(i))
138 if (penalty != 0.0) {
139 double deriv = p->J.get(0, ia);
140 p->J.set(0, ia, deriv + penalty);
141 deriv = p->J.get(
n, ia);
142 p->J.set(
n, ia, deriv + penalty);
144 for (
size_t j = 9; j <
n; j += 10) {
145 deriv = p->J.get(j, ia);
146 p->J.set(j, ia, deriv + penalty);
156 auto values = std::dynamic_pointer_cast<API::FunctionValues>(p->costFunction->getValues());
158 throw std::invalid_argument(
"FunctionValues expected");
162 std::copy(&m_tr.
mutator().data()[0], &m_tr.
mutator().data()[J_tr->size1 * J_tr->size2], &J->data[0]);
163 for (
size_t iY = 0; iY < p->n; iY++) {
164 const double weight = p->m_scaleFactor(values, iY);
165 for (
size_t iP = 0; iP < p->p; iP++) {
166 J->data[iY * p->p + iP] *= weight;
180int gsl_fdf(
const gsl_vector *
x,
void *params, gsl_vector *f, gsl_matrix *J) {
191 : function(cf->getFittingFunction()), costFunction(cf) {
192 gsl_set_error_handler_off();
194 if (std::dynamic_pointer_cast<CostFunctions::CostFuncPoisson>(cf)) {
195 this->
m_loss = &poissonLoss;
198 this->
m_loss = &leastSquaresLoss;
204 for (
size_t i = 0; i <
function->nParams(); ++i) {
210 n = cf->getValues()->size();
212 bool functionFixed =
false;
215 functionFixed =
true;
226 for (
size_t i = 0; i <
function->nParams(); ++i) {
235 for (
size_t i = 0; i <
function->nParams(); ++i) {
237 J.m_index.emplace_back(j);
240 J.m_index.emplace_back(-1);
std::map< DeltaEMode::Type, std::string > index
An interface to a constraint.
virtual double checkDeriv()=0
Returns the derivative of the penalty for each active parameter.
virtual double checkDeriv2()=0
Returns the derivative of the penalty for each active parameter.
A wrapper around Eigen::Matrix.
EigenMatrix tr() const
Calculate the eigensystem of a symmetric matrix.
map_type & mutator()
Get the map to Eigen matrix.
Exception thrown when a fitting function changes number of parameters during fit.
double MANTID_CURVEFITTING_DLL calculateJacobianScaleFactor(double observedCounts, double predicted)
double MANTID_CURVEFITTING_DLL calculatePoissonLossLM(double observedCounts, double predicted)
int gsl_fdf(const gsl_vector *x, void *params, gsl_vector *f, gsl_matrix *J)
Fit derivatives and function GSL wrapper.
int gsl_f(const gsl_vector *x, void *params, gsl_vector *f)
Fit GSL function wrapper.
int gsl_df(const gsl_vector *x, void *params, gsl_matrix *J)
Fit GSL derivative function wrapper.
Various GSL specific functions used GSL specific minimizers.
size_t n
number of points to be fitted (size of X, Y and sqrtWeightData arrays)
double(* m_loss)(const std::shared_ptr< API::FunctionValues > &values, size_t index)
GSL_FitData(const std::shared_ptr< CostFunctions::CostFuncFitting > &cf)
Constructor.
double(* m_scaleFactor)(const std::shared_ptr< API::FunctionValues > &values, size_t index)
~GSL_FitData()
Destructor.
API::IFunction_sptr function
Pointer to the function.
size_t p
number of (active) fit parameters
JacobianImpl1< EigenMatrix > J
Jacobi matrix interface.
gsl_vector * initFuncParams
Initial function parameters.