Message Boards Message Boards

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

Do arithmetic operations handle unit cancellation automatically?

Posted 7 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 7 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 7 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

Before making a plot I would always do an explicit conversion as it might break down in future versions. i.e. in future version it might recognize it automatically and then give you the 'wrong' units...

POSTED BY: Sander Huisman

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

Group Abstract Group Abstract