|
Packit |
67cb25 |
/* multiroots/fdjac.c
|
|
Packit |
67cb25 |
*
|
|
Packit |
67cb25 |
* Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Brian Gough
|
|
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 |
#include <config.h>
|
|
Packit |
67cb25 |
#include <gsl/gsl_multiroots.h>
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
int
|
|
Packit |
67cb25 |
gsl_multiroot_fdjacobian (gsl_multiroot_function * F,
|
|
Packit |
67cb25 |
const gsl_vector * x, const gsl_vector * f,
|
|
Packit |
67cb25 |
double epsrel, gsl_matrix * jacobian)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
const size_t n = x->size;
|
|
Packit |
67cb25 |
const size_t m = f->size;
|
|
Packit |
67cb25 |
const size_t n1 = jacobian->size1;
|
|
Packit |
67cb25 |
const size_t n2 = jacobian->size2;
|
|
Packit |
67cb25 |
int status = 0;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
if (m != n1 || n != n2)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
GSL_ERROR ("function and jacobian are not conformant", GSL_EBADLEN);
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
size_t i,j;
|
|
Packit |
67cb25 |
gsl_vector *x1, *f1;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
x1 = gsl_vector_alloc (n);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
if (x1 == 0)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
GSL_ERROR ("failed to allocate space for x1 workspace", GSL_ENOMEM);
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
f1 = gsl_vector_alloc (m);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
if (f1 == 0)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
gsl_vector_free (x1);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
GSL_ERROR ("failed to allocate space for f1 workspace", GSL_ENOMEM);
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_vector_memcpy (x1, x); /* copy x into x1 */
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
for (j = 0; j < n; j++)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
double xj = gsl_vector_get (x, j);
|
|
Packit |
67cb25 |
double dx = epsrel * fabs (xj);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
if (dx == 0)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
dx = epsrel;
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_vector_set (x1, j, xj + dx);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
int f_stat = GSL_MULTIROOT_FN_EVAL (F, x1, f1);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
if (f_stat != GSL_SUCCESS)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
status = GSL_EBADFUNC;
|
|
Packit |
67cb25 |
break; /* n.b. avoid memory leak for x1,f1 */
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_vector_set (x1, j, xj);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
for (i = 0; i < m; i++)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
double g1 = gsl_vector_get (f1, i);
|
|
Packit |
67cb25 |
double g0 = gsl_vector_get (f, i);
|
|
Packit |
67cb25 |
gsl_matrix_set (jacobian, i, j, (g1 - g0) / dx);
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
gsl_vector_view col = gsl_matrix_column (jacobian, j);
|
|
Packit |
67cb25 |
int null_col = gsl_vector_isnull (&col.vector);
|
|
Packit |
67cb25 |
/* if column is null, return an error - this may be due to
|
|
Packit |
67cb25 |
dx being too small. Try increasing epsrel */
|
|
Packit |
67cb25 |
if (null_col) {
|
|
Packit |
67cb25 |
status = GSL_ESING;
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_vector_free (x1);
|
|
Packit |
67cb25 |
gsl_vector_free (f1);
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
if (status)
|
|
Packit |
67cb25 |
return status;
|
|
Packit |
67cb25 |
else
|
|
Packit |
67cb25 |
return GSL_SUCCESS;
|
|
Packit |
67cb25 |
}
|