36
$\begingroup$

It’s great that Quantity can utilize Wolfram Alpha to interpret unit strings that it doesn’t recognize, but I need my code to work on machines that do not have Internet access.

Is there a complete list of built-in (i.e. canonical) unit strings recognized by Quantity?

Better yet, is there a programmatic way to produce such a list?

$\endgroup$
1
  • 4
    $\begingroup$ Good question! I hope Wolfram notices this question and intends to make the Quantity capability stand alone (I often use compound units and almost always run into this issue). $\endgroup$
    – Cassini
    Apr 19, 2013 at 21:25

3 Answers 3

34
$\begingroup$

This is certainly the optimal way of obtaining the list you are looking for

Quantity;QuantityUnits`Private`$UnitReplacementRules[[1, All, 1]]

EDIT for v10 (thanks @DavidCreech)

In v10 this undocumented variable format has been changed into an association, whose keys are the units.

Quantity; Keys[QuantityUnits`Private`$UnitReplacementRules]
$\endgroup$
7
  • 2
    $\begingroup$ Haha, I just came here to post this! Damn you fellow spelunker! $\endgroup$
    – rm -rf
    Jan 26, 2013 at 20:03
  • $\begingroup$ FWIW, this seems to me the optimal way to obtain this list, which incidentally is appropriate. $\endgroup$ Jan 27, 2013 at 0:55
  • $\begingroup$ @OleksandrR. I trust your judgement, editing $\endgroup$
    – Rojo
    Jan 27, 2013 at 1:10
  • 2
    $\begingroup$ Haha, nice! The Dispatch object from which this list is extracted is actually built from the downvalues of CalculateUnits`UnitCommonSymbols`KnownUnit0Q, which is the canonical arbiter of whether a unit is valid or not. But since this doesn't change after the QuantityUnits` package is loaded, recovering the list as you do here is surely the most direct approach. $\endgroup$ Jan 27, 2013 at 1:21
  • 3
    $\begingroup$ @Rojo Note that in version 10 an association is used, so use Quantity; Keys[QuantityUnitsPrivate$UnitReplacementRules] if you are using 10. $\endgroup$ Aug 26, 2014 at 20:25
9
$\begingroup$

As a partial answer the documentation says:

Supported units include all those specified by NIST Special Publication 811.

This is repeated in Unit Discovery. It also states:

Unit interpretation requires internet connectivity, and can entail additional evaluation time. If speed is a concern, it is advisable to use the canonical unit specification, which can be found using InputForm.

$\endgroup$
4
$\begingroup$

For Mathematica 12.2.

These two are equivalent

Sort@Keys[QuantityUnits`Private`$UnitReplacementRules] == 
 Sort@QuantityUnits`$UnitList
(*True*)

There are also

QuantityUnits`$UnitAlternateStandardNameReplacements

contains something like Celsius which is not contained in $UnitList. Most of these units are readily usable in Quantity except few cases need internet connection for interpretation.

There are also UnitShortName information in QuantityUnits`$UnitTable

shortUnitAssoc = QuantityUnits`$UnitTable // Query[;; , "UnitShortName"] // 
 Select[# =!= None &];

which contains "Milligrams" -> "mg", "Kilograms" -> "kg", "Meters" -> "m" etc. Some of these short unit are readily usable like Quantity[1,"mg"].

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.