话不多说直接上代码:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74from numpy import *
import numpy.linalg as npl
print(1)
'''生成零矩阵'''
data1 = mat(zeros((3, 3)))
print(data1)
print(2)
'''生成一矩阵(浮点型)'''
data2 = mat(ones((2, 4)))
print(data2)
print(3)
'''生成随机矩阵'''
data3 = mat(random.rand(2, 2))
print(data3)
print(4)
'''生成随机整数矩阵'''
data4 = mat(random.randint(1, 10, size=(3, 3)))
print(data4)
print(5)
'''产生对角矩阵'''
data5 = mat(eye(2, 2, dtype=int))
print(data5)
print(6)
a1 = [1, 2, 3]
data6 = mat(diag(a1))
print(data6)
print(7)
'''相乘'''
print(data4*data6)
print(8)
'''求逆和转置'''
print(data6.I)
print((data4*data6).T)
print(9)
'''行和,列和'''
data7 = mat([[1, 2], [3, 4], [5, 6]])
print(data7.sum(axis=0))
print(data7.sum(axis=1))
print(10)
'''求矩阵的行列式'''
print(linalg.det(data4))
print(11)
'''合并按行,按列'''
data8 = vstack((data1, data4))
print(data8)
data9 = hstack((data1, data4))
print(data9)
print(12)
'''列表、数组、矩阵之间的转换'''
a10 = [[1, 2], [2, 3], [3, 4]]
b10 = array(a1)
data10 = mat(b10)
c10 = data10.tolist()
print(type(c10))
print(13)
'''求解线性方程组'''
data11 = [[3, 2, 1],
[5, 5, 5],
[1, 4, 6]]
data12 = [[5], [5], [-3]]
data13 = npl.solve(data11, data12)
print(data13)
print(14)
'''取子矩阵(要把对象转化为矩阵)'''
data14 = mat(data11)[1:, 1:]
print(data14)
'''求特征值和特征向量'''
A = array([[3,-1],[-1,3]])
print('打印A:\n{}'.format(A))
a, b = linalg.eig(A)
print('打印特征值a:\n{}'.format(a))
print('打印特征向量b:\n{}'.format(b))