Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
2  
Possible duplicate of this question? –  Brett Champion Nov 30 '12 at 15:18
add comment

2 Answers 2

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 = {
        {u`g, "Grams"}, { u`mg, "Milligrams"}, {u`kg, "Kilograms"},
        {u`m, "Meters"}, {u`cm, "Centimeters"}, {u`mm, "Millimeters"},
        {u`L, "Liters"}, {u`mL, "Milliliters"}, {u`cm3, "Centimeters"^3}
        };

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

    (* map a function for each shortcut name using an upset assignment *)
    (q_[Evaluate[ #[[1]] ]]^= Quantity[q, #[[2]]])& /@ 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[1]:= Names["u`*"]

    Out[1]= {"u`cm", "u`cm3", "u`g", "u`kg", "u`L", "u`m", "u`mg", "u`mL", "u`mm"}

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[2]:= (x * y)/z /. { x -> 500.@u`mg, y -> 200.@u`mL, z -> 3.@u`L }

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

    In[3]:= (x * y)/z /. { x -> 500.@u`g, y -> 200.@u`cm3, z -> 3.@u`L }

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

    In[4]:= 3@u`cm3

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

    In[5]:= UnitConvert[3@u`cm3, "Milliliters"]

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

    In[6]:= x = 7@u`g

    Out[6]= 7 g

    In[7]:= y = 3@u`L

    Out[7]= 3 L

    In[8]:= N[x/y]

    Out[8]= 2.33333 g/L

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

share|improve this answer
add comment

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).

share|improve this answer
    
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. –  nikie 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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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