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 several notebooks that make use of the Units package and would like to be able to work simultaneously with the older units and Quantity objects introduced in version 9. What is my lowest-overhead method for updating units to Quantity objects?

share|improve this question
    
@rm -rf, thanks for the edits. Much better now. –  Daniel W Mar 24 '13 at 19:47

1 Answer 1

up vote 13 down vote accepted

I have mined the Units package for the names of all units defined therein and correlated them to the built-in strings recognized by Quantity(referenced here). I then define a new function Quantify to convert the old school units into Quantity objects.

unitRules = 
 Dispatch[{Abampere -> Quantity[1, "ABAmperes"], 
   Abcoulomb -> Quantity[1, "ABCoulombs"], 
   Abfarad -> Quantity[1, "ABFarads"], 
   Abhenry -> Quantity[1, "ABHenries"],
   Abmho -> Quantity[1, "ABMhos"],
   Abohm -> Quantity[1, "ABOhms"],
   Abvolt -> Quantity[1, "ABVolts"],
   Acre -> Quantity[1, "Acres"],
   Amp -> Quantity[1, "Amperes"],
   Ampere -> Quantity[1, "Amperes"], 
   AMU -> Quantity[1, "AtomicMassUnit"], 
   Angstrom -> Quantity[1, "Angstroms"],
   Apostilb -> Quantity[1, "Apostilbs"], 
   ArcMinute -> Quantity[1, "ArcMinutes"], 
   ArcSecond -> Quantity[1, "ArcSeconds"],
   Are -> Quantity[1, "Ares"], 
   AssayTon -> Quantity[1, "AssayMetricTons"], 
   AstronomicalUnit -> Quantity[1, "AstronomicalUnit"], 
   Atmosphere -> Quantity[1, "Atmospheres"], 
   AtomicMassUnit -> Quantity[1, "AtomicMassUnit"], 
   AU -> Quantity[1, "AstronomicalUnit"], 
   AvoirdupoisOunce -> Quantity[1, "Ounces"], 
   AvoirdupoisPound -> Quantity[1, "Pounds"], 
   Bag -> Quantity[1, "BagsUK"], 
   BakersDozen -> Quantity[1, "BakersDozens"], 
   Bale -> Quantity[1, "BalesOfCotton"],
   Bar -> Quantity[1, "Bars"],
   Barn -> Quantity[1, "Barns"],
   Barrel -> Quantity[1, "Barrels"],
   Barye -> Quantity[1, "Baryes"], 
   Baud -> Quantity[1, "Bits"/"Seconds"], 
   Becquerel -> Quantity[1, "Becquerels"],
   Biot -> Quantity[1, "Biots"],
   Bit -> Quantity[1, "Bits"],
   BoardFoot -> Quantity[1, "BoardFeet"], 
   BohrMagneton -> Quantity[1, "BohrMagneton"], 
   Bolt -> Quantity[1, "BoltsWidthCotton"], 
   BritishThermalUnit -> Quantity[1, "BritishThermalUnitsIT"], 
   BTU -> Quantity[1, "BritishThermalUnitsIT"], 
   Bucket -> Quantity[1, "BucketsUK"], 
   Bushel -> Quantity[1, "BushelsUS"],
   Butt -> Quantity[1, "ButtsUK"],
   Byte -> Quantity[1, "Bytes"],
   Cable -> Quantity[1, "CableLengthsUK"], 
   Caliber -> Quantity[1, "Calibres"], 
   Calorie -> Quantity[1, "LargeCalories"], 
   Candela -> Quantity[1, "Candelas"],
   Candle -> Quantity[1, "Candelas"],
   Carat -> Quantity[1, "Carats"], 
   Celsius -> Quantity[1, "DegreesCelsiusDifference"], 
   Cental -> Quantity[1, "Centals"], 
   Centigrade -> Quantity[1, "DegreesCelsiusDifference"], 
   Centimeter -> Quantity[1, "Centimeters"], 
   Century -> Quantity[1, "Centuries"], 
   Chain -> Quantity[1, "EngineersChains"], 
   ChevalVapeur -> Quantity[1, "ChevalVapeurs"], 
   Cicero -> Quantity[1, "Ciceros"],
   Circle -> Quantity[1, "Circles"],
   Cord -> Quantity[1, "Cords"],
   Coulomb -> Quantity[1, "Coulombs"],
   Cubit -> Quantity[1, "Cubits"],
   Cup -> Quantity[1, "Cups"],
   Curie -> Quantity[1, "Curies"],
   Dalton -> Quantity[1, "Daltons"],
   Day -> Quantity[1, "Days"],
   Decade -> Quantity[1, "Decades"],
   Denier -> Quantity[1, "Denier"],
   Didot -> Quantity[1, "DidotPoints"], 
   DidotPoint -> Quantity[1, "DidotPoints"], 
   Diopter -> Quantity[1, "Diopters"],
   Dozen -> Quantity[1, "Dozens"], 
   Drachma -> Quantity[1, "AeginaDrachmes"], 
   Drop -> Quantity[1, "MetricDrops"],
   Dyne -> Quantity[1, "Dynes"], 
   ElectronVolt -> Quantity[1, "Electronvolts"], 
   Ell -> Quantity[1, "Ells"],
   Ephah -> Quantity[1, "Ephahs"],
   Erg -> Quantity[1, "Ergs"], 
   Fahrenheit -> Quantity[1, "DegreesFahrenheitDifference"], 
   Farad -> Quantity[1, "Farads"],
   Fathom -> Quantity[1, "Fathoms"],
   Feet -> Quantity[1, "Feet"],
   Fermi -> Quantity[1, "Fermis"],
   Fifth -> Quantity[1, "Fifths"],
   Firkin -> Quantity[1, "FirkinsUS"], 
   FluidDram -> Quantity[1, "FluidDrams"], 
   FluidOunce -> Quantity[1, "FluidOunces"],
   Foot -> Quantity[1, "Feet"],
   FootCandle -> Quantity[1, "FootCandles"], 
   Fortnight -> Quantity[1, "Fortnights"], 
   Furlong -> Quantity[1, "Furlongs"],
   Gal -> Quantity[1, "Gallons"],
   Gallon -> Quantity[1, "Gallons"],
   Gamma -> Quantity[1, "Gammas"],
   Gauss -> Quantity[1, "Gausses"], 
   Geepound -> Quantity[1, "GeepoundsMass"], 
   Gilbert -> Quantity[1, "Gilberts"],
   Gill -> Quantity[1, "Gills"],
   Grade -> Quantity[1, "Grads"],
   Grain -> Quantity[1, "Grains"],
   Gram -> Quantity[1, "Grams"], 
   GramWeight -> Quantity[1, "GramsForce"], 
   Gravity -> Quantity[1, "StandardAccelerationOfGravity"], 
   GrayDose -> Quantity[1, "Grays"],
   Gross -> Quantity[1, "Gross"], 
   GrossHundredweight -> Quantity[1, "LongHundredweights"], 
   Hand -> Quantity[1, "HandsUnit"],
   Hectare -> Quantity[1, "Hectares"], 
   Hefner -> Quantity[0.92, "Candelas"],
   Henry -> Quantity[1, "Henries"],
   Hertz -> Quantity[1, "Hertz"], 
   Hogshead -> Quantity[1, "HogsheadsUS"], 
   Horsepower -> Quantity[1, "HorsepowerMechanical"], 
   Hour -> Quantity[1, "Hours"], 
   Hundredweight -> Quantity[1, "ShortHundredweights"], 
   ImperialGallon -> Quantity[1, "GallonsUK"], 
   ImperialPint -> Quantity[1, "PintsUK"],
   Inch -> Quantity[1, "Inches"], 
   InchMercury -> Quantity[1, "InchesOfMercury"], 
   Jeroboam -> Quantity[1, "Jeroboams"], 
   Jigger -> Quantity[1, "Jiggers"],
   Joule -> Quantity[1, "Joules"],
   Kayser -> Quantity[1, "Kaysers"],
   Kelvin -> Quantity[1, "Kelvins"], 
   Kilogram -> Quantity[1, "Kilograms"], 
   KilogramForce -> Quantity[1, "KilogramsForce"], 
   KilogramWeight -> Quantity[1, "KilogramsForce"], 
   Knot -> Quantity[1, "Knots"],
   Lambert -> Quantity[1, "Lamberts"], Last -> Quantity[1, "LastsUK"],
    League -> Quantity[1, "Leagues"], 
   Libra -> Quantity[1, "PortugueseLibras"], 
   LightYear -> Quantity[1, "LightYears"], 
   Link -> Quantity[1, "EngineersLinks"],
   Liter -> Quantity[1, "Liters"],
   LongTon -> Quantity[1, "LongTons"],
   Lumen -> Quantity[1, "Lumens"],
   Lumerg -> Quantity[1, "Lumergs"],
   Lux -> Quantity[1, "Lux"],
   Magnum -> Quantity[1, "Magnums"],
   Maxwell -> Quantity[1, "Maxwells"],
   Meter -> Quantity[1, "Meters"], 
   MetricTon -> Quantity[1, "MetricTons"],
   Mho -> Quantity[1, "Mhos"],
   Micron -> Quantity[1, "Microns"],
   Mil -> Quantity[1, "Mils"],
   Mile -> Quantity[1, "Miles"],
   Millennium -> Quantity[1, "Millennia"], 
   MillimeterMercury -> Quantity[1, "MillimetersOfMercury"], 
   Mina -> Quantity[1, "AeginaMinas"],
   Minim -> Quantity[1, "MinimsUS"],
   Minute -> Quantity[1, "Minutes"],
   Mole -> Quantity[1, "Moles"],
   Month -> Quantity[1, "Months"], 
   NauticalMile -> Quantity[1, "NauticalMiles"], 
   NetHundredweight -> Quantity[100, "Pounds"], 
   Newton -> Quantity[1, "Newtons"],
   Nibble -> Quantity[1, "Nibbles"],
   Nit -> Quantity[1, "Nits"],
   Noggin -> Quantity[1, "NogginsUK"], 
   NuclearMagneton -> Quantity[1, "NuclearMagnetons"], 
   Obolos -> Quantity[0.71538, "Grams"], 
   Oersted -> Quantity[1, "Oersteds"],
   Ohm -> Quantity[1, "Ohms"],
   Omer -> Quantity[1, "Omers"],
   Ounce -> Quantity[1, "Ounces"],
   Parsec -> Quantity[1, "Parsecs"],
   Pascal -> Quantity[1, "Pascals"],
   Peck -> Quantity[1, "Pecks"], 
   Pennyweight -> Quantity[1, "Pennyweights"], 
   Percent -> Quantity[1, "Percent"], 
   Perch -> Quantity[1, "PerchesLength"],
   Phot -> Quantity[1, "Phots"],
   Pica -> Quantity[1, "Picas"],
   Pint -> Quantity[1, "Pints"], 
   Point -> Quantity[1, "DesktopPublishingPoints"], 
   Poise -> Quantity[1, "Poise"],
   Pole -> Quantity[1, "Poles"],
   Pondus -> Quantity[1, "DutchPonds"],
   Pony -> Quantity[1, "PoniesUS"],
   Pound -> Quantity[1, "Pounds"],
   Poundal -> Quantity[1, "Poundals"], 
   PoundForce -> Quantity[1, "PoundsForce"], 
   PoundsPerSquareInch -> Quantity[1, "PoundsForce"/"Inches"^2], 
   PoundWeight -> Quantity[1, "PoundsForce"], 
   PrintersPoint -> Quantity[1, "DesktopPublishingPoints"], 
   PSI -> Quantity[1, "PoundsForce"/"Inches"^2], 
   Puncheon -> Quantity[1, "PuncheonsUS"], 
   Quadrant -> Quantity[1, "RightAngles"], 
   Quart -> Quantity[1, "Quarts"],
   Quintal -> Quantity[1, "Quintals"],
   Rad -> Quantity[1, "Rads"],
   Radian -> Quantity[1, "Radians"], 
   Rankine -> Quantity[1, "DegreesRankine"], 
   RegisterTon -> Quantity[1, "RegisterTons"], 
   Reyn -> Quantity[1, "Reyns"],
   Rhes -> Quantity[1, "Rhes"],
   RightAngle -> Quantity[1, "RightAngles"],
   Rod -> Quantity[1, "Rods"],
   Roentgen -> Quantity[1, "Roentgens"], 
   Rontgen -> Quantity[1, "Roentgens"],
   Rood -> Quantity[1, "Roods"],
   Rope -> Quantity[1, "Ropes"], 
   Rutherford -> Quantity[1, "Rutherfords"], 
   Rydberg -> Quantity[1, "Rydbergs"],
   Seam -> Quantity[1, "SeamsUK"],
   Second -> Quantity[1, "Seconds"],
   Section -> Quantity[1, "Miles"^2], 
   Shekel -> Quantity[1, "ShekelsMass"], 
   ShortHundredweight -> Quantity[1, "ShortHundredweights"], 
   ShortTon -> Quantity[1, "ShortTons"],
   Shot -> Quantity[1, "Shots"], 
   SiderealSecond -> Quantity[1, "SiderealSeconds"], 
   SiderealYear -> Quantity[1, "SiderealYears"], 
   Siemens -> Quantity[1, "Siemens"],
   Skein -> Quantity[1, "Skeins"],
   Slug -> Quantity[1, "SlugsMass"], 
   SolarMass -> Quantity[1, "SolarMass"], 
   Span -> Quantity[1, "SpansLength"], 
   Stadion -> Quantity[1, "GreekStadions"], 
   Stadium -> Quantity[1, "GreekStadions"], 
   Statampere -> Quantity[1, "StatAmperes"], 
   Statcoulomb -> Quantity[1, "StatCoulombs"], 
   Statfarad -> Quantity[1, "StatFarads"], 
   Stathenry -> Quantity[1, "StatHenries"], 
   Statohm -> Quantity[1, "StatOhms"], 
   StatuteMile -> Quantity[1, "Miles"], 
   Statvolt -> Quantity[1, "StatVolts"], 
   Steradian -> Quantity[1, "Steradians"], 
   Stere -> Quantity[1, "Steres"],
   Stilb -> Quantity[1, "Stilbs"],
   Stokes -> Quantity[1, "Stokes"],
   Stone -> Quantity[1, "Stones"], 
   SurveyMile -> Quantity[1, "SurveyMiles"], 
   Tablespoon -> Quantity[1, "Tablespoons"], 
   Talbot -> Quantity[1, "Talbots"], 
   Talent -> Quantity[1, "AeginaTalents"], 
   Teaspoon -> Quantity[1, "Teaspoons"],
   Tesla -> Quantity[1, "Teslas"],
   Therm -> Quantity[1, "Therms"],
   Ton -> Quantity[1, "ShortTons"], 
   TonForce -> Quantity[1, "ShortTonsForce"], 
   Tonne -> Quantity[1, "ShortTons"],
   Torr -> Quantity[1, "Torr"],
   Township -> Quantity[1, "Townships"], 
   TropicalYear -> Quantity[1, "TropicalYears"], 
   TroyOunce -> Quantity[1, "TroyOunces"],
   Tun -> Quantity[1, "Tuns"],
   UKGallon -> Quantity[1, "Gallons"],
   UKPint -> Quantity[1, "Pints"],
   Volt -> Quantity[1, "Volts"],
   Watt -> Quantity[1, "Watts"],
   Weber -> Quantity[1, "Webers"],
   Week -> Quantity[1, "Weeks"],
   Wey -> Quantity[1, "WeysMass"], 
   WineBottle -> Quantity[0.7576778, "Liters"], 
   XUnit -> Quantity[1, "XUnits"],
   Yard -> Quantity[1, "Yards"],
   Year -> Quantity[1, "Years"]}];

