3 gsl_function_fdf fdf_linear;
4 gsl_multimin_function_fdf *fdf;
10 /* cached values, for x(alpha) = x + alpha * p */
25 moveto (double alpha, wrapper_t * w)
27 if (alpha == w->x_cache_key) /* using previously cached position */
32 /* set x_alpha = x + alpha * p */
34 gsl_vector_memcpy (w->x_alpha, w->x);
35 gsl_blas_daxpy (alpha, w->p, w->x_alpha);
37 w->x_cache_key = alpha;
41 slope (wrapper_t * w) /* compute gradient . direction */
44 gsl_blas_ddot (w->g_alpha, w->p, &df);
49 wrap_f (double alpha, void *params)
51 wrapper_t *w = (wrapper_t *) params;
52 if (alpha == w->f_cache_key) /* using previously cached f(alpha) */
59 w->f_alpha = GSL_MULTIMIN_FN_EVAL_F (w->fdf, w->x_alpha);
60 w->f_cache_key = alpha;
66 wrap_df (double alpha, void *params)
68 wrapper_t *w = (wrapper_t *) params;
69 if (alpha == w->df_cache_key) /* using previously cached df(alpha) */
76 if (alpha != w->g_cache_key)
78 GSL_MULTIMIN_FN_EVAL_DF (w->fdf, w->x_alpha, w->g_alpha);
79 w->g_cache_key = alpha;
82 w->df_alpha = slope (w);
83 w->df_cache_key = alpha;
89 wrap_fdf (double alpha, void *params, double *f, double *df)
91 wrapper_t *w = (wrapper_t *) params;
93 /* Check for previously cached values */
95 if (alpha == w->f_cache_key && alpha == w->df_cache_key)
102 if (alpha == w->f_cache_key || alpha == w->df_cache_key)
104 *f = wrap_f (alpha, params);
105 *df = wrap_df (alpha, params);
110 GSL_MULTIMIN_FN_EVAL_F_DF (w->fdf, w->x_alpha, &w->f_alpha, w->g_alpha);
111 w->f_cache_key = alpha;
112 w->g_cache_key = alpha;
114 w->df_alpha = slope (w);
115 w->df_cache_key = alpha;
122 prepare_wrapper (wrapper_t * w, gsl_multimin_function_fdf * fdf,
123 const gsl_vector * x, double f, const gsl_vector *g,
124 const gsl_vector * p,
125 gsl_vector * x_alpha, gsl_vector *g_alpha)
127 w->fdf_linear.f = &wrap_f;
128 w->fdf_linear.df = &wrap_df;
129 w->fdf_linear.fdf = &wrap_fdf;
130 w->fdf_linear.params = (void *)w; /* pointer to "self" */
138 w->x_alpha = x_alpha;
139 w->g_alpha = g_alpha;
141 gsl_vector_memcpy(w->x_alpha, w->x);
142 w->x_cache_key = 0.0;
145 w->f_cache_key = 0.0;
147 gsl_vector_memcpy(w->g_alpha, w->g);
148 w->g_cache_key = 0.0;
150 w->df_alpha = slope(w);
151 w->df_cache_key = 0.0;
155 update_position (wrapper_t * w, double alpha, gsl_vector *x, double *f, gsl_vector *g)
157 /* ensure that everything is fully cached */
158 { double f_alpha, df_alpha; wrap_fdf (alpha, w, &f_alpha, &df_alpha); } ;
161 gsl_vector_memcpy(x, w->x_alpha);
162 gsl_vector_memcpy(g, w->g_alpha);
166 change_direction (wrapper_t * w)
168 /* Convert the cache values from the end of the current minimisation
169 to those needed for the start of the next minimisation, alpha=0 */
171 /* The new x_alpha for alpha=0 is the current position */
172 gsl_vector_memcpy (w->x_alpha, w->x);
173 w->x_cache_key = 0.0;
175 /* The function value does not change */
176 w->f_cache_key = 0.0;
178 /* The new g_alpha for alpha=0 is the current gradient at the endpoint */
179 gsl_vector_memcpy (w->g_alpha, w->g);
180 w->g_cache_key = 0.0;
182 /* Calculate the slope along the new direction vector, p */
183 w->df_alpha = slope (w);
184 w->df_cache_key = 0.0;