3

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

Michael E2
  • 235,386
  • 17
  • 334
  • 747
  • 3
    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 – Lukas Lang Nov 29 '20 at 15:04
  • 6
    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. – swish Nov 29 '20 at 15:59
  • 1
    Please provide the actual Mathematica code for this screenshot: https://i.stack.imgur.com/KpDKz.jpg . (The one edited my answer with.) – Anton Antonov Nov 29 '20 at 16:11
  • Using replacement rules you only need one to remove zero quantities: Quantity[0|0., ___] :> 0 – Bob Hanlon Nov 29 '20 at 16:12
  • 2
    This works btw: Integrate[v t, Quantity[t, "Seconds"]]. So maybe define your time as t = Quantity[\[FormalT], "Seconds"] – swish Nov 29 '20 at 16:17
  • Again: copy and paste your Mathematica code here, do not just post screenshots. – Anton Antonov Nov 29 '20 at 16:47
  • 5
    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? – Jason B. Nov 29 '20 at 16:49
  • 1
    @JasonB I have added apples and oranges, and even pears with considerable success. It was an excellent sangria... – Daniel Lichtblau Nov 29 '20 at 16:53
  • 4
    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. – Daniel Lichtblau Nov 29 '20 at 16:57
  • 5
    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. – b3m2a1 Nov 29 '20 at 18:12
  • Here is a related discussion: "Is Quantity necessary?" – Anton Antonov Nov 29 '20 at 18:48
  • 1
    Even on paper numbers should not just disappear. The result of the integral should have the same units as the other numbers, so you're adding 0 m + 37 m, not numbers with different units. I do think the fact that Integrate["0 m/s^2", t] = "0 m/s^2" and "0 m/s^2" Integrate[1, t] = t("0 m/s^2") is problematic. Since I think this only happens when integrating a constant of 0, you can pull it out of the integral: a Integrate[1, t, t], or use explicit integration limits as workarounds. – MassDefect Nov 29 '20 at 20:16
  • @DanielLichtblau - I’d like the opportunity to answer this question. The simple solution to integrating constants is to leave them undefined, integrate the expression, and substitute the constants into the integral. But the underlying issue is result of a zero-quantity in integration, such as \[Integral]\[Integral]Quantity[0.,"Meters"/"Seconds"^2]\[DifferentialD]t\[DifferentialD]t versus \[Integral]\[Integral]HoldForm[Quantity[0.,"Meters"/"Seconds"^2]]\[DifferentialD]t\[DifferentialD]t. In the first integral, the t^2 term disappears. This missing term causes the Quantity mismatches. – creidhne Nov 29 '20 at 22:06
  • @DanielLichtblau - Where does this lead? HoldForm with r0, v0 and a, and ReleaseHold after the integral solves the problem. – creidhne Nov 29 '20 at 22:17
  • @creidhne You may be interest in providing an answer to OP’s follow up question on this then. That one contained an answerable problem. – MarcoB Nov 30 '20 at 14:27
  • @MarcoB - Thank you. The new question is a specific issue; it’s an X-Y problem where my answer doesn’t apply. I’ll let this one stand as is. I appreciate your follow-up. – creidhne Nov 30 '20 at 16:16
  • @creidhne Curious to see your answer! I voted to reopen. – MarcoB Nov 30 '20 at 17:08
  • I avoid Quantity[] as much as possible but this seems ok, no?: https://i.stack.imgur.com/0e7B4.png – Michael E2 Nov 30 '20 at 18:15
  • I doubt that you really want V4, but you may want this: https://library.wolfram.com/infocenter/ID/5511/ . I still use it. I do not know why they threw away the Convert command. I only use the newer versions if I am copying someone else's examples. You can browse the infocenter for all kinds of stuff that still works, although a lot doesn't. – Bill Watts Dec 05 '20 at 00:03

2 Answers2

19

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.

Roman
  • 47,322
  • 2
  • 55
  • 121
  • 1
    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. – Francis Bush Nov 29 '20 at 17:19
  • 2
    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. – Roman Nov 29 '20 at 18:35
  • 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)$.... – Michael E2 Nov 30 '20 at 16:14
  • 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. – Michael E2 Nov 30 '20 at 16:17
  • There are gaps, like the distributive law: 0. (Quantity[1, "m"] + Quantity[2, "m/s"]) vs. 0. Quantity[1, "m"] + 0. Quantity[2, "m/s"]. As I said, I don't think Plus was designed to do this. – Michael E2 Nov 30 '20 at 16:18
  • @MichaelE2 WR has chosen the physicist's view of units, not the mathematician's; you can agree or disagree with this choice. There are advantages and disadvantages on both ends, and choices need to be made. In the physicist's view, adding two objects with different units is a "syntax error" and makes no sense. If you want to define a mathematician's addition operation that accommodates divergent units, that's fine; but that's not how physical units work. Physical units have a real-world correspondence to satisfy (through the measurement process) and cannot be redefined ad lib. – Roman Nov 30 '20 at 18:48
  • 2
    "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. – Michael E2 Nov 30 '20 at 19:18
11

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

Anton Antonov
  • 37,787
  • 3
  • 100
  • 178
  • 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? – Francis Bush Nov 29 '20 at 16:50
  • 2
    Quantity[...]/.Quantity[x_, _]:>x can just be done with QuantityMagnitude – b3m2a1 Nov 29 '20 at 18:09
  • 1
    @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. – Anton Antonov Nov 29 '20 at 18:33
  • 3
    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. – Simon Woods Nov 29 '20 at 22:49