{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Author: J. Simmons" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "require(['base/js/utils'],\n", "function(utils) {\n", " utils.load_extensions('usability/python-markdown/main');\n", "});" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%javascript\n", "require(['base/js/utils'],\n", "function(utils) {\n", " utils.load_extensions('usability/python-markdown/main');\n", "});" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Test Units with Pint\n", "\n", "This section tests using the Pint library to implement units in MTK. The following\n", "analysis computes the area of a rectangle." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from pint import UnitRegistry\n", "units = UnitRegistry()\n", "\n", "def displayFloat(x):\n", " return '{:~P}'.format(x)\n", "\n", "#def displayFloat(x, u):\n", "# return displayFloat(x.to(u))" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "length = 2.5 * units.meter\n", "width = 2.0 * units.meter\n", "\n", "area = length * width\n", "\n", "force = 20 * units.newton\n", "pressure = force / area\n", "pressure.ito(units.pascal)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true, "variables": { "displayFloat(area)": "5.0 m²", "displayFloat(force)": "20 N", "displayFloat(length)": "2.5 m", "displayFloat(pressure)": "4.0 Pa", "displayFloat(width)": "2.0 m" } }, "source": [ "The area of a rectangle of length {{displayFloat(length)}} and width {{displayFloat(width)}} is {{displayFloat(area)}}. For a force of {{displayFloat(force)}} the pressure on the rectangle is {{displayFloat(pressure)}}." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "This document is a work of Mach 30 and is licensed under the Creative Commons Attribution 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/." ] } ], "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.4.3" } }, "nbformat": 4, "nbformat_minor": 0 }