├── README.md └── lecture ├── Pre_OpenFOAM.pdf ├── lecture00.pdf ├── lecture01.pdf ├── lecture02.pdf ├── lecture03.pdf ├── lecture04.pdf ├── lecture05.pdf ├── lecture06.pdf ├── lecture07.pdf ├── lecture08.pdf ├── lecture09.pdf ├── lecture10.pdf ├── lecture11.pdf ├── lecture12.pdf ├── lecture13.pdf ├── lecture14.pdf ├── lecture15.pdf ├── lecture16.pdf ├── lecture17.pdf ├── lecture18.pdf ├── lecture19.pdf └── lecture20 ├── MatrixCalculus.pdf ├── Matrix_derivatives_cribsheet.pdf ├── OFW17_Cambridge_Linear_solvers_training.pdf ├── code ├── Lecture01 │ ├── .ipynb_checkpoints │ │ ├── Untitled-checkpoint.ipynb │ │ └── Vector_and_Matrices-checkpoint.ipynb │ ├── .~Vector_and_Matrices.ipynb │ ├── Vector_and_Matrices.ipynb │ ├── Vector_and_Matrices.md │ ├── bisection.png │ ├── bisection.svg │ └── bisection1.png ├── Lecture02 │ ├── .Lecture 02.ipynb.swx │ ├── .ipynb_checkpoints │ │ ├── Lecture 02-checkpoint.ipynb │ │ └── Untitled-checkpoint.ipynb │ └── Lecture 02.ipynb ├── Lecture03 │ ├── .ipynb_checkpoints │ │ ├── .Gauss_elimination-checkpoint.ipynb.swp │ │ ├── .Gauss_elimination-checkpoint.ipynb.swx │ │ ├── .swp │ │ ├── .swpx │ │ └── Gauss_elimination-checkpoint.ipynb │ └── Gauss_elimination.ipynb └── Lecture04 │ ├── .ipynb_checkpoints │ ├── A_painless-checkpoint.ipynb │ ├── MSD_CG-checkpoint.ipynb │ ├── goog-downloadwhite-proto.metadata │ ├── goog-downloadwhite-proto.vlpset │ ├── goog-malware-proto.metadata │ ├── goog-malware-proto.vlpset │ ├── goog-phish-proto.metadata │ ├── goog-unwanted-proto.metadata │ └── goog-unwanted-proto.vlpset │ └── MSD_CG.ipynb ├── matrixcookbook.pdf └── vmls.pdf /README.md: -------------------------------------------------------------------------------- 1 | # 2206CFD_OpenFOAM 2 | 3 | ## 简单说明 4 | 这是第一期培训的课件,开源给大家,希望能帮助到大家。 5 | 6 | ## 阅读顺序 7 | 1. `Pre_OpenFOAM.pdf`是前两周课程,关于工具链的。包含`Linux`下的一些工具,有兴趣可以参考 8 | 2. `lecture00-19`按照顺序阅读 9 | 3. `lecture20`里面是关于$Ax = b$的内容,是用`jupyter`写的教案,有四个lecture按照顺序阅读。 10 | 11 | ## 总结 12 | 第一次课程,希望大家多多包含。课程视频和一些case文件,会慢慢给大家开源出来。 13 | -------------------------------------------------------------------------------- /lecture/Pre_OpenFOAM.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wo315/2206CFD_OpenFOAM/c0be4f9a1eb0e08b621b6b3719dbdf8092d72f1e/lecture/Pre_OpenFOAM.pdf -------------------------------------------------------------------------------- /lecture/lecture00.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wo315/2206CFD_OpenFOAM/c0be4f9a1eb0e08b621b6b3719dbdf8092d72f1e/lecture/lecture00.pdf -------------------------------------------------------------------------------- /lecture/lecture01.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wo315/2206CFD_OpenFOAM/c0be4f9a1eb0e08b621b6b3719dbdf8092d72f1e/lecture/lecture01.pdf -------------------------------------------------------------------------------- /lecture/lecture02.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wo315/2206CFD_OpenFOAM/c0be4f9a1eb0e08b621b6b3719dbdf8092d72f1e/lecture/lecture02.pdf -------------------------------------------------------------------------------- /lecture/lecture03.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wo315/2206CFD_OpenFOAM/c0be4f9a1eb0e08b621b6b3719dbdf8092d72f1e/lecture/lecture03.pdf -------------------------------------------------------------------------------- /lecture/lecture04.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wo315/2206CFD_OpenFOAM/c0be4f9a1eb0e08b621b6b3719dbdf8092d72f1e/lecture/lecture04.pdf -------------------------------------------------------------------------------- /lecture/lecture05.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wo315/2206CFD_OpenFOAM/c0be4f9a1eb0e08b621b6b3719dbdf8092d72f1e/lecture/lecture05.pdf -------------------------------------------------------------------------------- /lecture/lecture06.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wo315/2206CFD_OpenFOAM/c0be4f9a1eb0e08b621b6b3719dbdf8092d72f1e/lecture/lecture06.pdf -------------------------------------------------------------------------------- /lecture/lecture07.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wo315/2206CFD_OpenFOAM/c0be4f9a1eb0e08b621b6b3719dbdf8092d72f1e/lecture/lecture07.pdf -------------------------------------------------------------------------------- /lecture/lecture08.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wo315/2206CFD_OpenFOAM/c0be4f9a1eb0e08b621b6b3719dbdf8092d72f1e/lecture/lecture08.pdf -------------------------------------------------------------------------------- /lecture/lecture09.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wo315/2206CFD_OpenFOAM/c0be4f9a1eb0e08b621b6b3719dbdf8092d72f1e/lecture/lecture09.pdf -------------------------------------------------------------------------------- /lecture/lecture10.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wo315/2206CFD_OpenFOAM/c0be4f9a1eb0e08b621b6b3719dbdf8092d72f1e/lecture/lecture10.pdf -------------------------------------------------------------------------------- /lecture/lecture11.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wo315/2206CFD_OpenFOAM/c0be4f9a1eb0e08b621b6b3719dbdf8092d72f1e/lecture/lecture11.pdf -------------------------------------------------------------------------------- /lecture/lecture12.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wo315/2206CFD_OpenFOAM/c0be4f9a1eb0e08b621b6b3719dbdf8092d72f1e/lecture/lecture12.pdf -------------------------------------------------------------------------------- /lecture/lecture13.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wo315/2206CFD_OpenFOAM/c0be4f9a1eb0e08b621b6b3719dbdf8092d72f1e/lecture/lecture13.pdf -------------------------------------------------------------------------------- /lecture/lecture14.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wo315/2206CFD_OpenFOAM/c0be4f9a1eb0e08b621b6b3719dbdf8092d72f1e/lecture/lecture14.pdf -------------------------------------------------------------------------------- /lecture/lecture15.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wo315/2206CFD_OpenFOAM/c0be4f9a1eb0e08b621b6b3719dbdf8092d72f1e/lecture/lecture15.pdf -------------------------------------------------------------------------------- /lecture/lecture16.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wo315/2206CFD_OpenFOAM/c0be4f9a1eb0e08b621b6b3719dbdf8092d72f1e/lecture/lecture16.pdf -------------------------------------------------------------------------------- /lecture/lecture17.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wo315/2206CFD_OpenFOAM/c0be4f9a1eb0e08b621b6b3719dbdf8092d72f1e/lecture/lecture17.pdf -------------------------------------------------------------------------------- /lecture/lecture18.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wo315/2206CFD_OpenFOAM/c0be4f9a1eb0e08b621b6b3719dbdf8092d72f1e/lecture/lecture18.pdf -------------------------------------------------------------------------------- /lecture/lecture19.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wo315/2206CFD_OpenFOAM/c0be4f9a1eb0e08b621b6b3719dbdf8092d72f1e/lecture/lecture19.pdf -------------------------------------------------------------------------------- /lecture/lecture20/MatrixCalculus.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wo315/2206CFD_OpenFOAM/c0be4f9a1eb0e08b621b6b3719dbdf8092d72f1e/lecture/lecture20/MatrixCalculus.pdf -------------------------------------------------------------------------------- /lecture/lecture20/Matrix_derivatives_cribsheet.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wo315/2206CFD_OpenFOAM/c0be4f9a1eb0e08b621b6b3719dbdf8092d72f1e/lecture/lecture20/Matrix_derivatives_cribsheet.pdf -------------------------------------------------------------------------------- /lecture/lecture20/OFW17_Cambridge_Linear_solvers_training.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wo315/2206CFD_OpenFOAM/c0be4f9a1eb0e08b621b6b3719dbdf8092d72f1e/lecture/lecture20/OFW17_Cambridge_Linear_solvers_training.pdf -------------------------------------------------------------------------------- /lecture/lecture20/code/Lecture01/.ipynb_checkpoints/Untitled-checkpoint.ipynb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wo315/2206CFD_OpenFOAM/c0be4f9a1eb0e08b621b6b3719dbdf8092d72f1e/lecture/lecture20/code/Lecture01/.ipynb_checkpoints/Untitled-checkpoint.ipynb -------------------------------------------------------------------------------- /lecture/lecture20/code/Lecture01/.ipynb_checkpoints/Vector_and_Matrices-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "554562d9", 6 | "metadata": {}, 7 | "source": [ 8 | "# Lecture 01 Vectors and Matrices\n" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "90e93f6d", 14 | "metadata": {}, 15 | "source": [ 16 | "## row vectors行向量\n", 17 | "\n", 18 | "$$\n", 19 | "b = \\begin{bmatrix}\n", 20 | "1, 2, 3\n", 21 | "\\end{bmatrix}\n", 22 | "$$" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": null, 28 | "id": "c21f124e", 29 | "metadata": {}, 30 | "outputs": [], 31 | "source": [ 32 | "import numpy as np\n", 33 | "\n", 34 | "# ways to make a row vector\n", 35 | "x1 = [1, 3, 2] # list\n", 36 | "x2 = np.array([1, 3, 2])\n", 37 | "x3 = np.matrix([1,3, 2])\n", 38 | "\n", 39 | "display(x1)\n", 40 | "display(x2)\n", 41 | "display(x3)" 42 | ] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "id": "d680e94d", 47 | "metadata": {}, 48 | "source": [ 49 | "## column 列向量" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": null, 55 | "id": "6f2feefe", 56 | "metadata": {}, 57 | "outputs": [], 58 | "source": [ 59 | "import numpy as np\n", 60 | "\n", 61 | "# ways to make a column vector\n", 62 | "x1 = np.matrix([[1], [3], [2]])\n", 63 | "x2 = np.array([[1],[3],[2]])\n", 64 | "display(x1)\n", 65 | "display(x2)" 66 | ] 67 | }, 68 | { 69 | "cell_type": "markdown", 70 | "id": "dbb8a8a7", 71 | "metadata": {}, 72 | "source": [ 73 | "## 更多表示方式\n", 74 | "$$\n", 75 | "b = \\begin{bmatrix}\n", 76 | "1\\\\\n", 77 | " 2\\\\\n", 78 | " 3\n", 79 | "\\end{bmatrix}\n", 80 | "$$" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": null, 86 | "id": "4dc4c647", 87 | "metadata": {}, 88 | "outputs": [], 89 | "source": [ 90 | "import numpy as np\n", 91 | "\n", 92 | "x1 = np.arange(0, 11, 1)\n", 93 | "x2 = np.arange(0, 11, 2)\n", 94 | "x3 = np.arange(0, 1.2, 0.2)\n", 95 | "x4 = np.arange(0, 9, 2)\n", 96 | "x5 = np.arange(0, 5)\n", 97 | "\n", 98 | "display(x1)\n", 99 | "display(x2)\n", 100 | "display(x3)\n", 101 | "display(x4)\n", 102 | "display(x5)" 103 | ] 104 | }, 105 | { 106 | "cell_type": "markdown", 107 | "id": "5f092ddb", 108 | "metadata": {}, 109 | "source": [ 110 | "## Matrices矩阵表示\n", 111 | "$$\n", 112 | "A=\\begin{bmatrix}\n", 113 | "1 & 3 & 2\\\\\n", 114 | "5 & 6 & 7\\\\\n", 115 | "8 & 3 & 1\n", 116 | "\\end{bmatrix}\n", 117 | "$$" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": null, 123 | "id": "8a3303b8", 124 | "metadata": {}, 125 | "outputs": [], 126 | "source": [ 127 | "import numpy as np\n", 128 | "\n", 129 | "A = np.array([[1, 3, 2], [5, 6, 7], [8, 3, 1]])\n", 130 | "display(A)" 131 | ] 132 | }, 133 | { 134 | "cell_type": "markdown", 135 | "id": "db2329da", 136 | "metadata": {}, 137 | "source": [ 138 | "## 矩阵切片,提取\n", 139 | "$$\n", 140 | "A=\\begin{bmatrix}\n", 141 | "1 & 3 & 2\\\\\n", 142 | "5 & 6 & 7\\\\\n", 143 | "8 & 3 & 1\n", 144 | "\\end{bmatrix}\n", 145 | "$$" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": null, 151 | "id": "03a854fa", 152 | "metadata": {}, 153 | "outputs": [], 154 | "source": [ 155 | "x1 = A[1, 2]\n", 156 | "x2 = A[1, :]\n", 157 | "x3 = A[:, 2]\n", 158 | "x4 = A[1:, 2]\n", 159 | "display(x1)\n", 160 | "display(x2)\n", 161 | "display(x3)\n", 162 | "display(x4)" 163 | ] 164 | }, 165 | { 166 | "cell_type": "markdown", 167 | "id": "0c0b6eff", 168 | "metadata": {}, 169 | "source": [ 170 | "## 向量转置 \n", 171 | "\n", 172 | "$$\n", 173 | "x = \\begin{bmatrix}\n", 174 | "2 + 3i \\\\\n", 175 | "7\\\\\n", 176 | "1\n", 177 | "\\end{bmatrix}\n", 178 | "$$" 179 | ] 180 | }, 181 | { 182 | "cell_type": "code", 183 | "execution_count": null, 184 | "id": "e3f6788f", 185 | "metadata": {}, 186 | "outputs": [], 187 | "source": [ 188 | "x = np.array([2+3j, 7, 1])\n", 189 | "x1 = x.T\n", 190 | "x2 = x.conjugate()\n", 191 | "x3 = x.conj()\n", 192 | "display(x)\n", 193 | "display(x1)\n", 194 | "display(x2)\n", 195 | "display(x3)" 196 | ] 197 | }, 198 | { 199 | "cell_type": "markdown", 200 | "id": "9dc48cd2", 201 | "metadata": {}, 202 | "source": [ 203 | "# Lecture 02 Programming logic: IF and For,判断和循环语句" 204 | ] 205 | }, 206 | { 207 | "cell_type": "markdown", 208 | "id": "3aa8d800", 209 | "metadata": {}, 210 | "source": [ 211 | "## The IF statement, if语句\n", 212 | "\n", 213 | "```python\n", 214 | "if (logical statement)\n", 215 | " (expressions to execute)\n", 216 | "elif (logical statement)\n", 217 | " (expressions to execute)\n", 218 | "elif (logical statement)\n", 219 | " (expressions to execute)\n", 220 | "else\n", 221 | " (expressions to execute)\n", 222 | "```\n", 223 | "\n", 224 | "## The FOR loop, for循环" 225 | ] 226 | }, 227 | { 228 | "cell_type": "code", 229 | "execution_count": null, 230 | "id": "6a4f7169", 231 | "metadata": {}, 232 | "outputs": [], 233 | "source": [ 234 | "import numpy as np\n", 235 | "\n", 236 | "a = 0\n", 237 | "for j in np.arange(0, 5):\n", 238 | " a = a + (j+1)\n", 239 | " display(a)" 240 | ] 241 | }, 242 | { 243 | "cell_type": "code", 244 | "execution_count": null, 245 | "id": "1c90fa60", 246 | "metadata": {}, 247 | "outputs": [], 248 | "source": [ 249 | "import numpy as np\n", 250 | "\n", 251 | "a = 0\n", 252 | "for j in np.arange(0, 5, 2):\n", 253 | " a = a + (j+1)\n", 254 | " display(a)" 255 | ] 256 | }, 257 | { 258 | "cell_type": "code", 259 | "execution_count": null, 260 | "id": "3a1a3ec8", 261 | "metadata": {}, 262 | "outputs": [], 263 | "source": [ 264 | "import numpy as np\n", 265 | "\n", 266 | "a = 0\n", 267 | "loop = [1, 5, 4]\n", 268 | "for j in loop:\n", 269 | " a = a + j\n", 270 | " display(a)" 271 | ] 272 | }, 273 | { 274 | "cell_type": "markdown", 275 | "id": "5fb803cf", 276 | "metadata": {}, 277 | "source": [ 278 | "## 算例,二分找根" 279 | ] 280 | }, 281 | { 282 | "cell_type": "markdown", 283 | "id": "8cf04eeb", 284 | "metadata": {}, 285 | "source": [ 286 | "![image](bisection.png)" 287 | ] 288 | }, 289 | { 290 | "cell_type": "markdown", 291 | "id": "f90d79d6", 292 | "metadata": {}, 293 | "source": [ 294 | "## 算例1\n", 295 | "$$\n", 296 | "f(x) = exp(x) - tan(x)\n", 297 | "$$" 298 | ] 299 | }, 300 | { 301 | "cell_type": "code", 302 | "execution_count": null, 303 | "id": "5e778a58", 304 | "metadata": {}, 305 | "outputs": [], 306 | "source": [ 307 | "xr = -2.8; xl = -4\n", 308 | "\n", 309 | "for j in range(100):\n", 310 | " xc = (xr + xl) / 2\n", 311 | " fc = np.exp(xc) - np.tan(xc)\n", 312 | " if ( fc > 0 ):\n", 313 | " xl = xc\n", 314 | " else:\n", 315 | " xr = xc\n", 316 | " \n", 317 | " if ( abs(fc) < 1e-5 ):\n", 318 | " display(xc)\n", 319 | " display(j)\n", 320 | " break\n" 321 | ] 322 | }, 323 | { 324 | "cell_type": "code", 325 | "execution_count": null, 326 | "id": "1c87df14", 327 | "metadata": {}, 328 | "outputs": [], 329 | "source": [ 330 | "import numpy as np\n", 331 | "import matplotlib.pyplot as plt\n", 332 | "\n", 333 | "\n", 334 | "fig, ax = plt.subplots(figsize=(24, 12))\n", 335 | "x = np.linspace(-4.1, -2, 1000)\n", 336 | "y = 0*x\n", 337 | "\n", 338 | "plt.plot(x, np.exp(x)-np.tan(x), color='k')\n", 339 | "plt.plot(x, y, color='r')\n", 340 | "\n", 341 | "plt.plot(-4, np.exp(-4)-np.tan(-4), marker='o', color='g')\n", 342 | "plt.plot(-2.8, np.exp(-2.8)-np.tan(-2.8), marker='o', color='g')\n", 343 | "\n", 344 | "plt.grid()\n", 345 | "plt.ylim(-2, 2)\n", 346 | "\n", 347 | "\n", 348 | "xr = -2.8; xl = -4\n", 349 | "\n", 350 | "for j in range(100):\n", 351 | " xc = (xr + xl) / 2\n", 352 | " fc = np.exp(xc) - np.tan(xc)\n", 353 | " if ( fc > 0 ):\n", 354 | " xl = xc\n", 355 | " else:\n", 356 | " xr = xc\n", 357 | " \n", 358 | " if ( abs(fc) < 1e-5 ):\n", 359 | " break\n", 360 | " plt.plot(xc, fc, marker='o', color='k')\n", 361 | " plt.annotate(j, [xc, fc], color='r',fontsize=20)\n", 362 | "\n" 363 | ] 364 | } 365 | ], 366 | "metadata": { 367 | "kernelspec": { 368 | "display_name": "Python 3 (ipykernel)", 369 | "language": "python", 370 | "name": "python3" 371 | }, 372 | "language_info": { 373 | "codemirror_mode": { 374 | "name": "ipython", 375 | "version": 3 376 | }, 377 | "file_extension": ".py", 378 | "mimetype": "text/x-python", 379 | "name": "python", 380 | "nbconvert_exporter": "python", 381 | "pygments_lexer": "ipython3", 382 | "version": "3.8.10" 383 | } 384 | }, 385 | "nbformat": 4, 386 | "nbformat_minor": 5 387 | } 388 | -------------------------------------------------------------------------------- /lecture/lecture20/code/Lecture01/.~Vector_and_Matrices.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "b60e91e9", 6 | "metadata": {}, 7 | "source": [ 8 | "# Lecture 02 Linear systems" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "88d7e8f7", 14 | "metadata": {}, 15 | "source": [ 16 | "\n", 17 | "## Section 2.1 Matrix and Vector Properties\n", 18 | "\n", 19 | "$$\n", 20 | "A = \\begin{bmatrix}\n", 21 | "a_{11} & a_{12} &\\dots &a_{1n} \\\\\n", 22 | "a_{21} & a_{22} &\\dots &a_{1n} \\\\\n", 23 | "\\vdots \\\\\n", 24 | "a_{m1} & a_{m2} &\\dots &a_{mn} \n", 25 | "\\end{bmatrix} = (a_{ij})\n", 26 | "$$" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "id": "176f0ed9", 32 | "metadata": {}, 33 | "source": [ 34 | "## Concepts概念\n", 35 | "\n", 36 | "Transpose(转置):$\\mathbf{A}^T = (a_{ij})^T = (a_{ji})\\rightarrow \\ if \\ \\mathbf{A} = \\begin{bmatrix}1 &5\\\\ 2 &3\\end{bmatrix} then \\ A^T = \\begin{bmatrix}1 &5\\\\2 &3\\\\\\end{bmatrix}$\n", 37 | "\n", 38 | "\n", 39 | "\n", 40 | "Complex Conjugate(复转置): $\\overline{A} = \\overline{a_{ij}} \\rightarrow if \\ A = \\begin{bmatrix} i &5\\\\ 3+i &6\\end{bmatrix} then \\ \\overline{A} = \\begin{bmatrix}-i & 5\\\\ 3-i &6\\end{bmatrix}$\n", 41 | "\n", 42 | "\n", 43 | "Adjoint: $\\overline{A}^T = A^* \\rightarrow if \\ \\mathbf{A}= \\begin{bmatrix} i &5\\\\ 3+i &6\\end{bmatrix} then \\ \\overline{A} = \\begin{bmatrix}-i & 3-i\\\\ 5 &6\\end{bmatrix}$\n", 44 | "\n", 45 | "\n", 46 | "伴随变换更合适$A^H$,共轭转置矩阵" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": null, 52 | "id": "30c73870", 53 | "metadata": {}, 54 | "outputs": [], 55 | "source": [ 56 | "import numpy as np\n", 57 | "\n", 58 | "A = np.matrix([[1, 5], [2, 3]])\n", 59 | "A.T\n", 60 | "print(A)\n", 61 | "print(\"........................\")\n", 62 | "\n", 63 | "B = np.matrix([[1j, 5], [3+1j, 6]])\n", 64 | "print(B.conjugate())\n", 65 | "print(\"........................\")\n", 66 | "\n", 67 | "C = np.matrix([[1j, 5], [3+1j, 6]])\n", 68 | "print(C.getH()) # Hermitian transpose" 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "id": "cbc096dc", 74 | "metadata": {}, 75 | "source": [ 76 | "## Matrix equalities and addition\n", 77 | "\n", 78 | "$\\mathbf{A} = \\mathbf{B}$,如果$a_{ij} = b_{ij}$\n", 79 | "\n", 80 | "$\\mathbf{A} = 0$,表示$a_{ij} = 0$\n", 81 | "\n", 82 | "$\\mathbf{A}\\pm\\mathbf{B} = (a_{ij})\\pm b_{ij}) = (a_{ij}\\pm b_{ij})$\n", 83 | "\n", 84 | "$\\textbf{commutative}: \\mathbf{A} + \\mathbf{B} = \\mathbf{B} + \\mathbf{A}$\n", 85 | "\n", 86 | "$\\textbf{Associative}: \\mathbf{A} + (\\mathbf{B} + \\mathbf{C})= (\\mathbf{A} + \\mathbf{B}) + \\mathbf{C}$" 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "id": "45725a33", 92 | "metadata": {}, 93 | "source": [ 94 | "## Matrix multiplication\n", 95 | "\n", 96 | "Multiply by a number: $\\alpha\\mathbf{A} = \\alpha(a_{ij})=(\\alpha a_{ij})$\n", 97 | "\n", 98 | "Matrix multiply:$\\mathbf{AB} = C, 其中:\\ c_{ij}=\\sum_{k=1}^na_{ik}b_{kj}$\n", 99 | "\n", 100 | "$$\n", 101 | "\\begin{pmatrix}\n", 102 | "3 &2 &1 \\\\\n", 103 | "6 &5 &0 \\\\\n", 104 | "1 &8 &3\n", 105 | "\\end{pmatrix}\n", 106 | "\\begin{pmatrix}\n", 107 | "1\\\\\n", 108 | "0\\\\\n", 109 | "2\n", 110 | "\\end{pmatrix}\n", 111 | "= \\begin{pmatrix}\n", 112 | "3\\cdot1 + 2\\cdot0 + 1\\cdot2 \\\\\n", 113 | "6\\cdot1 + 5\\cdot0 + 0\\cdot2 \\\\\n", 114 | "1\\cdot1 + 8\\cdot0 + 3\\cdot2\n", 115 | "\\end{pmatrix}\n", 116 | "=\n", 117 | "\\begin{pmatrix}\n", 118 | "5\\\\\n", 119 | "6\\\\\n", 120 | "7\n", 121 | "\\end{pmatrix}\n", 122 | "$$\n", 123 | "\n", 124 | "$\\textbf{distributive}: \\mathbf{A}(\\mathbf{B}+\\mathbf{C})= \\mathbf{A}\\mathbf{B}+\\mathbf{A}\\mathbf{C})$\n", 125 | "\n", 126 | "$\\textbf{Associative}: \\mathbf{A}(\\mathbf{B}\\mathbf{C})= (\\mathbf{A}\\mathbf{B})\\mathbf{C}$\n", 127 | "\n", 128 | "$\\textbf{not commutative}: \\mathbf{A}\\mathbf{B} \\neq \\mathbf{B}\\mathbf{A}$" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": 49, 134 | "id": "bd3ff409", 135 | "metadata": {}, 136 | "outputs": [ 137 | { 138 | "name": "stdout", 139 | "output_type": "stream", 140 | "text": [ 141 | "[[ 3 2 1]\n", 142 | " [ 0 0 0]\n", 143 | " [ 2 16 6]]\n", 144 | "[[5]\n", 145 | " [6]\n", 146 | " [7]]\n" 147 | ] 148 | } 149 | ], 150 | "source": [ 151 | "import numpy as np\n", 152 | "A = np.array([[3, 2, 1],\n", 153 | " [6, 5, 0],\n", 154 | " [1, 8, 3]])\n", 155 | "B = np.array([[1],\n", 156 | " [0],\n", 157 | " [2]])\n", 158 | "\n", 159 | "C = A*B # element-wise\n", 160 | "print(C)\n", 161 | "\n", 162 | "D = A@B\n", 163 | "print(D)" 164 | ] 165 | }, 166 | { 167 | "cell_type": "markdown", 168 | "id": "2d36e831", 169 | "metadata": {}, 170 | "source": [ 171 | "## Vector multiplication\n", 172 | "\n", 173 | "Vectors: \n", 174 | "\n", 175 | "$\\mathbf{u}^T\\mathbf{v} = \\sum_{i=1}^n u_iv_i$\n", 176 | "\n", 177 | "$$\n", 178 | "\\large{\n", 179 | "\\mathbf{u}^T\\mathbf{v}=(u_1, u_2, \\cdots, u_n)\n", 180 | "\\begin{pmatrix}\n", 181 | "v_1\\\\\n", 182 | "v_2\\\\\n", 183 | "\\vdots\\\\\n", 184 | "v_n\n", 185 | "\\end{pmatrix}\n", 186 | "= (u_1v_1 + u_2v_2 + \\cdots + u_nv_n)}\n", 187 | "$$\n", 188 | "\n", 189 | "## inner product 内积\n", 190 | "\n", 191 | "Inner product:\n", 192 | "\n", 193 | "$\\mathbf{(u, v)} = \\sum_{i=1}^{n}=u_i\\overline{v_i}=\\mathbf{u}^T\\overline{v}$\n", 194 | "\n", 195 | "* $\\mathbf{(u,v)}=\\overline{(v, u)}$\n", 196 | "\n", 197 | "\n", 198 | "* $(\\alpha \\mathbf{u}, \\mathbf{v}) = \\alpha(\\mathbf{u},\\mathbf{v})$\n", 199 | "\n", 200 | "\n", 201 | "* $(\\mathbf{u}, \\alpha\\mathbf{v})=\\overline{\\alpha}(\\mathbf{u},\\mathbf{v})$\n", 202 | "\n", 203 | "\n", 204 | "* $\\mathbf{u}, \\mathbf{v+w} = (\\mathbf{u},\\mathbf{v}) + (\\mathbf{u, w})$\n", 205 | "\n", 206 | "Vector Magnitudes:\n", 207 | "\n", 208 | "$\\mathbf{(u, u)}^{1/2} = \\sum_{i=1}^{n}u_i\\overline{u_i}=\\sum_{i=1}^{n}|u_i|$\n", 209 | "\n", 210 | "Orthogonality:\n", 211 | "\n", 212 | "$(\\mathbf{u,v})=0$" 213 | ] 214 | }, 215 | { 216 | "cell_type": "markdown", 217 | "id": "fb5de3bc", 218 | "metadata": {}, 219 | "source": [ 220 | "## Linear dependence 线性相关无关\n", 221 | "\n", 222 | "$$c_1\\mathbf{X_1} + c_2\\mathbf{X_2} + \\cdots + c_n\\mathbf{X_n}= 0$$\n", 223 | "\n", 224 | "如果$c_i$不全为0是线性相关,否则线性无关\n", 225 | "\n", 226 | "\n", 227 | "## Inverse 逆矩阵\n", 228 | "\n", 229 | "Identity Matrix: $\\mathbf{I} = \\delta_{ij},对于i=j,\\delta_{ij} = 1,否则,\\delta_{ij}=0$\n", 230 | "\n", 231 | "Inverse Matrix: $\\mathbf{AB = I},如果det(A)\\neq=0,则\\mathbf{B=A^{-1}}$\n", 232 | "\n", 233 | "## Solving $\\mathbf{A}x=b$\n", 234 | "\n", 235 | "$$\n", 236 | "if \\ det(A)\\neq 0 \\ x = \\mathbf{A}^{-1}b\n", 237 | "$$" 238 | ] 239 | }, 240 | { 241 | "cell_type": "code", 242 | "execution_count": null, 243 | "id": "639bbc46", 244 | "metadata": {}, 245 | "outputs": [], 246 | "source": [] 247 | } 248 | ], 249 | "metadata": { 250 | "kernelspec": { 251 | "display_name": "Python 3 (ipykernel)", 252 | "language": "python", 253 | "name": "python3" 254 | }, 255 | "language_info": { 256 | "codemirror_mode": { 257 | "name": "ipython", 258 | "version": 3 259 | }, 260 | "file_extension": ".py", 261 | "mimetype": "text/x-python", 262 | "name": "python", 263 | "nbconvert_exporter": "python", 264 | "pygments_lexer": "ipython3", 265 | "version": "3.8.10" 266 | } 267 | }, 268 | "nbformat": 4, 269 | "nbformat_minor": 5 270 | } 271 | -------------------------------------------------------------------------------- /lecture/lecture20/code/Lecture01/Vector_and_Matrices.md: -------------------------------------------------------------------------------- 1 | # Lecture 01 Vectors and Matrices 2 | 3 | 4 | ## row vectors行向量 5 | 6 | $$ 7 | b = \begin{bmatrix} 8 | 1, 2, 3 9 | \end{bmatrix} 10 | $$ 11 | 12 | 13 | ```python 14 | import numpy as np 15 | 16 | # ways to make a row vector 17 | x1 = [1, 3, 2] 18 | x2 = np.array([1, 3, 2]) 19 | x3 = np.matrix([1,3, 2]) 20 | 21 | display(x1) 22 | display(x2) 23 | display(x3) 24 | ``` 25 | 26 | ## 列向量 27 | 28 | 29 | ```python 30 | import numpy as np 31 | 32 | # ways to make a column vector 33 | x1 = np.matrix([[1], [3], [2]]) 34 | x2 = np.array([[1],[3],[2]]) 35 | display(x1) 36 | display(x2) 37 | ``` 38 | 39 | ## 更多表示方式 40 | $$ 41 | b = \begin{bmatrix} 42 | 1\\ 43 | 2\\ 44 | 3 45 | \end{bmatrix} 46 | $$ 47 | 48 | 49 | ```python 50 | import numpy as np 51 | 52 | x1 = np.arange(0, 11, 1) 53 | x2 = np.arange(0, 11, 2) 54 | x3 = np.arange(0, 1.2, 0.2) 55 | x4 = np.arange(0, 9, 2) 56 | x5 = np.arange(0, 5) 57 | 58 | display(x1) 59 | display(x2) 60 | display(x3) 61 | display(x4) 62 | display(x5) 63 | ``` 64 | 65 | ## Matrices矩阵表示 66 | $$ 67 | A=\begin{bmatrix} 68 | 1 & 3 & 2\\ 69 | 5 & 6 & 7\\ 70 | 8 & 3 & 1 71 | \end{bmatrix} 72 | $$ 73 | 74 | 75 | ```python 76 | import numpy as np 77 | 78 | A = np.array([[1, 3, 2], [5, 6, 7], [8, 3, 1]]) 79 | display(A) 80 | ``` 81 | 82 | ## 矩阵切片,提取 83 | $$ 84 | A=\begin{bmatrix} 85 | 1 & 3 & 2\\ 86 | 5 & 6 & 7\\ 87 | 8 & 3 & 1 88 | \end{bmatrix} 89 | $$ 90 | 91 | 92 | ```python 93 | x1 = A[1, 2] 94 | x2 = A[1, :] 95 | x3 = A[:, 2] 96 | x4 = A[1:, 2] 97 | display(x1) 98 | display(x2) 99 | display(x3) 100 | display(x4) 101 | ``` 102 | 103 | ## 向量转置 104 | 105 | $$ 106 | x = \begin{bmatrix} 107 | 2 + 3i \\ 108 | 7\\ 109 | 1 110 | \end{bmatrix} 111 | $$ 112 | 113 | 114 | ```python 115 | x = np.array([2+3j, 7, 1]) 116 | x1 = x.T 117 | x2 = x.conjugate() 118 | x3 = x.conj() 119 | display(x) 120 | display(x1) 121 | display(x2) 122 | display(x3) 123 | ``` 124 | 125 | # Lecture 02 Programming logic: IF and For,判断和循环语句 126 | 127 | ## The IF statement, if语句 128 | 129 | ```python 130 | if (logical statement) 131 | (expressions to execute) 132 | elif (logical statement) 133 | (expressions to execute) 134 | elif (logical statement) 135 | (expressions to execute) 136 | else 137 | (expressions to execute) 138 | ``` 139 | 140 | ## The FOR loop, for循环 141 | 142 | 143 | ```python 144 | import numpy as np 145 | 146 | a = 0 147 | for j in np.arange(0, 5): 148 | a = a + (j+1) 149 | display(a) 150 | ``` 151 | 152 | 153 | ```python 154 | import numpy as np 155 | 156 | a = 0 157 | for j in np.arange(0, 5, 2): 158 | a = a + (j+1) 159 | display(a) 160 | ``` 161 | 162 | 163 | ```python 164 | import numpy as np 165 | 166 | a = 0 167 | loop = [1, 5, 4] 168 | for j in loop: 169 | a = a + j 170 | display(a) 171 | ``` 172 | 173 | ## 算例,二分找根 174 | 175 | ![image](bisection.png) 176 | 177 | ## 算例1 178 | $$ 179 | f(x) = exp(x) - tan(x) 180 | $$ 181 | 182 | 183 | ```python 184 | xr = -2.8; xl = -4 185 | 186 | for j in range(100): 187 | xc = (xr + xl) / 2 188 | fc = np.exp(xc) - np.tan(xc) 189 | if ( fc > 0 ): 190 | xl = xc 191 | else: 192 | xr = xc 193 | 194 | if ( abs(fc) < 1e-5 ): 195 | display(xc) 196 | display(j) 197 | break 198 | 199 | ``` 200 | 201 | 202 | ```python 203 | import numpy as np 204 | import matplotlib.pyplot as plt 205 | 206 | 207 | fig, ax = plt.subplots(figsize=(24, 12)) 208 | x = np.linspace(-4.1, -2, 1000) 209 | y = 0*x 210 | 211 | plt.plot(x, np.exp(x)-np.tan(x), color='k') 212 | plt.plot(x, y, color='r') 213 | 214 | plt.plot(-4, np.exp(-4)-np.tan(-4), marker='o', color='g') 215 | plt.plot(-2.8, np.exp(-2.8)-np.tan(-2.8), marker='o', color='g') 216 | 217 | plt.grid() 218 | plt.ylim(-2, 2) 219 | 220 | 221 | xr = -2.8; xl = -4 222 | 223 | for j in range(100): 224 | xc = (xr + xl) / 2 225 | fc = np.exp(xc) - np.tan(xc) 226 | if ( fc > 0 ): 227 | xl = xc 228 | else: 229 | xr = xc 230 | 231 | if ( abs(fc) < 1e-5 ): 232 | break 233 | plt.plot(xc, fc, marker='o', color='k') 234 | plt.annotate(j, [xc, fc], color='r',fontsize=20) 235 | 236 | 237 | ``` 238 | -------------------------------------------------------------------------------- /lecture/lecture20/code/Lecture01/bisection.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wo315/2206CFD_OpenFOAM/c0be4f9a1eb0e08b621b6b3719dbdf8092d72f1e/lecture/lecture20/code/Lecture01/bisection.png -------------------------------------------------------------------------------- /lecture/lecture20/code/Lecture01/bisection.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 16 | 35 | 37 | 42 | 47 | 52 | 57 | 62 | 67 | 72 | 77 | 82 | 90 | 95 | 96 | 104 | 109 | 110 | 118 | 123 | 124 | 132 | 137 | 138 | 146 | 151 | 152 | 157 | 165 | 170 | 171 | 179 | 184 | 185 | 186 | 190 | 195 | 199 | 206 | 212 | 218 | 223 | 227 | 234 | 240 | 246 | 252 | x_l 263 | 267 | 268 | x_l 279 | x_R 290 | x_l 301 | x_R 312 | x_mid 323 | 329 | 335 | 341 | 342 | 343 | -------------------------------------------------------------------------------- /lecture/lecture20/code/Lecture01/bisection1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wo315/2206CFD_OpenFOAM/c0be4f9a1eb0e08b621b6b3719dbdf8092d72f1e/lecture/lecture20/code/Lecture01/bisection1.png -------------------------------------------------------------------------------- /lecture/lecture20/code/Lecture02/.Lecture 02.ipynb.swx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wo315/2206CFD_OpenFOAM/c0be4f9a1eb0e08b621b6b3719dbdf8092d72f1e/lecture/lecture20/code/Lecture02/.Lecture 02.ipynb.swx -------------------------------------------------------------------------------- /lecture/lecture20/code/Lecture02/.ipynb_checkpoints/Lecture 02-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "b60e91e9", 6 | "metadata": {}, 7 | "source": [ 8 | "# Lecture 02 Linear systems" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "88d7e8f7", 14 | "metadata": {}, 15 | "source": [ 16 | "\n", 17 | "## Section 2.1 Matrix and Vector Properties\n", 18 | "\n", 19 | "$$\n", 20 | "A = \\begin{bmatrix}\n", 21 | "a_{11} & a_{12} &\\dots &a_{1n} \\\\\n", 22 | "a_{21} & a_{22} &\\dots &a_{1n} \\\\\n", 23 | "\\vdots \\\\\n", 24 | "a_{m1} & a_{m2} &\\dots &a_{mn} \n", 25 | "\\end{bmatrix} = (a_{ij})\n", 26 | "$$" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "id": "176f0ed9", 32 | "metadata": {}, 33 | "source": [ 34 | "## Concepts概念\n", 35 | "\n", 36 | "Transpose(转置):$\\mathbf{A}^T = (a_{ij})^T = (a_{ji})\\rightarrow \\ if \\ \\mathbf{A} = \\begin{bmatrix}1 &5\\\\ 2 &3\\end{bmatrix} then \\ A^T = \\begin{bmatrix}1 &5\\\\2 &3\\\\\\end{bmatrix}$\n", 37 | "\n", 38 | "\n", 39 | "\n", 40 | "Complex Conjugate(复转置): $\\overline{A} = \\overline{a_{ij}} \\rightarrow if \\ A = \\begin{bmatrix} i &5\\\\ 3+i &6\\end{bmatrix} then \\ \\overline{A} = \\begin{bmatrix}-i & 5\\\\ 3-i &6\\end{bmatrix}$\n", 41 | "\n", 42 | "\n", 43 | "Adjoint: $\\overline{A}^T = A^* \\rightarrow if \\ \\mathbf{A}= \\begin{bmatrix} i &5\\\\ 3+i &6\\end{bmatrix} then \\ \\overline{A} = \\begin{bmatrix}-i & 3-i\\\\ 5 &6\\end{bmatrix}$\n", 44 | "\n", 45 | "\n", 46 | "伴随变换更合适$A^H$,共轭转置矩阵" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": null, 52 | "id": "30c73870", 53 | "metadata": {}, 54 | "outputs": [], 55 | "source": [ 56 | "import numpy as np\n", 57 | "\n", 58 | "A = np.matrix([[1, 5], [2, 3]])\n", 59 | "A.T\n", 60 | "print(A)\n", 61 | "print(\"........................\")\n", 62 | "\n", 63 | "B = np.matrix([[1j, 5], [3+1j, 6]])\n", 64 | "print(B.conjugate())\n", 65 | "print(\"........................\")\n", 66 | "\n", 67 | "C = np.matrix([[1j, 5], [3+1j, 6]])\n", 68 | "print(C.getH()) # Hermitian transpose" 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "id": "cbc096dc", 74 | "metadata": {}, 75 | "source": [ 76 | "## Matrix equalities and addition\n", 77 | "\n", 78 | "$\\mathbf{A} = \\mathbf{B}$,如果$a_{ij} = b_{ij}$\n", 79 | "\n", 80 | "$\\mathbf{A} = 0$,表示$a_{ij} = 0$\n", 81 | "\n", 82 | "$\\mathbf{A}\\pm\\mathbf{B} = (a_{ij})\\pm b_{ij}) = (a_{ij}\\pm b_{ij})$\n", 83 | "\n", 84 | "$\\textbf{commutative}: \\mathbf{A} + \\mathbf{B} = \\mathbf{B} + \\mathbf{A}$\n", 85 | "\n", 86 | "$\\textbf{Associative}: \\mathbf{A} + (\\mathbf{B} + \\mathbf{C})= (\\mathbf{A} + \\mathbf{B}) + \\mathbf{C}$" 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "id": "45725a33", 92 | "metadata": {}, 93 | "source": [ 94 | "## Matrix multiplication\n", 95 | "\n", 96 | "Multiply by a number: $\\alpha\\mathbf{A} = \\alpha(a_{ij})=(\\alpha a_{ij})$\n", 97 | "\n", 98 | "Matrix multiply:$\\mathbf{AB} = C, 其中:\\ c_{ij}=\\sum_{k=1}^na_{ik}b_{kj}$\n", 99 | "\n", 100 | "$$\n", 101 | "\\begin{pmatrix}\n", 102 | "3 &2 &1 \\\\\n", 103 | "6 &5 &0 \\\\\n", 104 | "1 &8 &3\n", 105 | "\\end{pmatrix}\n", 106 | "\\begin{pmatrix}\n", 107 | "1\\\\\n", 108 | "0\\\\\n", 109 | "2\n", 110 | "\\end{pmatrix}\n", 111 | "= \\begin{pmatrix}\n", 112 | "3\\cdot1 + 2\\cdot0 + 1\\cdot2 \\\\\n", 113 | "6\\cdot1 + 5\\cdot0 + 0\\cdot2 \\\\\n", 114 | "1\\cdot1 + 8\\cdot0 + 3\\cdot2\n", 115 | "\\end{pmatrix}\n", 116 | "=\n", 117 | "\\begin{pmatrix}\n", 118 | "5\\\\\n", 119 | "6\\\\\n", 120 | "7\n", 121 | "\\end{pmatrix}\n", 122 | "$$\n", 123 | "\n", 124 | "$\\textbf{distributive}: \\mathbf{A}(\\mathbf{B}+\\mathbf{C})= \\mathbf{A}\\mathbf{B}+\\mathbf{A}\\mathbf{C})$\n", 125 | "\n", 126 | "$\\textbf{Associative}: \\mathbf{A}(\\mathbf{B}\\mathbf{C})= (\\mathbf{A}\\mathbf{B})\\mathbf{C}$\n", 127 | "\n", 128 | "$\\textbf{not commutative}: \\mathbf{A}\\mathbf{B} \\neq \\mathbf{B}\\mathbf{A}$" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": 49, 134 | "id": "bd3ff409", 135 | "metadata": {}, 136 | "outputs": [ 137 | { 138 | "name": "stdout", 139 | "output_type": "stream", 140 | "text": [ 141 | "[[ 3 2 1]\n", 142 | " [ 0 0 0]\n", 143 | " [ 2 16 6]]\n", 144 | "[[5]\n", 145 | " [6]\n", 146 | " [7]]\n" 147 | ] 148 | } 149 | ], 150 | "source": [ 151 | "import numpy as np\n", 152 | "A = np.array([[3, 2, 1],\n", 153 | " [6, 5, 0],\n", 154 | " [1, 8, 3]])\n", 155 | "B = np.array([[1],\n", 156 | " [0],\n", 157 | " [2]])\n", 158 | "\n", 159 | "C = A*B # element-wise\n", 160 | "print(C)\n", 161 | "\n", 162 | "D = A@B\n", 163 | "print(D)" 164 | ] 165 | }, 166 | { 167 | "cell_type": "markdown", 168 | "id": "2d36e831", 169 | "metadata": {}, 170 | "source": [ 171 | "## Vector multiplication\n", 172 | "\n", 173 | "Vectors: \n", 174 | "\n", 175 | "$\\mathbf{u}^T\\mathbf{v} = \\sum_{i=1}^n u_iv_i$\n", 176 | "\n", 177 | "$$\n", 178 | "\\large{\n", 179 | "\\mathbf{u}^T\\mathbf{v}=(u_1, u_2, \\cdots, u_n)\n", 180 | "\\begin{pmatrix}\n", 181 | "v_1\\\\\n", 182 | "v_2\\\\\n", 183 | "\\vdots\\\\\n", 184 | "v_n\n", 185 | "\\end{pmatrix}\n", 186 | "= (u_1v_1 + u_2v_2 + \\cdots + u_nv_n)}\n", 187 | "$$\n", 188 | "\n", 189 | "## inner product 内积\n", 190 | "\n", 191 | "Inner product:\n", 192 | "\n", 193 | "$\\mathbf{(u, v)} = \\sum_{i=1}^{n}=u_i\\overline{v_i}=\\mathbf{u}^T\\overline{v}$\n", 194 | "\n", 195 | "* $\\mathbf{(u,v)}=\\overline{(v, u)}$\n", 196 | "\n", 197 | "\n", 198 | "* $(\\alpha \\mathbf{u}, \\mathbf{v}) = \\alpha(\\mathbf{u},\\mathbf{v})$\n", 199 | "\n", 200 | "\n", 201 | "* $(\\mathbf{u}, \\alpha\\mathbf{v})=\\overline{\\alpha}(\\mathbf{u},\\mathbf{v})$\n", 202 | "\n", 203 | "\n", 204 | "* $\\mathbf{u}, \\mathbf{v+w} = (\\mathbf{u},\\mathbf{v}) + (\\mathbf{u, w})$\n", 205 | "\n", 206 | "Vector Magnitudes:\n", 207 | "\n", 208 | "$\\mathbf{(u, u)}^{1/2} = \\sum_{i=1}^{n}u_i\\overline{u_i}=\\sum_{i=1}^{n}|u_i|$\n", 209 | "\n", 210 | "Orthogonality:\n", 211 | "\n", 212 | "$(\\mathbf{u,v})=0$" 213 | ] 214 | }, 215 | { 216 | "cell_type": "markdown", 217 | "id": "fb5de3bc", 218 | "metadata": {}, 219 | "source": [ 220 | "## Linear dependence 线性相关无关\n", 221 | "\n", 222 | "$$c_1\\mathbf{X_1} + c_2\\mathbf{X_2} + \\cdots + c_n\\mathbf{X_n}= 0$$\n", 223 | "\n", 224 | "如果$c_i$不全为0是线性相关,否则线性无关\n", 225 | "\n", 226 | "\n", 227 | "## Inverse 逆矩阵\n", 228 | "\n", 229 | "Identity Matrix: $\\mathbf{I} = \\delta_{ij},对于i=j,\\delta_{ij} = 1,否则,\\delta_{ij}=0$\n", 230 | "\n", 231 | "Inverse Matrix: $\\mathbf{AB = I},如果det(A)\\neq=0,则\\mathbf{B=A^{-1}}$\n", 232 | "\n", 233 | "## Solving $\\mathbf{A}x=b$\n", 234 | "\n", 235 | "$$\n", 236 | "if \\ det(A)\\neq 0 \\ x = \\mathbf{A}^{-1}b\n", 237 | "$$" 238 | ] 239 | }, 240 | { 241 | "cell_type": "code", 242 | "execution_count": null, 243 | "id": "639bbc46", 244 | "metadata": {}, 245 | "outputs": [], 246 | "source": [] 247 | } 248 | ], 249 | "metadata": { 250 | "kernelspec": { 251 | "display_name": "Python 3 (ipykernel)", 252 | "language": "python", 253 | "name": "python3" 254 | }, 255 | "language_info": { 256 | "codemirror_mode": { 257 | "name": "ipython", 258 | "version": 3 259 | }, 260 | "file_extension": ".py", 261 | "mimetype": "text/x-python", 262 | "name": "python", 263 | "nbconvert_exporter": "python", 264 | "pygments_lexer": "ipython3", 265 | "version": "3.8.10" 266 | } 267 | }, 268 | "nbformat": 4, 269 | "nbformat_minor": 5 270 | } 271 | -------------------------------------------------------------------------------- /lecture/lecture20/code/Lecture02/.ipynb_checkpoints/Untitled-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "b60e91e9", 6 | "metadata": {}, 7 | "source": [ 8 | "# Lecture 02 Linear systems" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "88d7e8f7", 14 | "metadata": {}, 15 | "source": [ 16 | "\n", 17 | "## Section 2.1 Matrix and Vector Properties\n", 18 | "\n", 19 | "$$\n", 20 | "A = \\begin{bmatrix}\n", 21 | "a_{11} & a_{12} &\\dots &a_{1n} \\\\\n", 22 | "a_{21} & a_{22} &\\dots &a_{1n} \\\\\n", 23 | "\\vdots \\\\\n", 24 | "a_{m1} & a_{m2} &\\dots &a_{mn} \n", 25 | "\\end{bmatrix} = (a_{ij})\n", 26 | "$$" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "id": "176f0ed9", 32 | "metadata": {}, 33 | "source": [ 34 | "## Concepts概念\n", 35 | "\n", 36 | "Transpose(转置):$\\mathbf{A}^T = (a_{ij})^T = (a_{ji})\\rightarrow \\ if \\ \\mathbf{A} = \\begin{bmatrix}1 &5\\\\ 2 &3\\end{bmatrix} then \\ A^T = \\begin{bmatrix}1 &5\\\\2 &3\\\\\\end{bmatrix}$\n", 37 | "\n", 38 | "\n", 39 | "\n", 40 | "Complex Conjugate(复转置): $\\overline{A} = \\overline{a_{ij}} \\rightarrow if \\ A = \\begin{bmatrix} i &5\\\\ 3+i &6\\end{bmatrix} then \\ \\overline{A} = \\begin{bmatrix}-i & 5\\\\ 3-i &6\\end{bmatrix}$\n", 41 | "\n", 42 | "\n", 43 | "Adjoint: $\\overline{A}^T = A^* \\rightarrow if \\ \\mathbf{A}= \\begin{bmatrix} i &5\\\\ 3+i &6\\end{bmatrix} then \\ \\overline{A} = \\begin{bmatrix}-i & 3-i\\\\ 5 &6\\end{bmatrix}$\n", 44 | "\n", 45 | "\n", 46 | "伴随变换更合适$A^H$,共轭转置矩阵" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": null, 52 | "id": "30c73870", 53 | "metadata": {}, 54 | "outputs": [], 55 | "source": [ 56 | "import numpy as np\n", 57 | "\n", 58 | "A = np.matrix([[1, 5], [2, 3]])\n", 59 | "A.T\n", 60 | "print(A)\n", 61 | "print(\"........................\")\n", 62 | "\n", 63 | "B = np.matrix([[1j, 5], [3+1j, 6]])\n", 64 | "print(B.conjugate())\n", 65 | "print(\"........................\")\n", 66 | "\n", 67 | "C = np.matrix([[1j, 5], [3+1j, 6]])\n", 68 | "print(C.getH()) # Hermitian transpose" 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "id": "cbc096dc", 74 | "metadata": {}, 75 | "source": [ 76 | "## Matrix equalities and addition\n", 77 | "\n", 78 | "$\\mathbf{A} = \\mathbf{B}$,如果$a_{ij} = b_{ij}$\n", 79 | "\n", 80 | "$\\mathbf{A} = 0$,表示$a_{ij} = 0$\n", 81 | "\n", 82 | "$\\mathbf{A}\\pm\\mathbf{B} = (a_{ij})\\pm b_{ij}) = (a_{ij}\\pm b_{ij})$\n", 83 | "\n", 84 | "$\\textbf{commutative}: \\mathbf{A} + \\mathbf{B} = \\mathbf{B} + \\mathbf{A}$\n", 85 | "\n", 86 | "$\\textbf{Associative}: \\mathbf{A} + (\\mathbf{B} + \\mathbf{C})= (\\mathbf{A} + \\mathbf{B}) + \\mathbf{C}$" 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "id": "45725a33", 92 | "metadata": {}, 93 | "source": [ 94 | "## Matrix multiplication\n", 95 | "\n", 96 | "Multiply by a number: $\\alpha\\mathbf{A} = \\alpha(a_{ij})=(\\alpha a_{ij})$\n", 97 | "\n", 98 | "Matrix multiply:$\\mathbf{AB} = C, 其中:\\ c_{ij}=\\sum_{k=1}^na_{ik}b_{kj}$\n", 99 | "\n", 100 | "$$\n", 101 | "\\begin{pmatrix}\n", 102 | "3 &2 &1 \\\\\n", 103 | "6 &5 &0 \\\\\n", 104 | "1 &8 &3\n", 105 | "\\end{pmatrix}\n", 106 | "\\begin{pmatrix}\n", 107 | "1\\\\\n", 108 | "0\\\\\n", 109 | "2\n", 110 | "\\end{pmatrix}\n", 111 | "= \\begin{pmatrix}\n", 112 | "3\\cdot1 + 2\\cdot0 + 1\\cdot2 \\\\\n", 113 | "6\\cdot1 + 5\\cdot0 + 0\\cdot2 \\\\\n", 114 | "1\\cdot1 + 8\\cdot0 + 3\\cdot2\n", 115 | "\\end{pmatrix}\n", 116 | "=\n", 117 | "\\begin{pmatrix}\n", 118 | "5\\\\\n", 119 | "6\\\\\n", 120 | "7\n", 121 | "\\end{pmatrix}\n", 122 | "$$\n", 123 | "\n", 124 | "$\\textbf{distributive}: \\mathbf{A}(\\mathbf{B}+\\mathbf{C})= \\mathbf{A}\\mathbf{B}+\\mathbf{A}\\mathbf{C})$\n", 125 | "\n", 126 | "$\\textbf{Associative}: \\mathbf{A}(\\mathbf{B}\\mathbf{C})= (\\mathbf{A}\\mathbf{B})\\mathbf{C}$\n", 127 | "\n", 128 | "$\\textbf{not commutative}: \\mathbf{A}\\mathbf{B} \\neq \\mathbf{B}\\mathbf{A}$" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": 49, 134 | "id": "bd3ff409", 135 | "metadata": {}, 136 | "outputs": [ 137 | { 138 | "name": "stdout", 139 | "output_type": "stream", 140 | "text": [ 141 | "[[ 3 2 1]\n", 142 | " [ 0 0 0]\n", 143 | " [ 2 16 6]]\n", 144 | "[[5]\n", 145 | " [6]\n", 146 | " [7]]\n" 147 | ] 148 | } 149 | ], 150 | "source": [ 151 | "import numpy as np\n", 152 | "A = np.array([[3, 2, 1],\n", 153 | " [6, 5, 0],\n", 154 | " [1, 8, 3]])\n", 155 | "B = np.array([[1],\n", 156 | " [0],\n", 157 | " [2]])\n", 158 | "\n", 159 | "C = A*B # element-wise\n", 160 | "print(C)\n", 161 | "\n", 162 | "D = A@B\n", 163 | "print(D)" 164 | ] 165 | }, 166 | { 167 | "cell_type": "markdown", 168 | "id": "2d36e831", 169 | "metadata": {}, 170 | "source": [ 171 | "## Vector multiplication\n", 172 | "\n", 173 | "Vectors: \n", 174 | "\n", 175 | "$\\mathbf{u}^T\\mathbf{v} = \\sum_{i=1}^n u_iv_i$\n", 176 | "\n", 177 | "$$\n", 178 | "\\large{\n", 179 | "\\mathbf{u}^T\\mathbf{v}=(u_1, u_2, \\cdots, u_n)\n", 180 | "\\begin{pmatrix}\n", 181 | "v_1\\\\\n", 182 | "v_2\\\\\n", 183 | "\\vdots\\\\\n", 184 | "v_n\n", 185 | "\\end{pmatrix}\n", 186 | "= (u_1v_1 + u_2v_2 + \\cdots + u_nv_n)}\n", 187 | "$$\n", 188 | "\n", 189 | "## inner product 内积\n", 190 | "\n", 191 | "Inner product:\n", 192 | "\n", 193 | "$\\mathbf{(u, v)} = \\sum_{i=1}^{n}=u_i\\overline{v_i}=\\mathbf{u}^T\\overline{v}$\n", 194 | "\n", 195 | "* $\\mathbf{(u,v)}=\\overline{(v, u)}$\n", 196 | "\n", 197 | "\n", 198 | "* $(\\alpha \\mathbf{u}, \\mathbf{v}) = \\alpha(\\mathbf{u},\\mathbf{v})$\n", 199 | "\n", 200 | "\n", 201 | "* $(\\mathbf{u}, \\alpha\\mathbf{v})=\\overline{\\alpha}(\\mathbf{u},\\mathbf{v})$\n", 202 | "\n", 203 | "\n", 204 | "* $\\mathbf{u}, \\mathbf{v+w} = (\\mathbf{u},\\mathbf{v}) + (\\mathbf{u, w})$\n", 205 | "\n", 206 | "Vector Magnitudes:\n", 207 | "\n", 208 | "$\\mathbf{(u, u)}^{1/2} = \\sum_{i=1}^{n}u_i\\overline{u_i}=\\sum_{i=1}^{n}|u_i|$\n", 209 | "\n", 210 | "Orthogonality:\n", 211 | "\n", 212 | "$(\\mathbf{u,v})=0$" 213 | ] 214 | }, 215 | { 216 | "cell_type": "markdown", 217 | "id": "fb5de3bc", 218 | "metadata": {}, 219 | "source": [ 220 | "## Linear dependence 线性相关无关\n", 221 | "\n", 222 | "$$c_1\\mathbf{X_1} + c_2\\mathbf{X_2} + \\cdots + c_n\\mathbf{X_n}= 0$$\n", 223 | "\n", 224 | "如果$c_i$不全为0是线性相关,否则线性无关\n", 225 | "\n", 226 | "\n", 227 | "## Inverse 逆矩阵\n", 228 | "\n", 229 | "Identity Matrix: $\\mathbf{I} = \\delta_{ij},对于i=j,\\delta_{ij} = 1,否则,\\delta_{ij}=0$\n", 230 | "\n", 231 | "Inverse Matrix: $\\mathbf{AB = I},如果det(A)\\neq=0,则\\mathbf{B=A^{-1}}$\n", 232 | "\n", 233 | "## Solving $\\mathbf{A}x=b$\n", 234 | "\n", 235 | "$$\n", 236 | "if \\ det(A)\\neq 0 \\ x = \\mathbf{A}^{-1}b\n", 237 | "$$" 238 | ] 239 | }, 240 | { 241 | "cell_type": "code", 242 | "execution_count": null, 243 | "id": "639bbc46", 244 | "metadata": {}, 245 | "outputs": [], 246 | "source": [] 247 | } 248 | ], 249 | "metadata": { 250 | "kernelspec": { 251 | "display_name": "Python 3 (ipykernel)", 252 | "language": "python", 253 | "name": "python3" 254 | }, 255 | "language_info": { 256 | "codemirror_mode": { 257 | "name": "ipython", 258 | "version": 3 259 | }, 260 | "file_extension": ".py", 261 | "mimetype": "text/x-python", 262 | "name": "python", 263 | "nbconvert_exporter": "python", 264 | "pygments_lexer": "ipython3", 265 | "version": "3.8.10" 266 | } 267 | }, 268 | "nbformat": 4, 269 | "nbformat_minor": 5 270 | } 271 | -------------------------------------------------------------------------------- /lecture/lecture20/code/Lecture02/Lecture 02.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "b60e91e9", 6 | "metadata": {}, 7 | "source": [ 8 | "# Lecture 02 Linear systems\n", 9 | "\n", 10 | "$$\n", 11 | "\\huge{\\mathbf{Ax=b}}\n", 12 | "$$" 13 | ] 14 | }, 15 | { 16 | "cell_type": "markdown", 17 | "id": "88d7e8f7", 18 | "metadata": {}, 19 | "source": [ 20 | "\n", 21 | "## Section 2.1 Matrix and Vector Properties\n", 22 | "\n", 23 | "$$\n", 24 | "A = \\begin{bmatrix}\n", 25 | "a_{11} & a_{12} &\\dots &a_{1n} \\\\\n", 26 | "a_{21} & a_{22} &\\dots &a_{1n} \\\\\n", 27 | "\\vdots \\\\\n", 28 | "a_{m1} & a_{m2} &\\dots &a_{mn} \n", 29 | "\\end{bmatrix} = (a_{ij})\n", 30 | "$$" 31 | ] 32 | }, 33 | { 34 | "cell_type": "markdown", 35 | "id": "176f0ed9", 36 | "metadata": {}, 37 | "source": [ 38 | "## Concepts概念\n", 39 | "\n", 40 | "Transpose(转置):$\\mathbf{A}^T = (a_{ij})^T = (a_{ji})\\rightarrow \\ if \\ \\mathbf{A} = \\begin{bmatrix}1 &5\\\\ 2 &3\\end{bmatrix} then \\ A^T = \\begin{bmatrix}1 &2\\\\5 &3\\\\\\end{bmatrix}$\n", 41 | "\n", 42 | "\n", 43 | "\n", 44 | "Complex Conjugate(复转置): $\\overline{A} = \\overline{a_{ij}} \\rightarrow if \\ A = \\begin{bmatrix} i &5\\\\ 3+i &6\\end{bmatrix} then \\ \\overline{A} = \\begin{bmatrix}-i & 5\\\\ 3-i &6\\end{bmatrix}$\n", 45 | "\n", 46 | "\n", 47 | "Adjoint: $\\overline{A}^T = A^* \\rightarrow if \\ \\mathbf{A}= \\begin{bmatrix} i &5\\\\ 3+i &6\\end{bmatrix} then \\ \\overline{A} = \\begin{bmatrix}-i & 3-i\\\\ 5 &6\\end{bmatrix}$\n", 48 | "\n", 49 | "\n", 50 | "伴随变换更合适$A^H$,共轭转置矩阵" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 84, 56 | "id": "30c73870", 57 | "metadata": {}, 58 | "outputs": [ 59 | { 60 | "name": "stdout", 61 | "output_type": "stream", 62 | "text": [ 63 | "[[1 2]\n", 64 | " [5 3]]\n", 65 | "........................\n", 66 | "[[0.-1.j 5.-0.j]\n", 67 | " [3.-1.j 6.-0.j]]\n", 68 | "........................\n", 69 | "[[0.-1.j 3.-1.j]\n", 70 | " [5.-0.j 6.-0.j]]\n" 71 | ] 72 | } 73 | ], 74 | "source": [ 75 | "import numpy as np\n", 76 | "\n", 77 | "A = np.matrix([[1, 5], [2, 3]])\n", 78 | "B = A.T\n", 79 | "print(B)\n", 80 | "print(\"........................\")\n", 81 | "\n", 82 | "B = np.matrix([[1j, 5], [3+1j, 6]])\n", 83 | "print(B.conjugate())\n", 84 | "print(\"........................\")\n", 85 | "\n", 86 | "C = np.matrix([[1j, 5], [3+1j, 6]])\n", 87 | "print(C.getH()) # Hermitian transpose" 88 | ] 89 | }, 90 | { 91 | "cell_type": "markdown", 92 | "id": "cbc096dc", 93 | "metadata": {}, 94 | "source": [ 95 | "## Matrix equalities and addition\n", 96 | "\n", 97 | "$\\mathbf{A} = \\mathbf{B}$,如果$a_{ij} = b_{ij}$\n", 98 | "\n", 99 | "$\\mathbf{A} = 0$,表示$a_{ij} = 0$\n", 100 | "\n", 101 | "$\\mathbf{A}\\pm\\mathbf{B} = (a_{ij})\\pm b_{ij}) = (a_{ij}\\pm b_{ij})$\n", 102 | "\n", 103 | "$\\textbf{commutative}: \\mathbf{A} + \\mathbf{B} = \\mathbf{B} + \\mathbf{A}$\n", 104 | "\n", 105 | "$\\textbf{Associative}: \\mathbf{A} + (\\mathbf{B} + \\mathbf{C})= (\\mathbf{A} + \\mathbf{B}) + \\mathbf{C}$" 106 | ] 107 | }, 108 | { 109 | "cell_type": "markdown", 110 | "id": "45725a33", 111 | "metadata": {}, 112 | "source": [ 113 | "## Matrix multiplication\n", 114 | "\n", 115 | "Multiply by a number: $\\alpha\\mathbf{A} = \\alpha(a_{ij})=(\\alpha a_{ij})$\n", 116 | "\n", 117 | "Matrix multiply:$\\mathbf{AB} = C, 其中:\\ c_{ij}=\\sum_{k=1}^na_{ik}b_{kj}$\n", 118 | "\n", 119 | "$$\n", 120 | "\\begin{pmatrix}\n", 121 | "3 &2 &1 \\\\\n", 122 | "6 &5 &0 \\\\\n", 123 | "1 &8 &3\n", 124 | "\\end{pmatrix}\n", 125 | "\\begin{pmatrix}\n", 126 | "1\\\\\n", 127 | "0\\\\\n", 128 | "2\n", 129 | "\\end{pmatrix}\n", 130 | "= \\begin{pmatrix}\n", 131 | "3\\cdot1 + 2\\cdot0 + 1\\cdot2 \\\\\n", 132 | "6\\cdot1 + 5\\cdot0 + 0\\cdot2 \\\\\n", 133 | "1\\cdot1 + 8\\cdot0 + 3\\cdot2\n", 134 | "\\end{pmatrix}\n", 135 | "=\n", 136 | "\\begin{pmatrix}\n", 137 | "5\\\\\n", 138 | "6\\\\\n", 139 | "7\n", 140 | "\\end{pmatrix}\n", 141 | "$$\n", 142 | "\n", 143 | "$\\textbf{distributive}: \\mathbf{A}(\\mathbf{B}+\\mathbf{C})= \\mathbf{A}\\mathbf{B}+\\mathbf{A}\\mathbf{C}$\n", 144 | "\n", 145 | "$\\textbf{Associative}: \\mathbf{A}(\\mathbf{B}\\mathbf{C})= (\\mathbf{A}\\mathbf{B})\\mathbf{C}$\n", 146 | "\n", 147 | "$\\textbf{not commutative}: \\mathbf{A}\\mathbf{B} \\neq \\mathbf{B}\\mathbf{A}$" 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": 85, 153 | "id": "61b4eff4", 154 | "metadata": {}, 155 | "outputs": [ 156 | { 157 | "name": "stdout", 158 | "output_type": "stream", 159 | "text": [ 160 | "[[ 3 2 1]\n", 161 | " [ 0 0 0]\n", 162 | " [ 2 16 6]]\n", 163 | "[[5]\n", 164 | " [6]\n", 165 | " [7]]\n" 166 | ] 167 | } 168 | ], 169 | "source": [ 170 | "import numpy as np\n", 171 | "A = np.array([[3, 2, 1],\n", 172 | " [6, 5, 0],\n", 173 | " [1, 8, 3]])\n", 174 | "B = np.array([[1],\n", 175 | " [0],\n", 176 | " [2]])\n", 177 | "\n", 178 | "C = A*B # element-wise\n", 179 | "print(C)\n", 180 | "\n", 181 | "D = A@B\n", 182 | "print(D)" 183 | ] 184 | }, 185 | { 186 | "cell_type": "markdown", 187 | "id": "333d913c", 188 | "metadata": {}, 189 | "source": [ 190 | "## Vector multiplication\n", 191 | "\n", 192 | "Vectors: \n", 193 | "\n", 194 | "$\\mathbf{u}^T\\mathbf{v} = \\sum_{i=1}^n u_iv_i$\n", 195 | "\n", 196 | "$$\n", 197 | "\\large{\n", 198 | "\\mathbf{u}^T\\mathbf{v}=(u_1, u_2, \\cdots, u_n)\n", 199 | "\\begin{pmatrix}\n", 200 | "v_1\\\\\n", 201 | "v_2\\\\\n", 202 | "\\vdots\\\\\n", 203 | "v_n\n", 204 | "\\end{pmatrix}\n", 205 | "= (u_1v_1 + u_2v_2 + \\cdots + u_nv_n)}\n", 206 | "$$\n", 207 | "\n", 208 | "## inner product 内积\n", 209 | "\n", 210 | "Inner product:\n", 211 | "\n", 212 | "$\\mathbf{(u, v)} = \\sum_{i=1}^{n}=u_i\\overline{v_i}=\\mathbf{u}^T\\overline{v}$\n", 213 | "\n", 214 | "* $\\mathbf{(u,v)}=\\overline{(v, u)}$\n", 215 | "\n", 216 | "\n", 217 | "* $(\\alpha \\mathbf{u}, \\mathbf{v}) = \\alpha(\\mathbf{u},\\mathbf{v})$\n", 218 | "\n", 219 | "\n", 220 | "* $(\\mathbf{u}, \\alpha\\mathbf{v})=\\overline{\\alpha}(\\mathbf{u},\\mathbf{v})$\n", 221 | "\n", 222 | "\n", 223 | "* $(\\mathbf{u}, \\mathbf{v+w}) = (\\mathbf{u},\\mathbf{v}) + (\\mathbf{u, w})$\n", 224 | "\n", 225 | "Vector Magnitudes:\n", 226 | "\n", 227 | "$\\mathbf{(u, u)}^{1/2} = \\sum_{i=1}^{n}u_i\\overline{u_i}=\\sum_{i=1}^{n}|u_i|$\n", 228 | "\n", 229 | "Orthogonality:\n", 230 | "\n", 231 | "$(\\mathbf{u,v})=0$" 232 | ] 233 | }, 234 | { 235 | "cell_type": "code", 236 | "execution_count": 86, 237 | "id": "ef72c732", 238 | "metadata": {}, 239 | "outputs": [ 240 | { 241 | "name": "stdout", 242 | "output_type": "stream", 243 | "text": [ 244 | "2\n", 245 | "2\n", 246 | "2\n" 247 | ] 248 | } 249 | ], 250 | "source": [ 251 | "a = np.array([1, 2, 3])\n", 252 | "b = np.array([0, 1, 0])\n", 253 | "print(np.inner(a, b))\n", 254 | "print(np.dot(a, b))\n", 255 | "print(np.vdot(a, b))\n" 256 | ] 257 | }, 258 | { 259 | "cell_type": "code", 260 | "execution_count": 87, 261 | "id": "a0c4d930", 262 | "metadata": {}, 263 | "outputs": [ 264 | { 265 | "name": "stdout", 266 | "output_type": "stream", 267 | "text": [ 268 | "(3+1j)\n", 269 | "(3-1j)\n", 270 | "(3-1j)\n" 271 | ] 272 | } 273 | ], 274 | "source": [ 275 | "a = np.array([1, 2+1j, 3])\n", 276 | "b = np.array([0, 1+1j, 0])\n", 277 | "print(np.vdot(a, b))\n", 278 | "print(np.vdot(b, a)) # correct\n", 279 | "print(np.dot(a, b.conjugate()))" 280 | ] 281 | }, 282 | { 283 | "cell_type": "markdown", 284 | "id": "1ff8aa9e", 285 | "metadata": {}, 286 | "source": [ 287 | "## Linear dependence 线性相关无关\n", 288 | "\n", 289 | "$$c_1\\mathbf{X_1} + c_2\\mathbf{X_2} + \\cdots + c_n\\mathbf{X_n}= 0$$\n", 290 | "\n", 291 | "如果$c_i$不全为0是线性相关,否则线性无关\n", 292 | "\n", 293 | "\n", 294 | "## Inverse 逆矩阵\n", 295 | "\n", 296 | "Identity Matrix: $\\mathbf{I} = \\delta_{ij},对于i=j,\\delta_{ij} = 1,否则,\\delta_{ij}=0$\n", 297 | "\n", 298 | "Inverse Matrix: $\\mathbf{AB = I},如果det(A)\\neq0,则\\mathbf{B=A^{-1}}$\n", 299 | "\n", 300 | "## Solving $\\mathbf{A}x=b$\n", 301 | "\n", 302 | "$$\n", 303 | "if \\ det(A)\\neq 0 \\ x = \\mathbf{A}^{-1}b\n", 304 | "$$" 305 | ] 306 | }, 307 | { 308 | "cell_type": "code", 309 | "execution_count": null, 310 | "id": "44e27953", 311 | "metadata": {}, 312 | "outputs": [], 313 | "source": [] 314 | } 315 | ], 316 | "metadata": { 317 | "kernelspec": { 318 | "display_name": "Python 3 (ipykernel)", 319 | "language": "python", 320 | "name": "python3" 321 | }, 322 | "language_info": { 323 | "codemirror_mode": { 324 | "name": "ipython", 325 | "version": 3 326 | }, 327 | "file_extension": ".py", 328 | "mimetype": "text/x-python", 329 | "name": "python", 330 | "nbconvert_exporter": "python", 331 | "pygments_lexer": "ipython3", 332 | "version": "3.8.10" 333 | } 334 | }, 335 | "nbformat": 4, 336 | "nbformat_minor": 5 337 | } 338 | -------------------------------------------------------------------------------- /lecture/lecture20/code/Lecture03/.ipynb_checkpoints/.Gauss_elimination-checkpoint.ipynb.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wo315/2206CFD_OpenFOAM/c0be4f9a1eb0e08b621b6b3719dbdf8092d72f1e/lecture/lecture20/code/Lecture03/.ipynb_checkpoints/.Gauss_elimination-checkpoint.ipynb.swp -------------------------------------------------------------------------------- /lecture/lecture20/code/Lecture03/.ipynb_checkpoints/.Gauss_elimination-checkpoint.ipynb.swx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wo315/2206CFD_OpenFOAM/c0be4f9a1eb0e08b621b6b3719dbdf8092d72f1e/lecture/lecture20/code/Lecture03/.ipynb_checkpoints/.Gauss_elimination-checkpoint.ipynb.swx -------------------------------------------------------------------------------- /lecture/lecture20/code/Lecture03/.ipynb_checkpoints/.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wo315/2206CFD_OpenFOAM/c0be4f9a1eb0e08b621b6b3719dbdf8092d72f1e/lecture/lecture20/code/Lecture03/.ipynb_checkpoints/.swp -------------------------------------------------------------------------------- /lecture/lecture20/code/Lecture03/.ipynb_checkpoints/.swpx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wo315/2206CFD_OpenFOAM/c0be4f9a1eb0e08b621b6b3719dbdf8092d72f1e/lecture/lecture20/code/Lecture03/.ipynb_checkpoints/.swpx -------------------------------------------------------------------------------- /lecture/lecture20/code/Lecture03/.ipynb_checkpoints/Gauss_elimination-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "61981b1a", 6 | "metadata": {}, 7 | "source": [ 8 | "# Gauss Elimination 高斯消元\n", 9 | "\n", 10 | "## 直接解法 $\\mathbf{Ax = b}$" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "id": "e63cf42f", 16 | "metadata": {}, 17 | "source": [ 18 | "$$\n", 19 | "x_1 + x_2 + x_3 = 1 \\\\\n", 20 | "x_1 + 2x_2 + 4x_3 = -1 \\\\\n", 21 | "x_1 + 3x_2 + 9x_3 = 1\n", 22 | "$$\n" 23 | ] 24 | }, 25 | { 26 | "cell_type": "markdown", 27 | "id": "bd0de494", 28 | "metadata": {}, 29 | "source": [ 30 | "$$\n", 31 | "\\mathbf{A} = \\begin{bmatrix}\n", 32 | "1 &1 &1\\\\\n", 33 | "1 &2 &4\\\\\n", 34 | "1 &3 &9\\\\\n", 35 | "\\end{bmatrix} \\quad\n", 36 | "\\mathbf{x} = \\begin{bmatrix}\n", 37 | "x_1 \\\\\n", 38 | "x_2 \\\\\n", 39 | "x_3 \\\\\n", 40 | "\\end{bmatrix} \\quad\n", 41 | "\\mathbf{b} = \\begin{bmatrix}\n", 42 | "1 \\\\\n", 43 | "-1 \\\\\n", 44 | "1 \\\\\n", 45 | "\\end{bmatrix}\n", 46 | "$$" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "id": "96b3873f", 52 | "metadata": {}, 53 | "source": [ 54 | "## 前向消元" 55 | ] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "id": "15db4c15", 60 | "metadata": {}, 61 | "source": [ 62 | "$$\\left[\\mathbf{A\\bigm|b}\\right]=\n", 63 | "\\left[\\begin{array}{@{}ccc|c@{}}\n", 64 | "\\fbox{1} & 1 & 1 & 1 \\\\\n", 65 | "1 & 2 & 4 & -1 \\\\\n", 66 | "1 & 3 & 9 & 1 \\\\\n", 67 | "\\end{array}\\right] \\\\\n", 68 | "=\\left[\\begin{array}{@{}ccc|c@{}}\n", 69 | "\\fbox{1} & 1 & 1 & 1 \\\\\n", 70 | "0 & 1 & 3 & -2 \\\\\n", 71 | "0 & 2 & 8 & 0 \\\\\n", 72 | "\\end{array}\\right] \\\\\n", 73 | "=\\left[\\begin{array}{@{}ccc|c@{}}\n", 74 | "1 & 1 & 1 & 1 \\\\\n", 75 | "0 & \\fbox{1} & 3& -2 \\\\\n", 76 | "0 & 1 & 4 & 0 \\\\\n", 77 | "\\end{array}\\right] \\\\\n", 78 | "=\\left[\\begin{array}{@{}ccc|c@{}}\n", 79 | "1 & 1 & 1 & 1 \\\\\n", 80 | "0 & 1 & 3 & -2 \\\\\n", 81 | "0 & 0 & 1 & 2 \\\\\n", 82 | "\\end{array}\\right] \n", 83 | "$$" 84 | ] 85 | }, 86 | { 87 | "cell_type": "markdown", 88 | "id": "b5c299c7", 89 | "metadata": {}, 90 | "source": [ 91 | "## 后向回代\n", 92 | "\n", 93 | "$$\n", 94 | "x_3 = 2 \\rightarrow x_3 = 2 \\\\\n", 95 | "x_2 + 3x_3 = -2 \\rightarrow x_2 = -8 \\\\\n", 96 | "x_1 + x_2 + x_3 = 1 \\rightarrow x_1 = 7\n", 97 | "$$" 98 | ] 99 | }, 100 | { 101 | "cell_type": "markdown", 102 | "id": "6313789c", 103 | "metadata": {}, 104 | "source": [ 105 | "## 计算开销" 106 | ] 107 | }, 108 | { 109 | "cell_type": "markdown", 110 | "id": "3de85034", 111 | "metadata": {}, 112 | "source": [ 113 | "前向操作:\n", 114 | "1. 移动N个pivots,主元\n", 115 | "2. 每个主元,对于该行对应的所有列进行加减操作\n", 116 | "3. 每个主元,要对下面N行进行加减操作\n", 117 | "\n", 118 | "$$\n", 119 | "\\mathcal{O}(N^3)\n", 120 | "$$\n", 121 | "回代操作:\n", 122 | "$$\n", 123 | "\\mathcal{O}(N^2)\n", 124 | "$$\n", 125 | "\n", 126 | "这个开销较大" 127 | ] 128 | }, 129 | { 130 | "cell_type": "markdown", 131 | "id": "56700aa8", 132 | "metadata": {}, 133 | "source": [ 134 | "## LU decomposition,LU分解\n", 135 | "\n", 136 | "$$\n", 137 | "\\mathbf{A=LU} \\rightarrow \\begin{bmatrix}\n", 138 | "a_{11} &a_{12} &a_{13}\\\\\n", 139 | "a_{21} &a_{22} &a_{23}\\\\\n", 140 | "a_{31} &a_{32} &a_{33}\\\\\n", 141 | "\\end{bmatrix}\n", 142 | "= \\underbrace{\\begin{bmatrix}\n", 143 | "1 &0 &0\\\\\n", 144 | "m_{21} &1 &0\\\\\n", 145 | "m_{31} &m_{32} &1\\\\\n", 146 | "\\end{bmatrix}}_\\mathbf{L}\n", 147 | "\\overbrace{\\begin{bmatrix}\n", 148 | "u_{11} &u_{12} &u_{13}\\\\\n", 149 | "0 &u_{22} &u_{23}\\\\\n", 150 | "0 &0 &u_{33}\\\\\n", 151 | "\\end{bmatrix}}^\\mathbf{U}\n", 152 | "$$" 153 | ] 154 | }, 155 | { 156 | "cell_type": "markdown", 157 | "id": "44afa2e5", 158 | "metadata": {}, 159 | "source": [ 160 | "## 计算思路\n", 161 | "$$\n", 162 | "\\mathbf{Ax = b}\\\\\n", 163 | "\\mathbf{LUx = b}\\\\\n", 164 | "\\mathbf{Ly = b}\\\\\n", 165 | "\\mathbf{Ux = y}\n", 166 | "$$" 167 | ] 168 | }, 169 | { 170 | "cell_type": "markdown", 171 | "id": "b9f1c475", 172 | "metadata": {}, 173 | "source": [ 174 | "## LU分解方法\n", 175 | "\n", 176 | "$$\n", 177 | "\\mathbf{A = IA}=\n", 178 | "\\begin{bmatrix}\n", 179 | "1 &0 &0 \\\\\n", 180 | "0 &1 &0 \\\\\n", 181 | "0 &0 &1\n", 182 | "\\end{bmatrix}\n", 183 | "\\begin{bmatrix}\n", 184 | "\\fbox{4} &3 &-1 \\\\\n", 185 | "-2 &-4 &5 \\\\\n", 186 | "1 &2 &6\n", 187 | "\\end{bmatrix}\n", 188 | "$$\n", 189 | "\n", 190 | "$$\n", 191 | "\\mathbf{A}=\n", 192 | "\\begin{bmatrix}\n", 193 | "1 &0 &0 \\\\\n", 194 | "-1/2 &1 &0 \\\\\n", 195 | "1/4 &0 &1\n", 196 | "\\end{bmatrix}\n", 197 | "\\begin{bmatrix}\n", 198 | "4 &3 &-1 \\\\\n", 199 | "0 &\\fbox{-2.5} &4.5 \\\\\n", 200 | "0 &1.25 &6.25\n", 201 | "\\end{bmatrix}\n", 202 | "$$\n", 203 | "\n", 204 | "$$\n", 205 | "\\mathbf{A}=\n", 206 | "\\begin{bmatrix}\n", 207 | "1 &0 &0 \\\\\n", 208 | "-1/2 &1 &0 \\\\\n", 209 | "1/4 &-1/2 &1\n", 210 | "\\end{bmatrix}\n", 211 | "\\begin{bmatrix}\n", 212 | "4 &3 &-1 \\\\\n", 213 | "0 &-2.5 &4.5 \\\\\n", 214 | "0 &0 &8.5\n", 215 | "\\end{bmatrix}\n", 216 | "$$\n", 217 | "\n", 218 | "$$\n", 219 | "\\mathbf{L}=\n", 220 | "\\begin{bmatrix}\n", 221 | "1 &0 &0 \\\\\n", 222 | "-1/2 &1 &0 \\\\\n", 223 | "1/4 &-1/2 &1\n", 224 | "\\end{bmatrix} \\quad and \\quad\n", 225 | "\\mathbf{U}=\n", 226 | "\\begin{bmatrix}\n", 227 | "4 &3 &-1 \\\\\n", 228 | "0 &-2.5 &4.5 \\\\\n", 229 | "0 &0 &8.5\n", 230 | "\\end{bmatrix}\n", 231 | "$$" 232 | ] 233 | }, 234 | { 235 | "cell_type": "markdown", 236 | "id": "5af64815", 237 | "metadata": {}, 238 | "source": [ 239 | "## 注意0主元\n", 240 | "\n", 241 | "### 置换矩阵,permutation matrix\n", 242 | "\n", 243 | "$$\n", 244 | "PAx = Pb\n", 245 | "$$\n", 246 | "\n", 247 | "$$\n", 248 | "LUx = Pb\n", 249 | "$$\n", 250 | "\n", 251 | "$$\n", 252 | "Ly = b' \n", 253 | "$$\n", 254 | "\n", 255 | "$$\n", 256 | "Ux = y\n", 257 | "$$" 258 | ] 259 | }, 260 | { 261 | "cell_type": "code", 262 | "execution_count": 2, 263 | "id": "02e04c93", 264 | "metadata": {}, 265 | "outputs": [ 266 | { 267 | "name": "stdout", 268 | "output_type": "stream", 269 | "text": [ 270 | "[[ 7.]\n", 271 | " [-8.]\n", 272 | " [ 2.]]\n" 273 | ] 274 | } 275 | ], 276 | "source": [ 277 | "import numpy as np\n", 278 | "\n", 279 | "A = np.array([[1, 1, 1], [1, 2, 4], [1, 3, 9]])\n", 280 | "b = np.array([[1], [-1], [1]])\n", 281 | "\n", 282 | "x = np.linalg.solve(A,b) # linear algebra matlab A \\ b;\n", 283 | "print(x)" 284 | ] 285 | }, 286 | { 287 | "cell_type": "code", 288 | "execution_count": 3, 289 | "id": "ab9e3d7d", 290 | "metadata": {}, 291 | "outputs": [ 292 | { 293 | "name": "stdout", 294 | "output_type": "stream", 295 | "text": [ 296 | "[[ 1.]\n", 297 | " [ 0.]\n", 298 | " [-2.]]\n", 299 | "[[ 7.]\n", 300 | " [-8.]\n", 301 | " [ 2.]]\n" 302 | ] 303 | } 304 | ], 305 | "source": [ 306 | "import scipy\n", 307 | "import scipy.linalg\n", 308 | "\n", 309 | "P, L, U = scipy.linalg.lu(A)\n", 310 | "b2 = np.matmul(P, b) # p@b P*b\n", 311 | "\n", 312 | "y = np.linalg.solve(L, b2)\n", 313 | "x = np.linalg.solve(U, y)\n", 314 | "\n", 315 | "print(y)\n", 316 | "print(x)\n" 317 | ] 318 | }, 319 | { 320 | "cell_type": "code", 321 | "execution_count": null, 322 | "id": "4c7b6b8f", 323 | "metadata": {}, 324 | "outputs": [], 325 | "source": [] 326 | }, 327 | { 328 | "cell_type": "markdown", 329 | "id": "2133a93a", 330 | "metadata": {}, 331 | "source": [ 332 | "## Iteative method" 333 | ] 334 | }, 335 | { 336 | "cell_type": "markdown", 337 | "id": "8536ba40", 338 | "metadata": {}, 339 | "source": [ 340 | "## solving $\\mathbf{Ax=b}$\n", 341 | "\n", 342 | "$$\n", 343 | "\\begin{array}{ccc}\n", 344 | "4x - y + z = 7\\\\\n", 345 | "4x -8y + z = -21\\\\\n", 346 | "-2x + y + 5z = 15\n", 347 | "\\end{array}\n", 348 | "$$\n", 349 | "\n", 350 | "$$\n", 351 | "\\begin{bmatrix}\n", 352 | "4 &-1 &1\\\\\n", 353 | "4 &-8 &1\\\\\n", 354 | "-2 &1 &5\n", 355 | "\\end{bmatrix}\n", 356 | "\\begin{bmatrix}\n", 357 | "x\\\\\n", 358 | "y\\\\\n", 359 | "z\n", 360 | "\\end{bmatrix}=\n", 361 | "\\begin{bmatrix}\n", 362 | "7\\\\\n", 363 | "-21\\\\\n", 364 | "15\n", 365 | "\\end{bmatrix}\n", 366 | "$$" 367 | ] 368 | }, 369 | { 370 | "cell_type": "code", 371 | "execution_count": 3, 372 | "id": "0d91e544", 373 | "metadata": {}, 374 | "outputs": [ 375 | { 376 | "name": "stdout", 377 | "output_type": "stream", 378 | "text": [ 379 | "[[2.]\n", 380 | " [4.]\n", 381 | " [3.]]\n" 382 | ] 383 | } 384 | ], 385 | "source": [ 386 | "import numpy as np\n", 387 | "\n", 388 | "A = np.array([[4, -1, 1],\n", 389 | " [4, -8, 1],\n", 390 | " [-2, 1, 5]])\n", 391 | "\n", 392 | "b = np.array([[7],\n", 393 | " [-21],\n", 394 | " [15]])\n", 395 | "\n", 396 | "x = np.linalg.solve(A, b)\n", 397 | "print(x)" 398 | ] 399 | }, 400 | { 401 | "cell_type": "markdown", 402 | "id": "a8ad1d83", 403 | "metadata": {}, 404 | "source": [ 405 | "## iterative process\n", 406 | "\n", 407 | "$$\n", 408 | " \\begin{array}{}\n", 409 | "x &= \\dfrac{7 + y - z}{4} \\\\\n", 410 | "y &= \\dfrac{21+4x+z}{8} \\\\\n", 411 | "z &= \\dfrac{15+2x-y}{5}\n", 412 | "\\end{array} \\Rightarrow\n", 413 | " \\begin{array}{}\n", 414 | "x_{k+1} &= \\dfrac{7 + y_{k} - z_{k}}{4} \\\\\n", 415 | "y_{k+1} &= \\dfrac{21+4x_{k}+z_{k}}{8} \\\\\n", 416 | "z_{k+1} &= \\dfrac{15+2x_{k}-y_{k}}{5}\n", 417 | "\\end{array} \n", 418 | "$$" 419 | ] 420 | }, 421 | { 422 | "cell_type": "markdown", 423 | "id": "b6f1fe2c", 424 | "metadata": {}, 425 | "source": [ 426 | "## 算法,algorithm\n", 427 | "\n", 428 | "### 1. Guess initial values: $(x_0, y_0, z_0)^T$\n", 429 | "### 2. Iterate the Jacobi scheme: $\\mathbf{x_{k+1} = Ax_k}$\n", 430 | "### 3. Check for convergence: $\\Vert\\mathbf{x_{k+1}-x_{k}}\\Vert< tolerance$" 431 | ] 432 | }, 433 | { 434 | "cell_type": "code", 435 | "execution_count": 38, 436 | "id": "f9c1f393", 437 | "metadata": {}, 438 | "outputs": [ 439 | { 440 | "name": "stdout", 441 | "output_type": "stream", 442 | "text": [ 443 | "correct, k is 22\n", 444 | "[[2.]\n", 445 | " [4.]\n", 446 | " [3.]]\n" 447 | ] 448 | } 449 | ], 450 | "source": [ 451 | "# jacobian\n", 452 | "import numpy as np\n", 453 | "\n", 454 | "X_initial = np.zeros((3, 1))\n", 455 | "X_initial = np.array([[1], [2], [2]])\n", 456 | "X_next = np.zeros((3, 1))\n", 457 | "\n", 458 | "for k in range(100):\n", 459 | " X_next[0] = (7 + X_initial[1] - X_initial[2]) / 4\n", 460 | " X_next[1] = (21 + 4*X_initial[0] + X_initial[2]) / 8\n", 461 | " X_next[2] = (15 + 2*X_initial[0] - X_initial[1]) / 5\n", 462 | "\n", 463 | " if (np.linalg.norm(X_next - X_initial) < 1e-10):\n", 464 | " print(\"correct, k is\", k)\n", 465 | " print(X_next)\n", 466 | " break\n", 467 | " X_initial = np.copy(X_next)\n", 468 | " " 469 | ] 470 | }, 471 | { 472 | "cell_type": "code", 473 | "execution_count": 50, 474 | "id": "a3629af2", 475 | "metadata": { 476 | "scrolled": false 477 | }, 478 | "outputs": [ 479 | { 480 | "name": "stdout", 481 | "output_type": "stream", 482 | "text": [ 483 | "correct, k is 12\n", 484 | "[[2.]\n", 485 | " [4.]\n", 486 | " [3.]]\n" 487 | ] 488 | } 489 | ], 490 | "source": [ 491 | "# gauss-seidel\n", 492 | "import numpy as np\n", 493 | "\n", 494 | "X_initial = np.zeros((3, 1))\n", 495 | "X_initial = np.array([[1], [2], [2]])\n", 496 | "X_next = np.zeros((3, 1))\n", 497 | "\n", 498 | "for k in range(100):\n", 499 | " X_next[0] = (7 + X_initial[1] - X_initial[2]) / 4\n", 500 | " X_next[1] = (21 + 4*X_next[0] + X_initial[2]) / 8\n", 501 | " X_next[2] = (15 + 2*X_next[0] - X_next[1]) / 5\n", 502 | "\n", 503 | " if (np.linalg.norm(X_next - X_initial) < 1e-10):\n", 504 | " print(\"correct, k is\", k)\n", 505 | " print(X_next)\n", 506 | " break\n", 507 | " X_initial = np.copy(X_next)" 508 | ] 509 | }, 510 | { 511 | "cell_type": "code", 512 | "execution_count": 27, 513 | "id": "fd4b7d77", 514 | "metadata": {}, 515 | "outputs": [ 516 | { 517 | "data": { 518 | "text/plain": [ 519 | "0.5" 520 | ] 521 | }, 522 | "execution_count": 27, 523 | "metadata": {}, 524 | "output_type": "execute_result" 525 | } 526 | ], 527 | "source": [] 528 | } 529 | ], 530 | "metadata": { 531 | "kernelspec": { 532 | "display_name": "Python 3 (ipykernel)", 533 | "language": "python", 534 | "name": "python3" 535 | }, 536 | "language_info": { 537 | "codemirror_mode": { 538 | "name": "ipython", 539 | "version": 3 540 | }, 541 | "file_extension": ".py", 542 | "mimetype": "text/x-python", 543 | "name": "python", 544 | "nbconvert_exporter": "python", 545 | "pygments_lexer": "ipython3", 546 | "version": "3.8.10" 547 | } 548 | }, 549 | "nbformat": 4, 550 | "nbformat_minor": 5 551 | } 552 | -------------------------------------------------------------------------------- /lecture/lecture20/code/Lecture03/Gauss_elimination.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "61981b1a", 6 | "metadata": {}, 7 | "source": [ 8 | "# Gauss Elimination 高斯消元\n", 9 | "\n", 10 | "## 直接解法 $\\mathbf{Ax = b}$" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "id": "e63cf42f", 16 | "metadata": {}, 17 | "source": [ 18 | "$$\n", 19 | "x_1 + x_2 + x_3 = 1 \\\\\n", 20 | "x_1 + 2x_2 + 4x_3 = -1 \\\\\n", 21 | "x_1 + 3x_2 + 9x_3 = 1\n", 22 | "$$\n" 23 | ] 24 | }, 25 | { 26 | "cell_type": "markdown", 27 | "id": "bd0de494", 28 | "metadata": {}, 29 | "source": [ 30 | "$$\n", 31 | "\\mathbf{A} = \\begin{bmatrix}\n", 32 | "1 &1 &1\\\\\n", 33 | "1 &2 &4\\\\\n", 34 | "1 &3 &9\\\\\n", 35 | "\\end{bmatrix} \\quad\n", 36 | "\\mathbf{x} = \\begin{bmatrix}\n", 37 | "x_1 \\\\\n", 38 | "x_2 \\\\\n", 39 | "x_3 \\\\\n", 40 | "\\end{bmatrix} \\quad\n", 41 | "\\mathbf{b} = \\begin{bmatrix}\n", 42 | "1 \\\\\n", 43 | "-1 \\\\\n", 44 | "1 \\\\\n", 45 | "\\end{bmatrix}\n", 46 | "$$" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "id": "96b3873f", 52 | "metadata": {}, 53 | "source": [ 54 | "## 前向消元" 55 | ] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "id": "15db4c15", 60 | "metadata": {}, 61 | "source": [ 62 | "$$\\left[\\mathbf{A\\bigm|b}\\right]=\n", 63 | "\\left[\\begin{array}{@{}ccc|c@{}}\n", 64 | "\\fbox{1} & 1 & 1 & 1 \\\\\n", 65 | "1 & 2 & 4 & -1 \\\\\n", 66 | "1 & 3 & 9 & 1 \\\\\n", 67 | "\\end{array}\\right] \\\\\n", 68 | "=\\left[\\begin{array}{@{}ccc|c@{}}\n", 69 | "\\fbox{1} & 1 & 1 & 1 \\\\\n", 70 | "0 & 1 & 3 & -2 \\\\\n", 71 | "0 & 2 & 8 & 0 \\\\\n", 72 | "\\end{array}\\right] \\\\\n", 73 | "=\\left[\\begin{array}{@{}ccc|c@{}}\n", 74 | "1 & 1 & 1 & 1 \\\\\n", 75 | "0 & \\fbox{1} & 3& -2 \\\\\n", 76 | "0 & 1 & 4 & 0 \\\\\n", 77 | "\\end{array}\\right] \\\\\n", 78 | "=\\left[\\begin{array}{@{}ccc|c@{}}\n", 79 | "1 & 1 & 1 & 1 \\\\\n", 80 | "0 & 1 & 3 & -2 \\\\\n", 81 | "0 & 0 & 1 & 2 \\\\\n", 82 | "\\end{array}\\right] \n", 83 | "$$" 84 | ] 85 | }, 86 | { 87 | "cell_type": "markdown", 88 | "id": "b5c299c7", 89 | "metadata": {}, 90 | "source": [ 91 | "## 后向回代\n", 92 | "\n", 93 | "$$\n", 94 | "x_3 = 2 \\rightarrow x_3 = 2 \\\\\n", 95 | "x_2 + 3x_3 = -2 \\rightarrow x_2 = -8 \\\\\n", 96 | "x_1 + x_2 + x_3 = 1 \\rightarrow x_1 = 7\n", 97 | "$$" 98 | ] 99 | }, 100 | { 101 | "cell_type": "markdown", 102 | "id": "6313789c", 103 | "metadata": {}, 104 | "source": [ 105 | "## 计算开销" 106 | ] 107 | }, 108 | { 109 | "cell_type": "markdown", 110 | "id": "3de85034", 111 | "metadata": {}, 112 | "source": [ 113 | "前向操作:\n", 114 | "1. 移动N个pivots,主元\n", 115 | "2. 每个主元,对于该行对应的所有列进行加减操作\n", 116 | "3. 每个主元,要对下面N行进行加减操作\n", 117 | "\n", 118 | "$$\n", 119 | "\\mathcal{O}(N^3)\n", 120 | "$$\n", 121 | "回代操作:\n", 122 | "$$\n", 123 | "\\mathcal{O}(N^2)\n", 124 | "$$\n", 125 | "\n", 126 | "这个开销较大" 127 | ] 128 | }, 129 | { 130 | "cell_type": "markdown", 131 | "id": "56700aa8", 132 | "metadata": {}, 133 | "source": [ 134 | "## LU decomposition,LU分解\n", 135 | "\n", 136 | "$$\n", 137 | "\\mathbf{A=LU} \\rightarrow \\begin{bmatrix}\n", 138 | "a_{11} &a_{12} &a_{13}\\\\\n", 139 | "a_{21} &a_{22} &a_{23}\\\\\n", 140 | "a_{31} &a_{32} &a_{33}\\\\\n", 141 | "\\end{bmatrix}\n", 142 | "= \\underbrace{\\begin{bmatrix}\n", 143 | "1 &0 &0\\\\\n", 144 | "m_{21} &1 &0\\\\\n", 145 | "m_{31} &m_{32} &1\\\\\n", 146 | "\\end{bmatrix}}_\\mathbf{L}\n", 147 | "\\overbrace{\\begin{bmatrix}\n", 148 | "u_{11} &u_{12} &u_{13}\\\\\n", 149 | "0 &u_{22} &u_{23}\\\\\n", 150 | "0 &0 &u_{33}\\\\\n", 151 | "\\end{bmatrix}}^\\mathbf{U}\n", 152 | "$$" 153 | ] 154 | }, 155 | { 156 | "cell_type": "markdown", 157 | "id": "44afa2e5", 158 | "metadata": {}, 159 | "source": [ 160 | "## 计算思路\n", 161 | "$$\n", 162 | "\\mathbf{Ax = b}\\\\\n", 163 | "\\mathbf{LUx = b}\\\\\n", 164 | "\\mathbf{Ly = b}\\\\\n", 165 | "\\mathbf{Ux = y}\n", 166 | "$$" 167 | ] 168 | }, 169 | { 170 | "cell_type": "markdown", 171 | "id": "b9f1c475", 172 | "metadata": {}, 173 | "source": [ 174 | "## LU分解方法\n", 175 | "\n", 176 | "$$\n", 177 | "\\mathbf{A = IA}=\n", 178 | "\\begin{bmatrix}\n", 179 | "1 &0 &0 \\\\\n", 180 | "0 &1 &0 \\\\\n", 181 | "0 &0 &1\n", 182 | "\\end{bmatrix}\n", 183 | "\\begin{bmatrix}\n", 184 | "\\fbox{4} &3 &-1 \\\\\n", 185 | "-2 &-4 &5 \\\\\n", 186 | "1 &2 &6\n", 187 | "\\end{bmatrix}\n", 188 | "$$\n", 189 | "\n", 190 | "$$\n", 191 | "\\mathbf{A}=\n", 192 | "\\begin{bmatrix}\n", 193 | "1 &0 &0 \\\\\n", 194 | "-1/2 &1 &0 \\\\\n", 195 | "1/4 &0 &1\n", 196 | "\\end{bmatrix}\n", 197 | "\\begin{bmatrix}\n", 198 | "4 &3 &-1 \\\\\n", 199 | "0 &\\fbox{-2.5} &4.5 \\\\\n", 200 | "0 &1.25 &6.25\n", 201 | "\\end{bmatrix}\n", 202 | "$$\n", 203 | "\n", 204 | "$$\n", 205 | "\\mathbf{A}=\n", 206 | "\\begin{bmatrix}\n", 207 | "1 &0 &0 \\\\\n", 208 | "-1/2 &1 &0 \\\\\n", 209 | "1/4 &-1/2 &1\n", 210 | "\\end{bmatrix}\n", 211 | "\\begin{bmatrix}\n", 212 | "4 &3 &-1 \\\\\n", 213 | "0 &-2.5 &4.5 \\\\\n", 214 | "0 &0 &8.5\n", 215 | "\\end{bmatrix}\n", 216 | "$$\n", 217 | "\n", 218 | "$$\n", 219 | "\\mathbf{L}=\n", 220 | "\\begin{bmatrix}\n", 221 | "1 &0 &0 \\\\\n", 222 | "-1/2 &1 &0 \\\\\n", 223 | "1/4 &-1/2 &1\n", 224 | "\\end{bmatrix} \\quad and \\quad\n", 225 | "\\mathbf{U}=\n", 226 | "\\begin{bmatrix}\n", 227 | "4 &3 &-1 \\\\\n", 228 | "0 &-2.5 &4.5 \\\\\n", 229 | "0 &0 &8.5\n", 230 | "\\end{bmatrix}\n", 231 | "$$" 232 | ] 233 | }, 234 | { 235 | "cell_type": "markdown", 236 | "id": "5af64815", 237 | "metadata": {}, 238 | "source": [ 239 | "## 注意0主元\n", 240 | "\n", 241 | "### 置换矩阵,permutation matrix\n", 242 | "\n", 243 | "$$\n", 244 | "PAx = Pb\n", 245 | "$$\n", 246 | "\n", 247 | "$$\n", 248 | "LUx = Pb\n", 249 | "$$\n", 250 | "\n", 251 | "$$\n", 252 | "Ly = b' \n", 253 | "$$\n", 254 | "\n", 255 | "$$\n", 256 | "Ux = y\n", 257 | "$$" 258 | ] 259 | }, 260 | { 261 | "cell_type": "code", 262 | "execution_count": null, 263 | "id": "02e04c93", 264 | "metadata": {}, 265 | "outputs": [], 266 | "source": [ 267 | "import numpy as np\n", 268 | "\n", 269 | "A = np.array([[1, 1, 1], [1, 2, 4], [1, 3, 9]])\n", 270 | "b = np.array([[1], [-1], [1]])\n", 271 | "\n", 272 | "x = np.linalg.solve(A,b) # linear algebra matlab A \\ b;\n", 273 | "print(x)" 274 | ] 275 | }, 276 | { 277 | "cell_type": "code", 278 | "execution_count": null, 279 | "id": "ab9e3d7d", 280 | "metadata": {}, 281 | "outputs": [], 282 | "source": [ 283 | "import scipy\n", 284 | "import scipy.linalg\n", 285 | "\n", 286 | "P, L, U = scipy.linalg.lu(A)\n", 287 | "b2 = np.matmul(P, b) # p@b P*b\n", 288 | "\n", 289 | "y = np.linalg.solve(L, b2)\n", 290 | "x = np.linalg.solve(U, y)\n", 291 | "\n", 292 | "print(y)\n", 293 | "print(x)\n" 294 | ] 295 | }, 296 | { 297 | "cell_type": "code", 298 | "execution_count": null, 299 | "id": "4c7b6b8f", 300 | "metadata": {}, 301 | "outputs": [], 302 | "source": [] 303 | }, 304 | { 305 | "cell_type": "markdown", 306 | "id": "2133a93a", 307 | "metadata": {}, 308 | "source": [ 309 | "## Iteative method" 310 | ] 311 | }, 312 | { 313 | "cell_type": "markdown", 314 | "id": "122305f2", 315 | "metadata": {}, 316 | "source": [ 317 | "## solving $\\mathbf{Ax=b}$\n", 318 | "\n", 319 | "$$\n", 320 | "\\begin{array}{ccc}\n", 321 | "4x - y + z = 7\\\\\n", 322 | "4x -8y + z = -21\\\\\n", 323 | "-2x + y + 5z = 15\n", 324 | "\\end{array}\n", 325 | "$$\n", 326 | "\n", 327 | "$$\n", 328 | "\\begin{bmatrix}\n", 329 | "4 &-1 &1\\\\\n", 330 | "4 &-8 &1\\\\\n", 331 | "-2 &1 &5\n", 332 | "\\end{bmatrix}\n", 333 | "\\begin{bmatrix}\n", 334 | "x\\\\\n", 335 | "y\\\\\n", 336 | "z\n", 337 | "\\end{bmatrix}=\n", 338 | "\\begin{bmatrix}\n", 339 | "7\\\\\n", 340 | "-21\\\\\n", 341 | "15\n", 342 | "\\end{bmatrix}\n", 343 | "$$" 344 | ] 345 | }, 346 | { 347 | "cell_type": "code", 348 | "execution_count": 55, 349 | "id": "37d63ed0", 350 | "metadata": {}, 351 | "outputs": [ 352 | { 353 | "name": "stdout", 354 | "output_type": "stream", 355 | "text": [ 356 | "[[2.]\n", 357 | " [4.]\n", 358 | " [3.]]\n" 359 | ] 360 | } 361 | ], 362 | "source": [ 363 | "import numpy as np\n", 364 | "\n", 365 | "A = np.array([[4, -1, 1],\n", 366 | " [4, -8, 1],\n", 367 | " [-2, 1, 5]])\n", 368 | "\n", 369 | "b = np.array([[7],\n", 370 | " [-21],\n", 371 | " [15]])\n", 372 | "\n", 373 | "x = np.linalg.solve(A, b)\n", 374 | "print(x)" 375 | ] 376 | }, 377 | { 378 | "cell_type": "markdown", 379 | "id": "ac63b78f", 380 | "metadata": {}, 381 | "source": [ 382 | "## iterative process\n", 383 | "\n", 384 | "$$\n", 385 | " \\begin{array}{}\n", 386 | "x &= \\dfrac{7 + y - z}{4} \\\\\n", 387 | "y &= \\dfrac{21+4x+z}{8} \\\\\n", 388 | "z &= \\dfrac{15+2x-y}{5}\n", 389 | "\\end{array} \\Rightarrow\n", 390 | " \\begin{array}{}\n", 391 | "x_{k+1} &= \\dfrac{7 + y_{k} - z_{k}}{4} \\\\\n", 392 | "y_{k+1} &= \\dfrac{21+4x_{k}+z_{k}}{8} \\\\\n", 393 | "z_{k+1} &= \\dfrac{15+2x_{k}-y_{k}}{5}\n", 394 | "\\end{array} \n", 395 | "$$\n", 396 | "\n", 397 | "$$\n", 398 | " \\begin{array}{}\n", 399 | "x &= \\dfrac{7 + y - z}{4} \\\\\n", 400 | "y &= \\dfrac{21+4x+z}{8} \\\\\n", 401 | "z &= \\dfrac{15+2x-y}{5}\n", 402 | "\\end{array} \\Rightarrow\n", 403 | " \\begin{array}{}\n", 404 | "x_{k+1} &= \\dfrac{7 + y_{k} - z_{k}}{4} \\\\\n", 405 | "y_{k+1} &= \\dfrac{21+4x_{k+1}+z_{k}}{8} \\\\\n", 406 | "z_{k+1} &= \\dfrac{15+2x_{k+1}-y_{k+1}}{5}\n", 407 | "\\end{array} \n", 408 | "$$" 409 | ] 410 | }, 411 | { 412 | "cell_type": "markdown", 413 | "id": "4c6e9da9", 414 | "metadata": {}, 415 | "source": [ 416 | "## 算法,algorithm\n", 417 | "\n", 418 | "### 1. Guess initial values: $(x_0, y_0, z_0)^T$\n", 419 | "### 2. Iterate the Jacobi scheme: $\\mathbf{x_{k+1} = Ax_k}$\n", 420 | "### 3. Check for convergence: $\\Vert\\mathbf{x_{k+1}-x_{k}}\\Vert< tolerance$" 421 | ] 422 | }, 423 | { 424 | "cell_type": "code", 425 | "execution_count": 59, 426 | "id": "8d4ede75", 427 | "metadata": {}, 428 | "outputs": [ 429 | { 430 | "name": "stdout", 431 | "output_type": "stream", 432 | "text": [ 433 | "correct, k is 23\n", 434 | "[[2.]\n", 435 | " [4.]\n", 436 | " [3.]]\n" 437 | ] 438 | } 439 | ], 440 | "source": [ 441 | "# jacobian\n", 442 | "import numpy as np\n", 443 | "\n", 444 | "X_initial = np.zeros((3, 1))\n", 445 | "#X_initial = np.array([[1], [1], [1]])\n", 446 | "X_next = np.zeros((3, 1))\n", 447 | "\n", 448 | "for k in range(100):\n", 449 | " X_next[0] = (7 + X_initial[1] - X_initial[2]) / 4\n", 450 | " X_next[1] = (21 + 4*X_initial[0] + X_initial[2]) / 8\n", 451 | " X_next[2] = (15 + 2*X_initial[0] - X_initial[1]) / 5\n", 452 | "\n", 453 | " if (np.linalg.norm(X_next - X_initial) < 1e-10):\n", 454 | " print(\"correct, k is\", k)\n", 455 | " print(X_next)\n", 456 | " break\n", 457 | " X_initial = np.copy(X_next)\n", 458 | " " 459 | ] 460 | }, 461 | { 462 | "cell_type": "code", 463 | "execution_count": 57, 464 | "id": "9cf02f6a", 465 | "metadata": { 466 | "scrolled": false 467 | }, 468 | "outputs": [ 469 | { 470 | "name": "stdout", 471 | "output_type": "stream", 472 | "text": [ 473 | "correct, k is 12\n", 474 | "[[2.]\n", 475 | " [4.]\n", 476 | " [3.]]\n" 477 | ] 478 | } 479 | ], 480 | "source": [ 481 | "# gauss-seidel\n", 482 | "import numpy as np\n", 483 | "\n", 484 | "X_initial = np.zeros((3, 1))\n", 485 | "#X_initial = np.array([[1], [2], [2]])\n", 486 | "X_next = np.zeros((3, 1))\n", 487 | "\n", 488 | "for k in range(100):\n", 489 | " X_next[0] = (7 + X_initial[1] - X_initial[2]) / 4\n", 490 | " X_next[1] = (21 + 4*X_next[0] + X_initial[2]) / 8\n", 491 | " X_next[2] = (15 + 2*X_next[0] - X_next[1]) / 5\n", 492 | "\n", 493 | " if (np.linalg.norm(X_next - X_initial) < 1e-10):\n", 494 | " print(\"correct, k is\", k)\n", 495 | " print(X_next)\n", 496 | " break\n", 497 | " X_initial = np.copy(X_next)" 498 | ] 499 | }, 500 | { 501 | "cell_type": "markdown", 502 | "id": "74945e48", 503 | "metadata": {}, 504 | "source": [ 505 | "## Strict diagonal dominance\n", 506 | "\n", 507 | "$$\n", 508 | "|a_{ii}| > \\sum_{j=1, j\\neq i}^{N}|a_{ij}|\n", 509 | "$$" 510 | ] 511 | }, 512 | { 513 | "cell_type": "markdown", 514 | "id": "0ab8d0f7", 515 | "metadata": {}, 516 | "source": [ 517 | "$$\n", 518 | "\\mathbf{A} = \\begin{bmatrix}\n", 519 | "4 &-1 &1\\\\\n", 520 | "4 &-8 &1\\\\\n", 521 | "-2 &1 &5\n", 522 | "\\end{bmatrix}\n", 523 | "$$\n", 524 | "\n", 525 | "$$\n", 526 | "\\mathbf{A} = \\begin{bmatrix}\n", 527 | "-2 &1 &5\\\\\n", 528 | "4 &-8 &1\\\\\n", 529 | "4 &-1 &1\n", 530 | "\\end{bmatrix}\n", 531 | "$$" 532 | ] 533 | } 534 | ], 535 | "metadata": { 536 | "kernelspec": { 537 | "display_name": "Python 3 (ipykernel)", 538 | "language": "python", 539 | "name": "python3" 540 | }, 541 | "language_info": { 542 | "codemirror_mode": { 543 | "name": "ipython", 544 | "version": 3 545 | }, 546 | "file_extension": ".py", 547 | "mimetype": "text/x-python", 548 | "name": "python", 549 | "nbconvert_exporter": "python", 550 | "pygments_lexer": "ipython3", 551 | "version": "3.8.10" 552 | } 553 | }, 554 | "nbformat": 4, 555 | "nbformat_minor": 5 556 | } 557 | -------------------------------------------------------------------------------- /lecture/lecture20/code/Lecture04/.ipynb_checkpoints/goog-downloadwhite-proto.metadata: -------------------------------------------------------------------------------- 1 | How to run me: 2 | 3 | dakota -i dakota_case.in 4 | 5 | or 6 | 7 | dakota -i dakota_case.in | tee log.dakota 8 | dakota -i dakota_case.in -o log.dakota_stdout > log.stdout | tail -f log.stdout 9 | dakota -i dakota_case.in -o log.dakota_stdout | tee log.dakota 10 | dakota -i dakota_case.in -o log.dakota_stdout -e log.dakota_stderror | tee log.stdout 11 | dakota -i dakota_case.in -o log.dakota_stdout -e log.dakota_stderror > log.stdout 2>&1 12 | dakota -i dakota_case.in -o log.dakota_stdout -e log.dakota_stderror 2>&1 | tee log.stdout #Maybe the best option 13 | 14 | 15 | 16 | This is a SBO case 17 | To construct the surrogate use an input file with no interface column or freeform format 18 | It does not work when using input file with interface column check this -------------------------------------------------------------------------------- /lecture/lecture20/code/Lecture04/.ipynb_checkpoints/goog-downloadwhite-proto.vlpset: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | rm dakota.rst > /dev/null 2>&1 4 | rm idr-parsed-input.txt > /dev/null 2>&1 5 | rm LHS* > /dev/null 2>&1 6 | rm S4 > /dev/null 2>&1 7 | rm fort.* > /dev/null 2>&1 8 | rm -r workdir.* > /dev/null 2>&1 9 | rm finaldata* > /dev/null 2>&1 10 | rm population_* > /dev/null 2>&1 11 | rm discards* > /dev/null 2>&1 12 | rm JEGAGlobal.log > /dev/null 2>&1 13 | rm params.in.* > /dev/null 2>&1 14 | rm results.out.* > /dev/null 2>&1 15 | rm run.out > /dev/null 2>&1 16 | rm stdout.out > /dev/null 2>&1 17 | rm table_out.dat > /dev/null 2>&1 18 | rm *~ > /dev/null 2>&1 19 | -------------------------------------------------------------------------------- /lecture/lecture20/code/Lecture04/.ipynb_checkpoints/goog-malware-proto.metadata: -------------------------------------------------------------------------------- 1 | ********************************************************** 2 | OPT++ version 2.4 3 | Job run at Sun Jun 6 02:25:04 2021 4 | 5 | ********************************************************** 6 | readOptInput: No opt.input file found 7 | readOptInput: default values will be used 8 | 9 | 10 | ========= Initial state =========== 11 | 12 | 13 | i xc grad fcn_accrcy 14 | 0 -0x1.8p-1 0x1.2e6bd398bfe8p-2 0x1p-52 15 | 1 0x1.8p-1 -0x1.d14944b38a8e2p-3 0x1p-52 16 | 2 0x1.8p-1 0x1.115761dcbffe8p-3 0x1p-52 17 | 3-0x1.999999999999ap-3 -0x1.d7fed901bbca2p-3 0x1p-52 18 | Function Value = 0x1.4a92cdefc0197p-5 19 | Norm of gradient = 0x1.d4fe6afa2ad59p-2 20 | 21 | 22 | ============================================== 23 | 24 | 25 | Bound constrained Quasi-Newton Method with Line Search 26 | 27 | Iter F(x) ||grad|| ||step|| f/g 28 | 29 | 0 0x1.4a92cdefc0197p-5 0x1.d4fe6afa2ad59p-2 30 | 1 -0x1.743126267278cp-3 0x1.59f42219974bcp-1 0x1.96fec0bb97ccep-2 N 2 2 31 | OptBCNewtonLike : variable added to working set : 3 32 | OptBCNewtonLike: Current working set 33 | ----- variable index: 3 0 34 | 2 -0x1.1994e974eb852p-2 0x1.d7519e2d0e9e3p-1 0x1.0d822da8c6a75p-1 N 3 3 35 | OptBCNewtonLike : variable added to working set : 0 36 | OptBCNewtonLike: Current working set 37 | ----- variable index: 0 -1.5 3 0 38 | 3 -0x1.9835652ebeef7p-2 0x1.1b01c92972abdp+0 0x1.7bd21253ee39ap-3 N 4 4 39 | OptBCNewtonLike: Current working set 40 | ----- variable index: 0 -1.5 3 0 41 | 4 -0x1.32dffe34fae7p-1 0x1.94d0f055f2933p+0 0x1.019b2261cf6ecp-1 N 5 5 42 | OptBCNewtonLike : variable added to working set : 1 43 | OptBCNewtonLike: Current working set 44 | ----- variable index: 0 -1.5 1 0.5 3 0 45 | 5 -0x1.33a1d1d327658p-1 0x1.998dd167c9df9p+0 0x1.4cdc5346905cp-6 N 6 6 46 | OptBCNewtonLike: Current working set 47 | ----- variable index: 0 -1.5 1 0.5 3 0 48 | 6 -0x1.345e49b8192bap-1 0x1.a0a3572072112p+0 0x1.fcba8413d8c8p-6 N 7 7 49 | OptBCNewtonLike: Current working set 50 | ----- variable index: 0 -1.5 1 0.5 3 0 51 | 7 -0x1.346877e023aa1p-1 0x1.a13541828ae51p+0 0x1.4c813af26eap-9 N 8 8 52 | checkConvg: gnorm = 0x1.4290fe0e4ep-14 gtol = 0x1.a36e2eb1c432dp-14 53 | OptBCNewtonLike : reduced_grad_norm = 7.69058e-05 54 | OptBCNewtonLike: Current working set 55 | ----- variable index: 0 -1.5 1 0.5 3 0 56 | OptBCNewtonLike : convergence achieved. 57 | 58 | 59 | ========= Solution from Opt++ =========== 60 | 61 | Optimization method = Bound constrained Quasi-Newton 62 | Dimension of the problem = 4 63 | No. of bound constraints = 4 64 | Return code = 3 (Algorithm converged - Norm of gradient is less than gradient tolerance) 65 | No. iterations taken = 7 66 | No. function evaluations = 8 67 | No. gradient evaluations = 8 68 | 69 | 70 | ========= Solution from Opt++ =========== 71 | 72 | 73 | i xc grad fcn_accrcy 74 | 0 -0x1.8p+0 0x1.18ba10c4d7798p+0 0x1p-52 75 | 1 0x1p-1 0x1.3c7eb78488c6cp-4 0x1p-52 76 | 20x1.dfddf1dddbf19p-1 -0x1.4290fe0e4ep-14 0x1p-52 77 | 3 0x0p+0 -0x1.33ffe0ffb6b5cp+0 0x1p-52 78 | Function Value = -0x1.346877e023aa1p-1 79 | Norm of gradient = 0x1.a13541828ae51p+0 80 | 81 | 82 | ============================================== 83 | 84 | 85 | 86 | ========== Tolerances =========== 87 | 88 | Machine Epsilon = 2.22045e-16 89 | Maximum Step = 1000 90 | Minimum Step = 1.49012e-08 91 | Maximum Iter = 100 92 | Maximum Backtracks = 5 93 | Maximum Fcn Eval = 1000 94 | Step Tolerance = 1.49012e-08 95 | Function Tolerance = 1e-05 96 | Constraint Tolerance = 1.49012e-08 97 | Gradient Tolerance = 0.0001 98 | LineSearch Tolerance = 0.0001 99 | -------------------------------------------------------------------------------- /lecture/lecture20/code/Lecture04/.ipynb_checkpoints/goog-malware-proto.vlpset: -------------------------------------------------------------------------------- 1 | { 2 | "name": "braces", 3 | "description": "Bash-like brace expansion, implemented in JavaScript. Safer than other brace expansion libs, with complete support for the Bash 4.3 braces specification, without sacrificing speed.", 4 | "version": "3.0.2", 5 | "homepage": "https://github.com/micromatch/braces", 6 | "author": "Jon Schlinkert (https://github.com/jonschlinkert)", 7 | "contributors": [ 8 | "Brian Woodward (https://twitter.com/doowb)", 9 | "Elan Shanker (https://github.com/es128)", 10 | "Eugene Sharygin (https://github.com/eush77)", 11 | "hemanth.hm (http://h3manth.com)", 12 | "Jon Schlinkert (http://twitter.com/jonschlinkert)" 13 | ], 14 | "repository": "micromatch/braces", 15 | "bugs": { 16 | "url": "https://github.com/micromatch/braces/issues" 17 | }, 18 | "license": "MIT", 19 | "files": [ 20 | "index.js", 21 | "lib" 22 | ], 23 | "main": "index.js", 24 | "engines": { 25 | "node": ">=8" 26 | }, 27 | "scripts": { 28 | "test": "mocha", 29 | "benchmark": "node benchmark" 30 | }, 31 | "dependencies": { 32 | "fill-range": "^7.0.1" 33 | }, 34 | "devDependencies": { 35 | "ansi-colors": "^3.2.4", 36 | "bash-path": "^2.0.1", 37 | "gulp-format-md": "^2.0.0", 38 | "mocha": "^6.1.1" 39 | }, 40 | "keywords": [ 41 | "alpha", 42 | "alphabetical", 43 | "bash", 44 | "brace", 45 | "braces", 46 | "expand", 47 | "expansion", 48 | "filepath", 49 | "fill", 50 | "fs", 51 | "glob", 52 | "globbing", 53 | "letter", 54 | "match", 55 | "matches", 56 | "matching", 57 | "number", 58 | "numerical", 59 | "path", 60 | "range", 61 | "ranges", 62 | "sh" 63 | ], 64 | "verb": { 65 | "toc": false, 66 | "layout": "default", 67 | "tasks": [ 68 | "readme" 69 | ], 70 | "lint": { 71 | "reflinks": true 72 | }, 73 | "plugins": [ 74 | "gulp-format-md" 75 | ] 76 | } 77 | 78 | ,"_resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz" 79 | ,"_integrity": "sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==" 80 | ,"_from": "braces@3.0.2" 81 | } -------------------------------------------------------------------------------- /lecture/lecture20/code/Lecture04/.ipynb_checkpoints/goog-phish-proto.metadata: -------------------------------------------------------------------------------- 1 | # braces [![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=W8YFZ425KND68) [![NPM version](https://img.shields.io/npm/v/braces.svg?style=flat)](https://www.npmjs.com/package/braces) [![NPM monthly downloads](https://img.shields.io/npm/dm/braces.svg?style=flat)](https://npmjs.org/package/braces) [![NPM total downloads](https://img.shields.io/npm/dt/braces.svg?style=flat)](https://npmjs.org/package/braces) [![Linux Build Status](https://img.shields.io/travis/micromatch/braces.svg?style=flat&label=Travis)](https://travis-ci.org/micromatch/braces) 2 | 3 | > Bash-like brace expansion, implemented in JavaScript. Safer than other brace expansion libs, with complete support for the Bash 4.3 braces specification, without sacrificing speed. 4 | 5 | Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support. 6 | 7 | ## Install 8 | 9 | Install with [npm](https://www.npmjs.com/): 10 | 11 | ```sh 12 | $ npm install --save braces 13 | ``` 14 | 15 | ## v3.0.0 Released!! 16 | 17 | See the [changelog](CHANGELOG.md) for details. 18 | 19 | ## Why use braces? 20 | 21 | Brace patterns make globs more powerful by adding the ability to match specific ranges and sequences of characters. 22 | 23 | * **Accurate** - complete support for the [Bash 4.3 Brace Expansion](www.gnu.org/software/bash/) specification (passes all of the Bash braces tests) 24 | * **[fast and performant](#benchmarks)** - Starts fast, runs fast and [scales well](#performance) as patterns increase in complexity. 25 | * **Organized code base** - The parser and compiler are easy to maintain and update when edge cases crop up. 26 | * **Well-tested** - Thousands of test assertions, and passes all of the Bash, minimatch, and [brace-expansion](https://github.com/juliangruber/brace-expansion) unit tests (as of the date this was written). 27 | * **Safer** - You shouldn't have to worry about users defining aggressive or malicious brace patterns that can break your application. Braces takes measures to prevent malicious regex that can be used for DDoS attacks (see [catastrophic backtracking](https://www.regular-expressions.info/catastrophic.html)). 28 | * [Supports lists](#lists) - (aka "sets") `a/{b,c}/d` => `['a/b/d', 'a/c/d']` 29 | * [Supports sequences](#sequences) - (aka "ranges") `{01..03}` => `['01', '02', '03']` 30 | * [Supports steps](#steps) - (aka "increments") `{2..10..2}` => `['2', '4', '6', '8', '10']` 31 | * [Supports escaping](#escaping) - To prevent evaluation of special characters. 32 | 33 | ## Usage 34 | 35 | The main export is a function that takes one or more brace `patterns` and `options`. 36 | 37 | ```js 38 | const braces = require('braces'); 39 | // braces(patterns[, options]); 40 | 41 | console.log(braces(['{01..05}', '{a..e}'])); 42 | //=> ['(0[1-5])', '([a-e])'] 43 | 44 | console.log(braces(['{01..05}', '{a..e}'], { expand: true })); 45 | //=> ['01', '02', '03', '04', '05', 'a', 'b', 'c', 'd', 'e'] 46 | ``` 47 | 48 | ### Brace Expansion vs. Compilation 49 | 50 | By default, brace patterns are compiled into strings that are optimized for creating regular expressions and matching. 51 | 52 | **Compiled** 53 | 54 | ```js 55 | console.log(braces('a/{x,y,z}/b')); 56 | //=> ['a/(x|y|z)/b'] 57 | console.log(braces(['a/{01..20}/b', 'a/{1..5}/b'])); 58 | //=> [ 'a/(0[1-9]|1[0-9]|20)/b', 'a/([1-5])/b' ] 59 | ``` 60 | 61 | **Expanded** 62 | 63 | Enable brace expansion by setting the `expand` option to true, or by using [braces.expand()](#expand) (returns an array similar to what you'd expect from Bash, or `echo {1..5}`, or [minimatch](https://github.com/isaacs/minimatch)): 64 | 65 | ```js 66 | console.log(braces('a/{x,y,z}/b', { expand: true })); 67 | //=> ['a/x/b', 'a/y/b', 'a/z/b'] 68 | 69 | console.log(braces.expand('{01..10}')); 70 | //=> ['01','02','03','04','05','06','07','08','09','10'] 71 | ``` 72 | 73 | ### Lists 74 | 75 | Expand lists (like Bash "sets"): 76 | 77 | ```js 78 | console.log(braces('a/{foo,bar,baz}/*.js')); 79 | //=> ['a/(foo|bar|baz)/*.js'] 80 | 81 | console.log(braces.expand('a/{foo,bar,baz}/*.js')); 82 | //=> ['a/foo/*.js', 'a/bar/*.js', 'a/baz/*.js'] 83 | ``` 84 | 85 | ### Sequences 86 | 87 | Expand ranges of characters (like Bash "sequences"): 88 | 89 | ```js 90 | console.log(braces.expand('{1..3}')); // ['1', '2', '3'] 91 | console.log(braces.expand('a/{1..3}/b')); // ['a/1/b', 'a/2/b', 'a/3/b'] 92 | console.log(braces('{a..c}', { expand: true })); // ['a', 'b', 'c'] 93 | console.log(braces('foo/{a..c}', { expand: true })); // ['foo/a', 'foo/b', 'foo/c'] 94 | 95 | // supports zero-padded ranges 96 | console.log(braces('a/{01..03}/b')); //=> ['a/(0[1-3])/b'] 97 | console.log(braces('a/{001..300}/b')); //=> ['a/(0{2}[1-9]|0[1-9][0-9]|[12][0-9]{2}|300)/b'] 98 | ``` 99 | 100 | See [fill-range](https://github.com/jonschlinkert/fill-range) for all available range-expansion options. 101 | 102 | ### Steppped ranges 103 | 104 | Steps, or increments, may be used with ranges: 105 | 106 | ```js 107 | console.log(braces.expand('{2..10..2}')); 108 | //=> ['2', '4', '6', '8', '10'] 109 | 110 | console.log(braces('{2..10..2}')); 111 | //=> ['(2|4|6|8|10)'] 112 | ``` 113 | 114 | When the [.optimize](#optimize) method is used, or [options.optimize](#optionsoptimize) is set to true, sequences are passed to [to-regex-range](https://github.com/jonschlinkert/to-regex-range) for expansion. 115 | 116 | ### Nesting 117 | 118 | Brace patterns may be nested. The results of each expanded string are not sorted, and left to right order is preserved. 119 | 120 | **"Expanded" braces** 121 | 122 | ```js 123 | console.log(braces.expand('a{b,c,/{x,y}}/e')); 124 | //=> ['ab/e', 'ac/e', 'a/x/e', 'a/y/e'] 125 | 126 | console.log(braces.expand('a/{x,{1..5},y}/c')); 127 | //=> ['a/x/c', 'a/1/c', 'a/2/c', 'a/3/c', 'a/4/c', 'a/5/c', 'a/y/c'] 128 | ``` 129 | 130 | **"Optimized" braces** 131 | 132 | ```js 133 | console.log(braces('a{b,c,/{x,y}}/e')); 134 | //=> ['a(b|c|/(x|y))/e'] 135 | 136 | console.log(braces('a/{x,{1..5},y}/c')); 137 | //=> ['a/(x|([1-5])|y)/c'] 138 | ``` 139 | 140 | ### Escaping 141 | 142 | **Escaping braces** 143 | 144 | A brace pattern will not be expanded or evaluted if _either the opening or closing brace is escaped_: 145 | 146 | ```js 147 | console.log(braces.expand('a\\{d,c,b}e')); 148 | //=> ['a{d,c,b}e'] 149 | 150 | console.log(braces.expand('a{d,c,b\\}e')); 151 | //=> ['a{d,c,b}e'] 152 | ``` 153 | 154 | **Escaping commas** 155 | 156 | Commas inside braces may also be escaped: 157 | 158 | ```js 159 | console.log(braces.expand('a{b\\,c}d')); 160 | //=> ['a{b,c}d'] 161 | 162 | console.log(braces.expand('a{d\\,c,b}e')); 163 | //=> ['ad,ce', 'abe'] 164 | ``` 165 | 166 | **Single items** 167 | 168 | Following bash conventions, a brace pattern is also not expanded when it contains a single character: 169 | 170 | ```js 171 | console.log(braces.expand('a{b}c')); 172 | //=> ['a{b}c'] 173 | ``` 174 | 175 | ## Options 176 | 177 | ### options.maxLength 178 | 179 | **Type**: `Number` 180 | 181 | **Default**: `65,536` 182 | 183 | **Description**: Limit the length of the input string. Useful when the input string is generated or your application allows users to pass a string, et cetera. 184 | 185 | ```js 186 | console.log(braces('a/{b,c}/d', { maxLength: 3 })); //=> throws an error 187 | ``` 188 | 189 | ### options.expand 190 | 191 | **Type**: `Boolean` 192 | 193 | **Default**: `undefined` 194 | 195 | **Description**: Generate an "expanded" brace pattern (alternatively you can use the `braces.expand()` method, which does the same thing). 196 | 197 | ```js 198 | console.log(braces('a/{b,c}/d', { expand: true })); 199 | //=> [ 'a/b/d', 'a/c/d' ] 200 | ``` 201 | 202 | ### options.nodupes 203 | 204 | **Type**: `Boolean` 205 | 206 | **Default**: `undefined` 207 | 208 | **Description**: Remove duplicates from the returned array. 209 | 210 | ### options.rangeLimit 211 | 212 | **Type**: `Number` 213 | 214 | **Default**: `1000` 215 | 216 | **Description**: To prevent malicious patterns from being passed by users, an error is thrown when `braces.expand()` is used or `options.expand` is true and the generated range will exceed the `rangeLimit`. 217 | 218 | You can customize `options.rangeLimit` or set it to `Inifinity` to disable this altogether. 219 | 220 | **Examples** 221 | 222 | ```js 223 | // pattern exceeds the "rangeLimit", so it's optimized automatically 224 | console.log(braces.expand('{1..1000}')); 225 | //=> ['([1-9]|[1-9][0-9]{1,2}|1000)'] 226 | 227 | // pattern does not exceed "rangeLimit", so it's NOT optimized 228 | console.log(braces.expand('{1..100}')); 229 | //=> ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '80', '81', '82', '83', '84', '85', '86', '87', '88', '89', '90', '91', '92', '93', '94', '95', '96', '97', '98', '99', '100'] 230 | ``` 231 | 232 | ### options.transform 233 | 234 | **Type**: `Function` 235 | 236 | **Default**: `undefined` 237 | 238 | **Description**: Customize range expansion. 239 | 240 | **Example: Transforming non-numeric values** 241 | 242 | ```js 243 | const alpha = braces.expand('x/{a..e}/y', { 244 | transform(value, index) { 245 | // When non-numeric values are passed, "value" is a character code. 246 | return 'foo/' + String.fromCharCode(value) + '-' + index; 247 | } 248 | }); 249 | console.log(alpha); 250 | //=> [ 'x/foo/a-0/y', 'x/foo/b-1/y', 'x/foo/c-2/y', 'x/foo/d-3/y', 'x/foo/e-4/y' ] 251 | ``` 252 | 253 | **Example: Transforming numeric values** 254 | 255 | ```js 256 | const numeric = braces.expand('{1..5}', { 257 | transform(value) { 258 | // when numeric values are passed, "value" is a number 259 | return 'foo/' + value * 2; 260 | } 261 | }); 262 | console.log(numeric); 263 | //=> [ 'foo/2', 'foo/4', 'foo/6', 'foo/8', 'foo/10' ] 264 | ``` 265 | 266 | ### options.quantifiers 267 | 268 | **Type**: `Boolean` 269 | 270 | **Default**: `undefined` 271 | 272 | **Description**: In regular expressions, quanitifiers can be used to specify how many times a token can be repeated. For example, `a{1,3}` will match the letter `a` one to three times. 273 | 274 | Unfortunately, regex quantifiers happen to share the same syntax as [Bash lists](#lists) 275 | 276 | The `quantifiers` option tells braces to detect when [regex quantifiers](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#quantifiers) are defined in the given pattern, and not to try to expand them as lists. 277 | 278 | **Examples** 279 | 280 | ```js 281 | const braces = require('braces'); 282 | console.log(braces('a/b{1,3}/{x,y,z}')); 283 | //=> [ 'a/b(1|3)/(x|y|z)' ] 284 | console.log(braces('a/b{1,3}/{x,y,z}', {quantifiers: true})); 285 | //=> [ 'a/b{1,3}/(x|y|z)' ] 286 | console.log(braces('a/b{1,3}/{x,y,z}', {quantifiers: true, expand: true})); 287 | //=> [ 'a/b{1,3}/x', 'a/b{1,3}/y', 'a/b{1,3}/z' ] 288 | ``` 289 | 290 | ### options.unescape 291 | 292 | **Type**: `Boolean` 293 | 294 | **Default**: `undefined` 295 | 296 | **Description**: Strip backslashes that were used for escaping from the result. 297 | 298 | ## What is "brace expansion"? 299 | 300 | Brace expansion is a type of parameter expansion that was made popular by unix shells for generating lists of strings, as well as regex-like matching when used alongside wildcards (globs). 301 | 302 | In addition to "expansion", braces are also used for matching. In other words: 303 | 304 | * [brace expansion](#brace-expansion) is for generating new lists 305 | * [brace matching](#brace-matching) is for filtering existing lists 306 | 307 |
308 | More about brace expansion (click to expand) 309 | 310 | There are two main types of brace expansion: 311 | 312 | 1. **lists**: which are defined using comma-separated values inside curly braces: `{a,b,c}` 313 | 2. **sequences**: which are defined using a starting value and an ending value, separated by two dots: `a{1..3}b`. Optionally, a third argument may be passed to define a "step" or increment to use: `a{1..100..10}b`. These are also sometimes referred to as "ranges". 314 | 315 | Here are some example brace patterns to illustrate how they work: 316 | 317 | **Sets** 318 | 319 | ``` 320 | {a,b,c} => a b c 321 | {a,b,c}{1,2} => a1 a2 b1 b2 c1 c2 322 | ``` 323 | 324 | **Sequences** 325 | 326 | ``` 327 | {1..9} => 1 2 3 4 5 6 7 8 9 328 | {4..-4} => 4 3 2 1 0 -1 -2 -3 -4 329 | {1..20..3} => 1 4 7 10 13 16 19 330 | {a..j} => a b c d e f g h i j 331 | {j..a} => j i h g f e d c b a 332 | {a..z..3} => a d g j m p s v y 333 | ``` 334 | 335 | **Combination** 336 | 337 | Sets and sequences can be mixed together or used along with any other strings. 338 | 339 | ``` 340 | {a,b,c}{1..3} => a1 a2 a3 b1 b2 b3 c1 c2 c3 341 | foo/{a,b,c}/bar => foo/a/bar foo/b/bar foo/c/bar 342 | ``` 343 | 344 | The fact that braces can be "expanded" from relatively simple patterns makes them ideal for quickly generating test fixtures, file paths, and similar use cases. 345 | 346 | ## Brace matching 347 | 348 | In addition to _expansion_, brace patterns are also useful for performing regular-expression-like matching. 349 | 350 | For example, the pattern `foo/{1..3}/bar` would match any of following strings: 351 | 352 | ``` 353 | foo/1/bar 354 | foo/2/bar 355 | foo/3/bar 356 | ``` 357 | 358 | But not: 359 | 360 | ``` 361 | baz/1/qux 362 | baz/2/qux 363 | baz/3/qux 364 | ``` 365 | 366 | Braces can also be combined with [glob patterns](https://github.com/jonschlinkert/micromatch) to perform more advanced wildcard matching. For example, the pattern `*/{1..3}/*` would match any of following strings: 367 | 368 | ``` 369 | foo/1/bar 370 | foo/2/bar 371 | foo/3/bar 372 | baz/1/qux 373 | baz/2/qux 374 | baz/3/qux 375 | ``` 376 | 377 | ## Brace matching pitfalls 378 | 379 | Although brace patterns offer a user-friendly way of matching ranges or sets of strings, there are also some major disadvantages and potential risks you should be aware of. 380 | 381 | ### tldr 382 | 383 | **"brace bombs"** 384 | 385 | * brace expansion can eat up a huge amount of processing resources 386 | * as brace patterns increase _linearly in size_, the system resources required to expand the pattern increase exponentially 387 | * users can accidentally (or intentially) exhaust your system's resources resulting in the equivalent of a DoS attack (bonus: no programming knowledge is required!) 388 | 389 | For a more detailed explanation with examples, see the [geometric complexity](#geometric-complexity) section. 390 | 391 | ### The solution 392 | 393 | Jump to the [performance section](#performance) to see how Braces solves this problem in comparison to other libraries. 394 | 395 | ### Geometric complexity 396 | 397 | At minimum, brace patterns with sets limited to two elements have quadradic or `O(n^2)` complexity. But the complexity of the algorithm increases exponentially as the number of sets, _and elements per set_, increases, which is `O(n^c)`. 398 | 399 | For example, the following sets demonstrate quadratic (`O(n^2)`) complexity: 400 | 401 | ``` 402 | {1,2}{3,4} => (2X2) => 13 14 23 24 403 | {1,2}{3,4}{5,6} => (2X2X2) => 135 136 145 146 235 236 245 246 404 | ``` 405 | 406 | But add an element to a set, and we get a n-fold Cartesian product with `O(n^c)` complexity: 407 | 408 | ``` 409 | {1,2,3}{4,5,6}{7,8,9} => (3X3X3) => 147 148 149 157 158 159 167 168 169 247 248 410 | 249 257 258 259 267 268 269 347 348 349 357 411 | 358 359 367 368 369 412 | ``` 413 | 414 | Now, imagine how this complexity grows given that each element is a n-tuple: 415 | 416 | ``` 417 | {1..100}{1..100} => (100X100) => 10,000 elements (38.4 kB) 418 | {1..100}{1..100}{1..100} => (100X100X100) => 1,000,000 elements (5.76 MB) 419 | ``` 420 | 421 | Although these examples are clearly contrived, they demonstrate how brace patterns can quickly grow out of control. 422 | 423 | **More information** 424 | 425 | Interested in learning more about brace expansion? 426 | 427 | * [linuxjournal/bash-brace-expansion](http://www.linuxjournal.com/content/bash-brace-expansion) 428 | * [rosettacode/Brace_expansion](https://rosettacode.org/wiki/Brace_expansion) 429 | * [cartesian product](https://en.wikipedia.org/wiki/Cartesian_product) 430 | 431 |
432 | 433 | ## Performance 434 | 435 | Braces is not only screaming fast, it's also more accurate the other brace expansion libraries. 436 | 437 | ### Better algorithms 438 | 439 | Fortunately there is a solution to the ["brace bomb" problem](#brace-matching-pitfalls): _don't expand brace patterns into an array when they're used for matching_. 440 | 441 | Instead, convert the pattern into an optimized regular expression. This is easier said than done, and braces is the only library that does this currently. 442 | 443 | **The proof is in the numbers** 444 | 445 | Minimatch gets exponentially slower as patterns increase in complexity, braces does not. The following results were generated using `braces()` and `minimatch.braceExpand()`, respectively. 446 | 447 | | **Pattern** | **braces** | **[minimatch][]** | 448 | | --- | --- | --- | 449 | | `{1..9007199254740991}`[^1] | `298 B` (5ms 459μs)| N/A (freezes) | 450 | | `{1..1000000000000000}` | `41 B` (1ms 15μs) | N/A (freezes) | 451 | | `{1..100000000000000}` | `40 B` (890μs) | N/A (freezes) | 452 | | `{1..10000000000000}` | `39 B` (2ms 49μs) | N/A (freezes) | 453 | | `{1..1000000000000}` | `38 B` (608μs) | N/A (freezes) | 454 | | `{1..100000000000}` | `37 B` (397μs) | N/A (freezes) | 455 | | `{1..10000000000}` | `35 B` (983μs) | N/A (freezes) | 456 | | `{1..1000000000}` | `34 B` (798μs) | N/A (freezes) | 457 | | `{1..100000000}` | `33 B` (733μs) | N/A (freezes) | 458 | | `{1..10000000}` | `32 B` (5ms 632μs) | `78.89 MB` (16s 388ms 569μs) | 459 | | `{1..1000000}` | `31 B` (1ms 381μs) | `6.89 MB` (1s 496ms 887μs) | 460 | | `{1..100000}` | `30 B` (950μs) | `588.89 kB` (146ms 921μs) | 461 | | `{1..10000}` | `29 B` (1ms 114μs) | `48.89 kB` (14ms 187μs) | 462 | | `{1..1000}` | `28 B` (760μs) | `3.89 kB` (1ms 453μs) | 463 | | `{1..100}` | `22 B` (345μs) | `291 B` (196μs) | 464 | | `{1..10}` | `10 B` (533μs) | `20 B` (37μs) | 465 | | `{1..3}` | `7 B` (190μs) | `5 B` (27μs) | 466 | 467 | ### Faster algorithms 468 | 469 | When you need expansion, braces is still much faster. 470 | 471 | _(the following results were generated using `braces.expand()` and `minimatch.braceExpand()`, respectively)_ 472 | 473 | | **Pattern** | **braces** | **[minimatch][]** | 474 | | --- | --- | --- | 475 | | `{1..10000000}` | `78.89 MB` (2s 698ms 642μs) | `78.89 MB` (18s 601ms 974μs) | 476 | | `{1..1000000}` | `6.89 MB` (458ms 576μs) | `6.89 MB` (1s 491ms 621μs) | 477 | | `{1..100000}` | `588.89 kB` (20ms 728μs) | `588.89 kB` (156ms 919μs) | 478 | | `{1..10000}` | `48.89 kB` (2ms 202μs) | `48.89 kB` (13ms 641μs) | 479 | | `{1..1000}` | `3.89 kB` (1ms 796μs) | `3.89 kB` (1ms 958μs) | 480 | | `{1..100}` | `291 B` (424μs) | `291 B` (211μs) | 481 | | `{1..10}` | `20 B` (487μs) | `20 B` (72μs) | 482 | | `{1..3}` | `5 B` (166μs) | `5 B` (27μs) | 483 | 484 | If you'd like to run these comparisons yourself, see [test/support/generate.js](test/support/generate.js). 485 | 486 | ## Benchmarks 487 | 488 | ### Running benchmarks 489 | 490 | Install dev dependencies: 491 | 492 | ```bash 493 | npm i -d && npm benchmark 494 | ``` 495 | 496 | ### Latest results 497 | 498 | Braces is more accurate, without sacrificing performance. 499 | 500 | ```bash 501 | # range (expanded) 502 | braces x 29,040 ops/sec ±3.69% (91 runs sampled)) 503 | minimatch x 4,735 ops/sec ±1.28% (90 runs sampled) 504 | 505 | # range (optimized for regex) 506 | braces x 382,878 ops/sec ±0.56% (94 runs sampled) 507 | minimatch x 1,040 ops/sec ±0.44% (93 runs sampled) 508 | 509 | # nested ranges (expanded) 510 | braces x 19,744 ops/sec ±2.27% (92 runs sampled)) 511 | minimatch x 4,579 ops/sec ±0.50% (93 runs sampled) 512 | 513 | # nested ranges (optimized for regex) 514 | braces x 246,019 ops/sec ±2.02% (93 runs sampled) 515 | minimatch x 1,028 ops/sec ±0.39% (94 runs sampled) 516 | 517 | # set (expanded) 518 | braces x 138,641 ops/sec ±0.53% (95 runs sampled) 519 | minimatch x 219,582 ops/sec ±0.98% (94 runs sampled) 520 | 521 | # set (optimized for regex) 522 | braces x 388,408 ops/sec ±0.41% (95 runs sampled) 523 | minimatch x 44,724 ops/sec ±0.91% (89 runs sampled) 524 | 525 | # nested sets (expanded) 526 | braces x 84,966 ops/sec ±0.48% (94 runs sampled) 527 | minimatch x 140,720 ops/sec ±0.37% (95 runs sampled) 528 | 529 | # nested sets (optimized for regex) 530 | braces x 263,340 ops/sec ±2.06% (92 runs sampled) 531 | minimatch x 28,714 ops/sec ±0.40% (90 runs sampled) 532 | ``` 533 | 534 | ## About 535 | 536 |
537 | Contributing 538 | 539 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). 540 | 541 |
542 | 543 |
544 | Running Tests 545 | 546 | Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command: 547 | 548 | ```sh 549 | $ npm install && npm test 550 | ``` 551 | 552 |
553 | 554 |
555 | Building docs 556 | 557 | _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_ 558 | 559 | To generate the readme, run the following command: 560 | 561 | ```sh 562 | $ npm install -g verbose/verb#dev verb-generate-readme && verb 563 | ``` 564 | 565 |
566 | 567 | ### Contributors 568 | 569 | | **Commits** | **Contributor** | 570 | | --- | --- | 571 | | 197 | [jonschlinkert](https://github.com/jonschlinkert) | 572 | | 4 | [doowb](https://github.com/doowb) | 573 | | 1 | [es128](https://github.com/es128) | 574 | | 1 | [eush77](https://github.com/eush77) | 575 | | 1 | [hemanth](https://github.com/hemanth) | 576 | | 1 | [wtgtybhertgeghgtwtg](https://github.com/wtgtybhertgeghgtwtg) | 577 | 578 | ### Author 579 | 580 | **Jon Schlinkert** 581 | 582 | * [GitHub Profile](https://github.com/jonschlinkert) 583 | * [Twitter Profile](https://twitter.com/jonschlinkert) 584 | * [LinkedIn Profile](https://linkedin.com/in/jonschlinkert) 585 | 586 | ### License 587 | 588 | Copyright © 2019, [Jon Schlinkert](https://github.com/jonschlinkert). 589 | Released under the [MIT License](LICENSE). 590 | 591 | *** 592 | 593 | _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.8.0, on April 08, 2019._ -------------------------------------------------------------------------------- /lecture/lecture20/code/Lecture04/.ipynb_checkpoints/goog-unwanted-proto.metadata: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wo315/2206CFD_OpenFOAM/c0be4f9a1eb0e08b621b6b3719dbdf8092d72f1e/lecture/lecture20/code/Lecture04/.ipynb_checkpoints/goog-unwanted-proto.metadata -------------------------------------------------------------------------------- /lecture/lecture20/code/Lecture04/.ipynb_checkpoints/goog-unwanted-proto.vlpset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wo315/2206CFD_OpenFOAM/c0be4f9a1eb0e08b621b6b3719dbdf8092d72f1e/lecture/lecture20/code/Lecture04/.ipynb_checkpoints/goog-unwanted-proto.vlpset -------------------------------------------------------------------------------- /lecture/lecture20/matrixcookbook.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wo315/2206CFD_OpenFOAM/c0be4f9a1eb0e08b621b6b3719dbdf8092d72f1e/lecture/lecture20/matrixcookbook.pdf -------------------------------------------------------------------------------- /lecture/lecture20/vmls.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wo315/2206CFD_OpenFOAM/c0be4f9a1eb0e08b621b6b3719dbdf8092d72f1e/lecture/lecture20/vmls.pdf --------------------------------------------------------------------------------