diff --git a/src/coordinax/_coordinax/base.py b/src/coordinax/_coordinax/base.py index de9c5158..9cec27d2 100644 --- a/src/coordinax/_coordinax/base.py +++ b/src/coordinax/_coordinax/base.py @@ -321,6 +321,26 @@ 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 + + We can get the dimensionality of a vector: + + >>> vec = cx.CartesianPosition2D.constructor([1, 2], "m") + >>> vec.dim + 2 + + """ + return len(self.components) + # --------------------------------------------------------------- # Methods 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