Units and measurements
July 27, 2018
units
clojure
Introduction
Properties
For a system one can define properties that describe the system in some sense. For example consider the system of the display you are reading this on. This system has many qualitative characteristics such as how large it is, how luminous it is, how often it refreshes the displayed image etc. Some of these system properties are also measurable:
A system property is measurable if by using a reproducible process, one can associate it with a real number.
For example the properties of a physical system (the physical properties) are all measurable. Here we will examine only measurable properties, so from now on a property is implied to be measurable.
One can define properties in terms of other properties of the system. For example on a physical system one can define pressure to be: force per unit area; force on the other hand can be defined in terms of other properties such as length, mass and time.
A system property is fundamental if it cannot be described in terms of other properties of the system.
Units
From the early days humans used a “comparison process” in order to quantify properties such as length, mass etc. This comparison process involves the existence of what we call a unit of measurement.
A unit of measurement is an arbitrarily defined quantity of a property that can be obtained by a reproducible process.
Note: Because a unit of measurement is arbitrarily defined, many different units of measurement that are associated with the same physical property have been used over the years. This led to the need for defining conversion rules between those units and groupings of units that we call systems of measurement. Well known and used systems of measurement include the International System of Units, the Imperial System, the USCS and several others. The above systems of measurement are all “in use” and that has led to disasters like the destruction of NASAs Mars Climate Orbiter and several airplane accidents.
If a unit of measurement is associated with a fundamental property then it is called a base unit of measurement. All other units are called derived units of measurement.
For any system one can define its fundamental properties, for example for a physical system those are:
- Length ($L$)
- Mass ($M$)
- Time ($T$)
- Temperature ($\Theta$)
- Electric charge ($Q$)
- Amount of substance ($N$)
- Luminosity ($J$)
Measurement
The measurement process of a property is done by comparing the occuring quantity with the unit of measurement. We call the result of this process a measurement and it is tied to the unit that was used for comparison.
Representation
We will denote a unit by bold letters. For example $\textbf{ft}$ is going to denote a unit. Every unit $\textbf{u}$ can be expressed as a combination (see derivation) of the base units and thus we can define its dimensions to be the way a unit is composed from those base units.
The dimension of a unit $\textbf{u}$ is a $n$-element vector \begin{equation} \vec{d}_{\textbf{u}} = \begin{bmatrix} d_{u1} \dots d_{un} \end{bmatrix} \end{equation} with elements $d_{ui} \in \mathbb{Z}$ that are the exponents in the way the unit has been composed from the base units.
A unit $\textbf{u}$ is dimensionally equivalent to another unit $\textbf{v}$ iff $\vec{d}_{\textbf{u}} = \vec{d}_{\textbf{v}}$ and we write \begin{equation} \textbf{u} \sim \textbf{v} \tag{d.1} \label{dim.eq} \end{equation} The binary relation $\sim$ is an equivalence relation in the set of all units $\mathbb{U}$.
Note: One could say that any two dimensionally equivalent units measure the same system property but that is not true. Consider for example that torque and energy have the same dimensions but are two very different physical properties.
We will represent a unit of measurement $\textbf{u}$ as a pair of a function $u_{\textbf{v}}$ that converts the magnitude of a measurement, expressed in this unit, to a magnitude expressed in another unit $\textbf{v}$, that we take as a reference unit, together with its dimensions:
The function $u_{\textbf{v}}$ is called the conversion function from $\textbf{u}$ to $\textbf{v}$.
The conversion function
For our further discussion we state some axioms / assumptions about the conversion function.
This axiom stems from the fact that generally a measurement is just a magnitude expressed in some units, so the dependent variables of $u_\textbf{v}$ should only be the magnitude of a measurement.
The domain $D_{u_\textbf{v}}$ and the range $R_{u_\textbf{v}}$ of the conversion function $u_\textbf{v}$ should be subsets of the real numbers because the magnitude of a measurement is a real number.
Axiom $\ref{conv.fun.axiom.2}$ exists because conversions from unit $\textbf{u}$ to unit $\textbf{v}$ should be reversible, therefore $u^{-1}_{\textbf{v}}$ should always exist.
Derivation
Derivation of units from other units is accomplished through the definition of certain relations between units of measurement.
It is known that for a differentiable function $f(x)$ that $\delta f(x) = f^{\prime}(x)\delta x$, so if we pose the extra restriction that the conversion function is differentiable (actually infinitely differentiable) we can define:
and therefore
where $m = \frac{\delta x}{\delta y}$ is the magnitude of the measurement expressed in $\textbf{c}$. Now, taking into account $\ref{conv.fun.axiom.1}$ (i.e. a conversion function should be unary), we have from $\ref{relative.change.conv.fun}$ that
Applying the same for the $y$ variable we have that
What the above say is that given our assumptions / restrictions about a conversion function and in order to be able to define the relative change $\ref{relative.change.def}$ (or in fact any derived unit) a conversion function from $\textbf{u}$ to $\textbf{v}$ should be of the form
where $c \in \mathbb{R}^*, d \in \mathbb{R}$.
Therefore given that
relation $\ref{relative.change.conv.fun}$ becomes
Following a similar approach with the relative change, one can conclude, given $\ref{conversion.fun}$, that for the conversion function $\delta a_{\textbf{v}} \delta b_{\textbf{w}}$ the following is true
Again, taking into account $\ref{conversion.fun}$ one can derive that the conversion function $c_{\textbf{v}^k}$ is
Note: The $k$ exponential change could be derived immediatelly from the multiplicative change applied $k$ times on the same unit.
Lastly we define the prefix of a unit, purely as a scaling operation onto the conversion function
Let $p$ be a symbol that has an associated factor $|p| \in \mathbb{R}^*$. The prefix $p$ of a unit $\textbf{a}$ is a new unit $\textbf{c}$:
An example metric prefix is $T$ (tera) with an associated factor of $10^{12}$.
Implementation
Let’s represent a unit with a 3-element vector. The first element is the dimensions of the unit, the second the slope of the conversion function and the third the y-intercept of the conversion function $\ref{conversion.fun.gen}$
(defn make-unit
([dimension slope]
(make-unit dimension slope 0))
([dimension slope y-intercept]
^{:type ::unit}
[dimension slope y-intercept]))
Next we define the selectors dimensions
, slope
, y-intercept
and the conv-fn
, inv-conv-fn
which are the conversion function for a unit and its inverse (see $\ref{conversion.fun.gen}$)
(defn dimensions
"The dimensions vector of a unit."
[unit]
(get unit 0))
(defn slope
[unit]
(get unit 1))
(defn y-intercept
[unit]
(get unit 2))
(defn conv-fn
"The conversion function of a unit."
[unit]
(fn [x] (+ (* x (slope unit)) (y-intercept unit))))
(defn inv-conv-fn
"The inverse conversion function of a unit."
[unit]
(fn [x] (/ (- x (y-intercept unit)) (slope unit))))
It is trivial now to implement the operations $\ref{relative.change.def}, \ref{multiplicative.change.def}, \ref{exponential.change.def}$ and $\ref{prefix.def}$
(defn div
"The relative change operation."
([u] u)
([u1 u2]
(make-unit (mapv - (dimensions u1) (dimensions u2))
(/ (slope u1) (slope u2))))
([u1 u2 & rest]
(reduce div (div u1 u2) rest)))
(defn mult
"The mutliplicative change operation."
([u] u)
([u1 u2]
(make-unit (mapv + (dimensions u1) (dimensions u2))
(* (slope u1) (slope u2))))
([u1 u2 & rest]
(reduce mult (mult u1 u2) rest)))
(defn exp
"The k exponentiation operation."
([u k]
(make-unit (mapv #(* % k) (dimensions u))
(math/expt (slope u) k))))
(defn prefix
"The prefix operation."
[u p]
(make-unit (dimensions u) (* (slope u) p) (y-intercept u)))
In order to be able to convert a magnitude from one unit to another we need a predicate function that tests whether two units are dimensionally equivalent $\ref{dim.eq}$ (again, note that this condition is only necessary and not sufficient for whether the conversion can be performed):
(defn dim-eq
"Check if the units are dimensionally equivalent."
([u] true)
([u1 u2] (= (dimensions u1) (dimensions u2))))
What remains now is a function that converts a magnitude from one unit to another unit. In order to be able to do this both the units conversion functions must convert a magnitude to a same reference unit. For example later when we see how all these apply to the physical system we choose the SI as the reference system.
(defn convert
"Convert a magnitude from a unit to another. Throw exception if not dimensionally equivalent."
[magnitude u1 u2]
(if (dim-eq u1 u2)
(-> magnitude
((conv-fn u1))
((inv-conv-fn u2)))
(throw (Exception. "Units have different dimensions."))))
The physical system - an example
As an example we define units that measure physical properties and we perform conversions between them. We use the SI as a reference system, which means that all of the conversion functions are defined to convert the magnitude from the unit to the corresponding SI unit.
(def prefixes
{:Y 1e24
:Z 1e21
:E 1e18
:P 1e15
:T 1e12
:G 1e9
:M 1e6
:k 1e3
:h 1e2
:da 1e1
:d 1e-1
:c 1e-2
:m 1e-3
:μ 1e-6
:n 1e-9
:p 1e-12
:f 1e-15
:a 1e-18
:z 1e-21
:y 1e-24})
(def meter (make-unit [1 0 0 0 0 0 0] 1))
(def foot (make-unit [1 0 0 0 0 0 0] 0.3048))
(def inch (make-unit [1 0 0 0 0 0 0] 0.0254))
(def yard (make-unit [1 0 0 0 0 0 0] 0.9144))
(def mile (make-unit [1 0 0 0 0 0 0] 1609.344))
(def gram (make-unit [0 1 0 0 0 0 0] 1e-3))
(def pound (make-unit [0 1 0 0 0 0 0] 0.45359237))
(def ounce (make-unit [0 1 0 0 0 0 0] 0.0283495231))
(def second (make-unit [0 0 1 0 0 0 0] 1))
(def minute (make-unit [0 0 1 0 0 0 0] 60))
(def hour (make-unit [0 0 1 0 0 0 0] 3600))
(def day (make-unit [0 0 1 0 0 0 0] 86400))
(def kelvin (make-unit [0 0 0 1 0 0 0] 1))
(def celsius (make-unit [0 0 0 1 0 0 0] 1 273.15))
(def fahrenheit (make-unit [0 0 0 1 0 0 0] 5/9 (* 5/9 459.67)))
(def joule (make-unit [2 1 -2 0 0 0 0] 1))
(def calorie (make-unit [2 1 -2 0 0 0 0] 4.184))
(def newton (make-unit [1 1 -2 0 0 0 0] 1))
(def dyn (make-unit [1 1 -2 0 0 0 0] 1e-5))
(def kilopond (make-unit [1 1 -2 0 0 0 0] 9.80665))
(def poundal (make-unit [1 1 -2 0 0 0 0] 0.138255))
(def poundforce (make-unit [1 1 -2 0 0 0 0] 4.448222))
(def watt (make-unit [2 1 -3 0 0 0 0] 1))
(def hertz (make-unit [0 0 -1 0 0 0 0] 1))
(def km-per-hr (div (prefix meter (prefixes :k)) hour))
(def miles-per-hour (div mile hour))
Having defined these few units we can calculate conversions between them. For example we can convert 3.5 ounces to kg:
(convert 3.5 ounce (prefix gram (prefixes :k))) ;; = 0.09922333085
We can convert something more exotic like heat transfer coefficient measurements. Say that we have $43\frac{W}{m^2K}$ and we want to convert it to $\frac{kcal}{ft^2h^{\circ}C}$. First we define the units
(def watt-per-sq-meter-kelvin (div watt (mult (exp meter 2) kelvin)))
(def kcal-per-sq-foot-celsius-hour (div (prefix calorie (prefixes :k))
(mult (exp foot 2)
(mult hour celsius))))
and then we perform the conversion
(convert 43 watt-per-sq-meter-kelvin kcal-per-sq-foot-celsius-hour) ;; = 3.437235
Conclusion
We presented a unit as a mathematical object and defined several operations that produce other derived units. Equations $\ref{relative.change.def}, \ref{multiplicative.change.def}, \ref{exponential.change.def}$ and $\ref{prefix.def}$ give us all the necessary tools to derive any kind of unit as long as its conversion function is given by $\ref{conversion.fun.gen}$.