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

Using np.linalg.inv with unyt_array has no effect on units #69

Closed
Tracked by #200
sharkweek opened this issue Mar 8, 2019 · 3 comments · Fixed by #200
Closed
Tracked by #200

Using np.linalg.inv with unyt_array has no effect on units #69

sharkweek opened this issue Mar 8, 2019 · 3 comments · Fixed by #200

Comments

@sharkweek
Copy link

  • unyt version: 1.0.7
  • Python version: 3.6
  • Operating System: Windows 10

Description

Using the numpy.linalg.inv() to calculate the inverse of a matrix, doesn't affect units.

What I Did

Here is what happens when you take the inverse of an unyt_array:

>>> x = unyt.unyt_array([[1. , 0.5, 0.5],
                         [0.5, 1. , 0.5],
                         [0.5, 0.5, 1. ]],
                        'psi')
>>> np.linalg.inv(x)
unyt_array([[ 1.5, -0.5, -0.5],
            [-0.5,  1.5, -0.5],
            [-0.5, -0.5,  1.5]], 'psi')

numpy correctly carries out the inverting operation, but the units remain the same where they should now be the reciprocal of the original units ('inch**2/lbf' in this case). As of now, the only work around I can think of is to create a new unyt_array with the inverted value of the original one and the reciprocal of the original's units.

@ngoldbaum
Copy link
Member

So I doubt there's anything we can do about making np.lingalg.inv respect units correctly without support in NumPy for __array_function__, which is being added in a future numpy release. I don't see any mention of lingalg.inv in NEP-18 but maybe @shoyer knows more?

We could also define a wrapper around lingalg.inv that correctly handles units and add the wrapper to unyt.

@ngoldbaum ngoldbaum changed the title inverting unyt_array has no effect on units Using np.linalg.inv with unyt_array has no effect on units Mar 8, 2019
@shoyer
Copy link

shoyer commented Mar 8, 2019

Every function that accepts arrays or array-like objects in NumPy's public APIs will support NEP 18. That includes np.linalg.inv.

@sharkweek
Copy link
Author

That's good news. I suppose we just have to wait until the numpy release that implements __array_function__.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants