diff --git a/60_linear_algebra_2/100_Systems_of_Linear_Equations.ipynb b/60_linear_algebra_2/100_Systems_of_Linear_Equations.ipynb
index 1b8acc52..698db4d6 100644
--- a/60_linear_algebra_2/100_Systems_of_Linear_Equations.ipynb
+++ b/60_linear_algebra_2/100_Systems_of_Linear_Equations.ipynb
@@ -16,6 +16,7 @@
"metadata": {},
"outputs": [],
"source": [
+ "import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import numpy.random as nr\n",
"import sympy as sy\n",
@@ -157,6 +158,40 @@
"\n"
]
},
+ {
+ "cell_type": "code",
+ "source": [
+ "def hinton(matrix, max_weight=None, ax=None):\n",
+ " '''\n",
+ " Draw Hinton diagram for visualizing a weight matrix.\n",
+ " https://matplotlib.org/stable/gallery/specialty_plots/hinton_demo.html\n",
+ " '''\n",
+ " ax = ax if ax is not None else plt.gca()\n",
+ "\n",
+ " if not max_weight:\n",
+ " max_weight = 2 ** np.ceil(np.log2(np.abs(matrix).max()))\n",
+ "\n",
+ " ax.patch.set_facecolor('gray')\n",
+ " ax.set_aspect('equal', 'box')\n",
+ "\n",
+ " ax.xaxis.set_major_locator(plt.NullLocator())\n",
+ " ax.yaxis.set_major_locator(plt.NullLocator())\n",
+ "\n",
+ " for (y, x), w in np.ndenumerate(matrix):\n",
+ " color = 'white' if w > 0 else 'black'\n",
+ " size = np.sqrt(abs(w) / max_weight)\n",
+ " rect = plt.Rectangle([x - size / 2, y - size / 2], size, size,\n",
+ " facecolor=color, edgecolor=color)\n",
+ " ax.add_patch(rect)\n",
+ "\n",
+ " ax.autoscale_view()\n",
+ " ax.invert_yaxis()\n",
+ "\n"
+ ],
+ "metadata": {},
+ "execution_count": null,
+ "outputs": []
+ },
{
"cell_type": "markdown",
"metadata": {},
@@ -216,6 +251,16 @@
"\n"
]
},
+ {
+ "cell_type": "code",
+ "source": [
+ "hinton(Ab)\n",
+ "\n"
+ ],
+ "metadata": {},
+ "execution_count": null,
+ "outputs": []
+ },
{
"cell_type": "markdown",
"metadata": {},
@@ -278,6 +323,16 @@
"\n"
]
},
+ {
+ "cell_type": "code",
+ "source": [
+ "hinton(Ab)\n",
+ "\n"
+ ],
+ "metadata": {},
+ "execution_count": null,
+ "outputs": []
+ },
{
"cell_type": "markdown",
"metadata": {},
@@ -359,6 +414,16 @@
"\n"
]
},
+ {
+ "cell_type": "code",
+ "source": [
+ "hinton(Ab)\n",
+ "\n"
+ ],
+ "metadata": {},
+ "execution_count": null,
+ "outputs": []
+ },
{
"cell_type": "markdown",
"metadata": {},
@@ -435,7 +500,7 @@
"metadata": {},
"outputs": [],
"source": [
- "sol = sy.Matrix([None] * n)\n",
+ "sol = sy.Matrix([sy.Symbol('None')] * n)\n",
"\n"
]
},
@@ -773,6 +838,16 @@
"\n"
]
},
+ {
+ "cell_type": "code",
+ "source": [
+ "hinton(Ab_list)\n",
+ "\n"
+ ],
+ "metadata": {},
+ "execution_count": null,
+ "outputs": []
+ },
{
"cell_type": "code",
"execution_count": null,
@@ -794,6 +869,16 @@
"\n"
]
},
+ {
+ "cell_type": "code",
+ "source": [
+ "hinton(Ab_list)\n",
+ "\n"
+ ],
+ "metadata": {},
+ "execution_count": null,
+ "outputs": []
+ },
{
"cell_type": "markdown",
"metadata": {},
@@ -918,8 +1003,12 @@
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.8"
+ },
+ "colab": {
+ "provenance": [],
+ "include_colab_link": true
}
},
"nbformat": 4,
- "nbformat_minor": 2
+ "nbformat_minor": 0
}
\ No newline at end of file
diff --git a/60_linear_algebra_2/150_Inverse_matrix.ipynb b/60_linear_algebra_2/150_Inverse_matrix.ipynb
index d3730973..f2235474 100644
--- a/60_linear_algebra_2/150_Inverse_matrix.ipynb
+++ b/60_linear_algebra_2/150_Inverse_matrix.ipynb
@@ -40,6 +40,7 @@
"metadata": {},
"outputs": [],
"source": [
+ "import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import numpy.linalg as nl\n",
"import numpy.random as nr\n",
@@ -50,6 +51,51 @@
"\n"
]
},
+ {
+ "cell_type": "markdown",
+ "source": [
+ "The following function would visualize a numerical matrix using the [Hinton Diagram](https://matplotlib.org/stable/gallery/specialty_plots/hinton_demo.html).
\n",
+ "아래 함수는 행렬을 [힌튼 다이어그램](https://matplotlib.org/stable/gallery/specialty_plots/hinton_demo.html)으로 시각화할 것이다.\n"
+ ],
+ "metadata": {}
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "def hinton(matrix, max_weight=None, ax=None):\n",
+ " '''\n",
+ " Draw Hinton diagram for visualizing a weight matrix.\n",
+ " https://matplotlib.org/stable/gallery/specialty_plots/hinton_demo.html\n",
+ " '''\n",
+ " ax = ax if ax is not None else plt.gca()\n",
+ "\n",
+ " if not max_weight:\n",
+ " max_weight = 2 ** np.ceil(np.log2(np.abs(matrix).max()))\n",
+ "\n",
+ " ax.patch.set_facecolor('gray')\n",
+ " ax.set_aspect('equal', 'box')\n",
+ "\n",
+ " ax.xaxis.set_major_locator(plt.NullLocator())\n",
+ " ax.yaxis.set_major_locator(plt.NullLocator())\n",
+ "\n",
+ " for (y, x), w in np.ndenumerate(matrix):\n",
+ " color = 'white' if w > 0 else 'black'\n",
+ " size = np.sqrt(abs(w) / max_weight)\n",
+ " rect = plt.Rectangle([x - size / 2, y - size / 2], size, size,\n",
+ " facecolor=color, edgecolor=color)\n",
+ " ax.add_patch(rect)\n",
+ "\n",
+ " ax.autoscale_view()\n",
+ " ax.invert_yaxis()\n",
+ "\n",
+ " plt.show()\n",
+ " plt.close()\n",
+ "\n"
+ ],
+ "metadata": {},
+ "execution_count": null,
+ "outputs": []
+ },
{
"cell_type": "code",
"execution_count": null,
@@ -632,6 +678,16 @@
"\n"
]
},
+ {
+ "cell_type": "code",
+ "source": [
+ "hinton(A22)\n",
+ "\n"
+ ],
+ "metadata": {},
+ "execution_count": null,
+ "outputs": []
+ },
{
"cell_type": "markdown",
"metadata": {},
@@ -681,6 +737,16 @@
"\n"
]
},
+ {
+ "cell_type": "code",
+ "source": [
+ "hinton(AX22)\n",
+ "\n"
+ ],
+ "metadata": {},
+ "execution_count": null,
+ "outputs": []
+ },
{
"cell_type": "markdown",
"metadata": {},
@@ -721,6 +787,16 @@
"\n"
]
},
+ {
+ "cell_type": "code",
+ "source": [
+ "hinton(AX22)\n",
+ "\n"
+ ],
+ "metadata": {},
+ "execution_count": null,
+ "outputs": []
+ },
{
"cell_type": "markdown",
"metadata": {},
@@ -752,6 +828,16 @@
"\n"
]
},
+ {
+ "cell_type": "code",
+ "source": [
+ "hinton(AX22)\n",
+ "\n"
+ ],
+ "metadata": {},
+ "execution_count": null,
+ "outputs": []
+ },
{
"cell_type": "markdown",
"metadata": {},
@@ -781,6 +867,16 @@
"\n"
]
},
+ {
+ "cell_type": "code",
+ "source": [
+ "hinton(A22_inv)\n",
+ "\n"
+ ],
+ "metadata": {},
+ "execution_count": null,
+ "outputs": []
+ },
{
"cell_type": "markdown",
"metadata": {},
@@ -837,6 +933,16 @@
"\n"
]
},
+ {
+ "cell_type": "code",
+ "source": [
+ "hinton(mat_A22_inv)\n",
+ "\n"
+ ],
+ "metadata": {},
+ "execution_count": null,
+ "outputs": []
+ },
{
"cell_type": "code",
"execution_count": null,
@@ -937,6 +1043,16 @@
"\n"
]
},
+ {
+ "cell_type": "code",
+ "source": [
+ "hinton(A33_list)\n",
+ "\n"
+ ],
+ "metadata": {},
+ "execution_count": null,
+ "outputs": []
+ },
{
"cell_type": "code",
"execution_count": null,
@@ -957,6 +1073,16 @@
"\n"
]
},
+ {
+ "cell_type": "code",
+ "source": [
+ "hinton(A33)\n",
+ "\n"
+ ],
+ "metadata": {},
+ "execution_count": null,
+ "outputs": []
+ },
{
"cell_type": "markdown",
"metadata": {},
@@ -1006,6 +1132,16 @@
"\n"
]
},
+ {
+ "cell_type": "code",
+ "source": [
+ "hinton(AX33)\n",
+ "\n"
+ ],
+ "metadata": {},
+ "execution_count": null,
+ "outputs": []
+ },
{
"cell_type": "markdown",
"metadata": {},
@@ -1046,6 +1182,16 @@
"\n"
]
},
+ {
+ "cell_type": "code",
+ "source": [
+ "hinton(AX33)\n",
+ "\n"
+ ],
+ "metadata": {},
+ "execution_count": null,
+ "outputs": []
+ },
{
"cell_type": "markdown",
"metadata": {},
@@ -1077,6 +1223,16 @@
"\n"
]
},
+ {
+ "cell_type": "code",
+ "source": [
+ "hinton(AX33)\n",
+ "\n"
+ ],
+ "metadata": {},
+ "execution_count": null,
+ "outputs": []
+ },
{
"cell_type": "markdown",
"metadata": {},
@@ -1108,6 +1264,16 @@
"\n"
]
},
+ {
+ "cell_type": "code",
+ "source": [
+ "hinton(AX33)\n",
+ "\n"
+ ],
+ "metadata": {},
+ "execution_count": null,
+ "outputs": []
+ },
{
"cell_type": "markdown",
"metadata": {},
@@ -1139,6 +1305,16 @@
"\n"
]
},
+ {
+ "cell_type": "code",
+ "source": [
+ "hinton(AX33)\n",
+ "\n"
+ ],
+ "metadata": {},
+ "execution_count": null,
+ "outputs": []
+ },
{
"cell_type": "markdown",
"metadata": {},
@@ -1168,6 +1344,16 @@
"\n"
]
},
+ {
+ "cell_type": "code",
+ "source": [
+ "hinton(A33_inv)\n",
+ "\n"
+ ],
+ "metadata": {},
+ "execution_count": null,
+ "outputs": []
+ },
{
"cell_type": "markdown",
"metadata": {},
@@ -1283,12 +1469,13 @@
" return [row_A + row_B for row_A, row_B in zip(A, B)]\n",
"\n",
"\n",
- "def gauss_jordan(A:Matrix) -> Matrix:\n",
+ "def gauss_jordan(A:Matrix, b_hinton:bool=False, epsilon=1e-7) -> Matrix:\n",
" AX = augment_mats(A, get_identity(len(A)))\n",
- " \n",
+ "\n",
" # pivot loop\n",
" for p in range(len(AX)):\n",
- " \n",
+ "\n",
+ " assert abs(AX[p][p]) > epsilon, (p, AX)\n",
" one_over_pivot = 1.0 / AX[p][p]\n",
"\n",
" # normalize a row with one_over_pivot\n",
@@ -1305,6 +1492,9 @@
" for j in range(0, len(AX[p])):\n",
" AX[i][j] += multiplier * AX[p][j]\n",
"\n",
+ " # visualize augmented matrix \n",
+ " if b_hinton: hinton(AX)\n",
+ "\n",
" return [row[len(A):] for row in AX]\n",
"\n"
]
@@ -1324,7 +1514,7 @@
"metadata": {},
"outputs": [],
"source": [
- "mat_A33_inv_GJ = gauss_jordan(A33_list)\n",
+ "mat_A33_inv_GJ = gauss_jordan(A33_list, b_hinton=True)\n",
"\n"
]
},
@@ -1387,7 +1577,7 @@
"metadata": {},
"outputs": [],
"source": [
- "A44 = np.array(A44_list)\n",
+ "A44 = np.array(A44_list, dtype=float)\n",
"\n"
]
},
@@ -1401,6 +1591,16 @@
"\n"
]
},
+ {
+ "cell_type": "code",
+ "source": [
+ "hinton(A44)\n",
+ "\n"
+ ],
+ "metadata": {},
+ "execution_count": null,
+ "outputs": []
+ },
{
"cell_type": "markdown",
"metadata": {},
@@ -1435,7 +1635,7 @@
"metadata": {},
"outputs": [],
"source": [
- "A44_inv_array = gj.inv(A44)\n",
+ "A44_inv_array = gauss_jordan(A44.tolist(), b_hinton=True)\n",
"\n"
]
},
@@ -1500,16 +1700,45 @@
"\n"
]
},
+ {
+ "cell_type": "code",
+ "source": [
+ "hinton(Ann)\n",
+ "\n"
+ ],
+ "metadata": {},
+ "execution_count": null,
+ "outputs": []
+ },
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
- "%time inv1 = gj.inv(Ann, b_verbose=False)\n",
+ "inv1 = gauss_jordan(Ann.tolist(), b_hinton=True)\n",
"\n"
]
},
+ {
+ "cell_type": "code",
+ "source": [
+ "%time inv1 = gauss_jordan(Ann.tolist())\n",
+ "\n"
+ ],
+ "metadata": {},
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Let's compare the computation time with the cofactor matrix algorithm.
\n",
+ "여인수 행렬 알고리듬과 계산 시간을 비교해 보자.\n",
+ "\n"
+ ],
+ "metadata": {}
+ },
{
"cell_type": "code",
"execution_count": null,
@@ -1751,8 +1980,12 @@
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.3"
+ },
+ "colab": {
+ "provenance": [],
+ "include_colab_link": true
}
},
"nbformat": 4,
- "nbformat_minor": 2
+ "nbformat_minor": 0
}
\ No newline at end of file
diff --git a/60_linear_algebra_2/200_Eigenvalues_of_a_Matrix_PowerMethod.ipynb b/60_linear_algebra_2/200_Eigenvalues_of_a_Matrix_PowerMethod.ipynb
index fe0cb676..b2c5b1d2 100644
--- a/60_linear_algebra_2/200_Eigenvalues_of_a_Matrix_PowerMethod.ipynb
+++ b/60_linear_algebra_2/200_Eigenvalues_of_a_Matrix_PowerMethod.ipynb
@@ -43,6 +43,7 @@
"metadata": {},
"outputs": [],
"source": [
+ "import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import numpy.random as nr\n",
"import numpy.linalg as nl\n",
@@ -379,6 +380,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
+ "\n",
"### `vecY` 시각화
Visualization of `vecY`\n",
"\n"
]
@@ -394,10 +396,22 @@
" vec_x = vec_array[:, 0]\n",
" vec_y = vec_array[:, 1]\n",
"\n",
- " import matplotlib.pyplot as plt\n",
+ " plt.plot(vec_x[0], vec_y[0], 'r.', label='initial')\n",
+ " plt.plot(vec_x[-1], vec_y[-1], 'x', label='final')\n",
+ "\n",
+ " head_length = 0.1\n",
+ "\n",
+ " for xi, yi in zip(vec_x, vec_y):\n",
+ " s = (xi**2 + yi**2) ** 0.5\n",
+ "\n",
+ " r = (s - head_length) / s\n",
"\n",
- " plt.plot(vec_x, vec_y, '.')\n",
+ " plt.arrow(0, 0, xi*r, yi*r, head_width=0.05, head_length=0.1, fc='k', ec='k')\n",
+ "\n",
+ " plt.ylim(0, max(vec_y)*1.1)\n",
" plt.grid(True)\n",
+ " plt.legend(loc=0)\n",
+ "\n",
" plt.savefig('vec_points.png', dpi=300)\n",
"\n"
]
@@ -507,8 +521,12 @@
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
+ },
+ "colab": {
+ "provenance": [],
+ "include_colab_link": true
}
},
"nbformat": 4,
- "nbformat_minor": 2
+ "nbformat_minor": 0
}
\ No newline at end of file
diff --git a/60_linear_algebra_2/240_Eigenvalue_Jacobi_Method_numpy.ipynb b/60_linear_algebra_2/240_Eigenvalue_Jacobi_Method_numpy.ipynb
index cf499218..dfb59aa4 100644
--- a/60_linear_algebra_2/240_Eigenvalue_Jacobi_Method_numpy.ipynb
+++ b/60_linear_algebra_2/240_Eigenvalue_Jacobi_Method_numpy.ipynb
@@ -43,6 +43,7 @@
"import os\n",
"from typing import List, Tuple, Union\n",
"\n",
+ "import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import numpy.random as nr\n",
"import numpy.testing as nt\n",
@@ -64,6 +65,57 @@
"\n"
]
},
+ {
+ "cell_type": "markdown",
+ "source": [
+ "The following function would visualize a numerical matrix using the [Hinton Diagram](https://matplotlib.org/stable/gallery/specialty_plots/hinton_demo.html).
\n",
+ "아래 함수는 행렬을 [힌튼 다이어그램](https://matplotlib.org/stable/gallery/specialty_plots/hinton_demo.html)으로 시각화할 것이다.\n",
+ "\n"
+ ],
+ "metadata": {}
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "def hinton(matrix, max_weight=None, ax=None):\n",
+ " '''\n",
+ " Draw Hinton diagram for visualizing a weight matrix.\n",
+ " https://matplotlib.org/stable/gallery/specialty_plots/hinton_demo.html\n",
+ " '''\n",
+ " if ax is None:\n",
+ " b_ax_none = True\n",
+ " ax = plt.gca()\n",
+ " else:\n",
+ " b_ax_none = False\n",
+ "\n",
+ " if not max_weight:\n",
+ " max_weight = 2 ** np.ceil(np.log2(np.abs(matrix).max()))\n",
+ "\n",
+ " ax.patch.set_facecolor('gray')\n",
+ " ax.set_aspect('equal', 'box')\n",
+ "\n",
+ " ax.xaxis.set_major_locator(plt.NullLocator())\n",
+ " ax.yaxis.set_major_locator(plt.NullLocator())\n",
+ "\n",
+ " for (y, x), w in np.ndenumerate(matrix):\n",
+ " color = 'white' if w > 0 else 'black'\n",
+ " size = np.sqrt(abs(w) / max_weight)\n",
+ " rect = plt.Rectangle([x - size / 2, y - size / 2], size, size,\n",
+ " facecolor=color, edgecolor=color)\n",
+ " ax.add_patch(rect)\n",
+ "\n",
+ " ax.autoscale_view()\n",
+ " ax.invert_yaxis()\n",
+ "\n",
+ " if b_ax_none:\n",
+ " plt.show()\n",
+ " plt.close()\n",
+ "\n"
+ ],
+ "metadata": {},
+ "execution_count": null,
+ "outputs": []
+ },
{
"cell_type": "markdown",
"metadata": {},
@@ -151,6 +203,16 @@
"\n"
]
},
+ {
+ "cell_type": "code",
+ "source": [
+ "hinton(matA)\n",
+ "\n"
+ ],
+ "metadata": {},
+ "execution_count": null,
+ "outputs": []
+ },
{
"cell_type": "markdown",
"metadata": {},
@@ -197,6 +259,16 @@
"\n"
]
},
+ {
+ "cell_type": "code",
+ "source": [
+ "hinton(w)\n",
+ "\n"
+ ],
+ "metadata": {},
+ "execution_count": null,
+ "outputs": []
+ },
{
"cell_type": "markdown",
"metadata": {},
@@ -215,6 +287,16 @@
"\n"
]
},
+ {
+ "cell_type": "code",
+ "source": [
+ "hinton(v)\n",
+ "\n"
+ ],
+ "metadata": {},
+ "execution_count": null,
+ "outputs": []
+ },
{
"cell_type": "markdown",
"metadata": {},
@@ -269,6 +351,16 @@
"\n"
]
},
+ {
+ "cell_type": "code",
+ "source": [
+ "hinton(matLambda)\n",
+ "\n"
+ ],
+ "metadata": {},
+ "execution_count": null,
+ "outputs": []
+ },
{
"cell_type": "markdown",
"metadata": {},
@@ -345,6 +437,16 @@
"\n"
]
},
+ {
+ "cell_type": "code",
+ "source": [
+ "hinton(mat_test_A)\n",
+ "\n"
+ ],
+ "metadata": {},
+ "execution_count": null,
+ "outputs": []
+ },
{
"cell_type": "code",
"execution_count": null,
@@ -385,6 +487,29 @@
"\n"
]
},
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Let's compare two matrices side by side.
\n",
+ "두 행렬을 옆에 나란이 놓고 비교해 보자.\n",
+ "\n"
+ ],
+ "metadata": {}
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "ax1 = plt.subplot(1, 2, 1)\n",
+ "hinton(np.array(result_test_v).T, ax=ax1)\n",
+ "\n",
+ "ax2 = plt.subplot(1, 2, 2)\n",
+ "hinton(expected_v_transpose, ax=ax2)\n",
+ "\n"
+ ],
+ "metadata": {},
+ "execution_count": null,
+ "outputs": []
+ },
{
"cell_type": "code",
"execution_count": null,
@@ -447,8 +572,12 @@
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
+ },
+ "colab": {
+ "provenance": [],
+ "include_colab_link": true
}
},
"nbformat": 4,
- "nbformat_minor": 4
+ "nbformat_minor": 0
}
\ No newline at end of file