def DFP step x_prev np eye eps1 001 eps2 001 delta 001 1000 -grad if n

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
def DFP(x, step = 0, x_prev = 0, G=np.eye(n), eps1 = 0.001, eps2 = 0.001, delta = 0.001, m = 1000):
d = -grad(x)
if np.linalg.norm(d) < eps1 or step > m:
return f(x), x, step
if step > 0:
delta_g = np.array([grad(x) - grad(x_prev)])
delta_x = np.array([x - x_prev])
G += np.matmul(delta_x.transpose(), delta_x)/np.matmul(delta_x, delta_g.transpose()) - \
np.matmul(np.matmul(np.matmul(G, delta_g.transpose()), delta_g), G)/np.matmul(np.matmul(delta_g , G), delta_g.transpose())
d = np.matmul(-G, np.array([grad(x)]).transpose()).transpose()[0]
alpha = scipy.optimize.minimize_scalar(lambda alpha: f(x + alpha*d), method = 'Golden')
new_x = x + alpha.x*d
if abs(np.linalg.norm(x - new_x)) < delta and abs(f(x) - f(new_x)) < eps2:
return f(new_x), new_x, step
return DFP(new_x, step+1, x, G)