From 99e91b755834e4125e516607a2e071b19a7c147d Mon Sep 17 00:00:00 2001 From: Adrian Price-Whelan Date: Mon, 2 Sep 2024 14:44:04 -0400 Subject: [PATCH 1/3] add property to get dimensionality of a vector --- src/coordinax/_coordinax/base.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/coordinax/_coordinax/base.py b/src/coordinax/_coordinax/base.py index de9c5158..5f1ae11d 100644 --- a/src/coordinax/_coordinax/base.py +++ b/src/coordinax/_coordinax/base.py @@ -321,6 +321,20 @@ def T(self) -> "Self": # noqa: N802 """ return replace(self, **{k: v.T for k, v in field_items(self)}) + @property + def dim(self) -> int: + """The dimensionality of the vector. + + Examples + -------- + We assume the following imports: + + >>> from unxt import Quantity + >>> import coordinax as cx + + """ + return len(self.components) + # --------------------------------------------------------------- # Methods From 414e20bfa935ec6e230275af587db43fc1b3819c Mon Sep 17 00:00:00 2001 From: Adrian Price-Whelan Date: Mon, 2 Sep 2024 14:45:35 -0400 Subject: [PATCH 2/3] add example --- src/coordinax/_coordinax/base.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/coordinax/_coordinax/base.py b/src/coordinax/_coordinax/base.py index 5f1ae11d..9cec27d2 100644 --- a/src/coordinax/_coordinax/base.py +++ b/src/coordinax/_coordinax/base.py @@ -332,6 +332,12 @@ def dim(self) -> int: >>> from unxt import Quantity >>> import coordinax as cx + We can get the dimensionality of a vector: + + >>> vec = cx.CartesianPosition2D.constructor([1, 2], "m") + >>> vec.dim + 2 + """ return len(self.components) From 79e2ddefcb607a2bd81cde859aacdc67b41749a6 Mon Sep 17 00:00:00 2001 From: Adrian Price-Whelan Date: Mon, 2 Sep 2024 14:45:49 -0400 Subject: [PATCH 3/3] add a weird test of vector dimensionality --- tests/test_base.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/test_base.py b/tests/test_base.py index d558cfcf..e06978d5 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -101,6 +101,10 @@ def test_shape(self, vector): *(getattr(vector, c).shape for c in vector.components) ) + def test_dim(self, vector): + """Test :meth:`AbstractVector.dim`.""" + assert f"{vector.dim}D" in vector.__class__.__name__ + def test_flatten(self, vector): """Test :meth:`AbstractVector.flatten`.""" # Test input vector @@ -183,7 +187,7 @@ class AbstractPositionTest(AbstractVectorTest): """Test :class:`coordinax.AbstractPosition`.""" @pytest.fixture(scope="class") - def vector(self) -> AbstractPosition: # noqa: PT004 + def vector(self) -> AbstractPosition: """Return a vector.""" raise NotImplementedError @@ -206,12 +210,12 @@ class AbstractVelocityTest(AbstractVectorTest): """Test :class:`coordinax.AbstractVelocity`.""" @pytest.fixture(scope="class") - def vector(self) -> AbstractPosition: # noqa: PT004 + def vector(self) -> AbstractPosition: """Return a vector.""" raise NotImplementedError @pytest.fixture(scope="class") - def difntl(self) -> AbstractVelocity: # noqa: PT004 + def difntl(self) -> AbstractVelocity: """Return a vector.""" raise NotImplementedError