{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## SYMBOLIC ROTATIONS in 3D with sympy" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import sympy as sp\n", "vy,vy,vz,theta,c,s,V = sp.symbols('vy vy vz theta c s V')\n", "phi = sp.symbols('phi')" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def Rx(theta,V):\n", " \"\"\"\n", " Rotation of a 3d vector V\n", " of an angle theta around\n", " the x-axis\n", " \"\"\"\n", " c = sp.cos(theta)\n", " s = sp.sin(theta)\n", " R = sp.Matrix(\n", " [[1, 0, 0], \n", " [0, c, -s], \n", " [0, s, c]]\n", " )\n", " return R*V\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Matrix([\n", "[ 1],\n", "[-3*sin(theta) + 2*cos(theta)],\n", "[ 2*sin(theta) + 3*cos(theta)]])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "V = sp.Matrix([[1],[2],[3]])\n", "Rx(theta,V)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "def Ry(theta,V):\n", " \"\"\"\n", " Rotation of a 3d vector V\n", " of an angle theta around\n", " the y-axis\n", " \"\"\"\n", " c = sp.cos(theta)\n", " s = sp.sin(theta)\n", " R = sp.Matrix(\n", " [[c, 0, s],\n", " [0, 1, 0],\n", " [-s, 0, c]]\n", " )\n", " return R*V\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def Rz(theta,V):\n", " \"\"\"\n", " Rotation of a 3d vector V\n", " of an angle theta around\n", " the z-axis\n", " \"\"\"\n", " c = sp.cos(theta)\n", " s = sp.sin(theta)\n", " R = sp.Matrix(\n", " [[c, -s, 0], \n", " [s, c, 0],\n", " [0, 0, 1]]\n", " )\n", " return R*V\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Matrix([\n", "[ -sqrt(2)/2],\n", "[3*sqrt(2)/2],\n", "[ 3]])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Rz(sp.pi/4,V)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "def R(theta,phi,V):\n", " \"\"\"\n", " Rotation of a 3d vector V\n", " of an angle theta around\n", " the y-axis followed by a rotation\n", " of phi around the z-axis\n", " \"\"\"\n", " return Rz(phi,Ry(theta,V))\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Examples:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Matrix([\n", "[cos(phi)*cos(theta)],\n", "[sin(phi)*cos(theta)],\n", "[ -sin(theta)]])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "V = sp.Matrix([[1],[0],[0]])\n", "R(theta,phi,V)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Matrix([\n", "[sin(theta)*cos(phi)],\n", "[sin(phi)*sin(theta)],\n", "[ cos(theta)]])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "V = sp.Matrix([[0],[0],[1]])\n", "R(theta,phi,V)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Matrix([\n", "[I*sin(phi) + cos(phi)*cos(theta)],\n", "[sin(phi)*cos(theta) - I*cos(phi)],\n", "[ -sin(theta)]])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "V = sp.Matrix([[1],[-sp.I],[0]])\n", "R(theta,phi,V)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Matrix([\n", "[ I*sin(phi) - cos(phi)*cos(theta)],\n", "[-sin(phi)*cos(theta) - I*cos(phi)],\n", "[ sin(theta)]])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "V = sp.Matrix([[-1],[-sp.I],[0]])\n", "R(theta,phi,V)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Matrix([\n", "[sin(theta)*cos(phi)],\n", "[sin(phi)*sin(theta)],\n", "[ cos(theta)]])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "V = sp.Matrix([[0],[0],[1]])\n", "R(theta,phi,V)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "hide_input": false, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": true, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }