22int gsl_f(
const gsl_vector *
x,
void *params, gsl_vector *f) {
24 auto *p =
reinterpret_cast<struct
GSL_FitData *
>(params);
28 for (
size_t i = 0; i < p->function->nParams(); ++i) {
29 if (p->function->isActive(i)) {
31 p->
function->setActiveParameter(i,
x->data[ia]);
40 p->function->applyTies();
42 auto values = std::dynamic_pointer_cast<API::FunctionValues>(p->costFunction->getValues());
44 throw std::invalid_argument(
"FunctionValues expected");
46 p->function->function(*p->costFunction->getDomain(), *values);
50 for (
size_t i = 0; i < p->function->nParams(); ++i) {
57 size_t n = values->size() - 1;
60 values->addToCalculated(0, penalty);
61 values->addToCalculated(
n, penalty);
63 for (
size_t i = 9; i <
n; i += 10) {
64 values->addToCalculated(i, penalty);
71 for (
size_t i = 0; i < p->n; i++) {
72 f->data[i] = (values->getCalculated(i) - values->getFitData(i)) * values->getFitWeight(i);
84int gsl_df(
const gsl_vector *
x,
void *params, gsl_matrix *J) {
86 auto *p =
reinterpret_cast<struct
GSL_FitData *
>(params);
87 gsl_matrix *J_tr = gsl_matrix_calloc(J->size2, J->size1);
88 gsl_matrix_transpose_memcpy(J_tr, J);
95 for (
size_t i = 0; i < p->function->nParams(); ++i) {
96 if (p->function->isActive(i)) {
97 p->function->setActiveParameter(i,
x->data[ia]);
102 p->function->applyTies();
105 p->function->functionDeriv(*p->costFunction->getDomain(), p->J);
109 size_t n = p->costFunction->getValues()->size() - 1;
111 for (
size_t i = 0; i < p->function->nParams(); ++i) {
112 if (!p->function->isActive(i))
117 if (penalty != 0.0) {
118 double deriv = p->J.get(0, ia);
119 p->J.set(0, ia, deriv + penalty);
120 deriv = p->J.get(
n, ia);
121 p->J.set(
n, ia, deriv + penalty);
123 for (
size_t j = 9; j <
n; j += 10) {
124 deriv = p->J.get(j, ia);
125 p->J.set(j, ia, deriv + penalty);
135 auto values = std::dynamic_pointer_cast<API::FunctionValues>(p->costFunction->getValues());
137 throw std::invalid_argument(
"FunctionValues expected");
141 std::copy(&m_tr.
mutator().data()[0], &m_tr.
mutator().data()[J_tr->size1 * J_tr->size2], &J->data[0]);
143 for (
size_t iY = 0; iY < p->n; iY++)
144 for (
size_t iP = 0; iP < p->p; iP++) {
145 J->data[iY * p->p + iP] *= values->getFitWeight(iY);
158int gsl_fdf(
const gsl_vector *
x,
void *params, gsl_vector *f, gsl_matrix *J) {
169 : function(cf->getFittingFunction()), costFunction(cf) {
170 gsl_set_error_handler_off();
173 for (
size_t i = 0; i <
function->nParams(); ++i) {
179 n = cf->getValues()->size();
181 bool functionFixed =
false;
184 functionFixed =
true;
195 for (
size_t i = 0; i <
function->nParams(); ++i) {
204 for (
size_t i = 0; i <
function->nParams(); ++i) {
206 J.m_index.emplace_back(j);
209 J.m_index.emplace_back(-1);
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.
map_type & mutator()
Get the map to Eigen matrix.
Exception thrown when a fitting function changes number of parameters during fit.
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)
~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_FitData(const std::shared_ptr< CostFunctions::CostFuncLeastSquares > &cf)
Constructor.
gsl_vector * initFuncParams
Initial function parameters.