NumPy函数库基础

时间:2021-05-29 21:18:57

矩阵

>>> from numpy import *
#求4*4随机矩阵>>> randMat = mat(random.rand(4,4))
#求矩阵的逆矩阵
>>> invRandMat = randMat.I
>>> print randMat
[[
0.78412287 0.54585614 0.98144309 0.31585072]
[
0.97836977 0.16112737 0.76317208 0.18055881]
[
0.08531485 0.7694393 0.41585813 0.41057737]
[
0.71374702 0.0135997 0.38255303 0.86235734]]
>>> print invRandMat
[[
-4.2437784 4.5183152 2.07112621 -0.37777692]
[
-3.22191713 2.9476215 2.98323956 -0.85744598]
[
5.89650176 -4.70212925 -3.20462974 0.35060289]
[
0.947492 -1.70023136 -0.33963938 1.33027681]]
>>> rev = randMat * invRandMat
#计算机处理误差计算,eye(4)是创建4*4单位矩阵
>>> rev - eye(4)
matrix([[
2.22044605e-16, 1.11022302e-16, 6.80011603e-16,
0.00000000e+00],
[
2.22044605e-16, -4.44089210e-16, 1.17961196e-16,
-2.77555756e-17],
[
-3.88578059e-16, 0.00000000e+00, -4.44089210e-16,
1.11022302e-16],
[
5.55111512e-16, -4.44089210e-16, 2.22044605e-16,
0.00000000e+00]])