|
Boost : |
From: Matthias Schabel (boost_at_[hidden])
Date: 2003-11-17 16:53:28
I've been digging into MPL for a while and have come up with a library
which I believe
addresses many of the complaints with previous dimensional
analysis/unit conversion
libraries. It is not in final form, but is functional and demonstrates
some nice properties:
1) the compile-time dimensional analysis code is completely separate
and separable -
it can be used to deal with arbitrary lists of tagged dimensions and
performs the appropriate
arithmetic operations on these lists - multiplication, division,
powers, and roots. This part
relies heavily on the MPL, which kicks ass (although the docs could use
some updating -
had to dredge through old boost posts to find working examples in
several cases). It also
handles dimensionless units as a special case.
2) uses a multi-layered paradigm in which there are dimension lists
(dealt with using the
dimensional analysis code) - basically MPL sequences of tag/value pairs
like :
typedef
mpl::list< dim< length_tag, Rational<2> >,
dim< time_tag, Rational<-1> >,
dim< mass_tag, Rational<1,2> >,
dim< goofy_tag, Rational<3,4> > >
my_unit_type;
for length^2 time^(-1) mass^(1/2) goofy^(3/4), where the tags are
completely arbitrary
and can be user defined. Specific combinations of tags, along with
utility typedefs and
definitions of unit symbols/conversion factors are grouped in unit
models. A dimensioned
unit is defined as a combination of a dimension list and a model, such
as :
typedef
mpl::list< dim< length_tag, Rational<1> >,
dim< time_tag, Rational<-1> > >
velocity_type;
typedef
DimensionedUnit<SIModel,velocity_type>
si_velocity_unit;
which would represent velocity in (m/s). The final layer is that of a
dimensioned quantity,
which represents a number of a particular unit. That is
DimensionedQuantity<double,velocity_unit> v(2.5);
represents 2.5 m/s.
3) currently has full SI and CGS unit systems and a toy
furlong-stone-fortnight system as a demonstration of
extensibility.
4) includes basic formatted IO for dimensioned units and dimensioned
quantities.
5) works with any type having basic arithmetic operations, so it could
easily be used in conjunction with a
fixed point decimal class, arbitrary precision float, etc... An
example in test_units.cpp is a class
implementing simple error propagation which plays perfectly nicely with
the dimension/unit code.
Documentation is basically in the form of comments in the code at the
moment; the file test_dimensional_analysis.cpp
demonstrates the compile-time dimensional analysis engine while the
file test_units.cpp demonstrates
the full unit library. It has been partially boostified; if there is
serious interest, I would be happy to finish
the process and work on documentation. There are also a couple of
oddities which I don't fully understand
to be found in the test_units.cpp program. Comments/suggestions are
welcome... I am also aware that
there are several other people who either have worked, are working on,
or are interested in this sort of a
library. I think it might be nice to get together an informal
dimensional analysis/unit library working group
to try to hash out issues more efficiently and get something
full-fledged put together. Code is in the Yahoo
files section : http://groups.yahoo.com/group/boost/files/YANL.zip
Matthias
------------------------------------------------------------------------
---------------------------
Matthias Schabel, Ph.D.
Utah Center for Advanced Imaging Research
729 Arapeen Drive
Salt Lake City, UT 84108
801-587-9413 (work)
801-585-3592 (fax)
801-706-5760 (cell)
801-484-0811 (home)
mschabel at ucair med utah edu
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk