mirror of
https://github.com/Visualize-ML/Book4_Power-of-Matrix.git
synced 2026-02-03 02:24:03 +08:00
194 lines
4.4 KiB
Plaintext
194 lines
4.4 KiB
Plaintext
{
|
||
"cells": [
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "73bd968b-d970-4a05-94ef-4e7abf990827",
|
||
"metadata": {},
|
||
"source": [
|
||
"Chapter 02\n",
|
||
"\n",
|
||
"# 向量积\n",
|
||
"Book_4《矩阵力量》 | 鸢尾花书:从加减乘除到机器学习 (第二版)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "873da06f-c90c-4a2a-abd3-7f8711feca02",
|
||
"metadata": {},
|
||
"source": [
|
||
"此代码定义了两个三维向量 $a$ 和 $b$,并计算了它们的叉积(向量积)。首先定义了行向量形式的 $a$ 和 $b$,然后分别计算了行向量和列向量的叉积。\n",
|
||
"\n",
|
||
"### 叉积公式\n",
|
||
"对于三维向量 $a = \\begin{bmatrix} a_1 \\\\ a_2 \\\\ a_3 \\end{bmatrix}$ 和 $b = \\begin{bmatrix} b_1 \\\\ b_2 \\\\ b_3 \\end{bmatrix}$,叉积定义为:\n",
|
||
"\n",
|
||
"$$\n",
|
||
"a \\times b = \\begin{bmatrix} a_2 b_3 - a_3 b_2 \\\\ a_3 b_1 - a_1 b_3 \\\\ a_1 b_2 - a_2 b_1 \\end{bmatrix}\n",
|
||
"$$\n",
|
||
"\n",
|
||
"代码中计算了行向量和列向量形式的叉积。结果表示两个向量的垂直方向,并可用于三维空间中的法向量计算。"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "4e369506-697f-450e-a731-76945415ed9c",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 导入所需库"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 1,
|
||
"id": "dbb6f14c-fcb4-499f-9618-d6ca9c8b36ab",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"import numpy as np # 导入NumPy库,用于数值计算"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "5d9fa082-6e75-4a30-9ff1-b119c73a3a25",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 定义两个行向量"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 2,
|
||
"id": "081b4fe5-be16-4854-914b-5c795f937fa8",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"a = np.array([-2, 1, 1]) # 定义向量a"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 3,
|
||
"id": "9a902aa8-799a-4461-a5c0-459f2550272a",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"b = np.array([1, -2, -1]) # 定义向量b"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "b04a2e40-0ae4-44f1-a6bd-e691ee781d18",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 计算行向量的叉积"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 4,
|
||
"id": "95ea922a-d94c-42c3-8459-d8fc755b8df3",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"a_cross_b = np.cross(a, b) # 计算a和b的叉积"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "6de6f635-f9e9-4667-a12a-884ccaeb26f3",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 定义两个列向量"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 5,
|
||
"id": "0d9f2b40-f6a0-4ca6-a70b-3910b69a9706",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"a_col = np.array([[-2], [1], [1]]) # 定义列向量a_col"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 6,
|
||
"id": "66e48d3a-7fa8-41a8-bf8b-61cfdf250200",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"b_col = np.array([[1], [-2], [-1]]) # 定义列向量b_col"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "c19ed8a3-f8e7-4c42-b6f7-bac01f31e016",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 计算列向量的叉积"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 7,
|
||
"id": "9c019178-15f5-41f6-93d5-ceea5505684f",
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([[ 1],\n",
|
||
" [-1],\n",
|
||
" [ 3]])"
|
||
]
|
||
},
|
||
"execution_count": 7,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"a_cross_b_col = np.cross(a_col, b_col, axis=0) # 计算a_col和b_col的叉积,沿axis=0进行计算\n",
|
||
"a_cross_b_col"
|
||
]
|
||
},
|
||
{
|
||
"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
|
||
}
|