{ "cells": [ { "cell_type": "markdown", "id": "a9d0ab82", "metadata": {}, "source": [ "# Tutorial 1 - Vectors & Matrices in `numethods`\n", "\n", "---\n", "\n", "## 1. Introduction\n", "\n", "In numerical computing, **vectors** and **matrices** are the basic objects. \n", "Almost every algorithm (linear solvers, eigenvalue problems, optimization, curve fitting, etc.) is built upon them. \n", "\n", "In this tutorial, we will:\n", "- Define what vectors and matrices are.\n", "- Review their basic operations (addition, scalar multiplication, multiplication).\n", "- Define and compute vector and matrix **norms**.\n", "- Show how these operations work in the `numethods` package." ] }, { "cell_type": "markdown", "id": "b91edc5c", "metadata": {}, "source": [ "## 2. Importing our package" ] }, { "cell_type": "code", "execution_count": 1, "id": "2107103a", "metadata": {}, "outputs": [], "source": [ "from numethods.linalg import Vector, Matrix" ] }, { "cell_type": "markdown", "id": "f64a4d9a", "metadata": {}, "source": [ "## 3. What is a vector?\n", "\n", "A **vector** is an ordered collection of numbers (scalars). \n", "We can think of a vector as a column:\n", "\n", "$$\n", "v =\n", "\\begin{bmatrix}\n", "v_1 \\\\\n", "v_2 \\\\\n", "\\vdots \\\\\n", "v_n\n", "\\end{bmatrix}, \\quad v \\in \\mathbb{R}^n\n", "$$\n", "\n", "or as a row:\n", "\n", "$$\n", "v^T = \\begin{bmatrix} v_1 & v_2 & \\cdots & v_n \\end{bmatrix}.\n", "$$\n", "\n", "### Example\n", "A vector in $\\mathbb{R}^3$:\n", "\n", "$$\n", "v = \\begin{bmatrix} 1 \\\\ 2 \\\\ 3 \\end{bmatrix}.\n", "$$" ] }, { "cell_type": "code", "execution_count": 2, "id": "46c96e42", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "v = Vector([1.0, 2.0, 3.0])\n" ] } ], "source": [ "v = Vector([1, 2, 3])\n", "print(\"v =\", v)" ] }, { "cell_type": "markdown", "id": "5e951a1b", "metadata": {}, "source": [ "## 4. What is a matrix?\n", "\n", "A **matrix** is a rectangular array of numbers with rows and columns:\n", "\n", "$$\n", "A =\n", "\\begin{bmatrix}\n", "a_{11} & a_{12} & \\cdots & a_{1n} \\\\\n", "a_{21} & a_{22} & \\cdots & a_{2n} \\\\\n", "\\vdots & \\vdots & \\ddots & \\vdots \\\\\n", "a_{m1} & a_{m2} & \\cdots & a_{mn}\n", "\\end{bmatrix}, \\quad A \\in \\mathbb{R}^{m \\times n}\n", "$$" ] }, { "cell_type": "code", "execution_count": 3, "id": "a092da2f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A =\n", " Matrix([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]])\n" ] } ], "source": [ "A = Matrix([[1, 2, 3],\n", " [4, 5, 6],\n", " [7, 8, 9]])\n", "print(\"A =\\n\", A)" ] }, { "cell_type": "markdown", "id": "4f0d9ec4", "metadata": {}, "source": [ "## 5. Basic operations\n", "\n", "### 5.1 Vector addition and subtraction\n", "For $ u, v \\in \\mathbb{R}^n $:\n", "\n", "$$\n", "u + v = \\begin{bmatrix} u_1 + v_1 \\\\ u_2 + v_2 \\\\ \\vdots \\\\ u_n + v_n \\end{bmatrix},\n", "\\quad\n", "u - v = \\begin{bmatrix} u_1 - v_1 \\\\ u_2 - v_2 \\\\ \\vdots \\\\ u_n - v_n \\end{bmatrix}.\n", "$$" ] }, { "cell_type": "code", "execution_count": 4, "id": "b7571e71", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "u + v = Vector([4.0, 4.0, 4.0])\n", "u - v = Vector([2.0, 0.0, -2.0])\n" ] } ], "source": [ "u = Vector([3, 2, 1])\n", "v = Vector([1, 2, 3])\n", "\n", "print(\"u + v =\", u + v)\n", "print(\"u - v =\", u - v)" ] }, { "cell_type": "markdown", "id": "1aa8f396", "metadata": {}, "source": [ "### 5.2 Scalar multiplication\n", "For $ \\alpha \\in \\mathbb{R}, v \\in \\mathbb{R}^n $:\n", "\n", "$$\n", "\\alpha v = \\begin{bmatrix} \\alpha v_1 \\\\ \\alpha v_2 \\\\ \\vdots \\\\ \\alpha v_n \\end{bmatrix}.\n", "$$" ] }, { "cell_type": "code", "execution_count": 5, "id": "b4168dfe", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2 * v = Vector([2.0, 4.0, 6.0])\n", "v * 2 = Vector([2.0, 4.0, 6.0])\n" ] } ], "source": [ "v = Vector([1, 2, 3])\n", "\n", "print(\"2 * v =\", 2 * v)\n", "print(\"v * 2 =\", v * 2)" ] }, { "cell_type": "markdown", "id": "e4919b59", "metadata": {}, "source": [ "### 5.3 Matrix addition and subtraction\n", "For $ A, B \\in \\mathbb{R}^{m \\times n} $:\n", "\n", "$$\n", "A + B = [ a_{ij} + b_{ij} ], \\quad\n", "A - B = [ a_{ij} - b_{ij} ].\n", "$$" ] }, { "cell_type": "code", "execution_count": 6, "id": "bd544880", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A + B =\n", " Matrix([[6.0, 8.0], [10.0, 12.0]])\n", "A - B =\n", " Matrix([[-4.0, -4.0], [-4.0, -4.0]])\n" ] } ], "source": [ "A = Matrix([[1, 2], [3, 4]])\n", "B = Matrix([[5, 6], [7, 8]])\n", "\n", "print(\"A + B =\\n\", A + B)\n", "print(\"A - B =\\n\", A - B)" ] }, { "cell_type": "markdown", "id": "0a1fb7f1", "metadata": {}, "source": [ "### 5.4 Matrix-Vector multiplication\n", "For $ A \\in \\mathbb{R}^{m \\times n}, v \\in \\mathbb{R}^n $:\n", "\n", "$$\n", "(Av)_i = \\sum_{j=1}^n a_{ij} v_j.\n", "$$" ] }, { "cell_type": "code", "execution_count": 7, "id": "d65fd20e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A @ v = Vector([-2.0, -2.0, -2.0])\n" ] } ], "source": [ "A = Matrix([[1, 2, 3],\n", " [4, 5, 6],\n", " [7, 8, 9]])\n", "v = Vector([1, 0, -1])\n", "\n", "print(\"A @ v =\", A @ v)" ] }, { "cell_type": "markdown", "id": "8c2d30ec", "metadata": {}, "source": [ "### 5.5 Matrix-Matrix multiplication\n", "For $ A \\in \\mathbb{R}^{m \\times n}, B \\in \\mathbb{R}^{n \\times p} $:\n", "\n", "$$\n", "(AB)_{ij} = \\sum_{k=1}^n a_{ik} b_{kj}.\n", "$$" ] }, { "cell_type": "code", "execution_count": 8, "id": "f46a96ec", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A @ B =\n", " Matrix([[4.0, 4.0], [10.0, 8.0]])\n" ] } ], "source": [ "A = Matrix([[1, 2],\n", " [3, 4]])\n", "B = Matrix([[2, 0],\n", " [1, 2]])\n", "\n", "print(\"A @ B =\\n\", A @ B)" ] }, { "cell_type": "markdown", "id": "470e38cb", "metadata": {}, "source": [ "### 5.6 Transpose\n", "For $ A \\in \\mathbb{R}^{m \\times n} $:\n", "\n", "$$\n", "A^T_{ij} = A_{ji}.\n", "$$" ] }, { "cell_type": "code", "execution_count": 9, "id": "42168bd4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A^T =\n", " Matrix([[1.0, 4.0], [2.0, 5.0], [3.0, 6.0]])\n" ] } ], "source": [ "A = Matrix([[1, 2, 3],\n", " [4, 5, 6]])\n", "\n", "print(\"A^T =\\n\", A.T)" ] }, { "cell_type": "markdown", "id": "226efb8f", "metadata": {}, "source": [ "## 6. Norms\n", "\n", "Norms measure the **size** or **length** of vectors and matrices." ] }, { "cell_type": "markdown", "id": "120cf212", "metadata": {}, "source": [ "### 6.1 Vector norms\n", "- **1-norm**: $\\|v\\|_1 = \\sum |v_i|$ \n", "- **2-norm (Euclidean)**: $\\|v\\|_2 = \\sqrt{\\sum v_i^2}$ \n", "- **∞-norm**: $\\|v\\|_\\infty = \\max |v_i|$" ] }, { "cell_type": "code", "execution_count": 10, "id": "3b187412", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "‖v‖₁ = 12.0\n", "‖v‖₂ = 7.0710678118654755\n", "‖v‖∞ = 5.0\n" ] } ], "source": [ "v = Vector([3, -4, 5])\n", "\n", "print(\"‖v‖₁ =\", v.norm())\n", "print(\"‖v‖₂ =\", v.norm2())\n", "print(\"‖v‖∞ =\", v.norm_inf())" ] }, { "cell_type": "markdown", "id": "3ad8369e", "metadata": {}, "source": [ "### 6.2 Matrix norms\n", "- **Frobenius norm**: $\\|A\\|_F = \\sqrt{\\sum_{i,j} a_{ij}^2}$ \n", "- **1-norm** (maximum column sum): $\\|A\\|_1 = \\max_j \\sum_i |a_{ij}|$ \n", "- **∞-norm** (maximum row sum): $\\|A\\|_\\infty = \\max_i \\sum_j |a_{ij}|$" ] }, { "cell_type": "code", "execution_count": 11, "id": "72a8ae7c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "‖A‖_F = 5.477225575051661\n", "‖A‖₁ = 6.0\n", "‖A‖∞ = 7.0\n" ] } ], "source": [ "A = Matrix([[1, -2],\n", " [3, 4]])\n", "\n", "print(\"‖A‖_F =\", A.norm_fro())\n", "print(\"‖A‖₁ =\", A.norm())\n", "print(\"‖A‖∞ =\", A.norm_inf())" ] }, { "cell_type": "markdown", "id": "eab94be3", "metadata": {}, "source": [ "## 7. Summary\n", "- A **vector** is an element of $\\mathbb{R}^n$, a list of numbers. \n", "- A **matrix** is a rectangular array of numbers $\\mathbb{R}^{m \\times n}$. \n", "- We can add, subtract, and scale vectors/matrices. \n", "- Multiplication extends naturally: matrix-vector and matrix-matrix. \n", "- **Transpose** flips rows and columns. \n", "- **Norms** measure the size of vectors and matrices." ] } ], "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.10.11" } }, "nbformat": 4, "nbformat_minor": 5 }