矩阵
使用 mat
方法将 2
维数组转化为矩阵:
In [1]:
import numpy as np
a = np.array([[1,2,4],
[2,5,3],
[7,8,9]])
A = np.mat(a)
A
Out[1]:
matrix([[1, 2, 4],
[2, 5, 3],
[7, 8, 9]])
也可以使用 Matlab 的语法传入一个字符串来生成矩阵:
In [2]:
A = np.mat('1,2,4;2,5,3;7,8,9')
A
Out[2]:
matrix([[1, 2, 4],
[2, 5, 3],
[7, 8, 9]])
利用分块创造新的矩阵:
In [3]:
a = np.array([[ 1, 2],
[ 3, 4]])
b = np.array([[10,20],
[30,40]])
np.bmat('a,b;b,a')
Out[3]:
matrix([[ 1, 2, 10, 20],
[ 3, 4, 30, 40],
[10, 20, 1, 2],
[30, 40, 3, 4]])
矩阵与向量的乘法:
In [4]:
x = np.array([[1], [2], [3]])
x
Out[4]:
array([[1],
[2],
[3]])
In [5]:
A * x
Out[5]:
matrix([[17],
[21],
[50]])
A.I
表示 A
矩阵的逆矩阵:
In [6]:
print A * A.I
[[ 1.00000000e+00 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 1.00000000e+00 2.08166817e-17]
[ 2.22044605e-16 -8.32667268e-17 1.00000000e+00]]
矩阵指数表示矩阵连乘:
In [7]:
print A ** 4
[[ 6497 9580 9836]
[ 7138 10561 10818]
[18434 27220 27945]]