"""
Linear algebra homework problem 2
"""
import numpy as np

np.set_printoptions(precision=2)

E = 3
t = 1

T = np.array([
    [E - t, t, 0, 0, 0],
    [t, E - t, t, 0, 0],
    [0, t, E - t, t, 0],
    [0, 0, t, E - t, t],
    [0, 0, 0, t, E - t]
])
C = np.array([
    [E - t, t, 0, 0, t],
    [t, E - t, t, 0, 0],
    [0, t, E - t, t, 0],
    [0, 0, t, E - t, t],
    [t, 0, 0, t, E - t]
])
T_inv = np.linalg.inv(T)
C_inv = np.linalg.inv(C)

# Evaluate the Sherman-Morrison formula for the bottom-left corner
u1 = np.array([0, 0, 0, 0, t])
v1 = np.array([1, 0, 0, 0, 0])
z1 = T_inv @ u1
w1 = T_inv.T @ v1
lam1 = np.dot(v1, z1)
sm1 = - np.tensordot(z1, w1, axes=0) / (1 + lam1)
C_sm = T_inv + sm1

# Evaluate the Sherman-Morrison formula for the top-right corner
u2 = np.array([t, 0, 0, 0, 0])
v2 = np.array([0, 0, 0, 0, 1])
z2 = C_sm @ u2
w2 = C_sm.T @ v2
lam2 = np.dot(v2, z2)
sm2 = - np.tensordot(z2, w2, axes=0) / (1 + lam2)
C_sm = C_sm + sm2

d_inv = T_inv - C_inv
d_sm = T_inv - C_sm


if __name__ == '__main__':
    print('COMMIT!!!!')
    print(f"Standard inversion:\n"
          f"C_inverse:\n{C_inv}\n"
          f"Delta:\n{d_inv}\n"
          f"\n"
          f"Sherman-Morrison:\n"
          f"C_inverse:\n{C_sm}\n"
          f"Delta:\n{d_sm}")
