# How to use the numdifftools.nd_cstep.Hessian function in numdifftools

## To help you get started, weâ€™ve selected a few numdifftools examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

pbrod / numdifftools / numdifftools / nd_cstep.py View on Github
``````print('\n2*np.dot(x.T, x)')
print(2 * np.dot(x.T, x))
jac2 = (jac + jacmin) / 2.
print(np.dot(jac2.T, jac2))

# he = approx_hess(xk,fun2,steps,*args)
print(Hessian(fun2, 1e-3, method='central2')(xk, *args))
he = Hessian(fun2, method='central2')(xk, *args)
print('hessfd')
print(he)
print('base_step =', None)
print(he - 2 * np.dot(x.T, x))

for eps in [1e-3, 1e-4, 1e-5, 1e-6]:
print('eps =', eps)
print(Hessian(fun2, eps, method='central2')(xk, *args) -
2 * np.dot(x.T, x))

hcs2 = Hessian(fun2, method='hybrid')(xk, *args)
print('hcs2')
print(hcs2 - 2 * np.dot(x.T, x))

hfd3 = Hessian(fun2, method='central')(xk, *args)
print('hfd3')
print(hfd3 - 2 * np.dot(x.T, x))

hfi = []
epsi = np.array([1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6]) * 10.
for eps in epsi:
h = eps * np.maximum(np.log1p(np.abs(xk)), 0.1)
hfi.append(Hessian(fun2, h, method='hybrid')(xk, *args))
print('hfi, eps =', eps)``````
pbrod / numdifftools / numdifftools / nd_cstep.py View on Github
``````print(Hessian(fun2, eps, method='central2')(xk, *args) -
2 * np.dot(x.T, x))

hcs2 = Hessian(fun2, method='hybrid')(xk, *args)
print('hcs2')
print(hcs2 - 2 * np.dot(x.T, x))

hfd3 = Hessian(fun2, method='central')(xk, *args)
print('hfd3')
print(hfd3 - 2 * np.dot(x.T, x))

hfi = []
epsi = np.array([1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6]) * 10.
for eps in epsi:
h = eps * np.maximum(np.log1p(np.abs(xk)), 0.1)
hfi.append(Hessian(fun2, h, method='hybrid')(xk, *args))
print('hfi, eps =', eps)
print(hfi[-1] - 2 * np.dot(x.T, x))

import numdifftools as nd
print('Dea3')
err = 1000 * np.ones(hfi[0].shape)
val = np.zeros(err.shape)
errt = []
for i in range(len(hfi) - 2):
tval, terr = nd.dea3(hfi[i], hfi[i + 1], hfi[i + 2])
errt.append(terr)
k = np.flatnonzero(terr &lt; err)
if k.size &gt; 0:
np.put(val, k, tval.flat[k])
np.put(err, k, terr.flat[k])
print(val - 2 * np.dot(x.T, x))``````
pbrod / numdifftools / numdifftools / nd_cstep.py View on Github
``````def __init__(self, f, step=None, method='central', full_output=False):
order = dict(backward=1, forward=1, complex=2).get(method, 2)
super(Hessian, self).__init__(f, n=2, step=step, method=method,
order=order, full_output=full_output)``````
pbrod / numdifftools / numdifftools / nd_cstep.py View on Github
``````he = Hessian(fun2, method='central2')(xk, *args)
print('hessfd')
print(he)
print('base_step =', None)
print(he - 2 * np.dot(x.T, x))

for eps in [1e-3, 1e-4, 1e-5, 1e-6]:
print('eps =', eps)
print(Hessian(fun2, eps, method='central2')(xk, *args) -
2 * np.dot(x.T, x))

hcs2 = Hessian(fun2, method='hybrid')(xk, *args)
print('hcs2')
print(hcs2 - 2 * np.dot(x.T, x))

hfd3 = Hessian(fun2, method='central')(xk, *args)
print('hfd3')
print(hfd3 - 2 * np.dot(x.T, x))

hfi = []
epsi = np.array([1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6]) * 10.
for eps in epsi:
h = eps * np.maximum(np.log1p(np.abs(xk)), 0.1)
hfi.append(Hessian(fun2, h, method='hybrid')(xk, *args))
print('hfi, eps =', eps)
print(hfi[-1] - 2 * np.dot(x.T, x))

import numdifftools as nd
print('Dea3')
err = 1000 * np.ones(hfi[0].shape)
val = np.zeros(err.shape)
errt = []``````
pbrod / numdifftools / numdifftools / nd_cstep.py View on Github
``````from scipy import optimize
_xfmin = optimize.fmin(fun2, (0, 0, 0), args)  # @UndefinedVariable
# print(approx_fprime((1, 2, 3), fun, steps, x))
jac = Gradient(fun1, epsilon, method='forward')(xk, *args)
jacmin = Gradient(fun1, -epsilon, method='forward')(xk, *args)
# print(jac)
print(jac.sum(0))
print('\nnp.dot(jac.T, jac)')
print(np.dot(jac.T, jac))
print('\n2*np.dot(x.T, x)')
print(2 * np.dot(x.T, x))
jac2 = (jac + jacmin) / 2.
print(np.dot(jac2.T, jac2))

# he = approx_hess(xk,fun2,steps,*args)
print(Hessian(fun2, 1e-3, method='central2')(xk, *args))
he = Hessian(fun2, method='central2')(xk, *args)
print('hessfd')
print(he)
print('base_step =', None)
print(he - 2 * np.dot(x.T, x))

for eps in [1e-3, 1e-4, 1e-5, 1e-6]:
print('eps =', eps)
print(Hessian(fun2, eps, method='central2')(xk, *args) -
2 * np.dot(x.T, x))

hcs2 = Hessian(fun2, method='hybrid')(xk, *args)
print('hcs2')
print(hcs2 - 2 * np.dot(x.T, x))

hfd3 = Hessian(fun2, method='central')(xk, *args)``````
pbrod / numdifftools / numdifftools / nd_cstep.py View on Github
``````_xfmin = optimize.fmin(fun2, (0, 0, 0), args)  # @UndefinedVariable
# print(approx_fprime((1, 2, 3), fun, steps, x))
jac = Gradient(fun1, epsilon, method='forward')(xk, *args)
jacmin = Gradient(fun1, -epsilon, method='forward')(xk, *args)
# print(jac)
print(jac.sum(0))
print('\nnp.dot(jac.T, jac)')
print(np.dot(jac.T, jac))
print('\n2*np.dot(x.T, x)')
print(2 * np.dot(x.T, x))
jac2 = (jac + jacmin) / 2.
print(np.dot(jac2.T, jac2))

# he = approx_hess(xk,fun2,steps,*args)
print(Hessian(fun2, 1e-3, method='central2')(xk, *args))
he = Hessian(fun2, method='central2')(xk, *args)
print('hessfd')
print(he)
print('base_step =', None)
print(he - 2 * np.dot(x.T, x))

for eps in [1e-3, 1e-4, 1e-5, 1e-6]:
print('eps =', eps)
print(Hessian(fun2, eps, method='central2')(xk, *args) -
2 * np.dot(x.T, x))

hcs2 = Hessian(fun2, method='hybrid')(xk, *args)
print('hcs2')
print(hcs2 - 2 * np.dot(x.T, x))

hfd3 = Hessian(fun2, method='central')(xk, *args)
print('hfd3')``````

## numdifftools

Solves automatic numerical differentiation problems in one or more variables.

BSD-3-Clause