# Blog post: Scientific computing with confidence using typed dimensions

**URL:** <https://discourse.haskell.org/t/blog-post-scientific-computing-with-confidence-using-typed-dimensions/10767>\
**Category:** Links\
**Created:** [November 20, 2024, 4:09pm UTC](https://discourse.haskell.org/t/blog-post-scientific-computing-with-confidence-using-typed-dimensions/10767 "2024-11-20T16:09:00Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![LaurentRDC](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/laurentrdc/32/4950_2.png) [@LaurentRDC](https://discourse.haskell.org/u/LaurentRDC)\
**Post date:** [November 20, 2024, 4:09pm UTC](https://discourse.haskell.org/t/blog-post-scientific-computing-with-confidence-using-typed-dimensions/10767/1 "2024-11-20T16:09:00Z")

</div>

Hey all,

I’m a big fan of the [`dimensional`](https://hackage.haskell.org/package/dimensional) library, which allows for type-safe physical dimensions. I wrote a [blog post about it](https://laurentrdc.xyz/posts/typed-dimensions.html).

Let me know what you think!

Additionally, I am now a maintainer of `dimensional`. I would love to hear about your experience so we can make it better.

---

<div class="post-metadata">

**Author:** ![olf](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/olf/32/3813_2.png) [@olf](https://discourse.haskell.org/u/olf)\
**Post date:** [November 20, 2024, 6:59pm UTC](https://discourse.haskell.org/t/blog-post-scientific-computing-with-confidence-using-typed-dimensions/10767/2 "2024-11-20T18:59:50Z")

</div>

I particularly like the derivation why arguments to `sin` etc. must be dimensionless.

One aspect where dimensions have helped me in exam situations was this: Given three (or more) units, dimension analysis often lets you _derive_ the mathematical formula of the relationship of their quantities, e.g.

```haskell
work = force * length
   because
joule = newton * meter 

```

One might envision a dimension-aware [Djinn](https://hackage.haskell.org/package/djinn) that can derive such formulas given the units.

How does `dimensional` deal with integrals and derivatives? Or is that subsumed by ordinary multiplication and division? Could it make sense to add Newton’s fluxions (infinitesimals) as an additional basic dimension? This would allow the scientific Haskeller to express differential equations in a type-safe way.

---

<div class="post-metadata">

**Author:** ![LaurentRDC](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/laurentrdc/32/4950_2.png) [@LaurentRDC](https://discourse.haskell.org/u/LaurentRDC)\
**Post date:** [November 20, 2024, 7:19pm UTC](https://discourse.haskell.org/t/blog-post-scientific-computing-with-confidence-using-typed-dimensions/10767/3 "2024-11-20T19:19:00Z")

</div>

> Given three (or more) units, dimension analysis often lets you _derive_ the mathematical formula

That is indeed very cool! One of the reasons I wish radians were a base unit is to enable even more precise dimensional analysis (e.g. Planck’s constant and reduced Planck’s constant would have different dimensions).

> How does `dimensional` deal with integrals and derivatives?

Right now it doesn’t do analytical integrals and derivatives. For many numerical routines (e.g. [Runge-Kutta](https://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods)), all you need are basic arithmetic operations.

I would love to see an ecosystem around `dimensional`; for example, supporting typed dimensions in the automatic differentiation [`ad`](https://hackage.haskell.org/package/ad) package or quantities with uncertainties in the [`uncertain`](https://hackage.haskell.org/package/uncertain) package.

This would be rather involved, since `(*)` and `(/)` for quantities is a lot more flexible than for the `Num` typeclass.

---

<div class="post-metadata">

**Author:** ![reuben](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/reuben/32/4733_2.png) [@reuben](https://discourse.haskell.org/u/reuben)\
**Post date:** [November 20, 2024, 7:44pm UTC](https://discourse.haskell.org/t/blog-post-scientific-computing-with-confidence-using-typed-dimensions/10767/4 "2024-11-20T19:44:38Z")

</div>

This is really great! One application I have in mind is [hamiltonian monte carlo](https://en.wikipedia.org/wiki/Hamiltonian_Monte_Carlo), which numerically solves an ODE to generate samples from a distribution. I implemented a version of this in Haskell, but it would be cool if I could distinguish between time and length on the type level. And for that matter, I’m working in phase space, so momentum and position should have different dimensions.

---

<div class="post-metadata">

**Author:** ![olf](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/olf/32/3813_2.png) [@olf](https://discourse.haskell.org/u/olf)\
**Post date:** [November 20, 2024, 8:21pm UTC](https://discourse.haskell.org/t/blog-post-scientific-computing-with-confidence-using-typed-dimensions/10767/5 "2024-11-20T20:21:44Z")

</div>

How does the dimensions package compare to [units](https://hackage.haskell.org/package/units)? For example, I wrote a physical model that also involves price calculations with a type-safe `cost = price * quantity` relationship. This is not derivable from SI `Dimension`.

---

<div class="post-metadata">

**Author:** ![LaurentRDC](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/laurentrdc/32/4950_2.png) [@LaurentRDC](https://discourse.haskell.org/u/LaurentRDC)\
**Post date:** [November 20, 2024, 8:29pm UTC](https://discourse.haskell.org/t/blog-post-scientific-computing-with-confidence-using-typed-dimensions/10767/6 "2024-11-20T20:29:54Z")

</div>

I did not know about `units`! From the description (emphasis mine):

> The units package provides a mechanism for compile-time dimensional analysis in Haskell programs. It defines an embedded type system based on units-of-measure. **The units defined are fully extensible, and need not relate to physical properties**.

That sounds very cool!

I’m not familiar with creating units in `dimensional`, but I _think_ that the dimensions are [restricted to the 7 basic physical dimensions](https://hackage.haskell.org/package/dimensional-1.6.1/docs/Numeric-Units-Dimensional-Prelude.html#t:Dimension) (although `dimensional` is NOT limited to SI units, as seen in the OP)

---

<div class="post-metadata">

**Author:** ![olf](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/olf/32/3813_2.png) [@olf](https://discourse.haskell.org/u/olf)\
**Post date:** [November 20, 2024, 9:17pm UTC](https://discourse.haskell.org/t/blog-post-scientific-computing-with-confidence-using-typed-dimensions/10767/7 "2024-11-20T21:17:13Z")

</div>

In an ideal world, the SI `Dimension` can be part of the extensible `units`. Without having used either of the packages, there is one apparent obstacle:  
Each value of type `Dimension` should serve as the associated type `DimOfUnit` of a SI unit and thereby give rise to an instance of the `Data.Metrology.Unit` class. However, the kind of `DimOfUnit` is `*` and not poly-kinded, as would be required if we were to use elements of the type `Dimension` as types. The corresponding [SI dimensions definitions](https://hackage.haskell.org/package/units-defs-2.2.1/docs/Data-Dimensions-SI.html) have an isolated type of kind `*` for every SI dimension.  
Perhaps a poly-kinded `DimOfUnit` is worth an issue in the tracker of `units`? @rae what do you think? Is that feasible?

---

<div class="post-metadata">

**Author:** ![Bodigrim](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/bodigrim/32/1457_2.png) [@Bodigrim](https://discourse.haskell.org/u/Bodigrim)\
**Post date:** [November 20, 2024, 10:22pm UTC](https://discourse.haskell.org/t/blog-post-scientific-computing-with-confidence-using-typed-dimensions/10767/8 "2024-11-20T22:22:44Z")

</div>

> [@olf](#):
>
> One aspect where dimensions have helped me in exam situations was this: Given three (or more) units, dimension analysis often lets you _derive_ the mathematical formula of the relationship of their quantities

My favorite example of this sort is the period of a small-angle pendulum. What is the period `T :: s` of a pendulum with weight `m :: kg`, length `l :: m` and gravity `g :: m / s^2`? Immediately from dimensional analysis alone we see that weight cannot possibly have any effect on the period, and the only way to unify `s = m^a (m / s^2)^b` is `b = -1/2` and `a = 1/2`, so `T = c sqrt(l / g)` for some coefficient `c`.

---

<div class="post-metadata">

**Author:** ![mpilgrem](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/mpilgrem/32/2529_2.png) [@mpilgrem](https://discourse.haskell.org/u/mpilgrem)\
**Post date:** [November 20, 2024, 11:32pm UTC](https://discourse.haskell.org/t/blog-post-scientific-computing-with-confidence-using-typed-dimensions/10767/9 "2024-11-20T23:32:36Z")

</div>

An opportunity to recall the classic example of the potential cost of getting units wrong in software: [Mars Climate Orbiter - Wikipedia](https://en.wikipedia.org/wiki/Mars_Climate_Orbiter#Cause_of_failure).

---

<div class="post-metadata">

**Author:** ![mikeplus64](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/mikeplus64/32/3998_2.png) [@mikeplus64](https://discourse.haskell.org/u/mikeplus64)\
**Post date:** [November 21, 2024, 4:01am UTC](https://discourse.haskell.org/t/blog-post-scientific-computing-with-confidence-using-typed-dimensions/10767/10 "2024-11-21T04:01:27Z")

</div>

Also using `dimensional` in a project with roughly estimating plane weight and balance among other things – and can confirm it’s been great. Has helped avoid a lot of dumb mistakes. I made a small wrapper module around it that gives me the operators prefixed with a `.` since usually I’m still needing to do arithmetic on plain old `Int`s and such I find qualified operators everywhere are quite distracting/hard to read/write.

---

<div class="post-metadata">

**Author:** ![olf](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/olf/32/3813_2.png) [@olf](https://discourse.haskell.org/u/olf)\
**Post date:** [November 27, 2024, 1:07pm UTC](https://discourse.haskell.org/t/blog-post-scientific-computing-with-confidence-using-typed-dimensions/10767/11 "2024-11-27T13:07:07Z")

</div>

Just curious: Does there exist a solution for the boundary problem? That is, when an application imports data from file or network (the data crosses the outside world/application boundary), the numbers usually come without units. It it easy to shoot yourself in the foot by interpreting the raw numbers in the wrong unit. Apparently the mentioned Mars Orbiter failure was a boundary problem. This might warrant a `Numeric.Units.Dimensional.Parser` module.

---

<div class="post-metadata">

**Author:** ![LaurentRDC](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/laurentrdc/32/4950_2.png) [@LaurentRDC](https://discourse.haskell.org/u/LaurentRDC)\
**Post date:** [November 27, 2024, 1:40pm UTC](https://discourse.haskell.org/t/blog-post-scientific-computing-with-confidence-using-typed-dimensions/10767/12 "2024-11-27T13:40:27Z")

</div>

That’s an excellent idea, I’ll see if there’s appetite to have this in the main dimensional repository

Edit: [Add parsing functionality (possible in a new package) · Issue #230 · bjornbm/dimensional · GitHub](https://github.com/bjornbm/dimensional/issues/230#issue-2698917639)
