15
$\begingroup$

Bug introduced in 9.0 or earlier and persisting through 11.2 or later


Why does this strange behavior occur when dealing with units and quantities?

A[t_] := A0 Exp[-b t / (2m)]                 

FullSimplify[A[Quantity[0, "Seconds"]]]      
 A0
 --                                            (* wrong; should be A0 *)
 2
b = Quantity[7, "Newtons"*"Seconds"/"Meters"]
7 second newtons per meter
m = Quantity[10, "Kilograms"]                
10 kilograms
FullSimplify[A[Quantity[0, "Seconds"]]]      
A0                                            (* right *)

I can imagine it spitting out an error because Exp[Quantity[0, "Seconds"]] doesn't make sense, but where does the 1/2 come from?

I don't think it's from the 2 in -b t / (2 m); changing this to -b t / (7m) gives the same result.

$\endgroup$
6
  • 2
    $\begingroup$ Has to be a bug. $\endgroup$ Commented Jan 11, 2015 at 6:21
  • 2
    $\begingroup$ The bug seems to be in FullSimplify (which always tries harder). Compare A[Quantity[0, "Seconds"]]; Simplify@A[Quantity[0, "Seconds"]]; FullSimplify@A[Quantity[0, "Seconds"]] screen shot: !Mathematica graphics $\endgroup$
    – Nasser
    Commented Jan 11, 2015 at 9:22
  • 2
    $\begingroup$ I marked this as a bug. A reduced example: x*E^Quantity[0, "Feet"] // FullSimplify $\endgroup$
    – Mr.Wizard
    Commented Jan 11, 2015 at 10:12
  • 1
    $\begingroup$ @Mr.Wizard Well honestly, what do you expect from E^Quantity[0, "Feet"]? Exponents are dimensionless quantities, and raising something to a number of feet (even if it's zero) doesn't really make sense, so I wouldn't expect Mathematica to do anything sensible (in fact, an error would be nice). That being said, in the equation in the example, the remaining variables are still able to cancel out the units, so I don't really know what to expect in that case. $\endgroup$ Commented Jan 11, 2015 at 15:19
  • 2
    $\begingroup$ I still occasionally use the deprecated package <<PhysicalConstants`, so if this (apparent) bug is cause for worry, you can always switch to the old package, which I've never had trouble with. $\endgroup$ Commented Jan 11, 2015 at 15:41

1 Answer 1

6
$\begingroup$

It is because a transformation rule in the SimplifyDump context is written without taking into account that Mathematica currently doesn't simplify zeroed Quantitiy expressions to a plain zero, which prevents the rule from detecting a division by zero. This happens even in the case where the Quantity has a correct unit, such as radians. Other than these (fatal) failings, the transformation appears correct.

In[1]:= xpr = z Exp@Quantity[0, "rad"]
ExpToTrig@xpr
FullSimplify@xpr; SimplifyDump`CosToSinRules[[-4]]
%% /. %

Out[1]= E^Quantity[0, "Radians"] z

Out[2]= z Cosh[Quantity[0, "Radians"]] + z Sinh[Quantity[0, "Radians"]]

Out[3]= Cosh[
  SimplifyDump`x_?(Internal`LiterallyAbsentQ[#1, {Complex, 
       DirectedInfinity}] &)] :> 
 With[{SimplifyDump`e = Sinh[2 SimplifyDump`x]/(
    2 Sinh[SimplifyDump`x])}, 
  SimplifyDump`e /; SimplifyDump`nonsingular[SimplifyDump`e]]

Out[4]= z/2 + z Sinh[Quantity[0, "Radians"]]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.