import numpy as np
import matplotlib.pyplot as plt
import numpy.linalg as alg

A = np.array([[2,4,5],[4,0,-2],[5,-2,4]])/12

# 2.a.1
X = np.array([[1,0,0]]).T
Y = A.dot(X)
L = [np.amax(abs(Y))]
for k in range(2,501):
    Y = A.dot(Y)
    L.append(np.amax(abs(Y))**(1/k))
plt.figure('2.a.1')
plt.plot(L)
plt.show()

# 2.a.2
X = np.array([[1,1,1]]).T
Y = A.dot(X)
L = [np.amax(abs(Y))]
for k in range(2,501):
    Y = A.dot(Y)
    L.append(np.amax(abs(Y))**(1/k))
plt.figure('2.a.2')
plt.plot(L)
plt.show()

# 2.a.3
X = np.array([[1,0,-1]]).T
Y = A.dot(X)
L = [np.amax(abs(Y))]
for k in range(2,501):
    Y = A.dot(Y)
    L.append(np.amax(abs(Y))**(1/k))
plt.figure('2.a.3')
plt.plot(L)
plt.show()

# 2.b
P = np.array([[1,0,-1],[1,2,1],[1,-1,0]]).T
print(alg.det(P))
B = alg.inv(P).dot(A).dot(P)
print(B)

# 2.c
W = np.array([[1,0.1,0.01]]).T
X = alg.inv(P).dot(W)
print(X)

# 2.d
Y = A.dot(W)
L1 = [np.amax(abs(Y))]
for k in range(2,501):
    Y = A.dot(Y)
    L1.append(np.amax(abs(Y))**(1/k))

Y = B.dot(X)
L2 = [np.amax(abs(Y))]
for k in range(2,501):
    Y = B.dot(Y)
    L2.append(np.amax(abs(Y))**(1/k))

plt.plot(L1, color='r')
plt.plot(L2, color='b')
plt.show()

# 3
print(alg.eigvals(A))
