Physlib

Physlib.Units.Basic

Dimensions and unit

A unit in physics arises from choice of something in physics which is non-canonical. An example is the choice of translationally-invariant metric on the time manifold `TimeMan`.

A dimension is a property of a quantity related to how it changes with respect to a change in the unit.

The fundamental choices one has in physics are related to: - Time - Length - Mass - Charge - Temperature

(In fact temperature is not really a fundamental choice, however we leave this as a `TODO`.)

From these fundamental choices one can construct all other units and dimensions.

Implementation details

Units within Physlib are implemented with the following convention: - The fundamental units, and the choices they correspond to, are defined within the appropriate physics directory, in particular: - `Physlib/SpaceAndTime/Time/TimeUnit.lean` - `Physlib/SpaceAndTime/Space/LengthUnit.lean` - `Physlib/ClassicalMechanics/Mass/MassUnit.lean` - `Physlib/Electromagnetism/Charge/ChargeUnit.lean` - `Physlib/Thermodynamics/Temperature/TemperatureUnit.lean` - In this `Units` directory, we define the necessary structures and properties to work derived units and dimensions.

References

Zulip chats discussing units: - https://leanprover.zulipchat.com/#narrow/channel/479953-Physlib/topic/physical.20units - https://leanprover.zulipchat.com/#narrow/channel/116395-maths/topic/Dimensional.20Analysis.20Revisited/with/530238303

Note

A lot of the results around units is still experimental and should be adapted based on needs.

Other implementations of units

There are other implementations of units in Lean, in particular: 1. https://github.com/ATOMSLab/LeanDimensionalAnalysis/tree/main 2. https://github.com/teorth/analysis/blob/main/analysis/Analysis/Misc/SI.lean 3. https://github.com/ecyrbe/lean-units Each of these have their own advantages and specific use-cases. For example both (1) and (3) allow for or work in Floats, allowing computability and the use of `#eval`. This is currently not possible with the more theoretical implementation here in Physlib which is based exclusively on Reals.

Units

Types carrying dimensions

Dimensions are assigned to types with the following type-classes

  • `HasDim` for any type `M` with an associated dimension
  • `CarriesDimension` for a type that also has an instance of `MulAction ℝ≥0 M`

Terms of the current dimension

Given a type `M` which carries a dimension `d`, we are interested in elements of `M` which depend on a choice of units, i.e. functions `UnitChoices → M`.

We define both a proposition - `HasDimension f` which says that `f` scales correctly with units, and a type - `Dimensionful M` which is the subtype of functions which `HasDimension`.

30 declarations

definition

Scaling factor for a dimension dd between unit choices u1u_1 and u2u_2

Given two sets of unit choices u1u_1 and u2u_2, this function defines a monoid homomorphism from physical dimensions to the non-negative real numbers R0\mathbb{R}_{\ge 0}. For a dimension dd, it computes the scaling factor σR0\sigma \in \mathbb{R}_{\ge 0} used to convert a quantity of dimension dd from unit system u1u_1 to u2u_2. This factor is calculated as the product of the ratios of the fundamental units (length, time, mass, charge, and temperature) in u1u_1 and u2u_2, each raised to the power of the corresponding component in the dimension dd: σ=(u1,lengthu2,length)d.length(u1,timeu2,time)d.time(u1,massu2,mass)d.mass(u1,chargeu2,charge)d.charge(u1,tempu2,temp)d.temp\sigma = \left(\frac{u_{1, \text{length}}}{u_{2, \text{length}}}\right)^{d.\text{length}} \cdot \left(\frac{u_{1, \text{time}}}{u_{2, \text{time}}}\right)^{d.\text{time}} \cdot \left(\frac{u_{1, \text{mass}}}{u_{2, \text{mass}}}\right)^{d.\text{mass}} \cdot \left(\frac{u_{1, \text{charge}}}{u_{2, \text{charge}}}\right)^{d.\text{charge}} \cdot \left(\frac{u_{1, \text{temp}}}{u_{2, \text{temp}}}\right)^{d.\text{temp}} where the exponents d.id.i are the rational powers associated with each fundamental dimension.

