|
Packit |
67cb25 |
/* multiroots/newton.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 |
|
|
Packit |
67cb25 |
#include <stddef.h>
|
|
Packit |
67cb25 |
#include <stdlib.h>
|
|
Packit |
67cb25 |
#include <stdio.h>
|
|
Packit |
67cb25 |
#include <math.h>
|
|
Packit |
67cb25 |
#include <float.h>
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
#include <gsl/gsl_math.h>
|
|
Packit |
67cb25 |
#include <gsl/gsl_errno.h>
|
|
Packit |
67cb25 |
#include <gsl/gsl_multiroots.h>
|
|
Packit |
67cb25 |
#include <gsl/gsl_linalg.h>
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
typedef struct
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
gsl_matrix * lu;
|
|
Packit |
67cb25 |
gsl_permutation * permutation;
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
newton_state_t;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
static int newton_alloc (void * vstate, size_t n);
|
|
Packit |
67cb25 |
static int newton_set (void * vstate, gsl_multiroot_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx);
|
|
Packit |
67cb25 |
static int newton_iterate (void * vstate, gsl_multiroot_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx);
|
|
Packit |
67cb25 |
static void newton_free (void * vstate);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
static int
|
|
Packit |
67cb25 |
newton_alloc (void * vstate, size_t n)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
newton_state_t * state = (newton_state_t *) vstate;
|
|
Packit |
67cb25 |
gsl_permutation * p;
|
|
Packit |
67cb25 |
gsl_matrix * m;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
m = gsl_matrix_calloc (n,n);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
if (m == 0)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
GSL_ERROR ("failed to allocate space for lu", GSL_ENOMEM);
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
state->lu = m ;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
p = gsl_permutation_calloc (n);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
if (p == 0)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
gsl_matrix_free(m);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
GSL_ERROR ("failed to allocate space for permutation", GSL_ENOMEM);
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
state->permutation = p ;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
return GSL_SUCCESS;
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
static int
|
|
Packit |
67cb25 |
newton_set (void * vstate, gsl_multiroot_function_fdf * FDF, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
newton_state_t * state = (newton_state_t *) vstate;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
size_t i, n = FDF->n ;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
state = 0 ; /* avoid warnings about unused parameters */
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
GSL_MULTIROOT_FN_EVAL_F_DF (FDF, x, f, J);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
for (i = 0; i < n; i++)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
gsl_vector_set (dx, i, 0.0);
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
return GSL_SUCCESS;
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
static int
|
|
Packit |
67cb25 |
newton_iterate (void * vstate, gsl_multiroot_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
newton_state_t * state = (newton_state_t *) vstate;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
int signum;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
size_t i;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
size_t n = fdf->n ;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_matrix_memcpy (state->lu, J);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_linalg_LU_decomp (state->lu, state->permutation, &signum);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
int status = gsl_linalg_LU_solve (state->lu, state->permutation, f, dx);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
if (status)
|
|
Packit |
67cb25 |
return status;
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
for (i = 0; i < n; i++)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
double e = gsl_vector_get (dx, i);
|
|
Packit |
67cb25 |
double y = gsl_vector_get (x, i);
|
|
Packit |
67cb25 |
gsl_vector_set (dx, i, -e);
|
|
Packit |
67cb25 |
gsl_vector_set (x, i, y - e);
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
int status = GSL_MULTIROOT_FN_EVAL_F_DF (fdf, x, f, J);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
if (status != GSL_SUCCESS)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
return GSL_EBADFUNC;
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
return GSL_SUCCESS;
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
static void
|
|
Packit |
67cb25 |
newton_free (void * vstate)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
newton_state_t * state = (newton_state_t *) vstate;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_matrix_free(state->lu);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_permutation_free(state->permutation);
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
static const gsl_multiroot_fdfsolver_type newton_type =
|
|
Packit |
67cb25 |
{"newton", /* name */
|
|
Packit |
67cb25 |
sizeof (newton_state_t),
|
|
Packit |
67cb25 |
&newton_alloc,
|
|
Packit |
67cb25 |
&newton_set,
|
|
Packit |
67cb25 |
&newton_iterate,
|
|
Packit |
67cb25 |
&newton_free};
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
const gsl_multiroot_fdfsolver_type * gsl_multiroot_fdfsolver_newton = &newton_type;
|