Skip to content

Possible inconsistency in how convert_to_unit handles null vectors in Cartesian vs polar coordinates #899

@swagat-mishra28

Description

@swagat-mishra28

Hi,

While reviewing the implementation of convert_to_unit() in movement/utils/vector.py, I noticed a potential difference in how null vectors are handled between the Cartesian and polar coordinate branches.

In the Cartesian branch the unit vectors are computed as:

return data / compute_norm(data)

If a vector is (0,0), its norm becomes 0, which leads to a division-by-zero operation. In practice this relies on NumPy’s behaviour to produce NaN values.

In the polar branch, however, null vectors are handled explicitly:

new_data = xr.where(data.sel(space_pol="rho") == 0, np.nan, data)

Since the docstring states that the unit vector for a null vector is undefined, I was wondering whether the Cartesian branch should also explicitly handle zero-norm vectors for clarity and consistency with the polar implementation.

For example, something like:

norm = compute_norm(data)
unit = data / norm
unit = xr.where(norm == 0, np.nan, unit)

Would it make sense to explicitly handle this case, or is the current behaviour (relying on NumPy division) the intended design?

Happy to open a PR if making the behaviour explicit would be useful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    🤔 Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions