博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Numpy
阅读量:5112 次
发布时间:2019-06-13

本文共 3777 字,大约阅读时间需要 12 分钟。

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 operations

  Calculate 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))

转载于:https://www.cnblogs.com/Efve/p/9075429.html

你可能感兴趣的文章
引用 移植Linux到s3c2410上
查看>>
人与人之间的差距是从大学开始的
查看>>
MySQL5.7开多实例指导
查看>>
[51nod] 1199 Money out of Thin Air #线段树+DFS序
查看>>
poj1201 查分约束系统
查看>>
Red and Black(poj-1979)
查看>>
分布式锁的思路以及实现分析
查看>>
腾讯元对象存储之文件删除
查看>>
jdk环境变量配置
查看>>
安装 Express
查看>>
包含列的索引:SQL Server索引的阶梯级别5
查看>>
myeclipse插件安装
查看>>
浙江省第十二届省赛 Beauty of Array(思维题)
查看>>
NOIP2013 提高组 Day1
查看>>
个人对vue生命周期的理解
查看>>
cocos2dx 3.x simpleAudioEngine 长音效被众多短音效打断问题
查看>>
存储(硬件方面的一些基本术语)
查看>>
观察者模式
查看>>
Weka中数据挖掘与机器学习系列之基本概念(三)
查看>>
Win磁盘MBR转换为GUID
查看>>