9 Numpy

Generate matrices A , with random Gaussian entries, B , a Toeplitz matrix, where A 2 R n × m and B 2 R m × m ,

for n = 200, m = 500.


Exercise 9.1: Matrix operations

Calculate A + A, AA>; A>A 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 : k A k F and the infinity norm of B : k B k 1 . 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 corresponding 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 A and finds the element in A 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


Code:

import numpy as np
import math
from scipy import linalg
import random 
import time


n = 200
m = 500
num = n * m
a = []
for i in range(num):
    a.append(random.gauss(0, 1))
A = np.mat(a)
A = A.reshape(n, m)

c = []
r = []
for i in range(m):
    c.append(random.gauss(0, 1))
    r.append(random.gauss(0, 1))
B = linalg.toeplitz(c, r)
B = np.mat(B)

Exercise 9.1: Matrix operations

res1 = A + A
res2 = np.dot(A, (A.T))
res3 = A.T
res4 = np.dot(A, B)
I = np.eye(m)
print(res1)
print(res2)
print(res3)
print(res4)

def solution1(inp):
    B1 = B - inp * I
    return np.dot(A, B1)
Exercise 9.2: Solving a linear system

b = []
for i in range(m):
    b.append(random.gauss(0, 1))
res5 = np.dot(b, (B.I)) 
print(res5)

Exercise 9.3: Norms

A_norm = 0
for i in range(n):
    for j in range(m):
        A_norm = A_norm + A[i, j] * A[i, j]
A_norm = math.sqrt(A_norm)

B_norm = 0
B_max = 0
for i in range(m):
    B_max = 0
    for j in range(m):
        B_max = B_max + B[i, j]
    if B_max > B_norm:
        B_norm = B_max
print(A_norm)
print(B_norm)

P, D, Q = linalg.svd(B)
B_max_svd = max(D)
B_min_svd = min(D)
print(B_max_svd)
print(B_min_svd)

Exercise 9.4: Power iteration

z = []
for i in range(n):
    for j in range(n):
        z.append(random.gauss(0, 1))
Z = np.mat(z)
Z = Z.reshape(n, n)

def power_iteration(Z):
    v_0 = np.ones((1, n))
    v_0 = np.mat(v_0)
    v_0 = v_0.T
    v_1 = np.ones((1, n))
    v_1 = np.mat(v_1)
    v_1 = v_1.T
    m_1 = 1
    m_2 = 0
    counter = 0
    while(abs(m_2 - m_1) > 10 ** -4):
            m_2 = m_1
            u_1 = Z * v_0
            m_1 = max(u_1) 
            v_1 = u_1 / m_1
            v_0 = v_1
            counter = counter + 1
    return counter, m_1, v_1

counter, Z_value, Z_vector = power_iteration(Z)
print(counter)
print(Z_value)
print(Z_vector)
Exercise 9.5: Singular values
def Singular_values(p):
    C = []
    for i in range(n):
        for i in range(n):
            if(p > random.random()):
                C.append(1)
            else:
                C.append(0)
    C = np.mat(C)
    C = C.reshape(n, n)
    U, sigma, V_T = linalg.svd(C)
    C_max_sigma = max(sigma)
    print(p)
    print(sigma)
    print(C_max_sigma)
Exercise 9.6: Nearest neighbor

def Nearest_neighbor(z, A):
    return A[np.argmin(np.abs(A - z))]



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值