import numpy as np


def table4_13():
    # QR method

    # matrix
    A = np.zeros((2, 2))
    A[0, 0] = 2
    A[0, 1] = 1
    A[1, 0] = 1
    A[1, 1] = 2

    y = A[:, 0].copy()
    print("y =")
    print(y)
    z = A[:, 1].copy()
    print("z =")
    print(z)

    print("\n k  \t   lambda_1 \t   lambda_2 ")
    K = 9
    for k in range(1, K + 1):
        e0 = y.copy()
        e1 = z - np.dot(z, e0) * e0 / np.dot(e0, e0)
        q0 = e0 / np.linalg.norm(e0, 2)
        q1 = e1 / np.linalg.norm(e1, 2)
        Q = np.column_stack((q0, q1))
        R = Q.T @ np.column_stack((y, z))
        Z = R @ Q
        y = Z[:, 0].copy()
        z = Z[:, 1].copy()
        eigens = np.array([Z[0, 0], Z[1, 1]])
        print(f"{k:d} & {Z[0, 0]:15.14f} & {Z[1, 1]:15.14f} ")
    print()


if __name__ == "__main__":
    table4_13()
