Message Boards Message Boards

1
|
1968 Views
|
5 Replies
|
6 Total Likes
View groups...
Share
Share this post:

Inconsistent behavior and unexpected loss of units with Quantity

Posted 1 year ago

I'm having some trouble with mathematics involving the Quantity object. The notebook I give below describes what is happening in a minimal example:

Am I misunderstanding something about the Quantity object or its application? To me, the outputs of (a) and (b) should be identical because the units are equivalent, and it seems like the language is aware of this (from the last line of the notebook). I'd appreciate any input on this, especially if I'm misunderstanding how Quantity should work in equations! I am using Mathematica 13.0.1.0 on Linux. Thanks in advance!

POSTED BY: Robben Migacz
5 Replies

This looks like a bug to me, maybe the simplest incarnation would be

In[10]:= Quantity[Log[x Quantity[1, "Liters"]], "Kilometers"] / Quantity[1, "Meters"]

Out[10]= 1000 (-3 Log[2] - 3 Log[5] + Log[x])

Where did the "Liters" go? You should report bugs here.

On a side note, you should try to use unit standard names when possible. Every time you type

In[11]:= Quantity[x, "volt"] // InputForm

Out[11]//InputForm= Quantity[x, "Volts"]

the system has to interpret the string "volt" to find the canonical unit "Volts". If you just use "Volts" from the start then computations will be faster, even if only a small amount.

POSTED BY: Jason Biggs
Posted 1 year ago

Thank you for trying to reproduce the issue, and for your comment about the unit names. I think your example illustrates this well and shows that the issue is more universal. I have included it, along with an equivalent line where a division by Quantity[1, "Kilometers"] is performed and the correct output is given, in a message to Wolfram Technical Support.

POSTED BY: Robben Migacz
Posted 1 year ago

The expression testFunction[testArgument]/Quantity[1, "joule/coulomb"] is evaluated before the ReplaceAll is evaluated.

testFunction[Quantity[1, "molar"]]/Quantity[1, "joule/coulomb"]

and

testFunction[Quantity[1, "molar"]]/Quantity[1, "volt"]

both give 0.

POSTED BY: Eric Rimbey
Posted 1 year ago

Thank you for the quick response. I encountered this issue in a notebook that does not use ReplaceAll, so there must be some other rule for the order of evaluation that I'm not aware of. I was able to work around this by replacing someQuantity[someValue, someUnits] with UnitConvert[someQuantity[someValue, someUnits], baseUnits]; everything seems to work as expected with this change.

POSTED BY: Robben Migacz
Posted 1 year ago

Well, without knowing the actual code you're working with, I can't provide an explanation or suggestion.

POSTED BY: Eric Rimbey
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