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. */
8 extern double macheps();
10 typedef double **RMatrix
, *RVector
;
16 numergrad(n
, x
, grad
, fsum
, ffun
, h
, typx
)
18 RVector x
, grad
, fsum
, typx
;
23 double old_xi
, f1
, f2
, hi
;
25 for (i
= 0; i
< n
; i
++) {
27 hi
= (typx
!= nil
) ? typx
[i
] * h
: h
;
29 (*ffun
)(x
, &f1
, nil
, nil
);
31 (*ffun
)(x
, &f2
, nil
, nil
);
33 grad
[i
] = (f1
- f2
) / (2.0 * hi
);
38 numerhess(n
, x
, hess
, f
, fsum
, ffun
, h
, typx
)
40 RVector x
, fsum
, typx
;
46 double old_xi
, old_xj
, f1
, f2
, hi
, hj
;
48 for (i
= 0; i
< n
; i
++) {
49 hi
= (typx
!= nil
) ? typx
[i
] * h
: h
;
50 hess
[i
][i
] = (fsum
[i
] - 2 * f
) / (hi
* hi
);
51 for (j
= i
+ 1; j
< n
; j
++) {
52 hj
= (typx
!= nil
) ? typx
[j
] * h
: h
;
57 (*ffun
)(x
, &f1
, nil
, nil
);
60 (*ffun
)(x
, &f2
, nil
, nil
);
63 hess
[i
][j
] = (2 * f
+ f1
+ f2
- fsum
[i
] - fsum
[j
]) / (2.0 * hi
* hj
);
64 hess
[j
][i
] = hess
[i
][j
];