theorem

Formula for the Scaling Factor of a Physical Dimension dd between Unit Systems u1u_1 and u2u_2

Given two sets of unit choices u1u_1 and u2u_2 and a physical dimension dd, the scaling factor dimScale(u1,u2,d)\text{dimScale}(u_1, u_2, d) is calculated as the product of the ratios of the fundamental units in u1u_1 and u2u_2, each raised to the power of the corresponding component in the dimension dd: dimScale(u1,u2,d)=(u1,lengthu2,length)d.length(u1,timeu2,time)d.time(u1,massu2,mass)d.mass(u1,chargeu2,charge)d.charge(u1,tempu2,temp)d.temp\text{dimScale}(u_1, u_2, d) = \left(\frac{u_{1, \text{length}}}{u_{2, \text{length}}}\right)^{d.\text{length}} \cdot \left(\frac{u_{1, \text{time}}}{u_{2, \text{time}}}\right)^{d.\text{time}} \cdot \left(\frac{u_{1, \text{mass}}}{u_{2, \text{mass}}}\right)^{d.\text{mass}} \cdot \left(\frac{u_{1, \text{charge}}}{u_{2, \text{charge}}}\right)^{d.\text{charge}} \cdot \left(\frac{u_{1, \text{temp}}}{u_{2, \text{temp}}}\right)^{d.\text{temp}} where ui,typeu_{i, \text{type}} represents the fundamental unit of a specific type (length, time, mass, charge, or temperature) in unit system uiu_i, and d.typed.\text{type} represents the rational exponent of that fundamental dimension in dd.

theorem

dimScale(u,u,d)=1\text{dimScale}(u, u, d) = 1 for identical unit systems

For any set of unit choices uu and any physical dimension dd, the scaling factor dimScale(u,u,d)\text{dimScale}(u, u, d) used to convert a quantity of dimension dd from unit system uu to itself is equal to 11.

theorem

The scaling factor for the dimensionless dimension 11 is 11

For any two systems of unit choices u1u_1 and u2u_2, the scaling factor associated with the identity dimension (dimensionless quantity) 11 is equal to 11.

theorem

dimScale(u1,u2,d)dimScale(u2,u3,d)=dimScale(u1,u3,d)\text{dimScale}(u_1, u_2, d) \cdot \text{dimScale}(u_2, u_3, d) = \text{dimScale}(u_1, u_3, d)

For any three sets of unit choices u1,u2,u3u_1, u_2, u_3 and any physical dimension dd, the scaling factor dimScale(u1,u2,d)R0\text{dimScale}(u_1, u_2, d) \in \mathbb{R}_{\ge 0} used to convert a quantity of dimension dd from unit system u1u_1 to u2u_2 satisfies the transitivity property: dimScale(u1,u2,d)dimScale(u2,u3,d)=dimScale(u1,u3,d)\text{dimScale}(u_1, u_2, d) \cdot \text{dimScale}(u_2, u_3, d) = \text{dimScale}(u_1, u_3, d)

theorem

dimScale(u1,u2,d)dimScale(u2,u1,d)=1\text{dimScale}(u_1, u_2, d) \cdot \text{dimScale}(u_2, u_1, d) = 1

For any two systems of unit choices u1u_1 and u2u_2 and any physical dimension dd, the product of the scaling factor dimScale(u1,u2,d)R0\text{dimScale}(u_1, u_2, d) \in \mathbb{R}_{\ge 0} (used to convert a quantity of dimension dd from unit system u1u_1 to u2u_2) and the scaling factor dimScale(u2,u1,d)\text{dimScale}(u_2, u_1, d) (used for the reverse conversion) is equal to 11: dimScale(u1,u2,d)dimScale(u2,u1,d)=1\text{dimScale}(u_1, u_2, d) \cdot \text{dimScale}(u_2, u_1, d) = 1

theorem

