Added script front-end for primer-design code
[htsworkflow.git] / htswanalysis / MACS / lib / gsl / gsl-1.11 / min / golden.c
1 /* min/golden.c
2  * 
3  * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Brian Gough
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or (at
8  * your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20
21 /* goldensection.c -- goldensection minimum finding algorithm */
22
23 #include <config.h>
24
25 #include <stddef.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <math.h>
29 #include <float.h>
30
31 #include <gsl/gsl_math.h>
32 #include <gsl/gsl_errno.h>
33 #include <gsl/gsl_min.h>
34
35 #include "min.h"
36
37 typedef struct
38   {
39     double dummy;
40   }
41 goldensection_state_t;
42
43 static int goldensection_init (void * vstate, gsl_function * f, double x_minimum, double f_minimum, double x_lower, double f_lower, double x_upper, double f_upper);
44 static int goldensection_iterate (void * vstate, gsl_function * f, double * x_minimum, double * f_minimum, double * x_lower, double * f_lower, double * x_upper, double * f_upper);
45
46 static int
47 goldensection_init (void * vstate, gsl_function * f, double x_minimum, double f_minimum, double x_lower, double f_lower, double x_upper, double f_upper)
48 {
49   goldensection_state_t * state = (goldensection_state_t *) vstate;
50
51   /* no initialization required, prevent warnings about unused variables */
52
53   state = 0;
54   f = 0;
55   x_minimum = 0;
56   f_minimum = 0;
57   x_lower = 0;
58   f_lower = 0;
59   x_upper = 0;
60   f_upper = 0;
61
62   return GSL_SUCCESS;
63 }
64
65 static int
66 goldensection_iterate (void * vstate, gsl_function * f, double * x_minimum, double * f_minimum, double * x_lower, double * f_lower, double * x_upper, double * f_upper)
67 {
68   goldensection_state_t * state = (goldensection_state_t *) vstate;
69
70   const double x_center = *x_minimum ;
71   const double x_left = *x_lower ;
72   const double x_right = *x_upper ;
73
74   const double f_min = *f_minimum;
75
76   const double golden = 0.3819660; /* golden = (3 - sqrt(5))/2 */
77   
78   const double w_lower = (x_center - x_left);
79   const double w_upper = (x_right - x_center);
80
81   double x_new, f_new;
82
83   state = 0 ; /* avoid warning about unused parameters */
84   
85   x_new = x_center + golden * ((w_upper > w_lower) ? w_upper : -w_lower) ;
86
87   SAFE_FUNC_CALL (f, x_new, &f_new);
88
89   if (f_new < f_min)
90     {
91       *x_minimum = x_new ;
92       *f_minimum = f_new ;
93       return GSL_SUCCESS;
94     }
95   else if (x_new < x_center && f_new > f_min)
96     {
97       *x_lower = x_new ;
98       *f_lower = f_new ;
99       return GSL_SUCCESS;
100     }
101   else if (x_new > x_center && f_new > f_min)
102     {
103       *x_upper = x_new ;
104       *f_upper = f_new ;
105       return GSL_SUCCESS;
106     }
107   else
108     {
109       return GSL_FAILURE;
110     }
111 }
112
113
114 static const gsl_min_fminimizer_type goldensection_type =
115 {"goldensection",                               /* name */
116  sizeof (goldensection_state_t),
117  &goldensection_init,
118  &goldensection_iterate};
119
120 const gsl_min_fminimizer_type  * gsl_min_fminimizer_goldensection = &goldensection_type;