Units in Mathematica 9
Today I tried using units in Mathematica. Here is how Mathematica wants you to do it:
Quantity[0.5, "Speed of Light"]
The Quantity
function checks your input against an internal list of
known units and constants. This internal list is pretty big - you can
see it by running:
Quantity; QuantityUnits`Private`$UnitReplacementRules[[1, All, 1]])
If your input is not an item of that list, the Quantity
function asks
WolframAlpha to parse your input. You can tell that this is happening
when the command does not finish instantly. Fortunately, the result
seems to be cached.
You can rightclick on the output and view the "Raw Input" form of the output to see how WolframAlpha interpreted it, and how you should enter it in the future. I don't know why this is hidden so much. If there is an external parser (always subject to change!), then it should tell me how it parsed the input. In any case, the Mathematica convention seems to be to write units in full word, with a capital letter, in plural form.
You can multiply, add, and otherwise transform your "quantities" in the obvious ways, and then convert the output to SI units by running
Quantity[1, "SpeedOfLight"] + Quantity[10^8, "Meters/Seconds"] // UnitConvert
You can also convert it to any unit by giving a second argument to the
UnitConvert
function:
UnitConvert[Quantity[1, "microparsec/(fortnight)"] , "mph"] // N
But don't rely on it. It does not parse SI prefixes correctly:
UnitConvert[Quantity[1, "microparsec/(kilofortnight)"] , "SI"] // N
(This gives you something with kilograms in the output...)
Things become particularily painful once you try to use temperatures. The following snippet works:
kB = Quantity[1, "Boltzmann Constant"];
T = Quantity[300, "Kelvins"];
Energy = kB*T;
UnitConvert[Energy, "eV"]
But it took a long time to get it working. The problem is that Mathematica defines their own unit system for temperature: absolute temperatures and difference temperature. This sort of make sense, because they transform differently (due to the 0-offsets). If you enter "Kelvin" as unit, it is interpreted by WolframAlpha as "KelvinDifference", if you enter "Kelvins", it is interpreted as absolute "Kelvins". This distinction is at first hidden from the user, because both use the same symbol "K" in the standard form output. Who the fuck designs this shit?!? Further, it's highly debatable if the unit of kB really should be J/Kelvin. If you really want to make the distinction between absolute and difference temperatures, then I think you should use J/KelvinDifference as unit for kB, otherwise it will transform in the wrong way if you transform to, say, J/°C.
Overall I have to say, working with units in Mathematica is an absolute pain. The feature is not ready. I recommend against using it. The "transparent" parsing by WolframAlpha is bad news, because noone guarantees that it won't be subject to change (meaning today's input will run differently tomorrow, even though you are using the same Mathematica version.) None of the weirdness behind is well documented or indicated to the user.
The standard Google search handles units better than Mathematica: It tells the user how it parsed the input (how to enter it in the future). Plus it's free, faster, and the syntax is better.