11
$\begingroup$

In Mathematica 12.2

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

gives

{Quantity[0, "Millimeters"]}

In Mathematica 13.1, same expression gives

{0}

with no unit!

This introduces inconsistency. For example

StandardDeviation@
 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"]}]

gives

{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?

Update

A patch I can think of is

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

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

$\endgroup$
5
  • $\begingroup$ As a workaround, Differences[list] /. {0 -> Quantity[0, QuantityUnit[list[[1]]]]} $\endgroup$
    – Bob Hanlon
    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
    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
    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.
    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
    Jul 22, 2022 at 1:22

0

Your Answer

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

Browse other questions tagged or ask your own question.