Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Simplifying Units #2

Open
petigura opened this Issue Mar 6, 2012 · 4 comments

Comments

Projects
None yet
2 participants

petigura commented Mar 6, 2012

Hey, Casey.

Should the time units be simplified?

Mdot = dm.Quantity(1,dm.Msun) * dm.G / dm.c**3 / dm.yr
Mdot.convert_to_cgs()
print Mdot

4.92579497077314e-6/yr s

Erik

Owner

caseywstark commented Mar 6, 2012

I'm guessing you did something like import dimensionful as dm. In this case dm.yr is a Unit object, not a Quantity. Here's what I tried...

from dimensionful import Quantity, G, c
mdot = Quantity(1.0, "Msun") * G / c**3 / Quantity(1.0, "yr")
mdot.convert_to_cgs()
print mdot

1.56195933878e-13 (dimensionless)

In your script, it tries to divide the data of Mdot by yr. Do you have any suggestions for what it should do when that happens? I wasn't sure what to do.

petigura commented Mar 7, 2012

Hey, Casey.

I'm guessing you did something like import dimensionful as dm. In this case dm.yr is a Unit object, not a Quantity. Here's what I tried...

from dimensionful import Quantity, G, c
mdot = Quantity(1.0, "Msun") * G / c**3 / Quantity(1.0, "yr")
mdot.convert_to_cgs()
print mdot

1.56195933878e-13 (dimensionless)

I see what I did. I was confusing year, the unit, with 1 year a dimensionful quantity.

In your script, it tries to divide the data of Mdot by yr. Do you have any suggestions for what it should do when that happens? I wasn't sure what to do.

I wouldn't try to guess what the user means. But an important choice is to decide whether we think of 1 year as a unit or just as a shorthand for 3.14e7 seconds.

Erik

petigura commented Mar 7, 2012

How about this?

Why is this not simplifying in CGS

import dimensionful
reload(dimensionful)
m = 4_pi_dm.Quantity(1.0,"Msun")/dm.Quantity(20.0,"day")_dm.Quantity(1.0,"Rsun")__2 /dm.c
print m
print m.convert_to_cgs()
2.09584502195e-11 Msun_Rsun*_2_s/(cm*day)
None

Owner

caseywstark commented Mar 7, 2012

Quantity.convert_to_cgs() always returns None.

m.convert_to_cgs()
print m

would do it.

There are several *_to_cgs methods of Quantity. Quantity.get_in_cgs() creates and returns a new Quantity, Quantity.convert_to_cgs() overwrites the attributes and has no return value, and Quantity.get_data_in_cgs() gets the conversion factor and returns the data multiplied to cgs only. Looking back at the docstrings, this is not totally obvious. I will make them more explicit.

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