{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from modsim import *" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "pint.unit.build_unit_class..Unit" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [] }, { "cell_type": "code", "execution_count": 165, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class Vector(np.ndarray):\n", " \"\"\"Represents a vector, usually in 2D or 3D.\n", " \n", " Based on https://gist.github.com/eigencoder/c029d7557e1f0828aec5\n", " \"\"\"\n", " def __new__(cls, *args):\n", " \"\"\"\n", " cls: class object\n", " args: tuple of coordinates\n", " \"\"\"\n", " if len(args) < 2:\n", " raise ValueError('Vector must contain at least two elements.')\n", " \n", " # if any of the elements have units, store the first one\n", " for elt in args:\n", " units = getattr(elt, 'units', None)\n", " if units:\n", " break\n", " \n", " if units:\n", " # if there are units, remove them\n", " args = [getattr(elt, 'magnitude', elt) for elt in args]\n", " \n", " obj = np.asarray(args).view(cls) \n", " return obj\n", " \n", " def __mul__(self, other):\n", " if isinstance(other, UNITS.Unit):\n", " return VectorQuantity(self, other)\n", " else:\n", " return super().__mul__(other)\n", " \n", " def __div__(self, other):\n", " if isinstance(other, UNITS.Unit):\n", " return VectorQuantity(self, 1/other)\n", " else:\n", " return super().__truediv__(other)\n", " \n", " __truediv__ = __div__\n", " \n", " @property\n", " def x(self):\n", " return self[0]\n", "\n", " @property\n", " def y(self):\n", " return self[1]\n", "\n", " @property\n", " def z(self):\n", " return self[2]\n", " \n", " def __repr__(self):\n", " t = map(str, self)\n", " s = ', '.join(t)\n", " return '%s(%s)' % (self.__class__.__name__, s)\n", " \n", " def __eq__(self, other):\n", " return np.array_equal(self, other)\n", "\n", " def __ne__(self, other):\n", " return not np.array_equal(self, other)\n", "\n", " def __iter__(self):\n", " for x in np.nditer(self):\n", " yield x.item()\n", "\n", " @property\n", " def mag(self):\n", " \"\"\"\n", " \"\"\"\n", " return np.sqrt(np.dot(self, self))\n", "\n", " @property\n", " def mag2(self):\n", " \"\"\"\n", " \"\"\"\n", " return np.dot(self, self)\n", "\n", " def hat(self):\n", " \"\"\"\n", " \"\"\"\n", " return self / self.mag\n", "\n", " @property\n", " def angle(self):\n", " \"\"\"\n", " \"\"\"\n", " return np.arctan2(self.y, self.x)\n", "\n", " def polar(self):\n", " return self.mag, self.angle\n", "\n", " def dot(self, other):\n", " \"\"\"\n", " \"\"\"\n", " return np.dot(self, other)\n", "\n", " def cross(self, other):\n", " \"\"\"\n", " \"\"\"\n", " return np.cross(self, other)\n", "\n", " def proj(self, other):\n", " \"\"\"\n", " \"\"\"\n", " print('hello1')\n", " b_hat = other.hat()\n", " return self.dot(b_hat) * b_hat\n", "\n", " def comp(self, other):\n", " \"\"\"\n", " \"\"\"\n", " return self.dot(other.hat())\n", "\n", " def dist(self, other):\n", " \"\"\"Euclidean distance from self to other.\n", " \"\"\"\n", " return np.linalg.norm(self - other)\n", "\n", " def diff_angle(self, other):\n", " \"\"\"\n", " \"\"\"\n", " #TODO: see http://www.euclideanspace.com/maths/algebra/vectors/angleBetween/\n", " raise NotImplementedError()" ] }, { "cell_type": "code", "execution_count": 192, "metadata": {}, "outputs": [ { "ename": "IndentationError", "evalue": "unindent does not match any outer indentation level (, line 13)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m13\u001b[0m\n\u001b[0;31m def __div__(self, other):\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m unindent does not match any outer indentation level\n" ] } ], "source": [ "def __mul__(self, other):\n", " if isinstance(other, UNITS.Unit):\n", " return VectorQuantity(self, self.units * other)\n", " \n", " if isinstance(other, UNITS.Quantity):\n", " prod = super().__mul__(other.magnitude)\n", " prod._units = self.units * other.units\n", " return prod\n", " else:\n", " prod = super().__mul__(other)\n", " return prod\n", " \n", " def __div__(self, other):\n", " if isinstance(other, UNITS.Unit):\n", " return VectorQuantity(self, self.units / other)\n", " \n", " if isinstance(other, UNITS.Quantity):\n", " quot = super().__div__(other.magnitude)\n", " quot._units = self.units / other.units\n", " return quot\n", " else:\n", " quot = super().__div__(other)\n", " return quot\n", " \n", " __truediv__ = __div__" ] }, { "cell_type": "code", "execution_count": 193, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class VectorQuantity(Vector, UNITS.Quantity):\n", " \n", " def __new__(cls, vector, units):\n", " \"\"\"\n", " cls: class object\n", " args: tuple of coordinates\n", " \"\"\"\n", " obj = np.asarray(vector).view(cls)\n", " obj._units = units \n", " return obj\n", " \n", " def __repr__(self):\n", " return super().__repr__() + ' ' + str(self.units)\n", " \n", " def __mul__(self, other):\n", " if isinstance(other, UNITS.Unit):\n", " return VectorQuantity(self, self.units * other)\n", " \n", " if isinstance(other, UNITS.Quantity):\n", " prod = super().__mul__(other.magnitude)\n", " prod._units = self.units * other.units\n", " return prod\n", " else:\n", " prod = super().__mul__(other)\n", " return prod\n", " \n", " def __div__(self, other):\n", " if isinstance(other, UNITS.Unit):\n", " return VectorQuantity(self, self.units / other)\n", " \n", " if isinstance(other, UNITS.Quantity):\n", " quot = super().__div__(other.magnitude)\n", " quot._units = self.units / other.units\n", " return quot\n", " else:\n", " quot = super().__div__(other)\n", " return quot\n", " \n", " __truediv__ = __div__\n", " \n", " #def __getitem__(self, key):\n", " # return super().__getitem__(key) * self.units\n", "\n", " @property\n", " def x(self):\n", " return self[0] * self.units\n", "\n", " @property\n", " def y(self):\n", " return self[1] * self.units\n", "\n", " @property\n", " def z(self):\n", " return self[2] * self.units\n", " \n", " @property\n", " def mag(self):\n", " return super().mag * self.units\n", " \n", " @property\n", " def mag2(self):\n", " return super().mag2 * self.units\n", "\n", " def hat(self):\n", " return super().hat() * self.units\n", "\n", " def dot(self, other):\n", " prod = super().dot(other) * self.units\n", " try:\n", " return prod * other.units\n", " except AttributeError:\n", " return prod\n", " \n", " def cross(self, other):\n", " prod = super().cross(other) * self.units\n", " try:\n", " return prod * other.units\n", " except AttributeError:\n", " return prod\n", " \n", " def proj(self, other):\n", " \"\"\"\n", " \"\"\"\n", " print('hello1')\n", " return super().proj(other) / self.units\n", " \n", " def comp(self, other):\n", " \"\"\"\n", " \"\"\"\n", " return super().comp(other) / self.units" ] }, { "cell_type": "code", "execution_count": 194, "metadata": { "collapsed": true }, "outputs": [], "source": [ "m = UNITS.m\n", "N = UNITS.newton" ] }, { "cell_type": "code", "execution_count": 195, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(2)" ] }, "execution_count": 195, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = Vector(3, 4)\n", "b = Vector(1, 2)\n", "a.cross(b)" ] }, { "cell_type": "code", "execution_count": 196, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "__main__.VectorQuantity" ] }, "execution_count": 196, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = Vector(3, 4) * m\n", "type(a)" ] }, { "cell_type": "code", "execution_count": 197, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "VectorQuantity(3, 4) meter" ] }, "execution_count": 197, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 198, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "VectorQuantity(3, 4) meter" ] }, "execution_count": 198, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a * m" ] }, { "cell_type": "code", "execution_count": 199, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "units must be of type str, Unit or UnitsContainer; not .Quantity'>.", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m/home/downey/anaconda3/lib/python3.6/site-packages/IPython/core/formatters.py\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, obj)\u001b[0m\n\u001b[1;32m 670\u001b[0m \u001b[0mtype_pprinters\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtype_printers\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 671\u001b[0m deferred_pprinters=self.deferred_printers)\n\u001b[0;32m--> 672\u001b[0;31m \u001b[0mprinter\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpretty\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mobj\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 673\u001b[0m \u001b[0mprinter\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mflush\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 674\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mstream\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgetvalue\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/home/downey/anaconda3/lib/python3.6/site-packages/IPython/lib/pretty.py\u001b[0m in \u001b[0;36mpretty\u001b[0;34m(self, obj)\u001b[0m\n\u001b[1;32m 381\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mcallable\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmeth\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 382\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mmeth\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mobj\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcycle\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 383\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0m_default_pprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mobj\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcycle\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 384\u001b[0m \u001b[0;32mfinally\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 385\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mend_group\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/home/downey/anaconda3/lib/python3.6/site-packages/IPython/lib/pretty.py\u001b[0m in \u001b[0;36m_default_pprint\u001b[0;34m(obj, p, cycle)\u001b[0m\n\u001b[1;32m 501\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0m_safe_getattr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mklass\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'__repr__'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32min\u001b[0m \u001b[0m_baseclass_reprs\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 502\u001b[0m \u001b[0;31m# A user-provided repr. Find newlines and replace them with p.break_()\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 503\u001b[0;31m \u001b[0m_repr_pprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mobj\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mp\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcycle\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 504\u001b[0m \u001b[0;32mreturn\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 505\u001b[0m \u001b[0mp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbegin_group\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'<'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/home/downey/anaconda3/lib/python3.6/site-packages/IPython/lib/pretty.py\u001b[0m in \u001b[0;36m_repr_pprint\u001b[0;34m(obj, p, cycle)\u001b[0m\n\u001b[1;32m 699\u001b[0m \u001b[0;34m\"\"\"A pprint that just redirects to the normal repr function.\"\"\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 700\u001b[0m \u001b[0;31m# Find newlines and replace them with p.break_()\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 701\u001b[0;31m \u001b[0moutput\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mrepr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mobj\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 702\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0midx\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0moutput_line\u001b[0m \u001b[0;32min\u001b[0m \u001b[0menumerate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0moutput\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msplitlines\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 703\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0midx\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m\u001b[0m in \u001b[0;36m__repr__\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__repr__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 13\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0msuper\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__repr__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m' '\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0munits\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 14\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/home/downey/anaconda3/lib/python3.6/site-packages/pint/quantity.py\u001b[0m in \u001b[0;36munits\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 175\u001b[0m \u001b[0;34m:\u001b[0m\u001b[0mrtype\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mUnitContainer\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 176\u001b[0m \"\"\"\n\u001b[0;32m--> 177\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_REGISTRY\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mUnit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_units\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 178\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 179\u001b[0m \u001b[0;34m@\u001b[0m\u001b[0mproperty\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/home/downey/anaconda3/lib/python3.6/site-packages/pint/unit.py\u001b[0m in \u001b[0;36m__new__\u001b[0;34m(cls, units)\u001b[0m\n\u001b[1;32m 69\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 70\u001b[0m raise TypeError('units must be of type str, Unit or '\n\u001b[0;32m---> 71\u001b[0;31m 'UnitsContainer; not {0}.'.format(type(units)))\n\u001b[0m\u001b[1;32m 72\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 73\u001b[0m \u001b[0minst\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__used\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mFalse\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mTypeError\u001b[0m: units must be of type str, Unit or UnitsContainer; not .Quantity'>." ] } ], "source": [ "a / m" ] }, { "cell_type": "code", "execution_count": 173, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 173, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[0]" ] }, { "cell_type": "code", "execution_count": 174, "metadata": {}, "outputs": [ { "data": { "text/html": [ "3 meter" ], "text/latex": [ "$3 meter$" ], "text/plain": [ "" ] }, "execution_count": 174, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.x" ] }, { "cell_type": "code", "execution_count": 175, "metadata": {}, "outputs": [ { "data": { "text/html": [ "4 meter" ], "text/latex": [ "$4 meter$" ], "text/plain": [ "" ] }, "execution_count": 175, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.y" ] }, { "cell_type": "code", "execution_count": 176, "metadata": {}, "outputs": [ { "data": { "text/html": [ "5.0 meter" ], "text/latex": [ "$5.0 meter$" ], "text/plain": [ "" ] }, "execution_count": 176, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.mag" ] }, { "cell_type": "code", "execution_count": 177, "metadata": {}, "outputs": [ { "data": { "text/html": [ "25 meter" ], "text/latex": [ "$25 meter$" ], "text/plain": [ "" ] }, "execution_count": 177, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.mag2" ] }, { "cell_type": "code", "execution_count": 178, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "__main__.VectorQuantity" ] }, "execution_count": 178, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(a)" ] }, { "cell_type": "code", "execution_count": 179, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "pint.unit.build_quantity_class..Quantity" ] }, "execution_count": 179, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(a.mag)" ] }, { "cell_type": "code", "execution_count": 180, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "VectorQuantity(15.0, 20.0) meter ** 2" ] }, "execution_count": 180, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c = a * a.mag\n", "c" ] }, { "cell_type": "code", "execution_count": 181, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "VectorQuantity(0.6, 0.8) dimensionless" ] }, "execution_count": 181, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c = a / a.mag\n", "c" ] }, { "cell_type": "code", "execution_count": 182, "metadata": {}, "outputs": [ { "data": { "text/html": [ "0.6 dimensionless" ], "text/latex": [ "$0.6 dimensionless$" ], "text/plain": [ "" ] }, "execution_count": 182, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c.x" ] }, { "cell_type": "code", "execution_count": 183, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "VectorQuantity(0.6, 0.8) meter" ] }, "execution_count": 183, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ah = a.hat()\n", "ah" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 184, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 0.6 0.8]\n" ] } ], "source": [ "print(ah)" ] }, { "cell_type": "code", "execution_count": 185, "metadata": {}, "outputs": [ { "data": { "text/html": [ "meter" ], "text/latex": [ "$meter$" ], "text/plain": [ "" ] }, "execution_count": 185, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ah.units" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 186, "metadata": { "collapsed": true }, "outputs": [], "source": [ "b = Vector(1, 2)" ] }, { "cell_type": "code", "execution_count": 187, "metadata": {}, "outputs": [ { "data": { "text/html": [ "11 meter" ], "text/latex": [ "$11 meter$" ], "text/plain": [ "" ] }, "execution_count": 187, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.dot(b)" ] }, { "cell_type": "code", "execution_count": 188, "metadata": {}, "outputs": [], "source": [ "b = Vector(1, 2) * N" ] }, { "cell_type": "code", "execution_count": 189, "metadata": {}, "outputs": [ { "data": { "text/html": [ "11.0 meter newton" ], "text/latex": [ "$11.0 meter \\cdot newton$" ], "text/plain": [ "" ] }, "execution_count": 189, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.dot(b)" ] }, { "cell_type": "code", "execution_count": 190, "metadata": {}, "outputs": [ { "data": { "text/html": [ "2.0 meter newton" ], "text/latex": [ "$2.0 meter \\cdot newton$" ], "text/plain": [ "" ] }, "execution_count": 190, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c = a.cross(b)\n", "c" ] }, { "cell_type": "code", "execution_count": 191, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello1\n", "hello1\n", "maximum recursion depth exceeded while calling a Python object\n" ] } ], "source": [ "c = a.proj(b)" ] }, { "cell_type": "code", "execution_count": 133, "metadata": {}, "outputs": [ { "data": { "text/html": [ "[ 2.2 4.4] 1/meter" ], "text/latex": [ "$[ 2.2 4.4] \\frac{1}{meter}$" ], "text/plain": [ "" ] }, "execution_count": 133, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c" ] }, { "cell_type": "code", "execution_count": 134, "metadata": {}, "outputs": [ { "data": { "text/html": [ "4.919349550499537 newton" ], "text/latex": [ "$4.919349550499537 newton$" ], "text/plain": [ "" ] }, "execution_count": 134, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.comp(b)" ] }, { "cell_type": "code", "execution_count": 135, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x_hat = Vector(1, 0)\n", "y_hat = Vector(0, 1)" ] }, { "cell_type": "code", "execution_count": 136, "metadata": {}, "outputs": [ { "data": { "text/html": [ "3.0 dimensionless" ], "text/latex": [ "$3.0 dimensionless$" ], "text/plain": [ "" ] }, "execution_count": 136, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.comp(x_hat)" ] }, { "cell_type": "code", "execution_count": 137, "metadata": {}, "outputs": [ { "data": { "text/html": [ "4.0 dimensionless" ], "text/latex": [ "$4.0 dimensionless$" ], "text/plain": [ "" ] }, "execution_count": 137, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.comp(y_hat)" ] }, { "cell_type": "code", "execution_count": 138, "metadata": {}, "outputs": [ { "data": { "text/html": [ "0.9272952180016122 radian" ], "text/latex": [ "$0.9272952180016122 radian$" ], "text/plain": [ "" ] }, "execution_count": 138, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.angle" ] }, { "cell_type": "code", "execution_count": 139, "metadata": {}, "outputs": [ { "data": { "text/html": [ "1.1071487177940904 radian" ], "text/latex": [ "$1.1071487177940904 radian$" ], "text/plain": [ "" ] }, "execution_count": 139, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b.angle" ] }, { "cell_type": "code", "execution_count": 140, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.25" ] }, "execution_count": 140, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Vector(1, 1).angle / pi" ] }, { "cell_type": "code", "execution_count": 141, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.75" ] }, "execution_count": 141, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Vector(-1, 1).angle / pi" ] }, { "cell_type": "code", "execution_count": 142, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-0.75" ] }, "execution_count": 142, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Vector(-1, -1).angle / pi" ] }, { "cell_type": "code", "execution_count": 143, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-0.25" ] }, "execution_count": 143, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Vector(1, -1).angle / pi" ] }, { "cell_type": "code", "execution_count": 144, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1.4142135623730951, -0.78539816339744828)" ] }, "execution_count": 144, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Vector(1, -1).polar()" ] }, { "cell_type": "code", "execution_count": 145, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[5, 3.5355, 0, -10]" ] }, "execution_count": 145, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = [5, 3.5355, 0, -10]\n", "x" ] }, { "cell_type": "code", "execution_count": 146, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 3.5355, 10, 0]" ] }, "execution_count": 146, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y = [0, 3.5355, 10, 0]\n", "y" ] }, { "cell_type": "code", "execution_count": 147, "metadata": {}, "outputs": [], "source": [ "theta, rho = cart2pol(x,y)" ] }, { "cell_type": "code", "execution_count": 148, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0. , 0.78539816, 1.57079633, 3.14159265])" ] }, "execution_count": 148, "metadata": {}, "output_type": "execute_result" } ], "source": [ "theta" ] }, { "cell_type": "code", "execution_count": 149, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 5. , 4.99995205, 10. , 10. ])" ] }, "execution_count": 149, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rho" ] }, { "cell_type": "code", "execution_count": 150, "metadata": {}, "outputs": [], "source": [ "theta = [0, pi/4, pi/2, pi]" ] }, { "cell_type": "code", "execution_count": 151, "metadata": { "collapsed": true }, "outputs": [], "source": [ "rho = [5, 5, 10, 10]" ] }, { "cell_type": "code", "execution_count": 152, "metadata": {}, "outputs": [], "source": [ "x2, y2 = pol2cart(theta,rho)" ] }, { "cell_type": "code", "execution_count": 153, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 5.00000000e+00 3.53553391e+00 6.12323400e-16 -1.00000000e+01]\n" ] } ], "source": [ "print(x2)" ] }, { "cell_type": "code", "execution_count": 154, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 0.00000000e+00 3.53553391e+00 1.00000000e+01 1.22464680e-15]\n" ] } ], "source": [ "print(y2)" ] }, { "cell_type": "code", "execution_count": 155, "metadata": { "collapsed": true }, "outputs": [], "source": [ "degree = UNITS.degree\n", "kg = UNITS.kg\n", "m = UNITS.m\n", "s = UNITS.s" ] }, { "cell_type": "code", "execution_count": 156, "metadata": {}, "outputs": [], "source": [ "condition = Condition(x = 0 * m, \n", " y = 0 * m,\n", " g = 9.8 * m/s**2,\n", " mass = 145e-3 * kg,\n", " diameter = 73e-3 * m,\n", " rho = 1.2 * kg/m**3,\n", " C_d = 0.3,\n", " angle = 45 * degree,\n", " velocity = 40 * m / s,\n", " duration = 5 * s)" ] }, { "cell_type": "code", "execution_count": 229, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def make_system(condition):\n", " unpack(condition)\n", " \n", " theta = np.deg2rad(angle)\n", " vx, vy = pol2cart(theta, velocity)\n", " init = State(x=x, y=y, vx=vx, vy=vy)\n", " v = State(vx=vx, vy=vy)\n", " \n", " area = np.pi * (diameter/2)**2\n", " ts = linspace(0, duration, 101)\n", " \n", " return System(init=init, g=g, mass=mass, v=v,\n", " area=area, rho=rho, C_d=C_d, ts=ts)" ] }, { "cell_type": "code", "execution_count": 230, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
value
x0 meter
y0 meter
vx28.284271247461902 meter / second
vy28.2842712474619 meter / second
\n", "
" ], "text/plain": [ "x 0 meter\n", "y 0 meter\n", "vx 28.284271247461902 meter / second\n", "vy 28.2842712474619 meter / second\n", "dtype: object" ] }, "execution_count": 230, "metadata": {}, "output_type": "execute_result" } ], "source": [ "system = make_system(condition)\n", "system.init" ] }, { "cell_type": "code", "execution_count": 235, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([,\n", " ], dtype=object)" ] }, "execution_count": 235, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v = system.v.values\n", "v" ] }, { "cell_type": "code", "execution_count": 233, "metadata": {}, "outputs": [ { "data": { "text/html": [ "1600.0 meter2/second2" ], "text/latex": [ "$1600.0 \\frac{meter^{2}}{second^{2}}$" ], "text/plain": [ "" ] }, "execution_count": 233, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.dot(v, v)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 222, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def slope_func(state, t, system):\n", " x, y, vx, vy = state\n", " unpack(system)\n", " \n", " a_grav = np.array([0, -9.8]) * m / s**2\n", "\n", " v = np.array([vx.magnitude, vy.magnitude]) * m / s\n", " \n", " f_drag = -rho * np.linalg.norm(v) * v.units * v * C_d * area / 2\n", " a_drag = f_drag / mass\n", "\n", " a = a_grav + a_drag\n", " \n", " return v, a" ] }, { "cell_type": "code", "execution_count": 223, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(,\n", " )" ] }, "execution_count": 223, "metadata": {}, "output_type": "execute_result" } ], "source": [ "slope_func(system.init, 0, system)" ] }, { "cell_type": "code", "execution_count": 161, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a = 5 * m" ] }, { "cell_type": "code", "execution_count": 162, "metadata": { "collapsed": true }, "outputs": [], "source": [ "b = Vector(1, 2) * s" ] }, { "cell_type": "code", "execution_count": 163, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "VectorQuantity(5, 10) meter * second" ] }, "execution_count": 163, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b * a" ] }, { "cell_type": "code", "execution_count": 164, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "maximum recursion depth exceeded in comparison\n" ] }, { "data": { "text/plain": [ "array([ 5, 10])" ] }, "execution_count": 164, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a * b" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "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.1" } }, "nbformat": 4, "nbformat_minor": 2 }