pysolvegn.build_numerical_jacobian#

build_numerical_jacobian(residual_func, method='central', epsilon=None)[source]#

Build a Jacobian function for the Gauss-Newton optimization by computing the numerical derivatives of the residual function with respect to the parameters using finite differences.

The step of the parameter \(j\) is relative to its magnitude: \(h_j = \epsilon \max(1, |p_j|)\). The step actually used is the representable one, \(h_j = (p_j + h_j) - p_j\), which removes the rounding error on the step.

With the "central" method (2 n_parameters calls of residual_func), the Jacobian is computed as:

\[J_{i,j} = \frac{R_i(p + h_j e_j) - R_i(p - h_j e_j)}{2 h_j}\]

With the "forward" method (n_parameters + 1 calls), the Jacobian is computed as:

\[J_{i,j} = \frac{R_i(p + h_j e_j) - R_i(p)}{h_j}\]

With the "backward" method (n_parameters + 1 calls), the Jacobian is computed as:

\[J_{i,j} = \frac{R_i(p) - R_i(p - h_j e_j)}{h_j}\]
Parameters:
  • residual_func (Callable) – A function that computes the residuals for a given set of parameters. The function should take a 1D array of parameters as input and return a 1D array of residuals.

  • method (str, optional (default="central")) – The finite difference method to use for computing the numerical Jacobian. Must be one of “central”, “forward”, or “backward”.

  • epsilon (Optional[Real], optional (default=None)) – The relative step \(\epsilon\) of the finite differences (finite and strictly positive). If None, the optimal order of magnitude for residuals computed to the machine precision \(\varepsilon_m \approx 2.2 \times 10^{-16}\) is used: \(\sqrt{\varepsilon_m} \approx 1.5 \times 10^{-8}\) for "forward" and "backward", \(\varepsilon_m^{1/3} \approx 6.1 \times 10^{-6}\) for "central". Use a larger value if the residuals are noisy or computed with a lower precision.

Returns:

  • jacobian_func (Callable) – A function that computes the Jacobian matrix for a given set of parameters. The function takes a 1D array of parameters as input and returns a 2D array representing the Jacobian matrix.

  • Version

  • ——-

  • - 0.0.1 (Initial version.)

  • - 0.0.2 (Renamed from build_jacobian to build_numerical_jacobian for clarity.)

  • - 1.0.0 (Added support for forward, backward, and central finite difference methods for numerical Jacobian computation.)

  • - 1.1.0 (Default step depending on the method (epsilon=None), representable steps,) – the residuals at p are computed only once ("forward" / "backward") and not at all ("central").

Return type:

Callable