prefixRules = 
  Dispatch[{Yotta -> 10^24, Zetta -> 10^21, Exa -> 10^18, 
    Peta -> 10^15, Tera -> 10^12, Giga -> 10^9, Mega -> 10^6, 
    Kilo -> 10^3, Hecto -> 100, Deca -> 10, Deci -> 10^-1, 
    Centi -> 10^-2, Milli -> 10^-3, Micro -> 10^-6, Nano -> 10^-9, 
    Pico -> 10^-12, Femto -> 10^-15, Atto -> 10^-18, Zepto -> 10^-21, 
    Yocto -> 10^-24}];

 Quantify[expr_]:=expr/.prefixRules/.unitRules

Quantity objects and old school units can be freely mixed

Quantify[{(2 Centimeter^3)/(Quantity[1.199, 
  "Kilograms"/"Meters"^3] Quantity[344.0, "Meters"/"Seconds"]^2), 
  30. Micro Farad}]

yields

{Quantity[0.00140959, (("Centimeters")^4 ("Seconds")^2)/("Kilograms")], 
    Quantity[0.00003, "Farads"]}
share|improve this answer
    
+1 goes w/o saying. This is great stuff. Can you share the code to extract all units? WRI will surely add more (new) units, so how to catalogue them dynamically? –  alancalvitti 2 days ago
    
@alancalvitti My intent was to allow the use of the units that were defined in the old (pre-9) Units package. Presumably, this package is no longer maintained as Quantity has taken over that functionality, so this was a one-time event performed manually. By manually, I mean with sloppy, throw-away code. However, if you like this approach, you are welcome to add your own units into the dispatch table. –  Daniel W 2 days ago
    
I also made sure that any Quantity I used were among those stored locally rather than those that make a call to the Wolfram servers. I have to work behind a proxy sometimes and entering proxy settings during execution of coded solutions is not acceptable. –  Daniel W 2 days ago
    
I meant the new versions. Can you use the same approach to list the new built in units. I don't know of a way to extract them with, say, Names[Quantity[_, _]] --> Quantity::unknunit : Unable to interpret unit specification". What's your approach? –  alancalvitti 2 days ago
    
Yes, glad you mention the over the network units - which are slow even w/ no firewall. Ideally would like a list of both types. –  alancalvitti 2 days ago

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.