{ "cells": [ { "cell_type": "markdown", "id": "73bd968b-d970-4a05-94ef-4e7abf990827", "metadata": {}, "source": [ "Chapter 02\n", "\n", "# 标量乘法\n", "Book_4《矩阵力量》 | 鸢尾花书:从加减乘除到机器学习 (第二版)" ] }, { "cell_type": "markdown", "id": "9fcf085a-8ecf-4003-b0b1-76f2e95c1063", "metadata": {}, "source": [ "\n", "\n", "此代码定义了一个二维列向量 $a$,其值为:\n", "\n", "$$\n", "a = \\begin{bmatrix} 2 \\\\ 2 \\end{bmatrix}\n", "$$\n", "\n", "接下来,代码通过数乘操作生成两个新向量 $b$ 和 $c$。具体而言,$b$ 是 $a$ 的 2 倍:\n", "\n", "$$\n", "b = 2 \\times a = 2 \\times \\begin{bmatrix} 2 \\\\ 2 \\end{bmatrix} = \\begin{bmatrix} 4 \\\\ 4 \\end{bmatrix}\n", "$$\n", "\n", "$c$ 是 $a$ 的 $-1.5$ 倍:\n", "\n", "$$\n", "c = -1.5 \\times a = -1.5 \\times \\begin{bmatrix} 2 \\\\ 2 \\end{bmatrix} = \\begin{bmatrix} -3 \\\\ -3 \\end{bmatrix}\n", "$$\n", "\n", "此过程展示了向量的数乘操作,通过将标量与向量相乘来改变向量的大小和方向。" ] }, { "cell_type": "markdown", "id": "7f2b0f70-8b54-49c7-a58c-736dd1838f39", "metadata": {}, "source": [ "## 导入所需库" ] }, { "cell_type": "code", "execution_count": 1, "id": "457dd7c5-1ef8-4272-9608-01802512d831", "metadata": {}, "outputs": [], "source": [ "import numpy as np # 导入NumPy库,用于数值计算" ] }, { "cell_type": "markdown", "id": "a2882d5b-db0c-4d22-96ba-76123168e589", "metadata": {}, "source": [ "## 定义一个列向量" ] }, { "cell_type": "code", "execution_count": 2, "id": "dc48b150-34ed-4cc8-a5c3-ae59f58fbcdf", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[2],\n", " [2]])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.array([[2], [2]]) # 定义向量a,值为[2, 2]\n", "a" ] }, { "cell_type": "markdown", "id": "0e89b43f-c8e1-4af6-a3aa-eb966479608b", "metadata": {}, "source": [ "## 进行向量的数乘" ] }, { "cell_type": "code", "execution_count": 3, "id": "352700c8-fc4c-4b7c-8f8b-2ee1a5905878", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[4],\n", " [4]])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = 2 * a # 计算b,为向量a的2倍\n", "b" ] }, { "cell_type": "code", "execution_count": 4, "id": "a7e94dac-9695-4bcc-b09b-3dc7e823fe3b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[-3.],\n", " [-3.]])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c = -1.5 * a # 计算c,为向量a的-1.5倍\n", "c" ] }, { "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 }