Dimensional Analysis

8 messages
Open this post in threaded view
|

Dimensional Analysis

 I was playing around with Dave's and Aleksey's dimensional analysis example, and I decided that I couldn't leave well enough alone.  To that end, I decided to reimplement it so that combining units could be written using infix operator notation.  Anyway, the attached file implements it so the following works (on EDG). #include "dim.hpp" #include int main(void) {     using namespace dim;     using dim::time; // annoying names from C headers!     DIM( length / time ) velocity;     DIM( velocity / time ) acceleration;     DIM( mass * acceleration ) force;     quantity m = 5.0f;     quantity a = 9.8f;     quantity f = m * a;     std::cout << "force = " << f.value() << '\n';     return 0; } I find the syntax like     DIM( mass * length / (time * time) ) force; mildly amusing, so I thought I'd share. Regards, Paul Mensonides _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost dim.hpp (3K) Download Attachment
Open this post in threaded view
|

Re: Dimensional Analysis

 Paul Mensonides wrote: > I was playing around with Dave's and Aleksey's dimensional analysis example, and > I decided that I couldn't leave well enough alone.  To that end, I decided to > reimplement it so that combining units could be written using infix operator > notation.  Anyway, the attached file implements it so the following works (on > EDG). Neato!  I could actually really use something like this that's extensible so I can define my own units and conversions between them.  Maybe I'll fiddle with it some but time is tight at the moment (when isn't it?).                          -Dave _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Open this post in threaded view
|

Re: Dimensional Analysis

 "David Greene" wrote > Paul Mensonides wrote: >> I was playing around with Dave's and Aleksey's dimensional analysis example, >> and >> I decided that I couldn't leave well enough alone.  To that end, I decided to >> reimplement it so that combining units could be written using infix operator >> notation.  Anyway, the attached file implements it so the following works (on >> EDG). > > Neato!  I could actually really use something like this that's > extensible so I can define my own units and conversions between > them.  Maybe I'll fiddle with it some but time is tight at the > moment (when isn't it?). **** Advert **** No time to define your own units? No time to look up those pesky conversion factors?  Now there is no need to define your own units or conversions ... pqs does it all for You! Do you need readymade output of units or fractional dimension, such as Johnson-noise voltage density?, physical constants? Standardised Repeatable Conversion-factors? pqs features all this  and lots Lots More!  NEW! IMPROVED! Now Includes detailed documentation including a Getting Started section Tests and Examples. Tested on VC7.1 ,VC8.0, gcc3.2 and gcc4.0. . pqs  Version 3.0.6  is in the Boost vault Now: http://tinyurl.com/7m5l8pqs is Also in the boost review queue. pqs is based on the SI as recommended down by NIST ~~~ Advert ~~~ regards Andy Little _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Open this post in threaded view
|

Re: Dimensional Analysis

 > -----Original Message----- > From: [hidden email] > [mailto:[hidden email]] On Behalf Of Andy Little > pqs  Version 3.0.6  is in the Boost vault Now: > http://tinyurl.com/7m5l8This (my small sample code) is in no way supposed to be a full-blown library, BTW.  It is just for amusement.  The things that I find amusing in it are that dimensions are specified using variables, not types (somewhat unusual), and the way that the reconstruction of type from variables occurs. Regards, Paul Mensonides _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Open this post in threaded view
|

Re: Dimensional Analysis

 "Paul Mensonides"  wrote >> -----Original Message----- >> From: [hidden email] >> [mailto:[hidden email]] On Behalf Of Andy Little > >> pqs  Version 3.0.6  is in the Boost vault Now: >> http://tinyurl.com/7m5l8> > This (my small sample code) is in no way supposed to be a full-blown library, > BTW.  It is just for amusement. How about rational dimension elements rather than integers. Would that be possible using the preprocessor? This occurs rarely but needs to be possible. One example is in measuring electrical noise where units for noise-voltage density are  Volts * (Hertz  to_power -1/2). The time element of the dimension works out as being to power -(2 1/2) FWIW! > The things that I find amusing in it are that > dimensions are specified using variables, not types (somewhat unusual), and > the > way that the reconstruction of type from variables occurs. Is that the same as how Boost.typeof works ...? acceleration a; mass m; BOOST_TYPEOF( m * a) f; //or BOOST_AUTO(f, m * a); regards Andy little _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Open this post in threaded view
|

Re: Dimensional Analysis

 > -----Original Message----- > From: [hidden email] > [mailto:[hidden email]] On Behalf Of Andy Little > > This (my small sample code) is in no way supposed to be a > full-blown > > library, BTW.  It is just for amusement. > > How about rational dimension elements rather than integers. > Would that be possible using the preprocessor? The preprocessor isn't doing the work.  It is just generating the code that does the work.  Use of the preprocessor (here) is just making all that code write itself based on an initial sequence of fundamental units. > This occurs > rarely but needs to be possible. > One example is in measuring electrical noise where units for > noise-voltage density are  Volts * (Hertz  to_power -1/2).                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Out of curiosity, how does this occur starting from SI's fundamental units? > The time element of the dimension works out as being to power > -(2 1/2) FWIW! You can certainly implement a static rational with a little template metaprogramming.  Reducing rational values with template metaprogramming would be expensive in general, but here were mostly talking about very "small" rationals.  You'd just end up with two "fields" per dimension (numerator and denominator) instead of one, and you'd have to use them as rational values. E.g. the way that I wrote it, you basically have: dim ...where 'mass' and 'time' are integers.  Instead, you have to have 'mass' and 'time' be types that represent rational values (such as 'rational<-5, 2>'). > > The things that I find amusing in it are that dimensions > are specified > > using variables, not types (somewhat unusual), and the way that the > > reconstruction of type from variables occurs. > > Is that the same as how Boost.typeof works ...? > > acceleration a; > mass m; > > BOOST_TYPEOF( m * a) f; > //or > BOOST_AUTO(f, m * a); > > regards > Andy little It is probably similar (as far as reconstruction goes).  I think that specifying dimensions using variables instead of types is different.  Instead of the above, you have something like: // fundamentals: length; time; mass; // etc. // composites: DIM(length / time)   velocity; DIM(velocity / time) acceleration;   // or DIM(length / (time * time)) DIM(mass * acceleration) force;   // or DIM(mass * length / (time * time)) All of the above are variable declarations, not typedefs.  They are variables that have no function except to be used in expressions for the purpose of extracting type.  In a scheme like this, the user never even sees the actual types of these variables.  Instead, the type is 'named' via an expression. Using this scheme, however, you can't say something like: 2 * time So you'd have to say something like: r<2> * time (or r<1, 2> to get such a fractional exponent at all). All of this is slightly more complex that what I wrote (which is no where near being a full-fledged physical quantities library), but it is all possible. Regards, Paul Mensonides _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Open this post in threaded view
|

Re: Dimensional Analysis

Open this post in threaded view
|