5
$\begingroup$

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")]}

$\endgroup$
1
  • 1
    $\begingroup$ Looks like this was what I was looking for: 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} $\endgroup$
    – Asas
    Apr 13, 2015 at 5:14

2 Answers 2

1
$\begingroup$

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")]}*)
$\endgroup$
1
  • $\begingroup$ 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 $\endgroup$
    – Asas
    Apr 12, 2015 at 0:48
1
$\begingroup$

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)

$\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.