5

I have a list of quantities with varying units (a short example list is shown below):

{Quantity[1, 1/("Millimoles" "Seconds")], Quantity[3, 1/(("Meters")^3 "Seconds")], Quantity[7, ("Meters")^3/("Seconds")]}

These units were produced by using Solve on equations with units. However, Solve spits out the base units of Meters^3 for volume, when I want Liters. Is there a way to convert every instance of meter^3 to liter in the list with a single function (or rule)?

A big problem I have run into is that the list contains some quantities with Meters^3, some with Meters^-3, and some with no volume units at all. I am not sure what units the output of Solve will give me either, but I just want to convert all Meters^3 and Meters^-3 to Liters.

The output I am hoping for is {Quantity[1, 1/("Millimoles" "Seconds")], Quantity[3/1000, 1/("Liters"*"Seconds")], Quantity[7000, ("Liters")/("Seconds")]}

Asas
  • 198
  • 7
  • 1
    Looks like this was what I was looking for: http://mathematica.stackexchange.com/a/21984/21923 I just added an additional item to the list to convert from length^3 to volume units. {"LengthUnit", x_?(Mod[#, 3] == 0 &)} :> {"Liters", x/3} – Asas Apr 13 '15 at 05:14

2 Answers2

1

You could try something like this:

UnitConvert[#, QuantityUnit[#] /.
    {
     "Meters"^3 -> "Liters",
     "Meters"^-3 -> 1/"Liters"
     }] & /@ list

{Quantity[1, 1/("Millimoles" "Seconds")], Quantity[3/1000, 1/("Liters" "Seconds")], Quantity[7000, ("Liters")/("Seconds")]}

(But you'll have to extend it if you have units like Liters * Meters)

Niki Estner
  • 36,101
  • 3
  • 92
  • 152
1

Here is one clunky and slow answer. I am sure there are better options, although setting up If clauses with CompatibleUnitsQ can give the output of your sample list.

If[CompatibleUnitQ[#, "Liters per second"],
   UnitConvert[#, "Liters per second"],
   If[CompatibleUnitQ[#, "per Liters per second"], 
    UnitConvert[#, "per Liters per second"], #]] & /@ list

(* {Quantity[1, 1/("Millimoles" "Seconds")], Quantity[3/1000, 1/("Liters"
 "Seconds")], Quantity[7000, ("Liters")/("Seconds")]}*)
bobthechemist
  • 19,693
  • 4
  • 52
  • 138
  • Unfortunately this list could have a lot of different types of units in it, from L/s to L/mmol*s or many combinations of L, mmol and s. I'd prefer to not have a huge nested If statement – Asas Apr 12 '15 at 00:48