|
Packit |
67cb25 |
/* multifit_nlinear/fdjac.c
|
|
Packit |
67cb25 |
*
|
|
Packit |
67cb25 |
* Copyright (C) 2013, 2016 Patrick Alken
|
|
Packit |
67cb25 |
*
|
|
Packit |
67cb25 |
* This program is free software; you can redistribute it and/or modify
|
|
Packit |
67cb25 |
* it under the terms of the GNU General Public License as published by
|
|
Packit |
67cb25 |
* the Free Software Foundation; either version 3 of the License, or (at
|
|
Packit |
67cb25 |
* your option) any later version.
|
|
Packit |
67cb25 |
*
|
|
Packit |
67cb25 |
* This program is distributed in the hope that it will be useful, but
|
|
Packit |
67cb25 |
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
67cb25 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
67cb25 |
* General Public License for more details.
|
|
Packit |
67cb25 |
*
|
|
Packit |
67cb25 |
* You should have received a copy of the GNU General Public License
|
|
Packit |
67cb25 |
* along with this program; if not, write to the Free Software
|
|
Packit |
67cb25 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
Packit |
67cb25 |
*
|
|
Packit |
67cb25 |
*
|
|
Packit |
67cb25 |
* This module contains routines for approximating the Jacobian with
|
|
Packit |
67cb25 |
* finite differences for nonlinear least-squares fitting.
|
|
Packit |
67cb25 |
*/
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
#include <config.h>
|
|
Packit |
67cb25 |
#include <gsl/gsl_math.h>
|
|
Packit |
67cb25 |
#include <gsl/gsl_multifit_nlinear.h>
|
|
Packit |
67cb25 |
#include <gsl/gsl_vector.h>
|
|
Packit |
67cb25 |
#include <gsl/gsl_matrix.h>
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
static int forward_jac(const double h, const gsl_vector *x,
|
|
Packit |
67cb25 |
const gsl_vector *wts,
|
|
Packit |
67cb25 |
gsl_multifit_nlinear_fdf *fdf,
|
|
Packit |
67cb25 |
const gsl_vector *f, gsl_matrix *J);
|
|
Packit |
67cb25 |
static int center_jac(const double h, const gsl_vector *x, const gsl_vector *wts,
|
|
Packit |
67cb25 |
gsl_multifit_nlinear_fdf *fdf, gsl_matrix *J, gsl_vector *work);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/*
|
|
Packit |
67cb25 |
forward_jac()
|
|
Packit |
67cb25 |
Compute approximate Jacobian using forward differences
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Inputs: h - finite difference step size
|
|
Packit |
67cb25 |
x - parameter vector
|
|
Packit |
67cb25 |
wts - data weights
|
|
Packit |
67cb25 |
fdf - fdf struct
|
|
Packit |
67cb25 |
f - (input) vector of function values f_i(x)
|
|
Packit |
67cb25 |
J - (output) Jacobian matrix
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Return: success or error
|
|
Packit |
67cb25 |
*/
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
static int
|
|
Packit |
67cb25 |
forward_jac(const double h, const gsl_vector *x, const gsl_vector *wts,
|
|
Packit |
67cb25 |
gsl_multifit_nlinear_fdf *fdf, const gsl_vector *f, gsl_matrix *J)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
int status = 0;
|
|
Packit |
67cb25 |
size_t i, j;
|
|
Packit |
67cb25 |
double delta;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
for (j = 0; j < fdf->p; ++j)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
double xj = gsl_vector_get(x, j);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* use column j of J as temporary storage for f(x + dx) */
|
|
Packit |
67cb25 |
gsl_vector_view v = gsl_matrix_column(J, j);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
delta = h * fabs(xj);
|
|
Packit |
67cb25 |
if (delta == 0.0)
|
|
Packit |
67cb25 |
delta = h;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* perturb x_j to compute forward difference */
|
|
Packit |
67cb25 |
gsl_vector_set((gsl_vector *) x, j, xj + delta);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
status += gsl_multifit_nlinear_eval_f (fdf, x, wts, &v.vector);
|
|
Packit |
67cb25 |
if (status)
|
|
Packit |
67cb25 |
return status;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* restore x_j */
|
|
Packit |
67cb25 |
gsl_vector_set((gsl_vector *) x, j, xj);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
delta = 1.0 / delta;
|
|
Packit |
67cb25 |
for (i = 0; i < fdf->n; ++i)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
double fnext = gsl_vector_get(&v.vector, i);
|
|
Packit |
67cb25 |
double fi = gsl_vector_get(f, i);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_matrix_set(J, i, j, (fnext - fi) * delta);
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
return status;
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/*
|
|
Packit |
67cb25 |
center_jac()
|
|
Packit |
67cb25 |
Compute approximate Jacobian using centered differences
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Inputs: h - finite difference step size
|
|
Packit |
67cb25 |
x - parameter vector
|
|
Packit |
67cb25 |
wts - data weights
|
|
Packit |
67cb25 |
fdf - fdf struct
|
|
Packit |
67cb25 |
J - (output) Jacobian matrix
|
|
Packit |
67cb25 |
work - additional workspace, size n
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Return: success or error
|
|
Packit |
67cb25 |
*/
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
static int
|
|
Packit |
67cb25 |
center_jac(const double h, const gsl_vector *x, const gsl_vector *wts,
|
|
Packit |
67cb25 |
gsl_multifit_nlinear_fdf *fdf, gsl_matrix *J, gsl_vector *work)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
int status = 0;
|
|
Packit |
67cb25 |
size_t i, j;
|
|
Packit |
67cb25 |
double delta;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
for (j = 0; j < fdf->p; ++j)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
double xj = gsl_vector_get(x, j);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* use column j of J as temporary storage for f(x + dx) */
|
|
Packit |
67cb25 |
gsl_vector_view v = gsl_matrix_column(J, j);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
delta = h * fabs(xj);
|
|
Packit |
67cb25 |
if (delta == 0.0)
|
|
Packit |
67cb25 |
delta = h;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* perturb x_j to compute forward difference, f(x + 1/2 delta e_j) */
|
|
Packit |
67cb25 |
gsl_vector_set((gsl_vector *) x, j, xj + 0.5 * delta);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
status += gsl_multifit_nlinear_eval_f (fdf, x, wts, &v.vector);
|
|
Packit |
67cb25 |
if (status)
|
|
Packit |
67cb25 |
return status;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* perturb x_j to compute backward difference, f(x - 1/2 delta e_j) */
|
|
Packit |
67cb25 |
gsl_vector_set((gsl_vector *) x, j, xj - 0.5 * delta);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
status += gsl_multifit_nlinear_eval_f (fdf, x, wts, work);
|
|
Packit |
67cb25 |
if (status)
|
|
Packit |
67cb25 |
return status;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* restore x_j */
|
|
Packit |
67cb25 |
gsl_vector_set((gsl_vector *) x, j, xj);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
delta = 1.0 / delta;
|
|
Packit |
67cb25 |
for (i = 0; i < fdf->n; ++i)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
double fnext = gsl_vector_get(&v.vector, i);
|
|
Packit |
67cb25 |
double fprev = gsl_vector_get(work, i);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_matrix_set(J, i, j, (fnext - fprev) * delta);
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
return status;
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/*
|
|
Packit |
67cb25 |
gsl_multifit_nlinear_df()
|
|
Packit |
67cb25 |
Compute approximate Jacobian using finite differences
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Inputs: h - finite difference step size
|
|
Packit |
67cb25 |
fdtype - finite difference method
|
|
Packit |
67cb25 |
x - parameter vector
|
|
Packit |
67cb25 |
wts - data weights (set to NULL if not needed)
|
|
Packit |
67cb25 |
fdf - fdf
|
|
Packit |
67cb25 |
f - (input) function values f_i(x)
|
|
Packit |
67cb25 |
J - (output) approximate (weighted) Jacobian matrix, sqrt(W) * J
|
|
Packit |
67cb25 |
work - additional workspace for centered differences, size n
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Return: success or error
|
|
Packit |
67cb25 |
*/
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
int
|
|
Packit |
67cb25 |
gsl_multifit_nlinear_df(const double h, const gsl_multifit_nlinear_fdtype fdtype,
|
|
Packit |
67cb25 |
const gsl_vector *x, const gsl_vector *wts,
|
|
Packit |
67cb25 |
gsl_multifit_nlinear_fdf *fdf,
|
|
Packit |
67cb25 |
const gsl_vector *f, gsl_matrix *J, gsl_vector *work)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
int status;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
if (fdtype == GSL_MULTIFIT_NLINEAR_FWDIFF)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
status = forward_jac(h, x, wts, fdf, f, J);
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
else if (fdtype == GSL_MULTIFIT_NLINEAR_CTRDIFF)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
status = center_jac(h, x, wts, fdf, J, work);
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
else
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
GSL_ERROR("invalid specified fdtype", GSL_EINVAL);
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
return status;
|
|
Packit |
67cb25 |
}
|