Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a property to access vector dimensionality #174

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/coordinax/_coordinax/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 7 additions & 3 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"""Return a vector."""
raise NotImplementedError

Expand All @@ -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

Expand Down
Loading