Bug introduced in 8 or earlier and fixed in 10.4.0

Bug isn't present in version 5.2

I don't think I quite understand how Except works.

I want to define $f(a,b) = (a-b)\ln(a-b)$, with the special case $f(a,a)=0$. Except, the function shouldn't evaluate if either $a$ or $b$ is zero.

I tried:

f[Except[0, a_], Except[0, a_]] := 0;
f[Except[0, a_], Except[0, b_]] := (a - b) Log[a - b];

But for some reason DownValues[f] only reports one definition:

(* { HoldPattern[ f[Except[0, a_], Except[0, b_]]] :> (a - b) Log[a - b] } *)

This is bad because if the user inputs f[x,x], I get Infinity::indet errors.

Question What did I do wrong, and how do I make definitions with Except?

  • $\begingroup$ I'm tempted to say this is a bug. In any way, this is not a case of one pattern having more priority than the other or so. If you change the order of the definitions you'll see the last one is always kept. It seems that Mathematica (incorrectly) sees both patterns as equivalent. $\endgroup$ – Sjoerd C. de Vries Dec 12 '15 at 12:27
  • 1
    $\begingroup$ I agree that this is a bug. Internal`ComparePatterns returns "Equivalent" for the two cases which is clearly not correct. $\endgroup$ – Simon Woods Dec 12 '15 at 12:49
  • $\begingroup$ @SimonWoods too bad this is a bug. Add the 'bugs' tag? $\endgroup$ – QuantumDot Dec 14 '15 at 8:49
  • 1
    $\begingroup$ Sent bug report to Wolfram $\endgroup$ – QuantumDot Dec 14 '15 at 23:28
  • 1
    $\begingroup$ With Mathematica 5.2 I observe the expected behavior but with 8.0.4 I reproduce the bug. $\endgroup$ – Alexey Popkov Dec 15 '15 at 8:04

Another way to get two definitions is to only use Except on the first argument for the case where the arguments are identical.


f[Except[0, a_], a_] := 0

f[Except[0, a_], Except[0, b_]] := (a - b) Log[a - b]

Annoyingly it doesn't matter the order for the definitions it comes out like this:

(* {HoldPattern[f[Except[0, a_], Except[0, b_]]] :> (a - b) Log[a - b], 
 HoldPattern[f[Except[0, a_], a_]] :> 0} *)

So in order to get the right behaviour you have to do

DownValues[f] = Reverse@DownValues@f
(* {HoldPattern[f[Except[0, a_], a_]] :> 0, 
 HoldPattern[f[Except[0, a_], Except[0, b_]]] :> (a - b) Log[a - b]} *)


f[3, 1]


2 Log[2]
| improve this answer | |
  • $\begingroup$ if you do f[a_, Except[0, a_]] := 0 it takes care of the DownValue order issue. $\endgroup$ – george2079 Dec 14 '15 at 20:22

It seems that f[Except[0, a_], Except[0, a_]] isn't considered doubled argument while this

f[a : Except[0], a : Except[0]] is. So the former isn't considered special case and is overwritten by your next definition.

I can't explain properly but seems to be expected.

f[a_, a_] := 0  (*or f[a : Except[0], a : Except[0]] if f[0,0] should give f[0,0]*)
f[a : Except[0], b : Except[0]] := {a, b}

f[1, 2]
f[0, 1]
f[0, 0]
f[1, 1]
| improve this answer | |
  • $\begingroup$ Yes, that's what I need, thanks. Can you explain what went wrong with what I did, too? $\endgroup$ – QuantumDot Dec 12 '15 at 11:08
  • $\begingroup$ @QuantumDot I edited the answer, still without full explanation. $\endgroup$ – Kuba Dec 12 '15 at 11:40
  • 3
    $\begingroup$ Another workaround is to use a trivial condition to stop the definition being overwritten, e.g. f[Except[0, a_], Except[0, a_]] /; True := 0 $\endgroup$ – Simon Woods Dec 12 '15 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.