toReal(dimScale(u1,u2,d))toReal(dimScale(u2,u1,d))=1\text{toReal}(\text{dimScale}(u_1, u_2, d)) \cdot \text{toReal}(\text{dimScale}(u_2, u_1, d)) = 1

For any two systems of unit choices u1,u2u_1, u_2 and any physical dimension dd, let dimScale(u1,u2,d)R0\text{dimScale}(u_1, u_2, d) \in \mathbb{R}_{\ge 0} be the factor used to convert a quantity of dimension dd from unit system u1u_1 to u2u_2. The product of this scaling factor and its reverse counterpart dimScale(u2,u1,d)\text{dimScale}(u_2, u_1, d), when converted to real numbers, is equal to 11: toReal(dimScale(u1,u2,d))toReal(dimScale(u2,u1,d))=1\text{toReal}(\text{dimScale}(u_1, u_2, d)) \cdot \text{toReal}(\text{dimScale}(u_2, u_1, d)) = 1

theorem

The dimension scaling factor dimScale(u1,u2,d)\text{dimScale}(u_1, u_2, d) is non-zero

For any two systems of unit choices u1,u2UnitChoicesu_1, u_2 \in \text{UnitChoices} and any physical dimension dDimensiond \in \text{Dimension}, the scaling factor dimScale(u1,u2,d)R0\text{dimScale}(u_1, u_2, d) \in \mathbb{R}_{\ge 0} is non-zero. This scaling factor represents the product of the ratios of fundamental units in u1u_1 and u2u_2 raised to the powers specified by the dimension dd, used to convert physical quantities between the two unit systems.

theorem

dimScale(u1,u2,d)=dimScale(u2,u1,d)1\text{dimScale}(u_1, u_2, d) = \text{dimScale}(u_2, u_1, d)^{-1}

For any two systems of unit choices u1,u2UnitChoicesu_1, u_2 \in \text{UnitChoices} and any physical dimension dDimensiond \in \text{Dimension}, the scaling factor dimScale(u1,u2,d)R0\text{dimScale}(u_1, u_2, d) \in \mathbb{R}_{\ge 0} used to convert quantities of dimension dd from unit system u1u_1 to u2u_2 is the multiplicative inverse of the scaling factor for the reverse conversion. That is, dimScale(u1,u2,d)=(dimScale(u2,u1,d))1\text{dimScale}(u_1, u_2, d) = (\text{dimScale}(u_2, u_1, d))^{-1}

theorem

dimScale(u1,u2,d1)=dimScale(u2,u1,d)\text{dimScale}(u_1, u_2, d^{-1}) = \text{dimScale}(u_2, u_1, d)

For any two systems of unit choices u1,u2UnitChoicesu_1, u_2 \in \text{UnitChoices} and any physical dimension dDimensiond \in \text{Dimension}, the scaling factor for the inverse dimension d1d^{-1} from unit system u1u_1 to u2u_2 is equal to the scaling factor for the original dimension dd when converting from unit system u2u_2 to u1u_1. That is, dimScale(u1,u2,d1)=dimScale(u2,u1,d)\text{dimScale}(u_1, u_2, d^{-1}) = \text{dimScale}(u_2, u_1, d)

theorem

Scaling by `dimScale` is injective in MM

Let MM be a type equipped with a scalar action of the non-negative real numbers R0\mathbb{R}_{\ge 0}. For any two systems of unit choices u1,u2u_1, u_2 and any physical dimension dd, let σ=dimScale(u1,u2,d)R0\sigma = \text{dimScale}(u_1, u_2, d) \in \mathbb{R}_{\ge 0} be the scaling factor associated with converting quantities of dimension dd from unit system u1u_1 to u2u_2. For any two elements m1,m2Mm_1, m_2 \in M, the result of scaling by σ\sigma is equal if and only if the original elements are equal: σm1=σm2    m1=m2 \sigma \cdot m_1 = \sigma \cdot m_2 \iff m_1 = m_2

theorem

