summaryrefslogtreecommitdiff
path: root/gsl-1.9/multimin/linear_wrapper.c
blob: b284da9d511da71fb0f797f45e3b57401b953ce3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
typedef struct
{
  gsl_function_fdf fdf_linear;
  gsl_multimin_function_fdf *fdf;
  /* fixed values */
  const gsl_vector *x;
  const gsl_vector *g;
  const gsl_vector *p;

  /* cached values, for x(alpha) = x + alpha * p */
  double f_alpha;
  double df_alpha;
  gsl_vector *x_alpha;
  gsl_vector *g_alpha;

  /* cache "keys" */
  double f_cache_key;
  double df_cache_key;
  double x_cache_key;
  double g_cache_key;
}
wrapper_t;

static void
moveto (double alpha, wrapper_t * w)
{
  if (alpha == w->x_cache_key)  /* using previously cached position */
    {
      return;
    }

  /* set x_alpha = x + alpha * p */

  gsl_vector_memcpy (w->x_alpha, w->x);
  gsl_blas_daxpy (alpha, w->p, w->x_alpha);

  w->x_cache_key = alpha;
}

static double
slope (wrapper_t * w)           /* compute gradient . direction */
{
  double df;
  gsl_blas_ddot (w->g_alpha, w->p, &df);
  return df;
}

static double
wrap_f (double alpha, void *params)
{
  wrapper_t *w = (wrapper_t *) params;
  if (alpha == w->f_cache_key)  /* using previously cached f(alpha) */
    {
      return w->f_alpha;
    }

  moveto (alpha, w);

  w->f_alpha = GSL_MULTIMIN_FN_EVAL_F (w->fdf, w->x_alpha);
  w->f_cache_key = alpha;

  return w->f_alpha;
}

static double
wrap_df (double alpha, void *params)
{
  wrapper_t *w = (wrapper_t *) params;
  if (alpha == w->df_cache_key) /* using previously cached df(alpha) */
    {
      return w->df_alpha;
    }

  moveto (alpha, w);

  if (alpha != w->g_cache_key) 
    {
      GSL_MULTIMIN_FN_EVAL_DF (w->fdf, w->x_alpha, w->g_alpha);
      w->g_cache_key = alpha;
    }

  w->df_alpha = slope (w);
  w->df_cache_key = alpha;

  return w->df_alpha;
}

static void
wrap_fdf (double alpha, void *params, double *f, double *df)
{
  wrapper_t *w = (wrapper_t *) params;

  /* Check for previously cached values */

  if (alpha == w->f_cache_key && alpha == w->df_cache_key)
    {
      *f = w->f_alpha;
      *df = w->df_alpha;
      return;
    }

  if (alpha == w->f_cache_key || alpha == w->df_cache_key)
    {
      *f = wrap_f (alpha, params);
      *df = wrap_df (alpha, params);
      return;
    }

  moveto (alpha, w);
  GSL_MULTIMIN_FN_EVAL_F_DF (w->fdf, w->x_alpha, &w->f_alpha, w->g_alpha);
  w->f_cache_key = alpha;
  w->g_cache_key = alpha;

  w->df_alpha = slope (w);
  w->df_cache_key = alpha;

  *f = w->f_alpha;
  *df = w->df_alpha;
}

static void
prepare_wrapper (wrapper_t * w, gsl_multimin_function_fdf * fdf,
                 const gsl_vector * x, double f, const gsl_vector *g, 
                 const gsl_vector * p,
                 gsl_vector * x_alpha, gsl_vector *g_alpha)
{
  w->fdf_linear.f = &wrap_f;
  w->fdf_linear.df = &wrap_df;
  w->fdf_linear.fdf = &wrap_fdf;
  w->fdf_linear.params = (void *)w;  /* pointer to "self" */

  w->fdf = fdf;

  w->x = x;
  w->g = g;
  w->p = p;

  w->x_alpha = x_alpha;
  w->g_alpha = g_alpha;

  gsl_vector_memcpy(w->x_alpha, w->x); 
  w->x_cache_key = 0.0;
  
  w->f_alpha = f;
  w->f_cache_key = 0.0;
  
  gsl_vector_memcpy(w->g_alpha, w->g);
  w->g_cache_key = 0.0;

  w->df_alpha = slope(w);
  w->df_cache_key = 0.0;
}

static void
update_position (wrapper_t * w, double alpha, gsl_vector *x, double *f, gsl_vector *g)
{
  /* ensure that everything is fully cached */
  { double f_alpha, df_alpha; wrap_fdf (alpha, w, &f_alpha, &df_alpha); } ;

  *f = w->f_alpha;
  gsl_vector_memcpy(x, w->x_alpha);
  gsl_vector_memcpy(g, w->g_alpha);
}  

static void
change_direction (wrapper_t * w)
{
  /* Convert the cache values from the end of the current minimisation
     to those needed for the start of the next minimisation, alpha=0 */

  /* The new x_alpha for alpha=0 is the current position */
  gsl_vector_memcpy (w->x_alpha, w->x);
  w->x_cache_key = 0.0;

  /* The function value does not change */
  w->f_cache_key = 0.0;

  /* The new g_alpha for alpha=0 is the current gradient at the endpoint */
  gsl_vector_memcpy (w->g_alpha, w->g);
  w->g_cache_key = 0.0;

  /* Calculate the slope along the new direction vector, p */
  w->df_alpha = slope (w);
  w->df_cache_key = 0.0;
}