python3中numpy矩阵操作与线性方程组
线性方程组要多引入一个模块from numpy.linalg import *
对矩阵进行单位阵初始化、求逆、转置、求行列式、解线性方程组操作。
1 #encoding = utf-8
2 #矩阵操作和线性方程组
3 import numpy as np
4 from numpy.linalg import *
5 print(np.eye(3))
6 list1 = np.array([[1,2],
7 [3,4]])
8 print("Inv:")
9 print(inv(list1))
10 print("T:")
11 print(list1.transpose())
12 print("Det:")
13 print(det(list1))
14 print(eig(list1))
15 print("Solve:")
16 y = np.array([[5],[7]])
17 print(solve(list1,y))
18
19 运行结果:
20 [[ 1. 0. 0.]
21 [ 0. 1. 0.]
22 [ 0. 0. 1.]]
23 Inv:
24 [[-2. 1. ]
25 [ 1.5 -0.5]]
26 T:
27 [[1 3]
28 [2 4]]
29 Det:
30 -2.0
31 (array([-0.37228132, 5.37228132]), array([[-0.82456484, -0.41597356],
32 [ 0.56576746, -0.90937671]]))
33 Solve:
34 [[-3.]
35 [ 4.]]