10

To solve some PDE, I have to set NumericQ[n]=True on some symbol to get a solution, else Mathematica will not solve it.

When I tried ClearAll[n] later on, above command effect was not removed.

I need to clear it, so that later commands do not fail, since now doing n>0 will fail because I did NumericQ[n]=True before.

Doing ClearAll[n] have no effect. It still fails.

Here is a MWE

(*new kernel *)
NumericQ[n]=True;
n>0
   Greater::nord2: Comparison of n and 0 is invalid.

Ok, Mathematica does not like to compare n with zero. So I do

ClearAll[n];

And try again

n>0  (*still gives same error as above*)

I looked at OwnValues@n and UpValues@n and DownValues@n and SubValues@n and they all give {}

One way I found is to do NumericQ[n]=False; and this works. But I do not understand why ClearAll[n] does not work.

Thanks to comment below by Anjan Kumar Remove[n] also removes the effect.

My question is, why ClearAll[n] did not work?

Mathematica 11.3 on windows.

Nasser
  • 143,286
  • 11
  • 154
  • 359

1 Answers1

9

You can undo this using

NumericQ[n] =.

(Note that there is no space between the = and the .)

ClearAll does not work because the definition is not associated with the symbol n. It seems that in this case it is not even associated with the symbol NumericQ. I believe that assignment operators are specially overloaded for NumericQ, and trying to make an assignment will cause the system to store away this information somewhere else.

Remove works because it effectively destroys the symbol n. So wherever this information about n was stored, n will be removed from there.


Here's an example of how I imagine NumericQ might work. This may of course be entirely wrong, but it illustrates one possible reason for why ClearAll wouldn't remove this setting.

Unprotect[spec];
ClearAll[spec, registry];
registry = <||>;
spec /: (Set | SetDelayed)[spec[sym_Symbol], val : True | False] := (AssociateTo[registry, sym -> val]; val)
spec /: Unset[spec[sym_Symbol]] := (KeyDropFrom[registry, sym];)
spec[sym_Symbol] := Lookup[registry, sym, False]
Protect[spec];

Demonstration:

spec[x] = True
(* True *)

ClearAll[x]

spec[x]
(* True *)

registry
(* <|x -> True|> *)
Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263