This is the mail archive of the gsl-discuss@sources.redhat.com mailing list for the GSL project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Additional multiroots functions.


Toby White <tow@theor.ch.cam.ac.uk> writes:

[snip patch for multiroots]

> Incidentally, if you're minded to accept this patch, would
> you object to similar patches for roots and min, both of 
> which use the same initial-alloc-and-set style functions?

I've now updated min in a similar fashion. The patch below does the 
following;

1) gsl_min_fminimizer_alloc now takes only 1 argument - the type of
minimizer - and does not set up a function. Thus

gsl_min_fminimizer *
gsl_min_fminimizer_alloc (const gsl_min_fminimizer_type * T) ;

2) gsl_min_fminimizer_alloc_with_values has been removed.

3) A test has been added to gsl_min_fminimizer_iterate to check that
the minimizer has been set up properly.

4) min/test.c has been updated to use the new alloc function.

5) doc/min.texi has been updated to reflect the altered functions.

------ 

diff -ur gsl.orig/doc/min.texi gsl/doc/min.texi
--- gsl.orig/doc/min.texi	Wed Mar 15 12:05:30 2000
+++ gsl/doc/min.texi	Fri Feb 16 17:40:36 2001
@@ -133,11 +133,9 @@
 @node Initializing the Minimizer
 @section Initializing the Minimizer
 
-@deftypefun {gsl_min_fminimizer *} gsl_min_fminimizer_alloc (const gsl_min_fminimizer_type * @var{T}, gsl_function * @var{f}, double @var{minimum}, gsl_interval @var{x})
+@deftypefun {gsl_min_fminimizer *} gsl_min_fminimizer_alloc (const gsl_min_fminimizer_type * @var{T})
 This function returns a pointer to a a newly allocated instance of a
-minimizer of type @var{T} for the function @var{f}, with an
-initial bracket on the minimum of @var{x} containing a guess for the
-location of the minimum @var{minimum}.  For example, the following code
+minimizer of type @var{T}.  For example, the following code
 creates an instance of a golden section minimizer,
 
 @example
@@ -150,16 +148,13 @@
 code of @code{GSL_ENOMEM}.
 @end deftypefun
 
-@deftypefun {gsl_min_fminimizer *} gsl_min_fminimizer_alloc_with_values (const gsl_min_fminimizer_type * @var{T}, gsl_function * @var{f}, double @var{minimum}, double @var{f_minimum}, gsl_interval @var{x}, double @var{f_lower}, double @var{f_upper})
-This function is equivalent to @code{gsl_min_fminimizer_alloc} but uses
-the values @var{f_minimum}, @var{f_lower} and @var{f_upper} instead of
-computing @code{f(minimum)}, @code{f(x.lower)} and @code{f(x.upper)}.
-@end deftypefun
-
 @deftypefun int gsl_min_fminimizer_set (gsl_min_fminimizer * @var{s}, gsl_function * @var{f}, double @var{minimum}, gsl_interval @var{x})
-This function reinitializes an existing minimizer @var{s} to use
+This function sets, or resets, an existing minimizer @var{s} to use
 the function @var{f} and the initial search interval @var{x}, with a
-guess for the location of the minimum @var{minimum}.
+guess for the location of the minimum @var{minimum}. 
+
+If the interval given does not contain a minimum, then the function
+returns an error code of @code{GSL_FAILURE}.
 @end deftypefun
 
 @deftypefun int gsl_min_fminimizer_set_with_values (gsl_min_fminimizer * @var{s}, gsl_function * @var{f}, double @var{minimum}, double @var{f_minimum}, gsl_interval @var{x}, double @var{f_lower}, double @var{f_upper})
@@ -393,7 +388,8 @@
   F.function = &fn1;
   F.params = 0;
 
-  s = gsl_min_fminimizer_alloc (gsl_min_fminimizer_brent, &F, m, x);
+  s = gsl_min_fminimizer_alloc (gsl_min_fminimizer_brent);
+  gsl_min_fminimizer_set (s, &F, m, x);
 
   printf ("using %s method\n", gsl_min_fminimizer_name (s));
 
diff -ur gsl.orig/min/fsolver.c gsl/min/fsolver.c
--- gsl.orig/min/fsolver.c	Mon Jun  5 20:27:36 2000
+++ gsl/min/fsolver.c	Fri Feb 16 17:31:58 2001
@@ -41,29 +41,6 @@
   return GSL_SUCCESS;
 }
 
