Units#

Recipes for calculations with units. handcalcs shows whatever value your code produces, so the units library does the arithmetic and formats the units. The background is in Units.

Calculate with forallpeople#

Load an environment once, in a cell you don’t render, then write units as you would on paper:

import forallpeople as si
si.environment("structural", top_level=True)
L = (6 * m)
w = 5 * kN / m  # hc: -p
M = w * L**2 / 8
Output (MarkdownRenderer)
\[\begin{aligned} L &= 6\ \mathrm{m} \\ w &= 5\ \mathrm{kN/m} \\ M &= \frac{w \cdot L^{2}}{8} = \frac{5\ \mathrm{kN/m} \cdot \left(6\ \mathrm{m}\right)^{2}}{8} = 22.5\ \mathrm{kN} \cdot \mathrm{m} \end{aligned}\]
$$\begin{aligned} L &= 6\ \mathrm{m} \\ w &= 5\ \mathrm{kN/m} \\ M &= \frac{w \cdot L^{2}}{8} = \frac{5\ \mathrm{kN/m} \cdot \left(6\ \mathrm{m}\right)^{2}}{8} = 22.5\ \mathrm{kN} \cdot \mathrm{m} \end{aligned}$$

An input such as 6 * m has nothing worth substituting: without help it renders as L = 6 · m = 6 · 1 m = 6 m. Skip the substitution in either of two ways, which give the same result:

  • wrap the right-hand side in parentheses: L = (6 * m);

  • end the line with the param-line flag: w = 5 * kN / m  # hc: -p.

Both show only name = value. Lines that calculate, like M, keep every step. See Show only the result of a line.

Units are set upright, and a substituted quantity raised to a power is bracketed.

Calculate with pint#

Asked in #203 and #245.

Choose pint’s short, pretty unit format when you create the registry:

import pint
ureg = pint.UnitRegistry()
ureg.formatter.default_format = "~P"
L = (6 * ureg.m)
w = 5 * ureg.kN / ureg.m  # hc: -p
M = w * L**2 / 8
Output (MarkdownRenderer)
\[\begin{aligned} L &= 6\ \mathrm{m} \\ w &= 5\ \frac{\mathrm{kN}}{\mathrm{m}} \\ M &= \frac{w \cdot L^{2}}{8} = \frac{5\ \frac{\mathrm{kN}}{\mathrm{m}} \cdot \left(6\ \mathrm{m}\right)^{2}}{8} = 22.5\ \mathrm{kN} \cdot \mathrm{m} \end{aligned}\]
$$\begin{aligned} L &= 6\ \mathrm{m} \\ w &= 5\ \frac{\mathrm{kN}}{\mathrm{m}} \\ M &= \frac{w \cdot L^{2}}{8} = \frac{5\ \frac{\mathrm{kN}}{\mathrm{m}} \cdot \left(6\ \mathrm{m}\right)^{2}}{8} = 22.5\ \mathrm{kN} \cdot \mathrm{m} \end{aligned}$$

The LaTeX and Markdown renderers typeset pint units in math, abbreviated when the registry’s default format contains ~; the HTML and ASCII renderers use the registry’s text format (22.5 kN⋅m with ~P).

Outside Jupyter, HandCalcs(renderer)(source, globals()) renders the same code from a script. See Python: HandCalcs.

Show a result in specific units#

Asked in #100 and #226.

Convert with the library’s own method. A conversion can be chained onto a whole expression, or put on its own line with -p to show only the converted value:

w = (5 * ureg.kN / ureg.m)
L = 6000 * ureg.mm  # hc: -p
M = (w * L**2 / 8).to("kN*m")
M_Nmm = M.to("N*mm")  # hc: -p
Output (MarkdownRenderer)
\[\begin{aligned} w &= 5\ \frac{\mathrm{kN}}{\mathrm{m}} \\ L &= 6000\ \mathrm{mm} \\ M &= \left(\frac{w \cdot L^{2}}{8}\right).\mathrm{to}\left(\text{"kN*m"}\right) = \left(\frac{5\ \frac{\mathrm{kN}}{\mathrm{m}} \cdot \left(6000\ \mathrm{mm}\right)^{2}}{8}\right).\mathrm{to}\left(\text{"kN*m"}\right) = 22.5\ \mathrm{kN} \cdot \mathrm{m} \\ M_{\mathrm{Nmm}} &= 2.25\times 10^{7}\ \mathrm{mm} \cdot \mathrm{N} \end{aligned}\]
$$\begin{aligned} w &= 5\ \frac{\mathrm{kN}}{\mathrm{m}} \\ L &= 6000\ \mathrm{mm} \\ M &= \left(\frac{w \cdot L^{2}}{8}\right).\mathrm{to}\left(\text{"kN*m"}\right) = \left(\frac{5\ \frac{\mathrm{kN}}{\mathrm{m}} \cdot \left(6000\ \mathrm{mm}\right)^{2}}{8}\right).\mathrm{to}\left(\text{"kN*m"}\right) = 22.5\ \mathrm{kN} \cdot \mathrm{m} \\ M_{\mathrm{Nmm}} &= 2.25\times 10^{7}\ \mathrm{mm} \cdot \mathrm{N} \end{aligned}$$

Set the precision of a quantity#

-f applies to quantities from both libraries:

w = (5.25 * kN / m)
L = 7.3 * m  # hc: -p
M = w * L**2 / 8  # hc: -f .2f
Output (MarkdownRenderer)
\[\begin{aligned} w &= 5.25\ \mathrm{kN/m} \\ L &= 7.3\ \mathrm{m} \\ M &= \frac{w \cdot L^{2}}{8} = \frac{5.25\ \mathrm{kN/m} \cdot \left(7.30\ \mathrm{m}\right)^{2}}{8} = 34.97\ \mathrm{kN} \cdot \mathrm{m} \end{aligned}\]
$$\begin{aligned} w &= 5.25\ \mathrm{kN/m} \\ L &= 7.3\ \mathrm{m} \\ M &= \frac{w \cdot L^{2}}{8} = \frac{5.25\ \mathrm{kN/m} \cdot \left(7.30\ \mathrm{m}\right)^{2}}{8} = 34.97\ \mathrm{kN} \cdot \mathrm{m} \end{aligned}$$

Keep the unit setup out of the output#

Set up the units in a cell you don’t render, or end each setup line with # hc: -i:

import forallpeople as si               # hc: -i
si.environment("structural", top_level=True)  # hc: -i
L = (6 * si.m)
Output (MarkdownRenderer)
\[\begin{aligned} L &= 6\ \mathrm{m} \end{aligned}\]
$$\begin{aligned} L &= 6\ \mathrm{m} \end{aligned}$$

Units in Excel#

The Excel renderer turns pint and forallpeople quantities into unit columns and SI-scaled formulas, so the workbook stays live.

Next: Reports and documents.