dimScale(u1,u2,d)>0\text{dimScale}(u_1, u_2, d) > 0

For any two systems of unit choices u1,u2UnitChoicesu_1, u_2 \in \text{UnitChoices} and any physical dimension dDimensiond \in \text{Dimension}, the scaling factor dimScale(u1,u2,d)R0\text{dimScale}(u_1, u_2, d) \in \mathbb{R}_{\ge 0} used to convert physical quantities of dimension dd from unit system u1u_1 to u2u_2 is strictly positive: dimScale(u1,u2,d)>0\text{dimScale}(u_1, u_2, d) > 0

definition

The International System of Units (SI) choices

The definition `UnitChoices.SI` represents the choice of base physical units corresponding to the International System of Units (SI). It is a structure of type `UnitChoices` where the fundamental units are assigned as follows: - The unit of length is meters. - The unit of time is seconds. - The unit of mass is kilograms. - The unit of charge is coulombs. - The unit of temperature is kelvin.

theorem

SI.length=meters\text{SI.length} = \text{meters}

In the International System of Units (SI\text{SI}), the base unit assigned to the length dimension is the meter: SI.length=LengthUnit.meters\text{SI.length} = \text{LengthUnit.meters}

theorem

SI.time=TimeUnit.secondsSI.time = TimeUnit.seconds

In the International System of Units (SISI), the fundamental unit chosen for the dimension of time is the second (TimeUnit.secondsTimeUnit.seconds).

theorem

The SI unit of mass is the kilogram

In the International System of Units (SI), the designated fundamental unit for the mass dimension is the kilogram.

theorem

The SI unit of charge is the coulomb

In the International System of Units (SI), the assigned base unit for physical charge is the coulomb.

theorem

The SI unit of temperature is Kelvin

In the International System of Units (SI), the base unit for temperature is defined as Kelvin.

definition

Prime-scaled SI unit system SISI'

`UnitChoices.SIPrimed` is a specific set of unit choices derived from the International System of Units (SI) where each base unit is scaled by a distinct prime number. It is defined as follows: - Length: 2×meters2 \times \text{meters} - Time: 3×seconds3 \times \text{seconds} - Mass: 5×kilograms5 \times \text{kilograms} - Charge: 7×coulombs7 \times \text{coulombs} - Temperature: 11×kelvin11 \times \text{kelvin} This configuration is primarily used to verify the dimensional correctness of physical expressions, as the prime scaling ensures that different combinations of base dimensions result in unique scaling factors.

theorem

Scaling factor for dimension dd from SISI to SISI' units

