add and fix some

This commit is contained in:
2025-11-10 17:24:53 +09:00
parent 49355f8f54
commit 12e68927ad
12 changed files with 595 additions and 10 deletions

View File

@@ -18,7 +18,9 @@
"source": [
"# Floating Point\n",
"\n",
"**Double Precision Floating Point Number Representation**\n",
"$$s\\times M \\times B^{e-E}$$\n",
"\n",
"**IEEE-754 Double Precision Floating Point Number Representation**\n",
"\n",
"$$(-1)^s \\times 2^{c-1023} \\times (1+f)$$\n",
"\n",
@@ -31,13 +33,35 @@
"cell_type": "markdown",
"id": "9c0b5413",
"metadata": {},
"source": []
"source": [
"## Machine Accuracy\n",
"\n",
"$$\\epsilon = b^{1-m}$$\n",
"\n",
"(typically $b = 2$, $m: \\text {bit for matissa}$)\n"
]
},
{
"cell_type": "markdown",
"id": "964d247f",
"metadata": {},
"source": []
"source": [
"# Errors\n",
"\n",
"* **Round-off Error**\n",
"\n",
"* **Truncation Error**\n",
"\n",
"Taylor Series\n",
"\n",
"approximations: zero order, first order(recommended), second order, etc..\n",
"\n",
"let $v_t$: true value, and $v_a$: approximation\n",
"Absolute error: $(v_t - v_a)$\n",
"Relative error: $(v_t - v_a) / v_t$\n",
"\n",
"so stopping condition is $e_a < e_s$ where $e_s$ is desired.\n"
]
},
{
"cell_type": "markdown",
@@ -48,8 +72,7 @@
"\n",
"When there is no or hard to find analytic solution.\n",
"\n",
"* General procedure of root finding\n",
"\n",
"General procedure of root finding\n",
"\n",
"## Incremental Search\n",
"\n",

View File

@@ -29,18 +29,75 @@
{
"cell_type": "code",
"execution_count": null,
"id": "3bed3d44",
"id": "e7357276",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"id": "1113de3c",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"id": "3bed3d44",
"metadata": {},
"source": [
"## Gauss Elimination\n",
"\n",
"## Gauss-Jordan Elimination"
]
},
{
"cell_type": "markdown",
"id": "a5239ed9",
"metadata": {},
"outputs": [],
"source": []
"source": [
"## LU decomposition\n",
"\n",
"$$A = LU$$\n",
"\n",
"$$Ax = b \\Rightarrow LUx = b \\Rightarrow \\\\\n",
"L^{-1}LUx = L^{-1}b \n",
"$$\n",
"\n",
"At that time, $Ux = c$ is easy to compute.\n",
"and compute $Lc = b$ to easily get $x$.\n",
"\n",
"\n",
"Various LUdcmps\n",
"* Doolittle $L_{ii} = 1$\n",
"* Crout $U_{ii} = 1$\n",
"* Cholesky $L_{ii} = U_{ii}$, which is appropriate for symmetric, positive-definite matrix\n",
"\n",
"### Crout Implementation\n",
"\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_{n1} & a_{n2} & \\cdots a_{nn}\n",
"\\end{bmatrix} = \\begin{bmatrix}\n",
"l_{11} & 0 & \\cdots 0 \\\\\n",
"l_{21} & l_{22} & \\cdots 0 \\\\\n",
"\\vdots & \\vdots & \\ddots \\vdots \\\\\n",
"l_{n1} & l_{n2} & \\cdots l_{nn}\n",
"\\end{bmatrix}\\begin{bmatrix}\n",
"1 & u_{12} & \\cdots u_{1n} \\\\\n",
"0 & 1 & \\cdots u_{2n} \\\\\n",
"\\vdots & \\vdots & \\ddots \\vdots \\\\\n",
"0 & 0 & \\cdots 1\n",
"\\end{bmatrix}$$\n",
"\n",
"$$\\begin{align*}\n",
"l_{i1} &= a_{i1} & (i = 1, 2, \\cdots, n) \\\\\n",
"u_{1j} &= a_{1j} / l_{11} & (j = 2, 3, \\cdots, n) \\\\\n",
"l_{ij} &= a_{ij} - \\sum_{k=1}^{j-1} {l_{ik} u_{kj}} & (j \\leq i, i = 2,3,\\cdots, n) \\\\\n",
"u_{ij} &= (a_{ij} - \\sum_{k=1}^{i-1} {l_{ik} u_{kj}}) / l_{ii} & (j > i, j = 3,4,\\cdots, n)\n",
"\\end{align*}$$"
]
},
{
"cell_type": "markdown",
@@ -111,6 +168,14 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "733012d2",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {

73
notes/3.ipynb Normal file
View File

@@ -0,0 +1,73 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"id": "af0a5571",
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"\n",
"sys.path.append(\"../modules\")\n",
"\n",
"import fixed"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "8a865027",
"metadata": {},
"outputs": [],
"source": [
"from fixed.c_fixedpoint import FixedPoint"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "802380cd",
"metadata": {},
"outputs": [
{
"ename": "SystemError",
"evalue": "invalid format string: %.8f, frac_bits=%d)",
"output_type": "error",
"traceback": [
"\u001b[31m---------------------------------------------------------------------------\u001b[39m",
"\u001b[31mSystemError\u001b[39m Traceback (most recent call last)",
"\u001b[36mFile \u001b[39m\u001b[32m~/uworkspace/2025-02-Numerical/.venv/lib/python3.12/site-packages/IPython/core/formatters.py:770\u001b[39m, in \u001b[36mPlainTextFormatter.__call__\u001b[39m\u001b[34m(self, obj)\u001b[39m\n\u001b[32m 763\u001b[39m stream = StringIO()\n\u001b[32m 764\u001b[39m printer = pretty.RepresentationPrinter(stream, \u001b[38;5;28mself\u001b[39m.verbose,\n\u001b[32m 765\u001b[39m \u001b[38;5;28mself\u001b[39m.max_width, \u001b[38;5;28mself\u001b[39m.newline,\n\u001b[32m 766\u001b[39m max_seq_length=\u001b[38;5;28mself\u001b[39m.max_seq_length,\n\u001b[32m 767\u001b[39m singleton_pprinters=\u001b[38;5;28mself\u001b[39m.singleton_printers,\n\u001b[32m 768\u001b[39m type_pprinters=\u001b[38;5;28mself\u001b[39m.type_printers,\n\u001b[32m 769\u001b[39m deferred_pprinters=\u001b[38;5;28mself\u001b[39m.deferred_printers)\n\u001b[32m--> \u001b[39m\u001b[32m770\u001b[39m \u001b[43mprinter\u001b[49m\u001b[43m.\u001b[49m\u001b[43mpretty\u001b[49m\u001b[43m(\u001b[49m\u001b[43mobj\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 771\u001b[39m printer.flush()\n\u001b[32m 772\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m stream.getvalue()\n",
"\u001b[36mFile \u001b[39m\u001b[32m~/uworkspace/2025-02-Numerical/.venv/lib/python3.12/site-packages/IPython/lib/pretty.py:411\u001b[39m, in \u001b[36mRepresentationPrinter.pretty\u001b[39m\u001b[34m(self, obj)\u001b[39m\n\u001b[32m 400\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m meth(obj, \u001b[38;5;28mself\u001b[39m, cycle)\n\u001b[32m 401\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m (\n\u001b[32m 402\u001b[39m \u001b[38;5;28mcls\u001b[39m \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28mobject\u001b[39m\n\u001b[32m 403\u001b[39m \u001b[38;5;66;03m# check if cls defines __repr__\u001b[39;00m\n\u001b[32m (...)\u001b[39m\u001b[32m 409\u001b[39m \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;28mcallable\u001b[39m(_safe_getattr(\u001b[38;5;28mcls\u001b[39m, \u001b[33m\"\u001b[39m\u001b[33m__repr__\u001b[39m\u001b[33m\"\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m))\n\u001b[32m 410\u001b[39m ):\n\u001b[32m--> \u001b[39m\u001b[32m411\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_repr_pprint\u001b[49m\u001b[43m(\u001b[49m\u001b[43mobj\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcycle\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 413\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m _default_pprint(obj, \u001b[38;5;28mself\u001b[39m, cycle)\n\u001b[32m 414\u001b[39m \u001b[38;5;28;01mfinally\u001b[39;00m:\n",
"\u001b[36mFile \u001b[39m\u001b[32m~/uworkspace/2025-02-Numerical/.venv/lib/python3.12/site-packages/IPython/lib/pretty.py:786\u001b[39m, in \u001b[36m_repr_pprint\u001b[39m\u001b[34m(obj, p, cycle)\u001b[39m\n\u001b[32m 784\u001b[39m \u001b[38;5;250m\u001b[39m\u001b[33;03m\"\"\"A pprint that just redirects to the normal repr function.\"\"\"\u001b[39;00m\n\u001b[32m 785\u001b[39m \u001b[38;5;66;03m# Find newlines and replace them with p.break_()\u001b[39;00m\n\u001b[32m--> \u001b[39m\u001b[32m786\u001b[39m output = \u001b[38;5;28;43mrepr\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mobj\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 787\u001b[39m lines = output.splitlines()\n\u001b[32m 788\u001b[39m \u001b[38;5;28;01mwith\u001b[39;00m p.group():\n",
"\u001b[31mSystemError\u001b[39m: invalid format string: %.8f, frac_bits=%d)"
]
}
],
"source": [
"FixedPoint(1.0, frac_bits=4)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "2025-02-Numerical (3.12.11)",
"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.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

41
notes/4.ipynb Normal file
View File

@@ -0,0 +1,41 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "db67ff8c",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "6e4ebb4e",
"metadata": {},
"source": [
"# Eigen\n",
"\n",
"$$Ax = \\lambda x$$\n",
"\n",
"$$(A-\\lambda I) x = 0$$\n",
"\n",
"$$\\lambda$$"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2a5a8e9b",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

225
notes/7.ipynb Normal file

File diff suppressed because one or more lines are too long

70
notes/8.ipynb Normal file
View File

@@ -0,0 +1,70 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "62ac3700",
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"sys.path.append(\"../modules\")\n",
"import numpy as np"
]
},
{
"cell_type": "markdown",
"id": "3d7375ae",
"metadata": {},
"source": [
"# Data Fitting\n",
"\n",
"It is approximate fit, which allows some errors depending on noise model.\n",
"\n",
"## Least-Square Data Fitting\n",
"\n",
"Given $N$ data points and a models has $M$ adjustable parameters.\n",
"\n",
"find $\\mathbf{a} = \\begin{bmatrix}a_1, a_2, \\cdots, a_M\\end{bmatrix}$ that minimizes $$S = \\sum_{i=1}^N{[y_i - y(x_i; \\mathbf{a})]^2}$$\n",
"\n",
"It is also equivalent to **maximum likelihood estimation** if $e_i$ is **independently distributed Gaussian**.\n",
"\n",
"\n",
"### Line Fitting\n",
"\n",
"$y = a + bx, \\quad e_i = y_i - (a + bx)$\n",
"\n",
"$S(a, b) = \\sum_{i = 1}^{N}e_i^2$\n",
"\n",
"$$\\begin{align*}\n",
"\\frac{\\partial S}{\\partial a} &= 2\\sum_{i = 1}^N [y_i-a-bx_i](-1) = 0\\\\\n",
"\\frac{\\partial S}{\\partial b} &= 2\\sum_{i = 1}^N [y_i =a - bx_i](-x_i) = 0\\\\\n",
"\\end{align*}$$\n",
"\n",
"$$\\begin{align*}\n",
"a\\sum 1 + b\\sum x_i &= \\sum y_i\\\\\n",
"a\\sum x_i + b \\sum x_i^2 &= \\sum x_iy_i\n",
"\\end{align*}$$\n",
"\n",
"$$\\therefore \\begin{bmatrix}\n",
"a \\\\ b\n",
"\\end{bmatrix} = \\begin{bmatrix}\n",
"\\sum 1 & \\sum x_i \\\\\n",
"\\sum x_i & \\sum x_i^2 \\\\\n",
"\\end{bmatrix}^{-1} \\begin{bmatrix}\n",
" \\sum y_i \\\\ \\sum x_i y_i\n",
"\\end{bmatrix}$$\n",
"\n",
"\n",
"\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 5
}