-gsl_min_fminimizer *
-gsl_min_fminimizer_alloc (const gsl_min_fminimizer_type * T, 
-			 gsl_function * f, double minimum, gsl_interval x)
-{
-  int status ;
-
-  gsl_min_fminimizer * s;
-
-  double f_minimum, f_lower, f_upper;
-
-  status = compute_f_values (f, minimum, &f_minimum, x, &f_lower, &f_upper);
-
-  if (status != GSL_SUCCESS)
-    {
-      GSL_ERROR_VAL ("bad function value", GSL_EBADFUNC, 0);
-    }
-  
-  s = gsl_min_fminimizer_alloc_with_values (T, f, minimum, f_minimum, 
-                                            x, f_lower, f_upper);
-
-  return s;
-}
-
 int
 gsl_min_fminimizer_set (gsl_min_fminimizer * s, 
                         gsl_function * f, double minimum, gsl_interval x)
@@ -84,13 +61,8 @@
   return status;
 }
 
-
 gsl_min_fminimizer *
-gsl_min_fminimizer_alloc_with_values (const gsl_min_fminimizer_type * T, 
-                                      gsl_function * f, 
-                                      double minimum, double f_minimum,
-                                      gsl_interval x, 
-                                      double f_lower, double f_upper)
+gsl_min_fminimizer_alloc (const gsl_min_fminimizer_type * T) 
 {
   int status;
 
@@ -115,17 +87,6 @@
 
   s->type = T ;
 
-  status = gsl_min_fminimizer_set_with_values (s, f, minimum, f_minimum,
-                                               x, f_lower, f_upper); 
-
-  if (status != GSL_SUCCESS)
-    {
-      free (s->state);
-      free (s);		/* exception in constructor, avoid memory leak */
-
-      GSL_ERROR_VAL ("failed to set minimizer", status, 0);
-    };
-
   return s;
 }
 
@@ -168,6 +129,11 @@
 int
 gsl_min_fminimizer_iterate (gsl_min_fminimizer * s)
 {
+  if (!(s->state))
+  {
+    GSL_ERROR ("minimizer not yet set", GSL_EINVAL);
+  }
+  
   return (s->type->iterate) (s->state, s->function, 
                              &(s->minimum), &(s->f_minimum),
                              &(s->interval), &(s->f_lower), &(s->f_upper));
diff -ur gsl.orig/min/gsl_min.h gsl/min/gsl_min.h
--- gsl.orig/min/gsl_min.h	Thu May  4 12:25:04 2000
+++ gsl/min/gsl_min.h	Fri Feb 16 17:23:04 2001
@@ -56,16 +56,8 @@
 gsl_min_fminimizer;
 
 gsl_min_fminimizer *
-gsl_min_fminimizer_alloc (const gsl_min_fminimizer_type * T, 
-			 gsl_function * f, double minimum, gsl_interval x);
-
-gsl_min_fminimizer *
-gsl_min_fminimizer_alloc_with_values (const gsl_min_fminimizer_type * T, 
-                                      gsl_function * f, 
-                                      double minimum, double f_minimum,
-                                      gsl_interval x, 
-                                      double f_lower, double f_upper);
-
+gsl_min_fminimizer_alloc (const gsl_min_fminimizer_type * T) ;
+                                      
 void gsl_min_fminimizer_free (gsl_min_fminimizer * s);
 
 int gsl_min_fminimizer_set (gsl_min_fminimizer * s, 
diff -ur gsl.orig/min/test.c gsl/min/test.c
--- gsl.orig/min/test.c	Mon May 15 20:39:14 2000
+++ gsl/min/test.c	Fri Feb 16 17:31:05 2001
@@ -94,7 +94,8 @@
   x.lower = lower_bound;
   x.upper = upper_bound;
 
-  s = gsl_min_fminimizer_alloc(T, f, middle, x) ;
+  s = gsl_min_fminimizer_alloc (T) ;
+  gsl_min_fminimizer_set (s, f, middle, x) ;
   
   do 
     {
@@ -155,11 +156,13 @@
   x.lower = lower_bound;
   x.upper = upper_bound;
 
-  s = gsl_min_fminimizer_alloc(T, f, middle, x) ;
+  s = gsl_min_fminimizer_alloc (T) ;
+  status = gsl_min_fminimizer_set (s, f, middle, x) ; 
 
-  if (s == 0) 
+  if (status != GSL_SUCCESS) 
     {
-      gsl_test (s != 0, "%s, %s", T->name, description);
+      gsl_min_fminimizer_free (s) ;
+      gsl_test (status == GSL_SUCCESS, "%s, %s", T->name, description);
       return ;
     }

------

Toby

-- 
Toby White, University Chemical Lab., Lensfield Road, Cambridge. CB2 1EW. U.K.
Email: <tow@theor.ch.cam.ac.uk> GPG Key ID: 1DE9DE75
Web: <URL:http://ket.ch.cam.ac.uk/people/tow/index.html>
Tel: +44 1223 336423
Fax: +44 1223 336362


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]