# 【Python】【线性代数】用Python学习线性代数——矩阵

2020/1/23 16:27:32 人评论 次浏览 分类：学习教程

• 矩阵的表示方法
• 矩阵的转置
• 特殊矩阵
• 一维矩阵
• 方阵
• 对称矩阵
• 零矩阵
• 对角矩阵
• 单位矩阵
• 矩阵的加法运算

## 矩阵的表示方法

import numpy as np
a = np.array([[1, 2],
[3, 4],
[5, 6],
[7, 8]])
print(a)
print(a.shape)


[[1 2]
[3 4]
[5 6]
[7 8]]	#矩阵
(4, 2)	#形状为4行2列


## 矩阵的转置

import numpy as np
a = np.array([[1, 2, 3, 4],
[5, 6, 7, 8]])
print(a)
print(a.T)


[[1 2 3 4]
[5 6 7 8]]
[[1 5]
[2 6]
[3 7]
[4 8]]


## 特殊矩阵

#### 一维矩阵

import numpy as np
a = np.array([[1, 2, 3, 4]])
print(a)
print(a.T)


[[1 2 3 4]]
[[1]
[2]
[3]
[4]]


#### 方阵

import numpy as np
a = np.array([[1, 1, 1, 1],
[2, 2, 2, 2],
[3, 3, 3, 3],
[4, 4, 4, 4]])
print(a)
print(a.shape)


[[1 1 1 1]
[2 2 2 2]
[3 3 3 3]
[4 4 4 4]]
(4, 4)


#### 对称矩阵

import numpy as np
a = np.array([[1, 2, 3, 4],
[2, 5, 6, 7],
[3, 6, 8, 9],
[4, 7, 9, 0]])
print(a)
print(a.T)

[[1 2 3 4]
[2 5 6 7]
[3 6 8 9]
[4 7 9 0]]
[[1 2 3 4]
[2 5 6 7]
[3 6 8 9]
[4 7 9 0]]


#### 零矩阵

import numpy as np
a = np.zeros([3, 4])	#zeros为创建零矩阵的方法
print(a)


[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]	#3行4列的零矩阵


#### 对角矩阵

import numpy as np
a = np.diag([1, 2, 3, 4, 5])	#diag为创建对角矩阵的方法
print(a)


[[1 0 0 0 0]
[0 2 0 0 0]
[0 0 3 0 0]
[0 0 0 4 0]
[0 0 0 0 5]]


#### 单位矩阵

import numpy as np
a = np.eye(5)	#eye为创建单位矩阵的方法
print(a)


[[1. 0. 0. 0. 0.]
[0. 1. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 0. 0. 1. 0.]
[0. 0. 0. 0. 1.]]


## 矩阵的加法运算

$\left[ \begin{matrix} a_{11} & a_{12} & \cdots & a_{1n} \\ a_{21} & a_{22} & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m1} & a_{m2} & \cdots & a_{mn} \\ \end{matrix} \right] + \left[ \begin{matrix} b_{11} & b_{12} & \cdots & b_{1n} \\ b_{21} & b_{22} & \cdots & b_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ b_{m1} & b_{m2} & \cdots & b_{mn} \\ \end{matrix} \right] = \left[ \begin{matrix} a_{11}+b_{11} & a_{12}+b_{12} & \cdots & a_{1n}b_{1n} \\ a_{21}+b_{21} & a_{22}+b_{22} & \cdots & a_{2n}+b_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m1}+b_{m1} & a_{m2}+b_{m2} & \cdots & a_{mn}+b_{mn} \\ \end{matrix} \right]$

暂无相关的资讯...

-->