1 /*******************************************************************
2 Copyright (C) 2017 AMPL Optimization, Inc.; written by David M. Gay.
3 
4 Permission to use, copy, modify, and distribute this software and its
5 documentation for any purpose and without fee is hereby granted,
6 provided that the above copyright notice appear in all copies and that
7 both that the copyright notice and this permission notice and warranty
8 disclaimer appear in supporting documentation.
9 
10 The author and AMPL Optimization, Inc. disclaim all warranties with
11 regard to this software, including all implied warranties of
12 merchantability and fitness.  In no event shall the author be liable
13 for any special, indirect or consequential damages or any damages
14 whatsoever resulting from loss of use, data or profits, whether in an
15 action of contract, negligence or other tortious action, arising out
16 of or in connection with the use or performance of this software.
17 *******************************************************************/
18 
19 /* Variant of nlp.h for Hessian times vector computations. */
20 
21 #ifndef NLP_H2_included
22 #define NLP_H2_included
23 
24 #ifndef ASL_included
25 #include "asl.h"
26 #endif
27 
28 #ifdef __cplusplus
29  extern "C" {
30 #endif
31 
32 typedef struct argpair2 argpair2;
33 typedef struct cde2 cde2;
34 typedef struct cexp2 cexp2;
35 typedef struct cexp21 cexp21;
36 typedef struct de2 de2;
37 typedef union  ei2 ei2;
38 typedef struct expr2 expr2;
39 typedef struct expr2_f expr2_f;
40 typedef struct expr2_h expr2_h;
41 typedef struct expr2_if expr2_if;
42 typedef struct expr2_v expr2_v;
43 typedef struct expr2_va expr2_va;
44 typedef struct funnel2 funnel2;
45 typedef struct hes_fun hes_fun;
46 typedef struct list2 list2;
47 typedef union  uir uir;
48 
49 typedef real  efunc2 ANSI((expr2* A_ASL));
50 typedef char *sfunc  ANSI((expr2* A_ASL));
51 
52  union
53 uir {
54 	int	i;
55 	real	r;
56 	};
57 
58  union
59 ei2 {
60 	expr2	*e;
61 	expr2	**ep;
62 	expr2_if*eif;
63 	expr_n	*en;
64 	expr2_v	*ev;
65 	int	i;
66 	plterm	*p;
67 	de2	*d;
68 	real	*rp;
69 	derp	*D;
70 	cexp2	*ce;
71 	};
72 
73  struct
74 expr2 {
75 	efunc2 *op;
76 	int a;		/* adjoint index (for gradient computation) */
77 	expr2 *fwd, *bak;
78 	uir  dO;	/* deriv of op w.r.t. t in x + t*p */
79 	real aO;	/* adjoint (in Hv computation) of op */
80 	real adO;	/* adjoint (in Hv computation) of dO */
81 	real dL;	/* deriv of op w.r.t. left operand */
82 	ei2 L, R;	/* left and right operands */
83 	real dR;	/* deriv of op w.r.t. right operand */
84 	real dL2;	/* second partial w.r.t. L, L */
85 	real dLR;	/* second partial w.r.t. L, R */
86 	real dR2;	/* second partial w.r.t. R, R */
87 	};
88 
89  struct
90 expr2_v {
91 	efunc2 *op;
92 	int a;
93 	expr2 *fwd, *bak;
94 	uir dO;
95 	real aO, adO;
96 	real v;
97 	};
98 
99  struct
100 expr2_if {
101 	efunc2 *op;
102 	int a;
103 	expr2 *fwd, *bak;
104 	uir  dO;
105 	real aO, adO;
106 	expr2 *val, *vale, *valf, *e, *T, *Te, *Tf, *F, *Fe, *Ff;
107 	derp *D, *dT, *dF, *d0;
108 	ei2 Tv, Fv;
109 	expr2_if *next, *next2;
110 	derp *dTlast;
111 	};
112 
113  struct
114 expr2_va {
115 	efunc2 *op;
116 	int a;
117 	expr2 *fwd, *bak;
118 	uir  dO;
119 	real aO, adO;
120 	expr2 *val, *vale, *valf;
121 	ei2 L, R;
122 	expr2_va *next, *next2;
123 	derp *d0;
124 	};
125 
126  struct
127 cde2 {
128 	expr2	*e, *ee, *ef;
129 	derp	*d;
130 	int	zaplen;
131 	int	com11, n_com1;
132 	};
133 
134  struct
135 de2 {			/* for varargs */
136 	expr2 *e, *ee, *ef;
137 	derp *d;
138 	ei2 dv;
139 	derp *dlast;	/* for sputhes setup */
140 	};
141 
142  struct
143 list2 {
144 	list2	*next;
145 	ei2	item;
146 	};
147 
148  struct
149 cexp21 {
150 	expr2	*e, *ee, *ef;
151 	linpart	*L;
152 	int	nlin;
153 	};
154 
155  struct
156 cexp2 {
157 	expr2	*e, *ee, *ef;
158 	linpart	*L;
159 	int	nlin;
160 	int	zlen;
161 	struct linarg2 *la;	/* corresponding to L */
162 	funnel2	*funneled;
163 	list2	*cref;
164 	ei2	z;
165 	derp	*d;
166 	int	*vref;
167 	hes_fun	*hfun;
168 	};
169 
170  struct
171 funnel2 {
172 	funnel2	*next;
173 	cexp2	*ce;
174 	cde2	fcde;
175 	derp	*fulld;
176 	cplist	*cl;
177 	};
178 
179  struct
180 argpair2 {
181 	expr2	*e;
182 	union {
183 		char	**s;
184 		real	*v;
185 		} u;
186 	};
187 
188  struct
189 expr2_f {
190 	efunc2 *op;
191 	int	a;
192 	expr2 *fwd, *bak;
193 	uir  dO;
194 	real aO, adO;
195 	func_info *fi;
196 	arglist *al;
197 	argpair2 *ap, *ape, *sap, *sape;
198 	argpair2 *da;	/* differentiable args -- nonconstant */
199 	argpair2 *dae;
200 	real	**fh;		/* Hessian info */
201 	expr2	*args[1];
202 	};
203 
204  struct
205 expr2_h {
206 	efunc2 *op;
207 	int	a;
208 	char	sym[1];
209 	};
210 
211  typedef struct
212 Edag2info {
213 	cde2	*con2_de_;	/* constraint deriv. and expr. info */
214 	cde2	*lcon2_de_;	/* logical constraints */
215 	cde2	*obj2_de_;	/* objective  deriv. and expr. info */
216 	expr2_v	*var2_e_;	/* variable values (and related items) */
217 
218 			/* stuff for "defined" variables */
219 	funnel2	*f2_b_;
220 	funnel2	*f2_c_;
221 	funnel2	*f2_o_;
222 	expr2_v	*var2_ex_,
223 		*var2_ex1_;
224 	cexp2	*cexps2_, *cexpsc_, *cexpso_, *cexpse_;
225 	cexp21	*cexps21_;
226 	hes_fun	*hesthread;
227 	char	*c_class;	/* class of each constraint: */
228 				/* 0 = constant */
229 				/* 1 = linear */
230 				/* 2 = quadratic */
231 				/* 3 = general nonlinear */
232 	char	*o_class;	/* class of each objective */
233 	char	*v_class;	/* class of each defined variable */
234 	int	c_class_max;	/* max of c_class values */
235 	int	o_class_max;	/* max of o_class values */
236 				/* The above are only computed if requested */
237 				/* by the ASL_find_c_class and */
238 				/* ASL_find_o_class bits of the flags arg */
239 				/* to pfgh_read() and pfg_read() */
240 	int	x0kind_init;
241 	} Edag2info;
242 
243  typedef struct
244 ASL_fgh {
245 	Edagpars  p;
246 	Edaginfo  i;
247 	Edag2info I;
248 	} ASL_fgh;
249 
250  extern efunc2 *r2_ops_ASL[];
251  extern void com21eval_ASL ANSI((ASL_fgh*, int, int));
252  extern void com2eval_ASL ANSI((ASL_fgh*, int, int));
253  extern void fun2set_ASL ANSI((ASL_fgh*, funnel2 *));
254 #ifdef __cplusplus
255 	}
256 #endif
257 
258 #ifndef SKIP_NL2_DEFINES
259 extern efunc2 f2_OPVARVAL_ASL;
260 
261 #define cexpsc		asl->I.cexpsc_
262 #define cexpse		asl->I.cexpse_
263 #define cexpso		asl->I.cexpso_
264 #define cexps1		asl->I.cexps21_
265 #define cexps		asl->I.cexps2_
266 #define con_de		asl->I.con2_de_
267 #define f_b		asl->I.f2_b_
268 #define f_c		asl->I.f2_c_
269 #define f_o		asl->I.f2_o_
270 #define lcon_de		asl->I.lcon2_de_
271 #define obj_de		asl->I.obj2_de_
272 #define var_e		asl->I.var2_e_
273 #define var_ex1		asl->I.var2_ex1_
274 #define var_ex		asl->I.var2_ex_
275 
276 #define argpair	argpair2
277 #define cde	cde2
278 #define cexp	cexp2
279 #define cexp1	cexp21
280 #define de	de2
281 #define ei	ei2
282 #define expr	expr2
283 #define expr_f	expr2_f
284 #define expr_h	expr2_h
285 #define expr_if	expr2_if
286 #define expr_v	expr2_v
287 #define expr_va	expr2_va
288 #define funnel	funnel2
289 #define list	list2
290 
291 #define com1eval	com21eval_ASL
292 #define comeval		com2eval_ASL
293 #define funnelset	fun2set_ASL
294 #undef  r_ops
295 #define r_ops		r2_ops_ASL
296 
297 #ifndef PSHVREAD
298 #define f_OPIFSYM	f2_IFSYM_ASL
299 #define f_OPPLTERM	f2_PLTERM_ASL
300 #define f_OPFUNCALL	f2_FUNCALL_ASL
301 #define f_OP1POW	f2_1POW_ASL
302 #define f_OP2POW	f2_2POW_ASL
303 #define f_OPCPOW	f2_CPOW_ASL
304 #define f_OPPLUS	f2_PLUS_ASL
305 #define f_OPSUMLIST	f2_SUMLIST_ASL
306 #define f_OPHOL		f2_HOL_ASL
307 #define f_OPPOW		f2_POW_ASL
308 #define f_OPVARVAL	f2_VARVAL_ASL
309 #endif
310 
311 /* operation classes (for H*v computation) */
312 
313 enum { /* operation classes (for H*v computation) */
314 	Hv_binaryR	= 0,
315 	Hv_binaryLR	= 1,
316 	Hv_unary	= 2,
317 	Hv_vararg	= 3,
318 	Hv_if		= 4,
319 	Hv_plterm	= 5,
320 	Hv_sumlist	= 6,
321 	Hv_func		= 7,
322 	Hv_negate	= 8,
323 	Hv_plusR	= 9,
324 	Hv_plusL	= 10,
325 	Hv_plusLR	= 11,
326 	Hv_minusR	= 12,
327 	Hv_minusLR	= 13,
328 	Hv_timesR	= 14,
329 	Hv_timesL	= 15,
330 	Hv_timesLR	= 16,
331 	Hv_divLR	= 17
332 	};
333 
334 /* treat if as vararg, minusL as plusL, binaryL as unary */
335 
336 #endif /* SKIP_NL2_DEFINES */
337 
338 #undef f_OPNUM
339 #define f_OPNUM (efunc2*)f_OPNUM_ASL
340 #endif /* NLP_H2_included */
341