Group Abstract Group Abstract

Message Boards Message Boards

0
|
7.7K Views
|
4 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Do arithmetic operations handle unit cancellation automatically?

Posted 9 years ago

According to the Mathematica documentation "Arithmetic operations also handle unit cancellation automatically". However the following code does not convert automatically. It needs to be converted explicitly.

v = 10 Quantity[1, "Electronvolts"]
\[HBar] = UnitConvert[Quantity[1, "ReducedPlanckConstant"]]
Subscript[m, e] = UnitConvert[Quantity[1, "ElectronMass"]]
z = Sqrt[2 Subscript[m, e] v]/\[HBar] (* returns unconverted units *)
zz = UnitConvert[Sqrt[2 Subscript[m, e] v]/\[HBar], "SI"] (* returns correct units (per meter) *)

Is this a bug in the units package or am I expecting too much when calculating with units?

POSTED BY: Mike Luntz
4 Replies
Posted 9 years ago

Thanks again Sander for the suggestion to explicitly convert to ensure that calculations remain compatible with future versions. Your insights are thoroughly appreciated.

POSTED BY: Mike Luntz
POSTED BY: Sander Huisman
Posted 9 years ago

Thanks for the information Sander, including the hint that the default unit conversion is to SI. The reason for maintaining v in eV was to use the value, including units, in a plot label. I was hoping to maintain one value of v for both computational and display purposes but found that the mixed units interfered with the plot routine. I eventually did an explicit unit conversion within my computation.

POSTED BY: Mike Luntz

I believe this only holds for simpler cases like:

Quantity[1, "Meters"/"Seconds"] Quantity[2, "Seconds"]

I think it is confused by the ElectronVolts. If you change line 1 to:

v = 10 Quantity[1, "Electronvolts"] // UnitConvert

it also works automatically. An explicit UnitConvert is sometimes necessary I found out. I'm not sure if it should be considered as a bug or as a lack of implementation. N.b. If you don't use the second argument "SI" it will do the "SIBase" by default, works most of the times for me.

POSTED BY: Sander Huisman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard