2
$\begingroup$

I am having trouble with UnitConvert when the QuantityMagnitude contains an embedded Quantity of its own. Here's an example of what happens, read on for my specific application. I would expect the last two outputs to be equal, but they are not.

Input:

Quantity[f[Quantity[x, "Meters"]], "Seconds"]
UnitConvert[%, "Hours"]
QuantityMagnitude[%] // FullForm
%[[2, 1]]
UnitConvert[%, Quantity["Feet"]]
UnitConvert[Quantity[x, "Meters"], Quantity["Feet"]]

Output:

f[x m] s
f[Quantity[x,CalculateUnits`UnitCommonSymbols`Meters]]/3600 h
Times[Rational[1,3600],f[Quantity[x,CalculateUnits`UnitCommonSymbols`Meters]]]
Quantity[x,CalculateUnits`UnitCommonSymbols`Meters]
UnitConvert[Quantity[x,CalculateUnits`UnitCommonSymbols`Meters],1 ft]
(1250 x)/381 ft

I have a rule defined to work on only Quantity parameters of the correct units, like so, which does some basic unit conversion and passes its magnitude to a numeric function:

f[Quantity[x_, ("Meters")^-1]] :> 
 f2[QuantityMagnitude@
   UnitConvert[Quantity[x, ("Meters")^-1], ("Angstroms")^-1]]

The result is dimensionless (f2 is looking up a ratio at some physics wavenumber).

I then multiply that dimensionless result by a Quantity with complicated units for a later calculation. I can simplify the complicated units using UnitConvert, which can only be done if the whole expression is a Quantity, so I bring the f[x] inside to multiply the value of the complicated units (see my earlier question here). But when I try to simplify the complicated units using UnitConvert, it replaces the "Meters" unit with the symbol

CalculateUnits`UnitCommonSymbols`Meters

which I suspect is never meant to be seen. In any case, the Quantity that it is a part of no longer works as a regular Quantity with functions like UnitConvert or even QuantityMagnitude.

Is this a bug? What can I do to work around it?

$\endgroup$
1
  • $\begingroup$ In Mathematica 10.0.2.0, the last two outputs are equal. More specifically: I do not see the CalculateUnits``UnitCommonSymbols``Meters anywhere in my output. Which version are you using? Maybe you need to try it in a new Mathematica session to avoid possibly conflicting definitions elsewhere. $\endgroup$
    – Jinxed
    Mar 11, 2015 at 6:39

1 Answer 1

2
$\begingroup$

I get that same odd result w/v9

you might try something like this as a workaround..

 unitConvert[Quantity[mag_, unit_], tounit_] :=
      Quantity[ QuantityMagnitude[#]  mag  ,
           QuantityUnit[#] ] &@ UnitConvert[Quantity[1, unit], tounit]

 unitConvert[Quantity[f[Quantity[x, "Meters"]], "Seconds"], "Hours"]

Quantity[f[Quantity[x, "Meters"]]/3600, "Hours"]

$\endgroup$
1
  • $\begingroup$ Yes, I'm using 9.0.2, so I suppose it is a bug that's been fixed in 10. That work around works well enough, just a shame that it needs to be used. $\endgroup$
    – Widjet
    Mar 12, 2015 at 3:54

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.