import numpy as np
import matplotlib.pyplot as plt


x = np.array([2., 2.])
d1 = np.array([2 * 4 + 2 * 4 - 8, 2 * 4 + 2 * 10 - 6])
d2 = -d1


f = lambda x: 2 * x[0] ** 2 + 4 * x[0] * x[1] + 5 * x[1] ** 2 - 8 * x[0] - 6 * x[1] + 10
    
f0 = f(x)
theta = np.linspace(0, 2 * np.pi, 181) 
unit_directions = np.array([(np.cos(t), np.sin(t)) for t in theta])


fig = plt.figure(figsize=(5, 5), dpi=500)
ax= fig.gca()
ax.set_aspect('equal')


# X = [d[0] for d in unit_directions]
# Y = [d[1] for d in unit_directions]


# plt.plot(X, Y)


most_decrease = 0
for d in unit_directions: 
    f_new = f(x + 0.01 * d)
    decrease = f0 - f_new
    
    
    if decrease > most_decrease:
        most_decrease = decrease
        d_most = d


    if decrease > 0:
        length = decrease
        arrow = length * d
        plt.arrow(x[0], x[1], arrow[0], arrow[1],
                  head_width=0.005, head_length=0.005, fc='r', ec='r', lw=0.1)
    


arrow_end = x + 0.012 * d2
plt.arrow(x[0], x[1], arrow_end[0] - x[0], arrow_end[1] - x[1],
          head_width=0.01, head_length=0.01, fc='darkblue', ec='darkblue', lw=0.01)
