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

add automatic unit simplification. closes #49 #58

Merged
merged 3 commits into from Jan 23, 2019

Conversation

ngoldbaum
Copy link
Member

This adds automatic simplification of units in the form of a new Unit.simplify() method. It also makes it so that products and quotients of arrays with units will now automatically be simplified.

Caveats:

  • I haven't done any performance benchmarking. In principle calling simplify() in the __array_ufunc__ machinery is "free" because it's behind an LRU cache but I need to actually verify that. There's also an extra multiplication on the array that happens if there's any unit cancellation. In practice though most people would be "manually" doing that simplification and therefore incurring the cost of the extra multiplication anyway. All that said I still need to actually verify that the performance impact is at least acceptable.
  • My tests are not super extensive and this is an invasive change. It's entirely possible there are untested corner cases.

@@ -83,16 +83,15 @@ end, days::

>>> period = 2*pi*(semimajor_axis**3/(G*Mjup))**0.5
>>> period
unyt_array([2.64196224e-12, 5.30291672e-12, 1.06837107e-11,
2.49212918e-11], 'AU**(3/2)*s/m**(3/2)')
unyt_array([ 152867.34547843, 306833.60667034, 618173.2944961 ,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a really good change here in the docs.

Copy link
Member

@chummels chummels left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Big fan of this PR. Thanks for implementing this. Yes, a substantial change, but definitely for the better.

@ngoldbaum
Copy link
Member Author

I've done some basic performance testing and don't see any significant slowdowns, e.g.:

goldbaum at ROUS in ~/Documents/unyt on simplify!
± python -m perf timeit -s "import numpy as np; data1 = np.random.random(1000); data2 = np.random.random(1000); import unyt as u; data1 *= u.s; data2 *= u.Hz" "data1*data2"
.....................
Mean +- std dev: 9.46 us +- 0.54 us

goldbaum at ROUS in ~/Documents/unyt on master!
± python -m perf timeit -s "import numpy as np; data1 = np.random.random(1000); data2 = np.random.random(1000); import unyt as u; data1 *= u.s; data2 *= u.Hz" "data1*data2"
.....................
Mean +- std dev: 9.35 us +- 0.42 us

Going to go ahead and merge. Thank you for looking this over!

@ngoldbaum ngoldbaum merged commit 52c9109 into yt-project:master Jan 23, 2019
@ngoldbaum ngoldbaum deleted the simplify branch March 8, 2019 17:09
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 this pull request may close these issues.

None yet

3 participants