Skip to content

Allow negation of representations with differentials #10987

Closed

Description

@mhvk

Currently, negation of representations that include differentials leads to an error, even though it is quite clearly defined for any derivative. This is a bit annoying since it does not easily allow a position offset with a velocity offset to be negated leading to unnecessary logic to calculate either the offset from the sun to the barycentre or its reverse (see #10970)

Activity

  1. ayshih commented on Nov 5, 2020

    @ayshih
    Contributor

    While I believe that the negation of a Cartesian representation/differential is uncontroversial, I suspect that it may be controversial for other representations, e.g., cylindrical:

    >>> import astropy.coordinates.representation as r
    >>> import astropy.units as u
    
    >>> pos = r.CartesianRepresentation(1, 0, 0)*u.m
    >>> vel = r.CartesianDifferential(0, 1, 0)*u.m/u.s
    
    # The velocity converted to cylindrical components
    >>> vel.represent_as(r.CylindricalDifferential, pos)
    <CylindricalDifferential (d_rho, d_phi, d_z) in (m / s, rad / s, m / s)
        (0., 1., 0.)>
    
    # If cylindrical velocity is directly negated, it negates the cylindrical components
    # The sign for `d_phi` has changed ==> the circulation is reversed
    >>> -vel.represent_as(r.CylindricalDifferential, pos)
    <CylindricalDifferential (d_rho, d_phi, d_z) in (m / s, deg / s, m / s)
        (-0., -1., -0.)>
    
    # The natural negation of the Cartesian representation/differential is {-pos, -vel}
    # We then convert the new velocity to cylindrical components
    # Note that the sign for `d_phi` has not changed ==> the circulation is preserved
    >>> (-vel).represent_as(r.CylindricalDifferential, -pos)
    <CylindricalDifferential (d_rho, d_phi, d_z) in (m / s, rad / s, m / s)
        (1.2246468e-16, 1., -0.)>

    That is, the "right" answer for a cylindrical representation/differential depends on whether you think that the circulation should be preserved/reversed. Note that expecting the circulation to be reversed when working in cylindrical could be weird because walking through Cartesian would always preserve the circulation.

  2. mhvk commented on Nov 5, 2020

    @mhvk
    ContributorAuthor

    @ayshih - agreed one has to be a bit careful, but I think one can make things clear and consistent by insisting that for any operation, the answer does not matter in what representation it is done, i.e., there is a default that converts to cartesian, applies the operation, and the converts back, and there are also shortcuts that give the same result. Indeed, this is how all the operations are implemented right now on representations without differentials. I think the problem with differentials is that for higher order ones, it might be less clear on how to proceed - but that is basically because we cannot yet transform them properly to cartesian in the first place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions