うまくいかなかったコード。
自分で見直すように残しておく。
class MultiNewton(object):
... def init(self,f,dx0f,dx1f,grad_f,hesse):
... self.f=f
... self.dx0f=dx0f
... self.dx1f=dx1f
... self.grad_f=grad_f
... self.hesse=hesse
... def _compute_dx(self,bar_x):
... grad=self.grad_f(bar_x)
... hesse=self.hesse(bar_x)
... dx=np.linalg.solve(hesse,-grad)
... return dx
... def solve(self,init_x,n_iter,tol,step_width):
... bar_x=init_x
... for i in range(n_iter):
... dx=self._compute_dx(bar_x)
... x=bar_x+dx*step_width
... bar_x=x
... norm_dx=np.linalg.norm(dx)
... if norm_dx<tol:
... break
... return x
...
multinewton=MultiNewton(f=f,dx0f=dx0f,dx1f=dx1f,grad_f=grad_f,hesse=hesse)
multinewton.solve(init_x=np.array([10.0,8.0]),n_iter=100,tol=0.01,step_width=1.0)
Traceback (most recent call last):
File "", line 1, in
File "", line 16, in solve
File "", line 11, in _compute_dx
TypeError: bad operand type for unary -: 'function'