|
1 | | -""" |
2 | | -pint_array |
3 | | -~~~~~~~~~~ |
4 | | -
|
5 | | -Pint interoperability with array API standard arrays. |
6 | | -""" |
| 1 | +"""Quantities with array API standard arrays.""" |
7 | 2 |
|
8 | 3 | import collections |
9 | 4 | import contextlib |
|
20 | 15 | from pint.facets.plain import MagnitudeT, PlainQuantity |
21 | 16 |
|
22 | 17 | __version__ = "0.0.1.dev0" |
23 | | -__all__ = ["__version__", "pint_namespace"] |
| 18 | +__all__ = ["__version__", "quantity_namespace"] |
24 | 19 |
|
25 | 20 |
|
26 | 21 | def __getattr__(name): |
27 | 22 | try: |
28 | 23 | xp = importlib.import_module(name) |
29 | | - mod = pint_namespace(xp) |
| 24 | + mod = quantity_namespace(xp) |
30 | 25 | sys.modules[f"marray.{name}"] = mod |
31 | 26 | return mod |
32 | 27 | except ModuleNotFoundError as e: |
33 | 28 | raise AttributeError(str(e)) from None |
34 | 29 |
|
35 | 30 |
|
36 | | -def pint_namespace(xp): |
37 | | - mod = types.ModuleType(f"pint({xp.__name__})") |
| 31 | +def quantity_namespace(xp): |
| 32 | + mod = types.ModuleType(f"quantity({xp.__name__})") |
38 | 33 |
|
39 | 34 | class ArrayQuantity(Generic[MagnitudeT], PlainQuantity[MagnitudeT]): |
40 | 35 | def __init__(self, *args, **kwargs): # noqa: ARG002 |
@@ -695,11 +690,10 @@ def fun(x, /, *args, func_str=func_str, **kwargs): |
695 | 690 |
|
696 | 691 | setattr(mod, func_str, fun) |
697 | 692 |
|
698 | | - |
699 | 693 | def _sqrt(x, /, *args, **kwargs): |
700 | 694 | x = asarray(x) |
701 | 695 | magnitude = xp.asarray(x.magnitude, copy=True) |
702 | | - magnitude = getattr(xp, "sqrt")(magnitude, *args, **kwargs) |
| 696 | + magnitude = xp.sqrt(magnitude, *args, **kwargs) |
703 | 697 | return ArrayUnitQuantity(magnitude, x.units**0.5) |
704 | 698 |
|
705 | 699 | mod.sqrt = _sqrt |
@@ -785,9 +779,7 @@ def pow(x1, x2, /, *args, **kwargs): |
785 | 779 | if x1.unitless: |
786 | 780 | units = "dimensionless" |
787 | 781 | elif not xp.all(x2_magnitude == x2_first_elem_magnitude): |
788 | | - extra_msg = ( |
789 | | - "The first array must be unitless, or the exponent must be a scalar or an array of all the same value." |
790 | | - ) |
| 782 | + extra_msg = "The first array must be unitless, or the exponent must be a scalar or an array of all the same value." |
791 | 783 | raise DimensionalityError( |
792 | 784 | x2.units, |
793 | 785 | "dimensionless", |
@@ -837,9 +829,9 @@ def matrix_transpose(x): |
837 | 829 | def _sort(x, /, **kwargs): |
838 | 830 | x = asarray(x) |
839 | 831 | magnitude = xp.asarray(x.magnitude, copy=True) |
840 | | - xp_func = getattr(xp, "sort") |
| 832 | + xp_func = xp.sort |
841 | 833 | magnitude = xp_func(magnitude, **kwargs) |
842 | | - units = x.units |
| 834 | + units = x.units |
843 | 835 | return ArrayUnitQuantity(magnitude, units) |
844 | 836 |
|
845 | 837 | mod.sort = _sort |
@@ -960,7 +952,7 @@ def clip(x, /, min=None, max=None): |
960 | 952 | preface = [ |
961 | 953 | "The following is the documentation for the corresponding " |
962 | 954 | f"attribute of `{xp.__name__}`.", |
963 | | - "The behavior on pint-wrapped arrays is the same for dimensionless " |
| 955 | + "The behavior on quantity-wrapped arrays is the same for dimensionless " |
964 | 956 | "quantities, and may differ for quantities with units.\n\n", |
965 | 957 | ] |
966 | 958 | preface = "\n".join(preface) |
|
0 commit comments