<div dir="ltr">Thank you!<div><br></div><div>Your 1st example clarifies things - In order to make my first attempt work correctly, I need to call the "to" method with the correct units on the constant G!</div><div><br></div><div>All the best.</div><div><br></div><div>Russ Osterlund</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jun 26, 2017 at 7:39 PM, Derek Homeier <span dir="ltr"><<a href="mailto:derek@astro.physik.uni-goettingen.de" target="_blank">derek@astro.physik.uni-goettingen.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On 27 Jun 2017, at 12:37 am, Russell Osterlund <<a href="mailto:russellosterlund@gmail.com">russellosterlund@gmail.com</a>> wrote:<br>
><br>
> When I execute the following:<br>
><br>
> print( 'v:', np.sqrt( ( G * <a href="http://u.M_sun.to" rel="noreferrer" target="_blank">u.M_sun.to</a>( u.kilogram ) ) / <a href="http://u.au.to" rel="noreferrer" target="_blank">u.au.to</a>( u.meter ) ) / 1000 )<br>
><br>
> this is the output:<br>
><br>
> v: 29.788833564362875 m(3/2) / (kg(1/2) s)<br>
><br>
> But when I change the code to this:<br>
><br>
> print( 'v:', np.sqrt( ( ( G * u.M_sun ) / u.au ) ).to( <a href="http://u.km" rel="noreferrer" target="_blank">u.km</a> / u.second ) )<br>
><br>
> I see this:<br>
><br>
> v: 29.788833564362875 km / s<br>
><br>
> The differing output suggests that the correct design pattern is to work with the constants as-is and only convert using the "to" method when required, e.g., np.sqrt(), or displaying output. Is this correct?<br>
><br>
</span>The difference is that the constant G is a Quantity as opposed to the units M_sun, au.<br>
When using constants and Quantities throughout, both ways should work:<br>
<br>
import astropy.constants as apc<br>
import astropy.units as apu<br>
<br>
print('v={0:}'.format(np.sqrt(<a href="http://apc.G.to" rel="noreferrer" target="_blank"><wbr>apc.G.to</a>('km3/(kg s2)')*<a href="http://apc.M_sun.to" rel="noreferrer" target="_blank">apc.M_sun.to</a>('kg')/(1*<wbr>apu.au).to('km'))))<br>
v=29.788833564362875 km / s<br>
<br>
print(‘v = {0:.3f}'.format(np.sqrt(apc.G*<wbr>apc.M_sun/(1*apu.au)).to('km/<wbr>s')))<br>
v = 29.789 km / s<br>
<br>
May not seem too intuitive, and I can’t actually tell you the reasoning why it is behaving<br>
this way, but as the unit is just an attribute of a Quantity, there probably is one…<br>
<br>
HTH,<br>
                                        Derek<br>
<br>
______________________________<wbr>_________________<br>
AstroPy mailing list<br>
<a href="mailto:AstroPy@python.org">AstroPy@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/astropy" rel="noreferrer" target="_blank">https://mail.python.org/<wbr>mailman/listinfo/astropy</a><br>
</blockquote></div><br></div>