In Mathematica 12.2

Differences[{Quantity[1, "mm"], Quantity[1, "mm"]}]


{Quantity[0, "Millimeters"]}

In Mathematica 13.1, same expression gives


with no unit!

This introduces inconsistency. For example

 Differences[{Quantity[1, "mm"], Quantity[1, "mm"], Quantity[2, "mm"],
    Quantity[3, "mm"]}]

prompts errors in 13.1, while no problem in 12.2

In the documentaion of Differences, it says

Differences[list] is equivalent to ListConvolve[{1,-1},list].

But it is not!

ListConvolve[{1, -1}, {Quantity[1, "mm"], Quantity[1, "mm"]}]


{Quantity[0, "Millimeters"]}

What is changed for Differences in 13.1? It seems like a bug. Are there some other side effects of this potential change?


A patch I can think of is

Differences[x : {__Quantity}] := ListConvolve[{1, -1}, x];

But I do not know if Unprotect and Protect a built-in function will have undesirable side effect or not. Anyone knows?

  • $\begingroup$ As a workaround, Differences[list] /. {0 -> Quantity[0, QuantityUnit[list[[1]]]]} $\endgroup$
    – Bob Hanlon
    Commented Jul 21, 2022 at 15:21
  • $\begingroup$ @BobHanlon Thank you for comment. I just add my workround in my post. But I do not know if it is proper or not $\endgroup$
    – matheorem
    Commented Jul 21, 2022 at 16:50
  • 1
    $\begingroup$ Looks like a bug to me. 0 mm is definitely not the same as 0, see here for an extended comment. $\endgroup$
    – Roman
    Commented Jul 21, 2022 at 17:00
  • 1
    $\begingroup$ Minimal example: compare Quantity[1, "Millimeters"] - Quantity[1, "Millimeters"] with Subtract[Quantity[1, "Millimeters"], Quantity[1, "Millimeters"]] $\endgroup$
    – Jason B.
    Commented Jul 21, 2022 at 17:57
  • $\begingroup$ @JasonB. So we need one more patch for Subtract. I even guess Subtract and Differences are related in the internal. $\endgroup$
    – matheorem
    Commented Jul 22, 2022 at 1:22


Your Answer

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

Browse other questions tagged or ask your own question.