Skip to content

Commit 9f5b897

Browse files
committed
feat: decomposition
Signed-off-by: Nathaniel Starkman <[email protected]>
1 parent 05af87c commit 9f5b897

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

2150
@runtime_checkable
2251
class Unit(Protocol):

0 commit comments

Comments
 (0)