# 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]
``````