{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## NUMERICAL ROTATIONS in 3D with numpy" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 8, "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 = np.cos(theta)\n", " s = np.sin(theta)\n", " R = np.matrix(\n", " [[1, 0, 0], \n", " [0, c, -s], \n", " [0, s, c]]\n", " )\n", " return R*V\n" ] }, { "cell_type": "code", "execution_count": 7, "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 = np.cos(theta)\n", " s = np.sin(theta)\n", " R = np.matrix(\n", " [[c, 0, s],\n", " [0, 1, 0],\n", " [-s, 0, c]]\n", " )\n", " return R*V\n" ] }, { "cell_type": "code", "execution_count": 9, "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 = np.cos(theta)\n", " s = np.sin(theta)\n", " R = np.matrix(\n", " [[c, -s, 0], \n", " [s, c, 0],\n", " [0, 0, 1]]\n", " )\n", " return R*V\n" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A little demo of how the functions in this module can be used:\n", "Original vector V:\n", "[[1]\n", " [2]\n", " [3]]\n", "V rotated of pi/2 araund the x-axis: Rx(np.pi/2.,V)\n", "[[ 1.]\n", " [-3.]\n", " [ 2.]]\n", "V rotated of pi/4 araund the z-axis: Rz(np.pi/4.,V)\n", "[[-0.70710678]\n", " [ 2.12132034]\n", " [ 3. ]]\n" ] } ], "source": [ "if __name__ == \"__main__\":\n", " print(\"A little demo of how the functions in this module can be used:\")\n", " V = np.matrix([[1],[2],[3]])\n", " print(\"Original vector V:\\n\" + str(V))\n", " V1 = Rx(np.pi/2.,V)\n", " print(\"V rotated of pi/2 around the x-axis: Rx(np.pi/2.,V)\\n\" + str(V1))\n", " V2 = Rz(np.pi/4,V)\n", " print(\"V rotated of pi/4 around the z-axis: Rz(np.pi/4.,V)\\n\" + str(V2))\n" ] }, { "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.8.5" }, "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 }