caseywstark / dimensionful

Attach symbolic units to any data
BSD 2-Clause "Simplified" License
5 stars 0 forks source link

Simplifying Units #2

Open petigura opened 12 years ago

petigura commented 12 years ago

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

caseywstark commented 12 years ago

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 12 years ago

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 12 years ago

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/(cmday) None

caseywstark commented 12 years ago

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.