UnitfulDual.jl - Combining dual numbers with physical units

Not sure where to post this, so I will give it a try here.

I write scientific models for my job and I always like to use Unitful.jl to avoid manually tracking unit conversions and also as a basic check that the model is correct, at least from the point of view of physical dimensions. At the same time, I like to use dual numbers as in ForwardDiff.jl to quickly and accurately calculate the local sensitivity of the model’s predictions to the different parameters. In the past I never manage to get both to work well together. I won’t go into the details but I think it ultimately boiled down to dual numbers assuming that data type use for the value and partial derivative being the same.

UnitfulDual.jl implements dual numbers in a very similar fashion ForwardDiff.jl, the main difference being that partial derivatives can be of different types (with respect to the main value and to each other, so not an NTuple). This alone allows to propagate physical dimensions and units through both the value and partial derivatives in a type stable manner. I am also experimenting with using a named tuple for the partial derivatives which I think can make it easier to construct dual numbers and extract derivative information from the output.

The package is not registered and I have only tested it in a couple of real life examples. I would appreciate if people who have faced similar problems could give it a try and provide some feedback and tell me if they find it useful.

10 Likes