TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Scientific computing with confidence using typed dimensions

97 pointsby g0xA52A2A7 months ago

19 comments

cosmic_quanta7 months ago
Author here, thanks for posting!<p>On the Haskell Discourse [0], someone posted about another Haskell library, units [1], which allows to define unit systems NOT limited to the 7 physical dimensions. For example, adding a base dimension for currency would be nice to model electricity prices in currency per energy.<p>[0]: <a href="https:&#x2F;&#x2F;discourse.haskell.org&#x2F;t&#x2F;blog-post-scientific-computing-with-confidence-using-typed-dimensions&#x2F;10767" rel="nofollow">https:&#x2F;&#x2F;discourse.haskell.org&#x2F;t&#x2F;blog-post-scientific-computi...</a><p>[1]: <a href="https:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;units" rel="nofollow">https:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;units</a>
评论 #42206983 未加载
jonjojojon7 months ago
The modeling of unit systems with types is fraught with all sorts of inconsistencies and design tradeoffs. There are quantities that have the same dimensions, but shouldn&#x27;t be compared or allow arithmetic between them. Many issues with Temperature scales and how to interpret differences in these spaces.<p>I think a pretty well thought out approach to this is the mp-units library in c++ that uses the ISQ(International System of Quantities) [1].<p>There are some great blog posts on their site about modeling units systems and dimensions. Fingers crossed it may even be added to the standard library in C++29.<p>[1]: <a href="https:&#x2F;&#x2F;mpusz.github.io&#x2F;mp-units&#x2F;latest&#x2F;" rel="nofollow">https:&#x2F;&#x2F;mpusz.github.io&#x2F;mp-units&#x2F;latest&#x2F;</a>
评论 #42206928 未加载
fph7 months ago
Also in Julia: <a href="https:&#x2F;&#x2F;painterqubits.github.io&#x2F;Unitful.jl&#x2F;stable&#x2F;" rel="nofollow">https:&#x2F;&#x2F;painterqubits.github.io&#x2F;Unitful.jl&#x2F;stable&#x2F;</a>
评论 #42212827 未加载
aithrowawaycomm7 months ago
I have yet to see a language which does units better than F#: <a href="https:&#x2F;&#x2F;learn.microsoft.com&#x2F;en-us&#x2F;dotnet&#x2F;fsharp&#x2F;language-reference&#x2F;units-of-measure" rel="nofollow">https:&#x2F;&#x2F;learn.microsoft.com&#x2F;en-us&#x2F;dotnet&#x2F;fsharp&#x2F;language-ref...</a> It is one of the main reasons I use F# over other functional languages.<p>Doing it within an existing type system is more trouble than it’s worth: the algebra of units is simple, but it doesn’t apply to other types without stretching the compiler. It is far easier to have a distinct subsystem with measure types specifically identified.
semi-extrinsic7 months ago
I will say that the OpenFOAM C++ library supports dimensions in a more user friendly way than this, with support for both vectors&#x2F;matrices and for checking units when you take partial derivatives of your quantities.<p>There is even native support for reading dimensioned values from input files, so your user can specify &quot;speed 12.7 [ft&#x2F;s]&quot; irrespective of what units you use internally in the code processing that input file. It just gets converted (or throws an error).<p>See e.g. this, from 4.2.6 onwards: <a href="https:&#x2F;&#x2F;doc.cfd.direct&#x2F;openfoam&#x2F;user-guide-v12&#x2F;basic-file-format" rel="nofollow">https:&#x2F;&#x2F;doc.cfd.direct&#x2F;openfoam&#x2F;user-guide-v12&#x2F;basic-file-fo...</a>
评论 #42209604 未加载
jcgrillo7 months ago
I misread this title (the word &quot;confidence&quot; threw me) and was initially very confused when it turned out to be about dimensional analysis instead of uncertainty.<p>But why not both? A number system with dimensions <i>and</i> which automatically propagates measurement uncertainty through calculations (a la Taylor&#x27;s train wreck book) doesn&#x27;t seem totally infeasible?<p>I would particularly like this for expressing cad models as code instead of brep.
评论 #42207087 未加载
edu_guitar7 months ago
I haven&#x27;t had the opportunity to use this in research yet, but i liked numbat [0], as it comes with relevant common units and lets you define your own. It appeared on HN before [1].<p>[0]: <a href="https:&#x2F;&#x2F;github.com&#x2F;sharkdp&#x2F;numbat">https:&#x2F;&#x2F;github.com&#x2F;sharkdp&#x2F;numbat</a><p>[1]: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=38276430">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=38276430</a>
zzbn007 months ago
There is a good dimensional analysis package `ezunits` in Maxima. Very useful for working through equations and then sometimes generating the c&#x2F;fortran code directly from Maxima is fine.
simiones7 months ago
Would this work for something like linear algebra? Could it support multiplying two 3x3 matrices where each cell can have a different dimension, and only work if they are compatible?
评论 #42209422 未加载
评论 #42205185 未加载
评论 #42204124 未加载
fofoz7 months ago
Similar library for Rust providing zero cost unit safety<p><a href="https:&#x2F;&#x2F;github.com&#x2F;paholg&#x2F;dimensioned">https:&#x2F;&#x2F;github.com&#x2F;paholg&#x2F;dimensioned</a>
ttoinou7 months ago
<p><pre><code> Two quantities with dimensions </code></pre> D1 and D2 can only be added or subtracted if (and only if) D1≡D2. For example, it does not make sense to add a length to a mass.<p>That’s not true though. Some units cannot be added together, for example all intensive units like speed, density, temperature etc.
评论 #42214093 未加载
chris_va7 months ago
I am partial to astropy, eg<p>from astropy import units as u<p>density = np.linspace(0, 1, 10)*u.g&#x2F;u.cm**3<p>mass = (density*1*u.m**3).to(u.kg)
librasteve7 months ago
This is a great article and I like that you are highlighting the benefits of Dimensions as Types.<p>While Raku is less popular than Python, it does have deep roots in Haskell and strong types (the first Raku &#x2F; Perl6 parser - PUGS - was written in Haskell and all the early devs were encouraged to learn Haskell first).<p>Similar concepts are used in these Raku modules... which provide dimensional analysis and marry types to dimensions.<p>- <a href="https:&#x2F;&#x2F;raku.land&#x2F;zef:librasteve&#x2F;Physics::Measure" rel="nofollow">https:&#x2F;&#x2F;raku.land&#x2F;zef:librasteve&#x2F;Physics::Measure</a><p>- <a href="https:&#x2F;&#x2F;raku.land&#x2F;zef:librasteve&#x2F;Physics::Unit" rel="nofollow">https:&#x2F;&#x2F;raku.land&#x2F;zef:librasteve&#x2F;Physics::Unit</a><p>I had some fun with making this example of using these Raku modules with Jupyter <a href="https:&#x2F;&#x2F;rakujourney.wordpress.com&#x2F;wp-content&#x2F;uploads&#x2F;2023&#x2F;08&#x2F;xkcd-solar-panel.pdf" rel="nofollow">https:&#x2F;&#x2F;rakujourney.wordpress.com&#x2F;wp-content&#x2F;uploads&#x2F;2023&#x2F;08...</a><p>[disclaimer: I am the author of these modules]<p>Raku is also good at Slangs (Sublanguages) and unicode, so these tools can be used in a very intuitive way:<p><pre><code> #!&#x2F;usr&#x2F;bin&#x2F;env raku use Physics::Constants; use Physics::Measure :ALL; say ~ℏ; #1.054571817e-34 J.s my \λ = 2.5nm; say &quot;Wavelength of photon (λ) is &quot; ~λ; my \ν = c &#x2F; λ; say &quot;Frequency of photon (ν) is &quot; ~ν.in(&#x27;petahertz&#x27;); my \Ep = ℎ * ν; say &quot;Energy of photon (Ep) is &quot; ~Ep.in(&#x27;attojoules&#x27;); Wavelength of photon (λ) is 2.5nm Frequency of photon (ν) is 119.92PHz Energy of photon (Ep) is 79.46aJ</code></pre>
antononcube7 months ago
Wolfram Language (aka Mathematica) is the best for doing scientific computing with physical unit dimensions.<p>See: <a href="https:&#x2F;&#x2F;reference.wolfram.com&#x2F;language&#x2F;guide&#x2F;Units.html" rel="nofollow">https:&#x2F;&#x2F;reference.wolfram.com&#x2F;language&#x2F;guide&#x2F;Units.html</a><p>&gt; The units framework [of Wolfram Language] integrates seamlessly with visualization, numeric and algebraic computation functions. It also supports dimensional analysis, as well as purely symbolic operations on quantities.
评论 #42209359 未加载
hyperjeff7 months ago
For those coding in Swift, there is the Physical[0] package.<p>[0] <a href="https:&#x2F;&#x2F;github.com&#x2F;hyperjeff&#x2F;Physical">https:&#x2F;&#x2F;github.com&#x2F;hyperjeff&#x2F;Physical</a>
amelius7 months ago
Can it also do reference frames?<p>Like if I have a cartesian coordinates in one reference frame, can I use them in a type-safe way? E.g. not add two vectors in two reference frames? Same for polar coordinates?<p>Etc.
评论 #42205420 未加载
toxik7 months ago
I was so disappointed that this turned from Python to Haskell, I would just _love_ to have something like this in Python. Even just type annotations.<p>A similar thing I have been thinking about doing in robotics contexts is to annotate vectors and transformation matrices with their reference frames. So you can only matrix-vector multiply between compatible types: `object_in_world = camera_to_world @ object_in_camera`. It can be done somewhat in C++.
评论 #42203843 未加载
评论 #42204380 未加载
评论 #42207691 未加载
Hizonner7 months ago
I used that package once, to write a program to do US taxes on Canadian mutual funds. Yes, it seemed necessary.
Mikhail_K7 months ago
See also <a href="https:&#x2F;&#x2F;painterqubits.github.io&#x2F;Unitful.jl&#x2F;stable&#x2F;" rel="nofollow">https:&#x2F;&#x2F;painterqubits.github.io&#x2F;Unitful.jl&#x2F;stable&#x2F;</a>