[SciPy-dev] design of a physical quantities package: seeking comments

Darren Dale dsdale24 at gmail.com
Tue Aug 5 19:20:19 EDT 2008


I worked through a bunch of issues today on the quantities package. One can now create a quantity by doing:

>>> import quantities, numpy


>>> q=quantities.Quantity([1,2,3.0], 'J')

or

>>> q=quantities.Quantity([1,2,3.0], quantities.J)

or

>>> q = numpy.array([1,2,3.0]) * quantities.J
>>> q
Quantity([ 1.,  2.,  3.]), kg * m^2 / s^2

I took the previous commenters' advice and made the following an error:

>>> q.units = 'ft'
IncompatibleUnits: Cannot convert between quanitites with units of 'kg m^2 s^-2' and 'ft'

Instead one can do:

>>> q.modify_units('ft')

or

>>> q.modify_units(quantities.ft)

The standard units will be decomposed to the fundamental dimensions, but there is a mechanism in place to preserve a compound unit, a function called compound, which can be used directly or referenced in a units string:

>>> quantities.Quantity(19,'compound("parsec/cm^3")*compound("J")')
Quantity(19), (parsec/cm**3) * (J)

or:

>>> q=quantities.Quantity(19,'compound("parsec/cm^3")*compound("m^3/m^2")')

or:

>>> q=19*quantities.compound("parsec/cm^3")*quantities.compound("m^3/m^2")
>>> q
Quantity(19.0), (m^3/m^2) * (parsec/cm^3)

and there is a mechanism to force compound units to be decomposed:

>>> q.reduce_units()
>>> q
Quantity(5.8627881999999992e+23), 1 / m

or they can be recomposed:

>>> q.units=quantities.compound("parsec/cm^3")*quantities.compound("m^3/m^2")
>>> q
Quantity(19.000000000000004), (m^3/m^2) * (parsec/cm^3)

I also started a unittest suite. A short description and directions to get the package are at http://dale.chess.cornell.edu/chess-wiki/Quantities.

I'm pretty pleased with how everything is coming together. In fact, its more capable than I had originally envisioned.  I intend to continue writing unittests, working out bugs, writing some documentation, and cleaning up the code but I'll refrain from future posts unless interest picks up.

Darren
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/scipy-dev/attachments/20080805/cf4d6c87/attachment.html>


More information about the SciPy-Dev mailing list