[SciPy-dev] design of a physical quantities package: seeking comments
Darren Dale
dsdale24 at gmail.com
Sat Aug 2 13:25:42 EDT 2008
I have been thinking about how to handle physical quantities by subclassing
ndarray and building on some of the ideas and code from
Charles Doutriaux python wrappers of udunits and Enthought's units package.
I would like to share my current thinking, and would appreciate some feedback
at these early stages so I can get the basic design right.
The proposed Quantity object would be an ndarray subclass with an additional
attribute and property:
* Quantity has a private attribute, a Dimensions container, which contains
zero or more Dimension objects, each having associated units (a string) and a
power (a number). The container object would be equipped with the various
__add__, __sub__, __mul__, etc. Before performing the operations on the
ndarray values, the operation would be performed with the Dimensions
containers, either updating self's Dimensions and yielding the conversion
factors required to scale the other quantity's values to perform the ndarray
operation, or raising an exception because the two dimensionalities are not
commensurate for the particular operation. I think this container approach is
necessary in order to allow scaling of each Dimension's units individually,
simplifying operations like 11 ft mA / ns * 1 hour = many ft mA.
* Quantity has a public units property, providing a view into the object's
dimensions and the ability to change from one set of units to another.
q.units would return the Dimensions instance, whose __repr__ would be
dynamically constructed from each dimension's units and power attributes. The
setter would have some limitations by design. For example if q has units of
kg m / s^2 and you do q.units='ft', then q.units would return kg ft /s^2.
I think the Dimensions container may provide enough abstraction to handle more
unusual operations if someone wanted to add them. Robert Kern suggested a few
years back (see
http://aspn.activestate.com/ASPN/Mail/Message/scipy-user/2538532) that a good
physical quanitities system should be able to handle operations like a
long,lat position minus another one would yield a distance, but addition
would not be supported. This functionality could be built into a subclass of
the Dimensions container.
Comments and criticism welcome.
Darren
More information about the SciPy-Dev
mailing list