Generate matrices A, with random Gaussian entries, B, a Toeplitz matrix, where A ∈Rn×m and B ∈Rm×m, for n = 200, m = 500.
Exercise 9.1: Matrix operationsCalculate A + A, AAT,ATA and AB. Write a function that computes A(B−λI) for any λ.
Exercise 9.2: Solving a linear system
Generate a vector b with m entries and solve Bx = b.
Exercise 9.3: Norms
Compute the Frobenius norm of A: ||A||F and the infinity norm of B: ||B||∞. Also find the largest and smallest singular values of B.
Exercise 9.4: Power iteration
Generate a matrix Z, n × n, with Gaussian entries, and use the power iteration to find the largest eigenvalue and orresponding eigenvector of Z. How many iterations are needed till convergence?
Optional: use the time.clock() method to compare computation time when varying n.
Exercise 9.5: Singular values
Generate an n×n matrix, denoted by C, where each entry is 1 with probability p and 0 otherwise. Use the linear algebra library of Scipy to compute the singular values of C. What can you say about the relationship between n, p and the largest singular value?
Exercise 9.6: Nearest neighbor
Write a function that takes a value z and an array Arr and finds the element in Arr that is closest to z. The function should return the closest value, not index.
Hint: Use the built-in functionality of Numpy rather than writing code to find this value manually. In particular, use brackets and argmin.
1 import numpy as np 2 import random as rd 3 from scipy.linalg import toeplitz 4 5 def E_9_1_1(A): 6 return A + A 7 8 def E_9_1_2(A): 9 return np.dot(A, A.T)10 11 def E_9_1_3(A):12 return np.dot(A.T, A)13 14 def E_9_1_4(A, B):15 return np.dot(A, B)16 17 def E_9_1_5(A, B, l):18 return np.dot(A, (B - l * np.eye(len(B))))19 20 def E_9_2(B, b):21 return np.dot(np.linalg.inv(B), b)22 23 def E_9_3_1(A):24 return np.linalg.norm(A)25 26 def E_9_3_2(B):27 return np.linalg.norm(B, np.inf)28 29 def E_9_3_3(B):30 s = np.linalg.svd(B)[1]31 return (s[0], s[len(s) - 1])32 33 def E_9_4(Z):34 e = np.linalg.eig(Z)35 temp = np.max(e[0])36 for i in range(len(e[0])):37 if (e[0][i] == temp):38 return (e[0][i], tuple(e[1][i]))39 return None40 41 def E_9_5_1(n, p):42 return np.random.binomial(1, p, size = (n, n))43 44 def E_9_5_2(C):45 return np.linalg.svd(C)[1]46 47 def E_9_6(Arr, z):48 return Arr[np.argmin(np.abs(Arr - z))]49 50 #n = 251 #m = 552 n = 20053 m = 50054 A = np.random.normal(size = (n, m))55 B_ = np.random.randn(m * 2)56 B = toeplitz(B_[: m], B_[: 1] + B_[m : m * 2])57 l = rd.normalvariate(0, 1)58 b = np.random.normal(size = (m, 1))59 Z = np.random.normal(size = (n, n))60 p = rd.random()61 z = rd.normalvariate(0, 1)62 Arr = np.random.randn(m)63 64 print("A =", A)65 print("B =", B)66 print("λ =", l)67 print("b =", b)68 print("Z =", Z)69 print("p =", p)70 print("z =", z)71 print("Arr =", Arr)72 73 print("\nExercise 9.1: Matrix operations")74 print("A + A =", E_9_1_1(A))75 print("AAᵀ =", E_9_1_2(A))76 print("AᵀA =", E_9_1_3(A))77 print("AB =", E_9_1_4(A, B))78 print("A(B−λI) =", E_9_1_5(A, B, l))79 80 print("\nExercise 9.2: Solving a linear system")81 print("x =", E_9_2(B, b))82 83 print("\nExercise 9.3: Norms")84 print("||A||F =", E_9_3_1(A))85 print("||A||∞ =", E_9_3_2(B))86 print("(max{singular values of B}, min{singular values of B}) =", E_9_3_3(B))87 88 print("\nExercise 9.4: Power iteration")89 print("(max{λ(Z)}, orresponding eigenvector of Z) =", E_9_4(Z))90 91 print("\nExercise 9.5: Singular values")92 C = E_9_5_1(n, p)93 print("C =", C)94 print("singular values of C =", E_9_5_2(C))95 96 print("\nExercise 9.6: Nearest neighbor")97 print("the closest value of Arr to z =", E_9_6(Arr, z))