Skip to content

Broadcasting quantities #7832

@drdavella

Description

@drdavella

Currently, when broadcasting a Quantity, the result is a bare ndarray. Consider the following basic example:

>>> import numpy as np
>>> from astropy import units as u
>>> q = [x for x in range(10)] * u.AA
>>> q
<Quantity [0., 1., 2., 3., 4., 5., 6., 7., 8., 9.] Angstrom>
>>> np.broadcast_to(q, (4, 10))
array([[0., 1., 2., 3., 4., 5., 6., 7., 8., 9.],
       [0., 1., 2., 3., 4., 5., 6., 7., 8., 9.],
       [0., 1., 2., 3., 4., 5., 6., 7., 8., 9.],
       [0., 1., 2., 3., 4., 5., 6., 7., 8., 9.]])

This isn't a huge deal, because I could have just done the following:

>>> np.broadcast_to(q, (4, 10)) * q.unit

But it's annoying and redundant. It would be nice if Quantity handled this automatically. I'm not sure whether there's some kind of magic method we can add to Quantity to handle this properly? Otherwise maybe something like astropy.units.quantity.broadcast_to?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy