by g0xA52A2A on 11/21/24, 10:26 AM with 47 comments
by cosmic_quanta on 11/21/24, 12:30 PM
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.
[0]: https://discourse.haskell.org/t/blog-post-scientific-computi...
by jonjojojon on 11/21/24, 4:12 PM
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].
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.
by fph on 11/21/24, 1:12 PM
by aithrowawaycomm on 11/21/24, 9:55 PM
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.
by semi-extrinsic on 11/21/24, 10:55 PM
There is even native support for reading dimensioned values from input files, so your user can specify "speed 12.7 [ft/s]" irrespective of what units you use internally in the code processing that input file. It just gets converted (or throws an error).
See e.g. this, from 4.2.6 onwards: https://doc.cfd.direct/openfoam/user-guide-v12/basic-file-fo...
by jcgrillo on 11/21/24, 5:45 PM
But why not both? A number system with dimensions and which automatically propagates measurement uncertainty through calculations (a la Taylor's train wreck book) doesn't seem totally infeasible?
I would particularly like this for expressing cad models as code instead of brep.
by edu_guitar on 11/21/24, 2:11 PM
by zzbn00 on 11/21/24, 1:13 PM
by simiones on 11/21/24, 1:18 PM
by fofoz on 11/21/24, 4:19 PM
by ttoinou on 11/22/24, 2:07 PM
Two quantities with dimensions
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.That’s not true though. Some units cannot be added together, for example all intensive units like speed, density, temperature etc.
by chris_va on 11/21/24, 10:52 PM
from astropy import units as u
density = np.linspace(0, 1, 10)*u.g/u.cm**3
mass = (density*1*u.m**3).to(u.kg)
by librasteve on 11/21/24, 1:09 PM
While Raku is less popular than Python, it does have deep roots in Haskell and strong types (the first Raku / Perl6 parser - PUGS - was written in Haskell and all the early devs were encouraged to learn Haskell first).
Similar concepts are used in these Raku modules... which provide dimensional analysis and marry types to dimensions.
- https://raku.land/zef:librasteve/Physics::Measure
- https://raku.land/zef:librasteve/Physics::Unit
I had some fun with making this example of using these Raku modules with Jupyter https://rakujourney.wordpress.com/wp-content/uploads/2023/08...
[disclaimer: I am the author of these modules]
Raku is also good at Slangs (Sublanguages) and unicode, so these tools can be used in a very intuitive way:
#!/usr/bin/env raku
use Physics::Constants;
use Physics::Measure :ALL;
say ~ℏ; #1.054571817e-34 J.s
my \λ = 2.5nm;
say "Wavelength of photon (λ) is " ~λ;
my \ν = c / λ;
say "Frequency of photon (ν) is " ~ν.in('petahertz');
my \Ep = ℎ * ν;
say "Energy of photon (Ep) is " ~Ep.in('attojoules');
Wavelength of photon (λ) is 2.5nm
Frequency of photon (ν) is 119.92PHz
Energy of photon (Ep) is 79.46aJ
by antononcube on 11/21/24, 6:13 PM
See: https://reference.wolfram.com/language/guide/Units.html
> 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.
by hyperjeff on 11/21/24, 7:49 PM
by amelius on 11/21/24, 3:33 PM
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?
Etc.
by toxik on 11/21/24, 12:45 PM
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++.
by Hizonner on 11/21/24, 2:58 PM
by Mikhail_K on 11/21/24, 5:20 PM