1 /* cbayes - Lisp interface to laplace approximation stuff */
2 /* Copyright (c) 1990, by Luke Tierney */
12 extern char *calloc(), *realloc();
14 /************************************************************************/
16 /** Definitions and Globals **/
18 /************************************************************************/
22 static char *mem
[MAXALLOC
], memcount
;
25 int n
, m
, k
, itnlimit
, backtrack
, verbose
, vals_suppl
, exptilt
;
30 double typf
, h
, gradtol
, steptol
, maxstep
, dflt
, tilt
, newtilt
, hessadd
;
43 /************************************************************************/
45 /** Fake Replacements for S Interface **/
47 /************************************************************************/
51 static inited
= FALSE
;
55 for (i
= 0; i
< MAXALLOC
; i
++) mem
[i
] = nil
;
62 static makespace(pptr
, size
)
66 if (size
<= 0) return;
67 if (*pptr
== nil
) *pptr
= calloc(size
, 1);
68 else *pptr
= realloc(*pptr
, size
);
69 if (size
> 0 && *pptr
== nil
) xlfail("memory allocation failed");
72 call_S(fun
, narg
, args
, mode
, length
, names
, nvals
, values
)
73 char *fun
, **args
, **mode
, **names
, **values
;
74 long narg
, nvals
, *length
;
76 int n
= length
[0], derivs
;
77 static double *fval
= nil
, *grad
= nil
, *hess
= nil
;
79 makespace(&fval
, 1 * sizeof(double));
80 makespace(&grad
, n
* sizeof(double));
81 makespace(&hess
, n
* n
* sizeof(double));
83 callLminfun(n
, args
[0], fval
, grad
, hess
, &derivs
);
85 values
[0] = (char *) fval
;
86 values
[1] = (derivs
> 0) ? (char *) grad
: nil
;
87 values
[2] = (derivs
> 1) ? (char *) hess
: nil
;
96 /************************************************************************/
98 /** Callback Function **/
100 /************************************************************************/
102 static callLminfun(n
, x
, fval
, grad
, hess
, derivs
)
104 RVector x
, grad
, hess
;
107 maximize_callback(n
, (PTR
) x
,
108 (PTR
) fval
, (PTR
) grad
, (PTR
) hess
, (PTR
) derivs
);
111 /************************************************************************/
113 /** Numerical Derivatives **/
115 /************************************************************************/
117 numgrad_front(n
, px
, pgrad
, h
, pscale
)
119 PTR px
, pgrad
, pscale
;
125 evalfront(&f
, &n
, (double *) px
,
126 &fval
, (double *) pgrad
, nil
, &h
, (double *) pscale
);
129 numhess_front(n
, px
, pf
, pgrad
, phess
, h
, pscale
)
131 PTR px
, pf
, pgrad
, phess
, pscale
;
136 evalfront(&f
, &n
, (double *) px
,
137 (double *) pf
, (double *) pgrad
, (double *) phess
,
138 &h
, (double *) pscale
);
141 /************************************************************************/
143 /** Maximization Interface **/
145 /************************************************************************/
147 /* internals array information */
162 static MaxIPars
getMaxIPars(ipars
)
170 ip
.itnlimit
= ipars
[3];
171 ip
.backtrack
= ipars
[4];
172 ip
.verbose
= ipars
[5];
173 ip
.vals_suppl
= ipars
[6];
174 ip
.exptilt
= ipars
[7];
176 ip
.termcode
= ipars
[9];
181 static MaxDPars
getMaxDPars(dpars
)
188 dp
.gradtol
= dpars
[2];
189 dp
.steptol
= dpars
[3];
190 dp
.maxstep
= dpars
[4];
193 dp
.newtilt
= dpars
[7];
194 dp
.hessadd
= dpars
[8];
199 minfo_maximize(px
, pfvals
, pscale
, pip
, pdp
, verbose
)
200 PTR px
, pfvals
, pscale
, pip
, pdp
;
207 static double *dx
, *typx
, *fvals
;
211 typx
= (double *) pscale
;
212 fvals
= (double *) pfvals
;
214 ip
= getMaxIPars((int *) pip
);
215 dp
= getMaxDPars((double *) pdp
);
220 if (verbose
>= 0) ip
.verbose
= verbose
;
223 maxfront(&f
, nil
, nil
, dx
, typx
, fvals
, nil
, nil
, nil
,
224 &ip
, &dp
, nil
, &msg
);