Exercise 9.1: Matrix operations
题目描述
Calculate A + A, AA^T, A^TA and AB. Write a function that computes A(B -tI) for any t.
输入样例
3
输出样例
A + A = [[-0.59334828 0.8884575 -1.36523816 ... -2.02030659 0.19453275
2.11587215]
[-2.81791186 -2.88267176 -1.03896815 ... 0.50513996 1.49173622
-0.33268223]
[ 1.81893223 -0.97778773 -0.24765626 ... -0.14222241 1.10786192
-4.35427287]
...
[ 2.58180949 0.43558676 0.72247307 ... -1.4871005 0.04631172
-0.59709626]
[ 1.5098593 -1.27361792 2.5884521 ... 1.89708659 0.98811815
0.28697544]
[-7.64837192 -0.14700114 -1.58280627 ... 2.32408483 2.58086481
-2.06438574]]
A * A^T = [[470.44338692 42.29280712 17.10223612 ... 18.91100284 15.67769139
40.75185721]
[ 42.29280712 475.70038961 -11.58347091 ... -15.97459961 16.07904308
-11.8156372 ]
[ 17.10223612 -11.58347091 471.39535028 ... -3.54325763 -22.74446501
5.16384204]
...
[ 18.91100284 -15.97459961 -3.54325763 ... 458.76332228 23.34234596
-1.45594504]
[ 15.67769139 16.07904308 -22.74446501 ... 23.34234596 472.47156727
-13.31998639]
[ 40.75185721 -11.8156372 5.16384204 ... -1.45594504 -13.31998639
511.43900327]]
A^T * A = [[232.80738856 7.95941402 15.37981253 ... -14.05881828 -20.71737172
-13.49575832]
[ 7.95941402 186.35319519 20.66773885 ... 3.39549772 -10.86798494
10.8023167 ]
[ 15.37981253 20.66773885 200.55277055 ... -8.24671455 -23.38532638
3.60540781]
...
[-14.05881828 3.39549772 -8.24671455 ... 204.24581984 -15.25325464
-0.83971902]
[-20.71737172 -10.86798494 -23.38532638 ... -15.25325464 191.8058579
-4.18234635]
[-13.49575832 10.8023167 3.60540781 ... -0.83971902 -4.18234635
200.11400152]]
A * B = [[-104.11890528 -127.50165662 -74.0060889 ... -73.41391463
-27.45200506 -97.81103017]
[ 17.43003568 132.62518782 92.59608437 ... 57.63015635
123.20641659 30.82112076]
[ 173.68506942 176.05025446 117.74589784 ... 189.7801047
127.43363064 118.65265685]
...
[-190.80779557 -75.98950803 -15.5155604 ... -179.06148314
-154.89361858 -84.96796462]
[ -3.34434955 -124.00123772 -45.13904847 ... -135.10616698
-87.78819411 -75.75593386]
[ -15.47863655 -33.75863698 59.53654699 ... 106.40775681
57.62160209 -60.303528 ]]
Please enter t:
3
A * (B - tI) = [[-103.22888286 -128.83434287 -71.95823165 ... -70.38345474
-27.74380419 -100.98483839]
[ 21.65690347 136.94919546 94.1545366 ... 56.8724464
120.96881226 31.32014411]
[ 170.95667108 177.51693606 118.11738223 ... 189.99343831
125.77183776 125.18406616]
...
[-194.6805098 -76.64288816 -16.59927001 ... -176.83083239
-154.96308616 -84.07232023]
[ -5.6091385 -122.09081083 -49.02172662 ... -137.95179686
-89.27037133 -76.18639703]
[ -4.00607867 -33.53813526 61.9107564 ... 102.92162957
53.75030488 -57.20694939]]
代码展示
import numpy as np
from scipy.linalg import toeplitz
A = np.mat(np.random.randn(200, 500))
B = np.mat(toeplitz([np.random.randint(0,10) for i in range(500)]))
print("A + A = ",end="")
print(A + A)
print("A * A^T = ",end="")
print(A * (A.T))
print("A^T * A = ",end="")
print(A.T * A)
print("A * B = ",end="")
print(A * B)
t = (float)(input("Please enter t: \n"))
C = B
for i in range(500):
C[i, i] = C[i , i] - t
print("A * (B - tI) = ",end="")
print(A * C)
Exercise 9.2: Solving a linear system
题目描述
Generate a vector b with m entries and solve Bx = b.
输入样例
none
输出样例
b = [[3]
[1]
[2]
[4]
......
[5]
x = [ 1.15838139e+00]
[-6.09815984e-01]
[ 1.15685576e+00]
......
[-1.38682161e-01]]
代码展示
import numpy as np
from scipy.linalg import toeplitz
# A = np.mat(np.random.randn(200, 500))
B = np.mat(toeplitz([np.random.randint(0,10) for i in range(500)]))
b = np.mat(np.random.randint(10, size=(500, 1)))
print("b = ",end="")
print(b)
print("x = ",end="")
print(B.I * b)
Exercise 9.3: Norms
题目描述
Compute the Frobenius norm of A:||A||F and the innity norm of B. Also nd the largest and smallest singular values of B.
输入样例
none
输出样例
the Frobenius norm of A: 316.4697694002661
the infinity norm of A: 268.4652239616853
Max singular values: 2165.778668032877
Min singular values: -158.09589104357846
代码展示
import numpy as np
from scipy.linalg import toeplitz
A = np.mat(np.random.randn(200, 500))
B = np.mat(toeplitz([np.random.randint(0,10) for i in range(500)]))
print("the Frobenius norm of A: ",end="")
sum = 0
for i in range(200):
for j in range(500):
sum += A[i, j] ** 2
print(np.sqrt(sum))
print("the infinity norm of A: ",end="")
max = 0
for i in range(200):
sum = 0
for j in range(300):
sum += np.fabs(A[i, j])
if sum > max:
max = sum
print(max)
x = np.linalg.eigvals(B)
max = 0
min = 0
for i in range(500):
if x[i] > max:
max = x[i]
if x[i] < min:
min = x[i]
print("Max singular values: ", end="")
print(max)
print("Min singular values: ", end="")
print(min)
Exercise 9.4: Power iteration
题目描述
Generate a matrix Z, n * n, with Gaussian entries, and use the power iteration to nd 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.
输入样例
none
输出样例
the largest eigenvalue of Z: iterations needed: 192
largest eigenvalue: [[15.25915122]]
corresponding eigenvector: [[-0.09026188]
[ 0.12826422]
[ 0.15051927]
[ 0.43330653]
[ 0.10297349]
......
[ 0.22702661]
[ 0.45739333]]
代码展示
import numpy as np
from scipy.linalg import toeplitz
Z = np.mat(np.random.randn(200, 200))
# B = np.mat(toeplitz([np.random.randint(0,10) for i in range(500)]))
print("the largest eigenvalue of Z: ",end="")
t = 0
tmp = 0
maxi = 0
last = maxi + 1
y = np.mat(np.ones(200))
y = y.T
x = y
while np.fabs(maxi - last) > 0.00001:
last = maxi
x = Z * y
tmp = max(x)
y = x / y
t = t + 1
print("iterations needed: ", end="")
print(t)
print("largest eigenvalue: ", end="")
print(maxi)
print("corresponding eigenvector: ", end="")
print(y)
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?
输入样例
none
输出样例
n = 10
p = 0.1
result = (1+0j)
n = 10
p = 0.2
result = (1+0j)
n = 10
p = 0.30000000000000004
result = (3.0056441200294044+0j)
n = 10
p = 0.4
result = (4.384673955948278+0j)
n = 10
p = 0.5
result = (5.221011084230081+0j)
n = 10
p = 0.6000000000000001
result = (5.276175173542105+0j)
n = 10
p = 0.7000000000000001
result = (7.440423491970307+0j)
n = 10
p = 0.8
result = (7.757232980358522+0j)
n = 10
p = 0.9
result = (9.533492200247167+0j)
n = 20
p = 0.1
result = (2.0049144518608095+0j)
n = 20
p = 0.2
result = (3.6124443794089585+0j)
n = 20
p = 0.30000000000000004
result = (6.611297165762469+0j)
n = 20
p = 0.4
result = (7.570977403284739+0j)
n = 20
p = 0.5
result = (9.546442350332846+0j)
n = 20
p = 0.6000000000000001
result = (11.947833697011028+0j)
n = 20
p = 0.7000000000000001
result = (13.819637287221628+0j)
n = 20
p = 0.8
result = (16.516489712286493+0j)
n = 20
p = 0.9
result = (18.109965025315887+0j)
n = 30
p = 0.1
result = (3.6440981108469317+0j)
n = 30
p = 0.2
result = (5.673336100367789+0j)
n = 30
p = 0.30000000000000004
result = (8.40759472207094+0j)
n = 30
p = 0.4
result = (12.899568557880066+0j)
n = 30
p = 0.5
result = (15.085403174055337+0j)
n = 30
p = 0.6000000000000001
result = (18.320594577899147+0j)
n = 30
p = 0.7000000000000001
result = (20.898662730299506+0j)
n = 30
p = 0.8
result = (24.256599019315715+0j)
n = 30
p = 0.9
result = (27.326856598773734+0j)
n = 40
p = 0.1
result = (3.9918360026793613+0j)
n = 40
p = 0.2
result = (7.615524926774821+0j)
n = 40
p = 0.30000000000000004
result = (13.079821998812237+0j)
n = 40
p = 0.4
result = (15.572456966973137+0j)
n = 40
p = 0.5
result = (19.68051998962197+0j)
n = 40
p = 0.6000000000000001
result = (23.860768371299418+0j)
n = 40
p = 0.7000000000000001
result = (27.34178318446844+0j)
n = 40
p = 0.8
result = (31.738692544160273+0j)
n = 40
p = 0.9
result = (36.16824365272423+0j)
n = 50
p = 0.1
result = (4.885068211737687+0j)
n = 50
p = 0.2
result = (9.64360321244893+0j)
n = 50
p = 0.30000000000000004
result = (14.800216443708042+0j)
n = 50
p = 0.4
result = (19.252331979323433+0j)
n = 50
p = 0.5
result = (24.872098680785097+0j)
n = 50
p = 0.6000000000000001
result = (30.08332613992508+0j)
n = 50
p = 0.7000000000000001
result = (34.638601651856476+0j)
n = 50
p = 0.8
result = (40.143339289496666+0j)
n = 50
p = 0.9
result = (44.44618792604578+0j)
n = 60
p = 0.1
result = (6.215823037834183+0j)
n = 60
p = 0.2
result = (12.190633425036994+0j)
n = 60
p = 0.30000000000000004
result = (18.545991505965045+0j)
n = 60
p = 0.4
result = (24.24301895782981+0j)
n = 60
p = 0.5
result = (30.737320462868677+0j)
n = 60
p = 0.6000000000000001
result = (35.69411460253234+0j)
n = 60
p = 0.7000000000000001
result = (41.261086359269335+0j)
n = 60
p = 0.8
result = (47.470966053100526+0j)
n = 60
p = 0.9
result = (54.26054719954475+0j)
n = 70
p = 0.1
result = (6.81037153802264+0j)
n = 70
p = 0.2
result = (14.528085100057709+0j)
n = 70
p = 0.30000000000000004
result = (20.6319396220105+0j)
n = 70
p = 0.4
result = (27.268429875418498+0j)
n = 70
p = 0.5
result = (34.432537650547026+0j)
n = 70
p = 0.6000000000000001
result = (42.39183261540491+0j)
n = 70
p = 0.7000000000000001
result = (48.72193105694764+0j)
n = 70
p = 0.8
result = (56.285041347268844+0j)
n = 70
p = 0.9
result = (62.97676309730214+0j)
n = 80
p = 0.1
result = (8.202670465891142+0j)
n = 80
p = 0.2
result = (15.621062102724345+0j)
n = 80
p = 0.30000000000000004
result = (24.50114963179705+0j)
n = 80
p = 0.4
result = (32.0718561408664+0j)
n = 80
p = 0.5
result = (39.7540179658928+0j)
n = 80
p = 0.6000000000000001
result = (47.29314443925617+0j)
n = 80
p = 0.7000000000000001
result = (55.203575907625876+0j)
n = 80
p = 0.8
result = (63.80445862239928+0j)
n = 80
p = 0.9
result = (72.11563908528714+0j)
n = 90
p = 0.1
result = (8.718519737561946+0j)
n = 90
p = 0.2
result = (18.47622678914926+0j)
n = 90
p = 0.30000000000000004
result = (27.34468015309816+0j)
n = 90
p = 0.4
result = (35.65166940947665+0j)
n = 90
p = 0.5
result = (45.81694041516667+0j)
n = 90
p = 0.6000000000000001
result = (53.93267926888739+0j)
n = 90
p = 0.7000000000000001
result = (63.19974021972269+0j)
n = 90
p = 0.8
result = (72.1576820709366+0j)
n = 90
p = 0.9
result = (80.89275079611768+0j)
n = 100
p = 0.1
result = (10.071819882840305+0j)
n = 100
p = 0.2
result = (20.31166667459846+0j)
n = 100
p = 0.30000000000000004
result = (29.644030021455738+0j)
n = 100
p = 0.4
result = (39.80697528380786+0j)
n = 100
p = 0.5
result = (49.79353805067437+0j)
n = 100
p = 0.6000000000000001
result = (59.37254798023554+0j)
n = 100
p = 0.7000000000000001
result = (69.38277234053848+0j)
n = 100
p = 0.8
result = (79.97792865290907+0j)
n = 100
p = 0.9
result = (89.78564146546881+0j)
代码展示
import numpy as np
from scipy.linalg import toeplitz
from scipy.linalg import eigvals
def deal(n, p):
Z = []
for i in range(n):
newLine = []
for j in range(n):
if np.random.random() <= p:
newLine.append(1)
else:
newLine.append(0)
Z.append(newLine)
x = eigvals(np.mat(Z))
return max(x)
for n in [10*(i + 1) for i in range(10)]:
for p in [0.1*(i + 1) for i in range(9)]:
print("n = ",end="")
print(n)
print(" p = ", end="")
print(p)
print(" result = ", end="")
print(deal(n, p))
Exercise 9.6: Nearest neighbor
题目描述
Write a function that takes a value z and an array A and nds 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 nd this value manually. In particular, use brackets and argmin.
输入样例
none
输出样例
A is [[ 0.42254129 -0.87117165 0.42894451 ... -0.63444994 -1.43767193
0.53730518]
[ 1.28752472 -0.26346702 -0.29326016 ... 1.07772979 0.71711088
-0.75864091]
[ 0.49012752 -0.90246077 0.74779149 ... 0.07699471 0.06608078
1.0728209 ]
...
[ 2.25830666 1.45357656 -1.72138887 ... 0.06410362 0.14600388
-1.76334643]
[ 0.10719701 -0.85852098 0.35197164 ... 2.41304663 -0.48824605
1.23490427]
[ 0.50518307 0.19371635 0.12307002 ... -0.29543703 -0.54018174
0.71282844]]
z is 0.965436
nearest neighbor is 0.422541
代码展示
import numpy as np
from scipy.linalg import toeplitz
from scipy.linalg import eigvals
A = np.random.randn(200, 200)
z = np.random.random()
min = 1000
num = 0
for i in range(200):
for j in range(200):
if np.fabs(A[i, j] - z) < min:
min = np.fabs(i - j)
num = A[i, j]
print("A is ", end="")
print(A)
print("z is %f" % z)
print("nearest neighbor is %f" % num)