1 /* derivatives - for Bayes code in XLISP-STAT and S */
2 /* XLISP-STAT 2.1 Copyright (c) 1990, by Luke Tierney */
3 /* Additions to Xlisp 2.1, Copyright (c) 1989 by David Michael Betz */
4 /* You may give out copies of this software; for conditions see the */
5 /* file COPYING included with this distribution. */
16 extern double macheps();
23 numergrad(size_t n
, double *x
, double *grad
, double *fsum
,
24 int ffun(double *, double *, double *, double **),
25 double h
, double *typx
)
29 double old_xi
, f1
, f2
, hi
;
31 for (i
= 0; i
< n
; i
++) {
33 hi
= (typx
!= nil
) ? typx
[i
] * h
: h
;
35 (*ffun
)(x
, &f1
, nil
, nil
);
37 (*ffun
)(x
, &f2
, nil
, nil
);
39 grad
[i
] = (f1
- f2
) / (2.0 * hi
);
45 numerhess(size_t n
, double *x
, double **hess
, double f
, double *fsum
,
46 int *ffun(double *, double *, double *, double *),
47 double h
, double *typx
)
50 double old_xi
, old_xj
, f1
, f2
, hi
, hj
;
52 for (i
= 0; i
< n
; i
++) {
53 hi
= (typx
!= nil
) ? typx
[i
] * h
: h
;
54 hess
[i
][i
] = (fsum
[i
] - 2 * f
) / (hi
* hi
);
55 for (j
= i
+ 1; j
< n
; j
++) {
56 hj
= (typx
!= nil
) ? typx
[j
] * h
: h
;
61 (*ffun
)(x
, &f1
, nil
, nil
);
64 (*ffun
)(x
, &f2
, nil
, nil
);
67 hess
[i
][j
] = (2 * f
+ f1
+ f2
- fsum
[i
] - fsum
[j
]) / (2.0 * hi
* hj
);
68 hess
[j
][i
] = hess
[i
][j
];