Files
Iris Series: Visualize Math -- From Arithmetic Basics to Machine Learning 79be5dda7d Add files via upload
2025-02-01 17:06:45 +08:00

292 lines
7.2 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
{
"cells": [
{
"cell_type": "markdown",
"id": "73bd968b-d970-4a05-94ef-4e7abf990827",
"metadata": {},
"source": [
"Chapter 06\n",
"\n",
"# 分块矩阵\n",
"Book_4《矩阵力量》 | 鸢尾花书:从加减乘除到机器学习 (第二版)"
]
},
{
"cell_type": "markdown",
"id": "25999c8b-f570-4ca4-b9d4-6e8c9537b859",
"metadata": {},
"source": [
"这段代码演示了如何对矩阵进行分块和重组操作。首先定义了一个 \\(4 \\times 5\\) 矩阵 \\( A \\)\n",
"\n",
"$$\n",
"A = \\begin{bmatrix}\n",
"1 & 2 & 3 & 0 & 0 \\\\\n",
"4 & 5 & 6 & 0 & 0 \\\\\n",
"0 & 0 & 0 & -1 & 0 \\\\\n",
"0 & 0 & 0 & 0 & 1 \\\\\n",
"\\end{bmatrix}\n",
"$$\n",
"\n",
"然后使用 NumPy 的切片方法,将矩阵 \\( A \\) 分割为 4 个子矩阵(即 \\(2 \\times 2\\) 的块矩阵结构):\n",
"\n",
"1. \\( A_{1,1} = A[0:2, 0:3] \\),即前两行和前三列的子矩阵:\n",
" $$\n",
" A_{1,1} = \\begin{bmatrix} 1 & 2 & 3 \\\\ 4 & 5 & 6 \\end{bmatrix}\n",
" $$\n",
" \n",
"2. \\( A_{1,2} = A[0:2, 3:] \\),即前两行和最后两列的子矩阵:\n",
" $$\n",
" A_{1,2} = \\begin{bmatrix} 0 & 0 \\\\ 0 & 0 \\end{bmatrix}\n",
" $$\n",
"\n",
"3. \\( A_{2,1} = A[2:, 0:3] \\),即后两行和前三列的子矩阵:\n",
" $$\n",
" A_{2,1} = \\begin{bmatrix} 0 & 0 & 0 \\\\ 0 & 0 & 0 \\end{bmatrix}\n",
" $$\n",
"\n",
"4. \\( A_{2,2} = A[2:, 3:] \\),即后两行和最后两列的子矩阵:\n",
" $$\n",
" A_{2,2} = \\begin{bmatrix} -1 & 0 \\\\ 0 & 1 \\end{bmatrix}\n",
" $$\n",
"\n",
"最后使用 `np.block` 函数将这些子矩阵重新组合为一个新的矩阵 \\( A' \\),其结构与原矩阵 \\( A \\) 相同:\n",
"\n",
"$$\n",
"A' = \\begin{bmatrix} \n",
"A_{1,1} & A_{1,2} \\\\ \n",
"A_{2,1} & A_{2,2} \n",
"\\end{bmatrix} = \n",
"\\begin{bmatrix} \n",
"1 & 2 & 3 & 0 & 0 \\\\ \n",
"4 & 5 & 6 & 0 & 0 \\\\ \n",
"0 & 0 & 0 & -1 & 0 \\\\ \n",
"0 & 0 & 0 & 0 & 1 \n",
"\\end{bmatrix}\n",
"$$ \n",
"\n",
"通过这种方法,可以方便地在分块矩阵操作中对矩阵进行重新组合,实现了矩阵的分解与重构。"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "da9bd893-e25f-4193-b12b-e5cac674f64b",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "markdown",
"id": "67b49f8e-b6d5-49d4-b4a4-190ecd915450",
"metadata": {},
"source": [
"## 定义矩阵 A"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "e00b27dd-c721-42e1-8f65-fa2937b460ad",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, 2, 3, 0, 0],\n",
" [ 4, 5, 6, 0, 0],\n",
" [ 0, 0, 0, -1, 0],\n",
" [ 0, 0, 0, 0, 1]])"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A = np.array([[1, 2, 3, 0, 0], # 定义矩阵 A 的元素\n",
" [4, 5, 6, 0, 0],\n",
" [0, 0, 0, -1, 0],\n",
" [0, 0 ,0, 0, 1]])\n",
"A"
]
},
{
"cell_type": "markdown",
"id": "5a878c1b-238b-4f8d-a033-db66fb2aa0fa",
"metadata": {},
"source": [
"## NumPy 数组切片操作"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "440560e5-ed77-4bbc-8fa7-d2c09285e3c7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2, 3],\n",
" [4, 5, 6]])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A_1_1 = A[0:2,0:3] # 提取矩阵 A 的前两行和前三列作为子矩阵 A_1_1\n",
"A_1_1"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "29344301-cbb7-4fbd-b64b-d23aced6823e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0, 0],\n",
" [0, 0]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A_1_2 = A[0:2,3:] # 提取矩阵 A 的前两行和最后两列作为子矩阵 A_1_2\n",
"A_1_2\n",
"# A_1_2 = A[0:2,-2:] # 或者用负索引方式提取前两行和最后两列(注释部分)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "b24013dd-a04e-44f6-9c89-67b2a0027bff",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0, 0, 0],\n",
" [0, 0, 0]])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A_2_1 = A[2:,0:3] # 提取矩阵 A 的后两行和前三列作为子矩阵 A_2_1\n",
"A_2_1\n",
"# A_2_1 = A[-2:,0:3] # 或者用负索引方式提取后两行和前三列(注释部分)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "2aa41d01-ef5a-450d-b912-d0cf9134f16f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[-1, 0],\n",
" [ 0, 1]])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A_2_2 = A[2:,3:] # 提取矩阵 A 的后两行和最后两列作为子矩阵 A_2_2\n",
"A_2_2\n",
"# A_2_2 = A[-2:,-2:] # 或者用负索引方式提取后两行和最后两列(注释部分)"
]
},
{
"cell_type": "markdown",
"id": "f618ccce-d822-417d-8ce1-6cb6d3f90999",
"metadata": {},
"source": [
"## 使用嵌套列表中的块组装矩阵"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "4dc81f48-c1a3-42e0-82df-b76c62601926",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, 2, 3, 0, 0],\n",
" [ 4, 5, 6, 0, 0],\n",
" [ 0, 0, 0, -1, 0],\n",
" [ 0, 0, 0, 0, 1]])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A_ = np.block([[A_1_1, A_1_2], # 通过 np.block 函数将子矩阵 A_1_1、A_1_2、A_2_1 和 A_2_2 组合成新的矩阵 A_\n",
" [A_2_1, A_2_2]])\n",
"A_"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "85a80909-2aac-49ed-bb7a-f8cc6b80ee7d",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "ecd322f4-f919-4be2-adc3-69d28ef25e69",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}