3
$\begingroup$

When I enter this in Wolfram Mathematica V12:

0 Dogs + 5 Cats

Mathematica sensibly returns this:

5 Cats

BUT, if I using the Units and Quantity features in Mathematica V12, and enter this:

0 m + 37 m/s

Or in long form:

Quantity[0, "Meters"] + Quantity[37, "Meters"/"Seconds"]

I get:

 Quantity::compat: Meters/Seconds and Meters are incompatible units

I know the units are not compatible, but ZERO "Meters" is just 0 and should logically disappear from the equation just as "0 Dogs" disappears from the first example.

This bug breaks most symbol differentiation and integration.

I guess Wolfram really screwed up by not making Units part of V1 back in the 1980's, and instead added it as a glued on bag, in V12. Talk about "creeping featurism" ;-)

BTW, the only means I have found of controlling this horrible Mathematica functionality is with pages of ugly substitution rules like this:

removeUnitsOfZeroMagnitude[myExpression_] := Module[{},
  myExpression /. Quantity[0, "Meters"] -> 0 /. 
     Quantity[0, "Meters"/"Seconds"] -> 0 /. 
    Quantity[0, "Meters"/"Seconds"^2] -> 0 /. 
   Quantity[0, "Meters"/"Seconds"^3] -> 0
  ]

Of course, this ugly hack makes simple one line calculations, that were so beautiful and elegant in V4, now many lines of nasty, unreadable code.

Am I doing something wrong?

Is Mathematica V4 still for sale?

Thanks,

  • Joe

Example

Here is a more detailed image of the errors I am receiving in Mathematica V12 when trying to evaluate a simple integral. It seems to me in an evaluation like Times or Plus, or whatever, expressions of Units with ZERO magnitude should be removed, as one would do with simple pen and paper.

enter image description here

$\endgroup$
19
  • 3
    $\begingroup$ It would help if you could provide an actual example where you encounter this issue. In principle, differentiating a valid expression should again yield a valid expression $\endgroup$
    – Lukas Lang
    Commented Nov 29, 2020 at 15:04
  • 6
    $\begingroup$ Why do you think this behaviour is sensible? What about this 1m + (-1m + 37m/s)? Zero quantity should still be a part of space it belongs to and not just disappear, this breaks a lot of math stuff. If this expression comes from some computation then most certainly this computation is wrong. $\endgroup$
    – swish
    Commented Nov 29, 2020 at 15:59
  • 5
    $\begingroup$ What type of physical system are you trying to model where it makes sense to add meters and meters per second? Or dogs and cats for that matter? $\endgroup$
    – Jason B.
    Commented Nov 29, 2020 at 16:49
  • 4
    $\begingroup$ Voting to close. Questioning design decisions falls very much into the "opinion based" closure category, and this does not have a clear Mathematica question that can be answered. $\endgroup$ Commented Nov 29, 2020 at 16:57
  • 5
    $\begingroup$ I'd just like to second what @JasonB. is saying. If your differentiation/integration code is trying to add inconsistent units, your differentiation is simply wrong. The message is telling you something and you ignore it at your peril. $\endgroup$
    – b3m2a1
    Commented Nov 29, 2020 at 18:12

2 Answers 2

19
$\begingroup$

Not a solution but an extended comment.

You misunderstand the physical underpinnings of quantities and units.

Every physical quantity has an inherent margin of error, even if it is very small (negligible) and thus tacitly suppressed. The quantity 1 meter truly means approximately 1 meter, give or take a few millimeters/nanometers/Planck distances.

The quantity 0 meters means a distance that is consistent with zero within the margin of my measurement's error. This error margin carries units, even if the mean value is precisely zero. So we cannot cancel out the units, and 0 meters is not equal to 0 apples or 0 seconds. Democritus had already pretty much figured this out 2500 years ago.

Mathematically, we observe that the point 0 is of zero measure on the real axis, and is the only point $x$ where $x$ meters could be said to be equal to nothing. But physical quantities refer to intervals, not points, on the real axis, and therefore the unit can never be cancelled out in the way you suggest.

Mathematica's behavior in this matter is thus completely expected and normal.

