Physical Quantities

%reload_ext rubberize
from rubberize.config import config

Rubberize integrates well with Pint to support physical quantities:

Note

Pint is not a core Rubberize dependency. You will need to install it separately.

pint.Quantity objects, which encapsulate numbers with units, are fully integrated into Rubberize. This guide demonstrates how Rubberize renders these quantities.

import pint

# instantiate a unit registry
ureg = pint.UnitRegistry()

Creating Quantities

Rubberize recognizes when the user intends to create physical quantities by applying units:

%%tap
v_1 = 0.0                       # Final velocity of the car
v_0 = 25.0 * (ureg.m / ureg.s)  # Initial velocity of the car
Deltat = 0.2 * ureg.s           # Time for the change in velocity

a = (v_1 - v_0) / Deltat  # Deceleration of the car
m = 1_500.0 * ureg.kg     # Mass of the car
F = (m * a).to(ureg.kN)   # Average force experienced by the car in the crash
\( \displaystyle v_{1} = 0.00 \)
Final velocity of the car
\( \displaystyle v_{0} = 25.00\ \mathrm{m} / \mathrm{s} \)
Initial velocity of the car
\( \displaystyle \Delta t = 0.20\ \mathrm{s} \)
Time for the change in velocity
\( \displaystyle a = \frac{v_{1} - v_{0}}{\Delta t} = \frac{0.00 - 25.00\ \mathrm{m} / \mathrm{s}}{0.20\ \mathrm{s}} = -125.00\ \mathrm{m} / \mathrm{s}^{2} \)
Deceleration of the car
\( \displaystyle m = 1\,500.00\ \mathrm{kg} \)
Mass of the car
\( \displaystyle F = m\,a = \left( 1\,500.00\ \mathrm{kg} \right)\,\left( -125.00\ \mathrm{m} / \mathrm{s}^{2} \right) = -187.50\ \mathrm{kN} \)
Average force experienced by the car in the crash

Unit assignments are rendered with a small space between the quantity value and the unit symbols. All unit symbols are in upright Roman font, in accordance with CGPM.1

When quantities are multiplied, they are wrapped in parentheses, following the recommendation of SI Brochure.2

Unit Conversions

A pint.Quantity object’s to() or ito() methods calls are hidden by Rubberize.

%%tap
F = (m * a).to(ureg.kN)
F.to(ureg.kip)
\( \displaystyle F = m\,a = \left( 1\,500.00\ \mathrm{kg} \right)\,\left( -125.00\ \mathrm{m} / \mathrm{s}^{2} \right) = -187.50\ \mathrm{kN} \)
\( \displaystyle F = -187.50\ \mathrm{kN} = -42.15\ \mathrm{kip} \)

Contextual Multiplication

Multiplication of compound units matches Rubberize’s config for @use_contextual_mult.

%%tap
50.0 * (ureg.kN * ureg.m)  # @use_contextual_mult=True
50.0 * (ureg.kN * ureg.m)  # @use_contextual_mult=False
\( \displaystyle 50.00\ \mathrm{kN}\,\mathrm{m} \)
\( \displaystyle 50.00\ \mathrm{kN} \cdot \mathrm{m} \)

Inline Units

@use_inline_units controls whether units will be shown inline using a solidus (\(a/b\)) or using a fraction (\(\frac{a}{b}\)).

config.use_inline_units
True
%%tap --grid
12.0 * (ureg.kN / ureg.m)  # @use_inline_units=True
12.0 * (ureg.kN / ureg.m)  # @use_inline_units=False
\( \displaystyle 12.00\ \mathrm{kN} / \mathrm{m} \)
\( \displaystyle 12.00\ \frac{\mathrm{kN}}{\mathrm{m}} \)

If units are inline and the fraction has multiple denominator terms, negative exponents will be used instead of a solidus.

%%tap --grid
1.1 * (ureg.W / (ureg.m**2 * ureg.K))  # @use_inline_units=True
1.1 * (ureg.W / (ureg.m**2 * ureg.K))  # @use_inline_units=False
\( \displaystyle 1.10\ \mathrm{W}\,\mathrm{K}^{-1}\,\mathrm{m}^{-2} \)
\( \displaystyle 1.10\ \frac{\mathrm{W}}{\left(\mathrm{K}\,\mathrm{m}^{2}\right)} \)

Angular Degrees

Angular degree units are rendered with \(x^{\circ}\) instead of \(x\,\deg\).

%%tap
45.0 * ureg.deg
\( \displaystyle 45.00^{\circ} \)

Foot-Inch-Fraction

@use_fif_units controls whether to render inch and foot quantities in foot-inch-fraction (FIF) format:

config.use_fif_units
False

This feature is particularly useful for applications in US construction and other fields where fractional inch measurements are standard. You can also specify the displayed precision of the fractional part using the @fif_prec option. By default, this is set to 16, representing sixteenths of an inch.

Note

Only the rendering is affected in the conversion. The value still contains the full precision of the inch or foot quantity.

%%tap @use_fif_units=True
height = 8.41 * ureg.ft
width = 42.1875 * ureg.inch
\( \displaystyle \mathrm{height} = 8.41\ \mathrm{ft} \)
\( \displaystyle \mathrm{width} = 42.19\ \mathrm{in} \)

Degree-Minute-Second

Similar to FIF format, Rubberize supports rendering angle measurements as degree-minute-second (DMS) format using the @use_dms_units option.

config.use_dms_units
False
%%tap @use_dms_units=True
42.6969 * ureg.deg
\( \displaystyle 42.70^{\circ} \)

Custom Unit LaTeX

Rubberize allows definition of custom LaTeX representations for specific unit combinations using register_units_latex().

For example, to register custom LaTeX for bending moment (structural engineering), ensuring that the units are displayed as \(x\,\mathrm{N}\,\mathrm{m}\) instead of \(x\,\mathrm{m}\,\mathrm{N}\):

from rubberize import register_units_latex

# Register a custom LaTeX representation for bending moment
register_units_latex(r"\mathrm{N} \cdot \mathrm{m}", meter=1, newton=1)
%%tap
42.0 * (ureg.N * ureg.m)
\( \displaystyle 42.00\ \mathrm{N}\,\mathrm{m} \)

The same process can be done for defining LaTeX representations of custom units:

ureg.define("boop = 3 newton")
register_units_latex(r"\mathrm{_{b}oO^{P}!}", boop=1)
%%tap
a = 84.42 * ureg.boop
a.to(ureg.N)
\( \displaystyle a = 84.42\ \mathrm{_{b}oO^{P}!} \)
\( \displaystyle a = 84.42\ \mathrm{_{b}oO^{P}!} = 253.26\ \mathrm{N} \)

Footnotes

  1. CGPM, “Resolution 10 of the 22nd General Conference on Weights and Measures (2003),” Bureau International des Poids et Mesures (BIPM). Available: https://www.bipm.org/en/committees/cg/cgpm/22-2003/resolution-10↩︎

  2. BIPM, “The International System of Units (SI),” 9th edition, Section 5.4.6, Bureau International des Poids et Mesures, 2019. Available: https://www.bipm.org/en/publications/si-brochure↩︎