# Python 中的对角矩阵

Dhruvdeep Singh Saini 2023年1月30日

## Python 中的矩阵是什么

`矩阵`是一种独特的二维结构，用于存储数据行和列。它可以保存各种值，例如整数、字符串、表达式、特殊符号等。

``````X = [["a", "b", "c"], ["d", "e", "f"], ["g", "h", "i"]]

print(X, "is our Matrix")
``````

``````[['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']] is our Matrix
``````

## Python 中的矩阵表示

``````X = [["a", "b", "c"], ["d", "e", "f"], ["g", "h", "i"]]

Y = [[27, 34], [61, 18]]

Z = [["one", "two", "three"], ["four", "five", "six"]]

print(X, "is 3x3")
print(Y, "is 2x2")
print(Z, "is 2x3")
``````

``````[['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']] is 3x3
[[27, 34], [61, 18]] is 2x2
[['one', 'two', 'three'], ['four', 'five', 'six']] is 2x3
``````

## Python 中不同类型的矩阵

1. 向量矩阵：仅包含单行或单列的矩阵为向量矩阵。如果它只有一行，则称为行向量，如果它只有一列，则称为列向量。
``````import numpy as np

x = np.array([13, 42, 93])

y = np.array([[21], [72], [36]])

print("Row: ", x)
print("Column: ", y)
``````

``````Row:  [13 42 93]
Column:  [[21]
[72]
[36]]
``````

1. 方阵：行数等于列数的矩阵。

``````Y = [[27, 34], [61, 18]]
X = [["a", "b", "c"], ["d", "e", "f"], ["g", "h", "i"]]
print("3X3 square: ", X)
print("2X2 sqaure: ", Y)
``````

``````3X3 square:  [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
2X2 sqaure:  [[27, 34], [61, 18]]
``````
1. 对角矩阵：对角矩阵是仅在对角位置具有元素的矩阵，即仅填充具有相似行号和列号的位置。对角线元素仅占据 `(1,1)``(2,2)``(3,3)``(4,4)` 位置等。

``````Y = [[27, 0], [0, 18]]
X = [[5, 0, 0], [0, 10, 0], [0, 0, 15]]

print("2x2 Diagonal:", X)
print("3x3 Diagonal:", Y)
``````

``````2x2 Diagonal: [[5, 0, 0], [0, 10, 0], [0, 0, 15]]
3x3 Diagonal: [[27, 0], [0, 18]]
``````

## 在 Python 中如何使用 NumPy 创建对角矩阵

``````pip install NumPy
``````

``````import numpy as np

X = np.array([[12, 0, 0], [0, 24, 0], [0, 0, 36]])
print("Diagonal: ")
print(X)
``````

``````Diagonal:
[[12  0  0]
[ 0 24  0]
[ 0  0 36]]
``````

## 在 Python 中将向量转换为对角矩阵

1. `diag` 函数：可以使用 Python 中的 `diag` 函数构造一个对角矩阵。它包含在 `NumPy` 库中并使用两个参数。

`diag` 函数是 `numpy.diag(v, k=0)` 其中 `v` 是一个返回对角矩阵的数组。指定 `v` 很重要，但你可以跳过 `k`

``````import numpy as np

diagonal = np.diag([5, 10, 15, 20])
print("Diagonal: ")
print(diagonal)
``````

``````Diagonal:
[[ 5  0  0  0]
[ 0 10  0  0]
[ 0  0 15  0]
[ 0  0  0 20]]
``````
1. `diagflat` 函数：`diagflat` 函数在语义上类似于 `diag` 函数，并带有 `NumPy` 库。`diagflat` 函数是 `numpy.diagflat(v, k=0)` 其中 `v``k``diag` 函数相同。
``````import numpy as np

diagonal = np.diagflat([5, 10, 15, 20])
print("Diagonal: ")
print(diagonal)
``````

``````Diagonal:
[[ 5  0  0  0]
[ 0 10  0  0]
[ 0  0 15  0]
[ 0  0  0 20]]
``````

``````import numpy as np

# Diagonal with k as 1
diagonal = np.diagflat([5, 10, 15, 20], 1)
print("Diagonal with k=1: ")
print(diagonal)
# Diagonal with k as -1
diagonal2 = np.diag([5, 10, 15, 20], -1)
print("Diagonal with k=-1: ")
print(diagonal2)
``````

``````Diagonal with k=1:
[[ 0  5  0  0  0]
[ 0  0 10  0  0]
[ 0  0  0 15  0]
[ 0  0  0  0 20]
[ 0  0  0  0  0]]
Diagonal with k=-1:
[[ 0  0  0  0  0]
[ 5  0  0  0  0]
[ 0 10  0  0  0]
[ 0  0 15  0  0]
[ 0  0  0 20  0]]
``````

## 在 Python 中如何获取矩阵的对角线

Numpy 还有另一个称为对角线的功能。对角函数用于获取矩阵的所有对角元素的值。

``````import numpy as np

X = np.array([[12, 0, 0], [0, 24, 0], [0, 0, 36]])

de = X.diagonal()
print("Diagonal elements: ", de)
``````

``````Diagonal elements:  [12 24 36]
``````