$\endgroup$
7
  • 1
    $\begingroup$ Please ;-) I appreciate the technical cuteness, but you are missing the key point about simple mathematical behavior. The kind of basic mathematical example, as one might work on a piece of paper. You have to admit that the dog/cat example is a contradiction in basic behavior with the integral example. As a CS guy, I consider this a fundamental flaw in the overall design of Matheamtica. It is inconsistent. And the dog/car behavior is the only sensible one. $\endgroup$ Commented Nov 29, 2020 at 17:19
  • 2
    $\begingroup$ Again, Quantity[0, "Meters"] is not the mathematical zero but represents an interval of unknown (but probably small) size around zero. Mathematica treats it as such, which is sensible. $\endgroup$
    – Roman
    Commented Nov 29, 2020 at 18:35
  • $\begingroup$ Since I can't post an answer....As a mathematician, I don't agree that you cannot represent an exact number of units. I would assert Quantity[0, "m"] does this, whereas Quantity[0., "m"] with a Real scalar represents an approximate amount. Conceptually (to me), Quantity[x, "m"] lives in a 1D vector space $\langle u\rangle$ generated by $u=$ "m" that is disjoint from other vector spaces $\langle u'\rangle$ with different units. There is a unique bilinear product on the collection unit spaces, which includes dimensionless scalars, fixed by $1u_1\times 1u_2=1(u_1\times u_2)$.... $\endgroup$
    – Michael E2
    Commented Nov 30, 2020 at 16:14
  • $\begingroup$ As for a sum of quantities, which is not as ridiculous as other commenters make appear — consider "bundles of goods" in rational choice theory in economic — the question is how to represent a sum of quantities such as five cats plus four dogs. The standard representation is the direct product (or direct sum) of unit vector spaces, which is a Cartesian product when finite and in Mathematica would be represented by a vector List. The Plus representation the OP presents is in effect a disjoint sum of the unit spaces. I don't think Plus was designed to do this. $\endgroup$
    – Michael E2
    Commented Nov 30, 2020 at 16:17
  • 2
    $\begingroup$ "WR has chosen the physicist's view of units" -- Have they really? Reference? Especially concerning Quantity[1, "m"] representing an approximate quantity. To me Quantity[] works exactly according to the mathematical conception of a unit being a distinguished basis vector. I'm not suggesting that one can meaningfully add quantities with distinct units. I think we agree on that. In the math view, vectors can be added only when from the same vector space. Where we disagree is whether a "margin of error" model or a "distinct vector space" model explains it, and whether quantities can be exact. $\endgroup$
    – Michael E2
    Commented Nov 30, 2020 at 19:18
11
$\begingroup$

Is Mathematica V4 still for sale?

This answer proposes less drastic solutions than moving back for Version 4.

"Global" solution

For the problems you encounter and approach you take one -- not that good -- proposal is to redefine Quantity:

Unprotect[Quantity];
(*Quantity[x_,___]:=x;*)
Quantity[0, ___] := 0;
Protect[Quantity];

Remark 1: I have not checked or used the code above extensively -- it put it more as a "I share your pains" statement, and, sort of, to encourage other posts proposing solutions in that style.

Remark 2: With Quantity[x_,___]:=x; you can "remove" the Quantity functionalities from most computations. But that is also dangerous, because some built-in functions do rely on Quantity working properly. For example, WeatherData or FinancialData. Personally, 95% of the time after ingesting data with those functions I use data /. Quantity[x_, _] :> x.

enter image description here

Evaluation with Quantity re-defintion

Consider using this function instead of the "global" approach above (and/or your removeUnitsOfZeroMagnitude):

Clear[RemoveQuantity]
SetAttributes[RemoveQuantity, {HoldFirst}];
RemoveQuantity[myExpression_] := Block[{Quantity = #1 &}, myExpression];

enter image description here

$\endgroup$
4
  • $\begingroup$ I appreciate the clever response, but I think you will agree, that they are not really solutions. One, overloading the function name, is likely to make the whole Mathematica system unstable. And the second idea, of just removing the units entirely defeats the purpose of having units in the first place, no? $\endgroup$ Commented Nov 29, 2020 at 16:50
  • 2
    $\begingroup$ Quantity[...]/.Quantity[x_, _]:>x can just be done with QuantityMagnitude $\endgroup$
    – b3m2a1
    Commented Nov 29, 2020 at 18:09
  • 1
    $\begingroup$ @FrancisBush Yes, you are correct on both points. As I mentioned in my answer, I posted it mostly to express solidarity with your frustrations over Quantity. I do find the quantity family of functionalities too intrusive, and I did/do spend non-trivial amounts time to deal with unwanted outcomes caused by them. $\endgroup$ Commented Nov 29, 2020 at 18:33
  • 3
    $\begingroup$ I don't think adding a definition for Quantity[0,___] is likely to make the whole system unstable. As noted in the question, the Quantity stuff is an add-on, not part of the core of Mathematica, so this seems like a safe change. $\endgroup$ Commented Nov 29, 2020 at 22:49

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.