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?
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$