Skip to content

Commit 9195af7

Browse files
committed
feat: decomposition
Signed-off-by: Nathaniel Starkman <[email protected]>
1 parent a9baedd commit 9195af7

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

src/metrology_apis/__init__.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
"""Metrology APIs."""
22

3-
from typing import Final, Protocol, Self, override, runtime_checkable
3+
from typing import TYPE_CHECKING, Final, Protocol, Self, override, runtime_checkable
44

55
import optype as op
66

7+
if TYPE_CHECKING:
8+
import fractions
9+
10+
711
__version__: Final = "0.0.1.dev0"
812
__all__ = ["__version__", "Dimension", "Quantity", "Unit"]
913

@@ -17,6 +21,31 @@ def __pow__(self, other: int, /) -> Self: ...
1721
def __rmul__(self, other: Self, /) -> Self: ...
1822
def __rtruediv__(self, other: Self, /) -> Self: ...
1923

24+
def decompose(self) -> "dict[Dimension, int | fractions.Fraction]":
25+
"""Decompose the dimension into its base dimensions and exponents.
26+
27+
Notes
28+
-----
29+
By far the most common dimension system is (Length, Mass, Time, Electric
30+
Current, Temperature, Amount of Substance, Luminous Intensity).
31+
This method will decompose the dimension into a dictionary of these
32+
base dimensions and their respective exponents.
33+
34+
Examples
35+
--------
36+
As this is an API protocol, no runtime examples are possible. The
37+
following is an illustrative example:
38+
39+
>>> import metrology as u
40+
41+
>>> dim = u.Length / u.Time
42+
43+
>>> dim.decompose()
44+
{Length: 1, Time: -1}
45+
46+
"""
47+
...
48+
2049

2150
@runtime_checkable
2251
class Unit(Protocol):

0 commit comments

Comments
 (0)