这节课老师讲了 Numpy ,装个 Numpy 都能装半个钟 TAT ……查一查就知道有镜像源可以用啊 QAQ
Calculate A+A,AAT,ATA A + A , A A T , A T A and AB A B . Write a function that computes A(B−λI) A ( B − λ I ) for any λ λ .
考虑到 n,m n , m 太大,不好调试,也不好把运行结果放在博客里。我把题目中的 n n 改为 6, m m 改为 4。
所有的函数基本上靠 Google 查文档……查到函数就没什么问题啦!
import numpy
from scipy.linalg import toeplitz
n = 6
m = 4
A = numpy.random.normal(size = (n, m))
B = toeplitz(list(numpy.random.rand(m)))
print("A = ")
print(A)
print("")
print("B = ")
print(B)
print("")
print("A + A =")
print(A + A)
print("")
print("A * A^T =")
print(numpy.dot(A, numpy.transpose(A)))
print("")
print("A^T * A =")
print(numpy.dot(numpy.transpose(A), A))
print("")
print("A * B =")
print(numpy.dot(A, B))
print("")
print("A * (B - xI) = ")
f = lambda x: numpy.dot(A, B - x * numpy.eye(m))
print(f(0.5))
运行结果:
A =
[[-0.11281631 0.41783085 0.09815799 -1.37959734]
[ 0.86089005 0.79009271 -0.37353857 -1.41071209]
[ 0.1595781 1.02578769 0.28194433 1.06656449]
[-0.72462512 -0.40801415 -0.37193955 1.25537165]
[-0.24226527 0.39698618 -0.84594276 0.25382869]
[-1.49292275 0.31526557 0.60724678 1.04393588]]
B =
[[0.61888271 0.80871496 0.83575727 0.28346094]
[0.80871496 0.61888271 0.80871496 0.83575727]
[0.83575727 0.80871496 0.61888271 0.80871496]
[0.28346094 0.83575727 0.80871496 0.61888271]]
A + A =
[[-0.22563263 0.83566169 0.19631597 -2.75919467]
[ 1.72178011 1.58018542 -0.74707714 -2.82142418]
[ 0.3191562 2.05157537 0.56388866 2.13312898]
[-1.44925024 -0.8160283 -0.7438791 2.5107433 ]
[-0.48453054 0.79397235 -1.69188551 0.50765738]
[-2.98584551 0.63053115 1.21449356 2.08787177]]
A * A^T =
[[ 2.10023394 2.14255151 -1.03315172 -1.85714759 -0.24001288 -1.08045132]
[ 2.14255151 3.49501784 -0.66208593 -2.57822576 0.06300517 -2.73567638]
[-1.03315172 -0.66208593 2.29475797 0.69989839 0.40077921 1.3697925 ]
[-1.85714759 -2.57822576 0.69989839 2.40585412 0.64686443 2.03784493]
[-0.24001288 0.06300517 0.40077921 0.64686443 0.99633864 0.23812427]
[-1.08045132 -2.73567638 1.3697925 2.03784493 0.23812427 3.78676151]]
A^T * A =
[[ 3.59191675 0.52555226 -0.71977057 -3.41830993]
[ 0.52555226 2.27453544 0.04247137 -0.67928881]
[-0.71977057 0.04247137 1.45136548 0.64452848]
[-3.41830993 -0.67928881 0.64452848 7.76114634]]
A * B =
[[-0.04093971 -0.90627463 -0.81133372 -0.45720095]
[ 0.45968042 -0.29590974 -0.01358561 -0.27079712]
[ 1.46629637 1.88329708 1.99997554 1.79063459]
[-0.73342746 -0.09013517 -0.15052698 -0.07026898]
[-0.46393742 -0.42222306 -0.19969058 -0.26392503]
[ 0.13444187 0.35133001 0.22730003 0.97746364]]
A * (B - xI) =
[[ 0.01546845 -1.11519005 -0.86041272 0.23259772]
[ 0.02923539 -0.69095609 0.17318367 0.43455893]
[ 1.38650732 1.37040324 1.85900337 1.25735235]
[-0.3711149 0.11387191 0.0354428 -0.6979548 ]
[-0.34280478 -0.62071614 0.22328079 -0.39083938]
[ 0.88090324 0.19369722 -0.07632336 0.45549569]]
Generate a vector b b with m m entries and solve Bx=b B x = b
考虑求 B B 的逆,然后与 b b 相乘即可。
import numpy
from scipy.linalg import toeplitz
n = 6
m = 4
A = numpy.random.normal(size = (n, m))
B = toeplitz(list(numpy.random.rand(m)))
b = numpy.array(list(numpy.random.rand(m)))
b.shape = (1, m)
b = numpy.transpose(b)
print("B = ")
print(B)
print("")
print("b = ")
print(b)
print("")
C = numpy.linalg.inv(B)
x = numpy.dot(C, b)
print("x = ")
print(x)
print("")
print("Validation: Bx = ")
print(numpy.dot(B, x))
print("")
运行结果:
B =
[[0.04230256 0.65233112 0.22936666 0.48550987]
[0.65233112 0.04230256 0.65233112 0.22936666]
[0.22936666 0.65233112 0.04230256 0.65233112]
[0.48550987 0.22936666 0.65233112 0.04230256]]
b =
[[0.90444191]
[0.27756339]
[0.12886221]
[0.14356352]]
x =
[[-3.33095582]
[-1.25577034]
[ 2.98309714]
[ 2.43106269]]
Validation: Bx =
[[0.90444191]
[0.27756339]
[0.12886221]
[0.14356352]]
好吧,又在 Google 上把两个函数找到
import numpy
from scipy.linalg import toeplitz
n = 6
m = 4
A = numpy.random.normal(size = (n, m))
B = toeplitz(list(numpy.random.rand(m)))
print("A =")
print(A)
print("")
AF = numpy.linalg.norm(A)
print("AF =")
print(AF)
print("")
print("B =")
print(B)
print("")
BF = numpy.linalg.norm(B, numpy.inf)
print("BF =")
print(BF)
print("")
运行结果:
A =
[[-1.85502387 1.45307504 -0.7298224 0.19064925]
[ 0.42006308 -1.06515485 -0.56644324 -0.86077161]
[ 0.13983461 -0.26931018 -0.40479553 -1.02185437]
[-0.05903845 1.20521775 0.03547713 -0.341537 ]
[ 1.37176362 -0.58666238 1.34213027 -1.47081987]
[ 0.03120845 0.03860088 0.11806082 0.09013899]]
AF =
4.193262620191245
B =
[[0.57248942 0.37242274 0.31374627 0.67335778]
[0.37242274 0.57248942 0.37242274 0.31374627]
[0.31374627 0.37242274 0.57248942 0.37242274]
[0.67335778 0.31374627 0.37242274 0.57248942]]
BF =
1.9320162030663386
继续搜索 eigenvalue/eigenvalue 的函数……
import numpy
from scipy.linalg import toeplitz
n = 6
Z = numpy.random.normal(size = (n, n))
print("Z =")
print(Z)
print("")
a = numpy.linalg.eig(Z)
print("eig = ")
print(a)
运行结果
Z =
[[ 1.17077846 -0.49404214 -0.19753952 -0.23422968 0.75231007 -0.42429438]
[-0.52852624 0.37942899 -0.72593476 -0.32278719 0.41597234 -1.5134676 ]
[ 0.21585695 0.18076521 0.10678318 -0.7991506 -0.34175139 0.43724075]
[-0.12561833 1.10554731 -1.92405506 -0.35950713 -2.3248662 -0.63795924]
[ 0.583325 2.45267851 -0.07688235 -0.5721484 0.92778711 0.10313715]
[-0.7880677 -1.10875937 -0.75130974 -0.95750364 0.51726749 -1.63898488]]
eig =
(array([-2.35840503+0.9828976j , -2.35840503-0.9828976j ,
1.22147278+0.j , 1.94255349+0.26928215j,
1.94255349-0.26928215j, 0.19651602+0.j ]), array([[ 0.14777223+0.03637904j, 0.14777223-0.03637904j,
0.89696767+0.j , 0.46630275-0.02977809j,
0.46630275+0.02977809j, -0.24901103+0.j ],
[ 0.33707481+0.02375161j, 0.33707481-0.02375161j,
-0.16268442+0.j , -0.16453443+0.120872j ,
-0.16453443-0.120872j , -0.08025294+0.j ],
[-0.11334332-0.26100894j, -0.11334332+0.26100894j,
-0.09770529+0.j , 0.3044162 -0.07231334j,
0.3044162 +0.07231334j, -0.71040722+0.j ],
[-0.12459487-0.56258514j, -0.12459487+0.56258514j,
0.25583785+0.j , -0.7026318 +0.j ,
-0.7026318 -0.j , 0.10038354+0.j ],
[-0.25894077-0.20569013j, -0.25894077+0.20569013j,
-0.14479392+0.j , 0.30822043+0.19914949j,
0.30822043-0.19914949j, 0.40020334+0.j ],
[ 0.5833518 +0.j , 0.5833518 -0.j ,
-0.27021875+0.j , 0.11715391+0.00425655j,
0.11715391-0.00425655j, 0.50659153+0.j ]]))