Calculates first- and second-order numerical approximations of the partial derivatives at the current values \(x = (x_1, \ldots, x_N)\) of the variables, using the central difference formulas
$$f'_i(x) \approx \frac{f(x_1, \ldots, x_i + d_i, \ldots, x_N) - f(x_1, \ldots, x_i - d_i, \ldots, x_N)}{2d_i}$$
$$f''_{ii}(x) \approx \frac{f(x_1, \ldots, x_i + d_i, \ldots, x_N) - 2f(x_1, \ldots, x_N) + f(x_1, \ldots, x_i - d_i, \ldots, x_N)}{d_i^2}$$
and, for the mixed partial derivatives (\(i \neq j\)), with \(x^{\pm}\) = \(x\) with \(x_i \pm d_i\) and \(x_j \pm d_j\),
$$f''_{ij}(x) \approx \frac{f(x^{+}) - 2f(x) + f(x^{-}) - f''_{ii}(x)\,d_i^2 - f''_{jj}(x)\,d_j^2}{2 d_i d_j}.$$
These approximations are refined by Richardson extrapolation, as in genD of the 'numDeriv' package: each is computed for \(r = 4\) step sizes, starting with \(d_i = 0.1|x_i|\) (plus \(10^{-4}\) if \(x_i\) is close to 0) and halving the step each time, and the \(r\) values \(D_1, \ldots, D_r\) are combined successively for \(m = 1, \ldots, r - 1\) by \(D_k \leftarrow (4^m D_{k+1} - D_k)/(4^m - 1)\).