# How to handle units of measurement?

I'm trying to understand how Mathematica handles units of measurement. That is I expect that a calculation should return a certain unit, but I don't succeed. Let's consider this example:

(314.159 m Sqrt[((7.34102*10^-6 - 0.0000178801 I) s^2)/m^2])/s


The results should be a scalar to which I can apply Coth[] but I am unable to "convince" Mathematica to give me that.

• MMA can not know that Sqrt[m^2]==m and Sqrt[s^2]==s. A quick and dirty trick is to assume that m,s>0 like: Assuming[{m > 0, s > 0}, (314.159 m Sqrt[((7.34102*10^-6 - 0.0000178801 I) s^2)/ m^2])/s // Simplify] Commented Nov 27, 2023 at 20:59
• @DanielHuber oh, I assumed these were units, as in meter and second. I could be wrong offcourse. I assumed UM means units measurement. Commented Nov 27, 2023 at 21:16
• Please avoid possibly ambiguous or unclear acronyms. I'm a very experienced Mathematica programmer and am not sure what UM stands for. Why not spell it out? Also, even WM is a poor acronym. Is it the same as MMA??? Commented Nov 28, 2023 at 1:49

q1 = Quantity[314.159, "Meters"]
q2 = Quantity[7.34102*10^-6 - 0.0000178801 I, "Seconds"^2/"Meters"^2];

(q1*Sqrt[q2])/Quantity[1, "Seconds"]


• I hoped in something simpler and more automatic... Yes, it could work but the final expression would be unreadable. Thanks anyway. Commented Nov 27, 2023 at 22:19
• @Teodoro: Typing Ctrl-= allows you to enter units in a more human way, if that is what you mean by "simpler and more automatic", and the corresponding DisplayForm is pretty readable. Commented Nov 28, 2023 at 0:00

You can use the syntax shown in your question, if you tell MMA what it means. Writing things like this works well for practical calculations

m = Quantity["Meters"];
s = Quantity["Seconds"];

(314.159 m Sqrt[((7.34102*10^-6 - 0.0000178801 I) s^2)/m^2])/s

(*  1.14721 - 0.769126 I  *)


In addition to Quantity, the UnitConvert and QuantityMagnitude commands are essential, at least for my puposes. I often abbreviate them by defining uconv = UnitConvert and qmag = QuantityMagnitude.

There is a way of operating with units as in the answers of Nasser and LouisB. However, before this was introduced, Mathematica used what I would call a more human way of treating units. It is exactly as you do. It only requires adding an assumption, as @Daniel Huber has shown. Instead, one can apply the following

yourExpression// Simplify[#, {m > 0, s > 0}] &


Alternatively, at the beginning of the session, one can evaluate the following:

$Assumptions = {erg > 0, dyne > 0, g > 0, cm > 0, s > 0, esu > 0, m > 0, J > 0, Pa > 0, Ν > 0, F > 0, W > 0, μm > 0, nm > 0};  Here, one has to keep in mind that Ν (Newton) is a Greek rather than a Latin letter. After this, Mathematica will always know up to the end of the current session, that these units are positive, and you only need to apply Simplify to your expression: For example: $Assumptions = {erg > 0, dyne > 0, g > 0, cm > 0, s > 0, esu > 0,
m > 0, J > 0, Pa > 0, Ν > 0, F > 0, W > 0, μm > 0,
nm > 0};
(314.159 m Sqrt[((7.34102*10^-6 - 0.0000178801 I) s^2)/m^2])/
s // Simplify

(*  1.14721 - 0.769126 I  *)


1. It is intuitive: it looks as we used to.
2. It does not require extra typing.
3. Easy rules help to transform them into any system. Below, for example, the rule to transform the units from the System International to CGS:

ruleSItoCGS = {Coul -> 310^9Sqrt[ergcm], J -> 10^7erg, W -> 10^7erg/s, Pa -> (10^7erg)/(100cm)^3, kg -> 10^3g, F -> 9.10^11cm, Ν -> 10^5ergcm^-1, m -> 100cm, μm -> 10^-4cm, nm -> 10^-7cm, kbar -> 10^9erg/cm^3};

One can easily invert this rule.

The disadvantage is that the letters reserved for units cannot be used as variables for some other aims.

Have fun

• Thanks, I worked in this way. The only pity is that, when dealing with UM and Simplify, WM is much slower. Commented Nov 28, 2023 at 17:23
• @Teodoro Marinucci What is UM? Commented Nov 28, 2023 at 17:25
• Unit of Measure Commented Nov 28, 2023 at 22:09
• @Teodoro Marinucci In your question above, there is no example of how you use UM. Please give an example. Anyway, I recommend always writing uM instead of UM. It is generally known that a good practice is to start names of custom variables with a small letter, to avoid coincidences with Mathematica service words. It might be that like this, you call the units you use above, like m and s? Is that the case? Commented Nov 29, 2023 at 11:28