(The code in this answer is tested using Mathematica 10.0, in which MixedUnit
and MixedMagnitude
are not available. However, the online help is pretty clear, and I explain why these functions are needed.)
When two quantities are summed, it seems like a reasonable default to convert them to the same unit, as otherwise one would quickly end up with expressions involving (say) meters, nanometers, Angstroms, etc., which would have to explicitly be converted to some target unit in order to have any idea what the magnitude actually is.
So, given this default, t1 = 1 Quantity["Hours"] + 30 Quantity["Minutes"]
gives Quantity[90, "Minutes"]
(by the way, the choice of unit in sums seems to be such as to make (the log of) the magnitude as close to (the log of) 30
as possible), and t2 = 30 Quantity["Minutes"] + 1 Quantity["Seconds"]
gives Quantity[1801/60, "Minutes"]
. Their difference t1-t2
is naturally Quantity[3599/60, "Minutes"]
.
This result can be converted to seconds using UnitConvert[t1-t2, "Seconds"]
, but one may want it to be split in terms of minutes and seconds. It would have been possible to implement UnitConvert
such that UnitConvert[t1-t2, {"Minutes","Seconds"}]
would split t1-t2
. However, UnitConvert
was made Listable
, which enables nice constructions such as UnitConvert[{t1,t2,t1-t2}, "Seconds"]
to convert several times into seconds, or UnitConvert[t1,{"Years","Hours","Nanoseconds"}]
to see a time expressed in different units, or UnitConvert[{
quantities
},{
units
}]
to convert several quantities to several units (same number of quantities and units). This behaviour is consistent with very many Mathematica builtins.
Since UnitConvert[t1-t2, {"Minutes","Seconds"}]
already is defined as giving t1-t2
twice, once in minutes and once in seconds, we need another way of splitting. Furthermore, it would be better if the split result Quantity[59, "Minutes"] + Quantity[59, "Seconds"]
did not automatically recombine as soon as it is evaluated again (e.g., copied and pasted into a new calculation/table/etc.) So the output as a sum of Quantity
is not great (one could have had a bunch of Hold
, but that's horrible). The choice of expressing the split result as Quantity[MixedMagnitude[{59,59}], MixedUnit[{"Minutes", "Seconds"}]]
is quite long-winded, I agree, but it has the advantages of preserving the fact that Quantity
have 2 arguments in all cases (plus options), the first being the magnitude and the second the unit. This also gives a natural way to express that some quantity should be split in terms of several units: simply use MixedUnit[{"Minutes", "Seconds"}]
as the unit argument of UnitConvert
.
One way to implement a short-hand to express things like 1 hour and 30 minutes is the following (I used &&
as a notation for a sum that preserves units using MixedUnit
and MixedMagnitude
; however, I cannot test because my version of Mathematica is too old):
Unprotect[Quantity];
HoldPattern[And[x__Quantity]] ^:=
Quantity[MixedMagnitude[QuantityMagnitude[{x}]],
MixedUnit[QuantityUnit[{x}]]];
Protect[Quantity];
1 Quantity["Hours"] && 30 Quantity["Minutes"]
(*=> Quantity[MixedMagnitude[{1, 30}], MixedUnit[{"Hours", "Minutes"}]]*)
By the way, it is very easy to define shorthands for Quantity["Hours"]
etc by doing
Clear[Mili, Kilo];
Mili = 1/1000;
Kilo = 1000;
Protect[Mili, Kilo];
Scan[(Clear[#]; # = Quantity[ToString[#]]; Protect[#]) &,
{Meters, Feet, Grams, Pounds, Kelvins, Fahrenheit}];
{UnitConvert[0 Fahrenheit, Kelvins], 2 Kilo Grams / (Mili Meters), 2 Feet + 3 Meters + 1 Kilo Feet}
(*=> {Quantity[45967/180, "Kelvins"], Quantity[2000000, ("Grams")/("Meters")], Quantity[192756/625, "Meters"]}*)
5*kg*10*m /. kg -> Nw/m
with the effect50 Nw
. The advantage is that the units notations are shorter and closer to the ones we use in real "scientific" life. $\endgroup$Miscellaneous`Units`
package), I'm hard-pressed to think of a unit example where $0$ was "special". $\endgroup$