From 69c0aee9cc4e0a7d6b7f425d6e0106ffab5804e4 Mon Sep 17 00:00:00 2001 From: sschuma Date: Fri, 13 Apr 2018 17:24:49 +0200 Subject: [PATCH] polish ex2 --- AM18_ex2.ipynb | 162 ++++++++++++++++++++++++++++++------------------- 1 file changed, 99 insertions(+), 63 deletions(-) diff --git a/AM18_ex2.ipynb b/AM18_ex2.ipynb index fedca96..544c2ee 100644 --- a/AM18_ex2.ipynb +++ b/AM18_ex2.ipynb @@ -6,17 +6,27 @@ "source": [ "# Example 2: active rotations in $\\unicode{0x211D}^3$\n", "\n", - "\n", - "$$\\vec{r}'={\\cal{R}}\\vec{r}$$" + "In this example we study simple rotations of vectors from $\\unicode{0x211D}^3$, i.e.\n", + "$$\\vec{r}'={\\cal{R}}\\vec{r}\\,.$$ To this end, we define functions for the multiplication \n", + "of vectors with $3\\times 3$ matrices, as well as the product of two such matrices. \n", + "Furthermore, we implement the calculation of the determinant of a $3\\times 3$ matrix, \n", + "to be able to check the corresponding condition for orthogonal rotation matrices, i.e.\n", + "$\\det({\\cal{R}})=+1$." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### definition of matrices and vectors for diplay purposes only (can be ignored)" ] }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 169, "metadata": {}, "outputs": [], "source": [ - "## The functions in this cell are only for displaying Matrices and Vectors in Latex-style\n", "from IPython.display import Markdown, Latex, Math\n", "\n", "def bmatrix(a):\n", @@ -47,9 +57,16 @@ "\n" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### definition of products of a matrix with another matrix or a vector, as well as the matrix determinant" + ] + }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 170, "metadata": { "scrolled": true }, @@ -62,7 +79,7 @@ "from numpy import cos, sin \n", "import math\n", "\n", - "##definition of the matix-vector multiplication\n", + "## definition of the matix-vector multiplication\n", "def MatrixVectorProduct( mat, vecpre):\n", " \"\"\"Returns the result of a matrix-vector Multiplication\n", " \n", @@ -78,7 +95,7 @@ " print('Error: Wrong dimensions')\n", " return vecnew\n", "\n", - "##definition of the matix product\n", + "## definition of the matix product\n", "def MatrixProduct( mat1, mat2):\n", " \"\"\"Returns the result of a matrix Multiplication\n", " \n", @@ -91,7 +108,7 @@ " matnew[i][j]+=mat1[i][k]*mat2[k][j] \n", " return matnew\n", "\n", - "##definition of the matix determinant\n", + "## definition of the matix determinant\n", "def Matrix3Determinant(mat3,column):\n", " \"\"\"Returns the determinant of a 3x3 Matrix\n", " \n", @@ -115,117 +132,129 @@ " return det\n" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Example calculation of a matrix determinant" + ] + }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 171, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "\n", - "# Calculation of Determinant\n", + "\n", "$$A=\\begin{vmatrix}\n", " 1 & 2 & 3\\\\\n", " 3 & 2 & 1\\\\\n", " 1 & 4 & 2\\\\\n", - "\\end{vmatrix}$$\n", - "\n", - "$$\\vert A \\vert=20$$\n" + "\\end{vmatrix}\\,,\\quad\\det(A)=20$$" ], "text/plain": [ "" ] }, - "execution_count": 3, + "execution_count": 171, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "# Example Determinant Calculation\n", - "\n", + "# input of matrix to compute the determinant for\n", "A = [[1,2,3],[3,2,1],[1,4,2]]\n", "det=Matrix3Determinant(A,2)\n", "\n", "Markdown(\"\"\"\n", - "# Calculation of Determinant\n", - "$$A={x}$$\n", "\n", - "$$\\\\vert A \\\\vert={det}$$\n", - "\"\"\".format(x=bmatrix(np.matrix(A)), det=det))\n" + "$$A={x}\\\\,,\\\\quad\\\\det(A)={det}$$\"\"\".format(x=bmatrix(np.matrix(A)), det=det))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "# Input: $3\\times 3$ rotation, angles and vector from $\\unicode{0x211D}^3$" + "### Rotations in $\\unicode{0x211D}^3$: definition of rotation angles and input vector" ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 172, "metadata": {}, "outputs": [], "source": [ "####################################################################################################################\n", "\n", - "## definition of rotation angles\n", + "## input rotation angles here\n", "phix = np.pi/8\n", "phiy = 0.1\n", - "phiz = 0.3\n", + "phiz = 0.5\n", + "\n", + "## input of vector to be rotated\n", + "vr = [1,1,1]\n", "\n", "## definition of Rotation Matrices\n", "Rx = [[1,0,0],[0,cos(phix),-sin(phix)],[0,sin(phix),cos(phix)]]\n", "Ry = [[cos(phiy),0,sin(phiy)],[0,1,0],[-sin(phiy),0,cos(phiy)]]\n", "Rz = [[cos(phiz),-sin(phiz),0],[sin(phiz),cos(phiz),0],[0,0,1]]\n", "\n", - "## definition of vector\n", - "r = [1,1,0]\n", + "## corresponding determinants \n", + "detRx = Matrix3Determinant(Rx,0)\n", + "detRy = Matrix3Determinant(Ry,0)\n", + "detRz = Matrix3Determinant(Rz,0)\n", "\n", "####################################################################################################################\n", "\n", "##calculation of matrix-vector product\n", - "## in thie example: rotation of r around z-Axis with angle phiz\n", - "rp=MatrixVectorProduct(Rz,r)\n", + "## in the example: rotation of r around z-Axis by angle phiz\n", + "vrp=MatrixVectorProduct(Rz,vr)\n", "\n", - "## resulting rotation matrices of combination of two rotations\n", + "## resulting rotation matrices for combination of two rotations\n", "Rxy=MatrixProduct(Rx,Ry)\n", "Ryx=MatrixProduct(Ry,Rx)\n" ] }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 173, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "\n", - "# Plotting the Results\n", + "## Illustrating the Results\n", "## Rotation Matrices\n", "$$R_x=\\begin{vmatrix}\n", " 1. & 0. & 0.\\\\\n", " 0. & 0.92 & -0.38\\\\\n", " 0. & 0.38 & 0.92\\\\\n", - "\\end{vmatrix}$$\n", + "\\end{vmatrix}\\,,\\quad \\det(R_x)=1.0$$\n", "$$R_y=\\begin{vmatrix}\n", " 1. & 0. & 0.1\\\\\n", " 0. & 1. & 0.\\\\\n", " -0.1 & 0. & 1.\\\\\n", - "\\end{vmatrix}$$\n", + "\\end{vmatrix}\\,,\\quad \\det(R_y)=1.0$$\n", "$$R_z=\\begin{vmatrix}\n", - " 0.96 & -0.3 & 0.\\\\\n", - " 0.3 & 0.96 & 0.\\\\\n", + " 0.88 & -0.48 & 0.\\\\\n", + " 0.48 & 0.88 & 0.\\\\\n", " 0. & 0. & 1.\\\\\n", - "\\end{vmatrix}$$\n", + "\\end{vmatrix}\\,,\\quad \\det(R_z)=1.0$$\n", + "\n", + "## Vector to be rotated\n", + "$$\\vec r=\\begin{pmatrix}\n", + "1 \\\\ 1 \\\\ 1\n", + "\\end{pmatrix}$$\n", "\n", - "## Rotating Vector\n", - "$$r=\\begin{pmatrix}\n", - "1 \\\\ 1 \\\\ 0\n", + "## Rotated Vector\n", + "$$\\vec r'=R_z\\vec r=\\begin{pmatrix}\n", + "0.39815702328616975 \\\\ 1.3570081004945758 \\\\ 1\n", "\\end{pmatrix}$$\n", + "\n", "## Matrix Products \n", "\n", "$$R_{xy}=\\begin{vmatrix}\n", @@ -246,34 +275,38 @@ "" ] }, - "execution_count": 5, + "execution_count": 173, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Markdown(\"\"\"\n", - "# Plotting the Results\n", + "## Illustrating the Results\n", "## Rotation Matrices\n", - "$$R_x={x}$$\n", - "$$R_y={y}$$\n", - "$$R_z={z}$$\n", + "$$R_x={x}\\\\,,\\\\quad \\\\det(R_x)={detRx}$$\n", + "$$R_y={y}\\\\,,\\\\quad \\\\det(R_y)={detRy}$$\n", + "$$R_z={z}\\\\,,\\\\quad \\\\det(R_z)={detRz}$$\n", + "\n", + "## Vector to be rotated\n", + "$$\\\\vec r={vr}$$\n", + "\n", + "## Rotated Vector\n", + "$$\\\\vec r'=R_z\\\\vec r={vrp}$$\n", "\n", - "## Rotating Vector\n", - "$$r={r}$$\n", "## Matrix Products \n", "\n", "$$R_{{xy}}={xy}$$\n", "\n", "$$R_{{yx}}={yx}$$\n", "\n", - "$$\\Rightarrow R_{{xy}} \\\\neq R_{{yx}}$$\"\"\".format(r=lvector(r),xy=bmatrix(np.around(Rxy,decimals=2)),yx=bmatrix(np.around(Ryx,decimals=2)),x=bmatrix(np.around(Rx,decimals=2)), y=bmatrix(np.around(Ry,decimals=2)),z=bmatrix(np.around(Rz,decimals=2))))\n", + "$$\\Rightarrow R_{{xy}} \\\\neq R_{{yx}}$$\"\"\".format(vr=lvector(vr),vrp=lvector(vrp),xy=bmatrix(np.around(Rxy,decimals=2)),yx=bmatrix(np.around(Ryx,decimals=2)),x=bmatrix(np.around(Rx,decimals=2)), y=bmatrix(np.around(Ry,decimals=2)),z=bmatrix(np.around(Rz,decimals=2)),detRx=detRx,detRy=detRy,detRz=detRz))\n", "\n" ] }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 174, "metadata": {}, "outputs": [ { @@ -358,7 +391,7 @@ " };\n", "\n", " this.imageObj.onunload = function() {\n", - " this.ws.close();\n", + " fig.ws.close();\n", " }\n", "\n", " this.ws.onmessage = this._make_on_message_function(this);\n", @@ -853,7 +886,7 @@ " // Register the callback with on_msg.\n", " comm.on_msg(function(msg) {\n", " //console.log('receiving', msg['content']['data'], msg);\n", - " // Pass the mpl event to the overriden (by mpl) onmessage function.\n", + " // Pass the mpl event to the overridden (by mpl) onmessage function.\n", " ws.onmessage(msg['content']['data'])\n", " });\n", " return ws;\n", @@ -1005,9 +1038,12 @@ " // Check for shift+enter\n", " if (event.shiftKey && event.which == 13) {\n", " this.canvas_div.blur();\n", - " // select the cell after this one\n", - " var index = IPython.notebook.find_cell_index(this.cell_info[0]);\n", - " IPython.notebook.select(index + 1);\n", + " event.shiftKey = false;\n", + " // Send a \"J\" for go to next cell\n", + " event.which = 74;\n", + " event.keyCode = 74;\n", + " manager.command_mode();\n", + " manager.handle_keydown(event);\n", " }\n", "}\n", "\n", @@ -1056,7 +1092,7 @@ { "data": { "text/html": [ - "" + "" ], "text/plain": [ "" @@ -1068,21 +1104,21 @@ ], "source": [ "## setup for plotting the vectors\n", - "r_pre=np.array([0,0,0,r[0],r[1],r[2]])\n", - "r_past=np.array([0,0,0,rp[0],rp[1],rp[2]])\n", + "r_pre=np.array([0,0,0,vr[0],vr[1],vr[2]])\n", + "r_past=np.array([0,0,0,vrp[0],vrp[1],vrp[2]])\n", "\n", "fig = plt.figure(1)\n", "ax = fig.add_subplot(111, projection='3d')\n", "\n", - "##plot initial vector \n", - "vprelength=np.linalg.norm(r)\n", + "## plot initial vector \n", + "vprelength=np.linalg.norm(vr)\n", "ax.quiver(r_pre[0],r_pre[1],r_pre[2],r_pre[3],r_pre[4],r_pre[5],color='black',\n", - " pivot='tail',arrow_length_ratio=0.3/vprelength, label='original vector r')\n", + " pivot='tail',arrow_length_ratio=0.3/vprelength, label='original vector $\\\\vec{r}$')\n", "\n", - "##plot transformed vector\n", - "vpastlength=np.linalg.norm(rp)\n", + "## plot transformed vector\n", + "vpastlength=np.linalg.norm(vrp)\n", "ax.quiver(r_past[0],r_past[1],r_past[2],r_past[3],r_past[4],r_past[5],color='r',\n", - " pivot='tail',arrow_length_ratio=0.3/vpastlength, label='resulting vector $r^,$')\n", + " pivot='tail',arrow_length_ratio=0.3/vpastlength, label='resulting vector $\\\\vec{r}^,$')\n", "\n", "ax.set_xlim([-2,2])\n", "ax.set_ylim([-2,2])\n", @@ -1119,7 +1155,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.5" + "version": "3.5.2" } }, "nbformat": 4,