-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcg_descent.h
267 lines (235 loc) · 7.37 KB
/
cg_descent.h
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#ifndef _CG_DESCENT_HEADER_H
#define _CG_DESCENT_HEADER_H
#include "cg_user.h"
#include <math.h>
#include <ctype.h>
#define PRIVATE static
#define ZERO ((double) 0)
#define ONE ((double) 1)
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
typedef struct cg_com_struct /* common variables */
{
/* parameters computed by the code */
INT n ; /* problem dimension, saved for reference */
INT nf ; /* number of function evaluations */
INT ng ; /* number of gradient evaluations */
int QuadOK ; /* T (quadratic step successful) */
int UseCubic ; /* T (use cubic step) F (use secant step) */
int neps ; /* number of time eps updated */
int PertRule ; /* T => estimated error in function value is eps*Ck,
F => estimated error in function value is eps */
int QuadF ; /* T => function appears to be quadratic */
double SmallCost ; /* |f| <= SmallCost => set PertRule = F */
double alpha ; /* stepsize along search direction */
double f ; /* function value for step alpha */
double df ; /* function derivative for step alpha */
double fpert ; /* perturbation is eps*|f| if PertRule is T */
double eps ; /* current value of eps */
double tol ; /* computing tolerance */
double f0 ; /* old function value */
double df0 ; /* old derivative */
double Ck ; /* average cost as given by the rule:
Qk = Qdecay*Qk + 1, Ck += (fabs (f) - Ck)/Qk */
double wolfe_hi ; /* upper bound for slope in Wolfe test */
double wolfe_lo ; /* lower bound for slope in Wolfe test */
double awolfe_hi ; /* upper bound for slope, approximate Wolfe test */
int AWolfe ; /* F (use Wolfe line search)
T (use approximate Wolfe line search)
do not change user's AWolfe, this value can be
changed based on AWolfeFac */
int Wolfe ; /* T (means code reached the Wolfe part of cg_line */
double rho ; /* either Parm->rho or Parm->nan_rho */
double alphaold ; /* previous value for stepsize alpha */
double *x ; /* current iterate */
double *xtemp ; /* x + alpha*d */
double *d ; /* current search direction */
double *g ; /* gradient at x */
double *gtemp ; /* gradient at x + alpha*d */
cg_value_fn cg_value ; /* f = cg_value (x, n, User) */
cg_grad_fn cg_grad ; /* cg_grad (g, x, n, User) */
cg_valgrad_fn cg_valgrad ; /* f = cg_valgrad (g, x, n, User)*/
void *User ; /* user provided pointer passed to functions */
cg_parameter *Parm ; /* user parameters */
} cg_com ;
/* prototypes */
PRIVATE int cg_Wolfe
(
double alpha, /* stepsize */
double f, /* function value associated with stepsize alpha */
double dphi, /* derivative value associated with stepsize alpha */
cg_com *Com /* cg com */
) ;
PRIVATE int cg_tol
(
double gnorm, /* gradient sup-norm */
cg_com *Com /* cg com */
) ;
PRIVATE int cg_line
(
cg_com *Com /* cg com structure */
) ;
PRIVATE int cg_contract
(
double *A, /* left side of bracketing interval */
double *fA, /* function value at a */
double *dA, /* derivative at a */
double *B, /* right side of bracketing interval */
double *fB, /* function value at b */
double *dB, /* derivative at b */
cg_com *Com /* cg com structure */
) ;
PRIVATE int cg_evaluate
(
char *what, /* fg = evaluate func and grad, g = grad only,f = func only*/
char *nan, /* y means check function/derivative values for nan */
cg_com *Com
) ;
PRIVATE double cg_cubic
(
double a,
double fa, /* function value at a */
double da, /* derivative at a */
double b,
double fb, /* function value at b */
double db /* derivative at b */
) ;
PRIVATE void cg_matvec
(
double *y, /* product vector */
double *A, /* dense matrix */
double *x, /* input vector */
int n, /* number of columns of A */
INT m, /* number of rows of A */
int w /* T => y = A*x, F => y = A'*x */
) ;
PRIVATE void cg_trisolve
(
double *x, /* right side on input, solution on output */
double *R, /* dense matrix */
int m, /* leading dimension of R */
int n, /* dimension of triangular system */
int w /* T => Rx = y, F => R'x = y */
) ;
PRIVATE double cg_inf
(
double *x, /* vector */
INT n /* length of vector */
) ;
PRIVATE void cg_scale0
(
double *y, /* output vector */
double *x, /* input vector */
double s, /* scalar */
int n /* length of vector */
) ;
PRIVATE void cg_scale
(
double *y, /* output vector */
double *x, /* input vector */
double s, /* scalar */
INT n /* length of vector */
) ;
PRIVATE void cg_daxpy0
(
double *x, /* input and output vector */
double *d, /* direction */
double alpha, /* stepsize */
int n /* length of the vectors */
) ;
PRIVATE void cg_daxpy
(
double *x, /* input and output vector */
double *d, /* direction */
double alpha, /* stepsize */
INT n /* length of the vectors */
) ;
PRIVATE double cg_dot0
(
double *x, /* first vector */
double *y, /* second vector */
int n /* length of vectors */
) ;
PRIVATE double cg_dot
(
double *x, /* first vector */
double *y, /* second vector */
INT n /* length of vectors */
) ;
PRIVATE void cg_copy0
(
double *y, /* output of copy */
double *x, /* input of copy */
int n /* length of vectors */
) ;
PRIVATE void cg_copy
(
double *y, /* output of copy */
double *x, /* input of copy */
INT n /* length of vectors */
) ;
PRIVATE void cg_step
(
double *xtemp, /*output vector */
double *x, /* initial vector */
double *d, /* search direction */
double alpha, /* stepsize */
INT n /* length of the vectors */
) ;
PRIVATE void cg_init
(
double *x, /* input and output vector */
double s, /* scalar */
INT n /* length of vector */
) ;
PRIVATE double cg_update_2
(
double *gold, /* old g */
double *gnew, /* new g */
double *d, /* d */
INT n /* length of vectors */
) ;
PRIVATE double cg_update_inf
(
double *gold, /* old g */
double *gnew, /* new g */
double *d, /* d */
INT n /* length of vectors */
) ;
PRIVATE double cg_update_ykyk
(
double *gold, /* old g */
double *gnew, /* new g */
double *Ykyk,
double *Ykgk,
INT n /* length of vectors */
) ;
PRIVATE double cg_update_inf2
(
double *gold, /* old g */
double *gnew, /* new g */
double *d, /* d */
double *gnorm2, /* 2-norm of g */
INT n /* length of vectors */
) ;
PRIVATE double cg_update_d
(
double *d,
double *g,
double beta,
double *gnorm2, /* 2-norm of g */
INT n /* length of vectors */
) ;
PRIVATE void cg_Yk
(
double *y, /*output vector */
double *gold, /* initial vector */
double *gnew, /* search direction */
double *yty, /* y'y */
INT n /* length of the vectors */
) ;
PRIVATE void cg_printParms
(
cg_parameter *Parm
) ;
#endif