import numpy as np


def table4_11(ic=2):
    # %% ic=1: randon starting matrix
    # %% ic=2: Vandermonde matrix

    # interations for increasing size of matrix
    print("\n n  \t   RGS \t\t  MGS  \t\t kappa ", end="")
    for in_value in range(1, 11):
        if ic == 1:
            n = 2**in_value
            B = np.random.rand(n, n)
        else:
            n = 2 * in_value
            B = np.zeros((n, n))
            for i in range(1, n + 1):
                a = 1 / i
                B[i - 1, 0] = 1
                for j in range(2, n + 1):
                    B[i - 1, j - 1] = a ** (j - 1)

        # iterations for two procedures
        BB = B.copy()
        c = np.linalg.cond(B, np.inf)
        B[:, 0] = B[:, 0] / np.linalg.norm(B[:, 0], 2)
        BB[:, 0] = B[:, 0]
        for j in range(1, n):
            # GS version
            e = B[:, j].copy()
            for k in range(j):
                r = np.dot(B[:, j], B[:, k])
                e = e - r * B[:, k]
            # MGS version
            B[:, j] = e / np.linalg.norm(e, 2)
            ee = BB[:, j].copy()
            for k in range(j):
                r = np.dot(ee, BB[:, k])
                ee = ee - r * BB[:, k]
            BB[:, j] = ee / np.linalg.norm(ee, 2)

        a = np.linalg.norm(B.T @ B - np.eye(n), 2)
        aa = np.linalg.norm(BB.T @ BB - np.eye(n), 2)

        print(f"\n {n:d} \t {a:5.2e}  \t {aa:5.2e}  \t {c:5.2e}", end="")
        # print(f"\n {n:d} & {a:5.2e}  & {aa:5.2e}  & {c:5.2e}", end="")
    print("\n")


if __name__ == "__main__":
    table4_11()
