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