import numpy import math from scipy optimize import minimize_scalar la

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import numpy
import math
from scipy.optimize import minimize_scalar
z = lambda a: a[0] ** 2 + a[1] ** 2
z_grad = lambda a: numpy.array([2 * a[0], 2 * a[1]])
def mininize(a):
global z
l_min = minimize_scalar(lambda l: z(a - l * z_grad(a))).x
return a - l_min * z_grad(a)
def norm(a):
return math.sqrt(a[0] ** 2 + a[1] ** 2)
def grad_step(dot):
return mininize(dot)
dot1 = numpy.array([1.0, 1.0])
dot2 = grad_step(dot1)
eps = 0.001
while norm(dot1 - dot2) < eps:
dot1 = dot2
dot2 = grad_step(dot2)
print dot2
print dot2