Skip to content

Commit b4a34f0

Browse files
committed
added numerical hessian
1 parent cb6568b commit b4a34f0

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

spatialmath/base/numeric.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,37 @@ def numjac(f, x, dx=1e-8, SO=0, SE=0):
5959

6060
return np.c_[Jcol].T
6161

62+
def numhess(J, x, dx=1e-8):
63+
r"""
64+
Numerically compute Hessian of Jacobian function
65+
66+
:param J: the Jacobian function, returns an ndarray(m,n)
67+
:type J: callable
68+
:param x: function argument
69+
:type x: ndarray(n)
70+
:param dx: the numerical perturbation, defaults to 1e-8
71+
:type dx: float, optional
72+
:return: Hessian matrix
73+
:rtype: ndarray(m,n,n)
74+
75+
Computes a numerical approximation to the Hessian for ``J(x)`` where
76+
:math:`f: \mathbb{R}^n \mapsto \mathbb{R}^{m \times n}`
77+
78+
Uses first-order difference :math:`H[:,:,i] = (J(x + dx) - J(x)) / dx`.
79+
"""
80+
81+
I = np.eye(len(x))
82+
Hcol = []
83+
J0 = J(x)
84+
for i in range(len(x)):
85+
86+
Ji = J(x + I[:,i] * dx)
87+
Hi = (Ji - J0) / dx
88+
89+
Hcol.append(Hi)
90+
91+
return np.stack(Hcol, axis=2)
92+
6293
def array2str(X, valuesep=", ", rowsep=" | ", fmt="{:.3g}",
6394
brackets=("[ ", " ]"), suppress_small=True):
6495
"""

0 commit comments

Comments
 (0)