Blame roots/fsolver.c

Packit 67cb25
/* roots/fsolver.c
Packit 67cb25
 * 
Packit 67cb25
 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Reid Priedhorsky, 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 <stdlib.h>
Packit 67cb25
#include <string.h>
Packit 67cb25
#include <gsl/gsl_errno.h>
Packit 67cb25
#include <gsl/gsl_roots.h>
Packit 67cb25
Packit 67cb25
gsl_root_fsolver *
Packit 67cb25
gsl_root_fsolver_alloc (const gsl_root_fsolver_type * T)
Packit 67cb25
{
Packit 67cb25
  gsl_root_fsolver * s = (gsl_root_fsolver *) malloc (sizeof (gsl_root_fsolver));
Packit 67cb25
Packit 67cb25
  if (s == 0)
Packit 67cb25
    {
Packit 67cb25
      GSL_ERROR_VAL ("failed to allocate space for root solver struct",
Packit 67cb25
                        GSL_ENOMEM, 0);
Packit 67cb25
    };
Packit 67cb25
Packit 67cb25
  s->state = malloc (T->size);
Packit 67cb25
Packit 67cb25
  if (s->state == 0)
Packit 67cb25
    {
Packit 67cb25
      free (s);         /* exception in constructor, avoid memory leak */
Packit 67cb25
Packit 67cb25
      GSL_ERROR_VAL ("failed to allocate space for root solver state",
Packit 67cb25
                        GSL_ENOMEM, 0);
Packit 67cb25
    };
Packit 67cb25
Packit 67cb25
  s->type = T ;
Packit 67cb25
  s->function = NULL ;
Packit 67cb25
Packit 67cb25
  return s;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
int
Packit 67cb25
gsl_root_fsolver_set (gsl_root_fsolver * s, gsl_function * f, double x_lower, double x_upper)
Packit 67cb25
{
Packit 67cb25
  if (x_lower > x_upper)
Packit 67cb25
    {
Packit 67cb25
      GSL_ERROR ("invalid interval (lower > upper)", GSL_EINVAL);
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  s->function = f;
Packit 67cb25
  s->root = 0.5 * (x_lower + x_upper);  /* initial estimate */
Packit 67cb25
  s->x_lower = x_lower;
Packit 67cb25
  s->x_upper = x_upper;
Packit 67cb25
Packit 67cb25
  return (s->type->set) (s->state, s->function, &(s->root), x_lower, x_upper);
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
int
Packit 67cb25
gsl_root_fsolver_iterate (gsl_root_fsolver * s)
Packit 67cb25
{
Packit 67cb25
  return (s->type->iterate) (s->state, 
Packit 67cb25
                             s->function, &(s->root), 
Packit 67cb25
                             &(s->x_lower), &(s->x_upper));
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
void
Packit 67cb25
gsl_root_fsolver_free (gsl_root_fsolver * s)
Packit 67cb25
{
Packit 67cb25
  RETURN_IF_NULL (s);
Packit 67cb25
  free (s->state);
Packit 67cb25
  free (s);
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
const char *
Packit 67cb25
gsl_root_fsolver_name (const gsl_root_fsolver * s)
Packit 67cb25
{
Packit 67cb25
  return s->type->name;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
double
Packit 67cb25
gsl_root_fsolver_root (const gsl_root_fsolver * s)
Packit 67cb25
{
Packit 67cb25
  return s->root;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
double
Packit 67cb25
gsl_root_fsolver_x_lower (const gsl_root_fsolver * s)
Packit 67cb25
{
Packit 67cb25
  return s->x_lower;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
double
Packit 67cb25
gsl_root_fsolver_x_upper (const gsl_root_fsolver * s)
Packit 67cb25
{
Packit 67cb25
  return s->x_upper;
Packit 67cb25
}
Packit 67cb25