# Use of own unit abbreviations

I have been using AutomaticUnits in my calculations. In this package all I had to do was to declare the units I wanted e.g. mm, kN, sec, kPa, litres etc. in terms of existing units, and then use them in my calculations. To get an answer in the units I needed I could use Convert[expression,unit] eg. Convert[100kNm/(1000cm3,MPa)]. This seemed simple and allowed me to write my calculations down in the way I would if I were doing the calcs by hand. I have looked at Quantity and the information on units in v. 9 and cannot understand how I can achieve the same results.

By the way, the use of Wolfram Alpha is not desirable except as an occasional reference source. I do not always have an internet connection and could not include the verbosity that results.

Any thoughts?

I think it is relatively simple and straightforward to achieve what you want with the new Quantity expressions as well: You could just define the symbols you want to use as units as the corresponding Quantities and go ahead:

kNm = Quantity["kN"]*Quantity["m"]
cm3 = Quantity["cm"]^3
MPa = Quantity["MPa"]


now you can use something like this to do the conversion as you have done before:

UnitConvert[100 kNm/(1000 cm3), MPa]


When making those definitions for the units you want to use you'll find that Quantity is interpreting many things correctly but not everything (that's the reason why the definitions for kNm and cm3 are the way they are). It also does need an internet connection for some of those interpretations. So I'd suggest to use the "canonical unit specification" for such definitions, which is explained here and also gives an example how to find those:

kNm = Quantity["Kilonewtons"*"Meters"];
cm3 = Quantity["Centimeters"^3];
MPa = Quantity["Megapascals"];


these should then also work without an internet connection (and without any unnecessary delays).

• So I'm not the only one who did that (mathematica.stackexchange.com/questions/15338/…). But you need some workarounds to make it work if some values can become 0. – Niki Estner Dec 7 '12 at 11:25
• @nikie: yes, I saw that question and the answers right after I posted the answer. So there are cerainly limits in what will work with such a simple approach... – Albert Retey Dec 8 '12 at 11:04
• Albert. The simple solution that you have put forward is what I would have expected to work. Unfortunately, it does not seem to work very often and I, more often than not, get the "Unable to interpret unit specification" message when using UnitConvert. At the moment I am continuing to use AutomaticUnits which seems far more robust, but would prefer to use a core Mathematica system rather than a separate package if possible, in order to future proof the notebooks I am creating now. Perhaps I am just missing something or have misunderstood the documentation. – Malcolm Dec 9 '12 at 17:33
• @MalcolmWoodruffLtd: I admittedly haven't used this very much, so I haven't seen many of the potential problems. But so far I think all problems I've seen and heard of are not specific in how you input Quantity expressions but how those behave during evaluation. There seems to be room for improvemnt here... – Albert Retey Dec 10 '12 at 8:50

Like others, my first reaction to WM9's system-wide physical units was "Why is this so laborious? Do they expect us to type Quantity[magnitude, unit] everytime we want to associate a unit with some quantity?"

After exploring units in WM9 for a while, I figured a way to assign shortcut names for my favorite units which, I think, is more user-friendly than what was provided by Wolfram (though I give them credit for building into Mathematica a full-fldged units manager).

First, evalute the following code in a WM9 notebook:

    Remove["Global*"];

(* pair shortcuts names with each unit's full name; just a few examples here *)
unitDef = {
{ug, "Grams"}, { umg, "Milligrams"}, {ukg, "Kilograms"},
{um, "Meters"}, {ucm, "Centimeters"}, {umm, "Millimeters"},
{uL, "Liters"}, {umL, "Milliliters"}, {ucm3, "Centimeters"^3}
};

(* create symbols from the shortcut names *)
Symbol@ToString[ #[] ]& /@ unitDef;

(* map a function for each shortcut name using an upset assignment *)
(q_[Evaluate[ #[] ]]^= Quantity[q, #[]])& /@ unitDef;


To keep the shortcut names from interferring with symbols having the same name, keep shortcut names inside their own context (I chose "u", of course, you can use any context name you want). Checking context "u" shows the shortcut names:

    In:= Names["u*"]

Out= {"ucm", "ucm3", "ug", "ukg", "uL", "um", "umg", "umL", "umm"}


Since the Quantity function is defined using an upset assignment, the function call operator "@" is used in the form quantity@ unit-shortcut.

Some examples:

    In:= (x * y)/z /. { x -> 500.@umg, y -> 200.@umL, z -> 3.@uL }

Out= Quantity[33.3333, "Milligrams"] (* displayed as 33.3333 mg in WM9 notebook*)

In:= (x * y)/z /. { x -> 500.@ug, y -> 200.@ucm3, z -> 3.@uL }

Out= Quantity[33.3333, "Grams"] (* displayed as 33.3333 g *)

In:= 3@ucm3

Out= Quantity[3, ("Centimeters")^3] (* displayed as 3 cm^3 *)

In:= UnitConvert[3@ucm3, "Milliliters"]

Out= Quantity[3, "Milliliters"] (* displayed as 3 mL *)

In:= x = 7@ug

Out= 7 g

In:= y = 3@uL

Out= 3 L

In:= N[x/y]

Out= 2.33333 g/L


While not fully tested, it seems to work well enough for me. I hope others find this useful.