{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Basics of Vectors" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import sympy as sm\n", "import sympy.physics.mechanics as me" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create some measure numbers. It is good practice to declare symbols as real to encourage SymPy to provide simpler results." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "v1, v2, v3 = sm.symbols('v1, v2, v3', real=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A reference frame must be created before constructing vectors." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "A = me.ReferenceFrame('A')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The three orthornormal vectors (orthogonal unit vectors) associated with the frame can be accessed with the`.x,.y,.z` attributes and will be used to construct vectors." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\mathbf{\\hat{a}_x}$" ], "text/plain": [ "A.x" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A.x" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\mathbf{\\hat{a}_y}$" ], "text/plain": [ "A.y" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A.y" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\mathbf{\\hat{a}_z}$" ], "text/plain": [ "A.z" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A.z" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Vectors are created by multiplying measure numbers by a frame's associated unit vectors." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "v = v1*A.x + v2*A.y + v3*A.z" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The vectors stored in variable `v` can be displayed:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle v_{1}\\mathbf{\\hat{a}_x} + v_{2}\\mathbf{\\hat{a}_y} + v_{3}\\mathbf{\\hat{a}_z}$" ], "text/plain": [ "v1*A.x + v2*A.y + v3*A.z" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are alternative ways for accessing the frames unit vectors and how they display. See the help for details." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[0;31mInit signature:\u001b[0m \u001b[0mme\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mReferenceFrame\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mindices\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlatexs\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvariables\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mDocstring:\u001b[0m \n", "A reference frame in classical mechanics.\n", "\n", "ReferenceFrame is a class used to represent a reference frame in classical\n", "mechanics. It has a standard basis of three unit vectors in the frame's\n", "x, y, and z directions.\n", "\n", "It also can have a rotation relative to a parent frame; this rotation is\n", "defined by a direction cosine matrix relating this frame's basis vectors to\n", "the parent frame's basis vectors. It can also have an angular velocity\n", "vector, defined in another frame.\n", "\u001b[0;31mInit docstring:\u001b[0m\n", "ReferenceFrame initialization method.\n", "\n", "A ReferenceFrame has a set of orthonormal basis vectors, along with\n", "orientations relative to other ReferenceFrames and angular velocities\n", "relative to other ReferenceFrames.\n", "\n", "Parameters\n", "==========\n", "\n", "indices : list (of strings)\n", " If custom indices are desired for console, pretty, and LaTeX\n", " printing, supply three as a list. The basis vectors can then be\n", " accessed with the get_item method.\n", "latexs : list (of strings)\n", " If custom names are desired for LaTeX printing of each basis\n", " vector, supply the names here in a list.\n", "\n", "Examples\n", "========\n", "\n", ">>> from sympy.physics.vector import ReferenceFrame, vlatex\n", ">>> N = ReferenceFrame('N')\n", ">>> N.x\n", "N.x\n", ">>> O = ReferenceFrame('O', indices=('1', '2', '3'))\n", ">>> O.x\n", "O['1']\n", ">>> O['1']\n", "O['1']\n", ">>> P = ReferenceFrame('P', latexs=('A1', 'A2', 'A3'))\n", ">>> vlatex(P.x)\n", "'A1'\n", "\u001b[0;31mFile:\u001b[0m /opt/conda/lib/python3.6/site-packages/sympy/physics/vector/frame.py\n", "\u001b[0;31mType:\u001b[0m type\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "me.ReferenceFrame?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For example, if you prefer a 1, 2, and 3 instead of x, y, z for the subscripts you can change them like so:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "B = me.ReferenceFrame('B', indices=['1', '2', '3'])" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\mathbf{\\hat{b}_{1}}$" ], "text/plain": [ "B['1']" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B.x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The unit vector can be accessed using the square brackets and the indice if preferred." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\mathbf{\\hat{b}_{1}}$" ], "text/plain": [ "B['1']" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B['1']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Manipulating Vectors" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle v_{1}\\mathbf{\\hat{a}_x} + v_{2}\\mathbf{\\hat{a}_y} + v_{3}\\mathbf{\\hat{a}_z}$" ], "text/plain": [ "v1*A.x + v2*A.y + v3*A.z" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The scalar magnitude of the vector can be found:" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\sqrt{v_{1}^{2} + v_{2}^{2} + v_{3}^{2}}$" ], "text/plain": [ "sqrt(v1**2 + v2**2 + v3**2)" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v.magnitude()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And a unit vector in the same direction as $\\mathbf{v}$ is found with:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\frac{v_{1}}{\\sqrt{v_{1}^{2} + v_{2}^{2} + v_{3}^{2}}}\\mathbf{\\hat{a}_x} + \\frac{v_{2}}{\\sqrt{v_{1}^{2} + v_{2}^{2} + v_{3}^{2}}}\\mathbf{\\hat{a}_y} + \\frac{v_{3}}{\\sqrt{v_{1}^{2} + v_{2}^{2} + v_{3}^{2}}}\\mathbf{\\hat{a}_z}$" ], "text/plain": [ "v1/sqrt(v1**2 + v2**2 + v3**2)*A.x + v2/sqrt(v1**2 + v2**2 + v3**2)*A.y + v3/sqrt(v1**2 + v2**2 + v3**2)*A.z" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v.normalize()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is equivalent to:" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\frac{v_{1}}{\\sqrt{v_{1}^{2} + v_{2}^{2} + v_{3}^{2}}}\\mathbf{\\hat{a}_x} + \\frac{v_{2}}{\\sqrt{v_{1}^{2} + v_{2}^{2} + v_{3}^{2}}}\\mathbf{\\hat{a}_y} + \\frac{v_{3}}{\\sqrt{v_{1}^{2} + v_{2}^{2} + v_{3}^{2}}}\\mathbf{\\hat{a}_z}$" ], "text/plain": [ "v1/sqrt(v1**2 + v2**2 + v3**2)*A.x + v2/sqrt(v1**2 + v2**2 + v3**2)*A.y + v3/sqrt(v1**2 + v2**2 + v3**2)*A.z" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v / v.magnitude()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create another vector expressed in $A$." ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "b1, b2, b3 = sm.symbols(\"b1, b2, b3\", real=True)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle b_{1}\\mathbf{\\hat{a}_x} + b_{2}\\mathbf{\\hat{a}_y} + b_{3}\\mathbf{\\hat{a}_z}$" ], "text/plain": [ "b1*A.x + b2*A.y + b3*A.z" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "w = b1*A.x + b2*A.y + b3 * A.z\n", "w" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can add vectors:" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle (b_{1} + v_{1})\\mathbf{\\hat{a}_x} + (b_{2} + v_{2})\\mathbf{\\hat{a}_y} + (b_{3} + v_{3})\\mathbf{\\hat{a}_z}$" ], "text/plain": [ "(b1 + v1)*A.x + (b2 + v2)*A.y + (b3 + v3)*A.z" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v + w" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle 2 v_{1}\\mathbf{\\hat{a}_x} + 2 v_{2}\\mathbf{\\hat{a}_y} + 2 v_{3}\\mathbf{\\hat{a}_z}$" ], "text/plain": [ "2*v1*A.x + 2*v2*A.y + 2*v3*A.z" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v + v" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Multipy by scalars:" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle 10 v_{1}\\mathbf{\\hat{a}_x} + 10 v_{2}\\mathbf{\\hat{a}_y} + 10 v_{3}\\mathbf{\\hat{a}_z}$" ], "text/plain": [ "10*v1*A.x + 10*v2*A.y + 10*v3*A.z" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10 * v" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Compute the scaler dot product between to vectors:" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle v_{1}^{2} + v_{2}^{2} + v_{3}^{2}$" ], "text/plain": [ "v1**2 + v2**2 + v3**2" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v.dot(v)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle b_{1} v_{1} + b_{2} v_{2} + b_{3} v_{3}$" ], "text/plain": [ "b1*v1 + b2*v2 + b3*v3" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v.dot(w)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Cross products:" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle (- b_{2} v_{3} + b_{3} v_{2})\\mathbf{\\hat{a}_x} + (b_{1} v_{3} - b_{3} v_{1})\\mathbf{\\hat{a}_y} + (- b_{1} v_{2} + b_{2} v_{1})\\mathbf{\\hat{a}_z}$" ], "text/plain": [ "(-b2*v3 + b3*v2)*A.x + (b1*v3 - b3*v1)*A.y + (-b1*v2 + b2*v1)*A.z" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v.cross(w)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also comput the outer product, which creats a dyadic (a reference frame aware resprenstation of a 3D tensor)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle b_{1} v_{1}\\mathbf{\\hat{a}_x}\\otimes \\mathbf{\\hat{a}_x} + b_{2} v_{1}\\mathbf{\\hat{a}_x}\\otimes \\mathbf{\\hat{a}_y} + b_{3} v_{1}\\mathbf{\\hat{a}_x}\\otimes \\mathbf{\\hat{a}_z} + b_{1} v_{2}\\mathbf{\\hat{a}_y}\\otimes \\mathbf{\\hat{a}_x} + b_{2} v_{2}\\mathbf{\\hat{a}_y}\\otimes \\mathbf{\\hat{a}_y} + b_{3} v_{2}\\mathbf{\\hat{a}_y}\\otimes \\mathbf{\\hat{a}_z} + b_{1} v_{3}\\mathbf{\\hat{a}_z}\\otimes \\mathbf{\\hat{a}_x} + b_{2} v_{3}\\mathbf{\\hat{a}_z}\\otimes \\mathbf{\\hat{a}_y} + b_{3} v_{3}\\mathbf{\\hat{a}_z}\\otimes \\mathbf{\\hat{a}_z}$" ], "text/plain": [ "b1*v1*(A.x|A.x) + b2*v1*(A.x|A.y) + b3*v1*(A.x|A.z) + b1*v2*(A.y|A.x) + b2*v2*(A.y|A.y) + b3*v2*(A.y|A.z) + b1*v3*(A.z|A.x) + b2*v3*(A.z|A.y) + b3*v3*(A.z|A.z)" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v.outer(w)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The matrix form of both vectors and dyadics can be found if the frame of interest is provided." ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}v_{1}\\\\v_{2}\\\\v_{3}\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[v1],\n", "[v2],\n", "[v3]])" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v.to_matrix(A)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "d = v.outer(w)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}b_{1} v_{1} & b_{2} v_{1} & b_{3} v_{1}\\\\b_{1} v_{2} & b_{2} v_{2} & b_{3} v_{2}\\\\b_{1} v_{3} & b_{2} v_{3} & b_{3} v_{3}\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[b1*v1, b2*v1, b3*v1],\n", "[b1*v2, b2*v2, b3*v2],\n", "[b1*v3, b2*v3, b3*v3]])" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d.to_matrix(A)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can find all of the scalar variables present in a vector with `free_symbols()`." ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{v1, v2, v3}" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v.free_symbols(A)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Parital derivatives of vectors can be computed in the specified refrence frame." ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\mathbf{\\hat{a}_y}$" ], "text/plain": [ "A.y" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v.diff(v2, A)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can substitute values or other symbols into the measure number expressions." ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle 1.34\\mathbf{\\hat{a}_x} + 5\\mathbf{\\hat{a}_y} + v_{3}\\mathbf{\\hat{a}_z}$" ], "text/plain": [ "1.34000000000000*A.x + 5*A.y + v3*A.z" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v.subs({v1: 1.34, v2: 5})" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "z = v.subs({v1: 1.34, v2: 5})" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle 1.34\\mathbf{\\hat{a}_x} + v_{2}\\mathbf{\\hat{a}_y} + v_{3}\\mathbf{\\hat{a}_z}$" ], "text/plain": [ "1.34000000000000*A.x + v2*A.y + v3*A.z" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "z.subs({5: v2})" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle v_{1}$" ], "text/plain": [ "v1" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v1" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle v_{2}$" ], "text/plain": [ "v2" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v2" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [], "source": [ "m = v.to_matrix(A)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}5\\\\v_{2}\\\\v_{3}\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[ 5],\n", "[v2],\n", "[v3]])" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m.subs({v1: 5})" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}v_{1}\\\\v_{2}\\\\v_{3}\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[v1],\n", "[v2],\n", "[v3]])" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [], "source": [ "m = v.subs({v1: 1, v2: 5})" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\mathbf{\\hat{a}_x} + 5\\mathbf{\\hat{a}_y} + v_{3}\\mathbf{\\hat{a}_z}$" ], "text/plain": [ "A.x + 5*A.y + v3*A.z" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle v_{1}\\mathbf{\\hat{a}_x} + v_{2}\\mathbf{\\hat{a}_y} + v_{3}\\mathbf{\\hat{a}_z}$" ], "text/plain": [ "v1*A.x + v2*A.y + v3*A.z" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v" ] } ], "metadata": { "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.6.7" } }, "nbformat": 4, "nbformat_minor": 4 }