2

Due to some bug shown in Differences of Quantities gives different result in different Mathematica version, I add an additional definition to Subtract. But I found it will severely slow down Szabolcs' BoolEval function.

A fresh Mathematica kernel, run

array = RandomReal[1, 1000000];
Pick[array, ResourceFunction["BoolEval"][array > 0.5], 
   1]; // RepeatedTiming

gives

{0.0163395, Null}

Another fresh Mathematica kernal, run

Unprotect[Subtract];
Subtract[x_Quantity, y_Quantity] := x - y;
Protect[Subtract];
array = RandomReal[1, 1000000];
Pick[array, ResourceFunction["BoolEval"][array > 0.5], 
   1]; // RepeatedTiming

gives

{0.521434, Null}

Why does adding an additional definition affect so much? What is more, how to fix Subtract quantity bug without affecting BoolEval?

update

Now I know how to fix Subtract quantity bug without affecting BoolEval. In the BoolEval package, replace all Subtract[a,b] and similar with a-b, then it will not affected by new definition of Subtract. But I still do not know why adding an additional Subtract definition affect so much?

matheorem
  • 17,132
  • 8
  • 45
  • 115
  • This is related, I guess: https://mathematica.stackexchange.com/q/40927/1871 – xzczd Jul 14 '23 at 12:14
  • @xzczd Thank you so much for your link. I forgot Subtract is faster than -. That is why BoolEval package use Subtract. And that makes my updated fix not perfect in terms of performance. Sad : ( – matheorem Jul 14 '23 at 12:35
  • 2
    How does Unprotect[Quantity]; Quantity /: Subtract[x_Quantity, y_Quantity] := x - y; Protect[Quantity]; instead behave? – Carl Woll Jul 14 '23 at 13:30
  • @CarlWoll Work like a charm! Thank you so much! – matheorem Jul 14 '23 at 13:49

0 Answers0