How to do numeric differentiation using Boost Units?

130 views Asked by At

I would like to perform a numeric differentiation in C++. For type safety, I'd like to use boost::units to avoid mixing units but also boost::units::absolute to avoid mixing relative and absolute units.

A minimal example is to calculate the velocity as a function of the change in position divided by the change in time: v = dx/dt, which can be approximated as (x1 - x0)/(t1 - t0).

In this example v has an absolute unit (velocity), dx and dt relative ones (distance / duration).

While boost::units derives the correct unit if we simply take relative units everywhere,

static_assert(std::is_same<boost::units::divide_typeof_helper<
                               boost::units::si::length, 
                               boost::units::si::time>::type, 
                           boost::units::si::velocity>::value);

the static_assert fails if we want the result of our division being an absolute velocity:

static_assert(std::is_same<boost::units::divide_typeof_helper<
                               boost::units::si::length, 
                               boost::units::si::time>::type, 
                           boost::units::absolute<boost::units::si::velocity>>::value);

Am I doing a wrong assumption that the result of dividing two relative units should always yield an absolute one? Or is this an error in the implementation of boost::units?

1

There are 1 answers

2
Ruslan On

From the docs on boost::units::absolute,

Description

A wrapper to represent absolute units (points rather than vectors).

Spacetime events are points (if not viewed as radius vectors), their differences are vectors. Velocity is also a vector. Thus, your assumption does indeed appear wrong.