For any physical dimension dd, the scaling factor dimScale(SI,SI,d)\text{dimScale}(SI, SI', d) required to convert a quantity from the International System of Units (SISI) to the prime-scaled system (SISI') is given by the product: dimScale(SI,SI,d)=(21)d.length(31)d.time(51)d.mass(71)d.charge(111)d.temperature\text{dimScale}(SI, SI', d) = (2^{-1})^{d.\text{length}} \cdot (3^{-1})^{d.\text{time}} \cdot (5^{-1})^{d.\text{mass}} \cdot (7^{-1})^{d.\text{charge}} \cdot (11^{-1})^{d.\text{temperature}} where d.length,d.time,d.mass,d.charged.\text{length}, d.\text{time}, d.\text{mass}, d.\text{charge}, and d.temperatured.\text{temperature} are the rational components of the dimension dd. This factor arises because the base units in SISI' (meters, seconds, kilograms, coulombs, and kelvin) are scaled by the factors 2, 3, 5, 7, and 11, respectively, relative to SISI.

theorem

Scaling factor for dimension dd from SISI' to SISI units

For any physical dimension dd, the scaling factor dimScale(SI,SI,d)\text{dimScale}(SI', SI, d) required to convert a quantity from the prime-scaled system (SISI') to the International System of Units (SISI) is given by the product: dimScale(SI,SI,d)=2d.length3d.time5d.mass7d.charge11d.temperature\text{dimScale}(SI', SI, d) = 2^{d.\text{length}} \cdot 3^{d.\text{time}} \cdot 5^{d.\text{mass}} \cdot 7^{d.\text{charge}} \cdot 11^{d.\text{temperature}} where d.length,d.time,d.mass,d.charged.\text{length}, d.\text{time}, d.\text{mass}, d.\text{charge}, and d.temperatured.\text{temperature} are the rational components (exponents) of the dimension dd. This factor corresponds to the ratios of the base units in SISI' relative to SISI, which are scaled by factors of 2 (length), 3 (time), 5 (mass), 7 (charge), and 11 (temperature).

definition

ff is consistent with dimension dimM\text{dim} M

Let MM be a type that carries a physical dimension, denoted as d=dimMd = \text{dim} M. A function f:UnitChoicesMf: \text{UnitChoices} \to M, representing a quantity whose value depends on the chosen system of units, satisfies the property **HasDimension** if it scales correctly under a change of units. Specifically, for any two unit choices u1,u2UnitChoicesu_1, u_2 \in \text{UnitChoices}, the value of the function at u2u_2 must relate to the value at u1u_1 by: f(u2)=dimScale(u1,u2,d)f(u1)f(u_2) = \text{dimScale}(u_1, u_2, d) \cdot f(u_1) where dimScale(u1,u2,d)R0\text{dimScale}(u_1, u_2, d) \in \mathbb{R}_{\ge 0} is the scaling factor associated with dimension dd for the transition from unit system u1u_1 to u2u_2, and the multiplication represents the action of non-negative reals on MM.

theorem

`HasDimension f` iff ff scales by `dimScale` for the dimension of MM

Let MM be a type that carries a physical dimension, denoted as dimM\text{dim} M. For any function f:UnitChoicesMf: \text{UnitChoices} \to M, the property **HasDimension** ff holds if and only if for all unit choices u1,u2UnitChoicesu_1, u_2 \in \text{UnitChoices}, the value of the function at u2u_2 is related to the value at u1u_1 by the scaling factor associated with the dimension of MM: f(u2)=dimScale(u1,u2,dimM)f(u1)f(u_2) = \text{dimScale}(u_1, u_2, \text{dim} M) \cdot f(u_1) where dimScale(u1,u2,dimM)R0\text{dimScale}(u_1, u_2, \text{dim} M) \in \mathbb{R}_{\ge 0} is the scaling factor for the transition from unit system u1u_1 to u2u_2, and the multiplication represents the action of non-negative reals on MM.

definition

The type of dimensionful quantities of type MM

Let MM be a type that carries a physical dimension, denoted as dimM\text{dim} M. The type **Dimensionful MM** is defined as the set of functions f:UnitChoicesMf: \text{UnitChoices} \to M that are consistent with the dimension of MM. Specifically, a function ff belongs to this type if, for all pairs of unit choices u1,u2UnitChoicesu_1, u_2 \in \text{UnitChoices}, it satisfies the scaling relation: f(u2)=dimScale(u1,u2,dimM)f(u1)f(u_2) = \text{dimScale}(u_1, u_2, \text{dim} M) \cdot f(u_1) where dimScale(u1,u2,dimM)R0\text{dimScale}(u_1, u_2, \text{dim} M) \in \mathbb{R}_{\ge 0} is the scaling factor associated with the dimension of MM for the transition from the unit system u1u_1 to u2u_2, and the multiplication denotes the action of non-negative reals on MM.

instance

Coercion from **Dimensionful MM** to functions UnitChoicesM\text{UnitChoices} \to M

For any type MM that carries a physical dimension, an element ff of the type **Dimensionful MM** can be treated as a function from the set of unit choices UnitChoices\text{UnitChoices} to MM. This coercion allows a dimensionful quantity to be evaluated at a specific system of units uUnitChoicesu \in \text{UnitChoices} to return its value in MM.

theorem

f1.val=f2.val    f1=f2f_1.\text{val} = f_2.\text{val} \implies f_1 = f_2 for Dimensionful Quantities

Let MM be a type that carries a physical dimension. For any two dimensionful quantities f1,f2Dimensionful Mf_1, f_2 \in \text{Dimensionful } M, if their underlying functions f1,f2:UnitChoicesMf_1, f_2: \text{UnitChoices} \to M are equal, then the dimensionful quantities themselves are equal (f1=f2f_1 = f_2).

instance

R0\mathbb{R}_{\ge 0}-action on Dimensionful M\text{Dimensionful } M

Let MM be a type that carries a physical dimension. The space of dimensionful quantities of type MM, denoted as Dimensionful M\text{Dimensionful } M, is equipped with a scalar action by the non-negative real numbers R0\mathbb{R}_{\ge 0}. For any scalar aR0a \in \mathbb{R}_{\ge 0} and any dimensionful quantity fDimensionful Mf \in \text{Dimensionful } M, the action afa \cdot f is defined pointwise for each choice of units uUnitChoicesu \in \text{UnitChoices} as: (af)(u)=af(u)(a \cdot f)(u) = a \cdot f(u) where the right-hand side is the action of R0\mathbb{R}_{\ge 0} on MM. This operation satisfies the axioms of a multiplicative action: 1f=f1 \cdot f = f and (ab)f=a(bf)(a \cdot b) \cdot f = a \cdot (b \cdot f).

theorem

(af)(u)=af(u)(a \cdot f)(u) = a \cdot f(u) for dimensionful quantities

Let MM be a type that carries a physical dimension. For any non-negative real number aR0a \in \mathbb{R}_{\ge 0}, any dimensionful quantity fDimensionful Mf \in \text{Dimensionful } M (represented as a function from unit choices to MM), and any specific choice of units uUnitChoicesu \in \text{UnitChoices}, the value of the scaled dimensionful quantity afa \cdot f at uu is equal to the scalar aa acting on the value of ff at uu: (af)(u)=af(u)(a \cdot f)(u) = a \cdot f(u) where the multiplication on the right-hand side is the action of R0\mathbb{R}_{\ge 0} on the type MM.

definition

Equivalence MDimensionful MM \simeq \text{Dimensionful } M relative to unit choice uu

Let MM be a type that carries a physical dimension d=dimMd = \text{dim} M. Given a specific system of units uUnitChoicesu \in \text{UnitChoices}, there exists an equivalence (bijection) MDimensionful MM \simeq \text{Dimensionful } M between the type MM and the type of functions that scale correctly under a change of units. An element mMm \in M corresponds to the dimensionful quantity f:UnitChoicesMf: \text{UnitChoices} \to M defined by: f(u)=dimScale(u,u,d)mf(u') = \text{dimScale}(u, u', d) \cdot m where dimScale(u,u,d)\text{dimScale}(u, u', d) is the scaling factor for dimension dd when transitioning from unit system uu to uu'. Conversely, a dimensionful quantity ff is mapped to the value it takes in the unit system uu, given by f(u)f(u).

theorem

Evaluating a dimensionful quantity from u1u_1 in u2u_2 equals dimScale(u1,u2,dim M)m\text{dimScale}(u_1, u_2, \text{dim } M) \cdot m

Let MM be a type that carries a physical dimension d=dim Md = \text{dim } M. For any two systems of unit choices u1,u2UnitChoicesu_1, u_2 \in \text{UnitChoices} and any value mMm \in M, let ff be the dimensionful quantity (an element of the type **Dimensionful MM**) that corresponds to mm in the unit system u1u_1. The value of ff when evaluated at the unit system u2u_2 is given by: f(u2)=dimScale(u1,u2,d)mf(u_2) = \text{dimScale}(u_1, u_2, d) \cdot m where dimScale(u1,u2,d)R0\text{dimScale}(u_1, u_2, d) \in \mathbb{R}_{\ge 0} is the scaling factor associated with dimension dd for the transition from unit system u1u_1 to u2u_2, and the multiplication denotes the action of non-negative reals on MM.