# %%

import pickle
import matplotlib.pyplot as plt
import numpy as np

with open('c10p1.pickle', 'rb') as f:
    data = pickle.load(f)

c10p1 = np.array(data['c10p1'])
c10p1 = c10p1 - c10p1.mean(axis=0)
eta = 1.0
alpha = 1.0
dt = 0.01

plt.scatter(c10p1[:,0],c10p1[:,1])

w = np.random.rand(1,2)
for i in range(100000):
  u = c10p1[i%c10p1.shape[1],:]
  v = np.dot(w,u)
  dw = dt * eta * (v * u - alpha * v * v * w)
  w = w + dw

print (w)

d = c10p1 + [6,5]

# print('mean:', m)
plt.scatter(d[:,0],d[:,1])

w = np.random.rand(1,2)
for i in range(100000):
  u = d[i%d.shape[1],:]
  v = np.dot(w,u)
  # Oja
  dw = dt * eta * (v * u - alpha * v * v * w)
  # Hebb
  # dw = dt * eta * (v * u)
  w = w + dw

print (w)

Q = c10p1.T @ c10p1 / 100
evals, evecs = np.linalg.eig(Q)
pc_index = np.argmax(np.abs(evals))
plt.plot([0,evecs[0,0]],[0,evecs[1,0]],c='green')

Q = d.T @ d / 100
evals, evecs = np.linalg.eig(Q)
plt.plot([0,evecs[0,0]],[0,evecs[1,0]],c='red')
print('>>>',evecs[:,0])

plt.show()
