gradient

Numerical derivative implementations.

mr_utils.optimization.gradient.cd_gen_complex_step(f, x0, h=None, v=None)[source]

Compute generalized central difference complex step derivative of f.

Parameters:
  • f (callable) – Function to compute derivative of at x0.
  • x0 (array_like) – Point to compute derivative of f on.
  • h (float, optional) – Real part of forward and backward derivatives.
  • v (float, optional) – Imaginary part of forward and backwards derivatives.
Returns:

g – Gradient of f at x0

Return type:

array_like

Notes

If you choose h, v such that 3*h**2 =/= v**2, there will be an additional error term proportional to 3rd order derivative (not implemented). So it’s in your best interest to choose h, v so this error is minimized. If h=None and v=None these values will be computed for you to satisfy this condition.

Implements Equation 5 from [2].

References

[2]Abreu, Rafael, et al. “On the accuracy of the Complex-Step-Finite-Difference method.” Journal of Computational and Applied Mathematics 340 (2018): 390-403.
mr_utils.optimization.gradient.complex_step_6th_order(f, x0, h=None, v=None)[source]

6th order accurate complex step difference method.

Parameters:
  • f (callable) – Function to compute derivative of at x0.
  • x0 (array_like) – Point to compute derivative of f on.
  • h (float, optional) – Real part of forward and backward derivatives.
  • v (float, optional) – Imaginary part of forward and backwards derivatives.
Returns:

g – Gradient of f at x0

Return type:

array_like

mr_utils.optimization.gradient.fd_complex_step(f, x0, h=2.220446049250313e-16)[source]

Compute forward difference complex step of function f.

Parameters:
  • f (callable) – Function to compute derivative of.
  • x0 (array_like) – Point at which to compute derivate of f.
  • h (float, optional) – Perturbation size.
Returns:

g – Gradient of f at x0

Return type:

array_like

mr_utils.optimization.gradient.fd_gen_complex_step(f, x0, h=0, v=2.220446049250313e-16)[source]

Compute generalized forward difference complex step derivative of f.

Parameters:
  • f (callable) – Function to compute derivative of at x0.
  • x0 (array_like) – Point to compute derivative of f on.
  • h (float, optional) – Real part of forward perturbation.
  • v (float, optional) – Imaginary part of forward perturbation.
Returns:

g – Gradient of f at x0

Return type:

array_like

Notes

Implements Equation 4 from [1].

References

[1]Abreu, Rafael, et al. “On the accuracy of the Complex-Step-Finite-Difference method.” Journal of Computational and Applied Mathematics 340 (2018): 390-403.