Skip to content

numpy.dot and numpy.cross won't preserve Quantity #1930

Closed

Description

@astrojuanlu

I'm using astropy 0.3 under python 3.3 with numpy 1.8.0, and I found that np.dot won't preserve Quantityes as inputs:

>>> import numpy as np
>>> from astropy import units as u
>>> q = 1 * u.m; q
<Quantity 1 m>
>>> np.dot(q, q)
1
>>> np.sum(q * q)
<Quantity 1 m2>

the same happens with np.cross:

>>> r = [1, 0, 0] * u.m
>>> v = [0, 1, 0] * u.m / u.s
>>> np.cross(r, v)
array([0, 0, 1])

Activity

  1. demorest commented on Dec 28, 2013

    @demorest

    I've recently noticed this problem as well. In case it helps, you can work around it by calling (in your example) q.dot(q) instead of np.dot(q,q). This usage does seem to preserve units in the result.

  2. mhvk commented on Dec 28, 2013

    @mhvk
    Contributor

    @Juanlu001 - thanks for the report! This is a problem with numpy not providing a hook where we can tell it that its input is a Quantity rather than a regular numpy array. There are a few more places where this is an issue (e.g., np.inner, np.outer, np.hstack; see #1274); some of these should be relatively easy to fix with numpy 1.9. If you find any other work-arounds, maybe add them to #1274 for now (I'll add @demorest's suggestion there for now, so there is a record).

    @astrofrog - this does seem a duplicate of #1274, so I think it can be closed.

  3. astrojuanlu commented on Dec 28, 2013

    @astrojuanlu
    MemberAuthor

    @mhvk It's true, I didn't see that report before. Thanks for the prompt response! I'm not that creative but if I find another work-around I will ping.

    Just out of curiosity, why does numpy 1.9 make things easier? Which are the relevant changes?

  4. mhvk commented on Dec 28, 2013

    @mhvk
    Contributor

    numpy 1.9 introduces a __numpy_ufunc__ attribute [1] that makes overriding easier than the current system with __array_prepare__, __array_wrap__, and __array_finalize__ (it includes np.dot, though that is not strictly a ufunc). I started a trial [2], but haven't really gotten around to seeing exactly what it solves and doesn't.

    [1] http://docs.scipy.org/doc/numpy-dev/reference/arrays.classes.html
    [2] https://github.com/mhvk/astropy/compare/quantity-numpy-ufunc?expand=1

  5. astrojuanlu commented on Dec 28, 2013

    @astrojuanlu
    MemberAuthor

    Good to know, I was not aware of this. Let's hope it helps fixing the issue - another function that doesn't work that I use quite frequently is numpy.linalg.norm.

  6. hamogu commented on Dec 29, 2013

    @hamogu
    Member

    Just for the record: scipy.integrate.odeint also does not preserve a quantity.

  7. mhvk commented on Dec 29, 2013

    @mhvk
    Contributor

    @hamogu - scipy routines may be harder to get right, but worth looking into...

  8. astrojuanlu commented on Dec 29, 2013

    @astrojuanlu
    MemberAuthor

    Well, taking into account that f2py is black sorcery I think one's sanity would be seriously endangered :)

  9. embray commented on Dec 30, 2013

    @embray
    Member

    I feel like the fact that some of these routines don't work with quantities was a known issue at release time, but never got documented on the "Known Issues" page or anywhere else I can find. If it does go on "Known Issues" the Quantity docs should link to that section. Some of these will be fixable with monkey-patching a la #1274, but some of them may remain "known issues".

  10. added this to the Future milestone on May 9, 2014
  11. mhvk commented on Jul 11, 2014

    @mhvk
    Contributor

    Closing this since this is part of a general problem that likely will need having our own versions of some numpy functions; see #2327.

  12. astrojuanlu commented on Aug 21, 2018

    @astrojuanlu
    MemberAuthor

    The original problem, which was that np.dot and np.cross don't retain the units, still persists with latest NumPy and Astropy. I got a bit lost on how Astropy wanted to workaround the issue or NumPy to provide a better API, could you please give a brief explanation?

  13. astrojuanlu commented on Aug 21, 2018

    @astrojuanlu
    MemberAuthor

    Thanks @pllim, I lost sight of that part!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions