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)
# hc: -u
L = 6 * m
w = 5 * kN / m
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. # hc: -u shows each line that is a number times units as name = value, and leaves the lines that calculate, like M, with every step. On its own line it lasts until # hc: units = False; at the end of a line it applies to that line. To use it everywhere, set it once: handcalcs.set_option("units", True).

A line counts as a number times units when it has exactly one number, multiplying units: 5 * kN, -12 * kN, 4.5 * kN / m, 300 * mm**2. A name only counts as a unit if it is one (kN), not a variable that holds one (F = 1 * kN, then R = 3 * F), so R keeps its steps. In symbolic-only mode, which hides values, lines are left as they are.

For a single line, two other ways 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.

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"
# hc: -u
L = 6 * ureg.m
w = 5 * ureg.kN / ureg.m
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}$$

Values with a tolerance, or their own LaTeX#

Asked in #340.

A value that is not a quantity but has its own _repr_latex_, such as a worstcase parameter, is shown with that LaTeX by the LaTeX and Markdown renderers. It is bracketed in a substitution when it has more than one term:

"""A value with a tolerance that writes its own LaTeX, like an uncertainty package's."""


class Tolerance:
    def __init__(self, nom, tol):
        self.nom, self.tol = nom, tol

    def __mul__(self, other):
        return Tolerance(self.nom * other, self.tol * other)

    __rmul__ = __mul__

    def __format__(self, spec):
        return f"{self.nom:g} ± {self.tol:g}"

    def _repr_latex_(self):
        return f"${self.nom:g} \\pm {self.tol:g}$"
y = x * 2
Output (MarkdownRenderer)
\[\begin{aligned} y &= x \cdot 2 = \left(1 \pm 0.1\right) \cdot 2 = \left(2 \pm 0.2\right) \end{aligned}\]
$$\begin{aligned} y &= x \cdot 2 = \left(1 \pm 0.1\right) \cdot 2 = \left(2 \pm 0.2\right) \end{aligned}$$

The format code does not apply to such a value: it is shown exactly as its _repr_latex_ writes it. The other renderers show its text (its __format__).

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.