def LM x0 mu0 eps1 delta2 eps2 func gradfunc gradfunc x0 if np linalg

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
def LM(x0, mu0, eps1, delta2, eps2, M, k, func, gradfunc):
d = gradfunc(x0)
if np.linalg.norm(gradfunc(x0)) < eps1:
return x0, func(x0), 'iter:', k
else:
if k >= M:
return x0, func(x0), 'iter:', k
else:
while (True):
temp1 = Hess(x0)
temp2 = temp1 + mu0*np.ones([n, n])
temp3 = temp2.I
d = -1.*np.dot(temp3, d)
xnext = x0 + d
if func(xnext) < func(x0):
return LM(xnext, mu0/2.0, eps1, delta2, eps2, M, k + 1, func, gradfunc)
mu0 *= 2.0