
A1.4 Calling Ox-coded functions from C 9
static OxVALUE *s_pvOxFunc; /* Ox code function to call */
static int myFunc(int cP, VECTOR vP, double *pdFunc,
VECTOR vScore, MATRIX mHess)
{
OxVALUE rtn, arg, *prtn, *parg;
prtn = &rtn; parg = &arg;
OxSetMatPtr(parg, 0, &vP, 1, cP);
if (!FOxCallBack(s_pvOxFunc, prtn, parg, 1))
return 1;
OxLibCheckType(OX_DOUBLE, prtn, 0, 0);
*pdFunc = OxDbl(prtn, 0);
return 0;
}
void OXCALL FnNumDer(OxVALUE *rtn, OxVALUE *pv, int cArg)
{
int c;
OxLibCheckType(OX_FUNCTION, pv, 0, 0);
s_pvOxFunc = pv; /* function pointer */
OxLibCheckType(OX_MATRIX, pv, 1, 1);
c = OxMatc(pv, 1);
OxLibCheckMatrixSize(pv, 1, 1, 1, c);
OxLibValMatMalloc(rtn, 1, c);
if (!FNum1Derivative(
myFunc, c, OxMat(pv, 1)[0], OxMat(rtn, 0)[0]))
{
OxFreeByValue(rtn);
OxZero(rtn, 0);
}
}
.......................................................................................
First we discuss FnNumDer which performs the actual numerical differentiation by call-
ing FNum1Derivative:
• Argument 0 in pv must be a function, argument 1 a matrix, from which we only
use the first row (expected to hold the parameter values at which to differentiate).
The function argument is stored in the global variable s
pvOxFunc, so that it can
be used later.
• OxLibCheckMatrixSize checks whether the matrix is 1×c (since the c value is
taken from that matrix, only the number of rows is checked).
• Finally, the C function FNum1Derivative is called to compute the numerical
derivative of myFunc. When successful, it will leave the result in the first row of
the matrix in rtn (for which we have already allocated the space).
Commentaires sur ces manuels